2 * Implementation of SCSI Direct Access Peripheral driver for CAM.
4 * Copyright (c) 1997 Justin T. Gibbs.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
39 #include <sys/taskqueue.h>
41 #include <sys/mutex.h>
43 #include <sys/devicestat.h>
44 #include <sys/eventhandler.h>
45 #include <sys/malloc.h>
47 #include <sys/endian.h>
49 #include <geom/geom.h>
50 #include <geom/geom_disk.h>
59 #include <cam/cam_ccb.h>
60 #include <cam/cam_periph.h>
61 #include <cam/cam_xpt_periph.h>
62 #include <cam/cam_sim.h>
64 #include <cam/scsi/scsi_message.h>
67 #include <cam/scsi/scsi_da.h>
75 DA_STATE_PROBE_BLK_LIMITS,
82 DA_FLAG_PACK_INVALID = 0x001,
83 DA_FLAG_NEW_PACK = 0x002,
84 DA_FLAG_PACK_LOCKED = 0x004,
85 DA_FLAG_PACK_REMOVABLE = 0x008,
86 DA_FLAG_NEED_OTAG = 0x020,
87 DA_FLAG_WAS_OTAG = 0x040,
88 DA_FLAG_RETRY_UA = 0x080,
90 DA_FLAG_SCTX_INIT = 0x200,
91 DA_FLAG_CAN_RC16 = 0x400,
92 DA_FLAG_PROBED = 0x800,
93 DA_FLAG_DIRTY = 0x1000
98 DA_Q_NO_SYNC_CACHE = 0x01,
99 DA_Q_NO_6_BYTE = 0x02,
100 DA_Q_NO_PREVENT = 0x04,
106 #define DA_Q_BIT_STRING \
108 "\001NO_SYNC_CACHE" \
115 DA_CCB_PROBE_RC = 0x01,
116 DA_CCB_PROBE_RC16 = 0x02,
117 DA_CCB_PROBE_LBP = 0x03,
118 DA_CCB_PROBE_BLK_LIMITS = 0x04,
119 DA_CCB_PROBE_BDC = 0x05,
120 DA_CCB_PROBE_ATA = 0x06,
121 DA_CCB_BUFFER_IO = 0x07,
122 DA_CCB_WAITING = 0x08,
124 DA_CCB_DELETE = 0x0B,
126 DA_CCB_TYPE_MASK = 0x0F,
127 DA_CCB_RETRY_UA = 0x10
131 * Order here is important for method choice
133 * We prefer ATA_TRIM as tests run against a Sandforce 2281 SSD attached to
134 * LSI 2008 (mps) controller (FW: v12, Drv: v14) resulted 20% quicker deletes
135 * using ATA_TRIM than the corresponding UNMAP results for a real world mysql
136 * import taking 5mins.
147 DA_DELETE_MIN = DA_DELETE_ATA_TRIM,
148 DA_DELETE_MAX = DA_DELETE_ZERO
151 typedef void da_delete_func_t (struct cam_periph *periph, union ccb *ccb,
153 static da_delete_func_t da_delete_trim;
154 static da_delete_func_t da_delete_unmap;
155 static da_delete_func_t da_delete_ws;
157 static const void * da_delete_functions[] = {
167 static const char *da_delete_method_names[] =
168 { "NONE", "DISABLE", "ATA_TRIM", "UNMAP", "WS16", "WS10", "ZERO" };
169 static const char *da_delete_method_desc[] =
170 { "NONE", "DISABLED", "ATA TRIM", "UNMAP", "WRITE SAME(16) with UNMAP",
171 "WRITE SAME(10) with UNMAP", "ZERO" };
173 /* Offsets into our private area for storing information */
174 #define ccb_state ppriv_field0
175 #define ccb_bp ppriv_ptr1
180 u_int8_t secs_per_track;
181 u_int32_t secsize; /* Number of bytes/sector */
182 u_int64_t sectors; /* total number sectors */
187 #define UNMAP_RANGE_MAX 0xffffffff
188 #define UNMAP_HEAD_SIZE 8
189 #define UNMAP_RANGE_SIZE 16
190 #define UNMAP_MAX_RANGES 2048 /* Protocol Max is 4095 */
191 #define UNMAP_BUF_SIZE ((UNMAP_MAX_RANGES * UNMAP_RANGE_SIZE) + \
194 #define WS10_MAX_BLKS 0xffff
195 #define WS16_MAX_BLKS 0xffffffff
196 #define ATA_TRIM_MAX_RANGES ((UNMAP_BUF_SIZE / \
197 (ATA_DSM_RANGE_SIZE * ATA_DSM_BLK_SIZE)) * ATA_DSM_BLK_SIZE)
200 struct bio_queue_head bio_queue;
201 struct bio_queue_head delete_queue;
202 struct bio_queue_head delete_run_queue;
203 LIST_HEAD(, ccb_hdr) pending_ccbs;
204 int tur; /* TEST UNIT READY should be sent */
205 int refcount; /* Active xpt_action() calls */
210 int minimum_cmd_size;
214 int delete_available; /* Delete methods possibly available */
215 uint32_t unmap_max_ranges;
216 uint32_t unmap_max_lba; /* Max LBAs in UNMAP req */
217 uint64_t ws_max_blks;
218 da_delete_methods delete_method;
219 da_delete_func_t *delete_func;
220 struct disk_params params;
223 struct task sysctl_task;
224 struct sysctl_ctx_list sysctl_ctx;
225 struct sysctl_oid *sysctl_tree;
226 struct callout sendordered_c;
228 uint8_t unmap_buf[UNMAP_BUF_SIZE];
229 struct scsi_read_capacity_data_long rcaplong;
230 struct callout mediapoll_c;
233 #define dadeleteflag(softc, delete_method, enable) \
235 softc->delete_available |= (1 << delete_method); \
237 softc->delete_available &= ~(1 << delete_method); \
240 struct da_quirk_entry {
241 struct scsi_inquiry_pattern inq_pat;
245 static const char quantum[] = "QUANTUM";
246 static const char microp[] = "MICROP";
248 static struct da_quirk_entry da_quirk_table[] =
250 /* SPI, FC devices */
253 * Fujitsu M2513A MO drives.
254 * Tested devices: M2513A2 firmware versions 1200 & 1300.
255 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
256 * Reported by: W.Scholten <whs@xs4all.nl>
258 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
259 /*quirks*/ DA_Q_NO_SYNC_CACHE
263 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
264 /*quirks*/ DA_Q_NO_SYNC_CACHE
268 * This particular Fujitsu drive doesn't like the
269 * synchronize cache command.
270 * Reported by: Tom Jackson <toj@gorilla.net>
272 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
273 /*quirks*/ DA_Q_NO_SYNC_CACHE
277 * This drive doesn't like the synchronize cache command
278 * either. Reported by: Matthew Jacob <mjacob@feral.com>
279 * in NetBSD PR kern/6027, August 24, 1998.
281 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
282 /*quirks*/ DA_Q_NO_SYNC_CACHE
286 * This drive doesn't like the synchronize cache command
287 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
290 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
291 /*quirks*/ DA_Q_NO_SYNC_CACHE
295 * Doesn't like the synchronize cache command.
296 * Reported by: Blaz Zupan <blaz@gold.amis.net>
298 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
299 /*quirks*/ DA_Q_NO_SYNC_CACHE
303 * Doesn't like the synchronize cache command.
304 * Reported by: Blaz Zupan <blaz@gold.amis.net>
306 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
307 /*quirks*/ DA_Q_NO_SYNC_CACHE
311 * Doesn't like the synchronize cache command.
313 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
314 /*quirks*/ DA_Q_NO_SYNC_CACHE
318 * Doesn't like the synchronize cache command.
319 * Reported by: walter@pelissero.de
321 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
322 /*quirks*/ DA_Q_NO_SYNC_CACHE
326 * Doesn't work correctly with 6 byte reads/writes.
327 * Returns illegal request, and points to byte 9 of the
329 * Reported by: Adam McDougall <bsdx@spawnet.com>
331 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
332 /*quirks*/ DA_Q_NO_6_BYTE
336 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
337 /*quirks*/ DA_Q_NO_6_BYTE
341 * Doesn't like the synchronize cache command.
342 * Reported by: walter@pelissero.de
344 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
345 /*quirks*/ DA_Q_NO_SYNC_CACHE
349 * The CISS RAID controllers do not support SYNC_CACHE
351 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
352 /*quirks*/ DA_Q_NO_SYNC_CACHE
356 * The STEC 842 sometimes hang on UNMAP.
358 {T_DIRECT, SIP_MEDIA_FIXED, "STEC", "S842E800M2", "*"},
359 /*quirks*/ DA_Q_NO_UNMAP
361 /* USB mass storage devices supported by umass(4) */
364 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
367 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
368 /*quirks*/ DA_Q_NO_SYNC_CACHE
372 * Power Quotient Int. (PQI) USB flash key
375 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
376 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
380 * Creative Nomad MUVO mp3 player (USB)
383 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
384 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
388 * Jungsoft NEXDISK USB flash key
391 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
392 /*quirks*/ DA_Q_NO_SYNC_CACHE
396 * FreeDik USB Mini Data Drive
399 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
400 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
404 * Sigmatel USB Flash MP3 Player
407 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
408 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
412 * Neuros USB Digital Audio Computer
415 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
416 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
420 * SEAGRAND NP-900 MP3 Player
423 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
424 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
428 * iRiver iFP MP3 player (with UMS Firmware)
429 * PR: kern/54881, i386/63941, kern/66124
431 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
432 /*quirks*/ DA_Q_NO_SYNC_CACHE
436 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
439 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
440 /*quirks*/ DA_Q_NO_SYNC_CACHE
444 * ZICPlay USB MP3 Player with FM
447 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
448 /*quirks*/ DA_Q_NO_SYNC_CACHE
452 * TEAC USB floppy mechanisms
454 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
455 /*quirks*/ DA_Q_NO_SYNC_CACHE
459 * Kingston DataTraveler II+ USB Pen-Drive.
460 * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
462 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
463 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
471 {T_DIRECT, SIP_MEDIA_REMOVABLE, " ", "USB DISK Pro", "PMAP"},
472 /*quirks*/ DA_Q_NO_SYNC_CACHE
476 * Motorola E398 Mobile Phone (TransFlash memory card).
477 * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
480 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
481 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
485 * Qware BeatZkey! Pro
488 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
489 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
493 * Time DPA20B 1GB MP3 Player
496 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
497 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
501 * Samsung USB key 128Mb
504 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
505 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
509 * Kingston DataTraveler 2.0 USB Flash memory.
512 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
513 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
517 * Creative MUVO Slim mp3 player (USB)
520 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
521 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
525 * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
528 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
529 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
533 * SanDisk Micro Cruzer 128MB
536 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
537 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
541 * TOSHIBA TransMemory USB sticks
544 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
545 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
550 * PR: usb/75578, usb/72344, usb/65436
552 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
553 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
557 * Genesys 6-in-1 Card Reader
560 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
561 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
565 * Rekam Digital CAMERA
568 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
569 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
573 * iRiver H10 MP3 player
576 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
577 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
581 * iRiver U10 MP3 player
584 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
585 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
592 {T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
593 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
597 * EasyMP3 EM732X USB 2.0 Flash MP3 Player
600 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
601 "1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
608 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
609 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
613 * Philips USB Key Audio KEY013
616 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
617 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
624 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
625 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
632 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
633 /*quirks*/ DA_Q_NO_SYNC_CACHE
637 * I/O Magic USB flash - Giga Bank
640 {T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
641 /*quirks*/ DA_Q_NO_SYNC_CACHE
645 * JoyFly 128mb USB Flash Drive
648 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
649 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
656 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
657 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
661 * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A
664 {T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*",
665 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
669 * Samsung YP-U3 mp3-player
672 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3",
673 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
676 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
677 "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
681 * Sony Cyber-Shot DSC cameras
684 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
685 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
688 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler G3",
689 "1.00"}, /*quirks*/ DA_Q_NO_PREVENT
692 /* At least several Transcent USB sticks lie on RC16. */
693 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JetFlash", "Transcend*",
694 "*"}, /*quirks*/ DA_Q_NO_RC16
696 /* ATA/SATA devices over SAS/USB/... */
698 /* Hitachi Advanced Format (4k) drives */
699 { T_DIRECT, SIP_MEDIA_FIXED, "Hitachi", "H??????????E3*", "*" },
703 /* Samsung Advanced Format (4k) drives */
704 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD155UI*", "*" },
708 /* Samsung Advanced Format (4k) drives */
709 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD155UI*", "*" },
713 /* Samsung Advanced Format (4k) drives */
714 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD204UI*", "*" },
718 /* Samsung Advanced Format (4k) drives */
719 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD204UI*", "*" },
723 /* Seagate Barracuda Green Advanced Format (4k) drives */
724 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DL*", "*" },
728 /* Seagate Barracuda Green Advanced Format (4k) drives */
729 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DL", "*", "*" },
733 /* Seagate Barracuda Green Advanced Format (4k) drives */
734 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???DM*", "*" },
738 /* Seagate Barracuda Green Advanced Format (4k) drives */
739 { T_DIRECT, SIP_MEDIA_FIXED, "ST???DM*", "*", "*" },
743 /* Seagate Barracuda Green Advanced Format (4k) drives */
744 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DM*", "*" },
748 /* Seagate Barracuda Green Advanced Format (4k) drives */
749 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DM", "*", "*" },
753 /* Seagate Momentus Advanced Format (4k) drives */
754 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500423AS*", "*" },
758 /* Seagate Momentus Advanced Format (4k) drives */
759 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "3AS*", "*" },
763 /* Seagate Momentus Advanced Format (4k) drives */
764 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500424AS*", "*" },
768 /* Seagate Momentus Advanced Format (4k) drives */
769 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "4AS*", "*" },
773 /* Seagate Momentus Advanced Format (4k) drives */
774 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640423AS*", "*" },
778 /* Seagate Momentus Advanced Format (4k) drives */
779 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "3AS*", "*" },
783 /* Seagate Momentus Advanced Format (4k) drives */
784 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640424AS*", "*" },
788 /* Seagate Momentus Advanced Format (4k) drives */
789 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "4AS*", "*" },
793 /* Seagate Momentus Advanced Format (4k) drives */
794 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750420AS*", "*" },
798 /* Seagate Momentus Advanced Format (4k) drives */
799 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "0AS*", "*" },
803 /* Seagate Momentus Advanced Format (4k) drives */
804 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750422AS*", "*" },
808 /* Seagate Momentus Advanced Format (4k) drives */
809 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "2AS*", "*" },
813 /* Seagate Momentus Advanced Format (4k) drives */
814 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750423AS*", "*" },
818 /* Seagate Momentus Advanced Format (4k) drives */
819 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "3AS*", "*" },
823 /* Seagate Momentus Thin Advanced Format (4k) drives */
824 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???LT*", "*" },
828 /* Seagate Momentus Thin Advanced Format (4k) drives */
829 { T_DIRECT, SIP_MEDIA_FIXED, "ST???LT*", "*", "*" },
833 /* WDC Caviar Green Advanced Format (4k) drives */
834 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RS*", "*" },
838 /* WDC Caviar Green Advanced Format (4k) drives */
839 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RS*", "*" },
843 /* WDC Caviar Green Advanced Format (4k) drives */
844 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RX*", "*" },
848 /* WDC Caviar Green Advanced Format (4k) drives */
849 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RX*", "*" },
853 /* WDC Caviar Green Advanced Format (4k) drives */
854 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RS*", "*" },
858 /* WDC Caviar Green Advanced Format (4k) drives */
859 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RS*", "*" },
863 /* WDC Caviar Green Advanced Format (4k) drives */
864 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RX*", "*" },
868 /* WDC Caviar Green Advanced Format (4k) drives */
869 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RX*", "*" },
873 /* WDC Scorpio Black Advanced Format (4k) drives */
874 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PKT*", "*" },
878 /* WDC Scorpio Black Advanced Format (4k) drives */
879 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PKT*", "*" },
883 /* WDC Scorpio Black Advanced Format (4k) drives */
884 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PKT*", "*" },
888 /* WDC Scorpio Black Advanced Format (4k) drives */
889 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PKT*", "*" },
893 /* WDC Scorpio Blue Advanced Format (4k) drives */
894 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PVT*", "*" },
898 /* WDC Scorpio Blue Advanced Format (4k) drives */
899 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PVT*", "*" },
903 /* WDC Scorpio Blue Advanced Format (4k) drives */
904 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PVT*", "*" },
908 /* WDC Scorpio Blue Advanced Format (4k) drives */
909 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PVT*", "*" },
914 * Olympus FE-210 camera
916 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "FE210*",
917 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
923 {T_DIRECT, SIP_MEDIA_REMOVABLE, "LG", "UP3S",
924 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
928 * Laser MP3-2GA13 MP3 player
930 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "(HS) Flash Disk",
931 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
935 * LaCie external 250GB Hard drive des by Porsche
936 * Submitted by: Ben Stuyts <ben@altesco.nl>
939 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HM250JI", "*"},
940 /*quirks*/ DA_Q_NO_SYNC_CACHE
945 * Corsair Force 2 SSDs
946 * 4k optimised & trim only works in 4k requests + 4k aligned
948 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair CSSD-F*", "*" },
953 * Corsair Force 3 SSDs
954 * 4k optimised & trim only works in 4k requests + 4k aligned
956 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force 3*", "*" },
961 * Corsair Force GT SSDs
962 * 4k optimised & trim only works in 4k requests + 4k aligned
964 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force GT*", "*" },
970 * 4k optimised & trim only works in 4k requests + 4k aligned
972 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "M4-CT???M4SSD2*", "*" },
977 * Crucial RealSSD C300 SSDs
980 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "C300-CTFDDAC???MAG*",
981 "*" }, /*quirks*/DA_Q_4K
985 * Intel 320 Series SSDs
986 * 4k optimised & trim only works in 4k requests + 4k aligned
988 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2CW*", "*" },
993 * Intel 330 Series SSDs
994 * 4k optimised & trim only works in 4k requests + 4k aligned
996 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2CT*", "*" },
1001 * Intel 510 Series SSDs
1002 * 4k optimised & trim only works in 4k requests + 4k aligned
1004 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2MH*", "*" },
1009 * Intel 520 Series SSDs
1010 * 4k optimised & trim only works in 4k requests + 4k aligned
1012 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2BW*", "*" },
1017 * Intel X25-M Series SSDs
1018 * 4k optimised & trim only works in 4k requests + 4k aligned
1020 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2M*", "*" },
1025 * Kingston E100 Series SSDs
1026 * 4k optimised & trim only works in 4k requests + 4k aligned
1028 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SE100S3*", "*" },
1033 * Kingston HyperX 3k SSDs
1034 * 4k optimised & trim only works in 4k requests + 4k aligned
1036 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SH103S3*", "*" },
1041 * Marvell SSDs (entry taken from OpenSolaris)
1042 * 4k optimised & trim only works in 4k requests + 4k aligned
1044 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "MARVELL SD88SA02*", "*" },
1049 * OCZ Agility 2 SSDs
1050 * 4k optimised & trim only works in 4k requests + 4k aligned
1052 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY2*", "*" },
1057 * OCZ Agility 3 SSDs
1058 * 4k optimised & trim only works in 4k requests + 4k aligned
1060 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-AGILITY3*", "*" },
1065 * OCZ Deneva R Series SSDs
1066 * 4k optimised & trim only works in 4k requests + 4k aligned
1068 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "DENRSTE251M45*", "*" },
1073 * OCZ Vertex 2 SSDs (inc pro series)
1074 * 4k optimised & trim only works in 4k requests + 4k aligned
1076 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ?VERTEX2*", "*" },
1082 * 4k optimised & trim only works in 4k requests + 4k aligned
1084 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX3*", "*" },
1090 * 4k optimised & trim only works in 4k requests + 4k aligned
1092 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX4*", "*" },
1097 * Samsung 830 Series SSDs
1098 * 4k optimised & trim only works in 4k requests + 4k aligned
1100 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG SSD 830 Series*", "*" },
1105 * SuperTalent TeraDrive CT SSDs
1106 * 4k optimised & trim only works in 4k requests + 4k aligned
1108 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "FTM??CT25H*", "*" },
1113 * XceedIOPS SATA SSDs
1116 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SG9XCS2D*", "*" },
1121 static disk_strategy_t dastrategy;
1122 static dumper_t dadump;
1123 static periph_init_t dainit;
1124 static void daasync(void *callback_arg, u_int32_t code,
1125 struct cam_path *path, void *arg);
1126 static void dasysctlinit(void *context, int pending);
1127 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
1128 static int dadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
1129 static int dadeletemaxsysctl(SYSCTL_HANDLER_ARGS);
1130 static void dadeletemethodset(struct da_softc *softc,
1131 da_delete_methods delete_method);
1132 static off_t dadeletemaxsize(struct da_softc *softc,
1133 da_delete_methods delete_method);
1134 static void dadeletemethodchoose(struct da_softc *softc,
1135 da_delete_methods default_method);
1136 static void daprobedone(struct cam_periph *periph, union ccb *ccb);
1138 static periph_ctor_t daregister;
1139 static periph_dtor_t dacleanup;
1140 static periph_start_t dastart;
1141 static periph_oninv_t daoninvalidate;
1142 static void dadone(struct cam_periph *periph,
1143 union ccb *done_ccb);
1144 static int daerror(union ccb *ccb, u_int32_t cam_flags,
1145 u_int32_t sense_flags);
1146 static void daprevent(struct cam_periph *periph, int action);
1147 static void dareprobe(struct cam_periph *periph);
1148 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
1150 struct scsi_read_capacity_data_long *rcaplong,
1152 static timeout_t dasendorderedtag;
1153 static void dashutdown(void *arg, int howto);
1154 static timeout_t damediapoll;
1156 #ifndef DA_DEFAULT_POLL_PERIOD
1157 #define DA_DEFAULT_POLL_PERIOD 3
1160 #ifndef DA_DEFAULT_TIMEOUT
1161 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
1164 #ifndef DA_DEFAULT_RETRY
1165 #define DA_DEFAULT_RETRY 4
1168 #ifndef DA_DEFAULT_SEND_ORDERED
1169 #define DA_DEFAULT_SEND_ORDERED 1
1172 #define DA_SIO (softc->sort_io_queue >= 0 ? \
1173 softc->sort_io_queue : cam_sort_io_queues)
1175 static int da_poll_period = DA_DEFAULT_POLL_PERIOD;
1176 static int da_retry_count = DA_DEFAULT_RETRY;
1177 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
1178 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
1180 static SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
1181 "CAM Direct Access Disk driver");
1182 SYSCTL_INT(_kern_cam_da, OID_AUTO, poll_period, CTLFLAG_RW,
1183 &da_poll_period, 0, "Media polling period in seconds");
1184 TUNABLE_INT("kern.cam.da.poll_period", &da_poll_period);
1185 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
1186 &da_retry_count, 0, "Normal I/O retry count");
1187 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
1188 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
1189 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
1190 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
1191 SYSCTL_INT(_kern_cam_da, OID_AUTO, send_ordered, CTLFLAG_RW,
1192 &da_send_ordered, 0, "Send Ordered Tags");
1193 TUNABLE_INT("kern.cam.da.send_ordered", &da_send_ordered);
1196 * DA_ORDEREDTAG_INTERVAL determines how often, relative
1197 * to the default timeout, we check to see whether an ordered
1198 * tagged transaction is appropriate to prevent simple tag
1199 * starvation. Since we'd like to ensure that there is at least
1200 * 1/2 of the timeout length left for a starved transaction to
1201 * complete after we've sent an ordered tag, we must poll at least
1202 * four times in every timeout period. This takes care of the worst
1203 * case where a starved transaction starts during an interval that
1204 * meets the requirement "don't send an ordered tag" test so it takes
1205 * us two intervals to determine that a tag must be sent.
1207 #ifndef DA_ORDEREDTAG_INTERVAL
1208 #define DA_ORDEREDTAG_INTERVAL 4
1211 static struct periph_driver dadriver =
1214 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
1217 PERIPHDRIVER_DECLARE(da, dadriver);
1219 static MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
1222 daopen(struct disk *dp)
1224 struct cam_periph *periph;
1225 struct da_softc *softc;
1228 periph = (struct cam_periph *)dp->d_drv1;
1229 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1233 cam_periph_lock(periph);
1234 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
1235 cam_periph_unlock(periph);
1236 cam_periph_release(periph);
1240 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1243 softc = (struct da_softc *)periph->softc;
1246 /* Wait for the disk size update. */
1247 error = cam_periph_sleep(periph, &softc->disk->d_mediasize, PRIBIO,
1250 xpt_print(periph->path, "unable to retrieve capacity data\n");
1252 if (periph->flags & CAM_PERIPH_INVALID)
1255 if (error == 0 && (softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1256 (softc->quirks & DA_Q_NO_PREVENT) == 0)
1257 daprevent(periph, PR_PREVENT);
1260 softc->flags &= ~DA_FLAG_PACK_INVALID;
1261 softc->flags |= DA_FLAG_OPEN;
1264 cam_periph_unhold(periph);
1265 cam_periph_unlock(periph);
1268 cam_periph_release(periph);
1274 daclose(struct disk *dp)
1276 struct cam_periph *periph;
1277 struct da_softc *softc;
1280 periph = (struct cam_periph *)dp->d_drv1;
1281 cam_periph_lock(periph);
1282 if (cam_periph_hold(periph, PRIBIO) != 0) {
1283 cam_periph_unlock(periph);
1284 cam_periph_release(periph);
1288 softc = (struct da_softc *)periph->softc;
1290 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1293 if ((softc->flags & DA_FLAG_DIRTY) != 0 &&
1294 (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0 &&
1295 (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
1298 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1300 scsi_synchronize_cache(&ccb->csio,
1304 /*begin_lba*/0,/* Cover the whole disk */
1309 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
1310 /*sense_flags*/SF_RETRY_UA | SF_QUIET_IR,
1311 softc->disk->d_devstat);
1313 softc->flags &= ~DA_FLAG_DIRTY;
1314 xpt_release_ccb(ccb);
1318 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
1319 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
1320 daprevent(periph, PR_ALLOW);
1322 * If we've got removeable media, mark the blocksize as
1323 * unavailable, since it could change when new media is
1326 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1329 softc->flags &= ~DA_FLAG_OPEN;
1330 cam_periph_unhold(periph);
1331 cam_periph_unlock(periph);
1332 cam_periph_release(periph);
1337 daschedule(struct cam_periph *periph)
1339 struct da_softc *softc = (struct da_softc *)periph->softc;
1342 if (softc->state != DA_STATE_NORMAL)
1345 /* Check if cam_periph_getccb() was called. */
1346 prio = periph->immediate_priority;
1348 /* Check if we have more work to do. */
1349 if (bioq_first(&softc->bio_queue) ||
1350 (!softc->delete_running && bioq_first(&softc->delete_queue)) ||
1352 prio = CAM_PRIORITY_NORMAL;
1355 /* Schedule CCB if any of above is true. */
1356 if (prio != CAM_PRIORITY_NONE)
1357 xpt_schedule(periph, prio);
1361 * Actually translate the requested transfer into one the physical driver
1362 * can understand. The transfer is described by a buf and will include
1363 * only one physical transfer.
1366 dastrategy(struct bio *bp)
1368 struct cam_periph *periph;
1369 struct da_softc *softc;
1371 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1372 softc = (struct da_softc *)periph->softc;
1374 cam_periph_lock(periph);
1377 * If the device has been made invalid, error out
1379 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1380 cam_periph_unlock(periph);
1381 biofinish(bp, NULL, ENXIO);
1385 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
1388 * Place it in the queue of disk activities for this disk
1390 if (bp->bio_cmd == BIO_DELETE) {
1391 if (bp->bio_bcount == 0)
1394 bioq_disksort(&softc->delete_queue, bp);
1396 bioq_insert_tail(&softc->delete_queue, bp);
1397 } else if (DA_SIO) {
1398 bioq_disksort(&softc->bio_queue, bp);
1400 bioq_insert_tail(&softc->bio_queue, bp);
1404 * Schedule ourselves for performing the work.
1407 cam_periph_unlock(periph);
1413 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1415 struct cam_periph *periph;
1416 struct da_softc *softc;
1418 struct ccb_scsiio csio;
1423 periph = dp->d_drv1;
1424 softc = (struct da_softc *)periph->softc;
1425 cam_periph_lock(periph);
1426 secsize = softc->params.secsize;
1428 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1429 cam_periph_unlock(periph);
1434 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1435 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1436 scsi_read_write(&csio,
1440 /*read*/SCSI_RW_WRITE,
1442 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1445 /*data_ptr*/(u_int8_t *) virtual,
1446 /*dxfer_len*/length,
1447 /*sense_len*/SSD_FULL_SIZE,
1448 da_default_timeout * 1000);
1449 xpt_polled_action((union ccb *)&csio);
1451 error = cam_periph_error((union ccb *)&csio,
1452 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1453 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1454 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1455 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1457 printf("Aborting dump due to I/O error.\n");
1458 cam_periph_unlock(periph);
1463 * Sync the disk cache contents to the physical media.
1465 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1467 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1468 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1469 scsi_synchronize_cache(&csio,
1473 /*begin_lba*/0,/* Cover the whole disk */
1477 xpt_polled_action((union ccb *)&csio);
1479 error = cam_periph_error((union ccb *)&csio,
1480 0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL);
1481 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1482 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1483 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1485 xpt_print(periph->path, "Synchronize cache failed\n");
1487 cam_periph_unlock(periph);
1492 dagetattr(struct bio *bp)
1495 struct cam_periph *periph;
1497 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1498 cam_periph_lock(periph);
1499 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1501 cam_periph_unlock(periph);
1503 bp->bio_completed = bp->bio_length;
1513 * Install a global async callback. This callback will
1514 * receive async callbacks like "new device found".
1516 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1518 if (status != CAM_REQ_CMP) {
1519 printf("da: Failed to attach master async callback "
1520 "due to status 0x%x!\n", status);
1521 } else if (da_send_ordered) {
1523 /* Register our shutdown event handler */
1524 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1525 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1526 printf("dainit: shutdown event registration failed!\n");
1531 * Callback from GEOM, called when it has finished cleaning up its
1535 dadiskgonecb(struct disk *dp)
1537 struct cam_periph *periph;
1539 periph = (struct cam_periph *)dp->d_drv1;
1540 cam_periph_release(periph);
1544 daoninvalidate(struct cam_periph *periph)
1546 struct da_softc *softc;
1548 softc = (struct da_softc *)periph->softc;
1551 * De-register any async callbacks.
1553 xpt_register_async(0, daasync, periph, periph->path);
1555 softc->flags |= DA_FLAG_PACK_INVALID;
1558 * Return all queued I/O with ENXIO.
1559 * XXX Handle any transactions queued to the card
1560 * with XPT_ABORT_CCB.
1562 bioq_flush(&softc->bio_queue, NULL, ENXIO);
1563 bioq_flush(&softc->delete_queue, NULL, ENXIO);
1566 * Tell GEOM that we've gone away, we'll get a callback when it is
1567 * done cleaning up its resources.
1569 disk_gone(softc->disk);
1573 dacleanup(struct cam_periph *periph)
1575 struct da_softc *softc;
1577 softc = (struct da_softc *)periph->softc;
1579 cam_periph_unlock(periph);
1582 * If we can't free the sysctl tree, oh well...
1584 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1585 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1586 xpt_print(periph->path, "can't remove sysctl context\n");
1589 callout_drain(&softc->mediapoll_c);
1590 disk_destroy(softc->disk);
1591 callout_drain(&softc->sendordered_c);
1592 free(softc, M_DEVBUF);
1593 cam_periph_lock(periph);
1597 daasync(void *callback_arg, u_int32_t code,
1598 struct cam_path *path, void *arg)
1600 struct cam_periph *periph;
1601 struct da_softc *softc;
1603 periph = (struct cam_periph *)callback_arg;
1605 case AC_FOUND_DEVICE:
1607 struct ccb_getdev *cgd;
1610 cgd = (struct ccb_getdev *)arg;
1614 if (cgd->protocol != PROTO_SCSI)
1617 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1618 && SID_TYPE(&cgd->inq_data) != T_RBC
1619 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1623 * Allocate a peripheral instance for
1624 * this device and start the probe
1627 status = cam_periph_alloc(daregister, daoninvalidate,
1629 "da", CAM_PERIPH_BIO,
1630 cgd->ccb_h.path, daasync,
1631 AC_FOUND_DEVICE, cgd);
1633 if (status != CAM_REQ_CMP
1634 && status != CAM_REQ_INPROG)
1635 printf("daasync: Unable to attach to new device "
1636 "due to status 0x%x\n", status);
1639 case AC_ADVINFO_CHANGED:
1643 buftype = (uintptr_t)arg;
1644 if (buftype == CDAI_TYPE_PHYS_PATH) {
1645 struct da_softc *softc;
1647 softc = periph->softc;
1648 disk_attr_changed(softc->disk, "GEOM::physpath",
1653 case AC_UNIT_ATTENTION:
1656 int error_code, sense_key, asc, ascq;
1658 softc = (struct da_softc *)periph->softc;
1659 ccb = (union ccb *)arg;
1662 * Handle all UNIT ATTENTIONs except our own,
1663 * as they will be handled by daerror().
1665 if (xpt_path_periph(ccb->ccb_h.path) != periph &&
1666 scsi_extract_sense_ccb(ccb,
1667 &error_code, &sense_key, &asc, &ascq)) {
1668 if (asc == 0x2A && ascq == 0x09) {
1669 xpt_print(ccb->ccb_h.path,
1670 "capacity data has changed\n");
1672 } else if (asc == 0x28 && ascq == 0x00)
1673 disk_media_changed(softc->disk, M_NOWAIT);
1675 cam_periph_async(periph, code, path, arg);
1679 softc = (struct da_softc *)periph->softc;
1681 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
1690 struct ccb_hdr *ccbh;
1692 softc = (struct da_softc *)periph->softc;
1694 * Don't fail on the expected unit attention
1697 softc->flags |= DA_FLAG_RETRY_UA;
1698 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1699 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1705 cam_periph_async(periph, code, path, arg);
1709 dasysctlinit(void *context, int pending)
1711 struct cam_periph *periph;
1712 struct da_softc *softc;
1713 char tmpstr[80], tmpstr2[80];
1714 struct ccb_trans_settings cts;
1716 periph = (struct cam_periph *)context;
1718 * periph was held for us when this task was enqueued
1720 if (periph->flags & CAM_PERIPH_INVALID) {
1721 cam_periph_release(periph);
1725 softc = (struct da_softc *)periph->softc;
1726 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1727 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1729 sysctl_ctx_init(&softc->sysctl_ctx);
1730 softc->flags |= DA_FLAG_SCTX_INIT;
1731 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1732 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1733 CTLFLAG_RD, 0, tmpstr);
1734 if (softc->sysctl_tree == NULL) {
1735 printf("dasysctlinit: unable to allocate sysctl tree\n");
1736 cam_periph_release(periph);
1741 * Now register the sysctl handler, so the user can change the value on
1744 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1745 OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RW,
1746 softc, 0, dadeletemethodsysctl, "A",
1747 "BIO_DELETE execution method");
1748 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1749 OID_AUTO, "delete_max", CTLTYPE_U64 | CTLFLAG_RW,
1750 softc, 0, dadeletemaxsysctl, "Q",
1751 "Maximum BIO_DELETE size");
1752 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1753 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1754 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1755 "Minimum CDB size");
1756 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1757 OID_AUTO, "sort_io_queue", CTLFLAG_RW, &softc->sort_io_queue, 0,
1758 "Sort IO queue to try and optimise disk access patterns");
1760 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1761 SYSCTL_CHILDREN(softc->sysctl_tree),
1765 &softc->error_inject,
1767 "error_inject leaf");
1771 * Add some addressing info.
1773 memset(&cts, 0, sizeof (cts));
1774 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1775 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1776 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1777 cam_periph_lock(periph);
1778 xpt_action((union ccb *)&cts);
1779 cam_periph_unlock(periph);
1780 if (cts.ccb_h.status != CAM_REQ_CMP) {
1781 cam_periph_release(periph);
1784 if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
1785 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1786 if (fc->valid & CTS_FC_VALID_WWPN) {
1787 softc->wwpn = fc->wwpn;
1788 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1789 SYSCTL_CHILDREN(softc->sysctl_tree),
1790 OID_AUTO, "wwpn", CTLFLAG_RD,
1791 &softc->wwpn, "World Wide Port Name");
1794 cam_periph_release(periph);
1798 dadeletemaxsysctl(SYSCTL_HANDLER_ARGS)
1802 struct da_softc *softc;
1804 softc = (struct da_softc *)arg1;
1806 value = softc->disk->d_delmaxsize;
1807 error = sysctl_handle_64(oidp, &value, 0, req);
1808 if ((error != 0) || (req->newptr == NULL))
1811 /* only accept values smaller than the calculated value */
1812 if (value > dadeletemaxsize(softc, softc->delete_method)) {
1815 softc->disk->d_delmaxsize = value;
1821 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1825 value = *(int *)arg1;
1827 error = sysctl_handle_int(oidp, &value, 0, req);
1830 || (req->newptr == NULL))
1834 * Acceptable values here are 6, 10, 12 or 16.
1838 else if ((value > 6)
1841 else if ((value > 10)
1844 else if (value > 12)
1847 *(int *)arg1 = value;
1853 dadeletemethodset(struct da_softc *softc, da_delete_methods delete_method)
1857 softc->delete_method = delete_method;
1858 softc->disk->d_delmaxsize = dadeletemaxsize(softc, delete_method);
1859 softc->delete_func = da_delete_functions[delete_method];
1861 if (softc->delete_method > DA_DELETE_DISABLE)
1862 softc->disk->d_flags |= DISKFLAG_CANDELETE;
1864 softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
1868 dadeletemaxsize(struct da_softc *softc, da_delete_methods delete_method)
1872 switch(delete_method) {
1873 case DA_DELETE_UNMAP:
1874 sectors = (off_t)softc->unmap_max_lba;
1876 case DA_DELETE_ATA_TRIM:
1877 sectors = (off_t)ATA_DSM_RANGE_MAX * softc->trim_max_ranges;
1879 case DA_DELETE_WS16:
1880 sectors = (off_t)min(softc->ws_max_blks, WS16_MAX_BLKS);
1882 case DA_DELETE_ZERO:
1883 case DA_DELETE_WS10:
1884 sectors = (off_t)min(softc->ws_max_blks, WS10_MAX_BLKS);
1890 return (off_t)softc->params.secsize *
1891 min(sectors, (off_t)softc->params.sectors);
1895 daprobedone(struct cam_periph *periph, union ccb *ccb)
1897 struct da_softc *softc;
1899 softc = (struct da_softc *)periph->softc;
1901 dadeletemethodchoose(softc, DA_DELETE_NONE);
1903 if (bootverbose && (softc->flags & DA_FLAG_PROBED) == 0) {
1907 snprintf(buf, sizeof(buf), "Delete methods: <");
1909 for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
1910 if (softc->delete_available & (1 << i)) {
1912 strlcat(buf, ",", sizeof(buf));
1916 strlcat(buf, da_delete_method_names[i],
1918 if (i == softc->delete_method) {
1919 strlcat(buf, "(*)", sizeof(buf));
1924 if (softc->delete_method == DA_DELETE_NONE)
1925 strlcat(buf, "NONE(*)", sizeof(buf));
1927 strlcat(buf, "DISABLED(*)", sizeof(buf));
1929 strlcat(buf, ">", sizeof(buf));
1930 printf("%s%d: %s\n", periph->periph_name,
1931 periph->unit_number, buf);
1935 * Since our peripheral may be invalidated by an error
1936 * above or an external event, we must release our CCB
1937 * before releasing the probe lock on the peripheral.
1938 * The peripheral will only go away once the last lock
1939 * is removed, and we need it around for the CCB release
1942 xpt_release_ccb(ccb);
1943 softc->state = DA_STATE_NORMAL;
1945 wakeup(&softc->disk->d_mediasize);
1946 if ((softc->flags & DA_FLAG_PROBED) == 0) {
1947 softc->flags |= DA_FLAG_PROBED;
1948 cam_periph_unhold(periph);
1950 cam_periph_release_locked(periph);
1954 dadeletemethodchoose(struct da_softc *softc, da_delete_methods default_method)
1956 int i, delete_method;
1958 delete_method = default_method;
1961 * Use the pre-defined order to choose the best
1962 * performing delete.
1964 for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
1965 if (softc->delete_available & (1 << i)) {
1966 dadeletemethodset(softc, i);
1970 dadeletemethodset(softc, delete_method);
1974 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
1978 struct da_softc *softc;
1979 int i, error, methods, value;
1981 softc = (struct da_softc *)arg1;
1983 value = softc->delete_method;
1984 if (value < 0 || value > DA_DELETE_MAX)
1987 p = da_delete_method_names[value];
1988 strncpy(buf, p, sizeof(buf));
1989 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1990 if (error != 0 || req->newptr == NULL)
1992 methods = softc->delete_available | (1 << DA_DELETE_DISABLE);
1993 for (i = 0; i <= DA_DELETE_MAX; i++) {
1994 if (!(methods & (1 << i)) ||
1995 strcmp(buf, da_delete_method_names[i]) != 0)
1997 dadeletemethodset(softc, i);
2004 daregister(struct cam_periph *periph, void *arg)
2006 struct da_softc *softc;
2007 struct ccb_pathinq cpi;
2008 struct ccb_getdev *cgd;
2012 cgd = (struct ccb_getdev *)arg;
2014 printf("daregister: no getdev CCB, can't register device\n");
2015 return(CAM_REQ_CMP_ERR);
2018 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
2021 if (softc == NULL) {
2022 printf("daregister: Unable to probe new device. "
2023 "Unable to allocate softc\n");
2024 return(CAM_REQ_CMP_ERR);
2027 LIST_INIT(&softc->pending_ccbs);
2028 softc->state = DA_STATE_PROBE_RC;
2029 bioq_init(&softc->bio_queue);
2030 bioq_init(&softc->delete_queue);
2031 bioq_init(&softc->delete_run_queue);
2032 if (SID_IS_REMOVABLE(&cgd->inq_data))
2033 softc->flags |= DA_FLAG_PACK_REMOVABLE;
2034 softc->unmap_max_ranges = UNMAP_MAX_RANGES;
2035 softc->unmap_max_lba = UNMAP_RANGE_MAX;
2036 softc->ws_max_blks = WS16_MAX_BLKS;
2037 softc->trim_max_ranges = ATA_TRIM_MAX_RANGES;
2038 softc->sort_io_queue = -1;
2040 periph->softc = softc;
2043 * See if this device has any quirks.
2045 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
2046 (caddr_t)da_quirk_table,
2047 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
2048 sizeof(*da_quirk_table), scsi_inquiry_match);
2051 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
2053 softc->quirks = DA_Q_NONE;
2055 /* Check if the SIM does not want 6 byte commands */
2056 bzero(&cpi, sizeof(cpi));
2057 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2058 cpi.ccb_h.func_code = XPT_PATH_INQ;
2059 xpt_action((union ccb *)&cpi);
2060 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
2061 softc->quirks |= DA_Q_NO_6_BYTE;
2063 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
2066 * Take an exclusive refcount on the periph while dastart is called
2067 * to finish the probe. The reference will be dropped in dadone at
2070 (void)cam_periph_hold(periph, PRIBIO);
2073 * Schedule a periodic event to occasionally send an
2074 * ordered tag to a device.
2076 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
2077 callout_reset(&softc->sendordered_c,
2078 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2079 dasendorderedtag, softc);
2081 cam_periph_unlock(periph);
2083 * RBC devices don't have to support READ(6), only READ(10).
2085 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
2086 softc->minimum_cmd_size = 10;
2088 softc->minimum_cmd_size = 6;
2091 * Load the user's default, if any.
2093 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
2094 periph->unit_number);
2095 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
2098 * 6, 10, 12 and 16 are the currently permissible values.
2100 if (softc->minimum_cmd_size < 6)
2101 softc->minimum_cmd_size = 6;
2102 else if ((softc->minimum_cmd_size > 6)
2103 && (softc->minimum_cmd_size <= 10))
2104 softc->minimum_cmd_size = 10;
2105 else if ((softc->minimum_cmd_size > 10)
2106 && (softc->minimum_cmd_size <= 12))
2107 softc->minimum_cmd_size = 12;
2108 else if (softc->minimum_cmd_size > 12)
2109 softc->minimum_cmd_size = 16;
2111 /* Predict whether device may support READ CAPACITY(16). */
2112 if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3 &&
2113 (softc->quirks & DA_Q_NO_RC16) == 0) {
2114 softc->flags |= DA_FLAG_CAN_RC16;
2115 softc->state = DA_STATE_PROBE_RC16;
2119 * Register this media as a disk.
2121 softc->disk = disk_alloc();
2122 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
2123 periph->unit_number, 0,
2124 DEVSTAT_BS_UNAVAILABLE,
2125 SID_TYPE(&cgd->inq_data) |
2126 XPORT_DEVSTAT_TYPE(cpi.transport),
2127 DEVSTAT_PRIORITY_DISK);
2128 softc->disk->d_open = daopen;
2129 softc->disk->d_close = daclose;
2130 softc->disk->d_strategy = dastrategy;
2131 softc->disk->d_dump = dadump;
2132 softc->disk->d_getattr = dagetattr;
2133 softc->disk->d_gone = dadiskgonecb;
2134 softc->disk->d_name = "da";
2135 softc->disk->d_drv1 = periph;
2137 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */
2138 else if (cpi.maxio > MAXPHYS)
2139 softc->disk->d_maxsize = MAXPHYS; /* for safety */
2141 softc->disk->d_maxsize = cpi.maxio;
2142 softc->disk->d_unit = periph->unit_number;
2143 softc->disk->d_flags = 0;
2144 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
2145 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
2146 if ((cpi.hba_misc & PIM_UNMAPPED) != 0)
2147 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
2148 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
2149 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
2150 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
2151 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
2152 cgd->inq_data.product, sizeof(cgd->inq_data.product),
2153 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
2154 softc->disk->d_hba_vendor = cpi.hba_vendor;
2155 softc->disk->d_hba_device = cpi.hba_device;
2156 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
2157 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
2160 * Acquire a reference to the periph before we register with GEOM.
2161 * We'll release this reference once GEOM calls us back (via
2162 * dadiskgonecb()) telling us that our provider has been freed.
2164 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
2165 xpt_print(periph->path, "%s: lost periph during "
2166 "registration!\n", __func__);
2167 cam_periph_lock(periph);
2168 return (CAM_REQ_CMP_ERR);
2171 disk_create(softc->disk, DISK_VERSION);
2172 cam_periph_lock(periph);
2175 * Add async callbacks for events of interest.
2176 * I don't bother checking if this fails as,
2177 * in most cases, the system will function just
2178 * fine without them and the only alternative
2179 * would be to not attach the device on failure.
2181 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
2182 AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION,
2183 daasync, periph, periph->path);
2186 * Emit an attribute changed notification just in case
2187 * physical path information arrived before our async
2188 * event handler was registered, but after anyone attaching
2189 * to our disk device polled it.
2191 disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
2194 * Schedule a periodic media polling events.
2196 callout_init_mtx(&softc->mediapoll_c, periph->sim->mtx, 0);
2197 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
2198 (cgd->inq_flags & SID_AEN) == 0 &&
2199 da_poll_period != 0)
2200 callout_reset(&softc->mediapoll_c, da_poll_period * hz,
2201 damediapoll, periph);
2203 xpt_schedule(periph, CAM_PRIORITY_DEV);
2205 return(CAM_REQ_CMP);
2209 dastart(struct cam_periph *periph, union ccb *start_ccb)
2211 struct da_softc *softc;
2213 softc = (struct da_softc *)periph->softc;
2215 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
2218 switch (softc->state) {
2219 case DA_STATE_NORMAL:
2224 /* Execute immediate CCB if waiting. */
2225 if (periph->immediate_priority <= periph->pinfo.priority) {
2226 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2227 ("queuing for immediate ccb\n"));
2228 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
2229 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
2231 periph->immediate_priority = CAM_PRIORITY_NONE;
2232 wakeup(&periph->ccb_list);
2233 /* May have more work to do, so ensure we stay scheduled */
2238 /* Run BIO_DELETE if not running yet. */
2239 if (!softc->delete_running &&
2240 (bp = bioq_first(&softc->delete_queue)) != NULL) {
2241 if (softc->delete_func != NULL) {
2242 softc->delete_func(periph, start_ccb, bp);
2245 bioq_flush(&softc->delete_queue, NULL, 0);
2250 /* Run regular command. */
2251 bp = bioq_takefirst(&softc->bio_queue);
2255 scsi_test_unit_ready(&start_ccb->csio,
2256 /*retries*/ da_retry_count,
2260 da_default_timeout * 1000);
2261 start_ccb->ccb_h.ccb_bp = NULL;
2262 start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
2263 xpt_action(start_ccb);
2265 xpt_release_ccb(start_ccb);
2270 cam_periph_release_locked(periph);
2273 if ((bp->bio_flags & BIO_ORDERED) != 0 ||
2274 (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
2275 softc->flags &= ~DA_FLAG_NEED_OTAG;
2276 softc->flags |= DA_FLAG_WAS_OTAG;
2277 tag_code = MSG_ORDERED_Q_TAG;
2279 tag_code = MSG_SIMPLE_Q_TAG;
2282 switch (bp->bio_cmd) {
2284 softc->flags |= DA_FLAG_DIRTY;
2287 scsi_read_write(&start_ccb->csio,
2288 /*retries*/da_retry_count,
2290 /*tag_action*/tag_code,
2291 /*read_op*/(bp->bio_cmd == BIO_READ ?
2292 SCSI_RW_READ : SCSI_RW_WRITE) |
2293 ((bp->bio_flags & BIO_UNMAPPED) != 0 ?
2296 softc->minimum_cmd_size,
2297 /*lba*/bp->bio_pblkno,
2298 /*block_count*/bp->bio_bcount /
2299 softc->params.secsize,
2300 /*data_ptr*/ (bp->bio_flags &
2301 BIO_UNMAPPED) != 0 ? (void *)bp :
2303 /*dxfer_len*/ bp->bio_bcount,
2304 /*sense_len*/SSD_FULL_SIZE,
2305 da_default_timeout * 1000);
2309 * BIO_FLUSH doesn't currently communicate
2310 * range data, so we synchronize the cache
2311 * over the whole disk. We also force
2312 * ordered tag semantics the flush applies
2313 * to all previously queued I/O.
2315 scsi_synchronize_cache(&start_ccb->csio,
2322 da_default_timeout*1000);
2325 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
2328 LIST_INSERT_HEAD(&softc->pending_ccbs,
2329 &start_ccb->ccb_h, periph_links.le);
2331 /* We expect a unit attention from this device */
2332 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
2333 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
2334 softc->flags &= ~DA_FLAG_RETRY_UA;
2337 start_ccb->ccb_h.ccb_bp = bp;
2338 xpt_action(start_ccb);
2340 /* May have more work to do, so ensure we stay scheduled */
2344 case DA_STATE_PROBE_RC:
2346 struct scsi_read_capacity_data *rcap;
2348 rcap = (struct scsi_read_capacity_data *)
2349 malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
2351 printf("dastart: Couldn't malloc read_capacity data\n");
2352 /* da_free_periph??? */
2355 scsi_read_capacity(&start_ccb->csio,
2356 /*retries*/da_retry_count,
2362 start_ccb->ccb_h.ccb_bp = NULL;
2363 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC;
2364 xpt_action(start_ccb);
2367 case DA_STATE_PROBE_RC16:
2369 struct scsi_read_capacity_data_long *rcaplong;
2371 rcaplong = (struct scsi_read_capacity_data_long *)
2372 malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
2373 if (rcaplong == NULL) {
2374 printf("dastart: Couldn't malloc read_capacity data\n");
2375 /* da_free_periph??? */
2378 scsi_read_capacity_16(&start_ccb->csio,
2379 /*retries*/ da_retry_count,
2381 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2386 /*sense_len*/ SSD_FULL_SIZE,
2387 /*timeout*/ da_default_timeout * 1000);
2388 start_ccb->ccb_h.ccb_bp = NULL;
2389 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC16;
2390 xpt_action(start_ccb);
2393 case DA_STATE_PROBE_LBP:
2395 struct scsi_vpd_logical_block_prov *lbp;
2397 if (!scsi_vpd_supported_page(periph, SVPD_LBP)) {
2399 * If we get here we don't support any SBC-3 delete
2400 * methods with UNMAP as the Logical Block Provisioning
2401 * VPD page support is required for devices which
2402 * support it according to T10/1799-D Revision 31
2403 * however older revisions of the spec don't mandate
2404 * this so we currently don't remove these methods
2405 * from the available set.
2407 softc->state = DA_STATE_PROBE_BLK_LIMITS;
2411 lbp = (struct scsi_vpd_logical_block_prov *)
2412 malloc(sizeof(*lbp), M_SCSIDA, M_NOWAIT|M_ZERO);
2415 printf("dastart: Couldn't malloc lbp data\n");
2416 /* da_free_periph??? */
2420 scsi_inquiry(&start_ccb->csio,
2421 /*retries*/da_retry_count,
2423 /*tag_action*/MSG_SIMPLE_Q_TAG,
2424 /*inq_buf*/(u_int8_t *)lbp,
2425 /*inq_len*/sizeof(*lbp),
2427 /*page_code*/SVPD_LBP,
2428 /*sense_len*/SSD_MIN_SIZE,
2429 /*timeout*/da_default_timeout * 1000);
2430 start_ccb->ccb_h.ccb_bp = NULL;
2431 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_LBP;
2432 xpt_action(start_ccb);
2435 case DA_STATE_PROBE_BLK_LIMITS:
2437 struct scsi_vpd_block_limits *block_limits;
2439 if (!scsi_vpd_supported_page(periph, SVPD_BLOCK_LIMITS)) {
2440 /* Not supported skip to next probe */
2441 softc->state = DA_STATE_PROBE_BDC;
2445 block_limits = (struct scsi_vpd_block_limits *)
2446 malloc(sizeof(*block_limits), M_SCSIDA, M_NOWAIT|M_ZERO);
2448 if (block_limits == NULL) {
2449 printf("dastart: Couldn't malloc block_limits data\n");
2450 /* da_free_periph??? */
2454 scsi_inquiry(&start_ccb->csio,
2455 /*retries*/da_retry_count,
2457 /*tag_action*/MSG_SIMPLE_Q_TAG,
2458 /*inq_buf*/(u_int8_t *)block_limits,
2459 /*inq_len*/sizeof(*block_limits),
2461 /*page_code*/SVPD_BLOCK_LIMITS,
2462 /*sense_len*/SSD_MIN_SIZE,
2463 /*timeout*/da_default_timeout * 1000);
2464 start_ccb->ccb_h.ccb_bp = NULL;
2465 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BLK_LIMITS;
2466 xpt_action(start_ccb);
2469 case DA_STATE_PROBE_BDC:
2471 struct scsi_vpd_block_characteristics *bdc;
2473 if (!scsi_vpd_supported_page(periph, SVPD_BDC)) {
2474 softc->state = DA_STATE_PROBE_ATA;
2478 bdc = (struct scsi_vpd_block_characteristics *)
2479 malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
2482 printf("dastart: Couldn't malloc bdc data\n");
2483 /* da_free_periph??? */
2487 scsi_inquiry(&start_ccb->csio,
2488 /*retries*/da_retry_count,
2490 /*tag_action*/MSG_SIMPLE_Q_TAG,
2491 /*inq_buf*/(u_int8_t *)bdc,
2492 /*inq_len*/sizeof(*bdc),
2494 /*page_code*/SVPD_BDC,
2495 /*sense_len*/SSD_MIN_SIZE,
2496 /*timeout*/da_default_timeout * 1000);
2497 start_ccb->ccb_h.ccb_bp = NULL;
2498 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BDC;
2499 xpt_action(start_ccb);
2502 case DA_STATE_PROBE_ATA:
2504 struct ata_params *ata_params;
2506 if (!scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
2507 daprobedone(periph, start_ccb);
2511 ata_params = (struct ata_params*)
2512 malloc(sizeof(*ata_params), M_SCSIDA, M_NOWAIT|M_ZERO);
2514 if (ata_params == NULL) {
2515 printf("dastart: Couldn't malloc ata_params data\n");
2516 /* da_free_periph??? */
2520 scsi_ata_identify(&start_ccb->csio,
2521 /*retries*/da_retry_count,
2523 /*tag_action*/MSG_SIMPLE_Q_TAG,
2524 /*data_ptr*/(u_int8_t *)ata_params,
2525 /*dxfer_len*/sizeof(*ata_params),
2526 /*sense_len*/SSD_FULL_SIZE,
2527 /*timeout*/da_default_timeout * 1000);
2528 start_ccb->ccb_h.ccb_bp = NULL;
2529 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA;
2530 xpt_action(start_ccb);
2537 * In each of the methods below, while its the caller's
2538 * responsibility to ensure the request will fit into a
2539 * single device request, we might have changed the delete
2540 * method due to the device incorrectly advertising either
2541 * its supported methods or limits.
2543 * To prevent this causing further issues we validate the
2544 * against the methods limits, and warn which would
2545 * otherwise be unnecessary.
2548 da_delete_unmap(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2550 struct da_softc *softc = (struct da_softc *)periph->softc;;
2552 uint8_t *buf = softc->unmap_buf;
2553 uint64_t lba, lastlba = (uint64_t)-1;
2554 uint64_t totalcount = 0;
2556 uint32_t lastcount = 0, c;
2557 uint32_t off, ranges = 0;
2560 * Currently this doesn't take the UNMAP
2561 * Granularity and Granularity Alignment
2562 * fields into account.
2564 * This could result in both unoptimal unmap
2565 * requests as as well as UNMAP calls unmapping
2566 * fewer LBA's than requested.
2569 softc->delete_running = 1;
2570 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2573 bioq_remove(&softc->delete_queue, bp1);
2575 bioq_insert_tail(&softc->delete_run_queue, bp1);
2576 lba = bp1->bio_pblkno;
2577 count = bp1->bio_bcount / softc->params.secsize;
2579 /* Try to extend the previous range. */
2580 if (lba == lastlba) {
2581 c = omin(count, UNMAP_RANGE_MAX - lastcount);
2583 off = ((ranges - 1) * UNMAP_RANGE_SIZE) +
2585 scsi_ulto4b(lastcount, &buf[off + 8]);
2592 c = omin(count, UNMAP_RANGE_MAX);
2593 if (totalcount + c > softc->unmap_max_lba ||
2594 ranges >= softc->unmap_max_ranges) {
2595 xpt_print(periph->path,
2596 "%s issuing short delete %ld > %ld"
2598 da_delete_method_desc[softc->delete_method],
2599 totalcount + c, softc->unmap_max_lba,
2600 ranges, softc->unmap_max_ranges);
2603 off = (ranges * UNMAP_RANGE_SIZE) + UNMAP_HEAD_SIZE;
2604 scsi_u64to8b(lba, &buf[off + 0]);
2605 scsi_ulto4b(c, &buf[off + 8]);
2613 bp1 = bioq_first(&softc->delete_queue);
2614 if (bp1 == NULL || ranges >= softc->unmap_max_ranges ||
2615 totalcount + bp1->bio_bcount /
2616 softc->params.secsize > softc->unmap_max_lba)
2619 scsi_ulto2b(ranges * 16 + 6, &buf[0]);
2620 scsi_ulto2b(ranges * 16, &buf[2]);
2622 scsi_unmap(&ccb->csio,
2623 /*retries*/da_retry_count,
2625 /*tag_action*/MSG_SIMPLE_Q_TAG,
2628 /*dxfer_len*/ ranges * 16 + 8,
2629 /*sense_len*/SSD_FULL_SIZE,
2630 da_default_timeout * 1000);
2631 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2635 da_delete_trim(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2637 struct da_softc *softc = (struct da_softc *)periph->softc;
2639 uint8_t *buf = softc->unmap_buf;
2640 uint64_t lastlba = (uint64_t)-1;
2643 uint32_t lastcount = 0, c, requestcount;
2644 int ranges = 0, off, block_count;
2646 softc->delete_running = 1;
2647 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2650 bioq_remove(&softc->delete_queue, bp1);
2652 bioq_insert_tail(&softc->delete_run_queue, bp1);
2653 lba = bp1->bio_pblkno;
2654 count = bp1->bio_bcount / softc->params.secsize;
2655 requestcount = count;
2657 /* Try to extend the previous range. */
2658 if (lba == lastlba) {
2659 c = min(count, ATA_DSM_RANGE_MAX - lastcount);
2661 off = (ranges - 1) * 8;
2662 buf[off + 6] = lastcount & 0xff;
2663 buf[off + 7] = (lastcount >> 8) & 0xff;
2669 c = min(count, ATA_DSM_RANGE_MAX);
2672 buf[off + 0] = lba & 0xff;
2673 buf[off + 1] = (lba >> 8) & 0xff;
2674 buf[off + 2] = (lba >> 16) & 0xff;
2675 buf[off + 3] = (lba >> 24) & 0xff;
2676 buf[off + 4] = (lba >> 32) & 0xff;
2677 buf[off + 5] = (lba >> 40) & 0xff;
2678 buf[off + 6] = c & 0xff;
2679 buf[off + 7] = (c >> 8) & 0xff;
2684 if (count != 0 && ranges == softc->trim_max_ranges) {
2685 xpt_print(periph->path,
2686 "%s issuing short delete %ld > %ld\n",
2687 da_delete_method_desc[softc->delete_method],
2689 (softc->trim_max_ranges - ranges) *
2695 bp1 = bioq_first(&softc->delete_queue);
2696 if (bp1 == NULL || bp1->bio_bcount / softc->params.secsize >
2697 (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX)
2701 block_count = (ranges + ATA_DSM_BLK_RANGES - 1) / ATA_DSM_BLK_RANGES;
2702 scsi_ata_trim(&ccb->csio,
2703 /*retries*/da_retry_count,
2705 /*tag_action*/MSG_SIMPLE_Q_TAG,
2708 /*dxfer_len*/block_count * ATA_DSM_BLK_SIZE,
2709 /*sense_len*/SSD_FULL_SIZE,
2710 da_default_timeout * 1000);
2711 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2715 * We calculate ws_max_blks here based off d_delmaxsize instead
2716 * of using softc->ws_max_blks as it is absolute max for the
2717 * device not the protocol max which may well be lower.
2720 da_delete_ws(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2722 struct da_softc *softc;
2724 uint64_t ws_max_blks;
2726 uint64_t count; /* forward compat with WS32 */
2728 softc = (struct da_softc *)periph->softc;
2729 ws_max_blks = softc->disk->d_delmaxsize / softc->params.secsize;
2730 softc->delete_running = 1;
2731 lba = bp->bio_pblkno;
2735 bioq_remove(&softc->delete_queue, bp1);
2737 bioq_insert_tail(&softc->delete_run_queue, bp1);
2738 count += bp1->bio_bcount / softc->params.secsize;
2739 if (count > ws_max_blks) {
2740 xpt_print(periph->path,
2741 "%s issuing short delete %ld > %ld\n",
2742 da_delete_method_desc[softc->delete_method],
2743 count, ws_max_blks);
2744 count = min(count, ws_max_blks);
2747 bp1 = bioq_first(&softc->delete_queue);
2748 if (bp1 == NULL || lba + count != bp1->bio_pblkno ||
2749 count + bp1->bio_bcount /
2750 softc->params.secsize > ws_max_blks)
2754 scsi_write_same(&ccb->csio,
2755 /*retries*/da_retry_count,
2757 /*tag_action*/MSG_SIMPLE_Q_TAG,
2758 /*byte2*/softc->delete_method ==
2759 DA_DELETE_ZERO ? 0 : SWS_UNMAP,
2760 softc->delete_method == DA_DELETE_WS16 ? 16 : 10,
2762 /*block_count*/count,
2763 /*data_ptr*/ __DECONST(void *, zero_region),
2764 /*dxfer_len*/ softc->params.secsize,
2765 /*sense_len*/SSD_FULL_SIZE,
2766 da_default_timeout * 1000);
2767 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2771 cmd6workaround(union ccb *ccb)
2773 struct scsi_rw_6 cmd6;
2774 struct scsi_rw_10 *cmd10;
2775 struct da_softc *softc;
2780 cdb = ccb->csio.cdb_io.cdb_bytes;
2781 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
2783 if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
2784 da_delete_methods old_method = softc->delete_method;
2787 * Typically there are two reasons for failure here
2788 * 1. Delete method was detected as supported but isn't
2789 * 2. Delete failed due to invalid params e.g. too big
2791 * While we will attempt to choose an alternative delete method
2792 * this may result in short deletes if the existing delete
2793 * requests from geom are big for the new method choosen.
2795 * This method assumes that the error which triggered this
2796 * will not retry the io otherwise a panic will occur
2798 dadeleteflag(softc, old_method, 0);
2799 dadeletemethodchoose(softc, DA_DELETE_DISABLE);
2800 if (softc->delete_method == DA_DELETE_DISABLE)
2801 xpt_print(ccb->ccb_h.path,
2802 "%s failed, disabling BIO_DELETE\n",
2803 da_delete_method_desc[old_method]);
2805 xpt_print(ccb->ccb_h.path,
2806 "%s failed, switching to %s BIO_DELETE\n",
2807 da_delete_method_desc[old_method],
2808 da_delete_method_desc[softc->delete_method]);
2811 while ((bp = bioq_takefirst(&softc->delete_run_queue))
2813 bioq_disksort(&softc->delete_queue, bp);
2815 while ((bp = bioq_takefirst(&softc->delete_run_queue))
2817 bioq_insert_tail(&softc->delete_queue, bp);
2819 bioq_insert_tail(&softc->delete_queue,
2820 (struct bio *)ccb->ccb_h.ccb_bp);
2821 ccb->ccb_h.ccb_bp = NULL;
2825 /* Detect unsupported PREVENT ALLOW MEDIUM REMOVAL. */
2826 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
2827 (*cdb == PREVENT_ALLOW) &&
2828 (softc->quirks & DA_Q_NO_PREVENT) == 0) {
2830 xpt_print(ccb->ccb_h.path,
2831 "PREVENT ALLOW MEDIUM REMOVAL not supported.\n");
2832 softc->quirks |= DA_Q_NO_PREVENT;
2836 /* Detect unsupported SYNCHRONIZE CACHE(10). */
2837 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
2838 (*cdb == SYNCHRONIZE_CACHE) &&
2839 (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
2841 xpt_print(ccb->ccb_h.path,
2842 "SYNCHRONIZE CACHE(10) not supported.\n");
2843 softc->quirks |= DA_Q_NO_SYNC_CACHE;
2844 softc->disk->d_flags &= ~DISKFLAG_CANFLUSHCACHE;
2848 /* Translation only possible if CDB is an array and cmd is R/W6 */
2849 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
2850 (*cdb != READ_6 && *cdb != WRITE_6))
2853 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
2854 "increasing minimum_cmd_size to 10.\n");
2855 softc->minimum_cmd_size = 10;
2857 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
2858 cmd10 = (struct scsi_rw_10 *)cdb;
2859 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
2861 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
2862 cmd10->reserved = 0;
2863 scsi_ulto2b(cmd6.length, cmd10->length);
2864 cmd10->control = cmd6.control;
2865 ccb->csio.cdb_len = sizeof(*cmd10);
2867 /* Requeue request, unfreezing queue if necessary */
2868 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
2869 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2872 cam_release_devq(ccb->ccb_h.path,
2876 /*getcount_only*/0);
2882 dadone(struct cam_periph *periph, union ccb *done_ccb)
2884 struct da_softc *softc;
2885 struct ccb_scsiio *csio;
2889 softc = (struct da_softc *)periph->softc;
2890 priority = done_ccb->ccb_h.pinfo.priority;
2892 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
2894 csio = &done_ccb->csio;
2895 state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
2897 case DA_CCB_BUFFER_IO:
2900 struct bio *bp, *bp1;
2902 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2903 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2907 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
2912 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
2913 if (error == ERESTART) {
2915 * A retry was scheduled, so
2920 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2925 * return all queued I/O with EIO, so that
2926 * the client can retry these I/Os in the
2927 * proper order should it attempt to recover.
2932 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
2934 * Catastrophic error. Mark our pack as
2938 * XXX See if this is really a media
2941 xpt_print(periph->path,
2942 "Invalidating pack\n");
2943 softc->flags |= DA_FLAG_PACK_INVALID;
2944 queued_error = ENXIO;
2946 bioq_flush(&softc->bio_queue, NULL,
2949 bp->bio_error = error;
2950 bp->bio_resid = bp->bio_bcount;
2951 bp->bio_flags |= BIO_ERROR;
2953 } else if (bp != NULL) {
2954 if (state == DA_CCB_DELETE)
2957 bp->bio_resid = csio->resid;
2959 if (bp->bio_resid != 0)
2960 bp->bio_flags |= BIO_ERROR;
2962 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2963 cam_release_devq(done_ccb->ccb_h.path,
2967 /*getcount_only*/0);
2968 } else if (bp != NULL) {
2969 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2970 panic("REQ_CMP with QFRZN");
2971 if (state == DA_CCB_DELETE)
2974 bp->bio_resid = csio->resid;
2975 if (csio->resid > 0)
2976 bp->bio_flags |= BIO_ERROR;
2977 if (softc->error_inject != 0) {
2978 bp->bio_error = softc->error_inject;
2979 bp->bio_resid = bp->bio_bcount;
2980 bp->bio_flags |= BIO_ERROR;
2981 softc->error_inject = 0;
2985 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
2986 if (LIST_EMPTY(&softc->pending_ccbs))
2987 softc->flags |= DA_FLAG_WAS_OTAG;
2989 if (state == DA_CCB_DELETE) {
2990 while ((bp1 = bioq_takefirst(&softc->delete_run_queue))
2992 bp1->bio_error = bp->bio_error;
2993 if (bp->bio_flags & BIO_ERROR) {
2994 bp1->bio_flags |= BIO_ERROR;
2995 bp1->bio_resid = bp1->bio_bcount;
3000 softc->delete_running = 0;
3004 } else if (bp != NULL)
3008 case DA_CCB_PROBE_RC:
3009 case DA_CCB_PROBE_RC16:
3011 struct scsi_read_capacity_data *rdcap;
3012 struct scsi_read_capacity_data_long *rcaplong;
3013 char announce_buf[80];
3019 if (state == DA_CCB_PROBE_RC)
3020 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
3022 rcaplong = (struct scsi_read_capacity_data_long *)
3025 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3026 struct disk_params *dp;
3027 uint32_t block_size;
3029 u_int lbppbe; /* LB per physical block exponent. */
3030 u_int lalba; /* Lowest aligned LBA. */
3032 if (state == DA_CCB_PROBE_RC) {
3033 block_size = scsi_4btoul(rdcap->length);
3034 maxsector = scsi_4btoul(rdcap->addr);
3039 * According to SBC-2, if the standard 10
3040 * byte READ CAPACITY command returns 2^32,
3041 * we should issue the 16 byte version of
3042 * the command, since the device in question
3043 * has more sectors than can be represented
3044 * with the short version of the command.
3046 if (maxsector == 0xffffffff) {
3047 free(rdcap, M_SCSIDA);
3048 xpt_release_ccb(done_ccb);
3049 softc->state = DA_STATE_PROBE_RC16;
3050 xpt_schedule(periph, priority);
3054 block_size = scsi_4btoul(rcaplong->length);
3055 maxsector = scsi_8btou64(rcaplong->addr);
3056 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
3057 lalba = scsi_2btoul(rcaplong->lalba_lbp);
3061 * Because GEOM code just will panic us if we
3062 * give them an 'illegal' value we'll avoid that
3065 if (block_size == 0 && maxsector == 0) {
3069 if (block_size >= MAXPHYS || block_size == 0) {
3070 xpt_print(periph->path,
3071 "unsupportable block size %ju\n",
3072 (uintmax_t) block_size);
3073 announce_buf[0] = '\0';
3074 cam_periph_invalidate(periph);
3077 * We pass rcaplong into dasetgeom(),
3078 * because it will only use it if it is
3081 dasetgeom(periph, block_size, maxsector,
3082 rcaplong, sizeof(*rcaplong));
3083 lbp = (lalba & SRC16_LBPME_A);
3084 dp = &softc->params;
3085 snprintf(announce_buf, sizeof(announce_buf),
3086 "%juMB (%ju %u byte sectors: %dH %dS/T "
3088 (((uintmax_t)dp->secsize *
3089 dp->sectors) / (1024*1024)),
3090 (uintmax_t)dp->sectors,
3091 dp->secsize, dp->heads,
3092 dp->secs_per_track, dp->cylinders);
3097 announce_buf[0] = '\0';
3100 * Retry any UNIT ATTENTION type errors. They
3101 * are expected at boot.
3103 error = daerror(done_ccb, CAM_RETRY_SELTO,
3104 SF_RETRY_UA|SF_NO_PRINT);
3105 if (error == ERESTART) {
3107 * A retry was scheuled, so
3111 } else if (error != 0) {
3113 int sense_key, error_code;
3116 struct ccb_getdev cgd;
3118 /* Don't wedge this device's queue */
3119 status = done_ccb->ccb_h.status;
3120 if ((status & CAM_DEV_QFRZN) != 0)
3121 cam_release_devq(done_ccb->ccb_h.path,
3125 /*getcount_only*/0);
3128 xpt_setup_ccb(&cgd.ccb_h,
3129 done_ccb->ccb_h.path,
3130 CAM_PRIORITY_NORMAL);
3131 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
3132 xpt_action((union ccb *)&cgd);
3134 if (scsi_extract_sense_ccb(done_ccb,
3135 &error_code, &sense_key, &asc, &ascq))
3141 * If we tried READ CAPACITY(16) and failed,
3142 * fallback to READ CAPACITY(10).
3144 if ((state == DA_CCB_PROBE_RC16) &&
3145 (softc->flags & DA_FLAG_CAN_RC16) &&
3146 (((csio->ccb_h.status & CAM_STATUS_MASK) ==
3149 (error_code == SSD_CURRENT_ERROR) &&
3150 (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
3151 softc->flags &= ~DA_FLAG_CAN_RC16;
3152 free(rdcap, M_SCSIDA);
3153 xpt_release_ccb(done_ccb);
3154 softc->state = DA_STATE_PROBE_RC;
3155 xpt_schedule(periph, priority);
3159 * Attach to anything that claims to be a
3160 * direct access or optical disk device,
3161 * as long as it doesn't return a "Logical
3162 * unit not supported" (0x25) error.
3164 if ((have_sense) && (asc != 0x25)
3165 && (error_code == SSD_CURRENT_ERROR)) {
3166 const char *sense_key_desc;
3167 const char *asc_desc;
3169 dasetgeom(periph, 512, -1, NULL, 0);
3170 scsi_sense_desc(sense_key, asc, ascq,
3174 snprintf(announce_buf,
3175 sizeof(announce_buf),
3176 "Attempt to query device "
3177 "size failed: %s, %s",
3185 xpt_print(periph->path,
3186 "got CAM status %#x\n",
3187 done_ccb->ccb_h.status);
3190 xpt_print(periph->path, "fatal error, "
3191 "failed to attach to device\n");
3194 * Free up resources.
3196 cam_periph_invalidate(periph);
3200 free(csio->data_ptr, M_SCSIDA);
3201 if (announce_buf[0] != '\0' && ((softc->flags & DA_FLAG_PROBED) == 0)) {
3203 * Create our sysctl variables, now that we know
3204 * we have successfully attached.
3206 /* increase the refcount */
3207 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3208 taskqueue_enqueue(taskqueue_thread,
3209 &softc->sysctl_task);
3210 xpt_announce_periph(periph, announce_buf);
3211 xpt_announce_quirks(periph, softc->quirks,
3214 xpt_print(periph->path, "fatal error, "
3215 "could not acquire reference count\n");
3219 /* Ensure re-probe doesn't see old delete. */
3220 softc->delete_available = 0;
3221 if (lbp && (softc->quirks & DA_Q_NO_UNMAP) == 0) {
3223 * Based on older SBC-3 spec revisions
3224 * any of the UNMAP methods "may" be
3225 * available via LBP given this flag so
3226 * we flag all of them as availble and
3227 * then remove those which further
3228 * probes confirm aren't available
3231 * We could also check readcap(16) p_type
3232 * flag to exclude one or more invalid
3233 * write same (X) types here
3235 dadeleteflag(softc, DA_DELETE_WS16, 1);
3236 dadeleteflag(softc, DA_DELETE_WS10, 1);
3237 dadeleteflag(softc, DA_DELETE_ZERO, 1);
3238 dadeleteflag(softc, DA_DELETE_UNMAP, 1);
3240 xpt_release_ccb(done_ccb);
3241 softc->state = DA_STATE_PROBE_LBP;
3242 xpt_schedule(periph, priority);
3246 xpt_release_ccb(done_ccb);
3247 softc->state = DA_STATE_PROBE_BDC;
3248 xpt_schedule(periph, priority);
3251 case DA_CCB_PROBE_LBP:
3253 struct scsi_vpd_logical_block_prov *lbp;
3255 lbp = (struct scsi_vpd_logical_block_prov *)csio->data_ptr;
3257 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3259 * T10/1799-D Revision 31 states at least one of these
3260 * must be supported but we don't currently enforce this.
3262 dadeleteflag(softc, DA_DELETE_WS16,
3263 (lbp->flags & SVPD_LBP_WS16));
3264 dadeleteflag(softc, DA_DELETE_WS10,
3265 (lbp->flags & SVPD_LBP_WS10));
3266 dadeleteflag(softc, DA_DELETE_ZERO,
3267 (lbp->flags & SVPD_LBP_WS10));
3268 dadeleteflag(softc, DA_DELETE_UNMAP,
3269 (lbp->flags & SVPD_LBP_UNMAP));
3271 if (lbp->flags & SVPD_LBP_UNMAP) {
3272 free(lbp, M_SCSIDA);
3273 xpt_release_ccb(done_ccb);
3274 softc->state = DA_STATE_PROBE_BLK_LIMITS;
3275 xpt_schedule(periph, priority);
3280 error = daerror(done_ccb, CAM_RETRY_SELTO,
3281 SF_RETRY_UA|SF_NO_PRINT);
3282 if (error == ERESTART)
3284 else if (error != 0) {
3285 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3286 /* Don't wedge this device's queue */
3287 cam_release_devq(done_ccb->ccb_h.path,
3291 /*getcount_only*/0);
3295 * Failure indicates we don't support any SBC-3
3296 * delete methods with UNMAP
3301 free(lbp, M_SCSIDA);
3302 xpt_release_ccb(done_ccb);
3303 softc->state = DA_STATE_PROBE_BDC;
3304 xpt_schedule(periph, priority);
3307 case DA_CCB_PROBE_BLK_LIMITS:
3309 struct scsi_vpd_block_limits *block_limits;
3311 block_limits = (struct scsi_vpd_block_limits *)csio->data_ptr;
3313 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3314 uint32_t max_unmap_lba_cnt = scsi_4btoul(
3315 block_limits->max_unmap_lba_cnt);
3316 uint32_t max_unmap_blk_cnt = scsi_4btoul(
3317 block_limits->max_unmap_blk_cnt);
3318 uint64_t ws_max_blks = scsi_8btou64(
3319 block_limits->max_write_same_length);
3321 * We should already support UNMAP but we check lba
3322 * and block count to be sure
3324 if (max_unmap_lba_cnt != 0x00L &&
3325 max_unmap_blk_cnt != 0x00L) {
3326 softc->unmap_max_lba = max_unmap_lba_cnt;
3327 softc->unmap_max_ranges = min(max_unmap_blk_cnt,
3331 * Unexpected UNMAP limits which means the
3332 * device doesn't actually support UNMAP
3334 dadeleteflag(softc, DA_DELETE_UNMAP, 0);
3337 if (ws_max_blks != 0x00L)
3338 softc->ws_max_blks = ws_max_blks;
3341 error = daerror(done_ccb, CAM_RETRY_SELTO,
3342 SF_RETRY_UA|SF_NO_PRINT);
3343 if (error == ERESTART)
3345 else if (error != 0) {
3346 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3347 /* Don't wedge this device's queue */
3348 cam_release_devq(done_ccb->ccb_h.path,
3352 /*getcount_only*/0);
3356 * Failure here doesn't mean UNMAP is not
3357 * supported as this is an optional page.
3359 softc->unmap_max_lba = 1;
3360 softc->unmap_max_ranges = 1;
3364 free(block_limits, M_SCSIDA);
3365 xpt_release_ccb(done_ccb);
3366 softc->state = DA_STATE_PROBE_BDC;
3367 xpt_schedule(periph, priority);
3370 case DA_CCB_PROBE_BDC:
3372 struct scsi_vpd_block_characteristics *bdc;
3374 bdc = (struct scsi_vpd_block_characteristics *)csio->data_ptr;
3376 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3378 * Disable queue sorting for non-rotational media
3381 if (scsi_2btoul(bdc->medium_rotation_rate) ==
3382 SVPD_BDC_RATE_NONE_ROTATING)
3383 softc->sort_io_queue = 0;
3386 error = daerror(done_ccb, CAM_RETRY_SELTO,
3387 SF_RETRY_UA|SF_NO_PRINT);
3388 if (error == ERESTART)
3390 else if (error != 0) {
3391 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3392 /* Don't wedge this device's queue */
3393 cam_release_devq(done_ccb->ccb_h.path,
3397 /*getcount_only*/0);
3402 free(bdc, M_SCSIDA);
3403 xpt_release_ccb(done_ccb);
3404 softc->state = DA_STATE_PROBE_ATA;
3405 xpt_schedule(periph, priority);
3408 case DA_CCB_PROBE_ATA:
3411 struct ata_params *ata_params;
3414 ata_params = (struct ata_params *)csio->data_ptr;
3415 ptr = (uint16_t *)ata_params;
3417 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3418 for (i = 0; i < sizeof(*ata_params) / 2; i++)
3419 ptr[i] = le16toh(ptr[i]);
3420 if (ata_params->support_dsm & ATA_SUPPORT_DSM_TRIM &&
3421 (softc->quirks & DA_Q_NO_UNMAP) == 0) {
3422 dadeleteflag(softc, DA_DELETE_ATA_TRIM, 1);
3423 if (ata_params->max_dsm_blocks != 0)
3424 softc->trim_max_ranges = min(
3425 softc->trim_max_ranges,
3426 ata_params->max_dsm_blocks *
3427 ATA_DSM_BLK_RANGES);
3430 * Disable queue sorting for non-rotational media
3433 if (ata_params->media_rotation_rate == 1)
3434 softc->sort_io_queue = 0;
3437 error = daerror(done_ccb, CAM_RETRY_SELTO,
3438 SF_RETRY_UA|SF_NO_PRINT);
3439 if (error == ERESTART)
3441 else if (error != 0) {
3442 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3443 /* Don't wedge this device's queue */
3444 cam_release_devq(done_ccb->ccb_h.path,
3448 /*getcount_only*/0);
3453 free(ata_params, M_SCSIDA);
3454 daprobedone(periph, done_ccb);
3457 case DA_CCB_WAITING:
3459 /* Caller will release the CCB */
3460 wakeup(&done_ccb->ccb_h.cbfcnp);
3464 /* No-op. We're polling */
3468 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3470 if (daerror(done_ccb, CAM_RETRY_SELTO,
3471 SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
3474 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3475 cam_release_devq(done_ccb->ccb_h.path,
3479 /*getcount_only*/0);
3481 xpt_release_ccb(done_ccb);
3482 cam_periph_release_locked(periph);
3488 xpt_release_ccb(done_ccb);
3492 dareprobe(struct cam_periph *periph)
3494 struct da_softc *softc;
3497 softc = (struct da_softc *)periph->softc;
3499 /* Probe in progress; don't interfere. */
3500 if (softc->state != DA_STATE_NORMAL)
3503 status = cam_periph_acquire(periph);
3504 KASSERT(status == CAM_REQ_CMP,
3505 ("dareprobe: cam_periph_acquire failed"));
3507 if (softc->flags & DA_FLAG_CAN_RC16)
3508 softc->state = DA_STATE_PROBE_RC16;
3510 softc->state = DA_STATE_PROBE_RC;
3512 xpt_schedule(periph, CAM_PRIORITY_DEV);
3516 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3518 struct da_softc *softc;
3519 struct cam_periph *periph;
3520 int error, error_code, sense_key, asc, ascq;
3522 periph = xpt_path_periph(ccb->ccb_h.path);
3523 softc = (struct da_softc *)periph->softc;
3526 * Automatically detect devices that do not support
3527 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
3530 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3531 error = cmd6workaround(ccb);
3532 } else if (scsi_extract_sense_ccb(ccb,
3533 &error_code, &sense_key, &asc, &ascq)) {
3534 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3535 error = cmd6workaround(ccb);
3537 * If the target replied with CAPACITY DATA HAS CHANGED UA,
3538 * query the capacity and notify upper layers.
3540 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3541 asc == 0x2A && ascq == 0x09) {
3542 xpt_print(periph->path, "capacity data has changed\n");
3544 sense_flags |= SF_NO_PRINT;
3545 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3546 asc == 0x28 && ascq == 0x00)
3547 disk_media_changed(softc->disk, M_NOWAIT);
3548 else if (sense_key == SSD_KEY_NOT_READY &&
3549 asc == 0x3a && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
3550 softc->flags |= DA_FLAG_PACK_INVALID;
3551 disk_media_gone(softc->disk, M_NOWAIT);
3554 if (error == ERESTART)
3559 * Until we have a better way of doing pack validation,
3560 * don't treat UAs as errors.
3562 sense_flags |= SF_RETRY_UA;
3563 return(cam_periph_error(ccb, cam_flags, sense_flags,
3564 &softc->saved_ccb));
3568 damediapoll(void *arg)
3570 struct cam_periph *periph = arg;
3571 struct da_softc *softc = periph->softc;
3573 if (!softc->tur && LIST_EMPTY(&softc->pending_ccbs)) {
3574 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3579 /* Queue us up again */
3580 if (da_poll_period != 0)
3581 callout_schedule(&softc->mediapoll_c, da_poll_period * hz);
3585 daprevent(struct cam_periph *periph, int action)
3587 struct da_softc *softc;
3591 softc = (struct da_softc *)periph->softc;
3593 if (((action == PR_ALLOW)
3594 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
3595 || ((action == PR_PREVENT)
3596 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
3600 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3602 scsi_prevent(&ccb->csio,
3610 error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
3611 SF_RETRY_UA | SF_NO_PRINT, softc->disk->d_devstat);
3614 if (action == PR_ALLOW)
3615 softc->flags &= ~DA_FLAG_PACK_LOCKED;
3617 softc->flags |= DA_FLAG_PACK_LOCKED;
3620 xpt_release_ccb(ccb);
3624 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
3625 struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
3627 struct ccb_calc_geometry ccg;
3628 struct da_softc *softc;
3629 struct disk_params *dp;
3630 u_int lbppbe, lalba;
3632 softc = (struct da_softc *)periph->softc;
3634 dp = &softc->params;
3635 dp->secsize = block_len;
3636 dp->sectors = maxsector + 1;
3637 if (rcaplong != NULL) {
3638 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
3639 lalba = scsi_2btoul(rcaplong->lalba_lbp);
3640 lalba &= SRC16_LALBA_A;
3647 dp->stripesize = block_len << lbppbe;
3648 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
3650 } else if (softc->quirks & DA_Q_4K) {
3651 dp->stripesize = 4096;
3652 dp->stripeoffset = 0;
3655 dp->stripeoffset = 0;
3658 * Have the controller provide us with a geometry
3659 * for this disk. The only time the geometry
3660 * matters is when we boot and the controller
3661 * is the only one knowledgeable enough to come
3662 * up with something that will make this a bootable
3665 xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3666 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
3667 ccg.block_size = dp->secsize;
3668 ccg.volume_size = dp->sectors;
3670 ccg.secs_per_track = 0;
3672 xpt_action((union ccb*)&ccg);
3673 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3675 * We don't know what went wrong here- but just pick
3676 * a geometry so we don't have nasty things like divide
3680 dp->secs_per_track = 255;
3681 dp->cylinders = dp->sectors / (255 * 255);
3682 if (dp->cylinders == 0) {
3686 dp->heads = ccg.heads;
3687 dp->secs_per_track = ccg.secs_per_track;
3688 dp->cylinders = ccg.cylinders;
3692 * If the user supplied a read capacity buffer, and if it is
3693 * different than the previous buffer, update the data in the EDT.
3694 * If it's the same, we don't bother. This avoids sending an
3695 * update every time someone opens this device.
3697 if ((rcaplong != NULL)
3698 && (bcmp(rcaplong, &softc->rcaplong,
3699 min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
3700 struct ccb_dev_advinfo cdai;
3702 xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3703 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
3704 cdai.buftype = CDAI_TYPE_RCAPLONG;
3705 cdai.flags |= CDAI_FLAG_STORE;
3706 cdai.bufsiz = rcap_len;
3707 cdai.buf = (uint8_t *)rcaplong;
3708 xpt_action((union ccb *)&cdai);
3709 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
3710 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
3711 if (cdai.ccb_h.status != CAM_REQ_CMP) {
3712 xpt_print(periph->path, "%s: failed to set read "
3713 "capacity advinfo\n", __func__);
3714 /* Use cam_error_print() to decode the status */
3715 cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
3718 bcopy(rcaplong, &softc->rcaplong,
3719 min(sizeof(softc->rcaplong), rcap_len));
3723 softc->disk->d_sectorsize = softc->params.secsize;
3724 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
3725 softc->disk->d_stripesize = softc->params.stripesize;
3726 softc->disk->d_stripeoffset = softc->params.stripeoffset;
3727 /* XXX: these are not actually "firmware" values, so they may be wrong */
3728 softc->disk->d_fwsectors = softc->params.secs_per_track;
3729 softc->disk->d_fwheads = softc->params.heads;
3730 softc->disk->d_devstat->block_size = softc->params.secsize;
3731 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
3735 dasendorderedtag(void *arg)
3737 struct da_softc *softc = arg;
3739 if (da_send_ordered) {
3740 if (!LIST_EMPTY(&softc->pending_ccbs)) {
3741 if ((softc->flags & DA_FLAG_WAS_OTAG) == 0)
3742 softc->flags |= DA_FLAG_NEED_OTAG;
3743 softc->flags &= ~DA_FLAG_WAS_OTAG;
3746 /* Queue us up again */
3747 callout_reset(&softc->sendordered_c,
3748 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
3749 dasendorderedtag, softc);
3753 * Step through all DA peripheral drivers, and if the device is still open,
3754 * sync the disk cache to physical media.
3757 dashutdown(void * arg, int howto)
3759 struct cam_periph *periph;
3760 struct da_softc *softc;
3764 CAM_PERIPH_FOREACH(periph, &dadriver) {
3765 softc = (struct da_softc *)periph->softc;
3766 if (SCHEDULER_STOPPED()) {
3767 /* If we paniced with the lock held, do not recurse. */
3768 if (!cam_periph_owned(periph) &&
3769 (softc->flags & DA_FLAG_OPEN)) {
3770 dadump(softc->disk, NULL, 0, 0, 0);
3774 cam_periph_lock(periph);
3777 * We only sync the cache if the drive is still open, and
3778 * if the drive is capable of it..
3780 if (((softc->flags & DA_FLAG_OPEN) == 0)
3781 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
3782 cam_periph_unlock(periph);
3786 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3787 scsi_synchronize_cache(&ccb->csio,
3791 /*begin_lba*/0, /* whole disk */
3796 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
3797 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR,
3798 softc->disk->d_devstat);
3800 xpt_print(periph->path, "Synchronize cache failed\n");
3801 xpt_release_ccb(ccb);
3802 cam_periph_unlock(periph);
3806 #else /* !_KERNEL */
3809 * XXX This is only left out of the kernel build to silence warnings. If,
3810 * for some reason this function is used in the kernel, the ifdefs should
3811 * be moved so it is included both in the kernel and userland.
3814 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
3815 void (*cbfcnp)(struct cam_periph *, union ccb *),
3816 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
3817 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
3820 struct scsi_format_unit *scsi_cmd;
3822 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
3823 scsi_cmd->opcode = FORMAT_UNIT;
3824 scsi_cmd->byte2 = byte2;
3825 scsi_ulto2b(ileave, scsi_cmd->interleave);
3830 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
3840 scsi_sanitize(struct ccb_scsiio *csio, u_int32_t retries,
3841 void (*cbfcnp)(struct cam_periph *, union ccb *),
3842 u_int8_t tag_action, u_int8_t byte2, u_int16_t control,
3843 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
3846 struct scsi_sanitize *scsi_cmd;
3848 scsi_cmd = (struct scsi_sanitize *)&csio->cdb_io.cdb_bytes;
3849 scsi_cmd->opcode = SANITIZE;
3850 scsi_cmd->byte2 = byte2;
3851 scsi_cmd->control = control;
3852 scsi_ulto2b(dxfer_len, scsi_cmd->length);
3857 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
3866 #endif /* _KERNEL */