2 * Implementation of SCSI Direct Access Peripheral driver for CAM.
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 1997 Justin T. Gibbs.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification, immediately at the beginning of the file.
15 * 2. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
42 #include <sys/taskqueue.h>
44 #include <sys/mutex.h>
46 #include <sys/devicestat.h>
47 #include <sys/eventhandler.h>
48 #include <sys/malloc.h>
50 #include <sys/endian.h>
53 #include <geom/geom.h>
54 #include <geom/geom_disk.h>
55 #include <machine/atomic.h>
64 #include <cam/cam_ccb.h>
65 #include <cam/cam_periph.h>
66 #include <cam/cam_xpt_periph.h>
67 #include <cam/cam_sim.h>
68 #include <cam/cam_iosched.h>
70 #include <cam/scsi/scsi_message.h>
71 #include <cam/scsi/scsi_da.h>
75 * Note that there are probe ordering dependencies here. The order isn't
76 * controlled by this enumeration, but by explicit state transitions in
77 * dastart() and dadone(). Here are some of the dependencies:
79 * 1. RC should come first, before RC16, unless there is evidence that RC16
81 * 2. BDC needs to come before any of the ATA probes, or the ZONE probe.
82 * 3. The ATA probes should go in this order:
83 * ATA -> LOGDIR -> IDDIR -> SUP -> ATA_ZONE
90 DA_STATE_PROBE_BLK_LIMITS,
93 DA_STATE_PROBE_ATA_LOGDIR,
94 DA_STATE_PROBE_ATA_IDDIR,
95 DA_STATE_PROBE_ATA_SUP,
96 DA_STATE_PROBE_ATA_ZONE,
102 DA_FLAG_PACK_INVALID = 0x000001,
103 DA_FLAG_NEW_PACK = 0x000002,
104 DA_FLAG_PACK_LOCKED = 0x000004,
105 DA_FLAG_PACK_REMOVABLE = 0x000008,
106 DA_FLAG_NEED_OTAG = 0x000020,
107 DA_FLAG_WAS_OTAG = 0x000040,
108 DA_FLAG_RETRY_UA = 0x000080,
109 DA_FLAG_OPEN = 0x000100,
110 DA_FLAG_SCTX_INIT = 0x000200,
111 DA_FLAG_CAN_RC16 = 0x000400,
112 DA_FLAG_PROBED = 0x000800,
113 DA_FLAG_DIRTY = 0x001000,
114 DA_FLAG_ANNOUNCED = 0x002000,
115 DA_FLAG_CAN_ATA_DMA = 0x004000,
116 DA_FLAG_CAN_ATA_LOG = 0x008000,
117 DA_FLAG_CAN_ATA_IDLOG = 0x010000,
118 DA_FLAG_CAN_ATA_SUPCAP = 0x020000,
119 DA_FLAG_CAN_ATA_ZONE = 0x040000
124 DA_Q_NO_SYNC_CACHE = 0x01,
125 DA_Q_NO_6_BYTE = 0x02,
126 DA_Q_NO_PREVENT = 0x04,
129 DA_Q_NO_UNMAP = 0x20,
130 DA_Q_RETRY_BUSY = 0x40,
132 DA_Q_STRICT_UNMAP = 0x100
135 #define DA_Q_BIT_STRING \
137 "\001NO_SYNC_CACHE" \
148 DA_CCB_PROBE_RC = 0x01,
149 DA_CCB_PROBE_RC16 = 0x02,
150 DA_CCB_PROBE_LBP = 0x03,
151 DA_CCB_PROBE_BLK_LIMITS = 0x04,
152 DA_CCB_PROBE_BDC = 0x05,
153 DA_CCB_PROBE_ATA = 0x06,
154 DA_CCB_BUFFER_IO = 0x07,
156 DA_CCB_DELETE = 0x0B,
158 DA_CCB_PROBE_ZONE = 0x0D,
159 DA_CCB_PROBE_ATA_LOGDIR = 0x0E,
160 DA_CCB_PROBE_ATA_IDDIR = 0x0F,
161 DA_CCB_PROBE_ATA_SUP = 0x10,
162 DA_CCB_PROBE_ATA_ZONE = 0x11,
163 DA_CCB_PROBE_WP = 0x12,
164 DA_CCB_TYPE_MASK = 0x1F,
165 DA_CCB_RETRY_UA = 0x20
169 * Order here is important for method choice
171 * We prefer ATA_TRIM as tests run against a Sandforce 2281 SSD attached to
172 * LSI 2008 (mps) controller (FW: v12, Drv: v14) resulted 20% quicker deletes
173 * using ATA_TRIM than the corresponding UNMAP results for a real world mysql
174 * import taking 5mins.
185 DA_DELETE_MIN = DA_DELETE_ATA_TRIM,
186 DA_DELETE_MAX = DA_DELETE_ZERO
190 * For SCSI, host managed drives show up as a separate device type. For
191 * ATA, host managed drives also have a different device signature.
192 * XXX KDM figure out the ATA host managed signature.
196 DA_ZONE_DRIVE_MANAGED = 0x01,
197 DA_ZONE_HOST_AWARE = 0x02,
198 DA_ZONE_HOST_MANAGED = 0x03
202 * We distinguish between these interface cases in addition to the drive type:
203 * o ATA drive behind a SCSI translation layer that knows about ZBC/ZAC
204 * o ATA drive behind a SCSI translation layer that does not know about
205 * ZBC/ZAC, and so needs to be managed via ATA passthrough. In this
206 * case, we would need to share the ATA code with the ada(4) driver.
216 DA_ZONE_FLAG_RZ_SUP = 0x0001,
217 DA_ZONE_FLAG_OPEN_SUP = 0x0002,
218 DA_ZONE_FLAG_CLOSE_SUP = 0x0004,
219 DA_ZONE_FLAG_FINISH_SUP = 0x0008,
220 DA_ZONE_FLAG_RWP_SUP = 0x0010,
221 DA_ZONE_FLAG_SUP_MASK = (DA_ZONE_FLAG_RZ_SUP |
222 DA_ZONE_FLAG_OPEN_SUP |
223 DA_ZONE_FLAG_CLOSE_SUP |
224 DA_ZONE_FLAG_FINISH_SUP |
225 DA_ZONE_FLAG_RWP_SUP),
226 DA_ZONE_FLAG_URSWRZ = 0x0020,
227 DA_ZONE_FLAG_OPT_SEQ_SET = 0x0040,
228 DA_ZONE_FLAG_OPT_NONSEQ_SET = 0x0080,
229 DA_ZONE_FLAG_MAX_SEQ_SET = 0x0100,
230 DA_ZONE_FLAG_SET_MASK = (DA_ZONE_FLAG_OPT_SEQ_SET |
231 DA_ZONE_FLAG_OPT_NONSEQ_SET |
232 DA_ZONE_FLAG_MAX_SEQ_SET)
235 static struct da_zone_desc {
238 } da_zone_desc_table[] = {
239 {DA_ZONE_FLAG_RZ_SUP, "Report Zones" },
240 {DA_ZONE_FLAG_OPEN_SUP, "Open" },
241 {DA_ZONE_FLAG_CLOSE_SUP, "Close" },
242 {DA_ZONE_FLAG_FINISH_SUP, "Finish" },
243 {DA_ZONE_FLAG_RWP_SUP, "Reset Write Pointer" },
246 typedef void da_delete_func_t (struct cam_periph *periph, union ccb *ccb,
248 static da_delete_func_t da_delete_trim;
249 static da_delete_func_t da_delete_unmap;
250 static da_delete_func_t da_delete_ws;
252 static const void * da_delete_functions[] = {
262 static const char *da_delete_method_names[] =
263 { "NONE", "DISABLE", "ATA_TRIM", "UNMAP", "WS16", "WS10", "ZERO" };
264 static const char *da_delete_method_desc[] =
265 { "NONE", "DISABLED", "ATA TRIM", "UNMAP", "WRITE SAME(16) with UNMAP",
266 "WRITE SAME(10) with UNMAP", "ZERO" };
268 /* Offsets into our private area for storing information */
269 #define ccb_state ppriv_field0
270 #define ccb_bp ppriv_ptr1
275 u_int8_t secs_per_track;
276 u_int32_t secsize; /* Number of bytes/sector */
277 u_int64_t sectors; /* total number sectors */
282 #define UNMAP_RANGE_MAX 0xffffffff
283 #define UNMAP_HEAD_SIZE 8
284 #define UNMAP_RANGE_SIZE 16
285 #define UNMAP_MAX_RANGES 2048 /* Protocol Max is 4095 */
286 #define UNMAP_BUF_SIZE ((UNMAP_MAX_RANGES * UNMAP_RANGE_SIZE) + \
289 #define WS10_MAX_BLKS 0xffff
290 #define WS16_MAX_BLKS 0xffffffff
291 #define ATA_TRIM_MAX_RANGES ((UNMAP_BUF_SIZE / \
292 (ATA_DSM_RANGE_SIZE * ATA_DSM_BLK_SIZE)) * ATA_DSM_BLK_SIZE)
294 #define DA_WORK_TUR (1 << 16)
305 DA_REF_MAX /* KEEP LAST */
309 struct cam_iosched_softc *cam_iosched;
310 struct bio_queue_head delete_run_queue;
311 LIST_HEAD(, ccb_hdr) pending_ccbs;
312 int refcount; /* Active xpt_action() calls */
316 int minimum_cmd_size;
319 int delete_available; /* Delete methods possibly available */
320 da_zone_mode zone_mode;
321 da_zone_interface zone_interface;
322 da_zone_flags zone_flags;
323 struct ata_gp_log_dir ata_logdir;
324 int valid_logdir_len;
325 struct ata_identify_log_pages ata_iddir;
327 uint64_t optimal_seq_zones;
328 uint64_t optimal_nonseq_zones;
329 uint64_t max_seq_zones;
331 uint32_t unmap_max_ranges;
332 uint32_t unmap_max_lba; /* Max LBAs in UNMAP req */
334 uint32_t unmap_gran_align;
335 uint64_t ws_max_blks;
336 da_delete_methods delete_method_pref;
337 da_delete_methods delete_method;
338 da_delete_func_t *delete_func;
341 struct disk_params params;
344 struct task sysctl_task;
345 struct sysctl_ctx_list sysctl_ctx;
346 struct sysctl_oid *sysctl_tree;
347 struct callout sendordered_c;
349 uint8_t unmap_buf[UNMAP_BUF_SIZE];
350 struct scsi_read_capacity_data_long rcaplong;
351 struct callout mediapoll_c;
352 int ref_flags[DA_REF_MAX];
354 struct sysctl_ctx_list sysctl_stats_ctx;
355 struct sysctl_oid *sysctl_stats_tree;
360 #define DA_ANNOUNCETMP_SZ 80
361 char announce_temp[DA_ANNOUNCETMP_SZ];
362 #define DA_ANNOUNCE_SZ 400
363 char announcebuf[DA_ANNOUNCE_SZ];
366 #define dadeleteflag(softc, delete_method, enable) \
368 softc->delete_available |= (1 << delete_method); \
370 softc->delete_available &= ~(1 << delete_method); \
373 struct da_quirk_entry {
374 struct scsi_inquiry_pattern inq_pat;
378 static const char quantum[] = "QUANTUM";
379 static const char microp[] = "MICROP";
381 static struct da_quirk_entry da_quirk_table[] =
383 /* SPI, FC devices */
386 * Fujitsu M2513A MO drives.
387 * Tested devices: M2513A2 firmware versions 1200 & 1300.
388 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
389 * Reported by: W.Scholten <whs@xs4all.nl>
391 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
392 /*quirks*/ DA_Q_NO_SYNC_CACHE
396 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
397 /*quirks*/ DA_Q_NO_SYNC_CACHE
401 * This particular Fujitsu drive doesn't like the
402 * synchronize cache command.
403 * Reported by: Tom Jackson <toj@gorilla.net>
405 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
406 /*quirks*/ DA_Q_NO_SYNC_CACHE
410 * This drive doesn't like the synchronize cache command
411 * either. Reported by: Matthew Jacob <mjacob@feral.com>
412 * in NetBSD PR kern/6027, August 24, 1998.
414 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
415 /*quirks*/ DA_Q_NO_SYNC_CACHE
419 * This drive doesn't like the synchronize cache command
420 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
423 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
424 /*quirks*/ DA_Q_NO_SYNC_CACHE
428 * Doesn't like the synchronize cache command.
429 * Reported by: Blaz Zupan <blaz@gold.amis.net>
431 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
432 /*quirks*/ DA_Q_NO_SYNC_CACHE
436 * Doesn't like the synchronize cache command.
437 * Reported by: Blaz Zupan <blaz@gold.amis.net>
439 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
440 /*quirks*/ DA_Q_NO_SYNC_CACHE
444 * Doesn't like the synchronize cache command.
446 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
447 /*quirks*/ DA_Q_NO_SYNC_CACHE
451 * Doesn't like the synchronize cache command.
452 * Reported by: walter@pelissero.de
454 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
455 /*quirks*/ DA_Q_NO_SYNC_CACHE
459 * Doesn't work correctly with 6 byte reads/writes.
460 * Returns illegal request, and points to byte 9 of the
462 * Reported by: Adam McDougall <bsdx@spawnet.com>
464 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
465 /*quirks*/ DA_Q_NO_6_BYTE
469 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
470 /*quirks*/ DA_Q_NO_6_BYTE
474 * Doesn't like the synchronize cache command.
475 * Reported by: walter@pelissero.de
477 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
478 /*quirks*/ DA_Q_NO_SYNC_CACHE
482 * The CISS RAID controllers do not support SYNC_CACHE
484 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
485 /*quirks*/ DA_Q_NO_SYNC_CACHE
489 * The STEC SSDs sometimes hang on UNMAP.
491 {T_DIRECT, SIP_MEDIA_FIXED, "STEC", "*", "*"},
492 /*quirks*/ DA_Q_NO_UNMAP
496 * VMware returns BUSY status when storage has transient
497 * connectivity problems, so better wait.
498 * Also VMware returns odd errors on misaligned UNMAPs.
500 {T_DIRECT, SIP_MEDIA_FIXED, "VMware*", "*", "*"},
501 /*quirks*/ DA_Q_RETRY_BUSY | DA_Q_STRICT_UNMAP
503 /* USB mass storage devices supported by umass(4) */
506 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
509 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
510 /*quirks*/ DA_Q_NO_SYNC_CACHE
514 * Power Quotient Int. (PQI) USB flash key
517 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
518 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
522 * Creative Nomad MUVO mp3 player (USB)
525 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
526 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
530 * Jungsoft NEXDISK USB flash key
533 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
534 /*quirks*/ DA_Q_NO_SYNC_CACHE
538 * FreeDik USB Mini Data Drive
541 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
542 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
546 * Sigmatel USB Flash MP3 Player
549 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
550 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
554 * Neuros USB Digital Audio Computer
557 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
558 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
562 * SEAGRAND NP-900 MP3 Player
565 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
566 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
570 * iRiver iFP MP3 player (with UMS Firmware)
571 * PR: kern/54881, i386/63941, kern/66124
573 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
574 /*quirks*/ DA_Q_NO_SYNC_CACHE
578 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
581 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
582 /*quirks*/ DA_Q_NO_SYNC_CACHE
586 * ZICPlay USB MP3 Player with FM
589 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
590 /*quirks*/ DA_Q_NO_SYNC_CACHE
594 * TEAC USB floppy mechanisms
596 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
597 /*quirks*/ DA_Q_NO_SYNC_CACHE
601 * Kingston DataTraveler II+ USB Pen-Drive.
602 * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
604 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
605 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
613 {T_DIRECT, SIP_MEDIA_REMOVABLE, " ", "USB DISK Pro", "PMAP"},
614 /*quirks*/ DA_Q_NO_SYNC_CACHE
618 * Motorola E398 Mobile Phone (TransFlash memory card).
619 * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
622 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
623 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
627 * Qware BeatZkey! Pro
630 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
631 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
635 * Time DPA20B 1GB MP3 Player
638 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
639 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
643 * Samsung USB key 128Mb
646 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
647 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
651 * Kingston DataTraveler 2.0 USB Flash memory.
654 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
655 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
659 * Creative MUVO Slim mp3 player (USB)
662 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
663 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
667 * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
670 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
671 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
675 * SanDisk Micro Cruzer 128MB
678 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
679 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
683 * TOSHIBA TransMemory USB sticks
686 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
687 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
691 * PNY USB 3.0 Flash Drives
693 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PNY", "USB 3.0 FD*",
694 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_RC16
699 * PR: usb/75578, usb/72344, usb/65436
701 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
702 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
708 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
709 "120?"}, /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_4K | DA_Q_NO_RC16
713 * Genesys 6-in-1 Card Reader
716 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
717 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
721 * Rekam Digital CAMERA
724 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
725 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
729 * iRiver H10 MP3 player
732 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
733 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
737 * iRiver U10 MP3 player
740 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
741 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
748 {T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
749 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
753 * EasyMP3 EM732X USB 2.0 Flash MP3 Player
756 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
757 "1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
764 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
765 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
769 * Philips USB Key Audio KEY013
772 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
773 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
780 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
781 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
788 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
789 /*quirks*/ DA_Q_NO_SYNC_CACHE
793 * I/O Magic USB flash - Giga Bank
796 {T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
797 /*quirks*/ DA_Q_NO_SYNC_CACHE
801 * JoyFly 128mb USB Flash Drive
804 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
805 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
812 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
813 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
817 * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A
820 {T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*",
821 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
825 * Samsung YP-U3 mp3-player
828 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3",
829 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
832 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
833 "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
837 * Sony Cyber-Shot DSC cameras
840 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
841 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
844 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler G3",
845 "1.00"}, /*quirks*/ DA_Q_NO_PREVENT
848 /* At least several Transcent USB sticks lie on RC16. */
849 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JetFlash", "Transcend*",
850 "*"}, /*quirks*/ DA_Q_NO_RC16
854 * I-O Data USB Flash Disk
857 {T_DIRECT, SIP_MEDIA_REMOVABLE, "I-O DATA", "USB Flash Disk*",
858 "*"}, /*quirks*/ DA_Q_NO_RC16
860 /* ATA/SATA devices over SAS/USB/... */
862 /* Hitachi Advanced Format (4k) drives */
863 { T_DIRECT, SIP_MEDIA_FIXED, "Hitachi", "H??????????E3*", "*" },
867 /* Micron Advanced Format (4k) drives */
868 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Micron 5100 MTFDDAK*", "*" },
872 /* Samsung Advanced Format (4k) drives */
873 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD155UI*", "*" },
877 /* Samsung Advanced Format (4k) drives */
878 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD155UI*", "*" },
882 /* Samsung Advanced Format (4k) drives */
883 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD204UI*", "*" },
887 /* Samsung Advanced Format (4k) drives */
888 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD204UI*", "*" },
892 /* Seagate Barracuda Green Advanced Format (4k) drives */
893 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DL*", "*" },
897 /* Seagate Barracuda Green Advanced Format (4k) drives */
898 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DL", "*", "*" },
902 /* Seagate Barracuda Green Advanced Format (4k) drives */
903 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???DM*", "*" },
907 /* Seagate Barracuda Green Advanced Format (4k) drives */
908 { T_DIRECT, SIP_MEDIA_FIXED, "ST???DM*", "*", "*" },
912 /* Seagate Barracuda Green Advanced Format (4k) drives */
913 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DM*", "*" },
917 /* Seagate Barracuda Green Advanced Format (4k) drives */
918 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DM", "*", "*" },
922 /* Seagate Momentus Advanced Format (4k) drives */
923 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500423AS*", "*" },
927 /* Seagate Momentus Advanced Format (4k) drives */
928 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "3AS*", "*" },
932 /* Seagate Momentus Advanced Format (4k) drives */
933 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500424AS*", "*" },
937 /* Seagate Momentus Advanced Format (4k) drives */
938 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "4AS*", "*" },
942 /* Seagate Momentus Advanced Format (4k) drives */
943 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640423AS*", "*" },
947 /* Seagate Momentus Advanced Format (4k) drives */
948 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "3AS*", "*" },
952 /* Seagate Momentus Advanced Format (4k) drives */
953 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640424AS*", "*" },
957 /* Seagate Momentus Advanced Format (4k) drives */
958 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "4AS*", "*" },
962 /* Seagate Momentus Advanced Format (4k) drives */
963 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750420AS*", "*" },
967 /* Seagate Momentus Advanced Format (4k) drives */
968 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "0AS*", "*" },
972 /* Seagate Momentus Advanced Format (4k) drives */
973 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750422AS*", "*" },
977 /* Seagate Momentus Advanced Format (4k) drives */
978 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "2AS*", "*" },
982 /* Seagate Momentus Advanced Format (4k) drives */
983 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750423AS*", "*" },
987 /* Seagate Momentus Advanced Format (4k) drives */
988 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "3AS*", "*" },
992 /* Seagate Momentus Thin Advanced Format (4k) drives */
993 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???LT*", "*" },
997 /* Seagate Momentus Thin Advanced Format (4k) drives */
998 { T_DIRECT, SIP_MEDIA_FIXED, "ST???LT*", "*", "*" },
1002 /* WDC Caviar Green Advanced Format (4k) drives */
1003 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RS*", "*" },
1007 /* WDC Caviar Green Advanced Format (4k) drives */
1008 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RS*", "*" },
1012 /* WDC Caviar Green Advanced Format (4k) drives */
1013 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RX*", "*" },
1017 /* WDC Caviar Green Advanced Format (4k) drives */
1018 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RX*", "*" },
1022 /* WDC Caviar Green Advanced Format (4k) drives */
1023 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RS*", "*" },
1027 /* WDC Caviar Green Advanced Format (4k) drives */
1028 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RS*", "*" },
1032 /* WDC Caviar Green Advanced Format (4k) drives */
1033 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RX*", "*" },
1037 /* WDC Caviar Green Advanced Format (4k) drives */
1038 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RX*", "*" },
1042 /* WDC Scorpio Black Advanced Format (4k) drives */
1043 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PKT*", "*" },
1047 /* WDC Scorpio Black Advanced Format (4k) drives */
1048 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PKT*", "*" },
1052 /* WDC Scorpio Black Advanced Format (4k) drives */
1053 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PKT*", "*" },
1057 /* WDC Scorpio Black Advanced Format (4k) drives */
1058 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PKT*", "*" },
1062 /* WDC Scorpio Blue Advanced Format (4k) drives */
1063 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PVT*", "*" },
1067 /* WDC Scorpio Blue Advanced Format (4k) drives */
1068 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PVT*", "*" },
1072 /* WDC Scorpio Blue Advanced Format (4k) drives */
1073 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PVT*", "*" },
1077 /* WDC Scorpio Blue Advanced Format (4k) drives */
1078 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PVT*", "*" },
1083 * Olympus FE-210 camera
1085 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "FE210*",
1086 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
1090 * LG UP3S MP3 player
1092 {T_DIRECT, SIP_MEDIA_REMOVABLE, "LG", "UP3S",
1093 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
1097 * Laser MP3-2GA13 MP3 player
1099 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "(HS) Flash Disk",
1100 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
1104 * LaCie external 250GB Hard drive des by Porsche
1105 * Submitted by: Ben Stuyts <ben@altesco.nl>
1108 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HM250JI", "*"},
1109 /*quirks*/ DA_Q_NO_SYNC_CACHE
1114 * Corsair Force 2 SSDs
1115 * 4k optimised & trim only works in 4k requests + 4k aligned
1117 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair CSSD-F*", "*" },
1122 * Corsair Force 3 SSDs
1123 * 4k optimised & trim only works in 4k requests + 4k aligned
1125 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force 3*", "*" },
1130 * Corsair Neutron GTX SSDs
1131 * 4k optimised & trim only works in 4k requests + 4k aligned
1133 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Neutron GTX*", "*" },
1138 * Corsair Force GT & GS SSDs
1139 * 4k optimised & trim only works in 4k requests + 4k aligned
1141 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force G*", "*" },
1147 * 4k optimised & trim only works in 4k requests + 4k aligned
1149 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "M4-CT???M4SSD2*", "*" },
1154 * Crucial RealSSD C300 SSDs
1157 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "C300-CTFDDAC???MAG*",
1158 "*" }, /*quirks*/DA_Q_4K
1162 * Intel 320 Series SSDs
1163 * 4k optimised & trim only works in 4k requests + 4k aligned
1165 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2CW*", "*" },
1170 * Intel 330 Series SSDs
1171 * 4k optimised & trim only works in 4k requests + 4k aligned
1173 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2CT*", "*" },
1178 * Intel 510 Series SSDs
1179 * 4k optimised & trim only works in 4k requests + 4k aligned
1181 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2MH*", "*" },
1186 * Intel 520 Series SSDs
1187 * 4k optimised & trim only works in 4k requests + 4k aligned
1189 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2BW*", "*" },
1194 * Intel S3610 Series SSDs
1195 * 4k optimised & trim only works in 4k requests + 4k aligned
1197 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2BX*", "*" },
1202 * Intel X25-M Series SSDs
1203 * 4k optimised & trim only works in 4k requests + 4k aligned
1205 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2M*", "*" },
1210 * Kingston E100 Series SSDs
1211 * 4k optimised & trim only works in 4k requests + 4k aligned
1213 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SE100S3*", "*" },
1218 * Kingston HyperX 3k SSDs
1219 * 4k optimised & trim only works in 4k requests + 4k aligned
1221 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SH103S3*", "*" },
1226 * Marvell SSDs (entry taken from OpenSolaris)
1227 * 4k optimised & trim only works in 4k requests + 4k aligned
1229 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "MARVELL SD88SA02*", "*" },
1234 * OCZ Agility 2 SSDs
1235 * 4k optimised & trim only works in 4k requests + 4k aligned
1237 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY2*", "*" },
1242 * OCZ Agility 3 SSDs
1243 * 4k optimised & trim only works in 4k requests + 4k aligned
1245 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-AGILITY3*", "*" },
1250 * OCZ Deneva R Series SSDs
1251 * 4k optimised & trim only works in 4k requests + 4k aligned
1253 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "DENRSTE251M45*", "*" },
1258 * OCZ Vertex 2 SSDs (inc pro series)
1259 * 4k optimised & trim only works in 4k requests + 4k aligned
1261 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ?VERTEX2*", "*" },
1267 * 4k optimised & trim only works in 4k requests + 4k aligned
1269 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX3*", "*" },
1275 * 4k optimised & trim only works in 4k requests + 4k aligned
1277 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX4*", "*" },
1282 * Samsung 750 Series SSDs
1283 * 4k optimised & trim only works in 4k requests + 4k aligned
1285 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 750*", "*" },
1290 * Samsung 830 Series SSDs
1291 * 4k optimised & trim only works in 4k requests + 4k aligned
1293 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG SSD 830 Series*", "*" },
1299 * 4k optimised & trim only works in 4k requests + 4k aligned
1301 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 840*", "*" },
1307 * 4k optimised & trim only works in 4k requests + 4k aligned
1309 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 845*", "*" },
1315 * 4k optimised & trim only works in 4k requests + 4k aligned
1317 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 850*", "*" },
1322 * Samsung 843T Series SSDs (MZ7WD*)
1323 * Samsung PM851 Series SSDs (MZ7TE*)
1324 * Samsung PM853T Series SSDs (MZ7GE*)
1325 * Samsung SM863 Series SSDs (MZ7KM*)
1328 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG MZ7*", "*" },
1333 * Same as for SAMSUNG MZ7* but enable the quirks for SSD
1334 * starting with MZ7* too
1336 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "MZ7*", "*" },
1341 * SuperTalent TeraDrive CT SSDs
1342 * 4k optimised & trim only works in 4k requests + 4k aligned
1344 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "FTM??CT25H*", "*" },
1349 * XceedIOPS SATA SSDs
1352 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SG9XCS2D*", "*" },
1357 * Hama Innostor USB-Stick
1359 { T_DIRECT, SIP_MEDIA_REMOVABLE, "Innostor", "Innostor*", "*" },
1360 /*quirks*/DA_Q_NO_RC16
1364 * Seagate Lamarr 8TB Shingled Magnetic Recording (SMR)
1365 * Drive Managed SATA hard drive. This drive doesn't report
1366 * in firmware that it is a drive managed SMR drive.
1368 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST8000AS000[23]*", "*" },
1369 /*quirks*/DA_Q_SMR_DM
1373 * MX-ES USB Drive by Mach Xtreme
1375 { T_DIRECT, SIP_MEDIA_REMOVABLE, "MX", "MXUB3*", "*"},
1376 /*quirks*/DA_Q_NO_RC16
1380 static disk_strategy_t dastrategy;
1381 static dumper_t dadump;
1382 static periph_init_t dainit;
1383 static void daasync(void *callback_arg, u_int32_t code,
1384 struct cam_path *path, void *arg);
1385 static void dasysctlinit(void *context, int pending);
1386 static int dasysctlsofttimeout(SYSCTL_HANDLER_ARGS);
1387 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
1388 static int dadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
1389 static int dazonemodesysctl(SYSCTL_HANDLER_ARGS);
1390 static int dazonesupsysctl(SYSCTL_HANDLER_ARGS);
1391 static int dadeletemaxsysctl(SYSCTL_HANDLER_ARGS);
1392 static void dadeletemethodset(struct da_softc *softc,
1393 da_delete_methods delete_method);
1394 static off_t dadeletemaxsize(struct da_softc *softc,
1395 da_delete_methods delete_method);
1396 static void dadeletemethodchoose(struct da_softc *softc,
1397 da_delete_methods default_method);
1398 static void daprobedone(struct cam_periph *periph, union ccb *ccb);
1400 static periph_ctor_t daregister;
1401 static periph_dtor_t dacleanup;
1402 static periph_start_t dastart;
1403 static periph_oninv_t daoninvalidate;
1404 static void dazonedone(struct cam_periph *periph, union ccb *ccb);
1405 static void dadone(struct cam_periph *periph,
1406 union ccb *done_ccb);
1407 static int daerror(union ccb *ccb, u_int32_t cam_flags,
1408 u_int32_t sense_flags);
1409 static void daprevent(struct cam_periph *periph, int action);
1410 static void dareprobe(struct cam_periph *periph);
1411 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
1413 struct scsi_read_capacity_data_long *rcaplong,
1415 static timeout_t dasendorderedtag;
1416 static void dashutdown(void *arg, int howto);
1417 static timeout_t damediapoll;
1419 #ifndef DA_DEFAULT_POLL_PERIOD
1420 #define DA_DEFAULT_POLL_PERIOD 3
1423 #ifndef DA_DEFAULT_TIMEOUT
1424 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
1427 #ifndef DA_DEFAULT_SOFTTIMEOUT
1428 #define DA_DEFAULT_SOFTTIMEOUT 0
1431 #ifndef DA_DEFAULT_RETRY
1432 #define DA_DEFAULT_RETRY 4
1435 #ifndef DA_DEFAULT_SEND_ORDERED
1436 #define DA_DEFAULT_SEND_ORDERED 1
1439 static int da_poll_period = DA_DEFAULT_POLL_PERIOD;
1440 static int da_retry_count = DA_DEFAULT_RETRY;
1441 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
1442 static sbintime_t da_default_softtimeout = DA_DEFAULT_SOFTTIMEOUT;
1443 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
1445 static SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
1446 "CAM Direct Access Disk driver");
1447 SYSCTL_INT(_kern_cam_da, OID_AUTO, poll_period, CTLFLAG_RWTUN,
1448 &da_poll_period, 0, "Media polling period in seconds");
1449 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RWTUN,
1450 &da_retry_count, 0, "Normal I/O retry count");
1451 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RWTUN,
1452 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
1453 SYSCTL_INT(_kern_cam_da, OID_AUTO, send_ordered, CTLFLAG_RWTUN,
1454 &da_send_ordered, 0, "Send Ordered Tags");
1456 SYSCTL_PROC(_kern_cam_da, OID_AUTO, default_softtimeout,
1457 CTLTYPE_UINT | CTLFLAG_RW, NULL, 0, dasysctlsofttimeout, "I",
1458 "Soft I/O timeout (ms)");
1459 TUNABLE_INT64("kern.cam.da.default_softtimeout", &da_default_softtimeout);
1462 * DA_ORDEREDTAG_INTERVAL determines how often, relative
1463 * to the default timeout, we check to see whether an ordered
1464 * tagged transaction is appropriate to prevent simple tag
1465 * starvation. Since we'd like to ensure that there is at least
1466 * 1/2 of the timeout length left for a starved transaction to
1467 * complete after we've sent an ordered tag, we must poll at least
1468 * four times in every timeout period. This takes care of the worst
1469 * case where a starved transaction starts during an interval that
1470 * meets the requirement "don't send an ordered tag" test so it takes
1471 * us two intervals to determine that a tag must be sent.
1473 #ifndef DA_ORDEREDTAG_INTERVAL
1474 #define DA_ORDEREDTAG_INTERVAL 4
1477 static struct periph_driver dadriver =
1480 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
1483 PERIPHDRIVER_DECLARE(da, dadriver);
1485 static MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
1488 * This driver takes out references / holds in well defined pairs, never
1489 * recursively. These macros / inline functions enforce those rules. They
1490 * are only enabled with DA_TRACK_REFS or INVARIANTS. If DA_TRACK_REFS is
1491 * defined to be 2 or larger, the tracking also includes debug printfs.
1493 #if defined(DA_TRACK_REFS) || defined(INVARIANTS)
1495 #ifndef DA_TRACK_REFS
1496 #define DA_TRACK_REFS 1
1499 #if DA_TRACK_REFS > 1
1500 static const char *da_ref_text[] = {
1513 #define DA_PERIPH_PRINT(periph, msg, args...) \
1514 CAM_PERIPH_PRINT(periph, msg, ##args)
1516 #define DA_PERIPH_PRINT(periph, msg, args...)
1520 token_sanity(da_ref_token token)
1522 if ((unsigned)token >= DA_REF_MAX)
1523 panic("Bad token value passed in %d\n", token);
1527 da_periph_hold(struct cam_periph *periph, int priority, da_ref_token token)
1529 int err = cam_periph_hold(periph, priority);
1531 token_sanity(token);
1532 DA_PERIPH_PRINT(periph, "Holding device %s (%d): %d\n",
1533 da_ref_text[token], token, err);
1536 struct da_softc *softc = periph->softc;
1538 cnt = atomic_fetchadd_int(&softc->ref_flags[token], 1);
1540 panic("Re-holding for reason %d, cnt = %d", token, cnt);
1546 da_periph_unhold(struct cam_periph *periph, da_ref_token token)
1549 struct da_softc *softc = periph->softc;
1551 token_sanity(token);
1552 DA_PERIPH_PRINT(periph, "Unholding device %s (%d)\n",
1553 da_ref_text[token], token);
1554 cnt = atomic_fetchadd_int(&softc->ref_flags[token], -1);
1556 panic("Unholding %d with cnt = %d", token, cnt);
1557 cam_periph_unhold(periph);
1561 da_periph_acquire(struct cam_periph *periph, da_ref_token token)
1563 int err = cam_periph_acquire(periph);
1565 token_sanity(token);
1566 DA_PERIPH_PRINT(periph, "acquiring device %s (%d): %d\n",
1567 da_ref_text[token], token, err);
1570 struct da_softc *softc = periph->softc;
1572 cnt = atomic_fetchadd_int(&softc->ref_flags[token], 1);
1574 panic("Re-refing for reason %d, cnt = %d", token, cnt);
1580 da_periph_release(struct cam_periph *periph, da_ref_token token)
1583 struct da_softc *softc = periph->softc;
1585 token_sanity(token);
1586 DA_PERIPH_PRINT(periph, "releasing device %s (%d)\n",
1587 da_ref_text[token], token);
1588 cnt = atomic_fetchadd_int(&softc->ref_flags[token], -1);
1590 panic("Releasing %d with cnt = %d", token, cnt);
1591 cam_periph_release(periph);
1595 da_periph_release_locked(struct cam_periph *periph, da_ref_token token)
1598 struct da_softc *softc = periph->softc;
1600 token_sanity(token);
1601 DA_PERIPH_PRINT(periph, "releasing device (locked) %s (%d)\n",
1602 da_ref_text[token], token);
1603 cnt = atomic_fetchadd_int(&softc->ref_flags[token], -1);
1605 panic("Unholding %d with cnt = %d", token, cnt);
1606 cam_periph_release_locked(periph);
1609 #define cam_periph_hold POISON
1610 #define cam_periph_unhold POISON
1611 #define cam_periph_acquire POISON
1612 #define cam_periph_release POISON
1613 #define cam_periph_release_locked POISON
1616 #define da_periph_hold(periph, prio, token) cam_periph_hold((periph), (prio))
1617 #define da_periph_unhold(periph, token) cam_periph_unhold((periph))
1618 #define da_periph_acquire(periph, token) cam_periph_acquire((periph))
1619 #define da_periph_release(periph, token) cam_periph_release((periph))
1620 #define da_periph_release_locked(periph, token) cam_periph_release_locked((periph))
1624 daopen(struct disk *dp)
1626 struct cam_periph *periph;
1627 struct da_softc *softc;
1630 periph = (struct cam_periph *)dp->d_drv1;
1631 if (da_periph_acquire(periph, DA_REF_OPEN) != 0) {
1635 cam_periph_lock(periph);
1636 if ((error = da_periph_hold(periph, PRIBIO|PCATCH, DA_REF_OPEN_HOLD)) != 0) {
1637 cam_periph_unlock(periph);
1638 da_periph_release(periph, DA_REF_OPEN);
1642 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1645 softc = (struct da_softc *)periph->softc;
1648 /* Wait for the disk size update. */
1649 error = cam_periph_sleep(periph, &softc->disk->d_mediasize, PRIBIO,
1652 xpt_print(periph->path, "unable to retrieve capacity data\n");
1654 if (periph->flags & CAM_PERIPH_INVALID)
1657 if (error == 0 && (softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1658 (softc->quirks & DA_Q_NO_PREVENT) == 0)
1659 daprevent(periph, PR_PREVENT);
1662 softc->flags &= ~DA_FLAG_PACK_INVALID;
1663 softc->flags |= DA_FLAG_OPEN;
1666 da_periph_unhold(periph, DA_REF_OPEN_HOLD);
1667 cam_periph_unlock(periph);
1670 da_periph_release(periph, DA_REF_OPEN);
1676 daclose(struct disk *dp)
1678 struct cam_periph *periph;
1679 struct da_softc *softc;
1682 periph = (struct cam_periph *)dp->d_drv1;
1683 softc = (struct da_softc *)periph->softc;
1684 cam_periph_lock(periph);
1685 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1688 if (da_periph_hold(periph, PRIBIO, DA_REF_CLOSE_HOLD) == 0) {
1690 /* Flush disk cache. */
1691 if ((softc->flags & DA_FLAG_DIRTY) != 0 &&
1692 (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0 &&
1693 (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
1694 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1695 scsi_synchronize_cache(&ccb->csio, /*retries*/1,
1696 /*cbfcnp*/dadone, MSG_SIMPLE_Q_TAG,
1697 /*begin_lba*/0, /*lb_count*/0, SSD_FULL_SIZE,
1699 cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
1700 /*sense_flags*/SF_RETRY_UA | SF_QUIET_IR,
1701 softc->disk->d_devstat);
1702 softc->flags &= ~DA_FLAG_DIRTY;
1703 xpt_release_ccb(ccb);
1706 /* Allow medium removal. */
1707 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1708 (softc->quirks & DA_Q_NO_PREVENT) == 0)
1709 daprevent(periph, PR_ALLOW);
1711 da_periph_unhold(periph, DA_REF_CLOSE_HOLD);
1715 * If we've got removeable media, mark the blocksize as
1716 * unavailable, since it could change when new media is
1719 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0)
1720 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1722 softc->flags &= ~DA_FLAG_OPEN;
1723 while (softc->refcount != 0)
1724 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "daclose", 1);
1725 cam_periph_unlock(periph);
1726 da_periph_release(periph, DA_REF_OPEN);
1731 daschedule(struct cam_periph *periph)
1733 struct da_softc *softc = (struct da_softc *)periph->softc;
1735 if (softc->state != DA_STATE_NORMAL)
1738 cam_iosched_schedule(softc->cam_iosched, periph);
1742 * Actually translate the requested transfer into one the physical driver
1743 * can understand. The transfer is described by a buf and will include
1744 * only one physical transfer.
1747 dastrategy(struct bio *bp)
1749 struct cam_periph *periph;
1750 struct da_softc *softc;
1752 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1753 softc = (struct da_softc *)periph->softc;
1755 cam_periph_lock(periph);
1758 * If the device has been made invalid, error out
1760 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1761 cam_periph_unlock(periph);
1762 biofinish(bp, NULL, ENXIO);
1766 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
1769 * Zone commands must be ordered, because they can depend on the
1770 * effects of previously issued commands, and they may affect
1771 * commands after them.
1773 if (bp->bio_cmd == BIO_ZONE)
1774 bp->bio_flags |= BIO_ORDERED;
1777 * Place it in the queue of disk activities for this disk
1779 cam_iosched_queue_work(softc->cam_iosched, bp);
1782 * Schedule ourselves for performing the work.
1785 cam_periph_unlock(periph);
1791 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1793 struct cam_periph *periph;
1794 struct da_softc *softc;
1796 struct ccb_scsiio csio;
1801 periph = dp->d_drv1;
1802 softc = (struct da_softc *)periph->softc;
1803 secsize = softc->params.secsize;
1805 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0)
1808 memset(&csio, 0, sizeof(csio));
1810 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1811 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1812 scsi_read_write(&csio,
1816 /*read*/SCSI_RW_WRITE,
1818 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1821 /*data_ptr*/(u_int8_t *) virtual,
1822 /*dxfer_len*/length,
1823 /*sense_len*/SSD_FULL_SIZE,
1824 da_default_timeout * 1000);
1825 error = cam_periph_runccb((union ccb *)&csio, cam_periph_error,
1826 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1828 printf("Aborting dump due to I/O error.\n");
1833 * Sync the disk cache contents to the physical media.
1835 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1837 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1838 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1839 scsi_synchronize_cache(&csio,
1843 /*begin_lba*/0,/* Cover the whole disk */
1847 error = cam_periph_runccb((union ccb *)&csio, cam_periph_error,
1848 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1850 xpt_print(periph->path, "Synchronize cache failed\n");
1856 dagetattr(struct bio *bp)
1859 struct cam_periph *periph;
1861 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1862 cam_periph_lock(periph);
1863 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1865 cam_periph_unlock(periph);
1867 bp->bio_completed = bp->bio_length;
1877 * Install a global async callback. This callback will
1878 * receive async callbacks like "new device found".
1880 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1882 if (status != CAM_REQ_CMP) {
1883 printf("da: Failed to attach master async callback "
1884 "due to status 0x%x!\n", status);
1885 } else if (da_send_ordered) {
1887 /* Register our shutdown event handler */
1888 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1889 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1890 printf("dainit: shutdown event registration failed!\n");
1895 * Callback from GEOM, called when it has finished cleaning up its
1899 dadiskgonecb(struct disk *dp)
1901 struct cam_periph *periph;
1903 periph = (struct cam_periph *)dp->d_drv1;
1904 da_periph_release(periph, DA_REF_GEOM);
1908 daoninvalidate(struct cam_periph *periph)
1910 struct da_softc *softc;
1912 cam_periph_assert(periph, MA_OWNED);
1913 softc = (struct da_softc *)periph->softc;
1916 * De-register any async callbacks.
1918 xpt_register_async(0, daasync, periph, periph->path);
1920 softc->flags |= DA_FLAG_PACK_INVALID;
1922 softc->invalidations++;
1926 * Return all queued I/O with ENXIO.
1927 * XXX Handle any transactions queued to the card
1928 * with XPT_ABORT_CCB.
1930 cam_iosched_flush(softc->cam_iosched, NULL, ENXIO);
1933 * Tell GEOM that we've gone away, we'll get a callback when it is
1934 * done cleaning up its resources.
1936 disk_gone(softc->disk);
1940 dacleanup(struct cam_periph *periph)
1942 struct da_softc *softc;
1944 softc = (struct da_softc *)periph->softc;
1946 cam_periph_unlock(periph);
1948 cam_iosched_fini(softc->cam_iosched);
1951 * If we can't free the sysctl tree, oh well...
1953 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0) {
1955 if (sysctl_ctx_free(&softc->sysctl_stats_ctx) != 0)
1956 xpt_print(periph->path,
1957 "can't remove sysctl stats context\n");
1959 if (sysctl_ctx_free(&softc->sysctl_ctx) != 0)
1960 xpt_print(periph->path,
1961 "can't remove sysctl context\n");
1964 callout_drain(&softc->mediapoll_c);
1965 disk_destroy(softc->disk);
1966 callout_drain(&softc->sendordered_c);
1967 free(softc, M_DEVBUF);
1968 cam_periph_lock(periph);
1972 daasync(void *callback_arg, u_int32_t code,
1973 struct cam_path *path, void *arg)
1975 struct cam_periph *periph;
1976 struct da_softc *softc;
1978 periph = (struct cam_periph *)callback_arg;
1980 case AC_FOUND_DEVICE:
1982 struct ccb_getdev *cgd;
1985 cgd = (struct ccb_getdev *)arg;
1989 if (cgd->protocol != PROTO_SCSI)
1991 if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED)
1993 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1994 && SID_TYPE(&cgd->inq_data) != T_RBC
1995 && SID_TYPE(&cgd->inq_data) != T_OPTICAL
1996 && SID_TYPE(&cgd->inq_data) != T_ZBC_HM)
2000 * Allocate a peripheral instance for
2001 * this device and start the probe
2004 status = cam_periph_alloc(daregister, daoninvalidate,
2006 "da", CAM_PERIPH_BIO,
2008 AC_FOUND_DEVICE, cgd);
2010 if (status != CAM_REQ_CMP
2011 && status != CAM_REQ_INPROG)
2012 printf("daasync: Unable to attach to new device "
2013 "due to status 0x%x\n", status);
2016 case AC_ADVINFO_CHANGED:
2020 buftype = (uintptr_t)arg;
2021 if (buftype == CDAI_TYPE_PHYS_PATH) {
2022 struct da_softc *softc;
2024 softc = periph->softc;
2025 disk_attr_changed(softc->disk, "GEOM::physpath",
2030 case AC_UNIT_ATTENTION:
2033 int error_code, sense_key, asc, ascq;
2035 softc = (struct da_softc *)periph->softc;
2036 ccb = (union ccb *)arg;
2039 * Handle all UNIT ATTENTIONs except our own,
2040 * as they will be handled by daerror().
2042 cam_periph_lock(periph);
2043 if (xpt_path_periph(ccb->ccb_h.path) != periph &&
2044 scsi_extract_sense_ccb(ccb,
2045 &error_code, &sense_key, &asc, &ascq)) {
2046 if (asc == 0x2A && ascq == 0x09) {
2047 xpt_print(ccb->ccb_h.path,
2048 "Capacity data has changed\n");
2049 softc->flags &= ~DA_FLAG_PROBED;
2051 } else if (asc == 0x28 && ascq == 0x00) {
2052 softc->flags &= ~DA_FLAG_PROBED;
2053 disk_media_changed(softc->disk, M_NOWAIT);
2054 } else if (asc == 0x3F && ascq == 0x03) {
2055 xpt_print(ccb->ccb_h.path,
2056 "INQUIRY data has changed\n");
2057 softc->flags &= ~DA_FLAG_PROBED;
2061 cam_periph_unlock(periph);
2065 softc = (struct da_softc *)periph->softc;
2066 cam_periph_lock(periph);
2067 if (!cam_iosched_has_work_flags(softc->cam_iosched, DA_WORK_TUR)) {
2068 if (da_periph_acquire(periph, DA_REF_TUR) == 0) {
2069 cam_iosched_set_work_flags(softc->cam_iosched, DA_WORK_TUR);
2073 cam_periph_unlock(periph);
2078 struct ccb_hdr *ccbh;
2080 softc = (struct da_softc *)periph->softc;
2082 * Don't fail on the expected unit attention
2085 cam_periph_lock(periph);
2086 softc->flags |= DA_FLAG_RETRY_UA;
2087 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
2088 ccbh->ccb_state |= DA_CCB_RETRY_UA;
2089 cam_periph_unlock(periph);
2092 case AC_INQ_CHANGED:
2093 cam_periph_lock(periph);
2094 softc = (struct da_softc *)periph->softc;
2095 softc->flags &= ~DA_FLAG_PROBED;
2097 cam_periph_unlock(periph);
2102 cam_periph_async(periph, code, path, arg);
2106 dasysctlinit(void *context, int pending)
2108 struct cam_periph *periph;
2109 struct da_softc *softc;
2110 char tmpstr[32], tmpstr2[16];
2111 struct ccb_trans_settings cts;
2113 periph = (struct cam_periph *)context;
2115 * periph was held for us when this task was enqueued
2117 if (periph->flags & CAM_PERIPH_INVALID) {
2118 da_periph_release(periph, DA_REF_SYSCTL);
2122 softc = (struct da_softc *)periph->softc;
2123 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
2124 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
2126 sysctl_ctx_init(&softc->sysctl_ctx);
2127 cam_periph_lock(periph);
2128 softc->flags |= DA_FLAG_SCTX_INIT;
2129 cam_periph_unlock(periph);
2130 softc->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&softc->sysctl_ctx,
2131 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
2132 CTLFLAG_RD, 0, tmpstr, "device_index");
2133 if (softc->sysctl_tree == NULL) {
2134 printf("dasysctlinit: unable to allocate sysctl tree\n");
2135 da_periph_release(periph, DA_REF_SYSCTL);
2140 * Now register the sysctl handler, so the user can change the value on
2143 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2144 OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RWTUN,
2145 softc, 0, dadeletemethodsysctl, "A",
2146 "BIO_DELETE execution method");
2147 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2148 OID_AUTO, "delete_max", CTLTYPE_U64 | CTLFLAG_RW,
2149 softc, 0, dadeletemaxsysctl, "Q",
2150 "Maximum BIO_DELETE size");
2151 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2152 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
2153 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
2154 "Minimum CDB size");
2156 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2157 OID_AUTO, "zone_mode", CTLTYPE_STRING | CTLFLAG_RD,
2158 softc, 0, dazonemodesysctl, "A",
2160 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2161 OID_AUTO, "zone_support", CTLTYPE_STRING | CTLFLAG_RD,
2162 softc, 0, dazonesupsysctl, "A",
2164 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2165 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
2166 "optimal_seq_zones", CTLFLAG_RD, &softc->optimal_seq_zones,
2167 "Optimal Number of Open Sequential Write Preferred Zones");
2168 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2169 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
2170 "optimal_nonseq_zones", CTLFLAG_RD,
2171 &softc->optimal_nonseq_zones,
2172 "Optimal Number of Non-Sequentially Written Sequential Write "
2174 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2175 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
2176 "max_seq_zones", CTLFLAG_RD, &softc->max_seq_zones,
2177 "Maximum Number of Open Sequential Write Required Zones");
2179 SYSCTL_ADD_INT(&softc->sysctl_ctx,
2180 SYSCTL_CHILDREN(softc->sysctl_tree),
2184 &softc->error_inject,
2186 "error_inject leaf");
2188 SYSCTL_ADD_INT(&softc->sysctl_ctx,
2189 SYSCTL_CHILDREN(softc->sysctl_tree),
2195 "Unmapped I/O leaf");
2197 SYSCTL_ADD_INT(&softc->sysctl_ctx,
2198 SYSCTL_CHILDREN(softc->sysctl_tree),
2207 * Add some addressing info.
2209 memset(&cts, 0, sizeof (cts));
2210 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
2211 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2212 cts.type = CTS_TYPE_CURRENT_SETTINGS;
2213 cam_periph_lock(periph);
2214 xpt_action((union ccb *)&cts);
2215 cam_periph_unlock(periph);
2216 if (cts.ccb_h.status != CAM_REQ_CMP) {
2217 da_periph_release(periph, DA_REF_SYSCTL);
2220 if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
2221 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
2222 if (fc->valid & CTS_FC_VALID_WWPN) {
2223 softc->wwpn = fc->wwpn;
2224 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
2225 SYSCTL_CHILDREN(softc->sysctl_tree),
2226 OID_AUTO, "wwpn", CTLFLAG_RD,
2227 &softc->wwpn, "World Wide Port Name");
2233 * Now add some useful stats.
2234 * XXX These should live in cam_periph and be common to all periphs
2236 softc->sysctl_stats_tree = SYSCTL_ADD_NODE(&softc->sysctl_stats_ctx,
2237 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, "stats",
2238 CTLFLAG_RD, 0, "Statistics");
2239 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
2240 SYSCTL_CHILDREN(softc->sysctl_stats_tree),
2246 "Transport errors reported by the SIM");
2247 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
2248 SYSCTL_CHILDREN(softc->sysctl_stats_tree),
2254 "Device timeouts reported by the SIM");
2255 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
2256 SYSCTL_CHILDREN(softc->sysctl_stats_tree),
2258 "pack_invalidations",
2260 &softc->invalidations,
2262 "Device pack invalidations");
2265 cam_iosched_sysctl_init(softc->cam_iosched, &softc->sysctl_ctx,
2266 softc->sysctl_tree);
2268 da_periph_release(periph, DA_REF_SYSCTL);
2272 dadeletemaxsysctl(SYSCTL_HANDLER_ARGS)
2276 struct da_softc *softc;
2278 softc = (struct da_softc *)arg1;
2280 value = softc->disk->d_delmaxsize;
2281 error = sysctl_handle_64(oidp, &value, 0, req);
2282 if ((error != 0) || (req->newptr == NULL))
2285 /* only accept values smaller than the calculated value */
2286 if (value > dadeletemaxsize(softc, softc->delete_method)) {
2289 softc->disk->d_delmaxsize = value;
2295 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
2299 value = *(int *)arg1;
2301 error = sysctl_handle_int(oidp, &value, 0, req);
2304 || (req->newptr == NULL))
2308 * Acceptable values here are 6, 10, 12 or 16.
2312 else if ((value > 6)
2315 else if ((value > 10)
2318 else if (value > 12)
2321 *(int *)arg1 = value;
2327 dasysctlsofttimeout(SYSCTL_HANDLER_ARGS)
2332 value = da_default_softtimeout / SBT_1MS;
2334 error = sysctl_handle_int(oidp, (int *)&value, 0, req);
2335 if ((error != 0) || (req->newptr == NULL))
2338 /* XXX Should clip this to a reasonable level */
2339 if (value > da_default_timeout * 1000)
2342 da_default_softtimeout = value * SBT_1MS;
2347 dadeletemethodset(struct da_softc *softc, da_delete_methods delete_method)
2350 softc->delete_method = delete_method;
2351 softc->disk->d_delmaxsize = dadeletemaxsize(softc, delete_method);
2352 softc->delete_func = da_delete_functions[delete_method];
2354 if (softc->delete_method > DA_DELETE_DISABLE)
2355 softc->disk->d_flags |= DISKFLAG_CANDELETE;
2357 softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
2361 dadeletemaxsize(struct da_softc *softc, da_delete_methods delete_method)
2365 switch(delete_method) {
2366 case DA_DELETE_UNMAP:
2367 sectors = (off_t)softc->unmap_max_lba;
2369 case DA_DELETE_ATA_TRIM:
2370 sectors = (off_t)ATA_DSM_RANGE_MAX * softc->trim_max_ranges;
2372 case DA_DELETE_WS16:
2373 sectors = omin(softc->ws_max_blks, WS16_MAX_BLKS);
2375 case DA_DELETE_ZERO:
2376 case DA_DELETE_WS10:
2377 sectors = omin(softc->ws_max_blks, WS10_MAX_BLKS);
2383 return (off_t)softc->params.secsize *
2384 omin(sectors, softc->params.sectors);
2388 daprobedone(struct cam_periph *periph, union ccb *ccb)
2390 struct da_softc *softc;
2392 softc = (struct da_softc *)periph->softc;
2394 dadeletemethodchoose(softc, DA_DELETE_NONE);
2396 if (bootverbose && (softc->flags & DA_FLAG_ANNOUNCED) == 0) {
2400 snprintf(buf, sizeof(buf), "Delete methods: <");
2402 for (i = 0; i <= DA_DELETE_MAX; i++) {
2403 if ((softc->delete_available & (1 << i)) == 0 &&
2404 i != softc->delete_method)
2407 strlcat(buf, ",", sizeof(buf));
2408 strlcat(buf, da_delete_method_names[i],
2410 if (i == softc->delete_method)
2411 strlcat(buf, "(*)", sizeof(buf));
2414 strlcat(buf, ">", sizeof(buf));
2415 printf("%s%d: %s\n", periph->periph_name,
2416 periph->unit_number, buf);
2420 * Since our peripheral may be invalidated by an error
2421 * above or an external event, we must release our CCB
2422 * before releasing the probe lock on the peripheral.
2423 * The peripheral will only go away once the last lock
2424 * is removed, and we need it around for the CCB release
2427 xpt_release_ccb(ccb);
2428 softc->state = DA_STATE_NORMAL;
2429 softc->flags |= DA_FLAG_PROBED;
2431 wakeup(&softc->disk->d_mediasize);
2432 if ((softc->flags & DA_FLAG_ANNOUNCED) == 0) {
2433 softc->flags |= DA_FLAG_ANNOUNCED;
2434 da_periph_unhold(periph, DA_REF_PROBE_HOLD);
2436 da_periph_release_locked(periph, DA_REF_REPROBE);
2440 dadeletemethodchoose(struct da_softc *softc, da_delete_methods default_method)
2444 /* If available, prefer the method requested by user. */
2445 i = softc->delete_method_pref;
2446 methods = softc->delete_available | (1 << DA_DELETE_DISABLE);
2447 if (methods & (1 << i)) {
2448 dadeletemethodset(softc, i);
2452 /* Use the pre-defined order to choose the best performing delete. */
2453 for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
2454 if (i == DA_DELETE_ZERO)
2456 if (softc->delete_available & (1 << i)) {
2457 dadeletemethodset(softc, i);
2462 /* Fallback to default. */
2463 dadeletemethodset(softc, default_method);
2467 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
2471 struct da_softc *softc;
2472 int i, error, value;
2474 softc = (struct da_softc *)arg1;
2476 value = softc->delete_method;
2477 if (value < 0 || value > DA_DELETE_MAX)
2480 p = da_delete_method_names[value];
2481 strncpy(buf, p, sizeof(buf));
2482 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2483 if (error != 0 || req->newptr == NULL)
2485 for (i = 0; i <= DA_DELETE_MAX; i++) {
2486 if (strcmp(buf, da_delete_method_names[i]) == 0)
2489 if (i > DA_DELETE_MAX)
2491 softc->delete_method_pref = i;
2492 dadeletemethodchoose(softc, DA_DELETE_NONE);
2497 dazonemodesysctl(SYSCTL_HANDLER_ARGS)
2500 struct da_softc *softc;
2503 softc = (struct da_softc *)arg1;
2505 switch (softc->zone_mode) {
2506 case DA_ZONE_DRIVE_MANAGED:
2507 snprintf(tmpbuf, sizeof(tmpbuf), "Drive Managed");
2509 case DA_ZONE_HOST_AWARE:
2510 snprintf(tmpbuf, sizeof(tmpbuf), "Host Aware");
2512 case DA_ZONE_HOST_MANAGED:
2513 snprintf(tmpbuf, sizeof(tmpbuf), "Host Managed");
2517 snprintf(tmpbuf, sizeof(tmpbuf), "Not Zoned");
2521 error = sysctl_handle_string(oidp, tmpbuf, sizeof(tmpbuf), req);
2527 dazonesupsysctl(SYSCTL_HANDLER_ARGS)
2530 struct da_softc *softc;
2535 softc = (struct da_softc *)arg1;
2539 sbuf_new(&sb, tmpbuf, sizeof(tmpbuf), 0);
2541 for (i = 0; i < sizeof(da_zone_desc_table) /
2542 sizeof(da_zone_desc_table[0]); i++) {
2543 if (softc->zone_flags & da_zone_desc_table[i].value) {
2545 sbuf_printf(&sb, ", ");
2548 sbuf_cat(&sb, da_zone_desc_table[i].desc);
2553 sbuf_printf(&sb, "None");
2557 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
2563 daregister(struct cam_periph *periph, void *arg)
2565 struct da_softc *softc;
2566 struct ccb_pathinq cpi;
2567 struct ccb_getdev *cgd;
2571 cgd = (struct ccb_getdev *)arg;
2573 printf("daregister: no getdev CCB, can't register device\n");
2574 return(CAM_REQ_CMP_ERR);
2577 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
2580 if (softc == NULL) {
2581 printf("daregister: Unable to probe new device. "
2582 "Unable to allocate softc\n");
2583 return(CAM_REQ_CMP_ERR);
2586 if (cam_iosched_init(&softc->cam_iosched, periph) != 0) {
2587 printf("daregister: Unable to probe new device. "
2588 "Unable to allocate iosched memory\n");
2589 free(softc, M_DEVBUF);
2590 return(CAM_REQ_CMP_ERR);
2593 LIST_INIT(&softc->pending_ccbs);
2594 softc->state = DA_STATE_PROBE_WP;
2595 bioq_init(&softc->delete_run_queue);
2596 if (SID_IS_REMOVABLE(&cgd->inq_data))
2597 softc->flags |= DA_FLAG_PACK_REMOVABLE;
2598 softc->unmap_max_ranges = UNMAP_MAX_RANGES;
2599 softc->unmap_max_lba = UNMAP_RANGE_MAX;
2600 softc->unmap_gran = 0;
2601 softc->unmap_gran_align = 0;
2602 softc->ws_max_blks = WS16_MAX_BLKS;
2603 softc->trim_max_ranges = ATA_TRIM_MAX_RANGES;
2604 softc->rotating = 1;
2606 periph->softc = softc;
2609 * See if this device has any quirks.
2611 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
2612 (caddr_t)da_quirk_table,
2613 nitems(da_quirk_table),
2614 sizeof(*da_quirk_table), scsi_inquiry_match);
2617 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
2619 softc->quirks = DA_Q_NONE;
2621 /* Check if the SIM does not want 6 byte commands */
2622 xpt_path_inq(&cpi, periph->path);
2623 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
2624 softc->quirks |= DA_Q_NO_6_BYTE;
2626 if (SID_TYPE(&cgd->inq_data) == T_ZBC_HM)
2627 softc->zone_mode = DA_ZONE_HOST_MANAGED;
2628 else if (softc->quirks & DA_Q_SMR_DM)
2629 softc->zone_mode = DA_ZONE_DRIVE_MANAGED;
2631 softc->zone_mode = DA_ZONE_NONE;
2633 if (softc->zone_mode != DA_ZONE_NONE) {
2634 if (scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
2635 if (scsi_vpd_supported_page(periph, SVPD_ZONED_BDC))
2636 softc->zone_interface = DA_ZONE_IF_ATA_SAT;
2638 softc->zone_interface = DA_ZONE_IF_ATA_PASS;
2640 softc->zone_interface = DA_ZONE_IF_SCSI;
2643 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
2646 * Take an exclusive refcount on the periph while dastart is called
2647 * to finish the probe. The reference will be dropped in dadone at
2650 * XXX if cam_periph_hold returns an error, we don't hold a refcount.
2652 (void)da_periph_hold(periph, PRIBIO, DA_REF_PROBE_HOLD);
2655 * Schedule a periodic event to occasionally send an
2656 * ordered tag to a device.
2658 callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0);
2659 callout_reset(&softc->sendordered_c,
2660 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2661 dasendorderedtag, periph);
2663 cam_periph_unlock(periph);
2665 * RBC devices don't have to support READ(6), only READ(10).
2667 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
2668 softc->minimum_cmd_size = 10;
2670 softc->minimum_cmd_size = 6;
2673 * Load the user's default, if any.
2675 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
2676 periph->unit_number);
2677 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
2680 * 6, 10, 12 and 16 are the currently permissible values.
2682 if (softc->minimum_cmd_size > 12)
2683 softc->minimum_cmd_size = 16;
2684 else if (softc->minimum_cmd_size > 10)
2685 softc->minimum_cmd_size = 12;
2686 else if (softc->minimum_cmd_size > 6)
2687 softc->minimum_cmd_size = 10;
2689 softc->minimum_cmd_size = 6;
2691 /* Predict whether device may support READ CAPACITY(16). */
2692 if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3 &&
2693 (softc->quirks & DA_Q_NO_RC16) == 0) {
2694 softc->flags |= DA_FLAG_CAN_RC16;
2698 * Register this media as a disk.
2700 softc->disk = disk_alloc();
2701 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
2702 periph->unit_number, 0,
2703 DEVSTAT_BS_UNAVAILABLE,
2704 SID_TYPE(&cgd->inq_data) |
2705 XPORT_DEVSTAT_TYPE(cpi.transport),
2706 DEVSTAT_PRIORITY_DISK);
2707 softc->disk->d_open = daopen;
2708 softc->disk->d_close = daclose;
2709 softc->disk->d_strategy = dastrategy;
2710 softc->disk->d_dump = dadump;
2711 softc->disk->d_getattr = dagetattr;
2712 softc->disk->d_gone = dadiskgonecb;
2713 softc->disk->d_name = "da";
2714 softc->disk->d_drv1 = periph;
2716 softc->maxio = DFLTPHYS; /* traditional default */
2717 else if (cpi.maxio > MAXPHYS)
2718 softc->maxio = MAXPHYS; /* for safety */
2720 softc->maxio = cpi.maxio;
2721 softc->disk->d_maxsize = softc->maxio;
2722 softc->disk->d_unit = periph->unit_number;
2723 softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION | DISKFLAG_CANZONE;
2724 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
2725 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
2726 if ((cpi.hba_misc & PIM_UNMAPPED) != 0) {
2727 softc->unmappedio = 1;
2728 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
2730 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
2731 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
2732 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
2733 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
2734 cgd->inq_data.product, sizeof(cgd->inq_data.product),
2735 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
2736 softc->disk->d_hba_vendor = cpi.hba_vendor;
2737 softc->disk->d_hba_device = cpi.hba_device;
2738 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
2739 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
2742 * Acquire a reference to the periph before we register with GEOM.
2743 * We'll release this reference once GEOM calls us back (via
2744 * dadiskgonecb()) telling us that our provider has been freed.
2746 if (da_periph_acquire(periph, DA_REF_GEOM) != 0) {
2747 xpt_print(periph->path, "%s: lost periph during "
2748 "registration!\n", __func__);
2749 cam_periph_lock(periph);
2750 return (CAM_REQ_CMP_ERR);
2753 disk_create(softc->disk, DISK_VERSION);
2754 cam_periph_lock(periph);
2757 * Add async callbacks for events of interest.
2758 * I don't bother checking if this fails as,
2759 * in most cases, the system will function just
2760 * fine without them and the only alternative
2761 * would be to not attach the device on failure.
2763 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
2764 AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION |
2765 AC_INQ_CHANGED, daasync, periph, periph->path);
2768 * Emit an attribute changed notification just in case
2769 * physical path information arrived before our async
2770 * event handler was registered, but after anyone attaching
2771 * to our disk device polled it.
2773 disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
2776 * Schedule a periodic media polling events.
2778 callout_init_mtx(&softc->mediapoll_c, cam_periph_mtx(periph), 0);
2779 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
2780 (cgd->inq_flags & SID_AEN) == 0 &&
2781 da_poll_period != 0)
2782 callout_reset(&softc->mediapoll_c, da_poll_period * hz,
2783 damediapoll, periph);
2785 xpt_schedule(periph, CAM_PRIORITY_DEV);
2787 return(CAM_REQ_CMP);
2791 da_zone_bio_to_scsi(int disk_zone_cmd)
2793 switch (disk_zone_cmd) {
2794 case DISK_ZONE_OPEN:
2795 return ZBC_OUT_SA_OPEN;
2796 case DISK_ZONE_CLOSE:
2797 return ZBC_OUT_SA_CLOSE;
2798 case DISK_ZONE_FINISH:
2799 return ZBC_OUT_SA_FINISH;
2801 return ZBC_OUT_SA_RWP;
2808 da_zone_cmd(struct cam_periph *periph, union ccb *ccb, struct bio *bp,
2811 struct da_softc *softc;
2816 if (bp->bio_cmd != BIO_ZONE) {
2821 softc = periph->softc;
2823 switch (bp->bio_zone.zone_cmd) {
2824 case DISK_ZONE_OPEN:
2825 case DISK_ZONE_CLOSE:
2826 case DISK_ZONE_FINISH:
2827 case DISK_ZONE_RWP: {
2832 zone_sa = da_zone_bio_to_scsi(bp->bio_zone.zone_cmd);
2833 if (zone_sa == -1) {
2834 xpt_print(periph->path, "Cannot translate zone "
2835 "cmd %#x to SCSI\n", bp->bio_zone.zone_cmd);
2841 lba = bp->bio_zone.zone_params.rwp.id;
2843 if (bp->bio_zone.zone_params.rwp.flags &
2844 DISK_ZONE_RWP_FLAG_ALL)
2845 zone_flags |= ZBC_OUT_ALL;
2847 if (softc->zone_interface != DA_ZONE_IF_ATA_PASS) {
2848 scsi_zbc_out(&ccb->csio,
2849 /*retries*/ da_retry_count,
2851 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2852 /*service_action*/ zone_sa,
2854 /*zone_flags*/ zone_flags,
2857 /*sense_len*/ SSD_FULL_SIZE,
2858 /*timeout*/ da_default_timeout * 1000);
2861 * Note that in this case, even though we can
2862 * technically use NCQ, we don't bother for several
2864 * 1. It hasn't been tested on a SAT layer that
2865 * supports it. This is new as of SAT-4.
2866 * 2. Even when there is a SAT layer that supports
2867 * it, that SAT layer will also probably support
2868 * ZBC -> ZAC translation, since they are both
2869 * in the SAT-4 spec.
2870 * 3. Translation will likely be preferable to ATA
2871 * passthrough. LSI / Avago at least single
2872 * steps ATA passthrough commands in the HBA,
2873 * regardless of protocol, so unless that
2874 * changes, there is a performance penalty for
2875 * doing ATA passthrough no matter whether
2876 * you're using NCQ/FPDMA, DMA or PIO.
2877 * 4. It requires a 32-byte CDB, which at least at
2878 * this point in CAM requires a CDB pointer, which
2879 * would require us to allocate an additional bit
2880 * of storage separate from the CCB.
2882 error = scsi_ata_zac_mgmt_out(&ccb->csio,
2883 /*retries*/ da_retry_count,
2885 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2887 /*zm_action*/ zone_sa,
2889 /*zone_flags*/ zone_flags,
2892 /*cdb_storage*/ NULL,
2893 /*cdb_storage_len*/ 0,
2894 /*sense_len*/ SSD_FULL_SIZE,
2895 /*timeout*/ da_default_timeout * 1000);
2898 xpt_print(periph->path,
2899 "scsi_ata_zac_mgmt_out() returned an "
2908 case DISK_ZONE_REPORT_ZONES: {
2910 uint32_t num_entries, alloc_size;
2911 struct disk_zone_report *rep;
2913 rep = &bp->bio_zone.zone_params.report;
2915 num_entries = rep->entries_allocated;
2916 if (num_entries == 0) {
2917 xpt_print(periph->path, "No entries allocated for "
2918 "Report Zones request\n");
2922 alloc_size = sizeof(struct scsi_report_zones_hdr) +
2923 (sizeof(struct scsi_report_zones_desc) * num_entries);
2924 alloc_size = min(alloc_size, softc->disk->d_maxsize);
2925 rz_ptr = malloc(alloc_size, M_SCSIDA, M_NOWAIT | M_ZERO);
2926 if (rz_ptr == NULL) {
2927 xpt_print(periph->path, "Unable to allocate memory "
2928 "for Report Zones request\n");
2933 if (softc->zone_interface != DA_ZONE_IF_ATA_PASS) {
2934 scsi_zbc_in(&ccb->csio,
2935 /*retries*/ da_retry_count,
2937 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2938 /*service_action*/ ZBC_IN_SA_REPORT_ZONES,
2939 /*zone_start_lba*/ rep->starting_id,
2940 /*zone_options*/ rep->rep_options,
2941 /*data_ptr*/ rz_ptr,
2942 /*dxfer_len*/ alloc_size,
2943 /*sense_len*/ SSD_FULL_SIZE,
2944 /*timeout*/ da_default_timeout * 1000);
2947 * Note that in this case, even though we can
2948 * technically use NCQ, we don't bother for several
2950 * 1. It hasn't been tested on a SAT layer that
2951 * supports it. This is new as of SAT-4.
2952 * 2. Even when there is a SAT layer that supports
2953 * it, that SAT layer will also probably support
2954 * ZBC -> ZAC translation, since they are both
2955 * in the SAT-4 spec.
2956 * 3. Translation will likely be preferable to ATA
2957 * passthrough. LSI / Avago at least single
2958 * steps ATA passthrough commands in the HBA,
2959 * regardless of protocol, so unless that
2960 * changes, there is a performance penalty for
2961 * doing ATA passthrough no matter whether
2962 * you're using NCQ/FPDMA, DMA or PIO.
2963 * 4. It requires a 32-byte CDB, which at least at
2964 * this point in CAM requires a CDB pointer, which
2965 * would require us to allocate an additional bit
2966 * of storage separate from the CCB.
2968 error = scsi_ata_zac_mgmt_in(&ccb->csio,
2969 /*retries*/ da_retry_count,
2971 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2973 /*zm_action*/ ATA_ZM_REPORT_ZONES,
2974 /*zone_id*/ rep->starting_id,
2975 /*zone_flags*/ rep->rep_options,
2976 /*data_ptr*/ rz_ptr,
2977 /*dxfer_len*/ alloc_size,
2978 /*cdb_storage*/ NULL,
2979 /*cdb_storage_len*/ 0,
2980 /*sense_len*/ SSD_FULL_SIZE,
2981 /*timeout*/ da_default_timeout * 1000);
2984 xpt_print(periph->path,
2985 "scsi_ata_zac_mgmt_in() returned an "
2992 * For BIO_ZONE, this isn't normally needed. However, it
2993 * is used by devstat_end_transaction_bio() to determine
2994 * how much data was transferred.
2997 * XXX KDM we have a problem. But I'm not sure how to fix
2998 * it. devstat uses bio_bcount - bio_resid to calculate
2999 * the amount of data transferred. The GEOM disk code
3000 * uses bio_length - bio_resid to calculate the amount of
3001 * data in bio_completed. We have different structure
3002 * sizes above and below the ada(4) driver. So, if we
3003 * use the sizes above, the amount transferred won't be
3004 * quite accurate for devstat. If we use different sizes
3005 * for bio_bcount and bio_length (above and below
3006 * respectively), then the residual needs to match one or
3007 * the other. Everything is calculated after the bio
3008 * leaves the driver, so changing the values around isn't
3009 * really an option. For now, just set the count to the
3010 * passed in length. This means that the calculations
3011 * above (e.g. bio_completed) will be correct, but the
3012 * amount of data reported to devstat will be slightly
3013 * under or overstated.
3015 bp->bio_bcount = bp->bio_length;
3021 case DISK_ZONE_GET_PARAMS: {
3022 struct disk_zone_disk_params *params;
3024 params = &bp->bio_zone.zone_params.disk_params;
3025 bzero(params, sizeof(*params));
3027 switch (softc->zone_mode) {
3028 case DA_ZONE_DRIVE_MANAGED:
3029 params->zone_mode = DISK_ZONE_MODE_DRIVE_MANAGED;
3031 case DA_ZONE_HOST_AWARE:
3032 params->zone_mode = DISK_ZONE_MODE_HOST_AWARE;
3034 case DA_ZONE_HOST_MANAGED:
3035 params->zone_mode = DISK_ZONE_MODE_HOST_MANAGED;
3039 params->zone_mode = DISK_ZONE_MODE_NONE;
3043 if (softc->zone_flags & DA_ZONE_FLAG_URSWRZ)
3044 params->flags |= DISK_ZONE_DISK_URSWRZ;
3046 if (softc->zone_flags & DA_ZONE_FLAG_OPT_SEQ_SET) {
3047 params->optimal_seq_zones = softc->optimal_seq_zones;
3048 params->flags |= DISK_ZONE_OPT_SEQ_SET;
3051 if (softc->zone_flags & DA_ZONE_FLAG_OPT_NONSEQ_SET) {
3052 params->optimal_nonseq_zones =
3053 softc->optimal_nonseq_zones;
3054 params->flags |= DISK_ZONE_OPT_NONSEQ_SET;
3057 if (softc->zone_flags & DA_ZONE_FLAG_MAX_SEQ_SET) {
3058 params->max_seq_zones = softc->max_seq_zones;
3059 params->flags |= DISK_ZONE_MAX_SEQ_SET;
3061 if (softc->zone_flags & DA_ZONE_FLAG_RZ_SUP)
3062 params->flags |= DISK_ZONE_RZ_SUP;
3064 if (softc->zone_flags & DA_ZONE_FLAG_OPEN_SUP)
3065 params->flags |= DISK_ZONE_OPEN_SUP;
3067 if (softc->zone_flags & DA_ZONE_FLAG_CLOSE_SUP)
3068 params->flags |= DISK_ZONE_CLOSE_SUP;
3070 if (softc->zone_flags & DA_ZONE_FLAG_FINISH_SUP)
3071 params->flags |= DISK_ZONE_FINISH_SUP;
3073 if (softc->zone_flags & DA_ZONE_FLAG_RWP_SUP)
3074 params->flags |= DISK_ZONE_RWP_SUP;
3085 dastart(struct cam_periph *periph, union ccb *start_ccb)
3087 struct da_softc *softc;
3089 cam_periph_assert(periph, MA_OWNED);
3090 softc = (struct da_softc *)periph->softc;
3092 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
3095 switch (softc->state) {
3096 case DA_STATE_NORMAL:
3102 bp = cam_iosched_next_bio(softc->cam_iosched);
3104 if (cam_iosched_has_work_flags(softc->cam_iosched, DA_WORK_TUR)) {
3105 cam_iosched_clr_work_flags(softc->cam_iosched, DA_WORK_TUR);
3106 scsi_test_unit_ready(&start_ccb->csio,
3107 /*retries*/ da_retry_count,
3111 da_default_timeout * 1000);
3112 start_ccb->ccb_h.ccb_bp = NULL;
3113 start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
3114 xpt_action(start_ccb);
3116 xpt_release_ccb(start_ccb);
3120 if (bp->bio_cmd == BIO_DELETE) {
3121 if (softc->delete_func != NULL) {
3122 softc->delete_func(periph, start_ccb, bp);
3125 /* Not sure this is possible, but failsafe by lying and saying "sure, done." */
3126 biofinish(bp, NULL, 0);
3131 if (cam_iosched_has_work_flags(softc->cam_iosched, DA_WORK_TUR)) {
3132 cam_iosched_clr_work_flags(softc->cam_iosched, DA_WORK_TUR);
3133 da_periph_release_locked(periph, DA_REF_TUR);
3136 if ((bp->bio_flags & BIO_ORDERED) != 0 ||
3137 (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
3138 softc->flags &= ~DA_FLAG_NEED_OTAG;
3139 softc->flags |= DA_FLAG_WAS_OTAG;
3140 tag_code = MSG_ORDERED_Q_TAG;
3142 tag_code = MSG_SIMPLE_Q_TAG;
3145 switch (bp->bio_cmd) {
3152 biotrack(bp, __func__);
3154 if (bp->bio_cmd == BIO_WRITE) {
3155 softc->flags |= DA_FLAG_DIRTY;
3156 rw_op = SCSI_RW_WRITE;
3158 rw_op = SCSI_RW_READ;
3161 data_ptr = bp->bio_data;
3162 if ((bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0) {
3163 rw_op |= SCSI_RW_BIO;
3167 scsi_read_write(&start_ccb->csio,
3168 /*retries*/da_retry_count,
3170 /*tag_action*/tag_code,
3173 softc->minimum_cmd_size,
3174 /*lba*/bp->bio_pblkno,
3175 /*block_count*/bp->bio_bcount /
3176 softc->params.secsize,
3178 /*dxfer_len*/ bp->bio_bcount,
3179 /*sense_len*/SSD_FULL_SIZE,
3180 da_default_timeout * 1000);
3181 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
3182 start_ccb->csio.bio = bp;
3188 * If we don't support sync cache, or the disk
3189 * isn't dirty, FLUSH is a no-op. Use the
3190 * allocated * CCB for the next bio if one is
3193 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) != 0 ||
3194 (softc->flags & DA_FLAG_DIRTY) == 0) {
3200 * BIO_FLUSH doesn't currently communicate
3201 * range data, so we synchronize the cache
3202 * over the whole disk. We also force
3203 * ordered tag semantics the flush applies
3204 * to all previously queued I/O.
3206 scsi_synchronize_cache(&start_ccb->csio,
3213 da_default_timeout*1000);
3215 * Clear the dirty flag before sending the command.
3216 * Either this sync cache will be successful, or it
3217 * will fail after a retry. If it fails, it is
3218 * unlikely to be successful if retried later, so
3219 * we'll save ourselves time by just marking the
3222 softc->flags &= ~DA_FLAG_DIRTY;
3225 int error, queue_ccb;
3229 error = da_zone_cmd(periph, start_ccb, bp,&queue_ccb);
3231 || (queue_ccb == 0)) {
3232 biofinish(bp, NULL, error);
3233 xpt_release_ccb(start_ccb);
3239 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
3240 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
3241 start_ccb->ccb_h.softtimeout = sbttotv(da_default_softtimeout);
3244 LIST_INSERT_HEAD(&softc->pending_ccbs,
3245 &start_ccb->ccb_h, periph_links.le);
3247 /* We expect a unit attention from this device */
3248 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
3249 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
3250 softc->flags &= ~DA_FLAG_RETRY_UA;
3253 start_ccb->ccb_h.ccb_bp = bp;
3255 cam_periph_unlock(periph);
3256 xpt_action(start_ccb);
3257 cam_periph_lock(periph);
3260 /* May have more work to do, so ensure we stay scheduled */
3264 case DA_STATE_PROBE_WP:
3270 mode_buf = malloc(mode_buf_len, M_SCSIDA, M_NOWAIT);
3271 if (mode_buf == NULL) {
3272 xpt_print(periph->path, "Unable to send mode sense - "
3273 "malloc failure\n");
3274 softc->state = DA_STATE_PROBE_RC;
3277 scsi_mode_sense_len(&start_ccb->csio,
3278 /*retries*/ da_retry_count,
3280 /*tag_action*/ MSG_SIMPLE_Q_TAG,
3282 /*pc*/ SMS_PAGE_CTRL_CURRENT,
3283 /*page*/ SMS_ALL_PAGES_PAGE,
3284 /*param_buf*/ mode_buf,
3285 /*param_len*/ mode_buf_len,
3286 /*minimum_cmd_size*/ softc->minimum_cmd_size,
3287 /*sense_len*/ SSD_FULL_SIZE,
3288 /*timeout*/ da_default_timeout * 1000);
3289 start_ccb->ccb_h.ccb_bp = NULL;
3290 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_WP;
3291 xpt_action(start_ccb);
3294 case DA_STATE_PROBE_RC:
3296 struct scsi_read_capacity_data *rcap;
3298 rcap = (struct scsi_read_capacity_data *)
3299 malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
3301 printf("dastart: Couldn't malloc read_capacity data\n");
3302 /* da_free_periph??? */
3305 scsi_read_capacity(&start_ccb->csio,
3306 /*retries*/da_retry_count,
3312 start_ccb->ccb_h.ccb_bp = NULL;
3313 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC;
3314 xpt_action(start_ccb);
3317 case DA_STATE_PROBE_RC16:
3319 struct scsi_read_capacity_data_long *rcaplong;
3321 rcaplong = (struct scsi_read_capacity_data_long *)
3322 malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
3323 if (rcaplong == NULL) {
3324 printf("dastart: Couldn't malloc read_capacity data\n");
3325 /* da_free_periph??? */
3328 scsi_read_capacity_16(&start_ccb->csio,
3329 /*retries*/ da_retry_count,
3331 /*tag_action*/ MSG_SIMPLE_Q_TAG,
3335 /*rcap_buf*/ (uint8_t *)rcaplong,
3336 /*rcap_buf_len*/ sizeof(*rcaplong),
3337 /*sense_len*/ SSD_FULL_SIZE,
3338 /*timeout*/ da_default_timeout * 1000);
3339 start_ccb->ccb_h.ccb_bp = NULL;
3340 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC16;
3341 xpt_action(start_ccb);
3344 case DA_STATE_PROBE_LBP:
3346 struct scsi_vpd_logical_block_prov *lbp;
3348 if (!scsi_vpd_supported_page(periph, SVPD_LBP)) {
3350 * If we get here we don't support any SBC-3 delete
3351 * methods with UNMAP as the Logical Block Provisioning
3352 * VPD page support is required for devices which
3353 * support it according to T10/1799-D Revision 31
3354 * however older revisions of the spec don't mandate
3355 * this so we currently don't remove these methods
3356 * from the available set.
3358 softc->state = DA_STATE_PROBE_BLK_LIMITS;
3362 lbp = (struct scsi_vpd_logical_block_prov *)
3363 malloc(sizeof(*lbp), M_SCSIDA, M_NOWAIT|M_ZERO);
3366 printf("dastart: Couldn't malloc lbp data\n");
3367 /* da_free_periph??? */
3371 scsi_inquiry(&start_ccb->csio,
3372 /*retries*/da_retry_count,
3374 /*tag_action*/MSG_SIMPLE_Q_TAG,
3375 /*inq_buf*/(u_int8_t *)lbp,
3376 /*inq_len*/sizeof(*lbp),
3378 /*page_code*/SVPD_LBP,
3379 /*sense_len*/SSD_MIN_SIZE,
3380 /*timeout*/da_default_timeout * 1000);
3381 start_ccb->ccb_h.ccb_bp = NULL;
3382 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_LBP;
3383 xpt_action(start_ccb);
3386 case DA_STATE_PROBE_BLK_LIMITS:
3388 struct scsi_vpd_block_limits *block_limits;
3390 if (!scsi_vpd_supported_page(periph, SVPD_BLOCK_LIMITS)) {
3391 /* Not supported skip to next probe */
3392 softc->state = DA_STATE_PROBE_BDC;
3396 block_limits = (struct scsi_vpd_block_limits *)
3397 malloc(sizeof(*block_limits), M_SCSIDA, M_NOWAIT|M_ZERO);
3399 if (block_limits == NULL) {
3400 printf("dastart: Couldn't malloc block_limits data\n");
3401 /* da_free_periph??? */
3405 scsi_inquiry(&start_ccb->csio,
3406 /*retries*/da_retry_count,
3408 /*tag_action*/MSG_SIMPLE_Q_TAG,
3409 /*inq_buf*/(u_int8_t *)block_limits,
3410 /*inq_len*/sizeof(*block_limits),
3412 /*page_code*/SVPD_BLOCK_LIMITS,
3413 /*sense_len*/SSD_MIN_SIZE,
3414 /*timeout*/da_default_timeout * 1000);
3415 start_ccb->ccb_h.ccb_bp = NULL;
3416 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BLK_LIMITS;
3417 xpt_action(start_ccb);
3420 case DA_STATE_PROBE_BDC:
3422 struct scsi_vpd_block_characteristics *bdc;
3424 if (!scsi_vpd_supported_page(periph, SVPD_BDC)) {
3425 softc->state = DA_STATE_PROBE_ATA;
3429 bdc = (struct scsi_vpd_block_characteristics *)
3430 malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
3433 printf("dastart: Couldn't malloc bdc data\n");
3434 /* da_free_periph??? */
3438 scsi_inquiry(&start_ccb->csio,
3439 /*retries*/da_retry_count,
3441 /*tag_action*/MSG_SIMPLE_Q_TAG,
3442 /*inq_buf*/(u_int8_t *)bdc,
3443 /*inq_len*/sizeof(*bdc),
3445 /*page_code*/SVPD_BDC,
3446 /*sense_len*/SSD_MIN_SIZE,
3447 /*timeout*/da_default_timeout * 1000);
3448 start_ccb->ccb_h.ccb_bp = NULL;
3449 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BDC;
3450 xpt_action(start_ccb);
3453 case DA_STATE_PROBE_ATA:
3455 struct ata_params *ata_params;
3457 if (!scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
3458 if ((softc->zone_mode == DA_ZONE_HOST_AWARE)
3459 || (softc->zone_mode == DA_ZONE_HOST_MANAGED)) {
3461 * Note that if the ATA VPD page isn't
3462 * supported, we aren't talking to an ATA
3463 * device anyway. Support for that VPD
3464 * page is mandatory for SCSI to ATA (SAT)
3465 * translation layers.
3467 softc->state = DA_STATE_PROBE_ZONE;
3470 daprobedone(periph, start_ccb);
3474 ata_params = (struct ata_params*)
3475 malloc(sizeof(*ata_params), M_SCSIDA,M_NOWAIT|M_ZERO);
3477 if (ata_params == NULL) {
3478 xpt_print(periph->path, "Couldn't malloc ata_params "
3480 /* da_free_periph??? */
3484 scsi_ata_identify(&start_ccb->csio,
3485 /*retries*/da_retry_count,
3487 /*tag_action*/MSG_SIMPLE_Q_TAG,
3488 /*data_ptr*/(u_int8_t *)ata_params,
3489 /*dxfer_len*/sizeof(*ata_params),
3490 /*sense_len*/SSD_FULL_SIZE,
3491 /*timeout*/da_default_timeout * 1000);
3492 start_ccb->ccb_h.ccb_bp = NULL;
3493 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA;
3494 xpt_action(start_ccb);
3497 case DA_STATE_PROBE_ATA_LOGDIR:
3499 struct ata_gp_log_dir *log_dir;
3504 if ((softc->flags & DA_FLAG_CAN_ATA_LOG) == 0) {
3506 * If we don't have log support, not much point in
3507 * trying to probe zone support.
3509 daprobedone(periph, start_ccb);
3514 * If we have an ATA device (the SCSI ATA Information VPD
3515 * page should be present and the ATA identify should have
3516 * succeeded) and it supports logs, ask for the log directory.
3519 log_dir = malloc(sizeof(*log_dir), M_SCSIDA, M_NOWAIT|M_ZERO);
3520 if (log_dir == NULL) {
3521 xpt_print(periph->path, "Couldn't malloc log_dir "
3523 daprobedone(periph, start_ccb);
3527 retval = scsi_ata_read_log(&start_ccb->csio,
3528 /*retries*/ da_retry_count,
3530 /*tag_action*/ MSG_SIMPLE_Q_TAG,
3531 /*log_address*/ ATA_LOG_DIRECTORY,
3534 /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3535 AP_PROTO_DMA : AP_PROTO_PIO_IN,
3536 /*data_ptr*/ (uint8_t *)log_dir,
3537 /*dxfer_len*/ sizeof(*log_dir),
3538 /*sense_len*/ SSD_FULL_SIZE,
3539 /*timeout*/ da_default_timeout * 1000);
3542 xpt_print(periph->path, "scsi_ata_read_log() failed!");
3543 free(log_dir, M_SCSIDA);
3544 daprobedone(periph, start_ccb);
3547 start_ccb->ccb_h.ccb_bp = NULL;
3548 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_LOGDIR;
3549 xpt_action(start_ccb);
3552 case DA_STATE_PROBE_ATA_IDDIR:
3554 struct ata_identify_log_pages *id_dir;
3560 * Check here to see whether the Identify Device log is
3561 * supported in the directory of logs. If so, continue
3562 * with requesting the log of identify device pages.
3564 if ((softc->flags & DA_FLAG_CAN_ATA_IDLOG) == 0) {
3565 daprobedone(periph, start_ccb);
3569 id_dir = malloc(sizeof(*id_dir), M_SCSIDA, M_NOWAIT | M_ZERO);
3570 if (id_dir == NULL) {
3571 xpt_print(periph->path, "Couldn't malloc id_dir "
3573 daprobedone(periph, start_ccb);
3577 retval = scsi_ata_read_log(&start_ccb->csio,
3578 /*retries*/ da_retry_count,
3580 /*tag_action*/ MSG_SIMPLE_Q_TAG,
3581 /*log_address*/ ATA_IDENTIFY_DATA_LOG,
3582 /*page_number*/ ATA_IDL_PAGE_LIST,
3584 /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3585 AP_PROTO_DMA : AP_PROTO_PIO_IN,
3586 /*data_ptr*/ (uint8_t *)id_dir,
3587 /*dxfer_len*/ sizeof(*id_dir),
3588 /*sense_len*/ SSD_FULL_SIZE,
3589 /*timeout*/ da_default_timeout * 1000);
3592 xpt_print(periph->path, "scsi_ata_read_log() failed!");
3593 free(id_dir, M_SCSIDA);
3594 daprobedone(periph, start_ccb);
3597 start_ccb->ccb_h.ccb_bp = NULL;
3598 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_IDDIR;
3599 xpt_action(start_ccb);
3602 case DA_STATE_PROBE_ATA_SUP:
3604 struct ata_identify_log_sup_cap *sup_cap;
3610 * Check here to see whether the Supported Capabilities log
3611 * is in the list of Identify Device logs.
3613 if ((softc->flags & DA_FLAG_CAN_ATA_SUPCAP) == 0) {
3614 daprobedone(periph, start_ccb);
3618 sup_cap = malloc(sizeof(*sup_cap), M_SCSIDA, M_NOWAIT|M_ZERO);
3619 if (sup_cap == NULL) {
3620 xpt_print(periph->path, "Couldn't malloc sup_cap "
3622 daprobedone(periph, start_ccb);
3626 retval = scsi_ata_read_log(&start_ccb->csio,
3627 /*retries*/ da_retry_count,
3629 /*tag_action*/ MSG_SIMPLE_Q_TAG,
3630 /*log_address*/ ATA_IDENTIFY_DATA_LOG,
3631 /*page_number*/ ATA_IDL_SUP_CAP,
3633 /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3634 AP_PROTO_DMA : AP_PROTO_PIO_IN,
3635 /*data_ptr*/ (uint8_t *)sup_cap,
3636 /*dxfer_len*/ sizeof(*sup_cap),
3637 /*sense_len*/ SSD_FULL_SIZE,
3638 /*timeout*/ da_default_timeout * 1000);
3641 xpt_print(periph->path, "scsi_ata_read_log() failed!");
3642 free(sup_cap, M_SCSIDA);
3643 daprobedone(periph, start_ccb);
3648 start_ccb->ccb_h.ccb_bp = NULL;
3649 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_SUP;
3650 xpt_action(start_ccb);
3653 case DA_STATE_PROBE_ATA_ZONE:
3655 struct ata_zoned_info_log *ata_zone;
3661 * Check here to see whether the zoned device information
3662 * page is supported. If so, continue on to request it.
3663 * If not, skip to DA_STATE_PROBE_LOG or done.
3665 if ((softc->flags & DA_FLAG_CAN_ATA_ZONE) == 0) {
3666 daprobedone(periph, start_ccb);
3669 ata_zone = malloc(sizeof(*ata_zone), M_SCSIDA,
3671 if (ata_zone == NULL) {
3672 xpt_print(periph->path, "Couldn't malloc ata_zone "
3674 daprobedone(periph, start_ccb);
3678 retval = scsi_ata_read_log(&start_ccb->csio,
3679 /*retries*/ da_retry_count,
3681 /*tag_action*/ MSG_SIMPLE_Q_TAG,
3682 /*log_address*/ ATA_IDENTIFY_DATA_LOG,
3683 /*page_number*/ ATA_IDL_ZDI,
3685 /*protocol*/ softc->flags & DA_FLAG_CAN_ATA_DMA ?
3686 AP_PROTO_DMA : AP_PROTO_PIO_IN,
3687 /*data_ptr*/ (uint8_t *)ata_zone,
3688 /*dxfer_len*/ sizeof(*ata_zone),
3689 /*sense_len*/ SSD_FULL_SIZE,
3690 /*timeout*/ da_default_timeout * 1000);
3693 xpt_print(periph->path, "scsi_ata_read_log() failed!");
3694 free(ata_zone, M_SCSIDA);
3695 daprobedone(periph, start_ccb);
3698 start_ccb->ccb_h.ccb_bp = NULL;
3699 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA_ZONE;
3700 xpt_action(start_ccb);
3704 case DA_STATE_PROBE_ZONE:
3706 struct scsi_vpd_zoned_bdc *bdc;
3709 * Note that this page will be supported for SCSI protocol
3710 * devices that support ZBC (SMR devices), as well as ATA
3711 * protocol devices that are behind a SAT (SCSI to ATA
3712 * Translation) layer that supports converting ZBC commands
3713 * to their ZAC equivalents.
3715 if (!scsi_vpd_supported_page(periph, SVPD_ZONED_BDC)) {
3716 daprobedone(periph, start_ccb);
3719 bdc = (struct scsi_vpd_zoned_bdc *)
3720 malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
3723 xpt_release_ccb(start_ccb);
3724 xpt_print(periph->path, "Couldn't malloc zone VPD "
3728 scsi_inquiry(&start_ccb->csio,
3729 /*retries*/da_retry_count,
3731 /*tag_action*/MSG_SIMPLE_Q_TAG,
3732 /*inq_buf*/(u_int8_t *)bdc,
3733 /*inq_len*/sizeof(*bdc),
3735 /*page_code*/SVPD_ZONED_BDC,
3736 /*sense_len*/SSD_FULL_SIZE,
3737 /*timeout*/da_default_timeout * 1000);
3738 start_ccb->ccb_h.ccb_bp = NULL;
3739 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ZONE;
3740 xpt_action(start_ccb);
3747 * In each of the methods below, while its the caller's
3748 * responsibility to ensure the request will fit into a
3749 * single device request, we might have changed the delete
3750 * method due to the device incorrectly advertising either
3751 * its supported methods or limits.
3753 * To prevent this causing further issues we validate the
3754 * against the methods limits, and warn which would
3755 * otherwise be unnecessary.
3758 da_delete_unmap(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
3760 struct da_softc *softc = (struct da_softc *)periph->softc;;
3762 uint8_t *buf = softc->unmap_buf;
3763 struct scsi_unmap_desc *d = (void *)&buf[UNMAP_HEAD_SIZE];
3764 uint64_t lba, lastlba = (uint64_t)-1;
3765 uint64_t totalcount = 0;
3767 uint32_t c, lastcount = 0, ranges = 0;
3770 * Currently this doesn't take the UNMAP
3771 * Granularity and Granularity Alignment
3772 * fields into account.
3774 * This could result in both unoptimal unmap
3775 * requests as as well as UNMAP calls unmapping
3776 * fewer LBA's than requested.
3779 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
3783 * Note: ada and da are different in how they store the
3784 * pending bp's in a trim. ada stores all of them in the
3785 * trim_req.bps. da stores all but the first one in the
3786 * delete_run_queue. ada then completes all the bps in
3787 * its adadone() loop. da completes all the bps in the
3788 * delete_run_queue in dadone, and relies on the biodone
3789 * after to complete. This should be reconciled since there's
3790 * no real reason to do it differently. XXX
3793 bioq_insert_tail(&softc->delete_run_queue, bp1);
3794 lba = bp1->bio_pblkno;
3795 count = bp1->bio_bcount / softc->params.secsize;
3797 /* Try to extend the previous range. */
3798 if (lba == lastlba) {
3799 c = omin(count, UNMAP_RANGE_MAX - lastcount);
3802 scsi_ulto4b(lastcount, d[ranges - 1].length);
3806 } else if ((softc->quirks & DA_Q_STRICT_UNMAP) &&
3807 softc->unmap_gran != 0) {
3808 /* Align length of the previous range. */
3809 if ((c = lastcount % softc->unmap_gran) != 0) {
3810 if (lastcount <= c) {
3811 totalcount -= lastcount;
3812 lastlba = (uint64_t)-1;
3819 scsi_ulto4b(lastcount, d[ranges - 1].length);
3822 /* Align beginning of the new range. */
3823 c = (lba - softc->unmap_gran_align) % softc->unmap_gran;
3825 c = softc->unmap_gran - c;
3836 c = omin(count, UNMAP_RANGE_MAX);
3837 if (totalcount + c > softc->unmap_max_lba ||
3838 ranges >= softc->unmap_max_ranges) {
3839 xpt_print(periph->path,
3840 "%s issuing short delete %ld > %ld"
3842 da_delete_method_desc[softc->delete_method],
3843 totalcount + c, softc->unmap_max_lba,
3844 ranges, softc->unmap_max_ranges);
3847 scsi_u64to8b(lba, d[ranges].lba);
3848 scsi_ulto4b(c, d[ranges].length);
3856 bp1 = cam_iosched_next_trim(softc->cam_iosched);
3859 if (ranges >= softc->unmap_max_ranges ||
3860 totalcount + bp1->bio_bcount /
3861 softc->params.secsize > softc->unmap_max_lba) {
3862 cam_iosched_put_back_trim(softc->cam_iosched, bp1);
3867 /* Align length of the last range. */
3868 if ((softc->quirks & DA_Q_STRICT_UNMAP) && softc->unmap_gran != 0 &&
3869 (c = lastcount % softc->unmap_gran) != 0) {
3873 scsi_ulto4b(lastcount - c, d[ranges - 1].length);
3876 scsi_ulto2b(ranges * 16 + 6, &buf[0]);
3877 scsi_ulto2b(ranges * 16, &buf[2]);
3879 scsi_unmap(&ccb->csio,
3880 /*retries*/da_retry_count,
3882 /*tag_action*/MSG_SIMPLE_Q_TAG,
3885 /*dxfer_len*/ ranges * 16 + 8,
3886 /*sense_len*/SSD_FULL_SIZE,
3887 da_default_timeout * 1000);
3888 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
3889 ccb->ccb_h.flags |= CAM_UNLOCKED;
3890 cam_iosched_submit_trim(softc->cam_iosched);
3894 da_delete_trim(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
3896 struct da_softc *softc = (struct da_softc *)periph->softc;
3898 uint8_t *buf = softc->unmap_buf;
3899 uint64_t lastlba = (uint64_t)-1;
3902 uint32_t lastcount = 0, c, requestcount;
3903 int ranges = 0, off, block_count;
3905 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
3908 if (bp1 != bp)//XXX imp XXX
3909 bioq_insert_tail(&softc->delete_run_queue, bp1);
3910 lba = bp1->bio_pblkno;
3911 count = bp1->bio_bcount / softc->params.secsize;
3912 requestcount = count;
3914 /* Try to extend the previous range. */
3915 if (lba == lastlba) {
3916 c = omin(count, ATA_DSM_RANGE_MAX - lastcount);
3918 off = (ranges - 1) * 8;
3919 buf[off + 6] = lastcount & 0xff;
3920 buf[off + 7] = (lastcount >> 8) & 0xff;
3926 c = omin(count, ATA_DSM_RANGE_MAX);
3929 buf[off + 0] = lba & 0xff;
3930 buf[off + 1] = (lba >> 8) & 0xff;
3931 buf[off + 2] = (lba >> 16) & 0xff;
3932 buf[off + 3] = (lba >> 24) & 0xff;
3933 buf[off + 4] = (lba >> 32) & 0xff;
3934 buf[off + 5] = (lba >> 40) & 0xff;
3935 buf[off + 6] = c & 0xff;
3936 buf[off + 7] = (c >> 8) & 0xff;
3941 if (count != 0 && ranges == softc->trim_max_ranges) {
3942 xpt_print(periph->path,
3943 "%s issuing short delete %ld > %ld\n",
3944 da_delete_method_desc[softc->delete_method],
3946 (softc->trim_max_ranges - ranges) *
3952 bp1 = cam_iosched_next_trim(softc->cam_iosched);
3955 if (bp1->bio_bcount / softc->params.secsize >
3956 (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX) {
3957 cam_iosched_put_back_trim(softc->cam_iosched, bp1);
3962 block_count = howmany(ranges, ATA_DSM_BLK_RANGES);
3963 scsi_ata_trim(&ccb->csio,
3964 /*retries*/da_retry_count,
3966 /*tag_action*/MSG_SIMPLE_Q_TAG,
3969 /*dxfer_len*/block_count * ATA_DSM_BLK_SIZE,
3970 /*sense_len*/SSD_FULL_SIZE,
3971 da_default_timeout * 1000);
3972 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
3973 ccb->ccb_h.flags |= CAM_UNLOCKED;
3974 cam_iosched_submit_trim(softc->cam_iosched);
3978 * We calculate ws_max_blks here based off d_delmaxsize instead
3979 * of using softc->ws_max_blks as it is absolute max for the
3980 * device not the protocol max which may well be lower.
3983 da_delete_ws(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
3985 struct da_softc *softc;
3987 uint64_t ws_max_blks;
3989 uint64_t count; /* forward compat with WS32 */
3991 softc = (struct da_softc *)periph->softc;
3992 ws_max_blks = softc->disk->d_delmaxsize / softc->params.secsize;
3993 lba = bp->bio_pblkno;
3997 if (bp1 != bp)//XXX imp XXX
3998 bioq_insert_tail(&softc->delete_run_queue, bp1);
3999 count += bp1->bio_bcount / softc->params.secsize;
4000 if (count > ws_max_blks) {
4001 xpt_print(periph->path,
4002 "%s issuing short delete %ld > %ld\n",
4003 da_delete_method_desc[softc->delete_method],
4004 count, ws_max_blks);
4005 count = omin(count, ws_max_blks);
4008 bp1 = cam_iosched_next_trim(softc->cam_iosched);
4011 if (lba + count != bp1->bio_pblkno ||
4012 count + bp1->bio_bcount /
4013 softc->params.secsize > ws_max_blks) {
4014 cam_iosched_put_back_trim(softc->cam_iosched, bp1);
4019 scsi_write_same(&ccb->csio,
4020 /*retries*/da_retry_count,
4022 /*tag_action*/MSG_SIMPLE_Q_TAG,
4023 /*byte2*/softc->delete_method ==
4024 DA_DELETE_ZERO ? 0 : SWS_UNMAP,
4025 softc->delete_method == DA_DELETE_WS16 ? 16 : 10,
4027 /*block_count*/count,
4028 /*data_ptr*/ __DECONST(void *, zero_region),
4029 /*dxfer_len*/ softc->params.secsize,
4030 /*sense_len*/SSD_FULL_SIZE,
4031 da_default_timeout * 1000);
4032 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
4033 ccb->ccb_h.flags |= CAM_UNLOCKED;
4034 cam_iosched_submit_trim(softc->cam_iosched);
4038 cmd6workaround(union ccb *ccb)
4040 struct scsi_rw_6 cmd6;
4041 struct scsi_rw_10 *cmd10;
4042 struct da_softc *softc;
4047 cdb = ccb->csio.cdb_io.cdb_bytes;
4048 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
4050 if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
4051 da_delete_methods old_method = softc->delete_method;
4054 * Typically there are two reasons for failure here
4055 * 1. Delete method was detected as supported but isn't
4056 * 2. Delete failed due to invalid params e.g. too big
4058 * While we will attempt to choose an alternative delete method
4059 * this may result in short deletes if the existing delete
4060 * requests from geom are big for the new method chosen.
4062 * This method assumes that the error which triggered this
4063 * will not retry the io otherwise a panic will occur
4065 dadeleteflag(softc, old_method, 0);
4066 dadeletemethodchoose(softc, DA_DELETE_DISABLE);
4067 if (softc->delete_method == DA_DELETE_DISABLE)
4068 xpt_print(ccb->ccb_h.path,
4069 "%s failed, disabling BIO_DELETE\n",
4070 da_delete_method_desc[old_method]);
4072 xpt_print(ccb->ccb_h.path,
4073 "%s failed, switching to %s BIO_DELETE\n",
4074 da_delete_method_desc[old_method],
4075 da_delete_method_desc[softc->delete_method]);
4077 while ((bp = bioq_takefirst(&softc->delete_run_queue)) != NULL)
4078 cam_iosched_queue_work(softc->cam_iosched, bp);
4079 cam_iosched_queue_work(softc->cam_iosched,
4080 (struct bio *)ccb->ccb_h.ccb_bp);
4081 ccb->ccb_h.ccb_bp = NULL;
4085 /* Detect unsupported PREVENT ALLOW MEDIUM REMOVAL. */
4086 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
4087 (*cdb == PREVENT_ALLOW) &&
4088 (softc->quirks & DA_Q_NO_PREVENT) == 0) {
4090 xpt_print(ccb->ccb_h.path,
4091 "PREVENT ALLOW MEDIUM REMOVAL not supported.\n");
4092 softc->quirks |= DA_Q_NO_PREVENT;
4096 /* Detect unsupported SYNCHRONIZE CACHE(10). */
4097 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
4098 (*cdb == SYNCHRONIZE_CACHE) &&
4099 (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
4101 xpt_print(ccb->ccb_h.path,
4102 "SYNCHRONIZE CACHE(10) not supported.\n");
4103 softc->quirks |= DA_Q_NO_SYNC_CACHE;
4104 softc->disk->d_flags &= ~DISKFLAG_CANFLUSHCACHE;
4108 /* Translation only possible if CDB is an array and cmd is R/W6 */
4109 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
4110 (*cdb != READ_6 && *cdb != WRITE_6))
4113 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
4114 "increasing minimum_cmd_size to 10.\n");
4115 softc->minimum_cmd_size = 10;
4117 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
4118 cmd10 = (struct scsi_rw_10 *)cdb;
4119 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
4121 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
4122 cmd10->reserved = 0;
4123 scsi_ulto2b(cmd6.length, cmd10->length);
4124 cmd10->control = cmd6.control;
4125 ccb->csio.cdb_len = sizeof(*cmd10);
4127 /* Requeue request, unfreezing queue if necessary */
4128 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
4129 ccb->ccb_h.status = CAM_REQUEUE_REQ;
4132 cam_release_devq(ccb->ccb_h.path,
4136 /*getcount_only*/0);
4142 dazonedone(struct cam_periph *periph, union ccb *ccb)
4144 struct da_softc *softc;
4147 softc = periph->softc;
4148 bp = (struct bio *)ccb->ccb_h.ccb_bp;
4150 switch (bp->bio_zone.zone_cmd) {
4151 case DISK_ZONE_OPEN:
4152 case DISK_ZONE_CLOSE:
4153 case DISK_ZONE_FINISH:
4156 case DISK_ZONE_REPORT_ZONES: {
4158 struct disk_zone_report *rep;
4159 struct scsi_report_zones_hdr *hdr;
4160 struct scsi_report_zones_desc *desc;
4161 struct disk_zone_rep_entry *entry;
4162 uint32_t hdr_len, num_avail;
4163 uint32_t num_to_fill, i;
4166 rep = &bp->bio_zone.zone_params.report;
4167 avail_len = ccb->csio.dxfer_len - ccb->csio.resid;
4169 * Note that bio_resid isn't normally used for zone
4170 * commands, but it is used by devstat_end_transaction_bio()
4171 * to determine how much data was transferred. Because
4172 * the size of the SCSI/ATA data structures is different
4173 * than the size of the BIO interface structures, the
4174 * amount of data actually transferred from the drive will
4175 * be different than the amount of data transferred to
4178 bp->bio_resid = ccb->csio.resid;
4179 hdr = (struct scsi_report_zones_hdr *)ccb->csio.data_ptr;
4180 if (avail_len < sizeof(*hdr)) {
4182 * Is there a better error than EIO here? We asked
4183 * for at least the header, and we got less than
4186 bp->bio_error = EIO;
4187 bp->bio_flags |= BIO_ERROR;
4188 bp->bio_resid = bp->bio_bcount;
4192 if (softc->zone_interface == DA_ZONE_IF_ATA_PASS)
4197 hdr_len = ata ? le32dec(hdr->length) :
4198 scsi_4btoul(hdr->length);
4200 rep->entries_available = hdr_len / sizeof(*desc);
4202 rep->entries_available = 0;
4204 * NOTE: using the same values for the BIO version of the
4205 * same field as the SCSI/ATA values. This means we could
4206 * get some additional values that aren't defined in bio.h
4207 * if more values of the same field are defined later.
4209 rep->header.same = hdr->byte4 & SRZ_SAME_MASK;
4210 rep->header.maximum_lba = ata ? le64dec(hdr->maximum_lba) :
4211 scsi_8btou64(hdr->maximum_lba);
4213 * If the drive reports no entries that match the query,
4217 rep->entries_filled = 0;
4221 num_avail = min((avail_len - sizeof(*hdr)) / sizeof(*desc),
4222 hdr_len / sizeof(*desc));
4224 * If the drive didn't return any data, then we're done.
4226 if (num_avail == 0) {
4227 rep->entries_filled = 0;
4231 num_to_fill = min(num_avail, rep->entries_allocated);
4233 * If the user didn't allocate any entries for us to fill,
4236 if (num_to_fill == 0) {
4237 rep->entries_filled = 0;
4241 for (i = 0, desc = &hdr->desc_list[0], entry=&rep->entries[0];
4242 i < num_to_fill; i++, desc++, entry++) {
4244 * NOTE: we're mapping the values here directly
4245 * from the SCSI/ATA bit definitions to the bio.h
4246 * definitons. There is also a warning in
4247 * disk_zone.h, but the impact is that if
4248 * additional values are added in the SCSI/ATA
4249 * specs these will be visible to consumers of
4252 entry->zone_type = desc->zone_type & SRZ_TYPE_MASK;
4253 entry->zone_condition =
4254 (desc->zone_flags & SRZ_ZONE_COND_MASK) >>
4255 SRZ_ZONE_COND_SHIFT;
4256 entry->zone_flags |= desc->zone_flags &
4257 (SRZ_ZONE_NON_SEQ|SRZ_ZONE_RESET);
4258 entry->zone_length =
4259 ata ? le64dec(desc->zone_length) :
4260 scsi_8btou64(desc->zone_length);
4261 entry->zone_start_lba =
4262 ata ? le64dec(desc->zone_start_lba) :
4263 scsi_8btou64(desc->zone_start_lba);
4264 entry->write_pointer_lba =
4265 ata ? le64dec(desc->write_pointer_lba) :
4266 scsi_8btou64(desc->write_pointer_lba);
4268 rep->entries_filled = num_to_fill;
4271 case DISK_ZONE_GET_PARAMS:
4274 * In theory we should not get a GET_PARAMS bio, since it
4275 * should be handled without queueing the command to the
4278 panic("%s: Invalid zone command %d", __func__,
4279 bp->bio_zone.zone_cmd);
4283 if (bp->bio_zone.zone_cmd == DISK_ZONE_REPORT_ZONES)
4284 free(ccb->csio.data_ptr, M_SCSIDA);
4288 dadone(struct cam_periph *periph, union ccb *done_ccb)
4290 struct da_softc *softc;
4291 struct ccb_scsiio *csio;
4295 softc = (struct da_softc *)periph->softc;
4296 priority = done_ccb->ccb_h.pinfo.priority;
4298 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
4300 csio = &done_ccb->csio;
4301 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
4302 if (csio->bio != NULL)
4303 biotrack(csio->bio, __func__);
4305 state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
4307 case DA_CCB_BUFFER_IO:
4310 struct bio *bp, *bp1;
4312 cam_periph_lock(periph);
4313 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
4314 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4318 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
4323 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
4324 if (error == ERESTART) {
4326 * A retry was scheduled, so
4329 cam_periph_unlock(periph);
4332 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
4337 * return all queued I/O with EIO, so that
4338 * the client can retry these I/Os in the
4339 * proper order should it attempt to recover.
4344 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
4346 * Catastrophic error. Mark our pack as
4350 * XXX See if this is really a media
4353 xpt_print(periph->path,
4354 "Invalidating pack\n");
4355 softc->flags |= DA_FLAG_PACK_INVALID;
4357 softc->invalidations++;
4359 queued_error = ENXIO;
4361 cam_iosched_flush(softc->cam_iosched, NULL,
4364 bp->bio_error = error;
4365 bp->bio_resid = bp->bio_bcount;
4366 bp->bio_flags |= BIO_ERROR;
4368 } else if (bp != NULL) {
4369 if (state == DA_CCB_DELETE)
4372 bp->bio_resid = csio->resid;
4374 if (bp->bio_resid != 0)
4375 bp->bio_flags |= BIO_ERROR;
4377 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
4378 cam_release_devq(done_ccb->ccb_h.path,
4382 /*getcount_only*/0);
4383 } else if (bp != NULL) {
4384 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
4385 panic("REQ_CMP with QFRZN");
4386 if (bp->bio_cmd == BIO_ZONE)
4387 dazonedone(periph, done_ccb);
4388 else if (state == DA_CCB_DELETE)
4391 bp->bio_resid = csio->resid;
4392 if ((csio->resid > 0)
4393 && (bp->bio_cmd != BIO_ZONE))
4394 bp->bio_flags |= BIO_ERROR;
4395 if (softc->error_inject != 0) {
4396 bp->bio_error = softc->error_inject;
4397 bp->bio_resid = bp->bio_bcount;
4398 bp->bio_flags |= BIO_ERROR;
4399 softc->error_inject = 0;
4404 biotrack(bp, __func__);
4405 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
4406 if (LIST_EMPTY(&softc->pending_ccbs))
4407 softc->flags |= DA_FLAG_WAS_OTAG;
4410 * We need to call cam_iosched before we call biodone so that we
4411 * don't measure any activity that happens in the completion
4412 * routine, which in the case of sendfile can be quite
4415 cam_iosched_bio_complete(softc->cam_iosched, bp, done_ccb);
4416 xpt_release_ccb(done_ccb);
4417 if (state == DA_CCB_DELETE) {
4418 TAILQ_HEAD(, bio) queue;
4421 TAILQ_CONCAT(&queue, &softc->delete_run_queue.queue, bio_queue);
4422 softc->delete_run_queue.insert_point = NULL;
4424 * Normally, the xpt_release_ccb() above would make sure
4425 * that when we have more work to do, that work would
4426 * get kicked off. However, we specifically keep
4427 * delete_running set to 0 before the call above to
4428 * allow other I/O to progress when many BIO_DELETE
4429 * requests are pushed down. We set delete_running to 0
4430 * and call daschedule again so that we don't stall if
4431 * there are no other I/Os pending apart from BIO_DELETEs.
4433 cam_iosched_trim_done(softc->cam_iosched);
4435 cam_periph_unlock(periph);
4436 while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
4437 TAILQ_REMOVE(&queue, bp1, bio_queue);
4438 bp1->bio_error = bp->bio_error;
4439 if (bp->bio_flags & BIO_ERROR) {
4440 bp1->bio_flags |= BIO_ERROR;
4441 bp1->bio_resid = bp1->bio_bcount;
4448 cam_periph_unlock(periph);
4454 case DA_CCB_PROBE_WP:
4456 struct scsi_mode_header_6 *mode_hdr6;
4457 struct scsi_mode_header_10 *mode_hdr10;
4460 if (softc->minimum_cmd_size > 6) {
4461 mode_hdr10 = (struct scsi_mode_header_10 *)csio->data_ptr;
4462 dev_spec = mode_hdr10->dev_spec;
4464 mode_hdr6 = (struct scsi_mode_header_6 *)csio->data_ptr;
4465 dev_spec = mode_hdr6->dev_spec;
4467 if (cam_ccb_status(done_ccb) == CAM_REQ_CMP) {
4468 if ((dev_spec & 0x80) != 0)
4469 softc->disk->d_flags |= DISKFLAG_WRITE_PROTECT;
4471 softc->disk->d_flags &= ~DISKFLAG_WRITE_PROTECT;
4475 error = daerror(done_ccb, CAM_RETRY_SELTO,
4476 SF_RETRY_UA|SF_NO_PRINT);
4477 if (error == ERESTART)
4479 else if (error != 0) {
4480 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
4481 /* Don't wedge this device's queue */
4482 cam_release_devq(done_ccb->ccb_h.path,
4486 /*getcount_only*/0);
4491 free(csio->data_ptr, M_SCSIDA);
4492 xpt_release_ccb(done_ccb);
4493 if ((softc->flags & DA_FLAG_CAN_RC16) != 0)
4494 softc->state = DA_STATE_PROBE_RC16;
4496 softc->state = DA_STATE_PROBE_RC;
4497 xpt_schedule(periph, priority);
4500 case DA_CCB_PROBE_RC:
4501 case DA_CCB_PROBE_RC16:
4503 struct scsi_read_capacity_data *rdcap;
4504 struct scsi_read_capacity_data_long *rcaplong;
4511 /* XXX TODO: can this be a malloc? */
4512 announce_buf = softc->announce_temp;
4513 bzero(announce_buf, DA_ANNOUNCETMP_SZ);
4515 if (state == DA_CCB_PROBE_RC)
4516 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
4518 rcaplong = (struct scsi_read_capacity_data_long *)
4521 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4522 struct disk_params *dp;
4523 uint32_t block_size;
4525 u_int lalba; /* Lowest aligned LBA. */
4527 if (state == DA_CCB_PROBE_RC) {
4528 block_size = scsi_4btoul(rdcap->length);
4529 maxsector = scsi_4btoul(rdcap->addr);
4533 * According to SBC-2, if the standard 10
4534 * byte READ CAPACITY command returns 2^32,
4535 * we should issue the 16 byte version of
4536 * the command, since the device in question
4537 * has more sectors than can be represented
4538 * with the short version of the command.
4540 if (maxsector == 0xffffffff) {
4541 free(rdcap, M_SCSIDA);
4542 xpt_release_ccb(done_ccb);
4543 softc->state = DA_STATE_PROBE_RC16;
4544 xpt_schedule(periph, priority);
4548 block_size = scsi_4btoul(rcaplong->length);
4549 maxsector = scsi_8btou64(rcaplong->addr);
4550 lalba = scsi_2btoul(rcaplong->lalba_lbp);
4554 * Because GEOM code just will panic us if we
4555 * give them an 'illegal' value we'll avoid that
4558 if (block_size == 0) {
4563 if (block_size >= MAXPHYS) {
4564 xpt_print(periph->path,
4565 "unsupportable block size %ju\n",
4566 (uintmax_t) block_size);
4567 announce_buf = NULL;
4568 cam_periph_invalidate(periph);
4571 * We pass rcaplong into dasetgeom(),
4572 * because it will only use it if it is
4575 dasetgeom(periph, block_size, maxsector,
4576 rcaplong, sizeof(*rcaplong));
4577 lbp = (lalba & SRC16_LBPME_A);
4578 dp = &softc->params;
4579 snprintf(announce_buf, DA_ANNOUNCETMP_SZ,
4580 "%juMB (%ju %u byte sectors)",
4581 ((uintmax_t)dp->secsize * dp->sectors) /
4583 (uintmax_t)dp->sectors, dp->secsize);
4589 * Retry any UNIT ATTENTION type errors. They
4590 * are expected at boot.
4592 error = daerror(done_ccb, CAM_RETRY_SELTO,
4593 SF_RETRY_UA|SF_NO_PRINT);
4594 if (error == ERESTART) {
4596 * A retry was scheuled, so
4600 } else if (error != 0) {
4602 int sense_key, error_code;
4605 struct ccb_getdev cgd;
4607 /* Don't wedge this device's queue */
4608 status = done_ccb->ccb_h.status;
4609 if ((status & CAM_DEV_QFRZN) != 0)
4610 cam_release_devq(done_ccb->ccb_h.path,
4614 /*getcount_only*/0);
4617 xpt_setup_ccb(&cgd.ccb_h,
4618 done_ccb->ccb_h.path,
4619 CAM_PRIORITY_NORMAL);
4620 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
4621 xpt_action((union ccb *)&cgd);
4623 if (scsi_extract_sense_ccb(done_ccb,
4624 &error_code, &sense_key, &asc, &ascq))
4630 * If we tried READ CAPACITY(16) and failed,
4631 * fallback to READ CAPACITY(10).
4633 if ((state == DA_CCB_PROBE_RC16) &&
4634 (softc->flags & DA_FLAG_CAN_RC16) &&
4635 (((csio->ccb_h.status & CAM_STATUS_MASK) ==
4638 (error_code == SSD_CURRENT_ERROR) &&
4639 (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
4640 cam_periph_lock(periph);
4641 softc->flags &= ~DA_FLAG_CAN_RC16;
4642 cam_periph_unlock(periph);
4643 free(rdcap, M_SCSIDA);
4644 xpt_release_ccb(done_ccb);
4645 softc->state = DA_STATE_PROBE_RC;
4646 xpt_schedule(periph, priority);
4651 * Attach to anything that claims to be a
4652 * direct access or optical disk device,
4653 * as long as it doesn't return a "Logical
4654 * unit not supported" (0x25) error.
4655 * "Internal Target Failure" (0x44) is also
4656 * special and typically means that the
4657 * device is a SATA drive behind a SATL
4658 * translation that's fallen into a
4659 * terminally fatal state.
4662 && (asc != 0x25) && (asc != 0x44)
4663 && (error_code == SSD_CURRENT_ERROR)) {
4664 const char *sense_key_desc;
4665 const char *asc_desc;
4667 dasetgeom(periph, 512, -1, NULL, 0);
4668 scsi_sense_desc(sense_key, asc, ascq,
4672 snprintf(announce_buf,
4674 "Attempt to query device "
4675 "size failed: %s, %s",
4676 sense_key_desc, asc_desc);
4682 xpt_print(periph->path,
4683 "got CAM status %#x\n",
4684 done_ccb->ccb_h.status);
4687 xpt_print(periph->path, "fatal error, "
4688 "failed to attach to device\n");
4690 announce_buf = NULL;
4693 * Free up resources.
4695 cam_periph_invalidate(periph);
4699 free(csio->data_ptr, M_SCSIDA);
4700 if (announce_buf != NULL &&
4701 ((softc->flags & DA_FLAG_ANNOUNCED) == 0)) {
4704 sbuf_new(&sb, softc->announcebuf, DA_ANNOUNCE_SZ,
4706 xpt_announce_periph_sbuf(periph, &sb, announce_buf);
4707 xpt_announce_quirks_sbuf(periph, &sb, softc->quirks,
4713 * Create our sysctl variables, now that we know
4714 * we have successfully attached.
4716 /* increase the refcount */
4717 if (da_periph_acquire(periph, DA_REF_SYSCTL) == 0) {
4719 taskqueue_enqueue(taskqueue_thread,
4720 &softc->sysctl_task);
4722 /* XXX This message is useless! */
4723 xpt_print(periph->path, "fatal error, "
4724 "could not acquire reference count\n");
4728 /* We already probed the device. */
4729 if (softc->flags & DA_FLAG_PROBED) {
4730 daprobedone(periph, done_ccb);
4734 /* Ensure re-probe doesn't see old delete. */
4735 softc->delete_available = 0;
4736 dadeleteflag(softc, DA_DELETE_ZERO, 1);
4737 if (lbp && (softc->quirks & DA_Q_NO_UNMAP) == 0) {
4739 * Based on older SBC-3 spec revisions
4740 * any of the UNMAP methods "may" be
4741 * available via LBP given this flag so
4742 * we flag all of them as available and
4743 * then remove those which further
4744 * probes confirm aren't available
4747 * We could also check readcap(16) p_type
4748 * flag to exclude one or more invalid
4749 * write same (X) types here
4751 dadeleteflag(softc, DA_DELETE_WS16, 1);
4752 dadeleteflag(softc, DA_DELETE_WS10, 1);
4753 dadeleteflag(softc, DA_DELETE_UNMAP, 1);
4755 xpt_release_ccb(done_ccb);
4756 softc->state = DA_STATE_PROBE_LBP;
4757 xpt_schedule(periph, priority);
4761 xpt_release_ccb(done_ccb);
4762 softc->state = DA_STATE_PROBE_BDC;
4763 xpt_schedule(periph, priority);
4766 case DA_CCB_PROBE_LBP:
4768 struct scsi_vpd_logical_block_prov *lbp;
4770 lbp = (struct scsi_vpd_logical_block_prov *)csio->data_ptr;
4772 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4774 * T10/1799-D Revision 31 states at least one of these
4775 * must be supported but we don't currently enforce this.
4777 dadeleteflag(softc, DA_DELETE_WS16,
4778 (lbp->flags & SVPD_LBP_WS16));
4779 dadeleteflag(softc, DA_DELETE_WS10,
4780 (lbp->flags & SVPD_LBP_WS10));
4781 dadeleteflag(softc, DA_DELETE_UNMAP,
4782 (lbp->flags & SVPD_LBP_UNMAP));
4785 error = daerror(done_ccb, CAM_RETRY_SELTO,
4786 SF_RETRY_UA|SF_NO_PRINT);
4787 if (error == ERESTART)
4789 else if (error != 0) {
4790 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
4791 /* Don't wedge this device's queue */
4792 cam_release_devq(done_ccb->ccb_h.path,
4796 /*getcount_only*/0);
4800 * Failure indicates we don't support any SBC-3
4801 * delete methods with UNMAP
4806 free(lbp, M_SCSIDA);
4807 xpt_release_ccb(done_ccb);
4808 softc->state = DA_STATE_PROBE_BLK_LIMITS;
4809 xpt_schedule(periph, priority);
4812 case DA_CCB_PROBE_BLK_LIMITS:
4814 struct scsi_vpd_block_limits *block_limits;
4816 block_limits = (struct scsi_vpd_block_limits *)csio->data_ptr;
4818 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4819 uint32_t max_txfer_len = scsi_4btoul(
4820 block_limits->max_txfer_len);
4821 uint32_t max_unmap_lba_cnt = scsi_4btoul(
4822 block_limits->max_unmap_lba_cnt);
4823 uint32_t max_unmap_blk_cnt = scsi_4btoul(
4824 block_limits->max_unmap_blk_cnt);
4825 uint32_t unmap_gran = scsi_4btoul(
4826 block_limits->opt_unmap_grain);
4827 uint32_t unmap_gran_align = scsi_4btoul(
4828 block_limits->unmap_grain_align);
4829 uint64_t ws_max_blks = scsi_8btou64(
4830 block_limits->max_write_same_length);
4832 if (max_txfer_len != 0) {
4833 softc->disk->d_maxsize = MIN(softc->maxio,
4834 (off_t)max_txfer_len * softc->params.secsize);
4838 * We should already support UNMAP but we check lba
4839 * and block count to be sure
4841 if (max_unmap_lba_cnt != 0x00L &&
4842 max_unmap_blk_cnt != 0x00L) {
4843 softc->unmap_max_lba = max_unmap_lba_cnt;
4844 softc->unmap_max_ranges = min(max_unmap_blk_cnt,
4846 if (unmap_gran > 1) {
4847 softc->unmap_gran = unmap_gran;
4848 if (unmap_gran_align & 0x80000000) {
4849 softc->unmap_gran_align =
4856 * Unexpected UNMAP limits which means the
4857 * device doesn't actually support UNMAP
4859 dadeleteflag(softc, DA_DELETE_UNMAP, 0);
4862 if (ws_max_blks != 0x00L)
4863 softc->ws_max_blks = ws_max_blks;
4866 error = daerror(done_ccb, CAM_RETRY_SELTO,
4867 SF_RETRY_UA|SF_NO_PRINT);
4868 if (error == ERESTART)
4870 else if (error != 0) {
4871 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
4872 /* Don't wedge this device's queue */
4873 cam_release_devq(done_ccb->ccb_h.path,
4877 /*getcount_only*/0);
4881 * Failure here doesn't mean UNMAP is not
4882 * supported as this is an optional page.
4884 softc->unmap_max_lba = 1;
4885 softc->unmap_max_ranges = 1;
4889 free(block_limits, M_SCSIDA);
4890 xpt_release_ccb(done_ccb);
4891 softc->state = DA_STATE_PROBE_BDC;
4892 xpt_schedule(periph, priority);
4895 case DA_CCB_PROBE_BDC:
4897 struct scsi_vpd_block_device_characteristics *bdc;
4899 bdc = (struct scsi_vpd_block_device_characteristics *)
4902 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
4906 * Disable queue sorting for non-rotational media
4909 u_int16_t old_rate = softc->disk->d_rotation_rate;
4911 valid_len = csio->dxfer_len - csio->resid;
4912 if (SBDC_IS_PRESENT(bdc, valid_len,
4913 medium_rotation_rate)) {
4914 softc->disk->d_rotation_rate =
4915 scsi_2btoul(bdc->medium_rotation_rate);
4916 if (softc->disk->d_rotation_rate ==
4917 SVPD_BDC_RATE_NON_ROTATING) {
4918 cam_iosched_set_sort_queue(
4919 softc->cam_iosched, 0);
4920 softc->rotating = 0;
4922 if (softc->disk->d_rotation_rate != old_rate) {
4923 disk_attr_changed(softc->disk,
4924 "GEOM::rotation_rate", M_NOWAIT);
4927 if ((SBDC_IS_PRESENT(bdc, valid_len, flags))
4928 && (softc->zone_mode == DA_ZONE_NONE)) {
4931 if (scsi_vpd_supported_page(periph,
4932 SVPD_ATA_INFORMATION))
4938 * The Zoned field will only be set for
4939 * Drive Managed and Host Aware drives. If
4940 * they are Host Managed, the device type
4941 * in the standard INQUIRY data should be
4942 * set to T_ZBC_HM (0x14).
4944 if ((bdc->flags & SVPD_ZBC_MASK) ==
4946 softc->zone_mode = DA_ZONE_HOST_AWARE;
4947 softc->zone_interface = (ata_proto) ?
4948 DA_ZONE_IF_ATA_SAT : DA_ZONE_IF_SCSI;
4949 } else if ((bdc->flags & SVPD_ZBC_MASK) ==
4951 softc->zone_mode =DA_ZONE_DRIVE_MANAGED;
4952 softc->zone_interface = (ata_proto) ?
4953 DA_ZONE_IF_ATA_SAT : DA_ZONE_IF_SCSI;
4954 } else if ((bdc->flags & SVPD_ZBC_MASK) !=
4956 xpt_print(periph->path, "Unknown zoned "
4958 bdc->flags & SVPD_ZBC_MASK);
4963 error = daerror(done_ccb, CAM_RETRY_SELTO,
4964 SF_RETRY_UA|SF_NO_PRINT);
4965 if (error == ERESTART)
4967 else if (error != 0) {
4968 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
4969 /* Don't wedge this device's queue */
4970 cam_release_devq(done_ccb->ccb_h.path,
4974 /*getcount_only*/0);
4979 free(bdc, M_SCSIDA);
4980 xpt_release_ccb(done_ccb);
4981 softc->state = DA_STATE_PROBE_ATA;
4982 xpt_schedule(periph, priority);
4985 case DA_CCB_PROBE_ATA:
4988 struct ata_params *ata_params;
4993 ata_params = (struct ata_params *)csio->data_ptr;
4994 ptr = (uint16_t *)ata_params;
4998 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5001 for (i = 0; i < sizeof(*ata_params) / 2; i++)
5002 ptr[i] = le16toh(ptr[i]);
5003 if (ata_params->support_dsm & ATA_SUPPORT_DSM_TRIM &&
5004 (softc->quirks & DA_Q_NO_UNMAP) == 0) {
5005 dadeleteflag(softc, DA_DELETE_ATA_TRIM, 1);
5006 if (ata_params->max_dsm_blocks != 0)
5007 softc->trim_max_ranges = min(
5008 softc->trim_max_ranges,
5009 ata_params->max_dsm_blocks *
5010 ATA_DSM_BLK_RANGES);
5013 * Disable queue sorting for non-rotational media
5016 old_rate = softc->disk->d_rotation_rate;
5017 softc->disk->d_rotation_rate =
5018 ata_params->media_rotation_rate;
5019 if (softc->disk->d_rotation_rate ==
5020 ATA_RATE_NON_ROTATING) {
5021 cam_iosched_set_sort_queue(softc->cam_iosched, 0);
5022 softc->rotating = 0;
5024 if (softc->disk->d_rotation_rate != old_rate) {
5025 disk_attr_changed(softc->disk,
5026 "GEOM::rotation_rate", M_NOWAIT);
5029 cam_periph_assert(periph, MA_OWNED);
5030 if (ata_params->capabilities1 & ATA_SUPPORT_DMA)
5031 softc->flags |= DA_FLAG_CAN_ATA_DMA;
5033 if (ata_params->support.extension &
5035 softc->flags |= DA_FLAG_CAN_ATA_LOG;
5038 * At this point, if we have a SATA host aware drive,
5039 * we communicate via ATA passthrough unless the
5040 * SAT layer supports ZBC -> ZAC translation. In
5044 * XXX KDM figure out how to detect a host managed
5047 if (softc->zone_mode == DA_ZONE_NONE) {
5049 * Note that we don't override the zone
5050 * mode or interface if it has already been
5051 * set. This is because it has either been
5052 * set as a quirk, or when we probed the
5053 * SCSI Block Device Characteristics page,
5054 * the zoned field was set. The latter
5055 * means that the SAT layer supports ZBC to
5056 * ZAC translation, and we would prefer to
5057 * use that if it is available.
5059 if ((ata_params->support3 &
5060 ATA_SUPPORT_ZONE_MASK) ==
5061 ATA_SUPPORT_ZONE_HOST_AWARE) {
5062 softc->zone_mode = DA_ZONE_HOST_AWARE;
5063 softc->zone_interface =
5064 DA_ZONE_IF_ATA_PASS;
5065 } else if ((ata_params->support3 &
5066 ATA_SUPPORT_ZONE_MASK) ==
5067 ATA_SUPPORT_ZONE_DEV_MANAGED) {
5068 softc->zone_mode =DA_ZONE_DRIVE_MANAGED;
5069 softc->zone_interface =
5070 DA_ZONE_IF_ATA_PASS;
5075 error = daerror(done_ccb, CAM_RETRY_SELTO,
5076 SF_RETRY_UA|SF_NO_PRINT);
5077 if (error == ERESTART)
5079 else if (error != 0) {
5080 if ((done_ccb->ccb_h.status &
5081 CAM_DEV_QFRZN) != 0) {
5082 /* Don't wedge this device's queue */
5083 cam_release_devq(done_ccb->ccb_h.path,
5087 /*getcount_only*/0);
5092 free(ata_params, M_SCSIDA);
5093 if ((softc->zone_mode == DA_ZONE_HOST_AWARE)
5094 || (softc->zone_mode == DA_ZONE_HOST_MANAGED)) {
5096 * If the ATA IDENTIFY failed, we could be talking
5097 * to a SCSI drive, although that seems unlikely,
5098 * since the drive did report that it supported the
5099 * ATA Information VPD page. If the ATA IDENTIFY
5100 * succeeded, and the SAT layer doesn't support
5101 * ZBC -> ZAC translation, continue on to get the
5102 * directory of ATA logs, and complete the rest of
5103 * the ZAC probe. If the SAT layer does support
5104 * ZBC -> ZAC translation, we want to use that,
5105 * and we'll probe the SCSI Zoned Block Device
5106 * Characteristics VPD page next.
5109 && (softc->flags & DA_FLAG_CAN_ATA_LOG)
5110 && (softc->zone_interface == DA_ZONE_IF_ATA_PASS))
5111 softc->state = DA_STATE_PROBE_ATA_LOGDIR;
5113 softc->state = DA_STATE_PROBE_ZONE;
5116 if (continue_probe != 0) {
5117 xpt_release_ccb(done_ccb);
5118 xpt_schedule(periph, priority);
5121 daprobedone(periph, done_ccb);
5124 case DA_CCB_PROBE_ATA_LOGDIR:
5128 cam_periph_lock(periph);
5129 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5131 softc->valid_logdir_len = 0;
5132 bzero(&softc->ata_logdir, sizeof(softc->ata_logdir));
5133 softc->valid_logdir_len =
5134 csio->dxfer_len - csio->resid;
5135 if (softc->valid_logdir_len > 0)
5136 bcopy(csio->data_ptr, &softc->ata_logdir,
5137 min(softc->valid_logdir_len,
5138 sizeof(softc->ata_logdir)));
5140 * Figure out whether the Identify Device log is
5141 * supported. The General Purpose log directory
5142 * has a header, and lists the number of pages
5143 * available for each GP log identified by the
5144 * offset into the list.
5146 if ((softc->valid_logdir_len >=
5147 ((ATA_IDENTIFY_DATA_LOG + 1) * sizeof(uint16_t)))
5148 && (le16dec(softc->ata_logdir.header) ==
5149 ATA_GP_LOG_DIR_VERSION)
5150 && (le16dec(&softc->ata_logdir.num_pages[
5151 (ATA_IDENTIFY_DATA_LOG *
5152 sizeof(uint16_t)) - sizeof(uint16_t)]) > 0)){
5153 softc->flags |= DA_FLAG_CAN_ATA_IDLOG;
5155 softc->flags &= ~DA_FLAG_CAN_ATA_IDLOG;
5158 error = daerror(done_ccb, CAM_RETRY_SELTO,
5159 SF_RETRY_UA|SF_NO_PRINT);
5160 if (error == ERESTART)
5162 else if (error != 0) {
5164 * If we can't get the ATA log directory,
5165 * then ATA logs are effectively not
5166 * supported even if the bit is set in the
5169 softc->flags &= ~(DA_FLAG_CAN_ATA_LOG |
5170 DA_FLAG_CAN_ATA_IDLOG);
5171 if ((done_ccb->ccb_h.status &
5172 CAM_DEV_QFRZN) != 0) {
5173 /* Don't wedge this device's queue */
5174 cam_release_devq(done_ccb->ccb_h.path,
5178 /*getcount_only*/0);
5182 cam_periph_unlock(periph);
5184 free(csio->data_ptr, M_SCSIDA);
5187 && (softc->flags & DA_FLAG_CAN_ATA_IDLOG)) {
5188 softc->state = DA_STATE_PROBE_ATA_IDDIR;
5189 xpt_release_ccb(done_ccb);
5190 xpt_schedule(periph, priority);
5193 daprobedone(periph, done_ccb);
5196 case DA_CCB_PROBE_ATA_IDDIR:
5200 cam_periph_lock(periph);
5201 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5202 off_t entries_offset, max_entries;
5205 softc->valid_iddir_len = 0;
5206 bzero(&softc->ata_iddir, sizeof(softc->ata_iddir));
5207 softc->flags &= ~(DA_FLAG_CAN_ATA_SUPCAP |
5208 DA_FLAG_CAN_ATA_ZONE);
5209 softc->valid_iddir_len =
5210 csio->dxfer_len - csio->resid;
5211 if (softc->valid_iddir_len > 0)
5212 bcopy(csio->data_ptr, &softc->ata_iddir,
5213 min(softc->valid_iddir_len,
5214 sizeof(softc->ata_iddir)));
5217 __offsetof(struct ata_identify_log_pages,entries);
5218 max_entries = softc->valid_iddir_len - entries_offset;
5219 if ((softc->valid_iddir_len > (entries_offset + 1))
5220 && (le64dec(softc->ata_iddir.header) ==
5222 && (softc->ata_iddir.entry_count > 0)) {
5225 num_entries = softc->ata_iddir.entry_count;
5226 num_entries = min(num_entries,
5227 softc->valid_iddir_len - entries_offset);
5228 for (i = 0; i < num_entries &&
5229 i < max_entries; i++) {
5230 if (softc->ata_iddir.entries[i] ==
5233 DA_FLAG_CAN_ATA_SUPCAP;
5234 else if (softc->ata_iddir.entries[i]==
5237 DA_FLAG_CAN_ATA_ZONE;
5240 DA_FLAG_CAN_ATA_SUPCAP)
5242 DA_FLAG_CAN_ATA_ZONE))
5247 error = daerror(done_ccb, CAM_RETRY_SELTO,
5248 SF_RETRY_UA|SF_NO_PRINT);
5249 if (error == ERESTART)
5251 else if (error != 0) {
5253 * If we can't get the ATA Identify Data log
5254 * directory, then it effectively isn't
5255 * supported even if the ATA Log directory
5256 * a non-zero number of pages present for
5259 softc->flags &= ~DA_FLAG_CAN_ATA_IDLOG;
5260 if ((done_ccb->ccb_h.status &
5261 CAM_DEV_QFRZN) != 0) {
5262 /* Don't wedge this device's queue */
5263 cam_release_devq(done_ccb->ccb_h.path,
5267 /*getcount_only*/0);
5271 cam_periph_unlock(periph);
5273 free(csio->data_ptr, M_SCSIDA);
5276 && (softc->flags & DA_FLAG_CAN_ATA_SUPCAP)) {
5277 softc->state = DA_STATE_PROBE_ATA_SUP;
5278 xpt_release_ccb(done_ccb);
5279 xpt_schedule(periph, priority);
5282 daprobedone(periph, done_ccb);
5285 case DA_CCB_PROBE_ATA_SUP:
5289 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5292 struct ata_identify_log_sup_cap *sup_cap;
5295 sup_cap = (struct ata_identify_log_sup_cap *)
5297 valid_len = csio->dxfer_len - csio->resid;
5299 __offsetof(struct ata_identify_log_sup_cap,
5300 sup_zac_cap) + 1 + sizeof(sup_cap->sup_zac_cap);
5301 if (valid_len >= needed_size) {
5302 uint64_t zoned, zac_cap;
5304 zoned = le64dec(sup_cap->zoned_cap);
5305 if (zoned & ATA_ZONED_VALID) {
5307 * This should have already been
5308 * set, because this is also in the
5309 * ATA identify data.
5311 if ((zoned & ATA_ZONED_MASK) ==
5312 ATA_SUPPORT_ZONE_HOST_AWARE)
5315 else if ((zoned & ATA_ZONED_MASK) ==
5316 ATA_SUPPORT_ZONE_DEV_MANAGED)
5318 DA_ZONE_DRIVE_MANAGED;
5321 zac_cap = le64dec(sup_cap->sup_zac_cap);
5322 if (zac_cap & ATA_SUP_ZAC_CAP_VALID) {
5323 if (zac_cap & ATA_REPORT_ZONES_SUP)
5324 softc->zone_flags |=
5325 DA_ZONE_FLAG_RZ_SUP;
5326 if (zac_cap & ATA_ND_OPEN_ZONE_SUP)
5327 softc->zone_flags |=
5328 DA_ZONE_FLAG_OPEN_SUP;
5329 if (zac_cap & ATA_ND_CLOSE_ZONE_SUP)
5330 softc->zone_flags |=
5331 DA_ZONE_FLAG_CLOSE_SUP;
5332 if (zac_cap & ATA_ND_FINISH_ZONE_SUP)
5333 softc->zone_flags |=
5334 DA_ZONE_FLAG_FINISH_SUP;
5335 if (zac_cap & ATA_ND_RWP_SUP)
5336 softc->zone_flags |=
5337 DA_ZONE_FLAG_RWP_SUP;
5340 * This field was introduced in
5341 * ACS-4, r08 on April 28th, 2015.
5342 * If the drive firmware was written
5343 * to an earlier spec, it won't have
5344 * the field. So, assume all
5345 * commands are supported.
5347 softc->zone_flags |=
5348 DA_ZONE_FLAG_SUP_MASK;
5353 error = daerror(done_ccb, CAM_RETRY_SELTO,
5354 SF_RETRY_UA|SF_NO_PRINT);
5355 if (error == ERESTART)
5357 else if (error != 0) {
5359 * If we can't get the ATA Identify Data
5360 * Supported Capabilities page, clear the
5363 cam_periph_lock(periph);
5364 softc->flags &= ~DA_FLAG_CAN_ATA_SUPCAP;
5365 cam_periph_unlock(periph);
5367 * And clear zone capabilities.
5369 softc->zone_flags &= ~DA_ZONE_FLAG_SUP_MASK;
5370 if ((done_ccb->ccb_h.status &
5371 CAM_DEV_QFRZN) != 0) {
5372 /* Don't wedge this device's queue */
5373 cam_release_devq(done_ccb->ccb_h.path,
5377 /*getcount_only*/0);
5382 free(csio->data_ptr, M_SCSIDA);
5385 && (softc->flags & DA_FLAG_CAN_ATA_ZONE)) {
5386 softc->state = DA_STATE_PROBE_ATA_ZONE;
5387 xpt_release_ccb(done_ccb);
5388 xpt_schedule(periph, priority);
5391 daprobedone(periph, done_ccb);
5394 case DA_CCB_PROBE_ATA_ZONE:
5398 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5399 struct ata_zoned_info_log *zi_log;
5403 zi_log = (struct ata_zoned_info_log *)csio->data_ptr;
5405 valid_len = csio->dxfer_len - csio->resid;
5406 needed_size = __offsetof(struct ata_zoned_info_log,
5407 version_info) + 1 + sizeof(zi_log->version_info);
5408 if (valid_len >= needed_size) {
5411 tmpvar = le64dec(zi_log->zoned_cap);
5412 if (tmpvar & ATA_ZDI_CAP_VALID) {
5413 if (tmpvar & ATA_ZDI_CAP_URSWRZ)
5414 softc->zone_flags |=
5415 DA_ZONE_FLAG_URSWRZ;
5417 softc->zone_flags &=
5418 ~DA_ZONE_FLAG_URSWRZ;
5420 tmpvar = le64dec(zi_log->optimal_seq_zones);
5421 if (tmpvar & ATA_ZDI_OPT_SEQ_VALID) {
5422 softc->zone_flags |=
5423 DA_ZONE_FLAG_OPT_SEQ_SET;
5424 softc->optimal_seq_zones = (tmpvar &
5425 ATA_ZDI_OPT_SEQ_MASK);
5427 softc->zone_flags &=
5428 ~DA_ZONE_FLAG_OPT_SEQ_SET;
5429 softc->optimal_seq_zones = 0;
5432 tmpvar =le64dec(zi_log->optimal_nonseq_zones);
5433 if (tmpvar & ATA_ZDI_OPT_NS_VALID) {
5434 softc->zone_flags |=
5435 DA_ZONE_FLAG_OPT_NONSEQ_SET;
5436 softc->optimal_nonseq_zones =
5437 (tmpvar & ATA_ZDI_OPT_NS_MASK);
5439 softc->zone_flags &=
5440 ~DA_ZONE_FLAG_OPT_NONSEQ_SET;
5441 softc->optimal_nonseq_zones = 0;
5444 tmpvar = le64dec(zi_log->max_seq_req_zones);
5445 if (tmpvar & ATA_ZDI_MAX_SEQ_VALID) {
5446 softc->zone_flags |=
5447 DA_ZONE_FLAG_MAX_SEQ_SET;
5448 softc->max_seq_zones =
5449 (tmpvar & ATA_ZDI_MAX_SEQ_MASK);
5451 softc->zone_flags &=
5452 ~DA_ZONE_FLAG_MAX_SEQ_SET;
5453 softc->max_seq_zones = 0;
5457 error = daerror(done_ccb, CAM_RETRY_SELTO,
5458 SF_RETRY_UA|SF_NO_PRINT);
5459 if (error == ERESTART)
5461 else if (error != 0) {
5462 cam_periph_lock(periph);
5463 softc->flags &= ~DA_FLAG_CAN_ATA_ZONE;
5464 softc->flags &= ~DA_ZONE_FLAG_SET_MASK;
5465 cam_periph_unlock(periph);
5467 if ((done_ccb->ccb_h.status &
5468 CAM_DEV_QFRZN) != 0) {
5469 /* Don't wedge this device's queue */
5470 cam_release_devq(done_ccb->ccb_h.path,
5474 /*getcount_only*/0);
5479 free(csio->data_ptr, M_SCSIDA);
5481 daprobedone(periph, done_ccb);
5484 case DA_CCB_PROBE_ZONE:
5488 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5491 struct scsi_vpd_zoned_bdc *zoned_bdc;
5494 zoned_bdc = (struct scsi_vpd_zoned_bdc *)
5496 valid_len = csio->dxfer_len - csio->resid;
5497 needed_len = __offsetof(struct scsi_vpd_zoned_bdc,
5498 max_seq_req_zones) + 1 +
5499 sizeof(zoned_bdc->max_seq_req_zones);
5500 if ((valid_len >= needed_len)
5501 && (scsi_2btoul(zoned_bdc->page_length) >=
5503 if (zoned_bdc->flags & SVPD_ZBDC_URSWRZ)
5504 softc->zone_flags |=
5505 DA_ZONE_FLAG_URSWRZ;
5507 softc->zone_flags &=
5508 ~DA_ZONE_FLAG_URSWRZ;
5509 softc->optimal_seq_zones =
5510 scsi_4btoul(zoned_bdc->optimal_seq_zones);
5511 softc->zone_flags |= DA_ZONE_FLAG_OPT_SEQ_SET;
5512 softc->optimal_nonseq_zones = scsi_4btoul(
5513 zoned_bdc->optimal_nonseq_zones);
5514 softc->zone_flags |=
5515 DA_ZONE_FLAG_OPT_NONSEQ_SET;
5516 softc->max_seq_zones =
5517 scsi_4btoul(zoned_bdc->max_seq_req_zones);
5518 softc->zone_flags |= DA_ZONE_FLAG_MAX_SEQ_SET;
5521 * All of the zone commands are mandatory for SCSI
5524 * XXX KDM this is valid as of September 2015.
5525 * Re-check this assumption once the SAT spec is
5526 * updated to support SCSI ZBC to ATA ZAC mapping.
5527 * Since ATA allows zone commands to be reported
5528 * as supported or not, this may not necessarily
5529 * be true for an ATA device behind a SAT (SCSI to
5530 * ATA Translation) layer.
5532 softc->zone_flags |= DA_ZONE_FLAG_SUP_MASK;
5534 error = daerror(done_ccb, CAM_RETRY_SELTO,
5535 SF_RETRY_UA|SF_NO_PRINT);
5536 if (error == ERESTART)
5538 else if (error != 0) {
5539 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5540 /* Don't wedge this device's queue */
5541 cam_release_devq(done_ccb->ccb_h.path,
5545 /*getcount_only*/0);
5549 daprobedone(periph, done_ccb);
5553 /* No-op. We're polling */
5557 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5559 if (daerror(done_ccb, CAM_RETRY_SELTO,
5560 SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
5563 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
5564 cam_release_devq(done_ccb->ccb_h.path,
5568 /*getcount_only*/0);
5570 xpt_release_ccb(done_ccb);
5571 da_periph_release_locked(periph, DA_REF_TUR);
5577 xpt_release_ccb(done_ccb);
5581 dareprobe(struct cam_periph *periph)
5583 struct da_softc *softc;
5586 softc = (struct da_softc *)periph->softc;
5588 /* Probe in progress; don't interfere. */
5589 if (softc->state != DA_STATE_NORMAL)
5592 status = da_periph_acquire(periph, DA_REF_REPROBE);
5593 KASSERT(status == 0, ("dareprobe: cam_periph_acquire failed"));
5595 softc->state = DA_STATE_PROBE_WP;
5596 xpt_schedule(periph, CAM_PRIORITY_DEV);
5600 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
5602 struct da_softc *softc;
5603 struct cam_periph *periph;
5604 int error, error_code, sense_key, asc, ascq;
5606 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
5607 if (ccb->csio.bio != NULL)
5608 biotrack(ccb->csio.bio, __func__);
5611 periph = xpt_path_periph(ccb->ccb_h.path);
5612 softc = (struct da_softc *)periph->softc;
5614 cam_periph_assert(periph, MA_OWNED);
5617 * Automatically detect devices that do not support
5618 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
5621 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
5622 error = cmd6workaround(ccb);
5623 } else if (scsi_extract_sense_ccb(ccb,
5624 &error_code, &sense_key, &asc, &ascq)) {
5625 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
5626 error = cmd6workaround(ccb);
5628 * If the target replied with CAPACITY DATA HAS CHANGED UA,
5629 * query the capacity and notify upper layers.
5631 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
5632 asc == 0x2A && ascq == 0x09) {
5633 xpt_print(periph->path, "Capacity data has changed\n");
5634 softc->flags &= ~DA_FLAG_PROBED;
5636 sense_flags |= SF_NO_PRINT;
5637 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
5638 asc == 0x28 && ascq == 0x00) {
5639 softc->flags &= ~DA_FLAG_PROBED;
5640 disk_media_changed(softc->disk, M_NOWAIT);
5641 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
5642 asc == 0x3F && ascq == 0x03) {
5643 xpt_print(periph->path, "INQUIRY data has changed\n");
5644 softc->flags &= ~DA_FLAG_PROBED;
5646 sense_flags |= SF_NO_PRINT;
5647 } else if (sense_key == SSD_KEY_NOT_READY &&
5648 asc == 0x3a && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
5649 softc->flags |= DA_FLAG_PACK_INVALID;
5650 disk_media_gone(softc->disk, M_NOWAIT);
5653 if (error == ERESTART)
5657 switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
5658 case CAM_CMD_TIMEOUT:
5661 case CAM_REQ_ABORTED:
5662 case CAM_REQ_CMP_ERR:
5663 case CAM_REQ_TERMIO:
5664 case CAM_UNREC_HBA_ERROR:
5665 case CAM_DATA_RUN_ERR:
5675 * Until we have a better way of doing pack validation,
5676 * don't treat UAs as errors.
5678 sense_flags |= SF_RETRY_UA;
5680 if (softc->quirks & DA_Q_RETRY_BUSY)
5681 sense_flags |= SF_RETRY_BUSY;
5682 return(cam_periph_error(ccb, cam_flags, sense_flags));
5686 damediapoll(void *arg)
5688 struct cam_periph *periph = arg;
5689 struct da_softc *softc = periph->softc;
5691 if (!cam_iosched_has_work_flags(softc->cam_iosched, DA_WORK_TUR) &&
5692 LIST_EMPTY(&softc->pending_ccbs)) {
5693 if (da_periph_acquire(periph, DA_REF_TUR) == 0) {
5694 cam_iosched_set_work_flags(softc->cam_iosched, DA_WORK_TUR);
5698 /* Queue us up again */
5699 if (da_poll_period != 0)
5700 callout_schedule(&softc->mediapoll_c, da_poll_period * hz);
5704 daprevent(struct cam_periph *periph, int action)
5706 struct da_softc *softc;
5710 cam_periph_assert(periph, MA_OWNED);
5711 softc = (struct da_softc *)periph->softc;
5713 if (((action == PR_ALLOW)
5714 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
5715 || ((action == PR_PREVENT)
5716 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
5720 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
5722 scsi_prevent(&ccb->csio,
5730 error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
5731 SF_RETRY_UA | SF_NO_PRINT, softc->disk->d_devstat);
5734 if (action == PR_ALLOW)
5735 softc->flags &= ~DA_FLAG_PACK_LOCKED;
5737 softc->flags |= DA_FLAG_PACK_LOCKED;
5740 xpt_release_ccb(ccb);
5744 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
5745 struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
5747 struct ccb_calc_geometry ccg;
5748 struct da_softc *softc;
5749 struct disk_params *dp;
5750 u_int lbppbe, lalba;
5753 softc = (struct da_softc *)periph->softc;
5755 dp = &softc->params;
5756 dp->secsize = block_len;
5757 dp->sectors = maxsector + 1;
5758 if (rcaplong != NULL) {
5759 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
5760 lalba = scsi_2btoul(rcaplong->lalba_lbp);
5761 lalba &= SRC16_LALBA_A;
5768 dp->stripesize = block_len << lbppbe;
5769 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
5771 } else if (softc->quirks & DA_Q_4K) {
5772 dp->stripesize = 4096;
5773 dp->stripeoffset = 0;
5774 } else if (softc->unmap_gran != 0) {
5775 dp->stripesize = block_len * softc->unmap_gran;
5776 dp->stripeoffset = (dp->stripesize - block_len *
5777 softc->unmap_gran_align) % dp->stripesize;
5780 dp->stripeoffset = 0;
5783 * Have the controller provide us with a geometry
5784 * for this disk. The only time the geometry
5785 * matters is when we boot and the controller
5786 * is the only one knowledgeable enough to come
5787 * up with something that will make this a bootable
5790 xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
5791 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
5792 ccg.block_size = dp->secsize;
5793 ccg.volume_size = dp->sectors;
5795 ccg.secs_per_track = 0;
5797 xpt_action((union ccb*)&ccg);
5798 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5800 * We don't know what went wrong here- but just pick
5801 * a geometry so we don't have nasty things like divide
5805 dp->secs_per_track = 255;
5806 dp->cylinders = dp->sectors / (255 * 255);
5807 if (dp->cylinders == 0) {
5811 dp->heads = ccg.heads;
5812 dp->secs_per_track = ccg.secs_per_track;
5813 dp->cylinders = ccg.cylinders;
5817 * If the user supplied a read capacity buffer, and if it is
5818 * different than the previous buffer, update the data in the EDT.
5819 * If it's the same, we don't bother. This avoids sending an
5820 * update every time someone opens this device.
5822 if ((rcaplong != NULL)
5823 && (bcmp(rcaplong, &softc->rcaplong,
5824 min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
5825 struct ccb_dev_advinfo cdai;
5827 xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
5828 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
5829 cdai.buftype = CDAI_TYPE_RCAPLONG;
5830 cdai.flags = CDAI_FLAG_STORE;
5831 cdai.bufsiz = rcap_len;
5832 cdai.buf = (uint8_t *)rcaplong;
5833 xpt_action((union ccb *)&cdai);
5834 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
5835 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
5836 if (cdai.ccb_h.status != CAM_REQ_CMP) {
5837 xpt_print(periph->path, "%s: failed to set read "
5838 "capacity advinfo\n", __func__);
5839 /* Use cam_error_print() to decode the status */
5840 cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
5843 bcopy(rcaplong, &softc->rcaplong,
5844 min(sizeof(softc->rcaplong), rcap_len));
5848 softc->disk->d_sectorsize = softc->params.secsize;
5849 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
5850 softc->disk->d_stripesize = softc->params.stripesize;
5851 softc->disk->d_stripeoffset = softc->params.stripeoffset;
5852 /* XXX: these are not actually "firmware" values, so they may be wrong */
5853 softc->disk->d_fwsectors = softc->params.secs_per_track;
5854 softc->disk->d_fwheads = softc->params.heads;
5855 softc->disk->d_devstat->block_size = softc->params.secsize;
5856 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
5858 error = disk_resize(softc->disk, M_NOWAIT);
5860 xpt_print(periph->path, "disk_resize(9) failed, error = %d\n", error);
5864 dasendorderedtag(void *arg)
5866 struct cam_periph *periph = arg;
5867 struct da_softc *softc = periph->softc;
5869 cam_periph_assert(periph, MA_OWNED);
5870 if (da_send_ordered) {
5871 if (!LIST_EMPTY(&softc->pending_ccbs)) {
5872 if ((softc->flags & DA_FLAG_WAS_OTAG) == 0)
5873 softc->flags |= DA_FLAG_NEED_OTAG;
5874 softc->flags &= ~DA_FLAG_WAS_OTAG;
5878 /* Queue us up again */
5879 callout_reset(&softc->sendordered_c,
5880 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
5881 dasendorderedtag, periph);
5885 * Step through all DA peripheral drivers, and if the device is still open,
5886 * sync the disk cache to physical media.
5889 dashutdown(void * arg, int howto)
5891 struct cam_periph *periph;
5892 struct da_softc *softc;
5896 CAM_PERIPH_FOREACH(periph, &dadriver) {
5897 softc = (struct da_softc *)periph->softc;
5898 if (SCHEDULER_STOPPED()) {
5899 /* If we paniced with the lock held, do not recurse. */
5900 if (!cam_periph_owned(periph) &&
5901 (softc->flags & DA_FLAG_OPEN)) {
5902 dadump(softc->disk, NULL, 0, 0, 0);
5906 cam_periph_lock(periph);
5909 * We only sync the cache if the drive is still open, and
5910 * if the drive is capable of it..
5912 if (((softc->flags & DA_FLAG_OPEN) == 0)
5913 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
5914 cam_periph_unlock(periph);
5918 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
5919 scsi_synchronize_cache(&ccb->csio,
5923 /*begin_lba*/0, /* whole disk */
5928 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
5929 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR,
5930 softc->disk->d_devstat);
5932 xpt_print(periph->path, "Synchronize cache failed\n");
5933 xpt_release_ccb(ccb);
5934 cam_periph_unlock(periph);
5938 #else /* !_KERNEL */
5941 * XXX These are only left out of the kernel build to silence warnings. If,
5942 * for some reason these functions are used in the kernel, the ifdefs should
5943 * be moved so they are included both in the kernel and userland.
5946 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
5947 void (*cbfcnp)(struct cam_periph *, union ccb *),
5948 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
5949 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
5952 struct scsi_format_unit *scsi_cmd;
5954 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
5955 scsi_cmd->opcode = FORMAT_UNIT;
5956 scsi_cmd->byte2 = byte2;
5957 scsi_ulto2b(ileave, scsi_cmd->interleave);
5962 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
5972 scsi_read_defects(struct ccb_scsiio *csio, uint32_t retries,
5973 void (*cbfcnp)(struct cam_periph *, union ccb *),
5974 uint8_t tag_action, uint8_t list_format,
5975 uint32_t addr_desc_index, uint8_t *data_ptr,
5976 uint32_t dxfer_len, int minimum_cmd_size,
5977 uint8_t sense_len, uint32_t timeout)
5982 * These conditions allow using the 10 byte command. Otherwise we
5983 * need to use the 12 byte command.
5985 if ((minimum_cmd_size <= 10)
5986 && (addr_desc_index == 0)
5987 && (dxfer_len <= SRDD10_MAX_LENGTH)) {
5988 struct scsi_read_defect_data_10 *cdb10;
5990 cdb10 = (struct scsi_read_defect_data_10 *)
5991 &csio->cdb_io.cdb_bytes;
5993 cdb_len = sizeof(*cdb10);
5994 bzero(cdb10, cdb_len);
5995 cdb10->opcode = READ_DEFECT_DATA_10;
5996 cdb10->format = list_format;
5997 scsi_ulto2b(dxfer_len, cdb10->alloc_length);
5999 struct scsi_read_defect_data_12 *cdb12;
6001 cdb12 = (struct scsi_read_defect_data_12 *)
6002 &csio->cdb_io.cdb_bytes;
6004 cdb_len = sizeof(*cdb12);
6005 bzero(cdb12, cdb_len);
6006 cdb12->opcode = READ_DEFECT_DATA_12;
6007 cdb12->format = list_format;
6008 scsi_ulto4b(dxfer_len, cdb12->alloc_length);
6009 scsi_ulto4b(addr_desc_index, cdb12->address_descriptor_index);
6015 /*flags*/ CAM_DIR_IN,
6025 scsi_sanitize(struct ccb_scsiio *csio, u_int32_t retries,
6026 void (*cbfcnp)(struct cam_periph *, union ccb *),
6027 u_int8_t tag_action, u_int8_t byte2, u_int16_t control,
6028 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
6031 struct scsi_sanitize *scsi_cmd;
6033 scsi_cmd = (struct scsi_sanitize *)&csio->cdb_io.cdb_bytes;
6034 scsi_cmd->opcode = SANITIZE;
6035 scsi_cmd->byte2 = byte2;
6036 scsi_cmd->control = control;
6037 scsi_ulto2b(dxfer_len, scsi_cmd->length);
6042 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
6051 #endif /* _KERNEL */
6054 scsi_zbc_out(struct ccb_scsiio *csio, uint32_t retries,
6055 void (*cbfcnp)(struct cam_periph *, union ccb *),
6056 uint8_t tag_action, uint8_t service_action, uint64_t zone_id,
6057 uint8_t zone_flags, uint8_t *data_ptr, uint32_t dxfer_len,
6058 uint8_t sense_len, uint32_t timeout)
6060 struct scsi_zbc_out *scsi_cmd;
6062 scsi_cmd = (struct scsi_zbc_out *)&csio->cdb_io.cdb_bytes;
6063 scsi_cmd->opcode = ZBC_OUT;
6064 scsi_cmd->service_action = service_action;
6065 scsi_u64to8b(zone_id, scsi_cmd->zone_id);
6066 scsi_cmd->zone_flags = zone_flags;
6071 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
6081 scsi_zbc_in(struct ccb_scsiio *csio, uint32_t retries,
6082 void (*cbfcnp)(struct cam_periph *, union ccb *),
6083 uint8_t tag_action, uint8_t service_action, uint64_t zone_start_lba,
6084 uint8_t zone_options, uint8_t *data_ptr, uint32_t dxfer_len,
6085 uint8_t sense_len, uint32_t timeout)
6087 struct scsi_zbc_in *scsi_cmd;
6089 scsi_cmd = (struct scsi_zbc_in *)&csio->cdb_io.cdb_bytes;
6090 scsi_cmd->opcode = ZBC_IN;
6091 scsi_cmd->service_action = service_action;
6092 scsi_ulto4b(dxfer_len, scsi_cmd->length);
6093 scsi_u64to8b(zone_start_lba, scsi_cmd->zone_start_lba);
6094 scsi_cmd->zone_options = zone_options;
6099 /*flags*/ (dxfer_len > 0) ? CAM_DIR_IN : CAM_DIR_NONE,
6110 scsi_ata_zac_mgmt_out(struct ccb_scsiio *csio, uint32_t retries,
6111 void (*cbfcnp)(struct cam_periph *, union ccb *),
6112 uint8_t tag_action, int use_ncq,
6113 uint8_t zm_action, uint64_t zone_id, uint8_t zone_flags,
6114 uint8_t *data_ptr, uint32_t dxfer_len,
6115 uint8_t *cdb_storage, size_t cdb_storage_len,
6116 uint8_t sense_len, uint32_t timeout)
6118 uint8_t command_out, protocol, ata_flags;
6119 uint16_t features_out;
6120 uint32_t sectors_out, auxiliary;
6126 command_out = ATA_ZAC_MANAGEMENT_OUT;
6127 features_out = (zm_action & 0xf) | (zone_flags << 8);
6128 ata_flags = AP_FLAG_BYT_BLOK_BLOCKS;
6129 if (dxfer_len == 0) {
6130 protocol = AP_PROTO_NON_DATA;
6131 ata_flags |= AP_FLAG_TLEN_NO_DATA;
6134 protocol = AP_PROTO_DMA;
6135 ata_flags |= AP_FLAG_TLEN_SECT_CNT |
6136 AP_FLAG_TDIR_TO_DEV;
6137 sectors_out = ((dxfer_len >> 9) & 0xffff);
6141 ata_flags = AP_FLAG_BYT_BLOK_BLOCKS;
6142 if (dxfer_len == 0) {
6143 command_out = ATA_NCQ_NON_DATA;
6144 features_out = ATA_NCQ_ZAC_MGMT_OUT;
6146 * We're assuming the SCSI to ATA translation layer
6147 * will set the NCQ tag number in the tag field.
6148 * That isn't clear from the SAT-4 spec (as of rev 05).
6151 ata_flags |= AP_FLAG_TLEN_NO_DATA;
6153 command_out = ATA_SEND_FPDMA_QUEUED;
6155 * Note that we're defaulting to normal priority,
6156 * and assuming that the SCSI to ATA translation
6157 * layer will insert the NCQ tag number in the tag
6158 * field. That isn't clear in the SAT-4 spec (as
6161 sectors_out = ATA_SFPDMA_ZAC_MGMT_OUT << 8;
6163 ata_flags |= AP_FLAG_TLEN_FEAT |
6164 AP_FLAG_TDIR_TO_DEV;
6167 * For SEND FPDMA QUEUED, the transfer length is
6168 * encoded in the FEATURE register, and 0 means
6169 * that 65536 512 byte blocks are to be tranferred.
6170 * In practice, it seems unlikely that we'll see
6171 * a transfer that large, and it may confuse the
6172 * the SAT layer, because generally that means that
6173 * 0 bytes should be transferred.
6175 if (dxfer_len == (65536 * 512)) {
6177 } else if (dxfer_len <= (65535 * 512)) {
6178 features_out = ((dxfer_len >> 9) & 0xffff);
6180 /* The transfer is too big. */
6187 auxiliary = (zm_action & 0xf) | (zone_flags << 8);
6188 protocol = AP_PROTO_FPDMA;
6191 protocol |= AP_EXTEND;
6193 retval = scsi_ata_pass(csio,
6196 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
6198 /*protocol*/ protocol,
6199 /*ata_flags*/ ata_flags,
6200 /*features*/ features_out,
6201 /*sector_count*/ sectors_out,
6203 /*command*/ command_out,
6206 /*auxiliary*/ auxiliary,
6208 /*data_ptr*/ data_ptr,
6209 /*dxfer_len*/ dxfer_len,
6210 /*cdb_storage*/ cdb_storage,
6211 /*cdb_storage_len*/ cdb_storage_len,
6212 /*minimum_cmd_size*/ 0,
6213 /*sense_len*/ SSD_FULL_SIZE,
6214 /*timeout*/ timeout);
6222 scsi_ata_zac_mgmt_in(struct ccb_scsiio *csio, uint32_t retries,
6223 void (*cbfcnp)(struct cam_periph *, union ccb *),
6224 uint8_t tag_action, int use_ncq,
6225 uint8_t zm_action, uint64_t zone_id, uint8_t zone_flags,
6226 uint8_t *data_ptr, uint32_t dxfer_len,
6227 uint8_t *cdb_storage, size_t cdb_storage_len,
6228 uint8_t sense_len, uint32_t timeout)
6230 uint8_t command_out, protocol;
6231 uint16_t features_out, sectors_out;
6237 ata_flags = AP_FLAG_TDIR_FROM_DEV | AP_FLAG_BYT_BLOK_BLOCKS;
6240 command_out = ATA_ZAC_MANAGEMENT_IN;
6241 /* XXX KDM put a macro here */
6242 features_out = (zm_action & 0xf) | (zone_flags << 8);
6243 sectors_out = dxfer_len >> 9; /* XXX KDM macro */
6244 protocol = AP_PROTO_DMA;
6245 ata_flags |= AP_FLAG_TLEN_SECT_CNT;
6248 ata_flags |= AP_FLAG_TLEN_FEAT;
6250 command_out = ATA_RECV_FPDMA_QUEUED;
6251 sectors_out = ATA_RFPDMA_ZAC_MGMT_IN << 8;
6254 * For RECEIVE FPDMA QUEUED, the transfer length is
6255 * encoded in the FEATURE register, and 0 means
6256 * that 65536 512 byte blocks are to be tranferred.
6257 * In practice, it seems unlikely that we'll see
6258 * a transfer that large, and it may confuse the
6259 * the SAT layer, because generally that means that
6260 * 0 bytes should be transferred.
6262 if (dxfer_len == (65536 * 512)) {
6264 } else if (dxfer_len <= (65535 * 512)) {
6265 features_out = ((dxfer_len >> 9) & 0xffff);
6267 /* The transfer is too big. */
6271 auxiliary = (zm_action & 0xf) | (zone_flags << 8),
6272 protocol = AP_PROTO_FPDMA;
6275 protocol |= AP_EXTEND;
6277 retval = scsi_ata_pass(csio,
6280 /*flags*/ CAM_DIR_IN,
6282 /*protocol*/ protocol,
6283 /*ata_flags*/ ata_flags,
6284 /*features*/ features_out,
6285 /*sector_count*/ sectors_out,
6287 /*command*/ command_out,
6290 /*auxiliary*/ auxiliary,
6292 /*data_ptr*/ data_ptr,
6293 /*dxfer_len*/ (dxfer_len >> 9) * 512, /* XXX KDM */
6294 /*cdb_storage*/ cdb_storage,
6295 /*cdb_storage_len*/ cdb_storage_len,
6296 /*minimum_cmd_size*/ 0,
6297 /*sense_len*/ SSD_FULL_SIZE,
6298 /*timeout*/ timeout);