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;
223 da_delete_func_t *delete_func;
224 struct disk_params params;
227 struct task sysctl_task;
228 struct sysctl_ctx_list sysctl_ctx;
229 struct sysctl_oid *sysctl_tree;
230 struct callout sendordered_c;
232 uint8_t unmap_buf[UNMAP_BUF_SIZE];
233 struct scsi_read_capacity_data_long rcaplong;
234 struct callout mediapoll_c;
237 #define dadeleteflag(softc, delete_method, enable) \
239 softc->delete_available |= (1 << delete_method); \
241 softc->delete_available &= ~(1 << delete_method); \
244 struct da_quirk_entry {
245 struct scsi_inquiry_pattern inq_pat;
249 static const char quantum[] = "QUANTUM";
250 static const char microp[] = "MICROP";
252 static struct da_quirk_entry da_quirk_table[] =
254 /* SPI, FC devices */
257 * Fujitsu M2513A MO drives.
258 * Tested devices: M2513A2 firmware versions 1200 & 1300.
259 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
260 * Reported by: W.Scholten <whs@xs4all.nl>
262 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
263 /*quirks*/ DA_Q_NO_SYNC_CACHE
267 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
268 /*quirks*/ DA_Q_NO_SYNC_CACHE
272 * This particular Fujitsu drive doesn't like the
273 * synchronize cache command.
274 * Reported by: Tom Jackson <toj@gorilla.net>
276 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
277 /*quirks*/ DA_Q_NO_SYNC_CACHE
281 * This drive doesn't like the synchronize cache command
282 * either. Reported by: Matthew Jacob <mjacob@feral.com>
283 * in NetBSD PR kern/6027, August 24, 1998.
285 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
286 /*quirks*/ DA_Q_NO_SYNC_CACHE
290 * This drive doesn't like the synchronize cache command
291 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
294 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
295 /*quirks*/ DA_Q_NO_SYNC_CACHE
299 * Doesn't like the synchronize cache command.
300 * Reported by: Blaz Zupan <blaz@gold.amis.net>
302 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
303 /*quirks*/ DA_Q_NO_SYNC_CACHE
307 * Doesn't like the synchronize cache command.
308 * Reported by: Blaz Zupan <blaz@gold.amis.net>
310 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
311 /*quirks*/ DA_Q_NO_SYNC_CACHE
315 * Doesn't like the synchronize cache command.
317 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
318 /*quirks*/ DA_Q_NO_SYNC_CACHE
322 * Doesn't like the synchronize cache command.
323 * Reported by: walter@pelissero.de
325 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
326 /*quirks*/ DA_Q_NO_SYNC_CACHE
330 * Doesn't work correctly with 6 byte reads/writes.
331 * Returns illegal request, and points to byte 9 of the
333 * Reported by: Adam McDougall <bsdx@spawnet.com>
335 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
336 /*quirks*/ DA_Q_NO_6_BYTE
340 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
341 /*quirks*/ DA_Q_NO_6_BYTE
345 * Doesn't like the synchronize cache command.
346 * Reported by: walter@pelissero.de
348 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
349 /*quirks*/ DA_Q_NO_SYNC_CACHE
353 * The CISS RAID controllers do not support SYNC_CACHE
355 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
356 /*quirks*/ DA_Q_NO_SYNC_CACHE
360 * The STEC SSDs sometimes hang on UNMAP.
362 {T_DIRECT, SIP_MEDIA_FIXED, "STEC", "*", "*"},
363 /*quirks*/ DA_Q_NO_UNMAP
367 * VMware returns BUSY status when storage has transient
368 * connectivity problems, so better wait.
370 {T_DIRECT, SIP_MEDIA_FIXED, "VMware", "Virtual disk", "*"},
371 /*quirks*/ DA_Q_RETRY_BUSY
373 /* USB mass storage devices supported by umass(4) */
376 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
379 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
380 /*quirks*/ DA_Q_NO_SYNC_CACHE
384 * Power Quotient Int. (PQI) USB flash key
387 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
388 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
392 * Creative Nomad MUVO mp3 player (USB)
395 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
396 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
400 * Jungsoft NEXDISK USB flash key
403 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
404 /*quirks*/ DA_Q_NO_SYNC_CACHE
408 * FreeDik USB Mini Data Drive
411 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
412 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
416 * Sigmatel USB Flash MP3 Player
419 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
420 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
424 * Neuros USB Digital Audio Computer
427 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
428 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
432 * SEAGRAND NP-900 MP3 Player
435 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
436 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
440 * iRiver iFP MP3 player (with UMS Firmware)
441 * PR: kern/54881, i386/63941, kern/66124
443 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
444 /*quirks*/ DA_Q_NO_SYNC_CACHE
448 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
451 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
452 /*quirks*/ DA_Q_NO_SYNC_CACHE
456 * ZICPlay USB MP3 Player with FM
459 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
460 /*quirks*/ DA_Q_NO_SYNC_CACHE
464 * TEAC USB floppy mechanisms
466 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
467 /*quirks*/ DA_Q_NO_SYNC_CACHE
471 * Kingston DataTraveler II+ USB Pen-Drive.
472 * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
474 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
475 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
483 {T_DIRECT, SIP_MEDIA_REMOVABLE, " ", "USB DISK Pro", "PMAP"},
484 /*quirks*/ DA_Q_NO_SYNC_CACHE
488 * Motorola E398 Mobile Phone (TransFlash memory card).
489 * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
492 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
493 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
497 * Qware BeatZkey! Pro
500 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
501 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
505 * Time DPA20B 1GB MP3 Player
508 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
509 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
513 * Samsung USB key 128Mb
516 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
517 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
521 * Kingston DataTraveler 2.0 USB Flash memory.
524 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
525 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
529 * Creative MUVO Slim mp3 player (USB)
532 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
533 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
537 * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
540 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
541 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
545 * SanDisk Micro Cruzer 128MB
548 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
549 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
553 * TOSHIBA TransMemory USB sticks
556 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
557 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
561 * PNY USB 3.0 Flash Drives
563 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PNY", "USB 3.0 FD*",
564 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_RC16
569 * PR: usb/75578, usb/72344, usb/65436
571 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
572 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
576 * Genesys 6-in-1 Card Reader
579 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
580 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
584 * Rekam Digital CAMERA
587 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
588 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
592 * iRiver H10 MP3 player
595 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
596 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
600 * iRiver U10 MP3 player
603 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
604 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
611 {T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
612 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
616 * EasyMP3 EM732X USB 2.0 Flash MP3 Player
619 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
620 "1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
627 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
628 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
632 * Philips USB Key Audio KEY013
635 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
636 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
643 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
644 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
651 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
652 /*quirks*/ DA_Q_NO_SYNC_CACHE
656 * I/O Magic USB flash - Giga Bank
659 {T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
660 /*quirks*/ DA_Q_NO_SYNC_CACHE
664 * JoyFly 128mb USB Flash Drive
667 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
668 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
675 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
676 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
680 * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A
683 {T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*",
684 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
688 * Samsung YP-U3 mp3-player
691 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3",
692 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
695 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
696 "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
700 * Sony Cyber-Shot DSC cameras
703 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
704 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
707 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler G3",
708 "1.00"}, /*quirks*/ DA_Q_NO_PREVENT
711 /* At least several Transcent USB sticks lie on RC16. */
712 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JetFlash", "Transcend*",
713 "*"}, /*quirks*/ DA_Q_NO_RC16
715 /* ATA/SATA devices over SAS/USB/... */
717 /* Hitachi Advanced Format (4k) drives */
718 { T_DIRECT, SIP_MEDIA_FIXED, "Hitachi", "H??????????E3*", "*" },
722 /* Samsung Advanced Format (4k) drives */
723 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD155UI*", "*" },
727 /* Samsung Advanced Format (4k) drives */
728 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD155UI*", "*" },
732 /* Samsung Advanced Format (4k) drives */
733 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD204UI*", "*" },
737 /* Samsung Advanced Format (4k) drives */
738 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD204UI*", "*" },
742 /* Seagate Barracuda Green Advanced Format (4k) drives */
743 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DL*", "*" },
747 /* Seagate Barracuda Green Advanced Format (4k) drives */
748 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DL", "*", "*" },
752 /* Seagate Barracuda Green Advanced Format (4k) drives */
753 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???DM*", "*" },
757 /* Seagate Barracuda Green Advanced Format (4k) drives */
758 { T_DIRECT, SIP_MEDIA_FIXED, "ST???DM*", "*", "*" },
762 /* Seagate Barracuda Green Advanced Format (4k) drives */
763 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DM*", "*" },
767 /* Seagate Barracuda Green Advanced Format (4k) drives */
768 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DM", "*", "*" },
772 /* Seagate Momentus Advanced Format (4k) drives */
773 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500423AS*", "*" },
777 /* Seagate Momentus Advanced Format (4k) drives */
778 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "3AS*", "*" },
782 /* Seagate Momentus Advanced Format (4k) drives */
783 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500424AS*", "*" },
787 /* Seagate Momentus Advanced Format (4k) drives */
788 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "4AS*", "*" },
792 /* Seagate Momentus Advanced Format (4k) drives */
793 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640423AS*", "*" },
797 /* Seagate Momentus Advanced Format (4k) drives */
798 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "3AS*", "*" },
802 /* Seagate Momentus Advanced Format (4k) drives */
803 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640424AS*", "*" },
807 /* Seagate Momentus Advanced Format (4k) drives */
808 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "4AS*", "*" },
812 /* Seagate Momentus Advanced Format (4k) drives */
813 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750420AS*", "*" },
817 /* Seagate Momentus Advanced Format (4k) drives */
818 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "0AS*", "*" },
822 /* Seagate Momentus Advanced Format (4k) drives */
823 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750422AS*", "*" },
827 /* Seagate Momentus Advanced Format (4k) drives */
828 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "2AS*", "*" },
832 /* Seagate Momentus Advanced Format (4k) drives */
833 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750423AS*", "*" },
837 /* Seagate Momentus Advanced Format (4k) drives */
838 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "3AS*", "*" },
842 /* Seagate Momentus Thin Advanced Format (4k) drives */
843 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???LT*", "*" },
847 /* Seagate Momentus Thin Advanced Format (4k) drives */
848 { T_DIRECT, SIP_MEDIA_FIXED, "ST???LT*", "*", "*" },
852 /* WDC Caviar Green Advanced Format (4k) drives */
853 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RS*", "*" },
857 /* WDC Caviar Green Advanced Format (4k) drives */
858 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RS*", "*" },
862 /* WDC Caviar Green Advanced Format (4k) drives */
863 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RX*", "*" },
867 /* WDC Caviar Green Advanced Format (4k) drives */
868 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RX*", "*" },
872 /* WDC Caviar Green Advanced Format (4k) drives */
873 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RS*", "*" },
877 /* WDC Caviar Green Advanced Format (4k) drives */
878 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RS*", "*" },
882 /* WDC Caviar Green Advanced Format (4k) drives */
883 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RX*", "*" },
887 /* WDC Caviar Green Advanced Format (4k) drives */
888 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RX*", "*" },
892 /* WDC Scorpio Black Advanced Format (4k) drives */
893 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PKT*", "*" },
897 /* WDC Scorpio Black Advanced Format (4k) drives */
898 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PKT*", "*" },
902 /* WDC Scorpio Black Advanced Format (4k) drives */
903 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PKT*", "*" },
907 /* WDC Scorpio Black Advanced Format (4k) drives */
908 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PKT*", "*" },
912 /* WDC Scorpio Blue Advanced Format (4k) drives */
913 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PVT*", "*" },
917 /* WDC Scorpio Blue Advanced Format (4k) drives */
918 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PVT*", "*" },
922 /* WDC Scorpio Blue Advanced Format (4k) drives */
923 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PVT*", "*" },
927 /* WDC Scorpio Blue Advanced Format (4k) drives */
928 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PVT*", "*" },
933 * Olympus FE-210 camera
935 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "FE210*",
936 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
942 {T_DIRECT, SIP_MEDIA_REMOVABLE, "LG", "UP3S",
943 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
947 * Laser MP3-2GA13 MP3 player
949 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "(HS) Flash Disk",
950 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
954 * LaCie external 250GB Hard drive des by Porsche
955 * Submitted by: Ben Stuyts <ben@altesco.nl>
958 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HM250JI", "*"},
959 /*quirks*/ DA_Q_NO_SYNC_CACHE
964 * Corsair Force 2 SSDs
965 * 4k optimised & trim only works in 4k requests + 4k aligned
967 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair CSSD-F*", "*" },
972 * Corsair Force 3 SSDs
973 * 4k optimised & trim only works in 4k requests + 4k aligned
975 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force 3*", "*" },
980 * Corsair Neutron GTX SSDs
981 * 4k optimised & trim only works in 4k requests + 4k aligned
983 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Neutron GTX*", "*" },
988 * Corsair Force GT & GS SSDs
989 * 4k optimised & trim only works in 4k requests + 4k aligned
991 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force G*", "*" },
997 * 4k optimised & trim only works in 4k requests + 4k aligned
999 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "M4-CT???M4SSD2*", "*" },
1004 * Crucial RealSSD C300 SSDs
1007 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "C300-CTFDDAC???MAG*",
1008 "*" }, /*quirks*/DA_Q_4K
1012 * Intel 320 Series SSDs
1013 * 4k optimised & trim only works in 4k requests + 4k aligned
1015 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2CW*", "*" },
1020 * Intel 330 Series SSDs
1021 * 4k optimised & trim only works in 4k requests + 4k aligned
1023 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2CT*", "*" },
1028 * Intel 510 Series SSDs
1029 * 4k optimised & trim only works in 4k requests + 4k aligned
1031 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2MH*", "*" },
1036 * Intel 520 Series SSDs
1037 * 4k optimised & trim only works in 4k requests + 4k aligned
1039 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2BW*", "*" },
1044 * Intel X25-M Series SSDs
1045 * 4k optimised & trim only works in 4k requests + 4k aligned
1047 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2M*", "*" },
1052 * Kingston E100 Series SSDs
1053 * 4k optimised & trim only works in 4k requests + 4k aligned
1055 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SE100S3*", "*" },
1060 * Kingston HyperX 3k SSDs
1061 * 4k optimised & trim only works in 4k requests + 4k aligned
1063 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SH103S3*", "*" },
1068 * Marvell SSDs (entry taken from OpenSolaris)
1069 * 4k optimised & trim only works in 4k requests + 4k aligned
1071 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "MARVELL SD88SA02*", "*" },
1076 * OCZ Agility 2 SSDs
1077 * 4k optimised & trim only works in 4k requests + 4k aligned
1079 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY2*", "*" },
1084 * OCZ Agility 3 SSDs
1085 * 4k optimised & trim only works in 4k requests + 4k aligned
1087 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-AGILITY3*", "*" },
1092 * OCZ Deneva R Series SSDs
1093 * 4k optimised & trim only works in 4k requests + 4k aligned
1095 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "DENRSTE251M45*", "*" },
1100 * OCZ Vertex 2 SSDs (inc pro series)
1101 * 4k optimised & trim only works in 4k requests + 4k aligned
1103 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ?VERTEX2*", "*" },
1109 * 4k optimised & trim only works in 4k requests + 4k aligned
1111 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX3*", "*" },
1117 * 4k optimised & trim only works in 4k requests + 4k aligned
1119 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX4*", "*" },
1124 * Samsung 830 Series SSDs
1125 * 4k optimised & trim only works in 4k requests + 4k aligned
1127 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG SSD 830 Series*", "*" },
1133 * 4k optimised & trim only works in 4k requests + 4k aligned
1135 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 840*", "*" },
1140 * Samsung 843T Series SSDs
1143 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG MZ7WD*", "*" },
1149 * 4k optimised & trim only works in 4k requests + 4k aligned
1151 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 850*", "*" },
1156 * Samsung PM853T Series SSDs
1159 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG MZ7GE*", "*" },
1164 * SuperTalent TeraDrive CT SSDs
1165 * 4k optimised & trim only works in 4k requests + 4k aligned
1167 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "FTM??CT25H*", "*" },
1172 * XceedIOPS SATA SSDs
1175 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SG9XCS2D*", "*" },
1180 * Hama Innostor USB-Stick
1182 { T_DIRECT, SIP_MEDIA_REMOVABLE, "Innostor", "Innostor*", "*" },
1183 /*quirks*/DA_Q_NO_RC16
1187 * MX-ES USB Drive by Mach Xtreme
1189 { T_DIRECT, SIP_MEDIA_REMOVABLE, "MX", "MXUB3*", "*"},
1190 /*quirks*/DA_Q_NO_RC16
1194 static disk_strategy_t dastrategy;
1195 static dumper_t dadump;
1196 static periph_init_t dainit;
1197 static void daasync(void *callback_arg, u_int32_t code,
1198 struct cam_path *path, void *arg);
1199 static void dasysctlinit(void *context, int pending);
1200 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
1201 static int dadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
1202 static int dadeletemaxsysctl(SYSCTL_HANDLER_ARGS);
1203 static void dadeletemethodset(struct da_softc *softc,
1204 da_delete_methods delete_method);
1205 static off_t dadeletemaxsize(struct da_softc *softc,
1206 da_delete_methods delete_method);
1207 static void dadeletemethodchoose(struct da_softc *softc,
1208 da_delete_methods default_method);
1209 static void daprobedone(struct cam_periph *periph, union ccb *ccb);
1211 static periph_ctor_t daregister;
1212 static periph_dtor_t dacleanup;
1213 static periph_start_t dastart;
1214 static periph_oninv_t daoninvalidate;
1215 static void dadone(struct cam_periph *periph,
1216 union ccb *done_ccb);
1217 static int daerror(union ccb *ccb, u_int32_t cam_flags,
1218 u_int32_t sense_flags);
1219 static void daprevent(struct cam_periph *periph, int action);
1220 static void dareprobe(struct cam_periph *periph);
1221 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
1223 struct scsi_read_capacity_data_long *rcaplong,
1225 static timeout_t dasendorderedtag;
1226 static void dashutdown(void *arg, int howto);
1227 static timeout_t damediapoll;
1229 #ifndef DA_DEFAULT_POLL_PERIOD
1230 #define DA_DEFAULT_POLL_PERIOD 3
1233 #ifndef DA_DEFAULT_TIMEOUT
1234 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
1237 #ifndef DA_DEFAULT_RETRY
1238 #define DA_DEFAULT_RETRY 4
1241 #ifndef DA_DEFAULT_SEND_ORDERED
1242 #define DA_DEFAULT_SEND_ORDERED 1
1245 #define DA_SIO (softc->sort_io_queue >= 0 ? \
1246 softc->sort_io_queue : cam_sort_io_queues)
1248 static int da_poll_period = DA_DEFAULT_POLL_PERIOD;
1249 static int da_retry_count = DA_DEFAULT_RETRY;
1250 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
1251 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
1253 static SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
1254 "CAM Direct Access Disk driver");
1255 SYSCTL_INT(_kern_cam_da, OID_AUTO, poll_period, CTLFLAG_RW,
1256 &da_poll_period, 0, "Media polling period in seconds");
1257 TUNABLE_INT("kern.cam.da.poll_period", &da_poll_period);
1258 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
1259 &da_retry_count, 0, "Normal I/O retry count");
1260 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
1261 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
1262 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
1263 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
1264 SYSCTL_INT(_kern_cam_da, OID_AUTO, send_ordered, CTLFLAG_RW,
1265 &da_send_ordered, 0, "Send Ordered Tags");
1266 TUNABLE_INT("kern.cam.da.send_ordered", &da_send_ordered);
1269 * DA_ORDEREDTAG_INTERVAL determines how often, relative
1270 * to the default timeout, we check to see whether an ordered
1271 * tagged transaction is appropriate to prevent simple tag
1272 * starvation. Since we'd like to ensure that there is at least
1273 * 1/2 of the timeout length left for a starved transaction to
1274 * complete after we've sent an ordered tag, we must poll at least
1275 * four times in every timeout period. This takes care of the worst
1276 * case where a starved transaction starts during an interval that
1277 * meets the requirement "don't send an ordered tag" test so it takes
1278 * us two intervals to determine that a tag must be sent.
1280 #ifndef DA_ORDEREDTAG_INTERVAL
1281 #define DA_ORDEREDTAG_INTERVAL 4
1284 static struct periph_driver dadriver =
1287 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
1290 PERIPHDRIVER_DECLARE(da, dadriver);
1292 static MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
1295 daopen(struct disk *dp)
1297 struct cam_periph *periph;
1298 struct da_softc *softc;
1301 periph = (struct cam_periph *)dp->d_drv1;
1302 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1306 cam_periph_lock(periph);
1307 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
1308 cam_periph_unlock(periph);
1309 cam_periph_release(periph);
1313 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1316 softc = (struct da_softc *)periph->softc;
1319 /* Wait for the disk size update. */
1320 error = cam_periph_sleep(periph, &softc->disk->d_mediasize, PRIBIO,
1323 xpt_print(periph->path, "unable to retrieve capacity data\n");
1325 if (periph->flags & CAM_PERIPH_INVALID)
1328 if (error == 0 && (softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1329 (softc->quirks & DA_Q_NO_PREVENT) == 0)
1330 daprevent(periph, PR_PREVENT);
1333 softc->flags &= ~DA_FLAG_PACK_INVALID;
1334 softc->flags |= DA_FLAG_OPEN;
1337 cam_periph_unhold(periph);
1338 cam_periph_unlock(periph);
1341 cam_periph_release(periph);
1347 daclose(struct disk *dp)
1349 struct cam_periph *periph;
1350 struct da_softc *softc;
1354 periph = (struct cam_periph *)dp->d_drv1;
1355 softc = (struct da_softc *)periph->softc;
1356 cam_periph_lock(periph);
1357 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1360 if (cam_periph_hold(periph, PRIBIO) == 0) {
1362 /* Flush disk cache. */
1363 if ((softc->flags & DA_FLAG_DIRTY) != 0 &&
1364 (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0 &&
1365 (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
1366 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1367 scsi_synchronize_cache(&ccb->csio, /*retries*/1,
1368 /*cbfcnp*/dadone, MSG_SIMPLE_Q_TAG,
1369 /*begin_lba*/0, /*lb_count*/0, SSD_FULL_SIZE,
1371 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
1372 /*sense_flags*/SF_RETRY_UA | SF_QUIET_IR,
1373 softc->disk->d_devstat);
1375 softc->flags &= ~DA_FLAG_DIRTY;
1376 xpt_release_ccb(ccb);
1379 /* Allow medium removal. */
1380 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1381 (softc->quirks & DA_Q_NO_PREVENT) == 0)
1382 daprevent(periph, PR_ALLOW);
1384 cam_periph_unhold(periph);
1388 * If we've got removeable media, mark the blocksize as
1389 * unavailable, since it could change when new media is
1392 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0)
1393 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1395 softc->flags &= ~DA_FLAG_OPEN;
1396 while (softc->refcount != 0)
1397 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "daclose", 1);
1398 cam_periph_unlock(periph);
1399 cam_periph_release(periph);
1404 daschedule(struct cam_periph *periph)
1406 struct da_softc *softc = (struct da_softc *)periph->softc;
1408 if (softc->state != DA_STATE_NORMAL)
1411 /* Check if we have more work to do. */
1412 if (bioq_first(&softc->bio_queue) ||
1413 (!softc->delete_running && bioq_first(&softc->delete_queue)) ||
1415 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1420 * Actually translate the requested transfer into one the physical driver
1421 * can understand. The transfer is described by a buf and will include
1422 * only one physical transfer.
1425 dastrategy(struct bio *bp)
1427 struct cam_periph *periph;
1428 struct da_softc *softc;
1430 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1431 softc = (struct da_softc *)periph->softc;
1433 cam_periph_lock(periph);
1436 * If the device has been made invalid, error out
1438 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1439 cam_periph_unlock(periph);
1440 biofinish(bp, NULL, ENXIO);
1444 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
1447 * Place it in the queue of disk activities for this disk
1449 if (bp->bio_cmd == BIO_DELETE) {
1450 bioq_disksort(&softc->delete_queue, bp);
1451 } else if (DA_SIO) {
1452 bioq_disksort(&softc->bio_queue, bp);
1454 bioq_insert_tail(&softc->bio_queue, bp);
1458 * Schedule ourselves for performing the work.
1461 cam_periph_unlock(periph);
1467 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1469 struct cam_periph *periph;
1470 struct da_softc *softc;
1472 struct ccb_scsiio csio;
1477 periph = dp->d_drv1;
1478 softc = (struct da_softc *)periph->softc;
1479 cam_periph_lock(periph);
1480 secsize = softc->params.secsize;
1482 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1483 cam_periph_unlock(periph);
1488 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1489 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1490 scsi_read_write(&csio,
1494 /*read*/SCSI_RW_WRITE,
1496 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1499 /*data_ptr*/(u_int8_t *) virtual,
1500 /*dxfer_len*/length,
1501 /*sense_len*/SSD_FULL_SIZE,
1502 da_default_timeout * 1000);
1503 xpt_polled_action((union ccb *)&csio);
1505 error = cam_periph_error((union ccb *)&csio,
1506 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1507 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1508 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1509 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1511 printf("Aborting dump due to I/O error.\n");
1512 cam_periph_unlock(periph);
1517 * Sync the disk cache contents to the physical media.
1519 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1521 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1522 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1523 scsi_synchronize_cache(&csio,
1527 /*begin_lba*/0,/* Cover the whole disk */
1531 xpt_polled_action((union ccb *)&csio);
1533 error = cam_periph_error((union ccb *)&csio,
1534 0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL);
1535 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1536 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1537 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1539 xpt_print(periph->path, "Synchronize cache failed\n");
1541 cam_periph_unlock(periph);
1546 dagetattr(struct bio *bp)
1549 struct cam_periph *periph;
1551 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1552 cam_periph_lock(periph);
1553 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1555 cam_periph_unlock(periph);
1557 bp->bio_completed = bp->bio_length;
1567 * Install a global async callback. This callback will
1568 * receive async callbacks like "new device found".
1570 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1572 if (status != CAM_REQ_CMP) {
1573 printf("da: Failed to attach master async callback "
1574 "due to status 0x%x!\n", status);
1575 } else if (da_send_ordered) {
1577 /* Register our shutdown event handler */
1578 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1579 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1580 printf("dainit: shutdown event registration failed!\n");
1585 * Callback from GEOM, called when it has finished cleaning up its
1589 dadiskgonecb(struct disk *dp)
1591 struct cam_periph *periph;
1593 periph = (struct cam_periph *)dp->d_drv1;
1594 cam_periph_release(periph);
1598 daoninvalidate(struct cam_periph *periph)
1600 struct da_softc *softc;
1602 softc = (struct da_softc *)periph->softc;
1605 * De-register any async callbacks.
1607 xpt_register_async(0, daasync, periph, periph->path);
1609 softc->flags |= DA_FLAG_PACK_INVALID;
1612 * Return all queued I/O with ENXIO.
1613 * XXX Handle any transactions queued to the card
1614 * with XPT_ABORT_CCB.
1616 bioq_flush(&softc->bio_queue, NULL, ENXIO);
1617 bioq_flush(&softc->delete_queue, NULL, ENXIO);
1620 * Tell GEOM that we've gone away, we'll get a callback when it is
1621 * done cleaning up its resources.
1623 disk_gone(softc->disk);
1627 dacleanup(struct cam_periph *periph)
1629 struct da_softc *softc;
1631 softc = (struct da_softc *)periph->softc;
1633 cam_periph_unlock(periph);
1636 * If we can't free the sysctl tree, oh well...
1638 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1639 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1640 xpt_print(periph->path, "can't remove sysctl context\n");
1643 callout_drain(&softc->mediapoll_c);
1644 disk_destroy(softc->disk);
1645 callout_drain(&softc->sendordered_c);
1646 free(softc, M_DEVBUF);
1647 cam_periph_lock(periph);
1651 daasync(void *callback_arg, u_int32_t code,
1652 struct cam_path *path, void *arg)
1654 struct cam_periph *periph;
1655 struct da_softc *softc;
1657 periph = (struct cam_periph *)callback_arg;
1659 case AC_FOUND_DEVICE:
1661 struct ccb_getdev *cgd;
1664 cgd = (struct ccb_getdev *)arg;
1668 if (cgd->protocol != PROTO_SCSI)
1670 if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED)
1672 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1673 && SID_TYPE(&cgd->inq_data) != T_RBC
1674 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1678 * Allocate a peripheral instance for
1679 * this device and start the probe
1682 status = cam_periph_alloc(daregister, daoninvalidate,
1684 "da", CAM_PERIPH_BIO,
1686 AC_FOUND_DEVICE, cgd);
1688 if (status != CAM_REQ_CMP
1689 && status != CAM_REQ_INPROG)
1690 printf("daasync: Unable to attach to new device "
1691 "due to status 0x%x\n", status);
1694 case AC_ADVINFO_CHANGED:
1698 buftype = (uintptr_t)arg;
1699 if (buftype == CDAI_TYPE_PHYS_PATH) {
1700 struct da_softc *softc;
1702 softc = periph->softc;
1703 disk_attr_changed(softc->disk, "GEOM::physpath",
1708 case AC_UNIT_ATTENTION:
1711 int error_code, sense_key, asc, ascq;
1713 softc = (struct da_softc *)periph->softc;
1714 ccb = (union ccb *)arg;
1717 * Handle all UNIT ATTENTIONs except our own,
1718 * as they will be handled by daerror().
1720 if (xpt_path_periph(ccb->ccb_h.path) != periph &&
1721 scsi_extract_sense_ccb(ccb,
1722 &error_code, &sense_key, &asc, &ascq)) {
1723 if (asc == 0x2A && ascq == 0x09) {
1724 xpt_print(ccb->ccb_h.path,
1725 "Capacity data has changed\n");
1726 softc->flags &= ~DA_FLAG_PROBED;
1728 } else if (asc == 0x28 && ascq == 0x00) {
1729 softc->flags &= ~DA_FLAG_PROBED;
1730 disk_media_changed(softc->disk, M_NOWAIT);
1731 } else if (asc == 0x3F && ascq == 0x03) {
1732 xpt_print(ccb->ccb_h.path,
1733 "INQUIRY data has changed\n");
1734 softc->flags &= ~DA_FLAG_PROBED;
1738 cam_periph_async(periph, code, path, arg);
1742 softc = (struct da_softc *)periph->softc;
1744 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
1753 struct ccb_hdr *ccbh;
1755 softc = (struct da_softc *)periph->softc;
1757 * Don't fail on the expected unit attention
1760 softc->flags |= DA_FLAG_RETRY_UA;
1761 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1762 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1768 cam_periph_async(periph, code, path, arg);
1772 dasysctlinit(void *context, int pending)
1774 struct cam_periph *periph;
1775 struct da_softc *softc;
1776 char tmpstr[80], tmpstr2[80];
1777 struct ccb_trans_settings cts;
1779 periph = (struct cam_periph *)context;
1781 * periph was held for us when this task was enqueued
1783 if (periph->flags & CAM_PERIPH_INVALID) {
1784 cam_periph_release(periph);
1788 softc = (struct da_softc *)periph->softc;
1789 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1790 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1792 sysctl_ctx_init(&softc->sysctl_ctx);
1793 softc->flags |= DA_FLAG_SCTX_INIT;
1794 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1795 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1796 CTLFLAG_RD, 0, tmpstr);
1797 if (softc->sysctl_tree == NULL) {
1798 printf("dasysctlinit: unable to allocate sysctl tree\n");
1799 cam_periph_release(periph);
1804 * Now register the sysctl handler, so the user can change the value on
1807 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1808 OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RW,
1809 softc, 0, dadeletemethodsysctl, "A",
1810 "BIO_DELETE execution method");
1811 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1812 OID_AUTO, "delete_max", CTLTYPE_U64 | CTLFLAG_RW,
1813 softc, 0, dadeletemaxsysctl, "Q",
1814 "Maximum BIO_DELETE size");
1815 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1816 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1817 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1818 "Minimum CDB size");
1819 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1820 OID_AUTO, "sort_io_queue", CTLFLAG_RW, &softc->sort_io_queue, 0,
1821 "Sort IO queue to try and optimise disk access patterns");
1823 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1824 SYSCTL_CHILDREN(softc->sysctl_tree),
1828 &softc->error_inject,
1830 "error_inject leaf");
1834 * Add some addressing info.
1836 memset(&cts, 0, sizeof (cts));
1837 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1838 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1839 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1840 cam_periph_lock(periph);
1841 xpt_action((union ccb *)&cts);
1842 cam_periph_unlock(periph);
1843 if (cts.ccb_h.status != CAM_REQ_CMP) {
1844 cam_periph_release(periph);
1847 if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
1848 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1849 if (fc->valid & CTS_FC_VALID_WWPN) {
1850 softc->wwpn = fc->wwpn;
1851 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1852 SYSCTL_CHILDREN(softc->sysctl_tree),
1853 OID_AUTO, "wwpn", CTLFLAG_RD,
1854 &softc->wwpn, "World Wide Port Name");
1857 cam_periph_release(periph);
1861 dadeletemaxsysctl(SYSCTL_HANDLER_ARGS)
1865 struct da_softc *softc;
1867 softc = (struct da_softc *)arg1;
1869 value = softc->disk->d_delmaxsize;
1870 error = sysctl_handle_64(oidp, &value, 0, req);
1871 if ((error != 0) || (req->newptr == NULL))
1874 /* only accept values smaller than the calculated value */
1875 if (value > dadeletemaxsize(softc, softc->delete_method)) {
1878 softc->disk->d_delmaxsize = value;
1884 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1888 value = *(int *)arg1;
1890 error = sysctl_handle_int(oidp, &value, 0, req);
1893 || (req->newptr == NULL))
1897 * Acceptable values here are 6, 10, 12 or 16.
1901 else if ((value > 6)
1904 else if ((value > 10)
1907 else if (value > 12)
1910 *(int *)arg1 = value;
1916 dadeletemethodset(struct da_softc *softc, da_delete_methods delete_method)
1920 softc->delete_method = delete_method;
1921 softc->disk->d_delmaxsize = dadeletemaxsize(softc, delete_method);
1922 softc->delete_func = da_delete_functions[delete_method];
1924 if (softc->delete_method > DA_DELETE_DISABLE)
1925 softc->disk->d_flags |= DISKFLAG_CANDELETE;
1927 softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
1931 dadeletemaxsize(struct da_softc *softc, da_delete_methods delete_method)
1935 switch(delete_method) {
1936 case DA_DELETE_UNMAP:
1937 sectors = (off_t)softc->unmap_max_lba;
1939 case DA_DELETE_ATA_TRIM:
1940 sectors = (off_t)ATA_DSM_RANGE_MAX * softc->trim_max_ranges;
1942 case DA_DELETE_WS16:
1943 sectors = omin(softc->ws_max_blks, WS16_MAX_BLKS);
1945 case DA_DELETE_ZERO:
1946 case DA_DELETE_WS10:
1947 sectors = omin(softc->ws_max_blks, WS10_MAX_BLKS);
1953 return (off_t)softc->params.secsize *
1954 omin(sectors, softc->params.sectors);
1958 daprobedone(struct cam_periph *periph, union ccb *ccb)
1960 struct da_softc *softc;
1962 softc = (struct da_softc *)periph->softc;
1964 dadeletemethodchoose(softc, DA_DELETE_NONE);
1966 if (bootverbose && (softc->flags & DA_FLAG_ANNOUNCED) == 0) {
1970 snprintf(buf, sizeof(buf), "Delete methods: <");
1972 for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
1973 if (softc->delete_available & (1 << i)) {
1975 strlcat(buf, ",", sizeof(buf));
1979 strlcat(buf, da_delete_method_names[i],
1981 if (i == softc->delete_method) {
1982 strlcat(buf, "(*)", sizeof(buf));
1987 if (softc->delete_method == DA_DELETE_NONE)
1988 strlcat(buf, "NONE(*)", sizeof(buf));
1990 strlcat(buf, "DISABLED(*)", sizeof(buf));
1992 strlcat(buf, ">", sizeof(buf));
1993 printf("%s%d: %s\n", periph->periph_name,
1994 periph->unit_number, buf);
1998 * Since our peripheral may be invalidated by an error
1999 * above or an external event, we must release our CCB
2000 * before releasing the probe lock on the peripheral.
2001 * The peripheral will only go away once the last lock
2002 * is removed, and we need it around for the CCB release
2005 xpt_release_ccb(ccb);
2006 softc->state = DA_STATE_NORMAL;
2007 softc->flags |= DA_FLAG_PROBED;
2009 wakeup(&softc->disk->d_mediasize);
2010 if ((softc->flags & DA_FLAG_ANNOUNCED) == 0) {
2011 softc->flags |= DA_FLAG_ANNOUNCED;
2012 cam_periph_unhold(periph);
2014 cam_periph_release_locked(periph);
2018 dadeletemethodchoose(struct da_softc *softc, da_delete_methods default_method)
2020 int i, delete_method;
2022 delete_method = default_method;
2025 * Use the pre-defined order to choose the best
2026 * performing delete.
2028 for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
2029 if (softc->delete_available & (1 << i)) {
2030 dadeletemethodset(softc, i);
2034 dadeletemethodset(softc, delete_method);
2038 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
2042 struct da_softc *softc;
2043 int i, error, methods, value;
2045 softc = (struct da_softc *)arg1;
2047 value = softc->delete_method;
2048 if (value < 0 || value > DA_DELETE_MAX)
2051 p = da_delete_method_names[value];
2052 strncpy(buf, p, sizeof(buf));
2053 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2054 if (error != 0 || req->newptr == NULL)
2056 methods = softc->delete_available | (1 << DA_DELETE_DISABLE);
2057 for (i = 0; i <= DA_DELETE_MAX; i++) {
2058 if (!(methods & (1 << i)) ||
2059 strcmp(buf, da_delete_method_names[i]) != 0)
2061 dadeletemethodset(softc, i);
2068 daregister(struct cam_periph *periph, void *arg)
2070 struct da_softc *softc;
2071 struct ccb_pathinq cpi;
2072 struct ccb_getdev *cgd;
2076 cgd = (struct ccb_getdev *)arg;
2078 printf("daregister: no getdev CCB, can't register device\n");
2079 return(CAM_REQ_CMP_ERR);
2082 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
2085 if (softc == NULL) {
2086 printf("daregister: Unable to probe new device. "
2087 "Unable to allocate softc\n");
2088 return(CAM_REQ_CMP_ERR);
2091 LIST_INIT(&softc->pending_ccbs);
2092 softc->state = DA_STATE_PROBE_RC;
2093 bioq_init(&softc->bio_queue);
2094 bioq_init(&softc->delete_queue);
2095 bioq_init(&softc->delete_run_queue);
2096 if (SID_IS_REMOVABLE(&cgd->inq_data))
2097 softc->flags |= DA_FLAG_PACK_REMOVABLE;
2098 softc->unmap_max_ranges = UNMAP_MAX_RANGES;
2099 softc->unmap_max_lba = UNMAP_RANGE_MAX;
2100 softc->ws_max_blks = WS16_MAX_BLKS;
2101 softc->trim_max_ranges = ATA_TRIM_MAX_RANGES;
2102 softc->sort_io_queue = -1;
2104 periph->softc = softc;
2107 * See if this device has any quirks.
2109 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
2110 (caddr_t)da_quirk_table,
2111 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
2112 sizeof(*da_quirk_table), scsi_inquiry_match);
2115 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
2117 softc->quirks = DA_Q_NONE;
2119 /* Check if the SIM does not want 6 byte commands */
2120 bzero(&cpi, sizeof(cpi));
2121 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2122 cpi.ccb_h.func_code = XPT_PATH_INQ;
2123 xpt_action((union ccb *)&cpi);
2124 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
2125 softc->quirks |= DA_Q_NO_6_BYTE;
2127 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
2130 * Take an exclusive refcount on the periph while dastart is called
2131 * to finish the probe. The reference will be dropped in dadone at
2134 (void)cam_periph_hold(periph, PRIBIO);
2137 * Schedule a periodic event to occasionally send an
2138 * ordered tag to a device.
2140 callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0);
2141 callout_reset(&softc->sendordered_c,
2142 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2143 dasendorderedtag, softc);
2145 cam_periph_unlock(periph);
2147 * RBC devices don't have to support READ(6), only READ(10).
2149 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
2150 softc->minimum_cmd_size = 10;
2152 softc->minimum_cmd_size = 6;
2155 * Load the user's default, if any.
2157 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
2158 periph->unit_number);
2159 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
2162 * 6, 10, 12 and 16 are the currently permissible values.
2164 if (softc->minimum_cmd_size < 6)
2165 softc->minimum_cmd_size = 6;
2166 else if ((softc->minimum_cmd_size > 6)
2167 && (softc->minimum_cmd_size <= 10))
2168 softc->minimum_cmd_size = 10;
2169 else if ((softc->minimum_cmd_size > 10)
2170 && (softc->minimum_cmd_size <= 12))
2171 softc->minimum_cmd_size = 12;
2172 else if (softc->minimum_cmd_size > 12)
2173 softc->minimum_cmd_size = 16;
2175 /* Predict whether device may support READ CAPACITY(16). */
2176 if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3 &&
2177 (softc->quirks & DA_Q_NO_RC16) == 0) {
2178 softc->flags |= DA_FLAG_CAN_RC16;
2179 softc->state = DA_STATE_PROBE_RC16;
2183 * Register this media as a disk.
2185 softc->disk = disk_alloc();
2186 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
2187 periph->unit_number, 0,
2188 DEVSTAT_BS_UNAVAILABLE,
2189 SID_TYPE(&cgd->inq_data) |
2190 XPORT_DEVSTAT_TYPE(cpi.transport),
2191 DEVSTAT_PRIORITY_DISK);
2192 softc->disk->d_open = daopen;
2193 softc->disk->d_close = daclose;
2194 softc->disk->d_strategy = dastrategy;
2195 softc->disk->d_dump = dadump;
2196 softc->disk->d_getattr = dagetattr;
2197 softc->disk->d_gone = dadiskgonecb;
2198 softc->disk->d_name = "da";
2199 softc->disk->d_drv1 = periph;
2201 softc->maxio = DFLTPHYS; /* traditional default */
2202 else if (cpi.maxio > MAXPHYS)
2203 softc->maxio = MAXPHYS; /* for safety */
2205 softc->maxio = cpi.maxio;
2206 softc->disk->d_maxsize = softc->maxio;
2207 softc->disk->d_unit = periph->unit_number;
2208 softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION;
2209 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
2210 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
2211 if ((cpi.hba_misc & PIM_UNMAPPED) != 0)
2212 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
2213 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
2214 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
2215 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
2216 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
2217 cgd->inq_data.product, sizeof(cgd->inq_data.product),
2218 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
2219 softc->disk->d_hba_vendor = cpi.hba_vendor;
2220 softc->disk->d_hba_device = cpi.hba_device;
2221 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
2222 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
2225 * Acquire a reference to the periph before we register with GEOM.
2226 * We'll release this reference once GEOM calls us back (via
2227 * dadiskgonecb()) telling us that our provider has been freed.
2229 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
2230 xpt_print(periph->path, "%s: lost periph during "
2231 "registration!\n", __func__);
2232 cam_periph_lock(periph);
2233 return (CAM_REQ_CMP_ERR);
2236 disk_create(softc->disk, DISK_VERSION);
2237 cam_periph_lock(periph);
2240 * Add async callbacks for events of interest.
2241 * I don't bother checking if this fails as,
2242 * in most cases, the system will function just
2243 * fine without them and the only alternative
2244 * would be to not attach the device on failure.
2246 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
2247 AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION,
2248 daasync, periph, periph->path);
2251 * Emit an attribute changed notification just in case
2252 * physical path information arrived before our async
2253 * event handler was registered, but after anyone attaching
2254 * to our disk device polled it.
2256 disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
2259 * Schedule a periodic media polling events.
2261 callout_init_mtx(&softc->mediapoll_c, cam_periph_mtx(periph), 0);
2262 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
2263 (cgd->inq_flags & SID_AEN) == 0 &&
2264 da_poll_period != 0)
2265 callout_reset(&softc->mediapoll_c, da_poll_period * hz,
2266 damediapoll, periph);
2268 xpt_schedule(periph, CAM_PRIORITY_DEV);
2270 return(CAM_REQ_CMP);
2274 dastart(struct cam_periph *periph, union ccb *start_ccb)
2276 struct da_softc *softc;
2278 softc = (struct da_softc *)periph->softc;
2280 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
2283 switch (softc->state) {
2284 case DA_STATE_NORMAL:
2289 /* Run BIO_DELETE if not running yet. */
2290 if (!softc->delete_running &&
2291 (bp = bioq_first(&softc->delete_queue)) != NULL) {
2292 if (softc->delete_func != NULL) {
2293 softc->delete_func(periph, start_ccb, bp);
2296 bioq_flush(&softc->delete_queue, NULL, 0);
2301 /* Run regular command. */
2302 bp = bioq_takefirst(&softc->bio_queue);
2306 scsi_test_unit_ready(&start_ccb->csio,
2307 /*retries*/ da_retry_count,
2311 da_default_timeout * 1000);
2312 start_ccb->ccb_h.ccb_bp = NULL;
2313 start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
2314 xpt_action(start_ccb);
2316 xpt_release_ccb(start_ccb);
2321 cam_periph_release_locked(periph);
2324 if ((bp->bio_flags & BIO_ORDERED) != 0 ||
2325 (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
2326 softc->flags &= ~DA_FLAG_NEED_OTAG;
2327 softc->flags |= DA_FLAG_WAS_OTAG;
2328 tag_code = MSG_ORDERED_Q_TAG;
2330 tag_code = MSG_SIMPLE_Q_TAG;
2333 switch (bp->bio_cmd) {
2335 softc->flags |= DA_FLAG_DIRTY;
2338 scsi_read_write(&start_ccb->csio,
2339 /*retries*/da_retry_count,
2341 /*tag_action*/tag_code,
2342 /*read_op*/(bp->bio_cmd == BIO_READ ?
2343 SCSI_RW_READ : SCSI_RW_WRITE) |
2344 ((bp->bio_flags & BIO_UNMAPPED) != 0 ?
2347 softc->minimum_cmd_size,
2348 /*lba*/bp->bio_pblkno,
2349 /*block_count*/bp->bio_bcount /
2350 softc->params.secsize,
2351 /*data_ptr*/ (bp->bio_flags &
2352 BIO_UNMAPPED) != 0 ? (void *)bp :
2354 /*dxfer_len*/ bp->bio_bcount,
2355 /*sense_len*/SSD_FULL_SIZE,
2356 da_default_timeout * 1000);
2360 * BIO_FLUSH doesn't currently communicate
2361 * range data, so we synchronize the cache
2362 * over the whole disk. We also force
2363 * ordered tag semantics the flush applies
2364 * to all previously queued I/O.
2366 scsi_synchronize_cache(&start_ccb->csio,
2373 da_default_timeout*1000);
2376 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
2377 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
2380 LIST_INSERT_HEAD(&softc->pending_ccbs,
2381 &start_ccb->ccb_h, periph_links.le);
2383 /* We expect a unit attention from this device */
2384 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
2385 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
2386 softc->flags &= ~DA_FLAG_RETRY_UA;
2389 start_ccb->ccb_h.ccb_bp = bp;
2391 cam_periph_unlock(periph);
2392 xpt_action(start_ccb);
2393 cam_periph_lock(periph);
2396 /* May have more work to do, so ensure we stay scheduled */
2400 case DA_STATE_PROBE_RC:
2402 struct scsi_read_capacity_data *rcap;
2404 rcap = (struct scsi_read_capacity_data *)
2405 malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
2407 printf("dastart: Couldn't malloc read_capacity data\n");
2408 /* da_free_periph??? */
2411 scsi_read_capacity(&start_ccb->csio,
2412 /*retries*/da_retry_count,
2418 start_ccb->ccb_h.ccb_bp = NULL;
2419 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC;
2420 xpt_action(start_ccb);
2423 case DA_STATE_PROBE_RC16:
2425 struct scsi_read_capacity_data_long *rcaplong;
2427 rcaplong = (struct scsi_read_capacity_data_long *)
2428 malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
2429 if (rcaplong == NULL) {
2430 printf("dastart: Couldn't malloc read_capacity data\n");
2431 /* da_free_periph??? */
2434 scsi_read_capacity_16(&start_ccb->csio,
2435 /*retries*/ da_retry_count,
2437 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2441 /*rcap_buf*/ (uint8_t *)rcaplong,
2442 /*rcap_buf_len*/ sizeof(*rcaplong),
2443 /*sense_len*/ SSD_FULL_SIZE,
2444 /*timeout*/ da_default_timeout * 1000);
2445 start_ccb->ccb_h.ccb_bp = NULL;
2446 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC16;
2447 xpt_action(start_ccb);
2450 case DA_STATE_PROBE_LBP:
2452 struct scsi_vpd_logical_block_prov *lbp;
2454 if (!scsi_vpd_supported_page(periph, SVPD_LBP)) {
2456 * If we get here we don't support any SBC-3 delete
2457 * methods with UNMAP as the Logical Block Provisioning
2458 * VPD page support is required for devices which
2459 * support it according to T10/1799-D Revision 31
2460 * however older revisions of the spec don't mandate
2461 * this so we currently don't remove these methods
2462 * from the available set.
2464 softc->state = DA_STATE_PROBE_BLK_LIMITS;
2468 lbp = (struct scsi_vpd_logical_block_prov *)
2469 malloc(sizeof(*lbp), M_SCSIDA, M_NOWAIT|M_ZERO);
2472 printf("dastart: Couldn't malloc lbp data\n");
2473 /* da_free_periph??? */
2477 scsi_inquiry(&start_ccb->csio,
2478 /*retries*/da_retry_count,
2480 /*tag_action*/MSG_SIMPLE_Q_TAG,
2481 /*inq_buf*/(u_int8_t *)lbp,
2482 /*inq_len*/sizeof(*lbp),
2484 /*page_code*/SVPD_LBP,
2485 /*sense_len*/SSD_MIN_SIZE,
2486 /*timeout*/da_default_timeout * 1000);
2487 start_ccb->ccb_h.ccb_bp = NULL;
2488 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_LBP;
2489 xpt_action(start_ccb);
2492 case DA_STATE_PROBE_BLK_LIMITS:
2494 struct scsi_vpd_block_limits *block_limits;
2496 if (!scsi_vpd_supported_page(periph, SVPD_BLOCK_LIMITS)) {
2497 /* Not supported skip to next probe */
2498 softc->state = DA_STATE_PROBE_BDC;
2502 block_limits = (struct scsi_vpd_block_limits *)
2503 malloc(sizeof(*block_limits), M_SCSIDA, M_NOWAIT|M_ZERO);
2505 if (block_limits == NULL) {
2506 printf("dastart: Couldn't malloc block_limits data\n");
2507 /* da_free_periph??? */
2511 scsi_inquiry(&start_ccb->csio,
2512 /*retries*/da_retry_count,
2514 /*tag_action*/MSG_SIMPLE_Q_TAG,
2515 /*inq_buf*/(u_int8_t *)block_limits,
2516 /*inq_len*/sizeof(*block_limits),
2518 /*page_code*/SVPD_BLOCK_LIMITS,
2519 /*sense_len*/SSD_MIN_SIZE,
2520 /*timeout*/da_default_timeout * 1000);
2521 start_ccb->ccb_h.ccb_bp = NULL;
2522 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BLK_LIMITS;
2523 xpt_action(start_ccb);
2526 case DA_STATE_PROBE_BDC:
2528 struct scsi_vpd_block_characteristics *bdc;
2530 if (!scsi_vpd_supported_page(periph, SVPD_BDC)) {
2531 softc->state = DA_STATE_PROBE_ATA;
2535 bdc = (struct scsi_vpd_block_characteristics *)
2536 malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
2539 printf("dastart: Couldn't malloc bdc data\n");
2540 /* da_free_periph??? */
2544 scsi_inquiry(&start_ccb->csio,
2545 /*retries*/da_retry_count,
2547 /*tag_action*/MSG_SIMPLE_Q_TAG,
2548 /*inq_buf*/(u_int8_t *)bdc,
2549 /*inq_len*/sizeof(*bdc),
2551 /*page_code*/SVPD_BDC,
2552 /*sense_len*/SSD_MIN_SIZE,
2553 /*timeout*/da_default_timeout * 1000);
2554 start_ccb->ccb_h.ccb_bp = NULL;
2555 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BDC;
2556 xpt_action(start_ccb);
2559 case DA_STATE_PROBE_ATA:
2561 struct ata_params *ata_params;
2563 if (!scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
2564 daprobedone(periph, start_ccb);
2568 ata_params = (struct ata_params*)
2569 malloc(sizeof(*ata_params), M_SCSIDA, M_NOWAIT|M_ZERO);
2571 if (ata_params == NULL) {
2572 printf("dastart: Couldn't malloc ata_params data\n");
2573 /* da_free_periph??? */
2577 scsi_ata_identify(&start_ccb->csio,
2578 /*retries*/da_retry_count,
2580 /*tag_action*/MSG_SIMPLE_Q_TAG,
2581 /*data_ptr*/(u_int8_t *)ata_params,
2582 /*dxfer_len*/sizeof(*ata_params),
2583 /*sense_len*/SSD_FULL_SIZE,
2584 /*timeout*/da_default_timeout * 1000);
2585 start_ccb->ccb_h.ccb_bp = NULL;
2586 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA;
2587 xpt_action(start_ccb);
2594 * In each of the methods below, while its the caller's
2595 * responsibility to ensure the request will fit into a
2596 * single device request, we might have changed the delete
2597 * method due to the device incorrectly advertising either
2598 * its supported methods or limits.
2600 * To prevent this causing further issues we validate the
2601 * against the methods limits, and warn which would
2602 * otherwise be unnecessary.
2605 da_delete_unmap(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2607 struct da_softc *softc = (struct da_softc *)periph->softc;;
2609 uint8_t *buf = softc->unmap_buf;
2610 uint64_t lba, lastlba = (uint64_t)-1;
2611 uint64_t totalcount = 0;
2613 uint32_t lastcount = 0, c;
2614 uint32_t off, ranges = 0;
2617 * Currently this doesn't take the UNMAP
2618 * Granularity and Granularity Alignment
2619 * fields into account.
2621 * This could result in both unoptimal unmap
2622 * requests as as well as UNMAP calls unmapping
2623 * fewer LBA's than requested.
2626 softc->delete_running = 1;
2627 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2630 bioq_remove(&softc->delete_queue, bp1);
2632 bioq_insert_tail(&softc->delete_run_queue, bp1);
2633 lba = bp1->bio_pblkno;
2634 count = bp1->bio_bcount / softc->params.secsize;
2636 /* Try to extend the previous range. */
2637 if (lba == lastlba) {
2638 c = omin(count, UNMAP_RANGE_MAX - lastcount);
2640 off = ((ranges - 1) * UNMAP_RANGE_SIZE) +
2642 scsi_ulto4b(lastcount, &buf[off + 8]);
2649 c = omin(count, UNMAP_RANGE_MAX);
2650 if (totalcount + c > softc->unmap_max_lba ||
2651 ranges >= softc->unmap_max_ranges) {
2652 xpt_print(periph->path,
2653 "%s issuing short delete %ld > %ld"
2655 da_delete_method_desc[softc->delete_method],
2656 totalcount + c, softc->unmap_max_lba,
2657 ranges, softc->unmap_max_ranges);
2660 off = (ranges * UNMAP_RANGE_SIZE) + UNMAP_HEAD_SIZE;
2661 scsi_u64to8b(lba, &buf[off + 0]);
2662 scsi_ulto4b(c, &buf[off + 8]);
2670 bp1 = bioq_first(&softc->delete_queue);
2671 if (bp1 == NULL || ranges >= softc->unmap_max_ranges ||
2672 totalcount + bp1->bio_bcount /
2673 softc->params.secsize > softc->unmap_max_lba)
2676 scsi_ulto2b(ranges * 16 + 6, &buf[0]);
2677 scsi_ulto2b(ranges * 16, &buf[2]);
2679 scsi_unmap(&ccb->csio,
2680 /*retries*/da_retry_count,
2682 /*tag_action*/MSG_SIMPLE_Q_TAG,
2685 /*dxfer_len*/ ranges * 16 + 8,
2686 /*sense_len*/SSD_FULL_SIZE,
2687 da_default_timeout * 1000);
2688 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2689 ccb->ccb_h.flags |= CAM_UNLOCKED;
2693 da_delete_trim(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2695 struct da_softc *softc = (struct da_softc *)periph->softc;
2697 uint8_t *buf = softc->unmap_buf;
2698 uint64_t lastlba = (uint64_t)-1;
2701 uint32_t lastcount = 0, c, requestcount;
2702 int ranges = 0, off, block_count;
2704 softc->delete_running = 1;
2705 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2708 bioq_remove(&softc->delete_queue, bp1);
2710 bioq_insert_tail(&softc->delete_run_queue, bp1);
2711 lba = bp1->bio_pblkno;
2712 count = bp1->bio_bcount / softc->params.secsize;
2713 requestcount = count;
2715 /* Try to extend the previous range. */
2716 if (lba == lastlba) {
2717 c = omin(count, ATA_DSM_RANGE_MAX - lastcount);
2719 off = (ranges - 1) * 8;
2720 buf[off + 6] = lastcount & 0xff;
2721 buf[off + 7] = (lastcount >> 8) & 0xff;
2727 c = omin(count, ATA_DSM_RANGE_MAX);
2730 buf[off + 0] = lba & 0xff;
2731 buf[off + 1] = (lba >> 8) & 0xff;
2732 buf[off + 2] = (lba >> 16) & 0xff;
2733 buf[off + 3] = (lba >> 24) & 0xff;
2734 buf[off + 4] = (lba >> 32) & 0xff;
2735 buf[off + 5] = (lba >> 40) & 0xff;
2736 buf[off + 6] = c & 0xff;
2737 buf[off + 7] = (c >> 8) & 0xff;
2742 if (count != 0 && ranges == softc->trim_max_ranges) {
2743 xpt_print(periph->path,
2744 "%s issuing short delete %ld > %ld\n",
2745 da_delete_method_desc[softc->delete_method],
2747 (softc->trim_max_ranges - ranges) *
2753 bp1 = bioq_first(&softc->delete_queue);
2754 if (bp1 == NULL || bp1->bio_bcount / softc->params.secsize >
2755 (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX)
2759 block_count = (ranges + ATA_DSM_BLK_RANGES - 1) / ATA_DSM_BLK_RANGES;
2760 scsi_ata_trim(&ccb->csio,
2761 /*retries*/da_retry_count,
2763 /*tag_action*/MSG_SIMPLE_Q_TAG,
2766 /*dxfer_len*/block_count * ATA_DSM_BLK_SIZE,
2767 /*sense_len*/SSD_FULL_SIZE,
2768 da_default_timeout * 1000);
2769 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2770 ccb->ccb_h.flags |= CAM_UNLOCKED;
2774 * We calculate ws_max_blks here based off d_delmaxsize instead
2775 * of using softc->ws_max_blks as it is absolute max for the
2776 * device not the protocol max which may well be lower.
2779 da_delete_ws(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2781 struct da_softc *softc;
2783 uint64_t ws_max_blks;
2785 uint64_t count; /* forward compat with WS32 */
2787 softc = (struct da_softc *)periph->softc;
2788 ws_max_blks = softc->disk->d_delmaxsize / softc->params.secsize;
2789 softc->delete_running = 1;
2790 lba = bp->bio_pblkno;
2794 bioq_remove(&softc->delete_queue, bp1);
2796 bioq_insert_tail(&softc->delete_run_queue, bp1);
2797 count += bp1->bio_bcount / softc->params.secsize;
2798 if (count > ws_max_blks) {
2799 xpt_print(periph->path,
2800 "%s issuing short delete %ld > %ld\n",
2801 da_delete_method_desc[softc->delete_method],
2802 count, ws_max_blks);
2803 count = omin(count, ws_max_blks);
2806 bp1 = bioq_first(&softc->delete_queue);
2807 if (bp1 == NULL || lba + count != bp1->bio_pblkno ||
2808 count + bp1->bio_bcount /
2809 softc->params.secsize > ws_max_blks)
2813 scsi_write_same(&ccb->csio,
2814 /*retries*/da_retry_count,
2816 /*tag_action*/MSG_SIMPLE_Q_TAG,
2817 /*byte2*/softc->delete_method ==
2818 DA_DELETE_ZERO ? 0 : SWS_UNMAP,
2819 softc->delete_method == DA_DELETE_WS16 ? 16 : 10,
2821 /*block_count*/count,
2822 /*data_ptr*/ __DECONST(void *, zero_region),
2823 /*dxfer_len*/ softc->params.secsize,
2824 /*sense_len*/SSD_FULL_SIZE,
2825 da_default_timeout * 1000);
2826 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2827 ccb->ccb_h.flags |= CAM_UNLOCKED;
2831 cmd6workaround(union ccb *ccb)
2833 struct scsi_rw_6 cmd6;
2834 struct scsi_rw_10 *cmd10;
2835 struct da_softc *softc;
2840 cdb = ccb->csio.cdb_io.cdb_bytes;
2841 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
2843 if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
2844 da_delete_methods old_method = softc->delete_method;
2847 * Typically there are two reasons for failure here
2848 * 1. Delete method was detected as supported but isn't
2849 * 2. Delete failed due to invalid params e.g. too big
2851 * While we will attempt to choose an alternative delete method
2852 * this may result in short deletes if the existing delete
2853 * requests from geom are big for the new method choosen.
2855 * This method assumes that the error which triggered this
2856 * will not retry the io otherwise a panic will occur
2858 dadeleteflag(softc, old_method, 0);
2859 dadeletemethodchoose(softc, DA_DELETE_DISABLE);
2860 if (softc->delete_method == DA_DELETE_DISABLE)
2861 xpt_print(ccb->ccb_h.path,
2862 "%s failed, disabling BIO_DELETE\n",
2863 da_delete_method_desc[old_method]);
2865 xpt_print(ccb->ccb_h.path,
2866 "%s failed, switching to %s BIO_DELETE\n",
2867 da_delete_method_desc[old_method],
2868 da_delete_method_desc[softc->delete_method]);
2870 while ((bp = bioq_takefirst(&softc->delete_run_queue)) != NULL)
2871 bioq_disksort(&softc->delete_queue, bp);
2872 bioq_disksort(&softc->delete_queue,
2873 (struct bio *)ccb->ccb_h.ccb_bp);
2874 ccb->ccb_h.ccb_bp = NULL;
2878 /* Detect unsupported PREVENT ALLOW MEDIUM REMOVAL. */
2879 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
2880 (*cdb == PREVENT_ALLOW) &&
2881 (softc->quirks & DA_Q_NO_PREVENT) == 0) {
2883 xpt_print(ccb->ccb_h.path,
2884 "PREVENT ALLOW MEDIUM REMOVAL not supported.\n");
2885 softc->quirks |= DA_Q_NO_PREVENT;
2889 /* Detect unsupported SYNCHRONIZE CACHE(10). */
2890 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
2891 (*cdb == SYNCHRONIZE_CACHE) &&
2892 (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
2894 xpt_print(ccb->ccb_h.path,
2895 "SYNCHRONIZE CACHE(10) not supported.\n");
2896 softc->quirks |= DA_Q_NO_SYNC_CACHE;
2897 softc->disk->d_flags &= ~DISKFLAG_CANFLUSHCACHE;
2901 /* Translation only possible if CDB is an array and cmd is R/W6 */
2902 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
2903 (*cdb != READ_6 && *cdb != WRITE_6))
2906 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
2907 "increasing minimum_cmd_size to 10.\n");
2908 softc->minimum_cmd_size = 10;
2910 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
2911 cmd10 = (struct scsi_rw_10 *)cdb;
2912 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
2914 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
2915 cmd10->reserved = 0;
2916 scsi_ulto2b(cmd6.length, cmd10->length);
2917 cmd10->control = cmd6.control;
2918 ccb->csio.cdb_len = sizeof(*cmd10);
2920 /* Requeue request, unfreezing queue if necessary */
2921 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
2922 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2925 cam_release_devq(ccb->ccb_h.path,
2929 /*getcount_only*/0);
2935 dadone(struct cam_periph *periph, union ccb *done_ccb)
2937 struct da_softc *softc;
2938 struct ccb_scsiio *csio;
2942 softc = (struct da_softc *)periph->softc;
2943 priority = done_ccb->ccb_h.pinfo.priority;
2945 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
2947 csio = &done_ccb->csio;
2948 state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
2950 case DA_CCB_BUFFER_IO:
2953 struct bio *bp, *bp1;
2955 cam_periph_lock(periph);
2956 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2957 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2961 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
2966 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
2967 if (error == ERESTART) {
2969 * A retry was scheduled, so
2972 cam_periph_unlock(periph);
2975 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2980 * return all queued I/O with EIO, so that
2981 * the client can retry these I/Os in the
2982 * proper order should it attempt to recover.
2987 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
2989 * Catastrophic error. Mark our pack as
2993 * XXX See if this is really a media
2996 xpt_print(periph->path,
2997 "Invalidating pack\n");
2998 softc->flags |= DA_FLAG_PACK_INVALID;
2999 queued_error = ENXIO;
3001 bioq_flush(&softc->bio_queue, NULL,
3004 bp->bio_error = error;
3005 bp->bio_resid = bp->bio_bcount;
3006 bp->bio_flags |= BIO_ERROR;
3008 } else if (bp != NULL) {
3009 if (state == DA_CCB_DELETE)
3012 bp->bio_resid = csio->resid;
3014 if (bp->bio_resid != 0)
3015 bp->bio_flags |= BIO_ERROR;
3017 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3018 cam_release_devq(done_ccb->ccb_h.path,
3022 /*getcount_only*/0);
3023 } else if (bp != NULL) {
3024 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3025 panic("REQ_CMP with QFRZN");
3026 if (state == DA_CCB_DELETE)
3029 bp->bio_resid = csio->resid;
3030 if (csio->resid > 0)
3031 bp->bio_flags |= BIO_ERROR;
3032 if (softc->error_inject != 0) {
3033 bp->bio_error = softc->error_inject;
3034 bp->bio_resid = bp->bio_bcount;
3035 bp->bio_flags |= BIO_ERROR;
3036 softc->error_inject = 0;
3040 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
3041 if (LIST_EMPTY(&softc->pending_ccbs))
3042 softc->flags |= DA_FLAG_WAS_OTAG;
3044 xpt_release_ccb(done_ccb);
3045 if (state == DA_CCB_DELETE) {
3046 TAILQ_HEAD(, bio) queue;
3049 TAILQ_CONCAT(&queue, &softc->delete_run_queue.queue, bio_queue);
3050 softc->delete_run_queue.insert_point = NULL;
3051 softc->delete_running = 0;
3053 cam_periph_unlock(periph);
3054 while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
3055 TAILQ_REMOVE(&queue, bp1, bio_queue);
3056 bp1->bio_error = bp->bio_error;
3057 if (bp->bio_flags & BIO_ERROR) {
3058 bp1->bio_flags |= BIO_ERROR;
3059 bp1->bio_resid = bp1->bio_bcount;
3065 cam_periph_unlock(periph);
3070 case DA_CCB_PROBE_RC:
3071 case DA_CCB_PROBE_RC16:
3073 struct scsi_read_capacity_data *rdcap;
3074 struct scsi_read_capacity_data_long *rcaplong;
3075 char announce_buf[80];
3081 if (state == DA_CCB_PROBE_RC)
3082 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
3084 rcaplong = (struct scsi_read_capacity_data_long *)
3087 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3088 struct disk_params *dp;
3089 uint32_t block_size;
3091 u_int lbppbe; /* LB per physical block exponent. */
3092 u_int lalba; /* Lowest aligned LBA. */
3094 if (state == DA_CCB_PROBE_RC) {
3095 block_size = scsi_4btoul(rdcap->length);
3096 maxsector = scsi_4btoul(rdcap->addr);
3101 * According to SBC-2, if the standard 10
3102 * byte READ CAPACITY command returns 2^32,
3103 * we should issue the 16 byte version of
3104 * the command, since the device in question
3105 * has more sectors than can be represented
3106 * with the short version of the command.
3108 if (maxsector == 0xffffffff) {
3109 free(rdcap, M_SCSIDA);
3110 xpt_release_ccb(done_ccb);
3111 softc->state = DA_STATE_PROBE_RC16;
3112 xpt_schedule(periph, priority);
3116 block_size = scsi_4btoul(rcaplong->length);
3117 maxsector = scsi_8btou64(rcaplong->addr);
3118 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
3119 lalba = scsi_2btoul(rcaplong->lalba_lbp);
3123 * Because GEOM code just will panic us if we
3124 * give them an 'illegal' value we'll avoid that
3127 if (block_size == 0) {
3132 if (block_size >= MAXPHYS) {
3133 xpt_print(periph->path,
3134 "unsupportable block size %ju\n",
3135 (uintmax_t) block_size);
3136 announce_buf[0] = '\0';
3137 cam_periph_invalidate(periph);
3140 * We pass rcaplong into dasetgeom(),
3141 * because it will only use it if it is
3144 dasetgeom(periph, block_size, maxsector,
3145 rcaplong, sizeof(*rcaplong));
3146 lbp = (lalba & SRC16_LBPME_A);
3147 dp = &softc->params;
3148 snprintf(announce_buf, sizeof(announce_buf),
3149 "%juMB (%ju %u byte sectors: %dH %dS/T "
3151 (((uintmax_t)dp->secsize *
3152 dp->sectors) / (1024*1024)),
3153 (uintmax_t)dp->sectors,
3154 dp->secsize, dp->heads,
3155 dp->secs_per_track, dp->cylinders);
3160 announce_buf[0] = '\0';
3163 * Retry any UNIT ATTENTION type errors. They
3164 * are expected at boot.
3166 error = daerror(done_ccb, CAM_RETRY_SELTO,
3167 SF_RETRY_UA|SF_NO_PRINT);
3168 if (error == ERESTART) {
3170 * A retry was scheuled, so
3174 } else if (error != 0) {
3176 int sense_key, error_code;
3179 struct ccb_getdev cgd;
3181 /* Don't wedge this device's queue */
3182 status = done_ccb->ccb_h.status;
3183 if ((status & CAM_DEV_QFRZN) != 0)
3184 cam_release_devq(done_ccb->ccb_h.path,
3188 /*getcount_only*/0);
3191 xpt_setup_ccb(&cgd.ccb_h,
3192 done_ccb->ccb_h.path,
3193 CAM_PRIORITY_NORMAL);
3194 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
3195 xpt_action((union ccb *)&cgd);
3197 if (scsi_extract_sense_ccb(done_ccb,
3198 &error_code, &sense_key, &asc, &ascq))
3204 * If we tried READ CAPACITY(16) and failed,
3205 * fallback to READ CAPACITY(10).
3207 if ((state == DA_CCB_PROBE_RC16) &&
3208 (softc->flags & DA_FLAG_CAN_RC16) &&
3209 (((csio->ccb_h.status & CAM_STATUS_MASK) ==
3212 (error_code == SSD_CURRENT_ERROR) &&
3213 (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
3214 softc->flags &= ~DA_FLAG_CAN_RC16;
3215 free(rdcap, M_SCSIDA);
3216 xpt_release_ccb(done_ccb);
3217 softc->state = DA_STATE_PROBE_RC;
3218 xpt_schedule(periph, priority);
3222 * Attach to anything that claims to be a
3223 * direct access or optical disk device,
3224 * as long as it doesn't return a "Logical
3225 * unit not supported" (0x25) error.
3227 if ((have_sense) && (asc != 0x25)
3228 && (error_code == SSD_CURRENT_ERROR)) {
3229 const char *sense_key_desc;
3230 const char *asc_desc;
3232 dasetgeom(periph, 512, -1, NULL, 0);
3233 scsi_sense_desc(sense_key, asc, ascq,
3237 snprintf(announce_buf,
3238 sizeof(announce_buf),
3239 "Attempt to query device "
3240 "size failed: %s, %s",
3248 xpt_print(periph->path,
3249 "got CAM status %#x\n",
3250 done_ccb->ccb_h.status);
3253 xpt_print(periph->path, "fatal error, "
3254 "failed to attach to device\n");
3257 * Free up resources.
3259 cam_periph_invalidate(periph);
3263 free(csio->data_ptr, M_SCSIDA);
3264 if (announce_buf[0] != '\0' &&
3265 ((softc->flags & DA_FLAG_ANNOUNCED) == 0)) {
3267 * Create our sysctl variables, now that we know
3268 * we have successfully attached.
3270 /* increase the refcount */
3271 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3272 taskqueue_enqueue(taskqueue_thread,
3273 &softc->sysctl_task);
3274 xpt_announce_periph(periph, announce_buf);
3275 xpt_announce_quirks(periph, softc->quirks,
3278 xpt_print(periph->path, "fatal error, "
3279 "could not acquire reference count\n");
3283 /* We already probed the device. */
3284 if (softc->flags & DA_FLAG_PROBED) {
3285 daprobedone(periph, done_ccb);
3289 /* Ensure re-probe doesn't see old delete. */
3290 softc->delete_available = 0;
3291 if (lbp && (softc->quirks & DA_Q_NO_UNMAP) == 0) {
3293 * Based on older SBC-3 spec revisions
3294 * any of the UNMAP methods "may" be
3295 * available via LBP given this flag so
3296 * we flag all of them as availble and
3297 * then remove those which further
3298 * probes confirm aren't available
3301 * We could also check readcap(16) p_type
3302 * flag to exclude one or more invalid
3303 * write same (X) types here
3305 dadeleteflag(softc, DA_DELETE_WS16, 1);
3306 dadeleteflag(softc, DA_DELETE_WS10, 1);
3307 dadeleteflag(softc, DA_DELETE_ZERO, 1);
3308 dadeleteflag(softc, DA_DELETE_UNMAP, 1);
3310 xpt_release_ccb(done_ccb);
3311 softc->state = DA_STATE_PROBE_LBP;
3312 xpt_schedule(periph, priority);
3316 xpt_release_ccb(done_ccb);
3317 softc->state = DA_STATE_PROBE_BDC;
3318 xpt_schedule(periph, priority);
3321 case DA_CCB_PROBE_LBP:
3323 struct scsi_vpd_logical_block_prov *lbp;
3325 lbp = (struct scsi_vpd_logical_block_prov *)csio->data_ptr;
3327 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3329 * T10/1799-D Revision 31 states at least one of these
3330 * must be supported but we don't currently enforce this.
3332 dadeleteflag(softc, DA_DELETE_WS16,
3333 (lbp->flags & SVPD_LBP_WS16));
3334 dadeleteflag(softc, DA_DELETE_WS10,
3335 (lbp->flags & SVPD_LBP_WS10));
3336 dadeleteflag(softc, DA_DELETE_ZERO,
3337 (lbp->flags & SVPD_LBP_WS10));
3338 dadeleteflag(softc, DA_DELETE_UNMAP,
3339 (lbp->flags & SVPD_LBP_UNMAP));
3342 error = daerror(done_ccb, CAM_RETRY_SELTO,
3343 SF_RETRY_UA|SF_NO_PRINT);
3344 if (error == ERESTART)
3346 else if (error != 0) {
3347 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3348 /* Don't wedge this device's queue */
3349 cam_release_devq(done_ccb->ccb_h.path,
3353 /*getcount_only*/0);
3357 * Failure indicates we don't support any SBC-3
3358 * delete methods with UNMAP
3363 free(lbp, M_SCSIDA);
3364 xpt_release_ccb(done_ccb);
3365 softc->state = DA_STATE_PROBE_BLK_LIMITS;
3366 xpt_schedule(periph, priority);
3369 case DA_CCB_PROBE_BLK_LIMITS:
3371 struct scsi_vpd_block_limits *block_limits;
3373 block_limits = (struct scsi_vpd_block_limits *)csio->data_ptr;
3375 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3376 uint32_t max_txfer_len = scsi_4btoul(
3377 block_limits->max_txfer_len);
3378 uint32_t max_unmap_lba_cnt = scsi_4btoul(
3379 block_limits->max_unmap_lba_cnt);
3380 uint32_t max_unmap_blk_cnt = scsi_4btoul(
3381 block_limits->max_unmap_blk_cnt);
3382 uint64_t ws_max_blks = scsi_8btou64(
3383 block_limits->max_write_same_length);
3385 if (max_txfer_len != 0) {
3386 softc->disk->d_maxsize = MIN(softc->maxio,
3387 (off_t)max_txfer_len * softc->params.secsize);
3391 * We should already support UNMAP but we check lba
3392 * and block count to be sure
3394 if (max_unmap_lba_cnt != 0x00L &&
3395 max_unmap_blk_cnt != 0x00L) {
3396 softc->unmap_max_lba = max_unmap_lba_cnt;
3397 softc->unmap_max_ranges = min(max_unmap_blk_cnt,
3401 * Unexpected UNMAP limits which means the
3402 * device doesn't actually support UNMAP
3404 dadeleteflag(softc, DA_DELETE_UNMAP, 0);
3407 if (ws_max_blks != 0x00L)
3408 softc->ws_max_blks = ws_max_blks;
3411 error = daerror(done_ccb, CAM_RETRY_SELTO,
3412 SF_RETRY_UA|SF_NO_PRINT);
3413 if (error == ERESTART)
3415 else if (error != 0) {
3416 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3417 /* Don't wedge this device's queue */
3418 cam_release_devq(done_ccb->ccb_h.path,
3422 /*getcount_only*/0);
3426 * Failure here doesn't mean UNMAP is not
3427 * supported as this is an optional page.
3429 softc->unmap_max_lba = 1;
3430 softc->unmap_max_ranges = 1;
3434 free(block_limits, M_SCSIDA);
3435 xpt_release_ccb(done_ccb);
3436 softc->state = DA_STATE_PROBE_BDC;
3437 xpt_schedule(periph, priority);
3440 case DA_CCB_PROBE_BDC:
3442 struct scsi_vpd_block_characteristics *bdc;
3444 bdc = (struct scsi_vpd_block_characteristics *)csio->data_ptr;
3446 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3448 * Disable queue sorting for non-rotational media
3451 u_int old_rate = softc->disk->d_rotation_rate;
3453 softc->disk->d_rotation_rate =
3454 scsi_2btoul(bdc->medium_rotation_rate);
3455 if (softc->disk->d_rotation_rate ==
3456 SVPD_BDC_RATE_NON_ROTATING) {
3457 softc->sort_io_queue = 0;
3459 if (softc->disk->d_rotation_rate != old_rate) {
3460 disk_attr_changed(softc->disk,
3461 "GEOM::rotation_rate", M_NOWAIT);
3465 error = daerror(done_ccb, CAM_RETRY_SELTO,
3466 SF_RETRY_UA|SF_NO_PRINT);
3467 if (error == ERESTART)
3469 else if (error != 0) {
3470 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3471 /* Don't wedge this device's queue */
3472 cam_release_devq(done_ccb->ccb_h.path,
3476 /*getcount_only*/0);
3481 free(bdc, M_SCSIDA);
3482 xpt_release_ccb(done_ccb);
3483 softc->state = DA_STATE_PROBE_ATA;
3484 xpt_schedule(periph, priority);
3487 case DA_CCB_PROBE_ATA:
3490 struct ata_params *ata_params;
3493 ata_params = (struct ata_params *)csio->data_ptr;
3494 ptr = (uint16_t *)ata_params;
3496 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3499 for (i = 0; i < sizeof(*ata_params) / 2; i++)
3500 ptr[i] = le16toh(ptr[i]);
3501 if (ata_params->support_dsm & ATA_SUPPORT_DSM_TRIM &&
3502 (softc->quirks & DA_Q_NO_UNMAP) == 0) {
3503 dadeleteflag(softc, DA_DELETE_ATA_TRIM, 1);
3504 if (ata_params->max_dsm_blocks != 0)
3505 softc->trim_max_ranges = min(
3506 softc->trim_max_ranges,
3507 ata_params->max_dsm_blocks *
3508 ATA_DSM_BLK_RANGES);
3511 * Disable queue sorting for non-rotational media
3514 old_rate = softc->disk->d_rotation_rate;
3515 softc->disk->d_rotation_rate =
3516 ata_params->media_rotation_rate;
3517 if (softc->disk->d_rotation_rate ==
3518 ATA_RATE_NON_ROTATING) {
3519 softc->sort_io_queue = 0;
3522 if (softc->disk->d_rotation_rate != old_rate) {
3523 disk_attr_changed(softc->disk,
3524 "GEOM::rotation_rate", M_NOWAIT);
3528 error = daerror(done_ccb, CAM_RETRY_SELTO,
3529 SF_RETRY_UA|SF_NO_PRINT);
3530 if (error == ERESTART)
3532 else if (error != 0) {
3533 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3534 /* Don't wedge this device's queue */
3535 cam_release_devq(done_ccb->ccb_h.path,
3539 /*getcount_only*/0);
3544 free(ata_params, M_SCSIDA);
3545 daprobedone(periph, done_ccb);
3549 /* No-op. We're polling */
3553 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3555 if (daerror(done_ccb, CAM_RETRY_SELTO,
3556 SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
3559 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3560 cam_release_devq(done_ccb->ccb_h.path,
3564 /*getcount_only*/0);
3566 xpt_release_ccb(done_ccb);
3567 cam_periph_release_locked(periph);
3573 xpt_release_ccb(done_ccb);
3577 dareprobe(struct cam_periph *periph)
3579 struct da_softc *softc;
3582 softc = (struct da_softc *)periph->softc;
3584 /* Probe in progress; don't interfere. */
3585 if (softc->state != DA_STATE_NORMAL)
3588 status = cam_periph_acquire(periph);
3589 KASSERT(status == CAM_REQ_CMP,
3590 ("dareprobe: cam_periph_acquire failed"));
3592 if (softc->flags & DA_FLAG_CAN_RC16)
3593 softc->state = DA_STATE_PROBE_RC16;
3595 softc->state = DA_STATE_PROBE_RC;
3597 xpt_schedule(periph, CAM_PRIORITY_DEV);
3601 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3603 struct da_softc *softc;
3604 struct cam_periph *periph;
3605 int error, error_code, sense_key, asc, ascq;
3607 periph = xpt_path_periph(ccb->ccb_h.path);
3608 softc = (struct da_softc *)periph->softc;
3611 * Automatically detect devices that do not support
3612 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
3615 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3616 error = cmd6workaround(ccb);
3617 } else if (scsi_extract_sense_ccb(ccb,
3618 &error_code, &sense_key, &asc, &ascq)) {
3619 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3620 error = cmd6workaround(ccb);
3622 * If the target replied with CAPACITY DATA HAS CHANGED UA,
3623 * query the capacity and notify upper layers.
3625 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3626 asc == 0x2A && ascq == 0x09) {
3627 xpt_print(periph->path, "Capacity data has changed\n");
3628 softc->flags &= ~DA_FLAG_PROBED;
3630 sense_flags |= SF_NO_PRINT;
3631 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3632 asc == 0x28 && ascq == 0x00) {
3633 softc->flags &= ~DA_FLAG_PROBED;
3634 disk_media_changed(softc->disk, M_NOWAIT);
3635 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3636 asc == 0x3F && ascq == 0x03) {
3637 xpt_print(periph->path, "INQUIRY data has changed\n");
3638 softc->flags &= ~DA_FLAG_PROBED;
3640 sense_flags |= SF_NO_PRINT;
3641 } else if (sense_key == SSD_KEY_NOT_READY &&
3642 asc == 0x3a && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
3643 softc->flags |= DA_FLAG_PACK_INVALID;
3644 disk_media_gone(softc->disk, M_NOWAIT);
3647 if (error == ERESTART)
3652 * Until we have a better way of doing pack validation,
3653 * don't treat UAs as errors.
3655 sense_flags |= SF_RETRY_UA;
3657 if (softc->quirks & DA_Q_RETRY_BUSY)
3658 sense_flags |= SF_RETRY_BUSY;
3659 return(cam_periph_error(ccb, cam_flags, sense_flags,
3660 &softc->saved_ccb));
3664 damediapoll(void *arg)
3666 struct cam_periph *periph = arg;
3667 struct da_softc *softc = periph->softc;
3669 if (!softc->tur && LIST_EMPTY(&softc->pending_ccbs)) {
3670 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3675 /* Queue us up again */
3676 if (da_poll_period != 0)
3677 callout_schedule(&softc->mediapoll_c, da_poll_period * hz);
3681 daprevent(struct cam_periph *periph, int action)
3683 struct da_softc *softc;
3687 softc = (struct da_softc *)periph->softc;
3689 if (((action == PR_ALLOW)
3690 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
3691 || ((action == PR_PREVENT)
3692 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
3696 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3698 scsi_prevent(&ccb->csio,
3706 error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
3707 SF_RETRY_UA | SF_NO_PRINT, softc->disk->d_devstat);
3710 if (action == PR_ALLOW)
3711 softc->flags &= ~DA_FLAG_PACK_LOCKED;
3713 softc->flags |= DA_FLAG_PACK_LOCKED;
3716 xpt_release_ccb(ccb);
3720 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
3721 struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
3723 struct ccb_calc_geometry ccg;
3724 struct da_softc *softc;
3725 struct disk_params *dp;
3726 u_int lbppbe, lalba;
3729 softc = (struct da_softc *)periph->softc;
3731 dp = &softc->params;
3732 dp->secsize = block_len;
3733 dp->sectors = maxsector + 1;
3734 if (rcaplong != NULL) {
3735 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
3736 lalba = scsi_2btoul(rcaplong->lalba_lbp);
3737 lalba &= SRC16_LALBA_A;
3744 dp->stripesize = block_len << lbppbe;
3745 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
3747 } else if (softc->quirks & DA_Q_4K) {
3748 dp->stripesize = 4096;
3749 dp->stripeoffset = 0;
3752 dp->stripeoffset = 0;
3755 * Have the controller provide us with a geometry
3756 * for this disk. The only time the geometry
3757 * matters is when we boot and the controller
3758 * is the only one knowledgeable enough to come
3759 * up with something that will make this a bootable
3762 xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3763 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
3764 ccg.block_size = dp->secsize;
3765 ccg.volume_size = dp->sectors;
3767 ccg.secs_per_track = 0;
3769 xpt_action((union ccb*)&ccg);
3770 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3772 * We don't know what went wrong here- but just pick
3773 * a geometry so we don't have nasty things like divide
3777 dp->secs_per_track = 255;
3778 dp->cylinders = dp->sectors / (255 * 255);
3779 if (dp->cylinders == 0) {
3783 dp->heads = ccg.heads;
3784 dp->secs_per_track = ccg.secs_per_track;
3785 dp->cylinders = ccg.cylinders;
3789 * If the user supplied a read capacity buffer, and if it is
3790 * different than the previous buffer, update the data in the EDT.
3791 * If it's the same, we don't bother. This avoids sending an
3792 * update every time someone opens this device.
3794 if ((rcaplong != NULL)
3795 && (bcmp(rcaplong, &softc->rcaplong,
3796 min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
3797 struct ccb_dev_advinfo cdai;
3799 xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3800 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
3801 cdai.buftype = CDAI_TYPE_RCAPLONG;
3802 cdai.flags = CDAI_FLAG_STORE;
3803 cdai.bufsiz = rcap_len;
3804 cdai.buf = (uint8_t *)rcaplong;
3805 xpt_action((union ccb *)&cdai);
3806 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
3807 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
3808 if (cdai.ccb_h.status != CAM_REQ_CMP) {
3809 xpt_print(periph->path, "%s: failed to set read "
3810 "capacity advinfo\n", __func__);
3811 /* Use cam_error_print() to decode the status */
3812 cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
3815 bcopy(rcaplong, &softc->rcaplong,
3816 min(sizeof(softc->rcaplong), rcap_len));
3820 softc->disk->d_sectorsize = softc->params.secsize;
3821 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
3822 softc->disk->d_stripesize = softc->params.stripesize;
3823 softc->disk->d_stripeoffset = softc->params.stripeoffset;
3824 /* XXX: these are not actually "firmware" values, so they may be wrong */
3825 softc->disk->d_fwsectors = softc->params.secs_per_track;
3826 softc->disk->d_fwheads = softc->params.heads;
3827 softc->disk->d_devstat->block_size = softc->params.secsize;
3828 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
3830 error = disk_resize(softc->disk, M_NOWAIT);
3832 xpt_print(periph->path, "disk_resize(9) failed, error = %d\n", error);
3836 dasendorderedtag(void *arg)
3838 struct da_softc *softc = arg;
3840 if (da_send_ordered) {
3841 if (!LIST_EMPTY(&softc->pending_ccbs)) {
3842 if ((softc->flags & DA_FLAG_WAS_OTAG) == 0)
3843 softc->flags |= DA_FLAG_NEED_OTAG;
3844 softc->flags &= ~DA_FLAG_WAS_OTAG;
3847 /* Queue us up again */
3848 callout_reset(&softc->sendordered_c,
3849 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
3850 dasendorderedtag, softc);
3854 * Step through all DA peripheral drivers, and if the device is still open,
3855 * sync the disk cache to physical media.
3858 dashutdown(void * arg, int howto)
3860 struct cam_periph *periph;
3861 struct da_softc *softc;
3865 CAM_PERIPH_FOREACH(periph, &dadriver) {
3866 softc = (struct da_softc *)periph->softc;
3867 if (SCHEDULER_STOPPED()) {
3868 /* If we paniced with the lock held, do not recurse. */
3869 if (!cam_periph_owned(periph) &&
3870 (softc->flags & DA_FLAG_OPEN)) {
3871 dadump(softc->disk, NULL, 0, 0, 0);
3875 cam_periph_lock(periph);
3878 * We only sync the cache if the drive is still open, and
3879 * if the drive is capable of it..
3881 if (((softc->flags & DA_FLAG_OPEN) == 0)
3882 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
3883 cam_periph_unlock(periph);
3887 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3888 scsi_synchronize_cache(&ccb->csio,
3892 /*begin_lba*/0, /* whole disk */
3897 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
3898 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR,
3899 softc->disk->d_devstat);
3901 xpt_print(periph->path, "Synchronize cache failed\n");
3902 xpt_release_ccb(ccb);
3903 cam_periph_unlock(periph);
3907 #else /* !_KERNEL */
3910 * XXX These are only left out of the kernel build to silence warnings. If,
3911 * for some reason these functions are used in the kernel, the ifdefs should
3912 * be moved so they are included both in the kernel and userland.
3915 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
3916 void (*cbfcnp)(struct cam_periph *, union ccb *),
3917 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
3918 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
3921 struct scsi_format_unit *scsi_cmd;
3923 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
3924 scsi_cmd->opcode = FORMAT_UNIT;
3925 scsi_cmd->byte2 = byte2;
3926 scsi_ulto2b(ileave, scsi_cmd->interleave);
3931 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
3941 scsi_read_defects(struct ccb_scsiio *csio, uint32_t retries,
3942 void (*cbfcnp)(struct cam_periph *, union ccb *),
3943 uint8_t tag_action, uint8_t list_format,
3944 uint32_t addr_desc_index, uint8_t *data_ptr,
3945 uint32_t dxfer_len, int minimum_cmd_size,
3946 uint8_t sense_len, uint32_t timeout)
3951 * These conditions allow using the 10 byte command. Otherwise we
3952 * need to use the 12 byte command.
3954 if ((minimum_cmd_size <= 10)
3955 && (addr_desc_index == 0)
3956 && (dxfer_len <= SRDD10_MAX_LENGTH)) {
3957 struct scsi_read_defect_data_10 *cdb10;
3959 cdb10 = (struct scsi_read_defect_data_10 *)
3960 &csio->cdb_io.cdb_bytes;
3962 cdb_len = sizeof(*cdb10);
3963 bzero(cdb10, cdb_len);
3964 cdb10->opcode = READ_DEFECT_DATA_10;
3965 cdb10->format = list_format;
3966 scsi_ulto2b(dxfer_len, cdb10->alloc_length);
3968 struct scsi_read_defect_data_12 *cdb12;
3970 cdb12 = (struct scsi_read_defect_data_12 *)
3971 &csio->cdb_io.cdb_bytes;
3973 cdb_len = sizeof(*cdb12);
3974 bzero(cdb12, cdb_len);
3975 cdb12->opcode = READ_DEFECT_DATA_12;
3976 cdb12->format = list_format;
3977 scsi_ulto4b(dxfer_len, cdb12->alloc_length);
3978 scsi_ulto4b(addr_desc_index, cdb12->address_descriptor_index);
3984 /*flags*/ CAM_DIR_IN,
3994 scsi_sanitize(struct ccb_scsiio *csio, u_int32_t retries,
3995 void (*cbfcnp)(struct cam_periph *, union ccb *),
3996 u_int8_t tag_action, u_int8_t byte2, u_int16_t control,
3997 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4000 struct scsi_sanitize *scsi_cmd;
4002 scsi_cmd = (struct scsi_sanitize *)&csio->cdb_io.cdb_bytes;
4003 scsi_cmd->opcode = SANITIZE;
4004 scsi_cmd->byte2 = byte2;
4005 scsi_cmd->control = control;
4006 scsi_ulto2b(dxfer_len, scsi_cmd->length);
4011 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
4020 #endif /* _KERNEL */