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 * Copyright 2024 RackTop Systems, Inc.
30 * Copyright 2023 Oxide Computer Company
31 * Copyright 2023 Jason King
32 */
33
34 /*
35 * SATA Framework
36 * Generic SATA Host Adapter Implementation
37 */
38
39 #include <sys/conf.h>
40 #include <sys/file.h>
41 #include <sys/ddi.h>
42 #include <sys/sunddi.h>
43 #include <sys/modctl.h>
44 #include <sys/cmn_err.h>
45 #include <sys/errno.h>
46 #include <sys/thread.h>
47 #include <sys/kstat.h>
48 #include <sys/note.h>
49 #include <sys/sysevent.h>
50 #include <sys/sysevent/eventdefs.h>
51 #include <sys/sysevent/dr.h>
52 #include <sys/taskq.h>
53 #include <sys/disp.h>
54 #include <sys/sdt.h>
55
56 #include <sys/sata/impl/sata.h>
57 #include <sys/sata/sata_hba.h>
58 #include <sys/sata/sata_defs.h>
59 #include <sys/sata/sata_cfgadm.h>
60 #include <sys/sata/sata_blacklist.h>
61 #include <sys/sata/sata_satl.h>
62
63 #include <sys/scsi/impl/spc3_types.h>
64
65 /*
66 * FMA header files
67 */
68 #include <sys/ddifm.h>
69 #include <sys/fm/protocol.h>
70 #include <sys/fm/util.h>
71 #include <sys/fm/io/ddi.h>
72
73 /* Debug flags - defined in sata.h */
74 int sata_debug_flags = 0;
75 int sata_msg = 0;
76
77 /*
78 * Flags enabling selected SATA HBA framework functionality
79 */
80 #define SATA_ENABLE_QUEUING 1
81 #define SATA_ENABLE_NCQ 2
82 #define SATA_ENABLE_PROCESS_EVENTS 4
83 #define SATA_ENABLE_PMULT_FBS 8 /* FIS-Based Switching */
84 int sata_func_enable =
85 SATA_ENABLE_PROCESS_EVENTS | SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ;
86
87 /*
88 * Global variable setting default maximum queue depth (NCQ or TCQ)
89 * Note:minimum queue depth is 1
90 */
91 int sata_max_queue_depth = SATA_MAX_QUEUE_DEPTH; /* max NCQ/TCQ queue depth */
92
93 /*
94 * Currently used default NCQ/TCQ queue depth. It is set-up during the driver
95 * initialization, using value from sata_max_queue_depth
96 * It is adjusted to minimum supported by the controller and by the device,
97 * if queueing is enabled.
98 */
99 static int sata_current_max_qdepth;
100
101 /*
102 * Global variable determining the default behavior after device hotpluggin.
103 * If non-zero, the hotplugged device is onlined (if possible) without explicit
104 * IOCTL request (AP_CONFIGURE).
105 * If zero, hotplugged device is identified, but not onlined.
106 * Enabling (AP_CONNECT) device port with an attached device does not result
107 * in device onlining regardless of the flag setting
108 */
109 int sata_auto_online = 0;
110
111 #ifdef SATA_DEBUG
112
113 #define SATA_LOG_D(args) sata_log args
114 uint64_t mbuf_count = 0;
115 uint64_t mbuffail_count = 0;
116
117 sata_atapi_cmd_t sata_atapi_trace[64];
118 uint32_t sata_atapi_trace_index = 0;
119 int sata_atapi_trace_save = 1;
120 static void sata_save_atapi_trace(sata_pkt_txlate_t *, int);
121 #define SATAATAPITRACE(spx, count) \
122 if (sata_atapi_trace_save) \
123 sata_save_atapi_trace(spx, count)
124
125 #else
126 #define SATA_LOG_D(args) sata_trace_log args
127 #define SATAATAPITRACE(spx, count)
128 #endif
129
130 #if 0
131 static void
132 sata_test_atapi_packet_command(sata_hba_inst_t *, int);
133 #endif
134
135 #ifdef SATA_INJECT_FAULTS
136
137 #define SATA_INJECT_PKT_FAULT 1
138 uint32_t sata_inject_fault = 0;
139
140 uint32_t sata_inject_fault_count = 0;
141 uint32_t sata_inject_fault_pause_count = 0;
142 uint32_t sata_fault_type = 0;
143 uint32_t sata_fault_cmd = 0;
144 dev_info_t *sata_fault_ctrl = NULL;
145 sata_device_t sata_fault_device;
146
147 static void sata_inject_pkt_fault(sata_pkt_t *, int *, int);
148
149 #endif
150
151 #define LEGACY_HWID_LEN 64 /* Model (40) + Serial (20) + pad */
152
153 /*
154 * SATA cb_ops functions
155 */
156 static int sata_hba_open(dev_t *, int, int, cred_t *);
157 static int sata_hba_close(dev_t, int, int, cred_t *);
158 static int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
159
160 /*
161 * SCSA required entry points
162 */
163 static int sata_scsi_tgt_init(dev_info_t *, dev_info_t *,
164 scsi_hba_tran_t *, struct scsi_device *);
165 static int sata_scsi_tgt_probe(struct scsi_device *,
166 int (*callback)(void));
167 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *,
168 scsi_hba_tran_t *, struct scsi_device *);
169 static int sata_scsi_start(struct scsi_address *, struct scsi_pkt *);
170 static int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *);
171 static int sata_scsi_reset(struct scsi_address *, int);
172 static int sata_scsi_getcap(struct scsi_address *, char *, int);
173 static int sata_scsi_setcap(struct scsi_address *, char *, int, int);
174 static struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *,
175 struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t),
176 caddr_t);
177 static void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *);
178 static void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *);
179 static void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *);
180
181 /*
182 * SATA HBA interface functions are defined in sata_hba.h header file
183 */
184
185 /* Event processing functions */
186 static void sata_event_daemon(void *);
187 static void sata_event_thread_control(int);
188 static void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst);
189 static void sata_process_pmult_events(sata_hba_inst_t *, uint8_t);
190 static void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *);
191 static void sata_process_pmdevice_reset(sata_hba_inst_t *, sata_address_t *);
192 static void sata_process_port_failed_event(sata_hba_inst_t *,
193 sata_address_t *);
194 static void sata_process_port_link_events(sata_hba_inst_t *,
195 sata_address_t *);
196 static void sata_process_pmport_link_events(sata_hba_inst_t *,
197 sata_address_t *);
198 static void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *);
199 static void sata_process_pmdevice_detached(sata_hba_inst_t *,
200 sata_address_t *);
201 static void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *);
202 static void sata_process_pmdevice_attached(sata_hba_inst_t *,
203 sata_address_t *);
204 static void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *);
205 static void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *);
206 static void sata_process_target_node_cleanup(sata_hba_inst_t *,
207 sata_address_t *);
208 static void sata_process_device_autoonline(sata_hba_inst_t *,
209 sata_address_t *saddr);
210
211 /*
212 * Local translation functions
213 */
214 static int sata_txlt_inquiry(sata_pkt_txlate_t *);
215 static int sata_txlt_test_unit_ready(sata_pkt_txlate_t *);
216 static int sata_txlt_start_stop_unit(sata_pkt_txlate_t *);
217 static int sata_txlt_read_capacity(sata_pkt_txlate_t *);
218 static int sata_txlt_read_capacity16(sata_pkt_txlate_t *);
219 static int sata_txlt_unmap(sata_pkt_txlate_t *);
220 static int sata_txlt_request_sense(sata_pkt_txlate_t *);
221 static int sata_txlt_read(sata_pkt_txlate_t *);
222 static int sata_txlt_write(sata_pkt_txlate_t *);
223 static int sata_txlt_log_sense(sata_pkt_txlate_t *);
224 static int sata_txlt_log_select(sata_pkt_txlate_t *);
225 static int sata_txlt_mode_sense(sata_pkt_txlate_t *);
226 static int sata_txlt_mode_select(sata_pkt_txlate_t *);
227 static int sata_txlt_ata_pass_thru(sata_pkt_txlate_t *);
228 static int sata_txlt_synchronize_cache(sata_pkt_txlate_t *);
229 static int sata_txlt_write_buffer(sata_pkt_txlate_t *);
230 static int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *);
231
232 static int sata_hba_start(sata_pkt_txlate_t *, int *);
233 static int sata_txlt_invalid_command(sata_pkt_txlate_t *);
234 static int sata_txlt_check_condition(sata_pkt_txlate_t *, uchar_t, uchar_t);
235 static int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *);
236 static int sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *);
237 static int sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *);
238 static void sata_txlt_rw_completion(sata_pkt_t *);
239 static void sata_txlt_nodata_cmd_completion(sata_pkt_t *);
240 static void sata_txlt_apt_completion(sata_pkt_t *sata_pkt);
241 static void sata_txlt_unmap_completion(sata_pkt_t *sata_pkt);
242 static int sata_emul_rw_completion(sata_pkt_txlate_t *);
243 static void sata_fill_ata_return_desc(sata_pkt_t *, uint8_t, uint8_t,
244 uint8_t);
245 static struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *);
246
247 static int sata_txlt_atapi(sata_pkt_txlate_t *);
248 static void sata_txlt_atapi_completion(sata_pkt_t *);
249
250 /*
251 * Local functions for ioctl
252 */
253 static int32_t sata_get_port_num(sata_hba_inst_t *, struct devctl_iocdata *);
254 static void sata_cfgadm_state(sata_hba_inst_t *, int32_t,
255 devctl_ap_state_t *);
256 static dev_info_t *sata_get_target_dip(dev_info_t *, uint8_t, uint8_t);
257 static dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *);
258 static dev_info_t *sata_devt_to_devinfo(dev_t);
259 static int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *);
260 static int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *);
261 static int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *);
262 static int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *);
263 static int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *);
264 static int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *);
265 static int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *);
266 static int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *);
267 static int sata_ioctl_reset_all(sata_hba_inst_t *);
268 static int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *);
269 static int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *,
270 sata_ioctl_data_t *, int mode);
271 static int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *,
272 sata_ioctl_data_t *, int mode);
273 static int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *,
274 sata_ioctl_data_t *, int mode);
275 static int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *,
276 sata_ioctl_data_t *, int mode);
277 static int sata_ioctl_get_serialnumber_info(sata_hba_inst_t *,
278 sata_device_t *, sata_ioctl_data_t *, int mode);
279
280 /*
281 * Local functions
282 */
283 static void sata_remove_hba_instance(dev_info_t *);
284 static int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *);
285 static void sata_probe_ports(sata_hba_inst_t *);
286 static void sata_probe_pmports(sata_hba_inst_t *, uint8_t);
287 static int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int);
288 static int sata_reprobe_pmult(sata_hba_inst_t *, sata_device_t *, int);
289 static int sata_reprobe_pmport(sata_hba_inst_t *, sata_device_t *, int);
290 static int sata_alloc_pmult(sata_hba_inst_t *, sata_device_t *);
291 static void sata_free_pmult(sata_hba_inst_t *, sata_device_t *);
292 static int sata_add_device(dev_info_t *, sata_hba_inst_t *, sata_device_t *);
293 static int sata_offline_device(sata_hba_inst_t *, sata_device_t *,
294 sata_drive_info_t *);
295 static dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *,
296 sata_address_t *);
297 static void sata_remove_target_node(sata_hba_inst_t *,
298 sata_address_t *);
299 static int sata_validate_scsi_address(sata_hba_inst_t *,
300 struct scsi_address *, sata_device_t *);
301 static int sata_validate_sata_address(sata_hba_inst_t *, int, int, int);
302 static sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t));
303 static void sata_pkt_free(sata_pkt_txlate_t *);
304 static int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t),
305 caddr_t, ddi_dma_attr_t *);
306 static void sata_common_free_dma_rsrcs(sata_pkt_txlate_t *);
307 static int sata_probe_device(sata_hba_inst_t *, sata_device_t *);
308 static sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *,
309 sata_device_t *);
310 static int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *);
311 static void sata_reidentify_device(sata_pkt_txlate_t *);
312 static struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, size_t);
313 static void sata_free_local_buffer(sata_pkt_txlate_t *);
314 static uint64_t sata_check_capacity(sata_drive_info_t *);
315 void sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *,
316 ddi_dma_attr_t *);
317 static int sata_fetch_device_identify_data(sata_hba_inst_t *,
318 sata_drive_info_t *);
319 static void sata_update_port_info(sata_hba_inst_t *, sata_device_t *);
320 static void sata_update_pmport_info(sata_hba_inst_t *, sata_device_t *);
321 static int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *);
322 static int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int);
323 static int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int);
324 static int sata_set_drive_features(sata_hba_inst_t *,
325 sata_drive_info_t *, int flag);
326 static void sata_init_write_cache_mode(sata_drive_info_t *sdinfo);
327 static int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *);
328 static void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *,
329 uint8_t *);
330 static int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *,
331 struct scsi_inquiry *);
332 static int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *);
333 static int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *);
334 static int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *);
335 static int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *);
336 static int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *);
337 static int sata_mode_select_page_8(sata_pkt_txlate_t *,
338 struct mode_cache_scsi3 *, int, int *, int *, int *);
339 static int sata_mode_select_page_1a(sata_pkt_txlate_t *,
340 struct mode_info_power_cond *, int, int *, int *, int *);
341 static int sata_mode_select_page_1c(sata_pkt_txlate_t *,
342 struct mode_info_excpt_page *, int, int *, int *, int *);
343 static int sata_mode_select_page_30(sata_pkt_txlate_t *,
344 struct mode_acoustic_management *, int, int *, int *, int *);
345
346 static int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *);
347 static int sata_build_lsense_page_03(sata_drive_info_t *, uint8_t *,
348 sata_hba_inst_t *);
349 static int sata_build_lsense_page_0d(sata_drive_info_t *, uint8_t *,
350 sata_hba_inst_t *);
351 static int sata_build_lsense_page_0e(sata_drive_info_t *, uint8_t *,
352 sata_pkt_txlate_t *);
353 static int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *,
354 sata_hba_inst_t *);
355 static int sata_build_lsense_page_11(sata_drive_info_t *, uint8_t *,
356 sata_hba_inst_t *);
357 static int sata_build_lsense_page_19(sata_drive_info_t *, uint8_t *,
358 sata_hba_inst_t *);
359 static int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *,
360 sata_hba_inst_t *);
361 static int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *,
362 sata_hba_inst_t *);
363
364 static void sata_set_arq_data(sata_pkt_t *);
365 static void sata_build_read_verify_cmd(sata_cmd_t *, uint16_t, uint64_t);
366 static void sata_build_generic_cmd(sata_cmd_t *, uint8_t);
367 static uint8_t sata_get_standby_timer(uint8_t *timer);
368
369 static void sata_save_drive_settings(sata_drive_info_t *);
370 static void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *);
371 static void sata_show_pmult_info(sata_hba_inst_t *, sata_device_t *);
372 static void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...);
373 #ifndef SATA_DEBUG
374 static void sata_trace_log(sata_hba_inst_t *, uint_t, const char *fmt, ...);
375 #endif
376 static int sata_fetch_smart_return_status(sata_hba_inst_t *,
377 sata_drive_info_t *);
378 static int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *,
379 struct smart_data *);
380 static int sata_smart_selftest_log(sata_hba_inst_t *,
381 sata_drive_info_t *,
382 struct smart_selftest_log *);
383 static int sata_ext_smart_selftest_read_log(sata_hba_inst_t *,
384 sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t);
385 static int sata_read_log_ext(sata_hba_inst_t *, sata_drive_info_t *, uint8_t,
386 uint16_t, void *, uint16_t);
387 static int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *,
388 uint8_t *, uint8_t, uint8_t);
389 static int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *,
390 struct read_log_ext_directory *);
391 static void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int);
392 static void sata_xlate_errors(sata_pkt_txlate_t *);
393 static void sata_decode_device_error(sata_pkt_txlate_t *,
394 struct scsi_extended_sense *);
395 static void sata_set_device_removed(dev_info_t *);
396 static boolean_t sata_check_device_removed(dev_info_t *);
397 static void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *);
398 static int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *,
399 sata_drive_info_t *);
400 static int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *,
401 sata_drive_info_t *);
402 static void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *);
403 static void sata_fixed_sense_data_preset(struct scsi_extended_sense *);
404 static void sata_target_devid_register(dev_info_t *, sata_drive_info_t *);
405 static int sata_check_modser(char *, int);
406
407 /*
408 * FMA
409 */
410 static boolean_t sata_check_for_dma_error(dev_info_t *, sata_pkt_txlate_t *);
411
412
413 /*
414 * SATA Framework will ignore SATA HBA driver cb_ops structure and
415 * register following one with SCSA framework.
416 * Open & close are provided, so scsi framework will not use its own
417 */
418 static struct cb_ops sata_cb_ops = {
419 sata_hba_open, /* open */
420 sata_hba_close, /* close */
421 nodev, /* strategy */
422 nodev, /* print */
423 nodev, /* dump */
424 nodev, /* read */
425 nodev, /* write */
426 sata_hba_ioctl, /* ioctl */
427 nodev, /* devmap */
428 nodev, /* mmap */
429 nodev, /* segmap */
430 nochpoll, /* chpoll */
431 ddi_prop_op, /* cb_prop_op */
432 0, /* streamtab */
433 D_NEW | D_MP, /* cb_flag */
434 CB_REV, /* rev */
435 nodev, /* aread */
436 nodev /* awrite */
437 };
438
439
440 extern struct mod_ops mod_miscops;
441 extern uchar_t scsi_cdb_size[];
442
443 static struct modlmisc modlmisc = {
444 &mod_miscops, /* Type of module */
445 "SATA Module" /* module name */
446 };
447
448
449 static struct modlinkage modlinkage = {
450 MODREV_1,
451 (void *)&modlmisc,
452 NULL
453 };
454
455 /*
456 * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero,
457 * i.e. when scsi_pkt has not timeout specified.
458 */
459 static int sata_default_pkt_time = 60; /* 60 seconds */
460
461 /*
462 * Intermediate buffer device access attributes - they are required,
463 * but not necessarily used.
464 */
465 static ddi_device_acc_attr_t sata_acc_attr = {
466 DDI_DEVICE_ATTR_V0,
467 DDI_STRUCTURE_LE_ACC,
468 DDI_STRICTORDER_ACC
469 };
470
471
472 /*
473 * Mutexes protecting structures in multithreaded operations.
474 * Because events are relatively rare, a single global mutex protecting
475 * data structures should be sufficient. To increase performance, add
476 * separate mutex per each sata port and use global mutex only to protect
477 * common data structures.
478 */
479 static kmutex_t sata_mutex; /* protects sata_hba_list */
480 static kmutex_t sata_log_mutex; /* protects log */
481
482 static char sata_log_buf[256];
483
484 /*
485 * sata trace debug
486 */
487 static sata_trace_rbuf_t *sata_debug_rbuf;
488 static sata_trace_dmsg_t *sata_trace_dmsg_alloc(void);
489 static void sata_trace_dmsg_free(void);
490 static void sata_trace_rbuf_alloc(void);
491 static void sata_trace_rbuf_free(void);
492
493 int dmsg_ring_size = DMSG_RING_SIZE;
494
495 /* Default write cache setting for SATA hard disks */
496 int sata_write_cache = 1; /* enabled */
497
498 /* Default write cache setting for SATA ATAPI CD/DVD */
499 int sata_atapicdvd_write_cache = 1; /* enabled */
500
501 /* Default write cache setting for SATA ATAPI tape */
502 int sata_atapitape_write_cache = 1; /* enabled */
503
504 /* Default write cache setting for SATA ATAPI disk */
505 int sata_atapidisk_write_cache = 1; /* enabled */
506
507 /*
508 * Linked list of HBA instances
509 */
510 static sata_hba_inst_t *sata_hba_list = NULL;
511 static sata_hba_inst_t *sata_hba_list_tail = NULL;
512 /*
513 * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran
514 * structure and in sata soft state.
515 */
516
517 /*
518 * Event daemon related variables
519 */
520 static kmutex_t sata_event_mutex;
521 static kcondvar_t sata_event_cv;
522 static kthread_t *sata_event_thread = NULL;
523 static int sata_event_thread_terminate = 0;
524 static int sata_event_pending = 0;
525 static int sata_event_thread_active = 0;
526 extern pri_t minclsyspri;
527
528 /*
529 * NCQ error recovery command
530 */
531 static const sata_cmd_t sata_rle_cmd = {
532 SATA_CMD_REV,
533 NULL,
534 {
535 SATA_DIR_READ
536 },
537 ATA_ADDR_LBA48,
538 0,
539 0,
540 0,
541 0,
542 0,
543 1,
544 READ_LOG_EXT_NCQ_ERROR_RECOVERY,
545 0,
546 0,
547 0,
548 SATAC_READ_LOG_EXT,
549 0,
550 0,
551 0,
552 };
553
554 /*
555 * ATAPI error recovery CDB
556 */
557 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = {
558 SCMD_REQUEST_SENSE,
559 0, /* Only fixed RQ format is supported */
560 0,
561 0,
562 SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */
563 0
564 };
565
566
567 /* Warlock directives */
568
569 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran))
570 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device))
571 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops))
572 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense))
573 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status))
574 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr))
575 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t))
576 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state))
577 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state))
_NOTE(MUTEX_PROTECTS_DATA (sata_mutex,sata_hba_list))578 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list))
579 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list))
580 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next))
581 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev))
582 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \
583 sata_hba_inst::satahba_scsi_tran))
584 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran))
585 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip))
586 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached))
587 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port))
588 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex,
589 sata_hba_inst::satahba_event_flags))
590 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
591 sata_cport_info::cport_devp))
592 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp))
593 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr))
594 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
595 sata_cport_info::cport_dev_type))
596 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type))
597 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
598 sata_cport_info::cport_state))
599 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state))
600 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
601 sata_pmport_info::pmport_state))
602 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state))
603 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
604 sata_pmport_info::pmport_dev_type))
605 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type))
606 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
607 sata_pmport_info::pmport_sata_drive))
608 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
609 sata_pmport_info::pmport_tgtnode_clean))
610 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
611 sata_pmport_info::pmport_event_flags))
612 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive))
613 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port))
614 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports))
615 #ifdef SATA_DEBUG
616 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count))
617 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count))
618 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace))
619 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index))
620 #endif
621
622 /* End of warlock directives */
623
624 /* ************** loadable module configuration functions ************** */
625
626 int
627 _init()
628 {
629 int rval;
630
631 mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL);
632 mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL);
633 mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL);
634 cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL);
635 sata_trace_rbuf_alloc();
636 if ((rval = mod_install(&modlinkage)) != 0) {
637 #ifdef SATA_DEBUG
638 cmn_err(CE_WARN, "sata: _init: mod_install failed\n");
639 #endif
640 sata_trace_rbuf_free();
641 mutex_destroy(&sata_log_mutex);
642 cv_destroy(&sata_event_cv);
643 mutex_destroy(&sata_event_mutex);
644 mutex_destroy(&sata_mutex);
645 }
646 return (rval);
647 }
648
649 int
_fini()650 _fini()
651 {
652 int rval;
653
654 if ((rval = mod_remove(&modlinkage)) != 0)
655 return (rval);
656
657 sata_trace_rbuf_free();
658 mutex_destroy(&sata_log_mutex);
659 cv_destroy(&sata_event_cv);
660 mutex_destroy(&sata_event_mutex);
661 mutex_destroy(&sata_mutex);
662 return (rval);
663 }
664
665 int
_info(struct modinfo * modinfop)666 _info(struct modinfo *modinfop)
667 {
668 return (mod_info(&modlinkage, modinfop));
669 }
670
671
672
673 /* ********************* SATA HBA entry points ********************* */
674
675
676 /*
677 * Called by SATA HBA from _init().
678 * Registers HBA driver instance/sata framework pair with scsi framework, by
679 * calling scsi_hba_init().
680 *
681 * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used
682 * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver
683 * cb_ops pointer in SATA HBA driver dev_ops structure.
684 * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors.
685 *
686 * Return status of the scsi_hba_init() is returned to a calling SATA HBA
687 * driver.
688 */
689 int
sata_hba_init(struct modlinkage * modlp)690 sata_hba_init(struct modlinkage *modlp)
691 {
692 int rval;
693 struct dev_ops *hba_ops;
694
695 SATADBG1(SATA_DBG_HBA_IF, NULL,
696 "sata_hba_init: name %s \n",
697 ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
698 /*
699 * Fill-up cb_ops and dev_ops when necessary
700 */
701 hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
702 /*
703 * Provide pointer to SATA dev_ops
704 */
705 hba_ops->devo_cb_ops = &sata_cb_ops;
706
707 /*
708 * Register SATA HBA with SCSI framework
709 */
710 if ((rval = scsi_hba_init(modlp)) != 0) {
711 SATADBG1(SATA_DBG_HBA_IF, NULL,
712 "sata_hba_init: scsi hba init failed\n", NULL);
713 return (rval);
714 }
715
716 return (0);
717 }
718
719
720 /* HBA attach stages */
721 #define HBA_ATTACH_STAGE_SATA_HBA_INST 1
722 #define HBA_ATTACH_STAGE_SCSI_ATTACHED 2
723 #define HBA_ATTACH_STAGE_SETUP 4
724 #define HBA_ATTACH_STAGE_LINKED 8
725
726
727 /*
728 *
729 * Called from SATA HBA driver's attach routine to attach an instance of
730 * the HBA.
731 *
732 * For DDI_ATTACH command:
733 * sata_hba_inst structure is allocated here and initialized with pointers to
734 * SATA framework implementation of required scsi tran functions.
735 * The scsi_tran's tran_hba_private field is used by SATA Framework to point
736 * to the soft structure (sata_hba_inst) allocated by SATA framework for
737 * SATA HBA instance related data.
738 * The scsi_tran's tran_hba_private field is used by SATA framework to
739 * store a pointer to per-HBA-instance of sata_hba_inst structure.
740 * The sata_hba_inst structure is cross-linked to scsi tran structure.
741 * Among other info, a pointer to sata_hba_tran structure is stored in
742 * sata_hba_inst. The sata_hba_inst structures for different HBA instances are
743 * linked together into the list, pointed to by sata_hba_list.
744 * On the first HBA instance attach the sata event thread is initialized.
745 * Attachment points are created for all SATA ports of the HBA being attached.
746 * All HBA instance's SATA ports are probed and type of plugged devices is
747 * determined. For each device of a supported type, a target node is created.
748 *
749 * DDI_SUCCESS is returned when attachment process is successful,
750 * DDI_FAILURE is returned otherwise.
751 *
752 * For DDI_RESUME command:
753 * Not implemented at this time (postponed until phase 2 of the development).
754 */
755 int
sata_hba_attach(dev_info_t * dip,sata_hba_tran_t * sata_tran,ddi_attach_cmd_t cmd)756 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran,
757 ddi_attach_cmd_t cmd)
758 {
759 sata_hba_inst_t *sata_hba_inst;
760 scsi_hba_tran_t *scsi_tran = NULL;
761 int hba_attach_state = 0;
762 char taskq_name[MAXPATHLEN];
763
764 SATADBG3(SATA_DBG_HBA_IF, NULL,
765 "sata_hba_attach: node %s (%s%d)\n",
766 ddi_node_name(dip), ddi_driver_name(dip),
767 ddi_get_instance(dip));
768
769 if (cmd == DDI_RESUME) {
770 /*
771 * Postponed until phase 2 of the development
772 */
773 return (DDI_FAILURE);
774 }
775
776 if (cmd != DDI_ATTACH) {
777 return (DDI_FAILURE);
778 }
779
780 /* cmd == DDI_ATTACH */
781
782 if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) {
783 SATA_LOG_D((NULL, CE_WARN,
784 "sata_hba_attach: invalid sata_hba_tran"));
785 return (DDI_FAILURE);
786 }
787 /*
788 * Allocate and initialize SCSI tran structure.
789 * SATA copy of tran_bus_config is provided to create port nodes.
790 */
791 scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
792 if (scsi_tran == NULL)
793 return (DDI_FAILURE);
794 /*
795 * Allocate soft structure for SATA HBA instance.
796 * There is a separate softstate for each HBA instance.
797 */
798 sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP);
799 ASSERT(sata_hba_inst != NULL); /* this should not fail */
800 mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL);
801 hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST;
802
803 /*
804 * scsi_trans's tran_hba_private is used by SATA Framework to point to
805 * soft structure allocated by SATA framework for
806 * SATA HBA instance related data.
807 */
808 scsi_tran->tran_hba_private = sata_hba_inst;
809 scsi_tran->tran_tgt_private = NULL;
810
811 scsi_tran->tran_tgt_init = sata_scsi_tgt_init;
812 scsi_tran->tran_tgt_probe = sata_scsi_tgt_probe;
813 scsi_tran->tran_tgt_free = sata_scsi_tgt_free;
814
815 scsi_tran->tran_start = sata_scsi_start;
816 scsi_tran->tran_reset = sata_scsi_reset;
817 scsi_tran->tran_abort = sata_scsi_abort;
818 scsi_tran->tran_getcap = sata_scsi_getcap;
819 scsi_tran->tran_setcap = sata_scsi_setcap;
820 scsi_tran->tran_init_pkt = sata_scsi_init_pkt;
821 scsi_tran->tran_destroy_pkt = sata_scsi_destroy_pkt;
822
823 scsi_tran->tran_dmafree = sata_scsi_dmafree;
824 scsi_tran->tran_sync_pkt = sata_scsi_sync_pkt;
825
826 scsi_tran->tran_reset_notify = NULL;
827 scsi_tran->tran_get_bus_addr = NULL;
828 scsi_tran->tran_quiesce = NULL;
829 scsi_tran->tran_unquiesce = NULL;
830 scsi_tran->tran_bus_reset = NULL;
831
832 if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr,
833 scsi_tran, 0) != DDI_SUCCESS) {
834 #ifdef SATA_DEBUG
835 cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed",
836 ddi_driver_name(dip), ddi_get_instance(dip));
837 #endif
838 goto fail;
839 }
840 hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED;
841
842 if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) {
843 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
844 "sata", 1) != DDI_PROP_SUCCESS) {
845 SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: "
846 "failed to create hba sata prop"));
847 goto fail;
848 }
849 }
850
851 /*
852 * Save pointers in hba instance soft state.
853 */
854 sata_hba_inst->satahba_scsi_tran = scsi_tran;
855 sata_hba_inst->satahba_tran = sata_tran;
856 sata_hba_inst->satahba_dip = dip;
857
858 /*
859 * Create a task queue to handle emulated commands completion
860 * Use node name, dash, instance number as the queue name.
861 */
862 taskq_name[0] = '\0';
863 (void) strlcat(taskq_name, DEVI(dip)->devi_node_name,
864 sizeof (taskq_name));
865 (void) snprintf(taskq_name + strlen(taskq_name),
866 sizeof (taskq_name) - strlen(taskq_name),
867 "-%d", DEVI(dip)->devi_instance);
868 sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1,
869 minclsyspri, 1, sata_tran->sata_tran_hba_num_cports * 4,
870 TASKQ_DYNAMIC);
871
872 hba_attach_state |= HBA_ATTACH_STAGE_SETUP;
873
874 /*
875 * Create events thread if not created yet.
876 */
877 sata_event_thread_control(1);
878
879 /*
880 * Link this hba instance into the list.
881 */
882 mutex_enter(&sata_mutex);
883
884 if (sata_hba_list == NULL) {
885 /*
886 * The first instance of HBA is attached.
887 * Set current/active default maximum NCQ/TCQ queue depth for
888 * all SATA devices. It is done here and now, to eliminate the
889 * possibility of the dynamic, programatic modification of the
890 * queue depth via global (and public) sata_max_queue_depth
891 * variable (this would require special handling in HBA drivers)
892 */
893 sata_current_max_qdepth = sata_max_queue_depth;
894 if (sata_current_max_qdepth > 32)
895 sata_current_max_qdepth = 32;
896 else if (sata_current_max_qdepth < 1)
897 sata_current_max_qdepth = 1;
898 }
899
900 sata_hba_inst->satahba_next = NULL;
901 sata_hba_inst->satahba_prev = sata_hba_list_tail;
902 if (sata_hba_list == NULL) {
903 sata_hba_list = sata_hba_inst;
904 }
905 if (sata_hba_list_tail != NULL) {
906 sata_hba_list_tail->satahba_next = sata_hba_inst;
907 }
908 sata_hba_list_tail = sata_hba_inst;
909 mutex_exit(&sata_mutex);
910 hba_attach_state |= HBA_ATTACH_STAGE_LINKED;
911
912 /*
913 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl
914 * SATA HBA driver should not use its own open/close entry points.
915 *
916 * Make sure that instance number doesn't overflow
917 * when forming minor numbers.
918 */
919 ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT));
920 if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
921 INST2DEVCTL(ddi_get_instance(dip)),
922 DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) {
923 #ifdef SATA_DEBUG
924 cmn_err(CE_WARN, "sata_hba_attach: "
925 "cannot create devctl minor node");
926 #endif
927 goto fail;
928 }
929
930
931 /*
932 * Set-up kstats here, if necessary.
933 * (postponed until future phase of the development).
934 */
935
936 /*
937 * Indicate that HBA is attached. This will enable events processing
938 * for this HBA.
939 */
940 sata_hba_inst->satahba_attached = 1;
941 /*
942 * Probe controller ports. This operation will describe a current
943 * controller/port/multipliers/device configuration and will create
944 * attachment points.
945 * We may end-up with just a controller with no devices attached.
946 * For the ports with a supported device attached, device target nodes
947 * are created and devices are initialized.
948 */
949 sata_probe_ports(sata_hba_inst);
950
951 return (DDI_SUCCESS);
952
953 fail:
954 if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) {
955 (void) sata_remove_hba_instance(dip);
956 if (sata_hba_list == NULL)
957 sata_event_thread_control(0);
958 }
959
960 if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) {
961 (void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
962 taskq_destroy(sata_hba_inst->satahba_taskq);
963 }
964
965 if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED)
966 (void) scsi_hba_detach(dip);
967
968 if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) {
969 mutex_destroy(&sata_hba_inst->satahba_mutex);
970 kmem_free((void *)sata_hba_inst,
971 sizeof (struct sata_hba_inst));
972 scsi_hba_tran_free(scsi_tran);
973 }
974
975 sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed",
976 ddi_driver_name(dip), ddi_get_instance(dip));
977
978 return (DDI_FAILURE);
979 }
980
981
982 /*
983 * Called by SATA HBA from to detach an instance of the driver.
984 *
985 * For DDI_DETACH command:
986 * Free local structures allocated for SATA HBA instance during
987 * sata_hba_attach processing.
988 *
989 * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise.
990 *
991 * For DDI_SUSPEND command:
992 * Not implemented at this time (postponed until phase 2 of the development)
993 * Returnd DDI_SUCCESS.
994 *
995 * When the last HBA instance is detached, the event daemon is terminated.
996 *
997 * NOTE: Port multiplier is supported.
998 */
999 int
sata_hba_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)1000 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
1001 {
1002 dev_info_t *tdip;
1003 sata_hba_inst_t *sata_hba_inst;
1004 scsi_hba_tran_t *scsi_hba_tran;
1005 sata_cport_info_t *cportinfo;
1006 sata_pmult_info_t *pminfo;
1007 sata_drive_info_t *sdinfo;
1008 sata_device_t sdevice;
1009 int ncport, npmport;
1010
1011 SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n",
1012 ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip));
1013
1014 switch (cmd) {
1015 case DDI_DETACH:
1016
1017 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1018 return (DDI_FAILURE);
1019
1020 sata_hba_inst = scsi_hba_tran->tran_hba_private;
1021 if (sata_hba_inst == NULL)
1022 return (DDI_FAILURE);
1023
1024 if (scsi_hba_detach(dip) == DDI_FAILURE) {
1025 sata_hba_inst->satahba_attached = 1;
1026 return (DDI_FAILURE);
1027 }
1028
1029 /*
1030 * Free all target nodes - at this point
1031 * devices should be at least offlined
1032 * otherwise scsi_hba_detach() should not be called.
1033 */
1034 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
1035 ncport++) {
1036 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
1037 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
1038 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
1039 if (sdinfo != NULL) {
1040 tdip = sata_get_target_dip(dip,
1041 ncport, 0);
1042 if (tdip != NULL) {
1043 if (ndi_devi_offline(tdip,
1044 NDI_DEVI_REMOVE) !=
1045 NDI_SUCCESS) {
1046 SATA_LOG_D((
1047 sata_hba_inst,
1048 CE_WARN,
1049 "sata_hba_detach: "
1050 "Target node not "
1051 "removed !"));
1052 return (DDI_FAILURE);
1053 }
1054 }
1055 }
1056 } else { /* SATA_DTYPE_PMULT */
1057 mutex_enter(&cportinfo->cport_mutex);
1058 pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
1059
1060 if (pminfo == NULL) {
1061 SATA_LOG_D((sata_hba_inst, CE_WARN,
1062 "sata_hba_detach: Port multiplier "
1063 "not ready yet!"));
1064 mutex_exit(&cportinfo->cport_mutex);
1065 return (DDI_FAILURE);
1066 }
1067
1068 /*
1069 * Detach would fail if removal of any of the
1070 * target nodes is failed - albeit in that
1071 * case some of them may have been removed.
1072 */
1073 for (npmport = 0; npmport < SATA_NUM_PMPORTS(
1074 sata_hba_inst, ncport); npmport++) {
1075 tdip = sata_get_target_dip(dip, ncport,
1076 npmport);
1077 if (tdip != NULL) {
1078 if (ndi_devi_offline(tdip,
1079 NDI_DEVI_REMOVE) !=
1080 NDI_SUCCESS) {
1081 SATA_LOG_D((
1082 sata_hba_inst,
1083 CE_WARN,
1084 "sata_hba_detach: "
1085 "Target node not "
1086 "removed !"));
1087 mutex_exit(&cportinfo->
1088 cport_mutex);
1089 return (DDI_FAILURE);
1090 }
1091 }
1092 }
1093 mutex_exit(&cportinfo->cport_mutex);
1094 }
1095 }
1096 /*
1097 * Disable sata event daemon processing for this HBA
1098 */
1099 sata_hba_inst->satahba_attached = 0;
1100
1101 /*
1102 * Remove event daemon thread, if it is last HBA instance.
1103 */
1104
1105 mutex_enter(&sata_mutex);
1106 if (sata_hba_list->satahba_next == NULL) {
1107 mutex_exit(&sata_mutex);
1108 sata_event_thread_control(0);
1109 mutex_enter(&sata_mutex);
1110 }
1111 mutex_exit(&sata_mutex);
1112
1113 /* Remove this HBA instance from the HBA list */
1114 sata_remove_hba_instance(dip);
1115
1116 /*
1117 * At this point there should be no target nodes attached.
1118 * Detach and destroy device and port info structures.
1119 */
1120 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
1121 ncport++) {
1122 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
1123 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
1124 sdinfo =
1125 cportinfo->cport_devp.cport_sata_drive;
1126 if (sdinfo != NULL) {
1127 /* Release device structure */
1128 kmem_free(sdinfo,
1129 sizeof (sata_drive_info_t));
1130 }
1131 /* Release cport info */
1132 mutex_destroy(&cportinfo->cport_mutex);
1133 kmem_free(cportinfo,
1134 sizeof (sata_cport_info_t));
1135 } else { /* SATA_DTYPE_PMULT */
1136 sdevice.satadev_addr.cport = (uint8_t)ncport;
1137 sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
1138 sata_free_pmult(sata_hba_inst, &sdevice);
1139 }
1140 }
1141
1142 scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran);
1143
1144 (void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
1145
1146 taskq_destroy(sata_hba_inst->satahba_taskq);
1147
1148 mutex_destroy(&sata_hba_inst->satahba_mutex);
1149 kmem_free((void *)sata_hba_inst,
1150 sizeof (struct sata_hba_inst));
1151
1152 return (DDI_SUCCESS);
1153
1154 case DDI_SUSPEND:
1155 /*
1156 * Postponed until phase 2
1157 */
1158 return (DDI_FAILURE);
1159
1160 default:
1161 return (DDI_FAILURE);
1162 }
1163 }
1164
1165
1166 /*
1167 * Called by an HBA drive from _fini() routine.
1168 * Unregisters SATA HBA instance/SATA framework pair from the scsi framework.
1169 */
1170 void
sata_hba_fini(struct modlinkage * modlp)1171 sata_hba_fini(struct modlinkage *modlp)
1172 {
1173 SATADBG1(SATA_DBG_HBA_IF, NULL,
1174 "sata_hba_fini: name %s\n",
1175 ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
1176
1177 scsi_hba_fini(modlp);
1178 }
1179
1180
1181 /*
1182 * Default open and close routine for sata_hba framework.
1183 *
1184 */
1185 /*
1186 * Open devctl node.
1187 *
1188 * Returns:
1189 * 0 if node was open successfully, error code otherwise.
1190 *
1191 *
1192 */
1193
1194 static int
sata_hba_open(dev_t * devp,int flags,int otyp,cred_t * credp)1195 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1196 {
1197 #ifndef __lock_lint
1198 _NOTE(ARGUNUSED(credp))
1199 #endif
1200 int rv = 0;
1201 dev_info_t *dip;
1202 scsi_hba_tran_t *scsi_hba_tran;
1203 sata_hba_inst_t *sata_hba_inst;
1204
1205 SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL);
1206
1207 if (otyp != OTYP_CHR)
1208 return (EINVAL);
1209
1210 dip = sata_devt_to_devinfo(*devp);
1211 if (dip == NULL)
1212 return (ENXIO);
1213
1214 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1215 return (ENXIO);
1216
1217 sata_hba_inst = scsi_hba_tran->tran_hba_private;
1218 if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1219 return (ENXIO);
1220
1221 mutex_enter(&sata_mutex);
1222 if (flags & FEXCL) {
1223 if (sata_hba_inst->satahba_open_flag != 0) {
1224 rv = EBUSY;
1225 } else {
1226 sata_hba_inst->satahba_open_flag =
1227 SATA_DEVCTL_EXOPENED;
1228 }
1229 } else {
1230 if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) {
1231 rv = EBUSY;
1232 } else {
1233 sata_hba_inst->satahba_open_flag =
1234 SATA_DEVCTL_SOPENED;
1235 }
1236 }
1237 mutex_exit(&sata_mutex);
1238
1239 return (rv);
1240 }
1241
1242
1243 /*
1244 * Close devctl node.
1245 * Returns:
1246 * 0 if node was closed successfully, error code otherwise.
1247 *
1248 */
1249
1250 static int
sata_hba_close(dev_t dev,int flag,int otyp,cred_t * credp)1251 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
1252 {
1253 #ifndef __lock_lint
1254 _NOTE(ARGUNUSED(credp))
1255 _NOTE(ARGUNUSED(flag))
1256 #endif
1257 dev_info_t *dip;
1258 scsi_hba_tran_t *scsi_hba_tran;
1259 sata_hba_inst_t *sata_hba_inst;
1260
1261 SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL);
1262
1263 if (otyp != OTYP_CHR)
1264 return (EINVAL);
1265
1266 dip = sata_devt_to_devinfo(dev);
1267 if (dip == NULL)
1268 return (ENXIO);
1269
1270 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1271 return (ENXIO);
1272
1273 sata_hba_inst = scsi_hba_tran->tran_hba_private;
1274 if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1275 return (ENXIO);
1276
1277 mutex_enter(&sata_mutex);
1278 sata_hba_inst->satahba_open_flag = 0;
1279 mutex_exit(&sata_mutex);
1280 return (0);
1281 }
1282
1283
1284
1285 /*
1286 * Standard IOCTL commands for SATA hotplugging.
1287 * Implemented DEVCTL_AP commands:
1288 * DEVCTL_AP_CONNECT
1289 * DEVCTL_AP_DISCONNECT
1290 * DEVCTL_AP_CONFIGURE
1291 * DEVCTL_UNCONFIGURE
1292 * DEVCTL_AP_CONTROL
1293 *
1294 * Commands passed to default ndi ioctl handler:
1295 * DEVCTL_DEVICE_GETSTATE
1296 * DEVCTL_DEVICE_ONLINE
1297 * DEVCTL_DEVICE_OFFLINE
1298 * DEVCTL_DEVICE_REMOVE
1299 * DEVCTL_DEVICE_INSERT
1300 * DEVCTL_BUS_GETSTATE
1301 *
1302 * All other cmds are passed to HBA if it provide ioctl handler, or failed
1303 * if not.
1304 *
1305 * Returns:
1306 * 0 if successful,
1307 * error code if operation failed.
1308 *
1309 * Port Multiplier support is supported now.
1310 *
1311 * NOTE: qual should be SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT
1312 */
1313
1314 static int
sata_hba_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)1315 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1316 int *rvalp)
1317 {
1318 #ifndef __lock_lint
1319 _NOTE(ARGUNUSED(credp))
1320 _NOTE(ARGUNUSED(rvalp))
1321 #endif
1322 int rv = 0;
1323 int32_t comp_port = -1;
1324 dev_info_t *dip;
1325 devctl_ap_state_t ap_state;
1326 struct devctl_iocdata *dcp = NULL;
1327 scsi_hba_tran_t *scsi_hba_tran;
1328 sata_hba_inst_t *sata_hba_inst;
1329 sata_device_t sata_device;
1330 sata_cport_info_t *cportinfo;
1331 int cport, pmport, qual;
1332 int rval = SATA_SUCCESS;
1333
1334 dip = sata_devt_to_devinfo(dev);
1335 if (dip == NULL)
1336 return (ENXIO);
1337
1338 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1339 return (ENXIO);
1340
1341 sata_hba_inst = scsi_hba_tran->tran_hba_private;
1342 if (sata_hba_inst == NULL)
1343 return (ENXIO);
1344
1345 if (sata_hba_inst->satahba_tran == NULL)
1346 return (ENXIO);
1347
1348 switch (cmd) {
1349
1350 case DEVCTL_DEVICE_GETSTATE:
1351 case DEVCTL_DEVICE_ONLINE:
1352 case DEVCTL_DEVICE_OFFLINE:
1353 case DEVCTL_DEVICE_REMOVE:
1354 case DEVCTL_BUS_GETSTATE:
1355 /*
1356 * There may be more cases that we want to pass to default
1357 * handler rather than fail them.
1358 */
1359 return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
1360 }
1361
1362 cport = pmport = qual = 0;
1363 cportinfo = NULL;
1364
1365 /* read devctl ioctl data */
1366 if (cmd != DEVCTL_AP_CONTROL && IS_DEVCTL(cmd)) {
1367 if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1368 return (EFAULT);
1369
1370 if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) ==
1371 -1) {
1372 if (dcp)
1373 ndi_dc_freehdl(dcp);
1374 return (EINVAL);
1375 }
1376
1377 /*
1378 * According to SCSI_TO_SATA_ADDR_QUAL, qual should be either
1379 * SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT.
1380 */
1381 cport = SCSI_TO_SATA_CPORT(comp_port);
1382 pmport = SCSI_TO_SATA_PMPORT(comp_port);
1383 qual = SCSI_TO_SATA_ADDR_QUAL(comp_port);
1384
1385 if (sata_validate_sata_address(sata_hba_inst, cport, pmport,
1386 qual) != 0) {
1387 ndi_dc_freehdl(dcp);
1388 return (EINVAL);
1389 }
1390
1391 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1392 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1393 cport_mutex);
1394 if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1395 /*
1396 * Cannot process ioctl request now. Come back later.
1397 */
1398 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1399 cport_mutex);
1400 ndi_dc_freehdl(dcp);
1401 return (EBUSY);
1402 }
1403 /* Block event processing for this port */
1404 cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1405 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1406
1407 sata_device.satadev_addr.cport = cport;
1408 sata_device.satadev_addr.pmport = pmport;
1409 sata_device.satadev_addr.qual = qual;
1410 sata_device.satadev_rev = SATA_DEVICE_REV;
1411 }
1412
1413 switch (cmd) {
1414
1415 case DEVCTL_AP_DISCONNECT:
1416
1417 /*
1418 * Normally, cfgadm sata plugin will try to offline
1419 * (unconfigure) device before this request. Nevertheless,
1420 * if a device is still configured, we need to
1421 * attempt to offline and unconfigure device first, and we will
1422 * deactivate the port regardless of the unconfigure
1423 * operation results.
1424 *
1425 */
1426 rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device);
1427
1428 break;
1429
1430 case DEVCTL_AP_UNCONFIGURE:
1431
1432 /*
1433 * The unconfigure operation uses generic nexus operation to
1434 * offline a device. It leaves a target device node attached.
1435 * and obviously sata_drive_info attached as well, because
1436 * from the hardware point of view nothing has changed.
1437 */
1438 rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device);
1439 break;
1440
1441 case DEVCTL_AP_CONNECT:
1442 {
1443 /*
1444 * The sata cfgadm pluging will invoke this operation only if
1445 * port was found in the disconnect state (failed state
1446 * is also treated as the disconnected state).
1447 * If port activation is successful and a device is found
1448 * attached to the port, the initialization sequence is
1449 * executed to probe the port and attach
1450 * a device structure to a port structure. The device is not
1451 * set in configured state (system-wise) by this operation.
1452 */
1453
1454 rv = sata_ioctl_connect(sata_hba_inst, &sata_device);
1455
1456 break;
1457 }
1458
1459 case DEVCTL_AP_CONFIGURE:
1460 {
1461 /*
1462 * A port may be in an active or shutdown state.
1463 * If port is in a failed state, operation is aborted.
1464 * If a port is in a shutdown state, sata_tran_port_activate()
1465 * is invoked prior to any other operation.
1466 *
1467 * Onlining the device involves creating a new target node.
1468 * If there is an old target node present (belonging to
1469 * previously removed device), the operation is aborted - the
1470 * old node has to be released and removed before configure
1471 * operation is attempted.
1472 */
1473
1474 rv = sata_ioctl_configure(sata_hba_inst, &sata_device);
1475
1476 break;
1477 }
1478
1479 case DEVCTL_AP_GETSTATE:
1480
1481 sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state);
1482
1483 ap_state.ap_last_change = (time_t)-1;
1484 ap_state.ap_error_code = 0;
1485 ap_state.ap_in_transition = 0;
1486
1487 /* Copy the return AP-state information to the user space */
1488 if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
1489 rv = EFAULT;
1490 }
1491 break;
1492
1493 case DEVCTL_AP_CONTROL:
1494 {
1495 /*
1496 * Generic devctl for hardware specific functionality
1497 */
1498 sata_ioctl_data_t ioc;
1499
1500 ASSERT(dcp == NULL);
1501
1502 /* Copy in user ioctl data first */
1503 #ifdef _MULTI_DATAMODEL
1504 if (ddi_model_convert_from(mode & FMODELS) ==
1505 DDI_MODEL_ILP32) {
1506
1507 sata_ioctl_data_32_t ioc32;
1508
1509 if (ddi_copyin((void *)arg, (void *)&ioc32,
1510 sizeof (ioc32), mode) != 0) {
1511 rv = EFAULT;
1512 break;
1513 }
1514 ioc.cmd = (uint_t)ioc32.cmd;
1515 ioc.port = (uint_t)ioc32.port;
1516 ioc.get_size = (uint_t)ioc32.get_size;
1517 ioc.buf = (caddr_t)(uintptr_t)ioc32.buf;
1518 ioc.bufsiz = (uint_t)ioc32.bufsiz;
1519 ioc.misc_arg = (uint_t)ioc32.misc_arg;
1520 } else
1521 #endif /* _MULTI_DATAMODEL */
1522 if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
1523 mode) != 0) {
1524 return (EFAULT);
1525 }
1526
1527 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
1528 "sata_hba_ioctl: DEVCTL_AP_CONTROL "
1529 "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port);
1530
1531 /*
1532 * To avoid BE/LE and 32/64 issues, a get_size always returns
1533 * a 32-bit number.
1534 */
1535 if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
1536 return (EINVAL);
1537 }
1538 /* validate address */
1539 cport = SCSI_TO_SATA_CPORT(ioc.port);
1540 pmport = SCSI_TO_SATA_PMPORT(ioc.port);
1541 qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port);
1542
1543 SATADBG3(SATA_DBG_IOCTL_IF, sata_hba_inst,
1544 "sata_hba_ioctl: target port is %d:%d (%d)",
1545 cport, pmport, qual);
1546
1547 if (sata_validate_sata_address(sata_hba_inst, cport,
1548 pmport, qual) != 0)
1549 return (EINVAL);
1550
1551 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1552 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1553 cport_mutex);
1554 /* Is the port locked by event processing daemon ? */
1555 if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1556 /*
1557 * Cannot process ioctl request now. Come back later
1558 */
1559 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1560 cport_mutex);
1561 return (EBUSY);
1562 }
1563 /* Block event processing for this port */
1564 cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1565 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1566
1567
1568 sata_device.satadev_addr.cport = cport;
1569 sata_device.satadev_addr.pmport = pmport;
1570 sata_device.satadev_addr.qual = qual;
1571 sata_device.satadev_rev = SATA_DEVICE_REV;
1572
1573 switch (ioc.cmd) {
1574
1575 case SATA_CFGA_RESET_PORT:
1576 /*
1577 * There is no protection for configured device.
1578 */
1579 rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device);
1580 break;
1581
1582 case SATA_CFGA_RESET_DEVICE:
1583 /*
1584 * There is no protection for configured device.
1585 */
1586 rv = sata_ioctl_reset_device(sata_hba_inst,
1587 &sata_device);
1588 break;
1589
1590 case SATA_CFGA_RESET_ALL:
1591 /*
1592 * There is no protection for configured devices.
1593 */
1594 rv = sata_ioctl_reset_all(sata_hba_inst);
1595 /*
1596 * We return here, because common return is for
1597 * a single port operation - we have already unlocked
1598 * all ports and no dc handle was allocated.
1599 */
1600 return (rv);
1601
1602 case SATA_CFGA_PORT_DEACTIVATE:
1603 /*
1604 * Arbitrarily unconfigure attached device, if any.
1605 * Even if the unconfigure fails, proceed with the
1606 * port deactivation.
1607 */
1608 rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device);
1609
1610 break;
1611
1612 case SATA_CFGA_PORT_ACTIVATE:
1613
1614 rv = sata_ioctl_activate(sata_hba_inst, &sata_device);
1615 break;
1616
1617 case SATA_CFGA_PORT_SELF_TEST:
1618
1619 rv = sata_ioctl_port_self_test(sata_hba_inst,
1620 &sata_device);
1621 break;
1622
1623 case SATA_CFGA_GET_DEVICE_PATH:
1624
1625 rv = sata_ioctl_get_device_path(sata_hba_inst,
1626 &sata_device, &ioc, mode);
1627 break;
1628
1629 case SATA_CFGA_GET_AP_TYPE:
1630
1631 rv = sata_ioctl_get_ap_type(sata_hba_inst,
1632 &sata_device, &ioc, mode);
1633 break;
1634
1635 case SATA_CFGA_GET_MODEL_INFO:
1636
1637 rv = sata_ioctl_get_model_info(sata_hba_inst,
1638 &sata_device, &ioc, mode);
1639 break;
1640
1641 case SATA_CFGA_GET_REVFIRMWARE_INFO:
1642
1643 rv = sata_ioctl_get_revfirmware_info(sata_hba_inst,
1644 &sata_device, &ioc, mode);
1645 break;
1646
1647 case SATA_CFGA_GET_SERIALNUMBER_INFO:
1648
1649 rv = sata_ioctl_get_serialnumber_info(sata_hba_inst,
1650 &sata_device, &ioc, mode);
1651 break;
1652
1653 default:
1654 rv = EINVAL;
1655 break;
1656
1657 } /* End of DEVCTL_AP_CONTROL cmd switch */
1658
1659 break;
1660 }
1661
1662 default:
1663 {
1664 /*
1665 * If we got here, we got an IOCTL that SATA HBA Framework
1666 * does not recognize. Pass ioctl to HBA driver, in case
1667 * it could process it.
1668 */
1669 sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran;
1670 dev_info_t *mydip = SATA_DIP(sata_hba_inst);
1671
1672 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1673 "IOCTL 0x%2x not supported in SATA framework, "
1674 "passthrough to HBA", cmd);
1675
1676 if (sata_tran->sata_tran_ioctl == NULL) {
1677 rv = EINVAL;
1678 break;
1679 }
1680 rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg);
1681 if (rval != 0) {
1682 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1683 "IOCTL 0x%2x failed in HBA", cmd);
1684 rv = rval;
1685 }
1686 break;
1687 }
1688
1689 } /* End of main IOCTL switch */
1690
1691 if (dcp) {
1692 ndi_dc_freehdl(dcp);
1693 }
1694
1695 if (IS_DEVCTL(cmd)) {
1696 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
1697 cport)->cport_mutex);
1698 cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
1699 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1700 }
1701
1702 return (rv);
1703 }
1704
1705
1706 /*
1707 * Create error retrieval sata packet
1708 *
1709 * A sata packet is allocated and set-up to contain specified error retrieval
1710 * command and appropriate dma-able data buffer.
1711 * No association with any scsi packet is made and no callback routine is
1712 * specified.
1713 *
1714 * Returns a pointer to sata packet upon successful packet creation.
1715 * Returns NULL, if packet cannot be created.
1716 */
1717 sata_pkt_t *
sata_get_error_retrieval_pkt(dev_info_t * dip,sata_device_t * sata_device,int pkt_type)1718 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device,
1719 int pkt_type)
1720 {
1721 sata_hba_inst_t *sata_hba_inst;
1722 sata_pkt_txlate_t *spx;
1723 sata_pkt_t *spkt;
1724 sata_drive_info_t *sdinfo;
1725
1726 mutex_enter(&sata_mutex);
1727 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1728 sata_hba_inst = sata_hba_inst->satahba_next) {
1729 if (SATA_DIP(sata_hba_inst) == dip)
1730 break;
1731 }
1732 mutex_exit(&sata_mutex);
1733 ASSERT(sata_hba_inst != NULL);
1734
1735 sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
1736 if (sdinfo == NULL) {
1737 sata_log(sata_hba_inst, CE_WARN,
1738 "sata: error recovery request for non-attached device at "
1739 "cport %d", sata_device->satadev_addr.cport);
1740 return (NULL);
1741 }
1742
1743 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1744 spx->txlt_sata_hba_inst = sata_hba_inst;
1745 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */
1746 spkt = sata_pkt_alloc(spx, NULL);
1747 if (spkt == NULL) {
1748 kmem_free(spx, sizeof (sata_pkt_txlate_t));
1749 return (NULL);
1750 }
1751 /* address is needed now */
1752 spkt->satapkt_device.satadev_addr = sata_device->satadev_addr;
1753
1754 switch (pkt_type) {
1755 case SATA_ERR_RETR_PKT_TYPE_NCQ:
1756 if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) {
1757 if (sata_check_for_dma_error(dip, spx)) {
1758 ddi_fm_service_impact(dip,
1759 DDI_SERVICE_UNAFFECTED);
1760 break;
1761 }
1762 return (spkt);
1763 }
1764 break;
1765
1766 case SATA_ERR_RETR_PKT_TYPE_ATAPI:
1767 if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) {
1768 if (sata_check_for_dma_error(dip, spx)) {
1769 ddi_fm_service_impact(dip,
1770 DDI_SERVICE_UNAFFECTED);
1771 break;
1772 }
1773 return (spkt);
1774 }
1775 break;
1776
1777 default:
1778 break;
1779 }
1780
1781 sata_pkt_free(spx);
1782 kmem_free(spx, sizeof (sata_pkt_txlate_t));
1783 return (NULL);
1784
1785 }
1786
1787
1788 /*
1789 * Free error retrieval sata packet
1790 *
1791 * Free sata packet and any associated resources allocated previously by
1792 * sata_get_error_retrieval_pkt().
1793 *
1794 * Void return.
1795 */
1796 void
sata_free_error_retrieval_pkt(sata_pkt_t * sata_pkt)1797 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt)
1798 {
1799 sata_pkt_txlate_t *spx =
1800 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1801
1802 ASSERT(sata_pkt != NULL);
1803
1804 sata_free_local_buffer(spx);
1805 sata_pkt_free(spx);
1806 kmem_free(spx, sizeof (sata_pkt_txlate_t));
1807
1808 }
1809
1810 /*
1811 * Create READ PORT MULTIPLIER and WRITE PORT MULTIPLIER sata packet
1812 *
1813 * No association with any scsi packet is made and no callback routine is
1814 * specified.
1815 *
1816 * Returns a pointer to sata packet upon successful packet creation.
1817 * Returns NULL, if packet cannot be created.
1818 *
1819 * NOTE: Input/Output value includes 64 bits accoring to SATA Spec 2.6,
1820 * only lower 32 bits are available currently.
1821 */
1822 sata_pkt_t *
sata_get_rdwr_pmult_pkt(dev_info_t * dip,sata_device_t * sd,uint16_t regn,uint32_t regv,uint32_t type)1823 sata_get_rdwr_pmult_pkt(dev_info_t *dip, sata_device_t *sd,
1824 uint16_t regn, uint32_t regv, uint32_t type)
1825 {
1826 sata_hba_inst_t *sata_hba_inst;
1827 sata_pkt_txlate_t *spx;
1828 sata_pkt_t *spkt;
1829 sata_cmd_t *scmd;
1830
1831 /* Only READ/WRITE commands are accepted. */
1832 ASSERT(type == SATA_RDWR_PMULT_PKT_TYPE_READ ||
1833 type == SATA_RDWR_PMULT_PKT_TYPE_WRITE);
1834
1835 mutex_enter(&sata_mutex);
1836 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1837 sata_hba_inst = sata_hba_inst->satahba_next) {
1838 if (SATA_DIP(sata_hba_inst) == dip)
1839 break;
1840 }
1841 mutex_exit(&sata_mutex);
1842 ASSERT(sata_hba_inst != NULL);
1843
1844 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1845 spx->txlt_sata_hba_inst = sata_hba_inst;
1846 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */
1847 spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
1848 if (spkt == NULL) {
1849 kmem_free(spx, sizeof (sata_pkt_txlate_t));
1850 return (NULL);
1851 }
1852
1853 /*
1854 * NOTE: We need to send this command to the port multiplier,
1855 * that means send to SATA_PMULT_HOSTPORT(0xf) pmport
1856 *
1857 * sata_device contains the address of actual target device, and the
1858 * pmport number in the command comes from the sata_device structure.
1859 */
1860 spkt->satapkt_device.satadev_addr = sd->satadev_addr;
1861 spkt->satapkt_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
1862 spkt->satapkt_device.satadev_addr.qual = SATA_ADDR_PMULT;
1863
1864 /* Fill sata_pkt */
1865 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_POLLING;
1866 spkt->satapkt_comp = NULL; /* Synchronous mode, no callback */
1867 spkt->satapkt_time = 10; /* Timeout 10s */
1868
1869 /* Build READ PORT MULTIPLIER cmd in the sata_pkt */
1870 scmd = &spkt->satapkt_cmd;
1871 scmd->satacmd_features_reg = regn & 0xff;
1872 scmd->satacmd_features_reg_ext = (regn >> 8) & 0xff;
1873 scmd->satacmd_device_reg = sd->satadev_addr.pmport;
1874 scmd->satacmd_addr_type = 0; /* N/A */
1875
1876 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
1877
1878 if (type == SATA_RDWR_PMULT_PKT_TYPE_READ) {
1879 scmd->satacmd_cmd_reg = SATAC_READ_PORTMULT;
1880 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
1881 scmd->satacmd_flags.sata_special_regs = 1;
1882 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1;
1883 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1;
1884 scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1;
1885 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1;
1886 } else if (type == SATA_RDWR_PMULT_PKT_TYPE_WRITE) {
1887 scmd->satacmd_cmd_reg = SATAC_WRITE_PORTMULT;
1888 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
1889 scmd->satacmd_sec_count_lsb = regv & 0xff;
1890 scmd->satacmd_lba_low_lsb = regv >> 8 & 0xff;
1891 scmd->satacmd_lba_mid_lsb = regv >> 16 & 0xff;
1892 scmd->satacmd_lba_high_lsb = regv >> 24 & 0xff;
1893 }
1894
1895 return (spkt);
1896 }
1897
1898 /*
1899 * Free sata packet and any associated resources allocated previously by
1900 * sata_get_rdwr_pmult_pkt().
1901 *
1902 * Void return.
1903 */
1904 void
sata_free_rdwr_pmult_pkt(sata_pkt_t * sata_pkt)1905 sata_free_rdwr_pmult_pkt(sata_pkt_t *sata_pkt)
1906 {
1907 sata_pkt_txlate_t *spx =
1908 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1909
1910 /* Free allocated resources */
1911 sata_pkt_free(spx);
1912 kmem_free(spx, sizeof (sata_pkt_txlate_t));
1913 }
1914
1915 /*
1916 * Register a port multiplier to framework.
1917 * 1) Store the GSCR values in the previous allocated pmult_info strctures.
1918 * 2) Search in the blacklist and update the number of the device ports of the
1919 * port multiplier.
1920 *
1921 * Void return.
1922 */
1923 void
sata_register_pmult(dev_info_t * dip,sata_device_t * sd,sata_pmult_gscr_t * sg)1924 sata_register_pmult(dev_info_t *dip, sata_device_t *sd, sata_pmult_gscr_t *sg)
1925 {
1926 sata_hba_inst_t *sata_hba_inst = NULL;
1927 sata_pmult_info_t *pmultinfo;
1928 sata_pmult_bl_t *blp;
1929 int cport = sd->satadev_addr.cport;
1930
1931 mutex_enter(&sata_mutex);
1932 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1933 sata_hba_inst = sata_hba_inst->satahba_next) {
1934 if (SATA_DIP(sata_hba_inst) == dip)
1935 if (sata_hba_inst->satahba_attached == 1)
1936 break;
1937 }
1938 mutex_exit(&sata_mutex);
1939 /* HBA not attached? */
1940 if (sata_hba_inst == NULL)
1941 return;
1942
1943 /* Number of pmports */
1944 sd->satadev_add_info = sg->gscr2 & SATA_PMULT_PORTNUM_MASK;
1945
1946 /* Check the blacklist */
1947 for (blp = sata_pmult_blacklist; blp->bl_gscr0; blp++) {
1948 if (sg->gscr0 != blp->bl_gscr0 && blp->bl_gscr0)
1949 continue;
1950 if (sg->gscr1 != blp->bl_gscr1 && blp->bl_gscr1)
1951 continue;
1952 if (sg->gscr2 != blp->bl_gscr2 && blp->bl_gscr2)
1953 continue;
1954
1955 cmn_err(CE_WARN, "!Port multiplier is on the blacklist.");
1956 sd->satadev_add_info = blp->bl_flags;
1957 break;
1958 }
1959
1960 /* Register the port multiplier GSCR */
1961 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
1962 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
1963 if (pmultinfo != NULL) {
1964 pmultinfo->pmult_gscr = *sg;
1965 pmultinfo->pmult_num_dev_ports =
1966 sd->satadev_add_info & SATA_PMULT_PORTNUM_MASK;
1967 SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
1968 "Port multiplier registered at port %d", cport);
1969 }
1970 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
1971 }
1972
1973 /*
1974 * sata_split_model splits the model ID into vendor and product IDs.
1975 * It assumes that a vendor ID cannot be longer than 8 characters, and
1976 * that vendor and product ID are separated by a whitespace.
1977 */
1978 void
sata_split_model(char * model,char ** vendor,char ** product)1979 sata_split_model(char *model, char **vendor, char **product)
1980 {
1981 int i, modlen;
1982 char *vid, *pid;
1983
1984 /*
1985 * remove whitespace at the end of model
1986 */
1987 for (i = SATA_ID_MODEL_LEN; i > 0; i--)
1988 if (model[i] == ' ' || model[i] == '\t' || model[i] == '\0')
1989 model[i] = '\0';
1990 else
1991 break;
1992
1993 /*
1994 * try to split model into into vid/pid
1995 */
1996 modlen = strlen(model);
1997 for (i = 0, pid = model; i < modlen; i++, pid++)
1998 if ((*pid == ' ') || (*pid == '\t'))
1999 break;
2000
2001 /*
2002 * only use vid if it is less than 8 chars (as in SCSI)
2003 */
2004 if (i < modlen && i <= 8) {
2005 vid = model;
2006 /*
2007 * terminate vid, establish pid
2008 */
2009 *pid++ = '\0';
2010 } else {
2011 /*
2012 * vid will stay "ATA "
2013 */
2014 vid = NULL;
2015 /*
2016 * model is all pid
2017 */
2018 pid = model;
2019 }
2020
2021 *vendor = vid;
2022 *product = pid;
2023 }
2024
2025 /*
2026 * sata_name_child is for composing the name of the node
2027 * the format of the name is "target,0".
2028 */
2029 static int
sata_name_child(dev_info_t * dip,char * name,int namelen)2030 sata_name_child(dev_info_t *dip, char *name, int namelen)
2031 {
2032 int target;
2033
2034 target = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
2035 DDI_PROP_DONTPASS, "target", -1);
2036 if (target == -1)
2037 return (DDI_FAILURE);
2038 (void) snprintf(name, namelen, "%x,0", target);
2039 return (DDI_SUCCESS);
2040 }
2041
2042
2043
2044 /* ****************** SCSA required entry points *********************** */
2045
2046 /*
2047 * Implementation of scsi tran_tgt_init.
2048 * sata_scsi_tgt_init() initializes scsi_device structure
2049 *
2050 * If successful, DDI_SUCCESS is returned.
2051 * DDI_FAILURE is returned if addressed device does not exist
2052 */
2053
2054 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)2055 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2056 scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2057 {
2058 #ifndef __lock_lint
2059 _NOTE(ARGUNUSED(hba_dip))
2060 _NOTE(ARGUNUSED(tgt_dip))
2061 #endif
2062 sata_device_t sata_device;
2063 sata_drive_info_t *sdinfo;
2064 struct sata_id *sid;
2065 sata_hba_inst_t *sata_hba_inst;
2066 char model[SATA_ID_MODEL_LEN + 1];
2067 char fw[SATA_ID_FW_LEN + 1];
2068 char *vid, *pid;
2069
2070 /*
2071 * Fail tran_tgt_init for .conf stub node
2072 */
2073 if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
2074 (void) ndi_merge_node(tgt_dip, sata_name_child);
2075 ddi_set_name_addr(tgt_dip, NULL);
2076 return (DDI_FAILURE);
2077 }
2078
2079 sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2080
2081 /* Validate scsi device address */
2082 if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2083 &sata_device) != 0)
2084 return (DDI_FAILURE);
2085
2086 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2087 sata_device.satadev_addr.cport)));
2088
2089 /* sata_device now contains a valid sata address */
2090 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2091 if (sdinfo == NULL) {
2092 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2093 sata_device.satadev_addr.cport)));
2094 return (DDI_FAILURE);
2095 }
2096 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2097 sata_device.satadev_addr.cport)));
2098
2099 /*
2100 * Check if we need to create a legacy devid (i.e cmdk style) for
2101 * the target disks.
2102 *
2103 * HBA devinfo node will have the property "use-cmdk-devid-format"
2104 * if we need to create cmdk-style devid for all the disk devices
2105 * attached to this controller. This property may have been set
2106 * from HBA driver's .conf file or by the HBA driver in its
2107 * attach(9F) function.
2108 */
2109 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2110 (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2111 "use-cmdk-devid-format", 0) == 1)) {
2112 /* register a legacy devid for this target node */
2113 sata_target_devid_register(tgt_dip, sdinfo);
2114 }
2115
2116
2117 /*
2118 * 'Identify Device Data' does not always fit in standard SCSI
2119 * INQUIRY data, so establish INQUIRY_* properties with full-form
2120 * of information.
2121 */
2122 sid = &sdinfo->satadrv_id;
2123 #ifdef _LITTLE_ENDIAN
2124 swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
2125 swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
2126 #else /* _LITTLE_ENDIAN */
2127 bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN);
2128 bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN);
2129 #endif /* _LITTLE_ENDIAN */
2130 model[SATA_ID_MODEL_LEN] = 0;
2131 fw[SATA_ID_FW_LEN] = 0;
2132
2133 sata_split_model(model, &vid, &pid);
2134
2135 if (vid)
2136 (void) scsi_device_prop_update_inqstring(sd, INQUIRY_VENDOR_ID,
2137 vid, strlen(vid));
2138 if (pid)
2139 (void) scsi_device_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID,
2140 pid, strlen(pid));
2141 (void) scsi_device_prop_update_inqstring(sd, INQUIRY_REVISION_ID,
2142 fw, strlen(fw));
2143
2144 return (DDI_SUCCESS);
2145 }
2146
2147 /*
2148 * Implementation of scsi tran_tgt_probe.
2149 * Probe target, by calling default scsi routine scsi_hba_probe()
2150 */
2151 static int
sata_scsi_tgt_probe(struct scsi_device * sd,int (* callback)(void))2152 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void))
2153 {
2154 sata_hba_inst_t *sata_hba_inst =
2155 (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private);
2156 int rval;
2157 uint32_t pm_cap;
2158
2159 rval = scsi_hba_probe(sd, callback);
2160 pm_cap = SATA_CAP_POWER_CONDITON | SATA_CAP_SMART_PAGE |
2161 SATA_CAP_LOG_SENSE;
2162
2163 if (rval == SCSIPROBE_EXISTS) {
2164 /*
2165 * Set property "pm-capable" on the target device node, so that
2166 * the target driver will not try to fetch scsi cycle counters
2167 * before enabling device power-management.
2168 */
2169 if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
2170 "pm-capable", pm_cap)) != DDI_PROP_SUCCESS) {
2171 sata_log(sata_hba_inst, CE_WARN,
2172 "SATA device at port %d: "
2173 "will not be power-managed ",
2174 SCSI_TO_SATA_CPORT(sd->sd_address.a_target));
2175 SATA_LOG_D((sata_hba_inst, CE_WARN,
2176 "failure updating pm-capable property"));
2177 }
2178 }
2179 return (rval);
2180 }
2181
2182 /*
2183 * Implementation of scsi tran_tgt_free.
2184 * Release all resources allocated for scsi_device
2185 */
2186 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)2187 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2188 scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2189 {
2190 #ifndef __lock_lint
2191 _NOTE(ARGUNUSED(hba_dip))
2192 #endif
2193 sata_device_t sata_device;
2194 sata_drive_info_t *sdinfo;
2195 sata_hba_inst_t *sata_hba_inst;
2196 ddi_devid_t devid;
2197
2198 sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2199
2200 /* Validate scsi device address */
2201 /*
2202 * Note: tgt_free relates to the SCSA view of a device. If called, there
2203 * was a device at this address, so even if the sata framework internal
2204 * resources were alredy released because a device was detached,
2205 * this function should be executed as long as its actions do
2206 * not require the internal sata view of a device and the address
2207 * refers to a valid sata address.
2208 * Validating the address here means that we do not trust SCSA...
2209 */
2210 if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2211 &sata_device) == -1)
2212 return;
2213
2214 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2215 sata_device.satadev_addr.cport)));
2216
2217 /* sata_device now should contain a valid sata address */
2218 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2219 if (sdinfo == NULL) {
2220 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2221 sata_device.satadev_addr.cport)));
2222 return;
2223 }
2224 /*
2225 * We did not allocate any resources in sata_scsi_tgt_init()
2226 * other than few properties.
2227 * Free them.
2228 */
2229 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2230 sata_device.satadev_addr.cport)));
2231 (void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable");
2232
2233 /*
2234 * If devid was previously created but not freed up from
2235 * sd(4D) driver (i.e during detach(9F)) then do it here.
2236 */
2237 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2238 (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2239 "use-cmdk-devid-format", 0) == 1) &&
2240 (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) {
2241 ddi_devid_unregister(tgt_dip);
2242 ddi_devid_free(devid);
2243 }
2244 }
2245
2246 /*
2247 * Implementation of scsi tran_init_pkt
2248 * Upon successful return, scsi pkt buffer has DMA resources allocated.
2249 *
2250 * It seems that we should always allocate pkt, even if the address is
2251 * for non-existing device - just use some default for dma_attr.
2252 * The reason is that there is no way to communicate this to a caller here.
2253 * Subsequent call to sata_scsi_start may fail appropriately.
2254 * Simply returning NULL does not seem to discourage a target driver...
2255 *
2256 * Returns a pointer to initialized scsi_pkt, or NULL otherwise.
2257 */
2258 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)2259 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
2260 struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
2261 int (*callback)(caddr_t), caddr_t arg)
2262 {
2263 sata_hba_inst_t *sata_hba_inst =
2264 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2265 dev_info_t *dip = SATA_DIP(sata_hba_inst);
2266 sata_device_t sata_device;
2267 sata_drive_info_t *sdinfo;
2268 sata_pkt_txlate_t *spx;
2269 ddi_dma_attr_t cur_dma_attr;
2270 int rval;
2271 boolean_t new_pkt = B_TRUE;
2272
2273 ASSERT(ap->a_hba_tran->tran_hba_dip == dip);
2274
2275 /*
2276 * We need to translate the address, even if it could be
2277 * a bogus one, for a non-existing device
2278 */
2279 sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
2280 sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target);
2281 sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2282 sata_device.satadev_rev = SATA_DEVICE_REV;
2283
2284 if (pkt == NULL) {
2285 /*
2286 * Have to allocate a brand new scsi packet.
2287 * We need to operate with auto request sense enabled.
2288 */
2289 pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen,
2290 MAX(statuslen, SATA_MAX_SENSE_LEN),
2291 tgtlen, sizeof (sata_pkt_txlate_t), callback, arg);
2292
2293 if (pkt == NULL)
2294 return (NULL);
2295
2296 /* Fill scsi packet structure */
2297 pkt->pkt_comp = (void (*)())NULL;
2298 pkt->pkt_time = 0;
2299 pkt->pkt_resid = 0;
2300 pkt->pkt_statistics = 0;
2301 pkt->pkt_reason = 0;
2302
2303 /*
2304 * pkt_hba_private will point to sata pkt txlate structure
2305 */
2306 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2307 bzero(spx, sizeof (sata_pkt_txlate_t));
2308
2309 spx->txlt_scsi_pkt = pkt;
2310 spx->txlt_sata_hba_inst = sata_hba_inst;
2311
2312 /* Allocate sata_pkt */
2313 spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback);
2314 if (spx->txlt_sata_pkt == NULL) {
2315 /* Could not allocate sata pkt */
2316 scsi_hba_pkt_free(ap, pkt);
2317 return (NULL);
2318 }
2319 /* Set sata address */
2320 spx->txlt_sata_pkt->satapkt_device.satadev_addr =
2321 sata_device.satadev_addr;
2322 spx->txlt_sata_pkt->satapkt_device.satadev_rev =
2323 sata_device.satadev_rev;
2324
2325 if ((bp == NULL) || (bp->b_bcount == 0))
2326 return (pkt);
2327
2328 spx->txlt_total_residue = bp->b_bcount;
2329 } else {
2330 new_pkt = B_FALSE;
2331 /*
2332 * Packet was preallocated/initialized by previous call
2333 */
2334 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2335
2336 if ((bp == NULL) || (bp->b_bcount == 0)) {
2337 return (pkt);
2338 }
2339
2340 /* Pkt is available already: spx->txlt_scsi_pkt == pkt; */
2341 }
2342
2343 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
2344
2345 /*
2346 * We use an adjusted version of the dma_attr, to account
2347 * for device addressing limitations.
2348 * sata_adjust_dma_attr() will handle sdinfo == NULL which may
2349 * happen when a device is not yet configured.
2350 */
2351 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2352 sata_device.satadev_addr.cport)));
2353 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2354 &spx->txlt_sata_pkt->satapkt_device);
2355 /* NULL sdinfo may be passsed to sata_adjust_dma_attr() */
2356 sata_adjust_dma_attr(sdinfo,
2357 SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
2358 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2359 sata_device.satadev_addr.cport)));
2360 /*
2361 * Allocate necessary DMA resources for the packet's data buffer
2362 * NOTE:
2363 * In case of read/write commands, DMA resource allocation here is
2364 * based on the premise that the transfer length specified in
2365 * the read/write scsi cdb will match exactly DMA resources -
2366 * returning correct packet residue is crucial.
2367 */
2368 if ((rval = sata_dma_buf_setup(spx, flags, callback, arg,
2369 &cur_dma_attr)) != DDI_SUCCESS) {
2370 /*
2371 * If a DMA allocation request fails with
2372 * DDI_DMA_NOMAPPING, indicate the error by calling
2373 * bioerror(9F) with bp and an error code of EFAULT.
2374 * If a DMA allocation request fails with
2375 * DDI_DMA_TOOBIG, indicate the error by calling
2376 * bioerror(9F) with bp and an error code of EINVAL.
2377 * For DDI_DMA_NORESOURCES, we may have some of them allocated.
2378 * Request may be repeated later - there is no real error.
2379 */
2380 switch (rval) {
2381 case DDI_DMA_NORESOURCES:
2382 bioerror(bp, 0);
2383 break;
2384 case DDI_DMA_NOMAPPING:
2385 case DDI_DMA_BADATTR:
2386 bioerror(bp, EFAULT);
2387 break;
2388 case DDI_DMA_TOOBIG:
2389 default:
2390 bioerror(bp, EINVAL);
2391 break;
2392 }
2393 goto fail;
2394 }
2395
2396 if (sata_check_for_dma_error(dip, spx)) {
2397 ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED);
2398 bioerror(bp, EFAULT);
2399 goto fail;
2400 }
2401
2402 /* Set number of bytes that are not yet accounted for */
2403 pkt->pkt_resid = spx->txlt_total_residue;
2404 ASSERT(pkt->pkt_resid >= 0);
2405
2406 return (pkt);
2407
2408 fail:
2409 if (new_pkt == B_TRUE) {
2410 /*
2411 * Since this is a new packet, we can clean-up
2412 * everything
2413 */
2414 sata_scsi_destroy_pkt(ap, pkt);
2415 } else {
2416 /*
2417 * This is a re-used packet. It will be target driver's
2418 * responsibility to eventually destroy it (which
2419 * will free allocated resources).
2420 * Here, we just "complete" the request, leaving
2421 * allocated resources intact, so the request may
2422 * be retried.
2423 */
2424 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2425 sata_pkt_free(spx);
2426 }
2427 return (NULL);
2428 }
2429
2430 /*
2431 * Implementation of scsi tran_start.
2432 * Translate scsi cmd into sata operation and return status.
2433 * ATAPI CDBs are passed to ATAPI devices - the device determines what commands
2434 * are supported.
2435 * For SATA hard disks, supported scsi commands:
2436 * SCMD_INQUIRY
2437 * SCMD_TEST_UNIT_READY
2438 * SCMD_START_STOP
2439 * SCMD_READ_CAPACITY
2440 * SCMD_SVC_ACTION_IN_G4 (READ CAPACITY (16))
2441 * SCMD_REQUEST_SENSE
2442 * SCMD_LOG_SENSE_G1
2443 * SCMD_LOG_SELECT_G1
2444 * SCMD_MODE_SENSE (specific pages)
2445 * SCMD_MODE_SENSE_G1 (specific pages)
2446 * SCMD_MODE_SELECT (specific pages)
2447 * SCMD_MODE_SELECT_G1 (specific pages)
2448 * SCMD_SYNCHRONIZE_CACHE
2449 * SCMD_SYNCHRONIZE_CACHE_G1
2450 * SCMD_READ
2451 * SCMD_READ_G1
2452 * SCMD_READ_G4
2453 * SCMD_READ_G5
2454 * SCMD_WRITE
2455 * SCMD_WRITE_BUFFER
2456 * SCMD_WRITE_G1
2457 * SCMD_WRITE_G4
2458 * SCMD_WRITE_G5
2459 * SCMD_SEEK (noop)
2460 * SCMD_SDIAG
2461 *
2462 * All other commands are rejected as unsupported.
2463 *
2464 * Returns:
2465 * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver
2466 * for execution. TRAN_ACCEPT may be returned also if device was removed but
2467 * a callback could be scheduled.
2468 * TRAN_BADPKT if cmd was directed to invalid address.
2469 * TRAN_FATAL_ERROR is command was rejected due to hardware error, including
2470 * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device
2471 * was removed and there was no callback specified in scsi pkt.
2472 * TRAN_BUSY if command could not be executed becasue HBA driver or SATA
2473 * framework was busy performing some other operation(s).
2474 *
2475 */
2476 static int
sata_scsi_start(struct scsi_address * ap,struct scsi_pkt * pkt)2477 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
2478 {
2479 sata_hba_inst_t *sata_hba_inst =
2480 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2481 sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2482 sata_device_t *sdevice = &spx->txlt_sata_pkt->satapkt_device;
2483 sata_drive_info_t *sdinfo = NULL;
2484 struct buf *bp;
2485 uint8_t cport, pmport;
2486 boolean_t dev_gone = B_FALSE;
2487 int rval;
2488
2489 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2490 "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]);
2491
2492 ASSERT(spx != NULL &&
2493 spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL);
2494
2495 cport = SCSI_TO_SATA_CPORT(ap->a_target);
2496 pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2497
2498 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2499
2500 if (sdevice->satadev_addr.qual == SATA_ADDR_DCPORT) {
2501 sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2502 if (sdinfo == NULL ||
2503 SATA_CPORT_INFO(sata_hba_inst, cport)->
2504 cport_tgtnode_clean == B_FALSE ||
2505 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2506 dev_gone = B_TRUE;
2507 }
2508 } else if (sdevice->satadev_addr.qual == SATA_ADDR_DPMPORT) {
2509 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
2510 SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst,
2511 cport) == NULL) {
2512 dev_gone = B_TRUE;
2513 } else if (SATA_PMPORT_INFO(sata_hba_inst, cport,
2514 pmport) == NULL) {
2515 dev_gone = B_TRUE;
2516 } else {
2517 mutex_enter(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2518 cport, pmport)));
2519 sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2520 if (sdinfo == NULL ||
2521 SATA_PMPORT_INFO(sata_hba_inst, cport, pmport)->
2522 pmport_tgtnode_clean == B_FALSE ||
2523 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2524 dev_gone = B_TRUE;
2525 }
2526 mutex_exit(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2527 cport, pmport)));
2528 }
2529 }
2530
2531 if (dev_gone == B_TRUE) {
2532 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2533 pkt->pkt_reason = CMD_DEV_GONE;
2534 /*
2535 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2536 * only in callback function (for normal requests) and
2537 * in the dump code path.
2538 * So, if the callback is available, we need to do
2539 * the callback rather than returning TRAN_FATAL_ERROR here.
2540 */
2541 if (pkt->pkt_comp != NULL) {
2542 /* scsi callback required */
2543 if (servicing_interrupt()) {
2544 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2545 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2546 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
2547 TASKQID_INVALID) {
2548 return (TRAN_BUSY);
2549 }
2550 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2551 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2552 spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
2553 /* Scheduling the callback failed */
2554 return (TRAN_BUSY);
2555 }
2556 return (TRAN_ACCEPT);
2557 }
2558 /* No callback available */
2559 return (TRAN_FATAL_ERROR);
2560 }
2561
2562 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
2563 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2564 rval = sata_txlt_atapi(spx);
2565 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2566 "sata_scsi_start atapi: rval %d\n", rval);
2567 return (rval);
2568 }
2569 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2570
2571 /*
2572 * Checking for power state, if it was on
2573 * STOPPED state, then the drive is not capable
2574 * of processing media access command. And
2575 * TEST_UNIT_READY, REQUEST_SENSE has special handling
2576 * in the function for different power state.
2577 */
2578 if (((sdinfo->satadrv_power_level == SATA_POWER_STANDBY) ||
2579 (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)) &&
2580 (SATA_IS_MEDIUM_ACCESS_CMD(pkt->pkt_cdbp[0]))) {
2581 return (sata_txlt_check_condition(spx, KEY_NOT_READY,
2582 SD_SCSI_ASC_LU_NOT_READY));
2583 }
2584
2585 /* ATA Disk commands processing starts here */
2586
2587 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2588
2589 switch (pkt->pkt_cdbp[0]) {
2590
2591 case SCMD_INQUIRY:
2592 /* Mapped to identify device */
2593 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2594 bp_mapin(bp);
2595 rval = sata_txlt_inquiry(spx);
2596 break;
2597
2598 case SCMD_TEST_UNIT_READY:
2599 /*
2600 * SAT "SATA to ATA Translation" doc specifies translation
2601 * to ATA CHECK POWER MODE.
2602 */
2603 rval = sata_txlt_test_unit_ready(spx);
2604 break;
2605
2606 case SCMD_START_STOP:
2607 /* Mapping depends on the command */
2608 rval = sata_txlt_start_stop_unit(spx);
2609 break;
2610
2611 case SCMD_READ_CAPACITY:
2612 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2613 bp_mapin(bp);
2614 rval = sata_txlt_read_capacity(spx);
2615 break;
2616
2617 case SCMD_SVC_ACTION_IN_G4: /* READ CAPACITY (16) */
2618 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2619 bp_mapin(bp);
2620 rval = sata_txlt_read_capacity16(spx);
2621 break;
2622
2623 case SCMD_REQUEST_SENSE:
2624 /*
2625 * Always No Sense, since we force ARQ
2626 */
2627 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2628 bp_mapin(bp);
2629 rval = sata_txlt_request_sense(spx);
2630 break;
2631
2632 case SCMD_LOG_SENSE_G1:
2633 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2634 bp_mapin(bp);
2635 rval = sata_txlt_log_sense(spx);
2636 break;
2637
2638 case SCMD_LOG_SELECT_G1:
2639 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2640 bp_mapin(bp);
2641 rval = sata_txlt_log_select(spx);
2642 break;
2643
2644 case SCMD_MODE_SENSE:
2645 case SCMD_MODE_SENSE_G1:
2646 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2647 bp_mapin(bp);
2648 rval = sata_txlt_mode_sense(spx);
2649 break;
2650
2651
2652 case SCMD_MODE_SELECT:
2653 case SCMD_MODE_SELECT_G1:
2654 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2655 bp_mapin(bp);
2656 rval = sata_txlt_mode_select(spx);
2657 break;
2658
2659 case SCMD_SYNCHRONIZE_CACHE:
2660 case SCMD_SYNCHRONIZE_CACHE_G1:
2661 rval = sata_txlt_synchronize_cache(spx);
2662 break;
2663
2664 case SCMD_READ:
2665 case SCMD_READ_G1:
2666 case SCMD_READ_G4:
2667 case SCMD_READ_G5:
2668 rval = sata_txlt_read(spx);
2669 break;
2670 case SCMD_WRITE_BUFFER:
2671 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2672 bp_mapin(bp);
2673 rval = sata_txlt_write_buffer(spx);
2674 break;
2675
2676 case SCMD_WRITE:
2677 case SCMD_WRITE_G1:
2678 case SCMD_WRITE_G4:
2679 case SCMD_WRITE_G5:
2680 rval = sata_txlt_write(spx);
2681 break;
2682
2683 case SCMD_SEEK:
2684 rval = sata_txlt_nodata_cmd_immediate(spx);
2685 break;
2686
2687 case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12:
2688 case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16:
2689 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2690 bp_mapin(bp);
2691 rval = sata_txlt_ata_pass_thru(spx);
2692 break;
2693
2694 /* Other cases will be filed later */
2695 /* postponed until phase 2 of the development */
2696 case SPC3_CMD_UNMAP:
2697 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2698 bp_mapin(bp);
2699 rval = sata_txlt_unmap(spx);
2700 break;
2701 default:
2702 rval = sata_txlt_invalid_command(spx);
2703 break;
2704 }
2705
2706 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2707 "sata_scsi_start: rval %d\n", rval);
2708
2709 return (rval);
2710 }
2711
2712 /*
2713 * Implementation of scsi tran_abort.
2714 * Abort specific pkt or all packets.
2715 *
2716 * Returns 1 if one or more packets were aborted, returns 0 otherwise
2717 *
2718 * May be called from an interrupt level.
2719 */
2720 static int
sata_scsi_abort(struct scsi_address * ap,struct scsi_pkt * scsi_pkt)2721 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt)
2722 {
2723 sata_hba_inst_t *sata_hba_inst =
2724 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2725 sata_device_t sata_device;
2726 sata_pkt_t *sata_pkt;
2727
2728 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2729 "sata_scsi_abort: %s at target: 0x%x\n",
2730 scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target);
2731
2732 /* Validate address */
2733 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0)
2734 /* Invalid address */
2735 return (0);
2736
2737 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2738 sata_device.satadev_addr.cport)));
2739 if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2740 /* invalid address */
2741 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2742 sata_device.satadev_addr.cport)));
2743 return (0);
2744 }
2745 if (scsi_pkt == NULL) {
2746 /*
2747 * Abort all packets.
2748 * Although we do not have specific packet, we still need
2749 * dummy packet structure to pass device address to HBA.
2750 * Allocate one, without sleeping. Fail if pkt cannot be
2751 * allocated.
2752 */
2753 sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP);
2754 if (sata_pkt == NULL) {
2755 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2756 sata_device.satadev_addr.cport)));
2757 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: "
2758 "could not allocate sata_pkt"));
2759 return (0);
2760 }
2761 sata_pkt->satapkt_rev = SATA_PKT_REV;
2762 sata_pkt->satapkt_device = sata_device;
2763 sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
2764 } else {
2765 if (scsi_pkt->pkt_ha_private == NULL) {
2766 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2767 sata_device.satadev_addr.cport)));
2768 return (0); /* Bad scsi pkt */
2769 }
2770 /* extract pointer to sata pkt */
2771 sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)->
2772 txlt_sata_pkt;
2773 }
2774
2775 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2776 sata_device.satadev_addr.cport)));
2777 /* Send abort request to HBA */
2778 if ((*SATA_ABORT_FUNC(sata_hba_inst))
2779 (SATA_DIP(sata_hba_inst), sata_pkt,
2780 scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) ==
2781 SATA_SUCCESS) {
2782 if (scsi_pkt == NULL)
2783 kmem_free(sata_pkt, sizeof (sata_pkt_t));
2784 /* Success */
2785 return (1);
2786 }
2787 /* Else, something did not go right */
2788 if (scsi_pkt == NULL)
2789 kmem_free(sata_pkt, sizeof (sata_pkt_t));
2790 /* Failure */
2791 return (0);
2792 }
2793
2794
2795 /*
2796 * Implementation of scsi tran_reset.
2797 * RESET_ALL request is translated into port reset.
2798 * RESET_TARGET requests is translated into a device reset,
2799 * RESET_LUN request is accepted only for LUN 0 and translated into
2800 * device reset.
2801 * The target reset should cause all HBA active and queued packets to
2802 * be terminated and returned with pkt reason SATA_PKT_RESET prior to
2803 * the return. HBA should report reset event for the device.
2804 *
2805 * Returns 1 upon success, 0 upon failure.
2806 */
2807 static int
sata_scsi_reset(struct scsi_address * ap,int level)2808 sata_scsi_reset(struct scsi_address *ap, int level)
2809 {
2810 sata_hba_inst_t *sata_hba_inst =
2811 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2812 sata_device_t sata_device;
2813 int val;
2814
2815 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2816 "sata_scsi_reset: level %d target: 0x%x\n",
2817 level, ap->a_target);
2818
2819 /* Validate address */
2820 val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device);
2821 if (val == -1)
2822 /* Invalid address */
2823 return (0);
2824
2825 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2826 sata_device.satadev_addr.cport)));
2827 if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2828 /* invalid address */
2829 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2830 sata_device.satadev_addr.cport)));
2831 return (0);
2832 }
2833 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2834 sata_device.satadev_addr.cport)));
2835 if (level == RESET_ALL) {
2836 /* port reset */
2837 if (sata_device.satadev_addr.qual == SATA_ADDR_DCPORT)
2838 sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2839 else
2840 sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
2841
2842 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2843 (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2844 return (1);
2845 else
2846 return (0);
2847
2848 } else if (val == 0 &&
2849 (level == RESET_TARGET || level == RESET_LUN)) {
2850 /* reset device (device attached) */
2851 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2852 (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2853 return (1);
2854 else
2855 return (0);
2856 }
2857 return (0);
2858 }
2859
2860
2861 /*
2862 * Implementation of scsi tran_getcap (get transport/device capabilities).
2863 * Supported capabilities for SATA hard disks:
2864 * auto-rqsense (always supported)
2865 * tagged-qing (supported if HBA supports it)
2866 * untagged-qing (could be supported if disk supports it, but because
2867 * caching behavior allowing untagged queuing actually
2868 * results in reduced performance. sd tries to throttle
2869 * back to only 3 outstanding commands, which may
2870 * work for real SCSI disks, but with read ahead
2871 * caching, having more than 1 outstanding command
2872 * results in cache thrashing.)
2873 * sector_size
2874 * dma_max
2875 * interconnect-type (INTERCONNECT_SATA)
2876 *
2877 * Supported capabilities for ATAPI CD/DVD devices:
2878 * auto-rqsense (always supported)
2879 * sector_size
2880 * dma_max
2881 * max-cdb-length
2882 * interconnect-type (INTERCONNECT_SATA)
2883 *
2884 * Supported capabilities for ATAPI TAPE devices:
2885 * auto-rqsense (always supported)
2886 * dma_max
2887 * max-cdb-length
2888 *
2889 * Supported capabilities for SATA ATAPI hard disks:
2890 * auto-rqsense (always supported)
2891 * interconnect-type (INTERCONNECT_SATA)
2892 * max-cdb-length
2893 *
2894 * Request for other capabilities is rejected as unsupported.
2895 *
2896 * Returns supported capability value, or -1 if capability is unsuppported or
2897 * the address is invalid - no device.
2898 */
2899
2900 static int
sata_scsi_getcap(struct scsi_address * ap,char * cap,int whom)2901 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
2902 {
2903
2904 sata_hba_inst_t *sata_hba_inst =
2905 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2906 sata_device_t sata_device;
2907 sata_drive_info_t *sdinfo;
2908 ddi_dma_attr_t adj_dma_attr;
2909 int rval;
2910
2911 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2912 "sata_scsi_getcap: target: 0x%x, cap: %s\n",
2913 ap->a_target, cap);
2914
2915 /*
2916 * We want to process the capabilities on per port granularity.
2917 * So, we are specifically restricting ourselves to whom != 0
2918 * to exclude the controller wide handling.
2919 */
2920 if (cap == NULL || whom == 0)
2921 return (-1);
2922
2923 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2924 /* Invalid address */
2925 return (-1);
2926 }
2927 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2928 sata_device.satadev_addr.cport)));
2929 if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) ==
2930 NULL) {
2931 /* invalid address */
2932 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2933 sata_device.satadev_addr.cport)));
2934 return (-1);
2935 }
2936
2937 switch (scsi_hba_lookup_capstr(cap)) {
2938 case SCSI_CAP_ARQ:
2939 rval = 1; /* ARQ supported, turned on */
2940 break;
2941
2942 case SCSI_CAP_SECTOR_SIZE:
2943 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK)
2944 rval = SATA_DISK_SECTOR_SIZE; /* fixed size */
2945 else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD)
2946 rval = SATA_ATAPI_SECTOR_SIZE;
2947 else rval = -1;
2948 break;
2949
2950 /*
2951 * untagged queuing cause a performance inversion because of
2952 * the way sd operates. Because of this reason we do not
2953 * use it when available.
2954 */
2955 case SCSI_CAP_UNTAGGED_QING:
2956 if (sdinfo->satadrv_features_enabled &
2957 SATA_DEV_F_E_UNTAGGED_QING)
2958 rval = 1; /* Untagged queuing available */
2959 else
2960 rval = -1; /* Untagged queuing not available */
2961 break;
2962
2963 case SCSI_CAP_TAGGED_QING:
2964 if ((sdinfo->satadrv_features_enabled &
2965 SATA_DEV_F_E_TAGGED_QING) &&
2966 (sdinfo->satadrv_max_queue_depth > 1))
2967 rval = 1; /* Tagged queuing available */
2968 else
2969 rval = -1; /* Tagged queuing not available */
2970 break;
2971
2972 case SCSI_CAP_DMA_MAX:
2973 sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst),
2974 &adj_dma_attr);
2975 rval = (int)adj_dma_attr.dma_attr_maxxfer;
2976 /* We rely on the fact that dma_attr_maxxfer < 0x80000000 */
2977 break;
2978
2979 case SCSI_CAP_INTERCONNECT_TYPE:
2980 rval = INTERCONNECT_SATA; /* SATA interconnect type */
2981 break;
2982
2983 case SCSI_CAP_CDB_LEN:
2984 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI)
2985 rval = sdinfo->satadrv_atapi_cdb_len;
2986 else
2987 rval = -1;
2988 break;
2989
2990 default:
2991 rval = -1;
2992 break;
2993 }
2994 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2995 sata_device.satadev_addr.cport)));
2996 return (rval);
2997 }
2998
2999 /*
3000 * Implementation of scsi tran_setcap
3001 *
3002 * Only SCSI_CAP_UNTAGGED_QING and SCSI_CAP_TAGGED_QING are changeable.
3003 *
3004 */
3005 static int
sata_scsi_setcap(struct scsi_address * ap,char * cap,int value,int whom)3006 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
3007 {
3008 sata_hba_inst_t *sata_hba_inst =
3009 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
3010 sata_device_t sata_device;
3011 sata_drive_info_t *sdinfo;
3012 int rval;
3013
3014 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
3015 "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap);
3016
3017 /*
3018 * We want to process the capabilities on per port granularity.
3019 * So, we are specifically restricting ourselves to whom != 0
3020 * to exclude the controller wide handling.
3021 */
3022 if (cap == NULL || whom == 0) {
3023 return (-1);
3024 }
3025
3026 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
3027 /* Invalid address */
3028 return (-1);
3029 }
3030 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
3031 sata_device.satadev_addr.cport)));
3032 if ((sdinfo = sata_get_device_info(sata_hba_inst,
3033 &sata_device)) == NULL) {
3034 /* invalid address */
3035 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3036 sata_device.satadev_addr.cport)));
3037 return (-1);
3038 }
3039 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3040 sata_device.satadev_addr.cport)));
3041
3042 switch (scsi_hba_lookup_capstr(cap)) {
3043 case SCSI_CAP_ARQ:
3044 case SCSI_CAP_SECTOR_SIZE:
3045 case SCSI_CAP_DMA_MAX:
3046 case SCSI_CAP_INTERCONNECT_TYPE:
3047 rval = 0;
3048 break;
3049 case SCSI_CAP_UNTAGGED_QING:
3050 if (SATA_QDEPTH(sata_hba_inst) > 1) {
3051 rval = 1;
3052 if (value == 1) {
3053 sdinfo->satadrv_features_enabled |=
3054 SATA_DEV_F_E_UNTAGGED_QING;
3055 } else if (value == 0) {
3056 sdinfo->satadrv_features_enabled &=
3057 ~SATA_DEV_F_E_UNTAGGED_QING;
3058 } else {
3059 rval = -1;
3060 }
3061 } else {
3062 rval = 0;
3063 }
3064 break;
3065 case SCSI_CAP_TAGGED_QING:
3066 /* This can TCQ or NCQ */
3067 if (sata_func_enable & SATA_ENABLE_QUEUING &&
3068 ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ &&
3069 SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) ||
3070 (sata_func_enable & SATA_ENABLE_NCQ &&
3071 sdinfo->satadrv_features_support & SATA_DEV_F_NCQ &&
3072 SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) &&
3073 (sdinfo->satadrv_max_queue_depth > 1)) {
3074 rval = 1;
3075 if (value == 1) {
3076 sdinfo->satadrv_features_enabled |=
3077 SATA_DEV_F_E_TAGGED_QING;
3078 } else if (value == 0) {
3079 sdinfo->satadrv_features_enabled &=
3080 ~SATA_DEV_F_E_TAGGED_QING;
3081 } else {
3082 rval = -1;
3083 }
3084 } else {
3085 rval = 0;
3086 }
3087 break;
3088 default:
3089 rval = -1;
3090 break;
3091 }
3092 return (rval);
3093 }
3094
3095 /*
3096 * Implementations of scsi tran_destroy_pkt.
3097 * Free resources allocated by sata_scsi_init_pkt()
3098 */
3099 static void
sata_scsi_destroy_pkt(struct scsi_address * ap,struct scsi_pkt * pkt)3100 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3101 {
3102 sata_pkt_txlate_t *spx;
3103
3104 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3105
3106 sata_common_free_dma_rsrcs(spx);
3107
3108 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
3109 sata_pkt_free(spx);
3110
3111 scsi_hba_pkt_free(ap, pkt);
3112 }
3113
3114 /*
3115 * Implementation of scsi tran_dmafree.
3116 * Free DMA resources allocated by sata_scsi_init_pkt()
3117 */
3118
3119 static void
sata_scsi_dmafree(struct scsi_address * ap,struct scsi_pkt * pkt)3120 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
3121 {
3122 #ifndef __lock_lint
3123 _NOTE(ARGUNUSED(ap))
3124 #endif
3125 sata_pkt_txlate_t *spx;
3126
3127 ASSERT(pkt != NULL);
3128 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3129
3130 sata_common_free_dma_rsrcs(spx);
3131 }
3132
3133 /*
3134 * Implementation of scsi tran_sync_pkt.
3135 *
3136 * The assumption below is that pkt is unique - there is no need to check ap
3137 *
3138 * Synchronize DMA buffer and, if the intermediate buffer is used, copy data
3139 * into/from the real buffer.
3140 */
3141 static void
sata_scsi_sync_pkt(struct scsi_address * ap __unused,struct scsi_pkt * pkt)3142 sata_scsi_sync_pkt(struct scsi_address *ap __unused, struct scsi_pkt *pkt)
3143 {
3144 sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3145 struct buf *bp;
3146 int direction;
3147 int rval;
3148
3149 ASSERT(spx != NULL);
3150 if (spx->txlt_buf_dma_handle == NULL)
3151 return;
3152
3153 if (spx->txlt_sata_pkt == NULL)
3154 return;
3155
3156 direction = spx->txlt_sata_pkt->
3157 satapkt_cmd.satacmd_flags.sata_data_direction;
3158
3159 if (direction == SATA_DIR_NODATA_XFER)
3160 return;
3161
3162 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3163
3164 if (spx->txlt_tmp_buf != NULL && (direction & SATA_DIR_WRITE) != 0) {
3165 /* Intermediate DMA buffer used */
3166 bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf, bp->b_bcount);
3167 }
3168
3169 /* Sync the buffer for device or for CPU */
3170 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
3171 (direction & SATA_DIR_WRITE) ?
3172 DDI_DMA_SYNC_FORDEV : DDI_DMA_SYNC_FORCPU);
3173 ASSERT3S(rval, ==, DDI_SUCCESS);
3174
3175 if (spx->txlt_tmp_buf != NULL && !(direction & SATA_DIR_WRITE)) {
3176 /* Intermediate DMA buffer used for read */
3177 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, bp->b_bcount);
3178 }
3179 }
3180
3181
3182
3183 /* ******************* SATA - SCSI Translation functions **************** */
3184 /*
3185 * SCSI to SATA pkt and command translation and SATA to SCSI status/error
3186 * translation.
3187 */
3188
3189 /*
3190 * Checks if a device exists and can be access and translates common
3191 * scsi_pkt data to sata_pkt data.
3192 *
3193 * Flag argument indicates that a non-read/write ATA command may be sent
3194 * to HBA in arbitrary SYNC mode to execute this packet.
3195 *
3196 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and
3197 * sata_pkt was set-up.
3198 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not
3199 * exist and pkt_comp callback was scheduled.
3200 * Returns other TRAN_XXXXX values when error occured and command should be
3201 * rejected with the returned TRAN_XXXXX value.
3202 *
3203 * This function should be called with port mutex held.
3204 */
3205 static int
sata_txlt_generic_pkt_info(sata_pkt_txlate_t * spx,int * reason,int flag)3206 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason, int flag)
3207 {
3208 sata_drive_info_t *sdinfo;
3209 sata_device_t sata_device;
3210 const struct sata_cmd_flags sata_initial_cmd_flags = {
3211 SATA_DIR_NODATA_XFER,
3212 /* all other values to 0/FALSE */
3213 };
3214 /*
3215 * Pkt_reason has to be set if the pkt_comp callback is invoked,
3216 * and that implies TRAN_ACCEPT return value. Any other returned value
3217 * indicates that the scsi packet was not accepted (the reason will not
3218 * be checked by the scsi target driver).
3219 * To make debugging easier, we set pkt_reason to know value here.
3220 * It may be changed later when different completion reason is
3221 * determined.
3222 */
3223 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
3224 *reason = CMD_TRAN_ERR;
3225
3226 /* Validate address */
3227 switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst,
3228 &spx->txlt_scsi_pkt->pkt_address, &sata_device)) {
3229
3230 case -1:
3231 /* Invalid address or invalid device type */
3232 return (TRAN_BADPKT);
3233 case 2:
3234 /*
3235 * Valid address but device type is unknown - Chack if it is
3236 * in the reset state and therefore in an indeterminate state.
3237 */
3238 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3239 &spx->txlt_sata_pkt->satapkt_device);
3240 if (sdinfo != NULL && (sdinfo->satadrv_event_flags &
3241 (SATA_EVNT_DEVICE_RESET |
3242 SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3243 if (!ddi_in_panic()) {
3244 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3245 *reason = CMD_INCOMPLETE;
3246 SATADBG1(SATA_DBG_SCSI_IF,
3247 spx->txlt_sata_hba_inst,
3248 "sata_scsi_start: rejecting command "
3249 "because of device reset state\n", NULL);
3250 return (TRAN_BUSY);
3251 }
3252 }
3253 /* FALLTHROUGH */
3254 case 1:
3255 /* valid address but no valid device - it has disappeared */
3256 spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE;
3257 *reason = CMD_DEV_GONE;
3258 /*
3259 * The sd target driver is checking CMD_DEV_GONE pkt_reason
3260 * only in callback function (for normal requests) and
3261 * in the dump code path.
3262 * So, if the callback is available, we need to do
3263 * the callback rather than returning TRAN_FATAL_ERROR here.
3264 */
3265 if (spx->txlt_scsi_pkt->pkt_comp != NULL) {
3266 /* scsi callback required */
3267 if (servicing_interrupt()) {
3268 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3269 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3270 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3271 TASKQID_INVALID) {
3272 return (TRAN_BUSY);
3273 }
3274 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3275 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3276 spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
3277 /* Scheduling the callback failed */
3278 return (TRAN_BUSY);
3279 }
3280
3281 return (TRAN_ACCEPT);
3282 }
3283 return (TRAN_FATAL_ERROR);
3284 default:
3285 /* all OK; pkt reason will be overwritten later */
3286 break;
3287 }
3288 /*
3289 * If pkt is to be executed in polling mode and a command will not be
3290 * emulated in SATA module (requires sending a non-read/write ATA
3291 * command to HBA driver in arbitrary SYNC mode) and we are in the
3292 * interrupt context and not in the panic dump, then reject the packet
3293 * to avoid a possible interrupt stack overrun or hang caused by
3294 * a potentially blocked interrupt.
3295 */
3296 if (((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0 || flag != 0) &&
3297 servicing_interrupt() && !ddi_in_panic()) {
3298 SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
3299 "sata_scsi_start: rejecting synchronous command because "
3300 "of interrupt context\n", NULL);
3301 return (TRAN_BUSY);
3302 }
3303
3304 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3305 &spx->txlt_sata_pkt->satapkt_device);
3306
3307 /*
3308 * If device is in reset condition, reject the packet with
3309 * TRAN_BUSY, unless:
3310 * 1. system is panicking (dumping)
3311 * In such case only one thread is running and there is no way to
3312 * process reset.
3313 * 2. cfgadm operation is is progress (internal APCTL lock is set)
3314 * Some cfgadm operations involve drive commands, so reset condition
3315 * needs to be ignored for IOCTL operations.
3316 */
3317 if ((sdinfo->satadrv_event_flags &
3318 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3319
3320 if (!ddi_in_panic() &&
3321 ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst,
3322 sata_device.satadev_addr.cport) &
3323 SATA_APCTL_LOCK_PORT_BUSY) == 0)) {
3324 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3325 *reason = CMD_INCOMPLETE;
3326 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3327 "sata_scsi_start: rejecting command because "
3328 "of device reset state\n", NULL);
3329 return (TRAN_BUSY);
3330 }
3331 }
3332
3333 /*
3334 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by
3335 * sata_scsi_pkt_init() because pkt init had to work also with
3336 * non-existing devices.
3337 * Now we know that the packet was set-up for a real device, so its
3338 * type is known.
3339 */
3340 spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type;
3341
3342 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags;
3343 if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst,
3344 sata_device.satadev_addr.cport)->cport_event_flags &
3345 SATA_APCTL_LOCK_PORT_BUSY) != 0) {
3346 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3347 sata_ignore_dev_reset = B_TRUE;
3348 }
3349 /*
3350 * At this point the generic translation routine determined that the
3351 * scsi packet should be accepted. Packet completion reason may be
3352 * changed later when a different completion reason is determined.
3353 */
3354 spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3355 *reason = CMD_CMPLT;
3356
3357 if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
3358 /* Synchronous execution */
3359 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH |
3360 SATA_OPMODE_POLLING;
3361 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3362 sata_ignore_dev_reset = ddi_in_panic();
3363 } else {
3364 /* Asynchronous execution */
3365 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH |
3366 SATA_OPMODE_INTERRUPTS;
3367 }
3368 /* Convert queuing information */
3369 if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG)
3370 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag =
3371 B_TRUE;
3372 else if (spx->txlt_scsi_pkt->pkt_flags &
3373 (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD))
3374 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag =
3375 B_TRUE;
3376
3377 /* Always limit pkt time */
3378 if (spx->txlt_scsi_pkt->pkt_time == 0)
3379 spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time;
3380 else
3381 /* Pass on scsi_pkt time */
3382 spx->txlt_sata_pkt->satapkt_time =
3383 spx->txlt_scsi_pkt->pkt_time;
3384
3385 return (TRAN_ACCEPT);
3386 }
3387
3388
3389 /*
3390 * Translate ATA Identify Device data to SCSI Inquiry data.
3391 * This function may be called only for ATA devices.
3392 * This function should not be called for ATAPI devices - they
3393 * respond directly to SCSI Inquiry command.
3394 *
3395 * SATA Identify Device data has to be valid in sata_drive_info.
3396 * Buffer has to accomodate the inquiry length (36 bytes).
3397 *
3398 * This function should be called with a port mutex held.
3399 */
3400 static void
sata_identdev_to_inquiry(sata_hba_inst_t * sata_hba_inst,sata_drive_info_t * sdinfo,uint8_t * buf)3401 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst,
3402 sata_drive_info_t *sdinfo, uint8_t *buf)
3403 {
3404
3405 struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
3406 struct sata_id *sid = &sdinfo->satadrv_id;
3407
3408 /* Start with a nice clean slate */
3409 bzero((void *)inq, sizeof (struct scsi_inquiry));
3410
3411 /*
3412 * Rely on the dev_type for setting paripheral qualifier.
3413 * Assume that DTYPE_RODIRECT applies to CD/DVD R/W devices.
3414 * It could be that DTYPE_OPTICAL could also qualify in the future.
3415 * ATAPI Inquiry may provide more data to the target driver.
3416 */
3417 inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3418 DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */
3419
3420 /* CFA type device is not a removable media device */
3421 inq->inq_rmb = ((sid->ai_config != SATA_CFA_TYPE) &&
3422 (sid->ai_config & SATA_REM_MEDIA)) ? 1 : 0;
3423 inq->inq_qual = 0; /* Device type qualifier (obsolete in SCSI3? */
3424 inq->inq_iso = 0; /* ISO version */
3425 inq->inq_ecma = 0; /* ECMA version */
3426 inq->inq_ansi = 3; /* ANSI version - SCSI 3 */
3427 inq->inq_aenc = 0; /* Async event notification cap. */
3428 inq->inq_trmiop = 0; /* Supports TERMINATE I/O PROC msg - NO */
3429 inq->inq_normaca = 0; /* setting NACA bit supported - NO */
3430 inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */
3431 inq->inq_len = 31; /* Additional length */
3432 inq->inq_dualp = 0; /* dual port device - NO */
3433 inq->inq_reladdr = 0; /* Supports relative addressing - NO */
3434 inq->inq_sync = 0; /* Supports synchronous data xfers - NO */
3435 inq->inq_linked = 0; /* Supports linked commands - NO */
3436 /*
3437 * Queuing support - controller has to
3438 * support some sort of command queuing.
3439 */
3440 if (SATA_QDEPTH(sata_hba_inst) > 1)
3441 inq->inq_cmdque = 1; /* Supports command queueing - YES */
3442 else
3443 inq->inq_cmdque = 0; /* Supports command queueing - NO */
3444 inq->inq_sftre = 0; /* Supports Soft Reset option - NO ??? */
3445 inq->inq_wbus32 = 0; /* Supports 32 bit wide data xfers - NO */
3446 inq->inq_wbus16 = 0; /* Supports 16 bit wide data xfers - NO */
3447
3448 #ifdef _LITTLE_ENDIAN
3449 /* Swap text fields to match SCSI format */
3450 bcopy("ATA ", inq->inq_vid, 8); /* Vendor ID */
3451 swab(sid->ai_model, inq->inq_pid, 16); /* Product ID */
3452 if (strncmp(&sid->ai_fw[4], " ", 4) == 0)
3453 swab(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
3454 else
3455 swab(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
3456 #else /* _LITTLE_ENDIAN */
3457 bcopy("ATA ", inq->inq_vid, 8); /* Vendor ID */
3458 bcopy(sid->ai_model, inq->inq_pid, 16); /* Product ID */
3459 if (strncmp(&sid->ai_fw[4], " ", 4) == 0)
3460 bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
3461 else
3462 bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
3463 #endif /* _LITTLE_ENDIAN */
3464 }
3465
3466
3467 /*
3468 * Scsi response set up for invalid command (command not supported)
3469 *
3470 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3471 */
3472 static int
sata_txlt_invalid_command(sata_pkt_txlate_t * spx)3473 sata_txlt_invalid_command(sata_pkt_txlate_t *spx)
3474 {
3475 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3476 struct scsi_extended_sense *sense;
3477
3478 scsipkt->pkt_reason = CMD_CMPLT;
3479 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3480 STATE_SENT_CMD | STATE_GOT_STATUS;
3481
3482 *scsipkt->pkt_scbp = STATUS_CHECK;
3483
3484 sense = sata_arq_sense(spx);
3485 sense->es_key = KEY_ILLEGAL_REQUEST;
3486 sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE;
3487
3488 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3489 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3490
3491 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3492 scsipkt->pkt_comp != NULL) {
3493 /* scsi callback required */
3494 if (servicing_interrupt()) {
3495 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3496 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3497 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3498 TASKQID_INVALID) {
3499 return (TRAN_BUSY);
3500 }
3501 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3502 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3503 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
3504 /* Scheduling the callback failed */
3505 return (TRAN_BUSY);
3506 }
3507 }
3508 return (TRAN_ACCEPT);
3509 }
3510
3511 /*
3512 * Scsi response set up for check condition with special sense key
3513 * and additional sense code.
3514 *
3515 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3516 */
3517 static int
sata_txlt_check_condition(sata_pkt_txlate_t * spx,uchar_t key,uchar_t code)3518 sata_txlt_check_condition(sata_pkt_txlate_t *spx, uchar_t key, uchar_t code)
3519 {
3520 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3521 int cport = SATA_TXLT_CPORT(spx);
3522 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3523 struct scsi_extended_sense *sense;
3524
3525 mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3526 scsipkt->pkt_reason = CMD_CMPLT;
3527 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3528 STATE_SENT_CMD | STATE_GOT_STATUS;
3529
3530 *scsipkt->pkt_scbp = STATUS_CHECK;
3531
3532 sense = sata_arq_sense(spx);
3533 sense->es_key = key;
3534 sense->es_add_code = code;
3535
3536 mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3537
3538 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3539 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3540
3541 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3542 scsipkt->pkt_comp != NULL) {
3543 /* scsi callback required */
3544 if (servicing_interrupt()) {
3545 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3546 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3547 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3548 TASKQID_INVALID) {
3549 return (TRAN_BUSY);
3550 }
3551 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3552 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3553 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
3554 /* Scheduling the callback failed */
3555 return (TRAN_BUSY);
3556 }
3557 }
3558 return (TRAN_ACCEPT);
3559 }
3560
3561 /*
3562 * Scsi response setup for
3563 * emulated non-data command that requires no action/return data
3564 *
3565 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3566 */
3567 static int
sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t * spx)3568 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx)
3569 {
3570 int rval;
3571 int reason;
3572 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
3573
3574 mutex_enter(cport_mutex);
3575
3576 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
3577 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3578 mutex_exit(cport_mutex);
3579 return (rval);
3580 }
3581 mutex_exit(cport_mutex);
3582
3583 spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3584 STATE_SENT_CMD | STATE_GOT_STATUS;
3585 spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3586 *(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD;
3587
3588 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3589 "Scsi_pkt completion reason %x\n",
3590 spx->txlt_scsi_pkt->pkt_reason);
3591
3592 if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 &&
3593 spx->txlt_scsi_pkt->pkt_comp != NULL) {
3594 /* scsi callback required */
3595 if (servicing_interrupt()) {
3596 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3597 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3598 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3599 TASKQID_INVALID) {
3600 return (TRAN_BUSY);
3601 }
3602 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3603 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3604 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
3605 /* Scheduling the callback failed */
3606 return (TRAN_BUSY);
3607 }
3608 }
3609 return (TRAN_ACCEPT);
3610 }
3611
3612
3613 /*
3614 * SATA translate command: Inquiry / Identify Device
3615 * Use cached Identify Device data for now, rather than issuing actual
3616 * Device Identify cmd request. If device is detached and re-attached,
3617 * asynchronous event processing should fetch and refresh Identify Device
3618 * data.
3619 * VPD pages supported now:
3620 * Vital Product Data page
3621 * Unit Serial Number page
3622 * Block Device Characteristics Page
3623 * ATA Information Page
3624 *
3625 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3626 */
3627
3628 #define EVPD 1 /* Extended Vital Product Data flag */
3629 #define CMDDT 2 /* Command Support Data - Obsolete */
3630 #define INQUIRY_SUP_VPD_PAGE 0 /* Supported VPD Pages Page Code */
3631 #define INQUIRY_USN_PAGE 0x80 /* Unit Serial Number Page Code */
3632 #define INQUIRY_BDC_PAGE 0xB1 /* Block Device Characteristics Page */
3633 /* Code */
3634 #define INQUIRY_ATA_INFO_PAGE 0x89 /* ATA Information Page Code */
3635 #define INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Device identifiers */
3636
3637 static int
sata_txlt_inquiry(sata_pkt_txlate_t * spx)3638 sata_txlt_inquiry(sata_pkt_txlate_t *spx)
3639 {
3640 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3641 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3642 sata_drive_info_t *sdinfo;
3643 struct scsi_extended_sense *sense;
3644 int count;
3645 uint8_t *p;
3646 int i, j;
3647 uint8_t page_buf[1024]; /* Max length */
3648 int rval, reason;
3649 ushort_t rate;
3650 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
3651
3652 /*
3653 * sata_txlt_generic_pkt_info() and sata_get_device_info() require
3654 * cport_mutex to be held while they are called. sdinfo is also
3655 * protected by cport_mutex, so we hold cport_mutex until after we've
3656 * finished using sdinfo.
3657 */
3658 mutex_enter(cport_mutex);
3659
3660 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
3661 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3662 mutex_exit(cport_mutex);
3663 return (rval);
3664 }
3665
3666 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3667 &spx->txlt_sata_pkt->satapkt_device);
3668
3669 ASSERT(sdinfo != NULL);
3670
3671 scsipkt->pkt_reason = CMD_CMPLT;
3672 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3673 STATE_SENT_CMD | STATE_GOT_STATUS;
3674
3675 /* Reject not supported request */
3676 if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */
3677 *scsipkt->pkt_scbp = STATUS_CHECK;
3678 sense = sata_arq_sense(spx);
3679 sense->es_key = KEY_ILLEGAL_REQUEST;
3680 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3681 goto done;
3682 }
3683
3684 /* Valid Inquiry request */
3685 *scsipkt->pkt_scbp = STATUS_GOOD;
3686
3687 if (bp == NULL || bp->b_un.b_addr == NULL || bp->b_bcount == 0)
3688 goto done;
3689
3690 /*
3691 * Because it is fully emulated command storing data
3692 * programatically in the specified buffer, release
3693 * preallocated DMA resources before storing data in the buffer,
3694 * so no unwanted DMA sync would take place.
3695 */
3696 sata_scsi_dmafree(NULL, scsipkt);
3697
3698 if (!(scsipkt->pkt_cdbp[1] & EVPD)) {
3699 /* Standard Inquiry Data request */
3700 struct scsi_inquiry inq;
3701 unsigned int bufsize;
3702
3703 sata_identdev_to_inquiry(spx->txlt_sata_hba_inst,
3704 sdinfo, (uint8_t *)&inq);
3705 /* Copy no more than requested */
3706 count = MIN(bp->b_bcount, sizeof (struct scsi_inquiry));
3707 bufsize = scsipkt->pkt_cdbp[4];
3708 bufsize |= scsipkt->pkt_cdbp[3] << 8;
3709 count = MIN(count, bufsize);
3710 bcopy(&inq, bp->b_un.b_addr, count);
3711
3712 scsipkt->pkt_state |= STATE_XFERRED_DATA;
3713 scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3714 bufsize - count : 0;
3715 goto done;
3716 }
3717
3718 /*
3719 * peripheral_qualifier = 0;
3720 *
3721 * We are dealing only with HD and will be
3722 * dealing with CD/DVD devices soon
3723 */
3724 uint8_t peripheral_device_type =
3725 sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3726 DTYPE_DIRECT : DTYPE_RODIRECT;
3727
3728 bzero(page_buf, sizeof (page_buf));
3729
3730 switch ((uint_t)scsipkt->pkt_cdbp[2]) {
3731 case INQUIRY_SUP_VPD_PAGE:
3732 /*
3733 * Request for supported Vital Product Data pages.
3734 */
3735 page_buf[0] = peripheral_device_type;
3736 page_buf[1] = INQUIRY_SUP_VPD_PAGE;
3737 page_buf[2] = 0;
3738 page_buf[4] = INQUIRY_SUP_VPD_PAGE;
3739 page_buf[5] = INQUIRY_USN_PAGE;
3740 page_buf[6] = INQUIRY_BDC_PAGE;
3741 /*
3742 * If WWN info is present, provide a page for it.
3743 * Modern drives always have, but some legacy ones do not.
3744 */
3745 if (sdinfo->satadrv_id.ai_naa_ieee_oui != 0) {
3746 page_buf[3] = 5; /* page length */
3747 page_buf[7] = INQUIRY_DEV_IDENTIFICATION_PAGE;
3748 page_buf[8] = INQUIRY_ATA_INFO_PAGE;
3749 count = 9;
3750 } else {
3751 page_buf[3] = 4; /* page length */
3752 page_buf[7] = INQUIRY_ATA_INFO_PAGE;
3753 count = 8;
3754 }
3755 /* Copy no more than requested */
3756 count = MIN(bp->b_bcount, count);
3757 bcopy(page_buf, bp->b_un.b_addr, count);
3758 break;
3759
3760 case INQUIRY_USN_PAGE:
3761 /*
3762 * Request for Unit Serial Number page.
3763 * Set-up the page.
3764 */
3765 page_buf[0] = peripheral_device_type;
3766 page_buf[1] = INQUIRY_USN_PAGE;
3767 page_buf[2] = 0;
3768 /* remaining page length */
3769 page_buf[3] = SATA_ID_SERIAL_LEN;
3770
3771 /*
3772 * Copy serial number from Identify Device data
3773 * words into the inquiry page and swap bytes
3774 * when necessary.
3775 */
3776 p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser);
3777 #ifdef _LITTLE_ENDIAN
3778 swab(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3779 #else
3780 bcopy(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3781 #endif
3782 /*
3783 * Least significant character of the serial
3784 * number shall appear as the last byte,
3785 * according to SBC-3 spec.
3786 * Count trailing spaces to determine the
3787 * necessary shift length.
3788 */
3789 p = &page_buf[SATA_ID_SERIAL_LEN + 4 - 1];
3790 for (j = 0; j < SATA_ID_SERIAL_LEN; j++) {
3791 if (*(p - j) != '\0' && *(p - j) != '\040')
3792 break;
3793 }
3794
3795 /*
3796 * Shift SN string right, so that the last
3797 * non-blank character would appear in last
3798 * byte of SN field in the page.
3799 * 'j' is the shift length.
3800 */
3801 for (i = 0; i < (SATA_ID_SERIAL_LEN - j) && j != 0; i++, p--)
3802 *p = *(p - j);
3803
3804 /*
3805 * Add leading spaces - same number as the
3806 * shift size
3807 */
3808 for (; j > 0; j--)
3809 page_buf[4 + j - 1] = '\040';
3810
3811 count = MIN(bp->b_bcount, SATA_ID_SERIAL_LEN + 4);
3812 bcopy(page_buf, bp->b_un.b_addr, count);
3813 break;
3814
3815 case INQUIRY_BDC_PAGE:
3816 /*
3817 * Request for Block Device Characteristics
3818 * page. Set-up the page.
3819 */
3820 page_buf[0] = peripheral_device_type;
3821 page_buf[1] = INQUIRY_BDC_PAGE;
3822 page_buf[2] = 0;
3823 /* remaining page length */
3824 page_buf[3] = SATA_ID_BDC_LEN;
3825
3826 rate = sdinfo->satadrv_id.ai_medrotrate;
3827 page_buf[4] = (rate >> 8) & 0xff;
3828 page_buf[5] = rate & 0xff;
3829 page_buf[6] = 0;
3830 page_buf[7] = sdinfo->satadrv_id.ai_nomformfactor & 0xf;
3831
3832 count = MIN(bp->b_bcount, SATA_ID_BDC_LEN + 4);
3833 bcopy(page_buf, bp->b_un.b_addr, count);
3834 break;
3835
3836 case INQUIRY_ATA_INFO_PAGE:
3837 /*
3838 * Request for ATA Information page.
3839 */
3840 page_buf[0] = peripheral_device_type;
3841 page_buf[1] = INQUIRY_ATA_INFO_PAGE;
3842 page_buf[2] = (SATA_ID_ATA_INFO_LEN >> 8) & 0xff;
3843 page_buf[3] = SATA_ID_ATA_INFO_LEN & 0xff;
3844 /* page_buf[4-7] reserved */
3845 #ifdef _LITTLE_ENDIAN
3846 bcopy("ATA ", &page_buf[8], 8);
3847 swab(sdinfo->satadrv_id.ai_model, &page_buf[16], 16);
3848 if (strncmp(&sdinfo->satadrv_id.ai_fw[4], " ", 4) == 0) {
3849 swab(sdinfo->satadrv_id.ai_fw, &page_buf[32], 4);
3850 } else {
3851 swab(&sdinfo->satadrv_id.ai_fw[4], &page_buf[32], 4);
3852 }
3853 #else /* _LITTLE_ENDIAN */
3854 bcopy("ATA ", &page_buf[8], 8);
3855 bcopy(sdinfo->satadrv_id.ai_model, &page_buf[16], 16);
3856 if (strncmp(&sdinfo->satadrv_id.ai_fw[4], " ", 4) == 0) {
3857 bcopy(sdinfo->satadrv_id.ai_fw, &page_buf[32], 4);
3858 } else {
3859 bcopy(&sdinfo->satadrv_id.ai_fw[4], &page_buf[32], 4);
3860 }
3861 #endif /* _LITTLE_ENDIAN */
3862 /*
3863 * page_buf[36-55] which defines the device
3864 * signature is not defined at this
3865 * time.
3866 */
3867
3868 /* Set the command code */
3869 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
3870 page_buf[56] = SATAC_ID_DEVICE;
3871 } else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPI) {
3872 page_buf[56] = SATAC_ID_PACKET_DEVICE;
3873 }
3874 /*
3875 * If the command code, page_buf[56], is not
3876 * zero and if one of the identify commands
3877 * succeeds, return the identify data.
3878 */
3879 if (page_buf[56] != 0) {
3880 sata_drive_info_t temp_info = {
3881 .satadrv_addr = sdinfo->satadrv_addr,
3882 .satadrv_type = sdinfo->satadrv_type,
3883 };
3884
3885 /*
3886 * It appears calls to an HBA's start (sata_hba_start)
3887 * method (which sata_fetch_device_identify_data_retry()
3888 * calls) must not be done while holding cport_mutex.
3889 *
3890 * A packet's completion routine may call back into
3891 * the sata framework and deadlock (and all extant
3892 * calls to the HBA's start method either drop and
3893 * re-acquire cport_mutex, or never held cport_mutex).
3894 *
3895 * sdinfo is protected by cport_mutex, so we need to
3896 * obtain the SATA address and type from sdinfo
3897 * before releasing cport_mutex and submitting the
3898 * request. We reacquire cport_mutex to simplfy
3899 * cleanup after the done label.
3900 */
3901 mutex_exit(cport_mutex);
3902 (void) sata_fetch_device_identify_data(
3903 spx->txlt_sata_hba_inst, &temp_info);
3904 mutex_enter(cport_mutex);
3905
3906 /*
3907 * If sata_fetch_device_identify_data()
3908 * fails, the bcopy() is harmless since we're copying
3909 * zeros back over zeros. If it succeeds, we're
3910 * copying over the portion of the response we need.
3911 */
3912 bcopy(&temp_info.satadrv_id, &page_buf[60],
3913 sizeof (sata_id_t));
3914 }
3915
3916 /* Need to copy out the page_buf to bp */
3917 count = MIN(bp->b_bcount, SATA_ID_ATA_INFO_LEN + 4);
3918 bcopy(page_buf, bp->b_un.b_addr, count);
3919 break;
3920
3921 case INQUIRY_DEV_IDENTIFICATION_PAGE:
3922 if (sdinfo->satadrv_id.ai_naa_ieee_oui != 0) {
3923 /*
3924 * Page 83; SAT-5 requires this, and modern
3925 * SATA devices all support a WWN.
3926 */
3927 page_buf[0] = peripheral_device_type;
3928 page_buf[1] = INQUIRY_DEV_IDENTIFICATION_PAGE;
3929 page_buf[2] = 0;
3930 page_buf[3] = 12; /* remaining length */
3931 page_buf[4] = 0x01; /* protocol 0, code set 1 */
3932 page_buf[5] = 0x03; /* LUN, NAA type */
3933 page_buf[6] = 0;
3934 page_buf[7] = 0x08; /* length (64-bit WWN) */
3935 #ifdef _LITTLE_ENDIAN
3936 swab(&sdinfo->satadrv_id.ai_naa_ieee_oui, &page_buf[8],
3937 8);
3938 #else
3939 bcopy(&sdinfo->satadrv_id.ai_naa_ieee_oui,
3940 &page_buf[8], 8);
3941 #endif
3942 /* header + designator */
3943 count = MIN(bp->b_bcount, 12 + 4);
3944 bcopy(page_buf, bp->b_un.b_addr, count);
3945 break;
3946 }
3947 /* FALLTHROUGH */
3948
3949 default:
3950 /* Request for unsupported VPD page */
3951 *scsipkt->pkt_scbp = STATUS_CHECK;
3952 sense = sata_arq_sense(spx);
3953 sense->es_key = KEY_ILLEGAL_REQUEST;
3954 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3955 goto done;
3956 }
3957
3958 scsipkt->pkt_state |= STATE_XFERRED_DATA;
3959 scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3960 scsipkt->pkt_cdbp[4] - count : 0;
3961
3962 done:
3963 mutex_exit(cport_mutex);
3964
3965 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3966 "Scsi_pkt completion reason %x\n",
3967 scsipkt->pkt_reason);
3968
3969 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3970 scsipkt->pkt_comp != NULL) {
3971 /* scsi callback required */
3972 if (servicing_interrupt()) {
3973 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3974 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3975 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3976 TASKQID_INVALID) {
3977 return (TRAN_BUSY);
3978 }
3979 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3980 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3981 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
3982 /* Scheduling the callback failed */
3983 return (TRAN_BUSY);
3984 }
3985 }
3986 return (TRAN_ACCEPT);
3987 }
3988
3989 /*
3990 * SATA translate command: Request Sense.
3991 *
3992 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3993 * At the moment this is an emulated command (ATA version for SATA hard disks).
3994 * May be translated into Check Power Mode command in the future.
3995 *
3996 * Note: There is a mismatch between already implemented Informational
3997 * Exception Mode Select page 0x1C and this function.
3998 * When MRIE bit is set in page 0x1C, Request Sense is supposed to return
3999 * NO SENSE and set additional sense code to the exception code - this is not
4000 * implemented here.
4001 */
4002 static int
sata_txlt_request_sense(sata_pkt_txlate_t * spx)4003 sata_txlt_request_sense(sata_pkt_txlate_t *spx)
4004 {
4005 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4006 struct scsi_extended_sense sense;
4007 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4008 sata_drive_info_t *sdinfo;
4009 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4010 int rval, reason, power_state = 0;
4011 kmutex_t *cport_mutex;
4012
4013 cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4014 mutex_enter(cport_mutex);
4015
4016 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
4017 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4018 mutex_exit(cport_mutex);
4019 return (rval);
4020 }
4021
4022 scsipkt->pkt_reason = CMD_CMPLT;
4023 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4024 STATE_SENT_CMD | STATE_GOT_STATUS;
4025 *scsipkt->pkt_scbp = STATUS_GOOD;
4026
4027 /*
4028 * when CONTROL field's NACA bit == 1
4029 * return ILLEGAL_REQUEST
4030 */
4031 if (scsipkt->pkt_cdbp[5] & CTL_BYTE_NACA_MASK) {
4032 mutex_exit(cport_mutex);
4033 return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4034 SD_SCSI_ASC_CMD_SEQUENCE_ERR));
4035 }
4036
4037 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4038 &spx->txlt_sata_pkt->satapkt_device);
4039 ASSERT(sdinfo != NULL);
4040
4041 spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
4042
4043 sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
4044 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
4045 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4046 if (sata_hba_start(spx, &rval) != 0) {
4047 mutex_exit(cport_mutex);
4048 return (rval);
4049 }
4050 if (scmd->satacmd_error_reg != 0) {
4051 mutex_exit(cport_mutex);
4052 return (sata_txlt_check_condition(spx, KEY_NO_SENSE,
4053 SD_SCSI_ASC_NO_ADD_SENSE));
4054 }
4055
4056 switch (scmd->satacmd_sec_count_lsb) {
4057 case SATA_PWRMODE_STANDBY: /* device in standby mode */
4058 if (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)
4059 power_state = SATA_POWER_STOPPED;
4060 else {
4061 power_state = SATA_POWER_STANDBY;
4062 sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
4063 }
4064 break;
4065 case SATA_PWRMODE_IDLE: /* device in idle mode */
4066 power_state = SATA_POWER_IDLE;
4067 sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4068 break;
4069 case SATA_PWRMODE_ACTIVE: /* device in active or idle mode */
4070 default: /* 0x40, 0x41 active mode */
4071 if (sdinfo->satadrv_power_level == SATA_POWER_IDLE)
4072 power_state = SATA_POWER_IDLE;
4073 else {
4074 power_state = SATA_POWER_ACTIVE;
4075 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4076 }
4077 break;
4078 }
4079
4080 mutex_exit(cport_mutex);
4081
4082 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4083 /*
4084 * Because it is fully emulated command storing data
4085 * programatically in the specified buffer, release
4086 * preallocated DMA resources before storing data in the buffer,
4087 * so no unwanted DMA sync would take place.
4088 */
4089 int count = MIN(bp->b_bcount,
4090 sizeof (struct scsi_extended_sense));
4091 sata_scsi_dmafree(NULL, scsipkt);
4092 bzero(&sense, sizeof (struct scsi_extended_sense));
4093 sense.es_valid = 0; /* Valid LBA */
4094 sense.es_class = 7; /* Response code 0x70 - current err */
4095 sense.es_key = KEY_NO_SENSE;
4096 sense.es_add_len = 6; /* Additional length */
4097 /* Copy no more than requested */
4098 bcopy(&sense, bp->b_un.b_addr, count);
4099 scsipkt->pkt_state |= STATE_XFERRED_DATA;
4100 scsipkt->pkt_resid = 0;
4101 switch (power_state) {
4102 case SATA_POWER_IDLE:
4103 case SATA_POWER_STANDBY:
4104 sense.es_add_code =
4105 SD_SCSI_ASC_LOW_POWER_CONDITION_ON;
4106 break;
4107 case SATA_POWER_STOPPED:
4108 sense.es_add_code = SD_SCSI_ASC_NO_ADD_SENSE;
4109 break;
4110 case SATA_POWER_ACTIVE:
4111 default:
4112 break;
4113 }
4114 }
4115
4116 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4117 "Scsi_pkt completion reason %x\n",
4118 scsipkt->pkt_reason);
4119
4120 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4121 scsipkt->pkt_comp != NULL) {
4122 /* scsi callback required */
4123 if (servicing_interrupt()) {
4124 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4125 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4126 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
4127 TASKQID_INVALID) {
4128 return (TRAN_BUSY);
4129 }
4130 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4131 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4132 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
4133 /* Scheduling the callback failed */
4134 return (TRAN_BUSY);
4135 }
4136 }
4137 return (TRAN_ACCEPT);
4138 }
4139
4140 /*
4141 * SATA translate command: Test Unit Ready
4142 * (ATA version for SATA hard disks).
4143 * It is translated into the Check Power Mode command.
4144 *
4145 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4146 */
4147 static int
sata_txlt_test_unit_ready(sata_pkt_txlate_t * spx)4148 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx)
4149 {
4150 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4151 struct scsi_extended_sense *sense;
4152 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4153 sata_drive_info_t *sdinfo;
4154 int power_state;
4155 int rval, reason;
4156 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4157
4158 mutex_enter(cport_mutex);
4159
4160 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
4161 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4162 mutex_exit(cport_mutex);
4163 return (rval);
4164 }
4165
4166 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4167 &spx->txlt_sata_pkt->satapkt_device);
4168 ASSERT(sdinfo != NULL);
4169
4170 spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
4171
4172 /* send CHECK POWER MODE command */
4173 sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
4174 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
4175 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4176 if (sata_hba_start(spx, &rval) != 0) {
4177 mutex_exit(cport_mutex);
4178 return (rval);
4179 }
4180
4181 if (scmd->satacmd_error_reg != 0) {
4182 mutex_exit(cport_mutex);
4183 return (sata_txlt_check_condition(spx, KEY_NOT_READY,
4184 SD_SCSI_ASC_LU_NOT_RESPONSE));
4185 }
4186
4187 power_state = scmd->satacmd_sec_count_lsb;
4188
4189 /*
4190 * return NOT READY when device in STOPPED mode
4191 */
4192 if (power_state == SATA_PWRMODE_STANDBY &&
4193 sdinfo->satadrv_power_level == SATA_POWER_STOPPED) {
4194 *scsipkt->pkt_scbp = STATUS_CHECK;
4195 sense = sata_arq_sense(spx);
4196 sense->es_key = KEY_NOT_READY;
4197 sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY;
4198 } else {
4199 /*
4200 * For other power mode, return GOOD status
4201 */
4202 *scsipkt->pkt_scbp = STATUS_GOOD;
4203 }
4204
4205 scsipkt->pkt_reason = CMD_CMPLT;
4206 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4207 STATE_SENT_CMD | STATE_GOT_STATUS;
4208
4209 mutex_exit(cport_mutex);
4210
4211 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4212 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4213
4214 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4215 scsipkt->pkt_comp != NULL) {
4216 /* scsi callback required */
4217 if (servicing_interrupt()) {
4218 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4219 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4220 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
4221 TASKQID_INVALID) {
4222 return (TRAN_BUSY);
4223 }
4224 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4225 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4226 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
4227 /* Scheduling the callback failed */
4228 return (TRAN_BUSY);
4229 }
4230 }
4231
4232 return (TRAN_ACCEPT);
4233 }
4234
4235 /*
4236 * SATA translate command: Start Stop Unit
4237 * Translation depends on a command:
4238 *
4239 * Power condition bits will be supported
4240 * and the power level should be maintained by SATL,
4241 * When SATL received a command, it will check the
4242 * power level firstly, and return the status according
4243 * to SAT2 v2.6 and SAT-2 Standby Modifications
4244 *
4245 * SPC-4/SBC-3 SATL ATA power condition SATL SPC/SBC
4246 * -----------------------------------------------------------------------
4247 * SSU_PC1 Active <==> ATA Active <==> SSU:start_bit =1
4248 * SSU_PC2 Idle <==> ATA Idle <==> N/A
4249 * SSU_PC3 Standby <==> ATA Standby <==> N/A
4250 * SSU_PC4 Stopped <==> ATA Standby <==> SSU:start_bit = 0
4251 *
4252 * Unload Media / NOT SUPPORTED YET
4253 * Load Media / NOT SUPPROTED YET
4254 * Immediate bit / NOT SUPPORTED YET (deferred error)
4255 *
4256 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4257 * appropriate values in scsi_pkt fields.
4258 */
4259 static int
sata_txlt_start_stop_unit(sata_pkt_txlate_t * spx)4260 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx)
4261 {
4262 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4263 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4264 int rval, reason;
4265 sata_drive_info_t *sdinfo;
4266 sata_id_t *sata_id;
4267 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4268
4269 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4270 "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1);
4271
4272 mutex_enter(cport_mutex);
4273
4274 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
4275 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4276 mutex_exit(cport_mutex);
4277 return (rval);
4278 }
4279
4280 if (scsipkt->pkt_cdbp[1] & START_STOP_IMMED_MASK) {
4281 /* IMMED bit - not supported */
4282 mutex_exit(cport_mutex);
4283 return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4284 SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4285 }
4286
4287 spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
4288 spx->txlt_sata_pkt->satapkt_comp = NULL;
4289
4290 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4291 &spx->txlt_sata_pkt->satapkt_device);
4292 ASSERT(sdinfo != NULL);
4293 sata_id = &sdinfo->satadrv_id;
4294
4295 switch ((scsipkt->pkt_cdbp[4] & START_STOP_POWER_COND_MASK) >> 4) {
4296 case 0:
4297 if (scsipkt->pkt_cdbp[4] & START_STOP_LOEJ_MASK) {
4298 /* Load/Unload Media - invalid request */
4299 goto err_out;
4300 }
4301 if (scsipkt->pkt_cdbp[4] & START_STOP_START_MASK) {
4302 /* Start Unit */
4303 sata_build_read_verify_cmd(scmd, 1, 5);
4304 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4305 /* Transfer command to HBA */
4306 if (sata_hba_start(spx, &rval) != 0) {
4307 /* Pkt not accepted for execution */
4308 mutex_exit(cport_mutex);
4309 return (rval);
4310 }
4311 if (scmd->satacmd_error_reg != 0) {
4312 goto err_out;
4313 }
4314 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4315 } else {
4316 /* Stop Unit */
4317 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4318 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4319 if (sata_hba_start(spx, &rval) != 0) {
4320 mutex_exit(cport_mutex);
4321 return (rval);
4322 } else {
4323 if (scmd->satacmd_error_reg != 0) {
4324 goto err_out;
4325 }
4326 }
4327 /* ata standby immediate command */
4328 sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
4329 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4330 if (sata_hba_start(spx, &rval) != 0) {
4331 mutex_exit(cport_mutex);
4332 return (rval);
4333 }
4334 if (scmd->satacmd_error_reg != 0) {
4335 goto err_out;
4336 }
4337 sdinfo->satadrv_power_level = SATA_POWER_STOPPED;
4338 }
4339 break;
4340 case 0x1:
4341 sata_build_generic_cmd(scmd, SATAC_IDLE);
4342 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4343 if (sata_hba_start(spx, &rval) != 0) {
4344 mutex_exit(cport_mutex);
4345 return (rval);
4346 }
4347 if (scmd->satacmd_error_reg != 0) {
4348 goto err_out;
4349 }
4350 sata_build_read_verify_cmd(scmd, 1, 5);
4351 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4352 /* Transfer command to HBA */
4353 if (sata_hba_start(spx, &rval) != 0) {
4354 /* Pkt not accepted for execution */
4355 mutex_exit(cport_mutex);
4356 return (rval);
4357 } else {
4358 if (scmd->satacmd_error_reg != 0) {
4359 goto err_out;
4360 }
4361 }
4362 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4363 break;
4364 case 0x2:
4365 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4366 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4367 if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4368 if (sata_hba_start(spx, &rval) != 0) {
4369 mutex_exit(cport_mutex);
4370 return (rval);
4371 }
4372 if (scmd->satacmd_error_reg != 0) {
4373 goto err_out;
4374 }
4375 }
4376 sata_build_generic_cmd(scmd, SATAC_IDLE);
4377 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4378 if (sata_hba_start(spx, &rval) != 0) {
4379 mutex_exit(cport_mutex);
4380 return (rval);
4381 }
4382 if (scmd->satacmd_error_reg != 0) {
4383 goto err_out;
4384 }
4385 if ((scsipkt->pkt_cdbp[3] & START_STOP_MODIFIER_MASK)) {
4386 /*
4387 * POWER CONDITION MODIFIER bit set
4388 * to 0x1 or larger it will be handled
4389 * on the same way as bit = 0x1
4390 */
4391 if (!(sata_id->ai_cmdset84 &
4392 SATA_IDLE_UNLOAD_SUPPORTED)) {
4393 sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4394 break;
4395 }
4396 sata_build_generic_cmd(scmd, SATAC_IDLE_IM);
4397 scmd->satacmd_features_reg = 0x44;
4398 scmd->satacmd_lba_low_lsb = 0x4c;
4399 scmd->satacmd_lba_mid_lsb = 0x4e;
4400 scmd->satacmd_lba_high_lsb = 0x55;
4401 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4402 if (sata_hba_start(spx, &rval) != 0) {
4403 mutex_exit(cport_mutex);
4404 return (rval);
4405 }
4406 if (scmd->satacmd_error_reg != 0) {
4407 goto err_out;
4408 }
4409 }
4410 sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4411 break;
4412 case 0x3:
4413 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4414 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4415 if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4416 if (sata_hba_start(spx, &rval) != 0) {
4417 mutex_exit(cport_mutex);
4418 return (rval);
4419 }
4420 if (scmd->satacmd_error_reg != 0) {
4421 goto err_out;
4422 }
4423 }
4424 sata_build_generic_cmd(scmd, SATAC_STANDBY);
4425 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4426 if (sata_hba_start(spx, &rval) != 0) {
4427 mutex_exit(cport_mutex);
4428 return (rval);
4429 }
4430 if (scmd->satacmd_error_reg != 0) {
4431 goto err_out;
4432 }
4433 sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
4434 break;
4435 case 0x7:
4436 sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
4437 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
4438 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4439 if (sata_hba_start(spx, &rval) != 0) {
4440 mutex_exit(cport_mutex);
4441 return (rval);
4442 }
4443 if (scmd->satacmd_error_reg != 0) {
4444 goto err_out;
4445 }
4446 switch (scmd->satacmd_sec_count_lsb) {
4447 case SATA_PWRMODE_STANDBY:
4448 sata_build_generic_cmd(scmd, SATAC_STANDBY);
4449 scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4450 sdinfo->satadrv_standby_timer);
4451 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4452 if (sata_hba_start(spx, &rval) != 0) {
4453 mutex_exit(cport_mutex);
4454 return (rval);
4455 } else {
4456 if (scmd->satacmd_error_reg != 0) {
4457 goto err_out;
4458 }
4459 }
4460 break;
4461 case SATA_PWRMODE_IDLE:
4462 sata_build_generic_cmd(scmd, SATAC_IDLE);
4463 scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4464 sdinfo->satadrv_standby_timer);
4465 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4466 if (sata_hba_start(spx, &rval) != 0) {
4467 mutex_exit(cport_mutex);
4468 return (rval);
4469 } else {
4470 if (scmd->satacmd_error_reg != 0) {
4471 goto err_out;
4472 }
4473 }
4474 break;
4475 case SATA_PWRMODE_ACTIVE_SPINDOWN:
4476 case SATA_PWRMODE_ACTIVE_SPINUP:
4477 case SATA_PWRMODE_ACTIVE:
4478 sata_build_generic_cmd(scmd, SATAC_IDLE);
4479 scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4480 sdinfo->satadrv_standby_timer);
4481 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4482 if (sata_hba_start(spx, &rval) != 0) {
4483 mutex_exit(cport_mutex);
4484 return (rval);
4485 }
4486 if (scmd->satacmd_error_reg != 0) {
4487 goto err_out;
4488 }
4489 sata_build_read_verify_cmd(scmd, 1, 5);
4490 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4491 if (sata_hba_start(spx, &rval) != 0) {
4492 mutex_exit(cport_mutex);
4493 return (rval);
4494 }
4495 if (scmd->satacmd_error_reg != 0) {
4496 goto err_out;
4497 }
4498 break;
4499 default:
4500 goto err_out;
4501 }
4502 break;
4503 case 0xb:
4504 if ((sata_get_standby_timer(sdinfo->satadrv_standby_timer) ==
4505 0) || (!(sata_id->ai_cap & SATA_STANDBYTIMER))) {
4506 mutex_exit(cport_mutex);
4507 return (sata_txlt_check_condition(spx,
4508 KEY_ILLEGAL_REQUEST,
4509 SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4510 }
4511 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4512 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4513 if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4514 if (sata_hba_start(spx, &rval) != 0) {
4515 mutex_exit(cport_mutex);
4516 return (rval);
4517 }
4518 if (scmd->satacmd_error_reg != 0) {
4519 goto err_out;
4520 }
4521 sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
4522 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4523 if (sata_hba_start(spx, &rval) != 0) {
4524 mutex_exit(cport_mutex);
4525 return (rval);
4526 }
4527 if (scmd->satacmd_error_reg != 0) {
4528 goto err_out;
4529 }
4530 }
4531 bzero(sdinfo->satadrv_standby_timer, sizeof (uchar_t) * 4);
4532 break;
4533 default:
4534 err_out:
4535 mutex_exit(cport_mutex);
4536 return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4537 SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4538 }
4539
4540 /*
4541 * Since it was a synchronous command,
4542 * a callback function will be called directly.
4543 */
4544 mutex_exit(cport_mutex);
4545 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4546 "synchronous execution status %x\n",
4547 spx->txlt_sata_pkt->satapkt_reason);
4548
4549 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4550 scsipkt->pkt_comp != NULL) {
4551 sata_set_arq_data(spx->txlt_sata_pkt);
4552 if (servicing_interrupt()) {
4553 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4554 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4555 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
4556 TASKQID_INVALID) {
4557 return (TRAN_BUSY);
4558 }
4559 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4560 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4561 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
4562 /* Scheduling the callback failed */
4563 return (TRAN_BUSY);
4564 }
4565 }
4566 else
4567
4568 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
4569
4570 return (TRAN_ACCEPT);
4571
4572 }
4573
4574 /*
4575 * SATA translate command: Read Capacity.
4576 * Emulated command for SATA disks.
4577 * Capacity is retrieved from cached Idenifty Device data.
4578 * Identify Device data shows effective disk capacity, not the native
4579 * capacity, which may be limitted by Set Max Address command.
4580 * This is ATA version for SATA hard disks.
4581 *
4582 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4583 */
4584 static int
sata_txlt_read_capacity(sata_pkt_txlate_t * spx)4585 sata_txlt_read_capacity(sata_pkt_txlate_t *spx)
4586 {
4587 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4588 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4589 sata_drive_info_t *sdinfo;
4590 uint64_t val;
4591 uint32_t lbsize = DEV_BSIZE;
4592 uchar_t *rbuf;
4593 int rval, reason;
4594 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4595
4596 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4597 "sata_txlt_read_capacity: ", NULL);
4598
4599 mutex_enter(cport_mutex);
4600
4601 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4602 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4603 mutex_exit(cport_mutex);
4604 return (rval);
4605 }
4606
4607 scsipkt->pkt_reason = CMD_CMPLT;
4608 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4609 STATE_SENT_CMD | STATE_GOT_STATUS;
4610 *scsipkt->pkt_scbp = STATUS_GOOD;
4611 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4612 /*
4613 * Because it is fully emulated command storing data
4614 * programatically in the specified buffer, release
4615 * preallocated DMA resources before storing data in the buffer,
4616 * so no unwanted DMA sync would take place.
4617 */
4618 sata_scsi_dmafree(NULL, scsipkt);
4619
4620 sdinfo = sata_get_device_info(
4621 spx->txlt_sata_hba_inst,
4622 &spx->txlt_sata_pkt->satapkt_device);
4623
4624 /*
4625 * As per SBC-3, the "returned LBA" is either the highest
4626 * addressable LBA or 0xffffffff, whichever is smaller.
4627 */
4628 val = MIN(sdinfo->satadrv_capacity - 1, UINT32_MAX);
4629
4630 if (sdinfo->satadrv_id.ai_phys_sect_sz & SATA_L2PS_CHECK_BIT) {
4631 /* physical/logical sector size word is valid */
4632
4633 if (sdinfo->satadrv_id.ai_phys_sect_sz &
4634 SATA_L2PS_BIG_SECTORS) {
4635 /* if this set 117-118 words are valid */
4636 lbsize = sdinfo->satadrv_id.ai_words_lsec[0] |
4637 (sdinfo->satadrv_id.ai_words_lsec[1] << 16);
4638 lbsize <<= 1; /* convert from words to bytes */
4639 }
4640 }
4641 rbuf = (uchar_t *)bp->b_un.b_addr;
4642 /* Need to swap endians to match scsi format */
4643 rbuf[0] = (val >> 24) & 0xff;
4644 rbuf[1] = (val >> 16) & 0xff;
4645 rbuf[2] = (val >> 8) & 0xff;
4646 rbuf[3] = val & 0xff;
4647 rbuf[4] = (lbsize >> 24) & 0xff;
4648 rbuf[5] = (lbsize >> 16) & 0xff;
4649 rbuf[6] = (lbsize >> 8) & 0xff;
4650 rbuf[7] = lbsize & 0xff;
4651
4652 scsipkt->pkt_state |= STATE_XFERRED_DATA;
4653 scsipkt->pkt_resid = 0;
4654
4655 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n",
4656 sdinfo->satadrv_capacity -1);
4657 }
4658 mutex_exit(cport_mutex);
4659 /*
4660 * If a callback was requested, do it now.
4661 */
4662 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4663 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4664
4665 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4666 scsipkt->pkt_comp != NULL) {
4667 /* scsi callback required */
4668 if (servicing_interrupt()) {
4669 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4670 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4671 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
4672 TASKQID_INVALID) {
4673 return (TRAN_BUSY);
4674 }
4675 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4676 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4677 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
4678 /* Scheduling the callback failed */
4679 return (TRAN_BUSY);
4680 }
4681 }
4682
4683 return (TRAN_ACCEPT);
4684 }
4685
4686 /*
4687 * SATA translate command: Read Capacity (16).
4688 * Emulated command for SATA disks.
4689 * Info is retrieved from cached Identify Device data.
4690 * Implemented to SBC-3 (draft 21) and SAT-2 (final) specifications.
4691 *
4692 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4693 */
4694 static int
sata_txlt_read_capacity16(sata_pkt_txlate_t * spx)4695 sata_txlt_read_capacity16(sata_pkt_txlate_t *spx)
4696 {
4697 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4698 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4699 sata_drive_info_t *sdinfo;
4700 uint64_t val;
4701 uint16_t l2p_exp;
4702 uint32_t lbsize = DEV_BSIZE;
4703 uchar_t *rbuf;
4704 int rval, reason;
4705 #define TPE 0x80
4706 #define TPRZ 0x40
4707 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4708
4709 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4710 "sata_txlt_read_capacity: ", NULL);
4711
4712 mutex_enter(cport_mutex);
4713
4714 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4715 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4716 mutex_exit(cport_mutex);
4717 return (rval);
4718 }
4719
4720 scsipkt->pkt_reason = CMD_CMPLT;
4721 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4722 STATE_SENT_CMD | STATE_GOT_STATUS;
4723 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4724 /*
4725 * Because it is fully emulated command storing data
4726 * programatically in the specified buffer, release
4727 * preallocated DMA resources before storing data in the buffer,
4728 * so no unwanted DMA sync would take place.
4729 */
4730 sata_scsi_dmafree(NULL, scsipkt);
4731
4732 /* Check SERVICE ACTION field */
4733 if ((scsipkt->pkt_cdbp[1] & 0x1f) !=
4734 SSVC_ACTION_READ_CAPACITY_G4) {
4735 mutex_exit(cport_mutex);
4736 return (sata_txlt_check_condition(spx,
4737 KEY_ILLEGAL_REQUEST,
4738 SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4739 }
4740
4741 /* Check LBA field */
4742 if ((scsipkt->pkt_cdbp[2] != 0) ||
4743 (scsipkt->pkt_cdbp[3] != 0) ||
4744 (scsipkt->pkt_cdbp[4] != 0) ||
4745 (scsipkt->pkt_cdbp[5] != 0) ||
4746 (scsipkt->pkt_cdbp[6] != 0) ||
4747 (scsipkt->pkt_cdbp[7] != 0) ||
4748 (scsipkt->pkt_cdbp[8] != 0) ||
4749 (scsipkt->pkt_cdbp[9] != 0)) {
4750 mutex_exit(cport_mutex);
4751 return (sata_txlt_check_condition(spx,
4752 KEY_ILLEGAL_REQUEST,
4753 SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4754 }
4755
4756 /* Check PMI bit */
4757 if (scsipkt->pkt_cdbp[14] & 0x1) {
4758 mutex_exit(cport_mutex);
4759 return (sata_txlt_check_condition(spx,
4760 KEY_ILLEGAL_REQUEST,
4761 SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4762 }
4763
4764 *scsipkt->pkt_scbp = STATUS_GOOD;
4765
4766 sdinfo = sata_get_device_info(
4767 spx->txlt_sata_hba_inst,
4768 &spx->txlt_sata_pkt->satapkt_device);
4769
4770 /* last logical block address */
4771 val = MIN(sdinfo->satadrv_capacity - 1,
4772 SCSI_READ_CAPACITY16_MAX_LBA);
4773
4774 /* logical to physical block size exponent */
4775 l2p_exp = 0;
4776 if (sdinfo->satadrv_id.ai_phys_sect_sz & SATA_L2PS_CHECK_BIT) {
4777 /* physical/logical sector size word is valid */
4778
4779 if (sdinfo->satadrv_id.ai_phys_sect_sz &
4780 SATA_L2PS_HAS_MULT) {
4781 /* multiple logical sectors per phys sectors */
4782 l2p_exp =
4783 sdinfo->satadrv_id.ai_phys_sect_sz &
4784 SATA_L2PS_EXP_MASK;
4785 }
4786
4787 if (sdinfo->satadrv_id.ai_phys_sect_sz &
4788 SATA_L2PS_BIG_SECTORS) {
4789 /* if this set 117-118 words are valid */
4790 lbsize = sdinfo->satadrv_id.ai_words_lsec[0] |
4791 (sdinfo->satadrv_id.ai_words_lsec[1] << 16);
4792 lbsize <<= 1; /* convert from words to bytes */
4793 }
4794 }
4795
4796 rbuf = (uchar_t *)bp->b_un.b_addr;
4797 bzero(rbuf, bp->b_bcount);
4798
4799 /* returned logical block address */
4800 rbuf[0] = (val >> 56) & 0xff;
4801 rbuf[1] = (val >> 48) & 0xff;
4802 rbuf[2] = (val >> 40) & 0xff;
4803 rbuf[3] = (val >> 32) & 0xff;
4804 rbuf[4] = (val >> 24) & 0xff;
4805 rbuf[5] = (val >> 16) & 0xff;
4806 rbuf[6] = (val >> 8) & 0xff;
4807 rbuf[7] = val & 0xff;
4808 rbuf[8] = (lbsize >> 24) & 0xff;
4809 rbuf[9] = (lbsize >> 16) & 0xff;
4810 rbuf[10] = (lbsize >> 8) & 0xff;
4811 rbuf[11] = lbsize & 0xff;
4812
4813 /* p_type, prot_en, unspecified by SAT-2 */
4814 /* rbuf[12] = 0; */
4815
4816 /* p_i_exponent, undefined by SAT-2 */
4817 /* logical blocks per physical block exponent */
4818 rbuf[13] = l2p_exp;
4819
4820 /*
4821 * tpe and tprz as defined in T10/10-079 r0.
4822 * TRIM support is indicated by the relevant bit in the data
4823 * set management word. Read-after-trim behavior is indicated
4824 * by the additional bits in the identify device word. Of the
4825 * three defined possibilities, we only flag read-zero.
4826 */
4827 if (sdinfo->satadrv_id.ai_dsm & SATA_DSM_TRIM) {
4828 rbuf[14] |= TPE;
4829
4830 if ((sdinfo->satadrv_id.ai_addsupported &
4831 SATA_DETERMINISTIC_READ) &&
4832 (sdinfo->satadrv_id.ai_addsupported &
4833 SATA_READ_ZERO)) {
4834 rbuf[14] |= TPRZ;
4835 }
4836 }
4837
4838 /* lowest aligned logical block address = 0 (for now) */
4839 /* rbuf[15] = 0; */
4840
4841 scsipkt->pkt_state |= STATE_XFERRED_DATA;
4842 scsipkt->pkt_resid = 0;
4843
4844 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%llu\n",
4845 sdinfo->satadrv_capacity -1);
4846 }
4847
4848 mutex_exit(cport_mutex);
4849
4850 /*
4851 * If a callback was requested, do it now.
4852 */
4853 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4854 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4855
4856 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4857 scsipkt->pkt_comp != NULL) {
4858 /* scsi callback required */
4859 if (servicing_interrupt()) {
4860 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4861 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4862 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
4863 TASKQID_INVALID) {
4864 return (TRAN_BUSY);
4865 }
4866 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4867 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4868 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
4869 /* Scheduling the callback failed */
4870 return (TRAN_BUSY);
4871 }
4872 }
4873
4874 return (TRAN_ACCEPT);
4875 }
4876
4877 /*
4878 * Translate command: UNMAP
4879 *
4880 * The function cannot be called in interrupt context since it may sleep.
4881 */
4882 static int
sata_txlt_unmap(sata_pkt_txlate_t * spx)4883 sata_txlt_unmap(sata_pkt_txlate_t *spx)
4884 {
4885 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4886 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4887 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4888 uint16_t count = 0;
4889 int synch;
4890 int rval, reason;
4891 int i, x;
4892 int bdlen = 0;
4893 int ranges = 0;
4894 int paramlen = 8;
4895 uint8_t *data, *tmpbd;
4896 sata_drive_info_t *sdinfo;
4897 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4898 #define TRIM 0x1
4899
4900 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4901 "sata_txlt_unmap: ", NULL);
4902
4903 mutex_enter(cport_mutex);
4904
4905 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4906 &spx->txlt_sata_pkt->satapkt_device);
4907 if (sdinfo != NULL) {
4908 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4909 "DSM support 0x%x, max number of 512 byte blocks of LBA "
4910 "range entries 0x%x\n", sdinfo->satadrv_id.ai_dsm,
4911 sdinfo->satadrv_id.ai_maxcount);
4912 }
4913
4914 rval = sata_txlt_generic_pkt_info(spx, &reason, 1);
4915 if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4916 mutex_exit(cport_mutex);
4917 return (rval);
4918 }
4919
4920 /*
4921 * Need to modify bp to have TRIM data instead of UNMAP data.
4922 * Start by getting the block descriptor data length by subtracting
4923 * the 8 byte parameter list header from the parameter list length.
4924 * The block descriptor size has to be a multiple of 16 bytes.
4925 */
4926 bdlen = scsipkt->pkt_cdbp[7];
4927 bdlen = (bdlen << 8) + scsipkt->pkt_cdbp[8] - paramlen;
4928 if ((bdlen < 0) || ((bdlen % 16) != 0) ||
4929 ((bp != NULL) && (bdlen > (bp->b_bcount - paramlen)))) {
4930 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4931 "sata_txlt_unmap: invalid block descriptor length", NULL);
4932 mutex_exit(cport_mutex);
4933 return ((sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4934 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)));
4935 }
4936 /*
4937 * If there are no parameter data or block descriptors, it is not
4938 * considered an error so just complete the command without sending
4939 * TRIM.
4940 */
4941 if ((bdlen == 0) || (bp == NULL) || (bp->b_un.b_addr == NULL) ||
4942 (bp->b_bcount == 0)) {
4943 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4944 "sata_txlt_unmap: no parameter data or block descriptors",
4945 NULL);
4946 mutex_exit(cport_mutex);
4947 return (sata_txlt_unmap_nodata_cmd(spx));
4948 }
4949 tmpbd = (uint8_t *)bp->b_un.b_addr + paramlen;
4950 data = kmem_zalloc(bdlen, KM_SLEEP);
4951
4952 /*
4953 * Loop through all the UNMAP block descriptors and convert the data
4954 * into TRIM format.
4955 */
4956 for (i = 0, x = 0; i < bdlen; i += 16, x += 8) {
4957 /* get range length */
4958 data[x] = tmpbd[i+7];
4959 data[x+1] = tmpbd[i+6];
4960 /* get LBA */
4961 data[x+2] = tmpbd[i+5];
4962 data[x+3] = tmpbd[i+4];
4963 data[x+4] = tmpbd[i+3];
4964 data[x+5] = tmpbd[i+2];
4965 data[x+6] = tmpbd[i+11];
4966 data[x+7] = tmpbd[i+10];
4967
4968 ranges++;
4969 }
4970
4971 /*
4972 * The TRIM command expects the data buffer to be a multiple of
4973 * 512-byte blocks of range entries. This means that the UNMAP buffer
4974 * may be too small. Free the original DMA resources and create a
4975 * local buffer.
4976 */
4977 sata_common_free_dma_rsrcs(spx);
4978
4979 /*
4980 * Get count of 512-byte blocks of range entries. The length
4981 * of a range entry is 8 bytes which means one count has 64 range
4982 * entries.
4983 */
4984 count = (ranges + 63)/64;
4985
4986 /* Allocate a buffer that is a multiple of 512 bytes. */
4987 mutex_exit(cport_mutex);
4988 bp = sata_alloc_local_buffer(spx, (size_t)count * 512);
4989 if (bp == NULL) {
4990 SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
4991 "sata_txlt_unmap: "
4992 "cannot allocate buffer for TRIM command", NULL);
4993 kmem_free(data, bdlen);
4994 return (TRAN_BUSY);
4995 }
4996 bp_mapin(bp); /* make data buffer accessible */
4997 mutex_enter(cport_mutex);
4998
4999 bzero(bp->b_un.b_addr, bp->b_bcount);
5000 bcopy(data, bp->b_un.b_addr, x);
5001 kmem_free(data, bdlen);
5002 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
5003 DDI_DMA_SYNC_FORDEV);
5004 ASSERT(rval == DDI_SUCCESS);
5005
5006 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
5007 scmd->satacmd_addr_type = ATA_ADDR_LBA48;
5008 scmd->satacmd_cmd_reg = SATAC_DSM;
5009 scmd->satacmd_sec_count_msb = (count >> 8) & 0xff;
5010 scmd->satacmd_sec_count_lsb = count & 0xff;
5011 scmd->satacmd_features_reg = TRIM;
5012 scmd->satacmd_device_reg = SATA_ADH_LBA;
5013 scmd->satacmd_status_reg = 0;
5014 scmd->satacmd_error_reg = 0;
5015
5016 /* Start processing command */
5017 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5018 spx->txlt_sata_pkt->satapkt_comp =
5019 sata_txlt_unmap_completion;
5020 synch = FALSE;
5021 } else {
5022 synch = TRUE;
5023 }
5024
5025 if (sata_hba_start(spx, &rval) != 0) {
5026 mutex_exit(cport_mutex);
5027 return (rval);
5028 }
5029
5030 mutex_exit(cport_mutex);
5031
5032 if (synch) {
5033 sata_txlt_unmap_completion(spx->txlt_sata_pkt);
5034 }
5035
5036 return (TRAN_ACCEPT);
5037 }
5038
5039 /*
5040 * SATA translate command: Mode Sense.
5041 * Translated into appropriate SATA command or emulated.
5042 * Saved Values Page Control (03) are not supported.
5043 *
5044 * NOTE: only caching mode sense page is currently implemented.
5045 *
5046 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
5047 */
5048
5049 #define LLBAA 0x10 /* Long LBA Accepted */
5050
5051 static int
sata_txlt_mode_sense(sata_pkt_txlate_t * spx)5052 sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
5053 {
5054 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5055 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5056 sata_drive_info_t *sdinfo;
5057 sata_id_t *sata_id;
5058 struct scsi_extended_sense *sense;
5059 int len, bdlen, count, alc_len;
5060 int pc; /* Page Control code */
5061 uint8_t *buf; /* mode sense buffer */
5062 int rval, reason;
5063 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5064
5065 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5066 "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
5067 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5068 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5069
5070 if (servicing_interrupt()) {
5071 buf = kmem_zalloc(1024, KM_NOSLEEP);
5072 if (buf == NULL) {
5073 return (TRAN_BUSY);
5074 }
5075 } else {
5076 buf = kmem_zalloc(1024, KM_SLEEP);
5077 }
5078
5079 mutex_enter(cport_mutex);
5080
5081 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
5082 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5083 mutex_exit(cport_mutex);
5084 kmem_free(buf, 1024);
5085 return (rval);
5086 }
5087
5088 scsipkt->pkt_reason = CMD_CMPLT;
5089 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5090 STATE_SENT_CMD | STATE_GOT_STATUS;
5091
5092 pc = scsipkt->pkt_cdbp[2] >> 6;
5093
5094 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
5095 /*
5096 * Because it is fully emulated command storing data
5097 * programatically in the specified buffer, release
5098 * preallocated DMA resources before storing data in the buffer,
5099 * so no unwanted DMA sync would take place.
5100 */
5101 sata_scsi_dmafree(NULL, scsipkt);
5102
5103 len = 0;
5104 bdlen = 0;
5105 if (!(scsipkt->pkt_cdbp[1] & 8)) {
5106 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
5107 (scsipkt->pkt_cdbp[1] & LLBAA))
5108 bdlen = 16;
5109 else
5110 bdlen = 8;
5111 }
5112 /* Build mode parameter header */
5113 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5114 /* 4-byte mode parameter header */
5115 buf[len++] = 0; /* mode data length */
5116 buf[len++] = 0; /* medium type */
5117 buf[len++] = 0; /* dev-specific param */
5118 buf[len++] = bdlen; /* Block Descriptor length */
5119 } else {
5120 /* 8-byte mode parameter header */
5121 buf[len++] = 0; /* mode data length */
5122 buf[len++] = 0;
5123 buf[len++] = 0; /* medium type */
5124 buf[len++] = 0; /* dev-specific param */
5125 if (bdlen == 16)
5126 buf[len++] = 1; /* long lba descriptor */
5127 else
5128 buf[len++] = 0;
5129 buf[len++] = 0;
5130 buf[len++] = 0; /* Block Descriptor length */
5131 buf[len++] = bdlen;
5132 }
5133
5134 sdinfo = sata_get_device_info(
5135 spx->txlt_sata_hba_inst,
5136 &spx->txlt_sata_pkt->satapkt_device);
5137
5138 /* Build block descriptor only if not disabled (DBD) */
5139 if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
5140 /* Block descriptor - direct-access device format */
5141 if (bdlen == 8) {
5142 /* build regular block descriptor */
5143 buf[len++] =
5144 (sdinfo->satadrv_capacity >> 24) & 0xff;
5145 buf[len++] =
5146 (sdinfo->satadrv_capacity >> 16) & 0xff;
5147 buf[len++] =
5148 (sdinfo->satadrv_capacity >> 8) & 0xff;
5149 buf[len++] = sdinfo->satadrv_capacity & 0xff;
5150 buf[len++] = 0; /* density code */
5151 buf[len++] = 0;
5152 if (sdinfo->satadrv_type ==
5153 SATA_DTYPE_ATADISK)
5154 buf[len++] = 2;
5155 else
5156 /* ATAPI */
5157 buf[len++] = 8;
5158 buf[len++] = 0;
5159 } else if (bdlen == 16) {
5160 /* Long LBA Accepted */
5161 /* build long lba block descriptor */
5162 #ifndef __lock_lint
5163 buf[len++] =
5164 (sdinfo->satadrv_capacity >> 56) & 0xff;
5165 buf[len++] =
5166 (sdinfo->satadrv_capacity >> 48) & 0xff;
5167 buf[len++] =
5168 (sdinfo->satadrv_capacity >> 40) & 0xff;
5169 buf[len++] =
5170 (sdinfo->satadrv_capacity >> 32) & 0xff;
5171 #endif
5172 buf[len++] =
5173 (sdinfo->satadrv_capacity >> 24) & 0xff;
5174 buf[len++] =
5175 (sdinfo->satadrv_capacity >> 16) & 0xff;
5176 buf[len++] =
5177 (sdinfo->satadrv_capacity >> 8) & 0xff;
5178 buf[len++] = sdinfo->satadrv_capacity & 0xff;
5179 buf[len++] = 0;
5180 buf[len++] = 0; /* density code */
5181 buf[len++] = 0;
5182 buf[len++] = 0;
5183 if (sdinfo->satadrv_type ==
5184 SATA_DTYPE_ATADISK)
5185 buf[len++] = 2;
5186 else
5187 /* ATAPI */
5188 buf[len++] = 8;
5189 buf[len++] = 0;
5190 }
5191 }
5192
5193 sata_id = &sdinfo->satadrv_id;
5194
5195 /*
5196 * Add requested pages.
5197 * Page 3 and 4 are obsolete and we are not supporting them.
5198 * We deal now with:
5199 * caching (read/write cache control).
5200 * We should eventually deal with following mode pages:
5201 * error recovery (0x01),
5202 * power condition (0x1a),
5203 * exception control page (enables SMART) (0x1c),
5204 * enclosure management (ses),
5205 * protocol-specific port mode (port control).
5206 */
5207 switch (scsipkt->pkt_cdbp[2] & 0x3f) {
5208 case MODEPAGE_RW_ERRRECOV:
5209 /* DAD_MODE_ERR_RECOV */
5210 /* R/W recovery */
5211 len += sata_build_msense_page_1(sdinfo, pc, buf+len);
5212 break;
5213 case MODEPAGE_CACHING:
5214 /* DAD_MODE_CACHE */
5215 /* Reject not supported request for saved parameters */
5216 if (pc == 3) {
5217 *scsipkt->pkt_scbp = STATUS_CHECK;
5218 sense = sata_arq_sense(spx);
5219 sense->es_key = KEY_ILLEGAL_REQUEST;
5220 sense->es_add_code =
5221 SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
5222 goto done;
5223 }
5224
5225 /* caching */
5226 len += sata_build_msense_page_8(sdinfo, pc, buf+len);
5227 break;
5228 case MODEPAGE_INFO_EXCPT:
5229 /* exception cntrl */
5230 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
5231 len += sata_build_msense_page_1c(sdinfo, pc,
5232 buf+len);
5233 }
5234 else
5235 goto err;
5236 break;
5237 case MODEPAGE_POWER_COND:
5238 /* DAD_MODE_POWER_COND */
5239 /* power condition */
5240 len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
5241 break;
5242
5243 case MODEPAGE_ACOUSTIC_MANAG:
5244 /* acoustic management */
5245 len += sata_build_msense_page_30(sdinfo, pc, buf+len);
5246 break;
5247 case MODEPAGE_ALLPAGES:
5248 /* all pages */
5249 len += sata_build_msense_page_1(sdinfo, pc, buf+len);
5250 len += sata_build_msense_page_8(sdinfo, pc, buf+len);
5251 len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
5252 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
5253 len += sata_build_msense_page_1c(sdinfo, pc,
5254 buf+len);
5255 }
5256 len += sata_build_msense_page_30(sdinfo, pc, buf+len);
5257 break;
5258 default:
5259 err:
5260 /* Invalid request */
5261 *scsipkt->pkt_scbp = STATUS_CHECK;
5262 sense = sata_arq_sense(spx);
5263 sense->es_key = KEY_ILLEGAL_REQUEST;
5264 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5265 goto done;
5266 }
5267
5268 /* fix total mode data length */
5269 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5270 /* 4-byte mode parameter header */
5271 buf[0] = len - 1; /* mode data length */
5272 } else {
5273 buf[0] = (len -2) >> 8;
5274 buf[1] = (len -2) & 0xff;
5275 }
5276
5277
5278 /* Check allocation length */
5279 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5280 alc_len = scsipkt->pkt_cdbp[4];
5281 } else {
5282 alc_len = scsipkt->pkt_cdbp[7];
5283 alc_len = (alc_len << 8) | scsipkt->pkt_cdbp[8];
5284 }
5285 /*
5286 * We do not check for possible parameters truncation
5287 * (alc_len < len) assuming that the target driver works
5288 * correctly. Just avoiding overrun.
5289 * Copy no more than requested and possible, buffer-wise.
5290 */
5291 count = MIN(alc_len, len);
5292 count = MIN(bp->b_bcount, count);
5293 bcopy(buf, bp->b_un.b_addr, count);
5294
5295 scsipkt->pkt_state |= STATE_XFERRED_DATA;
5296 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
5297 }
5298 *scsipkt->pkt_scbp = STATUS_GOOD;
5299 done:
5300 mutex_exit(cport_mutex);
5301 (void) kmem_free(buf, 1024);
5302
5303 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5304 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5305
5306 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5307 scsipkt->pkt_comp != NULL) {
5308 /* scsi callback required */
5309 if (servicing_interrupt()) {
5310 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5311 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5312 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
5313 TASKQID_INVALID) {
5314 return (TRAN_BUSY);
5315 }
5316 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5317 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5318 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
5319 /* Scheduling the callback failed */
5320 return (TRAN_BUSY);
5321 }
5322 }
5323
5324 return (TRAN_ACCEPT);
5325 }
5326
5327
5328 /*
5329 * SATA translate command: Mode Select.
5330 * Translated into appropriate SATA command or emulated.
5331 * Saving parameters is not supported.
5332 * Changing device capacity is not supported (although theoretically
5333 * possible by executing SET FEATURES/SET MAX ADDRESS)
5334 *
5335 * Assumption is that the target driver is working correctly.
5336 *
5337 * More than one SATA command may be executed to perform operations specified
5338 * by mode select pages. The first error terminates further execution.
5339 * Operations performed successully are not backed-up in such case.
5340 *
5341 * NOTE: Implemented pages:
5342 * - caching page
5343 * - informational exception page
5344 * - acoustic management page
5345 * - power condition page
5346 * Caching setup is remembered so it could be re-stored in case of
5347 * an unexpected device reset.
5348 *
5349 * Returns TRAN_XXXX.
5350 * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields.
5351 */
5352
5353 static int
sata_txlt_mode_select(sata_pkt_txlate_t * spx)5354 sata_txlt_mode_select(sata_pkt_txlate_t *spx)
5355 {
5356 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5357 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5358 struct scsi_extended_sense *sense;
5359 int len, pagelen, count, pllen;
5360 uint8_t *buf; /* mode select buffer */
5361 int rval, stat, reason;
5362 uint_t nointr_flag;
5363 int dmod = 0;
5364 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5365
5366 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5367 "sata_txlt_mode_select, pc %x page code 0x%02x\n",
5368 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5369 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5370
5371 mutex_enter(cport_mutex);
5372
5373 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
5374 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5375 mutex_exit(cport_mutex);
5376 return (rval);
5377 }
5378
5379 rval = TRAN_ACCEPT;
5380
5381 scsipkt->pkt_reason = CMD_CMPLT;
5382 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5383 STATE_SENT_CMD | STATE_GOT_STATUS;
5384 nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
5385
5386 /* Reject not supported request */
5387 if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
5388 *scsipkt->pkt_scbp = STATUS_CHECK;
5389 sense = sata_arq_sense(spx);
5390 sense->es_key = KEY_ILLEGAL_REQUEST;
5391 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5392 goto done;
5393 }
5394
5395 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
5396 pllen = scsipkt->pkt_cdbp[4];
5397 } else {
5398 pllen = scsipkt->pkt_cdbp[7];
5399 pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
5400 }
5401
5402 *scsipkt->pkt_scbp = STATUS_GOOD; /* Presumed outcome */
5403
5404 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
5405 buf = (uint8_t *)bp->b_un.b_addr;
5406 count = MIN(bp->b_bcount, pllen);
5407 scsipkt->pkt_state |= STATE_XFERRED_DATA;
5408 scsipkt->pkt_resid = 0;
5409 pllen = count;
5410
5411 /*
5412 * Check the header to skip the block descriptor(s) - we
5413 * do not support setting device capacity.
5414 * Existing macros do not recognize long LBA dscriptor,
5415 * hence manual calculation.
5416 */
5417 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
5418 /* 6-bytes CMD, 4 bytes header */
5419 if (count <= 4)
5420 goto done; /* header only */
5421 len = buf[3] + 4;
5422 } else {
5423 /* 10-bytes CMD, 8 bytes header */
5424 if (count <= 8)
5425 goto done; /* header only */
5426 len = buf[6];
5427 len = (len << 8) + buf[7] + 8;
5428 }
5429 if (len >= count)
5430 goto done; /* header + descriptor(s) only */
5431
5432 pllen -= len; /* remaining data length */
5433
5434 /*
5435 * We may be executing SATA command and want to execute it
5436 * in SYNCH mode, regardless of scsi_pkt setting.
5437 * Save scsi_pkt setting and indicate SYNCH mode
5438 */
5439 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5440 scsipkt->pkt_comp != NULL) {
5441 scsipkt->pkt_flags |= FLAG_NOINTR;
5442 }
5443 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
5444
5445 /*
5446 * len is now the offset to a first mode select page
5447 * Process all pages
5448 */
5449 while (pllen > 0) {
5450 switch ((int)buf[len]) {
5451 case MODEPAGE_CACHING:
5452 /* No support for SP (saving) */
5453 if (scsipkt->pkt_cdbp[1] & 0x01) {
5454 *scsipkt->pkt_scbp = STATUS_CHECK;
5455 sense = sata_arq_sense(spx);
5456 sense->es_key = KEY_ILLEGAL_REQUEST;
5457 sense->es_add_code =
5458 SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5459 goto done;
5460 }
5461 stat = sata_mode_select_page_8(spx,
5462 (struct mode_cache_scsi3 *)&buf[len],
5463 pllen, &pagelen, &rval, &dmod);
5464 /*
5465 * The pagelen value indicates the number of
5466 * parameter bytes already processed.
5467 * The rval is the return value from
5468 * sata_tran_start().
5469 * The stat indicates the overall status of
5470 * the operation(s).
5471 */
5472 if (stat != SATA_SUCCESS)
5473 /*
5474 * Page processing did not succeed -
5475 * all error info is already set-up,
5476 * just return
5477 */
5478 pllen = 0; /* this breaks the loop */
5479 else {
5480 len += pagelen;
5481 pllen -= pagelen;
5482 }
5483 break;
5484
5485 case MODEPAGE_INFO_EXCPT:
5486 stat = sata_mode_select_page_1c(spx,
5487 (struct mode_info_excpt_page *)&buf[len],
5488 pllen, &pagelen, &rval, &dmod);
5489 /*
5490 * The pagelen value indicates the number of
5491 * parameter bytes already processed.
5492 * The rval is the return value from
5493 * sata_tran_start().
5494 * The stat indicates the overall status of
5495 * the operation(s).
5496 */
5497 if (stat != SATA_SUCCESS)
5498 /*
5499 * Page processing did not succeed -
5500 * all error info is already set-up,
5501 * just return
5502 */
5503 pllen = 0; /* this breaks the loop */
5504 else {
5505 len += pagelen;
5506 pllen -= pagelen;
5507 }
5508 break;
5509
5510 case MODEPAGE_ACOUSTIC_MANAG:
5511 stat = sata_mode_select_page_30(spx,
5512 (struct mode_acoustic_management *)
5513 &buf[len], pllen, &pagelen, &rval, &dmod);
5514 /*
5515 * The pagelen value indicates the number of
5516 * parameter bytes already processed.
5517 * The rval is the return value from
5518 * sata_tran_start().
5519 * The stat indicates the overall status of
5520 * the operation(s).
5521 */
5522 if (stat != SATA_SUCCESS)
5523 /*
5524 * Page processing did not succeed -
5525 * all error info is already set-up,
5526 * just return
5527 */
5528 pllen = 0; /* this breaks the loop */
5529 else {
5530 len += pagelen;
5531 pllen -= pagelen;
5532 }
5533
5534 break;
5535 case MODEPAGE_POWER_COND:
5536 stat = sata_mode_select_page_1a(spx,
5537 (struct mode_info_power_cond *)&buf[len],
5538 pllen, &pagelen, &rval, &dmod);
5539 /*
5540 * The pagelen value indicates the number of
5541 * parameter bytes already processed.
5542 * The rval is the return value from
5543 * sata_tran_start().
5544 * The stat indicates the overall status of
5545 * the operation(s).
5546 */
5547 if (stat != SATA_SUCCESS)
5548 /*
5549 * Page processing did not succeed -
5550 * all error info is already set-up,
5551 * just return
5552 */
5553 pllen = 0; /* this breaks the loop */
5554 else {
5555 len += pagelen;
5556 pllen -= pagelen;
5557 }
5558 break;
5559 default:
5560 *scsipkt->pkt_scbp = STATUS_CHECK;
5561 sense = sata_arq_sense(spx);
5562 sense->es_key = KEY_ILLEGAL_REQUEST;
5563 sense->es_add_code =
5564 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
5565 goto done;
5566 }
5567 }
5568 }
5569 done:
5570 mutex_exit(cport_mutex);
5571 /*
5572 * If device parameters were modified, fetch and store the new
5573 * Identify Device data. Since port mutex could have been released
5574 * for accessing HBA driver, we need to re-check device existence.
5575 */
5576 if (dmod != 0) {
5577 sata_drive_info_t new_sdinfo, *sdinfo;
5578 int rv = 0;
5579
5580 /*
5581 * Following statement has to be changed if this function is
5582 * used for devices other than SATA hard disks.
5583 */
5584 new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
5585
5586 new_sdinfo.satadrv_addr =
5587 spx->txlt_sata_pkt->satapkt_device.satadev_addr;
5588 rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
5589 &new_sdinfo);
5590
5591 mutex_enter(cport_mutex);
5592 /*
5593 * Since port mutex could have been released when
5594 * accessing HBA driver, we need to re-check that the
5595 * framework still holds the device info structure.
5596 */
5597 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
5598 &spx->txlt_sata_pkt->satapkt_device);
5599 if (sdinfo != NULL) {
5600 /*
5601 * Device still has info structure in the
5602 * sata framework. Copy newly fetched info
5603 */
5604 if (rv == 0) {
5605 sdinfo->satadrv_id = new_sdinfo.satadrv_id;
5606 sata_save_drive_settings(sdinfo);
5607 } else {
5608 /*
5609 * Could not fetch new data - invalidate
5610 * sata_drive_info. That makes device
5611 * unusable.
5612 */
5613 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
5614 sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
5615 }
5616 }
5617 if (rv != 0 || sdinfo == NULL) {
5618 /*
5619 * This changes the overall mode select completion
5620 * reason to a failed one !!!!!
5621 */
5622 *scsipkt->pkt_scbp = STATUS_CHECK;
5623 sense = sata_arq_sense(spx);
5624 scsipkt->pkt_reason = CMD_INCOMPLETE;
5625 rval = TRAN_ACCEPT;
5626 }
5627 mutex_exit(cport_mutex);
5628 }
5629 /* Restore the scsi pkt flags */
5630 scsipkt->pkt_flags &= ~FLAG_NOINTR;
5631 scsipkt->pkt_flags |= nointr_flag;
5632
5633 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5634 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5635
5636 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5637 scsipkt->pkt_comp != NULL) {
5638 /* scsi callback required */
5639 if (servicing_interrupt()) {
5640 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5641 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5642 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
5643 TASKQID_INVALID) {
5644 return (TRAN_BUSY);
5645 }
5646 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5647 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5648 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
5649 /* Scheduling the callback failed */
5650 return (TRAN_BUSY);
5651 }
5652 }
5653
5654 return (rval);
5655 }
5656
5657 /*
5658 * Translate command: ATA Pass Through
5659 * Incomplete implementation. Only supports No-Data, PIO Data-In, and
5660 * PIO Data-Out protocols. Also supports CK_COND bit.
5661 *
5662 * Mapping of the incoming CDB bytes to the outgoing satacmd bytes is
5663 * described in Table 111 of SAT-2 (Draft 9).
5664 */
5665 static int
sata_txlt_ata_pass_thru(sata_pkt_txlate_t * spx)5666 sata_txlt_ata_pass_thru(sata_pkt_txlate_t *spx)
5667 {
5668 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5669 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5670 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5671 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5672 uint32_t xfer_len;
5673 int extend = 0;
5674 int synch, rval, reason;
5675
5676 mutex_enter(cport_mutex);
5677
5678 rval = sata_txlt_generic_pkt_info(spx, &reason, 1);
5679 if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5680 mutex_exit(cport_mutex);
5681 return (rval);
5682 }
5683
5684 /* T_DIR bit */
5685 if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_T_DIR)
5686 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
5687 else
5688 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
5689
5690 /* MULTIPLE_COUNT field. If non-zero, invalid command (for now). */
5691 if (((scsipkt->pkt_cdbp[1] >> 5) & 0x7) != 0) {
5692 mutex_exit(cport_mutex);
5693 return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5694 }
5695
5696 /* OFFLINE field. If non-zero, invalid command (for now). */
5697 if (((scsipkt->pkt_cdbp[2] >> 6) & 0x3) != 0) {
5698 mutex_exit(cport_mutex);
5699 return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5700 }
5701
5702 /* PROTOCOL field */
5703 switch ((scsipkt->pkt_cdbp[1] >> 1) & 0xf) {
5704 case SATL_APT_P_HW_RESET:
5705 case SATL_APT_P_SRST:
5706 case SATL_APT_P_DMA:
5707 case SATL_APT_P_DMA_QUEUED:
5708 case SATL_APT_P_DEV_DIAG:
5709 case SATL_APT_P_DEV_RESET:
5710 case SATL_APT_P_UDMA_IN:
5711 case SATL_APT_P_UDMA_OUT:
5712 case SATL_APT_P_FPDMA:
5713 case SATL_APT_P_RET_RESP:
5714 /* Not yet implemented */
5715 default:
5716 mutex_exit(cport_mutex);
5717 return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5718
5719 case SATL_APT_P_NON_DATA:
5720 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
5721 break;
5722
5723 case SATL_APT_P_PIO_DATA_IN:
5724 /* If PROTOCOL disagrees with T_DIR, invalid command */
5725 if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_WRITE) {
5726 mutex_exit(cport_mutex);
5727 return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5728 }
5729
5730 /* if there is a buffer, release its DMA resources */
5731 if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) {
5732 sata_scsi_dmafree(NULL, scsipkt);
5733 } else {
5734 /* if there is no buffer, how do you PIO in? */
5735 mutex_exit(cport_mutex);
5736 return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5737 }
5738
5739 break;
5740
5741 case SATL_APT_P_PIO_DATA_OUT:
5742 /* If PROTOCOL disagrees with T_DIR, invalid command */
5743 if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_READ) {
5744 mutex_exit(cport_mutex);
5745 return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5746 }
5747
5748 /* if there is a buffer, release its DMA resources */
5749 if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) {
5750 sata_scsi_dmafree(NULL, scsipkt);
5751 } else {
5752 /* if there is no buffer, how do you PIO out? */
5753 mutex_exit(cport_mutex);
5754 return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5755 }
5756
5757 break;
5758 }
5759
5760 /* Assume LBA28 by default */
5761 scmd->satacmd_addr_type = ATA_ADDR_LBA28;
5762 scmd->satacmd_lba_low_msb = 0;
5763 scmd->satacmd_lba_mid_msb = 0;
5764 scmd->satacmd_lba_high_msb = 0;
5765
5766 scmd->satacmd_features_reg_ext = 0;
5767 scmd->satacmd_sec_count_msb = 0;
5768
5769 /* Parse the ATA cmd fields, transfer some straight to the satacmd */
5770 switch ((uint_t)scsipkt->pkt_cdbp[0]) {
5771 case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12:
5772 scmd->satacmd_lba_low_lsb = scsipkt->pkt_cdbp[5];
5773 scmd->satacmd_lba_mid_lsb = scsipkt->pkt_cdbp[6];
5774 scmd->satacmd_lba_high_lsb = scsipkt->pkt_cdbp[7];
5775
5776 scmd->satacmd_features_reg = scsipkt->pkt_cdbp[3];
5777 scmd->satacmd_sec_count_lsb = scsipkt->pkt_cdbp[4];
5778
5779 scmd->satacmd_device_reg = scsipkt->pkt_cdbp[8];
5780 scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[9];
5781 break;
5782
5783 case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16:
5784 scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13];
5785 scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14];
5786
5787 scmd->satacmd_lba_low_lsb = scsipkt->pkt_cdbp[8];
5788 scmd->satacmd_lba_mid_lsb = scsipkt->pkt_cdbp[10];
5789 scmd->satacmd_lba_high_lsb = scsipkt->pkt_cdbp[12];
5790
5791 scmd->satacmd_features_reg = scsipkt->pkt_cdbp[4];
5792 scmd->satacmd_sec_count_lsb = scsipkt->pkt_cdbp[6];
5793
5794 if (scsipkt->pkt_cdbp[1] & SATL_APT_BM_EXTEND) {
5795 extend = 1;
5796
5797 scmd->satacmd_addr_type = ATA_ADDR_LBA48;
5798 scmd->satacmd_lba_low_msb = scsipkt->pkt_cdbp[7];
5799 scmd->satacmd_lba_mid_msb = scsipkt->pkt_cdbp[9];
5800 scmd->satacmd_lba_high_msb = scsipkt->pkt_cdbp[11];
5801
5802 scmd->satacmd_features_reg_ext = scsipkt->pkt_cdbp[3];
5803 scmd->satacmd_sec_count_msb = scsipkt->pkt_cdbp[5];
5804 }
5805 break;
5806
5807 default:
5808 /* No other SCSI ops should ever reach this function */
5809 cmn_err(CE_PANIC, "unexpected ATA pass-thru cmd %x",
5810 scsipkt->pkt_cdbp[0]);
5811 }
5812
5813 /* CK_COND bit */
5814 if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) {
5815 if (extend) {
5816 scmd->satacmd_flags.sata_copy_out_sec_count_msb = 1;
5817 scmd->satacmd_flags.sata_copy_out_lba_low_msb = 1;
5818 scmd->satacmd_flags.sata_copy_out_lba_mid_msb = 1;
5819 scmd->satacmd_flags.sata_copy_out_lba_high_msb = 1;
5820 }
5821
5822 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1;
5823 scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1;
5824 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1;
5825 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1;
5826 scmd->satacmd_flags.sata_copy_out_device_reg = 1;
5827 scmd->satacmd_flags.sata_copy_out_error_reg = 1;
5828 }
5829
5830 /* Determine transfer length */
5831 switch (scsipkt->pkt_cdbp[2] & 0x03) { /* T_LENGTH field */
5832 case 1:
5833 /* Length is in the FEATURE field */
5834 xfer_len = (uint32_t)scmd->satacmd_features_reg_ext << 8 |
5835 scmd->satacmd_features_reg;
5836
5837 /* If BYTE_BLOCK is set, above value is in units of blocks */
5838 if (((scsipkt->pkt_cdbp[2] >> 2) & 1) == 0)
5839 xfer_len *= SATA_DISK_SECTOR_SIZE;
5840 break;
5841 case 2:
5842 /* Length is in the COUNT field */
5843 xfer_len = (uint32_t)scmd->satacmd_sec_count_msb << 8 |
5844 scmd->satacmd_sec_count_lsb;
5845
5846 /* If BYTE_BLOCK is set, above value is in units of blocks */
5847 if (((scsipkt->pkt_cdbp[2] >> 2) & 1) == 0)
5848 xfer_len *= SATA_DISK_SECTOR_SIZE;
5849 break;
5850 case 3:
5851 /*
5852 * Length is transport specific. The spec is a bit vague on
5853 * this, but it seems like using buf->b_bcount is the most
5854 * reasonable analogue in our situation. b_bcount is in
5855 * units of bytes.
5856 */
5857 xfer_len = bp->b_bcount;
5858 break;
5859 default:
5860 xfer_len = 0;
5861 }
5862
5863 /* Don't allow a transfer larger than what the struct buf supports */
5864 if (xfer_len > bp->b_bcount) {
5865 mutex_exit(cport_mutex);
5866 return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5867 }
5868
5869 /* Start processing command */
5870 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5871 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_apt_completion;
5872 synch = FALSE;
5873 } else {
5874 synch = TRUE;
5875 }
5876
5877 if (sata_hba_start(spx, &rval) != 0) {
5878 mutex_exit(cport_mutex);
5879 return (rval);
5880 }
5881
5882 mutex_exit(cport_mutex);
5883
5884 if (synch) {
5885 sata_txlt_apt_completion(spx->txlt_sata_pkt);
5886 }
5887
5888 return (TRAN_ACCEPT);
5889 }
5890
5891 /*
5892 * Translate command: Log Sense
5893 */
5894 static int
sata_txlt_log_sense(sata_pkt_txlate_t * spx)5895 sata_txlt_log_sense(sata_pkt_txlate_t *spx)
5896 {
5897 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5898 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5899 sata_drive_info_t *sdinfo;
5900 struct scsi_extended_sense *sense;
5901 int len, count, alc_len;
5902 int pc; /* Page Control code */
5903 int page_code; /* Page code */
5904 uint8_t *buf; /* log sense buffer */
5905 int rval, reason;
5906 #define MAX_LOG_SENSE_PAGE_SIZE 512
5907 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5908
5909 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5910 "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
5911 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5912 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5913
5914 if (servicing_interrupt()) {
5915 buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_NOSLEEP);
5916 if (buf == NULL) {
5917 return (TRAN_BUSY);
5918 }
5919 } else {
5920 buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
5921 }
5922
5923 mutex_enter(cport_mutex);
5924
5925 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
5926 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5927 mutex_exit(cport_mutex);
5928 kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
5929 return (rval);
5930 }
5931
5932 scsipkt->pkt_reason = CMD_CMPLT;
5933 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5934 STATE_SENT_CMD | STATE_GOT_STATUS;
5935
5936 pc = scsipkt->pkt_cdbp[2] >> 6;
5937 page_code = scsipkt->pkt_cdbp[2] & 0x3f;
5938
5939 /* Reject not supported request for all but cumulative values */
5940 switch (pc) {
5941 case PC_CUMULATIVE_VALUES:
5942 break;
5943 default:
5944 *scsipkt->pkt_scbp = STATUS_CHECK;
5945 sense = sata_arq_sense(spx);
5946 sense->es_key = KEY_ILLEGAL_REQUEST;
5947 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5948 goto done;
5949 }
5950
5951 switch (page_code) {
5952 case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5953 case PAGE_CODE_SELF_TEST_RESULTS:
5954 case PAGE_CODE_INFORMATION_EXCEPTIONS:
5955 case PAGE_CODE_SMART_READ_DATA:
5956 case PAGE_CODE_START_STOP_CYCLE_COUNTER:
5957 case PAGE_CODE_TEMPERATURE:
5958 case PAGE_CODE_SOLID_STATE_MEDIA:
5959 case PAGE_CODE_READ_ERRORS:
5960 case PAGE_CODE_GENERAL_STATS:
5961 break;
5962 default:
5963 *scsipkt->pkt_scbp = STATUS_CHECK;
5964 sense = sata_arq_sense(spx);
5965 sense->es_key = KEY_ILLEGAL_REQUEST;
5966 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5967 goto done;
5968 }
5969
5970 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
5971 /*
5972 * Because log sense uses local buffers for data retrieval from
5973 * the devices and sets the data programatically in the
5974 * original specified buffer, release preallocated DMA
5975 * resources before storing data in the original buffer,
5976 * so no unwanted DMA sync would take place.
5977 */
5978 sata_id_t *sata_id;
5979
5980 sata_scsi_dmafree(NULL, scsipkt);
5981
5982 len = 0;
5983
5984 /* Build log parameter header */
5985 buf[len++] = page_code; /* page code as in the CDB */
5986 buf[len++] = 0; /* reserved */
5987 buf[len++] = 0; /* Zero out page length for now (MSB) */
5988 buf[len++] = 0; /* (LSB) */
5989
5990 sdinfo = sata_get_device_info(
5991 spx->txlt_sata_hba_inst,
5992 &spx->txlt_sata_pkt->satapkt_device);
5993
5994 sata_id = &sdinfo->satadrv_id;
5995
5996 /*
5997 * Add requested pages.
5998 */
5999 switch (page_code) {
6000 case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
6001 len = sata_build_lsense_page_0(sdinfo, buf + len);
6002 break;
6003 case PAGE_CODE_SELF_TEST_RESULTS:
6004 if ((! (sata_id->ai_cmdset84 &
6005 SATA_SMART_SELF_TEST_SUPPORTED)) ||
6006 (! (sata_id->ai_features87 &
6007 SATA_SMART_SELF_TEST_SUPPORTED))) {
6008 *scsipkt->pkt_scbp = STATUS_CHECK;
6009 sense = sata_arq_sense(spx);
6010 sense->es_key = KEY_ILLEGAL_REQUEST;
6011 sense->es_add_code =
6012 SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6013
6014 goto done;
6015 }
6016 len = sata_build_lsense_page_10(sdinfo, buf + len,
6017 spx->txlt_sata_hba_inst);
6018 break;
6019 case PAGE_CODE_INFORMATION_EXCEPTIONS:
6020 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
6021 *scsipkt->pkt_scbp = STATUS_CHECK;
6022 sense = sata_arq_sense(spx);
6023 sense->es_key = KEY_ILLEGAL_REQUEST;
6024 sense->es_add_code =
6025 SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6026
6027 goto done;
6028 }
6029 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
6030 *scsipkt->pkt_scbp = STATUS_CHECK;
6031 sense = sata_arq_sense(spx);
6032 sense->es_key = KEY_ABORTED_COMMAND;
6033 sense->es_add_code =
6034 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
6035 sense->es_qual_code =
6036 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
6037
6038 goto done;
6039 }
6040
6041 len = sata_build_lsense_page_2f(sdinfo, buf + len,
6042 spx->txlt_sata_hba_inst);
6043 break;
6044 case PAGE_CODE_SMART_READ_DATA:
6045 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
6046 *scsipkt->pkt_scbp = STATUS_CHECK;
6047 sense = sata_arq_sense(spx);
6048 sense->es_key = KEY_ILLEGAL_REQUEST;
6049 sense->es_add_code =
6050 SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6051
6052 goto done;
6053 }
6054 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
6055 *scsipkt->pkt_scbp = STATUS_CHECK;
6056 sense = sata_arq_sense(spx);
6057 sense->es_key = KEY_ABORTED_COMMAND;
6058 sense->es_add_code =
6059 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
6060 sense->es_qual_code =
6061 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
6062
6063 goto done;
6064 }
6065
6066 /* This page doesn't include a page header */
6067 len = sata_build_lsense_page_30(sdinfo, buf,
6068 spx->txlt_sata_hba_inst);
6069 goto no_header;
6070 case PAGE_CODE_START_STOP_CYCLE_COUNTER:
6071 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
6072 *scsipkt->pkt_scbp = STATUS_CHECK;
6073 sense = sata_arq_sense(spx);
6074 sense->es_key = KEY_ILLEGAL_REQUEST;
6075 sense->es_add_code =
6076 SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6077
6078 goto done;
6079 }
6080 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
6081 *scsipkt->pkt_scbp = STATUS_CHECK;
6082 sense = sata_arq_sense(spx);
6083 sense->es_key = KEY_ABORTED_COMMAND;
6084 sense->es_add_code =
6085 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
6086 sense->es_qual_code =
6087 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
6088
6089 goto done;
6090 }
6091 len = sata_build_lsense_page_0e(sdinfo, buf, spx);
6092 goto no_header;
6093 case PAGE_CODE_TEMPERATURE:
6094 len = sata_build_lsense_page_0d(sdinfo, buf + len,
6095 spx->txlt_sata_hba_inst);
6096 break;
6097 case PAGE_CODE_SOLID_STATE_MEDIA:
6098 len = sata_build_lsense_page_11(sdinfo, buf + len,
6099 spx->txlt_sata_hba_inst);
6100 break;
6101 case PAGE_CODE_READ_ERRORS:
6102 len = sata_build_lsense_page_03(sdinfo, buf + len,
6103 spx->txlt_sata_hba_inst);
6104 break;
6105 case PAGE_CODE_GENERAL_STATS:
6106 len = sata_build_lsense_page_19(sdinfo, buf + len,
6107 spx->txlt_sata_hba_inst);
6108 break;
6109 default:
6110 /* Invalid request */
6111 *scsipkt->pkt_scbp = STATUS_CHECK;
6112 sense = sata_arq_sense(spx);
6113 sense->es_key = KEY_ILLEGAL_REQUEST;
6114 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6115 goto done;
6116 }
6117
6118 if (len < 0) {
6119 /* Page not supported by device */
6120 *scsipkt->pkt_scbp = STATUS_CHECK;
6121 sense = sata_arq_sense(spx);
6122 sense->es_key = KEY_ILLEGAL_REQUEST;
6123 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6124 goto done;
6125 }
6126
6127 /* set parameter log sense data length */
6128 buf[2] = len >> 8; /* log sense length (MSB) */
6129 buf[3] = len & 0xff; /* log sense length (LSB) */
6130
6131 len += SCSI_LOG_PAGE_HDR_LEN;
6132 ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
6133
6134 no_header:
6135 /* Check allocation length */
6136 alc_len = scsipkt->pkt_cdbp[7];
6137 alc_len = (alc_len << 8) | scsipkt->pkt_cdbp[8];
6138
6139 /*
6140 * We do not check for possible parameters truncation
6141 * (alc_len < len) assuming that the target driver works
6142 * correctly. Just avoiding overrun.
6143 * Copy no more than requested and possible, buffer-wise.
6144 */
6145 count = MIN(alc_len, len);
6146 count = MIN(bp->b_bcount, count);
6147 bcopy(buf, bp->b_un.b_addr, count);
6148
6149 scsipkt->pkt_state |= STATE_XFERRED_DATA;
6150 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
6151 }
6152 *scsipkt->pkt_scbp = STATUS_GOOD;
6153 done:
6154 mutex_exit(cport_mutex);
6155 (void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
6156
6157 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6158 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6159
6160 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6161 scsipkt->pkt_comp != NULL) {
6162 /* scsi callback required */
6163 if (servicing_interrupt()) {
6164 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6165 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6166 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
6167 TASKQID_INVALID) {
6168 return (TRAN_BUSY);
6169 }
6170 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6171 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6172 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
6173 /* Scheduling the callback failed */
6174 return (TRAN_BUSY);
6175 }
6176 }
6177
6178 return (TRAN_ACCEPT);
6179 }
6180
6181 /*
6182 * Translate command: Log Select
6183 * Not implemented at this time - returns invalid command response.
6184 */
6185 static int
sata_txlt_log_select(sata_pkt_txlate_t * spx)6186 sata_txlt_log_select(sata_pkt_txlate_t *spx)
6187 {
6188 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6189 "sata_txlt_log_select\n", NULL);
6190
6191 return (sata_txlt_invalid_command(spx));
6192 }
6193
6194
6195 /*
6196 * Translate command: Read (various types).
6197 * Translated into appropriate type of ATA READ command
6198 * for SATA hard disks.
6199 * Both the device capabilities and requested operation mode are
6200 * considered.
6201 *
6202 * Following scsi cdb fields are ignored:
6203 * rdprotect, dpo, fua, fua_nv, group_number.
6204 *
6205 * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
6206 * enable variable sata_func_enable), the capability of the controller and
6207 * capability of a device are checked and if both support queueing, read
6208 * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
6209 * command rather than plain READ_XXX command.
6210 * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
6211 * both the controller and device suport such functionality, the read
6212 * request will be translated to READ_FPDMA_QUEUED command.
6213 * In both cases the maximum queue depth is derived as minimum of:
6214 * HBA capability,device capability and sata_max_queue_depth variable setting.
6215 * The value passed to HBA driver is decremented by 1, because only 5 bits are
6216 * used to pass max queue depth value, and the maximum possible queue depth
6217 * is 32.
6218 *
6219 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6220 * appropriate values in scsi_pkt fields.
6221 */
6222 static int
sata_txlt_read(sata_pkt_txlate_t * spx)6223 sata_txlt_read(sata_pkt_txlate_t *spx)
6224 {
6225 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6226 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6227 sata_drive_info_t *sdinfo;
6228 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
6229 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6230 uint16_t sec_count;
6231 uint64_t lba;
6232 int rval, reason;
6233 int synch;
6234
6235 mutex_enter(cport_mutex);
6236
6237 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
6238 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6239 mutex_exit(cport_mutex);
6240 return (rval);
6241 }
6242
6243 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6244 &spx->txlt_sata_pkt->satapkt_device);
6245
6246 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
6247 /*
6248 * Extract LBA and sector count from scsi CDB.
6249 */
6250 switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6251 case SCMD_READ:
6252 /* 6-byte scsi read cmd : 0x08 */
6253 lba = (scsipkt->pkt_cdbp[1] & 0x1f);
6254 lba = (lba << 8) | scsipkt->pkt_cdbp[2];
6255 lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6256 sec_count = scsipkt->pkt_cdbp[4];
6257 /* sec_count 0 will be interpreted as 256 by a device */
6258 break;
6259 case SCMD_READ_G1:
6260 /* 10-bytes scsi read command : 0x28 */
6261 lba = scsipkt->pkt_cdbp[2];
6262 lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6263 lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6264 lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6265 sec_count = scsipkt->pkt_cdbp[7];
6266 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6267 break;
6268 case SCMD_READ_G5:
6269 /* 12-bytes scsi read command : 0xA8 */
6270 lba = scsipkt->pkt_cdbp[2];
6271 lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6272 lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6273 lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6274 sec_count = scsipkt->pkt_cdbp[6];
6275 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
6276 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6277 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
6278 break;
6279 case SCMD_READ_G4:
6280 /* 16-bytes scsi read command : 0x88 */
6281 lba = scsipkt->pkt_cdbp[2];
6282 lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6283 lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6284 lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6285 lba = (lba << 8) | scsipkt->pkt_cdbp[6];
6286 lba = (lba << 8) | scsipkt->pkt_cdbp[7];
6287 lba = (lba << 8) | scsipkt->pkt_cdbp[8];
6288 lba = (lba << 8) | scsipkt->pkt_cdbp[9];
6289 sec_count = scsipkt->pkt_cdbp[10];
6290 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
6291 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
6292 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
6293 break;
6294 default:
6295 /* Unsupported command */
6296 mutex_exit(cport_mutex);
6297 return (sata_txlt_invalid_command(spx));
6298 }
6299
6300 /*
6301 * Check if specified address exceeds device capacity
6302 */
6303 if ((lba >= sdinfo->satadrv_capacity) ||
6304 ((lba + sec_count) > sdinfo->satadrv_capacity)) {
6305 /* LBA out of range */
6306 mutex_exit(cport_mutex);
6307 return (sata_txlt_lba_out_of_range(spx));
6308 }
6309
6310 /*
6311 * For zero-length transfer, emulate good completion of the command
6312 * (reasons for rejecting the command were already checked).
6313 * No DMA resources were allocated.
6314 */
6315 if (spx->txlt_dma_cookie_list == NULL) {
6316 mutex_exit(cport_mutex);
6317 return (sata_emul_rw_completion(spx));
6318 }
6319
6320 /*
6321 * Build cmd block depending on the device capability and
6322 * requested operation mode.
6323 * Do not bother with non-dma mode - we are working only with
6324 * devices supporting DMA.
6325 */
6326 scmd->satacmd_addr_type = ATA_ADDR_LBA;
6327 scmd->satacmd_device_reg = SATA_ADH_LBA;
6328 scmd->satacmd_cmd_reg = SATAC_READ_DMA;
6329 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6330 scmd->satacmd_addr_type = ATA_ADDR_LBA48;
6331 scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT;
6332 scmd->satacmd_sec_count_msb = sec_count >> 8;
6333 #ifndef __lock_lint
6334 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
6335 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
6336 scmd->satacmd_lba_high_msb = lba >> 40;
6337 #endif
6338 } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
6339 scmd->satacmd_addr_type = ATA_ADDR_LBA28;
6340 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
6341 }
6342 scmd->satacmd_sec_count_lsb = sec_count & 0xff;
6343 scmd->satacmd_lba_low_lsb = lba & 0xff;
6344 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
6345 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
6346 scmd->satacmd_features_reg = 0;
6347 scmd->satacmd_status_reg = 0;
6348 scmd->satacmd_error_reg = 0;
6349
6350 /*
6351 * Check if queueing commands should be used and switch
6352 * to appropriate command if possible
6353 */
6354 if (sata_func_enable & SATA_ENABLE_QUEUING) {
6355 boolean_t using_queuing;
6356
6357 /* Queuing supported by controller and device? */
6358 if ((sata_func_enable & SATA_ENABLE_NCQ) &&
6359 (sdinfo->satadrv_features_support &
6360 SATA_DEV_F_NCQ) &&
6361 (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6362 SATA_CTLF_NCQ)) {
6363 using_queuing = B_TRUE;
6364
6365 /* NCQ supported - use FPDMA READ */
6366 scmd->satacmd_cmd_reg =
6367 SATAC_READ_FPDMA_QUEUED;
6368 scmd->satacmd_features_reg_ext =
6369 scmd->satacmd_sec_count_msb;
6370 scmd->satacmd_sec_count_msb = 0;
6371 } else if ((sdinfo->satadrv_features_support &
6372 SATA_DEV_F_TCQ) &&
6373 (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6374 SATA_CTLF_QCMD)) {
6375 using_queuing = B_TRUE;
6376
6377 /* Legacy queueing */
6378 if (sdinfo->satadrv_features_support &
6379 SATA_DEV_F_LBA48) {
6380 scmd->satacmd_cmd_reg =
6381 SATAC_READ_DMA_QUEUED_EXT;
6382 scmd->satacmd_features_reg_ext =
6383 scmd->satacmd_sec_count_msb;
6384 scmd->satacmd_sec_count_msb = 0;
6385 } else {
6386 scmd->satacmd_cmd_reg =
6387 SATAC_READ_DMA_QUEUED;
6388 }
6389 } else /* NCQ nor legacy queuing not supported */
6390 using_queuing = B_FALSE;
6391
6392 /*
6393 * If queuing, the sector count goes in the features register
6394 * and the secount count will contain the tag.
6395 */
6396 if (using_queuing) {
6397 scmd->satacmd_features_reg =
6398 scmd->satacmd_sec_count_lsb;
6399 scmd->satacmd_sec_count_lsb = 0;
6400 scmd->satacmd_flags.sata_queued = B_TRUE;
6401
6402 /* Set-up maximum queue depth */
6403 scmd->satacmd_flags.sata_max_queue_depth =
6404 sdinfo->satadrv_max_queue_depth - 1;
6405 } else if (sdinfo->satadrv_features_enabled &
6406 SATA_DEV_F_E_UNTAGGED_QING) {
6407 /*
6408 * Although NCQ/TCQ is not enabled, untagged queuing
6409 * may be still used.
6410 * Set-up the maximum untagged queue depth.
6411 * Use controller's queue depth from sata_hba_tran.
6412 * SATA HBA drivers may ignore this value and rely on
6413 * the internal limits.For drivers that do not
6414 * ignore untaged queue depth, limit the value to
6415 * SATA_MAX_QUEUE_DEPTH (32), as this is the
6416 * largest value that can be passed via
6417 * satacmd_flags.sata_max_queue_depth.
6418 */
6419 scmd->satacmd_flags.sata_max_queue_depth =
6420 SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
6421 SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
6422
6423 } else {
6424 scmd->satacmd_flags.sata_max_queue_depth = 0;
6425 }
6426 } else
6427 scmd->satacmd_flags.sata_max_queue_depth = 0;
6428
6429 SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst,
6430 "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n",
6431 scmd->satacmd_cmd_reg, lba, sec_count);
6432
6433 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6434 /* Need callback function */
6435 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
6436 synch = FALSE;
6437 } else
6438 synch = TRUE;
6439
6440 /* Transfer command to HBA */
6441 if (sata_hba_start(spx, &rval) != 0) {
6442 /* Pkt not accepted for execution */
6443 mutex_exit(cport_mutex);
6444 return (rval);
6445 }
6446 mutex_exit(cport_mutex);
6447 /*
6448 * If execution is non-synchronous,
6449 * a callback function will handle potential errors, translate
6450 * the response and will do a callback to a target driver.
6451 * If it was synchronous, check execution status using the same
6452 * framework callback.
6453 */
6454 if (synch) {
6455 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6456 "synchronous execution status %x\n",
6457 spx->txlt_sata_pkt->satapkt_reason);
6458 sata_txlt_rw_completion(spx->txlt_sata_pkt);
6459 }
6460 return (TRAN_ACCEPT);
6461 }
6462
6463
6464 /*
6465 * SATA translate command: Write (various types)
6466 * Translated into appropriate type of ATA WRITE command
6467 * for SATA hard disks.
6468 * Both the device capabilities and requested operation mode are
6469 * considered.
6470 *
6471 * Following scsi cdb fields are ignored:
6472 * rwprotect, dpo, fua, fua_nv, group_number.
6473 *
6474 * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
6475 * enable variable sata_func_enable), the capability of the controller and
6476 * capability of a device are checked and if both support queueing, write
6477 * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT
6478 * command rather than plain WRITE_XXX command.
6479 * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
6480 * both the controller and device suport such functionality, the write
6481 * request will be translated to WRITE_FPDMA_QUEUED command.
6482 * In both cases the maximum queue depth is derived as minimum of:
6483 * HBA capability,device capability and sata_max_queue_depth variable setting.
6484 * The value passed to HBA driver is decremented by 1, because only 5 bits are
6485 * used to pass max queue depth value, and the maximum possible queue depth
6486 * is 32.
6487 *
6488 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6489 * appropriate values in scsi_pkt fields.
6490 */
6491 static int
sata_txlt_write(sata_pkt_txlate_t * spx)6492 sata_txlt_write(sata_pkt_txlate_t *spx)
6493 {
6494 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6495 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6496 sata_drive_info_t *sdinfo;
6497 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
6498 uint16_t sec_count;
6499 uint64_t lba;
6500 int rval, reason;
6501 int synch;
6502 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6503
6504 mutex_enter(cport_mutex);
6505
6506 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
6507 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6508 mutex_exit(cport_mutex);
6509 return (rval);
6510 }
6511
6512 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6513 &spx->txlt_sata_pkt->satapkt_device);
6514
6515 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
6516 /*
6517 * Extract LBA and sector count from scsi CDB
6518 */
6519 switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6520 case SCMD_WRITE:
6521 /* 6-byte scsi read cmd : 0x0A */
6522 lba = (scsipkt->pkt_cdbp[1] & 0x1f);
6523 lba = (lba << 8) | scsipkt->pkt_cdbp[2];
6524 lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6525 sec_count = scsipkt->pkt_cdbp[4];
6526 /* sec_count 0 will be interpreted as 256 by a device */
6527 break;
6528 case SCMD_WRITE_G1:
6529 /* 10-bytes scsi write command : 0x2A */
6530 lba = scsipkt->pkt_cdbp[2];
6531 lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6532 lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6533 lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6534 sec_count = scsipkt->pkt_cdbp[7];
6535 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6536 break;
6537 case SCMD_WRITE_G5:
6538 /* 12-bytes scsi read command : 0xAA */
6539 lba = scsipkt->pkt_cdbp[2];
6540 lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6541 lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6542 lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6543 sec_count = scsipkt->pkt_cdbp[6];
6544 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
6545 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6546 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
6547 break;
6548 case SCMD_WRITE_G4:
6549 /* 16-bytes scsi write command : 0x8A */
6550 lba = scsipkt->pkt_cdbp[2];
6551 lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6552 lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6553 lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6554 lba = (lba << 8) | scsipkt->pkt_cdbp[6];
6555 lba = (lba << 8) | scsipkt->pkt_cdbp[7];
6556 lba = (lba << 8) | scsipkt->pkt_cdbp[8];
6557 lba = (lba << 8) | scsipkt->pkt_cdbp[9];
6558 sec_count = scsipkt->pkt_cdbp[10];
6559 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
6560 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
6561 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
6562 break;
6563 default:
6564 /* Unsupported command */
6565 mutex_exit(cport_mutex);
6566 return (sata_txlt_invalid_command(spx));
6567 }
6568
6569 /*
6570 * Check if specified address and length exceeds device capacity
6571 */
6572 if ((lba >= sdinfo->satadrv_capacity) ||
6573 ((lba + sec_count) > sdinfo->satadrv_capacity)) {
6574 /* LBA out of range */
6575 mutex_exit(cport_mutex);
6576 return (sata_txlt_lba_out_of_range(spx));
6577 }
6578
6579 /*
6580 * For zero-length transfer, emulate good completion of the command
6581 * (reasons for rejecting the command were already checked).
6582 * No DMA resources were allocated.
6583 */
6584 if (spx->txlt_dma_cookie_list == NULL) {
6585 mutex_exit(cport_mutex);
6586 return (sata_emul_rw_completion(spx));
6587 }
6588
6589 /*
6590 * Build cmd block depending on the device capability and
6591 * requested operation mode.
6592 * Do not bother with non-dma mode- we are working only with
6593 * devices supporting DMA.
6594 */
6595 scmd->satacmd_addr_type = ATA_ADDR_LBA;
6596 scmd->satacmd_device_reg = SATA_ADH_LBA;
6597 scmd->satacmd_cmd_reg = SATAC_WRITE_DMA;
6598 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6599 scmd->satacmd_addr_type = ATA_ADDR_LBA48;
6600 scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT;
6601 scmd->satacmd_sec_count_msb = sec_count >> 8;
6602 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
6603 #ifndef __lock_lint
6604 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
6605 scmd->satacmd_lba_high_msb = lba >> 40;
6606 #endif
6607 } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
6608 scmd->satacmd_addr_type = ATA_ADDR_LBA28;
6609 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
6610 }
6611 scmd->satacmd_sec_count_lsb = sec_count & 0xff;
6612 scmd->satacmd_lba_low_lsb = lba & 0xff;
6613 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
6614 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
6615 scmd->satacmd_features_reg = 0;
6616 scmd->satacmd_status_reg = 0;
6617 scmd->satacmd_error_reg = 0;
6618
6619 /*
6620 * Check if queueing commands should be used and switch
6621 * to appropriate command if possible
6622 */
6623 if (sata_func_enable & SATA_ENABLE_QUEUING) {
6624 boolean_t using_queuing;
6625
6626 /* Queuing supported by controller and device? */
6627 if ((sata_func_enable & SATA_ENABLE_NCQ) &&
6628 (sdinfo->satadrv_features_support &
6629 SATA_DEV_F_NCQ) &&
6630 (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6631 SATA_CTLF_NCQ)) {
6632 using_queuing = B_TRUE;
6633
6634 /* NCQ supported - use FPDMA WRITE */
6635 scmd->satacmd_cmd_reg =
6636 SATAC_WRITE_FPDMA_QUEUED;
6637 scmd->satacmd_features_reg_ext =
6638 scmd->satacmd_sec_count_msb;
6639 scmd->satacmd_sec_count_msb = 0;
6640 } else if ((sdinfo->satadrv_features_support &
6641 SATA_DEV_F_TCQ) &&
6642 (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6643 SATA_CTLF_QCMD)) {
6644 using_queuing = B_TRUE;
6645
6646 /* Legacy queueing */
6647 if (sdinfo->satadrv_features_support &
6648 SATA_DEV_F_LBA48) {
6649 scmd->satacmd_cmd_reg =
6650 SATAC_WRITE_DMA_QUEUED_EXT;
6651 scmd->satacmd_features_reg_ext =
6652 scmd->satacmd_sec_count_msb;
6653 scmd->satacmd_sec_count_msb = 0;
6654 } else {
6655 scmd->satacmd_cmd_reg =
6656 SATAC_WRITE_DMA_QUEUED;
6657 }
6658 } else /* NCQ nor legacy queuing not supported */
6659 using_queuing = B_FALSE;
6660
6661 if (using_queuing) {
6662 scmd->satacmd_features_reg =
6663 scmd->satacmd_sec_count_lsb;
6664 scmd->satacmd_sec_count_lsb = 0;
6665 scmd->satacmd_flags.sata_queued = B_TRUE;
6666 /* Set-up maximum queue depth */
6667 scmd->satacmd_flags.sata_max_queue_depth =
6668 sdinfo->satadrv_max_queue_depth - 1;
6669 } else if (sdinfo->satadrv_features_enabled &
6670 SATA_DEV_F_E_UNTAGGED_QING) {
6671 /*
6672 * Although NCQ/TCQ is not enabled, untagged queuing
6673 * may be still used.
6674 * Set-up the maximum untagged queue depth.
6675 * Use controller's queue depth from sata_hba_tran.
6676 * SATA HBA drivers may ignore this value and rely on
6677 * the internal limits. For drivera that do not
6678 * ignore untaged queue depth, limit the value to
6679 * SATA_MAX_QUEUE_DEPTH (32), as this is the
6680 * largest value that can be passed via
6681 * satacmd_flags.sata_max_queue_depth.
6682 */
6683 scmd->satacmd_flags.sata_max_queue_depth =
6684 SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
6685 SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
6686
6687 } else {
6688 scmd->satacmd_flags.sata_max_queue_depth = 0;
6689 }
6690 } else
6691 scmd->satacmd_flags.sata_max_queue_depth = 0;
6692
6693 SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6694 "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n",
6695 scmd->satacmd_cmd_reg, lba, sec_count);
6696
6697 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6698 /* Need callback function */
6699 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
6700 synch = FALSE;
6701 } else
6702 synch = TRUE;
6703
6704 /* Transfer command to HBA */
6705 if (sata_hba_start(spx, &rval) != 0) {
6706 /* Pkt not accepted for execution */
6707 mutex_exit(cport_mutex);
6708 return (rval);
6709 }
6710 mutex_exit(cport_mutex);
6711
6712 /*
6713 * If execution is non-synchronous,
6714 * a callback function will handle potential errors, translate
6715 * the response and will do a callback to a target driver.
6716 * If it was synchronous, check execution status using the same
6717 * framework callback.
6718 */
6719 if (synch) {
6720 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6721 "synchronous execution status %x\n",
6722 spx->txlt_sata_pkt->satapkt_reason);
6723 sata_txlt_rw_completion(spx->txlt_sata_pkt);
6724 }
6725 return (TRAN_ACCEPT);
6726 }
6727
6728
6729 /*
6730 * Implements SCSI SBC WRITE BUFFER command download microcode option
6731 */
6732 static int
sata_txlt_write_buffer(sata_pkt_txlate_t * spx)6733 sata_txlt_write_buffer(sata_pkt_txlate_t *spx)
6734 {
6735 #define WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE 4
6736 #define WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE 5
6737
6738 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6739 struct sata_pkt *sata_pkt = spx->txlt_sata_pkt;
6740 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6741
6742 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
6743 struct scsi_extended_sense *sense;
6744 int rval, mode, sector_count, reason;
6745 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6746
6747 mode = scsipkt->pkt_cdbp[1] & 0x1f;
6748
6749 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6750 "sata_txlt_write_buffer, mode 0x%x\n", mode);
6751
6752 mutex_enter(cport_mutex);
6753
6754 if ((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
6755 TRAN_ACCEPT) {
6756 mutex_exit(cport_mutex);
6757 return (rval);
6758 }
6759
6760 /* Use synchronous mode */
6761 spx->txlt_sata_pkt->satapkt_op_mode
6762 |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
6763
6764 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
6765
6766 scsipkt->pkt_reason = CMD_CMPLT;
6767 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6768 STATE_SENT_CMD | STATE_GOT_STATUS;
6769
6770 /*
6771 * The SCSI to ATA translation specification only calls
6772 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE.
6773 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but
6774 * ATA 8 (draft) got rid of download microcode for temp
6775 * and it is even optional for ATA 7, so it may be aborted.
6776 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as
6777 * it is not specified and the buffer offset for SCSI is a 16-bit
6778 * value in bytes, but for ATA it is a 16-bit offset in 512 byte
6779 * sectors. Thus the offset really doesn't buy us anything.
6780 * If and when ATA 8 is stabilized and the SCSI to ATA specification
6781 * is revised, this can be revisisted.
6782 */
6783 /* Reject not supported request */
6784 switch (mode) {
6785 case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE:
6786 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP;
6787 break;
6788 case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE:
6789 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE;
6790 break;
6791 default:
6792 goto bad_param;
6793 }
6794
6795 *scsipkt->pkt_scbp = STATUS_GOOD; /* Presumed outcome */
6796
6797 scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE;
6798 if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0)
6799 goto bad_param;
6800 sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE;
6801 scmd->satacmd_sec_count_lsb = (uint8_t)sector_count;
6802 scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8;
6803 scmd->satacmd_lba_mid_lsb = 0;
6804 scmd->satacmd_lba_high_lsb = 0;
6805 scmd->satacmd_device_reg = 0;
6806 spx->txlt_sata_pkt->satapkt_comp = NULL;
6807 scmd->satacmd_addr_type = 0;
6808
6809 /* Transfer command to HBA */
6810 if (sata_hba_start(spx, &rval) != 0) {
6811 /* Pkt not accepted for execution */
6812 mutex_exit(cport_mutex);
6813 return (rval);
6814 }
6815
6816 mutex_exit(cport_mutex);
6817
6818 /* Then we need synchronous check the status of the disk */
6819 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6820 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
6821 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
6822 scsipkt->pkt_reason = CMD_CMPLT;
6823
6824 /* Download commmand succeed, so probe and identify device */
6825 sata_reidentify_device(spx);
6826 } else {
6827 /* Something went wrong, microcode download command failed */
6828 scsipkt->pkt_reason = CMD_INCOMPLETE;
6829 *scsipkt->pkt_scbp = STATUS_CHECK;
6830 sense = sata_arq_sense(spx);
6831 switch (sata_pkt->satapkt_reason) {
6832 case SATA_PKT_PORT_ERROR:
6833 /*
6834 * We have no device data. Assume no data transfered.
6835 */
6836 sense->es_key = KEY_HARDWARE_ERROR;
6837 break;
6838
6839 case SATA_PKT_DEV_ERROR:
6840 if (sata_pkt->satapkt_cmd.satacmd_status_reg &
6841 SATA_STATUS_ERR) {
6842 /*
6843 * determine dev error reason from error
6844 * reg content
6845 */
6846 sata_decode_device_error(spx, sense);
6847 break;
6848 }
6849 /* No extended sense key - no info available */
6850 break;
6851
6852 case SATA_PKT_TIMEOUT:
6853 scsipkt->pkt_reason = CMD_TIMEOUT;
6854 scsipkt->pkt_statistics |=
6855 STAT_TIMEOUT | STAT_DEV_RESET;
6856 /* No extended sense key ? */
6857 break;
6858
6859 case SATA_PKT_ABORTED:
6860 scsipkt->pkt_reason = CMD_ABORTED;
6861 scsipkt->pkt_statistics |= STAT_ABORTED;
6862 /* No extended sense key ? */
6863 break;
6864
6865 case SATA_PKT_RESET:
6866 /* pkt aborted by an explicit reset from a host */
6867 scsipkt->pkt_reason = CMD_RESET;
6868 scsipkt->pkt_statistics |= STAT_DEV_RESET;
6869 break;
6870
6871 default:
6872 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
6873 "sata_txlt_nodata_cmd_completion: "
6874 "invalid packet completion reason %d",
6875 sata_pkt->satapkt_reason));
6876 scsipkt->pkt_reason = CMD_TRAN_ERR;
6877 break;
6878 }
6879
6880 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6881 "scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6882
6883 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6884 /* scsi callback required */
6885 scsi_hba_pkt_comp(scsipkt);
6886 }
6887 return (TRAN_ACCEPT);
6888
6889 bad_param:
6890 mutex_exit(cport_mutex);
6891 *scsipkt->pkt_scbp = STATUS_CHECK;
6892 sense = sata_arq_sense(spx);
6893 sense->es_key = KEY_ILLEGAL_REQUEST;
6894 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6895 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6896 scsipkt->pkt_comp != NULL) {
6897 /* scsi callback required */
6898 if (servicing_interrupt()) {
6899 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6900 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6901 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
6902 TASKQID_INVALID) {
6903 return (TRAN_BUSY);
6904 }
6905 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6906 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6907 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
6908 /* Scheduling the callback failed */
6909 return (TRAN_BUSY);
6910 }
6911 }
6912 return (rval);
6913 }
6914
6915 /*
6916 * Re-identify device after doing a firmware download.
6917 */
6918 static void
sata_reidentify_device(sata_pkt_txlate_t * spx)6919 sata_reidentify_device(sata_pkt_txlate_t *spx)
6920 {
6921 #define DOWNLOAD_WAIT_TIME_SECS 60
6922 #define DOWNLOAD_WAIT_INTERVAL_SECS 1
6923 int rval;
6924 int retry_cnt;
6925 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6926 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
6927 sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
6928 sata_drive_info_t *sdinfo;
6929
6930 /*
6931 * Before returning good status, probe device.
6932 * Device probing will get IDENTIFY DEVICE data, if possible.
6933 * The assumption is that the new microcode is applied by the
6934 * device. It is a caller responsibility to verify this.
6935 */
6936 for (retry_cnt = 0;
6937 retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS;
6938 retry_cnt++) {
6939 rval = sata_probe_device(sata_hba_inst, &sata_device);
6940
6941 if (rval == SATA_SUCCESS) { /* Set default features */
6942 sdinfo = sata_get_device_info(sata_hba_inst,
6943 &sata_device);
6944 if (sata_initialize_device(sata_hba_inst, sdinfo) !=
6945 SATA_SUCCESS) {
6946 /* retry */
6947 rval = sata_initialize_device(sata_hba_inst,
6948 sdinfo);
6949 if (rval == SATA_RETRY)
6950 sata_log(sata_hba_inst, CE_WARN,
6951 "SATA device at port %d pmport %d -"
6952 " default device features could not"
6953 " be set. Device may not operate "
6954 "as expected.",
6955 sata_device.satadev_addr.cport,
6956 sata_device.satadev_addr.pmport);
6957 }
6958 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6959 scsi_hba_pkt_comp(scsipkt);
6960 return;
6961 } else if (rval == SATA_RETRY) {
6962 delay(drv_usectohz(1000000 *
6963 DOWNLOAD_WAIT_INTERVAL_SECS));
6964 continue;
6965 } else /* failed - no reason to retry */
6966 break;
6967 }
6968
6969 /*
6970 * Something went wrong, device probing failed.
6971 */
6972 SATA_LOG_D((sata_hba_inst, CE_WARN,
6973 "Cannot probe device after downloading microcode\n"));
6974
6975 /* Reset device to force retrying the probe. */
6976 (void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst))
6977 (SATA_DIP(sata_hba_inst), &sata_device);
6978
6979 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6980 scsi_hba_pkt_comp(scsipkt);
6981 }
6982
6983
6984 /*
6985 * Translate command: Synchronize Cache.
6986 * Translates into Flush Cache command for SATA hard disks.
6987 *
6988 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6989 * appropriate values in scsi_pkt fields.
6990 */
6991 static int
sata_txlt_synchronize_cache(sata_pkt_txlate_t * spx)6992 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx)
6993 {
6994 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6995 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6996 int rval, reason;
6997 int synch;
6998
6999 mutex_enter(cport_mutex);
7000
7001 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
7002 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
7003 mutex_exit(cport_mutex);
7004 return (rval);
7005 }
7006
7007 scmd->satacmd_addr_type = 0;
7008 scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE;
7009 scmd->satacmd_device_reg = 0;
7010 scmd->satacmd_sec_count_lsb = 0;
7011 scmd->satacmd_lba_low_lsb = 0;
7012 scmd->satacmd_lba_mid_lsb = 0;
7013 scmd->satacmd_lba_high_lsb = 0;
7014 scmd->satacmd_features_reg = 0;
7015 scmd->satacmd_status_reg = 0;
7016 scmd->satacmd_error_reg = 0;
7017
7018 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7019 "sata_txlt_synchronize_cache\n", NULL);
7020
7021 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
7022 /* Need to set-up a callback function */
7023 spx->txlt_sata_pkt->satapkt_comp =
7024 sata_txlt_nodata_cmd_completion;
7025 synch = FALSE;
7026 } else
7027 synch = TRUE;
7028
7029 /* Transfer command to HBA */
7030 if (sata_hba_start(spx, &rval) != 0) {
7031 /* Pkt not accepted for execution */
7032 mutex_exit(cport_mutex);
7033 return (rval);
7034 }
7035 mutex_exit(cport_mutex);
7036
7037 /*
7038 * If execution non-synchronous, it had to be completed
7039 * a callback function will handle potential errors, translate
7040 * the response and will do a callback to a target driver.
7041 * If it was synchronous, check status, using the same
7042 * framework callback.
7043 */
7044 if (synch) {
7045 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7046 "synchronous execution status %x\n",
7047 spx->txlt_sata_pkt->satapkt_reason);
7048 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
7049 }
7050 return (TRAN_ACCEPT);
7051 }
7052
7053
7054 /*
7055 * Send pkt to SATA HBA driver
7056 *
7057 * This function may be called only if the operation is requested by scsi_pkt,
7058 * i.e. scsi_pkt is not NULL.
7059 *
7060 * This function has to be called with cport mutex held. It does release
7061 * the mutex when it calls HBA driver sata_tran_start function and
7062 * re-acquires it afterwards.
7063 *
7064 * If return value is 0, pkt was accepted, -1 otherwise
7065 * rval is set to appropriate sata_scsi_start return value.
7066 *
7067 * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not
7068 * have called the sata_pkt callback function for this packet.
7069 *
7070 * The scsi callback has to be performed by the caller of this routine.
7071 */
7072 static int
sata_hba_start(sata_pkt_txlate_t * spx,int * rval)7073 sata_hba_start(sata_pkt_txlate_t *spx, int *rval)
7074 {
7075 int stat;
7076 uint8_t cport = SATA_TXLT_CPORT(spx);
7077 uint8_t pmport = SATA_TXLT_PMPORT(spx);
7078 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
7079 sata_drive_info_t *sdinfo;
7080 sata_pmult_info_t *pminfo = NULL;
7081 sata_pmport_info_t *pmportinfo = NULL;
7082 sata_device_t *sata_device = NULL;
7083 uint8_t cmd;
7084 struct sata_cmd_flags cmd_flags;
7085
7086 ASSERT(spx->txlt_sata_pkt != NULL);
7087
7088 ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7089
7090 sdinfo = sata_get_device_info(sata_hba_inst,
7091 &spx->txlt_sata_pkt->satapkt_device);
7092 ASSERT(sdinfo != NULL);
7093
7094 /* Clear device reset state? */
7095 /* qual should be XXX_DPMPORT, but add XXX_PMPORT in case */
7096 if (sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT ||
7097 sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT) {
7098
7099 /*
7100 * Get the pmult_info of the its parent port multiplier, all
7101 * sub-devices share a common device reset flags on in
7102 * pmult_info.
7103 */
7104 pminfo = SATA_PMULT_INFO(sata_hba_inst, cport);
7105 pmportinfo = pminfo->pmult_dev_port[pmport];
7106 ASSERT(pminfo != NULL);
7107 if (pminfo->pmult_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) {
7108 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
7109 sata_clear_dev_reset = B_TRUE;
7110 pminfo->pmult_event_flags &=
7111 ~SATA_EVNT_CLEAR_DEVICE_RESET;
7112 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7113 "sata_hba_start: clearing device reset state"
7114 "on pmult.\n", NULL);
7115 }
7116 } else {
7117 if (sdinfo->satadrv_event_flags &
7118 SATA_EVNT_CLEAR_DEVICE_RESET) {
7119 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
7120 sata_clear_dev_reset = B_TRUE;
7121 sdinfo->satadrv_event_flags &=
7122 ~SATA_EVNT_CLEAR_DEVICE_RESET;
7123 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7124 "sata_hba_start: clearing device reset state\n",
7125 NULL);
7126 }
7127 }
7128
7129 cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg;
7130 cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags;
7131 sata_device = &spx->txlt_sata_pkt->satapkt_device;
7132
7133 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7134
7135 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7136 "Sata cmd 0x%2x\n", cmd);
7137
7138 stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
7139 spx->txlt_sata_pkt);
7140 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7141 /*
7142 * If sata pkt was accepted and executed in asynchronous mode, i.e.
7143 * with the sata callback, the sata_pkt could be already destroyed
7144 * by the time we check ther return status from the hba_start()
7145 * function, because sata_scsi_destroy_pkt() could have been already
7146 * called (perhaps in the interrupt context). So, in such case, there
7147 * should be no references to it. In other cases, sata_pkt still
7148 * exists.
7149 */
7150 if (stat == SATA_TRAN_ACCEPTED) {
7151 /*
7152 * pkt accepted for execution.
7153 * If it was executed synchronously, it is already completed
7154 * and pkt completion_reason indicates completion status.
7155 */
7156 *rval = TRAN_ACCEPT;
7157 return (0);
7158 }
7159
7160 sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
7161 switch (stat) {
7162 case SATA_TRAN_QUEUE_FULL:
7163 /*
7164 * Controller detected queue full condition.
7165 */
7166 SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst,
7167 "sata_hba_start: queue full\n", NULL);
7168
7169 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
7170 *spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL;
7171
7172 *rval = TRAN_BUSY;
7173 break;
7174
7175 case SATA_TRAN_PORT_ERROR:
7176 /*
7177 * Communication/link with device or general port error
7178 * detected before pkt execution begun.
7179 */
7180 if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
7181 SATA_ADDR_CPORT ||
7182 spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
7183 SATA_ADDR_DCPORT)
7184 sata_log(sata_hba_inst, CE_CONT,
7185 "SATA port %d error",
7186 sata_device->satadev_addr.cport);
7187 else
7188 sata_log(sata_hba_inst, CE_CONT,
7189 "SATA port %d:%d error\n",
7190 sata_device->satadev_addr.cport,
7191 sata_device->satadev_addr.pmport);
7192
7193 /*
7194 * Update the port/device structure.
7195 * sata_pkt should be still valid. Since port error is
7196 * returned, sata_device content should reflect port
7197 * state - it means, that sata address have been changed,
7198 * because original packet's sata address refered to a device
7199 * attached to some port.
7200 */
7201 if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT ||
7202 sata_device->satadev_addr.qual == SATA_ADDR_PMPORT) {
7203 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7204 mutex_enter(&pmportinfo->pmport_mutex);
7205 sata_update_pmport_info(sata_hba_inst, sata_device);
7206 mutex_exit(&pmportinfo->pmport_mutex);
7207 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7208 } else {
7209 sata_update_port_info(sata_hba_inst, sata_device);
7210 }
7211
7212 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
7213 *rval = TRAN_FATAL_ERROR;
7214 break;
7215
7216 case SATA_TRAN_CMD_UNSUPPORTED:
7217 /*
7218 * Command rejected by HBA as unsupported. It was HBA driver
7219 * that rejected the command, command was not sent to
7220 * an attached device.
7221 */
7222 if ((sdinfo != NULL) &&
7223 (sdinfo->satadrv_state & SATA_DSTATE_RESET))
7224 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7225 "sat_hba_start: cmd 0x%2x rejected "
7226 "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd);
7227
7228 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7229 (void) sata_txlt_invalid_command(spx);
7230 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7231
7232 *rval = TRAN_ACCEPT;
7233 break;
7234
7235 case SATA_TRAN_BUSY:
7236 /*
7237 * Command rejected by HBA because other operation prevents
7238 * accepting the packet, or device is in RESET condition.
7239 */
7240 if (sdinfo != NULL) {
7241 sdinfo->satadrv_state =
7242 spx->txlt_sata_pkt->satapkt_device.satadev_state;
7243
7244 if (sdinfo->satadrv_state & SATA_DSTATE_RESET) {
7245 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7246 "sata_hba_start: cmd 0x%2x rejected "
7247 "because of device reset condition\n",
7248 cmd);
7249 } else {
7250 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7251 "sata_hba_start: cmd 0x%2x rejected "
7252 "with SATA_TRAN_BUSY status\n",
7253 cmd);
7254 }
7255 }
7256 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
7257 *rval = TRAN_BUSY;
7258 break;
7259
7260 default:
7261 /* Unrecognized HBA response */
7262 SATA_LOG_D((sata_hba_inst, CE_WARN,
7263 "sata_hba_start: unrecognized HBA response "
7264 "to cmd : 0x%2x resp 0x%x", cmd, rval));
7265 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
7266 *rval = TRAN_FATAL_ERROR;
7267 break;
7268 }
7269
7270 /*
7271 * If we got here, the packet was rejected.
7272 * Check if we need to remember reset state clearing request
7273 */
7274 if (cmd_flags.sata_clear_dev_reset) {
7275 /*
7276 * Check if device is still configured - it may have
7277 * disapeared from the configuration
7278 */
7279 sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
7280 if (sdinfo != NULL) {
7281 /*
7282 * Restore the flag that requests clearing of
7283 * the device reset state,
7284 * so the next sata packet may carry it to HBA.
7285 */
7286 if (sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT ||
7287 sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT) {
7288 pminfo->pmult_event_flags |=
7289 SATA_EVNT_CLEAR_DEVICE_RESET;
7290 } else {
7291 sdinfo->satadrv_event_flags |=
7292 SATA_EVNT_CLEAR_DEVICE_RESET;
7293 }
7294 }
7295 }
7296 return (-1);
7297 }
7298
7299 /*
7300 * Scsi response setup for invalid LBA
7301 *
7302 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
7303 */
7304 static int
sata_txlt_lba_out_of_range(sata_pkt_txlate_t * spx)7305 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx)
7306 {
7307 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7308 struct scsi_extended_sense *sense;
7309
7310 scsipkt->pkt_reason = CMD_CMPLT;
7311 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7312 STATE_SENT_CMD | STATE_GOT_STATUS;
7313 *scsipkt->pkt_scbp = STATUS_CHECK;
7314
7315 *scsipkt->pkt_scbp = STATUS_CHECK;
7316 sense = sata_arq_sense(spx);
7317 sense->es_key = KEY_ILLEGAL_REQUEST;
7318 sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7319
7320 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7321 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7322
7323 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7324 scsipkt->pkt_comp != NULL) {
7325 /* scsi callback required */
7326 if (servicing_interrupt()) {
7327 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7328 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7329 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
7330 TASKQID_INVALID) {
7331 return (TRAN_BUSY);
7332 }
7333 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7334 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7335 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
7336 /* Scheduling the callback failed */
7337 return (TRAN_BUSY);
7338 }
7339 }
7340 return (TRAN_ACCEPT);
7341 }
7342
7343
7344 /*
7345 * Analyze device status and error registers and translate them into
7346 * appropriate scsi sense codes.
7347 * NOTE: non-packet commands only for now
7348 */
7349 static void
sata_decode_device_error(sata_pkt_txlate_t * spx,struct scsi_extended_sense * sense)7350 sata_decode_device_error(sata_pkt_txlate_t *spx,
7351 struct scsi_extended_sense *sense)
7352 {
7353 uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg;
7354
7355 ASSERT(sense != NULL);
7356 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
7357 SATA_STATUS_ERR);
7358
7359
7360 if (err_reg & SATA_ERROR_ICRC) {
7361 sense->es_key = KEY_ABORTED_COMMAND;
7362 sense->es_add_code = 0x08; /* Communication failure */
7363 return;
7364 }
7365
7366 if (err_reg & SATA_ERROR_UNC) {
7367 sense->es_key = KEY_MEDIUM_ERROR;
7368 /* Information bytes (LBA) need to be set by a caller */
7369 return;
7370 }
7371
7372 /* ADD HERE: MC error bit handling for ATAPI CD/DVD */
7373 if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) {
7374 sense->es_key = KEY_UNIT_ATTENTION;
7375 sense->es_add_code = 0x3a; /* No media present */
7376 return;
7377 }
7378
7379 if (err_reg & SATA_ERROR_IDNF) {
7380 if (err_reg & SATA_ERROR_ABORT) {
7381 sense->es_key = KEY_ABORTED_COMMAND;
7382 } else {
7383 sense->es_key = KEY_ILLEGAL_REQUEST;
7384 sense->es_add_code = 0x21; /* LBA out of range */
7385 }
7386 return;
7387 }
7388
7389 if (err_reg & SATA_ERROR_ABORT) {
7390 ASSERT(spx->txlt_sata_pkt != NULL);
7391 sense->es_key = KEY_ABORTED_COMMAND;
7392 return;
7393 }
7394 }
7395
7396 /*
7397 * Extract error LBA from sata_pkt.satapkt_cmd register fields
7398 */
7399 static void
sata_extract_error_lba(sata_pkt_txlate_t * spx,uint64_t * lba)7400 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba)
7401 {
7402 sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd;
7403
7404 *lba = 0;
7405 if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) {
7406 *lba = sata_cmd->satacmd_lba_high_msb;
7407 *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb;
7408 *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb;
7409 } else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) {
7410 *lba = sata_cmd->satacmd_device_reg & 0xf;
7411 }
7412 *lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb;
7413 *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb;
7414 *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb;
7415 }
7416
7417 /*
7418 * This is fixed sense format - if LBA exceeds the info field size,
7419 * no valid info will be returned (valid bit in extended sense will
7420 * be set to 0).
7421 */
7422 static struct scsi_extended_sense *
sata_arq_sense(sata_pkt_txlate_t * spx)7423 sata_arq_sense(sata_pkt_txlate_t *spx)
7424 {
7425 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7426 struct scsi_arq_status *arqs;
7427 struct scsi_extended_sense *sense;
7428
7429 /* Fill ARQ sense data */
7430 scsipkt->pkt_state |= STATE_ARQ_DONE;
7431 arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp;
7432 *(uchar_t *)&arqs->sts_status = STATUS_CHECK;
7433 *(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD;
7434 arqs->sts_rqpkt_reason = CMD_CMPLT;
7435 arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7436 STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
7437 arqs->sts_rqpkt_resid = 0;
7438 sense = &arqs->sts_sensedata;
7439 bzero(sense, sizeof (struct scsi_extended_sense));
7440 sata_fixed_sense_data_preset(sense);
7441 return (sense);
7442 }
7443
7444 /*
7445 * ATA Pass Through support
7446 * Sets flags indicating that an invalid value was found in some
7447 * field in the command. It could be something illegal according to
7448 * the SAT-2 spec or it could be a feature that is not (yet?)
7449 * supported.
7450 */
7451 static int
sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t * spx)7452 sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *spx)
7453 {
7454 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7455 struct scsi_extended_sense *sense = sata_arq_sense(spx);
7456
7457 scsipkt->pkt_reason = CMD_CMPLT;
7458 *scsipkt->pkt_scbp = STATUS_CHECK;
7459 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7460 STATE_SENT_CMD | STATE_GOT_STATUS;
7461
7462 sense = sata_arq_sense(spx);
7463 sense->es_key = KEY_ILLEGAL_REQUEST;
7464 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
7465
7466 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7467 scsipkt->pkt_comp != NULL) {
7468 /* scsi callback required */
7469 if (servicing_interrupt()) {
7470 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7471 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7472 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
7473 TASKQID_INVALID) {
7474 return (TRAN_BUSY);
7475 }
7476 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7477 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7478 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
7479 /* Scheduling the callback failed */
7480 return (TRAN_BUSY);
7481 }
7482 }
7483
7484 return (TRAN_ACCEPT);
7485 }
7486
7487 /*
7488 * The UNMAP command considers it not to be an error if the parameter length
7489 * or block descriptor length is 0. For this case, there is nothing for TRIM
7490 * to do so just complete the command.
7491 */
7492 static int
sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t * spx)7493 sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *spx)
7494 {
7495 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7496
7497 scsipkt->pkt_reason = CMD_CMPLT;
7498 *scsipkt->pkt_scbp = STATUS_GOOD;
7499 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7500 STATE_SENT_CMD | STATE_GOT_STATUS;
7501
7502 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7503 scsipkt->pkt_comp != NULL) {
7504 /* scsi callback required */
7505 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7506 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7507 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
7508 /* Scheduling the callback failed */
7509 return (TRAN_BUSY);
7510 }
7511 }
7512
7513 return (TRAN_ACCEPT);
7514 }
7515
7516 /*
7517 * Emulated SATA Read/Write command completion for zero-length requests.
7518 * This request always succedes, so in synchronous mode it always returns
7519 * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the
7520 * callback cannot be scheduled.
7521 */
7522 static int
sata_emul_rw_completion(sata_pkt_txlate_t * spx)7523 sata_emul_rw_completion(sata_pkt_txlate_t *spx)
7524 {
7525 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7526
7527 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7528 STATE_SENT_CMD | STATE_GOT_STATUS;
7529 scsipkt->pkt_reason = CMD_CMPLT;
7530 *scsipkt->pkt_scbp = STATUS_GOOD;
7531 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
7532 /* scsi callback required - have to schedule it */
7533 if (servicing_interrupt()) {
7534 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7535 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7536 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
7537 TASKQID_INVALID) {
7538 return (TRAN_BUSY);
7539 }
7540 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7541 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7542 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
7543 /* Scheduling the callback failed */
7544 return (TRAN_BUSY);
7545 }
7546 }
7547 return (TRAN_ACCEPT);
7548 }
7549
7550
7551 /*
7552 * Translate completion status of SATA read/write commands into scsi response.
7553 * pkt completion_reason is checked to determine the completion status.
7554 * Do scsi callback if necessary.
7555 *
7556 * Note: this function may be called also for synchronously executed
7557 * commands.
7558 * This function may be used only if scsi_pkt is non-NULL.
7559 */
7560 static void
sata_txlt_rw_completion(sata_pkt_t * sata_pkt)7561 sata_txlt_rw_completion(sata_pkt_t *sata_pkt)
7562 {
7563 sata_pkt_txlate_t *spx =
7564 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7565 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7566 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7567 struct scsi_extended_sense *sense;
7568 uint64_t lba;
7569 struct buf *bp;
7570 int rval;
7571 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7572 /* Normal completion */
7573 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7574 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7575 scsipkt->pkt_reason = CMD_CMPLT;
7576 *scsipkt->pkt_scbp = STATUS_GOOD;
7577 if (spx->txlt_tmp_buf != NULL) {
7578 /* Temporary buffer was used */
7579 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7580 if (bp->b_flags & B_READ) {
7581 rval = ddi_dma_sync(
7582 spx->txlt_buf_dma_handle, 0, 0,
7583 DDI_DMA_SYNC_FORCPU);
7584 ASSERT(rval == DDI_SUCCESS);
7585 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7586 bp->b_bcount);
7587 }
7588 }
7589 } else {
7590 /*
7591 * Something went wrong - analyze return
7592 */
7593 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7594 STATE_SENT_CMD | STATE_GOT_STATUS;
7595 scsipkt->pkt_reason = CMD_INCOMPLETE;
7596 *scsipkt->pkt_scbp = STATUS_CHECK;
7597 sense = sata_arq_sense(spx);
7598 ASSERT(sense != NULL);
7599
7600 /*
7601 * SATA_PKT_DEV_ERROR is the only case where we may be able to
7602 * extract from device registers the failing LBA.
7603 */
7604 if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7605 if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) &&
7606 (scmd->satacmd_lba_mid_msb != 0 ||
7607 scmd->satacmd_lba_high_msb != 0)) {
7608 /*
7609 * We have problem reporting this cmd LBA
7610 * in fixed sense data format, because of
7611 * the size of the scsi LBA fields.
7612 */
7613 sense->es_valid = 0;
7614 } else {
7615 sata_extract_error_lba(spx, &lba);
7616 sense->es_info_1 = (lba & 0xFF000000) >> 24;
7617 sense->es_info_2 = (lba & 0xFF0000) >> 16;
7618 sense->es_info_3 = (lba & 0xFF00) >> 8;
7619 sense->es_info_4 = lba & 0xFF;
7620 }
7621 } else {
7622 /* Invalid extended sense info */
7623 sense->es_valid = 0;
7624 }
7625
7626 switch (sata_pkt->satapkt_reason) {
7627 case SATA_PKT_PORT_ERROR:
7628 /* We may want to handle DEV GONE state as well */
7629 /*
7630 * We have no device data. Assume no data transfered.
7631 */
7632 sense->es_key = KEY_HARDWARE_ERROR;
7633 break;
7634
7635 case SATA_PKT_DEV_ERROR:
7636 if (sata_pkt->satapkt_cmd.satacmd_status_reg &
7637 SATA_STATUS_ERR) {
7638 /*
7639 * determine dev error reason from error
7640 * reg content
7641 */
7642 sata_decode_device_error(spx, sense);
7643 if (sense->es_key == KEY_MEDIUM_ERROR) {
7644 switch (scmd->satacmd_cmd_reg) {
7645 case SATAC_READ_DMA:
7646 case SATAC_READ_DMA_EXT:
7647 case SATAC_READ_DMA_QUEUED:
7648 case SATAC_READ_DMA_QUEUED_EXT:
7649 case SATAC_READ_FPDMA_QUEUED:
7650 /* Unrecovered read error */
7651 sense->es_add_code =
7652 SD_SCSI_ASC_UNREC_READ_ERR;
7653 break;
7654 case SATAC_WRITE_DMA:
7655 case SATAC_WRITE_DMA_EXT:
7656 case SATAC_WRITE_DMA_QUEUED:
7657 case SATAC_WRITE_DMA_QUEUED_EXT:
7658 case SATAC_WRITE_FPDMA_QUEUED:
7659 /* Write error */
7660 sense->es_add_code =
7661 SD_SCSI_ASC_WRITE_ERR;
7662 break;
7663 default:
7664 /* Internal error */
7665 SATA_LOG_D((
7666 spx->txlt_sata_hba_inst,
7667 CE_WARN,
7668 "sata_txlt_rw_completion :"
7669 "internal error - invalid "
7670 "command 0x%2x",
7671 scmd->satacmd_cmd_reg));
7672 break;
7673 }
7674 }
7675 break;
7676 }
7677 /* No extended sense key - no info available */
7678 scsipkt->pkt_reason = CMD_INCOMPLETE;
7679 break;
7680
7681 case SATA_PKT_TIMEOUT:
7682 scsipkt->pkt_reason = CMD_TIMEOUT;
7683 scsipkt->pkt_statistics |=
7684 STAT_TIMEOUT | STAT_DEV_RESET;
7685 sense->es_key = KEY_ABORTED_COMMAND;
7686 break;
7687
7688 case SATA_PKT_ABORTED:
7689 scsipkt->pkt_reason = CMD_ABORTED;
7690 scsipkt->pkt_statistics |= STAT_ABORTED;
7691 sense->es_key = KEY_ABORTED_COMMAND;
7692 break;
7693
7694 case SATA_PKT_RESET:
7695 scsipkt->pkt_reason = CMD_RESET;
7696 scsipkt->pkt_statistics |= STAT_DEV_RESET;
7697 sense->es_key = KEY_ABORTED_COMMAND;
7698 break;
7699
7700 default:
7701 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7702 "sata_txlt_rw_completion: "
7703 "invalid packet completion reason"));
7704 scsipkt->pkt_reason = CMD_TRAN_ERR;
7705 break;
7706 }
7707 }
7708 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7709 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7710
7711 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7712 /* scsi callback required */
7713 scsi_hba_pkt_comp(scsipkt);
7714 }
7715
7716
7717 /*
7718 * Translate completion status of non-data commands (i.e. commands returning
7719 * no data).
7720 * pkt completion_reason is checked to determine the completion status.
7721 * Do scsi callback if necessary (FLAG_NOINTR == 0)
7722 *
7723 * Note: this function may be called also for synchronously executed
7724 * commands.
7725 * This function may be used only if scsi_pkt is non-NULL.
7726 */
7727
7728 static void
sata_txlt_nodata_cmd_completion(sata_pkt_t * sata_pkt)7729 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt)
7730 {
7731 sata_pkt_txlate_t *spx =
7732 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7733 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7734
7735 sata_set_arq_data(sata_pkt);
7736
7737 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7738 /* scsi callback required */
7739 scsi_hba_pkt_comp(scsipkt);
7740 }
7741
7742 /*
7743 * Completion handler for ATA Pass Through command
7744 */
7745 static void
sata_txlt_apt_completion(sata_pkt_t * sata_pkt)7746 sata_txlt_apt_completion(sata_pkt_t *sata_pkt)
7747 {
7748 sata_pkt_txlate_t *spx =
7749 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7750 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7751 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7752 struct buf *bp;
7753 uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0;
7754
7755 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7756 /* Normal completion */
7757 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7758 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7759 scsipkt->pkt_reason = CMD_CMPLT;
7760 *scsipkt->pkt_scbp = STATUS_GOOD;
7761
7762 /*
7763 * If the command has CK_COND set
7764 */
7765 if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) {
7766 *scsipkt->pkt_scbp = STATUS_CHECK;
7767 sata_fill_ata_return_desc(sata_pkt,
7768 KEY_RECOVERABLE_ERROR,
7769 SD_SCSI_ASC_APT_INFO_AVAIL, 0x1d);
7770 }
7771
7772 if (spx->txlt_tmp_buf != NULL) {
7773 /* Temporary buffer was used */
7774 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7775 if (bp->b_flags & B_READ) {
7776 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7777 bp->b_bcount);
7778 }
7779 }
7780 } else {
7781 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7782 STATE_SENT_CMD | STATE_GOT_STATUS;
7783 scsipkt->pkt_reason = CMD_INCOMPLETE;
7784 *scsipkt->pkt_scbp = STATUS_CHECK;
7785
7786 /*
7787 * If DF or ERR was set, the HBA should have copied out the
7788 * status and error registers to the satacmd structure.
7789 */
7790 if (scmd->satacmd_status_reg & SATA_STATUS_DF) {
7791 sense_key = KEY_HARDWARE_ERROR;
7792 addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE;
7793 addl_sense_qual = 0;
7794 } else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
7795 if (scmd->satacmd_error_reg & SATA_ERROR_NM) {
7796 sense_key = KEY_NOT_READY;
7797 addl_sense_code =
7798 SD_SCSI_ASC_MEDIUM_NOT_PRESENT;
7799 addl_sense_qual = 0;
7800 } else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) {
7801 sense_key = KEY_MEDIUM_ERROR;
7802 addl_sense_code = SD_SCSI_ASC_UNREC_READ_ERR;
7803 addl_sense_qual = 0;
7804 } else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) {
7805 sense_key = KEY_DATA_PROTECT;
7806 addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED;
7807 addl_sense_qual = 0;
7808 } else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) {
7809 sense_key = KEY_ILLEGAL_REQUEST;
7810 addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7811 addl_sense_qual = 0;
7812 } else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
7813 sense_key = KEY_ABORTED_COMMAND;
7814 addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE;
7815 addl_sense_qual = 0;
7816 } else if (scmd->satacmd_error_reg & SATA_ERROR_MC) {
7817 sense_key = KEY_UNIT_ATTENTION;
7818 addl_sense_code =
7819 SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED;
7820 addl_sense_qual = 0;
7821 } else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) {
7822 sense_key = KEY_UNIT_ATTENTION;
7823 addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ;
7824 addl_sense_qual = 0;
7825 } else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) {
7826 sense_key = KEY_ABORTED_COMMAND;
7827 addl_sense_code =
7828 SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR;
7829 addl_sense_qual = 0;
7830 }
7831 }
7832
7833 sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code,
7834 addl_sense_qual);
7835 }
7836
7837 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7838 /* scsi callback required */
7839 scsi_hba_pkt_comp(scsipkt);
7840 }
7841
7842 /*
7843 * Completion handler for unmap translation command
7844 */
7845 static void
sata_txlt_unmap_completion(sata_pkt_t * sata_pkt)7846 sata_txlt_unmap_completion(sata_pkt_t *sata_pkt)
7847 {
7848 sata_pkt_txlate_t *spx =
7849 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7850 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7851 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7852 struct buf *bp;
7853 uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0;
7854
7855 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7856 /* Normal completion */
7857 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7858 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7859 scsipkt->pkt_reason = CMD_CMPLT;
7860 *scsipkt->pkt_scbp = STATUS_GOOD;
7861
7862 if (spx->txlt_tmp_buf != NULL) {
7863 /* Temporary buffer was used */
7864 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7865 if (bp->b_flags & B_READ) {
7866 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7867 bp->b_bcount);
7868 }
7869 }
7870 } else {
7871 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7872 STATE_SENT_CMD | STATE_GOT_STATUS;
7873 scsipkt->pkt_reason = CMD_INCOMPLETE;
7874 *scsipkt->pkt_scbp = STATUS_CHECK;
7875
7876 /*
7877 * If DF or ERR was set, the HBA should have copied out the
7878 * status and error registers to the satacmd structure.
7879 */
7880 if (scmd->satacmd_status_reg & SATA_STATUS_DF) {
7881 sense_key = KEY_HARDWARE_ERROR;
7882 addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE;
7883 addl_sense_qual = 0;
7884 } else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
7885 if (scmd->satacmd_error_reg & SATA_ERROR_NM) {
7886 sense_key = KEY_NOT_READY;
7887 addl_sense_code =
7888 SD_SCSI_ASC_MEDIUM_NOT_PRESENT;
7889 addl_sense_qual = 0;
7890 } else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) {
7891 sense_key = KEY_MEDIUM_ERROR;
7892 addl_sense_code = SD_SCSI_ASC_WRITE_ERR;
7893 addl_sense_qual = 0;
7894 } else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) {
7895 sense_key = KEY_DATA_PROTECT;
7896 addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED;
7897 addl_sense_qual = 0;
7898 } else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) {
7899 sense_key = KEY_ILLEGAL_REQUEST;
7900 addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7901 addl_sense_qual = 0;
7902 } else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
7903 sense_key = KEY_ABORTED_COMMAND;
7904 addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE;
7905 addl_sense_qual = 0;
7906 } else if (scmd->satacmd_error_reg & SATA_ERROR_MC) {
7907 sense_key = KEY_UNIT_ATTENTION;
7908 addl_sense_code =
7909 SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED;
7910 addl_sense_qual = 0;
7911 } else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) {
7912 sense_key = KEY_UNIT_ATTENTION;
7913 addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ;
7914 addl_sense_qual = 0;
7915 } else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) {
7916 sense_key = KEY_ABORTED_COMMAND;
7917 addl_sense_code =
7918 SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR;
7919 addl_sense_qual = 0;
7920 }
7921 }
7922
7923 sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code,
7924 addl_sense_qual);
7925 }
7926
7927 sata_free_local_buffer(spx);
7928
7929 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7930 /* scsi callback required */
7931 scsi_hba_pkt_comp(scsipkt);
7932 }
7933
7934 /*
7935 *
7936 */
7937 static void
sata_fill_ata_return_desc(sata_pkt_t * sata_pkt,uint8_t sense_key,uint8_t addl_sense_code,uint8_t addl_sense_qual)7938 sata_fill_ata_return_desc(sata_pkt_t *sata_pkt, uint8_t sense_key,
7939 uint8_t addl_sense_code, uint8_t addl_sense_qual)
7940 {
7941 sata_pkt_txlate_t *spx =
7942 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7943 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7944 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7945 struct sata_apt_sense_data *apt_sd =
7946 (struct sata_apt_sense_data *)scsipkt->pkt_scbp;
7947 struct scsi_descr_sense_hdr *sds = &(apt_sd->apt_sd_hdr);
7948 struct scsi_ata_status_ret_sense_descr *ata_ret_desc =
7949 &(apt_sd->apt_sd_sense);
7950 int extend = 0;
7951
7952 if ((scsipkt->pkt_cdbp[0] == SPC3_CMD_ATA_COMMAND_PASS_THROUGH16) &&
7953 (scsipkt->pkt_cdbp[2] & SATL_APT_BM_EXTEND))
7954 extend = 1;
7955
7956 scsipkt->pkt_state |= STATE_ARQ_DONE;
7957
7958 /* update the residual count */
7959 *(uchar_t *)&apt_sd->apt_status = STATUS_CHECK;
7960 *(uchar_t *)&apt_sd->apt_rqpkt_status = STATUS_GOOD;
7961 apt_sd->apt_rqpkt_reason = CMD_CMPLT;
7962 apt_sd->apt_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7963 STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
7964 apt_sd->apt_rqpkt_resid = scsipkt->pkt_scblen -
7965 sizeof (struct sata_apt_sense_data);
7966
7967 /*
7968 * Fill in the Descriptor sense header
7969 */
7970 bzero(sds, sizeof (struct scsi_descr_sense_hdr));
7971 sds->ds_code = CODE_FMT_DESCR_CURRENT;
7972 sds->ds_class = CLASS_EXTENDED_SENSE;
7973 sds->ds_key = sense_key & 0xf;
7974 sds->ds_add_code = addl_sense_code;
7975 sds->ds_qual_code = addl_sense_qual;
7976 sds->ds_addl_sense_length =
7977 sizeof (struct scsi_ata_status_ret_sense_descr);
7978
7979 /*
7980 * Fill in the ATA Return descriptor sense data
7981 */
7982 bzero(ata_ret_desc, sizeof (struct scsi_ata_status_ret_sense_descr));
7983 ata_ret_desc->ars_descr_type = DESCR_ATA_STATUS_RETURN;
7984 ata_ret_desc->ars_addl_length = 0xc;
7985 ata_ret_desc->ars_error = scmd->satacmd_error_reg;
7986 ata_ret_desc->ars_sec_count_lsb = scmd->satacmd_sec_count_lsb;
7987 ata_ret_desc->ars_lba_low_lsb = scmd->satacmd_lba_low_lsb;
7988 ata_ret_desc->ars_lba_mid_lsb = scmd->satacmd_lba_mid_lsb;
7989 ata_ret_desc->ars_lba_high_lsb = scmd->satacmd_lba_high_lsb;
7990 ata_ret_desc->ars_device = scmd->satacmd_device_reg;
7991 ata_ret_desc->ars_status = scmd->satacmd_status_reg;
7992
7993 if (extend == 1) {
7994 ata_ret_desc->ars_extend = 1;
7995 ata_ret_desc->ars_sec_count_msb = scmd->satacmd_sec_count_msb;
7996 ata_ret_desc->ars_lba_low_msb = scmd->satacmd_lba_low_msb;
7997 ata_ret_desc->ars_lba_mid_msb = scmd->satacmd_lba_mid_msb;
7998 ata_ret_desc->ars_lba_high_msb = scmd->satacmd_lba_high_msb;
7999 } else {
8000 ata_ret_desc->ars_extend = 0;
8001 ata_ret_desc->ars_sec_count_msb = 0;
8002 ata_ret_desc->ars_lba_low_msb = 0;
8003 ata_ret_desc->ars_lba_mid_msb = 0;
8004 ata_ret_desc->ars_lba_high_msb = 0;
8005 }
8006 }
8007
8008 static void
sata_set_arq_data(sata_pkt_t * sata_pkt)8009 sata_set_arq_data(sata_pkt_t *sata_pkt)
8010 {
8011 sata_pkt_txlate_t *spx =
8012 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
8013 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8014 struct scsi_extended_sense *sense;
8015
8016 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
8017 STATE_SENT_CMD | STATE_GOT_STATUS;
8018 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
8019 /* Normal completion */
8020 scsipkt->pkt_reason = CMD_CMPLT;
8021 *scsipkt->pkt_scbp = STATUS_GOOD;
8022 } else {
8023 /* Something went wrong */
8024 scsipkt->pkt_reason = CMD_INCOMPLETE;
8025 *scsipkt->pkt_scbp = STATUS_CHECK;
8026 sense = sata_arq_sense(spx);
8027 switch (sata_pkt->satapkt_reason) {
8028 case SATA_PKT_PORT_ERROR:
8029 /*
8030 * We have no device data. Assume no data transfered.
8031 */
8032 sense->es_key = KEY_HARDWARE_ERROR;
8033 break;
8034
8035 case SATA_PKT_DEV_ERROR:
8036 if (sata_pkt->satapkt_cmd.satacmd_status_reg &
8037 SATA_STATUS_ERR) {
8038 /*
8039 * determine dev error reason from error
8040 * reg content
8041 */
8042 sata_decode_device_error(spx, sense);
8043 break;
8044 }
8045 /* No extended sense key - no info available */
8046 break;
8047
8048 case SATA_PKT_TIMEOUT:
8049 scsipkt->pkt_reason = CMD_TIMEOUT;
8050 scsipkt->pkt_statistics |=
8051 STAT_TIMEOUT | STAT_DEV_RESET;
8052 /* No extended sense key ? */
8053 break;
8054
8055 case SATA_PKT_ABORTED:
8056 scsipkt->pkt_reason = CMD_ABORTED;
8057 scsipkt->pkt_statistics |= STAT_ABORTED;
8058 /* No extended sense key ? */
8059 break;
8060
8061 case SATA_PKT_RESET:
8062 /* pkt aborted by an explicit reset from a host */
8063 scsipkt->pkt_reason = CMD_RESET;
8064 scsipkt->pkt_statistics |= STAT_DEV_RESET;
8065 break;
8066
8067 default:
8068 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
8069 "sata_txlt_nodata_cmd_completion: "
8070 "invalid packet completion reason %d",
8071 sata_pkt->satapkt_reason));
8072 scsipkt->pkt_reason = CMD_TRAN_ERR;
8073 break;
8074 }
8075
8076 }
8077 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
8078 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
8079 }
8080
8081
8082 /*
8083 * Build Mode sense R/W recovery page
8084 * NOT IMPLEMENTED
8085 */
8086
8087 static int
sata_build_msense_page_1(sata_drive_info_t * sdinfo,int pcntrl,uint8_t * buf)8088 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8089 {
8090 #ifndef __lock_lint
8091 _NOTE(ARGUNUSED(sdinfo))
8092 _NOTE(ARGUNUSED(pcntrl))
8093 _NOTE(ARGUNUSED(buf))
8094 #endif
8095 return (0);
8096 }
8097
8098 /*
8099 * Build Mode sense caching page - scsi-3 implementation.
8100 * Page length distinguishes previous format from scsi-3 format.
8101 * buf must have space for 0x12 bytes.
8102 * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable.
8103 *
8104 */
8105 static int
sata_build_msense_page_8(sata_drive_info_t * sdinfo,int pcntrl,uint8_t * buf)8106 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8107 {
8108 struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf;
8109 sata_id_t *sata_id = &sdinfo->satadrv_id;
8110
8111 /*
8112 * Most of the fields are set to 0, being not supported and/or disabled
8113 */
8114 bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3);
8115
8116 /* Saved paramters not supported */
8117 if (pcntrl == 3)
8118 return (0);
8119 if (pcntrl == 0 || pcntrl == 2) {
8120 /*
8121 * For now treat current and default parameters as same
8122 * That may have to change, if target driver will complain
8123 */
8124 page->mode_page.code = MODEPAGE_CACHING; /* PS = 0 */
8125 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
8126
8127 if (SATA_READ_AHEAD_SUPPORTED(*sata_id) &&
8128 !SATA_READ_AHEAD_ENABLED(*sata_id)) {
8129 page->dra = 1; /* Read Ahead disabled */
8130 page->rcd = 1; /* Read Cache disabled */
8131 }
8132 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id) &&
8133 SATA_WRITE_CACHE_ENABLED(*sata_id))
8134 page->wce = 1; /* Write Cache enabled */
8135 } else {
8136 /* Changeable parameters */
8137 page->mode_page.code = MODEPAGE_CACHING;
8138 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
8139 if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
8140 page->dra = 1;
8141 page->rcd = 1;
8142 }
8143 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id))
8144 page->wce = 1;
8145 }
8146 return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
8147 sizeof (struct mode_page));
8148 }
8149
8150 /*
8151 * Build Mode sense exception cntrl page
8152 */
8153 static int
sata_build_msense_page_1c(sata_drive_info_t * sdinfo,int pcntrl,uint8_t * buf)8154 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8155 {
8156 struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf;
8157 sata_id_t *sata_id = &sdinfo->satadrv_id;
8158
8159 /*
8160 * Most of the fields are set to 0, being not supported and/or disabled
8161 */
8162 bzero(buf, PAGELENGTH_INFO_EXCPT);
8163
8164 page->mode_page.code = MODEPAGE_INFO_EXCPT;
8165 page->mode_page.length = PAGELENGTH_INFO_EXCPT;
8166
8167 /* Indicate that this is page is saveable */
8168 page->mode_page.ps = 1;
8169
8170 /*
8171 * We will return the same data for default, current and saved page.
8172 * The only changeable bit is dexcpt and that bit is required
8173 * by the ATA specification to be preserved across power cycles.
8174 */
8175 if (pcntrl != 1) {
8176 page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED);
8177 page->mrie = MRIE_ONLY_ON_REQUEST;
8178 }
8179 else
8180 page->dexcpt = 1; /* Only changeable parameter */
8181
8182 return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page));
8183 }
8184
8185
8186 static int
sata_build_msense_page_30(sata_drive_info_t * sdinfo,int pcntrl,uint8_t * buf)8187 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8188 {
8189 struct mode_acoustic_management *page =
8190 (struct mode_acoustic_management *)buf;
8191 sata_id_t *sata_id = &sdinfo->satadrv_id;
8192
8193 /*
8194 * Most of the fields are set to 0, being not supported and/or disabled
8195 */
8196 bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT);
8197
8198 switch (pcntrl) {
8199 case P_CNTRL_DEFAULT:
8200 /* default paramters not supported */
8201 return (0);
8202
8203 case P_CNTRL_CURRENT:
8204 case P_CNTRL_SAVED:
8205 /* Saved and current are supported and are identical */
8206 page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
8207 page->mode_page.length =
8208 PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
8209 page->mode_page.ps = 1;
8210
8211 /* Word 83 indicates if feature is supported */
8212 /* If feature is not supported */
8213 if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) {
8214 page->acoustic_manag_enable =
8215 ACOUSTIC_DISABLED;
8216 } else {
8217 page->acoustic_manag_enable =
8218 ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT)
8219 != 0);
8220 /* Word 94 inidicates the value */
8221 #ifdef _LITTLE_ENDIAN
8222 page->acoustic_manag_level =
8223 (uchar_t)sata_id->ai_acoustic;
8224 page->vendor_recommended_value =
8225 sata_id->ai_acoustic >> 8;
8226 #else
8227 page->acoustic_manag_level =
8228 sata_id->ai_acoustic >> 8;
8229 page->vendor_recommended_value =
8230 (uchar_t)sata_id->ai_acoustic;
8231 #endif
8232 }
8233 break;
8234
8235 case P_CNTRL_CHANGEABLE:
8236 page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
8237 page->mode_page.length =
8238 PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
8239 page->mode_page.ps = 1;
8240
8241 /* Word 83 indicates if the feature is supported */
8242 if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) {
8243 page->acoustic_manag_enable =
8244 ACOUSTIC_ENABLED;
8245 page->acoustic_manag_level = 0xff;
8246 }
8247 break;
8248 }
8249 return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8250 sizeof (struct mode_page));
8251 }
8252
8253
8254 /*
8255 * Build Mode sense power condition page.
8256 */
8257 static int
sata_build_msense_page_1a(sata_drive_info_t * sdinfo,int pcntrl,uint8_t * buf)8258 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8259 {
8260 struct mode_info_power_cond *page = (struct mode_info_power_cond *)buf;
8261 sata_id_t *sata_id = &sdinfo->satadrv_id;
8262
8263 /*
8264 * Most of the fields are set to 0, being not supported and/or disabled
8265 * power condition page length was 0x0a
8266 */
8267 bzero(buf, sizeof (struct mode_info_power_cond));
8268
8269 if (pcntrl == P_CNTRL_DEFAULT) {
8270 /* default paramters not supported */
8271 return (0);
8272 }
8273
8274 page->mode_page.code = MODEPAGE_POWER_COND;
8275 page->mode_page.length = sizeof (struct mode_info_power_cond);
8276
8277 if (sata_id->ai_cap & SATA_STANDBYTIMER) {
8278 page->standby = 1;
8279 bcopy(sdinfo->satadrv_standby_timer, page->standby_cond_timer,
8280 sizeof (uchar_t) * 4);
8281 }
8282
8283 return (sizeof (struct mode_info_power_cond));
8284 }
8285
8286 /*
8287 * Process mode select caching page 8 (scsi3 format only).
8288 * Read Ahead (same as read cache) and Write Cache may be turned on and off
8289 * if these features are supported by the device. If these features are not
8290 * supported, the command will be terminated with STATUS_CHECK.
8291 * This function fails only if the SET FEATURE command sent to
8292 * the device fails. The page format is not verified, assuming that the
8293 * target driver operates correctly - if parameters length is too short,
8294 * we just drop the page.
8295 * Two command may be sent if both Read Cache/Read Ahead and Write Cache
8296 * setting have to be changed.
8297 * SET FEATURE command is executed synchronously, i.e. we wait here until
8298 * it is completed, regardless of the scsi pkt directives.
8299 *
8300 * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e.
8301 * changing DRA will change RCD.
8302 *
8303 * More than one SATA command may be executed to perform operations specified
8304 * by mode select pages. The first error terminates further execution.
8305 * Operations performed successully are not backed-up in such case.
8306 *
8307 * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
8308 * If operation resulted in changing device setup, dmod flag should be set to
8309 * one (1). If parameters were not changed, dmod flag should be set to 0.
8310 * Upon return, if operation required sending command to the device, the rval
8311 * should be set to the value returned by sata_hba_start. If operation
8312 * did not require device access, rval should be set to TRAN_ACCEPT.
8313 * The pagelen should be set to the length of the page.
8314 *
8315 * This function has to be called with a port mutex held.
8316 *
8317 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8318 */
8319 int
sata_mode_select_page_8(sata_pkt_txlate_t * spx,struct mode_cache_scsi3 * page,int parmlen,int * pagelen,int * rval,int * dmod)8320 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page,
8321 int parmlen, int *pagelen, int *rval, int *dmod)
8322 {
8323 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8324 sata_drive_info_t *sdinfo;
8325 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8326 sata_id_t *sata_id;
8327 struct scsi_extended_sense *sense;
8328 int wce, dra; /* Current settings */
8329
8330 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8331 &spx->txlt_sata_pkt->satapkt_device);
8332 sata_id = &sdinfo->satadrv_id;
8333 *dmod = 0;
8334
8335 /* Verify parameters length. If too short, drop it */
8336 if ((PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
8337 sizeof (struct mode_page)) > parmlen) {
8338 *scsipkt->pkt_scbp = STATUS_CHECK;
8339 sense = sata_arq_sense(spx);
8340 sense->es_key = KEY_ILLEGAL_REQUEST;
8341 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8342 *pagelen = parmlen;
8343 *rval = TRAN_ACCEPT;
8344 return (SATA_FAILURE);
8345 }
8346
8347 *pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page);
8348
8349 /* Current setting of Read Ahead (and Read Cache) */
8350 if (SATA_READ_AHEAD_ENABLED(*sata_id))
8351 dra = 0; /* 0 == not disabled */
8352 else
8353 dra = 1;
8354 /* Current setting of Write Cache */
8355 if (SATA_WRITE_CACHE_ENABLED(*sata_id))
8356 wce = 1;
8357 else
8358 wce = 0;
8359
8360 if (page->dra == dra && page->wce == wce && page->rcd == dra) {
8361 /* nothing to do */
8362 *rval = TRAN_ACCEPT;
8363 return (SATA_SUCCESS);
8364 }
8365
8366 /*
8367 * Need to flip some setting
8368 * Set-up Internal SET FEATURES command(s)
8369 */
8370 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8371 scmd->satacmd_addr_type = 0;
8372 scmd->satacmd_device_reg = 0;
8373 scmd->satacmd_status_reg = 0;
8374 scmd->satacmd_error_reg = 0;
8375 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
8376 if (page->dra != dra || page->rcd != dra) {
8377 if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
8378 /* Need to flip read ahead setting */
8379 if (dra == 0)
8380 /* Disable read ahead / read cache */
8381 scmd->satacmd_features_reg =
8382 SATAC_SF_DISABLE_READ_AHEAD;
8383 else
8384 /* Enable read ahead / read cache */
8385 scmd->satacmd_features_reg =
8386 SATAC_SF_ENABLE_READ_AHEAD;
8387
8388 /* Transfer command to HBA */
8389 if (sata_hba_start(spx, rval) != 0)
8390 /*
8391 * Pkt not accepted for execution.
8392 */
8393 return (SATA_FAILURE);
8394
8395 *dmod = 1;
8396
8397 /* Now process return */
8398 if (spx->txlt_sata_pkt->satapkt_reason !=
8399 SATA_PKT_COMPLETED) {
8400 goto failure; /* Terminate */
8401 }
8402 } else {
8403 *scsipkt->pkt_scbp = STATUS_CHECK;
8404 sense = sata_arq_sense(spx);
8405 sense->es_key = KEY_ILLEGAL_REQUEST;
8406 sense->es_add_code =
8407 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8408 *pagelen = parmlen;
8409 *rval = TRAN_ACCEPT;
8410 return (SATA_FAILURE);
8411 }
8412 }
8413
8414 /* Note that the packet is not removed, so it could be re-used */
8415 if (page->wce != wce) {
8416 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id)) {
8417 /* Need to flip Write Cache setting */
8418 if (page->wce == 1)
8419 /* Enable write cache */
8420 scmd->satacmd_features_reg =
8421 SATAC_SF_ENABLE_WRITE_CACHE;
8422 else
8423 /* Disable write cache */
8424 scmd->satacmd_features_reg =
8425 SATAC_SF_DISABLE_WRITE_CACHE;
8426
8427 /* Transfer command to HBA */
8428 if (sata_hba_start(spx, rval) != 0)
8429 /*
8430 * Pkt not accepted for execution.
8431 */
8432 return (SATA_FAILURE);
8433
8434 *dmod = 1;
8435
8436 /* Now process return */
8437 if (spx->txlt_sata_pkt->satapkt_reason !=
8438 SATA_PKT_COMPLETED) {
8439 goto failure;
8440 }
8441 } else {
8442 *scsipkt->pkt_scbp = STATUS_CHECK;
8443 sense = sata_arq_sense(spx);
8444 sense->es_key = KEY_ILLEGAL_REQUEST;
8445 sense->es_add_code =
8446 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8447 *pagelen = parmlen;
8448 *rval = TRAN_ACCEPT;
8449 return (SATA_FAILURE);
8450 }
8451 }
8452 return (SATA_SUCCESS);
8453
8454 failure:
8455 sata_xlate_errors(spx);
8456
8457 return (SATA_FAILURE);
8458 }
8459
8460 /*
8461 * Process mode select informational exceptions control page 0x1c
8462 *
8463 * The only changeable bit is dexcpt (disable exceptions).
8464 * MRIE (method of reporting informational exceptions) must be
8465 * "only on request".
8466 * This page applies to informational exceptions that report
8467 * additional sense codes with the ADDITIONAL SENSE CODE field set to 5Dh
8468 * (e.g.,FAILURE PREDICTION THRESHOLD EXCEEDED) or 0Bh (e.g., WARNING_).
8469 * Informational exception conditions occur as the result of background scan
8470 * errors, background self-test errors, or vendor specific events within a
8471 * logical unit. An informational exception condition may occur asynchronous
8472 * to any commands.
8473 *
8474 * Returns: SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
8475 * If operation resulted in changing device setup, dmod flag should be set to
8476 * one (1). If parameters were not changed, dmod flag should be set to 0.
8477 * Upon return, if operation required sending command to the device, the rval
8478 * should be set to the value returned by sata_hba_start. If operation
8479 * did not require device access, rval should be set to TRAN_ACCEPT.
8480 * The pagelen should be set to the length of the page.
8481 *
8482 * This function has to be called with a port mutex held.
8483 *
8484 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8485 *
8486 * Cannot be called in the interrupt context.
8487 */
8488 static int
sata_mode_select_page_1c(sata_pkt_txlate_t * spx,struct mode_info_excpt_page * page,int parmlen,int * pagelen,int * rval,int * dmod)8489 sata_mode_select_page_1c(
8490 sata_pkt_txlate_t *spx,
8491 struct mode_info_excpt_page *page,
8492 int parmlen,
8493 int *pagelen,
8494 int *rval,
8495 int *dmod)
8496 {
8497 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8498 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8499 sata_drive_info_t *sdinfo;
8500 sata_id_t *sata_id;
8501 struct scsi_extended_sense *sense;
8502
8503 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8504 &spx->txlt_sata_pkt->satapkt_device);
8505 sata_id = &sdinfo->satadrv_id;
8506
8507 *dmod = 0;
8508
8509 /* Verify parameters length. If too short, drop it */
8510 if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) > parmlen) ||
8511 page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) {
8512 *scsipkt->pkt_scbp = STATUS_CHECK;
8513 sense = sata_arq_sense(spx);
8514 sense->es_key = KEY_ILLEGAL_REQUEST;
8515 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8516 *pagelen = parmlen;
8517 *rval = TRAN_ACCEPT;
8518 return (SATA_FAILURE);
8519 }
8520
8521 *pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page);
8522
8523 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
8524 *scsipkt->pkt_scbp = STATUS_CHECK;
8525 sense = sata_arq_sense(spx);
8526 sense->es_key = KEY_ILLEGAL_REQUEST;
8527 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
8528 *pagelen = parmlen;
8529 *rval = TRAN_ACCEPT;
8530 return (SATA_FAILURE);
8531 }
8532
8533 /* If already in the state requested, we are done */
8534 if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
8535 /* nothing to do */
8536 *rval = TRAN_ACCEPT;
8537 return (SATA_SUCCESS);
8538 }
8539
8540 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8541
8542 /* Build SMART_ENABLE or SMART_DISABLE command */
8543 scmd->satacmd_addr_type = 0; /* N/A */
8544 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
8545 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
8546 scmd->satacmd_features_reg = page->dexcpt ?
8547 SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS;
8548 scmd->satacmd_device_reg = 0; /* Always device 0 */
8549 scmd->satacmd_cmd_reg = SATAC_SMART;
8550
8551 /* Transfer command to HBA */
8552 if (sata_hba_start(spx, rval) != 0)
8553 /*
8554 * Pkt not accepted for execution.
8555 */
8556 return (SATA_FAILURE);
8557
8558 *dmod = 1; /* At least may have been modified */
8559
8560 /* Now process return */
8561 if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED)
8562 return (SATA_SUCCESS);
8563
8564 /* Packet did not complete successfully */
8565 sata_xlate_errors(spx);
8566
8567 return (SATA_FAILURE);
8568 }
8569
8570 /*
8571 * Process mode select acoustic management control page 0x30
8572 *
8573 *
8574 * This function has to be called with a port mutex held.
8575 *
8576 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8577 *
8578 * Cannot be called in the interrupt context.
8579 */
8580 int
sata_mode_select_page_30(sata_pkt_txlate_t * spx,struct mode_acoustic_management * page,int parmlen,int * pagelen,int * rval,int * dmod)8581 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct
8582 mode_acoustic_management *page, int parmlen, int *pagelen,
8583 int *rval, int *dmod)
8584 {
8585 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8586 sata_drive_info_t *sdinfo;
8587 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8588 sata_id_t *sata_id;
8589 struct scsi_extended_sense *sense;
8590
8591 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8592 &spx->txlt_sata_pkt->satapkt_device);
8593 sata_id = &sdinfo->satadrv_id;
8594 *dmod = 0;
8595
8596 /* If parmlen is too short or the feature is not supported, drop it */
8597 if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8598 sizeof (struct mode_page)) > parmlen) ||
8599 (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) {
8600 *scsipkt->pkt_scbp = STATUS_CHECK;
8601 sense = sata_arq_sense(spx);
8602 sense->es_key = KEY_ILLEGAL_REQUEST;
8603 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8604 *pagelen = parmlen;
8605 *rval = TRAN_ACCEPT;
8606 return (SATA_FAILURE);
8607 }
8608
8609 *pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8610 sizeof (struct mode_page);
8611
8612 /*
8613 * We can enable and disable acoustice management and
8614 * set the acoustic management level.
8615 */
8616
8617 /*
8618 * Set-up Internal SET FEATURES command(s)
8619 */
8620 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8621 scmd->satacmd_addr_type = 0;
8622 scmd->satacmd_device_reg = 0;
8623 scmd->satacmd_status_reg = 0;
8624 scmd->satacmd_error_reg = 0;
8625 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
8626 if (page->acoustic_manag_enable) {
8627 scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC;
8628 scmd->satacmd_sec_count_lsb = page->acoustic_manag_level;
8629 } else { /* disabling acoustic management */
8630 scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC;
8631 }
8632
8633 /* Transfer command to HBA */
8634 if (sata_hba_start(spx, rval) != 0)
8635 /*
8636 * Pkt not accepted for execution.
8637 */
8638 return (SATA_FAILURE);
8639
8640 /* Now process return */
8641 if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) {
8642 sata_xlate_errors(spx);
8643 return (SATA_FAILURE);
8644 }
8645
8646 *dmod = 1;
8647
8648 return (SATA_SUCCESS);
8649 }
8650
8651 /*
8652 * Process mode select power condition page 0x1a
8653 *
8654 * This function has to be called with a port mutex held.
8655 *
8656 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8657 *
8658 * Cannot be called in the interrupt context.
8659 */
8660 int
sata_mode_select_page_1a(sata_pkt_txlate_t * spx,struct mode_info_power_cond * page,int parmlen,int * pagelen,int * rval,int * dmod)8661 sata_mode_select_page_1a(sata_pkt_txlate_t *spx, struct
8662 mode_info_power_cond *page, int parmlen, int *pagelen,
8663 int *rval, int *dmod)
8664 {
8665 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8666 sata_drive_info_t *sdinfo;
8667 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8668 sata_id_t *sata_id;
8669 struct scsi_extended_sense *sense;
8670 uint8_t ata_count;
8671 int i, len;
8672
8673 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8674 &spx->txlt_sata_pkt->satapkt_device);
8675 sata_id = &sdinfo->satadrv_id;
8676 *dmod = 0;
8677
8678 len = sizeof (struct mode_info_power_cond);
8679 len += sizeof (struct mode_page);
8680
8681 /* If parmlen is too short or the feature is not supported, drop it */
8682 if ((len < parmlen) || (page->idle == 1) ||
8683 (!(sata_id->ai_cap & SATA_STANDBYTIMER) && page->standby == 1)) {
8684 *scsipkt->pkt_scbp = STATUS_CHECK;
8685 sense = sata_arq_sense(spx);
8686 sense->es_key = KEY_ILLEGAL_REQUEST;
8687 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8688 *pagelen = parmlen;
8689 *rval = TRAN_ACCEPT;
8690 return (SATA_FAILURE);
8691 }
8692
8693 *pagelen = len;
8694
8695 /*
8696 * Set-up Internal STANDBY command(s)
8697 */
8698 if (page->standby == 0)
8699 goto out;
8700
8701 ata_count = sata_get_standby_timer(page->standby_cond_timer);
8702
8703 scmd->satacmd_addr_type = 0;
8704 scmd->satacmd_sec_count_lsb = ata_count;
8705 scmd->satacmd_lba_low_lsb = 0;
8706 scmd->satacmd_lba_mid_lsb = 0;
8707 scmd->satacmd_lba_high_lsb = 0;
8708 scmd->satacmd_features_reg = 0;
8709 scmd->satacmd_device_reg = 0;
8710 scmd->satacmd_status_reg = 0;
8711 scmd->satacmd_cmd_reg = SATAC_STANDBY;
8712 scmd->satacmd_flags.sata_special_regs = B_TRUE;
8713 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
8714
8715 /* Transfer command to HBA */
8716 if (sata_hba_start(spx, rval) != 0) {
8717 return (SATA_FAILURE);
8718 } else {
8719 if ((scmd->satacmd_error_reg != 0) ||
8720 (spx->txlt_sata_pkt->satapkt_reason !=
8721 SATA_PKT_COMPLETED)) {
8722 sata_xlate_errors(spx);
8723 return (SATA_FAILURE);
8724 }
8725 }
8726
8727 for (i = 0; i < 4; i++) {
8728 sdinfo->satadrv_standby_timer[i] = page->standby_cond_timer[i];
8729 }
8730 out:
8731 *dmod = 1;
8732 return (SATA_SUCCESS);
8733 }
8734
8735 /* Helper functions for manipulating struct log_parameter */
8736
8737 CTASSERT(sizeof (struct log_parameter) == 4);
8738
8739 static inline struct log_parameter *
log_param_next(struct log_parameter * lpp)8740 log_param_next(struct log_parameter *lpp)
8741 {
8742 uint8_t *ptr = (uint8_t *)lpp;
8743
8744 ptr += sizeof (*lpp) + lpp->param_len;
8745 return ((struct log_parameter *)ptr);
8746 }
8747
8748 static inline int
log_param_size(const struct log_parameter * last,const void * startp)8749 log_param_size(const struct log_parameter *last, const void *startp)
8750 {
8751 uintptr_t b = (uintptr_t)last;
8752 uintptr_t a = (uintptr_t)startp;
8753
8754 ASSERT3U(b, >=, a);
8755 return ((int)(b - a));
8756 }
8757
8758 /*
8759 * sata_build_lsense_page0() is used to create the
8760 * SCSI LOG SENSE page 0 (supported log pages)
8761 *
8762 * Currently supported pages are 0, 0x10, 0x2f, 0x30 and 0x0e
8763 * (supported log pages, self-test results, informational exceptions
8764 * Sun vendor specific ATA SMART data, and start stop cycle counter).
8765 *
8766 * Takes a sata_drive_info t * and the address of a buffer
8767 * in which to create the page information.
8768 *
8769 * Returns the number of bytes valid in the buffer.
8770 */
8771 static int
sata_build_lsense_page_0(sata_drive_info_t * sdinfo,uint8_t * buf)8772 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf)
8773 {
8774 uint8_t *ptr = buf;
8775 sata_id_t *sata_id = &sdinfo->satadrv_id;
8776
8777 /* The supported log pages should be in ascending order */
8778 *ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES;
8779
8780 if (sata_id->ai_cmdset84 & SATA_GPL_SUPPORTED) {
8781 *ptr++ = PAGE_CODE_READ_ERRORS;
8782 *ptr++ = PAGE_CODE_TEMPERATURE;
8783 }
8784
8785 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
8786 *ptr++ = PAGE_CODE_START_STOP_CYCLE_COUNTER;
8787 if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) {
8788 *ptr++ = PAGE_CODE_SELF_TEST_RESULTS;
8789 }
8790 }
8791
8792 if (sata_id->ai_medrotrate == 0x01 &&
8793 (sata_id->ai_cmdset84 & SATA_GPL_SUPPORTED))
8794 *ptr++ = PAGE_CODE_SOLID_STATE_MEDIA;
8795
8796 if (sata_id->ai_cmdset84 & SATA_GPL_SUPPORTED) {
8797 *ptr++ = PAGE_CODE_GENERAL_STATS;
8798 }
8799
8800 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
8801 *ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS;
8802 *ptr++ = PAGE_CODE_SMART_READ_DATA;
8803 }
8804
8805 return ((int)((uintptr_t)ptr - (uintptr_t)buf));
8806 }
8807
8808 static int
sata_build_lsense_page_03(sata_drive_info_t * sdinfo,uint8_t * buf,sata_hba_inst_t * sata_hba_inst)8809 sata_build_lsense_page_03(sata_drive_info_t *sdinfo, uint8_t *buf,
8810 sata_hba_inst_t *sata_hba_inst)
8811 {
8812 struct log_parameter *lpp = (struct log_parameter *)buf;
8813 uint64_t *lbuf;
8814 uint64_t param;
8815 int rval;
8816
8817 if (!(sdinfo->satadrv_id.ai_cmdset84 & SATA_GPL_SUPPORTED))
8818 return (-1);
8819
8820 lbuf = kmem_zalloc(512, KM_SLEEP);
8821 rval = sata_read_log_ext(sata_hba_inst, sdinfo, DEVICE_STATS_LOG,
8822 DEVSTAT_ROTATING_MEDIA_PAGE, lbuf, 1);
8823 if (rval == 0) {
8824 param = LE_64(lbuf[5]); /* Read recovery errors */
8825 if (SATA_STAT_SUPPORTED(param) && SATA_STAT_VALID(param)) {
8826 /* Total times corrected algorithm parameter */
8827 lpp->param_code[0] = 0x00;
8828 lpp->param_code[1] = 0x04;
8829 lpp->param_ctrl_flags = LOG_CTRL_LBIN;
8830 lpp->param_len = sizeof (uint32_t);
8831 BE_OUT32(&lpp->param_values[0],
8832 SATA_STAT_VALUE(param) & 0xffffffff);
8833
8834 lpp = log_param_next(lpp);
8835 }
8836 }
8837
8838 bzero(lbuf, 512);
8839 rval = sata_read_log_ext(sata_hba_inst, sdinfo, DEVICE_STATS_LOG,
8840 DEVSTAT_GENERAL_ERRORS_PAGE, lbuf, 1);
8841 if (rval == 0) {
8842 param = LE_64(lbuf[1]); /* Reported uncorrectable errors */
8843 if (SATA_STAT_SUPPORTED(param) && SATA_STAT_VALID(param)) {
8844 /* Total Uncorrected Errors parameter */
8845 lpp->param_code[0] = 0x00;
8846 lpp->param_code[1] = 0x06;
8847 lpp->param_ctrl_flags = LOG_CTRL_LBIN;
8848 lpp->param_len = sizeof (uint32_t);
8849 BE_OUT32(&lpp->param_values[0],
8850 SATA_STAT_VALUE(param) & 0xffffffff);
8851
8852 lpp = log_param_next(lpp);
8853 }
8854 }
8855
8856 kmem_free(lbuf, 512);
8857
8858 /*
8859 * If neither stat is supported, we treat it as the page not being
8860 * supported.
8861 */
8862 return (log_param_size(lpp, buf) > 0 ? log_param_size(lpp, buf) : -1);
8863 }
8864
8865 /*
8866 * sata_build_lsense_page_10() is used to create the
8867 * SCSI LOG SENSE page 0x10 (self-test results)
8868 *
8869 * Takes a sata_drive_info t * and the address of a buffer
8870 * in which to create the page information as well as a sata_hba_inst_t *.
8871 *
8872 * Returns the number of bytes valid in the buffer.
8873 *
8874 * Note: Self test and SMART data is accessible in device log pages.
8875 * The log pages can be accessed by SMART READ/WRITE LOG (up to 255 sectors
8876 * of data can be transferred by a single command), or by the General Purpose
8877 * Logging commands (GPL) READ LOG EXT and WRITE LOG EXT (up to 65,535 sectors
8878 * - approximately 33MB - can be transferred by a single command.
8879 * The SCT Command response (either error or command) is the same for both
8880 * the SMART and GPL methods of issuing commands.
8881 * This function uses READ LOG EXT command when drive supports LBA48, and
8882 * SMART READ command otherwise.
8883 *
8884 * Since above commands are executed in a synchronous mode, this function
8885 * should not be called in an interrupt context.
8886 */
8887 static int
sata_build_lsense_page_10(sata_drive_info_t * sdinfo,uint8_t * buf,sata_hba_inst_t * sata_hba_inst)8888 sata_build_lsense_page_10(
8889 sata_drive_info_t *sdinfo,
8890 uint8_t *buf,
8891 sata_hba_inst_t *sata_hba_inst)
8892 {
8893 struct log_parameter *lpp = (struct log_parameter *)buf;
8894 int rval;
8895
8896 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
8897 struct smart_ext_selftest_log *ext_selftest_log;
8898
8899 ext_selftest_log = kmem_zalloc(
8900 sizeof (struct smart_ext_selftest_log), KM_SLEEP);
8901
8902 rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo,
8903 ext_selftest_log, 0);
8904 if (rval == 0) {
8905 int index, start_index;
8906 struct smart_ext_selftest_log_entry *entry;
8907 static const struct smart_ext_selftest_log_entry empty =
8908 {0};
8909 uint16_t block_num;
8910 int count;
8911 boolean_t only_one_block = B_FALSE;
8912
8913 index = ext_selftest_log->
8914 smart_ext_selftest_log_index[0];
8915 index |= ext_selftest_log->
8916 smart_ext_selftest_log_index[1] << 8;
8917 if (index == 0)
8918 goto out;
8919
8920 --index; /* Correct for 0 origin */
8921 start_index = index; /* remember where we started */
8922 block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8923 if (block_num != 0) {
8924 rval = sata_ext_smart_selftest_read_log(
8925 sata_hba_inst, sdinfo, ext_selftest_log,
8926 block_num);
8927 if (rval != 0)
8928 goto out;
8929 }
8930 index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8931 entry =
8932 &ext_selftest_log->
8933 smart_ext_selftest_log_entries[index];
8934
8935 for (count = 1;
8936 count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
8937 ++count) {
8938 uint8_t status;
8939 uint8_t code;
8940 uint8_t sense_key;
8941 uint8_t add_sense_code;
8942 uint8_t add_sense_code_qual;
8943
8944 /* If this is an unused entry, we are done */
8945 if (bcmp(entry, &empty, sizeof (empty)) == 0) {
8946 /* Broken firmware on some disks */
8947 if (index + 1 ==
8948 ENTRIES_PER_EXT_SELFTEST_LOG_BLK) {
8949 --entry;
8950 --index;
8951 if (bcmp(entry, &empty,
8952 sizeof (empty)) == 0)
8953 goto out;
8954 } else
8955 goto out;
8956 }
8957
8958 if (only_one_block &&
8959 start_index == index)
8960 goto out;
8961
8962 lpp->param_code[0] = 0;
8963 lpp->param_code[1] = count;
8964 lpp->param_ctrl_flags =
8965 LOG_CTRL_LP | LOG_CTRL_LBIN;
8966 lpp->param_len =
8967 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
8968
8969 status = entry->smart_ext_selftest_log_status;
8970 status >>= 4;
8971 switch (status) {
8972 case 0:
8973 default:
8974 sense_key = KEY_NO_SENSE;
8975 add_sense_code =
8976 SD_SCSI_ASC_NO_ADD_SENSE;
8977 add_sense_code_qual = 0;
8978 break;
8979 case 1:
8980 sense_key = KEY_ABORTED_COMMAND;
8981 add_sense_code =
8982 DIAGNOSTIC_FAILURE_ON_COMPONENT;
8983 add_sense_code_qual = SCSI_COMPONENT_81;
8984 break;
8985 case 2:
8986 sense_key = KEY_ABORTED_COMMAND;
8987 add_sense_code =
8988 DIAGNOSTIC_FAILURE_ON_COMPONENT;
8989 add_sense_code_qual = SCSI_COMPONENT_82;
8990 break;
8991 case 3:
8992 sense_key = KEY_ABORTED_COMMAND;
8993 add_sense_code =
8994 DIAGNOSTIC_FAILURE_ON_COMPONENT;
8995 add_sense_code_qual = SCSI_COMPONENT_83;
8996 break;
8997 case 4:
8998 sense_key = KEY_HARDWARE_ERROR;
8999 add_sense_code =
9000 DIAGNOSTIC_FAILURE_ON_COMPONENT;
9001 add_sense_code_qual = SCSI_COMPONENT_84;
9002 break;
9003 case 5:
9004 sense_key = KEY_HARDWARE_ERROR;
9005 add_sense_code =
9006 DIAGNOSTIC_FAILURE_ON_COMPONENT;
9007 add_sense_code_qual = SCSI_COMPONENT_85;
9008 break;
9009 case 6:
9010 sense_key = KEY_HARDWARE_ERROR;
9011 add_sense_code =
9012 DIAGNOSTIC_FAILURE_ON_COMPONENT;
9013 add_sense_code_qual = SCSI_COMPONENT_86;
9014 break;
9015 case 7:
9016 sense_key = KEY_MEDIUM_ERROR;
9017 add_sense_code =
9018 DIAGNOSTIC_FAILURE_ON_COMPONENT;
9019 add_sense_code_qual = SCSI_COMPONENT_87;
9020 break;
9021 case 8:
9022 sense_key = KEY_HARDWARE_ERROR;
9023 add_sense_code =
9024 DIAGNOSTIC_FAILURE_ON_COMPONENT;
9025 add_sense_code_qual = SCSI_COMPONENT_88;
9026 break;
9027 }
9028 code = 0; /* unspecified */
9029 status |= (code << 4);
9030 lpp->param_values[0] = status;
9031 lpp->param_values[1] = 0; /* unspecified */
9032 lpp->param_values[2] = entry->
9033 smart_ext_selftest_log_timestamp[1];
9034 lpp->param_values[3] = entry->
9035 smart_ext_selftest_log_timestamp[0];
9036 if (status != 0) {
9037 lpp->param_values[4] = 0;
9038 lpp->param_values[5] = 0;
9039 lpp->param_values[6] = entry->
9040 smart_ext_selftest_log_failing_lba
9041 [5];
9042 lpp->param_values[7] = entry->
9043 smart_ext_selftest_log_failing_lba
9044 [4];
9045 lpp->param_values[8] = entry->
9046 smart_ext_selftest_log_failing_lba
9047 [3];
9048 lpp->param_values[9] = entry->
9049 smart_ext_selftest_log_failing_lba
9050 [2];
9051 lpp->param_values[10] = entry->
9052 smart_ext_selftest_log_failing_lba
9053 [1];
9054 lpp->param_values[11] = entry->
9055 smart_ext_selftest_log_failing_lba
9056 [0];
9057 } else { /* No bad block address */
9058 lpp->param_values[4] = 0xff;
9059 lpp->param_values[5] = 0xff;
9060 lpp->param_values[6] = 0xff;
9061 lpp->param_values[7] = 0xff;
9062 lpp->param_values[8] = 0xff;
9063 lpp->param_values[9] = 0xff;
9064 lpp->param_values[10] = 0xff;
9065 lpp->param_values[11] = 0xff;
9066 }
9067
9068 lpp->param_values[12] = sense_key;
9069 lpp->param_values[13] = add_sense_code;
9070 lpp->param_values[14] = add_sense_code_qual;
9071 lpp->param_values[15] = 0; /* undefined */
9072
9073 lpp = (struct log_parameter *)
9074 (((uint8_t *)lpp) +
9075 SCSI_LOG_PARAM_HDR_LEN +
9076 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
9077
9078 --index; /* Back up to previous entry */
9079 if (index < 0) {
9080 if (block_num > 0) {
9081 --block_num;
9082 } else {
9083 struct read_log_ext_directory
9084 logdir;
9085
9086 rval =
9087 sata_read_log_ext_directory(
9088 sata_hba_inst, sdinfo,
9089 &logdir);
9090 if (rval == -1)
9091 goto out;
9092 if ((logdir.read_log_ext_vers
9093 [0] == 0) &&
9094 (logdir.read_log_ext_vers
9095 [1] == 0))
9096 goto out;
9097 block_num =
9098 logdir.read_log_ext_nblks
9099 [EXT_SMART_SELFTEST_LOG_PAGE
9100 - 1][0];
9101 block_num |= logdir.
9102 read_log_ext_nblks
9103 [EXT_SMART_SELFTEST_LOG_PAGE
9104 - 1][1] << 8;
9105 --block_num;
9106 only_one_block =
9107 (block_num == 0);
9108 }
9109 rval = sata_ext_smart_selftest_read_log(
9110 sata_hba_inst, sdinfo,
9111 ext_selftest_log, block_num);
9112 if (rval != 0)
9113 goto out;
9114
9115 index =
9116 ENTRIES_PER_EXT_SELFTEST_LOG_BLK -
9117 1;
9118 }
9119 index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
9120 entry = &ext_selftest_log->
9121 smart_ext_selftest_log_entries[index];
9122 }
9123 }
9124 out:
9125 kmem_free(ext_selftest_log,
9126 sizeof (struct smart_ext_selftest_log));
9127 } else {
9128 struct smart_selftest_log *selftest_log;
9129
9130 selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log),
9131 KM_SLEEP);
9132
9133 rval = sata_smart_selftest_log(sata_hba_inst, sdinfo,
9134 selftest_log);
9135
9136 if (rval == 0) {
9137 int index;
9138 int count;
9139 struct smart_selftest_log_entry *entry;
9140 static const struct smart_selftest_log_entry empty =
9141 { 0 };
9142
9143 index = selftest_log->smart_selftest_log_index;
9144 if (index == 0)
9145 goto done;
9146 --index; /* Correct for 0 origin */
9147 entry = &selftest_log->
9148 smart_selftest_log_entries[index];
9149 for (count = 1;
9150 count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
9151 ++count) {
9152 uint8_t status;
9153 uint8_t code;
9154 uint8_t sense_key;
9155 uint8_t add_sense_code;
9156 uint8_t add_sense_code_qual = 0;
9157
9158 if (bcmp(entry, &empty, sizeof (empty)) == 0)
9159 goto done;
9160
9161 lpp->param_code[0] = 0;
9162 lpp->param_code[1] = count;
9163 lpp->param_ctrl_flags =
9164 LOG_CTRL_LP | LOG_CTRL_LBIN;
9165 lpp->param_len =
9166 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
9167
9168 status = entry->smart_selftest_log_status;
9169 status >>= 4;
9170 switch (status) {
9171 case 0:
9172 default:
9173 sense_key = KEY_NO_SENSE;
9174 add_sense_code =
9175 SD_SCSI_ASC_NO_ADD_SENSE;
9176 break;
9177 case 1:
9178 sense_key = KEY_ABORTED_COMMAND;
9179 add_sense_code =
9180 DIAGNOSTIC_FAILURE_ON_COMPONENT;
9181 add_sense_code_qual = SCSI_COMPONENT_81;
9182 break;
9183 case 2:
9184 sense_key = KEY_ABORTED_COMMAND;
9185 add_sense_code =
9186 DIAGNOSTIC_FAILURE_ON_COMPONENT;
9187 add_sense_code_qual = SCSI_COMPONENT_82;
9188 break;
9189 case 3:
9190 sense_key = KEY_ABORTED_COMMAND;
9191 add_sense_code =
9192 DIAGNOSTIC_FAILURE_ON_COMPONENT;
9193 add_sense_code_qual = SCSI_COMPONENT_83;
9194 break;
9195 case 4:
9196 sense_key = KEY_HARDWARE_ERROR;
9197 add_sense_code =
9198 DIAGNOSTIC_FAILURE_ON_COMPONENT;
9199 add_sense_code_qual = SCSI_COMPONENT_84;
9200 break;
9201 case 5:
9202 sense_key = KEY_HARDWARE_ERROR;
9203 add_sense_code =
9204 DIAGNOSTIC_FAILURE_ON_COMPONENT;
9205 add_sense_code_qual = SCSI_COMPONENT_85;
9206 break;
9207 case 6:
9208 sense_key = KEY_HARDWARE_ERROR;
9209 add_sense_code =
9210 DIAGNOSTIC_FAILURE_ON_COMPONENT;
9211 add_sense_code_qual = SCSI_COMPONENT_86;
9212 break;
9213 case 7:
9214 sense_key = KEY_MEDIUM_ERROR;
9215 add_sense_code =
9216 DIAGNOSTIC_FAILURE_ON_COMPONENT;
9217 add_sense_code_qual = SCSI_COMPONENT_87;
9218 break;
9219 case 8:
9220 sense_key = KEY_HARDWARE_ERROR;
9221 add_sense_code =
9222 DIAGNOSTIC_FAILURE_ON_COMPONENT;
9223 add_sense_code_qual = SCSI_COMPONENT_88;
9224 break;
9225 }
9226 code = 0; /* unspecified */
9227 status |= (code << 4);
9228 lpp->param_values[0] = status;
9229 lpp->param_values[1] = 0; /* unspecified */
9230 lpp->param_values[2] = entry->
9231 smart_selftest_log_timestamp[1];
9232 lpp->param_values[3] = entry->
9233 smart_selftest_log_timestamp[0];
9234 if (status != 0) {
9235 lpp->param_values[4] = 0;
9236 lpp->param_values[5] = 0;
9237 lpp->param_values[6] = 0;
9238 lpp->param_values[7] = 0;
9239 lpp->param_values[8] = entry->
9240 smart_selftest_log_failing_lba[3];
9241 lpp->param_values[9] = entry->
9242 smart_selftest_log_failing_lba[2];
9243 lpp->param_values[10] = entry->
9244 smart_selftest_log_failing_lba[1];
9245 lpp->param_values[11] = entry->
9246 smart_selftest_log_failing_lba[0];
9247 } else { /* No block address */
9248 lpp->param_values[4] = 0xff;
9249 lpp->param_values[5] = 0xff;
9250 lpp->param_values[6] = 0xff;
9251 lpp->param_values[7] = 0xff;
9252 lpp->param_values[8] = 0xff;
9253 lpp->param_values[9] = 0xff;
9254 lpp->param_values[10] = 0xff;
9255 lpp->param_values[11] = 0xff;
9256 }
9257 lpp->param_values[12] = sense_key;
9258 lpp->param_values[13] = add_sense_code;
9259 lpp->param_values[14] = add_sense_code_qual;
9260 lpp->param_values[15] = 0; /* undefined */
9261
9262 lpp = (struct log_parameter *)
9263 (((uint8_t *)lpp) +
9264 SCSI_LOG_PARAM_HDR_LEN +
9265 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
9266 --index; /* back up to previous entry */
9267 if (index < 0) {
9268 index =
9269 NUM_SMART_SELFTEST_LOG_ENTRIES - 1;
9270 }
9271 entry = &selftest_log->
9272 smart_selftest_log_entries[index];
9273 }
9274 }
9275 done:
9276 kmem_free(selftest_log, sizeof (struct smart_selftest_log));
9277 }
9278
9279 return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) *
9280 SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS);
9281 }
9282
9283 static uint8_t
sata_sct_temp(sata_hba_inst_t * sata_hba_inst,sata_drive_info_t * sdinfo,void * p,size_t lbufsz)9284 sata_sct_temp(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
9285 void *p, size_t lbufsz)
9286 {
9287 sata_id_t *sata_id = &sdinfo->satadrv_id;
9288 uint8_t *lbuf = p;
9289 int rval;
9290 uint8_t temp;
9291
9292 /* The log buffer we use should be at least 1 block in size */
9293 ASSERT3U(lbufsz, >=, 512);
9294
9295 if ((sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP) == 0)
9296 return (SCSI_NO_TEMP);
9297
9298 bzero(lbuf, lbufsz);
9299 rval = sata_smart_read_log(sata_hba_inst, sdinfo, lbuf,
9300 SCT_STATUS_LOG_PAGE, 1);
9301 if (rval == -1)
9302 return (SCSI_NO_TEMP);
9303
9304 /*
9305 * ACS-3 8.2.5 Table 186 -- If the value is 0x80, the field (HDA TEMP)
9306 * is not valid)
9307 */
9308 temp = lbuf[200];
9309 if (temp == 0x80)
9310 return (SCSI_NO_TEMP);
9311
9312 /*
9313 * SATA temps are signed (with 0x80 being a sentinel value indicating
9314 * not valid as noted above). SAT-5 says that values below 0 are
9315 * truncated to 0.
9316 */
9317 if ((temp & 0x80) != 0)
9318 return (0);
9319
9320 return (temp);
9321 }
9322
9323
9324 /*
9325 * sata_build_lsense_page_2f() is used to create the
9326 * SCSI LOG SENSE page 0x2f (informational exceptions)
9327 *
9328 * Takes a sata_drive_info t * and the address of a buffer
9329 * in which to create the page information as well as a sata_hba_inst_t *.
9330 *
9331 * Returns the number of bytes valid in the buffer.
9332 *
9333 * Because it invokes function(s) that send synchronously executed command
9334 * to the HBA, it cannot be called in the interrupt context.
9335 */
9336 static int
sata_build_lsense_page_2f(sata_drive_info_t * sdinfo,uint8_t * buf,sata_hba_inst_t * sata_hba_inst)9337 sata_build_lsense_page_2f(
9338 sata_drive_info_t *sdinfo,
9339 uint8_t *buf,
9340 sata_hba_inst_t *sata_hba_inst)
9341 {
9342 struct log_parameter *lpp = (struct log_parameter *)buf;
9343 int rval;
9344 uint8_t *smart_data;
9345 uint8_t temp;
9346 sata_id_t *sata_id;
9347
9348 lpp->param_code[0] = 0;
9349 lpp->param_code[1] = 0;
9350 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
9351
9352 /* Now get the SMART status w.r.t. threshold exceeded */
9353 rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo);
9354 switch (rval) {
9355 case 1:
9356 lpp->param_values[0] = SCSI_PREDICTED_FAILURE;
9357 lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE;
9358 break;
9359 case 0:
9360 case -1: /* failed to get data */
9361 lpp->param_values[0] = 0; /* No failure predicted */
9362 lpp->param_values[1] = 0;
9363 break;
9364 #if defined(SATA_DEBUG)
9365 default:
9366 cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value");
9367 /* NOTREACHED */
9368 #endif
9369 }
9370
9371 sata_id = &sdinfo->satadrv_id;
9372 if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP)) {
9373 temp = SCSI_NO_TEMP;
9374 } else {
9375 /* Now get the temperature */
9376 smart_data = kmem_zalloc(512, KM_SLEEP);
9377 temp = sata_sct_temp(sata_hba_inst, sdinfo, smart_data, 512);
9378 kmem_free(smart_data, 512);
9379 }
9380
9381 lpp->param_values[2] = temp; /* most recent temperature */
9382 lpp->param_values[3] = 0; /* required vendor specific byte */
9383
9384 lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN;
9385
9386
9387 return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN);
9388 }
9389
9390 static int
sata_build_lsense_page_0d(sata_drive_info_t * sdinfo,uint8_t * buf,sata_hba_inst_t * sata_hba_inst)9391 sata_build_lsense_page_0d(sata_drive_info_t *sdinfo, uint8_t *buf,
9392 sata_hba_inst_t *sata_hba_inst)
9393 {
9394 struct log_parameter *lpp = (struct log_parameter *)buf;
9395 uint64_t *lbuf;
9396 uint64_t param;
9397 int rval;
9398 uint8_t temp, ref_temp, sct_temp;
9399
9400 if (!(sdinfo->satadrv_id.ai_sctsupport & SATA_SCT_CMD_TRANS_SUP) &&
9401 !(sdinfo->satadrv_id.ai_cmdset84 & SATA_GPL_SUPPORTED))
9402 return (-1);
9403
9404 temp = ref_temp = sct_temp = SCSI_NO_TEMP;
9405
9406 lbuf = kmem_zalloc(512, KM_SLEEP);
9407 sct_temp = sata_sct_temp(sata_hba_inst, sdinfo, lbuf, 512);
9408
9409 bzero(lbuf, 512);
9410
9411 rval = sata_read_log_ext(sata_hba_inst, sdinfo, DEVICE_STATS_LOG,
9412 DEVSTAT_TEMP_PAGE, lbuf, 1);
9413 if (rval == -1)
9414 goto done;
9415
9416 param = LE_64(lbuf[1]); /* Current temperature */
9417 if (SATA_STAT_SUPPORTED(param) && SATA_STAT_VALID(param)) {
9418 /*
9419 * SAT-5 10.3.13.2 Table 136 says that only positive
9420 * temperatures (SATA temps are signed 8-bit values) -- i.e.
9421 * bit 7 is 0 are translated, otherwise 0xff (SCSI_NO_TEMP)
9422 * is returned.
9423 */
9424 temp = SATA_STAT_VALUE(param) & 0xff;
9425 if ((temp & 0x80) != 0)
9426 temp = SCSI_NO_TEMP;
9427 }
9428
9429 param = LE_64(lbuf[11]); /* Max operating temp */
9430 if (SATA_STAT_SUPPORTED(param) && SATA_STAT_VALID(param)) {
9431 /*
9432 * Interestingly, for the reference temperature, while the
9433 * SATA value is also an 8-bit signed value), SAT-5 10.3.13.3
9434 * Table 137 says that negative temps are translated to 0
9435 * unlike the current temperature.
9436 */
9437 int8_t val = (int8_t)(SATA_STAT_VALUE(param) & 0xff);
9438 ref_temp = (val < 0) ? 0 : val;
9439 }
9440
9441 rval = 0;
9442
9443 done:
9444 kmem_free(lbuf, 512);
9445
9446 /*
9447 * If we support SCT or GPL, we'll always return a value, even if
9448 * that value is SCSI_NO_TEMP (as it may be a transient issue and
9449 * appears to be allowable per SPC-5).
9450 */
9451
9452 lpp->param_code[0] = 0;
9453 lpp->param_code[1] = 0;
9454 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
9455 lpp->param_len = 2;
9456 lpp->param_values[0] = 0; /* Reserved */
9457
9458 /*
9459 * Per SAT-5 10.3.13.2 Table 136, The SCT temp is used if
9460 * valid, otherwise the current temp from the temp statistics page
9461 * is used.
9462 */
9463 lpp->param_values[1] = (sct_temp != SCSI_NO_TEMP) ? sct_temp : temp;
9464
9465 lpp = log_param_next(lpp);
9466
9467 if (ref_temp != SCSI_NO_TEMP) {
9468 lpp->param_code[0] = 0x00;
9469 lpp->param_code[1] = 0x01; /* Reference Temperature */
9470 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
9471 lpp->param_len = 2;
9472 lpp->param_values[0] = 0; /* Resreved */
9473 lpp->param_values[1] = ref_temp;
9474
9475 lpp = log_param_next(lpp);
9476 }
9477
9478 return (log_param_size(lpp, buf));
9479 }
9480
9481 /*
9482 * sata_build_lsense_page_30() is used to create the
9483 * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data).
9484 *
9485 * Takes a sata_drive_info t * and the address of a buffer
9486 * in which to create the page information as well as a sata_hba_inst_t *.
9487 *
9488 * Returns the number of bytes valid in the buffer.
9489 */
9490 static int
sata_build_lsense_page_30(sata_drive_info_t * sdinfo,uint8_t * buf,sata_hba_inst_t * sata_hba_inst)9491 sata_build_lsense_page_30(
9492 sata_drive_info_t *sdinfo,
9493 uint8_t *buf,
9494 sata_hba_inst_t *sata_hba_inst)
9495 {
9496 struct smart_data *smart_data = (struct smart_data *)buf;
9497 int rval;
9498
9499 /* Now do the SMART READ DATA */
9500 rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data);
9501 if (rval == -1)
9502 return (0);
9503
9504 return (sizeof (struct smart_data));
9505 }
9506
9507 /*
9508 * sata_build_lsense_page_0e() is used to create the
9509 * SCSI LOG SENSE page 0e (start-stop cycle counter page)
9510 *
9511 * Date of Manufacture (0x0001)
9512 * YEAR = "0000"
9513 * WEEK = "00"
9514 * Accounting Date (0x0002)
9515 * 6 ASCII space character(20h)
9516 * Specified cycle count over device lifetime
9517 * VALUE - THRESH - the delta between max and min;
9518 * Accumulated start-stop cycles
9519 * VALUE - WORST - the accumulated cycles;
9520 *
9521 * ID FLAG THRESH VALUE WORST RAW on start/stop counter attribute
9522 *
9523 * Takes a sata_drive_info t * and the address of a buffer
9524 * in which to create the page information as well as a sata_hba_inst_t *.
9525 *
9526 * Returns the number of bytes valid in the buffer.
9527 */
9528 static int
sata_build_lsense_page_0e(sata_drive_info_t * sdinfo,uint8_t * buf,sata_pkt_txlate_t * spx)9529 sata_build_lsense_page_0e(sata_drive_info_t *sdinfo, uint8_t *buf,
9530 sata_pkt_txlate_t *spx)
9531 {
9532 struct start_stop_cycle_counter_log *log_page;
9533 int i, rval, index;
9534 uint8_t smart_data[512], id, value, worst, thresh;
9535 uint32_t max_count, cycles;
9536
9537 /* Now do the SMART READ DATA */
9538 rval = sata_fetch_smart_data(spx->txlt_sata_hba_inst, sdinfo,
9539 (struct smart_data *)smart_data);
9540 if (rval == -1)
9541 return (0);
9542 for (i = 0, id = 0; i < SMART_START_STOP_COUNT_ID * 2; i++) {
9543 index = (i * 12) + 2;
9544 id = smart_data[index];
9545 if (id != SMART_START_STOP_COUNT_ID)
9546 continue;
9547 else {
9548 thresh = smart_data[index + 2];
9549 value = smart_data[index + 3];
9550 worst = smart_data[index + 4];
9551 break;
9552 }
9553 }
9554 if (id != SMART_START_STOP_COUNT_ID)
9555 return (0);
9556 max_count = value - thresh;
9557 cycles = value - worst;
9558
9559 log_page = (struct start_stop_cycle_counter_log *)buf;
9560 bzero(log_page, sizeof (struct start_stop_cycle_counter_log));
9561 log_page->code = 0x0e;
9562 log_page->page_len_low = 0x24;
9563
9564 log_page->manufactor_date_low = 0x1;
9565 log_page->param_1.fmt_link = 0x1; /* 01b */
9566 log_page->param_len_1 = 0x06;
9567 for (i = 0; i < 4; i++) {
9568 log_page->year_manu[i] = 0x30;
9569 if (i < 2)
9570 log_page->week_manu[i] = 0x30;
9571 }
9572
9573 log_page->account_date_low = 0x02;
9574 log_page->param_2.fmt_link = 0x01; /* 01b */
9575 log_page->param_len_2 = 0x06;
9576 for (i = 0; i < 4; i++) {
9577 log_page->year_account[i] = 0x20;
9578 if (i < 2)
9579 log_page->week_account[i] = 0x20;
9580 }
9581
9582 log_page->lifetime_code_low = 0x03;
9583 log_page->param_3.fmt_link = 0x03; /* 11b */
9584 log_page->param_len_3 = 0x04;
9585 /* VALUE - THRESH - the delta between max and min */
9586 log_page->cycle_code_low = 0x04;
9587 log_page->param_4.fmt_link = 0x03; /* 11b */
9588 log_page->param_len_4 = 0x04;
9589 /* WORST - THRESH - the distance from 'now' to min */
9590
9591 for (i = 0; i < 4; i++) {
9592 log_page->cycle_lifetime[i] =
9593 (max_count >> (8 * (3 - i))) & 0xff;
9594 log_page->cycle_accumulated[i] =
9595 (cycles >> (8 * (3 - i))) & 0xff;
9596 }
9597
9598 return (sizeof (struct start_stop_cycle_counter_log));
9599 }
9600
9601 static int
sata_build_lsense_page_11(sata_drive_info_t * sdinfo,uint8_t * buf,sata_hba_inst_t * sata_hba_inst)9602 sata_build_lsense_page_11(sata_drive_info_t *sdinfo, uint8_t *buf,
9603 sata_hba_inst_t *sata_hba_inst)
9604 {
9605 struct log_parameter *lpp = (struct log_parameter *)buf;
9606 uint64_t *lbuf;
9607 uint64_t param;
9608 int rval = 0;
9609
9610 /* Check if device is SSD */
9611 if (sdinfo->satadrv_id.ai_medrotrate != 0x01 ||
9612 !(sdinfo->satadrv_id.ai_cmdset84 & SATA_GPL_SUPPORTED)) {
9613 return (-1);
9614 }
9615
9616 lbuf = kmem_zalloc(512, KM_SLEEP);
9617 rval = sata_read_log_ext(sata_hba_inst, sdinfo, DEVICE_STATS_LOG,
9618 DEVSTAT_SSD_PAGE, lbuf, 1);
9619 if (rval == -1)
9620 goto done;
9621
9622 param = LE_64(lbuf[1]); /* %-age used endurance indicator */
9623 if (!SATA_STAT_SUPPORTED(param) || !SATA_STAT_VALID(param)) {
9624 /*
9625 * If the wear stat isn't supported or valid, the SAT-5
9626 * says this is unspecified. We'll treat it as the
9627 * log page being unsupported.
9628 */
9629 rval = -1;
9630 goto done;
9631 }
9632
9633 lpp->param_code[0] = 0x00;
9634 lpp->param_code[1] = 0x01;
9635 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
9636 lpp->param_len = 4;
9637 BE_OUT32(&lpp->param_values[0], SATA_STAT_VALUE(param) & 0xffffffff);
9638
9639 lpp = log_param_next(lpp);
9640
9641 done:
9642 kmem_free(lbuf, 512);
9643 return ((rval < 0) ? -1 : log_param_size(lpp, buf));
9644 }
9645
9646 static int
sata_build_lsense_page_19(sata_drive_info_t * sdinfo,uint8_t * buf,sata_hba_inst_t * sata_hba_inst)9647 sata_build_lsense_page_19(sata_drive_info_t *sdinfo, uint8_t *buf,
9648 sata_hba_inst_t *sata_hba_inst)
9649 {
9650 /*
9651 * The indexes into lbuf (the SATA general statistics log)
9652 * that correspond to the values of the general access statistics
9653 * and performance log values. -1 means there is no mapping (e.g.
9654 * write 0 for that value).
9655 */
9656 static const int stat_idx[] = {
9657 6, /* # of read commands */
9658 4, /* # of write commands */
9659 3, /* Logical sectors written */
9660 5, /* Logical sectors read */
9661 -1, -1, -1, -1
9662 };
9663
9664 struct log_parameter *lpp = (struct log_parameter *)buf;
9665 uint64_t *lbuf;
9666 uint64_t *paramp;
9667 uint64_t param;
9668 uint_t nvalid;
9669 int rval;
9670
9671 if (!(sdinfo->satadrv_id.ai_cmdset84 & SATA_GPL_SUPPORTED))
9672 return (-1);
9673
9674 nvalid = 0;
9675
9676 lbuf = kmem_zalloc(512, KM_SLEEP);
9677 rval = sata_read_log_ext(sata_hba_inst, sdinfo, DEVICE_STATS_LOG,
9678 DEVSTAT_GENERAL_STATS, lbuf, 1);
9679 if (rval == -1) {
9680 kmem_free(lbuf, 512);
9681 return (-1);
9682 }
9683
9684 lpp->param_code[0] = 0x00;
9685 lpp->param_code[1] = 0x01;
9686 /*
9687 * SPC-5 and SAT-5 disagree on this value -- SPC-5 7.3.9.2 says this
9688 * should be an unbounded data counter (10b LOG_CTRL_LBIN) while SAT-5
9689 * 10.3.4.2 Table 110 says this should be a binary format list (11b
9690 * aka LOG_CTRL_LP | LOG_CTRL_LBIN). Since SAT-5 is a bit more
9691 * explicit on the value, we've followed it. So far no software
9692 * has been uncovered to date that seems to care about the value, but
9693 * it may need to be updated of the two specs are ever brought into
9694 * agreement.
9695 */
9696 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
9697 lpp->param_len = 0x40;
9698
9699 paramp = (uint64_t *)&lpp->param_values[0];
9700
9701 /* Zero out all of parameter values */
9702 bzero(paramp, 0x40);
9703
9704 /* The stat parameters are 48 bits long */
9705 #define PARAM_VAL(x) ((x) & ((1ULL << 48) - 1))
9706
9707 for (uint_t i = 0; i < ARRAY_SIZE(stat_idx); i++, paramp++) {
9708 if (stat_idx[i] == -1) {
9709 continue;
9710 }
9711
9712 param = LE_64(lbuf[stat_idx[i]]);
9713
9714 if (SATA_STAT_SUPPORTED(param) && SATA_STAT_VALID(param)) {
9715 BE_OUT64(paramp, PARAM_VAL(param));
9716 nvalid++;
9717 }
9718 }
9719 #undef PARAM_VAL
9720
9721 kmem_free(lbuf, 512);
9722
9723 /* We must return at least one valid value for this page */
9724 if (nvalid == 0)
9725 return (-1);
9726
9727 /*
9728 * SPC-5 says that the IDLE TIME and TIME INTERVAL parameters
9729 * are mandatory, but SAT-5 gives no mention of either parameter.
9730 * Some utilities (e.g. sg3_utils) strictly follow the guidance of
9731 * SPC-5 and expect all three parameters, so we generate dummy
9732 * values for the IDLE TIME and TIME INTERVAL parameters.
9733 */
9734 lpp = log_param_next(lpp);
9735
9736 /* IDLE TIME */
9737 lpp->param_code[0] = 0x00;
9738 lpp->param_code[1] = 0x02;
9739 lpp->param_ctrl_flags = LOG_CTRL_LP;
9740 lpp->param_len = 0x08;
9741
9742 /*
9743 * The value is an 64-bit unsigned int, the address is almost
9744 * certainly going to be unaligned, so just set each byte
9745 * individually.
9746 */
9747 lpp->param_values[0] = lpp->param_values[1] = lpp->param_values[2] =
9748 lpp->param_values[3] = lpp->param_values[4] =
9749 lpp->param_values[5] = lpp->param_values[6] =
9750 lpp->param_values[7] = 0;
9751 lpp = log_param_next(lpp);
9752
9753 /* TIME INTERVAL */
9754 lpp->param_code[0] = 0x00;
9755 lpp->param_code[1] = 0x03;
9756 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
9757 lpp->param_len = 0x08;
9758
9759 uint32_t *vp = (uint32_t *)&lpp->param_values;
9760
9761 /*
9762 * SPC-5 7.3.6.7 -- The TIME INTERVAL parameter consists of
9763 * two 32-bit unsigned ints -- EXPONENT and INTEGER.
9764 * EXPONENT is the _negative_ power of ten (e.g. '3' implies
9765 * 10^-3) and INTEGER is the mantissa (e.g. the actual value
9766 * is INTEGER * 10^(-EXPONENT)).
9767 *
9768 * SPC-5 isn't completely clear on this, but from the description
9769 * of the fields of the General Access Statistics and Performance
9770 * log parameter in section 7.3.9.2, it implies that the TIME INTERVAL
9771 * parameter is used to in conjunction with the {READ,WRITE} COMMAND
9772 * PROCESSING INTERVAL statistics value. Since these values do not
9773 * have a translation defined (there doesn't appear to be any
9774 * equivalent statistic in any SATA log page), we always return
9775 * 0 for these stats. As a TIME INTERVAL of 0^-0 would be nonsensical
9776 * (and mathematically undefined), we choose an arbitrary interval of
9777 * 1ms (1 * 10^-3).
9778 */
9779 BE_OUT32(vp, 3);
9780 vp++;
9781 BE_OUT32(vp, 1);
9782
9783 lpp = log_param_next(lpp);
9784
9785 return (log_param_size(lpp, buf));
9786 }
9787
9788
9789 /*
9790 * This function was used for build a ATA read verify sector command
9791 */
9792 static void
sata_build_read_verify_cmd(sata_cmd_t * scmd,uint16_t sec,uint64_t lba)9793 sata_build_read_verify_cmd(sata_cmd_t *scmd, uint16_t sec, uint64_t lba)
9794 {
9795 scmd->satacmd_cmd_reg = SATAC_RDVER;
9796 scmd->satacmd_addr_type = ATA_ADDR_LBA28;
9797 scmd->satacmd_flags.sata_special_regs = B_TRUE;
9798
9799 scmd->satacmd_sec_count_lsb = sec & 0xff;
9800 scmd->satacmd_lba_low_lsb = lba & 0xff;
9801 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
9802 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
9803 scmd->satacmd_device_reg = (SATA_ADH_LBA | ((lba >> 24) & 0xf));
9804 scmd->satacmd_features_reg = 0;
9805 scmd->satacmd_status_reg = 0;
9806 scmd->satacmd_error_reg = 0;
9807 }
9808
9809 /*
9810 * This function was used for building an ATA
9811 * command, and only command register need to
9812 * be defined, other register will be zero or na.
9813 */
9814 static void
sata_build_generic_cmd(sata_cmd_t * scmd,uint8_t cmd)9815 sata_build_generic_cmd(sata_cmd_t *scmd, uint8_t cmd)
9816 {
9817 scmd->satacmd_addr_type = 0;
9818 scmd->satacmd_cmd_reg = cmd;
9819 scmd->satacmd_device_reg = 0;
9820 scmd->satacmd_sec_count_lsb = 0;
9821 scmd->satacmd_lba_low_lsb = 0;
9822 scmd->satacmd_lba_mid_lsb = 0;
9823 scmd->satacmd_lba_high_lsb = 0;
9824 scmd->satacmd_features_reg = 0;
9825 scmd->satacmd_status_reg = 0;
9826 scmd->satacmd_error_reg = 0;
9827 scmd->satacmd_flags.sata_special_regs = B_TRUE;
9828 }
9829
9830 /*
9831 * This function was used for changing the standby
9832 * timer format from SCSI to ATA.
9833 */
9834 static uint8_t
sata_get_standby_timer(uint8_t * timer)9835 sata_get_standby_timer(uint8_t *timer)
9836 {
9837 uint32_t i = 0, count = 0;
9838 uint8_t ata_count;
9839
9840 for (i = 0; i < 4; i++) {
9841 count = count << 8 | timer[i];
9842 }
9843
9844 if (count == 0)
9845 return (0);
9846
9847 if (count >= 1 && count <= 12000)
9848 ata_count = (count -1) / 50 + 1;
9849 else if (count > 12000 && count <= 12600)
9850 ata_count = 0xfc;
9851 else if (count > 12601 && count <= 12750)
9852 ata_count = 0xff;
9853 else if (count > 12750 && count <= 17999)
9854 ata_count = 0xf1;
9855 else if (count > 18000 && count <= 198000)
9856 ata_count = count / 18000 + 240;
9857 else
9858 ata_count = 0xfd;
9859 return (ata_count);
9860 }
9861
9862 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */
9863
9864 /*
9865 * Start command for ATAPI device.
9866 * This function processes scsi_pkt requests.
9867 * Now CD/DVD, tape and ATAPI disk devices are supported.
9868 * Most commands are packet without any translation into Packet Command.
9869 * Some may be trapped and executed as SATA commands (not clear which one).
9870 *
9871 * Returns TRAN_ACCEPT if command is accepted for execution (or completed
9872 * execution).
9873 * Returns other TRAN_XXXX codes if command is not accepted or completed
9874 * (see return values for sata_hba_start()).
9875 *
9876 * Note:
9877 * Inquiry cdb format differs between transport version 2 and 3.
9878 * However, the transport version 3 devices that were checked did not adhere
9879 * to the specification (ignored MSB of the allocation length). Therefore,
9880 * the transport version is not checked, but Inquiry allocation length is
9881 * truncated to 255 bytes if the original allocation length set-up by the
9882 * target driver is greater than 255 bytes.
9883 */
9884 static int
sata_txlt_atapi(sata_pkt_txlate_t * spx)9885 sata_txlt_atapi(sata_pkt_txlate_t *spx)
9886 {
9887 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
9888 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
9889 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9890 sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx);
9891 sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba,
9892 &spx->txlt_sata_pkt->satapkt_device);
9893 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
9894 int cdblen;
9895 int rval, reason;
9896 int synch;
9897 union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp;
9898
9899 mutex_enter(cport_mutex);
9900
9901 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
9902 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
9903 mutex_exit(cport_mutex);
9904 return (rval);
9905 }
9906
9907 /*
9908 * ATAPI device executes some ATA commands in addition to those
9909 * commands sent via PACKET command. These ATA commands may be
9910 * executed by the regular SATA translation functions. None needs
9911 * to be captured now.
9912 *
9913 * Commands sent via PACKET command include:
9914 * MMC command set for ATAPI CD/DVD device
9915 * SSC command set for ATAPI TAPE device
9916 * SBC command set for ATAPI disk device
9917 *
9918 */
9919
9920 /* Check the size of cdb */
9921
9922 switch (GETGROUP(cdbp)) {
9923 case CDB_GROUPID_3: /* Reserved, per SPC-4 */
9924 /*
9925 * opcodes 0x7e and 0x7f identify variable-length CDBs and
9926 * therefore require special handling. Return failure, for now.
9927 */
9928 mutex_exit(cport_mutex);
9929 return (TRAN_BADPKT);
9930
9931 case CDB_GROUPID_6: /* Vendor-specific, per SPC-4 */
9932 case CDB_GROUPID_7: /* Vendor-specific, per SPC-4 */
9933 /* obtain length from the scsi_pkt */
9934 cdblen = scsipkt->pkt_cdblen;
9935 break;
9936
9937 default:
9938 /* CDB's length is statically known, per SPC-4 */
9939 cdblen = scsi_cdb_size[GETGROUP(cdbp)];
9940 break;
9941 }
9942
9943 if (cdblen <= 0 || cdblen > sdinfo->satadrv_atapi_cdb_len) {
9944 sata_log(NULL, CE_WARN,
9945 "sata: invalid ATAPI cdb length %d",
9946 cdblen);
9947 mutex_exit(cport_mutex);
9948 return (TRAN_BADPKT);
9949 }
9950
9951 SATAATAPITRACE(spx, cdblen);
9952
9953 /*
9954 * For non-read/write commands we need to
9955 * map buffer
9956 */
9957 switch ((uint_t)scsipkt->pkt_cdbp[0]) {
9958 case SCMD_READ:
9959 case SCMD_READ_G1:
9960 case SCMD_READ_G5:
9961 case SCMD_READ_G4:
9962 case SCMD_WRITE:
9963 case SCMD_WRITE_G1:
9964 case SCMD_WRITE_G5:
9965 case SCMD_WRITE_G4:
9966 break;
9967 default:
9968 if (bp != NULL) {
9969 if (bp->b_flags & (B_PHYS | B_PAGEIO))
9970 bp_mapin(bp);
9971 }
9972 break;
9973 }
9974 /*
9975 * scmd->satacmd_flags.sata_data_direction default -
9976 * SATA_DIR_NODATA_XFER - is set by
9977 * sata_txlt_generic_pkt_info().
9978 */
9979 if (scmd->satacmd_bp) {
9980 if (scmd->satacmd_bp->b_flags & B_READ) {
9981 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9982 } else {
9983 scmd->satacmd_flags.sata_data_direction =
9984 SATA_DIR_WRITE;
9985 }
9986 }
9987
9988 /*
9989 * Set up ATAPI packet command.
9990 */
9991
9992 sata_atapi_packet_cmd_setup(scmd, sdinfo);
9993
9994 /* Copy cdb into sata_cmd */
9995 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9996 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
9997 bcopy(cdbp, scmd->satacmd_acdb, cdblen);
9998
9999 /* See note in the command header */
10000 if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) {
10001 if (scmd->satacmd_acdb[3] != 0)
10002 scmd->satacmd_acdb[4] = 255;
10003 }
10004
10005 #ifdef SATA_DEBUG
10006 if (sata_debug_flags & SATA_DBG_ATAPI) {
10007 uint8_t *p = scmd->satacmd_acdb;
10008 char buf[3 * SATA_ATAPI_MAX_CDB_LEN];
10009
10010 (void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN,
10011 "%02x %02x %02x %02x %02x %02x %02x %02x "
10012 "%2x %02x %02x %02x %02x %02x %02x %02x",
10013 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
10014 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
10015 buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0';
10016 cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf);
10017 }
10018 #endif
10019
10020 /*
10021 * Preset request sense data to NO SENSE.
10022 * If there is no way to get error information via Request Sense,
10023 * the packet request sense data would not have to be modified by HBA,
10024 * but it could be returned as is.
10025 */
10026 bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
10027 sata_fixed_sense_data_preset(
10028 (struct scsi_extended_sense *)scmd->satacmd_rqsense);
10029
10030 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
10031 /* Need callback function */
10032 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion;
10033 synch = FALSE;
10034 } else
10035 synch = TRUE;
10036
10037 /* Transfer command to HBA */
10038 if (sata_hba_start(spx, &rval) != 0) {
10039 /* Pkt not accepted for execution */
10040 mutex_exit(cport_mutex);
10041 return (rval);
10042 }
10043 mutex_exit(cport_mutex);
10044 /*
10045 * If execution is non-synchronous,
10046 * a callback function will handle potential errors, translate
10047 * the response and will do a callback to a target driver.
10048 * If it was synchronous, use the same framework callback to check
10049 * an execution status.
10050 */
10051 if (synch) {
10052 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
10053 "synchronous execution status %x\n",
10054 spx->txlt_sata_pkt->satapkt_reason);
10055 sata_txlt_atapi_completion(spx->txlt_sata_pkt);
10056 }
10057 return (TRAN_ACCEPT);
10058 }
10059
10060
10061 /*
10062 * ATAPI Packet command completion.
10063 *
10064 * Failure of the command passed via Packet command are considered device
10065 * error. SATA HBA driver would have to retrieve error data (via Request
10066 * Sense command delivered via error retrieval sata packet) and copy it
10067 * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data.
10068 */
10069 static void
sata_txlt_atapi_completion(sata_pkt_t * sata_pkt)10070 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt)
10071 {
10072 sata_pkt_txlate_t *spx =
10073 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
10074 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
10075 struct scsi_extended_sense *sense;
10076 struct buf *bp;
10077 int rval;
10078
10079 #ifdef SATA_DEBUG
10080 uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense;
10081 #endif
10082
10083 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
10084 STATE_SENT_CMD | STATE_GOT_STATUS;
10085
10086 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
10087 /* Normal completion */
10088 if (sata_pkt->satapkt_cmd.satacmd_bp != NULL)
10089 scsipkt->pkt_state |= STATE_XFERRED_DATA;
10090 scsipkt->pkt_reason = CMD_CMPLT;
10091 *scsipkt->pkt_scbp = STATUS_GOOD;
10092 if (spx->txlt_tmp_buf != NULL) {
10093 /* Temporary buffer was used */
10094 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
10095 if (bp->b_flags & B_READ) {
10096 rval = ddi_dma_sync(
10097 spx->txlt_buf_dma_handle, 0, 0,
10098 DDI_DMA_SYNC_FORCPU);
10099 ASSERT(rval == DDI_SUCCESS);
10100 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
10101 bp->b_bcount);
10102 }
10103 }
10104 } else {
10105 /*
10106 * Something went wrong - analyze return
10107 */
10108 *scsipkt->pkt_scbp = STATUS_CHECK;
10109 sense = sata_arq_sense(spx);
10110
10111 if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
10112 /*
10113 * pkt_reason should be CMD_CMPLT for DEVICE ERROR.
10114 * Under this condition ERR bit is set for ATA command,
10115 * and CHK bit set for ATAPI command.
10116 *
10117 * Please check st_intr & sdintr about how pkt_reason
10118 * is used.
10119 */
10120 scsipkt->pkt_reason = CMD_CMPLT;
10121
10122 /*
10123 * We may not have ARQ data if there was a double
10124 * error. But sense data in sata packet was pre-set
10125 * with NO SENSE so it is valid even if HBA could
10126 * not retrieve a real sense data.
10127 * Just copy this sense data into scsi pkt sense area.
10128 */
10129 bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense,
10130 SATA_ATAPI_MIN_RQSENSE_LEN);
10131 #ifdef SATA_DEBUG
10132 if (sata_debug_flags & SATA_DBG_SCSI_IF) {
10133 sata_log(spx->txlt_sata_hba_inst, CE_WARN,
10134 "sata_txlt_atapi_completion: %02x\n"
10135 "RQSENSE: %02x %02x %02x %02x %02x %02x "
10136 " %02x %02x %02x %02x %02x %02x "
10137 " %02x %02x %02x %02x %02x %02x\n",
10138 scsipkt->pkt_reason,
10139 rqsp[0], rqsp[1], rqsp[2], rqsp[3],
10140 rqsp[4], rqsp[5], rqsp[6], rqsp[7],
10141 rqsp[8], rqsp[9], rqsp[10], rqsp[11],
10142 rqsp[12], rqsp[13], rqsp[14], rqsp[15],
10143 rqsp[16], rqsp[17]);
10144 }
10145 #endif
10146 } else {
10147 switch (sata_pkt->satapkt_reason) {
10148 case SATA_PKT_PORT_ERROR:
10149 /*
10150 * We have no device data.
10151 */
10152 scsipkt->pkt_reason = CMD_INCOMPLETE;
10153 scsipkt->pkt_state &= ~(STATE_GOT_BUS |
10154 STATE_GOT_TARGET | STATE_SENT_CMD |
10155 STATE_GOT_STATUS);
10156 sense->es_key = KEY_HARDWARE_ERROR;
10157 break;
10158
10159 case SATA_PKT_TIMEOUT:
10160 scsipkt->pkt_reason = CMD_TIMEOUT;
10161 scsipkt->pkt_statistics |=
10162 STAT_TIMEOUT | STAT_DEV_RESET;
10163 /*
10164 * Need to check if HARDWARE_ERROR/
10165 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more
10166 * appropriate.
10167 */
10168 break;
10169
10170 case SATA_PKT_ABORTED:
10171 scsipkt->pkt_reason = CMD_ABORTED;
10172 scsipkt->pkt_statistics |= STAT_ABORTED;
10173 /* Should we set key COMMAND_ABPRTED? */
10174 break;
10175
10176 case SATA_PKT_RESET:
10177 scsipkt->pkt_reason = CMD_RESET;
10178 scsipkt->pkt_statistics |= STAT_DEV_RESET;
10179 /*
10180 * May be we should set Unit Attention /
10181 * Reset. Perhaps the same should be
10182 * returned for disks....
10183 */
10184 sense->es_key = KEY_UNIT_ATTENTION;
10185 sense->es_add_code = SD_SCSI_ASC_RESET;
10186 break;
10187
10188 default:
10189 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
10190 "sata_txlt_atapi_completion: "
10191 "invalid packet completion reason"));
10192 scsipkt->pkt_reason = CMD_TRAN_ERR;
10193 scsipkt->pkt_state &= ~(STATE_GOT_BUS |
10194 STATE_GOT_TARGET | STATE_SENT_CMD |
10195 STATE_GOT_STATUS);
10196 break;
10197 }
10198 }
10199 }
10200
10201 SATAATAPITRACE(spx, 0);
10202
10203 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
10204 scsipkt->pkt_comp != NULL) {
10205 /* scsi callback required */
10206 (*scsipkt->pkt_comp)(scsipkt);
10207 }
10208 }
10209
10210 /*
10211 * Set up error retrieval sata command for ATAPI Packet Command error data
10212 * recovery.
10213 *
10214 * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
10215 * returns SATA_FAILURE otherwise.
10216 */
10217
10218 static int
sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t * spx,sata_drive_info_t * sdinfo)10219 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
10220 {
10221 sata_pkt_t *spkt = spx->txlt_sata_pkt;
10222 sata_cmd_t *scmd;
10223 struct buf *bp;
10224
10225 /*
10226 * Allocate dma-able buffer error data.
10227 * Buffer allocation will take care of buffer alignment and other DMA
10228 * attributes.
10229 */
10230 bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN);
10231 if (bp == NULL) {
10232 SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
10233 "sata_get_err_retrieval_pkt: "
10234 "cannot allocate buffer for error data", NULL);
10235 return (SATA_FAILURE);
10236 }
10237 bp_mapin(bp); /* make data buffer accessible */
10238
10239 /* Operation modes are up to the caller */
10240 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10241
10242 /* Synchronous mode, no callback - may be changed by the caller */
10243 spkt->satapkt_comp = NULL;
10244 spkt->satapkt_time = sata_default_pkt_time;
10245
10246 scmd = &spkt->satapkt_cmd;
10247 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
10248 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10249
10250 sata_atapi_packet_cmd_setup(scmd, sdinfo);
10251
10252 /*
10253 * Set-up acdb. Request Sense CDB (packet command content) is
10254 * not in DMA-able buffer. Its handling is HBA-specific (how
10255 * it is transfered into packet FIS).
10256 */
10257 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
10258 bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN);
10259 /* Following zeroing of pad bytes may not be necessary */
10260 bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN],
10261 sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN);
10262
10263 /*
10264 * Set-up pointer to the buffer handle, so HBA can sync buffer
10265 * before accessing it. Handle is in usual place in translate struct.
10266 */
10267 scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
10268
10269 /*
10270 * Preset request sense data to NO SENSE.
10271 * Here it is redundant, only for a symetry with scsi-originated
10272 * packets. It should not be used for anything but debugging.
10273 */
10274 bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
10275 sata_fixed_sense_data_preset(
10276 (struct scsi_extended_sense *)scmd->satacmd_rqsense);
10277
10278 ASSERT(scmd->satacmd_num_dma_cookies != 0);
10279 ASSERT(scmd->satacmd_dma_cookie_list != NULL);
10280
10281 return (SATA_SUCCESS);
10282 }
10283
10284 /*
10285 * Set-up ATAPI packet command.
10286 * Data transfer direction has to be set-up in sata_cmd structure prior to
10287 * calling this function.
10288 *
10289 * Returns void
10290 */
10291
10292 static void
sata_atapi_packet_cmd_setup(sata_cmd_t * scmd,sata_drive_info_t * sdinfo)10293 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo)
10294 {
10295 scmd->satacmd_addr_type = 0; /* N/A */
10296 scmd->satacmd_sec_count_lsb = 0; /* no tag */
10297 scmd->satacmd_lba_low_lsb = 0; /* N/A */
10298 scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ;
10299 scmd->satacmd_lba_high_lsb =
10300 (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8);
10301 scmd->satacmd_cmd_reg = SATAC_PACKET; /* Command */
10302
10303 /*
10304 * We want all data to be transfered via DMA.
10305 * But specify it only if drive supports DMA and DMA mode is
10306 * selected - some drives are sensitive about it.
10307 * Hopefully it wil work for all drives....
10308 */
10309 if (sdinfo->satadrv_settings & SATA_DEV_DMA)
10310 scmd->satacmd_features_reg = SATA_ATAPI_F_DMA;
10311
10312 /*
10313 * Features register requires special care for devices that use
10314 * Serial ATA bridge - they need an explicit specification of
10315 * the data transfer direction for Packet DMA commands.
10316 * Setting this bit is harmless if DMA is not used.
10317 *
10318 * Many drives do not implement word 80, specifying what ATA/ATAPI
10319 * spec they follow.
10320 * We are arbitrarily following the latest SerialATA 2.6 spec,
10321 * which uses ATA/ATAPI 6 specification for Identify Data, unless
10322 * ATA/ATAPI-7 support is explicitly indicated.
10323 */
10324 if (sdinfo->satadrv_id.ai_majorversion != 0 &&
10325 sdinfo->satadrv_id.ai_majorversion != 0xffff &&
10326 (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) {
10327 /*
10328 * Specification of major version is valid and version 7
10329 * is supported. It does automatically imply that all
10330 * spec features are supported. For now, we assume that
10331 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete.
10332 */
10333 if ((sdinfo->satadrv_id.ai_dirdma &
10334 SATA_ATAPI_ID_DMADIR_REQ) != 0) {
10335 if (scmd->satacmd_flags.sata_data_direction ==
10336 SATA_DIR_READ) {
10337 scmd->satacmd_features_reg |=
10338 SATA_ATAPI_F_DATA_DIR_READ;
10339 }
10340 }
10341 }
10342 }
10343
10344
10345 #ifdef SATA_DEBUG
10346
10347 /* Display 18 bytes of Inquiry data */
10348 static void
sata_show_inqry_data(uint8_t * buf)10349 sata_show_inqry_data(uint8_t *buf)
10350 {
10351 struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
10352 uint8_t *p;
10353
10354 cmn_err(CE_NOTE, "Inquiry data:");
10355 cmn_err(CE_NOTE, "device type %x", inq->inq_dtype);
10356 cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb);
10357 cmn_err(CE_NOTE, "version %x", inq->inq_ansi);
10358 cmn_err(CE_NOTE, "ATAPI transport version %d",
10359 SATA_ATAPI_TRANS_VERSION(inq));
10360 cmn_err(CE_NOTE, "response data format %d, aenc %d",
10361 inq->inq_rdf, inq->inq_aenc);
10362 cmn_err(CE_NOTE, " additional length %d", inq->inq_len);
10363 cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs);
10364 p = (uint8_t *)inq->inq_vid;
10365 cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x "
10366 "%02x %02x %02x %02x",
10367 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
10368 p = (uint8_t *)inq->inq_vid;
10369 cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c",
10370 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
10371
10372 p = (uint8_t *)inq->inq_pid;
10373 cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x "
10374 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
10375 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
10376 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
10377 p = (uint8_t *)inq->inq_pid;
10378 cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c "
10379 "%c %c %c %c %c %c %c %c",
10380 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
10381 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
10382
10383 p = (uint8_t *)inq->inq_revision;
10384 cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x",
10385 p[0], p[1], p[2], p[3]);
10386 p = (uint8_t *)inq->inq_revision;
10387 cmn_err(CE_NOTE, "revision: %c %c %c %c",
10388 p[0], p[1], p[2], p[3]);
10389
10390 }
10391
10392
10393 static void
sata_save_atapi_trace(sata_pkt_txlate_t * spx,int count)10394 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count)
10395 {
10396 struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt;
10397
10398 if (scsi_pkt == NULL)
10399 return;
10400 if (count != 0) {
10401 /* saving cdb */
10402 bzero(sata_atapi_trace[sata_atapi_trace_index].acdb,
10403 SATA_ATAPI_MAX_CDB_LEN);
10404 bcopy(scsi_pkt->pkt_cdbp,
10405 sata_atapi_trace[sata_atapi_trace_index].acdb, count);
10406 } else {
10407 bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)->
10408 sts_sensedata,
10409 sata_atapi_trace[sata_atapi_trace_index].arqs,
10410 SATA_ATAPI_MIN_RQSENSE_LEN);
10411 sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason =
10412 scsi_pkt->pkt_reason;
10413 sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason =
10414 spx->txlt_sata_pkt->satapkt_reason;
10415
10416 if (++sata_atapi_trace_index >= 64)
10417 sata_atapi_trace_index = 0;
10418 }
10419 }
10420
10421 #endif
10422
10423 /*
10424 * Fetch inquiry data from ATAPI device
10425 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
10426 *
10427 * Note:
10428 * inqb pointer does not point to a DMA-able buffer. It is a local buffer
10429 * where the caller expects to see the inquiry data.
10430 *
10431 */
10432
10433 static int
sata_get_atapi_inquiry_data(sata_hba_inst_t * sata_hba,sata_address_t * saddr,struct scsi_inquiry * inq)10434 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba,
10435 sata_address_t *saddr, struct scsi_inquiry *inq)
10436 {
10437 sata_pkt_txlate_t *spx;
10438 sata_pkt_t *spkt;
10439 struct buf *bp;
10440 sata_drive_info_t *sdinfo;
10441 sata_cmd_t *scmd;
10442 int rval;
10443 uint8_t *rqsp;
10444 dev_info_t *dip = SATA_DIP(sata_hba);
10445 #ifdef SATA_DEBUG
10446 char msg_buf[MAXPATHLEN];
10447 #endif
10448 kmutex_t *cport_mutex;
10449
10450 ASSERT(sata_hba != NULL);
10451
10452 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10453 spx->txlt_sata_hba_inst = sata_hba;
10454 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */
10455 spkt = sata_pkt_alloc(spx, NULL);
10456 if (spkt == NULL) {
10457 kmem_free(spx, sizeof (sata_pkt_txlate_t));
10458 return (SATA_FAILURE);
10459 }
10460 /* address is needed now */
10461 spkt->satapkt_device.satadev_addr = *saddr;
10462
10463 /* scsi_inquiry size buffer */
10464 bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry));
10465 if (bp == NULL) {
10466 sata_pkt_free(spx);
10467 kmem_free(spx, sizeof (sata_pkt_txlate_t));
10468 SATA_LOG_D((sata_hba, CE_WARN,
10469 "sata_get_atapi_inquiry_data: "
10470 "cannot allocate data buffer"));
10471 return (SATA_FAILURE);
10472 }
10473 bp_mapin(bp); /* make data buffer accessible */
10474
10475 scmd = &spkt->satapkt_cmd;
10476 ASSERT(scmd->satacmd_num_dma_cookies != 0);
10477 ASSERT(scmd->satacmd_dma_cookie_list != NULL);
10478
10479 /* Use synchronous mode */
10480 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10481 spkt->satapkt_comp = NULL;
10482 spkt->satapkt_time = sata_default_pkt_time;
10483
10484 /* Issue inquiry command - 6 bytes cdb, data transfer, read */
10485
10486 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
10487 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10488
10489 cport_mutex = &(SATA_CPORT_MUTEX(sata_hba, saddr->cport));
10490 mutex_enter(cport_mutex);
10491 sdinfo = sata_get_device_info(sata_hba,
10492 &spx->txlt_sata_pkt->satapkt_device);
10493 if (sdinfo == NULL) {
10494 /* we have to be carefull about the disapearing device */
10495 mutex_exit(cport_mutex);
10496 rval = SATA_FAILURE;
10497 goto cleanup;
10498 }
10499 sata_atapi_packet_cmd_setup(scmd, sdinfo);
10500
10501 /*
10502 * Set-up acdb. This works for atapi transport version 2 and later.
10503 */
10504 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
10505 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
10506 scmd->satacmd_acdb[0] = 0x12; /* Inquiry */
10507 scmd->satacmd_acdb[1] = 0x00;
10508 scmd->satacmd_acdb[2] = 0x00;
10509 scmd->satacmd_acdb[3] = 0x00;
10510 scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
10511 scmd->satacmd_acdb[5] = 0x00;
10512
10513 sata_fixed_sense_data_preset(
10514 (struct scsi_extended_sense *)scmd->satacmd_rqsense);
10515
10516 /* Transfer command to HBA */
10517 if (sata_hba_start(spx, &rval) != 0) {
10518 /* Pkt not accepted for execution */
10519 SATADBG1(SATA_DBG_ATAPI, sata_hba,
10520 "sata_get_atapi_inquiry_data: "
10521 "Packet not accepted for execution - ret: %02x", rval);
10522 mutex_exit(cport_mutex);
10523 rval = SATA_FAILURE;
10524 goto cleanup;
10525 }
10526 mutex_exit(cport_mutex);
10527
10528 if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10529 SATADBG1(SATA_DBG_ATAPI, sata_hba,
10530 "sata_get_atapi_inquiry_data: "
10531 "Packet completed successfully - ret: %02x", rval);
10532 if (spx->txlt_buf_dma_handle != NULL) {
10533 /*
10534 * Sync buffer. Handle is in usual place in translate
10535 * struct.
10536 */
10537 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10538 DDI_DMA_SYNC_FORCPU);
10539 ASSERT(rval == DDI_SUCCESS);
10540 }
10541
10542 if (sata_check_for_dma_error(dip, spx)) {
10543 ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED);
10544 rval = SATA_FAILURE;
10545 } else {
10546 /*
10547 * Normal completion - copy data into caller's buffer
10548 */
10549 bcopy(bp->b_un.b_addr, (uint8_t *)inq,
10550 sizeof (struct scsi_inquiry));
10551 #ifdef SATA_DEBUG
10552 if (sata_debug_flags & SATA_DBG_ATAPI) {
10553 sata_show_inqry_data((uint8_t *)inq);
10554 }
10555 #endif
10556 rval = SATA_SUCCESS;
10557 }
10558 } else {
10559 /*
10560 * Something went wrong - analyze return - check rqsense data
10561 */
10562 rval = SATA_FAILURE;
10563 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
10564 /*
10565 * ARQ data hopefull show something other than NO SENSE
10566 */
10567 rqsp = scmd->satacmd_rqsense;
10568 #ifdef SATA_DEBUG
10569 if (sata_debug_flags & SATA_DBG_ATAPI) {
10570 msg_buf[0] = '\0';
10571 (void) snprintf(msg_buf, MAXPATHLEN,
10572 "ATAPI packet completion reason: %02x\n"
10573 "RQSENSE: %02x %02x %02x %02x %02x %02x\n"
10574 " %02x %02x %02x %02x %02x %02x\n"
10575 " %02x %02x %02x %02x %02x %02x",
10576 spkt->satapkt_reason,
10577 rqsp[0], rqsp[1], rqsp[2], rqsp[3],
10578 rqsp[4], rqsp[5], rqsp[6], rqsp[7],
10579 rqsp[8], rqsp[9], rqsp[10], rqsp[11],
10580 rqsp[12], rqsp[13], rqsp[14], rqsp[15],
10581 rqsp[16], rqsp[17]);
10582 sata_log(spx->txlt_sata_hba_inst, CE_WARN,
10583 "%s", msg_buf);
10584 }
10585 #endif
10586 } else {
10587 switch (spkt->satapkt_reason) {
10588 case SATA_PKT_PORT_ERROR:
10589 SATADBG1(SATA_DBG_ATAPI, sata_hba,
10590 "sata_get_atapi_inquiry_data: "
10591 "packet reason: port error", NULL);
10592 break;
10593
10594 case SATA_PKT_TIMEOUT:
10595 SATADBG1(SATA_DBG_ATAPI, sata_hba,
10596 "sata_get_atapi_inquiry_data: "
10597 "packet reason: timeout", NULL);
10598 break;
10599
10600 case SATA_PKT_ABORTED:
10601 SATADBG1(SATA_DBG_ATAPI, sata_hba,
10602 "sata_get_atapi_inquiry_data: "
10603 "packet reason: aborted", NULL);
10604 break;
10605
10606 case SATA_PKT_RESET:
10607 SATADBG1(SATA_DBG_ATAPI, sata_hba,
10608 "sata_get_atapi_inquiry_data: "
10609 "packet reason: reset\n", NULL);
10610 break;
10611 default:
10612 SATADBG1(SATA_DBG_ATAPI, sata_hba,
10613 "sata_get_atapi_inquiry_data: "
10614 "invalid packet reason: %02x\n",
10615 spkt->satapkt_reason);
10616 break;
10617 }
10618 }
10619 }
10620 cleanup:
10621 sata_free_local_buffer(spx);
10622 sata_pkt_free(spx);
10623 kmem_free(spx, sizeof (sata_pkt_txlate_t));
10624 return (rval);
10625 }
10626
10627
10628
10629
10630
10631 #if 0
10632 #ifdef SATA_DEBUG
10633
10634 /*
10635 * Test ATAPI packet command.
10636 * Single threaded test: send packet command in synch mode, process completion
10637 *
10638 */
10639 static void
10640 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport)
10641 {
10642 sata_pkt_txlate_t *spx;
10643 sata_pkt_t *spkt;
10644 struct buf *bp;
10645 sata_device_t sata_device;
10646 sata_drive_info_t *sdinfo;
10647 sata_cmd_t *scmd;
10648 int rval;
10649 uint8_t *rqsp;
10650
10651 ASSERT(sata_hba_inst != NULL);
10652 sata_device.satadev_addr.cport = cport;
10653 sata_device.satadev_addr.pmport = 0;
10654 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
10655 sata_device.satadev_rev = SATA_DEVICE_REV;
10656 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10657 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
10658 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10659 if (sdinfo == NULL) {
10660 sata_log(sata_hba_inst, CE_WARN,
10661 "sata_test_atapi_packet_command: "
10662 "no device info for cport %d",
10663 sata_device.satadev_addr.cport);
10664 return;
10665 }
10666
10667 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10668 spx->txlt_sata_hba_inst = sata_hba_inst;
10669 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */
10670 spkt = sata_pkt_alloc(spx, NULL);
10671 if (spkt == NULL) {
10672 kmem_free(spx, sizeof (sata_pkt_txlate_t));
10673 return;
10674 }
10675 /* address is needed now */
10676 spkt->satapkt_device.satadev_addr = sata_device.satadev_addr;
10677
10678 /* 1024k buffer */
10679 bp = sata_alloc_local_buffer(spx, 1024);
10680 if (bp == NULL) {
10681 sata_pkt_free(spx);
10682 kmem_free(spx, sizeof (sata_pkt_txlate_t));
10683 sata_log(sata_hba_inst, CE_WARN,
10684 "sata_test_atapi_packet_command: "
10685 "cannot allocate data buffer");
10686 return;
10687 }
10688 bp_mapin(bp); /* make data buffer accessible */
10689
10690 scmd = &spkt->satapkt_cmd;
10691 ASSERT(scmd->satacmd_num_dma_cookies != 0);
10692 ASSERT(scmd->satacmd_dma_cookie_list != NULL);
10693
10694 /* Use synchronous mode */
10695 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10696
10697 /* Synchronous mode, no callback - may be changed by the caller */
10698 spkt->satapkt_comp = NULL;
10699 spkt->satapkt_time = sata_default_pkt_time;
10700
10701 /* Issue inquiry command - 6 bytes cdb, data transfer, read */
10702
10703 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
10704 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10705
10706 sata_atapi_packet_cmd_setup(scmd, sdinfo);
10707
10708 /* Set-up acdb. */
10709 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
10710 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
10711 scmd->satacmd_acdb[0] = 0x12; /* Inquiry */
10712 scmd->satacmd_acdb[1] = 0x00;
10713 scmd->satacmd_acdb[2] = 0x00;
10714 scmd->satacmd_acdb[3] = 0x00;
10715 scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
10716 scmd->satacmd_acdb[5] = 0x00;
10717
10718 sata_fixed_sense_data_preset(
10719 (struct scsi_extended_sense *)scmd->satacmd_rqsense);
10720
10721 /* Transfer command to HBA */
10722 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10723 if (sata_hba_start(spx, &rval) != 0) {
10724 /* Pkt not accepted for execution */
10725 sata_log(sata_hba_inst, CE_WARN,
10726 "sata_test_atapi_packet_command: "
10727 "Packet not accepted for execution - ret: %02x", rval);
10728 mutex_exit(
10729 &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10730 goto cleanup;
10731 }
10732 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10733
10734 if (spx->txlt_buf_dma_handle != NULL) {
10735 /*
10736 * Sync buffer. Handle is in usual place in translate struct.
10737 */
10738 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10739 DDI_DMA_SYNC_FORCPU);
10740 ASSERT(rval == DDI_SUCCESS);
10741 }
10742 if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10743 sata_log(sata_hba_inst, CE_WARN,
10744 "sata_test_atapi_packet_command: "
10745 "Packet completed successfully");
10746 /*
10747 * Normal completion - show inquiry data
10748 */
10749 sata_show_inqry_data((uint8_t *)bp->b_un.b_addr);
10750 } else {
10751 /*
10752 * Something went wrong - analyze return - check rqsense data
10753 */
10754 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
10755 /*
10756 * ARQ data hopefull show something other than NO SENSE
10757 */
10758 rqsp = scmd->satacmd_rqsense;
10759 sata_log(spx->txlt_sata_hba_inst, CE_WARN,
10760 "ATAPI packet completion reason: %02x\n"
10761 "RQSENSE: %02x %02x %02x %02x %02x %02x "
10762 " %02x %02x %02x %02x %02x %02x "
10763 " %02x %02x %02x %02x %02x %02x\n",
10764 spkt->satapkt_reason,
10765 rqsp[0], rqsp[1], rqsp[2], rqsp[3],
10766 rqsp[4], rqsp[5], rqsp[6], rqsp[7],
10767 rqsp[8], rqsp[9], rqsp[10], rqsp[11],
10768 rqsp[12], rqsp[13], rqsp[14], rqsp[15],
10769 rqsp[16], rqsp[17]);
10770 } else {
10771 switch (spkt->satapkt_reason) {
10772 case SATA_PKT_PORT_ERROR:
10773 sata_log(sata_hba_inst, CE_WARN,
10774 "sata_test_atapi_packet_command: "
10775 "packet reason: port error\n");
10776 break;
10777
10778 case SATA_PKT_TIMEOUT:
10779 sata_log(sata_hba_inst, CE_WARN,
10780 "sata_test_atapi_packet_command: "
10781 "packet reason: timeout\n");
10782 break;
10783
10784 case SATA_PKT_ABORTED:
10785 sata_log(sata_hba_inst, CE_WARN,
10786 "sata_test_atapi_packet_command: "
10787 "packet reason: aborted\n");
10788 break;
10789
10790 case SATA_PKT_RESET:
10791 sata_log(sata_hba_inst, CE_WARN,
10792 "sata_test_atapi_packet_command: "
10793 "packet reason: reset\n");
10794 break;
10795 default:
10796 sata_log(sata_hba_inst, CE_WARN,
10797 "sata_test_atapi_packet_command: "
10798 "invalid packet reason: %02x\n",
10799 spkt->satapkt_reason);
10800 break;
10801 }
10802 }
10803 }
10804 cleanup:
10805 sata_free_local_buffer(spx);
10806 sata_pkt_free(spx);
10807 kmem_free(spx, sizeof (sata_pkt_txlate_t));
10808 }
10809
10810 #endif /* SATA_DEBUG */
10811 #endif /* 1 */
10812
10813
10814 /* ************************** LOCAL HELPER FUNCTIONS *********************** */
10815
10816 /*
10817 * Validate sata_tran info
10818 * SATA_FAILURE returns if structure is inconsistent or structure revision
10819 * does not match one used by the framework.
10820 *
10821 * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains
10822 * required function pointers.
10823 * Returns SATA_FAILURE otherwise.
10824 */
10825 static int
sata_validate_sata_hba_tran(dev_info_t * dip,sata_hba_tran_t * sata_tran)10826 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran)
10827 {
10828 /*
10829 * SATA_TRAN_HBA_REV is the current (highest) revision number
10830 * of the SATA interface.
10831 */
10832 if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) {
10833 sata_log(NULL, CE_WARN,
10834 "sata: invalid sata_hba_tran version %d for driver %s",
10835 sata_tran->sata_tran_hba_rev, ddi_driver_name(dip));
10836 return (SATA_FAILURE);
10837 }
10838
10839 if (dip != sata_tran->sata_tran_hba_dip) {
10840 SATA_LOG_D((NULL, CE_WARN,
10841 "sata: inconsistent sata_tran_hba_dip "
10842 "%p / %p", sata_tran->sata_tran_hba_dip, dip));
10843 return (SATA_FAILURE);
10844 }
10845
10846 if (sata_tran->sata_tran_probe_port == NULL ||
10847 sata_tran->sata_tran_start == NULL ||
10848 sata_tran->sata_tran_abort == NULL ||
10849 sata_tran->sata_tran_reset_dport == NULL ||
10850 sata_tran->sata_tran_hotplug_ops == NULL ||
10851 sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL ||
10852 sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate ==
10853 NULL) {
10854 SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing "
10855 "required functions"));
10856 }
10857 return (SATA_SUCCESS);
10858 }
10859
10860 /*
10861 * Remove HBA instance from sata_hba_list.
10862 */
10863 static void
sata_remove_hba_instance(dev_info_t * dip)10864 sata_remove_hba_instance(dev_info_t *dip)
10865 {
10866 sata_hba_inst_t *sata_hba_inst;
10867
10868 mutex_enter(&sata_mutex);
10869 for (sata_hba_inst = sata_hba_list;
10870 sata_hba_inst != (struct sata_hba_inst *)NULL;
10871 sata_hba_inst = sata_hba_inst->satahba_next) {
10872 if (sata_hba_inst->satahba_dip == dip)
10873 break;
10874 }
10875
10876 if (sata_hba_inst == (struct sata_hba_inst *)NULL) {
10877 #ifdef SATA_DEBUG
10878 cmn_err(CE_WARN, "sata_remove_hba_instance: "
10879 "unknown HBA instance\n");
10880 #endif
10881 ASSERT(FALSE);
10882 }
10883 if (sata_hba_inst == sata_hba_list) {
10884 sata_hba_list = sata_hba_inst->satahba_next;
10885 if (sata_hba_list) {
10886 sata_hba_list->satahba_prev =
10887 (struct sata_hba_inst *)NULL;
10888 }
10889 if (sata_hba_inst == sata_hba_list_tail) {
10890 sata_hba_list_tail = NULL;
10891 }
10892 } else if (sata_hba_inst == sata_hba_list_tail) {
10893 sata_hba_list_tail = sata_hba_inst->satahba_prev;
10894 if (sata_hba_list_tail) {
10895 sata_hba_list_tail->satahba_next =
10896 (struct sata_hba_inst *)NULL;
10897 }
10898 } else {
10899 sata_hba_inst->satahba_prev->satahba_next =
10900 sata_hba_inst->satahba_next;
10901 sata_hba_inst->satahba_next->satahba_prev =
10902 sata_hba_inst->satahba_prev;
10903 }
10904 mutex_exit(&sata_mutex);
10905 }
10906
10907 /*
10908 * Probe all SATA ports of the specified HBA instance.
10909 * The assumption is that there are no target and attachment point minor nodes
10910 * created by the boot subsystems, so we do not need to prune device tree.
10911 *
10912 * This function is called only from sata_hba_attach(). It does not have to
10913 * be protected by controller mutex, because the hba_attached flag is not set
10914 * yet and no one would be touching this HBA instance other than this thread.
10915 * Determines if port is active and what type of the device is attached
10916 * (if any). Allocates necessary structures for each port.
10917 *
10918 * An AP (Attachement Point) node is created for each SATA device port even
10919 * when there is no device attached.
10920 */
10921
10922 static void
sata_probe_ports(sata_hba_inst_t * sata_hba_inst)10923 sata_probe_ports(sata_hba_inst_t *sata_hba_inst)
10924 {
10925 dev_info_t *dip = SATA_DIP(sata_hba_inst);
10926 int ncport;
10927 sata_cport_info_t *cportinfo;
10928 sata_drive_info_t *drive;
10929 sata_device_t sata_device;
10930 int rval;
10931 dev_t minor_number;
10932 char name[16];
10933 clock_t start_time, cur_time;
10934
10935 /*
10936 * Probe controller ports first, to find port status and
10937 * any port multiplier attached.
10938 */
10939 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
10940 /* allocate cport structure */
10941 cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP);
10942 ASSERT(cportinfo != NULL);
10943 mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL);
10944
10945 mutex_enter(&cportinfo->cport_mutex);
10946
10947 cportinfo->cport_addr.cport = ncport;
10948 cportinfo->cport_addr.pmport = 0;
10949 cportinfo->cport_addr.qual = SATA_ADDR_CPORT;
10950 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
10951 cportinfo->cport_state |= SATA_STATE_PROBING;
10952 SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo;
10953
10954 /*
10955 * Regardless if a port is usable or not, create
10956 * an attachment point
10957 */
10958 mutex_exit(&cportinfo->cport_mutex);
10959 minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip),
10960 ncport, 0, SATA_ADDR_CPORT);
10961 (void) sprintf(name, "%d", ncport);
10962 if (ddi_create_minor_node(dip, name, S_IFCHR,
10963 minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) !=
10964 DDI_SUCCESS) {
10965 sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
10966 "cannot create SATA attachment point for port %d",
10967 ncport);
10968 }
10969
10970 /* Probe port */
10971 start_time = ddi_get_lbolt();
10972 reprobe_cport:
10973 sata_device.satadev_addr.cport = ncport;
10974 sata_device.satadev_addr.pmport = 0;
10975 sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
10976 sata_device.satadev_rev = SATA_DEVICE_REV;
10977
10978 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10979 (dip, &sata_device);
10980
10981 mutex_enter(&cportinfo->cport_mutex);
10982 cportinfo->cport_scr = sata_device.satadev_scr;
10983 if (rval != SATA_SUCCESS) {
10984 /* Something went wrong? Fail the port */
10985 cportinfo->cport_state = SATA_PSTATE_FAILED;
10986 mutex_exit(&cportinfo->cport_mutex);
10987 continue;
10988 }
10989 cportinfo->cport_state &= ~SATA_STATE_PROBING;
10990 cportinfo->cport_state |= SATA_STATE_PROBED;
10991 cportinfo->cport_dev_type = sata_device.satadev_type;
10992
10993 cportinfo->cport_state |= SATA_STATE_READY;
10994 if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) {
10995 mutex_exit(&cportinfo->cport_mutex);
10996 continue;
10997 }
10998 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
10999 /*
11000 * There is some device attached.
11001 * Allocate device info structure
11002 */
11003 if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) {
11004 mutex_exit(&cportinfo->cport_mutex);
11005 SATA_CPORTINFO_DRV_INFO(cportinfo) =
11006 kmem_zalloc(sizeof (sata_drive_info_t),
11007 KM_SLEEP);
11008 mutex_enter(&cportinfo->cport_mutex);
11009 }
11010 drive = SATA_CPORTINFO_DRV_INFO(cportinfo);
11011 drive->satadrv_addr = cportinfo->cport_addr;
11012 drive->satadrv_addr.qual = SATA_ADDR_DCPORT;
11013 drive->satadrv_type = cportinfo->cport_dev_type;
11014 drive->satadrv_state = SATA_STATE_UNKNOWN;
11015
11016 mutex_exit(&cportinfo->cport_mutex);
11017 if (sata_add_device(dip, sata_hba_inst, &sata_device) !=
11018 SATA_SUCCESS) {
11019 /*
11020 * Plugged device was not correctly identified.
11021 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT
11022 */
11023 cur_time = ddi_get_lbolt();
11024 if ((cur_time - start_time) <
11025 drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
11026 /* sleep for a while */
11027 delay(drv_usectohz(
11028 SATA_DEV_RETRY_DLY));
11029 goto reprobe_cport;
11030 }
11031 }
11032 } else { /* SATA_DTYPE_PMULT */
11033 mutex_exit(&cportinfo->cport_mutex);
11034
11035 /* Allocate sata_pmult_info and sata_pmport_info */
11036 if (sata_alloc_pmult(sata_hba_inst, &sata_device) !=
11037 SATA_SUCCESS)
11038 continue;
11039
11040 /* Log the information of the port multiplier */
11041 sata_show_pmult_info(sata_hba_inst, &sata_device);
11042
11043 /* Probe its pmports */
11044 sata_probe_pmports(sata_hba_inst, ncport);
11045 }
11046 }
11047 }
11048
11049 /*
11050 * Probe all device ports behind a port multiplier.
11051 *
11052 * PMult-related structure should be allocated before by sata_alloc_pmult().
11053 *
11054 * NOTE1: Only called from sata_probe_ports()
11055 * NOTE2: No mutex should be hold.
11056 */
11057 static void
sata_probe_pmports(sata_hba_inst_t * sata_hba_inst,uint8_t ncport)11058 sata_probe_pmports(sata_hba_inst_t *sata_hba_inst, uint8_t ncport)
11059 {
11060 dev_info_t *dip = SATA_DIP(sata_hba_inst);
11061 sata_pmult_info_t *pmultinfo = NULL;
11062 sata_pmport_info_t *pmportinfo = NULL;
11063 sata_drive_info_t *drive = NULL;
11064 sata_device_t sata_device;
11065
11066 clock_t start_time, cur_time;
11067 int npmport;
11068 int rval;
11069
11070 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, ncport);
11071
11072 /* Probe Port Multiplier ports */
11073 for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; npmport++) {
11074 pmportinfo = pmultinfo->pmult_dev_port[npmport];
11075 start_time = ddi_get_lbolt();
11076 reprobe_pmport:
11077 sata_device.satadev_addr.cport = ncport;
11078 sata_device.satadev_addr.pmport = npmport;
11079 sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
11080 sata_device.satadev_rev = SATA_DEVICE_REV;
11081
11082 /* Let HBA driver probe it. */
11083 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11084 (dip, &sata_device);
11085 mutex_enter(&pmportinfo->pmport_mutex);
11086
11087 pmportinfo->pmport_scr = sata_device.satadev_scr;
11088
11089 if (rval != SATA_SUCCESS) {
11090 pmportinfo->pmport_state =
11091 SATA_PSTATE_FAILED;
11092 mutex_exit(&pmportinfo->pmport_mutex);
11093 continue;
11094 }
11095 pmportinfo->pmport_state &= ~SATA_STATE_PROBING;
11096 pmportinfo->pmport_state |= SATA_STATE_PROBED;
11097 pmportinfo->pmport_dev_type = sata_device.satadev_type;
11098
11099 pmportinfo->pmport_state |= SATA_STATE_READY;
11100 if (pmportinfo->pmport_dev_type ==
11101 SATA_DTYPE_NONE) {
11102 SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
11103 "no device found at port %d:%d", ncport, npmport);
11104 mutex_exit(&pmportinfo->pmport_mutex);
11105 continue;
11106 }
11107 /* Port multipliers cannot be chained */
11108 ASSERT(pmportinfo->pmport_dev_type != SATA_DTYPE_PMULT);
11109 /*
11110 * There is something attached to Port
11111 * Multiplier device port
11112 * Allocate device info structure
11113 */
11114 if (pmportinfo->pmport_sata_drive == NULL) {
11115 mutex_exit(&pmportinfo->pmport_mutex);
11116 pmportinfo->pmport_sata_drive =
11117 kmem_zalloc(sizeof (sata_drive_info_t), KM_SLEEP);
11118 mutex_enter(&pmportinfo->pmport_mutex);
11119 }
11120 drive = pmportinfo->pmport_sata_drive;
11121 drive->satadrv_addr.cport = pmportinfo->pmport_addr.cport;
11122 drive->satadrv_addr.pmport = npmport;
11123 drive->satadrv_addr.qual = SATA_ADDR_DPMPORT;
11124 drive->satadrv_type = pmportinfo-> pmport_dev_type;
11125 drive->satadrv_state = SATA_STATE_UNKNOWN;
11126
11127 mutex_exit(&pmportinfo->pmport_mutex);
11128 rval = sata_add_device(dip, sata_hba_inst, &sata_device);
11129
11130 if (rval != SATA_SUCCESS) {
11131 /*
11132 * Plugged device was not correctly identified.
11133 * Retry, within the SATA_DEV_IDENTIFY_TIMEOUT
11134 */
11135 cur_time = ddi_get_lbolt();
11136 if ((cur_time - start_time) < drv_usectohz(
11137 SATA_DEV_IDENTIFY_TIMEOUT)) {
11138 /* sleep for a while */
11139 delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11140 goto reprobe_pmport;
11141 }
11142 }
11143 }
11144 }
11145
11146 /*
11147 * Add SATA device for specified HBA instance & port (SCSI target
11148 * device nodes).
11149 * This function is called (indirectly) only from sata_hba_attach().
11150 * A target node is created when there is a supported type device attached,
11151 * but may be removed if it cannot be put online.
11152 *
11153 * This function cannot be called from an interrupt context.
11154 *
11155 * Create target nodes for disk, CD/DVD, Tape and ATAPI disk devices
11156 *
11157 * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when
11158 * device identification failed - adding a device could be retried.
11159 *
11160 */
11161 static int
sata_add_device(dev_info_t * pdip,sata_hba_inst_t * sata_hba_inst,sata_device_t * sata_device)11162 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst,
11163 sata_device_t *sata_device)
11164 {
11165 sata_cport_info_t *cportinfo;
11166 sata_pmult_info_t *pminfo;
11167 sata_pmport_info_t *pmportinfo;
11168 dev_info_t *cdip; /* child dip */
11169 sata_address_t *saddr = &sata_device->satadev_addr;
11170 uint8_t cport, pmport;
11171 int rval;
11172
11173 cport = saddr->cport;
11174 pmport = saddr->pmport;
11175 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11176 ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE);
11177
11178 /*
11179 * Some device is attached to a controller port.
11180 * We rely on controllers distinquishing between no-device,
11181 * attached port multiplier and other kind of attached device.
11182 * We need to get Identify Device data and determine
11183 * positively the dev type before trying to attach
11184 * the target driver.
11185 */
11186 sata_device->satadev_rev = SATA_DEVICE_REV;
11187 switch (saddr->qual) {
11188 case SATA_ADDR_CPORT:
11189 /*
11190 * Add a non-port-multiplier device at controller port.
11191 */
11192 saddr->qual = SATA_ADDR_DCPORT;
11193
11194 rval = sata_probe_device(sata_hba_inst, sata_device);
11195 if (rval != SATA_SUCCESS ||
11196 sata_device->satadev_type == SATA_DTYPE_UNKNOWN)
11197 return (SATA_FAILURE);
11198
11199 mutex_enter(&cportinfo->cport_mutex);
11200 sata_show_drive_info(sata_hba_inst,
11201 SATA_CPORTINFO_DRV_INFO(cportinfo));
11202
11203 if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) {
11204 /*
11205 * Could not determine device type or
11206 * a device is not supported.
11207 * Degrade this device to unknown.
11208 */
11209 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11210 mutex_exit(&cportinfo->cport_mutex);
11211 return (SATA_SUCCESS);
11212 }
11213 cportinfo->cport_dev_type = sata_device->satadev_type;
11214 cportinfo->cport_tgtnode_clean = B_TRUE;
11215 mutex_exit(&cportinfo->cport_mutex);
11216
11217 /*
11218 * Initialize device to the desired state. Even if it
11219 * fails, the device will still attach but syslog
11220 * will show the warning.
11221 */
11222 if (sata_initialize_device(sata_hba_inst,
11223 SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) {
11224 /* Retry */
11225 rval = sata_initialize_device(sata_hba_inst,
11226 SATA_CPORTINFO_DRV_INFO(cportinfo));
11227
11228 if (rval == SATA_RETRY)
11229 sata_log(sata_hba_inst, CE_WARN,
11230 "SATA device at port %d - "
11231 "default device features could not be set."
11232 " Device may not operate as expected.",
11233 cport);
11234 }
11235
11236 cdip = sata_create_target_node(pdip, sata_hba_inst, saddr);
11237 if (cdip == NULL) {
11238 /*
11239 * Attaching target node failed.
11240 * We retain sata_drive_info structure...
11241 */
11242 return (SATA_SUCCESS);
11243 }
11244
11245 mutex_enter(&cportinfo->cport_mutex);
11246 (SATA_CPORTINFO_DRV_INFO(cportinfo))->
11247 satadrv_state = SATA_STATE_READY;
11248 mutex_exit(&cportinfo->cport_mutex);
11249
11250 break;
11251
11252 case SATA_ADDR_PMPORT:
11253 saddr->qual = SATA_ADDR_DPMPORT;
11254
11255 mutex_enter(&cportinfo->cport_mutex);
11256 /* It must be a Port Multiplier at the controller port */
11257 ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT);
11258
11259 pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
11260 pmportinfo = pminfo->pmult_dev_port[saddr->pmport];
11261 mutex_exit(&cportinfo->cport_mutex);
11262
11263 rval = sata_probe_device(sata_hba_inst, sata_device);
11264 if (rval != SATA_SUCCESS ||
11265 sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11266 return (SATA_FAILURE);
11267 }
11268
11269 mutex_enter(&pmportinfo->pmport_mutex);
11270 sata_show_drive_info(sata_hba_inst,
11271 SATA_PMPORTINFO_DRV_INFO(pmportinfo));
11272
11273 if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) {
11274 /*
11275 * Could not determine device type.
11276 * Degrade this device to unknown.
11277 */
11278 pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
11279 mutex_exit(&pmportinfo->pmport_mutex);
11280 return (SATA_SUCCESS);
11281 }
11282 pmportinfo->pmport_dev_type = sata_device->satadev_type;
11283 pmportinfo->pmport_tgtnode_clean = B_TRUE;
11284 mutex_exit(&pmportinfo->pmport_mutex);
11285
11286 /*
11287 * Initialize device to the desired state.
11288 * Even if it fails, the device will still
11289 * attach but syslog will show the warning.
11290 */
11291 if (sata_initialize_device(sata_hba_inst,
11292 pmportinfo->pmport_sata_drive) != SATA_SUCCESS) {
11293 /* Retry */
11294 rval = sata_initialize_device(sata_hba_inst,
11295 pmportinfo->pmport_sata_drive);
11296
11297 if (rval == SATA_RETRY)
11298 sata_log(sata_hba_inst, CE_WARN,
11299 "SATA device at port %d:%d - "
11300 "default device features could not be set."
11301 " Device may not operate as expected.",
11302 cport, pmport);
11303 }
11304
11305 cdip = sata_create_target_node(pdip, sata_hba_inst, saddr);
11306 if (cdip == NULL) {
11307 /*
11308 * Attaching target node failed.
11309 * We retain sata_drive_info structure...
11310 */
11311 return (SATA_SUCCESS);
11312 }
11313 mutex_enter(&pmportinfo->pmport_mutex);
11314 pmportinfo->pmport_sata_drive->satadrv_state |=
11315 SATA_STATE_READY;
11316 mutex_exit(&pmportinfo->pmport_mutex);
11317
11318 break;
11319
11320 default:
11321 return (SATA_FAILURE);
11322 }
11323
11324 return (SATA_SUCCESS);
11325 }
11326
11327 /*
11328 * Clean up target node at specific address.
11329 *
11330 * NOTE: No Mutex should be hold.
11331 */
11332 static int
sata_offline_device(sata_hba_inst_t * sata_hba_inst,sata_device_t * sata_device,sata_drive_info_t * sdinfo)11333 sata_offline_device(sata_hba_inst_t *sata_hba_inst,
11334 sata_device_t *sata_device, sata_drive_info_t *sdinfo)
11335 {
11336 uint8_t cport, pmport, qual;
11337 dev_info_t *tdip;
11338
11339 cport = sata_device->satadev_addr.cport;
11340 pmport = sata_device->satadev_addr.pmport;
11341 qual = sata_device->satadev_addr.qual;
11342
11343 if (qual == SATA_ADDR_DCPORT) {
11344 SATA_LOG_D((sata_hba_inst, CE_WARN,
11345 "sata_hba_ioctl: disconnect device at port %d", cport));
11346 } else {
11347 SATA_LOG_D((sata_hba_inst, CE_WARN,
11348 "sata_hba_ioctl: disconnect device at port %d:%d",
11349 cport, pmport));
11350 }
11351
11352 /* We are addressing attached device, not a port */
11353 sata_device->satadev_addr.qual =
11354 sdinfo->satadrv_addr.qual;
11355 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11356 &sata_device->satadev_addr);
11357 if (tdip != NULL && ndi_devi_offline(tdip,
11358 NDI_DEVI_REMOVE) != NDI_SUCCESS) {
11359 /*
11360 * Problem :
11361 * The target node remained attached.
11362 * This happens when the device file was open
11363 * or a node was waiting for resources.
11364 * Cannot do anything about it.
11365 */
11366 if (qual == SATA_ADDR_DCPORT) {
11367 SATA_LOG_D((sata_hba_inst, CE_WARN,
11368 "sata_hba_ioctl: disconnect: could "
11369 "not unconfigure device before "
11370 "disconnecting the SATA port %d",
11371 cport));
11372 } else {
11373 SATA_LOG_D((sata_hba_inst, CE_WARN,
11374 "sata_hba_ioctl: disconnect: could "
11375 "not unconfigure device before "
11376 "disconnecting the SATA port %d:%d",
11377 cport, pmport));
11378 }
11379 /*
11380 * Set DEVICE REMOVED state in the target
11381 * node. It will prevent access to the device
11382 * even when a new device is attached, until
11383 * the old target node is released, removed and
11384 * recreated for a new device.
11385 */
11386 sata_set_device_removed(tdip);
11387
11388 /*
11389 * Instruct event daemon to try the target
11390 * node cleanup later.
11391 */
11392 sata_set_target_node_cleanup(
11393 sata_hba_inst, &sata_device->satadev_addr);
11394 }
11395
11396
11397 return (SATA_SUCCESS);
11398 }
11399
11400
11401 /*
11402 * Create scsi target node for attached device, create node properties and
11403 * attach the node.
11404 * The node could be removed if the device onlining fails.
11405 *
11406 * A dev_info_t pointer is returned if operation is successful, NULL is
11407 * returned otherwise.
11408 */
11409
11410 static dev_info_t *
sata_create_target_node(dev_info_t * dip,sata_hba_inst_t * sata_hba_inst,sata_address_t * sata_addr)11411 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst,
11412 sata_address_t *sata_addr)
11413 {
11414 dev_info_t *cdip = NULL;
11415 int rval;
11416 char *nname = NULL;
11417 char **compatible = NULL;
11418 int ncompatible;
11419 struct scsi_inquiry inq;
11420 sata_device_t sata_device;
11421 sata_drive_info_t *sdinfo;
11422 int target;
11423 int i;
11424
11425 sata_device.satadev_rev = SATA_DEVICE_REV;
11426 sata_device.satadev_addr = *sata_addr;
11427
11428 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport)));
11429
11430 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
11431
11432 target = SATA_TO_SCSI_TARGET(sata_addr->cport,
11433 sata_addr->pmport, sata_addr->qual);
11434
11435 if (sdinfo == NULL) {
11436 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
11437 sata_addr->cport)));
11438 SATA_LOG_D((sata_hba_inst, CE_WARN,
11439 "sata_create_target_node: no sdinfo for target %x",
11440 target));
11441 return (NULL);
11442 }
11443
11444 /*
11445 * create or get scsi inquiry data, expected by
11446 * scsi_hba_nodename_compatible_get()
11447 * SATA hard disks get Identify Data translated into Inguiry Data.
11448 * ATAPI devices respond directly to Inquiry request.
11449 */
11450 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
11451 sata_identdev_to_inquiry(sata_hba_inst, sdinfo,
11452 (uint8_t *)&inq);
11453 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
11454 sata_addr->cport)));
11455 } else { /* Assume supported ATAPI device */
11456 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
11457 sata_addr->cport)));
11458 if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr,
11459 &inq) == SATA_FAILURE)
11460 return (NULL);
11461 /*
11462 * Save supported ATAPI transport version
11463 */
11464 sdinfo->satadrv_atapi_trans_ver =
11465 SATA_ATAPI_TRANS_VERSION(&inq);
11466 }
11467
11468 /* determine the node name and compatible */
11469 scsi_hba_nodename_compatible_get(&inq, NULL,
11470 inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
11471
11472 #ifdef SATA_DEBUG
11473 if (sata_debug_flags & SATA_DBG_NODES) {
11474 if (nname == NULL) {
11475 cmn_err(CE_NOTE, "sata_create_target_node: "
11476 "cannot determine nodename for target %d\n",
11477 target);
11478 } else {
11479 cmn_err(CE_WARN, "sata_create_target_node: "
11480 "target %d nodename: %s\n", target, nname);
11481 }
11482 if (compatible == NULL) {
11483 cmn_err(CE_WARN,
11484 "sata_create_target_node: no compatible name\n");
11485 } else {
11486 for (i = 0; i < ncompatible; i++) {
11487 cmn_err(CE_WARN, "sata_create_target_node: "
11488 "compatible name: %s\n", compatible[i]);
11489 }
11490 }
11491 }
11492 #endif
11493
11494 /* if nodename can't be determined, log error and exit */
11495 if (nname == NULL) {
11496 SATA_LOG_D((sata_hba_inst, CE_WARN,
11497 "sata_create_target_node: cannot determine nodename "
11498 "for target %d\n", target));
11499 scsi_hba_nodename_compatible_free(nname, compatible);
11500 return (NULL);
11501 }
11502 /*
11503 * Create scsi target node
11504 */
11505 ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip);
11506 rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
11507 "device-type", "scsi");
11508
11509 if (rval != DDI_PROP_SUCCESS) {
11510 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
11511 "updating device_type prop failed %d", rval));
11512 goto fail;
11513 }
11514
11515 /*
11516 * Create target node properties: target & lun
11517 */
11518 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target);
11519 if (rval != DDI_PROP_SUCCESS) {
11520 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
11521 "updating target prop failed %d", rval));
11522 goto fail;
11523 }
11524 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0);
11525 if (rval != DDI_PROP_SUCCESS) {
11526 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
11527 "updating target prop failed %d", rval));
11528 goto fail;
11529 }
11530
11531 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
11532 /*
11533 * Add "variant" property
11534 */
11535 rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
11536 "variant", "atapi");
11537 if (rval != DDI_PROP_SUCCESS) {
11538 SATA_LOG_D((sata_hba_inst, CE_WARN,
11539 "sata_create_target_node: variant atapi "
11540 "property could not be created: %d", rval));
11541 goto fail;
11542 }
11543 }
11544 /* decorate the node with compatible */
11545 if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible",
11546 compatible, ncompatible) != DDI_PROP_SUCCESS) {
11547 SATA_LOG_D((sata_hba_inst, CE_WARN,
11548 "sata_create_target_node: FAIL compatible props cdip 0x%p",
11549 (void *)cdip));
11550 goto fail;
11551 }
11552
11553 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
11554 /*
11555 * Add "sata-phy" property
11556 */
11557 if (ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "sata-phy",
11558 (int)sata_addr->cport) != DDI_PROP_SUCCESS) {
11559 SATA_LOG_D((sata_hba_inst, CE_WARN,
11560 "sata_create_target_node: failed to create "
11561 "\"sata-phy\" property: port %d",
11562 sata_addr->cport));
11563 }
11564 }
11565
11566
11567 /*
11568 * Now, try to attach the driver. If probing of the device fails,
11569 * the target node may be removed
11570 */
11571 rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH);
11572
11573 scsi_hba_nodename_compatible_free(nname, compatible);
11574
11575 if (rval == NDI_SUCCESS)
11576 return (cdip);
11577
11578 /* target node was removed - are we sure? */
11579 return (NULL);
11580
11581 fail:
11582 scsi_hba_nodename_compatible_free(nname, compatible);
11583 ddi_prop_remove_all(cdip);
11584 rval = ndi_devi_free(cdip);
11585 if (rval != NDI_SUCCESS) {
11586 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
11587 "node removal failed %d", rval));
11588 }
11589 sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: "
11590 "cannot create target node for SATA device at port %d",
11591 sata_addr->cport);
11592 return (NULL);
11593 }
11594
11595 /*
11596 * Remove a target node.
11597 */
11598 static void
sata_remove_target_node(sata_hba_inst_t * sata_hba_inst,sata_address_t * sata_addr)11599 sata_remove_target_node(sata_hba_inst_t *sata_hba_inst,
11600 sata_address_t *sata_addr)
11601 {
11602 dev_info_t *tdip;
11603 uint8_t cport = sata_addr->cport;
11604 uint8_t pmport = sata_addr->pmport;
11605 uint8_t qual = sata_addr->qual;
11606
11607 /* Note the sata daemon uses the address of the port/pmport */
11608 ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
11609
11610 /* Remove target node */
11611 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), cport, pmport);
11612 if (tdip != NULL) {
11613 /*
11614 * Target node exists. Unconfigure device
11615 * then remove the target node (one ndi
11616 * operation).
11617 */
11618 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
11619 /*
11620 * PROBLEM - no device, but target node remained. This
11621 * happens when the file was open or node was waiting
11622 * for resources.
11623 */
11624 SATA_LOG_D((sata_hba_inst, CE_WARN,
11625 "sata_remove_target_node: "
11626 "Failed to remove target node for "
11627 "detached SATA device."));
11628 /*
11629 * Set target node state to DEVI_DEVICE_REMOVED. But
11630 * re-check first that the node still exists.
11631 */
11632 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
11633 cport, pmport);
11634 if (tdip != NULL) {
11635 sata_set_device_removed(tdip);
11636 /*
11637 * Instruct event daemon to retry the cleanup
11638 * later.
11639 */
11640 sata_set_target_node_cleanup(sata_hba_inst,
11641 sata_addr);
11642 }
11643 }
11644
11645 if (qual == SATA_ADDR_CPORT)
11646 sata_log(sata_hba_inst, CE_WARN,
11647 "SATA device detached at port %d", cport);
11648 else
11649 sata_log(sata_hba_inst, CE_WARN,
11650 "SATA device detached at port %d:%d",
11651 cport, pmport);
11652 }
11653 #ifdef SATA_DEBUG
11654 else {
11655 if (qual == SATA_ADDR_CPORT)
11656 sata_log(sata_hba_inst, CE_WARN,
11657 "target node not found at port %d", cport);
11658 else
11659 sata_log(sata_hba_inst, CE_WARN,
11660 "target node not found at port %d:%d",
11661 cport, pmport);
11662 }
11663 #endif
11664 }
11665
11666
11667 /*
11668 * Re-probe sata port, check for a device and attach info
11669 * structures when necessary. Identify Device data is fetched, if possible.
11670 * Assumption: sata address is already validated.
11671 * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
11672 * the presence of a device and its type.
11673 *
11674 * flag arg specifies that the function should try multiple times to identify
11675 * device type and to initialize it, or it should return immediately on failure.
11676 * SATA_DEV_IDENTIFY_RETRY - retry
11677 * SATA_DEV_IDENTIFY_NORETRY - no retry
11678 *
11679 * SATA_FAILURE is returned if one of the operations failed.
11680 *
11681 * This function cannot be called in interrupt context - it may sleep.
11682 *
11683 * Note: Port multiplier is supported.
11684 */
11685 static int
sata_reprobe_port(sata_hba_inst_t * sata_hba_inst,sata_device_t * sata_device,int flag)11686 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11687 int flag)
11688 {
11689 sata_cport_info_t *cportinfo;
11690 sata_pmult_info_t *pmultinfo;
11691 sata_drive_info_t *sdinfo, *osdinfo;
11692 boolean_t init_device = B_FALSE;
11693 int prev_device_type = SATA_DTYPE_NONE;
11694 int prev_device_settings = 0;
11695 int prev_device_state = 0;
11696 clock_t start_time = 0;
11697 int retry = B_FALSE;
11698 uint8_t cport = sata_device->satadev_addr.cport;
11699 int rval_probe, rval_init;
11700
11701 /*
11702 * If target is pmport, sata_reprobe_pmport() will handle it.
11703 */
11704 if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT ||
11705 sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT)
11706 return (sata_reprobe_pmport(sata_hba_inst, sata_device, flag));
11707
11708 /* We only care about host sata cport for now */
11709 cportinfo = SATA_CPORT_INFO(sata_hba_inst,
11710 sata_device->satadev_addr.cport);
11711
11712 /*
11713 * If a port multiplier was previously attached (we have no idea it
11714 * still there or not), sata_reprobe_pmult() will handle it.
11715 */
11716 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT)
11717 return (sata_reprobe_pmult(sata_hba_inst, sata_device, flag));
11718
11719 /* Store sata_drive_info when a non-pmult device was attached. */
11720 osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11721 if (osdinfo != NULL) {
11722 /*
11723 * We are re-probing port with a previously attached device.
11724 * Save previous device type and settings.
11725 */
11726 prev_device_type = cportinfo->cport_dev_type;
11727 prev_device_settings = osdinfo->satadrv_settings;
11728 prev_device_state = osdinfo->satadrv_state;
11729 }
11730 if (flag == SATA_DEV_IDENTIFY_RETRY) {
11731 start_time = ddi_get_lbolt();
11732 retry = B_TRUE;
11733 }
11734 retry_probe:
11735
11736 /* probe port */
11737 mutex_enter(&cportinfo->cport_mutex);
11738 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11739 cportinfo->cport_state |= SATA_STATE_PROBING;
11740 mutex_exit(&cportinfo->cport_mutex);
11741
11742 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11743 (SATA_DIP(sata_hba_inst), sata_device);
11744
11745 mutex_enter(&cportinfo->cport_mutex);
11746 if (rval_probe != SATA_SUCCESS) {
11747 cportinfo->cport_state = SATA_PSTATE_FAILED;
11748 mutex_exit(&cportinfo->cport_mutex);
11749 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: "
11750 "SATA port %d probing failed",
11751 cportinfo->cport_addr.cport));
11752 return (SATA_FAILURE);
11753 }
11754
11755 /*
11756 * update sata port state and set device type
11757 */
11758 sata_update_port_info(sata_hba_inst, sata_device);
11759 cportinfo->cport_state &= ~SATA_STATE_PROBING;
11760
11761 /*
11762 * Sanity check - Port is active? Is the link active?
11763 * Is there any device attached?
11764 */
11765 if ((cportinfo->cport_state &
11766 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11767 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11768 SATA_PORT_DEVLINK_UP) {
11769 /*
11770 * Port in non-usable state or no link active/no device.
11771 * Free info structure if necessary (direct attached drive
11772 * only, for now!
11773 */
11774 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11775 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11776 /* Add here differentiation for device attached or not */
11777 cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11778 mutex_exit(&cportinfo->cport_mutex);
11779 if (sdinfo != NULL)
11780 kmem_free(sdinfo, sizeof (sata_drive_info_t));
11781 return (SATA_SUCCESS);
11782 }
11783
11784 cportinfo->cport_state |= SATA_STATE_READY;
11785 cportinfo->cport_state |= SATA_STATE_PROBED;
11786
11787 cportinfo->cport_dev_type = sata_device->satadev_type;
11788 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11789
11790 /*
11791 * If we are re-probing the port, there may be
11792 * sata_drive_info structure attached
11793 */
11794 if (sata_device->satadev_type == SATA_DTYPE_NONE) {
11795
11796 /*
11797 * There is no device, so remove device info structure,
11798 * if necessary.
11799 */
11800 /* Device change: Drive -> None */
11801 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11802 cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11803 if (sdinfo != NULL) {
11804 kmem_free(sdinfo, sizeof (sata_drive_info_t));
11805 sata_log(sata_hba_inst, CE_WARN,
11806 "SATA device detached "
11807 "from port %d", cportinfo->cport_addr.cport);
11808 }
11809 mutex_exit(&cportinfo->cport_mutex);
11810 return (SATA_SUCCESS);
11811
11812 }
11813
11814 if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
11815
11816 /* Device (may) change: Drive -> Drive */
11817 if (sdinfo == NULL) {
11818 /*
11819 * There is some device attached, but there is
11820 * no sata_drive_info structure - allocate one
11821 */
11822 mutex_exit(&cportinfo->cport_mutex);
11823 sdinfo = kmem_zalloc(
11824 sizeof (sata_drive_info_t), KM_SLEEP);
11825 mutex_enter(&cportinfo->cport_mutex);
11826 /*
11827 * Recheck, that the port state did not change when we
11828 * released mutex.
11829 */
11830 if (cportinfo->cport_state & SATA_STATE_READY) {
11831 SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo;
11832 sdinfo->satadrv_addr = cportinfo->cport_addr;
11833 sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT;
11834 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11835 sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
11836 } else {
11837 /*
11838 * Port is not in ready state, we
11839 * cannot attach a device.
11840 */
11841 mutex_exit(&cportinfo->cport_mutex);
11842 kmem_free(sdinfo, sizeof (sata_drive_info_t));
11843 return (SATA_SUCCESS);
11844 }
11845 /*
11846 * Since we are adding device, presumably new one,
11847 * indicate that it should be initalized,
11848 * as well as some internal framework states).
11849 */
11850 init_device = B_TRUE;
11851 }
11852 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11853 sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
11854 } else {
11855 /* Device change: Drive -> PMult */
11856 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11857 if (sdinfo != NULL) {
11858 kmem_free(sdinfo, sizeof (sata_drive_info_t));
11859 sata_log(sata_hba_inst, CE_WARN,
11860 "SATA device detached "
11861 "from port %d", cportinfo->cport_addr.cport);
11862 }
11863
11864 sata_log(sata_hba_inst, CE_WARN,
11865 "SATA port multiplier detected at port %d",
11866 cportinfo->cport_addr.cport);
11867
11868 mutex_exit(&cportinfo->cport_mutex);
11869 if (sata_alloc_pmult(sata_hba_inst, sata_device) !=
11870 SATA_SUCCESS)
11871 return (SATA_FAILURE);
11872 sata_show_pmult_info(sata_hba_inst, sata_device);
11873 mutex_enter(&cportinfo->cport_mutex);
11874
11875 /*
11876 * Mark all the port multiplier port behind the port
11877 * multiplier behind with link events, so that the sata daemon
11878 * will update their status.
11879 */
11880 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
11881 pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET;
11882 mutex_exit(&cportinfo->cport_mutex);
11883 return (SATA_SUCCESS);
11884 }
11885 mutex_exit(&cportinfo->cport_mutex);
11886
11887 /*
11888 * Figure out what kind of device we are really
11889 * dealing with. Failure of identifying device does not fail this
11890 * function.
11891 */
11892 rval_probe = sata_probe_device(sata_hba_inst, sata_device);
11893 rval_init = SATA_FAILURE;
11894 mutex_enter(&cportinfo->cport_mutex);
11895 if (rval_probe == SATA_SUCCESS) {
11896 /*
11897 * If we are dealing with the same type of a device as before,
11898 * restore its settings flags.
11899 */
11900 if (osdinfo != NULL &&
11901 sata_device->satadev_type == prev_device_type)
11902 sdinfo->satadrv_settings = prev_device_settings;
11903
11904 mutex_exit(&cportinfo->cport_mutex);
11905 rval_init = SATA_SUCCESS;
11906 /* Set initial device features, if necessary */
11907 if (init_device == B_TRUE) {
11908 rval_init = sata_initialize_device(sata_hba_inst,
11909 sdinfo);
11910 }
11911 if (rval_init == SATA_SUCCESS)
11912 return (rval_init);
11913 /* else we will retry if retry was asked for */
11914
11915 } else {
11916 /*
11917 * If there was some device info before we probe the device,
11918 * restore previous device setting, so we can retry from scratch
11919 * later. Providing, of course, that device has not disapear
11920 * during probing process.
11921 */
11922 if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11923 if (osdinfo != NULL) {
11924 cportinfo->cport_dev_type = prev_device_type;
11925 sdinfo->satadrv_type = prev_device_type;
11926 sdinfo->satadrv_state = prev_device_state;
11927 }
11928 } else {
11929 /* device is gone */
11930 kmem_free(sdinfo, sizeof (sata_drive_info_t));
11931 cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11932 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11933 mutex_exit(&cportinfo->cport_mutex);
11934 return (SATA_SUCCESS);
11935 }
11936 mutex_exit(&cportinfo->cport_mutex);
11937 }
11938
11939 if (retry) {
11940 clock_t cur_time = ddi_get_lbolt();
11941 /*
11942 * A device was not successfully identified or initialized.
11943 * Track retry time for device identification.
11944 */
11945 if ((cur_time - start_time) <
11946 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11947 /* sleep for a while */
11948 delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11949 goto retry_probe;
11950 }
11951 /* else no more retries */
11952 mutex_enter(&cportinfo->cport_mutex);
11953 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
11954 if (rval_init == SATA_RETRY) {
11955 /*
11956 * Setting drive features have failed, but
11957 * because the drive is still accessible,
11958 * keep it and emit a warning message.
11959 */
11960 sata_log(sata_hba_inst, CE_WARN,
11961 "SATA device at port %d - desired "
11962 "drive features could not be set. "
11963 "Device may not operate as expected.",
11964 cportinfo->cport_addr.cport);
11965 } else {
11966 SATA_CPORTINFO_DRV_INFO(cportinfo)->
11967 satadrv_state = SATA_DSTATE_FAILED;
11968 }
11969 }
11970 mutex_exit(&cportinfo->cport_mutex);
11971 }
11972 return (SATA_SUCCESS);
11973 }
11974
11975 /*
11976 * Reprobe a controller port that connected to a port multiplier.
11977 *
11978 * NOTE: No Mutex should be hold.
11979 */
11980 static int
sata_reprobe_pmult(sata_hba_inst_t * sata_hba_inst,sata_device_t * sata_device,int flag)11981 sata_reprobe_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11982 int flag)
11983 {
11984 _NOTE(ARGUNUSED(flag))
11985 sata_cport_info_t *cportinfo;
11986 sata_pmult_info_t *pmultinfo;
11987 uint8_t cport = sata_device->satadev_addr.cport;
11988 int rval_probe;
11989
11990 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11991 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
11992
11993 /* probe port */
11994 mutex_enter(&cportinfo->cport_mutex);
11995 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11996 cportinfo->cport_state |= SATA_STATE_PROBING;
11997 mutex_exit(&cportinfo->cport_mutex);
11998
11999 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
12000 (SATA_DIP(sata_hba_inst), sata_device);
12001
12002 mutex_enter(&cportinfo->cport_mutex);
12003 if (rval_probe != SATA_SUCCESS) {
12004 cportinfo->cport_state = SATA_PSTATE_FAILED;
12005 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmult: "
12006 "SATA port %d probing failed", cport));
12007 sata_log(sata_hba_inst, CE_WARN,
12008 "SATA port multiplier detached at port %d", cport);
12009 mutex_exit(&cportinfo->cport_mutex);
12010 sata_free_pmult(sata_hba_inst, sata_device);
12011 return (SATA_FAILURE);
12012 }
12013
12014 /*
12015 * update sata port state and set device type
12016 */
12017 sata_update_port_info(sata_hba_inst, sata_device);
12018 cportinfo->cport_state &= ~SATA_STATE_PROBING;
12019 cportinfo->cport_state |= SATA_STATE_PROBED;
12020
12021 /*
12022 * Sanity check - Port is active? Is the link active?
12023 * Is there any device attached?
12024 */
12025 if ((cportinfo->cport_state &
12026 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
12027 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
12028 SATA_PORT_DEVLINK_UP ||
12029 (sata_device->satadev_type == SATA_DTYPE_NONE)) {
12030 cportinfo->cport_dev_type = SATA_DTYPE_NONE;
12031 mutex_exit(&cportinfo->cport_mutex);
12032 sata_free_pmult(sata_hba_inst, sata_device);
12033 sata_log(sata_hba_inst, CE_WARN,
12034 "SATA port multiplier detached at port %d", cport);
12035 return (SATA_SUCCESS);
12036 }
12037
12038 /*
12039 * Device changed: PMult -> Non-PMult
12040 *
12041 * This situation is uncommon, most possibly being caused by errors
12042 * after which the port multiplier is not correct initialized and
12043 * recognized. In that case the new device will be marked as unknown
12044 * and will not be automatically probed in this routine. Instead
12045 * system administrator could manually restart it via cfgadm(8).
12046 */
12047 if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
12048 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
12049 mutex_exit(&cportinfo->cport_mutex);
12050 sata_free_pmult(sata_hba_inst, sata_device);
12051 sata_log(sata_hba_inst, CE_WARN,
12052 "SATA port multiplier detached at port %d", cport);
12053 return (SATA_FAILURE);
12054 }
12055
12056 /*
12057 * Now we know it is a port multiplier. However, if this is not the
12058 * previously attached port multiplier - they may have different
12059 * pmport numbers - we need to re-allocate data structures for every
12060 * pmport and drive.
12061 *
12062 * Port multipliers of the same model have identical values in these
12063 * registers, so it is still necessary to update the information of
12064 * all drives attached to the previous port multiplier afterwards.
12065 */
12066 /* Device changed: PMult -> another PMult */
12067 mutex_exit(&cportinfo->cport_mutex);
12068 sata_free_pmult(sata_hba_inst, sata_device);
12069 if (sata_alloc_pmult(sata_hba_inst, sata_device) != SATA_SUCCESS)
12070 return (SATA_FAILURE);
12071 mutex_enter(&cportinfo->cport_mutex);
12072
12073 SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
12074 "SATA port multiplier [changed] at port %d", cport);
12075 sata_log(sata_hba_inst, CE_WARN,
12076 "SATA port multiplier detected at port %d", cport);
12077
12078 /*
12079 * Mark all the port multiplier port behind the port
12080 * multiplier behind with link events, so that the sata daemon
12081 * will update their status.
12082 */
12083 pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET;
12084 mutex_exit(&cportinfo->cport_mutex);
12085
12086 return (SATA_SUCCESS);
12087 }
12088
12089 /*
12090 * Re-probe a port multiplier port, check for a device and attach info
12091 * structures when necessary. Identify Device data is fetched, if possible.
12092 * Assumption: sata address is already validated as port multiplier port.
12093 * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
12094 * the presence of a device and its type.
12095 *
12096 * flag arg specifies that the function should try multiple times to identify
12097 * device type and to initialize it, or it should return immediately on failure.
12098 * SATA_DEV_IDENTIFY_RETRY - retry
12099 * SATA_DEV_IDENTIFY_NORETRY - no retry
12100 *
12101 * SATA_FAILURE is returned if one of the operations failed.
12102 *
12103 * This function cannot be called in interrupt context - it may sleep.
12104 *
12105 * NOTE: Should be only called by sata_probe_port() in case target port is a
12106 * port multiplier port.
12107 * NOTE: No Mutex should be hold.
12108 */
12109 static int
sata_reprobe_pmport(sata_hba_inst_t * sata_hba_inst,sata_device_t * sata_device,int flag)12110 sata_reprobe_pmport(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
12111 int flag)
12112 {
12113 sata_cport_info_t *cportinfo = NULL;
12114 sata_pmport_info_t *pmportinfo = NULL;
12115 sata_drive_info_t *sdinfo, *osdinfo;
12116 sata_device_t sdevice;
12117 boolean_t init_device = B_FALSE;
12118 int prev_device_type = SATA_DTYPE_NONE;
12119 int prev_device_settings = 0;
12120 int prev_device_state = 0;
12121 clock_t start_time;
12122 uint8_t cport = sata_device->satadev_addr.cport;
12123 uint8_t pmport = sata_device->satadev_addr.pmport;
12124 int rval;
12125
12126 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
12127 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
12128 osdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
12129
12130 if (osdinfo != NULL) {
12131 /*
12132 * We are re-probing port with a previously attached device.
12133 * Save previous device type and settings.
12134 */
12135 prev_device_type = pmportinfo->pmport_dev_type;
12136 prev_device_settings = osdinfo->satadrv_settings;
12137 prev_device_state = osdinfo->satadrv_state;
12138 }
12139
12140 start_time = ddi_get_lbolt();
12141
12142 /* check parent status */
12143 mutex_enter(&cportinfo->cport_mutex);
12144 if ((cportinfo->cport_state &
12145 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
12146 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
12147 SATA_PORT_DEVLINK_UP) {
12148 mutex_exit(&cportinfo->cport_mutex);
12149 return (SATA_FAILURE);
12150 }
12151 mutex_exit(&cportinfo->cport_mutex);
12152
12153 retry_probe_pmport:
12154
12155 /* probe port */
12156 mutex_enter(&pmportinfo->pmport_mutex);
12157 pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
12158 pmportinfo->pmport_state |= SATA_STATE_PROBING;
12159 mutex_exit(&pmportinfo->pmport_mutex);
12160
12161 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
12162 (SATA_DIP(sata_hba_inst), sata_device);
12163
12164 /* might need retry because we cannot touch registers. */
12165 if (rval == SATA_FAILURE) {
12166 mutex_enter(&pmportinfo->pmport_mutex);
12167 pmportinfo->pmport_state = SATA_PSTATE_FAILED;
12168 mutex_exit(&pmportinfo->pmport_mutex);
12169 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: "
12170 "SATA port %d:%d probing failed",
12171 cport, pmport));
12172 return (SATA_FAILURE);
12173 } else if (rval == SATA_RETRY) {
12174 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: "
12175 "SATA port %d:%d probing failed, retrying...",
12176 cport, pmport));
12177 clock_t cur_time = ddi_get_lbolt();
12178 /*
12179 * A device was not successfully identified or initialized.
12180 * Track retry time for device identification.
12181 */
12182 if ((cur_time - start_time) <
12183 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
12184 /* sleep for a while */
12185 delay(drv_usectohz(SATA_DEV_RETRY_DLY));
12186 goto retry_probe_pmport;
12187 } else {
12188 mutex_enter(&pmportinfo->pmport_mutex);
12189 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL)
12190 SATA_PMPORTINFO_DRV_INFO(pmportinfo)->
12191 satadrv_state = SATA_DSTATE_FAILED;
12192 mutex_exit(&pmportinfo->pmport_mutex);
12193 return (SATA_SUCCESS);
12194 }
12195 }
12196
12197 /*
12198 * Sanity check - Controller port is active? Is the link active?
12199 * Is it still a port multiplier?
12200 */
12201 if ((cportinfo->cport_state &
12202 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
12203 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
12204 SATA_PORT_DEVLINK_UP ||
12205 (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) {
12206 /*
12207 * Port in non-usable state or no link active/no
12208 * device. Free info structure.
12209 */
12210 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
12211
12212 sdevice.satadev_addr.cport = cport;
12213 sdevice.satadev_addr.pmport = pmport;
12214 sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
12215 mutex_exit(&cportinfo->cport_mutex);
12216
12217 sata_free_pmult(sata_hba_inst, &sdevice);
12218 return (SATA_FAILURE);
12219 }
12220
12221 /* SATA_SUCCESS NOW */
12222 /*
12223 * update sata port state and set device type
12224 */
12225 mutex_enter(&pmportinfo->pmport_mutex);
12226 sata_update_pmport_info(sata_hba_inst, sata_device);
12227 pmportinfo->pmport_state &= ~SATA_STATE_PROBING;
12228
12229 /*
12230 * Sanity check - Port is active? Is the link active?
12231 * Is there any device attached?
12232 */
12233 if ((pmportinfo->pmport_state &
12234 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
12235 (pmportinfo->pmport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
12236 SATA_PORT_DEVLINK_UP) {
12237 /*
12238 * Port in non-usable state or no link active/no device.
12239 * Free info structure if necessary (direct attached drive
12240 * only, for now!
12241 */
12242 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
12243 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
12244 /* Add here differentiation for device attached or not */
12245 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
12246 mutex_exit(&pmportinfo->pmport_mutex);
12247 if (sdinfo != NULL)
12248 kmem_free(sdinfo, sizeof (sata_drive_info_t));
12249 return (SATA_SUCCESS);
12250 }
12251
12252 pmportinfo->pmport_state |= SATA_STATE_READY;
12253 pmportinfo->pmport_dev_type = sata_device->satadev_type;
12254 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
12255
12256 /*
12257 * If we are re-probing the port, there may be
12258 * sata_drive_info structure attached
12259 * (or sata_pm_info, if PMult is supported).
12260 */
12261 if (sata_device->satadev_type == SATA_DTYPE_NONE) {
12262 /*
12263 * There is no device, so remove device info structure,
12264 * if necessary.
12265 */
12266 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
12267 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
12268 if (sdinfo != NULL) {
12269 kmem_free(sdinfo, sizeof (sata_drive_info_t));
12270 sata_log(sata_hba_inst, CE_WARN,
12271 "SATA device detached from port %d:%d",
12272 cport, pmport);
12273 }
12274 mutex_exit(&pmportinfo->pmport_mutex);
12275 return (SATA_SUCCESS);
12276 }
12277
12278 /* this should not be a pmult */
12279 ASSERT(sata_device->satadev_type != SATA_DTYPE_PMULT);
12280 if (sdinfo == NULL) {
12281 /*
12282 * There is some device attached, but there is
12283 * no sata_drive_info structure - allocate one
12284 */
12285 mutex_exit(&pmportinfo->pmport_mutex);
12286 sdinfo = kmem_zalloc(sizeof (sata_drive_info_t),
12287 KM_SLEEP);
12288 mutex_enter(&pmportinfo->pmport_mutex);
12289 /*
12290 * Recheck, that the port state did not change when we
12291 * released mutex.
12292 */
12293 if (pmportinfo->pmport_state & SATA_STATE_READY) {
12294 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = sdinfo;
12295 sdinfo->satadrv_addr = pmportinfo->pmport_addr;
12296 sdinfo->satadrv_addr.qual = SATA_ADDR_DPMPORT;
12297 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12298 sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
12299 } else {
12300 /*
12301 * Port is not in ready state, we
12302 * cannot attach a device.
12303 */
12304 mutex_exit(&pmportinfo->pmport_mutex);
12305 kmem_free(sdinfo, sizeof (sata_drive_info_t));
12306 return (SATA_SUCCESS);
12307 }
12308 /*
12309 * Since we are adding device, presumably new one,
12310 * indicate that it should be initalized,
12311 * as well as some internal framework states).
12312 */
12313 init_device = B_TRUE;
12314 }
12315
12316 pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
12317 sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
12318
12319 mutex_exit(&pmportinfo->pmport_mutex);
12320 /*
12321 * Figure out what kind of device we are really
12322 * dealing with.
12323 */
12324 rval = sata_probe_device(sata_hba_inst, sata_device);
12325
12326 mutex_enter(&pmportinfo->pmport_mutex);
12327 if (rval == SATA_SUCCESS) {
12328 /*
12329 * If we are dealing with the same type of a device as before,
12330 * restore its settings flags.
12331 */
12332 if (osdinfo != NULL &&
12333 sata_device->satadev_type == prev_device_type)
12334 sdinfo->satadrv_settings = prev_device_settings;
12335
12336 mutex_exit(&pmportinfo->pmport_mutex);
12337 /* Set initial device features, if necessary */
12338 if (init_device == B_TRUE) {
12339 rval = sata_initialize_device(sata_hba_inst, sdinfo);
12340 }
12341 if (rval == SATA_SUCCESS)
12342 return (rval);
12343 } else {
12344 /*
12345 * If there was some device info before we probe the device,
12346 * restore previous device setting, so we can retry from scratch
12347 * later. Providing, of course, that device has not disappeared
12348 * during probing process.
12349 */
12350 if (sata_device->satadev_type != SATA_DTYPE_NONE) {
12351 if (osdinfo != NULL) {
12352 pmportinfo->pmport_dev_type = prev_device_type;
12353 sdinfo->satadrv_type = prev_device_type;
12354 sdinfo->satadrv_state = prev_device_state;
12355 }
12356 } else {
12357 /* device is gone */
12358 kmem_free(sdinfo, sizeof (sata_drive_info_t));
12359 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
12360 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
12361 mutex_exit(&pmportinfo->pmport_mutex);
12362 return (SATA_SUCCESS);
12363 }
12364 mutex_exit(&pmportinfo->pmport_mutex);
12365 }
12366
12367 if (flag == SATA_DEV_IDENTIFY_RETRY) {
12368 clock_t cur_time = ddi_get_lbolt();
12369 /*
12370 * A device was not successfully identified or initialized.
12371 * Track retry time for device identification.
12372 */
12373 if ((cur_time - start_time) <
12374 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
12375 /* sleep for a while */
12376 delay(drv_usectohz(SATA_DEV_RETRY_DLY));
12377 goto retry_probe_pmport;
12378 } else {
12379 mutex_enter(&pmportinfo->pmport_mutex);
12380 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL)
12381 SATA_PMPORTINFO_DRV_INFO(pmportinfo)->
12382 satadrv_state = SATA_DSTATE_FAILED;
12383 mutex_exit(&pmportinfo->pmport_mutex);
12384 }
12385 }
12386 return (SATA_SUCCESS);
12387 }
12388
12389 /*
12390 * Allocated related structure for a port multiplier and its device ports
12391 *
12392 * Port multiplier should be ready and probed, and related information like
12393 * the number of the device ports should be store in sata_device_t.
12394 *
12395 * NOTE: No Mutex should be hold.
12396 */
12397 static int
sata_alloc_pmult(sata_hba_inst_t * sata_hba_inst,sata_device_t * sata_device)12398 sata_alloc_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
12399 {
12400 dev_info_t *dip = SATA_DIP(sata_hba_inst);
12401 sata_cport_info_t *cportinfo = NULL;
12402 sata_pmult_info_t *pmultinfo = NULL;
12403 sata_pmport_info_t *pmportinfo = NULL;
12404 sata_device_t sd;
12405 dev_t minor_number;
12406 char name[16];
12407 uint8_t cport = sata_device->satadev_addr.cport;
12408 int rval;
12409 int npmport;
12410
12411 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
12412
12413 /* This function might be called while a port-mult is hot-plugged. */
12414 mutex_enter(&cportinfo->cport_mutex);
12415
12416 /* dev_type's not updated when get called from sata_reprobe_port() */
12417 if (SATA_CPORTINFO_PMULT_INFO(cportinfo) == NULL) {
12418 /* Create a pmult_info structure */
12419 SATA_CPORTINFO_PMULT_INFO(cportinfo) =
12420 kmem_zalloc(sizeof (sata_pmult_info_t), KM_SLEEP);
12421 }
12422 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
12423
12424 pmultinfo->pmult_addr = sata_device->satadev_addr;
12425 pmultinfo->pmult_addr.qual = SATA_ADDR_PMULT;
12426 pmultinfo->pmult_state = SATA_STATE_PROBING;
12427
12428 /*
12429 * Probe the port multiplier with qualifier SATA_ADDR_PMULT_SPEC,
12430 * The HBA driver should initialize and register the port multiplier,
12431 * sata_register_pmult() will fill following fields,
12432 * + sata_pmult_info.pmult_gscr
12433 * + sata_pmult_info.pmult_num_dev_ports
12434 */
12435 sd.satadev_addr = sata_device->satadev_addr;
12436 sd.satadev_addr.qual = SATA_ADDR_PMULT_SPEC;
12437 mutex_exit(&cportinfo->cport_mutex);
12438 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
12439 (SATA_DIP(sata_hba_inst), &sd);
12440 mutex_enter(&cportinfo->cport_mutex);
12441
12442 if (rval != SATA_SUCCESS ||
12443 (sd.satadev_type != SATA_DTYPE_PMULT) ||
12444 !(sd.satadev_state & SATA_DSTATE_PMULT_INIT)) {
12445 SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL;
12446 kmem_free(pmultinfo, sizeof (sata_pmult_info_t));
12447 cportinfo->cport_state = SATA_PSTATE_FAILED;
12448 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
12449 mutex_exit(&cportinfo->cport_mutex);
12450 SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
12451 "sata_alloc_pmult: failed to initialize pmult "
12452 "at port %d.", cport)
12453 return (SATA_FAILURE);
12454 }
12455
12456 /* Initialize pmport_info structure */
12457 for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports;
12458 npmport++) {
12459
12460 /* if everything is allocated, skip */
12461 if (SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) != NULL)
12462 continue;
12463
12464 pmportinfo = kmem_zalloc(sizeof (sata_pmport_info_t), KM_SLEEP);
12465 mutex_init(&pmportinfo->pmport_mutex, NULL, MUTEX_DRIVER, NULL);
12466 mutex_exit(&cportinfo->cport_mutex);
12467
12468 mutex_enter(&pmportinfo->pmport_mutex);
12469 pmportinfo->pmport_addr.cport = cport;
12470 pmportinfo->pmport_addr.pmport = (uint8_t)npmport;
12471 pmportinfo->pmport_addr.qual = SATA_ADDR_PMPORT;
12472 pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
12473 mutex_exit(&pmportinfo->pmport_mutex);
12474
12475 mutex_enter(&cportinfo->cport_mutex);
12476 SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) = pmportinfo;
12477
12478 /* Create an attachment point */
12479 minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip),
12480 cport, (uint8_t)npmport, SATA_ADDR_PMPORT);
12481 (void) sprintf(name, "%d.%d", cport, npmport);
12482
12483 if (ddi_create_minor_node(dip, name, S_IFCHR, minor_number,
12484 DDI_NT_SATA_ATTACHMENT_POINT, 0) != DDI_SUCCESS) {
12485 sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
12486 "cannot create SATA attachment point for "
12487 "port %d:%d", cport, npmport);
12488 }
12489 }
12490
12491 pmultinfo->pmult_state &= ~SATA_STATE_PROBING;
12492 pmultinfo->pmult_state |= (SATA_STATE_PROBED|SATA_STATE_READY);
12493 cportinfo->cport_dev_type = SATA_DTYPE_PMULT;
12494
12495 mutex_exit(&cportinfo->cport_mutex);
12496 return (SATA_SUCCESS);
12497 }
12498
12499 /*
12500 * Free data structures when a port multiplier is removed.
12501 *
12502 * NOTE: No Mutex should be hold.
12503 */
12504 static void
sata_free_pmult(sata_hba_inst_t * sata_hba_inst,sata_device_t * sata_device)12505 sata_free_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
12506 {
12507 sata_cport_info_t *cportinfo;
12508 sata_pmult_info_t *pmultinfo;
12509 sata_pmport_info_t *pmportinfo;
12510 sata_device_t pmport_device;
12511 sata_drive_info_t *sdinfo;
12512 dev_info_t *tdip;
12513 char name[16];
12514 uint8_t cport = sata_device->satadev_addr.cport;
12515 int npmport;
12516
12517 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
12518
12519 /* This function might be called while port-mult is hot plugged. */
12520 mutex_enter(&cportinfo->cport_mutex);
12521
12522 cportinfo->cport_dev_type = SATA_DTYPE_NONE;
12523 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
12524 ASSERT(pmultinfo != NULL);
12525
12526 /* Free pmport_info structure */
12527 for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports;
12528 npmport++) {
12529 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport);
12530 if (pmportinfo == NULL)
12531 continue;
12532 mutex_exit(&cportinfo->cport_mutex);
12533
12534 mutex_enter(&pmportinfo->pmport_mutex);
12535 sdinfo = pmportinfo->pmport_sata_drive;
12536 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
12537 mutex_exit(&pmportinfo->pmport_mutex);
12538
12539 /* Remove attachment point. */
12540 name[0] = '\0';
12541 (void) sprintf(name, "%d.%d", cport, npmport);
12542 ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name);
12543 sata_log(sata_hba_inst, CE_NOTE,
12544 "Remove attachment point of port %d:%d",
12545 cport, npmport);
12546
12547 /*
12548 * Rumove target node
12549 */
12550 bzero(&pmport_device, sizeof (sata_device_t));
12551 pmport_device.satadev_rev = SATA_DEVICE_REV;
12552 pmport_device.satadev_addr.cport = cport;
12553 pmport_device.satadev_addr.pmport = (uint8_t)npmport;
12554 pmport_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
12555
12556 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
12557 &(pmport_device.satadev_addr));
12558 if (tdip != NULL && ndi_devi_offline(tdip,
12559 NDI_DEVI_REMOVE) != NDI_SUCCESS) {
12560 /*
12561 * Problem :
12562 * The target node remained attached.
12563 * This happens when the device file was open
12564 * or a node was waiting for resources.
12565 * Cannot do anything about it.
12566 */
12567 SATA_LOG_D((sata_hba_inst, CE_WARN,
12568 "sata_free_pmult: could not unconfigure device "
12569 "before disconnecting the SATA port %d:%d",
12570 cport, npmport));
12571
12572 /*
12573 * Set DEVICE REMOVED state in the target
12574 * node. It will prevent access to the device
12575 * even when a new device is attached, until
12576 * the old target node is released, removed and
12577 * recreated for a new device.
12578 */
12579 sata_set_device_removed(tdip);
12580
12581 /*
12582 * Instruct event daemon to try the target
12583 * node cleanup later.
12584 */
12585 sata_set_target_node_cleanup(
12586 sata_hba_inst, &(pmport_device.satadev_addr));
12587
12588 }
12589 mutex_enter(&cportinfo->cport_mutex);
12590
12591 /*
12592 * Add here differentiation for device attached or not
12593 */
12594 if (sdinfo != NULL) {
12595 sata_log(sata_hba_inst, CE_WARN,
12596 "SATA device detached from port %d:%d",
12597 cport, npmport);
12598 kmem_free(sdinfo, sizeof (sata_drive_info_t));
12599 }
12600
12601 mutex_destroy(&pmportinfo->pmport_mutex);
12602 kmem_free(pmportinfo, sizeof (sata_pmport_info_t));
12603 }
12604
12605 kmem_free(pmultinfo, sizeof (sata_pmult_info_t));
12606
12607 cportinfo->cport_devp.cport_sata_pmult = NULL;
12608
12609 sata_log(sata_hba_inst, CE_WARN,
12610 "SATA port multiplier detached at port %d", cport);
12611
12612 mutex_exit(&cportinfo->cport_mutex);
12613 }
12614
12615 /*
12616 * Initialize device
12617 * Specified device is initialized to a default state.
12618 *
12619 * Returns SATA_SUCCESS if all device features are set successfully,
12620 * SATA_RETRY if device is accessible but device features were not set
12621 * successfully, and SATA_FAILURE otherwise.
12622 */
12623 static int
sata_initialize_device(sata_hba_inst_t * sata_hba_inst,sata_drive_info_t * sdinfo)12624 sata_initialize_device(sata_hba_inst_t *sata_hba_inst,
12625 sata_drive_info_t *sdinfo)
12626 {
12627 int rval;
12628
12629 sata_save_drive_settings(sdinfo);
12630
12631 sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
12632
12633 sata_init_write_cache_mode(sdinfo);
12634
12635 rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0);
12636
12637 /* Determine current data transfer mode */
12638 if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) {
12639 sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
12640 } else if ((sdinfo->satadrv_id.ai_validinfo &
12641 SATA_VALIDINFO_88) != 0 &&
12642 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) {
12643 sdinfo->satadrv_settings |= SATA_DEV_DMA;
12644 } else if ((sdinfo->satadrv_id.ai_dworddma &
12645 SATA_MDMA_SEL_MASK) != 0) {
12646 sdinfo->satadrv_settings |= SATA_DEV_DMA;
12647 } else
12648 /* DMA supported, not no DMA transfer mode is selected !? */
12649 sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
12650
12651 if ((sdinfo->satadrv_id.ai_cmdset83 & 0x20) &&
12652 (sdinfo->satadrv_id.ai_features86 & 0x20))
12653 sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
12654 else
12655 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
12656
12657 return (rval);
12658 }
12659
12660
12661 /*
12662 * Initialize write cache mode.
12663 *
12664 * The default write cache setting for SATA HDD is provided by sata_write_cache
12665 * static variable. ATAPI CD/DVDs devices have write cache default is
12666 * determined by sata_atapicdvd_write_cache static variable.
12667 * ATAPI tape devices have write cache default is determined by
12668 * sata_atapitape_write_cache static variable.
12669 * ATAPI disk devices have write cache default is determined by
12670 * sata_atapidisk_write_cache static variable.
12671 * 1 - enable
12672 * 0 - disable
12673 * any other value - current drive setting
12674 *
12675 * Although there is not reason to disable write cache on CD/DVD devices,
12676 * tape devices and ATAPI disk devices, the default setting control is provided
12677 * for the maximun flexibility.
12678 *
12679 * In the future, it may be overridden by the
12680 * disk-write-cache-enable property setting, if it is defined.
12681 * Returns SATA_SUCCESS if all device features are set successfully,
12682 * SATA_FAILURE otherwise.
12683 */
12684 static void
sata_init_write_cache_mode(sata_drive_info_t * sdinfo)12685 sata_init_write_cache_mode(sata_drive_info_t *sdinfo)
12686 {
12687 switch (sdinfo->satadrv_type) {
12688 case SATA_DTYPE_ATADISK:
12689 if (sata_write_cache == 1)
12690 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12691 else if (sata_write_cache == 0)
12692 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12693 /*
12694 * When sata_write_cache value is not 0 or 1,
12695 * a current setting of the drive's write cache is used.
12696 */
12697 break;
12698 case SATA_DTYPE_ATAPICD:
12699 if (sata_atapicdvd_write_cache == 1)
12700 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12701 else if (sata_atapicdvd_write_cache == 0)
12702 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12703 /*
12704 * When sata_atapicdvd_write_cache value is not 0 or 1,
12705 * a current setting of the drive's write cache is used.
12706 */
12707 break;
12708 case SATA_DTYPE_ATAPITAPE:
12709 if (sata_atapitape_write_cache == 1)
12710 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12711 else if (sata_atapitape_write_cache == 0)
12712 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12713 /*
12714 * When sata_atapitape_write_cache value is not 0 or 1,
12715 * a current setting of the drive's write cache is used.
12716 */
12717 break;
12718 case SATA_DTYPE_ATAPIDISK:
12719 if (sata_atapidisk_write_cache == 1)
12720 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12721 else if (sata_atapidisk_write_cache == 0)
12722 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12723 /*
12724 * When sata_atapidisk_write_cache value is not 0 or 1,
12725 * a current setting of the drive's write cache is used.
12726 */
12727 break;
12728 }
12729 }
12730
12731
12732 /*
12733 * Validate sata address.
12734 * Specified cport, pmport and qualifier has to match
12735 * passed sata_scsi configuration info.
12736 * The presence of an attached device is not verified.
12737 *
12738 * Returns 0 when address is valid, -1 otherwise.
12739 */
12740 static int
sata_validate_sata_address(sata_hba_inst_t * sata_hba_inst,int cport,int pmport,int qual)12741 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport,
12742 int pmport, int qual)
12743 {
12744 if (qual == SATA_ADDR_DCPORT && pmport != 0)
12745 goto invalid_address;
12746 if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
12747 goto invalid_address;
12748 if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) &&
12749 ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) ||
12750 (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) ||
12751 (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport))))
12752 goto invalid_address;
12753
12754 return (0);
12755
12756 invalid_address:
12757 return (-1);
12758
12759 }
12760
12761 /*
12762 * Validate scsi address
12763 * SCSI target address is translated into SATA cport/pmport and compared
12764 * with a controller port/device configuration. LUN has to be 0.
12765 * Returns 0 if a scsi target refers to an attached device,
12766 * returns 1 if address is valid but no valid device is attached,
12767 * returns 2 if address is valid but device type is unknown (not valid device),
12768 * returns -1 if bad address or device is of an unsupported type.
12769 * Upon return sata_device argument is set.
12770 *
12771 * Port multiplier is supported now.
12772 */
12773 static int
sata_validate_scsi_address(sata_hba_inst_t * sata_hba_inst,struct scsi_address * ap,sata_device_t * sata_device)12774 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst,
12775 struct scsi_address *ap, sata_device_t *sata_device)
12776 {
12777 int cport, pmport, qual, rval;
12778
12779 rval = -1; /* Invalid address */
12780 if (ap->a_lun != 0)
12781 goto out;
12782
12783 qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
12784 cport = SCSI_TO_SATA_CPORT(ap->a_target);
12785 pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
12786
12787 if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT)
12788 goto out;
12789
12790 if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) ==
12791 0) {
12792
12793 sata_cport_info_t *cportinfo;
12794 sata_pmult_info_t *pmultinfo;
12795 sata_drive_info_t *sdinfo = NULL;
12796
12797 sata_device->satadev_addr.qual = qual;
12798 sata_device->satadev_addr.cport = cport;
12799 sata_device->satadev_addr.pmport = pmport;
12800 sata_device->satadev_rev = SATA_DEVICE_REV_1;
12801
12802 rval = 1; /* Valid sata address */
12803
12804 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
12805 if (qual == SATA_ADDR_DCPORT) {
12806 if (cportinfo == NULL ||
12807 cportinfo->cport_dev_type == SATA_DTYPE_NONE)
12808 goto out;
12809
12810 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
12811 if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN &&
12812 sdinfo != NULL) {
12813 rval = 2;
12814 goto out;
12815 }
12816
12817 if ((cportinfo->cport_dev_type &
12818 SATA_VALID_DEV_TYPE) == 0) {
12819 rval = -1;
12820 goto out;
12821 }
12822
12823 } else if (qual == SATA_ADDR_DPMPORT) {
12824 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
12825 if (pmultinfo == NULL) {
12826 rval = -1;
12827 goto out;
12828 }
12829 if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) ==
12830 NULL ||
12831 SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12832 pmport) == SATA_DTYPE_NONE)
12833 goto out;
12834
12835 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport,
12836 pmport);
12837 if (SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12838 pmport) == SATA_DTYPE_UNKNOWN && sdinfo != NULL) {
12839 rval = 2;
12840 goto out;
12841 }
12842
12843 if ((SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12844 pmport) & SATA_VALID_DEV_TYPE) == 0) {
12845 rval = -1;
12846 goto out;
12847 }
12848
12849 } else {
12850 rval = -1;
12851 goto out;
12852 }
12853 if ((sdinfo == NULL) ||
12854 (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0)
12855 goto out;
12856
12857 sata_device->satadev_type = sdinfo->satadrv_type;
12858
12859 return (0);
12860 }
12861 out:
12862 if (rval > 0) {
12863 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
12864 "sata_validate_scsi_address: no valid target %x lun %x",
12865 ap->a_target, ap->a_lun);
12866 }
12867 return (rval);
12868 }
12869
12870 /*
12871 * Find dip corresponding to passed device number
12872 *
12873 * Returns NULL if invalid device number is passed or device cannot be found,
12874 * Returns dip is device is found.
12875 */
12876 static dev_info_t *
sata_devt_to_devinfo(dev_t dev)12877 sata_devt_to_devinfo(dev_t dev)
12878 {
12879 dev_info_t *dip;
12880 #ifndef __lock_lint
12881 struct devnames *dnp;
12882 major_t major = getmajor(dev);
12883 int instance = SATA_MINOR2INSTANCE(getminor(dev));
12884
12885 if (major >= devcnt)
12886 return (NULL);
12887
12888 dnp = &devnamesp[major];
12889 LOCK_DEV_OPS(&(dnp->dn_lock));
12890 dip = dnp->dn_head;
12891 while (dip && (ddi_get_instance(dip) != instance)) {
12892 dip = ddi_get_next(dip);
12893 }
12894 UNLOCK_DEV_OPS(&(dnp->dn_lock));
12895 #endif
12896
12897 return (dip);
12898 }
12899
12900
12901 /*
12902 * Probe device.
12903 * This function issues Identify Device command and initializes local
12904 * sata_drive_info structure if the device can be identified.
12905 * The device type is determined by examining Identify Device
12906 * command response.
12907 * If the sata_hba_inst has linked drive info structure for this
12908 * device address, the Identify Device data is stored into sata_drive_info
12909 * structure linked to the port info structure.
12910 *
12911 * sata_device has to refer to the valid sata port(s) for HBA described
12912 * by sata_hba_inst structure.
12913 *
12914 * Returns:
12915 * SATA_SUCCESS if device type was successfully probed and port-linked
12916 * drive info structure was updated;
12917 * SATA_FAILURE if there is no device, or device was not probed
12918 * successully;
12919 * SATA_RETRY if device probe can be retried later.
12920 * If a device cannot be identified, sata_device's dev_state and dev_type
12921 * fields are set to unknown.
12922 * There are no retries in this function. Any retries should be managed by
12923 * the caller.
12924 */
12925
12926
12927 static int
sata_probe_device(sata_hba_inst_t * sata_hba_inst,sata_device_t * sata_device)12928 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
12929 {
12930 sata_pmport_info_t *pmportinfo = NULL;
12931 sata_drive_info_t *sdinfo;
12932 sata_drive_info_t new_sdinfo; /* local drive info struct */
12933 int rval;
12934
12935 ASSERT((SATA_CPORT_STATE(sata_hba_inst,
12936 sata_device->satadev_addr.cport) &
12937 (SATA_STATE_PROBED | SATA_STATE_READY)) != 0);
12938
12939 sata_device->satadev_type = SATA_DTYPE_NONE;
12940
12941 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12942 sata_device->satadev_addr.cport)));
12943
12944 if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT) {
12945 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
12946 sata_device->satadev_addr.cport,
12947 sata_device->satadev_addr.pmport);
12948 ASSERT(pmportinfo != NULL);
12949 }
12950
12951 /* Get pointer to port-linked sata device info structure */
12952 sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12953 if (sdinfo != NULL) {
12954 sdinfo->satadrv_state &=
12955 ~(SATA_STATE_PROBED | SATA_STATE_READY);
12956 sdinfo->satadrv_state |= SATA_STATE_PROBING;
12957 } else {
12958 /* No device to probe */
12959 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12960 sata_device->satadev_addr.cport)));
12961 sata_device->satadev_type = SATA_DTYPE_NONE;
12962 sata_device->satadev_state = SATA_STATE_UNKNOWN;
12963 return (SATA_FAILURE);
12964 }
12965 /*
12966 * Need to issue both types of identify device command and
12967 * determine device type by examining retreived data/status.
12968 * First, ATA Identify Device.
12969 */
12970 bzero(&new_sdinfo, sizeof (sata_drive_info_t));
12971 new_sdinfo.satadrv_addr = sata_device->satadev_addr;
12972 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12973 sata_device->satadev_addr.cport)));
12974 new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
12975 rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
12976 if (rval == SATA_RETRY) {
12977 /* We may try to check for ATAPI device */
12978 if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) {
12979 /*
12980 * HBA supports ATAPI - try to issue Identify Packet
12981 * Device command.
12982 */
12983 new_sdinfo.satadrv_type = SATA_DTYPE_ATAPI;
12984 rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
12985 }
12986 }
12987 if (rval == SATA_SUCCESS) {
12988 /*
12989 * Got something responding positively to ATA Identify Device
12990 * or to Identify Packet Device cmd.
12991 * Save last used device type.
12992 */
12993 sata_device->satadev_type = new_sdinfo.satadrv_type;
12994
12995 /* save device info, if possible */
12996 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12997 sata_device->satadev_addr.cport)));
12998 sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12999 if (sdinfo == NULL) {
13000 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13001 sata_device->satadev_addr.cport)));
13002 return (SATA_FAILURE);
13003 }
13004 /*
13005 * Copy drive info into the port-linked drive info structure.
13006 */
13007 *sdinfo = new_sdinfo;
13008 sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
13009 sdinfo->satadrv_state |= SATA_STATE_PROBED;
13010 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
13011 SATA_CPORT_DEV_TYPE(sata_hba_inst,
13012 sata_device->satadev_addr.cport) =
13013 sdinfo->satadrv_type;
13014 else { /* SATA_ADDR_DPMPORT */
13015 mutex_enter(&pmportinfo->pmport_mutex);
13016 SATA_PMPORT_DEV_TYPE(sata_hba_inst,
13017 sata_device->satadev_addr.cport,
13018 sata_device->satadev_addr.pmport) =
13019 sdinfo->satadrv_type;
13020 mutex_exit(&pmportinfo->pmport_mutex);
13021 }
13022 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13023 sata_device->satadev_addr.cport)));
13024 return (SATA_SUCCESS);
13025 }
13026
13027 /*
13028 * It may be SATA_RETRY or SATA_FAILURE return.
13029 * Looks like we cannot determine the device type at this time.
13030 */
13031 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13032 sata_device->satadev_addr.cport)));
13033 sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
13034 if (sdinfo != NULL) {
13035 sata_device->satadev_type = SATA_DTYPE_UNKNOWN;
13036 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
13037 sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
13038 sdinfo->satadrv_state |= SATA_STATE_PROBED;
13039 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
13040 SATA_CPORT_DEV_TYPE(sata_hba_inst,
13041 sata_device->satadev_addr.cport) =
13042 SATA_DTYPE_UNKNOWN;
13043 else {
13044 /* SATA_ADDR_DPMPORT */
13045 mutex_enter(&pmportinfo->pmport_mutex);
13046 if ((SATA_PMULT_INFO(sata_hba_inst,
13047 sata_device->satadev_addr.cport) != NULL) &&
13048 (SATA_PMPORT_INFO(sata_hba_inst,
13049 sata_device->satadev_addr.cport,
13050 sata_device->satadev_addr.pmport) != NULL))
13051 SATA_PMPORT_DEV_TYPE(sata_hba_inst,
13052 sata_device->satadev_addr.cport,
13053 sata_device->satadev_addr.pmport) =
13054 SATA_DTYPE_UNKNOWN;
13055 mutex_exit(&pmportinfo->pmport_mutex);
13056 }
13057 }
13058 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13059 sata_device->satadev_addr.cport)));
13060 return (rval);
13061 }
13062
13063
13064 /*
13065 * Get pointer to sata_drive_info structure.
13066 *
13067 * The sata_device has to contain address (cport, pmport and qualifier) for
13068 * specified sata_scsi structure.
13069 *
13070 * Returns NULL if device address is not valid for this HBA configuration.
13071 * Otherwise, returns a pointer to sata_drive_info structure.
13072 *
13073 * This function should be called with a port mutex held.
13074 */
13075 static sata_drive_info_t *
sata_get_device_info(sata_hba_inst_t * sata_hba_inst,sata_device_t * sata_device)13076 sata_get_device_info(sata_hba_inst_t *sata_hba_inst,
13077 sata_device_t *sata_device)
13078 {
13079 uint8_t cport = sata_device->satadev_addr.cport;
13080 uint8_t pmport = sata_device->satadev_addr.pmport;
13081 uint8_t qual = sata_device->satadev_addr.qual;
13082
13083 if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
13084 return (NULL);
13085
13086 if (!(SATA_CPORT_STATE(sata_hba_inst, cport) &
13087 (SATA_STATE_PROBED | SATA_STATE_READY)))
13088 /* Port not probed yet */
13089 return (NULL);
13090
13091 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE)
13092 return (NULL);
13093
13094 if (qual == SATA_ADDR_DCPORT) {
13095 /* Request for a device on a controller port */
13096 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
13097 SATA_DTYPE_PMULT)
13098 /* Port multiplier attached */
13099 return (NULL);
13100 return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport));
13101 }
13102 if (qual == SATA_ADDR_DPMPORT) {
13103 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
13104 SATA_DTYPE_PMULT)
13105 return (NULL);
13106
13107 if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport))
13108 return (NULL);
13109
13110 if (!(SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) &
13111 (SATA_STATE_PROBED | SATA_STATE_READY)))
13112 /* Port multiplier port not probed yet */
13113 return (NULL);
13114
13115 return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport));
13116 }
13117
13118 /* we should not get here */
13119 return (NULL);
13120 }
13121
13122
13123 /*
13124 * sata_identify_device.
13125 * Send Identify Device command to SATA HBA driver.
13126 * If command executes successfully, update sata_drive_info structure pointed
13127 * to by sdinfo argument, including Identify Device data.
13128 * If command fails, invalidate data in sata_drive_info.
13129 *
13130 * Cannot be called from interrupt level.
13131 *
13132 * Returns:
13133 * SATA_SUCCESS if the device was identified as a supported device,
13134 * SATA_RETRY if the device was not identified but could be retried,
13135 * SATA_FAILURE if the device was not identified and identify attempt
13136 * should not be retried.
13137 */
13138 static int
sata_identify_device(sata_hba_inst_t * sata_hba_inst,sata_drive_info_t * sdinfo)13139 sata_identify_device(sata_hba_inst_t *sata_hba_inst,
13140 sata_drive_info_t *sdinfo)
13141 {
13142 uint16_t cfg_word;
13143 int rval;
13144
13145 /* fetch device identify data */
13146 if ((rval = sata_fetch_device_identify_data(sata_hba_inst,
13147 sdinfo)) != SATA_SUCCESS)
13148 goto fail_unknown;
13149
13150 cfg_word = sdinfo->satadrv_id.ai_config;
13151
13152 /* Set the correct device type */
13153 if ((cfg_word & SATA_ATA_TYPE_MASK) == SATA_ATA_TYPE) {
13154 sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
13155 } else if (cfg_word == SATA_CFA_TYPE) {
13156 /* It's a Compact Flash media via CF-to-SATA HDD adapter */
13157 sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
13158 } else if ((cfg_word & SATA_ATAPI_TYPE_MASK) == SATA_ATAPI_TYPE) {
13159 switch (cfg_word & SATA_ATAPI_ID_DEV_TYPE) {
13160 case SATA_ATAPI_CDROM_DEV:
13161 sdinfo->satadrv_type = SATA_DTYPE_ATAPICD;
13162 break;
13163 case SATA_ATAPI_SQACC_DEV:
13164 sdinfo->satadrv_type = SATA_DTYPE_ATAPITAPE;
13165 break;
13166 case SATA_ATAPI_DIRACC_DEV:
13167 sdinfo->satadrv_type = SATA_DTYPE_ATAPIDISK;
13168 break;
13169 case SATA_ATAPI_PROC_DEV:
13170 sdinfo->satadrv_type = SATA_DTYPE_ATAPIPROC;
13171 break;
13172 default:
13173 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
13174 }
13175 } else {
13176 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
13177 }
13178
13179 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
13180 if (sdinfo->satadrv_capacity == 0) {
13181 /* Non-LBA disk. Too bad... */
13182 sata_log(sata_hba_inst, CE_WARN,
13183 "SATA disk device at port %d does not support LBA",
13184 sdinfo->satadrv_addr.cport);
13185 rval = SATA_FAILURE;
13186 goto fail_unknown;
13187 }
13188 }
13189 #if 0
13190 /* Left for historical reason */
13191 /*
13192 * Some initial version of SATA spec indicated that at least
13193 * UDMA mode 4 has to be supported. It is not metioned in
13194 * SerialATA 2.6, so this restriction is removed.
13195 */
13196 /* Check for Ultra DMA modes 6 through 0 being supported */
13197 for (i = 6; i >= 0; --i) {
13198 if (sdinfo->satadrv_id.ai_ultradma & (1 << i))
13199 break;
13200 }
13201
13202 /*
13203 * At least UDMA 4 mode has to be supported. If mode 4 or
13204 * higher are not supported by the device, fail this
13205 * device.
13206 */
13207 if (i < 4) {
13208 /* No required Ultra DMA mode supported */
13209 sata_log(sata_hba_inst, CE_WARN,
13210 "SATA disk device at port %d does not support UDMA "
13211 "mode 4 or higher", sdinfo->satadrv_addr.cport);
13212 SATA_LOG_D((sata_hba_inst, CE_WARN,
13213 "mode 4 or higher required, %d supported", i));
13214 rval = SATA_FAILURE;
13215 goto fail_unknown;
13216 }
13217 #endif
13218
13219 /*
13220 * For Disk devices, if it doesn't support UDMA mode, we would
13221 * like to return failure directly.
13222 */
13223 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
13224 !((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
13225 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0)) {
13226 sata_log(sata_hba_inst, CE_WARN,
13227 "SATA disk device at port %d does not support UDMA",
13228 sdinfo->satadrv_addr.cport);
13229 rval = SATA_FAILURE;
13230 goto fail_unknown;
13231 }
13232
13233 return (SATA_SUCCESS);
13234
13235 fail_unknown:
13236 /* Invalidate sata_drive_info ? */
13237 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
13238 sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
13239 return (rval);
13240 }
13241
13242 /*
13243 * Log/display device information
13244 */
13245 static void
sata_show_drive_info(sata_hba_inst_t * sata_hba_inst,sata_drive_info_t * sdinfo)13246 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst,
13247 sata_drive_info_t *sdinfo)
13248 {
13249 int valid_version = 0;
13250 char msg_buf[MAXPATHLEN];
13251 int i;
13252
13253 /* Show HBA path */
13254 (void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf);
13255
13256 cmn_err(CE_CONT, "?%s :\n", msg_buf);
13257
13258 switch (sdinfo->satadrv_type) {
13259 case SATA_DTYPE_ATADISK:
13260 (void) sprintf(msg_buf, "SATA disk device at");
13261 break;
13262
13263 case SATA_DTYPE_ATAPICD:
13264 (void) sprintf(msg_buf, "SATA CD/DVD (ATAPI) device at");
13265 break;
13266
13267 case SATA_DTYPE_ATAPITAPE:
13268 (void) sprintf(msg_buf, "SATA tape (ATAPI) device at");
13269 break;
13270
13271 case SATA_DTYPE_ATAPIDISK:
13272 (void) sprintf(msg_buf, "SATA disk (ATAPI) device at");
13273 break;
13274
13275 case SATA_DTYPE_ATAPIPROC:
13276 (void) sprintf(msg_buf, "SATA processor (ATAPI) device at");
13277 break;
13278
13279 case SATA_DTYPE_UNKNOWN:
13280 (void) sprintf(msg_buf,
13281 "Unsupported SATA device type (cfg 0x%x) at ",
13282 sdinfo->satadrv_id.ai_config);
13283 break;
13284 }
13285
13286 if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT)
13287 cmn_err(CE_CONT, "?\t%s port %d\n",
13288 msg_buf, sdinfo->satadrv_addr.cport);
13289 else
13290 cmn_err(CE_CONT, "?\t%s port %d:%d\n",
13291 msg_buf, sdinfo->satadrv_addr.cport,
13292 sdinfo->satadrv_addr.pmport);
13293
13294 bcopy(&sdinfo->satadrv_id.ai_model, msg_buf,
13295 sizeof (sdinfo->satadrv_id.ai_model));
13296 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model));
13297 msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
13298 cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf);
13299
13300 bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf,
13301 sizeof (sdinfo->satadrv_id.ai_fw));
13302 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw));
13303 msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
13304 cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf);
13305
13306 bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf,
13307 sizeof (sdinfo->satadrv_id.ai_drvser));
13308 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser));
13309 msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
13310 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
13311 cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
13312 } else {
13313 /*
13314 * Some drives do not implement serial number and may
13315 * violate the spec by providing spaces rather than zeros
13316 * in serial number field. Scan the buffer to detect it.
13317 */
13318 for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) {
13319 if (msg_buf[i] != '\0' && msg_buf[i] != ' ')
13320 break;
13321 }
13322 if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) {
13323 cmn_err(CE_CONT, "?\tserial number - none\n");
13324 } else {
13325 cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
13326 }
13327 }
13328
13329 #ifdef SATA_DEBUG
13330 if (sdinfo->satadrv_id.ai_majorversion != 0 &&
13331 sdinfo->satadrv_id.ai_majorversion != 0xffff) {
13332 int i;
13333 for (i = 14; i >= 2; i--) {
13334 if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) {
13335 valid_version = i;
13336 break;
13337 }
13338 }
13339 cmn_err(CE_CONT,
13340 "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
13341 valid_version,
13342 sdinfo->satadrv_id.ai_majorversion,
13343 sdinfo->satadrv_id.ai_minorversion);
13344 }
13345 #endif
13346 /* Log some info */
13347 cmn_err(CE_CONT, "?\tsupported features:\n");
13348 msg_buf[0] = '\0';
13349 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
13350 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48)
13351 (void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN);
13352 else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28)
13353 (void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN);
13354 }
13355 if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA)
13356 (void) strlcat(msg_buf, "DMA", MAXPATHLEN);
13357 if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ)
13358 (void) strlcat(msg_buf, ", Native Command Queueing",
13359 MAXPATHLEN);
13360 if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)
13361 (void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN);
13362 if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) &&
13363 (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED))
13364 (void) strlcat(msg_buf, ", SMART", MAXPATHLEN);
13365 if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) &&
13366 (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED))
13367 (void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN);
13368 cmn_err(CE_CONT, "?\t %s\n", msg_buf);
13369 if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA3)
13370 cmn_err(CE_CONT, "?\tSATA Gen3 signaling speed (6.0Gbps)\n");
13371 else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2)
13372 cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n");
13373 else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1)
13374 cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n");
13375 if (sdinfo->satadrv_features_support &
13376 (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) {
13377 msg_buf[0] = '\0';
13378 (void) snprintf(msg_buf, MAXPATHLEN,
13379 "Supported queue depth %d",
13380 sdinfo->satadrv_queue_depth);
13381 if (!(sata_func_enable &
13382 (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ)))
13383 (void) strlcat(msg_buf,
13384 " - queueing disabled globally", MAXPATHLEN);
13385 else if (sdinfo->satadrv_queue_depth >
13386 sdinfo->satadrv_max_queue_depth) {
13387 (void) snprintf(&msg_buf[strlen(msg_buf)],
13388 MAXPATHLEN - strlen(msg_buf), ", limited to %d",
13389 (int)sdinfo->satadrv_max_queue_depth);
13390 }
13391 cmn_err(CE_CONT, "?\t%s\n", msg_buf);
13392 }
13393
13394 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
13395 (void) sprintf(msg_buf, "\tcapacity = %lu sectors\n",
13396 sdinfo->satadrv_capacity);
13397 cmn_err(CE_CONT, "?%s", msg_buf);
13398 }
13399 }
13400
13401 /*
13402 * Log/display port multiplier information
13403 * No Mutex should be hold.
13404 */
13405 static void
sata_show_pmult_info(sata_hba_inst_t * sata_hba_inst,sata_device_t * sata_device)13406 sata_show_pmult_info(sata_hba_inst_t *sata_hba_inst,
13407 sata_device_t *sata_device)
13408 {
13409 _NOTE(ARGUNUSED(sata_hba_inst))
13410
13411 int cport = sata_device->satadev_addr.cport;
13412 sata_pmult_info_t *pmultinfo;
13413 char msg_buf[MAXPATHLEN];
13414 uint32_t gscr0, gscr1, gscr2, gscr64;
13415
13416 mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
13417 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
13418 if (pmultinfo == NULL) {
13419 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
13420 return;
13421 }
13422
13423 gscr0 = pmultinfo->pmult_gscr.gscr0;
13424 gscr1 = pmultinfo->pmult_gscr.gscr1;
13425 gscr2 = pmultinfo->pmult_gscr.gscr2;
13426 gscr64 = pmultinfo->pmult_gscr.gscr64;
13427 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
13428
13429 cmn_err(CE_CONT, "?Port Multiplier %d device-ports found at port %d",
13430 sata_device->satadev_add_info, sata_device->satadev_addr.cport);
13431
13432 (void) sprintf(msg_buf, "\tVendor_ID 0x%04x, Module_ID 0x%04x",
13433 gscr0 & 0xffff, (gscr0 >> 16) & 0xffff);
13434 cmn_err(CE_CONT, "?%s", msg_buf);
13435
13436 (void) strcpy(msg_buf, "\tSupport SATA PMP Spec ");
13437 if (gscr1 & (1 << 3))
13438 (void) strlcat(msg_buf, "1.2", MAXPATHLEN);
13439 else if (gscr1 & (1 << 2))
13440 (void) strlcat(msg_buf, "1.1", MAXPATHLEN);
13441 else if (gscr1 & (1 << 1))
13442 (void) strlcat(msg_buf, "1.0", MAXPATHLEN);
13443 else
13444 (void) strlcat(msg_buf, "unknown", MAXPATHLEN);
13445 cmn_err(CE_CONT, "?%s", msg_buf);
13446
13447 (void) strcpy(msg_buf, "\tSupport ");
13448 if (gscr64 & (1 << 3))
13449 (void) strlcat(msg_buf, "Asy-Notif, ",
13450 MAXPATHLEN);
13451 if (gscr64 & (1 << 2))
13452 (void) strlcat(msg_buf, "Dyn-SSC, ", MAXPATHLEN);
13453 if (gscr64 & (1 << 1))
13454 (void) strlcat(msg_buf, "Iss-PMREQ, ", MAXPATHLEN);
13455 if (gscr64 & (1 << 0))
13456 (void) strlcat(msg_buf, "BIST", MAXPATHLEN);
13457 if ((gscr64 & 0xf) == 0)
13458 (void) strlcat(msg_buf, "nothing", MAXPATHLEN);
13459 cmn_err(CE_CONT, "?%s", msg_buf);
13460
13461 (void) sprintf(msg_buf, "\tNumber of exposed device fan-out ports: %d",
13462 gscr2 & SATA_PMULT_PORTNUM_MASK);
13463 cmn_err(CE_CONT, "?%s", msg_buf);
13464 }
13465
13466 /*
13467 * sata_save_drive_settings extracts current setting of the device and stores
13468 * it for future reference, in case the device setup would need to be restored
13469 * after the device reset.
13470 *
13471 * For all devices read ahead and write cache settings are saved, if the
13472 * device supports these features at all.
13473 * For ATAPI devices the Removable Media Status Notification setting is saved.
13474 */
13475 static void
sata_save_drive_settings(sata_drive_info_t * sdinfo)13476 sata_save_drive_settings(sata_drive_info_t *sdinfo)
13477 {
13478 if (SATA_READ_AHEAD_SUPPORTED(sdinfo->satadrv_id) ||
13479 SATA_WRITE_CACHE_SUPPORTED(sdinfo->satadrv_id)) {
13480
13481 /* Current setting of Read Ahead (and Read Cache) */
13482 if (SATA_READ_AHEAD_ENABLED(sdinfo->satadrv_id))
13483 sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
13484 else
13485 sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD;
13486
13487 /* Current setting of Write Cache */
13488 if (SATA_WRITE_CACHE_ENABLED(sdinfo->satadrv_id))
13489 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
13490 else
13491 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
13492 }
13493
13494 if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
13495 if (SATA_RM_NOTIFIC_SUPPORTED(sdinfo->satadrv_id))
13496 sdinfo->satadrv_settings |= SATA_DEV_RMSN;
13497 else
13498 sdinfo->satadrv_settings &= ~SATA_DEV_RMSN;
13499 }
13500 }
13501
13502
13503 /*
13504 * sata_check_capacity function determines a disk capacity
13505 * and addressing mode (LBA28/LBA48) by examining a disk identify device data.
13506 *
13507 * NOTE: CHS mode is not supported! If a device does not support LBA,
13508 * this function is not called.
13509 *
13510 * Returns device capacity in number of blocks, i.e. largest addressable LBA+1
13511 */
13512 static uint64_t
sata_check_capacity(sata_drive_info_t * sdinfo)13513 sata_check_capacity(sata_drive_info_t *sdinfo)
13514 {
13515 uint64_t capacity = 0;
13516 int i;
13517
13518 if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK ||
13519 (sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT) == 0)
13520 /* Capacity valid only for LBA-addressable disk devices */
13521 return (0);
13522
13523 if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) &&
13524 (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) &&
13525 (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) {
13526 /* LBA48 mode supported and enabled */
13527 sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 |
13528 SATA_DEV_F_LBA28;
13529 for (i = 3; i >= 0; --i) {
13530 capacity <<= 16;
13531 capacity += sdinfo->satadrv_id.ai_addrsecxt[i];
13532 }
13533 } else {
13534 capacity = sdinfo->satadrv_id.ai_addrsec[1];
13535 capacity <<= 16;
13536 capacity += sdinfo->satadrv_id.ai_addrsec[0];
13537 if (capacity >= 0x1000000)
13538 /* LBA28 mode */
13539 sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28;
13540 }
13541 return (capacity);
13542 }
13543
13544
13545 /*
13546 * Allocate consistent buffer for DMA transfer
13547 *
13548 * Cannot be called from interrupt level or with mutex held - it may sleep.
13549 *
13550 * Returns pointer to allocated buffer structure, or NULL if allocation failed.
13551 */
13552 static struct buf *
sata_alloc_local_buffer(sata_pkt_txlate_t * spx,size_t len)13553 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, size_t len)
13554 {
13555 struct scsi_address ap;
13556 struct buf *bp;
13557 ddi_dma_attr_t cur_dma_attr;
13558
13559 ASSERT(spx->txlt_sata_pkt != NULL);
13560 ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran;
13561 ap.a_target = SATA_TO_SCSI_TARGET(
13562 spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport,
13563 spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport,
13564 spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual);
13565 ap.a_lun = 0;
13566
13567 bp = scsi_alloc_consistent_buf(&ap, NULL, len,
13568 B_READ, SLEEP_FUNC, NULL);
13569
13570 if (bp != NULL) {
13571 /* Allocate DMA resources for this buffer */
13572 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
13573 /*
13574 * We use a local version of the dma_attr, to account
13575 * for a device addressing limitations.
13576 * sata_adjust_dma_attr() will handle sdinfo == NULL which
13577 * will cause dma attributes to be adjusted to a lowest
13578 * acceptable level.
13579 */
13580 sata_adjust_dma_attr(NULL,
13581 SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
13582
13583 if (sata_dma_buf_setup(spx, PKT_CONSISTENT,
13584 SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) {
13585 scsi_free_consistent_buf(bp);
13586 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
13587 bp = NULL;
13588 }
13589 }
13590 return (bp);
13591 }
13592
13593 /*
13594 * Release local buffer (consistent buffer for DMA transfer) allocated
13595 * via sata_alloc_local_buffer().
13596 */
13597 static void
sata_free_local_buffer(sata_pkt_txlate_t * spx)13598 sata_free_local_buffer(sata_pkt_txlate_t *spx)
13599 {
13600 ASSERT(spx->txlt_sata_pkt != NULL);
13601 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL);
13602
13603 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0;
13604 spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL;
13605
13606 sata_common_free_dma_rsrcs(spx);
13607
13608 /* Free buffer */
13609 scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp);
13610 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
13611 }
13612
13613 /*
13614 * Allocate sata_pkt
13615 * Pkt structure version and embedded strcutures version are initialized.
13616 * sata_pkt and sata_pkt_txlate structures are cross-linked.
13617 *
13618 * Since this may be called in interrupt context by sata_scsi_init_pkt,
13619 * callback argument determines if it can sleep or not.
13620 * Hence, it should not be called from interrupt context.
13621 *
13622 * If successful, non-NULL pointer to a sata pkt is returned.
13623 * Upon failure, NULL pointer is returned.
13624 */
13625 static sata_pkt_t *
sata_pkt_alloc(sata_pkt_txlate_t * spx,int (* callback)(caddr_t))13626 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t))
13627 {
13628 sata_pkt_t *spkt;
13629 int kmsflag;
13630
13631 kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
13632 spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag);
13633 if (spkt == NULL) {
13634 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13635 "sata_pkt_alloc: failed"));
13636 return (NULL);
13637 }
13638 spkt->satapkt_rev = SATA_PKT_REV;
13639 spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV;
13640 spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
13641 spkt->satapkt_framework_private = spx;
13642 spx->txlt_sata_pkt = spkt;
13643 return (spkt);
13644 }
13645
13646 /*
13647 * Free sata pkt allocated via sata_pkt_alloc()
13648 */
13649 static void
sata_pkt_free(sata_pkt_txlate_t * spx)13650 sata_pkt_free(sata_pkt_txlate_t *spx)
13651 {
13652 ASSERT(spx->txlt_sata_pkt != NULL);
13653 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL);
13654 kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t));
13655 spx->txlt_sata_pkt = NULL;
13656 }
13657
13658
13659 /*
13660 * Adjust DMA attributes.
13661 * SCSI cmds block count is up to 24 bits, SATA cmd block count vary
13662 * from 8 bits to 16 bits, depending on a command being used.
13663 * Limiting max block count arbitrarily to 256 for all read/write
13664 * commands may affects performance, so check both the device and
13665 * controller capability before adjusting dma attributes.
13666 */
13667 void
sata_adjust_dma_attr(sata_drive_info_t * sdinfo,ddi_dma_attr_t * dma_attr,ddi_dma_attr_t * adj_dma_attr)13668 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr,
13669 ddi_dma_attr_t *adj_dma_attr)
13670 {
13671 uint32_t count_max;
13672
13673 /* Copy original attributes */
13674 *adj_dma_attr = *dma_attr;
13675 /*
13676 * Things to consider: device addressing capability,
13677 * "excessive" controller DMA capabilities.
13678 * If a device is being probed/initialized, there are
13679 * no device info - use default limits then.
13680 */
13681 if (sdinfo == NULL) {
13682 count_max = dma_attr->dma_attr_granular * 0x100;
13683 if (dma_attr->dma_attr_count_max > count_max)
13684 adj_dma_attr->dma_attr_count_max = count_max;
13685 if (dma_attr->dma_attr_maxxfer > count_max)
13686 adj_dma_attr->dma_attr_maxxfer = count_max;
13687 return;
13688 }
13689
13690 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
13691 if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) {
13692 /*
13693 * 16-bit sector count may be used - we rely on
13694 * the assumption that only read and write cmds
13695 * will request more than 256 sectors worth of data
13696 */
13697 count_max = adj_dma_attr->dma_attr_granular * 0x10000;
13698 } else {
13699 /*
13700 * 8-bit sector count will be used - default limits
13701 * for dma attributes
13702 */
13703 count_max = adj_dma_attr->dma_attr_granular * 0x100;
13704 }
13705 /*
13706 * Adjust controler dma attributes, if necessary
13707 */
13708 if (dma_attr->dma_attr_count_max > count_max)
13709 adj_dma_attr->dma_attr_count_max = count_max;
13710 if (dma_attr->dma_attr_maxxfer > count_max)
13711 adj_dma_attr->dma_attr_maxxfer = count_max;
13712 }
13713 }
13714
13715
13716 /*
13717 * Allocate DMA resources for the buffer
13718 * This function handles initial DMA resource allocation as well as
13719 * DMA window shift and may be called repeatedly for the same DMA window
13720 * until all DMA cookies in the DMA window are processed.
13721 * To guarantee that there is always a coherent set of cookies to process
13722 * by SATA HBA driver (observing alignment, device granularity, etc.),
13723 * the number of slots for DMA cookies is equal to lesser of a number of
13724 * cookies in a DMA window and a max number of scatter/gather entries.
13725 *
13726 * Returns DDI_SUCCESS upon successful operation.
13727 * Return failure code of a failing command or DDI_FAILURE when
13728 * internal cleanup failed.
13729 */
13730 static int
sata_dma_buf_setup(sata_pkt_txlate_t * spx,int flags,int (* callback)(caddr_t),caddr_t arg,ddi_dma_attr_t * cur_dma_attr)13731 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags,
13732 int (*callback)(caddr_t), caddr_t arg,
13733 ddi_dma_attr_t *cur_dma_attr)
13734 {
13735 int rval;
13736 off_t offset;
13737 size_t size;
13738 int max_sg_len, req_len, i;
13739 uint_t dma_flags;
13740 struct buf *bp;
13741 uint64_t cur_txfer_len;
13742
13743
13744 ASSERT(spx->txlt_sata_pkt != NULL);
13745 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
13746 ASSERT(bp != NULL);
13747
13748
13749 if (spx->txlt_buf_dma_handle == NULL) {
13750 /*
13751 * No DMA resources allocated so far - this is a first call
13752 * for this sata pkt.
13753 */
13754 rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst),
13755 cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle);
13756
13757 if (rval != DDI_SUCCESS) {
13758 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13759 "sata_dma_buf_setup: no buf DMA resources %x",
13760 rval));
13761 return (rval);
13762 }
13763
13764 if (bp->b_flags & B_READ)
13765 dma_flags = DDI_DMA_READ;
13766 else
13767 dma_flags = DDI_DMA_WRITE;
13768
13769 if (flags & PKT_CONSISTENT)
13770 dma_flags |= DDI_DMA_CONSISTENT;
13771
13772 if (flags & PKT_DMA_PARTIAL)
13773 dma_flags |= DDI_DMA_PARTIAL;
13774
13775 /*
13776 * Check buffer alignment and size against dma attributes
13777 * Consider dma_attr_align only. There may be requests
13778 * with the size lower than device granularity, but they
13779 * will not read/write from/to the device, so no adjustment
13780 * is necessary. The dma_attr_minxfer theoretically should
13781 * be considered, but no HBA driver is checking it.
13782 */
13783 if (IS_P2ALIGNED(bp->b_un.b_addr,
13784 cur_dma_attr->dma_attr_align)) {
13785 rval = ddi_dma_buf_bind_handle(
13786 spx->txlt_buf_dma_handle,
13787 bp, dma_flags, callback, arg,
13788 &spx->txlt_dma_cookie,
13789 &spx->txlt_curwin_num_dma_cookies);
13790 } else { /* Buffer is not aligned */
13791
13792 int (*ddicallback)(caddr_t);
13793 size_t bufsz;
13794
13795 /* Check id sleeping is allowed */
13796 ddicallback = (callback == NULL_FUNC) ?
13797 DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
13798
13799 SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13800 "mis-aligned buffer: addr=0x%p, cnt=%lu",
13801 (void *)bp->b_un.b_addr, bp->b_bcount);
13802
13803 if (bp->b_flags & (B_PAGEIO|B_PHYS))
13804 /*
13805 * CPU will need to access data in the buffer
13806 * (for copying) so map it.
13807 */
13808 bp_mapin(bp);
13809
13810 ASSERT(spx->txlt_tmp_buf == NULL);
13811
13812 /* Buffer may be padded by ddi_dma_mem_alloc()! */
13813 rval = ddi_dma_mem_alloc(
13814 spx->txlt_buf_dma_handle,
13815 bp->b_bcount,
13816 &sata_acc_attr,
13817 DDI_DMA_STREAMING,
13818 ddicallback, NULL,
13819 &spx->txlt_tmp_buf,
13820 &bufsz,
13821 &spx->txlt_tmp_buf_handle);
13822
13823 if (rval != DDI_SUCCESS) {
13824 /* DMA mapping failed */
13825 (void) ddi_dma_free_handle(
13826 &spx->txlt_buf_dma_handle);
13827 spx->txlt_buf_dma_handle = NULL;
13828 #ifdef SATA_DEBUG
13829 mbuffail_count++;
13830 #endif
13831 SATADBG1(SATA_DBG_DMA_SETUP,
13832 spx->txlt_sata_hba_inst,
13833 "sata_dma_buf_setup: "
13834 "buf dma mem alloc failed %x\n", rval);
13835 return (rval);
13836 }
13837 ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf,
13838 cur_dma_attr->dma_attr_align));
13839
13840 #ifdef SATA_DEBUG
13841 mbuf_count++;
13842
13843 if (bp->b_bcount != bufsz)
13844 /*
13845 * This will require special handling, because
13846 * DMA cookies will be based on the temporary
13847 * buffer size, not the original buffer
13848 * b_bcount, so the residue may have to
13849 * be counted differently.
13850 */
13851 SATADBG2(SATA_DBG_DMA_SETUP,
13852 spx->txlt_sata_hba_inst,
13853 "sata_dma_buf_setup: bp size %x != "
13854 "bufsz %x\n", bp->b_bcount, bufsz);
13855 #endif
13856 if (dma_flags & DDI_DMA_WRITE) {
13857 /*
13858 * Write operation - copy data into
13859 * an aligned temporary buffer. Buffer will be
13860 * synced for device by ddi_dma_addr_bind_handle
13861 */
13862 bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf,
13863 bp->b_bcount);
13864 }
13865
13866 rval = ddi_dma_addr_bind_handle(
13867 spx->txlt_buf_dma_handle,
13868 NULL,
13869 spx->txlt_tmp_buf,
13870 bufsz, dma_flags, ddicallback, 0,
13871 &spx->txlt_dma_cookie,
13872 &spx->txlt_curwin_num_dma_cookies);
13873 }
13874
13875 switch (rval) {
13876 case DDI_DMA_PARTIAL_MAP:
13877 SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13878 "sata_dma_buf_setup: DMA Partial Map\n", NULL);
13879 /*
13880 * Partial DMA mapping.
13881 * Retrieve number of DMA windows for this request.
13882 */
13883 if (ddi_dma_numwin(spx->txlt_buf_dma_handle,
13884 &spx->txlt_num_dma_win) != DDI_SUCCESS) {
13885 if (spx->txlt_tmp_buf != NULL) {
13886 ddi_dma_mem_free(
13887 &spx->txlt_tmp_buf_handle);
13888 spx->txlt_tmp_buf = NULL;
13889 }
13890 (void) ddi_dma_unbind_handle(
13891 spx->txlt_buf_dma_handle);
13892 (void) ddi_dma_free_handle(
13893 &spx->txlt_buf_dma_handle);
13894 spx->txlt_buf_dma_handle = NULL;
13895 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13896 "sata_dma_buf_setup: numwin failed\n"));
13897 return (DDI_FAILURE);
13898 }
13899 SATADBG2(SATA_DBG_DMA_SETUP,
13900 spx->txlt_sata_hba_inst,
13901 "sata_dma_buf_setup: windows: %d, cookies: %d\n",
13902 spx->txlt_num_dma_win,
13903 spx->txlt_curwin_num_dma_cookies);
13904 spx->txlt_cur_dma_win = 0;
13905 break;
13906
13907 case DDI_DMA_MAPPED:
13908 /* DMA fully mapped */
13909 spx->txlt_num_dma_win = 1;
13910 spx->txlt_cur_dma_win = 0;
13911 SATADBG1(SATA_DBG_DMA_SETUP,
13912 spx->txlt_sata_hba_inst,
13913 "sata_dma_buf_setup: windows: 1 "
13914 "cookies: %d\n", spx->txlt_curwin_num_dma_cookies);
13915 break;
13916
13917 default:
13918 /* DMA mapping failed */
13919 if (spx->txlt_tmp_buf != NULL) {
13920 ddi_dma_mem_free(
13921 &spx->txlt_tmp_buf_handle);
13922 spx->txlt_tmp_buf = NULL;
13923 }
13924 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
13925 spx->txlt_buf_dma_handle = NULL;
13926 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13927 "sata_dma_buf_setup: buf dma handle binding "
13928 "failed %x\n", rval));
13929 return (rval);
13930 }
13931 spx->txlt_curwin_processed_dma_cookies = 0;
13932 spx->txlt_dma_cookie_list = NULL;
13933 } else {
13934 /*
13935 * DMA setup is reused. Check if we need to process more
13936 * cookies in current window, or to get next window, if any.
13937 */
13938
13939 ASSERT(spx->txlt_curwin_processed_dma_cookies <=
13940 spx->txlt_curwin_num_dma_cookies);
13941
13942 if (spx->txlt_curwin_processed_dma_cookies ==
13943 spx->txlt_curwin_num_dma_cookies) {
13944 /*
13945 * All cookies from current DMA window were processed.
13946 * Get next DMA window.
13947 */
13948 spx->txlt_cur_dma_win++;
13949 if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) {
13950 (void) ddi_dma_getwin(spx->txlt_buf_dma_handle,
13951 spx->txlt_cur_dma_win, &offset, &size,
13952 &spx->txlt_dma_cookie,
13953 &spx->txlt_curwin_num_dma_cookies);
13954 spx->txlt_curwin_processed_dma_cookies = 0;
13955 } else {
13956 /* No more windows! End of request! */
13957 /* What to do? - panic for now */
13958 ASSERT(spx->txlt_cur_dma_win >=
13959 spx->txlt_num_dma_win);
13960
13961 spx->txlt_curwin_num_dma_cookies = 0;
13962 spx->txlt_curwin_processed_dma_cookies = 0;
13963 spx->txlt_sata_pkt->
13964 satapkt_cmd.satacmd_num_dma_cookies = 0;
13965 return (DDI_SUCCESS);
13966 }
13967 }
13968 }
13969 /* There better be at least one DMA cookie outstanding */
13970 ASSERT((spx->txlt_curwin_num_dma_cookies -
13971 spx->txlt_curwin_processed_dma_cookies) > 0);
13972
13973 if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) {
13974 /* The default cookie slot was used in previous run */
13975 ASSERT(spx->txlt_curwin_processed_dma_cookies == 0);
13976 spx->txlt_dma_cookie_list = NULL;
13977 spx->txlt_dma_cookie_list_len = 0;
13978 }
13979 if (spx->txlt_curwin_processed_dma_cookies == 0) {
13980 /*
13981 * Processing a new DMA window - set-up dma cookies list.
13982 * We may reuse previously allocated cookie array if it is
13983 * possible.
13984 */
13985 if (spx->txlt_dma_cookie_list != NULL &&
13986 spx->txlt_dma_cookie_list_len <
13987 spx->txlt_curwin_num_dma_cookies) {
13988 /*
13989 * New DMA window contains more cookies than
13990 * the previous one. We need larger cookie list - free
13991 * the old one.
13992 */
13993 (void) kmem_free(spx->txlt_dma_cookie_list,
13994 spx->txlt_dma_cookie_list_len *
13995 sizeof (ddi_dma_cookie_t));
13996 spx->txlt_dma_cookie_list = NULL;
13997 spx->txlt_dma_cookie_list_len = 0;
13998 }
13999 if (spx->txlt_dma_cookie_list == NULL) {
14000 /*
14001 * Calculate lesser of number of cookies in this
14002 * DMA window and number of s/g entries.
14003 */
14004 max_sg_len = cur_dma_attr->dma_attr_sgllen;
14005 req_len = MIN(max_sg_len,
14006 spx->txlt_curwin_num_dma_cookies);
14007
14008 /* Allocate new dma cookie array if necessary */
14009 if (req_len == 1) {
14010 /* Only one cookie - no need for a list */
14011 spx->txlt_dma_cookie_list =
14012 &spx->txlt_dma_cookie;
14013 spx->txlt_dma_cookie_list_len = 1;
14014 } else {
14015 /*
14016 * More than one cookie - try to allocate space.
14017 */
14018 spx->txlt_dma_cookie_list = kmem_zalloc(
14019 sizeof (ddi_dma_cookie_t) * req_len,
14020 callback == NULL_FUNC ? KM_NOSLEEP :
14021 KM_SLEEP);
14022 if (spx->txlt_dma_cookie_list == NULL) {
14023 SATADBG1(SATA_DBG_DMA_SETUP,
14024 spx->txlt_sata_hba_inst,
14025 "sata_dma_buf_setup: cookie list "
14026 "allocation failed\n", NULL);
14027 /*
14028 * We could not allocate space for
14029 * neccessary number of dma cookies in
14030 * this window, so we fail this request.
14031 * Next invocation would try again to
14032 * allocate space for cookie list.
14033 * Note:Packet residue was not modified.
14034 */
14035 return (DDI_DMA_NORESOURCES);
14036 } else {
14037 spx->txlt_dma_cookie_list_len = req_len;
14038 }
14039 }
14040 }
14041 /*
14042 * Fetch DMA cookies into cookie list in sata_pkt_txlate.
14043 * First cookie was already fetched.
14044 */
14045 *(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie;
14046 cur_txfer_len =
14047 (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size;
14048 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1;
14049 spx->txlt_curwin_processed_dma_cookies++;
14050 for (i = 1; (i < spx->txlt_dma_cookie_list_len) &&
14051 (i < spx->txlt_curwin_num_dma_cookies); i++) {
14052 ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
14053 &spx->txlt_dma_cookie_list[i]);
14054 cur_txfer_len +=
14055 (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
14056 spx->txlt_curwin_processed_dma_cookies++;
14057 spx->txlt_sata_pkt->
14058 satapkt_cmd.satacmd_num_dma_cookies += 1;
14059 }
14060 } else {
14061 SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
14062 "sata_dma_buf_setup: sliding within DMA window, "
14063 "cur cookie %d, total cookies %d\n",
14064 spx->txlt_curwin_processed_dma_cookies,
14065 spx->txlt_curwin_num_dma_cookies);
14066
14067 /*
14068 * Not all cookies from the current dma window were used because
14069 * of s/g limitation.
14070 * There is no need to re-size the list - it was set at
14071 * optimal size, or only default entry is used (s/g = 1).
14072 */
14073 if (spx->txlt_dma_cookie_list == NULL) {
14074 spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie;
14075 spx->txlt_dma_cookie_list_len = 1;
14076 }
14077 /*
14078 * Since we are processing remaining cookies in a DMA window,
14079 * there may be less of them than the number of entries in the
14080 * current dma cookie list.
14081 */
14082 req_len = MIN(spx->txlt_dma_cookie_list_len,
14083 (spx->txlt_curwin_num_dma_cookies -
14084 spx->txlt_curwin_processed_dma_cookies));
14085
14086 /* Fetch the next batch of cookies */
14087 for (i = 0, cur_txfer_len = 0; i < req_len; i++) {
14088 ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
14089 &spx->txlt_dma_cookie_list[i]);
14090 cur_txfer_len +=
14091 (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
14092 spx->txlt_sata_pkt->
14093 satapkt_cmd.satacmd_num_dma_cookies++;
14094 spx->txlt_curwin_processed_dma_cookies++;
14095 }
14096 }
14097
14098 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0);
14099
14100 /* Point sata_cmd to the cookie list */
14101 spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list =
14102 &spx->txlt_dma_cookie_list[0];
14103
14104 /* Remember number of DMA cookies passed in sata packet */
14105 spx->txlt_num_dma_cookies =
14106 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies;
14107
14108 ASSERT(cur_txfer_len != 0);
14109 if (cur_txfer_len <= bp->b_bcount)
14110 spx->txlt_total_residue -= cur_txfer_len;
14111 else {
14112 /*
14113 * Temporary DMA buffer has been padded by
14114 * ddi_dma_mem_alloc()!
14115 * This requires special handling, because DMA cookies are
14116 * based on the temporary buffer size, not the b_bcount,
14117 * and we have extra bytes to transfer - but the packet
14118 * residue has to stay correct because we will copy only
14119 * the requested number of bytes.
14120 */
14121 spx->txlt_total_residue -= bp->b_bcount;
14122 }
14123
14124 return (DDI_SUCCESS);
14125 }
14126
14127 /*
14128 * Common routine for releasing DMA resources
14129 */
14130 static void
sata_common_free_dma_rsrcs(sata_pkt_txlate_t * spx)14131 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx)
14132 {
14133 if (spx->txlt_buf_dma_handle != NULL) {
14134 if (spx->txlt_tmp_buf != NULL) {
14135 /*
14136 * Intermediate DMA buffer was allocated.
14137 * Free allocated buffer and associated access handle.
14138 */
14139 ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
14140 spx->txlt_tmp_buf = NULL;
14141 }
14142 /*
14143 * Free DMA resources - cookies and handles
14144 */
14145 /* ASSERT(spx->txlt_dma_cookie_list != NULL); */
14146 if (spx->txlt_dma_cookie_list != NULL) {
14147 if (spx->txlt_dma_cookie_list !=
14148 &spx->txlt_dma_cookie) {
14149 (void) kmem_free(spx->txlt_dma_cookie_list,
14150 spx->txlt_dma_cookie_list_len *
14151 sizeof (ddi_dma_cookie_t));
14152 spx->txlt_dma_cookie_list = NULL;
14153 }
14154 }
14155 (void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
14156 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
14157 spx->txlt_buf_dma_handle = NULL;
14158 }
14159 }
14160
14161 /*
14162 * Free DMA resources
14163 * Used by the HBA driver to release DMA resources that it does not use.
14164 *
14165 * Returns Void
14166 */
14167 void
sata_free_dma_resources(sata_pkt_t * sata_pkt)14168 sata_free_dma_resources(sata_pkt_t *sata_pkt)
14169 {
14170 sata_pkt_txlate_t *spx;
14171
14172 if (sata_pkt == NULL)
14173 return;
14174
14175 spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
14176
14177 sata_common_free_dma_rsrcs(spx);
14178 }
14179
14180 /*
14181 * Fetch Device Identify data.
14182 * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type)
14183 * command to a device and get the device identify data.
14184 * The device_info structure has to be set to device type (for selecting proper
14185 * device identify command).
14186 *
14187 * Returns:
14188 * SATA_SUCCESS if cmd succeeded
14189 * SATA_RETRY if cmd was rejected and could be retried,
14190 * SATA_FAILURE if cmd failed and should not be retried (port error)
14191 *
14192 * Cannot be called in an interrupt context.
14193 */
14194
14195 static int
sata_fetch_device_identify_data(sata_hba_inst_t * sata_hba_inst,sata_drive_info_t * sdinfo)14196 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst,
14197 sata_drive_info_t *sdinfo)
14198 {
14199 struct buf *bp;
14200 sata_pkt_t *spkt;
14201 sata_cmd_t *scmd;
14202 sata_pkt_txlate_t *spx;
14203 int rval;
14204 dev_info_t *dip = SATA_DIP(sata_hba_inst);
14205
14206 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14207 spx->txlt_sata_hba_inst = sata_hba_inst;
14208 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */
14209 spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14210 if (spkt == NULL) {
14211 kmem_free(spx, sizeof (sata_pkt_txlate_t));
14212 return (SATA_RETRY); /* may retry later */
14213 }
14214 /* address is needed now */
14215 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14216
14217 /*
14218 * Allocate buffer for Identify Data return data
14219 */
14220 bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t));
14221 if (bp == NULL) {
14222 sata_pkt_free(spx);
14223 kmem_free(spx, sizeof (sata_pkt_txlate_t));
14224 SATA_LOG_D((sata_hba_inst, CE_WARN,
14225 "sata_fetch_device_identify_data: "
14226 "cannot allocate buffer for ID"));
14227 return (SATA_RETRY); /* may retry later */
14228 }
14229
14230 /* Fill sata_pkt */
14231 sdinfo->satadrv_state = SATA_STATE_PROBING;
14232 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14233 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14234 /* Synchronous mode, no callback */
14235 spkt->satapkt_comp = NULL;
14236 /* Timeout 30s */
14237 spkt->satapkt_time = sata_default_pkt_time;
14238
14239 scmd = &spkt->satapkt_cmd;
14240 scmd->satacmd_bp = bp;
14241 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
14242 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14243
14244 /* Build Identify Device cmd in the sata_pkt */
14245 scmd->satacmd_addr_type = 0; /* N/A */
14246 scmd->satacmd_sec_count_lsb = 0; /* N/A */
14247 scmd->satacmd_lba_low_lsb = 0; /* N/A */
14248 scmd->satacmd_lba_mid_lsb = 0; /* N/A */
14249 scmd->satacmd_lba_high_lsb = 0; /* N/A */
14250 scmd->satacmd_features_reg = 0; /* N/A */
14251 scmd->satacmd_device_reg = 0; /* Always device 0 */
14252 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
14253 /* Identify Packet Device cmd */
14254 scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE;
14255 } else {
14256 /* Identify Device cmd - mandatory for all other devices */
14257 scmd->satacmd_cmd_reg = SATAC_ID_DEVICE;
14258 }
14259
14260 /* Send pkt to SATA HBA driver */
14261 rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt);
14262
14263 #ifdef SATA_INJECT_FAULTS
14264 sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
14265 #endif
14266
14267 if (rval == SATA_TRAN_ACCEPTED &&
14268 spkt->satapkt_reason == SATA_PKT_COMPLETED) {
14269 if (spx->txlt_buf_dma_handle != NULL) {
14270 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
14271 DDI_DMA_SYNC_FORKERNEL);
14272 ASSERT(rval == DDI_SUCCESS);
14273 if (sata_check_for_dma_error(dip, spx)) {
14274 ddi_fm_service_impact(dip,
14275 DDI_SERVICE_UNAFFECTED);
14276 rval = SATA_RETRY;
14277 goto fail;
14278 }
14279
14280 }
14281 if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config &
14282 SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) {
14283 SATA_LOG_D((sata_hba_inst, CE_WARN,
14284 "SATA disk device at port %d - "
14285 "partial Identify Data",
14286 sdinfo->satadrv_addr.cport));
14287 rval = SATA_RETRY; /* may retry later */
14288 goto fail;
14289 }
14290 /* Update sata_drive_info */
14291 bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id,
14292 sizeof (sata_id_t));
14293
14294 sdinfo->satadrv_features_support = 0;
14295 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
14296 /*
14297 * Retrieve capacity (disks only) and addressing mode
14298 */
14299 sdinfo->satadrv_capacity = sata_check_capacity(sdinfo);
14300 } else {
14301 /*
14302 * For ATAPI devices one would have to issue
14303 * Get Capacity cmd for media capacity. Not here.
14304 */
14305 sdinfo->satadrv_capacity = 0;
14306 /*
14307 * Check what cdb length is supported
14308 */
14309 if ((sdinfo->satadrv_id.ai_config &
14310 SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B)
14311 sdinfo->satadrv_atapi_cdb_len = 16;
14312 else
14313 sdinfo->satadrv_atapi_cdb_len = 12;
14314 }
14315 /* Setup supported features flags */
14316 if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT)
14317 sdinfo->satadrv_features_support |= SATA_DEV_F_DMA;
14318
14319 /* Check for SATA GEN and NCQ support */
14320 if (sdinfo->satadrv_id.ai_satacap != 0 &&
14321 sdinfo->satadrv_id.ai_satacap != 0xffff) {
14322 /* SATA compliance */
14323 if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ)
14324 sdinfo->satadrv_features_support |=
14325 SATA_DEV_F_NCQ;
14326 if (sdinfo->satadrv_id.ai_satacap &
14327 (SATA_1_SPEED | SATA_2_SPEED | SATA_3_SPEED)) {
14328 if (sdinfo->satadrv_id.ai_satacap &
14329 SATA_3_SPEED)
14330 sdinfo->satadrv_features_support |=
14331 SATA_DEV_F_SATA3;
14332 if (sdinfo->satadrv_id.ai_satacap &
14333 SATA_2_SPEED)
14334 sdinfo->satadrv_features_support |=
14335 SATA_DEV_F_SATA2;
14336 if (sdinfo->satadrv_id.ai_satacap &
14337 SATA_1_SPEED)
14338 sdinfo->satadrv_features_support |=
14339 SATA_DEV_F_SATA1;
14340 } else {
14341 sdinfo->satadrv_features_support |=
14342 SATA_DEV_F_SATA1;
14343 }
14344 }
14345 if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) &&
14346 (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD))
14347 sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ;
14348
14349 sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth;
14350 if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) ||
14351 (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) {
14352 ++sdinfo->satadrv_queue_depth;
14353 /* Adjust according to controller capabilities */
14354 sdinfo->satadrv_max_queue_depth = MIN(
14355 sdinfo->satadrv_queue_depth,
14356 SATA_QDEPTH(sata_hba_inst));
14357 /* Adjust according to global queue depth limit */
14358 sdinfo->satadrv_max_queue_depth = MIN(
14359 sdinfo->satadrv_max_queue_depth,
14360 sata_current_max_qdepth);
14361 if (sdinfo->satadrv_max_queue_depth == 0)
14362 sdinfo->satadrv_max_queue_depth = 1;
14363 } else
14364 sdinfo->satadrv_max_queue_depth = 1;
14365
14366 rval = SATA_SUCCESS;
14367 } else {
14368 /*
14369 * Woops, no Identify Data.
14370 */
14371 if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) {
14372 rval = SATA_RETRY; /* may retry later */
14373 } else if (rval == SATA_TRAN_ACCEPTED) {
14374 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR ||
14375 spkt->satapkt_reason == SATA_PKT_ABORTED ||
14376 spkt->satapkt_reason == SATA_PKT_TIMEOUT ||
14377 spkt->satapkt_reason == SATA_PKT_RESET)
14378 rval = SATA_RETRY; /* may retry later */
14379 else
14380 rval = SATA_FAILURE;
14381 } else {
14382 rval = SATA_FAILURE;
14383 }
14384 }
14385 fail:
14386 /* Free allocated resources */
14387 sata_free_local_buffer(spx);
14388 sata_pkt_free(spx);
14389 kmem_free(spx, sizeof (sata_pkt_txlate_t));
14390
14391 return (rval);
14392 }
14393
14394
14395 /*
14396 * Some devices may not come-up with default DMA mode (UDMA or MWDMA).
14397 * UDMA mode is checked first, followed by MWDMA mode.
14398 * set correctly, so this function is setting it to the highest supported level.
14399 * Older SATA spec required that the device supports at least DMA 4 mode and
14400 * UDMA mode is selected. It is not mentioned in SerialATA 2.6, so this
14401 * restriction has been removed.
14402 *
14403 * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported.
14404 * Returns SATA_FAILURE if proper DMA mode could not be selected.
14405 *
14406 * NOTE: This function should be called only if DMA mode is supported.
14407 */
14408 static int
sata_set_dma_mode(sata_hba_inst_t * sata_hba_inst,sata_drive_info_t * sdinfo)14409 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo)
14410 {
14411 sata_pkt_t *spkt;
14412 sata_cmd_t *scmd;
14413 sata_pkt_txlate_t *spx;
14414 int i, mode;
14415 uint8_t subcmd;
14416 int rval = SATA_SUCCESS;
14417
14418 ASSERT(sdinfo != NULL);
14419 ASSERT(sata_hba_inst != NULL);
14420
14421 if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
14422 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) {
14423 /* Find highest Ultra DMA mode supported */
14424 for (mode = 6; mode >= 0; --mode) {
14425 if (sdinfo->satadrv_id.ai_ultradma & (1 << mode))
14426 break;
14427 }
14428 #if 0
14429 /* Left for historical reasons */
14430 /*
14431 * Some initial version of SATA spec indicated that at least
14432 * UDMA mode 4 has to be supported. It is not mentioned in
14433 * SerialATA 2.6, so this restriction is removed.
14434 */
14435 if (mode < 4)
14436 return (SATA_FAILURE);
14437 #endif
14438
14439 /*
14440 * For disk, we're still going to set DMA mode whatever is
14441 * selected by default
14442 *
14443 * We saw an old maxtor sata drive will select Ultra DMA and
14444 * Multi-Word DMA simultaneouly by default, which is going
14445 * to cause DMA command timed out, so we need to select DMA
14446 * mode even when it's already done by default
14447 */
14448 if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) {
14449
14450 /* Find UDMA mode currently selected */
14451 for (i = 6; i >= 0; --i) {
14452 if (sdinfo->satadrv_id.ai_ultradma &
14453 (1 << (i + 8)))
14454 break;
14455 }
14456 if (i >= mode)
14457 /* Nothing to do */
14458 return (SATA_SUCCESS);
14459 }
14460
14461 subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA;
14462
14463 } else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) {
14464 /* Find highest MultiWord DMA mode supported */
14465 for (mode = 2; mode >= 0; --mode) {
14466 if (sdinfo->satadrv_id.ai_dworddma & (1 << mode))
14467 break;
14468 }
14469
14470 /*
14471 * For disk, We're still going to set DMA mode whatever is
14472 * selected by default
14473 *
14474 * We saw an old maxtor sata drive will select Ultra DMA and
14475 * Multi-Word DMA simultaneouly by default, which is going
14476 * to cause DMA command timed out, so we need to select DMA
14477 * mode even when it's already done by default
14478 */
14479 if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) {
14480
14481 /* Find highest MultiWord DMA mode selected */
14482 for (i = 2; i >= 0; --i) {
14483 if (sdinfo->satadrv_id.ai_dworddma &
14484 (1 << (i + 8)))
14485 break;
14486 }
14487 if (i >= mode)
14488 /* Nothing to do */
14489 return (SATA_SUCCESS);
14490 }
14491
14492 subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA;
14493 } else
14494 return (SATA_SUCCESS);
14495
14496 /*
14497 * Set DMA mode via SET FEATURES COMMAND.
14498 * Prepare packet for SET FEATURES COMMAND.
14499 */
14500 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14501 spx->txlt_sata_hba_inst = sata_hba_inst;
14502 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */
14503 spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14504 if (spkt == NULL) {
14505 SATA_LOG_D((sata_hba_inst, CE_WARN,
14506 "sata_set_dma_mode: could not set DMA mode %d", mode));
14507 rval = SATA_FAILURE;
14508 goto done;
14509 }
14510 /* Fill sata_pkt */
14511 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14512 /* Timeout 30s */
14513 spkt->satapkt_time = sata_default_pkt_time;
14514 /* Synchronous mode, no callback, interrupts */
14515 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14516 spkt->satapkt_comp = NULL;
14517 scmd = &spkt->satapkt_cmd;
14518 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14519 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14520 scmd->satacmd_addr_type = 0;
14521 scmd->satacmd_device_reg = 0;
14522 scmd->satacmd_status_reg = 0;
14523 scmd->satacmd_error_reg = 0;
14524 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14525 scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE;
14526 scmd->satacmd_sec_count_lsb = subcmd | mode;
14527
14528 /* Transfer command to HBA */
14529 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
14530 spkt) != SATA_TRAN_ACCEPTED ||
14531 spkt->satapkt_reason != SATA_PKT_COMPLETED) {
14532 /* Pkt execution failed */
14533 rval = SATA_FAILURE;
14534 }
14535 done:
14536
14537 /* Free allocated resources */
14538 if (spkt != NULL)
14539 sata_pkt_free(spx);
14540 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14541
14542 return (rval);
14543 }
14544
14545
14546 /*
14547 * Set device caching mode.
14548 * One of the following operations should be specified:
14549 * SATAC_SF_ENABLE_READ_AHEAD
14550 * SATAC_SF_DISABLE_READ_AHEAD
14551 * SATAC_SF_ENABLE_WRITE_CACHE
14552 * SATAC_SF_DISABLE_WRITE_CACHE
14553 *
14554 * If operation fails, system log messgage is emitted.
14555 * Returns SATA_SUCCESS when the operation succeeds, SATA_RETRY if
14556 * command was sent but did not succeed, and SATA_FAILURE otherwise.
14557 */
14558
14559 static int
sata_set_cache_mode(sata_hba_inst_t * sata_hba_inst,sata_drive_info_t * sdinfo,int cache_op)14560 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
14561 int cache_op)
14562 {
14563 sata_pkt_t *spkt;
14564 sata_cmd_t *scmd;
14565 sata_pkt_txlate_t *spx;
14566 int rval = SATA_SUCCESS;
14567 int hba_rval;
14568 char *infop = NULL;
14569
14570 ASSERT(sdinfo != NULL);
14571 ASSERT(sata_hba_inst != NULL);
14572 ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD ||
14573 cache_op == SATAC_SF_DISABLE_READ_AHEAD ||
14574 cache_op == SATAC_SF_ENABLE_WRITE_CACHE ||
14575 cache_op == SATAC_SF_DISABLE_WRITE_CACHE);
14576
14577
14578 /* Prepare packet for SET FEATURES COMMAND */
14579 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14580 spx->txlt_sata_hba_inst = sata_hba_inst;
14581 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */
14582 spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14583 if (spkt == NULL) {
14584 rval = SATA_FAILURE;
14585 goto failure;
14586 }
14587 /* Fill sata_pkt */
14588 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14589 /* Timeout 30s */
14590 spkt->satapkt_time = sata_default_pkt_time;
14591 /* Synchronous mode, no callback, interrupts */
14592 spkt->satapkt_op_mode =
14593 SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14594 spkt->satapkt_comp = NULL;
14595 scmd = &spkt->satapkt_cmd;
14596 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14597 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14598 scmd->satacmd_addr_type = 0;
14599 scmd->satacmd_device_reg = 0;
14600 scmd->satacmd_status_reg = 0;
14601 scmd->satacmd_error_reg = 0;
14602 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14603 scmd->satacmd_features_reg = cache_op;
14604
14605 /* Transfer command to HBA */
14606 hba_rval = (*SATA_START_FUNC(sata_hba_inst))(
14607 SATA_DIP(sata_hba_inst), spkt);
14608
14609 #ifdef SATA_INJECT_FAULTS
14610 sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
14611 #endif
14612
14613 if ((hba_rval != SATA_TRAN_ACCEPTED) ||
14614 (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
14615 /* Pkt execution failed */
14616 switch (cache_op) {
14617 case SATAC_SF_ENABLE_READ_AHEAD:
14618 infop = "enabling read ahead failed";
14619 break;
14620 case SATAC_SF_DISABLE_READ_AHEAD:
14621 infop = "disabling read ahead failed";
14622 break;
14623 case SATAC_SF_ENABLE_WRITE_CACHE:
14624 infop = "enabling write cache failed";
14625 break;
14626 case SATAC_SF_DISABLE_WRITE_CACHE:
14627 infop = "disabling write cache failed";
14628 break;
14629 }
14630 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
14631 rval = SATA_RETRY;
14632 }
14633 failure:
14634 /* Free allocated resources */
14635 if (spkt != NULL)
14636 sata_pkt_free(spx);
14637 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14638 return (rval);
14639 }
14640
14641 /*
14642 * Set Removable Media Status Notification (enable/disable)
14643 * state == 0 , disable
14644 * state != 0 , enable
14645 *
14646 * If operation fails, system log messgage is emitted.
14647 * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
14648 */
14649
14650 static int
sata_set_rmsn(sata_hba_inst_t * sata_hba_inst,sata_drive_info_t * sdinfo,int state)14651 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
14652 int state)
14653 {
14654 sata_pkt_t *spkt;
14655 sata_cmd_t *scmd;
14656 sata_pkt_txlate_t *spx;
14657 int rval = SATA_SUCCESS;
14658 char *infop;
14659
14660 ASSERT(sdinfo != NULL);
14661 ASSERT(sata_hba_inst != NULL);
14662
14663 /* Prepare packet for SET FEATURES COMMAND */
14664 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14665 spx->txlt_sata_hba_inst = sata_hba_inst;
14666 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */
14667 spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14668 if (spkt == NULL) {
14669 rval = SATA_FAILURE;
14670 goto failure;
14671 }
14672 /* Fill sata_pkt */
14673 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14674 /* Timeout 30s */
14675 spkt->satapkt_time = sata_default_pkt_time;
14676 /* Synchronous mode, no callback, interrupts */
14677 spkt->satapkt_op_mode =
14678 SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14679 spkt->satapkt_comp = NULL;
14680 scmd = &spkt->satapkt_cmd;
14681 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14682 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14683 scmd->satacmd_addr_type = 0;
14684 scmd->satacmd_device_reg = 0;
14685 scmd->satacmd_status_reg = 0;
14686 scmd->satacmd_error_reg = 0;
14687 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14688 if (state == 0)
14689 scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN;
14690 else
14691 scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN;
14692
14693 /* Transfer command to HBA */
14694 if (((*SATA_START_FUNC(sata_hba_inst))(
14695 SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
14696 (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
14697 /* Pkt execution failed */
14698 if (state == 0)
14699 infop = "disabling Removable Media Status "
14700 "Notification failed";
14701 else
14702 infop = "enabling Removable Media Status "
14703 "Notification failed";
14704
14705 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
14706 rval = SATA_FAILURE;
14707 }
14708 failure:
14709 /* Free allocated resources */
14710 if (spkt != NULL)
14711 sata_pkt_free(spx);
14712 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14713 return (rval);
14714 }
14715
14716
14717 /*
14718 * Update state and copy port ss* values from passed sata_device structure.
14719 * sata_address is validated - if not valid, nothing is changed in sata_scsi
14720 * configuration struct.
14721 *
14722 * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function
14723 * regardless of the state in device argument.
14724 *
14725 * Port mutex should be held while calling this function.
14726 */
14727 static void
sata_update_port_info(sata_hba_inst_t * sata_hba_inst,sata_device_t * sata_device)14728 sata_update_port_info(sata_hba_inst_t *sata_hba_inst,
14729 sata_device_t *sata_device)
14730 {
14731 sata_cport_info_t *cportinfo;
14732
14733 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT ||
14734 sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
14735 if (SATA_NUM_CPORTS(sata_hba_inst) <=
14736 sata_device->satadev_addr.cport)
14737 return;
14738
14739 cportinfo = SATA_CPORT_INFO(sata_hba_inst,
14740 sata_device->satadev_addr.cport);
14741
14742 ASSERT(mutex_owned(&cportinfo->cport_mutex));
14743 cportinfo->cport_scr = sata_device->satadev_scr;
14744
14745 /* Preserve SATA_PSTATE_SHUTDOWN flag */
14746 cportinfo->cport_state &= ~(SATA_PSTATE_PWRON |
14747 SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
14748 cportinfo->cport_state |=
14749 sata_device->satadev_state & SATA_PSTATE_VALID;
14750 }
14751 }
14752
14753 void
sata_update_pmport_info(sata_hba_inst_t * sata_hba_inst,sata_device_t * sata_device)14754 sata_update_pmport_info(sata_hba_inst_t *sata_hba_inst,
14755 sata_device_t *sata_device)
14756 {
14757 sata_pmport_info_t *pmportinfo;
14758
14759 if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT &&
14760 sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) ||
14761 SATA_NUM_PMPORTS(sata_hba_inst,
14762 sata_device->satadev_addr.cport) <
14763 sata_device->satadev_addr.pmport) {
14764 SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
14765 "sata_update_port_info: error address %p.",
14766 &sata_device->satadev_addr);
14767 return;
14768 }
14769
14770 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
14771 sata_device->satadev_addr.cport,
14772 sata_device->satadev_addr.pmport);
14773
14774 ASSERT(mutex_owned(&pmportinfo->pmport_mutex));
14775 pmportinfo->pmport_scr = sata_device->satadev_scr;
14776
14777 /* Preserve SATA_PSTATE_SHUTDOWN flag */
14778 pmportinfo->pmport_state &=
14779 ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
14780 pmportinfo->pmport_state |=
14781 sata_device->satadev_state & SATA_PSTATE_VALID;
14782 }
14783
14784 /*
14785 * Extract SATA port specification from an IOCTL argument.
14786 *
14787 * This function return the port the user land send us as is, unless it
14788 * cannot retrieve port spec, then -1 is returned.
14789 *
14790 * Support port multiplier.
14791 */
14792 static int32_t
sata_get_port_num(sata_hba_inst_t * sata_hba_inst,struct devctl_iocdata * dcp)14793 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp)
14794 {
14795 int32_t port;
14796
14797 /* Extract port number from nvpair in dca structure */
14798 if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
14799 SATA_LOG_D((sata_hba_inst, CE_NOTE,
14800 "sata_get_port_num: invalid port spec 0x%x in ioctl",
14801 port));
14802 port = -1;
14803 }
14804
14805 return (port);
14806 }
14807
14808 /*
14809 * Get dev_info_t pointer to the device node pointed to by port argument.
14810 * NOTE: target argument is a value used in ioctls to identify
14811 * the AP - it is not a sata_address.
14812 * It is a combination of cport, pmport and address qualifier, encodded same
14813 * way as a scsi target number.
14814 * At this moment it carries only cport number.
14815 *
14816 * PMult hotplug is supported now.
14817 *
14818 * Returns dev_info_t pointer if target device was found, NULL otherwise.
14819 */
14820
14821 static dev_info_t *
sata_get_target_dip(dev_info_t * dip,uint8_t cport,uint8_t pmport)14822 sata_get_target_dip(dev_info_t *dip, uint8_t cport, uint8_t pmport)
14823 {
14824 dev_info_t *cdip = NULL;
14825 int target, tgt;
14826 uint8_t qual;
14827
14828 sata_hba_inst_t *sata_hba_inst;
14829 scsi_hba_tran_t *scsi_hba_tran;
14830
14831 /* Get target id */
14832 scsi_hba_tran = ddi_get_driver_private(dip);
14833 if (scsi_hba_tran == NULL)
14834 return (NULL);
14835
14836 sata_hba_inst = scsi_hba_tran->tran_hba_private;
14837
14838 if (sata_hba_inst == NULL)
14839 return (NULL);
14840
14841 /* Identify a port-mult by cport_info.cport_dev_type */
14842 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT)
14843 qual = SATA_ADDR_DPMPORT;
14844 else
14845 qual = SATA_ADDR_DCPORT;
14846
14847 target = SATA_TO_SCSI_TARGET(cport, pmport, qual);
14848
14849 /* Retrieve target dip */
14850 ndi_devi_enter(dip);
14851 for (cdip = ddi_get_child(dip); cdip != NULL; ) {
14852 dev_info_t *next = ddi_get_next_sibling(cdip);
14853
14854 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
14855 DDI_PROP_DONTPASS, "target", -1);
14856 if (tgt == -1) {
14857 /*
14858 * This is actually an error condition, but not
14859 * a fatal one. Just continue the search.
14860 */
14861 cdip = next;
14862 continue;
14863 }
14864
14865 if (tgt == target)
14866 break;
14867
14868 cdip = next;
14869 }
14870 ndi_devi_exit(dip);
14871
14872 return (cdip);
14873 }
14874
14875 /*
14876 * Get dev_info_t pointer to the device node pointed to by port argument.
14877 * NOTE: target argument is a value used in ioctls to identify
14878 * the AP - it is not a sata_address.
14879 * It is a combination of cport, pmport and address qualifier, encoded same
14880 * way as a scsi target number.
14881 *
14882 * Returns dev_info_t pointer if target device was found, NULL otherwise.
14883 */
14884
14885 static dev_info_t *
sata_get_scsi_target_dip(dev_info_t * dip,sata_address_t * saddr)14886 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr)
14887 {
14888 dev_info_t *cdip = NULL;
14889 int target, tgt;
14890
14891 target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual);
14892
14893 ndi_devi_enter(dip);
14894 for (cdip = ddi_get_child(dip); cdip != NULL; ) {
14895 dev_info_t *next = ddi_get_next_sibling(cdip);
14896
14897 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
14898 DDI_PROP_DONTPASS, "target", -1);
14899 if (tgt == -1) {
14900 /*
14901 * This is actually an error condition, but not
14902 * a fatal one. Just continue the search.
14903 */
14904 cdip = next;
14905 continue;
14906 }
14907
14908 if (tgt == target)
14909 break;
14910
14911 cdip = next;
14912 }
14913 ndi_devi_exit(dip);
14914
14915 return (cdip);
14916 }
14917
14918 /*
14919 * Process sata port disconnect request.
14920 * Normally, cfgadm sata plugin will try to offline (unconfigure) the device
14921 * before this request. Nevertheless, if a device is still configured,
14922 * we need to attempt to offline and unconfigure device.
14923 * Regardless of the unconfigure operation results the port is marked as
14924 * deactivated and no access to the attached device is possible.
14925 * If the target node remains because unconfigure operation failed, its state
14926 * will be set to DEVICE_REMOVED, preventing it to be used again when a device
14927 * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure
14928 * the device and remove old target node.
14929 *
14930 * This function invokes sata_hba_inst->satahba_tran->
14931 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
14932 * If successful, the device structure (if any) attached to the specified port
14933 * is removed and state of the port marked appropriately.
14934 * Failure of the port_deactivate may keep port in the physically active state,
14935 * or may fail the port.
14936 *
14937 * NOTE: Port multiplier is supported.
14938 */
14939
14940 static int
sata_ioctl_disconnect(sata_hba_inst_t * sata_hba_inst,sata_device_t * sata_device)14941 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst,
14942 sata_device_t *sata_device)
14943 {
14944 sata_drive_info_t *sdinfo = NULL, *subsdinfo = NULL;
14945 sata_cport_info_t *cportinfo = NULL;
14946 sata_pmport_info_t *pmportinfo = NULL;
14947 sata_pmult_info_t *pmultinfo = NULL;
14948 sata_device_t subsdevice;
14949 int cport, pmport, qual;
14950 int rval = SATA_SUCCESS;
14951 int npmport = 0;
14952 int rv = 0;
14953
14954 cport = sata_device->satadev_addr.cport;
14955 pmport = sata_device->satadev_addr.pmport;
14956 qual = sata_device->satadev_addr.qual;
14957
14958 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
14959 if (qual == SATA_ADDR_DCPORT)
14960 qual = SATA_ADDR_CPORT;
14961 else
14962 qual = SATA_ADDR_PMPORT;
14963
14964 /*
14965 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran->
14966 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
14967 * Do the sanity check.
14968 */
14969 if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
14970 /* No physical port deactivation supported. */
14971 return (EINVAL);
14972 }
14973
14974 /* Check the current state of the port */
14975 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14976 (SATA_DIP(sata_hba_inst), sata_device);
14977
14978 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
14979
14980 /*
14981 * Processing port mulitiplier
14982 */
14983 if (qual == SATA_ADDR_CPORT &&
14984 SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) {
14985 mutex_enter(&cportinfo->cport_mutex);
14986
14987 /* Check controller port status */
14988 sata_update_port_info(sata_hba_inst, sata_device);
14989 if (rval != SATA_SUCCESS ||
14990 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14991 /*
14992 * Device port status is unknown or it is in failed
14993 * state
14994 */
14995 SATA_CPORT_STATE(sata_hba_inst, cport) =
14996 SATA_PSTATE_FAILED;
14997 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14998 "sata_hba_ioctl: connect: failed to deactivate "
14999 "SATA port %d", cport);
15000 mutex_exit(&cportinfo->cport_mutex);
15001 return (EIO);
15002 }
15003
15004 /* Disconnect all sub-devices. */
15005 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
15006 if (pmultinfo != NULL) {
15007
15008 for (npmport = 0; npmport < SATA_NUM_PMPORTS(
15009 sata_hba_inst, cport); npmport ++) {
15010 subsdinfo = SATA_PMPORT_DRV_INFO(
15011 sata_hba_inst, cport, npmport);
15012 if (subsdinfo == NULL)
15013 continue;
15014
15015 subsdevice.satadev_addr = subsdinfo->
15016 satadrv_addr;
15017
15018 mutex_exit(&cportinfo->cport_mutex);
15019 if (sata_ioctl_disconnect(sata_hba_inst,
15020 &subsdevice) == SATA_SUCCESS) {
15021 SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
15022 "[Remove] device at port %d:%d "
15023 "successfully.", cport, npmport);
15024 }
15025 mutex_enter(&cportinfo->cport_mutex);
15026 }
15027 }
15028
15029 /* Disconnect the port multiplier */
15030 cportinfo->cport_state &= ~SATA_STATE_READY;
15031 mutex_exit(&cportinfo->cport_mutex);
15032
15033 sata_device->satadev_addr.qual = qual;
15034 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
15035 (SATA_DIP(sata_hba_inst), sata_device);
15036
15037 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15038 SE_NO_HINT);
15039
15040 mutex_enter(&cportinfo->cport_mutex);
15041 sata_update_port_info(sata_hba_inst, sata_device);
15042 if (rval != SATA_SUCCESS &&
15043 sata_device->satadev_state & SATA_PSTATE_FAILED) {
15044 cportinfo->cport_state = SATA_PSTATE_FAILED;
15045 rv = EIO;
15046 } else {
15047 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
15048 }
15049 mutex_exit(&cportinfo->cport_mutex);
15050
15051 return (rv);
15052 }
15053
15054 /*
15055 * Process non-port-multiplier device - it could be a drive connected
15056 * to a port multiplier port or a controller port.
15057 */
15058 if (qual == SATA_ADDR_PMPORT) {
15059 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
15060 mutex_enter(&pmportinfo->pmport_mutex);
15061 sata_update_pmport_info(sata_hba_inst, sata_device);
15062 if (rval != SATA_SUCCESS ||
15063 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
15064 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
15065 SATA_PSTATE_FAILED;
15066 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
15067 "sata_hba_ioctl: connect: failed to deactivate "
15068 "SATA port %d:%d", cport, pmport);
15069 mutex_exit(&pmportinfo->pmport_mutex);
15070 return (EIO);
15071 }
15072
15073 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
15074 sdinfo = pmportinfo->pmport_sata_drive;
15075 ASSERT(sdinfo != NULL);
15076 }
15077
15078 /*
15079 * Set port's dev_state to not ready - this will disable
15080 * an access to a potentially attached device.
15081 */
15082 pmportinfo->pmport_state &= ~SATA_STATE_READY;
15083
15084 /* Remove and release sata_drive info structure. */
15085 if (sdinfo != NULL) {
15086 if ((sdinfo->satadrv_type &
15087 SATA_VALID_DEV_TYPE) != 0) {
15088 /*
15089 * If a target node exists, try to offline
15090 * a device and remove target node.
15091 */
15092 mutex_exit(&pmportinfo->pmport_mutex);
15093 (void) sata_offline_device(sata_hba_inst,
15094 sata_device, sdinfo);
15095 mutex_enter(&pmportinfo->pmport_mutex);
15096 }
15097
15098 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
15099 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
15100 (void) kmem_free((void *)sdinfo,
15101 sizeof (sata_drive_info_t));
15102 }
15103 mutex_exit(&pmportinfo->pmport_mutex);
15104
15105 } else if (qual == SATA_ADDR_CPORT) {
15106 mutex_enter(&cportinfo->cport_mutex);
15107 sata_update_port_info(sata_hba_inst, sata_device);
15108 if (rval != SATA_SUCCESS ||
15109 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
15110 /*
15111 * Device port status is unknown or it is in failed
15112 * state
15113 */
15114 SATA_CPORT_STATE(sata_hba_inst, cport) =
15115 SATA_PSTATE_FAILED;
15116 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
15117 "sata_hba_ioctl: connect: failed to deactivate "
15118 "SATA port %d", cport);
15119 mutex_exit(&cportinfo->cport_mutex);
15120 return (EIO);
15121 }
15122
15123 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
15124 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
15125 ASSERT(pmultinfo != NULL);
15126 } else if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
15127 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
15128 ASSERT(sdinfo != NULL);
15129 }
15130 cportinfo->cport_state &= ~SATA_STATE_READY;
15131
15132 if (sdinfo != NULL) {
15133 if ((sdinfo->satadrv_type &
15134 SATA_VALID_DEV_TYPE) != 0) {
15135 /*
15136 * If a target node exists, try to offline
15137 * a device and remove target node.
15138 */
15139 mutex_exit(&cportinfo->cport_mutex);
15140 (void) sata_offline_device(sata_hba_inst,
15141 sata_device, sdinfo);
15142 mutex_enter(&cportinfo->cport_mutex);
15143 }
15144
15145 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
15146 cportinfo->cport_dev_type = SATA_DTYPE_NONE;
15147 (void) kmem_free((void *)sdinfo,
15148 sizeof (sata_drive_info_t));
15149 }
15150 mutex_exit(&cportinfo->cport_mutex);
15151 }
15152
15153 /* Just ask HBA driver to deactivate port */
15154 sata_device->satadev_addr.qual = qual;
15155
15156 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
15157 (SATA_DIP(sata_hba_inst), sata_device);
15158
15159 /*
15160 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15161 * without the hint (to force listener to investivate the state).
15162 */
15163 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15164 SE_NO_HINT);
15165
15166 if (qual == SATA_ADDR_PMPORT) {
15167 mutex_enter(&pmportinfo->pmport_mutex);
15168 sata_update_pmport_info(sata_hba_inst, sata_device);
15169
15170 if (rval != SATA_SUCCESS &&
15171 sata_device->satadev_state & SATA_PSTATE_FAILED) {
15172 /*
15173 * Port deactivation failure - do not change port
15174 * state unless the state returned by HBA indicates a
15175 * port failure.
15176 *
15177 * NOTE: device structures were released, so devices
15178 * now are invisible! Port reset is needed to
15179 * re-enumerate devices.
15180 */
15181 pmportinfo->pmport_state = SATA_PSTATE_FAILED;
15182 rv = EIO;
15183 } else {
15184 /*
15185 * Deactivation succeded. From now on the sata framework
15186 * will not care what is happening to the device, until
15187 * the port is activated again.
15188 */
15189 pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
15190 }
15191 mutex_exit(&pmportinfo->pmport_mutex);
15192 } else if (qual == SATA_ADDR_CPORT) {
15193 mutex_enter(&cportinfo->cport_mutex);
15194 sata_update_port_info(sata_hba_inst, sata_device);
15195
15196 if (rval != SATA_SUCCESS &&
15197 sata_device->satadev_state & SATA_PSTATE_FAILED) {
15198 cportinfo->cport_state = SATA_PSTATE_FAILED;
15199 rv = EIO;
15200 } else {
15201 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
15202 }
15203 mutex_exit(&cportinfo->cport_mutex);
15204 }
15205
15206 return (rv);
15207 }
15208
15209
15210
15211 /*
15212 * Process sata port connect request
15213 * The sata cfgadm pluging will invoke this operation only if port was found
15214 * in the disconnect state (failed state is also treated as the disconnected
15215 * state).
15216 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->satahba_tran->
15217 * sata_tran_hotplug_ops->sata_tran_port_activate().
15218 * If successful and a device is found attached to the port,
15219 * the initialization sequence is executed to attach a device structure to
15220 * a port structure. The state of the port and a device would be set
15221 * appropriately.
15222 * The device is not set in configured state (system-wise) by this operation.
15223 *
15224 * Note, that activating the port may generate link events,
15225 * so it is important that following processing and the
15226 * event processing does not interfere with each other!
15227 *
15228 * This operation may remove port failed state and will
15229 * try to make port active and in good standing.
15230 *
15231 * NOTE: Port multiplier is supported.
15232 */
15233
15234 static int
sata_ioctl_connect(sata_hba_inst_t * sata_hba_inst,sata_device_t * sata_device)15235 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst,
15236 sata_device_t *sata_device)
15237 {
15238 sata_pmport_info_t *pmportinfo = NULL;
15239 uint8_t cport, pmport, qual;
15240 int rv = 0;
15241
15242 cport = sata_device->satadev_addr.cport;
15243 pmport = sata_device->satadev_addr.pmport;
15244 qual = sata_device->satadev_addr.qual;
15245
15246 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
15247 if (qual == SATA_ADDR_DCPORT)
15248 qual = SATA_ADDR_CPORT;
15249 else
15250 qual = SATA_ADDR_PMPORT;
15251
15252 if (qual == SATA_ADDR_PMPORT)
15253 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
15254
15255 /*
15256 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->
15257 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate().
15258 * Perform sanity check now.
15259 */
15260 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) {
15261 /* No physical port activation supported. */
15262 return (EINVAL);
15263 }
15264
15265 /* Just ask HBA driver to activate port */
15266 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
15267 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15268 /*
15269 * Port activation failure.
15270 */
15271 if (qual == SATA_ADDR_CPORT) {
15272 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15273 cport)->cport_mutex);
15274 sata_update_port_info(sata_hba_inst, sata_device);
15275 if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15276 SATA_CPORT_STATE(sata_hba_inst, cport) =
15277 SATA_PSTATE_FAILED;
15278 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
15279 "sata_hba_ioctl: connect: failed to "
15280 "activate SATA port %d", cport);
15281 }
15282 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15283 cport)->cport_mutex);
15284 } else { /* port multiplier device port */
15285 mutex_enter(&pmportinfo->pmport_mutex);
15286 sata_update_pmport_info(sata_hba_inst, sata_device);
15287 if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15288 SATA_PMPORT_STATE(sata_hba_inst, cport,
15289 pmport) = SATA_PSTATE_FAILED;
15290 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
15291 "sata_hba_ioctl: connect: failed to "
15292 "activate SATA port %d:%d", cport, pmport);
15293 }
15294 mutex_exit(&pmportinfo->pmport_mutex);
15295 }
15296 return (EIO);
15297 }
15298
15299 /* Virgin port state - will be updated by the port re-probe. */
15300 if (qual == SATA_ADDR_CPORT) {
15301 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15302 cport)->cport_mutex);
15303 SATA_CPORT_STATE(sata_hba_inst, cport) = 0;
15304 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15305 cport)->cport_mutex);
15306 } else { /* port multiplier device port */
15307 mutex_enter(&pmportinfo->pmport_mutex);
15308 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0;
15309 mutex_exit(&pmportinfo->pmport_mutex);
15310 }
15311
15312 /*
15313 * Probe the port to find its state and attached device.
15314 */
15315 if (sata_reprobe_port(sata_hba_inst, sata_device,
15316 SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE)
15317 rv = EIO;
15318
15319 /*
15320 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15321 * without the hint
15322 */
15323 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15324 SE_NO_HINT);
15325
15326 /*
15327 * If there is a device attached to the port, emit
15328 * a message.
15329 */
15330 if (sata_device->satadev_type != SATA_DTYPE_NONE) {
15331
15332 if (qual == SATA_ADDR_CPORT) {
15333 if (sata_device->satadev_type == SATA_DTYPE_PMULT) {
15334 sata_log(sata_hba_inst, CE_WARN,
15335 "SATA port multiplier detected "
15336 "at port %d", cport);
15337 } else {
15338 sata_log(sata_hba_inst, CE_WARN,
15339 "SATA device detected at port %d", cport);
15340 if (sata_device->satadev_type ==
15341 SATA_DTYPE_UNKNOWN) {
15342 /*
15343 * A device was not successfully identified
15344 */
15345 sata_log(sata_hba_inst, CE_WARN,
15346 "Could not identify SATA "
15347 "device at port %d", cport);
15348 }
15349 }
15350 } else { /* port multiplier device port */
15351 sata_log(sata_hba_inst, CE_WARN,
15352 "SATA device detected at port %d:%d",
15353 cport, pmport);
15354 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
15355 /*
15356 * A device was not successfully identified
15357 */
15358 sata_log(sata_hba_inst, CE_WARN,
15359 "Could not identify SATA "
15360 "device at port %d:%d", cport, pmport);
15361 }
15362 }
15363 }
15364
15365 return (rv);
15366 }
15367
15368
15369 /*
15370 * Process sata device unconfigure request.
15371 * The unconfigure operation uses generic nexus operation to
15372 * offline a device. It leaves a target device node attached.
15373 * and obviously sata_drive_info attached as well, because
15374 * from the hardware point of view nothing has changed.
15375 */
15376 static int
sata_ioctl_unconfigure(sata_hba_inst_t * sata_hba_inst,sata_device_t * sata_device)15377 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst,
15378 sata_device_t *sata_device)
15379 {
15380 int rv = 0;
15381 dev_info_t *tdip;
15382
15383 /* We are addressing attached device, not a port */
15384 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
15385 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
15386 else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT)
15387 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15388
15389 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
15390 &sata_device->satadev_addr)) != NULL) {
15391
15392 if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) {
15393 SATA_LOG_D((sata_hba_inst, CE_WARN,
15394 "sata_hba_ioctl: unconfigure: "
15395 "failed to unconfigure device at SATA port %d:%d",
15396 sata_device->satadev_addr.cport,
15397 sata_device->satadev_addr.pmport));
15398 rv = EIO;
15399 }
15400 /*
15401 * The target node devi_state should be marked with
15402 * DEVI_DEVICE_OFFLINE by ndi_devi_offline().
15403 * This would be the indication for cfgadm that
15404 * the AP node occupant state is 'unconfigured'.
15405 */
15406
15407 } else {
15408 /*
15409 * This would indicate a failure on the part of cfgadm
15410 * to detect correct state of the node prior to this
15411 * call - one cannot unconfigure non-existing device.
15412 */
15413 SATA_LOG_D((sata_hba_inst, CE_WARN,
15414 "sata_hba_ioctl: unconfigure: "
15415 "attempt to unconfigure non-existing device "
15416 "at SATA port %d:%d",
15417 sata_device->satadev_addr.cport,
15418 sata_device->satadev_addr.pmport));
15419 rv = ENXIO;
15420 }
15421 return (rv);
15422 }
15423
15424 /*
15425 * Process sata device configure request
15426 * If port is in a failed state, operation is aborted - one has to use
15427 * an explicit connect or port activate request to try to get a port into
15428 * non-failed mode. Port reset wil also work in such situation.
15429 * If the port is in disconnected (shutdown) state, the connect operation is
15430 * attempted prior to any other action.
15431 * When port is in the active state, there is a device attached and the target
15432 * node exists, a device was most likely offlined.
15433 * If target node does not exist, a new target node is created. In both cases
15434 * an attempt is made to online (configure) the device.
15435 *
15436 * NOTE: Port multiplier is supported.
15437 */
15438 static int
sata_ioctl_configure(sata_hba_inst_t * sata_hba_inst,sata_device_t * sata_device)15439 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst,
15440 sata_device_t *sata_device)
15441 {
15442 int cport, pmport, qual;
15443 int rval;
15444 boolean_t target = B_TRUE;
15445 sata_cport_info_t *cportinfo;
15446 sata_pmport_info_t *pmportinfo = NULL;
15447 dev_info_t *tdip;
15448 sata_drive_info_t *sdinfo;
15449
15450 cport = sata_device->satadev_addr.cport;
15451 pmport = sata_device->satadev_addr.pmport;
15452 qual = sata_device->satadev_addr.qual;
15453
15454 /* Get current port state */
15455 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
15456 (SATA_DIP(sata_hba_inst), sata_device);
15457
15458 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
15459 if (qual == SATA_ADDR_DPMPORT) {
15460 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
15461 mutex_enter(&pmportinfo->pmport_mutex);
15462 sata_update_pmport_info(sata_hba_inst, sata_device);
15463 if (rval != SATA_SUCCESS ||
15464 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
15465 /*
15466 * Obviously, device on a failed port is not visible
15467 */
15468 mutex_exit(&pmportinfo->pmport_mutex);
15469 return (ENXIO);
15470 }
15471 mutex_exit(&pmportinfo->pmport_mutex);
15472 } else {
15473 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15474 cport)->cport_mutex);
15475 sata_update_port_info(sata_hba_inst, sata_device);
15476 if (rval != SATA_SUCCESS ||
15477 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
15478 /*
15479 * Obviously, device on a failed port is not visible
15480 */
15481 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15482 cport)->cport_mutex);
15483 return (ENXIO);
15484 }
15485 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15486 cport)->cport_mutex);
15487 }
15488
15489 if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) {
15490 /* need to activate port */
15491 target = B_FALSE;
15492
15493 /* Sanity check */
15494 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
15495 return (ENXIO);
15496
15497 /* Just let HBA driver to activate port */
15498 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
15499 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15500 /*
15501 * Port activation failure - do not change port state
15502 * unless the state returned by HBA indicates a port
15503 * failure.
15504 */
15505 if (qual == SATA_ADDR_DPMPORT) {
15506 mutex_enter(&pmportinfo->pmport_mutex);
15507 sata_update_pmport_info(sata_hba_inst,
15508 sata_device);
15509 if (sata_device->satadev_state &
15510 SATA_PSTATE_FAILED)
15511 pmportinfo->pmport_state =
15512 SATA_PSTATE_FAILED;
15513 mutex_exit(&pmportinfo->pmport_mutex);
15514 } else {
15515 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15516 cport)->cport_mutex);
15517 sata_update_port_info(sata_hba_inst,
15518 sata_device);
15519 if (sata_device->satadev_state &
15520 SATA_PSTATE_FAILED)
15521 cportinfo->cport_state =
15522 SATA_PSTATE_FAILED;
15523 mutex_exit(&SATA_CPORT_INFO(
15524 sata_hba_inst, cport)->cport_mutex);
15525 }
15526 }
15527 SATA_LOG_D((sata_hba_inst, CE_WARN,
15528 "sata_hba_ioctl: configure: "
15529 "failed to activate SATA port %d:%d",
15530 cport, pmport));
15531 return (EIO);
15532 }
15533 /*
15534 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15535 * without the hint.
15536 */
15537 sata_gen_sysevent(sata_hba_inst,
15538 &sata_device->satadev_addr, SE_NO_HINT);
15539
15540 /* Virgin port state */
15541 if (qual == SATA_ADDR_DPMPORT) {
15542 mutex_enter(&pmportinfo->pmport_mutex);
15543 pmportinfo->pmport_state = 0;
15544 mutex_exit(&pmportinfo->pmport_mutex);
15545 } else {
15546 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15547 cport)-> cport_mutex);
15548 cportinfo->cport_state = 0;
15549 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15550 cport)->cport_mutex);
15551 }
15552 /*
15553 * Always reprobe port, to get current device info.
15554 */
15555 if (sata_reprobe_port(sata_hba_inst, sata_device,
15556 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
15557 return (EIO);
15558
15559 if (sata_device->satadev_type != SATA_DTYPE_NONE && target == B_FALSE) {
15560 if (qual == SATA_ADDR_DPMPORT) {
15561 /*
15562 * That's the transition from "inactive" port
15563 * to active one with device attached.
15564 */
15565 sata_log(sata_hba_inst, CE_WARN,
15566 "SATA device detected at port %d:%d",
15567 cport, pmport);
15568 } else {
15569 /*
15570 * When PM is attached to the cport and cport is
15571 * activated, every PM device port needs to be reprobed.
15572 * We need to emit message for all devices detected
15573 * at port multiplier's device ports.
15574 * Add such code here.
15575 * For now, just inform about device attached to
15576 * cport.
15577 */
15578 sata_log(sata_hba_inst, CE_WARN,
15579 "SATA device detected at port %d", cport);
15580 }
15581 }
15582
15583 /*
15584 * This is where real configuration operation starts.
15585 *
15586 * When PM is attached to the cport and cport is activated,
15587 * devices attached PM device ports may have to be configured
15588 * explicitly. This may change when port multiplier is supported.
15589 * For now, configure only disks and other valid target devices.
15590 */
15591 if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) {
15592 if (qual == SATA_ADDR_DCPORT) {
15593 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
15594 /*
15595 * A device was not successfully identified
15596 */
15597 sata_log(sata_hba_inst, CE_WARN,
15598 "Could not identify SATA "
15599 "device at port %d", cport);
15600 }
15601 } else { /* port multiplier device port */
15602 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
15603 /*
15604 * A device was not successfully identified
15605 */
15606 sata_log(sata_hba_inst, CE_WARN,
15607 "Could not identify SATA "
15608 "device at port %d:%d", cport, pmport);
15609 }
15610 }
15611 return (ENXIO); /* No device to configure */
15612 }
15613
15614 /*
15615 * Here we may have a device in reset condition,
15616 * but because we are just configuring it, there is
15617 * no need to process the reset other than just
15618 * to clear device reset condition in the HBA driver.
15619 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will
15620 * cause a first command sent the HBA driver with the request
15621 * to clear device reset condition.
15622 */
15623 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15624 if (qual == SATA_ADDR_DPMPORT)
15625 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15626 else
15627 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
15628 sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
15629 if (sdinfo == NULL) {
15630 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15631 return (ENXIO);
15632 }
15633 if (sdinfo->satadrv_event_flags &
15634 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
15635 sdinfo->satadrv_event_flags = 0;
15636 }
15637 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
15638 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15639
15640 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
15641 &sata_device->satadev_addr)) != NULL) {
15642 /*
15643 * Target node exists. Verify, that it belongs
15644 * to existing, attached device and not to
15645 * a removed device.
15646 */
15647 if (sata_check_device_removed(tdip) == B_TRUE) {
15648 if (qual == SATA_ADDR_DPMPORT)
15649 sata_log(sata_hba_inst, CE_WARN,
15650 "SATA device at port %d cannot be "
15651 "configured. "
15652 "Application(s) accessing "
15653 "previously attached device "
15654 "have to release it before newly "
15655 "inserted device can be made accessible.",
15656 cport);
15657 else
15658 sata_log(sata_hba_inst, CE_WARN,
15659 "SATA device at port %d:%d cannot be"
15660 "configured. "
15661 "Application(s) accessing "
15662 "previously attached device "
15663 "have to release it before newly "
15664 "inserted device can be made accessible.",
15665 cport, pmport);
15666 return (EIO);
15667 }
15668 /*
15669 * Device was not removed and re-inserted.
15670 * Try to online it.
15671 */
15672 if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) {
15673 SATA_LOG_D((sata_hba_inst, CE_WARN,
15674 "sata_hba_ioctl: configure: "
15675 "onlining device at SATA port "
15676 "%d:%d failed", cport, pmport));
15677 return (EIO);
15678 }
15679
15680 if (qual == SATA_ADDR_DPMPORT) {
15681 mutex_enter(&pmportinfo->pmport_mutex);
15682 pmportinfo->pmport_tgtnode_clean = B_TRUE;
15683 mutex_exit(&pmportinfo->pmport_mutex);
15684 } else {
15685 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15686 cport)->cport_mutex);
15687 cportinfo-> cport_tgtnode_clean = B_TRUE;
15688 mutex_exit(&SATA_CPORT_INFO(
15689 sata_hba_inst, cport)->cport_mutex);
15690 }
15691 } else {
15692 /*
15693 * No target node - need to create a new target node.
15694 */
15695 if (qual == SATA_ADDR_DPMPORT) {
15696 mutex_enter(&pmportinfo->pmport_mutex);
15697 pmportinfo->pmport_tgtnode_clean = B_TRUE;
15698 mutex_exit(&pmportinfo->pmport_mutex);
15699 } else {
15700 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15701 cport_mutex);
15702 cportinfo-> cport_tgtnode_clean = B_TRUE;
15703 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15704 cport_mutex);
15705 }
15706
15707 tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
15708 sata_hba_inst, &sata_device->satadev_addr);
15709 if (tdip == NULL) {
15710 /* Configure operation failed */
15711 SATA_LOG_D((sata_hba_inst, CE_WARN,
15712 "sata_hba_ioctl: configure: "
15713 "configuring SATA device at port %d:%d "
15714 "failed", cport, pmport));
15715 return (EIO);
15716 }
15717 }
15718 return (0);
15719 }
15720
15721
15722 /*
15723 * Process ioctl deactivate port request.
15724 * Arbitrarily unconfigure attached device, if any.
15725 * Even if the unconfigure fails, proceed with the
15726 * port deactivation.
15727 *
15728 * NOTE: Port Multiplier is supported now.
15729 */
15730
15731 static int
sata_ioctl_deactivate(sata_hba_inst_t * sata_hba_inst,sata_device_t * sata_device)15732 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst,
15733 sata_device_t *sata_device)
15734 {
15735 int cport, pmport, qual;
15736 int rval, rv = 0;
15737 int npmport;
15738 sata_cport_info_t *cportinfo;
15739 sata_pmport_info_t *pmportinfo;
15740 sata_pmult_info_t *pmultinfo;
15741 dev_info_t *tdip;
15742 sata_drive_info_t *sdinfo = NULL;
15743 sata_device_t subsdevice;
15744
15745 /* Sanity check */
15746 if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL)
15747 return (ENOTSUP);
15748
15749 cport = sata_device->satadev_addr.cport;
15750 pmport = sata_device->satadev_addr.pmport;
15751 qual = sata_device->satadev_addr.qual;
15752
15753 /* SCSI_TO_SATA_ADDR_QUAL() translate ap_id into a device qualifier */
15754 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
15755 if (qual == SATA_ADDR_DCPORT)
15756 qual = SATA_ADDR_CPORT;
15757 else
15758 qual = SATA_ADDR_PMPORT;
15759
15760 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
15761 if (qual == SATA_ADDR_PMPORT)
15762 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
15763
15764 /*
15765 * Processing port multiplier
15766 */
15767 if (qual == SATA_ADDR_CPORT &&
15768 SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) {
15769 mutex_enter(&cportinfo->cport_mutex);
15770
15771 /* Deactivate all sub-deices */
15772 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
15773 if (pmultinfo != NULL) {
15774 for (npmport = 0; npmport < SATA_NUM_PMPORTS(
15775 sata_hba_inst, cport); npmport++) {
15776
15777 subsdevice.satadev_addr.cport = cport;
15778 subsdevice.satadev_addr.pmport =
15779 (uint8_t)npmport;
15780 subsdevice.satadev_addr.qual =
15781 SATA_ADDR_DPMPORT;
15782
15783 SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
15784 "sata_hba_ioctl: deactivate: trying to "
15785 "deactivate SATA port %d:%d",
15786 cport, npmport);
15787
15788 mutex_exit(&cportinfo->cport_mutex);
15789 if (sata_ioctl_deactivate(sata_hba_inst,
15790 &subsdevice) == SATA_SUCCESS) {
15791 SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
15792 "[Deactivate] device at port %d:%d "
15793 "successfully.", cport, npmport);
15794 }
15795 mutex_enter(&cportinfo->cport_mutex);
15796 }
15797 }
15798
15799 /* Deactivate the port multiplier now. */
15800 cportinfo->cport_state &= ~SATA_STATE_READY;
15801 mutex_exit(&cportinfo->cport_mutex);
15802
15803 sata_device->satadev_addr.qual = qual;
15804 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
15805 (SATA_DIP(sata_hba_inst), sata_device);
15806
15807 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15808 SE_NO_HINT);
15809
15810 mutex_enter(&cportinfo->cport_mutex);
15811 sata_update_port_info(sata_hba_inst, sata_device);
15812 if (rval != SATA_SUCCESS) {
15813 if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15814 cportinfo->cport_state = SATA_PSTATE_FAILED;
15815 }
15816 rv = EIO;
15817 } else {
15818 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
15819 }
15820 mutex_exit(&cportinfo->cport_mutex);
15821
15822 return (rv);
15823 }
15824
15825 /*
15826 * Process non-port-multiplier device - it could be a drive connected
15827 * to a port multiplier port or a controller port.
15828 */
15829 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15830 if (qual == SATA_ADDR_CPORT) {
15831 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
15832 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
15833 /* deal only with valid devices */
15834 if ((cportinfo->cport_dev_type &
15835 SATA_VALID_DEV_TYPE) != 0)
15836 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
15837 }
15838 cportinfo->cport_state &= ~SATA_STATE_READY;
15839 } else {
15840 /* Port multiplier device port */
15841 mutex_enter(&pmportinfo->pmport_mutex);
15842 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15843 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
15844 (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0)
15845 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
15846 pmportinfo->pmport_state &= ~SATA_STATE_READY;
15847 mutex_exit(&pmportinfo->pmport_mutex);
15848 }
15849
15850 if (sdinfo != NULL) {
15851 /*
15852 * If a target node exists, try to offline a device and
15853 * to remove a target node.
15854 */
15855 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15856 cport_mutex);
15857 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
15858 &sata_device->satadev_addr);
15859 if (tdip != NULL) {
15860 /* target node exist */
15861 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
15862 "sata_hba_ioctl: port deactivate: "
15863 "target node exists.", NULL);
15864
15865 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) !=
15866 NDI_SUCCESS) {
15867 SATA_LOG_D((sata_hba_inst, CE_WARN,
15868 "sata_hba_ioctl: port deactivate: "
15869 "failed to unconfigure device at port "
15870 "%d:%d before deactivating the port",
15871 cport, pmport));
15872 /*
15873 * Set DEVICE REMOVED state in the target
15874 * node. It will prevent an access to
15875 * the device even when a new device is
15876 * attached, until the old target node is
15877 * released, removed and recreated for a new
15878 * device.
15879 */
15880 sata_set_device_removed(tdip);
15881
15882 /*
15883 * Instruct the event daemon to try the
15884 * target node cleanup later.
15885 */
15886 sata_set_target_node_cleanup(sata_hba_inst,
15887 &sata_device->satadev_addr);
15888 }
15889 }
15890 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15891 cport_mutex);
15892 /*
15893 * In any case, remove and release sata_drive_info
15894 * structure.
15895 */
15896 if (qual == SATA_ADDR_CPORT) {
15897 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
15898 cportinfo->cport_dev_type = SATA_DTYPE_NONE;
15899 } else { /* port multiplier device port */
15900 mutex_enter(&pmportinfo->pmport_mutex);
15901 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
15902 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
15903 mutex_exit(&pmportinfo->pmport_mutex);
15904 }
15905 (void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
15906 }
15907
15908 if (qual == SATA_ADDR_CPORT) {
15909 cportinfo->cport_state &= ~(SATA_STATE_PROBED |
15910 SATA_STATE_PROBING);
15911 } else if (qual == SATA_ADDR_PMPORT) {
15912 mutex_enter(&pmportinfo->pmport_mutex);
15913 pmportinfo->pmport_state &= ~(SATA_STATE_PROBED |
15914 SATA_STATE_PROBING);
15915 mutex_exit(&pmportinfo->pmport_mutex);
15916 }
15917 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15918
15919 /* Just let HBA driver to deactivate port */
15920 sata_device->satadev_addr.qual = qual;
15921 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
15922 (SATA_DIP(sata_hba_inst), sata_device);
15923
15924 /*
15925 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15926 * without the hint
15927 */
15928 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15929 SE_NO_HINT);
15930
15931 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15932 sata_update_port_info(sata_hba_inst, sata_device);
15933 if (qual == SATA_ADDR_CPORT) {
15934 if (rval != SATA_SUCCESS) {
15935 /*
15936 * Port deactivation failure - do not change port state
15937 * unless the state returned by HBA indicates a port
15938 * failure.
15939 */
15940 if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15941 SATA_CPORT_STATE(sata_hba_inst, cport) =
15942 SATA_PSTATE_FAILED;
15943 }
15944 SATA_LOG_D((sata_hba_inst, CE_WARN,
15945 "sata_hba_ioctl: port deactivate: "
15946 "cannot deactivate SATA port %d", cport));
15947 rv = EIO;
15948 } else {
15949 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
15950 }
15951 } else {
15952 mutex_enter(&pmportinfo->pmport_mutex);
15953 if (rval != SATA_SUCCESS) {
15954 if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15955 SATA_PMPORT_STATE(sata_hba_inst, cport,
15956 pmport) = SATA_PSTATE_FAILED;
15957 }
15958 SATA_LOG_D((sata_hba_inst, CE_WARN,
15959 "sata_hba_ioctl: port deactivate: "
15960 "cannot deactivate SATA port %d:%d",
15961 cport, pmport));
15962 rv = EIO;
15963 } else {
15964 pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
15965 }
15966 mutex_exit(&pmportinfo->pmport_mutex);
15967 }
15968
15969 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15970
15971 return (rv);
15972 }
15973
15974 /*
15975 * Process ioctl port activate request.
15976 *
15977 * NOTE: Port multiplier is supported now.
15978 */
15979 static int
sata_ioctl_activate(sata_hba_inst_t * sata_hba_inst,sata_device_t * sata_device)15980 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst,
15981 sata_device_t *sata_device)
15982 {
15983 int cport, pmport, qual;
15984 sata_cport_info_t *cportinfo;
15985 sata_pmport_info_t *pmportinfo = NULL;
15986 boolean_t dev_existed = B_TRUE;
15987
15988 /* Sanity check */
15989 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
15990 return (ENOTSUP);
15991
15992 cport = sata_device->satadev_addr.cport;
15993 pmport = sata_device->satadev_addr.pmport;
15994 qual = sata_device->satadev_addr.qual;
15995
15996 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
15997
15998 /*
15999 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL())
16000 * is a device. But what we are dealing with is port/pmport.
16001 */
16002 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
16003 if (qual == SATA_ADDR_DCPORT)
16004 sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT;
16005 else
16006 sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT;
16007
16008 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
16009 if (qual == SATA_ADDR_PMPORT) {
16010 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
16011 if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN ||
16012 pmportinfo->pmport_dev_type == SATA_DTYPE_NONE)
16013 dev_existed = B_FALSE;
16014 } else { /* cport */
16015 if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN ||
16016 cportinfo->cport_dev_type == SATA_DTYPE_NONE)
16017 dev_existed = B_FALSE;
16018 }
16019 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
16020
16021 /* Just let HBA driver to activate port, if necessary */
16022 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
16023 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
16024 /*
16025 * Port activation failure - do not change port state unless
16026 * the state returned by HBA indicates a port failure.
16027 */
16028 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16029 cport)->cport_mutex);
16030 sata_update_port_info(sata_hba_inst, sata_device);
16031 if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
16032 if (qual == SATA_ADDR_PMPORT) {
16033 mutex_enter(&pmportinfo->pmport_mutex);
16034 pmportinfo->pmport_state = SATA_PSTATE_FAILED;
16035 mutex_exit(&pmportinfo->pmport_mutex);
16036 } else
16037 cportinfo->cport_state = SATA_PSTATE_FAILED;
16038
16039 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16040 cport)->cport_mutex);
16041 SATA_LOG_D((sata_hba_inst, CE_WARN,
16042 "sata_hba_ioctl: port activate: cannot activate "
16043 "SATA port %d:%d", cport, pmport));
16044 return (EIO);
16045 }
16046 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
16047 }
16048 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
16049 if (qual == SATA_ADDR_PMPORT) {
16050 mutex_enter(&pmportinfo->pmport_mutex);
16051 pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN;
16052 mutex_exit(&pmportinfo->pmport_mutex);
16053 } else
16054 cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN;
16055 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
16056
16057 /*
16058 * Re-probe port to find its current state and possibly attached device.
16059 * Port re-probing may change the cportinfo device type if device is
16060 * found attached.
16061 * If port probing failed, the device type would be set to
16062 * SATA_DTYPE_NONE.
16063 */
16064 (void) sata_reprobe_port(sata_hba_inst, sata_device,
16065 SATA_DEV_IDENTIFY_RETRY);
16066
16067 /*
16068 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
16069 * without the hint.
16070 */
16071 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
16072 SE_NO_HINT);
16073
16074 if (dev_existed == B_FALSE) {
16075 if (qual == SATA_ADDR_PMPORT &&
16076 pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
16077 /*
16078 * That's the transition from the "inactive" port state
16079 * or the active port without a device attached to the
16080 * active port state with a device attached.
16081 */
16082 sata_log(sata_hba_inst, CE_WARN,
16083 "SATA device detected at port %d:%d",
16084 cport, pmport);
16085 } else if (qual == SATA_ADDR_CPORT &&
16086 cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
16087 /*
16088 * That's the transition from the "inactive" port state
16089 * or the active port without a device attached to the
16090 * active port state with a device attached.
16091 */
16092 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
16093 sata_log(sata_hba_inst, CE_WARN,
16094 "SATA device detected at port %d", cport);
16095 } else {
16096 sata_log(sata_hba_inst, CE_WARN,
16097 "SATA port multiplier detected at port %d",
16098 cport);
16099 }
16100 }
16101 }
16102 return (0);
16103 }
16104
16105
16106
16107 /*
16108 * Process ioctl reset port request.
16109 *
16110 * NOTE: Port-Multiplier is supported.
16111 */
16112 static int
sata_ioctl_reset_port(sata_hba_inst_t * sata_hba_inst,sata_device_t * sata_device)16113 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst,
16114 sata_device_t *sata_device)
16115 {
16116 int cport, pmport, qual;
16117 int rv = 0;
16118
16119 cport = sata_device->satadev_addr.cport;
16120 pmport = sata_device->satadev_addr.pmport;
16121 qual = sata_device->satadev_addr.qual;
16122
16123 /*
16124 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL())
16125 * is a device. But what we are dealing with is port/pmport.
16126 */
16127 if (qual == SATA_ADDR_DCPORT)
16128 sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT;
16129 else
16130 sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT;
16131 ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
16132
16133 /* Sanity check */
16134 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
16135 SATA_LOG_D((sata_hba_inst, CE_WARN,
16136 "sata_hba_ioctl: sata_hba_tran missing required "
16137 "function sata_tran_reset_dport"));
16138 return (ENOTSUP);
16139 }
16140
16141 /* Ask HBA to reset port */
16142 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
16143 sata_device) != SATA_SUCCESS) {
16144 SATA_LOG_D((sata_hba_inst, CE_WARN,
16145 "sata_hba_ioctl: reset port: failed %d:%d",
16146 cport, pmport));
16147 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
16148 cport_mutex);
16149 sata_update_port_info(sata_hba_inst, sata_device);
16150 if (qual == SATA_ADDR_CPORT)
16151 SATA_CPORT_STATE(sata_hba_inst, cport) =
16152 SATA_PSTATE_FAILED;
16153 else {
16154 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport,
16155 pmport));
16156 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
16157 SATA_PSTATE_FAILED;
16158 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport,
16159 pmport));
16160 }
16161 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
16162 cport_mutex);
16163 rv = EIO;
16164 }
16165
16166 return (rv);
16167 }
16168
16169 /*
16170 * Process ioctl reset device request.
16171 *
16172 * NOTE: Port multiplier is supported.
16173 */
16174 static int
sata_ioctl_reset_device(sata_hba_inst_t * sata_hba_inst,sata_device_t * sata_device)16175 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst,
16176 sata_device_t *sata_device)
16177 {
16178 sata_drive_info_t *sdinfo = NULL;
16179 sata_pmult_info_t *pmultinfo = NULL;
16180 int cport, pmport;
16181 int rv = 0;
16182
16183 /* Sanity check */
16184 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
16185 SATA_LOG_D((sata_hba_inst, CE_WARN,
16186 "sata_hba_ioctl: sata_hba_tran missing required "
16187 "function sata_tran_reset_dport"));
16188 return (ENOTSUP);
16189 }
16190
16191 cport = sata_device->satadev_addr.cport;
16192 pmport = sata_device->satadev_addr.pmport;
16193
16194 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
16195 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
16196 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
16197 SATA_DTYPE_PMULT)
16198 pmultinfo = SATA_CPORT_INFO(sata_hba_inst, cport)->
16199 cport_devp.cport_sata_pmult;
16200 else
16201 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16202 sata_device->satadev_addr.cport);
16203 } else { /* port multiplier */
16204 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
16205 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16206 sata_device->satadev_addr.cport,
16207 sata_device->satadev_addr.pmport);
16208 }
16209 if (sdinfo == NULL && pmultinfo == NULL) {
16210 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
16211 return (EINVAL);
16212 }
16213 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
16214
16215 /* Ask HBA to reset device */
16216 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
16217 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
16218 SATA_LOG_D((sata_hba_inst, CE_WARN,
16219 "sata_hba_ioctl: reset device: failed at port %d:%d",
16220 cport, pmport));
16221 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
16222 cport_mutex);
16223 sata_update_port_info(sata_hba_inst, sata_device);
16224 /*
16225 * Device info structure remains attached. Another device reset
16226 * or port disconnect/connect and re-probing is
16227 * needed to change it's state
16228 */
16229 if (sdinfo != NULL) {
16230 sdinfo->satadrv_state &= ~SATA_STATE_READY;
16231 sdinfo->satadrv_state |= SATA_DSTATE_FAILED;
16232 } else if (pmultinfo != NULL) {
16233 pmultinfo->pmult_state &= ~SATA_STATE_READY;
16234 pmultinfo->pmult_state |= SATA_DSTATE_FAILED;
16235 }
16236
16237 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
16238 rv = EIO;
16239 }
16240 /*
16241 * If attached device was a port multiplier, some extra processing
16242 * may be needed to bring it back. SATA specification requies a
16243 * mandatory software reset on host port to reliably enumerate a port
16244 * multiplier, the HBA driver should handle that after reset
16245 * operation.
16246 */
16247 return (rv);
16248 }
16249
16250
16251 /*
16252 * Process ioctl reset all request.
16253 */
16254 static int
sata_ioctl_reset_all(sata_hba_inst_t * sata_hba_inst)16255 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst)
16256 {
16257 sata_device_t sata_device;
16258 int rv = 0;
16259 int tcport;
16260
16261 sata_device.satadev_rev = SATA_DEVICE_REV;
16262
16263 /*
16264 * There is no protection here for configured devices.
16265 */
16266 /* Sanity check */
16267 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
16268 SATA_LOG_D((sata_hba_inst, CE_WARN,
16269 "sata_hba_ioctl: sata_hba_tran missing required "
16270 "function sata_tran_reset_dport"));
16271 return (ENOTSUP);
16272 }
16273
16274 /*
16275 * Need to lock all ports, not just one.
16276 * If any port is locked by event processing, fail the whole operation.
16277 * One port is already locked, but for simplicity lock it again.
16278 */
16279 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
16280 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
16281 cport_mutex);
16282 if (((SATA_CPORT_INFO(sata_hba_inst, tcport)->
16283 cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) {
16284 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
16285 cport_mutex);
16286 rv = EBUSY;
16287 break;
16288 } else {
16289 /*
16290 * It is enough to lock cport in command-based
16291 * switching mode.
16292 */
16293 SATA_CPORT_INFO(sata_hba_inst, tcport)->
16294 cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
16295 }
16296 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
16297 cport_mutex);
16298 }
16299
16300 if (rv == 0) {
16301 /*
16302 * All cports were successfully locked.
16303 * Reset main SATA controller.
16304 * Set the device address to port 0, to have a valid device
16305 * address.
16306 */
16307 sata_device.satadev_addr.qual = SATA_ADDR_CNTRL;
16308 sata_device.satadev_addr.cport = 0;
16309 sata_device.satadev_addr.pmport = 0;
16310
16311 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
16312 (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) {
16313 SATA_LOG_D((sata_hba_inst, CE_WARN,
16314 "sata_hba_ioctl: reset controller failed"));
16315 return (EIO);
16316 }
16317 }
16318 /*
16319 * Unlock all ports
16320 */
16321 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
16322 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
16323 cport_mutex);
16324 SATA_CPORT_INFO(sata_hba_inst, tcport)->
16325 cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
16326 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
16327 cport_mutex);
16328 }
16329
16330 /*
16331 * This operation returns EFAULT if either reset
16332 * controller failed or a re-probing of any port failed.
16333 */
16334 return (rv);
16335 }
16336
16337
16338 /*
16339 * Process ioctl port self test request.
16340 *
16341 * NOTE: Port multiplier code is not completed nor tested.
16342 */
16343 static int
sata_ioctl_port_self_test(sata_hba_inst_t * sata_hba_inst,sata_device_t * sata_device)16344 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst,
16345 sata_device_t *sata_device)
16346 {
16347 int cport, pmport, qual;
16348 int rv = 0;
16349
16350 /* Sanity check */
16351 if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL)
16352 return (ENOTSUP);
16353
16354 cport = sata_device->satadev_addr.cport;
16355 pmport = sata_device->satadev_addr.pmport;
16356 qual = sata_device->satadev_addr.qual;
16357
16358 /*
16359 * There is no protection here for a configured
16360 * device attached to this port.
16361 */
16362
16363 if ((*SATA_SELFTEST_FUNC(sata_hba_inst))
16364 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
16365 SATA_LOG_D((sata_hba_inst, CE_WARN,
16366 "sata_hba_ioctl: port selftest: "
16367 "failed port %d:%d", cport, pmport));
16368 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
16369 cport_mutex);
16370 sata_update_port_info(sata_hba_inst, sata_device);
16371 if (qual == SATA_ADDR_CPORT)
16372 SATA_CPORT_STATE(sata_hba_inst, cport) =
16373 SATA_PSTATE_FAILED;
16374 else { /* port multiplier device port */
16375 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst,
16376 cport, pmport));
16377 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
16378 SATA_PSTATE_FAILED;
16379 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst,
16380 cport, pmport));
16381 }
16382
16383 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
16384 cport_mutex);
16385 return (EIO);
16386 }
16387 /*
16388 * Beacuse the port was reset in the course of testing, it should be
16389 * re-probed and attached device state should be restored. At this
16390 * point the port state is unknown - it's state is HBA-specific.
16391 * Force port re-probing to get it into a known state.
16392 */
16393 if (sata_reprobe_port(sata_hba_inst, sata_device,
16394 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
16395 rv = EIO;
16396 return (rv);
16397 }
16398
16399
16400 /*
16401 * sata_cfgadm_state:
16402 * Use the sata port state and state of the target node to figure out
16403 * the cfgadm_state.
16404 *
16405 * The port argument is a value with encoded cport,
16406 * pmport and address qualifier, in the same manner as a scsi target number.
16407 * SCSI_TO_SATA_CPORT macro extracts cport number,
16408 * SCSI_TO_SATA_PMPORT extracts pmport number and
16409 * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag.
16410 *
16411 * Port multiplier is supported.
16412 */
16413
16414 static void
sata_cfgadm_state(sata_hba_inst_t * sata_hba_inst,int32_t port,devctl_ap_state_t * ap_state)16415 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port,
16416 devctl_ap_state_t *ap_state)
16417 {
16418 uint8_t cport, pmport, qual;
16419 uint32_t port_state, pmult_state;
16420 uint32_t dev_type;
16421 sata_drive_info_t *sdinfo;
16422
16423 cport = SCSI_TO_SATA_CPORT(port);
16424 pmport = SCSI_TO_SATA_PMPORT(port);
16425 qual = SCSI_TO_SATA_ADDR_QUAL(port);
16426
16427 /* Check cport state */
16428 port_state = SATA_CPORT_STATE(sata_hba_inst, cport);
16429 if (port_state & SATA_PSTATE_SHUTDOWN ||
16430 port_state & SATA_PSTATE_FAILED) {
16431 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
16432 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16433 if (port_state & SATA_PSTATE_FAILED)
16434 ap_state->ap_condition = AP_COND_FAILED;
16435 else
16436 ap_state->ap_condition = AP_COND_UNKNOWN;
16437
16438 return;
16439 }
16440
16441 /* cport state is okay. Now check pmport state */
16442 if (qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) {
16443 /* Sanity check */
16444 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
16445 SATA_DTYPE_PMULT || SATA_PMPORT_INFO(sata_hba_inst,
16446 cport, pmport) == NULL)
16447 return;
16448 port_state = SATA_PMPORT_STATE(sata_hba_inst, cport, pmport);
16449 if (port_state & SATA_PSTATE_SHUTDOWN ||
16450 port_state & SATA_PSTATE_FAILED) {
16451 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
16452 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16453 if (port_state & SATA_PSTATE_FAILED)
16454 ap_state->ap_condition = AP_COND_FAILED;
16455 else
16456 ap_state->ap_condition = AP_COND_UNKNOWN;
16457
16458 return;
16459 }
16460 }
16461
16462 /* Port is enabled and ready */
16463 if (qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_CPORT)
16464 dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, cport);
16465 else
16466 dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, pmport);
16467
16468 switch (dev_type) {
16469 case SATA_DTYPE_NONE:
16470 {
16471 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16472 ap_state->ap_condition = AP_COND_OK;
16473 /* No device attached */
16474 ap_state->ap_rstate = AP_RSTATE_EMPTY;
16475 break;
16476 }
16477 case SATA_DTYPE_PMULT:
16478 {
16479 /* Need to check port multiplier state */
16480 ASSERT(qual == SATA_ADDR_DCPORT);
16481 pmult_state = SATA_PMULT_INFO(sata_hba_inst, cport)->
16482 pmult_state;
16483 if (pmult_state & (SATA_PSTATE_SHUTDOWN|SATA_PSTATE_FAILED)) {
16484 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
16485 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16486 if (pmult_state & SATA_PSTATE_FAILED)
16487 ap_state->ap_condition = AP_COND_FAILED;
16488 else
16489 ap_state->ap_condition = AP_COND_UNKNOWN;
16490
16491 return;
16492 }
16493
16494 /* Port multiplier is not configurable */
16495 ap_state->ap_ostate = AP_OSTATE_CONFIGURED;
16496 ap_state->ap_rstate = AP_RSTATE_CONNECTED;
16497 ap_state->ap_condition = AP_COND_OK;
16498 break;
16499 }
16500
16501 case SATA_DTYPE_ATADISK:
16502 case SATA_DTYPE_ATAPICD:
16503 case SATA_DTYPE_ATAPITAPE:
16504 case SATA_DTYPE_ATAPIDISK:
16505 {
16506 dev_info_t *tdip = NULL;
16507 dev_info_t *dip = NULL;
16508
16509 dip = SATA_DIP(sata_hba_inst);
16510 tdip = sata_get_target_dip(dip, cport, pmport);
16511 ap_state->ap_rstate = AP_RSTATE_CONNECTED;
16512 if (tdip != NULL) {
16513 ndi_devi_enter(dip);
16514 mutex_enter(&(DEVI(tdip)->devi_lock));
16515 if (DEVI_IS_DEVICE_REMOVED(tdip)) {
16516 /*
16517 * There could be the case where previously
16518 * configured and opened device was removed
16519 * and unknown device was plugged.
16520 * In such case we want to show a device, and
16521 * its configured or unconfigured state but
16522 * indicate unusable condition untill the
16523 * old target node is released and removed.
16524 */
16525 ap_state->ap_condition = AP_COND_UNUSABLE;
16526 } else {
16527 mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst,
16528 cport));
16529 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16530 cport);
16531 if (sdinfo != NULL) {
16532 if ((sdinfo->satadrv_state &
16533 SATA_DSTATE_FAILED) != 0)
16534 ap_state->ap_condition =
16535 AP_COND_FAILED;
16536 else
16537 ap_state->ap_condition =
16538 AP_COND_OK;
16539 } else {
16540 ap_state->ap_condition =
16541 AP_COND_UNKNOWN;
16542 }
16543 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst,
16544 cport));
16545 }
16546 if ((DEVI_IS_DEVICE_OFFLINE(tdip)) ||
16547 (DEVI_IS_DEVICE_DOWN(tdip))) {
16548 ap_state->ap_ostate =
16549 AP_OSTATE_UNCONFIGURED;
16550 } else {
16551 ap_state->ap_ostate =
16552 AP_OSTATE_CONFIGURED;
16553 }
16554 mutex_exit(&(DEVI(tdip)->devi_lock));
16555 ndi_devi_exit(dip);
16556 } else {
16557 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16558 ap_state->ap_condition = AP_COND_UNKNOWN;
16559 }
16560 break;
16561 }
16562 case SATA_DTYPE_ATAPIPROC:
16563 ap_state->ap_rstate = AP_RSTATE_CONNECTED;
16564 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16565 ap_state->ap_condition = AP_COND_OK;
16566 break;
16567 default:
16568 ap_state->ap_rstate = AP_RSTATE_CONNECTED;
16569 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16570 ap_state->ap_condition = AP_COND_UNKNOWN;
16571 /*
16572 * This is actually internal error condition (non fatal),
16573 * because we have already checked all defined device types.
16574 */
16575 SATA_LOG_D((sata_hba_inst, CE_WARN,
16576 "sata_cfgadm_state: Internal error: "
16577 "unknown device type"));
16578 break;
16579 }
16580 }
16581
16582
16583 /*
16584 * Process ioctl get device path request.
16585 *
16586 * NOTE: Port multiplier has no target dip. Devices connected to port
16587 * multiplier have target node attached to the HBA node. The only difference
16588 * between them and the directly-attached device node is a target address.
16589 */
16590 static int
sata_ioctl_get_device_path(sata_hba_inst_t * sata_hba_inst,sata_device_t * sata_device,sata_ioctl_data_t * ioc,int mode)16591 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst,
16592 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16593 {
16594 char path[MAXPATHLEN];
16595 uint32_t size;
16596 dev_info_t *tdip;
16597
16598 (void) strcpy(path, "/devices");
16599 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
16600 &sata_device->satadev_addr)) == NULL) {
16601 /*
16602 * No such device. If this is a request for a size, do not
16603 * return EINVAL for non-existing target, because cfgadm
16604 * will then indicate a meaningless ioctl failure.
16605 * If this is a request for a path, indicate invalid
16606 * argument.
16607 */
16608 if (ioc->get_size == 0)
16609 return (EINVAL);
16610 } else {
16611 (void) ddi_pathname(tdip, path + strlen(path));
16612 }
16613 size = strlen(path) + 1;
16614
16615 if (ioc->get_size != 0) {
16616 if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz,
16617 mode) != 0)
16618 return (EFAULT);
16619 } else {
16620 if (ioc->bufsiz != size)
16621 return (EINVAL);
16622
16623 else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz,
16624 mode) != 0)
16625 return (EFAULT);
16626 }
16627 return (0);
16628 }
16629
16630 /*
16631 * Process ioctl get attachment point type request.
16632 *
16633 * NOTE: Port multiplier is supported.
16634 */
16635 static int
sata_ioctl_get_ap_type(sata_hba_inst_t * sata_hba_inst,sata_device_t * sata_device,sata_ioctl_data_t * ioc,int mode)16636 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst,
16637 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16638 {
16639 uint32_t type_len;
16640 const char *ap_type;
16641 int dev_type;
16642
16643 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16644 dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst,
16645 sata_device->satadev_addr.cport);
16646 else /* pmport */
16647 dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst,
16648 sata_device->satadev_addr.cport,
16649 sata_device->satadev_addr.pmport);
16650
16651 switch (dev_type) {
16652 case SATA_DTYPE_NONE:
16653 ap_type = "port";
16654 break;
16655
16656 case SATA_DTYPE_ATADISK:
16657 case SATA_DTYPE_ATAPIDISK:
16658 ap_type = "disk";
16659 break;
16660
16661 case SATA_DTYPE_ATAPICD:
16662 ap_type = "cd/dvd";
16663 break;
16664
16665 case SATA_DTYPE_ATAPITAPE:
16666 ap_type = "tape";
16667 break;
16668
16669 case SATA_DTYPE_ATAPIPROC:
16670 ap_type = "processor";
16671 break;
16672
16673 case SATA_DTYPE_PMULT:
16674 ap_type = "sata-pmult";
16675 break;
16676
16677 case SATA_DTYPE_UNKNOWN:
16678 ap_type = "unknown";
16679 break;
16680
16681 default:
16682 ap_type = "unsupported";
16683 break;
16684
16685 } /* end of dev_type switch */
16686
16687 type_len = strlen(ap_type) + 1;
16688
16689 if (ioc->get_size) {
16690 if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz,
16691 mode) != 0)
16692 return (EFAULT);
16693 } else {
16694 if (ioc->bufsiz != type_len)
16695 return (EINVAL);
16696
16697 if (ddi_copyout((void *)ap_type, ioc->buf,
16698 ioc->bufsiz, mode) != 0)
16699 return (EFAULT);
16700 }
16701 return (0);
16702
16703 }
16704
16705 /*
16706 * Process ioctl get device model info request.
16707 * This operation should return to cfgadm the device model
16708 * information string
16709 *
16710 * NOTE: Port multiplier is supported.
16711 */
16712 static int
sata_ioctl_get_model_info(sata_hba_inst_t * sata_hba_inst,sata_device_t * sata_device,sata_ioctl_data_t * ioc,int mode)16713 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst,
16714 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16715 {
16716 sata_drive_info_t *sdinfo;
16717 uint32_t info_len;
16718 char ap_info[SATA_ID_MODEL_LEN + 1];
16719
16720 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16721 sata_device->satadev_addr.cport)->cport_mutex);
16722 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16723 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16724 sata_device->satadev_addr.cport);
16725 else /* port multiplier */
16726 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16727 sata_device->satadev_addr.cport,
16728 sata_device->satadev_addr.pmport);
16729 if (sdinfo == NULL) {
16730 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16731 sata_device->satadev_addr.cport)->cport_mutex);
16732 return (EINVAL);
16733 }
16734
16735 #ifdef _LITTLE_ENDIAN
16736 swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
16737 #else /* _LITTLE_ENDIAN */
16738 bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
16739 #endif /* _LITTLE_ENDIAN */
16740
16741 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16742 sata_device->satadev_addr.cport)->cport_mutex);
16743
16744 ap_info[SATA_ID_MODEL_LEN] = '\0';
16745
16746 info_len = strlen(ap_info) + 1;
16747
16748 if (ioc->get_size) {
16749 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16750 mode) != 0)
16751 return (EFAULT);
16752 } else {
16753 if (ioc->bufsiz < info_len)
16754 return (EINVAL);
16755 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16756 mode) != 0)
16757 return (EFAULT);
16758 }
16759 return (0);
16760 }
16761
16762
16763 /*
16764 * Process ioctl get device firmware revision info request.
16765 * This operation should return to cfgadm the device firmware revision
16766 * information string
16767 *
16768 * Port multiplier is supported.
16769 */
16770 static int
sata_ioctl_get_revfirmware_info(sata_hba_inst_t * sata_hba_inst,sata_device_t * sata_device,sata_ioctl_data_t * ioc,int mode)16771 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst,
16772 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16773 {
16774 sata_drive_info_t *sdinfo;
16775 uint32_t info_len;
16776 char ap_info[SATA_ID_FW_LEN + 1];
16777
16778 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16779 sata_device->satadev_addr.cport)->cport_mutex);
16780 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16781 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16782 sata_device->satadev_addr.cport);
16783 else /* port multiplier */
16784 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16785 sata_device->satadev_addr.cport,
16786 sata_device->satadev_addr.pmport);
16787 if (sdinfo == NULL) {
16788 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16789 sata_device->satadev_addr.cport)->cport_mutex);
16790 return (EINVAL);
16791 }
16792
16793 #ifdef _LITTLE_ENDIAN
16794 swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
16795 #else /* _LITTLE_ENDIAN */
16796 bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
16797 #endif /* _LITTLE_ENDIAN */
16798
16799 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16800 sata_device->satadev_addr.cport)->cport_mutex);
16801
16802 ap_info[SATA_ID_FW_LEN] = '\0';
16803
16804 info_len = strlen(ap_info) + 1;
16805
16806 if (ioc->get_size) {
16807 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16808 mode) != 0)
16809 return (EFAULT);
16810 } else {
16811 if (ioc->bufsiz < info_len)
16812 return (EINVAL);
16813 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16814 mode) != 0)
16815 return (EFAULT);
16816 }
16817 return (0);
16818 }
16819
16820
16821 /*
16822 * Process ioctl get device serial number info request.
16823 * This operation should return to cfgadm the device serial number string.
16824 *
16825 * NOTE: Port multiplier is supported.
16826 */
16827 static int
sata_ioctl_get_serialnumber_info(sata_hba_inst_t * sata_hba_inst,sata_device_t * sata_device,sata_ioctl_data_t * ioc,int mode)16828 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst,
16829 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16830 {
16831 sata_drive_info_t *sdinfo;
16832 uint32_t info_len;
16833 char ap_info[SATA_ID_SERIAL_LEN + 1];
16834
16835 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16836 sata_device->satadev_addr.cport)->cport_mutex);
16837 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16838 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16839 sata_device->satadev_addr.cport);
16840 else /* port multiplier */
16841 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16842 sata_device->satadev_addr.cport,
16843 sata_device->satadev_addr.pmport);
16844 if (sdinfo == NULL) {
16845 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16846 sata_device->satadev_addr.cport)->cport_mutex);
16847 return (EINVAL);
16848 }
16849
16850 #ifdef _LITTLE_ENDIAN
16851 swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
16852 #else /* _LITTLE_ENDIAN */
16853 bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
16854 #endif /* _LITTLE_ENDIAN */
16855
16856 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16857 sata_device->satadev_addr.cport)->cport_mutex);
16858
16859 ap_info[SATA_ID_SERIAL_LEN] = '\0';
16860
16861 info_len = strlen(ap_info) + 1;
16862
16863 if (ioc->get_size) {
16864 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16865 mode) != 0)
16866 return (EFAULT);
16867 } else {
16868 if (ioc->bufsiz < info_len)
16869 return (EINVAL);
16870 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16871 mode) != 0)
16872 return (EFAULT);
16873 }
16874 return (0);
16875 }
16876
16877
16878 /*
16879 * Preset scsi extended sense data (to NO SENSE)
16880 * First 18 bytes of the sense data are preset to current valid sense
16881 * with a key NO SENSE data.
16882 *
16883 * Returns void
16884 */
16885 static void
sata_fixed_sense_data_preset(struct scsi_extended_sense * sense)16886 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense)
16887 {
16888 sense->es_valid = 1; /* Valid sense */
16889 sense->es_class = CLASS_EXTENDED_SENSE; /* 0x70 - current err */
16890 sense->es_key = KEY_NO_SENSE;
16891 sense->es_info_1 = 0;
16892 sense->es_info_2 = 0;
16893 sense->es_info_3 = 0;
16894 sense->es_info_4 = 0;
16895 sense->es_add_len = 10; /* Additional length - replace with a def */
16896 sense->es_cmd_info[0] = 0;
16897 sense->es_cmd_info[1] = 0;
16898 sense->es_cmd_info[2] = 0;
16899 sense->es_cmd_info[3] = 0;
16900 sense->es_add_code = 0;
16901 sense->es_qual_code = 0;
16902 }
16903
16904 /*
16905 * Register a legacy cmdk-style devid for the target (disk) device.
16906 *
16907 * Note: This function is called only when the HBA devinfo node has the
16908 * property "use-cmdk-devid-format" set. This property indicates that
16909 * devid compatible with old cmdk (target) driver is to be generated
16910 * for any target device attached to this controller. This will take
16911 * precedence over the devid generated by sd (target) driver.
16912 * This function is derived from cmdk_devid_setup() function in cmdk.c.
16913 */
16914 static void
sata_target_devid_register(dev_info_t * dip,sata_drive_info_t * sdinfo)16915 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo)
16916 {
16917 char *hwid;
16918 int modlen;
16919 int serlen;
16920 int rval;
16921 ddi_devid_t devid;
16922
16923 /*
16924 * device ID is a concatanation of model number, "=", serial number.
16925 */
16926 hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP);
16927 bcopy(&sdinfo->satadrv_id.ai_model, hwid,
16928 sizeof (sdinfo->satadrv_id.ai_model));
16929 swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model));
16930 modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model));
16931 if (modlen == 0)
16932 goto err;
16933 hwid[modlen++] = '=';
16934 bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen],
16935 sizeof (sdinfo->satadrv_id.ai_drvser));
16936 swab(&hwid[modlen], &hwid[modlen],
16937 sizeof (sdinfo->satadrv_id.ai_drvser));
16938 serlen = sata_check_modser(&hwid[modlen],
16939 sizeof (sdinfo->satadrv_id.ai_drvser));
16940 if (serlen == 0)
16941 goto err;
16942 hwid[modlen + serlen] = 0; /* terminate the hwid string */
16943
16944 /* initialize/register devid */
16945 if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL,
16946 (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS) {
16947 rval = ddi_devid_register(dip, devid);
16948 /*
16949 * Free up the allocated devid buffer.
16950 * NOTE: This doesn't mean unregistering devid.
16951 */
16952 ddi_devid_free(devid);
16953 }
16954
16955 if (rval != DDI_SUCCESS)
16956 cmn_err(CE_WARN, "sata: failed to create devid for the disk"
16957 " on port %d", sdinfo->satadrv_addr.cport);
16958 err:
16959 kmem_free(hwid, LEGACY_HWID_LEN);
16960 }
16961
16962 /*
16963 * valid model/serial string must contain a non-zero non-space characters.
16964 * trim trailing spaces/NULLs.
16965 */
16966 static int
sata_check_modser(char * buf,int buf_len)16967 sata_check_modser(char *buf, int buf_len)
16968 {
16969 boolean_t ret;
16970 char *s;
16971 int i;
16972 int tb;
16973 char ch;
16974
16975 ret = B_FALSE;
16976 s = buf;
16977 for (i = 0; i < buf_len; i++) {
16978 ch = *s++;
16979 if (ch != ' ' && ch != '\0')
16980 tb = i + 1;
16981 if (ch != ' ' && ch != '\0' && ch != '0')
16982 ret = B_TRUE;
16983 }
16984
16985 if (ret == B_FALSE)
16986 return (0); /* invalid string */
16987
16988 return (tb); /* return length */
16989 }
16990
16991 /*
16992 * sata_set_drive_features function compares current device features setting
16993 * with the saved device features settings and, if there is a difference,
16994 * it restores device features setting to the previously saved state.
16995 * It also arbitrarily tries to select the highest supported DMA mode.
16996 * Device Identify or Identify Packet Device data has to be current.
16997 * At the moment read ahead and write cache are considered for all devices.
16998 * For atapi devices, Removable Media Status Notification is set in addition
16999 * to common features.
17000 *
17001 * This function cannot be called in the interrupt context (it may sleep).
17002 *
17003 * The input argument sdinfo should point to the drive info structure
17004 * to be updated after features are set. Note, that only
17005 * device (packet) identify data is updated, not the flags indicating the
17006 * supported features.
17007 *
17008 * Returns SATA_SUCCESS if successful or there was nothing to do.
17009 * Device Identify data in the drive info structure pointed to by the sdinfo
17010 * arguments is updated even when no features were set or changed.
17011 *
17012 * Returns SATA_FAILURE if device features could not be set or DMA mode
17013 * for a disk cannot be set and device identify data cannot be fetched.
17014 *
17015 * Returns SATA_RETRY if device features could not be set (other than disk
17016 * DMA mode) but the device identify data was fetched successfully.
17017 *
17018 * Note: This function may fail the port, making it inaccessible.
17019 * In such case the explicit port disconnect/connect or physical device
17020 * detach/attach is required to re-evaluate port state again.
17021 */
17022
17023 static int
sata_set_drive_features(sata_hba_inst_t * sata_hba_inst,sata_drive_info_t * sdinfo,int restore)17024 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst,
17025 sata_drive_info_t *sdinfo, int restore)
17026 {
17027 int rval = SATA_SUCCESS;
17028 int rval_set;
17029 sata_drive_info_t new_sdinfo;
17030 char *finfo = "sata_set_drive_features: cannot";
17031 char *finfox;
17032 int cache_op;
17033
17034 bzero(&new_sdinfo, sizeof (sata_drive_info_t));
17035 new_sdinfo.satadrv_addr = sdinfo->satadrv_addr;
17036 new_sdinfo.satadrv_type = sdinfo->satadrv_type;
17037 if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
17038 /*
17039 * Cannot get device identification - caller may retry later
17040 */
17041 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
17042 "%s fetch device identify data\n", finfo);
17043 return (SATA_FAILURE);
17044 }
17045 finfox = (restore != 0) ? " restore device features" :
17046 " initialize device features\n";
17047
17048 switch (sdinfo->satadrv_type) {
17049 case SATA_DTYPE_ATADISK:
17050 /* Arbitrarily set UDMA mode */
17051 if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
17052 SATA_SUCCESS) {
17053 SATA_LOG_D((sata_hba_inst, CE_WARN,
17054 "%s set UDMA mode\n", finfo));
17055 return (SATA_FAILURE);
17056 }
17057 break;
17058 case SATA_DTYPE_ATAPICD:
17059 case SATA_DTYPE_ATAPITAPE:
17060 case SATA_DTYPE_ATAPIDISK:
17061 /* Set Removable Media Status Notification, if necessary */
17062 if (SATA_RM_NOTIFIC_SUPPORTED(new_sdinfo.satadrv_id) &&
17063 restore != 0) {
17064 if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) &&
17065 (!SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id)))||
17066 ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) &&
17067 SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id))) {
17068 /* Current setting does not match saved one */
17069 if (sata_set_rmsn(sata_hba_inst, sdinfo,
17070 sdinfo->satadrv_settings &
17071 SATA_DEV_RMSN) != SATA_SUCCESS)
17072 rval = SATA_FAILURE;
17073 }
17074 }
17075 /*
17076 * We have to set Multiword DMA or UDMA, if it is supported, as
17077 * we want to use DMA transfer mode whenever possible.
17078 * Some devices require explicit setting of the DMA mode.
17079 */
17080 if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) {
17081 /* Set highest supported DMA mode */
17082 if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
17083 SATA_SUCCESS) {
17084 SATA_LOG_D((sata_hba_inst, CE_WARN,
17085 "%s set UDMA mode\n", finfo));
17086 rval = SATA_FAILURE;
17087 }
17088 }
17089 break;
17090 }
17091
17092 if (!SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id) &&
17093 !SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
17094 /*
17095 * neither READ AHEAD nor WRITE CACHE is supported
17096 * - do nothing
17097 */
17098 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
17099 "settable features not supported\n", NULL);
17100 goto update_sdinfo;
17101 }
17102
17103 if ((SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id) &&
17104 (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) &&
17105 (SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id) &&
17106 (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
17107 /*
17108 * both READ AHEAD and WRITE CACHE are enabled
17109 * - Nothing to do
17110 */
17111 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
17112 "no device features to set\n", NULL);
17113 goto update_sdinfo;
17114 }
17115
17116 cache_op = 0;
17117
17118 if (SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id)) {
17119 if ((sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
17120 !SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
17121 /* Enable read ahead / read cache */
17122 cache_op = SATAC_SF_ENABLE_READ_AHEAD;
17123 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
17124 "enabling read cache\n", NULL);
17125 } else if (!(sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
17126 SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
17127 /* Disable read ahead / read cache */
17128 cache_op = SATAC_SF_DISABLE_READ_AHEAD;
17129 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
17130 "disabling read cache\n", NULL);
17131 }
17132
17133 if (cache_op != 0) {
17134 /* Try to set read cache mode */
17135 rval_set = sata_set_cache_mode(sata_hba_inst,
17136 &new_sdinfo, cache_op);
17137 if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
17138 rval = rval_set;
17139 }
17140 }
17141
17142 cache_op = 0;
17143
17144 if (SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
17145 if ((sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
17146 !SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
17147 /* Enable write cache */
17148 cache_op = SATAC_SF_ENABLE_WRITE_CACHE;
17149 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
17150 "enabling write cache\n", NULL);
17151 } else if (!(sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
17152 SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
17153 /* Disable write cache */
17154 cache_op = SATAC_SF_DISABLE_WRITE_CACHE;
17155 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
17156 "disabling write cache\n", NULL);
17157 }
17158
17159 if (cache_op != 0) {
17160 /* Try to set write cache mode */
17161 rval_set = sata_set_cache_mode(sata_hba_inst,
17162 &new_sdinfo, cache_op);
17163 if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
17164 rval = rval_set;
17165 }
17166 }
17167 if (rval != SATA_SUCCESS)
17168 SATA_LOG_D((sata_hba_inst, CE_WARN,
17169 "%s %s", finfo, finfox));
17170
17171 update_sdinfo:
17172 /*
17173 * We need to fetch Device Identify data again
17174 */
17175 if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
17176 /*
17177 * Cannot get device identification - retry later
17178 */
17179 SATA_LOG_D((sata_hba_inst, CE_WARN,
17180 "%s re-fetch device identify data\n", finfo));
17181 rval = SATA_FAILURE;
17182 }
17183 /* Copy device sata info. */
17184 sdinfo->satadrv_id = new_sdinfo.satadrv_id;
17185
17186 return (rval);
17187 }
17188
17189
17190 /*
17191 *
17192 * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if
17193 * unable to determine.
17194 *
17195 * Cannot be called in an interrupt context.
17196 *
17197 * Called by sata_build_lsense_page_2f()
17198 */
17199
17200 static int
sata_fetch_smart_return_status(sata_hba_inst_t * sata_hba_inst,sata_drive_info_t * sdinfo)17201 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst,
17202 sata_drive_info_t *sdinfo)
17203 {
17204 sata_pkt_t *spkt;
17205 sata_cmd_t *scmd;
17206 sata_pkt_txlate_t *spx;
17207 int rval;
17208
17209 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17210 spx->txlt_sata_hba_inst = sata_hba_inst;
17211 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */
17212 spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17213 if (spkt == NULL) {
17214 kmem_free(spx, sizeof (sata_pkt_txlate_t));
17215 return (-1);
17216 }
17217 /* address is needed now */
17218 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17219
17220
17221 /* Fill sata_pkt */
17222 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17223 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17224 /* Synchronous mode, no callback */
17225 spkt->satapkt_comp = NULL;
17226 /* Timeout 30s */
17227 spkt->satapkt_time = sata_default_pkt_time;
17228
17229 scmd = &spkt->satapkt_cmd;
17230 scmd->satacmd_flags.sata_special_regs = B_TRUE;
17231 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
17232
17233 /* Set up which registers need to be returned */
17234 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE;
17235 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE;
17236
17237 /* Build SMART_RETURN_STATUS cmd in the sata_pkt */
17238 scmd->satacmd_addr_type = 0; /* N/A */
17239 scmd->satacmd_sec_count_lsb = 0; /* N/A */
17240 scmd->satacmd_lba_low_lsb = 0; /* N/A */
17241 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
17242 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
17243 scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS;
17244 scmd->satacmd_device_reg = 0; /* Always device 0 */
17245 scmd->satacmd_cmd_reg = SATAC_SMART;
17246 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17247 sdinfo->satadrv_addr.cport)));
17248
17249
17250 /* Send pkt to SATA HBA driver */
17251 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17252 SATA_TRAN_ACCEPTED ||
17253 spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17254 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17255 sdinfo->satadrv_addr.cport)));
17256 /*
17257 * Whoops, no SMART RETURN STATUS
17258 */
17259 rval = -1;
17260 } else {
17261 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17262 sdinfo->satadrv_addr.cport)));
17263 if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
17264 rval = -1;
17265 goto fail;
17266 }
17267 if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
17268 rval = -1;
17269 goto fail;
17270 }
17271 if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) &&
17272 (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2))
17273 rval = 0;
17274 else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) &&
17275 (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4))
17276 rval = 1;
17277 else {
17278 rval = -1;
17279 goto fail;
17280 }
17281 }
17282 fail:
17283 /* Free allocated resources */
17284 sata_pkt_free(spx);
17285 kmem_free(spx, sizeof (sata_pkt_txlate_t));
17286
17287 return (rval);
17288 }
17289
17290 /*
17291 *
17292 * Returns 0 if succeeded, -1 otherwise
17293 *
17294 * Cannot be called in an interrupt context.
17295 *
17296 */
17297 static int
sata_fetch_smart_data(sata_hba_inst_t * sata_hba_inst,sata_drive_info_t * sdinfo,struct smart_data * smart_data)17298 sata_fetch_smart_data(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
17299 struct smart_data *smart_data)
17300 {
17301 sata_pkt_t *spkt;
17302 sata_cmd_t *scmd;
17303 sata_pkt_txlate_t *spx;
17304 int rval = 0;
17305 dev_info_t *dip = SATA_DIP(sata_hba_inst);
17306
17307 #if ! defined(lint)
17308 ASSERT(sizeof (struct smart_data) == 512);
17309 #endif
17310
17311 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17312 spx->txlt_sata_hba_inst = sata_hba_inst;
17313 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */
17314 spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17315 if (spkt == NULL) {
17316 kmem_free(spx, sizeof (sata_pkt_txlate_t));
17317 return (-1);
17318 }
17319 /* address is needed now */
17320 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17321
17322
17323 /* Fill sata_pkt */
17324 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17325 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17326 /* Synchronous mode, no callback */
17327 spkt->satapkt_comp = NULL;
17328 /* Timeout 30s */
17329 spkt->satapkt_time = sata_default_pkt_time;
17330
17331 scmd = &spkt->satapkt_cmd;
17332 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17333
17334 /*
17335 * Allocate buffer for SMART data
17336 */
17337 scmd->satacmd_bp = sata_alloc_local_buffer(spx,
17338 sizeof (struct smart_data));
17339 if (scmd->satacmd_bp == NULL) {
17340 sata_pkt_free(spx);
17341 kmem_free(spx, sizeof (sata_pkt_txlate_t));
17342 SATA_LOG_D((sata_hba_inst, CE_WARN,
17343 "sata_fetch_smart_data: "
17344 "cannot allocate buffer"));
17345 return (-1);
17346 }
17347
17348
17349 /* Build SMART_READ_DATA cmd in the sata_pkt */
17350 scmd->satacmd_addr_type = 0; /* N/A */
17351 scmd->satacmd_sec_count_lsb = 0; /* N/A */
17352 scmd->satacmd_lba_low_lsb = 0; /* N/A */
17353 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
17354 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
17355 scmd->satacmd_features_reg = SATA_SMART_READ_DATA;
17356 scmd->satacmd_device_reg = 0; /* Always device 0 */
17357 scmd->satacmd_cmd_reg = SATAC_SMART;
17358 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17359 sdinfo->satadrv_addr.cport)));
17360
17361 /* Send pkt to SATA HBA driver */
17362 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17363 SATA_TRAN_ACCEPTED ||
17364 spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17365 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17366 sdinfo->satadrv_addr.cport)));
17367 /*
17368 * Whoops, no SMART DATA available
17369 */
17370 rval = -1;
17371 goto fail;
17372 } else {
17373 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17374 sdinfo->satadrv_addr.cport)));
17375 if (spx->txlt_buf_dma_handle != NULL) {
17376 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17377 DDI_DMA_SYNC_FORKERNEL);
17378 ASSERT(rval == DDI_SUCCESS);
17379 if (sata_check_for_dma_error(dip, spx)) {
17380 ddi_fm_service_impact(dip,
17381 DDI_SERVICE_UNAFFECTED);
17382 rval = -1;
17383 goto fail;
17384 }
17385 }
17386 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data,
17387 sizeof (struct smart_data));
17388 }
17389
17390 fail:
17391 /* Free allocated resources */
17392 sata_free_local_buffer(spx);
17393 sata_pkt_free(spx);
17394 kmem_free(spx, sizeof (sata_pkt_txlate_t));
17395
17396 return (rval);
17397 }
17398
17399 /*
17400 * Issue a READ LOG EXT command for the given log (log_addr) and page
17401 * (page_num) of the log. The output is written to buf. nsect is the size
17402 * of buf in units of 512-byte sectors.
17403 */
17404 static int
sata_read_log_ext(sata_hba_inst_t * sata_hba_inst,sata_drive_info_t * sdinfo,uint8_t log_addr,uint16_t page_num,void * buf,uint16_t nsect)17405 sata_read_log_ext(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
17406 uint8_t log_addr, uint16_t page_num, void *buf, uint16_t nsect)
17407 {
17408 dev_info_t *dip;
17409 sata_pkt_txlate_t *spx;
17410 sata_pkt_t *spkt;
17411 sata_cmd_t *scmd;
17412 kmutex_t *cmutex;
17413 int rval;
17414
17415 dip = SATA_DIP(sata_hba_inst);
17416 cmutex = &SATA_CPORT_MUTEX(sata_hba_inst, sdinfo->satadrv_addr.cport);
17417
17418 ASSERT(MUTEX_HELD(cmutex));
17419
17420 spx = kmem_zalloc(sizeof (*spx), KM_SLEEP);
17421 spx->txlt_sata_hba_inst = sata_hba_inst;
17422 spx->txlt_scsi_pkt = NULL;
17423
17424 spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17425 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17426 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17427 spkt->satapkt_comp = NULL;
17428 spkt->satapkt_time = sata_default_pkt_time;
17429
17430 scmd = &spkt->satapkt_cmd;
17431 scmd->satacmd_bp = sata_alloc_local_buffer(spx, (size_t)nsect * 512);
17432 if (scmd->satacmd_bp == NULL) {
17433 sata_pkt_free(spx);
17434 kmem_free(spx, sizeof (*spx));
17435 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s: cannot allocate bp",
17436 __func__));
17437 return (-1);
17438 }
17439
17440 scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
17441 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17442 scmd->satacmd_addr_type = ATA_ADDR_LBA48;
17443 scmd->satacmd_sec_count_lsb = nsect & 0xff;
17444 scmd->satacmd_sec_count_msb = nsect >> 8;
17445
17446 /*
17447 * From ACS-3 7.24.3.1 Table 68
17448 * LBA[47:40] Reserved
17449 * LBA[39:32] PAGE NUMBER (15:8)
17450 * LBA[31:16] Reserved
17451 * LBA[15:8] PAGE NUMBER (7:0)
17452 * LBA[7:0] LOG ADDRESS
17453 */
17454 scmd->satacmd_lba_low_lsb = log_addr; /* LBA[7:0] */
17455 scmd->satacmd_lba_mid_lsb = page_num & 0xff; /* LBA[15:8] */
17456 scmd->satacmd_lba_high_lsb = 0; /* LBA[23:16] */
17457 scmd->satacmd_lba_low_msb = 0; /* LBA[31:24] */
17458 scmd->satacmd_lba_mid_msb = page_num >> 8; /* LBA[39:32] */
17459 scmd->satacmd_lba_high_msb = 0; /* LBA[47:40] */
17460
17461 scmd->satacmd_device_reg = 0;
17462
17463 mutex_exit(cmutex);
17464 rval = (*SATA_START_FUNC(sata_hba_inst))(dip, spkt);
17465 mutex_enter(cmutex);
17466
17467 if (rval != SATA_TRAN_ACCEPTED ||
17468 spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17469 rval = -1;
17470 goto fail;
17471 }
17472
17473 if (spx->txlt_buf_dma_handle != NULL) {
17474 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17475 DDI_DMA_SYNC_FORKERNEL);
17476 ASSERT3S(rval, ==, DDI_SUCCESS);
17477 if (sata_check_for_dma_error(dip, spx)) {
17478 ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED);
17479 rval = -1;
17480 goto fail;
17481 }
17482
17483 bcopy(scmd->satacmd_bp->b_un.b_addr, buf, (size_t)nsect * 512);
17484 rval = 0;
17485 }
17486
17487 fail:
17488 sata_free_local_buffer(spx);
17489 sata_pkt_free(spx);
17490 kmem_free(spx, sizeof (*spx));
17491
17492 return (rval);
17493 }
17494
17495 /*
17496 * Used by LOG SENSE page 0x10
17497 * Reads (in synchronous mode) the self test log data using Read Log Ext cmd.
17498 * Note: cannot be called in the interrupt context.
17499 *
17500 * return 0 for success, -1 otherwise
17501 *
17502 */
17503 CTASSERT(sizeof (struct smart_ext_selftest_log) == 512);
17504
17505 static int
sata_ext_smart_selftest_read_log(sata_hba_inst_t * sata_hba_inst,sata_drive_info_t * sdinfo,struct smart_ext_selftest_log * ext_selftest_log,uint16_t block_num)17506 sata_ext_smart_selftest_read_log(sata_hba_inst_t *sata_hba_inst,
17507 sata_drive_info_t *sdinfo, struct smart_ext_selftest_log *ext_selftest_log,
17508 uint16_t block_num)
17509 {
17510 return (sata_read_log_ext(sata_hba_inst, sdinfo,
17511 EXT_SMART_SELFTEST_LOG_PAGE, block_num, ext_selftest_log, 1));
17512 }
17513
17514 /*
17515 * Returns 0 for success, -1 otherwise
17516 *
17517 * SMART self-test log data is returned in buffer pointed to by selftest_log
17518 */
17519 static int
sata_smart_selftest_log(sata_hba_inst_t * sata_hba_inst,sata_drive_info_t * sdinfo,struct smart_selftest_log * selftest_log)17520 sata_smart_selftest_log(
17521 sata_hba_inst_t *sata_hba_inst,
17522 sata_drive_info_t *sdinfo,
17523 struct smart_selftest_log *selftest_log)
17524 {
17525 sata_pkt_t *spkt;
17526 sata_cmd_t *scmd;
17527 sata_pkt_txlate_t *spx;
17528 int rval;
17529 dev_info_t *dip = SATA_DIP(sata_hba_inst);
17530
17531 #if ! defined(lint)
17532 ASSERT(sizeof (struct smart_selftest_log) == 512);
17533 #endif
17534
17535 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17536 spx->txlt_sata_hba_inst = sata_hba_inst;
17537 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */
17538 spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17539 if (spkt == NULL) {
17540 kmem_free(spx, sizeof (sata_pkt_txlate_t));
17541 return (-1);
17542 }
17543 /* address is needed now */
17544 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17545
17546
17547 /* Fill sata_pkt */
17548 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17549 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17550 /* Synchronous mode, no callback */
17551 spkt->satapkt_comp = NULL;
17552 /* Timeout 30s */
17553 spkt->satapkt_time = sata_default_pkt_time;
17554
17555 scmd = &spkt->satapkt_cmd;
17556 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17557
17558 /*
17559 * Allocate buffer for SMART SELFTEST LOG
17560 */
17561 scmd->satacmd_bp = sata_alloc_local_buffer(spx,
17562 sizeof (struct smart_selftest_log));
17563 if (scmd->satacmd_bp == NULL) {
17564 sata_pkt_free(spx);
17565 kmem_free(spx, sizeof (sata_pkt_txlate_t));
17566 SATA_LOG_D((sata_hba_inst, CE_WARN,
17567 "sata_smart_selftest_log: "
17568 "cannot allocate buffer"));
17569 return (-1);
17570 }
17571
17572 /* Build SMART_READ_LOG cmd in the sata_pkt */
17573 scmd->satacmd_addr_type = 0; /* N/A */
17574 scmd->satacmd_sec_count_lsb = 1; /* One sector of SMART log */
17575 scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE;
17576 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
17577 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
17578 scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
17579 scmd->satacmd_device_reg = 0; /* Always device 0 */
17580 scmd->satacmd_cmd_reg = SATAC_SMART;
17581 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17582 sdinfo->satadrv_addr.cport)));
17583
17584 /* Send pkt to SATA HBA driver */
17585 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17586 SATA_TRAN_ACCEPTED ||
17587 spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17588 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17589 sdinfo->satadrv_addr.cport)));
17590 /*
17591 * Whoops, no SMART DATA available
17592 */
17593 rval = -1;
17594 goto fail;
17595 } else {
17596 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17597 sdinfo->satadrv_addr.cport)));
17598 if (spx->txlt_buf_dma_handle != NULL) {
17599 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17600 DDI_DMA_SYNC_FORKERNEL);
17601 ASSERT(rval == DDI_SUCCESS);
17602 if (sata_check_for_dma_error(dip, spx)) {
17603 ddi_fm_service_impact(dip,
17604 DDI_SERVICE_UNAFFECTED);
17605 rval = -1;
17606 goto fail;
17607 }
17608 }
17609 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log,
17610 sizeof (struct smart_selftest_log));
17611 rval = 0;
17612 }
17613
17614 fail:
17615 /* Free allocated resources */
17616 sata_free_local_buffer(spx);
17617 sata_pkt_free(spx);
17618 kmem_free(spx, sizeof (sata_pkt_txlate_t));
17619
17620 return (rval);
17621 }
17622
17623
17624 /*
17625 * Returns 0 for success, -1 otherwise
17626 *
17627 * SMART READ LOG data is returned in buffer pointed to by smart_log
17628 */
17629 static int
sata_smart_read_log(sata_hba_inst_t * sata_hba_inst,sata_drive_info_t * sdinfo,uint8_t * smart_log,uint8_t which_log,uint8_t log_size)17630 sata_smart_read_log(
17631 sata_hba_inst_t *sata_hba_inst,
17632 sata_drive_info_t *sdinfo,
17633 uint8_t *smart_log, /* where the data should be returned */
17634 uint8_t which_log, /* which log should be returned */
17635 uint8_t log_size) /* # of 512 bytes in log */
17636 {
17637 sata_pkt_t *spkt;
17638 sata_cmd_t *scmd;
17639 sata_pkt_txlate_t *spx;
17640 int rval;
17641 dev_info_t *dip = SATA_DIP(sata_hba_inst);
17642
17643 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17644 spx->txlt_sata_hba_inst = sata_hba_inst;
17645 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */
17646 spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17647 if (spkt == NULL) {
17648 kmem_free(spx, sizeof (sata_pkt_txlate_t));
17649 return (-1);
17650 }
17651 /* address is needed now */
17652 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17653
17654
17655 /* Fill sata_pkt */
17656 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17657 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17658 /* Synchronous mode, no callback */
17659 spkt->satapkt_comp = NULL;
17660 /* Timeout 30s */
17661 spkt->satapkt_time = sata_default_pkt_time;
17662
17663 scmd = &spkt->satapkt_cmd;
17664 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17665
17666 /*
17667 * Allocate buffer for SMART READ LOG
17668 */
17669 scmd->satacmd_bp = sata_alloc_local_buffer(spx, (size_t)log_size * 512);
17670 if (scmd->satacmd_bp == NULL) {
17671 sata_pkt_free(spx);
17672 kmem_free(spx, sizeof (sata_pkt_txlate_t));
17673 SATA_LOG_D((sata_hba_inst, CE_WARN,
17674 "sata_smart_read_log: " "cannot allocate buffer"));
17675 return (-1);
17676 }
17677
17678 /* Build SMART_READ_LOG cmd in the sata_pkt */
17679 scmd->satacmd_addr_type = 0; /* N/A */
17680 scmd->satacmd_sec_count_lsb = log_size; /* what the caller asked for */
17681 scmd->satacmd_lba_low_lsb = which_log; /* which log page */
17682 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
17683 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
17684 scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
17685 scmd->satacmd_device_reg = 0; /* Always device 0 */
17686 scmd->satacmd_cmd_reg = SATAC_SMART;
17687
17688 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17689 sdinfo->satadrv_addr.cport)));
17690
17691 /* Send pkt to SATA HBA driver */
17692 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17693 SATA_TRAN_ACCEPTED ||
17694 spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17695 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17696 sdinfo->satadrv_addr.cport)));
17697
17698 /*
17699 * Whoops, no SMART DATA available
17700 */
17701 rval = -1;
17702 goto fail;
17703 } else {
17704 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17705 sdinfo->satadrv_addr.cport)));
17706
17707 if (spx->txlt_buf_dma_handle != NULL) {
17708 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17709 DDI_DMA_SYNC_FORKERNEL);
17710 ASSERT(rval == DDI_SUCCESS);
17711 if (sata_check_for_dma_error(dip, spx)) {
17712 ddi_fm_service_impact(dip,
17713 DDI_SERVICE_UNAFFECTED);
17714 rval = -1;
17715 goto fail;
17716 }
17717 }
17718 bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512);
17719 rval = 0;
17720 }
17721
17722 fail:
17723 /* Free allocated resources */
17724 sata_free_local_buffer(spx);
17725 sata_pkt_free(spx);
17726 kmem_free(spx, sizeof (sata_pkt_txlate_t));
17727
17728 return (rval);
17729 }
17730
17731 /*
17732 * Used by LOG SENSE page 0x10
17733 *
17734 * return 0 for success, -1 otherwise
17735 *
17736 */
17737 CTASSERT(sizeof (struct read_log_ext_directory) == 512);
17738
17739 static int
sata_read_log_ext_directory(sata_hba_inst_t * sata_hba_inst,sata_drive_info_t * sdinfo,struct read_log_ext_directory * logdir)17740 sata_read_log_ext_directory(sata_hba_inst_t *sata_hba_inst,
17741 sata_drive_info_t *sdinfo, struct read_log_ext_directory *logdir)
17742 {
17743 return (sata_read_log_ext(sata_hba_inst, sdinfo,
17744 READ_LOG_EXT_LOG_DIRECTORY, 0, logdir, 1));
17745 }
17746
17747 /*
17748 * Set up error retrieval sata command for NCQ command error data
17749 * recovery.
17750 *
17751 * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
17752 * returns SATA_FAILURE otherwise.
17753 */
17754 static int
sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t * spx,sata_drive_info_t * sdinfo)17755 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
17756 {
17757 #ifndef __lock_lint
17758 _NOTE(ARGUNUSED(sdinfo))
17759 #endif
17760
17761 sata_pkt_t *spkt = spx->txlt_sata_pkt;
17762 sata_cmd_t *scmd;
17763 struct buf *bp;
17764
17765 /* Operation modes are up to the caller */
17766 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17767
17768 /* Synchronous mode, no callback - may be changed by the caller */
17769 spkt->satapkt_comp = NULL;
17770 spkt->satapkt_time = sata_default_pkt_time;
17771
17772 scmd = &spkt->satapkt_cmd;
17773 bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t));
17774 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
17775
17776 /*
17777 * Allocate dma_able buffer error data.
17778 * Buffer allocation will take care of buffer alignment and other DMA
17779 * attributes.
17780 */
17781 bp = sata_alloc_local_buffer(spx,
17782 sizeof (struct sata_ncq_error_recovery_page));
17783 if (bp == NULL)
17784 return (SATA_FAILURE);
17785
17786 bp_mapin(bp); /* make data buffer accessible */
17787 scmd->satacmd_bp = bp;
17788
17789 /*
17790 * Set-up pointer to the buffer handle, so HBA can sync buffer
17791 * before accessing it. Handle is in usual place in translate struct.
17792 */
17793 scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
17794
17795 ASSERT(scmd->satacmd_num_dma_cookies != 0);
17796 ASSERT(scmd->satacmd_dma_cookie_list != NULL);
17797
17798 return (SATA_SUCCESS);
17799 }
17800
17801 /*
17802 * sata_xlate_errors() is used to translate (S)ATA error
17803 * information to SCSI information returned in the SCSI
17804 * packet.
17805 */
17806 static void
sata_xlate_errors(sata_pkt_txlate_t * spx)17807 sata_xlate_errors(sata_pkt_txlate_t *spx)
17808 {
17809 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
17810 struct scsi_extended_sense *sense;
17811
17812 scsipkt->pkt_reason = CMD_INCOMPLETE;
17813 *scsipkt->pkt_scbp = STATUS_CHECK;
17814 sense = sata_arq_sense(spx);
17815
17816 switch (spx->txlt_sata_pkt->satapkt_reason) {
17817 case SATA_PKT_PORT_ERROR:
17818 /*
17819 * We have no device data. Assume no data transfered.
17820 */
17821 sense->es_key = KEY_HARDWARE_ERROR;
17822 break;
17823
17824 case SATA_PKT_DEV_ERROR:
17825 if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
17826 SATA_STATUS_ERR) {
17827 /*
17828 * determine dev error reason from error
17829 * reg content
17830 */
17831 sata_decode_device_error(spx, sense);
17832 break;
17833 }
17834 /* No extended sense key - no info available */
17835 break;
17836
17837 case SATA_PKT_TIMEOUT:
17838 scsipkt->pkt_reason = CMD_TIMEOUT;
17839 scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET;
17840 /* No extended sense key */
17841 break;
17842
17843 case SATA_PKT_ABORTED:
17844 scsipkt->pkt_reason = CMD_ABORTED;
17845 scsipkt->pkt_statistics |= STAT_ABORTED;
17846 /* No extended sense key */
17847 break;
17848
17849 case SATA_PKT_RESET:
17850 /*
17851 * pkt aborted either by an explicit reset request from
17852 * a host, or due to error recovery
17853 */
17854 scsipkt->pkt_reason = CMD_RESET;
17855 scsipkt->pkt_statistics |= STAT_DEV_RESET;
17856 break;
17857
17858 default:
17859 scsipkt->pkt_reason = CMD_TRAN_ERR;
17860 break;
17861 }
17862 }
17863
17864
17865
17866
17867 /*
17868 * Log sata message
17869 * dev pathname msg line preceeds the logged message.
17870 */
17871
17872 static void
sata_log(sata_hba_inst_t * sata_hba_inst,uint_t level,char * fmt,...)17873 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...)
17874 {
17875 char pathname[128];
17876 dev_info_t *dip = NULL;
17877 va_list ap;
17878
17879 mutex_enter(&sata_log_mutex);
17880
17881 va_start(ap, fmt);
17882 (void) vsprintf(sata_log_buf, fmt, ap);
17883 va_end(ap);
17884
17885 if (sata_hba_inst != NULL) {
17886 dip = SATA_DIP(sata_hba_inst);
17887 (void) ddi_pathname(dip, pathname);
17888 } else {
17889 pathname[0] = 0;
17890 }
17891 if (level == CE_CONT) {
17892 if (sata_debug_flags == 0)
17893 cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf);
17894 else
17895 cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf);
17896 } else {
17897 if (level != CE_NOTE) {
17898 cmn_err(level, "%s:\n %s", pathname, sata_log_buf);
17899 } else if (sata_msg) {
17900 cmn_err(level, "%s:\n %s", pathname,
17901 sata_log_buf);
17902 }
17903 }
17904
17905 /* sata trace debug */
17906 sata_trace_debug(dip, sata_log_buf);
17907
17908 mutex_exit(&sata_log_mutex);
17909 }
17910
17911
17912 /* ******** Asynchronous HBA events handling & hotplugging support ******** */
17913
17914 /*
17915 * Start or terminate the thread, depending on flag arg and current state
17916 */
17917 static void
sata_event_thread_control(int startstop)17918 sata_event_thread_control(int startstop)
17919 {
17920 static int sata_event_thread_terminating = 0;
17921 static int sata_event_thread_starting = 0;
17922 int i;
17923
17924 mutex_enter(&sata_event_mutex);
17925
17926 if (startstop == 0 && (sata_event_thread_starting == 1 ||
17927 sata_event_thread_terminating == 1)) {
17928 mutex_exit(&sata_event_mutex);
17929 return;
17930 }
17931 if (startstop == 1 && sata_event_thread_starting == 1) {
17932 mutex_exit(&sata_event_mutex);
17933 return;
17934 }
17935 if (startstop == 1 && sata_event_thread_terminating == 1) {
17936 sata_event_thread_starting = 1;
17937 /* wait til terminate operation completes */
17938 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
17939 while (sata_event_thread_terminating == 1) {
17940 if (i-- <= 0) {
17941 sata_event_thread_starting = 0;
17942 mutex_exit(&sata_event_mutex);
17943 #ifdef SATA_DEBUG
17944 cmn_err(CE_WARN, "sata_event_thread_control: "
17945 "timeout waiting for thread to terminate");
17946 #endif
17947 return;
17948 }
17949 mutex_exit(&sata_event_mutex);
17950 delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
17951 mutex_enter(&sata_event_mutex);
17952 }
17953 }
17954 if (startstop == 1) {
17955 if (sata_event_thread == NULL) {
17956 sata_event_thread = thread_create(NULL, 0,
17957 (void (*)())sata_event_daemon,
17958 &sata_hba_list, 0, &p0, TS_RUN, minclsyspri);
17959 }
17960 sata_event_thread_starting = 0;
17961 mutex_exit(&sata_event_mutex);
17962 return;
17963 }
17964
17965 /*
17966 * If we got here, thread may need to be terminated
17967 */
17968 if (sata_event_thread != NULL) {
17969 int i;
17970 /* Signal event thread to go away */
17971 sata_event_thread_terminating = 1;
17972 sata_event_thread_terminate = 1;
17973 cv_signal(&sata_event_cv);
17974 /*
17975 * Wait til daemon terminates.
17976 */
17977 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
17978 while (sata_event_thread_terminate == 1) {
17979 mutex_exit(&sata_event_mutex);
17980 if (i-- <= 0) {
17981 /* Daemon did not go away !!! */
17982 #ifdef SATA_DEBUG
17983 cmn_err(CE_WARN, "sata_event_thread_control: "
17984 "cannot terminate event daemon thread");
17985 #endif
17986 mutex_enter(&sata_event_mutex);
17987 break;
17988 }
17989 delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
17990 mutex_enter(&sata_event_mutex);
17991 }
17992 sata_event_thread_terminating = 0;
17993 }
17994 ASSERT(sata_event_thread_terminating == 0);
17995 ASSERT(sata_event_thread_starting == 0);
17996 mutex_exit(&sata_event_mutex);
17997 }
17998
17999
18000 /*
18001 * SATA HBA event notification function.
18002 * Events reported by SATA HBA drivers per HBA instance relate to a change in
18003 * a port and/or device state or a controller itself.
18004 * Events for different addresses/addr types cannot be combined.
18005 * A warning message is generated for each event type.
18006 * Events are not processed by this function, so only the
18007 * event flag(s)is set for an affected entity and the event thread is
18008 * waken up. Event daemon thread processes all events.
18009 *
18010 * NOTE: Since more than one event may be reported at the same time, one
18011 * cannot determine a sequence of events when opposite event are reported, eg.
18012 * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing
18013 * is taking precedence over reported events, i.e. may cause ignoring some
18014 * events.
18015 */
18016 #define SATA_EVENT_MAX_MSG_LENGTH 79
18017
18018 void
sata_hba_event_notify(dev_info_t * dip,sata_device_t * sata_device,int event)18019 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event)
18020 {
18021 sata_hba_inst_t *sata_hba_inst = NULL;
18022 sata_address_t *saddr;
18023 sata_pmult_info_t *pmultinfo;
18024 sata_drive_info_t *sdinfo;
18025 sata_port_stats_t *pstats;
18026 sata_cport_info_t *cportinfo = NULL;
18027 sata_pmport_info_t *pmportinfo = NULL;
18028 int cport, pmport;
18029 char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1];
18030 char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1];
18031 char *lcp;
18032 static char *err_msg_evnt_1 =
18033 "sata_hba_event_notify: invalid port event 0x%x ";
18034 static char *err_msg_evnt_2 =
18035 "sata_hba_event_notify: invalid device event 0x%x ";
18036 int linkevent;
18037
18038 /*
18039 * There is a possibility that an event will be generated on HBA
18040 * that has not completed attachment or is detaching. We still want
18041 * to process events until HBA is detached.
18042 */
18043 mutex_enter(&sata_mutex);
18044 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
18045 sata_hba_inst = sata_hba_inst->satahba_next) {
18046 if (SATA_DIP(sata_hba_inst) == dip)
18047 if (sata_hba_inst->satahba_attached == 1)
18048 break;
18049 }
18050 mutex_exit(&sata_mutex);
18051 if (sata_hba_inst == NULL)
18052 /* HBA not attached */
18053 return;
18054
18055 ASSERT(sata_device != NULL);
18056
18057 /*
18058 * Validate address before - do not proceed with invalid address.
18059 */
18060 saddr = &sata_device->satadev_addr;
18061 if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst))
18062 return;
18063
18064 cport = saddr->cport;
18065 pmport = saddr->pmport;
18066
18067 buf1[0] = buf2[0] = '\0';
18068
18069 /*
18070 * If event relates to port or device, check port state.
18071 * Port has to be initialized, or we cannot accept an event.
18072 */
18073 if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT |
18074 SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT | SATA_ADDR_PMULT)) != 0) {
18075 mutex_enter(&sata_hba_inst->satahba_mutex);
18076 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
18077 mutex_exit(&sata_hba_inst->satahba_mutex);
18078 if (cportinfo == NULL || cportinfo->cport_state == 0)
18079 return;
18080 }
18081
18082 if ((saddr->qual & (SATA_ADDR_PMULT | SATA_ADDR_PMPORT |
18083 SATA_ADDR_DPMPORT)) != 0) {
18084 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
18085 SATA_LOG_D((sata_hba_inst, CE_WARN,
18086 "sata_hba_event_notify: Non-pmult device (0x%x)"
18087 "is attached to port %d, ignore pmult/pmport "
18088 "event 0x%x", cportinfo->cport_dev_type,
18089 cport, event));
18090 return;
18091 }
18092
18093 mutex_enter(&cportinfo->cport_mutex);
18094 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
18095 mutex_exit(&cportinfo->cport_mutex);
18096
18097 /*
18098 * The daemon might be processing attachment of port
18099 * multiplier, in that case we should ignore events on its
18100 * sub-devices.
18101 *
18102 * NOTE: Only pmult_state is checked in sata_hba_event_notify.
18103 * The pmport_state is checked by sata daemon.
18104 */
18105 if (pmultinfo == NULL ||
18106 pmultinfo->pmult_state == SATA_STATE_UNKNOWN) {
18107 SATA_LOG_D((sata_hba_inst, CE_WARN,
18108 "sata_hba_event_notify: pmult is not"
18109 "available at port %d:%d, ignore event 0x%x",
18110 cport, pmport, event));
18111 return;
18112 }
18113 }
18114
18115 if ((saddr->qual &
18116 (SATA_ADDR_PMPORT | SATA_ADDR_DPMPORT)) != 0) {
18117
18118 mutex_enter(&cportinfo->cport_mutex);
18119 if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) {
18120 SATA_LOG_D((sata_hba_inst, CE_WARN,
18121 "sata_hba_event_notify: invalid/"
18122 "un-implemented port %d:%d (%d ports), "
18123 "ignore event 0x%x", cport, pmport,
18124 SATA_NUM_PMPORTS(sata_hba_inst, cport), event));
18125 mutex_exit(&cportinfo->cport_mutex);
18126 return;
18127 }
18128 mutex_exit(&cportinfo->cport_mutex);
18129
18130 mutex_enter(&sata_hba_inst->satahba_mutex);
18131 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
18132 cport, pmport);
18133 mutex_exit(&sata_hba_inst->satahba_mutex);
18134
18135 /* pmport is implemented/valid? */
18136 if (pmportinfo == NULL) {
18137 SATA_LOG_D((sata_hba_inst, CE_WARN,
18138 "sata_hba_event_notify: invalid/"
18139 "un-implemented port %d:%d, ignore "
18140 "event 0x%x", cport, pmport, event));
18141 return;
18142 }
18143 }
18144
18145 /*
18146 * Events refer to devices, ports and controllers - each has
18147 * unique address. Events for different addresses cannot be combined.
18148 */
18149 if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) {
18150
18151 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
18152
18153 /* qualify this event(s) */
18154 if ((event & SATA_EVNT_PORT_EVENTS) == 0) {
18155 /* Invalid event for the device port */
18156 (void) sprintf(buf2, err_msg_evnt_1,
18157 event & SATA_EVNT_PORT_EVENTS);
18158 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
18159 goto event_info;
18160 }
18161 if (saddr->qual == SATA_ADDR_CPORT) {
18162 /* Controller's device port event */
18163
18164 (SATA_CPORT_INFO(sata_hba_inst, cport))->
18165 cport_event_flags |=
18166 event & SATA_EVNT_PORT_EVENTS;
18167 pstats =
18168 &(SATA_CPORT_INFO(sata_hba_inst, cport))->
18169 cport_stats;
18170 } else {
18171 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
18172 mutex_enter(&pmportinfo->pmport_mutex);
18173 /* Port multiplier's device port event */
18174 (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
18175 pmport_event_flags |=
18176 event & SATA_EVNT_PORT_EVENTS;
18177 pstats =
18178 &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
18179 pmport_stats;
18180 mutex_exit(&pmportinfo->pmport_mutex);
18181 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
18182 }
18183
18184 /*
18185 * Add to statistics and log the message. We have to do it
18186 * here rather than in the event daemon, because there may be
18187 * multiple events occuring before they are processed.
18188 */
18189 linkevent = event &
18190 (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED);
18191 if (linkevent) {
18192 if (linkevent == (SATA_EVNT_LINK_LOST |
18193 SATA_EVNT_LINK_ESTABLISHED)) {
18194 /* This is likely event combination */
18195 (void) strlcat(buf1, "link lost/established, ",
18196 SATA_EVENT_MAX_MSG_LENGTH);
18197
18198 if (pstats->link_lost < 0xffffffffffffffffULL)
18199 pstats->link_lost++;
18200 if (pstats->link_established <
18201 0xffffffffffffffffULL)
18202 pstats->link_established++;
18203 linkevent = 0;
18204 } else if (linkevent & SATA_EVNT_LINK_LOST) {
18205 (void) strlcat(buf1, "link lost, ",
18206 SATA_EVENT_MAX_MSG_LENGTH);
18207
18208 if (pstats->link_lost < 0xffffffffffffffffULL)
18209 pstats->link_lost++;
18210 } else {
18211 (void) strlcat(buf1, "link established, ",
18212 SATA_EVENT_MAX_MSG_LENGTH);
18213 if (pstats->link_established <
18214 0xffffffffffffffffULL)
18215 pstats->link_established++;
18216 }
18217 }
18218 if (event & SATA_EVNT_DEVICE_ATTACHED) {
18219 (void) strlcat(buf1, "device attached, ",
18220 SATA_EVENT_MAX_MSG_LENGTH);
18221 if (pstats->device_attached < 0xffffffffffffffffULL)
18222 pstats->device_attached++;
18223 }
18224 if (event & SATA_EVNT_DEVICE_DETACHED) {
18225 (void) strlcat(buf1, "device detached, ",
18226 SATA_EVENT_MAX_MSG_LENGTH);
18227 if (pstats->device_detached < 0xffffffffffffffffULL)
18228 pstats->device_detached++;
18229 }
18230 if (event & SATA_EVNT_PWR_LEVEL_CHANGED) {
18231 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
18232 "port %d power level changed", cport);
18233 if (pstats->port_pwr_changed < 0xffffffffffffffffULL)
18234 pstats->port_pwr_changed++;
18235 }
18236
18237 if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) {
18238 /* There should be no other events for this address */
18239 (void) sprintf(buf2, err_msg_evnt_1,
18240 event & ~SATA_EVNT_PORT_EVENTS);
18241 }
18242 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
18243
18244 } else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) {
18245 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
18246
18247 /* qualify this event */
18248 if ((event & SATA_EVNT_DEVICE_RESET) == 0) {
18249 /* Invalid event for a device */
18250 (void) sprintf(buf2, err_msg_evnt_2,
18251 event & SATA_EVNT_DEVICE_RESET);
18252 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
18253 goto event_info;
18254 }
18255 /* drive event */
18256 sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
18257 if (sdinfo != NULL) {
18258 if (event & SATA_EVNT_DEVICE_RESET) {
18259 (void) strlcat(buf1, "device reset, ",
18260 SATA_EVENT_MAX_MSG_LENGTH);
18261 if (sdinfo->satadrv_stats.drive_reset <
18262 0xffffffffffffffffULL)
18263 sdinfo->satadrv_stats.drive_reset++;
18264 sdinfo->satadrv_event_flags |=
18265 SATA_EVNT_DEVICE_RESET;
18266 }
18267 }
18268 if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) {
18269 /* Invalid event for a device */
18270 (void) sprintf(buf2, err_msg_evnt_2,
18271 event & ~SATA_EVNT_DRIVE_EVENTS);
18272 }
18273 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
18274 } else if (saddr->qual == SATA_ADDR_PMULT) {
18275 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
18276
18277 /* qualify this event */
18278 if ((event & (SATA_EVNT_DEVICE_RESET |
18279 SATA_EVNT_PMULT_LINK_CHANGED)) == 0) {
18280 /* Invalid event for a port multiplier */
18281 (void) sprintf(buf2, err_msg_evnt_2,
18282 event & SATA_EVNT_DEVICE_RESET);
18283 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
18284 goto event_info;
18285 }
18286
18287 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
18288
18289 if (event & SATA_EVNT_DEVICE_RESET) {
18290
18291 SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
18292 "[Reset] port-mult on cport %d", cport);
18293 pmultinfo->pmult_event_flags |=
18294 SATA_EVNT_DEVICE_RESET;
18295 (void) strlcat(buf1, "pmult reset, ",
18296 SATA_EVENT_MAX_MSG_LENGTH);
18297 }
18298
18299 if (event & SATA_EVNT_PMULT_LINK_CHANGED) {
18300
18301 SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
18302 "pmult link changed on cport %d", cport);
18303 pmultinfo->pmult_event_flags |=
18304 SATA_EVNT_PMULT_LINK_CHANGED;
18305 (void) strlcat(buf1, "pmult link changed, ",
18306 SATA_EVENT_MAX_MSG_LENGTH);
18307 }
18308 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
18309
18310 } else {
18311 if (saddr->qual != SATA_ADDR_NULL) {
18312 /* Wrong address qualifier */
18313 SATA_LOG_D((sata_hba_inst, CE_WARN,
18314 "sata_hba_event_notify: invalid address 0x%x",
18315 *(uint32_t *)saddr));
18316 return;
18317 }
18318 if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 ||
18319 (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) {
18320 /* Invalid event for the controller */
18321 SATA_LOG_D((sata_hba_inst, CE_WARN,
18322 "sata_hba_event_notify: invalid event 0x%x for "
18323 "controller",
18324 event & SATA_EVNT_CONTROLLER_EVENTS));
18325 return;
18326 }
18327 buf1[0] = '\0';
18328 /* This may be a frequent and not interesting event */
18329 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
18330 "controller power level changed\n", NULL);
18331
18332 mutex_enter(&sata_hba_inst->satahba_mutex);
18333 if (sata_hba_inst->satahba_stats.ctrl_pwr_change <
18334 0xffffffffffffffffULL)
18335 sata_hba_inst->satahba_stats.ctrl_pwr_change++;
18336
18337 sata_hba_inst->satahba_event_flags |=
18338 SATA_EVNT_PWR_LEVEL_CHANGED;
18339 mutex_exit(&sata_hba_inst->satahba_mutex);
18340 }
18341 /*
18342 * If we got here, there is something to do with this HBA
18343 * instance.
18344 */
18345 mutex_enter(&sata_hba_inst->satahba_mutex);
18346 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
18347 mutex_exit(&sata_hba_inst->satahba_mutex);
18348 mutex_enter(&sata_mutex);
18349 sata_event_pending |= SATA_EVNT_MAIN; /* global event indicator */
18350 mutex_exit(&sata_mutex);
18351
18352 /* Tickle event thread */
18353 mutex_enter(&sata_event_mutex);
18354 if (sata_event_thread_active == 0)
18355 cv_signal(&sata_event_cv);
18356 mutex_exit(&sata_event_mutex);
18357
18358 event_info:
18359 if (buf1[0] != '\0') {
18360 lcp = strrchr(buf1, ',');
18361 if (lcp != NULL)
18362 *lcp = '\0';
18363 }
18364 if (saddr->qual == SATA_ADDR_CPORT ||
18365 saddr->qual == SATA_ADDR_DCPORT) {
18366 if (buf1[0] != '\0') {
18367 sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
18368 cport, buf1);
18369 }
18370 if (buf2[0] != '\0') {
18371 sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
18372 cport, buf2);
18373 }
18374 } else if (saddr->qual == SATA_ADDR_PMPORT ||
18375 saddr->qual == SATA_ADDR_DPMPORT) {
18376 if (buf1[0] != '\0') {
18377 sata_log(sata_hba_inst, CE_NOTE,
18378 "port %d pmport %d: %s\n", cport, pmport, buf1);
18379 }
18380 if (buf2[0] != '\0') {
18381 sata_log(sata_hba_inst, CE_NOTE,
18382 "port %d pmport %d: %s\n", cport, pmport, buf2);
18383 }
18384 }
18385 }
18386
18387
18388 /*
18389 * Event processing thread.
18390 * Arg is a pointer to the sata_hba_list pointer.
18391 * It is not really needed, because sata_hba_list is global and static
18392 */
18393 static void
sata_event_daemon(void * arg)18394 sata_event_daemon(void *arg)
18395 {
18396 #ifndef __lock_lint
18397 _NOTE(ARGUNUSED(arg))
18398 #endif
18399 sata_hba_inst_t *sata_hba_inst;
18400 clock_t delta;
18401
18402 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18403 "SATA event daemon started\n", NULL);
18404 loop:
18405 /*
18406 * Process events here. Walk through all registered HBAs
18407 */
18408 mutex_enter(&sata_mutex);
18409 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
18410 sata_hba_inst = sata_hba_inst->satahba_next) {
18411 ASSERT(sata_hba_inst != NULL);
18412 mutex_enter(&sata_hba_inst->satahba_mutex);
18413 if (sata_hba_inst->satahba_attached == 0 ||
18414 (sata_hba_inst->satahba_event_flags &
18415 SATA_EVNT_SKIP) != 0) {
18416 mutex_exit(&sata_hba_inst->satahba_mutex);
18417 continue;
18418 }
18419 if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) {
18420 sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP;
18421 mutex_exit(&sata_hba_inst->satahba_mutex);
18422 mutex_exit(&sata_mutex);
18423 /* Got the controller with pending event */
18424 sata_process_controller_events(sata_hba_inst);
18425 /*
18426 * Since global mutex was released, there is a
18427 * possibility that HBA list has changed, so start
18428 * over from the top. Just processed controller
18429 * will be passed-over because of the SKIP flag.
18430 */
18431 goto loop;
18432 }
18433 mutex_exit(&sata_hba_inst->satahba_mutex);
18434 }
18435 /* Clear SKIP flag in all controllers */
18436 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
18437 sata_hba_inst = sata_hba_inst->satahba_next) {
18438 mutex_enter(&sata_hba_inst->satahba_mutex);
18439 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP;
18440 mutex_exit(&sata_hba_inst->satahba_mutex);
18441 }
18442 mutex_exit(&sata_mutex);
18443
18444 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18445 "SATA EVENT DAEMON suspending itself", NULL);
18446
18447 #ifdef SATA_DEBUG
18448 if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) {
18449 sata_log(sata_hba_inst, CE_WARN,
18450 "SATA EVENTS PROCESSING DISABLED\n");
18451 thread_exit(); /* Daemon will not run again */
18452 }
18453 #endif
18454 mutex_enter(&sata_event_mutex);
18455 sata_event_thread_active = 0;
18456 mutex_exit(&sata_event_mutex);
18457 /*
18458 * Go to sleep/suspend itself and wake up either because new event or
18459 * wait timeout. Exit if there is a termination request (driver
18460 * unload).
18461 */
18462 delta = drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME);
18463 do {
18464 mutex_enter(&sata_event_mutex);
18465 (void) cv_reltimedwait(&sata_event_cv, &sata_event_mutex,
18466 delta, TR_CLOCK_TICK);
18467
18468 if (sata_event_thread_active != 0) {
18469 mutex_exit(&sata_event_mutex);
18470 continue;
18471 }
18472
18473 /* Check if it is time to go away */
18474 if (sata_event_thread_terminate == 1) {
18475 /*
18476 * It is up to the thread setting above flag to make
18477 * sure that this thread is not killed prematurely.
18478 */
18479 sata_event_thread_terminate = 0;
18480 sata_event_thread = NULL;
18481 mutex_exit(&sata_event_mutex);
18482 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18483 "SATA_EVENT_DAEMON_TERMINATING", NULL);
18484 thread_exit(); { _NOTE(NOT_REACHED) }
18485 }
18486 mutex_exit(&sata_event_mutex);
18487 } while (!(sata_event_pending & SATA_EVNT_MAIN));
18488
18489 mutex_enter(&sata_event_mutex);
18490 sata_event_thread_active = 1;
18491 mutex_exit(&sata_event_mutex);
18492
18493 mutex_enter(&sata_mutex);
18494 sata_event_pending &= ~SATA_EVNT_MAIN;
18495 mutex_exit(&sata_mutex);
18496
18497 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18498 "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL);
18499
18500 goto loop;
18501 }
18502
18503 /*
18504 * Specific HBA instance event processing.
18505 *
18506 * NOTE: At the moment, device event processing is limited to hard disks
18507 * only.
18508 * Port multiplier is supported now.
18509 */
18510 static void
sata_process_controller_events(sata_hba_inst_t * sata_hba_inst)18511 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst)
18512 {
18513 int ncport;
18514 uint32_t event_flags;
18515 sata_address_t *saddr;
18516 sata_cport_info_t *cportinfo;
18517 sata_pmult_info_t *pmultinfo;
18518
18519 SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst,
18520 "Processing controller %d event(s)",
18521 ddi_get_instance(SATA_DIP(sata_hba_inst)));
18522
18523 mutex_enter(&sata_hba_inst->satahba_mutex);
18524 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN;
18525 event_flags = sata_hba_inst->satahba_event_flags;
18526 mutex_exit(&sata_hba_inst->satahba_mutex);
18527 /*
18528 * Process controller power change first
18529 * HERE
18530 */
18531 if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED)
18532 sata_process_cntrl_pwr_level_change(sata_hba_inst);
18533
18534 /*
18535 * Search through ports/devices to identify affected port/device.
18536 * We may have to process events for more than one port/device.
18537 */
18538 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
18539 /*
18540 * Not all ports may be processed in attach by the time we
18541 * get an event. Check if port info is initialized.
18542 */
18543 mutex_enter(&sata_hba_inst->satahba_mutex);
18544 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
18545 mutex_exit(&sata_hba_inst->satahba_mutex);
18546 if (cportinfo == NULL || cportinfo->cport_state == 0)
18547 continue;
18548
18549 /* We have initialized controller port info */
18550 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18551 event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))->
18552 cport_event_flags;
18553 /* Check if port was locked by IOCTL processing */
18554 if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) {
18555 /*
18556 * We ignore port events because port is busy
18557 * with AP control processing. Set again
18558 * controller and main event flag, so that
18559 * events may be processed by the next daemon
18560 * run.
18561 */
18562 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18563 mutex_enter(&sata_hba_inst->satahba_mutex);
18564 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
18565 mutex_exit(&sata_hba_inst->satahba_mutex);
18566 mutex_enter(&sata_mutex);
18567 sata_event_pending |= SATA_EVNT_MAIN;
18568 mutex_exit(&sata_mutex);
18569 SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst,
18570 "Event processing postponed until "
18571 "AP control processing completes",
18572 NULL);
18573 /* Check other ports */
18574 continue;
18575 } else {
18576 /*
18577 * Set BSY flag so that AP control would not
18578 * interfere with events processing for
18579 * this port.
18580 */
18581 (SATA_CPORT_INFO(sata_hba_inst, ncport))->
18582 cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY;
18583 }
18584 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18585
18586 saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr;
18587
18588 if ((event_flags &
18589 (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
18590 /*
18591 * Got port event.
18592 * We need some hierarchy of event processing as they
18593 * are affecting each other:
18594 * 1. port failed
18595 * 2. device detached/attached
18596 * 3. link events - link events may trigger device
18597 * detached or device attached events in some
18598 * circumstances.
18599 * 4. port power level changed
18600 */
18601 if (event_flags & SATA_EVNT_PORT_FAILED) {
18602 sata_process_port_failed_event(sata_hba_inst,
18603 saddr);
18604 }
18605 if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
18606 sata_process_device_detached(sata_hba_inst,
18607 saddr);
18608 }
18609 if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
18610 sata_process_device_attached(sata_hba_inst,
18611 saddr);
18612 }
18613 if (event_flags &
18614 (SATA_EVNT_LINK_ESTABLISHED |
18615 SATA_EVNT_LINK_LOST)) {
18616 sata_process_port_link_events(sata_hba_inst,
18617 saddr);
18618 }
18619 if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) {
18620 sata_process_port_pwr_change(sata_hba_inst,
18621 saddr);
18622 }
18623 if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
18624 sata_process_target_node_cleanup(
18625 sata_hba_inst, saddr);
18626 }
18627 if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) {
18628 sata_process_device_autoonline(
18629 sata_hba_inst, saddr);
18630 }
18631 }
18632
18633
18634 /*
18635 * Scan port multiplier and all its sub-ports event flags.
18636 * The events are marked by
18637 * (1) sata_pmult_info.pmult_event_flags
18638 * (2) sata_pmport_info.pmport_event_flags
18639 */
18640 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18641 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
18642 /*
18643 * There should be another extra check: this
18644 * port multiplier still exists?
18645 */
18646 pmultinfo = SATA_PMULT_INFO(sata_hba_inst,
18647 ncport);
18648
18649 if (pmultinfo != NULL) {
18650 mutex_exit(&(SATA_CPORT_MUTEX(
18651 sata_hba_inst, ncport)));
18652 sata_process_pmult_events(
18653 sata_hba_inst, ncport);
18654 mutex_enter(&(SATA_CPORT_MUTEX(
18655 sata_hba_inst, ncport)));
18656 } else {
18657 SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
18658 "Port-multiplier is gone. "
18659 "Ignore all sub-device events "
18660 "at port %d.", ncport);
18661 }
18662 }
18663
18664 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) !=
18665 SATA_DTYPE_NONE) &&
18666 (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) {
18667 if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)->
18668 satadrv_event_flags &
18669 (SATA_EVNT_DEVICE_RESET |
18670 SATA_EVNT_INPROC_DEVICE_RESET)) {
18671 /* Have device event */
18672 sata_process_device_reset(sata_hba_inst,
18673 saddr);
18674 }
18675 }
18676 /* Release PORT_BUSY flag */
18677 (SATA_CPORT_INFO(sata_hba_inst, ncport))->
18678 cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
18679 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18680
18681 } /* End of loop through the controller SATA ports */
18682 }
18683
18684 /*
18685 * Specific port multiplier instance event processing. At the moment, device
18686 * event processing is limited to link/attach event only.
18687 *
18688 * NOTE: power management event is not supported yet.
18689 */
18690 static void
sata_process_pmult_events(sata_hba_inst_t * sata_hba_inst,uint8_t cport)18691 sata_process_pmult_events(sata_hba_inst_t *sata_hba_inst, uint8_t cport)
18692 {
18693 sata_cport_info_t *cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
18694 sata_pmult_info_t *pmultinfo;
18695 sata_pmport_info_t *pmportinfo;
18696 sata_address_t *saddr;
18697 sata_device_t sata_device;
18698 uint32_t event_flags;
18699 int npmport;
18700 int rval;
18701
18702 SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst,
18703 "Processing pmult event(s) on cport %d of controller %d",
18704 cport, ddi_get_instance(SATA_DIP(sata_hba_inst)));
18705
18706 /* First process events on port multiplier */
18707 mutex_enter(&cportinfo->cport_mutex);
18708 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
18709 event_flags = pmultinfo->pmult_event_flags;
18710
18711 /*
18712 * Reset event (of port multiplier) has higher priority because the
18713 * port multiplier itself might be failed or removed after reset.
18714 */
18715 if (event_flags & SATA_EVNT_DEVICE_RESET) {
18716 /*
18717 * The status of the sub-links are uncertain,
18718 * so mark all sub-ports as RESET
18719 */
18720 for (npmport = 0; npmport < SATA_NUM_PMPORTS(
18721 sata_hba_inst, cport); npmport ++) {
18722 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
18723 cport, npmport);
18724 if (pmportinfo == NULL) {
18725 /* That's weird. */
18726 SATA_LOG_D((sata_hba_inst, CE_WARN,
18727 "sata_hba_event_notify: "
18728 "invalid/un-implemented "
18729 "port %d:%d (%d ports), ",
18730 cport, npmport, SATA_NUM_PMPORTS(
18731 sata_hba_inst, cport)));
18732 continue;
18733 }
18734
18735 mutex_enter(&pmportinfo->pmport_mutex);
18736
18737 /* Mark all pmport to unknow state. */
18738 pmportinfo->pmport_state = SATA_STATE_UNKNOWN;
18739 /* Mark all pmports with link events. */
18740 pmportinfo->pmport_event_flags =
18741 (SATA_EVNT_LINK_ESTABLISHED|SATA_EVNT_LINK_LOST);
18742 mutex_exit(&pmportinfo->pmport_mutex);
18743 }
18744
18745 } else if (event_flags & SATA_EVNT_PMULT_LINK_CHANGED) {
18746 /*
18747 * We need probe the port multiplier to know what has
18748 * happened.
18749 */
18750 bzero(&sata_device, sizeof (sata_device_t));
18751 sata_device.satadev_rev = SATA_DEVICE_REV;
18752 sata_device.satadev_addr.cport = cport;
18753 sata_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
18754 sata_device.satadev_addr.qual = SATA_ADDR_PMULT;
18755
18756 mutex_exit(&cportinfo->cport_mutex);
18757 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18758 (SATA_DIP(sata_hba_inst), &sata_device);
18759 mutex_enter(&cportinfo->cport_mutex);
18760 if (rval != SATA_SUCCESS) {
18761 /* Something went wrong? Fail the port */
18762 cportinfo->cport_state = SATA_PSTATE_FAILED;
18763 mutex_exit(&cportinfo->cport_mutex);
18764 SATA_LOG_D((sata_hba_inst, CE_WARN,
18765 "SATA port %d probing failed", cport));
18766
18767 /* PMult structure must be released. */
18768 sata_free_pmult(sata_hba_inst, &sata_device);
18769 return;
18770 }
18771
18772 sata_update_port_info(sata_hba_inst, &sata_device);
18773
18774 /*
18775 * Sanity check - Port is active? Is the link active?
18776 * The device is still a port multiplier?
18777 */
18778 if ((cportinfo->cport_state &
18779 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
18780 ((cportinfo->cport_scr.sstatus &
18781 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) ||
18782 (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) {
18783 mutex_exit(&cportinfo->cport_mutex);
18784
18785 /* PMult structure must be released. */
18786 sata_free_pmult(sata_hba_inst, &sata_device);
18787 return;
18788 }
18789
18790 /* Probed succeed, set port ready. */
18791 cportinfo->cport_state |=
18792 SATA_STATE_PROBED | SATA_STATE_READY;
18793 }
18794
18795 /* Release port multiplier event flags. */
18796 pmultinfo->pmult_event_flags &=
18797 ~(SATA_EVNT_DEVICE_RESET|SATA_EVNT_PMULT_LINK_CHANGED);
18798 mutex_exit(&cportinfo->cport_mutex);
18799
18800 /*
18801 * Check all sub-links.
18802 */
18803 for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst, cport);
18804 npmport ++) {
18805 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport);
18806 mutex_enter(&pmportinfo->pmport_mutex);
18807 event_flags = pmportinfo->pmport_event_flags;
18808 mutex_exit(&pmportinfo->pmport_mutex);
18809 saddr = &pmportinfo->pmport_addr;
18810
18811 if ((event_flags &
18812 (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
18813 /*
18814 * Got port multiplier port event.
18815 * We need some hierarchy of event processing as they
18816 * are affecting each other:
18817 * 1. device detached/attached
18818 * 2. link events - link events may trigger device
18819 * detached or device attached events in some
18820 * circumstances.
18821 */
18822 if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
18823 sata_process_pmdevice_detached(sata_hba_inst,
18824 saddr);
18825 }
18826 if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
18827 sata_process_pmdevice_attached(sata_hba_inst,
18828 saddr);
18829 }
18830 if (event_flags & SATA_EVNT_LINK_ESTABLISHED ||
18831 event_flags & SATA_EVNT_LINK_LOST) {
18832 sata_process_pmport_link_events(sata_hba_inst,
18833 saddr);
18834 }
18835 if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
18836 sata_process_target_node_cleanup(
18837 sata_hba_inst, saddr);
18838 }
18839 }
18840
18841 /* Checking drive event(s). */
18842 mutex_enter(&pmportinfo->pmport_mutex);
18843 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
18844 pmportinfo->pmport_sata_drive != NULL) {
18845 event_flags = pmportinfo->pmport_sata_drive->
18846 satadrv_event_flags;
18847 if (event_flags & (SATA_EVNT_DEVICE_RESET |
18848 SATA_EVNT_INPROC_DEVICE_RESET)) {
18849
18850 /* Have device event */
18851 sata_process_pmdevice_reset(sata_hba_inst,
18852 saddr);
18853 }
18854 }
18855 mutex_exit(&pmportinfo->pmport_mutex);
18856
18857 /* Release PORT_BUSY flag */
18858 mutex_enter(&cportinfo->cport_mutex);
18859 cportinfo->cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
18860 mutex_exit(&cportinfo->cport_mutex);
18861 }
18862
18863 SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst,
18864 "[DONE] pmult event(s) on cport %d of controller %d",
18865 cport, ddi_get_instance(SATA_DIP(sata_hba_inst)));
18866 }
18867
18868 /*
18869 * Process HBA power level change reported by HBA driver.
18870 * Not implemented at this time - event is ignored.
18871 */
18872 static void
sata_process_cntrl_pwr_level_change(sata_hba_inst_t * sata_hba_inst)18873 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst)
18874 {
18875 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18876 "Processing controller power level change", NULL);
18877
18878 /* Ignoring it for now */
18879 mutex_enter(&sata_hba_inst->satahba_mutex);
18880 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
18881 mutex_exit(&sata_hba_inst->satahba_mutex);
18882 }
18883
18884 /*
18885 * Process port power level change reported by HBA driver.
18886 * Not implemented at this time - event is ignored.
18887 */
18888 static void
sata_process_port_pwr_change(sata_hba_inst_t * sata_hba_inst,sata_address_t * saddr)18889 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst,
18890 sata_address_t *saddr)
18891 {
18892 sata_cport_info_t *cportinfo;
18893
18894 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18895 "Processing port power level change", NULL);
18896
18897 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18898 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18899 /* Reset event flag */
18900 cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
18901 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18902 }
18903
18904 /*
18905 * Process port failure reported by HBA driver.
18906 * cports support only - no pmports.
18907 */
18908 static void
sata_process_port_failed_event(sata_hba_inst_t * sata_hba_inst,sata_address_t * saddr)18909 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst,
18910 sata_address_t *saddr)
18911 {
18912 sata_cport_info_t *cportinfo;
18913
18914 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18915 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18916 /* Reset event flag first */
18917 cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED;
18918 /* If the port is in SHUTDOWN or FAILED state, ignore this event. */
18919 if ((cportinfo->cport_state &
18920 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) {
18921 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18922 cport_mutex);
18923 return;
18924 }
18925 /* Fail the port */
18926 cportinfo->cport_state = SATA_PSTATE_FAILED;
18927 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18928 sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport);
18929 }
18930
18931 /*
18932 * Device Reset Event processing.
18933 * The sequence is managed by 3 stage flags:
18934 * - reset event reported,
18935 * - reset event being processed,
18936 * - request to clear device reset state.
18937 *
18938 * NOTE: This function has to be entered with cport mutex held. It exits with
18939 * mutex held as well, but can release mutex during the processing.
18940 */
18941 static void
sata_process_device_reset(sata_hba_inst_t * sata_hba_inst,sata_address_t * saddr)18942 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst,
18943 sata_address_t *saddr)
18944 {
18945 sata_drive_info_t old_sdinfo; /* local copy of the drive info */
18946 sata_drive_info_t *sdinfo;
18947 sata_cport_info_t *cportinfo;
18948 sata_device_t sata_device;
18949 int rval_probe, rval_set;
18950
18951 /* We only care about host sata cport for now */
18952 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18953 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18954 /*
18955 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
18956 * state, ignore reset event.
18957 */
18958 if (((cportinfo->cport_state &
18959 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
18960 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
18961 sdinfo->satadrv_event_flags &=
18962 ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
18963 return;
18964 }
18965
18966 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) ==
18967 SATA_DTYPE_PMULT)) {
18968 /*
18969 * Should not happened: this is already handled in
18970 * sata_hba_event_notify()
18971 */
18972 mutex_exit(&cportinfo->cport_mutex);
18973 goto done;
18974 }
18975
18976 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) &
18977 SATA_VALID_DEV_TYPE) == 0) {
18978 /*
18979 * This should not happen - coding error.
18980 * But we can recover, so do not panic, just clean up
18981 * and if in debug mode, log the message.
18982 */
18983 #ifdef SATA_DEBUG
18984 sata_log(sata_hba_inst, CE_WARN,
18985 "sata_process_device_reset: "
18986 "Invalid device type with sdinfo!", NULL);
18987 #endif
18988 sdinfo->satadrv_event_flags = 0;
18989 return;
18990 }
18991
18992 #ifdef SATA_DEBUG
18993 if ((sdinfo->satadrv_event_flags &
18994 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
18995 /* Nothing to do */
18996 /* Something is weird - why we are processing dev reset? */
18997 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18998 "No device reset event!!!!", NULL);
18999
19000 return;
19001 }
19002 if ((sdinfo->satadrv_event_flags &
19003 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
19004 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
19005 /* Something is weird - new device reset event */
19006 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19007 "Overlapping device reset events!", NULL);
19008 }
19009 #endif
19010 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19011 "Processing port %d device reset", saddr->cport);
19012
19013 /* Clear event flag */
19014 sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
19015
19016 /* It seems that we always need to check the port state first */
19017 sata_device.satadev_rev = SATA_DEVICE_REV;
19018 sata_device.satadev_addr = *saddr;
19019 /*
19020 * We have to exit mutex, because the HBA probe port function may
19021 * block on its own mutex.
19022 */
19023 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19024 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19025 (SATA_DIP(sata_hba_inst), &sata_device);
19026 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19027 sata_update_port_info(sata_hba_inst, &sata_device);
19028 if (rval_probe != SATA_SUCCESS) {
19029 /* Something went wrong? Fail the port */
19030 cportinfo->cport_state = SATA_PSTATE_FAILED;
19031 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
19032 if (sdinfo != NULL)
19033 sdinfo->satadrv_event_flags = 0;
19034 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19035 cport_mutex);
19036 SATA_LOG_D((sata_hba_inst, CE_WARN,
19037 "SATA port %d probing failed",
19038 saddr->cport));
19039 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19040 saddr->cport)->cport_mutex);
19041 return;
19042 }
19043 if ((sata_device.satadev_scr.sstatus &
19044 SATA_PORT_DEVLINK_UP_MASK) !=
19045 SATA_PORT_DEVLINK_UP ||
19046 sata_device.satadev_type == SATA_DTYPE_NONE) {
19047 /*
19048 * No device to process, anymore. Some other event processing
19049 * would or have already performed port info cleanup.
19050 * To be safe (HBA may need it), request clearing device
19051 * reset condition.
19052 */
19053 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
19054 if (sdinfo != NULL) {
19055 sdinfo->satadrv_event_flags &=
19056 ~SATA_EVNT_INPROC_DEVICE_RESET;
19057 sdinfo->satadrv_event_flags |=
19058 SATA_EVNT_CLEAR_DEVICE_RESET;
19059 }
19060 return;
19061 }
19062
19063 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
19064 if (sdinfo == NULL) {
19065 return;
19066 }
19067 if ((sdinfo->satadrv_event_flags &
19068 SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
19069 /*
19070 * Start tracking time for device feature restoration and
19071 * identification. Save current time (lbolt value).
19072 */
19073 sdinfo->satadrv_reset_time = ddi_get_lbolt();
19074 }
19075 /* Mark device reset processing as active */
19076 sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
19077
19078 old_sdinfo = *sdinfo; /* local copy of the drive info */
19079 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19080
19081 rval_set = sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1);
19082
19083 if (rval_set != SATA_SUCCESS) {
19084 /*
19085 * Restoring drive setting failed.
19086 * Probe the port first, to check if the port state has changed
19087 */
19088 sata_device.satadev_rev = SATA_DEVICE_REV;
19089 sata_device.satadev_addr = *saddr;
19090 sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
19091 /* probe port */
19092 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19093 (SATA_DIP(sata_hba_inst), &sata_device);
19094 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19095 cport_mutex);
19096 if (rval_probe == SATA_SUCCESS &&
19097 (sata_device.satadev_state &
19098 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
19099 (sata_device.satadev_scr.sstatus &
19100 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
19101 sata_device.satadev_type != SATA_DTYPE_NONE) {
19102 /*
19103 * We may retry this a bit later - in-process reset
19104 * condition should be already set.
19105 * Track retry time for device identification.
19106 */
19107 if ((cportinfo->cport_dev_type &
19108 SATA_VALID_DEV_TYPE) != 0 &&
19109 SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL &&
19110 sdinfo->satadrv_reset_time != 0) {
19111 clock_t cur_time = ddi_get_lbolt();
19112 /*
19113 * If the retry time limit was not
19114 * exceeded, retry.
19115 */
19116 if ((cur_time - sdinfo->satadrv_reset_time) <
19117 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
19118 mutex_enter(
19119 &sata_hba_inst->satahba_mutex);
19120 sata_hba_inst->satahba_event_flags |=
19121 SATA_EVNT_MAIN;
19122 mutex_exit(
19123 &sata_hba_inst->satahba_mutex);
19124 mutex_enter(&sata_mutex);
19125 sata_event_pending |= SATA_EVNT_MAIN;
19126 mutex_exit(&sata_mutex);
19127 return;
19128 }
19129 if (rval_set == SATA_RETRY) {
19130 /*
19131 * Setting drive features failed, but
19132 * the drive is still accessible,
19133 * so emit a warning message before
19134 * return.
19135 */
19136 mutex_exit(&SATA_CPORT_INFO(
19137 sata_hba_inst,
19138 saddr->cport)->cport_mutex);
19139 goto done;
19140 }
19141 }
19142 /* Fail the drive */
19143 sdinfo->satadrv_state = SATA_DSTATE_FAILED;
19144
19145 sata_log(sata_hba_inst, CE_WARN,
19146 "SATA device at port %d - device failed",
19147 saddr->cport);
19148
19149 DTRACE_PROBE(port_failed_f);
19150 }
19151 /*
19152 * No point of retrying - device failed or some other event
19153 * processing or already did or will do port info cleanup.
19154 * To be safe (HBA may need it),
19155 * request clearing device reset condition.
19156 */
19157 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
19158 sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
19159 sdinfo->satadrv_reset_time = 0;
19160 return;
19161 }
19162 done:
19163 /*
19164 * If setting of drive features failed, but the drive is still
19165 * accessible, emit a warning message.
19166 */
19167 if (rval_set == SATA_RETRY) {
19168 sata_log(sata_hba_inst, CE_WARN,
19169 "SATA device at port %d - desired setting could not be "
19170 "restored after reset. Device may not operate as expected.",
19171 saddr->cport);
19172 }
19173 /*
19174 * Raise the flag indicating that the next sata command could
19175 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
19176 * reset is reported.
19177 */
19178 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19179 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19180 sdinfo->satadrv_reset_time = 0;
19181 if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) {
19182 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19183 sdinfo->satadrv_event_flags &=
19184 ~SATA_EVNT_INPROC_DEVICE_RESET;
19185 sdinfo->satadrv_event_flags |=
19186 SATA_EVNT_CLEAR_DEVICE_RESET;
19187 }
19188 }
19189 }
19190
19191
19192 /*
19193 * Port Multiplier Port Device Reset Event processing.
19194 *
19195 * NOTE: This function has to be entered with pmport mutex held. It exits with
19196 * mutex held as well, but can release mutex during the processing.
19197 */
19198 static void
sata_process_pmdevice_reset(sata_hba_inst_t * sata_hba_inst,sata_address_t * saddr)19199 sata_process_pmdevice_reset(sata_hba_inst_t *sata_hba_inst,
19200 sata_address_t *saddr)
19201 {
19202 sata_drive_info_t old_sdinfo; /* local copy of the drive info */
19203 sata_drive_info_t *sdinfo = NULL;
19204 sata_cport_info_t *cportinfo = NULL;
19205 sata_pmport_info_t *pmportinfo = NULL;
19206 sata_pmult_info_t *pminfo = NULL;
19207 sata_device_t sata_device;
19208 uint8_t cport = saddr->cport;
19209 uint8_t pmport = saddr->pmport;
19210 int rval;
19211
19212 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19213 "Processing drive reset at port %d:%d", cport, pmport);
19214
19215 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
19216 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
19217 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport);
19218
19219 /*
19220 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
19221 * state, ignore reset event.
19222 */
19223 if (((cportinfo->cport_state &
19224 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
19225 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
19226 sdinfo->satadrv_event_flags &=
19227 ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
19228 return;
19229 }
19230
19231 if ((pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
19232 /*
19233 * This should not happen - coding error.
19234 * But we can recover, so do not panic, just clean up
19235 * and if in debug mode, log the message.
19236 */
19237 #ifdef SATA_DEBUG
19238 sata_log(sata_hba_inst, CE_WARN,
19239 "sata_process_pmdevice_reset: "
19240 "Invalid device type with sdinfo!", NULL);
19241 #endif
19242 sdinfo->satadrv_event_flags = 0;
19243 return;
19244 }
19245
19246 #ifdef SATA_DEBUG
19247 if ((sdinfo->satadrv_event_flags &
19248 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
19249 /* Nothing to do */
19250 /* Something is weird - why we are processing dev reset? */
19251 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19252 "No device reset event!!!!", NULL);
19253
19254 return;
19255 }
19256 if ((sdinfo->satadrv_event_flags &
19257 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
19258 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
19259 /* Something is weird - new device reset event */
19260 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19261 "Overlapping device reset events!", NULL);
19262 }
19263 #endif
19264 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19265 "Processing port %d:%d device reset", cport, pmport);
19266
19267 /* Clear event flag */
19268 sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
19269
19270 /* It seems that we always need to check the port state first */
19271 sata_device.satadev_rev = SATA_DEVICE_REV;
19272 sata_device.satadev_addr = *saddr;
19273 /*
19274 * We have to exit mutex, because the HBA probe port function may
19275 * block on its own mutex.
19276 */
19277 mutex_exit(&pmportinfo->pmport_mutex);
19278 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19279 (SATA_DIP(sata_hba_inst), &sata_device);
19280 mutex_enter(&pmportinfo->pmport_mutex);
19281
19282 sata_update_pmport_info(sata_hba_inst, &sata_device);
19283 if (rval != SATA_SUCCESS) {
19284 /* Something went wrong? Fail the port */
19285 pmportinfo->pmport_state = SATA_PSTATE_FAILED;
19286 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
19287 saddr->pmport);
19288 if (sdinfo != NULL)
19289 sdinfo->satadrv_event_flags = 0;
19290 mutex_exit(&pmportinfo->pmport_mutex);
19291 SATA_LOG_D((sata_hba_inst, CE_WARN,
19292 "SATA port %d:%d probing failed",
19293 saddr->cport, saddr->pmport));
19294 mutex_enter(&pmportinfo->pmport_mutex);
19295 return;
19296 }
19297 if ((sata_device.satadev_scr.sstatus &
19298 SATA_PORT_DEVLINK_UP_MASK) !=
19299 SATA_PORT_DEVLINK_UP ||
19300 sata_device.satadev_type == SATA_DTYPE_NONE) {
19301 /*
19302 * No device to process, anymore. Some other event processing
19303 * would or have already performed port info cleanup.
19304 * To be safe (HBA may need it), request clearing device
19305 * reset condition.
19306 */
19307 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
19308 saddr->pmport);
19309 if (sdinfo != NULL) {
19310 sdinfo->satadrv_event_flags &=
19311 ~SATA_EVNT_INPROC_DEVICE_RESET;
19312 /* must clear flags on cport */
19313 pminfo = SATA_PMULT_INFO(sata_hba_inst,
19314 saddr->cport);
19315 pminfo->pmult_event_flags |=
19316 SATA_EVNT_CLEAR_DEVICE_RESET;
19317 }
19318 return;
19319 }
19320
19321 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
19322 saddr->pmport);
19323 if (sdinfo == NULL) {
19324 return;
19325 }
19326 if ((sdinfo->satadrv_event_flags &
19327 SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
19328 /*
19329 * Start tracking time for device feature restoration and
19330 * identification. Save current time (lbolt value).
19331 */
19332 sdinfo->satadrv_reset_time = ddi_get_lbolt();
19333 }
19334 /* Mark device reset processing as active */
19335 sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
19336
19337 old_sdinfo = *sdinfo; /* local copy of the drive info */
19338 mutex_exit(&pmportinfo->pmport_mutex);
19339
19340 if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) ==
19341 SATA_FAILURE) {
19342 /*
19343 * Restoring drive setting failed.
19344 * Probe the port first, to check if the port state has changed
19345 */
19346 sata_device.satadev_rev = SATA_DEVICE_REV;
19347 sata_device.satadev_addr = *saddr;
19348 sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
19349
19350 /* probe port */
19351 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19352 (SATA_DIP(sata_hba_inst), &sata_device);
19353 mutex_enter(&pmportinfo->pmport_mutex);
19354 if (rval == SATA_SUCCESS &&
19355 (sata_device.satadev_state &
19356 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
19357 (sata_device.satadev_scr.sstatus &
19358 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
19359 sata_device.satadev_type != SATA_DTYPE_NONE) {
19360 /*
19361 * We may retry this a bit later - in-process reset
19362 * condition should be already set.
19363 * Track retry time for device identification.
19364 */
19365 if ((pmportinfo->pmport_dev_type &
19366 SATA_VALID_DEV_TYPE) != 0 &&
19367 SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL &&
19368 sdinfo->satadrv_reset_time != 0) {
19369 clock_t cur_time = ddi_get_lbolt();
19370 /*
19371 * If the retry time limit was not
19372 * exceeded, retry.
19373 */
19374 if ((cur_time - sdinfo->satadrv_reset_time) <
19375 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
19376 mutex_enter(
19377 &sata_hba_inst->satahba_mutex);
19378 sata_hba_inst->satahba_event_flags |=
19379 SATA_EVNT_MAIN;
19380 mutex_exit(
19381 &sata_hba_inst->satahba_mutex);
19382 mutex_enter(&sata_mutex);
19383 sata_event_pending |= SATA_EVNT_MAIN;
19384 mutex_exit(&sata_mutex);
19385 return;
19386 }
19387 }
19388 /* Fail the drive */
19389 sdinfo->satadrv_state = SATA_DSTATE_FAILED;
19390
19391 sata_log(sata_hba_inst, CE_WARN,
19392 "SATA device at port %d:%d - device failed",
19393 saddr->cport, saddr->pmport);
19394 } else {
19395 /*
19396 * No point of retrying - some other event processing
19397 * would or already did port info cleanup.
19398 * To be safe (HBA may need it),
19399 * request clearing device reset condition.
19400 */
19401 sdinfo->satadrv_event_flags |=
19402 SATA_EVNT_CLEAR_DEVICE_RESET;
19403 }
19404 sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
19405 sdinfo->satadrv_reset_time = 0;
19406 return;
19407 }
19408 /*
19409 * Raise the flag indicating that the next sata command could
19410 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
19411 * reset is reported.
19412 */
19413 mutex_enter(&pmportinfo->pmport_mutex);
19414 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
19415 sdinfo->satadrv_reset_time = 0;
19416 if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) {
19417 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19418 sdinfo->satadrv_event_flags &=
19419 ~SATA_EVNT_INPROC_DEVICE_RESET;
19420 /* must clear flags on cport */
19421 pminfo = SATA_PMULT_INFO(sata_hba_inst,
19422 saddr->cport);
19423 pminfo->pmult_event_flags |=
19424 SATA_EVNT_CLEAR_DEVICE_RESET;
19425 }
19426 }
19427 }
19428
19429 /*
19430 * Port Link Events processing.
19431 * Every link established event may involve device reset (due to
19432 * COMRESET signal, equivalent of the hard reset) so arbitrarily
19433 * set device reset event for an attached device (if any).
19434 * If the port is in SHUTDOWN or FAILED state, ignore link events.
19435 *
19436 * The link established event processing varies, depending on the state
19437 * of the target node, HBA hotplugging capabilities, state of the port.
19438 * If the link is not active, the link established event is ignored.
19439 * If HBA cannot detect device attachment and there is no target node,
19440 * the link established event triggers device attach event processing.
19441 * Else, link established event triggers device reset event processing.
19442 *
19443 * The link lost event processing varies, depending on a HBA hotplugging
19444 * capability and the state of the port (link active or not active).
19445 * If the link is active, the lost link event is ignored.
19446 * If HBA cannot detect device removal, the lost link event triggers
19447 * device detached event processing after link lost timeout.
19448 * Else, the event is ignored.
19449 *
19450 * NOTE: Port multiplier ports events are handled by
19451 * sata_process_pmport_link_events();
19452 */
19453 static void
sata_process_port_link_events(sata_hba_inst_t * sata_hba_inst,sata_address_t * saddr)19454 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst,
19455 sata_address_t *saddr)
19456 {
19457 sata_device_t sata_device;
19458 sata_cport_info_t *cportinfo;
19459 sata_drive_info_t *sdinfo;
19460 uint32_t event_flags;
19461 int rval;
19462
19463 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19464 "Processing port %d link event(s)", saddr->cport);
19465
19466 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19467 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19468 event_flags = cportinfo->cport_event_flags;
19469
19470 /* Reset event flags first */
19471 cportinfo->cport_event_flags &=
19472 ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
19473
19474 /* If the port is in SHUTDOWN or FAILED state, ignore link events. */
19475 if ((cportinfo->cport_state &
19476 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19477 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19478 cport_mutex);
19479 return;
19480 }
19481
19482 /*
19483 * For the sanity sake get current port state.
19484 * Set device address only. Other sata_device fields should be
19485 * set by HBA driver.
19486 */
19487 sata_device.satadev_rev = SATA_DEVICE_REV;
19488 sata_device.satadev_addr = *saddr;
19489 /*
19490 * We have to exit mutex, because the HBA probe port function may
19491 * block on its own mutex.
19492 */
19493 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19494 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19495 (SATA_DIP(sata_hba_inst), &sata_device);
19496 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19497 sata_update_port_info(sata_hba_inst, &sata_device);
19498 if (rval != SATA_SUCCESS) {
19499 /* Something went wrong? Fail the port */
19500 cportinfo->cport_state = SATA_PSTATE_FAILED;
19501 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19502 cport_mutex);
19503 SATA_LOG_D((sata_hba_inst, CE_WARN,
19504 "SATA port %d probing failed",
19505 saddr->cport));
19506 /*
19507 * We may want to release device info structure, but
19508 * it is not necessary.
19509 */
19510 return;
19511 } else {
19512 /* port probed successfully */
19513 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19514 }
19515 if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
19516
19517 if ((sata_device.satadev_scr.sstatus &
19518 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
19519 /* Ignore event */
19520 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19521 "Ignoring port %d link established event - "
19522 "link down",
19523 saddr->cport);
19524 goto linklost;
19525 }
19526
19527 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19528 "Processing port %d link established event",
19529 saddr->cport);
19530
19531 /*
19532 * For the sanity sake check if a device is attached - check
19533 * return state of a port probing.
19534 */
19535 if (sata_device.satadev_type != SATA_DTYPE_NONE) {
19536 /*
19537 * HBA port probe indicated that there is a device
19538 * attached. Check if the framework had device info
19539 * structure attached for this device.
19540 */
19541 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
19542 ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) !=
19543 NULL);
19544
19545 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19546 if ((sdinfo->satadrv_type &
19547 SATA_VALID_DEV_TYPE) != 0) {
19548 /*
19549 * Dev info structure is present.
19550 * If dev_type is set to known type in
19551 * the framework's drive info struct
19552 * then the device existed before and
19553 * the link was probably lost
19554 * momentarily - in such case
19555 * we may want to check device
19556 * identity.
19557 * Identity check is not supported now.
19558 *
19559 * Link established event
19560 * triggers device reset event.
19561 */
19562 (SATA_CPORTINFO_DRV_INFO(cportinfo))->
19563 satadrv_event_flags |=
19564 SATA_EVNT_DEVICE_RESET;
19565 }
19566 } else if (cportinfo->cport_dev_type ==
19567 SATA_DTYPE_NONE) {
19568 /*
19569 * We got new device attached! If HBA does not
19570 * generate device attached events, trigger it
19571 * here.
19572 */
19573 if (!(SATA_FEATURES(sata_hba_inst) &
19574 SATA_CTLF_HOTPLUG)) {
19575 cportinfo->cport_event_flags |=
19576 SATA_EVNT_DEVICE_ATTACHED;
19577 }
19578 }
19579 /* Reset link lost timeout */
19580 cportinfo->cport_link_lost_time = 0;
19581 }
19582 }
19583 linklost:
19584 if (event_flags & SATA_EVNT_LINK_LOST) {
19585 if ((sata_device.satadev_scr.sstatus &
19586 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
19587 /* Ignore event */
19588 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19589 "Ignoring port %d link lost event - link is up",
19590 saddr->cport);
19591 goto done;
19592 }
19593 #ifdef SATA_DEBUG
19594 if (cportinfo->cport_link_lost_time == 0) {
19595 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19596 "Processing port %d link lost event",
19597 saddr->cport);
19598 }
19599 #endif
19600 /*
19601 * When HBA cannot generate device attached/detached events,
19602 * we need to track link lost time and eventually generate
19603 * device detach event.
19604 */
19605 if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
19606 /* We are tracking link lost time */
19607 if (cportinfo->cport_link_lost_time == 0) {
19608 /* save current time (lbolt value) */
19609 cportinfo->cport_link_lost_time =
19610 ddi_get_lbolt();
19611 /* just keep link lost event */
19612 cportinfo->cport_event_flags |=
19613 SATA_EVNT_LINK_LOST;
19614 } else {
19615 clock_t cur_time = ddi_get_lbolt();
19616 if ((cur_time -
19617 cportinfo->cport_link_lost_time) >=
19618 drv_usectohz(
19619 SATA_EVNT_LINK_LOST_TIMEOUT)) {
19620 /* trigger device detach event */
19621 cportinfo->cport_event_flags |=
19622 SATA_EVNT_DEVICE_DETACHED;
19623 cportinfo->cport_link_lost_time = 0;
19624 SATADBG1(SATA_DBG_EVENTS,
19625 sata_hba_inst,
19626 "Triggering port %d "
19627 "device detached event",
19628 saddr->cport);
19629 } else {
19630 /* keep link lost event */
19631 cportinfo->cport_event_flags |=
19632 SATA_EVNT_LINK_LOST;
19633 }
19634 }
19635 }
19636 /*
19637 * We could change port state to disable/delay access to
19638 * the attached device until the link is recovered.
19639 */
19640 }
19641 done:
19642 event_flags = cportinfo->cport_event_flags;
19643 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19644 if (event_flags != 0) {
19645 mutex_enter(&sata_hba_inst->satahba_mutex);
19646 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19647 mutex_exit(&sata_hba_inst->satahba_mutex);
19648 mutex_enter(&sata_mutex);
19649 sata_event_pending |= SATA_EVNT_MAIN;
19650 mutex_exit(&sata_mutex);
19651 }
19652 }
19653
19654 /*
19655 * Port Multiplier Port Link Events processing.
19656 */
19657 static void
sata_process_pmport_link_events(sata_hba_inst_t * sata_hba_inst,sata_address_t * saddr)19658 sata_process_pmport_link_events(sata_hba_inst_t *sata_hba_inst,
19659 sata_address_t *saddr)
19660 {
19661 sata_device_t sata_device;
19662 sata_pmport_info_t *pmportinfo = NULL;
19663 sata_drive_info_t *sdinfo = NULL;
19664 uint32_t event_flags;
19665 uint8_t cport = saddr->cport;
19666 uint8_t pmport = saddr->pmport;
19667 int rval;
19668
19669 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19670 "Processing port %d:%d link event(s)",
19671 cport, pmport);
19672
19673 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
19674 mutex_enter(&pmportinfo->pmport_mutex);
19675 event_flags = pmportinfo->pmport_event_flags;
19676
19677 /* Reset event flags first */
19678 pmportinfo->pmport_event_flags &=
19679 ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
19680
19681 /* If the port is in SHUTDOWN or FAILED state, ignore link events. */
19682 if ((pmportinfo->pmport_state &
19683 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19684 mutex_exit(&pmportinfo->pmport_mutex);
19685 return;
19686 }
19687
19688 /*
19689 * For the sanity sake get current port state.
19690 * Set device address only. Other sata_device fields should be
19691 * set by HBA driver.
19692 */
19693 sata_device.satadev_rev = SATA_DEVICE_REV;
19694 sata_device.satadev_addr = *saddr;
19695 /*
19696 * We have to exit mutex, because the HBA probe port function may
19697 * block on its own mutex.
19698 */
19699 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19700 saddr->pmport));
19701 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19702 (SATA_DIP(sata_hba_inst), &sata_device);
19703 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19704 saddr->pmport));
19705 sata_update_pmport_info(sata_hba_inst, &sata_device);
19706 if (rval != SATA_SUCCESS) {
19707 /* Something went wrong? Fail the port */
19708 pmportinfo->pmport_state = SATA_PSTATE_FAILED;
19709 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19710 saddr->pmport));
19711 SATA_LOG_D((sata_hba_inst, CE_WARN,
19712 "SATA port %d:%d probing failed",
19713 saddr->cport, saddr->pmport));
19714 /*
19715 * We may want to release device info structure, but
19716 * it is not necessary.
19717 */
19718 return;
19719 } else {
19720 /* port probed successfully */
19721 pmportinfo->pmport_state |=
19722 SATA_STATE_PROBED | SATA_STATE_READY;
19723 }
19724 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst,
19725 saddr->cport, saddr->pmport));
19726 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst,
19727 saddr->cport, saddr->pmport));
19728 if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
19729
19730 if ((sata_device.satadev_scr.sstatus &
19731 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
19732 /* Ignore event */
19733 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19734 "Ignoring port %d:%d link established event - "
19735 "link down",
19736 saddr->cport, saddr->pmport);
19737 goto linklost;
19738 }
19739
19740 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19741 "Processing port %d:%d link established event",
19742 cport, pmport);
19743
19744 /*
19745 * For the sanity sake check if a device is attached - check
19746 * return state of a port probing.
19747 */
19748 if (sata_device.satadev_type != SATA_DTYPE_NONE &&
19749 sata_device.satadev_type != SATA_DTYPE_PMULT) {
19750 /*
19751 * HBA port probe indicated that there is a device
19752 * attached. Check if the framework had device info
19753 * structure attached for this device.
19754 */
19755 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
19756 ASSERT(SATA_PMPORTINFO_DRV_INFO(pmportinfo) !=
19757 NULL);
19758
19759 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19760 if ((sdinfo->satadrv_type &
19761 SATA_VALID_DEV_TYPE) != 0) {
19762 /*
19763 * Dev info structure is present.
19764 * If dev_type is set to known type in
19765 * the framework's drive info struct
19766 * then the device existed before and
19767 * the link was probably lost
19768 * momentarily - in such case
19769 * we may want to check device
19770 * identity.
19771 * Identity check is not supported now.
19772 *
19773 * Link established event
19774 * triggers device reset event.
19775 */
19776 (SATA_PMPORTINFO_DRV_INFO(pmportinfo))->
19777 satadrv_event_flags |=
19778 SATA_EVNT_DEVICE_RESET;
19779 }
19780 } else if (pmportinfo->pmport_dev_type ==
19781 SATA_DTYPE_NONE) {
19782 /*
19783 * We got new device attached! If HBA does not
19784 * generate device attached events, trigger it
19785 * here.
19786 */
19787 if (!(SATA_FEATURES(sata_hba_inst) &
19788 SATA_CTLF_HOTPLUG)) {
19789 pmportinfo->pmport_event_flags |=
19790 SATA_EVNT_DEVICE_ATTACHED;
19791 }
19792 }
19793 /* Reset link lost timeout */
19794 pmportinfo->pmport_link_lost_time = 0;
19795 }
19796 }
19797 linklost:
19798 if (event_flags & SATA_EVNT_LINK_LOST) {
19799 #ifdef SATA_DEBUG
19800 if (pmportinfo->pmport_link_lost_time == 0) {
19801 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19802 "Processing port %d:%d link lost event",
19803 saddr->cport, saddr->pmport);
19804 }
19805 #endif
19806 if ((sata_device.satadev_scr.sstatus &
19807 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
19808 /* Ignore event */
19809 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19810 "Ignoring port %d:%d link lost event - link is up",
19811 saddr->cport, saddr->pmport);
19812 goto done;
19813 }
19814 /*
19815 * When HBA cannot generate device attached/detached events,
19816 * we need to track link lost time and eventually generate
19817 * device detach event.
19818 */
19819 if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
19820 /* We are tracking link lost time */
19821 if (pmportinfo->pmport_link_lost_time == 0) {
19822 /* save current time (lbolt value) */
19823 pmportinfo->pmport_link_lost_time =
19824 ddi_get_lbolt();
19825 /* just keep link lost event */
19826 pmportinfo->pmport_event_flags |=
19827 SATA_EVNT_LINK_LOST;
19828 } else {
19829 clock_t cur_time = ddi_get_lbolt();
19830 if ((cur_time -
19831 pmportinfo->pmport_link_lost_time) >=
19832 drv_usectohz(
19833 SATA_EVNT_LINK_LOST_TIMEOUT)) {
19834 /* trigger device detach event */
19835 pmportinfo->pmport_event_flags |=
19836 SATA_EVNT_DEVICE_DETACHED;
19837 pmportinfo->pmport_link_lost_time = 0;
19838 SATADBG2(SATA_DBG_EVENTS,
19839 sata_hba_inst,
19840 "Triggering port %d:%d "
19841 "device detached event",
19842 saddr->cport, saddr->pmport);
19843 } else {
19844 /* keep link lost event */
19845 pmportinfo->pmport_event_flags |=
19846 SATA_EVNT_LINK_LOST;
19847 }
19848 }
19849 }
19850 /*
19851 * We could change port state to disable/delay access to
19852 * the attached device until the link is recovered.
19853 */
19854 }
19855 done:
19856 event_flags = pmportinfo->pmport_event_flags;
19857 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19858 saddr->pmport));
19859 if (event_flags != 0) {
19860 mutex_enter(&sata_hba_inst->satahba_mutex);
19861 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19862 mutex_exit(&sata_hba_inst->satahba_mutex);
19863 mutex_enter(&sata_mutex);
19864 sata_event_pending |= SATA_EVNT_MAIN;
19865 mutex_exit(&sata_mutex);
19866 }
19867 }
19868
19869 /*
19870 * Device Detached Event processing.
19871 * Port is probed to find if a device is really gone. If so,
19872 * the device info structure is detached from the SATA port info structure
19873 * and released.
19874 * Port status is updated.
19875 *
19876 * NOTE: Port multiplier ports events are handled by
19877 * sata_process_pmdevice_detached()
19878 */
19879 static void
sata_process_device_detached(sata_hba_inst_t * sata_hba_inst,sata_address_t * saddr)19880 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst,
19881 sata_address_t *saddr)
19882 {
19883 sata_cport_info_t *cportinfo;
19884 sata_pmport_info_t *pmportinfo;
19885 sata_drive_info_t *sdevinfo;
19886 sata_device_t sata_device;
19887 sata_address_t pmport_addr;
19888 char name[16];
19889 uint8_t cport = saddr->cport;
19890 int npmport;
19891 int rval;
19892
19893 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19894 "Processing port %d device detached", saddr->cport);
19895
19896 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19897 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19898 /* Clear event flag */
19899 cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
19900
19901 /* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
19902 if ((cportinfo->cport_state &
19903 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19904 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19905 cport_mutex);
19906 return;
19907 }
19908 /* For sanity, re-probe the port */
19909 sata_device.satadev_rev = SATA_DEVICE_REV;
19910 sata_device.satadev_addr = *saddr;
19911
19912 /*
19913 * We have to exit mutex, because the HBA probe port function may
19914 * block on its own mutex.
19915 */
19916 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19917 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19918 (SATA_DIP(sata_hba_inst), &sata_device);
19919 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19920 sata_update_port_info(sata_hba_inst, &sata_device);
19921 if (rval != SATA_SUCCESS) {
19922 /* Something went wrong? Fail the port */
19923 cportinfo->cport_state = SATA_PSTATE_FAILED;
19924 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19925 cport_mutex);
19926 SATA_LOG_D((sata_hba_inst, CE_WARN,
19927 "SATA port %d probing failed",
19928 saddr->cport));
19929 /*
19930 * We may want to release device info structure, but
19931 * it is not necessary.
19932 */
19933 return;
19934 } else {
19935 /* port probed successfully */
19936 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19937 }
19938 /*
19939 * Check if a device is still attached. For sanity, check also
19940 * link status - if no link, there is no device.
19941 */
19942 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
19943 SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
19944 SATA_DTYPE_NONE) {
19945 /*
19946 * Device is still attached - ignore detach event.
19947 */
19948 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19949 cport_mutex);
19950 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19951 "Ignoring detach - device still attached to port %d",
19952 sata_device.satadev_addr.cport);
19953 return;
19954 }
19955 /*
19956 * We need to detach and release device info structure here
19957 */
19958 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
19959 /*
19960 * A port-multiplier is removed.
19961 *
19962 * Calling sata_process_pmdevice_detached() does not work
19963 * here. The port multiplier is gone, so we cannot probe
19964 * sub-port any more and all pmult-related data structure must
19965 * be de-allocated immediately. Following structure of every
19966 * implemented sub-port behind the pmult are required to
19967 * released.
19968 *
19969 * - attachment point
19970 * - target node
19971 * - sata_drive_info
19972 * - sata_pmport_info
19973 */
19974 for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst,
19975 cport); npmport ++) {
19976 SATADBG2(SATA_DBG_PMULT|SATA_DBG_EVENTS_PROC,
19977 sata_hba_inst,
19978 "Detaching target node at port %d:%d",
19979 cport, npmport);
19980
19981 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
19982
19983 /* Remove attachment point. */
19984 name[0] = '\0';
19985 (void) sprintf(name, "%d.%d", cport, npmport);
19986 ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name);
19987 sata_log(sata_hba_inst, CE_NOTE,
19988 "Remove attachment point of port %d:%d",
19989 cport, npmport);
19990
19991 /* Remove target node */
19992 pmport_addr.cport = cport;
19993 pmport_addr.pmport = (uint8_t)npmport;
19994 pmport_addr.qual = SATA_ADDR_PMPORT;
19995 sata_remove_target_node(sata_hba_inst, &pmport_addr);
19996
19997 mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
19998
19999 /* Release sata_pmport_info & sata_drive_info. */
20000 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
20001 cport, npmport);
20002 ASSERT(pmportinfo != NULL);
20003
20004 sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
20005 if (sdevinfo != NULL) {
20006 (void) kmem_free((void *) sdevinfo,
20007 sizeof (sata_drive_info_t));
20008 }
20009
20010 /* Release sata_pmport_info at last */
20011 (void) kmem_free((void *) pmportinfo,
20012 sizeof (sata_pmport_info_t));
20013 }
20014
20015 /* Finally, release sata_pmult_info */
20016 (void) kmem_free((void *)
20017 SATA_CPORTINFO_PMULT_INFO(cportinfo),
20018 sizeof (sata_pmult_info_t));
20019 SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL;
20020
20021 sata_log(sata_hba_inst, CE_WARN,
20022 "SATA port-multiplier detached at port %d", cport);
20023
20024 cportinfo->cport_dev_type = SATA_DTYPE_NONE;
20025 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20026 saddr->cport)->cport_mutex);
20027 } else {
20028 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
20029 sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
20030 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
20031 (void) kmem_free((void *)sdevinfo,
20032 sizeof (sata_drive_info_t));
20033 }
20034 sata_log(sata_hba_inst, CE_WARN,
20035 "SATA device detached at port %d", cport);
20036
20037 cportinfo->cport_dev_type = SATA_DTYPE_NONE;
20038 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20039 saddr->cport)->cport_mutex);
20040
20041 /*
20042 * Try to offline a device and remove target node
20043 * if it still exists
20044 */
20045 sata_remove_target_node(sata_hba_inst, saddr);
20046 }
20047
20048
20049 /*
20050 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
20051 * with the hint: SE_HINT_REMOVE
20052 */
20053 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
20054 }
20055
20056 /*
20057 * Port Multiplier Port Device Deattached Event processing.
20058 *
20059 * NOTE: No Mutex should be hold.
20060 */
20061 static void
sata_process_pmdevice_detached(sata_hba_inst_t * sata_hba_inst,sata_address_t * saddr)20062 sata_process_pmdevice_detached(sata_hba_inst_t *sata_hba_inst,
20063 sata_address_t *saddr)
20064 {
20065 sata_pmport_info_t *pmportinfo;
20066 sata_drive_info_t *sdevinfo;
20067 sata_device_t sata_device;
20068 int rval;
20069 uint8_t cport, pmport;
20070
20071 cport = saddr->cport;
20072 pmport = saddr->pmport;
20073
20074 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20075 "Processing port %d:%d device detached",
20076 cport, pmport);
20077
20078 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
20079 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
20080
20081 /* Clear event flag */
20082 pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
20083
20084 /* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
20085 if ((pmportinfo->pmport_state &
20086 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
20087 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
20088 return;
20089 }
20090 /* For sanity, re-probe the port */
20091 sata_device.satadev_rev = SATA_DEVICE_REV;
20092 sata_device.satadev_addr = *saddr;
20093
20094 /*
20095 * We have to exit mutex, because the HBA probe port function may
20096 * block on its own mutex.
20097 */
20098 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
20099 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
20100 (SATA_DIP(sata_hba_inst), &sata_device);
20101 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
20102 sata_update_pmport_info(sata_hba_inst, &sata_device);
20103 if (rval != SATA_SUCCESS) {
20104 /* Something went wrong? Fail the port */
20105 pmportinfo->pmport_state = SATA_PSTATE_FAILED;
20106 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
20107 SATA_LOG_D((sata_hba_inst, CE_WARN,
20108 "SATA port %d:%d probing failed",
20109 saddr->pmport));
20110 /*
20111 * We may want to release device info structure, but
20112 * it is not necessary.
20113 */
20114 return;
20115 } else {
20116 /* port probed successfully */
20117 pmportinfo->pmport_state |=
20118 SATA_STATE_PROBED | SATA_STATE_READY;
20119 }
20120 /*
20121 * Check if a device is still attached. For sanity, check also
20122 * link status - if no link, there is no device.
20123 */
20124 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
20125 SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
20126 SATA_DTYPE_NONE) {
20127 /*
20128 * Device is still attached - ignore detach event.
20129 */
20130 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
20131 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20132 "Ignoring detach - device still attached to port %d",
20133 sata_device.satadev_addr.pmport);
20134 return;
20135 }
20136 /*
20137 * We need to detach and release device info structure here
20138 */
20139 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20140 sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
20141 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
20142 (void) kmem_free((void *)sdevinfo,
20143 sizeof (sata_drive_info_t));
20144 }
20145 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
20146 /*
20147 * Device cannot be reached anymore, even if the target node may be
20148 * still present.
20149 */
20150 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
20151
20152 /*
20153 * Try to offline a device and remove target node if it still exists
20154 */
20155 sata_remove_target_node(sata_hba_inst, saddr);
20156
20157 /*
20158 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
20159 * with the hint: SE_HINT_REMOVE
20160 */
20161 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
20162 }
20163
20164
20165 /*
20166 * Device Attached Event processing.
20167 * Port state is checked to verify that a device is really attached. If so,
20168 * the device info structure is created and attached to the SATA port info
20169 * structure.
20170 *
20171 * If attached device cannot be identified or set-up, the retry for the
20172 * attach processing is set-up. Subsequent daemon run would try again to
20173 * identify the device, until the time limit is reached
20174 * (SATA_DEV_IDENTIFY_TIMEOUT).
20175 *
20176 * This function cannot be called in interrupt context (it may sleep).
20177 *
20178 * NOTE: Port multiplier ports events are handled by
20179 * sata_process_pmdevice_attached()
20180 */
20181 static void
sata_process_device_attached(sata_hba_inst_t * sata_hba_inst,sata_address_t * saddr)20182 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst,
20183 sata_address_t *saddr)
20184 {
20185 sata_cport_info_t *cportinfo = NULL;
20186 sata_drive_info_t *sdevinfo = NULL;
20187 sata_pmult_info_t *pmultinfo = NULL;
20188 sata_pmport_info_t *pmportinfo = NULL;
20189 sata_device_t sata_device;
20190 dev_info_t *tdip;
20191 uint32_t event_flags = 0, pmult_event_flags = 0;
20192 int rval;
20193 int npmport;
20194
20195 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20196 "Processing port %d device attached", saddr->cport);
20197
20198 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
20199 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20200
20201 /* Clear attach event flag first */
20202 cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
20203
20204 /* If the port is in SHUTDOWN or FAILED state, ignore event. */
20205 if ((cportinfo->cport_state &
20206 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
20207 cportinfo->cport_dev_attach_time = 0;
20208 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
20209 cport_mutex);
20210 return;
20211 }
20212
20213 /*
20214 * If the sata_drive_info structure is found attached to the port info,
20215 * despite the fact the device was removed and now it is re-attached,
20216 * the old drive info structure was not removed.
20217 * Arbitrarily release device info structure.
20218 */
20219 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
20220 sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
20221 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
20222 (void) kmem_free((void *)sdevinfo,
20223 sizeof (sata_drive_info_t));
20224 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20225 "Arbitrarily detaching old device info.", NULL);
20226 }
20227 cportinfo->cport_dev_type = SATA_DTYPE_NONE;
20228
20229 /* For sanity, re-probe the port */
20230 sata_device.satadev_rev = SATA_DEVICE_REV;
20231 sata_device.satadev_addr = *saddr;
20232
20233 /*
20234 * We have to exit mutex, because the HBA probe port function may
20235 * block on its own mutex.
20236 */
20237 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20238 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
20239 (SATA_DIP(sata_hba_inst), &sata_device);
20240 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20241 sata_update_port_info(sata_hba_inst, &sata_device);
20242 if (rval != SATA_SUCCESS) {
20243 /* Something went wrong? Fail the port */
20244 cportinfo->cport_state = SATA_PSTATE_FAILED;
20245 cportinfo->cport_dev_attach_time = 0;
20246 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
20247 cport_mutex);
20248 SATA_LOG_D((sata_hba_inst, CE_WARN,
20249 "SATA port %d probing failed",
20250 saddr->cport));
20251 return;
20252 } else {
20253 /* port probed successfully */
20254 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
20255 }
20256 /*
20257 * Check if a device is still attached. For sanity, check also
20258 * link status - if no link, there is no device.
20259 */
20260 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
20261 SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
20262 SATA_DTYPE_NONE) {
20263 /*
20264 * No device - ignore attach event.
20265 */
20266 cportinfo->cport_dev_attach_time = 0;
20267 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
20268 cport_mutex);
20269 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20270 "Ignoring attach - no device connected to port %d",
20271 sata_device.satadev_addr.cport);
20272 return;
20273 }
20274
20275 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20276 /*
20277 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
20278 * with the hint: SE_HINT_INSERT
20279 */
20280 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
20281
20282 /*
20283 * Port reprobing will take care of the creation of the device
20284 * info structure and determination of the device type.
20285 */
20286 sata_device.satadev_addr = *saddr;
20287 (void) sata_reprobe_port(sata_hba_inst, &sata_device,
20288 SATA_DEV_IDENTIFY_NORETRY);
20289
20290 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
20291 cport_mutex);
20292 if ((cportinfo->cport_state & SATA_STATE_READY) &&
20293 (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) {
20294 /* Some device is attached to the port */
20295 if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) {
20296 /*
20297 * A device was not successfully attached.
20298 * Track retry time for device identification.
20299 */
20300 if (cportinfo->cport_dev_attach_time != 0) {
20301 clock_t cur_time = ddi_get_lbolt();
20302 /*
20303 * If the retry time limit was not exceeded,
20304 * reinstate attach event.
20305 */
20306 if ((cur_time -
20307 cportinfo->cport_dev_attach_time) <
20308 drv_usectohz(
20309 SATA_DEV_IDENTIFY_TIMEOUT)) {
20310 /* OK, restore attach event */
20311 cportinfo->cport_event_flags |=
20312 SATA_EVNT_DEVICE_ATTACHED;
20313 } else {
20314 /* Timeout - cannot identify device */
20315 cportinfo->cport_dev_attach_time = 0;
20316 sata_log(sata_hba_inst,
20317 CE_WARN,
20318 "Could not identify SATA device "
20319 "at port %d",
20320 saddr->cport);
20321 }
20322 } else {
20323 /*
20324 * Start tracking time for device
20325 * identification.
20326 * Save current time (lbolt value).
20327 */
20328 cportinfo->cport_dev_attach_time =
20329 ddi_get_lbolt();
20330 /* Restore attach event */
20331 cportinfo->cport_event_flags |=
20332 SATA_EVNT_DEVICE_ATTACHED;
20333 }
20334 } else if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
20335 cportinfo->cport_dev_attach_time = 0;
20336 sata_log(sata_hba_inst, CE_NOTE,
20337 "SATA port-multiplier detected at port %d",
20338 saddr->cport);
20339
20340 if (SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL) {
20341 /* Log the info of new port multiplier */
20342 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20343 saddr->cport)->cport_mutex);
20344 sata_show_pmult_info(sata_hba_inst,
20345 &sata_device);
20346 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20347 saddr->cport)->cport_mutex);
20348 }
20349
20350 ASSERT(SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL);
20351 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
20352 for (npmport = 0; npmport <
20353 pmultinfo->pmult_num_dev_ports; npmport++) {
20354 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
20355 saddr->cport, npmport);
20356 ASSERT(pmportinfo != NULL);
20357
20358 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20359 saddr->cport)->cport_mutex);
20360 mutex_enter(&pmportinfo->pmport_mutex);
20361 /* Marked all pmports with link events. */
20362 pmportinfo->pmport_event_flags =
20363 SATA_EVNT_LINK_ESTABLISHED;
20364 pmult_event_flags |=
20365 pmportinfo->pmport_event_flags;
20366 mutex_exit(&pmportinfo->pmport_mutex);
20367 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20368 saddr->cport)->cport_mutex);
20369 }
20370 /* Auto-online is not available for PMult now. */
20371
20372 } else {
20373 /*
20374 * If device was successfully attached, the subsequent
20375 * action depends on a state of the
20376 * sata_auto_online variable. If it is set to zero.
20377 * an explicit 'configure' command will be needed to
20378 * configure it. If its value is non-zero, we will
20379 * attempt to online (configure) the device.
20380 * First, log the message indicating that a device
20381 * was attached.
20382 */
20383 cportinfo->cport_dev_attach_time = 0;
20384 sata_log(sata_hba_inst, CE_WARN,
20385 "SATA device detected at port %d", saddr->cport);
20386
20387 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
20388 sata_drive_info_t new_sdinfo;
20389
20390 /* Log device info data */
20391 new_sdinfo = *(SATA_CPORTINFO_DRV_INFO(
20392 cportinfo));
20393 sata_show_drive_info(sata_hba_inst,
20394 &new_sdinfo);
20395 }
20396
20397 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20398 saddr->cport)->cport_mutex);
20399
20400 /*
20401 * Make sure that there is no target node for that
20402 * device. If so, release it. It should not happen,
20403 * unless we had problem removing the node when
20404 * device was detached.
20405 */
20406 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
20407 saddr->cport, saddr->pmport);
20408 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20409 saddr->cport)->cport_mutex);
20410 if (tdip != NULL) {
20411
20412 #ifdef SATA_DEBUG
20413 if ((cportinfo->cport_event_flags &
20414 SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
20415 sata_log(sata_hba_inst, CE_WARN,
20416 "sata_process_device_attached: "
20417 "old device target node exists!");
20418 #endif
20419 /*
20420 * target node exists - try to unconfigure
20421 * device and remove the node.
20422 */
20423 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20424 saddr->cport)->cport_mutex);
20425 rval = ndi_devi_offline(tdip,
20426 NDI_DEVI_REMOVE);
20427 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20428 saddr->cport)->cport_mutex);
20429
20430 if (rval == NDI_SUCCESS) {
20431 cportinfo->cport_event_flags &=
20432 ~SATA_EVNT_TARGET_NODE_CLEANUP;
20433 cportinfo->cport_tgtnode_clean = B_TRUE;
20434 } else {
20435 /*
20436 * PROBLEM - the target node remained
20437 * and it belongs to a previously
20438 * attached device.
20439 * This happens when the file was open
20440 * or the node was waiting for
20441 * resources at the time the
20442 * associated device was removed.
20443 * Instruct event daemon to retry the
20444 * cleanup later.
20445 */
20446 sata_log(sata_hba_inst,
20447 CE_WARN,
20448 "Application(s) accessing "
20449 "previously attached SATA "
20450 "device have to release "
20451 "it before newly inserted "
20452 "device can be made accessible.",
20453 saddr->cport);
20454 cportinfo->cport_event_flags |=
20455 SATA_EVNT_TARGET_NODE_CLEANUP;
20456 cportinfo->cport_tgtnode_clean =
20457 B_FALSE;
20458 }
20459 }
20460 if (sata_auto_online != 0) {
20461 cportinfo->cport_event_flags |=
20462 SATA_EVNT_AUTOONLINE_DEVICE;
20463 }
20464
20465 }
20466 } else {
20467 cportinfo->cport_dev_attach_time = 0;
20468 }
20469
20470 event_flags = cportinfo->cport_event_flags;
20471 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20472 if (event_flags != 0 || pmult_event_flags != 0) {
20473 mutex_enter(&sata_hba_inst->satahba_mutex);
20474 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20475 mutex_exit(&sata_hba_inst->satahba_mutex);
20476 mutex_enter(&sata_mutex);
20477 sata_event_pending |= SATA_EVNT_MAIN;
20478 mutex_exit(&sata_mutex);
20479 }
20480 }
20481
20482 /*
20483 * Port Multiplier Port Device Attached Event processing.
20484 *
20485 * NOTE: No Mutex should be hold.
20486 */
20487 static void
sata_process_pmdevice_attached(sata_hba_inst_t * sata_hba_inst,sata_address_t * saddr)20488 sata_process_pmdevice_attached(sata_hba_inst_t *sata_hba_inst,
20489 sata_address_t *saddr)
20490 {
20491 sata_pmport_info_t *pmportinfo;
20492 sata_drive_info_t *sdinfo;
20493 sata_device_t sata_device;
20494 dev_info_t *tdip;
20495 uint32_t event_flags;
20496 uint8_t cport = saddr->cport;
20497 uint8_t pmport = saddr->pmport;
20498 int rval;
20499
20500 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20501 "Processing port %d:%d device attached", cport, pmport);
20502
20503 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
20504
20505 mutex_enter(&pmportinfo->pmport_mutex);
20506
20507 /* Clear attach event flag first */
20508 pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
20509
20510 /* If the port is in SHUTDOWN or FAILED state, ignore event. */
20511 if ((pmportinfo->pmport_state &
20512 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
20513 pmportinfo->pmport_dev_attach_time = 0;
20514 mutex_exit(&pmportinfo->pmport_mutex);
20515 return;
20516 }
20517
20518 /*
20519 * If the sata_drive_info structure is found attached to the port info,
20520 * despite the fact the device was removed and now it is re-attached,
20521 * the old drive info structure was not removed.
20522 * Arbitrarily release device info structure.
20523 */
20524 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20525 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
20526 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
20527 (void) kmem_free((void *)sdinfo,
20528 sizeof (sata_drive_info_t));
20529 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20530 "Arbitrarily detaching old device info.", NULL);
20531 }
20532 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
20533
20534 /* For sanity, re-probe the port */
20535 sata_device.satadev_rev = SATA_DEVICE_REV;
20536 sata_device.satadev_addr = *saddr;
20537
20538 /*
20539 * We have to exit mutex, because the HBA probe port function may
20540 * block on its own mutex.
20541 */
20542 mutex_exit(&pmportinfo->pmport_mutex);
20543 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
20544 (SATA_DIP(sata_hba_inst), &sata_device);
20545 mutex_enter(&pmportinfo->pmport_mutex);
20546
20547 sata_update_pmport_info(sata_hba_inst, &sata_device);
20548 if (rval != SATA_SUCCESS) {
20549 /* Something went wrong? Fail the port */
20550 pmportinfo->pmport_state = SATA_PSTATE_FAILED;
20551 pmportinfo->pmport_dev_attach_time = 0;
20552 mutex_exit(&pmportinfo->pmport_mutex);
20553 SATA_LOG_D((sata_hba_inst, CE_WARN,
20554 "SATA port %d:%d probing failed", cport, pmport));
20555 return;
20556 } else {
20557 /* pmport probed successfully */
20558 pmportinfo->pmport_state |=
20559 SATA_STATE_PROBED | SATA_STATE_READY;
20560 }
20561 /*
20562 * Check if a device is still attached. For sanity, check also
20563 * link status - if no link, there is no device.
20564 */
20565 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
20566 SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
20567 SATA_DTYPE_NONE) {
20568 /*
20569 * No device - ignore attach event.
20570 */
20571 pmportinfo->pmport_dev_attach_time = 0;
20572 mutex_exit(&pmportinfo->pmport_mutex);
20573 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20574 "Ignoring attach - no device connected to port %d:%d",
20575 cport, pmport);
20576 return;
20577 }
20578
20579 mutex_exit(&pmportinfo->pmport_mutex);
20580 /*
20581 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
20582 * with the hint: SE_HINT_INSERT
20583 */
20584 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
20585
20586 /*
20587 * Port reprobing will take care of the creation of the device
20588 * info structure and determination of the device type.
20589 */
20590 sata_device.satadev_addr = *saddr;
20591 (void) sata_reprobe_port(sata_hba_inst, &sata_device,
20592 SATA_DEV_IDENTIFY_NORETRY);
20593
20594 mutex_enter(&pmportinfo->pmport_mutex);
20595 if ((pmportinfo->pmport_state & SATA_STATE_READY) &&
20596 (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE)) {
20597 /* Some device is attached to the port */
20598 if (pmportinfo->pmport_dev_type == SATA_DTYPE_UNKNOWN) {
20599 /*
20600 * A device was not successfully attached.
20601 * Track retry time for device identification.
20602 */
20603 if (pmportinfo->pmport_dev_attach_time != 0) {
20604 clock_t cur_time = ddi_get_lbolt();
20605 /*
20606 * If the retry time limit was not exceeded,
20607 * reinstate attach event.
20608 */
20609 if ((cur_time -
20610 pmportinfo->pmport_dev_attach_time) <
20611 drv_usectohz(
20612 SATA_DEV_IDENTIFY_TIMEOUT)) {
20613 /* OK, restore attach event */
20614 pmportinfo->pmport_event_flags |=
20615 SATA_EVNT_DEVICE_ATTACHED;
20616 } else {
20617 /* Timeout - cannot identify device */
20618 pmportinfo->pmport_dev_attach_time = 0;
20619 sata_log(sata_hba_inst, CE_WARN,
20620 "Could not identify SATA device "
20621 "at port %d:%d",
20622 cport, pmport);
20623 }
20624 } else {
20625 /*
20626 * Start tracking time for device
20627 * identification.
20628 * Save current time (lbolt value).
20629 */
20630 pmportinfo->pmport_dev_attach_time =
20631 ddi_get_lbolt();
20632 /* Restore attach event */
20633 pmportinfo->pmport_event_flags |=
20634 SATA_EVNT_DEVICE_ATTACHED;
20635 }
20636 } else {
20637 /*
20638 * If device was successfully attached, the subsequent
20639 * action depends on a state of the
20640 * sata_auto_online variable. If it is set to zero.
20641 * an explicit 'configure' command will be needed to
20642 * configure it. If its value is non-zero, we will
20643 * attempt to online (configure) the device.
20644 * First, log the message indicating that a device
20645 * was attached.
20646 */
20647 pmportinfo->pmport_dev_attach_time = 0;
20648 sata_log(sata_hba_inst, CE_WARN,
20649 "SATA device detected at port %d:%d",
20650 cport, pmport);
20651
20652 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20653 sata_drive_info_t new_sdinfo;
20654
20655 /* Log device info data */
20656 new_sdinfo = *(SATA_PMPORTINFO_DRV_INFO(
20657 pmportinfo));
20658 sata_show_drive_info(sata_hba_inst,
20659 &new_sdinfo);
20660 }
20661
20662 mutex_exit(&pmportinfo->pmport_mutex);
20663
20664 /*
20665 * Make sure that there is no target node for that
20666 * device. If so, release it. It should not happen,
20667 * unless we had problem removing the node when
20668 * device was detached.
20669 */
20670 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
20671 saddr->cport, saddr->pmport);
20672 mutex_enter(&pmportinfo->pmport_mutex);
20673 if (tdip != NULL) {
20674
20675 #ifdef SATA_DEBUG
20676 if ((pmportinfo->pmport_event_flags &
20677 SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
20678 sata_log(sata_hba_inst, CE_WARN,
20679 "sata_process_device_attached: "
20680 "old device target node exists!");
20681 #endif
20682 /*
20683 * target node exists - try to unconfigure
20684 * device and remove the node.
20685 */
20686 mutex_exit(&pmportinfo->pmport_mutex);
20687 rval = ndi_devi_offline(tdip,
20688 NDI_DEVI_REMOVE);
20689 mutex_enter(&pmportinfo->pmport_mutex);
20690
20691 if (rval == NDI_SUCCESS) {
20692 pmportinfo->pmport_event_flags &=
20693 ~SATA_EVNT_TARGET_NODE_CLEANUP;
20694 pmportinfo->pmport_tgtnode_clean =
20695 B_TRUE;
20696 } else {
20697 /*
20698 * PROBLEM - the target node remained
20699 * and it belongs to a previously
20700 * attached device.
20701 * This happens when the file was open
20702 * or the node was waiting for
20703 * resources at the time the
20704 * associated device was removed.
20705 * Instruct event daemon to retry the
20706 * cleanup later.
20707 */
20708 sata_log(sata_hba_inst,
20709 CE_WARN,
20710 "Application(s) accessing "
20711 "previously attached SATA "
20712 "device have to release "
20713 "it before newly inserted "
20714 "device can be made accessible."
20715 "at port %d:%d",
20716 cport, pmport);
20717 pmportinfo->pmport_event_flags |=
20718 SATA_EVNT_TARGET_NODE_CLEANUP;
20719 pmportinfo->pmport_tgtnode_clean =
20720 B_FALSE;
20721 }
20722 }
20723 if (sata_auto_online != 0) {
20724 pmportinfo->pmport_event_flags |=
20725 SATA_EVNT_AUTOONLINE_DEVICE;
20726 }
20727
20728 }
20729 } else {
20730 pmportinfo->pmport_dev_attach_time = 0;
20731 }
20732
20733 event_flags = pmportinfo->pmport_event_flags;
20734 mutex_exit(&pmportinfo->pmport_mutex);
20735 if (event_flags != 0) {
20736 mutex_enter(&sata_hba_inst->satahba_mutex);
20737 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20738 mutex_exit(&sata_hba_inst->satahba_mutex);
20739 mutex_enter(&sata_mutex);
20740 sata_event_pending |= SATA_EVNT_MAIN;
20741 mutex_exit(&sata_mutex);
20742 }
20743
20744 /* clear the reset_in_progress events */
20745 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20746 if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) {
20747 /* must clear flags on cport */
20748 sata_pmult_info_t *pminfo =
20749 SATA_PMULT_INFO(sata_hba_inst,
20750 saddr->cport);
20751 pminfo->pmult_event_flags |=
20752 SATA_EVNT_CLEAR_DEVICE_RESET;
20753 }
20754 }
20755 }
20756
20757 /*
20758 * Device Target Node Cleanup Event processing.
20759 * If the target node associated with a sata port device is in
20760 * DEVI_DEVICE_REMOVED state, an attempt is made to remove it.
20761 * If the target node cannot be removed, the event flag is left intact,
20762 * so that event daemon may re-run this function later.
20763 *
20764 * This function cannot be called in interrupt context (it may sleep).
20765 *
20766 * NOTE: Processes cport events only, not port multiplier ports.
20767 */
20768 static void
sata_process_target_node_cleanup(sata_hba_inst_t * sata_hba_inst,sata_address_t * saddr)20769 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
20770 sata_address_t *saddr)
20771 {
20772 sata_cport_info_t *cportinfo;
20773 dev_info_t *tdip;
20774
20775 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20776 "Processing port %d device target node cleanup", saddr->cport);
20777
20778 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
20779
20780 /*
20781 * Check if there is target node for that device and it is in the
20782 * DEVI_DEVICE_REMOVED state. If so, release it.
20783 */
20784 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport,
20785 saddr->pmport);
20786 if (tdip != NULL) {
20787 /*
20788 * target node exists - check if it is target node of
20789 * a removed device.
20790 */
20791 if (sata_check_device_removed(tdip) == B_TRUE) {
20792 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20793 "sata_process_target_node_cleanup: "
20794 "old device target node exists!", NULL);
20795 /*
20796 * Unconfigure and remove the target node
20797 */
20798 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) ==
20799 NDI_SUCCESS) {
20800 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20801 saddr->cport)->cport_mutex);
20802 cportinfo->cport_event_flags &=
20803 ~SATA_EVNT_TARGET_NODE_CLEANUP;
20804 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20805 saddr->cport)->cport_mutex);
20806 return;
20807 }
20808 /*
20809 * Event daemon will retry the cleanup later.
20810 */
20811 mutex_enter(&sata_hba_inst->satahba_mutex);
20812 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20813 mutex_exit(&sata_hba_inst->satahba_mutex);
20814 mutex_enter(&sata_mutex);
20815 sata_event_pending |= SATA_EVNT_MAIN;
20816 mutex_exit(&sata_mutex);
20817 }
20818 } else {
20819 if (saddr->qual == SATA_ADDR_CPORT ||
20820 saddr->qual == SATA_ADDR_DCPORT) {
20821 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20822 saddr->cport)->cport_mutex);
20823 cportinfo->cport_event_flags &=
20824 ~SATA_EVNT_TARGET_NODE_CLEANUP;
20825 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20826 saddr->cport)->cport_mutex);
20827 } else {
20828 /* sanity check */
20829 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) !=
20830 SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst,
20831 saddr->cport) == NULL)
20832 return;
20833 if (SATA_PMPORT_INFO(sata_hba_inst, saddr->cport,
20834 saddr->pmport) == NULL)
20835 return;
20836
20837 mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst,
20838 saddr->cport, saddr->pmport)->pmport_mutex);
20839 SATA_PMPORT_INFO(sata_hba_inst, saddr->cport,
20840 saddr->pmport)->pmport_event_flags &=
20841 ~SATA_EVNT_TARGET_NODE_CLEANUP;
20842 mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst,
20843 saddr->cport, saddr->pmport)->pmport_mutex);
20844 }
20845 }
20846 }
20847
20848 /*
20849 * Device AutoOnline Event processing.
20850 * If attached device is to be onlined, an attempt is made to online this
20851 * device, but only if there is no lingering (old) target node present.
20852 * If the device cannot be onlined, the event flag is left intact,
20853 * so that event daemon may re-run this function later.
20854 *
20855 * This function cannot be called in interrupt context (it may sleep).
20856 *
20857 * NOTE: Processes cport events only, not port multiplier ports.
20858 */
20859 static void
sata_process_device_autoonline(sata_hba_inst_t * sata_hba_inst,sata_address_t * saddr)20860 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst,
20861 sata_address_t *saddr)
20862 {
20863 sata_cport_info_t *cportinfo;
20864 sata_drive_info_t *sdinfo;
20865 sata_device_t sata_device;
20866 dev_info_t *tdip;
20867
20868 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20869 "Processing port %d attached device auto-onlining", saddr->cport);
20870
20871 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
20872
20873 /*
20874 * Check if device is present and recognized. If not, reset event.
20875 */
20876 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20877 if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
20878 /* Nothing to online */
20879 cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
20880 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20881 saddr->cport)->cport_mutex);
20882 return;
20883 }
20884 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20885
20886 /*
20887 * Check if there is target node for this device and if it is in the
20888 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep
20889 * the event for later processing.
20890 */
20891 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport,
20892 saddr->pmport);
20893 if (tdip != NULL) {
20894 /*
20895 * target node exists - check if it is target node of
20896 * a removed device.
20897 */
20898 if (sata_check_device_removed(tdip) == B_TRUE) {
20899 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20900 "sata_process_device_autoonline: "
20901 "old device target node exists!", NULL);
20902 /*
20903 * Event daemon will retry device onlining later.
20904 */
20905 mutex_enter(&sata_hba_inst->satahba_mutex);
20906 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20907 mutex_exit(&sata_hba_inst->satahba_mutex);
20908 mutex_enter(&sata_mutex);
20909 sata_event_pending |= SATA_EVNT_MAIN;
20910 mutex_exit(&sata_mutex);
20911 return;
20912 }
20913 /*
20914 * If the target node is not in the 'removed" state, assume
20915 * that it belongs to this device. There is nothing more to do,
20916 * but reset the event.
20917 */
20918 } else {
20919
20920 /*
20921 * Try to online the device
20922 * If there is any reset-related event, remove it. We are
20923 * configuring the device and no state restoring is needed.
20924 */
20925 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20926 saddr->cport)->cport_mutex);
20927 sata_device.satadev_addr = *saddr;
20928 if (saddr->qual == SATA_ADDR_CPORT)
20929 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
20930 else
20931 sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
20932 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
20933 if (sdinfo != NULL) {
20934 if (sdinfo->satadrv_event_flags &
20935 (SATA_EVNT_DEVICE_RESET |
20936 SATA_EVNT_INPROC_DEVICE_RESET))
20937 sdinfo->satadrv_event_flags = 0;
20938 sdinfo->satadrv_event_flags |=
20939 SATA_EVNT_CLEAR_DEVICE_RESET;
20940
20941 /* Need to create a new target node. */
20942 cportinfo->cport_tgtnode_clean = B_TRUE;
20943 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20944 saddr->cport)->cport_mutex);
20945 tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
20946 sata_hba_inst, &sata_device.satadev_addr);
20947 if (tdip == NULL) {
20948 /*
20949 * Configure (onlining) failed.
20950 * We will NOT retry
20951 */
20952 SATA_LOG_D((sata_hba_inst, CE_WARN,
20953 "sata_process_device_autoonline: "
20954 "configuring SATA device at port %d failed",
20955 saddr->cport));
20956 }
20957 } else {
20958 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20959 saddr->cport)->cport_mutex);
20960 }
20961
20962 }
20963 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20964 cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
20965 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20966 saddr->cport)->cport_mutex);
20967 }
20968
20969
20970 static void
sata_gen_sysevent(sata_hba_inst_t * sata_hba_inst,sata_address_t * saddr,int hint)20971 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr,
20972 int hint)
20973 {
20974 char ap[MAXPATHLEN];
20975 nvlist_t *ev_attr_list = NULL;
20976 int err;
20977
20978 /* Allocate and build sysevent attribute list */
20979 err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP);
20980 if (err != 0) {
20981 SATA_LOG_D((sata_hba_inst, CE_WARN,
20982 "sata_gen_sysevent: "
20983 "cannot allocate memory for sysevent attributes\n"));
20984 return;
20985 }
20986 /* Add hint attribute */
20987 err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint));
20988 if (err != 0) {
20989 SATA_LOG_D((sata_hba_inst, CE_WARN,
20990 "sata_gen_sysevent: "
20991 "failed to add DR_HINT attr for sysevent"));
20992 nvlist_free(ev_attr_list);
20993 return;
20994 }
20995 /*
20996 * Add AP attribute.
20997 * Get controller pathname and convert it into AP pathname by adding
20998 * a target number.
20999 */
21000 (void) snprintf(ap, MAXPATHLEN, "/devices");
21001 (void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap));
21002 (void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d",
21003 SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual));
21004
21005 err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap);
21006 if (err != 0) {
21007 SATA_LOG_D((sata_hba_inst, CE_WARN,
21008 "sata_gen_sysevent: "
21009 "failed to add DR_AP_ID attr for sysevent"));
21010 nvlist_free(ev_attr_list);
21011 return;
21012 }
21013
21014 /* Generate/log sysevent */
21015 err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR,
21016 ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP);
21017 if (err != DDI_SUCCESS) {
21018 SATA_LOG_D((sata_hba_inst, CE_WARN,
21019 "sata_gen_sysevent: "
21020 "cannot log sysevent, err code %x\n", err));
21021 }
21022
21023 nvlist_free(ev_attr_list);
21024 }
21025
21026
21027
21028
21029 /*
21030 * Set DEVI_DEVICE_REMOVED state in the SATA device target node.
21031 */
21032 static void
sata_set_device_removed(dev_info_t * tdip)21033 sata_set_device_removed(dev_info_t *tdip)
21034 {
21035 ASSERT(tdip != NULL);
21036
21037 ndi_devi_enter(tdip);
21038 mutex_enter(&DEVI(tdip)->devi_lock);
21039 DEVI_SET_DEVICE_REMOVED(tdip);
21040 mutex_exit(&DEVI(tdip)->devi_lock);
21041 ndi_devi_exit(tdip);
21042 }
21043
21044
21045 /*
21046 * Set internal event instructing event daemon to try
21047 * to perform the target node cleanup.
21048 */
21049 static void
sata_set_target_node_cleanup(sata_hba_inst_t * sata_hba_inst,sata_address_t * saddr)21050 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
21051 sata_address_t *saddr)
21052 {
21053 if (saddr->qual == SATA_ADDR_CPORT ||
21054 saddr->qual == SATA_ADDR_DCPORT) {
21055 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
21056 saddr->cport)->cport_mutex);
21057 SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |=
21058 SATA_EVNT_TARGET_NODE_CLEANUP;
21059 SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
21060 cport_tgtnode_clean = B_FALSE;
21061 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
21062 saddr->cport)->cport_mutex);
21063 } else {
21064 mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst,
21065 saddr->cport, saddr->pmport)->pmport_mutex);
21066 SATA_PMPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport,
21067 saddr->pmport) |= SATA_EVNT_TARGET_NODE_CLEANUP;
21068 SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, saddr->pmport)->
21069 pmport_tgtnode_clean = B_FALSE;
21070 mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst,
21071 saddr->cport, saddr->pmport)->pmport_mutex);
21072 }
21073 mutex_enter(&sata_hba_inst->satahba_mutex);
21074 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
21075 mutex_exit(&sata_hba_inst->satahba_mutex);
21076 mutex_enter(&sata_mutex);
21077 sata_event_pending |= SATA_EVNT_MAIN;
21078 mutex_exit(&sata_mutex);
21079 }
21080
21081
21082 /*
21083 * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state,
21084 * i.e. check if the target node state indicates that it belongs to a removed
21085 * device.
21086 *
21087 * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state,
21088 * B_FALSE otherwise.
21089 */
21090 static boolean_t
sata_check_device_removed(dev_info_t * tdip)21091 sata_check_device_removed(dev_info_t *tdip)
21092 {
21093 ASSERT(tdip != NULL);
21094
21095 if (DEVI_IS_DEVICE_REMOVED(tdip))
21096 return (B_TRUE);
21097 else
21098 return (B_FALSE);
21099 }
21100
21101
21102 /*
21103 * Check for DMA error. Return B_TRUE if error, B_FALSE otherwise.
21104 */
21105 static boolean_t
sata_check_for_dma_error(dev_info_t * dip,sata_pkt_txlate_t * spx)21106 sata_check_for_dma_error(dev_info_t *dip, sata_pkt_txlate_t *spx)
21107 {
21108 int fm_capability = ddi_fm_capable(dip);
21109 ddi_fm_error_t de;
21110
21111 if (fm_capability & DDI_FM_DMACHK_CAPABLE) {
21112 if (spx->txlt_buf_dma_handle != NULL) {
21113 ddi_fm_dma_err_get(spx->txlt_buf_dma_handle, &de,
21114 DDI_FME_VERSION);
21115 if (de.fme_status != DDI_SUCCESS)
21116 return (B_TRUE);
21117 }
21118 }
21119 return (B_FALSE);
21120 }
21121
21122
21123 /* ************************ FAULT INJECTTION **************************** */
21124
21125 #ifdef SATA_INJECT_FAULTS
21126
21127 static uint32_t sata_fault_count = 0;
21128 static uint32_t sata_fault_suspend_count = 0;
21129
21130 /*
21131 * Inject sata pkt fault
21132 * It modifies returned values of the sata packet.
21133 * It returns immediately if:
21134 * pkt fault injection is not enabled (via sata_inject_fault,
21135 * sata_inject_fault_count), or invalid fault is specified (sata_fault_type),
21136 * or pkt does not contain command to be faulted (set in sata_fault_cmd), or
21137 * pkt is not directed to specified fault controller/device
21138 * (sata_fault_ctrl_dev and sata_fault_device).
21139 * If fault controller is not specified, fault injection applies to all
21140 * controllers and devices.
21141 *
21142 * First argument is the pointer to the executed sata packet.
21143 * Second argument is a pointer to a value returned by the HBA tran_start
21144 * function.
21145 * Third argument specifies injected error. Injected sata packet faults
21146 * are the satapkt_reason values.
21147 * SATA_PKT_BUSY -1 Not completed, busy
21148 * SATA_PKT_DEV_ERROR 1 Device reported error
21149 * SATA_PKT_QUEUE_FULL 2 Not accepted, queue full
21150 * SATA_PKT_PORT_ERROR 3 Not completed, port error
21151 * SATA_PKT_CMD_UNSUPPORTED 4 Cmd unsupported
21152 * SATA_PKT_ABORTED 5 Aborted by request
21153 * SATA_PKT_TIMEOUT 6 Operation timeut
21154 * SATA_PKT_RESET 7 Aborted by reset request
21155 *
21156 * Additional global variables affecting the execution:
21157 *
21158 * sata_inject_fault_count variable specifies number of times in row the
21159 * error is injected. Value of -1 specifies permanent fault, ie. every time
21160 * the fault injection point is reached, the fault is injected and a pause
21161 * between fault injection specified by sata_inject_fault_pause_count is
21162 * ignored). Fault injection routine decrements sata_inject_fault_count
21163 * (if greater than zero) until it reaches 0. No fault is injected when
21164 * sata_inject_fault_count is 0 (zero).
21165 *
21166 * sata_inject_fault_pause_count variable specifies number of times a fault
21167 * injection is bypassed (pause between fault injections).
21168 * If set to 0, a fault is injected only a number of times specified by
21169 * sata_inject_fault_count.
21170 *
21171 * The fault counts are static, so for periodic errors they have to be manually
21172 * reset to start repetition sequence from scratch.
21173 * If the original value returned by the HBA tran_start function is not
21174 * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error
21175 * is injected (to avoid masking real problems);
21176 *
21177 * NOTE: In its current incarnation, this function should be invoked only for
21178 * commands executed in SYNCHRONOUS mode.
21179 */
21180
21181
21182 static void
sata_inject_pkt_fault(sata_pkt_t * spkt,int * rval,int fault)21183 sata_inject_pkt_fault(sata_pkt_t *spkt, int *rval, int fault)
21184 {
21185
21186 if (sata_inject_fault != SATA_INJECT_PKT_FAULT)
21187 return;
21188
21189 if (sata_inject_fault_count == 0)
21190 return;
21191
21192 if (fault == 0)
21193 return;
21194
21195 if (sata_fault_cmd != spkt->satapkt_cmd.satacmd_cmd_reg)
21196 return;
21197
21198 if (sata_fault_ctrl != NULL) {
21199 sata_pkt_txlate_t *spx =
21200 (sata_pkt_txlate_t *)spkt->satapkt_framework_private;
21201
21202 if (sata_fault_ctrl != NULL && sata_fault_ctrl !=
21203 spx->txlt_sata_hba_inst->satahba_dip)
21204 return;
21205
21206 if (sata_fault_device.satadev_addr.cport !=
21207 spkt->satapkt_device.satadev_addr.cport ||
21208 sata_fault_device.satadev_addr.pmport !=
21209 spkt->satapkt_device.satadev_addr.pmport ||
21210 sata_fault_device.satadev_addr.qual !=
21211 spkt->satapkt_device.satadev_addr.qual)
21212 return;
21213 }
21214
21215 /* Modify pkt return parameters */
21216 if (*rval != SATA_TRAN_ACCEPTED ||
21217 spkt->satapkt_reason != SATA_PKT_COMPLETED) {
21218 sata_fault_count = 0;
21219 sata_fault_suspend_count = 0;
21220 return;
21221 }
21222 if (sata_fault_count == 0 && sata_fault_suspend_count != 0) {
21223 /* Pause in the injection */
21224 sata_fault_suspend_count -= 1;
21225 return;
21226 }
21227
21228 if (sata_fault_count == 0 && sata_fault_suspend_count == 0) {
21229 /*
21230 * Init inject fault cycle. If fault count is set to -1,
21231 * it is a permanent fault.
21232 */
21233 if (sata_inject_fault_count != -1) {
21234 sata_fault_count = sata_inject_fault_count;
21235 sata_fault_suspend_count =
21236 sata_inject_fault_pause_count;
21237 if (sata_fault_suspend_count == 0)
21238 sata_inject_fault_count = 0;
21239 }
21240 }
21241
21242 if (sata_fault_count != 0)
21243 sata_fault_count -= 1;
21244
21245 switch (fault) {
21246 case SATA_PKT_BUSY:
21247 *rval = SATA_TRAN_BUSY;
21248 spkt->satapkt_reason = SATA_PKT_BUSY;
21249 break;
21250
21251 case SATA_PKT_QUEUE_FULL:
21252 *rval = SATA_TRAN_QUEUE_FULL;
21253 spkt->satapkt_reason = SATA_PKT_QUEUE_FULL;
21254 break;
21255
21256 case SATA_PKT_CMD_UNSUPPORTED:
21257 *rval = SATA_TRAN_CMD_UNSUPPORTED;
21258 spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED;
21259 break;
21260
21261 case SATA_PKT_PORT_ERROR:
21262 /* This is "rejected" command */
21263 *rval = SATA_TRAN_PORT_ERROR;
21264 spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
21265 /* Additional error setup could be done here - port state */
21266 break;
21267
21268 case SATA_PKT_DEV_ERROR:
21269 spkt->satapkt_reason = SATA_PKT_DEV_ERROR;
21270 /*
21271 * Additional error setup could be done here
21272 */
21273 break;
21274
21275 case SATA_PKT_ABORTED:
21276 spkt->satapkt_reason = SATA_PKT_ABORTED;
21277 break;
21278
21279 case SATA_PKT_TIMEOUT:
21280 spkt->satapkt_reason = SATA_PKT_TIMEOUT;
21281 /* Additional error setup could be done here */
21282 break;
21283
21284 case SATA_PKT_RESET:
21285 spkt->satapkt_reason = SATA_PKT_RESET;
21286 /*
21287 * Additional error setup could be done here - device reset
21288 */
21289 break;
21290
21291 default:
21292 break;
21293 }
21294 }
21295
21296 #endif
21297
21298 /*
21299 * SATA Trace Ring Buffer
21300 * ----------------------
21301 *
21302 * Overview
21303 *
21304 * The SATA trace ring buffer is a ring buffer created and managed by
21305 * the SATA framework module that can be used by any module or driver
21306 * within the SATA framework to store debug messages.
21307 *
21308 * Ring Buffer Interfaces:
21309 *
21310 * sata_vtrace_debug() <-- Adds debug message to ring buffer
21311 * sata_trace_debug() <-- Wraps varargs into sata_vtrace_debug()
21312 *
21313 * Note that the sata_trace_debug() interface was created to give
21314 * consumers the flexibilty of sending debug messages to ring buffer
21315 * as variable arguments. Consumers can send type va_list debug
21316 * messages directly to sata_vtrace_debug(). The sata_trace_debug()
21317 * and sata_vtrace_debug() relationship is similar to that of
21318 * cmn_err(9F) and vcmn_err(9F).
21319 *
21320 * Below is a diagram of the SATA trace ring buffer interfaces and
21321 * sample consumers:
21322 *
21323 * +---------------------------------+
21324 * | o o SATA Framework Module |
21325 * | o SATA o +------------------+ +------------------+
21326 * |o Trace o <--|sata_vtrace_debug/|<-----|SATA HBA Driver #1|
21327 * |o R-Buf o |sata_trace_debug |<--+ +------------------+
21328 * | o o +------------------+ | +------------------+
21329 * | o o ^ | +--|SATA HBA Driver #2|
21330 * | | | +------------------+
21331 * | +------------------+ |
21332 * | |SATA Debug Message| |
21333 * | +------------------+ |
21334 * +---------------------------------+
21335 *
21336 * Supporting Routines:
21337 *
21338 * sata_trace_rbuf_alloc() <-- Initializes ring buffer
21339 * sata_trace_rbuf_free() <-- Destroys ring buffer
21340 * sata_trace_dmsg_alloc() <-- Creates or reuses buffer in ring buffer
21341 * sata_trace_dmsg_free() <-- Destroys content of ring buffer
21342 *
21343 * The default SATA trace ring buffer size is defined by DMSG_RING_SIZE.
21344 * The ring buffer size can be adjusted by setting dmsg_ring_size in
21345 * /etc/system to desired size in unit of bytes.
21346 *
21347 * The individual debug message size in the ring buffer is restricted
21348 * to DMSG_BUF_SIZE.
21349 */
21350 void
sata_vtrace_debug(dev_info_t * dip,const char * fmt,va_list ap)21351 sata_vtrace_debug(dev_info_t *dip, const char *fmt, va_list ap)
21352 {
21353 sata_trace_dmsg_t *dmsg;
21354
21355 if (sata_debug_rbuf == NULL) {
21356 return;
21357 }
21358
21359 /*
21360 * If max size of ring buffer is smaller than size
21361 * required for one debug message then just return
21362 * since we have no room for the debug message.
21363 */
21364 if (sata_debug_rbuf->maxsize < (sizeof (sata_trace_dmsg_t))) {
21365 return;
21366 }
21367
21368 mutex_enter(&sata_debug_rbuf->lock);
21369
21370 /* alloc or reuse on ring buffer */
21371 dmsg = sata_trace_dmsg_alloc();
21372
21373 if (dmsg == NULL) {
21374 /* resource allocation failed */
21375 mutex_exit(&sata_debug_rbuf->lock);
21376 return;
21377 }
21378
21379 dmsg->dip = dip;
21380 gethrestime(&dmsg->timestamp);
21381
21382 (void) vsnprintf(dmsg->buf, sizeof (dmsg->buf), fmt, ap);
21383
21384 mutex_exit(&sata_debug_rbuf->lock);
21385 }
21386
21387 void
sata_trace_debug(dev_info_t * dip,const char * fmt,...)21388 sata_trace_debug(dev_info_t *dip, const char *fmt, ...)
21389 {
21390 va_list ap;
21391
21392 va_start(ap, fmt);
21393 sata_vtrace_debug(dip, fmt, ap);
21394 va_end(ap);
21395 }
21396
21397 /*
21398 * This routine is used to manage debug messages
21399 * on ring buffer.
21400 */
21401 static sata_trace_dmsg_t *
sata_trace_dmsg_alloc(void)21402 sata_trace_dmsg_alloc(void)
21403 {
21404 sata_trace_dmsg_t *dmsg_alloc, *dmsg = sata_debug_rbuf->dmsgp;
21405
21406 if (sata_debug_rbuf->looped == TRUE) {
21407 sata_debug_rbuf->dmsgp = dmsg->next;
21408 return (sata_debug_rbuf->dmsgp);
21409 }
21410
21411 /*
21412 * If we're looping for the first time,
21413 * connect the ring.
21414 */
21415 if (((sata_debug_rbuf->size + (sizeof (sata_trace_dmsg_t))) >
21416 sata_debug_rbuf->maxsize) && (sata_debug_rbuf->dmsgh != NULL)) {
21417 dmsg->next = sata_debug_rbuf->dmsgh;
21418 sata_debug_rbuf->dmsgp = sata_debug_rbuf->dmsgh;
21419 sata_debug_rbuf->looped = TRUE;
21420 return (sata_debug_rbuf->dmsgp);
21421 }
21422
21423 /* If we've gotten this far then memory allocation is needed */
21424 dmsg_alloc = kmem_zalloc(sizeof (sata_trace_dmsg_t), KM_NOSLEEP);
21425 if (dmsg_alloc == NULL) {
21426 sata_debug_rbuf->allocfailed++;
21427 return (dmsg_alloc);
21428 } else {
21429 sata_debug_rbuf->size += sizeof (sata_trace_dmsg_t);
21430 }
21431
21432 if (sata_debug_rbuf->dmsgp != NULL) {
21433 dmsg->next = dmsg_alloc;
21434 sata_debug_rbuf->dmsgp = dmsg->next;
21435 return (sata_debug_rbuf->dmsgp);
21436 } else {
21437 /*
21438 * We should only be here if we're initializing
21439 * the ring buffer.
21440 */
21441 if (sata_debug_rbuf->dmsgh == NULL) {
21442 sata_debug_rbuf->dmsgh = dmsg_alloc;
21443 } else {
21444 /* Something is wrong */
21445 kmem_free(dmsg_alloc, sizeof (sata_trace_dmsg_t));
21446 return (NULL);
21447 }
21448
21449 sata_debug_rbuf->dmsgp = dmsg_alloc;
21450 return (sata_debug_rbuf->dmsgp);
21451 }
21452 }
21453
21454
21455 /*
21456 * Free all messages on debug ring buffer.
21457 */
21458 static void
sata_trace_dmsg_free(void)21459 sata_trace_dmsg_free(void)
21460 {
21461 sata_trace_dmsg_t *dmsg_next, *dmsg = sata_debug_rbuf->dmsgh;
21462
21463 while (dmsg != NULL) {
21464 dmsg_next = dmsg->next;
21465 kmem_free(dmsg, sizeof (sata_trace_dmsg_t));
21466
21467 /*
21468 * If we've looped around the ring than we're done.
21469 */
21470 if (dmsg_next == sata_debug_rbuf->dmsgh) {
21471 break;
21472 } else {
21473 dmsg = dmsg_next;
21474 }
21475 }
21476 }
21477
21478
21479 /*
21480 * This function can block
21481 */
21482 static void
sata_trace_rbuf_alloc(void)21483 sata_trace_rbuf_alloc(void)
21484 {
21485 sata_debug_rbuf = kmem_zalloc(sizeof (sata_trace_rbuf_t), KM_SLEEP);
21486
21487 mutex_init(&sata_debug_rbuf->lock, NULL, MUTEX_DRIVER, NULL);
21488
21489 if (dmsg_ring_size > 0) {
21490 sata_debug_rbuf->maxsize = (size_t)dmsg_ring_size;
21491 }
21492 }
21493
21494
21495 static void
sata_trace_rbuf_free(void)21496 sata_trace_rbuf_free(void)
21497 {
21498 sata_trace_dmsg_free();
21499 mutex_destroy(&sata_debug_rbuf->lock);
21500 kmem_free(sata_debug_rbuf, sizeof (sata_trace_rbuf_t));
21501 }
21502
21503 #ifndef SATA_DEBUG
21504 /*
21505 * If SATA_DEBUG is not defined then this routine is called instead
21506 * of sata_log() via the SATA_LOG_D macro.
21507 */
21508 static void
sata_trace_log(sata_hba_inst_t * sata_hba_inst,uint_t level __unused,const char * fmt,...)21509 sata_trace_log(sata_hba_inst_t *sata_hba_inst, uint_t level __unused,
21510 const char *fmt, ...)
21511 {
21512 dev_info_t *dip = NULL;
21513 va_list ap;
21514
21515 if (sata_hba_inst != NULL) {
21516 dip = SATA_DIP(sata_hba_inst);
21517 }
21518
21519 va_start(ap, fmt);
21520 sata_vtrace_debug(dip, fmt, ap);
21521 va_end(ap);
21522 }
21523
21524 #endif /* SATA_DEBUG */
21525