2 * Implementation of SCSI Direct Access Peripheral driver for CAM.
4 * Copyright (c) 1997 Justin T. Gibbs.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
39 #include <sys/taskqueue.h>
41 #include <sys/mutex.h>
43 #include <sys/devicestat.h>
44 #include <sys/eventhandler.h>
45 #include <sys/malloc.h>
47 #include <sys/endian.h>
49 #include <geom/geom.h>
50 #include <geom/geom_disk.h>
59 #include <cam/cam_ccb.h>
60 #include <cam/cam_periph.h>
61 #include <cam/cam_xpt_periph.h>
62 #include <cam/cam_sim.h>
64 #include <cam/scsi/scsi_message.h>
67 #include <cam/scsi/scsi_da.h>
75 DA_STATE_PROBE_BLK_LIMITS,
82 DA_FLAG_PACK_INVALID = 0x001,
83 DA_FLAG_NEW_PACK = 0x002,
84 DA_FLAG_PACK_LOCKED = 0x004,
85 DA_FLAG_PACK_REMOVABLE = 0x008,
86 DA_FLAG_NEED_OTAG = 0x020,
87 DA_FLAG_WAS_OTAG = 0x040,
88 DA_FLAG_RETRY_UA = 0x080,
90 DA_FLAG_SCTX_INIT = 0x200,
91 DA_FLAG_CAN_RC16 = 0x400,
92 DA_FLAG_PROBED = 0x800,
93 DA_FLAG_DIRTY = 0x1000,
94 DA_FLAG_ANNOUNCED = 0x2000
99 DA_Q_NO_SYNC_CACHE = 0x01,
100 DA_Q_NO_6_BYTE = 0x02,
101 DA_Q_NO_PREVENT = 0x04,
104 DA_Q_NO_UNMAP = 0x20,
105 DA_Q_RETRY_BUSY = 0x40
108 #define DA_Q_BIT_STRING \
110 "\001NO_SYNC_CACHE" \
119 DA_CCB_PROBE_RC = 0x01,
120 DA_CCB_PROBE_RC16 = 0x02,
121 DA_CCB_PROBE_LBP = 0x03,
122 DA_CCB_PROBE_BLK_LIMITS = 0x04,
123 DA_CCB_PROBE_BDC = 0x05,
124 DA_CCB_PROBE_ATA = 0x06,
125 DA_CCB_BUFFER_IO = 0x07,
127 DA_CCB_DELETE = 0x0B,
129 DA_CCB_TYPE_MASK = 0x0F,
130 DA_CCB_RETRY_UA = 0x10
134 * Order here is important for method choice
136 * We prefer ATA_TRIM as tests run against a Sandforce 2281 SSD attached to
137 * LSI 2008 (mps) controller (FW: v12, Drv: v14) resulted 20% quicker deletes
138 * using ATA_TRIM than the corresponding UNMAP results for a real world mysql
139 * import taking 5mins.
150 DA_DELETE_MIN = DA_DELETE_ATA_TRIM,
151 DA_DELETE_MAX = DA_DELETE_ZERO
154 typedef void da_delete_func_t (struct cam_periph *periph, union ccb *ccb,
156 static da_delete_func_t da_delete_trim;
157 static da_delete_func_t da_delete_unmap;
158 static da_delete_func_t da_delete_ws;
160 static const void * da_delete_functions[] = {
170 static const char *da_delete_method_names[] =
171 { "NONE", "DISABLE", "ATA_TRIM", "UNMAP", "WS16", "WS10", "ZERO" };
172 static const char *da_delete_method_desc[] =
173 { "NONE", "DISABLED", "ATA TRIM", "UNMAP", "WRITE SAME(16) with UNMAP",
174 "WRITE SAME(10) with UNMAP", "ZERO" };
176 /* Offsets into our private area for storing information */
177 #define ccb_state ppriv_field0
178 #define ccb_bp ppriv_ptr1
183 u_int8_t secs_per_track;
184 u_int32_t secsize; /* Number of bytes/sector */
185 u_int64_t sectors; /* total number sectors */
190 #define UNMAP_RANGE_MAX 0xffffffff
191 #define UNMAP_HEAD_SIZE 8
192 #define UNMAP_RANGE_SIZE 16
193 #define UNMAP_MAX_RANGES 2048 /* Protocol Max is 4095 */
194 #define UNMAP_BUF_SIZE ((UNMAP_MAX_RANGES * UNMAP_RANGE_SIZE) + \
197 #define WS10_MAX_BLKS 0xffff
198 #define WS16_MAX_BLKS 0xffffffff
199 #define ATA_TRIM_MAX_RANGES ((UNMAP_BUF_SIZE / \
200 (ATA_DSM_RANGE_SIZE * ATA_DSM_BLK_SIZE)) * ATA_DSM_BLK_SIZE)
203 struct bio_queue_head bio_queue;
204 struct bio_queue_head delete_queue;
205 struct bio_queue_head delete_run_queue;
206 LIST_HEAD(, ccb_hdr) pending_ccbs;
207 int tur; /* TEST UNIT READY should be sent */
208 int refcount; /* Active xpt_action() calls */
213 int minimum_cmd_size;
217 int delete_available; /* Delete methods possibly available */
219 uint32_t unmap_max_ranges;
220 uint32_t unmap_max_lba; /* Max LBAs in UNMAP req */
221 uint64_t ws_max_blks;
222 da_delete_methods delete_method_pref;
223 da_delete_methods delete_method;
224 da_delete_func_t *delete_func;
225 struct disk_params params;
228 struct task sysctl_task;
229 struct sysctl_ctx_list sysctl_ctx;
230 struct sysctl_oid *sysctl_tree;
231 struct callout sendordered_c;
233 uint8_t unmap_buf[UNMAP_BUF_SIZE];
234 struct scsi_read_capacity_data_long rcaplong;
235 struct callout mediapoll_c;
238 #define dadeleteflag(softc, delete_method, enable) \
240 softc->delete_available |= (1 << delete_method); \
242 softc->delete_available &= ~(1 << delete_method); \
245 struct da_quirk_entry {
246 struct scsi_inquiry_pattern inq_pat;
250 static const char quantum[] = "QUANTUM";
251 static const char microp[] = "MICROP";
253 static struct da_quirk_entry da_quirk_table[] =
255 /* SPI, FC devices */
258 * Fujitsu M2513A MO drives.
259 * Tested devices: M2513A2 firmware versions 1200 & 1300.
260 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
261 * Reported by: W.Scholten <whs@xs4all.nl>
263 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
264 /*quirks*/ DA_Q_NO_SYNC_CACHE
268 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
269 /*quirks*/ DA_Q_NO_SYNC_CACHE
273 * This particular Fujitsu drive doesn't like the
274 * synchronize cache command.
275 * Reported by: Tom Jackson <toj@gorilla.net>
277 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
278 /*quirks*/ DA_Q_NO_SYNC_CACHE
282 * This drive doesn't like the synchronize cache command
283 * either. Reported by: Matthew Jacob <mjacob@feral.com>
284 * in NetBSD PR kern/6027, August 24, 1998.
286 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
287 /*quirks*/ DA_Q_NO_SYNC_CACHE
291 * This drive doesn't like the synchronize cache command
292 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
295 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
296 /*quirks*/ DA_Q_NO_SYNC_CACHE
300 * Doesn't like the synchronize cache command.
301 * Reported by: Blaz Zupan <blaz@gold.amis.net>
303 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
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, quantum, "MAVERICK 540S", "*"},
312 /*quirks*/ DA_Q_NO_SYNC_CACHE
316 * Doesn't like the synchronize cache command.
318 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
319 /*quirks*/ DA_Q_NO_SYNC_CACHE
323 * Doesn't like the synchronize cache command.
324 * Reported by: walter@pelissero.de
326 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
327 /*quirks*/ DA_Q_NO_SYNC_CACHE
331 * Doesn't work correctly with 6 byte reads/writes.
332 * Returns illegal request, and points to byte 9 of the
334 * Reported by: Adam McDougall <bsdx@spawnet.com>
336 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
337 /*quirks*/ DA_Q_NO_6_BYTE
341 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
342 /*quirks*/ DA_Q_NO_6_BYTE
346 * Doesn't like the synchronize cache command.
347 * Reported by: walter@pelissero.de
349 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
350 /*quirks*/ DA_Q_NO_SYNC_CACHE
354 * The CISS RAID controllers do not support SYNC_CACHE
356 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
357 /*quirks*/ DA_Q_NO_SYNC_CACHE
361 * The STEC SSDs sometimes hang on UNMAP.
363 {T_DIRECT, SIP_MEDIA_FIXED, "STEC", "*", "*"},
364 /*quirks*/ DA_Q_NO_UNMAP
368 * VMware returns BUSY status when storage has transient
369 * connectivity problems, so better wait.
371 {T_DIRECT, SIP_MEDIA_FIXED, "VMware*", "*", "*"},
372 /*quirks*/ DA_Q_RETRY_BUSY
374 /* USB mass storage devices supported by umass(4) */
377 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
380 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
381 /*quirks*/ DA_Q_NO_SYNC_CACHE
385 * Power Quotient Int. (PQI) USB flash key
388 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
389 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
393 * Creative Nomad MUVO mp3 player (USB)
396 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
397 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
401 * Jungsoft NEXDISK USB flash key
404 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
405 /*quirks*/ DA_Q_NO_SYNC_CACHE
409 * FreeDik USB Mini Data Drive
412 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
413 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
417 * Sigmatel USB Flash MP3 Player
420 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
421 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
425 * Neuros USB Digital Audio Computer
428 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
429 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
433 * SEAGRAND NP-900 MP3 Player
436 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
437 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
441 * iRiver iFP MP3 player (with UMS Firmware)
442 * PR: kern/54881, i386/63941, kern/66124
444 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
445 /*quirks*/ DA_Q_NO_SYNC_CACHE
449 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
452 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
453 /*quirks*/ DA_Q_NO_SYNC_CACHE
457 * ZICPlay USB MP3 Player with FM
460 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
461 /*quirks*/ DA_Q_NO_SYNC_CACHE
465 * TEAC USB floppy mechanisms
467 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
468 /*quirks*/ DA_Q_NO_SYNC_CACHE
472 * Kingston DataTraveler II+ USB Pen-Drive.
473 * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
475 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
476 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
484 {T_DIRECT, SIP_MEDIA_REMOVABLE, " ", "USB DISK Pro", "PMAP"},
485 /*quirks*/ DA_Q_NO_SYNC_CACHE
489 * Motorola E398 Mobile Phone (TransFlash memory card).
490 * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
493 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
494 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
498 * Qware BeatZkey! Pro
501 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
502 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
506 * Time DPA20B 1GB MP3 Player
509 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
510 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
514 * Samsung USB key 128Mb
517 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
518 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
522 * Kingston DataTraveler 2.0 USB Flash memory.
525 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
526 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
530 * Creative MUVO Slim mp3 player (USB)
533 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
534 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
538 * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
541 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
542 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
546 * SanDisk Micro Cruzer 128MB
549 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
550 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
554 * TOSHIBA TransMemory USB sticks
557 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
558 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
562 * PNY USB 3.0 Flash Drives
564 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PNY", "USB 3.0 FD*",
565 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_RC16
570 * PR: usb/75578, usb/72344, usb/65436
572 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
573 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
577 * Genesys 6-in-1 Card Reader
580 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
581 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
585 * Rekam Digital CAMERA
588 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
589 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
593 * iRiver H10 MP3 player
596 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
597 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
601 * iRiver U10 MP3 player
604 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
605 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
612 {T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
613 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
617 * EasyMP3 EM732X USB 2.0 Flash MP3 Player
620 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
621 "1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
628 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
629 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
633 * Philips USB Key Audio KEY013
636 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
637 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
644 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
645 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
652 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
653 /*quirks*/ DA_Q_NO_SYNC_CACHE
657 * I/O Magic USB flash - Giga Bank
660 {T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
661 /*quirks*/ DA_Q_NO_SYNC_CACHE
665 * JoyFly 128mb USB Flash Drive
668 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
669 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
676 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
677 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
681 * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A
684 {T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*",
685 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
689 * Samsung YP-U3 mp3-player
692 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3",
693 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
696 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
697 "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
701 * Sony Cyber-Shot DSC cameras
704 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
705 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
708 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler G3",
709 "1.00"}, /*quirks*/ DA_Q_NO_PREVENT
712 /* At least several Transcent USB sticks lie on RC16. */
713 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JetFlash", "Transcend*",
714 "*"}, /*quirks*/ DA_Q_NO_RC16
716 /* ATA/SATA devices over SAS/USB/... */
718 /* Hitachi Advanced Format (4k) drives */
719 { T_DIRECT, SIP_MEDIA_FIXED, "Hitachi", "H??????????E3*", "*" },
723 /* Samsung Advanced Format (4k) drives */
724 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD155UI*", "*" },
728 /* Samsung Advanced Format (4k) drives */
729 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD155UI*", "*" },
733 /* Samsung Advanced Format (4k) drives */
734 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD204UI*", "*" },
738 /* Samsung Advanced Format (4k) drives */
739 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD204UI*", "*" },
743 /* Seagate Barracuda Green Advanced Format (4k) drives */
744 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DL*", "*" },
748 /* Seagate Barracuda Green Advanced Format (4k) drives */
749 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DL", "*", "*" },
753 /* Seagate Barracuda Green Advanced Format (4k) drives */
754 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???DM*", "*" },
758 /* Seagate Barracuda Green Advanced Format (4k) drives */
759 { T_DIRECT, SIP_MEDIA_FIXED, "ST???DM*", "*", "*" },
763 /* Seagate Barracuda Green Advanced Format (4k) drives */
764 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DM*", "*" },
768 /* Seagate Barracuda Green Advanced Format (4k) drives */
769 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DM", "*", "*" },
773 /* Seagate Momentus Advanced Format (4k) drives */
774 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500423AS*", "*" },
778 /* Seagate Momentus Advanced Format (4k) drives */
779 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "3AS*", "*" },
783 /* Seagate Momentus Advanced Format (4k) drives */
784 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500424AS*", "*" },
788 /* Seagate Momentus Advanced Format (4k) drives */
789 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "4AS*", "*" },
793 /* Seagate Momentus Advanced Format (4k) drives */
794 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640423AS*", "*" },
798 /* Seagate Momentus Advanced Format (4k) drives */
799 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "3AS*", "*" },
803 /* Seagate Momentus Advanced Format (4k) drives */
804 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640424AS*", "*" },
808 /* Seagate Momentus Advanced Format (4k) drives */
809 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "4AS*", "*" },
813 /* Seagate Momentus Advanced Format (4k) drives */
814 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750420AS*", "*" },
818 /* Seagate Momentus Advanced Format (4k) drives */
819 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "0AS*", "*" },
823 /* Seagate Momentus Advanced Format (4k) drives */
824 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750422AS*", "*" },
828 /* Seagate Momentus Advanced Format (4k) drives */
829 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "2AS*", "*" },
833 /* Seagate Momentus Advanced Format (4k) drives */
834 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750423AS*", "*" },
838 /* Seagate Momentus Advanced Format (4k) drives */
839 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "3AS*", "*" },
843 /* Seagate Momentus Thin Advanced Format (4k) drives */
844 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???LT*", "*" },
848 /* Seagate Momentus Thin Advanced Format (4k) drives */
849 { T_DIRECT, SIP_MEDIA_FIXED, "ST???LT*", "*", "*" },
853 /* WDC Caviar Green Advanced Format (4k) drives */
854 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RS*", "*" },
858 /* WDC Caviar Green Advanced Format (4k) drives */
859 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RS*", "*" },
863 /* WDC Caviar Green Advanced Format (4k) drives */
864 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RX*", "*" },
868 /* WDC Caviar Green Advanced Format (4k) drives */
869 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RX*", "*" },
873 /* WDC Caviar Green Advanced Format (4k) drives */
874 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RS*", "*" },
878 /* WDC Caviar Green Advanced Format (4k) drives */
879 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RS*", "*" },
883 /* WDC Caviar Green Advanced Format (4k) drives */
884 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RX*", "*" },
888 /* WDC Caviar Green Advanced Format (4k) drives */
889 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RX*", "*" },
893 /* WDC Scorpio Black Advanced Format (4k) drives */
894 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PKT*", "*" },
898 /* WDC Scorpio Black Advanced Format (4k) drives */
899 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PKT*", "*" },
903 /* WDC Scorpio Black Advanced Format (4k) drives */
904 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PKT*", "*" },
908 /* WDC Scorpio Black Advanced Format (4k) drives */
909 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PKT*", "*" },
913 /* WDC Scorpio Blue Advanced Format (4k) drives */
914 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PVT*", "*" },
918 /* WDC Scorpio Blue Advanced Format (4k) drives */
919 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PVT*", "*" },
923 /* WDC Scorpio Blue Advanced Format (4k) drives */
924 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PVT*", "*" },
928 /* WDC Scorpio Blue Advanced Format (4k) drives */
929 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PVT*", "*" },
934 * Olympus FE-210 camera
936 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "FE210*",
937 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
943 {T_DIRECT, SIP_MEDIA_REMOVABLE, "LG", "UP3S",
944 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
948 * Laser MP3-2GA13 MP3 player
950 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "(HS) Flash Disk",
951 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
955 * LaCie external 250GB Hard drive des by Porsche
956 * Submitted by: Ben Stuyts <ben@altesco.nl>
959 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HM250JI", "*"},
960 /*quirks*/ DA_Q_NO_SYNC_CACHE
965 * Corsair Force 2 SSDs
966 * 4k optimised & trim only works in 4k requests + 4k aligned
968 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair CSSD-F*", "*" },
973 * Corsair Force 3 SSDs
974 * 4k optimised & trim only works in 4k requests + 4k aligned
976 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force 3*", "*" },
981 * Corsair Neutron GTX SSDs
982 * 4k optimised & trim only works in 4k requests + 4k aligned
984 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Neutron GTX*", "*" },
989 * Corsair Force GT & GS SSDs
990 * 4k optimised & trim only works in 4k requests + 4k aligned
992 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force G*", "*" },
998 * 4k optimised & trim only works in 4k requests + 4k aligned
1000 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "M4-CT???M4SSD2*", "*" },
1005 * Crucial RealSSD C300 SSDs
1008 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "C300-CTFDDAC???MAG*",
1009 "*" }, /*quirks*/DA_Q_4K
1013 * Intel 320 Series SSDs
1014 * 4k optimised & trim only works in 4k requests + 4k aligned
1016 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2CW*", "*" },
1021 * Intel 330 Series SSDs
1022 * 4k optimised & trim only works in 4k requests + 4k aligned
1024 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2CT*", "*" },
1029 * Intel 510 Series SSDs
1030 * 4k optimised & trim only works in 4k requests + 4k aligned
1032 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2MH*", "*" },
1037 * Intel 520 Series SSDs
1038 * 4k optimised & trim only works in 4k requests + 4k aligned
1040 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2BW*", "*" },
1045 * Intel X25-M Series SSDs
1046 * 4k optimised & trim only works in 4k requests + 4k aligned
1048 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2M*", "*" },
1053 * Kingston E100 Series SSDs
1054 * 4k optimised & trim only works in 4k requests + 4k aligned
1056 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SE100S3*", "*" },
1061 * Kingston HyperX 3k SSDs
1062 * 4k optimised & trim only works in 4k requests + 4k aligned
1064 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SH103S3*", "*" },
1069 * Marvell SSDs (entry taken from OpenSolaris)
1070 * 4k optimised & trim only works in 4k requests + 4k aligned
1072 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "MARVELL SD88SA02*", "*" },
1077 * OCZ Agility 2 SSDs
1078 * 4k optimised & trim only works in 4k requests + 4k aligned
1080 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY2*", "*" },
1085 * OCZ Agility 3 SSDs
1086 * 4k optimised & trim only works in 4k requests + 4k aligned
1088 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-AGILITY3*", "*" },
1093 * OCZ Deneva R Series SSDs
1094 * 4k optimised & trim only works in 4k requests + 4k aligned
1096 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "DENRSTE251M45*", "*" },
1101 * OCZ Vertex 2 SSDs (inc pro series)
1102 * 4k optimised & trim only works in 4k requests + 4k aligned
1104 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ?VERTEX2*", "*" },
1110 * 4k optimised & trim only works in 4k requests + 4k aligned
1112 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX3*", "*" },
1118 * 4k optimised & trim only works in 4k requests + 4k aligned
1120 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX4*", "*" },
1125 * Samsung 830 Series SSDs
1126 * 4k optimised & trim only works in 4k requests + 4k aligned
1128 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG SSD 830 Series*", "*" },
1134 * 4k optimised & trim only works in 4k requests + 4k aligned
1136 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 840*", "*" },
1141 * Samsung 843T Series SSDs
1144 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG MZ7WD*", "*" },
1150 * 4k optimised & trim only works in 4k requests + 4k aligned
1152 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 850*", "*" },
1157 * Samsung PM853T Series SSDs
1160 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG MZ7GE*", "*" },
1165 * SuperTalent TeraDrive CT SSDs
1166 * 4k optimised & trim only works in 4k requests + 4k aligned
1168 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "FTM??CT25H*", "*" },
1173 * XceedIOPS SATA SSDs
1176 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SG9XCS2D*", "*" },
1181 * Hama Innostor USB-Stick
1183 { T_DIRECT, SIP_MEDIA_REMOVABLE, "Innostor", "Innostor*", "*" },
1184 /*quirks*/DA_Q_NO_RC16
1188 * MX-ES USB Drive by Mach Xtreme
1190 { T_DIRECT, SIP_MEDIA_REMOVABLE, "MX", "MXUB3*", "*"},
1191 /*quirks*/DA_Q_NO_RC16
1195 static disk_strategy_t dastrategy;
1196 static dumper_t dadump;
1197 static periph_init_t dainit;
1198 static void daasync(void *callback_arg, u_int32_t code,
1199 struct cam_path *path, void *arg);
1200 static void dasysctlinit(void *context, int pending);
1201 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
1202 static int dadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
1203 static int dadeletemaxsysctl(SYSCTL_HANDLER_ARGS);
1204 static void dadeletemethodset(struct da_softc *softc,
1205 da_delete_methods delete_method);
1206 static off_t dadeletemaxsize(struct da_softc *softc,
1207 da_delete_methods delete_method);
1208 static void dadeletemethodchoose(struct da_softc *softc,
1209 da_delete_methods default_method);
1210 static void daprobedone(struct cam_periph *periph, union ccb *ccb);
1212 static periph_ctor_t daregister;
1213 static periph_dtor_t dacleanup;
1214 static periph_start_t dastart;
1215 static periph_oninv_t daoninvalidate;
1216 static void dadone(struct cam_periph *periph,
1217 union ccb *done_ccb);
1218 static int daerror(union ccb *ccb, u_int32_t cam_flags,
1219 u_int32_t sense_flags);
1220 static void daprevent(struct cam_periph *periph, int action);
1221 static void dareprobe(struct cam_periph *periph);
1222 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
1224 struct scsi_read_capacity_data_long *rcaplong,
1226 static timeout_t dasendorderedtag;
1227 static void dashutdown(void *arg, int howto);
1228 static timeout_t damediapoll;
1230 #ifndef DA_DEFAULT_POLL_PERIOD
1231 #define DA_DEFAULT_POLL_PERIOD 3
1234 #ifndef DA_DEFAULT_TIMEOUT
1235 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
1238 #ifndef DA_DEFAULT_RETRY
1239 #define DA_DEFAULT_RETRY 4
1242 #ifndef DA_DEFAULT_SEND_ORDERED
1243 #define DA_DEFAULT_SEND_ORDERED 1
1246 #define DA_SIO (softc->sort_io_queue >= 0 ? \
1247 softc->sort_io_queue : cam_sort_io_queues)
1249 static int da_poll_period = DA_DEFAULT_POLL_PERIOD;
1250 static int da_retry_count = DA_DEFAULT_RETRY;
1251 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
1252 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
1254 static SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
1255 "CAM Direct Access Disk driver");
1256 SYSCTL_INT(_kern_cam_da, OID_AUTO, poll_period, CTLFLAG_RWTUN,
1257 &da_poll_period, 0, "Media polling period in seconds");
1258 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RWTUN,
1259 &da_retry_count, 0, "Normal I/O retry count");
1260 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RWTUN,
1261 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
1262 SYSCTL_INT(_kern_cam_da, OID_AUTO, send_ordered, CTLFLAG_RWTUN,
1263 &da_send_ordered, 0, "Send Ordered Tags");
1266 * DA_ORDEREDTAG_INTERVAL determines how often, relative
1267 * to the default timeout, we check to see whether an ordered
1268 * tagged transaction is appropriate to prevent simple tag
1269 * starvation. Since we'd like to ensure that there is at least
1270 * 1/2 of the timeout length left for a starved transaction to
1271 * complete after we've sent an ordered tag, we must poll at least
1272 * four times in every timeout period. This takes care of the worst
1273 * case where a starved transaction starts during an interval that
1274 * meets the requirement "don't send an ordered tag" test so it takes
1275 * us two intervals to determine that a tag must be sent.
1277 #ifndef DA_ORDEREDTAG_INTERVAL
1278 #define DA_ORDEREDTAG_INTERVAL 4
1281 static struct periph_driver dadriver =
1284 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
1287 PERIPHDRIVER_DECLARE(da, dadriver);
1289 static MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
1292 daopen(struct disk *dp)
1294 struct cam_periph *periph;
1295 struct da_softc *softc;
1298 periph = (struct cam_periph *)dp->d_drv1;
1299 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1303 cam_periph_lock(periph);
1304 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
1305 cam_periph_unlock(periph);
1306 cam_periph_release(periph);
1310 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1313 softc = (struct da_softc *)periph->softc;
1316 /* Wait for the disk size update. */
1317 error = cam_periph_sleep(periph, &softc->disk->d_mediasize, PRIBIO,
1320 xpt_print(periph->path, "unable to retrieve capacity data\n");
1322 if (periph->flags & CAM_PERIPH_INVALID)
1325 if (error == 0 && (softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1326 (softc->quirks & DA_Q_NO_PREVENT) == 0)
1327 daprevent(periph, PR_PREVENT);
1330 softc->flags &= ~DA_FLAG_PACK_INVALID;
1331 softc->flags |= DA_FLAG_OPEN;
1334 cam_periph_unhold(periph);
1335 cam_periph_unlock(periph);
1338 cam_periph_release(periph);
1344 daclose(struct disk *dp)
1346 struct cam_periph *periph;
1347 struct da_softc *softc;
1351 periph = (struct cam_periph *)dp->d_drv1;
1352 softc = (struct da_softc *)periph->softc;
1353 cam_periph_lock(periph);
1354 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1357 if (cam_periph_hold(periph, PRIBIO) == 0) {
1359 /* Flush disk cache. */
1360 if ((softc->flags & DA_FLAG_DIRTY) != 0 &&
1361 (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0 &&
1362 (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
1363 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1364 scsi_synchronize_cache(&ccb->csio, /*retries*/1,
1365 /*cbfcnp*/dadone, MSG_SIMPLE_Q_TAG,
1366 /*begin_lba*/0, /*lb_count*/0, SSD_FULL_SIZE,
1368 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
1369 /*sense_flags*/SF_RETRY_UA | SF_QUIET_IR,
1370 softc->disk->d_devstat);
1372 softc->flags &= ~DA_FLAG_DIRTY;
1373 xpt_release_ccb(ccb);
1376 /* Allow medium removal. */
1377 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1378 (softc->quirks & DA_Q_NO_PREVENT) == 0)
1379 daprevent(periph, PR_ALLOW);
1381 cam_periph_unhold(periph);
1385 * If we've got removeable media, mark the blocksize as
1386 * unavailable, since it could change when new media is
1389 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0)
1390 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1392 softc->flags &= ~DA_FLAG_OPEN;
1393 while (softc->refcount != 0)
1394 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "daclose", 1);
1395 cam_periph_unlock(periph);
1396 cam_periph_release(periph);
1401 daschedule(struct cam_periph *periph)
1403 struct da_softc *softc = (struct da_softc *)periph->softc;
1405 if (softc->state != DA_STATE_NORMAL)
1408 /* Check if we have more work to do. */
1409 if (bioq_first(&softc->bio_queue) ||
1410 (!softc->delete_running && bioq_first(&softc->delete_queue)) ||
1412 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1417 * Actually translate the requested transfer into one the physical driver
1418 * can understand. The transfer is described by a buf and will include
1419 * only one physical transfer.
1422 dastrategy(struct bio *bp)
1424 struct cam_periph *periph;
1425 struct da_softc *softc;
1427 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1428 softc = (struct da_softc *)periph->softc;
1430 cam_periph_lock(periph);
1433 * If the device has been made invalid, error out
1435 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1436 cam_periph_unlock(periph);
1437 biofinish(bp, NULL, ENXIO);
1441 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
1444 * Place it in the queue of disk activities for this disk
1446 if (bp->bio_cmd == BIO_DELETE) {
1447 bioq_disksort(&softc->delete_queue, bp);
1448 } else if (DA_SIO) {
1449 bioq_disksort(&softc->bio_queue, bp);
1451 bioq_insert_tail(&softc->bio_queue, 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;
1609 * Return all queued I/O with ENXIO.
1610 * XXX Handle any transactions queued to the card
1611 * with XPT_ABORT_CCB.
1613 bioq_flush(&softc->bio_queue, NULL, ENXIO);
1614 bioq_flush(&softc->delete_queue, NULL, ENXIO);
1617 * Tell GEOM that we've gone away, we'll get a callback when it is
1618 * done cleaning up its resources.
1620 disk_gone(softc->disk);
1624 dacleanup(struct cam_periph *periph)
1626 struct da_softc *softc;
1628 softc = (struct da_softc *)periph->softc;
1630 cam_periph_unlock(periph);
1633 * If we can't free the sysctl tree, oh well...
1635 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1636 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1637 xpt_print(periph->path, "can't remove sysctl context\n");
1640 callout_drain(&softc->mediapoll_c);
1641 disk_destroy(softc->disk);
1642 callout_drain(&softc->sendordered_c);
1643 free(softc, M_DEVBUF);
1644 cam_periph_lock(periph);
1648 daasync(void *callback_arg, u_int32_t code,
1649 struct cam_path *path, void *arg)
1651 struct cam_periph *periph;
1652 struct da_softc *softc;
1654 periph = (struct cam_periph *)callback_arg;
1656 case AC_FOUND_DEVICE:
1658 struct ccb_getdev *cgd;
1661 cgd = (struct ccb_getdev *)arg;
1665 if (cgd->protocol != PROTO_SCSI)
1667 if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED)
1669 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1670 && SID_TYPE(&cgd->inq_data) != T_RBC
1671 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1675 * Allocate a peripheral instance for
1676 * this device and start the probe
1679 status = cam_periph_alloc(daregister, daoninvalidate,
1681 "da", CAM_PERIPH_BIO,
1683 AC_FOUND_DEVICE, cgd);
1685 if (status != CAM_REQ_CMP
1686 && status != CAM_REQ_INPROG)
1687 printf("daasync: Unable to attach to new device "
1688 "due to status 0x%x\n", status);
1691 case AC_ADVINFO_CHANGED:
1695 buftype = (uintptr_t)arg;
1696 if (buftype == CDAI_TYPE_PHYS_PATH) {
1697 struct da_softc *softc;
1699 softc = periph->softc;
1700 disk_attr_changed(softc->disk, "GEOM::physpath",
1705 case AC_UNIT_ATTENTION:
1708 int error_code, sense_key, asc, ascq;
1710 softc = (struct da_softc *)periph->softc;
1711 ccb = (union ccb *)arg;
1714 * Handle all UNIT ATTENTIONs except our own,
1715 * as they will be handled by daerror().
1717 if (xpt_path_periph(ccb->ccb_h.path) != periph &&
1718 scsi_extract_sense_ccb(ccb,
1719 &error_code, &sense_key, &asc, &ascq)) {
1720 if (asc == 0x2A && ascq == 0x09) {
1721 xpt_print(ccb->ccb_h.path,
1722 "Capacity data has changed\n");
1723 softc->flags &= ~DA_FLAG_PROBED;
1725 } else if (asc == 0x28 && ascq == 0x00) {
1726 softc->flags &= ~DA_FLAG_PROBED;
1727 disk_media_changed(softc->disk, M_NOWAIT);
1728 } else if (asc == 0x3F && ascq == 0x03) {
1729 xpt_print(ccb->ccb_h.path,
1730 "INQUIRY data has changed\n");
1731 softc->flags &= ~DA_FLAG_PROBED;
1735 cam_periph_async(periph, code, path, arg);
1739 softc = (struct da_softc *)periph->softc;
1741 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
1750 struct ccb_hdr *ccbh;
1752 softc = (struct da_softc *)periph->softc;
1754 * Don't fail on the expected unit attention
1757 softc->flags |= DA_FLAG_RETRY_UA;
1758 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1759 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1765 cam_periph_async(periph, code, path, arg);
1769 dasysctlinit(void *context, int pending)
1771 struct cam_periph *periph;
1772 struct da_softc *softc;
1773 char tmpstr[80], tmpstr2[80];
1774 struct ccb_trans_settings cts;
1776 periph = (struct cam_periph *)context;
1778 * periph was held for us when this task was enqueued
1780 if (periph->flags & CAM_PERIPH_INVALID) {
1781 cam_periph_release(periph);
1785 softc = (struct da_softc *)periph->softc;
1786 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1787 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1789 sysctl_ctx_init(&softc->sysctl_ctx);
1790 softc->flags |= DA_FLAG_SCTX_INIT;
1791 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1792 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1793 CTLFLAG_RD, 0, tmpstr);
1794 if (softc->sysctl_tree == NULL) {
1795 printf("dasysctlinit: unable to allocate sysctl tree\n");
1796 cam_periph_release(periph);
1801 * Now register the sysctl handler, so the user can change the value on
1804 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1805 OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RWTUN,
1806 softc, 0, dadeletemethodsysctl, "A",
1807 "BIO_DELETE execution method");
1808 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1809 OID_AUTO, "delete_max", CTLTYPE_U64 | CTLFLAG_RW,
1810 softc, 0, dadeletemaxsysctl, "Q",
1811 "Maximum BIO_DELETE size");
1812 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1813 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1814 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1815 "Minimum CDB size");
1816 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1817 OID_AUTO, "sort_io_queue", CTLFLAG_RW, &softc->sort_io_queue, 0,
1818 "Sort IO queue to try and optimise disk access patterns");
1820 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1821 SYSCTL_CHILDREN(softc->sysctl_tree),
1825 &softc->error_inject,
1827 "error_inject leaf");
1831 * Add some addressing info.
1833 memset(&cts, 0, sizeof (cts));
1834 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1835 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1836 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1837 cam_periph_lock(periph);
1838 xpt_action((union ccb *)&cts);
1839 cam_periph_unlock(periph);
1840 if (cts.ccb_h.status != CAM_REQ_CMP) {
1841 cam_periph_release(periph);
1844 if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
1845 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1846 if (fc->valid & CTS_FC_VALID_WWPN) {
1847 softc->wwpn = fc->wwpn;
1848 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1849 SYSCTL_CHILDREN(softc->sysctl_tree),
1850 OID_AUTO, "wwpn", CTLFLAG_RD,
1851 &softc->wwpn, "World Wide Port Name");
1854 cam_periph_release(periph);
1858 dadeletemaxsysctl(SYSCTL_HANDLER_ARGS)
1862 struct da_softc *softc;
1864 softc = (struct da_softc *)arg1;
1866 value = softc->disk->d_delmaxsize;
1867 error = sysctl_handle_64(oidp, &value, 0, req);
1868 if ((error != 0) || (req->newptr == NULL))
1871 /* only accept values smaller than the calculated value */
1872 if (value > dadeletemaxsize(softc, softc->delete_method)) {
1875 softc->disk->d_delmaxsize = value;
1881 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1885 value = *(int *)arg1;
1887 error = sysctl_handle_int(oidp, &value, 0, req);
1890 || (req->newptr == NULL))
1894 * Acceptable values here are 6, 10, 12 or 16.
1898 else if ((value > 6)
1901 else if ((value > 10)
1904 else if (value > 12)
1907 *(int *)arg1 = value;
1913 dadeletemethodset(struct da_softc *softc, da_delete_methods delete_method)
1916 softc->delete_method = delete_method;
1917 softc->disk->d_delmaxsize = dadeletemaxsize(softc, delete_method);
1918 softc->delete_func = da_delete_functions[delete_method];
1920 if (softc->delete_method > DA_DELETE_DISABLE)
1921 softc->disk->d_flags |= DISKFLAG_CANDELETE;
1923 softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
1927 dadeletemaxsize(struct da_softc *softc, da_delete_methods delete_method)
1931 switch(delete_method) {
1932 case DA_DELETE_UNMAP:
1933 sectors = (off_t)softc->unmap_max_lba;
1935 case DA_DELETE_ATA_TRIM:
1936 sectors = (off_t)ATA_DSM_RANGE_MAX * softc->trim_max_ranges;
1938 case DA_DELETE_WS16:
1939 sectors = omin(softc->ws_max_blks, WS16_MAX_BLKS);
1941 case DA_DELETE_ZERO:
1942 case DA_DELETE_WS10:
1943 sectors = omin(softc->ws_max_blks, WS10_MAX_BLKS);
1949 return (off_t)softc->params.secsize *
1950 omin(sectors, softc->params.sectors);
1954 daprobedone(struct cam_periph *periph, union ccb *ccb)
1956 struct da_softc *softc;
1958 softc = (struct da_softc *)periph->softc;
1960 dadeletemethodchoose(softc, DA_DELETE_NONE);
1962 if (bootverbose && (softc->flags & DA_FLAG_ANNOUNCED) == 0) {
1966 snprintf(buf, sizeof(buf), "Delete methods: <");
1968 for (i = 0; i <= DA_DELETE_MAX; i++) {
1969 if ((softc->delete_available & (1 << i)) == 0 &&
1970 i != softc->delete_method)
1973 strlcat(buf, ",", sizeof(buf));
1974 strlcat(buf, da_delete_method_names[i],
1976 if (i == softc->delete_method)
1977 strlcat(buf, "(*)", sizeof(buf));
1980 strlcat(buf, ">", sizeof(buf));
1981 printf("%s%d: %s\n", periph->periph_name,
1982 periph->unit_number, buf);
1986 * Since our peripheral may be invalidated by an error
1987 * above or an external event, we must release our CCB
1988 * before releasing the probe lock on the peripheral.
1989 * The peripheral will only go away once the last lock
1990 * is removed, and we need it around for the CCB release
1993 xpt_release_ccb(ccb);
1994 softc->state = DA_STATE_NORMAL;
1995 softc->flags |= DA_FLAG_PROBED;
1997 wakeup(&softc->disk->d_mediasize);
1998 if ((softc->flags & DA_FLAG_ANNOUNCED) == 0) {
1999 softc->flags |= DA_FLAG_ANNOUNCED;
2000 cam_periph_unhold(periph);
2002 cam_periph_release_locked(periph);
2006 dadeletemethodchoose(struct da_softc *softc, da_delete_methods default_method)
2010 /* If available, prefer the method requested by user. */
2011 i = softc->delete_method_pref;
2012 methods = softc->delete_available | (1 << DA_DELETE_DISABLE);
2013 if (methods & (1 << i)) {
2014 dadeletemethodset(softc, i);
2018 /* Use the pre-defined order to choose the best performing delete. */
2019 for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
2020 if (i == DA_DELETE_ZERO)
2022 if (softc->delete_available & (1 << i)) {
2023 dadeletemethodset(softc, i);
2028 /* Fallback to default. */
2029 dadeletemethodset(softc, default_method);
2033 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
2037 struct da_softc *softc;
2038 int i, error, methods, value;
2040 softc = (struct da_softc *)arg1;
2042 value = softc->delete_method;
2043 if (value < 0 || value > DA_DELETE_MAX)
2046 p = da_delete_method_names[value];
2047 strncpy(buf, p, sizeof(buf));
2048 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2049 if (error != 0 || req->newptr == NULL)
2051 methods = softc->delete_available | (1 << DA_DELETE_DISABLE);
2052 for (i = 0; i <= DA_DELETE_MAX; i++) {
2053 if (strcmp(buf, da_delete_method_names[i]) == 0)
2056 if (i > DA_DELETE_MAX)
2058 softc->delete_method_pref = i;
2059 dadeletemethodchoose(softc, DA_DELETE_NONE);
2064 daregister(struct cam_periph *periph, void *arg)
2066 struct da_softc *softc;
2067 struct ccb_pathinq cpi;
2068 struct ccb_getdev *cgd;
2072 cgd = (struct ccb_getdev *)arg;
2074 printf("daregister: no getdev CCB, can't register device\n");
2075 return(CAM_REQ_CMP_ERR);
2078 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
2081 if (softc == NULL) {
2082 printf("daregister: Unable to probe new device. "
2083 "Unable to allocate softc\n");
2084 return(CAM_REQ_CMP_ERR);
2087 LIST_INIT(&softc->pending_ccbs);
2088 softc->state = DA_STATE_PROBE_RC;
2089 bioq_init(&softc->bio_queue);
2090 bioq_init(&softc->delete_queue);
2091 bioq_init(&softc->delete_run_queue);
2092 if (SID_IS_REMOVABLE(&cgd->inq_data))
2093 softc->flags |= DA_FLAG_PACK_REMOVABLE;
2094 softc->unmap_max_ranges = UNMAP_MAX_RANGES;
2095 softc->unmap_max_lba = UNMAP_RANGE_MAX;
2096 softc->ws_max_blks = WS16_MAX_BLKS;
2097 softc->trim_max_ranges = ATA_TRIM_MAX_RANGES;
2098 softc->sort_io_queue = -1;
2100 periph->softc = softc;
2103 * See if this device has any quirks.
2105 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
2106 (caddr_t)da_quirk_table,
2107 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
2108 sizeof(*da_quirk_table), scsi_inquiry_match);
2111 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
2113 softc->quirks = DA_Q_NONE;
2115 /* Check if the SIM does not want 6 byte commands */
2116 bzero(&cpi, sizeof(cpi));
2117 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2118 cpi.ccb_h.func_code = XPT_PATH_INQ;
2119 xpt_action((union ccb *)&cpi);
2120 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
2121 softc->quirks |= DA_Q_NO_6_BYTE;
2123 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
2126 * Take an exclusive refcount on the periph while dastart is called
2127 * to finish the probe. The reference will be dropped in dadone at
2130 (void)cam_periph_hold(periph, PRIBIO);
2133 * Schedule a periodic event to occasionally send an
2134 * ordered tag to a device.
2136 callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0);
2137 callout_reset(&softc->sendordered_c,
2138 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2139 dasendorderedtag, softc);
2141 cam_periph_unlock(periph);
2143 * RBC devices don't have to support READ(6), only READ(10).
2145 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
2146 softc->minimum_cmd_size = 10;
2148 softc->minimum_cmd_size = 6;
2151 * Load the user's default, if any.
2153 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
2154 periph->unit_number);
2155 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
2158 * 6, 10, 12 and 16 are the currently permissible values.
2160 if (softc->minimum_cmd_size < 6)
2161 softc->minimum_cmd_size = 6;
2162 else if ((softc->minimum_cmd_size > 6)
2163 && (softc->minimum_cmd_size <= 10))
2164 softc->minimum_cmd_size = 10;
2165 else if ((softc->minimum_cmd_size > 10)
2166 && (softc->minimum_cmd_size <= 12))
2167 softc->minimum_cmd_size = 12;
2168 else if (softc->minimum_cmd_size > 12)
2169 softc->minimum_cmd_size = 16;
2171 /* Predict whether device may support READ CAPACITY(16). */
2172 if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3 &&
2173 (softc->quirks & DA_Q_NO_RC16) == 0) {
2174 softc->flags |= DA_FLAG_CAN_RC16;
2175 softc->state = DA_STATE_PROBE_RC16;
2179 * Register this media as a disk.
2181 softc->disk = disk_alloc();
2182 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
2183 periph->unit_number, 0,
2184 DEVSTAT_BS_UNAVAILABLE,
2185 SID_TYPE(&cgd->inq_data) |
2186 XPORT_DEVSTAT_TYPE(cpi.transport),
2187 DEVSTAT_PRIORITY_DISK);
2188 softc->disk->d_open = daopen;
2189 softc->disk->d_close = daclose;
2190 softc->disk->d_strategy = dastrategy;
2191 softc->disk->d_dump = dadump;
2192 softc->disk->d_getattr = dagetattr;
2193 softc->disk->d_gone = dadiskgonecb;
2194 softc->disk->d_name = "da";
2195 softc->disk->d_drv1 = periph;
2197 softc->maxio = DFLTPHYS; /* traditional default */
2198 else if (cpi.maxio > MAXPHYS)
2199 softc->maxio = MAXPHYS; /* for safety */
2201 softc->maxio = cpi.maxio;
2202 softc->disk->d_maxsize = softc->maxio;
2203 softc->disk->d_unit = periph->unit_number;
2204 softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION;
2205 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
2206 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
2207 if ((cpi.hba_misc & PIM_UNMAPPED) != 0)
2208 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
2209 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
2210 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
2211 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
2212 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
2213 cgd->inq_data.product, sizeof(cgd->inq_data.product),
2214 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
2215 softc->disk->d_hba_vendor = cpi.hba_vendor;
2216 softc->disk->d_hba_device = cpi.hba_device;
2217 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
2218 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
2221 * Acquire a reference to the periph before we register with GEOM.
2222 * We'll release this reference once GEOM calls us back (via
2223 * dadiskgonecb()) telling us that our provider has been freed.
2225 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
2226 xpt_print(periph->path, "%s: lost periph during "
2227 "registration!\n", __func__);
2228 cam_periph_lock(periph);
2229 return (CAM_REQ_CMP_ERR);
2232 disk_create(softc->disk, DISK_VERSION);
2233 cam_periph_lock(periph);
2236 * Add async callbacks for events of interest.
2237 * I don't bother checking if this fails as,
2238 * in most cases, the system will function just
2239 * fine without them and the only alternative
2240 * would be to not attach the device on failure.
2242 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
2243 AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION,
2244 daasync, periph, periph->path);
2247 * Emit an attribute changed notification just in case
2248 * physical path information arrived before our async
2249 * event handler was registered, but after anyone attaching
2250 * to our disk device polled it.
2252 disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
2255 * Schedule a periodic media polling events.
2257 callout_init_mtx(&softc->mediapoll_c, cam_periph_mtx(periph), 0);
2258 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
2259 (cgd->inq_flags & SID_AEN) == 0 &&
2260 da_poll_period != 0)
2261 callout_reset(&softc->mediapoll_c, da_poll_period * hz,
2262 damediapoll, periph);
2264 xpt_schedule(periph, CAM_PRIORITY_DEV);
2266 return(CAM_REQ_CMP);
2270 dastart(struct cam_periph *periph, union ccb *start_ccb)
2272 struct da_softc *softc;
2274 softc = (struct da_softc *)periph->softc;
2276 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
2279 switch (softc->state) {
2280 case DA_STATE_NORMAL:
2285 /* Run BIO_DELETE if not running yet. */
2286 if (!softc->delete_running &&
2287 (bp = bioq_first(&softc->delete_queue)) != NULL) {
2288 if (softc->delete_func != NULL) {
2289 softc->delete_func(periph, start_ccb, bp);
2292 bioq_flush(&softc->delete_queue, NULL, 0);
2297 /* Run regular command. */
2298 bp = bioq_takefirst(&softc->bio_queue);
2302 scsi_test_unit_ready(&start_ccb->csio,
2303 /*retries*/ da_retry_count,
2307 da_default_timeout * 1000);
2308 start_ccb->ccb_h.ccb_bp = NULL;
2309 start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
2310 xpt_action(start_ccb);
2312 xpt_release_ccb(start_ccb);
2317 cam_periph_release_locked(periph);
2320 if ((bp->bio_flags & BIO_ORDERED) != 0 ||
2321 (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
2322 softc->flags &= ~DA_FLAG_NEED_OTAG;
2323 softc->flags |= DA_FLAG_WAS_OTAG;
2324 tag_code = MSG_ORDERED_Q_TAG;
2326 tag_code = MSG_SIMPLE_Q_TAG;
2329 switch (bp->bio_cmd) {
2336 if (bp->bio_cmd == BIO_WRITE) {
2337 softc->flags |= DA_FLAG_DIRTY;
2338 rw_op = SCSI_RW_WRITE;
2340 rw_op = SCSI_RW_READ;
2343 data_ptr = bp->bio_data;
2344 if ((bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0) {
2345 rw_op |= SCSI_RW_BIO;
2349 scsi_read_write(&start_ccb->csio,
2350 /*retries*/da_retry_count,
2352 /*tag_action*/tag_code,
2355 softc->minimum_cmd_size,
2356 /*lba*/bp->bio_pblkno,
2357 /*block_count*/bp->bio_bcount /
2358 softc->params.secsize,
2360 /*dxfer_len*/ bp->bio_bcount,
2361 /*sense_len*/SSD_FULL_SIZE,
2362 da_default_timeout * 1000);
2367 * BIO_FLUSH doesn't currently communicate
2368 * range data, so we synchronize the cache
2369 * over the whole disk. We also force
2370 * ordered tag semantics the flush applies
2371 * to all previously queued I/O.
2373 scsi_synchronize_cache(&start_ccb->csio,
2380 da_default_timeout*1000);
2383 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
2384 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
2387 LIST_INSERT_HEAD(&softc->pending_ccbs,
2388 &start_ccb->ccb_h, periph_links.le);
2390 /* We expect a unit attention from this device */
2391 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
2392 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
2393 softc->flags &= ~DA_FLAG_RETRY_UA;
2396 start_ccb->ccb_h.ccb_bp = bp;
2398 cam_periph_unlock(periph);
2399 xpt_action(start_ccb);
2400 cam_periph_lock(periph);
2403 /* May have more work to do, so ensure we stay scheduled */
2407 case DA_STATE_PROBE_RC:
2409 struct scsi_read_capacity_data *rcap;
2411 rcap = (struct scsi_read_capacity_data *)
2412 malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
2414 printf("dastart: Couldn't malloc read_capacity data\n");
2415 /* da_free_periph??? */
2418 scsi_read_capacity(&start_ccb->csio,
2419 /*retries*/da_retry_count,
2425 start_ccb->ccb_h.ccb_bp = NULL;
2426 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC;
2427 xpt_action(start_ccb);
2430 case DA_STATE_PROBE_RC16:
2432 struct scsi_read_capacity_data_long *rcaplong;
2434 rcaplong = (struct scsi_read_capacity_data_long *)
2435 malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
2436 if (rcaplong == NULL) {
2437 printf("dastart: Couldn't malloc read_capacity data\n");
2438 /* da_free_periph??? */
2441 scsi_read_capacity_16(&start_ccb->csio,
2442 /*retries*/ da_retry_count,
2444 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2448 /*rcap_buf*/ (uint8_t *)rcaplong,
2449 /*rcap_buf_len*/ sizeof(*rcaplong),
2450 /*sense_len*/ SSD_FULL_SIZE,
2451 /*timeout*/ da_default_timeout * 1000);
2452 start_ccb->ccb_h.ccb_bp = NULL;
2453 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC16;
2454 xpt_action(start_ccb);
2457 case DA_STATE_PROBE_LBP:
2459 struct scsi_vpd_logical_block_prov *lbp;
2461 if (!scsi_vpd_supported_page(periph, SVPD_LBP)) {
2463 * If we get here we don't support any SBC-3 delete
2464 * methods with UNMAP as the Logical Block Provisioning
2465 * VPD page support is required for devices which
2466 * support it according to T10/1799-D Revision 31
2467 * however older revisions of the spec don't mandate
2468 * this so we currently don't remove these methods
2469 * from the available set.
2471 softc->state = DA_STATE_PROBE_BLK_LIMITS;
2475 lbp = (struct scsi_vpd_logical_block_prov *)
2476 malloc(sizeof(*lbp), M_SCSIDA, M_NOWAIT|M_ZERO);
2479 printf("dastart: Couldn't malloc lbp data\n");
2480 /* da_free_periph??? */
2484 scsi_inquiry(&start_ccb->csio,
2485 /*retries*/da_retry_count,
2487 /*tag_action*/MSG_SIMPLE_Q_TAG,
2488 /*inq_buf*/(u_int8_t *)lbp,
2489 /*inq_len*/sizeof(*lbp),
2491 /*page_code*/SVPD_LBP,
2492 /*sense_len*/SSD_MIN_SIZE,
2493 /*timeout*/da_default_timeout * 1000);
2494 start_ccb->ccb_h.ccb_bp = NULL;
2495 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_LBP;
2496 xpt_action(start_ccb);
2499 case DA_STATE_PROBE_BLK_LIMITS:
2501 struct scsi_vpd_block_limits *block_limits;
2503 if (!scsi_vpd_supported_page(periph, SVPD_BLOCK_LIMITS)) {
2504 /* Not supported skip to next probe */
2505 softc->state = DA_STATE_PROBE_BDC;
2509 block_limits = (struct scsi_vpd_block_limits *)
2510 malloc(sizeof(*block_limits), M_SCSIDA, M_NOWAIT|M_ZERO);
2512 if (block_limits == NULL) {
2513 printf("dastart: Couldn't malloc block_limits data\n");
2514 /* da_free_periph??? */
2518 scsi_inquiry(&start_ccb->csio,
2519 /*retries*/da_retry_count,
2521 /*tag_action*/MSG_SIMPLE_Q_TAG,
2522 /*inq_buf*/(u_int8_t *)block_limits,
2523 /*inq_len*/sizeof(*block_limits),
2525 /*page_code*/SVPD_BLOCK_LIMITS,
2526 /*sense_len*/SSD_MIN_SIZE,
2527 /*timeout*/da_default_timeout * 1000);
2528 start_ccb->ccb_h.ccb_bp = NULL;
2529 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BLK_LIMITS;
2530 xpt_action(start_ccb);
2533 case DA_STATE_PROBE_BDC:
2535 struct scsi_vpd_block_characteristics *bdc;
2537 if (!scsi_vpd_supported_page(periph, SVPD_BDC)) {
2538 softc->state = DA_STATE_PROBE_ATA;
2542 bdc = (struct scsi_vpd_block_characteristics *)
2543 malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
2546 printf("dastart: Couldn't malloc bdc data\n");
2547 /* da_free_periph??? */
2551 scsi_inquiry(&start_ccb->csio,
2552 /*retries*/da_retry_count,
2554 /*tag_action*/MSG_SIMPLE_Q_TAG,
2555 /*inq_buf*/(u_int8_t *)bdc,
2556 /*inq_len*/sizeof(*bdc),
2558 /*page_code*/SVPD_BDC,
2559 /*sense_len*/SSD_MIN_SIZE,
2560 /*timeout*/da_default_timeout * 1000);
2561 start_ccb->ccb_h.ccb_bp = NULL;
2562 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BDC;
2563 xpt_action(start_ccb);
2566 case DA_STATE_PROBE_ATA:
2568 struct ata_params *ata_params;
2570 if (!scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
2571 daprobedone(periph, start_ccb);
2575 ata_params = (struct ata_params*)
2576 malloc(sizeof(*ata_params), M_SCSIDA, M_NOWAIT|M_ZERO);
2578 if (ata_params == NULL) {
2579 printf("dastart: Couldn't malloc ata_params data\n");
2580 /* da_free_periph??? */
2584 scsi_ata_identify(&start_ccb->csio,
2585 /*retries*/da_retry_count,
2587 /*tag_action*/MSG_SIMPLE_Q_TAG,
2588 /*data_ptr*/(u_int8_t *)ata_params,
2589 /*dxfer_len*/sizeof(*ata_params),
2590 /*sense_len*/SSD_FULL_SIZE,
2591 /*timeout*/da_default_timeout * 1000);
2592 start_ccb->ccb_h.ccb_bp = NULL;
2593 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA;
2594 xpt_action(start_ccb);
2601 * In each of the methods below, while its the caller's
2602 * responsibility to ensure the request will fit into a
2603 * single device request, we might have changed the delete
2604 * method due to the device incorrectly advertising either
2605 * its supported methods or limits.
2607 * To prevent this causing further issues we validate the
2608 * against the methods limits, and warn which would
2609 * otherwise be unnecessary.
2612 da_delete_unmap(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2614 struct da_softc *softc = (struct da_softc *)periph->softc;;
2616 uint8_t *buf = softc->unmap_buf;
2617 uint64_t lba, lastlba = (uint64_t)-1;
2618 uint64_t totalcount = 0;
2620 uint32_t lastcount = 0, c;
2621 uint32_t off, ranges = 0;
2624 * Currently this doesn't take the UNMAP
2625 * Granularity and Granularity Alignment
2626 * fields into account.
2628 * This could result in both unoptimal unmap
2629 * requests as as well as UNMAP calls unmapping
2630 * fewer LBA's than requested.
2633 softc->delete_running = 1;
2634 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2637 bioq_remove(&softc->delete_queue, bp1);
2639 bioq_insert_tail(&softc->delete_run_queue, bp1);
2640 lba = bp1->bio_pblkno;
2641 count = bp1->bio_bcount / softc->params.secsize;
2643 /* Try to extend the previous range. */
2644 if (lba == lastlba) {
2645 c = omin(count, UNMAP_RANGE_MAX - lastcount);
2647 off = ((ranges - 1) * UNMAP_RANGE_SIZE) +
2649 scsi_ulto4b(lastcount, &buf[off + 8]);
2656 c = omin(count, UNMAP_RANGE_MAX);
2657 if (totalcount + c > softc->unmap_max_lba ||
2658 ranges >= softc->unmap_max_ranges) {
2659 xpt_print(periph->path,
2660 "%s issuing short delete %ld > %ld"
2662 da_delete_method_desc[softc->delete_method],
2663 totalcount + c, softc->unmap_max_lba,
2664 ranges, softc->unmap_max_ranges);
2667 off = (ranges * UNMAP_RANGE_SIZE) + UNMAP_HEAD_SIZE;
2668 scsi_u64to8b(lba, &buf[off + 0]);
2669 scsi_ulto4b(c, &buf[off + 8]);
2677 bp1 = bioq_first(&softc->delete_queue);
2678 if (bp1 == NULL || ranges >= softc->unmap_max_ranges ||
2679 totalcount + bp1->bio_bcount /
2680 softc->params.secsize > softc->unmap_max_lba)
2683 scsi_ulto2b(ranges * 16 + 6, &buf[0]);
2684 scsi_ulto2b(ranges * 16, &buf[2]);
2686 scsi_unmap(&ccb->csio,
2687 /*retries*/da_retry_count,
2689 /*tag_action*/MSG_SIMPLE_Q_TAG,
2692 /*dxfer_len*/ ranges * 16 + 8,
2693 /*sense_len*/SSD_FULL_SIZE,
2694 da_default_timeout * 1000);
2695 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2696 ccb->ccb_h.flags |= CAM_UNLOCKED;
2700 da_delete_trim(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2702 struct da_softc *softc = (struct da_softc *)periph->softc;
2704 uint8_t *buf = softc->unmap_buf;
2705 uint64_t lastlba = (uint64_t)-1;
2708 uint32_t lastcount = 0, c, requestcount;
2709 int ranges = 0, off, block_count;
2711 softc->delete_running = 1;
2712 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2715 bioq_remove(&softc->delete_queue, bp1);
2717 bioq_insert_tail(&softc->delete_run_queue, bp1);
2718 lba = bp1->bio_pblkno;
2719 count = bp1->bio_bcount / softc->params.secsize;
2720 requestcount = count;
2722 /* Try to extend the previous range. */
2723 if (lba == lastlba) {
2724 c = omin(count, ATA_DSM_RANGE_MAX - lastcount);
2726 off = (ranges - 1) * 8;
2727 buf[off + 6] = lastcount & 0xff;
2728 buf[off + 7] = (lastcount >> 8) & 0xff;
2734 c = omin(count, ATA_DSM_RANGE_MAX);
2737 buf[off + 0] = lba & 0xff;
2738 buf[off + 1] = (lba >> 8) & 0xff;
2739 buf[off + 2] = (lba >> 16) & 0xff;
2740 buf[off + 3] = (lba >> 24) & 0xff;
2741 buf[off + 4] = (lba >> 32) & 0xff;
2742 buf[off + 5] = (lba >> 40) & 0xff;
2743 buf[off + 6] = c & 0xff;
2744 buf[off + 7] = (c >> 8) & 0xff;
2749 if (count != 0 && ranges == softc->trim_max_ranges) {
2750 xpt_print(periph->path,
2751 "%s issuing short delete %ld > %ld\n",
2752 da_delete_method_desc[softc->delete_method],
2754 (softc->trim_max_ranges - ranges) *
2760 bp1 = bioq_first(&softc->delete_queue);
2761 if (bp1 == NULL || bp1->bio_bcount / softc->params.secsize >
2762 (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX)
2766 block_count = (ranges + ATA_DSM_BLK_RANGES - 1) / ATA_DSM_BLK_RANGES;
2767 scsi_ata_trim(&ccb->csio,
2768 /*retries*/da_retry_count,
2770 /*tag_action*/MSG_SIMPLE_Q_TAG,
2773 /*dxfer_len*/block_count * ATA_DSM_BLK_SIZE,
2774 /*sense_len*/SSD_FULL_SIZE,
2775 da_default_timeout * 1000);
2776 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2777 ccb->ccb_h.flags |= CAM_UNLOCKED;
2781 * We calculate ws_max_blks here based off d_delmaxsize instead
2782 * of using softc->ws_max_blks as it is absolute max for the
2783 * device not the protocol max which may well be lower.
2786 da_delete_ws(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2788 struct da_softc *softc;
2790 uint64_t ws_max_blks;
2792 uint64_t count; /* forward compat with WS32 */
2794 softc = (struct da_softc *)periph->softc;
2795 ws_max_blks = softc->disk->d_delmaxsize / softc->params.secsize;
2796 softc->delete_running = 1;
2797 lba = bp->bio_pblkno;
2801 bioq_remove(&softc->delete_queue, bp1);
2803 bioq_insert_tail(&softc->delete_run_queue, bp1);
2804 count += bp1->bio_bcount / softc->params.secsize;
2805 if (count > ws_max_blks) {
2806 xpt_print(periph->path,
2807 "%s issuing short delete %ld > %ld\n",
2808 da_delete_method_desc[softc->delete_method],
2809 count, ws_max_blks);
2810 count = omin(count, ws_max_blks);
2813 bp1 = bioq_first(&softc->delete_queue);
2814 if (bp1 == NULL || lba + count != bp1->bio_pblkno ||
2815 count + bp1->bio_bcount /
2816 softc->params.secsize > ws_max_blks)
2820 scsi_write_same(&ccb->csio,
2821 /*retries*/da_retry_count,
2823 /*tag_action*/MSG_SIMPLE_Q_TAG,
2824 /*byte2*/softc->delete_method ==
2825 DA_DELETE_ZERO ? 0 : SWS_UNMAP,
2826 softc->delete_method == DA_DELETE_WS16 ? 16 : 10,
2828 /*block_count*/count,
2829 /*data_ptr*/ __DECONST(void *, zero_region),
2830 /*dxfer_len*/ softc->params.secsize,
2831 /*sense_len*/SSD_FULL_SIZE,
2832 da_default_timeout * 1000);
2833 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2834 ccb->ccb_h.flags |= CAM_UNLOCKED;
2838 cmd6workaround(union ccb *ccb)
2840 struct scsi_rw_6 cmd6;
2841 struct scsi_rw_10 *cmd10;
2842 struct da_softc *softc;
2847 cdb = ccb->csio.cdb_io.cdb_bytes;
2848 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
2850 if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
2851 da_delete_methods old_method = softc->delete_method;
2854 * Typically there are two reasons for failure here
2855 * 1. Delete method was detected as supported but isn't
2856 * 2. Delete failed due to invalid params e.g. too big
2858 * While we will attempt to choose an alternative delete method
2859 * this may result in short deletes if the existing delete
2860 * requests from geom are big for the new method choosen.
2862 * This method assumes that the error which triggered this
2863 * will not retry the io otherwise a panic will occur
2865 dadeleteflag(softc, old_method, 0);
2866 dadeletemethodchoose(softc, DA_DELETE_DISABLE);
2867 if (softc->delete_method == DA_DELETE_DISABLE)
2868 xpt_print(ccb->ccb_h.path,
2869 "%s failed, disabling BIO_DELETE\n",
2870 da_delete_method_desc[old_method]);
2872 xpt_print(ccb->ccb_h.path,
2873 "%s failed, switching to %s BIO_DELETE\n",
2874 da_delete_method_desc[old_method],
2875 da_delete_method_desc[softc->delete_method]);
2877 while ((bp = bioq_takefirst(&softc->delete_run_queue)) != NULL)
2878 bioq_disksort(&softc->delete_queue, bp);
2879 bioq_disksort(&softc->delete_queue,
2880 (struct bio *)ccb->ccb_h.ccb_bp);
2881 ccb->ccb_h.ccb_bp = NULL;
2885 /* Detect unsupported PREVENT ALLOW MEDIUM REMOVAL. */
2886 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
2887 (*cdb == PREVENT_ALLOW) &&
2888 (softc->quirks & DA_Q_NO_PREVENT) == 0) {
2890 xpt_print(ccb->ccb_h.path,
2891 "PREVENT ALLOW MEDIUM REMOVAL not supported.\n");
2892 softc->quirks |= DA_Q_NO_PREVENT;
2896 /* Detect unsupported SYNCHRONIZE CACHE(10). */
2897 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
2898 (*cdb == SYNCHRONIZE_CACHE) &&
2899 (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
2901 xpt_print(ccb->ccb_h.path,
2902 "SYNCHRONIZE CACHE(10) not supported.\n");
2903 softc->quirks |= DA_Q_NO_SYNC_CACHE;
2904 softc->disk->d_flags &= ~DISKFLAG_CANFLUSHCACHE;
2908 /* Translation only possible if CDB is an array and cmd is R/W6 */
2909 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
2910 (*cdb != READ_6 && *cdb != WRITE_6))
2913 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
2914 "increasing minimum_cmd_size to 10.\n");
2915 softc->minimum_cmd_size = 10;
2917 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
2918 cmd10 = (struct scsi_rw_10 *)cdb;
2919 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
2921 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
2922 cmd10->reserved = 0;
2923 scsi_ulto2b(cmd6.length, cmd10->length);
2924 cmd10->control = cmd6.control;
2925 ccb->csio.cdb_len = sizeof(*cmd10);
2927 /* Requeue request, unfreezing queue if necessary */
2928 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
2929 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2932 cam_release_devq(ccb->ccb_h.path,
2936 /*getcount_only*/0);
2942 dadone(struct cam_periph *periph, union ccb *done_ccb)
2944 struct da_softc *softc;
2945 struct ccb_scsiio *csio;
2949 softc = (struct da_softc *)periph->softc;
2950 priority = done_ccb->ccb_h.pinfo.priority;
2952 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
2954 csio = &done_ccb->csio;
2955 state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
2957 case DA_CCB_BUFFER_IO:
2960 struct bio *bp, *bp1;
2962 cam_periph_lock(periph);
2963 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2964 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2968 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
2973 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
2974 if (error == ERESTART) {
2976 * A retry was scheduled, so
2979 cam_periph_unlock(periph);
2982 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2987 * return all queued I/O with EIO, so that
2988 * the client can retry these I/Os in the
2989 * proper order should it attempt to recover.
2994 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
2996 * Catastrophic error. Mark our pack as
3000 * XXX See if this is really a media
3003 xpt_print(periph->path,
3004 "Invalidating pack\n");
3005 softc->flags |= DA_FLAG_PACK_INVALID;
3006 queued_error = ENXIO;
3008 bioq_flush(&softc->bio_queue, NULL,
3011 bp->bio_error = error;
3012 bp->bio_resid = bp->bio_bcount;
3013 bp->bio_flags |= BIO_ERROR;
3015 } else if (bp != NULL) {
3016 if (state == DA_CCB_DELETE)
3019 bp->bio_resid = csio->resid;
3021 if (bp->bio_resid != 0)
3022 bp->bio_flags |= BIO_ERROR;
3024 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3025 cam_release_devq(done_ccb->ccb_h.path,
3029 /*getcount_only*/0);
3030 } else if (bp != NULL) {
3031 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3032 panic("REQ_CMP with QFRZN");
3033 if (state == DA_CCB_DELETE)
3036 bp->bio_resid = csio->resid;
3037 if (csio->resid > 0)
3038 bp->bio_flags |= BIO_ERROR;
3039 if (softc->error_inject != 0) {
3040 bp->bio_error = softc->error_inject;
3041 bp->bio_resid = bp->bio_bcount;
3042 bp->bio_flags |= BIO_ERROR;
3043 softc->error_inject = 0;
3047 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
3048 if (LIST_EMPTY(&softc->pending_ccbs))
3049 softc->flags |= DA_FLAG_WAS_OTAG;
3051 xpt_release_ccb(done_ccb);
3052 if (state == DA_CCB_DELETE) {
3053 TAILQ_HEAD(, bio) queue;
3056 TAILQ_CONCAT(&queue, &softc->delete_run_queue.queue, bio_queue);
3057 softc->delete_run_queue.insert_point = NULL;
3059 * Normally, the xpt_release_ccb() above would make sure
3060 * that when we have more work to do, that work would
3061 * get kicked off. However, we specifically keep
3062 * delete_running set to 0 before the call above to
3063 * allow other I/O to progress when many BIO_DELETE
3064 * requests are pushed down. We set delete_running to 0
3065 * and call daschedule again so that we don't stall if
3066 * there are no other I/Os pending apart from BIO_DELETEs.
3068 softc->delete_running = 0;
3070 cam_periph_unlock(periph);
3071 while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
3072 TAILQ_REMOVE(&queue, bp1, bio_queue);
3073 bp1->bio_error = bp->bio_error;
3074 if (bp->bio_flags & BIO_ERROR) {
3075 bp1->bio_flags |= BIO_ERROR;
3076 bp1->bio_resid = bp1->bio_bcount;
3082 cam_periph_unlock(periph);
3087 case DA_CCB_PROBE_RC:
3088 case DA_CCB_PROBE_RC16:
3090 struct scsi_read_capacity_data *rdcap;
3091 struct scsi_read_capacity_data_long *rcaplong;
3092 char announce_buf[80];
3098 if (state == DA_CCB_PROBE_RC)
3099 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
3101 rcaplong = (struct scsi_read_capacity_data_long *)
3104 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3105 struct disk_params *dp;
3106 uint32_t block_size;
3108 u_int lalba; /* Lowest aligned LBA. */
3110 if (state == DA_CCB_PROBE_RC) {
3111 block_size = scsi_4btoul(rdcap->length);
3112 maxsector = scsi_4btoul(rdcap->addr);
3116 * According to SBC-2, if the standard 10
3117 * byte READ CAPACITY command returns 2^32,
3118 * we should issue the 16 byte version of
3119 * the command, since the device in question
3120 * has more sectors than can be represented
3121 * with the short version of the command.
3123 if (maxsector == 0xffffffff) {
3124 free(rdcap, M_SCSIDA);
3125 xpt_release_ccb(done_ccb);
3126 softc->state = DA_STATE_PROBE_RC16;
3127 xpt_schedule(periph, priority);
3131 block_size = scsi_4btoul(rcaplong->length);
3132 maxsector = scsi_8btou64(rcaplong->addr);
3133 lalba = scsi_2btoul(rcaplong->lalba_lbp);
3137 * Because GEOM code just will panic us if we
3138 * give them an 'illegal' value we'll avoid that
3141 if (block_size == 0) {
3146 if (block_size >= MAXPHYS) {
3147 xpt_print(periph->path,
3148 "unsupportable block size %ju\n",
3149 (uintmax_t) block_size);
3150 announce_buf[0] = '\0';
3151 cam_periph_invalidate(periph);
3154 * We pass rcaplong into dasetgeom(),
3155 * because it will only use it if it is
3158 dasetgeom(periph, block_size, maxsector,
3159 rcaplong, sizeof(*rcaplong));
3160 lbp = (lalba & SRC16_LBPME_A);
3161 dp = &softc->params;
3162 snprintf(announce_buf, sizeof(announce_buf),
3163 "%juMB (%ju %u byte sectors)",
3164 ((uintmax_t)dp->secsize * dp->sectors) /
3166 (uintmax_t)dp->sectors, dp->secsize);
3171 announce_buf[0] = '\0';
3174 * Retry any UNIT ATTENTION type errors. They
3175 * are expected at boot.
3177 error = daerror(done_ccb, CAM_RETRY_SELTO,
3178 SF_RETRY_UA|SF_NO_PRINT);
3179 if (error == ERESTART) {
3181 * A retry was scheuled, so
3185 } else if (error != 0) {
3187 int sense_key, error_code;
3190 struct ccb_getdev cgd;
3192 /* Don't wedge this device's queue */
3193 status = done_ccb->ccb_h.status;
3194 if ((status & CAM_DEV_QFRZN) != 0)
3195 cam_release_devq(done_ccb->ccb_h.path,
3199 /*getcount_only*/0);
3202 xpt_setup_ccb(&cgd.ccb_h,
3203 done_ccb->ccb_h.path,
3204 CAM_PRIORITY_NORMAL);
3205 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
3206 xpt_action((union ccb *)&cgd);
3208 if (scsi_extract_sense_ccb(done_ccb,
3209 &error_code, &sense_key, &asc, &ascq))
3215 * If we tried READ CAPACITY(16) and failed,
3216 * fallback to READ CAPACITY(10).
3218 if ((state == DA_CCB_PROBE_RC16) &&
3219 (softc->flags & DA_FLAG_CAN_RC16) &&
3220 (((csio->ccb_h.status & CAM_STATUS_MASK) ==
3223 (error_code == SSD_CURRENT_ERROR) &&
3224 (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
3225 softc->flags &= ~DA_FLAG_CAN_RC16;
3226 free(rdcap, M_SCSIDA);
3227 xpt_release_ccb(done_ccb);
3228 softc->state = DA_STATE_PROBE_RC;
3229 xpt_schedule(periph, priority);
3234 * Attach to anything that claims to be a
3235 * direct access or optical disk device,
3236 * as long as it doesn't return a "Logical
3237 * unit not supported" (0x25) error.
3239 if ((have_sense) && (asc != 0x25)
3240 && (error_code == SSD_CURRENT_ERROR)) {
3241 const char *sense_key_desc;
3242 const char *asc_desc;
3244 dasetgeom(periph, 512, -1, NULL, 0);
3245 scsi_sense_desc(sense_key, asc, ascq,
3249 snprintf(announce_buf,
3250 sizeof(announce_buf),
3251 "Attempt to query device "
3252 "size failed: %s, %s",
3260 xpt_print(periph->path,
3261 "got CAM status %#x\n",
3262 done_ccb->ccb_h.status);
3265 xpt_print(periph->path, "fatal error, "
3266 "failed to attach to device\n");
3269 * Free up resources.
3271 cam_periph_invalidate(periph);
3275 free(csio->data_ptr, M_SCSIDA);
3276 if (announce_buf[0] != '\0' &&
3277 ((softc->flags & DA_FLAG_ANNOUNCED) == 0)) {
3279 * Create our sysctl variables, now that we know
3280 * we have successfully attached.
3282 /* increase the refcount */
3283 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3284 taskqueue_enqueue(taskqueue_thread,
3285 &softc->sysctl_task);
3286 xpt_announce_periph(periph, announce_buf);
3287 xpt_announce_quirks(periph, softc->quirks,
3290 xpt_print(periph->path, "fatal error, "
3291 "could not acquire reference count\n");
3295 /* We already probed the device. */
3296 if (softc->flags & DA_FLAG_PROBED) {
3297 daprobedone(periph, done_ccb);
3301 /* Ensure re-probe doesn't see old delete. */
3302 softc->delete_available = 0;
3303 dadeleteflag(softc, DA_DELETE_ZERO, 1);
3304 if (lbp && (softc->quirks & DA_Q_NO_UNMAP) == 0) {
3306 * Based on older SBC-3 spec revisions
3307 * any of the UNMAP methods "may" be
3308 * available via LBP given this flag so
3309 * we flag all of them as availble and
3310 * then remove those which further
3311 * probes confirm aren't available
3314 * We could also check readcap(16) p_type
3315 * flag to exclude one or more invalid
3316 * write same (X) types here
3318 dadeleteflag(softc, DA_DELETE_WS16, 1);
3319 dadeleteflag(softc, DA_DELETE_WS10, 1);
3320 dadeleteflag(softc, DA_DELETE_UNMAP, 1);
3322 xpt_release_ccb(done_ccb);
3323 softc->state = DA_STATE_PROBE_LBP;
3324 xpt_schedule(periph, priority);
3328 xpt_release_ccb(done_ccb);
3329 softc->state = DA_STATE_PROBE_BDC;
3330 xpt_schedule(periph, priority);
3333 case DA_CCB_PROBE_LBP:
3335 struct scsi_vpd_logical_block_prov *lbp;
3337 lbp = (struct scsi_vpd_logical_block_prov *)csio->data_ptr;
3339 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3341 * T10/1799-D Revision 31 states at least one of these
3342 * must be supported but we don't currently enforce this.
3344 dadeleteflag(softc, DA_DELETE_WS16,
3345 (lbp->flags & SVPD_LBP_WS16));
3346 dadeleteflag(softc, DA_DELETE_WS10,
3347 (lbp->flags & SVPD_LBP_WS10));
3348 dadeleteflag(softc, DA_DELETE_UNMAP,
3349 (lbp->flags & SVPD_LBP_UNMAP));
3352 error = daerror(done_ccb, CAM_RETRY_SELTO,
3353 SF_RETRY_UA|SF_NO_PRINT);
3354 if (error == ERESTART)
3356 else if (error != 0) {
3357 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3358 /* Don't wedge this device's queue */
3359 cam_release_devq(done_ccb->ccb_h.path,
3363 /*getcount_only*/0);
3367 * Failure indicates we don't support any SBC-3
3368 * delete methods with UNMAP
3373 free(lbp, M_SCSIDA);
3374 xpt_release_ccb(done_ccb);
3375 softc->state = DA_STATE_PROBE_BLK_LIMITS;
3376 xpt_schedule(periph, priority);
3379 case DA_CCB_PROBE_BLK_LIMITS:
3381 struct scsi_vpd_block_limits *block_limits;
3383 block_limits = (struct scsi_vpd_block_limits *)csio->data_ptr;
3385 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3386 uint32_t max_txfer_len = scsi_4btoul(
3387 block_limits->max_txfer_len);
3388 uint32_t max_unmap_lba_cnt = scsi_4btoul(
3389 block_limits->max_unmap_lba_cnt);
3390 uint32_t max_unmap_blk_cnt = scsi_4btoul(
3391 block_limits->max_unmap_blk_cnt);
3392 uint64_t ws_max_blks = scsi_8btou64(
3393 block_limits->max_write_same_length);
3395 if (max_txfer_len != 0) {
3396 softc->disk->d_maxsize = MIN(softc->maxio,
3397 (off_t)max_txfer_len * softc->params.secsize);
3401 * We should already support UNMAP but we check lba
3402 * and block count to be sure
3404 if (max_unmap_lba_cnt != 0x00L &&
3405 max_unmap_blk_cnt != 0x00L) {
3406 softc->unmap_max_lba = max_unmap_lba_cnt;
3407 softc->unmap_max_ranges = min(max_unmap_blk_cnt,
3411 * Unexpected UNMAP limits which means the
3412 * device doesn't actually support UNMAP
3414 dadeleteflag(softc, DA_DELETE_UNMAP, 0);
3417 if (ws_max_blks != 0x00L)
3418 softc->ws_max_blks = ws_max_blks;
3421 error = daerror(done_ccb, CAM_RETRY_SELTO,
3422 SF_RETRY_UA|SF_NO_PRINT);
3423 if (error == ERESTART)
3425 else if (error != 0) {
3426 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3427 /* Don't wedge this device's queue */
3428 cam_release_devq(done_ccb->ccb_h.path,
3432 /*getcount_only*/0);
3436 * Failure here doesn't mean UNMAP is not
3437 * supported as this is an optional page.
3439 softc->unmap_max_lba = 1;
3440 softc->unmap_max_ranges = 1;
3444 free(block_limits, M_SCSIDA);
3445 xpt_release_ccb(done_ccb);
3446 softc->state = DA_STATE_PROBE_BDC;
3447 xpt_schedule(periph, priority);
3450 case DA_CCB_PROBE_BDC:
3452 struct scsi_vpd_block_characteristics *bdc;
3454 bdc = (struct scsi_vpd_block_characteristics *)csio->data_ptr;
3456 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3458 * Disable queue sorting for non-rotational media
3461 u_int16_t old_rate = softc->disk->d_rotation_rate;
3463 softc->disk->d_rotation_rate =
3464 scsi_2btoul(bdc->medium_rotation_rate);
3465 if (softc->disk->d_rotation_rate ==
3466 SVPD_BDC_RATE_NON_ROTATING) {
3467 softc->sort_io_queue = 0;
3469 if (softc->disk->d_rotation_rate != old_rate) {
3470 disk_attr_changed(softc->disk,
3471 "GEOM::rotation_rate", M_NOWAIT);
3475 error = daerror(done_ccb, CAM_RETRY_SELTO,
3476 SF_RETRY_UA|SF_NO_PRINT);
3477 if (error == ERESTART)
3479 else if (error != 0) {
3480 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3481 /* Don't wedge this device's queue */
3482 cam_release_devq(done_ccb->ccb_h.path,
3486 /*getcount_only*/0);
3491 free(bdc, M_SCSIDA);
3492 xpt_release_ccb(done_ccb);
3493 softc->state = DA_STATE_PROBE_ATA;
3494 xpt_schedule(periph, priority);
3497 case DA_CCB_PROBE_ATA:
3500 struct ata_params *ata_params;
3503 ata_params = (struct ata_params *)csio->data_ptr;
3504 ptr = (uint16_t *)ata_params;
3506 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3509 for (i = 0; i < sizeof(*ata_params) / 2; i++)
3510 ptr[i] = le16toh(ptr[i]);
3511 if (ata_params->support_dsm & ATA_SUPPORT_DSM_TRIM &&
3512 (softc->quirks & DA_Q_NO_UNMAP) == 0) {
3513 dadeleteflag(softc, DA_DELETE_ATA_TRIM, 1);
3514 if (ata_params->max_dsm_blocks != 0)
3515 softc->trim_max_ranges = min(
3516 softc->trim_max_ranges,
3517 ata_params->max_dsm_blocks *
3518 ATA_DSM_BLK_RANGES);
3521 * Disable queue sorting for non-rotational media
3524 old_rate = softc->disk->d_rotation_rate;
3525 softc->disk->d_rotation_rate =
3526 ata_params->media_rotation_rate;
3527 if (softc->disk->d_rotation_rate ==
3528 ATA_RATE_NON_ROTATING) {
3529 softc->sort_io_queue = 0;
3532 if (softc->disk->d_rotation_rate != old_rate) {
3533 disk_attr_changed(softc->disk,
3534 "GEOM::rotation_rate", M_NOWAIT);
3538 error = daerror(done_ccb, CAM_RETRY_SELTO,
3539 SF_RETRY_UA|SF_NO_PRINT);
3540 if (error == ERESTART)
3542 else if (error != 0) {
3543 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3544 /* Don't wedge this device's queue */
3545 cam_release_devq(done_ccb->ccb_h.path,
3549 /*getcount_only*/0);
3554 free(ata_params, M_SCSIDA);
3555 daprobedone(periph, done_ccb);
3559 /* No-op. We're polling */
3563 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3565 if (daerror(done_ccb, CAM_RETRY_SELTO,
3566 SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
3569 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3570 cam_release_devq(done_ccb->ccb_h.path,
3574 /*getcount_only*/0);
3576 xpt_release_ccb(done_ccb);
3577 cam_periph_release_locked(periph);
3583 xpt_release_ccb(done_ccb);
3587 dareprobe(struct cam_periph *periph)
3589 struct da_softc *softc;
3592 softc = (struct da_softc *)periph->softc;
3594 /* Probe in progress; don't interfere. */
3595 if (softc->state != DA_STATE_NORMAL)
3598 status = cam_periph_acquire(periph);
3599 KASSERT(status == CAM_REQ_CMP,
3600 ("dareprobe: cam_periph_acquire failed"));
3602 if (softc->flags & DA_FLAG_CAN_RC16)
3603 softc->state = DA_STATE_PROBE_RC16;
3605 softc->state = DA_STATE_PROBE_RC;
3607 xpt_schedule(periph, CAM_PRIORITY_DEV);
3611 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3613 struct da_softc *softc;
3614 struct cam_periph *periph;
3615 int error, error_code, sense_key, asc, ascq;
3617 periph = xpt_path_periph(ccb->ccb_h.path);
3618 softc = (struct da_softc *)periph->softc;
3621 * Automatically detect devices that do not support
3622 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
3625 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3626 error = cmd6workaround(ccb);
3627 } else if (scsi_extract_sense_ccb(ccb,
3628 &error_code, &sense_key, &asc, &ascq)) {
3629 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3630 error = cmd6workaround(ccb);
3632 * If the target replied with CAPACITY DATA HAS CHANGED UA,
3633 * query the capacity and notify upper layers.
3635 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3636 asc == 0x2A && ascq == 0x09) {
3637 xpt_print(periph->path, "Capacity data has changed\n");
3638 softc->flags &= ~DA_FLAG_PROBED;
3640 sense_flags |= SF_NO_PRINT;
3641 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3642 asc == 0x28 && ascq == 0x00) {
3643 softc->flags &= ~DA_FLAG_PROBED;
3644 disk_media_changed(softc->disk, M_NOWAIT);
3645 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3646 asc == 0x3F && ascq == 0x03) {
3647 xpt_print(periph->path, "INQUIRY data has changed\n");
3648 softc->flags &= ~DA_FLAG_PROBED;
3650 sense_flags |= SF_NO_PRINT;
3651 } else if (sense_key == SSD_KEY_NOT_READY &&
3652 asc == 0x3a && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
3653 softc->flags |= DA_FLAG_PACK_INVALID;
3654 disk_media_gone(softc->disk, M_NOWAIT);
3657 if (error == ERESTART)
3662 * Until we have a better way of doing pack validation,
3663 * don't treat UAs as errors.
3665 sense_flags |= SF_RETRY_UA;
3667 if (softc->quirks & DA_Q_RETRY_BUSY)
3668 sense_flags |= SF_RETRY_BUSY;
3669 return(cam_periph_error(ccb, cam_flags, sense_flags,
3670 &softc->saved_ccb));
3674 damediapoll(void *arg)
3676 struct cam_periph *periph = arg;
3677 struct da_softc *softc = periph->softc;
3679 if (!softc->tur && LIST_EMPTY(&softc->pending_ccbs)) {
3680 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3685 /* Queue us up again */
3686 if (da_poll_period != 0)
3687 callout_schedule(&softc->mediapoll_c, da_poll_period * hz);
3691 daprevent(struct cam_periph *periph, int action)
3693 struct da_softc *softc;
3697 softc = (struct da_softc *)periph->softc;
3699 if (((action == PR_ALLOW)
3700 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
3701 || ((action == PR_PREVENT)
3702 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
3706 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3708 scsi_prevent(&ccb->csio,
3716 error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
3717 SF_RETRY_UA | SF_NO_PRINT, softc->disk->d_devstat);
3720 if (action == PR_ALLOW)
3721 softc->flags &= ~DA_FLAG_PACK_LOCKED;
3723 softc->flags |= DA_FLAG_PACK_LOCKED;
3726 xpt_release_ccb(ccb);
3730 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
3731 struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
3733 struct ccb_calc_geometry ccg;
3734 struct da_softc *softc;
3735 struct disk_params *dp;
3736 u_int lbppbe, lalba;
3739 softc = (struct da_softc *)periph->softc;
3741 dp = &softc->params;
3742 dp->secsize = block_len;
3743 dp->sectors = maxsector + 1;
3744 if (rcaplong != NULL) {
3745 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
3746 lalba = scsi_2btoul(rcaplong->lalba_lbp);
3747 lalba &= SRC16_LALBA_A;
3754 dp->stripesize = block_len << lbppbe;
3755 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
3757 } else if (softc->quirks & DA_Q_4K) {
3758 dp->stripesize = 4096;
3759 dp->stripeoffset = 0;
3762 dp->stripeoffset = 0;
3765 * Have the controller provide us with a geometry
3766 * for this disk. The only time the geometry
3767 * matters is when we boot and the controller
3768 * is the only one knowledgeable enough to come
3769 * up with something that will make this a bootable
3772 xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3773 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
3774 ccg.block_size = dp->secsize;
3775 ccg.volume_size = dp->sectors;
3777 ccg.secs_per_track = 0;
3779 xpt_action((union ccb*)&ccg);
3780 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3782 * We don't know what went wrong here- but just pick
3783 * a geometry so we don't have nasty things like divide
3787 dp->secs_per_track = 255;
3788 dp->cylinders = dp->sectors / (255 * 255);
3789 if (dp->cylinders == 0) {
3793 dp->heads = ccg.heads;
3794 dp->secs_per_track = ccg.secs_per_track;
3795 dp->cylinders = ccg.cylinders;
3799 * If the user supplied a read capacity buffer, and if it is
3800 * different than the previous buffer, update the data in the EDT.
3801 * If it's the same, we don't bother. This avoids sending an
3802 * update every time someone opens this device.
3804 if ((rcaplong != NULL)
3805 && (bcmp(rcaplong, &softc->rcaplong,
3806 min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
3807 struct ccb_dev_advinfo cdai;
3809 xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3810 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
3811 cdai.buftype = CDAI_TYPE_RCAPLONG;
3812 cdai.flags = CDAI_FLAG_STORE;
3813 cdai.bufsiz = rcap_len;
3814 cdai.buf = (uint8_t *)rcaplong;
3815 xpt_action((union ccb *)&cdai);
3816 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
3817 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
3818 if (cdai.ccb_h.status != CAM_REQ_CMP) {
3819 xpt_print(periph->path, "%s: failed to set read "
3820 "capacity advinfo\n", __func__);
3821 /* Use cam_error_print() to decode the status */
3822 cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
3825 bcopy(rcaplong, &softc->rcaplong,
3826 min(sizeof(softc->rcaplong), rcap_len));
3830 softc->disk->d_sectorsize = softc->params.secsize;
3831 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
3832 softc->disk->d_stripesize = softc->params.stripesize;
3833 softc->disk->d_stripeoffset = softc->params.stripeoffset;
3834 /* XXX: these are not actually "firmware" values, so they may be wrong */
3835 softc->disk->d_fwsectors = softc->params.secs_per_track;
3836 softc->disk->d_fwheads = softc->params.heads;
3837 softc->disk->d_devstat->block_size = softc->params.secsize;
3838 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
3840 error = disk_resize(softc->disk, M_NOWAIT);
3842 xpt_print(periph->path, "disk_resize(9) failed, error = %d\n", error);
3846 dasendorderedtag(void *arg)
3848 struct da_softc *softc = arg;
3850 if (da_send_ordered) {
3851 if (!LIST_EMPTY(&softc->pending_ccbs)) {
3852 if ((softc->flags & DA_FLAG_WAS_OTAG) == 0)
3853 softc->flags |= DA_FLAG_NEED_OTAG;
3854 softc->flags &= ~DA_FLAG_WAS_OTAG;
3857 /* Queue us up again */
3858 callout_reset(&softc->sendordered_c,
3859 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
3860 dasendorderedtag, softc);
3864 * Step through all DA peripheral drivers, and if the device is still open,
3865 * sync the disk cache to physical media.
3868 dashutdown(void * arg, int howto)
3870 struct cam_periph *periph;
3871 struct da_softc *softc;
3875 CAM_PERIPH_FOREACH(periph, &dadriver) {
3876 softc = (struct da_softc *)periph->softc;
3877 if (SCHEDULER_STOPPED()) {
3878 /* If we paniced with the lock held, do not recurse. */
3879 if (!cam_periph_owned(periph) &&
3880 (softc->flags & DA_FLAG_OPEN)) {
3881 dadump(softc->disk, NULL, 0, 0, 0);
3885 cam_periph_lock(periph);
3888 * We only sync the cache if the drive is still open, and
3889 * if the drive is capable of it..
3891 if (((softc->flags & DA_FLAG_OPEN) == 0)
3892 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
3893 cam_periph_unlock(periph);
3897 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3898 scsi_synchronize_cache(&ccb->csio,
3902 /*begin_lba*/0, /* whole disk */
3907 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
3908 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR,
3909 softc->disk->d_devstat);
3911 xpt_print(periph->path, "Synchronize cache failed\n");
3912 xpt_release_ccb(ccb);
3913 cam_periph_unlock(periph);
3917 #else /* !_KERNEL */
3920 * XXX These are only left out of the kernel build to silence warnings. If,
3921 * for some reason these functions are used in the kernel, the ifdefs should
3922 * be moved so they are included both in the kernel and userland.
3925 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
3926 void (*cbfcnp)(struct cam_periph *, union ccb *),
3927 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
3928 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
3931 struct scsi_format_unit *scsi_cmd;
3933 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
3934 scsi_cmd->opcode = FORMAT_UNIT;
3935 scsi_cmd->byte2 = byte2;
3936 scsi_ulto2b(ileave, scsi_cmd->interleave);
3941 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
3951 scsi_read_defects(struct ccb_scsiio *csio, uint32_t retries,
3952 void (*cbfcnp)(struct cam_periph *, union ccb *),
3953 uint8_t tag_action, uint8_t list_format,
3954 uint32_t addr_desc_index, uint8_t *data_ptr,
3955 uint32_t dxfer_len, int minimum_cmd_size,
3956 uint8_t sense_len, uint32_t timeout)
3961 * These conditions allow using the 10 byte command. Otherwise we
3962 * need to use the 12 byte command.
3964 if ((minimum_cmd_size <= 10)
3965 && (addr_desc_index == 0)
3966 && (dxfer_len <= SRDD10_MAX_LENGTH)) {
3967 struct scsi_read_defect_data_10 *cdb10;
3969 cdb10 = (struct scsi_read_defect_data_10 *)
3970 &csio->cdb_io.cdb_bytes;
3972 cdb_len = sizeof(*cdb10);
3973 bzero(cdb10, cdb_len);
3974 cdb10->opcode = READ_DEFECT_DATA_10;
3975 cdb10->format = list_format;
3976 scsi_ulto2b(dxfer_len, cdb10->alloc_length);
3978 struct scsi_read_defect_data_12 *cdb12;
3980 cdb12 = (struct scsi_read_defect_data_12 *)
3981 &csio->cdb_io.cdb_bytes;
3983 cdb_len = sizeof(*cdb12);
3984 bzero(cdb12, cdb_len);
3985 cdb12->opcode = READ_DEFECT_DATA_12;
3986 cdb12->format = list_format;
3987 scsi_ulto4b(dxfer_len, cdb12->alloc_length);
3988 scsi_ulto4b(addr_desc_index, cdb12->address_descriptor_index);
3994 /*flags*/ CAM_DIR_IN,
4004 scsi_sanitize(struct ccb_scsiio *csio, u_int32_t retries,
4005 void (*cbfcnp)(struct cam_periph *, union ccb *),
4006 u_int8_t tag_action, u_int8_t byte2, u_int16_t control,
4007 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4010 struct scsi_sanitize *scsi_cmd;
4012 scsi_cmd = (struct scsi_sanitize *)&csio->cdb_io.cdb_bytes;
4013 scsi_cmd->opcode = SANITIZE;
4014 scsi_cmd->byte2 = byte2;
4015 scsi_cmd->control = control;
4016 scsi_ulto2b(dxfer_len, scsi_cmd->length);
4021 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
4030 #endif /* _KERNEL */