Print this page
7127 remove -Wno-missing-braces from Makefile.uts
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/io/sata/impl/sata.c
+++ new/usr/src/uts/common/io/sata/impl/sata.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 */
25 25 /*
26 26 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
27 27 */
28 28
29 29 /*
30 30 * SATA Framework
31 31 * Generic SATA Host Adapter Implementation
32 32 */
33 33
34 34 #include <sys/conf.h>
35 35 #include <sys/file.h>
36 36 #include <sys/ddi.h>
37 37 #include <sys/sunddi.h>
38 38 #include <sys/modctl.h>
39 39 #include <sys/cmn_err.h>
40 40 #include <sys/errno.h>
41 41 #include <sys/thread.h>
42 42 #include <sys/kstat.h>
43 43 #include <sys/note.h>
44 44 #include <sys/sysevent.h>
45 45 #include <sys/sysevent/eventdefs.h>
46 46 #include <sys/sysevent/dr.h>
47 47 #include <sys/taskq.h>
48 48 #include <sys/disp.h>
49 49 #include <sys/sdt.h>
50 50
51 51 #include <sys/sata/impl/sata.h>
52 52 #include <sys/sata/sata_hba.h>
53 53 #include <sys/sata/sata_defs.h>
54 54 #include <sys/sata/sata_cfgadm.h>
55 55 #include <sys/sata/sata_blacklist.h>
56 56 #include <sys/sata/sata_satl.h>
57 57
58 58 #include <sys/scsi/impl/spc3_types.h>
59 59
60 60 /*
61 61 * FMA header files
62 62 */
63 63 #include <sys/ddifm.h>
64 64 #include <sys/fm/protocol.h>
65 65 #include <sys/fm/util.h>
66 66 #include <sys/fm/io/ddi.h>
67 67
68 68 /* Debug flags - defined in sata.h */
69 69 int sata_debug_flags = 0;
70 70 int sata_msg = 0;
71 71
72 72 /*
73 73 * Flags enabling selected SATA HBA framework functionality
74 74 */
75 75 #define SATA_ENABLE_QUEUING 1
76 76 #define SATA_ENABLE_NCQ 2
77 77 #define SATA_ENABLE_PROCESS_EVENTS 4
78 78 #define SATA_ENABLE_PMULT_FBS 8 /* FIS-Based Switching */
79 79 int sata_func_enable =
80 80 SATA_ENABLE_PROCESS_EVENTS | SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ;
81 81
82 82 /*
83 83 * Global variable setting default maximum queue depth (NCQ or TCQ)
84 84 * Note:minimum queue depth is 1
85 85 */
86 86 int sata_max_queue_depth = SATA_MAX_QUEUE_DEPTH; /* max NCQ/TCQ queue depth */
87 87
88 88 /*
89 89 * Currently used default NCQ/TCQ queue depth. It is set-up during the driver
90 90 * initialization, using value from sata_max_queue_depth
91 91 * It is adjusted to minimum supported by the controller and by the device,
92 92 * if queueing is enabled.
93 93 */
94 94 static int sata_current_max_qdepth;
95 95
96 96 /*
97 97 * Global variable determining the default behavior after device hotpluggin.
98 98 * If non-zero, the hotplugged device is onlined (if possible) without explicit
99 99 * IOCTL request (AP_CONFIGURE).
100 100 * If zero, hotplugged device is identified, but not onlined.
101 101 * Enabling (AP_CONNECT) device port with an attached device does not result
102 102 * in device onlining regardless of the flag setting
103 103 */
104 104 int sata_auto_online = 0;
105 105
106 106 #ifdef SATA_DEBUG
107 107
108 108 #define SATA_LOG_D(args) sata_log args
109 109 uint64_t mbuf_count = 0;
110 110 uint64_t mbuffail_count = 0;
111 111
112 112 sata_atapi_cmd_t sata_atapi_trace[64];
113 113 uint32_t sata_atapi_trace_index = 0;
114 114 int sata_atapi_trace_save = 1;
115 115 static void sata_save_atapi_trace(sata_pkt_txlate_t *, int);
116 116 #define SATAATAPITRACE(spx, count) if (sata_atapi_trace_save) \
117 117 sata_save_atapi_trace(spx, count);
118 118
119 119 #else
120 120 #define SATA_LOG_D(args) sata_trace_log args
121 121 #define SATAATAPITRACE(spx, count)
122 122 #endif
123 123
124 124 #if 0
125 125 static void
126 126 sata_test_atapi_packet_command(sata_hba_inst_t *, int);
127 127 #endif
128 128
129 129 #ifdef SATA_INJECT_FAULTS
130 130
131 131 #define SATA_INJECT_PKT_FAULT 1
132 132 uint32_t sata_inject_fault = 0;
133 133
134 134 uint32_t sata_inject_fault_count = 0;
135 135 uint32_t sata_inject_fault_pause_count = 0;
136 136 uint32_t sata_fault_type = 0;
137 137 uint32_t sata_fault_cmd = 0;
138 138 dev_info_t *sata_fault_ctrl = NULL;
139 139 sata_device_t sata_fault_device;
140 140
141 141 static void sata_inject_pkt_fault(sata_pkt_t *, int *, int);
142 142
143 143 #endif
144 144
145 145 #define LEGACY_HWID_LEN 64 /* Model (40) + Serial (20) + pad */
146 146
147 147 static char sata_rev_tag[] = {"1.46"};
148 148
149 149 /*
150 150 * SATA cb_ops functions
151 151 */
152 152 static int sata_hba_open(dev_t *, int, int, cred_t *);
153 153 static int sata_hba_close(dev_t, int, int, cred_t *);
154 154 static int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
155 155
156 156 /*
157 157 * SCSA required entry points
158 158 */
159 159 static int sata_scsi_tgt_init(dev_info_t *, dev_info_t *,
160 160 scsi_hba_tran_t *, struct scsi_device *);
161 161 static int sata_scsi_tgt_probe(struct scsi_device *,
162 162 int (*callback)(void));
163 163 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *,
164 164 scsi_hba_tran_t *, struct scsi_device *);
165 165 static int sata_scsi_start(struct scsi_address *, struct scsi_pkt *);
166 166 static int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *);
167 167 static int sata_scsi_reset(struct scsi_address *, int);
168 168 static int sata_scsi_getcap(struct scsi_address *, char *, int);
169 169 static int sata_scsi_setcap(struct scsi_address *, char *, int, int);
170 170 static struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *,
171 171 struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t),
172 172 caddr_t);
173 173 static void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *);
174 174 static void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *);
175 175 static void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *);
176 176
177 177 /*
178 178 * SATA HBA interface functions are defined in sata_hba.h header file
179 179 */
180 180
181 181 /* Event processing functions */
182 182 static void sata_event_daemon(void *);
183 183 static void sata_event_thread_control(int);
184 184 static void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst);
185 185 static void sata_process_pmult_events(sata_hba_inst_t *, uint8_t);
186 186 static void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *);
187 187 static void sata_process_pmdevice_reset(sata_hba_inst_t *, sata_address_t *);
188 188 static void sata_process_port_failed_event(sata_hba_inst_t *,
189 189 sata_address_t *);
190 190 static void sata_process_port_link_events(sata_hba_inst_t *,
191 191 sata_address_t *);
192 192 static void sata_process_pmport_link_events(sata_hba_inst_t *,
193 193 sata_address_t *);
194 194 static void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *);
195 195 static void sata_process_pmdevice_detached(sata_hba_inst_t *,
196 196 sata_address_t *);
197 197 static void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *);
198 198 static void sata_process_pmdevice_attached(sata_hba_inst_t *,
199 199 sata_address_t *);
200 200 static void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *);
201 201 static void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *);
202 202 static void sata_process_target_node_cleanup(sata_hba_inst_t *,
203 203 sata_address_t *);
204 204 static void sata_process_device_autoonline(sata_hba_inst_t *,
205 205 sata_address_t *saddr);
206 206
207 207 /*
208 208 * Local translation functions
209 209 */
210 210 static int sata_txlt_inquiry(sata_pkt_txlate_t *);
211 211 static int sata_txlt_test_unit_ready(sata_pkt_txlate_t *);
212 212 static int sata_txlt_start_stop_unit(sata_pkt_txlate_t *);
213 213 static int sata_txlt_read_capacity(sata_pkt_txlate_t *);
214 214 static int sata_txlt_read_capacity16(sata_pkt_txlate_t *);
215 215 static int sata_txlt_unmap(sata_pkt_txlate_t *);
216 216 static int sata_txlt_request_sense(sata_pkt_txlate_t *);
217 217 static int sata_txlt_read(sata_pkt_txlate_t *);
218 218 static int sata_txlt_write(sata_pkt_txlate_t *);
219 219 static int sata_txlt_log_sense(sata_pkt_txlate_t *);
220 220 static int sata_txlt_log_select(sata_pkt_txlate_t *);
221 221 static int sata_txlt_mode_sense(sata_pkt_txlate_t *);
222 222 static int sata_txlt_mode_select(sata_pkt_txlate_t *);
223 223 static int sata_txlt_ata_pass_thru(sata_pkt_txlate_t *);
224 224 static int sata_txlt_synchronize_cache(sata_pkt_txlate_t *);
225 225 static int sata_txlt_write_buffer(sata_pkt_txlate_t *);
226 226 static int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *);
227 227
228 228 static int sata_hba_start(sata_pkt_txlate_t *, int *);
229 229 static int sata_txlt_invalid_command(sata_pkt_txlate_t *);
230 230 static int sata_txlt_check_condition(sata_pkt_txlate_t *, uchar_t, uchar_t);
231 231 static int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *);
232 232 static int sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *);
233 233 static int sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *);
234 234 static void sata_txlt_rw_completion(sata_pkt_t *);
235 235 static void sata_txlt_nodata_cmd_completion(sata_pkt_t *);
236 236 static void sata_txlt_apt_completion(sata_pkt_t *sata_pkt);
237 237 static void sata_txlt_unmap_completion(sata_pkt_t *sata_pkt);
238 238 static void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *);
239 239 static int sata_emul_rw_completion(sata_pkt_txlate_t *);
240 240 static void sata_fill_ata_return_desc(sata_pkt_t *, uint8_t, uint8_t,
241 241 uint8_t);
242 242 static struct scsi_extended_sense *sata_immediate_error_response(
243 243 sata_pkt_txlate_t *, int);
244 244 static struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *);
245 245
246 246 static int sata_txlt_atapi(sata_pkt_txlate_t *);
247 247 static void sata_txlt_atapi_completion(sata_pkt_t *);
248 248
249 249 /*
250 250 * Local functions for ioctl
251 251 */
252 252 static int32_t sata_get_port_num(sata_hba_inst_t *, struct devctl_iocdata *);
253 253 static void sata_cfgadm_state(sata_hba_inst_t *, int32_t,
254 254 devctl_ap_state_t *);
255 255 static dev_info_t *sata_get_target_dip(dev_info_t *, uint8_t, uint8_t);
256 256 static dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *);
257 257 static dev_info_t *sata_devt_to_devinfo(dev_t);
258 258 static int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *);
259 259 static int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *);
260 260 static int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *);
261 261 static int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *);
262 262 static int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *);
263 263 static int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *);
264 264 static int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *);
265 265 static int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *);
266 266 static int sata_ioctl_reset_all(sata_hba_inst_t *);
267 267 static int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *);
268 268 static int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *,
269 269 sata_ioctl_data_t *, int mode);
270 270 static int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *,
271 271 sata_ioctl_data_t *, int mode);
272 272 static int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *,
273 273 sata_ioctl_data_t *, int mode);
274 274 static int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *,
275 275 sata_ioctl_data_t *, int mode);
276 276 static int sata_ioctl_get_serialnumber_info(sata_hba_inst_t *,
277 277 sata_device_t *, sata_ioctl_data_t *, int mode);
278 278
279 279 /*
280 280 * Local functions
281 281 */
282 282 static void sata_remove_hba_instance(dev_info_t *);
283 283 static int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *);
284 284 static void sata_probe_ports(sata_hba_inst_t *);
285 285 static void sata_probe_pmports(sata_hba_inst_t *, uint8_t);
286 286 static int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int);
287 287 static int sata_reprobe_pmult(sata_hba_inst_t *, sata_device_t *, int);
288 288 static int sata_reprobe_pmport(sata_hba_inst_t *, sata_device_t *, int);
289 289 static int sata_alloc_pmult(sata_hba_inst_t *, sata_device_t *);
290 290 static void sata_free_pmult(sata_hba_inst_t *, sata_device_t *);
291 291 static int sata_add_device(dev_info_t *, sata_hba_inst_t *, sata_device_t *);
292 292 static int sata_offline_device(sata_hba_inst_t *, sata_device_t *,
293 293 sata_drive_info_t *);
294 294 static dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *,
295 295 sata_address_t *);
296 296 static void sata_remove_target_node(sata_hba_inst_t *,
297 297 sata_address_t *);
298 298 static int sata_validate_scsi_address(sata_hba_inst_t *,
299 299 struct scsi_address *, sata_device_t *);
300 300 static int sata_validate_sata_address(sata_hba_inst_t *, int, int, int);
301 301 static sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t));
302 302 static void sata_pkt_free(sata_pkt_txlate_t *);
303 303 static int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t),
304 304 caddr_t, ddi_dma_attr_t *);
305 305 static void sata_common_free_dma_rsrcs(sata_pkt_txlate_t *);
306 306 static int sata_probe_device(sata_hba_inst_t *, sata_device_t *);
307 307 static sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *,
308 308 sata_device_t *);
309 309 static int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *);
310 310 static void sata_reidentify_device(sata_pkt_txlate_t *);
311 311 static struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int);
312 312 static void sata_free_local_buffer(sata_pkt_txlate_t *);
313 313 static uint64_t sata_check_capacity(sata_drive_info_t *);
314 314 void sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *,
315 315 ddi_dma_attr_t *);
316 316 static int sata_fetch_device_identify_data(sata_hba_inst_t *,
317 317 sata_drive_info_t *);
318 318 static void sata_update_port_info(sata_hba_inst_t *, sata_device_t *);
319 319 static void sata_update_pmport_info(sata_hba_inst_t *, sata_device_t *);
320 320 static void sata_update_port_scr(sata_port_scr_t *, sata_device_t *);
321 321 static int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *);
322 322 static int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int);
323 323 static int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int);
324 324 static int sata_set_drive_features(sata_hba_inst_t *,
325 325 sata_drive_info_t *, int flag);
326 326 static void sata_init_write_cache_mode(sata_drive_info_t *sdinfo);
327 327 static int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *);
328 328 static void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *,
329 329 uint8_t *);
330 330 static int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *,
331 331 struct scsi_inquiry *);
332 332 static int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *);
333 333 static int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *);
334 334 static int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *);
335 335 static int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *);
336 336 static int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *);
337 337 static int sata_mode_select_page_8(sata_pkt_txlate_t *,
338 338 struct mode_cache_scsi3 *, int, int *, int *, int *);
339 339 static int sata_mode_select_page_1a(sata_pkt_txlate_t *,
340 340 struct mode_info_power_cond *, int, int *, int *, int *);
341 341 static int sata_mode_select_page_1c(sata_pkt_txlate_t *,
342 342 struct mode_info_excpt_page *, int, int *, int *, int *);
343 343 static int sata_mode_select_page_30(sata_pkt_txlate_t *,
344 344 struct mode_acoustic_management *, int, int *, int *, int *);
345 345
346 346 static int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *);
347 347 static int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *,
348 348 sata_hba_inst_t *);
349 349 static int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *,
350 350 sata_hba_inst_t *);
351 351 static int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *,
352 352 sata_hba_inst_t *);
353 353 static int sata_build_lsense_page_0e(sata_drive_info_t *, uint8_t *,
354 354 sata_pkt_txlate_t *);
355 355
356 356 static void sata_set_arq_data(sata_pkt_t *);
357 357 static void sata_build_read_verify_cmd(sata_cmd_t *, uint16_t, uint64_t);
358 358 static void sata_build_generic_cmd(sata_cmd_t *, uint8_t);
359 359 static uint8_t sata_get_standby_timer(uint8_t *timer);
360 360
361 361 static void sata_save_drive_settings(sata_drive_info_t *);
362 362 static void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *);
363 363 static void sata_show_pmult_info(sata_hba_inst_t *, sata_device_t *);
364 364 static void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...);
365 365 static void sata_trace_log(sata_hba_inst_t *, uint_t, const char *fmt, ...);
366 366 static int sata_fetch_smart_return_status(sata_hba_inst_t *,
367 367 sata_drive_info_t *);
368 368 static int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *,
369 369 struct smart_data *);
370 370 static int sata_smart_selftest_log(sata_hba_inst_t *,
371 371 sata_drive_info_t *,
372 372 struct smart_selftest_log *);
373 373 static int sata_ext_smart_selftest_read_log(sata_hba_inst_t *,
374 374 sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t);
375 375 static int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *,
376 376 uint8_t *, uint8_t, uint8_t);
377 377 static int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *,
378 378 struct read_log_ext_directory *);
379 379 static void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int);
380 380 static void sata_xlate_errors(sata_pkt_txlate_t *);
381 381 static void sata_decode_device_error(sata_pkt_txlate_t *,
382 382 struct scsi_extended_sense *);
383 383 static void sata_set_device_removed(dev_info_t *);
384 384 static boolean_t sata_check_device_removed(dev_info_t *);
385 385 static void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *);
386 386 static int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *,
387 387 sata_drive_info_t *);
388 388 static int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *,
389 389 sata_drive_info_t *);
390 390 static void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *);
391 391 static void sata_fixed_sense_data_preset(struct scsi_extended_sense *);
392 392 static void sata_target_devid_register(dev_info_t *, sata_drive_info_t *);
393 393 static int sata_check_modser(char *, int);
394 394
395 395 /*
396 396 * FMA
397 397 */
398 398 static boolean_t sata_check_for_dma_error(dev_info_t *, sata_pkt_txlate_t *);
399 399
400 400
401 401 /*
402 402 * SATA Framework will ignore SATA HBA driver cb_ops structure and
403 403 * register following one with SCSA framework.
404 404 * Open & close are provided, so scsi framework will not use its own
405 405 */
406 406 static struct cb_ops sata_cb_ops = {
407 407 sata_hba_open, /* open */
408 408 sata_hba_close, /* close */
409 409 nodev, /* strategy */
410 410 nodev, /* print */
411 411 nodev, /* dump */
412 412 nodev, /* read */
413 413 nodev, /* write */
414 414 sata_hba_ioctl, /* ioctl */
415 415 nodev, /* devmap */
416 416 nodev, /* mmap */
417 417 nodev, /* segmap */
418 418 nochpoll, /* chpoll */
419 419 ddi_prop_op, /* cb_prop_op */
420 420 0, /* streamtab */
421 421 D_NEW | D_MP, /* cb_flag */
422 422 CB_REV, /* rev */
423 423 nodev, /* aread */
424 424 nodev /* awrite */
425 425 };
426 426
427 427
428 428 extern struct mod_ops mod_miscops;
↓ open down ↓ |
428 lines elided |
↑ open up ↑ |
429 429 extern uchar_t scsi_cdb_size[];
430 430
431 431 static struct modlmisc modlmisc = {
432 432 &mod_miscops, /* Type of module */
433 433 "SATA Module" /* module name */
434 434 };
435 435
436 436
437 437 static struct modlinkage modlinkage = {
438 438 MODREV_1,
439 - (void *)&modlmisc,
440 - NULL
439 + { (void *)&modlmisc, NULL }
441 440 };
442 441
443 442 /*
444 443 * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero,
445 444 * i.e. when scsi_pkt has not timeout specified.
446 445 */
447 446 static int sata_default_pkt_time = 60; /* 60 seconds */
448 447
449 448 /*
450 449 * Intermediate buffer device access attributes - they are required,
451 450 * but not necessarily used.
452 451 */
453 452 static ddi_device_acc_attr_t sata_acc_attr = {
454 453 DDI_DEVICE_ATTR_V0,
455 454 DDI_STRUCTURE_LE_ACC,
456 455 DDI_STRICTORDER_ACC
457 456 };
458 457
459 458
460 459 /*
461 460 * Mutexes protecting structures in multithreaded operations.
462 461 * Because events are relatively rare, a single global mutex protecting
463 462 * data structures should be sufficient. To increase performance, add
464 463 * separate mutex per each sata port and use global mutex only to protect
465 464 * common data structures.
466 465 */
467 466 static kmutex_t sata_mutex; /* protects sata_hba_list */
468 467 static kmutex_t sata_log_mutex; /* protects log */
469 468
470 469 static char sata_log_buf[256];
471 470
472 471 /*
473 472 * sata trace debug
474 473 */
475 474 static sata_trace_rbuf_t *sata_debug_rbuf;
476 475 static sata_trace_dmsg_t *sata_trace_dmsg_alloc(void);
477 476 static void sata_trace_dmsg_free(void);
478 477 static void sata_trace_rbuf_alloc(void);
479 478 static void sata_trace_rbuf_free(void);
480 479
481 480 int dmsg_ring_size = DMSG_RING_SIZE;
482 481
483 482 /* Default write cache setting for SATA hard disks */
484 483 int sata_write_cache = 1; /* enabled */
485 484
486 485 /* Default write cache setting for SATA ATAPI CD/DVD */
487 486 int sata_atapicdvd_write_cache = 1; /* enabled */
488 487
489 488 /* Default write cache setting for SATA ATAPI tape */
490 489 int sata_atapitape_write_cache = 1; /* enabled */
491 490
492 491 /* Default write cache setting for SATA ATAPI disk */
493 492 int sata_atapidisk_write_cache = 1; /* enabled */
494 493
495 494 /*
496 495 * Linked list of HBA instances
497 496 */
498 497 static sata_hba_inst_t *sata_hba_list = NULL;
499 498 static sata_hba_inst_t *sata_hba_list_tail = NULL;
500 499 /*
501 500 * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran
502 501 * structure and in sata soft state.
503 502 */
504 503
505 504 /*
506 505 * Event daemon related variables
507 506 */
508 507 static kmutex_t sata_event_mutex;
509 508 static kcondvar_t sata_event_cv;
510 509 static kthread_t *sata_event_thread = NULL;
511 510 static int sata_event_thread_terminate = 0;
512 511 static int sata_event_pending = 0;
513 512 static int sata_event_thread_active = 0;
514 513 extern pri_t minclsyspri;
515 514
516 515 /*
517 516 * NCQ error recovery command
518 517 */
519 518 static const sata_cmd_t sata_rle_cmd = {
520 519 SATA_CMD_REV,
521 520 NULL,
522 521 {
523 522 SATA_DIR_READ
524 523 },
525 524 ATA_ADDR_LBA48,
526 525 0,
527 526 0,
528 527 0,
529 528 0,
530 529 0,
531 530 1,
532 531 READ_LOG_EXT_NCQ_ERROR_RECOVERY,
533 532 0,
534 533 0,
535 534 0,
536 535 SATAC_READ_LOG_EXT,
537 536 0,
538 537 0,
539 538 0,
540 539 };
541 540
542 541 /*
543 542 * ATAPI error recovery CDB
544 543 */
545 544 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = {
546 545 SCMD_REQUEST_SENSE,
547 546 0, /* Only fixed RQ format is supported */
548 547 0,
549 548 0,
550 549 SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */
551 550 0
552 551 };
553 552
554 553
555 554 /* Warlock directives */
556 555
557 556 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran))
558 557 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device))
559 558 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops))
560 559 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense))
561 560 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status))
562 561 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr))
563 562 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t))
564 563 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state))
565 564 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state))
566 565 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list))
567 566 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list))
568 567 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next))
569 568 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev))
570 569 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \
571 570 sata_hba_inst::satahba_scsi_tran))
572 571 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran))
573 572 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip))
574 573 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached))
575 574 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port))
576 575 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex,
577 576 sata_hba_inst::satahba_event_flags))
578 577 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
579 578 sata_cport_info::cport_devp))
580 579 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp))
581 580 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr))
582 581 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
583 582 sata_cport_info::cport_dev_type))
584 583 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type))
585 584 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
586 585 sata_cport_info::cport_state))
587 586 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state))
588 587 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
589 588 sata_pmport_info::pmport_state))
590 589 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state))
591 590 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
592 591 sata_pmport_info::pmport_dev_type))
593 592 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type))
594 593 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
595 594 sata_pmport_info::pmport_sata_drive))
596 595 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
597 596 sata_pmport_info::pmport_tgtnode_clean))
598 597 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
599 598 sata_pmport_info::pmport_event_flags))
600 599 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive))
601 600 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port))
602 601 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports))
603 602 #ifdef SATA_DEBUG
604 603 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count))
605 604 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count))
606 605 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace))
607 606 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index))
608 607 #endif
609 608
610 609 /* End of warlock directives */
611 610
612 611 /* ************** loadable module configuration functions ************** */
613 612
614 613 int
615 614 _init()
616 615 {
617 616 int rval;
618 617
619 618 mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL);
620 619 mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL);
621 620 mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL);
622 621 cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL);
623 622 sata_trace_rbuf_alloc();
624 623 if ((rval = mod_install(&modlinkage)) != 0) {
625 624 #ifdef SATA_DEBUG
626 625 cmn_err(CE_WARN, "sata: _init: mod_install failed\n");
627 626 #endif
628 627 sata_trace_rbuf_free();
629 628 mutex_destroy(&sata_log_mutex);
630 629 cv_destroy(&sata_event_cv);
631 630 mutex_destroy(&sata_event_mutex);
632 631 mutex_destroy(&sata_mutex);
633 632 }
634 633 return (rval);
635 634 }
636 635
637 636 int
638 637 _fini()
639 638 {
640 639 int rval;
641 640
642 641 if ((rval = mod_remove(&modlinkage)) != 0)
643 642 return (rval);
644 643
645 644 sata_trace_rbuf_free();
646 645 mutex_destroy(&sata_log_mutex);
647 646 cv_destroy(&sata_event_cv);
648 647 mutex_destroy(&sata_event_mutex);
649 648 mutex_destroy(&sata_mutex);
650 649 return (rval);
651 650 }
652 651
653 652 int
654 653 _info(struct modinfo *modinfop)
655 654 {
656 655 return (mod_info(&modlinkage, modinfop));
657 656 }
658 657
659 658
660 659
661 660 /* ********************* SATA HBA entry points ********************* */
662 661
663 662
664 663 /*
665 664 * Called by SATA HBA from _init().
666 665 * Registers HBA driver instance/sata framework pair with scsi framework, by
667 666 * calling scsi_hba_init().
668 667 *
669 668 * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used
670 669 * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver
671 670 * cb_ops pointer in SATA HBA driver dev_ops structure.
672 671 * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors.
673 672 *
674 673 * Return status of the scsi_hba_init() is returned to a calling SATA HBA
675 674 * driver.
676 675 */
677 676 int
678 677 sata_hba_init(struct modlinkage *modlp)
679 678 {
680 679 int rval;
681 680 struct dev_ops *hba_ops;
682 681
683 682 SATADBG1(SATA_DBG_HBA_IF, NULL,
684 683 "sata_hba_init: name %s \n",
685 684 ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
686 685 /*
687 686 * Fill-up cb_ops and dev_ops when necessary
688 687 */
689 688 hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
690 689 /*
691 690 * Provide pointer to SATA dev_ops
692 691 */
693 692 hba_ops->devo_cb_ops = &sata_cb_ops;
694 693
695 694 /*
696 695 * Register SATA HBA with SCSI framework
697 696 */
698 697 if ((rval = scsi_hba_init(modlp)) != 0) {
699 698 SATADBG1(SATA_DBG_HBA_IF, NULL,
700 699 "sata_hba_init: scsi hba init failed\n", NULL);
701 700 return (rval);
702 701 }
703 702
704 703 return (0);
705 704 }
706 705
707 706
708 707 /* HBA attach stages */
709 708 #define HBA_ATTACH_STAGE_SATA_HBA_INST 1
710 709 #define HBA_ATTACH_STAGE_SCSI_ATTACHED 2
711 710 #define HBA_ATTACH_STAGE_SETUP 4
712 711 #define HBA_ATTACH_STAGE_LINKED 8
713 712
714 713
715 714 /*
716 715 *
717 716 * Called from SATA HBA driver's attach routine to attach an instance of
718 717 * the HBA.
719 718 *
720 719 * For DDI_ATTACH command:
721 720 * sata_hba_inst structure is allocated here and initialized with pointers to
722 721 * SATA framework implementation of required scsi tran functions.
723 722 * The scsi_tran's tran_hba_private field is used by SATA Framework to point
724 723 * to the soft structure (sata_hba_inst) allocated by SATA framework for
725 724 * SATA HBA instance related data.
726 725 * The scsi_tran's tran_hba_private field is used by SATA framework to
727 726 * store a pointer to per-HBA-instance of sata_hba_inst structure.
728 727 * The sata_hba_inst structure is cross-linked to scsi tran structure.
729 728 * Among other info, a pointer to sata_hba_tran structure is stored in
730 729 * sata_hba_inst. The sata_hba_inst structures for different HBA instances are
731 730 * linked together into the list, pointed to by sata_hba_list.
732 731 * On the first HBA instance attach the sata event thread is initialized.
733 732 * Attachment points are created for all SATA ports of the HBA being attached.
734 733 * All HBA instance's SATA ports are probed and type of plugged devices is
735 734 * determined. For each device of a supported type, a target node is created.
736 735 *
737 736 * DDI_SUCCESS is returned when attachment process is successful,
738 737 * DDI_FAILURE is returned otherwise.
739 738 *
740 739 * For DDI_RESUME command:
741 740 * Not implemented at this time (postponed until phase 2 of the development).
742 741 */
743 742 int
744 743 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran,
745 744 ddi_attach_cmd_t cmd)
746 745 {
747 746 sata_hba_inst_t *sata_hba_inst;
748 747 scsi_hba_tran_t *scsi_tran = NULL;
749 748 int hba_attach_state = 0;
750 749 char taskq_name[MAXPATHLEN];
751 750
752 751 SATADBG3(SATA_DBG_HBA_IF, NULL,
753 752 "sata_hba_attach: node %s (%s%d)\n",
754 753 ddi_node_name(dip), ddi_driver_name(dip),
755 754 ddi_get_instance(dip));
756 755
757 756 if (cmd == DDI_RESUME) {
758 757 /*
759 758 * Postponed until phase 2 of the development
760 759 */
761 760 return (DDI_FAILURE);
762 761 }
763 762
764 763 if (cmd != DDI_ATTACH) {
765 764 return (DDI_FAILURE);
766 765 }
767 766
768 767 /* cmd == DDI_ATTACH */
769 768
770 769 if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) {
771 770 SATA_LOG_D((NULL, CE_WARN,
772 771 "sata_hba_attach: invalid sata_hba_tran"));
773 772 return (DDI_FAILURE);
774 773 }
775 774 /*
776 775 * Allocate and initialize SCSI tran structure.
777 776 * SATA copy of tran_bus_config is provided to create port nodes.
778 777 */
779 778 scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
780 779 if (scsi_tran == NULL)
781 780 return (DDI_FAILURE);
782 781 /*
783 782 * Allocate soft structure for SATA HBA instance.
784 783 * There is a separate softstate for each HBA instance.
785 784 */
786 785 sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP);
787 786 ASSERT(sata_hba_inst != NULL); /* this should not fail */
788 787 mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL);
789 788 hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST;
790 789
791 790 /*
792 791 * scsi_trans's tran_hba_private is used by SATA Framework to point to
793 792 * soft structure allocated by SATA framework for
794 793 * SATA HBA instance related data.
795 794 */
796 795 scsi_tran->tran_hba_private = sata_hba_inst;
797 796 scsi_tran->tran_tgt_private = NULL;
798 797
799 798 scsi_tran->tran_tgt_init = sata_scsi_tgt_init;
800 799 scsi_tran->tran_tgt_probe = sata_scsi_tgt_probe;
801 800 scsi_tran->tran_tgt_free = sata_scsi_tgt_free;
802 801
803 802 scsi_tran->tran_start = sata_scsi_start;
804 803 scsi_tran->tran_reset = sata_scsi_reset;
805 804 scsi_tran->tran_abort = sata_scsi_abort;
806 805 scsi_tran->tran_getcap = sata_scsi_getcap;
807 806 scsi_tran->tran_setcap = sata_scsi_setcap;
808 807 scsi_tran->tran_init_pkt = sata_scsi_init_pkt;
809 808 scsi_tran->tran_destroy_pkt = sata_scsi_destroy_pkt;
810 809
811 810 scsi_tran->tran_dmafree = sata_scsi_dmafree;
812 811 scsi_tran->tran_sync_pkt = sata_scsi_sync_pkt;
813 812
814 813 scsi_tran->tran_reset_notify = NULL;
815 814 scsi_tran->tran_get_bus_addr = NULL;
816 815 scsi_tran->tran_quiesce = NULL;
817 816 scsi_tran->tran_unquiesce = NULL;
818 817 scsi_tran->tran_bus_reset = NULL;
819 818
820 819 if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr,
821 820 scsi_tran, 0) != DDI_SUCCESS) {
822 821 #ifdef SATA_DEBUG
823 822 cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed",
824 823 ddi_driver_name(dip), ddi_get_instance(dip));
825 824 #endif
826 825 goto fail;
827 826 }
828 827 hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED;
829 828
830 829 if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) {
831 830 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
832 831 "sata", 1) != DDI_PROP_SUCCESS) {
833 832 SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: "
834 833 "failed to create hba sata prop"));
835 834 goto fail;
836 835 }
837 836 }
838 837
839 838 /*
840 839 * Save pointers in hba instance soft state.
841 840 */
842 841 sata_hba_inst->satahba_scsi_tran = scsi_tran;
843 842 sata_hba_inst->satahba_tran = sata_tran;
844 843 sata_hba_inst->satahba_dip = dip;
845 844
846 845 /*
847 846 * Create a task queue to handle emulated commands completion
848 847 * Use node name, dash, instance number as the queue name.
849 848 */
850 849 taskq_name[0] = '\0';
851 850 (void) strlcat(taskq_name, DEVI(dip)->devi_node_name,
852 851 sizeof (taskq_name));
853 852 (void) snprintf(taskq_name + strlen(taskq_name),
854 853 sizeof (taskq_name) - strlen(taskq_name),
855 854 "-%d", DEVI(dip)->devi_instance);
856 855 sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1,
857 856 minclsyspri, 1, sata_tran->sata_tran_hba_num_cports * 4,
858 857 TASKQ_DYNAMIC);
859 858
860 859 hba_attach_state |= HBA_ATTACH_STAGE_SETUP;
861 860
862 861 /*
863 862 * Create events thread if not created yet.
864 863 */
865 864 sata_event_thread_control(1);
866 865
867 866 /*
868 867 * Link this hba instance into the list.
869 868 */
870 869 mutex_enter(&sata_mutex);
871 870
872 871 if (sata_hba_list == NULL) {
873 872 /*
874 873 * The first instance of HBA is attached.
875 874 * Set current/active default maximum NCQ/TCQ queue depth for
876 875 * all SATA devices. It is done here and now, to eliminate the
877 876 * possibility of the dynamic, programatic modification of the
878 877 * queue depth via global (and public) sata_max_queue_depth
879 878 * variable (this would require special handling in HBA drivers)
880 879 */
881 880 sata_current_max_qdepth = sata_max_queue_depth;
882 881 if (sata_current_max_qdepth > 32)
883 882 sata_current_max_qdepth = 32;
884 883 else if (sata_current_max_qdepth < 1)
885 884 sata_current_max_qdepth = 1;
886 885 }
887 886
888 887 sata_hba_inst->satahba_next = NULL;
889 888 sata_hba_inst->satahba_prev = sata_hba_list_tail;
890 889 if (sata_hba_list == NULL) {
891 890 sata_hba_list = sata_hba_inst;
892 891 }
893 892 if (sata_hba_list_tail != NULL) {
894 893 sata_hba_list_tail->satahba_next = sata_hba_inst;
895 894 }
896 895 sata_hba_list_tail = sata_hba_inst;
897 896 mutex_exit(&sata_mutex);
898 897 hba_attach_state |= HBA_ATTACH_STAGE_LINKED;
899 898
900 899 /*
901 900 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl
902 901 * SATA HBA driver should not use its own open/close entry points.
903 902 *
904 903 * Make sure that instance number doesn't overflow
905 904 * when forming minor numbers.
906 905 */
907 906 ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT));
908 907 if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
909 908 INST2DEVCTL(ddi_get_instance(dip)),
910 909 DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) {
911 910 #ifdef SATA_DEBUG
912 911 cmn_err(CE_WARN, "sata_hba_attach: "
913 912 "cannot create devctl minor node");
914 913 #endif
915 914 goto fail;
916 915 }
917 916
918 917
919 918 /*
920 919 * Set-up kstats here, if necessary.
921 920 * (postponed until future phase of the development).
922 921 */
923 922
924 923 /*
925 924 * Indicate that HBA is attached. This will enable events processing
926 925 * for this HBA.
927 926 */
928 927 sata_hba_inst->satahba_attached = 1;
929 928 /*
930 929 * Probe controller ports. This operation will describe a current
931 930 * controller/port/multipliers/device configuration and will create
932 931 * attachment points.
933 932 * We may end-up with just a controller with no devices attached.
934 933 * For the ports with a supported device attached, device target nodes
935 934 * are created and devices are initialized.
936 935 */
937 936 sata_probe_ports(sata_hba_inst);
938 937
939 938 return (DDI_SUCCESS);
940 939
941 940 fail:
942 941 if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) {
943 942 (void) sata_remove_hba_instance(dip);
944 943 if (sata_hba_list == NULL)
945 944 sata_event_thread_control(0);
946 945 }
947 946
948 947 if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) {
949 948 (void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
950 949 taskq_destroy(sata_hba_inst->satahba_taskq);
951 950 }
952 951
953 952 if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED)
954 953 (void) scsi_hba_detach(dip);
955 954
956 955 if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) {
957 956 mutex_destroy(&sata_hba_inst->satahba_mutex);
958 957 kmem_free((void *)sata_hba_inst,
959 958 sizeof (struct sata_hba_inst));
960 959 scsi_hba_tran_free(scsi_tran);
961 960 }
962 961
963 962 sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed",
964 963 ddi_driver_name(dip), ddi_get_instance(dip));
965 964
966 965 return (DDI_FAILURE);
967 966 }
968 967
969 968
970 969 /*
971 970 * Called by SATA HBA from to detach an instance of the driver.
972 971 *
973 972 * For DDI_DETACH command:
974 973 * Free local structures allocated for SATA HBA instance during
975 974 * sata_hba_attach processing.
976 975 *
977 976 * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise.
978 977 *
979 978 * For DDI_SUSPEND command:
980 979 * Not implemented at this time (postponed until phase 2 of the development)
981 980 * Returnd DDI_SUCCESS.
982 981 *
983 982 * When the last HBA instance is detached, the event daemon is terminated.
984 983 *
985 984 * NOTE: Port multiplier is supported.
986 985 */
987 986 int
988 987 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
989 988 {
990 989 dev_info_t *tdip;
991 990 sata_hba_inst_t *sata_hba_inst;
992 991 scsi_hba_tran_t *scsi_hba_tran;
993 992 sata_cport_info_t *cportinfo;
994 993 sata_pmult_info_t *pminfo;
995 994 sata_drive_info_t *sdinfo;
996 995 sata_device_t sdevice;
997 996 int ncport, npmport;
998 997
999 998 SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n",
1000 999 ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip));
1001 1000
1002 1001 switch (cmd) {
1003 1002 case DDI_DETACH:
1004 1003
1005 1004 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1006 1005 return (DDI_FAILURE);
1007 1006
1008 1007 sata_hba_inst = scsi_hba_tran->tran_hba_private;
1009 1008 if (sata_hba_inst == NULL)
1010 1009 return (DDI_FAILURE);
1011 1010
1012 1011 if (scsi_hba_detach(dip) == DDI_FAILURE) {
1013 1012 sata_hba_inst->satahba_attached = 1;
1014 1013 return (DDI_FAILURE);
1015 1014 }
1016 1015
1017 1016 /*
1018 1017 * Free all target nodes - at this point
1019 1018 * devices should be at least offlined
1020 1019 * otherwise scsi_hba_detach() should not be called.
1021 1020 */
1022 1021 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
1023 1022 ncport++) {
1024 1023 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
1025 1024 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
1026 1025 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
1027 1026 if (sdinfo != NULL) {
1028 1027 tdip = sata_get_target_dip(dip,
1029 1028 ncport, 0);
1030 1029 if (tdip != NULL) {
1031 1030 if (ndi_devi_offline(tdip,
1032 1031 NDI_DEVI_REMOVE) !=
1033 1032 NDI_SUCCESS) {
1034 1033 SATA_LOG_D((
1035 1034 sata_hba_inst,
1036 1035 CE_WARN,
1037 1036 "sata_hba_detach: "
1038 1037 "Target node not "
1039 1038 "removed !"));
1040 1039 return (DDI_FAILURE);
1041 1040 }
1042 1041 }
1043 1042 }
1044 1043 } else { /* SATA_DTYPE_PMULT */
1045 1044 mutex_enter(&cportinfo->cport_mutex);
1046 1045 pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
1047 1046
1048 1047 if (pminfo == NULL) {
1049 1048 SATA_LOG_D((sata_hba_inst, CE_WARN,
1050 1049 "sata_hba_detach: Port multiplier "
1051 1050 "not ready yet!"));
1052 1051 mutex_exit(&cportinfo->cport_mutex);
1053 1052 return (DDI_FAILURE);
1054 1053 }
1055 1054
1056 1055 /*
1057 1056 * Detach would fail if removal of any of the
1058 1057 * target nodes is failed - albeit in that
1059 1058 * case some of them may have been removed.
1060 1059 */
1061 1060 for (npmport = 0; npmport < SATA_NUM_PMPORTS(
1062 1061 sata_hba_inst, ncport); npmport++) {
1063 1062 tdip = sata_get_target_dip(dip, ncport,
1064 1063 npmport);
1065 1064 if (tdip != NULL) {
1066 1065 if (ndi_devi_offline(tdip,
1067 1066 NDI_DEVI_REMOVE) !=
1068 1067 NDI_SUCCESS) {
1069 1068 SATA_LOG_D((
1070 1069 sata_hba_inst,
1071 1070 CE_WARN,
1072 1071 "sata_hba_detach: "
1073 1072 "Target node not "
1074 1073 "removed !"));
1075 1074 mutex_exit(&cportinfo->
1076 1075 cport_mutex);
1077 1076 return (DDI_FAILURE);
1078 1077 }
1079 1078 }
1080 1079 }
1081 1080 mutex_exit(&cportinfo->cport_mutex);
1082 1081 }
1083 1082 }
1084 1083 /*
1085 1084 * Disable sata event daemon processing for this HBA
1086 1085 */
1087 1086 sata_hba_inst->satahba_attached = 0;
1088 1087
1089 1088 /*
1090 1089 * Remove event daemon thread, if it is last HBA instance.
1091 1090 */
1092 1091
1093 1092 mutex_enter(&sata_mutex);
1094 1093 if (sata_hba_list->satahba_next == NULL) {
1095 1094 mutex_exit(&sata_mutex);
1096 1095 sata_event_thread_control(0);
1097 1096 mutex_enter(&sata_mutex);
1098 1097 }
1099 1098 mutex_exit(&sata_mutex);
1100 1099
1101 1100 /* Remove this HBA instance from the HBA list */
1102 1101 sata_remove_hba_instance(dip);
1103 1102
1104 1103 /*
1105 1104 * At this point there should be no target nodes attached.
1106 1105 * Detach and destroy device and port info structures.
1107 1106 */
1108 1107 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
1109 1108 ncport++) {
1110 1109 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
1111 1110 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
1112 1111 sdinfo =
1113 1112 cportinfo->cport_devp.cport_sata_drive;
1114 1113 if (sdinfo != NULL) {
1115 1114 /* Release device structure */
1116 1115 kmem_free(sdinfo,
1117 1116 sizeof (sata_drive_info_t));
1118 1117 }
1119 1118 /* Release cport info */
1120 1119 mutex_destroy(&cportinfo->cport_mutex);
1121 1120 kmem_free(cportinfo,
1122 1121 sizeof (sata_cport_info_t));
1123 1122 } else { /* SATA_DTYPE_PMULT */
1124 1123 sdevice.satadev_addr.cport = (uint8_t)ncport;
1125 1124 sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
1126 1125 sata_free_pmult(sata_hba_inst, &sdevice);
1127 1126 }
1128 1127 }
1129 1128
1130 1129 scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran);
1131 1130
1132 1131 (void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
1133 1132
1134 1133 taskq_destroy(sata_hba_inst->satahba_taskq);
1135 1134
1136 1135 mutex_destroy(&sata_hba_inst->satahba_mutex);
1137 1136 kmem_free((void *)sata_hba_inst,
1138 1137 sizeof (struct sata_hba_inst));
1139 1138
1140 1139 return (DDI_SUCCESS);
1141 1140
1142 1141 case DDI_SUSPEND:
1143 1142 /*
1144 1143 * Postponed until phase 2
1145 1144 */
1146 1145 return (DDI_FAILURE);
1147 1146
1148 1147 default:
1149 1148 return (DDI_FAILURE);
1150 1149 }
1151 1150 }
1152 1151
1153 1152
1154 1153 /*
1155 1154 * Called by an HBA drive from _fini() routine.
1156 1155 * Unregisters SATA HBA instance/SATA framework pair from the scsi framework.
1157 1156 */
1158 1157 void
1159 1158 sata_hba_fini(struct modlinkage *modlp)
1160 1159 {
1161 1160 SATADBG1(SATA_DBG_HBA_IF, NULL,
1162 1161 "sata_hba_fini: name %s\n",
1163 1162 ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
1164 1163
1165 1164 scsi_hba_fini(modlp);
1166 1165 }
1167 1166
1168 1167
1169 1168 /*
1170 1169 * Default open and close routine for sata_hba framework.
1171 1170 *
1172 1171 */
1173 1172 /*
1174 1173 * Open devctl node.
1175 1174 *
1176 1175 * Returns:
1177 1176 * 0 if node was open successfully, error code otherwise.
1178 1177 *
1179 1178 *
1180 1179 */
1181 1180
1182 1181 static int
1183 1182 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1184 1183 {
1185 1184 #ifndef __lock_lint
1186 1185 _NOTE(ARGUNUSED(credp))
1187 1186 #endif
1188 1187 int rv = 0;
1189 1188 dev_info_t *dip;
1190 1189 scsi_hba_tran_t *scsi_hba_tran;
1191 1190 sata_hba_inst_t *sata_hba_inst;
1192 1191
1193 1192 SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL);
1194 1193
1195 1194 if (otyp != OTYP_CHR)
1196 1195 return (EINVAL);
1197 1196
1198 1197 dip = sata_devt_to_devinfo(*devp);
1199 1198 if (dip == NULL)
1200 1199 return (ENXIO);
1201 1200
1202 1201 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1203 1202 return (ENXIO);
1204 1203
1205 1204 sata_hba_inst = scsi_hba_tran->tran_hba_private;
1206 1205 if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1207 1206 return (ENXIO);
1208 1207
1209 1208 mutex_enter(&sata_mutex);
1210 1209 if (flags & FEXCL) {
1211 1210 if (sata_hba_inst->satahba_open_flag != 0) {
1212 1211 rv = EBUSY;
1213 1212 } else {
1214 1213 sata_hba_inst->satahba_open_flag =
1215 1214 SATA_DEVCTL_EXOPENED;
1216 1215 }
1217 1216 } else {
1218 1217 if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) {
1219 1218 rv = EBUSY;
1220 1219 } else {
1221 1220 sata_hba_inst->satahba_open_flag =
1222 1221 SATA_DEVCTL_SOPENED;
1223 1222 }
1224 1223 }
1225 1224 mutex_exit(&sata_mutex);
1226 1225
1227 1226 return (rv);
1228 1227 }
1229 1228
1230 1229
1231 1230 /*
1232 1231 * Close devctl node.
1233 1232 * Returns:
1234 1233 * 0 if node was closed successfully, error code otherwise.
1235 1234 *
1236 1235 */
1237 1236
1238 1237 static int
1239 1238 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
1240 1239 {
1241 1240 #ifndef __lock_lint
1242 1241 _NOTE(ARGUNUSED(credp))
1243 1242 _NOTE(ARGUNUSED(flag))
1244 1243 #endif
1245 1244 dev_info_t *dip;
1246 1245 scsi_hba_tran_t *scsi_hba_tran;
1247 1246 sata_hba_inst_t *sata_hba_inst;
1248 1247
1249 1248 SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL);
1250 1249
1251 1250 if (otyp != OTYP_CHR)
1252 1251 return (EINVAL);
1253 1252
1254 1253 dip = sata_devt_to_devinfo(dev);
1255 1254 if (dip == NULL)
1256 1255 return (ENXIO);
1257 1256
1258 1257 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1259 1258 return (ENXIO);
1260 1259
1261 1260 sata_hba_inst = scsi_hba_tran->tran_hba_private;
1262 1261 if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1263 1262 return (ENXIO);
1264 1263
1265 1264 mutex_enter(&sata_mutex);
1266 1265 sata_hba_inst->satahba_open_flag = 0;
1267 1266 mutex_exit(&sata_mutex);
1268 1267 return (0);
1269 1268 }
1270 1269
1271 1270
1272 1271
1273 1272 /*
1274 1273 * Standard IOCTL commands for SATA hotplugging.
1275 1274 * Implemented DEVCTL_AP commands:
1276 1275 * DEVCTL_AP_CONNECT
1277 1276 * DEVCTL_AP_DISCONNECT
1278 1277 * DEVCTL_AP_CONFIGURE
1279 1278 * DEVCTL_UNCONFIGURE
1280 1279 * DEVCTL_AP_CONTROL
1281 1280 *
1282 1281 * Commands passed to default ndi ioctl handler:
1283 1282 * DEVCTL_DEVICE_GETSTATE
1284 1283 * DEVCTL_DEVICE_ONLINE
1285 1284 * DEVCTL_DEVICE_OFFLINE
1286 1285 * DEVCTL_DEVICE_REMOVE
1287 1286 * DEVCTL_DEVICE_INSERT
1288 1287 * DEVCTL_BUS_GETSTATE
1289 1288 *
1290 1289 * All other cmds are passed to HBA if it provide ioctl handler, or failed
1291 1290 * if not.
1292 1291 *
1293 1292 * Returns:
1294 1293 * 0 if successful,
1295 1294 * error code if operation failed.
1296 1295 *
1297 1296 * Port Multiplier support is supported now.
1298 1297 *
1299 1298 * NOTE: qual should be SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT
1300 1299 */
1301 1300
1302 1301 static int
1303 1302 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1304 1303 int *rvalp)
1305 1304 {
1306 1305 #ifndef __lock_lint
1307 1306 _NOTE(ARGUNUSED(credp))
1308 1307 _NOTE(ARGUNUSED(rvalp))
1309 1308 #endif
1310 1309 int rv = 0;
1311 1310 int32_t comp_port = -1;
1312 1311 dev_info_t *dip;
1313 1312 devctl_ap_state_t ap_state;
1314 1313 struct devctl_iocdata *dcp = NULL;
1315 1314 scsi_hba_tran_t *scsi_hba_tran;
1316 1315 sata_hba_inst_t *sata_hba_inst;
1317 1316 sata_device_t sata_device;
1318 1317 sata_cport_info_t *cportinfo;
1319 1318 int cport, pmport, qual;
1320 1319 int rval = SATA_SUCCESS;
1321 1320
1322 1321 dip = sata_devt_to_devinfo(dev);
1323 1322 if (dip == NULL)
1324 1323 return (ENXIO);
1325 1324
1326 1325 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1327 1326 return (ENXIO);
1328 1327
1329 1328 sata_hba_inst = scsi_hba_tran->tran_hba_private;
1330 1329 if (sata_hba_inst == NULL)
1331 1330 return (ENXIO);
1332 1331
1333 1332 if (sata_hba_inst->satahba_tran == NULL)
1334 1333 return (ENXIO);
1335 1334
1336 1335 switch (cmd) {
1337 1336
1338 1337 case DEVCTL_DEVICE_GETSTATE:
1339 1338 case DEVCTL_DEVICE_ONLINE:
1340 1339 case DEVCTL_DEVICE_OFFLINE:
1341 1340 case DEVCTL_DEVICE_REMOVE:
1342 1341 case DEVCTL_BUS_GETSTATE:
1343 1342 /*
1344 1343 * There may be more cases that we want to pass to default
1345 1344 * handler rather than fail them.
1346 1345 */
1347 1346 return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
1348 1347 }
1349 1348
1350 1349 /* read devctl ioctl data */
1351 1350 if (cmd != DEVCTL_AP_CONTROL) {
1352 1351 if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1353 1352 return (EFAULT);
1354 1353
1355 1354 if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) ==
1356 1355 -1) {
1357 1356 if (dcp)
1358 1357 ndi_dc_freehdl(dcp);
1359 1358 return (EINVAL);
1360 1359 }
1361 1360
1362 1361 /*
1363 1362 * According to SCSI_TO_SATA_ADDR_QUAL, qual should be either
1364 1363 * SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT.
1365 1364 */
1366 1365 cport = SCSI_TO_SATA_CPORT(comp_port);
1367 1366 pmport = SCSI_TO_SATA_PMPORT(comp_port);
1368 1367 qual = SCSI_TO_SATA_ADDR_QUAL(comp_port);
1369 1368
1370 1369 if (sata_validate_sata_address(sata_hba_inst, cport, pmport,
1371 1370 qual) != 0) {
1372 1371 ndi_dc_freehdl(dcp);
1373 1372 return (EINVAL);
1374 1373 }
1375 1374
1376 1375 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1377 1376 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1378 1377 cport_mutex);
1379 1378 if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1380 1379 /*
1381 1380 * Cannot process ioctl request now. Come back later.
1382 1381 */
1383 1382 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1384 1383 cport_mutex);
1385 1384 ndi_dc_freehdl(dcp);
1386 1385 return (EBUSY);
1387 1386 }
1388 1387 /* Block event processing for this port */
1389 1388 cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1390 1389 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1391 1390
1392 1391 sata_device.satadev_addr.cport = cport;
1393 1392 sata_device.satadev_addr.pmport = pmport;
1394 1393 sata_device.satadev_addr.qual = qual;
1395 1394 sata_device.satadev_rev = SATA_DEVICE_REV;
1396 1395 }
1397 1396
1398 1397 switch (cmd) {
1399 1398
1400 1399 case DEVCTL_AP_DISCONNECT:
1401 1400
1402 1401 /*
1403 1402 * Normally, cfgadm sata plugin will try to offline
1404 1403 * (unconfigure) device before this request. Nevertheless,
1405 1404 * if a device is still configured, we need to
1406 1405 * attempt to offline and unconfigure device first, and we will
1407 1406 * deactivate the port regardless of the unconfigure
1408 1407 * operation results.
1409 1408 *
1410 1409 */
1411 1410 rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device);
1412 1411
1413 1412 break;
1414 1413
1415 1414 case DEVCTL_AP_UNCONFIGURE:
1416 1415
1417 1416 /*
1418 1417 * The unconfigure operation uses generic nexus operation to
1419 1418 * offline a device. It leaves a target device node attached.
1420 1419 * and obviously sata_drive_info attached as well, because
1421 1420 * from the hardware point of view nothing has changed.
1422 1421 */
1423 1422 rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device);
1424 1423 break;
1425 1424
1426 1425 case DEVCTL_AP_CONNECT:
1427 1426 {
1428 1427 /*
1429 1428 * The sata cfgadm pluging will invoke this operation only if
1430 1429 * port was found in the disconnect state (failed state
1431 1430 * is also treated as the disconnected state).
1432 1431 * If port activation is successful and a device is found
1433 1432 * attached to the port, the initialization sequence is
1434 1433 * executed to probe the port and attach
1435 1434 * a device structure to a port structure. The device is not
1436 1435 * set in configured state (system-wise) by this operation.
1437 1436 */
1438 1437
1439 1438 rv = sata_ioctl_connect(sata_hba_inst, &sata_device);
1440 1439
1441 1440 break;
1442 1441 }
1443 1442
1444 1443 case DEVCTL_AP_CONFIGURE:
1445 1444 {
1446 1445 /*
1447 1446 * A port may be in an active or shutdown state.
1448 1447 * If port is in a failed state, operation is aborted.
1449 1448 * If a port is in a shutdown state, sata_tran_port_activate()
1450 1449 * is invoked prior to any other operation.
1451 1450 *
1452 1451 * Onlining the device involves creating a new target node.
1453 1452 * If there is an old target node present (belonging to
1454 1453 * previously removed device), the operation is aborted - the
1455 1454 * old node has to be released and removed before configure
1456 1455 * operation is attempted.
1457 1456 */
1458 1457
1459 1458 rv = sata_ioctl_configure(sata_hba_inst, &sata_device);
1460 1459
1461 1460 break;
1462 1461 }
1463 1462
1464 1463 case DEVCTL_AP_GETSTATE:
1465 1464
1466 1465 sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state);
1467 1466
1468 1467 ap_state.ap_last_change = (time_t)-1;
1469 1468 ap_state.ap_error_code = 0;
1470 1469 ap_state.ap_in_transition = 0;
1471 1470
1472 1471 /* Copy the return AP-state information to the user space */
1473 1472 if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
1474 1473 rv = EFAULT;
1475 1474 }
1476 1475 break;
1477 1476
1478 1477 case DEVCTL_AP_CONTROL:
1479 1478 {
1480 1479 /*
1481 1480 * Generic devctl for hardware specific functionality
1482 1481 */
1483 1482 sata_ioctl_data_t ioc;
1484 1483
1485 1484 ASSERT(dcp == NULL);
1486 1485
1487 1486 /* Copy in user ioctl data first */
1488 1487 #ifdef _MULTI_DATAMODEL
1489 1488 if (ddi_model_convert_from(mode & FMODELS) ==
1490 1489 DDI_MODEL_ILP32) {
1491 1490
1492 1491 sata_ioctl_data_32_t ioc32;
1493 1492
1494 1493 if (ddi_copyin((void *)arg, (void *)&ioc32,
1495 1494 sizeof (ioc32), mode) != 0) {
1496 1495 rv = EFAULT;
1497 1496 break;
1498 1497 }
1499 1498 ioc.cmd = (uint_t)ioc32.cmd;
1500 1499 ioc.port = (uint_t)ioc32.port;
1501 1500 ioc.get_size = (uint_t)ioc32.get_size;
1502 1501 ioc.buf = (caddr_t)(uintptr_t)ioc32.buf;
1503 1502 ioc.bufsiz = (uint_t)ioc32.bufsiz;
1504 1503 ioc.misc_arg = (uint_t)ioc32.misc_arg;
1505 1504 } else
1506 1505 #endif /* _MULTI_DATAMODEL */
1507 1506 if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
1508 1507 mode) != 0) {
1509 1508 return (EFAULT);
1510 1509 }
1511 1510
1512 1511 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
1513 1512 "sata_hba_ioctl: DEVCTL_AP_CONTROL "
1514 1513 "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port);
1515 1514
1516 1515 /*
1517 1516 * To avoid BE/LE and 32/64 issues, a get_size always returns
1518 1517 * a 32-bit number.
1519 1518 */
1520 1519 if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
1521 1520 return (EINVAL);
1522 1521 }
1523 1522 /* validate address */
1524 1523 cport = SCSI_TO_SATA_CPORT(ioc.port);
1525 1524 pmport = SCSI_TO_SATA_PMPORT(ioc.port);
1526 1525 qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port);
1527 1526
1528 1527 SATADBG3(SATA_DBG_IOCTL_IF, sata_hba_inst,
1529 1528 "sata_hba_ioctl: target port is %d:%d (%d)",
1530 1529 cport, pmport, qual);
1531 1530
1532 1531 if (sata_validate_sata_address(sata_hba_inst, cport,
1533 1532 pmport, qual) != 0)
1534 1533 return (EINVAL);
1535 1534
1536 1535 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1537 1536 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1538 1537 cport_mutex);
1539 1538 /* Is the port locked by event processing daemon ? */
1540 1539 if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1541 1540 /*
1542 1541 * Cannot process ioctl request now. Come back later
1543 1542 */
1544 1543 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1545 1544 cport_mutex);
1546 1545 return (EBUSY);
1547 1546 }
1548 1547 /* Block event processing for this port */
1549 1548 cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1550 1549 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1551 1550
1552 1551
1553 1552 sata_device.satadev_addr.cport = cport;
1554 1553 sata_device.satadev_addr.pmport = pmport;
1555 1554 sata_device.satadev_addr.qual = qual;
1556 1555 sata_device.satadev_rev = SATA_DEVICE_REV;
1557 1556
1558 1557 switch (ioc.cmd) {
1559 1558
1560 1559 case SATA_CFGA_RESET_PORT:
1561 1560 /*
1562 1561 * There is no protection for configured device.
1563 1562 */
1564 1563 rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device);
1565 1564 break;
1566 1565
1567 1566 case SATA_CFGA_RESET_DEVICE:
1568 1567 /*
1569 1568 * There is no protection for configured device.
1570 1569 */
1571 1570 rv = sata_ioctl_reset_device(sata_hba_inst,
1572 1571 &sata_device);
1573 1572 break;
1574 1573
1575 1574 case SATA_CFGA_RESET_ALL:
1576 1575 /*
1577 1576 * There is no protection for configured devices.
1578 1577 */
1579 1578 rv = sata_ioctl_reset_all(sata_hba_inst);
1580 1579 /*
1581 1580 * We return here, because common return is for
1582 1581 * a single port operation - we have already unlocked
1583 1582 * all ports and no dc handle was allocated.
1584 1583 */
1585 1584 return (rv);
1586 1585
1587 1586 case SATA_CFGA_PORT_DEACTIVATE:
1588 1587 /*
1589 1588 * Arbitrarily unconfigure attached device, if any.
1590 1589 * Even if the unconfigure fails, proceed with the
1591 1590 * port deactivation.
1592 1591 */
1593 1592 rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device);
1594 1593
1595 1594 break;
1596 1595
1597 1596 case SATA_CFGA_PORT_ACTIVATE:
1598 1597
1599 1598 rv = sata_ioctl_activate(sata_hba_inst, &sata_device);
1600 1599 break;
1601 1600
1602 1601 case SATA_CFGA_PORT_SELF_TEST:
1603 1602
1604 1603 rv = sata_ioctl_port_self_test(sata_hba_inst,
1605 1604 &sata_device);
1606 1605 break;
1607 1606
1608 1607 case SATA_CFGA_GET_DEVICE_PATH:
1609 1608
1610 1609 rv = sata_ioctl_get_device_path(sata_hba_inst,
1611 1610 &sata_device, &ioc, mode);
1612 1611 break;
1613 1612
1614 1613 case SATA_CFGA_GET_AP_TYPE:
1615 1614
1616 1615 rv = sata_ioctl_get_ap_type(sata_hba_inst,
1617 1616 &sata_device, &ioc, mode);
1618 1617 break;
1619 1618
1620 1619 case SATA_CFGA_GET_MODEL_INFO:
1621 1620
1622 1621 rv = sata_ioctl_get_model_info(sata_hba_inst,
1623 1622 &sata_device, &ioc, mode);
1624 1623 break;
1625 1624
1626 1625 case SATA_CFGA_GET_REVFIRMWARE_INFO:
1627 1626
1628 1627 rv = sata_ioctl_get_revfirmware_info(sata_hba_inst,
1629 1628 &sata_device, &ioc, mode);
1630 1629 break;
1631 1630
1632 1631 case SATA_CFGA_GET_SERIALNUMBER_INFO:
1633 1632
1634 1633 rv = sata_ioctl_get_serialnumber_info(sata_hba_inst,
1635 1634 &sata_device, &ioc, mode);
1636 1635 break;
1637 1636
1638 1637 default:
1639 1638 rv = EINVAL;
1640 1639 break;
1641 1640
1642 1641 } /* End of DEVCTL_AP_CONTROL cmd switch */
1643 1642
1644 1643 break;
1645 1644 }
1646 1645
1647 1646 default:
1648 1647 {
1649 1648 /*
1650 1649 * If we got here, we got an IOCTL that SATA HBA Framework
1651 1650 * does not recognize. Pass ioctl to HBA driver, in case
1652 1651 * it could process it.
1653 1652 */
1654 1653 sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran;
1655 1654 dev_info_t *mydip = SATA_DIP(sata_hba_inst);
1656 1655
1657 1656 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1658 1657 "IOCTL 0x%2x not supported in SATA framework, "
1659 1658 "passthrough to HBA", cmd);
1660 1659
1661 1660 if (sata_tran->sata_tran_ioctl == NULL) {
1662 1661 rv = EINVAL;
1663 1662 break;
1664 1663 }
1665 1664 rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg);
1666 1665 if (rval != 0) {
1667 1666 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1668 1667 "IOCTL 0x%2x failed in HBA", cmd);
1669 1668 rv = rval;
1670 1669 }
1671 1670 break;
1672 1671 }
1673 1672
1674 1673 } /* End of main IOCTL switch */
1675 1674
1676 1675 if (dcp) {
1677 1676 ndi_dc_freehdl(dcp);
1678 1677 }
1679 1678 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1680 1679 cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
1681 1680 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1682 1681
1683 1682 return (rv);
1684 1683 }
1685 1684
1686 1685
1687 1686 /*
1688 1687 * Create error retrieval sata packet
1689 1688 *
1690 1689 * A sata packet is allocated and set-up to contain specified error retrieval
1691 1690 * command and appropriate dma-able data buffer.
1692 1691 * No association with any scsi packet is made and no callback routine is
1693 1692 * specified.
1694 1693 *
1695 1694 * Returns a pointer to sata packet upon successful packet creation.
1696 1695 * Returns NULL, if packet cannot be created.
1697 1696 */
1698 1697 sata_pkt_t *
1699 1698 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device,
1700 1699 int pkt_type)
1701 1700 {
1702 1701 sata_hba_inst_t *sata_hba_inst;
1703 1702 sata_pkt_txlate_t *spx;
1704 1703 sata_pkt_t *spkt;
1705 1704 sata_drive_info_t *sdinfo;
1706 1705
1707 1706 mutex_enter(&sata_mutex);
1708 1707 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1709 1708 sata_hba_inst = sata_hba_inst->satahba_next) {
1710 1709 if (SATA_DIP(sata_hba_inst) == dip)
1711 1710 break;
1712 1711 }
1713 1712 mutex_exit(&sata_mutex);
1714 1713 ASSERT(sata_hba_inst != NULL);
1715 1714
1716 1715 sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
1717 1716 if (sdinfo == NULL) {
1718 1717 sata_log(sata_hba_inst, CE_WARN,
1719 1718 "sata: error recovery request for non-attached device at "
1720 1719 "cport %d", sata_device->satadev_addr.cport);
1721 1720 return (NULL);
1722 1721 }
1723 1722
1724 1723 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1725 1724 spx->txlt_sata_hba_inst = sata_hba_inst;
1726 1725 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */
1727 1726 spkt = sata_pkt_alloc(spx, NULL);
1728 1727 if (spkt == NULL) {
1729 1728 kmem_free(spx, sizeof (sata_pkt_txlate_t));
1730 1729 return (NULL);
1731 1730 }
1732 1731 /* address is needed now */
1733 1732 spkt->satapkt_device.satadev_addr = sata_device->satadev_addr;
1734 1733
1735 1734 switch (pkt_type) {
1736 1735 case SATA_ERR_RETR_PKT_TYPE_NCQ:
1737 1736 if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) {
1738 1737 if (sata_check_for_dma_error(dip, spx)) {
1739 1738 ddi_fm_service_impact(dip,
1740 1739 DDI_SERVICE_UNAFFECTED);
1741 1740 break;
1742 1741 }
1743 1742 return (spkt);
1744 1743 }
1745 1744 break;
1746 1745
1747 1746 case SATA_ERR_RETR_PKT_TYPE_ATAPI:
1748 1747 if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) {
1749 1748 if (sata_check_for_dma_error(dip, spx)) {
1750 1749 ddi_fm_service_impact(dip,
1751 1750 DDI_SERVICE_UNAFFECTED);
1752 1751 break;
1753 1752 }
1754 1753 return (spkt);
1755 1754 }
1756 1755 break;
1757 1756
1758 1757 default:
1759 1758 break;
1760 1759 }
1761 1760
1762 1761 sata_pkt_free(spx);
1763 1762 kmem_free(spx, sizeof (sata_pkt_txlate_t));
1764 1763 return (NULL);
1765 1764
1766 1765 }
1767 1766
1768 1767
1769 1768 /*
1770 1769 * Free error retrieval sata packet
1771 1770 *
1772 1771 * Free sata packet and any associated resources allocated previously by
1773 1772 * sata_get_error_retrieval_pkt().
1774 1773 *
1775 1774 * Void return.
1776 1775 */
1777 1776 void
1778 1777 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt)
1779 1778 {
1780 1779 sata_pkt_txlate_t *spx =
1781 1780 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1782 1781
1783 1782 ASSERT(sata_pkt != NULL);
1784 1783
1785 1784 sata_free_local_buffer(spx);
1786 1785 sata_pkt_free(spx);
1787 1786 kmem_free(spx, sizeof (sata_pkt_txlate_t));
1788 1787
1789 1788 }
1790 1789
1791 1790 /*
1792 1791 * Create READ PORT MULTIPLIER and WRITE PORT MULTIPLIER sata packet
1793 1792 *
1794 1793 * No association with any scsi packet is made and no callback routine is
1795 1794 * specified.
1796 1795 *
1797 1796 * Returns a pointer to sata packet upon successful packet creation.
1798 1797 * Returns NULL, if packet cannot be created.
1799 1798 *
1800 1799 * NOTE: Input/Output value includes 64 bits accoring to SATA Spec 2.6,
1801 1800 * only lower 32 bits are available currently.
1802 1801 */
1803 1802 sata_pkt_t *
1804 1803 sata_get_rdwr_pmult_pkt(dev_info_t *dip, sata_device_t *sd,
1805 1804 uint8_t regn, uint32_t regv, uint32_t type)
1806 1805 {
1807 1806 sata_hba_inst_t *sata_hba_inst;
1808 1807 sata_pkt_txlate_t *spx;
1809 1808 sata_pkt_t *spkt;
1810 1809 sata_cmd_t *scmd;
1811 1810
1812 1811 /* Only READ/WRITE commands are accepted. */
1813 1812 ASSERT(type == SATA_RDWR_PMULT_PKT_TYPE_READ ||
1814 1813 type == SATA_RDWR_PMULT_PKT_TYPE_WRITE);
1815 1814
1816 1815 mutex_enter(&sata_mutex);
1817 1816 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1818 1817 sata_hba_inst = sata_hba_inst->satahba_next) {
1819 1818 if (SATA_DIP(sata_hba_inst) == dip)
1820 1819 break;
1821 1820 }
1822 1821 mutex_exit(&sata_mutex);
1823 1822 ASSERT(sata_hba_inst != NULL);
1824 1823
1825 1824 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1826 1825 spx->txlt_sata_hba_inst = sata_hba_inst;
1827 1826 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */
1828 1827 spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
1829 1828 if (spkt == NULL) {
1830 1829 kmem_free(spx, sizeof (sata_pkt_txlate_t));
1831 1830 return (NULL);
1832 1831 }
1833 1832
1834 1833 /*
1835 1834 * NOTE: We need to send this command to the port multiplier,
1836 1835 * that means send to SATA_PMULT_HOSTPORT(0xf) pmport
1837 1836 *
1838 1837 * sata_device contains the address of actual target device, and the
1839 1838 * pmport number in the command comes from the sata_device structure.
1840 1839 */
1841 1840 spkt->satapkt_device.satadev_addr = sd->satadev_addr;
1842 1841 spkt->satapkt_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
1843 1842 spkt->satapkt_device.satadev_addr.qual = SATA_ADDR_PMULT;
1844 1843
1845 1844 /* Fill sata_pkt */
1846 1845 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_POLLING;
1847 1846 spkt->satapkt_comp = NULL; /* Synchronous mode, no callback */
1848 1847 spkt->satapkt_time = 10; /* Timeout 10s */
1849 1848
1850 1849 /* Build READ PORT MULTIPLIER cmd in the sata_pkt */
1851 1850 scmd = &spkt->satapkt_cmd;
1852 1851 scmd->satacmd_features_reg = regn & 0xff;
1853 1852 scmd->satacmd_features_reg_ext = (regn >> 8) & 0xff;
1854 1853 scmd->satacmd_device_reg = sd->satadev_addr.pmport;
1855 1854 scmd->satacmd_addr_type = 0; /* N/A */
1856 1855
1857 1856 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
1858 1857
1859 1858 if (type == SATA_RDWR_PMULT_PKT_TYPE_READ) {
1860 1859 scmd->satacmd_cmd_reg = SATAC_READ_PORTMULT;
1861 1860 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
1862 1861 scmd->satacmd_flags.sata_special_regs = 1;
1863 1862 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1;
1864 1863 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1;
1865 1864 scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1;
1866 1865 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1;
1867 1866 } else if (type == SATA_RDWR_PMULT_PKT_TYPE_WRITE) {
1868 1867 scmd->satacmd_cmd_reg = SATAC_WRITE_PORTMULT;
1869 1868 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
1870 1869 scmd->satacmd_sec_count_lsb = regv & 0xff;
1871 1870 scmd->satacmd_lba_low_lsb = regv >> 8 & 0xff;
1872 1871 scmd->satacmd_lba_mid_lsb = regv >> 16 & 0xff;
1873 1872 scmd->satacmd_lba_high_lsb = regv >> 24 & 0xff;
1874 1873 }
1875 1874
1876 1875 return (spkt);
1877 1876 }
1878 1877
1879 1878 /*
1880 1879 * Free sata packet and any associated resources allocated previously by
1881 1880 * sata_get_rdwr_pmult_pkt().
1882 1881 *
1883 1882 * Void return.
1884 1883 */
1885 1884 void
1886 1885 sata_free_rdwr_pmult_pkt(sata_pkt_t *sata_pkt)
1887 1886 {
1888 1887 sata_pkt_txlate_t *spx =
1889 1888 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1890 1889
1891 1890 /* Free allocated resources */
1892 1891 sata_pkt_free(spx);
1893 1892 kmem_free(spx, sizeof (sata_pkt_txlate_t));
1894 1893 }
1895 1894
1896 1895 /*
1897 1896 * Register a port multiplier to framework.
1898 1897 * 1) Store the GSCR values in the previous allocated pmult_info strctures.
1899 1898 * 2) Search in the blacklist and update the number of the device ports of the
1900 1899 * port multiplier.
1901 1900 *
1902 1901 * Void return.
1903 1902 */
1904 1903 void
1905 1904 sata_register_pmult(dev_info_t *dip, sata_device_t *sd, sata_pmult_gscr_t *sg)
1906 1905 {
1907 1906 sata_hba_inst_t *sata_hba_inst = NULL;
1908 1907 sata_pmult_info_t *pmultinfo;
1909 1908 sata_pmult_bl_t *blp;
1910 1909 int cport = sd->satadev_addr.cport;
1911 1910
1912 1911 mutex_enter(&sata_mutex);
1913 1912 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1914 1913 sata_hba_inst = sata_hba_inst->satahba_next) {
1915 1914 if (SATA_DIP(sata_hba_inst) == dip)
1916 1915 if (sata_hba_inst->satahba_attached == 1)
1917 1916 break;
1918 1917 }
1919 1918 mutex_exit(&sata_mutex);
1920 1919 /* HBA not attached? */
1921 1920 if (sata_hba_inst == NULL)
1922 1921 return;
1923 1922
1924 1923 /* Number of pmports */
1925 1924 sd->satadev_add_info = sg->gscr2 & SATA_PMULT_PORTNUM_MASK;
1926 1925
1927 1926 /* Check the blacklist */
1928 1927 for (blp = sata_pmult_blacklist; blp->bl_gscr0; blp++) {
1929 1928 if (sg->gscr0 != blp->bl_gscr0 && blp->bl_gscr0)
1930 1929 continue;
1931 1930 if (sg->gscr1 != blp->bl_gscr1 && blp->bl_gscr1)
1932 1931 continue;
1933 1932 if (sg->gscr2 != blp->bl_gscr2 && blp->bl_gscr2)
1934 1933 continue;
1935 1934
1936 1935 cmn_err(CE_WARN, "!Port multiplier is on the blacklist.");
1937 1936 sd->satadev_add_info = blp->bl_flags;
1938 1937 break;
1939 1938 }
1940 1939
1941 1940 /* Register the port multiplier GSCR */
1942 1941 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
1943 1942 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
1944 1943 if (pmultinfo != NULL) {
1945 1944 pmultinfo->pmult_gscr = *sg;
1946 1945 pmultinfo->pmult_num_dev_ports =
1947 1946 sd->satadev_add_info & SATA_PMULT_PORTNUM_MASK;
1948 1947 SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
1949 1948 "Port multiplier registered at port %d", cport);
1950 1949 }
1951 1950 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
1952 1951 }
1953 1952
1954 1953 /*
1955 1954 * sata_split_model splits the model ID into vendor and product IDs.
1956 1955 * It assumes that a vendor ID cannot be longer than 8 characters, and
1957 1956 * that vendor and product ID are separated by a whitespace.
1958 1957 */
1959 1958 void
1960 1959 sata_split_model(char *model, char **vendor, char **product)
1961 1960 {
1962 1961 int i, modlen;
1963 1962 char *vid, *pid;
1964 1963
1965 1964 /*
1966 1965 * remove whitespace at the end of model
1967 1966 */
1968 1967 for (i = SATA_ID_MODEL_LEN; i > 0; i--)
1969 1968 if (model[i] == ' ' || model[i] == '\t' || model[i] == '\0')
1970 1969 model[i] = '\0';
1971 1970 else
1972 1971 break;
1973 1972
1974 1973 /*
1975 1974 * try to split model into into vid/pid
1976 1975 */
1977 1976 modlen = strlen(model);
1978 1977 for (i = 0, pid = model; i < modlen; i++, pid++)
1979 1978 if ((*pid == ' ') || (*pid == '\t'))
1980 1979 break;
1981 1980
1982 1981 /*
1983 1982 * only use vid if it is less than 8 chars (as in SCSI)
1984 1983 */
1985 1984 if (i < modlen && i <= 8) {
1986 1985 vid = model;
1987 1986 /*
1988 1987 * terminate vid, establish pid
1989 1988 */
1990 1989 *pid++ = '\0';
1991 1990 } else {
1992 1991 /*
1993 1992 * vid will stay "ATA "
1994 1993 */
1995 1994 vid = NULL;
1996 1995 /*
1997 1996 * model is all pid
1998 1997 */
1999 1998 pid = model;
2000 1999 }
2001 2000
2002 2001 *vendor = vid;
2003 2002 *product = pid;
2004 2003 }
2005 2004
2006 2005 /*
2007 2006 * sata_name_child is for composing the name of the node
2008 2007 * the format of the name is "target,0".
2009 2008 */
2010 2009 static int
2011 2010 sata_name_child(dev_info_t *dip, char *name, int namelen)
2012 2011 {
2013 2012 int target;
2014 2013
2015 2014 target = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
2016 2015 DDI_PROP_DONTPASS, "target", -1);
2017 2016 if (target == -1)
2018 2017 return (DDI_FAILURE);
2019 2018 (void) snprintf(name, namelen, "%x,0", target);
2020 2019 return (DDI_SUCCESS);
2021 2020 }
2022 2021
2023 2022
2024 2023
2025 2024 /* ****************** SCSA required entry points *********************** */
2026 2025
2027 2026 /*
2028 2027 * Implementation of scsi tran_tgt_init.
2029 2028 * sata_scsi_tgt_init() initializes scsi_device structure
2030 2029 *
2031 2030 * If successful, DDI_SUCCESS is returned.
2032 2031 * DDI_FAILURE is returned if addressed device does not exist
2033 2032 */
2034 2033
2035 2034 static int
2036 2035 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2037 2036 scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2038 2037 {
2039 2038 #ifndef __lock_lint
2040 2039 _NOTE(ARGUNUSED(hba_dip))
2041 2040 _NOTE(ARGUNUSED(tgt_dip))
2042 2041 #endif
2043 2042 sata_device_t sata_device;
2044 2043 sata_drive_info_t *sdinfo;
2045 2044 struct sata_id *sid;
2046 2045 sata_hba_inst_t *sata_hba_inst;
2047 2046 char model[SATA_ID_MODEL_LEN + 1];
2048 2047 char fw[SATA_ID_FW_LEN + 1];
2049 2048 char *vid, *pid;
2050 2049
2051 2050 /*
2052 2051 * Fail tran_tgt_init for .conf stub node
2053 2052 */
2054 2053 if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
2055 2054 (void) ndi_merge_node(tgt_dip, sata_name_child);
2056 2055 ddi_set_name_addr(tgt_dip, NULL);
2057 2056 return (DDI_FAILURE);
2058 2057 }
2059 2058
2060 2059 sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2061 2060
2062 2061 /* Validate scsi device address */
2063 2062 if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2064 2063 &sata_device) != 0)
2065 2064 return (DDI_FAILURE);
2066 2065
2067 2066 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2068 2067 sata_device.satadev_addr.cport)));
2069 2068
2070 2069 /* sata_device now contains a valid sata address */
2071 2070 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2072 2071 if (sdinfo == NULL) {
2073 2072 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2074 2073 sata_device.satadev_addr.cport)));
2075 2074 return (DDI_FAILURE);
2076 2075 }
2077 2076 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2078 2077 sata_device.satadev_addr.cport)));
2079 2078
2080 2079 /*
2081 2080 * Check if we need to create a legacy devid (i.e cmdk style) for
2082 2081 * the target disks.
2083 2082 *
2084 2083 * HBA devinfo node will have the property "use-cmdk-devid-format"
2085 2084 * if we need to create cmdk-style devid for all the disk devices
2086 2085 * attached to this controller. This property may have been set
2087 2086 * from HBA driver's .conf file or by the HBA driver in its
2088 2087 * attach(9F) function.
2089 2088 */
2090 2089 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2091 2090 (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2092 2091 "use-cmdk-devid-format", 0) == 1)) {
2093 2092 /* register a legacy devid for this target node */
2094 2093 sata_target_devid_register(tgt_dip, sdinfo);
2095 2094 }
2096 2095
2097 2096
2098 2097 /*
2099 2098 * 'Identify Device Data' does not always fit in standard SCSI
2100 2099 * INQUIRY data, so establish INQUIRY_* properties with full-form
2101 2100 * of information.
2102 2101 */
2103 2102 sid = &sdinfo->satadrv_id;
2104 2103 #ifdef _LITTLE_ENDIAN
2105 2104 swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
2106 2105 swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
2107 2106 #else /* _LITTLE_ENDIAN */
2108 2107 bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN);
2109 2108 bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN);
2110 2109 #endif /* _LITTLE_ENDIAN */
2111 2110 model[SATA_ID_MODEL_LEN] = 0;
2112 2111 fw[SATA_ID_FW_LEN] = 0;
2113 2112
2114 2113 sata_split_model(model, &vid, &pid);
2115 2114
2116 2115 if (vid)
2117 2116 (void) scsi_device_prop_update_inqstring(sd, INQUIRY_VENDOR_ID,
2118 2117 vid, strlen(vid));
2119 2118 if (pid)
2120 2119 (void) scsi_device_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID,
2121 2120 pid, strlen(pid));
2122 2121 (void) scsi_device_prop_update_inqstring(sd, INQUIRY_REVISION_ID,
2123 2122 fw, strlen(fw));
2124 2123
2125 2124 return (DDI_SUCCESS);
2126 2125 }
2127 2126
2128 2127 /*
2129 2128 * Implementation of scsi tran_tgt_probe.
2130 2129 * Probe target, by calling default scsi routine scsi_hba_probe()
2131 2130 */
2132 2131 static int
2133 2132 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void))
2134 2133 {
2135 2134 sata_hba_inst_t *sata_hba_inst =
2136 2135 (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private);
2137 2136 int rval;
2138 2137 uint32_t pm_cap;
2139 2138
2140 2139 rval = scsi_hba_probe(sd, callback);
2141 2140 pm_cap = SATA_CAP_POWER_CONDITON | SATA_CAP_SMART_PAGE |
2142 2141 SATA_CAP_LOG_SENSE;
2143 2142
2144 2143 if (rval == SCSIPROBE_EXISTS) {
2145 2144 /*
2146 2145 * Set property "pm-capable" on the target device node, so that
2147 2146 * the target driver will not try to fetch scsi cycle counters
2148 2147 * before enabling device power-management.
2149 2148 */
2150 2149 if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
2151 2150 "pm-capable", pm_cap)) != DDI_PROP_SUCCESS) {
2152 2151 sata_log(sata_hba_inst, CE_WARN,
2153 2152 "SATA device at port %d: "
2154 2153 "will not be power-managed ",
2155 2154 SCSI_TO_SATA_CPORT(sd->sd_address.a_target));
2156 2155 SATA_LOG_D((sata_hba_inst, CE_WARN,
2157 2156 "failure updating pm-capable property"));
2158 2157 }
2159 2158 }
2160 2159 return (rval);
2161 2160 }
2162 2161
2163 2162 /*
2164 2163 * Implementation of scsi tran_tgt_free.
2165 2164 * Release all resources allocated for scsi_device
2166 2165 */
2167 2166 static void
2168 2167 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2169 2168 scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2170 2169 {
2171 2170 #ifndef __lock_lint
2172 2171 _NOTE(ARGUNUSED(hba_dip))
2173 2172 #endif
2174 2173 sata_device_t sata_device;
2175 2174 sata_drive_info_t *sdinfo;
2176 2175 sata_hba_inst_t *sata_hba_inst;
2177 2176 ddi_devid_t devid;
2178 2177
2179 2178 sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2180 2179
2181 2180 /* Validate scsi device address */
2182 2181 /*
2183 2182 * Note: tgt_free relates to the SCSA view of a device. If called, there
2184 2183 * was a device at this address, so even if the sata framework internal
2185 2184 * resources were alredy released because a device was detached,
2186 2185 * this function should be executed as long as its actions do
2187 2186 * not require the internal sata view of a device and the address
2188 2187 * refers to a valid sata address.
2189 2188 * Validating the address here means that we do not trust SCSA...
2190 2189 */
2191 2190 if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2192 2191 &sata_device) == -1)
2193 2192 return;
2194 2193
2195 2194 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2196 2195 sata_device.satadev_addr.cport)));
2197 2196
2198 2197 /* sata_device now should contain a valid sata address */
2199 2198 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2200 2199 if (sdinfo == NULL) {
2201 2200 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2202 2201 sata_device.satadev_addr.cport)));
2203 2202 return;
2204 2203 }
2205 2204 /*
2206 2205 * We did not allocate any resources in sata_scsi_tgt_init()
2207 2206 * other than few properties.
2208 2207 * Free them.
2209 2208 */
2210 2209 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2211 2210 sata_device.satadev_addr.cport)));
2212 2211 (void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable");
2213 2212
2214 2213 /*
2215 2214 * If devid was previously created but not freed up from
2216 2215 * sd(7D) driver (i.e during detach(9F)) then do it here.
2217 2216 */
2218 2217 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2219 2218 (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2220 2219 "use-cmdk-devid-format", 0) == 1) &&
2221 2220 (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) {
2222 2221 ddi_devid_unregister(tgt_dip);
2223 2222 ddi_devid_free(devid);
2224 2223 }
2225 2224 }
2226 2225
2227 2226 /*
2228 2227 * Implementation of scsi tran_init_pkt
2229 2228 * Upon successful return, scsi pkt buffer has DMA resources allocated.
2230 2229 *
2231 2230 * It seems that we should always allocate pkt, even if the address is
2232 2231 * for non-existing device - just use some default for dma_attr.
2233 2232 * The reason is that there is no way to communicate this to a caller here.
2234 2233 * Subsequent call to sata_scsi_start may fail appropriately.
2235 2234 * Simply returning NULL does not seem to discourage a target driver...
2236 2235 *
2237 2236 * Returns a pointer to initialized scsi_pkt, or NULL otherwise.
2238 2237 */
2239 2238 static struct scsi_pkt *
2240 2239 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
2241 2240 struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
2242 2241 int (*callback)(caddr_t), caddr_t arg)
2243 2242 {
2244 2243 sata_hba_inst_t *sata_hba_inst =
2245 2244 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2246 2245 dev_info_t *dip = SATA_DIP(sata_hba_inst);
2247 2246 sata_device_t sata_device;
2248 2247 sata_drive_info_t *sdinfo;
2249 2248 sata_pkt_txlate_t *spx;
2250 2249 ddi_dma_attr_t cur_dma_attr;
2251 2250 int rval;
2252 2251 boolean_t new_pkt = B_TRUE;
2253 2252
2254 2253 ASSERT(ap->a_hba_tran->tran_hba_dip == dip);
2255 2254
2256 2255 /*
2257 2256 * We need to translate the address, even if it could be
2258 2257 * a bogus one, for a non-existing device
2259 2258 */
2260 2259 sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
2261 2260 sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target);
2262 2261 sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2263 2262 sata_device.satadev_rev = SATA_DEVICE_REV;
2264 2263
2265 2264 if (pkt == NULL) {
2266 2265 /*
2267 2266 * Have to allocate a brand new scsi packet.
2268 2267 * We need to operate with auto request sense enabled.
2269 2268 */
2270 2269 pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen,
2271 2270 MAX(statuslen, SATA_MAX_SENSE_LEN),
2272 2271 tgtlen, sizeof (sata_pkt_txlate_t), callback, arg);
2273 2272
2274 2273 if (pkt == NULL)
2275 2274 return (NULL);
2276 2275
2277 2276 /* Fill scsi packet structure */
2278 2277 pkt->pkt_comp = (void (*)())NULL;
2279 2278 pkt->pkt_time = 0;
2280 2279 pkt->pkt_resid = 0;
2281 2280 pkt->pkt_statistics = 0;
2282 2281 pkt->pkt_reason = 0;
2283 2282
2284 2283 /*
2285 2284 * pkt_hba_private will point to sata pkt txlate structure
2286 2285 */
2287 2286 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2288 2287 bzero(spx, sizeof (sata_pkt_txlate_t));
2289 2288
2290 2289 spx->txlt_scsi_pkt = pkt;
2291 2290 spx->txlt_sata_hba_inst = sata_hba_inst;
2292 2291
2293 2292 /* Allocate sata_pkt */
2294 2293 spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback);
2295 2294 if (spx->txlt_sata_pkt == NULL) {
2296 2295 /* Could not allocate sata pkt */
2297 2296 scsi_hba_pkt_free(ap, pkt);
2298 2297 return (NULL);
2299 2298 }
2300 2299 /* Set sata address */
2301 2300 spx->txlt_sata_pkt->satapkt_device.satadev_addr =
2302 2301 sata_device.satadev_addr;
2303 2302 spx->txlt_sata_pkt->satapkt_device.satadev_rev =
2304 2303 sata_device.satadev_rev;
2305 2304
2306 2305 if ((bp == NULL) || (bp->b_bcount == 0))
2307 2306 return (pkt);
2308 2307
2309 2308 spx->txlt_total_residue = bp->b_bcount;
2310 2309 } else {
2311 2310 new_pkt = B_FALSE;
2312 2311 /*
2313 2312 * Packet was preallocated/initialized by previous call
2314 2313 */
2315 2314 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2316 2315
2317 2316 if ((bp == NULL) || (bp->b_bcount == 0)) {
2318 2317 return (pkt);
2319 2318 }
2320 2319
2321 2320 /* Pkt is available already: spx->txlt_scsi_pkt == pkt; */
2322 2321 }
2323 2322
2324 2323 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
2325 2324
2326 2325 /*
2327 2326 * We use an adjusted version of the dma_attr, to account
2328 2327 * for device addressing limitations.
2329 2328 * sata_adjust_dma_attr() will handle sdinfo == NULL which may
2330 2329 * happen when a device is not yet configured.
2331 2330 */
2332 2331 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2333 2332 sata_device.satadev_addr.cport)));
2334 2333 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2335 2334 &spx->txlt_sata_pkt->satapkt_device);
2336 2335 /* NULL sdinfo may be passsed to sata_adjust_dma_attr() */
2337 2336 sata_adjust_dma_attr(sdinfo,
2338 2337 SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
2339 2338 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2340 2339 sata_device.satadev_addr.cport)));
2341 2340 /*
2342 2341 * Allocate necessary DMA resources for the packet's data buffer
2343 2342 * NOTE:
2344 2343 * In case of read/write commands, DMA resource allocation here is
2345 2344 * based on the premise that the transfer length specified in
2346 2345 * the read/write scsi cdb will match exactly DMA resources -
2347 2346 * returning correct packet residue is crucial.
2348 2347 */
2349 2348 if ((rval = sata_dma_buf_setup(spx, flags, callback, arg,
2350 2349 &cur_dma_attr)) != DDI_SUCCESS) {
2351 2350 /*
2352 2351 * If a DMA allocation request fails with
2353 2352 * DDI_DMA_NOMAPPING, indicate the error by calling
2354 2353 * bioerror(9F) with bp and an error code of EFAULT.
2355 2354 * If a DMA allocation request fails with
2356 2355 * DDI_DMA_TOOBIG, indicate the error by calling
2357 2356 * bioerror(9F) with bp and an error code of EINVAL.
2358 2357 * For DDI_DMA_NORESOURCES, we may have some of them allocated.
2359 2358 * Request may be repeated later - there is no real error.
2360 2359 */
2361 2360 switch (rval) {
2362 2361 case DDI_DMA_NORESOURCES:
2363 2362 bioerror(bp, 0);
2364 2363 break;
2365 2364 case DDI_DMA_NOMAPPING:
2366 2365 case DDI_DMA_BADATTR:
2367 2366 bioerror(bp, EFAULT);
2368 2367 break;
2369 2368 case DDI_DMA_TOOBIG:
2370 2369 default:
2371 2370 bioerror(bp, EINVAL);
2372 2371 break;
2373 2372 }
2374 2373 goto fail;
2375 2374 }
2376 2375
2377 2376 if (sata_check_for_dma_error(dip, spx)) {
2378 2377 ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED);
2379 2378 bioerror(bp, EFAULT);
2380 2379 goto fail;
2381 2380 }
2382 2381
2383 2382 success:
2384 2383 /* Set number of bytes that are not yet accounted for */
2385 2384 pkt->pkt_resid = spx->txlt_total_residue;
2386 2385 ASSERT(pkt->pkt_resid >= 0);
2387 2386
2388 2387 return (pkt);
2389 2388
2390 2389 fail:
2391 2390 if (new_pkt == B_TRUE) {
2392 2391 /*
2393 2392 * Since this is a new packet, we can clean-up
2394 2393 * everything
2395 2394 */
2396 2395 sata_scsi_destroy_pkt(ap, pkt);
2397 2396 } else {
2398 2397 /*
2399 2398 * This is a re-used packet. It will be target driver's
2400 2399 * responsibility to eventually destroy it (which
2401 2400 * will free allocated resources).
2402 2401 * Here, we just "complete" the request, leaving
2403 2402 * allocated resources intact, so the request may
2404 2403 * be retried.
2405 2404 */
2406 2405 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2407 2406 sata_pkt_free(spx);
2408 2407 }
2409 2408 return (NULL);
2410 2409 }
2411 2410
2412 2411 /*
2413 2412 * Implementation of scsi tran_start.
2414 2413 * Translate scsi cmd into sata operation and return status.
2415 2414 * ATAPI CDBs are passed to ATAPI devices - the device determines what commands
2416 2415 * are supported.
2417 2416 * For SATA hard disks, supported scsi commands:
2418 2417 * SCMD_INQUIRY
2419 2418 * SCMD_TEST_UNIT_READY
2420 2419 * SCMD_START_STOP
2421 2420 * SCMD_READ_CAPACITY
2422 2421 * SCMD_SVC_ACTION_IN_G4 (READ CAPACITY (16))
2423 2422 * SCMD_REQUEST_SENSE
2424 2423 * SCMD_LOG_SENSE_G1
2425 2424 * SCMD_LOG_SELECT_G1
2426 2425 * SCMD_MODE_SENSE (specific pages)
2427 2426 * SCMD_MODE_SENSE_G1 (specific pages)
2428 2427 * SCMD_MODE_SELECT (specific pages)
2429 2428 * SCMD_MODE_SELECT_G1 (specific pages)
2430 2429 * SCMD_SYNCHRONIZE_CACHE
2431 2430 * SCMD_SYNCHRONIZE_CACHE_G1
2432 2431 * SCMD_READ
2433 2432 * SCMD_READ_G1
2434 2433 * SCMD_READ_G4
2435 2434 * SCMD_READ_G5
2436 2435 * SCMD_WRITE
2437 2436 * SCMD_WRITE_BUFFER
2438 2437 * SCMD_WRITE_G1
2439 2438 * SCMD_WRITE_G4
2440 2439 * SCMD_WRITE_G5
2441 2440 * SCMD_SEEK (noop)
2442 2441 * SCMD_SDIAG
2443 2442 *
2444 2443 * All other commands are rejected as unsupported.
2445 2444 *
2446 2445 * Returns:
2447 2446 * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver
2448 2447 * for execution. TRAN_ACCEPT may be returned also if device was removed but
2449 2448 * a callback could be scheduled.
2450 2449 * TRAN_BADPKT if cmd was directed to invalid address.
2451 2450 * TRAN_FATAL_ERROR is command was rejected due to hardware error, including
2452 2451 * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device
2453 2452 * was removed and there was no callback specified in scsi pkt.
2454 2453 * TRAN_BUSY if command could not be executed becasue HBA driver or SATA
2455 2454 * framework was busy performing some other operation(s).
2456 2455 *
2457 2456 */
2458 2457 static int
2459 2458 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
2460 2459 {
2461 2460 sata_hba_inst_t *sata_hba_inst =
2462 2461 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2463 2462 sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2464 2463 sata_device_t *sdevice = &spx->txlt_sata_pkt->satapkt_device;
2465 2464 sata_drive_info_t *sdinfo;
2466 2465 struct buf *bp;
2467 2466 uint8_t cport, pmport;
2468 2467 boolean_t dev_gone = B_FALSE;
2469 2468 int rval;
2470 2469
2471 2470 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2472 2471 "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]);
2473 2472
2474 2473 ASSERT(spx != NULL &&
2475 2474 spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL);
2476 2475
2477 2476 cport = SCSI_TO_SATA_CPORT(ap->a_target);
2478 2477 pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2479 2478
2480 2479 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2481 2480
2482 2481 if (sdevice->satadev_addr.qual == SATA_ADDR_DCPORT) {
2483 2482 sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2484 2483 if (sdinfo == NULL ||
2485 2484 SATA_CPORT_INFO(sata_hba_inst, cport)->
2486 2485 cport_tgtnode_clean == B_FALSE ||
2487 2486 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2488 2487 dev_gone = B_TRUE;
2489 2488 }
2490 2489 } else if (sdevice->satadev_addr.qual == SATA_ADDR_DPMPORT) {
2491 2490 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
2492 2491 SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst,
2493 2492 cport) == NULL) {
2494 2493 dev_gone = B_TRUE;
2495 2494 } else if (SATA_PMPORT_INFO(sata_hba_inst, cport,
2496 2495 pmport) == NULL) {
2497 2496 dev_gone = B_TRUE;
2498 2497 } else {
2499 2498 mutex_enter(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2500 2499 cport, pmport)));
2501 2500 sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2502 2501 if (sdinfo == NULL ||
2503 2502 SATA_PMPORT_INFO(sata_hba_inst, cport, pmport)->
2504 2503 pmport_tgtnode_clean == B_FALSE ||
2505 2504 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2506 2505 dev_gone = B_TRUE;
2507 2506 }
2508 2507 mutex_exit(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2509 2508 cport, pmport)));
2510 2509 }
2511 2510 }
2512 2511
2513 2512 if (dev_gone == B_TRUE) {
2514 2513 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2515 2514 pkt->pkt_reason = CMD_DEV_GONE;
2516 2515 /*
2517 2516 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2518 2517 * only in callback function (for normal requests) and
2519 2518 * in the dump code path.
2520 2519 * So, if the callback is available, we need to do
2521 2520 * the callback rather than returning TRAN_FATAL_ERROR here.
2522 2521 */
2523 2522 if (pkt->pkt_comp != NULL) {
2524 2523 /* scsi callback required */
2525 2524 if (servicing_interrupt()) {
2526 2525 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2527 2526 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2528 2527 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
2529 2528 NULL) {
2530 2529 return (TRAN_BUSY);
2531 2530 }
2532 2531 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2533 2532 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2534 2533 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
2535 2534 /* Scheduling the callback failed */
2536 2535 return (TRAN_BUSY);
2537 2536 }
2538 2537 return (TRAN_ACCEPT);
2539 2538 }
2540 2539 /* No callback available */
2541 2540 return (TRAN_FATAL_ERROR);
2542 2541 }
2543 2542
2544 2543 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
2545 2544 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2546 2545 rval = sata_txlt_atapi(spx);
2547 2546 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2548 2547 "sata_scsi_start atapi: rval %d\n", rval);
2549 2548 return (rval);
2550 2549 }
2551 2550 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2552 2551
2553 2552 /*
2554 2553 * Checking for power state, if it was on
2555 2554 * STOPPED state, then the drive is not capable
2556 2555 * of processing media access command. And
2557 2556 * TEST_UNIT_READY, REQUEST_SENSE has special handling
2558 2557 * in the function for different power state.
2559 2558 */
2560 2559 if (((sdinfo->satadrv_power_level == SATA_POWER_STANDBY) ||
2561 2560 (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)) &&
2562 2561 (SATA_IS_MEDIUM_ACCESS_CMD(pkt->pkt_cdbp[0]))) {
2563 2562 return (sata_txlt_check_condition(spx, KEY_NOT_READY,
2564 2563 SD_SCSI_ASC_LU_NOT_READY));
2565 2564 }
2566 2565
2567 2566 /* ATA Disk commands processing starts here */
2568 2567
2569 2568 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2570 2569
2571 2570 switch (pkt->pkt_cdbp[0]) {
2572 2571
2573 2572 case SCMD_INQUIRY:
2574 2573 /* Mapped to identify device */
2575 2574 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2576 2575 bp_mapin(bp);
2577 2576 rval = sata_txlt_inquiry(spx);
2578 2577 break;
2579 2578
2580 2579 case SCMD_TEST_UNIT_READY:
2581 2580 /*
2582 2581 * SAT "SATA to ATA Translation" doc specifies translation
2583 2582 * to ATA CHECK POWER MODE.
2584 2583 */
2585 2584 rval = sata_txlt_test_unit_ready(spx);
2586 2585 break;
2587 2586
2588 2587 case SCMD_START_STOP:
2589 2588 /* Mapping depends on the command */
2590 2589 rval = sata_txlt_start_stop_unit(spx);
2591 2590 break;
2592 2591
2593 2592 case SCMD_READ_CAPACITY:
2594 2593 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2595 2594 bp_mapin(bp);
2596 2595 rval = sata_txlt_read_capacity(spx);
2597 2596 break;
2598 2597
2599 2598 case SCMD_SVC_ACTION_IN_G4: /* READ CAPACITY (16) */
2600 2599 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2601 2600 bp_mapin(bp);
2602 2601 rval = sata_txlt_read_capacity16(spx);
2603 2602 break;
2604 2603
2605 2604 case SCMD_REQUEST_SENSE:
2606 2605 /*
2607 2606 * Always No Sense, since we force ARQ
2608 2607 */
2609 2608 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2610 2609 bp_mapin(bp);
2611 2610 rval = sata_txlt_request_sense(spx);
2612 2611 break;
2613 2612
2614 2613 case SCMD_LOG_SENSE_G1:
2615 2614 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2616 2615 bp_mapin(bp);
2617 2616 rval = sata_txlt_log_sense(spx);
2618 2617 break;
2619 2618
2620 2619 case SCMD_LOG_SELECT_G1:
2621 2620 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2622 2621 bp_mapin(bp);
2623 2622 rval = sata_txlt_log_select(spx);
2624 2623 break;
2625 2624
2626 2625 case SCMD_MODE_SENSE:
2627 2626 case SCMD_MODE_SENSE_G1:
2628 2627 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2629 2628 bp_mapin(bp);
2630 2629 rval = sata_txlt_mode_sense(spx);
2631 2630 break;
2632 2631
2633 2632
2634 2633 case SCMD_MODE_SELECT:
2635 2634 case SCMD_MODE_SELECT_G1:
2636 2635 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2637 2636 bp_mapin(bp);
2638 2637 rval = sata_txlt_mode_select(spx);
2639 2638 break;
2640 2639
2641 2640 case SCMD_SYNCHRONIZE_CACHE:
2642 2641 case SCMD_SYNCHRONIZE_CACHE_G1:
2643 2642 rval = sata_txlt_synchronize_cache(spx);
2644 2643 break;
2645 2644
2646 2645 case SCMD_READ:
2647 2646 case SCMD_READ_G1:
2648 2647 case SCMD_READ_G4:
2649 2648 case SCMD_READ_G5:
2650 2649 rval = sata_txlt_read(spx);
2651 2650 break;
2652 2651 case SCMD_WRITE_BUFFER:
2653 2652 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2654 2653 bp_mapin(bp);
2655 2654 rval = sata_txlt_write_buffer(spx);
2656 2655 break;
2657 2656
2658 2657 case SCMD_WRITE:
2659 2658 case SCMD_WRITE_G1:
2660 2659 case SCMD_WRITE_G4:
2661 2660 case SCMD_WRITE_G5:
2662 2661 rval = sata_txlt_write(spx);
2663 2662 break;
2664 2663
2665 2664 case SCMD_SEEK:
2666 2665 rval = sata_txlt_nodata_cmd_immediate(spx);
2667 2666 break;
2668 2667
2669 2668 case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12:
2670 2669 case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16:
2671 2670 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2672 2671 bp_mapin(bp);
2673 2672 rval = sata_txlt_ata_pass_thru(spx);
2674 2673 break;
2675 2674
2676 2675 /* Other cases will be filed later */
2677 2676 /* postponed until phase 2 of the development */
2678 2677 case SPC3_CMD_UNMAP:
2679 2678 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2680 2679 bp_mapin(bp);
2681 2680 rval = sata_txlt_unmap(spx);
2682 2681 break;
2683 2682 default:
2684 2683 rval = sata_txlt_invalid_command(spx);
2685 2684 break;
2686 2685 }
2687 2686
2688 2687 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2689 2688 "sata_scsi_start: rval %d\n", rval);
2690 2689
2691 2690 return (rval);
2692 2691 }
2693 2692
2694 2693 /*
2695 2694 * Implementation of scsi tran_abort.
2696 2695 * Abort specific pkt or all packets.
2697 2696 *
2698 2697 * Returns 1 if one or more packets were aborted, returns 0 otherwise
2699 2698 *
2700 2699 * May be called from an interrupt level.
2701 2700 */
2702 2701 static int
2703 2702 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt)
2704 2703 {
2705 2704 sata_hba_inst_t *sata_hba_inst =
2706 2705 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2707 2706 sata_device_t sata_device;
2708 2707 sata_pkt_t *sata_pkt;
2709 2708
2710 2709 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2711 2710 "sata_scsi_abort: %s at target: 0x%x\n",
2712 2711 scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target);
2713 2712
2714 2713 /* Validate address */
2715 2714 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0)
2716 2715 /* Invalid address */
2717 2716 return (0);
2718 2717
2719 2718 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2720 2719 sata_device.satadev_addr.cport)));
2721 2720 if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2722 2721 /* invalid address */
2723 2722 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2724 2723 sata_device.satadev_addr.cport)));
2725 2724 return (0);
2726 2725 }
2727 2726 if (scsi_pkt == NULL) {
2728 2727 /*
2729 2728 * Abort all packets.
2730 2729 * Although we do not have specific packet, we still need
2731 2730 * dummy packet structure to pass device address to HBA.
2732 2731 * Allocate one, without sleeping. Fail if pkt cannot be
2733 2732 * allocated.
2734 2733 */
2735 2734 sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP);
2736 2735 if (sata_pkt == NULL) {
2737 2736 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2738 2737 sata_device.satadev_addr.cport)));
2739 2738 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: "
2740 2739 "could not allocate sata_pkt"));
2741 2740 return (0);
2742 2741 }
2743 2742 sata_pkt->satapkt_rev = SATA_PKT_REV;
2744 2743 sata_pkt->satapkt_device = sata_device;
2745 2744 sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
2746 2745 } else {
2747 2746 if (scsi_pkt->pkt_ha_private == NULL) {
2748 2747 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2749 2748 sata_device.satadev_addr.cport)));
2750 2749 return (0); /* Bad scsi pkt */
2751 2750 }
2752 2751 /* extract pointer to sata pkt */
2753 2752 sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)->
2754 2753 txlt_sata_pkt;
2755 2754 }
2756 2755
2757 2756 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2758 2757 sata_device.satadev_addr.cport)));
2759 2758 /* Send abort request to HBA */
2760 2759 if ((*SATA_ABORT_FUNC(sata_hba_inst))
2761 2760 (SATA_DIP(sata_hba_inst), sata_pkt,
2762 2761 scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) ==
2763 2762 SATA_SUCCESS) {
2764 2763 if (scsi_pkt == NULL)
2765 2764 kmem_free(sata_pkt, sizeof (sata_pkt_t));
2766 2765 /* Success */
2767 2766 return (1);
2768 2767 }
2769 2768 /* Else, something did not go right */
2770 2769 if (scsi_pkt == NULL)
2771 2770 kmem_free(sata_pkt, sizeof (sata_pkt_t));
2772 2771 /* Failure */
2773 2772 return (0);
2774 2773 }
2775 2774
2776 2775
2777 2776 /*
2778 2777 * Implementation of scsi tran_reset.
2779 2778 * RESET_ALL request is translated into port reset.
2780 2779 * RESET_TARGET requests is translated into a device reset,
2781 2780 * RESET_LUN request is accepted only for LUN 0 and translated into
2782 2781 * device reset.
2783 2782 * The target reset should cause all HBA active and queued packets to
2784 2783 * be terminated and returned with pkt reason SATA_PKT_RESET prior to
2785 2784 * the return. HBA should report reset event for the device.
2786 2785 *
2787 2786 * Returns 1 upon success, 0 upon failure.
2788 2787 */
2789 2788 static int
2790 2789 sata_scsi_reset(struct scsi_address *ap, int level)
2791 2790 {
2792 2791 sata_hba_inst_t *sata_hba_inst =
2793 2792 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2794 2793 sata_device_t sata_device;
2795 2794 int val;
2796 2795
2797 2796 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2798 2797 "sata_scsi_reset: level %d target: 0x%x\n",
2799 2798 level, ap->a_target);
2800 2799
2801 2800 /* Validate address */
2802 2801 val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device);
2803 2802 if (val == -1)
2804 2803 /* Invalid address */
2805 2804 return (0);
2806 2805
2807 2806 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2808 2807 sata_device.satadev_addr.cport)));
2809 2808 if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2810 2809 /* invalid address */
2811 2810 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2812 2811 sata_device.satadev_addr.cport)));
2813 2812 return (0);
2814 2813 }
2815 2814 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2816 2815 sata_device.satadev_addr.cport)));
2817 2816 if (level == RESET_ALL) {
2818 2817 /* port reset */
2819 2818 if (sata_device.satadev_addr.qual == SATA_ADDR_DCPORT)
2820 2819 sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2821 2820 else
2822 2821 sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
2823 2822
2824 2823 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2825 2824 (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2826 2825 return (1);
2827 2826 else
2828 2827 return (0);
2829 2828
2830 2829 } else if (val == 0 &&
2831 2830 (level == RESET_TARGET || level == RESET_LUN)) {
2832 2831 /* reset device (device attached) */
2833 2832 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2834 2833 (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2835 2834 return (1);
2836 2835 else
2837 2836 return (0);
2838 2837 }
2839 2838 return (0);
2840 2839 }
2841 2840
2842 2841
2843 2842 /*
2844 2843 * Implementation of scsi tran_getcap (get transport/device capabilities).
2845 2844 * Supported capabilities for SATA hard disks:
2846 2845 * auto-rqsense (always supported)
2847 2846 * tagged-qing (supported if HBA supports it)
2848 2847 * untagged-qing (could be supported if disk supports it, but because
2849 2848 * caching behavior allowing untagged queuing actually
2850 2849 * results in reduced performance. sd tries to throttle
2851 2850 * back to only 3 outstanding commands, which may
2852 2851 * work for real SCSI disks, but with read ahead
2853 2852 * caching, having more than 1 outstanding command
2854 2853 * results in cache thrashing.)
2855 2854 * sector_size
2856 2855 * dma_max
2857 2856 * interconnect-type (INTERCONNECT_SATA)
2858 2857 *
2859 2858 * Supported capabilities for ATAPI CD/DVD devices:
2860 2859 * auto-rqsense (always supported)
2861 2860 * sector_size
2862 2861 * dma_max
2863 2862 * max-cdb-length
2864 2863 * interconnect-type (INTERCONNECT_SATA)
2865 2864 *
2866 2865 * Supported capabilities for ATAPI TAPE devices:
2867 2866 * auto-rqsense (always supported)
2868 2867 * dma_max
2869 2868 * max-cdb-length
2870 2869 *
2871 2870 * Supported capabilities for SATA ATAPI hard disks:
2872 2871 * auto-rqsense (always supported)
2873 2872 * interconnect-type (INTERCONNECT_SATA)
2874 2873 * max-cdb-length
2875 2874 *
2876 2875 * Request for other capabilities is rejected as unsupported.
2877 2876 *
2878 2877 * Returns supported capability value, or -1 if capability is unsuppported or
2879 2878 * the address is invalid - no device.
2880 2879 */
2881 2880
2882 2881 static int
2883 2882 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
2884 2883 {
2885 2884
2886 2885 sata_hba_inst_t *sata_hba_inst =
2887 2886 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2888 2887 sata_device_t sata_device;
2889 2888 sata_drive_info_t *sdinfo;
2890 2889 ddi_dma_attr_t adj_dma_attr;
2891 2890 int rval;
2892 2891
2893 2892 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2894 2893 "sata_scsi_getcap: target: 0x%x, cap: %s\n",
2895 2894 ap->a_target, cap);
2896 2895
2897 2896 /*
2898 2897 * We want to process the capabilities on per port granularity.
2899 2898 * So, we are specifically restricting ourselves to whom != 0
2900 2899 * to exclude the controller wide handling.
2901 2900 */
2902 2901 if (cap == NULL || whom == 0)
2903 2902 return (-1);
2904 2903
2905 2904 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2906 2905 /* Invalid address */
2907 2906 return (-1);
2908 2907 }
2909 2908 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2910 2909 sata_device.satadev_addr.cport)));
2911 2910 if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) ==
2912 2911 NULL) {
2913 2912 /* invalid address */
2914 2913 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2915 2914 sata_device.satadev_addr.cport)));
2916 2915 return (-1);
2917 2916 }
2918 2917
2919 2918 switch (scsi_hba_lookup_capstr(cap)) {
2920 2919 case SCSI_CAP_ARQ:
2921 2920 rval = 1; /* ARQ supported, turned on */
2922 2921 break;
2923 2922
2924 2923 case SCSI_CAP_SECTOR_SIZE:
2925 2924 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK)
2926 2925 rval = SATA_DISK_SECTOR_SIZE; /* fixed size */
2927 2926 else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD)
2928 2927 rval = SATA_ATAPI_SECTOR_SIZE;
2929 2928 else rval = -1;
2930 2929 break;
2931 2930
2932 2931 /*
2933 2932 * untagged queuing cause a performance inversion because of
2934 2933 * the way sd operates. Because of this reason we do not
2935 2934 * use it when available.
2936 2935 */
2937 2936 case SCSI_CAP_UNTAGGED_QING:
2938 2937 if (sdinfo->satadrv_features_enabled &
2939 2938 SATA_DEV_F_E_UNTAGGED_QING)
2940 2939 rval = 1; /* Untagged queuing available */
2941 2940 else
2942 2941 rval = -1; /* Untagged queuing not available */
2943 2942 break;
2944 2943
2945 2944 case SCSI_CAP_TAGGED_QING:
2946 2945 if ((sdinfo->satadrv_features_enabled &
2947 2946 SATA_DEV_F_E_TAGGED_QING) &&
2948 2947 (sdinfo->satadrv_max_queue_depth > 1))
2949 2948 rval = 1; /* Tagged queuing available */
2950 2949 else
2951 2950 rval = -1; /* Tagged queuing not available */
2952 2951 break;
2953 2952
2954 2953 case SCSI_CAP_DMA_MAX:
2955 2954 sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst),
2956 2955 &adj_dma_attr);
2957 2956 rval = (int)adj_dma_attr.dma_attr_maxxfer;
2958 2957 /* We rely on the fact that dma_attr_maxxfer < 0x80000000 */
2959 2958 break;
2960 2959
2961 2960 case SCSI_CAP_INTERCONNECT_TYPE:
2962 2961 rval = INTERCONNECT_SATA; /* SATA interconnect type */
2963 2962 break;
2964 2963
2965 2964 case SCSI_CAP_CDB_LEN:
2966 2965 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI)
2967 2966 rval = sdinfo->satadrv_atapi_cdb_len;
2968 2967 else
2969 2968 rval = -1;
2970 2969 break;
2971 2970
2972 2971 default:
2973 2972 rval = -1;
2974 2973 break;
2975 2974 }
2976 2975 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2977 2976 sata_device.satadev_addr.cport)));
2978 2977 return (rval);
2979 2978 }
2980 2979
2981 2980 /*
2982 2981 * Implementation of scsi tran_setcap
2983 2982 *
2984 2983 * Only SCSI_CAP_UNTAGGED_QING and SCSI_CAP_TAGGED_QING are changeable.
2985 2984 *
2986 2985 */
2987 2986 static int
2988 2987 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
2989 2988 {
2990 2989 sata_hba_inst_t *sata_hba_inst =
2991 2990 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2992 2991 sata_device_t sata_device;
2993 2992 sata_drive_info_t *sdinfo;
2994 2993 int rval;
2995 2994
2996 2995 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2997 2996 "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap);
2998 2997
2999 2998 /*
3000 2999 * We want to process the capabilities on per port granularity.
3001 3000 * So, we are specifically restricting ourselves to whom != 0
3002 3001 * to exclude the controller wide handling.
3003 3002 */
3004 3003 if (cap == NULL || whom == 0) {
3005 3004 return (-1);
3006 3005 }
3007 3006
3008 3007 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
3009 3008 /* Invalid address */
3010 3009 return (-1);
3011 3010 }
3012 3011 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
3013 3012 sata_device.satadev_addr.cport)));
3014 3013 if ((sdinfo = sata_get_device_info(sata_hba_inst,
3015 3014 &sata_device)) == NULL) {
3016 3015 /* invalid address */
3017 3016 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3018 3017 sata_device.satadev_addr.cport)));
3019 3018 return (-1);
3020 3019 }
3021 3020 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3022 3021 sata_device.satadev_addr.cport)));
3023 3022
3024 3023 switch (scsi_hba_lookup_capstr(cap)) {
3025 3024 case SCSI_CAP_ARQ:
3026 3025 case SCSI_CAP_SECTOR_SIZE:
3027 3026 case SCSI_CAP_DMA_MAX:
3028 3027 case SCSI_CAP_INTERCONNECT_TYPE:
3029 3028 rval = 0;
3030 3029 break;
3031 3030 case SCSI_CAP_UNTAGGED_QING:
3032 3031 if (SATA_QDEPTH(sata_hba_inst) > 1) {
3033 3032 rval = 1;
3034 3033 if (value == 1) {
3035 3034 sdinfo->satadrv_features_enabled |=
3036 3035 SATA_DEV_F_E_UNTAGGED_QING;
3037 3036 } else if (value == 0) {
3038 3037 sdinfo->satadrv_features_enabled &=
3039 3038 ~SATA_DEV_F_E_UNTAGGED_QING;
3040 3039 } else {
3041 3040 rval = -1;
3042 3041 }
3043 3042 } else {
3044 3043 rval = 0;
3045 3044 }
3046 3045 break;
3047 3046 case SCSI_CAP_TAGGED_QING:
3048 3047 /* This can TCQ or NCQ */
3049 3048 if (sata_func_enable & SATA_ENABLE_QUEUING &&
3050 3049 ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ &&
3051 3050 SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) ||
3052 3051 (sata_func_enable & SATA_ENABLE_NCQ &&
3053 3052 sdinfo->satadrv_features_support & SATA_DEV_F_NCQ &&
3054 3053 SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) &&
3055 3054 (sdinfo->satadrv_max_queue_depth > 1)) {
3056 3055 rval = 1;
3057 3056 if (value == 1) {
3058 3057 sdinfo->satadrv_features_enabled |=
3059 3058 SATA_DEV_F_E_TAGGED_QING;
3060 3059 } else if (value == 0) {
3061 3060 sdinfo->satadrv_features_enabled &=
3062 3061 ~SATA_DEV_F_E_TAGGED_QING;
3063 3062 } else {
3064 3063 rval = -1;
3065 3064 }
3066 3065 } else {
3067 3066 rval = 0;
3068 3067 }
3069 3068 break;
3070 3069 default:
3071 3070 rval = -1;
3072 3071 break;
3073 3072 }
3074 3073 return (rval);
3075 3074 }
3076 3075
3077 3076 /*
3078 3077 * Implementations of scsi tran_destroy_pkt.
3079 3078 * Free resources allocated by sata_scsi_init_pkt()
3080 3079 */
3081 3080 static void
3082 3081 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3083 3082 {
3084 3083 sata_pkt_txlate_t *spx;
3085 3084
3086 3085 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3087 3086
3088 3087 sata_common_free_dma_rsrcs(spx);
3089 3088
3090 3089 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
3091 3090 sata_pkt_free(spx);
3092 3091
3093 3092 scsi_hba_pkt_free(ap, pkt);
3094 3093 }
3095 3094
3096 3095 /*
3097 3096 * Implementation of scsi tran_dmafree.
3098 3097 * Free DMA resources allocated by sata_scsi_init_pkt()
3099 3098 */
3100 3099
3101 3100 static void
3102 3101 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
3103 3102 {
3104 3103 #ifndef __lock_lint
3105 3104 _NOTE(ARGUNUSED(ap))
3106 3105 #endif
3107 3106 sata_pkt_txlate_t *spx;
3108 3107
3109 3108 ASSERT(pkt != NULL);
3110 3109 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3111 3110
3112 3111 sata_common_free_dma_rsrcs(spx);
3113 3112 }
3114 3113
3115 3114 /*
3116 3115 * Implementation of scsi tran_sync_pkt.
3117 3116 *
3118 3117 * The assumption below is that pkt is unique - there is no need to check ap
3119 3118 *
3120 3119 * Synchronize DMA buffer and, if the intermediate buffer is used, copy data
3121 3120 * into/from the real buffer.
3122 3121 */
3123 3122 static void
3124 3123 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3125 3124 {
3126 3125 #ifndef __lock_lint
3127 3126 _NOTE(ARGUNUSED(ap))
3128 3127 #endif
3129 3128 int rval;
3130 3129 sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3131 3130 struct buf *bp;
3132 3131 int direction;
3133 3132
3134 3133 ASSERT(spx != NULL);
3135 3134 if (spx->txlt_buf_dma_handle != NULL) {
3136 3135 direction = spx->txlt_sata_pkt->
3137 3136 satapkt_cmd.satacmd_flags.sata_data_direction;
3138 3137 if (spx->txlt_sata_pkt != NULL &&
3139 3138 direction != SATA_DIR_NODATA_XFER) {
3140 3139 if (spx->txlt_tmp_buf != NULL) {
3141 3140 /* Intermediate DMA buffer used */
3142 3141 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3143 3142
3144 3143 if (direction & SATA_DIR_WRITE) {
3145 3144 bcopy(bp->b_un.b_addr,
3146 3145 spx->txlt_tmp_buf, bp->b_bcount);
3147 3146 }
3148 3147 }
3149 3148 /* Sync the buffer for device or for CPU */
3150 3149 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
3151 3150 (direction & SATA_DIR_WRITE) ?
3152 3151 DDI_DMA_SYNC_FORDEV : DDI_DMA_SYNC_FORCPU);
3153 3152 ASSERT(rval == DDI_SUCCESS);
3154 3153 if (spx->txlt_tmp_buf != NULL &&
3155 3154 !(direction & SATA_DIR_WRITE)) {
3156 3155 /* Intermediate DMA buffer used for read */
3157 3156 bcopy(spx->txlt_tmp_buf,
3158 3157 bp->b_un.b_addr, bp->b_bcount);
3159 3158 }
3160 3159
3161 3160 }
3162 3161 }
3163 3162 }
3164 3163
3165 3164
3166 3165
3167 3166 /* ******************* SATA - SCSI Translation functions **************** */
3168 3167 /*
3169 3168 * SCSI to SATA pkt and command translation and SATA to SCSI status/error
3170 3169 * translation.
3171 3170 */
3172 3171
3173 3172 /*
3174 3173 * Checks if a device exists and can be access and translates common
3175 3174 * scsi_pkt data to sata_pkt data.
3176 3175 *
3177 3176 * Flag argument indicates that a non-read/write ATA command may be sent
3178 3177 * to HBA in arbitrary SYNC mode to execute this packet.
3179 3178 *
3180 3179 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and
3181 3180 * sata_pkt was set-up.
3182 3181 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not
3183 3182 * exist and pkt_comp callback was scheduled.
3184 3183 * Returns other TRAN_XXXXX values when error occured and command should be
3185 3184 * rejected with the returned TRAN_XXXXX value.
3186 3185 *
3187 3186 * This function should be called with port mutex held.
3188 3187 */
3189 3188 static int
3190 3189 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason, int flag)
3191 3190 {
3192 3191 sata_drive_info_t *sdinfo;
3193 3192 sata_device_t sata_device;
3194 3193 const struct sata_cmd_flags sata_initial_cmd_flags = {
3195 3194 SATA_DIR_NODATA_XFER,
3196 3195 /* all other values to 0/FALSE */
3197 3196 };
3198 3197 /*
3199 3198 * Pkt_reason has to be set if the pkt_comp callback is invoked,
3200 3199 * and that implies TRAN_ACCEPT return value. Any other returned value
3201 3200 * indicates that the scsi packet was not accepted (the reason will not
3202 3201 * be checked by the scsi target driver).
3203 3202 * To make debugging easier, we set pkt_reason to know value here.
3204 3203 * It may be changed later when different completion reason is
3205 3204 * determined.
3206 3205 */
3207 3206 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
3208 3207 *reason = CMD_TRAN_ERR;
3209 3208
3210 3209 /* Validate address */
3211 3210 switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst,
3212 3211 &spx->txlt_scsi_pkt->pkt_address, &sata_device)) {
3213 3212
3214 3213 case -1:
3215 3214 /* Invalid address or invalid device type */
3216 3215 return (TRAN_BADPKT);
3217 3216 case 2:
3218 3217 /*
3219 3218 * Valid address but device type is unknown - Chack if it is
3220 3219 * in the reset state and therefore in an indeterminate state.
3221 3220 */
3222 3221 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3223 3222 &spx->txlt_sata_pkt->satapkt_device);
3224 3223 if (sdinfo != NULL && (sdinfo->satadrv_event_flags &
3225 3224 (SATA_EVNT_DEVICE_RESET |
3226 3225 SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3227 3226 if (!ddi_in_panic()) {
3228 3227 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3229 3228 *reason = CMD_INCOMPLETE;
3230 3229 SATADBG1(SATA_DBG_SCSI_IF,
3231 3230 spx->txlt_sata_hba_inst,
3232 3231 "sata_scsi_start: rejecting command "
3233 3232 "because of device reset state\n", NULL);
3234 3233 return (TRAN_BUSY);
3235 3234 }
3236 3235 }
3237 3236 /* FALLTHROUGH */
3238 3237 case 1:
3239 3238 /* valid address but no valid device - it has disappeared */
3240 3239 spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE;
3241 3240 *reason = CMD_DEV_GONE;
3242 3241 /*
3243 3242 * The sd target driver is checking CMD_DEV_GONE pkt_reason
3244 3243 * only in callback function (for normal requests) and
3245 3244 * in the dump code path.
3246 3245 * So, if the callback is available, we need to do
3247 3246 * the callback rather than returning TRAN_FATAL_ERROR here.
3248 3247 */
3249 3248 if (spx->txlt_scsi_pkt->pkt_comp != NULL) {
3250 3249 /* scsi callback required */
3251 3250 if (servicing_interrupt()) {
3252 3251 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3253 3252 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3254 3253 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3255 3254 NULL) {
3256 3255 return (TRAN_BUSY);
3257 3256 }
3258 3257 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3259 3258 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3260 3259 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3261 3260 /* Scheduling the callback failed */
3262 3261 return (TRAN_BUSY);
3263 3262 }
3264 3263
3265 3264 return (TRAN_ACCEPT);
3266 3265 }
3267 3266 return (TRAN_FATAL_ERROR);
3268 3267 default:
3269 3268 /* all OK; pkt reason will be overwritten later */
3270 3269 break;
3271 3270 }
3272 3271 /*
3273 3272 * If pkt is to be executed in polling mode and a command will not be
3274 3273 * emulated in SATA module (requires sending a non-read/write ATA
3275 3274 * command to HBA driver in arbitrary SYNC mode) and we are in the
3276 3275 * interrupt context and not in the panic dump, then reject the packet
3277 3276 * to avoid a possible interrupt stack overrun or hang caused by
3278 3277 * a potentially blocked interrupt.
3279 3278 */
3280 3279 if (((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0 || flag != 0) &&
3281 3280 servicing_interrupt() && !ddi_in_panic()) {
3282 3281 SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
3283 3282 "sata_scsi_start: rejecting synchronous command because "
3284 3283 "of interrupt context\n", NULL);
3285 3284 return (TRAN_BUSY);
3286 3285 }
3287 3286
3288 3287 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3289 3288 &spx->txlt_sata_pkt->satapkt_device);
3290 3289
3291 3290 /*
3292 3291 * If device is in reset condition, reject the packet with
3293 3292 * TRAN_BUSY, unless:
3294 3293 * 1. system is panicking (dumping)
3295 3294 * In such case only one thread is running and there is no way to
3296 3295 * process reset.
3297 3296 * 2. cfgadm operation is is progress (internal APCTL lock is set)
3298 3297 * Some cfgadm operations involve drive commands, so reset condition
3299 3298 * needs to be ignored for IOCTL operations.
3300 3299 */
3301 3300 if ((sdinfo->satadrv_event_flags &
3302 3301 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3303 3302
3304 3303 if (!ddi_in_panic() &&
3305 3304 ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst,
3306 3305 sata_device.satadev_addr.cport) &
3307 3306 SATA_APCTL_LOCK_PORT_BUSY) == 0)) {
3308 3307 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3309 3308 *reason = CMD_INCOMPLETE;
3310 3309 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3311 3310 "sata_scsi_start: rejecting command because "
3312 3311 "of device reset state\n", NULL);
3313 3312 return (TRAN_BUSY);
3314 3313 }
3315 3314 }
3316 3315
3317 3316 /*
3318 3317 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by
3319 3318 * sata_scsi_pkt_init() because pkt init had to work also with
3320 3319 * non-existing devices.
3321 3320 * Now we know that the packet was set-up for a real device, so its
3322 3321 * type is known.
3323 3322 */
3324 3323 spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type;
3325 3324
3326 3325 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags;
3327 3326 if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst,
3328 3327 sata_device.satadev_addr.cport)->cport_event_flags &
3329 3328 SATA_APCTL_LOCK_PORT_BUSY) != 0) {
3330 3329 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3331 3330 sata_ignore_dev_reset = B_TRUE;
3332 3331 }
3333 3332 /*
3334 3333 * At this point the generic translation routine determined that the
3335 3334 * scsi packet should be accepted. Packet completion reason may be
3336 3335 * changed later when a different completion reason is determined.
3337 3336 */
3338 3337 spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3339 3338 *reason = CMD_CMPLT;
3340 3339
3341 3340 if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
3342 3341 /* Synchronous execution */
3343 3342 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH |
3344 3343 SATA_OPMODE_POLLING;
3345 3344 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3346 3345 sata_ignore_dev_reset = ddi_in_panic();
3347 3346 } else {
3348 3347 /* Asynchronous execution */
3349 3348 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH |
3350 3349 SATA_OPMODE_INTERRUPTS;
3351 3350 }
3352 3351 /* Convert queuing information */
3353 3352 if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG)
3354 3353 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag =
3355 3354 B_TRUE;
3356 3355 else if (spx->txlt_scsi_pkt->pkt_flags &
3357 3356 (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD))
3358 3357 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag =
3359 3358 B_TRUE;
3360 3359
3361 3360 /* Always limit pkt time */
3362 3361 if (spx->txlt_scsi_pkt->pkt_time == 0)
3363 3362 spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time;
3364 3363 else
3365 3364 /* Pass on scsi_pkt time */
3366 3365 spx->txlt_sata_pkt->satapkt_time =
3367 3366 spx->txlt_scsi_pkt->pkt_time;
3368 3367
3369 3368 return (TRAN_ACCEPT);
3370 3369 }
3371 3370
3372 3371
3373 3372 /*
3374 3373 * Translate ATA Identify Device data to SCSI Inquiry data.
3375 3374 * This function may be called only for ATA devices.
3376 3375 * This function should not be called for ATAPI devices - they
3377 3376 * respond directly to SCSI Inquiry command.
3378 3377 *
3379 3378 * SATA Identify Device data has to be valid in sata_drive_info.
3380 3379 * Buffer has to accomodate the inquiry length (36 bytes).
3381 3380 *
3382 3381 * This function should be called with a port mutex held.
3383 3382 */
3384 3383 static void
3385 3384 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst,
3386 3385 sata_drive_info_t *sdinfo, uint8_t *buf)
3387 3386 {
3388 3387
3389 3388 struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
3390 3389 struct sata_id *sid = &sdinfo->satadrv_id;
3391 3390
3392 3391 /* Start with a nice clean slate */
3393 3392 bzero((void *)inq, sizeof (struct scsi_inquiry));
3394 3393
3395 3394 /*
3396 3395 * Rely on the dev_type for setting paripheral qualifier.
3397 3396 * Assume that DTYPE_RODIRECT applies to CD/DVD R/W devices.
3398 3397 * It could be that DTYPE_OPTICAL could also qualify in the future.
3399 3398 * ATAPI Inquiry may provide more data to the target driver.
3400 3399 */
3401 3400 inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3402 3401 DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */
3403 3402
3404 3403 /* CFA type device is not a removable media device */
3405 3404 inq->inq_rmb = ((sid->ai_config != SATA_CFA_TYPE) &&
3406 3405 (sid->ai_config & SATA_REM_MEDIA)) ? 1 : 0;
3407 3406 inq->inq_qual = 0; /* Device type qualifier (obsolete in SCSI3? */
3408 3407 inq->inq_iso = 0; /* ISO version */
3409 3408 inq->inq_ecma = 0; /* ECMA version */
3410 3409 inq->inq_ansi = 3; /* ANSI version - SCSI 3 */
3411 3410 inq->inq_aenc = 0; /* Async event notification cap. */
3412 3411 inq->inq_trmiop = 0; /* Supports TERMINATE I/O PROC msg - NO */
3413 3412 inq->inq_normaca = 0; /* setting NACA bit supported - NO */
3414 3413 inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */
3415 3414 inq->inq_len = 31; /* Additional length */
3416 3415 inq->inq_dualp = 0; /* dual port device - NO */
3417 3416 inq->inq_reladdr = 0; /* Supports relative addressing - NO */
3418 3417 inq->inq_sync = 0; /* Supports synchronous data xfers - NO */
3419 3418 inq->inq_linked = 0; /* Supports linked commands - NO */
3420 3419 /*
3421 3420 * Queuing support - controller has to
3422 3421 * support some sort of command queuing.
3423 3422 */
3424 3423 if (SATA_QDEPTH(sata_hba_inst) > 1)
3425 3424 inq->inq_cmdque = 1; /* Supports command queueing - YES */
3426 3425 else
3427 3426 inq->inq_cmdque = 0; /* Supports command queueing - NO */
3428 3427 inq->inq_sftre = 0; /* Supports Soft Reset option - NO ??? */
3429 3428 inq->inq_wbus32 = 0; /* Supports 32 bit wide data xfers - NO */
3430 3429 inq->inq_wbus16 = 0; /* Supports 16 bit wide data xfers - NO */
3431 3430
3432 3431 #ifdef _LITTLE_ENDIAN
3433 3432 /* Swap text fields to match SCSI format */
3434 3433 bcopy("ATA ", inq->inq_vid, 8); /* Vendor ID */
3435 3434 swab(sid->ai_model, inq->inq_pid, 16); /* Product ID */
3436 3435 if (strncmp(&sid->ai_fw[4], " ", 4) == 0)
3437 3436 swab(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
3438 3437 else
3439 3438 swab(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
3440 3439 #else /* _LITTLE_ENDIAN */
3441 3440 bcopy("ATA ", inq->inq_vid, 8); /* Vendor ID */
3442 3441 bcopy(sid->ai_model, inq->inq_pid, 16); /* Product ID */
3443 3442 if (strncmp(&sid->ai_fw[4], " ", 4) == 0)
3444 3443 bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
3445 3444 else
3446 3445 bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
3447 3446 #endif /* _LITTLE_ENDIAN */
3448 3447 }
3449 3448
3450 3449
3451 3450 /*
3452 3451 * Scsi response set up for invalid command (command not supported)
3453 3452 *
3454 3453 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3455 3454 */
3456 3455 static int
3457 3456 sata_txlt_invalid_command(sata_pkt_txlate_t *spx)
3458 3457 {
3459 3458 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3460 3459 struct scsi_extended_sense *sense;
3461 3460
3462 3461 scsipkt->pkt_reason = CMD_CMPLT;
3463 3462 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3464 3463 STATE_SENT_CMD | STATE_GOT_STATUS;
3465 3464
3466 3465 *scsipkt->pkt_scbp = STATUS_CHECK;
3467 3466
3468 3467 sense = sata_arq_sense(spx);
3469 3468 sense->es_key = KEY_ILLEGAL_REQUEST;
3470 3469 sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE;
3471 3470
3472 3471 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3473 3472 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3474 3473
3475 3474 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3476 3475 scsipkt->pkt_comp != NULL) {
3477 3476 /* scsi callback required */
3478 3477 if (servicing_interrupt()) {
3479 3478 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3480 3479 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3481 3480 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3482 3481 return (TRAN_BUSY);
3483 3482 }
3484 3483 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3485 3484 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3486 3485 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3487 3486 /* Scheduling the callback failed */
3488 3487 return (TRAN_BUSY);
3489 3488 }
3490 3489 }
3491 3490 return (TRAN_ACCEPT);
3492 3491 }
3493 3492
3494 3493 /*
3495 3494 * Scsi response set up for check condition with special sense key
3496 3495 * and additional sense code.
3497 3496 *
3498 3497 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3499 3498 */
3500 3499 static int
3501 3500 sata_txlt_check_condition(sata_pkt_txlate_t *spx, uchar_t key, uchar_t code)
3502 3501 {
3503 3502 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3504 3503 int cport = SATA_TXLT_CPORT(spx);
3505 3504 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3506 3505 struct scsi_extended_sense *sense;
3507 3506
3508 3507 mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3509 3508 scsipkt->pkt_reason = CMD_CMPLT;
3510 3509 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3511 3510 STATE_SENT_CMD | STATE_GOT_STATUS;
3512 3511
3513 3512 *scsipkt->pkt_scbp = STATUS_CHECK;
3514 3513
3515 3514 sense = sata_arq_sense(spx);
3516 3515 sense->es_key = key;
3517 3516 sense->es_add_code = code;
3518 3517
3519 3518 mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3520 3519
3521 3520 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3522 3521 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3523 3522
3524 3523 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3525 3524 scsipkt->pkt_comp != NULL) {
3526 3525 /* scsi callback required */
3527 3526 if (servicing_interrupt()) {
3528 3527 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3529 3528 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3530 3529 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3531 3530 return (TRAN_BUSY);
3532 3531 }
3533 3532 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3534 3533 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3535 3534 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3536 3535 /* Scheduling the callback failed */
3537 3536 return (TRAN_BUSY);
3538 3537 }
3539 3538 }
3540 3539 return (TRAN_ACCEPT);
3541 3540 }
3542 3541
3543 3542 /*
3544 3543 * Scsi response setup for
3545 3544 * emulated non-data command that requires no action/return data
3546 3545 *
3547 3546 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3548 3547 */
3549 3548 static int
3550 3549 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx)
3551 3550 {
3552 3551 int rval;
3553 3552 int reason;
3554 3553 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
3555 3554
3556 3555 mutex_enter(cport_mutex);
3557 3556
3558 3557 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
3559 3558 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3560 3559 mutex_exit(cport_mutex);
3561 3560 return (rval);
3562 3561 }
3563 3562 mutex_exit(cport_mutex);
3564 3563
3565 3564 spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3566 3565 STATE_SENT_CMD | STATE_GOT_STATUS;
3567 3566 spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3568 3567 *(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD;
3569 3568
3570 3569 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3571 3570 "Scsi_pkt completion reason %x\n",
3572 3571 spx->txlt_scsi_pkt->pkt_reason);
3573 3572
3574 3573 if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 &&
3575 3574 spx->txlt_scsi_pkt->pkt_comp != NULL) {
3576 3575 /* scsi callback required */
3577 3576 if (servicing_interrupt()) {
3578 3577 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3579 3578 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3580 3579 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3581 3580 return (TRAN_BUSY);
3582 3581 }
3583 3582 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3584 3583 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3585 3584 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3586 3585 /* Scheduling the callback failed */
3587 3586 return (TRAN_BUSY);
3588 3587 }
3589 3588 }
3590 3589 return (TRAN_ACCEPT);
3591 3590 }
3592 3591
3593 3592
3594 3593 /*
3595 3594 * SATA translate command: Inquiry / Identify Device
3596 3595 * Use cached Identify Device data for now, rather than issuing actual
3597 3596 * Device Identify cmd request. If device is detached and re-attached,
3598 3597 * asynchronous event processing should fetch and refresh Identify Device
3599 3598 * data.
3600 3599 * VPD pages supported now:
3601 3600 * Vital Product Data page
3602 3601 * Unit Serial Number page
3603 3602 * Block Device Characteristics Page
3604 3603 * ATA Information Page
3605 3604 *
3606 3605 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3607 3606 */
3608 3607
3609 3608 #define EVPD 1 /* Extended Vital Product Data flag */
3610 3609 #define CMDDT 2 /* Command Support Data - Obsolete */
3611 3610 #define INQUIRY_SUP_VPD_PAGE 0 /* Supported VPD Pages Page Code */
3612 3611 #define INQUIRY_USN_PAGE 0x80 /* Unit Serial Number Page Code */
3613 3612 #define INQUIRY_BDC_PAGE 0xB1 /* Block Device Characteristics Page */
3614 3613 /* Code */
3615 3614 #define INQUIRY_ATA_INFO_PAGE 0x89 /* ATA Information Page Code */
3616 3615 #define INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */
3617 3616
3618 3617 static int
3619 3618 sata_txlt_inquiry(sata_pkt_txlate_t *spx)
3620 3619 {
3621 3620 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3622 3621 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3623 3622 sata_drive_info_t *sdinfo;
3624 3623 struct scsi_extended_sense *sense;
3625 3624 int count;
3626 3625 uint8_t *p;
3627 3626 int i, j;
3628 3627 uint8_t page_buf[1024]; /* Max length */
3629 3628 int rval, reason;
3630 3629 ushort_t rate;
3631 3630 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
3632 3631
3633 3632 mutex_enter(cport_mutex);
3634 3633
3635 3634 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
3636 3635 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3637 3636 mutex_exit(cport_mutex);
3638 3637 return (rval);
3639 3638 }
3640 3639
3641 3640 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3642 3641 &spx->txlt_sata_pkt->satapkt_device);
3643 3642
3644 3643 ASSERT(sdinfo != NULL);
3645 3644
3646 3645 scsipkt->pkt_reason = CMD_CMPLT;
3647 3646 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3648 3647 STATE_SENT_CMD | STATE_GOT_STATUS;
3649 3648
3650 3649 /* Reject not supported request */
3651 3650 if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */
3652 3651 *scsipkt->pkt_scbp = STATUS_CHECK;
3653 3652 sense = sata_arq_sense(spx);
3654 3653 sense->es_key = KEY_ILLEGAL_REQUEST;
3655 3654 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3656 3655 goto done;
3657 3656 }
3658 3657
3659 3658 /* Valid Inquiry request */
3660 3659 *scsipkt->pkt_scbp = STATUS_GOOD;
3661 3660
3662 3661 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3663 3662
3664 3663 /*
3665 3664 * Because it is fully emulated command storing data
3666 3665 * programatically in the specified buffer, release
3667 3666 * preallocated DMA resources before storing data in the buffer,
3668 3667 * so no unwanted DMA sync would take place.
3669 3668 */
3670 3669 sata_scsi_dmafree(NULL, scsipkt);
3671 3670
3672 3671 if (!(scsipkt->pkt_cdbp[1] & EVPD)) {
3673 3672 /* Standard Inquiry Data request */
3674 3673 struct scsi_inquiry inq;
3675 3674 unsigned int bufsize;
3676 3675
3677 3676 sata_identdev_to_inquiry(spx->txlt_sata_hba_inst,
3678 3677 sdinfo, (uint8_t *)&inq);
3679 3678 /* Copy no more than requested */
3680 3679 count = MIN(bp->b_bcount,
3681 3680 sizeof (struct scsi_inquiry));
3682 3681 bufsize = scsipkt->pkt_cdbp[4];
3683 3682 bufsize |= scsipkt->pkt_cdbp[3] << 8;
3684 3683 count = MIN(count, bufsize);
3685 3684 bcopy(&inq, bp->b_un.b_addr, count);
3686 3685
3687 3686 scsipkt->pkt_state |= STATE_XFERRED_DATA;
3688 3687 scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3689 3688 bufsize - count : 0;
3690 3689 } else {
3691 3690 /*
3692 3691 * peripheral_qualifier = 0;
3693 3692 *
3694 3693 * We are dealing only with HD and will be
3695 3694 * dealing with CD/DVD devices soon
3696 3695 */
3697 3696 uint8_t peripheral_device_type =
3698 3697 sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3699 3698 DTYPE_DIRECT : DTYPE_RODIRECT;
3700 3699
3701 3700 bzero(page_buf, sizeof (page_buf));
3702 3701
3703 3702 switch ((uint_t)scsipkt->pkt_cdbp[2]) {
3704 3703 case INQUIRY_SUP_VPD_PAGE:
3705 3704 /*
3706 3705 * Request for supported Vital Product Data
3707 3706 * pages.
3708 3707 */
3709 3708 page_buf[0] = peripheral_device_type;
3710 3709 page_buf[1] = INQUIRY_SUP_VPD_PAGE;
3711 3710 page_buf[2] = 0;
3712 3711 page_buf[3] = 4; /* page length */
3713 3712 page_buf[4] = INQUIRY_SUP_VPD_PAGE;
3714 3713 page_buf[5] = INQUIRY_USN_PAGE;
3715 3714 page_buf[6] = INQUIRY_BDC_PAGE;
3716 3715 page_buf[7] = INQUIRY_ATA_INFO_PAGE;
3717 3716 /* Copy no more than requested */
3718 3717 count = MIN(bp->b_bcount, 8);
3719 3718 bcopy(page_buf, bp->b_un.b_addr, count);
3720 3719 break;
3721 3720
3722 3721 case INQUIRY_USN_PAGE:
3723 3722 /*
3724 3723 * Request for Unit Serial Number page.
3725 3724 * Set-up the page.
3726 3725 */
3727 3726 page_buf[0] = peripheral_device_type;
3728 3727 page_buf[1] = INQUIRY_USN_PAGE;
3729 3728 page_buf[2] = 0;
3730 3729 /* remaining page length */
3731 3730 page_buf[3] = SATA_ID_SERIAL_LEN;
3732 3731
3733 3732 /*
3734 3733 * Copy serial number from Identify Device data
3735 3734 * words into the inquiry page and swap bytes
3736 3735 * when necessary.
3737 3736 */
3738 3737 p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser);
3739 3738 #ifdef _LITTLE_ENDIAN
3740 3739 swab(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3741 3740 #else
3742 3741 bcopy(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3743 3742 #endif
3744 3743 /*
3745 3744 * Least significant character of the serial
3746 3745 * number shall appear as the last byte,
3747 3746 * according to SBC-3 spec.
3748 3747 * Count trailing spaces to determine the
3749 3748 * necessary shift length.
3750 3749 */
3751 3750 p = &page_buf[SATA_ID_SERIAL_LEN + 4 - 1];
3752 3751 for (j = 0; j < SATA_ID_SERIAL_LEN; j++) {
3753 3752 if (*(p - j) != '\0' &&
3754 3753 *(p - j) != '\040')
3755 3754 break;
3756 3755 }
3757 3756
3758 3757 /*
3759 3758 * Shift SN string right, so that the last
3760 3759 * non-blank character would appear in last
3761 3760 * byte of SN field in the page.
3762 3761 * 'j' is the shift length.
3763 3762 */
3764 3763 for (i = 0;
3765 3764 i < (SATA_ID_SERIAL_LEN - j) && j != 0;
3766 3765 i++, p--)
3767 3766 *p = *(p - j);
3768 3767
3769 3768 /*
3770 3769 * Add leading spaces - same number as the
3771 3770 * shift size
3772 3771 */
3773 3772 for (; j > 0; j--)
3774 3773 page_buf[4 + j - 1] = '\040';
3775 3774
3776 3775 count = MIN(bp->b_bcount,
3777 3776 SATA_ID_SERIAL_LEN + 4);
3778 3777 bcopy(page_buf, bp->b_un.b_addr, count);
3779 3778 break;
3780 3779
3781 3780 case INQUIRY_BDC_PAGE:
3782 3781 /*
3783 3782 * Request for Block Device Characteristics
3784 3783 * page. Set-up the page.
3785 3784 */
3786 3785 page_buf[0] = peripheral_device_type;
3787 3786 page_buf[1] = INQUIRY_BDC_PAGE;
3788 3787 page_buf[2] = 0;
3789 3788 /* remaining page length */
3790 3789 page_buf[3] = SATA_ID_BDC_LEN;
3791 3790
3792 3791 rate = sdinfo->satadrv_id.ai_medrotrate;
3793 3792 page_buf[4] = (rate >> 8) & 0xff;
3794 3793 page_buf[5] = rate & 0xff;
3795 3794 page_buf[6] = 0;
3796 3795 page_buf[7] = sdinfo->satadrv_id.
3797 3796 ai_nomformfactor & 0xf;
3798 3797
3799 3798 count = MIN(bp->b_bcount,
3800 3799 SATA_ID_BDC_LEN + 4);
3801 3800 bcopy(page_buf, bp->b_un.b_addr, count);
3802 3801 break;
3803 3802
3804 3803 case INQUIRY_ATA_INFO_PAGE:
3805 3804 /*
3806 3805 * Request for ATA Information page.
3807 3806 */
3808 3807 page_buf[0] = peripheral_device_type;
3809 3808 page_buf[1] = INQUIRY_ATA_INFO_PAGE;
3810 3809 page_buf[2] = (SATA_ID_ATA_INFO_LEN >> 8) &
3811 3810 0xff;
3812 3811 page_buf[3] = SATA_ID_ATA_INFO_LEN & 0xff;
3813 3812 /* page_buf[4-7] reserved */
3814 3813 #ifdef _LITTLE_ENDIAN
3815 3814 bcopy("ATA ", &page_buf[8], 8);
3816 3815 swab(sdinfo->satadrv_id.ai_model,
3817 3816 &page_buf[16], 16);
3818 3817 if (strncmp(&sdinfo->satadrv_id.ai_fw[4],
3819 3818 " ", 4) == 0) {
3820 3819 swab(sdinfo->satadrv_id.ai_fw,
3821 3820 &page_buf[32], 4);
3822 3821 } else {
3823 3822 swab(&sdinfo->satadrv_id.ai_fw[4],
3824 3823 &page_buf[32], 4);
3825 3824 }
3826 3825 #else /* _LITTLE_ENDIAN */
3827 3826 bcopy("ATA ", &page_buf[8], 8);
3828 3827 bcopy(sdinfo->satadrv_id.ai_model,
3829 3828 &page_buf[16], 16);
3830 3829 if (strncmp(&sdinfo->satadrv_id.ai_fw[4],
3831 3830 " ", 4) == 0) {
3832 3831 bcopy(sdinfo->satadrv_id.ai_fw,
3833 3832 &page_buf[32], 4);
3834 3833 } else {
3835 3834 bcopy(&sdinfo->satadrv_id.ai_fw[4],
3836 3835 &page_buf[32], 4);
3837 3836 }
3838 3837 #endif /* _LITTLE_ENDIAN */
3839 3838 /*
3840 3839 * page_buf[36-55] which defines the device
3841 3840 * signature is not defined at this
3842 3841 * time.
3843 3842 */
3844 3843
3845 3844 /* Set the command code */
3846 3845 if (sdinfo->satadrv_type ==
3847 3846 SATA_DTYPE_ATADISK) {
3848 3847 page_buf[56] = SATAC_ID_DEVICE;
3849 3848 } else if (sdinfo->satadrv_type ==
3850 3849 SATA_DTYPE_ATAPI) {
3851 3850 page_buf[56] = SATAC_ID_PACKET_DEVICE;
3852 3851 }
3853 3852 /*
3854 3853 * If the command code, page_buf[56], is not
3855 3854 * zero and if one of the identify commands
3856 3855 * succeeds, return the identify data.
3857 3856 */
3858 3857 if ((page_buf[56] != 0) &&
3859 3858 (sata_fetch_device_identify_data(
3860 3859 spx->txlt_sata_hba_inst, sdinfo) ==
3861 3860 SATA_SUCCESS)) {
3862 3861 bcopy(&sdinfo->satadrv_id,
3863 3862 &page_buf[60], sizeof (sata_id_t));
3864 3863 }
3865 3864
3866 3865 /* Need to copy out the page_buf to bp */
3867 3866 count = MIN(bp->b_bcount,
3868 3867 SATA_ID_ATA_INFO_LEN + 4);
3869 3868 bcopy(page_buf, bp->b_un.b_addr, count);
3870 3869 break;
3871 3870
3872 3871 case INQUIRY_DEV_IDENTIFICATION_PAGE:
3873 3872 /*
3874 3873 * We may want to implement this page, when
3875 3874 * identifiers are common for SATA devices
3876 3875 * But not now.
3877 3876 */
3878 3877 /*FALLTHROUGH*/
3879 3878
3880 3879 default:
3881 3880 /* Request for unsupported VPD page */
3882 3881 *scsipkt->pkt_scbp = STATUS_CHECK;
3883 3882 sense = sata_arq_sense(spx);
3884 3883 sense->es_key = KEY_ILLEGAL_REQUEST;
3885 3884 sense->es_add_code =
3886 3885 SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3887 3886 goto done;
3888 3887 }
3889 3888 }
3890 3889 scsipkt->pkt_state |= STATE_XFERRED_DATA;
3891 3890 scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3892 3891 scsipkt->pkt_cdbp[4] - count : 0;
3893 3892 }
3894 3893 done:
3895 3894 mutex_exit(cport_mutex);
3896 3895
3897 3896 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3898 3897 "Scsi_pkt completion reason %x\n",
3899 3898 scsipkt->pkt_reason);
3900 3899
3901 3900 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3902 3901 scsipkt->pkt_comp != NULL) {
3903 3902 /* scsi callback required */
3904 3903 if (servicing_interrupt()) {
3905 3904 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3906 3905 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3907 3906 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3908 3907 return (TRAN_BUSY);
3909 3908 }
3910 3909 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3911 3910 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3912 3911 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3913 3912 /* Scheduling the callback failed */
3914 3913 return (TRAN_BUSY);
3915 3914 }
3916 3915 }
3917 3916 return (TRAN_ACCEPT);
3918 3917 }
3919 3918
3920 3919 /*
3921 3920 * SATA translate command: Request Sense.
3922 3921 *
3923 3922 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3924 3923 * At the moment this is an emulated command (ATA version for SATA hard disks).
3925 3924 * May be translated into Check Power Mode command in the future.
3926 3925 *
3927 3926 * Note: There is a mismatch between already implemented Informational
3928 3927 * Exception Mode Select page 0x1C and this function.
3929 3928 * When MRIE bit is set in page 0x1C, Request Sense is supposed to return
3930 3929 * NO SENSE and set additional sense code to the exception code - this is not
3931 3930 * implemented here.
3932 3931 */
3933 3932 static int
3934 3933 sata_txlt_request_sense(sata_pkt_txlate_t *spx)
3935 3934 {
3936 3935 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3937 3936 struct scsi_extended_sense sense;
3938 3937 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3939 3938 sata_drive_info_t *sdinfo;
3940 3939 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3941 3940 int rval, reason, power_state = 0;
3942 3941 kmutex_t *cport_mutex;
3943 3942
3944 3943 cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
3945 3944 mutex_enter(cport_mutex);
3946 3945
3947 3946 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
3948 3947 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3949 3948 mutex_exit(cport_mutex);
3950 3949 return (rval);
3951 3950 }
3952 3951
3953 3952 scsipkt->pkt_reason = CMD_CMPLT;
3954 3953 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3955 3954 STATE_SENT_CMD | STATE_GOT_STATUS;
3956 3955 *scsipkt->pkt_scbp = STATUS_GOOD;
3957 3956
3958 3957 /*
3959 3958 * when CONTROL field's NACA bit == 1
3960 3959 * return ILLEGAL_REQUEST
3961 3960 */
3962 3961 if (scsipkt->pkt_cdbp[5] & CTL_BYTE_NACA_MASK) {
3963 3962 mutex_exit(cport_mutex);
3964 3963 return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
3965 3964 SD_SCSI_ASC_CMD_SEQUENCE_ERR));
3966 3965 }
3967 3966
3968 3967 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3969 3968 &spx->txlt_sata_pkt->satapkt_device);
3970 3969 ASSERT(sdinfo != NULL);
3971 3970
3972 3971 spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
3973 3972
3974 3973 sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
3975 3974 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
3976 3975 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3977 3976 if (sata_hba_start(spx, &rval) != 0) {
3978 3977 mutex_exit(cport_mutex);
3979 3978 return (rval);
3980 3979 }
3981 3980 if (scmd->satacmd_error_reg != 0) {
3982 3981 mutex_exit(cport_mutex);
3983 3982 return (sata_txlt_check_condition(spx, KEY_NO_SENSE,
3984 3983 SD_SCSI_ASC_NO_ADD_SENSE));
3985 3984 }
3986 3985
3987 3986 switch (scmd->satacmd_sec_count_lsb) {
3988 3987 case SATA_PWRMODE_STANDBY: /* device in standby mode */
3989 3988 if (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)
3990 3989 power_state = SATA_POWER_STOPPED;
3991 3990 else {
3992 3991 power_state = SATA_POWER_STANDBY;
3993 3992 sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
3994 3993 }
3995 3994 break;
3996 3995 case SATA_PWRMODE_IDLE: /* device in idle mode */
3997 3996 power_state = SATA_POWER_IDLE;
3998 3997 sdinfo->satadrv_power_level = SATA_POWER_IDLE;
3999 3998 break;
4000 3999 case SATA_PWRMODE_ACTIVE: /* device in active or idle mode */
4001 4000 default: /* 0x40, 0x41 active mode */
4002 4001 if (sdinfo->satadrv_power_level == SATA_POWER_IDLE)
4003 4002 power_state = SATA_POWER_IDLE;
4004 4003 else {
4005 4004 power_state = SATA_POWER_ACTIVE;
4006 4005 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4007 4006 }
4008 4007 break;
4009 4008 }
4010 4009
4011 4010 mutex_exit(cport_mutex);
4012 4011
4013 4012 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4014 4013 /*
4015 4014 * Because it is fully emulated command storing data
4016 4015 * programatically in the specified buffer, release
4017 4016 * preallocated DMA resources before storing data in the buffer,
4018 4017 * so no unwanted DMA sync would take place.
4019 4018 */
4020 4019 int count = MIN(bp->b_bcount,
4021 4020 sizeof (struct scsi_extended_sense));
4022 4021 sata_scsi_dmafree(NULL, scsipkt);
4023 4022 bzero(&sense, sizeof (struct scsi_extended_sense));
4024 4023 sense.es_valid = 0; /* Valid LBA */
4025 4024 sense.es_class = 7; /* Response code 0x70 - current err */
4026 4025 sense.es_key = KEY_NO_SENSE;
4027 4026 sense.es_add_len = 6; /* Additional length */
4028 4027 /* Copy no more than requested */
4029 4028 bcopy(&sense, bp->b_un.b_addr, count);
4030 4029 scsipkt->pkt_state |= STATE_XFERRED_DATA;
4031 4030 scsipkt->pkt_resid = 0;
4032 4031 switch (power_state) {
4033 4032 case SATA_POWER_IDLE:
4034 4033 case SATA_POWER_STANDBY:
4035 4034 sense.es_add_code =
4036 4035 SD_SCSI_ASC_LOW_POWER_CONDITION_ON;
4037 4036 break;
4038 4037 case SATA_POWER_STOPPED:
4039 4038 sense.es_add_code = SD_SCSI_ASC_NO_ADD_SENSE;
4040 4039 break;
4041 4040 case SATA_POWER_ACTIVE:
4042 4041 default:
4043 4042 break;
4044 4043 }
4045 4044 }
4046 4045
4047 4046 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4048 4047 "Scsi_pkt completion reason %x\n",
4049 4048 scsipkt->pkt_reason);
4050 4049
4051 4050 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4052 4051 scsipkt->pkt_comp != NULL) {
4053 4052 /* scsi callback required */
4054 4053 if (servicing_interrupt()) {
4055 4054 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4056 4055 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4057 4056 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4058 4057 return (TRAN_BUSY);
4059 4058 }
4060 4059 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4061 4060 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4062 4061 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4063 4062 /* Scheduling the callback failed */
4064 4063 return (TRAN_BUSY);
4065 4064 }
4066 4065 }
4067 4066 return (TRAN_ACCEPT);
4068 4067 }
4069 4068
4070 4069 /*
4071 4070 * SATA translate command: Test Unit Ready
4072 4071 * (ATA version for SATA hard disks).
4073 4072 * It is translated into the Check Power Mode command.
4074 4073 *
4075 4074 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4076 4075 */
4077 4076 static int
4078 4077 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx)
4079 4078 {
4080 4079 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4081 4080 struct scsi_extended_sense *sense;
4082 4081 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4083 4082 sata_drive_info_t *sdinfo;
4084 4083 int power_state;
4085 4084 int rval, reason;
4086 4085 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4087 4086
4088 4087 mutex_enter(cport_mutex);
4089 4088
4090 4089 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
4091 4090 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4092 4091 mutex_exit(cport_mutex);
4093 4092 return (rval);
4094 4093 }
4095 4094
4096 4095 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4097 4096 &spx->txlt_sata_pkt->satapkt_device);
4098 4097 ASSERT(sdinfo != NULL);
4099 4098
4100 4099 spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
4101 4100
4102 4101 /* send CHECK POWER MODE command */
4103 4102 sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
4104 4103 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
4105 4104 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4106 4105 if (sata_hba_start(spx, &rval) != 0) {
4107 4106 mutex_exit(cport_mutex);
4108 4107 return (rval);
4109 4108 }
4110 4109
4111 4110 if (scmd->satacmd_error_reg != 0) {
4112 4111 mutex_exit(cport_mutex);
4113 4112 return (sata_txlt_check_condition(spx, KEY_NOT_READY,
4114 4113 SD_SCSI_ASC_LU_NOT_RESPONSE));
4115 4114 }
4116 4115
4117 4116 power_state = scmd->satacmd_sec_count_lsb;
4118 4117
4119 4118 /*
4120 4119 * return NOT READY when device in STOPPED mode
4121 4120 */
4122 4121 if (power_state == SATA_PWRMODE_STANDBY &&
4123 4122 sdinfo->satadrv_power_level == SATA_POWER_STOPPED) {
4124 4123 *scsipkt->pkt_scbp = STATUS_CHECK;
4125 4124 sense = sata_arq_sense(spx);
4126 4125 sense->es_key = KEY_NOT_READY;
4127 4126 sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY;
4128 4127 } else {
4129 4128 /*
4130 4129 * For other power mode, return GOOD status
4131 4130 */
4132 4131 *scsipkt->pkt_scbp = STATUS_GOOD;
4133 4132 }
4134 4133
4135 4134 scsipkt->pkt_reason = CMD_CMPLT;
4136 4135 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4137 4136 STATE_SENT_CMD | STATE_GOT_STATUS;
4138 4137
4139 4138 mutex_exit(cport_mutex);
4140 4139
4141 4140 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4142 4141 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4143 4142
4144 4143 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4145 4144 scsipkt->pkt_comp != NULL) {
4146 4145 /* scsi callback required */
4147 4146 if (servicing_interrupt()) {
4148 4147 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4149 4148 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4150 4149 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4151 4150 return (TRAN_BUSY);
4152 4151 }
4153 4152 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4154 4153 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4155 4154 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4156 4155 /* Scheduling the callback failed */
4157 4156 return (TRAN_BUSY);
4158 4157 }
4159 4158 }
4160 4159
4161 4160 return (TRAN_ACCEPT);
4162 4161 }
4163 4162
4164 4163 /*
4165 4164 * SATA translate command: Start Stop Unit
4166 4165 * Translation depends on a command:
4167 4166 *
4168 4167 * Power condition bits will be supported
4169 4168 * and the power level should be maintained by SATL,
4170 4169 * When SATL received a command, it will check the
4171 4170 * power level firstly, and return the status according
4172 4171 * to SAT2 v2.6 and SAT-2 Standby Modifications
4173 4172 *
4174 4173 * SPC-4/SBC-3 SATL ATA power condition SATL SPC/SBC
4175 4174 * -----------------------------------------------------------------------
4176 4175 * SSU_PC1 Active <==> ATA Active <==> SSU:start_bit =1
4177 4176 * SSU_PC2 Idle <==> ATA Idle <==> N/A
4178 4177 * SSU_PC3 Standby <==> ATA Standby <==> N/A
4179 4178 * SSU_PC4 Stopped <==> ATA Standby <==> SSU:start_bit = 0
4180 4179 *
4181 4180 * Unload Media / NOT SUPPORTED YET
4182 4181 * Load Media / NOT SUPPROTED YET
4183 4182 * Immediate bit / NOT SUPPORTED YET (deferred error)
4184 4183 *
4185 4184 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4186 4185 * appropriate values in scsi_pkt fields.
4187 4186 */
4188 4187 static int
4189 4188 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx)
4190 4189 {
4191 4190 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4192 4191 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4193 4192 int rval, reason;
4194 4193 sata_drive_info_t *sdinfo;
4195 4194 sata_id_t *sata_id;
4196 4195 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4197 4196
4198 4197 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4199 4198 "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1);
4200 4199
4201 4200 mutex_enter(cport_mutex);
4202 4201
4203 4202 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
4204 4203 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4205 4204 mutex_exit(cport_mutex);
4206 4205 return (rval);
4207 4206 }
4208 4207
4209 4208 if (scsipkt->pkt_cdbp[1] & START_STOP_IMMED_MASK) {
4210 4209 /* IMMED bit - not supported */
4211 4210 mutex_exit(cport_mutex);
4212 4211 return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4213 4212 SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4214 4213 }
4215 4214
4216 4215 spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
4217 4216 spx->txlt_sata_pkt->satapkt_comp = NULL;
4218 4217
4219 4218 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4220 4219 &spx->txlt_sata_pkt->satapkt_device);
4221 4220 ASSERT(sdinfo != NULL);
4222 4221 sata_id = &sdinfo->satadrv_id;
4223 4222
4224 4223 switch ((scsipkt->pkt_cdbp[4] & START_STOP_POWER_COND_MASK) >> 4) {
4225 4224 case 0:
4226 4225 if (scsipkt->pkt_cdbp[4] & START_STOP_LOEJ_MASK) {
4227 4226 /* Load/Unload Media - invalid request */
4228 4227 goto err_out;
4229 4228 }
4230 4229 if (scsipkt->pkt_cdbp[4] & START_STOP_START_MASK) {
4231 4230 /* Start Unit */
4232 4231 sata_build_read_verify_cmd(scmd, 1, 5);
4233 4232 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4234 4233 /* Transfer command to HBA */
4235 4234 if (sata_hba_start(spx, &rval) != 0) {
4236 4235 /* Pkt not accepted for execution */
4237 4236 mutex_exit(cport_mutex);
4238 4237 return (rval);
4239 4238 }
4240 4239 if (scmd->satacmd_error_reg != 0) {
4241 4240 goto err_out;
4242 4241 }
4243 4242 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4244 4243 } else {
4245 4244 /* Stop Unit */
4246 4245 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4247 4246 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4248 4247 if (sata_hba_start(spx, &rval) != 0) {
4249 4248 mutex_exit(cport_mutex);
4250 4249 return (rval);
4251 4250 } else {
4252 4251 if (scmd->satacmd_error_reg != 0) {
4253 4252 goto err_out;
4254 4253 }
4255 4254 }
4256 4255 /* ata standby immediate command */
4257 4256 sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
4258 4257 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4259 4258 if (sata_hba_start(spx, &rval) != 0) {
4260 4259 mutex_exit(cport_mutex);
4261 4260 return (rval);
4262 4261 }
4263 4262 if (scmd->satacmd_error_reg != 0) {
4264 4263 goto err_out;
4265 4264 }
4266 4265 sdinfo->satadrv_power_level = SATA_POWER_STOPPED;
4267 4266 }
4268 4267 break;
4269 4268 case 0x1:
4270 4269 sata_build_generic_cmd(scmd, SATAC_IDLE);
4271 4270 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4272 4271 if (sata_hba_start(spx, &rval) != 0) {
4273 4272 mutex_exit(cport_mutex);
4274 4273 return (rval);
4275 4274 }
4276 4275 if (scmd->satacmd_error_reg != 0) {
4277 4276 goto err_out;
4278 4277 }
4279 4278 sata_build_read_verify_cmd(scmd, 1, 5);
4280 4279 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4281 4280 /* Transfer command to HBA */
4282 4281 if (sata_hba_start(spx, &rval) != 0) {
4283 4282 /* Pkt not accepted for execution */
4284 4283 mutex_exit(cport_mutex);
4285 4284 return (rval);
4286 4285 } else {
4287 4286 if (scmd->satacmd_error_reg != 0) {
4288 4287 goto err_out;
4289 4288 }
4290 4289 }
4291 4290 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4292 4291 break;
4293 4292 case 0x2:
4294 4293 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4295 4294 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4296 4295 if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4297 4296 if (sata_hba_start(spx, &rval) != 0) {
4298 4297 mutex_exit(cport_mutex);
4299 4298 return (rval);
4300 4299 }
4301 4300 if (scmd->satacmd_error_reg != 0) {
4302 4301 goto err_out;
4303 4302 }
4304 4303 }
4305 4304 sata_build_generic_cmd(scmd, SATAC_IDLE);
4306 4305 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4307 4306 if (sata_hba_start(spx, &rval) != 0) {
4308 4307 mutex_exit(cport_mutex);
4309 4308 return (rval);
4310 4309 }
4311 4310 if (scmd->satacmd_error_reg != 0) {
4312 4311 goto err_out;
4313 4312 }
4314 4313 if ((scsipkt->pkt_cdbp[3] & START_STOP_MODIFIER_MASK)) {
4315 4314 /*
4316 4315 * POWER CONDITION MODIFIER bit set
4317 4316 * to 0x1 or larger it will be handled
4318 4317 * on the same way as bit = 0x1
4319 4318 */
4320 4319 if (!(sata_id->ai_cmdset84 &
4321 4320 SATA_IDLE_UNLOAD_SUPPORTED)) {
4322 4321 sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4323 4322 break;
4324 4323 }
4325 4324 sata_build_generic_cmd(scmd, SATAC_IDLE_IM);
4326 4325 scmd->satacmd_features_reg = 0x44;
4327 4326 scmd->satacmd_lba_low_lsb = 0x4c;
4328 4327 scmd->satacmd_lba_mid_lsb = 0x4e;
4329 4328 scmd->satacmd_lba_high_lsb = 0x55;
4330 4329 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4331 4330 if (sata_hba_start(spx, &rval) != 0) {
4332 4331 mutex_exit(cport_mutex);
4333 4332 return (rval);
4334 4333 }
4335 4334 if (scmd->satacmd_error_reg != 0) {
4336 4335 goto err_out;
4337 4336 }
4338 4337 }
4339 4338 sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4340 4339 break;
4341 4340 case 0x3:
4342 4341 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4343 4342 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4344 4343 if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4345 4344 if (sata_hba_start(spx, &rval) != 0) {
4346 4345 mutex_exit(cport_mutex);
4347 4346 return (rval);
4348 4347 }
4349 4348 if (scmd->satacmd_error_reg != 0) {
4350 4349 goto err_out;
4351 4350 }
4352 4351 }
4353 4352 sata_build_generic_cmd(scmd, SATAC_STANDBY);
4354 4353 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4355 4354 if (sata_hba_start(spx, &rval) != 0) {
4356 4355 mutex_exit(cport_mutex);
4357 4356 return (rval);
4358 4357 }
4359 4358 if (scmd->satacmd_error_reg != 0) {
4360 4359 goto err_out;
4361 4360 }
4362 4361 sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
4363 4362 break;
4364 4363 case 0x7:
4365 4364 sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
4366 4365 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
4367 4366 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4368 4367 if (sata_hba_start(spx, &rval) != 0) {
4369 4368 mutex_exit(cport_mutex);
4370 4369 return (rval);
4371 4370 }
4372 4371 if (scmd->satacmd_error_reg != 0) {
4373 4372 goto err_out;
4374 4373 }
4375 4374 switch (scmd->satacmd_sec_count_lsb) {
4376 4375 case SATA_PWRMODE_STANDBY:
4377 4376 sata_build_generic_cmd(scmd, SATAC_STANDBY);
4378 4377 scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4379 4378 sdinfo->satadrv_standby_timer);
4380 4379 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4381 4380 if (sata_hba_start(spx, &rval) != 0) {
4382 4381 mutex_exit(cport_mutex);
4383 4382 return (rval);
4384 4383 } else {
4385 4384 if (scmd->satacmd_error_reg != 0) {
4386 4385 goto err_out;
4387 4386 }
4388 4387 }
4389 4388 break;
4390 4389 case SATA_PWRMODE_IDLE:
4391 4390 sata_build_generic_cmd(scmd, SATAC_IDLE);
4392 4391 scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4393 4392 sdinfo->satadrv_standby_timer);
4394 4393 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4395 4394 if (sata_hba_start(spx, &rval) != 0) {
4396 4395 mutex_exit(cport_mutex);
4397 4396 return (rval);
4398 4397 } else {
4399 4398 if (scmd->satacmd_error_reg != 0) {
4400 4399 goto err_out;
4401 4400 }
4402 4401 }
4403 4402 break;
4404 4403 case SATA_PWRMODE_ACTIVE_SPINDOWN:
4405 4404 case SATA_PWRMODE_ACTIVE_SPINUP:
4406 4405 case SATA_PWRMODE_ACTIVE:
4407 4406 sata_build_generic_cmd(scmd, SATAC_IDLE);
4408 4407 scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4409 4408 sdinfo->satadrv_standby_timer);
4410 4409 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4411 4410 if (sata_hba_start(spx, &rval) != 0) {
4412 4411 mutex_exit(cport_mutex);
4413 4412 return (rval);
4414 4413 }
4415 4414 if (scmd->satacmd_error_reg != 0) {
4416 4415 goto err_out;
4417 4416 }
4418 4417 sata_build_read_verify_cmd(scmd, 1, 5);
4419 4418 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4420 4419 if (sata_hba_start(spx, &rval) != 0) {
4421 4420 mutex_exit(cport_mutex);
4422 4421 return (rval);
4423 4422 }
4424 4423 if (scmd->satacmd_error_reg != 0) {
4425 4424 goto err_out;
4426 4425 }
4427 4426 break;
4428 4427 default:
4429 4428 goto err_out;
4430 4429 }
4431 4430 break;
4432 4431 case 0xb:
4433 4432 if ((sata_get_standby_timer(sdinfo->satadrv_standby_timer) ==
4434 4433 0) || (!(sata_id->ai_cap & SATA_STANDBYTIMER))) {
4435 4434 mutex_exit(cport_mutex);
4436 4435 return (sata_txlt_check_condition(spx,
4437 4436 KEY_ILLEGAL_REQUEST,
4438 4437 SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4439 4438 }
4440 4439 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4441 4440 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4442 4441 if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4443 4442 if (sata_hba_start(spx, &rval) != 0) {
4444 4443 mutex_exit(cport_mutex);
4445 4444 return (rval);
4446 4445 }
4447 4446 if (scmd->satacmd_error_reg != 0) {
4448 4447 goto err_out;
4449 4448 }
4450 4449 sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
4451 4450 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4452 4451 if (sata_hba_start(spx, &rval) != 0) {
4453 4452 mutex_exit(cport_mutex);
4454 4453 return (rval);
4455 4454 }
4456 4455 if (scmd->satacmd_error_reg != 0) {
4457 4456 goto err_out;
4458 4457 }
4459 4458 }
4460 4459 bzero(sdinfo->satadrv_standby_timer, sizeof (uchar_t) * 4);
4461 4460 break;
4462 4461 default:
4463 4462 err_out:
4464 4463 mutex_exit(cport_mutex);
4465 4464 return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4466 4465 SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4467 4466 }
4468 4467
4469 4468 /*
4470 4469 * Since it was a synchronous command,
4471 4470 * a callback function will be called directly.
4472 4471 */
4473 4472 mutex_exit(cport_mutex);
4474 4473 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4475 4474 "synchronous execution status %x\n",
4476 4475 spx->txlt_sata_pkt->satapkt_reason);
4477 4476
4478 4477 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4479 4478 scsipkt->pkt_comp != NULL) {
4480 4479 sata_set_arq_data(spx->txlt_sata_pkt);
4481 4480 if (servicing_interrupt()) {
4482 4481 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4483 4482 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4484 4483 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4485 4484 return (TRAN_BUSY);
4486 4485 }
4487 4486 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4488 4487 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4489 4488 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4490 4489 /* Scheduling the callback failed */
4491 4490 return (TRAN_BUSY);
4492 4491 }
4493 4492 }
4494 4493 else
4495 4494
4496 4495 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
4497 4496
4498 4497 return (TRAN_ACCEPT);
4499 4498
4500 4499 }
4501 4500
4502 4501 /*
4503 4502 * SATA translate command: Read Capacity.
4504 4503 * Emulated command for SATA disks.
4505 4504 * Capacity is retrieved from cached Idenifty Device data.
4506 4505 * Identify Device data shows effective disk capacity, not the native
4507 4506 * capacity, which may be limitted by Set Max Address command.
4508 4507 * This is ATA version for SATA hard disks.
4509 4508 *
4510 4509 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4511 4510 */
4512 4511 static int
4513 4512 sata_txlt_read_capacity(sata_pkt_txlate_t *spx)
4514 4513 {
4515 4514 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4516 4515 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4517 4516 sata_drive_info_t *sdinfo;
4518 4517 uint64_t val;
4519 4518 uchar_t *rbuf;
4520 4519 int rval, reason;
4521 4520 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4522 4521
4523 4522 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4524 4523 "sata_txlt_read_capacity: ", NULL);
4525 4524
4526 4525 mutex_enter(cport_mutex);
4527 4526
4528 4527 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4529 4528 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4530 4529 mutex_exit(cport_mutex);
4531 4530 return (rval);
4532 4531 }
4533 4532
4534 4533 scsipkt->pkt_reason = CMD_CMPLT;
4535 4534 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4536 4535 STATE_SENT_CMD | STATE_GOT_STATUS;
4537 4536 *scsipkt->pkt_scbp = STATUS_GOOD;
4538 4537 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4539 4538 /*
4540 4539 * Because it is fully emulated command storing data
4541 4540 * programatically in the specified buffer, release
4542 4541 * preallocated DMA resources before storing data in the buffer,
4543 4542 * so no unwanted DMA sync would take place.
4544 4543 */
4545 4544 sata_scsi_dmafree(NULL, scsipkt);
4546 4545
4547 4546 sdinfo = sata_get_device_info(
4548 4547 spx->txlt_sata_hba_inst,
4549 4548 &spx->txlt_sata_pkt->satapkt_device);
4550 4549
4551 4550 /*
4552 4551 * As per SBC-3, the "returned LBA" is either the highest
4553 4552 * addressable LBA or 0xffffffff, whichever is smaller.
4554 4553 */
4555 4554 val = MIN(sdinfo->satadrv_capacity - 1, UINT32_MAX);
4556 4555
4557 4556 rbuf = (uchar_t *)bp->b_un.b_addr;
4558 4557 /* Need to swap endians to match scsi format */
4559 4558 rbuf[0] = (val >> 24) & 0xff;
4560 4559 rbuf[1] = (val >> 16) & 0xff;
4561 4560 rbuf[2] = (val >> 8) & 0xff;
4562 4561 rbuf[3] = val & 0xff;
4563 4562 /* block size - always 512 bytes, for now */
4564 4563 rbuf[4] = 0;
4565 4564 rbuf[5] = 0;
4566 4565 rbuf[6] = 0x02;
4567 4566 rbuf[7] = 0;
4568 4567 scsipkt->pkt_state |= STATE_XFERRED_DATA;
4569 4568 scsipkt->pkt_resid = 0;
4570 4569
4571 4570 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n",
4572 4571 sdinfo->satadrv_capacity -1);
4573 4572 }
4574 4573 mutex_exit(cport_mutex);
4575 4574 /*
4576 4575 * If a callback was requested, do it now.
4577 4576 */
4578 4577 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4579 4578 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4580 4579
4581 4580 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4582 4581 scsipkt->pkt_comp != NULL) {
4583 4582 /* scsi callback required */
4584 4583 if (servicing_interrupt()) {
4585 4584 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4586 4585 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4587 4586 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4588 4587 return (TRAN_BUSY);
4589 4588 }
4590 4589 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4591 4590 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4592 4591 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4593 4592 /* Scheduling the callback failed */
4594 4593 return (TRAN_BUSY);
4595 4594 }
4596 4595 }
4597 4596
4598 4597 return (TRAN_ACCEPT);
4599 4598 }
4600 4599
4601 4600 /*
4602 4601 * SATA translate command: Read Capacity (16).
4603 4602 * Emulated command for SATA disks.
4604 4603 * Info is retrieved from cached Identify Device data.
4605 4604 * Implemented to SBC-3 (draft 21) and SAT-2 (final) specifications.
4606 4605 *
4607 4606 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4608 4607 */
4609 4608 static int
4610 4609 sata_txlt_read_capacity16(sata_pkt_txlate_t *spx)
4611 4610 {
4612 4611 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4613 4612 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4614 4613 sata_drive_info_t *sdinfo;
4615 4614 uint64_t val;
4616 4615 uint16_t l2p_exp;
4617 4616 uchar_t *rbuf;
4618 4617 int rval, reason;
4619 4618 #define TPE 0x80
4620 4619 #define TPRZ 0x40
4621 4620 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4622 4621
4623 4622 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4624 4623 "sata_txlt_read_capacity: ", NULL);
4625 4624
4626 4625 mutex_enter(cport_mutex);
4627 4626
4628 4627 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4629 4628 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4630 4629 mutex_exit(cport_mutex);
4631 4630 return (rval);
4632 4631 }
4633 4632
4634 4633 scsipkt->pkt_reason = CMD_CMPLT;
4635 4634 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4636 4635 STATE_SENT_CMD | STATE_GOT_STATUS;
4637 4636 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4638 4637 /*
4639 4638 * Because it is fully emulated command storing data
4640 4639 * programatically in the specified buffer, release
4641 4640 * preallocated DMA resources before storing data in the buffer,
4642 4641 * so no unwanted DMA sync would take place.
4643 4642 */
4644 4643 sata_scsi_dmafree(NULL, scsipkt);
4645 4644
4646 4645 /* Check SERVICE ACTION field */
4647 4646 if ((scsipkt->pkt_cdbp[1] & 0x1f) !=
4648 4647 SSVC_ACTION_READ_CAPACITY_G4) {
4649 4648 mutex_exit(cport_mutex);
4650 4649 return (sata_txlt_check_condition(spx,
4651 4650 KEY_ILLEGAL_REQUEST,
4652 4651 SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4653 4652 }
4654 4653
4655 4654 /* Check LBA field */
4656 4655 if ((scsipkt->pkt_cdbp[2] != 0) ||
4657 4656 (scsipkt->pkt_cdbp[3] != 0) ||
4658 4657 (scsipkt->pkt_cdbp[4] != 0) ||
4659 4658 (scsipkt->pkt_cdbp[5] != 0) ||
4660 4659 (scsipkt->pkt_cdbp[6] != 0) ||
4661 4660 (scsipkt->pkt_cdbp[7] != 0) ||
4662 4661 (scsipkt->pkt_cdbp[8] != 0) ||
4663 4662 (scsipkt->pkt_cdbp[9] != 0)) {
4664 4663 mutex_exit(cport_mutex);
4665 4664 return (sata_txlt_check_condition(spx,
4666 4665 KEY_ILLEGAL_REQUEST,
4667 4666 SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4668 4667 }
4669 4668
4670 4669 /* Check PMI bit */
4671 4670 if (scsipkt->pkt_cdbp[14] & 0x1) {
4672 4671 mutex_exit(cport_mutex);
4673 4672 return (sata_txlt_check_condition(spx,
4674 4673 KEY_ILLEGAL_REQUEST,
4675 4674 SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4676 4675 }
4677 4676
4678 4677 *scsipkt->pkt_scbp = STATUS_GOOD;
4679 4678
4680 4679 sdinfo = sata_get_device_info(
4681 4680 spx->txlt_sata_hba_inst,
4682 4681 &spx->txlt_sata_pkt->satapkt_device);
4683 4682
4684 4683 /* last logical block address */
4685 4684 val = MIN(sdinfo->satadrv_capacity - 1,
4686 4685 SCSI_READ_CAPACITY16_MAX_LBA);
4687 4686
4688 4687 /* logical to physical block size exponent */
4689 4688 l2p_exp = 0;
4690 4689 if (sdinfo->satadrv_id.ai_phys_sect_sz & SATA_L2PS_CHECK_BIT) {
4691 4690 /* physical/logical sector size word is valid */
4692 4691
4693 4692 if (sdinfo->satadrv_id.ai_phys_sect_sz &
4694 4693 SATA_L2PS_HAS_MULT) {
4695 4694 /* multiple logical sectors per phys sectors */
4696 4695 l2p_exp =
4697 4696 sdinfo->satadrv_id.ai_phys_sect_sz &
4698 4697 SATA_L2PS_EXP_MASK;
4699 4698 }
4700 4699 }
4701 4700
4702 4701 rbuf = (uchar_t *)bp->b_un.b_addr;
4703 4702 bzero(rbuf, bp->b_bcount);
4704 4703
4705 4704 /* returned logical block address */
4706 4705 rbuf[0] = (val >> 56) & 0xff;
4707 4706 rbuf[1] = (val >> 48) & 0xff;
4708 4707 rbuf[2] = (val >> 40) & 0xff;
4709 4708 rbuf[3] = (val >> 32) & 0xff;
4710 4709 rbuf[4] = (val >> 24) & 0xff;
4711 4710 rbuf[5] = (val >> 16) & 0xff;
4712 4711 rbuf[6] = (val >> 8) & 0xff;
4713 4712 rbuf[7] = val & 0xff;
4714 4713
4715 4714 /* logical block length in bytes = 512 (for now) */
4716 4715 /* rbuf[8] = 0; */
4717 4716 /* rbuf[9] = 0; */
4718 4717 rbuf[10] = 0x02;
4719 4718 /* rbuf[11] = 0; */
4720 4719
4721 4720 /* p_type, prot_en, unspecified by SAT-2 */
4722 4721 /* rbuf[12] = 0; */
4723 4722
4724 4723 /* p_i_exponent, undefined by SAT-2 */
4725 4724 /* logical blocks per physical block exponent */
4726 4725 rbuf[13] = l2p_exp;
4727 4726
4728 4727 /* lowest aligned logical block address = 0 (for now) */
4729 4728 /* tpe and tprz as defined in T10/10-079 r0 */
4730 4729 if (sdinfo->satadrv_id.ai_addsupported &
4731 4730 SATA_DETERMINISTIC_READ) {
4732 4731 if (sdinfo->satadrv_id.ai_addsupported &
4733 4732 SATA_READ_ZERO) {
4734 4733 rbuf[14] |= TPRZ;
4735 4734 } else {
4736 4735 rbuf[14] |= TPE;
4737 4736 }
4738 4737 }
4739 4738 /* rbuf[15] = 0; */
4740 4739
4741 4740 scsipkt->pkt_state |= STATE_XFERRED_DATA;
4742 4741 scsipkt->pkt_resid = 0;
4743 4742
4744 4743 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%llu\n",
4745 4744 sdinfo->satadrv_capacity -1);
4746 4745 }
4747 4746
4748 4747 mutex_exit(cport_mutex);
4749 4748
4750 4749 /*
4751 4750 * If a callback was requested, do it now.
4752 4751 */
4753 4752 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4754 4753 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4755 4754
4756 4755 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4757 4756 scsipkt->pkt_comp != NULL) {
4758 4757 /* scsi callback required */
4759 4758 if (servicing_interrupt()) {
4760 4759 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4761 4760 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4762 4761 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4763 4762 return (TRAN_BUSY);
4764 4763 }
4765 4764 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4766 4765 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4767 4766 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4768 4767 /* Scheduling the callback failed */
4769 4768 return (TRAN_BUSY);
4770 4769 }
4771 4770 }
4772 4771
4773 4772 return (TRAN_ACCEPT);
4774 4773 }
4775 4774
4776 4775 /*
4777 4776 * Translate command: UNMAP
4778 4777 *
4779 4778 * The function cannot be called in interrupt context since it may sleep.
4780 4779 */
4781 4780 static int
4782 4781 sata_txlt_unmap(sata_pkt_txlate_t *spx)
4783 4782 {
4784 4783 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4785 4784 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4786 4785 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4787 4786 uint16_t count = 0;
4788 4787 int synch;
4789 4788 int rval, reason;
4790 4789 int i, x;
4791 4790 int bdlen = 0;
4792 4791 int ranges = 0;
4793 4792 int paramlen = 8;
4794 4793 uint8_t *data, *tmpbd;
4795 4794 sata_drive_info_t *sdinfo;
4796 4795 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4797 4796 #define TRIM 0x1
4798 4797
4799 4798 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4800 4799 "sata_txlt_unmap: ", NULL);
4801 4800
4802 4801 mutex_enter(cport_mutex);
4803 4802
4804 4803 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4805 4804 &spx->txlt_sata_pkt->satapkt_device);
4806 4805 if (sdinfo != NULL) {
4807 4806 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4808 4807 "DSM support 0x%x, max number of 512 byte blocks of LBA "
4809 4808 "range entries 0x%x\n", sdinfo->satadrv_id.ai_dsm,
4810 4809 sdinfo->satadrv_id.ai_maxcount);
4811 4810 }
4812 4811
4813 4812 rval = sata_txlt_generic_pkt_info(spx, &reason, 1);
4814 4813 if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4815 4814 mutex_exit(cport_mutex);
4816 4815 return (rval);
4817 4816 }
4818 4817
4819 4818 /*
4820 4819 * Need to modify bp to have TRIM data instead of UNMAP data.
4821 4820 * Start by getting the block descriptor data length by subtracting
4822 4821 * the 8 byte parameter list header from the parameter list length.
4823 4822 * The block descriptor size has to be a multiple of 16 bytes.
4824 4823 */
4825 4824 bdlen = scsipkt->pkt_cdbp[7];
4826 4825 bdlen = (bdlen << 8) + scsipkt->pkt_cdbp[8] - paramlen;
4827 4826 if ((bdlen < 0) || ((bdlen % 16) != 0) ||
4828 4827 (bdlen > (bp->b_bcount - paramlen))) {
4829 4828 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4830 4829 "sata_txlt_unmap: invalid block descriptor length", NULL);
4831 4830 mutex_exit(cport_mutex);
4832 4831 return ((sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4833 4832 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)));
4834 4833 }
4835 4834 /*
4836 4835 * If there are no parameter data or block descriptors, it is not
4837 4836 * considered an error so just complete the command without sending
4838 4837 * TRIM.
4839 4838 */
4840 4839 if ((bdlen == 0) || (bp == NULL) || (bp->b_un.b_addr == NULL) ||
4841 4840 (bp->b_bcount == 0)) {
4842 4841 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4843 4842 "sata_txlt_unmap: no parameter data or block descriptors",
4844 4843 NULL);
4845 4844 mutex_exit(cport_mutex);
4846 4845 return (sata_txlt_unmap_nodata_cmd(spx));
4847 4846 }
4848 4847 tmpbd = (uint8_t *)bp->b_un.b_addr + paramlen;
4849 4848 data = kmem_zalloc(bdlen, KM_SLEEP);
4850 4849
4851 4850 /*
4852 4851 * Loop through all the UNMAP block descriptors and convert the data
4853 4852 * into TRIM format.
4854 4853 */
4855 4854 for (i = 0, x = 0; i < bdlen; i += 16, x += 8) {
4856 4855 /* get range length */
4857 4856 data[x] = tmpbd[i+7];
4858 4857 data[x+1] = tmpbd[i+6];
4859 4858 /* get LBA */
4860 4859 data[x+2] = tmpbd[i+5];
4861 4860 data[x+3] = tmpbd[i+4];
4862 4861 data[x+4] = tmpbd[i+3];
4863 4862 data[x+5] = tmpbd[i+2];
4864 4863 data[x+6] = tmpbd[i+11];
4865 4864 data[x+7] = tmpbd[i+10];
4866 4865
4867 4866 ranges++;
4868 4867 }
4869 4868
4870 4869 /*
4871 4870 * The TRIM command expects the data buffer to be a multiple of
4872 4871 * 512-byte blocks of range entries. This means that the UNMAP buffer
4873 4872 * may be too small. Free the original DMA resources and create a
4874 4873 * local buffer.
4875 4874 */
4876 4875 sata_common_free_dma_rsrcs(spx);
4877 4876
4878 4877 /*
4879 4878 * Get count of 512-byte blocks of range entries. The length
4880 4879 * of a range entry is 8 bytes which means one count has 64 range
4881 4880 * entries.
4882 4881 */
4883 4882 count = (ranges + 63)/64;
4884 4883
4885 4884 /* Allocate a buffer that is a multiple of 512 bytes. */
4886 4885 mutex_exit(cport_mutex);
4887 4886 bp = sata_alloc_local_buffer(spx, count * 512);
4888 4887 if (bp == NULL) {
4889 4888 SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
4890 4889 "sata_txlt_unmap: "
4891 4890 "cannot allocate buffer for TRIM command", NULL);
4892 4891 kmem_free(data, bdlen);
4893 4892 return (TRAN_BUSY);
4894 4893 }
4895 4894 bp_mapin(bp); /* make data buffer accessible */
4896 4895 mutex_enter(cport_mutex);
4897 4896
4898 4897 bzero(bp->b_un.b_addr, bp->b_bcount);
4899 4898 bcopy(data, bp->b_un.b_addr, x);
4900 4899 kmem_free(data, bdlen);
4901 4900 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
4902 4901 DDI_DMA_SYNC_FORDEV);
4903 4902 ASSERT(rval == DDI_SUCCESS);
4904 4903
4905 4904 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4906 4905 scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4907 4906 scmd->satacmd_cmd_reg = SATAC_DSM;
4908 4907 scmd->satacmd_sec_count_msb = (count >> 8) & 0xff;
4909 4908 scmd->satacmd_sec_count_lsb = count & 0xff;
4910 4909 scmd->satacmd_features_reg = TRIM;
4911 4910 scmd->satacmd_device_reg = SATA_ADH_LBA;
4912 4911 scmd->satacmd_status_reg = 0;
4913 4912 scmd->satacmd_error_reg = 0;
4914 4913
4915 4914 /* Start processing command */
4916 4915 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4917 4916 spx->txlt_sata_pkt->satapkt_comp =
4918 4917 sata_txlt_unmap_completion;
4919 4918 synch = FALSE;
4920 4919 } else {
4921 4920 synch = TRUE;
4922 4921 }
4923 4922
4924 4923 if (sata_hba_start(spx, &rval) != 0) {
4925 4924 mutex_exit(cport_mutex);
4926 4925 return (rval);
4927 4926 }
4928 4927
4929 4928 mutex_exit(cport_mutex);
4930 4929
4931 4930 if (synch) {
4932 4931 sata_txlt_unmap_completion(spx->txlt_sata_pkt);
4933 4932 }
4934 4933
4935 4934 return (TRAN_ACCEPT);
4936 4935 }
4937 4936
4938 4937 /*
4939 4938 * SATA translate command: Mode Sense.
4940 4939 * Translated into appropriate SATA command or emulated.
4941 4940 * Saved Values Page Control (03) are not supported.
4942 4941 *
4943 4942 * NOTE: only caching mode sense page is currently implemented.
4944 4943 *
4945 4944 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4946 4945 */
4947 4946
4948 4947 #define LLBAA 0x10 /* Long LBA Accepted */
4949 4948
4950 4949 static int
4951 4950 sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
4952 4951 {
4953 4952 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4954 4953 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4955 4954 sata_drive_info_t *sdinfo;
4956 4955 sata_id_t *sata_id;
4957 4956 struct scsi_extended_sense *sense;
4958 4957 int len, bdlen, count, alc_len;
4959 4958 int pc; /* Page Control code */
4960 4959 uint8_t *buf; /* mode sense buffer */
4961 4960 int rval, reason;
4962 4961 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4963 4962
4964 4963 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4965 4964 "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
4966 4965 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4967 4966 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4968 4967
4969 4968 if (servicing_interrupt()) {
4970 4969 buf = kmem_zalloc(1024, KM_NOSLEEP);
4971 4970 if (buf == NULL) {
4972 4971 return (TRAN_BUSY);
4973 4972 }
4974 4973 } else {
4975 4974 buf = kmem_zalloc(1024, KM_SLEEP);
4976 4975 }
4977 4976
4978 4977 mutex_enter(cport_mutex);
4979 4978
4980 4979 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4981 4980 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4982 4981 mutex_exit(cport_mutex);
4983 4982 kmem_free(buf, 1024);
4984 4983 return (rval);
4985 4984 }
4986 4985
4987 4986 scsipkt->pkt_reason = CMD_CMPLT;
4988 4987 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4989 4988 STATE_SENT_CMD | STATE_GOT_STATUS;
4990 4989
4991 4990 pc = scsipkt->pkt_cdbp[2] >> 6;
4992 4991
4993 4992 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4994 4993 /*
4995 4994 * Because it is fully emulated command storing data
4996 4995 * programatically in the specified buffer, release
4997 4996 * preallocated DMA resources before storing data in the buffer,
4998 4997 * so no unwanted DMA sync would take place.
4999 4998 */
5000 4999 sata_scsi_dmafree(NULL, scsipkt);
5001 5000
5002 5001 len = 0;
5003 5002 bdlen = 0;
5004 5003 if (!(scsipkt->pkt_cdbp[1] & 8)) {
5005 5004 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
5006 5005 (scsipkt->pkt_cdbp[1] & LLBAA))
5007 5006 bdlen = 16;
5008 5007 else
5009 5008 bdlen = 8;
5010 5009 }
5011 5010 /* Build mode parameter header */
5012 5011 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5013 5012 /* 4-byte mode parameter header */
5014 5013 buf[len++] = 0; /* mode data length */
5015 5014 buf[len++] = 0; /* medium type */
5016 5015 buf[len++] = 0; /* dev-specific param */
5017 5016 buf[len++] = bdlen; /* Block Descriptor length */
5018 5017 } else {
5019 5018 /* 8-byte mode parameter header */
5020 5019 buf[len++] = 0; /* mode data length */
5021 5020 buf[len++] = 0;
5022 5021 buf[len++] = 0; /* medium type */
5023 5022 buf[len++] = 0; /* dev-specific param */
5024 5023 if (bdlen == 16)
5025 5024 buf[len++] = 1; /* long lba descriptor */
5026 5025 else
5027 5026 buf[len++] = 0;
5028 5027 buf[len++] = 0;
5029 5028 buf[len++] = 0; /* Block Descriptor length */
5030 5029 buf[len++] = bdlen;
5031 5030 }
5032 5031
5033 5032 sdinfo = sata_get_device_info(
5034 5033 spx->txlt_sata_hba_inst,
5035 5034 &spx->txlt_sata_pkt->satapkt_device);
5036 5035
5037 5036 /* Build block descriptor only if not disabled (DBD) */
5038 5037 if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
5039 5038 /* Block descriptor - direct-access device format */
5040 5039 if (bdlen == 8) {
5041 5040 /* build regular block descriptor */
5042 5041 buf[len++] =
5043 5042 (sdinfo->satadrv_capacity >> 24) & 0xff;
5044 5043 buf[len++] =
5045 5044 (sdinfo->satadrv_capacity >> 16) & 0xff;
5046 5045 buf[len++] =
5047 5046 (sdinfo->satadrv_capacity >> 8) & 0xff;
5048 5047 buf[len++] = sdinfo->satadrv_capacity & 0xff;
5049 5048 buf[len++] = 0; /* density code */
5050 5049 buf[len++] = 0;
5051 5050 if (sdinfo->satadrv_type ==
5052 5051 SATA_DTYPE_ATADISK)
5053 5052 buf[len++] = 2;
5054 5053 else
5055 5054 /* ATAPI */
5056 5055 buf[len++] = 8;
5057 5056 buf[len++] = 0;
5058 5057 } else if (bdlen == 16) {
5059 5058 /* Long LBA Accepted */
5060 5059 /* build long lba block descriptor */
5061 5060 #ifndef __lock_lint
5062 5061 buf[len++] =
5063 5062 (sdinfo->satadrv_capacity >> 56) & 0xff;
5064 5063 buf[len++] =
5065 5064 (sdinfo->satadrv_capacity >> 48) & 0xff;
5066 5065 buf[len++] =
5067 5066 (sdinfo->satadrv_capacity >> 40) & 0xff;
5068 5067 buf[len++] =
5069 5068 (sdinfo->satadrv_capacity >> 32) & 0xff;
5070 5069 #endif
5071 5070 buf[len++] =
5072 5071 (sdinfo->satadrv_capacity >> 24) & 0xff;
5073 5072 buf[len++] =
5074 5073 (sdinfo->satadrv_capacity >> 16) & 0xff;
5075 5074 buf[len++] =
5076 5075 (sdinfo->satadrv_capacity >> 8) & 0xff;
5077 5076 buf[len++] = sdinfo->satadrv_capacity & 0xff;
5078 5077 buf[len++] = 0;
5079 5078 buf[len++] = 0; /* density code */
5080 5079 buf[len++] = 0;
5081 5080 buf[len++] = 0;
5082 5081 if (sdinfo->satadrv_type ==
5083 5082 SATA_DTYPE_ATADISK)
5084 5083 buf[len++] = 2;
5085 5084 else
5086 5085 /* ATAPI */
5087 5086 buf[len++] = 8;
5088 5087 buf[len++] = 0;
5089 5088 }
5090 5089 }
5091 5090
5092 5091 sata_id = &sdinfo->satadrv_id;
5093 5092
5094 5093 /*
5095 5094 * Add requested pages.
5096 5095 * Page 3 and 4 are obsolete and we are not supporting them.
5097 5096 * We deal now with:
5098 5097 * caching (read/write cache control).
5099 5098 * We should eventually deal with following mode pages:
5100 5099 * error recovery (0x01),
5101 5100 * power condition (0x1a),
5102 5101 * exception control page (enables SMART) (0x1c),
5103 5102 * enclosure management (ses),
5104 5103 * protocol-specific port mode (port control).
5105 5104 */
5106 5105 switch (scsipkt->pkt_cdbp[2] & 0x3f) {
5107 5106 case MODEPAGE_RW_ERRRECOV:
5108 5107 /* DAD_MODE_ERR_RECOV */
5109 5108 /* R/W recovery */
5110 5109 len += sata_build_msense_page_1(sdinfo, pc, buf+len);
5111 5110 break;
5112 5111 case MODEPAGE_CACHING:
5113 5112 /* DAD_MODE_CACHE */
5114 5113 /* Reject not supported request for saved parameters */
5115 5114 if (pc == 3) {
5116 5115 *scsipkt->pkt_scbp = STATUS_CHECK;
5117 5116 sense = sata_arq_sense(spx);
5118 5117 sense->es_key = KEY_ILLEGAL_REQUEST;
5119 5118 sense->es_add_code =
5120 5119 SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
5121 5120 goto done;
5122 5121 }
5123 5122
5124 5123 /* caching */
5125 5124 len += sata_build_msense_page_8(sdinfo, pc, buf+len);
5126 5125 break;
5127 5126 case MODEPAGE_INFO_EXCPT:
5128 5127 /* exception cntrl */
5129 5128 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
5130 5129 len += sata_build_msense_page_1c(sdinfo, pc,
5131 5130 buf+len);
5132 5131 }
5133 5132 else
5134 5133 goto err;
5135 5134 break;
5136 5135 case MODEPAGE_POWER_COND:
5137 5136 /* DAD_MODE_POWER_COND */
5138 5137 /* power condition */
5139 5138 len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
5140 5139 break;
5141 5140
5142 5141 case MODEPAGE_ACOUSTIC_MANAG:
5143 5142 /* acoustic management */
5144 5143 len += sata_build_msense_page_30(sdinfo, pc, buf+len);
5145 5144 break;
5146 5145 case MODEPAGE_ALLPAGES:
5147 5146 /* all pages */
5148 5147 len += sata_build_msense_page_1(sdinfo, pc, buf+len);
5149 5148 len += sata_build_msense_page_8(sdinfo, pc, buf+len);
5150 5149 len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
5151 5150 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
5152 5151 len += sata_build_msense_page_1c(sdinfo, pc,
5153 5152 buf+len);
5154 5153 }
5155 5154 len += sata_build_msense_page_30(sdinfo, pc, buf+len);
5156 5155 break;
5157 5156 default:
5158 5157 err:
5159 5158 /* Invalid request */
5160 5159 *scsipkt->pkt_scbp = STATUS_CHECK;
5161 5160 sense = sata_arq_sense(spx);
5162 5161 sense->es_key = KEY_ILLEGAL_REQUEST;
5163 5162 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5164 5163 goto done;
5165 5164 }
5166 5165
5167 5166 /* fix total mode data length */
5168 5167 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5169 5168 /* 4-byte mode parameter header */
5170 5169 buf[0] = len - 1; /* mode data length */
5171 5170 } else {
5172 5171 buf[0] = (len -2) >> 8;
5173 5172 buf[1] = (len -2) & 0xff;
5174 5173 }
5175 5174
5176 5175
5177 5176 /* Check allocation length */
5178 5177 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5179 5178 alc_len = scsipkt->pkt_cdbp[4];
5180 5179 } else {
5181 5180 alc_len = scsipkt->pkt_cdbp[7];
5182 5181 alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
5183 5182 }
5184 5183 /*
5185 5184 * We do not check for possible parameters truncation
5186 5185 * (alc_len < len) assuming that the target driver works
5187 5186 * correctly. Just avoiding overrun.
5188 5187 * Copy no more than requested and possible, buffer-wise.
5189 5188 */
5190 5189 count = MIN(alc_len, len);
5191 5190 count = MIN(bp->b_bcount, count);
5192 5191 bcopy(buf, bp->b_un.b_addr, count);
5193 5192
5194 5193 scsipkt->pkt_state |= STATE_XFERRED_DATA;
5195 5194 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
5196 5195 }
5197 5196 *scsipkt->pkt_scbp = STATUS_GOOD;
5198 5197 done:
5199 5198 mutex_exit(cport_mutex);
5200 5199 (void) kmem_free(buf, 1024);
5201 5200
5202 5201 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5203 5202 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5204 5203
5205 5204 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5206 5205 scsipkt->pkt_comp != NULL) {
5207 5206 /* scsi callback required */
5208 5207 if (servicing_interrupt()) {
5209 5208 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5210 5209 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5211 5210 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
5212 5211 return (TRAN_BUSY);
5213 5212 }
5214 5213 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5215 5214 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5216 5215 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
5217 5216 /* Scheduling the callback failed */
5218 5217 return (TRAN_BUSY);
5219 5218 }
5220 5219 }
5221 5220
5222 5221 return (TRAN_ACCEPT);
5223 5222 }
5224 5223
5225 5224
5226 5225 /*
5227 5226 * SATA translate command: Mode Select.
5228 5227 * Translated into appropriate SATA command or emulated.
5229 5228 * Saving parameters is not supported.
5230 5229 * Changing device capacity is not supported (although theoretically
5231 5230 * possible by executing SET FEATURES/SET MAX ADDRESS)
5232 5231 *
5233 5232 * Assumption is that the target driver is working correctly.
5234 5233 *
5235 5234 * More than one SATA command may be executed to perform operations specified
5236 5235 * by mode select pages. The first error terminates further execution.
5237 5236 * Operations performed successully are not backed-up in such case.
5238 5237 *
5239 5238 * NOTE: Implemented pages:
5240 5239 * - caching page
5241 5240 * - informational exception page
5242 5241 * - acoustic management page
5243 5242 * - power condition page
5244 5243 * Caching setup is remembered so it could be re-stored in case of
5245 5244 * an unexpected device reset.
5246 5245 *
5247 5246 * Returns TRAN_XXXX.
5248 5247 * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields.
5249 5248 */
5250 5249
5251 5250 static int
5252 5251 sata_txlt_mode_select(sata_pkt_txlate_t *spx)
5253 5252 {
5254 5253 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5255 5254 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5256 5255 struct scsi_extended_sense *sense;
5257 5256 int len, pagelen, count, pllen;
5258 5257 uint8_t *buf; /* mode select buffer */
5259 5258 int rval, stat, reason;
5260 5259 uint_t nointr_flag;
5261 5260 int dmod = 0;
5262 5261 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5263 5262
5264 5263 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5265 5264 "sata_txlt_mode_select, pc %x page code 0x%02x\n",
5266 5265 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5267 5266 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5268 5267
5269 5268 mutex_enter(cport_mutex);
5270 5269
5271 5270 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
5272 5271 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5273 5272 mutex_exit(cport_mutex);
5274 5273 return (rval);
5275 5274 }
5276 5275
5277 5276 rval = TRAN_ACCEPT;
5278 5277
5279 5278 scsipkt->pkt_reason = CMD_CMPLT;
5280 5279 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5281 5280 STATE_SENT_CMD | STATE_GOT_STATUS;
5282 5281 nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
5283 5282
5284 5283 /* Reject not supported request */
5285 5284 if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
5286 5285 *scsipkt->pkt_scbp = STATUS_CHECK;
5287 5286 sense = sata_arq_sense(spx);
5288 5287 sense->es_key = KEY_ILLEGAL_REQUEST;
5289 5288 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5290 5289 goto done;
5291 5290 }
5292 5291
5293 5292 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
5294 5293 pllen = scsipkt->pkt_cdbp[4];
5295 5294 } else {
5296 5295 pllen = scsipkt->pkt_cdbp[7];
5297 5296 pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
5298 5297 }
5299 5298
5300 5299 *scsipkt->pkt_scbp = STATUS_GOOD; /* Presumed outcome */
5301 5300
5302 5301 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
5303 5302 buf = (uint8_t *)bp->b_un.b_addr;
5304 5303 count = MIN(bp->b_bcount, pllen);
5305 5304 scsipkt->pkt_state |= STATE_XFERRED_DATA;
5306 5305 scsipkt->pkt_resid = 0;
5307 5306 pllen = count;
5308 5307
5309 5308 /*
5310 5309 * Check the header to skip the block descriptor(s) - we
5311 5310 * do not support setting device capacity.
5312 5311 * Existing macros do not recognize long LBA dscriptor,
5313 5312 * hence manual calculation.
5314 5313 */
5315 5314 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
5316 5315 /* 6-bytes CMD, 4 bytes header */
5317 5316 if (count <= 4)
5318 5317 goto done; /* header only */
5319 5318 len = buf[3] + 4;
5320 5319 } else {
5321 5320 /* 10-bytes CMD, 8 bytes header */
5322 5321 if (count <= 8)
5323 5322 goto done; /* header only */
5324 5323 len = buf[6];
5325 5324 len = (len << 8) + buf[7] + 8;
5326 5325 }
5327 5326 if (len >= count)
5328 5327 goto done; /* header + descriptor(s) only */
5329 5328
5330 5329 pllen -= len; /* remaining data length */
5331 5330
5332 5331 /*
5333 5332 * We may be executing SATA command and want to execute it
5334 5333 * in SYNCH mode, regardless of scsi_pkt setting.
5335 5334 * Save scsi_pkt setting and indicate SYNCH mode
5336 5335 */
5337 5336 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5338 5337 scsipkt->pkt_comp != NULL) {
5339 5338 scsipkt->pkt_flags |= FLAG_NOINTR;
5340 5339 }
5341 5340 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
5342 5341
5343 5342 /*
5344 5343 * len is now the offset to a first mode select page
5345 5344 * Process all pages
5346 5345 */
5347 5346 while (pllen > 0) {
5348 5347 switch ((int)buf[len]) {
5349 5348 case MODEPAGE_CACHING:
5350 5349 /* No support for SP (saving) */
5351 5350 if (scsipkt->pkt_cdbp[1] & 0x01) {
5352 5351 *scsipkt->pkt_scbp = STATUS_CHECK;
5353 5352 sense = sata_arq_sense(spx);
5354 5353 sense->es_key = KEY_ILLEGAL_REQUEST;
5355 5354 sense->es_add_code =
5356 5355 SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5357 5356 goto done;
5358 5357 }
5359 5358 stat = sata_mode_select_page_8(spx,
5360 5359 (struct mode_cache_scsi3 *)&buf[len],
5361 5360 pllen, &pagelen, &rval, &dmod);
5362 5361 /*
5363 5362 * The pagelen value indicates the number of
5364 5363 * parameter bytes already processed.
5365 5364 * The rval is the return value from
5366 5365 * sata_tran_start().
5367 5366 * The stat indicates the overall status of
5368 5367 * the operation(s).
5369 5368 */
5370 5369 if (stat != SATA_SUCCESS)
5371 5370 /*
5372 5371 * Page processing did not succeed -
5373 5372 * all error info is already set-up,
5374 5373 * just return
5375 5374 */
5376 5375 pllen = 0; /* this breaks the loop */
5377 5376 else {
5378 5377 len += pagelen;
5379 5378 pllen -= pagelen;
5380 5379 }
5381 5380 break;
5382 5381
5383 5382 case MODEPAGE_INFO_EXCPT:
5384 5383 stat = sata_mode_select_page_1c(spx,
5385 5384 (struct mode_info_excpt_page *)&buf[len],
5386 5385 pllen, &pagelen, &rval, &dmod);
5387 5386 /*
5388 5387 * The pagelen value indicates the number of
5389 5388 * parameter bytes already processed.
5390 5389 * The rval is the return value from
5391 5390 * sata_tran_start().
5392 5391 * The stat indicates the overall status of
5393 5392 * the operation(s).
5394 5393 */
5395 5394 if (stat != SATA_SUCCESS)
5396 5395 /*
5397 5396 * Page processing did not succeed -
5398 5397 * all error info is already set-up,
5399 5398 * just return
5400 5399 */
5401 5400 pllen = 0; /* this breaks the loop */
5402 5401 else {
5403 5402 len += pagelen;
5404 5403 pllen -= pagelen;
5405 5404 }
5406 5405 break;
5407 5406
5408 5407 case MODEPAGE_ACOUSTIC_MANAG:
5409 5408 stat = sata_mode_select_page_30(spx,
5410 5409 (struct mode_acoustic_management *)
5411 5410 &buf[len], pllen, &pagelen, &rval, &dmod);
5412 5411 /*
5413 5412 * The pagelen value indicates the number of
5414 5413 * parameter bytes already processed.
5415 5414 * The rval is the return value from
5416 5415 * sata_tran_start().
5417 5416 * The stat indicates the overall status of
5418 5417 * the operation(s).
5419 5418 */
5420 5419 if (stat != SATA_SUCCESS)
5421 5420 /*
5422 5421 * Page processing did not succeed -
5423 5422 * all error info is already set-up,
5424 5423 * just return
5425 5424 */
5426 5425 pllen = 0; /* this breaks the loop */
5427 5426 else {
5428 5427 len += pagelen;
5429 5428 pllen -= pagelen;
5430 5429 }
5431 5430
5432 5431 break;
5433 5432 case MODEPAGE_POWER_COND:
5434 5433 stat = sata_mode_select_page_1a(spx,
5435 5434 (struct mode_info_power_cond *)&buf[len],
5436 5435 pllen, &pagelen, &rval, &dmod);
5437 5436 /*
5438 5437 * The pagelen value indicates the number of
5439 5438 * parameter bytes already processed.
5440 5439 * The rval is the return value from
5441 5440 * sata_tran_start().
5442 5441 * The stat indicates the overall status of
5443 5442 * the operation(s).
5444 5443 */
5445 5444 if (stat != SATA_SUCCESS)
5446 5445 /*
5447 5446 * Page processing did not succeed -
5448 5447 * all error info is already set-up,
5449 5448 * just return
5450 5449 */
5451 5450 pllen = 0; /* this breaks the loop */
5452 5451 else {
5453 5452 len += pagelen;
5454 5453 pllen -= pagelen;
5455 5454 }
5456 5455 break;
5457 5456 default:
5458 5457 *scsipkt->pkt_scbp = STATUS_CHECK;
5459 5458 sense = sata_arq_sense(spx);
5460 5459 sense->es_key = KEY_ILLEGAL_REQUEST;
5461 5460 sense->es_add_code =
5462 5461 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
5463 5462 goto done;
5464 5463 }
5465 5464 }
5466 5465 }
5467 5466 done:
5468 5467 mutex_exit(cport_mutex);
5469 5468 /*
5470 5469 * If device parameters were modified, fetch and store the new
5471 5470 * Identify Device data. Since port mutex could have been released
5472 5471 * for accessing HBA driver, we need to re-check device existence.
5473 5472 */
5474 5473 if (dmod != 0) {
5475 5474 sata_drive_info_t new_sdinfo, *sdinfo;
5476 5475 int rv = 0;
5477 5476
5478 5477 /*
5479 5478 * Following statement has to be changed if this function is
5480 5479 * used for devices other than SATA hard disks.
5481 5480 */
5482 5481 new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
5483 5482
5484 5483 new_sdinfo.satadrv_addr =
5485 5484 spx->txlt_sata_pkt->satapkt_device.satadev_addr;
5486 5485 rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
5487 5486 &new_sdinfo);
5488 5487
5489 5488 mutex_enter(cport_mutex);
5490 5489 /*
5491 5490 * Since port mutex could have been released when
5492 5491 * accessing HBA driver, we need to re-check that the
5493 5492 * framework still holds the device info structure.
5494 5493 */
5495 5494 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
5496 5495 &spx->txlt_sata_pkt->satapkt_device);
5497 5496 if (sdinfo != NULL) {
5498 5497 /*
5499 5498 * Device still has info structure in the
5500 5499 * sata framework. Copy newly fetched info
5501 5500 */
5502 5501 if (rv == 0) {
5503 5502 sdinfo->satadrv_id = new_sdinfo.satadrv_id;
5504 5503 sata_save_drive_settings(sdinfo);
5505 5504 } else {
5506 5505 /*
5507 5506 * Could not fetch new data - invalidate
5508 5507 * sata_drive_info. That makes device
5509 5508 * unusable.
5510 5509 */
5511 5510 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
5512 5511 sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
5513 5512 }
5514 5513 }
5515 5514 if (rv != 0 || sdinfo == NULL) {
5516 5515 /*
5517 5516 * This changes the overall mode select completion
5518 5517 * reason to a failed one !!!!!
5519 5518 */
5520 5519 *scsipkt->pkt_scbp = STATUS_CHECK;
5521 5520 sense = sata_arq_sense(spx);
5522 5521 scsipkt->pkt_reason = CMD_INCOMPLETE;
5523 5522 rval = TRAN_ACCEPT;
5524 5523 }
5525 5524 mutex_exit(cport_mutex);
5526 5525 }
5527 5526 /* Restore the scsi pkt flags */
5528 5527 scsipkt->pkt_flags &= ~FLAG_NOINTR;
5529 5528 scsipkt->pkt_flags |= nointr_flag;
5530 5529
5531 5530 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5532 5531 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5533 5532
5534 5533 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5535 5534 scsipkt->pkt_comp != NULL) {
5536 5535 /* scsi callback required */
5537 5536 if (servicing_interrupt()) {
5538 5537 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5539 5538 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5540 5539 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
5541 5540 return (TRAN_BUSY);
5542 5541 }
5543 5542 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5544 5543 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5545 5544 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
5546 5545 /* Scheduling the callback failed */
5547 5546 return (TRAN_BUSY);
5548 5547 }
5549 5548 }
5550 5549
5551 5550 return (rval);
5552 5551 }
5553 5552
5554 5553 /*
5555 5554 * Translate command: ATA Pass Through
5556 5555 * Incomplete implementation. Only supports No-Data, PIO Data-In, and
5557 5556 * PIO Data-Out protocols. Also supports CK_COND bit.
5558 5557 *
5559 5558 * Mapping of the incoming CDB bytes to the outgoing satacmd bytes is
5560 5559 * described in Table 111 of SAT-2 (Draft 9).
5561 5560 */
5562 5561 static int
5563 5562 sata_txlt_ata_pass_thru(sata_pkt_txlate_t *spx)
5564 5563 {
5565 5564 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5566 5565 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5567 5566 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5568 5567 int extend;
5569 5568 uint64_t lba;
5570 5569 uint16_t feature, sec_count;
5571 5570 int t_len, synch;
5572 5571 int rval, reason;
5573 5572 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5574 5573
5575 5574 mutex_enter(cport_mutex);
5576 5575
5577 5576 rval = sata_txlt_generic_pkt_info(spx, &reason, 1);
5578 5577 if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5579 5578 mutex_exit(cport_mutex);
5580 5579 return (rval);
5581 5580 }
5582 5581
5583 5582 /* T_DIR bit */
5584 5583 if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_T_DIR)
5585 5584 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
5586 5585 else
5587 5586 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
5588 5587
5589 5588 /* MULTIPLE_COUNT field. If non-zero, invalid command (for now). */
5590 5589 if (((scsipkt->pkt_cdbp[1] >> 5) & 0x7) != 0) {
5591 5590 mutex_exit(cport_mutex);
5592 5591 return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5593 5592 }
5594 5593
5595 5594 /* OFFLINE field. If non-zero, invalid command (for now). */
5596 5595 if (((scsipkt->pkt_cdbp[2] >> 6) & 0x3) != 0) {
5597 5596 mutex_exit(cport_mutex);
5598 5597 return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5599 5598 }
5600 5599
5601 5600 /* PROTOCOL field */
5602 5601 switch ((scsipkt->pkt_cdbp[1] >> 1) & 0xf) {
5603 5602 case SATL_APT_P_HW_RESET:
5604 5603 case SATL_APT_P_SRST:
5605 5604 case SATL_APT_P_DMA:
5606 5605 case SATL_APT_P_DMA_QUEUED:
5607 5606 case SATL_APT_P_DEV_DIAG:
5608 5607 case SATL_APT_P_DEV_RESET:
5609 5608 case SATL_APT_P_UDMA_IN:
5610 5609 case SATL_APT_P_UDMA_OUT:
5611 5610 case SATL_APT_P_FPDMA:
5612 5611 case SATL_APT_P_RET_RESP:
5613 5612 /* Not yet implemented */
5614 5613 default:
5615 5614 mutex_exit(cport_mutex);
5616 5615 return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5617 5616
5618 5617 case SATL_APT_P_NON_DATA:
5619 5618 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
5620 5619 break;
5621 5620
5622 5621 case SATL_APT_P_PIO_DATA_IN:
5623 5622 /* If PROTOCOL disagrees with T_DIR, invalid command */
5624 5623 if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_WRITE) {
5625 5624 mutex_exit(cport_mutex);
5626 5625 return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5627 5626 }
5628 5627
5629 5628 /* if there is a buffer, release its DMA resources */
5630 5629 if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) {
5631 5630 sata_scsi_dmafree(NULL, scsipkt);
5632 5631 } else {
5633 5632 /* if there is no buffer, how do you PIO in? */
5634 5633 mutex_exit(cport_mutex);
5635 5634 return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5636 5635 }
5637 5636
5638 5637 break;
5639 5638
5640 5639 case SATL_APT_P_PIO_DATA_OUT:
5641 5640 /* If PROTOCOL disagrees with T_DIR, invalid command */
5642 5641 if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_READ) {
5643 5642 mutex_exit(cport_mutex);
5644 5643 return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5645 5644 }
5646 5645
5647 5646 /* if there is a buffer, release its DMA resources */
5648 5647 if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) {
5649 5648 sata_scsi_dmafree(NULL, scsipkt);
5650 5649 } else {
5651 5650 /* if there is no buffer, how do you PIO out? */
5652 5651 mutex_exit(cport_mutex);
5653 5652 return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5654 5653 }
5655 5654
5656 5655 break;
5657 5656 }
5658 5657
5659 5658 /* Parse the ATA cmd fields, transfer some straight to the satacmd */
5660 5659 switch ((uint_t)scsipkt->pkt_cdbp[0]) {
5661 5660 case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12:
5662 5661 feature = scsipkt->pkt_cdbp[3];
5663 5662
5664 5663 sec_count = scsipkt->pkt_cdbp[4];
5665 5664
5666 5665 lba = scsipkt->pkt_cdbp[8] & 0xf;
5667 5666 lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5668 5667 lba = (lba << 8) | scsipkt->pkt_cdbp[6];
5669 5668 lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5670 5669
5671 5670 scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] & 0xf0;
5672 5671 scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[9];
5673 5672
5674 5673 break;
5675 5674
5676 5675 case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16:
5677 5676 if (scsipkt->pkt_cdbp[1] & SATL_APT_BM_EXTEND) {
5678 5677 extend = 1;
5679 5678
5680 5679 feature = scsipkt->pkt_cdbp[3];
5681 5680 feature = (feature << 8) | scsipkt->pkt_cdbp[4];
5682 5681
5683 5682 sec_count = scsipkt->pkt_cdbp[5];
5684 5683 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[6];
5685 5684
5686 5685 lba = scsipkt->pkt_cdbp[11];
5687 5686 lba = (lba << 8) | scsipkt->pkt_cdbp[12];
5688 5687 lba = (lba << 8) | scsipkt->pkt_cdbp[9];
5689 5688 lba = (lba << 8) | scsipkt->pkt_cdbp[10];
5690 5689 lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5691 5690 lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5692 5691
5693 5692 scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13];
5694 5693 scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14];
5695 5694 } else {
5696 5695 feature = scsipkt->pkt_cdbp[3];
5697 5696
5698 5697 sec_count = scsipkt->pkt_cdbp[5];
5699 5698
5700 5699 lba = scsipkt->pkt_cdbp[13] & 0xf;
5701 5700 lba = (lba << 8) | scsipkt->pkt_cdbp[12];
5702 5701 lba = (lba << 8) | scsipkt->pkt_cdbp[10];
5703 5702 lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5704 5703
5705 5704 scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] &
5706 5705 0xf0;
5707 5706 scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14];
5708 5707 }
5709 5708
5710 5709 break;
5711 5710 }
5712 5711
5713 5712 /* CK_COND bit */
5714 5713 if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) {
5715 5714 if (extend) {
5716 5715 scmd->satacmd_flags.sata_copy_out_sec_count_msb = 1;
5717 5716 scmd->satacmd_flags.sata_copy_out_lba_low_msb = 1;
5718 5717 scmd->satacmd_flags.sata_copy_out_lba_mid_msb = 1;
5719 5718 scmd->satacmd_flags.sata_copy_out_lba_high_msb = 1;
5720 5719 }
5721 5720
5722 5721 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1;
5723 5722 scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1;
5724 5723 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1;
5725 5724 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1;
5726 5725 scmd->satacmd_flags.sata_copy_out_device_reg = 1;
5727 5726 scmd->satacmd_flags.sata_copy_out_error_reg = 1;
5728 5727 }
5729 5728
5730 5729 /* Transfer remaining parsed ATA cmd values to the satacmd */
5731 5730 if (extend) {
5732 5731 scmd->satacmd_addr_type = ATA_ADDR_LBA48;
5733 5732
5734 5733 scmd->satacmd_features_reg_ext = (feature >> 8) & 0xff;
5735 5734 scmd->satacmd_sec_count_msb = (sec_count >> 8) & 0xff;
5736 5735 scmd->satacmd_lba_low_msb = (lba >> 8) & 0xff;
5737 5736 scmd->satacmd_lba_mid_msb = (lba >> 8) & 0xff;
5738 5737 scmd->satacmd_lba_high_msb = lba >> 40;
5739 5738 } else {
5740 5739 scmd->satacmd_addr_type = ATA_ADDR_LBA28;
5741 5740
5742 5741 scmd->satacmd_features_reg_ext = 0;
5743 5742 scmd->satacmd_sec_count_msb = 0;
5744 5743 scmd->satacmd_lba_low_msb = 0;
5745 5744 scmd->satacmd_lba_mid_msb = 0;
5746 5745 scmd->satacmd_lba_high_msb = 0;
5747 5746 }
5748 5747
5749 5748 scmd->satacmd_features_reg = feature & 0xff;
5750 5749 scmd->satacmd_sec_count_lsb = sec_count & 0xff;
5751 5750 scmd->satacmd_lba_low_lsb = lba & 0xff;
5752 5751 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
5753 5752 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
5754 5753
5755 5754 /* Determine transfer length */
5756 5755 switch (scsipkt->pkt_cdbp[2] & 0x3) { /* T_LENGTH field */
5757 5756 case 1:
5758 5757 t_len = feature;
5759 5758 break;
5760 5759 case 2:
5761 5760 t_len = sec_count;
5762 5761 break;
5763 5762 default:
5764 5763 t_len = 0;
5765 5764 break;
5766 5765 }
5767 5766
5768 5767 /* Adjust transfer length for the Byte Block bit */
5769 5768 if ((scsipkt->pkt_cdbp[2] >> 2) & 1)
5770 5769 t_len *= SATA_DISK_SECTOR_SIZE;
5771 5770
5772 5771 /* Start processing command */
5773 5772 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5774 5773 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_apt_completion;
5775 5774 synch = FALSE;
5776 5775 } else {
5777 5776 synch = TRUE;
5778 5777 }
5779 5778
5780 5779 if (sata_hba_start(spx, &rval) != 0) {
5781 5780 mutex_exit(cport_mutex);
5782 5781 return (rval);
5783 5782 }
5784 5783
5785 5784 mutex_exit(cport_mutex);
5786 5785
5787 5786 if (synch) {
5788 5787 sata_txlt_apt_completion(spx->txlt_sata_pkt);
5789 5788 }
5790 5789
5791 5790 return (TRAN_ACCEPT);
5792 5791 }
5793 5792
5794 5793 /*
5795 5794 * Translate command: Log Sense
5796 5795 */
5797 5796 static int
5798 5797 sata_txlt_log_sense(sata_pkt_txlate_t *spx)
5799 5798 {
5800 5799 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5801 5800 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5802 5801 sata_drive_info_t *sdinfo;
5803 5802 struct scsi_extended_sense *sense;
5804 5803 int len, count, alc_len;
5805 5804 int pc; /* Page Control code */
5806 5805 int page_code; /* Page code */
5807 5806 uint8_t *buf; /* log sense buffer */
5808 5807 int rval, reason;
5809 5808 #define MAX_LOG_SENSE_PAGE_SIZE 512
5810 5809 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5811 5810
5812 5811 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5813 5812 "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
5814 5813 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5815 5814 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5816 5815
5817 5816 if (servicing_interrupt()) {
5818 5817 buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_NOSLEEP);
5819 5818 if (buf == NULL) {
5820 5819 return (TRAN_BUSY);
5821 5820 }
5822 5821 } else {
5823 5822 buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
5824 5823 }
5825 5824
5826 5825 mutex_enter(cport_mutex);
5827 5826
5828 5827 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
5829 5828 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5830 5829 mutex_exit(cport_mutex);
5831 5830 kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
5832 5831 return (rval);
5833 5832 }
5834 5833
5835 5834 scsipkt->pkt_reason = CMD_CMPLT;
5836 5835 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5837 5836 STATE_SENT_CMD | STATE_GOT_STATUS;
5838 5837
5839 5838 pc = scsipkt->pkt_cdbp[2] >> 6;
5840 5839 page_code = scsipkt->pkt_cdbp[2] & 0x3f;
5841 5840
5842 5841 /* Reject not supported request for all but cumulative values */
5843 5842 switch (pc) {
5844 5843 case PC_CUMULATIVE_VALUES:
5845 5844 break;
5846 5845 default:
5847 5846 *scsipkt->pkt_scbp = STATUS_CHECK;
5848 5847 sense = sata_arq_sense(spx);
5849 5848 sense->es_key = KEY_ILLEGAL_REQUEST;
5850 5849 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5851 5850 goto done;
5852 5851 }
5853 5852
5854 5853 switch (page_code) {
5855 5854 case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5856 5855 case PAGE_CODE_SELF_TEST_RESULTS:
5857 5856 case PAGE_CODE_INFORMATION_EXCEPTIONS:
5858 5857 case PAGE_CODE_SMART_READ_DATA:
5859 5858 case PAGE_CODE_START_STOP_CYCLE_COUNTER:
5860 5859 break;
5861 5860 default:
5862 5861 *scsipkt->pkt_scbp = STATUS_CHECK;
5863 5862 sense = sata_arq_sense(spx);
5864 5863 sense->es_key = KEY_ILLEGAL_REQUEST;
5865 5864 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5866 5865 goto done;
5867 5866 }
5868 5867
5869 5868 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
5870 5869 /*
5871 5870 * Because log sense uses local buffers for data retrieval from
5872 5871 * the devices and sets the data programatically in the
5873 5872 * original specified buffer, release preallocated DMA
5874 5873 * resources before storing data in the original buffer,
5875 5874 * so no unwanted DMA sync would take place.
5876 5875 */
5877 5876 sata_id_t *sata_id;
5878 5877
5879 5878 sata_scsi_dmafree(NULL, scsipkt);
5880 5879
5881 5880 len = 0;
5882 5881
5883 5882 /* Build log parameter header */
5884 5883 buf[len++] = page_code; /* page code as in the CDB */
5885 5884 buf[len++] = 0; /* reserved */
5886 5885 buf[len++] = 0; /* Zero out page length for now (MSB) */
5887 5886 buf[len++] = 0; /* (LSB) */
5888 5887
5889 5888 sdinfo = sata_get_device_info(
5890 5889 spx->txlt_sata_hba_inst,
5891 5890 &spx->txlt_sata_pkt->satapkt_device);
5892 5891
5893 5892 /*
5894 5893 * Add requested pages.
5895 5894 */
5896 5895 switch (page_code) {
5897 5896 case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5898 5897 len = sata_build_lsense_page_0(sdinfo, buf + len);
5899 5898 break;
5900 5899 case PAGE_CODE_SELF_TEST_RESULTS:
5901 5900 sata_id = &sdinfo->satadrv_id;
5902 5901 if ((! (sata_id->ai_cmdset84 &
5903 5902 SATA_SMART_SELF_TEST_SUPPORTED)) ||
5904 5903 (! (sata_id->ai_features87 &
5905 5904 SATA_SMART_SELF_TEST_SUPPORTED))) {
5906 5905 *scsipkt->pkt_scbp = STATUS_CHECK;
5907 5906 sense = sata_arq_sense(spx);
5908 5907 sense->es_key = KEY_ILLEGAL_REQUEST;
5909 5908 sense->es_add_code =
5910 5909 SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5911 5910
5912 5911 goto done;
5913 5912 }
5914 5913 len = sata_build_lsense_page_10(sdinfo, buf + len,
5915 5914 spx->txlt_sata_hba_inst);
5916 5915 break;
5917 5916 case PAGE_CODE_INFORMATION_EXCEPTIONS:
5918 5917 sata_id = &sdinfo->satadrv_id;
5919 5918 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5920 5919 *scsipkt->pkt_scbp = STATUS_CHECK;
5921 5920 sense = sata_arq_sense(spx);
5922 5921 sense->es_key = KEY_ILLEGAL_REQUEST;
5923 5922 sense->es_add_code =
5924 5923 SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5925 5924
5926 5925 goto done;
5927 5926 }
5928 5927 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5929 5928 *scsipkt->pkt_scbp = STATUS_CHECK;
5930 5929 sense = sata_arq_sense(spx);
5931 5930 sense->es_key = KEY_ABORTED_COMMAND;
5932 5931 sense->es_add_code =
5933 5932 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5934 5933 sense->es_qual_code =
5935 5934 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5936 5935
5937 5936 goto done;
5938 5937 }
5939 5938
5940 5939 len = sata_build_lsense_page_2f(sdinfo, buf + len,
5941 5940 spx->txlt_sata_hba_inst);
5942 5941 break;
5943 5942 case PAGE_CODE_SMART_READ_DATA:
5944 5943 sata_id = &sdinfo->satadrv_id;
5945 5944 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5946 5945 *scsipkt->pkt_scbp = STATUS_CHECK;
5947 5946 sense = sata_arq_sense(spx);
5948 5947 sense->es_key = KEY_ILLEGAL_REQUEST;
5949 5948 sense->es_add_code =
5950 5949 SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5951 5950
5952 5951 goto done;
5953 5952 }
5954 5953 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5955 5954 *scsipkt->pkt_scbp = STATUS_CHECK;
5956 5955 sense = sata_arq_sense(spx);
5957 5956 sense->es_key = KEY_ABORTED_COMMAND;
5958 5957 sense->es_add_code =
5959 5958 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5960 5959 sense->es_qual_code =
5961 5960 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5962 5961
5963 5962 goto done;
5964 5963 }
5965 5964
5966 5965 /* This page doesn't include a page header */
5967 5966 len = sata_build_lsense_page_30(sdinfo, buf,
5968 5967 spx->txlt_sata_hba_inst);
5969 5968 goto no_header;
5970 5969 case PAGE_CODE_START_STOP_CYCLE_COUNTER:
5971 5970 sata_id = &sdinfo->satadrv_id;
5972 5971 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5973 5972 *scsipkt->pkt_scbp = STATUS_CHECK;
5974 5973 sense = sata_arq_sense(spx);
5975 5974 sense->es_key = KEY_ILLEGAL_REQUEST;
5976 5975 sense->es_add_code =
5977 5976 SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5978 5977
5979 5978 goto done;
5980 5979 }
5981 5980 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5982 5981 *scsipkt->pkt_scbp = STATUS_CHECK;
5983 5982 sense = sata_arq_sense(spx);
5984 5983 sense->es_key = KEY_ABORTED_COMMAND;
5985 5984 sense->es_add_code =
5986 5985 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5987 5986 sense->es_qual_code =
5988 5987 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5989 5988
5990 5989 goto done;
5991 5990 }
5992 5991 len = sata_build_lsense_page_0e(sdinfo, buf, spx);
5993 5992 goto no_header;
5994 5993 default:
5995 5994 /* Invalid request */
5996 5995 *scsipkt->pkt_scbp = STATUS_CHECK;
5997 5996 sense = sata_arq_sense(spx);
5998 5997 sense->es_key = KEY_ILLEGAL_REQUEST;
5999 5998 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6000 5999 goto done;
6001 6000 }
6002 6001
6003 6002 /* set parameter log sense data length */
6004 6003 buf[2] = len >> 8; /* log sense length (MSB) */
6005 6004 buf[3] = len & 0xff; /* log sense length (LSB) */
6006 6005
6007 6006 len += SCSI_LOG_PAGE_HDR_LEN;
6008 6007 ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
6009 6008
6010 6009 no_header:
6011 6010 /* Check allocation length */
6012 6011 alc_len = scsipkt->pkt_cdbp[7];
6013 6012 alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
6014 6013
6015 6014 /*
6016 6015 * We do not check for possible parameters truncation
6017 6016 * (alc_len < len) assuming that the target driver works
6018 6017 * correctly. Just avoiding overrun.
6019 6018 * Copy no more than requested and possible, buffer-wise.
6020 6019 */
6021 6020 count = MIN(alc_len, len);
6022 6021 count = MIN(bp->b_bcount, count);
6023 6022 bcopy(buf, bp->b_un.b_addr, count);
6024 6023
6025 6024 scsipkt->pkt_state |= STATE_XFERRED_DATA;
6026 6025 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
6027 6026 }
6028 6027 *scsipkt->pkt_scbp = STATUS_GOOD;
6029 6028 done:
6030 6029 mutex_exit(cport_mutex);
6031 6030 (void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
6032 6031
6033 6032 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6034 6033 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6035 6034
6036 6035 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6037 6036 scsipkt->pkt_comp != NULL) {
6038 6037 /* scsi callback required */
6039 6038 if (servicing_interrupt()) {
6040 6039 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6041 6040 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6042 6041 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
6043 6042 return (TRAN_BUSY);
6044 6043 }
6045 6044 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6046 6045 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6047 6046 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
6048 6047 /* Scheduling the callback failed */
6049 6048 return (TRAN_BUSY);
6050 6049 }
6051 6050 }
6052 6051
6053 6052 return (TRAN_ACCEPT);
6054 6053 }
6055 6054
6056 6055 /*
6057 6056 * Translate command: Log Select
6058 6057 * Not implemented at this time - returns invalid command response.
6059 6058 */
6060 6059 static int
6061 6060 sata_txlt_log_select(sata_pkt_txlate_t *spx)
6062 6061 {
6063 6062 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6064 6063 "sata_txlt_log_select\n", NULL);
6065 6064
6066 6065 return (sata_txlt_invalid_command(spx));
6067 6066 }
6068 6067
6069 6068
6070 6069 /*
6071 6070 * Translate command: Read (various types).
6072 6071 * Translated into appropriate type of ATA READ command
6073 6072 * for SATA hard disks.
6074 6073 * Both the device capabilities and requested operation mode are
6075 6074 * considered.
6076 6075 *
6077 6076 * Following scsi cdb fields are ignored:
6078 6077 * rdprotect, dpo, fua, fua_nv, group_number.
6079 6078 *
6080 6079 * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
6081 6080 * enable variable sata_func_enable), the capability of the controller and
6082 6081 * capability of a device are checked and if both support queueing, read
6083 6082 * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
6084 6083 * command rather than plain READ_XXX command.
6085 6084 * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
6086 6085 * both the controller and device suport such functionality, the read
6087 6086 * request will be translated to READ_FPDMA_QUEUED command.
6088 6087 * In both cases the maximum queue depth is derived as minimum of:
6089 6088 * HBA capability,device capability and sata_max_queue_depth variable setting.
6090 6089 * The value passed to HBA driver is decremented by 1, because only 5 bits are
6091 6090 * used to pass max queue depth value, and the maximum possible queue depth
6092 6091 * is 32.
6093 6092 *
6094 6093 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6095 6094 * appropriate values in scsi_pkt fields.
6096 6095 */
6097 6096 static int
6098 6097 sata_txlt_read(sata_pkt_txlate_t *spx)
6099 6098 {
6100 6099 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6101 6100 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6102 6101 sata_drive_info_t *sdinfo;
6103 6102 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
6104 6103 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6105 6104 uint16_t sec_count;
6106 6105 uint64_t lba;
6107 6106 int rval, reason;
6108 6107 int synch;
6109 6108
6110 6109 mutex_enter(cport_mutex);
6111 6110
6112 6111 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
6113 6112 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6114 6113 mutex_exit(cport_mutex);
6115 6114 return (rval);
6116 6115 }
6117 6116
6118 6117 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6119 6118 &spx->txlt_sata_pkt->satapkt_device);
6120 6119
6121 6120 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
6122 6121 /*
6123 6122 * Extract LBA and sector count from scsi CDB.
6124 6123 */
6125 6124 switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6126 6125 case SCMD_READ:
6127 6126 /* 6-byte scsi read cmd : 0x08 */
6128 6127 lba = (scsipkt->pkt_cdbp[1] & 0x1f);
6129 6128 lba = (lba << 8) | scsipkt->pkt_cdbp[2];
6130 6129 lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6131 6130 sec_count = scsipkt->pkt_cdbp[4];
6132 6131 /* sec_count 0 will be interpreted as 256 by a device */
6133 6132 break;
6134 6133 case SCMD_READ_G1:
6135 6134 /* 10-bytes scsi read command : 0x28 */
6136 6135 lba = scsipkt->pkt_cdbp[2];
6137 6136 lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6138 6137 lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6139 6138 lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6140 6139 sec_count = scsipkt->pkt_cdbp[7];
6141 6140 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6142 6141 break;
6143 6142 case SCMD_READ_G5:
6144 6143 /* 12-bytes scsi read command : 0xA8 */
6145 6144 lba = scsipkt->pkt_cdbp[2];
6146 6145 lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6147 6146 lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6148 6147 lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6149 6148 sec_count = scsipkt->pkt_cdbp[6];
6150 6149 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
6151 6150 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6152 6151 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
6153 6152 break;
6154 6153 case SCMD_READ_G4:
6155 6154 /* 16-bytes scsi read command : 0x88 */
6156 6155 lba = scsipkt->pkt_cdbp[2];
6157 6156 lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6158 6157 lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6159 6158 lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6160 6159 lba = (lba << 8) | scsipkt->pkt_cdbp[6];
6161 6160 lba = (lba << 8) | scsipkt->pkt_cdbp[7];
6162 6161 lba = (lba << 8) | scsipkt->pkt_cdbp[8];
6163 6162 lba = (lba << 8) | scsipkt->pkt_cdbp[9];
6164 6163 sec_count = scsipkt->pkt_cdbp[10];
6165 6164 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
6166 6165 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
6167 6166 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
6168 6167 break;
6169 6168 default:
6170 6169 /* Unsupported command */
6171 6170 mutex_exit(cport_mutex);
6172 6171 return (sata_txlt_invalid_command(spx));
6173 6172 }
6174 6173
6175 6174 /*
6176 6175 * Check if specified address exceeds device capacity
6177 6176 */
6178 6177 if ((lba >= sdinfo->satadrv_capacity) ||
6179 6178 ((lba + sec_count) > sdinfo->satadrv_capacity)) {
6180 6179 /* LBA out of range */
6181 6180 mutex_exit(cport_mutex);
6182 6181 return (sata_txlt_lba_out_of_range(spx));
6183 6182 }
6184 6183
6185 6184 /*
6186 6185 * For zero-length transfer, emulate good completion of the command
6187 6186 * (reasons for rejecting the command were already checked).
6188 6187 * No DMA resources were allocated.
6189 6188 */
6190 6189 if (spx->txlt_dma_cookie_list == NULL) {
6191 6190 mutex_exit(cport_mutex);
6192 6191 return (sata_emul_rw_completion(spx));
6193 6192 }
6194 6193
6195 6194 /*
6196 6195 * Build cmd block depending on the device capability and
6197 6196 * requested operation mode.
6198 6197 * Do not bother with non-dma mode - we are working only with
6199 6198 * devices supporting DMA.
6200 6199 */
6201 6200 scmd->satacmd_addr_type = ATA_ADDR_LBA;
6202 6201 scmd->satacmd_device_reg = SATA_ADH_LBA;
6203 6202 scmd->satacmd_cmd_reg = SATAC_READ_DMA;
6204 6203 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6205 6204 scmd->satacmd_addr_type = ATA_ADDR_LBA48;
6206 6205 scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT;
6207 6206 scmd->satacmd_sec_count_msb = sec_count >> 8;
6208 6207 #ifndef __lock_lint
6209 6208 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
6210 6209 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
6211 6210 scmd->satacmd_lba_high_msb = lba >> 40;
6212 6211 #endif
6213 6212 } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
6214 6213 scmd->satacmd_addr_type = ATA_ADDR_LBA28;
6215 6214 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
6216 6215 }
6217 6216 scmd->satacmd_sec_count_lsb = sec_count & 0xff;
6218 6217 scmd->satacmd_lba_low_lsb = lba & 0xff;
6219 6218 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
6220 6219 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
6221 6220 scmd->satacmd_features_reg = 0;
6222 6221 scmd->satacmd_status_reg = 0;
6223 6222 scmd->satacmd_error_reg = 0;
6224 6223
6225 6224 /*
6226 6225 * Check if queueing commands should be used and switch
6227 6226 * to appropriate command if possible
6228 6227 */
6229 6228 if (sata_func_enable & SATA_ENABLE_QUEUING) {
6230 6229 boolean_t using_queuing;
6231 6230
6232 6231 /* Queuing supported by controller and device? */
6233 6232 if ((sata_func_enable & SATA_ENABLE_NCQ) &&
6234 6233 (sdinfo->satadrv_features_support &
6235 6234 SATA_DEV_F_NCQ) &&
6236 6235 (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6237 6236 SATA_CTLF_NCQ)) {
6238 6237 using_queuing = B_TRUE;
6239 6238
6240 6239 /* NCQ supported - use FPDMA READ */
6241 6240 scmd->satacmd_cmd_reg =
6242 6241 SATAC_READ_FPDMA_QUEUED;
6243 6242 scmd->satacmd_features_reg_ext =
6244 6243 scmd->satacmd_sec_count_msb;
6245 6244 scmd->satacmd_sec_count_msb = 0;
6246 6245 } else if ((sdinfo->satadrv_features_support &
6247 6246 SATA_DEV_F_TCQ) &&
6248 6247 (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6249 6248 SATA_CTLF_QCMD)) {
6250 6249 using_queuing = B_TRUE;
6251 6250
6252 6251 /* Legacy queueing */
6253 6252 if (sdinfo->satadrv_features_support &
6254 6253 SATA_DEV_F_LBA48) {
6255 6254 scmd->satacmd_cmd_reg =
6256 6255 SATAC_READ_DMA_QUEUED_EXT;
6257 6256 scmd->satacmd_features_reg_ext =
6258 6257 scmd->satacmd_sec_count_msb;
6259 6258 scmd->satacmd_sec_count_msb = 0;
6260 6259 } else {
6261 6260 scmd->satacmd_cmd_reg =
6262 6261 SATAC_READ_DMA_QUEUED;
6263 6262 }
6264 6263 } else /* NCQ nor legacy queuing not supported */
6265 6264 using_queuing = B_FALSE;
6266 6265
6267 6266 /*
6268 6267 * If queuing, the sector count goes in the features register
6269 6268 * and the secount count will contain the tag.
6270 6269 */
6271 6270 if (using_queuing) {
6272 6271 scmd->satacmd_features_reg =
6273 6272 scmd->satacmd_sec_count_lsb;
6274 6273 scmd->satacmd_sec_count_lsb = 0;
6275 6274 scmd->satacmd_flags.sata_queued = B_TRUE;
6276 6275
6277 6276 /* Set-up maximum queue depth */
6278 6277 scmd->satacmd_flags.sata_max_queue_depth =
6279 6278 sdinfo->satadrv_max_queue_depth - 1;
6280 6279 } else if (sdinfo->satadrv_features_enabled &
6281 6280 SATA_DEV_F_E_UNTAGGED_QING) {
6282 6281 /*
6283 6282 * Although NCQ/TCQ is not enabled, untagged queuing
6284 6283 * may be still used.
6285 6284 * Set-up the maximum untagged queue depth.
6286 6285 * Use controller's queue depth from sata_hba_tran.
6287 6286 * SATA HBA drivers may ignore this value and rely on
6288 6287 * the internal limits.For drivers that do not
6289 6288 * ignore untaged queue depth, limit the value to
6290 6289 * SATA_MAX_QUEUE_DEPTH (32), as this is the
6291 6290 * largest value that can be passed via
6292 6291 * satacmd_flags.sata_max_queue_depth.
6293 6292 */
6294 6293 scmd->satacmd_flags.sata_max_queue_depth =
6295 6294 SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
6296 6295 SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
6297 6296
6298 6297 } else {
6299 6298 scmd->satacmd_flags.sata_max_queue_depth = 0;
6300 6299 }
6301 6300 } else
6302 6301 scmd->satacmd_flags.sata_max_queue_depth = 0;
6303 6302
6304 6303 SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst,
6305 6304 "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n",
6306 6305 scmd->satacmd_cmd_reg, lba, sec_count);
6307 6306
6308 6307 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6309 6308 /* Need callback function */
6310 6309 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
6311 6310 synch = FALSE;
6312 6311 } else
6313 6312 synch = TRUE;
6314 6313
6315 6314 /* Transfer command to HBA */
6316 6315 if (sata_hba_start(spx, &rval) != 0) {
6317 6316 /* Pkt not accepted for execution */
6318 6317 mutex_exit(cport_mutex);
6319 6318 return (rval);
6320 6319 }
6321 6320 mutex_exit(cport_mutex);
6322 6321 /*
6323 6322 * If execution is non-synchronous,
6324 6323 * a callback function will handle potential errors, translate
6325 6324 * the response and will do a callback to a target driver.
6326 6325 * If it was synchronous, check execution status using the same
6327 6326 * framework callback.
6328 6327 */
6329 6328 if (synch) {
6330 6329 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6331 6330 "synchronous execution status %x\n",
6332 6331 spx->txlt_sata_pkt->satapkt_reason);
6333 6332 sata_txlt_rw_completion(spx->txlt_sata_pkt);
6334 6333 }
6335 6334 return (TRAN_ACCEPT);
6336 6335 }
6337 6336
6338 6337
6339 6338 /*
6340 6339 * SATA translate command: Write (various types)
6341 6340 * Translated into appropriate type of ATA WRITE command
6342 6341 * for SATA hard disks.
6343 6342 * Both the device capabilities and requested operation mode are
6344 6343 * considered.
6345 6344 *
6346 6345 * Following scsi cdb fields are ignored:
6347 6346 * rwprotect, dpo, fua, fua_nv, group_number.
6348 6347 *
6349 6348 * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
6350 6349 * enable variable sata_func_enable), the capability of the controller and
6351 6350 * capability of a device are checked and if both support queueing, write
6352 6351 * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT
6353 6352 * command rather than plain WRITE_XXX command.
6354 6353 * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
6355 6354 * both the controller and device suport such functionality, the write
6356 6355 * request will be translated to WRITE_FPDMA_QUEUED command.
6357 6356 * In both cases the maximum queue depth is derived as minimum of:
6358 6357 * HBA capability,device capability and sata_max_queue_depth variable setting.
6359 6358 * The value passed to HBA driver is decremented by 1, because only 5 bits are
6360 6359 * used to pass max queue depth value, and the maximum possible queue depth
6361 6360 * is 32.
6362 6361 *
6363 6362 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6364 6363 * appropriate values in scsi_pkt fields.
6365 6364 */
6366 6365 static int
6367 6366 sata_txlt_write(sata_pkt_txlate_t *spx)
6368 6367 {
6369 6368 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6370 6369 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6371 6370 sata_drive_info_t *sdinfo;
6372 6371 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
6373 6372 uint16_t sec_count;
6374 6373 uint64_t lba;
6375 6374 int rval, reason;
6376 6375 int synch;
6377 6376 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6378 6377
6379 6378 mutex_enter(cport_mutex);
6380 6379
6381 6380 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
6382 6381 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6383 6382 mutex_exit(cport_mutex);
6384 6383 return (rval);
6385 6384 }
6386 6385
6387 6386 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6388 6387 &spx->txlt_sata_pkt->satapkt_device);
6389 6388
6390 6389 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
6391 6390 /*
6392 6391 * Extract LBA and sector count from scsi CDB
6393 6392 */
6394 6393 switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6395 6394 case SCMD_WRITE:
6396 6395 /* 6-byte scsi read cmd : 0x0A */
6397 6396 lba = (scsipkt->pkt_cdbp[1] & 0x1f);
6398 6397 lba = (lba << 8) | scsipkt->pkt_cdbp[2];
6399 6398 lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6400 6399 sec_count = scsipkt->pkt_cdbp[4];
6401 6400 /* sec_count 0 will be interpreted as 256 by a device */
6402 6401 break;
6403 6402 case SCMD_WRITE_G1:
6404 6403 /* 10-bytes scsi write command : 0x2A */
6405 6404 lba = scsipkt->pkt_cdbp[2];
6406 6405 lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6407 6406 lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6408 6407 lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6409 6408 sec_count = scsipkt->pkt_cdbp[7];
6410 6409 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6411 6410 break;
6412 6411 case SCMD_WRITE_G5:
6413 6412 /* 12-bytes scsi read command : 0xAA */
6414 6413 lba = scsipkt->pkt_cdbp[2];
6415 6414 lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6416 6415 lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6417 6416 lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6418 6417 sec_count = scsipkt->pkt_cdbp[6];
6419 6418 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
6420 6419 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6421 6420 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
6422 6421 break;
6423 6422 case SCMD_WRITE_G4:
6424 6423 /* 16-bytes scsi write command : 0x8A */
6425 6424 lba = scsipkt->pkt_cdbp[2];
6426 6425 lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6427 6426 lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6428 6427 lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6429 6428 lba = (lba << 8) | scsipkt->pkt_cdbp[6];
6430 6429 lba = (lba << 8) | scsipkt->pkt_cdbp[7];
6431 6430 lba = (lba << 8) | scsipkt->pkt_cdbp[8];
6432 6431 lba = (lba << 8) | scsipkt->pkt_cdbp[9];
6433 6432 sec_count = scsipkt->pkt_cdbp[10];
6434 6433 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
6435 6434 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
6436 6435 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
6437 6436 break;
6438 6437 default:
6439 6438 /* Unsupported command */
6440 6439 mutex_exit(cport_mutex);
6441 6440 return (sata_txlt_invalid_command(spx));
6442 6441 }
6443 6442
6444 6443 /*
6445 6444 * Check if specified address and length exceeds device capacity
6446 6445 */
6447 6446 if ((lba >= sdinfo->satadrv_capacity) ||
6448 6447 ((lba + sec_count) > sdinfo->satadrv_capacity)) {
6449 6448 /* LBA out of range */
6450 6449 mutex_exit(cport_mutex);
6451 6450 return (sata_txlt_lba_out_of_range(spx));
6452 6451 }
6453 6452
6454 6453 /*
6455 6454 * For zero-length transfer, emulate good completion of the command
6456 6455 * (reasons for rejecting the command were already checked).
6457 6456 * No DMA resources were allocated.
6458 6457 */
6459 6458 if (spx->txlt_dma_cookie_list == NULL) {
6460 6459 mutex_exit(cport_mutex);
6461 6460 return (sata_emul_rw_completion(spx));
6462 6461 }
6463 6462
6464 6463 /*
6465 6464 * Build cmd block depending on the device capability and
6466 6465 * requested operation mode.
6467 6466 * Do not bother with non-dma mode- we are working only with
6468 6467 * devices supporting DMA.
6469 6468 */
6470 6469 scmd->satacmd_addr_type = ATA_ADDR_LBA;
6471 6470 scmd->satacmd_device_reg = SATA_ADH_LBA;
6472 6471 scmd->satacmd_cmd_reg = SATAC_WRITE_DMA;
6473 6472 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6474 6473 scmd->satacmd_addr_type = ATA_ADDR_LBA48;
6475 6474 scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT;
6476 6475 scmd->satacmd_sec_count_msb = sec_count >> 8;
6477 6476 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
6478 6477 #ifndef __lock_lint
6479 6478 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
6480 6479 scmd->satacmd_lba_high_msb = lba >> 40;
6481 6480 #endif
6482 6481 } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
6483 6482 scmd->satacmd_addr_type = ATA_ADDR_LBA28;
6484 6483 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
6485 6484 }
6486 6485 scmd->satacmd_sec_count_lsb = sec_count & 0xff;
6487 6486 scmd->satacmd_lba_low_lsb = lba & 0xff;
6488 6487 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
6489 6488 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
6490 6489 scmd->satacmd_features_reg = 0;
6491 6490 scmd->satacmd_status_reg = 0;
6492 6491 scmd->satacmd_error_reg = 0;
6493 6492
6494 6493 /*
6495 6494 * Check if queueing commands should be used and switch
6496 6495 * to appropriate command if possible
6497 6496 */
6498 6497 if (sata_func_enable & SATA_ENABLE_QUEUING) {
6499 6498 boolean_t using_queuing;
6500 6499
6501 6500 /* Queuing supported by controller and device? */
6502 6501 if ((sata_func_enable & SATA_ENABLE_NCQ) &&
6503 6502 (sdinfo->satadrv_features_support &
6504 6503 SATA_DEV_F_NCQ) &&
6505 6504 (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6506 6505 SATA_CTLF_NCQ)) {
6507 6506 using_queuing = B_TRUE;
6508 6507
6509 6508 /* NCQ supported - use FPDMA WRITE */
6510 6509 scmd->satacmd_cmd_reg =
6511 6510 SATAC_WRITE_FPDMA_QUEUED;
6512 6511 scmd->satacmd_features_reg_ext =
6513 6512 scmd->satacmd_sec_count_msb;
6514 6513 scmd->satacmd_sec_count_msb = 0;
6515 6514 } else if ((sdinfo->satadrv_features_support &
6516 6515 SATA_DEV_F_TCQ) &&
6517 6516 (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6518 6517 SATA_CTLF_QCMD)) {
6519 6518 using_queuing = B_TRUE;
6520 6519
6521 6520 /* Legacy queueing */
6522 6521 if (sdinfo->satadrv_features_support &
6523 6522 SATA_DEV_F_LBA48) {
6524 6523 scmd->satacmd_cmd_reg =
6525 6524 SATAC_WRITE_DMA_QUEUED_EXT;
6526 6525 scmd->satacmd_features_reg_ext =
6527 6526 scmd->satacmd_sec_count_msb;
6528 6527 scmd->satacmd_sec_count_msb = 0;
6529 6528 } else {
6530 6529 scmd->satacmd_cmd_reg =
6531 6530 SATAC_WRITE_DMA_QUEUED;
6532 6531 }
6533 6532 } else /* NCQ nor legacy queuing not supported */
6534 6533 using_queuing = B_FALSE;
6535 6534
6536 6535 if (using_queuing) {
6537 6536 scmd->satacmd_features_reg =
6538 6537 scmd->satacmd_sec_count_lsb;
6539 6538 scmd->satacmd_sec_count_lsb = 0;
6540 6539 scmd->satacmd_flags.sata_queued = B_TRUE;
6541 6540 /* Set-up maximum queue depth */
6542 6541 scmd->satacmd_flags.sata_max_queue_depth =
6543 6542 sdinfo->satadrv_max_queue_depth - 1;
6544 6543 } else if (sdinfo->satadrv_features_enabled &
6545 6544 SATA_DEV_F_E_UNTAGGED_QING) {
6546 6545 /*
6547 6546 * Although NCQ/TCQ is not enabled, untagged queuing
6548 6547 * may be still used.
6549 6548 * Set-up the maximum untagged queue depth.
6550 6549 * Use controller's queue depth from sata_hba_tran.
6551 6550 * SATA HBA drivers may ignore this value and rely on
6552 6551 * the internal limits. For drivera that do not
6553 6552 * ignore untaged queue depth, limit the value to
6554 6553 * SATA_MAX_QUEUE_DEPTH (32), as this is the
6555 6554 * largest value that can be passed via
6556 6555 * satacmd_flags.sata_max_queue_depth.
6557 6556 */
6558 6557 scmd->satacmd_flags.sata_max_queue_depth =
6559 6558 SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
6560 6559 SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
6561 6560
6562 6561 } else {
6563 6562 scmd->satacmd_flags.sata_max_queue_depth = 0;
6564 6563 }
6565 6564 } else
6566 6565 scmd->satacmd_flags.sata_max_queue_depth = 0;
6567 6566
6568 6567 SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6569 6568 "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n",
6570 6569 scmd->satacmd_cmd_reg, lba, sec_count);
6571 6570
6572 6571 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6573 6572 /* Need callback function */
6574 6573 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
6575 6574 synch = FALSE;
6576 6575 } else
6577 6576 synch = TRUE;
6578 6577
6579 6578 /* Transfer command to HBA */
6580 6579 if (sata_hba_start(spx, &rval) != 0) {
6581 6580 /* Pkt not accepted for execution */
6582 6581 mutex_exit(cport_mutex);
6583 6582 return (rval);
6584 6583 }
6585 6584 mutex_exit(cport_mutex);
6586 6585
6587 6586 /*
6588 6587 * If execution is non-synchronous,
6589 6588 * a callback function will handle potential errors, translate
6590 6589 * the response and will do a callback to a target driver.
6591 6590 * If it was synchronous, check execution status using the same
6592 6591 * framework callback.
6593 6592 */
6594 6593 if (synch) {
6595 6594 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6596 6595 "synchronous execution status %x\n",
6597 6596 spx->txlt_sata_pkt->satapkt_reason);
6598 6597 sata_txlt_rw_completion(spx->txlt_sata_pkt);
6599 6598 }
6600 6599 return (TRAN_ACCEPT);
6601 6600 }
6602 6601
6603 6602
6604 6603 /*
6605 6604 * Implements SCSI SBC WRITE BUFFER command download microcode option
6606 6605 */
6607 6606 static int
6608 6607 sata_txlt_write_buffer(sata_pkt_txlate_t *spx)
6609 6608 {
6610 6609 #define WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE 4
6611 6610 #define WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE 5
6612 6611
6613 6612 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6614 6613 struct sata_pkt *sata_pkt = spx->txlt_sata_pkt;
6615 6614 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6616 6615
6617 6616 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
6618 6617 struct scsi_extended_sense *sense;
6619 6618 int rval, mode, sector_count, reason;
6620 6619 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6621 6620
6622 6621 mode = scsipkt->pkt_cdbp[1] & 0x1f;
6623 6622
6624 6623 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6625 6624 "sata_txlt_write_buffer, mode 0x%x\n", mode);
6626 6625
6627 6626 mutex_enter(cport_mutex);
6628 6627
6629 6628 if ((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
6630 6629 TRAN_ACCEPT) {
6631 6630 mutex_exit(cport_mutex);
6632 6631 return (rval);
6633 6632 }
6634 6633
6635 6634 /* Use synchronous mode */
6636 6635 spx->txlt_sata_pkt->satapkt_op_mode
6637 6636 |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
6638 6637
6639 6638 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
6640 6639
6641 6640 scsipkt->pkt_reason = CMD_CMPLT;
6642 6641 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6643 6642 STATE_SENT_CMD | STATE_GOT_STATUS;
6644 6643
6645 6644 /*
6646 6645 * The SCSI to ATA translation specification only calls
6647 6646 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE.
6648 6647 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but
6649 6648 * ATA 8 (draft) got rid of download microcode for temp
6650 6649 * and it is even optional for ATA 7, so it may be aborted.
6651 6650 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as
6652 6651 * it is not specified and the buffer offset for SCSI is a 16-bit
6653 6652 * value in bytes, but for ATA it is a 16-bit offset in 512 byte
6654 6653 * sectors. Thus the offset really doesn't buy us anything.
6655 6654 * If and when ATA 8 is stabilized and the SCSI to ATA specification
6656 6655 * is revised, this can be revisisted.
6657 6656 */
6658 6657 /* Reject not supported request */
6659 6658 switch (mode) {
6660 6659 case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE:
6661 6660 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP;
6662 6661 break;
6663 6662 case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE:
6664 6663 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE;
6665 6664 break;
6666 6665 default:
6667 6666 goto bad_param;
6668 6667 }
6669 6668
6670 6669 *scsipkt->pkt_scbp = STATUS_GOOD; /* Presumed outcome */
6671 6670
6672 6671 scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE;
6673 6672 if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0)
6674 6673 goto bad_param;
6675 6674 sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE;
6676 6675 scmd->satacmd_sec_count_lsb = (uint8_t)sector_count;
6677 6676 scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8;
6678 6677 scmd->satacmd_lba_mid_lsb = 0;
6679 6678 scmd->satacmd_lba_high_lsb = 0;
6680 6679 scmd->satacmd_device_reg = 0;
6681 6680 spx->txlt_sata_pkt->satapkt_comp = NULL;
6682 6681 scmd->satacmd_addr_type = 0;
6683 6682
6684 6683 /* Transfer command to HBA */
6685 6684 if (sata_hba_start(spx, &rval) != 0) {
6686 6685 /* Pkt not accepted for execution */
6687 6686 mutex_exit(cport_mutex);
6688 6687 return (rval);
6689 6688 }
6690 6689
6691 6690 mutex_exit(cport_mutex);
6692 6691
6693 6692 /* Then we need synchronous check the status of the disk */
6694 6693 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6695 6694 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
6696 6695 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
6697 6696 scsipkt->pkt_reason = CMD_CMPLT;
6698 6697
6699 6698 /* Download commmand succeed, so probe and identify device */
6700 6699 sata_reidentify_device(spx);
6701 6700 } else {
6702 6701 /* Something went wrong, microcode download command failed */
6703 6702 scsipkt->pkt_reason = CMD_INCOMPLETE;
6704 6703 *scsipkt->pkt_scbp = STATUS_CHECK;
6705 6704 sense = sata_arq_sense(spx);
6706 6705 switch (sata_pkt->satapkt_reason) {
6707 6706 case SATA_PKT_PORT_ERROR:
6708 6707 /*
6709 6708 * We have no device data. Assume no data transfered.
6710 6709 */
6711 6710 sense->es_key = KEY_HARDWARE_ERROR;
6712 6711 break;
6713 6712
6714 6713 case SATA_PKT_DEV_ERROR:
6715 6714 if (sata_pkt->satapkt_cmd.satacmd_status_reg &
6716 6715 SATA_STATUS_ERR) {
6717 6716 /*
6718 6717 * determine dev error reason from error
6719 6718 * reg content
6720 6719 */
6721 6720 sata_decode_device_error(spx, sense);
6722 6721 break;
6723 6722 }
6724 6723 /* No extended sense key - no info available */
6725 6724 break;
6726 6725
6727 6726 case SATA_PKT_TIMEOUT:
6728 6727 scsipkt->pkt_reason = CMD_TIMEOUT;
6729 6728 scsipkt->pkt_statistics |=
6730 6729 STAT_TIMEOUT | STAT_DEV_RESET;
6731 6730 /* No extended sense key ? */
6732 6731 break;
6733 6732
6734 6733 case SATA_PKT_ABORTED:
6735 6734 scsipkt->pkt_reason = CMD_ABORTED;
6736 6735 scsipkt->pkt_statistics |= STAT_ABORTED;
6737 6736 /* No extended sense key ? */
6738 6737 break;
6739 6738
6740 6739 case SATA_PKT_RESET:
6741 6740 /* pkt aborted by an explicit reset from a host */
6742 6741 scsipkt->pkt_reason = CMD_RESET;
6743 6742 scsipkt->pkt_statistics |= STAT_DEV_RESET;
6744 6743 break;
6745 6744
6746 6745 default:
6747 6746 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
6748 6747 "sata_txlt_nodata_cmd_completion: "
6749 6748 "invalid packet completion reason %d",
6750 6749 sata_pkt->satapkt_reason));
6751 6750 scsipkt->pkt_reason = CMD_TRAN_ERR;
6752 6751 break;
6753 6752 }
6754 6753
6755 6754 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6756 6755 "scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6757 6756
6758 6757 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6759 6758 /* scsi callback required */
6760 6759 scsi_hba_pkt_comp(scsipkt);
6761 6760 }
6762 6761 return (TRAN_ACCEPT);
6763 6762
6764 6763 bad_param:
6765 6764 mutex_exit(cport_mutex);
6766 6765 *scsipkt->pkt_scbp = STATUS_CHECK;
6767 6766 sense = sata_arq_sense(spx);
6768 6767 sense->es_key = KEY_ILLEGAL_REQUEST;
6769 6768 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6770 6769 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6771 6770 scsipkt->pkt_comp != NULL) {
6772 6771 /* scsi callback required */
6773 6772 if (servicing_interrupt()) {
6774 6773 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6775 6774 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6776 6775 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
6777 6776 return (TRAN_BUSY);
6778 6777 }
6779 6778 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6780 6779 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6781 6780 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
6782 6781 /* Scheduling the callback failed */
6783 6782 return (TRAN_BUSY);
6784 6783 }
6785 6784 }
6786 6785 return (rval);
6787 6786 }
6788 6787
6789 6788 /*
6790 6789 * Re-identify device after doing a firmware download.
6791 6790 */
6792 6791 static void
6793 6792 sata_reidentify_device(sata_pkt_txlate_t *spx)
6794 6793 {
6795 6794 #define DOWNLOAD_WAIT_TIME_SECS 60
6796 6795 #define DOWNLOAD_WAIT_INTERVAL_SECS 1
6797 6796 int rval;
6798 6797 int retry_cnt;
6799 6798 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6800 6799 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
6801 6800 sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
6802 6801 sata_drive_info_t *sdinfo;
6803 6802
6804 6803 /*
6805 6804 * Before returning good status, probe device.
6806 6805 * Device probing will get IDENTIFY DEVICE data, if possible.
6807 6806 * The assumption is that the new microcode is applied by the
6808 6807 * device. It is a caller responsibility to verify this.
6809 6808 */
6810 6809 for (retry_cnt = 0;
6811 6810 retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS;
6812 6811 retry_cnt++) {
6813 6812 rval = sata_probe_device(sata_hba_inst, &sata_device);
6814 6813
6815 6814 if (rval == SATA_SUCCESS) { /* Set default features */
6816 6815 sdinfo = sata_get_device_info(sata_hba_inst,
6817 6816 &sata_device);
6818 6817 if (sata_initialize_device(sata_hba_inst, sdinfo) !=
6819 6818 SATA_SUCCESS) {
6820 6819 /* retry */
6821 6820 rval = sata_initialize_device(sata_hba_inst,
6822 6821 sdinfo);
6823 6822 if (rval == SATA_RETRY)
6824 6823 sata_log(sata_hba_inst, CE_WARN,
6825 6824 "SATA device at port %d pmport %d -"
6826 6825 " default device features could not"
6827 6826 " be set. Device may not operate "
6828 6827 "as expected.",
6829 6828 sata_device.satadev_addr.cport,
6830 6829 sata_device.satadev_addr.pmport);
6831 6830 }
6832 6831 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6833 6832 scsi_hba_pkt_comp(scsipkt);
6834 6833 return;
6835 6834 } else if (rval == SATA_RETRY) {
6836 6835 delay(drv_usectohz(1000000 *
6837 6836 DOWNLOAD_WAIT_INTERVAL_SECS));
6838 6837 continue;
6839 6838 } else /* failed - no reason to retry */
6840 6839 break;
6841 6840 }
6842 6841
6843 6842 /*
6844 6843 * Something went wrong, device probing failed.
6845 6844 */
6846 6845 SATA_LOG_D((sata_hba_inst, CE_WARN,
6847 6846 "Cannot probe device after downloading microcode\n"));
6848 6847
6849 6848 /* Reset device to force retrying the probe. */
6850 6849 (void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst))
6851 6850 (SATA_DIP(sata_hba_inst), &sata_device);
6852 6851
6853 6852 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6854 6853 scsi_hba_pkt_comp(scsipkt);
6855 6854 }
6856 6855
6857 6856
6858 6857 /*
6859 6858 * Translate command: Synchronize Cache.
6860 6859 * Translates into Flush Cache command for SATA hard disks.
6861 6860 *
6862 6861 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6863 6862 * appropriate values in scsi_pkt fields.
6864 6863 */
6865 6864 static int
6866 6865 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx)
6867 6866 {
6868 6867 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6869 6868 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6870 6869 int rval, reason;
6871 6870 int synch;
6872 6871
6873 6872 mutex_enter(cport_mutex);
6874 6873
6875 6874 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
6876 6875 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6877 6876 mutex_exit(cport_mutex);
6878 6877 return (rval);
6879 6878 }
6880 6879
6881 6880 scmd->satacmd_addr_type = 0;
6882 6881 scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE;
6883 6882 scmd->satacmd_device_reg = 0;
6884 6883 scmd->satacmd_sec_count_lsb = 0;
6885 6884 scmd->satacmd_lba_low_lsb = 0;
6886 6885 scmd->satacmd_lba_mid_lsb = 0;
6887 6886 scmd->satacmd_lba_high_lsb = 0;
6888 6887 scmd->satacmd_features_reg = 0;
6889 6888 scmd->satacmd_status_reg = 0;
6890 6889 scmd->satacmd_error_reg = 0;
6891 6890
6892 6891 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6893 6892 "sata_txlt_synchronize_cache\n", NULL);
6894 6893
6895 6894 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6896 6895 /* Need to set-up a callback function */
6897 6896 spx->txlt_sata_pkt->satapkt_comp =
6898 6897 sata_txlt_nodata_cmd_completion;
6899 6898 synch = FALSE;
6900 6899 } else
6901 6900 synch = TRUE;
6902 6901
6903 6902 /* Transfer command to HBA */
6904 6903 if (sata_hba_start(spx, &rval) != 0) {
6905 6904 /* Pkt not accepted for execution */
6906 6905 mutex_exit(cport_mutex);
6907 6906 return (rval);
6908 6907 }
6909 6908 mutex_exit(cport_mutex);
6910 6909
6911 6910 /*
6912 6911 * If execution non-synchronous, it had to be completed
6913 6912 * a callback function will handle potential errors, translate
6914 6913 * the response and will do a callback to a target driver.
6915 6914 * If it was synchronous, check status, using the same
6916 6915 * framework callback.
6917 6916 */
6918 6917 if (synch) {
6919 6918 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6920 6919 "synchronous execution status %x\n",
6921 6920 spx->txlt_sata_pkt->satapkt_reason);
6922 6921 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
6923 6922 }
6924 6923 return (TRAN_ACCEPT);
6925 6924 }
6926 6925
6927 6926
6928 6927 /*
6929 6928 * Send pkt to SATA HBA driver
6930 6929 *
6931 6930 * This function may be called only if the operation is requested by scsi_pkt,
6932 6931 * i.e. scsi_pkt is not NULL.
6933 6932 *
6934 6933 * This function has to be called with cport mutex held. It does release
6935 6934 * the mutex when it calls HBA driver sata_tran_start function and
6936 6935 * re-acquires it afterwards.
6937 6936 *
6938 6937 * If return value is 0, pkt was accepted, -1 otherwise
6939 6938 * rval is set to appropriate sata_scsi_start return value.
6940 6939 *
6941 6940 * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not
6942 6941 * have called the sata_pkt callback function for this packet.
6943 6942 *
6944 6943 * The scsi callback has to be performed by the caller of this routine.
6945 6944 */
6946 6945 static int
6947 6946 sata_hba_start(sata_pkt_txlate_t *spx, int *rval)
6948 6947 {
6949 6948 int stat;
6950 6949 uint8_t cport = SATA_TXLT_CPORT(spx);
6951 6950 uint8_t pmport = SATA_TXLT_PMPORT(spx);
6952 6951 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
6953 6952 sata_drive_info_t *sdinfo;
6954 6953 sata_pmult_info_t *pminfo;
6955 6954 sata_pmport_info_t *pmportinfo = NULL;
6956 6955 sata_device_t *sata_device = NULL;
6957 6956 uint8_t cmd;
6958 6957 struct sata_cmd_flags cmd_flags;
6959 6958
6960 6959 ASSERT(spx->txlt_sata_pkt != NULL);
6961 6960
6962 6961 ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6963 6962
6964 6963 sdinfo = sata_get_device_info(sata_hba_inst,
6965 6964 &spx->txlt_sata_pkt->satapkt_device);
6966 6965 ASSERT(sdinfo != NULL);
6967 6966
6968 6967 /* Clear device reset state? */
6969 6968 /* qual should be XXX_DPMPORT, but add XXX_PMPORT in case */
6970 6969 if (sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT ||
6971 6970 sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT) {
6972 6971
6973 6972 /*
6974 6973 * Get the pmult_info of the its parent port multiplier, all
6975 6974 * sub-devices share a common device reset flags on in
6976 6975 * pmult_info.
6977 6976 */
6978 6977 pminfo = SATA_PMULT_INFO(sata_hba_inst, cport);
6979 6978 pmportinfo = pminfo->pmult_dev_port[pmport];
6980 6979 ASSERT(pminfo != NULL);
6981 6980 if (pminfo->pmult_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) {
6982 6981 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
6983 6982 sata_clear_dev_reset = B_TRUE;
6984 6983 pminfo->pmult_event_flags &=
6985 6984 ~SATA_EVNT_CLEAR_DEVICE_RESET;
6986 6985 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6987 6986 "sata_hba_start: clearing device reset state"
6988 6987 "on pmult.\n", NULL);
6989 6988 }
6990 6989 } else {
6991 6990 if (sdinfo->satadrv_event_flags &
6992 6991 SATA_EVNT_CLEAR_DEVICE_RESET) {
6993 6992 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
6994 6993 sata_clear_dev_reset = B_TRUE;
6995 6994 sdinfo->satadrv_event_flags &=
6996 6995 ~SATA_EVNT_CLEAR_DEVICE_RESET;
6997 6996 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6998 6997 "sata_hba_start: clearing device reset state\n",
6999 6998 NULL);
7000 6999 }
7001 7000 }
7002 7001
7003 7002 cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg;
7004 7003 cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags;
7005 7004 sata_device = &spx->txlt_sata_pkt->satapkt_device;
7006 7005
7007 7006 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7008 7007
7009 7008 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7010 7009 "Sata cmd 0x%2x\n", cmd);
7011 7010
7012 7011 stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
7013 7012 spx->txlt_sata_pkt);
7014 7013 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7015 7014 /*
7016 7015 * If sata pkt was accepted and executed in asynchronous mode, i.e.
7017 7016 * with the sata callback, the sata_pkt could be already destroyed
7018 7017 * by the time we check ther return status from the hba_start()
7019 7018 * function, because sata_scsi_destroy_pkt() could have been already
7020 7019 * called (perhaps in the interrupt context). So, in such case, there
7021 7020 * should be no references to it. In other cases, sata_pkt still
7022 7021 * exists.
7023 7022 */
7024 7023 if (stat == SATA_TRAN_ACCEPTED) {
7025 7024 /*
7026 7025 * pkt accepted for execution.
7027 7026 * If it was executed synchronously, it is already completed
7028 7027 * and pkt completion_reason indicates completion status.
7029 7028 */
7030 7029 *rval = TRAN_ACCEPT;
7031 7030 return (0);
7032 7031 }
7033 7032
7034 7033 sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
7035 7034 switch (stat) {
7036 7035 case SATA_TRAN_QUEUE_FULL:
7037 7036 /*
7038 7037 * Controller detected queue full condition.
7039 7038 */
7040 7039 SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst,
7041 7040 "sata_hba_start: queue full\n", NULL);
7042 7041
7043 7042 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
7044 7043 *spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL;
7045 7044
7046 7045 *rval = TRAN_BUSY;
7047 7046 break;
7048 7047
7049 7048 case SATA_TRAN_PORT_ERROR:
7050 7049 /*
7051 7050 * Communication/link with device or general port error
7052 7051 * detected before pkt execution begun.
7053 7052 */
7054 7053 if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
7055 7054 SATA_ADDR_CPORT ||
7056 7055 spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
7057 7056 SATA_ADDR_DCPORT)
7058 7057 sata_log(sata_hba_inst, CE_CONT,
7059 7058 "SATA port %d error",
7060 7059 sata_device->satadev_addr.cport);
7061 7060 else
7062 7061 sata_log(sata_hba_inst, CE_CONT,
7063 7062 "SATA port %d:%d error\n",
7064 7063 sata_device->satadev_addr.cport,
7065 7064 sata_device->satadev_addr.pmport);
7066 7065
7067 7066 /*
7068 7067 * Update the port/device structure.
7069 7068 * sata_pkt should be still valid. Since port error is
7070 7069 * returned, sata_device content should reflect port
7071 7070 * state - it means, that sata address have been changed,
7072 7071 * because original packet's sata address refered to a device
7073 7072 * attached to some port.
7074 7073 */
7075 7074 if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT ||
7076 7075 sata_device->satadev_addr.qual == SATA_ADDR_PMPORT) {
7077 7076 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7078 7077 mutex_enter(&pmportinfo->pmport_mutex);
7079 7078 sata_update_pmport_info(sata_hba_inst, sata_device);
7080 7079 mutex_exit(&pmportinfo->pmport_mutex);
7081 7080 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7082 7081 } else {
7083 7082 sata_update_port_info(sata_hba_inst, sata_device);
7084 7083 }
7085 7084
7086 7085 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
7087 7086 *rval = TRAN_FATAL_ERROR;
7088 7087 break;
7089 7088
7090 7089 case SATA_TRAN_CMD_UNSUPPORTED:
7091 7090 /*
7092 7091 * Command rejected by HBA as unsupported. It was HBA driver
7093 7092 * that rejected the command, command was not sent to
7094 7093 * an attached device.
7095 7094 */
7096 7095 if ((sdinfo != NULL) &&
7097 7096 (sdinfo->satadrv_state & SATA_DSTATE_RESET))
7098 7097 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7099 7098 "sat_hba_start: cmd 0x%2x rejected "
7100 7099 "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd);
7101 7100
7102 7101 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7103 7102 (void) sata_txlt_invalid_command(spx);
7104 7103 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7105 7104
7106 7105 *rval = TRAN_ACCEPT;
7107 7106 break;
7108 7107
7109 7108 case SATA_TRAN_BUSY:
7110 7109 /*
7111 7110 * Command rejected by HBA because other operation prevents
7112 7111 * accepting the packet, or device is in RESET condition.
7113 7112 */
7114 7113 if (sdinfo != NULL) {
7115 7114 sdinfo->satadrv_state =
7116 7115 spx->txlt_sata_pkt->satapkt_device.satadev_state;
7117 7116
7118 7117 if (sdinfo->satadrv_state & SATA_DSTATE_RESET) {
7119 7118 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7120 7119 "sata_hba_start: cmd 0x%2x rejected "
7121 7120 "because of device reset condition\n",
7122 7121 cmd);
7123 7122 } else {
7124 7123 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7125 7124 "sata_hba_start: cmd 0x%2x rejected "
7126 7125 "with SATA_TRAN_BUSY status\n",
7127 7126 cmd);
7128 7127 }
7129 7128 }
7130 7129 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
7131 7130 *rval = TRAN_BUSY;
7132 7131 break;
7133 7132
7134 7133 default:
7135 7134 /* Unrecognized HBA response */
7136 7135 SATA_LOG_D((sata_hba_inst, CE_WARN,
7137 7136 "sata_hba_start: unrecognized HBA response "
7138 7137 "to cmd : 0x%2x resp 0x%x", cmd, rval));
7139 7138 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
7140 7139 *rval = TRAN_FATAL_ERROR;
7141 7140 break;
7142 7141 }
7143 7142
7144 7143 /*
7145 7144 * If we got here, the packet was rejected.
7146 7145 * Check if we need to remember reset state clearing request
7147 7146 */
7148 7147 if (cmd_flags.sata_clear_dev_reset) {
7149 7148 /*
7150 7149 * Check if device is still configured - it may have
7151 7150 * disapeared from the configuration
7152 7151 */
7153 7152 sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
7154 7153 if (sdinfo != NULL) {
7155 7154 /*
7156 7155 * Restore the flag that requests clearing of
7157 7156 * the device reset state,
7158 7157 * so the next sata packet may carry it to HBA.
7159 7158 */
7160 7159 if (sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT ||
7161 7160 sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT) {
7162 7161 pminfo->pmult_event_flags |=
7163 7162 SATA_EVNT_CLEAR_DEVICE_RESET;
7164 7163 } else {
7165 7164 sdinfo->satadrv_event_flags |=
7166 7165 SATA_EVNT_CLEAR_DEVICE_RESET;
7167 7166 }
7168 7167 }
7169 7168 }
7170 7169 return (-1);
7171 7170 }
7172 7171
7173 7172 /*
7174 7173 * Scsi response setup for invalid LBA
7175 7174 *
7176 7175 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
7177 7176 */
7178 7177 static int
7179 7178 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx)
7180 7179 {
7181 7180 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7182 7181 struct scsi_extended_sense *sense;
7183 7182
7184 7183 scsipkt->pkt_reason = CMD_CMPLT;
7185 7184 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7186 7185 STATE_SENT_CMD | STATE_GOT_STATUS;
7187 7186 *scsipkt->pkt_scbp = STATUS_CHECK;
7188 7187
7189 7188 *scsipkt->pkt_scbp = STATUS_CHECK;
7190 7189 sense = sata_arq_sense(spx);
7191 7190 sense->es_key = KEY_ILLEGAL_REQUEST;
7192 7191 sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7193 7192
7194 7193 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7195 7194 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7196 7195
7197 7196 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7198 7197 scsipkt->pkt_comp != NULL) {
7199 7198 /* scsi callback required */
7200 7199 if (servicing_interrupt()) {
7201 7200 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7202 7201 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7203 7202 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
7204 7203 return (TRAN_BUSY);
7205 7204 }
7206 7205 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7207 7206 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7208 7207 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7209 7208 /* Scheduling the callback failed */
7210 7209 return (TRAN_BUSY);
7211 7210 }
7212 7211 }
7213 7212 return (TRAN_ACCEPT);
7214 7213 }
7215 7214
7216 7215
7217 7216 /*
7218 7217 * Analyze device status and error registers and translate them into
7219 7218 * appropriate scsi sense codes.
7220 7219 * NOTE: non-packet commands only for now
7221 7220 */
7222 7221 static void
7223 7222 sata_decode_device_error(sata_pkt_txlate_t *spx,
7224 7223 struct scsi_extended_sense *sense)
7225 7224 {
7226 7225 uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg;
7227 7226
7228 7227 ASSERT(sense != NULL);
7229 7228 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
7230 7229 SATA_STATUS_ERR);
7231 7230
7232 7231
7233 7232 if (err_reg & SATA_ERROR_ICRC) {
7234 7233 sense->es_key = KEY_ABORTED_COMMAND;
7235 7234 sense->es_add_code = 0x08; /* Communication failure */
7236 7235 return;
7237 7236 }
7238 7237
7239 7238 if (err_reg & SATA_ERROR_UNC) {
7240 7239 sense->es_key = KEY_MEDIUM_ERROR;
7241 7240 /* Information bytes (LBA) need to be set by a caller */
7242 7241 return;
7243 7242 }
7244 7243
7245 7244 /* ADD HERE: MC error bit handling for ATAPI CD/DVD */
7246 7245 if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) {
7247 7246 sense->es_key = KEY_UNIT_ATTENTION;
7248 7247 sense->es_add_code = 0x3a; /* No media present */
7249 7248 return;
7250 7249 }
7251 7250
7252 7251 if (err_reg & SATA_ERROR_IDNF) {
7253 7252 if (err_reg & SATA_ERROR_ABORT) {
7254 7253 sense->es_key = KEY_ABORTED_COMMAND;
7255 7254 } else {
7256 7255 sense->es_key = KEY_ILLEGAL_REQUEST;
7257 7256 sense->es_add_code = 0x21; /* LBA out of range */
7258 7257 }
7259 7258 return;
7260 7259 }
7261 7260
7262 7261 if (err_reg & SATA_ERROR_ABORT) {
7263 7262 ASSERT(spx->txlt_sata_pkt != NULL);
7264 7263 sense->es_key = KEY_ABORTED_COMMAND;
7265 7264 return;
7266 7265 }
7267 7266 }
7268 7267
7269 7268 /*
7270 7269 * Extract error LBA from sata_pkt.satapkt_cmd register fields
7271 7270 */
7272 7271 static void
7273 7272 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba)
7274 7273 {
7275 7274 sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd;
7276 7275
7277 7276 *lba = 0;
7278 7277 if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) {
7279 7278 *lba = sata_cmd->satacmd_lba_high_msb;
7280 7279 *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb;
7281 7280 *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb;
7282 7281 } else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) {
7283 7282 *lba = sata_cmd->satacmd_device_reg & 0xf;
7284 7283 }
7285 7284 *lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb;
7286 7285 *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb;
7287 7286 *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb;
7288 7287 }
7289 7288
7290 7289 /*
7291 7290 * This is fixed sense format - if LBA exceeds the info field size,
7292 7291 * no valid info will be returned (valid bit in extended sense will
7293 7292 * be set to 0).
7294 7293 */
7295 7294 static struct scsi_extended_sense *
7296 7295 sata_arq_sense(sata_pkt_txlate_t *spx)
7297 7296 {
7298 7297 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7299 7298 struct scsi_arq_status *arqs;
7300 7299 struct scsi_extended_sense *sense;
7301 7300
7302 7301 /* Fill ARQ sense data */
7303 7302 scsipkt->pkt_state |= STATE_ARQ_DONE;
7304 7303 arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp;
7305 7304 *(uchar_t *)&arqs->sts_status = STATUS_CHECK;
7306 7305 *(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD;
7307 7306 arqs->sts_rqpkt_reason = CMD_CMPLT;
7308 7307 arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7309 7308 STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
7310 7309 arqs->sts_rqpkt_resid = 0;
7311 7310 sense = &arqs->sts_sensedata;
7312 7311 bzero(sense, sizeof (struct scsi_extended_sense));
7313 7312 sata_fixed_sense_data_preset(sense);
7314 7313 return (sense);
7315 7314 }
7316 7315
7317 7316 /*
7318 7317 * ATA Pass Through support
7319 7318 * Sets flags indicating that an invalid value was found in some
7320 7319 * field in the command. It could be something illegal according to
7321 7320 * the SAT-2 spec or it could be a feature that is not (yet?)
7322 7321 * supported.
7323 7322 */
7324 7323 static int
7325 7324 sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *spx)
7326 7325 {
7327 7326 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7328 7327 struct scsi_extended_sense *sense = sata_arq_sense(spx);
7329 7328
7330 7329 scsipkt->pkt_reason = CMD_CMPLT;
7331 7330 *scsipkt->pkt_scbp = STATUS_CHECK;
7332 7331 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7333 7332 STATE_SENT_CMD | STATE_GOT_STATUS;
7334 7333
7335 7334 sense = sata_arq_sense(spx);
7336 7335 sense->es_key = KEY_ILLEGAL_REQUEST;
7337 7336 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
7338 7337
7339 7338 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7340 7339 scsipkt->pkt_comp != NULL) {
7341 7340 /* scsi callback required */
7342 7341 if (servicing_interrupt()) {
7343 7342 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7344 7343 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7345 7344 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
7346 7345 return (TRAN_BUSY);
7347 7346 }
7348 7347 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7349 7348 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7350 7349 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7351 7350 /* Scheduling the callback failed */
7352 7351 return (TRAN_BUSY);
7353 7352 }
7354 7353 }
7355 7354
7356 7355 return (TRAN_ACCEPT);
7357 7356 }
7358 7357
7359 7358 /*
7360 7359 * The UNMAP command considers it not to be an error if the parameter length
7361 7360 * or block descriptor length is 0. For this case, there is nothing for TRIM
7362 7361 * to do so just complete the command.
7363 7362 */
7364 7363 static int
7365 7364 sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *spx)
7366 7365 {
7367 7366 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7368 7367
7369 7368 scsipkt->pkt_reason = CMD_CMPLT;
7370 7369 *scsipkt->pkt_scbp = STATUS_GOOD;
7371 7370 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7372 7371 STATE_SENT_CMD | STATE_GOT_STATUS;
7373 7372
7374 7373 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7375 7374 scsipkt->pkt_comp != NULL) {
7376 7375 /* scsi callback required */
7377 7376 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7378 7377 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7379 7378 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7380 7379 /* Scheduling the callback failed */
7381 7380 return (TRAN_BUSY);
7382 7381 }
7383 7382 }
7384 7383
7385 7384 return (TRAN_ACCEPT);
7386 7385 }
7387 7386
7388 7387 /*
7389 7388 * Emulated SATA Read/Write command completion for zero-length requests.
7390 7389 * This request always succedes, so in synchronous mode it always returns
7391 7390 * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the
7392 7391 * callback cannot be scheduled.
7393 7392 */
7394 7393 static int
7395 7394 sata_emul_rw_completion(sata_pkt_txlate_t *spx)
7396 7395 {
7397 7396 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7398 7397
7399 7398 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7400 7399 STATE_SENT_CMD | STATE_GOT_STATUS;
7401 7400 scsipkt->pkt_reason = CMD_CMPLT;
7402 7401 *scsipkt->pkt_scbp = STATUS_GOOD;
7403 7402 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
7404 7403 /* scsi callback required - have to schedule it */
7405 7404 if (servicing_interrupt()) {
7406 7405 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7407 7406 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7408 7407 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
7409 7408 return (TRAN_BUSY);
7410 7409 }
7411 7410 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7412 7411 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7413 7412 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7414 7413 /* Scheduling the callback failed */
7415 7414 return (TRAN_BUSY);
7416 7415 }
7417 7416 }
7418 7417 return (TRAN_ACCEPT);
7419 7418 }
7420 7419
7421 7420
7422 7421 /*
7423 7422 * Translate completion status of SATA read/write commands into scsi response.
7424 7423 * pkt completion_reason is checked to determine the completion status.
7425 7424 * Do scsi callback if necessary.
7426 7425 *
7427 7426 * Note: this function may be called also for synchronously executed
7428 7427 * commands.
7429 7428 * This function may be used only if scsi_pkt is non-NULL.
7430 7429 */
7431 7430 static void
7432 7431 sata_txlt_rw_completion(sata_pkt_t *sata_pkt)
7433 7432 {
7434 7433 sata_pkt_txlate_t *spx =
7435 7434 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7436 7435 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7437 7436 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7438 7437 struct scsi_extended_sense *sense;
7439 7438 uint64_t lba;
7440 7439 struct buf *bp;
7441 7440 int rval;
7442 7441 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7443 7442 /* Normal completion */
7444 7443 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7445 7444 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7446 7445 scsipkt->pkt_reason = CMD_CMPLT;
7447 7446 *scsipkt->pkt_scbp = STATUS_GOOD;
7448 7447 if (spx->txlt_tmp_buf != NULL) {
7449 7448 /* Temporary buffer was used */
7450 7449 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7451 7450 if (bp->b_flags & B_READ) {
7452 7451 rval = ddi_dma_sync(
7453 7452 spx->txlt_buf_dma_handle, 0, 0,
7454 7453 DDI_DMA_SYNC_FORCPU);
7455 7454 ASSERT(rval == DDI_SUCCESS);
7456 7455 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7457 7456 bp->b_bcount);
7458 7457 }
7459 7458 }
7460 7459 } else {
7461 7460 /*
7462 7461 * Something went wrong - analyze return
7463 7462 */
7464 7463 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7465 7464 STATE_SENT_CMD | STATE_GOT_STATUS;
7466 7465 scsipkt->pkt_reason = CMD_INCOMPLETE;
7467 7466 *scsipkt->pkt_scbp = STATUS_CHECK;
7468 7467 sense = sata_arq_sense(spx);
7469 7468 ASSERT(sense != NULL);
7470 7469
7471 7470 /*
7472 7471 * SATA_PKT_DEV_ERROR is the only case where we may be able to
7473 7472 * extract from device registers the failing LBA.
7474 7473 */
7475 7474 if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7476 7475 if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) &&
7477 7476 (scmd->satacmd_lba_mid_msb != 0 ||
7478 7477 scmd->satacmd_lba_high_msb != 0)) {
7479 7478 /*
7480 7479 * We have problem reporting this cmd LBA
7481 7480 * in fixed sense data format, because of
7482 7481 * the size of the scsi LBA fields.
7483 7482 */
7484 7483 sense->es_valid = 0;
7485 7484 } else {
7486 7485 sata_extract_error_lba(spx, &lba);
7487 7486 sense->es_info_1 = (lba & 0xFF000000) >> 24;
7488 7487 sense->es_info_2 = (lba & 0xFF0000) >> 16;
7489 7488 sense->es_info_3 = (lba & 0xFF00) >> 8;
7490 7489 sense->es_info_4 = lba & 0xFF;
7491 7490 }
7492 7491 } else {
7493 7492 /* Invalid extended sense info */
7494 7493 sense->es_valid = 0;
7495 7494 }
7496 7495
7497 7496 switch (sata_pkt->satapkt_reason) {
7498 7497 case SATA_PKT_PORT_ERROR:
7499 7498 /* We may want to handle DEV GONE state as well */
7500 7499 /*
7501 7500 * We have no device data. Assume no data transfered.
7502 7501 */
7503 7502 sense->es_key = KEY_HARDWARE_ERROR;
7504 7503 break;
7505 7504
7506 7505 case SATA_PKT_DEV_ERROR:
7507 7506 if (sata_pkt->satapkt_cmd.satacmd_status_reg &
7508 7507 SATA_STATUS_ERR) {
7509 7508 /*
7510 7509 * determine dev error reason from error
7511 7510 * reg content
7512 7511 */
7513 7512 sata_decode_device_error(spx, sense);
7514 7513 if (sense->es_key == KEY_MEDIUM_ERROR) {
7515 7514 switch (scmd->satacmd_cmd_reg) {
7516 7515 case SATAC_READ_DMA:
7517 7516 case SATAC_READ_DMA_EXT:
7518 7517 case SATAC_READ_DMA_QUEUED:
7519 7518 case SATAC_READ_DMA_QUEUED_EXT:
7520 7519 case SATAC_READ_FPDMA_QUEUED:
7521 7520 /* Unrecovered read error */
7522 7521 sense->es_add_code =
7523 7522 SD_SCSI_ASC_UNREC_READ_ERR;
7524 7523 break;
7525 7524 case SATAC_WRITE_DMA:
7526 7525 case SATAC_WRITE_DMA_EXT:
7527 7526 case SATAC_WRITE_DMA_QUEUED:
7528 7527 case SATAC_WRITE_DMA_QUEUED_EXT:
7529 7528 case SATAC_WRITE_FPDMA_QUEUED:
7530 7529 /* Write error */
7531 7530 sense->es_add_code =
7532 7531 SD_SCSI_ASC_WRITE_ERR;
7533 7532 break;
7534 7533 default:
7535 7534 /* Internal error */
7536 7535 SATA_LOG_D((
7537 7536 spx->txlt_sata_hba_inst,
7538 7537 CE_WARN,
7539 7538 "sata_txlt_rw_completion :"
7540 7539 "internal error - invalid "
7541 7540 "command 0x%2x",
7542 7541 scmd->satacmd_cmd_reg));
7543 7542 break;
7544 7543 }
7545 7544 }
7546 7545 break;
7547 7546 }
7548 7547 /* No extended sense key - no info available */
7549 7548 scsipkt->pkt_reason = CMD_INCOMPLETE;
7550 7549 break;
7551 7550
7552 7551 case SATA_PKT_TIMEOUT:
7553 7552 scsipkt->pkt_reason = CMD_TIMEOUT;
7554 7553 scsipkt->pkt_statistics |=
7555 7554 STAT_TIMEOUT | STAT_DEV_RESET;
7556 7555 sense->es_key = KEY_ABORTED_COMMAND;
7557 7556 break;
7558 7557
7559 7558 case SATA_PKT_ABORTED:
7560 7559 scsipkt->pkt_reason = CMD_ABORTED;
7561 7560 scsipkt->pkt_statistics |= STAT_ABORTED;
7562 7561 sense->es_key = KEY_ABORTED_COMMAND;
7563 7562 break;
7564 7563
7565 7564 case SATA_PKT_RESET:
7566 7565 scsipkt->pkt_reason = CMD_RESET;
7567 7566 scsipkt->pkt_statistics |= STAT_DEV_RESET;
7568 7567 sense->es_key = KEY_ABORTED_COMMAND;
7569 7568 break;
7570 7569
7571 7570 default:
7572 7571 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7573 7572 "sata_txlt_rw_completion: "
7574 7573 "invalid packet completion reason"));
7575 7574 scsipkt->pkt_reason = CMD_TRAN_ERR;
7576 7575 break;
7577 7576 }
7578 7577 }
7579 7578 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7580 7579 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7581 7580
7582 7581 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7583 7582 /* scsi callback required */
7584 7583 scsi_hba_pkt_comp(scsipkt);
7585 7584 }
7586 7585
7587 7586
7588 7587 /*
7589 7588 * Translate completion status of non-data commands (i.e. commands returning
7590 7589 * no data).
7591 7590 * pkt completion_reason is checked to determine the completion status.
7592 7591 * Do scsi callback if necessary (FLAG_NOINTR == 0)
7593 7592 *
7594 7593 * Note: this function may be called also for synchronously executed
7595 7594 * commands.
7596 7595 * This function may be used only if scsi_pkt is non-NULL.
7597 7596 */
7598 7597
7599 7598 static void
7600 7599 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt)
7601 7600 {
7602 7601 sata_pkt_txlate_t *spx =
7603 7602 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7604 7603 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7605 7604
7606 7605 sata_set_arq_data(sata_pkt);
7607 7606
7608 7607 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7609 7608 /* scsi callback required */
7610 7609 scsi_hba_pkt_comp(scsipkt);
7611 7610 }
7612 7611
7613 7612 /*
7614 7613 * Completion handler for ATA Pass Through command
7615 7614 */
7616 7615 static void
7617 7616 sata_txlt_apt_completion(sata_pkt_t *sata_pkt)
7618 7617 {
7619 7618 sata_pkt_txlate_t *spx =
7620 7619 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7621 7620 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7622 7621 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7623 7622 struct buf *bp;
7624 7623 uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0;
7625 7624
7626 7625 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7627 7626 /* Normal completion */
7628 7627 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7629 7628 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7630 7629 scsipkt->pkt_reason = CMD_CMPLT;
7631 7630 *scsipkt->pkt_scbp = STATUS_GOOD;
7632 7631
7633 7632 /*
7634 7633 * If the command has CK_COND set
7635 7634 */
7636 7635 if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) {
7637 7636 *scsipkt->pkt_scbp = STATUS_CHECK;
7638 7637 sata_fill_ata_return_desc(sata_pkt,
7639 7638 KEY_RECOVERABLE_ERROR,
7640 7639 SD_SCSI_ASC_APT_INFO_AVAIL, 0x1d);
7641 7640 }
7642 7641
7643 7642 if (spx->txlt_tmp_buf != NULL) {
7644 7643 /* Temporary buffer was used */
7645 7644 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7646 7645 if (bp->b_flags & B_READ) {
7647 7646 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7648 7647 bp->b_bcount);
7649 7648 }
7650 7649 }
7651 7650 } else {
7652 7651 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7653 7652 STATE_SENT_CMD | STATE_GOT_STATUS;
7654 7653 scsipkt->pkt_reason = CMD_INCOMPLETE;
7655 7654 *scsipkt->pkt_scbp = STATUS_CHECK;
7656 7655
7657 7656 /*
7658 7657 * If DF or ERR was set, the HBA should have copied out the
7659 7658 * status and error registers to the satacmd structure.
7660 7659 */
7661 7660 if (scmd->satacmd_status_reg & SATA_STATUS_DF) {
7662 7661 sense_key = KEY_HARDWARE_ERROR;
7663 7662 addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE;
7664 7663 addl_sense_qual = 0;
7665 7664 } else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
7666 7665 if (scmd->satacmd_error_reg & SATA_ERROR_NM) {
7667 7666 sense_key = KEY_NOT_READY;
7668 7667 addl_sense_code =
7669 7668 SD_SCSI_ASC_MEDIUM_NOT_PRESENT;
7670 7669 addl_sense_qual = 0;
7671 7670 } else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) {
7672 7671 sense_key = KEY_MEDIUM_ERROR;
7673 7672 addl_sense_code = SD_SCSI_ASC_UNREC_READ_ERR;
7674 7673 addl_sense_qual = 0;
7675 7674 } else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) {
7676 7675 sense_key = KEY_DATA_PROTECT;
7677 7676 addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED;
7678 7677 addl_sense_qual = 0;
7679 7678 } else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) {
7680 7679 sense_key = KEY_ILLEGAL_REQUEST;
7681 7680 addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7682 7681 addl_sense_qual = 0;
7683 7682 } else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
7684 7683 sense_key = KEY_ABORTED_COMMAND;
7685 7684 addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE;
7686 7685 addl_sense_qual = 0;
7687 7686 } else if (scmd->satacmd_error_reg & SATA_ERROR_MC) {
7688 7687 sense_key = KEY_UNIT_ATTENTION;
7689 7688 addl_sense_code =
7690 7689 SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED;
7691 7690 addl_sense_qual = 0;
7692 7691 } else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) {
7693 7692 sense_key = KEY_UNIT_ATTENTION;
7694 7693 addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ;
7695 7694 addl_sense_qual = 0;
7696 7695 } else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) {
7697 7696 sense_key = KEY_ABORTED_COMMAND;
7698 7697 addl_sense_code =
7699 7698 SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR;
7700 7699 addl_sense_qual = 0;
7701 7700 }
7702 7701 }
7703 7702
7704 7703 sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code,
7705 7704 addl_sense_qual);
7706 7705 }
7707 7706
7708 7707 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7709 7708 /* scsi callback required */
7710 7709 scsi_hba_pkt_comp(scsipkt);
7711 7710 }
7712 7711
7713 7712 /*
7714 7713 * Completion handler for unmap translation command
7715 7714 */
7716 7715 static void
7717 7716 sata_txlt_unmap_completion(sata_pkt_t *sata_pkt)
7718 7717 {
7719 7718 sata_pkt_txlate_t *spx =
7720 7719 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7721 7720 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7722 7721 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7723 7722 struct buf *bp;
7724 7723 uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0;
7725 7724
7726 7725 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7727 7726 /* Normal completion */
7728 7727 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7729 7728 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7730 7729 scsipkt->pkt_reason = CMD_CMPLT;
7731 7730 *scsipkt->pkt_scbp = STATUS_GOOD;
7732 7731
7733 7732 if (spx->txlt_tmp_buf != NULL) {
7734 7733 /* Temporary buffer was used */
7735 7734 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7736 7735 if (bp->b_flags & B_READ) {
7737 7736 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7738 7737 bp->b_bcount);
7739 7738 }
7740 7739 }
7741 7740 } else {
7742 7741 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7743 7742 STATE_SENT_CMD | STATE_GOT_STATUS;
7744 7743 scsipkt->pkt_reason = CMD_INCOMPLETE;
7745 7744 *scsipkt->pkt_scbp = STATUS_CHECK;
7746 7745
7747 7746 /*
7748 7747 * If DF or ERR was set, the HBA should have copied out the
7749 7748 * status and error registers to the satacmd structure.
7750 7749 */
7751 7750 if (scmd->satacmd_status_reg & SATA_STATUS_DF) {
7752 7751 sense_key = KEY_HARDWARE_ERROR;
7753 7752 addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE;
7754 7753 addl_sense_qual = 0;
7755 7754 } else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
7756 7755 if (scmd->satacmd_error_reg & SATA_ERROR_NM) {
7757 7756 sense_key = KEY_NOT_READY;
7758 7757 addl_sense_code =
7759 7758 SD_SCSI_ASC_MEDIUM_NOT_PRESENT;
7760 7759 addl_sense_qual = 0;
7761 7760 } else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) {
7762 7761 sense_key = KEY_MEDIUM_ERROR;
7763 7762 addl_sense_code = SD_SCSI_ASC_WRITE_ERR;
7764 7763 addl_sense_qual = 0;
7765 7764 } else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) {
7766 7765 sense_key = KEY_DATA_PROTECT;
7767 7766 addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED;
7768 7767 addl_sense_qual = 0;
7769 7768 } else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) {
7770 7769 sense_key = KEY_ILLEGAL_REQUEST;
7771 7770 addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7772 7771 addl_sense_qual = 0;
7773 7772 } else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
7774 7773 sense_key = KEY_ABORTED_COMMAND;
7775 7774 addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE;
7776 7775 addl_sense_qual = 0;
7777 7776 } else if (scmd->satacmd_error_reg & SATA_ERROR_MC) {
7778 7777 sense_key = KEY_UNIT_ATTENTION;
7779 7778 addl_sense_code =
7780 7779 SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED;
7781 7780 addl_sense_qual = 0;
7782 7781 } else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) {
7783 7782 sense_key = KEY_UNIT_ATTENTION;
7784 7783 addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ;
7785 7784 addl_sense_qual = 0;
7786 7785 } else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) {
7787 7786 sense_key = KEY_ABORTED_COMMAND;
7788 7787 addl_sense_code =
7789 7788 SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR;
7790 7789 addl_sense_qual = 0;
7791 7790 }
7792 7791 }
7793 7792
7794 7793 sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code,
7795 7794 addl_sense_qual);
7796 7795 }
7797 7796
7798 7797 sata_free_local_buffer(spx);
7799 7798
7800 7799 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7801 7800 /* scsi callback required */
7802 7801 scsi_hba_pkt_comp(scsipkt);
7803 7802 }
7804 7803
7805 7804 /*
7806 7805 *
7807 7806 */
7808 7807 static void
7809 7808 sata_fill_ata_return_desc(sata_pkt_t *sata_pkt, uint8_t sense_key,
7810 7809 uint8_t addl_sense_code, uint8_t addl_sense_qual)
7811 7810 {
7812 7811 sata_pkt_txlate_t *spx =
7813 7812 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7814 7813 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7815 7814 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7816 7815 struct sata_apt_sense_data *apt_sd =
7817 7816 (struct sata_apt_sense_data *)scsipkt->pkt_scbp;
7818 7817 struct scsi_descr_sense_hdr *sds = &(apt_sd->apt_sd_hdr);
7819 7818 struct scsi_ata_status_ret_sense_descr *ata_ret_desc =
7820 7819 &(apt_sd->apt_sd_sense);
7821 7820 int extend = 0;
7822 7821
7823 7822 if ((scsipkt->pkt_cdbp[0] == SPC3_CMD_ATA_COMMAND_PASS_THROUGH16) &&
7824 7823 (scsipkt->pkt_cdbp[2] & SATL_APT_BM_EXTEND))
7825 7824 extend = 1;
7826 7825
7827 7826 scsipkt->pkt_state |= STATE_ARQ_DONE;
7828 7827
7829 7828 /* update the residual count */
7830 7829 *(uchar_t *)&apt_sd->apt_status = STATUS_CHECK;
7831 7830 *(uchar_t *)&apt_sd->apt_rqpkt_status = STATUS_GOOD;
7832 7831 apt_sd->apt_rqpkt_reason = CMD_CMPLT;
7833 7832 apt_sd->apt_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7834 7833 STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
7835 7834 apt_sd->apt_rqpkt_resid = scsipkt->pkt_scblen -
7836 7835 sizeof (struct sata_apt_sense_data);
7837 7836
7838 7837 /*
7839 7838 * Fill in the Descriptor sense header
7840 7839 */
7841 7840 bzero(sds, sizeof (struct scsi_descr_sense_hdr));
7842 7841 sds->ds_code = CODE_FMT_DESCR_CURRENT;
7843 7842 sds->ds_class = CLASS_EXTENDED_SENSE;
7844 7843 sds->ds_key = sense_key & 0xf;
7845 7844 sds->ds_add_code = addl_sense_code;
7846 7845 sds->ds_qual_code = addl_sense_qual;
7847 7846 sds->ds_addl_sense_length =
7848 7847 sizeof (struct scsi_ata_status_ret_sense_descr);
7849 7848
7850 7849 /*
7851 7850 * Fill in the ATA Return descriptor sense data
7852 7851 */
7853 7852 bzero(ata_ret_desc, sizeof (struct scsi_ata_status_ret_sense_descr));
7854 7853 ata_ret_desc->ars_descr_type = DESCR_ATA_STATUS_RETURN;
7855 7854 ata_ret_desc->ars_addl_length = 0xc;
7856 7855 ata_ret_desc->ars_error = scmd->satacmd_error_reg;
7857 7856 ata_ret_desc->ars_sec_count_lsb = scmd->satacmd_sec_count_lsb;
7858 7857 ata_ret_desc->ars_lba_low_lsb = scmd->satacmd_lba_low_lsb;
7859 7858 ata_ret_desc->ars_lba_mid_lsb = scmd->satacmd_lba_mid_lsb;
7860 7859 ata_ret_desc->ars_lba_high_lsb = scmd->satacmd_lba_high_lsb;
7861 7860 ata_ret_desc->ars_device = scmd->satacmd_device_reg;
7862 7861 ata_ret_desc->ars_status = scmd->satacmd_status_reg;
7863 7862
7864 7863 if (extend == 1) {
7865 7864 ata_ret_desc->ars_extend = 1;
7866 7865 ata_ret_desc->ars_sec_count_msb = scmd->satacmd_sec_count_msb;
7867 7866 ata_ret_desc->ars_lba_low_msb = scmd->satacmd_lba_low_msb;
7868 7867 ata_ret_desc->ars_lba_mid_msb = scmd->satacmd_lba_mid_msb;
7869 7868 ata_ret_desc->ars_lba_high_msb = scmd->satacmd_lba_high_msb;
7870 7869 } else {
7871 7870 ata_ret_desc->ars_extend = 0;
7872 7871 ata_ret_desc->ars_sec_count_msb = 0;
7873 7872 ata_ret_desc->ars_lba_low_msb = 0;
7874 7873 ata_ret_desc->ars_lba_mid_msb = 0;
7875 7874 ata_ret_desc->ars_lba_high_msb = 0;
7876 7875 }
7877 7876 }
7878 7877
7879 7878 static void
7880 7879 sata_set_arq_data(sata_pkt_t *sata_pkt)
7881 7880 {
7882 7881 sata_pkt_txlate_t *spx =
7883 7882 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7884 7883 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7885 7884 struct scsi_extended_sense *sense;
7886 7885
7887 7886 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7888 7887 STATE_SENT_CMD | STATE_GOT_STATUS;
7889 7888 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7890 7889 /* Normal completion */
7891 7890 scsipkt->pkt_reason = CMD_CMPLT;
7892 7891 *scsipkt->pkt_scbp = STATUS_GOOD;
7893 7892 } else {
7894 7893 /* Something went wrong */
7895 7894 scsipkt->pkt_reason = CMD_INCOMPLETE;
7896 7895 *scsipkt->pkt_scbp = STATUS_CHECK;
7897 7896 sense = sata_arq_sense(spx);
7898 7897 switch (sata_pkt->satapkt_reason) {
7899 7898 case SATA_PKT_PORT_ERROR:
7900 7899 /*
7901 7900 * We have no device data. Assume no data transfered.
7902 7901 */
7903 7902 sense->es_key = KEY_HARDWARE_ERROR;
7904 7903 break;
7905 7904
7906 7905 case SATA_PKT_DEV_ERROR:
7907 7906 if (sata_pkt->satapkt_cmd.satacmd_status_reg &
7908 7907 SATA_STATUS_ERR) {
7909 7908 /*
7910 7909 * determine dev error reason from error
7911 7910 * reg content
7912 7911 */
7913 7912 sata_decode_device_error(spx, sense);
7914 7913 break;
7915 7914 }
7916 7915 /* No extended sense key - no info available */
7917 7916 break;
7918 7917
7919 7918 case SATA_PKT_TIMEOUT:
7920 7919 scsipkt->pkt_reason = CMD_TIMEOUT;
7921 7920 scsipkt->pkt_statistics |=
7922 7921 STAT_TIMEOUT | STAT_DEV_RESET;
7923 7922 /* No extended sense key ? */
7924 7923 break;
7925 7924
7926 7925 case SATA_PKT_ABORTED:
7927 7926 scsipkt->pkt_reason = CMD_ABORTED;
7928 7927 scsipkt->pkt_statistics |= STAT_ABORTED;
7929 7928 /* No extended sense key ? */
7930 7929 break;
7931 7930
7932 7931 case SATA_PKT_RESET:
7933 7932 /* pkt aborted by an explicit reset from a host */
7934 7933 scsipkt->pkt_reason = CMD_RESET;
7935 7934 scsipkt->pkt_statistics |= STAT_DEV_RESET;
7936 7935 break;
7937 7936
7938 7937 default:
7939 7938 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7940 7939 "sata_txlt_nodata_cmd_completion: "
7941 7940 "invalid packet completion reason %d",
7942 7941 sata_pkt->satapkt_reason));
7943 7942 scsipkt->pkt_reason = CMD_TRAN_ERR;
7944 7943 break;
7945 7944 }
7946 7945
7947 7946 }
7948 7947 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7949 7948 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7950 7949 }
7951 7950
7952 7951
7953 7952 /*
7954 7953 * Build Mode sense R/W recovery page
7955 7954 * NOT IMPLEMENTED
7956 7955 */
7957 7956
7958 7957 static int
7959 7958 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
7960 7959 {
7961 7960 #ifndef __lock_lint
7962 7961 _NOTE(ARGUNUSED(sdinfo))
7963 7962 _NOTE(ARGUNUSED(pcntrl))
7964 7963 _NOTE(ARGUNUSED(buf))
7965 7964 #endif
7966 7965 return (0);
7967 7966 }
7968 7967
7969 7968 /*
7970 7969 * Build Mode sense caching page - scsi-3 implementation.
7971 7970 * Page length distinguishes previous format from scsi-3 format.
7972 7971 * buf must have space for 0x12 bytes.
7973 7972 * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable.
7974 7973 *
7975 7974 */
7976 7975 static int
7977 7976 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
7978 7977 {
7979 7978 struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf;
7980 7979 sata_id_t *sata_id = &sdinfo->satadrv_id;
7981 7980
7982 7981 /*
7983 7982 * Most of the fields are set to 0, being not supported and/or disabled
7984 7983 */
7985 7984 bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3);
7986 7985
7987 7986 /* Saved paramters not supported */
7988 7987 if (pcntrl == 3)
7989 7988 return (0);
7990 7989 if (pcntrl == 0 || pcntrl == 2) {
7991 7990 /*
7992 7991 * For now treat current and default parameters as same
7993 7992 * That may have to change, if target driver will complain
7994 7993 */
7995 7994 page->mode_page.code = MODEPAGE_CACHING; /* PS = 0 */
7996 7995 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
7997 7996
7998 7997 if (SATA_READ_AHEAD_SUPPORTED(*sata_id) &&
7999 7998 !SATA_READ_AHEAD_ENABLED(*sata_id)) {
8000 7999 page->dra = 1; /* Read Ahead disabled */
8001 8000 page->rcd = 1; /* Read Cache disabled */
8002 8001 }
8003 8002 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id) &&
8004 8003 SATA_WRITE_CACHE_ENABLED(*sata_id))
8005 8004 page->wce = 1; /* Write Cache enabled */
8006 8005 } else {
8007 8006 /* Changeable parameters */
8008 8007 page->mode_page.code = MODEPAGE_CACHING;
8009 8008 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
8010 8009 if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
8011 8010 page->dra = 1;
8012 8011 page->rcd = 1;
8013 8012 }
8014 8013 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id))
8015 8014 page->wce = 1;
8016 8015 }
8017 8016 return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
8018 8017 sizeof (struct mode_page));
8019 8018 }
8020 8019
8021 8020 /*
8022 8021 * Build Mode sense exception cntrl page
8023 8022 */
8024 8023 static int
8025 8024 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8026 8025 {
8027 8026 struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf;
8028 8027 sata_id_t *sata_id = &sdinfo->satadrv_id;
8029 8028
8030 8029 /*
8031 8030 * Most of the fields are set to 0, being not supported and/or disabled
8032 8031 */
8033 8032 bzero(buf, PAGELENGTH_INFO_EXCPT);
8034 8033
8035 8034 page->mode_page.code = MODEPAGE_INFO_EXCPT;
8036 8035 page->mode_page.length = PAGELENGTH_INFO_EXCPT;
8037 8036
8038 8037 /* Indicate that this is page is saveable */
8039 8038 page->mode_page.ps = 1;
8040 8039
8041 8040 /*
8042 8041 * We will return the same data for default, current and saved page.
8043 8042 * The only changeable bit is dexcpt and that bit is required
8044 8043 * by the ATA specification to be preserved across power cycles.
8045 8044 */
8046 8045 if (pcntrl != 1) {
8047 8046 page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED);
8048 8047 page->mrie = MRIE_ONLY_ON_REQUEST;
8049 8048 }
8050 8049 else
8051 8050 page->dexcpt = 1; /* Only changeable parameter */
8052 8051
8053 8052 return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page));
8054 8053 }
8055 8054
8056 8055
8057 8056 static int
8058 8057 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8059 8058 {
8060 8059 struct mode_acoustic_management *page =
8061 8060 (struct mode_acoustic_management *)buf;
8062 8061 sata_id_t *sata_id = &sdinfo->satadrv_id;
8063 8062
8064 8063 /*
8065 8064 * Most of the fields are set to 0, being not supported and/or disabled
8066 8065 */
8067 8066 bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT);
8068 8067
8069 8068 switch (pcntrl) {
8070 8069 case P_CNTRL_DEFAULT:
8071 8070 /* default paramters not supported */
8072 8071 return (0);
8073 8072
8074 8073 case P_CNTRL_CURRENT:
8075 8074 case P_CNTRL_SAVED:
8076 8075 /* Saved and current are supported and are identical */
8077 8076 page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
8078 8077 page->mode_page.length =
8079 8078 PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
8080 8079 page->mode_page.ps = 1;
8081 8080
8082 8081 /* Word 83 indicates if feature is supported */
8083 8082 /* If feature is not supported */
8084 8083 if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) {
8085 8084 page->acoustic_manag_enable =
8086 8085 ACOUSTIC_DISABLED;
8087 8086 } else {
8088 8087 page->acoustic_manag_enable =
8089 8088 ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT)
8090 8089 != 0);
8091 8090 /* Word 94 inidicates the value */
8092 8091 #ifdef _LITTLE_ENDIAN
8093 8092 page->acoustic_manag_level =
8094 8093 (uchar_t)sata_id->ai_acoustic;
8095 8094 page->vendor_recommended_value =
8096 8095 sata_id->ai_acoustic >> 8;
8097 8096 #else
8098 8097 page->acoustic_manag_level =
8099 8098 sata_id->ai_acoustic >> 8;
8100 8099 page->vendor_recommended_value =
8101 8100 (uchar_t)sata_id->ai_acoustic;
8102 8101 #endif
8103 8102 }
8104 8103 break;
8105 8104
8106 8105 case P_CNTRL_CHANGEABLE:
8107 8106 page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
8108 8107 page->mode_page.length =
8109 8108 PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
8110 8109 page->mode_page.ps = 1;
8111 8110
8112 8111 /* Word 83 indicates if the feature is supported */
8113 8112 if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) {
8114 8113 page->acoustic_manag_enable =
8115 8114 ACOUSTIC_ENABLED;
8116 8115 page->acoustic_manag_level = 0xff;
8117 8116 }
8118 8117 break;
8119 8118 }
8120 8119 return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8121 8120 sizeof (struct mode_page));
8122 8121 }
8123 8122
8124 8123
8125 8124 /*
8126 8125 * Build Mode sense power condition page.
8127 8126 */
8128 8127 static int
8129 8128 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8130 8129 {
8131 8130 struct mode_info_power_cond *page = (struct mode_info_power_cond *)buf;
8132 8131 sata_id_t *sata_id = &sdinfo->satadrv_id;
8133 8132
8134 8133 /*
8135 8134 * Most of the fields are set to 0, being not supported and/or disabled
8136 8135 * power condition page length was 0x0a
8137 8136 */
8138 8137 bzero(buf, sizeof (struct mode_info_power_cond));
8139 8138
8140 8139 if (pcntrl == P_CNTRL_DEFAULT) {
8141 8140 /* default paramters not supported */
8142 8141 return (0);
8143 8142 }
8144 8143
8145 8144 page->mode_page.code = MODEPAGE_POWER_COND;
8146 8145 page->mode_page.length = sizeof (struct mode_info_power_cond);
8147 8146
8148 8147 if (sata_id->ai_cap & SATA_STANDBYTIMER) {
8149 8148 page->standby = 1;
8150 8149 bcopy(sdinfo->satadrv_standby_timer, page->standby_cond_timer,
8151 8150 sizeof (uchar_t) * 4);
8152 8151 }
8153 8152
8154 8153 return (sizeof (struct mode_info_power_cond));
8155 8154 }
8156 8155
8157 8156 /*
8158 8157 * Process mode select caching page 8 (scsi3 format only).
8159 8158 * Read Ahead (same as read cache) and Write Cache may be turned on and off
8160 8159 * if these features are supported by the device. If these features are not
8161 8160 * supported, the command will be terminated with STATUS_CHECK.
8162 8161 * This function fails only if the SET FEATURE command sent to
8163 8162 * the device fails. The page format is not verified, assuming that the
8164 8163 * target driver operates correctly - if parameters length is too short,
8165 8164 * we just drop the page.
8166 8165 * Two command may be sent if both Read Cache/Read Ahead and Write Cache
8167 8166 * setting have to be changed.
8168 8167 * SET FEATURE command is executed synchronously, i.e. we wait here until
8169 8168 * it is completed, regardless of the scsi pkt directives.
8170 8169 *
8171 8170 * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e.
8172 8171 * changing DRA will change RCD.
8173 8172 *
8174 8173 * More than one SATA command may be executed to perform operations specified
8175 8174 * by mode select pages. The first error terminates further execution.
8176 8175 * Operations performed successully are not backed-up in such case.
8177 8176 *
8178 8177 * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
8179 8178 * If operation resulted in changing device setup, dmod flag should be set to
8180 8179 * one (1). If parameters were not changed, dmod flag should be set to 0.
8181 8180 * Upon return, if operation required sending command to the device, the rval
8182 8181 * should be set to the value returned by sata_hba_start. If operation
8183 8182 * did not require device access, rval should be set to TRAN_ACCEPT.
8184 8183 * The pagelen should be set to the length of the page.
8185 8184 *
8186 8185 * This function has to be called with a port mutex held.
8187 8186 *
8188 8187 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8189 8188 */
8190 8189 int
8191 8190 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page,
8192 8191 int parmlen, int *pagelen, int *rval, int *dmod)
8193 8192 {
8194 8193 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8195 8194 sata_drive_info_t *sdinfo;
8196 8195 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8197 8196 sata_id_t *sata_id;
8198 8197 struct scsi_extended_sense *sense;
8199 8198 int wce, dra; /* Current settings */
8200 8199
8201 8200 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8202 8201 &spx->txlt_sata_pkt->satapkt_device);
8203 8202 sata_id = &sdinfo->satadrv_id;
8204 8203 *dmod = 0;
8205 8204
8206 8205 /* Verify parameters length. If too short, drop it */
8207 8206 if ((PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
8208 8207 sizeof (struct mode_page)) > parmlen) {
8209 8208 *scsipkt->pkt_scbp = STATUS_CHECK;
8210 8209 sense = sata_arq_sense(spx);
8211 8210 sense->es_key = KEY_ILLEGAL_REQUEST;
8212 8211 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8213 8212 *pagelen = parmlen;
8214 8213 *rval = TRAN_ACCEPT;
8215 8214 return (SATA_FAILURE);
8216 8215 }
8217 8216
8218 8217 *pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page);
8219 8218
8220 8219 /* Current setting of Read Ahead (and Read Cache) */
8221 8220 if (SATA_READ_AHEAD_ENABLED(*sata_id))
8222 8221 dra = 0; /* 0 == not disabled */
8223 8222 else
8224 8223 dra = 1;
8225 8224 /* Current setting of Write Cache */
8226 8225 if (SATA_WRITE_CACHE_ENABLED(*sata_id))
8227 8226 wce = 1;
8228 8227 else
8229 8228 wce = 0;
8230 8229
8231 8230 if (page->dra == dra && page->wce == wce && page->rcd == dra) {
8232 8231 /* nothing to do */
8233 8232 *rval = TRAN_ACCEPT;
8234 8233 return (SATA_SUCCESS);
8235 8234 }
8236 8235
8237 8236 /*
8238 8237 * Need to flip some setting
8239 8238 * Set-up Internal SET FEATURES command(s)
8240 8239 */
8241 8240 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8242 8241 scmd->satacmd_addr_type = 0;
8243 8242 scmd->satacmd_device_reg = 0;
8244 8243 scmd->satacmd_status_reg = 0;
8245 8244 scmd->satacmd_error_reg = 0;
8246 8245 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
8247 8246 if (page->dra != dra || page->rcd != dra) {
8248 8247 if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
8249 8248 /* Need to flip read ahead setting */
8250 8249 if (dra == 0)
8251 8250 /* Disable read ahead / read cache */
8252 8251 scmd->satacmd_features_reg =
8253 8252 SATAC_SF_DISABLE_READ_AHEAD;
8254 8253 else
8255 8254 /* Enable read ahead / read cache */
8256 8255 scmd->satacmd_features_reg =
8257 8256 SATAC_SF_ENABLE_READ_AHEAD;
8258 8257
8259 8258 /* Transfer command to HBA */
8260 8259 if (sata_hba_start(spx, rval) != 0)
8261 8260 /*
8262 8261 * Pkt not accepted for execution.
8263 8262 */
8264 8263 return (SATA_FAILURE);
8265 8264
8266 8265 *dmod = 1;
8267 8266
8268 8267 /* Now process return */
8269 8268 if (spx->txlt_sata_pkt->satapkt_reason !=
8270 8269 SATA_PKT_COMPLETED) {
8271 8270 goto failure; /* Terminate */
8272 8271 }
8273 8272 } else {
8274 8273 *scsipkt->pkt_scbp = STATUS_CHECK;
8275 8274 sense = sata_arq_sense(spx);
8276 8275 sense->es_key = KEY_ILLEGAL_REQUEST;
8277 8276 sense->es_add_code =
8278 8277 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8279 8278 *pagelen = parmlen;
8280 8279 *rval = TRAN_ACCEPT;
8281 8280 return (SATA_FAILURE);
8282 8281 }
8283 8282 }
8284 8283
8285 8284 /* Note that the packet is not removed, so it could be re-used */
8286 8285 if (page->wce != wce) {
8287 8286 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id)) {
8288 8287 /* Need to flip Write Cache setting */
8289 8288 if (page->wce == 1)
8290 8289 /* Enable write cache */
8291 8290 scmd->satacmd_features_reg =
8292 8291 SATAC_SF_ENABLE_WRITE_CACHE;
8293 8292 else
8294 8293 /* Disable write cache */
8295 8294 scmd->satacmd_features_reg =
8296 8295 SATAC_SF_DISABLE_WRITE_CACHE;
8297 8296
8298 8297 /* Transfer command to HBA */
8299 8298 if (sata_hba_start(spx, rval) != 0)
8300 8299 /*
8301 8300 * Pkt not accepted for execution.
8302 8301 */
8303 8302 return (SATA_FAILURE);
8304 8303
8305 8304 *dmod = 1;
8306 8305
8307 8306 /* Now process return */
8308 8307 if (spx->txlt_sata_pkt->satapkt_reason !=
8309 8308 SATA_PKT_COMPLETED) {
8310 8309 goto failure;
8311 8310 }
8312 8311 } else {
8313 8312 *scsipkt->pkt_scbp = STATUS_CHECK;
8314 8313 sense = sata_arq_sense(spx);
8315 8314 sense->es_key = KEY_ILLEGAL_REQUEST;
8316 8315 sense->es_add_code =
8317 8316 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8318 8317 *pagelen = parmlen;
8319 8318 *rval = TRAN_ACCEPT;
8320 8319 return (SATA_FAILURE);
8321 8320 }
8322 8321 }
8323 8322 return (SATA_SUCCESS);
8324 8323
8325 8324 failure:
8326 8325 sata_xlate_errors(spx);
8327 8326
8328 8327 return (SATA_FAILURE);
8329 8328 }
8330 8329
8331 8330 /*
8332 8331 * Process mode select informational exceptions control page 0x1c
8333 8332 *
8334 8333 * The only changeable bit is dexcpt (disable exceptions).
8335 8334 * MRIE (method of reporting informational exceptions) must be
8336 8335 * "only on request".
8337 8336 * This page applies to informational exceptions that report
8338 8337 * additional sense codes with the ADDITIONAL SENSE CODE field set to 5Dh
8339 8338 * (e.g.,FAILURE PREDICTION THRESHOLD EXCEEDED) or 0Bh (e.g., WARNING_).
8340 8339 * Informational exception conditions occur as the result of background scan
8341 8340 * errors, background self-test errors, or vendor specific events within a
8342 8341 * logical unit. An informational exception condition may occur asynchronous
8343 8342 * to any commands.
8344 8343 *
8345 8344 * Returns: SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
8346 8345 * If operation resulted in changing device setup, dmod flag should be set to
8347 8346 * one (1). If parameters were not changed, dmod flag should be set to 0.
8348 8347 * Upon return, if operation required sending command to the device, the rval
8349 8348 * should be set to the value returned by sata_hba_start. If operation
8350 8349 * did not require device access, rval should be set to TRAN_ACCEPT.
8351 8350 * The pagelen should be set to the length of the page.
8352 8351 *
8353 8352 * This function has to be called with a port mutex held.
8354 8353 *
8355 8354 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8356 8355 *
8357 8356 * Cannot be called in the interrupt context.
8358 8357 */
8359 8358 static int
8360 8359 sata_mode_select_page_1c(
8361 8360 sata_pkt_txlate_t *spx,
8362 8361 struct mode_info_excpt_page *page,
8363 8362 int parmlen,
8364 8363 int *pagelen,
8365 8364 int *rval,
8366 8365 int *dmod)
8367 8366 {
8368 8367 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8369 8368 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8370 8369 sata_drive_info_t *sdinfo;
8371 8370 sata_id_t *sata_id;
8372 8371 struct scsi_extended_sense *sense;
8373 8372
8374 8373 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8375 8374 &spx->txlt_sata_pkt->satapkt_device);
8376 8375 sata_id = &sdinfo->satadrv_id;
8377 8376
8378 8377 *dmod = 0;
8379 8378
8380 8379 /* Verify parameters length. If too short, drop it */
8381 8380 if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) > parmlen) ||
8382 8381 page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) {
8383 8382 *scsipkt->pkt_scbp = STATUS_CHECK;
8384 8383 sense = sata_arq_sense(spx);
8385 8384 sense->es_key = KEY_ILLEGAL_REQUEST;
8386 8385 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8387 8386 *pagelen = parmlen;
8388 8387 *rval = TRAN_ACCEPT;
8389 8388 return (SATA_FAILURE);
8390 8389 }
8391 8390
8392 8391 *pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page);
8393 8392
8394 8393 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
8395 8394 *scsipkt->pkt_scbp = STATUS_CHECK;
8396 8395 sense = sata_arq_sense(spx);
8397 8396 sense->es_key = KEY_ILLEGAL_REQUEST;
8398 8397 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
8399 8398 *pagelen = parmlen;
8400 8399 *rval = TRAN_ACCEPT;
8401 8400 return (SATA_FAILURE);
8402 8401 }
8403 8402
8404 8403 /* If already in the state requested, we are done */
8405 8404 if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
8406 8405 /* nothing to do */
8407 8406 *rval = TRAN_ACCEPT;
8408 8407 return (SATA_SUCCESS);
8409 8408 }
8410 8409
8411 8410 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8412 8411
8413 8412 /* Build SMART_ENABLE or SMART_DISABLE command */
8414 8413 scmd->satacmd_addr_type = 0; /* N/A */
8415 8414 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
8416 8415 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
8417 8416 scmd->satacmd_features_reg = page->dexcpt ?
8418 8417 SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS;
8419 8418 scmd->satacmd_device_reg = 0; /* Always device 0 */
8420 8419 scmd->satacmd_cmd_reg = SATAC_SMART;
8421 8420
8422 8421 /* Transfer command to HBA */
8423 8422 if (sata_hba_start(spx, rval) != 0)
8424 8423 /*
8425 8424 * Pkt not accepted for execution.
8426 8425 */
8427 8426 return (SATA_FAILURE);
8428 8427
8429 8428 *dmod = 1; /* At least may have been modified */
8430 8429
8431 8430 /* Now process return */
8432 8431 if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED)
8433 8432 return (SATA_SUCCESS);
8434 8433
8435 8434 /* Packet did not complete successfully */
8436 8435 sata_xlate_errors(spx);
8437 8436
8438 8437 return (SATA_FAILURE);
8439 8438 }
8440 8439
8441 8440 /*
8442 8441 * Process mode select acoustic management control page 0x30
8443 8442 *
8444 8443 *
8445 8444 * This function has to be called with a port mutex held.
8446 8445 *
8447 8446 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8448 8447 *
8449 8448 * Cannot be called in the interrupt context.
8450 8449 */
8451 8450 int
8452 8451 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct
8453 8452 mode_acoustic_management *page, int parmlen, int *pagelen,
8454 8453 int *rval, int *dmod)
8455 8454 {
8456 8455 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8457 8456 sata_drive_info_t *sdinfo;
8458 8457 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8459 8458 sata_id_t *sata_id;
8460 8459 struct scsi_extended_sense *sense;
8461 8460
8462 8461 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8463 8462 &spx->txlt_sata_pkt->satapkt_device);
8464 8463 sata_id = &sdinfo->satadrv_id;
8465 8464 *dmod = 0;
8466 8465
8467 8466 /* If parmlen is too short or the feature is not supported, drop it */
8468 8467 if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8469 8468 sizeof (struct mode_page)) > parmlen) ||
8470 8469 (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) {
8471 8470 *scsipkt->pkt_scbp = STATUS_CHECK;
8472 8471 sense = sata_arq_sense(spx);
8473 8472 sense->es_key = KEY_ILLEGAL_REQUEST;
8474 8473 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8475 8474 *pagelen = parmlen;
8476 8475 *rval = TRAN_ACCEPT;
8477 8476 return (SATA_FAILURE);
8478 8477 }
8479 8478
8480 8479 *pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8481 8480 sizeof (struct mode_page);
8482 8481
8483 8482 /*
8484 8483 * We can enable and disable acoustice management and
8485 8484 * set the acoustic management level.
8486 8485 */
8487 8486
8488 8487 /*
8489 8488 * Set-up Internal SET FEATURES command(s)
8490 8489 */
8491 8490 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8492 8491 scmd->satacmd_addr_type = 0;
8493 8492 scmd->satacmd_device_reg = 0;
8494 8493 scmd->satacmd_status_reg = 0;
8495 8494 scmd->satacmd_error_reg = 0;
8496 8495 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
8497 8496 if (page->acoustic_manag_enable) {
8498 8497 scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC;
8499 8498 scmd->satacmd_sec_count_lsb = page->acoustic_manag_level;
8500 8499 } else { /* disabling acoustic management */
8501 8500 scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC;
8502 8501 }
8503 8502
8504 8503 /* Transfer command to HBA */
8505 8504 if (sata_hba_start(spx, rval) != 0)
8506 8505 /*
8507 8506 * Pkt not accepted for execution.
8508 8507 */
8509 8508 return (SATA_FAILURE);
8510 8509
8511 8510 /* Now process return */
8512 8511 if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) {
8513 8512 sata_xlate_errors(spx);
8514 8513 return (SATA_FAILURE);
8515 8514 }
8516 8515
8517 8516 *dmod = 1;
8518 8517
8519 8518 return (SATA_SUCCESS);
8520 8519 }
8521 8520
8522 8521 /*
8523 8522 * Process mode select power condition page 0x1a
8524 8523 *
8525 8524 * This function has to be called with a port mutex held.
8526 8525 *
8527 8526 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8528 8527 *
8529 8528 * Cannot be called in the interrupt context.
8530 8529 */
8531 8530 int
8532 8531 sata_mode_select_page_1a(sata_pkt_txlate_t *spx, struct
8533 8532 mode_info_power_cond *page, int parmlen, int *pagelen,
8534 8533 int *rval, int *dmod)
8535 8534 {
8536 8535 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8537 8536 sata_drive_info_t *sdinfo;
8538 8537 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8539 8538 sata_id_t *sata_id;
8540 8539 struct scsi_extended_sense *sense;
8541 8540 uint8_t ata_count;
8542 8541 int i, len;
8543 8542
8544 8543 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8545 8544 &spx->txlt_sata_pkt->satapkt_device);
8546 8545 sata_id = &sdinfo->satadrv_id;
8547 8546 *dmod = 0;
8548 8547
8549 8548 len = sizeof (struct mode_info_power_cond);
8550 8549 len += sizeof (struct mode_page);
8551 8550
8552 8551 /* If parmlen is too short or the feature is not supported, drop it */
8553 8552 if ((len < parmlen) || (page->idle == 1) ||
8554 8553 (!(sata_id->ai_cap & SATA_STANDBYTIMER) && page->standby == 1)) {
8555 8554 *scsipkt->pkt_scbp = STATUS_CHECK;
8556 8555 sense = sata_arq_sense(spx);
8557 8556 sense->es_key = KEY_ILLEGAL_REQUEST;
8558 8557 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8559 8558 *pagelen = parmlen;
8560 8559 *rval = TRAN_ACCEPT;
8561 8560 return (SATA_FAILURE);
8562 8561 }
8563 8562
8564 8563 *pagelen = len;
8565 8564
8566 8565 /*
8567 8566 * Set-up Internal STANDBY command(s)
8568 8567 */
8569 8568 if (page->standby == 0)
8570 8569 goto out;
8571 8570
8572 8571 ata_count = sata_get_standby_timer(page->standby_cond_timer);
8573 8572
8574 8573 scmd->satacmd_addr_type = 0;
8575 8574 scmd->satacmd_sec_count_lsb = ata_count;
8576 8575 scmd->satacmd_lba_low_lsb = 0;
8577 8576 scmd->satacmd_lba_mid_lsb = 0;
8578 8577 scmd->satacmd_lba_high_lsb = 0;
8579 8578 scmd->satacmd_features_reg = 0;
8580 8579 scmd->satacmd_device_reg = 0;
8581 8580 scmd->satacmd_status_reg = 0;
8582 8581 scmd->satacmd_cmd_reg = SATAC_STANDBY;
8583 8582 scmd->satacmd_flags.sata_special_regs = B_TRUE;
8584 8583 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
8585 8584
8586 8585 /* Transfer command to HBA */
8587 8586 if (sata_hba_start(spx, rval) != 0) {
8588 8587 return (SATA_FAILURE);
8589 8588 } else {
8590 8589 if ((scmd->satacmd_error_reg != 0) ||
8591 8590 (spx->txlt_sata_pkt->satapkt_reason !=
8592 8591 SATA_PKT_COMPLETED)) {
8593 8592 sata_xlate_errors(spx);
8594 8593 return (SATA_FAILURE);
8595 8594 }
8596 8595 }
8597 8596
8598 8597 for (i = 0; i < 4; i++) {
8599 8598 sdinfo->satadrv_standby_timer[i] = page->standby_cond_timer[i];
8600 8599 }
8601 8600 out:
8602 8601 *dmod = 1;
8603 8602 return (SATA_SUCCESS);
8604 8603 }
8605 8604
8606 8605 /*
8607 8606 * sata_build_lsense_page0() is used to create the
8608 8607 * SCSI LOG SENSE page 0 (supported log pages)
8609 8608 *
8610 8609 * Currently supported pages are 0, 0x10, 0x2f, 0x30 and 0x0e
8611 8610 * (supported log pages, self-test results, informational exceptions
8612 8611 * Sun vendor specific ATA SMART data, and start stop cycle counter).
8613 8612 *
8614 8613 * Takes a sata_drive_info t * and the address of a buffer
8615 8614 * in which to create the page information.
8616 8615 *
8617 8616 * Returns the number of bytes valid in the buffer.
8618 8617 */
8619 8618 static int
8620 8619 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf)
8621 8620 {
8622 8621 struct log_parameter *lpp = (struct log_parameter *)buf;
8623 8622 uint8_t *page_ptr = (uint8_t *)lpp->param_values;
8624 8623 int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */
8625 8624 sata_id_t *sata_id = &sdinfo->satadrv_id;
8626 8625
8627 8626 lpp->param_code[0] = 0;
8628 8627 lpp->param_code[1] = 0;
8629 8628 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
8630 8629 *page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES;
8631 8630
8632 8631 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
8633 8632 if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) {
8634 8633 *page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS;
8635 8634 ++num_pages_supported;
8636 8635 }
8637 8636 *page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS;
8638 8637 ++num_pages_supported;
8639 8638 *page_ptr++ = PAGE_CODE_SMART_READ_DATA;
8640 8639 ++num_pages_supported;
8641 8640 *page_ptr++ = PAGE_CODE_START_STOP_CYCLE_COUNTER;
8642 8641 ++num_pages_supported;
8643 8642 }
8644 8643
8645 8644 lpp->param_len = num_pages_supported;
8646 8645
8647 8646 return ((&lpp->param_values[0] - (uint8_t *)lpp) +
8648 8647 num_pages_supported);
8649 8648 }
8650 8649
8651 8650 /*
8652 8651 * sata_build_lsense_page_10() is used to create the
8653 8652 * SCSI LOG SENSE page 0x10 (self-test results)
8654 8653 *
8655 8654 * Takes a sata_drive_info t * and the address of a buffer
8656 8655 * in which to create the page information as well as a sata_hba_inst_t *.
8657 8656 *
8658 8657 * Returns the number of bytes valid in the buffer.
8659 8658 *
8660 8659 * Note: Self test and SMART data is accessible in device log pages.
8661 8660 * The log pages can be accessed by SMART READ/WRITE LOG (up to 255 sectors
8662 8661 * of data can be transferred by a single command), or by the General Purpose
8663 8662 * Logging commands (GPL) READ LOG EXT and WRITE LOG EXT (up to 65,535 sectors
8664 8663 * - approximately 33MB - can be transferred by a single command.
8665 8664 * The SCT Command response (either error or command) is the same for both
8666 8665 * the SMART and GPL methods of issuing commands.
8667 8666 * This function uses READ LOG EXT command when drive supports LBA48, and
8668 8667 * SMART READ command otherwise.
8669 8668 *
8670 8669 * Since above commands are executed in a synchronous mode, this function
8671 8670 * should not be called in an interrupt context.
8672 8671 */
8673 8672 static int
8674 8673 sata_build_lsense_page_10(
8675 8674 sata_drive_info_t *sdinfo,
8676 8675 uint8_t *buf,
8677 8676 sata_hba_inst_t *sata_hba_inst)
8678 8677 {
8679 8678 struct log_parameter *lpp = (struct log_parameter *)buf;
8680 8679 int rval;
8681 8680
8682 8681 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
8683 8682 struct smart_ext_selftest_log *ext_selftest_log;
8684 8683
8685 8684 ext_selftest_log = kmem_zalloc(
8686 8685 sizeof (struct smart_ext_selftest_log), KM_SLEEP);
8687 8686
8688 8687 rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo,
8689 8688 ext_selftest_log, 0);
8690 8689 if (rval == 0) {
8691 8690 int index, start_index;
8692 8691 struct smart_ext_selftest_log_entry *entry;
8693 8692 static const struct smart_ext_selftest_log_entry empty =
8694 8693 {0};
8695 8694 uint16_t block_num;
8696 8695 int count;
8697 8696 boolean_t only_one_block = B_FALSE;
8698 8697
8699 8698 index = ext_selftest_log->
8700 8699 smart_ext_selftest_log_index[0];
8701 8700 index |= ext_selftest_log->
8702 8701 smart_ext_selftest_log_index[1] << 8;
8703 8702 if (index == 0)
8704 8703 goto out;
8705 8704
8706 8705 --index; /* Correct for 0 origin */
8707 8706 start_index = index; /* remember where we started */
8708 8707 block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8709 8708 if (block_num != 0) {
8710 8709 rval = sata_ext_smart_selftest_read_log(
8711 8710 sata_hba_inst, sdinfo, ext_selftest_log,
8712 8711 block_num);
8713 8712 if (rval != 0)
8714 8713 goto out;
8715 8714 }
8716 8715 index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8717 8716 entry =
8718 8717 &ext_selftest_log->
8719 8718 smart_ext_selftest_log_entries[index];
8720 8719
8721 8720 for (count = 1;
8722 8721 count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
8723 8722 ++count) {
8724 8723 uint8_t status;
8725 8724 uint8_t code;
8726 8725 uint8_t sense_key;
8727 8726 uint8_t add_sense_code;
8728 8727 uint8_t add_sense_code_qual;
8729 8728
8730 8729 /* If this is an unused entry, we are done */
8731 8730 if (bcmp(entry, &empty, sizeof (empty)) == 0) {
8732 8731 /* Broken firmware on some disks */
8733 8732 if (index + 1 ==
8734 8733 ENTRIES_PER_EXT_SELFTEST_LOG_BLK) {
8735 8734 --entry;
8736 8735 --index;
8737 8736 if (bcmp(entry, &empty,
8738 8737 sizeof (empty)) == 0)
8739 8738 goto out;
8740 8739 } else
8741 8740 goto out;
8742 8741 }
8743 8742
8744 8743 if (only_one_block &&
8745 8744 start_index == index)
8746 8745 goto out;
8747 8746
8748 8747 lpp->param_code[0] = 0;
8749 8748 lpp->param_code[1] = count;
8750 8749 lpp->param_ctrl_flags =
8751 8750 LOG_CTRL_LP | LOG_CTRL_LBIN;
8752 8751 lpp->param_len =
8753 8752 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
8754 8753
8755 8754 status = entry->smart_ext_selftest_log_status;
8756 8755 status >>= 4;
8757 8756 switch (status) {
8758 8757 case 0:
8759 8758 default:
8760 8759 sense_key = KEY_NO_SENSE;
8761 8760 add_sense_code =
8762 8761 SD_SCSI_ASC_NO_ADD_SENSE;
8763 8762 add_sense_code_qual = 0;
8764 8763 break;
8765 8764 case 1:
8766 8765 sense_key = KEY_ABORTED_COMMAND;
8767 8766 add_sense_code =
8768 8767 DIAGNOSTIC_FAILURE_ON_COMPONENT;
8769 8768 add_sense_code_qual = SCSI_COMPONENT_81;
8770 8769 break;
8771 8770 case 2:
8772 8771 sense_key = KEY_ABORTED_COMMAND;
8773 8772 add_sense_code =
8774 8773 DIAGNOSTIC_FAILURE_ON_COMPONENT;
8775 8774 add_sense_code_qual = SCSI_COMPONENT_82;
8776 8775 break;
8777 8776 case 3:
8778 8777 sense_key = KEY_ABORTED_COMMAND;
8779 8778 add_sense_code =
8780 8779 DIAGNOSTIC_FAILURE_ON_COMPONENT;
8781 8780 add_sense_code_qual = SCSI_COMPONENT_83;
8782 8781 break;
8783 8782 case 4:
8784 8783 sense_key = KEY_HARDWARE_ERROR;
8785 8784 add_sense_code =
8786 8785 DIAGNOSTIC_FAILURE_ON_COMPONENT;
8787 8786 add_sense_code_qual = SCSI_COMPONENT_84;
8788 8787 break;
8789 8788 case 5:
8790 8789 sense_key = KEY_HARDWARE_ERROR;
8791 8790 add_sense_code =
8792 8791 DIAGNOSTIC_FAILURE_ON_COMPONENT;
8793 8792 add_sense_code_qual = SCSI_COMPONENT_85;
8794 8793 break;
8795 8794 case 6:
8796 8795 sense_key = KEY_HARDWARE_ERROR;
8797 8796 add_sense_code =
8798 8797 DIAGNOSTIC_FAILURE_ON_COMPONENT;
8799 8798 add_sense_code_qual = SCSI_COMPONENT_86;
8800 8799 break;
8801 8800 case 7:
8802 8801 sense_key = KEY_MEDIUM_ERROR;
8803 8802 add_sense_code =
8804 8803 DIAGNOSTIC_FAILURE_ON_COMPONENT;
8805 8804 add_sense_code_qual = SCSI_COMPONENT_87;
8806 8805 break;
8807 8806 case 8:
8808 8807 sense_key = KEY_HARDWARE_ERROR;
8809 8808 add_sense_code =
8810 8809 DIAGNOSTIC_FAILURE_ON_COMPONENT;
8811 8810 add_sense_code_qual = SCSI_COMPONENT_88;
8812 8811 break;
8813 8812 }
8814 8813 code = 0; /* unspecified */
8815 8814 status |= (code << 4);
8816 8815 lpp->param_values[0] = status;
8817 8816 lpp->param_values[1] = 0; /* unspecified */
8818 8817 lpp->param_values[2] = entry->
8819 8818 smart_ext_selftest_log_timestamp[1];
8820 8819 lpp->param_values[3] = entry->
8821 8820 smart_ext_selftest_log_timestamp[0];
8822 8821 if (status != 0) {
8823 8822 lpp->param_values[4] = 0;
8824 8823 lpp->param_values[5] = 0;
8825 8824 lpp->param_values[6] = entry->
8826 8825 smart_ext_selftest_log_failing_lba
8827 8826 [5];
8828 8827 lpp->param_values[7] = entry->
8829 8828 smart_ext_selftest_log_failing_lba
8830 8829 [4];
8831 8830 lpp->param_values[8] = entry->
8832 8831 smart_ext_selftest_log_failing_lba
8833 8832 [3];
8834 8833 lpp->param_values[9] = entry->
8835 8834 smart_ext_selftest_log_failing_lba
8836 8835 [2];
8837 8836 lpp->param_values[10] = entry->
8838 8837 smart_ext_selftest_log_failing_lba
8839 8838 [1];
8840 8839 lpp->param_values[11] = entry->
8841 8840 smart_ext_selftest_log_failing_lba
8842 8841 [0];
8843 8842 } else { /* No bad block address */
8844 8843 lpp->param_values[4] = 0xff;
8845 8844 lpp->param_values[5] = 0xff;
8846 8845 lpp->param_values[6] = 0xff;
8847 8846 lpp->param_values[7] = 0xff;
8848 8847 lpp->param_values[8] = 0xff;
8849 8848 lpp->param_values[9] = 0xff;
8850 8849 lpp->param_values[10] = 0xff;
8851 8850 lpp->param_values[11] = 0xff;
8852 8851 }
8853 8852
8854 8853 lpp->param_values[12] = sense_key;
8855 8854 lpp->param_values[13] = add_sense_code;
8856 8855 lpp->param_values[14] = add_sense_code_qual;
8857 8856 lpp->param_values[15] = 0; /* undefined */
8858 8857
8859 8858 lpp = (struct log_parameter *)
8860 8859 (((uint8_t *)lpp) +
8861 8860 SCSI_LOG_PARAM_HDR_LEN +
8862 8861 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
8863 8862
8864 8863 --index; /* Back up to previous entry */
8865 8864 if (index < 0) {
8866 8865 if (block_num > 0) {
8867 8866 --block_num;
8868 8867 } else {
8869 8868 struct read_log_ext_directory
8870 8869 logdir;
8871 8870
8872 8871 rval =
8873 8872 sata_read_log_ext_directory(
8874 8873 sata_hba_inst, sdinfo,
8875 8874 &logdir);
8876 8875 if (rval == -1)
8877 8876 goto out;
8878 8877 if ((logdir.read_log_ext_vers
8879 8878 [0] == 0) &&
8880 8879 (logdir.read_log_ext_vers
8881 8880 [1] == 0))
8882 8881 goto out;
8883 8882 block_num =
8884 8883 logdir.read_log_ext_nblks
8885 8884 [EXT_SMART_SELFTEST_LOG_PAGE
8886 8885 - 1][0];
8887 8886 block_num |= logdir.
8888 8887 read_log_ext_nblks
8889 8888 [EXT_SMART_SELFTEST_LOG_PAGE
8890 8889 - 1][1] << 8;
8891 8890 --block_num;
8892 8891 only_one_block =
8893 8892 (block_num == 0);
8894 8893 }
8895 8894 rval = sata_ext_smart_selftest_read_log(
8896 8895 sata_hba_inst, sdinfo,
8897 8896 ext_selftest_log, block_num);
8898 8897 if (rval != 0)
8899 8898 goto out;
8900 8899
8901 8900 index =
8902 8901 ENTRIES_PER_EXT_SELFTEST_LOG_BLK -
8903 8902 1;
8904 8903 }
8905 8904 index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8906 8905 entry = &ext_selftest_log->
8907 8906 smart_ext_selftest_log_entries[index];
8908 8907 }
8909 8908 }
8910 8909 out:
8911 8910 kmem_free(ext_selftest_log,
8912 8911 sizeof (struct smart_ext_selftest_log));
8913 8912 } else {
8914 8913 struct smart_selftest_log *selftest_log;
8915 8914
8916 8915 selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log),
8917 8916 KM_SLEEP);
8918 8917
8919 8918 rval = sata_smart_selftest_log(sata_hba_inst, sdinfo,
8920 8919 selftest_log);
8921 8920
8922 8921 if (rval == 0) {
8923 8922 int index;
8924 8923 int count;
8925 8924 struct smart_selftest_log_entry *entry;
8926 8925 static const struct smart_selftest_log_entry empty =
8927 8926 { 0 };
8928 8927
8929 8928 index = selftest_log->smart_selftest_log_index;
8930 8929 if (index == 0)
8931 8930 goto done;
8932 8931 --index; /* Correct for 0 origin */
8933 8932 entry = &selftest_log->
8934 8933 smart_selftest_log_entries[index];
8935 8934 for (count = 1;
8936 8935 count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
8937 8936 ++count) {
8938 8937 uint8_t status;
8939 8938 uint8_t code;
8940 8939 uint8_t sense_key;
8941 8940 uint8_t add_sense_code;
8942 8941 uint8_t add_sense_code_qual;
8943 8942
8944 8943 if (bcmp(entry, &empty, sizeof (empty)) == 0)
8945 8944 goto done;
8946 8945
8947 8946 lpp->param_code[0] = 0;
8948 8947 lpp->param_code[1] = count;
8949 8948 lpp->param_ctrl_flags =
8950 8949 LOG_CTRL_LP | LOG_CTRL_LBIN;
8951 8950 lpp->param_len =
8952 8951 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
8953 8952
8954 8953 status = entry->smart_selftest_log_status;
8955 8954 status >>= 4;
8956 8955 switch (status) {
8957 8956 case 0:
8958 8957 default:
8959 8958 sense_key = KEY_NO_SENSE;
8960 8959 add_sense_code =
8961 8960 SD_SCSI_ASC_NO_ADD_SENSE;
8962 8961 break;
8963 8962 case 1:
8964 8963 sense_key = KEY_ABORTED_COMMAND;
8965 8964 add_sense_code =
8966 8965 DIAGNOSTIC_FAILURE_ON_COMPONENT;
8967 8966 add_sense_code_qual = SCSI_COMPONENT_81;
8968 8967 break;
8969 8968 case 2:
8970 8969 sense_key = KEY_ABORTED_COMMAND;
8971 8970 add_sense_code =
8972 8971 DIAGNOSTIC_FAILURE_ON_COMPONENT;
8973 8972 add_sense_code_qual = SCSI_COMPONENT_82;
8974 8973 break;
8975 8974 case 3:
8976 8975 sense_key = KEY_ABORTED_COMMAND;
8977 8976 add_sense_code =
8978 8977 DIAGNOSTIC_FAILURE_ON_COMPONENT;
8979 8978 add_sense_code_qual = SCSI_COMPONENT_83;
8980 8979 break;
8981 8980 case 4:
8982 8981 sense_key = KEY_HARDWARE_ERROR;
8983 8982 add_sense_code =
8984 8983 DIAGNOSTIC_FAILURE_ON_COMPONENT;
8985 8984 add_sense_code_qual = SCSI_COMPONENT_84;
8986 8985 break;
8987 8986 case 5:
8988 8987 sense_key = KEY_HARDWARE_ERROR;
8989 8988 add_sense_code =
8990 8989 DIAGNOSTIC_FAILURE_ON_COMPONENT;
8991 8990 add_sense_code_qual = SCSI_COMPONENT_85;
8992 8991 break;
8993 8992 case 6:
8994 8993 sense_key = KEY_HARDWARE_ERROR;
8995 8994 add_sense_code =
8996 8995 DIAGNOSTIC_FAILURE_ON_COMPONENT;
8997 8996 add_sense_code_qual = SCSI_COMPONENT_86;
8998 8997 break;
8999 8998 case 7:
9000 8999 sense_key = KEY_MEDIUM_ERROR;
9001 9000 add_sense_code =
9002 9001 DIAGNOSTIC_FAILURE_ON_COMPONENT;
9003 9002 add_sense_code_qual = SCSI_COMPONENT_87;
9004 9003 break;
9005 9004 case 8:
9006 9005 sense_key = KEY_HARDWARE_ERROR;
9007 9006 add_sense_code =
9008 9007 DIAGNOSTIC_FAILURE_ON_COMPONENT;
9009 9008 add_sense_code_qual = SCSI_COMPONENT_88;
9010 9009 break;
9011 9010 }
9012 9011 code = 0; /* unspecified */
9013 9012 status |= (code << 4);
9014 9013 lpp->param_values[0] = status;
9015 9014 lpp->param_values[1] = 0; /* unspecified */
9016 9015 lpp->param_values[2] = entry->
9017 9016 smart_selftest_log_timestamp[1];
9018 9017 lpp->param_values[3] = entry->
9019 9018 smart_selftest_log_timestamp[0];
9020 9019 if (status != 0) {
9021 9020 lpp->param_values[4] = 0;
9022 9021 lpp->param_values[5] = 0;
9023 9022 lpp->param_values[6] = 0;
9024 9023 lpp->param_values[7] = 0;
9025 9024 lpp->param_values[8] = entry->
9026 9025 smart_selftest_log_failing_lba[3];
9027 9026 lpp->param_values[9] = entry->
9028 9027 smart_selftest_log_failing_lba[2];
9029 9028 lpp->param_values[10] = entry->
9030 9029 smart_selftest_log_failing_lba[1];
9031 9030 lpp->param_values[11] = entry->
9032 9031 smart_selftest_log_failing_lba[0];
9033 9032 } else { /* No block address */
9034 9033 lpp->param_values[4] = 0xff;
9035 9034 lpp->param_values[5] = 0xff;
9036 9035 lpp->param_values[6] = 0xff;
9037 9036 lpp->param_values[7] = 0xff;
9038 9037 lpp->param_values[8] = 0xff;
9039 9038 lpp->param_values[9] = 0xff;
9040 9039 lpp->param_values[10] = 0xff;
9041 9040 lpp->param_values[11] = 0xff;
9042 9041 }
9043 9042 lpp->param_values[12] = sense_key;
9044 9043 lpp->param_values[13] = add_sense_code;
9045 9044 lpp->param_values[14] = add_sense_code_qual;
9046 9045 lpp->param_values[15] = 0; /* undefined */
9047 9046
9048 9047 lpp = (struct log_parameter *)
9049 9048 (((uint8_t *)lpp) +
9050 9049 SCSI_LOG_PARAM_HDR_LEN +
9051 9050 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
9052 9051 --index; /* back up to previous entry */
9053 9052 if (index < 0) {
9054 9053 index =
9055 9054 NUM_SMART_SELFTEST_LOG_ENTRIES - 1;
9056 9055 }
9057 9056 entry = &selftest_log->
9058 9057 smart_selftest_log_entries[index];
9059 9058 }
9060 9059 }
9061 9060 done:
9062 9061 kmem_free(selftest_log, sizeof (struct smart_selftest_log));
9063 9062 }
9064 9063
9065 9064 return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) *
9066 9065 SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS);
9067 9066 }
9068 9067
9069 9068 /*
9070 9069 * sata_build_lsense_page_2f() is used to create the
9071 9070 * SCSI LOG SENSE page 0x2f (informational exceptions)
9072 9071 *
9073 9072 * Takes a sata_drive_info t * and the address of a buffer
9074 9073 * in which to create the page information as well as a sata_hba_inst_t *.
9075 9074 *
9076 9075 * Returns the number of bytes valid in the buffer.
9077 9076 *
9078 9077 * Because it invokes function(s) that send synchronously executed command
9079 9078 * to the HBA, it cannot be called in the interrupt context.
9080 9079 */
9081 9080 static int
9082 9081 sata_build_lsense_page_2f(
9083 9082 sata_drive_info_t *sdinfo,
9084 9083 uint8_t *buf,
9085 9084 sata_hba_inst_t *sata_hba_inst)
9086 9085 {
9087 9086 struct log_parameter *lpp = (struct log_parameter *)buf;
9088 9087 int rval;
9089 9088 uint8_t *smart_data;
9090 9089 uint8_t temp;
9091 9090 sata_id_t *sata_id;
9092 9091 #define SMART_NO_TEMP 0xff
9093 9092
9094 9093 lpp->param_code[0] = 0;
9095 9094 lpp->param_code[1] = 0;
9096 9095 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
9097 9096
9098 9097 /* Now get the SMART status w.r.t. threshold exceeded */
9099 9098 rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo);
9100 9099 switch (rval) {
9101 9100 case 1:
9102 9101 lpp->param_values[0] = SCSI_PREDICTED_FAILURE;
9103 9102 lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE;
9104 9103 break;
9105 9104 case 0:
9106 9105 case -1: /* failed to get data */
9107 9106 lpp->param_values[0] = 0; /* No failure predicted */
9108 9107 lpp->param_values[1] = 0;
9109 9108 break;
9110 9109 #if defined(SATA_DEBUG)
9111 9110 default:
9112 9111 cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value");
9113 9112 /* NOTREACHED */
9114 9113 #endif
9115 9114 }
9116 9115
9117 9116 sata_id = &sdinfo->satadrv_id;
9118 9117 if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP))
9119 9118 temp = SMART_NO_TEMP;
9120 9119 else {
9121 9120 /* Now get the temperature */
9122 9121 smart_data = kmem_zalloc(512, KM_SLEEP);
9123 9122 rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data,
9124 9123 SCT_STATUS_LOG_PAGE, 1);
9125 9124 if (rval == -1)
9126 9125 temp = SMART_NO_TEMP;
9127 9126 else {
9128 9127 temp = smart_data[200];
9129 9128 if (temp & 0x80) {
9130 9129 if (temp & 0x7f)
9131 9130 temp = 0;
9132 9131 else
9133 9132 temp = SMART_NO_TEMP;
9134 9133 }
9135 9134 }
9136 9135 kmem_free(smart_data, 512);
9137 9136 }
9138 9137
9139 9138 lpp->param_values[2] = temp; /* most recent temperature */
9140 9139 lpp->param_values[3] = 0; /* required vendor specific byte */
9141 9140
9142 9141 lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN;
9143 9142
9144 9143
9145 9144 return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN);
9146 9145 }
9147 9146
9148 9147 /*
9149 9148 * sata_build_lsense_page_30() is used to create the
9150 9149 * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data).
9151 9150 *
9152 9151 * Takes a sata_drive_info t * and the address of a buffer
9153 9152 * in which to create the page information as well as a sata_hba_inst_t *.
9154 9153 *
9155 9154 * Returns the number of bytes valid in the buffer.
9156 9155 */
9157 9156 static int
9158 9157 sata_build_lsense_page_30(
9159 9158 sata_drive_info_t *sdinfo,
9160 9159 uint8_t *buf,
9161 9160 sata_hba_inst_t *sata_hba_inst)
9162 9161 {
9163 9162 struct smart_data *smart_data = (struct smart_data *)buf;
9164 9163 int rval;
9165 9164
9166 9165 /* Now do the SMART READ DATA */
9167 9166 rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data);
9168 9167 if (rval == -1)
9169 9168 return (0);
9170 9169
9171 9170 return (sizeof (struct smart_data));
9172 9171 }
9173 9172
9174 9173 /*
9175 9174 * sata_build_lsense_page_0e() is used to create the
9176 9175 * SCSI LOG SENSE page 0e (start-stop cycle counter page)
9177 9176 *
9178 9177 * Date of Manufacture (0x0001)
9179 9178 * YEAR = "0000"
9180 9179 * WEEK = "00"
9181 9180 * Accounting Date (0x0002)
9182 9181 * 6 ASCII space character(20h)
9183 9182 * Specified cycle count over device lifetime
9184 9183 * VALUE - THRESH - the delta between max and min;
9185 9184 * Accumulated start-stop cycles
9186 9185 * VALUE - WORST - the accumulated cycles;
9187 9186 *
9188 9187 * ID FLAG THRESH VALUE WORST RAW on start/stop counter attribute
9189 9188 *
9190 9189 * Takes a sata_drive_info t * and the address of a buffer
9191 9190 * in which to create the page information as well as a sata_hba_inst_t *.
9192 9191 *
9193 9192 * Returns the number of bytes valid in the buffer.
9194 9193 */
9195 9194 static int
9196 9195 sata_build_lsense_page_0e(sata_drive_info_t *sdinfo, uint8_t *buf,
9197 9196 sata_pkt_txlate_t *spx)
9198 9197 {
9199 9198 struct start_stop_cycle_counter_log *log_page;
9200 9199 int i, rval, index;
9201 9200 uint8_t smart_data[512], id, value, worst, thresh;
9202 9201 uint32_t max_count, cycles;
9203 9202
9204 9203 /* Now do the SMART READ DATA */
9205 9204 rval = sata_fetch_smart_data(spx->txlt_sata_hba_inst, sdinfo,
9206 9205 (struct smart_data *)smart_data);
9207 9206 if (rval == -1)
9208 9207 return (0);
9209 9208 for (i = 0, id = 0; i < SMART_START_STOP_COUNT_ID * 2; i++) {
9210 9209 index = (i * 12) + 2;
9211 9210 id = smart_data[index];
9212 9211 if (id != SMART_START_STOP_COUNT_ID)
9213 9212 continue;
9214 9213 else {
9215 9214 thresh = smart_data[index + 2];
9216 9215 value = smart_data[index + 3];
9217 9216 worst = smart_data[index + 4];
9218 9217 break;
9219 9218 }
9220 9219 }
9221 9220 if (id != SMART_START_STOP_COUNT_ID)
9222 9221 return (0);
9223 9222 max_count = value - thresh;
9224 9223 cycles = value - worst;
9225 9224
9226 9225 log_page = (struct start_stop_cycle_counter_log *)buf;
9227 9226 bzero(log_page, sizeof (struct start_stop_cycle_counter_log));
9228 9227 log_page->code = 0x0e;
9229 9228 log_page->page_len_low = 0x24;
9230 9229
9231 9230 log_page->manufactor_date_low = 0x1;
9232 9231 log_page->param_1.fmt_link = 0x1; /* 01b */
9233 9232 log_page->param_len_1 = 0x06;
9234 9233 for (i = 0; i < 4; i++) {
9235 9234 log_page->year_manu[i] = 0x30;
9236 9235 if (i < 2)
9237 9236 log_page->week_manu[i] = 0x30;
9238 9237 }
9239 9238
9240 9239 log_page->account_date_low = 0x02;
9241 9240 log_page->param_2.fmt_link = 0x01; /* 01b */
9242 9241 log_page->param_len_2 = 0x06;
9243 9242 for (i = 0; i < 4; i++) {
9244 9243 log_page->year_account[i] = 0x20;
9245 9244 if (i < 2)
9246 9245 log_page->week_account[i] = 0x20;
9247 9246 }
9248 9247
9249 9248 log_page->lifetime_code_low = 0x03;
9250 9249 log_page->param_3.fmt_link = 0x03; /* 11b */
9251 9250 log_page->param_len_3 = 0x04;
9252 9251 /* VALUE - THRESH - the delta between max and min */
9253 9252 log_page->cycle_code_low = 0x04;
9254 9253 log_page->param_4.fmt_link = 0x03; /* 11b */
9255 9254 log_page->param_len_4 = 0x04;
9256 9255 /* WORST - THRESH - the distance from 'now' to min */
9257 9256
9258 9257 for (i = 0; i < 4; i++) {
9259 9258 log_page->cycle_lifetime[i] =
9260 9259 (max_count >> (8 * (3 - i))) & 0xff;
9261 9260 log_page->cycle_accumulated[i] =
9262 9261 (cycles >> (8 * (3 - i))) & 0xff;
9263 9262 }
9264 9263
9265 9264 return (sizeof (struct start_stop_cycle_counter_log));
9266 9265 }
9267 9266
9268 9267 /*
9269 9268 * This function was used for build a ATA read verify sector command
9270 9269 */
9271 9270 static void
9272 9271 sata_build_read_verify_cmd(sata_cmd_t *scmd, uint16_t sec, uint64_t lba)
9273 9272 {
9274 9273 scmd->satacmd_cmd_reg = SATAC_RDVER;
9275 9274 scmd->satacmd_addr_type = ATA_ADDR_LBA28;
9276 9275 scmd->satacmd_flags.sata_special_regs = B_TRUE;
9277 9276
9278 9277 scmd->satacmd_sec_count_lsb = sec & 0xff;
9279 9278 scmd->satacmd_lba_low_lsb = lba & 0xff;
9280 9279 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
9281 9280 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
9282 9281 scmd->satacmd_device_reg = (SATA_ADH_LBA | (lba >> 24) & 0xf);
9283 9282 scmd->satacmd_features_reg = 0;
9284 9283 scmd->satacmd_status_reg = 0;
9285 9284 scmd->satacmd_error_reg = 0;
9286 9285 }
9287 9286
9288 9287 /*
9289 9288 * This function was used for building an ATA
9290 9289 * command, and only command register need to
9291 9290 * be defined, other register will be zero or na.
9292 9291 */
9293 9292 static void
9294 9293 sata_build_generic_cmd(sata_cmd_t *scmd, uint8_t cmd)
9295 9294 {
9296 9295 scmd->satacmd_addr_type = 0;
9297 9296 scmd->satacmd_cmd_reg = cmd;
9298 9297 scmd->satacmd_device_reg = 0;
9299 9298 scmd->satacmd_sec_count_lsb = 0;
9300 9299 scmd->satacmd_lba_low_lsb = 0;
9301 9300 scmd->satacmd_lba_mid_lsb = 0;
9302 9301 scmd->satacmd_lba_high_lsb = 0;
9303 9302 scmd->satacmd_features_reg = 0;
9304 9303 scmd->satacmd_status_reg = 0;
9305 9304 scmd->satacmd_error_reg = 0;
9306 9305 scmd->satacmd_flags.sata_special_regs = B_TRUE;
9307 9306 }
9308 9307
9309 9308 /*
9310 9309 * This function was used for changing the standby
9311 9310 * timer format from SCSI to ATA.
9312 9311 */
9313 9312 static uint8_t
9314 9313 sata_get_standby_timer(uint8_t *timer)
9315 9314 {
9316 9315 uint32_t i = 0, count = 0;
9317 9316 uint8_t ata_count;
9318 9317
9319 9318 for (i = 0; i < 4; i++) {
9320 9319 count = count << 8 | timer[i];
9321 9320 }
9322 9321
9323 9322 if (count == 0)
9324 9323 return (0);
9325 9324
9326 9325 if (count >= 1 && count <= 12000)
9327 9326 ata_count = (count -1) / 50 + 1;
9328 9327 else if (count > 12000 && count <= 12600)
9329 9328 ata_count = 0xfc;
9330 9329 else if (count > 12601 && count <= 12750)
9331 9330 ata_count = 0xff;
9332 9331 else if (count > 12750 && count <= 17999)
9333 9332 ata_count = 0xf1;
9334 9333 else if (count > 18000 && count <= 198000)
9335 9334 ata_count = count / 18000 + 240;
9336 9335 else
9337 9336 ata_count = 0xfd;
9338 9337 return (ata_count);
9339 9338 }
9340 9339
9341 9340 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */
9342 9341
9343 9342 /*
9344 9343 * Start command for ATAPI device.
9345 9344 * This function processes scsi_pkt requests.
9346 9345 * Now CD/DVD, tape and ATAPI disk devices are supported.
9347 9346 * Most commands are packet without any translation into Packet Command.
9348 9347 * Some may be trapped and executed as SATA commands (not clear which one).
9349 9348 *
9350 9349 * Returns TRAN_ACCEPT if command is accepted for execution (or completed
9351 9350 * execution).
9352 9351 * Returns other TRAN_XXXX codes if command is not accepted or completed
9353 9352 * (see return values for sata_hba_start()).
9354 9353 *
9355 9354 * Note:
9356 9355 * Inquiry cdb format differs between transport version 2 and 3.
9357 9356 * However, the transport version 3 devices that were checked did not adhere
9358 9357 * to the specification (ignored MSB of the allocation length). Therefore,
9359 9358 * the transport version is not checked, but Inquiry allocation length is
9360 9359 * truncated to 255 bytes if the original allocation length set-up by the
9361 9360 * target driver is greater than 255 bytes.
9362 9361 */
9363 9362 static int
9364 9363 sata_txlt_atapi(sata_pkt_txlate_t *spx)
9365 9364 {
9366 9365 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
9367 9366 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
9368 9367 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9369 9368 sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx);
9370 9369 sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba,
9371 9370 &spx->txlt_sata_pkt->satapkt_device);
9372 9371 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
9373 9372 int cdblen;
9374 9373 int rval, reason;
9375 9374 int synch;
9376 9375 union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp;
9377 9376
9378 9377 mutex_enter(cport_mutex);
9379 9378
9380 9379 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
9381 9380 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
9382 9381 mutex_exit(cport_mutex);
9383 9382 return (rval);
9384 9383 }
9385 9384
9386 9385 /*
9387 9386 * ATAPI device executes some ATA commands in addition to those
9388 9387 * commands sent via PACKET command. These ATA commands may be
9389 9388 * executed by the regular SATA translation functions. None needs
9390 9389 * to be captured now.
9391 9390 *
9392 9391 * Commands sent via PACKET command include:
9393 9392 * MMC command set for ATAPI CD/DVD device
9394 9393 * SSC command set for ATAPI TAPE device
9395 9394 * SBC command set for ATAPI disk device
9396 9395 *
9397 9396 */
9398 9397
9399 9398 /* Check the size of cdb */
9400 9399
9401 9400 switch (GETGROUP(cdbp)) {
9402 9401 case CDB_GROUPID_3: /* Reserved, per SPC-4 */
9403 9402 /*
9404 9403 * opcodes 0x7e and 0x7f identify variable-length CDBs and
9405 9404 * therefore require special handling. Return failure, for now.
9406 9405 */
9407 9406 mutex_exit(cport_mutex);
9408 9407 return (TRAN_BADPKT);
9409 9408
9410 9409 case CDB_GROUPID_6: /* Vendor-specific, per SPC-4 */
9411 9410 case CDB_GROUPID_7: /* Vendor-specific, per SPC-4 */
9412 9411 /* obtain length from the scsi_pkt */
9413 9412 cdblen = scsipkt->pkt_cdblen;
9414 9413 break;
9415 9414
9416 9415 default:
9417 9416 /* CDB's length is statically known, per SPC-4 */
9418 9417 cdblen = scsi_cdb_size[GETGROUP(cdbp)];
9419 9418 break;
9420 9419 }
9421 9420
9422 9421 if (cdblen <= 0 || cdblen > sdinfo->satadrv_atapi_cdb_len) {
9423 9422 sata_log(NULL, CE_WARN,
9424 9423 "sata: invalid ATAPI cdb length %d",
9425 9424 cdblen);
9426 9425 mutex_exit(cport_mutex);
9427 9426 return (TRAN_BADPKT);
9428 9427 }
9429 9428
9430 9429 SATAATAPITRACE(spx, cdblen);
9431 9430
9432 9431 /*
9433 9432 * For non-read/write commands we need to
9434 9433 * map buffer
9435 9434 */
9436 9435 switch ((uint_t)scsipkt->pkt_cdbp[0]) {
9437 9436 case SCMD_READ:
9438 9437 case SCMD_READ_G1:
9439 9438 case SCMD_READ_G5:
9440 9439 case SCMD_READ_G4:
9441 9440 case SCMD_WRITE:
9442 9441 case SCMD_WRITE_G1:
9443 9442 case SCMD_WRITE_G5:
9444 9443 case SCMD_WRITE_G4:
9445 9444 break;
9446 9445 default:
9447 9446 if (bp != NULL) {
9448 9447 if (bp->b_flags & (B_PHYS | B_PAGEIO))
9449 9448 bp_mapin(bp);
9450 9449 }
9451 9450 break;
9452 9451 }
9453 9452 /*
9454 9453 * scmd->satacmd_flags.sata_data_direction default -
9455 9454 * SATA_DIR_NODATA_XFER - is set by
9456 9455 * sata_txlt_generic_pkt_info().
9457 9456 */
9458 9457 if (scmd->satacmd_bp) {
9459 9458 if (scmd->satacmd_bp->b_flags & B_READ) {
9460 9459 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9461 9460 } else {
9462 9461 scmd->satacmd_flags.sata_data_direction =
9463 9462 SATA_DIR_WRITE;
9464 9463 }
9465 9464 }
9466 9465
9467 9466 /*
9468 9467 * Set up ATAPI packet command.
9469 9468 */
9470 9469
9471 9470 sata_atapi_packet_cmd_setup(scmd, sdinfo);
9472 9471
9473 9472 /* Copy cdb into sata_cmd */
9474 9473 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9475 9474 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
9476 9475 bcopy(cdbp, scmd->satacmd_acdb, cdblen);
9477 9476
9478 9477 /* See note in the command header */
9479 9478 if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) {
9480 9479 if (scmd->satacmd_acdb[3] != 0)
9481 9480 scmd->satacmd_acdb[4] = 255;
9482 9481 }
9483 9482
9484 9483 #ifdef SATA_DEBUG
9485 9484 if (sata_debug_flags & SATA_DBG_ATAPI) {
9486 9485 uint8_t *p = scmd->satacmd_acdb;
9487 9486 char buf[3 * SATA_ATAPI_MAX_CDB_LEN];
9488 9487
9489 9488 (void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN,
9490 9489 "%02x %02x %02x %02x %02x %02x %02x %02x "
9491 9490 "%2x %02x %02x %02x %02x %02x %02x %02x",
9492 9491 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9493 9492 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9494 9493 buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0';
9495 9494 cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf);
9496 9495 }
9497 9496 #endif
9498 9497
9499 9498 /*
9500 9499 * Preset request sense data to NO SENSE.
9501 9500 * If there is no way to get error information via Request Sense,
9502 9501 * the packet request sense data would not have to be modified by HBA,
9503 9502 * but it could be returned as is.
9504 9503 */
9505 9504 bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
9506 9505 sata_fixed_sense_data_preset(
9507 9506 (struct scsi_extended_sense *)scmd->satacmd_rqsense);
9508 9507
9509 9508 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
9510 9509 /* Need callback function */
9511 9510 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion;
9512 9511 synch = FALSE;
9513 9512 } else
9514 9513 synch = TRUE;
9515 9514
9516 9515 /* Transfer command to HBA */
9517 9516 if (sata_hba_start(spx, &rval) != 0) {
9518 9517 /* Pkt not accepted for execution */
9519 9518 mutex_exit(cport_mutex);
9520 9519 return (rval);
9521 9520 }
9522 9521 mutex_exit(cport_mutex);
9523 9522 /*
9524 9523 * If execution is non-synchronous,
9525 9524 * a callback function will handle potential errors, translate
9526 9525 * the response and will do a callback to a target driver.
9527 9526 * If it was synchronous, use the same framework callback to check
9528 9527 * an execution status.
9529 9528 */
9530 9529 if (synch) {
9531 9530 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
9532 9531 "synchronous execution status %x\n",
9533 9532 spx->txlt_sata_pkt->satapkt_reason);
9534 9533 sata_txlt_atapi_completion(spx->txlt_sata_pkt);
9535 9534 }
9536 9535 return (TRAN_ACCEPT);
9537 9536 }
9538 9537
9539 9538
9540 9539 /*
9541 9540 * ATAPI Packet command completion.
9542 9541 *
9543 9542 * Failure of the command passed via Packet command are considered device
9544 9543 * error. SATA HBA driver would have to retrieve error data (via Request
9545 9544 * Sense command delivered via error retrieval sata packet) and copy it
9546 9545 * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data.
9547 9546 */
9548 9547 static void
9549 9548 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt)
9550 9549 {
9551 9550 sata_pkt_txlate_t *spx =
9552 9551 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
9553 9552 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
9554 9553 struct scsi_extended_sense *sense;
9555 9554 struct buf *bp;
9556 9555 int rval;
9557 9556
9558 9557 #ifdef SATA_DEBUG
9559 9558 uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense;
9560 9559 #endif
9561 9560
9562 9561 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
9563 9562 STATE_SENT_CMD | STATE_GOT_STATUS;
9564 9563
9565 9564 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
9566 9565 /* Normal completion */
9567 9566 if (sata_pkt->satapkt_cmd.satacmd_bp != NULL)
9568 9567 scsipkt->pkt_state |= STATE_XFERRED_DATA;
9569 9568 scsipkt->pkt_reason = CMD_CMPLT;
9570 9569 *scsipkt->pkt_scbp = STATUS_GOOD;
9571 9570 if (spx->txlt_tmp_buf != NULL) {
9572 9571 /* Temporary buffer was used */
9573 9572 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9574 9573 if (bp->b_flags & B_READ) {
9575 9574 rval = ddi_dma_sync(
9576 9575 spx->txlt_buf_dma_handle, 0, 0,
9577 9576 DDI_DMA_SYNC_FORCPU);
9578 9577 ASSERT(rval == DDI_SUCCESS);
9579 9578 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
9580 9579 bp->b_bcount);
9581 9580 }
9582 9581 }
9583 9582 } else {
9584 9583 /*
9585 9584 * Something went wrong - analyze return
9586 9585 */
9587 9586 *scsipkt->pkt_scbp = STATUS_CHECK;
9588 9587 sense = sata_arq_sense(spx);
9589 9588
9590 9589 if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
9591 9590 /*
9592 9591 * pkt_reason should be CMD_CMPLT for DEVICE ERROR.
9593 9592 * Under this condition ERR bit is set for ATA command,
9594 9593 * and CHK bit set for ATAPI command.
9595 9594 *
9596 9595 * Please check st_intr & sdintr about how pkt_reason
9597 9596 * is used.
9598 9597 */
9599 9598 scsipkt->pkt_reason = CMD_CMPLT;
9600 9599
9601 9600 /*
9602 9601 * We may not have ARQ data if there was a double
9603 9602 * error. But sense data in sata packet was pre-set
9604 9603 * with NO SENSE so it is valid even if HBA could
9605 9604 * not retrieve a real sense data.
9606 9605 * Just copy this sense data into scsi pkt sense area.
9607 9606 */
9608 9607 bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense,
9609 9608 SATA_ATAPI_MIN_RQSENSE_LEN);
9610 9609 #ifdef SATA_DEBUG
9611 9610 if (sata_debug_flags & SATA_DBG_SCSI_IF) {
9612 9611 sata_log(spx->txlt_sata_hba_inst, CE_WARN,
9613 9612 "sata_txlt_atapi_completion: %02x\n"
9614 9613 "RQSENSE: %02x %02x %02x %02x %02x %02x "
9615 9614 " %02x %02x %02x %02x %02x %02x "
9616 9615 " %02x %02x %02x %02x %02x %02x\n",
9617 9616 scsipkt->pkt_reason,
9618 9617 rqsp[0], rqsp[1], rqsp[2], rqsp[3],
9619 9618 rqsp[4], rqsp[5], rqsp[6], rqsp[7],
9620 9619 rqsp[8], rqsp[9], rqsp[10], rqsp[11],
9621 9620 rqsp[12], rqsp[13], rqsp[14], rqsp[15],
9622 9621 rqsp[16], rqsp[17]);
9623 9622 }
9624 9623 #endif
9625 9624 } else {
9626 9625 switch (sata_pkt->satapkt_reason) {
9627 9626 case SATA_PKT_PORT_ERROR:
9628 9627 /*
9629 9628 * We have no device data.
9630 9629 */
9631 9630 scsipkt->pkt_reason = CMD_INCOMPLETE;
9632 9631 scsipkt->pkt_state &= ~(STATE_GOT_BUS |
9633 9632 STATE_GOT_TARGET | STATE_SENT_CMD |
9634 9633 STATE_GOT_STATUS);
9635 9634 sense->es_key = KEY_HARDWARE_ERROR;
9636 9635 break;
9637 9636
9638 9637 case SATA_PKT_TIMEOUT:
9639 9638 scsipkt->pkt_reason = CMD_TIMEOUT;
9640 9639 scsipkt->pkt_statistics |=
9641 9640 STAT_TIMEOUT | STAT_DEV_RESET;
9642 9641 /*
9643 9642 * Need to check if HARDWARE_ERROR/
9644 9643 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more
9645 9644 * appropriate.
9646 9645 */
9647 9646 break;
9648 9647
9649 9648 case SATA_PKT_ABORTED:
9650 9649 scsipkt->pkt_reason = CMD_ABORTED;
9651 9650 scsipkt->pkt_statistics |= STAT_ABORTED;
9652 9651 /* Should we set key COMMAND_ABPRTED? */
9653 9652 break;
9654 9653
9655 9654 case SATA_PKT_RESET:
9656 9655 scsipkt->pkt_reason = CMD_RESET;
9657 9656 scsipkt->pkt_statistics |= STAT_DEV_RESET;
9658 9657 /*
9659 9658 * May be we should set Unit Attention /
9660 9659 * Reset. Perhaps the same should be
9661 9660 * returned for disks....
9662 9661 */
9663 9662 sense->es_key = KEY_UNIT_ATTENTION;
9664 9663 sense->es_add_code = SD_SCSI_ASC_RESET;
9665 9664 break;
9666 9665
9667 9666 default:
9668 9667 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9669 9668 "sata_txlt_atapi_completion: "
9670 9669 "invalid packet completion reason"));
9671 9670 scsipkt->pkt_reason = CMD_TRAN_ERR;
9672 9671 scsipkt->pkt_state &= ~(STATE_GOT_BUS |
9673 9672 STATE_GOT_TARGET | STATE_SENT_CMD |
9674 9673 STATE_GOT_STATUS);
9675 9674 break;
9676 9675 }
9677 9676 }
9678 9677 }
9679 9678
9680 9679 SATAATAPITRACE(spx, 0);
9681 9680
9682 9681 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
9683 9682 scsipkt->pkt_comp != NULL) {
9684 9683 /* scsi callback required */
9685 9684 (*scsipkt->pkt_comp)(scsipkt);
9686 9685 }
9687 9686 }
9688 9687
9689 9688 /*
9690 9689 * Set up error retrieval sata command for ATAPI Packet Command error data
9691 9690 * recovery.
9692 9691 *
9693 9692 * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
9694 9693 * returns SATA_FAILURE otherwise.
9695 9694 */
9696 9695
9697 9696 static int
9698 9697 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
9699 9698 {
9700 9699 sata_pkt_t *spkt = spx->txlt_sata_pkt;
9701 9700 sata_cmd_t *scmd;
9702 9701 struct buf *bp;
9703 9702
9704 9703 /*
9705 9704 * Allocate dma-able buffer error data.
9706 9705 * Buffer allocation will take care of buffer alignment and other DMA
9707 9706 * attributes.
9708 9707 */
9709 9708 bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN);
9710 9709 if (bp == NULL) {
9711 9710 SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
9712 9711 "sata_get_err_retrieval_pkt: "
9713 9712 "cannot allocate buffer for error data", NULL);
9714 9713 return (SATA_FAILURE);
9715 9714 }
9716 9715 bp_mapin(bp); /* make data buffer accessible */
9717 9716
9718 9717 /* Operation modes are up to the caller */
9719 9718 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
9720 9719
9721 9720 /* Synchronous mode, no callback - may be changed by the caller */
9722 9721 spkt->satapkt_comp = NULL;
9723 9722 spkt->satapkt_time = sata_default_pkt_time;
9724 9723
9725 9724 scmd = &spkt->satapkt_cmd;
9726 9725 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9727 9726 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
9728 9727
9729 9728 sata_atapi_packet_cmd_setup(scmd, sdinfo);
9730 9729
9731 9730 /*
9732 9731 * Set-up acdb. Request Sense CDB (packet command content) is
9733 9732 * not in DMA-able buffer. Its handling is HBA-specific (how
9734 9733 * it is transfered into packet FIS).
9735 9734 */
9736 9735 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9737 9736 bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN);
9738 9737 /* Following zeroing of pad bytes may not be necessary */
9739 9738 bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN],
9740 9739 sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN);
9741 9740
9742 9741 /*
9743 9742 * Set-up pointer to the buffer handle, so HBA can sync buffer
9744 9743 * before accessing it. Handle is in usual place in translate struct.
9745 9744 */
9746 9745 scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
9747 9746
9748 9747 /*
9749 9748 * Preset request sense data to NO SENSE.
9750 9749 * Here it is redundant, only for a symetry with scsi-originated
9751 9750 * packets. It should not be used for anything but debugging.
9752 9751 */
9753 9752 bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
9754 9753 sata_fixed_sense_data_preset(
9755 9754 (struct scsi_extended_sense *)scmd->satacmd_rqsense);
9756 9755
9757 9756 ASSERT(scmd->satacmd_num_dma_cookies != 0);
9758 9757 ASSERT(scmd->satacmd_dma_cookie_list != NULL);
9759 9758
9760 9759 return (SATA_SUCCESS);
9761 9760 }
9762 9761
9763 9762 /*
9764 9763 * Set-up ATAPI packet command.
9765 9764 * Data transfer direction has to be set-up in sata_cmd structure prior to
9766 9765 * calling this function.
9767 9766 *
9768 9767 * Returns void
9769 9768 */
9770 9769
9771 9770 static void
9772 9771 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo)
9773 9772 {
9774 9773 scmd->satacmd_addr_type = 0; /* N/A */
9775 9774 scmd->satacmd_sec_count_lsb = 0; /* no tag */
9776 9775 scmd->satacmd_lba_low_lsb = 0; /* N/A */
9777 9776 scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ;
9778 9777 scmd->satacmd_lba_high_lsb =
9779 9778 (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8);
9780 9779 scmd->satacmd_cmd_reg = SATAC_PACKET; /* Command */
9781 9780
9782 9781 /*
9783 9782 * We want all data to be transfered via DMA.
9784 9783 * But specify it only if drive supports DMA and DMA mode is
9785 9784 * selected - some drives are sensitive about it.
9786 9785 * Hopefully it wil work for all drives....
9787 9786 */
9788 9787 if (sdinfo->satadrv_settings & SATA_DEV_DMA)
9789 9788 scmd->satacmd_features_reg = SATA_ATAPI_F_DMA;
9790 9789
9791 9790 /*
9792 9791 * Features register requires special care for devices that use
9793 9792 * Serial ATA bridge - they need an explicit specification of
9794 9793 * the data transfer direction for Packet DMA commands.
9795 9794 * Setting this bit is harmless if DMA is not used.
9796 9795 *
9797 9796 * Many drives do not implement word 80, specifying what ATA/ATAPI
9798 9797 * spec they follow.
9799 9798 * We are arbitrarily following the latest SerialATA 2.6 spec,
9800 9799 * which uses ATA/ATAPI 6 specification for Identify Data, unless
9801 9800 * ATA/ATAPI-7 support is explicitly indicated.
9802 9801 */
9803 9802 if (sdinfo->satadrv_id.ai_majorversion != 0 &&
9804 9803 sdinfo->satadrv_id.ai_majorversion != 0xffff &&
9805 9804 (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) {
9806 9805 /*
9807 9806 * Specification of major version is valid and version 7
9808 9807 * is supported. It does automatically imply that all
9809 9808 * spec features are supported. For now, we assume that
9810 9809 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete.
9811 9810 */
9812 9811 if ((sdinfo->satadrv_id.ai_dirdma &
9813 9812 SATA_ATAPI_ID_DMADIR_REQ) != 0) {
9814 9813 if (scmd->satacmd_flags.sata_data_direction ==
9815 9814 SATA_DIR_READ)
9816 9815 scmd->satacmd_features_reg |=
9817 9816 SATA_ATAPI_F_DATA_DIR_READ;
9818 9817 }
9819 9818 }
9820 9819 }
9821 9820
9822 9821
9823 9822 #ifdef SATA_DEBUG
9824 9823
9825 9824 /* Display 18 bytes of Inquiry data */
9826 9825 static void
9827 9826 sata_show_inqry_data(uint8_t *buf)
9828 9827 {
9829 9828 struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
9830 9829 uint8_t *p;
9831 9830
9832 9831 cmn_err(CE_NOTE, "Inquiry data:");
9833 9832 cmn_err(CE_NOTE, "device type %x", inq->inq_dtype);
9834 9833 cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb);
9835 9834 cmn_err(CE_NOTE, "version %x", inq->inq_ansi);
9836 9835 cmn_err(CE_NOTE, "ATAPI transport version %d",
9837 9836 SATA_ATAPI_TRANS_VERSION(inq));
9838 9837 cmn_err(CE_NOTE, "response data format %d, aenc %d",
9839 9838 inq->inq_rdf, inq->inq_aenc);
9840 9839 cmn_err(CE_NOTE, " additional length %d", inq->inq_len);
9841 9840 cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs);
9842 9841 p = (uint8_t *)inq->inq_vid;
9843 9842 cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x "
9844 9843 "%02x %02x %02x %02x",
9845 9844 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
9846 9845 p = (uint8_t *)inq->inq_vid;
9847 9846 cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c",
9848 9847 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
9849 9848
9850 9849 p = (uint8_t *)inq->inq_pid;
9851 9850 cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x "
9852 9851 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
9853 9852 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9854 9853 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9855 9854 p = (uint8_t *)inq->inq_pid;
9856 9855 cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c "
9857 9856 "%c %c %c %c %c %c %c %c",
9858 9857 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9859 9858 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9860 9859
9861 9860 p = (uint8_t *)inq->inq_revision;
9862 9861 cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x",
9863 9862 p[0], p[1], p[2], p[3]);
9864 9863 p = (uint8_t *)inq->inq_revision;
9865 9864 cmn_err(CE_NOTE, "revision: %c %c %c %c",
9866 9865 p[0], p[1], p[2], p[3]);
9867 9866
9868 9867 }
9869 9868
9870 9869
9871 9870 static void
9872 9871 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count)
9873 9872 {
9874 9873 struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt;
9875 9874
9876 9875 if (scsi_pkt == NULL)
9877 9876 return;
9878 9877 if (count != 0) {
9879 9878 /* saving cdb */
9880 9879 bzero(sata_atapi_trace[sata_atapi_trace_index].acdb,
9881 9880 SATA_ATAPI_MAX_CDB_LEN);
9882 9881 bcopy(scsi_pkt->pkt_cdbp,
9883 9882 sata_atapi_trace[sata_atapi_trace_index].acdb, count);
9884 9883 } else {
9885 9884 bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)->
9886 9885 sts_sensedata,
9887 9886 sata_atapi_trace[sata_atapi_trace_index].arqs,
9888 9887 SATA_ATAPI_MIN_RQSENSE_LEN);
9889 9888 sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason =
9890 9889 scsi_pkt->pkt_reason;
9891 9890 sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason =
9892 9891 spx->txlt_sata_pkt->satapkt_reason;
9893 9892
9894 9893 if (++sata_atapi_trace_index >= 64)
9895 9894 sata_atapi_trace_index = 0;
9896 9895 }
9897 9896 }
9898 9897
9899 9898 #endif
9900 9899
9901 9900 /*
9902 9901 * Fetch inquiry data from ATAPI device
9903 9902 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
9904 9903 *
9905 9904 * Note:
9906 9905 * inqb pointer does not point to a DMA-able buffer. It is a local buffer
9907 9906 * where the caller expects to see the inquiry data.
9908 9907 *
9909 9908 */
9910 9909
9911 9910 static int
9912 9911 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba,
9913 9912 sata_address_t *saddr, struct scsi_inquiry *inq)
9914 9913 {
9915 9914 sata_pkt_txlate_t *spx;
9916 9915 sata_pkt_t *spkt;
9917 9916 struct buf *bp;
9918 9917 sata_drive_info_t *sdinfo;
9919 9918 sata_cmd_t *scmd;
9920 9919 int rval;
9921 9920 uint8_t *rqsp;
9922 9921 dev_info_t *dip = SATA_DIP(sata_hba);
9923 9922 #ifdef SATA_DEBUG
9924 9923 char msg_buf[MAXPATHLEN];
9925 9924 #endif
9926 9925 kmutex_t *cport_mutex;
9927 9926
9928 9927 ASSERT(sata_hba != NULL);
9929 9928
9930 9929 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
9931 9930 spx->txlt_sata_hba_inst = sata_hba;
9932 9931 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */
9933 9932 spkt = sata_pkt_alloc(spx, NULL);
9934 9933 if (spkt == NULL) {
9935 9934 kmem_free(spx, sizeof (sata_pkt_txlate_t));
9936 9935 return (SATA_FAILURE);
9937 9936 }
9938 9937 /* address is needed now */
9939 9938 spkt->satapkt_device.satadev_addr = *saddr;
9940 9939
9941 9940 /* scsi_inquiry size buffer */
9942 9941 bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry));
9943 9942 if (bp == NULL) {
9944 9943 sata_pkt_free(spx);
9945 9944 kmem_free(spx, sizeof (sata_pkt_txlate_t));
9946 9945 SATA_LOG_D((sata_hba, CE_WARN,
9947 9946 "sata_get_atapi_inquiry_data: "
9948 9947 "cannot allocate data buffer"));
9949 9948 return (SATA_FAILURE);
9950 9949 }
9951 9950 bp_mapin(bp); /* make data buffer accessible */
9952 9951
9953 9952 scmd = &spkt->satapkt_cmd;
9954 9953 ASSERT(scmd->satacmd_num_dma_cookies != 0);
9955 9954 ASSERT(scmd->satacmd_dma_cookie_list != NULL);
9956 9955
9957 9956 /* Use synchronous mode */
9958 9957 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
9959 9958 spkt->satapkt_comp = NULL;
9960 9959 spkt->satapkt_time = sata_default_pkt_time;
9961 9960
9962 9961 /* Issue inquiry command - 6 bytes cdb, data transfer, read */
9963 9962
9964 9963 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9965 9964 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
9966 9965
9967 9966 cport_mutex = &(SATA_CPORT_MUTEX(sata_hba, saddr->cport));
9968 9967 mutex_enter(cport_mutex);
9969 9968 sdinfo = sata_get_device_info(sata_hba,
9970 9969 &spx->txlt_sata_pkt->satapkt_device);
9971 9970 if (sdinfo == NULL) {
9972 9971 /* we have to be carefull about the disapearing device */
9973 9972 mutex_exit(cport_mutex);
9974 9973 rval = SATA_FAILURE;
9975 9974 goto cleanup;
9976 9975 }
9977 9976 sata_atapi_packet_cmd_setup(scmd, sdinfo);
9978 9977
9979 9978 /*
9980 9979 * Set-up acdb. This works for atapi transport version 2 and later.
9981 9980 */
9982 9981 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9983 9982 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
9984 9983 scmd->satacmd_acdb[0] = 0x12; /* Inquiry */
9985 9984 scmd->satacmd_acdb[1] = 0x00;
9986 9985 scmd->satacmd_acdb[2] = 0x00;
9987 9986 scmd->satacmd_acdb[3] = 0x00;
9988 9987 scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
9989 9988 scmd->satacmd_acdb[5] = 0x00;
9990 9989
9991 9990 sata_fixed_sense_data_preset(
9992 9991 (struct scsi_extended_sense *)scmd->satacmd_rqsense);
9993 9992
9994 9993 /* Transfer command to HBA */
9995 9994 if (sata_hba_start(spx, &rval) != 0) {
9996 9995 /* Pkt not accepted for execution */
9997 9996 SATADBG1(SATA_DBG_ATAPI, sata_hba,
9998 9997 "sata_get_atapi_inquiry_data: "
9999 9998 "Packet not accepted for execution - ret: %02x", rval);
10000 9999 mutex_exit(cport_mutex);
10001 10000 rval = SATA_FAILURE;
10002 10001 goto cleanup;
10003 10002 }
10004 10003 mutex_exit(cport_mutex);
10005 10004
10006 10005 if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10007 10006 SATADBG1(SATA_DBG_ATAPI, sata_hba,
10008 10007 "sata_get_atapi_inquiry_data: "
10009 10008 "Packet completed successfully - ret: %02x", rval);
10010 10009 if (spx->txlt_buf_dma_handle != NULL) {
10011 10010 /*
10012 10011 * Sync buffer. Handle is in usual place in translate
10013 10012 * struct.
10014 10013 */
10015 10014 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10016 10015 DDI_DMA_SYNC_FORCPU);
10017 10016 ASSERT(rval == DDI_SUCCESS);
10018 10017 }
10019 10018
10020 10019 if (sata_check_for_dma_error(dip, spx)) {
10021 10020 ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED);
10022 10021 rval = SATA_FAILURE;
10023 10022 } else {
10024 10023 /*
10025 10024 * Normal completion - copy data into caller's buffer
10026 10025 */
10027 10026 bcopy(bp->b_un.b_addr, (uint8_t *)inq,
10028 10027 sizeof (struct scsi_inquiry));
10029 10028 #ifdef SATA_DEBUG
10030 10029 if (sata_debug_flags & SATA_DBG_ATAPI) {
10031 10030 sata_show_inqry_data((uint8_t *)inq);
10032 10031 }
10033 10032 #endif
10034 10033 rval = SATA_SUCCESS;
10035 10034 }
10036 10035 } else {
10037 10036 /*
10038 10037 * Something went wrong - analyze return - check rqsense data
10039 10038 */
10040 10039 rval = SATA_FAILURE;
10041 10040 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
10042 10041 /*
10043 10042 * ARQ data hopefull show something other than NO SENSE
10044 10043 */
10045 10044 rqsp = scmd->satacmd_rqsense;
10046 10045 #ifdef SATA_DEBUG
10047 10046 if (sata_debug_flags & SATA_DBG_ATAPI) {
10048 10047 msg_buf[0] = '\0';
10049 10048 (void) snprintf(msg_buf, MAXPATHLEN,
10050 10049 "ATAPI packet completion reason: %02x\n"
10051 10050 "RQSENSE: %02x %02x %02x %02x %02x %02x\n"
10052 10051 " %02x %02x %02x %02x %02x %02x\n"
10053 10052 " %02x %02x %02x %02x %02x %02x",
10054 10053 spkt->satapkt_reason,
10055 10054 rqsp[0], rqsp[1], rqsp[2], rqsp[3],
10056 10055 rqsp[4], rqsp[5], rqsp[6], rqsp[7],
10057 10056 rqsp[8], rqsp[9], rqsp[10], rqsp[11],
10058 10057 rqsp[12], rqsp[13], rqsp[14], rqsp[15],
10059 10058 rqsp[16], rqsp[17]);
10060 10059 sata_log(spx->txlt_sata_hba_inst, CE_WARN,
10061 10060 "%s", msg_buf);
10062 10061 }
10063 10062 #endif
10064 10063 } else {
10065 10064 switch (spkt->satapkt_reason) {
10066 10065 case SATA_PKT_PORT_ERROR:
10067 10066 SATADBG1(SATA_DBG_ATAPI, sata_hba,
10068 10067 "sata_get_atapi_inquiry_data: "
10069 10068 "packet reason: port error", NULL);
10070 10069 break;
10071 10070
10072 10071 case SATA_PKT_TIMEOUT:
10073 10072 SATADBG1(SATA_DBG_ATAPI, sata_hba,
10074 10073 "sata_get_atapi_inquiry_data: "
10075 10074 "packet reason: timeout", NULL);
10076 10075 break;
10077 10076
10078 10077 case SATA_PKT_ABORTED:
10079 10078 SATADBG1(SATA_DBG_ATAPI, sata_hba,
10080 10079 "sata_get_atapi_inquiry_data: "
10081 10080 "packet reason: aborted", NULL);
10082 10081 break;
10083 10082
10084 10083 case SATA_PKT_RESET:
10085 10084 SATADBG1(SATA_DBG_ATAPI, sata_hba,
10086 10085 "sata_get_atapi_inquiry_data: "
10087 10086 "packet reason: reset\n", NULL);
10088 10087 break;
10089 10088 default:
10090 10089 SATADBG1(SATA_DBG_ATAPI, sata_hba,
10091 10090 "sata_get_atapi_inquiry_data: "
10092 10091 "invalid packet reason: %02x\n",
10093 10092 spkt->satapkt_reason);
10094 10093 break;
10095 10094 }
10096 10095 }
10097 10096 }
10098 10097 cleanup:
10099 10098 sata_free_local_buffer(spx);
10100 10099 sata_pkt_free(spx);
10101 10100 kmem_free(spx, sizeof (sata_pkt_txlate_t));
10102 10101 return (rval);
10103 10102 }
10104 10103
10105 10104
10106 10105
10107 10106
10108 10107
10109 10108 #if 0
10110 10109 #ifdef SATA_DEBUG
10111 10110
10112 10111 /*
10113 10112 * Test ATAPI packet command.
10114 10113 * Single threaded test: send packet command in synch mode, process completion
10115 10114 *
10116 10115 */
10117 10116 static void
10118 10117 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport)
10119 10118 {
10120 10119 sata_pkt_txlate_t *spx;
10121 10120 sata_pkt_t *spkt;
10122 10121 struct buf *bp;
10123 10122 sata_device_t sata_device;
10124 10123 sata_drive_info_t *sdinfo;
10125 10124 sata_cmd_t *scmd;
10126 10125 int rval;
10127 10126 uint8_t *rqsp;
10128 10127
10129 10128 ASSERT(sata_hba_inst != NULL);
10130 10129 sata_device.satadev_addr.cport = cport;
10131 10130 sata_device.satadev_addr.pmport = 0;
10132 10131 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
10133 10132 sata_device.satadev_rev = SATA_DEVICE_REV;
10134 10133 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10135 10134 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
10136 10135 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10137 10136 if (sdinfo == NULL) {
10138 10137 sata_log(sata_hba_inst, CE_WARN,
10139 10138 "sata_test_atapi_packet_command: "
10140 10139 "no device info for cport %d",
10141 10140 sata_device.satadev_addr.cport);
10142 10141 return;
10143 10142 }
10144 10143
10145 10144 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10146 10145 spx->txlt_sata_hba_inst = sata_hba_inst;
10147 10146 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */
10148 10147 spkt = sata_pkt_alloc(spx, NULL);
10149 10148 if (spkt == NULL) {
10150 10149 kmem_free(spx, sizeof (sata_pkt_txlate_t));
10151 10150 return;
10152 10151 }
10153 10152 /* address is needed now */
10154 10153 spkt->satapkt_device.satadev_addr = sata_device.satadev_addr;
10155 10154
10156 10155 /* 1024k buffer */
10157 10156 bp = sata_alloc_local_buffer(spx, 1024);
10158 10157 if (bp == NULL) {
10159 10158 sata_pkt_free(spx);
10160 10159 kmem_free(spx, sizeof (sata_pkt_txlate_t));
10161 10160 sata_log(sata_hba_inst, CE_WARN,
10162 10161 "sata_test_atapi_packet_command: "
10163 10162 "cannot allocate data buffer");
10164 10163 return;
10165 10164 }
10166 10165 bp_mapin(bp); /* make data buffer accessible */
10167 10166
10168 10167 scmd = &spkt->satapkt_cmd;
10169 10168 ASSERT(scmd->satacmd_num_dma_cookies != 0);
10170 10169 ASSERT(scmd->satacmd_dma_cookie_list != NULL);
10171 10170
10172 10171 /* Use synchronous mode */
10173 10172 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10174 10173
10175 10174 /* Synchronous mode, no callback - may be changed by the caller */
10176 10175 spkt->satapkt_comp = NULL;
10177 10176 spkt->satapkt_time = sata_default_pkt_time;
10178 10177
10179 10178 /* Issue inquiry command - 6 bytes cdb, data transfer, read */
10180 10179
10181 10180 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
10182 10181 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10183 10182
10184 10183 sata_atapi_packet_cmd_setup(scmd, sdinfo);
10185 10184
10186 10185 /* Set-up acdb. */
10187 10186 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
10188 10187 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
10189 10188 scmd->satacmd_acdb[0] = 0x12; /* Inquiry */
10190 10189 scmd->satacmd_acdb[1] = 0x00;
10191 10190 scmd->satacmd_acdb[2] = 0x00;
10192 10191 scmd->satacmd_acdb[3] = 0x00;
10193 10192 scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
10194 10193 scmd->satacmd_acdb[5] = 0x00;
10195 10194
10196 10195 sata_fixed_sense_data_preset(
10197 10196 (struct scsi_extended_sense *)scmd->satacmd_rqsense);
10198 10197
10199 10198 /* Transfer command to HBA */
10200 10199 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10201 10200 if (sata_hba_start(spx, &rval) != 0) {
10202 10201 /* Pkt not accepted for execution */
10203 10202 sata_log(sata_hba_inst, CE_WARN,
10204 10203 "sata_test_atapi_packet_command: "
10205 10204 "Packet not accepted for execution - ret: %02x", rval);
10206 10205 mutex_exit(
10207 10206 &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10208 10207 goto cleanup;
10209 10208 }
10210 10209 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10211 10210
10212 10211 if (spx->txlt_buf_dma_handle != NULL) {
10213 10212 /*
10214 10213 * Sync buffer. Handle is in usual place in translate struct.
10215 10214 */
10216 10215 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10217 10216 DDI_DMA_SYNC_FORCPU);
10218 10217 ASSERT(rval == DDI_SUCCESS);
10219 10218 }
10220 10219 if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10221 10220 sata_log(sata_hba_inst, CE_WARN,
10222 10221 "sata_test_atapi_packet_command: "
10223 10222 "Packet completed successfully");
10224 10223 /*
10225 10224 * Normal completion - show inquiry data
10226 10225 */
10227 10226 sata_show_inqry_data((uint8_t *)bp->b_un.b_addr);
10228 10227 } else {
10229 10228 /*
10230 10229 * Something went wrong - analyze return - check rqsense data
10231 10230 */
10232 10231 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
10233 10232 /*
10234 10233 * ARQ data hopefull show something other than NO SENSE
10235 10234 */
10236 10235 rqsp = scmd->satacmd_rqsense;
10237 10236 sata_log(spx->txlt_sata_hba_inst, CE_WARN,
10238 10237 "ATAPI packet completion reason: %02x\n"
10239 10238 "RQSENSE: %02x %02x %02x %02x %02x %02x "
10240 10239 " %02x %02x %02x %02x %02x %02x "
10241 10240 " %02x %02x %02x %02x %02x %02x\n",
10242 10241 spkt->satapkt_reason,
10243 10242 rqsp[0], rqsp[1], rqsp[2], rqsp[3],
10244 10243 rqsp[4], rqsp[5], rqsp[6], rqsp[7],
10245 10244 rqsp[8], rqsp[9], rqsp[10], rqsp[11],
10246 10245 rqsp[12], rqsp[13], rqsp[14], rqsp[15],
10247 10246 rqsp[16], rqsp[17]);
10248 10247 } else {
10249 10248 switch (spkt->satapkt_reason) {
10250 10249 case SATA_PKT_PORT_ERROR:
10251 10250 sata_log(sata_hba_inst, CE_WARN,
10252 10251 "sata_test_atapi_packet_command: "
10253 10252 "packet reason: port error\n");
10254 10253 break;
10255 10254
10256 10255 case SATA_PKT_TIMEOUT:
10257 10256 sata_log(sata_hba_inst, CE_WARN,
10258 10257 "sata_test_atapi_packet_command: "
10259 10258 "packet reason: timeout\n");
10260 10259 break;
10261 10260
10262 10261 case SATA_PKT_ABORTED:
10263 10262 sata_log(sata_hba_inst, CE_WARN,
10264 10263 "sata_test_atapi_packet_command: "
10265 10264 "packet reason: aborted\n");
10266 10265 break;
10267 10266
10268 10267 case SATA_PKT_RESET:
10269 10268 sata_log(sata_hba_inst, CE_WARN,
10270 10269 "sata_test_atapi_packet_command: "
10271 10270 "packet reason: reset\n");
10272 10271 break;
10273 10272 default:
10274 10273 sata_log(sata_hba_inst, CE_WARN,
10275 10274 "sata_test_atapi_packet_command: "
10276 10275 "invalid packet reason: %02x\n",
10277 10276 spkt->satapkt_reason);
10278 10277 break;
10279 10278 }
10280 10279 }
10281 10280 }
10282 10281 cleanup:
10283 10282 sata_free_local_buffer(spx);
10284 10283 sata_pkt_free(spx);
10285 10284 kmem_free(spx, sizeof (sata_pkt_txlate_t));
10286 10285 }
10287 10286
10288 10287 #endif /* SATA_DEBUG */
10289 10288 #endif /* 1 */
10290 10289
10291 10290
10292 10291 /* ************************** LOCAL HELPER FUNCTIONS *********************** */
10293 10292
10294 10293 /*
10295 10294 * Validate sata_tran info
10296 10295 * SATA_FAILURE returns if structure is inconsistent or structure revision
10297 10296 * does not match one used by the framework.
10298 10297 *
10299 10298 * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains
10300 10299 * required function pointers.
10301 10300 * Returns SATA_FAILURE otherwise.
10302 10301 */
10303 10302 static int
10304 10303 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran)
10305 10304 {
10306 10305 /*
10307 10306 * SATA_TRAN_HBA_REV is the current (highest) revision number
10308 10307 * of the SATA interface.
10309 10308 */
10310 10309 if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) {
10311 10310 sata_log(NULL, CE_WARN,
10312 10311 "sata: invalid sata_hba_tran version %d for driver %s",
10313 10312 sata_tran->sata_tran_hba_rev, ddi_driver_name(dip));
10314 10313 return (SATA_FAILURE);
10315 10314 }
10316 10315
10317 10316 if (dip != sata_tran->sata_tran_hba_dip) {
10318 10317 SATA_LOG_D((NULL, CE_WARN,
10319 10318 "sata: inconsistent sata_tran_hba_dip "
10320 10319 "%p / %p", sata_tran->sata_tran_hba_dip, dip));
10321 10320 return (SATA_FAILURE);
10322 10321 }
10323 10322
10324 10323 if (sata_tran->sata_tran_probe_port == NULL ||
10325 10324 sata_tran->sata_tran_start == NULL ||
10326 10325 sata_tran->sata_tran_abort == NULL ||
10327 10326 sata_tran->sata_tran_reset_dport == NULL ||
10328 10327 sata_tran->sata_tran_hotplug_ops == NULL ||
10329 10328 sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL ||
10330 10329 sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate ==
10331 10330 NULL) {
10332 10331 SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing "
10333 10332 "required functions"));
10334 10333 }
10335 10334 return (SATA_SUCCESS);
10336 10335 }
10337 10336
10338 10337 /*
10339 10338 * Remove HBA instance from sata_hba_list.
10340 10339 */
10341 10340 static void
10342 10341 sata_remove_hba_instance(dev_info_t *dip)
10343 10342 {
10344 10343 sata_hba_inst_t *sata_hba_inst;
10345 10344
10346 10345 mutex_enter(&sata_mutex);
10347 10346 for (sata_hba_inst = sata_hba_list;
10348 10347 sata_hba_inst != (struct sata_hba_inst *)NULL;
10349 10348 sata_hba_inst = sata_hba_inst->satahba_next) {
10350 10349 if (sata_hba_inst->satahba_dip == dip)
10351 10350 break;
10352 10351 }
10353 10352
10354 10353 if (sata_hba_inst == (struct sata_hba_inst *)NULL) {
10355 10354 #ifdef SATA_DEBUG
10356 10355 cmn_err(CE_WARN, "sata_remove_hba_instance: "
10357 10356 "unknown HBA instance\n");
10358 10357 #endif
10359 10358 ASSERT(FALSE);
10360 10359 }
10361 10360 if (sata_hba_inst == sata_hba_list) {
10362 10361 sata_hba_list = sata_hba_inst->satahba_next;
10363 10362 if (sata_hba_list) {
10364 10363 sata_hba_list->satahba_prev =
10365 10364 (struct sata_hba_inst *)NULL;
10366 10365 }
10367 10366 if (sata_hba_inst == sata_hba_list_tail) {
10368 10367 sata_hba_list_tail = NULL;
10369 10368 }
10370 10369 } else if (sata_hba_inst == sata_hba_list_tail) {
10371 10370 sata_hba_list_tail = sata_hba_inst->satahba_prev;
10372 10371 if (sata_hba_list_tail) {
10373 10372 sata_hba_list_tail->satahba_next =
10374 10373 (struct sata_hba_inst *)NULL;
10375 10374 }
10376 10375 } else {
10377 10376 sata_hba_inst->satahba_prev->satahba_next =
10378 10377 sata_hba_inst->satahba_next;
10379 10378 sata_hba_inst->satahba_next->satahba_prev =
10380 10379 sata_hba_inst->satahba_prev;
10381 10380 }
10382 10381 mutex_exit(&sata_mutex);
10383 10382 }
10384 10383
10385 10384 /*
10386 10385 * Probe all SATA ports of the specified HBA instance.
10387 10386 * The assumption is that there are no target and attachment point minor nodes
10388 10387 * created by the boot subsystems, so we do not need to prune device tree.
10389 10388 *
10390 10389 * This function is called only from sata_hba_attach(). It does not have to
10391 10390 * be protected by controller mutex, because the hba_attached flag is not set
10392 10391 * yet and no one would be touching this HBA instance other than this thread.
10393 10392 * Determines if port is active and what type of the device is attached
10394 10393 * (if any). Allocates necessary structures for each port.
10395 10394 *
10396 10395 * An AP (Attachement Point) node is created for each SATA device port even
10397 10396 * when there is no device attached.
10398 10397 */
10399 10398
10400 10399 static void
10401 10400 sata_probe_ports(sata_hba_inst_t *sata_hba_inst)
10402 10401 {
10403 10402 dev_info_t *dip = SATA_DIP(sata_hba_inst);
10404 10403 int ncport;
10405 10404 sata_cport_info_t *cportinfo;
10406 10405 sata_drive_info_t *drive;
10407 10406 sata_device_t sata_device;
10408 10407 int rval;
10409 10408 dev_t minor_number;
10410 10409 char name[16];
10411 10410 clock_t start_time, cur_time;
10412 10411
10413 10412 /*
10414 10413 * Probe controller ports first, to find port status and
10415 10414 * any port multiplier attached.
10416 10415 */
10417 10416 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
10418 10417 /* allocate cport structure */
10419 10418 cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP);
10420 10419 ASSERT(cportinfo != NULL);
10421 10420 mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL);
10422 10421
10423 10422 mutex_enter(&cportinfo->cport_mutex);
10424 10423
10425 10424 cportinfo->cport_addr.cport = ncport;
10426 10425 cportinfo->cport_addr.pmport = 0;
10427 10426 cportinfo->cport_addr.qual = SATA_ADDR_CPORT;
10428 10427 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
10429 10428 cportinfo->cport_state |= SATA_STATE_PROBING;
10430 10429 SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo;
10431 10430
10432 10431 /*
10433 10432 * Regardless if a port is usable or not, create
10434 10433 * an attachment point
10435 10434 */
10436 10435 mutex_exit(&cportinfo->cport_mutex);
10437 10436 minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip),
10438 10437 ncport, 0, SATA_ADDR_CPORT);
10439 10438 (void) sprintf(name, "%d", ncport);
10440 10439 if (ddi_create_minor_node(dip, name, S_IFCHR,
10441 10440 minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) !=
10442 10441 DDI_SUCCESS) {
10443 10442 sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
10444 10443 "cannot create SATA attachment point for port %d",
10445 10444 ncport);
10446 10445 }
10447 10446
10448 10447 /* Probe port */
10449 10448 start_time = ddi_get_lbolt();
10450 10449 reprobe_cport:
10451 10450 sata_device.satadev_addr.cport = ncport;
10452 10451 sata_device.satadev_addr.pmport = 0;
10453 10452 sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
10454 10453 sata_device.satadev_rev = SATA_DEVICE_REV;
10455 10454
10456 10455 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10457 10456 (dip, &sata_device);
10458 10457
10459 10458 mutex_enter(&cportinfo->cport_mutex);
10460 10459 cportinfo->cport_scr = sata_device.satadev_scr;
10461 10460 if (rval != SATA_SUCCESS) {
10462 10461 /* Something went wrong? Fail the port */
10463 10462 cportinfo->cport_state = SATA_PSTATE_FAILED;
10464 10463 mutex_exit(&cportinfo->cport_mutex);
10465 10464 continue;
10466 10465 }
10467 10466 cportinfo->cport_state &= ~SATA_STATE_PROBING;
10468 10467 cportinfo->cport_state |= SATA_STATE_PROBED;
10469 10468 cportinfo->cport_dev_type = sata_device.satadev_type;
10470 10469
10471 10470 cportinfo->cport_state |= SATA_STATE_READY;
10472 10471 if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) {
10473 10472 mutex_exit(&cportinfo->cport_mutex);
10474 10473 continue;
10475 10474 }
10476 10475 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
10477 10476 /*
10478 10477 * There is some device attached.
10479 10478 * Allocate device info structure
10480 10479 */
10481 10480 if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) {
10482 10481 mutex_exit(&cportinfo->cport_mutex);
10483 10482 SATA_CPORTINFO_DRV_INFO(cportinfo) =
10484 10483 kmem_zalloc(sizeof (sata_drive_info_t),
10485 10484 KM_SLEEP);
10486 10485 mutex_enter(&cportinfo->cport_mutex);
10487 10486 }
10488 10487 drive = SATA_CPORTINFO_DRV_INFO(cportinfo);
10489 10488 drive->satadrv_addr = cportinfo->cport_addr;
10490 10489 drive->satadrv_addr.qual = SATA_ADDR_DCPORT;
10491 10490 drive->satadrv_type = cportinfo->cport_dev_type;
10492 10491 drive->satadrv_state = SATA_STATE_UNKNOWN;
10493 10492
10494 10493 mutex_exit(&cportinfo->cport_mutex);
10495 10494 if (sata_add_device(dip, sata_hba_inst, &sata_device) !=
10496 10495 SATA_SUCCESS) {
10497 10496 /*
10498 10497 * Plugged device was not correctly identified.
10499 10498 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT
10500 10499 */
10501 10500 cur_time = ddi_get_lbolt();
10502 10501 if ((cur_time - start_time) <
10503 10502 drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
10504 10503 /* sleep for a while */
10505 10504 delay(drv_usectohz(
10506 10505 SATA_DEV_RETRY_DLY));
10507 10506 goto reprobe_cport;
10508 10507 }
10509 10508 }
10510 10509 } else { /* SATA_DTYPE_PMULT */
10511 10510 mutex_exit(&cportinfo->cport_mutex);
10512 10511
10513 10512 /* Allocate sata_pmult_info and sata_pmport_info */
10514 10513 if (sata_alloc_pmult(sata_hba_inst, &sata_device) !=
10515 10514 SATA_SUCCESS)
10516 10515 continue;
10517 10516
10518 10517 /* Log the information of the port multiplier */
10519 10518 sata_show_pmult_info(sata_hba_inst, &sata_device);
10520 10519
10521 10520 /* Probe its pmports */
10522 10521 sata_probe_pmports(sata_hba_inst, ncport);
10523 10522 }
10524 10523 }
10525 10524 }
10526 10525
10527 10526 /*
10528 10527 * Probe all device ports behind a port multiplier.
10529 10528 *
10530 10529 * PMult-related structure should be allocated before by sata_alloc_pmult().
10531 10530 *
10532 10531 * NOTE1: Only called from sata_probe_ports()
10533 10532 * NOTE2: No mutex should be hold.
10534 10533 */
10535 10534 static void
10536 10535 sata_probe_pmports(sata_hba_inst_t *sata_hba_inst, uint8_t ncport)
10537 10536 {
10538 10537 dev_info_t *dip = SATA_DIP(sata_hba_inst);
10539 10538 sata_pmult_info_t *pmultinfo = NULL;
10540 10539 sata_pmport_info_t *pmportinfo = NULL;
10541 10540 sata_drive_info_t *drive = NULL;
10542 10541 sata_device_t sata_device;
10543 10542
10544 10543 clock_t start_time, cur_time;
10545 10544 int npmport;
10546 10545 int rval;
10547 10546
10548 10547 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, ncport);
10549 10548
10550 10549 /* Probe Port Multiplier ports */
10551 10550 for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; npmport++) {
10552 10551 pmportinfo = pmultinfo->pmult_dev_port[npmport];
10553 10552 start_time = ddi_get_lbolt();
10554 10553 reprobe_pmport:
10555 10554 sata_device.satadev_addr.cport = ncport;
10556 10555 sata_device.satadev_addr.pmport = npmport;
10557 10556 sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
10558 10557 sata_device.satadev_rev = SATA_DEVICE_REV;
10559 10558
10560 10559 /* Let HBA driver probe it. */
10561 10560 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10562 10561 (dip, &sata_device);
10563 10562 mutex_enter(&pmportinfo->pmport_mutex);
10564 10563
10565 10564 pmportinfo->pmport_scr = sata_device.satadev_scr;
10566 10565
10567 10566 if (rval != SATA_SUCCESS) {
10568 10567 pmportinfo->pmport_state =
10569 10568 SATA_PSTATE_FAILED;
10570 10569 mutex_exit(&pmportinfo->pmport_mutex);
10571 10570 continue;
10572 10571 }
10573 10572 pmportinfo->pmport_state &= ~SATA_STATE_PROBING;
10574 10573 pmportinfo->pmport_state |= SATA_STATE_PROBED;
10575 10574 pmportinfo->pmport_dev_type = sata_device.satadev_type;
10576 10575
10577 10576 pmportinfo->pmport_state |= SATA_STATE_READY;
10578 10577 if (pmportinfo->pmport_dev_type ==
10579 10578 SATA_DTYPE_NONE) {
10580 10579 SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
10581 10580 "no device found at port %d:%d", ncport, npmport);
10582 10581 mutex_exit(&pmportinfo->pmport_mutex);
10583 10582 continue;
10584 10583 }
10585 10584 /* Port multipliers cannot be chained */
10586 10585 ASSERT(pmportinfo->pmport_dev_type != SATA_DTYPE_PMULT);
10587 10586 /*
10588 10587 * There is something attached to Port
10589 10588 * Multiplier device port
10590 10589 * Allocate device info structure
10591 10590 */
10592 10591 if (pmportinfo->pmport_sata_drive == NULL) {
10593 10592 mutex_exit(&pmportinfo->pmport_mutex);
10594 10593 pmportinfo->pmport_sata_drive =
10595 10594 kmem_zalloc(sizeof (sata_drive_info_t), KM_SLEEP);
10596 10595 mutex_enter(&pmportinfo->pmport_mutex);
10597 10596 }
10598 10597 drive = pmportinfo->pmport_sata_drive;
10599 10598 drive->satadrv_addr.cport = pmportinfo->pmport_addr.cport;
10600 10599 drive->satadrv_addr.pmport = npmport;
10601 10600 drive->satadrv_addr.qual = SATA_ADDR_DPMPORT;
10602 10601 drive->satadrv_type = pmportinfo-> pmport_dev_type;
10603 10602 drive->satadrv_state = SATA_STATE_UNKNOWN;
10604 10603
10605 10604 mutex_exit(&pmportinfo->pmport_mutex);
10606 10605 rval = sata_add_device(dip, sata_hba_inst, &sata_device);
10607 10606
10608 10607 if (rval != SATA_SUCCESS) {
10609 10608 /*
10610 10609 * Plugged device was not correctly identified.
10611 10610 * Retry, within the SATA_DEV_IDENTIFY_TIMEOUT
10612 10611 */
10613 10612 cur_time = ddi_get_lbolt();
10614 10613 if ((cur_time - start_time) < drv_usectohz(
10615 10614 SATA_DEV_IDENTIFY_TIMEOUT)) {
10616 10615 /* sleep for a while */
10617 10616 delay(drv_usectohz(SATA_DEV_RETRY_DLY));
10618 10617 goto reprobe_pmport;
10619 10618 }
10620 10619 }
10621 10620 }
10622 10621 }
10623 10622
10624 10623 /*
10625 10624 * Add SATA device for specified HBA instance & port (SCSI target
10626 10625 * device nodes).
10627 10626 * This function is called (indirectly) only from sata_hba_attach().
10628 10627 * A target node is created when there is a supported type device attached,
10629 10628 * but may be removed if it cannot be put online.
10630 10629 *
10631 10630 * This function cannot be called from an interrupt context.
10632 10631 *
10633 10632 * Create target nodes for disk, CD/DVD, Tape and ATAPI disk devices
10634 10633 *
10635 10634 * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when
10636 10635 * device identification failed - adding a device could be retried.
10637 10636 *
10638 10637 */
10639 10638 static int
10640 10639 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst,
10641 10640 sata_device_t *sata_device)
10642 10641 {
10643 10642 sata_cport_info_t *cportinfo;
10644 10643 sata_pmult_info_t *pminfo;
10645 10644 sata_pmport_info_t *pmportinfo;
10646 10645 dev_info_t *cdip; /* child dip */
10647 10646 sata_address_t *saddr = &sata_device->satadev_addr;
10648 10647 uint8_t cport, pmport;
10649 10648 int rval;
10650 10649
10651 10650 cport = saddr->cport;
10652 10651 pmport = saddr->pmport;
10653 10652 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
10654 10653 ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE);
10655 10654
10656 10655 /*
10657 10656 * Some device is attached to a controller port.
10658 10657 * We rely on controllers distinquishing between no-device,
10659 10658 * attached port multiplier and other kind of attached device.
10660 10659 * We need to get Identify Device data and determine
10661 10660 * positively the dev type before trying to attach
10662 10661 * the target driver.
10663 10662 */
10664 10663 sata_device->satadev_rev = SATA_DEVICE_REV;
10665 10664 switch (saddr->qual) {
10666 10665 case SATA_ADDR_CPORT:
10667 10666 /*
10668 10667 * Add a non-port-multiplier device at controller port.
10669 10668 */
10670 10669 saddr->qual = SATA_ADDR_DCPORT;
10671 10670
10672 10671 rval = sata_probe_device(sata_hba_inst, sata_device);
10673 10672 if (rval != SATA_SUCCESS ||
10674 10673 sata_device->satadev_type == SATA_DTYPE_UNKNOWN)
10675 10674 return (SATA_FAILURE);
10676 10675
10677 10676 mutex_enter(&cportinfo->cport_mutex);
10678 10677 sata_show_drive_info(sata_hba_inst,
10679 10678 SATA_CPORTINFO_DRV_INFO(cportinfo));
10680 10679
10681 10680 if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) {
10682 10681 /*
10683 10682 * Could not determine device type or
10684 10683 * a device is not supported.
10685 10684 * Degrade this device to unknown.
10686 10685 */
10687 10686 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
10688 10687 mutex_exit(&cportinfo->cport_mutex);
10689 10688 return (SATA_SUCCESS);
10690 10689 }
10691 10690 cportinfo->cport_dev_type = sata_device->satadev_type;
10692 10691 cportinfo->cport_tgtnode_clean = B_TRUE;
10693 10692 mutex_exit(&cportinfo->cport_mutex);
10694 10693
10695 10694 /*
10696 10695 * Initialize device to the desired state. Even if it
10697 10696 * fails, the device will still attach but syslog
10698 10697 * will show the warning.
10699 10698 */
10700 10699 if (sata_initialize_device(sata_hba_inst,
10701 10700 SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) {
10702 10701 /* Retry */
10703 10702 rval = sata_initialize_device(sata_hba_inst,
10704 10703 SATA_CPORTINFO_DRV_INFO(cportinfo));
10705 10704
10706 10705 if (rval == SATA_RETRY)
10707 10706 sata_log(sata_hba_inst, CE_WARN,
10708 10707 "SATA device at port %d - "
10709 10708 "default device features could not be set."
10710 10709 " Device may not operate as expected.",
10711 10710 cport);
10712 10711 }
10713 10712
10714 10713 cdip = sata_create_target_node(pdip, sata_hba_inst, saddr);
10715 10714 if (cdip == NULL) {
10716 10715 /*
10717 10716 * Attaching target node failed.
10718 10717 * We retain sata_drive_info structure...
10719 10718 */
10720 10719 return (SATA_SUCCESS);
10721 10720 }
10722 10721
10723 10722 mutex_enter(&cportinfo->cport_mutex);
10724 10723 (SATA_CPORTINFO_DRV_INFO(cportinfo))->
10725 10724 satadrv_state = SATA_STATE_READY;
10726 10725 mutex_exit(&cportinfo->cport_mutex);
10727 10726
10728 10727 break;
10729 10728
10730 10729 case SATA_ADDR_PMPORT:
10731 10730 saddr->qual = SATA_ADDR_DPMPORT;
10732 10731
10733 10732 mutex_enter(&cportinfo->cport_mutex);
10734 10733 /* It must be a Port Multiplier at the controller port */
10735 10734 ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT);
10736 10735
10737 10736 pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
10738 10737 pmportinfo = pminfo->pmult_dev_port[saddr->pmport];
10739 10738 mutex_exit(&cportinfo->cport_mutex);
10740 10739
10741 10740 rval = sata_probe_device(sata_hba_inst, sata_device);
10742 10741 if (rval != SATA_SUCCESS ||
10743 10742 sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
10744 10743 return (SATA_FAILURE);
10745 10744 }
10746 10745
10747 10746 mutex_enter(&pmportinfo->pmport_mutex);
10748 10747 sata_show_drive_info(sata_hba_inst,
10749 10748 SATA_PMPORTINFO_DRV_INFO(pmportinfo));
10750 10749
10751 10750 if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) {
10752 10751 /*
10753 10752 * Could not determine device type.
10754 10753 * Degrade this device to unknown.
10755 10754 */
10756 10755 pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
10757 10756 mutex_exit(&pmportinfo->pmport_mutex);
10758 10757 return (SATA_SUCCESS);
10759 10758 }
10760 10759 pmportinfo->pmport_dev_type = sata_device->satadev_type;
10761 10760 pmportinfo->pmport_tgtnode_clean = B_TRUE;
10762 10761 mutex_exit(&pmportinfo->pmport_mutex);
10763 10762
10764 10763 /*
10765 10764 * Initialize device to the desired state.
10766 10765 * Even if it fails, the device will still
10767 10766 * attach but syslog will show the warning.
10768 10767 */
10769 10768 if (sata_initialize_device(sata_hba_inst,
10770 10769 pmportinfo->pmport_sata_drive) != SATA_SUCCESS) {
10771 10770 /* Retry */
10772 10771 rval = sata_initialize_device(sata_hba_inst,
10773 10772 pmportinfo->pmport_sata_drive);
10774 10773
10775 10774 if (rval == SATA_RETRY)
10776 10775 sata_log(sata_hba_inst, CE_WARN,
10777 10776 "SATA device at port %d:%d - "
10778 10777 "default device features could not be set."
10779 10778 " Device may not operate as expected.",
10780 10779 cport, pmport);
10781 10780 }
10782 10781
10783 10782 cdip = sata_create_target_node(pdip, sata_hba_inst, saddr);
10784 10783 if (cdip == NULL) {
10785 10784 /*
10786 10785 * Attaching target node failed.
10787 10786 * We retain sata_drive_info structure...
10788 10787 */
10789 10788 return (SATA_SUCCESS);
10790 10789 }
10791 10790 mutex_enter(&pmportinfo->pmport_mutex);
10792 10791 pmportinfo->pmport_sata_drive->satadrv_state |=
10793 10792 SATA_STATE_READY;
10794 10793 mutex_exit(&pmportinfo->pmport_mutex);
10795 10794
10796 10795 break;
10797 10796
10798 10797 default:
10799 10798 return (SATA_FAILURE);
10800 10799 }
10801 10800
10802 10801 return (SATA_SUCCESS);
10803 10802 }
10804 10803
10805 10804 /*
10806 10805 * Clean up target node at specific address.
10807 10806 *
10808 10807 * NOTE: No Mutex should be hold.
10809 10808 */
10810 10809 static int
10811 10810 sata_offline_device(sata_hba_inst_t *sata_hba_inst,
10812 10811 sata_device_t *sata_device, sata_drive_info_t *sdinfo)
10813 10812 {
10814 10813 uint8_t cport, pmport, qual;
10815 10814 dev_info_t *tdip;
10816 10815
10817 10816 cport = sata_device->satadev_addr.cport;
10818 10817 pmport = sata_device->satadev_addr.pmport;
10819 10818 qual = sata_device->satadev_addr.qual;
10820 10819
10821 10820 if (qual == SATA_ADDR_DCPORT) {
10822 10821 SATA_LOG_D((sata_hba_inst, CE_WARN,
10823 10822 "sata_hba_ioctl: disconnect device at port %d", cport));
10824 10823 } else {
10825 10824 SATA_LOG_D((sata_hba_inst, CE_WARN,
10826 10825 "sata_hba_ioctl: disconnect device at port %d:%d",
10827 10826 cport, pmport));
10828 10827 }
10829 10828
10830 10829 /* We are addressing attached device, not a port */
10831 10830 sata_device->satadev_addr.qual =
10832 10831 sdinfo->satadrv_addr.qual;
10833 10832 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
10834 10833 &sata_device->satadev_addr);
10835 10834 if (tdip != NULL && ndi_devi_offline(tdip,
10836 10835 NDI_DEVI_REMOVE) != NDI_SUCCESS) {
10837 10836 /*
10838 10837 * Problem :
10839 10838 * The target node remained attached.
10840 10839 * This happens when the device file was open
10841 10840 * or a node was waiting for resources.
10842 10841 * Cannot do anything about it.
10843 10842 */
10844 10843 if (qual == SATA_ADDR_DCPORT) {
10845 10844 SATA_LOG_D((sata_hba_inst, CE_WARN,
10846 10845 "sata_hba_ioctl: disconnect: could "
10847 10846 "not unconfigure device before "
10848 10847 "disconnecting the SATA port %d",
10849 10848 cport));
10850 10849 } else {
10851 10850 SATA_LOG_D((sata_hba_inst, CE_WARN,
10852 10851 "sata_hba_ioctl: disconnect: could "
10853 10852 "not unconfigure device before "
10854 10853 "disconnecting the SATA port %d:%d",
10855 10854 cport, pmport));
10856 10855 }
10857 10856 /*
10858 10857 * Set DEVICE REMOVED state in the target
10859 10858 * node. It will prevent access to the device
10860 10859 * even when a new device is attached, until
10861 10860 * the old target node is released, removed and
10862 10861 * recreated for a new device.
10863 10862 */
10864 10863 sata_set_device_removed(tdip);
10865 10864
10866 10865 /*
10867 10866 * Instruct event daemon to try the target
10868 10867 * node cleanup later.
10869 10868 */
10870 10869 sata_set_target_node_cleanup(
10871 10870 sata_hba_inst, &sata_device->satadev_addr);
10872 10871 }
10873 10872
10874 10873
10875 10874 return (SATA_SUCCESS);
10876 10875 }
10877 10876
10878 10877
10879 10878 /*
10880 10879 * Create scsi target node for attached device, create node properties and
10881 10880 * attach the node.
10882 10881 * The node could be removed if the device onlining fails.
10883 10882 *
10884 10883 * A dev_info_t pointer is returned if operation is successful, NULL is
10885 10884 * returned otherwise.
10886 10885 */
10887 10886
10888 10887 static dev_info_t *
10889 10888 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst,
10890 10889 sata_address_t *sata_addr)
10891 10890 {
10892 10891 dev_info_t *cdip = NULL;
10893 10892 int rval;
10894 10893 char *nname = NULL;
10895 10894 char **compatible = NULL;
10896 10895 int ncompatible;
10897 10896 struct scsi_inquiry inq;
10898 10897 sata_device_t sata_device;
10899 10898 sata_drive_info_t *sdinfo;
10900 10899 int target;
10901 10900 int i;
10902 10901
10903 10902 sata_device.satadev_rev = SATA_DEVICE_REV;
10904 10903 sata_device.satadev_addr = *sata_addr;
10905 10904
10906 10905 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport)));
10907 10906
10908 10907 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
10909 10908
10910 10909 target = SATA_TO_SCSI_TARGET(sata_addr->cport,
10911 10910 sata_addr->pmport, sata_addr->qual);
10912 10911
10913 10912 if (sdinfo == NULL) {
10914 10913 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10915 10914 sata_addr->cport)));
10916 10915 SATA_LOG_D((sata_hba_inst, CE_WARN,
10917 10916 "sata_create_target_node: no sdinfo for target %x",
10918 10917 target));
10919 10918 return (NULL);
10920 10919 }
10921 10920
10922 10921 /*
10923 10922 * create or get scsi inquiry data, expected by
10924 10923 * scsi_hba_nodename_compatible_get()
10925 10924 * SATA hard disks get Identify Data translated into Inguiry Data.
10926 10925 * ATAPI devices respond directly to Inquiry request.
10927 10926 */
10928 10927 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10929 10928 sata_identdev_to_inquiry(sata_hba_inst, sdinfo,
10930 10929 (uint8_t *)&inq);
10931 10930 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10932 10931 sata_addr->cport)));
10933 10932 } else { /* Assume supported ATAPI device */
10934 10933 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10935 10934 sata_addr->cport)));
10936 10935 if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr,
10937 10936 &inq) == SATA_FAILURE)
10938 10937 return (NULL);
10939 10938 /*
10940 10939 * Save supported ATAPI transport version
10941 10940 */
10942 10941 sdinfo->satadrv_atapi_trans_ver =
10943 10942 SATA_ATAPI_TRANS_VERSION(&inq);
10944 10943 }
10945 10944
10946 10945 /* determine the node name and compatible */
10947 10946 scsi_hba_nodename_compatible_get(&inq, NULL,
10948 10947 inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
10949 10948
10950 10949 #ifdef SATA_DEBUG
10951 10950 if (sata_debug_flags & SATA_DBG_NODES) {
10952 10951 if (nname == NULL) {
10953 10952 cmn_err(CE_NOTE, "sata_create_target_node: "
10954 10953 "cannot determine nodename for target %d\n",
10955 10954 target);
10956 10955 } else {
10957 10956 cmn_err(CE_WARN, "sata_create_target_node: "
10958 10957 "target %d nodename: %s\n", target, nname);
10959 10958 }
10960 10959 if (compatible == NULL) {
10961 10960 cmn_err(CE_WARN,
10962 10961 "sata_create_target_node: no compatible name\n");
10963 10962 } else {
10964 10963 for (i = 0; i < ncompatible; i++) {
10965 10964 cmn_err(CE_WARN, "sata_create_target_node: "
10966 10965 "compatible name: %s\n", compatible[i]);
10967 10966 }
10968 10967 }
10969 10968 }
10970 10969 #endif
10971 10970
10972 10971 /* if nodename can't be determined, log error and exit */
10973 10972 if (nname == NULL) {
10974 10973 SATA_LOG_D((sata_hba_inst, CE_WARN,
10975 10974 "sata_create_target_node: cannot determine nodename "
10976 10975 "for target %d\n", target));
10977 10976 scsi_hba_nodename_compatible_free(nname, compatible);
10978 10977 return (NULL);
10979 10978 }
10980 10979 /*
10981 10980 * Create scsi target node
10982 10981 */
10983 10982 ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip);
10984 10983 rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
10985 10984 "device-type", "scsi");
10986 10985
10987 10986 if (rval != DDI_PROP_SUCCESS) {
10988 10987 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
10989 10988 "updating device_type prop failed %d", rval));
10990 10989 goto fail;
10991 10990 }
10992 10991
10993 10992 /*
10994 10993 * Create target node properties: target & lun
10995 10994 */
10996 10995 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target);
10997 10996 if (rval != DDI_PROP_SUCCESS) {
10998 10997 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
10999 10998 "updating target prop failed %d", rval));
11000 10999 goto fail;
11001 11000 }
11002 11001 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0);
11003 11002 if (rval != DDI_PROP_SUCCESS) {
11004 11003 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
11005 11004 "updating target prop failed %d", rval));
11006 11005 goto fail;
11007 11006 }
11008 11007
11009 11008 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
11010 11009 /*
11011 11010 * Add "variant" property
11012 11011 */
11013 11012 rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
11014 11013 "variant", "atapi");
11015 11014 if (rval != DDI_PROP_SUCCESS) {
11016 11015 SATA_LOG_D((sata_hba_inst, CE_WARN,
11017 11016 "sata_create_target_node: variant atapi "
11018 11017 "property could not be created: %d", rval));
11019 11018 goto fail;
11020 11019 }
11021 11020 }
11022 11021 /* decorate the node with compatible */
11023 11022 if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible",
11024 11023 compatible, ncompatible) != DDI_PROP_SUCCESS) {
11025 11024 SATA_LOG_D((sata_hba_inst, CE_WARN,
11026 11025 "sata_create_target_node: FAIL compatible props cdip 0x%p",
11027 11026 (void *)cdip));
11028 11027 goto fail;
11029 11028 }
11030 11029
11031 11030 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
11032 11031 /*
11033 11032 * Add "sata-phy" property
11034 11033 */
11035 11034 if (ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "sata-phy",
11036 11035 (int)sata_addr->cport) != DDI_PROP_SUCCESS) {
11037 11036 SATA_LOG_D((sata_hba_inst, CE_WARN,
11038 11037 "sata_create_target_node: failed to create "
11039 11038 "\"sata-phy\" property: port %d",
11040 11039 sata_addr->cport));
11041 11040 }
11042 11041 }
11043 11042
11044 11043
11045 11044 /*
11046 11045 * Now, try to attach the driver. If probing of the device fails,
11047 11046 * the target node may be removed
11048 11047 */
11049 11048 rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH);
11050 11049
11051 11050 scsi_hba_nodename_compatible_free(nname, compatible);
11052 11051
11053 11052 if (rval == NDI_SUCCESS)
11054 11053 return (cdip);
11055 11054
11056 11055 /* target node was removed - are we sure? */
11057 11056 return (NULL);
11058 11057
11059 11058 fail:
11060 11059 scsi_hba_nodename_compatible_free(nname, compatible);
11061 11060 ddi_prop_remove_all(cdip);
11062 11061 rval = ndi_devi_free(cdip);
11063 11062 if (rval != NDI_SUCCESS) {
11064 11063 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
11065 11064 "node removal failed %d", rval));
11066 11065 }
11067 11066 sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: "
11068 11067 "cannot create target node for SATA device at port %d",
11069 11068 sata_addr->cport);
11070 11069 return (NULL);
11071 11070 }
11072 11071
11073 11072 /*
11074 11073 * Remove a target node.
11075 11074 */
11076 11075 static void
11077 11076 sata_remove_target_node(sata_hba_inst_t *sata_hba_inst,
11078 11077 sata_address_t *sata_addr)
11079 11078 {
11080 11079 dev_info_t *tdip;
11081 11080 uint8_t cport = sata_addr->cport;
11082 11081 uint8_t pmport = sata_addr->pmport;
11083 11082 uint8_t qual = sata_addr->qual;
11084 11083
11085 11084 /* Note the sata daemon uses the address of the port/pmport */
11086 11085 ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
11087 11086
11088 11087 /* Remove target node */
11089 11088 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), cport, pmport);
11090 11089 if (tdip != NULL) {
11091 11090 /*
11092 11091 * Target node exists. Unconfigure device
11093 11092 * then remove the target node (one ndi
11094 11093 * operation).
11095 11094 */
11096 11095 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
11097 11096 /*
11098 11097 * PROBLEM - no device, but target node remained. This
11099 11098 * happens when the file was open or node was waiting
11100 11099 * for resources.
11101 11100 */
11102 11101 SATA_LOG_D((sata_hba_inst, CE_WARN,
11103 11102 "sata_remove_target_node: "
11104 11103 "Failed to remove target node for "
11105 11104 "detached SATA device."));
11106 11105 /*
11107 11106 * Set target node state to DEVI_DEVICE_REMOVED. But
11108 11107 * re-check first that the node still exists.
11109 11108 */
11110 11109 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
11111 11110 cport, pmport);
11112 11111 if (tdip != NULL) {
11113 11112 sata_set_device_removed(tdip);
11114 11113 /*
11115 11114 * Instruct event daemon to retry the cleanup
11116 11115 * later.
11117 11116 */
11118 11117 sata_set_target_node_cleanup(sata_hba_inst,
11119 11118 sata_addr);
11120 11119 }
11121 11120 }
11122 11121
11123 11122 if (qual == SATA_ADDR_CPORT)
11124 11123 sata_log(sata_hba_inst, CE_WARN,
11125 11124 "SATA device detached at port %d", cport);
11126 11125 else
11127 11126 sata_log(sata_hba_inst, CE_WARN,
11128 11127 "SATA device detached at port %d:%d",
11129 11128 cport, pmport);
11130 11129 }
11131 11130 #ifdef SATA_DEBUG
11132 11131 else {
11133 11132 if (qual == SATA_ADDR_CPORT)
11134 11133 sata_log(sata_hba_inst, CE_WARN,
11135 11134 "target node not found at port %d", cport);
11136 11135 else
11137 11136 sata_log(sata_hba_inst, CE_WARN,
11138 11137 "target node not found at port %d:%d",
11139 11138 cport, pmport);
11140 11139 }
11141 11140 #endif
11142 11141 }
11143 11142
11144 11143
11145 11144 /*
11146 11145 * Re-probe sata port, check for a device and attach info
11147 11146 * structures when necessary. Identify Device data is fetched, if possible.
11148 11147 * Assumption: sata address is already validated.
11149 11148 * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
11150 11149 * the presence of a device and its type.
11151 11150 *
11152 11151 * flag arg specifies that the function should try multiple times to identify
11153 11152 * device type and to initialize it, or it should return immediately on failure.
11154 11153 * SATA_DEV_IDENTIFY_RETRY - retry
11155 11154 * SATA_DEV_IDENTIFY_NORETRY - no retry
11156 11155 *
11157 11156 * SATA_FAILURE is returned if one of the operations failed.
11158 11157 *
11159 11158 * This function cannot be called in interrupt context - it may sleep.
11160 11159 *
11161 11160 * Note: Port multiplier is supported.
11162 11161 */
11163 11162 static int
11164 11163 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11165 11164 int flag)
11166 11165 {
11167 11166 sata_cport_info_t *cportinfo;
11168 11167 sata_pmult_info_t *pmultinfo;
11169 11168 sata_drive_info_t *sdinfo, *osdinfo;
11170 11169 boolean_t init_device = B_FALSE;
11171 11170 int prev_device_type = SATA_DTYPE_NONE;
11172 11171 int prev_device_settings = 0;
11173 11172 int prev_device_state = 0;
11174 11173 clock_t start_time;
11175 11174 int retry = B_FALSE;
11176 11175 uint8_t cport = sata_device->satadev_addr.cport;
11177 11176 int rval_probe, rval_init;
11178 11177
11179 11178 /*
11180 11179 * If target is pmport, sata_reprobe_pmport() will handle it.
11181 11180 */
11182 11181 if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT ||
11183 11182 sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT)
11184 11183 return (sata_reprobe_pmport(sata_hba_inst, sata_device, flag));
11185 11184
11186 11185 /* We only care about host sata cport for now */
11187 11186 cportinfo = SATA_CPORT_INFO(sata_hba_inst,
11188 11187 sata_device->satadev_addr.cport);
11189 11188
11190 11189 /*
11191 11190 * If a port multiplier was previously attached (we have no idea it
11192 11191 * still there or not), sata_reprobe_pmult() will handle it.
11193 11192 */
11194 11193 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT)
11195 11194 return (sata_reprobe_pmult(sata_hba_inst, sata_device, flag));
11196 11195
11197 11196 /* Store sata_drive_info when a non-pmult device was attached. */
11198 11197 osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11199 11198 if (osdinfo != NULL) {
11200 11199 /*
11201 11200 * We are re-probing port with a previously attached device.
11202 11201 * Save previous device type and settings.
11203 11202 */
11204 11203 prev_device_type = cportinfo->cport_dev_type;
11205 11204 prev_device_settings = osdinfo->satadrv_settings;
11206 11205 prev_device_state = osdinfo->satadrv_state;
11207 11206 }
11208 11207 if (flag == SATA_DEV_IDENTIFY_RETRY) {
11209 11208 start_time = ddi_get_lbolt();
11210 11209 retry = B_TRUE;
11211 11210 }
11212 11211 retry_probe:
11213 11212
11214 11213 /* probe port */
11215 11214 mutex_enter(&cportinfo->cport_mutex);
11216 11215 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11217 11216 cportinfo->cport_state |= SATA_STATE_PROBING;
11218 11217 mutex_exit(&cportinfo->cport_mutex);
11219 11218
11220 11219 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11221 11220 (SATA_DIP(sata_hba_inst), sata_device);
11222 11221
11223 11222 mutex_enter(&cportinfo->cport_mutex);
11224 11223 if (rval_probe != SATA_SUCCESS) {
11225 11224 cportinfo->cport_state = SATA_PSTATE_FAILED;
11226 11225 mutex_exit(&cportinfo->cport_mutex);
11227 11226 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: "
11228 11227 "SATA port %d probing failed",
11229 11228 cportinfo->cport_addr.cport));
11230 11229 return (SATA_FAILURE);
11231 11230 }
11232 11231
11233 11232 /*
11234 11233 * update sata port state and set device type
11235 11234 */
11236 11235 sata_update_port_info(sata_hba_inst, sata_device);
11237 11236 cportinfo->cport_state &= ~SATA_STATE_PROBING;
11238 11237
11239 11238 /*
11240 11239 * Sanity check - Port is active? Is the link active?
11241 11240 * Is there any device attached?
11242 11241 */
11243 11242 if ((cportinfo->cport_state &
11244 11243 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11245 11244 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11246 11245 SATA_PORT_DEVLINK_UP) {
11247 11246 /*
11248 11247 * Port in non-usable state or no link active/no device.
11249 11248 * Free info structure if necessary (direct attached drive
11250 11249 * only, for now!
11251 11250 */
11252 11251 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11253 11252 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11254 11253 /* Add here differentiation for device attached or not */
11255 11254 cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11256 11255 mutex_exit(&cportinfo->cport_mutex);
11257 11256 if (sdinfo != NULL)
11258 11257 kmem_free(sdinfo, sizeof (sata_drive_info_t));
11259 11258 return (SATA_SUCCESS);
11260 11259 }
11261 11260
11262 11261 cportinfo->cport_state |= SATA_STATE_READY;
11263 11262 cportinfo->cport_state |= SATA_STATE_PROBED;
11264 11263
11265 11264 cportinfo->cport_dev_type = sata_device->satadev_type;
11266 11265 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11267 11266
11268 11267 /*
11269 11268 * If we are re-probing the port, there may be
11270 11269 * sata_drive_info structure attached
11271 11270 */
11272 11271 if (sata_device->satadev_type == SATA_DTYPE_NONE) {
11273 11272
11274 11273 /*
11275 11274 * There is no device, so remove device info structure,
11276 11275 * if necessary.
11277 11276 */
11278 11277 /* Device change: Drive -> None */
11279 11278 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11280 11279 cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11281 11280 if (sdinfo != NULL) {
11282 11281 kmem_free(sdinfo, sizeof (sata_drive_info_t));
11283 11282 sata_log(sata_hba_inst, CE_WARN,
11284 11283 "SATA device detached "
11285 11284 "from port %d", cportinfo->cport_addr.cport);
11286 11285 }
11287 11286 mutex_exit(&cportinfo->cport_mutex);
11288 11287 return (SATA_SUCCESS);
11289 11288
11290 11289 }
11291 11290
11292 11291 if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
11293 11292
11294 11293 /* Device (may) change: Drive -> Drive */
11295 11294 if (sdinfo == NULL) {
11296 11295 /*
11297 11296 * There is some device attached, but there is
11298 11297 * no sata_drive_info structure - allocate one
11299 11298 */
11300 11299 mutex_exit(&cportinfo->cport_mutex);
11301 11300 sdinfo = kmem_zalloc(
11302 11301 sizeof (sata_drive_info_t), KM_SLEEP);
11303 11302 mutex_enter(&cportinfo->cport_mutex);
11304 11303 /*
11305 11304 * Recheck, that the port state did not change when we
11306 11305 * released mutex.
11307 11306 */
11308 11307 if (cportinfo->cport_state & SATA_STATE_READY) {
11309 11308 SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo;
11310 11309 sdinfo->satadrv_addr = cportinfo->cport_addr;
11311 11310 sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT;
11312 11311 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11313 11312 sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
11314 11313 } else {
11315 11314 /*
11316 11315 * Port is not in ready state, we
11317 11316 * cannot attach a device.
11318 11317 */
11319 11318 mutex_exit(&cportinfo->cport_mutex);
11320 11319 kmem_free(sdinfo, sizeof (sata_drive_info_t));
11321 11320 return (SATA_SUCCESS);
11322 11321 }
11323 11322 /*
11324 11323 * Since we are adding device, presumably new one,
11325 11324 * indicate that it should be initalized,
11326 11325 * as well as some internal framework states).
11327 11326 */
11328 11327 init_device = B_TRUE;
11329 11328 }
11330 11329 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11331 11330 sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
11332 11331 } else {
11333 11332 /* Device change: Drive -> PMult */
11334 11333 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11335 11334 if (sdinfo != NULL) {
11336 11335 kmem_free(sdinfo, sizeof (sata_drive_info_t));
11337 11336 sata_log(sata_hba_inst, CE_WARN,
11338 11337 "SATA device detached "
11339 11338 "from port %d", cportinfo->cport_addr.cport);
11340 11339 }
11341 11340
11342 11341 sata_log(sata_hba_inst, CE_WARN,
11343 11342 "SATA port multiplier detected at port %d",
11344 11343 cportinfo->cport_addr.cport);
11345 11344
11346 11345 mutex_exit(&cportinfo->cport_mutex);
11347 11346 if (sata_alloc_pmult(sata_hba_inst, sata_device) !=
11348 11347 SATA_SUCCESS)
11349 11348 return (SATA_FAILURE);
11350 11349 sata_show_pmult_info(sata_hba_inst, sata_device);
11351 11350 mutex_enter(&cportinfo->cport_mutex);
11352 11351
11353 11352 /*
11354 11353 * Mark all the port multiplier port behind the port
11355 11354 * multiplier behind with link events, so that the sata daemon
11356 11355 * will update their status.
11357 11356 */
11358 11357 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
11359 11358 pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET;
11360 11359 mutex_exit(&cportinfo->cport_mutex);
11361 11360 return (SATA_SUCCESS);
11362 11361 }
11363 11362 mutex_exit(&cportinfo->cport_mutex);
11364 11363
11365 11364 /*
11366 11365 * Figure out what kind of device we are really
11367 11366 * dealing with. Failure of identifying device does not fail this
11368 11367 * function.
11369 11368 */
11370 11369 rval_probe = sata_probe_device(sata_hba_inst, sata_device);
11371 11370 rval_init = SATA_FAILURE;
11372 11371 mutex_enter(&cportinfo->cport_mutex);
11373 11372 if (rval_probe == SATA_SUCCESS) {
11374 11373 /*
11375 11374 * If we are dealing with the same type of a device as before,
11376 11375 * restore its settings flags.
11377 11376 */
11378 11377 if (osdinfo != NULL &&
11379 11378 sata_device->satadev_type == prev_device_type)
11380 11379 sdinfo->satadrv_settings = prev_device_settings;
11381 11380
11382 11381 mutex_exit(&cportinfo->cport_mutex);
11383 11382 rval_init = SATA_SUCCESS;
11384 11383 /* Set initial device features, if necessary */
11385 11384 if (init_device == B_TRUE) {
11386 11385 rval_init = sata_initialize_device(sata_hba_inst,
11387 11386 sdinfo);
11388 11387 }
11389 11388 if (rval_init == SATA_SUCCESS)
11390 11389 return (rval_init);
11391 11390 /* else we will retry if retry was asked for */
11392 11391
11393 11392 } else {
11394 11393 /*
11395 11394 * If there was some device info before we probe the device,
11396 11395 * restore previous device setting, so we can retry from scratch
11397 11396 * later. Providing, of course, that device has not disapear
11398 11397 * during probing process.
11399 11398 */
11400 11399 if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11401 11400 if (osdinfo != NULL) {
11402 11401 cportinfo->cport_dev_type = prev_device_type;
11403 11402 sdinfo->satadrv_type = prev_device_type;
11404 11403 sdinfo->satadrv_state = prev_device_state;
11405 11404 }
11406 11405 } else {
11407 11406 /* device is gone */
11408 11407 kmem_free(sdinfo, sizeof (sata_drive_info_t));
11409 11408 cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11410 11409 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11411 11410 mutex_exit(&cportinfo->cport_mutex);
11412 11411 return (SATA_SUCCESS);
11413 11412 }
11414 11413 mutex_exit(&cportinfo->cport_mutex);
11415 11414 }
11416 11415
11417 11416 if (retry) {
11418 11417 clock_t cur_time = ddi_get_lbolt();
11419 11418 /*
11420 11419 * A device was not successfully identified or initialized.
11421 11420 * Track retry time for device identification.
11422 11421 */
11423 11422 if ((cur_time - start_time) <
11424 11423 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11425 11424 /* sleep for a while */
11426 11425 delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11427 11426 goto retry_probe;
11428 11427 }
11429 11428 /* else no more retries */
11430 11429 mutex_enter(&cportinfo->cport_mutex);
11431 11430 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
11432 11431 if (rval_init == SATA_RETRY) {
11433 11432 /*
11434 11433 * Setting drive features have failed, but
11435 11434 * because the drive is still accessible,
11436 11435 * keep it and emit a warning message.
11437 11436 */
11438 11437 sata_log(sata_hba_inst, CE_WARN,
11439 11438 "SATA device at port %d - desired "
11440 11439 "drive features could not be set. "
11441 11440 "Device may not operate as expected.",
11442 11441 cportinfo->cport_addr.cport);
11443 11442 } else {
11444 11443 SATA_CPORTINFO_DRV_INFO(cportinfo)->
11445 11444 satadrv_state = SATA_DSTATE_FAILED;
11446 11445 }
11447 11446 }
11448 11447 mutex_exit(&cportinfo->cport_mutex);
11449 11448 }
11450 11449 return (SATA_SUCCESS);
11451 11450 }
11452 11451
11453 11452 /*
11454 11453 * Reprobe a controller port that connected to a port multiplier.
11455 11454 *
11456 11455 * NOTE: No Mutex should be hold.
11457 11456 */
11458 11457 static int
11459 11458 sata_reprobe_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11460 11459 int flag)
11461 11460 {
11462 11461 _NOTE(ARGUNUSED(flag))
11463 11462 sata_cport_info_t *cportinfo;
11464 11463 sata_pmult_info_t *pmultinfo;
11465 11464 uint8_t cport = sata_device->satadev_addr.cport;
11466 11465 int rval_probe;
11467 11466
11468 11467 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11469 11468 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
11470 11469
11471 11470 /* probe port */
11472 11471 mutex_enter(&cportinfo->cport_mutex);
11473 11472 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11474 11473 cportinfo->cport_state |= SATA_STATE_PROBING;
11475 11474 mutex_exit(&cportinfo->cport_mutex);
11476 11475
11477 11476 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11478 11477 (SATA_DIP(sata_hba_inst), sata_device);
11479 11478
11480 11479 mutex_enter(&cportinfo->cport_mutex);
11481 11480 if (rval_probe != SATA_SUCCESS) {
11482 11481 cportinfo->cport_state = SATA_PSTATE_FAILED;
11483 11482 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmult: "
11484 11483 "SATA port %d probing failed", cport));
11485 11484 sata_log(sata_hba_inst, CE_WARN,
11486 11485 "SATA port multiplier detached at port %d", cport);
11487 11486 mutex_exit(&cportinfo->cport_mutex);
11488 11487 sata_free_pmult(sata_hba_inst, sata_device);
11489 11488 return (SATA_FAILURE);
11490 11489 }
11491 11490
11492 11491 /*
11493 11492 * update sata port state and set device type
11494 11493 */
11495 11494 sata_update_port_info(sata_hba_inst, sata_device);
11496 11495 cportinfo->cport_state &= ~SATA_STATE_PROBING;
11497 11496 cportinfo->cport_state |= SATA_STATE_PROBED;
11498 11497
11499 11498 /*
11500 11499 * Sanity check - Port is active? Is the link active?
11501 11500 * Is there any device attached?
11502 11501 */
11503 11502 if ((cportinfo->cport_state &
11504 11503 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11505 11504 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11506 11505 SATA_PORT_DEVLINK_UP ||
11507 11506 (sata_device->satadev_type == SATA_DTYPE_NONE)) {
11508 11507 cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11509 11508 mutex_exit(&cportinfo->cport_mutex);
11510 11509 sata_free_pmult(sata_hba_inst, sata_device);
11511 11510 sata_log(sata_hba_inst, CE_WARN,
11512 11511 "SATA port multiplier detached at port %d", cport);
11513 11512 return (SATA_SUCCESS);
11514 11513 }
11515 11514
11516 11515 /*
11517 11516 * Device changed: PMult -> Non-PMult
11518 11517 *
11519 11518 * This situation is uncommon, most possibly being caused by errors
11520 11519 * after which the port multiplier is not correct initialized and
11521 11520 * recognized. In that case the new device will be marked as unknown
11522 11521 * and will not be automatically probed in this routine. Instead
11523 11522 * system administrator could manually restart it via cfgadm(1M).
11524 11523 */
11525 11524 if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
11526 11525 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11527 11526 mutex_exit(&cportinfo->cport_mutex);
11528 11527 sata_free_pmult(sata_hba_inst, sata_device);
11529 11528 sata_log(sata_hba_inst, CE_WARN,
11530 11529 "SATA port multiplier detached at port %d", cport);
11531 11530 return (SATA_FAILURE);
11532 11531 }
11533 11532
11534 11533 /*
11535 11534 * Now we know it is a port multiplier. However, if this is not the
11536 11535 * previously attached port multiplier - they may have different
11537 11536 * pmport numbers - we need to re-allocate data structures for every
11538 11537 * pmport and drive.
11539 11538 *
11540 11539 * Port multipliers of the same model have identical values in these
11541 11540 * registers, so it is still necessary to update the information of
11542 11541 * all drives attached to the previous port multiplier afterwards.
11543 11542 */
11544 11543 /* Device changed: PMult -> another PMult */
11545 11544 mutex_exit(&cportinfo->cport_mutex);
11546 11545 sata_free_pmult(sata_hba_inst, sata_device);
11547 11546 if (sata_alloc_pmult(sata_hba_inst, sata_device) != SATA_SUCCESS)
11548 11547 return (SATA_FAILURE);
11549 11548 mutex_enter(&cportinfo->cport_mutex);
11550 11549
11551 11550 SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
11552 11551 "SATA port multiplier [changed] at port %d", cport);
11553 11552 sata_log(sata_hba_inst, CE_WARN,
11554 11553 "SATA port multiplier detected at port %d", cport);
11555 11554
11556 11555 /*
11557 11556 * Mark all the port multiplier port behind the port
11558 11557 * multiplier behind with link events, so that the sata daemon
11559 11558 * will update their status.
11560 11559 */
11561 11560 pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET;
11562 11561 mutex_exit(&cportinfo->cport_mutex);
11563 11562
11564 11563 return (SATA_SUCCESS);
11565 11564 }
11566 11565
11567 11566 /*
11568 11567 * Re-probe a port multiplier port, check for a device and attach info
11569 11568 * structures when necessary. Identify Device data is fetched, if possible.
11570 11569 * Assumption: sata address is already validated as port multiplier port.
11571 11570 * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
11572 11571 * the presence of a device and its type.
11573 11572 *
11574 11573 * flag arg specifies that the function should try multiple times to identify
11575 11574 * device type and to initialize it, or it should return immediately on failure.
11576 11575 * SATA_DEV_IDENTIFY_RETRY - retry
11577 11576 * SATA_DEV_IDENTIFY_NORETRY - no retry
11578 11577 *
11579 11578 * SATA_FAILURE is returned if one of the operations failed.
11580 11579 *
11581 11580 * This function cannot be called in interrupt context - it may sleep.
11582 11581 *
11583 11582 * NOTE: Should be only called by sata_probe_port() in case target port is a
11584 11583 * port multiplier port.
11585 11584 * NOTE: No Mutex should be hold.
11586 11585 */
11587 11586 static int
11588 11587 sata_reprobe_pmport(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11589 11588 int flag)
11590 11589 {
11591 11590 sata_cport_info_t *cportinfo = NULL;
11592 11591 sata_pmport_info_t *pmportinfo = NULL;
11593 11592 sata_drive_info_t *sdinfo, *osdinfo;
11594 11593 sata_device_t sdevice;
11595 11594 boolean_t init_device = B_FALSE;
11596 11595 int prev_device_type = SATA_DTYPE_NONE;
11597 11596 int prev_device_settings = 0;
11598 11597 int prev_device_state = 0;
11599 11598 clock_t start_time;
11600 11599 uint8_t cport = sata_device->satadev_addr.cport;
11601 11600 uint8_t pmport = sata_device->satadev_addr.pmport;
11602 11601 int rval;
11603 11602
11604 11603 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11605 11604 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11606 11605 osdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11607 11606
11608 11607 if (osdinfo != NULL) {
11609 11608 /*
11610 11609 * We are re-probing port with a previously attached device.
11611 11610 * Save previous device type and settings.
11612 11611 */
11613 11612 prev_device_type = pmportinfo->pmport_dev_type;
11614 11613 prev_device_settings = osdinfo->satadrv_settings;
11615 11614 prev_device_state = osdinfo->satadrv_state;
11616 11615 }
11617 11616
11618 11617 start_time = ddi_get_lbolt();
11619 11618
11620 11619 /* check parent status */
11621 11620 mutex_enter(&cportinfo->cport_mutex);
11622 11621 if ((cportinfo->cport_state &
11623 11622 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11624 11623 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11625 11624 SATA_PORT_DEVLINK_UP) {
11626 11625 mutex_exit(&cportinfo->cport_mutex);
11627 11626 return (SATA_FAILURE);
11628 11627 }
11629 11628 mutex_exit(&cportinfo->cport_mutex);
11630 11629
11631 11630 retry_probe_pmport:
11632 11631
11633 11632 /* probe port */
11634 11633 mutex_enter(&pmportinfo->pmport_mutex);
11635 11634 pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11636 11635 pmportinfo->pmport_state |= SATA_STATE_PROBING;
11637 11636 mutex_exit(&pmportinfo->pmport_mutex);
11638 11637
11639 11638 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11640 11639 (SATA_DIP(sata_hba_inst), sata_device);
11641 11640
11642 11641 /* might need retry because we cannot touch registers. */
11643 11642 if (rval == SATA_FAILURE) {
11644 11643 mutex_enter(&pmportinfo->pmport_mutex);
11645 11644 pmportinfo->pmport_state = SATA_PSTATE_FAILED;
11646 11645 mutex_exit(&pmportinfo->pmport_mutex);
11647 11646 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: "
11648 11647 "SATA port %d:%d probing failed",
11649 11648 cport, pmport));
11650 11649 return (SATA_FAILURE);
11651 11650 } else if (rval == SATA_RETRY) {
11652 11651 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: "
11653 11652 "SATA port %d:%d probing failed, retrying...",
11654 11653 cport, pmport));
11655 11654 clock_t cur_time = ddi_get_lbolt();
11656 11655 /*
11657 11656 * A device was not successfully identified or initialized.
11658 11657 * Track retry time for device identification.
11659 11658 */
11660 11659 if ((cur_time - start_time) <
11661 11660 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11662 11661 /* sleep for a while */
11663 11662 delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11664 11663 goto retry_probe_pmport;
11665 11664 } else {
11666 11665 mutex_enter(&pmportinfo->pmport_mutex);
11667 11666 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL)
11668 11667 SATA_PMPORTINFO_DRV_INFO(pmportinfo)->
11669 11668 satadrv_state = SATA_DSTATE_FAILED;
11670 11669 mutex_exit(&pmportinfo->pmport_mutex);
11671 11670 return (SATA_SUCCESS);
11672 11671 }
11673 11672 }
11674 11673
11675 11674 /*
11676 11675 * Sanity check - Controller port is active? Is the link active?
11677 11676 * Is it still a port multiplier?
11678 11677 */
11679 11678 if ((cportinfo->cport_state &
11680 11679 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11681 11680 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11682 11681 SATA_PORT_DEVLINK_UP ||
11683 11682 (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) {
11684 11683 /*
11685 11684 * Port in non-usable state or no link active/no
11686 11685 * device. Free info structure.
11687 11686 */
11688 11687 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11689 11688
11690 11689 sdevice.satadev_addr.cport = cport;
11691 11690 sdevice.satadev_addr.pmport = pmport;
11692 11691 sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
11693 11692 mutex_exit(&cportinfo->cport_mutex);
11694 11693
11695 11694 sata_free_pmult(sata_hba_inst, &sdevice);
11696 11695 return (SATA_FAILURE);
11697 11696 }
11698 11697
11699 11698 /* SATA_SUCCESS NOW */
11700 11699 /*
11701 11700 * update sata port state and set device type
11702 11701 */
11703 11702 mutex_enter(&pmportinfo->pmport_mutex);
11704 11703 sata_update_pmport_info(sata_hba_inst, sata_device);
11705 11704 pmportinfo->pmport_state &= ~SATA_STATE_PROBING;
11706 11705
11707 11706 /*
11708 11707 * Sanity check - Port is active? Is the link active?
11709 11708 * Is there any device attached?
11710 11709 */
11711 11710 if ((pmportinfo->pmport_state &
11712 11711 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11713 11712 (pmportinfo->pmport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11714 11713 SATA_PORT_DEVLINK_UP) {
11715 11714 /*
11716 11715 * Port in non-usable state or no link active/no device.
11717 11716 * Free info structure if necessary (direct attached drive
11718 11717 * only, for now!
11719 11718 */
11720 11719 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11721 11720 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11722 11721 /* Add here differentiation for device attached or not */
11723 11722 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11724 11723 mutex_exit(&pmportinfo->pmport_mutex);
11725 11724 if (sdinfo != NULL)
11726 11725 kmem_free(sdinfo, sizeof (sata_drive_info_t));
11727 11726 return (SATA_SUCCESS);
11728 11727 }
11729 11728
11730 11729 pmportinfo->pmport_state |= SATA_STATE_READY;
11731 11730 pmportinfo->pmport_dev_type = sata_device->satadev_type;
11732 11731 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11733 11732
11734 11733 /*
11735 11734 * If we are re-probing the port, there may be
11736 11735 * sata_drive_info structure attached
11737 11736 * (or sata_pm_info, if PMult is supported).
11738 11737 */
11739 11738 if (sata_device->satadev_type == SATA_DTYPE_NONE) {
11740 11739 /*
11741 11740 * There is no device, so remove device info structure,
11742 11741 * if necessary.
11743 11742 */
11744 11743 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11745 11744 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11746 11745 if (sdinfo != NULL) {
11747 11746 kmem_free(sdinfo, sizeof (sata_drive_info_t));
11748 11747 sata_log(sata_hba_inst, CE_WARN,
11749 11748 "SATA device detached from port %d:%d",
11750 11749 cport, pmport);
11751 11750 }
11752 11751 mutex_exit(&pmportinfo->pmport_mutex);
11753 11752 return (SATA_SUCCESS);
11754 11753 }
11755 11754
11756 11755 /* this should not be a pmult */
11757 11756 ASSERT(sata_device->satadev_type != SATA_DTYPE_PMULT);
11758 11757 if (sdinfo == NULL) {
11759 11758 /*
11760 11759 * There is some device attached, but there is
11761 11760 * no sata_drive_info structure - allocate one
11762 11761 */
11763 11762 mutex_exit(&pmportinfo->pmport_mutex);
11764 11763 sdinfo = kmem_zalloc(sizeof (sata_drive_info_t),
11765 11764 KM_SLEEP);
11766 11765 mutex_enter(&pmportinfo->pmport_mutex);
11767 11766 /*
11768 11767 * Recheck, that the port state did not change when we
11769 11768 * released mutex.
11770 11769 */
11771 11770 if (pmportinfo->pmport_state & SATA_STATE_READY) {
11772 11771 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = sdinfo;
11773 11772 sdinfo->satadrv_addr = pmportinfo->pmport_addr;
11774 11773 sdinfo->satadrv_addr.qual = SATA_ADDR_DPMPORT;
11775 11774 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11776 11775 sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
11777 11776 } else {
11778 11777 /*
11779 11778 * Port is not in ready state, we
11780 11779 * cannot attach a device.
11781 11780 */
11782 11781 mutex_exit(&pmportinfo->pmport_mutex);
11783 11782 kmem_free(sdinfo, sizeof (sata_drive_info_t));
11784 11783 return (SATA_SUCCESS);
11785 11784 }
11786 11785 /*
11787 11786 * Since we are adding device, presumably new one,
11788 11787 * indicate that it should be initalized,
11789 11788 * as well as some internal framework states).
11790 11789 */
11791 11790 init_device = B_TRUE;
11792 11791 }
11793 11792
11794 11793 pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
11795 11794 sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
11796 11795
11797 11796 mutex_exit(&pmportinfo->pmport_mutex);
11798 11797 /*
11799 11798 * Figure out what kind of device we are really
11800 11799 * dealing with.
11801 11800 */
11802 11801 rval = sata_probe_device(sata_hba_inst, sata_device);
11803 11802
11804 11803 mutex_enter(&pmportinfo->pmport_mutex);
11805 11804 if (rval == SATA_SUCCESS) {
11806 11805 /*
11807 11806 * If we are dealing with the same type of a device as before,
11808 11807 * restore its settings flags.
11809 11808 */
11810 11809 if (osdinfo != NULL &&
11811 11810 sata_device->satadev_type == prev_device_type)
11812 11811 sdinfo->satadrv_settings = prev_device_settings;
11813 11812
11814 11813 mutex_exit(&pmportinfo->pmport_mutex);
11815 11814 /* Set initial device features, if necessary */
11816 11815 if (init_device == B_TRUE) {
11817 11816 rval = sata_initialize_device(sata_hba_inst, sdinfo);
11818 11817 }
11819 11818 if (rval == SATA_SUCCESS)
11820 11819 return (rval);
11821 11820 } else {
11822 11821 /*
11823 11822 * If there was some device info before we probe the device,
11824 11823 * restore previous device setting, so we can retry from scratch
11825 11824 * later. Providing, of course, that device has not disappeared
11826 11825 * during probing process.
11827 11826 */
11828 11827 if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11829 11828 if (osdinfo != NULL) {
11830 11829 pmportinfo->pmport_dev_type = prev_device_type;
11831 11830 sdinfo->satadrv_type = prev_device_type;
11832 11831 sdinfo->satadrv_state = prev_device_state;
11833 11832 }
11834 11833 } else {
11835 11834 /* device is gone */
11836 11835 kmem_free(sdinfo, sizeof (sata_drive_info_t));
11837 11836 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11838 11837 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11839 11838 mutex_exit(&pmportinfo->pmport_mutex);
11840 11839 return (SATA_SUCCESS);
11841 11840 }
11842 11841 mutex_exit(&pmportinfo->pmport_mutex);
11843 11842 }
11844 11843
11845 11844 if (flag == SATA_DEV_IDENTIFY_RETRY) {
11846 11845 clock_t cur_time = ddi_get_lbolt();
11847 11846 /*
11848 11847 * A device was not successfully identified or initialized.
11849 11848 * Track retry time for device identification.
11850 11849 */
11851 11850 if ((cur_time - start_time) <
11852 11851 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11853 11852 /* sleep for a while */
11854 11853 delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11855 11854 goto retry_probe_pmport;
11856 11855 } else {
11857 11856 mutex_enter(&pmportinfo->pmport_mutex);
11858 11857 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL)
11859 11858 SATA_PMPORTINFO_DRV_INFO(pmportinfo)->
11860 11859 satadrv_state = SATA_DSTATE_FAILED;
11861 11860 mutex_exit(&pmportinfo->pmport_mutex);
11862 11861 }
11863 11862 }
11864 11863 return (SATA_SUCCESS);
11865 11864 }
11866 11865
11867 11866 /*
11868 11867 * Allocated related structure for a port multiplier and its device ports
11869 11868 *
11870 11869 * Port multiplier should be ready and probed, and related information like
11871 11870 * the number of the device ports should be store in sata_device_t.
11872 11871 *
11873 11872 * NOTE: No Mutex should be hold.
11874 11873 */
11875 11874 static int
11876 11875 sata_alloc_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
11877 11876 {
11878 11877 dev_info_t *dip = SATA_DIP(sata_hba_inst);
11879 11878 sata_cport_info_t *cportinfo = NULL;
11880 11879 sata_pmult_info_t *pmultinfo = NULL;
11881 11880 sata_pmport_info_t *pmportinfo = NULL;
11882 11881 sata_device_t sd;
11883 11882 dev_t minor_number;
11884 11883 char name[16];
11885 11884 uint8_t cport = sata_device->satadev_addr.cport;
11886 11885 int rval;
11887 11886 int npmport;
11888 11887
11889 11888 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11890 11889
11891 11890 /* This function might be called while a port-mult is hot-plugged. */
11892 11891 mutex_enter(&cportinfo->cport_mutex);
11893 11892
11894 11893 /* dev_type's not updated when get called from sata_reprobe_port() */
11895 11894 if (SATA_CPORTINFO_PMULT_INFO(cportinfo) == NULL) {
11896 11895 /* Create a pmult_info structure */
11897 11896 SATA_CPORTINFO_PMULT_INFO(cportinfo) =
11898 11897 kmem_zalloc(sizeof (sata_pmult_info_t), KM_SLEEP);
11899 11898 }
11900 11899 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
11901 11900
11902 11901 pmultinfo->pmult_addr = sata_device->satadev_addr;
11903 11902 pmultinfo->pmult_addr.qual = SATA_ADDR_PMULT;
11904 11903 pmultinfo->pmult_state = SATA_STATE_PROBING;
11905 11904
11906 11905 /*
11907 11906 * Probe the port multiplier with qualifier SATA_ADDR_PMULT_SPEC,
11908 11907 * The HBA driver should initialize and register the port multiplier,
11909 11908 * sata_register_pmult() will fill following fields,
11910 11909 * + sata_pmult_info.pmult_gscr
11911 11910 * + sata_pmult_info.pmult_num_dev_ports
11912 11911 */
11913 11912 sd.satadev_addr = sata_device->satadev_addr;
11914 11913 sd.satadev_addr.qual = SATA_ADDR_PMULT_SPEC;
11915 11914 mutex_exit(&cportinfo->cport_mutex);
11916 11915 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11917 11916 (SATA_DIP(sata_hba_inst), &sd);
11918 11917 mutex_enter(&cportinfo->cport_mutex);
11919 11918
11920 11919 if (rval != SATA_SUCCESS ||
11921 11920 (sd.satadev_type != SATA_DTYPE_PMULT) ||
11922 11921 !(sd.satadev_state & SATA_DSTATE_PMULT_INIT)) {
11923 11922 SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL;
11924 11923 kmem_free(pmultinfo, sizeof (sata_pmult_info_t));
11925 11924 cportinfo->cport_state = SATA_PSTATE_FAILED;
11926 11925 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11927 11926 mutex_exit(&cportinfo->cport_mutex);
11928 11927 SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
11929 11928 "sata_alloc_pmult: failed to initialize pmult "
11930 11929 "at port %d.", cport)
11931 11930 return (SATA_FAILURE);
11932 11931 }
11933 11932
11934 11933 /* Initialize pmport_info structure */
11935 11934 for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports;
11936 11935 npmport++) {
11937 11936
11938 11937 /* if everything is allocated, skip */
11939 11938 if (SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) != NULL)
11940 11939 continue;
11941 11940
11942 11941 pmportinfo = kmem_zalloc(sizeof (sata_pmport_info_t), KM_SLEEP);
11943 11942 mutex_init(&pmportinfo->pmport_mutex, NULL, MUTEX_DRIVER, NULL);
11944 11943 mutex_exit(&cportinfo->cport_mutex);
11945 11944
11946 11945 mutex_enter(&pmportinfo->pmport_mutex);
11947 11946 pmportinfo->pmport_addr.cport = cport;
11948 11947 pmportinfo->pmport_addr.pmport = (uint8_t)npmport;
11949 11948 pmportinfo->pmport_addr.qual = SATA_ADDR_PMPORT;
11950 11949 pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11951 11950 mutex_exit(&pmportinfo->pmport_mutex);
11952 11951
11953 11952 mutex_enter(&cportinfo->cport_mutex);
11954 11953 SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) = pmportinfo;
11955 11954
11956 11955 /* Create an attachment point */
11957 11956 minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip),
11958 11957 cport, (uint8_t)npmport, SATA_ADDR_PMPORT);
11959 11958 (void) sprintf(name, "%d.%d", cport, npmport);
11960 11959
11961 11960 if (ddi_create_minor_node(dip, name, S_IFCHR, minor_number,
11962 11961 DDI_NT_SATA_ATTACHMENT_POINT, 0) != DDI_SUCCESS) {
11963 11962 sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
11964 11963 "cannot create SATA attachment point for "
11965 11964 "port %d:%d", cport, npmport);
11966 11965 }
11967 11966 }
11968 11967
11969 11968 pmultinfo->pmult_state &= ~SATA_STATE_PROBING;
11970 11969 pmultinfo->pmult_state |= (SATA_STATE_PROBED|SATA_STATE_READY);
11971 11970 cportinfo->cport_dev_type = SATA_DTYPE_PMULT;
11972 11971
11973 11972 mutex_exit(&cportinfo->cport_mutex);
11974 11973 return (SATA_SUCCESS);
11975 11974 }
11976 11975
11977 11976 /*
11978 11977 * Free data structures when a port multiplier is removed.
11979 11978 *
11980 11979 * NOTE: No Mutex should be hold.
11981 11980 */
11982 11981 static void
11983 11982 sata_free_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
11984 11983 {
11985 11984 sata_cport_info_t *cportinfo;
11986 11985 sata_pmult_info_t *pmultinfo;
11987 11986 sata_pmport_info_t *pmportinfo;
11988 11987 sata_device_t pmport_device;
11989 11988 sata_drive_info_t *sdinfo;
11990 11989 dev_info_t *tdip;
11991 11990 char name[16];
11992 11991 uint8_t cport = sata_device->satadev_addr.cport;
11993 11992 int npmport;
11994 11993
11995 11994 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11996 11995
11997 11996 /* This function might be called while port-mult is hot plugged. */
11998 11997 mutex_enter(&cportinfo->cport_mutex);
11999 11998
12000 11999 cportinfo->cport_dev_type = SATA_DTYPE_NONE;
12001 12000 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
12002 12001 ASSERT(pmultinfo != NULL);
12003 12002
12004 12003 /* Free pmport_info structure */
12005 12004 for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports;
12006 12005 npmport++) {
12007 12006 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport);
12008 12007 if (pmportinfo == NULL)
12009 12008 continue;
12010 12009 mutex_exit(&cportinfo->cport_mutex);
12011 12010
12012 12011 mutex_enter(&pmportinfo->pmport_mutex);
12013 12012 sdinfo = pmportinfo->pmport_sata_drive;
12014 12013 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
12015 12014 mutex_exit(&pmportinfo->pmport_mutex);
12016 12015
12017 12016 /* Remove attachment point. */
12018 12017 name[0] = '\0';
12019 12018 (void) sprintf(name, "%d.%d", cport, npmport);
12020 12019 ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name);
12021 12020 sata_log(sata_hba_inst, CE_NOTE,
12022 12021 "Remove attachment point of port %d:%d",
12023 12022 cport, npmport);
12024 12023
12025 12024 /*
12026 12025 * Rumove target node
12027 12026 */
12028 12027 bzero(&pmport_device, sizeof (sata_device_t));
12029 12028 pmport_device.satadev_rev = SATA_DEVICE_REV;
12030 12029 pmport_device.satadev_addr.cport = cport;
12031 12030 pmport_device.satadev_addr.pmport = (uint8_t)npmport;
12032 12031 pmport_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
12033 12032
12034 12033 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
12035 12034 &(pmport_device.satadev_addr));
12036 12035 if (tdip != NULL && ndi_devi_offline(tdip,
12037 12036 NDI_DEVI_REMOVE) != NDI_SUCCESS) {
12038 12037 /*
12039 12038 * Problem :
12040 12039 * The target node remained attached.
12041 12040 * This happens when the device file was open
12042 12041 * or a node was waiting for resources.
12043 12042 * Cannot do anything about it.
12044 12043 */
12045 12044 SATA_LOG_D((sata_hba_inst, CE_WARN,
12046 12045 "sata_free_pmult: could not unconfigure device "
12047 12046 "before disconnecting the SATA port %d:%d",
12048 12047 cport, npmport));
12049 12048
12050 12049 /*
12051 12050 * Set DEVICE REMOVED state in the target
12052 12051 * node. It will prevent access to the device
12053 12052 * even when a new device is attached, until
12054 12053 * the old target node is released, removed and
12055 12054 * recreated for a new device.
12056 12055 */
12057 12056 sata_set_device_removed(tdip);
12058 12057
12059 12058 /*
12060 12059 * Instruct event daemon to try the target
12061 12060 * node cleanup later.
12062 12061 */
12063 12062 sata_set_target_node_cleanup(
12064 12063 sata_hba_inst, &(pmport_device.satadev_addr));
12065 12064
12066 12065 }
12067 12066 mutex_enter(&cportinfo->cport_mutex);
12068 12067
12069 12068 /*
12070 12069 * Add here differentiation for device attached or not
12071 12070 */
12072 12071 if (sdinfo != NULL) {
12073 12072 sata_log(sata_hba_inst, CE_WARN,
12074 12073 "SATA device detached from port %d:%d",
12075 12074 cport, npmport);
12076 12075 kmem_free(sdinfo, sizeof (sata_drive_info_t));
12077 12076 }
12078 12077
12079 12078 mutex_destroy(&pmportinfo->pmport_mutex);
12080 12079 kmem_free(pmportinfo, sizeof (sata_pmport_info_t));
12081 12080 }
12082 12081
12083 12082 kmem_free(pmultinfo, sizeof (sata_pmult_info_t));
12084 12083
12085 12084 cportinfo->cport_devp.cport_sata_pmult = NULL;
12086 12085
12087 12086 sata_log(sata_hba_inst, CE_WARN,
12088 12087 "SATA port multiplier detached at port %d", cport);
12089 12088
12090 12089 mutex_exit(&cportinfo->cport_mutex);
12091 12090 }
12092 12091
12093 12092 /*
12094 12093 * Initialize device
12095 12094 * Specified device is initialized to a default state.
12096 12095 *
12097 12096 * Returns SATA_SUCCESS if all device features are set successfully,
12098 12097 * SATA_RETRY if device is accessible but device features were not set
12099 12098 * successfully, and SATA_FAILURE otherwise.
12100 12099 */
12101 12100 static int
12102 12101 sata_initialize_device(sata_hba_inst_t *sata_hba_inst,
12103 12102 sata_drive_info_t *sdinfo)
12104 12103 {
12105 12104 int rval;
12106 12105
12107 12106 sata_save_drive_settings(sdinfo);
12108 12107
12109 12108 sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
12110 12109
12111 12110 sata_init_write_cache_mode(sdinfo);
12112 12111
12113 12112 rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0);
12114 12113
12115 12114 /* Determine current data transfer mode */
12116 12115 if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) {
12117 12116 sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
12118 12117 } else if ((sdinfo->satadrv_id.ai_validinfo &
12119 12118 SATA_VALIDINFO_88) != 0 &&
12120 12119 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) {
12121 12120 sdinfo->satadrv_settings |= SATA_DEV_DMA;
12122 12121 } else if ((sdinfo->satadrv_id.ai_dworddma &
12123 12122 SATA_MDMA_SEL_MASK) != 0) {
12124 12123 sdinfo->satadrv_settings |= SATA_DEV_DMA;
12125 12124 } else
12126 12125 /* DMA supported, not no DMA transfer mode is selected !? */
12127 12126 sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
12128 12127
12129 12128 if ((sdinfo->satadrv_id.ai_cmdset83 & 0x20) &&
12130 12129 (sdinfo->satadrv_id.ai_features86 & 0x20))
12131 12130 sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
12132 12131 else
12133 12132 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
12134 12133
12135 12134 return (rval);
12136 12135 }
12137 12136
12138 12137
12139 12138 /*
12140 12139 * Initialize write cache mode.
12141 12140 *
12142 12141 * The default write cache setting for SATA HDD is provided by sata_write_cache
12143 12142 * static variable. ATAPI CD/DVDs devices have write cache default is
12144 12143 * determined by sata_atapicdvd_write_cache static variable.
12145 12144 * ATAPI tape devices have write cache default is determined by
12146 12145 * sata_atapitape_write_cache static variable.
12147 12146 * ATAPI disk devices have write cache default is determined by
12148 12147 * sata_atapidisk_write_cache static variable.
12149 12148 * 1 - enable
12150 12149 * 0 - disable
12151 12150 * any other value - current drive setting
12152 12151 *
12153 12152 * Although there is not reason to disable write cache on CD/DVD devices,
12154 12153 * tape devices and ATAPI disk devices, the default setting control is provided
12155 12154 * for the maximun flexibility.
12156 12155 *
12157 12156 * In the future, it may be overridden by the
12158 12157 * disk-write-cache-enable property setting, if it is defined.
12159 12158 * Returns SATA_SUCCESS if all device features are set successfully,
12160 12159 * SATA_FAILURE otherwise.
12161 12160 */
12162 12161 static void
12163 12162 sata_init_write_cache_mode(sata_drive_info_t *sdinfo)
12164 12163 {
12165 12164 switch (sdinfo->satadrv_type) {
12166 12165 case SATA_DTYPE_ATADISK:
12167 12166 if (sata_write_cache == 1)
12168 12167 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12169 12168 else if (sata_write_cache == 0)
12170 12169 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12171 12170 /*
12172 12171 * When sata_write_cache value is not 0 or 1,
12173 12172 * a current setting of the drive's write cache is used.
12174 12173 */
12175 12174 break;
12176 12175 case SATA_DTYPE_ATAPICD:
12177 12176 if (sata_atapicdvd_write_cache == 1)
12178 12177 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12179 12178 else if (sata_atapicdvd_write_cache == 0)
12180 12179 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12181 12180 /*
12182 12181 * When sata_atapicdvd_write_cache value is not 0 or 1,
12183 12182 * a current setting of the drive's write cache is used.
12184 12183 */
12185 12184 break;
12186 12185 case SATA_DTYPE_ATAPITAPE:
12187 12186 if (sata_atapitape_write_cache == 1)
12188 12187 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12189 12188 else if (sata_atapitape_write_cache == 0)
12190 12189 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12191 12190 /*
12192 12191 * When sata_atapitape_write_cache value is not 0 or 1,
12193 12192 * a current setting of the drive's write cache is used.
12194 12193 */
12195 12194 break;
12196 12195 case SATA_DTYPE_ATAPIDISK:
12197 12196 if (sata_atapidisk_write_cache == 1)
12198 12197 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12199 12198 else if (sata_atapidisk_write_cache == 0)
12200 12199 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12201 12200 /*
12202 12201 * When sata_atapidisk_write_cache value is not 0 or 1,
12203 12202 * a current setting of the drive's write cache is used.
12204 12203 */
12205 12204 break;
12206 12205 }
12207 12206 }
12208 12207
12209 12208
12210 12209 /*
12211 12210 * Validate sata address.
12212 12211 * Specified cport, pmport and qualifier has to match
12213 12212 * passed sata_scsi configuration info.
12214 12213 * The presence of an attached device is not verified.
12215 12214 *
12216 12215 * Returns 0 when address is valid, -1 otherwise.
12217 12216 */
12218 12217 static int
12219 12218 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport,
12220 12219 int pmport, int qual)
12221 12220 {
12222 12221 if (qual == SATA_ADDR_DCPORT && pmport != 0)
12223 12222 goto invalid_address;
12224 12223 if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
12225 12224 goto invalid_address;
12226 12225 if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) &&
12227 12226 ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) ||
12228 12227 (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) ||
12229 12228 (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport))))
12230 12229 goto invalid_address;
12231 12230
12232 12231 return (0);
12233 12232
12234 12233 invalid_address:
12235 12234 return (-1);
12236 12235
12237 12236 }
12238 12237
12239 12238 /*
12240 12239 * Validate scsi address
12241 12240 * SCSI target address is translated into SATA cport/pmport and compared
12242 12241 * with a controller port/device configuration. LUN has to be 0.
12243 12242 * Returns 0 if a scsi target refers to an attached device,
12244 12243 * returns 1 if address is valid but no valid device is attached,
12245 12244 * returns 2 if address is valid but device type is unknown (not valid device),
12246 12245 * returns -1 if bad address or device is of an unsupported type.
12247 12246 * Upon return sata_device argument is set.
12248 12247 *
12249 12248 * Port multiplier is supported now.
12250 12249 */
12251 12250 static int
12252 12251 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst,
12253 12252 struct scsi_address *ap, sata_device_t *sata_device)
12254 12253 {
12255 12254 int cport, pmport, qual, rval;
12256 12255
12257 12256 rval = -1; /* Invalid address */
12258 12257 if (ap->a_lun != 0)
12259 12258 goto out;
12260 12259
12261 12260 qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
12262 12261 cport = SCSI_TO_SATA_CPORT(ap->a_target);
12263 12262 pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
12264 12263
12265 12264 if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT)
12266 12265 goto out;
12267 12266
12268 12267 if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) ==
12269 12268 0) {
12270 12269
12271 12270 sata_cport_info_t *cportinfo;
12272 12271 sata_pmult_info_t *pmultinfo;
12273 12272 sata_drive_info_t *sdinfo = NULL;
12274 12273
12275 12274 sata_device->satadev_addr.qual = qual;
12276 12275 sata_device->satadev_addr.cport = cport;
12277 12276 sata_device->satadev_addr.pmport = pmport;
12278 12277 sata_device->satadev_rev = SATA_DEVICE_REV_1;
12279 12278
12280 12279 rval = 1; /* Valid sata address */
12281 12280
12282 12281 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
12283 12282 if (qual == SATA_ADDR_DCPORT) {
12284 12283 if (cportinfo == NULL ||
12285 12284 cportinfo->cport_dev_type == SATA_DTYPE_NONE)
12286 12285 goto out;
12287 12286
12288 12287 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
12289 12288 if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN &&
12290 12289 sdinfo != NULL) {
12291 12290 rval = 2;
12292 12291 goto out;
12293 12292 }
12294 12293
12295 12294 if ((cportinfo->cport_dev_type &
12296 12295 SATA_VALID_DEV_TYPE) == 0) {
12297 12296 rval = -1;
12298 12297 goto out;
12299 12298 }
12300 12299
12301 12300 } else if (qual == SATA_ADDR_DPMPORT) {
12302 12301 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
12303 12302 if (pmultinfo == NULL) {
12304 12303 rval = -1;
12305 12304 goto out;
12306 12305 }
12307 12306 if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) ==
12308 12307 NULL ||
12309 12308 SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12310 12309 pmport) == SATA_DTYPE_NONE)
12311 12310 goto out;
12312 12311
12313 12312 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport,
12314 12313 pmport);
12315 12314 if (SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12316 12315 pmport) == SATA_DTYPE_UNKNOWN && sdinfo != NULL) {
12317 12316 rval = 2;
12318 12317 goto out;
12319 12318 }
12320 12319
12321 12320 if ((SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12322 12321 pmport) & SATA_VALID_DEV_TYPE) == 0) {
12323 12322 rval = -1;
12324 12323 goto out;
12325 12324 }
12326 12325
12327 12326 } else {
12328 12327 rval = -1;
12329 12328 goto out;
12330 12329 }
12331 12330 if ((sdinfo == NULL) ||
12332 12331 (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0)
12333 12332 goto out;
12334 12333
12335 12334 sata_device->satadev_type = sdinfo->satadrv_type;
12336 12335
12337 12336 return (0);
12338 12337 }
12339 12338 out:
12340 12339 if (rval > 0) {
12341 12340 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
12342 12341 "sata_validate_scsi_address: no valid target %x lun %x",
12343 12342 ap->a_target, ap->a_lun);
12344 12343 }
12345 12344 return (rval);
12346 12345 }
12347 12346
12348 12347 /*
12349 12348 * Find dip corresponding to passed device number
12350 12349 *
12351 12350 * Returns NULL if invalid device number is passed or device cannot be found,
12352 12351 * Returns dip is device is found.
12353 12352 */
12354 12353 static dev_info_t *
12355 12354 sata_devt_to_devinfo(dev_t dev)
12356 12355 {
12357 12356 dev_info_t *dip;
12358 12357 #ifndef __lock_lint
12359 12358 struct devnames *dnp;
12360 12359 major_t major = getmajor(dev);
12361 12360 int instance = SATA_MINOR2INSTANCE(getminor(dev));
12362 12361
12363 12362 if (major >= devcnt)
12364 12363 return (NULL);
12365 12364
12366 12365 dnp = &devnamesp[major];
12367 12366 LOCK_DEV_OPS(&(dnp->dn_lock));
12368 12367 dip = dnp->dn_head;
12369 12368 while (dip && (ddi_get_instance(dip) != instance)) {
12370 12369 dip = ddi_get_next(dip);
12371 12370 }
12372 12371 UNLOCK_DEV_OPS(&(dnp->dn_lock));
12373 12372 #endif
12374 12373
12375 12374 return (dip);
12376 12375 }
12377 12376
12378 12377
12379 12378 /*
12380 12379 * Probe device.
12381 12380 * This function issues Identify Device command and initializes local
12382 12381 * sata_drive_info structure if the device can be identified.
12383 12382 * The device type is determined by examining Identify Device
12384 12383 * command response.
12385 12384 * If the sata_hba_inst has linked drive info structure for this
12386 12385 * device address, the Identify Device data is stored into sata_drive_info
12387 12386 * structure linked to the port info structure.
12388 12387 *
12389 12388 * sata_device has to refer to the valid sata port(s) for HBA described
12390 12389 * by sata_hba_inst structure.
12391 12390 *
12392 12391 * Returns:
12393 12392 * SATA_SUCCESS if device type was successfully probed and port-linked
12394 12393 * drive info structure was updated;
12395 12394 * SATA_FAILURE if there is no device, or device was not probed
12396 12395 * successully;
12397 12396 * SATA_RETRY if device probe can be retried later.
12398 12397 * If a device cannot be identified, sata_device's dev_state and dev_type
12399 12398 * fields are set to unknown.
12400 12399 * There are no retries in this function. Any retries should be managed by
12401 12400 * the caller.
12402 12401 */
12403 12402
12404 12403
12405 12404 static int
12406 12405 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
12407 12406 {
12408 12407 sata_pmport_info_t *pmportinfo;
12409 12408 sata_drive_info_t *sdinfo;
12410 12409 sata_drive_info_t new_sdinfo; /* local drive info struct */
12411 12410 int rval;
12412 12411
12413 12412 ASSERT((SATA_CPORT_STATE(sata_hba_inst,
12414 12413 sata_device->satadev_addr.cport) &
12415 12414 (SATA_STATE_PROBED | SATA_STATE_READY)) != 0);
12416 12415
12417 12416 sata_device->satadev_type = SATA_DTYPE_NONE;
12418 12417
12419 12418 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12420 12419 sata_device->satadev_addr.cport)));
12421 12420
12422 12421 if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT) {
12423 12422 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
12424 12423 sata_device->satadev_addr.cport,
12425 12424 sata_device->satadev_addr.pmport);
12426 12425 ASSERT(pmportinfo != NULL);
12427 12426 }
12428 12427
12429 12428 /* Get pointer to port-linked sata device info structure */
12430 12429 sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12431 12430 if (sdinfo != NULL) {
12432 12431 sdinfo->satadrv_state &=
12433 12432 ~(SATA_STATE_PROBED | SATA_STATE_READY);
12434 12433 sdinfo->satadrv_state |= SATA_STATE_PROBING;
12435 12434 } else {
12436 12435 /* No device to probe */
12437 12436 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12438 12437 sata_device->satadev_addr.cport)));
12439 12438 sata_device->satadev_type = SATA_DTYPE_NONE;
12440 12439 sata_device->satadev_state = SATA_STATE_UNKNOWN;
12441 12440 return (SATA_FAILURE);
12442 12441 }
12443 12442 /*
12444 12443 * Need to issue both types of identify device command and
12445 12444 * determine device type by examining retreived data/status.
12446 12445 * First, ATA Identify Device.
12447 12446 */
12448 12447 bzero(&new_sdinfo, sizeof (sata_drive_info_t));
12449 12448 new_sdinfo.satadrv_addr = sata_device->satadev_addr;
12450 12449 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12451 12450 sata_device->satadev_addr.cport)));
12452 12451 new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
12453 12452 rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
12454 12453 if (rval == SATA_RETRY) {
12455 12454 /* We may try to check for ATAPI device */
12456 12455 if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) {
12457 12456 /*
12458 12457 * HBA supports ATAPI - try to issue Identify Packet
12459 12458 * Device command.
12460 12459 */
12461 12460 new_sdinfo.satadrv_type = SATA_DTYPE_ATAPI;
12462 12461 rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
12463 12462 }
12464 12463 }
12465 12464 if (rval == SATA_SUCCESS) {
12466 12465 /*
12467 12466 * Got something responding positively to ATA Identify Device
12468 12467 * or to Identify Packet Device cmd.
12469 12468 * Save last used device type.
12470 12469 */
12471 12470 sata_device->satadev_type = new_sdinfo.satadrv_type;
12472 12471
12473 12472 /* save device info, if possible */
12474 12473 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12475 12474 sata_device->satadev_addr.cport)));
12476 12475 sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12477 12476 if (sdinfo == NULL) {
12478 12477 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12479 12478 sata_device->satadev_addr.cport)));
12480 12479 return (SATA_FAILURE);
12481 12480 }
12482 12481 /*
12483 12482 * Copy drive info into the port-linked drive info structure.
12484 12483 */
12485 12484 *sdinfo = new_sdinfo;
12486 12485 sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
12487 12486 sdinfo->satadrv_state |= SATA_STATE_PROBED;
12488 12487 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
12489 12488 SATA_CPORT_DEV_TYPE(sata_hba_inst,
12490 12489 sata_device->satadev_addr.cport) =
12491 12490 sdinfo->satadrv_type;
12492 12491 else { /* SATA_ADDR_DPMPORT */
12493 12492 mutex_enter(&pmportinfo->pmport_mutex);
12494 12493 SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12495 12494 sata_device->satadev_addr.cport,
12496 12495 sata_device->satadev_addr.pmport) =
12497 12496 sdinfo->satadrv_type;
12498 12497 mutex_exit(&pmportinfo->pmport_mutex);
12499 12498 }
12500 12499 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12501 12500 sata_device->satadev_addr.cport)));
12502 12501 return (SATA_SUCCESS);
12503 12502 }
12504 12503
12505 12504 /*
12506 12505 * It may be SATA_RETRY or SATA_FAILURE return.
12507 12506 * Looks like we cannot determine the device type at this time.
12508 12507 */
12509 12508 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12510 12509 sata_device->satadev_addr.cport)));
12511 12510 sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12512 12511 if (sdinfo != NULL) {
12513 12512 sata_device->satadev_type = SATA_DTYPE_UNKNOWN;
12514 12513 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12515 12514 sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
12516 12515 sdinfo->satadrv_state |= SATA_STATE_PROBED;
12517 12516 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
12518 12517 SATA_CPORT_DEV_TYPE(sata_hba_inst,
12519 12518 sata_device->satadev_addr.cport) =
12520 12519 SATA_DTYPE_UNKNOWN;
12521 12520 else {
12522 12521 /* SATA_ADDR_DPMPORT */
12523 12522 mutex_enter(&pmportinfo->pmport_mutex);
12524 12523 if ((SATA_PMULT_INFO(sata_hba_inst,
12525 12524 sata_device->satadev_addr.cport) != NULL) &&
12526 12525 (SATA_PMPORT_INFO(sata_hba_inst,
12527 12526 sata_device->satadev_addr.cport,
12528 12527 sata_device->satadev_addr.pmport) != NULL))
12529 12528 SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12530 12529 sata_device->satadev_addr.cport,
12531 12530 sata_device->satadev_addr.pmport) =
12532 12531 SATA_DTYPE_UNKNOWN;
12533 12532 mutex_exit(&pmportinfo->pmport_mutex);
12534 12533 }
12535 12534 }
12536 12535 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12537 12536 sata_device->satadev_addr.cport)));
12538 12537 return (rval);
12539 12538 }
12540 12539
12541 12540
12542 12541 /*
12543 12542 * Get pointer to sata_drive_info structure.
12544 12543 *
12545 12544 * The sata_device has to contain address (cport, pmport and qualifier) for
12546 12545 * specified sata_scsi structure.
12547 12546 *
12548 12547 * Returns NULL if device address is not valid for this HBA configuration.
12549 12548 * Otherwise, returns a pointer to sata_drive_info structure.
12550 12549 *
12551 12550 * This function should be called with a port mutex held.
12552 12551 */
12553 12552 static sata_drive_info_t *
12554 12553 sata_get_device_info(sata_hba_inst_t *sata_hba_inst,
12555 12554 sata_device_t *sata_device)
12556 12555 {
12557 12556 uint8_t cport = sata_device->satadev_addr.cport;
12558 12557 uint8_t pmport = sata_device->satadev_addr.pmport;
12559 12558 uint8_t qual = sata_device->satadev_addr.qual;
12560 12559
12561 12560 if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
12562 12561 return (NULL);
12563 12562
12564 12563 if (!(SATA_CPORT_STATE(sata_hba_inst, cport) &
12565 12564 (SATA_STATE_PROBED | SATA_STATE_READY)))
12566 12565 /* Port not probed yet */
12567 12566 return (NULL);
12568 12567
12569 12568 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE)
12570 12569 return (NULL);
12571 12570
12572 12571 if (qual == SATA_ADDR_DCPORT) {
12573 12572 /* Request for a device on a controller port */
12574 12573 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
12575 12574 SATA_DTYPE_PMULT)
12576 12575 /* Port multiplier attached */
12577 12576 return (NULL);
12578 12577 return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport));
12579 12578 }
12580 12579 if (qual == SATA_ADDR_DPMPORT) {
12581 12580 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
12582 12581 SATA_DTYPE_PMULT)
12583 12582 return (NULL);
12584 12583
12585 12584 if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport))
12586 12585 return (NULL);
12587 12586
12588 12587 if (!(SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) &
12589 12588 (SATA_STATE_PROBED | SATA_STATE_READY)))
12590 12589 /* Port multiplier port not probed yet */
12591 12590 return (NULL);
12592 12591
12593 12592 return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport));
12594 12593 }
12595 12594
12596 12595 /* we should not get here */
12597 12596 return (NULL);
12598 12597 }
12599 12598
12600 12599
12601 12600 /*
12602 12601 * sata_identify_device.
12603 12602 * Send Identify Device command to SATA HBA driver.
12604 12603 * If command executes successfully, update sata_drive_info structure pointed
12605 12604 * to by sdinfo argument, including Identify Device data.
12606 12605 * If command fails, invalidate data in sata_drive_info.
12607 12606 *
12608 12607 * Cannot be called from interrupt level.
12609 12608 *
12610 12609 * Returns:
12611 12610 * SATA_SUCCESS if the device was identified as a supported device,
12612 12611 * SATA_RETRY if the device was not identified but could be retried,
12613 12612 * SATA_FAILURE if the device was not identified and identify attempt
12614 12613 * should not be retried.
12615 12614 */
12616 12615 static int
12617 12616 sata_identify_device(sata_hba_inst_t *sata_hba_inst,
12618 12617 sata_drive_info_t *sdinfo)
12619 12618 {
12620 12619 uint16_t cfg_word;
12621 12620 int rval;
12622 12621
12623 12622 /* fetch device identify data */
12624 12623 if ((rval = sata_fetch_device_identify_data(sata_hba_inst,
12625 12624 sdinfo)) != SATA_SUCCESS)
12626 12625 goto fail_unknown;
12627 12626
12628 12627 cfg_word = sdinfo->satadrv_id.ai_config;
12629 12628
12630 12629 /* Set the correct device type */
12631 12630 if ((cfg_word & SATA_ATA_TYPE_MASK) == SATA_ATA_TYPE) {
12632 12631 sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
12633 12632 } else if (cfg_word == SATA_CFA_TYPE) {
12634 12633 /* It's a Compact Flash media via CF-to-SATA HDD adapter */
12635 12634 sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
12636 12635 } else if ((cfg_word & SATA_ATAPI_TYPE_MASK) == SATA_ATAPI_TYPE) {
12637 12636 switch (cfg_word & SATA_ATAPI_ID_DEV_TYPE) {
12638 12637 case SATA_ATAPI_CDROM_DEV:
12639 12638 sdinfo->satadrv_type = SATA_DTYPE_ATAPICD;
12640 12639 break;
12641 12640 case SATA_ATAPI_SQACC_DEV:
12642 12641 sdinfo->satadrv_type = SATA_DTYPE_ATAPITAPE;
12643 12642 break;
12644 12643 case SATA_ATAPI_DIRACC_DEV:
12645 12644 sdinfo->satadrv_type = SATA_DTYPE_ATAPIDISK;
12646 12645 break;
12647 12646 case SATA_ATAPI_PROC_DEV:
12648 12647 sdinfo->satadrv_type = SATA_DTYPE_ATAPIPROC;
12649 12648 break;
12650 12649 default:
12651 12650 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12652 12651 }
12653 12652 } else {
12654 12653 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12655 12654 }
12656 12655
12657 12656 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12658 12657 if (sdinfo->satadrv_capacity == 0) {
12659 12658 /* Non-LBA disk. Too bad... */
12660 12659 sata_log(sata_hba_inst, CE_WARN,
12661 12660 "SATA disk device at port %d does not support LBA",
12662 12661 sdinfo->satadrv_addr.cport);
12663 12662 rval = SATA_FAILURE;
12664 12663 goto fail_unknown;
12665 12664 }
12666 12665 }
12667 12666 #if 0
12668 12667 /* Left for historical reason */
12669 12668 /*
12670 12669 * Some initial version of SATA spec indicated that at least
12671 12670 * UDMA mode 4 has to be supported. It is not metioned in
12672 12671 * SerialATA 2.6, so this restriction is removed.
12673 12672 */
12674 12673 /* Check for Ultra DMA modes 6 through 0 being supported */
12675 12674 for (i = 6; i >= 0; --i) {
12676 12675 if (sdinfo->satadrv_id.ai_ultradma & (1 << i))
12677 12676 break;
12678 12677 }
12679 12678
12680 12679 /*
12681 12680 * At least UDMA 4 mode has to be supported. If mode 4 or
12682 12681 * higher are not supported by the device, fail this
12683 12682 * device.
12684 12683 */
12685 12684 if (i < 4) {
12686 12685 /* No required Ultra DMA mode supported */
12687 12686 sata_log(sata_hba_inst, CE_WARN,
12688 12687 "SATA disk device at port %d does not support UDMA "
12689 12688 "mode 4 or higher", sdinfo->satadrv_addr.cport);
12690 12689 SATA_LOG_D((sata_hba_inst, CE_WARN,
12691 12690 "mode 4 or higher required, %d supported", i));
12692 12691 rval = SATA_FAILURE;
12693 12692 goto fail_unknown;
12694 12693 }
12695 12694 #endif
12696 12695
12697 12696 /*
12698 12697 * For Disk devices, if it doesn't support UDMA mode, we would
12699 12698 * like to return failure directly.
12700 12699 */
12701 12700 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
12702 12701 !((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
12703 12702 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0)) {
12704 12703 sata_log(sata_hba_inst, CE_WARN,
12705 12704 "SATA disk device at port %d does not support UDMA",
12706 12705 sdinfo->satadrv_addr.cport);
12707 12706 rval = SATA_FAILURE;
12708 12707 goto fail_unknown;
12709 12708 }
12710 12709
12711 12710 return (SATA_SUCCESS);
12712 12711
12713 12712 fail_unknown:
12714 12713 /* Invalidate sata_drive_info ? */
12715 12714 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12716 12715 sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
12717 12716 return (rval);
12718 12717 }
12719 12718
12720 12719 /*
12721 12720 * Log/display device information
12722 12721 */
12723 12722 static void
12724 12723 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst,
12725 12724 sata_drive_info_t *sdinfo)
12726 12725 {
12727 12726 int valid_version;
12728 12727 char msg_buf[MAXPATHLEN];
12729 12728 int i;
12730 12729
12731 12730 /* Show HBA path */
12732 12731 (void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf);
12733 12732
12734 12733 cmn_err(CE_CONT, "?%s :\n", msg_buf);
12735 12734
12736 12735 switch (sdinfo->satadrv_type) {
12737 12736 case SATA_DTYPE_ATADISK:
12738 12737 (void) sprintf(msg_buf, "SATA disk device at");
12739 12738 break;
12740 12739
12741 12740 case SATA_DTYPE_ATAPICD:
12742 12741 (void) sprintf(msg_buf, "SATA CD/DVD (ATAPI) device at");
12743 12742 break;
12744 12743
12745 12744 case SATA_DTYPE_ATAPITAPE:
12746 12745 (void) sprintf(msg_buf, "SATA tape (ATAPI) device at");
12747 12746 break;
12748 12747
12749 12748 case SATA_DTYPE_ATAPIDISK:
12750 12749 (void) sprintf(msg_buf, "SATA disk (ATAPI) device at");
12751 12750 break;
12752 12751
12753 12752 case SATA_DTYPE_ATAPIPROC:
12754 12753 (void) sprintf(msg_buf, "SATA processor (ATAPI) device at");
12755 12754 break;
12756 12755
12757 12756 case SATA_DTYPE_UNKNOWN:
12758 12757 (void) sprintf(msg_buf,
12759 12758 "Unsupported SATA device type (cfg 0x%x) at ",
12760 12759 sdinfo->satadrv_id.ai_config);
12761 12760 break;
12762 12761 }
12763 12762
12764 12763 if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT)
12765 12764 cmn_err(CE_CONT, "?\t%s port %d\n",
12766 12765 msg_buf, sdinfo->satadrv_addr.cport);
12767 12766 else
12768 12767 cmn_err(CE_CONT, "?\t%s port %d:%d\n",
12769 12768 msg_buf, sdinfo->satadrv_addr.cport,
12770 12769 sdinfo->satadrv_addr.pmport);
12771 12770
12772 12771 bcopy(&sdinfo->satadrv_id.ai_model, msg_buf,
12773 12772 sizeof (sdinfo->satadrv_id.ai_model));
12774 12773 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model));
12775 12774 msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
12776 12775 cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf);
12777 12776
12778 12777 bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf,
12779 12778 sizeof (sdinfo->satadrv_id.ai_fw));
12780 12779 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw));
12781 12780 msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
12782 12781 cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf);
12783 12782
12784 12783 bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf,
12785 12784 sizeof (sdinfo->satadrv_id.ai_drvser));
12786 12785 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser));
12787 12786 msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
12788 12787 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12789 12788 cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
12790 12789 } else {
12791 12790 /*
12792 12791 * Some drives do not implement serial number and may
12793 12792 * violate the spec by providing spaces rather than zeros
12794 12793 * in serial number field. Scan the buffer to detect it.
12795 12794 */
12796 12795 for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) {
12797 12796 if (msg_buf[i] != '\0' && msg_buf[i] != ' ')
12798 12797 break;
12799 12798 }
12800 12799 if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) {
12801 12800 cmn_err(CE_CONT, "?\tserial number - none\n");
12802 12801 } else {
12803 12802 cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
12804 12803 }
12805 12804 }
12806 12805
12807 12806 #ifdef SATA_DEBUG
12808 12807 if (sdinfo->satadrv_id.ai_majorversion != 0 &&
12809 12808 sdinfo->satadrv_id.ai_majorversion != 0xffff) {
12810 12809 int i;
12811 12810 for (i = 14; i >= 2; i--) {
12812 12811 if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) {
12813 12812 valid_version = i;
12814 12813 break;
12815 12814 }
12816 12815 }
12817 12816 cmn_err(CE_CONT,
12818 12817 "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
12819 12818 valid_version,
12820 12819 sdinfo->satadrv_id.ai_majorversion,
12821 12820 sdinfo->satadrv_id.ai_minorversion);
12822 12821 }
12823 12822 #endif
12824 12823 /* Log some info */
12825 12824 cmn_err(CE_CONT, "?\tsupported features:\n");
12826 12825 msg_buf[0] = '\0';
12827 12826 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12828 12827 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48)
12829 12828 (void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN);
12830 12829 else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28)
12831 12830 (void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN);
12832 12831 }
12833 12832 if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA)
12834 12833 (void) strlcat(msg_buf, "DMA", MAXPATHLEN);
12835 12834 if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ)
12836 12835 (void) strlcat(msg_buf, ", Native Command Queueing",
12837 12836 MAXPATHLEN);
12838 12837 if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)
12839 12838 (void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN);
12840 12839 if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) &&
12841 12840 (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED))
12842 12841 (void) strlcat(msg_buf, ", SMART", MAXPATHLEN);
12843 12842 if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) &&
12844 12843 (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED))
12845 12844 (void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN);
12846 12845 cmn_err(CE_CONT, "?\t %s\n", msg_buf);
12847 12846 if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA3)
12848 12847 cmn_err(CE_CONT, "?\tSATA Gen3 signaling speed (6.0Gbps)\n");
12849 12848 else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2)
12850 12849 cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n");
12851 12850 else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1)
12852 12851 cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n");
12853 12852 if (sdinfo->satadrv_features_support &
12854 12853 (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) {
12855 12854 msg_buf[0] = '\0';
12856 12855 (void) snprintf(msg_buf, MAXPATHLEN,
12857 12856 "Supported queue depth %d",
12858 12857 sdinfo->satadrv_queue_depth);
12859 12858 if (!(sata_func_enable &
12860 12859 (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ)))
12861 12860 (void) strlcat(msg_buf,
12862 12861 " - queueing disabled globally", MAXPATHLEN);
12863 12862 else if (sdinfo->satadrv_queue_depth >
12864 12863 sdinfo->satadrv_max_queue_depth) {
12865 12864 (void) snprintf(&msg_buf[strlen(msg_buf)],
12866 12865 MAXPATHLEN - strlen(msg_buf), ", limited to %d",
12867 12866 (int)sdinfo->satadrv_max_queue_depth);
12868 12867 }
12869 12868 cmn_err(CE_CONT, "?\t%s\n", msg_buf);
12870 12869 }
12871 12870
12872 12871 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12873 12872 #ifdef __i386
12874 12873 (void) sprintf(msg_buf, "\tcapacity = %llu sectors\n",
12875 12874 sdinfo->satadrv_capacity);
12876 12875 #else
12877 12876 (void) sprintf(msg_buf, "\tcapacity = %lu sectors\n",
12878 12877 sdinfo->satadrv_capacity);
12879 12878 #endif
12880 12879 cmn_err(CE_CONT, "?%s", msg_buf);
12881 12880 }
12882 12881 }
12883 12882
12884 12883 /*
12885 12884 * Log/display port multiplier information
12886 12885 * No Mutex should be hold.
12887 12886 */
12888 12887 static void
12889 12888 sata_show_pmult_info(sata_hba_inst_t *sata_hba_inst,
12890 12889 sata_device_t *sata_device)
12891 12890 {
12892 12891 _NOTE(ARGUNUSED(sata_hba_inst))
12893 12892
12894 12893 int cport = sata_device->satadev_addr.cport;
12895 12894 sata_pmult_info_t *pmultinfo;
12896 12895 char msg_buf[MAXPATHLEN];
12897 12896 uint32_t gscr0, gscr1, gscr2, gscr64;
12898 12897
12899 12898 mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12900 12899 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
12901 12900 if (pmultinfo == NULL) {
12902 12901 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12903 12902 return;
12904 12903 }
12905 12904
12906 12905 gscr0 = pmultinfo->pmult_gscr.gscr0;
12907 12906 gscr1 = pmultinfo->pmult_gscr.gscr1;
12908 12907 gscr2 = pmultinfo->pmult_gscr.gscr2;
12909 12908 gscr64 = pmultinfo->pmult_gscr.gscr64;
12910 12909 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12911 12910
12912 12911 cmn_err(CE_CONT, "?Port Multiplier %d device-ports found at port %d",
12913 12912 sata_device->satadev_add_info, sata_device->satadev_addr.cport);
12914 12913
12915 12914 (void) sprintf(msg_buf, "\tVendor_ID 0x%04x, Module_ID 0x%04x",
12916 12915 gscr0 & 0xffff, (gscr0 >> 16) & 0xffff);
12917 12916 cmn_err(CE_CONT, "?%s", msg_buf);
12918 12917
12919 12918 (void) strcpy(msg_buf, "\tSupport SATA PMP Spec ");
12920 12919 if (gscr1 & (1 << 3))
12921 12920 (void) strlcat(msg_buf, "1.2", MAXPATHLEN);
12922 12921 else if (gscr1 & (1 << 2))
12923 12922 (void) strlcat(msg_buf, "1.1", MAXPATHLEN);
12924 12923 else if (gscr1 & (1 << 1))
12925 12924 (void) strlcat(msg_buf, "1.0", MAXPATHLEN);
12926 12925 else
12927 12926 (void) strlcat(msg_buf, "unknown", MAXPATHLEN);
12928 12927 cmn_err(CE_CONT, "?%s", msg_buf);
12929 12928
12930 12929 (void) strcpy(msg_buf, "\tSupport ");
12931 12930 if (gscr64 & (1 << 3))
12932 12931 (void) strlcat(msg_buf, "Asy-Notif, ",
12933 12932 MAXPATHLEN);
12934 12933 if (gscr64 & (1 << 2))
12935 12934 (void) strlcat(msg_buf, "Dyn-SSC, ", MAXPATHLEN);
12936 12935 if (gscr64 & (1 << 1))
12937 12936 (void) strlcat(msg_buf, "Iss-PMREQ, ", MAXPATHLEN);
12938 12937 if (gscr64 & (1 << 0))
12939 12938 (void) strlcat(msg_buf, "BIST", MAXPATHLEN);
12940 12939 if ((gscr64 & 0xf) == 0)
12941 12940 (void) strlcat(msg_buf, "nothing", MAXPATHLEN);
12942 12941 cmn_err(CE_CONT, "?%s", msg_buf);
12943 12942
12944 12943 (void) sprintf(msg_buf, "\tNumber of exposed device fan-out ports: %d",
12945 12944 gscr2 & SATA_PMULT_PORTNUM_MASK);
12946 12945 cmn_err(CE_CONT, "?%s", msg_buf);
12947 12946 }
12948 12947
12949 12948 /*
12950 12949 * sata_save_drive_settings extracts current setting of the device and stores
12951 12950 * it for future reference, in case the device setup would need to be restored
12952 12951 * after the device reset.
12953 12952 *
12954 12953 * For all devices read ahead and write cache settings are saved, if the
12955 12954 * device supports these features at all.
12956 12955 * For ATAPI devices the Removable Media Status Notification setting is saved.
12957 12956 */
12958 12957 static void
12959 12958 sata_save_drive_settings(sata_drive_info_t *sdinfo)
12960 12959 {
12961 12960 if (SATA_READ_AHEAD_SUPPORTED(sdinfo->satadrv_id) ||
12962 12961 SATA_WRITE_CACHE_SUPPORTED(sdinfo->satadrv_id)) {
12963 12962
12964 12963 /* Current setting of Read Ahead (and Read Cache) */
12965 12964 if (SATA_READ_AHEAD_ENABLED(sdinfo->satadrv_id))
12966 12965 sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
12967 12966 else
12968 12967 sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD;
12969 12968
12970 12969 /* Current setting of Write Cache */
12971 12970 if (SATA_WRITE_CACHE_ENABLED(sdinfo->satadrv_id))
12972 12971 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12973 12972 else
12974 12973 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12975 12974 }
12976 12975
12977 12976 if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
12978 12977 if (SATA_RM_NOTIFIC_SUPPORTED(sdinfo->satadrv_id))
12979 12978 sdinfo->satadrv_settings |= SATA_DEV_RMSN;
12980 12979 else
12981 12980 sdinfo->satadrv_settings &= ~SATA_DEV_RMSN;
12982 12981 }
12983 12982 }
12984 12983
12985 12984
12986 12985 /*
12987 12986 * sata_check_capacity function determines a disk capacity
12988 12987 * and addressing mode (LBA28/LBA48) by examining a disk identify device data.
12989 12988 *
12990 12989 * NOTE: CHS mode is not supported! If a device does not support LBA,
12991 12990 * this function is not called.
12992 12991 *
12993 12992 * Returns device capacity in number of blocks, i.e. largest addressable LBA+1
12994 12993 */
12995 12994 static uint64_t
12996 12995 sata_check_capacity(sata_drive_info_t *sdinfo)
12997 12996 {
12998 12997 uint64_t capacity = 0;
12999 12998 int i;
13000 12999
13001 13000 if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK ||
13002 13001 !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT)
13003 13002 /* Capacity valid only for LBA-addressable disk devices */
13004 13003 return (0);
13005 13004
13006 13005 if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) &&
13007 13006 (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) &&
13008 13007 (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) {
13009 13008 /* LBA48 mode supported and enabled */
13010 13009 sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 |
13011 13010 SATA_DEV_F_LBA28;
13012 13011 for (i = 3; i >= 0; --i) {
13013 13012 capacity <<= 16;
13014 13013 capacity += sdinfo->satadrv_id.ai_addrsecxt[i];
13015 13014 }
13016 13015 } else {
13017 13016 capacity = sdinfo->satadrv_id.ai_addrsec[1];
13018 13017 capacity <<= 16;
13019 13018 capacity += sdinfo->satadrv_id.ai_addrsec[0];
13020 13019 if (capacity >= 0x1000000)
13021 13020 /* LBA28 mode */
13022 13021 sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28;
13023 13022 }
13024 13023 return (capacity);
13025 13024 }
13026 13025
13027 13026
13028 13027 /*
13029 13028 * Allocate consistent buffer for DMA transfer
13030 13029 *
13031 13030 * Cannot be called from interrupt level or with mutex held - it may sleep.
13032 13031 *
13033 13032 * Returns pointer to allocated buffer structure, or NULL if allocation failed.
13034 13033 */
13035 13034 static struct buf *
13036 13035 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len)
13037 13036 {
13038 13037 struct scsi_address ap;
13039 13038 struct buf *bp;
13040 13039 ddi_dma_attr_t cur_dma_attr;
13041 13040
13042 13041 ASSERT(spx->txlt_sata_pkt != NULL);
13043 13042 ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran;
13044 13043 ap.a_target = SATA_TO_SCSI_TARGET(
13045 13044 spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport,
13046 13045 spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport,
13047 13046 spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual);
13048 13047 ap.a_lun = 0;
13049 13048
13050 13049 bp = scsi_alloc_consistent_buf(&ap, NULL, len,
13051 13050 B_READ, SLEEP_FUNC, NULL);
13052 13051
13053 13052 if (bp != NULL) {
13054 13053 /* Allocate DMA resources for this buffer */
13055 13054 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
13056 13055 /*
13057 13056 * We use a local version of the dma_attr, to account
13058 13057 * for a device addressing limitations.
13059 13058 * sata_adjust_dma_attr() will handle sdinfo == NULL which
13060 13059 * will cause dma attributes to be adjusted to a lowest
13061 13060 * acceptable level.
13062 13061 */
13063 13062 sata_adjust_dma_attr(NULL,
13064 13063 SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
13065 13064
13066 13065 if (sata_dma_buf_setup(spx, PKT_CONSISTENT,
13067 13066 SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) {
13068 13067 scsi_free_consistent_buf(bp);
13069 13068 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
13070 13069 bp = NULL;
13071 13070 }
13072 13071 }
13073 13072 return (bp);
13074 13073 }
13075 13074
13076 13075 /*
13077 13076 * Release local buffer (consistent buffer for DMA transfer) allocated
13078 13077 * via sata_alloc_local_buffer().
13079 13078 */
13080 13079 static void
13081 13080 sata_free_local_buffer(sata_pkt_txlate_t *spx)
13082 13081 {
13083 13082 ASSERT(spx->txlt_sata_pkt != NULL);
13084 13083 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL);
13085 13084
13086 13085 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0;
13087 13086 spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL;
13088 13087
13089 13088 sata_common_free_dma_rsrcs(spx);
13090 13089
13091 13090 /* Free buffer */
13092 13091 scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp);
13093 13092 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
13094 13093 }
13095 13094
13096 13095 /*
13097 13096 * Allocate sata_pkt
13098 13097 * Pkt structure version and embedded strcutures version are initialized.
13099 13098 * sata_pkt and sata_pkt_txlate structures are cross-linked.
13100 13099 *
13101 13100 * Since this may be called in interrupt context by sata_scsi_init_pkt,
13102 13101 * callback argument determines if it can sleep or not.
13103 13102 * Hence, it should not be called from interrupt context.
13104 13103 *
13105 13104 * If successful, non-NULL pointer to a sata pkt is returned.
13106 13105 * Upon failure, NULL pointer is returned.
13107 13106 */
13108 13107 static sata_pkt_t *
13109 13108 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t))
13110 13109 {
13111 13110 sata_pkt_t *spkt;
13112 13111 int kmsflag;
13113 13112
13114 13113 kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
13115 13114 spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag);
13116 13115 if (spkt == NULL) {
13117 13116 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13118 13117 "sata_pkt_alloc: failed"));
13119 13118 return (NULL);
13120 13119 }
13121 13120 spkt->satapkt_rev = SATA_PKT_REV;
13122 13121 spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV;
13123 13122 spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
13124 13123 spkt->satapkt_framework_private = spx;
13125 13124 spx->txlt_sata_pkt = spkt;
13126 13125 return (spkt);
13127 13126 }
13128 13127
13129 13128 /*
13130 13129 * Free sata pkt allocated via sata_pkt_alloc()
13131 13130 */
13132 13131 static void
13133 13132 sata_pkt_free(sata_pkt_txlate_t *spx)
13134 13133 {
13135 13134 ASSERT(spx->txlt_sata_pkt != NULL);
13136 13135 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL);
13137 13136 kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t));
13138 13137 spx->txlt_sata_pkt = NULL;
13139 13138 }
13140 13139
13141 13140
13142 13141 /*
13143 13142 * Adjust DMA attributes.
13144 13143 * SCSI cmds block count is up to 24 bits, SATA cmd block count vary
13145 13144 * from 8 bits to 16 bits, depending on a command being used.
13146 13145 * Limiting max block count arbitrarily to 256 for all read/write
13147 13146 * commands may affects performance, so check both the device and
13148 13147 * controller capability before adjusting dma attributes.
13149 13148 */
13150 13149 void
13151 13150 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr,
13152 13151 ddi_dma_attr_t *adj_dma_attr)
13153 13152 {
13154 13153 uint32_t count_max;
13155 13154
13156 13155 /* Copy original attributes */
13157 13156 *adj_dma_attr = *dma_attr;
13158 13157 /*
13159 13158 * Things to consider: device addressing capability,
13160 13159 * "excessive" controller DMA capabilities.
13161 13160 * If a device is being probed/initialized, there are
13162 13161 * no device info - use default limits then.
13163 13162 */
13164 13163 if (sdinfo == NULL) {
13165 13164 count_max = dma_attr->dma_attr_granular * 0x100;
13166 13165 if (dma_attr->dma_attr_count_max > count_max)
13167 13166 adj_dma_attr->dma_attr_count_max = count_max;
13168 13167 if (dma_attr->dma_attr_maxxfer > count_max)
13169 13168 adj_dma_attr->dma_attr_maxxfer = count_max;
13170 13169 return;
13171 13170 }
13172 13171
13173 13172 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
13174 13173 if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) {
13175 13174 /*
13176 13175 * 16-bit sector count may be used - we rely on
13177 13176 * the assumption that only read and write cmds
13178 13177 * will request more than 256 sectors worth of data
13179 13178 */
13180 13179 count_max = adj_dma_attr->dma_attr_granular * 0x10000;
13181 13180 } else {
13182 13181 /*
13183 13182 * 8-bit sector count will be used - default limits
13184 13183 * for dma attributes
13185 13184 */
13186 13185 count_max = adj_dma_attr->dma_attr_granular * 0x100;
13187 13186 }
13188 13187 /*
13189 13188 * Adjust controler dma attributes, if necessary
13190 13189 */
13191 13190 if (dma_attr->dma_attr_count_max > count_max)
13192 13191 adj_dma_attr->dma_attr_count_max = count_max;
13193 13192 if (dma_attr->dma_attr_maxxfer > count_max)
13194 13193 adj_dma_attr->dma_attr_maxxfer = count_max;
13195 13194 }
13196 13195 }
13197 13196
13198 13197
13199 13198 /*
13200 13199 * Allocate DMA resources for the buffer
13201 13200 * This function handles initial DMA resource allocation as well as
13202 13201 * DMA window shift and may be called repeatedly for the same DMA window
13203 13202 * until all DMA cookies in the DMA window are processed.
13204 13203 * To guarantee that there is always a coherent set of cookies to process
13205 13204 * by SATA HBA driver (observing alignment, device granularity, etc.),
13206 13205 * the number of slots for DMA cookies is equal to lesser of a number of
13207 13206 * cookies in a DMA window and a max number of scatter/gather entries.
13208 13207 *
13209 13208 * Returns DDI_SUCCESS upon successful operation.
13210 13209 * Return failure code of a failing command or DDI_FAILURE when
13211 13210 * internal cleanup failed.
13212 13211 */
13213 13212 static int
13214 13213 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags,
13215 13214 int (*callback)(caddr_t), caddr_t arg,
13216 13215 ddi_dma_attr_t *cur_dma_attr)
13217 13216 {
13218 13217 int rval;
13219 13218 off_t offset;
13220 13219 size_t size;
13221 13220 int max_sg_len, req_len, i;
13222 13221 uint_t dma_flags;
13223 13222 struct buf *bp;
13224 13223 uint64_t cur_txfer_len;
13225 13224
13226 13225
13227 13226 ASSERT(spx->txlt_sata_pkt != NULL);
13228 13227 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
13229 13228 ASSERT(bp != NULL);
13230 13229
13231 13230
13232 13231 if (spx->txlt_buf_dma_handle == NULL) {
13233 13232 /*
13234 13233 * No DMA resources allocated so far - this is a first call
13235 13234 * for this sata pkt.
13236 13235 */
13237 13236 rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst),
13238 13237 cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle);
13239 13238
13240 13239 if (rval != DDI_SUCCESS) {
13241 13240 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13242 13241 "sata_dma_buf_setup: no buf DMA resources %x",
13243 13242 rval));
13244 13243 return (rval);
13245 13244 }
13246 13245
13247 13246 if (bp->b_flags & B_READ)
13248 13247 dma_flags = DDI_DMA_READ;
13249 13248 else
13250 13249 dma_flags = DDI_DMA_WRITE;
13251 13250
13252 13251 if (flags & PKT_CONSISTENT)
13253 13252 dma_flags |= DDI_DMA_CONSISTENT;
13254 13253
13255 13254 if (flags & PKT_DMA_PARTIAL)
13256 13255 dma_flags |= DDI_DMA_PARTIAL;
13257 13256
13258 13257 /*
13259 13258 * Check buffer alignment and size against dma attributes
13260 13259 * Consider dma_attr_align only. There may be requests
13261 13260 * with the size lower than device granularity, but they
13262 13261 * will not read/write from/to the device, so no adjustment
13263 13262 * is necessary. The dma_attr_minxfer theoretically should
13264 13263 * be considered, but no HBA driver is checking it.
13265 13264 */
13266 13265 if (IS_P2ALIGNED(bp->b_un.b_addr,
13267 13266 cur_dma_attr->dma_attr_align)) {
13268 13267 rval = ddi_dma_buf_bind_handle(
13269 13268 spx->txlt_buf_dma_handle,
13270 13269 bp, dma_flags, callback, arg,
13271 13270 &spx->txlt_dma_cookie,
13272 13271 &spx->txlt_curwin_num_dma_cookies);
13273 13272 } else { /* Buffer is not aligned */
13274 13273
13275 13274 int (*ddicallback)(caddr_t);
13276 13275 size_t bufsz;
13277 13276
13278 13277 /* Check id sleeping is allowed */
13279 13278 ddicallback = (callback == NULL_FUNC) ?
13280 13279 DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
13281 13280
13282 13281 SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13283 13282 "mis-aligned buffer: addr=0x%p, cnt=%lu",
13284 13283 (void *)bp->b_un.b_addr, bp->b_bcount);
13285 13284
13286 13285 if (bp->b_flags & (B_PAGEIO|B_PHYS))
13287 13286 /*
13288 13287 * CPU will need to access data in the buffer
13289 13288 * (for copying) so map it.
13290 13289 */
13291 13290 bp_mapin(bp);
13292 13291
13293 13292 ASSERT(spx->txlt_tmp_buf == NULL);
13294 13293
13295 13294 /* Buffer may be padded by ddi_dma_mem_alloc()! */
13296 13295 rval = ddi_dma_mem_alloc(
13297 13296 spx->txlt_buf_dma_handle,
13298 13297 bp->b_bcount,
13299 13298 &sata_acc_attr,
13300 13299 DDI_DMA_STREAMING,
13301 13300 ddicallback, NULL,
13302 13301 &spx->txlt_tmp_buf,
13303 13302 &bufsz,
13304 13303 &spx->txlt_tmp_buf_handle);
13305 13304
13306 13305 if (rval != DDI_SUCCESS) {
13307 13306 /* DMA mapping failed */
13308 13307 (void) ddi_dma_free_handle(
13309 13308 &spx->txlt_buf_dma_handle);
13310 13309 spx->txlt_buf_dma_handle = NULL;
13311 13310 #ifdef SATA_DEBUG
13312 13311 mbuffail_count++;
13313 13312 #endif
13314 13313 SATADBG1(SATA_DBG_DMA_SETUP,
13315 13314 spx->txlt_sata_hba_inst,
13316 13315 "sata_dma_buf_setup: "
13317 13316 "buf dma mem alloc failed %x\n", rval);
13318 13317 return (rval);
13319 13318 }
13320 13319 ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf,
13321 13320 cur_dma_attr->dma_attr_align));
13322 13321
13323 13322 #ifdef SATA_DEBUG
13324 13323 mbuf_count++;
13325 13324
13326 13325 if (bp->b_bcount != bufsz)
13327 13326 /*
13328 13327 * This will require special handling, because
13329 13328 * DMA cookies will be based on the temporary
13330 13329 * buffer size, not the original buffer
13331 13330 * b_bcount, so the residue may have to
13332 13331 * be counted differently.
13333 13332 */
13334 13333 SATADBG2(SATA_DBG_DMA_SETUP,
13335 13334 spx->txlt_sata_hba_inst,
13336 13335 "sata_dma_buf_setup: bp size %x != "
13337 13336 "bufsz %x\n", bp->b_bcount, bufsz);
13338 13337 #endif
13339 13338 if (dma_flags & DDI_DMA_WRITE) {
13340 13339 /*
13341 13340 * Write operation - copy data into
13342 13341 * an aligned temporary buffer. Buffer will be
13343 13342 * synced for device by ddi_dma_addr_bind_handle
13344 13343 */
13345 13344 bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf,
13346 13345 bp->b_bcount);
13347 13346 }
13348 13347
13349 13348 rval = ddi_dma_addr_bind_handle(
13350 13349 spx->txlt_buf_dma_handle,
13351 13350 NULL,
13352 13351 spx->txlt_tmp_buf,
13353 13352 bufsz, dma_flags, ddicallback, 0,
13354 13353 &spx->txlt_dma_cookie,
13355 13354 &spx->txlt_curwin_num_dma_cookies);
13356 13355 }
13357 13356
13358 13357 switch (rval) {
13359 13358 case DDI_DMA_PARTIAL_MAP:
13360 13359 SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13361 13360 "sata_dma_buf_setup: DMA Partial Map\n", NULL);
13362 13361 /*
13363 13362 * Partial DMA mapping.
13364 13363 * Retrieve number of DMA windows for this request.
13365 13364 */
13366 13365 if (ddi_dma_numwin(spx->txlt_buf_dma_handle,
13367 13366 &spx->txlt_num_dma_win) != DDI_SUCCESS) {
13368 13367 if (spx->txlt_tmp_buf != NULL) {
13369 13368 ddi_dma_mem_free(
13370 13369 &spx->txlt_tmp_buf_handle);
13371 13370 spx->txlt_tmp_buf = NULL;
13372 13371 }
13373 13372 (void) ddi_dma_unbind_handle(
13374 13373 spx->txlt_buf_dma_handle);
13375 13374 (void) ddi_dma_free_handle(
13376 13375 &spx->txlt_buf_dma_handle);
13377 13376 spx->txlt_buf_dma_handle = NULL;
13378 13377 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13379 13378 "sata_dma_buf_setup: numwin failed\n"));
13380 13379 return (DDI_FAILURE);
13381 13380 }
13382 13381 SATADBG2(SATA_DBG_DMA_SETUP,
13383 13382 spx->txlt_sata_hba_inst,
13384 13383 "sata_dma_buf_setup: windows: %d, cookies: %d\n",
13385 13384 spx->txlt_num_dma_win,
13386 13385 spx->txlt_curwin_num_dma_cookies);
13387 13386 spx->txlt_cur_dma_win = 0;
13388 13387 break;
13389 13388
13390 13389 case DDI_DMA_MAPPED:
13391 13390 /* DMA fully mapped */
13392 13391 spx->txlt_num_dma_win = 1;
13393 13392 spx->txlt_cur_dma_win = 0;
13394 13393 SATADBG1(SATA_DBG_DMA_SETUP,
13395 13394 spx->txlt_sata_hba_inst,
13396 13395 "sata_dma_buf_setup: windows: 1 "
13397 13396 "cookies: %d\n", spx->txlt_curwin_num_dma_cookies);
13398 13397 break;
13399 13398
13400 13399 default:
13401 13400 /* DMA mapping failed */
13402 13401 if (spx->txlt_tmp_buf != NULL) {
13403 13402 ddi_dma_mem_free(
13404 13403 &spx->txlt_tmp_buf_handle);
13405 13404 spx->txlt_tmp_buf = NULL;
13406 13405 }
13407 13406 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
13408 13407 spx->txlt_buf_dma_handle = NULL;
13409 13408 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13410 13409 "sata_dma_buf_setup: buf dma handle binding "
13411 13410 "failed %x\n", rval));
13412 13411 return (rval);
13413 13412 }
13414 13413 spx->txlt_curwin_processed_dma_cookies = 0;
13415 13414 spx->txlt_dma_cookie_list = NULL;
13416 13415 } else {
13417 13416 /*
13418 13417 * DMA setup is reused. Check if we need to process more
13419 13418 * cookies in current window, or to get next window, if any.
13420 13419 */
13421 13420
13422 13421 ASSERT(spx->txlt_curwin_processed_dma_cookies <=
13423 13422 spx->txlt_curwin_num_dma_cookies);
13424 13423
13425 13424 if (spx->txlt_curwin_processed_dma_cookies ==
13426 13425 spx->txlt_curwin_num_dma_cookies) {
13427 13426 /*
13428 13427 * All cookies from current DMA window were processed.
13429 13428 * Get next DMA window.
13430 13429 */
13431 13430 spx->txlt_cur_dma_win++;
13432 13431 if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) {
13433 13432 (void) ddi_dma_getwin(spx->txlt_buf_dma_handle,
13434 13433 spx->txlt_cur_dma_win, &offset, &size,
13435 13434 &spx->txlt_dma_cookie,
13436 13435 &spx->txlt_curwin_num_dma_cookies);
13437 13436 spx->txlt_curwin_processed_dma_cookies = 0;
13438 13437 } else {
13439 13438 /* No more windows! End of request! */
13440 13439 /* What to do? - panic for now */
13441 13440 ASSERT(spx->txlt_cur_dma_win >=
13442 13441 spx->txlt_num_dma_win);
13443 13442
13444 13443 spx->txlt_curwin_num_dma_cookies = 0;
13445 13444 spx->txlt_curwin_processed_dma_cookies = 0;
13446 13445 spx->txlt_sata_pkt->
13447 13446 satapkt_cmd.satacmd_num_dma_cookies = 0;
13448 13447 return (DDI_SUCCESS);
13449 13448 }
13450 13449 }
13451 13450 }
13452 13451 /* There better be at least one DMA cookie outstanding */
13453 13452 ASSERT((spx->txlt_curwin_num_dma_cookies -
13454 13453 spx->txlt_curwin_processed_dma_cookies) > 0);
13455 13454
13456 13455 if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) {
13457 13456 /* The default cookie slot was used in previous run */
13458 13457 ASSERT(spx->txlt_curwin_processed_dma_cookies == 0);
13459 13458 spx->txlt_dma_cookie_list = NULL;
13460 13459 spx->txlt_dma_cookie_list_len = 0;
13461 13460 }
13462 13461 if (spx->txlt_curwin_processed_dma_cookies == 0) {
13463 13462 /*
13464 13463 * Processing a new DMA window - set-up dma cookies list.
13465 13464 * We may reuse previously allocated cookie array if it is
13466 13465 * possible.
13467 13466 */
13468 13467 if (spx->txlt_dma_cookie_list != NULL &&
13469 13468 spx->txlt_dma_cookie_list_len <
13470 13469 spx->txlt_curwin_num_dma_cookies) {
13471 13470 /*
13472 13471 * New DMA window contains more cookies than
13473 13472 * the previous one. We need larger cookie list - free
13474 13473 * the old one.
13475 13474 */
13476 13475 (void) kmem_free(spx->txlt_dma_cookie_list,
13477 13476 spx->txlt_dma_cookie_list_len *
13478 13477 sizeof (ddi_dma_cookie_t));
13479 13478 spx->txlt_dma_cookie_list = NULL;
13480 13479 spx->txlt_dma_cookie_list_len = 0;
13481 13480 }
13482 13481 if (spx->txlt_dma_cookie_list == NULL) {
13483 13482 /*
13484 13483 * Calculate lesser of number of cookies in this
13485 13484 * DMA window and number of s/g entries.
13486 13485 */
13487 13486 max_sg_len = cur_dma_attr->dma_attr_sgllen;
13488 13487 req_len = MIN(max_sg_len,
13489 13488 spx->txlt_curwin_num_dma_cookies);
13490 13489
13491 13490 /* Allocate new dma cookie array if necessary */
13492 13491 if (req_len == 1) {
13493 13492 /* Only one cookie - no need for a list */
13494 13493 spx->txlt_dma_cookie_list =
13495 13494 &spx->txlt_dma_cookie;
13496 13495 spx->txlt_dma_cookie_list_len = 1;
13497 13496 } else {
13498 13497 /*
13499 13498 * More than one cookie - try to allocate space.
13500 13499 */
13501 13500 spx->txlt_dma_cookie_list = kmem_zalloc(
13502 13501 sizeof (ddi_dma_cookie_t) * req_len,
13503 13502 callback == NULL_FUNC ? KM_NOSLEEP :
13504 13503 KM_SLEEP);
13505 13504 if (spx->txlt_dma_cookie_list == NULL) {
13506 13505 SATADBG1(SATA_DBG_DMA_SETUP,
13507 13506 spx->txlt_sata_hba_inst,
13508 13507 "sata_dma_buf_setup: cookie list "
13509 13508 "allocation failed\n", NULL);
13510 13509 /*
13511 13510 * We could not allocate space for
13512 13511 * neccessary number of dma cookies in
13513 13512 * this window, so we fail this request.
13514 13513 * Next invocation would try again to
13515 13514 * allocate space for cookie list.
13516 13515 * Note:Packet residue was not modified.
13517 13516 */
13518 13517 return (DDI_DMA_NORESOURCES);
13519 13518 } else {
13520 13519 spx->txlt_dma_cookie_list_len = req_len;
13521 13520 }
13522 13521 }
13523 13522 }
13524 13523 /*
13525 13524 * Fetch DMA cookies into cookie list in sata_pkt_txlate.
13526 13525 * First cookie was already fetched.
13527 13526 */
13528 13527 *(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie;
13529 13528 cur_txfer_len =
13530 13529 (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size;
13531 13530 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1;
13532 13531 spx->txlt_curwin_processed_dma_cookies++;
13533 13532 for (i = 1; (i < spx->txlt_dma_cookie_list_len) &&
13534 13533 (i < spx->txlt_curwin_num_dma_cookies); i++) {
13535 13534 ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
13536 13535 &spx->txlt_dma_cookie_list[i]);
13537 13536 cur_txfer_len +=
13538 13537 (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
13539 13538 spx->txlt_curwin_processed_dma_cookies++;
13540 13539 spx->txlt_sata_pkt->
13541 13540 satapkt_cmd.satacmd_num_dma_cookies += 1;
13542 13541 }
13543 13542 } else {
13544 13543 SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13545 13544 "sata_dma_buf_setup: sliding within DMA window, "
13546 13545 "cur cookie %d, total cookies %d\n",
13547 13546 spx->txlt_curwin_processed_dma_cookies,
13548 13547 spx->txlt_curwin_num_dma_cookies);
13549 13548
13550 13549 /*
13551 13550 * Not all cookies from the current dma window were used because
13552 13551 * of s/g limitation.
13553 13552 * There is no need to re-size the list - it was set at
13554 13553 * optimal size, or only default entry is used (s/g = 1).
13555 13554 */
13556 13555 if (spx->txlt_dma_cookie_list == NULL) {
13557 13556 spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie;
13558 13557 spx->txlt_dma_cookie_list_len = 1;
13559 13558 }
13560 13559 /*
13561 13560 * Since we are processing remaining cookies in a DMA window,
13562 13561 * there may be less of them than the number of entries in the
13563 13562 * current dma cookie list.
13564 13563 */
13565 13564 req_len = MIN(spx->txlt_dma_cookie_list_len,
13566 13565 (spx->txlt_curwin_num_dma_cookies -
13567 13566 spx->txlt_curwin_processed_dma_cookies));
13568 13567
13569 13568 /* Fetch the next batch of cookies */
13570 13569 for (i = 0, cur_txfer_len = 0; i < req_len; i++) {
13571 13570 ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
13572 13571 &spx->txlt_dma_cookie_list[i]);
13573 13572 cur_txfer_len +=
13574 13573 (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
13575 13574 spx->txlt_sata_pkt->
13576 13575 satapkt_cmd.satacmd_num_dma_cookies++;
13577 13576 spx->txlt_curwin_processed_dma_cookies++;
13578 13577 }
13579 13578 }
13580 13579
13581 13580 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0);
13582 13581
13583 13582 /* Point sata_cmd to the cookie list */
13584 13583 spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list =
13585 13584 &spx->txlt_dma_cookie_list[0];
13586 13585
13587 13586 /* Remember number of DMA cookies passed in sata packet */
13588 13587 spx->txlt_num_dma_cookies =
13589 13588 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies;
13590 13589
13591 13590 ASSERT(cur_txfer_len != 0);
13592 13591 if (cur_txfer_len <= bp->b_bcount)
13593 13592 spx->txlt_total_residue -= cur_txfer_len;
13594 13593 else {
13595 13594 /*
13596 13595 * Temporary DMA buffer has been padded by
13597 13596 * ddi_dma_mem_alloc()!
13598 13597 * This requires special handling, because DMA cookies are
13599 13598 * based on the temporary buffer size, not the b_bcount,
13600 13599 * and we have extra bytes to transfer - but the packet
13601 13600 * residue has to stay correct because we will copy only
13602 13601 * the requested number of bytes.
13603 13602 */
13604 13603 spx->txlt_total_residue -= bp->b_bcount;
13605 13604 }
13606 13605
13607 13606 return (DDI_SUCCESS);
13608 13607 }
13609 13608
13610 13609 /*
13611 13610 * Common routine for releasing DMA resources
13612 13611 */
13613 13612 static void
13614 13613 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx)
13615 13614 {
13616 13615 if (spx->txlt_buf_dma_handle != NULL) {
13617 13616 if (spx->txlt_tmp_buf != NULL) {
13618 13617 /*
13619 13618 * Intermediate DMA buffer was allocated.
13620 13619 * Free allocated buffer and associated access handle.
13621 13620 */
13622 13621 ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
13623 13622 spx->txlt_tmp_buf = NULL;
13624 13623 }
13625 13624 /*
13626 13625 * Free DMA resources - cookies and handles
13627 13626 */
13628 13627 /* ASSERT(spx->txlt_dma_cookie_list != NULL); */
13629 13628 if (spx->txlt_dma_cookie_list != NULL) {
13630 13629 if (spx->txlt_dma_cookie_list !=
13631 13630 &spx->txlt_dma_cookie) {
13632 13631 (void) kmem_free(spx->txlt_dma_cookie_list,
13633 13632 spx->txlt_dma_cookie_list_len *
13634 13633 sizeof (ddi_dma_cookie_t));
13635 13634 spx->txlt_dma_cookie_list = NULL;
13636 13635 }
13637 13636 }
13638 13637 (void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
13639 13638 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
13640 13639 spx->txlt_buf_dma_handle = NULL;
13641 13640 }
13642 13641 }
13643 13642
13644 13643 /*
13645 13644 * Free DMA resources
13646 13645 * Used by the HBA driver to release DMA resources that it does not use.
13647 13646 *
13648 13647 * Returns Void
13649 13648 */
13650 13649 void
13651 13650 sata_free_dma_resources(sata_pkt_t *sata_pkt)
13652 13651 {
13653 13652 sata_pkt_txlate_t *spx;
13654 13653
13655 13654 if (sata_pkt == NULL)
13656 13655 return;
13657 13656
13658 13657 spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
13659 13658
13660 13659 sata_common_free_dma_rsrcs(spx);
13661 13660 }
13662 13661
13663 13662 /*
13664 13663 * Fetch Device Identify data.
13665 13664 * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type)
13666 13665 * command to a device and get the device identify data.
13667 13666 * The device_info structure has to be set to device type (for selecting proper
13668 13667 * device identify command).
13669 13668 *
13670 13669 * Returns:
13671 13670 * SATA_SUCCESS if cmd succeeded
13672 13671 * SATA_RETRY if cmd was rejected and could be retried,
13673 13672 * SATA_FAILURE if cmd failed and should not be retried (port error)
13674 13673 *
13675 13674 * Cannot be called in an interrupt context.
13676 13675 */
13677 13676
13678 13677 static int
13679 13678 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst,
13680 13679 sata_drive_info_t *sdinfo)
13681 13680 {
13682 13681 struct buf *bp;
13683 13682 sata_pkt_t *spkt;
13684 13683 sata_cmd_t *scmd;
13685 13684 sata_pkt_txlate_t *spx;
13686 13685 int rval;
13687 13686 dev_info_t *dip = SATA_DIP(sata_hba_inst);
13688 13687
13689 13688 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13690 13689 spx->txlt_sata_hba_inst = sata_hba_inst;
13691 13690 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */
13692 13691 spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13693 13692 if (spkt == NULL) {
13694 13693 kmem_free(spx, sizeof (sata_pkt_txlate_t));
13695 13694 return (SATA_RETRY); /* may retry later */
13696 13695 }
13697 13696 /* address is needed now */
13698 13697 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13699 13698
13700 13699 /*
13701 13700 * Allocate buffer for Identify Data return data
13702 13701 */
13703 13702 bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t));
13704 13703 if (bp == NULL) {
13705 13704 sata_pkt_free(spx);
13706 13705 kmem_free(spx, sizeof (sata_pkt_txlate_t));
13707 13706 SATA_LOG_D((sata_hba_inst, CE_WARN,
13708 13707 "sata_fetch_device_identify_data: "
13709 13708 "cannot allocate buffer for ID"));
13710 13709 return (SATA_RETRY); /* may retry later */
13711 13710 }
13712 13711
13713 13712 /* Fill sata_pkt */
13714 13713 sdinfo->satadrv_state = SATA_STATE_PROBING;
13715 13714 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13716 13715 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13717 13716 /* Synchronous mode, no callback */
13718 13717 spkt->satapkt_comp = NULL;
13719 13718 /* Timeout 30s */
13720 13719 spkt->satapkt_time = sata_default_pkt_time;
13721 13720
13722 13721 scmd = &spkt->satapkt_cmd;
13723 13722 scmd->satacmd_bp = bp;
13724 13723 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13725 13724 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
13726 13725
13727 13726 /* Build Identify Device cmd in the sata_pkt */
13728 13727 scmd->satacmd_addr_type = 0; /* N/A */
13729 13728 scmd->satacmd_sec_count_lsb = 0; /* N/A */
13730 13729 scmd->satacmd_lba_low_lsb = 0; /* N/A */
13731 13730 scmd->satacmd_lba_mid_lsb = 0; /* N/A */
13732 13731 scmd->satacmd_lba_high_lsb = 0; /* N/A */
13733 13732 scmd->satacmd_features_reg = 0; /* N/A */
13734 13733 scmd->satacmd_device_reg = 0; /* Always device 0 */
13735 13734 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
13736 13735 /* Identify Packet Device cmd */
13737 13736 scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE;
13738 13737 } else {
13739 13738 /* Identify Device cmd - mandatory for all other devices */
13740 13739 scmd->satacmd_cmd_reg = SATAC_ID_DEVICE;
13741 13740 }
13742 13741
13743 13742 /* Send pkt to SATA HBA driver */
13744 13743 rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt);
13745 13744
13746 13745 #ifdef SATA_INJECT_FAULTS
13747 13746 sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
13748 13747 #endif
13749 13748
13750 13749 if (rval == SATA_TRAN_ACCEPTED &&
13751 13750 spkt->satapkt_reason == SATA_PKT_COMPLETED) {
13752 13751 if (spx->txlt_buf_dma_handle != NULL) {
13753 13752 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13754 13753 DDI_DMA_SYNC_FORKERNEL);
13755 13754 ASSERT(rval == DDI_SUCCESS);
13756 13755 if (sata_check_for_dma_error(dip, spx)) {
13757 13756 ddi_fm_service_impact(dip,
13758 13757 DDI_SERVICE_UNAFFECTED);
13759 13758 rval = SATA_RETRY;
13760 13759 goto fail;
13761 13760 }
13762 13761
13763 13762 }
13764 13763 if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config &
13765 13764 SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) {
13766 13765 SATA_LOG_D((sata_hba_inst, CE_WARN,
13767 13766 "SATA disk device at port %d - "
13768 13767 "partial Identify Data",
13769 13768 sdinfo->satadrv_addr.cport));
13770 13769 rval = SATA_RETRY; /* may retry later */
13771 13770 goto fail;
13772 13771 }
13773 13772 /* Update sata_drive_info */
13774 13773 bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id,
13775 13774 sizeof (sata_id_t));
13776 13775
13777 13776 sdinfo->satadrv_features_support = 0;
13778 13777 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
13779 13778 /*
13780 13779 * Retrieve capacity (disks only) and addressing mode
13781 13780 */
13782 13781 sdinfo->satadrv_capacity = sata_check_capacity(sdinfo);
13783 13782 } else {
13784 13783 /*
13785 13784 * For ATAPI devices one would have to issue
13786 13785 * Get Capacity cmd for media capacity. Not here.
13787 13786 */
13788 13787 sdinfo->satadrv_capacity = 0;
13789 13788 /*
13790 13789 * Check what cdb length is supported
13791 13790 */
13792 13791 if ((sdinfo->satadrv_id.ai_config &
13793 13792 SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B)
13794 13793 sdinfo->satadrv_atapi_cdb_len = 16;
13795 13794 else
13796 13795 sdinfo->satadrv_atapi_cdb_len = 12;
13797 13796 }
13798 13797 /* Setup supported features flags */
13799 13798 if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT)
13800 13799 sdinfo->satadrv_features_support |= SATA_DEV_F_DMA;
13801 13800
13802 13801 /* Check for SATA GEN and NCQ support */
13803 13802 if (sdinfo->satadrv_id.ai_satacap != 0 &&
13804 13803 sdinfo->satadrv_id.ai_satacap != 0xffff) {
13805 13804 /* SATA compliance */
13806 13805 if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ)
13807 13806 sdinfo->satadrv_features_support |=
13808 13807 SATA_DEV_F_NCQ;
13809 13808 if (sdinfo->satadrv_id.ai_satacap &
13810 13809 (SATA_1_SPEED | SATA_2_SPEED | SATA_3_SPEED)) {
13811 13810 if (sdinfo->satadrv_id.ai_satacap &
13812 13811 SATA_3_SPEED)
13813 13812 sdinfo->satadrv_features_support |=
13814 13813 SATA_DEV_F_SATA3;
13815 13814 if (sdinfo->satadrv_id.ai_satacap &
13816 13815 SATA_2_SPEED)
13817 13816 sdinfo->satadrv_features_support |=
13818 13817 SATA_DEV_F_SATA2;
13819 13818 if (sdinfo->satadrv_id.ai_satacap &
13820 13819 SATA_1_SPEED)
13821 13820 sdinfo->satadrv_features_support |=
13822 13821 SATA_DEV_F_SATA1;
13823 13822 } else {
13824 13823 sdinfo->satadrv_features_support |=
13825 13824 SATA_DEV_F_SATA1;
13826 13825 }
13827 13826 }
13828 13827 if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) &&
13829 13828 (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD))
13830 13829 sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ;
13831 13830
13832 13831 sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth;
13833 13832 if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) ||
13834 13833 (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) {
13835 13834 ++sdinfo->satadrv_queue_depth;
13836 13835 /* Adjust according to controller capabilities */
13837 13836 sdinfo->satadrv_max_queue_depth = MIN(
13838 13837 sdinfo->satadrv_queue_depth,
13839 13838 SATA_QDEPTH(sata_hba_inst));
13840 13839 /* Adjust according to global queue depth limit */
13841 13840 sdinfo->satadrv_max_queue_depth = MIN(
13842 13841 sdinfo->satadrv_max_queue_depth,
13843 13842 sata_current_max_qdepth);
13844 13843 if (sdinfo->satadrv_max_queue_depth == 0)
13845 13844 sdinfo->satadrv_max_queue_depth = 1;
13846 13845 } else
13847 13846 sdinfo->satadrv_max_queue_depth = 1;
13848 13847
13849 13848 rval = SATA_SUCCESS;
13850 13849 } else {
13851 13850 /*
13852 13851 * Woops, no Identify Data.
13853 13852 */
13854 13853 if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) {
13855 13854 rval = SATA_RETRY; /* may retry later */
13856 13855 } else if (rval == SATA_TRAN_ACCEPTED) {
13857 13856 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR ||
13858 13857 spkt->satapkt_reason == SATA_PKT_ABORTED ||
13859 13858 spkt->satapkt_reason == SATA_PKT_TIMEOUT ||
13860 13859 spkt->satapkt_reason == SATA_PKT_RESET)
13861 13860 rval = SATA_RETRY; /* may retry later */
13862 13861 else
13863 13862 rval = SATA_FAILURE;
13864 13863 } else {
13865 13864 rval = SATA_FAILURE;
13866 13865 }
13867 13866 }
13868 13867 fail:
13869 13868 /* Free allocated resources */
13870 13869 sata_free_local_buffer(spx);
13871 13870 sata_pkt_free(spx);
13872 13871 kmem_free(spx, sizeof (sata_pkt_txlate_t));
13873 13872
13874 13873 return (rval);
13875 13874 }
13876 13875
13877 13876
13878 13877 /*
13879 13878 * Some devices may not come-up with default DMA mode (UDMA or MWDMA).
13880 13879 * UDMA mode is checked first, followed by MWDMA mode.
13881 13880 * set correctly, so this function is setting it to the highest supported level.
13882 13881 * Older SATA spec required that the device supports at least DMA 4 mode and
13883 13882 * UDMA mode is selected. It is not mentioned in SerialATA 2.6, so this
13884 13883 * restriction has been removed.
13885 13884 *
13886 13885 * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported.
13887 13886 * Returns SATA_FAILURE if proper DMA mode could not be selected.
13888 13887 *
13889 13888 * NOTE: This function should be called only if DMA mode is supported.
13890 13889 */
13891 13890 static int
13892 13891 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo)
13893 13892 {
13894 13893 sata_pkt_t *spkt;
13895 13894 sata_cmd_t *scmd;
13896 13895 sata_pkt_txlate_t *spx;
13897 13896 int i, mode;
13898 13897 uint8_t subcmd;
13899 13898 int rval = SATA_SUCCESS;
13900 13899
13901 13900 ASSERT(sdinfo != NULL);
13902 13901 ASSERT(sata_hba_inst != NULL);
13903 13902
13904 13903 if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
13905 13904 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) {
13906 13905 /* Find highest Ultra DMA mode supported */
13907 13906 for (mode = 6; mode >= 0; --mode) {
13908 13907 if (sdinfo->satadrv_id.ai_ultradma & (1 << mode))
13909 13908 break;
13910 13909 }
13911 13910 #if 0
13912 13911 /* Left for historical reasons */
13913 13912 /*
13914 13913 * Some initial version of SATA spec indicated that at least
13915 13914 * UDMA mode 4 has to be supported. It is not mentioned in
13916 13915 * SerialATA 2.6, so this restriction is removed.
13917 13916 */
13918 13917 if (mode < 4)
13919 13918 return (SATA_FAILURE);
13920 13919 #endif
13921 13920
13922 13921 /*
13923 13922 * For disk, we're still going to set DMA mode whatever is
13924 13923 * selected by default
13925 13924 *
13926 13925 * We saw an old maxtor sata drive will select Ultra DMA and
13927 13926 * Multi-Word DMA simultaneouly by default, which is going
13928 13927 * to cause DMA command timed out, so we need to select DMA
13929 13928 * mode even when it's already done by default
13930 13929 */
13931 13930 if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) {
13932 13931
13933 13932 /* Find UDMA mode currently selected */
13934 13933 for (i = 6; i >= 0; --i) {
13935 13934 if (sdinfo->satadrv_id.ai_ultradma &
13936 13935 (1 << (i + 8)))
13937 13936 break;
13938 13937 }
13939 13938 if (i >= mode)
13940 13939 /* Nothing to do */
13941 13940 return (SATA_SUCCESS);
13942 13941 }
13943 13942
13944 13943 subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA;
13945 13944
13946 13945 } else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) {
13947 13946 /* Find highest MultiWord DMA mode supported */
13948 13947 for (mode = 2; mode >= 0; --mode) {
13949 13948 if (sdinfo->satadrv_id.ai_dworddma & (1 << mode))
13950 13949 break;
13951 13950 }
13952 13951
13953 13952 /*
13954 13953 * For disk, We're still going to set DMA mode whatever is
13955 13954 * selected by default
13956 13955 *
13957 13956 * We saw an old maxtor sata drive will select Ultra DMA and
13958 13957 * Multi-Word DMA simultaneouly by default, which is going
13959 13958 * to cause DMA command timed out, so we need to select DMA
13960 13959 * mode even when it's already done by default
13961 13960 */
13962 13961 if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) {
13963 13962
13964 13963 /* Find highest MultiWord DMA mode selected */
13965 13964 for (i = 2; i >= 0; --i) {
13966 13965 if (sdinfo->satadrv_id.ai_dworddma &
13967 13966 (1 << (i + 8)))
13968 13967 break;
13969 13968 }
13970 13969 if (i >= mode)
13971 13970 /* Nothing to do */
13972 13971 return (SATA_SUCCESS);
13973 13972 }
13974 13973
13975 13974 subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA;
13976 13975 } else
13977 13976 return (SATA_SUCCESS);
13978 13977
13979 13978 /*
13980 13979 * Set DMA mode via SET FEATURES COMMAND.
13981 13980 * Prepare packet for SET FEATURES COMMAND.
13982 13981 */
13983 13982 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13984 13983 spx->txlt_sata_hba_inst = sata_hba_inst;
13985 13984 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */
13986 13985 spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13987 13986 if (spkt == NULL) {
13988 13987 SATA_LOG_D((sata_hba_inst, CE_WARN,
13989 13988 "sata_set_dma_mode: could not set DMA mode %d", mode));
13990 13989 rval = SATA_FAILURE;
13991 13990 goto done;
13992 13991 }
13993 13992 /* Fill sata_pkt */
13994 13993 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13995 13994 /* Timeout 30s */
13996 13995 spkt->satapkt_time = sata_default_pkt_time;
13997 13996 /* Synchronous mode, no callback, interrupts */
13998 13997 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13999 13998 spkt->satapkt_comp = NULL;
14000 13999 scmd = &spkt->satapkt_cmd;
14001 14000 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14002 14001 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14003 14002 scmd->satacmd_addr_type = 0;
14004 14003 scmd->satacmd_device_reg = 0;
14005 14004 scmd->satacmd_status_reg = 0;
14006 14005 scmd->satacmd_error_reg = 0;
14007 14006 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14008 14007 scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE;
14009 14008 scmd->satacmd_sec_count_lsb = subcmd | mode;
14010 14009
14011 14010 /* Transfer command to HBA */
14012 14011 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
14013 14012 spkt) != SATA_TRAN_ACCEPTED ||
14014 14013 spkt->satapkt_reason != SATA_PKT_COMPLETED) {
14015 14014 /* Pkt execution failed */
14016 14015 rval = SATA_FAILURE;
14017 14016 }
14018 14017 done:
14019 14018
14020 14019 /* Free allocated resources */
14021 14020 if (spkt != NULL)
14022 14021 sata_pkt_free(spx);
14023 14022 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14024 14023
14025 14024 return (rval);
14026 14025 }
14027 14026
14028 14027
14029 14028 /*
14030 14029 * Set device caching mode.
14031 14030 * One of the following operations should be specified:
14032 14031 * SATAC_SF_ENABLE_READ_AHEAD
14033 14032 * SATAC_SF_DISABLE_READ_AHEAD
14034 14033 * SATAC_SF_ENABLE_WRITE_CACHE
14035 14034 * SATAC_SF_DISABLE_WRITE_CACHE
14036 14035 *
14037 14036 * If operation fails, system log messgage is emitted.
14038 14037 * Returns SATA_SUCCESS when the operation succeeds, SATA_RETRY if
14039 14038 * command was sent but did not succeed, and SATA_FAILURE otherwise.
14040 14039 */
14041 14040
14042 14041 static int
14043 14042 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
14044 14043 int cache_op)
14045 14044 {
14046 14045 sata_pkt_t *spkt;
14047 14046 sata_cmd_t *scmd;
14048 14047 sata_pkt_txlate_t *spx;
14049 14048 int rval = SATA_SUCCESS;
14050 14049 int hba_rval;
14051 14050 char *infop;
14052 14051
14053 14052 ASSERT(sdinfo != NULL);
14054 14053 ASSERT(sata_hba_inst != NULL);
14055 14054 ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD ||
14056 14055 cache_op == SATAC_SF_DISABLE_READ_AHEAD ||
14057 14056 cache_op == SATAC_SF_ENABLE_WRITE_CACHE ||
14058 14057 cache_op == SATAC_SF_DISABLE_WRITE_CACHE);
14059 14058
14060 14059
14061 14060 /* Prepare packet for SET FEATURES COMMAND */
14062 14061 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14063 14062 spx->txlt_sata_hba_inst = sata_hba_inst;
14064 14063 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */
14065 14064 spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14066 14065 if (spkt == NULL) {
14067 14066 rval = SATA_FAILURE;
14068 14067 goto failure;
14069 14068 }
14070 14069 /* Fill sata_pkt */
14071 14070 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14072 14071 /* Timeout 30s */
14073 14072 spkt->satapkt_time = sata_default_pkt_time;
14074 14073 /* Synchronous mode, no callback, interrupts */
14075 14074 spkt->satapkt_op_mode =
14076 14075 SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14077 14076 spkt->satapkt_comp = NULL;
14078 14077 scmd = &spkt->satapkt_cmd;
14079 14078 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14080 14079 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14081 14080 scmd->satacmd_addr_type = 0;
14082 14081 scmd->satacmd_device_reg = 0;
14083 14082 scmd->satacmd_status_reg = 0;
14084 14083 scmd->satacmd_error_reg = 0;
14085 14084 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14086 14085 scmd->satacmd_features_reg = cache_op;
14087 14086
14088 14087 /* Transfer command to HBA */
14089 14088 hba_rval = (*SATA_START_FUNC(sata_hba_inst))(
14090 14089 SATA_DIP(sata_hba_inst), spkt);
14091 14090
14092 14091 #ifdef SATA_INJECT_FAULTS
14093 14092 sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
14094 14093 #endif
14095 14094
14096 14095 if ((hba_rval != SATA_TRAN_ACCEPTED) ||
14097 14096 (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
14098 14097 /* Pkt execution failed */
14099 14098 switch (cache_op) {
14100 14099 case SATAC_SF_ENABLE_READ_AHEAD:
14101 14100 infop = "enabling read ahead failed";
14102 14101 break;
14103 14102 case SATAC_SF_DISABLE_READ_AHEAD:
14104 14103 infop = "disabling read ahead failed";
14105 14104 break;
14106 14105 case SATAC_SF_ENABLE_WRITE_CACHE:
14107 14106 infop = "enabling write cache failed";
14108 14107 break;
14109 14108 case SATAC_SF_DISABLE_WRITE_CACHE:
14110 14109 infop = "disabling write cache failed";
14111 14110 break;
14112 14111 }
14113 14112 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
14114 14113 rval = SATA_RETRY;
14115 14114 }
14116 14115 failure:
14117 14116 /* Free allocated resources */
14118 14117 if (spkt != NULL)
14119 14118 sata_pkt_free(spx);
14120 14119 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14121 14120 return (rval);
14122 14121 }
14123 14122
14124 14123 /*
14125 14124 * Set Removable Media Status Notification (enable/disable)
14126 14125 * state == 0 , disable
14127 14126 * state != 0 , enable
14128 14127 *
14129 14128 * If operation fails, system log messgage is emitted.
14130 14129 * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
14131 14130 */
14132 14131
14133 14132 static int
14134 14133 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
14135 14134 int state)
14136 14135 {
14137 14136 sata_pkt_t *spkt;
14138 14137 sata_cmd_t *scmd;
14139 14138 sata_pkt_txlate_t *spx;
14140 14139 int rval = SATA_SUCCESS;
14141 14140 char *infop;
14142 14141
14143 14142 ASSERT(sdinfo != NULL);
14144 14143 ASSERT(sata_hba_inst != NULL);
14145 14144
14146 14145 /* Prepare packet for SET FEATURES COMMAND */
14147 14146 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14148 14147 spx->txlt_sata_hba_inst = sata_hba_inst;
14149 14148 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */
14150 14149 spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14151 14150 if (spkt == NULL) {
14152 14151 rval = SATA_FAILURE;
14153 14152 goto failure;
14154 14153 }
14155 14154 /* Fill sata_pkt */
14156 14155 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14157 14156 /* Timeout 30s */
14158 14157 spkt->satapkt_time = sata_default_pkt_time;
14159 14158 /* Synchronous mode, no callback, interrupts */
14160 14159 spkt->satapkt_op_mode =
14161 14160 SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14162 14161 spkt->satapkt_comp = NULL;
14163 14162 scmd = &spkt->satapkt_cmd;
14164 14163 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14165 14164 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14166 14165 scmd->satacmd_addr_type = 0;
14167 14166 scmd->satacmd_device_reg = 0;
14168 14167 scmd->satacmd_status_reg = 0;
14169 14168 scmd->satacmd_error_reg = 0;
14170 14169 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14171 14170 if (state == 0)
14172 14171 scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN;
14173 14172 else
14174 14173 scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN;
14175 14174
14176 14175 /* Transfer command to HBA */
14177 14176 if (((*SATA_START_FUNC(sata_hba_inst))(
14178 14177 SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
14179 14178 (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
14180 14179 /* Pkt execution failed */
14181 14180 if (state == 0)
14182 14181 infop = "disabling Removable Media Status "
14183 14182 "Notification failed";
14184 14183 else
14185 14184 infop = "enabling Removable Media Status "
14186 14185 "Notification failed";
14187 14186
14188 14187 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
14189 14188 rval = SATA_FAILURE;
14190 14189 }
14191 14190 failure:
14192 14191 /* Free allocated resources */
14193 14192 if (spkt != NULL)
14194 14193 sata_pkt_free(spx);
14195 14194 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14196 14195 return (rval);
14197 14196 }
14198 14197
14199 14198
14200 14199 /*
14201 14200 * Update state and copy port ss* values from passed sata_device structure.
14202 14201 * sata_address is validated - if not valid, nothing is changed in sata_scsi
14203 14202 * configuration struct.
14204 14203 *
14205 14204 * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function
14206 14205 * regardless of the state in device argument.
14207 14206 *
14208 14207 * Port mutex should be held while calling this function.
14209 14208 */
14210 14209 static void
14211 14210 sata_update_port_info(sata_hba_inst_t *sata_hba_inst,
14212 14211 sata_device_t *sata_device)
14213 14212 {
14214 14213 sata_cport_info_t *cportinfo;
14215 14214
14216 14215 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT ||
14217 14216 sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
14218 14217 if (SATA_NUM_CPORTS(sata_hba_inst) <=
14219 14218 sata_device->satadev_addr.cport)
14220 14219 return;
14221 14220
14222 14221 cportinfo = SATA_CPORT_INFO(sata_hba_inst,
14223 14222 sata_device->satadev_addr.cport);
14224 14223
14225 14224 ASSERT(mutex_owned(&cportinfo->cport_mutex));
14226 14225 cportinfo->cport_scr = sata_device->satadev_scr;
14227 14226
14228 14227 /* Preserve SATA_PSTATE_SHUTDOWN flag */
14229 14228 cportinfo->cport_state &= ~(SATA_PSTATE_PWRON |
14230 14229 SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
14231 14230 cportinfo->cport_state |=
14232 14231 sata_device->satadev_state & SATA_PSTATE_VALID;
14233 14232 }
14234 14233 }
14235 14234
14236 14235 void
14237 14236 sata_update_pmport_info(sata_hba_inst_t *sata_hba_inst,
14238 14237 sata_device_t *sata_device)
14239 14238 {
14240 14239 sata_pmport_info_t *pmportinfo;
14241 14240
14242 14241 if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT &&
14243 14242 sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) ||
14244 14243 SATA_NUM_PMPORTS(sata_hba_inst,
14245 14244 sata_device->satadev_addr.cport) <
14246 14245 sata_device->satadev_addr.pmport) {
14247 14246 SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
14248 14247 "sata_update_port_info: error address %p.",
14249 14248 &sata_device->satadev_addr);
14250 14249 return;
14251 14250 }
14252 14251
14253 14252 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
14254 14253 sata_device->satadev_addr.cport,
14255 14254 sata_device->satadev_addr.pmport);
14256 14255
14257 14256 ASSERT(mutex_owned(&pmportinfo->pmport_mutex));
14258 14257 pmportinfo->pmport_scr = sata_device->satadev_scr;
14259 14258
14260 14259 /* Preserve SATA_PSTATE_SHUTDOWN flag */
14261 14260 pmportinfo->pmport_state &=
14262 14261 ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
14263 14262 pmportinfo->pmport_state |=
14264 14263 sata_device->satadev_state & SATA_PSTATE_VALID;
14265 14264 }
14266 14265
14267 14266 /*
14268 14267 * Extract SATA port specification from an IOCTL argument.
14269 14268 *
14270 14269 * This function return the port the user land send us as is, unless it
14271 14270 * cannot retrieve port spec, then -1 is returned.
14272 14271 *
14273 14272 * Support port multiplier.
14274 14273 */
14275 14274 static int32_t
14276 14275 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp)
14277 14276 {
14278 14277 int32_t port;
14279 14278
14280 14279 /* Extract port number from nvpair in dca structure */
14281 14280 if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
14282 14281 SATA_LOG_D((sata_hba_inst, CE_NOTE,
14283 14282 "sata_get_port_num: invalid port spec 0x%x in ioctl",
14284 14283 port));
14285 14284 port = -1;
14286 14285 }
14287 14286
14288 14287 return (port);
14289 14288 }
14290 14289
14291 14290 /*
14292 14291 * Get dev_info_t pointer to the device node pointed to by port argument.
14293 14292 * NOTE: target argument is a value used in ioctls to identify
14294 14293 * the AP - it is not a sata_address.
14295 14294 * It is a combination of cport, pmport and address qualifier, encodded same
14296 14295 * way as a scsi target number.
14297 14296 * At this moment it carries only cport number.
14298 14297 *
14299 14298 * PMult hotplug is supported now.
14300 14299 *
14301 14300 * Returns dev_info_t pointer if target device was found, NULL otherwise.
14302 14301 */
14303 14302
14304 14303 static dev_info_t *
14305 14304 sata_get_target_dip(dev_info_t *dip, uint8_t cport, uint8_t pmport)
14306 14305 {
14307 14306 dev_info_t *cdip = NULL;
14308 14307 int target, tgt;
14309 14308 int circ;
14310 14309 uint8_t qual;
14311 14310
14312 14311 sata_hba_inst_t *sata_hba_inst;
14313 14312 scsi_hba_tran_t *scsi_hba_tran;
14314 14313
14315 14314 /* Get target id */
14316 14315 scsi_hba_tran = ddi_get_driver_private(dip);
14317 14316 if (scsi_hba_tran == NULL)
14318 14317 return (NULL);
14319 14318
14320 14319 sata_hba_inst = scsi_hba_tran->tran_hba_private;
14321 14320
14322 14321 if (sata_hba_inst == NULL)
14323 14322 return (NULL);
14324 14323
14325 14324 /* Identify a port-mult by cport_info.cport_dev_type */
14326 14325 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT)
14327 14326 qual = SATA_ADDR_DPMPORT;
14328 14327 else
14329 14328 qual = SATA_ADDR_DCPORT;
14330 14329
14331 14330 target = SATA_TO_SCSI_TARGET(cport, pmport, qual);
14332 14331
14333 14332 /* Retrieve target dip */
14334 14333 ndi_devi_enter(dip, &circ);
14335 14334 for (cdip = ddi_get_child(dip); cdip != NULL; ) {
14336 14335 dev_info_t *next = ddi_get_next_sibling(cdip);
14337 14336
14338 14337 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
14339 14338 DDI_PROP_DONTPASS, "target", -1);
14340 14339 if (tgt == -1) {
14341 14340 /*
14342 14341 * This is actually an error condition, but not
14343 14342 * a fatal one. Just continue the search.
14344 14343 */
14345 14344 cdip = next;
14346 14345 continue;
14347 14346 }
14348 14347
14349 14348 if (tgt == target)
14350 14349 break;
14351 14350
14352 14351 cdip = next;
14353 14352 }
14354 14353 ndi_devi_exit(dip, circ);
14355 14354
14356 14355 return (cdip);
14357 14356 }
14358 14357
14359 14358 /*
14360 14359 * Get dev_info_t pointer to the device node pointed to by port argument.
14361 14360 * NOTE: target argument is a value used in ioctls to identify
14362 14361 * the AP - it is not a sata_address.
14363 14362 * It is a combination of cport, pmport and address qualifier, encoded same
14364 14363 * way as a scsi target number.
14365 14364 *
14366 14365 * Returns dev_info_t pointer if target device was found, NULL otherwise.
14367 14366 */
14368 14367
14369 14368 static dev_info_t *
14370 14369 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr)
14371 14370 {
14372 14371 dev_info_t *cdip = NULL;
14373 14372 int target, tgt;
14374 14373 int circ;
14375 14374
14376 14375 target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual);
14377 14376
14378 14377 ndi_devi_enter(dip, &circ);
14379 14378 for (cdip = ddi_get_child(dip); cdip != NULL; ) {
14380 14379 dev_info_t *next = ddi_get_next_sibling(cdip);
14381 14380
14382 14381 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
14383 14382 DDI_PROP_DONTPASS, "target", -1);
14384 14383 if (tgt == -1) {
14385 14384 /*
14386 14385 * This is actually an error condition, but not
14387 14386 * a fatal one. Just continue the search.
14388 14387 */
14389 14388 cdip = next;
14390 14389 continue;
14391 14390 }
14392 14391
14393 14392 if (tgt == target)
14394 14393 break;
14395 14394
14396 14395 cdip = next;
14397 14396 }
14398 14397 ndi_devi_exit(dip, circ);
14399 14398
14400 14399 return (cdip);
14401 14400 }
14402 14401
14403 14402 /*
14404 14403 * Process sata port disconnect request.
14405 14404 * Normally, cfgadm sata plugin will try to offline (unconfigure) the device
14406 14405 * before this request. Nevertheless, if a device is still configured,
14407 14406 * we need to attempt to offline and unconfigure device.
14408 14407 * Regardless of the unconfigure operation results the port is marked as
14409 14408 * deactivated and no access to the attached device is possible.
14410 14409 * If the target node remains because unconfigure operation failed, its state
14411 14410 * will be set to DEVICE_REMOVED, preventing it to be used again when a device
14412 14411 * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure
14413 14412 * the device and remove old target node.
14414 14413 *
14415 14414 * This function invokes sata_hba_inst->satahba_tran->
14416 14415 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
14417 14416 * If successful, the device structure (if any) attached to the specified port
14418 14417 * is removed and state of the port marked appropriately.
14419 14418 * Failure of the port_deactivate may keep port in the physically active state,
14420 14419 * or may fail the port.
14421 14420 *
14422 14421 * NOTE: Port multiplier is supported.
14423 14422 */
14424 14423
14425 14424 static int
14426 14425 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst,
14427 14426 sata_device_t *sata_device)
14428 14427 {
14429 14428 sata_drive_info_t *sdinfo = NULL, *subsdinfo = NULL;
14430 14429 sata_cport_info_t *cportinfo = NULL;
14431 14430 sata_pmport_info_t *pmportinfo = NULL;
14432 14431 sata_pmult_info_t *pmultinfo = NULL;
14433 14432 sata_device_t subsdevice;
14434 14433 int cport, pmport, qual;
14435 14434 int rval = SATA_SUCCESS;
14436 14435 int npmport = 0;
14437 14436 int rv = 0;
14438 14437
14439 14438 cport = sata_device->satadev_addr.cport;
14440 14439 pmport = sata_device->satadev_addr.pmport;
14441 14440 qual = sata_device->satadev_addr.qual;
14442 14441
14443 14442 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
14444 14443 if (qual == SATA_ADDR_DCPORT)
14445 14444 qual = SATA_ADDR_CPORT;
14446 14445 else
14447 14446 qual = SATA_ADDR_PMPORT;
14448 14447
14449 14448 /*
14450 14449 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran->
14451 14450 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
14452 14451 * Do the sanity check.
14453 14452 */
14454 14453 if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
14455 14454 /* No physical port deactivation supported. */
14456 14455 return (EINVAL);
14457 14456 }
14458 14457
14459 14458 /* Check the current state of the port */
14460 14459 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14461 14460 (SATA_DIP(sata_hba_inst), sata_device);
14462 14461
14463 14462 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
14464 14463
14465 14464 /*
14466 14465 * Processing port mulitiplier
14467 14466 */
14468 14467 if (qual == SATA_ADDR_CPORT &&
14469 14468 SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) {
14470 14469 mutex_enter(&cportinfo->cport_mutex);
14471 14470
14472 14471 /* Check controller port status */
14473 14472 sata_update_port_info(sata_hba_inst, sata_device);
14474 14473 if (rval != SATA_SUCCESS ||
14475 14474 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14476 14475 /*
14477 14476 * Device port status is unknown or it is in failed
14478 14477 * state
14479 14478 */
14480 14479 SATA_CPORT_STATE(sata_hba_inst, cport) =
14481 14480 SATA_PSTATE_FAILED;
14482 14481 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14483 14482 "sata_hba_ioctl: connect: failed to deactivate "
14484 14483 "SATA port %d", cport);
14485 14484 mutex_exit(&cportinfo->cport_mutex);
14486 14485 return (EIO);
14487 14486 }
14488 14487
14489 14488 /* Disconnect all sub-devices. */
14490 14489 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
14491 14490 if (pmultinfo != NULL) {
14492 14491
14493 14492 for (npmport = 0; npmport < SATA_NUM_PMPORTS(
14494 14493 sata_hba_inst, cport); npmport ++) {
14495 14494 subsdinfo = SATA_PMPORT_DRV_INFO(
14496 14495 sata_hba_inst, cport, npmport);
14497 14496 if (subsdinfo == NULL)
14498 14497 continue;
14499 14498
14500 14499 subsdevice.satadev_addr = subsdinfo->
14501 14500 satadrv_addr;
14502 14501
14503 14502 mutex_exit(&cportinfo->cport_mutex);
14504 14503 if (sata_ioctl_disconnect(sata_hba_inst,
14505 14504 &subsdevice) == SATA_SUCCESS) {
14506 14505 SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
14507 14506 "[Remove] device at port %d:%d "
14508 14507 "successfully.", cport, npmport);
14509 14508 }
14510 14509 mutex_enter(&cportinfo->cport_mutex);
14511 14510 }
14512 14511 }
14513 14512
14514 14513 /* Disconnect the port multiplier */
14515 14514 cportinfo->cport_state &= ~SATA_STATE_READY;
14516 14515 mutex_exit(&cportinfo->cport_mutex);
14517 14516
14518 14517 sata_device->satadev_addr.qual = qual;
14519 14518 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
14520 14519 (SATA_DIP(sata_hba_inst), sata_device);
14521 14520
14522 14521 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14523 14522 SE_NO_HINT);
14524 14523
14525 14524 mutex_enter(&cportinfo->cport_mutex);
14526 14525 sata_update_port_info(sata_hba_inst, sata_device);
14527 14526 if (rval != SATA_SUCCESS &&
14528 14527 sata_device->satadev_state & SATA_PSTATE_FAILED) {
14529 14528 cportinfo->cport_state = SATA_PSTATE_FAILED;
14530 14529 rv = EIO;
14531 14530 } else {
14532 14531 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
14533 14532 }
14534 14533 mutex_exit(&cportinfo->cport_mutex);
14535 14534
14536 14535 return (rv);
14537 14536 }
14538 14537
14539 14538 /*
14540 14539 * Process non-port-multiplier device - it could be a drive connected
14541 14540 * to a port multiplier port or a controller port.
14542 14541 */
14543 14542 if (qual == SATA_ADDR_PMPORT) {
14544 14543 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14545 14544 mutex_enter(&pmportinfo->pmport_mutex);
14546 14545 sata_update_pmport_info(sata_hba_inst, sata_device);
14547 14546 if (rval != SATA_SUCCESS ||
14548 14547 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14549 14548 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
14550 14549 SATA_PSTATE_FAILED;
14551 14550 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
14552 14551 "sata_hba_ioctl: connect: failed to deactivate "
14553 14552 "SATA port %d:%d", cport, pmport);
14554 14553 mutex_exit(&pmportinfo->pmport_mutex);
14555 14554 return (EIO);
14556 14555 }
14557 14556
14558 14557 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
14559 14558 sdinfo = pmportinfo->pmport_sata_drive;
14560 14559 ASSERT(sdinfo != NULL);
14561 14560 }
14562 14561
14563 14562 /*
14564 14563 * Set port's dev_state to not ready - this will disable
14565 14564 * an access to a potentially attached device.
14566 14565 */
14567 14566 pmportinfo->pmport_state &= ~SATA_STATE_READY;
14568 14567
14569 14568 /* Remove and release sata_drive info structure. */
14570 14569 if (sdinfo != NULL) {
14571 14570 if ((sdinfo->satadrv_type &
14572 14571 SATA_VALID_DEV_TYPE) != 0) {
14573 14572 /*
14574 14573 * If a target node exists, try to offline
14575 14574 * a device and remove target node.
14576 14575 */
14577 14576 mutex_exit(&pmportinfo->pmport_mutex);
14578 14577 (void) sata_offline_device(sata_hba_inst,
14579 14578 sata_device, sdinfo);
14580 14579 mutex_enter(&pmportinfo->pmport_mutex);
14581 14580 }
14582 14581
14583 14582 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
14584 14583 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
14585 14584 (void) kmem_free((void *)sdinfo,
14586 14585 sizeof (sata_drive_info_t));
14587 14586 }
14588 14587 mutex_exit(&pmportinfo->pmport_mutex);
14589 14588
14590 14589 } else if (qual == SATA_ADDR_CPORT) {
14591 14590 mutex_enter(&cportinfo->cport_mutex);
14592 14591 sata_update_port_info(sata_hba_inst, sata_device);
14593 14592 if (rval != SATA_SUCCESS ||
14594 14593 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14595 14594 /*
14596 14595 * Device port status is unknown or it is in failed
14597 14596 * state
14598 14597 */
14599 14598 SATA_CPORT_STATE(sata_hba_inst, cport) =
14600 14599 SATA_PSTATE_FAILED;
14601 14600 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14602 14601 "sata_hba_ioctl: connect: failed to deactivate "
14603 14602 "SATA port %d", cport);
14604 14603 mutex_exit(&cportinfo->cport_mutex);
14605 14604 return (EIO);
14606 14605 }
14607 14606
14608 14607 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
14609 14608 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
14610 14609 ASSERT(pmultinfo != NULL);
14611 14610 } else if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
14612 14611 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14613 14612 ASSERT(sdinfo != NULL);
14614 14613 }
14615 14614 cportinfo->cport_state &= ~SATA_STATE_READY;
14616 14615
14617 14616 if (sdinfo != NULL) {
14618 14617 if ((sdinfo->satadrv_type &
14619 14618 SATA_VALID_DEV_TYPE) != 0) {
14620 14619 /*
14621 14620 * If a target node exists, try to offline
14622 14621 * a device and remove target node.
14623 14622 */
14624 14623 mutex_exit(&cportinfo->cport_mutex);
14625 14624 (void) sata_offline_device(sata_hba_inst,
14626 14625 sata_device, sdinfo);
14627 14626 mutex_enter(&cportinfo->cport_mutex);
14628 14627 }
14629 14628
14630 14629 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14631 14630 cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14632 14631 (void) kmem_free((void *)sdinfo,
14633 14632 sizeof (sata_drive_info_t));
14634 14633 }
14635 14634 mutex_exit(&cportinfo->cport_mutex);
14636 14635 }
14637 14636
14638 14637 /* Just ask HBA driver to deactivate port */
14639 14638 sata_device->satadev_addr.qual = qual;
14640 14639
14641 14640 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
14642 14641 (SATA_DIP(sata_hba_inst), sata_device);
14643 14642
14644 14643 /*
14645 14644 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14646 14645 * without the hint (to force listener to investivate the state).
14647 14646 */
14648 14647 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14649 14648 SE_NO_HINT);
14650 14649
14651 14650 if (qual == SATA_ADDR_PMPORT) {
14652 14651 mutex_enter(&pmportinfo->pmport_mutex);
14653 14652 sata_update_pmport_info(sata_hba_inst, sata_device);
14654 14653
14655 14654 if (rval != SATA_SUCCESS &&
14656 14655 sata_device->satadev_state & SATA_PSTATE_FAILED) {
14657 14656 /*
14658 14657 * Port deactivation failure - do not change port
14659 14658 * state unless the state returned by HBA indicates a
14660 14659 * port failure.
14661 14660 *
14662 14661 * NOTE: device structures were released, so devices
14663 14662 * now are invisible! Port reset is needed to
14664 14663 * re-enumerate devices.
14665 14664 */
14666 14665 pmportinfo->pmport_state = SATA_PSTATE_FAILED;
14667 14666 rv = EIO;
14668 14667 } else {
14669 14668 /*
14670 14669 * Deactivation succeded. From now on the sata framework
14671 14670 * will not care what is happening to the device, until
14672 14671 * the port is activated again.
14673 14672 */
14674 14673 pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
14675 14674 }
14676 14675 mutex_exit(&pmportinfo->pmport_mutex);
14677 14676 } else if (qual == SATA_ADDR_CPORT) {
14678 14677 mutex_enter(&cportinfo->cport_mutex);
14679 14678 sata_update_port_info(sata_hba_inst, sata_device);
14680 14679
14681 14680 if (rval != SATA_SUCCESS &&
14682 14681 sata_device->satadev_state & SATA_PSTATE_FAILED) {
14683 14682 cportinfo->cport_state = SATA_PSTATE_FAILED;
14684 14683 rv = EIO;
14685 14684 } else {
14686 14685 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
14687 14686 }
14688 14687 mutex_exit(&cportinfo->cport_mutex);
14689 14688 }
14690 14689
14691 14690 return (rv);
14692 14691 }
14693 14692
14694 14693
14695 14694
14696 14695 /*
14697 14696 * Process sata port connect request
14698 14697 * The sata cfgadm pluging will invoke this operation only if port was found
14699 14698 * in the disconnect state (failed state is also treated as the disconnected
14700 14699 * state).
14701 14700 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->satahba_tran->
14702 14701 * sata_tran_hotplug_ops->sata_tran_port_activate().
14703 14702 * If successful and a device is found attached to the port,
14704 14703 * the initialization sequence is executed to attach a device structure to
14705 14704 * a port structure. The state of the port and a device would be set
14706 14705 * appropriately.
14707 14706 * The device is not set in configured state (system-wise) by this operation.
14708 14707 *
14709 14708 * Note, that activating the port may generate link events,
14710 14709 * so it is important that following processing and the
14711 14710 * event processing does not interfere with each other!
14712 14711 *
14713 14712 * This operation may remove port failed state and will
14714 14713 * try to make port active and in good standing.
14715 14714 *
14716 14715 * NOTE: Port multiplier is supported.
14717 14716 */
14718 14717
14719 14718 static int
14720 14719 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst,
14721 14720 sata_device_t *sata_device)
14722 14721 {
14723 14722 sata_pmport_info_t *pmportinfo = NULL;
14724 14723 uint8_t cport, pmport, qual;
14725 14724 int rv = 0;
14726 14725
14727 14726 cport = sata_device->satadev_addr.cport;
14728 14727 pmport = sata_device->satadev_addr.pmport;
14729 14728 qual = sata_device->satadev_addr.qual;
14730 14729
14731 14730 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
14732 14731 if (qual == SATA_ADDR_DCPORT)
14733 14732 qual = SATA_ADDR_CPORT;
14734 14733 else
14735 14734 qual = SATA_ADDR_PMPORT;
14736 14735
14737 14736 if (qual == SATA_ADDR_PMPORT)
14738 14737 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14739 14738
14740 14739 /*
14741 14740 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->
14742 14741 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate().
14743 14742 * Perform sanity check now.
14744 14743 */
14745 14744 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) {
14746 14745 /* No physical port activation supported. */
14747 14746 return (EINVAL);
14748 14747 }
14749 14748
14750 14749 /* Just ask HBA driver to activate port */
14751 14750 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
14752 14751 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
14753 14752 /*
14754 14753 * Port activation failure.
14755 14754 */
14756 14755 if (qual == SATA_ADDR_CPORT) {
14757 14756 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14758 14757 cport)->cport_mutex);
14759 14758 sata_update_port_info(sata_hba_inst, sata_device);
14760 14759 if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
14761 14760 SATA_CPORT_STATE(sata_hba_inst, cport) =
14762 14761 SATA_PSTATE_FAILED;
14763 14762 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14764 14763 "sata_hba_ioctl: connect: failed to "
14765 14764 "activate SATA port %d", cport);
14766 14765 }
14767 14766 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14768 14767 cport)->cport_mutex);
14769 14768 } else { /* port multiplier device port */
14770 14769 mutex_enter(&pmportinfo->pmport_mutex);
14771 14770 sata_update_pmport_info(sata_hba_inst, sata_device);
14772 14771 if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
14773 14772 SATA_PMPORT_STATE(sata_hba_inst, cport,
14774 14773 pmport) = SATA_PSTATE_FAILED;
14775 14774 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
14776 14775 "sata_hba_ioctl: connect: failed to "
14777 14776 "activate SATA port %d:%d", cport, pmport);
14778 14777 }
14779 14778 mutex_exit(&pmportinfo->pmport_mutex);
14780 14779 }
14781 14780 return (EIO);
14782 14781 }
14783 14782
14784 14783 /* Virgin port state - will be updated by the port re-probe. */
14785 14784 if (qual == SATA_ADDR_CPORT) {
14786 14785 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14787 14786 cport)->cport_mutex);
14788 14787 SATA_CPORT_STATE(sata_hba_inst, cport) = 0;
14789 14788 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14790 14789 cport)->cport_mutex);
14791 14790 } else { /* port multiplier device port */
14792 14791 mutex_enter(&pmportinfo->pmport_mutex);
14793 14792 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0;
14794 14793 mutex_exit(&pmportinfo->pmport_mutex);
14795 14794 }
14796 14795
14797 14796 /*
14798 14797 * Probe the port to find its state and attached device.
14799 14798 */
14800 14799 if (sata_reprobe_port(sata_hba_inst, sata_device,
14801 14800 SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE)
14802 14801 rv = EIO;
14803 14802
14804 14803 /*
14805 14804 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14806 14805 * without the hint
14807 14806 */
14808 14807 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14809 14808 SE_NO_HINT);
14810 14809
14811 14810 /*
14812 14811 * If there is a device attached to the port, emit
14813 14812 * a message.
14814 14813 */
14815 14814 if (sata_device->satadev_type != SATA_DTYPE_NONE) {
14816 14815
14817 14816 if (qual == SATA_ADDR_CPORT) {
14818 14817 if (sata_device->satadev_type == SATA_DTYPE_PMULT) {
14819 14818 sata_log(sata_hba_inst, CE_WARN,
14820 14819 "SATA port multiplier detected "
14821 14820 "at port %d", cport);
14822 14821 } else {
14823 14822 sata_log(sata_hba_inst, CE_WARN,
14824 14823 "SATA device detected at port %d", cport);
14825 14824 if (sata_device->satadev_type ==
14826 14825 SATA_DTYPE_UNKNOWN) {
14827 14826 /*
14828 14827 * A device was not successfully identified
14829 14828 */
14830 14829 sata_log(sata_hba_inst, CE_WARN,
14831 14830 "Could not identify SATA "
14832 14831 "device at port %d", cport);
14833 14832 }
14834 14833 }
14835 14834 } else { /* port multiplier device port */
14836 14835 sata_log(sata_hba_inst, CE_WARN,
14837 14836 "SATA device detected at port %d:%d",
14838 14837 cport, pmport);
14839 14838 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
14840 14839 /*
14841 14840 * A device was not successfully identified
14842 14841 */
14843 14842 sata_log(sata_hba_inst, CE_WARN,
14844 14843 "Could not identify SATA "
14845 14844 "device at port %d:%d", cport, pmport);
14846 14845 }
14847 14846 }
14848 14847 }
14849 14848
14850 14849 return (rv);
14851 14850 }
14852 14851
14853 14852
14854 14853 /*
14855 14854 * Process sata device unconfigure request.
14856 14855 * The unconfigure operation uses generic nexus operation to
14857 14856 * offline a device. It leaves a target device node attached.
14858 14857 * and obviously sata_drive_info attached as well, because
14859 14858 * from the hardware point of view nothing has changed.
14860 14859 */
14861 14860 static int
14862 14861 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst,
14863 14862 sata_device_t *sata_device)
14864 14863 {
14865 14864 int rv = 0;
14866 14865 dev_info_t *tdip;
14867 14866
14868 14867 /* We are addressing attached device, not a port */
14869 14868 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
14870 14869 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
14871 14870 else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT)
14872 14871 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
14873 14872
14874 14873 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
14875 14874 &sata_device->satadev_addr)) != NULL) {
14876 14875
14877 14876 if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) {
14878 14877 SATA_LOG_D((sata_hba_inst, CE_WARN,
14879 14878 "sata_hba_ioctl: unconfigure: "
14880 14879 "failed to unconfigure device at SATA port %d:%d",
14881 14880 sata_device->satadev_addr.cport,
14882 14881 sata_device->satadev_addr.pmport));
14883 14882 rv = EIO;
14884 14883 }
14885 14884 /*
14886 14885 * The target node devi_state should be marked with
14887 14886 * DEVI_DEVICE_OFFLINE by ndi_devi_offline().
14888 14887 * This would be the indication for cfgadm that
14889 14888 * the AP node occupant state is 'unconfigured'.
14890 14889 */
14891 14890
14892 14891 } else {
14893 14892 /*
14894 14893 * This would indicate a failure on the part of cfgadm
14895 14894 * to detect correct state of the node prior to this
14896 14895 * call - one cannot unconfigure non-existing device.
14897 14896 */
14898 14897 SATA_LOG_D((sata_hba_inst, CE_WARN,
14899 14898 "sata_hba_ioctl: unconfigure: "
14900 14899 "attempt to unconfigure non-existing device "
14901 14900 "at SATA port %d:%d",
14902 14901 sata_device->satadev_addr.cport,
14903 14902 sata_device->satadev_addr.pmport));
14904 14903 rv = ENXIO;
14905 14904 }
14906 14905 return (rv);
14907 14906 }
14908 14907
14909 14908 /*
14910 14909 * Process sata device configure request
14911 14910 * If port is in a failed state, operation is aborted - one has to use
14912 14911 * an explicit connect or port activate request to try to get a port into
14913 14912 * non-failed mode. Port reset wil also work in such situation.
14914 14913 * If the port is in disconnected (shutdown) state, the connect operation is
14915 14914 * attempted prior to any other action.
14916 14915 * When port is in the active state, there is a device attached and the target
14917 14916 * node exists, a device was most likely offlined.
14918 14917 * If target node does not exist, a new target node is created. In both cases
14919 14918 * an attempt is made to online (configure) the device.
14920 14919 *
14921 14920 * NOTE: Port multiplier is supported.
14922 14921 */
14923 14922 static int
14924 14923 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst,
14925 14924 sata_device_t *sata_device)
14926 14925 {
14927 14926 int cport, pmport, qual;
14928 14927 int rval;
14929 14928 boolean_t target = B_TRUE;
14930 14929 sata_cport_info_t *cportinfo;
14931 14930 sata_pmport_info_t *pmportinfo = NULL;
14932 14931 dev_info_t *tdip;
14933 14932 sata_drive_info_t *sdinfo;
14934 14933
14935 14934 cport = sata_device->satadev_addr.cport;
14936 14935 pmport = sata_device->satadev_addr.pmport;
14937 14936 qual = sata_device->satadev_addr.qual;
14938 14937
14939 14938 /* Get current port state */
14940 14939 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14941 14940 (SATA_DIP(sata_hba_inst), sata_device);
14942 14941
14943 14942 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
14944 14943 if (qual == SATA_ADDR_DPMPORT) {
14945 14944 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14946 14945 mutex_enter(&pmportinfo->pmport_mutex);
14947 14946 sata_update_pmport_info(sata_hba_inst, sata_device);
14948 14947 if (rval != SATA_SUCCESS ||
14949 14948 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14950 14949 /*
14951 14950 * Obviously, device on a failed port is not visible
14952 14951 */
14953 14952 mutex_exit(&pmportinfo->pmport_mutex);
14954 14953 return (ENXIO);
14955 14954 }
14956 14955 mutex_exit(&pmportinfo->pmport_mutex);
14957 14956 } else {
14958 14957 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14959 14958 cport)->cport_mutex);
14960 14959 sata_update_port_info(sata_hba_inst, sata_device);
14961 14960 if (rval != SATA_SUCCESS ||
14962 14961 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14963 14962 /*
14964 14963 * Obviously, device on a failed port is not visible
14965 14964 */
14966 14965 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14967 14966 cport)->cport_mutex);
14968 14967 return (ENXIO);
14969 14968 }
14970 14969 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14971 14970 cport)->cport_mutex);
14972 14971 }
14973 14972
14974 14973 if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) {
14975 14974 /* need to activate port */
14976 14975 target = B_FALSE;
14977 14976
14978 14977 /* Sanity check */
14979 14978 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
14980 14979 return (ENXIO);
14981 14980
14982 14981 /* Just let HBA driver to activate port */
14983 14982 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
14984 14983 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
14985 14984 /*
14986 14985 * Port activation failure - do not change port state
14987 14986 * unless the state returned by HBA indicates a port
14988 14987 * failure.
14989 14988 */
14990 14989 if (qual == SATA_ADDR_DPMPORT) {
14991 14990 mutex_enter(&pmportinfo->pmport_mutex);
14992 14991 sata_update_pmport_info(sata_hba_inst,
14993 14992 sata_device);
14994 14993 if (sata_device->satadev_state &
14995 14994 SATA_PSTATE_FAILED)
14996 14995 pmportinfo->pmport_state =
14997 14996 SATA_PSTATE_FAILED;
14998 14997 mutex_exit(&pmportinfo->pmport_mutex);
14999 14998 } else {
15000 14999 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15001 15000 cport)->cport_mutex);
15002 15001 sata_update_port_info(sata_hba_inst,
15003 15002 sata_device);
15004 15003 if (sata_device->satadev_state &
15005 15004 SATA_PSTATE_FAILED)
15006 15005 cportinfo->cport_state =
15007 15006 SATA_PSTATE_FAILED;
15008 15007 mutex_exit(&SATA_CPORT_INFO(
15009 15008 sata_hba_inst, cport)->cport_mutex);
15010 15009 }
15011 15010 }
15012 15011 SATA_LOG_D((sata_hba_inst, CE_WARN,
15013 15012 "sata_hba_ioctl: configure: "
15014 15013 "failed to activate SATA port %d:%d",
15015 15014 cport, pmport));
15016 15015 return (EIO);
15017 15016 }
15018 15017 /*
15019 15018 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15020 15019 * without the hint.
15021 15020 */
15022 15021 sata_gen_sysevent(sata_hba_inst,
15023 15022 &sata_device->satadev_addr, SE_NO_HINT);
15024 15023
15025 15024 /* Virgin port state */
15026 15025 if (qual == SATA_ADDR_DPMPORT) {
15027 15026 mutex_enter(&pmportinfo->pmport_mutex);
15028 15027 pmportinfo->pmport_state = 0;
15029 15028 mutex_exit(&pmportinfo->pmport_mutex);
15030 15029 } else {
15031 15030 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15032 15031 cport)-> cport_mutex);
15033 15032 cportinfo->cport_state = 0;
15034 15033 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15035 15034 cport)->cport_mutex);
15036 15035 }
15037 15036 /*
15038 15037 * Always reprobe port, to get current device info.
15039 15038 */
15040 15039 if (sata_reprobe_port(sata_hba_inst, sata_device,
15041 15040 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
15042 15041 return (EIO);
15043 15042
15044 15043 if (sata_device->satadev_type != SATA_DTYPE_NONE && target == B_FALSE) {
15045 15044 if (qual == SATA_ADDR_DPMPORT) {
15046 15045 /*
15047 15046 * That's the transition from "inactive" port
15048 15047 * to active one with device attached.
15049 15048 */
15050 15049 sata_log(sata_hba_inst, CE_WARN,
15051 15050 "SATA device detected at port %d:%d",
15052 15051 cport, pmport);
15053 15052 } else {
15054 15053 /*
15055 15054 * When PM is attached to the cport and cport is
15056 15055 * activated, every PM device port needs to be reprobed.
15057 15056 * We need to emit message for all devices detected
15058 15057 * at port multiplier's device ports.
15059 15058 * Add such code here.
15060 15059 * For now, just inform about device attached to
15061 15060 * cport.
15062 15061 */
15063 15062 sata_log(sata_hba_inst, CE_WARN,
15064 15063 "SATA device detected at port %d", cport);
15065 15064 }
15066 15065 }
15067 15066
15068 15067 /*
15069 15068 * This is where real configuration operation starts.
15070 15069 *
15071 15070 * When PM is attached to the cport and cport is activated,
15072 15071 * devices attached PM device ports may have to be configured
15073 15072 * explicitly. This may change when port multiplier is supported.
15074 15073 * For now, configure only disks and other valid target devices.
15075 15074 */
15076 15075 if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) {
15077 15076 if (qual == SATA_ADDR_DCPORT) {
15078 15077 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
15079 15078 /*
15080 15079 * A device was not successfully identified
15081 15080 */
15082 15081 sata_log(sata_hba_inst, CE_WARN,
15083 15082 "Could not identify SATA "
15084 15083 "device at port %d", cport);
15085 15084 }
15086 15085 } else { /* port multiplier device port */
15087 15086 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
15088 15087 /*
15089 15088 * A device was not successfully identified
15090 15089 */
15091 15090 sata_log(sata_hba_inst, CE_WARN,
15092 15091 "Could not identify SATA "
15093 15092 "device at port %d:%d", cport, pmport);
15094 15093 }
15095 15094 }
15096 15095 return (ENXIO); /* No device to configure */
15097 15096 }
15098 15097
15099 15098 /*
15100 15099 * Here we may have a device in reset condition,
15101 15100 * but because we are just configuring it, there is
15102 15101 * no need to process the reset other than just
15103 15102 * to clear device reset condition in the HBA driver.
15104 15103 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will
15105 15104 * cause a first command sent the HBA driver with the request
15106 15105 * to clear device reset condition.
15107 15106 */
15108 15107 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15109 15108 if (qual == SATA_ADDR_DPMPORT)
15110 15109 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15111 15110 else
15112 15111 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
15113 15112 sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
15114 15113 if (sdinfo == NULL) {
15115 15114 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15116 15115 return (ENXIO);
15117 15116 }
15118 15117 if (sdinfo->satadrv_event_flags &
15119 15118 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
15120 15119 sdinfo->satadrv_event_flags = 0;
15121 15120 }
15122 15121 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
15123 15122 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15124 15123
15125 15124 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
15126 15125 &sata_device->satadev_addr)) != NULL) {
15127 15126 /*
15128 15127 * Target node exists. Verify, that it belongs
15129 15128 * to existing, attached device and not to
15130 15129 * a removed device.
15131 15130 */
15132 15131 if (sata_check_device_removed(tdip) == B_TRUE) {
15133 15132 if (qual == SATA_ADDR_DPMPORT)
15134 15133 sata_log(sata_hba_inst, CE_WARN,
15135 15134 "SATA device at port %d cannot be "
15136 15135 "configured. "
15137 15136 "Application(s) accessing "
15138 15137 "previously attached device "
15139 15138 "have to release it before newly "
15140 15139 "inserted device can be made accessible.",
15141 15140 cport);
15142 15141 else
15143 15142 sata_log(sata_hba_inst, CE_WARN,
15144 15143 "SATA device at port %d:%d cannot be"
15145 15144 "configured. "
15146 15145 "Application(s) accessing "
15147 15146 "previously attached device "
15148 15147 "have to release it before newly "
15149 15148 "inserted device can be made accessible.",
15150 15149 cport, pmport);
15151 15150 return (EIO);
15152 15151 }
15153 15152 /*
15154 15153 * Device was not removed and re-inserted.
15155 15154 * Try to online it.
15156 15155 */
15157 15156 if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) {
15158 15157 SATA_LOG_D((sata_hba_inst, CE_WARN,
15159 15158 "sata_hba_ioctl: configure: "
15160 15159 "onlining device at SATA port "
15161 15160 "%d:%d failed", cport, pmport));
15162 15161 return (EIO);
15163 15162 }
15164 15163
15165 15164 if (qual == SATA_ADDR_DPMPORT) {
15166 15165 mutex_enter(&pmportinfo->pmport_mutex);
15167 15166 pmportinfo->pmport_tgtnode_clean = B_TRUE;
15168 15167 mutex_exit(&pmportinfo->pmport_mutex);
15169 15168 } else {
15170 15169 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15171 15170 cport)->cport_mutex);
15172 15171 cportinfo-> cport_tgtnode_clean = B_TRUE;
15173 15172 mutex_exit(&SATA_CPORT_INFO(
15174 15173 sata_hba_inst, cport)->cport_mutex);
15175 15174 }
15176 15175 } else {
15177 15176 /*
15178 15177 * No target node - need to create a new target node.
15179 15178 */
15180 15179 if (qual == SATA_ADDR_DPMPORT) {
15181 15180 mutex_enter(&pmportinfo->pmport_mutex);
15182 15181 pmportinfo->pmport_tgtnode_clean = B_TRUE;
15183 15182 mutex_exit(&pmportinfo->pmport_mutex);
15184 15183 } else {
15185 15184 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15186 15185 cport_mutex);
15187 15186 cportinfo-> cport_tgtnode_clean = B_TRUE;
15188 15187 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15189 15188 cport_mutex);
15190 15189 }
15191 15190
15192 15191 tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
15193 15192 sata_hba_inst, &sata_device->satadev_addr);
15194 15193 if (tdip == NULL) {
15195 15194 /* Configure operation failed */
15196 15195 SATA_LOG_D((sata_hba_inst, CE_WARN,
15197 15196 "sata_hba_ioctl: configure: "
15198 15197 "configuring SATA device at port %d:%d "
15199 15198 "failed", cport, pmport));
15200 15199 return (EIO);
15201 15200 }
15202 15201 }
15203 15202 return (0);
15204 15203 }
15205 15204
15206 15205
15207 15206 /*
15208 15207 * Process ioctl deactivate port request.
15209 15208 * Arbitrarily unconfigure attached device, if any.
15210 15209 * Even if the unconfigure fails, proceed with the
15211 15210 * port deactivation.
15212 15211 *
15213 15212 * NOTE: Port Multiplier is supported now.
15214 15213 */
15215 15214
15216 15215 static int
15217 15216 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst,
15218 15217 sata_device_t *sata_device)
15219 15218 {
15220 15219 int cport, pmport, qual;
15221 15220 int rval, rv = 0;
15222 15221 int npmport;
15223 15222 sata_cport_info_t *cportinfo;
15224 15223 sata_pmport_info_t *pmportinfo;
15225 15224 sata_pmult_info_t *pmultinfo;
15226 15225 dev_info_t *tdip;
15227 15226 sata_drive_info_t *sdinfo = NULL;
15228 15227 sata_device_t subsdevice;
15229 15228
15230 15229 /* Sanity check */
15231 15230 if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL)
15232 15231 return (ENOTSUP);
15233 15232
15234 15233 cport = sata_device->satadev_addr.cport;
15235 15234 pmport = sata_device->satadev_addr.pmport;
15236 15235 qual = sata_device->satadev_addr.qual;
15237 15236
15238 15237 /* SCSI_TO_SATA_ADDR_QUAL() translate ap_id into a device qualifier */
15239 15238 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
15240 15239 if (qual == SATA_ADDR_DCPORT)
15241 15240 qual = SATA_ADDR_CPORT;
15242 15241 else
15243 15242 qual = SATA_ADDR_PMPORT;
15244 15243
15245 15244 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
15246 15245 if (qual == SATA_ADDR_PMPORT)
15247 15246 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
15248 15247
15249 15248 /*
15250 15249 * Processing port multiplier
15251 15250 */
15252 15251 if (qual == SATA_ADDR_CPORT &&
15253 15252 SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) {
15254 15253 mutex_enter(&cportinfo->cport_mutex);
15255 15254
15256 15255 /* Deactivate all sub-deices */
15257 15256 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
15258 15257 if (pmultinfo != NULL) {
15259 15258 for (npmport = 0; npmport < SATA_NUM_PMPORTS(
15260 15259 sata_hba_inst, cport); npmport++) {
15261 15260
15262 15261 subsdevice.satadev_addr.cport = cport;
15263 15262 subsdevice.satadev_addr.pmport =
15264 15263 (uint8_t)npmport;
15265 15264 subsdevice.satadev_addr.qual =
15266 15265 SATA_ADDR_DPMPORT;
15267 15266
15268 15267 SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
15269 15268 "sata_hba_ioctl: deactivate: trying to "
15270 15269 "deactivate SATA port %d:%d",
15271 15270 cport, npmport);
15272 15271
15273 15272 mutex_exit(&cportinfo->cport_mutex);
15274 15273 if (sata_ioctl_deactivate(sata_hba_inst,
15275 15274 &subsdevice) == SATA_SUCCESS) {
15276 15275 SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
15277 15276 "[Deactivate] device at port %d:%d "
15278 15277 "successfully.", cport, npmport);
15279 15278 }
15280 15279 mutex_enter(&cportinfo->cport_mutex);
15281 15280 }
15282 15281 }
15283 15282
15284 15283 /* Deactivate the port multiplier now. */
15285 15284 cportinfo->cport_state &= ~SATA_STATE_READY;
15286 15285 mutex_exit(&cportinfo->cport_mutex);
15287 15286
15288 15287 sata_device->satadev_addr.qual = qual;
15289 15288 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
15290 15289 (SATA_DIP(sata_hba_inst), sata_device);
15291 15290
15292 15291 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15293 15292 SE_NO_HINT);
15294 15293
15295 15294 mutex_enter(&cportinfo->cport_mutex);
15296 15295 sata_update_port_info(sata_hba_inst, sata_device);
15297 15296 if (rval != SATA_SUCCESS) {
15298 15297 if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15299 15298 cportinfo->cport_state = SATA_PSTATE_FAILED;
15300 15299 }
15301 15300 rv = EIO;
15302 15301 } else {
15303 15302 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
15304 15303 }
15305 15304 mutex_exit(&cportinfo->cport_mutex);
15306 15305
15307 15306 return (rv);
15308 15307 }
15309 15308
15310 15309 /*
15311 15310 * Process non-port-multiplier device - it could be a drive connected
15312 15311 * to a port multiplier port or a controller port.
15313 15312 */
15314 15313 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15315 15314 if (qual == SATA_ADDR_CPORT) {
15316 15315 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
15317 15316 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
15318 15317 /* deal only with valid devices */
15319 15318 if ((cportinfo->cport_dev_type &
15320 15319 SATA_VALID_DEV_TYPE) != 0)
15321 15320 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
15322 15321 }
15323 15322 cportinfo->cport_state &= ~SATA_STATE_READY;
15324 15323 } else {
15325 15324 /* Port multiplier device port */
15326 15325 mutex_enter(&pmportinfo->pmport_mutex);
15327 15326 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15328 15327 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
15329 15328 (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0)
15330 15329 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
15331 15330 pmportinfo->pmport_state &= ~SATA_STATE_READY;
15332 15331 mutex_exit(&pmportinfo->pmport_mutex);
15333 15332 }
15334 15333
15335 15334 if (sdinfo != NULL) {
15336 15335 /*
15337 15336 * If a target node exists, try to offline a device and
15338 15337 * to remove a target node.
15339 15338 */
15340 15339 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15341 15340 cport_mutex);
15342 15341 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
15343 15342 &sata_device->satadev_addr);
15344 15343 if (tdip != NULL) {
15345 15344 /* target node exist */
15346 15345 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
15347 15346 "sata_hba_ioctl: port deactivate: "
15348 15347 "target node exists.", NULL);
15349 15348
15350 15349 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) !=
15351 15350 NDI_SUCCESS) {
15352 15351 SATA_LOG_D((sata_hba_inst, CE_WARN,
15353 15352 "sata_hba_ioctl: port deactivate: "
15354 15353 "failed to unconfigure device at port "
15355 15354 "%d:%d before deactivating the port",
15356 15355 cport, pmport));
15357 15356 /*
15358 15357 * Set DEVICE REMOVED state in the target
15359 15358 * node. It will prevent an access to
15360 15359 * the device even when a new device is
15361 15360 * attached, until the old target node is
15362 15361 * released, removed and recreated for a new
15363 15362 * device.
15364 15363 */
15365 15364 sata_set_device_removed(tdip);
15366 15365
15367 15366 /*
15368 15367 * Instruct the event daemon to try the
15369 15368 * target node cleanup later.
15370 15369 */
15371 15370 sata_set_target_node_cleanup(sata_hba_inst,
15372 15371 &sata_device->satadev_addr);
15373 15372 }
15374 15373 }
15375 15374 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15376 15375 cport_mutex);
15377 15376 /*
15378 15377 * In any case, remove and release sata_drive_info
15379 15378 * structure.
15380 15379 */
15381 15380 if (qual == SATA_ADDR_CPORT) {
15382 15381 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
15383 15382 cportinfo->cport_dev_type = SATA_DTYPE_NONE;
15384 15383 } else { /* port multiplier device port */
15385 15384 mutex_enter(&pmportinfo->pmport_mutex);
15386 15385 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
15387 15386 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
15388 15387 mutex_exit(&pmportinfo->pmport_mutex);
15389 15388 }
15390 15389 (void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
15391 15390 }
15392 15391
15393 15392 if (qual == SATA_ADDR_CPORT) {
15394 15393 cportinfo->cport_state &= ~(SATA_STATE_PROBED |
15395 15394 SATA_STATE_PROBING);
15396 15395 } else if (qual == SATA_ADDR_PMPORT) {
15397 15396 mutex_enter(&pmportinfo->pmport_mutex);
15398 15397 pmportinfo->pmport_state &= ~(SATA_STATE_PROBED |
15399 15398 SATA_STATE_PROBING);
15400 15399 mutex_exit(&pmportinfo->pmport_mutex);
15401 15400 }
15402 15401 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15403 15402
15404 15403 /* Just let HBA driver to deactivate port */
15405 15404 sata_device->satadev_addr.qual = qual;
15406 15405 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
15407 15406 (SATA_DIP(sata_hba_inst), sata_device);
15408 15407
15409 15408 /*
15410 15409 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15411 15410 * without the hint
15412 15411 */
15413 15412 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15414 15413 SE_NO_HINT);
15415 15414
15416 15415 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15417 15416 sata_update_port_info(sata_hba_inst, sata_device);
15418 15417 if (qual == SATA_ADDR_CPORT) {
15419 15418 if (rval != SATA_SUCCESS) {
15420 15419 /*
15421 15420 * Port deactivation failure - do not change port state
15422 15421 * unless the state returned by HBA indicates a port
15423 15422 * failure.
15424 15423 */
15425 15424 if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15426 15425 SATA_CPORT_STATE(sata_hba_inst, cport) =
15427 15426 SATA_PSTATE_FAILED;
15428 15427 }
15429 15428 SATA_LOG_D((sata_hba_inst, CE_WARN,
15430 15429 "sata_hba_ioctl: port deactivate: "
15431 15430 "cannot deactivate SATA port %d", cport));
15432 15431 rv = EIO;
15433 15432 } else {
15434 15433 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
15435 15434 }
15436 15435 } else {
15437 15436 mutex_enter(&pmportinfo->pmport_mutex);
15438 15437 if (rval != SATA_SUCCESS) {
15439 15438 if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15440 15439 SATA_PMPORT_STATE(sata_hba_inst, cport,
15441 15440 pmport) = SATA_PSTATE_FAILED;
15442 15441 }
15443 15442 SATA_LOG_D((sata_hba_inst, CE_WARN,
15444 15443 "sata_hba_ioctl: port deactivate: "
15445 15444 "cannot deactivate SATA port %d:%d",
15446 15445 cport, pmport));
15447 15446 rv = EIO;
15448 15447 } else {
15449 15448 pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
15450 15449 }
15451 15450 mutex_exit(&pmportinfo->pmport_mutex);
15452 15451 }
15453 15452
15454 15453 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15455 15454
15456 15455 return (rv);
15457 15456 }
15458 15457
15459 15458 /*
15460 15459 * Process ioctl port activate request.
15461 15460 *
15462 15461 * NOTE: Port multiplier is supported now.
15463 15462 */
15464 15463 static int
15465 15464 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst,
15466 15465 sata_device_t *sata_device)
15467 15466 {
15468 15467 int cport, pmport, qual;
15469 15468 sata_cport_info_t *cportinfo;
15470 15469 sata_pmport_info_t *pmportinfo = NULL;
15471 15470 boolean_t dev_existed = B_TRUE;
15472 15471
15473 15472 /* Sanity check */
15474 15473 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
15475 15474 return (ENOTSUP);
15476 15475
15477 15476 cport = sata_device->satadev_addr.cport;
15478 15477 pmport = sata_device->satadev_addr.pmport;
15479 15478 qual = sata_device->satadev_addr.qual;
15480 15479
15481 15480 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
15482 15481
15483 15482 /*
15484 15483 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL())
15485 15484 * is a device. But what we are dealing with is port/pmport.
15486 15485 */
15487 15486 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
15488 15487 if (qual == SATA_ADDR_DCPORT)
15489 15488 sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT;
15490 15489 else
15491 15490 sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT;
15492 15491
15493 15492 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15494 15493 if (qual == SATA_ADDR_PMPORT) {
15495 15494 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
15496 15495 if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN ||
15497 15496 pmportinfo->pmport_dev_type == SATA_DTYPE_NONE)
15498 15497 dev_existed = B_FALSE;
15499 15498 } else { /* cport */
15500 15499 if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN ||
15501 15500 cportinfo->cport_dev_type == SATA_DTYPE_NONE)
15502 15501 dev_existed = B_FALSE;
15503 15502 }
15504 15503 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15505 15504
15506 15505 /* Just let HBA driver to activate port, if necessary */
15507 15506 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
15508 15507 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15509 15508 /*
15510 15509 * Port activation failure - do not change port state unless
15511 15510 * the state returned by HBA indicates a port failure.
15512 15511 */
15513 15512 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15514 15513 cport)->cport_mutex);
15515 15514 sata_update_port_info(sata_hba_inst, sata_device);
15516 15515 if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15517 15516 if (qual == SATA_ADDR_PMPORT) {
15518 15517 mutex_enter(&pmportinfo->pmport_mutex);
15519 15518 pmportinfo->pmport_state = SATA_PSTATE_FAILED;
15520 15519 mutex_exit(&pmportinfo->pmport_mutex);
15521 15520 } else
15522 15521 cportinfo->cport_state = SATA_PSTATE_FAILED;
15523 15522
15524 15523 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15525 15524 cport)->cport_mutex);
15526 15525 SATA_LOG_D((sata_hba_inst, CE_WARN,
15527 15526 "sata_hba_ioctl: port activate: cannot activate "
15528 15527 "SATA port %d:%d", cport, pmport));
15529 15528 return (EIO);
15530 15529 }
15531 15530 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15532 15531 }
15533 15532 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15534 15533 if (qual == SATA_ADDR_PMPORT) {
15535 15534 mutex_enter(&pmportinfo->pmport_mutex);
15536 15535 pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN;
15537 15536 mutex_exit(&pmportinfo->pmport_mutex);
15538 15537 } else
15539 15538 cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN;
15540 15539 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15541 15540
15542 15541 /*
15543 15542 * Re-probe port to find its current state and possibly attached device.
15544 15543 * Port re-probing may change the cportinfo device type if device is
15545 15544 * found attached.
15546 15545 * If port probing failed, the device type would be set to
15547 15546 * SATA_DTYPE_NONE.
15548 15547 */
15549 15548 (void) sata_reprobe_port(sata_hba_inst, sata_device,
15550 15549 SATA_DEV_IDENTIFY_RETRY);
15551 15550
15552 15551 /*
15553 15552 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15554 15553 * without the hint.
15555 15554 */
15556 15555 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15557 15556 SE_NO_HINT);
15558 15557
15559 15558 if (dev_existed == B_FALSE) {
15560 15559 if (qual == SATA_ADDR_PMPORT &&
15561 15560 pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
15562 15561 /*
15563 15562 * That's the transition from the "inactive" port state
15564 15563 * or the active port without a device attached to the
15565 15564 * active port state with a device attached.
15566 15565 */
15567 15566 sata_log(sata_hba_inst, CE_WARN,
15568 15567 "SATA device detected at port %d:%d",
15569 15568 cport, pmport);
15570 15569 } else if (qual == SATA_ADDR_CPORT &&
15571 15570 cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
15572 15571 /*
15573 15572 * That's the transition from the "inactive" port state
15574 15573 * or the active port without a device attached to the
15575 15574 * active port state with a device attached.
15576 15575 */
15577 15576 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
15578 15577 sata_log(sata_hba_inst, CE_WARN,
15579 15578 "SATA device detected at port %d", cport);
15580 15579 } else {
15581 15580 sata_log(sata_hba_inst, CE_WARN,
15582 15581 "SATA port multiplier detected at port %d",
15583 15582 cport);
15584 15583 }
15585 15584 }
15586 15585 }
15587 15586 return (0);
15588 15587 }
15589 15588
15590 15589
15591 15590
15592 15591 /*
15593 15592 * Process ioctl reset port request.
15594 15593 *
15595 15594 * NOTE: Port-Multiplier is supported.
15596 15595 */
15597 15596 static int
15598 15597 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst,
15599 15598 sata_device_t *sata_device)
15600 15599 {
15601 15600 int cport, pmport, qual;
15602 15601 int rv = 0;
15603 15602
15604 15603 cport = sata_device->satadev_addr.cport;
15605 15604 pmport = sata_device->satadev_addr.pmport;
15606 15605 qual = sata_device->satadev_addr.qual;
15607 15606
15608 15607 /*
15609 15608 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL())
15610 15609 * is a device. But what we are dealing with is port/pmport.
15611 15610 */
15612 15611 if (qual == SATA_ADDR_DCPORT)
15613 15612 sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT;
15614 15613 else
15615 15614 sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT;
15616 15615 ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
15617 15616
15618 15617 /* Sanity check */
15619 15618 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15620 15619 SATA_LOG_D((sata_hba_inst, CE_WARN,
15621 15620 "sata_hba_ioctl: sata_hba_tran missing required "
15622 15621 "function sata_tran_reset_dport"));
15623 15622 return (ENOTSUP);
15624 15623 }
15625 15624
15626 15625 /* Ask HBA to reset port */
15627 15626 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
15628 15627 sata_device) != SATA_SUCCESS) {
15629 15628 SATA_LOG_D((sata_hba_inst, CE_WARN,
15630 15629 "sata_hba_ioctl: reset port: failed %d:%d",
15631 15630 cport, pmport));
15632 15631 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15633 15632 cport_mutex);
15634 15633 sata_update_port_info(sata_hba_inst, sata_device);
15635 15634 if (qual == SATA_ADDR_CPORT)
15636 15635 SATA_CPORT_STATE(sata_hba_inst, cport) =
15637 15636 SATA_PSTATE_FAILED;
15638 15637 else {
15639 15638 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport,
15640 15639 pmport));
15641 15640 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
15642 15641 SATA_PSTATE_FAILED;
15643 15642 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport,
15644 15643 pmport));
15645 15644 }
15646 15645 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15647 15646 cport_mutex);
15648 15647 rv = EIO;
15649 15648 }
15650 15649
15651 15650 return (rv);
15652 15651 }
15653 15652
15654 15653 /*
15655 15654 * Process ioctl reset device request.
15656 15655 *
15657 15656 * NOTE: Port multiplier is supported.
15658 15657 */
15659 15658 static int
15660 15659 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst,
15661 15660 sata_device_t *sata_device)
15662 15661 {
15663 15662 sata_drive_info_t *sdinfo = NULL;
15664 15663 sata_pmult_info_t *pmultinfo = NULL;
15665 15664 int cport, pmport;
15666 15665 int rv = 0;
15667 15666
15668 15667 /* Sanity check */
15669 15668 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15670 15669 SATA_LOG_D((sata_hba_inst, CE_WARN,
15671 15670 "sata_hba_ioctl: sata_hba_tran missing required "
15672 15671 "function sata_tran_reset_dport"));
15673 15672 return (ENOTSUP);
15674 15673 }
15675 15674
15676 15675 cport = sata_device->satadev_addr.cport;
15677 15676 pmport = sata_device->satadev_addr.pmport;
15678 15677
15679 15678 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15680 15679 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
15681 15680 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
15682 15681 SATA_DTYPE_PMULT)
15683 15682 pmultinfo = SATA_CPORT_INFO(sata_hba_inst, cport)->
15684 15683 cport_devp.cport_sata_pmult;
15685 15684 else
15686 15685 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
15687 15686 sata_device->satadev_addr.cport);
15688 15687 } else { /* port multiplier */
15689 15688 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15690 15689 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
15691 15690 sata_device->satadev_addr.cport,
15692 15691 sata_device->satadev_addr.pmport);
15693 15692 }
15694 15693 if (sdinfo == NULL && pmultinfo == NULL) {
15695 15694 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15696 15695 return (EINVAL);
15697 15696 }
15698 15697 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15699 15698
15700 15699 /* Ask HBA to reset device */
15701 15700 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
15702 15701 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15703 15702 SATA_LOG_D((sata_hba_inst, CE_WARN,
15704 15703 "sata_hba_ioctl: reset device: failed at port %d:%d",
15705 15704 cport, pmport));
15706 15705 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15707 15706 cport_mutex);
15708 15707 sata_update_port_info(sata_hba_inst, sata_device);
15709 15708 /*
15710 15709 * Device info structure remains attached. Another device reset
15711 15710 * or port disconnect/connect and re-probing is
15712 15711 * needed to change it's state
15713 15712 */
15714 15713 if (sdinfo != NULL) {
15715 15714 sdinfo->satadrv_state &= ~SATA_STATE_READY;
15716 15715 sdinfo->satadrv_state |= SATA_DSTATE_FAILED;
15717 15716 } else if (pmultinfo != NULL) {
15718 15717 pmultinfo->pmult_state &= ~SATA_STATE_READY;
15719 15718 pmultinfo->pmult_state |= SATA_DSTATE_FAILED;
15720 15719 }
15721 15720
15722 15721 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15723 15722 rv = EIO;
15724 15723 }
15725 15724 /*
15726 15725 * If attached device was a port multiplier, some extra processing
15727 15726 * may be needed to bring it back. SATA specification requies a
15728 15727 * mandatory software reset on host port to reliably enumerate a port
15729 15728 * multiplier, the HBA driver should handle that after reset
15730 15729 * operation.
15731 15730 */
15732 15731 return (rv);
15733 15732 }
15734 15733
15735 15734
15736 15735 /*
15737 15736 * Process ioctl reset all request.
15738 15737 */
15739 15738 static int
15740 15739 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst)
15741 15740 {
15742 15741 sata_device_t sata_device;
15743 15742 int rv = 0;
15744 15743 int tcport;
15745 15744
15746 15745 sata_device.satadev_rev = SATA_DEVICE_REV;
15747 15746
15748 15747 /*
15749 15748 * There is no protection here for configured devices.
15750 15749 */
15751 15750 /* Sanity check */
15752 15751 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15753 15752 SATA_LOG_D((sata_hba_inst, CE_WARN,
15754 15753 "sata_hba_ioctl: sata_hba_tran missing required "
15755 15754 "function sata_tran_reset_dport"));
15756 15755 return (ENOTSUP);
15757 15756 }
15758 15757
15759 15758 /*
15760 15759 * Need to lock all ports, not just one.
15761 15760 * If any port is locked by event processing, fail the whole operation.
15762 15761 * One port is already locked, but for simplicity lock it again.
15763 15762 */
15764 15763 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
15765 15764 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15766 15765 cport_mutex);
15767 15766 if (((SATA_CPORT_INFO(sata_hba_inst, tcport)->
15768 15767 cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) {
15769 15768 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15770 15769 cport_mutex);
15771 15770 rv = EBUSY;
15772 15771 break;
15773 15772 } else {
15774 15773 /*
15775 15774 * It is enough to lock cport in command-based
15776 15775 * switching mode.
15777 15776 */
15778 15777 SATA_CPORT_INFO(sata_hba_inst, tcport)->
15779 15778 cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
15780 15779 }
15781 15780 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15782 15781 cport_mutex);
15783 15782 }
15784 15783
15785 15784 if (rv == 0) {
15786 15785 /*
15787 15786 * All cports were successfully locked.
15788 15787 * Reset main SATA controller.
15789 15788 * Set the device address to port 0, to have a valid device
15790 15789 * address.
15791 15790 */
15792 15791 sata_device.satadev_addr.qual = SATA_ADDR_CNTRL;
15793 15792 sata_device.satadev_addr.cport = 0;
15794 15793 sata_device.satadev_addr.pmport = 0;
15795 15794
15796 15795 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
15797 15796 (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) {
15798 15797 SATA_LOG_D((sata_hba_inst, CE_WARN,
15799 15798 "sata_hba_ioctl: reset controller failed"));
15800 15799 return (EIO);
15801 15800 }
15802 15801 }
15803 15802 /*
15804 15803 * Unlock all ports
15805 15804 */
15806 15805 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
15807 15806 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15808 15807 cport_mutex);
15809 15808 SATA_CPORT_INFO(sata_hba_inst, tcport)->
15810 15809 cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
15811 15810 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15812 15811 cport_mutex);
15813 15812 }
15814 15813
15815 15814 /*
15816 15815 * This operation returns EFAULT if either reset
15817 15816 * controller failed or a re-probing of any port failed.
15818 15817 */
15819 15818 return (rv);
15820 15819 }
15821 15820
15822 15821
15823 15822 /*
15824 15823 * Process ioctl port self test request.
15825 15824 *
15826 15825 * NOTE: Port multiplier code is not completed nor tested.
15827 15826 */
15828 15827 static int
15829 15828 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst,
15830 15829 sata_device_t *sata_device)
15831 15830 {
15832 15831 int cport, pmport, qual;
15833 15832 int rv = 0;
15834 15833
15835 15834 /* Sanity check */
15836 15835 if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL)
15837 15836 return (ENOTSUP);
15838 15837
15839 15838 cport = sata_device->satadev_addr.cport;
15840 15839 pmport = sata_device->satadev_addr.pmport;
15841 15840 qual = sata_device->satadev_addr.qual;
15842 15841
15843 15842 /*
15844 15843 * There is no protection here for a configured
15845 15844 * device attached to this port.
15846 15845 */
15847 15846
15848 15847 if ((*SATA_SELFTEST_FUNC(sata_hba_inst))
15849 15848 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15850 15849 SATA_LOG_D((sata_hba_inst, CE_WARN,
15851 15850 "sata_hba_ioctl: port selftest: "
15852 15851 "failed port %d:%d", cport, pmport));
15853 15852 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15854 15853 cport_mutex);
15855 15854 sata_update_port_info(sata_hba_inst, sata_device);
15856 15855 if (qual == SATA_ADDR_CPORT)
15857 15856 SATA_CPORT_STATE(sata_hba_inst, cport) =
15858 15857 SATA_PSTATE_FAILED;
15859 15858 else { /* port multiplier device port */
15860 15859 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst,
15861 15860 cport, pmport));
15862 15861 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
15863 15862 SATA_PSTATE_FAILED;
15864 15863 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst,
15865 15864 cport, pmport));
15866 15865 }
15867 15866
15868 15867 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15869 15868 cport_mutex);
15870 15869 return (EIO);
15871 15870 }
15872 15871 /*
15873 15872 * Beacuse the port was reset in the course of testing, it should be
15874 15873 * re-probed and attached device state should be restored. At this
15875 15874 * point the port state is unknown - it's state is HBA-specific.
15876 15875 * Force port re-probing to get it into a known state.
15877 15876 */
15878 15877 if (sata_reprobe_port(sata_hba_inst, sata_device,
15879 15878 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
15880 15879 rv = EIO;
15881 15880 return (rv);
15882 15881 }
15883 15882
15884 15883
15885 15884 /*
15886 15885 * sata_cfgadm_state:
15887 15886 * Use the sata port state and state of the target node to figure out
15888 15887 * the cfgadm_state.
15889 15888 *
15890 15889 * The port argument is a value with encoded cport,
15891 15890 * pmport and address qualifier, in the same manner as a scsi target number.
15892 15891 * SCSI_TO_SATA_CPORT macro extracts cport number,
15893 15892 * SCSI_TO_SATA_PMPORT extracts pmport number and
15894 15893 * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag.
15895 15894 *
15896 15895 * Port multiplier is supported.
15897 15896 */
15898 15897
15899 15898 static void
15900 15899 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port,
15901 15900 devctl_ap_state_t *ap_state)
15902 15901 {
15903 15902 uint8_t cport, pmport, qual;
15904 15903 uint32_t port_state, pmult_state;
15905 15904 uint32_t dev_type;
15906 15905 sata_drive_info_t *sdinfo;
15907 15906
15908 15907 cport = SCSI_TO_SATA_CPORT(port);
15909 15908 pmport = SCSI_TO_SATA_PMPORT(port);
15910 15909 qual = SCSI_TO_SATA_ADDR_QUAL(port);
15911 15910
15912 15911 /* Check cport state */
15913 15912 port_state = SATA_CPORT_STATE(sata_hba_inst, cport);
15914 15913 if (port_state & SATA_PSTATE_SHUTDOWN ||
15915 15914 port_state & SATA_PSTATE_FAILED) {
15916 15915 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
15917 15916 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15918 15917 if (port_state & SATA_PSTATE_FAILED)
15919 15918 ap_state->ap_condition = AP_COND_FAILED;
15920 15919 else
15921 15920 ap_state->ap_condition = AP_COND_UNKNOWN;
15922 15921
15923 15922 return;
15924 15923 }
15925 15924
15926 15925 /* cport state is okay. Now check pmport state */
15927 15926 if (qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) {
15928 15927 /* Sanity check */
15929 15928 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
15930 15929 SATA_DTYPE_PMULT || SATA_PMPORT_INFO(sata_hba_inst,
15931 15930 cport, pmport) == NULL)
15932 15931 return;
15933 15932 port_state = SATA_PMPORT_STATE(sata_hba_inst, cport, pmport);
15934 15933 if (port_state & SATA_PSTATE_SHUTDOWN ||
15935 15934 port_state & SATA_PSTATE_FAILED) {
15936 15935 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
15937 15936 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15938 15937 if (port_state & SATA_PSTATE_FAILED)
15939 15938 ap_state->ap_condition = AP_COND_FAILED;
15940 15939 else
15941 15940 ap_state->ap_condition = AP_COND_UNKNOWN;
15942 15941
15943 15942 return;
15944 15943 }
15945 15944 }
15946 15945
15947 15946 /* Port is enabled and ready */
15948 15947 if (qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_CPORT)
15949 15948 dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, cport);
15950 15949 else
15951 15950 dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, pmport);
15952 15951
15953 15952 switch (dev_type) {
15954 15953 case SATA_DTYPE_NONE:
15955 15954 {
15956 15955 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15957 15956 ap_state->ap_condition = AP_COND_OK;
15958 15957 /* No device attached */
15959 15958 ap_state->ap_rstate = AP_RSTATE_EMPTY;
15960 15959 break;
15961 15960 }
15962 15961 case SATA_DTYPE_PMULT:
15963 15962 {
15964 15963 /* Need to check port multiplier state */
15965 15964 ASSERT(qual == SATA_ADDR_DCPORT);
15966 15965 pmult_state = SATA_PMULT_INFO(sata_hba_inst, cport)->
15967 15966 pmult_state;
15968 15967 if (pmult_state & (SATA_PSTATE_SHUTDOWN|SATA_PSTATE_FAILED)) {
15969 15968 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
15970 15969 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15971 15970 if (pmult_state & SATA_PSTATE_FAILED)
15972 15971 ap_state->ap_condition = AP_COND_FAILED;
15973 15972 else
15974 15973 ap_state->ap_condition = AP_COND_UNKNOWN;
15975 15974
15976 15975 return;
15977 15976 }
15978 15977
15979 15978 /* Port multiplier is not configurable */
15980 15979 ap_state->ap_ostate = AP_OSTATE_CONFIGURED;
15981 15980 ap_state->ap_rstate = AP_RSTATE_CONNECTED;
15982 15981 ap_state->ap_condition = AP_COND_OK;
15983 15982 break;
15984 15983 }
15985 15984
15986 15985 case SATA_DTYPE_ATADISK:
15987 15986 case SATA_DTYPE_ATAPICD:
15988 15987 case SATA_DTYPE_ATAPITAPE:
15989 15988 case SATA_DTYPE_ATAPIDISK:
15990 15989 {
15991 15990 dev_info_t *tdip = NULL;
15992 15991 dev_info_t *dip = NULL;
15993 15992 int circ;
15994 15993
15995 15994 dip = SATA_DIP(sata_hba_inst);
15996 15995 tdip = sata_get_target_dip(dip, cport, pmport);
15997 15996 ap_state->ap_rstate = AP_RSTATE_CONNECTED;
15998 15997 if (tdip != NULL) {
15999 15998 ndi_devi_enter(dip, &circ);
16000 15999 mutex_enter(&(DEVI(tdip)->devi_lock));
16001 16000 if (DEVI_IS_DEVICE_REMOVED(tdip)) {
16002 16001 /*
16003 16002 * There could be the case where previously
16004 16003 * configured and opened device was removed
16005 16004 * and unknown device was plugged.
16006 16005 * In such case we want to show a device, and
16007 16006 * its configured or unconfigured state but
16008 16007 * indicate unusable condition untill the
16009 16008 * old target node is released and removed.
16010 16009 */
16011 16010 ap_state->ap_condition = AP_COND_UNUSABLE;
16012 16011 } else {
16013 16012 mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst,
16014 16013 cport));
16015 16014 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16016 16015 cport);
16017 16016 if (sdinfo != NULL) {
16018 16017 if ((sdinfo->satadrv_state &
16019 16018 SATA_DSTATE_FAILED) != 0)
16020 16019 ap_state->ap_condition =
16021 16020 AP_COND_FAILED;
16022 16021 else
16023 16022 ap_state->ap_condition =
16024 16023 AP_COND_OK;
16025 16024 } else {
16026 16025 ap_state->ap_condition =
16027 16026 AP_COND_UNKNOWN;
16028 16027 }
16029 16028 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst,
16030 16029 cport));
16031 16030 }
16032 16031 if ((DEVI_IS_DEVICE_OFFLINE(tdip)) ||
16033 16032 (DEVI_IS_DEVICE_DOWN(tdip))) {
16034 16033 ap_state->ap_ostate =
16035 16034 AP_OSTATE_UNCONFIGURED;
16036 16035 } else {
16037 16036 ap_state->ap_ostate =
16038 16037 AP_OSTATE_CONFIGURED;
16039 16038 }
16040 16039 mutex_exit(&(DEVI(tdip)->devi_lock));
16041 16040 ndi_devi_exit(dip, circ);
16042 16041 } else {
16043 16042 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16044 16043 ap_state->ap_condition = AP_COND_UNKNOWN;
16045 16044 }
16046 16045 break;
16047 16046 }
16048 16047 case SATA_DTYPE_ATAPIPROC:
16049 16048 ap_state->ap_rstate = AP_RSTATE_CONNECTED;
16050 16049 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16051 16050 ap_state->ap_condition = AP_COND_OK;
16052 16051 break;
16053 16052 default:
16054 16053 ap_state->ap_rstate = AP_RSTATE_CONNECTED;
16055 16054 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16056 16055 ap_state->ap_condition = AP_COND_UNKNOWN;
16057 16056 /*
16058 16057 * This is actually internal error condition (non fatal),
16059 16058 * because we have already checked all defined device types.
16060 16059 */
16061 16060 SATA_LOG_D((sata_hba_inst, CE_WARN,
16062 16061 "sata_cfgadm_state: Internal error: "
16063 16062 "unknown device type"));
16064 16063 break;
16065 16064 }
16066 16065 }
16067 16066
16068 16067
16069 16068 /*
16070 16069 * Process ioctl get device path request.
16071 16070 *
16072 16071 * NOTE: Port multiplier has no target dip. Devices connected to port
16073 16072 * multiplier have target node attached to the HBA node. The only difference
16074 16073 * between them and the directly-attached device node is a target address.
16075 16074 */
16076 16075 static int
16077 16076 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst,
16078 16077 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16079 16078 {
16080 16079 char path[MAXPATHLEN];
16081 16080 uint32_t size;
16082 16081 dev_info_t *tdip;
16083 16082
16084 16083 (void) strcpy(path, "/devices");
16085 16084 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
16086 16085 &sata_device->satadev_addr)) == NULL) {
16087 16086 /*
16088 16087 * No such device. If this is a request for a size, do not
16089 16088 * return EINVAL for non-existing target, because cfgadm
16090 16089 * will then indicate a meaningless ioctl failure.
16091 16090 * If this is a request for a path, indicate invalid
16092 16091 * argument.
16093 16092 */
16094 16093 if (ioc->get_size == 0)
16095 16094 return (EINVAL);
16096 16095 } else {
16097 16096 (void) ddi_pathname(tdip, path + strlen(path));
16098 16097 }
16099 16098 size = strlen(path) + 1;
16100 16099
16101 16100 if (ioc->get_size != 0) {
16102 16101 if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz,
16103 16102 mode) != 0)
16104 16103 return (EFAULT);
16105 16104 } else {
16106 16105 if (ioc->bufsiz != size)
16107 16106 return (EINVAL);
16108 16107
16109 16108 else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz,
16110 16109 mode) != 0)
16111 16110 return (EFAULT);
16112 16111 }
16113 16112 return (0);
16114 16113 }
16115 16114
16116 16115 /*
16117 16116 * Process ioctl get attachment point type request.
16118 16117 *
16119 16118 * NOTE: Port multiplier is supported.
16120 16119 */
16121 16120 static int
16122 16121 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst,
16123 16122 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16124 16123 {
16125 16124 uint32_t type_len;
16126 16125 const char *ap_type;
16127 16126 int dev_type;
16128 16127
16129 16128 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16130 16129 dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst,
16131 16130 sata_device->satadev_addr.cport);
16132 16131 else /* pmport */
16133 16132 dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst,
16134 16133 sata_device->satadev_addr.cport,
16135 16134 sata_device->satadev_addr.pmport);
16136 16135
16137 16136 switch (dev_type) {
16138 16137 case SATA_DTYPE_NONE:
16139 16138 ap_type = "port";
16140 16139 break;
16141 16140
16142 16141 case SATA_DTYPE_ATADISK:
16143 16142 case SATA_DTYPE_ATAPIDISK:
16144 16143 ap_type = "disk";
16145 16144 break;
16146 16145
16147 16146 case SATA_DTYPE_ATAPICD:
16148 16147 ap_type = "cd/dvd";
16149 16148 break;
16150 16149
16151 16150 case SATA_DTYPE_ATAPITAPE:
16152 16151 ap_type = "tape";
16153 16152 break;
16154 16153
16155 16154 case SATA_DTYPE_ATAPIPROC:
16156 16155 ap_type = "processor";
16157 16156 break;
16158 16157
16159 16158 case SATA_DTYPE_PMULT:
16160 16159 ap_type = "sata-pmult";
16161 16160 break;
16162 16161
16163 16162 case SATA_DTYPE_UNKNOWN:
16164 16163 ap_type = "unknown";
16165 16164 break;
16166 16165
16167 16166 default:
16168 16167 ap_type = "unsupported";
16169 16168 break;
16170 16169
16171 16170 } /* end of dev_type switch */
16172 16171
16173 16172 type_len = strlen(ap_type) + 1;
16174 16173
16175 16174 if (ioc->get_size) {
16176 16175 if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz,
16177 16176 mode) != 0)
16178 16177 return (EFAULT);
16179 16178 } else {
16180 16179 if (ioc->bufsiz != type_len)
16181 16180 return (EINVAL);
16182 16181
16183 16182 if (ddi_copyout((void *)ap_type, ioc->buf,
16184 16183 ioc->bufsiz, mode) != 0)
16185 16184 return (EFAULT);
16186 16185 }
16187 16186 return (0);
16188 16187
16189 16188 }
16190 16189
16191 16190 /*
16192 16191 * Process ioctl get device model info request.
16193 16192 * This operation should return to cfgadm the device model
16194 16193 * information string
16195 16194 *
16196 16195 * NOTE: Port multiplier is supported.
16197 16196 */
16198 16197 static int
16199 16198 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst,
16200 16199 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16201 16200 {
16202 16201 sata_drive_info_t *sdinfo;
16203 16202 uint32_t info_len;
16204 16203 char ap_info[SATA_ID_MODEL_LEN + 1];
16205 16204
16206 16205 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16207 16206 sata_device->satadev_addr.cport)->cport_mutex);
16208 16207 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16209 16208 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16210 16209 sata_device->satadev_addr.cport);
16211 16210 else /* port multiplier */
16212 16211 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16213 16212 sata_device->satadev_addr.cport,
16214 16213 sata_device->satadev_addr.pmport);
16215 16214 if (sdinfo == NULL) {
16216 16215 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16217 16216 sata_device->satadev_addr.cport)->cport_mutex);
16218 16217 return (EINVAL);
16219 16218 }
16220 16219
16221 16220 #ifdef _LITTLE_ENDIAN
16222 16221 swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
16223 16222 #else /* _LITTLE_ENDIAN */
16224 16223 bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
16225 16224 #endif /* _LITTLE_ENDIAN */
16226 16225
16227 16226 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16228 16227 sata_device->satadev_addr.cport)->cport_mutex);
16229 16228
16230 16229 ap_info[SATA_ID_MODEL_LEN] = '\0';
16231 16230
16232 16231 info_len = strlen(ap_info) + 1;
16233 16232
16234 16233 if (ioc->get_size) {
16235 16234 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16236 16235 mode) != 0)
16237 16236 return (EFAULT);
16238 16237 } else {
16239 16238 if (ioc->bufsiz < info_len)
16240 16239 return (EINVAL);
16241 16240 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16242 16241 mode) != 0)
16243 16242 return (EFAULT);
16244 16243 }
16245 16244 return (0);
16246 16245 }
16247 16246
16248 16247
16249 16248 /*
16250 16249 * Process ioctl get device firmware revision info request.
16251 16250 * This operation should return to cfgadm the device firmware revision
16252 16251 * information string
16253 16252 *
16254 16253 * Port multiplier is supported.
16255 16254 */
16256 16255 static int
16257 16256 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst,
16258 16257 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16259 16258 {
16260 16259 sata_drive_info_t *sdinfo;
16261 16260 uint32_t info_len;
16262 16261 char ap_info[SATA_ID_FW_LEN + 1];
16263 16262
16264 16263 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16265 16264 sata_device->satadev_addr.cport)->cport_mutex);
16266 16265 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16267 16266 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16268 16267 sata_device->satadev_addr.cport);
16269 16268 else /* port multiplier */
16270 16269 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16271 16270 sata_device->satadev_addr.cport,
16272 16271 sata_device->satadev_addr.pmport);
16273 16272 if (sdinfo == NULL) {
16274 16273 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16275 16274 sata_device->satadev_addr.cport)->cport_mutex);
16276 16275 return (EINVAL);
16277 16276 }
16278 16277
16279 16278 #ifdef _LITTLE_ENDIAN
16280 16279 swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
16281 16280 #else /* _LITTLE_ENDIAN */
16282 16281 bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
16283 16282 #endif /* _LITTLE_ENDIAN */
16284 16283
16285 16284 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16286 16285 sata_device->satadev_addr.cport)->cport_mutex);
16287 16286
16288 16287 ap_info[SATA_ID_FW_LEN] = '\0';
16289 16288
16290 16289 info_len = strlen(ap_info) + 1;
16291 16290
16292 16291 if (ioc->get_size) {
16293 16292 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16294 16293 mode) != 0)
16295 16294 return (EFAULT);
16296 16295 } else {
16297 16296 if (ioc->bufsiz < info_len)
16298 16297 return (EINVAL);
16299 16298 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16300 16299 mode) != 0)
16301 16300 return (EFAULT);
16302 16301 }
16303 16302 return (0);
16304 16303 }
16305 16304
16306 16305
16307 16306 /*
16308 16307 * Process ioctl get device serial number info request.
16309 16308 * This operation should return to cfgadm the device serial number string.
16310 16309 *
16311 16310 * NOTE: Port multiplier is supported.
16312 16311 */
16313 16312 static int
16314 16313 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst,
16315 16314 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16316 16315 {
16317 16316 sata_drive_info_t *sdinfo;
16318 16317 uint32_t info_len;
16319 16318 char ap_info[SATA_ID_SERIAL_LEN + 1];
16320 16319
16321 16320 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16322 16321 sata_device->satadev_addr.cport)->cport_mutex);
16323 16322 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16324 16323 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16325 16324 sata_device->satadev_addr.cport);
16326 16325 else /* port multiplier */
16327 16326 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16328 16327 sata_device->satadev_addr.cport,
16329 16328 sata_device->satadev_addr.pmport);
16330 16329 if (sdinfo == NULL) {
16331 16330 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16332 16331 sata_device->satadev_addr.cport)->cport_mutex);
16333 16332 return (EINVAL);
16334 16333 }
16335 16334
16336 16335 #ifdef _LITTLE_ENDIAN
16337 16336 swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
16338 16337 #else /* _LITTLE_ENDIAN */
16339 16338 bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
16340 16339 #endif /* _LITTLE_ENDIAN */
16341 16340
16342 16341 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16343 16342 sata_device->satadev_addr.cport)->cport_mutex);
16344 16343
16345 16344 ap_info[SATA_ID_SERIAL_LEN] = '\0';
16346 16345
16347 16346 info_len = strlen(ap_info) + 1;
16348 16347
16349 16348 if (ioc->get_size) {
16350 16349 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16351 16350 mode) != 0)
16352 16351 return (EFAULT);
16353 16352 } else {
16354 16353 if (ioc->bufsiz < info_len)
16355 16354 return (EINVAL);
16356 16355 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16357 16356 mode) != 0)
16358 16357 return (EFAULT);
16359 16358 }
16360 16359 return (0);
16361 16360 }
16362 16361
16363 16362
16364 16363 /*
16365 16364 * Preset scsi extended sense data (to NO SENSE)
16366 16365 * First 18 bytes of the sense data are preset to current valid sense
16367 16366 * with a key NO SENSE data.
16368 16367 *
16369 16368 * Returns void
16370 16369 */
16371 16370 static void
16372 16371 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense)
16373 16372 {
16374 16373 sense->es_valid = 1; /* Valid sense */
16375 16374 sense->es_class = CLASS_EXTENDED_SENSE; /* 0x70 - current err */
16376 16375 sense->es_key = KEY_NO_SENSE;
16377 16376 sense->es_info_1 = 0;
16378 16377 sense->es_info_2 = 0;
16379 16378 sense->es_info_3 = 0;
16380 16379 sense->es_info_4 = 0;
16381 16380 sense->es_add_len = 10; /* Additional length - replace with a def */
16382 16381 sense->es_cmd_info[0] = 0;
16383 16382 sense->es_cmd_info[1] = 0;
16384 16383 sense->es_cmd_info[2] = 0;
16385 16384 sense->es_cmd_info[3] = 0;
16386 16385 sense->es_add_code = 0;
16387 16386 sense->es_qual_code = 0;
16388 16387 }
16389 16388
16390 16389 /*
16391 16390 * Register a legacy cmdk-style devid for the target (disk) device.
16392 16391 *
16393 16392 * Note: This function is called only when the HBA devinfo node has the
16394 16393 * property "use-cmdk-devid-format" set. This property indicates that
16395 16394 * devid compatible with old cmdk (target) driver is to be generated
16396 16395 * for any target device attached to this controller. This will take
16397 16396 * precedence over the devid generated by sd (target) driver.
16398 16397 * This function is derived from cmdk_devid_setup() function in cmdk.c.
16399 16398 */
16400 16399 static void
16401 16400 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo)
16402 16401 {
16403 16402 char *hwid;
16404 16403 int modlen;
16405 16404 int serlen;
16406 16405 int rval;
16407 16406 ddi_devid_t devid;
16408 16407
16409 16408 /*
16410 16409 * device ID is a concatanation of model number, "=", serial number.
16411 16410 */
16412 16411 hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP);
16413 16412 bcopy(&sdinfo->satadrv_id.ai_model, hwid,
16414 16413 sizeof (sdinfo->satadrv_id.ai_model));
16415 16414 swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model));
16416 16415 modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model));
16417 16416 if (modlen == 0)
16418 16417 goto err;
16419 16418 hwid[modlen++] = '=';
16420 16419 bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen],
16421 16420 sizeof (sdinfo->satadrv_id.ai_drvser));
16422 16421 swab(&hwid[modlen], &hwid[modlen],
16423 16422 sizeof (sdinfo->satadrv_id.ai_drvser));
16424 16423 serlen = sata_check_modser(&hwid[modlen],
16425 16424 sizeof (sdinfo->satadrv_id.ai_drvser));
16426 16425 if (serlen == 0)
16427 16426 goto err;
16428 16427 hwid[modlen + serlen] = 0; /* terminate the hwid string */
16429 16428
16430 16429 /* initialize/register devid */
16431 16430 if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL,
16432 16431 (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS) {
16433 16432 rval = ddi_devid_register(dip, devid);
16434 16433 /*
16435 16434 * Free up the allocated devid buffer.
16436 16435 * NOTE: This doesn't mean unregistering devid.
16437 16436 */
16438 16437 ddi_devid_free(devid);
16439 16438 }
16440 16439
16441 16440 if (rval != DDI_SUCCESS)
16442 16441 cmn_err(CE_WARN, "sata: failed to create devid for the disk"
16443 16442 " on port %d", sdinfo->satadrv_addr.cport);
16444 16443 err:
16445 16444 kmem_free(hwid, LEGACY_HWID_LEN);
16446 16445 }
16447 16446
16448 16447 /*
16449 16448 * valid model/serial string must contain a non-zero non-space characters.
16450 16449 * trim trailing spaces/NULLs.
16451 16450 */
16452 16451 static int
16453 16452 sata_check_modser(char *buf, int buf_len)
16454 16453 {
16455 16454 boolean_t ret;
16456 16455 char *s;
16457 16456 int i;
16458 16457 int tb;
16459 16458 char ch;
16460 16459
16461 16460 ret = B_FALSE;
16462 16461 s = buf;
16463 16462 for (i = 0; i < buf_len; i++) {
16464 16463 ch = *s++;
16465 16464 if (ch != ' ' && ch != '\0')
16466 16465 tb = i + 1;
16467 16466 if (ch != ' ' && ch != '\0' && ch != '0')
16468 16467 ret = B_TRUE;
16469 16468 }
16470 16469
16471 16470 if (ret == B_FALSE)
16472 16471 return (0); /* invalid string */
16473 16472
16474 16473 return (tb); /* return length */
16475 16474 }
16476 16475
16477 16476 /*
16478 16477 * sata_set_drive_features function compares current device features setting
16479 16478 * with the saved device features settings and, if there is a difference,
16480 16479 * it restores device features setting to the previously saved state.
16481 16480 * It also arbitrarily tries to select the highest supported DMA mode.
16482 16481 * Device Identify or Identify Packet Device data has to be current.
16483 16482 * At the moment read ahead and write cache are considered for all devices.
16484 16483 * For atapi devices, Removable Media Status Notification is set in addition
16485 16484 * to common features.
16486 16485 *
16487 16486 * This function cannot be called in the interrupt context (it may sleep).
16488 16487 *
16489 16488 * The input argument sdinfo should point to the drive info structure
16490 16489 * to be updated after features are set. Note, that only
16491 16490 * device (packet) identify data is updated, not the flags indicating the
16492 16491 * supported features.
16493 16492 *
16494 16493 * Returns SATA_SUCCESS if successful or there was nothing to do.
16495 16494 * Device Identify data in the drive info structure pointed to by the sdinfo
16496 16495 * arguments is updated even when no features were set or changed.
16497 16496 *
16498 16497 * Returns SATA_FAILURE if device features could not be set or DMA mode
16499 16498 * for a disk cannot be set and device identify data cannot be fetched.
16500 16499 *
16501 16500 * Returns SATA_RETRY if device features could not be set (other than disk
16502 16501 * DMA mode) but the device identify data was fetched successfully.
16503 16502 *
16504 16503 * Note: This function may fail the port, making it inaccessible.
16505 16504 * In such case the explicit port disconnect/connect or physical device
16506 16505 * detach/attach is required to re-evaluate port state again.
16507 16506 */
16508 16507
16509 16508 static int
16510 16509 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst,
16511 16510 sata_drive_info_t *sdinfo, int restore)
16512 16511 {
16513 16512 int rval = SATA_SUCCESS;
16514 16513 int rval_set;
16515 16514 sata_drive_info_t new_sdinfo;
16516 16515 char *finfo = "sata_set_drive_features: cannot";
16517 16516 char *finfox;
16518 16517 int cache_op;
16519 16518
16520 16519 bzero(&new_sdinfo, sizeof (sata_drive_info_t));
16521 16520 new_sdinfo.satadrv_addr = sdinfo->satadrv_addr;
16522 16521 new_sdinfo.satadrv_type = sdinfo->satadrv_type;
16523 16522 if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
16524 16523 /*
16525 16524 * Cannot get device identification - caller may retry later
16526 16525 */
16527 16526 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16528 16527 "%s fetch device identify data\n", finfo);
16529 16528 return (SATA_FAILURE);
16530 16529 }
16531 16530 finfox = (restore != 0) ? " restore device features" :
16532 16531 " initialize device features\n";
16533 16532
16534 16533 switch (sdinfo->satadrv_type) {
16535 16534 case SATA_DTYPE_ATADISK:
16536 16535 /* Arbitrarily set UDMA mode */
16537 16536 if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
16538 16537 SATA_SUCCESS) {
16539 16538 SATA_LOG_D((sata_hba_inst, CE_WARN,
16540 16539 "%s set UDMA mode\n", finfo));
16541 16540 return (SATA_FAILURE);
16542 16541 }
16543 16542 break;
16544 16543 case SATA_DTYPE_ATAPICD:
16545 16544 case SATA_DTYPE_ATAPITAPE:
16546 16545 case SATA_DTYPE_ATAPIDISK:
16547 16546 /* Set Removable Media Status Notification, if necessary */
16548 16547 if (SATA_RM_NOTIFIC_SUPPORTED(new_sdinfo.satadrv_id) &&
16549 16548 restore != 0) {
16550 16549 if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) &&
16551 16550 (!SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id)))||
16552 16551 ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) &&
16553 16552 SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id))) {
16554 16553 /* Current setting does not match saved one */
16555 16554 if (sata_set_rmsn(sata_hba_inst, sdinfo,
16556 16555 sdinfo->satadrv_settings &
16557 16556 SATA_DEV_RMSN) != SATA_SUCCESS)
16558 16557 rval = SATA_FAILURE;
16559 16558 }
16560 16559 }
16561 16560 /*
16562 16561 * We have to set Multiword DMA or UDMA, if it is supported, as
16563 16562 * we want to use DMA transfer mode whenever possible.
16564 16563 * Some devices require explicit setting of the DMA mode.
16565 16564 */
16566 16565 if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) {
16567 16566 /* Set highest supported DMA mode */
16568 16567 if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
16569 16568 SATA_SUCCESS) {
16570 16569 SATA_LOG_D((sata_hba_inst, CE_WARN,
16571 16570 "%s set UDMA mode\n", finfo));
16572 16571 rval = SATA_FAILURE;
16573 16572 }
16574 16573 }
16575 16574 break;
16576 16575 }
16577 16576
16578 16577 if (!SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id) &&
16579 16578 !SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
16580 16579 /*
16581 16580 * neither READ AHEAD nor WRITE CACHE is supported
16582 16581 * - do nothing
16583 16582 */
16584 16583 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16585 16584 "settable features not supported\n", NULL);
16586 16585 goto update_sdinfo;
16587 16586 }
16588 16587
16589 16588 if ((SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id) &&
16590 16589 (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) &&
16591 16590 (SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id) &&
16592 16591 (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
16593 16592 /*
16594 16593 * both READ AHEAD and WRITE CACHE are enabled
16595 16594 * - Nothing to do
16596 16595 */
16597 16596 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16598 16597 "no device features to set\n", NULL);
16599 16598 goto update_sdinfo;
16600 16599 }
16601 16600
16602 16601 cache_op = 0;
16603 16602
16604 16603 if (SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id)) {
16605 16604 if ((sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
16606 16605 !SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
16607 16606 /* Enable read ahead / read cache */
16608 16607 cache_op = SATAC_SF_ENABLE_READ_AHEAD;
16609 16608 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16610 16609 "enabling read cache\n", NULL);
16611 16610 } else if (!(sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
16612 16611 SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
16613 16612 /* Disable read ahead / read cache */
16614 16613 cache_op = SATAC_SF_DISABLE_READ_AHEAD;
16615 16614 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16616 16615 "disabling read cache\n", NULL);
16617 16616 }
16618 16617
16619 16618 if (cache_op != 0) {
16620 16619 /* Try to set read cache mode */
16621 16620 rval_set = sata_set_cache_mode(sata_hba_inst,
16622 16621 &new_sdinfo, cache_op);
16623 16622 if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
16624 16623 rval = rval_set;
16625 16624 }
16626 16625 }
16627 16626
16628 16627 cache_op = 0;
16629 16628
16630 16629 if (SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
16631 16630 if ((sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
16632 16631 !SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
16633 16632 /* Enable write cache */
16634 16633 cache_op = SATAC_SF_ENABLE_WRITE_CACHE;
16635 16634 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16636 16635 "enabling write cache\n", NULL);
16637 16636 } else if (!(sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
16638 16637 SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
16639 16638 /* Disable write cache */
16640 16639 cache_op = SATAC_SF_DISABLE_WRITE_CACHE;
16641 16640 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16642 16641 "disabling write cache\n", NULL);
16643 16642 }
16644 16643
16645 16644 if (cache_op != 0) {
16646 16645 /* Try to set write cache mode */
16647 16646 rval_set = sata_set_cache_mode(sata_hba_inst,
16648 16647 &new_sdinfo, cache_op);
16649 16648 if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
16650 16649 rval = rval_set;
16651 16650 }
16652 16651 }
16653 16652 if (rval != SATA_SUCCESS)
16654 16653 SATA_LOG_D((sata_hba_inst, CE_WARN,
16655 16654 "%s %s", finfo, finfox));
16656 16655
16657 16656 update_sdinfo:
16658 16657 /*
16659 16658 * We need to fetch Device Identify data again
16660 16659 */
16661 16660 if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
16662 16661 /*
16663 16662 * Cannot get device identification - retry later
16664 16663 */
16665 16664 SATA_LOG_D((sata_hba_inst, CE_WARN,
16666 16665 "%s re-fetch device identify data\n", finfo));
16667 16666 rval = SATA_FAILURE;
16668 16667 }
16669 16668 /* Copy device sata info. */
16670 16669 sdinfo->satadrv_id = new_sdinfo.satadrv_id;
16671 16670
16672 16671 return (rval);
16673 16672 }
16674 16673
16675 16674
16676 16675 /*
16677 16676 *
16678 16677 * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if
16679 16678 * unable to determine.
16680 16679 *
16681 16680 * Cannot be called in an interrupt context.
16682 16681 *
16683 16682 * Called by sata_build_lsense_page_2f()
16684 16683 */
16685 16684
16686 16685 static int
16687 16686 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst,
16688 16687 sata_drive_info_t *sdinfo)
16689 16688 {
16690 16689 sata_pkt_t *spkt;
16691 16690 sata_cmd_t *scmd;
16692 16691 sata_pkt_txlate_t *spx;
16693 16692 int rval;
16694 16693
16695 16694 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16696 16695 spx->txlt_sata_hba_inst = sata_hba_inst;
16697 16696 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */
16698 16697 spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16699 16698 if (spkt == NULL) {
16700 16699 kmem_free(spx, sizeof (sata_pkt_txlate_t));
16701 16700 return (-1);
16702 16701 }
16703 16702 /* address is needed now */
16704 16703 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16705 16704
16706 16705
16707 16706 /* Fill sata_pkt */
16708 16707 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16709 16708 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16710 16709 /* Synchronous mode, no callback */
16711 16710 spkt->satapkt_comp = NULL;
16712 16711 /* Timeout 30s */
16713 16712 spkt->satapkt_time = sata_default_pkt_time;
16714 16713
16715 16714 scmd = &spkt->satapkt_cmd;
16716 16715 scmd->satacmd_flags.sata_special_regs = B_TRUE;
16717 16716 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
16718 16717
16719 16718 /* Set up which registers need to be returned */
16720 16719 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE;
16721 16720 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE;
16722 16721
16723 16722 /* Build SMART_RETURN_STATUS cmd in the sata_pkt */
16724 16723 scmd->satacmd_addr_type = 0; /* N/A */
16725 16724 scmd->satacmd_sec_count_lsb = 0; /* N/A */
16726 16725 scmd->satacmd_lba_low_lsb = 0; /* N/A */
16727 16726 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
16728 16727 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
16729 16728 scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS;
16730 16729 scmd->satacmd_device_reg = 0; /* Always device 0 */
16731 16730 scmd->satacmd_cmd_reg = SATAC_SMART;
16732 16731 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16733 16732 sdinfo->satadrv_addr.cport)));
16734 16733
16735 16734
16736 16735 /* Send pkt to SATA HBA driver */
16737 16736 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16738 16737 SATA_TRAN_ACCEPTED ||
16739 16738 spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16740 16739 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16741 16740 sdinfo->satadrv_addr.cport)));
16742 16741 /*
16743 16742 * Whoops, no SMART RETURN STATUS
16744 16743 */
16745 16744 rval = -1;
16746 16745 } else {
16747 16746 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16748 16747 sdinfo->satadrv_addr.cport)));
16749 16748 if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
16750 16749 rval = -1;
16751 16750 goto fail;
16752 16751 }
16753 16752 if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
16754 16753 rval = -1;
16755 16754 goto fail;
16756 16755 }
16757 16756 if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) &&
16758 16757 (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2))
16759 16758 rval = 0;
16760 16759 else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) &&
16761 16760 (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4))
16762 16761 rval = 1;
16763 16762 else {
16764 16763 rval = -1;
16765 16764 goto fail;
16766 16765 }
16767 16766 }
16768 16767 fail:
16769 16768 /* Free allocated resources */
16770 16769 sata_pkt_free(spx);
16771 16770 kmem_free(spx, sizeof (sata_pkt_txlate_t));
16772 16771
16773 16772 return (rval);
16774 16773 }
16775 16774
16776 16775 /*
16777 16776 *
16778 16777 * Returns 0 if succeeded, -1 otherwise
16779 16778 *
16780 16779 * Cannot be called in an interrupt context.
16781 16780 *
16782 16781 */
16783 16782 static int
16784 16783 sata_fetch_smart_data(
16785 16784 sata_hba_inst_t *sata_hba_inst,
16786 16785 sata_drive_info_t *sdinfo,
16787 16786 struct smart_data *smart_data)
16788 16787 {
16789 16788 sata_pkt_t *spkt;
16790 16789 sata_cmd_t *scmd;
16791 16790 sata_pkt_txlate_t *spx;
16792 16791 int rval;
16793 16792 dev_info_t *dip = SATA_DIP(sata_hba_inst);
16794 16793
16795 16794 #if ! defined(lint)
16796 16795 ASSERT(sizeof (struct smart_data) == 512);
16797 16796 #endif
16798 16797
16799 16798 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16800 16799 spx->txlt_sata_hba_inst = sata_hba_inst;
16801 16800 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */
16802 16801 spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16803 16802 if (spkt == NULL) {
16804 16803 kmem_free(spx, sizeof (sata_pkt_txlate_t));
16805 16804 return (-1);
16806 16805 }
16807 16806 /* address is needed now */
16808 16807 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16809 16808
16810 16809
16811 16810 /* Fill sata_pkt */
16812 16811 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16813 16812 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16814 16813 /* Synchronous mode, no callback */
16815 16814 spkt->satapkt_comp = NULL;
16816 16815 /* Timeout 30s */
16817 16816 spkt->satapkt_time = sata_default_pkt_time;
16818 16817
16819 16818 scmd = &spkt->satapkt_cmd;
16820 16819 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
16821 16820
16822 16821 /*
16823 16822 * Allocate buffer for SMART data
16824 16823 */
16825 16824 scmd->satacmd_bp = sata_alloc_local_buffer(spx,
16826 16825 sizeof (struct smart_data));
16827 16826 if (scmd->satacmd_bp == NULL) {
16828 16827 sata_pkt_free(spx);
16829 16828 kmem_free(spx, sizeof (sata_pkt_txlate_t));
16830 16829 SATA_LOG_D((sata_hba_inst, CE_WARN,
16831 16830 "sata_fetch_smart_data: "
16832 16831 "cannot allocate buffer"));
16833 16832 return (-1);
16834 16833 }
16835 16834
16836 16835
16837 16836 /* Build SMART_READ_DATA cmd in the sata_pkt */
16838 16837 scmd->satacmd_addr_type = 0; /* N/A */
16839 16838 scmd->satacmd_sec_count_lsb = 0; /* N/A */
16840 16839 scmd->satacmd_lba_low_lsb = 0; /* N/A */
16841 16840 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
16842 16841 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
16843 16842 scmd->satacmd_features_reg = SATA_SMART_READ_DATA;
16844 16843 scmd->satacmd_device_reg = 0; /* Always device 0 */
16845 16844 scmd->satacmd_cmd_reg = SATAC_SMART;
16846 16845 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16847 16846 sdinfo->satadrv_addr.cport)));
16848 16847
16849 16848 /* Send pkt to SATA HBA driver */
16850 16849 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16851 16850 SATA_TRAN_ACCEPTED ||
16852 16851 spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16853 16852 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16854 16853 sdinfo->satadrv_addr.cport)));
16855 16854 /*
16856 16855 * Whoops, no SMART DATA available
16857 16856 */
16858 16857 rval = -1;
16859 16858 goto fail;
16860 16859 } else {
16861 16860 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16862 16861 sdinfo->satadrv_addr.cport)));
16863 16862 if (spx->txlt_buf_dma_handle != NULL) {
16864 16863 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
16865 16864 DDI_DMA_SYNC_FORKERNEL);
16866 16865 ASSERT(rval == DDI_SUCCESS);
16867 16866 if (sata_check_for_dma_error(dip, spx)) {
16868 16867 ddi_fm_service_impact(dip,
16869 16868 DDI_SERVICE_UNAFFECTED);
16870 16869 rval = -1;
16871 16870 goto fail;
16872 16871 }
16873 16872 }
16874 16873 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data,
16875 16874 sizeof (struct smart_data));
16876 16875 }
16877 16876
16878 16877 fail:
16879 16878 /* Free allocated resources */
16880 16879 sata_free_local_buffer(spx);
16881 16880 sata_pkt_free(spx);
16882 16881 kmem_free(spx, sizeof (sata_pkt_txlate_t));
16883 16882
16884 16883 return (rval);
16885 16884 }
16886 16885
16887 16886 /*
16888 16887 * Used by LOG SENSE page 0x10
16889 16888 * Reads (in synchronous mode) the self test log data using Read Log Ext cmd.
16890 16889 * Note: cannot be called in the interrupt context.
16891 16890 *
16892 16891 * return 0 for success, -1 otherwise
16893 16892 *
16894 16893 */
16895 16894 static int
16896 16895 sata_ext_smart_selftest_read_log(
16897 16896 sata_hba_inst_t *sata_hba_inst,
16898 16897 sata_drive_info_t *sdinfo,
16899 16898 struct smart_ext_selftest_log *ext_selftest_log,
16900 16899 uint16_t block_num)
16901 16900 {
16902 16901 sata_pkt_txlate_t *spx;
16903 16902 sata_pkt_t *spkt;
16904 16903 sata_cmd_t *scmd;
16905 16904 int rval;
16906 16905 dev_info_t *dip = SATA_DIP(sata_hba_inst);
16907 16906
16908 16907 #if ! defined(lint)
16909 16908 ASSERT(sizeof (struct smart_ext_selftest_log) == 512);
16910 16909 #endif
16911 16910
16912 16911 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16913 16912 spx->txlt_sata_hba_inst = sata_hba_inst;
16914 16913 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */
16915 16914 spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16916 16915 if (spkt == NULL) {
16917 16916 kmem_free(spx, sizeof (sata_pkt_txlate_t));
16918 16917 return (-1);
16919 16918 }
16920 16919 /* address is needed now */
16921 16920 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16922 16921
16923 16922
16924 16923 /* Fill sata_pkt */
16925 16924 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16926 16925 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16927 16926 /* Synchronous mode, no callback */
16928 16927 spkt->satapkt_comp = NULL;
16929 16928 /* Timeout 30s */
16930 16929 spkt->satapkt_time = sata_default_pkt_time;
16931 16930
16932 16931 scmd = &spkt->satapkt_cmd;
16933 16932 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
16934 16933
16935 16934 /*
16936 16935 * Allocate buffer for SMART extended self-test log
16937 16936 */
16938 16937 scmd->satacmd_bp = sata_alloc_local_buffer(spx,
16939 16938 sizeof (struct smart_ext_selftest_log));
16940 16939 if (scmd->satacmd_bp == NULL) {
16941 16940 sata_pkt_free(spx);
16942 16941 kmem_free(spx, sizeof (sata_pkt_txlate_t));
16943 16942 SATA_LOG_D((sata_hba_inst, CE_WARN,
16944 16943 "sata_ext_smart_selftest_log: "
16945 16944 "cannot allocate buffer"));
16946 16945 return (-1);
16947 16946 }
16948 16947
16949 16948 /* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */
16950 16949 scmd->satacmd_addr_type = ATA_ADDR_LBA48;
16951 16950 scmd->satacmd_sec_count_lsb = 1; /* One sector of selftest log */
16952 16951 scmd->satacmd_sec_count_msb = 0; /* One sector of selftest log */
16953 16952 scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE;
16954 16953 scmd->satacmd_lba_low_msb = 0;
16955 16954 scmd->satacmd_lba_mid_lsb = block_num & 0xff;
16956 16955 scmd->satacmd_lba_mid_msb = block_num >> 8;
16957 16956 scmd->satacmd_device_reg = 0; /* Always device 0 */
16958 16957 scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
16959 16958
16960 16959 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16961 16960 sdinfo->satadrv_addr.cport)));
16962 16961
16963 16962 /* Send pkt to SATA HBA driver */
16964 16963 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16965 16964 SATA_TRAN_ACCEPTED ||
16966 16965 spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16967 16966 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16968 16967 sdinfo->satadrv_addr.cport)));
16969 16968
16970 16969 /*
16971 16970 * Whoops, no SMART selftest log info available
16972 16971 */
16973 16972 rval = -1;
16974 16973 goto fail;
16975 16974 } else {
16976 16975 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16977 16976 sdinfo->satadrv_addr.cport)));
16978 16977
16979 16978 if (spx->txlt_buf_dma_handle != NULL) {
16980 16979 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
16981 16980 DDI_DMA_SYNC_FORKERNEL);
16982 16981 ASSERT(rval == DDI_SUCCESS);
16983 16982 if (sata_check_for_dma_error(dip, spx)) {
16984 16983 ddi_fm_service_impact(dip,
16985 16984 DDI_SERVICE_UNAFFECTED);
16986 16985 rval = -1;
16987 16986 goto fail;
16988 16987 }
16989 16988 }
16990 16989 bcopy(scmd->satacmd_bp->b_un.b_addr,
16991 16990 (uint8_t *)ext_selftest_log,
16992 16991 sizeof (struct smart_ext_selftest_log));
16993 16992 rval = 0;
16994 16993 }
16995 16994
16996 16995 fail:
16997 16996 /* Free allocated resources */
16998 16997 sata_free_local_buffer(spx);
16999 16998 sata_pkt_free(spx);
17000 16999 kmem_free(spx, sizeof (sata_pkt_txlate_t));
17001 17000
17002 17001 return (rval);
17003 17002 }
17004 17003
17005 17004 /*
17006 17005 * Returns 0 for success, -1 otherwise
17007 17006 *
17008 17007 * SMART self-test log data is returned in buffer pointed to by selftest_log
17009 17008 */
17010 17009 static int
17011 17010 sata_smart_selftest_log(
17012 17011 sata_hba_inst_t *sata_hba_inst,
17013 17012 sata_drive_info_t *sdinfo,
17014 17013 struct smart_selftest_log *selftest_log)
17015 17014 {
17016 17015 sata_pkt_t *spkt;
17017 17016 sata_cmd_t *scmd;
17018 17017 sata_pkt_txlate_t *spx;
17019 17018 int rval;
17020 17019 dev_info_t *dip = SATA_DIP(sata_hba_inst);
17021 17020
17022 17021 #if ! defined(lint)
17023 17022 ASSERT(sizeof (struct smart_selftest_log) == 512);
17024 17023 #endif
17025 17024
17026 17025 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17027 17026 spx->txlt_sata_hba_inst = sata_hba_inst;
17028 17027 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */
17029 17028 spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17030 17029 if (spkt == NULL) {
17031 17030 kmem_free(spx, sizeof (sata_pkt_txlate_t));
17032 17031 return (-1);
17033 17032 }
17034 17033 /* address is needed now */
17035 17034 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17036 17035
17037 17036
17038 17037 /* Fill sata_pkt */
17039 17038 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17040 17039 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17041 17040 /* Synchronous mode, no callback */
17042 17041 spkt->satapkt_comp = NULL;
17043 17042 /* Timeout 30s */
17044 17043 spkt->satapkt_time = sata_default_pkt_time;
17045 17044
17046 17045 scmd = &spkt->satapkt_cmd;
17047 17046 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17048 17047
17049 17048 /*
17050 17049 * Allocate buffer for SMART SELFTEST LOG
17051 17050 */
17052 17051 scmd->satacmd_bp = sata_alloc_local_buffer(spx,
17053 17052 sizeof (struct smart_selftest_log));
17054 17053 if (scmd->satacmd_bp == NULL) {
17055 17054 sata_pkt_free(spx);
17056 17055 kmem_free(spx, sizeof (sata_pkt_txlate_t));
17057 17056 SATA_LOG_D((sata_hba_inst, CE_WARN,
17058 17057 "sata_smart_selftest_log: "
17059 17058 "cannot allocate buffer"));
17060 17059 return (-1);
17061 17060 }
17062 17061
17063 17062 /* Build SMART_READ_LOG cmd in the sata_pkt */
17064 17063 scmd->satacmd_addr_type = 0; /* N/A */
17065 17064 scmd->satacmd_sec_count_lsb = 1; /* One sector of SMART log */
17066 17065 scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE;
17067 17066 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
17068 17067 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
17069 17068 scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
17070 17069 scmd->satacmd_device_reg = 0; /* Always device 0 */
17071 17070 scmd->satacmd_cmd_reg = SATAC_SMART;
17072 17071 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17073 17072 sdinfo->satadrv_addr.cport)));
17074 17073
17075 17074 /* Send pkt to SATA HBA driver */
17076 17075 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17077 17076 SATA_TRAN_ACCEPTED ||
17078 17077 spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17079 17078 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17080 17079 sdinfo->satadrv_addr.cport)));
17081 17080 /*
17082 17081 * Whoops, no SMART DATA available
17083 17082 */
17084 17083 rval = -1;
17085 17084 goto fail;
17086 17085 } else {
17087 17086 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17088 17087 sdinfo->satadrv_addr.cport)));
17089 17088 if (spx->txlt_buf_dma_handle != NULL) {
17090 17089 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17091 17090 DDI_DMA_SYNC_FORKERNEL);
17092 17091 ASSERT(rval == DDI_SUCCESS);
17093 17092 if (sata_check_for_dma_error(dip, spx)) {
17094 17093 ddi_fm_service_impact(dip,
17095 17094 DDI_SERVICE_UNAFFECTED);
17096 17095 rval = -1;
17097 17096 goto fail;
17098 17097 }
17099 17098 }
17100 17099 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log,
17101 17100 sizeof (struct smart_selftest_log));
17102 17101 rval = 0;
17103 17102 }
17104 17103
17105 17104 fail:
17106 17105 /* Free allocated resources */
17107 17106 sata_free_local_buffer(spx);
17108 17107 sata_pkt_free(spx);
17109 17108 kmem_free(spx, sizeof (sata_pkt_txlate_t));
17110 17109
17111 17110 return (rval);
17112 17111 }
17113 17112
17114 17113
17115 17114 /*
17116 17115 * Returns 0 for success, -1 otherwise
17117 17116 *
17118 17117 * SMART READ LOG data is returned in buffer pointed to by smart_log
17119 17118 */
17120 17119 static int
17121 17120 sata_smart_read_log(
17122 17121 sata_hba_inst_t *sata_hba_inst,
17123 17122 sata_drive_info_t *sdinfo,
17124 17123 uint8_t *smart_log, /* where the data should be returned */
17125 17124 uint8_t which_log, /* which log should be returned */
17126 17125 uint8_t log_size) /* # of 512 bytes in log */
17127 17126 {
17128 17127 sata_pkt_t *spkt;
17129 17128 sata_cmd_t *scmd;
17130 17129 sata_pkt_txlate_t *spx;
17131 17130 int rval;
17132 17131 dev_info_t *dip = SATA_DIP(sata_hba_inst);
17133 17132
17134 17133 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17135 17134 spx->txlt_sata_hba_inst = sata_hba_inst;
17136 17135 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */
17137 17136 spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17138 17137 if (spkt == NULL) {
17139 17138 kmem_free(spx, sizeof (sata_pkt_txlate_t));
17140 17139 return (-1);
17141 17140 }
17142 17141 /* address is needed now */
17143 17142 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17144 17143
17145 17144
17146 17145 /* Fill sata_pkt */
17147 17146 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17148 17147 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17149 17148 /* Synchronous mode, no callback */
17150 17149 spkt->satapkt_comp = NULL;
17151 17150 /* Timeout 30s */
17152 17151 spkt->satapkt_time = sata_default_pkt_time;
17153 17152
17154 17153 scmd = &spkt->satapkt_cmd;
17155 17154 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17156 17155
17157 17156 /*
17158 17157 * Allocate buffer for SMART READ LOG
17159 17158 */
17160 17159 scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512);
17161 17160 if (scmd->satacmd_bp == NULL) {
17162 17161 sata_pkt_free(spx);
17163 17162 kmem_free(spx, sizeof (sata_pkt_txlate_t));
17164 17163 SATA_LOG_D((sata_hba_inst, CE_WARN,
17165 17164 "sata_smart_read_log: " "cannot allocate buffer"));
17166 17165 return (-1);
17167 17166 }
17168 17167
17169 17168 /* Build SMART_READ_LOG cmd in the sata_pkt */
17170 17169 scmd->satacmd_addr_type = 0; /* N/A */
17171 17170 scmd->satacmd_sec_count_lsb = log_size; /* what the caller asked for */
17172 17171 scmd->satacmd_lba_low_lsb = which_log; /* which log page */
17173 17172 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
17174 17173 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
17175 17174 scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
17176 17175 scmd->satacmd_device_reg = 0; /* Always device 0 */
17177 17176 scmd->satacmd_cmd_reg = SATAC_SMART;
17178 17177
17179 17178 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17180 17179 sdinfo->satadrv_addr.cport)));
17181 17180
17182 17181 /* Send pkt to SATA HBA driver */
17183 17182 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17184 17183 SATA_TRAN_ACCEPTED ||
17185 17184 spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17186 17185 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17187 17186 sdinfo->satadrv_addr.cport)));
17188 17187
17189 17188 /*
17190 17189 * Whoops, no SMART DATA available
17191 17190 */
17192 17191 rval = -1;
17193 17192 goto fail;
17194 17193 } else {
17195 17194 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17196 17195 sdinfo->satadrv_addr.cport)));
17197 17196
17198 17197 if (spx->txlt_buf_dma_handle != NULL) {
17199 17198 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17200 17199 DDI_DMA_SYNC_FORKERNEL);
17201 17200 ASSERT(rval == DDI_SUCCESS);
17202 17201 if (sata_check_for_dma_error(dip, spx)) {
17203 17202 ddi_fm_service_impact(dip,
17204 17203 DDI_SERVICE_UNAFFECTED);
17205 17204 rval = -1;
17206 17205 goto fail;
17207 17206 }
17208 17207 }
17209 17208 bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512);
17210 17209 rval = 0;
17211 17210 }
17212 17211
17213 17212 fail:
17214 17213 /* Free allocated resources */
17215 17214 sata_free_local_buffer(spx);
17216 17215 sata_pkt_free(spx);
17217 17216 kmem_free(spx, sizeof (sata_pkt_txlate_t));
17218 17217
17219 17218 return (rval);
17220 17219 }
17221 17220
17222 17221 /*
17223 17222 * Used by LOG SENSE page 0x10
17224 17223 *
17225 17224 * return 0 for success, -1 otherwise
17226 17225 *
17227 17226 */
17228 17227 static int
17229 17228 sata_read_log_ext_directory(
17230 17229 sata_hba_inst_t *sata_hba_inst,
17231 17230 sata_drive_info_t *sdinfo,
17232 17231 struct read_log_ext_directory *logdir)
17233 17232 {
17234 17233 sata_pkt_txlate_t *spx;
17235 17234 sata_pkt_t *spkt;
17236 17235 sata_cmd_t *scmd;
17237 17236 int rval;
17238 17237 dev_info_t *dip = SATA_DIP(sata_hba_inst);
17239 17238
17240 17239 #if ! defined(lint)
17241 17240 ASSERT(sizeof (struct read_log_ext_directory) == 512);
17242 17241 #endif
17243 17242
17244 17243 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17245 17244 spx->txlt_sata_hba_inst = sata_hba_inst;
17246 17245 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */
17247 17246 spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17248 17247 if (spkt == NULL) {
17249 17248 kmem_free(spx, sizeof (sata_pkt_txlate_t));
17250 17249 return (-1);
17251 17250 }
17252 17251
17253 17252 /* Fill sata_pkt */
17254 17253 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17255 17254 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17256 17255 /* Synchronous mode, no callback */
17257 17256 spkt->satapkt_comp = NULL;
17258 17257 /* Timeout 30s */
17259 17258 spkt->satapkt_time = sata_default_pkt_time;
17260 17259
17261 17260 scmd = &spkt->satapkt_cmd;
17262 17261 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17263 17262
17264 17263 /*
17265 17264 * Allocate buffer for SMART READ LOG EXTENDED command
17266 17265 */
17267 17266 scmd->satacmd_bp = sata_alloc_local_buffer(spx,
17268 17267 sizeof (struct read_log_ext_directory));
17269 17268 if (scmd->satacmd_bp == NULL) {
17270 17269 sata_pkt_free(spx);
17271 17270 kmem_free(spx, sizeof (sata_pkt_txlate_t));
17272 17271 SATA_LOG_D((sata_hba_inst, CE_WARN,
17273 17272 "sata_read_log_ext_directory: "
17274 17273 "cannot allocate buffer"));
17275 17274 return (-1);
17276 17275 }
17277 17276
17278 17277 /* Build READ LOG EXT w/ log directory cmd in the sata_pkt */
17279 17278 scmd->satacmd_addr_type = ATA_ADDR_LBA48;
17280 17279 scmd->satacmd_sec_count_lsb = 1; /* One sector of directory */
17281 17280 scmd->satacmd_sec_count_msb = 0; /* One sector of directory */
17282 17281 scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY;
17283 17282 scmd->satacmd_lba_low_msb = 0;
17284 17283 scmd->satacmd_lba_mid_lsb = 0;
17285 17284 scmd->satacmd_lba_mid_msb = 0;
17286 17285 scmd->satacmd_device_reg = 0; /* Always device 0 */
17287 17286 scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
17288 17287
17289 17288 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17290 17289 sdinfo->satadrv_addr.cport)));
17291 17290
17292 17291 /* Send pkt to SATA HBA driver */
17293 17292 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17294 17293 SATA_TRAN_ACCEPTED ||
17295 17294 spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17296 17295 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17297 17296 sdinfo->satadrv_addr.cport)));
17298 17297 /*
17299 17298 * Whoops, no SMART selftest log info available
17300 17299 */
17301 17300 rval = -1;
17302 17301 goto fail;
17303 17302 } else {
17304 17303 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17305 17304 sdinfo->satadrv_addr.cport)));
17306 17305 if (spx->txlt_buf_dma_handle != NULL) {
17307 17306 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17308 17307 DDI_DMA_SYNC_FORKERNEL);
17309 17308 ASSERT(rval == DDI_SUCCESS);
17310 17309 if (sata_check_for_dma_error(dip, spx)) {
17311 17310 ddi_fm_service_impact(dip,
17312 17311 DDI_SERVICE_UNAFFECTED);
17313 17312 rval = -1;
17314 17313 goto fail;
17315 17314 }
17316 17315 }
17317 17316 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir,
17318 17317 sizeof (struct read_log_ext_directory));
17319 17318 rval = 0;
17320 17319 }
17321 17320
17322 17321 fail:
17323 17322 /* Free allocated resources */
17324 17323 sata_free_local_buffer(spx);
17325 17324 sata_pkt_free(spx);
17326 17325 kmem_free(spx, sizeof (sata_pkt_txlate_t));
17327 17326
17328 17327 return (rval);
17329 17328 }
17330 17329
17331 17330 /*
17332 17331 * Set up error retrieval sata command for NCQ command error data
17333 17332 * recovery.
17334 17333 *
17335 17334 * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
17336 17335 * returns SATA_FAILURE otherwise.
17337 17336 */
17338 17337 static int
17339 17338 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
17340 17339 {
17341 17340 #ifndef __lock_lint
17342 17341 _NOTE(ARGUNUSED(sdinfo))
17343 17342 #endif
17344 17343
17345 17344 sata_pkt_t *spkt = spx->txlt_sata_pkt;
17346 17345 sata_cmd_t *scmd;
17347 17346 struct buf *bp;
17348 17347
17349 17348 /* Operation modes are up to the caller */
17350 17349 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17351 17350
17352 17351 /* Synchronous mode, no callback - may be changed by the caller */
17353 17352 spkt->satapkt_comp = NULL;
17354 17353 spkt->satapkt_time = sata_default_pkt_time;
17355 17354
17356 17355 scmd = &spkt->satapkt_cmd;
17357 17356 bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t));
17358 17357 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
17359 17358
17360 17359 /*
17361 17360 * Allocate dma_able buffer error data.
17362 17361 * Buffer allocation will take care of buffer alignment and other DMA
17363 17362 * attributes.
17364 17363 */
17365 17364 bp = sata_alloc_local_buffer(spx,
17366 17365 sizeof (struct sata_ncq_error_recovery_page));
17367 17366 if (bp == NULL)
17368 17367 return (SATA_FAILURE);
17369 17368
17370 17369 bp_mapin(bp); /* make data buffer accessible */
17371 17370 scmd->satacmd_bp = bp;
17372 17371
17373 17372 /*
17374 17373 * Set-up pointer to the buffer handle, so HBA can sync buffer
17375 17374 * before accessing it. Handle is in usual place in translate struct.
17376 17375 */
17377 17376 scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
17378 17377
17379 17378 ASSERT(scmd->satacmd_num_dma_cookies != 0);
17380 17379 ASSERT(scmd->satacmd_dma_cookie_list != NULL);
17381 17380
17382 17381 return (SATA_SUCCESS);
17383 17382 }
17384 17383
17385 17384 /*
17386 17385 * sata_xlate_errors() is used to translate (S)ATA error
17387 17386 * information to SCSI information returned in the SCSI
17388 17387 * packet.
17389 17388 */
17390 17389 static void
17391 17390 sata_xlate_errors(sata_pkt_txlate_t *spx)
17392 17391 {
17393 17392 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
17394 17393 struct scsi_extended_sense *sense;
17395 17394
17396 17395 scsipkt->pkt_reason = CMD_INCOMPLETE;
17397 17396 *scsipkt->pkt_scbp = STATUS_CHECK;
17398 17397 sense = sata_arq_sense(spx);
17399 17398
17400 17399 switch (spx->txlt_sata_pkt->satapkt_reason) {
17401 17400 case SATA_PKT_PORT_ERROR:
17402 17401 /*
17403 17402 * We have no device data. Assume no data transfered.
17404 17403 */
17405 17404 sense->es_key = KEY_HARDWARE_ERROR;
17406 17405 break;
17407 17406
17408 17407 case SATA_PKT_DEV_ERROR:
17409 17408 if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
17410 17409 SATA_STATUS_ERR) {
17411 17410 /*
17412 17411 * determine dev error reason from error
17413 17412 * reg content
17414 17413 */
17415 17414 sata_decode_device_error(spx, sense);
17416 17415 break;
17417 17416 }
17418 17417 /* No extended sense key - no info available */
17419 17418 break;
17420 17419
17421 17420 case SATA_PKT_TIMEOUT:
17422 17421 scsipkt->pkt_reason = CMD_TIMEOUT;
17423 17422 scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET;
17424 17423 /* No extended sense key */
17425 17424 break;
17426 17425
17427 17426 case SATA_PKT_ABORTED:
17428 17427 scsipkt->pkt_reason = CMD_ABORTED;
17429 17428 scsipkt->pkt_statistics |= STAT_ABORTED;
17430 17429 /* No extended sense key */
17431 17430 break;
17432 17431
17433 17432 case SATA_PKT_RESET:
17434 17433 /*
17435 17434 * pkt aborted either by an explicit reset request from
17436 17435 * a host, or due to error recovery
17437 17436 */
17438 17437 scsipkt->pkt_reason = CMD_RESET;
17439 17438 scsipkt->pkt_statistics |= STAT_DEV_RESET;
17440 17439 break;
17441 17440
17442 17441 default:
17443 17442 scsipkt->pkt_reason = CMD_TRAN_ERR;
17444 17443 break;
17445 17444 }
17446 17445 }
17447 17446
17448 17447
17449 17448
17450 17449
17451 17450 /*
17452 17451 * Log sata message
17453 17452 * dev pathname msg line preceeds the logged message.
17454 17453 */
17455 17454
17456 17455 static void
17457 17456 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...)
17458 17457 {
17459 17458 char pathname[128];
17460 17459 dev_info_t *dip = NULL;
17461 17460 va_list ap;
17462 17461
17463 17462 mutex_enter(&sata_log_mutex);
17464 17463
17465 17464 va_start(ap, fmt);
17466 17465 (void) vsprintf(sata_log_buf, fmt, ap);
17467 17466 va_end(ap);
17468 17467
17469 17468 if (sata_hba_inst != NULL) {
17470 17469 dip = SATA_DIP(sata_hba_inst);
17471 17470 (void) ddi_pathname(dip, pathname);
17472 17471 } else {
17473 17472 pathname[0] = 0;
17474 17473 }
17475 17474 if (level == CE_CONT) {
17476 17475 if (sata_debug_flags == 0)
17477 17476 cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf);
17478 17477 else
17479 17478 cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf);
17480 17479 } else {
17481 17480 if (level != CE_NOTE) {
17482 17481 cmn_err(level, "%s:\n %s", pathname, sata_log_buf);
17483 17482 } else if (sata_msg) {
17484 17483 cmn_err(level, "%s:\n %s", pathname,
17485 17484 sata_log_buf);
17486 17485 }
17487 17486 }
17488 17487
17489 17488 /* sata trace debug */
17490 17489 sata_trace_debug(dip, sata_log_buf);
17491 17490
17492 17491 mutex_exit(&sata_log_mutex);
17493 17492 }
17494 17493
17495 17494
17496 17495 /* ******** Asynchronous HBA events handling & hotplugging support ******** */
17497 17496
17498 17497 /*
17499 17498 * Start or terminate the thread, depending on flag arg and current state
17500 17499 */
17501 17500 static void
17502 17501 sata_event_thread_control(int startstop)
17503 17502 {
17504 17503 static int sata_event_thread_terminating = 0;
17505 17504 static int sata_event_thread_starting = 0;
17506 17505 int i;
17507 17506
17508 17507 mutex_enter(&sata_event_mutex);
17509 17508
17510 17509 if (startstop == 0 && (sata_event_thread_starting == 1 ||
17511 17510 sata_event_thread_terminating == 1)) {
17512 17511 mutex_exit(&sata_event_mutex);
17513 17512 return;
17514 17513 }
17515 17514 if (startstop == 1 && sata_event_thread_starting == 1) {
17516 17515 mutex_exit(&sata_event_mutex);
17517 17516 return;
17518 17517 }
17519 17518 if (startstop == 1 && sata_event_thread_terminating == 1) {
17520 17519 sata_event_thread_starting = 1;
17521 17520 /* wait til terminate operation completes */
17522 17521 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
17523 17522 while (sata_event_thread_terminating == 1) {
17524 17523 if (i-- <= 0) {
17525 17524 sata_event_thread_starting = 0;
17526 17525 mutex_exit(&sata_event_mutex);
17527 17526 #ifdef SATA_DEBUG
17528 17527 cmn_err(CE_WARN, "sata_event_thread_control: "
17529 17528 "timeout waiting for thread to terminate");
17530 17529 #endif
17531 17530 return;
17532 17531 }
17533 17532 mutex_exit(&sata_event_mutex);
17534 17533 delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
17535 17534 mutex_enter(&sata_event_mutex);
17536 17535 }
17537 17536 }
17538 17537 if (startstop == 1) {
17539 17538 if (sata_event_thread == NULL) {
17540 17539 sata_event_thread = thread_create(NULL, 0,
17541 17540 (void (*)())sata_event_daemon,
17542 17541 &sata_hba_list, 0, &p0, TS_RUN, minclsyspri);
17543 17542 }
17544 17543 sata_event_thread_starting = 0;
17545 17544 mutex_exit(&sata_event_mutex);
17546 17545 return;
17547 17546 }
17548 17547
17549 17548 /*
17550 17549 * If we got here, thread may need to be terminated
17551 17550 */
17552 17551 if (sata_event_thread != NULL) {
17553 17552 int i;
17554 17553 /* Signal event thread to go away */
17555 17554 sata_event_thread_terminating = 1;
17556 17555 sata_event_thread_terminate = 1;
17557 17556 cv_signal(&sata_event_cv);
17558 17557 /*
17559 17558 * Wait til daemon terminates.
17560 17559 */
17561 17560 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
17562 17561 while (sata_event_thread_terminate == 1) {
17563 17562 mutex_exit(&sata_event_mutex);
17564 17563 if (i-- <= 0) {
17565 17564 /* Daemon did not go away !!! */
17566 17565 #ifdef SATA_DEBUG
17567 17566 cmn_err(CE_WARN, "sata_event_thread_control: "
17568 17567 "cannot terminate event daemon thread");
17569 17568 #endif
17570 17569 mutex_enter(&sata_event_mutex);
17571 17570 break;
17572 17571 }
17573 17572 delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
17574 17573 mutex_enter(&sata_event_mutex);
17575 17574 }
17576 17575 sata_event_thread_terminating = 0;
17577 17576 }
17578 17577 ASSERT(sata_event_thread_terminating == 0);
17579 17578 ASSERT(sata_event_thread_starting == 0);
17580 17579 mutex_exit(&sata_event_mutex);
17581 17580 }
17582 17581
17583 17582
17584 17583 /*
17585 17584 * SATA HBA event notification function.
17586 17585 * Events reported by SATA HBA drivers per HBA instance relate to a change in
17587 17586 * a port and/or device state or a controller itself.
17588 17587 * Events for different addresses/addr types cannot be combined.
17589 17588 * A warning message is generated for each event type.
17590 17589 * Events are not processed by this function, so only the
17591 17590 * event flag(s)is set for an affected entity and the event thread is
17592 17591 * waken up. Event daemon thread processes all events.
17593 17592 *
17594 17593 * NOTE: Since more than one event may be reported at the same time, one
17595 17594 * cannot determine a sequence of events when opposite event are reported, eg.
17596 17595 * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing
17597 17596 * is taking precedence over reported events, i.e. may cause ignoring some
17598 17597 * events.
17599 17598 */
17600 17599 #define SATA_EVENT_MAX_MSG_LENGTH 79
17601 17600
17602 17601 void
17603 17602 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event)
17604 17603 {
17605 17604 sata_hba_inst_t *sata_hba_inst = NULL;
17606 17605 sata_address_t *saddr;
17607 17606 sata_pmult_info_t *pmultinfo;
17608 17607 sata_drive_info_t *sdinfo;
17609 17608 sata_port_stats_t *pstats;
17610 17609 sata_cport_info_t *cportinfo;
17611 17610 sata_pmport_info_t *pmportinfo;
17612 17611 int cport, pmport;
17613 17612 char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1];
17614 17613 char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1];
17615 17614 char *lcp;
17616 17615 static char *err_msg_evnt_1 =
17617 17616 "sata_hba_event_notify: invalid port event 0x%x ";
17618 17617 static char *err_msg_evnt_2 =
17619 17618 "sata_hba_event_notify: invalid device event 0x%x ";
17620 17619 int linkevent;
17621 17620
17622 17621 /*
17623 17622 * There is a possibility that an event will be generated on HBA
17624 17623 * that has not completed attachment or is detaching. We still want
17625 17624 * to process events until HBA is detached.
17626 17625 */
17627 17626 mutex_enter(&sata_mutex);
17628 17627 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
17629 17628 sata_hba_inst = sata_hba_inst->satahba_next) {
17630 17629 if (SATA_DIP(sata_hba_inst) == dip)
17631 17630 if (sata_hba_inst->satahba_attached == 1)
17632 17631 break;
17633 17632 }
17634 17633 mutex_exit(&sata_mutex);
17635 17634 if (sata_hba_inst == NULL)
17636 17635 /* HBA not attached */
17637 17636 return;
17638 17637
17639 17638 ASSERT(sata_device != NULL);
17640 17639
17641 17640 /*
17642 17641 * Validate address before - do not proceed with invalid address.
17643 17642 */
17644 17643 saddr = &sata_device->satadev_addr;
17645 17644 if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst))
17646 17645 return;
17647 17646
17648 17647 cport = saddr->cport;
17649 17648 pmport = saddr->pmport;
17650 17649
17651 17650 buf1[0] = buf2[0] = '\0';
17652 17651
17653 17652 /*
17654 17653 * If event relates to port or device, check port state.
17655 17654 * Port has to be initialized, or we cannot accept an event.
17656 17655 */
17657 17656 if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT |
17658 17657 SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT | SATA_ADDR_PMULT)) != 0) {
17659 17658 mutex_enter(&sata_hba_inst->satahba_mutex);
17660 17659 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
17661 17660 mutex_exit(&sata_hba_inst->satahba_mutex);
17662 17661 if (cportinfo == NULL || cportinfo->cport_state == 0)
17663 17662 return;
17664 17663 }
17665 17664
17666 17665 if ((saddr->qual & (SATA_ADDR_PMULT | SATA_ADDR_PMPORT |
17667 17666 SATA_ADDR_DPMPORT)) != 0) {
17668 17667 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
17669 17668 SATA_LOG_D((sata_hba_inst, CE_WARN,
17670 17669 "sata_hba_event_notify: Non-pmult device (0x%x)"
17671 17670 "is attached to port %d, ignore pmult/pmport "
17672 17671 "event 0x%x", cportinfo->cport_dev_type,
17673 17672 cport, event));
17674 17673 return;
17675 17674 }
17676 17675
17677 17676 mutex_enter(&cportinfo->cport_mutex);
17678 17677 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
17679 17678 mutex_exit(&cportinfo->cport_mutex);
17680 17679
17681 17680 /*
17682 17681 * The daemon might be processing attachment of port
17683 17682 * multiplier, in that case we should ignore events on its
17684 17683 * sub-devices.
17685 17684 *
17686 17685 * NOTE: Only pmult_state is checked in sata_hba_event_notify.
17687 17686 * The pmport_state is checked by sata daemon.
17688 17687 */
17689 17688 if (pmultinfo == NULL ||
17690 17689 pmultinfo->pmult_state == SATA_STATE_UNKNOWN) {
17691 17690 SATA_LOG_D((sata_hba_inst, CE_WARN,
17692 17691 "sata_hba_event_notify: pmult is not"
17693 17692 "available at port %d:%d, ignore event 0x%x",
17694 17693 cport, pmport, event));
17695 17694 return;
17696 17695 }
17697 17696 }
17698 17697
17699 17698 if ((saddr->qual &
17700 17699 (SATA_ADDR_PMPORT | SATA_ADDR_DPMPORT)) != 0) {
17701 17700
17702 17701 mutex_enter(&cportinfo->cport_mutex);
17703 17702 if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) {
17704 17703 SATA_LOG_D((sata_hba_inst, CE_WARN,
17705 17704 "sata_hba_event_notify: invalid/"
17706 17705 "un-implemented port %d:%d (%d ports), "
17707 17706 "ignore event 0x%x", cport, pmport,
17708 17707 SATA_NUM_PMPORTS(sata_hba_inst, cport), event));
17709 17708 mutex_exit(&cportinfo->cport_mutex);
17710 17709 return;
17711 17710 }
17712 17711 mutex_exit(&cportinfo->cport_mutex);
17713 17712
17714 17713 mutex_enter(&sata_hba_inst->satahba_mutex);
17715 17714 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
17716 17715 cport, pmport);
17717 17716 mutex_exit(&sata_hba_inst->satahba_mutex);
17718 17717
17719 17718 /* pmport is implemented/valid? */
17720 17719 if (pmportinfo == NULL) {
17721 17720 SATA_LOG_D((sata_hba_inst, CE_WARN,
17722 17721 "sata_hba_event_notify: invalid/"
17723 17722 "un-implemented port %d:%d, ignore "
17724 17723 "event 0x%x", cport, pmport, event));
17725 17724 return;
17726 17725 }
17727 17726 }
17728 17727
17729 17728 /*
17730 17729 * Events refer to devices, ports and controllers - each has
17731 17730 * unique address. Events for different addresses cannot be combined.
17732 17731 */
17733 17732 if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) {
17734 17733
17735 17734 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17736 17735
17737 17736 /* qualify this event(s) */
17738 17737 if ((event & SATA_EVNT_PORT_EVENTS) == 0) {
17739 17738 /* Invalid event for the device port */
17740 17739 (void) sprintf(buf2, err_msg_evnt_1,
17741 17740 event & SATA_EVNT_PORT_EVENTS);
17742 17741 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17743 17742 goto event_info;
17744 17743 }
17745 17744 if (saddr->qual == SATA_ADDR_CPORT) {
17746 17745 /* Controller's device port event */
17747 17746
17748 17747 (SATA_CPORT_INFO(sata_hba_inst, cport))->
17749 17748 cport_event_flags |=
17750 17749 event & SATA_EVNT_PORT_EVENTS;
17751 17750 pstats =
17752 17751 &(SATA_CPORT_INFO(sata_hba_inst, cport))->
17753 17752 cport_stats;
17754 17753 } else {
17755 17754 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17756 17755 mutex_enter(&pmportinfo->pmport_mutex);
17757 17756 /* Port multiplier's device port event */
17758 17757 (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
17759 17758 pmport_event_flags |=
17760 17759 event & SATA_EVNT_PORT_EVENTS;
17761 17760 pstats =
17762 17761 &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
17763 17762 pmport_stats;
17764 17763 mutex_exit(&pmportinfo->pmport_mutex);
17765 17764 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17766 17765 }
17767 17766
17768 17767 /*
17769 17768 * Add to statistics and log the message. We have to do it
17770 17769 * here rather than in the event daemon, because there may be
17771 17770 * multiple events occuring before they are processed.
17772 17771 */
17773 17772 linkevent = event &
17774 17773 (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED);
17775 17774 if (linkevent) {
17776 17775 if (linkevent == (SATA_EVNT_LINK_LOST |
17777 17776 SATA_EVNT_LINK_ESTABLISHED)) {
17778 17777 /* This is likely event combination */
17779 17778 (void) strlcat(buf1, "link lost/established, ",
17780 17779 SATA_EVENT_MAX_MSG_LENGTH);
17781 17780
17782 17781 if (pstats->link_lost < 0xffffffffffffffffULL)
17783 17782 pstats->link_lost++;
17784 17783 if (pstats->link_established <
17785 17784 0xffffffffffffffffULL)
17786 17785 pstats->link_established++;
17787 17786 linkevent = 0;
17788 17787 } else if (linkevent & SATA_EVNT_LINK_LOST) {
17789 17788 (void) strlcat(buf1, "link lost, ",
17790 17789 SATA_EVENT_MAX_MSG_LENGTH);
17791 17790
17792 17791 if (pstats->link_lost < 0xffffffffffffffffULL)
17793 17792 pstats->link_lost++;
17794 17793 } else {
17795 17794 (void) strlcat(buf1, "link established, ",
17796 17795 SATA_EVENT_MAX_MSG_LENGTH);
17797 17796 if (pstats->link_established <
17798 17797 0xffffffffffffffffULL)
17799 17798 pstats->link_established++;
17800 17799 }
17801 17800 }
17802 17801 if (event & SATA_EVNT_DEVICE_ATTACHED) {
17803 17802 (void) strlcat(buf1, "device attached, ",
17804 17803 SATA_EVENT_MAX_MSG_LENGTH);
17805 17804 if (pstats->device_attached < 0xffffffffffffffffULL)
17806 17805 pstats->device_attached++;
17807 17806 }
17808 17807 if (event & SATA_EVNT_DEVICE_DETACHED) {
17809 17808 (void) strlcat(buf1, "device detached, ",
17810 17809 SATA_EVENT_MAX_MSG_LENGTH);
17811 17810 if (pstats->device_detached < 0xffffffffffffffffULL)
17812 17811 pstats->device_detached++;
17813 17812 }
17814 17813 if (event & SATA_EVNT_PWR_LEVEL_CHANGED) {
17815 17814 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
17816 17815 "port %d power level changed", cport);
17817 17816 if (pstats->port_pwr_changed < 0xffffffffffffffffULL)
17818 17817 pstats->port_pwr_changed++;
17819 17818 }
17820 17819
17821 17820 if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) {
17822 17821 /* There should be no other events for this address */
17823 17822 (void) sprintf(buf2, err_msg_evnt_1,
17824 17823 event & ~SATA_EVNT_PORT_EVENTS);
17825 17824 }
17826 17825 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17827 17826
17828 17827 } else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) {
17829 17828 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17830 17829
17831 17830 /* qualify this event */
17832 17831 if ((event & SATA_EVNT_DEVICE_RESET) == 0) {
17833 17832 /* Invalid event for a device */
17834 17833 (void) sprintf(buf2, err_msg_evnt_2,
17835 17834 event & SATA_EVNT_DEVICE_RESET);
17836 17835 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17837 17836 goto event_info;
17838 17837 }
17839 17838 /* drive event */
17840 17839 sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
17841 17840 if (sdinfo != NULL) {
17842 17841 if (event & SATA_EVNT_DEVICE_RESET) {
17843 17842 (void) strlcat(buf1, "device reset, ",
17844 17843 SATA_EVENT_MAX_MSG_LENGTH);
17845 17844 if (sdinfo->satadrv_stats.drive_reset <
17846 17845 0xffffffffffffffffULL)
17847 17846 sdinfo->satadrv_stats.drive_reset++;
17848 17847 sdinfo->satadrv_event_flags |=
17849 17848 SATA_EVNT_DEVICE_RESET;
17850 17849 }
17851 17850 }
17852 17851 if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) {
17853 17852 /* Invalid event for a device */
17854 17853 (void) sprintf(buf2, err_msg_evnt_2,
17855 17854 event & ~SATA_EVNT_DRIVE_EVENTS);
17856 17855 }
17857 17856 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17858 17857 } else if (saddr->qual == SATA_ADDR_PMULT) {
17859 17858 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17860 17859
17861 17860 /* qualify this event */
17862 17861 if ((event & (SATA_EVNT_DEVICE_RESET |
17863 17862 SATA_EVNT_PMULT_LINK_CHANGED)) == 0) {
17864 17863 /* Invalid event for a port multiplier */
17865 17864 (void) sprintf(buf2, err_msg_evnt_2,
17866 17865 event & SATA_EVNT_DEVICE_RESET);
17867 17866 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17868 17867 goto event_info;
17869 17868 }
17870 17869
17871 17870 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
17872 17871
17873 17872 if (event & SATA_EVNT_DEVICE_RESET) {
17874 17873
17875 17874 SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
17876 17875 "[Reset] port-mult on cport %d", cport);
17877 17876 pmultinfo->pmult_event_flags |=
17878 17877 SATA_EVNT_DEVICE_RESET;
17879 17878 (void) strlcat(buf1, "pmult reset, ",
17880 17879 SATA_EVENT_MAX_MSG_LENGTH);
17881 17880 }
17882 17881
17883 17882 if (event & SATA_EVNT_PMULT_LINK_CHANGED) {
17884 17883
17885 17884 SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
17886 17885 "pmult link changed on cport %d", cport);
17887 17886 pmultinfo->pmult_event_flags |=
17888 17887 SATA_EVNT_PMULT_LINK_CHANGED;
17889 17888 (void) strlcat(buf1, "pmult link changed, ",
17890 17889 SATA_EVENT_MAX_MSG_LENGTH);
17891 17890 }
17892 17891 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17893 17892
17894 17893 } else {
17895 17894 if (saddr->qual != SATA_ADDR_NULL) {
17896 17895 /* Wrong address qualifier */
17897 17896 SATA_LOG_D((sata_hba_inst, CE_WARN,
17898 17897 "sata_hba_event_notify: invalid address 0x%x",
17899 17898 *(uint32_t *)saddr));
17900 17899 return;
17901 17900 }
17902 17901 if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 ||
17903 17902 (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) {
17904 17903 /* Invalid event for the controller */
17905 17904 SATA_LOG_D((sata_hba_inst, CE_WARN,
17906 17905 "sata_hba_event_notify: invalid event 0x%x for "
17907 17906 "controller",
17908 17907 event & SATA_EVNT_CONTROLLER_EVENTS));
17909 17908 return;
17910 17909 }
17911 17910 buf1[0] = '\0';
17912 17911 /* This may be a frequent and not interesting event */
17913 17912 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
17914 17913 "controller power level changed\n", NULL);
17915 17914
17916 17915 mutex_enter(&sata_hba_inst->satahba_mutex);
17917 17916 if (sata_hba_inst->satahba_stats.ctrl_pwr_change <
17918 17917 0xffffffffffffffffULL)
17919 17918 sata_hba_inst->satahba_stats.ctrl_pwr_change++;
17920 17919
17921 17920 sata_hba_inst->satahba_event_flags |=
17922 17921 SATA_EVNT_PWR_LEVEL_CHANGED;
17923 17922 mutex_exit(&sata_hba_inst->satahba_mutex);
17924 17923 }
17925 17924 /*
17926 17925 * If we got here, there is something to do with this HBA
17927 17926 * instance.
17928 17927 */
17929 17928 mutex_enter(&sata_hba_inst->satahba_mutex);
17930 17929 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
17931 17930 mutex_exit(&sata_hba_inst->satahba_mutex);
17932 17931 mutex_enter(&sata_mutex);
17933 17932 sata_event_pending |= SATA_EVNT_MAIN; /* global event indicator */
17934 17933 mutex_exit(&sata_mutex);
17935 17934
17936 17935 /* Tickle event thread */
17937 17936 mutex_enter(&sata_event_mutex);
17938 17937 if (sata_event_thread_active == 0)
17939 17938 cv_signal(&sata_event_cv);
17940 17939 mutex_exit(&sata_event_mutex);
17941 17940
17942 17941 event_info:
17943 17942 if (buf1[0] != '\0') {
17944 17943 lcp = strrchr(buf1, ',');
17945 17944 if (lcp != NULL)
17946 17945 *lcp = '\0';
17947 17946 }
17948 17947 if (saddr->qual == SATA_ADDR_CPORT ||
17949 17948 saddr->qual == SATA_ADDR_DCPORT) {
17950 17949 if (buf1[0] != '\0') {
17951 17950 sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
17952 17951 cport, buf1);
17953 17952 }
17954 17953 if (buf2[0] != '\0') {
17955 17954 sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
17956 17955 cport, buf2);
17957 17956 }
17958 17957 } else if (saddr->qual == SATA_ADDR_PMPORT ||
17959 17958 saddr->qual == SATA_ADDR_DPMPORT) {
17960 17959 if (buf1[0] != '\0') {
17961 17960 sata_log(sata_hba_inst, CE_NOTE,
17962 17961 "port %d pmport %d: %s\n", cport, pmport, buf1);
17963 17962 }
17964 17963 if (buf2[0] != '\0') {
17965 17964 sata_log(sata_hba_inst, CE_NOTE,
17966 17965 "port %d pmport %d: %s\n", cport, pmport, buf2);
17967 17966 }
17968 17967 }
17969 17968 }
17970 17969
17971 17970
17972 17971 /*
17973 17972 * Event processing thread.
17974 17973 * Arg is a pointer to the sata_hba_list pointer.
17975 17974 * It is not really needed, because sata_hba_list is global and static
17976 17975 */
17977 17976 static void
17978 17977 sata_event_daemon(void *arg)
17979 17978 {
17980 17979 #ifndef __lock_lint
17981 17980 _NOTE(ARGUNUSED(arg))
17982 17981 #endif
17983 17982 sata_hba_inst_t *sata_hba_inst;
17984 17983 clock_t delta;
17985 17984
17986 17985 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
17987 17986 "SATA event daemon started\n", NULL);
17988 17987 loop:
17989 17988 /*
17990 17989 * Process events here. Walk through all registered HBAs
17991 17990 */
17992 17991 mutex_enter(&sata_mutex);
17993 17992 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
17994 17993 sata_hba_inst = sata_hba_inst->satahba_next) {
17995 17994 ASSERT(sata_hba_inst != NULL);
17996 17995 mutex_enter(&sata_hba_inst->satahba_mutex);
17997 17996 if (sata_hba_inst->satahba_attached == 0 ||
17998 17997 (sata_hba_inst->satahba_event_flags &
17999 17998 SATA_EVNT_SKIP) != 0) {
18000 17999 mutex_exit(&sata_hba_inst->satahba_mutex);
18001 18000 continue;
18002 18001 }
18003 18002 if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) {
18004 18003 sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP;
18005 18004 mutex_exit(&sata_hba_inst->satahba_mutex);
18006 18005 mutex_exit(&sata_mutex);
18007 18006 /* Got the controller with pending event */
18008 18007 sata_process_controller_events(sata_hba_inst);
18009 18008 /*
18010 18009 * Since global mutex was released, there is a
18011 18010 * possibility that HBA list has changed, so start
18012 18011 * over from the top. Just processed controller
18013 18012 * will be passed-over because of the SKIP flag.
18014 18013 */
18015 18014 goto loop;
18016 18015 }
18017 18016 mutex_exit(&sata_hba_inst->satahba_mutex);
18018 18017 }
18019 18018 /* Clear SKIP flag in all controllers */
18020 18019 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
18021 18020 sata_hba_inst = sata_hba_inst->satahba_next) {
18022 18021 mutex_enter(&sata_hba_inst->satahba_mutex);
18023 18022 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP;
18024 18023 mutex_exit(&sata_hba_inst->satahba_mutex);
18025 18024 }
18026 18025 mutex_exit(&sata_mutex);
18027 18026
18028 18027 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18029 18028 "SATA EVENT DAEMON suspending itself", NULL);
18030 18029
18031 18030 #ifdef SATA_DEBUG
18032 18031 if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) {
18033 18032 sata_log(sata_hba_inst, CE_WARN,
18034 18033 "SATA EVENTS PROCESSING DISABLED\n");
18035 18034 thread_exit(); /* Daemon will not run again */
18036 18035 }
18037 18036 #endif
18038 18037 mutex_enter(&sata_event_mutex);
18039 18038 sata_event_thread_active = 0;
18040 18039 mutex_exit(&sata_event_mutex);
18041 18040 /*
18042 18041 * Go to sleep/suspend itself and wake up either because new event or
18043 18042 * wait timeout. Exit if there is a termination request (driver
18044 18043 * unload).
18045 18044 */
18046 18045 delta = drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME);
18047 18046 do {
18048 18047 mutex_enter(&sata_event_mutex);
18049 18048 (void) cv_reltimedwait(&sata_event_cv, &sata_event_mutex,
18050 18049 delta, TR_CLOCK_TICK);
18051 18050
18052 18051 if (sata_event_thread_active != 0) {
18053 18052 mutex_exit(&sata_event_mutex);
18054 18053 continue;
18055 18054 }
18056 18055
18057 18056 /* Check if it is time to go away */
18058 18057 if (sata_event_thread_terminate == 1) {
18059 18058 /*
18060 18059 * It is up to the thread setting above flag to make
18061 18060 * sure that this thread is not killed prematurely.
18062 18061 */
18063 18062 sata_event_thread_terminate = 0;
18064 18063 sata_event_thread = NULL;
18065 18064 mutex_exit(&sata_event_mutex);
18066 18065 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18067 18066 "SATA_EVENT_DAEMON_TERMINATING", NULL);
18068 18067 thread_exit(); { _NOTE(NOT_REACHED) }
18069 18068 }
18070 18069 mutex_exit(&sata_event_mutex);
18071 18070 } while (!(sata_event_pending & SATA_EVNT_MAIN));
18072 18071
18073 18072 mutex_enter(&sata_event_mutex);
18074 18073 sata_event_thread_active = 1;
18075 18074 mutex_exit(&sata_event_mutex);
18076 18075
18077 18076 mutex_enter(&sata_mutex);
18078 18077 sata_event_pending &= ~SATA_EVNT_MAIN;
18079 18078 mutex_exit(&sata_mutex);
18080 18079
18081 18080 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18082 18081 "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL);
18083 18082
18084 18083 goto loop;
18085 18084 }
18086 18085
18087 18086 /*
18088 18087 * Specific HBA instance event processing.
18089 18088 *
18090 18089 * NOTE: At the moment, device event processing is limited to hard disks
18091 18090 * only.
18092 18091 * Port multiplier is supported now.
18093 18092 */
18094 18093 static void
18095 18094 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst)
18096 18095 {
18097 18096 int ncport;
18098 18097 uint32_t event_flags;
18099 18098 sata_address_t *saddr;
18100 18099 sata_cport_info_t *cportinfo;
18101 18100 sata_pmult_info_t *pmultinfo;
18102 18101
18103 18102 SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst,
18104 18103 "Processing controller %d event(s)",
18105 18104 ddi_get_instance(SATA_DIP(sata_hba_inst)));
18106 18105
18107 18106 mutex_enter(&sata_hba_inst->satahba_mutex);
18108 18107 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN;
18109 18108 event_flags = sata_hba_inst->satahba_event_flags;
18110 18109 mutex_exit(&sata_hba_inst->satahba_mutex);
18111 18110 /*
18112 18111 * Process controller power change first
18113 18112 * HERE
18114 18113 */
18115 18114 if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED)
18116 18115 sata_process_cntrl_pwr_level_change(sata_hba_inst);
18117 18116
18118 18117 /*
18119 18118 * Search through ports/devices to identify affected port/device.
18120 18119 * We may have to process events for more than one port/device.
18121 18120 */
18122 18121 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
18123 18122 /*
18124 18123 * Not all ports may be processed in attach by the time we
18125 18124 * get an event. Check if port info is initialized.
18126 18125 */
18127 18126 mutex_enter(&sata_hba_inst->satahba_mutex);
18128 18127 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
18129 18128 mutex_exit(&sata_hba_inst->satahba_mutex);
18130 18129 if (cportinfo == NULL || cportinfo->cport_state == NULL)
18131 18130 continue;
18132 18131
18133 18132 /* We have initialized controller port info */
18134 18133 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18135 18134 event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))->
18136 18135 cport_event_flags;
18137 18136 /* Check if port was locked by IOCTL processing */
18138 18137 if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) {
18139 18138 /*
18140 18139 * We ignore port events because port is busy
18141 18140 * with AP control processing. Set again
18142 18141 * controller and main event flag, so that
18143 18142 * events may be processed by the next daemon
18144 18143 * run.
18145 18144 */
18146 18145 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18147 18146 mutex_enter(&sata_hba_inst->satahba_mutex);
18148 18147 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
18149 18148 mutex_exit(&sata_hba_inst->satahba_mutex);
18150 18149 mutex_enter(&sata_mutex);
18151 18150 sata_event_pending |= SATA_EVNT_MAIN;
18152 18151 mutex_exit(&sata_mutex);
18153 18152 SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst,
18154 18153 "Event processing postponed until "
18155 18154 "AP control processing completes",
18156 18155 NULL);
18157 18156 /* Check other ports */
18158 18157 continue;
18159 18158 } else {
18160 18159 /*
18161 18160 * Set BSY flag so that AP control would not
18162 18161 * interfere with events processing for
18163 18162 * this port.
18164 18163 */
18165 18164 (SATA_CPORT_INFO(sata_hba_inst, ncport))->
18166 18165 cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY;
18167 18166 }
18168 18167 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18169 18168
18170 18169 saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr;
18171 18170
18172 18171 if ((event_flags &
18173 18172 (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
18174 18173 /*
18175 18174 * Got port event.
18176 18175 * We need some hierarchy of event processing as they
18177 18176 * are affecting each other:
18178 18177 * 1. port failed
18179 18178 * 2. device detached/attached
18180 18179 * 3. link events - link events may trigger device
18181 18180 * detached or device attached events in some
18182 18181 * circumstances.
18183 18182 * 4. port power level changed
18184 18183 */
18185 18184 if (event_flags & SATA_EVNT_PORT_FAILED) {
18186 18185 sata_process_port_failed_event(sata_hba_inst,
18187 18186 saddr);
18188 18187 }
18189 18188 if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
18190 18189 sata_process_device_detached(sata_hba_inst,
18191 18190 saddr);
18192 18191 }
18193 18192 if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
18194 18193 sata_process_device_attached(sata_hba_inst,
18195 18194 saddr);
18196 18195 }
18197 18196 if (event_flags &
18198 18197 (SATA_EVNT_LINK_ESTABLISHED |
18199 18198 SATA_EVNT_LINK_LOST)) {
18200 18199 sata_process_port_link_events(sata_hba_inst,
18201 18200 saddr);
18202 18201 }
18203 18202 if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) {
18204 18203 sata_process_port_pwr_change(sata_hba_inst,
18205 18204 saddr);
18206 18205 }
18207 18206 if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
18208 18207 sata_process_target_node_cleanup(
18209 18208 sata_hba_inst, saddr);
18210 18209 }
18211 18210 if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) {
18212 18211 sata_process_device_autoonline(
18213 18212 sata_hba_inst, saddr);
18214 18213 }
18215 18214 }
18216 18215
18217 18216
18218 18217 /*
18219 18218 * Scan port multiplier and all its sub-ports event flags.
18220 18219 * The events are marked by
18221 18220 * (1) sata_pmult_info.pmult_event_flags
18222 18221 * (2) sata_pmport_info.pmport_event_flags
18223 18222 */
18224 18223 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18225 18224 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
18226 18225 /*
18227 18226 * There should be another extra check: this
18228 18227 * port multiplier still exists?
18229 18228 */
18230 18229 pmultinfo = SATA_PMULT_INFO(sata_hba_inst,
18231 18230 ncport);
18232 18231
18233 18232 if (pmultinfo != NULL) {
18234 18233 mutex_exit(&(SATA_CPORT_MUTEX(
18235 18234 sata_hba_inst, ncport)));
18236 18235 sata_process_pmult_events(
18237 18236 sata_hba_inst, ncport);
18238 18237 mutex_enter(&(SATA_CPORT_MUTEX(
18239 18238 sata_hba_inst, ncport)));
18240 18239 } else {
18241 18240 SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
18242 18241 "Port-multiplier is gone. "
18243 18242 "Ignore all sub-device events "
18244 18243 "at port %d.", ncport);
18245 18244 }
18246 18245 }
18247 18246
18248 18247 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) !=
18249 18248 SATA_DTYPE_NONE) &&
18250 18249 (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) {
18251 18250 if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)->
18252 18251 satadrv_event_flags &
18253 18252 (SATA_EVNT_DEVICE_RESET |
18254 18253 SATA_EVNT_INPROC_DEVICE_RESET)) {
18255 18254 /* Have device event */
18256 18255 sata_process_device_reset(sata_hba_inst,
18257 18256 saddr);
18258 18257 }
18259 18258 }
18260 18259 /* Release PORT_BUSY flag */
18261 18260 (SATA_CPORT_INFO(sata_hba_inst, ncport))->
18262 18261 cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
18263 18262 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18264 18263
18265 18264 } /* End of loop through the controller SATA ports */
18266 18265 }
18267 18266
18268 18267 /*
18269 18268 * Specific port multiplier instance event processing. At the moment, device
18270 18269 * event processing is limited to link/attach event only.
18271 18270 *
18272 18271 * NOTE: power management event is not supported yet.
18273 18272 */
18274 18273 static void
18275 18274 sata_process_pmult_events(sata_hba_inst_t *sata_hba_inst, uint8_t cport)
18276 18275 {
18277 18276 sata_cport_info_t *cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
18278 18277 sata_pmult_info_t *pmultinfo;
18279 18278 sata_pmport_info_t *pmportinfo;
18280 18279 sata_address_t *saddr;
18281 18280 sata_device_t sata_device;
18282 18281 uint32_t event_flags;
18283 18282 int npmport;
18284 18283 int rval;
18285 18284
18286 18285 SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst,
18287 18286 "Processing pmult event(s) on cport %d of controller %d",
18288 18287 cport, ddi_get_instance(SATA_DIP(sata_hba_inst)));
18289 18288
18290 18289 /* First process events on port multiplier */
18291 18290 mutex_enter(&cportinfo->cport_mutex);
18292 18291 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
18293 18292 event_flags = pmultinfo->pmult_event_flags;
18294 18293
18295 18294 /*
18296 18295 * Reset event (of port multiplier) has higher priority because the
18297 18296 * port multiplier itself might be failed or removed after reset.
18298 18297 */
18299 18298 if (event_flags & SATA_EVNT_DEVICE_RESET) {
18300 18299 /*
18301 18300 * The status of the sub-links are uncertain,
18302 18301 * so mark all sub-ports as RESET
18303 18302 */
18304 18303 for (npmport = 0; npmport < SATA_NUM_PMPORTS(
18305 18304 sata_hba_inst, cport); npmport ++) {
18306 18305 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
18307 18306 cport, npmport);
18308 18307 if (pmportinfo == NULL) {
18309 18308 /* That's weird. */
18310 18309 SATA_LOG_D((sata_hba_inst, CE_WARN,
18311 18310 "sata_hba_event_notify: "
18312 18311 "invalid/un-implemented "
18313 18312 "port %d:%d (%d ports), ",
18314 18313 cport, npmport, SATA_NUM_PMPORTS(
18315 18314 sata_hba_inst, cport)));
18316 18315 continue;
18317 18316 }
18318 18317
18319 18318 mutex_enter(&pmportinfo->pmport_mutex);
18320 18319
18321 18320 /* Mark all pmport to unknow state. */
18322 18321 pmportinfo->pmport_state = SATA_STATE_UNKNOWN;
18323 18322 /* Mark all pmports with link events. */
18324 18323 pmportinfo->pmport_event_flags =
18325 18324 (SATA_EVNT_LINK_ESTABLISHED|SATA_EVNT_LINK_LOST);
18326 18325 mutex_exit(&pmportinfo->pmport_mutex);
18327 18326 }
18328 18327
18329 18328 } else if (event_flags & SATA_EVNT_PMULT_LINK_CHANGED) {
18330 18329 /*
18331 18330 * We need probe the port multiplier to know what has
18332 18331 * happened.
18333 18332 */
18334 18333 bzero(&sata_device, sizeof (sata_device_t));
18335 18334 sata_device.satadev_rev = SATA_DEVICE_REV;
18336 18335 sata_device.satadev_addr.cport = cport;
18337 18336 sata_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
18338 18337 sata_device.satadev_addr.qual = SATA_ADDR_PMULT;
18339 18338
18340 18339 mutex_exit(&cportinfo->cport_mutex);
18341 18340 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18342 18341 (SATA_DIP(sata_hba_inst), &sata_device);
18343 18342 mutex_enter(&cportinfo->cport_mutex);
18344 18343 if (rval != SATA_SUCCESS) {
18345 18344 /* Something went wrong? Fail the port */
18346 18345 cportinfo->cport_state = SATA_PSTATE_FAILED;
18347 18346 mutex_exit(&cportinfo->cport_mutex);
18348 18347 SATA_LOG_D((sata_hba_inst, CE_WARN,
18349 18348 "SATA port %d probing failed", cport));
18350 18349
18351 18350 /* PMult structure must be released. */
18352 18351 sata_free_pmult(sata_hba_inst, &sata_device);
18353 18352 return;
18354 18353 }
18355 18354
18356 18355 sata_update_port_info(sata_hba_inst, &sata_device);
18357 18356
18358 18357 /*
18359 18358 * Sanity check - Port is active? Is the link active?
18360 18359 * The device is still a port multiplier?
18361 18360 */
18362 18361 if ((cportinfo->cport_state &
18363 18362 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
18364 18363 ((cportinfo->cport_scr.sstatus &
18365 18364 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) ||
18366 18365 (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) {
18367 18366 mutex_exit(&cportinfo->cport_mutex);
18368 18367
18369 18368 /* PMult structure must be released. */
18370 18369 sata_free_pmult(sata_hba_inst, &sata_device);
18371 18370 return;
18372 18371 }
18373 18372
18374 18373 /* Probed succeed, set port ready. */
18375 18374 cportinfo->cport_state |=
18376 18375 SATA_STATE_PROBED | SATA_STATE_READY;
18377 18376 }
18378 18377
18379 18378 /* Release port multiplier event flags. */
18380 18379 pmultinfo->pmult_event_flags &=
18381 18380 ~(SATA_EVNT_DEVICE_RESET|SATA_EVNT_PMULT_LINK_CHANGED);
18382 18381 mutex_exit(&cportinfo->cport_mutex);
18383 18382
18384 18383 /*
18385 18384 * Check all sub-links.
18386 18385 */
18387 18386 for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst, cport);
18388 18387 npmport ++) {
18389 18388 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport);
18390 18389 mutex_enter(&pmportinfo->pmport_mutex);
18391 18390 event_flags = pmportinfo->pmport_event_flags;
18392 18391 mutex_exit(&pmportinfo->pmport_mutex);
18393 18392 saddr = &pmportinfo->pmport_addr;
18394 18393
18395 18394 if ((event_flags &
18396 18395 (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
18397 18396 /*
18398 18397 * Got port multiplier port event.
18399 18398 * We need some hierarchy of event processing as they
18400 18399 * are affecting each other:
18401 18400 * 1. device detached/attached
18402 18401 * 2. link events - link events may trigger device
18403 18402 * detached or device attached events in some
18404 18403 * circumstances.
18405 18404 */
18406 18405 if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
18407 18406 sata_process_pmdevice_detached(sata_hba_inst,
18408 18407 saddr);
18409 18408 }
18410 18409 if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
18411 18410 sata_process_pmdevice_attached(sata_hba_inst,
18412 18411 saddr);
18413 18412 }
18414 18413 if (event_flags & SATA_EVNT_LINK_ESTABLISHED ||
18415 18414 event_flags & SATA_EVNT_LINK_LOST) {
18416 18415 sata_process_pmport_link_events(sata_hba_inst,
18417 18416 saddr);
18418 18417 }
18419 18418 if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
18420 18419 sata_process_target_node_cleanup(
18421 18420 sata_hba_inst, saddr);
18422 18421 }
18423 18422 }
18424 18423
18425 18424 /* Checking drive event(s). */
18426 18425 mutex_enter(&pmportinfo->pmport_mutex);
18427 18426 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
18428 18427 pmportinfo->pmport_sata_drive != NULL) {
18429 18428 event_flags = pmportinfo->pmport_sata_drive->
18430 18429 satadrv_event_flags;
18431 18430 if (event_flags & (SATA_EVNT_DEVICE_RESET |
18432 18431 SATA_EVNT_INPROC_DEVICE_RESET)) {
18433 18432
18434 18433 /* Have device event */
18435 18434 sata_process_pmdevice_reset(sata_hba_inst,
18436 18435 saddr);
18437 18436 }
18438 18437 }
18439 18438 mutex_exit(&pmportinfo->pmport_mutex);
18440 18439
18441 18440 /* Release PORT_BUSY flag */
18442 18441 mutex_enter(&cportinfo->cport_mutex);
18443 18442 cportinfo->cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
18444 18443 mutex_exit(&cportinfo->cport_mutex);
18445 18444 }
18446 18445
18447 18446 SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst,
18448 18447 "[DONE] pmult event(s) on cport %d of controller %d",
18449 18448 cport, ddi_get_instance(SATA_DIP(sata_hba_inst)));
18450 18449 }
18451 18450
18452 18451 /*
18453 18452 * Process HBA power level change reported by HBA driver.
18454 18453 * Not implemented at this time - event is ignored.
18455 18454 */
18456 18455 static void
18457 18456 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst)
18458 18457 {
18459 18458 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18460 18459 "Processing controller power level change", NULL);
18461 18460
18462 18461 /* Ignoring it for now */
18463 18462 mutex_enter(&sata_hba_inst->satahba_mutex);
18464 18463 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
18465 18464 mutex_exit(&sata_hba_inst->satahba_mutex);
18466 18465 }
18467 18466
18468 18467 /*
18469 18468 * Process port power level change reported by HBA driver.
18470 18469 * Not implemented at this time - event is ignored.
18471 18470 */
18472 18471 static void
18473 18472 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst,
18474 18473 sata_address_t *saddr)
18475 18474 {
18476 18475 sata_cport_info_t *cportinfo;
18477 18476
18478 18477 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18479 18478 "Processing port power level change", NULL);
18480 18479
18481 18480 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18482 18481 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18483 18482 /* Reset event flag */
18484 18483 cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
18485 18484 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18486 18485 }
18487 18486
18488 18487 /*
18489 18488 * Process port failure reported by HBA driver.
18490 18489 * cports support only - no pmports.
18491 18490 */
18492 18491 static void
18493 18492 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst,
18494 18493 sata_address_t *saddr)
18495 18494 {
18496 18495 sata_cport_info_t *cportinfo;
18497 18496
18498 18497 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18499 18498 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18500 18499 /* Reset event flag first */
18501 18500 cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED;
18502 18501 /* If the port is in SHUTDOWN or FAILED state, ignore this event. */
18503 18502 if ((cportinfo->cport_state &
18504 18503 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) {
18505 18504 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18506 18505 cport_mutex);
18507 18506 return;
18508 18507 }
18509 18508 /* Fail the port */
18510 18509 cportinfo->cport_state = SATA_PSTATE_FAILED;
18511 18510 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18512 18511 sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport);
18513 18512 }
18514 18513
18515 18514 /*
18516 18515 * Device Reset Event processing.
18517 18516 * The sequence is managed by 3 stage flags:
18518 18517 * - reset event reported,
18519 18518 * - reset event being processed,
18520 18519 * - request to clear device reset state.
18521 18520 *
18522 18521 * NOTE: This function has to be entered with cport mutex held. It exits with
18523 18522 * mutex held as well, but can release mutex during the processing.
18524 18523 */
18525 18524 static void
18526 18525 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst,
18527 18526 sata_address_t *saddr)
18528 18527 {
18529 18528 sata_drive_info_t old_sdinfo; /* local copy of the drive info */
18530 18529 sata_drive_info_t *sdinfo;
18531 18530 sata_cport_info_t *cportinfo;
18532 18531 sata_device_t sata_device;
18533 18532 int rval_probe, rval_set;
18534 18533
18535 18534 /* We only care about host sata cport for now */
18536 18535 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18537 18536 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18538 18537 /*
18539 18538 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
18540 18539 * state, ignore reset event.
18541 18540 */
18542 18541 if (((cportinfo->cport_state &
18543 18542 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
18544 18543 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
18545 18544 sdinfo->satadrv_event_flags &=
18546 18545 ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
18547 18546 return;
18548 18547 }
18549 18548
18550 18549 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) ==
18551 18550 SATA_DTYPE_PMULT)) {
18552 18551 /*
18553 18552 * Should not happened: this is already handled in
18554 18553 * sata_hba_event_notify()
18555 18554 */
18556 18555 mutex_exit(&cportinfo->cport_mutex);
18557 18556 goto done;
18558 18557 }
18559 18558
18560 18559 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) &
18561 18560 SATA_VALID_DEV_TYPE) == 0) {
18562 18561 /*
18563 18562 * This should not happen - coding error.
18564 18563 * But we can recover, so do not panic, just clean up
18565 18564 * and if in debug mode, log the message.
18566 18565 */
18567 18566 #ifdef SATA_DEBUG
18568 18567 sata_log(sata_hba_inst, CE_WARN,
18569 18568 "sata_process_device_reset: "
18570 18569 "Invalid device type with sdinfo!", NULL);
18571 18570 #endif
18572 18571 sdinfo->satadrv_event_flags = 0;
18573 18572 return;
18574 18573 }
18575 18574
18576 18575 #ifdef SATA_DEBUG
18577 18576 if ((sdinfo->satadrv_event_flags &
18578 18577 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
18579 18578 /* Nothing to do */
18580 18579 /* Something is weird - why we are processing dev reset? */
18581 18580 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18582 18581 "No device reset event!!!!", NULL);
18583 18582
18584 18583 return;
18585 18584 }
18586 18585 if ((sdinfo->satadrv_event_flags &
18587 18586 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
18588 18587 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
18589 18588 /* Something is weird - new device reset event */
18590 18589 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18591 18590 "Overlapping device reset events!", NULL);
18592 18591 }
18593 18592 #endif
18594 18593 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18595 18594 "Processing port %d device reset", saddr->cport);
18596 18595
18597 18596 /* Clear event flag */
18598 18597 sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
18599 18598
18600 18599 /* It seems that we always need to check the port state first */
18601 18600 sata_device.satadev_rev = SATA_DEVICE_REV;
18602 18601 sata_device.satadev_addr = *saddr;
18603 18602 /*
18604 18603 * We have to exit mutex, because the HBA probe port function may
18605 18604 * block on its own mutex.
18606 18605 */
18607 18606 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18608 18607 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18609 18608 (SATA_DIP(sata_hba_inst), &sata_device);
18610 18609 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18611 18610 sata_update_port_info(sata_hba_inst, &sata_device);
18612 18611 if (rval_probe != SATA_SUCCESS) {
18613 18612 /* Something went wrong? Fail the port */
18614 18613 cportinfo->cport_state = SATA_PSTATE_FAILED;
18615 18614 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18616 18615 if (sdinfo != NULL)
18617 18616 sdinfo->satadrv_event_flags = 0;
18618 18617 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18619 18618 cport_mutex);
18620 18619 SATA_LOG_D((sata_hba_inst, CE_WARN,
18621 18620 "SATA port %d probing failed",
18622 18621 saddr->cport));
18623 18622 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
18624 18623 saddr->cport)->cport_mutex);
18625 18624 return;
18626 18625 }
18627 18626 if ((sata_device.satadev_scr.sstatus &
18628 18627 SATA_PORT_DEVLINK_UP_MASK) !=
18629 18628 SATA_PORT_DEVLINK_UP ||
18630 18629 sata_device.satadev_type == SATA_DTYPE_NONE) {
18631 18630 /*
18632 18631 * No device to process, anymore. Some other event processing
18633 18632 * would or have already performed port info cleanup.
18634 18633 * To be safe (HBA may need it), request clearing device
18635 18634 * reset condition.
18636 18635 */
18637 18636 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18638 18637 if (sdinfo != NULL) {
18639 18638 sdinfo->satadrv_event_flags &=
18640 18639 ~SATA_EVNT_INPROC_DEVICE_RESET;
18641 18640 sdinfo->satadrv_event_flags |=
18642 18641 SATA_EVNT_CLEAR_DEVICE_RESET;
18643 18642 }
18644 18643 return;
18645 18644 }
18646 18645
18647 18646 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18648 18647 if (sdinfo == NULL) {
18649 18648 return;
18650 18649 }
18651 18650 if ((sdinfo->satadrv_event_flags &
18652 18651 SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
18653 18652 /*
18654 18653 * Start tracking time for device feature restoration and
18655 18654 * identification. Save current time (lbolt value).
18656 18655 */
18657 18656 sdinfo->satadrv_reset_time = ddi_get_lbolt();
18658 18657 }
18659 18658 /* Mark device reset processing as active */
18660 18659 sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
18661 18660
18662 18661 old_sdinfo = *sdinfo; /* local copy of the drive info */
18663 18662 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18664 18663
18665 18664 rval_set = sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1);
18666 18665
18667 18666 if (rval_set != SATA_SUCCESS) {
18668 18667 /*
18669 18668 * Restoring drive setting failed.
18670 18669 * Probe the port first, to check if the port state has changed
18671 18670 */
18672 18671 sata_device.satadev_rev = SATA_DEVICE_REV;
18673 18672 sata_device.satadev_addr = *saddr;
18674 18673 sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
18675 18674 /* probe port */
18676 18675 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18677 18676 (SATA_DIP(sata_hba_inst), &sata_device);
18678 18677 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18679 18678 cport_mutex);
18680 18679 if (rval_probe == SATA_SUCCESS &&
18681 18680 (sata_device.satadev_state &
18682 18681 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
18683 18682 (sata_device.satadev_scr.sstatus &
18684 18683 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
18685 18684 sata_device.satadev_type != SATA_DTYPE_NONE) {
18686 18685 /*
18687 18686 * We may retry this a bit later - in-process reset
18688 18687 * condition should be already set.
18689 18688 * Track retry time for device identification.
18690 18689 */
18691 18690 if ((cportinfo->cport_dev_type &
18692 18691 SATA_VALID_DEV_TYPE) != 0 &&
18693 18692 SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL &&
18694 18693 sdinfo->satadrv_reset_time != 0) {
18695 18694 clock_t cur_time = ddi_get_lbolt();
18696 18695 /*
18697 18696 * If the retry time limit was not
18698 18697 * exceeded, retry.
18699 18698 */
18700 18699 if ((cur_time - sdinfo->satadrv_reset_time) <
18701 18700 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
18702 18701 mutex_enter(
18703 18702 &sata_hba_inst->satahba_mutex);
18704 18703 sata_hba_inst->satahba_event_flags |=
18705 18704 SATA_EVNT_MAIN;
18706 18705 mutex_exit(
18707 18706 &sata_hba_inst->satahba_mutex);
18708 18707 mutex_enter(&sata_mutex);
18709 18708 sata_event_pending |= SATA_EVNT_MAIN;
18710 18709 mutex_exit(&sata_mutex);
18711 18710 return;
18712 18711 }
18713 18712 if (rval_set == SATA_RETRY) {
18714 18713 /*
18715 18714 * Setting drive features failed, but
18716 18715 * the drive is still accessible,
18717 18716 * so emit a warning message before
18718 18717 * return.
18719 18718 */
18720 18719 mutex_exit(&SATA_CPORT_INFO(
18721 18720 sata_hba_inst,
18722 18721 saddr->cport)->cport_mutex);
18723 18722 goto done;
18724 18723 }
18725 18724 }
18726 18725 /* Fail the drive */
18727 18726 sdinfo->satadrv_state = SATA_DSTATE_FAILED;
18728 18727
18729 18728 sata_log(sata_hba_inst, CE_WARN,
18730 18729 "SATA device at port %d - device failed",
18731 18730 saddr->cport);
18732 18731
18733 18732 DTRACE_PROBE(port_failed_f);
18734 18733 }
18735 18734 /*
18736 18735 * No point of retrying - device failed or some other event
18737 18736 * processing or already did or will do port info cleanup.
18738 18737 * To be safe (HBA may need it),
18739 18738 * request clearing device reset condition.
18740 18739 */
18741 18740 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
18742 18741 sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
18743 18742 sdinfo->satadrv_reset_time = 0;
18744 18743 return;
18745 18744 }
18746 18745 done:
18747 18746 /*
18748 18747 * If setting of drive features failed, but the drive is still
18749 18748 * accessible, emit a warning message.
18750 18749 */
18751 18750 if (rval_set == SATA_RETRY) {
18752 18751 sata_log(sata_hba_inst, CE_WARN,
18753 18752 "SATA device at port %d - desired setting could not be "
18754 18753 "restored after reset. Device may not operate as expected.",
18755 18754 saddr->cport);
18756 18755 }
18757 18756 /*
18758 18757 * Raise the flag indicating that the next sata command could
18759 18758 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
18760 18759 * reset is reported.
18761 18760 */
18762 18761 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18763 18762 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
18764 18763 sdinfo->satadrv_reset_time = 0;
18765 18764 if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) {
18766 18765 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
18767 18766 sdinfo->satadrv_event_flags &=
18768 18767 ~SATA_EVNT_INPROC_DEVICE_RESET;
18769 18768 sdinfo->satadrv_event_flags |=
18770 18769 SATA_EVNT_CLEAR_DEVICE_RESET;
18771 18770 }
18772 18771 }
18773 18772 }
18774 18773
18775 18774
18776 18775 /*
18777 18776 * Port Multiplier Port Device Reset Event processing.
18778 18777 *
18779 18778 * NOTE: This function has to be entered with pmport mutex held. It exits with
18780 18779 * mutex held as well, but can release mutex during the processing.
18781 18780 */
18782 18781 static void
18783 18782 sata_process_pmdevice_reset(sata_hba_inst_t *sata_hba_inst,
18784 18783 sata_address_t *saddr)
18785 18784 {
18786 18785 sata_drive_info_t old_sdinfo; /* local copy of the drive info */
18787 18786 sata_drive_info_t *sdinfo = NULL;
18788 18787 sata_cport_info_t *cportinfo = NULL;
18789 18788 sata_pmport_info_t *pmportinfo = NULL;
18790 18789 sata_pmult_info_t *pminfo = NULL;
18791 18790 sata_device_t sata_device;
18792 18791 uint8_t cport = saddr->cport;
18793 18792 uint8_t pmport = saddr->pmport;
18794 18793 int rval;
18795 18794
18796 18795 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18797 18796 "Processing drive reset at port %d:%d", cport, pmport);
18798 18797
18799 18798 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
18800 18799 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
18801 18800 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport);
18802 18801
18803 18802 /*
18804 18803 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
18805 18804 * state, ignore reset event.
18806 18805 */
18807 18806 if (((cportinfo->cport_state &
18808 18807 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
18809 18808 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
18810 18809 sdinfo->satadrv_event_flags &=
18811 18810 ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
18812 18811 return;
18813 18812 }
18814 18813
18815 18814 if ((pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
18816 18815 /*
18817 18816 * This should not happen - coding error.
18818 18817 * But we can recover, so do not panic, just clean up
18819 18818 * and if in debug mode, log the message.
18820 18819 */
18821 18820 #ifdef SATA_DEBUG
18822 18821 sata_log(sata_hba_inst, CE_WARN,
18823 18822 "sata_process_pmdevice_reset: "
18824 18823 "Invalid device type with sdinfo!", NULL);
18825 18824 #endif
18826 18825 sdinfo->satadrv_event_flags = 0;
18827 18826 return;
18828 18827 }
18829 18828
18830 18829 #ifdef SATA_DEBUG
18831 18830 if ((sdinfo->satadrv_event_flags &
18832 18831 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
18833 18832 /* Nothing to do */
18834 18833 /* Something is weird - why we are processing dev reset? */
18835 18834 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18836 18835 "No device reset event!!!!", NULL);
18837 18836
18838 18837 return;
18839 18838 }
18840 18839 if ((sdinfo->satadrv_event_flags &
18841 18840 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
18842 18841 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
18843 18842 /* Something is weird - new device reset event */
18844 18843 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18845 18844 "Overlapping device reset events!", NULL);
18846 18845 }
18847 18846 #endif
18848 18847 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18849 18848 "Processing port %d:%d device reset", cport, pmport);
18850 18849
18851 18850 /* Clear event flag */
18852 18851 sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
18853 18852
18854 18853 /* It seems that we always need to check the port state first */
18855 18854 sata_device.satadev_rev = SATA_DEVICE_REV;
18856 18855 sata_device.satadev_addr = *saddr;
18857 18856 /*
18858 18857 * We have to exit mutex, because the HBA probe port function may
18859 18858 * block on its own mutex.
18860 18859 */
18861 18860 mutex_exit(&pmportinfo->pmport_mutex);
18862 18861 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18863 18862 (SATA_DIP(sata_hba_inst), &sata_device);
18864 18863 mutex_enter(&pmportinfo->pmport_mutex);
18865 18864
18866 18865 sata_update_pmport_info(sata_hba_inst, &sata_device);
18867 18866 if (rval != SATA_SUCCESS) {
18868 18867 /* Something went wrong? Fail the port */
18869 18868 pmportinfo->pmport_state = SATA_PSTATE_FAILED;
18870 18869 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18871 18870 saddr->pmport);
18872 18871 if (sdinfo != NULL)
18873 18872 sdinfo->satadrv_event_flags = 0;
18874 18873 mutex_exit(&pmportinfo->pmport_mutex);
18875 18874 SATA_LOG_D((sata_hba_inst, CE_WARN,
18876 18875 "SATA port %d:%d probing failed",
18877 18876 saddr->cport, saddr->pmport));
18878 18877 mutex_enter(&pmportinfo->pmport_mutex);
18879 18878 return;
18880 18879 }
18881 18880 if ((sata_device.satadev_scr.sstatus &
18882 18881 SATA_PORT_DEVLINK_UP_MASK) !=
18883 18882 SATA_PORT_DEVLINK_UP ||
18884 18883 sata_device.satadev_type == SATA_DTYPE_NONE) {
18885 18884 /*
18886 18885 * No device to process, anymore. Some other event processing
18887 18886 * would or have already performed port info cleanup.
18888 18887 * To be safe (HBA may need it), request clearing device
18889 18888 * reset condition.
18890 18889 */
18891 18890 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18892 18891 saddr->pmport);
18893 18892 if (sdinfo != NULL) {
18894 18893 sdinfo->satadrv_event_flags &=
18895 18894 ~SATA_EVNT_INPROC_DEVICE_RESET;
18896 18895 /* must clear flags on cport */
18897 18896 pminfo = SATA_PMULT_INFO(sata_hba_inst,
18898 18897 saddr->cport);
18899 18898 pminfo->pmult_event_flags |=
18900 18899 SATA_EVNT_CLEAR_DEVICE_RESET;
18901 18900 }
18902 18901 return;
18903 18902 }
18904 18903
18905 18904 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18906 18905 saddr->pmport);
18907 18906 if (sdinfo == NULL) {
18908 18907 return;
18909 18908 }
18910 18909 if ((sdinfo->satadrv_event_flags &
18911 18910 SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
18912 18911 /*
18913 18912 * Start tracking time for device feature restoration and
18914 18913 * identification. Save current time (lbolt value).
18915 18914 */
18916 18915 sdinfo->satadrv_reset_time = ddi_get_lbolt();
18917 18916 }
18918 18917 /* Mark device reset processing as active */
18919 18918 sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
18920 18919
18921 18920 old_sdinfo = *sdinfo; /* local copy of the drive info */
18922 18921 mutex_exit(&pmportinfo->pmport_mutex);
18923 18922
18924 18923 if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) ==
18925 18924 SATA_FAILURE) {
18926 18925 /*
18927 18926 * Restoring drive setting failed.
18928 18927 * Probe the port first, to check if the port state has changed
18929 18928 */
18930 18929 sata_device.satadev_rev = SATA_DEVICE_REV;
18931 18930 sata_device.satadev_addr = *saddr;
18932 18931 sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
18933 18932
18934 18933 /* probe port */
18935 18934 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18936 18935 (SATA_DIP(sata_hba_inst), &sata_device);
18937 18936 mutex_enter(&pmportinfo->pmport_mutex);
18938 18937 if (rval == SATA_SUCCESS &&
18939 18938 (sata_device.satadev_state &
18940 18939 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
18941 18940 (sata_device.satadev_scr.sstatus &
18942 18941 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
18943 18942 sata_device.satadev_type != SATA_DTYPE_NONE) {
18944 18943 /*
18945 18944 * We may retry this a bit later - in-process reset
18946 18945 * condition should be already set.
18947 18946 * Track retry time for device identification.
18948 18947 */
18949 18948 if ((pmportinfo->pmport_dev_type &
18950 18949 SATA_VALID_DEV_TYPE) != 0 &&
18951 18950 SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL &&
18952 18951 sdinfo->satadrv_reset_time != 0) {
18953 18952 clock_t cur_time = ddi_get_lbolt();
18954 18953 /*
18955 18954 * If the retry time limit was not
18956 18955 * exceeded, retry.
18957 18956 */
18958 18957 if ((cur_time - sdinfo->satadrv_reset_time) <
18959 18958 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
18960 18959 mutex_enter(
18961 18960 &sata_hba_inst->satahba_mutex);
18962 18961 sata_hba_inst->satahba_event_flags |=
18963 18962 SATA_EVNT_MAIN;
18964 18963 mutex_exit(
18965 18964 &sata_hba_inst->satahba_mutex);
18966 18965 mutex_enter(&sata_mutex);
18967 18966 sata_event_pending |= SATA_EVNT_MAIN;
18968 18967 mutex_exit(&sata_mutex);
18969 18968 return;
18970 18969 }
18971 18970 }
18972 18971 /* Fail the drive */
18973 18972 sdinfo->satadrv_state = SATA_DSTATE_FAILED;
18974 18973
18975 18974 sata_log(sata_hba_inst, CE_WARN,
18976 18975 "SATA device at port %d:%d - device failed",
18977 18976 saddr->cport, saddr->pmport);
18978 18977 } else {
18979 18978 /*
18980 18979 * No point of retrying - some other event processing
18981 18980 * would or already did port info cleanup.
18982 18981 * To be safe (HBA may need it),
18983 18982 * request clearing device reset condition.
18984 18983 */
18985 18984 sdinfo->satadrv_event_flags |=
18986 18985 SATA_EVNT_CLEAR_DEVICE_RESET;
18987 18986 }
18988 18987 sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
18989 18988 sdinfo->satadrv_reset_time = 0;
18990 18989 return;
18991 18990 }
18992 18991 /*
18993 18992 * Raise the flag indicating that the next sata command could
18994 18993 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
18995 18994 * reset is reported.
18996 18995 */
18997 18996 mutex_enter(&pmportinfo->pmport_mutex);
18998 18997 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
18999 18998 sdinfo->satadrv_reset_time = 0;
19000 18999 if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) {
19001 19000 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19002 19001 sdinfo->satadrv_event_flags &=
19003 19002 ~SATA_EVNT_INPROC_DEVICE_RESET;
19004 19003 /* must clear flags on cport */
19005 19004 pminfo = SATA_PMULT_INFO(sata_hba_inst,
19006 19005 saddr->cport);
19007 19006 pminfo->pmult_event_flags |=
19008 19007 SATA_EVNT_CLEAR_DEVICE_RESET;
19009 19008 }
19010 19009 }
19011 19010 }
19012 19011
19013 19012 /*
19014 19013 * Port Link Events processing.
19015 19014 * Every link established event may involve device reset (due to
19016 19015 * COMRESET signal, equivalent of the hard reset) so arbitrarily
19017 19016 * set device reset event for an attached device (if any).
19018 19017 * If the port is in SHUTDOWN or FAILED state, ignore link events.
19019 19018 *
19020 19019 * The link established event processing varies, depending on the state
19021 19020 * of the target node, HBA hotplugging capabilities, state of the port.
19022 19021 * If the link is not active, the link established event is ignored.
19023 19022 * If HBA cannot detect device attachment and there is no target node,
19024 19023 * the link established event triggers device attach event processing.
19025 19024 * Else, link established event triggers device reset event processing.
19026 19025 *
19027 19026 * The link lost event processing varies, depending on a HBA hotplugging
19028 19027 * capability and the state of the port (link active or not active).
19029 19028 * If the link is active, the lost link event is ignored.
19030 19029 * If HBA cannot detect device removal, the lost link event triggers
19031 19030 * device detached event processing after link lost timeout.
19032 19031 * Else, the event is ignored.
19033 19032 *
19034 19033 * NOTE: Port multiplier ports events are handled by
19035 19034 * sata_process_pmport_link_events();
19036 19035 */
19037 19036 static void
19038 19037 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst,
19039 19038 sata_address_t *saddr)
19040 19039 {
19041 19040 sata_device_t sata_device;
19042 19041 sata_cport_info_t *cportinfo;
19043 19042 sata_drive_info_t *sdinfo;
19044 19043 uint32_t event_flags;
19045 19044 int rval;
19046 19045
19047 19046 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19048 19047 "Processing port %d link event(s)", saddr->cport);
19049 19048
19050 19049 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19051 19050 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19052 19051 event_flags = cportinfo->cport_event_flags;
19053 19052
19054 19053 /* Reset event flags first */
19055 19054 cportinfo->cport_event_flags &=
19056 19055 ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
19057 19056
19058 19057 /* If the port is in SHUTDOWN or FAILED state, ignore link events. */
19059 19058 if ((cportinfo->cport_state &
19060 19059 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19061 19060 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19062 19061 cport_mutex);
19063 19062 return;
19064 19063 }
19065 19064
19066 19065 /*
19067 19066 * For the sanity sake get current port state.
19068 19067 * Set device address only. Other sata_device fields should be
19069 19068 * set by HBA driver.
19070 19069 */
19071 19070 sata_device.satadev_rev = SATA_DEVICE_REV;
19072 19071 sata_device.satadev_addr = *saddr;
19073 19072 /*
19074 19073 * We have to exit mutex, because the HBA probe port function may
19075 19074 * block on its own mutex.
19076 19075 */
19077 19076 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19078 19077 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19079 19078 (SATA_DIP(sata_hba_inst), &sata_device);
19080 19079 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19081 19080 sata_update_port_info(sata_hba_inst, &sata_device);
19082 19081 if (rval != SATA_SUCCESS) {
19083 19082 /* Something went wrong? Fail the port */
19084 19083 cportinfo->cport_state = SATA_PSTATE_FAILED;
19085 19084 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19086 19085 cport_mutex);
19087 19086 SATA_LOG_D((sata_hba_inst, CE_WARN,
19088 19087 "SATA port %d probing failed",
19089 19088 saddr->cport));
19090 19089 /*
19091 19090 * We may want to release device info structure, but
19092 19091 * it is not necessary.
19093 19092 */
19094 19093 return;
19095 19094 } else {
19096 19095 /* port probed successfully */
19097 19096 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19098 19097 }
19099 19098 if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
19100 19099
19101 19100 if ((sata_device.satadev_scr.sstatus &
19102 19101 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
19103 19102 /* Ignore event */
19104 19103 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19105 19104 "Ignoring port %d link established event - "
19106 19105 "link down",
19107 19106 saddr->cport);
19108 19107 goto linklost;
19109 19108 }
19110 19109
19111 19110 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19112 19111 "Processing port %d link established event",
19113 19112 saddr->cport);
19114 19113
19115 19114 /*
19116 19115 * For the sanity sake check if a device is attached - check
19117 19116 * return state of a port probing.
19118 19117 */
19119 19118 if (sata_device.satadev_type != SATA_DTYPE_NONE) {
19120 19119 /*
19121 19120 * HBA port probe indicated that there is a device
19122 19121 * attached. Check if the framework had device info
19123 19122 * structure attached for this device.
19124 19123 */
19125 19124 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
19126 19125 ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) !=
19127 19126 NULL);
19128 19127
19129 19128 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19130 19129 if ((sdinfo->satadrv_type &
19131 19130 SATA_VALID_DEV_TYPE) != 0) {
19132 19131 /*
19133 19132 * Dev info structure is present.
19134 19133 * If dev_type is set to known type in
19135 19134 * the framework's drive info struct
19136 19135 * then the device existed before and
19137 19136 * the link was probably lost
19138 19137 * momentarily - in such case
19139 19138 * we may want to check device
19140 19139 * identity.
19141 19140 * Identity check is not supported now.
19142 19141 *
19143 19142 * Link established event
19144 19143 * triggers device reset event.
19145 19144 */
19146 19145 (SATA_CPORTINFO_DRV_INFO(cportinfo))->
19147 19146 satadrv_event_flags |=
19148 19147 SATA_EVNT_DEVICE_RESET;
19149 19148 }
19150 19149 } else if (cportinfo->cport_dev_type ==
19151 19150 SATA_DTYPE_NONE) {
19152 19151 /*
19153 19152 * We got new device attached! If HBA does not
19154 19153 * generate device attached events, trigger it
19155 19154 * here.
19156 19155 */
19157 19156 if (!(SATA_FEATURES(sata_hba_inst) &
19158 19157 SATA_CTLF_HOTPLUG)) {
19159 19158 cportinfo->cport_event_flags |=
19160 19159 SATA_EVNT_DEVICE_ATTACHED;
19161 19160 }
19162 19161 }
19163 19162 /* Reset link lost timeout */
19164 19163 cportinfo->cport_link_lost_time = 0;
19165 19164 }
19166 19165 }
19167 19166 linklost:
19168 19167 if (event_flags & SATA_EVNT_LINK_LOST) {
19169 19168 if ((sata_device.satadev_scr.sstatus &
19170 19169 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
19171 19170 /* Ignore event */
19172 19171 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19173 19172 "Ignoring port %d link lost event - link is up",
19174 19173 saddr->cport);
19175 19174 goto done;
19176 19175 }
19177 19176 #ifdef SATA_DEBUG
19178 19177 if (cportinfo->cport_link_lost_time == 0) {
19179 19178 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19180 19179 "Processing port %d link lost event",
19181 19180 saddr->cport);
19182 19181 }
19183 19182 #endif
19184 19183 /*
19185 19184 * When HBA cannot generate device attached/detached events,
19186 19185 * we need to track link lost time and eventually generate
19187 19186 * device detach event.
19188 19187 */
19189 19188 if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
19190 19189 /* We are tracking link lost time */
19191 19190 if (cportinfo->cport_link_lost_time == 0) {
19192 19191 /* save current time (lbolt value) */
19193 19192 cportinfo->cport_link_lost_time =
19194 19193 ddi_get_lbolt();
19195 19194 /* just keep link lost event */
19196 19195 cportinfo->cport_event_flags |=
19197 19196 SATA_EVNT_LINK_LOST;
19198 19197 } else {
19199 19198 clock_t cur_time = ddi_get_lbolt();
19200 19199 if ((cur_time -
19201 19200 cportinfo->cport_link_lost_time) >=
19202 19201 drv_usectohz(
19203 19202 SATA_EVNT_LINK_LOST_TIMEOUT)) {
19204 19203 /* trigger device detach event */
19205 19204 cportinfo->cport_event_flags |=
19206 19205 SATA_EVNT_DEVICE_DETACHED;
19207 19206 cportinfo->cport_link_lost_time = 0;
19208 19207 SATADBG1(SATA_DBG_EVENTS,
19209 19208 sata_hba_inst,
19210 19209 "Triggering port %d "
19211 19210 "device detached event",
19212 19211 saddr->cport);
19213 19212 } else {
19214 19213 /* keep link lost event */
19215 19214 cportinfo->cport_event_flags |=
19216 19215 SATA_EVNT_LINK_LOST;
19217 19216 }
19218 19217 }
19219 19218 }
19220 19219 /*
19221 19220 * We could change port state to disable/delay access to
19222 19221 * the attached device until the link is recovered.
19223 19222 */
19224 19223 }
19225 19224 done:
19226 19225 event_flags = cportinfo->cport_event_flags;
19227 19226 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19228 19227 if (event_flags != 0) {
19229 19228 mutex_enter(&sata_hba_inst->satahba_mutex);
19230 19229 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19231 19230 mutex_exit(&sata_hba_inst->satahba_mutex);
19232 19231 mutex_enter(&sata_mutex);
19233 19232 sata_event_pending |= SATA_EVNT_MAIN;
19234 19233 mutex_exit(&sata_mutex);
19235 19234 }
19236 19235 }
19237 19236
19238 19237 /*
19239 19238 * Port Multiplier Port Link Events processing.
19240 19239 */
19241 19240 static void
19242 19241 sata_process_pmport_link_events(sata_hba_inst_t *sata_hba_inst,
19243 19242 sata_address_t *saddr)
19244 19243 {
19245 19244 sata_device_t sata_device;
19246 19245 sata_pmport_info_t *pmportinfo = NULL;
19247 19246 sata_drive_info_t *sdinfo = NULL;
19248 19247 uint32_t event_flags;
19249 19248 uint8_t cport = saddr->cport;
19250 19249 uint8_t pmport = saddr->pmport;
19251 19250 int rval;
19252 19251
19253 19252 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19254 19253 "Processing port %d:%d link event(s)",
19255 19254 cport, pmport);
19256 19255
19257 19256 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
19258 19257 mutex_enter(&pmportinfo->pmport_mutex);
19259 19258 event_flags = pmportinfo->pmport_event_flags;
19260 19259
19261 19260 /* Reset event flags first */
19262 19261 pmportinfo->pmport_event_flags &=
19263 19262 ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
19264 19263
19265 19264 /* If the port is in SHUTDOWN or FAILED state, ignore link events. */
19266 19265 if ((pmportinfo->pmport_state &
19267 19266 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19268 19267 mutex_exit(&pmportinfo->pmport_mutex);
19269 19268 return;
19270 19269 }
19271 19270
19272 19271 /*
19273 19272 * For the sanity sake get current port state.
19274 19273 * Set device address only. Other sata_device fields should be
19275 19274 * set by HBA driver.
19276 19275 */
19277 19276 sata_device.satadev_rev = SATA_DEVICE_REV;
19278 19277 sata_device.satadev_addr = *saddr;
19279 19278 /*
19280 19279 * We have to exit mutex, because the HBA probe port function may
19281 19280 * block on its own mutex.
19282 19281 */
19283 19282 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19284 19283 saddr->pmport));
19285 19284 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19286 19285 (SATA_DIP(sata_hba_inst), &sata_device);
19287 19286 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19288 19287 saddr->pmport));
19289 19288 sata_update_pmport_info(sata_hba_inst, &sata_device);
19290 19289 if (rval != SATA_SUCCESS) {
19291 19290 /* Something went wrong? Fail the port */
19292 19291 pmportinfo->pmport_state = SATA_PSTATE_FAILED;
19293 19292 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19294 19293 saddr->pmport));
19295 19294 SATA_LOG_D((sata_hba_inst, CE_WARN,
19296 19295 "SATA port %d:%d probing failed",
19297 19296 saddr->cport, saddr->pmport));
19298 19297 /*
19299 19298 * We may want to release device info structure, but
19300 19299 * it is not necessary.
19301 19300 */
19302 19301 return;
19303 19302 } else {
19304 19303 /* port probed successfully */
19305 19304 pmportinfo->pmport_state |=
19306 19305 SATA_STATE_PROBED | SATA_STATE_READY;
19307 19306 }
19308 19307 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst,
19309 19308 saddr->cport, saddr->pmport));
19310 19309 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst,
19311 19310 saddr->cport, saddr->pmport));
19312 19311 if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
19313 19312
19314 19313 if ((sata_device.satadev_scr.sstatus &
19315 19314 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
19316 19315 /* Ignore event */
19317 19316 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19318 19317 "Ignoring port %d:%d link established event - "
19319 19318 "link down",
19320 19319 saddr->cport, saddr->pmport);
19321 19320 goto linklost;
19322 19321 }
19323 19322
19324 19323 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19325 19324 "Processing port %d:%d link established event",
19326 19325 cport, pmport);
19327 19326
19328 19327 /*
19329 19328 * For the sanity sake check if a device is attached - check
19330 19329 * return state of a port probing.
19331 19330 */
19332 19331 if (sata_device.satadev_type != SATA_DTYPE_NONE &&
19333 19332 sata_device.satadev_type != SATA_DTYPE_PMULT) {
19334 19333 /*
19335 19334 * HBA port probe indicated that there is a device
19336 19335 * attached. Check if the framework had device info
19337 19336 * structure attached for this device.
19338 19337 */
19339 19338 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
19340 19339 ASSERT(SATA_PMPORTINFO_DRV_INFO(pmportinfo) !=
19341 19340 NULL);
19342 19341
19343 19342 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19344 19343 if ((sdinfo->satadrv_type &
19345 19344 SATA_VALID_DEV_TYPE) != 0) {
19346 19345 /*
19347 19346 * Dev info structure is present.
19348 19347 * If dev_type is set to known type in
19349 19348 * the framework's drive info struct
19350 19349 * then the device existed before and
19351 19350 * the link was probably lost
19352 19351 * momentarily - in such case
19353 19352 * we may want to check device
19354 19353 * identity.
19355 19354 * Identity check is not supported now.
19356 19355 *
19357 19356 * Link established event
19358 19357 * triggers device reset event.
19359 19358 */
19360 19359 (SATA_PMPORTINFO_DRV_INFO(pmportinfo))->
19361 19360 satadrv_event_flags |=
19362 19361 SATA_EVNT_DEVICE_RESET;
19363 19362 }
19364 19363 } else if (pmportinfo->pmport_dev_type ==
19365 19364 SATA_DTYPE_NONE) {
19366 19365 /*
19367 19366 * We got new device attached! If HBA does not
19368 19367 * generate device attached events, trigger it
19369 19368 * here.
19370 19369 */
19371 19370 if (!(SATA_FEATURES(sata_hba_inst) &
19372 19371 SATA_CTLF_HOTPLUG)) {
19373 19372 pmportinfo->pmport_event_flags |=
19374 19373 SATA_EVNT_DEVICE_ATTACHED;
19375 19374 }
19376 19375 }
19377 19376 /* Reset link lost timeout */
19378 19377 pmportinfo->pmport_link_lost_time = 0;
19379 19378 }
19380 19379 }
19381 19380 linklost:
19382 19381 if (event_flags & SATA_EVNT_LINK_LOST) {
19383 19382 #ifdef SATA_DEBUG
19384 19383 if (pmportinfo->pmport_link_lost_time == 0) {
19385 19384 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19386 19385 "Processing port %d:%d link lost event",
19387 19386 saddr->cport, saddr->pmport);
19388 19387 }
19389 19388 #endif
19390 19389 if ((sata_device.satadev_scr.sstatus &
19391 19390 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
19392 19391 /* Ignore event */
19393 19392 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19394 19393 "Ignoring port %d:%d link lost event - link is up",
19395 19394 saddr->cport, saddr->pmport);
19396 19395 goto done;
19397 19396 }
19398 19397 /*
19399 19398 * When HBA cannot generate device attached/detached events,
19400 19399 * we need to track link lost time and eventually generate
19401 19400 * device detach event.
19402 19401 */
19403 19402 if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
19404 19403 /* We are tracking link lost time */
19405 19404 if (pmportinfo->pmport_link_lost_time == 0) {
19406 19405 /* save current time (lbolt value) */
19407 19406 pmportinfo->pmport_link_lost_time =
19408 19407 ddi_get_lbolt();
19409 19408 /* just keep link lost event */
19410 19409 pmportinfo->pmport_event_flags |=
19411 19410 SATA_EVNT_LINK_LOST;
19412 19411 } else {
19413 19412 clock_t cur_time = ddi_get_lbolt();
19414 19413 if ((cur_time -
19415 19414 pmportinfo->pmport_link_lost_time) >=
19416 19415 drv_usectohz(
19417 19416 SATA_EVNT_LINK_LOST_TIMEOUT)) {
19418 19417 /* trigger device detach event */
19419 19418 pmportinfo->pmport_event_flags |=
19420 19419 SATA_EVNT_DEVICE_DETACHED;
19421 19420 pmportinfo->pmport_link_lost_time = 0;
19422 19421 SATADBG2(SATA_DBG_EVENTS,
19423 19422 sata_hba_inst,
19424 19423 "Triggering port %d:%d "
19425 19424 "device detached event",
19426 19425 saddr->cport, saddr->pmport);
19427 19426 } else {
19428 19427 /* keep link lost event */
19429 19428 pmportinfo->pmport_event_flags |=
19430 19429 SATA_EVNT_LINK_LOST;
19431 19430 }
19432 19431 }
19433 19432 }
19434 19433 /*
19435 19434 * We could change port state to disable/delay access to
19436 19435 * the attached device until the link is recovered.
19437 19436 */
19438 19437 }
19439 19438 done:
19440 19439 event_flags = pmportinfo->pmport_event_flags;
19441 19440 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19442 19441 saddr->pmport));
19443 19442 if (event_flags != 0) {
19444 19443 mutex_enter(&sata_hba_inst->satahba_mutex);
19445 19444 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19446 19445 mutex_exit(&sata_hba_inst->satahba_mutex);
19447 19446 mutex_enter(&sata_mutex);
19448 19447 sata_event_pending |= SATA_EVNT_MAIN;
19449 19448 mutex_exit(&sata_mutex);
19450 19449 }
19451 19450 }
19452 19451
19453 19452 /*
19454 19453 * Device Detached Event processing.
19455 19454 * Port is probed to find if a device is really gone. If so,
19456 19455 * the device info structure is detached from the SATA port info structure
19457 19456 * and released.
19458 19457 * Port status is updated.
19459 19458 *
19460 19459 * NOTE: Port multiplier ports events are handled by
19461 19460 * sata_process_pmdevice_detached()
19462 19461 */
19463 19462 static void
19464 19463 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst,
19465 19464 sata_address_t *saddr)
19466 19465 {
19467 19466 sata_cport_info_t *cportinfo;
19468 19467 sata_pmport_info_t *pmportinfo;
19469 19468 sata_drive_info_t *sdevinfo;
19470 19469 sata_device_t sata_device;
19471 19470 sata_address_t pmport_addr;
19472 19471 char name[16];
19473 19472 uint8_t cport = saddr->cport;
19474 19473 int npmport;
19475 19474 int rval;
19476 19475
19477 19476 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19478 19477 "Processing port %d device detached", saddr->cport);
19479 19478
19480 19479 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19481 19480 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19482 19481 /* Clear event flag */
19483 19482 cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
19484 19483
19485 19484 /* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
19486 19485 if ((cportinfo->cport_state &
19487 19486 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19488 19487 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19489 19488 cport_mutex);
19490 19489 return;
19491 19490 }
19492 19491 /* For sanity, re-probe the port */
19493 19492 sata_device.satadev_rev = SATA_DEVICE_REV;
19494 19493 sata_device.satadev_addr = *saddr;
19495 19494
19496 19495 /*
19497 19496 * We have to exit mutex, because the HBA probe port function may
19498 19497 * block on its own mutex.
19499 19498 */
19500 19499 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19501 19500 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19502 19501 (SATA_DIP(sata_hba_inst), &sata_device);
19503 19502 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19504 19503 sata_update_port_info(sata_hba_inst, &sata_device);
19505 19504 if (rval != SATA_SUCCESS) {
19506 19505 /* Something went wrong? Fail the port */
19507 19506 cportinfo->cport_state = SATA_PSTATE_FAILED;
19508 19507 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19509 19508 cport_mutex);
19510 19509 SATA_LOG_D((sata_hba_inst, CE_WARN,
19511 19510 "SATA port %d probing failed",
19512 19511 saddr->cport));
19513 19512 /*
19514 19513 * We may want to release device info structure, but
19515 19514 * it is not necessary.
19516 19515 */
19517 19516 return;
19518 19517 } else {
19519 19518 /* port probed successfully */
19520 19519 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19521 19520 }
19522 19521 /*
19523 19522 * Check if a device is still attached. For sanity, check also
19524 19523 * link status - if no link, there is no device.
19525 19524 */
19526 19525 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
19527 19526 SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
19528 19527 SATA_DTYPE_NONE) {
19529 19528 /*
19530 19529 * Device is still attached - ignore detach event.
19531 19530 */
19532 19531 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19533 19532 cport_mutex);
19534 19533 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19535 19534 "Ignoring detach - device still attached to port %d",
19536 19535 sata_device.satadev_addr.cport);
19537 19536 return;
19538 19537 }
19539 19538 /*
19540 19539 * We need to detach and release device info structure here
19541 19540 */
19542 19541 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
19543 19542 /*
19544 19543 * A port-multiplier is removed.
19545 19544 *
19546 19545 * Calling sata_process_pmdevice_detached() does not work
19547 19546 * here. The port multiplier is gone, so we cannot probe
19548 19547 * sub-port any more and all pmult-related data structure must
19549 19548 * be de-allocated immediately. Following structure of every
19550 19549 * implemented sub-port behind the pmult are required to
19551 19550 * released.
19552 19551 *
19553 19552 * - attachment point
19554 19553 * - target node
19555 19554 * - sata_drive_info
19556 19555 * - sata_pmport_info
19557 19556 */
19558 19557 for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst,
19559 19558 cport); npmport ++) {
19560 19559 SATADBG2(SATA_DBG_PMULT|SATA_DBG_EVENTS_PROC,
19561 19560 sata_hba_inst,
19562 19561 "Detaching target node at port %d:%d",
19563 19562 cport, npmport);
19564 19563
19565 19564 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
19566 19565
19567 19566 /* Remove attachment point. */
19568 19567 name[0] = '\0';
19569 19568 (void) sprintf(name, "%d.%d", cport, npmport);
19570 19569 ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name);
19571 19570 sata_log(sata_hba_inst, CE_NOTE,
19572 19571 "Remove attachment point of port %d:%d",
19573 19572 cport, npmport);
19574 19573
19575 19574 /* Remove target node */
19576 19575 pmport_addr.cport = cport;
19577 19576 pmport_addr.pmport = (uint8_t)npmport;
19578 19577 pmport_addr.qual = SATA_ADDR_PMPORT;
19579 19578 sata_remove_target_node(sata_hba_inst, &pmport_addr);
19580 19579
19581 19580 mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
19582 19581
19583 19582 /* Release sata_pmport_info & sata_drive_info. */
19584 19583 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
19585 19584 cport, npmport);
19586 19585 ASSERT(pmportinfo != NULL);
19587 19586
19588 19587 sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19589 19588 if (sdevinfo != NULL) {
19590 19589 (void) kmem_free((void *) sdevinfo,
19591 19590 sizeof (sata_drive_info_t));
19592 19591 }
19593 19592
19594 19593 /* Release sata_pmport_info at last */
19595 19594 (void) kmem_free((void *) pmportinfo,
19596 19595 sizeof (sata_pmport_info_t));
19597 19596 }
19598 19597
19599 19598 /* Finally, release sata_pmult_info */
19600 19599 (void) kmem_free((void *)
19601 19600 SATA_CPORTINFO_PMULT_INFO(cportinfo),
19602 19601 sizeof (sata_pmult_info_t));
19603 19602 SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL;
19604 19603
19605 19604 sata_log(sata_hba_inst, CE_WARN,
19606 19605 "SATA port-multiplier detached at port %d", cport);
19607 19606
19608 19607 cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19609 19608 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19610 19609 saddr->cport)->cport_mutex);
19611 19610 } else {
19612 19611 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19613 19612 sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19614 19613 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
19615 19614 (void) kmem_free((void *)sdevinfo,
19616 19615 sizeof (sata_drive_info_t));
19617 19616 }
19618 19617 sata_log(sata_hba_inst, CE_WARN,
19619 19618 "SATA device detached at port %d", cport);
19620 19619
19621 19620 cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19622 19621 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19623 19622 saddr->cport)->cport_mutex);
19624 19623
19625 19624 /*
19626 19625 * Try to offline a device and remove target node
19627 19626 * if it still exists
19628 19627 */
19629 19628 sata_remove_target_node(sata_hba_inst, saddr);
19630 19629 }
19631 19630
19632 19631
19633 19632 /*
19634 19633 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19635 19634 * with the hint: SE_HINT_REMOVE
19636 19635 */
19637 19636 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
19638 19637 }
19639 19638
19640 19639 /*
19641 19640 * Port Multiplier Port Device Deattached Event processing.
19642 19641 *
19643 19642 * NOTE: No Mutex should be hold.
19644 19643 */
19645 19644 static void
19646 19645 sata_process_pmdevice_detached(sata_hba_inst_t *sata_hba_inst,
19647 19646 sata_address_t *saddr)
19648 19647 {
19649 19648 sata_pmport_info_t *pmportinfo;
19650 19649 sata_drive_info_t *sdevinfo;
19651 19650 sata_device_t sata_device;
19652 19651 int rval;
19653 19652 uint8_t cport, pmport;
19654 19653
19655 19654 cport = saddr->cport;
19656 19655 pmport = saddr->pmport;
19657 19656
19658 19657 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19659 19658 "Processing port %d:%d device detached",
19660 19659 cport, pmport);
19661 19660
19662 19661 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
19663 19662 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19664 19663
19665 19664 /* Clear event flag */
19666 19665 pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
19667 19666
19668 19667 /* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
19669 19668 if ((pmportinfo->pmport_state &
19670 19669 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19671 19670 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19672 19671 return;
19673 19672 }
19674 19673 /* For sanity, re-probe the port */
19675 19674 sata_device.satadev_rev = SATA_DEVICE_REV;
19676 19675 sata_device.satadev_addr = *saddr;
19677 19676
19678 19677 /*
19679 19678 * We have to exit mutex, because the HBA probe port function may
19680 19679 * block on its own mutex.
19681 19680 */
19682 19681 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19683 19682 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19684 19683 (SATA_DIP(sata_hba_inst), &sata_device);
19685 19684 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19686 19685 sata_update_pmport_info(sata_hba_inst, &sata_device);
19687 19686 if (rval != SATA_SUCCESS) {
19688 19687 /* Something went wrong? Fail the port */
19689 19688 pmportinfo->pmport_state = SATA_PSTATE_FAILED;
19690 19689 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19691 19690 SATA_LOG_D((sata_hba_inst, CE_WARN,
19692 19691 "SATA port %d:%d probing failed",
19693 19692 saddr->pmport));
19694 19693 /*
19695 19694 * We may want to release device info structure, but
19696 19695 * it is not necessary.
19697 19696 */
19698 19697 return;
19699 19698 } else {
19700 19699 /* port probed successfully */
19701 19700 pmportinfo->pmport_state |=
19702 19701 SATA_STATE_PROBED | SATA_STATE_READY;
19703 19702 }
19704 19703 /*
19705 19704 * Check if a device is still attached. For sanity, check also
19706 19705 * link status - if no link, there is no device.
19707 19706 */
19708 19707 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
19709 19708 SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
19710 19709 SATA_DTYPE_NONE) {
19711 19710 /*
19712 19711 * Device is still attached - ignore detach event.
19713 19712 */
19714 19713 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19715 19714 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19716 19715 "Ignoring detach - device still attached to port %d",
19717 19716 sata_device.satadev_addr.pmport);
19718 19717 return;
19719 19718 }
19720 19719 /*
19721 19720 * We need to detach and release device info structure here
19722 19721 */
19723 19722 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
19724 19723 sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19725 19724 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
19726 19725 (void) kmem_free((void *)sdevinfo,
19727 19726 sizeof (sata_drive_info_t));
19728 19727 }
19729 19728 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
19730 19729 /*
19731 19730 * Device cannot be reached anymore, even if the target node may be
19732 19731 * still present.
19733 19732 */
19734 19733 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19735 19734
19736 19735 /*
19737 19736 * Try to offline a device and remove target node if it still exists
19738 19737 */
19739 19738 sata_remove_target_node(sata_hba_inst, saddr);
19740 19739
19741 19740 /*
19742 19741 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19743 19742 * with the hint: SE_HINT_REMOVE
19744 19743 */
19745 19744 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
19746 19745 }
19747 19746
19748 19747
19749 19748 /*
19750 19749 * Device Attached Event processing.
19751 19750 * Port state is checked to verify that a device is really attached. If so,
19752 19751 * the device info structure is created and attached to the SATA port info
19753 19752 * structure.
19754 19753 *
19755 19754 * If attached device cannot be identified or set-up, the retry for the
19756 19755 * attach processing is set-up. Subsequent daemon run would try again to
19757 19756 * identify the device, until the time limit is reached
19758 19757 * (SATA_DEV_IDENTIFY_TIMEOUT).
19759 19758 *
19760 19759 * This function cannot be called in interrupt context (it may sleep).
19761 19760 *
19762 19761 * NOTE: Port multiplier ports events are handled by
19763 19762 * sata_process_pmdevice_attached()
19764 19763 */
19765 19764 static void
19766 19765 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst,
19767 19766 sata_address_t *saddr)
19768 19767 {
19769 19768 sata_cport_info_t *cportinfo = NULL;
19770 19769 sata_drive_info_t *sdevinfo = NULL;
19771 19770 sata_pmult_info_t *pmultinfo = NULL;
19772 19771 sata_pmport_info_t *pmportinfo = NULL;
19773 19772 sata_device_t sata_device;
19774 19773 dev_info_t *tdip;
19775 19774 uint32_t event_flags = 0, pmult_event_flags = 0;
19776 19775 int rval;
19777 19776 int npmport;
19778 19777
19779 19778 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19780 19779 "Processing port %d device attached", saddr->cport);
19781 19780
19782 19781 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19783 19782 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19784 19783
19785 19784 /* Clear attach event flag first */
19786 19785 cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
19787 19786
19788 19787 /* If the port is in SHUTDOWN or FAILED state, ignore event. */
19789 19788 if ((cportinfo->cport_state &
19790 19789 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19791 19790 cportinfo->cport_dev_attach_time = 0;
19792 19791 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19793 19792 cport_mutex);
19794 19793 return;
19795 19794 }
19796 19795
19797 19796 /*
19798 19797 * If the sata_drive_info structure is found attached to the port info,
19799 19798 * despite the fact the device was removed and now it is re-attached,
19800 19799 * the old drive info structure was not removed.
19801 19800 * Arbitrarily release device info structure.
19802 19801 */
19803 19802 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19804 19803 sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19805 19804 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
19806 19805 (void) kmem_free((void *)sdevinfo,
19807 19806 sizeof (sata_drive_info_t));
19808 19807 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19809 19808 "Arbitrarily detaching old device info.", NULL);
19810 19809 }
19811 19810 cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19812 19811
19813 19812 /* For sanity, re-probe the port */
19814 19813 sata_device.satadev_rev = SATA_DEVICE_REV;
19815 19814 sata_device.satadev_addr = *saddr;
19816 19815
19817 19816 /*
19818 19817 * We have to exit mutex, because the HBA probe port function may
19819 19818 * block on its own mutex.
19820 19819 */
19821 19820 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19822 19821 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19823 19822 (SATA_DIP(sata_hba_inst), &sata_device);
19824 19823 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19825 19824 sata_update_port_info(sata_hba_inst, &sata_device);
19826 19825 if (rval != SATA_SUCCESS) {
19827 19826 /* Something went wrong? Fail the port */
19828 19827 cportinfo->cport_state = SATA_PSTATE_FAILED;
19829 19828 cportinfo->cport_dev_attach_time = 0;
19830 19829 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19831 19830 cport_mutex);
19832 19831 SATA_LOG_D((sata_hba_inst, CE_WARN,
19833 19832 "SATA port %d probing failed",
19834 19833 saddr->cport));
19835 19834 return;
19836 19835 } else {
19837 19836 /* port probed successfully */
19838 19837 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19839 19838 }
19840 19839 /*
19841 19840 * Check if a device is still attached. For sanity, check also
19842 19841 * link status - if no link, there is no device.
19843 19842 */
19844 19843 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
19845 19844 SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
19846 19845 SATA_DTYPE_NONE) {
19847 19846 /*
19848 19847 * No device - ignore attach event.
19849 19848 */
19850 19849 cportinfo->cport_dev_attach_time = 0;
19851 19850 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19852 19851 cport_mutex);
19853 19852 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19854 19853 "Ignoring attach - no device connected to port %d",
19855 19854 sata_device.satadev_addr.cport);
19856 19855 return;
19857 19856 }
19858 19857
19859 19858 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19860 19859 /*
19861 19860 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19862 19861 * with the hint: SE_HINT_INSERT
19863 19862 */
19864 19863 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
19865 19864
19866 19865 /*
19867 19866 * Port reprobing will take care of the creation of the device
19868 19867 * info structure and determination of the device type.
19869 19868 */
19870 19869 sata_device.satadev_addr = *saddr;
19871 19870 (void) sata_reprobe_port(sata_hba_inst, &sata_device,
19872 19871 SATA_DEV_IDENTIFY_NORETRY);
19873 19872
19874 19873 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19875 19874 cport_mutex);
19876 19875 if ((cportinfo->cport_state & SATA_STATE_READY) &&
19877 19876 (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) {
19878 19877 /* Some device is attached to the port */
19879 19878 if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) {
19880 19879 /*
19881 19880 * A device was not successfully attached.
19882 19881 * Track retry time for device identification.
19883 19882 */
19884 19883 if (cportinfo->cport_dev_attach_time != 0) {
19885 19884 clock_t cur_time = ddi_get_lbolt();
19886 19885 /*
19887 19886 * If the retry time limit was not exceeded,
19888 19887 * reinstate attach event.
19889 19888 */
19890 19889 if ((cur_time -
19891 19890 cportinfo->cport_dev_attach_time) <
19892 19891 drv_usectohz(
19893 19892 SATA_DEV_IDENTIFY_TIMEOUT)) {
19894 19893 /* OK, restore attach event */
19895 19894 cportinfo->cport_event_flags |=
19896 19895 SATA_EVNT_DEVICE_ATTACHED;
19897 19896 } else {
19898 19897 /* Timeout - cannot identify device */
19899 19898 cportinfo->cport_dev_attach_time = 0;
19900 19899 sata_log(sata_hba_inst,
19901 19900 CE_WARN,
19902 19901 "Could not identify SATA device "
19903 19902 "at port %d",
19904 19903 saddr->cport);
19905 19904 }
19906 19905 } else {
19907 19906 /*
19908 19907 * Start tracking time for device
19909 19908 * identification.
19910 19909 * Save current time (lbolt value).
19911 19910 */
19912 19911 cportinfo->cport_dev_attach_time =
19913 19912 ddi_get_lbolt();
19914 19913 /* Restore attach event */
19915 19914 cportinfo->cport_event_flags |=
19916 19915 SATA_EVNT_DEVICE_ATTACHED;
19917 19916 }
19918 19917 } else if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
19919 19918 cportinfo->cport_dev_attach_time = 0;
19920 19919 sata_log(sata_hba_inst, CE_NOTE,
19921 19920 "SATA port-multiplier detected at port %d",
19922 19921 saddr->cport);
19923 19922
19924 19923 if (SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL) {
19925 19924 /* Log the info of new port multiplier */
19926 19925 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19927 19926 saddr->cport)->cport_mutex);
19928 19927 sata_show_pmult_info(sata_hba_inst,
19929 19928 &sata_device);
19930 19929 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19931 19930 saddr->cport)->cport_mutex);
19932 19931 }
19933 19932
19934 19933 ASSERT(SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL);
19935 19934 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
19936 19935 for (npmport = 0; npmport <
19937 19936 pmultinfo->pmult_num_dev_ports; npmport++) {
19938 19937 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
19939 19938 saddr->cport, npmport);
19940 19939 ASSERT(pmportinfo != NULL);
19941 19940
19942 19941 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19943 19942 saddr->cport)->cport_mutex);
19944 19943 mutex_enter(&pmportinfo->pmport_mutex);
19945 19944 /* Marked all pmports with link events. */
19946 19945 pmportinfo->pmport_event_flags =
19947 19946 SATA_EVNT_LINK_ESTABLISHED;
19948 19947 pmult_event_flags |=
19949 19948 pmportinfo->pmport_event_flags;
19950 19949 mutex_exit(&pmportinfo->pmport_mutex);
19951 19950 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19952 19951 saddr->cport)->cport_mutex);
19953 19952 }
19954 19953 /* Auto-online is not available for PMult now. */
19955 19954
19956 19955 } else {
19957 19956 /*
19958 19957 * If device was successfully attached, the subsequent
19959 19958 * action depends on a state of the
19960 19959 * sata_auto_online variable. If it is set to zero.
19961 19960 * an explicit 'configure' command will be needed to
19962 19961 * configure it. If its value is non-zero, we will
19963 19962 * attempt to online (configure) the device.
19964 19963 * First, log the message indicating that a device
19965 19964 * was attached.
19966 19965 */
19967 19966 cportinfo->cport_dev_attach_time = 0;
19968 19967 sata_log(sata_hba_inst, CE_WARN,
19969 19968 "SATA device detected at port %d", saddr->cport);
19970 19969
19971 19970 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19972 19971 sata_drive_info_t new_sdinfo;
19973 19972
19974 19973 /* Log device info data */
19975 19974 new_sdinfo = *(SATA_CPORTINFO_DRV_INFO(
19976 19975 cportinfo));
19977 19976 sata_show_drive_info(sata_hba_inst,
19978 19977 &new_sdinfo);
19979 19978 }
19980 19979
19981 19980 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19982 19981 saddr->cport)->cport_mutex);
19983 19982
19984 19983 /*
19985 19984 * Make sure that there is no target node for that
19986 19985 * device. If so, release it. It should not happen,
19987 19986 * unless we had problem removing the node when
19988 19987 * device was detached.
19989 19988 */
19990 19989 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
19991 19990 saddr->cport, saddr->pmport);
19992 19991 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19993 19992 saddr->cport)->cport_mutex);
19994 19993 if (tdip != NULL) {
19995 19994
19996 19995 #ifdef SATA_DEBUG
19997 19996 if ((cportinfo->cport_event_flags &
19998 19997 SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
19999 19998 sata_log(sata_hba_inst, CE_WARN,
20000 19999 "sata_process_device_attached: "
20001 20000 "old device target node exists!");
20002 20001 #endif
20003 20002 /*
20004 20003 * target node exists - try to unconfigure
20005 20004 * device and remove the node.
20006 20005 */
20007 20006 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20008 20007 saddr->cport)->cport_mutex);
20009 20008 rval = ndi_devi_offline(tdip,
20010 20009 NDI_DEVI_REMOVE);
20011 20010 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20012 20011 saddr->cport)->cport_mutex);
20013 20012
20014 20013 if (rval == NDI_SUCCESS) {
20015 20014 cportinfo->cport_event_flags &=
20016 20015 ~SATA_EVNT_TARGET_NODE_CLEANUP;
20017 20016 cportinfo->cport_tgtnode_clean = B_TRUE;
20018 20017 } else {
20019 20018 /*
20020 20019 * PROBLEM - the target node remained
20021 20020 * and it belongs to a previously
20022 20021 * attached device.
20023 20022 * This happens when the file was open
20024 20023 * or the node was waiting for
20025 20024 * resources at the time the
20026 20025 * associated device was removed.
20027 20026 * Instruct event daemon to retry the
20028 20027 * cleanup later.
20029 20028 */
20030 20029 sata_log(sata_hba_inst,
20031 20030 CE_WARN,
20032 20031 "Application(s) accessing "
20033 20032 "previously attached SATA "
20034 20033 "device have to release "
20035 20034 "it before newly inserted "
20036 20035 "device can be made accessible.",
20037 20036 saddr->cport);
20038 20037 cportinfo->cport_event_flags |=
20039 20038 SATA_EVNT_TARGET_NODE_CLEANUP;
20040 20039 cportinfo->cport_tgtnode_clean =
20041 20040 B_FALSE;
20042 20041 }
20043 20042 }
20044 20043 if (sata_auto_online != 0) {
20045 20044 cportinfo->cport_event_flags |=
20046 20045 SATA_EVNT_AUTOONLINE_DEVICE;
20047 20046 }
20048 20047
20049 20048 }
20050 20049 } else {
20051 20050 cportinfo->cport_dev_attach_time = 0;
20052 20051 }
20053 20052
20054 20053 event_flags = cportinfo->cport_event_flags;
20055 20054 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20056 20055 if (event_flags != 0 || pmult_event_flags != 0) {
20057 20056 mutex_enter(&sata_hba_inst->satahba_mutex);
20058 20057 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20059 20058 mutex_exit(&sata_hba_inst->satahba_mutex);
20060 20059 mutex_enter(&sata_mutex);
20061 20060 sata_event_pending |= SATA_EVNT_MAIN;
20062 20061 mutex_exit(&sata_mutex);
20063 20062 }
20064 20063 }
20065 20064
20066 20065 /*
20067 20066 * Port Multiplier Port Device Attached Event processing.
20068 20067 *
20069 20068 * NOTE: No Mutex should be hold.
20070 20069 */
20071 20070 static void
20072 20071 sata_process_pmdevice_attached(sata_hba_inst_t *sata_hba_inst,
20073 20072 sata_address_t *saddr)
20074 20073 {
20075 20074 sata_pmport_info_t *pmportinfo;
20076 20075 sata_drive_info_t *sdinfo;
20077 20076 sata_device_t sata_device;
20078 20077 dev_info_t *tdip;
20079 20078 uint32_t event_flags;
20080 20079 uint8_t cport = saddr->cport;
20081 20080 uint8_t pmport = saddr->pmport;
20082 20081 int rval;
20083 20082
20084 20083 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20085 20084 "Processing port %d:%d device attached", cport, pmport);
20086 20085
20087 20086 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
20088 20087
20089 20088 mutex_enter(&pmportinfo->pmport_mutex);
20090 20089
20091 20090 /* Clear attach event flag first */
20092 20091 pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
20093 20092
20094 20093 /* If the port is in SHUTDOWN or FAILED state, ignore event. */
20095 20094 if ((pmportinfo->pmport_state &
20096 20095 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
20097 20096 pmportinfo->pmport_dev_attach_time = 0;
20098 20097 mutex_exit(&pmportinfo->pmport_mutex);
20099 20098 return;
20100 20099 }
20101 20100
20102 20101 /*
20103 20102 * If the sata_drive_info structure is found attached to the port info,
20104 20103 * despite the fact the device was removed and now it is re-attached,
20105 20104 * the old drive info structure was not removed.
20106 20105 * Arbitrarily release device info structure.
20107 20106 */
20108 20107 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20109 20108 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
20110 20109 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
20111 20110 (void) kmem_free((void *)sdinfo,
20112 20111 sizeof (sata_drive_info_t));
20113 20112 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20114 20113 "Arbitrarily detaching old device info.", NULL);
20115 20114 }
20116 20115 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
20117 20116
20118 20117 /* For sanity, re-probe the port */
20119 20118 sata_device.satadev_rev = SATA_DEVICE_REV;
20120 20119 sata_device.satadev_addr = *saddr;
20121 20120
20122 20121 /*
20123 20122 * We have to exit mutex, because the HBA probe port function may
20124 20123 * block on its own mutex.
20125 20124 */
20126 20125 mutex_exit(&pmportinfo->pmport_mutex);
20127 20126 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
20128 20127 (SATA_DIP(sata_hba_inst), &sata_device);
20129 20128 mutex_enter(&pmportinfo->pmport_mutex);
20130 20129
20131 20130 sata_update_pmport_info(sata_hba_inst, &sata_device);
20132 20131 if (rval != SATA_SUCCESS) {
20133 20132 /* Something went wrong? Fail the port */
20134 20133 pmportinfo->pmport_state = SATA_PSTATE_FAILED;
20135 20134 pmportinfo->pmport_dev_attach_time = 0;
20136 20135 mutex_exit(&pmportinfo->pmport_mutex);
20137 20136 SATA_LOG_D((sata_hba_inst, CE_WARN,
20138 20137 "SATA port %d:%d probing failed", cport, pmport));
20139 20138 return;
20140 20139 } else {
20141 20140 /* pmport probed successfully */
20142 20141 pmportinfo->pmport_state |=
20143 20142 SATA_STATE_PROBED | SATA_STATE_READY;
20144 20143 }
20145 20144 /*
20146 20145 * Check if a device is still attached. For sanity, check also
20147 20146 * link status - if no link, there is no device.
20148 20147 */
20149 20148 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
20150 20149 SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
20151 20150 SATA_DTYPE_NONE) {
20152 20151 /*
20153 20152 * No device - ignore attach event.
20154 20153 */
20155 20154 pmportinfo->pmport_dev_attach_time = 0;
20156 20155 mutex_exit(&pmportinfo->pmport_mutex);
20157 20156 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20158 20157 "Ignoring attach - no device connected to port %d:%d",
20159 20158 cport, pmport);
20160 20159 return;
20161 20160 }
20162 20161
20163 20162 mutex_exit(&pmportinfo->pmport_mutex);
20164 20163 /*
20165 20164 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
20166 20165 * with the hint: SE_HINT_INSERT
20167 20166 */
20168 20167 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
20169 20168
20170 20169 /*
20171 20170 * Port reprobing will take care of the creation of the device
20172 20171 * info structure and determination of the device type.
20173 20172 */
20174 20173 sata_device.satadev_addr = *saddr;
20175 20174 (void) sata_reprobe_port(sata_hba_inst, &sata_device,
20176 20175 SATA_DEV_IDENTIFY_NORETRY);
20177 20176
20178 20177 mutex_enter(&pmportinfo->pmport_mutex);
20179 20178 if ((pmportinfo->pmport_state & SATA_STATE_READY) &&
20180 20179 (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE)) {
20181 20180 /* Some device is attached to the port */
20182 20181 if (pmportinfo->pmport_dev_type == SATA_DTYPE_UNKNOWN) {
20183 20182 /*
20184 20183 * A device was not successfully attached.
20185 20184 * Track retry time for device identification.
20186 20185 */
20187 20186 if (pmportinfo->pmport_dev_attach_time != 0) {
20188 20187 clock_t cur_time = ddi_get_lbolt();
20189 20188 /*
20190 20189 * If the retry time limit was not exceeded,
20191 20190 * reinstate attach event.
20192 20191 */
20193 20192 if ((cur_time -
20194 20193 pmportinfo->pmport_dev_attach_time) <
20195 20194 drv_usectohz(
20196 20195 SATA_DEV_IDENTIFY_TIMEOUT)) {
20197 20196 /* OK, restore attach event */
20198 20197 pmportinfo->pmport_event_flags |=
20199 20198 SATA_EVNT_DEVICE_ATTACHED;
20200 20199 } else {
20201 20200 /* Timeout - cannot identify device */
20202 20201 pmportinfo->pmport_dev_attach_time = 0;
20203 20202 sata_log(sata_hba_inst, CE_WARN,
20204 20203 "Could not identify SATA device "
20205 20204 "at port %d:%d",
20206 20205 cport, pmport);
20207 20206 }
20208 20207 } else {
20209 20208 /*
20210 20209 * Start tracking time for device
20211 20210 * identification.
20212 20211 * Save current time (lbolt value).
20213 20212 */
20214 20213 pmportinfo->pmport_dev_attach_time =
20215 20214 ddi_get_lbolt();
20216 20215 /* Restore attach event */
20217 20216 pmportinfo->pmport_event_flags |=
20218 20217 SATA_EVNT_DEVICE_ATTACHED;
20219 20218 }
20220 20219 } else {
20221 20220 /*
20222 20221 * If device was successfully attached, the subsequent
20223 20222 * action depends on a state of the
20224 20223 * sata_auto_online variable. If it is set to zero.
20225 20224 * an explicit 'configure' command will be needed to
20226 20225 * configure it. If its value is non-zero, we will
20227 20226 * attempt to online (configure) the device.
20228 20227 * First, log the message indicating that a device
20229 20228 * was attached.
20230 20229 */
20231 20230 pmportinfo->pmport_dev_attach_time = 0;
20232 20231 sata_log(sata_hba_inst, CE_WARN,
20233 20232 "SATA device detected at port %d:%d",
20234 20233 cport, pmport);
20235 20234
20236 20235 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20237 20236 sata_drive_info_t new_sdinfo;
20238 20237
20239 20238 /* Log device info data */
20240 20239 new_sdinfo = *(SATA_PMPORTINFO_DRV_INFO(
20241 20240 pmportinfo));
20242 20241 sata_show_drive_info(sata_hba_inst,
20243 20242 &new_sdinfo);
20244 20243 }
20245 20244
20246 20245 mutex_exit(&pmportinfo->pmport_mutex);
20247 20246
20248 20247 /*
20249 20248 * Make sure that there is no target node for that
20250 20249 * device. If so, release it. It should not happen,
20251 20250 * unless we had problem removing the node when
20252 20251 * device was detached.
20253 20252 */
20254 20253 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
20255 20254 saddr->cport, saddr->pmport);
20256 20255 mutex_enter(&pmportinfo->pmport_mutex);
20257 20256 if (tdip != NULL) {
20258 20257
20259 20258 #ifdef SATA_DEBUG
20260 20259 if ((pmportinfo->pmport_event_flags &
20261 20260 SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
20262 20261 sata_log(sata_hba_inst, CE_WARN,
20263 20262 "sata_process_device_attached: "
20264 20263 "old device target node exists!");
20265 20264 #endif
20266 20265 /*
20267 20266 * target node exists - try to unconfigure
20268 20267 * device and remove the node.
20269 20268 */
20270 20269 mutex_exit(&pmportinfo->pmport_mutex);
20271 20270 rval = ndi_devi_offline(tdip,
20272 20271 NDI_DEVI_REMOVE);
20273 20272 mutex_enter(&pmportinfo->pmport_mutex);
20274 20273
20275 20274 if (rval == NDI_SUCCESS) {
20276 20275 pmportinfo->pmport_event_flags &=
20277 20276 ~SATA_EVNT_TARGET_NODE_CLEANUP;
20278 20277 pmportinfo->pmport_tgtnode_clean =
20279 20278 B_TRUE;
20280 20279 } else {
20281 20280 /*
20282 20281 * PROBLEM - the target node remained
20283 20282 * and it belongs to a previously
20284 20283 * attached device.
20285 20284 * This happens when the file was open
20286 20285 * or the node was waiting for
20287 20286 * resources at the time the
20288 20287 * associated device was removed.
20289 20288 * Instruct event daemon to retry the
20290 20289 * cleanup later.
20291 20290 */
20292 20291 sata_log(sata_hba_inst,
20293 20292 CE_WARN,
20294 20293 "Application(s) accessing "
20295 20294 "previously attached SATA "
20296 20295 "device have to release "
20297 20296 "it before newly inserted "
20298 20297 "device can be made accessible."
20299 20298 "at port %d:%d",
20300 20299 cport, pmport);
20301 20300 pmportinfo->pmport_event_flags |=
20302 20301 SATA_EVNT_TARGET_NODE_CLEANUP;
20303 20302 pmportinfo->pmport_tgtnode_clean =
20304 20303 B_FALSE;
20305 20304 }
20306 20305 }
20307 20306 if (sata_auto_online != 0) {
20308 20307 pmportinfo->pmport_event_flags |=
20309 20308 SATA_EVNT_AUTOONLINE_DEVICE;
20310 20309 }
20311 20310
20312 20311 }
20313 20312 } else {
20314 20313 pmportinfo->pmport_dev_attach_time = 0;
20315 20314 }
20316 20315
20317 20316 event_flags = pmportinfo->pmport_event_flags;
20318 20317 mutex_exit(&pmportinfo->pmport_mutex);
20319 20318 if (event_flags != 0) {
20320 20319 mutex_enter(&sata_hba_inst->satahba_mutex);
20321 20320 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20322 20321 mutex_exit(&sata_hba_inst->satahba_mutex);
20323 20322 mutex_enter(&sata_mutex);
20324 20323 sata_event_pending |= SATA_EVNT_MAIN;
20325 20324 mutex_exit(&sata_mutex);
20326 20325 }
20327 20326
20328 20327 /* clear the reset_in_progress events */
20329 20328 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20330 20329 if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) {
20331 20330 /* must clear flags on cport */
20332 20331 sata_pmult_info_t *pminfo =
20333 20332 SATA_PMULT_INFO(sata_hba_inst,
20334 20333 saddr->cport);
20335 20334 pminfo->pmult_event_flags |=
20336 20335 SATA_EVNT_CLEAR_DEVICE_RESET;
20337 20336 }
20338 20337 }
20339 20338 }
20340 20339
20341 20340 /*
20342 20341 * Device Target Node Cleanup Event processing.
20343 20342 * If the target node associated with a sata port device is in
20344 20343 * DEVI_DEVICE_REMOVED state, an attempt is made to remove it.
20345 20344 * If the target node cannot be removed, the event flag is left intact,
20346 20345 * so that event daemon may re-run this function later.
20347 20346 *
20348 20347 * This function cannot be called in interrupt context (it may sleep).
20349 20348 *
20350 20349 * NOTE: Processes cport events only, not port multiplier ports.
20351 20350 */
20352 20351 static void
20353 20352 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
20354 20353 sata_address_t *saddr)
20355 20354 {
20356 20355 sata_cport_info_t *cportinfo;
20357 20356 dev_info_t *tdip;
20358 20357
20359 20358 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20360 20359 "Processing port %d device target node cleanup", saddr->cport);
20361 20360
20362 20361 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
20363 20362
20364 20363 /*
20365 20364 * Check if there is target node for that device and it is in the
20366 20365 * DEVI_DEVICE_REMOVED state. If so, release it.
20367 20366 */
20368 20367 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport,
20369 20368 saddr->pmport);
20370 20369 if (tdip != NULL) {
20371 20370 /*
20372 20371 * target node exists - check if it is target node of
20373 20372 * a removed device.
20374 20373 */
20375 20374 if (sata_check_device_removed(tdip) == B_TRUE) {
20376 20375 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20377 20376 "sata_process_target_node_cleanup: "
20378 20377 "old device target node exists!", NULL);
20379 20378 /*
20380 20379 * Unconfigure and remove the target node
20381 20380 */
20382 20381 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) ==
20383 20382 NDI_SUCCESS) {
20384 20383 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20385 20384 saddr->cport)->cport_mutex);
20386 20385 cportinfo->cport_event_flags &=
20387 20386 ~SATA_EVNT_TARGET_NODE_CLEANUP;
20388 20387 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20389 20388 saddr->cport)->cport_mutex);
20390 20389 return;
20391 20390 }
20392 20391 /*
20393 20392 * Event daemon will retry the cleanup later.
20394 20393 */
20395 20394 mutex_enter(&sata_hba_inst->satahba_mutex);
20396 20395 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20397 20396 mutex_exit(&sata_hba_inst->satahba_mutex);
20398 20397 mutex_enter(&sata_mutex);
20399 20398 sata_event_pending |= SATA_EVNT_MAIN;
20400 20399 mutex_exit(&sata_mutex);
20401 20400 }
20402 20401 } else {
20403 20402 if (saddr->qual == SATA_ADDR_CPORT ||
20404 20403 saddr->qual == SATA_ADDR_DCPORT) {
20405 20404 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20406 20405 saddr->cport)->cport_mutex);
20407 20406 cportinfo->cport_event_flags &=
20408 20407 ~SATA_EVNT_TARGET_NODE_CLEANUP;
20409 20408 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20410 20409 saddr->cport)->cport_mutex);
20411 20410 } else {
20412 20411 /* sanity check */
20413 20412 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) !=
20414 20413 SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst,
20415 20414 saddr->cport) == NULL)
20416 20415 return;
20417 20416 if (SATA_PMPORT_INFO(sata_hba_inst, saddr->cport,
20418 20417 saddr->pmport) == NULL)
20419 20418 return;
20420 20419
20421 20420 mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst,
20422 20421 saddr->cport, saddr->pmport)->pmport_mutex);
20423 20422 SATA_PMPORT_INFO(sata_hba_inst, saddr->cport,
20424 20423 saddr->pmport)->pmport_event_flags &=
20425 20424 ~SATA_EVNT_TARGET_NODE_CLEANUP;
20426 20425 mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst,
20427 20426 saddr->cport, saddr->pmport)->pmport_mutex);
20428 20427 }
20429 20428 }
20430 20429 }
20431 20430
20432 20431 /*
20433 20432 * Device AutoOnline Event processing.
20434 20433 * If attached device is to be onlined, an attempt is made to online this
20435 20434 * device, but only if there is no lingering (old) target node present.
20436 20435 * If the device cannot be onlined, the event flag is left intact,
20437 20436 * so that event daemon may re-run this function later.
20438 20437 *
20439 20438 * This function cannot be called in interrupt context (it may sleep).
20440 20439 *
20441 20440 * NOTE: Processes cport events only, not port multiplier ports.
20442 20441 */
20443 20442 static void
20444 20443 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst,
20445 20444 sata_address_t *saddr)
20446 20445 {
20447 20446 sata_cport_info_t *cportinfo;
20448 20447 sata_drive_info_t *sdinfo;
20449 20448 sata_device_t sata_device;
20450 20449 dev_info_t *tdip;
20451 20450
20452 20451 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20453 20452 "Processing port %d attached device auto-onlining", saddr->cport);
20454 20453
20455 20454 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
20456 20455
20457 20456 /*
20458 20457 * Check if device is present and recognized. If not, reset event.
20459 20458 */
20460 20459 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20461 20460 if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
20462 20461 /* Nothing to online */
20463 20462 cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
20464 20463 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20465 20464 saddr->cport)->cport_mutex);
20466 20465 return;
20467 20466 }
20468 20467 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20469 20468
20470 20469 /*
20471 20470 * Check if there is target node for this device and if it is in the
20472 20471 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep
20473 20472 * the event for later processing.
20474 20473 */
20475 20474 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport,
20476 20475 saddr->pmport);
20477 20476 if (tdip != NULL) {
20478 20477 /*
20479 20478 * target node exists - check if it is target node of
20480 20479 * a removed device.
20481 20480 */
20482 20481 if (sata_check_device_removed(tdip) == B_TRUE) {
20483 20482 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20484 20483 "sata_process_device_autoonline: "
20485 20484 "old device target node exists!", NULL);
20486 20485 /*
20487 20486 * Event daemon will retry device onlining later.
20488 20487 */
20489 20488 mutex_enter(&sata_hba_inst->satahba_mutex);
20490 20489 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20491 20490 mutex_exit(&sata_hba_inst->satahba_mutex);
20492 20491 mutex_enter(&sata_mutex);
20493 20492 sata_event_pending |= SATA_EVNT_MAIN;
20494 20493 mutex_exit(&sata_mutex);
20495 20494 return;
20496 20495 }
20497 20496 /*
20498 20497 * If the target node is not in the 'removed" state, assume
20499 20498 * that it belongs to this device. There is nothing more to do,
20500 20499 * but reset the event.
20501 20500 */
20502 20501 } else {
20503 20502
20504 20503 /*
20505 20504 * Try to online the device
20506 20505 * If there is any reset-related event, remove it. We are
20507 20506 * configuring the device and no state restoring is needed.
20508 20507 */
20509 20508 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20510 20509 saddr->cport)->cport_mutex);
20511 20510 sata_device.satadev_addr = *saddr;
20512 20511 if (saddr->qual == SATA_ADDR_CPORT)
20513 20512 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
20514 20513 else
20515 20514 sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
20516 20515 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
20517 20516 if (sdinfo != NULL) {
20518 20517 if (sdinfo->satadrv_event_flags &
20519 20518 (SATA_EVNT_DEVICE_RESET |
20520 20519 SATA_EVNT_INPROC_DEVICE_RESET))
20521 20520 sdinfo->satadrv_event_flags = 0;
20522 20521 sdinfo->satadrv_event_flags |=
20523 20522 SATA_EVNT_CLEAR_DEVICE_RESET;
20524 20523
20525 20524 /* Need to create a new target node. */
20526 20525 cportinfo->cport_tgtnode_clean = B_TRUE;
20527 20526 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20528 20527 saddr->cport)->cport_mutex);
20529 20528 tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
20530 20529 sata_hba_inst, &sata_device.satadev_addr);
20531 20530 if (tdip == NULL) {
20532 20531 /*
20533 20532 * Configure (onlining) failed.
20534 20533 * We will NOT retry
20535 20534 */
20536 20535 SATA_LOG_D((sata_hba_inst, CE_WARN,
20537 20536 "sata_process_device_autoonline: "
20538 20537 "configuring SATA device at port %d failed",
20539 20538 saddr->cport));
20540 20539 }
20541 20540 } else {
20542 20541 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20543 20542 saddr->cport)->cport_mutex);
20544 20543 }
20545 20544
20546 20545 }
20547 20546 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20548 20547 cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
20549 20548 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20550 20549 saddr->cport)->cport_mutex);
20551 20550 }
20552 20551
20553 20552
20554 20553 static void
20555 20554 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr,
20556 20555 int hint)
20557 20556 {
20558 20557 char ap[MAXPATHLEN];
20559 20558 nvlist_t *ev_attr_list = NULL;
20560 20559 int err;
20561 20560
20562 20561 /* Allocate and build sysevent attribute list */
20563 20562 err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP);
20564 20563 if (err != 0) {
20565 20564 SATA_LOG_D((sata_hba_inst, CE_WARN,
20566 20565 "sata_gen_sysevent: "
20567 20566 "cannot allocate memory for sysevent attributes\n"));
20568 20567 return;
20569 20568 }
20570 20569 /* Add hint attribute */
20571 20570 err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint));
20572 20571 if (err != 0) {
20573 20572 SATA_LOG_D((sata_hba_inst, CE_WARN,
20574 20573 "sata_gen_sysevent: "
20575 20574 "failed to add DR_HINT attr for sysevent"));
20576 20575 nvlist_free(ev_attr_list);
20577 20576 return;
20578 20577 }
20579 20578 /*
20580 20579 * Add AP attribute.
20581 20580 * Get controller pathname and convert it into AP pathname by adding
20582 20581 * a target number.
20583 20582 */
20584 20583 (void) snprintf(ap, MAXPATHLEN, "/devices");
20585 20584 (void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap));
20586 20585 (void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d",
20587 20586 SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual));
20588 20587
20589 20588 err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap);
20590 20589 if (err != 0) {
20591 20590 SATA_LOG_D((sata_hba_inst, CE_WARN,
20592 20591 "sata_gen_sysevent: "
20593 20592 "failed to add DR_AP_ID attr for sysevent"));
20594 20593 nvlist_free(ev_attr_list);
20595 20594 return;
20596 20595 }
20597 20596
20598 20597 /* Generate/log sysevent */
20599 20598 err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR,
20600 20599 ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP);
20601 20600 if (err != DDI_SUCCESS) {
20602 20601 SATA_LOG_D((sata_hba_inst, CE_WARN,
20603 20602 "sata_gen_sysevent: "
20604 20603 "cannot log sysevent, err code %x\n", err));
20605 20604 }
20606 20605
20607 20606 nvlist_free(ev_attr_list);
20608 20607 }
20609 20608
20610 20609
20611 20610
20612 20611
20613 20612 /*
20614 20613 * Set DEVI_DEVICE_REMOVED state in the SATA device target node.
20615 20614 */
20616 20615 static void
20617 20616 sata_set_device_removed(dev_info_t *tdip)
20618 20617 {
20619 20618 int circ;
20620 20619
20621 20620 ASSERT(tdip != NULL);
20622 20621
20623 20622 ndi_devi_enter(tdip, &circ);
20624 20623 mutex_enter(&DEVI(tdip)->devi_lock);
20625 20624 DEVI_SET_DEVICE_REMOVED(tdip);
20626 20625 mutex_exit(&DEVI(tdip)->devi_lock);
20627 20626 ndi_devi_exit(tdip, circ);
20628 20627 }
20629 20628
20630 20629
20631 20630 /*
20632 20631 * Set internal event instructing event daemon to try
20633 20632 * to perform the target node cleanup.
20634 20633 */
20635 20634 static void
20636 20635 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
20637 20636 sata_address_t *saddr)
20638 20637 {
20639 20638 if (saddr->qual == SATA_ADDR_CPORT ||
20640 20639 saddr->qual == SATA_ADDR_DCPORT) {
20641 20640 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20642 20641 saddr->cport)->cport_mutex);
20643 20642 SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |=
20644 20643 SATA_EVNT_TARGET_NODE_CLEANUP;
20645 20644 SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
20646 20645 cport_tgtnode_clean = B_FALSE;
20647 20646 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20648 20647 saddr->cport)->cport_mutex);
20649 20648 } else {
20650 20649 mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst,
20651 20650 saddr->cport, saddr->pmport)->pmport_mutex);
20652 20651 SATA_PMPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport,
20653 20652 saddr->pmport) |= SATA_EVNT_TARGET_NODE_CLEANUP;
20654 20653 SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, saddr->pmport)->
20655 20654 pmport_tgtnode_clean = B_FALSE;
20656 20655 mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst,
20657 20656 saddr->cport, saddr->pmport)->pmport_mutex);
20658 20657 }
20659 20658 mutex_enter(&sata_hba_inst->satahba_mutex);
20660 20659 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20661 20660 mutex_exit(&sata_hba_inst->satahba_mutex);
20662 20661 mutex_enter(&sata_mutex);
20663 20662 sata_event_pending |= SATA_EVNT_MAIN;
20664 20663 mutex_exit(&sata_mutex);
20665 20664 }
20666 20665
20667 20666
20668 20667 /*
20669 20668 * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state,
20670 20669 * i.e. check if the target node state indicates that it belongs to a removed
20671 20670 * device.
20672 20671 *
20673 20672 * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state,
20674 20673 * B_FALSE otherwise.
20675 20674 */
20676 20675 static boolean_t
20677 20676 sata_check_device_removed(dev_info_t *tdip)
20678 20677 {
20679 20678 ASSERT(tdip != NULL);
20680 20679
20681 20680 if (DEVI_IS_DEVICE_REMOVED(tdip))
20682 20681 return (B_TRUE);
20683 20682 else
20684 20683 return (B_FALSE);
20685 20684 }
20686 20685
20687 20686
20688 20687 /*
20689 20688 * Check for DMA error. Return B_TRUE if error, B_FALSE otherwise.
20690 20689 */
20691 20690 static boolean_t
20692 20691 sata_check_for_dma_error(dev_info_t *dip, sata_pkt_txlate_t *spx)
20693 20692 {
20694 20693 int fm_capability = ddi_fm_capable(dip);
20695 20694 ddi_fm_error_t de;
20696 20695
20697 20696 if (fm_capability & DDI_FM_DMACHK_CAPABLE) {
20698 20697 if (spx->txlt_buf_dma_handle != NULL) {
20699 20698 ddi_fm_dma_err_get(spx->txlt_buf_dma_handle, &de,
20700 20699 DDI_FME_VERSION);
20701 20700 if (de.fme_status != DDI_SUCCESS)
20702 20701 return (B_TRUE);
20703 20702 }
20704 20703 }
20705 20704 return (B_FALSE);
20706 20705 }
20707 20706
20708 20707
20709 20708 /* ************************ FAULT INJECTTION **************************** */
20710 20709
20711 20710 #ifdef SATA_INJECT_FAULTS
20712 20711
20713 20712 static uint32_t sata_fault_count = 0;
20714 20713 static uint32_t sata_fault_suspend_count = 0;
20715 20714
20716 20715 /*
20717 20716 * Inject sata pkt fault
20718 20717 * It modifies returned values of the sata packet.
20719 20718 * It returns immediately if:
20720 20719 * pkt fault injection is not enabled (via sata_inject_fault,
20721 20720 * sata_inject_fault_count), or invalid fault is specified (sata_fault_type),
20722 20721 * or pkt does not contain command to be faulted (set in sata_fault_cmd), or
20723 20722 * pkt is not directed to specified fault controller/device
20724 20723 * (sata_fault_ctrl_dev and sata_fault_device).
20725 20724 * If fault controller is not specified, fault injection applies to all
20726 20725 * controllers and devices.
20727 20726 *
20728 20727 * First argument is the pointer to the executed sata packet.
20729 20728 * Second argument is a pointer to a value returned by the HBA tran_start
20730 20729 * function.
20731 20730 * Third argument specifies injected error. Injected sata packet faults
20732 20731 * are the satapkt_reason values.
20733 20732 * SATA_PKT_BUSY -1 Not completed, busy
20734 20733 * SATA_PKT_DEV_ERROR 1 Device reported error
20735 20734 * SATA_PKT_QUEUE_FULL 2 Not accepted, queue full
20736 20735 * SATA_PKT_PORT_ERROR 3 Not completed, port error
20737 20736 * SATA_PKT_CMD_UNSUPPORTED 4 Cmd unsupported
20738 20737 * SATA_PKT_ABORTED 5 Aborted by request
20739 20738 * SATA_PKT_TIMEOUT 6 Operation timeut
20740 20739 * SATA_PKT_RESET 7 Aborted by reset request
20741 20740 *
20742 20741 * Additional global variables affecting the execution:
20743 20742 *
20744 20743 * sata_inject_fault_count variable specifies number of times in row the
20745 20744 * error is injected. Value of -1 specifies permanent fault, ie. every time
20746 20745 * the fault injection point is reached, the fault is injected and a pause
20747 20746 * between fault injection specified by sata_inject_fault_pause_count is
20748 20747 * ignored). Fault injection routine decrements sata_inject_fault_count
20749 20748 * (if greater than zero) until it reaches 0. No fault is injected when
20750 20749 * sata_inject_fault_count is 0 (zero).
20751 20750 *
20752 20751 * sata_inject_fault_pause_count variable specifies number of times a fault
20753 20752 * injection is bypassed (pause between fault injections).
20754 20753 * If set to 0, a fault is injected only a number of times specified by
20755 20754 * sata_inject_fault_count.
20756 20755 *
20757 20756 * The fault counts are static, so for periodic errors they have to be manually
20758 20757 * reset to start repetition sequence from scratch.
20759 20758 * If the original value returned by the HBA tran_start function is not
20760 20759 * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error
20761 20760 * is injected (to avoid masking real problems);
20762 20761 *
20763 20762 * NOTE: In its current incarnation, this function should be invoked only for
20764 20763 * commands executed in SYNCHRONOUS mode.
20765 20764 */
20766 20765
20767 20766
20768 20767 static void
20769 20768 sata_inject_pkt_fault(sata_pkt_t *spkt, int *rval, int fault)
20770 20769 {
20771 20770
20772 20771 if (sata_inject_fault != SATA_INJECT_PKT_FAULT)
20773 20772 return;
20774 20773
20775 20774 if (sata_inject_fault_count == 0)
20776 20775 return;
20777 20776
20778 20777 if (fault == 0)
20779 20778 return;
20780 20779
20781 20780 if (sata_fault_cmd != spkt->satapkt_cmd.satacmd_cmd_reg)
20782 20781 return;
20783 20782
20784 20783 if (sata_fault_ctrl != NULL) {
20785 20784 sata_pkt_txlate_t *spx =
20786 20785 (sata_pkt_txlate_t *)spkt->satapkt_framework_private;
20787 20786
20788 20787 if (sata_fault_ctrl != NULL && sata_fault_ctrl !=
20789 20788 spx->txlt_sata_hba_inst->satahba_dip)
20790 20789 return;
20791 20790
20792 20791 if (sata_fault_device.satadev_addr.cport !=
20793 20792 spkt->satapkt_device.satadev_addr.cport ||
20794 20793 sata_fault_device.satadev_addr.pmport !=
20795 20794 spkt->satapkt_device.satadev_addr.pmport ||
20796 20795 sata_fault_device.satadev_addr.qual !=
20797 20796 spkt->satapkt_device.satadev_addr.qual)
20798 20797 return;
20799 20798 }
20800 20799
20801 20800 /* Modify pkt return parameters */
20802 20801 if (*rval != SATA_TRAN_ACCEPTED ||
20803 20802 spkt->satapkt_reason != SATA_PKT_COMPLETED) {
20804 20803 sata_fault_count = 0;
20805 20804 sata_fault_suspend_count = 0;
20806 20805 return;
20807 20806 }
20808 20807 if (sata_fault_count == 0 && sata_fault_suspend_count != 0) {
20809 20808 /* Pause in the injection */
20810 20809 sata_fault_suspend_count -= 1;
20811 20810 return;
20812 20811 }
20813 20812
20814 20813 if (sata_fault_count == 0 && sata_fault_suspend_count == 0) {
20815 20814 /*
20816 20815 * Init inject fault cycle. If fault count is set to -1,
20817 20816 * it is a permanent fault.
20818 20817 */
20819 20818 if (sata_inject_fault_count != -1) {
20820 20819 sata_fault_count = sata_inject_fault_count;
20821 20820 sata_fault_suspend_count =
20822 20821 sata_inject_fault_pause_count;
20823 20822 if (sata_fault_suspend_count == 0)
20824 20823 sata_inject_fault_count = 0;
20825 20824 }
20826 20825 }
20827 20826
20828 20827 if (sata_fault_count != 0)
20829 20828 sata_fault_count -= 1;
20830 20829
20831 20830 switch (fault) {
20832 20831 case SATA_PKT_BUSY:
20833 20832 *rval = SATA_TRAN_BUSY;
20834 20833 spkt->satapkt_reason = SATA_PKT_BUSY;
20835 20834 break;
20836 20835
20837 20836 case SATA_PKT_QUEUE_FULL:
20838 20837 *rval = SATA_TRAN_QUEUE_FULL;
20839 20838 spkt->satapkt_reason = SATA_PKT_QUEUE_FULL;
20840 20839 break;
20841 20840
20842 20841 case SATA_PKT_CMD_UNSUPPORTED:
20843 20842 *rval = SATA_TRAN_CMD_UNSUPPORTED;
20844 20843 spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED;
20845 20844 break;
20846 20845
20847 20846 case SATA_PKT_PORT_ERROR:
20848 20847 /* This is "rejected" command */
20849 20848 *rval = SATA_TRAN_PORT_ERROR;
20850 20849 spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
20851 20850 /* Additional error setup could be done here - port state */
20852 20851 break;
20853 20852
20854 20853 case SATA_PKT_DEV_ERROR:
20855 20854 spkt->satapkt_reason = SATA_PKT_DEV_ERROR;
20856 20855 /*
20857 20856 * Additional error setup could be done here
20858 20857 */
20859 20858 break;
20860 20859
20861 20860 case SATA_PKT_ABORTED:
20862 20861 spkt->satapkt_reason = SATA_PKT_ABORTED;
20863 20862 break;
20864 20863
20865 20864 case SATA_PKT_TIMEOUT:
20866 20865 spkt->satapkt_reason = SATA_PKT_TIMEOUT;
20867 20866 /* Additional error setup could be done here */
20868 20867 break;
20869 20868
20870 20869 case SATA_PKT_RESET:
20871 20870 spkt->satapkt_reason = SATA_PKT_RESET;
20872 20871 /*
20873 20872 * Additional error setup could be done here - device reset
20874 20873 */
20875 20874 break;
20876 20875
20877 20876 default:
20878 20877 break;
20879 20878 }
20880 20879 }
20881 20880
20882 20881 #endif
20883 20882
20884 20883 /*
20885 20884 * SATA Trace Ring Buffer
20886 20885 * ----------------------
20887 20886 *
20888 20887 * Overview
20889 20888 *
20890 20889 * The SATA trace ring buffer is a ring buffer created and managed by
20891 20890 * the SATA framework module that can be used by any module or driver
20892 20891 * within the SATA framework to store debug messages.
20893 20892 *
20894 20893 * Ring Buffer Interfaces:
20895 20894 *
20896 20895 * sata_vtrace_debug() <-- Adds debug message to ring buffer
20897 20896 * sata_trace_debug() <-- Wraps varargs into sata_vtrace_debug()
20898 20897 *
20899 20898 * Note that the sata_trace_debug() interface was created to give
20900 20899 * consumers the flexibilty of sending debug messages to ring buffer
20901 20900 * as variable arguments. Consumers can send type va_list debug
20902 20901 * messages directly to sata_vtrace_debug(). The sata_trace_debug()
20903 20902 * and sata_vtrace_debug() relationship is similar to that of
20904 20903 * cmn_err(9F) and vcmn_err(9F).
20905 20904 *
20906 20905 * Below is a diagram of the SATA trace ring buffer interfaces and
20907 20906 * sample consumers:
20908 20907 *
20909 20908 * +---------------------------------+
20910 20909 * | o o SATA Framework Module |
20911 20910 * | o SATA o +------------------+ +------------------+
20912 20911 * |o Trace o <--|sata_vtrace_debug/|<-----|SATA HBA Driver #1|
20913 20912 * |o R-Buf o |sata_trace_debug |<--+ +------------------+
20914 20913 * | o o +------------------+ | +------------------+
20915 20914 * | o o ^ | +--|SATA HBA Driver #2|
20916 20915 * | | | +------------------+
20917 20916 * | +------------------+ |
20918 20917 * | |SATA Debug Message| |
20919 20918 * | +------------------+ |
20920 20919 * +---------------------------------+
20921 20920 *
20922 20921 * Supporting Routines:
20923 20922 *
20924 20923 * sata_trace_rbuf_alloc() <-- Initializes ring buffer
20925 20924 * sata_trace_rbuf_free() <-- Destroys ring buffer
20926 20925 * sata_trace_dmsg_alloc() <-- Creates or reuses buffer in ring buffer
20927 20926 * sata_trace_dmsg_free() <-- Destroys content of ring buffer
20928 20927 *
20929 20928 * The default SATA trace ring buffer size is defined by DMSG_RING_SIZE.
20930 20929 * The ring buffer size can be adjusted by setting dmsg_ring_size in
20931 20930 * /etc/system to desired size in unit of bytes.
20932 20931 *
20933 20932 * The individual debug message size in the ring buffer is restricted
20934 20933 * to DMSG_BUF_SIZE.
20935 20934 */
20936 20935 void
20937 20936 sata_vtrace_debug(dev_info_t *dip, const char *fmt, va_list ap)
20938 20937 {
20939 20938 sata_trace_dmsg_t *dmsg;
20940 20939
20941 20940 if (sata_debug_rbuf == NULL) {
20942 20941 return;
20943 20942 }
20944 20943
20945 20944 /*
20946 20945 * If max size of ring buffer is smaller than size
20947 20946 * required for one debug message then just return
20948 20947 * since we have no room for the debug message.
20949 20948 */
20950 20949 if (sata_debug_rbuf->maxsize < (sizeof (sata_trace_dmsg_t))) {
20951 20950 return;
20952 20951 }
20953 20952
20954 20953 mutex_enter(&sata_debug_rbuf->lock);
20955 20954
20956 20955 /* alloc or reuse on ring buffer */
20957 20956 dmsg = sata_trace_dmsg_alloc();
20958 20957
20959 20958 if (dmsg == NULL) {
20960 20959 /* resource allocation failed */
20961 20960 mutex_exit(&sata_debug_rbuf->lock);
20962 20961 return;
20963 20962 }
20964 20963
20965 20964 dmsg->dip = dip;
20966 20965 gethrestime(&dmsg->timestamp);
20967 20966
20968 20967 (void) vsnprintf(dmsg->buf, sizeof (dmsg->buf), fmt, ap);
20969 20968
20970 20969 mutex_exit(&sata_debug_rbuf->lock);
20971 20970 }
20972 20971
20973 20972 void
20974 20973 sata_trace_debug(dev_info_t *dip, const char *fmt, ...)
20975 20974 {
20976 20975 va_list ap;
20977 20976
20978 20977 va_start(ap, fmt);
20979 20978 sata_vtrace_debug(dip, fmt, ap);
20980 20979 va_end(ap);
20981 20980 }
20982 20981
20983 20982 /*
20984 20983 * This routine is used to manage debug messages
20985 20984 * on ring buffer.
20986 20985 */
20987 20986 static sata_trace_dmsg_t *
20988 20987 sata_trace_dmsg_alloc(void)
20989 20988 {
20990 20989 sata_trace_dmsg_t *dmsg_alloc, *dmsg = sata_debug_rbuf->dmsgp;
20991 20990
20992 20991 if (sata_debug_rbuf->looped == TRUE) {
20993 20992 sata_debug_rbuf->dmsgp = dmsg->next;
20994 20993 return (sata_debug_rbuf->dmsgp);
20995 20994 }
20996 20995
20997 20996 /*
20998 20997 * If we're looping for the first time,
20999 20998 * connect the ring.
21000 20999 */
21001 21000 if (((sata_debug_rbuf->size + (sizeof (sata_trace_dmsg_t))) >
21002 21001 sata_debug_rbuf->maxsize) && (sata_debug_rbuf->dmsgh != NULL)) {
21003 21002 dmsg->next = sata_debug_rbuf->dmsgh;
21004 21003 sata_debug_rbuf->dmsgp = sata_debug_rbuf->dmsgh;
21005 21004 sata_debug_rbuf->looped = TRUE;
21006 21005 return (sata_debug_rbuf->dmsgp);
21007 21006 }
21008 21007
21009 21008 /* If we've gotten this far then memory allocation is needed */
21010 21009 dmsg_alloc = kmem_zalloc(sizeof (sata_trace_dmsg_t), KM_NOSLEEP);
21011 21010 if (dmsg_alloc == NULL) {
21012 21011 sata_debug_rbuf->allocfailed++;
21013 21012 return (dmsg_alloc);
21014 21013 } else {
21015 21014 sata_debug_rbuf->size += sizeof (sata_trace_dmsg_t);
21016 21015 }
21017 21016
21018 21017 if (sata_debug_rbuf->dmsgp != NULL) {
21019 21018 dmsg->next = dmsg_alloc;
21020 21019 sata_debug_rbuf->dmsgp = dmsg->next;
21021 21020 return (sata_debug_rbuf->dmsgp);
21022 21021 } else {
21023 21022 /*
21024 21023 * We should only be here if we're initializing
21025 21024 * the ring buffer.
21026 21025 */
21027 21026 if (sata_debug_rbuf->dmsgh == NULL) {
21028 21027 sata_debug_rbuf->dmsgh = dmsg_alloc;
21029 21028 } else {
21030 21029 /* Something is wrong */
21031 21030 kmem_free(dmsg_alloc, sizeof (sata_trace_dmsg_t));
21032 21031 return (NULL);
21033 21032 }
21034 21033
21035 21034 sata_debug_rbuf->dmsgp = dmsg_alloc;
21036 21035 return (sata_debug_rbuf->dmsgp);
21037 21036 }
21038 21037 }
21039 21038
21040 21039
21041 21040 /*
21042 21041 * Free all messages on debug ring buffer.
21043 21042 */
21044 21043 static void
21045 21044 sata_trace_dmsg_free(void)
21046 21045 {
21047 21046 sata_trace_dmsg_t *dmsg_next, *dmsg = sata_debug_rbuf->dmsgh;
21048 21047
21049 21048 while (dmsg != NULL) {
21050 21049 dmsg_next = dmsg->next;
21051 21050 kmem_free(dmsg, sizeof (sata_trace_dmsg_t));
21052 21051
21053 21052 /*
21054 21053 * If we've looped around the ring than we're done.
21055 21054 */
21056 21055 if (dmsg_next == sata_debug_rbuf->dmsgh) {
21057 21056 break;
21058 21057 } else {
21059 21058 dmsg = dmsg_next;
21060 21059 }
21061 21060 }
21062 21061 }
21063 21062
21064 21063
21065 21064 /*
21066 21065 * This function can block
21067 21066 */
21068 21067 static void
21069 21068 sata_trace_rbuf_alloc(void)
21070 21069 {
21071 21070 sata_debug_rbuf = kmem_zalloc(sizeof (sata_trace_rbuf_t), KM_SLEEP);
21072 21071
21073 21072 mutex_init(&sata_debug_rbuf->lock, NULL, MUTEX_DRIVER, NULL);
21074 21073
21075 21074 if (dmsg_ring_size > 0) {
21076 21075 sata_debug_rbuf->maxsize = (size_t)dmsg_ring_size;
21077 21076 }
21078 21077 }
21079 21078
21080 21079
21081 21080 static void
21082 21081 sata_trace_rbuf_free(void)
21083 21082 {
21084 21083 sata_trace_dmsg_free();
21085 21084 mutex_destroy(&sata_debug_rbuf->lock);
21086 21085 kmem_free(sata_debug_rbuf, sizeof (sata_trace_rbuf_t));
21087 21086 }
21088 21087
21089 21088 /*
21090 21089 * If SATA_DEBUG is not defined then this routine is called instead
21091 21090 * of sata_log() via the SATA_LOG_D macro.
21092 21091 */
21093 21092 static void
21094 21093 sata_trace_log(sata_hba_inst_t *sata_hba_inst, uint_t level,
21095 21094 const char *fmt, ...)
21096 21095 {
21097 21096 #ifndef __lock_lint
21098 21097 _NOTE(ARGUNUSED(level))
21099 21098 #endif
21100 21099
21101 21100 dev_info_t *dip = NULL;
21102 21101 va_list ap;
21103 21102
21104 21103 if (sata_hba_inst != NULL) {
21105 21104 dip = SATA_DIP(sata_hba_inst);
21106 21105 }
21107 21106
21108 21107 va_start(ap, fmt);
21109 21108 sata_vtrace_debug(dip, fmt, ap);
21110 21109 va_end(ap);
21111 21110 }
↓ open down ↓ |
20661 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX