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 (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24 */
25/*
26 * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
27 * Copyright 2016 Argo Technologies SA
28 * Copyright 2019 Joyent, Inc.
29 */
30
31/*
32 * SATA Framework
33 * Generic SATA Host Adapter Implementation
34 */
35
36#include <sys/conf.h>
37#include <sys/file.h>
38#include <sys/ddi.h>
39#include <sys/sunddi.h>
40#include <sys/modctl.h>
41#include <sys/cmn_err.h>
42#include <sys/errno.h>
43#include <sys/thread.h>
44#include <sys/kstat.h>
45#include <sys/note.h>
46#include <sys/sysevent.h>
47#include <sys/sysevent/eventdefs.h>
48#include <sys/sysevent/dr.h>
49#include <sys/taskq.h>
50#include <sys/disp.h>
51#include <sys/sdt.h>
52
53#include <sys/sata/impl/sata.h>
54#include <sys/sata/sata_hba.h>
55#include <sys/sata/sata_defs.h>
56#include <sys/sata/sata_cfgadm.h>
57#include <sys/sata/sata_blacklist.h>
58#include <sys/sata/sata_satl.h>
59
60#include <sys/scsi/impl/spc3_types.h>
61
62/*
63 * FMA header files
64 */
65#include <sys/ddifm.h>
66#include <sys/fm/protocol.h>
67#include <sys/fm/util.h>
68#include <sys/fm/io/ddi.h>
69
70/* Debug flags - defined in sata.h */
71int	sata_debug_flags = 0;
72int	sata_msg = 0;
73
74/*
75 * Flags enabling selected SATA HBA framework functionality
76 */
77#define	SATA_ENABLE_QUEUING		1
78#define	SATA_ENABLE_NCQ			2
79#define	SATA_ENABLE_PROCESS_EVENTS	4
80#define	SATA_ENABLE_PMULT_FBS		8 /* FIS-Based Switching */
81int sata_func_enable =
82	SATA_ENABLE_PROCESS_EVENTS | SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ;
83
84/*
85 * Global variable setting default maximum queue depth (NCQ or TCQ)
86 * Note:minimum queue depth is 1
87 */
88int sata_max_queue_depth = SATA_MAX_QUEUE_DEPTH; /* max NCQ/TCQ queue depth */
89
90/*
91 * Currently used default NCQ/TCQ queue depth. It is set-up during the driver
92 * initialization, using value from sata_max_queue_depth
93 * It is adjusted to minimum supported by the controller and by the device,
94 * if queueing is enabled.
95 */
96static	int sata_current_max_qdepth;
97
98/*
99 * Global variable determining the default behavior after device hotpluggin.
100 * If non-zero, the hotplugged device is onlined (if possible) without explicit
101 * IOCTL request (AP_CONFIGURE).
102 * If zero, hotplugged device is identified, but not onlined.
103 * Enabling (AP_CONNECT) device port with an attached device does not result
104 * in device onlining regardless of the flag setting
105 */
106int sata_auto_online = 0;
107
108#ifdef SATA_DEBUG
109
110#define	SATA_LOG_D(args)	sata_log args
111uint64_t mbuf_count = 0;
112uint64_t mbuffail_count = 0;
113
114sata_atapi_cmd_t sata_atapi_trace[64];
115uint32_t sata_atapi_trace_index = 0;
116int sata_atapi_trace_save = 1;
117static	void sata_save_atapi_trace(sata_pkt_txlate_t *, int);
118#define	SATAATAPITRACE(spx, count)	if (sata_atapi_trace_save) \
119    sata_save_atapi_trace(spx, count);
120
121#else
122#define	SATA_LOG_D(args)	sata_trace_log args
123#define	SATAATAPITRACE(spx, count)
124#endif
125
126#if 0
127static void
128sata_test_atapi_packet_command(sata_hba_inst_t *, int);
129#endif
130
131#ifdef SATA_INJECT_FAULTS
132
133#define		SATA_INJECT_PKT_FAULT	1
134uint32_t	sata_inject_fault = 0;
135
136uint32_t	sata_inject_fault_count = 0;
137uint32_t	sata_inject_fault_pause_count = 0;
138uint32_t	sata_fault_type = 0;
139uint32_t	sata_fault_cmd = 0;
140dev_info_t	*sata_fault_ctrl = NULL;
141sata_device_t	sata_fault_device;
142
143static	void sata_inject_pkt_fault(sata_pkt_t *, int *, int);
144
145#endif
146
147#define	LEGACY_HWID_LEN	64	/* Model (40) + Serial (20) + pad */
148
149static char sata_rev_tag[] = {"1.46"};
150
151/*
152 * SATA cb_ops functions
153 */
154static	int sata_hba_open(dev_t *, int, int, cred_t *);
155static	int sata_hba_close(dev_t, int, int, cred_t *);
156static	int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *,	int *);
157
158/*
159 * SCSA required entry points
160 */
161static	int sata_scsi_tgt_init(dev_info_t *, dev_info_t *,
162    scsi_hba_tran_t *, struct scsi_device *);
163static	int sata_scsi_tgt_probe(struct scsi_device *,
164    int (*callback)(void));
165static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *,
166    scsi_hba_tran_t *, struct scsi_device *);
167static	int sata_scsi_start(struct scsi_address *, struct scsi_pkt *);
168static	int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *);
169static	int sata_scsi_reset(struct scsi_address *, int);
170static	int sata_scsi_getcap(struct scsi_address *, char *, int);
171static	int sata_scsi_setcap(struct scsi_address *, char *, int, int);
172static	struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *,
173    struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t),
174    caddr_t);
175static	void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *);
176static	void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *);
177static	void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *);
178
179/*
180 * SATA HBA interface functions are defined in sata_hba.h header file
181 */
182
183/* Event processing functions */
184static	void sata_event_daemon(void *);
185static	void sata_event_thread_control(int);
186static	void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst);
187static	void sata_process_pmult_events(sata_hba_inst_t *, uint8_t);
188static	void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *);
189static	void sata_process_pmdevice_reset(sata_hba_inst_t *, sata_address_t *);
190static	void sata_process_port_failed_event(sata_hba_inst_t *,
191    sata_address_t *);
192static	void sata_process_port_link_events(sata_hba_inst_t *,
193    sata_address_t *);
194static	void sata_process_pmport_link_events(sata_hba_inst_t *,
195    sata_address_t *);
196static	void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *);
197static	void sata_process_pmdevice_detached(sata_hba_inst_t *,
198    sata_address_t *);
199static	void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *);
200static	void sata_process_pmdevice_attached(sata_hba_inst_t *,
201    sata_address_t *);
202static	void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *);
203static	void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *);
204static	void sata_process_target_node_cleanup(sata_hba_inst_t *,
205    sata_address_t *);
206static	void sata_process_device_autoonline(sata_hba_inst_t *,
207    sata_address_t *saddr);
208
209/*
210 * Local translation functions
211 */
212static	int sata_txlt_inquiry(sata_pkt_txlate_t *);
213static	int sata_txlt_test_unit_ready(sata_pkt_txlate_t *);
214static	int sata_txlt_start_stop_unit(sata_pkt_txlate_t *);
215static	int sata_txlt_read_capacity(sata_pkt_txlate_t *);
216static	int sata_txlt_read_capacity16(sata_pkt_txlate_t *);
217static  int sata_txlt_unmap(sata_pkt_txlate_t *);
218static	int sata_txlt_request_sense(sata_pkt_txlate_t *);
219static	int sata_txlt_read(sata_pkt_txlate_t *);
220static	int sata_txlt_write(sata_pkt_txlate_t *);
221static	int sata_txlt_log_sense(sata_pkt_txlate_t *);
222static	int sata_txlt_log_select(sata_pkt_txlate_t *);
223static	int sata_txlt_mode_sense(sata_pkt_txlate_t *);
224static	int sata_txlt_mode_select(sata_pkt_txlate_t *);
225static	int sata_txlt_ata_pass_thru(sata_pkt_txlate_t *);
226static	int sata_txlt_synchronize_cache(sata_pkt_txlate_t *);
227static	int sata_txlt_write_buffer(sata_pkt_txlate_t *);
228static	int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *);
229
230static	int sata_hba_start(sata_pkt_txlate_t *, int *);
231static	int sata_txlt_invalid_command(sata_pkt_txlate_t *);
232static	int sata_txlt_check_condition(sata_pkt_txlate_t *, uchar_t, uchar_t);
233static	int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *);
234static	int sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *);
235static  int sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *);
236static	void sata_txlt_rw_completion(sata_pkt_t *);
237static	void sata_txlt_nodata_cmd_completion(sata_pkt_t *);
238static	void sata_txlt_apt_completion(sata_pkt_t *sata_pkt);
239static	void sata_txlt_unmap_completion(sata_pkt_t *sata_pkt);
240static	void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *);
241static	int sata_emul_rw_completion(sata_pkt_txlate_t *);
242static	void sata_fill_ata_return_desc(sata_pkt_t *, uint8_t, uint8_t,
243    uint8_t);
244static	struct scsi_extended_sense *sata_immediate_error_response(
245    sata_pkt_txlate_t *, int);
246static	struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *);
247
248static	int sata_txlt_atapi(sata_pkt_txlate_t *);
249static	void sata_txlt_atapi_completion(sata_pkt_t *);
250
251/*
252 * Local functions for ioctl
253 */
254static	int32_t sata_get_port_num(sata_hba_inst_t *,  struct devctl_iocdata *);
255static	void sata_cfgadm_state(sata_hba_inst_t *, int32_t,
256    devctl_ap_state_t *);
257static	dev_info_t *sata_get_target_dip(dev_info_t *, uint8_t, uint8_t);
258static	dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *);
259static	dev_info_t *sata_devt_to_devinfo(dev_t);
260static	int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *);
261static	int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *);
262static	int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *);
263static	int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *);
264static	int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *);
265static	int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *);
266static	int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *);
267static	int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *);
268static	int sata_ioctl_reset_all(sata_hba_inst_t *);
269static	int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *);
270static	int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *,
271    sata_ioctl_data_t *, int mode);
272static	int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *,
273    sata_ioctl_data_t *, int mode);
274static	int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *,
275    sata_ioctl_data_t *, int mode);
276static	int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *,
277    sata_ioctl_data_t *, int mode);
278static	int sata_ioctl_get_serialnumber_info(sata_hba_inst_t *,
279    sata_device_t *, sata_ioctl_data_t *, int mode);
280
281/*
282 * Local functions
283 */
284static	void sata_remove_hba_instance(dev_info_t *);
285static	int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *);
286static	void sata_probe_ports(sata_hba_inst_t *);
287static	void sata_probe_pmports(sata_hba_inst_t *, uint8_t);
288static	int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int);
289static	int sata_reprobe_pmult(sata_hba_inst_t *, sata_device_t *, int);
290static	int sata_reprobe_pmport(sata_hba_inst_t *, sata_device_t *, int);
291static	int sata_alloc_pmult(sata_hba_inst_t *, sata_device_t *);
292static	void sata_free_pmult(sata_hba_inst_t *, sata_device_t *);
293static	int sata_add_device(dev_info_t *, sata_hba_inst_t *, sata_device_t *);
294static	int sata_offline_device(sata_hba_inst_t *, sata_device_t *,
295    sata_drive_info_t *);
296static	dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *,
297    sata_address_t *);
298static	void sata_remove_target_node(sata_hba_inst_t *,
299    sata_address_t *);
300static	int sata_validate_scsi_address(sata_hba_inst_t *,
301    struct scsi_address *, sata_device_t *);
302static	int sata_validate_sata_address(sata_hba_inst_t *, int, int, int);
303static	sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t));
304static	void sata_pkt_free(sata_pkt_txlate_t *);
305static	int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t),
306    caddr_t, ddi_dma_attr_t *);
307static	void sata_common_free_dma_rsrcs(sata_pkt_txlate_t *);
308static	int sata_probe_device(sata_hba_inst_t *, sata_device_t *);
309static	sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *,
310    sata_device_t *);
311static	int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *);
312static	void sata_reidentify_device(sata_pkt_txlate_t *);
313static	struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int);
314static	void sata_free_local_buffer(sata_pkt_txlate_t *);
315static	uint64_t sata_check_capacity(sata_drive_info_t *);
316void	sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *,
317    ddi_dma_attr_t *);
318static	int sata_fetch_device_identify_data(sata_hba_inst_t *,
319    sata_drive_info_t *);
320static	void sata_update_port_info(sata_hba_inst_t *, sata_device_t *);
321static	void sata_update_pmport_info(sata_hba_inst_t *, sata_device_t *);
322static	void sata_update_port_scr(sata_port_scr_t *, sata_device_t *);
323static	int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *);
324static	int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int);
325static	int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int);
326static	int sata_set_drive_features(sata_hba_inst_t *,
327    sata_drive_info_t *, int flag);
328static	void sata_init_write_cache_mode(sata_drive_info_t *sdinfo);
329static	int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *);
330static	void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *,
331    uint8_t *);
332static	int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *,
333    struct scsi_inquiry *);
334static	int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *);
335static	int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *);
336static	int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *);
337static	int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *);
338static	int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *);
339static	int sata_mode_select_page_8(sata_pkt_txlate_t *,
340    struct mode_cache_scsi3 *, int, int *, int *, int *);
341static	int sata_mode_select_page_1a(sata_pkt_txlate_t *,
342    struct mode_info_power_cond *, int, int *, int *, int *);
343static	int sata_mode_select_page_1c(sata_pkt_txlate_t *,
344    struct mode_info_excpt_page *, int, int *, int *, int *);
345static	int sata_mode_select_page_30(sata_pkt_txlate_t *,
346    struct mode_acoustic_management *, int, int *, int *, int *);
347
348static	int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *);
349static	int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *,
350    sata_hba_inst_t *);
351static	int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *,
352    sata_hba_inst_t *);
353static	int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *,
354    sata_hba_inst_t *);
355static	int sata_build_lsense_page_0e(sata_drive_info_t *, uint8_t *,
356    sata_pkt_txlate_t *);
357
358static	void sata_set_arq_data(sata_pkt_t *);
359static	void sata_build_read_verify_cmd(sata_cmd_t *, uint16_t, uint64_t);
360static	void sata_build_generic_cmd(sata_cmd_t *, uint8_t);
361static	uint8_t sata_get_standby_timer(uint8_t *timer);
362
363static	void sata_save_drive_settings(sata_drive_info_t *);
364static	void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *);
365static	void sata_show_pmult_info(sata_hba_inst_t *, sata_device_t *);
366static	void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...);
367static	void sata_trace_log(sata_hba_inst_t *, uint_t, const char *fmt, ...);
368static	int sata_fetch_smart_return_status(sata_hba_inst_t *,
369    sata_drive_info_t *);
370static	int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *,
371    struct smart_data *);
372static	int sata_smart_selftest_log(sata_hba_inst_t *,
373    sata_drive_info_t *,
374    struct smart_selftest_log *);
375static	int sata_ext_smart_selftest_read_log(sata_hba_inst_t *,
376    sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t);
377static	int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *,
378    uint8_t *, uint8_t, uint8_t);
379static	int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *,
380    struct read_log_ext_directory *);
381static	void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int);
382static	void sata_xlate_errors(sata_pkt_txlate_t *);
383static	void sata_decode_device_error(sata_pkt_txlate_t *,
384    struct scsi_extended_sense *);
385static	void sata_set_device_removed(dev_info_t *);
386static	boolean_t sata_check_device_removed(dev_info_t *);
387static	void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *);
388static	int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *,
389    sata_drive_info_t *);
390static	int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *,
391    sata_drive_info_t *);
392static	void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *);
393static	void sata_fixed_sense_data_preset(struct scsi_extended_sense *);
394static  void sata_target_devid_register(dev_info_t *, sata_drive_info_t *);
395static  int sata_check_modser(char *, int);
396
397/*
398 * FMA
399 */
400static boolean_t sata_check_for_dma_error(dev_info_t *, sata_pkt_txlate_t *);
401
402
403/*
404 * SATA Framework will ignore SATA HBA driver cb_ops structure and
405 * register following one with SCSA framework.
406 * Open & close are provided, so scsi framework will not use its own
407 */
408static struct cb_ops sata_cb_ops = {
409	sata_hba_open,			/* open */
410	sata_hba_close,			/* close */
411	nodev,				/* strategy */
412	nodev,				/* print */
413	nodev,				/* dump */
414	nodev,				/* read */
415	nodev,				/* write */
416	sata_hba_ioctl,			/* ioctl */
417	nodev,				/* devmap */
418	nodev,				/* mmap */
419	nodev,				/* segmap */
420	nochpoll,			/* chpoll */
421	ddi_prop_op,			/* cb_prop_op */
422	0,				/* streamtab */
423	D_NEW | D_MP,			/* cb_flag */
424	CB_REV,				/* rev */
425	nodev,				/* aread */
426	nodev				/* awrite */
427};
428
429
430extern struct mod_ops mod_miscops;
431extern uchar_t	scsi_cdb_size[];
432
433static struct modlmisc modlmisc = {
434	&mod_miscops,			/* Type of module */
435	"SATA Module"			/* module name */
436};
437
438
439static struct modlinkage modlinkage = {
440	MODREV_1,
441	(void *)&modlmisc,
442	NULL
443};
444
445/*
446 * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero,
447 * i.e. when scsi_pkt has not timeout specified.
448 */
449static int sata_default_pkt_time = 60;	/* 60 seconds */
450
451/*
452 * Intermediate buffer device access attributes - they are required,
453 * but not necessarily used.
454 */
455static ddi_device_acc_attr_t sata_acc_attr = {
456	DDI_DEVICE_ATTR_V0,
457	DDI_STRUCTURE_LE_ACC,
458	DDI_STRICTORDER_ACC
459};
460
461
462/*
463 * Mutexes protecting structures in multithreaded operations.
464 * Because events are relatively rare, a single global mutex protecting
465 * data structures should be sufficient. To increase performance, add
466 * separate mutex per each sata port and use global mutex only to protect
467 * common data structures.
468 */
469static	kmutex_t sata_mutex;		/* protects sata_hba_list */
470static	kmutex_t sata_log_mutex;	/* protects log */
471
472static	char sata_log_buf[256];
473
474/*
475 * sata trace debug
476 */
477static	sata_trace_rbuf_t *sata_debug_rbuf;
478static	sata_trace_dmsg_t *sata_trace_dmsg_alloc(void);
479static	void sata_trace_dmsg_free(void);
480static	void sata_trace_rbuf_alloc(void);
481static	void sata_trace_rbuf_free(void);
482
483int	dmsg_ring_size = DMSG_RING_SIZE;
484
485/* Default write cache setting for SATA hard disks */
486int	sata_write_cache = 1;		/* enabled */
487
488/* Default write cache setting for SATA ATAPI CD/DVD */
489int	sata_atapicdvd_write_cache = 1; /* enabled */
490
491/* Default write cache setting for SATA ATAPI tape */
492int	sata_atapitape_write_cache = 1; /* enabled */
493
494/* Default write cache setting for SATA ATAPI disk */
495int	sata_atapidisk_write_cache = 1;	/* enabled */
496
497/*
498 * Linked list of HBA instances
499 */
500static	sata_hba_inst_t *sata_hba_list = NULL;
501static	sata_hba_inst_t *sata_hba_list_tail = NULL;
502/*
503 * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran
504 * structure and in sata soft state.
505 */
506
507/*
508 * Event daemon related variables
509 */
510static	kmutex_t sata_event_mutex;
511static	kcondvar_t sata_event_cv;
512static	kthread_t *sata_event_thread = NULL;
513static	int sata_event_thread_terminate = 0;
514static	int sata_event_pending = 0;
515static	int sata_event_thread_active = 0;
516extern	pri_t minclsyspri;
517
518/*
519 * NCQ error recovery command
520 */
521static const sata_cmd_t sata_rle_cmd = {
522	SATA_CMD_REV,
523	NULL,
524	{
525		SATA_DIR_READ
526	},
527	ATA_ADDR_LBA48,
528	0,
529	0,
530	0,
531	0,
532	0,
533	1,
534	READ_LOG_EXT_NCQ_ERROR_RECOVERY,
535	0,
536	0,
537	0,
538	SATAC_READ_LOG_EXT,
539	0,
540	0,
541	0,
542};
543
544/*
545 * ATAPI error recovery CDB
546 */
547static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = {
548	SCMD_REQUEST_SENSE,
549	0,			/* Only fixed RQ format is supported */
550	0,
551	0,
552	SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */
553	0
554};
555
556
557/* Warlock directives */
558
559_NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran))
560_NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device))
561_NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops))
562_NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense))
563_NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status))
564_NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr))
565_NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t))
566_NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state))
567_NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state))
568_NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list))
569_NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list))
570_NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next))
571_NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev))
572_NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \
573    sata_hba_inst::satahba_scsi_tran))
574_NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran))
575_NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip))
576_NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached))
577_NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port))
578_NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex,
579    sata_hba_inst::satahba_event_flags))
580_NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
581    sata_cport_info::cport_devp))
582_NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp))
583_NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr))
584_NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
585    sata_cport_info::cport_dev_type))
586_NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type))
587_NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
588    sata_cport_info::cport_state))
589_NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state))
590_NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
591    sata_pmport_info::pmport_state))
592_NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state))
593_NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
594    sata_pmport_info::pmport_dev_type))
595_NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type))
596_NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
597    sata_pmport_info::pmport_sata_drive))
598_NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
599    sata_pmport_info::pmport_tgtnode_clean))
600_NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
601    sata_pmport_info::pmport_event_flags))
602_NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive))
603_NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port))
604_NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports))
605#ifdef SATA_DEBUG
606_NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count))
607_NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count))
608_NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace))
609_NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index))
610#endif
611
612/* End of warlock directives */
613
614/* ************** loadable module configuration functions ************** */
615
616int
617_init()
618{
619	int rval;
620
621	mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL);
622	mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL);
623	mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL);
624	cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL);
625	sata_trace_rbuf_alloc();
626	if ((rval = mod_install(&modlinkage)) != 0) {
627#ifdef SATA_DEBUG
628		cmn_err(CE_WARN, "sata: _init: mod_install failed\n");
629#endif
630		sata_trace_rbuf_free();
631		mutex_destroy(&sata_log_mutex);
632		cv_destroy(&sata_event_cv);
633		mutex_destroy(&sata_event_mutex);
634		mutex_destroy(&sata_mutex);
635	}
636	return (rval);
637}
638
639int
640_fini()
641{
642	int rval;
643
644	if ((rval = mod_remove(&modlinkage)) != 0)
645		return (rval);
646
647	sata_trace_rbuf_free();
648	mutex_destroy(&sata_log_mutex);
649	cv_destroy(&sata_event_cv);
650	mutex_destroy(&sata_event_mutex);
651	mutex_destroy(&sata_mutex);
652	return (rval);
653}
654
655int
656_info(struct modinfo *modinfop)
657{
658	return (mod_info(&modlinkage, modinfop));
659}
660
661
662
663/* ********************* SATA HBA entry points ********************* */
664
665
666/*
667 * Called by SATA HBA from _init().
668 * Registers HBA driver instance/sata framework pair with scsi framework, by
669 * calling scsi_hba_init().
670 *
671 * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used
672 * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver
673 * cb_ops pointer in SATA HBA driver dev_ops structure.
674 * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors.
675 *
676 * Return status of the scsi_hba_init() is returned to a calling SATA HBA
677 * driver.
678 */
679int
680sata_hba_init(struct modlinkage *modlp)
681{
682	int rval;
683	struct dev_ops *hba_ops;
684
685	SATADBG1(SATA_DBG_HBA_IF, NULL,
686	    "sata_hba_init: name %s \n",
687	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
688	/*
689	 * Fill-up cb_ops and dev_ops when necessary
690	 */
691	hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
692	/*
693	 * Provide pointer to SATA dev_ops
694	 */
695	hba_ops->devo_cb_ops = &sata_cb_ops;
696
697	/*
698	 * Register SATA HBA with SCSI framework
699	 */
700	if ((rval = scsi_hba_init(modlp)) != 0) {
701		SATADBG1(SATA_DBG_HBA_IF, NULL,
702		    "sata_hba_init: scsi hba init failed\n", NULL);
703		return (rval);
704	}
705
706	return (0);
707}
708
709
710/* HBA attach stages */
711#define	HBA_ATTACH_STAGE_SATA_HBA_INST	1
712#define	HBA_ATTACH_STAGE_SCSI_ATTACHED	2
713#define	HBA_ATTACH_STAGE_SETUP		4
714#define	HBA_ATTACH_STAGE_LINKED		8
715
716
717/*
718 *
719 * Called from SATA HBA driver's attach routine to attach an instance of
720 * the HBA.
721 *
722 * For DDI_ATTACH command:
723 * sata_hba_inst structure is allocated here and initialized with pointers to
724 * SATA framework implementation of required scsi tran functions.
725 * The scsi_tran's tran_hba_private field is used by SATA Framework to point
726 * to the soft structure (sata_hba_inst) allocated by SATA framework for
727 * SATA HBA instance related data.
728 * The scsi_tran's tran_hba_private field is used by SATA framework to
729 * store a pointer to per-HBA-instance of sata_hba_inst structure.
730 * The sata_hba_inst structure is cross-linked to scsi tran structure.
731 * Among other info, a pointer to sata_hba_tran structure is stored in
732 * sata_hba_inst. The sata_hba_inst structures for different HBA instances are
733 * linked together into the list, pointed to by sata_hba_list.
734 * On the first HBA instance attach the sata event thread is initialized.
735 * Attachment points are created for all SATA ports of the HBA being attached.
736 * All HBA instance's SATA ports are probed and type of plugged devices is
737 * determined. For each device of a supported type, a target node is created.
738 *
739 * DDI_SUCCESS is returned when attachment process is successful,
740 * DDI_FAILURE is returned otherwise.
741 *
742 * For DDI_RESUME command:
743 * Not implemented at this time (postponed until phase 2 of the development).
744 */
745int
746sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran,
747    ddi_attach_cmd_t cmd)
748{
749	sata_hba_inst_t	*sata_hba_inst;
750	scsi_hba_tran_t *scsi_tran = NULL;
751	int hba_attach_state = 0;
752	char taskq_name[MAXPATHLEN];
753
754	SATADBG3(SATA_DBG_HBA_IF, NULL,
755	    "sata_hba_attach: node %s (%s%d)\n",
756	    ddi_node_name(dip), ddi_driver_name(dip),
757	    ddi_get_instance(dip));
758
759	if (cmd == DDI_RESUME) {
760		/*
761		 * Postponed until phase 2 of the development
762		 */
763		return (DDI_FAILURE);
764	}
765
766	if (cmd != DDI_ATTACH) {
767		return (DDI_FAILURE);
768	}
769
770	/* cmd == DDI_ATTACH */
771
772	if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) {
773		SATA_LOG_D((NULL, CE_WARN,
774		    "sata_hba_attach: invalid sata_hba_tran"));
775		return (DDI_FAILURE);
776	}
777	/*
778	 * Allocate and initialize SCSI tran structure.
779	 * SATA copy of tran_bus_config is provided to create port nodes.
780	 */
781	scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
782	if (scsi_tran == NULL)
783		return (DDI_FAILURE);
784	/*
785	 * Allocate soft structure for SATA HBA instance.
786	 * There is a separate softstate for each HBA instance.
787	 */
788	sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP);
789	ASSERT(sata_hba_inst != NULL); /* this should not fail */
790	mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL);
791	hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST;
792
793	/*
794	 * scsi_trans's tran_hba_private is used by SATA Framework to point to
795	 * soft structure allocated by SATA framework for
796	 * SATA HBA instance related data.
797	 */
798	scsi_tran->tran_hba_private	= sata_hba_inst;
799	scsi_tran->tran_tgt_private	= NULL;
800
801	scsi_tran->tran_tgt_init	= sata_scsi_tgt_init;
802	scsi_tran->tran_tgt_probe	= sata_scsi_tgt_probe;
803	scsi_tran->tran_tgt_free	= sata_scsi_tgt_free;
804
805	scsi_tran->tran_start		= sata_scsi_start;
806	scsi_tran->tran_reset		= sata_scsi_reset;
807	scsi_tran->tran_abort		= sata_scsi_abort;
808	scsi_tran->tran_getcap		= sata_scsi_getcap;
809	scsi_tran->tran_setcap		= sata_scsi_setcap;
810	scsi_tran->tran_init_pkt	= sata_scsi_init_pkt;
811	scsi_tran->tran_destroy_pkt	= sata_scsi_destroy_pkt;
812
813	scsi_tran->tran_dmafree		= sata_scsi_dmafree;
814	scsi_tran->tran_sync_pkt	= sata_scsi_sync_pkt;
815
816	scsi_tran->tran_reset_notify	= NULL;
817	scsi_tran->tran_get_bus_addr	= NULL;
818	scsi_tran->tran_quiesce		= NULL;
819	scsi_tran->tran_unquiesce	= NULL;
820	scsi_tran->tran_bus_reset	= NULL;
821
822	if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr,
823	    scsi_tran, 0) != DDI_SUCCESS) {
824#ifdef SATA_DEBUG
825		cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed",
826		    ddi_driver_name(dip), ddi_get_instance(dip));
827#endif
828		goto fail;
829	}
830	hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED;
831
832	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) {
833		if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
834		    "sata", 1) != DDI_PROP_SUCCESS) {
835			SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: "
836			    "failed to create hba sata prop"));
837			goto fail;
838		}
839	}
840
841	/*
842	 * Save pointers in hba instance soft state.
843	 */
844	sata_hba_inst->satahba_scsi_tran = scsi_tran;
845	sata_hba_inst->satahba_tran = sata_tran;
846	sata_hba_inst->satahba_dip = dip;
847
848	/*
849	 * Create a task queue to handle emulated commands completion
850	 * Use node name, dash, instance number as the queue name.
851	 */
852	taskq_name[0] = '\0';
853	(void) strlcat(taskq_name, DEVI(dip)->devi_node_name,
854	    sizeof (taskq_name));
855	(void) snprintf(taskq_name + strlen(taskq_name),
856	    sizeof (taskq_name) - strlen(taskq_name),
857	    "-%d", DEVI(dip)->devi_instance);
858	sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1,
859	    minclsyspri, 1, sata_tran->sata_tran_hba_num_cports * 4,
860	    TASKQ_DYNAMIC);
861
862	hba_attach_state |= HBA_ATTACH_STAGE_SETUP;
863
864	/*
865	 * Create events thread if not created yet.
866	 */
867	sata_event_thread_control(1);
868
869	/*
870	 * Link this hba instance into the list.
871	 */
872	mutex_enter(&sata_mutex);
873
874	if (sata_hba_list == NULL) {
875		/*
876		 * The first instance of HBA is attached.
877		 * Set current/active default maximum NCQ/TCQ queue depth for
878		 * all SATA devices. It is done here and now, to eliminate the
879		 * possibility of the dynamic, programatic modification of the
880		 * queue depth via global (and public) sata_max_queue_depth
881		 * variable (this would require special handling in HBA drivers)
882		 */
883		sata_current_max_qdepth = sata_max_queue_depth;
884		if (sata_current_max_qdepth > 32)
885			sata_current_max_qdepth = 32;
886		else if (sata_current_max_qdepth < 1)
887			sata_current_max_qdepth = 1;
888	}
889
890	sata_hba_inst->satahba_next = NULL;
891	sata_hba_inst->satahba_prev = sata_hba_list_tail;
892	if (sata_hba_list == NULL) {
893		sata_hba_list = sata_hba_inst;
894	}
895	if (sata_hba_list_tail != NULL) {
896		sata_hba_list_tail->satahba_next = sata_hba_inst;
897	}
898	sata_hba_list_tail = sata_hba_inst;
899	mutex_exit(&sata_mutex);
900	hba_attach_state |= HBA_ATTACH_STAGE_LINKED;
901
902	/*
903	 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl
904	 * SATA HBA driver should not use its own open/close entry points.
905	 *
906	 * Make sure that instance number doesn't overflow
907	 * when forming minor numbers.
908	 */
909	ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT));
910	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
911	    INST2DEVCTL(ddi_get_instance(dip)),
912	    DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) {
913#ifdef SATA_DEBUG
914		cmn_err(CE_WARN, "sata_hba_attach: "
915		    "cannot create devctl minor node");
916#endif
917		goto fail;
918	}
919
920
921	/*
922	 * Set-up kstats here, if necessary.
923	 * (postponed until future phase of the development).
924	 */
925
926	/*
927	 * Indicate that HBA is attached. This will enable events processing
928	 * for this HBA.
929	 */
930	sata_hba_inst->satahba_attached = 1;
931	/*
932	 * Probe controller ports. This operation will describe a current
933	 * controller/port/multipliers/device configuration and will create
934	 * attachment points.
935	 * We may end-up with just a controller with no devices attached.
936	 * For the ports with a supported device attached, device target nodes
937	 * are created and devices are initialized.
938	 */
939	sata_probe_ports(sata_hba_inst);
940
941	return (DDI_SUCCESS);
942
943fail:
944	if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) {
945		(void) sata_remove_hba_instance(dip);
946		if (sata_hba_list == NULL)
947			sata_event_thread_control(0);
948	}
949
950	if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) {
951		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
952		taskq_destroy(sata_hba_inst->satahba_taskq);
953	}
954
955	if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED)
956		(void) scsi_hba_detach(dip);
957
958	if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) {
959		mutex_destroy(&sata_hba_inst->satahba_mutex);
960		kmem_free((void *)sata_hba_inst,
961		    sizeof (struct sata_hba_inst));
962		scsi_hba_tran_free(scsi_tran);
963	}
964
965	sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed",
966	    ddi_driver_name(dip), ddi_get_instance(dip));
967
968	return (DDI_FAILURE);
969}
970
971
972/*
973 * Called by SATA HBA from to detach an instance of the driver.
974 *
975 * For DDI_DETACH command:
976 * Free local structures allocated for SATA HBA instance during
977 * sata_hba_attach processing.
978 *
979 * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise.
980 *
981 * For DDI_SUSPEND command:
982 * Not implemented at this time (postponed until phase 2 of the development)
983 * Returnd DDI_SUCCESS.
984 *
985 * When the last HBA instance is detached, the event daemon is terminated.
986 *
987 * NOTE: Port multiplier is supported.
988 */
989int
990sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
991{
992	dev_info_t	*tdip;
993	sata_hba_inst_t	*sata_hba_inst;
994	scsi_hba_tran_t *scsi_hba_tran;
995	sata_cport_info_t *cportinfo;
996	sata_pmult_info_t *pminfo;
997	sata_drive_info_t *sdinfo;
998	sata_device_t	sdevice;
999	int ncport, npmport;
1000
1001	SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n",
1002	    ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip));
1003
1004	switch (cmd) {
1005	case DDI_DETACH:
1006
1007		if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1008			return (DDI_FAILURE);
1009
1010		sata_hba_inst = scsi_hba_tran->tran_hba_private;
1011		if (sata_hba_inst == NULL)
1012			return (DDI_FAILURE);
1013
1014		if (scsi_hba_detach(dip) == DDI_FAILURE) {
1015			sata_hba_inst->satahba_attached = 1;
1016			return (DDI_FAILURE);
1017		}
1018
1019		/*
1020		 * Free all target nodes - at this point
1021		 * devices should be at least offlined
1022		 * otherwise scsi_hba_detach() should not be called.
1023		 */
1024		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
1025		    ncport++) {
1026			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
1027			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
1028				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
1029				if (sdinfo != NULL) {
1030					tdip = sata_get_target_dip(dip,
1031					    ncport, 0);
1032					if (tdip != NULL) {
1033						if (ndi_devi_offline(tdip,
1034						    NDI_DEVI_REMOVE) !=
1035						    NDI_SUCCESS) {
1036							SATA_LOG_D((
1037							    sata_hba_inst,
1038							    CE_WARN,
1039							    "sata_hba_detach: "
1040							    "Target node not "
1041							    "removed !"));
1042							return (DDI_FAILURE);
1043						}
1044					}
1045				}
1046			} else { /* SATA_DTYPE_PMULT */
1047				mutex_enter(&cportinfo->cport_mutex);
1048				pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
1049
1050				if (pminfo == NULL) {
1051					SATA_LOG_D((sata_hba_inst, CE_WARN,
1052					    "sata_hba_detach: Port multiplier "
1053					    "not ready yet!"));
1054					mutex_exit(&cportinfo->cport_mutex);
1055					return (DDI_FAILURE);
1056				}
1057
1058				/*
1059				 * Detach would fail if removal of any of the
1060				 * target nodes is failed - albeit in that
1061				 * case some of them may have been removed.
1062				 */
1063				for (npmport = 0; npmport < SATA_NUM_PMPORTS(
1064				    sata_hba_inst, ncport); npmport++) {
1065					tdip = sata_get_target_dip(dip, ncport,
1066					    npmport);
1067					if (tdip != NULL) {
1068						if (ndi_devi_offline(tdip,
1069						    NDI_DEVI_REMOVE) !=
1070						    NDI_SUCCESS) {
1071							SATA_LOG_D((
1072							    sata_hba_inst,
1073							    CE_WARN,
1074							    "sata_hba_detach: "
1075							    "Target node not "
1076							    "removed !"));
1077							mutex_exit(&cportinfo->
1078							    cport_mutex);
1079							return (DDI_FAILURE);
1080						}
1081					}
1082				}
1083				mutex_exit(&cportinfo->cport_mutex);
1084			}
1085		}
1086		/*
1087		 * Disable sata event daemon processing for this HBA
1088		 */
1089		sata_hba_inst->satahba_attached = 0;
1090
1091		/*
1092		 * Remove event daemon thread, if it is last HBA instance.
1093		 */
1094
1095		mutex_enter(&sata_mutex);
1096		if (sata_hba_list->satahba_next == NULL) {
1097			mutex_exit(&sata_mutex);
1098			sata_event_thread_control(0);
1099			mutex_enter(&sata_mutex);
1100		}
1101		mutex_exit(&sata_mutex);
1102
1103		/* Remove this HBA instance from the HBA list */
1104		sata_remove_hba_instance(dip);
1105
1106		/*
1107		 * At this point there should be no target nodes attached.
1108		 * Detach and destroy device and port info structures.
1109		 */
1110		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
1111		    ncport++) {
1112			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
1113			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
1114				sdinfo =
1115				    cportinfo->cport_devp.cport_sata_drive;
1116				if (sdinfo != NULL) {
1117					/* Release device structure */
1118					kmem_free(sdinfo,
1119					    sizeof (sata_drive_info_t));
1120				}
1121				/* Release cport info */
1122				mutex_destroy(&cportinfo->cport_mutex);
1123				kmem_free(cportinfo,
1124				    sizeof (sata_cport_info_t));
1125			} else { /* SATA_DTYPE_PMULT */
1126				sdevice.satadev_addr.cport = (uint8_t)ncport;
1127				sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
1128				sata_free_pmult(sata_hba_inst, &sdevice);
1129			}
1130		}
1131
1132		scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran);
1133
1134		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
1135
1136		taskq_destroy(sata_hba_inst->satahba_taskq);
1137
1138		mutex_destroy(&sata_hba_inst->satahba_mutex);
1139		kmem_free((void *)sata_hba_inst,
1140		    sizeof (struct sata_hba_inst));
1141
1142		return (DDI_SUCCESS);
1143
1144	case DDI_SUSPEND:
1145		/*
1146		 * Postponed until phase 2
1147		 */
1148		return (DDI_FAILURE);
1149
1150	default:
1151		return (DDI_FAILURE);
1152	}
1153}
1154
1155
1156/*
1157 * Called by an HBA drive from _fini() routine.
1158 * Unregisters SATA HBA instance/SATA framework pair from the scsi framework.
1159 */
1160void
1161sata_hba_fini(struct modlinkage *modlp)
1162{
1163	SATADBG1(SATA_DBG_HBA_IF, NULL,
1164	    "sata_hba_fini: name %s\n",
1165	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
1166
1167	scsi_hba_fini(modlp);
1168}
1169
1170
1171/*
1172 * Default open and close routine for sata_hba framework.
1173 *
1174 */
1175/*
1176 * Open devctl node.
1177 *
1178 * Returns:
1179 * 0 if node was open successfully, error code otherwise.
1180 *
1181 *
1182 */
1183
1184static int
1185sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1186{
1187#ifndef __lock_lint
1188	_NOTE(ARGUNUSED(credp))
1189#endif
1190	int rv = 0;
1191	dev_info_t *dip;
1192	scsi_hba_tran_t *scsi_hba_tran;
1193	sata_hba_inst_t	*sata_hba_inst;
1194
1195	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL);
1196
1197	if (otyp != OTYP_CHR)
1198		return (EINVAL);
1199
1200	dip = sata_devt_to_devinfo(*devp);
1201	if (dip == NULL)
1202		return (ENXIO);
1203
1204	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1205		return (ENXIO);
1206
1207	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1208	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1209		return (ENXIO);
1210
1211	mutex_enter(&sata_mutex);
1212	if (flags & FEXCL) {
1213		if (sata_hba_inst->satahba_open_flag != 0) {
1214			rv = EBUSY;
1215		} else {
1216			sata_hba_inst->satahba_open_flag =
1217			    SATA_DEVCTL_EXOPENED;
1218		}
1219	} else {
1220		if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) {
1221			rv = EBUSY;
1222		} else {
1223			sata_hba_inst->satahba_open_flag =
1224			    SATA_DEVCTL_SOPENED;
1225		}
1226	}
1227	mutex_exit(&sata_mutex);
1228
1229	return (rv);
1230}
1231
1232
1233/*
1234 * Close devctl node.
1235 * Returns:
1236 * 0 if node was closed successfully, error code otherwise.
1237 *
1238 */
1239
1240static int
1241sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
1242{
1243#ifndef __lock_lint
1244	_NOTE(ARGUNUSED(credp))
1245	_NOTE(ARGUNUSED(flag))
1246#endif
1247	dev_info_t *dip;
1248	scsi_hba_tran_t *scsi_hba_tran;
1249	sata_hba_inst_t	*sata_hba_inst;
1250
1251	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL);
1252
1253	if (otyp != OTYP_CHR)
1254		return (EINVAL);
1255
1256	dip = sata_devt_to_devinfo(dev);
1257	if (dip == NULL)
1258		return (ENXIO);
1259
1260	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1261		return (ENXIO);
1262
1263	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1264	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1265		return (ENXIO);
1266
1267	mutex_enter(&sata_mutex);
1268	sata_hba_inst->satahba_open_flag = 0;
1269	mutex_exit(&sata_mutex);
1270	return (0);
1271}
1272
1273
1274
1275/*
1276 * Standard IOCTL commands for SATA hotplugging.
1277 * Implemented DEVCTL_AP commands:
1278 * DEVCTL_AP_CONNECT
1279 * DEVCTL_AP_DISCONNECT
1280 * DEVCTL_AP_CONFIGURE
1281 * DEVCTL_UNCONFIGURE
1282 * DEVCTL_AP_CONTROL
1283 *
1284 * Commands passed to default ndi ioctl handler:
1285 * DEVCTL_DEVICE_GETSTATE
1286 * DEVCTL_DEVICE_ONLINE
1287 * DEVCTL_DEVICE_OFFLINE
1288 * DEVCTL_DEVICE_REMOVE
1289 * DEVCTL_DEVICE_INSERT
1290 * DEVCTL_BUS_GETSTATE
1291 *
1292 * All other cmds are passed to HBA if it provide ioctl handler, or failed
1293 * if not.
1294 *
1295 * Returns:
1296 * 0 if successful,
1297 * error code if operation failed.
1298 *
1299 * Port Multiplier support is supported now.
1300 *
1301 * NOTE: qual should be SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT
1302 */
1303
1304static int
1305sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1306    int *rvalp)
1307{
1308#ifndef __lock_lint
1309	_NOTE(ARGUNUSED(credp))
1310	_NOTE(ARGUNUSED(rvalp))
1311#endif
1312	int rv = 0;
1313	int32_t	comp_port = -1;
1314	dev_info_t *dip;
1315	devctl_ap_state_t ap_state;
1316	struct devctl_iocdata *dcp = NULL;
1317	scsi_hba_tran_t *scsi_hba_tran;
1318	sata_hba_inst_t *sata_hba_inst;
1319	sata_device_t sata_device;
1320	sata_cport_info_t *cportinfo;
1321	int cport, pmport, qual;
1322	int rval = SATA_SUCCESS;
1323
1324	dip = sata_devt_to_devinfo(dev);
1325	if (dip == NULL)
1326		return (ENXIO);
1327
1328	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1329		return (ENXIO);
1330
1331	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1332	if (sata_hba_inst == NULL)
1333		return (ENXIO);
1334
1335	if (sata_hba_inst->satahba_tran == NULL)
1336		return (ENXIO);
1337
1338	switch (cmd) {
1339
1340	case DEVCTL_DEVICE_GETSTATE:
1341	case DEVCTL_DEVICE_ONLINE:
1342	case DEVCTL_DEVICE_OFFLINE:
1343	case DEVCTL_DEVICE_REMOVE:
1344	case DEVCTL_BUS_GETSTATE:
1345		/*
1346		 * There may be more cases that we want to pass to default
1347		 * handler rather than fail them.
1348		 */
1349		return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
1350	}
1351
1352	/* read devctl ioctl data */
1353	if (cmd != DEVCTL_AP_CONTROL && cmd >= DEVCTL_IOC &&
1354	    cmd <= DEVCTL_IOC_MAX) {
1355		if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1356			return (EFAULT);
1357
1358		if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) ==
1359		    -1) {
1360			if (dcp)
1361				ndi_dc_freehdl(dcp);
1362			return (EINVAL);
1363		}
1364
1365		/*
1366		 * According to SCSI_TO_SATA_ADDR_QUAL, qual should be either
1367		 * SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT.
1368		 */
1369		cport = SCSI_TO_SATA_CPORT(comp_port);
1370		pmport = SCSI_TO_SATA_PMPORT(comp_port);
1371		qual = SCSI_TO_SATA_ADDR_QUAL(comp_port);
1372
1373		if (sata_validate_sata_address(sata_hba_inst, cport, pmport,
1374		    qual) != 0) {
1375			ndi_dc_freehdl(dcp);
1376			return (EINVAL);
1377		}
1378
1379		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1380		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1381		    cport_mutex);
1382		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1383			/*
1384			 * Cannot process ioctl request now. Come back later.
1385			 */
1386			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1387			    cport_mutex);
1388			ndi_dc_freehdl(dcp);
1389			return (EBUSY);
1390		}
1391		/* Block event processing for this port */
1392		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1393		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1394
1395		sata_device.satadev_addr.cport = cport;
1396		sata_device.satadev_addr.pmport = pmport;
1397		sata_device.satadev_addr.qual = qual;
1398		sata_device.satadev_rev = SATA_DEVICE_REV;
1399	}
1400
1401	switch (cmd) {
1402
1403	case DEVCTL_AP_DISCONNECT:
1404
1405		/*
1406		 * Normally, cfgadm sata plugin will try to offline
1407		 * (unconfigure) device before this request. Nevertheless,
1408		 * if a device is still configured, we need to
1409		 * attempt to offline and unconfigure device first, and we will
1410		 * deactivate the port regardless of the unconfigure
1411		 * operation results.
1412		 *
1413		 */
1414		rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device);
1415
1416		break;
1417
1418	case DEVCTL_AP_UNCONFIGURE:
1419
1420		/*
1421		 * The unconfigure operation uses generic nexus operation to
1422		 * offline a device. It leaves a target device node attached.
1423		 * and obviously sata_drive_info attached as well, because
1424		 * from the hardware point of view nothing has changed.
1425		 */
1426		rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device);
1427		break;
1428
1429	case DEVCTL_AP_CONNECT:
1430	{
1431		/*
1432		 * The sata cfgadm pluging will invoke this operation only if
1433		 * port was found in the disconnect state (failed state
1434		 * is also treated as the disconnected state).
1435		 * If port activation is successful and a device is found
1436		 * attached to the port, the initialization sequence is
1437		 * executed to probe the port and attach
1438		 * a device structure to a port structure. The device is not
1439		 * set in configured state (system-wise) by this operation.
1440		 */
1441
1442		rv = sata_ioctl_connect(sata_hba_inst, &sata_device);
1443
1444		break;
1445	}
1446
1447	case DEVCTL_AP_CONFIGURE:
1448	{
1449		/*
1450		 * A port may be in an active or shutdown state.
1451		 * If port is in a failed state, operation is aborted.
1452		 * If a port is in a shutdown state, sata_tran_port_activate()
1453		 * is invoked prior to any other operation.
1454		 *
1455		 * Onlining the device involves creating a new target node.
1456		 * If there is an old target node present (belonging to
1457		 * previously removed device), the operation is aborted - the
1458		 * old node has to be released and removed before configure
1459		 * operation is attempted.
1460		 */
1461
1462		rv = sata_ioctl_configure(sata_hba_inst, &sata_device);
1463
1464		break;
1465	}
1466
1467	case DEVCTL_AP_GETSTATE:
1468
1469		sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state);
1470
1471		ap_state.ap_last_change = (time_t)-1;
1472		ap_state.ap_error_code = 0;
1473		ap_state.ap_in_transition = 0;
1474
1475		/* Copy the return AP-state information to the user space */
1476		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
1477			rv = EFAULT;
1478		}
1479		break;
1480
1481	case DEVCTL_AP_CONTROL:
1482	{
1483		/*
1484		 * Generic devctl for hardware specific functionality
1485		 */
1486		sata_ioctl_data_t	ioc;
1487
1488		ASSERT(dcp == NULL);
1489
1490		/* Copy in user ioctl data first */
1491#ifdef _MULTI_DATAMODEL
1492		if (ddi_model_convert_from(mode & FMODELS) ==
1493		    DDI_MODEL_ILP32) {
1494
1495			sata_ioctl_data_32_t	ioc32;
1496
1497			if (ddi_copyin((void *)arg, (void *)&ioc32,
1498			    sizeof (ioc32), mode) != 0) {
1499				rv = EFAULT;
1500				break;
1501			}
1502			ioc.cmd		= (uint_t)ioc32.cmd;
1503			ioc.port	= (uint_t)ioc32.port;
1504			ioc.get_size	= (uint_t)ioc32.get_size;
1505			ioc.buf		= (caddr_t)(uintptr_t)ioc32.buf;
1506			ioc.bufsiz	= (uint_t)ioc32.bufsiz;
1507			ioc.misc_arg	= (uint_t)ioc32.misc_arg;
1508		} else
1509#endif /* _MULTI_DATAMODEL */
1510		if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
1511		    mode) != 0) {
1512			return (EFAULT);
1513		}
1514
1515		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
1516		    "sata_hba_ioctl: DEVCTL_AP_CONTROL "
1517		    "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port);
1518
1519		/*
1520		 * To avoid BE/LE and 32/64 issues, a get_size always returns
1521		 * a 32-bit number.
1522		 */
1523		if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
1524			return (EINVAL);
1525		}
1526		/* validate address */
1527		cport = SCSI_TO_SATA_CPORT(ioc.port);
1528		pmport = SCSI_TO_SATA_PMPORT(ioc.port);
1529		qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port);
1530
1531		SATADBG3(SATA_DBG_IOCTL_IF, sata_hba_inst,
1532		    "sata_hba_ioctl: target port is %d:%d (%d)",
1533		    cport, pmport, qual);
1534
1535		if (sata_validate_sata_address(sata_hba_inst, cport,
1536		    pmport, qual) != 0)
1537			return (EINVAL);
1538
1539		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1540		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1541		    cport_mutex);
1542		/* Is the port locked by event processing daemon ? */
1543		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1544			/*
1545			 * Cannot process ioctl request now. Come back later
1546			 */
1547			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1548			    cport_mutex);
1549			return (EBUSY);
1550		}
1551		/* Block event processing for this port */
1552		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1553		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1554
1555
1556		sata_device.satadev_addr.cport = cport;
1557		sata_device.satadev_addr.pmport = pmport;
1558		sata_device.satadev_addr.qual = qual;
1559		sata_device.satadev_rev = SATA_DEVICE_REV;
1560
1561		switch (ioc.cmd) {
1562
1563		case SATA_CFGA_RESET_PORT:
1564			/*
1565			 * There is no protection for configured device.
1566			 */
1567			rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device);
1568			break;
1569
1570		case SATA_CFGA_RESET_DEVICE:
1571			/*
1572			 * There is no protection for configured device.
1573			 */
1574			rv = sata_ioctl_reset_device(sata_hba_inst,
1575			    &sata_device);
1576			break;
1577
1578		case SATA_CFGA_RESET_ALL:
1579			/*
1580			 * There is no protection for configured devices.
1581			 */
1582			rv = sata_ioctl_reset_all(sata_hba_inst);
1583			/*
1584			 * We return here, because common return is for
1585			 * a single port operation - we have already unlocked
1586			 * all ports and no dc handle was allocated.
1587			 */
1588			return (rv);
1589
1590		case SATA_CFGA_PORT_DEACTIVATE:
1591			/*
1592			 * Arbitrarily unconfigure attached device, if any.
1593			 * Even if the unconfigure fails, proceed with the
1594			 * port deactivation.
1595			 */
1596			rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device);
1597
1598			break;
1599
1600		case SATA_CFGA_PORT_ACTIVATE:
1601
1602			rv = sata_ioctl_activate(sata_hba_inst, &sata_device);
1603			break;
1604
1605		case SATA_CFGA_PORT_SELF_TEST:
1606
1607			rv = sata_ioctl_port_self_test(sata_hba_inst,
1608			    &sata_device);
1609			break;
1610
1611		case SATA_CFGA_GET_DEVICE_PATH:
1612
1613			rv = sata_ioctl_get_device_path(sata_hba_inst,
1614			    &sata_device, &ioc, mode);
1615			break;
1616
1617		case SATA_CFGA_GET_AP_TYPE:
1618
1619			rv = sata_ioctl_get_ap_type(sata_hba_inst,
1620			    &sata_device, &ioc, mode);
1621			break;
1622
1623		case SATA_CFGA_GET_MODEL_INFO:
1624
1625			rv = sata_ioctl_get_model_info(sata_hba_inst,
1626			    &sata_device, &ioc, mode);
1627			break;
1628
1629		case SATA_CFGA_GET_REVFIRMWARE_INFO:
1630
1631			rv = sata_ioctl_get_revfirmware_info(sata_hba_inst,
1632			    &sata_device, &ioc, mode);
1633			break;
1634
1635		case SATA_CFGA_GET_SERIALNUMBER_INFO:
1636
1637			rv = sata_ioctl_get_serialnumber_info(sata_hba_inst,
1638			    &sata_device, &ioc, mode);
1639			break;
1640
1641		default:
1642			rv = EINVAL;
1643			break;
1644
1645		} /* End of DEVCTL_AP_CONTROL cmd switch */
1646
1647		break;
1648	}
1649
1650	default:
1651	{
1652		/*
1653		 * If we got here, we got an IOCTL that SATA HBA Framework
1654		 * does not recognize. Pass ioctl to HBA driver, in case
1655		 * it could process it.
1656		 */
1657		sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran;
1658		dev_info_t	*mydip = SATA_DIP(sata_hba_inst);
1659
1660		SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1661		    "IOCTL 0x%2x not supported in SATA framework, "
1662		    "passthrough to HBA", cmd);
1663
1664		if (sata_tran->sata_tran_ioctl == NULL) {
1665			rv = EINVAL;
1666			break;
1667		}
1668		rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg);
1669		if (rval != 0) {
1670			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1671			    "IOCTL 0x%2x failed in HBA", cmd);
1672			rv = rval;
1673		}
1674		break;
1675	}
1676
1677	} /* End of main IOCTL switch */
1678
1679	if (dcp) {
1680		ndi_dc_freehdl(dcp);
1681	}
1682
1683	if (cmd >= DEVCTL_IOC && cmd <= DEVCTL_IOC_MAX) {
1684		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
1685		    cport)->cport_mutex);
1686		cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
1687		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1688	}
1689
1690	return (rv);
1691}
1692
1693
1694/*
1695 * Create error retrieval sata packet
1696 *
1697 * A sata packet is allocated and set-up to contain specified error retrieval
1698 * command and appropriate dma-able data buffer.
1699 * No association with any scsi packet is made and no callback routine is
1700 * specified.
1701 *
1702 * Returns a pointer to sata packet upon successful packet creation.
1703 * Returns NULL, if packet cannot be created.
1704 */
1705sata_pkt_t *
1706sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device,
1707    int pkt_type)
1708{
1709	sata_hba_inst_t	*sata_hba_inst;
1710	sata_pkt_txlate_t *spx;
1711	sata_pkt_t *spkt;
1712	sata_drive_info_t *sdinfo;
1713
1714	mutex_enter(&sata_mutex);
1715	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1716	    sata_hba_inst = sata_hba_inst->satahba_next) {
1717		if (SATA_DIP(sata_hba_inst) == dip)
1718			break;
1719	}
1720	mutex_exit(&sata_mutex);
1721	ASSERT(sata_hba_inst != NULL);
1722
1723	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
1724	if (sdinfo == NULL) {
1725		sata_log(sata_hba_inst, CE_WARN,
1726		    "sata: error recovery request for non-attached device at "
1727		    "cport %d", sata_device->satadev_addr.cport);
1728		return (NULL);
1729	}
1730
1731	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1732	spx->txlt_sata_hba_inst = sata_hba_inst;
1733	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
1734	spkt = sata_pkt_alloc(spx, NULL);
1735	if (spkt == NULL) {
1736		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1737		return (NULL);
1738	}
1739	/* address is needed now */
1740	spkt->satapkt_device.satadev_addr = sata_device->satadev_addr;
1741
1742	switch (pkt_type) {
1743	case SATA_ERR_RETR_PKT_TYPE_NCQ:
1744		if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) {
1745			if (sata_check_for_dma_error(dip, spx)) {
1746				ddi_fm_service_impact(dip,
1747				    DDI_SERVICE_UNAFFECTED);
1748				break;
1749			}
1750			return (spkt);
1751		}
1752		break;
1753
1754	case SATA_ERR_RETR_PKT_TYPE_ATAPI:
1755		if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) {
1756			if (sata_check_for_dma_error(dip, spx)) {
1757				ddi_fm_service_impact(dip,
1758				    DDI_SERVICE_UNAFFECTED);
1759				break;
1760			}
1761			return (spkt);
1762		}
1763		break;
1764
1765	default:
1766		break;
1767	}
1768
1769	sata_pkt_free(spx);
1770	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1771	return (NULL);
1772
1773}
1774
1775
1776/*
1777 * Free error retrieval sata packet
1778 *
1779 * Free sata packet and any associated resources allocated previously by
1780 * sata_get_error_retrieval_pkt().
1781 *
1782 * Void return.
1783 */
1784void
1785sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt)
1786{
1787	sata_pkt_txlate_t *spx =
1788	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1789
1790	ASSERT(sata_pkt != NULL);
1791
1792	sata_free_local_buffer(spx);
1793	sata_pkt_free(spx);
1794	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1795
1796}
1797
1798/*
1799 * Create READ PORT MULTIPLIER and WRITE PORT MULTIPLIER sata packet
1800 *
1801 * No association with any scsi packet is made and no callback routine is
1802 * specified.
1803 *
1804 * Returns a pointer to sata packet upon successful packet creation.
1805 * Returns NULL, if packet cannot be created.
1806 *
1807 * NOTE: Input/Output value includes 64 bits accoring to SATA Spec 2.6,
1808 * only lower 32 bits are available currently.
1809 */
1810sata_pkt_t *
1811sata_get_rdwr_pmult_pkt(dev_info_t *dip, sata_device_t *sd,
1812    uint8_t regn, uint32_t regv, uint32_t type)
1813{
1814	sata_hba_inst_t	*sata_hba_inst;
1815	sata_pkt_txlate_t *spx;
1816	sata_pkt_t *spkt;
1817	sata_cmd_t *scmd;
1818
1819	/* Only READ/WRITE commands are accepted. */
1820	ASSERT(type == SATA_RDWR_PMULT_PKT_TYPE_READ ||
1821	    type == SATA_RDWR_PMULT_PKT_TYPE_WRITE);
1822
1823	mutex_enter(&sata_mutex);
1824	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1825	    sata_hba_inst = sata_hba_inst->satahba_next) {
1826		if (SATA_DIP(sata_hba_inst) == dip)
1827			break;
1828	}
1829	mutex_exit(&sata_mutex);
1830	ASSERT(sata_hba_inst != NULL);
1831
1832	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1833	spx->txlt_sata_hba_inst = sata_hba_inst;
1834	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
1835	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
1836	if (spkt == NULL) {
1837		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1838		return (NULL);
1839	}
1840
1841	/*
1842	 * NOTE: We need to send this command to the port multiplier,
1843	 * that means send to SATA_PMULT_HOSTPORT(0xf) pmport
1844	 *
1845	 * sata_device contains the address of actual target device, and the
1846	 * pmport number in the command comes from the sata_device structure.
1847	 */
1848	spkt->satapkt_device.satadev_addr = sd->satadev_addr;
1849	spkt->satapkt_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
1850	spkt->satapkt_device.satadev_addr.qual = SATA_ADDR_PMULT;
1851
1852	/* Fill sata_pkt */
1853	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_POLLING;
1854	spkt->satapkt_comp = NULL; /* Synchronous mode, no callback */
1855	spkt->satapkt_time = 10; /* Timeout 10s */
1856
1857	/* Build READ PORT MULTIPLIER cmd in the sata_pkt */
1858	scmd = &spkt->satapkt_cmd;
1859	scmd->satacmd_features_reg = regn & 0xff;
1860	scmd->satacmd_features_reg_ext = (regn >> 8) & 0xff;
1861	scmd->satacmd_device_reg = sd->satadev_addr.pmport;
1862	scmd->satacmd_addr_type = 0;		/* N/A */
1863
1864	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
1865
1866	if (type == SATA_RDWR_PMULT_PKT_TYPE_READ) {
1867		scmd->satacmd_cmd_reg = SATAC_READ_PORTMULT;
1868		scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
1869		scmd->satacmd_flags.sata_special_regs = 1;
1870		scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1;
1871		scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1;
1872		scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1;
1873		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1;
1874	} else if (type == SATA_RDWR_PMULT_PKT_TYPE_WRITE) {
1875		scmd->satacmd_cmd_reg = SATAC_WRITE_PORTMULT;
1876		scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
1877		scmd->satacmd_sec_count_lsb = regv & 0xff;
1878		scmd->satacmd_lba_low_lsb = regv >> 8 & 0xff;
1879		scmd->satacmd_lba_mid_lsb = regv >> 16 & 0xff;
1880		scmd->satacmd_lba_high_lsb = regv >> 24 & 0xff;
1881	}
1882
1883	return (spkt);
1884}
1885
1886/*
1887 * Free sata packet and any associated resources allocated previously by
1888 * sata_get_rdwr_pmult_pkt().
1889 *
1890 * Void return.
1891 */
1892void
1893sata_free_rdwr_pmult_pkt(sata_pkt_t *sata_pkt)
1894{
1895	sata_pkt_txlate_t *spx =
1896	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1897
1898	/* Free allocated resources */
1899	sata_pkt_free(spx);
1900	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1901}
1902
1903/*
1904 * Register a port multiplier to framework.
1905 * 1) Store the GSCR values in the previous allocated pmult_info strctures.
1906 * 2) Search in the blacklist and update the number of the device ports of the
1907 * port multiplier.
1908 *
1909 * Void return.
1910 */
1911void
1912sata_register_pmult(dev_info_t *dip, sata_device_t *sd, sata_pmult_gscr_t *sg)
1913{
1914	sata_hba_inst_t *sata_hba_inst = NULL;
1915	sata_pmult_info_t *pmultinfo;
1916	sata_pmult_bl_t *blp;
1917	int cport = sd->satadev_addr.cport;
1918
1919	mutex_enter(&sata_mutex);
1920	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1921	    sata_hba_inst = sata_hba_inst->satahba_next) {
1922		if (SATA_DIP(sata_hba_inst) == dip)
1923			if (sata_hba_inst->satahba_attached == 1)
1924				break;
1925	}
1926	mutex_exit(&sata_mutex);
1927	/* HBA not attached? */
1928	if (sata_hba_inst == NULL)
1929		return;
1930
1931	/* Number of pmports */
1932	sd->satadev_add_info = sg->gscr2 & SATA_PMULT_PORTNUM_MASK;
1933
1934	/* Check the blacklist */
1935	for (blp = sata_pmult_blacklist; blp->bl_gscr0; blp++) {
1936		if (sg->gscr0 != blp->bl_gscr0 && blp->bl_gscr0)
1937			continue;
1938		if (sg->gscr1 != blp->bl_gscr1 && blp->bl_gscr1)
1939			continue;
1940		if (sg->gscr2 != blp->bl_gscr2 && blp->bl_gscr2)
1941			continue;
1942
1943		cmn_err(CE_WARN, "!Port multiplier is on the blacklist.");
1944		sd->satadev_add_info = blp->bl_flags;
1945		break;
1946	}
1947
1948	/* Register the port multiplier GSCR */
1949	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
1950	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
1951	if (pmultinfo != NULL) {
1952		pmultinfo->pmult_gscr = *sg;
1953		pmultinfo->pmult_num_dev_ports =
1954		    sd->satadev_add_info & SATA_PMULT_PORTNUM_MASK;
1955		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
1956		    "Port multiplier registered at port %d", cport);
1957	}
1958	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
1959}
1960
1961/*
1962 * sata_split_model splits the model ID into vendor and product IDs.
1963 * It assumes that a vendor ID cannot be longer than 8 characters, and
1964 * that vendor and product ID are separated by a whitespace.
1965 */
1966void
1967sata_split_model(char *model, char **vendor, char **product)
1968{
1969	int i, modlen;
1970	char *vid, *pid;
1971
1972	/*
1973	 * remove whitespace at the end of model
1974	 */
1975	for (i = SATA_ID_MODEL_LEN; i > 0; i--)
1976		if (model[i] == ' ' || model[i] == '\t' || model[i] == '\0')
1977			model[i] = '\0';
1978		else
1979			break;
1980
1981	/*
1982	 * try to split model into into vid/pid
1983	 */
1984	modlen = strlen(model);
1985	for (i = 0, pid = model; i < modlen; i++, pid++)
1986		if ((*pid == ' ') || (*pid == '\t'))
1987			break;
1988
1989	/*
1990	 * only use vid if it is less than 8 chars (as in SCSI)
1991	 */
1992	if (i < modlen && i <= 8) {
1993		vid = model;
1994		/*
1995		 * terminate vid, establish pid
1996		 */
1997		*pid++ = '\0';
1998	} else {
1999		/*
2000		 * vid will stay "ATA     "
2001		 */
2002		vid = NULL;
2003		/*
2004		 * model is all pid
2005		 */
2006		pid = model;
2007	}
2008
2009	*vendor = vid;
2010	*product = pid;
2011}
2012
2013/*
2014 * sata_name_child is for composing the name of the node
2015 * the format of the name is "target,0".
2016 */
2017static int
2018sata_name_child(dev_info_t *dip, char *name, int namelen)
2019{
2020	int target;
2021
2022	target = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
2023	    DDI_PROP_DONTPASS, "target", -1);
2024	if (target == -1)
2025		return (DDI_FAILURE);
2026	(void) snprintf(name, namelen, "%x,0", target);
2027	return (DDI_SUCCESS);
2028}
2029
2030
2031
2032/* ****************** SCSA required entry points *********************** */
2033
2034/*
2035 * Implementation of scsi tran_tgt_init.
2036 * sata_scsi_tgt_init() initializes scsi_device structure
2037 *
2038 * If successful, DDI_SUCCESS is returned.
2039 * DDI_FAILURE is returned if addressed device does not exist
2040 */
2041
2042static int
2043sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2044    scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2045{
2046#ifndef __lock_lint
2047	_NOTE(ARGUNUSED(hba_dip))
2048	_NOTE(ARGUNUSED(tgt_dip))
2049#endif
2050	sata_device_t		sata_device;
2051	sata_drive_info_t	*sdinfo;
2052	struct sata_id		*sid;
2053	sata_hba_inst_t		*sata_hba_inst;
2054	char			model[SATA_ID_MODEL_LEN + 1];
2055	char			fw[SATA_ID_FW_LEN + 1];
2056	char			*vid, *pid;
2057
2058	/*
2059	 * Fail tran_tgt_init for .conf stub node
2060	 */
2061	if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
2062		(void) ndi_merge_node(tgt_dip, sata_name_child);
2063		ddi_set_name_addr(tgt_dip, NULL);
2064		return (DDI_FAILURE);
2065	}
2066
2067	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2068
2069	/* Validate scsi device address */
2070	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2071	    &sata_device) != 0)
2072		return (DDI_FAILURE);
2073
2074	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2075	    sata_device.satadev_addr.cport)));
2076
2077	/* sata_device now contains a valid sata address */
2078	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2079	if (sdinfo == NULL) {
2080		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2081		    sata_device.satadev_addr.cport)));
2082		return (DDI_FAILURE);
2083	}
2084	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2085	    sata_device.satadev_addr.cport)));
2086
2087	/*
2088	 * Check if we need to create a legacy devid (i.e cmdk style) for
2089	 * the target disks.
2090	 *
2091	 * HBA devinfo node will have the property "use-cmdk-devid-format"
2092	 * if we need to create cmdk-style devid for all the disk devices
2093	 * attached to this controller. This property may have been set
2094	 * from HBA driver's .conf file or by the HBA driver in its
2095	 * attach(9F) function.
2096	 */
2097	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2098	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2099	    "use-cmdk-devid-format", 0) == 1)) {
2100		/* register a legacy devid for this target node */
2101		sata_target_devid_register(tgt_dip, sdinfo);
2102	}
2103
2104
2105	/*
2106	 * 'Identify Device Data' does not always fit in standard SCSI
2107	 * INQUIRY data, so establish INQUIRY_* properties with full-form
2108	 * of information.
2109	 */
2110	sid = &sdinfo->satadrv_id;
2111#ifdef	_LITTLE_ENDIAN
2112	swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
2113	swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
2114#else	/* _LITTLE_ENDIAN */
2115	bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN);
2116	bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN);
2117#endif	/* _LITTLE_ENDIAN */
2118	model[SATA_ID_MODEL_LEN] = 0;
2119	fw[SATA_ID_FW_LEN] = 0;
2120
2121	sata_split_model(model, &vid, &pid);
2122
2123	if (vid)
2124		(void) scsi_device_prop_update_inqstring(sd, INQUIRY_VENDOR_ID,
2125		    vid, strlen(vid));
2126	if (pid)
2127		(void) scsi_device_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID,
2128		    pid, strlen(pid));
2129	(void) scsi_device_prop_update_inqstring(sd, INQUIRY_REVISION_ID,
2130	    fw, strlen(fw));
2131
2132	return (DDI_SUCCESS);
2133}
2134
2135/*
2136 * Implementation of scsi tran_tgt_probe.
2137 * Probe target, by calling default scsi routine scsi_hba_probe()
2138 */
2139static int
2140sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void))
2141{
2142	sata_hba_inst_t *sata_hba_inst =
2143	    (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private);
2144	int rval;
2145	uint32_t pm_cap;
2146
2147	rval = scsi_hba_probe(sd, callback);
2148	pm_cap = SATA_CAP_POWER_CONDITON | SATA_CAP_SMART_PAGE |
2149	    SATA_CAP_LOG_SENSE;
2150
2151	if (rval == SCSIPROBE_EXISTS) {
2152		/*
2153		 * Set property "pm-capable" on the target device node, so that
2154		 * the target driver will not try to fetch scsi cycle counters
2155		 * before enabling device power-management.
2156		 */
2157		if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
2158		    "pm-capable", pm_cap)) != DDI_PROP_SUCCESS) {
2159			sata_log(sata_hba_inst, CE_WARN,
2160			    "SATA device at port %d: "
2161			    "will not be power-managed ",
2162			    SCSI_TO_SATA_CPORT(sd->sd_address.a_target));
2163			SATA_LOG_D((sata_hba_inst, CE_WARN,
2164			    "failure updating pm-capable property"));
2165		}
2166	}
2167	return (rval);
2168}
2169
2170/*
2171 * Implementation of scsi tran_tgt_free.
2172 * Release all resources allocated for scsi_device
2173 */
2174static void
2175sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2176    scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2177{
2178#ifndef __lock_lint
2179	_NOTE(ARGUNUSED(hba_dip))
2180#endif
2181	sata_device_t		sata_device;
2182	sata_drive_info_t	*sdinfo;
2183	sata_hba_inst_t		*sata_hba_inst;
2184	ddi_devid_t		devid;
2185
2186	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2187
2188	/* Validate scsi device address */
2189	/*
2190	 * Note: tgt_free relates to the SCSA view of a device. If called, there
2191	 * was a device at this address, so even if the sata framework internal
2192	 * resources were alredy released because a device was detached,
2193	 * this function should be executed as long as its actions do
2194	 * not require the internal sata view of a device and the address
2195	 * refers to a valid sata address.
2196	 * Validating the address here means that we do not trust SCSA...
2197	 */
2198	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2199	    &sata_device) == -1)
2200		return;
2201
2202	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2203	    sata_device.satadev_addr.cport)));
2204
2205	/* sata_device now should contain a valid sata address */
2206	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2207	if (sdinfo == NULL) {
2208		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2209		    sata_device.satadev_addr.cport)));
2210		return;
2211	}
2212	/*
2213	 * We did not allocate any resources in sata_scsi_tgt_init()
2214	 * other than few properties.
2215	 * Free them.
2216	 */
2217	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2218	    sata_device.satadev_addr.cport)));
2219	(void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable");
2220
2221	/*
2222	 * If devid was previously created but not freed up from
2223	 * sd(7D) driver (i.e during detach(9F)) then do it here.
2224	 */
2225	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2226	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2227	    "use-cmdk-devid-format", 0) == 1) &&
2228	    (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) {
2229		ddi_devid_unregister(tgt_dip);
2230		ddi_devid_free(devid);
2231	}
2232}
2233
2234/*
2235 * Implementation of scsi tran_init_pkt
2236 * Upon successful return, scsi pkt buffer has DMA resources allocated.
2237 *
2238 * It seems that we should always allocate pkt, even if the address is
2239 * for non-existing device - just use some default for dma_attr.
2240 * The reason is that there is no way to communicate this to a caller here.
2241 * Subsequent call to sata_scsi_start may fail appropriately.
2242 * Simply returning NULL does not seem to discourage a target driver...
2243 *
2244 * Returns a pointer to initialized scsi_pkt, or NULL otherwise.
2245 */
2246static struct scsi_pkt *
2247sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
2248    struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
2249    int (*callback)(caddr_t), caddr_t arg)
2250{
2251	sata_hba_inst_t *sata_hba_inst =
2252	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2253	dev_info_t *dip = SATA_DIP(sata_hba_inst);
2254	sata_device_t sata_device;
2255	sata_drive_info_t *sdinfo;
2256	sata_pkt_txlate_t *spx;
2257	ddi_dma_attr_t cur_dma_attr;
2258	int rval;
2259	boolean_t new_pkt = B_TRUE;
2260
2261	ASSERT(ap->a_hba_tran->tran_hba_dip == dip);
2262
2263	/*
2264	 * We need to translate the address, even if it could be
2265	 * a bogus one, for a non-existing device
2266	 */
2267	sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
2268	sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target);
2269	sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2270	sata_device.satadev_rev = SATA_DEVICE_REV;
2271
2272	if (pkt == NULL) {
2273		/*
2274		 * Have to allocate a brand new scsi packet.
2275		 * We need to operate with auto request sense enabled.
2276		 */
2277		pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen,
2278		    MAX(statuslen, SATA_MAX_SENSE_LEN),
2279		    tgtlen, sizeof (sata_pkt_txlate_t), callback, arg);
2280
2281		if (pkt == NULL)
2282			return (NULL);
2283
2284		/* Fill scsi packet structure */
2285		pkt->pkt_comp		= (void (*)())NULL;
2286		pkt->pkt_time		= 0;
2287		pkt->pkt_resid		= 0;
2288		pkt->pkt_statistics	= 0;
2289		pkt->pkt_reason		= 0;
2290
2291		/*
2292		 * pkt_hba_private will point to sata pkt txlate structure
2293		 */
2294		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2295		bzero(spx, sizeof (sata_pkt_txlate_t));
2296
2297		spx->txlt_scsi_pkt = pkt;
2298		spx->txlt_sata_hba_inst = sata_hba_inst;
2299
2300		/* Allocate sata_pkt */
2301		spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback);
2302		if (spx->txlt_sata_pkt == NULL) {
2303			/* Could not allocate sata pkt */
2304			scsi_hba_pkt_free(ap, pkt);
2305			return (NULL);
2306		}
2307		/* Set sata address */
2308		spx->txlt_sata_pkt->satapkt_device.satadev_addr =
2309		    sata_device.satadev_addr;
2310		spx->txlt_sata_pkt->satapkt_device.satadev_rev =
2311		    sata_device.satadev_rev;
2312
2313		if ((bp == NULL) || (bp->b_bcount == 0))
2314			return (pkt);
2315
2316		spx->txlt_total_residue = bp->b_bcount;
2317	} else {
2318		new_pkt = B_FALSE;
2319		/*
2320		 * Packet was preallocated/initialized by previous call
2321		 */
2322		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2323
2324		if ((bp == NULL) || (bp->b_bcount == 0)) {
2325			return (pkt);
2326		}
2327
2328		/* Pkt is available already: spx->txlt_scsi_pkt == pkt; */
2329	}
2330
2331	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
2332
2333	/*
2334	 * We use an adjusted version of the dma_attr, to account
2335	 * for device addressing limitations.
2336	 * sata_adjust_dma_attr() will handle sdinfo == NULL which may
2337	 * happen when a device is not yet configured.
2338	 */
2339	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2340	    sata_device.satadev_addr.cport)));
2341	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2342	    &spx->txlt_sata_pkt->satapkt_device);
2343	/* NULL sdinfo may be passsed to sata_adjust_dma_attr() */
2344	sata_adjust_dma_attr(sdinfo,
2345	    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
2346	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2347	    sata_device.satadev_addr.cport)));
2348	/*
2349	 * Allocate necessary DMA resources for the packet's data buffer
2350	 * NOTE:
2351	 * In case of read/write commands, DMA resource allocation here is
2352	 * based on the premise that the transfer length specified in
2353	 * the read/write scsi cdb will match exactly DMA resources -
2354	 * returning correct packet residue is crucial.
2355	 */
2356	if ((rval = sata_dma_buf_setup(spx, flags, callback, arg,
2357	    &cur_dma_attr)) != DDI_SUCCESS) {
2358		/*
2359		 * If a DMA allocation request fails with
2360		 * DDI_DMA_NOMAPPING, indicate the error by calling
2361		 * bioerror(9F) with bp and an error code of EFAULT.
2362		 * If a DMA allocation request fails with
2363		 * DDI_DMA_TOOBIG, indicate the error by calling
2364		 * bioerror(9F) with bp and an error code of EINVAL.
2365		 * For DDI_DMA_NORESOURCES, we may have some of them allocated.
2366		 * Request may be repeated later - there is no real error.
2367		 */
2368		switch (rval) {
2369		case DDI_DMA_NORESOURCES:
2370			bioerror(bp, 0);
2371			break;
2372		case DDI_DMA_NOMAPPING:
2373		case DDI_DMA_BADATTR:
2374			bioerror(bp, EFAULT);
2375			break;
2376		case DDI_DMA_TOOBIG:
2377		default:
2378			bioerror(bp, EINVAL);
2379			break;
2380		}
2381		goto fail;
2382	}
2383
2384	if (sata_check_for_dma_error(dip, spx)) {
2385		ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED);
2386		bioerror(bp, EFAULT);
2387		goto fail;
2388	}
2389
2390success:
2391	/* Set number of bytes that are not yet accounted for */
2392	pkt->pkt_resid = spx->txlt_total_residue;
2393	ASSERT(pkt->pkt_resid >= 0);
2394
2395	return (pkt);
2396
2397fail:
2398	if (new_pkt == B_TRUE) {
2399		/*
2400		 * Since this is a new packet, we can clean-up
2401		 * everything
2402		 */
2403		sata_scsi_destroy_pkt(ap, pkt);
2404	} else {
2405		/*
2406		 * This is a re-used packet. It will be target driver's
2407		 * responsibility to eventually destroy it (which
2408		 * will free allocated resources).
2409		 * Here, we just "complete" the request, leaving
2410		 * allocated resources intact, so the request may
2411		 * be retried.
2412		 */
2413		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2414		sata_pkt_free(spx);
2415	}
2416	return (NULL);
2417}
2418
2419/*
2420 * Implementation of scsi tran_start.
2421 * Translate scsi cmd into sata operation and return status.
2422 * ATAPI CDBs are passed to ATAPI devices - the device determines what commands
2423 * are supported.
2424 * For SATA hard disks, supported scsi commands:
2425 * SCMD_INQUIRY
2426 * SCMD_TEST_UNIT_READY
2427 * SCMD_START_STOP
2428 * SCMD_READ_CAPACITY
2429 * SCMD_SVC_ACTION_IN_G4 (READ CAPACITY (16))
2430 * SCMD_REQUEST_SENSE
2431 * SCMD_LOG_SENSE_G1
2432 * SCMD_LOG_SELECT_G1
2433 * SCMD_MODE_SENSE	(specific pages)
2434 * SCMD_MODE_SENSE_G1	(specific pages)
2435 * SCMD_MODE_SELECT	(specific pages)
2436 * SCMD_MODE_SELECT_G1	(specific pages)
2437 * SCMD_SYNCHRONIZE_CACHE
2438 * SCMD_SYNCHRONIZE_CACHE_G1
2439 * SCMD_READ
2440 * SCMD_READ_G1
2441 * SCMD_READ_G4
2442 * SCMD_READ_G5
2443 * SCMD_WRITE
2444 * SCMD_WRITE_BUFFER
2445 * SCMD_WRITE_G1
2446 * SCMD_WRITE_G4
2447 * SCMD_WRITE_G5
2448 * SCMD_SEEK		(noop)
2449 * SCMD_SDIAG
2450 *
2451 * All other commands are rejected as unsupported.
2452 *
2453 * Returns:
2454 * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver
2455 * for execution. TRAN_ACCEPT may be returned also if device was removed but
2456 * a callback could be scheduled.
2457 * TRAN_BADPKT if cmd was directed to invalid address.
2458 * TRAN_FATAL_ERROR is command was rejected due to hardware error, including
2459 * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device
2460 * was removed and there was no callback specified in scsi pkt.
2461 * TRAN_BUSY if command could not be executed becasue HBA driver or SATA
2462 * framework was busy performing some other operation(s).
2463 *
2464 */
2465static int
2466sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
2467{
2468	sata_hba_inst_t *sata_hba_inst =
2469	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2470	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2471	sata_device_t *sdevice = &spx->txlt_sata_pkt->satapkt_device;
2472	sata_drive_info_t *sdinfo;
2473	struct buf *bp;
2474	uint8_t cport, pmport;
2475	boolean_t dev_gone = B_FALSE;
2476	int rval;
2477
2478	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2479	    "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]);
2480
2481	ASSERT(spx != NULL &&
2482	    spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL);
2483
2484	cport = SCSI_TO_SATA_CPORT(ap->a_target);
2485	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2486
2487	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2488
2489	if (sdevice->satadev_addr.qual == SATA_ADDR_DCPORT) {
2490		sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2491		if (sdinfo == NULL ||
2492		    SATA_CPORT_INFO(sata_hba_inst, cport)->
2493		    cport_tgtnode_clean == B_FALSE ||
2494		    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2495			dev_gone = B_TRUE;
2496		}
2497	} else if (sdevice->satadev_addr.qual == SATA_ADDR_DPMPORT) {
2498		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
2499		    SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst,
2500		    cport) == NULL) {
2501			dev_gone = B_TRUE;
2502		} else if (SATA_PMPORT_INFO(sata_hba_inst, cport,
2503		    pmport) == NULL) {
2504			dev_gone = B_TRUE;
2505		} else {
2506			mutex_enter(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2507			    cport, pmport)));
2508			sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2509			if (sdinfo == NULL ||
2510			    SATA_PMPORT_INFO(sata_hba_inst, cport, pmport)->
2511			    pmport_tgtnode_clean == B_FALSE ||
2512			    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2513				dev_gone = B_TRUE;
2514			}
2515			mutex_exit(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2516			    cport, pmport)));
2517		}
2518	}
2519
2520	if (dev_gone == B_TRUE) {
2521		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2522		pkt->pkt_reason = CMD_DEV_GONE;
2523		/*
2524		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2525		 * only in callback function (for normal requests) and
2526		 * in the dump code path.
2527		 * So, if the callback is available, we need to do
2528		 * the callback rather than returning TRAN_FATAL_ERROR here.
2529		 */
2530		if (pkt->pkt_comp != NULL) {
2531			/* scsi callback required */
2532			if (servicing_interrupt()) {
2533				if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2534				    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2535				    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
2536				    TASKQID_INVALID) {
2537					return (TRAN_BUSY);
2538				}
2539			} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2540			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2541			    spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
2542				/* Scheduling the callback failed */
2543				return (TRAN_BUSY);
2544			}
2545			return (TRAN_ACCEPT);
2546		}
2547		/* No callback available */
2548		return (TRAN_FATAL_ERROR);
2549	}
2550
2551	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
2552		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2553		rval = sata_txlt_atapi(spx);
2554		SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2555		    "sata_scsi_start atapi: rval %d\n", rval);
2556		return (rval);
2557	}
2558	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2559
2560	/*
2561	 * Checking for power state, if it was on
2562	 * STOPPED state, then the drive is not capable
2563	 * of processing media access command.  And
2564	 * TEST_UNIT_READY, REQUEST_SENSE has special handling
2565	 * in the function for different power state.
2566	 */
2567	if (((sdinfo->satadrv_power_level == SATA_POWER_STANDBY) ||
2568	    (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)) &&
2569	    (SATA_IS_MEDIUM_ACCESS_CMD(pkt->pkt_cdbp[0]))) {
2570		return (sata_txlt_check_condition(spx, KEY_NOT_READY,
2571		    SD_SCSI_ASC_LU_NOT_READY));
2572	}
2573
2574	/* ATA Disk commands processing starts here */
2575
2576	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2577
2578	switch (pkt->pkt_cdbp[0]) {
2579
2580	case SCMD_INQUIRY:
2581		/* Mapped to identify device */
2582		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2583			bp_mapin(bp);
2584		rval = sata_txlt_inquiry(spx);
2585		break;
2586
2587	case SCMD_TEST_UNIT_READY:
2588		/*
2589		 * SAT "SATA to ATA Translation" doc specifies translation
2590		 * to ATA CHECK POWER MODE.
2591		 */
2592		rval = sata_txlt_test_unit_ready(spx);
2593		break;
2594
2595	case SCMD_START_STOP:
2596		/* Mapping depends on the command */
2597		rval = sata_txlt_start_stop_unit(spx);
2598		break;
2599
2600	case SCMD_READ_CAPACITY:
2601		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2602			bp_mapin(bp);
2603		rval = sata_txlt_read_capacity(spx);
2604		break;
2605
2606	case SCMD_SVC_ACTION_IN_G4:		/* READ CAPACITY (16) */
2607		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2608			bp_mapin(bp);
2609		rval = sata_txlt_read_capacity16(spx);
2610		break;
2611
2612	case SCMD_REQUEST_SENSE:
2613		/*
2614		 * Always No Sense, since we force ARQ
2615		 */
2616		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2617			bp_mapin(bp);
2618		rval = sata_txlt_request_sense(spx);
2619		break;
2620
2621	case SCMD_LOG_SENSE_G1:
2622		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2623			bp_mapin(bp);
2624		rval = sata_txlt_log_sense(spx);
2625		break;
2626
2627	case SCMD_LOG_SELECT_G1:
2628		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2629			bp_mapin(bp);
2630		rval = sata_txlt_log_select(spx);
2631		break;
2632
2633	case SCMD_MODE_SENSE:
2634	case SCMD_MODE_SENSE_G1:
2635		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2636			bp_mapin(bp);
2637		rval = sata_txlt_mode_sense(spx);
2638		break;
2639
2640
2641	case SCMD_MODE_SELECT:
2642	case SCMD_MODE_SELECT_G1:
2643		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2644			bp_mapin(bp);
2645		rval = sata_txlt_mode_select(spx);
2646		break;
2647
2648	case SCMD_SYNCHRONIZE_CACHE:
2649	case SCMD_SYNCHRONIZE_CACHE_G1:
2650		rval = sata_txlt_synchronize_cache(spx);
2651		break;
2652
2653	case SCMD_READ:
2654	case SCMD_READ_G1:
2655	case SCMD_READ_G4:
2656	case SCMD_READ_G5:
2657		rval = sata_txlt_read(spx);
2658		break;
2659	case SCMD_WRITE_BUFFER:
2660		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2661			bp_mapin(bp);
2662		rval = sata_txlt_write_buffer(spx);
2663		break;
2664
2665	case SCMD_WRITE:
2666	case SCMD_WRITE_G1:
2667	case SCMD_WRITE_G4:
2668	case SCMD_WRITE_G5:
2669		rval = sata_txlt_write(spx);
2670		break;
2671
2672	case SCMD_SEEK:
2673		rval = sata_txlt_nodata_cmd_immediate(spx);
2674		break;
2675
2676	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12:
2677	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16:
2678		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2679			bp_mapin(bp);
2680		rval = sata_txlt_ata_pass_thru(spx);
2681		break;
2682
2683		/* Other cases will be filed later */
2684		/* postponed until phase 2 of the development */
2685	case SPC3_CMD_UNMAP:
2686		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2687			bp_mapin(bp);
2688		rval = sata_txlt_unmap(spx);
2689		break;
2690	default:
2691		rval = sata_txlt_invalid_command(spx);
2692		break;
2693	}
2694
2695	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2696	    "sata_scsi_start: rval %d\n", rval);
2697
2698	return (rval);
2699}
2700
2701/*
2702 * Implementation of scsi tran_abort.
2703 * Abort specific pkt or all packets.
2704 *
2705 * Returns 1 if one or more packets were aborted, returns 0 otherwise
2706 *
2707 * May be called from an interrupt level.
2708 */
2709static int
2710sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt)
2711{
2712	sata_hba_inst_t *sata_hba_inst =
2713	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2714	sata_device_t	sata_device;
2715	sata_pkt_t	*sata_pkt;
2716
2717	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2718	    "sata_scsi_abort: %s at target: 0x%x\n",
2719	    scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target);
2720
2721	/* Validate address */
2722	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0)
2723		/* Invalid address */
2724		return (0);
2725
2726	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2727	    sata_device.satadev_addr.cport)));
2728	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2729		/* invalid address */
2730		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2731		    sata_device.satadev_addr.cport)));
2732		return (0);
2733	}
2734	if (scsi_pkt == NULL) {
2735		/*
2736		 * Abort all packets.
2737		 * Although we do not have specific packet, we still need
2738		 * dummy packet structure to pass device address to HBA.
2739		 * Allocate one, without sleeping. Fail if pkt cannot be
2740		 * allocated.
2741		 */
2742		sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP);
2743		if (sata_pkt == NULL) {
2744			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2745			    sata_device.satadev_addr.cport)));
2746			SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: "
2747			    "could not allocate sata_pkt"));
2748			return (0);
2749		}
2750		sata_pkt->satapkt_rev = SATA_PKT_REV;
2751		sata_pkt->satapkt_device = sata_device;
2752		sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
2753	} else {
2754		if (scsi_pkt->pkt_ha_private == NULL) {
2755			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2756			    sata_device.satadev_addr.cport)));
2757			return (0); /* Bad scsi pkt */
2758		}
2759		/* extract pointer to sata pkt */
2760		sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)->
2761		    txlt_sata_pkt;
2762	}
2763
2764	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2765	    sata_device.satadev_addr.cport)));
2766	/* Send abort request to HBA */
2767	if ((*SATA_ABORT_FUNC(sata_hba_inst))
2768	    (SATA_DIP(sata_hba_inst), sata_pkt,
2769	    scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) ==
2770	    SATA_SUCCESS) {
2771		if (scsi_pkt == NULL)
2772			kmem_free(sata_pkt, sizeof (sata_pkt_t));
2773		/* Success */
2774		return (1);
2775	}
2776	/* Else, something did not go right */
2777	if (scsi_pkt == NULL)
2778		kmem_free(sata_pkt, sizeof (sata_pkt_t));
2779	/* Failure */
2780	return (0);
2781}
2782
2783
2784/*
2785 * Implementation of scsi tran_reset.
2786 * RESET_ALL request is translated into port reset.
2787 * RESET_TARGET requests is translated into a device reset,
2788 * RESET_LUN request is accepted only for LUN 0 and translated into
2789 * device reset.
2790 * The target reset should cause all HBA active and queued packets to
2791 * be terminated and returned with pkt reason SATA_PKT_RESET prior to
2792 * the return. HBA should report reset event for the device.
2793 *
2794 * Returns 1 upon success, 0 upon failure.
2795 */
2796static int
2797sata_scsi_reset(struct scsi_address *ap, int level)
2798{
2799	sata_hba_inst_t	*sata_hba_inst =
2800	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2801	sata_device_t	sata_device;
2802	int		val;
2803
2804	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2805	    "sata_scsi_reset: level %d target: 0x%x\n",
2806	    level, ap->a_target);
2807
2808	/* Validate address */
2809	val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device);
2810	if (val == -1)
2811		/* Invalid address */
2812		return (0);
2813
2814	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2815	    sata_device.satadev_addr.cport)));
2816	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2817		/* invalid address */
2818		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2819		    sata_device.satadev_addr.cport)));
2820		return (0);
2821	}
2822	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2823	    sata_device.satadev_addr.cport)));
2824	if (level == RESET_ALL) {
2825		/* port reset */
2826		if (sata_device.satadev_addr.qual == SATA_ADDR_DCPORT)
2827			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2828		else
2829			sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
2830
2831		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2832		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2833			return (1);
2834		else
2835			return (0);
2836
2837	} else if (val == 0 &&
2838	    (level == RESET_TARGET || level == RESET_LUN)) {
2839		/* reset device (device attached) */
2840		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2841		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2842			return (1);
2843		else
2844			return (0);
2845	}
2846	return (0);
2847}
2848
2849
2850/*
2851 * Implementation of scsi tran_getcap (get transport/device capabilities).
2852 * Supported capabilities for SATA hard disks:
2853 * auto-rqsense		(always supported)
2854 * tagged-qing		(supported if HBA supports it)
2855 * untagged-qing	(could be supported if disk supports it, but because
2856 *			 caching behavior allowing untagged queuing actually
2857 *			 results in reduced performance.  sd tries to throttle
2858 *			 back to only 3 outstanding commands, which may
2859 *			 work for real SCSI disks, but with read ahead
2860 *			 caching, having more than 1 outstanding command
2861 *			 results in cache thrashing.)
2862 * sector_size
2863 * dma_max
2864 * interconnect-type	(INTERCONNECT_SATA)
2865 *
2866 * Supported capabilities for ATAPI CD/DVD devices:
2867 * auto-rqsense		(always supported)
2868 * sector_size
2869 * dma_max
2870 * max-cdb-length
2871 * interconnect-type	(INTERCONNECT_SATA)
2872 *
2873 * Supported capabilities for ATAPI TAPE devices:
2874 * auto-rqsense		(always supported)
2875 * dma_max
2876 * max-cdb-length
2877 *
2878 * Supported capabilities for SATA ATAPI hard disks:
2879 * auto-rqsense		(always supported)
2880 * interconnect-type	(INTERCONNECT_SATA)
2881 * max-cdb-length
2882 *
2883 * Request for other capabilities is rejected as unsupported.
2884 *
2885 * Returns supported capability value, or -1 if capability is unsuppported or
2886 * the address is invalid - no device.
2887 */
2888
2889static int
2890sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
2891{
2892
2893	sata_hba_inst_t		*sata_hba_inst =
2894	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2895	sata_device_t		sata_device;
2896	sata_drive_info_t	*sdinfo;
2897	ddi_dma_attr_t		adj_dma_attr;
2898	int			rval;
2899
2900	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2901	    "sata_scsi_getcap: target: 0x%x, cap: %s\n",
2902	    ap->a_target, cap);
2903
2904	/*
2905	 * We want to process the capabilities on per port granularity.
2906	 * So, we are specifically restricting ourselves to whom != 0
2907	 * to exclude the controller wide handling.
2908	 */
2909	if (cap == NULL || whom == 0)
2910		return (-1);
2911
2912	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2913		/* Invalid address */
2914		return (-1);
2915	}
2916	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2917	    sata_device.satadev_addr.cport)));
2918	if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) ==
2919	    NULL) {
2920		/* invalid address */
2921		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2922		    sata_device.satadev_addr.cport)));
2923		return (-1);
2924	}
2925
2926	switch (scsi_hba_lookup_capstr(cap)) {
2927	case SCSI_CAP_ARQ:
2928		rval = 1;		/* ARQ supported, turned on */
2929		break;
2930
2931	case SCSI_CAP_SECTOR_SIZE:
2932		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK)
2933			rval = SATA_DISK_SECTOR_SIZE;	/* fixed size */
2934		else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD)
2935			rval = SATA_ATAPI_SECTOR_SIZE;
2936		else rval = -1;
2937		break;
2938
2939	/*
2940	 * untagged queuing cause a performance inversion because of
2941	 * the way sd operates.  Because of this reason we do not
2942	 * use it when available.
2943	 */
2944	case SCSI_CAP_UNTAGGED_QING:
2945		if (sdinfo->satadrv_features_enabled &
2946		    SATA_DEV_F_E_UNTAGGED_QING)
2947			rval = 1;	/* Untagged queuing available */
2948		else
2949			rval = -1;	/* Untagged queuing not available */
2950		break;
2951
2952	case SCSI_CAP_TAGGED_QING:
2953		if ((sdinfo->satadrv_features_enabled &
2954		    SATA_DEV_F_E_TAGGED_QING) &&
2955		    (sdinfo->satadrv_max_queue_depth > 1))
2956			rval = 1;	/* Tagged queuing available */
2957		else
2958			rval = -1;	/* Tagged queuing not available */
2959		break;
2960
2961	case SCSI_CAP_DMA_MAX:
2962		sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst),
2963		    &adj_dma_attr);
2964		rval = (int)adj_dma_attr.dma_attr_maxxfer;
2965		/* We rely on the fact that dma_attr_maxxfer < 0x80000000 */
2966		break;
2967
2968	case SCSI_CAP_INTERCONNECT_TYPE:
2969		rval = INTERCONNECT_SATA;	/* SATA interconnect type */
2970		break;
2971
2972	case SCSI_CAP_CDB_LEN:
2973		if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI)
2974			rval = sdinfo->satadrv_atapi_cdb_len;
2975		else
2976			rval = -1;
2977		break;
2978
2979	default:
2980		rval = -1;
2981		break;
2982	}
2983	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2984	    sata_device.satadev_addr.cport)));
2985	return (rval);
2986}
2987
2988/*
2989 * Implementation of scsi tran_setcap
2990 *
2991 * Only SCSI_CAP_UNTAGGED_QING and  SCSI_CAP_TAGGED_QING are changeable.
2992 *
2993 */
2994static int
2995sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
2996{
2997	sata_hba_inst_t	*sata_hba_inst =
2998	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2999	sata_device_t	sata_device;
3000	sata_drive_info_t	*sdinfo;
3001	int		rval;
3002
3003	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
3004	    "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap);
3005
3006	/*
3007	 * We want to process the capabilities on per port granularity.
3008	 * So, we are specifically restricting ourselves to whom != 0
3009	 * to exclude the controller wide handling.
3010	 */
3011	if (cap == NULL || whom == 0) {
3012		return (-1);
3013	}
3014
3015	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
3016		/* Invalid address */
3017		return (-1);
3018	}
3019	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
3020	    sata_device.satadev_addr.cport)));
3021	if ((sdinfo = sata_get_device_info(sata_hba_inst,
3022	    &sata_device)) == NULL) {
3023		/* invalid address */
3024		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3025		    sata_device.satadev_addr.cport)));
3026		return (-1);
3027	}
3028	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3029	    sata_device.satadev_addr.cport)));
3030
3031	switch (scsi_hba_lookup_capstr(cap)) {
3032	case SCSI_CAP_ARQ:
3033	case SCSI_CAP_SECTOR_SIZE:
3034	case SCSI_CAP_DMA_MAX:
3035	case SCSI_CAP_INTERCONNECT_TYPE:
3036		rval = 0;
3037		break;
3038	case SCSI_CAP_UNTAGGED_QING:
3039		if (SATA_QDEPTH(sata_hba_inst) > 1) {
3040			rval = 1;
3041			if (value == 1) {
3042				sdinfo->satadrv_features_enabled |=
3043				    SATA_DEV_F_E_UNTAGGED_QING;
3044			} else if (value == 0) {
3045				sdinfo->satadrv_features_enabled &=
3046				    ~SATA_DEV_F_E_UNTAGGED_QING;
3047			} else {
3048				rval = -1;
3049			}
3050		} else {
3051			rval = 0;
3052		}
3053		break;
3054	case SCSI_CAP_TAGGED_QING:
3055		/* This can TCQ or NCQ */
3056		if (sata_func_enable & SATA_ENABLE_QUEUING &&
3057		    ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ &&
3058		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) ||
3059		    (sata_func_enable & SATA_ENABLE_NCQ &&
3060		    sdinfo->satadrv_features_support & SATA_DEV_F_NCQ &&
3061		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) &&
3062		    (sdinfo->satadrv_max_queue_depth > 1)) {
3063			rval = 1;
3064			if (value == 1) {
3065				sdinfo->satadrv_features_enabled |=
3066				    SATA_DEV_F_E_TAGGED_QING;
3067			} else if (value == 0) {
3068				sdinfo->satadrv_features_enabled &=
3069				    ~SATA_DEV_F_E_TAGGED_QING;
3070			} else {
3071				rval = -1;
3072			}
3073		} else {
3074			rval = 0;
3075		}
3076		break;
3077	default:
3078		rval = -1;
3079		break;
3080	}
3081	return (rval);
3082}
3083
3084/*
3085 * Implementations of scsi tran_destroy_pkt.
3086 * Free resources allocated by sata_scsi_init_pkt()
3087 */
3088static void
3089sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3090{
3091	sata_pkt_txlate_t *spx;
3092
3093	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3094
3095	sata_common_free_dma_rsrcs(spx);
3096
3097	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
3098	sata_pkt_free(spx);
3099
3100	scsi_hba_pkt_free(ap, pkt);
3101}
3102
3103/*
3104 * Implementation of scsi tran_dmafree.
3105 * Free DMA resources allocated by sata_scsi_init_pkt()
3106 */
3107
3108static void
3109sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
3110{
3111#ifndef __lock_lint
3112	_NOTE(ARGUNUSED(ap))
3113#endif
3114	sata_pkt_txlate_t *spx;
3115
3116	ASSERT(pkt != NULL);
3117	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3118
3119	sata_common_free_dma_rsrcs(spx);
3120}
3121
3122/*
3123 * Implementation of scsi tran_sync_pkt.
3124 *
3125 * The assumption below is that pkt is unique - there is no need to check ap
3126 *
3127 * Synchronize DMA buffer and, if the intermediate buffer is used, copy data
3128 * into/from the real buffer.
3129 */
3130static void
3131sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3132{
3133#ifndef __lock_lint
3134	_NOTE(ARGUNUSED(ap))
3135#endif
3136	int rval;
3137	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3138	struct buf *bp;
3139	int direction;
3140
3141	ASSERT(spx != NULL);
3142	if (spx->txlt_buf_dma_handle != NULL) {
3143		direction = spx->txlt_sata_pkt->
3144		    satapkt_cmd.satacmd_flags.sata_data_direction;
3145		if (spx->txlt_sata_pkt != NULL &&
3146		    direction != SATA_DIR_NODATA_XFER) {
3147			if (spx->txlt_tmp_buf != NULL) {
3148				/* Intermediate DMA buffer used */
3149				bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3150
3151				if (direction & SATA_DIR_WRITE) {
3152					bcopy(bp->b_un.b_addr,
3153					    spx->txlt_tmp_buf, bp->b_bcount);
3154				}
3155			}
3156			/* Sync the buffer for device or for CPU */
3157			rval = ddi_dma_sync(spx->txlt_buf_dma_handle,   0, 0,
3158			    (direction & SATA_DIR_WRITE) ?
3159			    DDI_DMA_SYNC_FORDEV :  DDI_DMA_SYNC_FORCPU);
3160			ASSERT(rval == DDI_SUCCESS);
3161			if (spx->txlt_tmp_buf != NULL &&
3162			    !(direction & SATA_DIR_WRITE)) {
3163				/* Intermediate DMA buffer used for read */
3164				bcopy(spx->txlt_tmp_buf,
3165				    bp->b_un.b_addr, bp->b_bcount);
3166			}
3167
3168		}
3169	}
3170}
3171
3172
3173
3174/* *******************  SATA - SCSI Translation functions **************** */
3175/*
3176 * SCSI to SATA pkt and command translation and SATA to SCSI status/error
3177 * translation.
3178 */
3179
3180/*
3181 * Checks if a device exists and can be access and translates common
3182 * scsi_pkt data to sata_pkt data.
3183 *
3184 * Flag argument indicates that a non-read/write ATA command may be sent
3185 * to HBA in arbitrary SYNC mode to execute this packet.
3186 *
3187 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and
3188 * sata_pkt was set-up.
3189 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not
3190 * exist and pkt_comp callback was scheduled.
3191 * Returns other TRAN_XXXXX values when error occured and command should be
3192 * rejected with the returned TRAN_XXXXX value.
3193 *
3194 * This function should be called with port mutex held.
3195 */
3196static int
3197sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason, int flag)
3198{
3199	sata_drive_info_t *sdinfo;
3200	sata_device_t sata_device;
3201	const struct sata_cmd_flags sata_initial_cmd_flags = {
3202		SATA_DIR_NODATA_XFER,
3203		/* all other values to 0/FALSE */
3204	};
3205	/*
3206	 * Pkt_reason has to be set if the pkt_comp callback is invoked,
3207	 * and that implies TRAN_ACCEPT return value. Any other returned value
3208	 * indicates that the scsi packet was not accepted (the reason will not
3209	 * be checked by the scsi target driver).
3210	 * To make debugging easier, we set pkt_reason to know value here.
3211	 * It may be changed later when different completion reason is
3212	 * determined.
3213	 */
3214	spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
3215	*reason = CMD_TRAN_ERR;
3216
3217	/* Validate address */
3218	switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst,
3219	    &spx->txlt_scsi_pkt->pkt_address, &sata_device)) {
3220
3221	case -1:
3222		/* Invalid address or invalid device type */
3223		return (TRAN_BADPKT);
3224	case 2:
3225		/*
3226		 * Valid address but device type is unknown - Chack if it is
3227		 * in the reset state and therefore in an indeterminate state.
3228		 */
3229		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3230		    &spx->txlt_sata_pkt->satapkt_device);
3231		if (sdinfo != NULL && (sdinfo->satadrv_event_flags &
3232		    (SATA_EVNT_DEVICE_RESET |
3233		    SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3234			if (!ddi_in_panic()) {
3235				spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3236				*reason = CMD_INCOMPLETE;
3237				SATADBG1(SATA_DBG_SCSI_IF,
3238				    spx->txlt_sata_hba_inst,
3239				    "sata_scsi_start: rejecting command "
3240				    "because of device reset state\n", NULL);
3241				return (TRAN_BUSY);
3242			}
3243		}
3244		/* FALLTHROUGH */
3245	case 1:
3246		/* valid address but no valid device - it has disappeared */
3247		spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE;
3248		*reason = CMD_DEV_GONE;
3249		/*
3250		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
3251		 * only in callback function (for normal requests) and
3252		 * in the dump code path.
3253		 * So, if the callback is available, we need to do
3254		 * the callback rather than returning TRAN_FATAL_ERROR here.
3255		 */
3256		if (spx->txlt_scsi_pkt->pkt_comp != NULL) {
3257			/* scsi callback required */
3258			if (servicing_interrupt()) {
3259				if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3260				    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3261				    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3262				    TASKQID_INVALID) {
3263					return (TRAN_BUSY);
3264				}
3265			} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3266			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3267			    spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
3268				/* Scheduling the callback failed */
3269				return (TRAN_BUSY);
3270			}
3271
3272			return (TRAN_ACCEPT);
3273		}
3274		return (TRAN_FATAL_ERROR);
3275	default:
3276		/* all OK; pkt reason will be overwritten later */
3277		break;
3278	}
3279	/*
3280	 * If pkt is to be executed in polling mode and a command will not be
3281	 * emulated in SATA module (requires sending a non-read/write ATA
3282	 * command to HBA driver in arbitrary SYNC mode) and we are in the
3283	 * interrupt context and not in the panic dump, then reject the packet
3284	 * to avoid a possible interrupt stack overrun or hang caused by
3285	 * a potentially blocked interrupt.
3286	 */
3287	if (((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0 || flag != 0) &&
3288	    servicing_interrupt() && !ddi_in_panic()) {
3289		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
3290		    "sata_scsi_start: rejecting synchronous command because "
3291		    "of interrupt context\n", NULL);
3292		return (TRAN_BUSY);
3293	}
3294
3295	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3296	    &spx->txlt_sata_pkt->satapkt_device);
3297
3298	/*
3299	 * If device is in reset condition, reject the packet with
3300	 * TRAN_BUSY, unless:
3301	 * 1. system is panicking (dumping)
3302	 * In such case only one thread is running and there is no way to
3303	 * process reset.
3304	 * 2. cfgadm operation is is progress (internal APCTL lock is set)
3305	 * Some cfgadm operations involve drive commands, so reset condition
3306	 * needs to be ignored for IOCTL operations.
3307	 */
3308	if ((sdinfo->satadrv_event_flags &
3309	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3310
3311		if (!ddi_in_panic() &&
3312		    ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst,
3313		    sata_device.satadev_addr.cport) &
3314		    SATA_APCTL_LOCK_PORT_BUSY) == 0)) {
3315			spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3316			*reason = CMD_INCOMPLETE;
3317			SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3318			    "sata_scsi_start: rejecting command because "
3319			    "of device reset state\n", NULL);
3320			return (TRAN_BUSY);
3321		}
3322	}
3323
3324	/*
3325	 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by
3326	 * sata_scsi_pkt_init() because pkt init had to work also with
3327	 * non-existing devices.
3328	 * Now we know that the packet was set-up for a real device, so its
3329	 * type is known.
3330	 */
3331	spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type;
3332
3333	spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags;
3334	if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst,
3335	    sata_device.satadev_addr.cport)->cport_event_flags &
3336	    SATA_APCTL_LOCK_PORT_BUSY) != 0) {
3337		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3338		    sata_ignore_dev_reset = B_TRUE;
3339	}
3340	/*
3341	 * At this point the generic translation routine determined that the
3342	 * scsi packet should be accepted. Packet completion reason may be
3343	 * changed later when a different completion reason is determined.
3344	 */
3345	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3346	*reason = CMD_CMPLT;
3347
3348	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
3349		/* Synchronous execution */
3350		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH |
3351		    SATA_OPMODE_POLLING;
3352		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3353		    sata_ignore_dev_reset = ddi_in_panic();
3354	} else {
3355		/* Asynchronous execution */
3356		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH |
3357		    SATA_OPMODE_INTERRUPTS;
3358	}
3359	/* Convert queuing information */
3360	if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG)
3361		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag =
3362		    B_TRUE;
3363	else if (spx->txlt_scsi_pkt->pkt_flags &
3364	    (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD))
3365		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag =
3366		    B_TRUE;
3367
3368	/* Always limit pkt time */
3369	if (spx->txlt_scsi_pkt->pkt_time == 0)
3370		spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time;
3371	else
3372		/* Pass on scsi_pkt time */
3373		spx->txlt_sata_pkt->satapkt_time =
3374		    spx->txlt_scsi_pkt->pkt_time;
3375
3376	return (TRAN_ACCEPT);
3377}
3378
3379
3380/*
3381 * Translate ATA Identify Device data to SCSI Inquiry data.
3382 * This function may be called only for ATA devices.
3383 * This function should not be called for ATAPI devices - they
3384 * respond directly to SCSI Inquiry command.
3385 *
3386 * SATA Identify Device data has to be valid in sata_drive_info.
3387 * Buffer has to accomodate the inquiry length (36 bytes).
3388 *
3389 * This function should be called with a port mutex held.
3390 */
3391static	void
3392sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst,
3393    sata_drive_info_t *sdinfo, uint8_t *buf)
3394{
3395
3396	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
3397	struct sata_id *sid = &sdinfo->satadrv_id;
3398
3399	/* Start with a nice clean slate */
3400	bzero((void *)inq, sizeof (struct scsi_inquiry));
3401
3402	/*
3403	 * Rely on the dev_type for setting paripheral qualifier.
3404	 * Assume that  DTYPE_RODIRECT applies to CD/DVD R/W devices.
3405	 * It could be that DTYPE_OPTICAL could also qualify in the future.
3406	 * ATAPI Inquiry may provide more data to the target driver.
3407	 */
3408	inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3409	    DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */
3410
3411	/* CFA type device is not a removable media device */
3412	inq->inq_rmb = ((sid->ai_config != SATA_CFA_TYPE) &&
3413	    (sid->ai_config & SATA_REM_MEDIA)) ? 1 : 0;
3414	inq->inq_qual = 0;	/* Device type qualifier (obsolete in SCSI3? */
3415	inq->inq_iso = 0;	/* ISO version */
3416	inq->inq_ecma = 0;	/* ECMA version */
3417	inq->inq_ansi = 3;	/* ANSI version - SCSI 3 */
3418	inq->inq_aenc = 0;	/* Async event notification cap. */
3419	inq->inq_trmiop = 0;	/* Supports TERMINATE I/O PROC msg - NO */
3420	inq->inq_normaca = 0;	/* setting NACA bit supported - NO */
3421	inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */
3422	inq->inq_len = 31;	/* Additional length */
3423	inq->inq_dualp = 0;	/* dual port device - NO */
3424	inq->inq_reladdr = 0;	/* Supports relative addressing - NO */
3425	inq->inq_sync = 0;	/* Supports synchronous data xfers - NO */
3426	inq->inq_linked = 0;	/* Supports linked commands - NO */
3427				/*
3428				 * Queuing support - controller has to
3429				 * support some sort of command queuing.
3430				 */
3431	if (SATA_QDEPTH(sata_hba_inst) > 1)
3432		inq->inq_cmdque = 1; /* Supports command queueing - YES */
3433	else
3434		inq->inq_cmdque = 0; /* Supports command queueing - NO */
3435	inq->inq_sftre = 0;	/* Supports Soft Reset option - NO ??? */
3436	inq->inq_wbus32 = 0;	/* Supports 32 bit wide data xfers - NO */
3437	inq->inq_wbus16 = 0;	/* Supports 16 bit wide data xfers - NO */
3438
3439#ifdef	_LITTLE_ENDIAN
3440	/* Swap text fields to match SCSI format */
3441	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3442	swab(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3443	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3444		swab(sid->ai_fw, inq->inq_revision, 4);	/* Revision level */
3445	else
3446		swab(&sid->ai_fw[4], inq->inq_revision, 4);	/* Rev. level */
3447#else	/* _LITTLE_ENDIAN */
3448	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3449	bcopy(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3450	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3451		bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
3452	else
3453		bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
3454#endif	/* _LITTLE_ENDIAN */
3455}
3456
3457
3458/*
3459 * Scsi response set up for invalid command (command not supported)
3460 *
3461 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3462 */
3463static int
3464sata_txlt_invalid_command(sata_pkt_txlate_t *spx)
3465{
3466	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3467	struct scsi_extended_sense *sense;
3468
3469	scsipkt->pkt_reason = CMD_CMPLT;
3470	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3471	    STATE_SENT_CMD | STATE_GOT_STATUS;
3472
3473	*scsipkt->pkt_scbp = STATUS_CHECK;
3474
3475	sense = sata_arq_sense(spx);
3476	sense->es_key = KEY_ILLEGAL_REQUEST;
3477	sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE;
3478
3479	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3480	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3481
3482	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3483	    scsipkt->pkt_comp != NULL) {
3484		/* scsi callback required */
3485		if (servicing_interrupt()) {
3486			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3487			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3488			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3489			    TASKQID_INVALID) {
3490				return (TRAN_BUSY);
3491			}
3492		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3493		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3494		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
3495			/* Scheduling the callback failed */
3496			return (TRAN_BUSY);
3497		}
3498	}
3499	return (TRAN_ACCEPT);
3500}
3501
3502/*
3503 * Scsi response set up for check condition with special sense key
3504 * and additional sense code.
3505 *
3506 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3507 */
3508static int
3509sata_txlt_check_condition(sata_pkt_txlate_t *spx, uchar_t key, uchar_t code)
3510{
3511	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3512	int cport = SATA_TXLT_CPORT(spx);
3513	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3514	struct scsi_extended_sense *sense;
3515
3516	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3517	scsipkt->pkt_reason = CMD_CMPLT;
3518	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3519	    STATE_SENT_CMD | STATE_GOT_STATUS;
3520
3521	*scsipkt->pkt_scbp = STATUS_CHECK;
3522
3523	sense = sata_arq_sense(spx);
3524	sense->es_key = key;
3525	sense->es_add_code = code;
3526
3527	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3528
3529	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3530	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3531
3532	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3533	    scsipkt->pkt_comp != NULL) {
3534		/* scsi callback required */
3535		if (servicing_interrupt()) {
3536			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3537			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3538			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3539			    TASKQID_INVALID) {
3540				return (TRAN_BUSY);
3541			}
3542		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3543		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3544		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
3545			/* Scheduling the callback failed */
3546			return (TRAN_BUSY);
3547		}
3548	}
3549	return (TRAN_ACCEPT);
3550}
3551
3552/*
3553 * Scsi response setup for
3554 * emulated non-data command that requires no action/return data
3555 *
3556 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3557 */
3558static	int
3559sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx)
3560{
3561	int rval;
3562	int reason;
3563	kmutex_t *cport_mutex =  &(SATA_TXLT_CPORT_MUTEX(spx));
3564
3565	mutex_enter(cport_mutex);
3566
3567	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
3568	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3569		mutex_exit(cport_mutex);
3570		return (rval);
3571	}
3572	mutex_exit(cport_mutex);
3573
3574	spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3575	    STATE_SENT_CMD | STATE_GOT_STATUS;
3576	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3577	*(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD;
3578
3579	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3580	    "Scsi_pkt completion reason %x\n",
3581	    spx->txlt_scsi_pkt->pkt_reason);
3582
3583	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 &&
3584	    spx->txlt_scsi_pkt->pkt_comp != NULL) {
3585		/* scsi callback required */
3586		if (servicing_interrupt()) {
3587			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3588			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3589			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3590			    TASKQID_INVALID) {
3591				return (TRAN_BUSY);
3592			}
3593		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3594		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3595		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
3596			/* Scheduling the callback failed */
3597			return (TRAN_BUSY);
3598		}
3599	}
3600	return (TRAN_ACCEPT);
3601}
3602
3603
3604/*
3605 * SATA translate command: Inquiry / Identify Device
3606 * Use cached Identify Device data for now, rather than issuing actual
3607 * Device Identify cmd request. If device is detached and re-attached,
3608 * asynchronous event processing should fetch and refresh Identify Device
3609 * data.
3610 * VPD pages supported now:
3611 * Vital Product Data page
3612 * Unit Serial Number page
3613 * Block Device Characteristics Page
3614 * ATA Information Page
3615 *
3616 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3617 */
3618
3619#define	EVPD			1	/* Extended Vital Product Data flag */
3620#define	CMDDT			2	/* Command Support Data - Obsolete */
3621#define	INQUIRY_SUP_VPD_PAGE	0	/* Supported VPD Pages Page Code */
3622#define	INQUIRY_USN_PAGE	0x80	/* Unit Serial Number Page Code */
3623#define	INQUIRY_BDC_PAGE	0xB1	/* Block Device Characteristics Page */
3624					/* Code */
3625#define	INQUIRY_ATA_INFO_PAGE	0x89	/* ATA Information Page Code */
3626#define	INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */
3627
3628static int
3629sata_txlt_inquiry(sata_pkt_txlate_t *spx)
3630{
3631	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3632	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3633	sata_drive_info_t *sdinfo;
3634	struct scsi_extended_sense *sense;
3635	int count;
3636	uint8_t *p;
3637	int i, j;
3638	uint8_t page_buf[1024]; /* Max length */
3639	int rval, reason;
3640	ushort_t rate;
3641	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
3642
3643	mutex_enter(cport_mutex);
3644
3645	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
3646	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3647		mutex_exit(cport_mutex);
3648		return (rval);
3649	}
3650
3651	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3652	    &spx->txlt_sata_pkt->satapkt_device);
3653
3654	ASSERT(sdinfo != NULL);
3655
3656	scsipkt->pkt_reason = CMD_CMPLT;
3657	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3658	    STATE_SENT_CMD | STATE_GOT_STATUS;
3659
3660	/* Reject not supported request */
3661	if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */
3662		*scsipkt->pkt_scbp = STATUS_CHECK;
3663		sense = sata_arq_sense(spx);
3664		sense->es_key = KEY_ILLEGAL_REQUEST;
3665		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3666		goto done;
3667	}
3668
3669	/* Valid Inquiry request */
3670	*scsipkt->pkt_scbp = STATUS_GOOD;
3671
3672	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3673
3674		/*
3675		 * Because it is fully emulated command storing data
3676		 * programatically in the specified buffer, release
3677		 * preallocated DMA resources before storing data in the buffer,
3678		 * so no unwanted DMA sync would take place.
3679		 */
3680		sata_scsi_dmafree(NULL, scsipkt);
3681
3682		if (!(scsipkt->pkt_cdbp[1] & EVPD)) {
3683			/* Standard Inquiry Data request */
3684			struct scsi_inquiry inq;
3685			unsigned int bufsize;
3686
3687			sata_identdev_to_inquiry(spx->txlt_sata_hba_inst,
3688			    sdinfo, (uint8_t *)&inq);
3689			/* Copy no more than requested */
3690			count = MIN(bp->b_bcount,
3691			    sizeof (struct scsi_inquiry));
3692			bufsize = scsipkt->pkt_cdbp[4];
3693			bufsize |= scsipkt->pkt_cdbp[3] << 8;
3694			count = MIN(count, bufsize);
3695			bcopy(&inq, bp->b_un.b_addr, count);
3696
3697			scsipkt->pkt_state |= STATE_XFERRED_DATA;
3698			scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3699			    bufsize - count : 0;
3700		} else {
3701			/*
3702			 * peripheral_qualifier = 0;
3703			 *
3704			 * We are dealing only with HD and will be
3705			 * dealing with CD/DVD devices soon
3706			 */
3707			uint8_t peripheral_device_type =
3708			    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3709			    DTYPE_DIRECT : DTYPE_RODIRECT;
3710
3711			bzero(page_buf, sizeof (page_buf));
3712
3713			switch ((uint_t)scsipkt->pkt_cdbp[2]) {
3714			case INQUIRY_SUP_VPD_PAGE:
3715				/*
3716				 * Request for supported Vital Product Data
3717				 * pages.
3718				 */
3719				page_buf[0] = peripheral_device_type;
3720				page_buf[1] = INQUIRY_SUP_VPD_PAGE;
3721				page_buf[2] = 0;
3722				page_buf[3] = 4; /* page length */
3723				page_buf[4] = INQUIRY_SUP_VPD_PAGE;
3724				page_buf[5] = INQUIRY_USN_PAGE;
3725				page_buf[6] = INQUIRY_BDC_PAGE;
3726				page_buf[7] = INQUIRY_ATA_INFO_PAGE;
3727				/* Copy no more than requested */
3728				count = MIN(bp->b_bcount, 8);
3729				bcopy(page_buf, bp->b_un.b_addr, count);
3730				break;
3731
3732			case INQUIRY_USN_PAGE:
3733				/*
3734				 * Request for Unit Serial Number page.
3735				 * Set-up the page.
3736				 */
3737				page_buf[0] = peripheral_device_type;
3738				page_buf[1] = INQUIRY_USN_PAGE;
3739				page_buf[2] = 0;
3740				/* remaining page length */
3741				page_buf[3] = SATA_ID_SERIAL_LEN;
3742
3743				/*
3744				 * Copy serial number from Identify Device data
3745				 * words into the inquiry page and swap bytes
3746				 * when necessary.
3747				 */
3748				p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser);
3749#ifdef	_LITTLE_ENDIAN
3750				swab(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3751#else
3752				bcopy(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3753#endif
3754				/*
3755				 * Least significant character of the serial
3756				 * number shall appear as the last byte,
3757				 * according to SBC-3 spec.
3758				 * Count trailing spaces to determine the
3759				 * necessary shift length.
3760				 */
3761				p = &page_buf[SATA_ID_SERIAL_LEN + 4 - 1];
3762				for (j = 0; j < SATA_ID_SERIAL_LEN; j++) {
3763					if (*(p - j) != '\0' &&
3764					    *(p - j) != '\040')
3765						break;
3766				}
3767
3768				/*
3769				 * Shift SN string right, so that the last
3770				 * non-blank character would appear in last
3771				 * byte of SN field in the page.
3772				 * 'j' is the shift length.
3773				 */
3774				for (i = 0;
3775				    i < (SATA_ID_SERIAL_LEN - j) && j != 0;
3776				    i++, p--)
3777					*p = *(p - j);
3778
3779				/*
3780				 * Add leading spaces - same number as the
3781				 * shift size
3782				 */
3783				for (; j > 0; j--)
3784					page_buf[4 + j - 1] = '\040';
3785
3786				count = MIN(bp->b_bcount,
3787				    SATA_ID_SERIAL_LEN + 4);
3788				bcopy(page_buf, bp->b_un.b_addr, count);
3789				break;
3790
3791			case INQUIRY_BDC_PAGE:
3792				/*
3793				 * Request for Block Device Characteristics
3794				 * page.  Set-up the page.
3795				 */
3796				page_buf[0] = peripheral_device_type;
3797				page_buf[1] = INQUIRY_BDC_PAGE;
3798				page_buf[2] = 0;
3799				/* remaining page length */
3800				page_buf[3] = SATA_ID_BDC_LEN;
3801
3802				rate = sdinfo->satadrv_id.ai_medrotrate;
3803				page_buf[4] = (rate >> 8) & 0xff;
3804				page_buf[5] = rate & 0xff;
3805				page_buf[6] = 0;
3806				page_buf[7] = sdinfo->satadrv_id.
3807				    ai_nomformfactor & 0xf;
3808
3809				count = MIN(bp->b_bcount,
3810				    SATA_ID_BDC_LEN + 4);
3811				bcopy(page_buf, bp->b_un.b_addr, count);
3812				break;
3813
3814			case INQUIRY_ATA_INFO_PAGE:
3815				/*
3816				 * Request for ATA Information page.
3817				 */
3818				page_buf[0] = peripheral_device_type;
3819				page_buf[1] = INQUIRY_ATA_INFO_PAGE;
3820				page_buf[2] = (SATA_ID_ATA_INFO_LEN >> 8) &
3821				    0xff;
3822				page_buf[3] = SATA_ID_ATA_INFO_LEN & 0xff;
3823				/* page_buf[4-7] reserved */
3824#ifdef  _LITTLE_ENDIAN
3825				bcopy("ATA     ", &page_buf[8], 8);
3826				swab(sdinfo->satadrv_id.ai_model,
3827				    &page_buf[16], 16);
3828				if (strncmp(&sdinfo->satadrv_id.ai_fw[4],
3829				    "    ", 4) == 0) {
3830					swab(sdinfo->satadrv_id.ai_fw,
3831					    &page_buf[32], 4);
3832				} else {
3833					swab(&sdinfo->satadrv_id.ai_fw[4],
3834					    &page_buf[32], 4);
3835				}
3836#else   /* _LITTLE_ENDIAN */
3837				bcopy("ATA     ", &page_buf[8], 8);
3838				bcopy(sdinfo->satadrv_id.ai_model,
3839				    &page_buf[16], 16);
3840				if (strncmp(&sdinfo->satadrv_id.ai_fw[4],
3841				    "    ", 4) == 0) {
3842					bcopy(sdinfo->satadrv_id.ai_fw,
3843					    &page_buf[32], 4);
3844				} else {
3845					bcopy(&sdinfo->satadrv_id.ai_fw[4],
3846					    &page_buf[32], 4);
3847				}
3848#endif  /* _LITTLE_ENDIAN */
3849				/*
3850				 * page_buf[36-55] which defines the device
3851				 * signature is not defined at this
3852				 * time.
3853				 */
3854
3855				/* Set the command code */
3856				if (sdinfo->satadrv_type ==
3857				    SATA_DTYPE_ATADISK) {
3858					page_buf[56] = SATAC_ID_DEVICE;
3859				} else if (sdinfo->satadrv_type ==
3860				    SATA_DTYPE_ATAPI) {
3861					page_buf[56] = SATAC_ID_PACKET_DEVICE;
3862				}
3863				/*
3864				 * If the command code, page_buf[56], is not
3865				 * zero and if one of the identify commands
3866				 * succeeds, return the identify data.
3867				 */
3868				if ((page_buf[56] != 0) &&
3869				    (sata_fetch_device_identify_data(
3870				    spx->txlt_sata_hba_inst, sdinfo) ==
3871				    SATA_SUCCESS)) {
3872					bcopy(&sdinfo->satadrv_id,
3873					    &page_buf[60], sizeof (sata_id_t));
3874				}
3875
3876				/* Need to copy out the page_buf to bp */
3877				count = MIN(bp->b_bcount,
3878				    SATA_ID_ATA_INFO_LEN + 4);
3879				bcopy(page_buf, bp->b_un.b_addr, count);
3880				break;
3881
3882			case INQUIRY_DEV_IDENTIFICATION_PAGE:
3883				/*
3884				 * We may want to implement this page, when
3885				 * identifiers are common for SATA devices
3886				 * But not now.
3887				 */
3888				/*FALLTHROUGH*/
3889
3890			default:
3891				/* Request for unsupported VPD page */
3892				*scsipkt->pkt_scbp = STATUS_CHECK;
3893				sense = sata_arq_sense(spx);
3894				sense->es_key = KEY_ILLEGAL_REQUEST;
3895				sense->es_add_code =
3896				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3897				goto done;
3898			}
3899		}
3900		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3901		scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3902		    scsipkt->pkt_cdbp[4] - count : 0;
3903	}
3904done:
3905	mutex_exit(cport_mutex);
3906
3907	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3908	    "Scsi_pkt completion reason %x\n",
3909	    scsipkt->pkt_reason);
3910
3911	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3912	    scsipkt->pkt_comp != NULL) {
3913		/* scsi callback required */
3914		if (servicing_interrupt()) {
3915			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3916			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3917			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3918			    TASKQID_INVALID) {
3919				return (TRAN_BUSY);
3920			}
3921		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3922		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3923		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
3924			/* Scheduling the callback failed */
3925			return (TRAN_BUSY);
3926		}
3927	}
3928	return (TRAN_ACCEPT);
3929}
3930
3931/*
3932 * SATA translate command: Request Sense.
3933 *
3934 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3935 * At the moment this is an emulated command (ATA version for SATA hard disks).
3936 * May be translated into Check Power Mode command in the future.
3937 *
3938 * Note: There is a mismatch between already implemented Informational
3939 * Exception Mode Select page 0x1C and this function.
3940 * When MRIE bit is set in page 0x1C, Request Sense is supposed to return
3941 * NO SENSE and set additional sense code to the exception code - this is not
3942 * implemented here.
3943 */
3944static int
3945sata_txlt_request_sense(sata_pkt_txlate_t *spx)
3946{
3947	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3948	struct scsi_extended_sense sense;
3949	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3950	sata_drive_info_t *sdinfo;
3951	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3952	int rval, reason, power_state = 0;
3953	kmutex_t *cport_mutex;
3954
3955	cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
3956	mutex_enter(cport_mutex);
3957
3958	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
3959	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3960		mutex_exit(cport_mutex);
3961		return (rval);
3962	}
3963
3964	scsipkt->pkt_reason = CMD_CMPLT;
3965	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3966	    STATE_SENT_CMD | STATE_GOT_STATUS;
3967	*scsipkt->pkt_scbp = STATUS_GOOD;
3968
3969	/*
3970	 * when CONTROL field's NACA bit == 1
3971	 * return ILLEGAL_REQUEST
3972	 */
3973	if (scsipkt->pkt_cdbp[5] & CTL_BYTE_NACA_MASK) {
3974		mutex_exit(cport_mutex);
3975		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
3976		    SD_SCSI_ASC_CMD_SEQUENCE_ERR));
3977	}
3978
3979	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3980	    &spx->txlt_sata_pkt->satapkt_device);
3981	ASSERT(sdinfo != NULL);
3982
3983	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
3984
3985	sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
3986	scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
3987	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3988	if (sata_hba_start(spx, &rval) != 0) {
3989		mutex_exit(cport_mutex);
3990		return (rval);
3991	}
3992	if (scmd->satacmd_error_reg != 0) {
3993		mutex_exit(cport_mutex);
3994		return (sata_txlt_check_condition(spx, KEY_NO_SENSE,
3995		    SD_SCSI_ASC_NO_ADD_SENSE));
3996	}
3997
3998	switch (scmd->satacmd_sec_count_lsb) {
3999	case SATA_PWRMODE_STANDBY: /* device in standby mode */
4000		if (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)
4001			power_state = SATA_POWER_STOPPED;
4002		else {
4003			power_state = SATA_POWER_STANDBY;
4004			sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
4005		}
4006		break;
4007	case SATA_PWRMODE_IDLE: /* device in idle mode */
4008		power_state = SATA_POWER_IDLE;
4009		sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4010		break;
4011	case SATA_PWRMODE_ACTIVE: /* device in active or idle mode */
4012	default:		  /* 0x40, 0x41 active mode */
4013		if (sdinfo->satadrv_power_level == SATA_POWER_IDLE)
4014			power_state = SATA_POWER_IDLE;
4015		else {
4016			power_state = SATA_POWER_ACTIVE;
4017			sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4018		}
4019		break;
4020	}
4021
4022	mutex_exit(cport_mutex);
4023
4024	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4025		/*
4026		 * Because it is fully emulated command storing data
4027		 * programatically in the specified buffer, release
4028		 * preallocated DMA resources before storing data in the buffer,
4029		 * so no unwanted DMA sync would take place.
4030		 */
4031		int count = MIN(bp->b_bcount,
4032		    sizeof (struct scsi_extended_sense));
4033		sata_scsi_dmafree(NULL, scsipkt);
4034		bzero(&sense, sizeof (struct scsi_extended_sense));
4035		sense.es_valid = 0;	/* Valid LBA */
4036		sense.es_class = 7;	/* Response code 0x70 - current err */
4037		sense.es_key = KEY_NO_SENSE;
4038		sense.es_add_len = 6;	/* Additional length */
4039		/* Copy no more than requested */
4040		bcopy(&sense, bp->b_un.b_addr, count);
4041		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4042		scsipkt->pkt_resid = 0;
4043		switch (power_state) {
4044		case SATA_POWER_IDLE:
4045		case SATA_POWER_STANDBY:
4046			sense.es_add_code =
4047			    SD_SCSI_ASC_LOW_POWER_CONDITION_ON;
4048			break;
4049		case SATA_POWER_STOPPED:
4050			sense.es_add_code = SD_SCSI_ASC_NO_ADD_SENSE;
4051			break;
4052		case SATA_POWER_ACTIVE:
4053		default:
4054			break;
4055		}
4056	}
4057
4058	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4059	    "Scsi_pkt completion reason %x\n",
4060	    scsipkt->pkt_reason);
4061
4062	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4063	    scsipkt->pkt_comp != NULL) {
4064		/* scsi callback required */
4065		if (servicing_interrupt()) {
4066			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4067			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4068			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
4069			    TASKQID_INVALID) {
4070				return (TRAN_BUSY);
4071			}
4072		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4073		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4074		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
4075			/* Scheduling the callback failed */
4076			return (TRAN_BUSY);
4077		}
4078	}
4079	return (TRAN_ACCEPT);
4080}
4081
4082/*
4083 * SATA translate command: Test Unit Ready
4084 * (ATA version for SATA hard disks).
4085 * It is translated into the Check Power Mode command.
4086 *
4087 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4088 */
4089static int
4090sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx)
4091{
4092	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4093	struct scsi_extended_sense *sense;
4094	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4095	sata_drive_info_t *sdinfo;
4096	int power_state;
4097	int rval, reason;
4098	kmutex_t *cport_mutex =  &(SATA_TXLT_CPORT_MUTEX(spx));
4099
4100	mutex_enter(cport_mutex);
4101
4102	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
4103	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4104		mutex_exit(cport_mutex);
4105		return (rval);
4106	}
4107
4108	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4109	    &spx->txlt_sata_pkt->satapkt_device);
4110	ASSERT(sdinfo != NULL);
4111
4112	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
4113
4114	/* send CHECK POWER MODE command */
4115	sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
4116	scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
4117	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4118	if (sata_hba_start(spx, &rval) != 0) {
4119		mutex_exit(cport_mutex);
4120		return (rval);
4121	}
4122
4123	if (scmd->satacmd_error_reg != 0) {
4124		mutex_exit(cport_mutex);
4125		return (sata_txlt_check_condition(spx, KEY_NOT_READY,
4126		    SD_SCSI_ASC_LU_NOT_RESPONSE));
4127	}
4128
4129	power_state = scmd->satacmd_sec_count_lsb;
4130
4131	/*
4132	 * return NOT READY when device in STOPPED mode
4133	 */
4134	if (power_state == SATA_PWRMODE_STANDBY &&
4135	    sdinfo->satadrv_power_level == SATA_POWER_STOPPED) {
4136		*scsipkt->pkt_scbp = STATUS_CHECK;
4137		sense = sata_arq_sense(spx);
4138		sense->es_key = KEY_NOT_READY;
4139		sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY;
4140	} else {
4141		/*
4142		 * For other power mode, return GOOD status
4143		 */
4144		*scsipkt->pkt_scbp = STATUS_GOOD;
4145	}
4146
4147	scsipkt->pkt_reason = CMD_CMPLT;
4148	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4149	    STATE_SENT_CMD | STATE_GOT_STATUS;
4150
4151	mutex_exit(cport_mutex);
4152
4153	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4154	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4155
4156	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4157	    scsipkt->pkt_comp != NULL) {
4158		/* scsi callback required */
4159		if (servicing_interrupt()) {
4160			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4161			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4162			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
4163			    TASKQID_INVALID) {
4164				return (TRAN_BUSY);
4165			}
4166		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4167		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4168		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
4169			/* Scheduling the callback failed */
4170			return (TRAN_BUSY);
4171		}
4172	}
4173
4174	return (TRAN_ACCEPT);
4175}
4176
4177/*
4178 * SATA translate command: Start Stop Unit
4179 * Translation depends on a command:
4180 *
4181 * Power condition bits will be supported
4182 * and the power level should be maintained by SATL,
4183 * When SATL received a command, it will check the
4184 * power level firstly, and return the status according
4185 * to SAT2 v2.6 and SAT-2 Standby Modifications
4186 *
4187 * SPC-4/SBC-3      SATL    ATA power condition  SATL      SPC/SBC
4188 * -----------------------------------------------------------------------
4189 * SSU_PC1 Active   <==>     ATA  Active         <==>     SSU:start_bit =1
4190 * SSU_PC2 Idle     <==>     ATA  Idle           <==>     N/A
4191 * SSU_PC3 Standby  <==>     ATA  Standby        <==>     N/A
4192 * SSU_PC4 Stopped  <==>     ATA  Standby        <==>     SSU:start_bit = 0
4193 *
4194 *	Unload Media / NOT SUPPORTED YET
4195 *	Load Media / NOT SUPPROTED YET
4196 *	Immediate bit / NOT SUPPORTED YET (deferred error)
4197 *
4198 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4199 * appropriate values in scsi_pkt fields.
4200 */
4201static int
4202sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx)
4203{
4204	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4205	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4206	int rval, reason;
4207	sata_drive_info_t *sdinfo;
4208	sata_id_t *sata_id;
4209	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4210
4211	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4212	    "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1);
4213
4214	mutex_enter(cport_mutex);
4215
4216	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
4217	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4218		mutex_exit(cport_mutex);
4219		return (rval);
4220	}
4221
4222	if (scsipkt->pkt_cdbp[1] & START_STOP_IMMED_MASK) {
4223		/* IMMED bit - not supported */
4224		mutex_exit(cport_mutex);
4225		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4226		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4227	}
4228
4229	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
4230	spx->txlt_sata_pkt->satapkt_comp = NULL;
4231
4232	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4233	    &spx->txlt_sata_pkt->satapkt_device);
4234	ASSERT(sdinfo != NULL);
4235	sata_id = &sdinfo->satadrv_id;
4236
4237	switch ((scsipkt->pkt_cdbp[4] & START_STOP_POWER_COND_MASK) >> 4) {
4238	case 0:
4239		if (scsipkt->pkt_cdbp[4] & START_STOP_LOEJ_MASK) {
4240			/* Load/Unload Media - invalid request */
4241			goto err_out;
4242		}
4243		if (scsipkt->pkt_cdbp[4] & START_STOP_START_MASK) {
4244			/* Start Unit */
4245			sata_build_read_verify_cmd(scmd, 1, 5);
4246			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4247			/* Transfer command to HBA */
4248			if (sata_hba_start(spx, &rval) != 0) {
4249				/* Pkt not accepted for execution */
4250				mutex_exit(cport_mutex);
4251				return (rval);
4252			}
4253			if (scmd->satacmd_error_reg != 0) {
4254				goto err_out;
4255			}
4256			sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4257		} else {
4258			/* Stop Unit */
4259			sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4260			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4261			if (sata_hba_start(spx, &rval) != 0) {
4262				mutex_exit(cport_mutex);
4263				return (rval);
4264			} else {
4265				if (scmd->satacmd_error_reg != 0) {
4266					goto err_out;
4267				}
4268			}
4269			/* ata standby immediate command */
4270			sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
4271			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4272			if (sata_hba_start(spx, &rval) != 0) {
4273				mutex_exit(cport_mutex);
4274				return (rval);
4275			}
4276			if (scmd->satacmd_error_reg != 0) {
4277				goto err_out;
4278			}
4279			sdinfo->satadrv_power_level = SATA_POWER_STOPPED;
4280		}
4281		break;
4282	case 0x1:
4283		sata_build_generic_cmd(scmd, SATAC_IDLE);
4284		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4285		if (sata_hba_start(spx, &rval) != 0) {
4286			mutex_exit(cport_mutex);
4287			return (rval);
4288		}
4289		if (scmd->satacmd_error_reg != 0) {
4290			goto err_out;
4291		}
4292		sata_build_read_verify_cmd(scmd, 1, 5);
4293		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4294		/* Transfer command to HBA */
4295		if (sata_hba_start(spx, &rval) != 0) {
4296			/* Pkt not accepted for execution */
4297			mutex_exit(cport_mutex);
4298			return (rval);
4299		} else {
4300			if (scmd->satacmd_error_reg != 0) {
4301				goto err_out;
4302			}
4303		}
4304		sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4305		break;
4306	case 0x2:
4307		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4308		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4309		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4310			if (sata_hba_start(spx, &rval) != 0) {
4311				mutex_exit(cport_mutex);
4312				return (rval);
4313			}
4314			if (scmd->satacmd_error_reg != 0) {
4315				goto err_out;
4316			}
4317		}
4318		sata_build_generic_cmd(scmd, SATAC_IDLE);
4319		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4320		if (sata_hba_start(spx, &rval) != 0) {
4321			mutex_exit(cport_mutex);
4322			return (rval);
4323		}
4324		if (scmd->satacmd_error_reg != 0) {
4325			goto err_out;
4326		}
4327		if ((scsipkt->pkt_cdbp[3] & START_STOP_MODIFIER_MASK)) {
4328			/*
4329			 *  POWER CONDITION MODIFIER bit set
4330			 *  to 0x1 or larger it will be handled
4331			 *  on the same way as bit = 0x1
4332			 */
4333			if (!(sata_id->ai_cmdset84 &
4334			    SATA_IDLE_UNLOAD_SUPPORTED)) {
4335				sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4336				break;
4337			}
4338			sata_build_generic_cmd(scmd, SATAC_IDLE_IM);
4339			scmd->satacmd_features_reg = 0x44;
4340			scmd->satacmd_lba_low_lsb = 0x4c;
4341			scmd->satacmd_lba_mid_lsb = 0x4e;
4342			scmd->satacmd_lba_high_lsb = 0x55;
4343			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4344			if (sata_hba_start(spx, &rval) != 0) {
4345				mutex_exit(cport_mutex);
4346				return (rval);
4347			}
4348			if (scmd->satacmd_error_reg != 0) {
4349				goto err_out;
4350			}
4351		}
4352		sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4353		break;
4354	case 0x3:
4355		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4356		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4357		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4358			if (sata_hba_start(spx, &rval) != 0) {
4359				mutex_exit(cport_mutex);
4360				return (rval);
4361			}
4362			if (scmd->satacmd_error_reg != 0) {
4363				goto err_out;
4364			}
4365		}
4366		sata_build_generic_cmd(scmd, SATAC_STANDBY);
4367		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4368		if (sata_hba_start(spx, &rval) != 0) {
4369			mutex_exit(cport_mutex);
4370			return (rval);
4371		}
4372		if (scmd->satacmd_error_reg != 0) {
4373			goto err_out;
4374		}
4375		sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
4376		break;
4377	case 0x7:
4378		sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
4379		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
4380		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4381		if (sata_hba_start(spx, &rval) != 0) {
4382			mutex_exit(cport_mutex);
4383			return (rval);
4384		}
4385		if (scmd->satacmd_error_reg != 0) {
4386			goto err_out;
4387		}
4388		switch (scmd->satacmd_sec_count_lsb) {
4389		case SATA_PWRMODE_STANDBY:
4390			sata_build_generic_cmd(scmd, SATAC_STANDBY);
4391			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4392			    sdinfo->satadrv_standby_timer);
4393			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4394			if (sata_hba_start(spx, &rval) != 0) {
4395				mutex_exit(cport_mutex);
4396				return (rval);
4397			} else {
4398				if (scmd->satacmd_error_reg != 0) {
4399					goto err_out;
4400				}
4401			}
4402			break;
4403		case SATA_PWRMODE_IDLE:
4404			sata_build_generic_cmd(scmd, SATAC_IDLE);
4405			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4406			    sdinfo->satadrv_standby_timer);
4407			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4408			if (sata_hba_start(spx, &rval) != 0) {
4409				mutex_exit(cport_mutex);
4410				return (rval);
4411			} else {
4412				if (scmd->satacmd_error_reg != 0) {
4413					goto err_out;
4414				}
4415			}
4416			break;
4417		case SATA_PWRMODE_ACTIVE_SPINDOWN:
4418		case SATA_PWRMODE_ACTIVE_SPINUP:
4419		case SATA_PWRMODE_ACTIVE:
4420			sata_build_generic_cmd(scmd, SATAC_IDLE);
4421			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4422			    sdinfo->satadrv_standby_timer);
4423			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4424			if (sata_hba_start(spx, &rval) != 0) {
4425				mutex_exit(cport_mutex);
4426				return (rval);
4427			}
4428			if (scmd->satacmd_error_reg != 0) {
4429				goto err_out;
4430			}
4431			sata_build_read_verify_cmd(scmd, 1, 5);
4432			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4433			if (sata_hba_start(spx, &rval) != 0) {
4434				mutex_exit(cport_mutex);
4435				return (rval);
4436			}
4437			if (scmd->satacmd_error_reg != 0) {
4438				goto err_out;
4439			}
4440			break;
4441		default:
4442			goto err_out;
4443		}
4444		break;
4445	case 0xb:
4446		if ((sata_get_standby_timer(sdinfo->satadrv_standby_timer) ==
4447		    0) || (!(sata_id->ai_cap & SATA_STANDBYTIMER))) {
4448			mutex_exit(cport_mutex);
4449			return (sata_txlt_check_condition(spx,
4450			    KEY_ILLEGAL_REQUEST,
4451			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4452		}
4453		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4454		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4455		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4456			if (sata_hba_start(spx, &rval) != 0) {
4457				mutex_exit(cport_mutex);
4458				return (rval);
4459			}
4460			if (scmd->satacmd_error_reg != 0) {
4461				goto err_out;
4462			}
4463			sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
4464			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4465			if (sata_hba_start(spx, &rval) != 0) {
4466				mutex_exit(cport_mutex);
4467				return (rval);
4468			}
4469			if (scmd->satacmd_error_reg != 0) {
4470				goto err_out;
4471			}
4472		}
4473		bzero(sdinfo->satadrv_standby_timer, sizeof (uchar_t) * 4);
4474		break;
4475	default:
4476err_out:
4477		mutex_exit(cport_mutex);
4478		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4479		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4480	}
4481
4482	/*
4483	 * Since it was a synchronous command,
4484	 * a callback function will be called directly.
4485	 */
4486	mutex_exit(cport_mutex);
4487	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4488	    "synchronous execution status %x\n",
4489	    spx->txlt_sata_pkt->satapkt_reason);
4490
4491	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4492	    scsipkt->pkt_comp != NULL) {
4493		sata_set_arq_data(spx->txlt_sata_pkt);
4494		if (servicing_interrupt()) {
4495			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4496			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4497			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
4498			    TASKQID_INVALID) {
4499				return (TRAN_BUSY);
4500			}
4501		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4502		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4503		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
4504			/* Scheduling the callback failed */
4505			return (TRAN_BUSY);
4506		}
4507	}
4508	else
4509
4510		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
4511
4512	return (TRAN_ACCEPT);
4513
4514}
4515
4516/*
4517 * SATA translate command:  Read Capacity.
4518 * Emulated command for SATA disks.
4519 * Capacity is retrieved from cached Idenifty Device data.
4520 * Identify Device data shows effective disk capacity, not the native
4521 * capacity, which may be limitted by Set Max Address command.
4522 * This is ATA version for SATA hard disks.
4523 *
4524 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4525 */
4526static int
4527sata_txlt_read_capacity(sata_pkt_txlate_t *spx)
4528{
4529	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4530	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4531	sata_drive_info_t *sdinfo;
4532	uint64_t val;
4533	uint32_t lbsize = DEV_BSIZE;
4534	uchar_t *rbuf;
4535	int rval, reason;
4536	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4537
4538	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4539	    "sata_txlt_read_capacity: ", NULL);
4540
4541	mutex_enter(cport_mutex);
4542
4543	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4544	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4545		mutex_exit(cport_mutex);
4546		return (rval);
4547	}
4548
4549	scsipkt->pkt_reason = CMD_CMPLT;
4550	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4551	    STATE_SENT_CMD | STATE_GOT_STATUS;
4552	*scsipkt->pkt_scbp = STATUS_GOOD;
4553	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4554		/*
4555		 * Because it is fully emulated command storing data
4556		 * programatically in the specified buffer, release
4557		 * preallocated DMA resources before storing data in the buffer,
4558		 * so no unwanted DMA sync would take place.
4559		 */
4560		sata_scsi_dmafree(NULL, scsipkt);
4561
4562		sdinfo = sata_get_device_info(
4563		    spx->txlt_sata_hba_inst,
4564		    &spx->txlt_sata_pkt->satapkt_device);
4565
4566		/*
4567		 * As per SBC-3, the "returned LBA" is either the highest
4568		 * addressable LBA or 0xffffffff, whichever is smaller.
4569		 */
4570		val = MIN(sdinfo->satadrv_capacity - 1, UINT32_MAX);
4571
4572		if (sdinfo->satadrv_id.ai_phys_sect_sz & SATA_L2PS_CHECK_BIT) {
4573			/* physical/logical sector size word is valid */
4574
4575			if (sdinfo->satadrv_id.ai_phys_sect_sz &
4576			    SATA_L2PS_BIG_SECTORS) {
4577				/* if this set 117-118 words are valid */
4578				lbsize = sdinfo->satadrv_id.ai_words_lsec[0] |
4579				    (sdinfo->satadrv_id.ai_words_lsec[1] << 16);
4580				lbsize <<= 1; /* convert from words to bytes */
4581			}
4582		}
4583		rbuf = (uchar_t *)bp->b_un.b_addr;
4584		/* Need to swap endians to match scsi format */
4585		rbuf[0] = (val >> 24) & 0xff;
4586		rbuf[1] = (val >> 16) & 0xff;
4587		rbuf[2] = (val >> 8) & 0xff;
4588		rbuf[3] = val & 0xff;
4589		rbuf[4] = (lbsize >> 24) & 0xff;
4590		rbuf[5] = (lbsize >> 16) & 0xff;
4591		rbuf[6] = (lbsize >> 8) & 0xff;
4592		rbuf[7] = lbsize & 0xff;
4593
4594		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4595		scsipkt->pkt_resid = 0;
4596
4597		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n",
4598		    sdinfo->satadrv_capacity -1);
4599	}
4600	mutex_exit(cport_mutex);
4601	/*
4602	 * If a callback was requested, do it now.
4603	 */
4604	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4605	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4606
4607	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4608	    scsipkt->pkt_comp != NULL) {
4609		/* scsi callback required */
4610		if (servicing_interrupt()) {
4611			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4612			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4613			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
4614			    TASKQID_INVALID) {
4615				return (TRAN_BUSY);
4616			}
4617		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4618		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4619		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
4620			/* Scheduling the callback failed */
4621			return (TRAN_BUSY);
4622		}
4623	}
4624
4625	return (TRAN_ACCEPT);
4626}
4627
4628/*
4629 * SATA translate command:  Read Capacity (16).
4630 * Emulated command for SATA disks.
4631 * Info is retrieved from cached Identify Device data.
4632 * Implemented to SBC-3 (draft 21) and SAT-2 (final) specifications.
4633 *
4634 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4635 */
4636static int
4637sata_txlt_read_capacity16(sata_pkt_txlate_t *spx)
4638{
4639	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4640	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4641	sata_drive_info_t *sdinfo;
4642	uint64_t val;
4643	uint16_t l2p_exp;
4644	uint32_t lbsize = DEV_BSIZE;
4645	uchar_t *rbuf;
4646	int rval, reason;
4647#define	TPE	0x80
4648#define	TPRZ	0x40
4649	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4650
4651	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4652	    "sata_txlt_read_capacity: ", NULL);
4653
4654	mutex_enter(cport_mutex);
4655
4656	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4657	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4658		mutex_exit(cport_mutex);
4659		return (rval);
4660	}
4661
4662	scsipkt->pkt_reason = CMD_CMPLT;
4663	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4664	    STATE_SENT_CMD | STATE_GOT_STATUS;
4665	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4666		/*
4667		 * Because it is fully emulated command storing data
4668		 * programatically in the specified buffer, release
4669		 * preallocated DMA resources before storing data in the buffer,
4670		 * so no unwanted DMA sync would take place.
4671		 */
4672		sata_scsi_dmafree(NULL, scsipkt);
4673
4674		/* Check SERVICE ACTION field */
4675		if ((scsipkt->pkt_cdbp[1] & 0x1f) !=
4676		    SSVC_ACTION_READ_CAPACITY_G4) {
4677			mutex_exit(cport_mutex);
4678			return (sata_txlt_check_condition(spx,
4679			    KEY_ILLEGAL_REQUEST,
4680			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4681		}
4682
4683		/* Check LBA field */
4684		if ((scsipkt->pkt_cdbp[2] != 0) ||
4685		    (scsipkt->pkt_cdbp[3] != 0) ||
4686		    (scsipkt->pkt_cdbp[4] != 0) ||
4687		    (scsipkt->pkt_cdbp[5] != 0) ||
4688		    (scsipkt->pkt_cdbp[6] != 0) ||
4689		    (scsipkt->pkt_cdbp[7] != 0) ||
4690		    (scsipkt->pkt_cdbp[8] != 0) ||
4691		    (scsipkt->pkt_cdbp[9] != 0)) {
4692			mutex_exit(cport_mutex);
4693			return (sata_txlt_check_condition(spx,
4694			    KEY_ILLEGAL_REQUEST,
4695			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4696		}
4697
4698		/* Check PMI bit */
4699		if (scsipkt->pkt_cdbp[14] & 0x1) {
4700			mutex_exit(cport_mutex);
4701			return (sata_txlt_check_condition(spx,
4702			    KEY_ILLEGAL_REQUEST,
4703			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4704		}
4705
4706		*scsipkt->pkt_scbp = STATUS_GOOD;
4707
4708		sdinfo = sata_get_device_info(
4709		    spx->txlt_sata_hba_inst,
4710		    &spx->txlt_sata_pkt->satapkt_device);
4711
4712		/* last logical block address */
4713		val = MIN(sdinfo->satadrv_capacity - 1,
4714		    SCSI_READ_CAPACITY16_MAX_LBA);
4715
4716		/* logical to physical block size exponent */
4717		l2p_exp = 0;
4718		if (sdinfo->satadrv_id.ai_phys_sect_sz & SATA_L2PS_CHECK_BIT) {
4719			/* physical/logical sector size word is valid */
4720
4721			if (sdinfo->satadrv_id.ai_phys_sect_sz &
4722			    SATA_L2PS_HAS_MULT) {
4723				/* multiple logical sectors per phys sectors */
4724				l2p_exp =
4725				    sdinfo->satadrv_id.ai_phys_sect_sz &
4726				    SATA_L2PS_EXP_MASK;
4727			}
4728
4729			if (sdinfo->satadrv_id.ai_phys_sect_sz &
4730			    SATA_L2PS_BIG_SECTORS) {
4731				/* if this set 117-118 words are valid */
4732				lbsize = sdinfo->satadrv_id.ai_words_lsec[0] |
4733				    (sdinfo->satadrv_id.ai_words_lsec[1] << 16);
4734				lbsize <<= 1; /* convert from words to bytes */
4735			}
4736		}
4737
4738		rbuf = (uchar_t *)bp->b_un.b_addr;
4739		bzero(rbuf, bp->b_bcount);
4740
4741		/* returned logical block address */
4742		rbuf[0] = (val >> 56) & 0xff;
4743		rbuf[1] = (val >> 48) & 0xff;
4744		rbuf[2] = (val >> 40) & 0xff;
4745		rbuf[3] = (val >> 32) & 0xff;
4746		rbuf[4] = (val >> 24) & 0xff;
4747		rbuf[5] = (val >> 16) & 0xff;
4748		rbuf[6] = (val >> 8) & 0xff;
4749		rbuf[7] = val & 0xff;
4750		rbuf[8] = (lbsize >> 24) & 0xff;
4751		rbuf[9] = (lbsize >> 16) & 0xff;
4752		rbuf[10] = (lbsize >> 8) & 0xff;
4753		rbuf[11] = lbsize & 0xff;
4754
4755		/* p_type, prot_en, unspecified by SAT-2 */
4756		/* rbuf[12] = 0; */
4757
4758		/* p_i_exponent, undefined by SAT-2 */
4759		/* logical blocks per physical block exponent */
4760		rbuf[13] = l2p_exp;
4761
4762		/*
4763		 * tpe and tprz as defined in T10/10-079 r0.
4764		 * TRIM support is indicated by the relevant bit in the data
4765		 * set management word. Read-after-trim behavior is indicated
4766		 * by the additional bits in the identify device word. Of the
4767		 * three defined possibilities, we only flag read-zero.
4768		 */
4769		if (sdinfo->satadrv_id.ai_dsm & SATA_DSM_TRIM) {
4770			rbuf[14] |= TPE;
4771
4772			if ((sdinfo->satadrv_id.ai_addsupported &
4773			    SATA_DETERMINISTIC_READ) &&
4774			    (sdinfo->satadrv_id.ai_addsupported &
4775			    SATA_READ_ZERO)) {
4776				rbuf[14] |= TPRZ;
4777			}
4778		}
4779
4780		/* lowest aligned logical block address = 0 (for now) */
4781		/* rbuf[15] = 0; */
4782
4783		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4784		scsipkt->pkt_resid = 0;
4785
4786		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%llu\n",
4787		    sdinfo->satadrv_capacity -1);
4788	}
4789
4790	mutex_exit(cport_mutex);
4791
4792	/*
4793	 * If a callback was requested, do it now.
4794	 */
4795	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4796	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4797
4798	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4799	    scsipkt->pkt_comp != NULL) {
4800		/* scsi callback required */
4801		if (servicing_interrupt()) {
4802			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4803			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4804			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
4805			    TASKQID_INVALID) {
4806				return (TRAN_BUSY);
4807			}
4808		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4809		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4810		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
4811			/* Scheduling the callback failed */
4812			return (TRAN_BUSY);
4813		}
4814	}
4815
4816	return (TRAN_ACCEPT);
4817}
4818
4819/*
4820 * Translate command: UNMAP
4821 *
4822 * The function cannot be called in interrupt context since it may sleep.
4823 */
4824static int
4825sata_txlt_unmap(sata_pkt_txlate_t *spx)
4826{
4827	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4828	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4829	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4830	uint16_t count = 0;
4831	int synch;
4832	int rval, reason;
4833	int i, x;
4834	int bdlen = 0;
4835	int ranges = 0;
4836	int paramlen = 8;
4837	uint8_t *data, *tmpbd;
4838	sata_drive_info_t *sdinfo;
4839	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4840#define	TRIM	0x1
4841
4842	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4843	    "sata_txlt_unmap: ", NULL);
4844
4845	mutex_enter(cport_mutex);
4846
4847	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4848	    &spx->txlt_sata_pkt->satapkt_device);
4849	if (sdinfo != NULL) {
4850		SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4851		    "DSM support 0x%x, max number of 512 byte blocks of LBA "
4852		    "range entries 0x%x\n", sdinfo->satadrv_id.ai_dsm,
4853		    sdinfo->satadrv_id.ai_maxcount);
4854	}
4855
4856	rval = sata_txlt_generic_pkt_info(spx, &reason, 1);
4857	if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4858		mutex_exit(cport_mutex);
4859		return (rval);
4860	}
4861
4862	/*
4863	 * Need to modify bp to have TRIM data instead of UNMAP data.
4864	 * Start by getting the block descriptor data length by subtracting
4865	 * the 8 byte parameter list header from the parameter list length.
4866	 * The block descriptor size has to be a multiple of 16 bytes.
4867	 */
4868	bdlen = scsipkt->pkt_cdbp[7];
4869	bdlen = (bdlen << 8) + scsipkt->pkt_cdbp[8] - paramlen;
4870	if ((bdlen < 0) || ((bdlen % 16) != 0) ||
4871	    ((bp != NULL) && (bdlen > (bp->b_bcount - paramlen)))) {
4872		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4873		    "sata_txlt_unmap: invalid block descriptor length", NULL);
4874		mutex_exit(cport_mutex);
4875		return ((sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4876		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB)));
4877	}
4878	/*
4879	 * If there are no parameter data or block descriptors, it is not
4880	 * considered an error so just complete the command without sending
4881	 * TRIM.
4882	 */
4883	if ((bdlen == 0) || (bp == NULL) || (bp->b_un.b_addr == NULL) ||
4884	    (bp->b_bcount == 0)) {
4885		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4886		    "sata_txlt_unmap: no parameter data or block descriptors",
4887		    NULL);
4888		mutex_exit(cport_mutex);
4889		return (sata_txlt_unmap_nodata_cmd(spx));
4890	}
4891	tmpbd = (uint8_t *)bp->b_un.b_addr + paramlen;
4892	data = kmem_zalloc(bdlen, KM_SLEEP);
4893
4894	/*
4895	 * Loop through all the UNMAP block descriptors and convert the data
4896	 * into TRIM format.
4897	 */
4898	for (i = 0, x = 0; i < bdlen; i += 16, x += 8) {
4899		/* get range length */
4900		data[x] = tmpbd[i+7];
4901		data[x+1] = tmpbd[i+6];
4902		/* get LBA */
4903		data[x+2] = tmpbd[i+5];
4904		data[x+3] = tmpbd[i+4];
4905		data[x+4] = tmpbd[i+3];
4906		data[x+5] = tmpbd[i+2];
4907		data[x+6] = tmpbd[i+11];
4908		data[x+7] = tmpbd[i+10];
4909
4910		ranges++;
4911	}
4912
4913	/*
4914	 * The TRIM command expects the data buffer to be a multiple of
4915	 * 512-byte blocks of range entries.  This means that the UNMAP buffer
4916	 * may be too small.  Free the original DMA resources and create a
4917	 * local buffer.
4918	 */
4919	sata_common_free_dma_rsrcs(spx);
4920
4921	/*
4922	 * Get count of 512-byte blocks of range entries.  The length
4923	 * of a range entry is 8 bytes which means one count has 64 range
4924	 * entries.
4925	 */
4926	count = (ranges + 63)/64;
4927
4928	/* Allocate a buffer that is a multiple of 512 bytes. */
4929	mutex_exit(cport_mutex);
4930	bp = sata_alloc_local_buffer(spx, count * 512);
4931	if (bp == NULL) {
4932		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
4933		    "sata_txlt_unmap: "
4934		    "cannot allocate buffer for TRIM command", NULL);
4935		kmem_free(data, bdlen);
4936		return (TRAN_BUSY);
4937	}
4938	bp_mapin(bp); /* make data buffer accessible */
4939	mutex_enter(cport_mutex);
4940
4941	bzero(bp->b_un.b_addr, bp->b_bcount);
4942	bcopy(data, bp->b_un.b_addr, x);
4943	kmem_free(data, bdlen);
4944	rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
4945	    DDI_DMA_SYNC_FORDEV);
4946	ASSERT(rval == DDI_SUCCESS);
4947
4948	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4949	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4950	scmd->satacmd_cmd_reg = SATAC_DSM;
4951	scmd->satacmd_sec_count_msb = (count >> 8) & 0xff;
4952	scmd->satacmd_sec_count_lsb = count & 0xff;
4953	scmd->satacmd_features_reg = TRIM;
4954	scmd->satacmd_device_reg = SATA_ADH_LBA;
4955	scmd->satacmd_status_reg = 0;
4956	scmd->satacmd_error_reg = 0;
4957
4958	/* Start processing command */
4959	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4960		spx->txlt_sata_pkt->satapkt_comp =
4961		    sata_txlt_unmap_completion;
4962		synch = FALSE;
4963	} else {
4964		synch = TRUE;
4965	}
4966
4967	if (sata_hba_start(spx, &rval) != 0) {
4968		mutex_exit(cport_mutex);
4969		return (rval);
4970	}
4971
4972	mutex_exit(cport_mutex);
4973
4974	if (synch) {
4975		sata_txlt_unmap_completion(spx->txlt_sata_pkt);
4976	}
4977
4978	return (TRAN_ACCEPT);
4979}
4980
4981/*
4982 * SATA translate command: Mode Sense.
4983 * Translated into appropriate SATA command or emulated.
4984 * Saved Values Page Control (03) are not supported.
4985 *
4986 * NOTE: only caching mode sense page is currently implemented.
4987 *
4988 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4989 */
4990
4991#define	LLBAA	0x10	/* Long LBA Accepted */
4992
4993static int
4994sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
4995{
4996	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4997	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4998	sata_drive_info_t *sdinfo;
4999	sata_id_t *sata_id;
5000	struct scsi_extended_sense *sense;
5001	int		len, bdlen, count, alc_len;
5002	int		pc;	/* Page Control code */
5003	uint8_t		*buf;	/* mode sense buffer */
5004	int		rval, reason;
5005	kmutex_t	*cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5006
5007	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5008	    "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
5009	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5010	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5011
5012	if (servicing_interrupt()) {
5013		buf = kmem_zalloc(1024, KM_NOSLEEP);
5014		if (buf == NULL) {
5015			return (TRAN_BUSY);
5016		}
5017	} else {
5018		buf = kmem_zalloc(1024, KM_SLEEP);
5019	}
5020
5021	mutex_enter(cport_mutex);
5022
5023	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
5024	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5025		mutex_exit(cport_mutex);
5026		kmem_free(buf, 1024);
5027		return (rval);
5028	}
5029
5030	scsipkt->pkt_reason = CMD_CMPLT;
5031	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5032	    STATE_SENT_CMD | STATE_GOT_STATUS;
5033
5034	pc = scsipkt->pkt_cdbp[2] >> 6;
5035
5036	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
5037		/*
5038		 * Because it is fully emulated command storing data
5039		 * programatically in the specified buffer, release
5040		 * preallocated DMA resources before storing data in the buffer,
5041		 * so no unwanted DMA sync would take place.
5042		 */
5043		sata_scsi_dmafree(NULL, scsipkt);
5044
5045		len = 0;
5046		bdlen = 0;
5047		if (!(scsipkt->pkt_cdbp[1] & 8)) {
5048			if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
5049			    (scsipkt->pkt_cdbp[1] & LLBAA))
5050				bdlen = 16;
5051			else
5052				bdlen = 8;
5053		}
5054		/* Build mode parameter header */
5055		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5056			/* 4-byte mode parameter header */
5057			buf[len++] = 0;		/* mode data length */
5058			buf[len++] = 0;		/* medium type */
5059			buf[len++] = 0;		/* dev-specific param */
5060			buf[len++] = bdlen;	/* Block Descriptor length */
5061		} else {
5062			/* 8-byte mode parameter header */
5063			buf[len++] = 0;		/* mode data length */
5064			buf[len++] = 0;
5065			buf[len++] = 0;		/* medium type */
5066			buf[len++] = 0;		/* dev-specific param */
5067			if (bdlen == 16)
5068				buf[len++] = 1;	/* long lba descriptor */
5069			else
5070				buf[len++] = 0;
5071			buf[len++] = 0;
5072			buf[len++] = 0;		/* Block Descriptor length */
5073			buf[len++] = bdlen;
5074		}
5075
5076		sdinfo = sata_get_device_info(
5077		    spx->txlt_sata_hba_inst,
5078		    &spx->txlt_sata_pkt->satapkt_device);
5079
5080		/* Build block descriptor only if not disabled (DBD) */
5081		if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
5082			/* Block descriptor - direct-access device format */
5083			if (bdlen == 8) {
5084				/* build regular block descriptor */
5085				buf[len++] =
5086				    (sdinfo->satadrv_capacity >> 24) & 0xff;
5087				buf[len++] =
5088				    (sdinfo->satadrv_capacity >> 16) & 0xff;
5089				buf[len++] =
5090				    (sdinfo->satadrv_capacity >> 8) & 0xff;
5091				buf[len++] = sdinfo->satadrv_capacity & 0xff;
5092				buf[len++] = 0; /* density code */
5093				buf[len++] = 0;
5094				if (sdinfo->satadrv_type ==
5095				    SATA_DTYPE_ATADISK)
5096					buf[len++] = 2;
5097				else
5098					/* ATAPI */
5099					buf[len++] = 8;
5100				buf[len++] = 0;
5101			} else if (bdlen == 16) {
5102				/* Long LBA Accepted */
5103				/* build long lba block descriptor */
5104#ifndef __lock_lint
5105				buf[len++] =
5106				    (sdinfo->satadrv_capacity >> 56) & 0xff;
5107				buf[len++] =
5108				    (sdinfo->satadrv_capacity >> 48) & 0xff;
5109				buf[len++] =
5110				    (sdinfo->satadrv_capacity >> 40) & 0xff;
5111				buf[len++] =
5112				    (sdinfo->satadrv_capacity >> 32) & 0xff;
5113#endif
5114				buf[len++] =
5115				    (sdinfo->satadrv_capacity >> 24) & 0xff;
5116				buf[len++] =
5117				    (sdinfo->satadrv_capacity >> 16) & 0xff;
5118				buf[len++] =
5119				    (sdinfo->satadrv_capacity >> 8) & 0xff;
5120				buf[len++] = sdinfo->satadrv_capacity & 0xff;
5121				buf[len++] = 0;
5122				buf[len++] = 0; /* density code */
5123				buf[len++] = 0;
5124				buf[len++] = 0;
5125				if (sdinfo->satadrv_type ==
5126				    SATA_DTYPE_ATADISK)
5127					buf[len++] = 2;
5128				else
5129					/* ATAPI */
5130					buf[len++] = 8;
5131				buf[len++] = 0;
5132			}
5133		}
5134
5135		sata_id = &sdinfo->satadrv_id;
5136
5137		/*
5138		 * Add requested pages.
5139		 * Page 3 and 4 are obsolete and we are not supporting them.
5140		 * We deal now with:
5141		 * caching (read/write cache control).
5142		 * We should eventually deal with following mode pages:
5143		 * error recovery  (0x01),
5144		 * power condition (0x1a),
5145		 * exception control page (enables SMART) (0x1c),
5146		 * enclosure management (ses),
5147		 * protocol-specific port mode (port control).
5148		 */
5149		switch (scsipkt->pkt_cdbp[2] & 0x3f) {
5150		case MODEPAGE_RW_ERRRECOV:
5151			/* DAD_MODE_ERR_RECOV */
5152			/* R/W recovery */
5153			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
5154			break;
5155		case MODEPAGE_CACHING:
5156			/* DAD_MODE_CACHE */
5157			/* Reject not supported request for saved parameters */
5158			if (pc == 3) {
5159				*scsipkt->pkt_scbp = STATUS_CHECK;
5160				sense = sata_arq_sense(spx);
5161				sense->es_key = KEY_ILLEGAL_REQUEST;
5162				sense->es_add_code =
5163				    SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
5164				goto done;
5165			}
5166
5167			/* caching */
5168			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
5169			break;
5170		case MODEPAGE_INFO_EXCPT:
5171			/* exception cntrl */
5172			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
5173				len += sata_build_msense_page_1c(sdinfo, pc,
5174				    buf+len);
5175			}
5176			else
5177				goto err;
5178			break;
5179		case MODEPAGE_POWER_COND:
5180			/* DAD_MODE_POWER_COND */
5181			/* power condition */
5182			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
5183			break;
5184
5185		case MODEPAGE_ACOUSTIC_MANAG:
5186			/* acoustic management */
5187			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
5188			break;
5189		case MODEPAGE_ALLPAGES:
5190			/* all pages */
5191			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
5192			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
5193			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
5194			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
5195				len += sata_build_msense_page_1c(sdinfo, pc,
5196				    buf+len);
5197			}
5198			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
5199			break;
5200		default:
5201		err:
5202			/* Invalid request */
5203			*scsipkt->pkt_scbp = STATUS_CHECK;
5204			sense = sata_arq_sense(spx);
5205			sense->es_key = KEY_ILLEGAL_REQUEST;
5206			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5207			goto done;
5208		}
5209
5210		/* fix total mode data length */
5211		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5212			/* 4-byte mode parameter header */
5213			buf[0] = len - 1;	/* mode data length */
5214		} else {
5215			buf[0] = (len -2) >> 8;
5216			buf[1] = (len -2) & 0xff;
5217		}
5218
5219
5220		/* Check allocation length */
5221		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5222			alc_len = scsipkt->pkt_cdbp[4];
5223		} else {
5224			alc_len = scsipkt->pkt_cdbp[7];
5225			alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
5226		}
5227		/*
5228		 * We do not check for possible parameters truncation
5229		 * (alc_len < len) assuming that the target driver works
5230		 * correctly. Just avoiding overrun.
5231		 * Copy no more than requested and possible, buffer-wise.
5232		 */
5233		count = MIN(alc_len, len);
5234		count = MIN(bp->b_bcount, count);
5235		bcopy(buf, bp->b_un.b_addr, count);
5236
5237		scsipkt->pkt_state |= STATE_XFERRED_DATA;
5238		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
5239	}
5240	*scsipkt->pkt_scbp = STATUS_GOOD;
5241done:
5242	mutex_exit(cport_mutex);
5243	(void) kmem_free(buf, 1024);
5244
5245	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5246	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5247
5248	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5249	    scsipkt->pkt_comp != NULL) {
5250		/* scsi callback required */
5251		if (servicing_interrupt()) {
5252			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5253			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5254			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
5255			    TASKQID_INVALID) {
5256				return (TRAN_BUSY);
5257			}
5258		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5259		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5260		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
5261			/* Scheduling the callback failed */
5262			return (TRAN_BUSY);
5263		}
5264	}
5265
5266	return (TRAN_ACCEPT);
5267}
5268
5269
5270/*
5271 * SATA translate command: Mode Select.
5272 * Translated into appropriate SATA command or emulated.
5273 * Saving parameters is not supported.
5274 * Changing device capacity is not supported (although theoretically
5275 * possible by executing SET FEATURES/SET MAX ADDRESS)
5276 *
5277 * Assumption is that the target driver is working correctly.
5278 *
5279 * More than one SATA command may be executed to perform operations specified
5280 * by mode select pages. The first error terminates further execution.
5281 * Operations performed successully are not backed-up in such case.
5282 *
5283 * NOTE: Implemented pages:
5284 * - caching page
5285 * - informational exception page
5286 * - acoustic management page
5287 * - power condition page
5288 * Caching setup is remembered so it could be re-stored in case of
5289 * an unexpected device reset.
5290 *
5291 * Returns TRAN_XXXX.
5292 * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields.
5293 */
5294
5295static int
5296sata_txlt_mode_select(sata_pkt_txlate_t *spx)
5297{
5298	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5299	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5300	struct scsi_extended_sense *sense;
5301	int len, pagelen, count, pllen;
5302	uint8_t *buf;	/* mode select buffer */
5303	int rval, stat, reason;
5304	uint_t nointr_flag;
5305	int dmod = 0;
5306	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5307
5308	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5309	    "sata_txlt_mode_select, pc %x page code 0x%02x\n",
5310	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5311	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5312
5313	mutex_enter(cport_mutex);
5314
5315	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
5316	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5317		mutex_exit(cport_mutex);
5318		return (rval);
5319	}
5320
5321	rval = TRAN_ACCEPT;
5322
5323	scsipkt->pkt_reason = CMD_CMPLT;
5324	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5325	    STATE_SENT_CMD | STATE_GOT_STATUS;
5326	nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
5327
5328	/* Reject not supported request */
5329	if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
5330		*scsipkt->pkt_scbp = STATUS_CHECK;
5331		sense = sata_arq_sense(spx);
5332		sense->es_key = KEY_ILLEGAL_REQUEST;
5333		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5334		goto done;
5335	}
5336
5337	if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
5338		pllen = scsipkt->pkt_cdbp[4];
5339	} else {
5340		pllen = scsipkt->pkt_cdbp[7];
5341		pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
5342	}
5343
5344	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
5345
5346	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
5347		buf = (uint8_t *)bp->b_un.b_addr;
5348		count = MIN(bp->b_bcount, pllen);
5349		scsipkt->pkt_state |= STATE_XFERRED_DATA;
5350		scsipkt->pkt_resid = 0;
5351		pllen = count;
5352
5353		/*
5354		 * Check the header to skip the block descriptor(s) - we
5355		 * do not support setting device capacity.
5356		 * Existing macros do not recognize long LBA dscriptor,
5357		 * hence manual calculation.
5358		 */
5359		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
5360			/* 6-bytes CMD, 4 bytes header */
5361			if (count <= 4)
5362				goto done;		/* header only */
5363			len = buf[3] + 4;
5364		} else {
5365			/* 10-bytes CMD, 8 bytes header */
5366			if (count <= 8)
5367				goto done;		/* header only */
5368			len = buf[6];
5369			len = (len << 8) + buf[7] + 8;
5370		}
5371		if (len >= count)
5372			goto done;	/* header + descriptor(s) only */
5373
5374		pllen -= len;		/* remaining data length */
5375
5376		/*
5377		 * We may be executing SATA command and want to execute it
5378		 * in SYNCH mode, regardless of scsi_pkt setting.
5379		 * Save scsi_pkt setting and indicate SYNCH mode
5380		 */
5381		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5382		    scsipkt->pkt_comp != NULL) {
5383			scsipkt->pkt_flags |= FLAG_NOINTR;
5384		}
5385		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
5386
5387		/*
5388		 * len is now the offset to a first mode select page
5389		 * Process all pages
5390		 */
5391		while (pllen > 0) {
5392			switch ((int)buf[len]) {
5393			case MODEPAGE_CACHING:
5394				/* No support for SP (saving) */
5395				if (scsipkt->pkt_cdbp[1] & 0x01) {
5396					*scsipkt->pkt_scbp = STATUS_CHECK;
5397					sense = sata_arq_sense(spx);
5398					sense->es_key = KEY_ILLEGAL_REQUEST;
5399					sense->es_add_code =
5400					    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5401					goto done;
5402				}
5403				stat = sata_mode_select_page_8(spx,
5404				    (struct mode_cache_scsi3 *)&buf[len],
5405				    pllen, &pagelen, &rval, &dmod);
5406				/*
5407				 * The pagelen value indicates the number of
5408				 * parameter bytes already processed.
5409				 * The rval is the return value from
5410				 * sata_tran_start().
5411				 * The stat indicates the overall status of
5412				 * the operation(s).
5413				 */
5414				if (stat != SATA_SUCCESS)
5415					/*
5416					 * Page processing did not succeed -
5417					 * all error info is already set-up,
5418					 * just return
5419					 */
5420					pllen = 0; /* this breaks the loop */
5421				else {
5422					len += pagelen;
5423					pllen -= pagelen;
5424				}
5425				break;
5426
5427			case MODEPAGE_INFO_EXCPT:
5428				stat = sata_mode_select_page_1c(spx,
5429				    (struct mode_info_excpt_page *)&buf[len],
5430				    pllen, &pagelen, &rval, &dmod);
5431				/*
5432				 * The pagelen value indicates the number of
5433				 * parameter bytes already processed.
5434				 * The rval is the return value from
5435				 * sata_tran_start().
5436				 * The stat indicates the overall status of
5437				 * the operation(s).
5438				 */
5439				if (stat != SATA_SUCCESS)
5440					/*
5441					 * Page processing did not succeed -
5442					 * all error info is already set-up,
5443					 * just return
5444					 */
5445					pllen = 0; /* this breaks the loop */
5446				else {
5447					len += pagelen;
5448					pllen -= pagelen;
5449				}
5450				break;
5451
5452			case MODEPAGE_ACOUSTIC_MANAG:
5453				stat = sata_mode_select_page_30(spx,
5454				    (struct mode_acoustic_management *)
5455				    &buf[len], pllen, &pagelen, &rval, &dmod);
5456				/*
5457				 * The pagelen value indicates the number of
5458				 * parameter bytes already processed.
5459				 * The rval is the return value from
5460				 * sata_tran_start().
5461				 * The stat indicates the overall status of
5462				 * the operation(s).
5463				 */
5464				if (stat != SATA_SUCCESS)
5465					/*
5466					 * Page processing did not succeed -
5467					 * all error info is already set-up,
5468					 * just return
5469					 */
5470					pllen = 0; /* this breaks the loop */
5471				else {
5472					len += pagelen;
5473					pllen -= pagelen;
5474				}
5475
5476				break;
5477			case MODEPAGE_POWER_COND:
5478				stat = sata_mode_select_page_1a(spx,
5479				    (struct mode_info_power_cond *)&buf[len],
5480				    pllen, &pagelen, &rval, &dmod);
5481				/*
5482				 * The pagelen value indicates the number of
5483				 * parameter bytes already processed.
5484				 * The rval is the return value from
5485				 * sata_tran_start().
5486				 * The stat indicates the overall status of
5487				 * the operation(s).
5488				 */
5489				if (stat != SATA_SUCCESS)
5490					/*
5491					 * Page processing did not succeed -
5492					 * all error info is already set-up,
5493					 * just return
5494					 */
5495					pllen = 0; /* this breaks the loop */
5496				else {
5497					len += pagelen;
5498					pllen -= pagelen;
5499				}
5500				break;
5501			default:
5502				*scsipkt->pkt_scbp = STATUS_CHECK;
5503				sense = sata_arq_sense(spx);
5504				sense->es_key = KEY_ILLEGAL_REQUEST;
5505				sense->es_add_code =
5506				    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
5507				goto done;
5508			}
5509		}
5510	}
5511done:
5512	mutex_exit(cport_mutex);
5513	/*
5514	 * If device parameters were modified, fetch and store the new
5515	 * Identify Device data. Since port mutex could have been released
5516	 * for accessing HBA driver, we need to re-check device existence.
5517	 */
5518	if (dmod != 0) {
5519		sata_drive_info_t new_sdinfo, *sdinfo;
5520		int rv = 0;
5521
5522		/*
5523		 * Following statement has to be changed if this function is
5524		 * used for devices other than SATA hard disks.
5525		 */
5526		new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
5527
5528		new_sdinfo.satadrv_addr =
5529		    spx->txlt_sata_pkt->satapkt_device.satadev_addr;
5530		rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
5531		    &new_sdinfo);
5532
5533		mutex_enter(cport_mutex);
5534		/*
5535		 * Since port mutex could have been released when
5536		 * accessing HBA driver, we need to re-check that the
5537		 * framework still holds the device info structure.
5538		 */
5539		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
5540		    &spx->txlt_sata_pkt->satapkt_device);
5541		if (sdinfo != NULL) {
5542			/*
5543			 * Device still has info structure in the
5544			 * sata framework. Copy newly fetched info
5545			 */
5546			if (rv == 0) {
5547				sdinfo->satadrv_id = new_sdinfo.satadrv_id;
5548				sata_save_drive_settings(sdinfo);
5549			} else {
5550				/*
5551				 * Could not fetch new data - invalidate
5552				 * sata_drive_info. That makes device
5553				 * unusable.
5554				 */
5555				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
5556				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
5557			}
5558		}
5559		if (rv != 0 || sdinfo == NULL) {
5560			/*
5561			 * This changes the overall mode select completion
5562			 * reason to a failed one !!!!!
5563			 */
5564			*scsipkt->pkt_scbp = STATUS_CHECK;
5565			sense = sata_arq_sense(spx);
5566			scsipkt->pkt_reason = CMD_INCOMPLETE;
5567			rval = TRAN_ACCEPT;
5568		}
5569		mutex_exit(cport_mutex);
5570	}
5571	/* Restore the scsi pkt flags */
5572	scsipkt->pkt_flags &= ~FLAG_NOINTR;
5573	scsipkt->pkt_flags |= nointr_flag;
5574
5575	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5576	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5577
5578	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5579	    scsipkt->pkt_comp != NULL) {
5580		/* scsi callback required */
5581		if (servicing_interrupt()) {
5582			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5583			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5584			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
5585			    TASKQID_INVALID) {
5586				return (TRAN_BUSY);
5587			}
5588		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5589		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5590		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
5591			/* Scheduling the callback failed */
5592			return (TRAN_BUSY);
5593		}
5594	}
5595
5596	return (rval);
5597}
5598
5599/*
5600 * Translate command: ATA Pass Through
5601 * Incomplete implementation.  Only supports No-Data, PIO Data-In, and
5602 * PIO Data-Out protocols.  Also supports CK_COND bit.
5603 *
5604 * Mapping of the incoming CDB bytes to the outgoing satacmd bytes is
5605 * described in Table 111 of SAT-2 (Draft 9).
5606 */
5607static  int
5608sata_txlt_ata_pass_thru(sata_pkt_txlate_t *spx)
5609{
5610	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5611	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5612	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5613	int extend;
5614	uint64_t lba;
5615	uint16_t feature, sec_count;
5616	int t_len, synch;
5617	int rval, reason;
5618	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5619
5620	mutex_enter(cport_mutex);
5621
5622	rval = sata_txlt_generic_pkt_info(spx, &reason, 1);
5623	if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5624		mutex_exit(cport_mutex);
5625		return (rval);
5626	}
5627
5628	/* T_DIR bit */
5629	if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_T_DIR)
5630		scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
5631	else
5632		scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
5633
5634	/* MULTIPLE_COUNT field.  If non-zero, invalid command (for now). */
5635	if (((scsipkt->pkt_cdbp[1] >> 5) & 0x7) != 0) {
5636		mutex_exit(cport_mutex);
5637		return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5638	}
5639
5640	/* OFFLINE field. If non-zero, invalid command (for now). */
5641	if (((scsipkt->pkt_cdbp[2] >> 6) & 0x3) != 0) {
5642		mutex_exit(cport_mutex);
5643		return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5644	}
5645
5646	/* PROTOCOL field */
5647	switch ((scsipkt->pkt_cdbp[1] >> 1) & 0xf) {
5648	case SATL_APT_P_HW_RESET:
5649	case SATL_APT_P_SRST:
5650	case SATL_APT_P_DMA:
5651	case SATL_APT_P_DMA_QUEUED:
5652	case SATL_APT_P_DEV_DIAG:
5653	case SATL_APT_P_DEV_RESET:
5654	case SATL_APT_P_UDMA_IN:
5655	case SATL_APT_P_UDMA_OUT:
5656	case SATL_APT_P_FPDMA:
5657	case SATL_APT_P_RET_RESP:
5658		/* Not yet implemented */
5659	default:
5660		mutex_exit(cport_mutex);
5661		return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5662
5663	case SATL_APT_P_NON_DATA:
5664		scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
5665		break;
5666
5667	case SATL_APT_P_PIO_DATA_IN:
5668		/* If PROTOCOL disagrees with T_DIR, invalid command */
5669		if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_WRITE) {
5670			mutex_exit(cport_mutex);
5671			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5672		}
5673
5674		/* if there is a buffer, release its DMA resources */
5675		if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) {
5676			sata_scsi_dmafree(NULL, scsipkt);
5677		} else {
5678			/* if there is no buffer, how do you PIO in? */
5679			mutex_exit(cport_mutex);
5680			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5681		}
5682
5683		break;
5684
5685	case SATL_APT_P_PIO_DATA_OUT:
5686		/* If PROTOCOL disagrees with T_DIR, invalid command */
5687		if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_READ) {
5688			mutex_exit(cport_mutex);
5689			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5690		}
5691
5692		/* if there is a buffer, release its DMA resources */
5693		if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) {
5694			sata_scsi_dmafree(NULL, scsipkt);
5695		} else {
5696			/* if there is no buffer, how do you PIO out? */
5697			mutex_exit(cport_mutex);
5698			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5699		}
5700
5701		break;
5702	}
5703
5704	/* Parse the ATA cmd fields, transfer some straight to the satacmd */
5705	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
5706	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12:
5707		feature = scsipkt->pkt_cdbp[3];
5708
5709		sec_count = scsipkt->pkt_cdbp[4];
5710
5711		lba = scsipkt->pkt_cdbp[8] & 0xf;
5712		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5713		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
5714		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5715
5716		scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] & 0xf0;
5717		scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[9];
5718
5719		break;
5720
5721	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16:
5722		if (scsipkt->pkt_cdbp[1] & SATL_APT_BM_EXTEND) {
5723			extend = 1;
5724
5725			feature = scsipkt->pkt_cdbp[3];
5726			feature = (feature << 8) | scsipkt->pkt_cdbp[4];
5727
5728			sec_count = scsipkt->pkt_cdbp[5];
5729			sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[6];
5730
5731			lba = scsipkt->pkt_cdbp[11];
5732			lba = (lba << 8) | scsipkt->pkt_cdbp[12];
5733			lba = (lba << 8) | scsipkt->pkt_cdbp[9];
5734			lba = (lba << 8) | scsipkt->pkt_cdbp[10];
5735			lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5736			lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5737
5738			scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13];
5739			scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14];
5740		} else {
5741			feature = scsipkt->pkt_cdbp[3];
5742
5743			sec_count = scsipkt->pkt_cdbp[5];
5744
5745			lba = scsipkt->pkt_cdbp[13] & 0xf;
5746			lba = (lba << 8) | scsipkt->pkt_cdbp[12];
5747			lba = (lba << 8) | scsipkt->pkt_cdbp[10];
5748			lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5749
5750			scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] &
5751			    0xf0;
5752			scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14];
5753		}
5754
5755		break;
5756	}
5757
5758	/* CK_COND bit */
5759	if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) {
5760		if (extend) {
5761			scmd->satacmd_flags.sata_copy_out_sec_count_msb = 1;
5762			scmd->satacmd_flags.sata_copy_out_lba_low_msb = 1;
5763			scmd->satacmd_flags.sata_copy_out_lba_mid_msb = 1;
5764			scmd->satacmd_flags.sata_copy_out_lba_high_msb = 1;
5765		}
5766
5767		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1;
5768		scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1;
5769		scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1;
5770		scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1;
5771		scmd->satacmd_flags.sata_copy_out_device_reg = 1;
5772		scmd->satacmd_flags.sata_copy_out_error_reg = 1;
5773	}
5774
5775	/* Transfer remaining parsed ATA cmd values to the satacmd */
5776	if (extend) {
5777		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
5778
5779		scmd->satacmd_features_reg_ext = (feature >> 8) & 0xff;
5780		scmd->satacmd_sec_count_msb = (sec_count >> 8) & 0xff;
5781		scmd->satacmd_lba_low_msb = (lba >> 8) & 0xff;
5782		scmd->satacmd_lba_mid_msb = (lba >> 8) & 0xff;
5783		scmd->satacmd_lba_high_msb = lba >> 40;
5784	} else {
5785		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
5786
5787		scmd->satacmd_features_reg_ext = 0;
5788		scmd->satacmd_sec_count_msb = 0;
5789		scmd->satacmd_lba_low_msb = 0;
5790		scmd->satacmd_lba_mid_msb = 0;
5791		scmd->satacmd_lba_high_msb = 0;
5792	}
5793
5794	scmd->satacmd_features_reg = feature & 0xff;
5795	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
5796	scmd->satacmd_lba_low_lsb = lba & 0xff;
5797	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
5798	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
5799
5800	/* Determine transfer length */
5801	switch (scsipkt->pkt_cdbp[2] & 0x3) {		/* T_LENGTH field */
5802	case 1:
5803		t_len = feature;
5804		break;
5805	case 2:
5806		t_len = sec_count;
5807		break;
5808	default:
5809		t_len = 0;
5810		break;
5811	}
5812
5813	/* Adjust transfer length for the Byte Block bit */
5814	if ((scsipkt->pkt_cdbp[2] >> 2) & 1)
5815		t_len *= SATA_DISK_SECTOR_SIZE;
5816
5817	/* Start processing command */
5818	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5819		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_apt_completion;
5820		synch = FALSE;
5821	} else {
5822		synch = TRUE;
5823	}
5824
5825	if (sata_hba_start(spx, &rval) != 0) {
5826		mutex_exit(cport_mutex);
5827		return (rval);
5828	}
5829
5830	mutex_exit(cport_mutex);
5831
5832	if (synch) {
5833		sata_txlt_apt_completion(spx->txlt_sata_pkt);
5834	}
5835
5836	return (TRAN_ACCEPT);
5837}
5838
5839/*
5840 * Translate command: Log Sense
5841 */
5842static int
5843sata_txlt_log_sense(sata_pkt_txlate_t *spx)
5844{
5845	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
5846	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5847	sata_drive_info_t *sdinfo;
5848	struct scsi_extended_sense *sense;
5849	int		len, count, alc_len;
5850	int		pc;	/* Page Control code */
5851	int		page_code;	/* Page code */
5852	uint8_t		*buf;	/* log sense buffer */
5853	int		rval, reason;
5854#define	MAX_LOG_SENSE_PAGE_SIZE	512
5855	kmutex_t	*cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5856
5857	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5858	    "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
5859	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5860	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5861
5862	if (servicing_interrupt()) {
5863		buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_NOSLEEP);
5864		if (buf == NULL) {
5865			return (TRAN_BUSY);
5866		}
5867	} else {
5868		buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
5869	}
5870
5871	mutex_enter(cport_mutex);
5872
5873	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
5874	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5875		mutex_exit(cport_mutex);
5876		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
5877		return (rval);
5878	}
5879
5880	scsipkt->pkt_reason = CMD_CMPLT;
5881	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5882	    STATE_SENT_CMD | STATE_GOT_STATUS;
5883
5884	pc = scsipkt->pkt_cdbp[2] >> 6;
5885	page_code = scsipkt->pkt_cdbp[2] & 0x3f;
5886
5887	/* Reject not supported request for all but cumulative values */
5888	switch (pc) {
5889	case PC_CUMULATIVE_VALUES:
5890		break;
5891	default:
5892		*scsipkt->pkt_scbp = STATUS_CHECK;
5893		sense = sata_arq_sense(spx);
5894		sense->es_key = KEY_ILLEGAL_REQUEST;
5895		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5896		goto done;
5897	}
5898
5899	switch (page_code) {
5900	case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5901	case PAGE_CODE_SELF_TEST_RESULTS:
5902	case PAGE_CODE_INFORMATION_EXCEPTIONS:
5903	case PAGE_CODE_SMART_READ_DATA:
5904	case PAGE_CODE_START_STOP_CYCLE_COUNTER:
5905		break;
5906	default:
5907		*scsipkt->pkt_scbp = STATUS_CHECK;
5908		sense = sata_arq_sense(spx);
5909		sense->es_key = KEY_ILLEGAL_REQUEST;
5910		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5911		goto done;
5912	}
5913
5914	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
5915		/*
5916		 * Because log sense uses local buffers for data retrieval from
5917		 * the devices and sets the data programatically in the
5918		 * original specified buffer, release preallocated DMA
5919		 * resources before storing data in the original buffer,
5920		 * so no unwanted DMA sync would take place.
5921		 */
5922		sata_id_t *sata_id;
5923
5924		sata_scsi_dmafree(NULL, scsipkt);
5925
5926		len = 0;
5927
5928		/* Build log parameter header */
5929		buf[len++] = page_code;	/* page code as in the CDB */
5930		buf[len++] = 0;		/* reserved */
5931		buf[len++] = 0;		/* Zero out page length for now (MSB) */
5932		buf[len++] = 0;		/* (LSB) */
5933
5934		sdinfo = sata_get_device_info(
5935		    spx->txlt_sata_hba_inst,
5936		    &spx->txlt_sata_pkt->satapkt_device);
5937
5938		/*
5939		 * Add requested pages.
5940		 */
5941		switch (page_code) {
5942		case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5943			len = sata_build_lsense_page_0(sdinfo, buf + len);
5944			break;
5945		case PAGE_CODE_SELF_TEST_RESULTS:
5946			sata_id = &sdinfo->satadrv_id;
5947			if ((! (sata_id->ai_cmdset84 &
5948			    SATA_SMART_SELF_TEST_SUPPORTED)) ||
5949			    (! (sata_id->ai_features87 &
5950			    SATA_SMART_SELF_TEST_SUPPORTED))) {
5951				*scsipkt->pkt_scbp = STATUS_CHECK;
5952				sense = sata_arq_sense(spx);
5953				sense->es_key = KEY_ILLEGAL_REQUEST;
5954				sense->es_add_code =
5955				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5956
5957				goto done;
5958			}
5959			len = sata_build_lsense_page_10(sdinfo, buf + len,
5960			    spx->txlt_sata_hba_inst);
5961			break;
5962		case PAGE_CODE_INFORMATION_EXCEPTIONS:
5963			sata_id = &sdinfo->satadrv_id;
5964			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5965				*scsipkt->pkt_scbp = STATUS_CHECK;
5966				sense = sata_arq_sense(spx);
5967				sense->es_key = KEY_ILLEGAL_REQUEST;
5968				sense->es_add_code =
5969				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5970
5971				goto done;
5972			}
5973			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5974				*scsipkt->pkt_scbp = STATUS_CHECK;
5975				sense = sata_arq_sense(spx);
5976				sense->es_key = KEY_ABORTED_COMMAND;
5977				sense->es_add_code =
5978				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5979				sense->es_qual_code =
5980				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5981
5982				goto done;
5983			}
5984
5985			len = sata_build_lsense_page_2f(sdinfo, buf + len,
5986			    spx->txlt_sata_hba_inst);
5987			break;
5988		case PAGE_CODE_SMART_READ_DATA:
5989			sata_id = &sdinfo->satadrv_id;
5990			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5991				*scsipkt->pkt_scbp = STATUS_CHECK;
5992				sense = sata_arq_sense(spx);
5993				sense->es_key = KEY_ILLEGAL_REQUEST;
5994				sense->es_add_code =
5995				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5996
5997				goto done;
5998			}
5999			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
6000				*scsipkt->pkt_scbp = STATUS_CHECK;
6001				sense = sata_arq_sense(spx);
6002				sense->es_key = KEY_ABORTED_COMMAND;
6003				sense->es_add_code =
6004				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
6005				sense->es_qual_code =
6006				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
6007
6008				goto done;
6009			}
6010
6011			/* This page doesn't include a page header */
6012			len = sata_build_lsense_page_30(sdinfo, buf,
6013			    spx->txlt_sata_hba_inst);
6014			goto no_header;
6015		case PAGE_CODE_START_STOP_CYCLE_COUNTER:
6016			sata_id = &sdinfo->satadrv_id;
6017			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
6018				*scsipkt->pkt_scbp = STATUS_CHECK;
6019				sense = sata_arq_sense(spx);
6020				sense->es_key = KEY_ILLEGAL_REQUEST;
6021				sense->es_add_code =
6022				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6023
6024				goto done;
6025			}
6026			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
6027				*scsipkt->pkt_scbp = STATUS_CHECK;
6028				sense = sata_arq_sense(spx);
6029				sense->es_key = KEY_ABORTED_COMMAND;
6030				sense->es_add_code =
6031				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
6032				sense->es_qual_code =
6033				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
6034
6035				goto done;
6036			}
6037			len = sata_build_lsense_page_0e(sdinfo, buf, spx);
6038			goto no_header;
6039		default:
6040			/* Invalid request */
6041			*scsipkt->pkt_scbp = STATUS_CHECK;
6042			sense = sata_arq_sense(spx);
6043			sense->es_key = KEY_ILLEGAL_REQUEST;
6044			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6045			goto done;
6046		}
6047
6048		/* set parameter log sense data length */
6049		buf[2] = len >> 8;	/* log sense length (MSB) */
6050		buf[3] = len & 0xff;	/* log sense length (LSB) */
6051
6052		len += SCSI_LOG_PAGE_HDR_LEN;
6053		ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
6054
6055no_header:
6056		/* Check allocation length */
6057		alc_len = scsipkt->pkt_cdbp[7];
6058		alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
6059
6060		/*
6061		 * We do not check for possible parameters truncation
6062		 * (alc_len < len) assuming that the target driver works
6063		 * correctly. Just avoiding overrun.
6064		 * Copy no more than requested and possible, buffer-wise.
6065		 */
6066		count = MIN(alc_len, len);
6067		count = MIN(bp->b_bcount, count);
6068		bcopy(buf, bp->b_un.b_addr, count);
6069
6070		scsipkt->pkt_state |= STATE_XFERRED_DATA;
6071		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
6072	}
6073	*scsipkt->pkt_scbp = STATUS_GOOD;
6074done:
6075	mutex_exit(cport_mutex);
6076	(void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
6077
6078	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6079	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6080
6081	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6082	    scsipkt->pkt_comp != NULL) {
6083		/* scsi callback required */
6084		if (servicing_interrupt()) {
6085			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6086			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6087			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
6088			    TASKQID_INVALID) {
6089				return (TRAN_BUSY);
6090			}
6091		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6092		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6093		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
6094			/* Scheduling the callback failed */
6095			return (TRAN_BUSY);
6096		}
6097	}
6098
6099	return (TRAN_ACCEPT);
6100}
6101
6102/*
6103 * Translate command: Log Select
6104 * Not implemented at this time - returns invalid command response.
6105 */
6106static	int
6107sata_txlt_log_select(sata_pkt_txlate_t *spx)
6108{
6109	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6110	    "sata_txlt_log_select\n", NULL);
6111
6112	return (sata_txlt_invalid_command(spx));
6113}
6114
6115
6116/*
6117 * Translate command: Read (various types).
6118 * Translated into appropriate type of ATA READ command
6119 * for SATA hard disks.
6120 * Both the device capabilities and requested operation mode are
6121 * considered.
6122 *
6123 * Following scsi cdb fields are ignored:
6124 * rdprotect, dpo, fua, fua_nv, group_number.
6125 *
6126 * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
6127 * enable variable sata_func_enable), the capability of the controller and
6128 * capability of a device are checked and if both support queueing, read
6129 * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
6130 * command rather than plain READ_XXX command.
6131 * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
6132 * both the controller and device suport such functionality, the read
6133 * request will be translated to READ_FPDMA_QUEUED command.
6134 * In both cases the maximum queue depth is derived as minimum of:
6135 * HBA capability,device capability and sata_max_queue_depth variable setting.
6136 * The value passed to HBA driver is decremented by 1, because only 5 bits are
6137 * used to pass max queue depth value, and the maximum possible queue depth
6138 * is 32.
6139 *
6140 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6141 * appropriate values in scsi_pkt fields.
6142 */
6143static int
6144sata_txlt_read(sata_pkt_txlate_t *spx)
6145{
6146	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6147	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6148	sata_drive_info_t *sdinfo;
6149	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
6150	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6151	uint16_t sec_count;
6152	uint64_t lba;
6153	int rval, reason;
6154	int synch;
6155
6156	mutex_enter(cport_mutex);
6157
6158	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
6159	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6160		mutex_exit(cport_mutex);
6161		return (rval);
6162	}
6163
6164	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6165	    &spx->txlt_sata_pkt->satapkt_device);
6166
6167	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
6168	/*
6169	 * Extract LBA and sector count from scsi CDB.
6170	 */
6171	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6172	case SCMD_READ:
6173		/* 6-byte scsi read cmd : 0x08 */
6174		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
6175		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
6176		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6177		sec_count = scsipkt->pkt_cdbp[4];
6178		/* sec_count 0 will be interpreted as 256 by a device */
6179		break;
6180	case SCMD_READ_G1:
6181		/* 10-bytes scsi read command : 0x28 */
6182		lba = scsipkt->pkt_cdbp[2];
6183		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6184		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6185		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6186		sec_count = scsipkt->pkt_cdbp[7];
6187		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6188		break;
6189	case SCMD_READ_G5:
6190		/* 12-bytes scsi read command : 0xA8 */
6191		lba = scsipkt->pkt_cdbp[2];
6192		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6193		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6194		lba = (