xref: /illumos-gate/usr/src/uts/common/io/sata/impl/sata.c (revision 8b35e523)
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 */
71 int	sata_debug_flags = 0;
72 int	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 */
81 int 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  */
88 int 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  */
96 static	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  */
106 int sata_auto_online = 0;
107 
108 #ifdef SATA_DEBUG
109 
110 #define	SATA_LOG_D(args)	sata_log args
111 uint64_t mbuf_count = 0;
112 uint64_t mbuffail_count = 0;
113 
114 sata_atapi_cmd_t sata_atapi_trace[64];
115 uint32_t sata_atapi_trace_index = 0;
116 int sata_atapi_trace_save = 1;
117 static	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
127 static void
128 sata_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
134 uint32_t	sata_inject_fault = 0;
135 
136 uint32_t	sata_inject_fault_count = 0;
137 uint32_t	sata_inject_fault_pause_count = 0;
138 uint32_t	sata_fault_type = 0;
139 uint32_t	sata_fault_cmd = 0;
140 dev_info_t	*sata_fault_ctrl = NULL;
141 sata_device_t	sata_fault_device;
142 
143 static	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 
149 static char sata_rev_tag[] = {"1.46"};
150 
151 /*
152  * SATA cb_ops functions
153  */
154 static	int sata_hba_open(dev_t *, int, int, cred_t *);
155 static	int sata_hba_close(dev_t, int, int, cred_t *);
156 static	int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *,	int *);
157 
158 /*
159  * SCSA required entry points
160  */
161 static	int sata_scsi_tgt_init(dev_info_t *, dev_info_t *,
162     scsi_hba_tran_t *, struct scsi_device *);
163 static	int sata_scsi_tgt_probe(struct scsi_device *,
164     int (*callback)(void));
165 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *,
166     scsi_hba_tran_t *, struct scsi_device *);
167 static	int sata_scsi_start(struct scsi_address *, struct scsi_pkt *);
168 static	int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *);
169 static	int sata_scsi_reset(struct scsi_address *, int);
170 static	int sata_scsi_getcap(struct scsi_address *, char *, int);
171 static	int sata_scsi_setcap(struct scsi_address *, char *, int, int);
172 static	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);
175 static	void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *);
176 static	void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *);
177 static	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 */
184 static	void sata_event_daemon(void *);
185 static	void sata_event_thread_control(int);
186 static	void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst);
187 static	void sata_process_pmult_events(sata_hba_inst_t *, uint8_t);
188 static	void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *);
189 static	void sata_process_pmdevice_reset(sata_hba_inst_t *, sata_address_t *);
190 static	void sata_process_port_failed_event(sata_hba_inst_t *,
191     sata_address_t *);
192 static	void sata_process_port_link_events(sata_hba_inst_t *,
193     sata_address_t *);
194 static	void sata_process_pmport_link_events(sata_hba_inst_t *,
195     sata_address_t *);
196 static	void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *);
197 static	void sata_process_pmdevice_detached(sata_hba_inst_t *,
198     sata_address_t *);
199 static	void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *);
200 static	void sata_process_pmdevice_attached(sata_hba_inst_t *,
201     sata_address_t *);
202 static	void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *);
203 static	void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *);
204 static	void sata_process_target_node_cleanup(sata_hba_inst_t *,
205     sata_address_t *);
206 static	void sata_process_device_autoonline(sata_hba_inst_t *,
207     sata_address_t *saddr);
208 
209 /*
210  * Local translation functions
211  */
212 static	int sata_txlt_inquiry(sata_pkt_txlate_t *);
213 static	int sata_txlt_test_unit_ready(sata_pkt_txlate_t *);
214 static	int sata_txlt_start_stop_unit(sata_pkt_txlate_t *);
215 static	int sata_txlt_read_capacity(sata_pkt_txlate_t *);
216 static	int sata_txlt_read_capacity16(sata_pkt_txlate_t *);
217 static  int sata_txlt_unmap(sata_pkt_txlate_t *);
218 static	int sata_txlt_request_sense(sata_pkt_txlate_t *);
219 static	int sata_txlt_read(sata_pkt_txlate_t *);
220 static	int sata_txlt_write(sata_pkt_txlate_t *);
221 static	int sata_txlt_log_sense(sata_pkt_txlate_t *);
222 static	int sata_txlt_log_select(sata_pkt_txlate_t *);
223 static	int sata_txlt_mode_sense(sata_pkt_txlate_t *);
224 static	int sata_txlt_mode_select(sata_pkt_txlate_t *);
225 static	int sata_txlt_ata_pass_thru(sata_pkt_txlate_t *);
226 static	int sata_txlt_synchronize_cache(sata_pkt_txlate_t *);
227 static	int sata_txlt_write_buffer(sata_pkt_txlate_t *);
228 static	int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *);
229 
230 static	int sata_hba_start(sata_pkt_txlate_t *, int *);
231 static	int sata_txlt_invalid_command(sata_pkt_txlate_t *);
232 static	int sata_txlt_check_condition(sata_pkt_txlate_t *, uchar_t, uchar_t);
233 static	int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *);
234 static	int sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *);
235 static  int sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *);
236 static	void sata_txlt_rw_completion(sata_pkt_t *);
237 static	void sata_txlt_nodata_cmd_completion(sata_pkt_t *);
238 static	void sata_txlt_apt_completion(sata_pkt_t *sata_pkt);
239 static	void sata_txlt_unmap_completion(sata_pkt_t *sata_pkt);
240 static	void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *);
241 static	int sata_emul_rw_completion(sata_pkt_txlate_t *);
242 static	void sata_fill_ata_return_desc(sata_pkt_t *, uint8_t, uint8_t,
243     uint8_t);
244 static	struct scsi_extended_sense *sata_immediate_error_response(
245     sata_pkt_txlate_t *, int);
246 static	struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *);
247 
248 static	int sata_txlt_atapi(sata_pkt_txlate_t *);
249 static	void sata_txlt_atapi_completion(sata_pkt_t *);
250 
251 /*
252  * Local functions for ioctl
253  */
254 static	int32_t sata_get_port_num(sata_hba_inst_t *,  struct devctl_iocdata *);
255 static	void sata_cfgadm_state(sata_hba_inst_t *, int32_t,
256     devctl_ap_state_t *);
257 static	dev_info_t *sata_get_target_dip(dev_info_t *, uint8_t, uint8_t);
258 static	dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *);
259 static	dev_info_t *sata_devt_to_devinfo(dev_t);
260 static	int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *);
261 static	int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *);
262 static	int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *);
263 static	int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *);
264 static	int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *);
265 static	int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *);
266 static	int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *);
267 static	int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *);
268 static	int sata_ioctl_reset_all(sata_hba_inst_t *);
269 static	int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *);
270 static	int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *,
271     sata_ioctl_data_t *, int mode);
272 static	int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *,
273     sata_ioctl_data_t *, int mode);
274 static	int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *,
275     sata_ioctl_data_t *, int mode);
276 static	int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *,
277     sata_ioctl_data_t *, int mode);
278 static	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  */
284 static	void sata_remove_hba_instance(dev_info_t *);
285 static	int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *);
286 static	void sata_probe_ports(sata_hba_inst_t *);
287 static	void sata_probe_pmports(sata_hba_inst_t *, uint8_t);
288 static	int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int);
289 static	int sata_reprobe_pmult(sata_hba_inst_t *, sata_device_t *, int);
290 static	int sata_reprobe_pmport(sata_hba_inst_t *, sata_device_t *, int);
291 static	int sata_alloc_pmult(sata_hba_inst_t *, sata_device_t *);
292 static	void sata_free_pmult(sata_hba_inst_t *, sata_device_t *);
293 static	int sata_add_device(dev_info_t *, sata_hba_inst_t *, sata_device_t *);
294 static	int sata_offline_device(sata_hba_inst_t *, sata_device_t *,
295     sata_drive_info_t *);
296 static	dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *,
297     sata_address_t *);
298 static	void sata_remove_target_node(sata_hba_inst_t *,
299     sata_address_t *);
300 static	int sata_validate_scsi_address(sata_hba_inst_t *,
301     struct scsi_address *, sata_device_t *);
302 static	int sata_validate_sata_address(sata_hba_inst_t *, int, int, int);
303 static	sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t));
304 static	void sata_pkt_free(sata_pkt_txlate_t *);
305 static	int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t),
306     caddr_t, ddi_dma_attr_t *);
307 static	void sata_common_free_dma_rsrcs(sata_pkt_txlate_t *);
308 static	int sata_probe_device(sata_hba_inst_t *, sata_device_t *);
309 static	sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *,
310     sata_device_t *);
311 static	int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *);
312 static	void sata_reidentify_device(sata_pkt_txlate_t *);
313 static	struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int);
314 static	void sata_free_local_buffer(sata_pkt_txlate_t *);
315 static	uint64_t sata_check_capacity(sata_drive_info_t *);
316 void	sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *,
317     ddi_dma_attr_t *);
318 static	int sata_fetch_device_identify_data(sata_hba_inst_t *,
319     sata_drive_info_t *);
320 static	void sata_update_port_info(sata_hba_inst_t *, sata_device_t *);
321 static	void sata_update_pmport_info(sata_hba_inst_t *, sata_device_t *);
322 static	void sata_update_port_scr(sata_port_scr_t *, sata_device_t *);
323 static	int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *);
324 static	int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int);
325 static	int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int);
326 static	int sata_set_drive_features(sata_hba_inst_t *,
327     sata_drive_info_t *, int flag);
328 static	void sata_init_write_cache_mode(sata_drive_info_t *sdinfo);
329 static	int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *);
330 static	void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *,
331     uint8_t *);
332 static	int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *,
333     struct scsi_inquiry *);
334 static	int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *);
335 static	int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *);
336 static	int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *);
337 static	int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *);
338 static	int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *);
339 static	int sata_mode_select_page_8(sata_pkt_txlate_t *,
340     struct mode_cache_scsi3 *, int, int *, int *, int *);
341 static	int sata_mode_select_page_1a(sata_pkt_txlate_t *,
342     struct mode_info_power_cond *, int, int *, int *, int *);
343 static	int sata_mode_select_page_1c(sata_pkt_txlate_t *,
344     struct mode_info_excpt_page *, int, int *, int *, int *);
345 static	int sata_mode_select_page_30(sata_pkt_txlate_t *,
346     struct mode_acoustic_management *, int, int *, int *, int *);
347 
348 static	int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *);
349 static	int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *,
350     sata_hba_inst_t *);
351 static	int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *,
352     sata_hba_inst_t *);
353 static	int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *,
354     sata_hba_inst_t *);
355 static	int sata_build_lsense_page_0e(sata_drive_info_t *, uint8_t *,
356     sata_pkt_txlate_t *);
357 
358 static	void sata_set_arq_data(sata_pkt_t *);
359 static	void sata_build_read_verify_cmd(sata_cmd_t *, uint16_t, uint64_t);
360 static	void sata_build_generic_cmd(sata_cmd_t *, uint8_t);
361 static	uint8_t sata_get_standby_timer(uint8_t *timer);
362 
363 static	void sata_save_drive_settings(sata_drive_info_t *);
364 static	void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *);
365 static	void sata_show_pmult_info(sata_hba_inst_t *, sata_device_t *);
366 static	void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...);
367 static	void sata_trace_log(sata_hba_inst_t *, uint_t, const char *fmt, ...);
368 static	int sata_fetch_smart_return_status(sata_hba_inst_t *,
369     sata_drive_info_t *);
370 static	int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *,
371     struct smart_data *);
372 static	int sata_smart_selftest_log(sata_hba_inst_t *,
373     sata_drive_info_t *,
374     struct smart_selftest_log *);
375 static	int sata_ext_smart_selftest_read_log(sata_hba_inst_t *,
376     sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t);
377 static	int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *,
378     uint8_t *, uint8_t, uint8_t);
379 static	int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *,
380     struct read_log_ext_directory *);
381 static	void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int);
382 static	void sata_xlate_errors(sata_pkt_txlate_t *);
383 static	void sata_decode_device_error(sata_pkt_txlate_t *,
384     struct scsi_extended_sense *);
385 static	void sata_set_device_removed(dev_info_t *);
386 static	boolean_t sata_check_device_removed(dev_info_t *);
387 static	void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *);
388 static	int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *,
389     sata_drive_info_t *);
390 static	int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *,
391     sata_drive_info_t *);
392 static	void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *);
393 static	void sata_fixed_sense_data_preset(struct scsi_extended_sense *);
394 static  void sata_target_devid_register(dev_info_t *, sata_drive_info_t *);
395 static  int sata_check_modser(char *, int);
396 
397 /*
398  * FMA
399  */
400 static 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  */
408 static 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 
430 extern struct mod_ops mod_miscops;
431 extern uchar_t	scsi_cdb_size[];
432 
433 static struct modlmisc modlmisc = {
434 	&mod_miscops,			/* Type of module */
435 	"SATA Module"			/* module name */
436 };
437 
438 
439 static 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  */
449 static 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  */
455 static 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  */
469 static	kmutex_t sata_mutex;		/* protects sata_hba_list */
470 static	kmutex_t sata_log_mutex;	/* protects log */
471 
472 static	char sata_log_buf[256];
473 
474 /*
475  * sata trace debug
476  */
477 static	sata_trace_rbuf_t *sata_debug_rbuf;
478 static	sata_trace_dmsg_t *sata_trace_dmsg_alloc(void);
479 static	void sata_trace_dmsg_free(void);
480 static	void sata_trace_rbuf_alloc(void);
481 static	void sata_trace_rbuf_free(void);
482 
483 int	dmsg_ring_size = DMSG_RING_SIZE;
484 
485 /* Default write cache setting for SATA hard disks */
486 int	sata_write_cache = 1;		/* enabled */
487 
488 /* Default write cache setting for SATA ATAPI CD/DVD */
489 int	sata_atapicdvd_write_cache = 1; /* enabled */
490 
491 /* Default write cache setting for SATA ATAPI tape */
492 int	sata_atapitape_write_cache = 1; /* enabled */
493 
494 /* Default write cache setting for SATA ATAPI disk */
495 int	sata_atapidisk_write_cache = 1;	/* enabled */
496 
497 /*
498  * Linked list of HBA instances
499  */
500 static	sata_hba_inst_t *sata_hba_list = NULL;
501 static	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  */
510 static	kmutex_t sata_event_mutex;
511 static	kcondvar_t sata_event_cv;
512 static	kthread_t *sata_event_thread = NULL;
513 static	int sata_event_thread_terminate = 0;
514 static	int sata_event_pending = 0;
515 static	int sata_event_thread_active = 0;
516 extern	pri_t minclsyspri;
517 
518 /*
519  * NCQ error recovery command
520  */
521 static 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  */
547 static 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))
_NOTE(MUTEX_PROTECTS_DATA (sata_mutex,sata_hba_list))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 
616 int
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 
639 int
_fini()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 
655 int
_info(struct modinfo * modinfop)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  */
679 int
sata_hba_init(struct modlinkage * modlp)680 sata_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  */
745 int
sata_hba_attach(dev_info_t * dip,sata_hba_tran_t * sata_tran,ddi_attach_cmd_t cmd)746 sata_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 
943 fail:
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  */
989 int
sata_hba_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)990 sata_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  */
1160 void
sata_hba_fini(struct modlinkage * modlp)1161 sata_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 
1184 static int
sata_hba_open(dev_t * devp,int flags,int otyp,cred_t * credp)1185 sata_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 
1240 static int
sata_hba_close(dev_t dev,int flag,int otyp,cred_t * credp)1241 sata_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 
1304 static int
sata_hba_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)1305 sata_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  */
1705 sata_pkt_t *
sata_get_error_retrieval_pkt(dev_info_t * dip,sata_device_t * sata_device,int pkt_type)1706 sata_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  */
1784 void
sata_free_error_retrieval_pkt(sata_pkt_t * sata_pkt)1785 sata_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  */
1810 sata_pkt_t *
sata_get_rdwr_pmult_pkt(dev_info_t * dip,sata_device_t * sd,uint8_t regn,uint32_t regv,uint32_t type)1811 sata_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  */
1892 void
sata_free_rdwr_pmult_pkt(sata_pkt_t * sata_pkt)1893 sata_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  */
1911 void
sata_register_pmult(dev_info_t * dip,sata_device_t * sd,sata_pmult_gscr_t * sg)1912 sata_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  */
1966 void
sata_split_model(char * model,char ** vendor,char ** product)1967 sata_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  */
2017 static int
sata_name_child(dev_info_t * dip,char * name,int namelen)2018 sata_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 
2042 static int
sata_scsi_tgt_init(dev_info_t * hba_dip,dev_info_t * tgt_dip,scsi_hba_tran_t * hba_tran,struct scsi_device * sd)2043 sata_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  */
2139 static int
sata_scsi_tgt_probe(struct scsi_device * sd,int (* callback)(void))2140 sata_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  */
2174 static void
sata_scsi_tgt_free(dev_info_t * hba_dip,dev_info_t * tgt_dip,scsi_hba_tran_t * hba_tran,struct scsi_device * sd)2175 sata_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  */
2246 static struct scsi_pkt *
sata_scsi_init_pkt(struct scsi_address * ap,struct scsi_pkt * pkt,struct buf * bp,int cmdlen,int statuslen,int tgtlen,int flags,int (* callback)(caddr_t),caddr_t arg)2247 sata_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 
2390 success:
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 
2397 fail:
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  */
2465 static int
sata_scsi_start(struct scsi_address * ap,struct scsi_pkt * pkt)2466 sata_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  */
2709 static int
sata_scsi_abort(struct scsi_address * ap,struct scsi_pkt * scsi_pkt)2710 sata_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  */
2796 static int
sata_scsi_reset(struct scsi_address * ap,int level)2797 sata_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 
2889 static int
sata_scsi_getcap(struct scsi_address * ap,char * cap,int whom)2890 sata_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  */
2994 static int
sata_scsi_setcap(struct scsi_address * ap,char * cap,int value,int whom)2995 sata_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  */
3088 static void
sata_scsi_destroy_pkt(struct scsi_address * ap,struct scsi_pkt * pkt)3089 sata_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 
3108 static void
sata_scsi_dmafree(struct scsi_address * ap,struct scsi_pkt * pkt)3109 sata_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  */
3130 static void
sata_scsi_sync_pkt(struct scsi_address * ap,struct scsi_pkt * pkt)3131 sata_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  */
3196 static int
sata_txlt_generic_pkt_info(sata_pkt_txlate_t * spx,int * reason,int flag)3197 sata_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  */
3391 static	void
sata_identdev_to_inquiry(sata_hba_inst_t * sata_hba_inst,sata_drive_info_t * sdinfo,uint8_t * buf)3392 sata_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  */
3463 static int
sata_txlt_invalid_command(sata_pkt_txlate_t * spx)3464 sata_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  */
3508 static int
sata_txlt_check_condition(sata_pkt_txlate_t * spx,uchar_t key,uchar_t code)3509 sata_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  */
3558 static	int
sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t * spx)3559 sata_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