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_SAW_MEDIA = 0x010,
87 DA_FLAG_NEED_OTAG = 0x020,
88 DA_FLAG_WENT_IDLE = 0x040,
89 DA_FLAG_RETRY_UA = 0x080,
91 DA_FLAG_SCTX_INIT = 0x200,
92 DA_FLAG_CAN_RC16 = 0x400,
93 DA_FLAG_PROBED = 0x800
98 DA_Q_NO_SYNC_CACHE = 0x01,
99 DA_Q_NO_6_BYTE = 0x02,
100 DA_Q_NO_PREVENT = 0x04,
104 #define DA_Q_BIT_STRING \
106 "\001NO_SYNC_CACHE" \
112 DA_CCB_PROBE_RC = 0x01,
113 DA_CCB_PROBE_RC16 = 0x02,
114 DA_CCB_PROBE_LBP = 0x03,
115 DA_CCB_PROBE_BLK_LIMITS = 0x04,
116 DA_CCB_PROBE_BDC = 0x05,
117 DA_CCB_PROBE_ATA = 0x06,
118 DA_CCB_BUFFER_IO = 0x07,
119 DA_CCB_WAITING = 0x08,
121 DA_CCB_DELETE = 0x0B,
123 DA_CCB_TYPE_MASK = 0x0F,
124 DA_CCB_RETRY_UA = 0x10
128 * Order here is important for method choice
130 * We prefer ATA_TRIM as tests run against a Sandforce 2281 SSD attached to
131 * LSI 2008 (mps) controller (FW: v12, Drv: v14) resulted 20% quicker deletes
132 * using ATA_TRIM than the corresponding UNMAP results for a real world mysql
133 * import taking 5mins.
144 DA_DELETE_MIN = DA_DELETE_ATA_TRIM,
145 DA_DELETE_MAX = DA_DELETE_ZERO
148 static const char *da_delete_method_names[] =
149 { "NONE", "DISABLE", "ATA_TRIM", "UNMAP", "WS16", "WS10", "ZERO" };
150 static const char *da_delete_method_desc[] =
151 { "NONE", "DISABLED", "ATA TRIM", "UNMAP", "WRITE SAME(16) with UNMAP",
152 "WRITE SAME(10) with UNMAP", "ZERO" };
154 /* Offsets into our private area for storing information */
155 #define ccb_state ppriv_field0
156 #define ccb_bp ppriv_ptr1
161 u_int8_t secs_per_track;
162 u_int32_t secsize; /* Number of bytes/sector */
163 u_int64_t sectors; /* total number sectors */
168 #define UNMAP_RANGE_MAX 0xffffffff
169 #define UNMAP_HEAD_SIZE 8
170 #define UNMAP_RANGE_SIZE 16
171 #define UNMAP_MAX_RANGES 2048 /* Protocol Max is 4095 */
172 #define UNMAP_BUF_SIZE ((UNMAP_MAX_RANGES * UNMAP_RANGE_SIZE) + \
175 #define WS10_MAX_BLKS 0xffff
176 #define WS16_MAX_BLKS 0xffffffff
177 #define ATA_TRIM_MAX_RANGES ((UNMAP_BUF_SIZE / \
178 (ATA_DSM_RANGE_SIZE * ATA_DSM_BLK_SIZE)) * ATA_DSM_BLK_SIZE)
181 struct bio_queue_head bio_queue;
182 struct bio_queue_head delete_queue;
183 struct bio_queue_head delete_run_queue;
184 SLIST_ENTRY(da_softc) links;
185 LIST_HEAD(, ccb_hdr) pending_ccbs;
190 int minimum_cmd_size;
192 int ordered_tag_count;
193 int outstanding_cmds;
197 int delete_available; /* Delete methods possibly available */
198 uint32_t unmap_max_ranges;
199 uint32_t unmap_max_lba;
200 uint64_t ws_max_blks;
201 da_delete_methods delete_method;
202 struct disk_params params;
205 struct task sysctl_task;
206 struct sysctl_ctx_list sysctl_ctx;
207 struct sysctl_oid *sysctl_tree;
208 struct callout sendordered_c;
210 uint8_t unmap_buf[UNMAP_BUF_SIZE];
211 struct scsi_read_capacity_data_long rcaplong;
212 struct callout mediapoll_c;
215 #define dadeleteflag(softc, delete_method, enable) \
217 softc->delete_available |= (1 << delete_method); \
219 softc->delete_available &= ~(1 << delete_method); \
222 struct da_quirk_entry {
223 struct scsi_inquiry_pattern inq_pat;
227 static const char quantum[] = "QUANTUM";
228 static const char microp[] = "MICROP";
230 static struct da_quirk_entry da_quirk_table[] =
232 /* SPI, FC devices */
235 * Fujitsu M2513A MO drives.
236 * Tested devices: M2513A2 firmware versions 1200 & 1300.
237 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
238 * Reported by: W.Scholten <whs@xs4all.nl>
240 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
241 /*quirks*/ DA_Q_NO_SYNC_CACHE
245 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
246 /*quirks*/ DA_Q_NO_SYNC_CACHE
250 * This particular Fujitsu drive doesn't like the
251 * synchronize cache command.
252 * Reported by: Tom Jackson <toj@gorilla.net>
254 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
255 /*quirks*/ DA_Q_NO_SYNC_CACHE
259 * This drive doesn't like the synchronize cache command
260 * either. Reported by: Matthew Jacob <mjacob@feral.com>
261 * in NetBSD PR kern/6027, August 24, 1998.
263 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
264 /*quirks*/ DA_Q_NO_SYNC_CACHE
268 * This drive doesn't like the synchronize cache command
269 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
272 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
273 /*quirks*/ DA_Q_NO_SYNC_CACHE
277 * Doesn't like the synchronize cache command.
278 * Reported by: Blaz Zupan <blaz@gold.amis.net>
280 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
281 /*quirks*/ DA_Q_NO_SYNC_CACHE
285 * Doesn't like the synchronize cache command.
286 * Reported by: Blaz Zupan <blaz@gold.amis.net>
288 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
289 /*quirks*/ DA_Q_NO_SYNC_CACHE
293 * Doesn't like the synchronize cache command.
295 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
296 /*quirks*/ DA_Q_NO_SYNC_CACHE
300 * Doesn't like the synchronize cache command.
301 * Reported by: walter@pelissero.de
303 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
304 /*quirks*/ DA_Q_NO_SYNC_CACHE
308 * Doesn't work correctly with 6 byte reads/writes.
309 * Returns illegal request, and points to byte 9 of the
311 * Reported by: Adam McDougall <bsdx@spawnet.com>
313 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
314 /*quirks*/ DA_Q_NO_6_BYTE
318 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
319 /*quirks*/ DA_Q_NO_6_BYTE
323 * Doesn't like the synchronize cache command.
324 * Reported by: walter@pelissero.de
326 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
327 /*quirks*/ DA_Q_NO_SYNC_CACHE
331 * The CISS RAID controllers do not support SYNC_CACHE
333 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
334 /*quirks*/ DA_Q_NO_SYNC_CACHE
336 /* USB mass storage devices supported by umass(4) */
339 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
342 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
343 /*quirks*/ DA_Q_NO_SYNC_CACHE
347 * Power Quotient Int. (PQI) USB flash key
350 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
351 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
355 * Creative Nomad MUVO mp3 player (USB)
358 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
359 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
363 * Jungsoft NEXDISK USB flash key
366 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
367 /*quirks*/ DA_Q_NO_SYNC_CACHE
371 * FreeDik USB Mini Data Drive
374 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
375 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
379 * Sigmatel USB Flash MP3 Player
382 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
383 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
387 * Neuros USB Digital Audio Computer
390 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
391 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
395 * SEAGRAND NP-900 MP3 Player
398 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
399 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
403 * iRiver iFP MP3 player (with UMS Firmware)
404 * PR: kern/54881, i386/63941, kern/66124
406 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
407 /*quirks*/ DA_Q_NO_SYNC_CACHE
411 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
414 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
415 /*quirks*/ DA_Q_NO_SYNC_CACHE
419 * ZICPlay USB MP3 Player with FM
422 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
423 /*quirks*/ DA_Q_NO_SYNC_CACHE
427 * TEAC USB floppy mechanisms
429 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
430 /*quirks*/ DA_Q_NO_SYNC_CACHE
434 * Kingston DataTraveler II+ USB Pen-Drive.
435 * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
437 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
438 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
446 {T_DIRECT, SIP_MEDIA_REMOVABLE, " ", "USB DISK Pro", "PMAP"},
447 /*quirks*/ DA_Q_NO_SYNC_CACHE
451 * Motorola E398 Mobile Phone (TransFlash memory card).
452 * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
455 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
456 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
460 * Qware BeatZkey! Pro
463 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
464 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
468 * Time DPA20B 1GB MP3 Player
471 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
472 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
476 * Samsung USB key 128Mb
479 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
480 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
484 * Kingston DataTraveler 2.0 USB Flash memory.
487 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
488 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
492 * Creative MUVO Slim mp3 player (USB)
495 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
496 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
500 * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
503 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
504 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
508 * SanDisk Micro Cruzer 128MB
511 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
512 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
516 * TOSHIBA TransMemory USB sticks
519 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
520 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
525 * PR: usb/75578, usb/72344, usb/65436
527 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
528 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
532 * Genesys 6-in-1 Card Reader
535 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
536 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
540 * Rekam Digital CAMERA
543 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
544 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
548 * iRiver H10 MP3 player
551 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
552 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
556 * iRiver U10 MP3 player
559 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
560 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
567 {T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
568 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
572 * EasyMP3 EM732X USB 2.0 Flash MP3 Player
575 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
576 "1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
583 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
584 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
588 * Philips USB Key Audio KEY013
591 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
592 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
599 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
600 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
607 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
608 /*quirks*/ DA_Q_NO_SYNC_CACHE
612 * I/O Magic USB flash - Giga Bank
615 {T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
616 /*quirks*/ DA_Q_NO_SYNC_CACHE
620 * JoyFly 128mb USB Flash Drive
623 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
624 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
631 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
632 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
636 * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A
639 {T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*",
640 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
644 * Samsung YP-U3 mp3-player
647 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3",
648 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
651 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
652 "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
656 * Sony Cyber-Shot DSC cameras
659 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
660 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
663 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler G3",
664 "1.00"}, /*quirks*/ DA_Q_NO_PREVENT
666 /* ATA/SATA devices over SAS/USB/... */
668 /* Hitachi Advanced Format (4k) drives */
669 { T_DIRECT, SIP_MEDIA_FIXED, "Hitachi", "H??????????E3*", "*" },
673 /* Samsung Advanced Format (4k) drives */
674 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD155UI*", "*" },
678 /* Samsung Advanced Format (4k) drives */
679 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD155UI*", "*" },
683 /* Samsung Advanced Format (4k) drives */
684 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD204UI*", "*" },
688 /* Samsung Advanced Format (4k) drives */
689 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD204UI*", "*" },
693 /* Seagate Barracuda Green Advanced Format (4k) drives */
694 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DL*", "*" },
698 /* Seagate Barracuda Green Advanced Format (4k) drives */
699 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DL", "*", "*" },
703 /* Seagate Barracuda Green Advanced Format (4k) drives */
704 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???DM*", "*" },
708 /* Seagate Barracuda Green Advanced Format (4k) drives */
709 { T_DIRECT, SIP_MEDIA_FIXED, "ST???DM*", "*", "*" },
713 /* Seagate Barracuda Green Advanced Format (4k) drives */
714 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DM*", "*" },
718 /* Seagate Barracuda Green Advanced Format (4k) drives */
719 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DM", "*", "*" },
723 /* Seagate Momentus Advanced Format (4k) drives */
724 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500423AS*", "*" },
728 /* Seagate Momentus Advanced Format (4k) drives */
729 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "3AS*", "*" },
733 /* Seagate Momentus Advanced Format (4k) drives */
734 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500424AS*", "*" },
738 /* Seagate Momentus Advanced Format (4k) drives */
739 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "4AS*", "*" },
743 /* Seagate Momentus Advanced Format (4k) drives */
744 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640423AS*", "*" },
748 /* Seagate Momentus Advanced Format (4k) drives */
749 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "3AS*", "*" },
753 /* Seagate Momentus Advanced Format (4k) drives */
754 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640424AS*", "*" },
758 /* Seagate Momentus Advanced Format (4k) drives */
759 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "4AS*", "*" },
763 /* Seagate Momentus Advanced Format (4k) drives */
764 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750420AS*", "*" },
768 /* Seagate Momentus Advanced Format (4k) drives */
769 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "0AS*", "*" },
773 /* Seagate Momentus Advanced Format (4k) drives */
774 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750422AS*", "*" },
778 /* Seagate Momentus Advanced Format (4k) drives */
779 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "2AS*", "*" },
783 /* Seagate Momentus Advanced Format (4k) drives */
784 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750423AS*", "*" },
788 /* Seagate Momentus Advanced Format (4k) drives */
789 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "3AS*", "*" },
793 /* Seagate Momentus Thin Advanced Format (4k) drives */
794 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???LT*", "*" },
798 /* Seagate Momentus Thin Advanced Format (4k) drives */
799 { T_DIRECT, SIP_MEDIA_FIXED, "ST???LT*", "*", "*" },
803 /* WDC Caviar Green Advanced Format (4k) drives */
804 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RS*", "*" },
808 /* WDC Caviar Green Advanced Format (4k) drives */
809 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RS*", "*" },
813 /* WDC Caviar Green Advanced Format (4k) drives */
814 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RX*", "*" },
818 /* WDC Caviar Green Advanced Format (4k) drives */
819 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RX*", "*" },
823 /* WDC Caviar Green Advanced Format (4k) drives */
824 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RS*", "*" },
828 /* WDC Caviar Green Advanced Format (4k) drives */
829 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RS*", "*" },
833 /* WDC Caviar Green Advanced Format (4k) drives */
834 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RX*", "*" },
838 /* WDC Caviar Green Advanced Format (4k) drives */
839 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RX*", "*" },
843 /* WDC Scorpio Black Advanced Format (4k) drives */
844 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PKT*", "*" },
848 /* WDC Scorpio Black Advanced Format (4k) drives */
849 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PKT*", "*" },
853 /* WDC Scorpio Black Advanced Format (4k) drives */
854 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PKT*", "*" },
858 /* WDC Scorpio Black Advanced Format (4k) drives */
859 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PKT*", "*" },
863 /* WDC Scorpio Blue Advanced Format (4k) drives */
864 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PVT*", "*" },
868 /* WDC Scorpio Blue Advanced Format (4k) drives */
869 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PVT*", "*" },
873 /* WDC Scorpio Blue Advanced Format (4k) drives */
874 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PVT*", "*" },
878 /* WDC Scorpio Blue Advanced Format (4k) drives */
879 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PVT*", "*" },
884 * Olympus FE-210 camera
886 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "FE210*",
887 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
893 {T_DIRECT, SIP_MEDIA_REMOVABLE, "LG", "UP3S",
894 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
898 * Laser MP3-2GA13 MP3 player
900 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "(HS) Flash Disk",
901 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
905 * LaCie external 250GB Hard drive des by Porsche
906 * Submitted by: Ben Stuyts <ben@altesco.nl>
909 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HM250JI", "*"},
910 /*quirks*/ DA_Q_NO_SYNC_CACHE
915 * Corsair Force 2 SSDs
916 * 4k optimised & trim only works in 4k requests + 4k aligned
918 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair CSSD-F*", "*" },
923 * Corsair Force 3 SSDs
924 * 4k optimised & trim only works in 4k requests + 4k aligned
926 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force 3*", "*" },
931 * Corsair Force GT SSDs
932 * 4k optimised & trim only works in 4k requests + 4k aligned
934 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force GT*", "*" },
940 * 4k optimised & trim only works in 4k requests + 4k aligned
942 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "M4-CT???M4SSD2*", "*" },
947 * Crucial RealSSD C300 SSDs
950 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "C300-CTFDDAC???MAG*",
951 "*" }, /*quirks*/DA_Q_4K
955 * Intel 320 Series SSDs
956 * 4k optimised & trim only works in 4k requests + 4k aligned
958 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2CW*", "*" },
963 * Intel 330 Series SSDs
964 * 4k optimised & trim only works in 4k requests + 4k aligned
966 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2CT*", "*" },
971 * Intel 510 Series SSDs
972 * 4k optimised & trim only works in 4k requests + 4k aligned
974 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2MH*", "*" },
979 * Intel 520 Series SSDs
980 * 4k optimised & trim only works in 4k requests + 4k aligned
982 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2BW*", "*" },
987 * Kingston E100 Series SSDs
988 * 4k optimised & trim only works in 4k requests + 4k aligned
990 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SE100S3*", "*" },
995 * Kingston HyperX 3k SSDs
996 * 4k optimised & trim only works in 4k requests + 4k aligned
998 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SH103S3*", "*" },
1003 * OCZ Agility 3 SSDs
1004 * 4k optimised & trim only works in 4k requests + 4k aligned
1006 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-AGILITY3*", "*" },
1011 * OCZ Deneva R Series SSDs
1012 * 4k optimised & trim only works in 4k requests + 4k aligned
1014 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "DENRSTE251M45*", "*" },
1019 * OCZ Vertex 2 SSDs (inc pro series)
1020 * 4k optimised & trim only works in 4k requests + 4k aligned
1022 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ?VERTEX2*", "*" },
1028 * 4k optimised & trim only works in 4k requests + 4k aligned
1030 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX3*", "*" },
1036 * 4k optimised & trim only works in 4k requests + 4k aligned
1038 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX4*", "*" },
1043 * Samsung 830 Series SSDs
1044 * 4k optimised & trim only works in 4k requests + 4k aligned
1046 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG SSD 830 Series*", "*" },
1051 * SuperTalent TeraDrive CT SSDs
1052 * 4k optimised & trim only works in 4k requests + 4k aligned
1054 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "FTM??CT25H*", "*" },
1059 * XceedIOPS SATA SSDs
1062 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SG9XCS2D*", "*" },
1067 static disk_strategy_t dastrategy;
1068 static dumper_t dadump;
1069 static periph_init_t dainit;
1070 static void daasync(void *callback_arg, u_int32_t code,
1071 struct cam_path *path, void *arg);
1072 static void dasysctlinit(void *context, int pending);
1073 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
1074 static int dadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
1075 static int dadeletemaxsysctl(SYSCTL_HANDLER_ARGS);
1076 static void dadeletemethodset(struct da_softc *softc,
1077 da_delete_methods delete_method);
1078 static off_t dadeletemaxsize(struct da_softc *softc,
1079 da_delete_methods delete_method);
1080 static void dadeletemethodchoose(struct da_softc *softc,
1081 da_delete_methods default_method);
1082 static void daprobedone(struct cam_periph *periph, union ccb *ccb);
1084 static periph_ctor_t daregister;
1085 static periph_dtor_t dacleanup;
1086 static periph_start_t dastart;
1087 static periph_oninv_t daoninvalidate;
1088 static void dadone(struct cam_periph *periph,
1089 union ccb *done_ccb);
1090 static int daerror(union ccb *ccb, u_int32_t cam_flags,
1091 u_int32_t sense_flags);
1092 static void daprevent(struct cam_periph *periph, int action);
1093 static void dareprobe(struct cam_periph *periph);
1094 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
1096 struct scsi_read_capacity_data_long *rcaplong,
1098 static timeout_t dasendorderedtag;
1099 static void dashutdown(void *arg, int howto);
1100 static timeout_t damediapoll;
1102 #ifndef DA_DEFAULT_POLL_PERIOD
1103 #define DA_DEFAULT_POLL_PERIOD 3
1106 #ifndef DA_DEFAULT_TIMEOUT
1107 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
1110 #ifndef DA_DEFAULT_RETRY
1111 #define DA_DEFAULT_RETRY 4
1114 #ifndef DA_DEFAULT_SEND_ORDERED
1115 #define DA_DEFAULT_SEND_ORDERED 1
1118 #define DA_SIO (softc->sort_io_queue >= 0 ? \
1119 softc->sort_io_queue : cam_sort_io_queues)
1121 static int da_poll_period = DA_DEFAULT_POLL_PERIOD;
1122 static int da_retry_count = DA_DEFAULT_RETRY;
1123 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
1124 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
1126 static SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
1127 "CAM Direct Access Disk driver");
1128 SYSCTL_INT(_kern_cam_da, OID_AUTO, poll_period, CTLFLAG_RW,
1129 &da_poll_period, 0, "Media polling period in seconds");
1130 TUNABLE_INT("kern.cam.da.poll_period", &da_poll_period);
1131 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
1132 &da_retry_count, 0, "Normal I/O retry count");
1133 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
1134 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
1135 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
1136 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
1137 SYSCTL_INT(_kern_cam_da, OID_AUTO, send_ordered, CTLFLAG_RW,
1138 &da_send_ordered, 0, "Send Ordered Tags");
1139 TUNABLE_INT("kern.cam.da.send_ordered", &da_send_ordered);
1142 * DA_ORDEREDTAG_INTERVAL determines how often, relative
1143 * to the default timeout, we check to see whether an ordered
1144 * tagged transaction is appropriate to prevent simple tag
1145 * starvation. Since we'd like to ensure that there is at least
1146 * 1/2 of the timeout length left for a starved transaction to
1147 * complete after we've sent an ordered tag, we must poll at least
1148 * four times in every timeout period. This takes care of the worst
1149 * case where a starved transaction starts during an interval that
1150 * meets the requirement "don't send an ordered tag" test so it takes
1151 * us two intervals to determine that a tag must be sent.
1153 #ifndef DA_ORDEREDTAG_INTERVAL
1154 #define DA_ORDEREDTAG_INTERVAL 4
1157 static struct periph_driver dadriver =
1160 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
1163 PERIPHDRIVER_DECLARE(da, dadriver);
1165 static MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
1168 daopen(struct disk *dp)
1170 struct cam_periph *periph;
1171 struct da_softc *softc;
1175 periph = (struct cam_periph *)dp->d_drv1;
1176 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1180 cam_periph_lock(periph);
1181 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
1182 cam_periph_unlock(periph);
1183 cam_periph_release(periph);
1187 unit = periph->unit_number;
1188 softc = (struct da_softc *)periph->softc;
1189 softc->flags |= DA_FLAG_OPEN;
1191 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1194 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1195 /* Invalidate our pack information. */
1196 softc->flags &= ~DA_FLAG_PACK_INVALID;
1201 /* Wait for the disk size update. */
1202 error = cam_periph_sleep(periph, &softc->disk->d_mediasize, PRIBIO,
1205 xpt_print(periph->path, "unable to retrieve capacity data");
1207 if (periph->flags & CAM_PERIPH_INVALID ||
1208 softc->disk->d_sectorsize == 0 ||
1209 softc->disk->d_mediasize == 0)
1212 if (error == 0 && (softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1213 (softc->quirks & DA_Q_NO_PREVENT) == 0)
1214 daprevent(periph, PR_PREVENT);
1217 softc->flags |= DA_FLAG_SAW_MEDIA;
1219 cam_periph_unhold(periph);
1220 cam_periph_unlock(periph);
1223 softc->flags &= ~DA_FLAG_OPEN;
1224 cam_periph_release(periph);
1231 daclose(struct disk *dp)
1233 struct cam_periph *periph;
1234 struct da_softc *softc;
1236 periph = (struct cam_periph *)dp->d_drv1;
1237 cam_periph_lock(periph);
1238 if (cam_periph_hold(periph, PRIBIO) != 0) {
1239 cam_periph_unlock(periph);
1240 cam_periph_release(periph);
1244 softc = (struct da_softc *)periph->softc;
1246 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1249 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0
1250 && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
1253 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1255 scsi_synchronize_cache(&ccb->csio,
1259 /*begin_lba*/0,/* Cover the whole disk */
1264 cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
1265 /*sense_flags*/SF_RETRY_UA | SF_QUIET_IR,
1266 softc->disk->d_devstat);
1267 xpt_release_ccb(ccb);
1271 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
1272 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
1273 daprevent(periph, PR_ALLOW);
1275 * If we've got removeable media, mark the blocksize as
1276 * unavailable, since it could change when new media is
1279 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1282 softc->flags &= ~DA_FLAG_OPEN;
1283 cam_periph_unhold(periph);
1284 cam_periph_unlock(periph);
1285 cam_periph_release(periph);
1290 daschedule(struct cam_periph *periph)
1292 struct da_softc *softc = (struct da_softc *)periph->softc;
1295 if (softc->state != DA_STATE_NORMAL)
1298 /* Check if cam_periph_getccb() was called. */
1299 prio = periph->immediate_priority;
1301 /* Check if we have more work to do. */
1302 if (bioq_first(&softc->bio_queue) ||
1303 (!softc->delete_running && bioq_first(&softc->delete_queue)) ||
1305 prio = CAM_PRIORITY_NORMAL;
1308 /* Schedule CCB if any of above is true. */
1309 if (prio != CAM_PRIORITY_NONE)
1310 xpt_schedule(periph, prio);
1314 * Actually translate the requested transfer into one the physical driver
1315 * can understand. The transfer is described by a buf and will include
1316 * only one physical transfer.
1319 dastrategy(struct bio *bp)
1321 struct cam_periph *periph;
1322 struct da_softc *softc;
1324 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1325 softc = (struct da_softc *)periph->softc;
1327 cam_periph_lock(periph);
1330 * If the device has been made invalid, error out
1332 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1333 cam_periph_unlock(periph);
1334 biofinish(bp, NULL, ENXIO);
1338 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
1341 * Place it in the queue of disk activities for this disk
1343 if (bp->bio_cmd == BIO_DELETE) {
1344 if (bp->bio_bcount == 0)
1347 bioq_disksort(&softc->delete_queue, bp);
1349 bioq_insert_tail(&softc->delete_queue, bp);
1350 } else if (DA_SIO) {
1351 bioq_disksort(&softc->bio_queue, bp);
1353 bioq_insert_tail(&softc->bio_queue, bp);
1357 * Schedule ourselves for performing the work.
1360 cam_periph_unlock(periph);
1366 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1368 struct cam_periph *periph;
1369 struct da_softc *softc;
1371 struct ccb_scsiio csio;
1376 periph = dp->d_drv1;
1377 softc = (struct da_softc *)periph->softc;
1378 cam_periph_lock(periph);
1379 secsize = softc->params.secsize;
1381 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1382 cam_periph_unlock(periph);
1387 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1388 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1389 scsi_read_write(&csio,
1393 /*read*/SCSI_RW_WRITE,
1395 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1398 /*data_ptr*/(u_int8_t *) virtual,
1399 /*dxfer_len*/length,
1400 /*sense_len*/SSD_FULL_SIZE,
1401 da_default_timeout * 1000);
1402 xpt_polled_action((union ccb *)&csio);
1404 error = cam_periph_error((union ccb *)&csio,
1405 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1406 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1407 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1408 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1410 printf("Aborting dump due to I/O error.\n");
1411 cam_periph_unlock(periph);
1416 * Sync the disk cache contents to the physical media.
1418 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1420 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1421 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1422 scsi_synchronize_cache(&csio,
1426 /*begin_lba*/0,/* Cover the whole disk */
1430 xpt_polled_action((union ccb *)&csio);
1432 error = cam_periph_error((union ccb *)&csio,
1433 0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL);
1434 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1435 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1436 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1438 xpt_print(periph->path, "Synchronize cache failed\n");
1440 cam_periph_unlock(periph);
1445 dagetattr(struct bio *bp)
1448 struct cam_periph *periph;
1450 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1451 cam_periph_lock(periph);
1452 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1454 cam_periph_unlock(periph);
1456 bp->bio_completed = bp->bio_length;
1466 * Install a global async callback. This callback will
1467 * receive async callbacks like "new device found".
1469 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1471 if (status != CAM_REQ_CMP) {
1472 printf("da: Failed to attach master async callback "
1473 "due to status 0x%x!\n", status);
1474 } else if (da_send_ordered) {
1476 /* Register our shutdown event handler */
1477 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1478 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1479 printf("dainit: shutdown event registration failed!\n");
1484 * Callback from GEOM, called when it has finished cleaning up its
1488 dadiskgonecb(struct disk *dp)
1490 struct cam_periph *periph;
1492 periph = (struct cam_periph *)dp->d_drv1;
1493 cam_periph_release(periph);
1497 daoninvalidate(struct cam_periph *periph)
1499 struct da_softc *softc;
1501 softc = (struct da_softc *)periph->softc;
1504 * De-register any async callbacks.
1506 xpt_register_async(0, daasync, periph, periph->path);
1508 softc->flags |= DA_FLAG_PACK_INVALID;
1511 * Return all queued I/O with ENXIO.
1512 * XXX Handle any transactions queued to the card
1513 * with XPT_ABORT_CCB.
1515 bioq_flush(&softc->bio_queue, NULL, ENXIO);
1516 bioq_flush(&softc->delete_queue, NULL, ENXIO);
1519 * Tell GEOM that we've gone away, we'll get a callback when it is
1520 * done cleaning up its resources.
1522 disk_gone(softc->disk);
1524 xpt_print(periph->path, "lost device - %d outstanding, %d refs\n",
1525 softc->outstanding_cmds, periph->refcount);
1529 dacleanup(struct cam_periph *periph)
1531 struct da_softc *softc;
1533 softc = (struct da_softc *)periph->softc;
1535 xpt_print(periph->path, "removing device entry\n");
1536 cam_periph_unlock(periph);
1539 * If we can't free the sysctl tree, oh well...
1541 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1542 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1543 xpt_print(periph->path, "can't remove sysctl context\n");
1546 callout_drain(&softc->mediapoll_c);
1547 disk_destroy(softc->disk);
1548 callout_drain(&softc->sendordered_c);
1549 free(softc, M_DEVBUF);
1550 cam_periph_lock(periph);
1554 daasync(void *callback_arg, u_int32_t code,
1555 struct cam_path *path, void *arg)
1557 struct cam_periph *periph;
1558 struct da_softc *softc;
1560 periph = (struct cam_periph *)callback_arg;
1562 case AC_FOUND_DEVICE:
1564 struct ccb_getdev *cgd;
1567 cgd = (struct ccb_getdev *)arg;
1571 if (cgd->protocol != PROTO_SCSI)
1574 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1575 && SID_TYPE(&cgd->inq_data) != T_RBC
1576 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1580 * Allocate a peripheral instance for
1581 * this device and start the probe
1584 status = cam_periph_alloc(daregister, daoninvalidate,
1586 "da", CAM_PERIPH_BIO,
1587 cgd->ccb_h.path, daasync,
1588 AC_FOUND_DEVICE, cgd);
1590 if (status != CAM_REQ_CMP
1591 && status != CAM_REQ_INPROG)
1592 printf("daasync: Unable to attach to new device "
1593 "due to status 0x%x\n", status);
1596 case AC_ADVINFO_CHANGED:
1600 buftype = (uintptr_t)arg;
1601 if (buftype == CDAI_TYPE_PHYS_PATH) {
1602 struct da_softc *softc;
1604 softc = periph->softc;
1605 disk_attr_changed(softc->disk, "GEOM::physpath",
1610 case AC_UNIT_ATTENTION:
1613 int error_code, sense_key, asc, ascq;
1615 softc = (struct da_softc *)periph->softc;
1616 ccb = (union ccb *)arg;
1619 * Handle all UNIT ATTENTIONs except our own,
1620 * as they will be handled by daerror().
1622 if (xpt_path_periph(ccb->ccb_h.path) != periph &&
1623 scsi_extract_sense_ccb(ccb,
1624 &error_code, &sense_key, &asc, &ascq)) {
1625 if (asc == 0x2A && ascq == 0x09) {
1626 xpt_print(ccb->ccb_h.path,
1627 "capacity data has changed\n");
1629 } else if (asc == 0x28 && ascq == 0x00)
1630 disk_media_changed(softc->disk, M_NOWAIT);
1632 cam_periph_async(periph, code, path, arg);
1636 softc = (struct da_softc *)periph->softc;
1638 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
1647 struct ccb_hdr *ccbh;
1649 softc = (struct da_softc *)periph->softc;
1651 * Don't fail on the expected unit attention
1654 softc->flags |= DA_FLAG_RETRY_UA;
1655 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1656 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1662 cam_periph_async(periph, code, path, arg);
1666 dasysctlinit(void *context, int pending)
1668 struct cam_periph *periph;
1669 struct da_softc *softc;
1670 char tmpstr[80], tmpstr2[80];
1671 struct ccb_trans_settings cts;
1673 periph = (struct cam_periph *)context;
1675 * periph was held for us when this task was enqueued
1677 if (periph->flags & CAM_PERIPH_INVALID) {
1678 cam_periph_release(periph);
1682 softc = (struct da_softc *)periph->softc;
1683 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1684 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1686 sysctl_ctx_init(&softc->sysctl_ctx);
1687 softc->flags |= DA_FLAG_SCTX_INIT;
1688 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1689 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1690 CTLFLAG_RD, 0, tmpstr);
1691 if (softc->sysctl_tree == NULL) {
1692 printf("dasysctlinit: unable to allocate sysctl tree\n");
1693 cam_periph_release(periph);
1698 * Now register the sysctl handler, so the user can change the value on
1701 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1702 OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RW,
1703 softc, 0, dadeletemethodsysctl, "A",
1704 "BIO_DELETE execution method");
1705 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1706 OID_AUTO, "delete_max", CTLTYPE_U64 | CTLFLAG_RW,
1707 softc, 0, dadeletemaxsysctl, "Q",
1708 "Maximum BIO_DELETE size");
1709 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1710 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1711 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1712 "Minimum CDB size");
1713 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1714 OID_AUTO, "sort_io_queue", CTLFLAG_RW, &softc->sort_io_queue, 0,
1715 "Sort IO queue to try and optimise disk access patterns");
1717 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1718 SYSCTL_CHILDREN(softc->sysctl_tree),
1722 &softc->error_inject,
1724 "error_inject leaf");
1728 * Add some addressing info.
1730 memset(&cts, 0, sizeof (cts));
1731 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1732 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1733 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1734 cam_periph_lock(periph);
1735 xpt_action((union ccb *)&cts);
1736 cam_periph_unlock(periph);
1737 if (cts.ccb_h.status != CAM_REQ_CMP) {
1738 cam_periph_release(periph);
1741 if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
1742 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1743 if (fc->valid & CTS_FC_VALID_WWPN) {
1744 softc->wwpn = fc->wwpn;
1745 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1746 SYSCTL_CHILDREN(softc->sysctl_tree),
1747 OID_AUTO, "wwpn", CTLFLAG_RD,
1748 &softc->wwpn, "World Wide Port Name");
1751 cam_periph_release(periph);
1755 dadeletemaxsysctl(SYSCTL_HANDLER_ARGS)
1759 struct da_softc *softc;
1761 softc = (struct da_softc *)arg1;
1763 value = softc->disk->d_delmaxsize;
1764 error = sysctl_handle_64(oidp, &value, 0, req);
1765 if ((error != 0) || (req->newptr == NULL))
1768 /* only accept values smaller than the calculated value */
1769 if (value > dadeletemaxsize(softc, softc->delete_method)) {
1772 softc->disk->d_delmaxsize = value;
1778 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1782 value = *(int *)arg1;
1784 error = sysctl_handle_int(oidp, &value, 0, req);
1787 || (req->newptr == NULL))
1791 * Acceptable values here are 6, 10, 12 or 16.
1795 else if ((value > 6)
1798 else if ((value > 10)
1801 else if (value > 12)
1804 *(int *)arg1 = value;
1810 dadeletemethodset(struct da_softc *softc, da_delete_methods delete_method)
1814 softc->delete_method = delete_method;
1815 softc->disk->d_delmaxsize = dadeletemaxsize(softc, delete_method);
1817 if (softc->delete_method > DA_DELETE_DISABLE)
1818 softc->disk->d_flags |= DISKFLAG_CANDELETE;
1820 softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
1824 dadeletemaxsize(struct da_softc *softc, da_delete_methods delete_method)
1828 switch(delete_method) {
1829 case DA_DELETE_UNMAP:
1830 sectors = (off_t)softc->unmap_max_lba * softc->unmap_max_ranges;
1832 case DA_DELETE_ATA_TRIM:
1833 sectors = (off_t)ATA_DSM_RANGE_MAX * softc->trim_max_ranges;
1835 case DA_DELETE_WS16:
1836 sectors = (off_t)min(softc->ws_max_blks, WS16_MAX_BLKS);
1838 case DA_DELETE_ZERO:
1839 case DA_DELETE_WS10:
1840 sectors = (off_t)min(softc->ws_max_blks, WS10_MAX_BLKS);
1846 return (off_t)softc->params.secsize *
1847 min(sectors, (off_t)softc->params.sectors);
1851 daprobedone(struct cam_periph *periph, union ccb *ccb)
1853 struct da_softc *softc;
1855 softc = (struct da_softc *)periph->softc;
1857 dadeletemethodchoose(softc, DA_DELETE_NONE);
1859 if (bootverbose && (softc->flags & DA_FLAG_PROBED) == 0) {
1863 snprintf(buf, sizeof(buf), "Delete methods: <");
1865 for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
1866 if (softc->delete_available & (1 << i)) {
1868 strlcat(buf, ",", sizeof(buf));
1872 strlcat(buf, da_delete_method_names[i],
1874 if (i == softc->delete_method) {
1875 strlcat(buf, "(*)", sizeof(buf));
1880 if (softc->delete_method == DA_DELETE_NONE)
1881 strlcat(buf, "NONE(*)", sizeof(buf));
1883 strlcat(buf, "DISABLED(*)", sizeof(buf));
1885 strlcat(buf, ">", sizeof(buf));
1886 printf("%s%d: %s\n", periph->periph_name,
1887 periph->unit_number, buf);
1891 * Since our peripheral may be invalidated by an error
1892 * above or an external event, we must release our CCB
1893 * before releasing the probe lock on the peripheral.
1894 * The peripheral will only go away once the last lock
1895 * is removed, and we need it around for the CCB release
1898 xpt_release_ccb(ccb);
1899 softc->state = DA_STATE_NORMAL;
1901 wakeup(&softc->disk->d_mediasize);
1902 if ((softc->flags & DA_FLAG_PROBED) == 0) {
1903 softc->flags |= DA_FLAG_PROBED;
1904 cam_periph_unhold(periph);
1906 cam_periph_release_locked(periph);
1910 dadeletemethodchoose(struct da_softc *softc, da_delete_methods default_method)
1912 int i, delete_method;
1914 delete_method = default_method;
1917 * Use the pre-defined order to choose the best
1918 * performing delete.
1920 for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
1921 if (softc->delete_available & (1 << i)) {
1922 dadeletemethodset(softc, i);
1926 dadeletemethodset(softc, delete_method);
1930 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
1934 struct da_softc *softc;
1935 int i, error, value;
1937 softc = (struct da_softc *)arg1;
1939 value = softc->delete_method;
1940 if (value < 0 || value > DA_DELETE_MAX)
1943 p = da_delete_method_names[value];
1944 strncpy(buf, p, sizeof(buf));
1945 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1946 if (error != 0 || req->newptr == NULL)
1948 for (i = 0; i <= DA_DELETE_MAX; i++) {
1949 if (!(softc->delete_available & (1 << i)) ||
1950 strcmp(buf, da_delete_method_names[i]) != 0)
1952 dadeletemethodset(softc, i);
1959 daregister(struct cam_periph *periph, void *arg)
1961 struct da_softc *softc;
1962 struct ccb_pathinq cpi;
1963 struct ccb_getdev *cgd;
1967 cgd = (struct ccb_getdev *)arg;
1969 printf("daregister: no getdev CCB, can't register device\n");
1970 return(CAM_REQ_CMP_ERR);
1973 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
1976 if (softc == NULL) {
1977 printf("daregister: Unable to probe new device. "
1978 "Unable to allocate softc\n");
1979 return(CAM_REQ_CMP_ERR);
1982 LIST_INIT(&softc->pending_ccbs);
1983 softc->state = DA_STATE_PROBE_RC;
1984 bioq_init(&softc->bio_queue);
1985 bioq_init(&softc->delete_queue);
1986 bioq_init(&softc->delete_run_queue);
1987 if (SID_IS_REMOVABLE(&cgd->inq_data))
1988 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1989 softc->unmap_max_ranges = UNMAP_MAX_RANGES;
1990 softc->unmap_max_lba = UNMAP_RANGE_MAX;
1991 softc->ws_max_blks = WS16_MAX_BLKS;
1992 softc->trim_max_ranges = ATA_TRIM_MAX_RANGES;
1993 softc->sort_io_queue = -1;
1995 periph->softc = softc;
1998 * See if this device has any quirks.
2000 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
2001 (caddr_t)da_quirk_table,
2002 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
2003 sizeof(*da_quirk_table), scsi_inquiry_match);
2006 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
2008 softc->quirks = DA_Q_NONE;
2010 /* Check if the SIM does not want 6 byte commands */
2011 bzero(&cpi, sizeof(cpi));
2012 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2013 cpi.ccb_h.func_code = XPT_PATH_INQ;
2014 xpt_action((union ccb *)&cpi);
2015 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
2016 softc->quirks |= DA_Q_NO_6_BYTE;
2018 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
2021 * Take an exclusive refcount on the periph while dastart is called
2022 * to finish the probe. The reference will be dropped in dadone at
2025 (void)cam_periph_hold(periph, PRIBIO);
2028 * Schedule a periodic event to occasionally send an
2029 * ordered tag to a device.
2031 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
2032 callout_reset(&softc->sendordered_c,
2033 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2034 dasendorderedtag, softc);
2036 cam_periph_unlock(periph);
2038 * RBC devices don't have to support READ(6), only READ(10).
2040 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
2041 softc->minimum_cmd_size = 10;
2043 softc->minimum_cmd_size = 6;
2046 * Load the user's default, if any.
2048 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
2049 periph->unit_number);
2050 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
2053 * 6, 10, 12 and 16 are the currently permissible values.
2055 if (softc->minimum_cmd_size < 6)
2056 softc->minimum_cmd_size = 6;
2057 else if ((softc->minimum_cmd_size > 6)
2058 && (softc->minimum_cmd_size <= 10))
2059 softc->minimum_cmd_size = 10;
2060 else if ((softc->minimum_cmd_size > 10)
2061 && (softc->minimum_cmd_size <= 12))
2062 softc->minimum_cmd_size = 12;
2063 else if (softc->minimum_cmd_size > 12)
2064 softc->minimum_cmd_size = 16;
2066 /* Predict whether device may support READ CAPACITY(16). */
2067 if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3) {
2068 softc->flags |= DA_FLAG_CAN_RC16;
2069 softc->state = DA_STATE_PROBE_RC16;
2073 * Register this media as a disk.
2075 softc->disk = disk_alloc();
2076 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
2077 periph->unit_number, 0,
2078 DEVSTAT_BS_UNAVAILABLE,
2079 SID_TYPE(&cgd->inq_data) |
2080 XPORT_DEVSTAT_TYPE(cpi.transport),
2081 DEVSTAT_PRIORITY_DISK);
2082 softc->disk->d_open = daopen;
2083 softc->disk->d_close = daclose;
2084 softc->disk->d_strategy = dastrategy;
2085 softc->disk->d_dump = dadump;
2086 softc->disk->d_getattr = dagetattr;
2087 softc->disk->d_gone = dadiskgonecb;
2088 softc->disk->d_name = "da";
2089 softc->disk->d_drv1 = periph;
2091 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */
2092 else if (cpi.maxio > MAXPHYS)
2093 softc->disk->d_maxsize = MAXPHYS; /* for safety */
2095 softc->disk->d_maxsize = cpi.maxio;
2096 softc->disk->d_unit = periph->unit_number;
2097 softc->disk->d_flags = 0;
2098 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
2099 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
2100 if ((cpi.hba_misc & PIM_UNMAPPED) != 0)
2101 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
2102 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
2103 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
2104 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
2105 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
2106 cgd->inq_data.product, sizeof(cgd->inq_data.product),
2107 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
2108 softc->disk->d_hba_vendor = cpi.hba_vendor;
2109 softc->disk->d_hba_device = cpi.hba_device;
2110 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
2111 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
2114 * Acquire a reference to the periph before we register with GEOM.
2115 * We'll release this reference once GEOM calls us back (via
2116 * dadiskgonecb()) telling us that our provider has been freed.
2118 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
2119 xpt_print(periph->path, "%s: lost periph during "
2120 "registration!\n", __func__);
2121 cam_periph_lock(periph);
2122 return (CAM_REQ_CMP_ERR);
2125 disk_create(softc->disk, DISK_VERSION);
2126 cam_periph_lock(periph);
2129 * Add async callbacks for events of interest.
2130 * I don't bother checking if this fails as,
2131 * in most cases, the system will function just
2132 * fine without them and the only alternative
2133 * would be to not attach the device on failure.
2135 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
2136 AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION,
2137 daasync, periph, periph->path);
2140 * Emit an attribute changed notification just in case
2141 * physical path information arrived before our async
2142 * event handler was registered, but after anyone attaching
2143 * to our disk device polled it.
2145 disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
2148 * Schedule a periodic media polling events.
2150 callout_init_mtx(&softc->mediapoll_c, periph->sim->mtx, 0);
2151 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
2152 (cgd->inq_flags & SID_AEN) == 0 &&
2153 da_poll_period != 0)
2154 callout_reset(&softc->mediapoll_c, da_poll_period * hz,
2155 damediapoll, periph);
2157 xpt_schedule(periph, CAM_PRIORITY_DEV);
2159 return(CAM_REQ_CMP);
2163 dastart(struct cam_periph *periph, union ccb *start_ccb)
2165 struct da_softc *softc;
2167 softc = (struct da_softc *)periph->softc;
2169 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
2172 switch (softc->state) {
2173 case DA_STATE_NORMAL:
2175 struct bio *bp, *bp1;
2178 /* Execute immediate CCB if waiting. */
2179 if (periph->immediate_priority <= periph->pinfo.priority) {
2180 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2181 ("queuing for immediate ccb\n"));
2182 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
2183 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
2185 periph->immediate_priority = CAM_PRIORITY_NONE;
2186 wakeup(&periph->ccb_list);
2187 /* May have more work to do, so ensure we stay scheduled */
2192 /* Run BIO_DELETE if not running yet. */
2193 if (!softc->delete_running &&
2194 (bp = bioq_first(&softc->delete_queue)) != NULL) {
2196 uint64_t count; /* forward compat with WS32 */
2199 * In each of the methods below, while its the caller's
2200 * responsibility to ensure the request will fit into a
2201 * single device request, we might have changed the delete
2202 * method due to the device incorrectly advertising either
2203 * its supported methods or limits.
2205 * To prevent this causing further issues we validate the
2206 * against the methods limits, and warn which would
2207 * otherwise be unnecessary.
2210 if (softc->delete_method == DA_DELETE_UNMAP) {
2211 uint8_t *buf = softc->unmap_buf;
2212 uint64_t lastlba = (uint64_t)-1;
2213 uint32_t lastcount = 0, c;
2214 uint64_t totalcount = 0;
2215 uint32_t off, ranges = 0;
2218 * Currently this doesn't take the UNMAP
2219 * Granularity and Granularity Alignment
2220 * fields into account.
2222 * This could result in both unoptimal unmap
2223 * requests as as well as UNMAP calls unmapping
2224 * fewer LBA's than requested.
2227 softc->delete_running = 1;
2228 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2231 bioq_remove(&softc->delete_queue, bp1);
2233 bioq_insert_tail(&softc->delete_run_queue, bp1);
2234 lba = bp1->bio_pblkno;
2235 count = bp1->bio_bcount / softc->params.secsize;
2237 /* Try to extend the previous range. */
2238 if (lba == lastlba) {
2239 c = min(count, softc->unmap_max_lba -
2242 off = ((ranges - 1) * UNMAP_RANGE_SIZE) +
2244 scsi_ulto4b(lastcount, &buf[off + 8]);
2251 c = min(count, softc->unmap_max_lba);
2252 if (totalcount + c > softc->unmap_max_lba ||
2253 ranges >= softc->unmap_max_ranges) {
2254 xpt_print(periph->path,
2255 "%s issuing short delete %ld > %ld"
2257 da_delete_method_desc[softc->delete_method],
2258 totalcount + c, softc->unmap_max_lba,
2259 ranges, softc->unmap_max_ranges);
2262 off = (ranges * UNMAP_RANGE_SIZE) +
2264 scsi_u64to8b(lba, &buf[off + 0]);
2265 scsi_ulto4b(c, &buf[off + 8]);
2273 bp1 = bioq_first(&softc->delete_queue);
2275 ranges >= softc->unmap_max_ranges ||
2276 totalcount + bp1->bio_bcount /
2277 softc->params.secsize > softc->unmap_max_lba)
2280 scsi_ulto2b(ranges * 16 + 6, &buf[0]);
2281 scsi_ulto2b(ranges * 16, &buf[2]);
2283 scsi_unmap(&start_ccb->csio,
2284 /*retries*/da_retry_count,
2286 /*tag_action*/MSG_SIMPLE_Q_TAG,
2289 /*dxfer_len*/ ranges * 16 + 8,
2290 /*sense_len*/SSD_FULL_SIZE,
2291 da_default_timeout * 1000);
2292 start_ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2294 } else if (softc->delete_method == DA_DELETE_ATA_TRIM) {
2295 uint8_t *buf = softc->unmap_buf;
2296 uint64_t lastlba = (uint64_t)-1;
2297 uint32_t lastcount = 0, c, requestcount;
2298 int ranges = 0, off, block_count;
2300 softc->delete_running = 1;
2301 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2304 bioq_remove(&softc->delete_queue, bp1);
2306 bioq_insert_tail(&softc->delete_run_queue, bp1);
2307 lba = bp1->bio_pblkno;
2308 count = bp1->bio_bcount / softc->params.secsize;
2309 requestcount = count;
2311 /* Try to extend the previous range. */
2312 if (lba == lastlba) {
2313 c = min(count, ATA_DSM_RANGE_MAX - lastcount);
2315 off = (ranges - 1) * 8;
2316 buf[off + 6] = lastcount & 0xff;
2317 buf[off + 7] = (lastcount >> 8) & 0xff;
2323 c = min(count, ATA_DSM_RANGE_MAX);
2326 buf[off + 0] = lba & 0xff;
2327 buf[off + 1] = (lba >> 8) & 0xff;
2328 buf[off + 2] = (lba >> 16) & 0xff;
2329 buf[off + 3] = (lba >> 24) & 0xff;
2330 buf[off + 4] = (lba >> 32) & 0xff;
2331 buf[off + 5] = (lba >> 40) & 0xff;
2332 buf[off + 6] = c & 0xff;
2333 buf[off + 7] = (c >> 8) & 0xff;
2338 if (count != 0 && ranges == softc->trim_max_ranges) {
2339 xpt_print(periph->path,
2340 "%s issuing short delete %ld > %ld",
2341 da_delete_method_desc[softc->delete_method],
2343 (softc->trim_max_ranges - ranges) *
2349 bp1 = bioq_first(&softc->delete_queue);
2351 bp1->bio_bcount / softc->params.secsize >
2352 (softc->trim_max_ranges - ranges) *
2357 block_count = (ranges + ATA_DSM_BLK_RANGES - 1) /
2359 scsi_ata_trim(&start_ccb->csio,
2360 /*retries*/da_retry_count,
2362 /*tag_action*/MSG_SIMPLE_Q_TAG,
2365 /*dxfer_len*/block_count * ATA_DSM_BLK_SIZE,
2366 /*sense_len*/SSD_FULL_SIZE,
2367 da_default_timeout * 1000);
2368 start_ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2370 } else if (softc->delete_method == DA_DELETE_ZERO ||
2371 softc->delete_method == DA_DELETE_WS10 ||
2372 softc->delete_method == DA_DELETE_WS16) {
2374 * We calculate ws_max_blks here based off d_delmaxsize instead
2375 * of using softc->ws_max_blks as it is absolute max for the
2376 * device not the protocol max which may well be lower
2378 uint64_t ws_max_blks;
2379 ws_max_blks = softc->disk->d_delmaxsize / softc->params.secsize;
2380 softc->delete_running = 1;
2381 lba = bp->bio_pblkno;
2385 bioq_remove(&softc->delete_queue, bp1);
2387 bioq_insert_tail(&softc->delete_run_queue, bp1);
2388 count += bp1->bio_bcount / softc->params.secsize;
2389 if (count > ws_max_blks) {
2390 count = min(count, ws_max_blks);
2391 xpt_print(periph->path,
2392 "%s issuing short delete %ld > %ld",
2393 da_delete_method_desc[softc->delete_method],
2394 count, ws_max_blks);
2397 bp1 = bioq_first(&softc->delete_queue);
2399 lba + count != bp1->bio_pblkno ||
2400 count + bp1->bio_bcount /
2401 softc->params.secsize > ws_max_blks)
2405 scsi_write_same(&start_ccb->csio,
2406 /*retries*/da_retry_count,
2408 /*tag_action*/MSG_SIMPLE_Q_TAG,
2409 /*byte2*/softc->delete_method ==
2410 DA_DELETE_ZERO ? 0 : SWS_UNMAP,
2411 softc->delete_method ==
2412 DA_DELETE_WS16 ? 16 : 10,
2414 /*block_count*/count,
2415 /*data_ptr*/ __DECONST(void *,
2417 /*dxfer_len*/ softc->params.secsize,
2418 /*sense_len*/SSD_FULL_SIZE,
2419 da_default_timeout * 1000);
2420 start_ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2423 bioq_flush(&softc->delete_queue, NULL, 0);
2428 /* Run regular command. */
2429 bp = bioq_takefirst(&softc->bio_queue);
2433 scsi_test_unit_ready(&start_ccb->csio,
2434 /*retries*/ da_retry_count,
2438 da_default_timeout * 1000);
2439 start_ccb->ccb_h.ccb_bp = NULL;
2440 start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
2441 xpt_action(start_ccb);
2443 xpt_release_ccb(start_ccb);
2448 cam_periph_release_locked(periph);
2451 if ((bp->bio_flags & BIO_ORDERED) != 0 ||
2452 (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
2453 softc->flags &= ~DA_FLAG_NEED_OTAG;
2454 softc->ordered_tag_count++;
2455 tag_code = MSG_ORDERED_Q_TAG;
2457 tag_code = MSG_SIMPLE_Q_TAG;
2460 switch (bp->bio_cmd) {
2463 scsi_read_write(&start_ccb->csio,
2464 /*retries*/da_retry_count,
2466 /*tag_action*/tag_code,
2467 /*read_op*/(bp->bio_cmd == BIO_READ ?
2468 SCSI_RW_READ : SCSI_RW_WRITE) |
2469 ((bp->bio_flags & BIO_UNMAPPED) != 0 ?
2472 softc->minimum_cmd_size,
2473 /*lba*/bp->bio_pblkno,
2474 /*block_count*/bp->bio_bcount /
2475 softc->params.secsize,
2476 /*data_ptr*/ (bp->bio_flags &
2477 BIO_UNMAPPED) != 0 ? (void *)bp :
2479 /*dxfer_len*/ bp->bio_bcount,
2480 /*sense_len*/SSD_FULL_SIZE,
2481 da_default_timeout * 1000);
2485 * BIO_FLUSH doesn't currently communicate
2486 * range data, so we synchronize the cache
2487 * over the whole disk. We also force
2488 * ordered tag semantics the flush applies
2489 * to all previously queued I/O.
2491 scsi_synchronize_cache(&start_ccb->csio,
2498 da_default_timeout*1000);
2501 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
2505 * Block out any asyncronous callbacks
2506 * while we touch the pending ccb list.
2508 LIST_INSERT_HEAD(&softc->pending_ccbs,
2509 &start_ccb->ccb_h, periph_links.le);
2510 softc->outstanding_cmds++;
2512 /* We expect a unit attention from this device */
2513 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
2514 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
2515 softc->flags &= ~DA_FLAG_RETRY_UA;
2518 start_ccb->ccb_h.ccb_bp = bp;
2519 xpt_action(start_ccb);
2521 /* May have more work to do, so ensure we stay scheduled */
2525 case DA_STATE_PROBE_RC:
2527 struct scsi_read_capacity_data *rcap;
2529 rcap = (struct scsi_read_capacity_data *)
2530 malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
2532 printf("dastart: Couldn't malloc read_capacity data\n");
2533 /* da_free_periph??? */
2536 scsi_read_capacity(&start_ccb->csio,
2537 /*retries*/da_retry_count,
2543 start_ccb->ccb_h.ccb_bp = NULL;
2544 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC;
2545 xpt_action(start_ccb);
2548 case DA_STATE_PROBE_RC16:
2550 struct scsi_read_capacity_data_long *rcaplong;
2552 rcaplong = (struct scsi_read_capacity_data_long *)
2553 malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
2554 if (rcaplong == NULL) {
2555 printf("dastart: Couldn't malloc read_capacity data\n");
2556 /* da_free_periph??? */
2559 scsi_read_capacity_16(&start_ccb->csio,
2560 /*retries*/ da_retry_count,
2562 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2567 /*sense_len*/ SSD_FULL_SIZE,
2568 /*timeout*/ da_default_timeout * 1000);
2569 start_ccb->ccb_h.ccb_bp = NULL;
2570 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC16;
2571 xpt_action(start_ccb);
2574 case DA_STATE_PROBE_LBP:
2576 struct scsi_vpd_logical_block_prov *lbp;
2578 if (!scsi_vpd_supported_page(periph, SVPD_LBP)) {
2580 * If we get here we don't support any SBC-3 delete
2581 * methods with UNMAP as the Logical Block Provisioning
2582 * VPD page support is required for devices which
2583 * support it according to T10/1799-D Revision 31
2584 * however older revisions of the spec don't mandate
2585 * this so we currently don't remove these methods
2586 * from the available set.
2588 softc->state = DA_STATE_PROBE_BLK_LIMITS;
2592 lbp = (struct scsi_vpd_logical_block_prov *)
2593 malloc(sizeof(*lbp), M_SCSIDA, M_NOWAIT|M_ZERO);
2596 printf("dastart: Couldn't malloc lbp data\n");
2597 /* da_free_periph??? */
2601 scsi_inquiry(&start_ccb->csio,
2602 /*retries*/da_retry_count,
2604 /*tag_action*/MSG_SIMPLE_Q_TAG,
2605 /*inq_buf*/(u_int8_t *)lbp,
2606 /*inq_len*/sizeof(*lbp),
2608 /*page_code*/SVPD_LBP,
2609 /*sense_len*/SSD_MIN_SIZE,
2610 /*timeout*/da_default_timeout * 1000);
2611 start_ccb->ccb_h.ccb_bp = NULL;
2612 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_LBP;
2613 xpt_action(start_ccb);
2616 case DA_STATE_PROBE_BLK_LIMITS:
2618 struct scsi_vpd_block_limits *block_limits;
2620 if (!scsi_vpd_supported_page(periph, SVPD_BLOCK_LIMITS)) {
2621 /* Not supported skip to next probe */
2622 softc->state = DA_STATE_PROBE_BDC;
2626 block_limits = (struct scsi_vpd_block_limits *)
2627 malloc(sizeof(*block_limits), M_SCSIDA, M_NOWAIT|M_ZERO);
2629 if (block_limits == NULL) {
2630 printf("dastart: Couldn't malloc block_limits data\n");
2631 /* da_free_periph??? */
2635 scsi_inquiry(&start_ccb->csio,
2636 /*retries*/da_retry_count,
2638 /*tag_action*/MSG_SIMPLE_Q_TAG,
2639 /*inq_buf*/(u_int8_t *)block_limits,
2640 /*inq_len*/sizeof(*block_limits),
2642 /*page_code*/SVPD_BLOCK_LIMITS,
2643 /*sense_len*/SSD_MIN_SIZE,
2644 /*timeout*/da_default_timeout * 1000);
2645 start_ccb->ccb_h.ccb_bp = NULL;
2646 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BLK_LIMITS;
2647 xpt_action(start_ccb);
2650 case DA_STATE_PROBE_BDC:
2652 struct scsi_vpd_block_characteristics *bdc;
2654 if (!scsi_vpd_supported_page(periph, SVPD_BDC)) {
2655 softc->state = DA_STATE_PROBE_ATA;
2659 bdc = (struct scsi_vpd_block_characteristics *)
2660 malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
2663 printf("dastart: Couldn't malloc bdc data\n");
2664 /* da_free_periph??? */
2668 scsi_inquiry(&start_ccb->csio,
2669 /*retries*/da_retry_count,
2671 /*tag_action*/MSG_SIMPLE_Q_TAG,
2672 /*inq_buf*/(u_int8_t *)bdc,
2673 /*inq_len*/sizeof(*bdc),
2675 /*page_code*/SVPD_BDC,
2676 /*sense_len*/SSD_MIN_SIZE,
2677 /*timeout*/da_default_timeout * 1000);
2678 start_ccb->ccb_h.ccb_bp = NULL;
2679 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BDC;
2680 xpt_action(start_ccb);
2683 case DA_STATE_PROBE_ATA:
2685 struct ata_params *ata_params;
2687 if (!scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
2688 daprobedone(periph, start_ccb);
2692 ata_params = (struct ata_params*)
2693 malloc(sizeof(*ata_params), M_SCSIDA, M_NOWAIT|M_ZERO);
2695 if (ata_params == NULL) {
2696 printf("dastart: Couldn't malloc ata_params data\n");
2697 /* da_free_periph??? */
2701 scsi_ata_identify(&start_ccb->csio,
2702 /*retries*/da_retry_count,
2704 /*tag_action*/MSG_SIMPLE_Q_TAG,
2705 /*data_ptr*/(u_int8_t *)ata_params,
2706 /*dxfer_len*/sizeof(*ata_params),
2707 /*sense_len*/SSD_FULL_SIZE,
2708 /*timeout*/da_default_timeout * 1000);
2709 start_ccb->ccb_h.ccb_bp = NULL;
2710 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA;
2711 xpt_action(start_ccb);
2718 cmd6workaround(union ccb *ccb)
2720 struct scsi_rw_6 cmd6;
2721 struct scsi_rw_10 *cmd10;
2722 struct da_softc *softc;
2727 cdb = ccb->csio.cdb_io.cdb_bytes;
2728 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
2730 if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
2731 da_delete_methods old_method = softc->delete_method;
2734 * Typically there are two reasons for failure here
2735 * 1. Delete method was detected as supported but isn't
2736 * 2. Delete failed due to invalid params e.g. too big
2738 * While we will attempt to choose an alternative delete method
2739 * this may result in short deletes if the existing delete
2740 * requests from geom are big for the new method choosen.
2742 * This method assumes that the error which triggered this
2743 * will not retry the io otherwise a panic will occur
2745 dadeleteflag(softc, old_method, 0);
2746 dadeletemethodchoose(softc, DA_DELETE_DISABLE);
2747 if (softc->delete_method == DA_DELETE_DISABLE)
2748 xpt_print(ccb->ccb_h.path,
2749 "%s failed, disabling BIO_DELETE\n",
2750 da_delete_method_desc[old_method]);
2752 xpt_print(ccb->ccb_h.path,
2753 "%s failed, switching to %s BIO_DELETE\n",
2754 da_delete_method_desc[old_method],
2755 da_delete_method_desc[softc->delete_method]);
2758 while ((bp = bioq_takefirst(&softc->delete_run_queue))
2760 bioq_disksort(&softc->delete_queue, bp);
2762 while ((bp = bioq_takefirst(&softc->delete_run_queue))
2764 bioq_insert_tail(&softc->delete_queue, bp);
2766 bioq_insert_tail(&softc->delete_queue,
2767 (struct bio *)ccb->ccb_h.ccb_bp);
2768 ccb->ccb_h.ccb_bp = NULL;
2772 /* Translation only possible if CDB is an array and cmd is R/W6 */
2773 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
2774 (*cdb != READ_6 && *cdb != WRITE_6))
2777 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
2778 "increasing minimum_cmd_size to 10.\n");
2779 softc->minimum_cmd_size = 10;
2781 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
2782 cmd10 = (struct scsi_rw_10 *)cdb;
2783 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
2785 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
2786 cmd10->reserved = 0;
2787 scsi_ulto2b(cmd6.length, cmd10->length);
2788 cmd10->control = cmd6.control;
2789 ccb->csio.cdb_len = sizeof(*cmd10);
2791 /* Requeue request, unfreezing queue if necessary */
2792 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
2793 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2796 cam_release_devq(ccb->ccb_h.path,
2800 /*getcount_only*/0);
2806 dadone(struct cam_periph *periph, union ccb *done_ccb)
2808 struct da_softc *softc;
2809 struct ccb_scsiio *csio;
2813 softc = (struct da_softc *)periph->softc;
2814 priority = done_ccb->ccb_h.pinfo.priority;
2816 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
2818 csio = &done_ccb->csio;
2819 state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
2821 case DA_CCB_BUFFER_IO:
2824 struct bio *bp, *bp1;
2826 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2827 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2831 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
2836 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
2837 if (error == ERESTART) {
2839 * A retry was scheduled, so
2844 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2849 * return all queued I/O with EIO, so that
2850 * the client can retry these I/Os in the
2851 * proper order should it attempt to recover.
2856 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
2858 * Catastrophic error. Mark our pack as
2862 * XXX See if this is really a media
2865 xpt_print(periph->path,
2866 "Invalidating pack\n");
2867 softc->flags |= DA_FLAG_PACK_INVALID;
2868 queued_error = ENXIO;
2870 bioq_flush(&softc->bio_queue, NULL,
2873 bp->bio_error = error;
2874 bp->bio_resid = bp->bio_bcount;
2875 bp->bio_flags |= BIO_ERROR;
2877 } else if (bp != NULL) {
2878 bp->bio_resid = csio->resid;
2880 if (bp->bio_resid != 0)
2881 bp->bio_flags |= BIO_ERROR;
2883 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2884 cam_release_devq(done_ccb->ccb_h.path,
2888 /*getcount_only*/0);
2889 } else if (bp != NULL) {
2890 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2891 panic("REQ_CMP with QFRZN");
2892 bp->bio_resid = csio->resid;
2893 if (csio->resid > 0)
2894 bp->bio_flags |= BIO_ERROR;
2895 if (softc->error_inject != 0) {
2896 bp->bio_error = softc->error_inject;
2897 bp->bio_resid = bp->bio_bcount;
2898 bp->bio_flags |= BIO_ERROR;
2899 softc->error_inject = 0;
2905 * Block out any asyncronous callbacks
2906 * while we touch the pending ccb list.
2908 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
2909 softc->outstanding_cmds--;
2910 if (softc->outstanding_cmds == 0)
2911 softc->flags |= DA_FLAG_WENT_IDLE;
2913 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
2914 xpt_print(periph->path, "oustanding %d\n",
2915 softc->outstanding_cmds);
2918 if (state == DA_CCB_DELETE) {
2919 while ((bp1 = bioq_takefirst(&softc->delete_run_queue))
2921 bp1->bio_resid = bp->bio_resid;
2922 bp1->bio_error = bp->bio_error;
2923 if (bp->bio_flags & BIO_ERROR)
2924 bp1->bio_flags |= BIO_ERROR;
2927 softc->delete_running = 0;
2931 } else if (bp != NULL)
2935 case DA_CCB_PROBE_RC:
2936 case DA_CCB_PROBE_RC16:
2938 struct scsi_read_capacity_data *rdcap;
2939 struct scsi_read_capacity_data_long *rcaplong;
2940 char announce_buf[80];
2946 if (state == DA_CCB_PROBE_RC)
2947 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
2949 rcaplong = (struct scsi_read_capacity_data_long *)
2952 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
2953 struct disk_params *dp;
2954 uint32_t block_size;
2956 u_int lbppbe; /* LB per physical block exponent. */
2957 u_int lalba; /* Lowest aligned LBA. */
2959 if (state == DA_CCB_PROBE_RC) {
2960 block_size = scsi_4btoul(rdcap->length);
2961 maxsector = scsi_4btoul(rdcap->addr);
2966 * According to SBC-2, if the standard 10
2967 * byte READ CAPACITY command returns 2^32,
2968 * we should issue the 16 byte version of
2969 * the command, since the device in question
2970 * has more sectors than can be represented
2971 * with the short version of the command.
2973 if (maxsector == 0xffffffff) {
2974 free(rdcap, M_SCSIDA);
2975 xpt_release_ccb(done_ccb);
2976 softc->state = DA_STATE_PROBE_RC16;
2977 xpt_schedule(periph, priority);
2981 block_size = scsi_4btoul(rcaplong->length);
2982 maxsector = scsi_8btou64(rcaplong->addr);
2983 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
2984 lalba = scsi_2btoul(rcaplong->lalba_lbp);
2988 * Because GEOM code just will panic us if we
2989 * give them an 'illegal' value we'll avoid that
2992 if (block_size == 0 && maxsector == 0) {
2993 snprintf(announce_buf, sizeof(announce_buf),
2995 } else if (block_size >= MAXPHYS || block_size == 0) {
2996 xpt_print(periph->path,
2997 "unsupportable block size %ju\n",
2998 (uintmax_t) block_size);
2999 announce_buf[0] = '\0';
3000 cam_periph_invalidate(periph);
3003 * We pass rcaplong into dasetgeom(),
3004 * because it will only use it if it is
3007 dasetgeom(periph, block_size, maxsector,
3008 rcaplong, sizeof(*rcaplong));
3009 lbp = (lalba & SRC16_LBPME_A);
3010 dp = &softc->params;
3011 snprintf(announce_buf, sizeof(announce_buf),
3012 "%juMB (%ju %u byte sectors: %dH %dS/T "
3014 (((uintmax_t)dp->secsize *
3015 dp->sectors) / (1024*1024)),
3016 (uintmax_t)dp->sectors,
3017 dp->secsize, dp->heads,
3018 dp->secs_per_track, dp->cylinders);
3023 announce_buf[0] = '\0';
3026 * Retry any UNIT ATTENTION type errors. They
3027 * are expected at boot.
3029 error = daerror(done_ccb, CAM_RETRY_SELTO,
3030 SF_RETRY_UA|SF_NO_PRINT);
3031 if (error == ERESTART) {
3033 * A retry was scheuled, so
3037 } else if (error != 0) {
3039 int sense_key, error_code;
3042 struct ccb_getdev cgd;
3044 /* Don't wedge this device's queue */
3045 status = done_ccb->ccb_h.status;
3046 if ((status & CAM_DEV_QFRZN) != 0)
3047 cam_release_devq(done_ccb->ccb_h.path,
3051 /*getcount_only*/0);
3054 xpt_setup_ccb(&cgd.ccb_h,
3055 done_ccb->ccb_h.path,
3056 CAM_PRIORITY_NORMAL);
3057 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
3058 xpt_action((union ccb *)&cgd);
3060 if (scsi_extract_sense_ccb(done_ccb,
3061 &error_code, &sense_key, &asc, &ascq))
3067 * If we tried READ CAPACITY(16) and failed,
3068 * fallback to READ CAPACITY(10).
3070 if ((state == DA_CCB_PROBE_RC16) &&
3071 (softc->flags & DA_FLAG_CAN_RC16) &&
3072 (((csio->ccb_h.status & CAM_STATUS_MASK) ==
3075 (error_code == SSD_CURRENT_ERROR) &&
3076 (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
3077 softc->flags &= ~DA_FLAG_CAN_RC16;
3078 free(rdcap, M_SCSIDA);
3079 xpt_release_ccb(done_ccb);
3080 softc->state = DA_STATE_PROBE_RC;
3081 xpt_schedule(periph, priority);
3085 * Attach to anything that claims to be a
3086 * direct access or optical disk device,
3087 * as long as it doesn't return a "Logical
3088 * unit not supported" (0x25) error.
3090 if ((have_sense) && (asc != 0x25)
3091 && (error_code == SSD_CURRENT_ERROR)) {
3092 const char *sense_key_desc;
3093 const char *asc_desc;
3095 scsi_sense_desc(sense_key, asc, ascq,
3099 snprintf(announce_buf,
3100 sizeof(announce_buf),
3101 "Attempt to query device "
3102 "size failed: %s, %s",
3110 xpt_print(periph->path,
3111 "got CAM status %#x\n",
3112 done_ccb->ccb_h.status);
3115 xpt_print(periph->path, "fatal error, "
3116 "failed to attach to device\n");
3119 * Free up resources.
3121 cam_periph_invalidate(periph);
3125 free(csio->data_ptr, M_SCSIDA);
3126 if (announce_buf[0] != '\0' && ((softc->flags & DA_FLAG_PROBED) == 0)) {
3128 * Create our sysctl variables, now that we know
3129 * we have successfully attached.
3131 /* increase the refcount */
3132 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3133 taskqueue_enqueue(taskqueue_thread,
3134 &softc->sysctl_task);
3135 xpt_announce_periph(periph, announce_buf);
3136 xpt_announce_quirks(periph, softc->quirks,
3139 xpt_print(periph->path, "fatal error, "
3140 "could not acquire reference count\n");
3144 /* Ensure re-probe doesn't see old delete. */
3145 softc->delete_available = 0;
3148 * Based on older SBC-3 spec revisions
3149 * any of the UNMAP methods "may" be
3150 * available via LBP given this flag so
3151 * we flag all of them as availble and
3152 * then remove those which further
3153 * probes confirm aren't available
3156 * We could also check readcap(16) p_type
3157 * flag to exclude one or more invalid
3158 * write same (X) types here
3160 dadeleteflag(softc, DA_DELETE_WS16, 1);
3161 dadeleteflag(softc, DA_DELETE_WS10, 1);
3162 dadeleteflag(softc, DA_DELETE_ZERO, 1);
3163 dadeleteflag(softc, DA_DELETE_UNMAP, 1);
3165 xpt_release_ccb(done_ccb);
3166 softc->state = DA_STATE_PROBE_LBP;
3167 xpt_schedule(periph, priority);
3171 xpt_release_ccb(done_ccb);
3172 softc->state = DA_STATE_PROBE_BDC;
3173 xpt_schedule(periph, priority);
3176 case DA_CCB_PROBE_LBP:
3178 struct scsi_vpd_logical_block_prov *lbp;
3180 lbp = (struct scsi_vpd_logical_block_prov *)csio->data_ptr;
3182 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3184 * T10/1799-D Revision 31 states at least one of these
3185 * must be supported but we don't currently enforce this.
3187 dadeleteflag(softc, DA_DELETE_WS16,
3188 (lbp->flags & SVPD_LBP_WS16));
3189 dadeleteflag(softc, DA_DELETE_WS10,
3190 (lbp->flags & SVPD_LBP_WS10));
3191 dadeleteflag(softc, DA_DELETE_ZERO,
3192 (lbp->flags & SVPD_LBP_WS10));
3193 dadeleteflag(softc, DA_DELETE_UNMAP,
3194 (lbp->flags & SVPD_LBP_UNMAP));
3196 if (lbp->flags & SVPD_LBP_UNMAP) {
3197 free(lbp, M_SCSIDA);
3198 xpt_release_ccb(done_ccb);
3199 softc->state = DA_STATE_PROBE_BLK_LIMITS;
3200 xpt_schedule(periph, priority);
3205 error = daerror(done_ccb, CAM_RETRY_SELTO,
3206 SF_RETRY_UA|SF_NO_PRINT);
3207 if (error == ERESTART)
3209 else if (error != 0) {
3210 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3211 /* Don't wedge this device's queue */
3212 cam_release_devq(done_ccb->ccb_h.path,
3216 /*getcount_only*/0);
3220 * Failure indicates we don't support any SBC-3
3221 * delete methods with UNMAP
3226 free(lbp, M_SCSIDA);
3227 xpt_release_ccb(done_ccb);
3228 softc->state = DA_STATE_PROBE_BDC;
3229 xpt_schedule(periph, priority);
3232 case DA_CCB_PROBE_BLK_LIMITS:
3234 struct scsi_vpd_block_limits *block_limits;
3236 block_limits = (struct scsi_vpd_block_limits *)csio->data_ptr;
3238 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3239 uint32_t max_unmap_lba_cnt = scsi_4btoul(
3240 block_limits->max_unmap_lba_cnt);
3241 uint32_t max_unmap_blk_cnt = scsi_4btoul(
3242 block_limits->max_unmap_blk_cnt);
3243 uint64_t ws_max_blks = scsi_8btou64(
3244 block_limits->max_write_same_length);
3246 * We should already support UNMAP but we check lba
3247 * and block count to be sure
3249 if (max_unmap_lba_cnt != 0x00L &&
3250 max_unmap_blk_cnt != 0x00L) {
3251 softc->unmap_max_lba = max_unmap_lba_cnt;
3252 softc->unmap_max_ranges = min(max_unmap_blk_cnt,
3256 * Unexpected UNMAP limits which means the
3257 * device doesn't actually support UNMAP
3259 dadeleteflag(softc, DA_DELETE_UNMAP, 0);
3262 if (ws_max_blks != 0x00L)
3263 softc->ws_max_blks = ws_max_blks;
3266 error = daerror(done_ccb, CAM_RETRY_SELTO,
3267 SF_RETRY_UA|SF_NO_PRINT);
3268 if (error == ERESTART)
3270 else if (error != 0) {
3271 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3272 /* Don't wedge this device's queue */
3273 cam_release_devq(done_ccb->ccb_h.path,
3277 /*getcount_only*/0);
3281 * Failure here doesn't mean UNMAP is not
3282 * supported as this is an optional page.
3284 softc->unmap_max_lba = 1;
3285 softc->unmap_max_ranges = 1;
3289 free(block_limits, M_SCSIDA);
3290 xpt_release_ccb(done_ccb);
3291 softc->state = DA_STATE_PROBE_BDC;
3292 xpt_schedule(periph, priority);
3295 case DA_CCB_PROBE_BDC:
3297 struct scsi_vpd_block_characteristics *bdc;
3299 bdc = (struct scsi_vpd_block_characteristics *)csio->data_ptr;
3301 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3303 * Disable queue sorting for non-rotational media
3306 if (scsi_2btoul(bdc->medium_rotation_rate) ==
3307 SVPD_BDC_RATE_NONE_ROTATING)
3308 softc->sort_io_queue = 0;
3311 error = daerror(done_ccb, CAM_RETRY_SELTO,
3312 SF_RETRY_UA|SF_NO_PRINT);
3313 if (error == ERESTART)
3315 else if (error != 0) {
3316 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3317 /* Don't wedge this device's queue */
3318 cam_release_devq(done_ccb->ccb_h.path,
3322 /*getcount_only*/0);
3327 free(bdc, M_SCSIDA);
3328 xpt_release_ccb(done_ccb);
3329 softc->state = DA_STATE_PROBE_ATA;
3330 xpt_schedule(periph, priority);
3333 case DA_CCB_PROBE_ATA:
3336 struct ata_params *ata_params;
3339 ata_params = (struct ata_params *)csio->data_ptr;
3340 ptr = (uint16_t *)ata_params;
3342 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3343 for (i = 0; i < sizeof(*ata_params) / 2; i++)
3344 ptr[i] = le16toh(ptr[i]);
3345 if (ata_params->support_dsm & ATA_SUPPORT_DSM_TRIM) {
3346 dadeleteflag(softc, DA_DELETE_ATA_TRIM, 1);
3347 if (ata_params->max_dsm_blocks != 0)
3348 softc->trim_max_ranges = min(
3349 softc->trim_max_ranges,
3350 ata_params->max_dsm_blocks *
3351 ATA_DSM_BLK_RANGES);
3354 * Disable queue sorting for non-rotational media
3357 if (ata_params->media_rotation_rate == 1)
3358 softc->sort_io_queue = 0;
3361 error = daerror(done_ccb, CAM_RETRY_SELTO,
3362 SF_RETRY_UA|SF_NO_PRINT);
3363 if (error == ERESTART)
3365 else if (error != 0) {
3366 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3367 /* Don't wedge this device's queue */
3368 cam_release_devq(done_ccb->ccb_h.path,
3372 /*getcount_only*/0);
3377 free(ata_params, M_SCSIDA);
3378 daprobedone(periph, done_ccb);
3381 case DA_CCB_WAITING:
3383 /* Caller will release the CCB */
3384 wakeup(&done_ccb->ccb_h.cbfcnp);
3388 /* No-op. We're polling */
3392 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3394 if (daerror(done_ccb, CAM_RETRY_SELTO,
3395 SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
3398 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3399 cam_release_devq(done_ccb->ccb_h.path,
3403 /*getcount_only*/0);
3405 xpt_release_ccb(done_ccb);
3406 cam_periph_release_locked(periph);
3412 xpt_release_ccb(done_ccb);
3416 dareprobe(struct cam_periph *periph)
3418 struct da_softc *softc;
3421 softc = (struct da_softc *)periph->softc;
3423 /* Probe in progress; don't interfere. */
3424 if (softc->state != DA_STATE_NORMAL)
3427 status = cam_periph_acquire(periph);
3428 KASSERT(status == CAM_REQ_CMP,
3429 ("dareprobe: cam_periph_acquire failed"));
3431 if (softc->flags & DA_FLAG_CAN_RC16)
3432 softc->state = DA_STATE_PROBE_RC16;
3434 softc->state = DA_STATE_PROBE_RC;
3436 xpt_schedule(periph, CAM_PRIORITY_DEV);
3440 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3442 struct da_softc *softc;
3443 struct cam_periph *periph;
3444 int error, error_code, sense_key, asc, ascq;
3446 periph = xpt_path_periph(ccb->ccb_h.path);
3447 softc = (struct da_softc *)periph->softc;
3450 * Automatically detect devices that do not support
3451 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
3454 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3455 error = cmd6workaround(ccb);
3456 } else if (scsi_extract_sense_ccb(ccb,
3457 &error_code, &sense_key, &asc, &ascq)) {
3458 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3459 error = cmd6workaround(ccb);
3461 * If the target replied with CAPACITY DATA HAS CHANGED UA,
3462 * query the capacity and notify upper layers.
3464 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3465 asc == 0x2A && ascq == 0x09) {
3466 xpt_print(periph->path, "capacity data has changed\n");
3468 sense_flags |= SF_NO_PRINT;
3469 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3470 asc == 0x28 && ascq == 0x00)
3471 disk_media_changed(softc->disk, M_NOWAIT);
3472 else if (sense_key == SSD_KEY_NOT_READY &&
3473 asc == 0x3a && (softc->flags & DA_FLAG_SAW_MEDIA)) {
3474 softc->flags &= ~DA_FLAG_SAW_MEDIA;
3475 disk_media_gone(softc->disk, M_NOWAIT);
3478 if (error == ERESTART)
3483 * Until we have a better way of doing pack validation,
3484 * don't treat UAs as errors.
3486 sense_flags |= SF_RETRY_UA;
3487 return(cam_periph_error(ccb, cam_flags, sense_flags,
3488 &softc->saved_ccb));
3492 damediapoll(void *arg)
3494 struct cam_periph *periph = arg;
3495 struct da_softc *softc = periph->softc;
3497 if (!softc->tur && softc->outstanding_cmds == 0) {
3498 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3503 /* Queue us up again */
3504 if (da_poll_period != 0)
3505 callout_schedule(&softc->mediapoll_c, da_poll_period * hz);
3509 daprevent(struct cam_periph *periph, int action)
3511 struct da_softc *softc;
3515 softc = (struct da_softc *)periph->softc;
3517 if (((action == PR_ALLOW)
3518 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
3519 || ((action == PR_PREVENT)
3520 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
3524 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3526 scsi_prevent(&ccb->csio,
3534 error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
3535 SF_RETRY_UA | SF_QUIET_IR, softc->disk->d_devstat);
3538 if (action == PR_ALLOW)
3539 softc->flags &= ~DA_FLAG_PACK_LOCKED;
3541 softc->flags |= DA_FLAG_PACK_LOCKED;
3544 xpt_release_ccb(ccb);
3548 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
3549 struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
3551 struct ccb_calc_geometry ccg;
3552 struct da_softc *softc;
3553 struct disk_params *dp;
3554 u_int lbppbe, lalba;
3556 softc = (struct da_softc *)periph->softc;
3558 dp = &softc->params;
3559 dp->secsize = block_len;
3560 dp->sectors = maxsector + 1;
3561 if (rcaplong != NULL) {
3562 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
3563 lalba = scsi_2btoul(rcaplong->lalba_lbp);
3564 lalba &= SRC16_LALBA_A;
3571 dp->stripesize = block_len << lbppbe;
3572 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
3574 } else if (softc->quirks & DA_Q_4K) {
3575 dp->stripesize = 4096;
3576 dp->stripeoffset = 0;
3579 dp->stripeoffset = 0;
3582 * Have the controller provide us with a geometry
3583 * for this disk. The only time the geometry
3584 * matters is when we boot and the controller
3585 * is the only one knowledgeable enough to come
3586 * up with something that will make this a bootable
3589 xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3590 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
3591 ccg.block_size = dp->secsize;
3592 ccg.volume_size = dp->sectors;
3594 ccg.secs_per_track = 0;
3596 xpt_action((union ccb*)&ccg);
3597 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3599 * We don't know what went wrong here- but just pick
3600 * a geometry so we don't have nasty things like divide
3604 dp->secs_per_track = 255;
3605 dp->cylinders = dp->sectors / (255 * 255);
3606 if (dp->cylinders == 0) {
3610 dp->heads = ccg.heads;
3611 dp->secs_per_track = ccg.secs_per_track;
3612 dp->cylinders = ccg.cylinders;
3616 * If the user supplied a read capacity buffer, and if it is
3617 * different than the previous buffer, update the data in the EDT.
3618 * If it's the same, we don't bother. This avoids sending an
3619 * update every time someone opens this device.
3621 if ((rcaplong != NULL)
3622 && (bcmp(rcaplong, &softc->rcaplong,
3623 min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
3624 struct ccb_dev_advinfo cdai;
3626 xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3627 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
3628 cdai.buftype = CDAI_TYPE_RCAPLONG;
3629 cdai.flags |= CDAI_FLAG_STORE;
3630 cdai.bufsiz = rcap_len;
3631 cdai.buf = (uint8_t *)rcaplong;
3632 xpt_action((union ccb *)&cdai);
3633 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
3634 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
3635 if (cdai.ccb_h.status != CAM_REQ_CMP) {
3636 xpt_print(periph->path, "%s: failed to set read "
3637 "capacity advinfo\n", __func__);
3638 /* Use cam_error_print() to decode the status */
3639 cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
3642 bcopy(rcaplong, &softc->rcaplong,
3643 min(sizeof(softc->rcaplong), rcap_len));
3647 softc->disk->d_sectorsize = softc->params.secsize;
3648 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
3649 softc->disk->d_stripesize = softc->params.stripesize;
3650 softc->disk->d_stripeoffset = softc->params.stripeoffset;
3651 /* XXX: these are not actually "firmware" values, so they may be wrong */
3652 softc->disk->d_fwsectors = softc->params.secs_per_track;
3653 softc->disk->d_fwheads = softc->params.heads;
3654 softc->disk->d_devstat->block_size = softc->params.secsize;
3655 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
3659 dasendorderedtag(void *arg)
3661 struct da_softc *softc = arg;
3663 if (da_send_ordered) {
3664 if ((softc->ordered_tag_count == 0)
3665 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
3666 softc->flags |= DA_FLAG_NEED_OTAG;
3668 if (softc->outstanding_cmds > 0)
3669 softc->flags &= ~DA_FLAG_WENT_IDLE;
3671 softc->ordered_tag_count = 0;
3673 /* Queue us up again */
3674 callout_reset(&softc->sendordered_c,
3675 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
3676 dasendorderedtag, softc);
3680 * Step through all DA peripheral drivers, and if the device is still open,
3681 * sync the disk cache to physical media.
3684 dashutdown(void * arg, int howto)
3686 struct cam_periph *periph;
3687 struct da_softc *softc;
3691 CAM_PERIPH_FOREACH(periph, &dadriver) {
3692 softc = (struct da_softc *)periph->softc;
3693 if (SCHEDULER_STOPPED()) {
3694 /* If we paniced with the lock held, do not recurse. */
3695 if (!cam_periph_owned(periph) &&
3696 (softc->flags & DA_FLAG_OPEN)) {
3697 dadump(softc->disk, NULL, 0, 0, 0);
3701 cam_periph_lock(periph);
3704 * We only sync the cache if the drive is still open, and
3705 * if the drive is capable of it..
3707 if (((softc->flags & DA_FLAG_OPEN) == 0)
3708 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
3709 cam_periph_unlock(periph);
3713 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3714 scsi_synchronize_cache(&ccb->csio,
3718 /*begin_lba*/0, /* whole disk */
3723 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
3724 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR,
3725 softc->disk->d_devstat);
3727 xpt_print(periph->path, "Synchronize cache failed\n");
3728 xpt_release_ccb(ccb);
3729 cam_periph_unlock(periph);
3733 #else /* !_KERNEL */
3736 * XXX This is only left out of the kernel build to silence warnings. If,
3737 * for some reason this function is used in the kernel, the ifdefs should
3738 * be moved so it is included both in the kernel and userland.
3741 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
3742 void (*cbfcnp)(struct cam_periph *, union ccb *),
3743 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
3744 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
3747 struct scsi_format_unit *scsi_cmd;
3749 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
3750 scsi_cmd->opcode = FORMAT_UNIT;
3751 scsi_cmd->byte2 = byte2;
3752 scsi_ulto2b(ileave, scsi_cmd->interleave);
3757 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
3766 #endif /* _KERNEL */