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>
63 #include <cam/cam_iosched.h>
65 #include <cam/scsi/scsi_message.h>
68 #include <cam/scsi/scsi_da.h>
76 DA_STATE_PROBE_BLK_LIMITS,
83 DA_FLAG_PACK_INVALID = 0x001,
84 DA_FLAG_NEW_PACK = 0x002,
85 DA_FLAG_PACK_LOCKED = 0x004,
86 DA_FLAG_PACK_REMOVABLE = 0x008,
87 DA_FLAG_NEED_OTAG = 0x020,
88 DA_FLAG_WAS_OTAG = 0x040,
89 DA_FLAG_RETRY_UA = 0x080,
91 DA_FLAG_SCTX_INIT = 0x200,
92 DA_FLAG_CAN_RC16 = 0x400,
93 DA_FLAG_PROBED = 0x800,
94 DA_FLAG_DIRTY = 0x1000,
95 DA_FLAG_ANNOUNCED = 0x2000
100 DA_Q_NO_SYNC_CACHE = 0x01,
101 DA_Q_NO_6_BYTE = 0x02,
102 DA_Q_NO_PREVENT = 0x04,
105 DA_Q_NO_UNMAP = 0x20,
106 DA_Q_RETRY_BUSY = 0x40
109 #define DA_Q_BIT_STRING \
111 "\001NO_SYNC_CACHE" \
120 DA_CCB_PROBE_RC = 0x01,
121 DA_CCB_PROBE_RC16 = 0x02,
122 DA_CCB_PROBE_LBP = 0x03,
123 DA_CCB_PROBE_BLK_LIMITS = 0x04,
124 DA_CCB_PROBE_BDC = 0x05,
125 DA_CCB_PROBE_ATA = 0x06,
126 DA_CCB_BUFFER_IO = 0x07,
128 DA_CCB_DELETE = 0x0B,
130 DA_CCB_TYPE_MASK = 0x0F,
131 DA_CCB_RETRY_UA = 0x10
135 * Order here is important for method choice
137 * We prefer ATA_TRIM as tests run against a Sandforce 2281 SSD attached to
138 * LSI 2008 (mps) controller (FW: v12, Drv: v14) resulted 20% quicker deletes
139 * using ATA_TRIM than the corresponding UNMAP results for a real world mysql
140 * import taking 5mins.
151 DA_DELETE_MIN = DA_DELETE_ATA_TRIM,
152 DA_DELETE_MAX = DA_DELETE_ZERO
155 typedef void da_delete_func_t (struct cam_periph *periph, union ccb *ccb,
157 static da_delete_func_t da_delete_trim;
158 static da_delete_func_t da_delete_unmap;
159 static da_delete_func_t da_delete_ws;
161 static const void * da_delete_functions[] = {
171 static const char *da_delete_method_names[] =
172 { "NONE", "DISABLE", "ATA_TRIM", "UNMAP", "WS16", "WS10", "ZERO" };
173 static const char *da_delete_method_desc[] =
174 { "NONE", "DISABLED", "ATA TRIM", "UNMAP", "WRITE SAME(16) with UNMAP",
175 "WRITE SAME(10) with UNMAP", "ZERO" };
177 /* Offsets into our private area for storing information */
178 #define ccb_state ppriv_field0
179 #define ccb_bp ppriv_ptr1
184 u_int8_t secs_per_track;
185 u_int32_t secsize; /* Number of bytes/sector */
186 u_int64_t sectors; /* total number sectors */
191 #define UNMAP_RANGE_MAX 0xffffffff
192 #define UNMAP_HEAD_SIZE 8
193 #define UNMAP_RANGE_SIZE 16
194 #define UNMAP_MAX_RANGES 2048 /* Protocol Max is 4095 */
195 #define UNMAP_BUF_SIZE ((UNMAP_MAX_RANGES * UNMAP_RANGE_SIZE) + \
198 #define WS10_MAX_BLKS 0xffff
199 #define WS16_MAX_BLKS 0xffffffff
200 #define ATA_TRIM_MAX_RANGES ((UNMAP_BUF_SIZE / \
201 (ATA_DSM_RANGE_SIZE * ATA_DSM_BLK_SIZE)) * ATA_DSM_BLK_SIZE)
203 #define DA_WORK_TUR (1 << 16)
206 struct cam_iosched_softc *cam_iosched;
207 struct bio_queue_head delete_run_queue;
208 LIST_HEAD(, ccb_hdr) pending_ccbs;
209 int refcount; /* Active xpt_action() calls */
213 int minimum_cmd_size;
216 int delete_available; /* Delete methods possibly available */
218 uint32_t unmap_max_ranges;
219 uint32_t unmap_max_lba; /* Max LBAs in UNMAP req */
220 uint64_t ws_max_blks;
221 da_delete_methods delete_method_pref;
222 da_delete_methods delete_method;
223 da_delete_func_t *delete_func;
226 struct disk_params params;
229 struct task sysctl_task;
230 struct sysctl_ctx_list sysctl_ctx;
231 struct sysctl_oid *sysctl_tree;
232 struct callout sendordered_c;
234 uint8_t unmap_buf[UNMAP_BUF_SIZE];
235 struct scsi_read_capacity_data_long rcaplong;
236 struct callout mediapoll_c;
238 struct sysctl_ctx_list sysctl_stats_ctx;
239 struct sysctl_oid *sysctl_stats_tree;
246 #define dadeleteflag(softc, delete_method, enable) \
248 softc->delete_available |= (1 << delete_method); \
250 softc->delete_available &= ~(1 << delete_method); \
253 struct da_quirk_entry {
254 struct scsi_inquiry_pattern inq_pat;
258 static const char quantum[] = "QUANTUM";
259 static const char microp[] = "MICROP";
261 static struct da_quirk_entry da_quirk_table[] =
263 /* SPI, FC devices */
266 * Fujitsu M2513A MO drives.
267 * Tested devices: M2513A2 firmware versions 1200 & 1300.
268 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
269 * Reported by: W.Scholten <whs@xs4all.nl>
271 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
272 /*quirks*/ DA_Q_NO_SYNC_CACHE
276 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
277 /*quirks*/ DA_Q_NO_SYNC_CACHE
281 * This particular Fujitsu drive doesn't like the
282 * synchronize cache command.
283 * Reported by: Tom Jackson <toj@gorilla.net>
285 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
286 /*quirks*/ DA_Q_NO_SYNC_CACHE
290 * This drive doesn't like the synchronize cache command
291 * either. Reported by: Matthew Jacob <mjacob@feral.com>
292 * in NetBSD PR kern/6027, August 24, 1998.
294 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
295 /*quirks*/ DA_Q_NO_SYNC_CACHE
299 * This drive doesn't like the synchronize cache command
300 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
303 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
304 /*quirks*/ DA_Q_NO_SYNC_CACHE
308 * Doesn't like the synchronize cache command.
309 * Reported by: Blaz Zupan <blaz@gold.amis.net>
311 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
312 /*quirks*/ DA_Q_NO_SYNC_CACHE
316 * Doesn't like the synchronize cache command.
317 * Reported by: Blaz Zupan <blaz@gold.amis.net>
319 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
320 /*quirks*/ DA_Q_NO_SYNC_CACHE
324 * Doesn't like the synchronize cache command.
326 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
327 /*quirks*/ DA_Q_NO_SYNC_CACHE
331 * Doesn't like the synchronize cache command.
332 * Reported by: walter@pelissero.de
334 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
335 /*quirks*/ DA_Q_NO_SYNC_CACHE
339 * Doesn't work correctly with 6 byte reads/writes.
340 * Returns illegal request, and points to byte 9 of the
342 * Reported by: Adam McDougall <bsdx@spawnet.com>
344 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
345 /*quirks*/ DA_Q_NO_6_BYTE
349 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
350 /*quirks*/ DA_Q_NO_6_BYTE
354 * Doesn't like the synchronize cache command.
355 * Reported by: walter@pelissero.de
357 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
358 /*quirks*/ DA_Q_NO_SYNC_CACHE
362 * The CISS RAID controllers do not support SYNC_CACHE
364 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
365 /*quirks*/ DA_Q_NO_SYNC_CACHE
369 * The STEC SSDs sometimes hang on UNMAP.
371 {T_DIRECT, SIP_MEDIA_FIXED, "STEC", "*", "*"},
372 /*quirks*/ DA_Q_NO_UNMAP
376 * VMware returns BUSY status when storage has transient
377 * connectivity problems, so better wait.
379 {T_DIRECT, SIP_MEDIA_FIXED, "VMware*", "*", "*"},
380 /*quirks*/ DA_Q_RETRY_BUSY
382 /* USB mass storage devices supported by umass(4) */
385 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
388 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
389 /*quirks*/ DA_Q_NO_SYNC_CACHE
393 * Power Quotient Int. (PQI) USB flash key
396 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
397 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
401 * Creative Nomad MUVO mp3 player (USB)
404 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
405 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
409 * Jungsoft NEXDISK USB flash key
412 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
413 /*quirks*/ DA_Q_NO_SYNC_CACHE
417 * FreeDik USB Mini Data Drive
420 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
421 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
425 * Sigmatel USB Flash MP3 Player
428 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
429 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
433 * Neuros USB Digital Audio Computer
436 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
437 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
441 * SEAGRAND NP-900 MP3 Player
444 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
445 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
449 * iRiver iFP MP3 player (with UMS Firmware)
450 * PR: kern/54881, i386/63941, kern/66124
452 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
453 /*quirks*/ DA_Q_NO_SYNC_CACHE
457 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
460 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
461 /*quirks*/ DA_Q_NO_SYNC_CACHE
465 * ZICPlay USB MP3 Player with FM
468 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
469 /*quirks*/ DA_Q_NO_SYNC_CACHE
473 * TEAC USB floppy mechanisms
475 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
476 /*quirks*/ DA_Q_NO_SYNC_CACHE
480 * Kingston DataTraveler II+ USB Pen-Drive.
481 * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
483 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
484 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
492 {T_DIRECT, SIP_MEDIA_REMOVABLE, " ", "USB DISK Pro", "PMAP"},
493 /*quirks*/ DA_Q_NO_SYNC_CACHE
497 * Motorola E398 Mobile Phone (TransFlash memory card).
498 * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
501 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
502 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
506 * Qware BeatZkey! Pro
509 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
510 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
514 * Time DPA20B 1GB MP3 Player
517 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
518 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
522 * Samsung USB key 128Mb
525 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
526 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
530 * Kingston DataTraveler 2.0 USB Flash memory.
533 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
534 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
538 * Creative MUVO Slim mp3 player (USB)
541 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
542 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
546 * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
549 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
550 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
554 * SanDisk Micro Cruzer 128MB
557 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
558 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
562 * TOSHIBA TransMemory USB sticks
565 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
566 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
570 * PNY USB 3.0 Flash Drives
572 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PNY", "USB 3.0 FD*",
573 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_RC16
578 * PR: usb/75578, usb/72344, usb/65436
580 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
581 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
585 * Genesys 6-in-1 Card Reader
588 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
589 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
593 * Rekam Digital CAMERA
596 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
597 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
601 * iRiver H10 MP3 player
604 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
605 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
609 * iRiver U10 MP3 player
612 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
613 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
620 {T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
621 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
625 * EasyMP3 EM732X USB 2.0 Flash MP3 Player
628 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
629 "1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
636 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
637 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
641 * Philips USB Key Audio KEY013
644 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
645 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
652 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
653 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
660 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
661 /*quirks*/ DA_Q_NO_SYNC_CACHE
665 * I/O Magic USB flash - Giga Bank
668 {T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
669 /*quirks*/ DA_Q_NO_SYNC_CACHE
673 * JoyFly 128mb USB Flash Drive
676 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
677 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
684 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
685 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
689 * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A
692 {T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*",
693 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
697 * Samsung YP-U3 mp3-player
700 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3",
701 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
704 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
705 "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
709 * Sony Cyber-Shot DSC cameras
712 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
713 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
716 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler G3",
717 "1.00"}, /*quirks*/ DA_Q_NO_PREVENT
720 /* At least several Transcent USB sticks lie on RC16. */
721 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JetFlash", "Transcend*",
722 "*"}, /*quirks*/ DA_Q_NO_RC16
724 /* ATA/SATA devices over SAS/USB/... */
726 /* Hitachi Advanced Format (4k) drives */
727 { T_DIRECT, SIP_MEDIA_FIXED, "Hitachi", "H??????????E3*", "*" },
731 /* Samsung Advanced Format (4k) drives */
732 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD155UI*", "*" },
736 /* Samsung Advanced Format (4k) drives */
737 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD155UI*", "*" },
741 /* Samsung Advanced Format (4k) drives */
742 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD204UI*", "*" },
746 /* Samsung Advanced Format (4k) drives */
747 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD204UI*", "*" },
751 /* Seagate Barracuda Green Advanced Format (4k) drives */
752 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DL*", "*" },
756 /* Seagate Barracuda Green Advanced Format (4k) drives */
757 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DL", "*", "*" },
761 /* Seagate Barracuda Green Advanced Format (4k) drives */
762 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???DM*", "*" },
766 /* Seagate Barracuda Green Advanced Format (4k) drives */
767 { T_DIRECT, SIP_MEDIA_FIXED, "ST???DM*", "*", "*" },
771 /* Seagate Barracuda Green Advanced Format (4k) drives */
772 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DM*", "*" },
776 /* Seagate Barracuda Green Advanced Format (4k) drives */
777 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DM", "*", "*" },
781 /* Seagate Momentus Advanced Format (4k) drives */
782 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500423AS*", "*" },
786 /* Seagate Momentus Advanced Format (4k) drives */
787 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "3AS*", "*" },
791 /* Seagate Momentus Advanced Format (4k) drives */
792 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500424AS*", "*" },
796 /* Seagate Momentus Advanced Format (4k) drives */
797 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "4AS*", "*" },
801 /* Seagate Momentus Advanced Format (4k) drives */
802 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640423AS*", "*" },
806 /* Seagate Momentus Advanced Format (4k) drives */
807 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "3AS*", "*" },
811 /* Seagate Momentus Advanced Format (4k) drives */
812 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640424AS*", "*" },
816 /* Seagate Momentus Advanced Format (4k) drives */
817 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "4AS*", "*" },
821 /* Seagate Momentus Advanced Format (4k) drives */
822 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750420AS*", "*" },
826 /* Seagate Momentus Advanced Format (4k) drives */
827 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "0AS*", "*" },
831 /* Seagate Momentus Advanced Format (4k) drives */
832 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750422AS*", "*" },
836 /* Seagate Momentus Advanced Format (4k) drives */
837 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "2AS*", "*" },
841 /* Seagate Momentus Advanced Format (4k) drives */
842 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750423AS*", "*" },
846 /* Seagate Momentus Advanced Format (4k) drives */
847 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "3AS*", "*" },
851 /* Seagate Momentus Thin Advanced Format (4k) drives */
852 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???LT*", "*" },
856 /* Seagate Momentus Thin Advanced Format (4k) drives */
857 { T_DIRECT, SIP_MEDIA_FIXED, "ST???LT*", "*", "*" },
861 /* WDC Caviar Green Advanced Format (4k) drives */
862 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RS*", "*" },
866 /* WDC Caviar Green Advanced Format (4k) drives */
867 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RS*", "*" },
871 /* WDC Caviar Green Advanced Format (4k) drives */
872 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RX*", "*" },
876 /* WDC Caviar Green Advanced Format (4k) drives */
877 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RX*", "*" },
881 /* WDC Caviar Green Advanced Format (4k) drives */
882 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RS*", "*" },
886 /* WDC Caviar Green Advanced Format (4k) drives */
887 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RS*", "*" },
891 /* WDC Caviar Green Advanced Format (4k) drives */
892 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RX*", "*" },
896 /* WDC Caviar Green Advanced Format (4k) drives */
897 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RX*", "*" },
901 /* WDC Scorpio Black Advanced Format (4k) drives */
902 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PKT*", "*" },
906 /* WDC Scorpio Black Advanced Format (4k) drives */
907 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PKT*", "*" },
911 /* WDC Scorpio Black Advanced Format (4k) drives */
912 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PKT*", "*" },
916 /* WDC Scorpio Black Advanced Format (4k) drives */
917 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PKT*", "*" },
921 /* WDC Scorpio Blue Advanced Format (4k) drives */
922 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PVT*", "*" },
926 /* WDC Scorpio Blue Advanced Format (4k) drives */
927 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PVT*", "*" },
931 /* WDC Scorpio Blue Advanced Format (4k) drives */
932 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PVT*", "*" },
936 /* WDC Scorpio Blue Advanced Format (4k) drives */
937 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PVT*", "*" },
942 * Olympus FE-210 camera
944 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "FE210*",
945 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
951 {T_DIRECT, SIP_MEDIA_REMOVABLE, "LG", "UP3S",
952 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
956 * Laser MP3-2GA13 MP3 player
958 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "(HS) Flash Disk",
959 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
963 * LaCie external 250GB Hard drive des by Porsche
964 * Submitted by: Ben Stuyts <ben@altesco.nl>
967 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HM250JI", "*"},
968 /*quirks*/ DA_Q_NO_SYNC_CACHE
973 * Corsair Force 2 SSDs
974 * 4k optimised & trim only works in 4k requests + 4k aligned
976 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair CSSD-F*", "*" },
981 * Corsair Force 3 SSDs
982 * 4k optimised & trim only works in 4k requests + 4k aligned
984 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force 3*", "*" },
989 * Corsair Neutron GTX SSDs
990 * 4k optimised & trim only works in 4k requests + 4k aligned
992 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Neutron GTX*", "*" },
997 * Corsair Force GT & GS SSDs
998 * 4k optimised & trim only works in 4k requests + 4k aligned
1000 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force G*", "*" },
1006 * 4k optimised & trim only works in 4k requests + 4k aligned
1008 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "M4-CT???M4SSD2*", "*" },
1013 * Crucial RealSSD C300 SSDs
1016 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "C300-CTFDDAC???MAG*",
1017 "*" }, /*quirks*/DA_Q_4K
1021 * Intel 320 Series SSDs
1022 * 4k optimised & trim only works in 4k requests + 4k aligned
1024 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2CW*", "*" },
1029 * Intel 330 Series SSDs
1030 * 4k optimised & trim only works in 4k requests + 4k aligned
1032 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2CT*", "*" },
1037 * Intel 510 Series SSDs
1038 * 4k optimised & trim only works in 4k requests + 4k aligned
1040 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2MH*", "*" },
1045 * Intel 520 Series SSDs
1046 * 4k optimised & trim only works in 4k requests + 4k aligned
1048 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2BW*", "*" },
1053 * Intel X25-M Series SSDs
1054 * 4k optimised & trim only works in 4k requests + 4k aligned
1056 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2M*", "*" },
1061 * Kingston E100 Series SSDs
1062 * 4k optimised & trim only works in 4k requests + 4k aligned
1064 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SE100S3*", "*" },
1069 * Kingston HyperX 3k SSDs
1070 * 4k optimised & trim only works in 4k requests + 4k aligned
1072 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SH103S3*", "*" },
1077 * Marvell SSDs (entry taken from OpenSolaris)
1078 * 4k optimised & trim only works in 4k requests + 4k aligned
1080 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "MARVELL SD88SA02*", "*" },
1085 * OCZ Agility 2 SSDs
1086 * 4k optimised & trim only works in 4k requests + 4k aligned
1088 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY2*", "*" },
1093 * OCZ Agility 3 SSDs
1094 * 4k optimised & trim only works in 4k requests + 4k aligned
1096 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-AGILITY3*", "*" },
1101 * OCZ Deneva R Series SSDs
1102 * 4k optimised & trim only works in 4k requests + 4k aligned
1104 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "DENRSTE251M45*", "*" },
1109 * OCZ Vertex 2 SSDs (inc pro series)
1110 * 4k optimised & trim only works in 4k requests + 4k aligned
1112 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ?VERTEX2*", "*" },
1118 * 4k optimised & trim only works in 4k requests + 4k aligned
1120 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX3*", "*" },
1126 * 4k optimised & trim only works in 4k requests + 4k aligned
1128 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX4*", "*" },
1133 * Samsung 830 Series SSDs
1134 * 4k optimised & trim only works in 4k requests + 4k aligned
1136 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG SSD 830 Series*", "*" },
1142 * 4k optimised & trim only works in 4k requests + 4k aligned
1144 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 840*", "*" },
1150 * 4k optimised & trim only works in 4k requests + 4k aligned
1152 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 850*", "*" },
1157 * Samsung 843T Series SSDs (MZ7WD*)
1158 * Samsung PM851 Series SSDs (MZ7TE*)
1159 * Samsung PM853T Series SSDs (MZ7GE*)
1160 * Samsung SM863 Series SSDs (MZ7KM*)
1163 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG MZ7*", "*" },
1168 * SuperTalent TeraDrive CT SSDs
1169 * 4k optimised & trim only works in 4k requests + 4k aligned
1171 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "FTM??CT25H*", "*" },
1176 * XceedIOPS SATA SSDs
1179 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SG9XCS2D*", "*" },
1184 * Hama Innostor USB-Stick
1186 { T_DIRECT, SIP_MEDIA_REMOVABLE, "Innostor", "Innostor*", "*" },
1187 /*quirks*/DA_Q_NO_RC16
1191 * MX-ES USB Drive by Mach Xtreme
1193 { T_DIRECT, SIP_MEDIA_REMOVABLE, "MX", "MXUB3*", "*"},
1194 /*quirks*/DA_Q_NO_RC16
1198 static disk_strategy_t dastrategy;
1199 static dumper_t dadump;
1200 static periph_init_t dainit;
1201 static void daasync(void *callback_arg, u_int32_t code,
1202 struct cam_path *path, void *arg);
1203 static void dasysctlinit(void *context, int pending);
1204 static int dasysctlsofttimeout(SYSCTL_HANDLER_ARGS);
1205 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
1206 static int dadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
1207 static int dadeletemaxsysctl(SYSCTL_HANDLER_ARGS);
1208 static void dadeletemethodset(struct da_softc *softc,
1209 da_delete_methods delete_method);
1210 static off_t dadeletemaxsize(struct da_softc *softc,
1211 da_delete_methods delete_method);
1212 static void dadeletemethodchoose(struct da_softc *softc,
1213 da_delete_methods default_method);
1214 static void daprobedone(struct cam_periph *periph, union ccb *ccb);
1216 static periph_ctor_t daregister;
1217 static periph_dtor_t dacleanup;
1218 static periph_start_t dastart;
1219 static periph_oninv_t daoninvalidate;
1220 static void dadone(struct cam_periph *periph,
1221 union ccb *done_ccb);
1222 static int daerror(union ccb *ccb, u_int32_t cam_flags,
1223 u_int32_t sense_flags);
1224 static void daprevent(struct cam_periph *periph, int action);
1225 static void dareprobe(struct cam_periph *periph);
1226 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
1228 struct scsi_read_capacity_data_long *rcaplong,
1230 static timeout_t dasendorderedtag;
1231 static void dashutdown(void *arg, int howto);
1232 static timeout_t damediapoll;
1234 #ifndef DA_DEFAULT_POLL_PERIOD
1235 #define DA_DEFAULT_POLL_PERIOD 3
1238 #ifndef DA_DEFAULT_TIMEOUT
1239 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
1242 #ifndef DA_DEFAULT_SOFTTIMEOUT
1243 #define DA_DEFAULT_SOFTTIMEOUT 0
1246 #ifndef DA_DEFAULT_RETRY
1247 #define DA_DEFAULT_RETRY 4
1250 #ifndef DA_DEFAULT_SEND_ORDERED
1251 #define DA_DEFAULT_SEND_ORDERED 1
1254 static int da_poll_period = DA_DEFAULT_POLL_PERIOD;
1255 static int da_retry_count = DA_DEFAULT_RETRY;
1256 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
1257 static sbintime_t da_default_softtimeout = DA_DEFAULT_SOFTTIMEOUT;
1258 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
1260 static SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
1261 "CAM Direct Access Disk driver");
1262 SYSCTL_INT(_kern_cam_da, OID_AUTO, poll_period, CTLFLAG_RWTUN,
1263 &da_poll_period, 0, "Media polling period in seconds");
1264 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RWTUN,
1265 &da_retry_count, 0, "Normal I/O retry count");
1266 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RWTUN,
1267 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
1268 SYSCTL_INT(_kern_cam_da, OID_AUTO, send_ordered, CTLFLAG_RWTUN,
1269 &da_send_ordered, 0, "Send Ordered Tags");
1271 SYSCTL_PROC(_kern_cam_da, OID_AUTO, default_softtimeout,
1272 CTLTYPE_UINT | CTLFLAG_RW, NULL, 0, dasysctlsofttimeout, "I",
1273 "Soft I/O timeout (ms)");
1274 TUNABLE_INT64("kern.cam.da.default_softtimeout", &da_default_softtimeout);
1277 * DA_ORDEREDTAG_INTERVAL determines how often, relative
1278 * to the default timeout, we check to see whether an ordered
1279 * tagged transaction is appropriate to prevent simple tag
1280 * starvation. Since we'd like to ensure that there is at least
1281 * 1/2 of the timeout length left for a starved transaction to
1282 * complete after we've sent an ordered tag, we must poll at least
1283 * four times in every timeout period. This takes care of the worst
1284 * case where a starved transaction starts during an interval that
1285 * meets the requirement "don't send an ordered tag" test so it takes
1286 * us two intervals to determine that a tag must be sent.
1288 #ifndef DA_ORDEREDTAG_INTERVAL
1289 #define DA_ORDEREDTAG_INTERVAL 4
1292 static struct periph_driver dadriver =
1295 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
1298 PERIPHDRIVER_DECLARE(da, dadriver);
1300 static MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
1303 daopen(struct disk *dp)
1305 struct cam_periph *periph;
1306 struct da_softc *softc;
1309 periph = (struct cam_periph *)dp->d_drv1;
1310 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1314 cam_periph_lock(periph);
1315 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
1316 cam_periph_unlock(periph);
1317 cam_periph_release(periph);
1321 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1324 softc = (struct da_softc *)periph->softc;
1327 /* Wait for the disk size update. */
1328 error = cam_periph_sleep(periph, &softc->disk->d_mediasize, PRIBIO,
1331 xpt_print(periph->path, "unable to retrieve capacity data\n");
1333 if (periph->flags & CAM_PERIPH_INVALID)
1336 if (error == 0 && (softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1337 (softc->quirks & DA_Q_NO_PREVENT) == 0)
1338 daprevent(periph, PR_PREVENT);
1341 softc->flags &= ~DA_FLAG_PACK_INVALID;
1342 softc->flags |= DA_FLAG_OPEN;
1345 cam_periph_unhold(periph);
1346 cam_periph_unlock(periph);
1349 cam_periph_release(periph);
1355 daclose(struct disk *dp)
1357 struct cam_periph *periph;
1358 struct da_softc *softc;
1362 periph = (struct cam_periph *)dp->d_drv1;
1363 softc = (struct da_softc *)periph->softc;
1364 cam_periph_lock(periph);
1365 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1368 if (cam_periph_hold(periph, PRIBIO) == 0) {
1370 /* Flush disk cache. */
1371 if ((softc->flags & DA_FLAG_DIRTY) != 0 &&
1372 (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0 &&
1373 (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
1374 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1375 scsi_synchronize_cache(&ccb->csio, /*retries*/1,
1376 /*cbfcnp*/dadone, MSG_SIMPLE_Q_TAG,
1377 /*begin_lba*/0, /*lb_count*/0, SSD_FULL_SIZE,
1379 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
1380 /*sense_flags*/SF_RETRY_UA | SF_QUIET_IR,
1381 softc->disk->d_devstat);
1383 softc->flags &= ~DA_FLAG_DIRTY;
1384 xpt_release_ccb(ccb);
1387 /* Allow medium removal. */
1388 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1389 (softc->quirks & DA_Q_NO_PREVENT) == 0)
1390 daprevent(periph, PR_ALLOW);
1392 cam_periph_unhold(periph);
1396 * If we've got removeable media, mark the blocksize as
1397 * unavailable, since it could change when new media is
1400 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0)
1401 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1403 softc->flags &= ~DA_FLAG_OPEN;
1404 while (softc->refcount != 0)
1405 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "daclose", 1);
1406 cam_periph_unlock(periph);
1407 cam_periph_release(periph);
1412 daschedule(struct cam_periph *periph)
1414 struct da_softc *softc = (struct da_softc *)periph->softc;
1416 if (softc->state != DA_STATE_NORMAL)
1419 cam_iosched_schedule(softc->cam_iosched, periph);
1423 * Actually translate the requested transfer into one the physical driver
1424 * can understand. The transfer is described by a buf and will include
1425 * only one physical transfer.
1428 dastrategy(struct bio *bp)
1430 struct cam_periph *periph;
1431 struct da_softc *softc;
1433 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1434 softc = (struct da_softc *)periph->softc;
1436 cam_periph_lock(periph);
1439 * If the device has been made invalid, error out
1441 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1442 cam_periph_unlock(periph);
1443 biofinish(bp, NULL, ENXIO);
1447 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
1450 * Place it in the queue of disk activities for this disk
1452 cam_iosched_queue_work(softc->cam_iosched, bp);
1455 * Schedule ourselves for performing the work.
1458 cam_periph_unlock(periph);
1464 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1466 struct cam_periph *periph;
1467 struct da_softc *softc;
1469 struct ccb_scsiio csio;
1474 periph = dp->d_drv1;
1475 softc = (struct da_softc *)periph->softc;
1476 cam_periph_lock(periph);
1477 secsize = softc->params.secsize;
1479 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1480 cam_periph_unlock(periph);
1485 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1486 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1487 scsi_read_write(&csio,
1491 /*read*/SCSI_RW_WRITE,
1493 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1496 /*data_ptr*/(u_int8_t *) virtual,
1497 /*dxfer_len*/length,
1498 /*sense_len*/SSD_FULL_SIZE,
1499 da_default_timeout * 1000);
1500 xpt_polled_action((union ccb *)&csio);
1502 error = cam_periph_error((union ccb *)&csio,
1503 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1504 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1505 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1506 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1508 printf("Aborting dump due to I/O error.\n");
1509 cam_periph_unlock(periph);
1514 * Sync the disk cache contents to the physical media.
1516 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1518 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1519 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1520 scsi_synchronize_cache(&csio,
1524 /*begin_lba*/0,/* Cover the whole disk */
1528 xpt_polled_action((union ccb *)&csio);
1530 error = cam_periph_error((union ccb *)&csio,
1531 0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL);
1532 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1533 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1534 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1536 xpt_print(periph->path, "Synchronize cache failed\n");
1538 cam_periph_unlock(periph);
1543 dagetattr(struct bio *bp)
1546 struct cam_periph *periph;
1548 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1549 cam_periph_lock(periph);
1550 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1552 cam_periph_unlock(periph);
1554 bp->bio_completed = bp->bio_length;
1564 * Install a global async callback. This callback will
1565 * receive async callbacks like "new device found".
1567 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1569 if (status != CAM_REQ_CMP) {
1570 printf("da: Failed to attach master async callback "
1571 "due to status 0x%x!\n", status);
1572 } else if (da_send_ordered) {
1574 /* Register our shutdown event handler */
1575 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1576 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1577 printf("dainit: shutdown event registration failed!\n");
1582 * Callback from GEOM, called when it has finished cleaning up its
1586 dadiskgonecb(struct disk *dp)
1588 struct cam_periph *periph;
1590 periph = (struct cam_periph *)dp->d_drv1;
1591 cam_periph_release(periph);
1595 daoninvalidate(struct cam_periph *periph)
1597 struct da_softc *softc;
1599 softc = (struct da_softc *)periph->softc;
1602 * De-register any async callbacks.
1604 xpt_register_async(0, daasync, periph, periph->path);
1606 softc->flags |= DA_FLAG_PACK_INVALID;
1608 softc->invalidations++;
1612 * Return all queued I/O with ENXIO.
1613 * XXX Handle any transactions queued to the card
1614 * with XPT_ABORT_CCB.
1616 cam_iosched_flush(softc->cam_iosched, NULL, ENXIO);
1619 * Tell GEOM that we've gone away, we'll get a callback when it is
1620 * done cleaning up its resources.
1622 disk_gone(softc->disk);
1626 dacleanup(struct cam_periph *periph)
1628 struct da_softc *softc;
1630 softc = (struct da_softc *)periph->softc;
1632 cam_periph_unlock(periph);
1634 cam_iosched_fini(softc->cam_iosched);
1637 * If we can't free the sysctl tree, oh well...
1639 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0) {
1641 if (sysctl_ctx_free(&softc->sysctl_stats_ctx) != 0)
1642 xpt_print(periph->path,
1643 "can't remove sysctl stats context\n");
1645 if (sysctl_ctx_free(&softc->sysctl_ctx) != 0)
1646 xpt_print(periph->path,
1647 "can't remove sysctl context\n");
1650 callout_drain(&softc->mediapoll_c);
1651 disk_destroy(softc->disk);
1652 callout_drain(&softc->sendordered_c);
1653 free(softc, M_DEVBUF);
1654 cam_periph_lock(periph);
1658 daasync(void *callback_arg, u_int32_t code,
1659 struct cam_path *path, void *arg)
1661 struct cam_periph *periph;
1662 struct da_softc *softc;
1664 periph = (struct cam_periph *)callback_arg;
1666 case AC_FOUND_DEVICE:
1668 struct ccb_getdev *cgd;
1671 cgd = (struct ccb_getdev *)arg;
1675 if (cgd->protocol != PROTO_SCSI)
1677 if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED)
1679 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1680 && SID_TYPE(&cgd->inq_data) != T_RBC
1681 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1685 * Allocate a peripheral instance for
1686 * this device and start the probe
1689 status = cam_periph_alloc(daregister, daoninvalidate,
1691 "da", CAM_PERIPH_BIO,
1693 AC_FOUND_DEVICE, cgd);
1695 if (status != CAM_REQ_CMP
1696 && status != CAM_REQ_INPROG)
1697 printf("daasync: Unable to attach to new device "
1698 "due to status 0x%x\n", status);
1701 case AC_ADVINFO_CHANGED:
1705 buftype = (uintptr_t)arg;
1706 if (buftype == CDAI_TYPE_PHYS_PATH) {
1707 struct da_softc *softc;
1709 softc = periph->softc;
1710 disk_attr_changed(softc->disk, "GEOM::physpath",
1715 case AC_UNIT_ATTENTION:
1718 int error_code, sense_key, asc, ascq;
1720 softc = (struct da_softc *)periph->softc;
1721 ccb = (union ccb *)arg;
1724 * Handle all UNIT ATTENTIONs except our own,
1725 * as they will be handled by daerror().
1727 if (xpt_path_periph(ccb->ccb_h.path) != periph &&
1728 scsi_extract_sense_ccb(ccb,
1729 &error_code, &sense_key, &asc, &ascq)) {
1730 if (asc == 0x2A && ascq == 0x09) {
1731 xpt_print(ccb->ccb_h.path,
1732 "Capacity data has changed\n");
1733 softc->flags &= ~DA_FLAG_PROBED;
1735 } else if (asc == 0x28 && ascq == 0x00) {
1736 softc->flags &= ~DA_FLAG_PROBED;
1737 disk_media_changed(softc->disk, M_NOWAIT);
1738 } else if (asc == 0x3F && ascq == 0x03) {
1739 xpt_print(ccb->ccb_h.path,
1740 "INQUIRY data has changed\n");
1741 softc->flags &= ~DA_FLAG_PROBED;
1745 cam_periph_async(periph, code, path, arg);
1749 softc = (struct da_softc *)periph->softc;
1750 if (!cam_iosched_has_work_flags(softc->cam_iosched, DA_WORK_TUR)) {
1751 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
1752 cam_iosched_set_work_flags(softc->cam_iosched, DA_WORK_TUR);
1760 struct ccb_hdr *ccbh;
1762 softc = (struct da_softc *)periph->softc;
1764 * Don't fail on the expected unit attention
1767 softc->flags |= DA_FLAG_RETRY_UA;
1768 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1769 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1775 cam_periph_async(periph, code, path, arg);
1779 dasysctlinit(void *context, int pending)
1781 struct cam_periph *periph;
1782 struct da_softc *softc;
1783 char tmpstr[80], tmpstr2[80];
1784 struct ccb_trans_settings cts;
1786 periph = (struct cam_periph *)context;
1788 * periph was held for us when this task was enqueued
1790 if (periph->flags & CAM_PERIPH_INVALID) {
1791 cam_periph_release(periph);
1795 softc = (struct da_softc *)periph->softc;
1796 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1797 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1799 sysctl_ctx_init(&softc->sysctl_ctx);
1800 softc->flags |= DA_FLAG_SCTX_INIT;
1801 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1802 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1803 CTLFLAG_RD, 0, tmpstr);
1804 if (softc->sysctl_tree == NULL) {
1805 printf("dasysctlinit: unable to allocate sysctl tree\n");
1806 cam_periph_release(periph);
1811 * Now register the sysctl handler, so the user can change the value on
1814 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1815 OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RWTUN,
1816 softc, 0, dadeletemethodsysctl, "A",
1817 "BIO_DELETE execution method");
1818 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1819 OID_AUTO, "delete_max", CTLTYPE_U64 | CTLFLAG_RW,
1820 softc, 0, dadeletemaxsysctl, "Q",
1821 "Maximum BIO_DELETE size");
1822 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1823 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1824 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1825 "Minimum CDB size");
1827 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1828 SYSCTL_CHILDREN(softc->sysctl_tree),
1832 &softc->error_inject,
1834 "error_inject leaf");
1836 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1837 SYSCTL_CHILDREN(softc->sysctl_tree),
1843 "Unmapped I/O leaf");
1845 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1846 SYSCTL_CHILDREN(softc->sysctl_tree),
1855 * Add some addressing info.
1857 memset(&cts, 0, sizeof (cts));
1858 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1859 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1860 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1861 cam_periph_lock(periph);
1862 xpt_action((union ccb *)&cts);
1863 cam_periph_unlock(periph);
1864 if (cts.ccb_h.status != CAM_REQ_CMP) {
1865 cam_periph_release(periph);
1868 if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
1869 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1870 if (fc->valid & CTS_FC_VALID_WWPN) {
1871 softc->wwpn = fc->wwpn;
1872 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1873 SYSCTL_CHILDREN(softc->sysctl_tree),
1874 OID_AUTO, "wwpn", CTLFLAG_RD,
1875 &softc->wwpn, "World Wide Port Name");
1881 * Now add some useful stats.
1882 * XXX These should live in cam_periph and be common to all periphs
1884 softc->sysctl_stats_tree = SYSCTL_ADD_NODE(&softc->sysctl_stats_ctx,
1885 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, "stats",
1886 CTLFLAG_RD, 0, "Statistics");
1887 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
1888 SYSCTL_CHILDREN(softc->sysctl_stats_tree),
1894 "Transport errors reported by the SIM");
1895 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
1896 SYSCTL_CHILDREN(softc->sysctl_stats_tree),
1902 "Device timeouts reported by the SIM");
1903 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
1904 SYSCTL_CHILDREN(softc->sysctl_stats_tree),
1906 "pack_invalidations",
1908 &softc->invalidations,
1910 "Device pack invalidations");
1913 cam_iosched_sysctl_init(softc->cam_iosched, &softc->sysctl_ctx,
1914 softc->sysctl_tree);
1916 cam_periph_release(periph);
1920 dadeletemaxsysctl(SYSCTL_HANDLER_ARGS)
1924 struct da_softc *softc;
1926 softc = (struct da_softc *)arg1;
1928 value = softc->disk->d_delmaxsize;
1929 error = sysctl_handle_64(oidp, &value, 0, req);
1930 if ((error != 0) || (req->newptr == NULL))
1933 /* only accept values smaller than the calculated value */
1934 if (value > dadeletemaxsize(softc, softc->delete_method)) {
1937 softc->disk->d_delmaxsize = value;
1943 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1947 value = *(int *)arg1;
1949 error = sysctl_handle_int(oidp, &value, 0, req);
1952 || (req->newptr == NULL))
1956 * Acceptable values here are 6, 10, 12 or 16.
1960 else if ((value > 6)
1963 else if ((value > 10)
1966 else if (value > 12)
1969 *(int *)arg1 = value;
1975 dasysctlsofttimeout(SYSCTL_HANDLER_ARGS)
1980 value = da_default_softtimeout / SBT_1MS;
1982 error = sysctl_handle_int(oidp, (int *)&value, 0, req);
1983 if ((error != 0) || (req->newptr == NULL))
1986 /* XXX Should clip this to a reasonable level */
1987 if (value > da_default_timeout * 1000)
1990 da_default_softtimeout = value * SBT_1MS;
1995 dadeletemethodset(struct da_softc *softc, da_delete_methods delete_method)
1998 softc->delete_method = delete_method;
1999 softc->disk->d_delmaxsize = dadeletemaxsize(softc, delete_method);
2000 softc->delete_func = da_delete_functions[delete_method];
2002 if (softc->delete_method > DA_DELETE_DISABLE)
2003 softc->disk->d_flags |= DISKFLAG_CANDELETE;
2005 softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
2009 dadeletemaxsize(struct da_softc *softc, da_delete_methods delete_method)
2013 switch(delete_method) {
2014 case DA_DELETE_UNMAP:
2015 sectors = (off_t)softc->unmap_max_lba;
2017 case DA_DELETE_ATA_TRIM:
2018 sectors = (off_t)ATA_DSM_RANGE_MAX * softc->trim_max_ranges;
2020 case DA_DELETE_WS16:
2021 sectors = omin(softc->ws_max_blks, WS16_MAX_BLKS);
2023 case DA_DELETE_ZERO:
2024 case DA_DELETE_WS10:
2025 sectors = omin(softc->ws_max_blks, WS10_MAX_BLKS);
2031 return (off_t)softc->params.secsize *
2032 omin(sectors, softc->params.sectors);
2036 daprobedone(struct cam_periph *periph, union ccb *ccb)
2038 struct da_softc *softc;
2040 softc = (struct da_softc *)periph->softc;
2042 dadeletemethodchoose(softc, DA_DELETE_NONE);
2044 if (bootverbose && (softc->flags & DA_FLAG_ANNOUNCED) == 0) {
2048 snprintf(buf, sizeof(buf), "Delete methods: <");
2050 for (i = 0; i <= DA_DELETE_MAX; i++) {
2051 if ((softc->delete_available & (1 << i)) == 0 &&
2052 i != softc->delete_method)
2055 strlcat(buf, ",", sizeof(buf));
2056 strlcat(buf, da_delete_method_names[i],
2058 if (i == softc->delete_method)
2059 strlcat(buf, "(*)", sizeof(buf));
2062 strlcat(buf, ">", sizeof(buf));
2063 printf("%s%d: %s\n", periph->periph_name,
2064 periph->unit_number, buf);
2068 * Since our peripheral may be invalidated by an error
2069 * above or an external event, we must release our CCB
2070 * before releasing the probe lock on the peripheral.
2071 * The peripheral will only go away once the last lock
2072 * is removed, and we need it around for the CCB release
2075 xpt_release_ccb(ccb);
2076 softc->state = DA_STATE_NORMAL;
2077 softc->flags |= DA_FLAG_PROBED;
2079 wakeup(&softc->disk->d_mediasize);
2080 if ((softc->flags & DA_FLAG_ANNOUNCED) == 0) {
2081 softc->flags |= DA_FLAG_ANNOUNCED;
2082 cam_periph_unhold(periph);
2084 cam_periph_release_locked(periph);
2088 dadeletemethodchoose(struct da_softc *softc, da_delete_methods default_method)
2092 /* If available, prefer the method requested by user. */
2093 i = softc->delete_method_pref;
2094 methods = softc->delete_available | (1 << DA_DELETE_DISABLE);
2095 if (methods & (1 << i)) {
2096 dadeletemethodset(softc, i);
2100 /* Use the pre-defined order to choose the best performing delete. */
2101 for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
2102 if (i == DA_DELETE_ZERO)
2104 if (softc->delete_available & (1 << i)) {
2105 dadeletemethodset(softc, i);
2110 /* Fallback to default. */
2111 dadeletemethodset(softc, default_method);
2115 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
2119 struct da_softc *softc;
2120 int i, error, methods, value;
2122 softc = (struct da_softc *)arg1;
2124 value = softc->delete_method;
2125 if (value < 0 || value > DA_DELETE_MAX)
2128 p = da_delete_method_names[value];
2129 strncpy(buf, p, sizeof(buf));
2130 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2131 if (error != 0 || req->newptr == NULL)
2133 methods = softc->delete_available | (1 << DA_DELETE_DISABLE);
2134 for (i = 0; i <= DA_DELETE_MAX; i++) {
2135 if (strcmp(buf, da_delete_method_names[i]) == 0)
2138 if (i > DA_DELETE_MAX)
2140 softc->delete_method_pref = i;
2141 dadeletemethodchoose(softc, DA_DELETE_NONE);
2146 daregister(struct cam_periph *periph, void *arg)
2148 struct da_softc *softc;
2149 struct ccb_pathinq cpi;
2150 struct ccb_getdev *cgd;
2154 cgd = (struct ccb_getdev *)arg;
2156 printf("daregister: no getdev CCB, can't register device\n");
2157 return(CAM_REQ_CMP_ERR);
2160 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
2163 if (softc == NULL) {
2164 printf("daregister: Unable to probe new device. "
2165 "Unable to allocate softc\n");
2166 return(CAM_REQ_CMP_ERR);
2169 if (cam_iosched_init(&softc->cam_iosched, periph) != 0) {
2170 printf("daregister: Unable to probe new device. "
2171 "Unable to allocate iosched memory\n");
2172 return(CAM_REQ_CMP_ERR);
2175 LIST_INIT(&softc->pending_ccbs);
2176 softc->state = DA_STATE_PROBE_RC;
2177 bioq_init(&softc->delete_run_queue);
2178 if (SID_IS_REMOVABLE(&cgd->inq_data))
2179 softc->flags |= DA_FLAG_PACK_REMOVABLE;
2180 softc->unmap_max_ranges = UNMAP_MAX_RANGES;
2181 softc->unmap_max_lba = UNMAP_RANGE_MAX;
2182 softc->ws_max_blks = WS16_MAX_BLKS;
2183 softc->trim_max_ranges = ATA_TRIM_MAX_RANGES;
2184 softc->rotating = 1;
2186 periph->softc = softc;
2189 * See if this device has any quirks.
2191 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
2192 (caddr_t)da_quirk_table,
2193 nitems(da_quirk_table),
2194 sizeof(*da_quirk_table), scsi_inquiry_match);
2197 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
2199 softc->quirks = DA_Q_NONE;
2201 /* Check if the SIM does not want 6 byte commands */
2202 bzero(&cpi, sizeof(cpi));
2203 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2204 cpi.ccb_h.func_code = XPT_PATH_INQ;
2205 xpt_action((union ccb *)&cpi);
2206 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
2207 softc->quirks |= DA_Q_NO_6_BYTE;
2209 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
2212 * Take an exclusive refcount on the periph while dastart is called
2213 * to finish the probe. The reference will be dropped in dadone at
2216 (void)cam_periph_hold(periph, PRIBIO);
2219 * Schedule a periodic event to occasionally send an
2220 * ordered tag to a device.
2222 callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0);
2223 callout_reset(&softc->sendordered_c,
2224 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2225 dasendorderedtag, softc);
2227 cam_periph_unlock(periph);
2229 * RBC devices don't have to support READ(6), only READ(10).
2231 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
2232 softc->minimum_cmd_size = 10;
2234 softc->minimum_cmd_size = 6;
2237 * Load the user's default, if any.
2239 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
2240 periph->unit_number);
2241 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
2244 * 6, 10, 12 and 16 are the currently permissible values.
2246 if (softc->minimum_cmd_size < 6)
2247 softc->minimum_cmd_size = 6;
2248 else if ((softc->minimum_cmd_size > 6)
2249 && (softc->minimum_cmd_size <= 10))
2250 softc->minimum_cmd_size = 10;
2251 else if ((softc->minimum_cmd_size > 10)
2252 && (softc->minimum_cmd_size <= 12))
2253 softc->minimum_cmd_size = 12;
2254 else if (softc->minimum_cmd_size > 12)
2255 softc->minimum_cmd_size = 16;
2257 /* Predict whether device may support READ CAPACITY(16). */
2258 if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3 &&
2259 (softc->quirks & DA_Q_NO_RC16) == 0) {
2260 softc->flags |= DA_FLAG_CAN_RC16;
2261 softc->state = DA_STATE_PROBE_RC16;
2265 * Register this media as a disk.
2267 softc->disk = disk_alloc();
2268 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
2269 periph->unit_number, 0,
2270 DEVSTAT_BS_UNAVAILABLE,
2271 SID_TYPE(&cgd->inq_data) |
2272 XPORT_DEVSTAT_TYPE(cpi.transport),
2273 DEVSTAT_PRIORITY_DISK);
2274 softc->disk->d_open = daopen;
2275 softc->disk->d_close = daclose;
2276 softc->disk->d_strategy = dastrategy;
2277 softc->disk->d_dump = dadump;
2278 softc->disk->d_getattr = dagetattr;
2279 softc->disk->d_gone = dadiskgonecb;
2280 softc->disk->d_name = "da";
2281 softc->disk->d_drv1 = periph;
2283 softc->maxio = DFLTPHYS; /* traditional default */
2284 else if (cpi.maxio > MAXPHYS)
2285 softc->maxio = MAXPHYS; /* for safety */
2287 softc->maxio = cpi.maxio;
2288 softc->disk->d_maxsize = softc->maxio;
2289 softc->disk->d_unit = periph->unit_number;
2290 softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION;
2291 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
2292 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
2293 if ((cpi.hba_misc & PIM_UNMAPPED) != 0) {
2294 softc->unmappedio = 1;
2295 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
2296 xpt_print(periph->path, "UNMAPPED\n");
2298 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
2299 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
2300 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
2301 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
2302 cgd->inq_data.product, sizeof(cgd->inq_data.product),
2303 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
2304 softc->disk->d_hba_vendor = cpi.hba_vendor;
2305 softc->disk->d_hba_device = cpi.hba_device;
2306 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
2307 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
2310 * Acquire a reference to the periph before we register with GEOM.
2311 * We'll release this reference once GEOM calls us back (via
2312 * dadiskgonecb()) telling us that our provider has been freed.
2314 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
2315 xpt_print(periph->path, "%s: lost periph during "
2316 "registration!\n", __func__);
2317 cam_periph_lock(periph);
2318 return (CAM_REQ_CMP_ERR);
2321 disk_create(softc->disk, DISK_VERSION);
2322 cam_periph_lock(periph);
2325 * Add async callbacks for events of interest.
2326 * I don't bother checking if this fails as,
2327 * in most cases, the system will function just
2328 * fine without them and the only alternative
2329 * would be to not attach the device on failure.
2331 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
2332 AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION,
2333 daasync, periph, periph->path);
2336 * Emit an attribute changed notification just in case
2337 * physical path information arrived before our async
2338 * event handler was registered, but after anyone attaching
2339 * to our disk device polled it.
2341 disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
2344 * Schedule a periodic media polling events.
2346 callout_init_mtx(&softc->mediapoll_c, cam_periph_mtx(periph), 0);
2347 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
2348 (cgd->inq_flags & SID_AEN) == 0 &&
2349 da_poll_period != 0)
2350 callout_reset(&softc->mediapoll_c, da_poll_period * hz,
2351 damediapoll, periph);
2353 xpt_schedule(periph, CAM_PRIORITY_DEV);
2355 return(CAM_REQ_CMP);
2359 dastart(struct cam_periph *periph, union ccb *start_ccb)
2361 struct da_softc *softc;
2363 softc = (struct da_softc *)periph->softc;
2365 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
2368 switch (softc->state) {
2369 case DA_STATE_NORMAL:
2375 bp = cam_iosched_next_bio(softc->cam_iosched);
2377 if (cam_iosched_has_work_flags(softc->cam_iosched, DA_WORK_TUR)) {
2378 cam_iosched_clr_work_flags(softc->cam_iosched, DA_WORK_TUR);
2379 scsi_test_unit_ready(&start_ccb->csio,
2380 /*retries*/ da_retry_count,
2384 da_default_timeout * 1000);
2385 start_ccb->ccb_h.ccb_bp = NULL;
2386 start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
2387 xpt_action(start_ccb);
2389 xpt_release_ccb(start_ccb);
2393 if (bp->bio_cmd == BIO_DELETE) {
2394 if (softc->delete_func != NULL) {
2395 softc->delete_func(periph, start_ccb, bp);
2398 /* Not sure this is possible, but failsafe by lying and saying "sure, done." */
2399 biofinish(bp, NULL, 0);
2404 if (cam_iosched_has_work_flags(softc->cam_iosched, DA_WORK_TUR)) {
2405 cam_iosched_clr_work_flags(softc->cam_iosched, DA_WORK_TUR);
2406 cam_periph_release_locked(periph); /* XXX is this still valid? I think so but unverified */
2409 if ((bp->bio_flags & BIO_ORDERED) != 0 ||
2410 (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
2411 softc->flags &= ~DA_FLAG_NEED_OTAG;
2412 softc->flags |= DA_FLAG_WAS_OTAG;
2413 tag_code = MSG_ORDERED_Q_TAG;
2415 tag_code = MSG_SIMPLE_Q_TAG;
2418 switch (bp->bio_cmd) {
2425 if (bp->bio_cmd == BIO_WRITE) {
2426 softc->flags |= DA_FLAG_DIRTY;
2427 rw_op = SCSI_RW_WRITE;
2429 rw_op = SCSI_RW_READ;
2432 data_ptr = bp->bio_data;
2433 if ((bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0) {
2434 rw_op |= SCSI_RW_BIO;
2438 scsi_read_write(&start_ccb->csio,
2439 /*retries*/da_retry_count,
2441 /*tag_action*/tag_code,
2444 softc->minimum_cmd_size,
2445 /*lba*/bp->bio_pblkno,
2446 /*block_count*/bp->bio_bcount /
2447 softc->params.secsize,
2449 /*dxfer_len*/ bp->bio_bcount,
2450 /*sense_len*/SSD_FULL_SIZE,
2451 da_default_timeout * 1000);
2456 * BIO_FLUSH doesn't currently communicate
2457 * range data, so we synchronize the cache
2458 * over the whole disk. We also force
2459 * ordered tag semantics the flush applies
2460 * to all previously queued I/O.
2462 scsi_synchronize_cache(&start_ccb->csio,
2469 da_default_timeout*1000);
2472 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
2473 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
2474 start_ccb->ccb_h.softtimeout = sbttotv(da_default_softtimeout);
2477 LIST_INSERT_HEAD(&softc->pending_ccbs,
2478 &start_ccb->ccb_h, periph_links.le);
2480 /* We expect a unit attention from this device */
2481 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
2482 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
2483 softc->flags &= ~DA_FLAG_RETRY_UA;
2486 start_ccb->ccb_h.ccb_bp = bp;
2488 cam_periph_unlock(periph);
2489 xpt_action(start_ccb);
2490 cam_periph_lock(periph);
2493 /* May have more work to do, so ensure we stay scheduled */
2497 case DA_STATE_PROBE_RC:
2499 struct scsi_read_capacity_data *rcap;
2501 rcap = (struct scsi_read_capacity_data *)
2502 malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
2504 printf("dastart: Couldn't malloc read_capacity data\n");
2505 /* da_free_periph??? */
2508 scsi_read_capacity(&start_ccb->csio,
2509 /*retries*/da_retry_count,
2515 start_ccb->ccb_h.ccb_bp = NULL;
2516 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC;
2517 xpt_action(start_ccb);
2520 case DA_STATE_PROBE_RC16:
2522 struct scsi_read_capacity_data_long *rcaplong;
2524 rcaplong = (struct scsi_read_capacity_data_long *)
2525 malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
2526 if (rcaplong == NULL) {
2527 printf("dastart: Couldn't malloc read_capacity data\n");
2528 /* da_free_periph??? */
2531 scsi_read_capacity_16(&start_ccb->csio,
2532 /*retries*/ da_retry_count,
2534 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2538 /*rcap_buf*/ (uint8_t *)rcaplong,
2539 /*rcap_buf_len*/ sizeof(*rcaplong),
2540 /*sense_len*/ SSD_FULL_SIZE,
2541 /*timeout*/ da_default_timeout * 1000);
2542 start_ccb->ccb_h.ccb_bp = NULL;
2543 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC16;
2544 xpt_action(start_ccb);
2547 case DA_STATE_PROBE_LBP:
2549 struct scsi_vpd_logical_block_prov *lbp;
2551 if (!scsi_vpd_supported_page(periph, SVPD_LBP)) {
2553 * If we get here we don't support any SBC-3 delete
2554 * methods with UNMAP as the Logical Block Provisioning
2555 * VPD page support is required for devices which
2556 * support it according to T10/1799-D Revision 31
2557 * however older revisions of the spec don't mandate
2558 * this so we currently don't remove these methods
2559 * from the available set.
2561 softc->state = DA_STATE_PROBE_BLK_LIMITS;
2565 lbp = (struct scsi_vpd_logical_block_prov *)
2566 malloc(sizeof(*lbp), M_SCSIDA, M_NOWAIT|M_ZERO);
2569 printf("dastart: Couldn't malloc lbp data\n");
2570 /* da_free_periph??? */
2574 scsi_inquiry(&start_ccb->csio,
2575 /*retries*/da_retry_count,
2577 /*tag_action*/MSG_SIMPLE_Q_TAG,
2578 /*inq_buf*/(u_int8_t *)lbp,
2579 /*inq_len*/sizeof(*lbp),
2581 /*page_code*/SVPD_LBP,
2582 /*sense_len*/SSD_MIN_SIZE,
2583 /*timeout*/da_default_timeout * 1000);
2584 start_ccb->ccb_h.ccb_bp = NULL;
2585 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_LBP;
2586 xpt_action(start_ccb);
2589 case DA_STATE_PROBE_BLK_LIMITS:
2591 struct scsi_vpd_block_limits *block_limits;
2593 if (!scsi_vpd_supported_page(periph, SVPD_BLOCK_LIMITS)) {
2594 /* Not supported skip to next probe */
2595 softc->state = DA_STATE_PROBE_BDC;
2599 block_limits = (struct scsi_vpd_block_limits *)
2600 malloc(sizeof(*block_limits), M_SCSIDA, M_NOWAIT|M_ZERO);
2602 if (block_limits == NULL) {
2603 printf("dastart: Couldn't malloc block_limits data\n");
2604 /* da_free_periph??? */
2608 scsi_inquiry(&start_ccb->csio,
2609 /*retries*/da_retry_count,
2611 /*tag_action*/MSG_SIMPLE_Q_TAG,
2612 /*inq_buf*/(u_int8_t *)block_limits,
2613 /*inq_len*/sizeof(*block_limits),
2615 /*page_code*/SVPD_BLOCK_LIMITS,
2616 /*sense_len*/SSD_MIN_SIZE,
2617 /*timeout*/da_default_timeout * 1000);
2618 start_ccb->ccb_h.ccb_bp = NULL;
2619 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BLK_LIMITS;
2620 xpt_action(start_ccb);
2623 case DA_STATE_PROBE_BDC:
2625 struct scsi_vpd_block_characteristics *bdc;
2627 if (!scsi_vpd_supported_page(periph, SVPD_BDC)) {
2628 softc->state = DA_STATE_PROBE_ATA;
2632 bdc = (struct scsi_vpd_block_characteristics *)
2633 malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
2636 printf("dastart: Couldn't malloc bdc data\n");
2637 /* da_free_periph??? */
2641 scsi_inquiry(&start_ccb->csio,
2642 /*retries*/da_retry_count,
2644 /*tag_action*/MSG_SIMPLE_Q_TAG,
2645 /*inq_buf*/(u_int8_t *)bdc,
2646 /*inq_len*/sizeof(*bdc),
2648 /*page_code*/SVPD_BDC,
2649 /*sense_len*/SSD_MIN_SIZE,
2650 /*timeout*/da_default_timeout * 1000);
2651 start_ccb->ccb_h.ccb_bp = NULL;
2652 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BDC;
2653 xpt_action(start_ccb);
2656 case DA_STATE_PROBE_ATA:
2658 struct ata_params *ata_params;
2660 if (!scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
2661 daprobedone(periph, start_ccb);
2665 ata_params = (struct ata_params*)
2666 malloc(sizeof(*ata_params), M_SCSIDA, M_NOWAIT|M_ZERO);
2668 if (ata_params == NULL) {
2669 printf("dastart: Couldn't malloc ata_params data\n");
2670 /* da_free_periph??? */
2674 scsi_ata_identify(&start_ccb->csio,
2675 /*retries*/da_retry_count,
2677 /*tag_action*/MSG_SIMPLE_Q_TAG,
2678 /*data_ptr*/(u_int8_t *)ata_params,
2679 /*dxfer_len*/sizeof(*ata_params),
2680 /*sense_len*/SSD_FULL_SIZE,
2681 /*timeout*/da_default_timeout * 1000);
2682 start_ccb->ccb_h.ccb_bp = NULL;
2683 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA;
2684 xpt_action(start_ccb);
2691 * In each of the methods below, while its the caller's
2692 * responsibility to ensure the request will fit into a
2693 * single device request, we might have changed the delete
2694 * method due to the device incorrectly advertising either
2695 * its supported methods or limits.
2697 * To prevent this causing further issues we validate the
2698 * against the methods limits, and warn which would
2699 * otherwise be unnecessary.
2702 da_delete_unmap(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2704 struct da_softc *softc = (struct da_softc *)periph->softc;;
2706 uint8_t *buf = softc->unmap_buf;
2707 uint64_t lba, lastlba = (uint64_t)-1;
2708 uint64_t totalcount = 0;
2710 uint32_t lastcount = 0, c;
2711 uint32_t off, ranges = 0;
2714 * Currently this doesn't take the UNMAP
2715 * Granularity and Granularity Alignment
2716 * fields into account.
2718 * This could result in both unoptimal unmap
2719 * requests as as well as UNMAP calls unmapping
2720 * fewer LBA's than requested.
2723 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2727 * Note: ada and da are different in how they store the
2728 * pending bp's in a trim. ada stores all of them in the
2729 * trim_req.bps. da stores all but the first one in the
2730 * delete_run_queue. ada then completes all the bps in
2731 * its adadone() loop. da completes all the bps in the
2732 * delete_run_queue in dadone, and relies on the biodone
2733 * after to complete. This should be reconciled since there's
2734 * no real reason to do it differently. XXX
2737 bioq_insert_tail(&softc->delete_run_queue, bp1);
2738 lba = bp1->bio_pblkno;
2739 count = bp1->bio_bcount / softc->params.secsize;
2741 /* Try to extend the previous range. */
2742 if (lba == lastlba) {
2743 c = omin(count, UNMAP_RANGE_MAX - lastcount);
2745 off = ((ranges - 1) * UNMAP_RANGE_SIZE) +
2747 scsi_ulto4b(lastcount, &buf[off + 8]);
2754 c = omin(count, UNMAP_RANGE_MAX);
2755 if (totalcount + c > softc->unmap_max_lba ||
2756 ranges >= softc->unmap_max_ranges) {
2757 xpt_print(periph->path,
2758 "%s issuing short delete %ld > %ld"
2760 da_delete_method_desc[softc->delete_method],
2761 totalcount + c, softc->unmap_max_lba,
2762 ranges, softc->unmap_max_ranges);
2765 off = (ranges * UNMAP_RANGE_SIZE) + UNMAP_HEAD_SIZE;
2766 scsi_u64to8b(lba, &buf[off + 0]);
2767 scsi_ulto4b(c, &buf[off + 8]);
2775 bp1 = cam_iosched_next_trim(softc->cam_iosched);
2778 if (ranges >= softc->unmap_max_ranges ||
2779 totalcount + bp1->bio_bcount /
2780 softc->params.secsize > softc->unmap_max_lba) {
2781 cam_iosched_put_back_trim(softc->cam_iosched, bp1);
2785 scsi_ulto2b(ranges * 16 + 6, &buf[0]);
2786 scsi_ulto2b(ranges * 16, &buf[2]);
2788 scsi_unmap(&ccb->csio,
2789 /*retries*/da_retry_count,
2791 /*tag_action*/MSG_SIMPLE_Q_TAG,
2794 /*dxfer_len*/ ranges * 16 + 8,
2795 /*sense_len*/SSD_FULL_SIZE,
2796 da_default_timeout * 1000);
2797 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2798 ccb->ccb_h.flags |= CAM_UNLOCKED;
2799 cam_iosched_submit_trim(softc->cam_iosched);
2803 da_delete_trim(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2805 struct da_softc *softc = (struct da_softc *)periph->softc;
2807 uint8_t *buf = softc->unmap_buf;
2808 uint64_t lastlba = (uint64_t)-1;
2811 uint32_t lastcount = 0, c, requestcount;
2812 int ranges = 0, off, block_count;
2814 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2817 if (bp1 != bp)//XXX imp XXX
2818 bioq_insert_tail(&softc->delete_run_queue, bp1);
2819 lba = bp1->bio_pblkno;
2820 count = bp1->bio_bcount / softc->params.secsize;
2821 requestcount = count;
2823 /* Try to extend the previous range. */
2824 if (lba == lastlba) {
2825 c = omin(count, ATA_DSM_RANGE_MAX - lastcount);
2827 off = (ranges - 1) * 8;
2828 buf[off + 6] = lastcount & 0xff;
2829 buf[off + 7] = (lastcount >> 8) & 0xff;
2835 c = omin(count, ATA_DSM_RANGE_MAX);
2838 buf[off + 0] = lba & 0xff;
2839 buf[off + 1] = (lba >> 8) & 0xff;
2840 buf[off + 2] = (lba >> 16) & 0xff;
2841 buf[off + 3] = (lba >> 24) & 0xff;
2842 buf[off + 4] = (lba >> 32) & 0xff;
2843 buf[off + 5] = (lba >> 40) & 0xff;
2844 buf[off + 6] = c & 0xff;
2845 buf[off + 7] = (c >> 8) & 0xff;
2850 if (count != 0 && ranges == softc->trim_max_ranges) {
2851 xpt_print(periph->path,
2852 "%s issuing short delete %ld > %ld\n",
2853 da_delete_method_desc[softc->delete_method],
2855 (softc->trim_max_ranges - ranges) *
2861 bp1 = cam_iosched_next_trim(softc->cam_iosched);
2864 if (bp1->bio_bcount / softc->params.secsize >
2865 (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX) {
2866 cam_iosched_put_back_trim(softc->cam_iosched, bp1);
2871 block_count = howmany(ranges, ATA_DSM_BLK_RANGES);
2872 scsi_ata_trim(&ccb->csio,
2873 /*retries*/da_retry_count,
2875 /*tag_action*/MSG_SIMPLE_Q_TAG,
2878 /*dxfer_len*/block_count * ATA_DSM_BLK_SIZE,
2879 /*sense_len*/SSD_FULL_SIZE,
2880 da_default_timeout * 1000);
2881 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2882 ccb->ccb_h.flags |= CAM_UNLOCKED;
2883 cam_iosched_submit_trim(softc->cam_iosched);
2887 * We calculate ws_max_blks here based off d_delmaxsize instead
2888 * of using softc->ws_max_blks as it is absolute max for the
2889 * device not the protocol max which may well be lower.
2892 da_delete_ws(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2894 struct da_softc *softc;
2896 uint64_t ws_max_blks;
2898 uint64_t count; /* forward compat with WS32 */
2900 softc = (struct da_softc *)periph->softc;
2901 ws_max_blks = softc->disk->d_delmaxsize / softc->params.secsize;
2902 lba = bp->bio_pblkno;
2906 if (bp1 != bp)//XXX imp XXX
2907 bioq_insert_tail(&softc->delete_run_queue, bp1);
2908 count += bp1->bio_bcount / softc->params.secsize;
2909 if (count > ws_max_blks) {
2910 xpt_print(periph->path,
2911 "%s issuing short delete %ld > %ld\n",
2912 da_delete_method_desc[softc->delete_method],
2913 count, ws_max_blks);
2914 count = omin(count, ws_max_blks);
2917 bp1 = cam_iosched_next_trim(softc->cam_iosched);
2920 if (lba + count != bp1->bio_pblkno ||
2921 count + bp1->bio_bcount /
2922 softc->params.secsize > ws_max_blks) {
2923 cam_iosched_put_back_trim(softc->cam_iosched, bp1);
2928 scsi_write_same(&ccb->csio,
2929 /*retries*/da_retry_count,
2931 /*tag_action*/MSG_SIMPLE_Q_TAG,
2932 /*byte2*/softc->delete_method ==
2933 DA_DELETE_ZERO ? 0 : SWS_UNMAP,
2934 softc->delete_method == DA_DELETE_WS16 ? 16 : 10,
2936 /*block_count*/count,
2937 /*data_ptr*/ __DECONST(void *, zero_region),
2938 /*dxfer_len*/ softc->params.secsize,
2939 /*sense_len*/SSD_FULL_SIZE,
2940 da_default_timeout * 1000);
2941 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2942 ccb->ccb_h.flags |= CAM_UNLOCKED;
2943 cam_iosched_submit_trim(softc->cam_iosched);
2947 cmd6workaround(union ccb *ccb)
2949 struct scsi_rw_6 cmd6;
2950 struct scsi_rw_10 *cmd10;
2951 struct da_softc *softc;
2956 cdb = ccb->csio.cdb_io.cdb_bytes;
2957 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
2959 if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
2960 da_delete_methods old_method = softc->delete_method;
2963 * Typically there are two reasons for failure here
2964 * 1. Delete method was detected as supported but isn't
2965 * 2. Delete failed due to invalid params e.g. too big
2967 * While we will attempt to choose an alternative delete method
2968 * this may result in short deletes if the existing delete
2969 * requests from geom are big for the new method choosen.
2971 * This method assumes that the error which triggered this
2972 * will not retry the io otherwise a panic will occur
2974 dadeleteflag(softc, old_method, 0);
2975 dadeletemethodchoose(softc, DA_DELETE_DISABLE);
2976 if (softc->delete_method == DA_DELETE_DISABLE)
2977 xpt_print(ccb->ccb_h.path,
2978 "%s failed, disabling BIO_DELETE\n",
2979 da_delete_method_desc[old_method]);
2981 xpt_print(ccb->ccb_h.path,
2982 "%s failed, switching to %s BIO_DELETE\n",
2983 da_delete_method_desc[old_method],
2984 da_delete_method_desc[softc->delete_method]);
2986 while ((bp = bioq_takefirst(&softc->delete_run_queue)) != NULL)
2987 cam_iosched_queue_work(softc->cam_iosched, bp);
2988 cam_iosched_queue_work(softc->cam_iosched,
2989 (struct bio *)ccb->ccb_h.ccb_bp);
2990 ccb->ccb_h.ccb_bp = NULL;
2994 /* Detect unsupported PREVENT ALLOW MEDIUM REMOVAL. */
2995 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
2996 (*cdb == PREVENT_ALLOW) &&
2997 (softc->quirks & DA_Q_NO_PREVENT) == 0) {
2999 xpt_print(ccb->ccb_h.path,
3000 "PREVENT ALLOW MEDIUM REMOVAL not supported.\n");
3001 softc->quirks |= DA_Q_NO_PREVENT;
3005 /* Detect unsupported SYNCHRONIZE CACHE(10). */
3006 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
3007 (*cdb == SYNCHRONIZE_CACHE) &&
3008 (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
3010 xpt_print(ccb->ccb_h.path,
3011 "SYNCHRONIZE CACHE(10) not supported.\n");
3012 softc->quirks |= DA_Q_NO_SYNC_CACHE;
3013 softc->disk->d_flags &= ~DISKFLAG_CANFLUSHCACHE;
3017 /* Translation only possible if CDB is an array and cmd is R/W6 */
3018 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
3019 (*cdb != READ_6 && *cdb != WRITE_6))
3022 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
3023 "increasing minimum_cmd_size to 10.\n");
3024 softc->minimum_cmd_size = 10;
3026 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
3027 cmd10 = (struct scsi_rw_10 *)cdb;
3028 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
3030 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
3031 cmd10->reserved = 0;
3032 scsi_ulto2b(cmd6.length, cmd10->length);
3033 cmd10->control = cmd6.control;
3034 ccb->csio.cdb_len = sizeof(*cmd10);
3036 /* Requeue request, unfreezing queue if necessary */
3037 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
3038 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3041 cam_release_devq(ccb->ccb_h.path,
3045 /*getcount_only*/0);
3051 dadone(struct cam_periph *periph, union ccb *done_ccb)
3053 struct da_softc *softc;
3054 struct ccb_scsiio *csio;
3058 softc = (struct da_softc *)periph->softc;
3059 priority = done_ccb->ccb_h.pinfo.priority;
3061 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
3063 csio = &done_ccb->csio;
3064 state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
3066 case DA_CCB_BUFFER_IO:
3069 struct bio *bp, *bp1;
3071 cam_periph_lock(periph);
3072 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
3073 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3077 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
3082 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
3083 if (error == ERESTART) {
3085 * A retry was scheduled, so
3088 cam_periph_unlock(periph);
3091 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
3096 * return all queued I/O with EIO, so that
3097 * the client can retry these I/Os in the
3098 * proper order should it attempt to recover.
3103 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
3105 * Catastrophic error. Mark our pack as
3109 * XXX See if this is really a media
3112 xpt_print(periph->path,
3113 "Invalidating pack\n");
3114 softc->flags |= DA_FLAG_PACK_INVALID;
3116 softc->invalidations++;
3118 queued_error = ENXIO;
3120 cam_iosched_flush(softc->cam_iosched, NULL,
3123 bp->bio_error = error;
3124 bp->bio_resid = bp->bio_bcount;
3125 bp->bio_flags |= BIO_ERROR;
3127 } else if (bp != NULL) {
3128 if (state == DA_CCB_DELETE)
3131 bp->bio_resid = csio->resid;
3133 if (bp->bio_resid != 0)
3134 bp->bio_flags |= BIO_ERROR;
3136 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3137 cam_release_devq(done_ccb->ccb_h.path,
3141 /*getcount_only*/0);
3142 } else if (bp != NULL) {
3143 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3144 panic("REQ_CMP with QFRZN");
3145 if (state == DA_CCB_DELETE)
3148 bp->bio_resid = csio->resid;
3149 if (csio->resid > 0)
3150 bp->bio_flags |= BIO_ERROR;
3151 if (softc->error_inject != 0) {
3152 bp->bio_error = softc->error_inject;
3153 bp->bio_resid = bp->bio_bcount;
3154 bp->bio_flags |= BIO_ERROR;
3155 softc->error_inject = 0;
3159 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
3160 if (LIST_EMPTY(&softc->pending_ccbs))
3161 softc->flags |= DA_FLAG_WAS_OTAG;
3163 cam_iosched_bio_complete(softc->cam_iosched, bp, done_ccb);
3164 xpt_release_ccb(done_ccb);
3165 if (state == DA_CCB_DELETE) {
3166 TAILQ_HEAD(, bio) queue;
3169 TAILQ_CONCAT(&queue, &softc->delete_run_queue.queue, bio_queue);
3170 softc->delete_run_queue.insert_point = NULL;
3172 * Normally, the xpt_release_ccb() above would make sure
3173 * that when we have more work to do, that work would
3174 * get kicked off. However, we specifically keep
3175 * delete_running set to 0 before the call above to
3176 * allow other I/O to progress when many BIO_DELETE
3177 * requests are pushed down. We set delete_running to 0
3178 * and call daschedule again so that we don't stall if
3179 * there are no other I/Os pending apart from BIO_DELETEs.
3181 cam_iosched_trim_done(softc->cam_iosched);
3183 cam_periph_unlock(periph);
3184 while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
3185 TAILQ_REMOVE(&queue, bp1, bio_queue);
3186 bp1->bio_error = bp->bio_error;
3187 if (bp->bio_flags & BIO_ERROR) {
3188 bp1->bio_flags |= BIO_ERROR;
3189 bp1->bio_resid = bp1->bio_bcount;
3196 cam_periph_unlock(periph);
3202 case DA_CCB_PROBE_RC:
3203 case DA_CCB_PROBE_RC16:
3205 struct scsi_read_capacity_data *rdcap;
3206 struct scsi_read_capacity_data_long *rcaplong;
3207 char announce_buf[80];
3213 if (state == DA_CCB_PROBE_RC)
3214 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
3216 rcaplong = (struct scsi_read_capacity_data_long *)
3219 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3220 struct disk_params *dp;
3221 uint32_t block_size;
3223 u_int lalba; /* Lowest aligned LBA. */
3225 if (state == DA_CCB_PROBE_RC) {
3226 block_size = scsi_4btoul(rdcap->length);
3227 maxsector = scsi_4btoul(rdcap->addr);
3231 * According to SBC-2, if the standard 10
3232 * byte READ CAPACITY command returns 2^32,
3233 * we should issue the 16 byte version of
3234 * the command, since the device in question
3235 * has more sectors than can be represented
3236 * with the short version of the command.
3238 if (maxsector == 0xffffffff) {
3239 free(rdcap, M_SCSIDA);
3240 xpt_release_ccb(done_ccb);
3241 softc->state = DA_STATE_PROBE_RC16;
3242 xpt_schedule(periph, priority);
3246 block_size = scsi_4btoul(rcaplong->length);
3247 maxsector = scsi_8btou64(rcaplong->addr);
3248 lalba = scsi_2btoul(rcaplong->lalba_lbp);
3252 * Because GEOM code just will panic us if we
3253 * give them an 'illegal' value we'll avoid that
3256 if (block_size == 0) {
3261 if (block_size >= MAXPHYS) {
3262 xpt_print(periph->path,
3263 "unsupportable block size %ju\n",
3264 (uintmax_t) block_size);
3265 announce_buf[0] = '\0';
3266 cam_periph_invalidate(periph);
3269 * We pass rcaplong into dasetgeom(),
3270 * because it will only use it if it is
3273 dasetgeom(periph, block_size, maxsector,
3274 rcaplong, sizeof(*rcaplong));
3275 lbp = (lalba & SRC16_LBPME_A);
3276 dp = &softc->params;
3277 snprintf(announce_buf, sizeof(announce_buf),
3278 "%juMB (%ju %u byte sectors)",
3279 ((uintmax_t)dp->secsize * dp->sectors) /
3281 (uintmax_t)dp->sectors, dp->secsize);
3286 announce_buf[0] = '\0';
3289 * Retry any UNIT ATTENTION type errors. They
3290 * are expected at boot.
3292 error = daerror(done_ccb, CAM_RETRY_SELTO,
3293 SF_RETRY_UA|SF_NO_PRINT);
3294 if (error == ERESTART) {
3296 * A retry was scheuled, so
3300 } else if (error != 0) {
3302 int sense_key, error_code;
3305 struct ccb_getdev cgd;
3307 /* Don't wedge this device's queue */
3308 status = done_ccb->ccb_h.status;
3309 if ((status & CAM_DEV_QFRZN) != 0)
3310 cam_release_devq(done_ccb->ccb_h.path,
3314 /*getcount_only*/0);
3317 xpt_setup_ccb(&cgd.ccb_h,
3318 done_ccb->ccb_h.path,
3319 CAM_PRIORITY_NORMAL);
3320 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
3321 xpt_action((union ccb *)&cgd);
3323 if (scsi_extract_sense_ccb(done_ccb,
3324 &error_code, &sense_key, &asc, &ascq))
3330 * If we tried READ CAPACITY(16) and failed,
3331 * fallback to READ CAPACITY(10).
3333 if ((state == DA_CCB_PROBE_RC16) &&
3334 (softc->flags & DA_FLAG_CAN_RC16) &&
3335 (((csio->ccb_h.status & CAM_STATUS_MASK) ==
3338 (error_code == SSD_CURRENT_ERROR) &&
3339 (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
3340 softc->flags &= ~DA_FLAG_CAN_RC16;
3341 free(rdcap, M_SCSIDA);
3342 xpt_release_ccb(done_ccb);
3343 softc->state = DA_STATE_PROBE_RC;
3344 xpt_schedule(periph, priority);
3349 * Attach to anything that claims to be a
3350 * direct access or optical disk device,
3351 * as long as it doesn't return a "Logical
3352 * unit not supported" (0x25) error.
3354 if ((have_sense) && (asc != 0x25)
3355 && (error_code == SSD_CURRENT_ERROR)) {
3356 const char *sense_key_desc;
3357 const char *asc_desc;
3359 dasetgeom(periph, 512, -1, NULL, 0);
3360 scsi_sense_desc(sense_key, asc, ascq,
3364 snprintf(announce_buf,
3365 sizeof(announce_buf),
3366 "Attempt to query device "
3367 "size failed: %s, %s",
3375 xpt_print(periph->path,
3376 "got CAM status %#x\n",
3377 done_ccb->ccb_h.status);
3380 xpt_print(periph->path, "fatal error, "
3381 "failed to attach to device\n");
3384 * Free up resources.
3386 cam_periph_invalidate(periph);
3390 free(csio->data_ptr, M_SCSIDA);
3391 if (announce_buf[0] != '\0' &&
3392 ((softc->flags & DA_FLAG_ANNOUNCED) == 0)) {
3394 * Create our sysctl variables, now that we know
3395 * we have successfully attached.
3397 /* increase the refcount */
3398 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3399 taskqueue_enqueue(taskqueue_thread,
3400 &softc->sysctl_task);
3401 xpt_announce_periph(periph, announce_buf);
3402 xpt_announce_quirks(periph, softc->quirks,
3405 xpt_print(periph->path, "fatal error, "
3406 "could not acquire reference count\n");
3410 /* We already probed the device. */
3411 if (softc->flags & DA_FLAG_PROBED) {
3412 daprobedone(periph, done_ccb);
3416 /* Ensure re-probe doesn't see old delete. */
3417 softc->delete_available = 0;
3418 dadeleteflag(softc, DA_DELETE_ZERO, 1);
3419 if (lbp && (softc->quirks & DA_Q_NO_UNMAP) == 0) {
3421 * Based on older SBC-3 spec revisions
3422 * any of the UNMAP methods "may" be
3423 * available via LBP given this flag so
3424 * we flag all of them as availble and
3425 * then remove those which further
3426 * probes confirm aren't available
3429 * We could also check readcap(16) p_type
3430 * flag to exclude one or more invalid
3431 * write same (X) types here
3433 dadeleteflag(softc, DA_DELETE_WS16, 1);
3434 dadeleteflag(softc, DA_DELETE_WS10, 1);
3435 dadeleteflag(softc, DA_DELETE_UNMAP, 1);
3437 xpt_release_ccb(done_ccb);
3438 softc->state = DA_STATE_PROBE_LBP;
3439 xpt_schedule(periph, priority);
3443 xpt_release_ccb(done_ccb);
3444 softc->state = DA_STATE_PROBE_BDC;
3445 xpt_schedule(periph, priority);
3448 case DA_CCB_PROBE_LBP:
3450 struct scsi_vpd_logical_block_prov *lbp;
3452 lbp = (struct scsi_vpd_logical_block_prov *)csio->data_ptr;
3454 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3456 * T10/1799-D Revision 31 states at least one of these
3457 * must be supported but we don't currently enforce this.
3459 dadeleteflag(softc, DA_DELETE_WS16,
3460 (lbp->flags & SVPD_LBP_WS16));
3461 dadeleteflag(softc, DA_DELETE_WS10,
3462 (lbp->flags & SVPD_LBP_WS10));
3463 dadeleteflag(softc, DA_DELETE_UNMAP,
3464 (lbp->flags & SVPD_LBP_UNMAP));
3467 error = daerror(done_ccb, CAM_RETRY_SELTO,
3468 SF_RETRY_UA|SF_NO_PRINT);
3469 if (error == ERESTART)
3471 else if (error != 0) {
3472 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3473 /* Don't wedge this device's queue */
3474 cam_release_devq(done_ccb->ccb_h.path,
3478 /*getcount_only*/0);
3482 * Failure indicates we don't support any SBC-3
3483 * delete methods with UNMAP
3488 free(lbp, M_SCSIDA);
3489 xpt_release_ccb(done_ccb);
3490 softc->state = DA_STATE_PROBE_BLK_LIMITS;
3491 xpt_schedule(periph, priority);
3494 case DA_CCB_PROBE_BLK_LIMITS:
3496 struct scsi_vpd_block_limits *block_limits;
3498 block_limits = (struct scsi_vpd_block_limits *)csio->data_ptr;
3500 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3501 uint32_t max_txfer_len = scsi_4btoul(
3502 block_limits->max_txfer_len);
3503 uint32_t max_unmap_lba_cnt = scsi_4btoul(
3504 block_limits->max_unmap_lba_cnt);
3505 uint32_t max_unmap_blk_cnt = scsi_4btoul(
3506 block_limits->max_unmap_blk_cnt);
3507 uint64_t ws_max_blks = scsi_8btou64(
3508 block_limits->max_write_same_length);
3510 if (max_txfer_len != 0) {
3511 softc->disk->d_maxsize = MIN(softc->maxio,
3512 (off_t)max_txfer_len * softc->params.secsize);
3516 * We should already support UNMAP but we check lba
3517 * and block count to be sure
3519 if (max_unmap_lba_cnt != 0x00L &&
3520 max_unmap_blk_cnt != 0x00L) {
3521 softc->unmap_max_lba = max_unmap_lba_cnt;
3522 softc->unmap_max_ranges = min(max_unmap_blk_cnt,
3526 * Unexpected UNMAP limits which means the
3527 * device doesn't actually support UNMAP
3529 dadeleteflag(softc, DA_DELETE_UNMAP, 0);
3532 if (ws_max_blks != 0x00L)
3533 softc->ws_max_blks = ws_max_blks;
3536 error = daerror(done_ccb, CAM_RETRY_SELTO,
3537 SF_RETRY_UA|SF_NO_PRINT);
3538 if (error == ERESTART)
3540 else if (error != 0) {
3541 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3542 /* Don't wedge this device's queue */
3543 cam_release_devq(done_ccb->ccb_h.path,
3547 /*getcount_only*/0);
3551 * Failure here doesn't mean UNMAP is not
3552 * supported as this is an optional page.
3554 softc->unmap_max_lba = 1;
3555 softc->unmap_max_ranges = 1;
3559 free(block_limits, M_SCSIDA);
3560 xpt_release_ccb(done_ccb);
3561 softc->state = DA_STATE_PROBE_BDC;
3562 xpt_schedule(periph, priority);
3565 case DA_CCB_PROBE_BDC:
3567 struct scsi_vpd_block_characteristics *bdc;
3569 bdc = (struct scsi_vpd_block_characteristics *)csio->data_ptr;
3571 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3573 * Disable queue sorting for non-rotational media
3576 u_int16_t old_rate = softc->disk->d_rotation_rate;
3578 softc->disk->d_rotation_rate =
3579 scsi_2btoul(bdc->medium_rotation_rate);
3580 if (softc->disk->d_rotation_rate ==
3581 SVPD_BDC_RATE_NON_ROTATING) {
3582 cam_iosched_set_sort_queue(softc->cam_iosched, 0);
3583 softc->rotating = 0;
3585 if (softc->disk->d_rotation_rate != old_rate) {
3586 disk_attr_changed(softc->disk,
3587 "GEOM::rotation_rate", M_NOWAIT);
3591 error = daerror(done_ccb, CAM_RETRY_SELTO,
3592 SF_RETRY_UA|SF_NO_PRINT);
3593 if (error == ERESTART)
3595 else if (error != 0) {
3596 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3597 /* Don't wedge this device's queue */
3598 cam_release_devq(done_ccb->ccb_h.path,
3602 /*getcount_only*/0);
3607 free(bdc, M_SCSIDA);
3608 xpt_release_ccb(done_ccb);
3609 softc->state = DA_STATE_PROBE_ATA;
3610 xpt_schedule(periph, priority);
3613 case DA_CCB_PROBE_ATA:
3616 struct ata_params *ata_params;
3619 ata_params = (struct ata_params *)csio->data_ptr;
3620 ptr = (uint16_t *)ata_params;
3622 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3625 for (i = 0; i < sizeof(*ata_params) / 2; i++)
3626 ptr[i] = le16toh(ptr[i]);
3627 if (ata_params->support_dsm & ATA_SUPPORT_DSM_TRIM &&
3628 (softc->quirks & DA_Q_NO_UNMAP) == 0) {
3629 dadeleteflag(softc, DA_DELETE_ATA_TRIM, 1);
3630 if (ata_params->max_dsm_blocks != 0)
3631 softc->trim_max_ranges = min(
3632 softc->trim_max_ranges,
3633 ata_params->max_dsm_blocks *
3634 ATA_DSM_BLK_RANGES);
3637 * Disable queue sorting for non-rotational media
3640 old_rate = softc->disk->d_rotation_rate;
3641 softc->disk->d_rotation_rate =
3642 ata_params->media_rotation_rate;
3643 if (softc->disk->d_rotation_rate ==
3644 ATA_RATE_NON_ROTATING) {
3645 cam_iosched_set_sort_queue(softc->cam_iosched, 0);
3646 softc->rotating = 0;
3648 if (softc->disk->d_rotation_rate != old_rate) {
3649 disk_attr_changed(softc->disk,
3650 "GEOM::rotation_rate", M_NOWAIT);
3654 error = daerror(done_ccb, CAM_RETRY_SELTO,
3655 SF_RETRY_UA|SF_NO_PRINT);
3656 if (error == ERESTART)
3658 else if (error != 0) {
3659 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3660 /* Don't wedge this device's queue */
3661 cam_release_devq(done_ccb->ccb_h.path,
3665 /*getcount_only*/0);
3670 free(ata_params, M_SCSIDA);
3671 daprobedone(periph, done_ccb);
3675 /* No-op. We're polling */
3679 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3681 if (daerror(done_ccb, CAM_RETRY_SELTO,
3682 SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
3685 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3686 cam_release_devq(done_ccb->ccb_h.path,
3690 /*getcount_only*/0);
3692 xpt_release_ccb(done_ccb);
3693 cam_periph_release_locked(periph);
3699 xpt_release_ccb(done_ccb);
3703 dareprobe(struct cam_periph *periph)
3705 struct da_softc *softc;
3708 softc = (struct da_softc *)periph->softc;
3710 /* Probe in progress; don't interfere. */
3711 if (softc->state != DA_STATE_NORMAL)
3714 status = cam_periph_acquire(periph);
3715 KASSERT(status == CAM_REQ_CMP,
3716 ("dareprobe: cam_periph_acquire failed"));
3718 if (softc->flags & DA_FLAG_CAN_RC16)
3719 softc->state = DA_STATE_PROBE_RC16;
3721 softc->state = DA_STATE_PROBE_RC;
3723 xpt_schedule(periph, CAM_PRIORITY_DEV);
3727 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3729 struct da_softc *softc;
3730 struct cam_periph *periph;
3731 int error, error_code, sense_key, asc, ascq;
3733 periph = xpt_path_periph(ccb->ccb_h.path);
3734 softc = (struct da_softc *)periph->softc;
3737 * Automatically detect devices that do not support
3738 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
3741 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3742 error = cmd6workaround(ccb);
3743 } else if (scsi_extract_sense_ccb(ccb,
3744 &error_code, &sense_key, &asc, &ascq)) {
3745 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3746 error = cmd6workaround(ccb);
3748 * If the target replied with CAPACITY DATA HAS CHANGED UA,
3749 * query the capacity and notify upper layers.
3751 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3752 asc == 0x2A && ascq == 0x09) {
3753 xpt_print(periph->path, "Capacity data has changed\n");
3754 softc->flags &= ~DA_FLAG_PROBED;
3756 sense_flags |= SF_NO_PRINT;
3757 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3758 asc == 0x28 && ascq == 0x00) {
3759 softc->flags &= ~DA_FLAG_PROBED;
3760 disk_media_changed(softc->disk, M_NOWAIT);
3761 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3762 asc == 0x3F && ascq == 0x03) {
3763 xpt_print(periph->path, "INQUIRY data has changed\n");
3764 softc->flags &= ~DA_FLAG_PROBED;
3766 sense_flags |= SF_NO_PRINT;
3767 } else if (sense_key == SSD_KEY_NOT_READY &&
3768 asc == 0x3a && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
3769 softc->flags |= DA_FLAG_PACK_INVALID;
3770 disk_media_gone(softc->disk, M_NOWAIT);
3773 if (error == ERESTART)
3777 switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
3778 case CAM_CMD_TIMEOUT:
3781 case CAM_REQ_ABORTED:
3782 case CAM_REQ_CMP_ERR:
3783 case CAM_REQ_TERMIO:
3784 case CAM_UNREC_HBA_ERROR:
3785 case CAM_DATA_RUN_ERR:
3795 * Until we have a better way of doing pack validation,
3796 * don't treat UAs as errors.
3798 sense_flags |= SF_RETRY_UA;
3800 if (softc->quirks & DA_Q_RETRY_BUSY)
3801 sense_flags |= SF_RETRY_BUSY;
3802 return(cam_periph_error(ccb, cam_flags, sense_flags,
3803 &softc->saved_ccb));
3807 damediapoll(void *arg)
3809 struct cam_periph *periph = arg;
3810 struct da_softc *softc = periph->softc;
3812 if (!cam_iosched_has_work_flags(softc->cam_iosched, DA_WORK_TUR) &&
3813 LIST_EMPTY(&softc->pending_ccbs)) {
3814 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3815 cam_iosched_set_work_flags(softc->cam_iosched, DA_WORK_TUR);
3819 /* Queue us up again */
3820 if (da_poll_period != 0)
3821 callout_schedule(&softc->mediapoll_c, da_poll_period * hz);
3825 daprevent(struct cam_periph *periph, int action)
3827 struct da_softc *softc;
3831 softc = (struct da_softc *)periph->softc;
3833 if (((action == PR_ALLOW)
3834 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
3835 || ((action == PR_PREVENT)
3836 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
3840 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3842 scsi_prevent(&ccb->csio,
3850 error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
3851 SF_RETRY_UA | SF_NO_PRINT, softc->disk->d_devstat);
3854 if (action == PR_ALLOW)
3855 softc->flags &= ~DA_FLAG_PACK_LOCKED;
3857 softc->flags |= DA_FLAG_PACK_LOCKED;
3860 xpt_release_ccb(ccb);
3864 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
3865 struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
3867 struct ccb_calc_geometry ccg;
3868 struct da_softc *softc;
3869 struct disk_params *dp;
3870 u_int lbppbe, lalba;
3873 softc = (struct da_softc *)periph->softc;
3875 dp = &softc->params;
3876 dp->secsize = block_len;
3877 dp->sectors = maxsector + 1;
3878 if (rcaplong != NULL) {
3879 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
3880 lalba = scsi_2btoul(rcaplong->lalba_lbp);
3881 lalba &= SRC16_LALBA_A;
3888 dp->stripesize = block_len << lbppbe;
3889 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
3891 } else if (softc->quirks & DA_Q_4K) {
3892 dp->stripesize = 4096;
3893 dp->stripeoffset = 0;
3896 dp->stripeoffset = 0;
3899 * Have the controller provide us with a geometry
3900 * for this disk. The only time the geometry
3901 * matters is when we boot and the controller
3902 * is the only one knowledgeable enough to come
3903 * up with something that will make this a bootable
3906 xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3907 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
3908 ccg.block_size = dp->secsize;
3909 ccg.volume_size = dp->sectors;
3911 ccg.secs_per_track = 0;
3913 xpt_action((union ccb*)&ccg);
3914 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3916 * We don't know what went wrong here- but just pick
3917 * a geometry so we don't have nasty things like divide
3921 dp->secs_per_track = 255;
3922 dp->cylinders = dp->sectors / (255 * 255);
3923 if (dp->cylinders == 0) {
3927 dp->heads = ccg.heads;
3928 dp->secs_per_track = ccg.secs_per_track;
3929 dp->cylinders = ccg.cylinders;
3933 * If the user supplied a read capacity buffer, and if it is
3934 * different than the previous buffer, update the data in the EDT.
3935 * If it's the same, we don't bother. This avoids sending an
3936 * update every time someone opens this device.
3938 if ((rcaplong != NULL)
3939 && (bcmp(rcaplong, &softc->rcaplong,
3940 min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
3941 struct ccb_dev_advinfo cdai;
3943 xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3944 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
3945 cdai.buftype = CDAI_TYPE_RCAPLONG;
3946 cdai.flags = CDAI_FLAG_STORE;
3947 cdai.bufsiz = rcap_len;
3948 cdai.buf = (uint8_t *)rcaplong;
3949 xpt_action((union ccb *)&cdai);
3950 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
3951 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
3952 if (cdai.ccb_h.status != CAM_REQ_CMP) {
3953 xpt_print(periph->path, "%s: failed to set read "
3954 "capacity advinfo\n", __func__);
3955 /* Use cam_error_print() to decode the status */
3956 cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
3959 bcopy(rcaplong, &softc->rcaplong,
3960 min(sizeof(softc->rcaplong), rcap_len));
3964 softc->disk->d_sectorsize = softc->params.secsize;
3965 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
3966 softc->disk->d_stripesize = softc->params.stripesize;
3967 softc->disk->d_stripeoffset = softc->params.stripeoffset;
3968 /* XXX: these are not actually "firmware" values, so they may be wrong */
3969 softc->disk->d_fwsectors = softc->params.secs_per_track;
3970 softc->disk->d_fwheads = softc->params.heads;
3971 softc->disk->d_devstat->block_size = softc->params.secsize;
3972 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
3974 error = disk_resize(softc->disk, M_NOWAIT);
3976 xpt_print(periph->path, "disk_resize(9) failed, error = %d\n", error);
3980 dasendorderedtag(void *arg)
3982 struct da_softc *softc = arg;
3984 if (da_send_ordered) {
3985 if (!LIST_EMPTY(&softc->pending_ccbs)) {
3986 if ((softc->flags & DA_FLAG_WAS_OTAG) == 0)
3987 softc->flags |= DA_FLAG_NEED_OTAG;
3988 softc->flags &= ~DA_FLAG_WAS_OTAG;
3991 /* Queue us up again */
3992 callout_reset(&softc->sendordered_c,
3993 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
3994 dasendorderedtag, softc);
3998 * Step through all DA peripheral drivers, and if the device is still open,
3999 * sync the disk cache to physical media.
4002 dashutdown(void * arg, int howto)
4004 struct cam_periph *periph;
4005 struct da_softc *softc;
4009 CAM_PERIPH_FOREACH(periph, &dadriver) {
4010 softc = (struct da_softc *)periph->softc;
4011 if (SCHEDULER_STOPPED()) {
4012 /* If we paniced with the lock held, do not recurse. */
4013 if (!cam_periph_owned(periph) &&
4014 (softc->flags & DA_FLAG_OPEN)) {
4015 dadump(softc->disk, NULL, 0, 0, 0);
4019 cam_periph_lock(periph);
4022 * We only sync the cache if the drive is still open, and
4023 * if the drive is capable of it..
4025 if (((softc->flags & DA_FLAG_OPEN) == 0)
4026 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
4027 cam_periph_unlock(periph);
4031 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
4032 scsi_synchronize_cache(&ccb->csio,
4036 /*begin_lba*/0, /* whole disk */
4041 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
4042 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR,
4043 softc->disk->d_devstat);
4045 xpt_print(periph->path, "Synchronize cache failed\n");
4046 xpt_release_ccb(ccb);
4047 cam_periph_unlock(periph);
4051 #else /* !_KERNEL */
4054 * XXX These are only left out of the kernel build to silence warnings. If,
4055 * for some reason these functions are used in the kernel, the ifdefs should
4056 * be moved so they are included both in the kernel and userland.
4059 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
4060 void (*cbfcnp)(struct cam_periph *, union ccb *),
4061 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
4062 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4065 struct scsi_format_unit *scsi_cmd;
4067 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
4068 scsi_cmd->opcode = FORMAT_UNIT;
4069 scsi_cmd->byte2 = byte2;
4070 scsi_ulto2b(ileave, scsi_cmd->interleave);
4075 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
4085 scsi_read_defects(struct ccb_scsiio *csio, uint32_t retries,
4086 void (*cbfcnp)(struct cam_periph *, union ccb *),
4087 uint8_t tag_action, uint8_t list_format,
4088 uint32_t addr_desc_index, uint8_t *data_ptr,
4089 uint32_t dxfer_len, int minimum_cmd_size,
4090 uint8_t sense_len, uint32_t timeout)
4095 * These conditions allow using the 10 byte command. Otherwise we
4096 * need to use the 12 byte command.
4098 if ((minimum_cmd_size <= 10)
4099 && (addr_desc_index == 0)
4100 && (dxfer_len <= SRDD10_MAX_LENGTH)) {
4101 struct scsi_read_defect_data_10 *cdb10;
4103 cdb10 = (struct scsi_read_defect_data_10 *)
4104 &csio->cdb_io.cdb_bytes;
4106 cdb_len = sizeof(*cdb10);
4107 bzero(cdb10, cdb_len);
4108 cdb10->opcode = READ_DEFECT_DATA_10;
4109 cdb10->format = list_format;
4110 scsi_ulto2b(dxfer_len, cdb10->alloc_length);
4112 struct scsi_read_defect_data_12 *cdb12;
4114 cdb12 = (struct scsi_read_defect_data_12 *)
4115 &csio->cdb_io.cdb_bytes;
4117 cdb_len = sizeof(*cdb12);
4118 bzero(cdb12, cdb_len);
4119 cdb12->opcode = READ_DEFECT_DATA_12;
4120 cdb12->format = list_format;
4121 scsi_ulto4b(dxfer_len, cdb12->alloc_length);
4122 scsi_ulto4b(addr_desc_index, cdb12->address_descriptor_index);
4128 /*flags*/ CAM_DIR_IN,
4138 scsi_sanitize(struct ccb_scsiio *csio, u_int32_t retries,
4139 void (*cbfcnp)(struct cam_periph *, union ccb *),
4140 u_int8_t tag_action, u_int8_t byte2, u_int16_t control,
4141 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4144 struct scsi_sanitize *scsi_cmd;
4146 scsi_cmd = (struct scsi_sanitize *)&csio->cdb_io.cdb_bytes;
4147 scsi_cmd->opcode = SANITIZE;
4148 scsi_cmd->byte2 = byte2;
4149 scsi_cmd->control = control;
4150 scsi_ulto2b(dxfer_len, scsi_cmd->length);
4155 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
4164 #endif /* _KERNEL */