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 * MX-ES USB Drive by Mach Xtreme
1182 { T_DIRECT, SIP_MEDIA_REMOVABLE, "MX", "MXUB3SES*", "*"},
1183 /*quirks*/DA_Q_NO_RC16
1187 static disk_strategy_t dastrategy;
1188 static dumper_t dadump;
1189 static periph_init_t dainit;
1190 static void daasync(void *callback_arg, u_int32_t code,
1191 struct cam_path *path, void *arg);
1192 static void dasysctlinit(void *context, int pending);
1193 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
1194 static int dadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
1195 static int dadeletemaxsysctl(SYSCTL_HANDLER_ARGS);
1196 static void dadeletemethodset(struct da_softc *softc,
1197 da_delete_methods delete_method);
1198 static off_t dadeletemaxsize(struct da_softc *softc,
1199 da_delete_methods delete_method);
1200 static void dadeletemethodchoose(struct da_softc *softc,
1201 da_delete_methods default_method);
1202 static void daprobedone(struct cam_periph *periph, union ccb *ccb);
1204 static periph_ctor_t daregister;
1205 static periph_dtor_t dacleanup;
1206 static periph_start_t dastart;
1207 static periph_oninv_t daoninvalidate;
1208 static void dadone(struct cam_periph *periph,
1209 union ccb *done_ccb);
1210 static int daerror(union ccb *ccb, u_int32_t cam_flags,
1211 u_int32_t sense_flags);
1212 static void daprevent(struct cam_periph *periph, int action);
1213 static void dareprobe(struct cam_periph *periph);
1214 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
1216 struct scsi_read_capacity_data_long *rcaplong,
1218 static timeout_t dasendorderedtag;
1219 static void dashutdown(void *arg, int howto);
1220 static timeout_t damediapoll;
1222 #ifndef DA_DEFAULT_POLL_PERIOD
1223 #define DA_DEFAULT_POLL_PERIOD 3
1226 #ifndef DA_DEFAULT_TIMEOUT
1227 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
1230 #ifndef DA_DEFAULT_RETRY
1231 #define DA_DEFAULT_RETRY 4
1234 #ifndef DA_DEFAULT_SEND_ORDERED
1235 #define DA_DEFAULT_SEND_ORDERED 1
1238 #define DA_SIO (softc->sort_io_queue >= 0 ? \
1239 softc->sort_io_queue : cam_sort_io_queues)
1241 static int da_poll_period = DA_DEFAULT_POLL_PERIOD;
1242 static int da_retry_count = DA_DEFAULT_RETRY;
1243 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
1244 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
1246 static SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
1247 "CAM Direct Access Disk driver");
1248 SYSCTL_INT(_kern_cam_da, OID_AUTO, poll_period, CTLFLAG_RW,
1249 &da_poll_period, 0, "Media polling period in seconds");
1250 TUNABLE_INT("kern.cam.da.poll_period", &da_poll_period);
1251 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
1252 &da_retry_count, 0, "Normal I/O retry count");
1253 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
1254 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
1255 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
1256 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
1257 SYSCTL_INT(_kern_cam_da, OID_AUTO, send_ordered, CTLFLAG_RW,
1258 &da_send_ordered, 0, "Send Ordered Tags");
1259 TUNABLE_INT("kern.cam.da.send_ordered", &da_send_ordered);
1262 * DA_ORDEREDTAG_INTERVAL determines how often, relative
1263 * to the default timeout, we check to see whether an ordered
1264 * tagged transaction is appropriate to prevent simple tag
1265 * starvation. Since we'd like to ensure that there is at least
1266 * 1/2 of the timeout length left for a starved transaction to
1267 * complete after we've sent an ordered tag, we must poll at least
1268 * four times in every timeout period. This takes care of the worst
1269 * case where a starved transaction starts during an interval that
1270 * meets the requirement "don't send an ordered tag" test so it takes
1271 * us two intervals to determine that a tag must be sent.
1273 #ifndef DA_ORDEREDTAG_INTERVAL
1274 #define DA_ORDEREDTAG_INTERVAL 4
1277 static struct periph_driver dadriver =
1280 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
1283 PERIPHDRIVER_DECLARE(da, dadriver);
1285 static MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
1288 daopen(struct disk *dp)
1290 struct cam_periph *periph;
1291 struct da_softc *softc;
1294 periph = (struct cam_periph *)dp->d_drv1;
1295 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1299 cam_periph_lock(periph);
1300 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
1301 cam_periph_unlock(periph);
1302 cam_periph_release(periph);
1306 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1309 softc = (struct da_softc *)periph->softc;
1312 /* Wait for the disk size update. */
1313 error = cam_periph_sleep(periph, &softc->disk->d_mediasize, PRIBIO,
1316 xpt_print(periph->path, "unable to retrieve capacity data\n");
1318 if (periph->flags & CAM_PERIPH_INVALID)
1321 if (error == 0 && (softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1322 (softc->quirks & DA_Q_NO_PREVENT) == 0)
1323 daprevent(periph, PR_PREVENT);
1326 softc->flags &= ~DA_FLAG_PACK_INVALID;
1327 softc->flags |= DA_FLAG_OPEN;
1330 cam_periph_unhold(periph);
1331 cam_periph_unlock(periph);
1334 cam_periph_release(periph);
1340 daclose(struct disk *dp)
1342 struct cam_periph *periph;
1343 struct da_softc *softc;
1347 periph = (struct cam_periph *)dp->d_drv1;
1348 softc = (struct da_softc *)periph->softc;
1349 cam_periph_lock(periph);
1350 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1353 if (cam_periph_hold(periph, PRIBIO) == 0) {
1355 /* Flush disk cache. */
1356 if ((softc->flags & DA_FLAG_DIRTY) != 0 &&
1357 (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0 &&
1358 (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
1359 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1360 scsi_synchronize_cache(&ccb->csio, /*retries*/1,
1361 /*cbfcnp*/dadone, MSG_SIMPLE_Q_TAG,
1362 /*begin_lba*/0, /*lb_count*/0, SSD_FULL_SIZE,
1364 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
1365 /*sense_flags*/SF_RETRY_UA | SF_QUIET_IR,
1366 softc->disk->d_devstat);
1368 softc->flags &= ~DA_FLAG_DIRTY;
1369 xpt_release_ccb(ccb);
1372 /* Allow medium removal. */
1373 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1374 (softc->quirks & DA_Q_NO_PREVENT) == 0)
1375 daprevent(periph, PR_ALLOW);
1377 cam_periph_unhold(periph);
1381 * If we've got removeable media, mark the blocksize as
1382 * unavailable, since it could change when new media is
1385 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0)
1386 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1388 softc->flags &= ~DA_FLAG_OPEN;
1389 while (softc->refcount != 0)
1390 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "daclose", 1);
1391 cam_periph_unlock(periph);
1392 cam_periph_release(periph);
1397 daschedule(struct cam_periph *periph)
1399 struct da_softc *softc = (struct da_softc *)periph->softc;
1401 if (softc->state != DA_STATE_NORMAL)
1404 /* Check if we have more work to do. */
1405 if (bioq_first(&softc->bio_queue) ||
1406 (!softc->delete_running && bioq_first(&softc->delete_queue)) ||
1408 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1413 * Actually translate the requested transfer into one the physical driver
1414 * can understand. The transfer is described by a buf and will include
1415 * only one physical transfer.
1418 dastrategy(struct bio *bp)
1420 struct cam_periph *periph;
1421 struct da_softc *softc;
1423 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1424 softc = (struct da_softc *)periph->softc;
1426 cam_periph_lock(periph);
1429 * If the device has been made invalid, error out
1431 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1432 cam_periph_unlock(periph);
1433 biofinish(bp, NULL, ENXIO);
1437 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
1440 * Place it in the queue of disk activities for this disk
1442 if (bp->bio_cmd == BIO_DELETE) {
1443 bioq_disksort(&softc->delete_queue, bp);
1444 } else if (DA_SIO) {
1445 bioq_disksort(&softc->bio_queue, bp);
1447 bioq_insert_tail(&softc->bio_queue, bp);
1451 * Schedule ourselves for performing the work.
1454 cam_periph_unlock(periph);
1460 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1462 struct cam_periph *periph;
1463 struct da_softc *softc;
1465 struct ccb_scsiio csio;
1470 periph = dp->d_drv1;
1471 softc = (struct da_softc *)periph->softc;
1472 cam_periph_lock(periph);
1473 secsize = softc->params.secsize;
1475 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1476 cam_periph_unlock(periph);
1481 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1482 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1483 scsi_read_write(&csio,
1487 /*read*/SCSI_RW_WRITE,
1489 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1492 /*data_ptr*/(u_int8_t *) virtual,
1493 /*dxfer_len*/length,
1494 /*sense_len*/SSD_FULL_SIZE,
1495 da_default_timeout * 1000);
1496 xpt_polled_action((union ccb *)&csio);
1498 error = cam_periph_error((union ccb *)&csio,
1499 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1500 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1501 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1502 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1504 printf("Aborting dump due to I/O error.\n");
1505 cam_periph_unlock(periph);
1510 * Sync the disk cache contents to the physical media.
1512 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1514 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1515 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1516 scsi_synchronize_cache(&csio,
1520 /*begin_lba*/0,/* Cover the whole disk */
1524 xpt_polled_action((union ccb *)&csio);
1526 error = cam_periph_error((union ccb *)&csio,
1527 0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL);
1528 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1529 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1530 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1532 xpt_print(periph->path, "Synchronize cache failed\n");
1534 cam_periph_unlock(periph);
1539 dagetattr(struct bio *bp)
1542 struct cam_periph *periph;
1544 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1545 cam_periph_lock(periph);
1546 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1548 cam_periph_unlock(periph);
1550 bp->bio_completed = bp->bio_length;
1560 * Install a global async callback. This callback will
1561 * receive async callbacks like "new device found".
1563 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1565 if (status != CAM_REQ_CMP) {
1566 printf("da: Failed to attach master async callback "
1567 "due to status 0x%x!\n", status);
1568 } else if (da_send_ordered) {
1570 /* Register our shutdown event handler */
1571 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1572 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1573 printf("dainit: shutdown event registration failed!\n");
1578 * Callback from GEOM, called when it has finished cleaning up its
1582 dadiskgonecb(struct disk *dp)
1584 struct cam_periph *periph;
1586 periph = (struct cam_periph *)dp->d_drv1;
1587 cam_periph_release(periph);
1591 daoninvalidate(struct cam_periph *periph)
1593 struct da_softc *softc;
1595 softc = (struct da_softc *)periph->softc;
1598 * De-register any async callbacks.
1600 xpt_register_async(0, daasync, periph, periph->path);
1602 softc->flags |= DA_FLAG_PACK_INVALID;
1605 * Return all queued I/O with ENXIO.
1606 * XXX Handle any transactions queued to the card
1607 * with XPT_ABORT_CCB.
1609 bioq_flush(&softc->bio_queue, NULL, ENXIO);
1610 bioq_flush(&softc->delete_queue, NULL, ENXIO);
1613 * Tell GEOM that we've gone away, we'll get a callback when it is
1614 * done cleaning up its resources.
1616 disk_gone(softc->disk);
1620 dacleanup(struct cam_periph *periph)
1622 struct da_softc *softc;
1624 softc = (struct da_softc *)periph->softc;
1626 cam_periph_unlock(periph);
1629 * If we can't free the sysctl tree, oh well...
1631 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1632 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1633 xpt_print(periph->path, "can't remove sysctl context\n");
1636 callout_drain(&softc->mediapoll_c);
1637 disk_destroy(softc->disk);
1638 callout_drain(&softc->sendordered_c);
1639 free(softc, M_DEVBUF);
1640 cam_periph_lock(periph);
1644 daasync(void *callback_arg, u_int32_t code,
1645 struct cam_path *path, void *arg)
1647 struct cam_periph *periph;
1648 struct da_softc *softc;
1650 periph = (struct cam_periph *)callback_arg;
1652 case AC_FOUND_DEVICE:
1654 struct ccb_getdev *cgd;
1657 cgd = (struct ccb_getdev *)arg;
1661 if (cgd->protocol != PROTO_SCSI)
1664 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1665 && SID_TYPE(&cgd->inq_data) != T_RBC
1666 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1670 * Allocate a peripheral instance for
1671 * this device and start the probe
1674 status = cam_periph_alloc(daregister, daoninvalidate,
1676 "da", CAM_PERIPH_BIO,
1678 AC_FOUND_DEVICE, cgd);
1680 if (status != CAM_REQ_CMP
1681 && status != CAM_REQ_INPROG)
1682 printf("daasync: Unable to attach to new device "
1683 "due to status 0x%x\n", status);
1686 case AC_ADVINFO_CHANGED:
1690 buftype = (uintptr_t)arg;
1691 if (buftype == CDAI_TYPE_PHYS_PATH) {
1692 struct da_softc *softc;
1694 softc = periph->softc;
1695 disk_attr_changed(softc->disk, "GEOM::physpath",
1700 case AC_UNIT_ATTENTION:
1703 int error_code, sense_key, asc, ascq;
1705 softc = (struct da_softc *)periph->softc;
1706 ccb = (union ccb *)arg;
1709 * Handle all UNIT ATTENTIONs except our own,
1710 * as they will be handled by daerror().
1712 if (xpt_path_periph(ccb->ccb_h.path) != periph &&
1713 scsi_extract_sense_ccb(ccb,
1714 &error_code, &sense_key, &asc, &ascq)) {
1715 if (asc == 0x2A && ascq == 0x09) {
1716 xpt_print(ccb->ccb_h.path,
1717 "Capacity data has changed\n");
1718 softc->flags &= ~DA_FLAG_PROBED;
1720 } else if (asc == 0x28 && ascq == 0x00) {
1721 softc->flags &= ~DA_FLAG_PROBED;
1722 disk_media_changed(softc->disk, M_NOWAIT);
1723 } else if (asc == 0x3F && ascq == 0x03) {
1724 xpt_print(ccb->ccb_h.path,
1725 "INQUIRY data has changed\n");
1726 softc->flags &= ~DA_FLAG_PROBED;
1730 cam_periph_async(periph, code, path, arg);
1734 softc = (struct da_softc *)periph->softc;
1736 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
1745 struct ccb_hdr *ccbh;
1747 softc = (struct da_softc *)periph->softc;
1749 * Don't fail on the expected unit attention
1752 softc->flags |= DA_FLAG_RETRY_UA;
1753 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1754 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1760 cam_periph_async(periph, code, path, arg);
1764 dasysctlinit(void *context, int pending)
1766 struct cam_periph *periph;
1767 struct da_softc *softc;
1768 char tmpstr[80], tmpstr2[80];
1769 struct ccb_trans_settings cts;
1771 periph = (struct cam_periph *)context;
1773 * periph was held for us when this task was enqueued
1775 if (periph->flags & CAM_PERIPH_INVALID) {
1776 cam_periph_release(periph);
1780 softc = (struct da_softc *)periph->softc;
1781 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1782 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1784 sysctl_ctx_init(&softc->sysctl_ctx);
1785 softc->flags |= DA_FLAG_SCTX_INIT;
1786 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1787 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1788 CTLFLAG_RD, 0, tmpstr);
1789 if (softc->sysctl_tree == NULL) {
1790 printf("dasysctlinit: unable to allocate sysctl tree\n");
1791 cam_periph_release(periph);
1796 * Now register the sysctl handler, so the user can change the value on
1799 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1800 OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RW,
1801 softc, 0, dadeletemethodsysctl, "A",
1802 "BIO_DELETE execution method");
1803 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1804 OID_AUTO, "delete_max", CTLTYPE_U64 | CTLFLAG_RW,
1805 softc, 0, dadeletemaxsysctl, "Q",
1806 "Maximum BIO_DELETE size");
1807 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1808 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1809 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1810 "Minimum CDB size");
1811 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1812 OID_AUTO, "sort_io_queue", CTLFLAG_RW, &softc->sort_io_queue, 0,
1813 "Sort IO queue to try and optimise disk access patterns");
1815 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1816 SYSCTL_CHILDREN(softc->sysctl_tree),
1820 &softc->error_inject,
1822 "error_inject leaf");
1826 * Add some addressing info.
1828 memset(&cts, 0, sizeof (cts));
1829 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1830 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1831 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1832 cam_periph_lock(periph);
1833 xpt_action((union ccb *)&cts);
1834 cam_periph_unlock(periph);
1835 if (cts.ccb_h.status != CAM_REQ_CMP) {
1836 cam_periph_release(periph);
1839 if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
1840 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1841 if (fc->valid & CTS_FC_VALID_WWPN) {
1842 softc->wwpn = fc->wwpn;
1843 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1844 SYSCTL_CHILDREN(softc->sysctl_tree),
1845 OID_AUTO, "wwpn", CTLFLAG_RD,
1846 &softc->wwpn, "World Wide Port Name");
1849 cam_periph_release(periph);
1853 dadeletemaxsysctl(SYSCTL_HANDLER_ARGS)
1857 struct da_softc *softc;
1859 softc = (struct da_softc *)arg1;
1861 value = softc->disk->d_delmaxsize;
1862 error = sysctl_handle_64(oidp, &value, 0, req);
1863 if ((error != 0) || (req->newptr == NULL))
1866 /* only accept values smaller than the calculated value */
1867 if (value > dadeletemaxsize(softc, softc->delete_method)) {
1870 softc->disk->d_delmaxsize = value;
1876 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1880 value = *(int *)arg1;
1882 error = sysctl_handle_int(oidp, &value, 0, req);
1885 || (req->newptr == NULL))
1889 * Acceptable values here are 6, 10, 12 or 16.
1893 else if ((value > 6)
1896 else if ((value > 10)
1899 else if (value > 12)
1902 *(int *)arg1 = value;
1908 dadeletemethodset(struct da_softc *softc, da_delete_methods delete_method)
1912 softc->delete_method = delete_method;
1913 softc->disk->d_delmaxsize = dadeletemaxsize(softc, delete_method);
1914 softc->delete_func = da_delete_functions[delete_method];
1916 if (softc->delete_method > DA_DELETE_DISABLE)
1917 softc->disk->d_flags |= DISKFLAG_CANDELETE;
1919 softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
1923 dadeletemaxsize(struct da_softc *softc, da_delete_methods delete_method)
1927 switch(delete_method) {
1928 case DA_DELETE_UNMAP:
1929 sectors = (off_t)softc->unmap_max_lba;
1931 case DA_DELETE_ATA_TRIM:
1932 sectors = (off_t)ATA_DSM_RANGE_MAX * softc->trim_max_ranges;
1934 case DA_DELETE_WS16:
1935 sectors = omin(softc->ws_max_blks, WS16_MAX_BLKS);
1937 case DA_DELETE_ZERO:
1938 case DA_DELETE_WS10:
1939 sectors = omin(softc->ws_max_blks, WS10_MAX_BLKS);
1945 return (off_t)softc->params.secsize *
1946 omin(sectors, softc->params.sectors);
1950 daprobedone(struct cam_periph *periph, union ccb *ccb)
1952 struct da_softc *softc;
1954 softc = (struct da_softc *)periph->softc;
1956 dadeletemethodchoose(softc, DA_DELETE_NONE);
1958 if (bootverbose && (softc->flags & DA_FLAG_ANNOUNCED) == 0) {
1962 snprintf(buf, sizeof(buf), "Delete methods: <");
1964 for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
1965 if (softc->delete_available & (1 << i)) {
1967 strlcat(buf, ",", sizeof(buf));
1971 strlcat(buf, da_delete_method_names[i],
1973 if (i == softc->delete_method) {
1974 strlcat(buf, "(*)", sizeof(buf));
1979 if (softc->delete_method == DA_DELETE_NONE)
1980 strlcat(buf, "NONE(*)", sizeof(buf));
1982 strlcat(buf, "DISABLED(*)", sizeof(buf));
1984 strlcat(buf, ">", sizeof(buf));
1985 printf("%s%d: %s\n", periph->periph_name,
1986 periph->unit_number, buf);
1990 * Since our peripheral may be invalidated by an error
1991 * above or an external event, we must release our CCB
1992 * before releasing the probe lock on the peripheral.
1993 * The peripheral will only go away once the last lock
1994 * is removed, and we need it around for the CCB release
1997 xpt_release_ccb(ccb);
1998 softc->state = DA_STATE_NORMAL;
1999 softc->flags |= DA_FLAG_PROBED;
2001 wakeup(&softc->disk->d_mediasize);
2002 if ((softc->flags & DA_FLAG_ANNOUNCED) == 0) {
2003 softc->flags |= DA_FLAG_ANNOUNCED;
2004 cam_periph_unhold(periph);
2006 cam_periph_release_locked(periph);
2010 dadeletemethodchoose(struct da_softc *softc, da_delete_methods default_method)
2012 int i, delete_method;
2014 delete_method = default_method;
2017 * Use the pre-defined order to choose the best
2018 * performing delete.
2020 for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
2021 if (softc->delete_available & (1 << i)) {
2022 dadeletemethodset(softc, i);
2026 dadeletemethodset(softc, delete_method);
2030 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
2034 struct da_softc *softc;
2035 int i, error, methods, value;
2037 softc = (struct da_softc *)arg1;
2039 value = softc->delete_method;
2040 if (value < 0 || value > DA_DELETE_MAX)
2043 p = da_delete_method_names[value];
2044 strncpy(buf, p, sizeof(buf));
2045 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2046 if (error != 0 || req->newptr == NULL)
2048 methods = softc->delete_available | (1 << DA_DELETE_DISABLE);
2049 for (i = 0; i <= DA_DELETE_MAX; i++) {
2050 if (!(methods & (1 << i)) ||
2051 strcmp(buf, da_delete_method_names[i]) != 0)
2053 dadeletemethodset(softc, i);
2060 daregister(struct cam_periph *periph, void *arg)
2062 struct da_softc *softc;
2063 struct ccb_pathinq cpi;
2064 struct ccb_getdev *cgd;
2068 cgd = (struct ccb_getdev *)arg;
2070 printf("daregister: no getdev CCB, can't register device\n");
2071 return(CAM_REQ_CMP_ERR);
2074 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
2077 if (softc == NULL) {
2078 printf("daregister: Unable to probe new device. "
2079 "Unable to allocate softc\n");
2080 return(CAM_REQ_CMP_ERR);
2083 LIST_INIT(&softc->pending_ccbs);
2084 softc->state = DA_STATE_PROBE_RC;
2085 bioq_init(&softc->bio_queue);
2086 bioq_init(&softc->delete_queue);
2087 bioq_init(&softc->delete_run_queue);
2088 if (SID_IS_REMOVABLE(&cgd->inq_data))
2089 softc->flags |= DA_FLAG_PACK_REMOVABLE;
2090 softc->unmap_max_ranges = UNMAP_MAX_RANGES;
2091 softc->unmap_max_lba = UNMAP_RANGE_MAX;
2092 softc->ws_max_blks = WS16_MAX_BLKS;
2093 softc->trim_max_ranges = ATA_TRIM_MAX_RANGES;
2094 softc->sort_io_queue = -1;
2096 periph->softc = softc;
2099 * See if this device has any quirks.
2101 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
2102 (caddr_t)da_quirk_table,
2103 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
2104 sizeof(*da_quirk_table), scsi_inquiry_match);
2107 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
2109 softc->quirks = DA_Q_NONE;
2111 /* Check if the SIM does not want 6 byte commands */
2112 bzero(&cpi, sizeof(cpi));
2113 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2114 cpi.ccb_h.func_code = XPT_PATH_INQ;
2115 xpt_action((union ccb *)&cpi);
2116 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
2117 softc->quirks |= DA_Q_NO_6_BYTE;
2119 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
2122 * Take an exclusive refcount on the periph while dastart is called
2123 * to finish the probe. The reference will be dropped in dadone at
2126 (void)cam_periph_hold(periph, PRIBIO);
2129 * Schedule a periodic event to occasionally send an
2130 * ordered tag to a device.
2132 callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0);
2133 callout_reset(&softc->sendordered_c,
2134 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2135 dasendorderedtag, softc);
2137 cam_periph_unlock(periph);
2139 * RBC devices don't have to support READ(6), only READ(10).
2141 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
2142 softc->minimum_cmd_size = 10;
2144 softc->minimum_cmd_size = 6;
2147 * Load the user's default, if any.
2149 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
2150 periph->unit_number);
2151 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
2154 * 6, 10, 12 and 16 are the currently permissible values.
2156 if (softc->minimum_cmd_size < 6)
2157 softc->minimum_cmd_size = 6;
2158 else if ((softc->minimum_cmd_size > 6)
2159 && (softc->minimum_cmd_size <= 10))
2160 softc->minimum_cmd_size = 10;
2161 else if ((softc->minimum_cmd_size > 10)
2162 && (softc->minimum_cmd_size <= 12))
2163 softc->minimum_cmd_size = 12;
2164 else if (softc->minimum_cmd_size > 12)
2165 softc->minimum_cmd_size = 16;
2167 /* Predict whether device may support READ CAPACITY(16). */
2168 if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3 &&
2169 (softc->quirks & DA_Q_NO_RC16) == 0) {
2170 softc->flags |= DA_FLAG_CAN_RC16;
2171 softc->state = DA_STATE_PROBE_RC16;
2175 * Register this media as a disk.
2177 softc->disk = disk_alloc();
2178 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
2179 periph->unit_number, 0,
2180 DEVSTAT_BS_UNAVAILABLE,
2181 SID_TYPE(&cgd->inq_data) |
2182 XPORT_DEVSTAT_TYPE(cpi.transport),
2183 DEVSTAT_PRIORITY_DISK);
2184 softc->disk->d_open = daopen;
2185 softc->disk->d_close = daclose;
2186 softc->disk->d_strategy = dastrategy;
2187 softc->disk->d_dump = dadump;
2188 softc->disk->d_getattr = dagetattr;
2189 softc->disk->d_gone = dadiskgonecb;
2190 softc->disk->d_name = "da";
2191 softc->disk->d_drv1 = periph;
2193 softc->maxio = DFLTPHYS; /* traditional default */
2194 else if (cpi.maxio > MAXPHYS)
2195 softc->maxio = MAXPHYS; /* for safety */
2197 softc->maxio = cpi.maxio;
2198 softc->disk->d_maxsize = softc->maxio;
2199 softc->disk->d_unit = periph->unit_number;
2200 softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION;
2201 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
2202 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
2203 if ((cpi.hba_misc & PIM_UNMAPPED) != 0)
2204 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
2205 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
2206 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
2207 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
2208 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
2209 cgd->inq_data.product, sizeof(cgd->inq_data.product),
2210 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
2211 softc->disk->d_hba_vendor = cpi.hba_vendor;
2212 softc->disk->d_hba_device = cpi.hba_device;
2213 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
2214 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
2217 * Acquire a reference to the periph before we register with GEOM.
2218 * We'll release this reference once GEOM calls us back (via
2219 * dadiskgonecb()) telling us that our provider has been freed.
2221 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
2222 xpt_print(periph->path, "%s: lost periph during "
2223 "registration!\n", __func__);
2224 cam_periph_lock(periph);
2225 return (CAM_REQ_CMP_ERR);
2228 disk_create(softc->disk, DISK_VERSION);
2229 cam_periph_lock(periph);
2232 * Add async callbacks for events of interest.
2233 * I don't bother checking if this fails as,
2234 * in most cases, the system will function just
2235 * fine without them and the only alternative
2236 * would be to not attach the device on failure.
2238 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
2239 AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION,
2240 daasync, periph, periph->path);
2243 * Emit an attribute changed notification just in case
2244 * physical path information arrived before our async
2245 * event handler was registered, but after anyone attaching
2246 * to our disk device polled it.
2248 disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
2251 * Schedule a periodic media polling events.
2253 callout_init_mtx(&softc->mediapoll_c, cam_periph_mtx(periph), 0);
2254 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
2255 (cgd->inq_flags & SID_AEN) == 0 &&
2256 da_poll_period != 0)
2257 callout_reset(&softc->mediapoll_c, da_poll_period * hz,
2258 damediapoll, periph);
2260 xpt_schedule(periph, CAM_PRIORITY_DEV);
2262 return(CAM_REQ_CMP);
2266 dastart(struct cam_periph *periph, union ccb *start_ccb)
2268 struct da_softc *softc;
2270 softc = (struct da_softc *)periph->softc;
2272 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
2275 switch (softc->state) {
2276 case DA_STATE_NORMAL:
2281 /* Run BIO_DELETE if not running yet. */
2282 if (!softc->delete_running &&
2283 (bp = bioq_first(&softc->delete_queue)) != NULL) {
2284 if (softc->delete_func != NULL) {
2285 softc->delete_func(periph, start_ccb, bp);
2288 bioq_flush(&softc->delete_queue, NULL, 0);
2293 /* Run regular command. */
2294 bp = bioq_takefirst(&softc->bio_queue);
2298 scsi_test_unit_ready(&start_ccb->csio,
2299 /*retries*/ da_retry_count,
2303 da_default_timeout * 1000);
2304 start_ccb->ccb_h.ccb_bp = NULL;
2305 start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
2306 xpt_action(start_ccb);
2308 xpt_release_ccb(start_ccb);
2313 cam_periph_release_locked(periph);
2316 if ((bp->bio_flags & BIO_ORDERED) != 0 ||
2317 (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
2318 softc->flags &= ~DA_FLAG_NEED_OTAG;
2319 softc->flags |= DA_FLAG_WAS_OTAG;
2320 tag_code = MSG_ORDERED_Q_TAG;
2322 tag_code = MSG_SIMPLE_Q_TAG;
2325 switch (bp->bio_cmd) {
2327 softc->flags |= DA_FLAG_DIRTY;
2330 scsi_read_write(&start_ccb->csio,
2331 /*retries*/da_retry_count,
2333 /*tag_action*/tag_code,
2334 /*read_op*/(bp->bio_cmd == BIO_READ ?
2335 SCSI_RW_READ : SCSI_RW_WRITE) |
2336 ((bp->bio_flags & BIO_UNMAPPED) != 0 ?
2339 softc->minimum_cmd_size,
2340 /*lba*/bp->bio_pblkno,
2341 /*block_count*/bp->bio_bcount /
2342 softc->params.secsize,
2343 /*data_ptr*/ (bp->bio_flags &
2344 BIO_UNMAPPED) != 0 ? (void *)bp :
2346 /*dxfer_len*/ bp->bio_bcount,
2347 /*sense_len*/SSD_FULL_SIZE,
2348 da_default_timeout * 1000);
2352 * BIO_FLUSH doesn't currently communicate
2353 * range data, so we synchronize the cache
2354 * over the whole disk. We also force
2355 * ordered tag semantics the flush applies
2356 * to all previously queued I/O.
2358 scsi_synchronize_cache(&start_ccb->csio,
2365 da_default_timeout*1000);
2368 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
2369 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
2372 LIST_INSERT_HEAD(&softc->pending_ccbs,
2373 &start_ccb->ccb_h, periph_links.le);
2375 /* We expect a unit attention from this device */
2376 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
2377 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
2378 softc->flags &= ~DA_FLAG_RETRY_UA;
2381 start_ccb->ccb_h.ccb_bp = bp;
2383 cam_periph_unlock(periph);
2384 xpt_action(start_ccb);
2385 cam_periph_lock(periph);
2388 /* May have more work to do, so ensure we stay scheduled */
2392 case DA_STATE_PROBE_RC:
2394 struct scsi_read_capacity_data *rcap;
2396 rcap = (struct scsi_read_capacity_data *)
2397 malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
2399 printf("dastart: Couldn't malloc read_capacity data\n");
2400 /* da_free_periph??? */
2403 scsi_read_capacity(&start_ccb->csio,
2404 /*retries*/da_retry_count,
2410 start_ccb->ccb_h.ccb_bp = NULL;
2411 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC;
2412 xpt_action(start_ccb);
2415 case DA_STATE_PROBE_RC16:
2417 struct scsi_read_capacity_data_long *rcaplong;
2419 rcaplong = (struct scsi_read_capacity_data_long *)
2420 malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
2421 if (rcaplong == NULL) {
2422 printf("dastart: Couldn't malloc read_capacity data\n");
2423 /* da_free_periph??? */
2426 scsi_read_capacity_16(&start_ccb->csio,
2427 /*retries*/ da_retry_count,
2429 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2433 /*rcap_buf*/ (uint8_t *)rcaplong,
2434 /*rcap_buf_len*/ sizeof(*rcaplong),
2435 /*sense_len*/ SSD_FULL_SIZE,
2436 /*timeout*/ da_default_timeout * 1000);
2437 start_ccb->ccb_h.ccb_bp = NULL;
2438 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC16;
2439 xpt_action(start_ccb);
2442 case DA_STATE_PROBE_LBP:
2444 struct scsi_vpd_logical_block_prov *lbp;
2446 if (!scsi_vpd_supported_page(periph, SVPD_LBP)) {
2448 * If we get here we don't support any SBC-3 delete
2449 * methods with UNMAP as the Logical Block Provisioning
2450 * VPD page support is required for devices which
2451 * support it according to T10/1799-D Revision 31
2452 * however older revisions of the spec don't mandate
2453 * this so we currently don't remove these methods
2454 * from the available set.
2456 softc->state = DA_STATE_PROBE_BLK_LIMITS;
2460 lbp = (struct scsi_vpd_logical_block_prov *)
2461 malloc(sizeof(*lbp), M_SCSIDA, M_NOWAIT|M_ZERO);
2464 printf("dastart: Couldn't malloc lbp data\n");
2465 /* da_free_periph??? */
2469 scsi_inquiry(&start_ccb->csio,
2470 /*retries*/da_retry_count,
2472 /*tag_action*/MSG_SIMPLE_Q_TAG,
2473 /*inq_buf*/(u_int8_t *)lbp,
2474 /*inq_len*/sizeof(*lbp),
2476 /*page_code*/SVPD_LBP,
2477 /*sense_len*/SSD_MIN_SIZE,
2478 /*timeout*/da_default_timeout * 1000);
2479 start_ccb->ccb_h.ccb_bp = NULL;
2480 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_LBP;
2481 xpt_action(start_ccb);
2484 case DA_STATE_PROBE_BLK_LIMITS:
2486 struct scsi_vpd_block_limits *block_limits;
2488 if (!scsi_vpd_supported_page(periph, SVPD_BLOCK_LIMITS)) {
2489 /* Not supported skip to next probe */
2490 softc->state = DA_STATE_PROBE_BDC;
2494 block_limits = (struct scsi_vpd_block_limits *)
2495 malloc(sizeof(*block_limits), M_SCSIDA, M_NOWAIT|M_ZERO);
2497 if (block_limits == NULL) {
2498 printf("dastart: Couldn't malloc block_limits data\n");
2499 /* da_free_periph??? */
2503 scsi_inquiry(&start_ccb->csio,
2504 /*retries*/da_retry_count,
2506 /*tag_action*/MSG_SIMPLE_Q_TAG,
2507 /*inq_buf*/(u_int8_t *)block_limits,
2508 /*inq_len*/sizeof(*block_limits),
2510 /*page_code*/SVPD_BLOCK_LIMITS,
2511 /*sense_len*/SSD_MIN_SIZE,
2512 /*timeout*/da_default_timeout * 1000);
2513 start_ccb->ccb_h.ccb_bp = NULL;
2514 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BLK_LIMITS;
2515 xpt_action(start_ccb);
2518 case DA_STATE_PROBE_BDC:
2520 struct scsi_vpd_block_characteristics *bdc;
2522 if (!scsi_vpd_supported_page(periph, SVPD_BDC)) {
2523 softc->state = DA_STATE_PROBE_ATA;
2527 bdc = (struct scsi_vpd_block_characteristics *)
2528 malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
2531 printf("dastart: Couldn't malloc bdc data\n");
2532 /* da_free_periph??? */
2536 scsi_inquiry(&start_ccb->csio,
2537 /*retries*/da_retry_count,
2539 /*tag_action*/MSG_SIMPLE_Q_TAG,
2540 /*inq_buf*/(u_int8_t *)bdc,
2541 /*inq_len*/sizeof(*bdc),
2543 /*page_code*/SVPD_BDC,
2544 /*sense_len*/SSD_MIN_SIZE,
2545 /*timeout*/da_default_timeout * 1000);
2546 start_ccb->ccb_h.ccb_bp = NULL;
2547 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BDC;
2548 xpt_action(start_ccb);
2551 case DA_STATE_PROBE_ATA:
2553 struct ata_params *ata_params;
2555 if (!scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
2556 daprobedone(periph, start_ccb);
2560 ata_params = (struct ata_params*)
2561 malloc(sizeof(*ata_params), M_SCSIDA, M_NOWAIT|M_ZERO);
2563 if (ata_params == NULL) {
2564 printf("dastart: Couldn't malloc ata_params data\n");
2565 /* da_free_periph??? */
2569 scsi_ata_identify(&start_ccb->csio,
2570 /*retries*/da_retry_count,
2572 /*tag_action*/MSG_SIMPLE_Q_TAG,
2573 /*data_ptr*/(u_int8_t *)ata_params,
2574 /*dxfer_len*/sizeof(*ata_params),
2575 /*sense_len*/SSD_FULL_SIZE,
2576 /*timeout*/da_default_timeout * 1000);
2577 start_ccb->ccb_h.ccb_bp = NULL;
2578 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA;
2579 xpt_action(start_ccb);
2586 * In each of the methods below, while its the caller's
2587 * responsibility to ensure the request will fit into a
2588 * single device request, we might have changed the delete
2589 * method due to the device incorrectly advertising either
2590 * its supported methods or limits.
2592 * To prevent this causing further issues we validate the
2593 * against the methods limits, and warn which would
2594 * otherwise be unnecessary.
2597 da_delete_unmap(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2599 struct da_softc *softc = (struct da_softc *)periph->softc;;
2601 uint8_t *buf = softc->unmap_buf;
2602 uint64_t lba, lastlba = (uint64_t)-1;
2603 uint64_t totalcount = 0;
2605 uint32_t lastcount = 0, c;
2606 uint32_t off, ranges = 0;
2609 * Currently this doesn't take the UNMAP
2610 * Granularity and Granularity Alignment
2611 * fields into account.
2613 * This could result in both unoptimal unmap
2614 * requests as as well as UNMAP calls unmapping
2615 * fewer LBA's than requested.
2618 softc->delete_running = 1;
2619 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2622 bioq_remove(&softc->delete_queue, bp1);
2624 bioq_insert_tail(&softc->delete_run_queue, bp1);
2625 lba = bp1->bio_pblkno;
2626 count = bp1->bio_bcount / softc->params.secsize;
2628 /* Try to extend the previous range. */
2629 if (lba == lastlba) {
2630 c = omin(count, UNMAP_RANGE_MAX - lastcount);
2632 off = ((ranges - 1) * UNMAP_RANGE_SIZE) +
2634 scsi_ulto4b(lastcount, &buf[off + 8]);
2641 c = omin(count, UNMAP_RANGE_MAX);
2642 if (totalcount + c > softc->unmap_max_lba ||
2643 ranges >= softc->unmap_max_ranges) {
2644 xpt_print(periph->path,
2645 "%s issuing short delete %ld > %ld"
2647 da_delete_method_desc[softc->delete_method],
2648 totalcount + c, softc->unmap_max_lba,
2649 ranges, softc->unmap_max_ranges);
2652 off = (ranges * UNMAP_RANGE_SIZE) + UNMAP_HEAD_SIZE;
2653 scsi_u64to8b(lba, &buf[off + 0]);
2654 scsi_ulto4b(c, &buf[off + 8]);
2662 bp1 = bioq_first(&softc->delete_queue);
2663 if (bp1 == NULL || ranges >= softc->unmap_max_ranges ||
2664 totalcount + bp1->bio_bcount /
2665 softc->params.secsize > softc->unmap_max_lba)
2668 scsi_ulto2b(ranges * 16 + 6, &buf[0]);
2669 scsi_ulto2b(ranges * 16, &buf[2]);
2671 scsi_unmap(&ccb->csio,
2672 /*retries*/da_retry_count,
2674 /*tag_action*/MSG_SIMPLE_Q_TAG,
2677 /*dxfer_len*/ ranges * 16 + 8,
2678 /*sense_len*/SSD_FULL_SIZE,
2679 da_default_timeout * 1000);
2680 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2681 ccb->ccb_h.flags |= CAM_UNLOCKED;
2685 da_delete_trim(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2687 struct da_softc *softc = (struct da_softc *)periph->softc;
2689 uint8_t *buf = softc->unmap_buf;
2690 uint64_t lastlba = (uint64_t)-1;
2693 uint32_t lastcount = 0, c, requestcount;
2694 int ranges = 0, off, block_count;
2696 softc->delete_running = 1;
2697 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2700 bioq_remove(&softc->delete_queue, bp1);
2702 bioq_insert_tail(&softc->delete_run_queue, bp1);
2703 lba = bp1->bio_pblkno;
2704 count = bp1->bio_bcount / softc->params.secsize;
2705 requestcount = count;
2707 /* Try to extend the previous range. */
2708 if (lba == lastlba) {
2709 c = omin(count, ATA_DSM_RANGE_MAX - lastcount);
2711 off = (ranges - 1) * 8;
2712 buf[off + 6] = lastcount & 0xff;
2713 buf[off + 7] = (lastcount >> 8) & 0xff;
2719 c = omin(count, ATA_DSM_RANGE_MAX);
2722 buf[off + 0] = lba & 0xff;
2723 buf[off + 1] = (lba >> 8) & 0xff;
2724 buf[off + 2] = (lba >> 16) & 0xff;
2725 buf[off + 3] = (lba >> 24) & 0xff;
2726 buf[off + 4] = (lba >> 32) & 0xff;
2727 buf[off + 5] = (lba >> 40) & 0xff;
2728 buf[off + 6] = c & 0xff;
2729 buf[off + 7] = (c >> 8) & 0xff;
2734 if (count != 0 && ranges == softc->trim_max_ranges) {
2735 xpt_print(periph->path,
2736 "%s issuing short delete %ld > %ld\n",
2737 da_delete_method_desc[softc->delete_method],
2739 (softc->trim_max_ranges - ranges) *
2745 bp1 = bioq_first(&softc->delete_queue);
2746 if (bp1 == NULL || bp1->bio_bcount / softc->params.secsize >
2747 (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX)
2751 block_count = (ranges + ATA_DSM_BLK_RANGES - 1) / ATA_DSM_BLK_RANGES;
2752 scsi_ata_trim(&ccb->csio,
2753 /*retries*/da_retry_count,
2755 /*tag_action*/MSG_SIMPLE_Q_TAG,
2758 /*dxfer_len*/block_count * ATA_DSM_BLK_SIZE,
2759 /*sense_len*/SSD_FULL_SIZE,
2760 da_default_timeout * 1000);
2761 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2762 ccb->ccb_h.flags |= CAM_UNLOCKED;
2766 * We calculate ws_max_blks here based off d_delmaxsize instead
2767 * of using softc->ws_max_blks as it is absolute max for the
2768 * device not the protocol max which may well be lower.
2771 da_delete_ws(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2773 struct da_softc *softc;
2775 uint64_t ws_max_blks;
2777 uint64_t count; /* forward compat with WS32 */
2779 softc = (struct da_softc *)periph->softc;
2780 ws_max_blks = softc->disk->d_delmaxsize / softc->params.secsize;
2781 softc->delete_running = 1;
2782 lba = bp->bio_pblkno;
2786 bioq_remove(&softc->delete_queue, bp1);
2788 bioq_insert_tail(&softc->delete_run_queue, bp1);
2789 count += bp1->bio_bcount / softc->params.secsize;
2790 if (count > ws_max_blks) {
2791 xpt_print(periph->path,
2792 "%s issuing short delete %ld > %ld\n",
2793 da_delete_method_desc[softc->delete_method],
2794 count, ws_max_blks);
2795 count = omin(count, ws_max_blks);
2798 bp1 = bioq_first(&softc->delete_queue);
2799 if (bp1 == NULL || lba + count != bp1->bio_pblkno ||
2800 count + bp1->bio_bcount /
2801 softc->params.secsize > ws_max_blks)
2805 scsi_write_same(&ccb->csio,
2806 /*retries*/da_retry_count,
2808 /*tag_action*/MSG_SIMPLE_Q_TAG,
2809 /*byte2*/softc->delete_method ==
2810 DA_DELETE_ZERO ? 0 : SWS_UNMAP,
2811 softc->delete_method == DA_DELETE_WS16 ? 16 : 10,
2813 /*block_count*/count,
2814 /*data_ptr*/ __DECONST(void *, zero_region),
2815 /*dxfer_len*/ softc->params.secsize,
2816 /*sense_len*/SSD_FULL_SIZE,
2817 da_default_timeout * 1000);
2818 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2819 ccb->ccb_h.flags |= CAM_UNLOCKED;
2823 cmd6workaround(union ccb *ccb)
2825 struct scsi_rw_6 cmd6;
2826 struct scsi_rw_10 *cmd10;
2827 struct da_softc *softc;
2832 cdb = ccb->csio.cdb_io.cdb_bytes;
2833 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
2835 if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
2836 da_delete_methods old_method = softc->delete_method;
2839 * Typically there are two reasons for failure here
2840 * 1. Delete method was detected as supported but isn't
2841 * 2. Delete failed due to invalid params e.g. too big
2843 * While we will attempt to choose an alternative delete method
2844 * this may result in short deletes if the existing delete
2845 * requests from geom are big for the new method choosen.
2847 * This method assumes that the error which triggered this
2848 * will not retry the io otherwise a panic will occur
2850 dadeleteflag(softc, old_method, 0);
2851 dadeletemethodchoose(softc, DA_DELETE_DISABLE);
2852 if (softc->delete_method == DA_DELETE_DISABLE)
2853 xpt_print(ccb->ccb_h.path,
2854 "%s failed, disabling BIO_DELETE\n",
2855 da_delete_method_desc[old_method]);
2857 xpt_print(ccb->ccb_h.path,
2858 "%s failed, switching to %s BIO_DELETE\n",
2859 da_delete_method_desc[old_method],
2860 da_delete_method_desc[softc->delete_method]);
2862 while ((bp = bioq_takefirst(&softc->delete_run_queue)) != NULL)
2863 bioq_disksort(&softc->delete_queue, bp);
2864 bioq_disksort(&softc->delete_queue,
2865 (struct bio *)ccb->ccb_h.ccb_bp);
2866 ccb->ccb_h.ccb_bp = NULL;
2870 /* Detect unsupported PREVENT ALLOW MEDIUM REMOVAL. */
2871 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
2872 (*cdb == PREVENT_ALLOW) &&
2873 (softc->quirks & DA_Q_NO_PREVENT) == 0) {
2875 xpt_print(ccb->ccb_h.path,
2876 "PREVENT ALLOW MEDIUM REMOVAL not supported.\n");
2877 softc->quirks |= DA_Q_NO_PREVENT;
2881 /* Detect unsupported SYNCHRONIZE CACHE(10). */
2882 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
2883 (*cdb == SYNCHRONIZE_CACHE) &&
2884 (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
2886 xpt_print(ccb->ccb_h.path,
2887 "SYNCHRONIZE CACHE(10) not supported.\n");
2888 softc->quirks |= DA_Q_NO_SYNC_CACHE;
2889 softc->disk->d_flags &= ~DISKFLAG_CANFLUSHCACHE;
2893 /* Translation only possible if CDB is an array and cmd is R/W6 */
2894 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
2895 (*cdb != READ_6 && *cdb != WRITE_6))
2898 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
2899 "increasing minimum_cmd_size to 10.\n");
2900 softc->minimum_cmd_size = 10;
2902 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
2903 cmd10 = (struct scsi_rw_10 *)cdb;
2904 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
2906 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
2907 cmd10->reserved = 0;
2908 scsi_ulto2b(cmd6.length, cmd10->length);
2909 cmd10->control = cmd6.control;
2910 ccb->csio.cdb_len = sizeof(*cmd10);
2912 /* Requeue request, unfreezing queue if necessary */
2913 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
2914 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2917 cam_release_devq(ccb->ccb_h.path,
2921 /*getcount_only*/0);
2927 dadone(struct cam_periph *periph, union ccb *done_ccb)
2929 struct da_softc *softc;
2930 struct ccb_scsiio *csio;
2934 softc = (struct da_softc *)periph->softc;
2935 priority = done_ccb->ccb_h.pinfo.priority;
2937 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
2939 csio = &done_ccb->csio;
2940 state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
2942 case DA_CCB_BUFFER_IO:
2945 struct bio *bp, *bp1;
2947 cam_periph_lock(periph);
2948 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2949 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2953 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
2958 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
2959 if (error == ERESTART) {
2961 * A retry was scheduled, so
2964 cam_periph_unlock(periph);
2967 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2972 * return all queued I/O with EIO, so that
2973 * the client can retry these I/Os in the
2974 * proper order should it attempt to recover.
2979 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
2981 * Catastrophic error. Mark our pack as
2985 * XXX See if this is really a media
2988 xpt_print(periph->path,
2989 "Invalidating pack\n");
2990 softc->flags |= DA_FLAG_PACK_INVALID;
2991 queued_error = ENXIO;
2993 bioq_flush(&softc->bio_queue, NULL,
2996 bp->bio_error = error;
2997 bp->bio_resid = bp->bio_bcount;
2998 bp->bio_flags |= BIO_ERROR;
3000 } else if (bp != NULL) {
3001 if (state == DA_CCB_DELETE)
3004 bp->bio_resid = csio->resid;
3006 if (bp->bio_resid != 0)
3007 bp->bio_flags |= BIO_ERROR;
3009 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3010 cam_release_devq(done_ccb->ccb_h.path,
3014 /*getcount_only*/0);
3015 } else if (bp != NULL) {
3016 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3017 panic("REQ_CMP with QFRZN");
3018 if (state == DA_CCB_DELETE)
3021 bp->bio_resid = csio->resid;
3022 if (csio->resid > 0)
3023 bp->bio_flags |= BIO_ERROR;
3024 if (softc->error_inject != 0) {
3025 bp->bio_error = softc->error_inject;
3026 bp->bio_resid = bp->bio_bcount;
3027 bp->bio_flags |= BIO_ERROR;
3028 softc->error_inject = 0;
3032 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
3033 if (LIST_EMPTY(&softc->pending_ccbs))
3034 softc->flags |= DA_FLAG_WAS_OTAG;
3036 xpt_release_ccb(done_ccb);
3037 if (state == DA_CCB_DELETE) {
3038 TAILQ_HEAD(, bio) queue;
3041 TAILQ_CONCAT(&queue, &softc->delete_run_queue.queue, bio_queue);
3042 softc->delete_run_queue.insert_point = NULL;
3043 softc->delete_running = 0;
3045 cam_periph_unlock(periph);
3046 while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
3047 TAILQ_REMOVE(&queue, bp1, bio_queue);
3048 bp1->bio_error = bp->bio_error;
3049 if (bp->bio_flags & BIO_ERROR) {
3050 bp1->bio_flags |= BIO_ERROR;
3051 bp1->bio_resid = bp1->bio_bcount;
3057 cam_periph_unlock(periph);
3062 case DA_CCB_PROBE_RC:
3063 case DA_CCB_PROBE_RC16:
3065 struct scsi_read_capacity_data *rdcap;
3066 struct scsi_read_capacity_data_long *rcaplong;
3067 char announce_buf[80];
3073 if (state == DA_CCB_PROBE_RC)
3074 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
3076 rcaplong = (struct scsi_read_capacity_data_long *)
3079 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3080 struct disk_params *dp;
3081 uint32_t block_size;
3083 u_int lbppbe; /* LB per physical block exponent. */
3084 u_int lalba; /* Lowest aligned LBA. */
3086 if (state == DA_CCB_PROBE_RC) {
3087 block_size = scsi_4btoul(rdcap->length);
3088 maxsector = scsi_4btoul(rdcap->addr);
3093 * According to SBC-2, if the standard 10
3094 * byte READ CAPACITY command returns 2^32,
3095 * we should issue the 16 byte version of
3096 * the command, since the device in question
3097 * has more sectors than can be represented
3098 * with the short version of the command.
3100 if (maxsector == 0xffffffff) {
3101 free(rdcap, M_SCSIDA);
3102 xpt_release_ccb(done_ccb);
3103 softc->state = DA_STATE_PROBE_RC16;
3104 xpt_schedule(periph, priority);
3108 block_size = scsi_4btoul(rcaplong->length);
3109 maxsector = scsi_8btou64(rcaplong->addr);
3110 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
3111 lalba = scsi_2btoul(rcaplong->lalba_lbp);
3115 * Because GEOM code just will panic us if we
3116 * give them an 'illegal' value we'll avoid that
3119 if (block_size == 0) {
3124 if (block_size >= MAXPHYS) {
3125 xpt_print(periph->path,
3126 "unsupportable block size %ju\n",
3127 (uintmax_t) block_size);
3128 announce_buf[0] = '\0';
3129 cam_periph_invalidate(periph);
3132 * We pass rcaplong into dasetgeom(),
3133 * because it will only use it if it is
3136 dasetgeom(periph, block_size, maxsector,
3137 rcaplong, sizeof(*rcaplong));
3138 lbp = (lalba & SRC16_LBPME_A);
3139 dp = &softc->params;
3140 snprintf(announce_buf, sizeof(announce_buf),
3141 "%juMB (%ju %u byte sectors: %dH %dS/T "
3143 (((uintmax_t)dp->secsize *
3144 dp->sectors) / (1024*1024)),
3145 (uintmax_t)dp->sectors,
3146 dp->secsize, dp->heads,
3147 dp->secs_per_track, dp->cylinders);
3152 announce_buf[0] = '\0';
3155 * Retry any UNIT ATTENTION type errors. They
3156 * are expected at boot.
3158 error = daerror(done_ccb, CAM_RETRY_SELTO,
3159 SF_RETRY_UA|SF_NO_PRINT);
3160 if (error == ERESTART) {
3162 * A retry was scheuled, so
3166 } else if (error != 0) {
3168 int sense_key, error_code;
3171 struct ccb_getdev cgd;
3173 /* Don't wedge this device's queue */
3174 status = done_ccb->ccb_h.status;
3175 if ((status & CAM_DEV_QFRZN) != 0)
3176 cam_release_devq(done_ccb->ccb_h.path,
3180 /*getcount_only*/0);
3183 xpt_setup_ccb(&cgd.ccb_h,
3184 done_ccb->ccb_h.path,
3185 CAM_PRIORITY_NORMAL);
3186 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
3187 xpt_action((union ccb *)&cgd);
3189 if (scsi_extract_sense_ccb(done_ccb,
3190 &error_code, &sense_key, &asc, &ascq))
3196 * If we tried READ CAPACITY(16) and failed,
3197 * fallback to READ CAPACITY(10).
3199 if ((state == DA_CCB_PROBE_RC16) &&
3200 (softc->flags & DA_FLAG_CAN_RC16) &&
3201 (((csio->ccb_h.status & CAM_STATUS_MASK) ==
3204 (error_code == SSD_CURRENT_ERROR) &&
3205 (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
3206 softc->flags &= ~DA_FLAG_CAN_RC16;
3207 free(rdcap, M_SCSIDA);
3208 xpt_release_ccb(done_ccb);
3209 softc->state = DA_STATE_PROBE_RC;
3210 xpt_schedule(periph, priority);
3214 * Attach to anything that claims to be a
3215 * direct access or optical disk device,
3216 * as long as it doesn't return a "Logical
3217 * unit not supported" (0x25) error.
3219 if ((have_sense) && (asc != 0x25)
3220 && (error_code == SSD_CURRENT_ERROR)) {
3221 const char *sense_key_desc;
3222 const char *asc_desc;
3224 dasetgeom(periph, 512, -1, NULL, 0);
3225 scsi_sense_desc(sense_key, asc, ascq,
3229 snprintf(announce_buf,
3230 sizeof(announce_buf),
3231 "Attempt to query device "
3232 "size failed: %s, %s",
3240 xpt_print(periph->path,
3241 "got CAM status %#x\n",
3242 done_ccb->ccb_h.status);
3245 xpt_print(periph->path, "fatal error, "
3246 "failed to attach to device\n");
3249 * Free up resources.
3251 cam_periph_invalidate(periph);
3255 free(csio->data_ptr, M_SCSIDA);
3256 if (announce_buf[0] != '\0' &&
3257 ((softc->flags & DA_FLAG_ANNOUNCED) == 0)) {
3259 * Create our sysctl variables, now that we know
3260 * we have successfully attached.
3262 /* increase the refcount */
3263 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3264 taskqueue_enqueue(taskqueue_thread,
3265 &softc->sysctl_task);
3266 xpt_announce_periph(periph, announce_buf);
3267 xpt_announce_quirks(periph, softc->quirks,
3270 xpt_print(periph->path, "fatal error, "
3271 "could not acquire reference count\n");
3275 /* We already probed the device. */
3276 if (softc->flags & DA_FLAG_PROBED) {
3277 daprobedone(periph, done_ccb);
3281 /* Ensure re-probe doesn't see old delete. */
3282 softc->delete_available = 0;
3283 if (lbp && (softc->quirks & DA_Q_NO_UNMAP) == 0) {
3285 * Based on older SBC-3 spec revisions
3286 * any of the UNMAP methods "may" be
3287 * available via LBP given this flag so
3288 * we flag all of them as availble and
3289 * then remove those which further
3290 * probes confirm aren't available
3293 * We could also check readcap(16) p_type
3294 * flag to exclude one or more invalid
3295 * write same (X) types here
3297 dadeleteflag(softc, DA_DELETE_WS16, 1);
3298 dadeleteflag(softc, DA_DELETE_WS10, 1);
3299 dadeleteflag(softc, DA_DELETE_ZERO, 1);
3300 dadeleteflag(softc, DA_DELETE_UNMAP, 1);
3302 xpt_release_ccb(done_ccb);
3303 softc->state = DA_STATE_PROBE_LBP;
3304 xpt_schedule(periph, priority);
3308 xpt_release_ccb(done_ccb);
3309 softc->state = DA_STATE_PROBE_BDC;
3310 xpt_schedule(periph, priority);
3313 case DA_CCB_PROBE_LBP:
3315 struct scsi_vpd_logical_block_prov *lbp;
3317 lbp = (struct scsi_vpd_logical_block_prov *)csio->data_ptr;
3319 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3321 * T10/1799-D Revision 31 states at least one of these
3322 * must be supported but we don't currently enforce this.
3324 dadeleteflag(softc, DA_DELETE_WS16,
3325 (lbp->flags & SVPD_LBP_WS16));
3326 dadeleteflag(softc, DA_DELETE_WS10,
3327 (lbp->flags & SVPD_LBP_WS10));
3328 dadeleteflag(softc, DA_DELETE_ZERO,
3329 (lbp->flags & SVPD_LBP_WS10));
3330 dadeleteflag(softc, DA_DELETE_UNMAP,
3331 (lbp->flags & SVPD_LBP_UNMAP));
3334 error = daerror(done_ccb, CAM_RETRY_SELTO,
3335 SF_RETRY_UA|SF_NO_PRINT);
3336 if (error == ERESTART)
3338 else if (error != 0) {
3339 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3340 /* Don't wedge this device's queue */
3341 cam_release_devq(done_ccb->ccb_h.path,
3345 /*getcount_only*/0);
3349 * Failure indicates we don't support any SBC-3
3350 * delete methods with UNMAP
3355 free(lbp, M_SCSIDA);
3356 xpt_release_ccb(done_ccb);
3357 softc->state = DA_STATE_PROBE_BLK_LIMITS;
3358 xpt_schedule(periph, priority);
3361 case DA_CCB_PROBE_BLK_LIMITS:
3363 struct scsi_vpd_block_limits *block_limits;
3365 block_limits = (struct scsi_vpd_block_limits *)csio->data_ptr;
3367 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3368 uint32_t max_txfer_len = scsi_4btoul(
3369 block_limits->max_txfer_len);
3370 uint32_t max_unmap_lba_cnt = scsi_4btoul(
3371 block_limits->max_unmap_lba_cnt);
3372 uint32_t max_unmap_blk_cnt = scsi_4btoul(
3373 block_limits->max_unmap_blk_cnt);
3374 uint64_t ws_max_blks = scsi_8btou64(
3375 block_limits->max_write_same_length);
3377 if (max_txfer_len != 0) {
3378 softc->disk->d_maxsize = MIN(softc->maxio,
3379 (off_t)max_txfer_len * softc->params.secsize);
3383 * We should already support UNMAP but we check lba
3384 * and block count to be sure
3386 if (max_unmap_lba_cnt != 0x00L &&
3387 max_unmap_blk_cnt != 0x00L) {
3388 softc->unmap_max_lba = max_unmap_lba_cnt;
3389 softc->unmap_max_ranges = min(max_unmap_blk_cnt,
3393 * Unexpected UNMAP limits which means the
3394 * device doesn't actually support UNMAP
3396 dadeleteflag(softc, DA_DELETE_UNMAP, 0);
3399 if (ws_max_blks != 0x00L)
3400 softc->ws_max_blks = ws_max_blks;
3403 error = daerror(done_ccb, CAM_RETRY_SELTO,
3404 SF_RETRY_UA|SF_NO_PRINT);
3405 if (error == ERESTART)
3407 else if (error != 0) {
3408 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3409 /* Don't wedge this device's queue */
3410 cam_release_devq(done_ccb->ccb_h.path,
3414 /*getcount_only*/0);
3418 * Failure here doesn't mean UNMAP is not
3419 * supported as this is an optional page.
3421 softc->unmap_max_lba = 1;
3422 softc->unmap_max_ranges = 1;
3426 free(block_limits, M_SCSIDA);
3427 xpt_release_ccb(done_ccb);
3428 softc->state = DA_STATE_PROBE_BDC;
3429 xpt_schedule(periph, priority);
3432 case DA_CCB_PROBE_BDC:
3434 struct scsi_vpd_block_characteristics *bdc;
3436 bdc = (struct scsi_vpd_block_characteristics *)csio->data_ptr;
3438 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3440 * Disable queue sorting for non-rotational media
3443 u_int old_rate = softc->disk->d_rotation_rate;
3445 softc->disk->d_rotation_rate =
3446 scsi_2btoul(bdc->medium_rotation_rate);
3447 if (softc->disk->d_rotation_rate ==
3448 SVPD_BDC_RATE_NON_ROTATING) {
3449 softc->sort_io_queue = 0;
3451 if (softc->disk->d_rotation_rate != old_rate) {
3452 disk_attr_changed(softc->disk,
3453 "GEOM::rotation_rate", M_NOWAIT);
3457 error = daerror(done_ccb, CAM_RETRY_SELTO,
3458 SF_RETRY_UA|SF_NO_PRINT);
3459 if (error == ERESTART)
3461 else if (error != 0) {
3462 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3463 /* Don't wedge this device's queue */
3464 cam_release_devq(done_ccb->ccb_h.path,
3468 /*getcount_only*/0);
3473 free(bdc, M_SCSIDA);
3474 xpt_release_ccb(done_ccb);
3475 softc->state = DA_STATE_PROBE_ATA;
3476 xpt_schedule(periph, priority);
3479 case DA_CCB_PROBE_ATA:
3482 struct ata_params *ata_params;
3485 ata_params = (struct ata_params *)csio->data_ptr;
3486 ptr = (uint16_t *)ata_params;
3488 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3491 for (i = 0; i < sizeof(*ata_params) / 2; i++)
3492 ptr[i] = le16toh(ptr[i]);
3493 if (ata_params->support_dsm & ATA_SUPPORT_DSM_TRIM &&
3494 (softc->quirks & DA_Q_NO_UNMAP) == 0) {
3495 dadeleteflag(softc, DA_DELETE_ATA_TRIM, 1);
3496 if (ata_params->max_dsm_blocks != 0)
3497 softc->trim_max_ranges = min(
3498 softc->trim_max_ranges,
3499 ata_params->max_dsm_blocks *
3500 ATA_DSM_BLK_RANGES);
3503 * Disable queue sorting for non-rotational media
3506 old_rate = softc->disk->d_rotation_rate;
3507 softc->disk->d_rotation_rate =
3508 ata_params->media_rotation_rate;
3509 if (softc->disk->d_rotation_rate ==
3510 ATA_RATE_NON_ROTATING) {
3511 softc->sort_io_queue = 0;
3514 if (softc->disk->d_rotation_rate != old_rate) {
3515 disk_attr_changed(softc->disk,
3516 "GEOM::rotation_rate", M_NOWAIT);
3520 error = daerror(done_ccb, CAM_RETRY_SELTO,
3521 SF_RETRY_UA|SF_NO_PRINT);
3522 if (error == ERESTART)
3524 else if (error != 0) {
3525 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3526 /* Don't wedge this device's queue */
3527 cam_release_devq(done_ccb->ccb_h.path,
3531 /*getcount_only*/0);
3536 free(ata_params, M_SCSIDA);
3537 daprobedone(periph, done_ccb);
3541 /* No-op. We're polling */
3545 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3547 if (daerror(done_ccb, CAM_RETRY_SELTO,
3548 SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
3551 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3552 cam_release_devq(done_ccb->ccb_h.path,
3556 /*getcount_only*/0);
3558 xpt_release_ccb(done_ccb);
3559 cam_periph_release_locked(periph);
3565 xpt_release_ccb(done_ccb);
3569 dareprobe(struct cam_periph *periph)
3571 struct da_softc *softc;
3574 softc = (struct da_softc *)periph->softc;
3576 /* Probe in progress; don't interfere. */
3577 if (softc->state != DA_STATE_NORMAL)
3580 status = cam_periph_acquire(periph);
3581 KASSERT(status == CAM_REQ_CMP,
3582 ("dareprobe: cam_periph_acquire failed"));
3584 if (softc->flags & DA_FLAG_CAN_RC16)
3585 softc->state = DA_STATE_PROBE_RC16;
3587 softc->state = DA_STATE_PROBE_RC;
3589 xpt_schedule(periph, CAM_PRIORITY_DEV);
3593 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3595 struct da_softc *softc;
3596 struct cam_periph *periph;
3597 int error, error_code, sense_key, asc, ascq;
3599 periph = xpt_path_periph(ccb->ccb_h.path);
3600 softc = (struct da_softc *)periph->softc;
3603 * Automatically detect devices that do not support
3604 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
3607 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3608 error = cmd6workaround(ccb);
3609 } else if (scsi_extract_sense_ccb(ccb,
3610 &error_code, &sense_key, &asc, &ascq)) {
3611 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3612 error = cmd6workaround(ccb);
3614 * If the target replied with CAPACITY DATA HAS CHANGED UA,
3615 * query the capacity and notify upper layers.
3617 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3618 asc == 0x2A && ascq == 0x09) {
3619 xpt_print(periph->path, "Capacity data has changed\n");
3620 softc->flags &= ~DA_FLAG_PROBED;
3622 sense_flags |= SF_NO_PRINT;
3623 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3624 asc == 0x28 && ascq == 0x00) {
3625 softc->flags &= ~DA_FLAG_PROBED;
3626 disk_media_changed(softc->disk, M_NOWAIT);
3627 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3628 asc == 0x3F && ascq == 0x03) {
3629 xpt_print(periph->path, "INQUIRY data has changed\n");
3630 softc->flags &= ~DA_FLAG_PROBED;
3632 sense_flags |= SF_NO_PRINT;
3633 } else if (sense_key == SSD_KEY_NOT_READY &&
3634 asc == 0x3a && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
3635 softc->flags |= DA_FLAG_PACK_INVALID;
3636 disk_media_gone(softc->disk, M_NOWAIT);
3639 if (error == ERESTART)
3644 * Until we have a better way of doing pack validation,
3645 * don't treat UAs as errors.
3647 sense_flags |= SF_RETRY_UA;
3649 if (softc->quirks & DA_Q_RETRY_BUSY)
3650 sense_flags |= SF_RETRY_BUSY;
3651 return(cam_periph_error(ccb, cam_flags, sense_flags,
3652 &softc->saved_ccb));
3656 damediapoll(void *arg)
3658 struct cam_periph *periph = arg;
3659 struct da_softc *softc = periph->softc;
3661 if (!softc->tur && LIST_EMPTY(&softc->pending_ccbs)) {
3662 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3667 /* Queue us up again */
3668 if (da_poll_period != 0)
3669 callout_schedule(&softc->mediapoll_c, da_poll_period * hz);
3673 daprevent(struct cam_periph *periph, int action)
3675 struct da_softc *softc;
3679 softc = (struct da_softc *)periph->softc;
3681 if (((action == PR_ALLOW)
3682 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
3683 || ((action == PR_PREVENT)
3684 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
3688 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3690 scsi_prevent(&ccb->csio,
3698 error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
3699 SF_RETRY_UA | SF_NO_PRINT, softc->disk->d_devstat);
3702 if (action == PR_ALLOW)
3703 softc->flags &= ~DA_FLAG_PACK_LOCKED;
3705 softc->flags |= DA_FLAG_PACK_LOCKED;
3708 xpt_release_ccb(ccb);
3712 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
3713 struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
3715 struct ccb_calc_geometry ccg;
3716 struct da_softc *softc;
3717 struct disk_params *dp;
3718 u_int lbppbe, lalba;
3721 softc = (struct da_softc *)periph->softc;
3723 dp = &softc->params;
3724 dp->secsize = block_len;
3725 dp->sectors = maxsector + 1;
3726 if (rcaplong != NULL) {
3727 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
3728 lalba = scsi_2btoul(rcaplong->lalba_lbp);
3729 lalba &= SRC16_LALBA_A;
3736 dp->stripesize = block_len << lbppbe;
3737 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
3739 } else if (softc->quirks & DA_Q_4K) {
3740 dp->stripesize = 4096;
3741 dp->stripeoffset = 0;
3744 dp->stripeoffset = 0;
3747 * Have the controller provide us with a geometry
3748 * for this disk. The only time the geometry
3749 * matters is when we boot and the controller
3750 * is the only one knowledgeable enough to come
3751 * up with something that will make this a bootable
3754 xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3755 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
3756 ccg.block_size = dp->secsize;
3757 ccg.volume_size = dp->sectors;
3759 ccg.secs_per_track = 0;
3761 xpt_action((union ccb*)&ccg);
3762 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3764 * We don't know what went wrong here- but just pick
3765 * a geometry so we don't have nasty things like divide
3769 dp->secs_per_track = 255;
3770 dp->cylinders = dp->sectors / (255 * 255);
3771 if (dp->cylinders == 0) {
3775 dp->heads = ccg.heads;
3776 dp->secs_per_track = ccg.secs_per_track;
3777 dp->cylinders = ccg.cylinders;
3781 * If the user supplied a read capacity buffer, and if it is
3782 * different than the previous buffer, update the data in the EDT.
3783 * If it's the same, we don't bother. This avoids sending an
3784 * update every time someone opens this device.
3786 if ((rcaplong != NULL)
3787 && (bcmp(rcaplong, &softc->rcaplong,
3788 min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
3789 struct ccb_dev_advinfo cdai;
3791 xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3792 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
3793 cdai.buftype = CDAI_TYPE_RCAPLONG;
3794 cdai.flags = CDAI_FLAG_STORE;
3795 cdai.bufsiz = rcap_len;
3796 cdai.buf = (uint8_t *)rcaplong;
3797 xpt_action((union ccb *)&cdai);
3798 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
3799 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
3800 if (cdai.ccb_h.status != CAM_REQ_CMP) {
3801 xpt_print(periph->path, "%s: failed to set read "
3802 "capacity advinfo\n", __func__);
3803 /* Use cam_error_print() to decode the status */
3804 cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
3807 bcopy(rcaplong, &softc->rcaplong,
3808 min(sizeof(softc->rcaplong), rcap_len));
3812 softc->disk->d_sectorsize = softc->params.secsize;
3813 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
3814 softc->disk->d_stripesize = softc->params.stripesize;
3815 softc->disk->d_stripeoffset = softc->params.stripeoffset;
3816 /* XXX: these are not actually "firmware" values, so they may be wrong */
3817 softc->disk->d_fwsectors = softc->params.secs_per_track;
3818 softc->disk->d_fwheads = softc->params.heads;
3819 softc->disk->d_devstat->block_size = softc->params.secsize;
3820 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
3822 error = disk_resize(softc->disk, M_NOWAIT);
3824 xpt_print(periph->path, "disk_resize(9) failed, error = %d\n", error);
3828 dasendorderedtag(void *arg)
3830 struct da_softc *softc = arg;
3832 if (da_send_ordered) {
3833 if (!LIST_EMPTY(&softc->pending_ccbs)) {
3834 if ((softc->flags & DA_FLAG_WAS_OTAG) == 0)
3835 softc->flags |= DA_FLAG_NEED_OTAG;
3836 softc->flags &= ~DA_FLAG_WAS_OTAG;
3839 /* Queue us up again */
3840 callout_reset(&softc->sendordered_c,
3841 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
3842 dasendorderedtag, softc);
3846 * Step through all DA peripheral drivers, and if the device is still open,
3847 * sync the disk cache to physical media.
3850 dashutdown(void * arg, int howto)
3852 struct cam_periph *periph;
3853 struct da_softc *softc;
3857 CAM_PERIPH_FOREACH(periph, &dadriver) {
3858 softc = (struct da_softc *)periph->softc;
3859 if (SCHEDULER_STOPPED()) {
3860 /* If we paniced with the lock held, do not recurse. */
3861 if (!cam_periph_owned(periph) &&
3862 (softc->flags & DA_FLAG_OPEN)) {
3863 dadump(softc->disk, NULL, 0, 0, 0);
3867 cam_periph_lock(periph);
3870 * We only sync the cache if the drive is still open, and
3871 * if the drive is capable of it..
3873 if (((softc->flags & DA_FLAG_OPEN) == 0)
3874 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
3875 cam_periph_unlock(periph);
3879 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3880 scsi_synchronize_cache(&ccb->csio,
3884 /*begin_lba*/0, /* whole disk */
3889 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
3890 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR,
3891 softc->disk->d_devstat);
3893 xpt_print(periph->path, "Synchronize cache failed\n");
3894 xpt_release_ccb(ccb);
3895 cam_periph_unlock(periph);
3899 #else /* !_KERNEL */
3902 * XXX These are only left out of the kernel build to silence warnings. If,
3903 * for some reason these functions are used in the kernel, the ifdefs should
3904 * be moved so they are included both in the kernel and userland.
3907 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
3908 void (*cbfcnp)(struct cam_periph *, union ccb *),
3909 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
3910 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
3913 struct scsi_format_unit *scsi_cmd;
3915 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
3916 scsi_cmd->opcode = FORMAT_UNIT;
3917 scsi_cmd->byte2 = byte2;
3918 scsi_ulto2b(ileave, scsi_cmd->interleave);
3923 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
3933 scsi_read_defects(struct ccb_scsiio *csio, uint32_t retries,
3934 void (*cbfcnp)(struct cam_periph *, union ccb *),
3935 uint8_t tag_action, uint8_t list_format,
3936 uint32_t addr_desc_index, uint8_t *data_ptr,
3937 uint32_t dxfer_len, int minimum_cmd_size,
3938 uint8_t sense_len, uint32_t timeout)
3943 * These conditions allow using the 10 byte command. Otherwise we
3944 * need to use the 12 byte command.
3946 if ((minimum_cmd_size <= 10)
3947 && (addr_desc_index == 0)
3948 && (dxfer_len <= SRDD10_MAX_LENGTH)) {
3949 struct scsi_read_defect_data_10 *cdb10;
3951 cdb10 = (struct scsi_read_defect_data_10 *)
3952 &csio->cdb_io.cdb_bytes;
3954 cdb_len = sizeof(*cdb10);
3955 bzero(cdb10, cdb_len);
3956 cdb10->opcode = READ_DEFECT_DATA_10;
3957 cdb10->format = list_format;
3958 scsi_ulto2b(dxfer_len, cdb10->alloc_length);
3960 struct scsi_read_defect_data_12 *cdb12;
3962 cdb12 = (struct scsi_read_defect_data_12 *)
3963 &csio->cdb_io.cdb_bytes;
3965 cdb_len = sizeof(*cdb12);
3966 bzero(cdb12, cdb_len);
3967 cdb12->opcode = READ_DEFECT_DATA_12;
3968 cdb12->format = list_format;
3969 scsi_ulto4b(dxfer_len, cdb12->alloc_length);
3970 scsi_ulto4b(addr_desc_index, cdb12->address_descriptor_index);
3976 /*flags*/ CAM_DIR_IN,
3986 scsi_sanitize(struct ccb_scsiio *csio, u_int32_t retries,
3987 void (*cbfcnp)(struct cam_periph *, union ccb *),
3988 u_int8_t tag_action, u_int8_t byte2, u_int16_t control,
3989 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
3992 struct scsi_sanitize *scsi_cmd;
3994 scsi_cmd = (struct scsi_sanitize *)&csio->cdb_io.cdb_bytes;
3995 scsi_cmd->opcode = SANITIZE;
3996 scsi_cmd->byte2 = byte2;
3997 scsi_cmd->control = control;
3998 scsi_ulto2b(dxfer_len, scsi_cmd->length);
4003 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
4012 #endif /* _KERNEL */