/[base]/stable/12/sys/cam/ata/ata_da.c
ViewVC logotype

Contents of /stable/12/sys/cam/ata/ata_da.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 371915 - (show annotations) (download)
Sat Apr 9 06:36:02 2022 UTC (2 years, 7 months ago) by git2svn
File MIME type: text/plain
File size: 100606 byte(s)
cam: Fix typos in source code comments

- s/paniced/panicked/

(cherry picked from commit 49dace1d465dd99b72a73a9d373137be7816e2f0)

Git Hash:   782e8223681a1011ff9cc5d8f468e0dc6760ce3c
Git Author: gbe@FreeBSD.org
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include "opt_ada.h"
33
34 #include <sys/param.h>
35
36 #ifdef _KERNEL
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/bio.h>
40 #include <sys/sysctl.h>
41 #include <sys/taskqueue.h>
42 #include <sys/lock.h>
43 #include <sys/mutex.h>
44 #include <sys/conf.h>
45 #include <sys/devicestat.h>
46 #include <sys/eventhandler.h>
47 #include <sys/malloc.h>
48 #include <sys/endian.h>
49 #include <sys/cons.h>
50 #include <sys/proc.h>
51 #include <sys/reboot.h>
52 #include <sys/sbuf.h>
53 #include <geom/geom_disk.h>
54 #endif /* _KERNEL */
55
56 #ifndef _KERNEL
57 #include <stdio.h>
58 #include <string.h>
59 #endif /* _KERNEL */
60
61 #include <cam/cam.h>
62 #include <cam/cam_ccb.h>
63 #include <cam/cam_periph.h>
64 #include <cam/cam_xpt_periph.h>
65 #include <cam/scsi/scsi_all.h>
66 #include <cam/scsi/scsi_da.h>
67 #include <cam/cam_sim.h>
68 #include <cam/cam_iosched.h>
69
70 #include <cam/ata/ata_all.h>
71
72 #include <machine/md_var.h> /* geometry translation */
73
74 #ifdef _KERNEL
75
76 #define ATA_MAX_28BIT_LBA 268435455UL
77
78 extern int iosched_debug;
79
80 typedef enum {
81 ADA_STATE_RAHEAD,
82 ADA_STATE_WCACHE,
83 ADA_STATE_LOGDIR,
84 ADA_STATE_IDDIR,
85 ADA_STATE_SUP_CAP,
86 ADA_STATE_ZONE,
87 ADA_STATE_NORMAL
88 } ada_state;
89
90 typedef enum {
91 ADA_FLAG_CAN_48BIT = 0x00000002,
92 ADA_FLAG_CAN_FLUSHCACHE = 0x00000004,
93 ADA_FLAG_CAN_NCQ = 0x00000008,
94 ADA_FLAG_CAN_DMA = 0x00000010,
95 ADA_FLAG_NEED_OTAG = 0x00000020,
96 ADA_FLAG_WAS_OTAG = 0x00000040,
97 ADA_FLAG_CAN_TRIM = 0x00000080,
98 ADA_FLAG_OPEN = 0x00000100,
99 ADA_FLAG_SCTX_INIT = 0x00000200,
100 ADA_FLAG_CAN_CFA = 0x00000400,
101 ADA_FLAG_CAN_POWERMGT = 0x00000800,
102 ADA_FLAG_CAN_DMA48 = 0x00001000,
103 ADA_FLAG_CAN_LOG = 0x00002000,
104 ADA_FLAG_CAN_IDLOG = 0x00004000,
105 ADA_FLAG_CAN_SUPCAP = 0x00008000,
106 ADA_FLAG_CAN_ZONE = 0x00010000,
107 ADA_FLAG_CAN_WCACHE = 0x00020000,
108 ADA_FLAG_CAN_RAHEAD = 0x00040000,
109 ADA_FLAG_PROBED = 0x00080000,
110 ADA_FLAG_ANNOUNCED = 0x00100000,
111 ADA_FLAG_DIRTY = 0x00200000,
112 ADA_FLAG_CAN_NCQ_TRIM = 0x00400000, /* CAN_TRIM also set */
113 ADA_FLAG_PIM_ATA_EXT = 0x00800000
114 } ada_flags;
115
116 typedef enum {
117 ADA_Q_NONE = 0x00,
118 ADA_Q_4K = 0x01,
119 ADA_Q_NCQ_TRIM_BROKEN = 0x02,
120 ADA_Q_LOG_BROKEN = 0x04,
121 ADA_Q_SMR_DM = 0x08,
122 ADA_Q_NO_TRIM = 0x10,
123 ADA_Q_128KB = 0x20
124 } ada_quirks;
125
126 #define ADA_Q_BIT_STRING \
127 "\020" \
128 "\0014K" \
129 "\002NCQ_TRIM_BROKEN" \
130 "\003LOG_BROKEN" \
131 "\004SMR_DM" \
132 "\005NO_TRIM" \
133 "\006128KB"
134
135 typedef enum {
136 ADA_CCB_RAHEAD = 0x01,
137 ADA_CCB_WCACHE = 0x02,
138 ADA_CCB_BUFFER_IO = 0x03,
139 ADA_CCB_DUMP = 0x05,
140 ADA_CCB_TRIM = 0x06,
141 ADA_CCB_LOGDIR = 0x07,
142 ADA_CCB_IDDIR = 0x08,
143 ADA_CCB_SUP_CAP = 0x09,
144 ADA_CCB_ZONE = 0x0a,
145 ADA_CCB_TYPE_MASK = 0x0F,
146 } ada_ccb_state;
147
148 typedef enum {
149 ADA_ZONE_NONE = 0x00,
150 ADA_ZONE_DRIVE_MANAGED = 0x01,
151 ADA_ZONE_HOST_AWARE = 0x02,
152 ADA_ZONE_HOST_MANAGED = 0x03
153 } ada_zone_mode;
154
155 typedef enum {
156 ADA_ZONE_FLAG_RZ_SUP = 0x0001,
157 ADA_ZONE_FLAG_OPEN_SUP = 0x0002,
158 ADA_ZONE_FLAG_CLOSE_SUP = 0x0004,
159 ADA_ZONE_FLAG_FINISH_SUP = 0x0008,
160 ADA_ZONE_FLAG_RWP_SUP = 0x0010,
161 ADA_ZONE_FLAG_SUP_MASK = (ADA_ZONE_FLAG_RZ_SUP |
162 ADA_ZONE_FLAG_OPEN_SUP |
163 ADA_ZONE_FLAG_CLOSE_SUP |
164 ADA_ZONE_FLAG_FINISH_SUP |
165 ADA_ZONE_FLAG_RWP_SUP),
166 ADA_ZONE_FLAG_URSWRZ = 0x0020,
167 ADA_ZONE_FLAG_OPT_SEQ_SET = 0x0040,
168 ADA_ZONE_FLAG_OPT_NONSEQ_SET = 0x0080,
169 ADA_ZONE_FLAG_MAX_SEQ_SET = 0x0100,
170 ADA_ZONE_FLAG_SET_MASK = (ADA_ZONE_FLAG_OPT_SEQ_SET |
171 ADA_ZONE_FLAG_OPT_NONSEQ_SET |
172 ADA_ZONE_FLAG_MAX_SEQ_SET)
173 } ada_zone_flags;
174
175 static struct ada_zone_desc {
176 ada_zone_flags value;
177 const char *desc;
178 } ada_zone_desc_table[] = {
179 {ADA_ZONE_FLAG_RZ_SUP, "Report Zones" },
180 {ADA_ZONE_FLAG_OPEN_SUP, "Open" },
181 {ADA_ZONE_FLAG_CLOSE_SUP, "Close" },
182 {ADA_ZONE_FLAG_FINISH_SUP, "Finish" },
183 {ADA_ZONE_FLAG_RWP_SUP, "Reset Write Pointer" },
184 };
185
186
187 /* Offsets into our private area for storing information */
188 #define ccb_state ppriv_field0
189 #define ccb_bp ppriv_ptr1
190
191 typedef enum {
192 ADA_DELETE_NONE,
193 ADA_DELETE_DISABLE,
194 ADA_DELETE_CFA_ERASE,
195 ADA_DELETE_DSM_TRIM,
196 ADA_DELETE_NCQ_DSM_TRIM,
197 ADA_DELETE_MIN = ADA_DELETE_CFA_ERASE,
198 ADA_DELETE_MAX = ADA_DELETE_NCQ_DSM_TRIM,
199 } ada_delete_methods;
200
201 static const char *ada_delete_method_names[] =
202 { "NONE", "DISABLE", "CFA_ERASE", "DSM_TRIM", "NCQ_DSM_TRIM" };
203 #if 0
204 static const char *ada_delete_method_desc[] =
205 { "NONE", "DISABLED", "CFA Erase", "DSM Trim", "DSM Trim via NCQ" };
206 #endif
207
208 struct disk_params {
209 u_int8_t heads;
210 u_int8_t secs_per_track;
211 u_int32_t cylinders;
212 u_int32_t secsize; /* Number of bytes/logical sector */
213 u_int64_t sectors; /* Total number sectors */
214 };
215
216 #define TRIM_MAX_BLOCKS 8
217 #define TRIM_MAX_RANGES (TRIM_MAX_BLOCKS * ATA_DSM_BLK_RANGES)
218 struct trim_request {
219 uint8_t data[TRIM_MAX_RANGES * ATA_DSM_RANGE_SIZE];
220 TAILQ_HEAD(, bio) bps;
221 };
222
223 struct ada_softc {
224 struct cam_iosched_softc *cam_iosched;
225 int outstanding_cmds; /* Number of active commands */
226 int refcount; /* Active xpt_action() calls */
227 ada_state state;
228 ada_flags flags;
229 ada_zone_mode zone_mode;
230 ada_zone_flags zone_flags;
231 struct ata_gp_log_dir ata_logdir;
232 int valid_logdir_len;
233 struct ata_identify_log_pages ata_iddir;
234 int valid_iddir_len;
235 uint64_t optimal_seq_zones;
236 uint64_t optimal_nonseq_zones;
237 uint64_t max_seq_zones;
238 ada_quirks quirks;
239 ada_delete_methods delete_method;
240 int trim_max_ranges;
241 int read_ahead;
242 int write_cache;
243 int unmappedio;
244 int rotating;
245 #ifdef CAM_TEST_FAILURE
246 int force_read_error;
247 int force_write_error;
248 int periodic_read_error;
249 int periodic_read_count;
250 #endif
251 struct ccb_pathinq cpi;
252 struct disk_params params;
253 struct disk *disk;
254 struct task sysctl_task;
255 struct sysctl_ctx_list sysctl_ctx;
256 struct sysctl_oid *sysctl_tree;
257 struct callout sendordered_c;
258 struct trim_request trim_req;
259 #ifdef CAM_IO_STATS
260 struct sysctl_ctx_list sysctl_stats_ctx;
261 struct sysctl_oid *sysctl_stats_tree;
262 u_int timeouts;
263 u_int errors;
264 u_int invalidations;
265 #endif
266 #define ADA_ANNOUNCETMP_SZ 80
267 char announce_temp[ADA_ANNOUNCETMP_SZ];
268 #define ADA_ANNOUNCE_SZ 400
269 char announce_buffer[ADA_ANNOUNCE_SZ];
270 };
271
272 struct ada_quirk_entry {
273 struct scsi_inquiry_pattern inq_pat;
274 ada_quirks quirks;
275 };
276
277 static struct ada_quirk_entry ada_quirk_table[] =
278 {
279 {
280 /* Sandisk X400 */
281 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SanDisk?SD8SB8U1T00*", "X4162000*" },
282 /*quirks*/ADA_Q_128KB
283 },
284 {
285 /* Hitachi Advanced Format (4k) drives */
286 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Hitachi H??????????E3*", "*" },
287 /*quirks*/ADA_Q_4K
288 },
289 {
290 /* Samsung Advanced Format (4k) drives */
291 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD155UI*", "*" },
292 /*quirks*/ADA_Q_4K
293 },
294 {
295 /* Samsung Advanced Format (4k) drives */
296 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD204UI*", "*" },
297 /*quirks*/ADA_Q_4K
298 },
299 {
300 /* Seagate Barracuda Green Advanced Format (4k) drives */
301 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DL*", "*" },
302 /*quirks*/ADA_Q_4K
303 },
304 {
305 /* Seagate Barracuda Advanced Format (4k) drives */
306 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???DM*", "*" },
307 /*quirks*/ADA_Q_4K
308 },
309 {
310 /* Seagate Barracuda Advanced Format (4k) drives */
311 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DM*", "*" },
312 /*quirks*/ADA_Q_4K
313 },
314 {
315 /* Seagate Momentus Advanced Format (4k) drives */
316 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500423AS*", "*" },
317 /*quirks*/ADA_Q_4K
318 },
319 {
320 /* Seagate Momentus Advanced Format (4k) drives */
321 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500424AS*", "*" },
322 /*quirks*/ADA_Q_4K
323 },
324 {
325 /* Seagate Momentus Advanced Format (4k) drives */
326 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640423AS*", "*" },
327 /*quirks*/ADA_Q_4K
328 },
329 {
330 /* Seagate Momentus Advanced Format (4k) drives */
331 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640424AS*", "*" },
332 /*quirks*/ADA_Q_4K
333 },
334 {
335 /* Seagate Momentus Advanced Format (4k) drives */
336 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750420AS*", "*" },
337 /*quirks*/ADA_Q_4K
338 },
339 {
340 /* Seagate Momentus Advanced Format (4k) drives */
341 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750422AS*", "*" },
342 /*quirks*/ADA_Q_4K
343 },
344 {
345 /* Seagate Momentus Advanced Format (4k) drives */
346 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750423AS*", "*" },
347 /*quirks*/ADA_Q_4K
348 },
349 {
350 /* Seagate Momentus Thin Advanced Format (4k) drives */
351 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???LT*", "*" },
352 /*quirks*/ADA_Q_4K
353 },
354 {
355 /* WDC Caviar Red Advanced Format (4k) drives */
356 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????CX*", "*" },
357 /*quirks*/ADA_Q_4K
358 },
359 {
360 /* WDC Caviar Green Advanced Format (4k) drives */
361 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RS*", "*" },
362 /*quirks*/ADA_Q_4K
363 },
364 {
365 /* WDC Caviar Green/Red Advanced Format (4k) drives */
366 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RX*", "*" },
367 /*quirks*/ADA_Q_4K
368 },
369 {
370 /* WDC Caviar Red Advanced Format (4k) drives */
371 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????CX*", "*" },
372 /*quirks*/ADA_Q_4K
373 },
374 {
375 /* WDC Caviar Black Advanced Format (4k) drives */
376 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????AZEX*", "*" },
377 /*quirks*/ADA_Q_4K
378 },
379 {
380 /* WDC Caviar Black Advanced Format (4k) drives */
381 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????FZEX*", "*" },
382 /*quirks*/ADA_Q_4K
383 },
384 {
385 /* WDC Caviar Green Advanced Format (4k) drives */
386 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RS*", "*" },
387 /*quirks*/ADA_Q_4K
388 },
389 {
390 /* WDC Caviar Green Advanced Format (4k) drives */
391 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RX*", "*" },
392 /*quirks*/ADA_Q_4K
393 },
394 {
395 /* WDC Scorpio Black Advanced Format (4k) drives */
396 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PKT*", "*" },
397 /*quirks*/ADA_Q_4K
398 },
399 {
400 /* WDC Scorpio Black Advanced Format (4k) drives */
401 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PKT*", "*" },
402 /*quirks*/ADA_Q_4K
403 },
404 {
405 /* WDC Scorpio Blue Advanced Format (4k) drives */
406 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PVT*", "*" },
407 /*quirks*/ADA_Q_4K
408 },
409 {
410 /* WDC Scorpio Blue Advanced Format (4k) drives */
411 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PVT*", "*" },
412 /*quirks*/ADA_Q_4K
413 },
414 /* SSDs */
415 {
416 /*
417 * Corsair Force 2 SSDs
418 * 4k optimised & trim only works in 4k requests + 4k aligned
419 */
420 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair CSSD-F*", "*" },
421 /*quirks*/ADA_Q_4K
422 },
423 {
424 /*
425 * Corsair Force 3 SSDs
426 * 4k optimised & trim only works in 4k requests + 4k aligned
427 */
428 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force 3*", "*" },
429 /*quirks*/ADA_Q_4K
430 },
431 {
432 /*
433 * Corsair Neutron GTX SSDs
434 * 4k optimised & trim only works in 4k requests + 4k aligned
435 */
436 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Neutron GTX*", "*" },
437 /*quirks*/ADA_Q_4K
438 },
439 {
440 /*
441 * Corsair Force GT & GS SSDs
442 * 4k optimised & trim only works in 4k requests + 4k aligned
443 */
444 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force G*", "*" },
445 /*quirks*/ADA_Q_4K
446 },
447 {
448 /*
449 * Crucial M4 SSDs
450 * 4k optimised & trim only works in 4k requests + 4k aligned
451 */
452 { T_DIRECT, SIP_MEDIA_FIXED, "*", "M4-CT???M4SSD2*", "*" },
453 /*quirks*/ADA_Q_4K
454 },
455 {
456 /*
457 * Crucial M500 SSDs MU07 firmware
458 * NCQ Trim works
459 */
460 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Crucial CT*M500*", "MU07" },
461 /*quirks*/0
462 },
463 {
464 /*
465 * Crucial M500 SSDs all other firmware
466 * NCQ Trim doesn't work
467 */
468 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Crucial CT*M500*", "*" },
469 /*quirks*/ADA_Q_NCQ_TRIM_BROKEN
470 },
471 {
472 /*
473 * Crucial M550 SSDs
474 * NCQ Trim doesn't work, but only on MU01 firmware
475 */
476 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Crucial CT*M550*", "MU01" },
477 /*quirks*/ADA_Q_NCQ_TRIM_BROKEN
478 },
479 {
480 /*
481 * Crucial MX100 SSDs
482 * NCQ Trim doesn't work, but only on MU01 firmware
483 */
484 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Crucial CT*MX100*", "MU01" },
485 /*quirks*/ADA_Q_NCQ_TRIM_BROKEN
486 },
487 {
488 /*
489 * Crucial RealSSD C300 SSDs
490 * 4k optimised
491 */
492 { T_DIRECT, SIP_MEDIA_FIXED, "*", "C300-CTFDDAC???MAG*",
493 "*" }, /*quirks*/ADA_Q_4K
494 },
495 {
496 /*
497 * FCCT M500 SSDs
498 * NCQ Trim doesn't work
499 */
500 { T_DIRECT, SIP_MEDIA_FIXED, "*", "FCCT*M500*", "*" },
501 /*quirks*/ADA_Q_NCQ_TRIM_BROKEN
502 },
503 {
504 /*
505 * Intel 320 Series SSDs
506 * 4k optimised & trim only works in 4k requests + 4k aligned
507 */
508 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2CW*", "*" },
509 /*quirks*/ADA_Q_4K
510 },
511 {
512 /*
513 * Intel 330 Series SSDs
514 * 4k optimised & trim only works in 4k requests + 4k aligned
515 */
516 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2CT*", "*" },
517 /*quirks*/ADA_Q_4K
518 },
519 {
520 /*
521 * Intel 510 Series SSDs
522 * 4k optimised & trim only works in 4k requests + 4k aligned
523 */
524 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2MH*", "*" },
525 /*quirks*/ADA_Q_4K
526 },
527 {
528 /*
529 * Intel 520 Series SSDs
530 * 4k optimised & trim only works in 4k requests + 4k aligned
531 */
532 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2BW*", "*" },
533 /*quirks*/ADA_Q_4K
534 },
535 {
536 /*
537 * Intel S3610 Series SSDs
538 * 4k optimised & trim only works in 4k requests + 4k aligned
539 */
540 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2BX*", "*" },
541 /*quirks*/ADA_Q_4K
542 },
543 {
544 /*
545 * Intel X25-M Series SSDs
546 * 4k optimised & trim only works in 4k requests + 4k aligned
547 */
548 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2M*", "*" },
549 /*quirks*/ADA_Q_4K
550 },
551 {
552 /*
553 * KingDian S200 60GB P0921B
554 * Trimming crash the SSD
555 */
556 { T_DIRECT, SIP_MEDIA_FIXED, "*", "KingDian S200 *", "*" },
557 /*quirks*/ADA_Q_NO_TRIM
558 },
559 {
560 /*
561 * Kingston E100 Series SSDs
562 * 4k optimised & trim only works in 4k requests + 4k aligned
563 */
564 { T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SE100S3*", "*" },
565 /*quirks*/ADA_Q_4K
566 },
567 {
568 /*
569 * Kingston HyperX 3k SSDs
570 * 4k optimised & trim only works in 4k requests + 4k aligned
571 */
572 { T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SH103S3*", "*" },
573 /*quirks*/ADA_Q_4K
574 },
575 {
576 /*
577 * Marvell SSDs (entry taken from OpenSolaris)
578 * 4k optimised & trim only works in 4k requests + 4k aligned
579 */
580 { T_DIRECT, SIP_MEDIA_FIXED, "*", "MARVELL SD88SA02*", "*" },
581 /*quirks*/ADA_Q_4K
582 },
583 {
584 /*
585 * Micron M500 SSDs firmware MU07
586 * NCQ Trim works?
587 */
588 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Micron M500*", "MU07" },
589 /*quirks*/0
590 },
591 {
592 /*
593 * Micron M500 SSDs all other firmware
594 * NCQ Trim doesn't work
595 */
596 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Micron M500*", "*" },
597 /*quirks*/ADA_Q_NCQ_TRIM_BROKEN
598 },
599 {
600 /*
601 * Micron M5[15]0 SSDs
602 * NCQ Trim doesn't work, but only MU01 firmware
603 */
604 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Micron M5[15]0*", "MU01" },
605 /*quirks*/ADA_Q_NCQ_TRIM_BROKEN
606 },
607 {
608 /*
609 * Micron 5100 SSDs
610 * 4k optimised & trim only works in 4k requests + 4k aligned
611 */
612 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Micron 5100 MTFDDAK*", "*" },
613 /*quirks*/ADA_Q_4K
614 },
615 {
616 /*
617 * OCZ Agility 2 SSDs
618 * 4k optimised & trim only works in 4k requests + 4k aligned
619 */
620 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY2*", "*" },
621 /*quirks*/ADA_Q_4K
622 },
623 {
624 /*
625 * OCZ Agility 3 SSDs
626 * 4k optimised & trim only works in 4k requests + 4k aligned
627 */
628 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY3*", "*" },
629 /*quirks*/ADA_Q_4K
630 },
631 {
632 /*
633 * OCZ Deneva R Series SSDs
634 * 4k optimised & trim only works in 4k requests + 4k aligned
635 */
636 { T_DIRECT, SIP_MEDIA_FIXED, "*", "DENRSTE251M45*", "*" },
637 /*quirks*/ADA_Q_4K
638 },
639 {
640 /*
641 * OCZ Vertex 2 SSDs (inc pro series)
642 * 4k optimised & trim only works in 4k requests + 4k aligned
643 */
644 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ?VERTEX2*", "*" },
645 /*quirks*/ADA_Q_4K
646 },
647 {
648 /*
649 * OCZ Vertex 3 SSDs
650 * 4k optimised & trim only works in 4k requests + 4k aligned
651 */
652 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX3*", "*" },
653 /*quirks*/ADA_Q_4K
654 },
655 {
656 /*
657 * OCZ Vertex 4 SSDs
658 * 4k optimised & trim only works in 4k requests + 4k aligned
659 */
660 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX4*", "*" },
661 /*quirks*/ADA_Q_4K
662 },
663 {
664 /*
665 * Samsung 750 SSDs
666 * 4k optimised, NCQ TRIM seems to work
667 */
668 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Samsung SSD 750*", "*" },
669 /*quirks*/ADA_Q_4K
670 },
671 {
672 /*
673 * Samsung 830 Series SSDs
674 * 4k optimised, NCQ TRIM Broken (normal TRIM is fine)
675 */
676 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG SSD 830 Series*", "*" },
677 /*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN
678 },
679 {
680 /*
681 * Samsung 840 SSDs
682 * 4k optimised, NCQ TRIM Broken (normal TRIM is fine)
683 */
684 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Samsung SSD 840*", "*" },
685 /*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN
686 },
687 {
688 /*
689 * Samsung 845 SSDs
690 * 4k optimised, NCQ TRIM Broken (normal TRIM is fine)
691 */
692 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Samsung SSD 845*", "*" },
693 /*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN
694 },
695 {
696 /*
697 * Samsung 850 SSDs
698 * 4k optimised, NCQ TRIM broken (normal TRIM fine)
699 */
700 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Samsung SSD 850*", "*" },
701 /*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN
702 },
703 {
704 /*
705 * Samsung SM863 Series SSDs (MZ7KM*)
706 * 4k optimised, NCQ believed to be working
707 */
708 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG MZ7KM*", "*" },
709 /*quirks*/ADA_Q_4K
710 },
711 {
712 /*
713 * Samsung 843T Series SSDs (MZ7WD*)
714 * Samsung PM851 Series SSDs (MZ7TE*)
715 * Samsung PM853T Series SSDs (MZ7GE*)
716 * 4k optimised, NCQ believed to be broken since these are
717 * appear to be built with the same controllers as the 840/850.
718 */
719 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG MZ7*", "*" },
720 /*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN
721 },
722 {
723 /*
724 * Same as for SAMSUNG MZ7* but enable the quirks for SSD
725 * starting with MZ7* too
726 */
727 { T_DIRECT, SIP_MEDIA_FIXED, "*", "MZ7*", "*" },
728 /*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN
729 },
730 {
731 /*
732 * Samsung PM851 Series SSDs Dell OEM
733 * device model "SAMSUNG SSD PM851 mSATA 256GB"
734 * 4k optimised, NCQ broken
735 */
736 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG SSD PM851*", "*" },
737 /*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN
738 },
739 {
740 /*
741 * SuperTalent TeraDrive CT SSDs
742 * 4k optimised & trim only works in 4k requests + 4k aligned
743 */
744 { T_DIRECT, SIP_MEDIA_FIXED, "*", "FTM??CT25H*", "*" },
745 /*quirks*/ADA_Q_4K
746 },
747 {
748 /*
749 * XceedIOPS SATA SSDs
750 * 4k optimised
751 */
752 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SG9XCS2D*", "*" },
753 /*quirks*/ADA_Q_4K
754 },
755 {
756 /*
757 * Samsung drive that doesn't support READ LOG EXT or
758 * READ LOG DMA EXT, despite reporting that it does in
759 * ATA identify data:
760 * SAMSUNG HD200HJ KF100-06
761 */
762 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD200*", "*" },
763 /*quirks*/ADA_Q_LOG_BROKEN
764 },
765 {
766 /*
767 * Samsung drive that doesn't support READ LOG EXT or
768 * READ LOG DMA EXT, despite reporting that it does in
769 * ATA identify data:
770 * SAMSUNG HD501LJ CR100-10
771 */
772 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD501*", "*" },
773 /*quirks*/ADA_Q_LOG_BROKEN
774 },
775 {
776 /*
777 * Seagate Lamarr 8TB Shingled Magnetic Recording (SMR)
778 * Drive Managed SATA hard drive. This drive doesn't report
779 * in firmware that it is a drive managed SMR drive.
780 */
781 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST8000AS000[23]*", "*" },
782 /*quirks*/ADA_Q_SMR_DM
783 },
784 {
785 /* WD Green SSD */
786 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WDS?????G0*", "*" },
787 /*quirks*/ADA_Q_4K | ADA_Q_NCQ_TRIM_BROKEN
788 },
789 {
790 /* Default */
791 {
792 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
793 /*vendor*/"*", /*product*/"*", /*revision*/"*"
794 },
795 /*quirks*/0
796 },
797 };
798
799 static disk_strategy_t adastrategy;
800 static dumper_t adadump;
801 static periph_init_t adainit;
802 static void adadiskgonecb(struct disk *dp);
803 static periph_oninv_t adaoninvalidate;
804 static periph_dtor_t adacleanup;
805 static void adaasync(void *callback_arg, u_int32_t code,
806 struct cam_path *path, void *arg);
807 static int adazonemodesysctl(SYSCTL_HANDLER_ARGS);
808 static int adazonesupsysctl(SYSCTL_HANDLER_ARGS);
809 static void adasysctlinit(void *context, int pending);
810 static int adagetattr(struct bio *bp);
811 static void adasetflags(struct ada_softc *softc,
812 struct ccb_getdev *cgd);
813 static void adasetgeom(struct ada_softc *softc,
814 struct ccb_getdev *cgd);
815 static periph_ctor_t adaregister;
816 static void ada_dsmtrim(struct ada_softc *softc, struct bio *bp,
817 struct ccb_ataio *ataio);
818 static void ada_cfaerase(struct ada_softc *softc, struct bio *bp,
819 struct ccb_ataio *ataio);
820 static int ada_zone_bio_to_ata(int disk_zone_cmd);
821 static int ada_zone_cmd(struct cam_periph *periph, union ccb *ccb,
822 struct bio *bp, int *queue_ccb);
823 static periph_start_t adastart;
824 static void adaprobedone(struct cam_periph *periph, union ccb *ccb);
825 static void adazonedone(struct cam_periph *periph, union ccb *ccb);
826 static void adadone(struct cam_periph *periph,
827 union ccb *done_ccb);
828 static int adaerror(union ccb *ccb, u_int32_t cam_flags,
829 u_int32_t sense_flags);
830 static timeout_t adasendorderedtag;
831 static void adashutdown(void *arg, int howto);
832 static void adasuspend(void *arg);
833 static void adaresume(void *arg);
834
835 #ifndef ADA_DEFAULT_TIMEOUT
836 #define ADA_DEFAULT_TIMEOUT 30 /* Timeout in seconds */
837 #endif
838
839 #ifndef ADA_DEFAULT_RETRY
840 #define ADA_DEFAULT_RETRY 4
841 #endif
842
843 #ifndef ADA_DEFAULT_SEND_ORDERED
844 #define ADA_DEFAULT_SEND_ORDERED 1
845 #endif
846
847 #ifndef ADA_DEFAULT_SPINDOWN_SHUTDOWN
848 #define ADA_DEFAULT_SPINDOWN_SHUTDOWN 1
849 #endif
850
851 #ifndef ADA_DEFAULT_SPINDOWN_SUSPEND
852 #define ADA_DEFAULT_SPINDOWN_SUSPEND 1
853 #endif
854
855 #ifndef ADA_DEFAULT_READ_AHEAD
856 #define ADA_DEFAULT_READ_AHEAD 1
857 #endif
858
859 #ifndef ADA_DEFAULT_WRITE_CACHE
860 #define ADA_DEFAULT_WRITE_CACHE 1
861 #endif
862
863 #define ADA_RA (softc->read_ahead >= 0 ? \
864 softc->read_ahead : ada_read_ahead)
865 #define ADA_WC (softc->write_cache >= 0 ? \
866 softc->write_cache : ada_write_cache)
867
868 /*
869 * Most platforms map firmware geometry to actual, but some don't. If
870 * not overridden, default to nothing.
871 */
872 #ifndef ata_disk_firmware_geom_adjust
873 #define ata_disk_firmware_geom_adjust(disk)
874 #endif
875
876 static int ada_retry_count = ADA_DEFAULT_RETRY;
877 static int ada_default_timeout = ADA_DEFAULT_TIMEOUT;
878 static int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED;
879 static int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN;
880 static int ada_spindown_suspend = ADA_DEFAULT_SPINDOWN_SUSPEND;
881 static int ada_read_ahead = ADA_DEFAULT_READ_AHEAD;
882 static int ada_write_cache = ADA_DEFAULT_WRITE_CACHE;
883
884 static SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD, 0,
885 "CAM Direct Access Disk driver");
886 SYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RWTUN,
887 &ada_retry_count, 0, "Normal I/O retry count");
888 SYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RWTUN,
889 &ada_default_timeout, 0, "Normal I/O timeout (in seconds)");
890 SYSCTL_INT(_kern_cam_ada, OID_AUTO, send_ordered, CTLFLAG_RWTUN,
891 &ada_send_ordered, 0, "Send Ordered Tags");
892 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RWTUN,
893 &ada_spindown_shutdown, 0, "Spin down upon shutdown");
894 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_suspend, CTLFLAG_RWTUN,
895 &ada_spindown_suspend, 0, "Spin down upon suspend");
896 SYSCTL_INT(_kern_cam_ada, OID_AUTO, read_ahead, CTLFLAG_RWTUN,
897 &ada_read_ahead, 0, "Enable disk read-ahead");
898 SYSCTL_INT(_kern_cam_ada, OID_AUTO, write_cache, CTLFLAG_RWTUN,
899 &ada_write_cache, 0, "Enable disk write cache");
900
901 /*
902 * ADA_ORDEREDTAG_INTERVAL determines how often, relative
903 * to the default timeout, we check to see whether an ordered
904 * tagged transaction is appropriate to prevent simple tag
905 * starvation. Since we'd like to ensure that there is at least
906 * 1/2 of the timeout length left for a starved transaction to
907 * complete after we've sent an ordered tag, we must poll at least
908 * four times in every timeout period. This takes care of the worst
909 * case where a starved transaction starts during an interval that
910 * meets the requirement "don't send an ordered tag" test so it takes
911 * us two intervals to determine that a tag must be sent.
912 */
913 #ifndef ADA_ORDEREDTAG_INTERVAL
914 #define ADA_ORDEREDTAG_INTERVAL 4
915 #endif
916
917 static struct periph_driver adadriver =
918 {
919 adainit, "ada",
920 TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0
921 };
922
923 static int adadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
924
925 PERIPHDRIVER_DECLARE(ada, adadriver);
926
927 static MALLOC_DEFINE(M_ATADA, "ata_da", "ata_da buffers");
928
929 static int
930 adaopen(struct disk *dp)
931 {
932 struct cam_periph *periph;
933 struct ada_softc *softc;
934 int error;
935
936 periph = (struct cam_periph *)dp->d_drv1;
937 if (cam_periph_acquire(periph) != 0) {
938 return(ENXIO);
939 }
940
941 cam_periph_lock(periph);
942 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
943 cam_periph_unlock(periph);
944 cam_periph_release(periph);
945 return (error);
946 }
947
948 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
949 ("adaopen\n"));
950
951 softc = (struct ada_softc *)periph->softc;
952 softc->flags |= ADA_FLAG_OPEN;
953
954 cam_periph_unhold(periph);
955 cam_periph_unlock(periph);
956 return (0);
957 }
958
959 static int
960 adaclose(struct disk *dp)
961 {
962 struct cam_periph *periph;
963 struct ada_softc *softc;
964 union ccb *ccb;
965 int error;
966
967 periph = (struct cam_periph *)dp->d_drv1;
968 softc = (struct ada_softc *)periph->softc;
969 cam_periph_lock(periph);
970
971 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
972 ("adaclose\n"));
973
974 /* We only sync the cache if the drive is capable of it. */
975 if ((softc->flags & ADA_FLAG_DIRTY) != 0 &&
976 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) != 0 &&
977 (periph->flags & CAM_PERIPH_INVALID) == 0 &&
978 cam_periph_hold(periph, PRIBIO) == 0) {
979
980 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
981 cam_fill_ataio(&ccb->ataio,
982 1,
983 NULL,
984 CAM_DIR_NONE,
985 0,
986 NULL,
987 0,
988 ada_default_timeout*1000);
989
990 if (softc->flags & ADA_FLAG_CAN_48BIT)
991 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
992 else
993 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
994 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
995 /*sense_flags*/0, softc->disk->d_devstat);
996
997 if (error != 0)
998 xpt_print(periph->path, "Synchronize cache failed\n");
999 softc->flags &= ~ADA_FLAG_DIRTY;
1000 xpt_release_ccb(ccb);
1001 cam_periph_unhold(periph);
1002 }
1003
1004 softc->flags &= ~ADA_FLAG_OPEN;
1005
1006 while (softc->refcount != 0)
1007 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "adaclose", 1);
1008 cam_periph_unlock(periph);
1009 cam_periph_release(periph);
1010 return (0);
1011 }
1012
1013 static void
1014 adaschedule(struct cam_periph *periph)
1015 {
1016 struct ada_softc *softc = (struct ada_softc *)periph->softc;
1017
1018 if (softc->state != ADA_STATE_NORMAL)
1019 return;
1020
1021 cam_iosched_schedule(softc->cam_iosched, periph);
1022 }
1023
1024 /*
1025 * Actually translate the requested transfer into one the physical driver
1026 * can understand. The transfer is described by a buf and will include
1027 * only one physical transfer.
1028 */
1029 static void
1030 adastrategy(struct bio *bp)
1031 {
1032 struct cam_periph *periph;
1033 struct ada_softc *softc;
1034
1035 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1036 softc = (struct ada_softc *)periph->softc;
1037
1038 cam_periph_lock(periph);
1039
1040 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastrategy(%p)\n", bp));
1041
1042 /*
1043 * If the device has been made invalid, error out
1044 */
1045 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
1046 cam_periph_unlock(periph);
1047 biofinish(bp, NULL, ENXIO);
1048 return;
1049 }
1050
1051 /*
1052 * Zone commands must be ordered, because they can depend on the
1053 * effects of previously issued commands, and they may affect
1054 * commands after them.
1055 */
1056 if (bp->bio_cmd == BIO_ZONE)
1057 bp->bio_flags |= BIO_ORDERED;
1058
1059 /*
1060 * Place it in the queue of disk activities for this disk
1061 */
1062 cam_iosched_queue_work(softc->cam_iosched, bp);
1063
1064 /*
1065 * Schedule ourselves for performing the work.
1066 */
1067 adaschedule(periph);
1068 cam_periph_unlock(periph);
1069
1070 return;
1071 }
1072
1073 static int
1074 adadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1075 {
1076 struct cam_periph *periph;
1077 struct ada_softc *softc;
1078 u_int secsize;
1079 struct ccb_ataio ataio;
1080 struct disk *dp;
1081 uint64_t lba;
1082 uint16_t count;
1083 int error = 0;
1084
1085 dp = arg;
1086 periph = dp->d_drv1;
1087 softc = (struct ada_softc *)periph->softc;
1088 secsize = softc->params.secsize;
1089 lba = offset / secsize;
1090 count = length / secsize;
1091 if ((periph->flags & CAM_PERIPH_INVALID) != 0)
1092 return (ENXIO);
1093
1094 memset(&ataio, 0, sizeof(ataio));
1095 if (length > 0) {
1096 xpt_setup_ccb(&ataio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1097 ataio.ccb_h.ccb_state = ADA_CCB_DUMP;
1098 cam_fill_ataio(&ataio,
1099 0,
1100 NULL,
1101 CAM_DIR_OUT,
1102 0,
1103 (u_int8_t *) virtual,
1104 length,
1105 ada_default_timeout*1000);
1106 if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
1107 (lba + count >= ATA_MAX_28BIT_LBA ||
1108 count >= 256)) {
1109 ata_48bit_cmd(&ataio, ATA_WRITE_DMA48,
1110 0, lba, count);
1111 } else {
1112 ata_28bit_cmd(&ataio, ATA_WRITE_DMA,
1113 0, lba, count);
1114 }
1115 error = cam_periph_runccb((union ccb *)&ataio, adaerror,
1116 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1117 if (error != 0)
1118 printf("Aborting dump due to I/O error.\n");
1119
1120 return (error);
1121 }
1122
1123 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) {
1124 xpt_setup_ccb(&ataio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1125
1126 /*
1127 * Tell the drive to flush its internal cache. if we
1128 * can't flush in 5s we have big problems. No need to
1129 * wait the default 60s to detect problems.
1130 */
1131 ataio.ccb_h.ccb_state = ADA_CCB_DUMP;
1132 cam_fill_ataio(&ataio,
1133 0,
1134 NULL,
1135 CAM_DIR_NONE,
1136 0,
1137 NULL,
1138 0,
1139 5*1000);
1140
1141 if (softc->flags & ADA_FLAG_CAN_48BIT)
1142 ata_48bit_cmd(&ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1143 else
1144 ata_28bit_cmd(&ataio, ATA_FLUSHCACHE, 0, 0, 0);
1145 error = cam_periph_runccb((union ccb *)&ataio, adaerror,
1146 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1147 if (error != 0)
1148 xpt_print(periph->path, "Synchronize cache failed\n");
1149 }
1150 return (error);
1151 }
1152
1153 static void
1154 adainit(void)
1155 {
1156 cam_status status;
1157
1158 /*
1159 * Install a global async callback. This callback will
1160 * receive async callbacks like "new device found".
1161 */
1162 status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL);
1163
1164 if (status != CAM_REQ_CMP) {
1165 printf("ada: Failed to attach master async callback "
1166 "due to status 0x%x!\n", status);
1167 } else if (ada_send_ordered) {
1168
1169 /* Register our event handlers */
1170 if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend,
1171 NULL, EVENTHANDLER_PRI_LAST)) == NULL)
1172 printf("adainit: power event registration failed!\n");
1173 if ((EVENTHANDLER_REGISTER(power_resume, adaresume,
1174 NULL, EVENTHANDLER_PRI_LAST)) == NULL)
1175 printf("adainit: power event registration failed!\n");
1176 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown,
1177 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1178 printf("adainit: shutdown event registration failed!\n");
1179 }
1180 }
1181
1182 /*
1183 * Callback from GEOM, called when it has finished cleaning up its
1184 * resources.
1185 */
1186 static void
1187 adadiskgonecb(struct disk *dp)
1188 {
1189 struct cam_periph *periph;
1190
1191 periph = (struct cam_periph *)dp->d_drv1;
1192
1193 cam_periph_release(periph);
1194 }
1195
1196 static void
1197 adaoninvalidate(struct cam_periph *periph)
1198 {
1199 struct ada_softc *softc;
1200
1201 softc = (struct ada_softc *)periph->softc;
1202
1203 /*
1204 * De-register any async callbacks.
1205 */
1206 xpt_register_async(0, adaasync, periph, periph->path);
1207 #ifdef CAM_IO_STATS
1208 softc->invalidations++;
1209 #endif
1210
1211 /*
1212 * Return all queued I/O with ENXIO.
1213 * XXX Handle any transactions queued to the card
1214 * with XPT_ABORT_CCB.
1215 */
1216 cam_iosched_flush(softc->cam_iosched, NULL, ENXIO);
1217
1218 disk_gone(softc->disk);
1219 }
1220
1221 static void
1222 adacleanup(struct cam_periph *periph)
1223 {
1224 struct ada_softc *softc;
1225
1226 softc = (struct ada_softc *)periph->softc;
1227
1228 cam_periph_unlock(periph);
1229
1230 cam_iosched_fini(softc->cam_iosched);
1231
1232 /*
1233 * If we can't free the sysctl tree, oh well...
1234 */
1235 if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0) {
1236 #ifdef CAM_IO_STATS
1237 if (sysctl_ctx_free(&softc->sysctl_stats_ctx) != 0)
1238 xpt_print(periph->path,
1239 "can't remove sysctl stats context\n");
1240 #endif
1241 if (sysctl_ctx_free(&softc->sysctl_ctx) != 0)
1242 xpt_print(periph->path,
1243 "can't remove sysctl context\n");
1244 }
1245
1246 disk_destroy(softc->disk);
1247 callout_drain(&softc->sendordered_c);
1248 free(softc, M_DEVBUF);
1249 cam_periph_lock(periph);
1250 }
1251
1252 static void
1253 adasetdeletemethod(struct ada_softc *softc)
1254 {
1255
1256 if (softc->flags & ADA_FLAG_CAN_NCQ_TRIM)
1257 softc->delete_method = ADA_DELETE_NCQ_DSM_TRIM;
1258 else if (softc->flags & ADA_FLAG_CAN_TRIM)
1259 softc->delete_method = ADA_DELETE_DSM_TRIM;
1260 else if ((softc->flags & ADA_FLAG_CAN_CFA) && !(softc->flags & ADA_FLAG_CAN_48BIT))
1261 softc->delete_method = ADA_DELETE_CFA_ERASE;
1262 else
1263 softc->delete_method = ADA_DELETE_NONE;
1264 }
1265
1266 static void
1267 adaasync(void *callback_arg, u_int32_t code,
1268 struct cam_path *path, void *arg)
1269 {
1270 struct ccb_getdev cgd;
1271 struct cam_periph *periph;
1272 struct ada_softc *softc;
1273
1274 periph = (struct cam_periph *)callback_arg;
1275 switch (code) {
1276 case AC_FOUND_DEVICE:
1277 {
1278 struct ccb_getdev *cgd;
1279 cam_status status;
1280
1281 cgd = (struct ccb_getdev *)arg;
1282 if (cgd == NULL)
1283 break;
1284
1285 if (cgd->protocol != PROTO_ATA)
1286 break;
1287
1288 /*
1289 * Allocate a peripheral instance for
1290 * this device and start the probe
1291 * process.
1292 */
1293 status = cam_periph_alloc(adaregister, adaoninvalidate,
1294 adacleanup, adastart,
1295 "ada", CAM_PERIPH_BIO,
1296 path, adaasync,
1297 AC_FOUND_DEVICE, cgd);
1298
1299 if (status != CAM_REQ_CMP
1300 && status != CAM_REQ_INPROG)
1301 printf("adaasync: Unable to attach to new device "
1302 "due to status 0x%x\n", status);
1303 break;
1304 }
1305 case AC_GETDEV_CHANGED:
1306 {
1307 softc = (struct ada_softc *)periph->softc;
1308 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1309 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1310 xpt_action((union ccb *)&cgd);
1311
1312 /*
1313 * Update our information based on the new Identify data.
1314 */
1315 adasetflags(softc, &cgd);
1316 adasetgeom(softc, &cgd);
1317 disk_resize(softc->disk, M_NOWAIT);
1318
1319 cam_periph_async(periph, code, path, arg);
1320 break;
1321 }
1322 case AC_ADVINFO_CHANGED:
1323 {
1324 uintptr_t buftype;
1325
1326 buftype = (uintptr_t)arg;
1327 if (buftype == CDAI_TYPE_PHYS_PATH) {
1328 struct ada_softc *softc;
1329
1330 softc = periph->softc;
1331 disk_attr_changed(softc->disk, "GEOM::physpath",
1332 M_NOWAIT);
1333 }
1334 break;
1335 }
1336 case AC_SENT_BDR:
1337 case AC_BUS_RESET:
1338 {
1339 softc = (struct ada_softc *)periph->softc;
1340 cam_periph_async(periph, code, path, arg);
1341 if (softc->state != ADA_STATE_NORMAL)
1342 break;
1343 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1344 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1345 xpt_action((union ccb *)&cgd);
1346 if (ADA_RA >= 0 && softc->flags & ADA_FLAG_CAN_RAHEAD)
1347 softc->state = ADA_STATE_RAHEAD;
1348 else if (ADA_WC >= 0 && softc->flags & ADA_FLAG_CAN_WCACHE)
1349 softc->state = ADA_STATE_WCACHE;
1350 else if ((softc->flags & ADA_FLAG_CAN_LOG)
1351 && (softc->zone_mode != ADA_ZONE_NONE))
1352 softc->state = ADA_STATE_LOGDIR;
1353 else
1354 break;
1355 if (cam_periph_acquire(periph) != 0)
1356 softc->state = ADA_STATE_NORMAL;
1357 else
1358 xpt_schedule(periph, CAM_PRIORITY_DEV);
1359 }
1360 default:
1361 cam_periph_async(periph, code, path, arg);
1362 break;
1363 }
1364 }
1365
1366 static int
1367 adazonemodesysctl(SYSCTL_HANDLER_ARGS)
1368 {
1369 char tmpbuf[40];
1370 struct ada_softc *softc;
1371 int error;
1372
1373 softc = (struct ada_softc *)arg1;
1374
1375 switch (softc->zone_mode) {
1376 case ADA_ZONE_DRIVE_MANAGED:
1377 snprintf(tmpbuf, sizeof(tmpbuf), "Drive Managed");
1378 break;
1379 case ADA_ZONE_HOST_AWARE:
1380 snprintf(tmpbuf, sizeof(tmpbuf), "Host Aware");
1381 break;
1382 case ADA_ZONE_HOST_MANAGED:
1383 snprintf(tmpbuf, sizeof(tmpbuf), "Host Managed");
1384 break;
1385 case ADA_ZONE_NONE:
1386 default:
1387 snprintf(tmpbuf, sizeof(tmpbuf), "Not Zoned");
1388 break;
1389 }
1390
1391 error = sysctl_handle_string(oidp, tmpbuf, sizeof(tmpbuf), req);
1392
1393 return (error);
1394 }
1395
1396 static int
1397 adazonesupsysctl(SYSCTL_HANDLER_ARGS)
1398 {
1399 char tmpbuf[180];
1400 struct ada_softc *softc;
1401 struct sbuf sb;
1402 int error, first;
1403 unsigned int i;
1404
1405 softc = (struct ada_softc *)arg1;
1406
1407 error = 0;
1408 first = 1;
1409 sbuf_new(&sb, tmpbuf, sizeof(tmpbuf), 0);
1410
1411 for (i = 0; i < sizeof(ada_zone_desc_table) /
1412 sizeof(ada_zone_desc_table[0]); i++) {
1413 if (softc->zone_flags & ada_zone_desc_table[i].value) {
1414 if (first == 0)
1415 sbuf_printf(&sb, ", ");
1416 else
1417 first = 0;
1418 sbuf_cat(&sb, ada_zone_desc_table[i].desc);
1419 }
1420 }
1421
1422 if (first == 1)
1423 sbuf_printf(&sb, "None");
1424
1425 sbuf_finish(&sb);
1426
1427 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
1428
1429 return (error);
1430 }
1431
1432
1433 static void
1434 adasysctlinit(void *context, int pending)
1435 {
1436 struct cam_periph *periph;
1437 struct ada_softc *softc;
1438 char tmpstr[32], tmpstr2[16];
1439
1440 periph = (struct cam_periph *)context;
1441
1442 /* periph was held for us when this task was enqueued */
1443 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
1444 cam_periph_release(periph);
1445 return;
1446 }
1447
1448 softc = (struct ada_softc *)periph->softc;
1449 snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d",periph->unit_number);
1450 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1451
1452 sysctl_ctx_init(&softc->sysctl_ctx);
1453 softc->flags |= ADA_FLAG_SCTX_INIT;
1454 softc->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&softc->sysctl_ctx,
1455 SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2,
1456 CTLFLAG_RD, 0, tmpstr, "device_index");
1457 if (softc->sysctl_tree == NULL) {
1458 printf("adasysctlinit: unable to allocate sysctl tree\n");
1459 cam_periph_release(periph);
1460 return;
1461 }
1462
1463 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1464 OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RW,
1465 softc, 0, adadeletemethodsysctl, "A",
1466 "BIO_DELETE execution method");
1467 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1468 OID_AUTO, "read_ahead", CTLFLAG_RW | CTLFLAG_MPSAFE,
1469 &softc->read_ahead, 0, "Enable disk read ahead.");
1470 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1471 OID_AUTO, "write_cache", CTLFLAG_RW | CTLFLAG_MPSAFE,
1472 &softc->write_cache, 0, "Enable disk write cache.");
1473 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1474 OID_AUTO, "unmapped_io", CTLFLAG_RD | CTLFLAG_MPSAFE,
1475 &softc->unmappedio, 0, "Unmapped I/O leaf");
1476 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1477 OID_AUTO, "rotating", CTLFLAG_RD | CTLFLAG_MPSAFE,
1478 &softc->rotating, 0, "Rotating media");
1479 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1480 OID_AUTO, "zone_mode", CTLTYPE_STRING | CTLFLAG_RD,
1481 softc, 0, adazonemodesysctl, "A",
1482 "Zone Mode");
1483 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1484 OID_AUTO, "zone_support", CTLTYPE_STRING | CTLFLAG_RD,
1485 softc, 0, adazonesupsysctl, "A",
1486 "Zone Support");
1487 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1488 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
1489 "optimal_seq_zones", CTLFLAG_RD, &softc->optimal_seq_zones,
1490 "Optimal Number of Open Sequential Write Preferred Zones");
1491 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1492 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
1493 "optimal_nonseq_zones", CTLFLAG_RD,
1494 &softc->optimal_nonseq_zones,
1495 "Optimal Number of Non-Sequentially Written Sequential Write "
1496 "Preferred Zones");
1497 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1498 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
1499 "max_seq_zones", CTLFLAG_RD, &softc->max_seq_zones,
1500 "Maximum Number of Open Sequential Write Required Zones");
1501
1502 #ifdef CAM_TEST_FAILURE
1503 /*
1504 * Add a 'door bell' sysctl which allows one to set it from userland
1505 * and cause something bad to happen. For the moment, we only allow
1506 * whacking the next read or write.
1507 */
1508 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1509 OID_AUTO, "force_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1510 &softc->force_read_error, 0,
1511 "Force a read error for the next N reads.");
1512 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1513 OID_AUTO, "force_write_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1514 &softc->force_write_error, 0,
1515 "Force a write error for the next N writes.");
1516 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1517 OID_AUTO, "periodic_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1518 &softc->periodic_read_error, 0,
1519 "Force a read error every N reads (don't set too low).");
1520 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1521 OID_AUTO, "invalidate", CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE,
1522 periph, 0, cam_periph_invalidate_sysctl, "I",
1523 "Write 1 to invalidate the drive immediately");
1524 #endif
1525
1526 #ifdef CAM_IO_STATS
1527 softc->sysctl_stats_tree = SYSCTL_ADD_NODE(&softc->sysctl_stats_ctx,
1528 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, "stats",
1529 CTLFLAG_RD, 0, "Statistics");
1530 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
1531 SYSCTL_CHILDREN(softc->sysctl_stats_tree),
1532 OID_AUTO, "timeouts", CTLFLAG_RD | CTLFLAG_MPSAFE,
1533 &softc->timeouts, 0,
1534 "Device timeouts reported by the SIM");
1535 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
1536 SYSCTL_CHILDREN(softc->sysctl_stats_tree),
1537 OID_AUTO, "errors", CTLFLAG_RD | CTLFLAG_MPSAFE,
1538 &softc->errors, 0,
1539 "Transport errors reported by the SIM.");
1540 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
1541 SYSCTL_CHILDREN(softc->sysctl_stats_tree),
1542 OID_AUTO, "pack_invalidations", CTLFLAG_RD | CTLFLAG_MPSAFE,
1543 &softc->invalidations, 0,
1544 "Device pack invalidations.");
1545 #endif
1546
1547 cam_iosched_sysctl_init(softc->cam_iosched, &softc->sysctl_ctx,
1548 softc->sysctl_tree);
1549
1550 cam_periph_release(periph);
1551 }
1552
1553 static int
1554 adagetattr(struct bio *bp)
1555 {
1556 int ret;
1557 struct cam_periph *periph;
1558
1559 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1560 cam_periph_lock(periph);
1561 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1562 periph->path);
1563 cam_periph_unlock(periph);
1564 if (ret == 0)
1565 bp->bio_completed = bp->bio_length;
1566 return ret;
1567 }
1568
1569 static int
1570 adadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
1571 {
1572 char buf[16];
1573 const char *p;
1574 struct ada_softc *softc;
1575 int i, error, value, methods;
1576
1577 softc = (struct ada_softc *)arg1;
1578
1579 value = softc->delete_method;
1580 if (value < 0 || value > ADA_DELETE_MAX)
1581 p = "UNKNOWN";
1582 else
1583 p = ada_delete_method_names[value];
1584 strncpy(buf, p, sizeof(buf));
1585 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1586 if (error != 0 || req->newptr == NULL)
1587 return (error);
1588 methods = 1 << ADA_DELETE_DISABLE;
1589 if ((softc->flags & ADA_FLAG_CAN_CFA) &&
1590 !(softc->flags & ADA_FLAG_CAN_48BIT))
1591 methods |= 1 << ADA_DELETE_CFA_ERASE;
1592 if (softc->flags & ADA_FLAG_CAN_TRIM)
1593 methods |= 1 << ADA_DELETE_DSM_TRIM;
1594 if (softc->flags & ADA_FLAG_CAN_NCQ_TRIM)
1595 methods |= 1 << ADA_DELETE_NCQ_DSM_TRIM;
1596 for (i = 0; i <= ADA_DELETE_MAX; i++) {
1597 if (!(methods & (1 << i)) ||
1598 strcmp(buf, ada_delete_method_names[i]) != 0)
1599 continue;
1600 softc->delete_method = i;
1601 return (0);
1602 }
1603 return (EINVAL);
1604 }
1605
1606 static void
1607 adasetflags(struct ada_softc *softc, struct ccb_getdev *cgd)
1608 {
1609 if ((cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA) &&
1610 (cgd->inq_flags & SID_DMA))
1611 softc->flags |= ADA_FLAG_CAN_DMA;
1612 else
1613 softc->flags &= ~ADA_FLAG_CAN_DMA;
1614
1615 if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) {
1616 softc->flags |= ADA_FLAG_CAN_48BIT;
1617 if (cgd->inq_flags & SID_DMA48)
1618 softc->flags |= ADA_FLAG_CAN_DMA48;
1619 else
1620 softc->flags &= ~ADA_FLAG_CAN_DMA48;
1621 } else
1622 softc->flags &= ~(ADA_FLAG_CAN_48BIT | ADA_FLAG_CAN_DMA48);
1623
1624 if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE)
1625 softc->flags |= ADA_FLAG_CAN_FLUSHCACHE;
1626 else
1627 softc->flags &= ~ADA_FLAG_CAN_FLUSHCACHE;
1628
1629 if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT)
1630 softc->flags |= ADA_FLAG_CAN_POWERMGT;
1631 else
1632 softc->flags &= ~ADA_FLAG_CAN_POWERMGT;
1633
1634 if ((cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ) &&
1635 (cgd->inq_flags & SID_DMA) && (cgd->inq_flags & SID_CmdQue))
1636 softc->flags |= ADA_FLAG_CAN_NCQ;
1637 else
1638 softc->flags &= ~ADA_FLAG_CAN_NCQ;
1639
1640 if ((cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) &&
1641 (cgd->inq_flags & SID_DMA) &&
1642 (softc->quirks & ADA_Q_NO_TRIM) == 0) {
1643 softc->flags |= ADA_FLAG_CAN_TRIM;
1644 softc->trim_max_ranges = TRIM_MAX_RANGES;
1645 if (cgd->ident_data.max_dsm_blocks != 0) {
1646 softc->trim_max_ranges =
1647 min(cgd->ident_data.max_dsm_blocks *
1648 ATA_DSM_BLK_RANGES, softc->trim_max_ranges);
1649 }
1650 /*
1651 * If we can do RCVSND_FPDMA_QUEUED commands, we may be able
1652 * to do NCQ trims, if we support trims at all. We also need
1653 * support from the SIM to do things properly. Perhaps we
1654 * should look at log 13 dword 0 bit 0 and dword 1 bit 0 are
1655 * set too...
1656 */
1657 if ((softc->quirks & ADA_Q_NCQ_TRIM_BROKEN) == 0 &&
1658 (softc->flags & ADA_FLAG_PIM_ATA_EXT) != 0 &&
1659 (cgd->ident_data.satacapabilities2 &
1660 ATA_SUPPORT_RCVSND_FPDMA_QUEUED) != 0 &&
1661 (softc->flags & ADA_FLAG_CAN_TRIM) != 0)
1662 softc->flags |= ADA_FLAG_CAN_NCQ_TRIM;
1663 else
1664 softc->flags &= ~ADA_FLAG_CAN_NCQ_TRIM;
1665 } else
1666 softc->flags &= ~(ADA_FLAG_CAN_TRIM | ADA_FLAG_CAN_NCQ_TRIM);
1667
1668 if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA)
1669 softc->flags |= ADA_FLAG_CAN_CFA;
1670 else
1671 softc->flags &= ~ADA_FLAG_CAN_CFA;
1672
1673 /*
1674 * Now that we've set the appropriate flags, setup the delete
1675 * method.
1676 */
1677 adasetdeletemethod(softc);
1678
1679 if ((cgd->ident_data.support.extension & ATA_SUPPORT_GENLOG)
1680 && ((softc->quirks & ADA_Q_LOG_BROKEN) == 0))
1681 softc->flags |= ADA_FLAG_CAN_LOG;
1682 else
1683 softc->flags &= ~ADA_FLAG_CAN_LOG;
1684
1685 if ((cgd->ident_data.support3 & ATA_SUPPORT_ZONE_MASK) ==
1686 ATA_SUPPORT_ZONE_HOST_AWARE)
1687 softc->zone_mode = ADA_ZONE_HOST_AWARE;
1688 else if (((cgd->ident_data.support3 & ATA_SUPPORT_ZONE_MASK) ==
1689 ATA_SUPPORT_ZONE_DEV_MANAGED)
1690 || (softc->quirks & ADA_Q_SMR_DM))
1691 softc->zone_mode = ADA_ZONE_DRIVE_MANAGED;
1692 else
1693 softc->zone_mode = ADA_ZONE_NONE;
1694
1695 if (cgd->ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD)
1696 softc->flags |= ADA_FLAG_CAN_RAHEAD;
1697 else
1698 softc->flags &= ~ADA_FLAG_CAN_RAHEAD;
1699
1700 if (cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE)
1701 softc->flags |= ADA_FLAG_CAN_WCACHE;
1702 else
1703 softc->flags &= ~ADA_FLAG_CAN_WCACHE;
1704 }
1705
1706 static cam_status
1707 adaregister(struct cam_periph *periph, void *arg)
1708 {
1709 struct ada_softc *softc;
1710 struct ccb_getdev *cgd;
1711 struct disk_params *dp;
1712 struct sbuf sb;
1713 char *announce_buf;
1714 caddr_t match;
1715 int quirks;
1716
1717 cgd = (struct ccb_getdev *)arg;
1718 if (cgd == NULL) {
1719 printf("adaregister: no getdev CCB, can't register device\n");
1720 return(CAM_REQ_CMP_ERR);
1721 }
1722
1723 softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF,
1724 M_NOWAIT|M_ZERO);
1725
1726 if (softc == NULL) {
1727 printf("adaregister: Unable to probe new device. "
1728 "Unable to allocate softc\n");
1729 return(CAM_REQ_CMP_ERR);
1730 }
1731
1732 announce_buf = softc->announce_temp;
1733 bzero(announce_buf, ADA_ANNOUNCETMP_SZ);
1734
1735 if (cam_iosched_init(&softc->cam_iosched, periph) != 0) {
1736 printf("adaregister: Unable to probe new device. "
1737 "Unable to allocate iosched memory\n");
1738 free(softc, M_DEVBUF);
1739 return(CAM_REQ_CMP_ERR);
1740 }
1741
1742 periph->softc = softc;
1743 xpt_path_inq(&softc->cpi, periph->path);
1744
1745 /*
1746 * See if this device has any quirks.
1747 */
1748 match = cam_quirkmatch((caddr_t)&cgd->ident_data,
1749 (caddr_t)ada_quirk_table,
1750 nitems(ada_quirk_table),
1751 sizeof(*ada_quirk_table), ata_identify_match);
1752 if (match != NULL)
1753 softc->quirks = ((struct ada_quirk_entry *)match)->quirks;
1754 else
1755 softc->quirks = ADA_Q_NONE;
1756
1757 TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph);
1758
1759 /*
1760 * Register this media as a disk
1761 */
1762 (void)cam_periph_hold(periph, PRIBIO);
1763 cam_periph_unlock(periph);
1764 snprintf(announce_buf, ADA_ANNOUNCETMP_SZ,
1765 "kern.cam.ada.%d.quirks", periph->unit_number);
1766 quirks = softc->quirks;
1767 TUNABLE_INT_FETCH(announce_buf, &quirks);
1768 softc->quirks = quirks;
1769 softc->read_ahead = -1;
1770 snprintf(announce_buf, ADA_ANNOUNCETMP_SZ,
1771 "kern.cam.ada.%d.read_ahead", periph->unit_number);
1772 TUNABLE_INT_FETCH(announce_buf, &softc->read_ahead);
1773 softc->write_cache = -1;
1774 snprintf(announce_buf, ADA_ANNOUNCETMP_SZ,
1775 "kern.cam.ada.%d.write_cache", periph->unit_number);
1776 TUNABLE_INT_FETCH(announce_buf, &softc->write_cache);
1777
1778 /*
1779 * Set support flags based on the Identify data and quirks.
1780 */
1781 adasetflags(softc, cgd);
1782 if (softc->cpi.hba_misc & PIM_ATA_EXT)
1783 softc->flags |= ADA_FLAG_PIM_ATA_EXT;
1784
1785 /* Disable queue sorting for non-rotational media by default. */
1786 if (cgd->ident_data.media_rotation_rate == ATA_RATE_NON_ROTATING) {
1787 softc->rotating = 0;
1788 } else {
1789 softc->rotating = 1;
1790 }
1791 cam_iosched_set_sort_queue(softc->cam_iosched, softc->rotating ? -1 : 0);
1792 softc->disk = disk_alloc();
1793 adasetgeom(softc, cgd);
1794 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
1795 periph->unit_number, softc->params.secsize,
1796 DEVSTAT_ALL_SUPPORTED,
1797 DEVSTAT_TYPE_DIRECT |
1798 XPORT_DEVSTAT_TYPE(softc->cpi.transport),
1799 DEVSTAT_PRIORITY_DISK);
1800 softc->disk->d_open = adaopen;
1801 softc->disk->d_close = adaclose;
1802 softc->disk->d_strategy = adastrategy;
1803 softc->disk->d_getattr = adagetattr;
1804 softc->disk->d_dump = adadump;
1805 softc->disk->d_gone = adadiskgonecb;
1806 softc->disk->d_name = "ada";
1807 softc->disk->d_drv1 = periph;
1808 softc->disk->d_unit = periph->unit_number;
1809
1810 /*
1811 * Acquire a reference to the periph before we register with GEOM.
1812 * We'll release this reference once GEOM calls us back (via
1813 * adadiskgonecb()) telling us that our provider has been freed.
1814 */
1815 if (cam_periph_acquire(periph) != 0) {
1816 xpt_print(periph->path, "%s: lost periph during "
1817 "registration!\n", __func__);
1818 cam_periph_lock(periph);
1819 return (CAM_REQ_CMP_ERR);
1820 }
1821 disk_create(softc->disk, DISK_VERSION);
1822 cam_periph_lock(periph);
1823
1824 dp = &softc->params;
1825 snprintf(announce_buf, ADA_ANNOUNCETMP_SZ,
1826 "%juMB (%ju %u byte sectors)",
1827 ((uintmax_t)dp->secsize * dp->sectors) / (1024 * 1024),
1828 (uintmax_t)dp->sectors, dp->secsize);
1829
1830 sbuf_new(&sb, softc->announce_buffer, ADA_ANNOUNCE_SZ, SBUF_FIXEDLEN);
1831 xpt_announce_periph_sbuf(periph, &sb, announce_buf);
1832 xpt_announce_quirks_sbuf(periph, &sb, softc->quirks, ADA_Q_BIT_STRING);
1833 sbuf_finish(&sb);
1834 sbuf_putbuf(&sb);
1835
1836 /*
1837 * Create our sysctl variables, now that we know
1838 * we have successfully attached.
1839 */
1840 if (cam_periph_acquire(periph) == 0)
1841 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task);
1842
1843 /*
1844 * Add async callbacks for bus reset and
1845 * bus device reset calls. I don't bother
1846 * checking if this fails as, in most cases,
1847 * the system will function just fine without
1848 * them and the only alternative would be to
1849 * not attach the device on failure.
1850 */
1851 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
1852 AC_GETDEV_CHANGED | AC_ADVINFO_CHANGED,
1853 adaasync, periph, periph->path);
1854
1855 /*
1856 * Schedule a periodic event to occasionally send an
1857 * ordered tag to a device.
1858 */
1859 callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0);
1860 callout_reset(&softc->sendordered_c,
1861 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL,
1862 adasendorderedtag, softc);
1863
1864 if (ADA_RA >= 0 && softc->flags & ADA_FLAG_CAN_RAHEAD) {
1865 softc->state = ADA_STATE_RAHEAD;
1866 } else if (ADA_WC >= 0 && softc->flags & ADA_FLAG_CAN_WCACHE) {
1867 softc->state = ADA_STATE_WCACHE;
1868 } else if ((softc->flags & ADA_FLAG_CAN_LOG)
1869 && (softc->zone_mode != ADA_ZONE_NONE)) {
1870 softc->state = ADA_STATE_LOGDIR;
1871 } else {
1872 /*
1873 * Nothing to probe, so we can just transition to the
1874 * normal state.
1875 */
1876 adaprobedone(periph, NULL);
1877 return(CAM_REQ_CMP);
1878 }
1879
1880 xpt_schedule(periph, CAM_PRIORITY_DEV);
1881
1882 return(CAM_REQ_CMP);
1883 }
1884
1885 static int
1886 ada_dsmtrim_req_create(struct ada_softc *softc, struct bio *bp, struct trim_request *req)
1887 {
1888 uint64_t lastlba = (uint64_t)-1;
1889 int c, lastcount = 0, off, ranges = 0;
1890
1891 bzero(req, sizeof(*req));
1892 TAILQ_INIT(&req->bps);
1893 do {
1894 uint64_t lba = bp->bio_pblkno;
1895 int count = bp->bio_bcount / softc->params.secsize;
1896
1897 /* Try to extend the previous range. */
1898 if (lba == lastlba) {
1899 c = min(count, ATA_DSM_RANGE_MAX - lastcount);
1900 lastcount += c;
1901 off = (ranges - 1) * ATA_DSM_RANGE_SIZE;
1902 req->data[off + 6] = lastcount & 0xff;
1903 req->data[off + 7] =
1904 (lastcount >> 8) & 0xff;
1905 count -= c;
1906 lba += c;
1907 }
1908
1909 while (count > 0) {
1910 c = min(count, ATA_DSM_RANGE_MAX);
1911 off = ranges * ATA_DSM_RANGE_SIZE;
1912 req->data[off + 0] = lba & 0xff;
1913 req->data[off + 1] = (lba >> 8) & 0xff;
1914 req->data[off + 2] = (lba >> 16) & 0xff;
1915 req->data[off + 3] = (lba >> 24) & 0xff;
1916 req->data[off + 4] = (lba >> 32) & 0xff;
1917 req->data[off + 5] = (lba >> 40) & 0xff;
1918 req->data[off + 6] = c & 0xff;
1919 req->data[off + 7] = (c >> 8) & 0xff;
1920 lba += c;
1921 count -= c;
1922 lastcount = c;
1923 ranges++;
1924 /*
1925 * Its the caller's responsibility to ensure the
1926 * request will fit so we don't need to check for
1927 * overrun here
1928 */
1929 }
1930 lastlba = lba;
1931 TAILQ_INSERT_TAIL(&req->bps, bp, bio_queue);
1932
1933 bp = cam_iosched_next_trim(softc->cam_iosched);
1934 if (bp == NULL)
1935 break;
1936 if (bp->bio_bcount / softc->params.secsize >
1937 (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX) {
1938 cam_iosched_put_back_trim(softc->cam_iosched, bp);
1939 break;
1940 }
1941 } while (1);
1942
1943 return (ranges);
1944 }
1945
1946 static void
1947 ada_dsmtrim(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio)
1948 {
1949 struct trim_request *req = &softc->trim_req;
1950 int ranges;
1951
1952 ranges = ada_dsmtrim_req_create(softc, bp, req);
1953 cam_fill_ataio(ataio,
1954 ada_retry_count,
1955 adadone,
1956 CAM_DIR_OUT,
1957 0,
1958 req->data,
1959 howmany(ranges, ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE,
1960 ada_default_timeout * 1000);
1961 ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT,
1962 ATA_DSM_TRIM, 0, howmany(ranges, ATA_DSM_BLK_RANGES));
1963 }
1964
1965 static void
1966 ada_ncq_dsmtrim(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio)
1967 {
1968 struct trim_request *req = &softc->trim_req;
1969 int ranges;
1970
1971 ranges = ada_dsmtrim_req_create(softc, bp, req);
1972 cam_fill_ataio(ataio,
1973 ada_retry_count,
1974 adadone,
1975 CAM_DIR_OUT,
1976 0,
1977 req->data,
1978 howmany(ranges, ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE,
1979 ada_default_timeout * 1000);
1980 ata_ncq_cmd(ataio,
1981 ATA_SEND_FPDMA_QUEUED,
1982 0,
1983 howmany(ranges, ATA_DSM_BLK_RANGES));
1984 ataio->cmd.sector_count_exp = ATA_SFPDMA_DSM;
1985 ataio->ata_flags |= ATA_FLAG_AUX;
1986 ataio->aux = 1;
1987 }
1988
1989 static void
1990 ada_cfaerase(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio)
1991 {
1992 struct trim_request *req = &softc->trim_req;
1993 uint64_t lba = bp->bio_pblkno;
1994 uint16_t count = bp->bio_bcount / softc->params.secsize;
1995
1996 bzero(req, sizeof(*req));
1997 TAILQ_INIT(&req->bps);
1998 TAILQ_INSERT_TAIL(&req->bps, bp, bio_queue);
1999
2000 cam_fill_ataio(ataio,
2001 ada_retry_count,
2002 adadone,
2003 CAM_DIR_NONE,
2004 0,
2005 NULL,
2006 0,
2007 ada_default_timeout*1000);
2008
2009 if (count >= 256)
2010 count = 0;
2011 ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count);
2012 }
2013
2014 static int
2015 ada_zone_bio_to_ata(int disk_zone_cmd)
2016 {
2017 switch (disk_zone_cmd) {
2018 case DISK_ZONE_OPEN:
2019 return ATA_ZM_OPEN_ZONE;
2020 case DISK_ZONE_CLOSE:
2021 return ATA_ZM_CLOSE_ZONE;
2022 case DISK_ZONE_FINISH:
2023 return ATA_ZM_FINISH_ZONE;
2024 case DISK_ZONE_RWP:
2025 return ATA_ZM_RWP;
2026 }
2027
2028 return -1;
2029 }
2030
2031 static int
2032 ada_zone_cmd(struct cam_periph *periph, union ccb *ccb, struct bio *bp,
2033 int *queue_ccb)
2034 {
2035 struct ada_softc *softc;
2036 int error;
2037
2038 error = 0;
2039
2040 if (bp->bio_cmd != BIO_ZONE) {
2041 error = EINVAL;
2042 goto bailout;
2043 }
2044
2045 softc = periph->softc;
2046
2047 switch (bp->bio_zone.zone_cmd) {
2048 case DISK_ZONE_OPEN:
2049 case DISK_ZONE_CLOSE:
2050 case DISK_ZONE_FINISH:
2051 case DISK_ZONE_RWP: {
2052 int zone_flags;
2053 int zone_sa;
2054 uint64_t lba;
2055
2056 zone_sa = ada_zone_bio_to_ata(bp->bio_zone.zone_cmd);
2057 if (zone_sa == -1) {
2058 xpt_print(periph->path, "Cannot translate zone "
2059 "cmd %#x to ATA\n", bp->bio_zone.zone_cmd);
2060 error = EINVAL;
2061 goto bailout;
2062 }
2063
2064 zone_flags = 0;
2065 lba = bp->bio_zone.zone_params.rwp.id;
2066
2067 if (bp->bio_zone.zone_params.rwp.flags &
2068 DISK_ZONE_RWP_FLAG_ALL)
2069 zone_flags |= ZBC_OUT_ALL;
2070
2071 ata_zac_mgmt_out(&ccb->ataio,
2072 /*retries*/ ada_retry_count,
2073 /*cbfcnp*/ adadone,
2074 /*use_ncq*/ (softc->flags &
2075 ADA_FLAG_PIM_ATA_EXT) ? 1 : 0,
2076 /*zm_action*/ zone_sa,
2077 /*zone_id*/ lba,
2078 /*zone_flags*/ zone_flags,
2079 /*sector_count*/ 0,
2080 /*data_ptr*/ NULL,
2081 /*dxfer_len*/ 0,
2082 /*timeout*/ ada_default_timeout * 1000);
2083 *queue_ccb = 1;
2084
2085 break;
2086 }
2087 case DISK_ZONE_REPORT_ZONES: {
2088 uint8_t *rz_ptr;
2089 uint32_t num_entries, alloc_size;
2090 struct disk_zone_report *rep;
2091
2092 rep = &bp->bio_zone.zone_params.report;
2093
2094 num_entries = rep->entries_allocated;
2095 if (num_entries == 0) {
2096 xpt_print(periph->path, "No entries allocated for "
2097 "Report Zones request\n");
2098 error = EINVAL;
2099 goto bailout;
2100 }
2101 alloc_size = sizeof(struct scsi_report_zones_hdr) +
2102 (sizeof(struct scsi_report_zones_desc) * num_entries);
2103 alloc_size = min(alloc_size, softc->disk->d_maxsize);
2104 rz_ptr = malloc(alloc_size, M_ATADA, M_NOWAIT | M_ZERO);
2105 if (rz_ptr == NULL) {
2106 xpt_print(periph->path, "Unable to allocate memory "
2107 "for Report Zones request\n");
2108 error = ENOMEM;
2109 goto bailout;
2110 }
2111
2112 ata_zac_mgmt_in(&ccb->ataio,
2113 /*retries*/ ada_retry_count,
2114 /*cbcfnp*/ adadone,
2115 /*use_ncq*/ (softc->flags &
2116 ADA_FLAG_PIM_ATA_EXT) ? 1 : 0,
2117 /*zm_action*/ ATA_ZM_REPORT_ZONES,
2118 /*zone_id*/ rep->starting_id,
2119 /*zone_flags*/ rep->rep_options,
2120 /*data_ptr*/ rz_ptr,
2121 /*dxfer_len*/ alloc_size,
2122 /*timeout*/ ada_default_timeout * 1000);
2123
2124 /*
2125 * For BIO_ZONE, this isn't normally needed. However, it
2126 * is used by devstat_end_transaction_bio() to determine
2127 * how much data was transferred.
2128 */
2129 /*
2130 * XXX KDM we have a problem. But I'm not sure how to fix
2131 * it. devstat uses bio_bcount - bio_resid to calculate
2132 * the amount of data transferred. The GEOM disk code
2133 * uses bio_length - bio_resid to calculate the amount of
2134 * data in bio_completed. We have different structure
2135 * sizes above and below the ada(4) driver. So, if we
2136 * use the sizes above, the amount transferred won't be
2137 * quite accurate for devstat. If we use different sizes
2138 * for bio_bcount and bio_length (above and below
2139 * respectively), then the residual needs to match one or
2140 * the other. Everything is calculated after the bio
2141 * leaves the driver, so changing the values around isn't
2142 * really an option. For now, just set the count to the
2143 * passed in length. This means that the calculations
2144 * above (e.g. bio_completed) will be correct, but the
2145 * amount of data reported to devstat will be slightly
2146 * under or overstated.
2147 */
2148 bp->bio_bcount = bp->bio_length;
2149
2150 *queue_ccb = 1;
2151
2152 break;
2153 }
2154 case DISK_ZONE_GET_PARAMS: {
2155 struct disk_zone_disk_params *params;
2156
2157 params = &bp->bio_zone.zone_params.disk_params;
2158 bzero(params, sizeof(*params));
2159
2160 switch (softc->zone_mode) {
2161 case ADA_ZONE_DRIVE_MANAGED:
2162 params->zone_mode = DISK_ZONE_MODE_DRIVE_MANAGED;
2163 break;
2164 case ADA_ZONE_HOST_AWARE:
2165 params->zone_mode = DISK_ZONE_MODE_HOST_AWARE;
2166 break;
2167 case ADA_ZONE_HOST_MANAGED:
2168 params->zone_mode = DISK_ZONE_MODE_HOST_MANAGED;
2169 break;
2170 default:
2171 case ADA_ZONE_NONE:
2172 params->zone_mode = DISK_ZONE_MODE_NONE;
2173 break;
2174 }
2175
2176 if (softc->zone_flags & ADA_ZONE_FLAG_URSWRZ)
2177 params->flags |= DISK_ZONE_DISK_URSWRZ;
2178
2179 if (softc->zone_flags & ADA_ZONE_FLAG_OPT_SEQ_SET) {
2180 params->optimal_seq_zones = softc->optimal_seq_zones;
2181 params->flags |= DISK_ZONE_OPT_SEQ_SET;
2182 }
2183
2184 if (softc->zone_flags & ADA_ZONE_FLAG_OPT_NONSEQ_SET) {
2185 params->optimal_nonseq_zones =
2186 softc->optimal_nonseq_zones;
2187 params->flags |= DISK_ZONE_OPT_NONSEQ_SET;
2188 }
2189
2190 if (softc->zone_flags & ADA_ZONE_FLAG_MAX_SEQ_SET) {
2191 params->max_seq_zones = softc->max_seq_zones;
2192 params->flags |= DISK_ZONE_MAX_SEQ_SET;
2193 }
2194 if (softc->zone_flags & ADA_ZONE_FLAG_RZ_SUP)
2195 params->flags |= DISK_ZONE_RZ_SUP;
2196
2197 if (softc->zone_flags & ADA_ZONE_FLAG_OPEN_SUP)
2198 params->flags |= DISK_ZONE_OPEN_SUP;
2199
2200 if (softc->zone_flags & ADA_ZONE_FLAG_CLOSE_SUP)
2201 params->flags |= DISK_ZONE_CLOSE_SUP;
2202
2203 if (softc->zone_flags & ADA_ZONE_FLAG_FINISH_SUP)
2204 params->flags |= DISK_ZONE_FINISH_SUP;
2205
2206 if (softc->zone_flags & ADA_ZONE_FLAG_RWP_SUP)
2207 params->flags |= DISK_ZONE_RWP_SUP;
2208 break;
2209 }
2210 default:
2211 break;
2212 }
2213 bailout:
2214 return (error);
2215 }
2216
2217 static void
2218 adastart(struct cam_periph *periph, union ccb *start_ccb)
2219 {
2220 struct ada_softc *softc = (struct ada_softc *)periph->softc;
2221 struct ccb_ataio *ataio = &start_ccb->ataio;
2222
2223 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastart\n"));
2224
2225 switch (softc->state) {
2226 case ADA_STATE_NORMAL:
2227 {
2228 struct bio *bp;
2229 u_int8_t tag_code;
2230
2231 bp = cam_iosched_next_bio(softc->cam_iosched);
2232 if (bp == NULL) {
2233 xpt_release_ccb(start_ccb);
2234 break;
2235 }
2236
2237 if ((bp->bio_flags & BIO_ORDERED) != 0 ||
2238 (bp->bio_cmd != BIO_DELETE && (softc->flags & ADA_FLAG_NEED_OTAG) != 0)) {
2239 softc->flags &= ~ADA_FLAG_NEED_OTAG;
2240 softc->flags |= ADA_FLAG_WAS_OTAG;
2241 tag_code = 0;
2242 } else {
2243 tag_code = 1;
2244 }
2245 switch (bp->bio_cmd) {
2246 case BIO_WRITE:
2247 case BIO_READ:
2248 {
2249 uint64_t lba = bp->bio_pblkno;
2250 uint16_t count = bp->bio_bcount / softc->params.secsize;
2251 void *data_ptr;
2252 int rw_op;
2253
2254 if (bp->bio_cmd == BIO_WRITE) {
2255 softc->flags |= ADA_FLAG_DIRTY;
2256 rw_op = CAM_DIR_OUT;
2257 } else {
2258 rw_op = CAM_DIR_IN;
2259 }
2260
2261 data_ptr = bp->bio_data;
2262 if ((bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0) {
2263 rw_op |= CAM_DATA_BIO;
2264 data_ptr = bp;
2265 }
2266
2267 #ifdef CAM_TEST_FAILURE
2268 int fail = 0;
2269
2270 /*
2271 * Support the failure ioctls. If the command is a
2272 * read, and there are pending forced read errors, or
2273 * if a write and pending write errors, then fail this
2274 * operation with EIO. This is useful for testing
2275 * purposes. Also, support having every Nth read fail.
2276 *
2277 * This is a rather blunt tool.
2278 */
2279 if (bp->bio_cmd == BIO_READ) {
2280 if (softc->force_read_error) {
2281 softc->force_read_error--;
2282 fail = 1;
2283 }
2284 if (softc->periodic_read_error > 0) {
2285 if (++softc->periodic_read_count >=
2286 softc->periodic_read_error) {
2287 softc->periodic_read_count = 0;
2288 fail = 1;
2289 }
2290 }
2291 } else {
2292 if (softc->force_write_error) {
2293 softc->force_write_error--;
2294 fail = 1;
2295 }
2296 }
2297 if (fail) {
2298 biofinish(bp, NULL, EIO);
2299 xpt_release_ccb(start_ccb);
2300 adaschedule(periph);
2301 return;
2302 }
2303 #endif
2304 KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 ||
2305 round_page(bp->bio_bcount + bp->bio_ma_offset) /
2306 PAGE_SIZE == bp->bio_ma_n,
2307 ("Short bio %p", bp));
2308 cam_fill_ataio(ataio,
2309 ada_retry_count,
2310 adadone,
2311 rw_op,
2312 0,
2313 data_ptr,
2314 bp->bio_bcount,
2315 ada_default_timeout*1000);
2316
2317 if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) {
2318 if (bp->bio_cmd == BIO_READ) {
2319 ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED,
2320 lba, count);
2321 } else {
2322 ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED,
2323 lba, count);
2324 }
2325 } else if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
2326 (lba + count >= ATA_MAX_28BIT_LBA ||
2327 count > 256)) {
2328 if (softc->flags & ADA_FLAG_CAN_DMA48) {
2329 if (bp->bio_cmd == BIO_READ) {
2330 ata_48bit_cmd(ataio, ATA_READ_DMA48,
2331 0, lba, count);
2332 } else {
2333 ata_48bit_cmd(ataio, ATA_WRITE_DMA48,
2334 0, lba, count);
2335 }
2336 } else {
2337 if (bp->bio_cmd == BIO_READ) {
2338 ata_48bit_cmd(ataio, ATA_READ_MUL48,
2339 0, lba, count);
2340 } else {
2341 ata_48bit_cmd(ataio, ATA_WRITE_MUL48,
2342 0, lba, count);
2343 }
2344 }
2345 } else {
2346 if (count == 256)
2347 count = 0;
2348 if (softc->flags & ADA_FLAG_CAN_DMA) {
2349 if (bp->bio_cmd == BIO_READ) {
2350 ata_28bit_cmd(ataio, ATA_READ_DMA,
2351 0, lba, count);
2352 } else {
2353 ata_28bit_cmd(ataio, ATA_WRITE_DMA,
2354 0, lba, count);
2355 }
2356 } else {
2357 if (bp->bio_cmd == BIO_READ) {
2358 ata_28bit_cmd(ataio, ATA_READ_MUL,
2359 0, lba, count);
2360 } else {
2361 ata_28bit_cmd(ataio, ATA_WRITE_MUL,
2362 0, lba, count);
2363 }
2364 }
2365 }
2366 break;
2367 }
2368 case BIO_DELETE:
2369 switch (softc->delete_method) {
2370 case ADA_DELETE_NCQ_DSM_TRIM:
2371 ada_ncq_dsmtrim(softc, bp, ataio);
2372 break;
2373 case ADA_DELETE_DSM_TRIM:
2374 ada_dsmtrim(softc, bp, ataio);
2375 break;
2376 case ADA_DELETE_CFA_ERASE:
2377 ada_cfaerase(softc, bp, ataio);
2378 break;
2379 default:
2380 biofinish(bp, NULL, EOPNOTSUPP);
2381 xpt_release_ccb(start_ccb);
2382 adaschedule(periph);
2383 return;
2384 }
2385 start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM;
2386 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
2387 cam_iosched_submit_trim(softc->cam_iosched);
2388 goto out;
2389 case BIO_FLUSH:
2390 cam_fill_ataio(ataio,
2391 1,
2392 adadone,
2393 CAM_DIR_NONE,
2394 0,
2395 NULL,
2396 0,
2397 ada_default_timeout*1000);
2398
2399 if (softc->flags & ADA_FLAG_CAN_48BIT)
2400 ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0);
2401 else
2402 ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0);
2403 break;
2404 case BIO_ZONE: {
2405 int error, queue_ccb;
2406
2407 queue_ccb = 0;
2408
2409 error = ada_zone_cmd(periph, start_ccb, bp, &queue_ccb);
2410 if ((error != 0)
2411 || (queue_ccb == 0)) {
2412 biofinish(bp, NULL, error);
2413 xpt_release_ccb(start_ccb);
2414 return;
2415 }
2416 break;
2417 }
2418 }
2419 start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO;
2420 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
2421 out:
2422 start_ccb->ccb_h.ccb_bp = bp;
2423 softc->outstanding_cmds++;
2424 softc->refcount++;
2425 cam_periph_unlock(periph);
2426 xpt_action(start_ccb);
2427 cam_periph_lock(periph);
2428
2429 /* May have more work to do, so ensure we stay scheduled */
2430 adaschedule(periph);
2431 break;
2432 }
2433 case ADA_STATE_RAHEAD:
2434 case ADA_STATE_WCACHE:
2435 {
2436 cam_fill_ataio(ataio,
2437 1,
2438 adadone,
2439 CAM_DIR_NONE,
2440 0,
2441 NULL,
2442 0,
2443 ada_default_timeout*1000);
2444
2445 if (softc->state == ADA_STATE_RAHEAD) {
2446 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_RA ?
2447 ATA_SF_ENAB_RCACHE : ATA_SF_DIS_RCACHE, 0, 0);
2448 start_ccb->ccb_h.ccb_state = ADA_CCB_RAHEAD;
2449 } else {
2450 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_WC ?
2451 ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0);
2452 start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE;
2453 }
2454 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2455 xpt_action(start_ccb);
2456 break;
2457 }
2458 case ADA_STATE_LOGDIR:
2459 {
2460 struct ata_gp_log_dir *log_dir;
2461
2462 if ((softc->flags & ADA_FLAG_CAN_LOG) == 0) {
2463 adaprobedone(periph, start_ccb);
2464 break;
2465 }
2466
2467 log_dir = malloc(sizeof(*log_dir), M_ATADA, M_NOWAIT|M_ZERO);
2468 if (log_dir == NULL) {
2469 xpt_print(periph->path, "Couldn't malloc log_dir "
2470 "data\n");
2471 softc->state = ADA_STATE_NORMAL;
2472 xpt_release_ccb(start_ccb);
2473 break;
2474 }
2475
2476
2477 ata_read_log(ataio,
2478 /*retries*/1,
2479 /*cbfcnp*/adadone,
2480 /*log_address*/ ATA_LOG_DIRECTORY,
2481 /*page_number*/ 0,
2482 /*block_count*/ 1,
2483 /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ?
2484 CAM_ATAIO_DMA : 0,
2485 /*data_ptr*/ (uint8_t *)log_dir,
2486 /*dxfer_len*/sizeof(*log_dir),
2487 /*timeout*/ada_default_timeout*1000);
2488
2489 start_ccb->ccb_h.ccb_state = ADA_CCB_LOGDIR;
2490 xpt_action(start_ccb);
2491 break;
2492 }
2493 case ADA_STATE_IDDIR:
2494 {
2495 struct ata_identify_log_pages *id_dir;
2496
2497 id_dir = malloc(sizeof(*id_dir), M_ATADA, M_NOWAIT | M_ZERO);
2498 if (id_dir == NULL) {
2499 xpt_print(periph->path, "Couldn't malloc id_dir "
2500 "data\n");
2501 adaprobedone(periph, start_ccb);
2502 break;
2503 }
2504
2505 ata_read_log(ataio,
2506 /*retries*/1,
2507 /*cbfcnp*/adadone,
2508 /*log_address*/ ATA_IDENTIFY_DATA_LOG,
2509 /*page_number*/ ATA_IDL_PAGE_LIST,
2510 /*block_count*/ 1,
2511 /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ?
2512 CAM_ATAIO_DMA : 0,
2513 /*data_ptr*/ (uint8_t *)id_dir,
2514 /*dxfer_len*/ sizeof(*id_dir),
2515 /*timeout*/ada_default_timeout*1000);
2516
2517 start_ccb->ccb_h.ccb_state = ADA_CCB_IDDIR;
2518 xpt_action(start_ccb);
2519 break;
2520 }
2521 case ADA_STATE_SUP_CAP:
2522 {
2523 struct ata_identify_log_sup_cap *sup_cap;
2524
2525 sup_cap = malloc(sizeof(*sup_cap), M_ATADA, M_NOWAIT|M_ZERO);
2526 if (sup_cap == NULL) {
2527 xpt_print(periph->path, "Couldn't malloc sup_cap "
2528 "data\n");
2529 adaprobedone(periph, start_ccb);
2530 break;
2531 }
2532
2533 ata_read_log(ataio,
2534 /*retries*/1,
2535 /*cbfcnp*/adadone,
2536 /*log_address*/ ATA_IDENTIFY_DATA_LOG,
2537 /*page_number*/ ATA_IDL_SUP_CAP,
2538 /*block_count*/ 1,
2539 /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ?
2540 CAM_ATAIO_DMA : 0,
2541 /*data_ptr*/ (uint8_t *)sup_cap,
2542 /*dxfer_len*/ sizeof(*sup_cap),
2543 /*timeout*/ada_default_timeout*1000);
2544
2545 start_ccb->ccb_h.ccb_state = ADA_CCB_SUP_CAP;
2546 xpt_action(start_ccb);
2547 break;
2548 }
2549 case ADA_STATE_ZONE:
2550 {
2551 struct ata_zoned_info_log *ata_zone;
2552
2553 ata_zone = malloc(sizeof(*ata_zone), M_ATADA, M_NOWAIT|M_ZERO);
2554 if (ata_zone == NULL) {
2555 xpt_print(periph->path, "Couldn't malloc ata_zone "
2556 "data\n");
2557 adaprobedone(periph, start_ccb);
2558 break;
2559 }
2560
2561 ata_read_log(ataio,
2562 /*retries*/1,
2563 /*cbfcnp*/adadone,
2564 /*log_address*/ ATA_IDENTIFY_DATA_LOG,
2565 /*page_number*/ ATA_IDL_ZDI,
2566 /*block_count*/ 1,
2567 /*protocol*/ softc->flags & ADA_FLAG_CAN_DMA ?
2568 CAM_ATAIO_DMA : 0,
2569 /*data_ptr*/ (uint8_t *)ata_zone,
2570 /*dxfer_len*/ sizeof(*ata_zone),
2571 /*timeout*/ada_default_timeout*1000);
2572
2573 start_ccb->ccb_h.ccb_state = ADA_CCB_ZONE;
2574 xpt_action(start_ccb);
2575 break;
2576 }
2577 }
2578 }
2579
2580 static void
2581 adaprobedone(struct cam_periph *periph, union ccb *ccb)
2582 {
2583 struct ada_softc *softc;
2584
2585 softc = (struct ada_softc *)periph->softc;
2586
2587 if (ccb != NULL)
2588 xpt_release_ccb(ccb);
2589
2590 softc->state = ADA_STATE_NORMAL;
2591 softc->flags |= ADA_FLAG_PROBED;
2592 adaschedule(periph);
2593 if ((softc->flags & ADA_FLAG_ANNOUNCED) == 0) {
2594 softc->flags |= ADA_FLAG_ANNOUNCED;
2595 cam_periph_unhold(periph);
2596 } else {
2597 cam_periph_release_locked(periph);
2598 }
2599 }
2600
2601 static void
2602 adazonedone(struct cam_periph *periph, union ccb *ccb)
2603 {
2604 struct bio *bp;
2605
2606 bp = (struct bio *)ccb->ccb_h.ccb_bp;
2607
2608 switch (bp->bio_zone.zone_cmd) {
2609 case DISK_ZONE_OPEN:
2610 case DISK_ZONE_CLOSE:
2611 case DISK_ZONE_FINISH:
2612 case DISK_ZONE_RWP:
2613 break;
2614 case DISK_ZONE_REPORT_ZONES: {
2615 uint32_t avail_len;
2616 struct disk_zone_report *rep;
2617 struct scsi_report_zones_hdr *hdr;
2618 struct scsi_report_zones_desc *desc;
2619 struct disk_zone_rep_entry *entry;
2620 uint32_t hdr_len, num_avail;
2621 uint32_t num_to_fill, i;
2622
2623 rep = &bp->bio_zone.zone_params.report;
2624 avail_len = ccb->ataio.dxfer_len - ccb->ataio.resid;
2625 /*
2626 * Note that bio_resid isn't normally used for zone
2627 * commands, but it is used by devstat_end_transaction_bio()
2628 * to determine how much data was transferred. Because
2629 * the size of the SCSI/ATA data structures is different
2630 * than the size of the BIO interface structures, the
2631 * amount of data actually transferred from the drive will
2632 * be different than the amount of data transferred to
2633 * the user.
2634 */
2635 hdr = (struct scsi_report_zones_hdr *)ccb->ataio.data_ptr;
2636 if (avail_len < sizeof(*hdr)) {
2637 /*
2638 * Is there a better error than EIO here? We asked
2639 * for at least the header, and we got less than
2640 * that.
2641 */
2642 bp->bio_error = EIO;
2643 bp->bio_flags |= BIO_ERROR;
2644 bp->bio_resid = bp->bio_bcount;
2645 break;
2646 }
2647
2648 hdr_len = le32dec(hdr->length);
2649 if (hdr_len > 0)
2650 rep->entries_available = hdr_len / sizeof(*desc);
2651 else
2652 rep->entries_available = 0;
2653 /*
2654 * NOTE: using the same values for the BIO version of the
2655 * same field as the SCSI/ATA values. This means we could
2656 * get some additional values that aren't defined in bio.h
2657 * if more values of the same field are defined later.
2658 */
2659 rep->header.same = hdr->byte4 & SRZ_SAME_MASK;
2660 rep->header.maximum_lba = le64dec(hdr->maximum_lba);
2661 /*
2662 * If the drive reports no entries that match the query,
2663 * we're done.
2664 */
2665 if (hdr_len == 0) {
2666 rep->entries_filled = 0;
2667 bp->bio_resid = bp->bio_bcount;
2668 break;
2669 }
2670
2671 num_avail = min((avail_len - sizeof(*hdr)) / sizeof(*desc),
2672 hdr_len / sizeof(*desc));
2673 /*
2674 * If the drive didn't return any data, then we're done.
2675 */
2676 if (num_avail == 0) {
2677 rep->entries_filled = 0;
2678 bp->bio_resid = bp->bio_bcount;
2679 break;
2680 }
2681
2682 num_to_fill = min(num_avail, rep->entries_allocated);
2683 /*
2684 * If the user didn't allocate any entries for us to fill,
2685 * we're done.
2686 */
2687 if (num_to_fill == 0) {
2688 rep->entries_filled = 0;
2689 bp->bio_resid = bp->bio_bcount;
2690 break;
2691 }
2692
2693 for (i = 0, desc = &hdr->desc_list[0], entry=&rep->entries[0];
2694 i < num_to_fill; i++, desc++, entry++) {
2695 /*
2696 * NOTE: we're mapping the values here directly
2697 * from the SCSI/ATA bit definitions to the bio.h
2698 * definitions. There is also a warning in
2699 * disk_zone.h, but the impact is that if
2700 * additional values are added in the SCSI/ATA
2701 * specs these will be visible to consumers of
2702 * this interface.
2703 */
2704 entry->zone_type = desc->zone_type & SRZ_TYPE_MASK;
2705 entry->zone_condition =
2706 (desc->zone_flags & SRZ_ZONE_COND_MASK) >>
2707 SRZ_ZONE_COND_SHIFT;
2708 entry->zone_flags |= desc->zone_flags &
2709 (SRZ_ZONE_NON_SEQ|SRZ_ZONE_RESET);
2710 entry->zone_length = le64dec(desc->zone_length);
2711 entry->zone_start_lba = le64dec(desc->zone_start_lba);
2712 entry->write_pointer_lba =
2713 le64dec(desc->write_pointer_lba);
2714 }
2715 rep->entries_filled = num_to_fill;
2716 /*
2717 * Note that this residual is accurate from the user's
2718 * standpoint, but the amount transferred isn't accurate
2719 * from the standpoint of what actually came back from the
2720 * drive.
2721 */
2722 bp->bio_resid = bp->bio_bcount - (num_to_fill * sizeof(*entry));
2723 break;
2724 }
2725 case DISK_ZONE_GET_PARAMS:
2726 default:
2727 /*
2728 * In theory we should not get a GET_PARAMS bio, since it
2729 * should be handled without queueing the command to the
2730 * drive.
2731 */
2732 panic("%s: Invalid zone command %d", __func__,
2733 bp->bio_zone.zone_cmd);
2734 break;
2735 }
2736
2737 if (bp->bio_zone.zone_cmd == DISK_ZONE_REPORT_ZONES)
2738 free(ccb->ataio.data_ptr, M_ATADA);
2739 }
2740
2741
2742 static void
2743 adadone(struct cam_periph *periph, union ccb *done_ccb)
2744 {
2745 struct ada_softc *softc;
2746 struct ccb_ataio *ataio;
2747 struct cam_path *path;
2748 uint32_t priority;
2749 int state;
2750
2751 softc = (struct ada_softc *)periph->softc;
2752 ataio = &done_ccb->ataio;
2753 path = done_ccb->ccb_h.path;
2754 priority = done_ccb->ccb_h.pinfo.priority;
2755
2756 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("adadone\n"));
2757
2758 state = ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK;
2759 switch (state) {
2760 case ADA_CCB_BUFFER_IO:
2761 case ADA_CCB_TRIM:
2762 {
2763 struct bio *bp;
2764 int error;
2765
2766 cam_periph_lock(periph);
2767 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2768 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2769 error = adaerror(done_ccb, 0, 0);
2770 if (error == ERESTART) {
2771 /* A retry was scheduled, so just return. */
2772 cam_periph_unlock(periph);
2773 return;
2774 }
2775 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2776 cam_release_devq(path,
2777 /*relsim_flags*/0,
2778 /*reduction*/0,
2779 /*timeout*/0,
2780 /*getcount_only*/0);
2781 /*
2782 * If we get an error on an NCQ DSM TRIM, fall back
2783 * to a non-NCQ DSM TRIM forever. Please note that if
2784 * CAN_NCQ_TRIM is set, CAN_TRIM is necessarily set too.
2785 * However, for this one trim, we treat it as advisory
2786 * and return success up the stack.
2787 */
2788 if (state == ADA_CCB_TRIM &&
2789 error != 0 &&
2790 (softc->flags & ADA_FLAG_CAN_NCQ_TRIM) != 0) {
2791 softc->flags &= ~ADA_FLAG_CAN_NCQ_TRIM;
2792 error = 0;
2793 adasetdeletemethod(softc);
2794 }
2795 } else {
2796 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2797 panic("REQ_CMP with QFRZN");
2798
2799 error = 0;
2800 }
2801 bp->bio_error = error;
2802 if (error != 0) {
2803 bp->bio_resid = bp->bio_bcount;
2804 bp->bio_flags |= BIO_ERROR;
2805 } else {
2806 if (bp->bio_cmd == BIO_ZONE)
2807 adazonedone(periph, done_ccb);
2808 else if (state == ADA_CCB_TRIM)
2809 bp->bio_resid = 0;
2810 else
2811 bp->bio_resid = ataio->resid;
2812
2813 if ((bp->bio_resid > 0)
2814 && (bp->bio_cmd != BIO_ZONE))
2815 bp->bio_flags |= BIO_ERROR;
2816 }
2817 softc->outstanding_cmds--;
2818 if (softc->outstanding_cmds == 0)
2819 softc->flags |= ADA_FLAG_WAS_OTAG;
2820
2821 /*
2822 * We need to call cam_iosched before we call biodone so that we
2823 * don't measure any activity that happens in the completion
2824 * routine, which in the case of sendfile can be quite
2825 * extensive. Release the periph refcount taken in adastart()
2826 * for each CCB.
2827 */
2828 cam_iosched_bio_complete(softc->cam_iosched, bp, done_ccb);
2829 xpt_release_ccb(done_ccb);
2830 KASSERT(softc->refcount >= 1, ("adadone softc %p refcount %d", softc, softc->refcount));
2831 softc->refcount--;
2832 if (state == ADA_CCB_TRIM) {
2833 TAILQ_HEAD(, bio) queue;
2834 struct bio *bp1;
2835
2836 TAILQ_INIT(&queue);
2837 TAILQ_CONCAT(&queue, &softc->trim_req.bps, bio_queue);
2838 /*
2839 * Normally, the xpt_release_ccb() above would make sure
2840 * that when we have more work to do, that work would
2841 * get kicked off. However, we specifically keep
2842 * trim_running set to 0 before the call above to allow
2843 * other I/O to progress when many BIO_DELETE requests
2844 * are pushed down. We set trim_running to 0 and call
2845 * daschedule again so that we don't stall if there are
2846 * no other I/Os pending apart from BIO_DELETEs.
2847 */
2848 cam_iosched_trim_done(softc->cam_iosched);
2849 adaschedule(periph);
2850 cam_periph_unlock(periph);
2851 while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
2852 TAILQ_REMOVE(&queue, bp1, bio_queue);
2853 bp1->bio_error = error;
2854 if (error != 0) {
2855 bp1->bio_flags |= BIO_ERROR;
2856 bp1->bio_resid = bp1->bio_bcount;
2857 } else
2858 bp1->bio_resid = 0;
2859 biodone(bp1);
2860 }
2861 } else {
2862 adaschedule(periph);
2863 cam_periph_unlock(periph);
2864 biodone(bp);
2865 }
2866 return;
2867 }
2868 case ADA_CCB_RAHEAD:
2869 {
2870 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2871 if (adaerror(done_ccb, 0, 0) == ERESTART) {
2872 /* Drop freeze taken due to CAM_DEV_QFREEZE */
2873 cam_release_devq(path, 0, 0, 0, FALSE);
2874 return;
2875 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
2876 cam_release_devq(path,
2877 /*relsim_flags*/0,
2878 /*reduction*/0,
2879 /*timeout*/0,
2880 /*getcount_only*/0);
2881 }
2882 }
2883
2884 /*
2885 * Since our peripheral may be invalidated by an error
2886 * above or an external event, we must release our CCB
2887 * before releasing the reference on the peripheral.
2888 * The peripheral will only go away once the last reference
2889 * is removed, and we need it around for the CCB release
2890 * operation.
2891 */
2892
2893 xpt_release_ccb(done_ccb);
2894 softc->state = ADA_STATE_WCACHE;
2895 xpt_schedule(periph, priority);
2896 /* Drop freeze taken due to CAM_DEV_QFREEZE */
2897 cam_release_devq(path, 0, 0, 0, FALSE);
2898 return;
2899 }
2900 case ADA_CCB_WCACHE:
2901 {
2902 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2903 if (adaerror(done_ccb, 0, 0) == ERESTART) {
2904 /* Drop freeze taken due to CAM_DEV_QFREEZE */
2905 cam_release_devq(path, 0, 0, 0, FALSE);
2906 return;
2907 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
2908 cam_release_devq(path,
2909 /*relsim_flags*/0,
2910 /*reduction*/0,
2911 /*timeout*/0,
2912 /*getcount_only*/0);
2913 }
2914 }
2915
2916 /* Drop freeze taken due to CAM_DEV_QFREEZE */
2917 cam_release_devq(path, 0, 0, 0, FALSE);
2918
2919 if ((softc->flags & ADA_FLAG_CAN_LOG)
2920 && (softc->zone_mode != ADA_ZONE_NONE)) {
2921 xpt_release_ccb(done_ccb);
2922 softc->state = ADA_STATE_LOGDIR;
2923 xpt_schedule(periph, priority);
2924 } else {
2925 adaprobedone(periph, done_ccb);
2926 }
2927 return;
2928 }
2929 case ADA_CCB_LOGDIR:
2930 {
2931 int error;
2932
2933 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
2934 error = 0;
2935 softc->valid_logdir_len = 0;
2936 bzero(&softc->ata_logdir, sizeof(softc->ata_logdir));
2937 softc->valid_logdir_len =
2938 ataio->dxfer_len - ataio->resid;
2939 if (softc->valid_logdir_len > 0)
2940 bcopy(ataio->data_ptr, &softc->ata_logdir,
2941 min(softc->valid_logdir_len,
2942 sizeof(softc->ata_logdir)));
2943 /*
2944 * Figure out whether the Identify Device log is
2945 * supported. The General Purpose log directory
2946 * has a header, and lists the number of pages
2947 * available for each GP log identified by the
2948 * offset into the list.
2949 */
2950 if ((softc->valid_logdir_len >=
2951 ((ATA_IDENTIFY_DATA_LOG + 1) * sizeof(uint16_t)))
2952 && (le16dec(softc->ata_logdir.header) ==
2953 ATA_GP_LOG_DIR_VERSION)
2954 && (le16dec(&softc->ata_logdir.num_pages[
2955 (ATA_IDENTIFY_DATA_LOG *
2956 sizeof(uint16_t)) - sizeof(uint16_t)]) > 0)){
2957 softc->flags |= ADA_FLAG_CAN_IDLOG;
2958 } else {
2959 softc->flags &= ~ADA_FLAG_CAN_IDLOG;
2960 }
2961 } else {
2962 error = adaerror(done_ccb, CAM_RETRY_SELTO,
2963 SF_RETRY_UA|SF_NO_PRINT);
2964 if (error == ERESTART)
2965 return;
2966 else if (error != 0) {
2967 /*
2968 * If we can't get the ATA log directory,
2969 * then ATA logs are effectively not
2970 * supported even if the bit is set in the
2971 * identify data.
2972 */
2973 softc->flags &= ~(ADA_FLAG_CAN_LOG |
2974 ADA_FLAG_CAN_IDLOG);
2975 if ((done_ccb->ccb_h.status &
2976 CAM_DEV_QFRZN) != 0) {
2977 /* Don't wedge this device's queue */
2978 cam_release_devq(done_ccb->ccb_h.path,
2979 /*relsim_flags*/0,
2980 /*reduction*/0,
2981 /*timeout*/0,
2982 /*getcount_only*/0);
2983 }
2984 }
2985
2986
2987 }
2988
2989 free(ataio->data_ptr, M_ATADA);
2990
2991 if ((error == 0)
2992 && (softc->flags & ADA_FLAG_CAN_IDLOG)) {
2993 softc->state = ADA_STATE_IDDIR;
2994 xpt_release_ccb(done_ccb);
2995 xpt_schedule(periph, priority);
2996 } else
2997 adaprobedone(periph, done_ccb);
2998
2999 return;
3000 }
3001 case ADA_CCB_IDDIR: {
3002 int error;
3003
3004 if ((ataio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3005 off_t entries_offset, max_entries;
3006 error = 0;
3007
3008 softc->valid_iddir_len = 0;
3009 bzero(&softc->ata_iddir, sizeof(softc->ata_iddir));
3010 softc->flags &= ~(ADA_FLAG_CAN_SUPCAP |
3011 ADA_FLAG_CAN_ZONE);
3012 softc->valid_iddir_len =
3013 ataio->dxfer_len - ataio->resid;
3014 if (softc->valid_iddir_len > 0)
3015 bcopy(ataio->data_ptr, &softc->ata_iddir,
3016 min(softc->valid_iddir_len,
3017 sizeof(softc->ata_iddir)));
3018
3019 entries_offset =
3020 __offsetof(struct ata_identify_log_pages,entries);
3021 max_entries = softc->valid_iddir_len - entries_offset;
3022 if ((softc->valid_iddir_len > (entries_offset + 1))
3023 && (le64dec(softc->ata_iddir.header) ==
3024 ATA_IDLOG_REVISION)
3025 && (softc->ata_iddir.entry_count > 0)) {
3026 int num_entries, i;
3027
3028 num_entries = softc->ata_iddir.entry_count;
3029 num_entries = min(num_entries,
3030 softc->valid_iddir_len - entries_offset);
3031 for (i = 0; i < num_entries &&
3032 i < max_entries; i++) {
3033 if (softc->ata_iddir.entries[i] ==
3034 ATA_IDL_SUP_CAP)
3035 softc->flags |=
3036 ADA_FLAG_CAN_SUPCAP;
3037 else if (softc->ata_iddir.entries[i]==
3038 ATA_IDL_ZDI)
3039 softc->flags |=
3040 ADA_FLAG_CAN_ZONE;
3041
3042 if ((softc->flags &
3043 ADA_FLAG_CAN_SUPCAP)
3044 && (softc->flags &
3045 ADA_FLAG_CAN_ZONE))
3046 break;
3047 }
3048 }
3049 } else {
3050 error = adaerror(done_ccb, CAM_RETRY_SELTO,
3051 SF_RETRY_UA|SF_NO_PRINT);
3052 if (error == ERESTART)
3053 return;
3054 else if (error != 0) {
3055 /*
3056 * If we can't get the ATA Identify Data log
3057 * directory, then it effectively isn't
3058 * supported even if the ATA Log directory
3059 * a non-zero number of pages present for
3060 * this log.
3061 */
3062 softc->flags &= ~ADA_FLAG_CAN_IDLOG;
3063 if ((done_ccb->ccb_h.status &
3064 CAM_DEV_QFRZN) != 0) {
3065 /* Don't wedge this device's queue */
3066 cam_release_devq(done_ccb->ccb_h.path,
3067 /*relsim_flags*/0,
3068 /*reduction*/0,
3069 /*timeout*/0,
3070 /*getcount_only*/0);
3071 }
3072 }
3073 }
3074
3075 free(ataio->data_ptr, M_ATADA);
3076
3077 if ((error == 0)
3078 && (softc->flags & ADA_FLAG_CAN_SUPCAP)) {
3079 softc->state = ADA_STATE_SUP_CAP;
3080 xpt_release_ccb(done_ccb);
3081 xpt_schedule(periph, priority);
3082 } else
3083 adaprobedone(periph, done_ccb);
3084 return;
3085 }
3086 case ADA_CCB_SUP_CAP: {
3087 int error;
3088
3089 if ((ataio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3090 uint32_t valid_len;
3091 size_t needed_size;
3092 struct ata_identify_log_sup_cap *sup_cap;
3093 error = 0;
3094
3095 sup_cap = (struct ata_identify_log_sup_cap *)
3096 ataio->data_ptr;
3097 valid_len = ataio->dxfer_len - ataio->resid;
3098 needed_size =
3099 __offsetof(struct ata_identify_log_sup_cap,
3100 sup_zac_cap) + 1 + sizeof(sup_cap->sup_zac_cap);
3101 if (valid_len >= needed_size) {
3102 uint64_t zoned, zac_cap;
3103
3104 zoned = le64dec(sup_cap->zoned_cap);
3105 if (zoned & ATA_ZONED_VALID) {
3106 /*
3107 * This should have already been
3108 * set, because this is also in the
3109 * ATA identify data.
3110 */
3111 if ((zoned & ATA_ZONED_MASK) ==
3112 ATA_SUPPORT_ZONE_HOST_AWARE)
3113 softc->zone_mode =
3114 ADA_ZONE_HOST_AWARE;
3115 else if ((zoned & ATA_ZONED_MASK) ==
3116 ATA_SUPPORT_ZONE_DEV_MANAGED)
3117 softc->zone_mode =
3118 ADA_ZONE_DRIVE_MANAGED;
3119 }
3120
3121 zac_cap = le64dec(sup_cap->sup_zac_cap);
3122 if (zac_cap & ATA_SUP_ZAC_CAP_VALID) {
3123 if (zac_cap & ATA_REPORT_ZONES_SUP)
3124 softc->zone_flags |=
3125 ADA_ZONE_FLAG_RZ_SUP;
3126 if (zac_cap & ATA_ND_OPEN_ZONE_SUP)
3127 softc->zone_flags |=
3128 ADA_ZONE_FLAG_OPEN_SUP;
3129 if (zac_cap & ATA_ND_CLOSE_ZONE_SUP)
3130 softc->zone_flags |=
3131 ADA_ZONE_FLAG_CLOSE_SUP;
3132 if (zac_cap & ATA_ND_FINISH_ZONE_SUP)
3133 softc->zone_flags |=
3134 ADA_ZONE_FLAG_FINISH_SUP;
3135 if (zac_cap & ATA_ND_RWP_SUP)
3136 softc->zone_flags |=
3137 ADA_ZONE_FLAG_RWP_SUP;
3138 } else {
3139 /*
3140 * This field was introduced in
3141 * ACS-4, r08 on April 28th, 2015.
3142 * If the drive firmware was written
3143 * to an earlier spec, it won't have
3144 * the field. So, assume all
3145 * commands are supported.
3146 */
3147 softc->zone_flags |=
3148 ADA_ZONE_FLAG_SUP_MASK;
3149 }
3150 }
3151 } else {
3152 error = adaerror(done_ccb, CAM_RETRY_SELTO,
3153 SF_RETRY_UA|SF_NO_PRINT);
3154 if (error == ERESTART)
3155 return;
3156 else if (error != 0) {
3157 /*
3158 * If we can't get the ATA Identify Data
3159 * Supported Capabilities page, clear the
3160 * flag...
3161 */
3162 softc->flags &= ~ADA_FLAG_CAN_SUPCAP;
3163 /*
3164 * And clear zone capabilities.
3165 */
3166 softc->zone_flags &= ~ADA_ZONE_FLAG_SUP_MASK;
3167 if ((done_ccb->ccb_h.status &
3168 CAM_DEV_QFRZN) != 0) {
3169 /* Don't wedge this device's queue */
3170 cam_release_devq(done_ccb->ccb_h.path,
3171 /*relsim_flags*/0,
3172 /*reduction*/0,
3173 /*timeout*/0,
3174 /*getcount_only*/0);
3175 }
3176 }
3177 }
3178
3179 free(ataio->data_ptr, M_ATADA);
3180
3181 if ((error == 0)
3182 && (softc->flags & ADA_FLAG_CAN_ZONE)) {
3183 softc->state = ADA_STATE_ZONE;
3184 xpt_release_ccb(done_ccb);
3185 xpt_schedule(periph, priority);
3186 } else
3187 adaprobedone(periph, done_ccb);
3188 return;
3189 }
3190 case ADA_CCB_ZONE: {
3191 int error;
3192
3193 if ((ataio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3194 struct ata_zoned_info_log *zi_log;
3195 uint32_t valid_len;
3196 size_t needed_size;
3197
3198 zi_log = (struct ata_zoned_info_log *)ataio->data_ptr;
3199
3200 valid_len = ataio->dxfer_len - ataio->resid;
3201 needed_size = __offsetof(struct ata_zoned_info_log,
3202 version_info) + 1 + sizeof(zi_log->version_info);
3203 if (valid_len >= needed_size) {
3204 uint64_t tmpvar;
3205
3206 tmpvar = le64dec(zi_log->zoned_cap);
3207 if (tmpvar & ATA_ZDI_CAP_VALID) {
3208 if (tmpvar & ATA_ZDI_CAP_URSWRZ)
3209 softc->zone_flags |=
3210 ADA_ZONE_FLAG_URSWRZ;
3211 else
3212 softc->zone_flags &=
3213 ~ADA_ZONE_FLAG_URSWRZ;
3214 }
3215 tmpvar = le64dec(zi_log->optimal_seq_zones);
3216 if (tmpvar & ATA_ZDI_OPT_SEQ_VALID) {
3217 softc->zone_flags |=
3218 ADA_ZONE_FLAG_OPT_SEQ_SET;
3219 softc->optimal_seq_zones = (tmpvar &
3220 ATA_ZDI_OPT_SEQ_MASK);
3221 } else {
3222 softc->zone_flags &=
3223 ~ADA_ZONE_FLAG_OPT_SEQ_SET;
3224 softc->optimal_seq_zones = 0;
3225 }
3226
3227 tmpvar =le64dec(zi_log->optimal_nonseq_zones);
3228 if (tmpvar & ATA_ZDI_OPT_NS_VALID) {
3229 softc->zone_flags |=
3230 ADA_ZONE_FLAG_OPT_NONSEQ_SET;
3231 softc->optimal_nonseq_zones =
3232 (tmpvar & ATA_ZDI_OPT_NS_MASK);
3233 } else {
3234 softc->zone_flags &=
3235 ~ADA_ZONE_FLAG_OPT_NONSEQ_SET;
3236 softc->optimal_nonseq_zones = 0;
3237 }
3238
3239 tmpvar = le64dec(zi_log->max_seq_req_zones);
3240 if (tmpvar & ATA_ZDI_MAX_SEQ_VALID) {
3241 softc->zone_flags |=
3242 ADA_ZONE_FLAG_MAX_SEQ_SET;
3243 softc->max_seq_zones =
3244 (tmpvar & ATA_ZDI_MAX_SEQ_MASK);
3245 } else {
3246 softc->zone_flags &=
3247 ~ADA_ZONE_FLAG_MAX_SEQ_SET;
3248 softc->max_seq_zones = 0;
3249 }
3250 }
3251 } else {
3252 error = adaerror(done_ccb, CAM_RETRY_SELTO,
3253 SF_RETRY_UA|SF_NO_PRINT);
3254 if (error == ERESTART)
3255 return;
3256 else if (error != 0) {
3257 softc->flags &= ~ADA_FLAG_CAN_ZONE;
3258 softc->flags &= ~ADA_ZONE_FLAG_SET_MASK;
3259
3260 if ((done_ccb->ccb_h.status &
3261 CAM_DEV_QFRZN) != 0) {
3262 /* Don't wedge this device's queue */
3263 cam_release_devq(done_ccb->ccb_h.path,
3264 /*relsim_flags*/0,
3265 /*reduction*/0,
3266 /*timeout*/0,
3267 /*getcount_only*/0);
3268 }
3269 }
3270 }
3271 free(ataio->data_ptr, M_ATADA);
3272
3273 adaprobedone(periph, done_ccb);
3274 return;
3275 }
3276 case ADA_CCB_DUMP:
3277 /* No-op. We're polling */
3278 return;
3279 default:
3280 break;
3281 }
3282 xpt_release_ccb(done_ccb);
3283 }
3284
3285 static int
3286 adaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3287 {
3288 #ifdef CAM_IO_STATS
3289 struct ada_softc *softc;
3290 struct cam_periph *periph;
3291
3292 periph = xpt_path_periph(ccb->ccb_h.path);
3293 softc = (struct ada_softc *)periph->softc;
3294
3295 switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
3296 case CAM_CMD_TIMEOUT:
3297 softc->timeouts++;
3298 break;
3299 case CAM_REQ_ABORTED:
3300 case CAM_REQ_CMP_ERR:
3301 case CAM_REQ_TERMIO:
3302 case CAM_UNREC_HBA_ERROR:
3303 case CAM_DATA_RUN_ERR:
3304 case CAM_ATA_STATUS_ERROR:
3305 softc->errors++;
3306 break;
3307 default:
3308 break;
3309 }
3310 #endif
3311
3312 return(cam_periph_error(ccb, cam_flags, sense_flags));
3313 }
3314
3315 static void
3316 adasetgeom(struct ada_softc *softc, struct ccb_getdev *cgd)
3317 {
3318 struct disk_params *dp = &softc->params;
3319 u_int64_t lbasize48;
3320 u_int32_t lbasize;
3321 u_int maxio, d_flags;
3322
3323 dp->secsize = ata_logical_sector_size(&cgd->ident_data);
3324 if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) &&
3325 cgd->ident_data.current_heads != 0 &&
3326 cgd->ident_data.current_sectors != 0) {
3327 dp->heads = cgd->ident_data.current_heads;
3328 dp->secs_per_track = cgd->ident_data.current_sectors;
3329 dp->cylinders = cgd->ident_data.cylinders;
3330 dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 |
3331 ((u_int32_t)cgd->ident_data.current_size_2 << 16);
3332 } else {
3333 dp->heads = cgd->ident_data.heads;
3334 dp->secs_per_track = cgd->ident_data.sectors;
3335 dp->cylinders = cgd->ident_data.cylinders;
3336 dp->sectors = cgd->ident_data.cylinders *
3337 (u_int32_t)(dp->heads * dp->secs_per_track);
3338 }
3339 lbasize = (u_int32_t)cgd->ident_data.lba_size_1 |
3340 ((u_int32_t)cgd->ident_data.lba_size_2 << 16);
3341
3342 /* use the 28bit LBA size if valid or bigger than the CHS mapping */
3343 if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize)
3344 dp->sectors = lbasize;
3345
3346 /* use the 48bit LBA size if valid */
3347 lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) |
3348 ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) |
3349 ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) |
3350 ((u_int64_t)cgd->ident_data.lba_size48_4 << 48);
3351 if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) &&
3352 lbasize48 > ATA_MAX_28BIT_LBA)
3353 dp->sectors = lbasize48;
3354
3355 maxio = softc->cpi.maxio; /* Honor max I/O size of SIM */
3356 if (maxio == 0)
3357 maxio = DFLTPHYS; /* traditional default */
3358 else if (maxio > MAXPHYS)
3359 maxio = MAXPHYS; /* for safety */
3360 if (softc->flags & ADA_FLAG_CAN_48BIT)
3361 maxio = min(maxio, 65536 * softc->params.secsize);
3362 else /* 28bit ATA command limit */
3363 maxio = min(maxio, 256 * softc->params.secsize);
3364 if (softc->quirks & ADA_Q_128KB)
3365 maxio = min(maxio, 128 * 1024);
3366 softc->disk->d_maxsize = maxio;
3367 d_flags = DISKFLAG_DIRECT_COMPLETION | DISKFLAG_CANZONE;
3368 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE)
3369 d_flags |= DISKFLAG_CANFLUSHCACHE;
3370 if (softc->flags & ADA_FLAG_CAN_TRIM) {
3371 d_flags |= DISKFLAG_CANDELETE;
3372 softc->disk->d_delmaxsize = softc->params.secsize *
3373 ATA_DSM_RANGE_MAX * softc->trim_max_ranges;
3374 } else if ((softc->flags & ADA_FLAG_CAN_CFA) &&
3375 !(softc->flags & ADA_FLAG_CAN_48BIT)) {
3376 d_flags |= DISKFLAG_CANDELETE;
3377 softc->disk->d_delmaxsize = 256 * softc->params.secsize;
3378 } else
3379 softc->disk->d_delmaxsize = maxio;
3380 if ((softc->cpi.hba_misc & PIM_UNMAPPED) != 0) {
3381 d_flags |= DISKFLAG_UNMAPPED_BIO;
3382 softc->unmappedio = 1;
3383 }
3384 softc->disk->d_flags = d_flags;
3385 strlcpy(softc->disk->d_descr, cgd->ident_data.model,
3386 MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model)));
3387 strlcpy(softc->disk->d_ident, cgd->ident_data.serial,
3388 MIN(sizeof(softc->disk->d_ident), sizeof(cgd->ident_data.serial)));
3389
3390 softc->disk->d_sectorsize = softc->params.secsize;
3391 softc->disk->d_mediasize = (off_t)softc->params.sectors *
3392 softc->params.secsize;
3393 if (ata_physical_sector_size(&cgd->ident_data) !=
3394 softc->params.secsize) {
3395 softc->disk->d_stripesize =
3396 ata_physical_sector_size(&cgd->ident_data);
3397 softc->disk->d_stripeoffset = (softc->disk->d_stripesize -
3398 ata_logical_sector_offset(&cgd->ident_data)) %
3399 softc->disk->d_stripesize;
3400 } else if (softc->quirks & ADA_Q_4K) {
3401 softc->disk->d_stripesize = 4096;
3402 softc->disk->d_stripeoffset = 0;
3403 }
3404 softc->disk->d_fwsectors = softc->params.secs_per_track;
3405 softc->disk->d_fwheads = softc->params.heads;
3406 ata_disk_firmware_geom_adjust(softc->disk);
3407 softc->disk->d_rotation_rate = cgd->ident_data.media_rotation_rate;
3408 }
3409
3410 static void
3411 adasendorderedtag(void *arg)
3412 {
3413 struct ada_softc *softc = arg;
3414
3415 if (ada_send_ordered) {
3416 if (softc->outstanding_cmds > 0) {
3417 if ((softc->flags & ADA_FLAG_WAS_OTAG) == 0)
3418 softc->flags |= ADA_FLAG_NEED_OTAG;
3419 softc->flags &= ~ADA_FLAG_WAS_OTAG;
3420 }
3421 }
3422 /* Queue us up again */
3423 callout_reset(&softc->sendordered_c,
3424 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL,
3425 adasendorderedtag, softc);
3426 }
3427
3428 /*
3429 * Step through all ADA peripheral drivers, and if the device is still open,
3430 * sync the disk cache to physical media.
3431 */
3432 static void
3433 adaflush(void)
3434 {
3435 struct cam_periph *periph;
3436 struct ada_softc *softc;
3437 union ccb *ccb;
3438 int error;
3439
3440 CAM_PERIPH_FOREACH(periph, &adadriver) {
3441 softc = (struct ada_softc *)periph->softc;
3442 if (SCHEDULER_STOPPED()) {
3443 /* If we panicked with the lock held, do not recurse. */
3444 if (!cam_periph_owned(periph) &&
3445 (softc->flags & ADA_FLAG_OPEN)) {
3446 adadump(softc->disk, NULL, 0, 0, 0);
3447 }
3448 continue;
3449 }
3450 cam_periph_lock(periph);
3451 /*
3452 * We only sync the cache if the drive is still open, and
3453 * if the drive is capable of it..
3454 */
3455 if (((softc->flags & ADA_FLAG_OPEN) == 0) ||
3456 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) {
3457 cam_periph_unlock(periph);
3458 continue;
3459 }
3460
3461 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3462 cam_fill_ataio(&ccb->ataio,
3463 0,
3464 NULL,
3465 CAM_DIR_NONE,
3466 0,
3467 NULL,
3468 0,
3469 ada_default_timeout*1000);
3470 if (softc->flags & ADA_FLAG_CAN_48BIT)
3471 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
3472 else
3473 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
3474
3475 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
3476 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
3477 softc->disk->d_devstat);
3478 if (error != 0)
3479 xpt_print(periph->path, "Synchronize cache failed\n");
3480 xpt_release_ccb(ccb);
3481 cam_periph_unlock(periph);
3482 }
3483 }
3484
3485 static void
3486 adaspindown(uint8_t cmd, int flags)
3487 {
3488 struct cam_periph *periph;
3489 struct ada_softc *softc;
3490 struct ccb_ataio local_ccb;
3491 int error;
3492 int mode;
3493
3494 CAM_PERIPH_FOREACH(periph, &adadriver) {
3495 /* If we panicked with lock held - not recurse here. */
3496 if (cam_periph_owned(periph))
3497 continue;
3498 cam_periph_lock(periph);
3499 softc = (struct ada_softc *)periph->softc;
3500 /*
3501 * We only spin-down the drive if it is capable of it..
3502 */
3503 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
3504 cam_periph_unlock(periph);
3505 continue;
3506 }
3507
3508 /*
3509 * Additionally check if we would spin up the drive instead of
3510 * spinning it down.
3511 */
3512 if (cmd == ATA_IDLE_IMMEDIATE) {
3513 memset(&local_ccb, 0, sizeof(local_ccb));
3514 xpt_setup_ccb(&local_ccb.ccb_h, periph->path,
3515 CAM_PRIORITY_NORMAL);
3516 local_ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
3517
3518 cam_fill_ataio(&local_ccb, 0, NULL, CAM_DIR_NONE,
3519 0, NULL, 0, ada_default_timeout * 1000);
3520 ata_28bit_cmd(&local_ccb, ATA_CHECK_POWER_MODE,
3521 0, 0, 0);
3522 local_ccb.cmd.flags |= CAM_ATAIO_NEEDRESULT;
3523
3524 error = cam_periph_runccb((union ccb *)&local_ccb,
3525 adaerror, /*cam_flags*/0,
3526 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
3527 softc->disk->d_devstat);
3528 if (error != 0) {
3529 xpt_print(periph->path,
3530 "Failed to read current power mode\n");
3531 } else {
3532 mode = local_ccb.res.sector_count;
3533 #ifdef DIAGNOSTIC
3534 if (bootverbose) {
3535 xpt_print(periph->path,
3536 "disk power mode 0x%02x\n", mode);
3537 }
3538 #endif
3539 switch (mode) {
3540 case ATA_PM_STANDBY:
3541 case ATA_PM_STANDBY_Y:
3542 if (bootverbose) {
3543 xpt_print(periph->path,
3544 "already spun down\n");
3545 }
3546 cam_periph_unlock(periph);
3547 continue;
3548 default:
3549 break;
3550 }
3551 }
3552 }
3553
3554 if (bootverbose)
3555 xpt_print(periph->path, "spin-down\n");
3556
3557 memset(&local_ccb, 0, sizeof(local_ccb));
3558 xpt_setup_ccb(&local_ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3559 local_ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
3560
3561 cam_fill_ataio(&local_ccb,
3562 0,
3563 NULL,
3564 CAM_DIR_NONE | flags,
3565 0,
3566 NULL,
3567 0,
3568 ada_default_timeout*1000);
3569 ata_28bit_cmd(&local_ccb, cmd, 0, 0, 0);
3570 error = cam_periph_runccb((union ccb *)&local_ccb, adaerror,
3571 /*cam_flags*/0, /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
3572 softc->disk->d_devstat);
3573 if (error != 0)
3574 xpt_print(periph->path, "Spin-down disk failed\n");
3575 cam_periph_unlock(periph);
3576 }
3577 }
3578
3579 static void
3580 adashutdown(void *arg, int howto)
3581 {
3582 int how;
3583
3584 adaflush();
3585
3586 /*
3587 * STANDBY IMMEDIATE saves any volatile data to the drive. It also spins
3588 * down hard drives. IDLE IMMEDIATE also saves the volatile data without
3589 * a spindown. We send the former when we expect to lose power soon. For
3590 * a warm boot, we send the latter to avoid a thundering herd of spinups
3591 * just after the kernel loads while probing. We have to do something to
3592 * flush the data because the BIOS in many systems resets the HBA
3593 * causing a COMINIT/COMRESET negotiation, which some drives interpret
3594 * as license to toss the volatile data, and others count as unclean
3595 * shutdown when in the Active PM state in SMART attributes.
3596 *
3597 * adaspindown will ensure that we don't send this to a drive that
3598 * doesn't support it.
3599 */
3600 if (ada_spindown_shutdown != 0) {
3601 how = (howto & (RB_HALT | RB_POWEROFF | RB_POWERCYCLE)) ?
3602 ATA_STANDBY_IMMEDIATE : ATA_IDLE_IMMEDIATE;
3603 adaspindown(how, 0);
3604 }
3605 }
3606
3607 static void
3608 adasuspend(void *arg)
3609 {
3610
3611 adaflush();
3612 /*
3613 * SLEEP also fushes any volatile data, like STANDBY IMEDIATE,
3614 * so we don't need to send it as well.
3615 */
3616 if (ada_spindown_suspend != 0)
3617 adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE);
3618 }
3619
3620 static void
3621 adaresume(void *arg)
3622 {
3623 struct cam_periph *periph;
3624 struct ada_softc *softc;
3625
3626 if (ada_spindown_suspend == 0)
3627 return;
3628
3629 CAM_PERIPH_FOREACH(periph, &adadriver) {
3630 cam_periph_lock(periph);
3631 softc = (struct ada_softc *)periph->softc;
3632 /*
3633 * We only spin-down the drive if it is capable of it..
3634 */
3635 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
3636 cam_periph_unlock(periph);
3637 continue;
3638 }
3639
3640 if (bootverbose)
3641 xpt_print(periph->path, "resume\n");
3642
3643 /*
3644 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on
3645 * sleep request.
3646 */
3647 cam_release_devq(periph->path,
3648 /*relsim_flags*/0,
3649 /*openings*/0,
3650 /*timeout*/0,
3651 /*getcount_only*/0);
3652
3653 cam_periph_unlock(periph);
3654 }
3655 }
3656
3657 #endif /* _KERNEL */

Properties

Name Value
svn:eol-style native
svn:keywords FreeBSD=%H
svn:mime-type text/plain

  ViewVC Help
Powered by ViewVC 1.1.27