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