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*", "*" },
1035 * Samsung 830 Series SSDs
1036 * 4k optimised & trim only works in 4k requests + 4k aligned
1038 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG SSD 830 Series*", "*" },
1043 * SuperTalent TeraDrive CT SSDs
1044 * 4k optimised & trim only works in 4k requests + 4k aligned
1046 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "FTM??CT25H*", "*" },
1051 * XceedIOPS SATA SSDs
1054 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SG9XCS2D*", "*" },
1059 static disk_strategy_t dastrategy;
1060 static dumper_t dadump;
1061 static periph_init_t dainit;
1062 static void daasync(void *callback_arg, u_int32_t code,
1063 struct cam_path *path, void *arg);
1064 static void dasysctlinit(void *context, int pending);
1065 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
1066 static int dadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
1067 static void dadeletemethodset(struct da_softc *softc,
1068 da_delete_methods delete_method);
1069 static void dadeletemethodchoose(struct da_softc *softc,
1070 da_delete_methods default_method);
1071 static void daprobedone(struct cam_periph *periph, union ccb *ccb);
1073 static periph_ctor_t daregister;
1074 static periph_dtor_t dacleanup;
1075 static periph_start_t dastart;
1076 static periph_oninv_t daoninvalidate;
1077 static void dadone(struct cam_periph *periph,
1078 union ccb *done_ccb);
1079 static int daerror(union ccb *ccb, u_int32_t cam_flags,
1080 u_int32_t sense_flags);
1081 static void daprevent(struct cam_periph *periph, int action);
1082 static void dareprobe(struct cam_periph *periph);
1083 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
1085 struct scsi_read_capacity_data_long *rcaplong,
1087 static timeout_t dasendorderedtag;
1088 static void dashutdown(void *arg, int howto);
1089 static timeout_t damediapoll;
1091 #ifndef DA_DEFAULT_POLL_PERIOD
1092 #define DA_DEFAULT_POLL_PERIOD 3
1095 #ifndef DA_DEFAULT_TIMEOUT
1096 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
1099 #ifndef DA_DEFAULT_RETRY
1100 #define DA_DEFAULT_RETRY 4
1103 #ifndef DA_DEFAULT_SEND_ORDERED
1104 #define DA_DEFAULT_SEND_ORDERED 1
1107 #define DA_SIO (softc->sort_io_queue >= 0 ? \
1108 softc->sort_io_queue : cam_sort_io_queues)
1110 static int da_poll_period = DA_DEFAULT_POLL_PERIOD;
1111 static int da_retry_count = DA_DEFAULT_RETRY;
1112 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
1113 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
1115 static SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
1116 "CAM Direct Access Disk driver");
1117 SYSCTL_INT(_kern_cam_da, OID_AUTO, poll_period, CTLFLAG_RW,
1118 &da_poll_period, 0, "Media polling period in seconds");
1119 TUNABLE_INT("kern.cam.da.poll_period", &da_poll_period);
1120 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
1121 &da_retry_count, 0, "Normal I/O retry count");
1122 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
1123 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
1124 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
1125 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
1126 SYSCTL_INT(_kern_cam_da, OID_AUTO, send_ordered, CTLFLAG_RW,
1127 &da_send_ordered, 0, "Send Ordered Tags");
1128 TUNABLE_INT("kern.cam.da.send_ordered", &da_send_ordered);
1131 * DA_ORDEREDTAG_INTERVAL determines how often, relative
1132 * to the default timeout, we check to see whether an ordered
1133 * tagged transaction is appropriate to prevent simple tag
1134 * starvation. Since we'd like to ensure that there is at least
1135 * 1/2 of the timeout length left for a starved transaction to
1136 * complete after we've sent an ordered tag, we must poll at least
1137 * four times in every timeout period. This takes care of the worst
1138 * case where a starved transaction starts during an interval that
1139 * meets the requirement "don't send an ordered tag" test so it takes
1140 * us two intervals to determine that a tag must be sent.
1142 #ifndef DA_ORDEREDTAG_INTERVAL
1143 #define DA_ORDEREDTAG_INTERVAL 4
1146 static struct periph_driver dadriver =
1149 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
1152 PERIPHDRIVER_DECLARE(da, dadriver);
1154 static MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
1157 daopen(struct disk *dp)
1159 struct cam_periph *periph;
1160 struct da_softc *softc;
1164 periph = (struct cam_periph *)dp->d_drv1;
1165 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1169 cam_periph_lock(periph);
1170 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
1171 cam_periph_unlock(periph);
1172 cam_periph_release(periph);
1176 unit = periph->unit_number;
1177 softc = (struct da_softc *)periph->softc;
1178 softc->flags |= DA_FLAG_OPEN;
1180 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1183 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1184 /* Invalidate our pack information. */
1185 softc->flags &= ~DA_FLAG_PACK_INVALID;
1190 /* Wait for the disk size update. */
1191 error = cam_periph_sleep(periph, &softc->disk->d_mediasize, PRIBIO,
1194 xpt_print(periph->path, "unable to retrieve capacity data");
1196 if (periph->flags & CAM_PERIPH_INVALID ||
1197 softc->disk->d_sectorsize == 0 ||
1198 softc->disk->d_mediasize == 0)
1201 if (error == 0 && (softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1202 (softc->quirks & DA_Q_NO_PREVENT) == 0)
1203 daprevent(periph, PR_PREVENT);
1206 softc->flags |= DA_FLAG_SAW_MEDIA;
1208 cam_periph_unhold(periph);
1209 cam_periph_unlock(periph);
1212 softc->flags &= ~DA_FLAG_OPEN;
1213 cam_periph_release(periph);
1220 daclose(struct disk *dp)
1222 struct cam_periph *periph;
1223 struct da_softc *softc;
1225 periph = (struct cam_periph *)dp->d_drv1;
1226 cam_periph_lock(periph);
1227 if (cam_periph_hold(periph, PRIBIO) != 0) {
1228 cam_periph_unlock(periph);
1229 cam_periph_release(periph);
1233 softc = (struct da_softc *)periph->softc;
1235 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1238 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0
1239 && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
1242 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1244 scsi_synchronize_cache(&ccb->csio,
1248 /*begin_lba*/0,/* Cover the whole disk */
1253 cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
1254 /*sense_flags*/SF_RETRY_UA | SF_QUIET_IR,
1255 softc->disk->d_devstat);
1256 xpt_release_ccb(ccb);
1260 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
1261 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
1262 daprevent(periph, PR_ALLOW);
1264 * If we've got removeable media, mark the blocksize as
1265 * unavailable, since it could change when new media is
1268 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1271 softc->flags &= ~DA_FLAG_OPEN;
1272 cam_periph_unhold(periph);
1273 cam_periph_unlock(periph);
1274 cam_periph_release(periph);
1279 daschedule(struct cam_periph *periph)
1281 struct da_softc *softc = (struct da_softc *)periph->softc;
1284 if (softc->state != DA_STATE_NORMAL)
1287 /* Check if cam_periph_getccb() was called. */
1288 prio = periph->immediate_priority;
1290 /* Check if we have more work to do. */
1291 if (bioq_first(&softc->bio_queue) ||
1292 (!softc->delete_running && bioq_first(&softc->delete_queue)) ||
1294 prio = CAM_PRIORITY_NORMAL;
1297 /* Schedule CCB if any of above is true. */
1298 if (prio != CAM_PRIORITY_NONE)
1299 xpt_schedule(periph, prio);
1303 * Actually translate the requested transfer into one the physical driver
1304 * can understand. The transfer is described by a buf and will include
1305 * only one physical transfer.
1308 dastrategy(struct bio *bp)
1310 struct cam_periph *periph;
1311 struct da_softc *softc;
1313 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1314 softc = (struct da_softc *)periph->softc;
1316 cam_periph_lock(periph);
1319 * If the device has been made invalid, error out
1321 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1322 cam_periph_unlock(periph);
1323 biofinish(bp, NULL, ENXIO);
1327 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
1330 * Place it in the queue of disk activities for this disk
1332 if (bp->bio_cmd == BIO_DELETE) {
1333 if (bp->bio_bcount == 0)
1336 bioq_disksort(&softc->delete_queue, bp);
1338 bioq_insert_tail(&softc->delete_queue, bp);
1339 } else if (DA_SIO) {
1340 bioq_disksort(&softc->bio_queue, bp);
1342 bioq_insert_tail(&softc->bio_queue, bp);
1346 * Schedule ourselves for performing the work.
1349 cam_periph_unlock(periph);
1355 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1357 struct cam_periph *periph;
1358 struct da_softc *softc;
1360 struct ccb_scsiio csio;
1365 periph = dp->d_drv1;
1366 softc = (struct da_softc *)periph->softc;
1367 cam_periph_lock(periph);
1368 secsize = softc->params.secsize;
1370 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1371 cam_periph_unlock(periph);
1376 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1377 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1378 scsi_read_write(&csio,
1382 /*read*/SCSI_RW_WRITE,
1384 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1387 /*data_ptr*/(u_int8_t *) virtual,
1388 /*dxfer_len*/length,
1389 /*sense_len*/SSD_FULL_SIZE,
1390 da_default_timeout * 1000);
1391 xpt_polled_action((union ccb *)&csio);
1393 error = cam_periph_error((union ccb *)&csio,
1394 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1395 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1396 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1397 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1399 printf("Aborting dump due to I/O error.\n");
1400 cam_periph_unlock(periph);
1405 * Sync the disk cache contents to the physical media.
1407 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1409 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1410 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1411 scsi_synchronize_cache(&csio,
1415 /*begin_lba*/0,/* Cover the whole disk */
1419 xpt_polled_action((union ccb *)&csio);
1421 error = cam_periph_error((union ccb *)&csio,
1422 0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL);
1423 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1424 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1425 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1427 xpt_print(periph->path, "Synchronize cache failed\n");
1429 cam_periph_unlock(periph);
1434 dagetattr(struct bio *bp)
1437 struct cam_periph *periph;
1439 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1440 cam_periph_lock(periph);
1441 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1443 cam_periph_unlock(periph);
1445 bp->bio_completed = bp->bio_length;
1455 * Install a global async callback. This callback will
1456 * receive async callbacks like "new device found".
1458 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1460 if (status != CAM_REQ_CMP) {
1461 printf("da: Failed to attach master async callback "
1462 "due to status 0x%x!\n", status);
1463 } else if (da_send_ordered) {
1465 /* Register our shutdown event handler */
1466 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1467 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1468 printf("dainit: shutdown event registration failed!\n");
1473 * Callback from GEOM, called when it has finished cleaning up its
1477 dadiskgonecb(struct disk *dp)
1479 struct cam_periph *periph;
1481 periph = (struct cam_periph *)dp->d_drv1;
1482 cam_periph_release(periph);
1486 daoninvalidate(struct cam_periph *periph)
1488 struct da_softc *softc;
1490 softc = (struct da_softc *)periph->softc;
1493 * De-register any async callbacks.
1495 xpt_register_async(0, daasync, periph, periph->path);
1497 softc->flags |= DA_FLAG_PACK_INVALID;
1500 * Return all queued I/O with ENXIO.
1501 * XXX Handle any transactions queued to the card
1502 * with XPT_ABORT_CCB.
1504 bioq_flush(&softc->bio_queue, NULL, ENXIO);
1505 bioq_flush(&softc->delete_queue, NULL, ENXIO);
1508 * Tell GEOM that we've gone away, we'll get a callback when it is
1509 * done cleaning up its resources.
1511 disk_gone(softc->disk);
1513 xpt_print(periph->path, "lost device - %d outstanding, %d refs\n",
1514 softc->outstanding_cmds, periph->refcount);
1518 dacleanup(struct cam_periph *periph)
1520 struct da_softc *softc;
1522 softc = (struct da_softc *)periph->softc;
1524 xpt_print(periph->path, "removing device entry\n");
1525 cam_periph_unlock(periph);
1528 * If we can't free the sysctl tree, oh well...
1530 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1531 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1532 xpt_print(periph->path, "can't remove sysctl context\n");
1535 callout_drain(&softc->mediapoll_c);
1536 disk_destroy(softc->disk);
1537 callout_drain(&softc->sendordered_c);
1538 free(softc, M_DEVBUF);
1539 cam_periph_lock(periph);
1543 daasync(void *callback_arg, u_int32_t code,
1544 struct cam_path *path, void *arg)
1546 struct cam_periph *periph;
1547 struct da_softc *softc;
1549 periph = (struct cam_periph *)callback_arg;
1551 case AC_FOUND_DEVICE:
1553 struct ccb_getdev *cgd;
1556 cgd = (struct ccb_getdev *)arg;
1560 if (cgd->protocol != PROTO_SCSI)
1563 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1564 && SID_TYPE(&cgd->inq_data) != T_RBC
1565 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1569 * Allocate a peripheral instance for
1570 * this device and start the probe
1573 status = cam_periph_alloc(daregister, daoninvalidate,
1575 "da", CAM_PERIPH_BIO,
1576 cgd->ccb_h.path, daasync,
1577 AC_FOUND_DEVICE, cgd);
1579 if (status != CAM_REQ_CMP
1580 && status != CAM_REQ_INPROG)
1581 printf("daasync: Unable to attach to new device "
1582 "due to status 0x%x\n", status);
1585 case AC_ADVINFO_CHANGED:
1589 buftype = (uintptr_t)arg;
1590 if (buftype == CDAI_TYPE_PHYS_PATH) {
1591 struct da_softc *softc;
1593 softc = periph->softc;
1594 disk_attr_changed(softc->disk, "GEOM::physpath",
1599 case AC_UNIT_ATTENTION:
1602 int error_code, sense_key, asc, ascq;
1604 softc = (struct da_softc *)periph->softc;
1605 ccb = (union ccb *)arg;
1608 * Handle all UNIT ATTENTIONs except our own,
1609 * as they will be handled by daerror().
1611 if (xpt_path_periph(ccb->ccb_h.path) != periph &&
1612 scsi_extract_sense_ccb(ccb,
1613 &error_code, &sense_key, &asc, &ascq)) {
1614 if (asc == 0x2A && ascq == 0x09) {
1615 xpt_print(ccb->ccb_h.path,
1616 "capacity data has changed\n");
1618 } else if (asc == 0x28 && ascq == 0x00)
1619 disk_media_changed(softc->disk, M_NOWAIT);
1621 cam_periph_async(periph, code, path, arg);
1625 softc = (struct da_softc *)periph->softc;
1627 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
1636 struct ccb_hdr *ccbh;
1638 softc = (struct da_softc *)periph->softc;
1640 * Don't fail on the expected unit attention
1643 softc->flags |= DA_FLAG_RETRY_UA;
1644 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1645 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1651 cam_periph_async(periph, code, path, arg);
1655 dasysctlinit(void *context, int pending)
1657 struct cam_periph *periph;
1658 struct da_softc *softc;
1659 char tmpstr[80], tmpstr2[80];
1660 struct ccb_trans_settings cts;
1662 periph = (struct cam_periph *)context;
1664 * periph was held for us when this task was enqueued
1666 if (periph->flags & CAM_PERIPH_INVALID) {
1667 cam_periph_release(periph);
1671 softc = (struct da_softc *)periph->softc;
1672 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1673 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1675 sysctl_ctx_init(&softc->sysctl_ctx);
1676 softc->flags |= DA_FLAG_SCTX_INIT;
1677 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1678 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1679 CTLFLAG_RD, 0, tmpstr);
1680 if (softc->sysctl_tree == NULL) {
1681 printf("dasysctlinit: unable to allocate sysctl tree\n");
1682 cam_periph_release(periph);
1687 * Now register the sysctl handler, so the user can change the value on
1690 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1691 OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RW,
1692 softc, 0, dadeletemethodsysctl, "A",
1693 "BIO_DELETE execution method");
1694 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1695 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1696 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1697 "Minimum CDB size");
1698 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1699 OID_AUTO, "sort_io_queue", CTLFLAG_RW, &softc->sort_io_queue, 0,
1700 "Sort IO queue to try and optimise disk access patterns");
1702 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1703 SYSCTL_CHILDREN(softc->sysctl_tree),
1707 &softc->error_inject,
1709 "error_inject leaf");
1713 * Add some addressing info.
1715 memset(&cts, 0, sizeof (cts));
1716 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1717 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1718 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1719 cam_periph_lock(periph);
1720 xpt_action((union ccb *)&cts);
1721 cam_periph_unlock(periph);
1722 if (cts.ccb_h.status != CAM_REQ_CMP) {
1723 cam_periph_release(periph);
1726 if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
1727 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1728 if (fc->valid & CTS_FC_VALID_WWPN) {
1729 softc->wwpn = fc->wwpn;
1730 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1731 SYSCTL_CHILDREN(softc->sysctl_tree),
1732 OID_AUTO, "wwpn", CTLFLAG_RD,
1733 &softc->wwpn, "World Wide Port Name");
1736 cam_periph_release(periph);
1740 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1744 value = *(int *)arg1;
1746 error = sysctl_handle_int(oidp, &value, 0, req);
1749 || (req->newptr == NULL))
1753 * Acceptable values here are 6, 10, 12 or 16.
1757 else if ((value > 6)
1760 else if ((value > 10)
1763 else if (value > 12)
1766 *(int *)arg1 = value;
1772 dadeletemethodset(struct da_softc *softc, da_delete_methods delete_method)
1776 softc->delete_method = delete_method;
1778 if (softc->delete_method > DA_DELETE_DISABLE)
1779 softc->disk->d_flags |= DISKFLAG_CANDELETE;
1781 softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
1785 daprobedone(struct cam_periph *periph, union ccb *ccb)
1787 struct da_softc *softc;
1789 softc = (struct da_softc *)periph->softc;
1791 dadeletemethodchoose(softc, DA_DELETE_NONE);
1793 if (bootverbose && (softc->flags & DA_FLAG_PROBED) == 0) {
1797 snprintf(buf, sizeof(buf), "Delete methods: <");
1799 for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
1800 if (softc->delete_available & (1 << i)) {
1802 strlcat(buf, ",", sizeof(buf));
1806 strlcat(buf, da_delete_method_names[i],
1808 if (i == softc->delete_method) {
1809 strlcat(buf, "(*)", sizeof(buf));
1814 if (softc->delete_method == DA_DELETE_NONE)
1815 strlcat(buf, "NONE(*)", sizeof(buf));
1817 strlcat(buf, "DISABLED(*)", sizeof(buf));
1819 strlcat(buf, ">", sizeof(buf));
1820 printf("%s%d: %s\n", periph->periph_name,
1821 periph->unit_number, buf);
1825 * Since our peripheral may be invalidated by an error
1826 * above or an external event, we must release our CCB
1827 * before releasing the probe lock on the peripheral.
1828 * The peripheral will only go away once the last lock
1829 * is removed, and we need it around for the CCB release
1832 xpt_release_ccb(ccb);
1833 softc->state = DA_STATE_NORMAL;
1835 wakeup(&softc->disk->d_mediasize);
1836 if ((softc->flags & DA_FLAG_PROBED) == 0) {
1837 softc->flags |= DA_FLAG_PROBED;
1838 cam_periph_unhold(periph);
1840 cam_periph_release_locked(periph);
1844 dadeletemethodchoose(struct da_softc *softc, da_delete_methods default_method)
1846 int i, delete_method;
1848 delete_method = default_method;
1851 * Use the pre-defined order to choose the best
1852 * performing delete.
1854 for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
1855 if (softc->delete_available & (1 << i)) {
1856 dadeletemethodset(softc, i);
1860 dadeletemethodset(softc, delete_method);
1864 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
1868 struct da_softc *softc;
1869 int i, error, value;
1871 softc = (struct da_softc *)arg1;
1873 value = softc->delete_method;
1874 if (value < 0 || value > DA_DELETE_MAX)
1877 p = da_delete_method_names[value];
1878 strncpy(buf, p, sizeof(buf));
1879 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1880 if (error != 0 || req->newptr == NULL)
1882 for (i = 0; i <= DA_DELETE_MAX; i++) {
1883 if (!(softc->delete_available & (1 << i)) ||
1884 strcmp(buf, da_delete_method_names[i]) != 0)
1886 dadeletemethodset(softc, i);
1893 daregister(struct cam_periph *periph, void *arg)
1895 struct da_softc *softc;
1896 struct ccb_pathinq cpi;
1897 struct ccb_getdev *cgd;
1901 cgd = (struct ccb_getdev *)arg;
1903 printf("daregister: no getdev CCB, can't register device\n");
1904 return(CAM_REQ_CMP_ERR);
1907 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
1910 if (softc == NULL) {
1911 printf("daregister: Unable to probe new device. "
1912 "Unable to allocate softc\n");
1913 return(CAM_REQ_CMP_ERR);
1916 LIST_INIT(&softc->pending_ccbs);
1917 softc->state = DA_STATE_PROBE_RC;
1918 bioq_init(&softc->bio_queue);
1919 bioq_init(&softc->delete_queue);
1920 bioq_init(&softc->delete_run_queue);
1921 if (SID_IS_REMOVABLE(&cgd->inq_data))
1922 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1923 softc->unmap_max_ranges = UNMAP_MAX_RANGES;
1924 softc->unmap_max_lba = UNMAP_RANGE_MAX;
1925 softc->ws_max_blks = WS16_MAX_BLKS;
1926 softc->trim_max_ranges = ATA_TRIM_MAX_RANGES;
1927 softc->sort_io_queue = -1;
1929 periph->softc = softc;
1932 * See if this device has any quirks.
1934 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1935 (caddr_t)da_quirk_table,
1936 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1937 sizeof(*da_quirk_table), scsi_inquiry_match);
1940 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1942 softc->quirks = DA_Q_NONE;
1944 /* Check if the SIM does not want 6 byte commands */
1945 bzero(&cpi, sizeof(cpi));
1946 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1947 cpi.ccb_h.func_code = XPT_PATH_INQ;
1948 xpt_action((union ccb *)&cpi);
1949 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1950 softc->quirks |= DA_Q_NO_6_BYTE;
1952 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1955 * Take an exclusive refcount on the periph while dastart is called
1956 * to finish the probe. The reference will be dropped in dadone at
1959 (void)cam_periph_hold(periph, PRIBIO);
1962 * Schedule a periodic event to occasionally send an
1963 * ordered tag to a device.
1965 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
1966 callout_reset(&softc->sendordered_c,
1967 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
1968 dasendorderedtag, softc);
1970 cam_periph_unlock(periph);
1972 * RBC devices don't have to support READ(6), only READ(10).
1974 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1975 softc->minimum_cmd_size = 10;
1977 softc->minimum_cmd_size = 6;
1980 * Load the user's default, if any.
1982 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1983 periph->unit_number);
1984 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1987 * 6, 10, 12 and 16 are the currently permissible values.
1989 if (softc->minimum_cmd_size < 6)
1990 softc->minimum_cmd_size = 6;
1991 else if ((softc->minimum_cmd_size > 6)
1992 && (softc->minimum_cmd_size <= 10))
1993 softc->minimum_cmd_size = 10;
1994 else if ((softc->minimum_cmd_size > 10)
1995 && (softc->minimum_cmd_size <= 12))
1996 softc->minimum_cmd_size = 12;
1997 else if (softc->minimum_cmd_size > 12)
1998 softc->minimum_cmd_size = 16;
2000 /* Predict whether device may support READ CAPACITY(16). */
2001 if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3) {
2002 softc->flags |= DA_FLAG_CAN_RC16;
2003 softc->state = DA_STATE_PROBE_RC16;
2007 * Register this media as a disk.
2009 softc->disk = disk_alloc();
2010 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
2011 periph->unit_number, 0,
2012 DEVSTAT_BS_UNAVAILABLE,
2013 SID_TYPE(&cgd->inq_data) |
2014 XPORT_DEVSTAT_TYPE(cpi.transport),
2015 DEVSTAT_PRIORITY_DISK);
2016 softc->disk->d_open = daopen;
2017 softc->disk->d_close = daclose;
2018 softc->disk->d_strategy = dastrategy;
2019 softc->disk->d_dump = dadump;
2020 softc->disk->d_getattr = dagetattr;
2021 softc->disk->d_gone = dadiskgonecb;
2022 softc->disk->d_name = "da";
2023 softc->disk->d_drv1 = periph;
2025 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */
2026 else if (cpi.maxio > MAXPHYS)
2027 softc->disk->d_maxsize = MAXPHYS; /* for safety */
2029 softc->disk->d_maxsize = cpi.maxio;
2030 softc->disk->d_unit = periph->unit_number;
2031 softc->disk->d_flags = 0;
2032 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
2033 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
2034 if ((cpi.hba_misc & PIM_UNMAPPED) != 0)
2035 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
2036 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
2037 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
2038 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
2039 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
2040 cgd->inq_data.product, sizeof(cgd->inq_data.product),
2041 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
2042 softc->disk->d_hba_vendor = cpi.hba_vendor;
2043 softc->disk->d_hba_device = cpi.hba_device;
2044 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
2045 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
2048 * Acquire a reference to the periph before we register with GEOM.
2049 * We'll release this reference once GEOM calls us back (via
2050 * dadiskgonecb()) telling us that our provider has been freed.
2052 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
2053 xpt_print(periph->path, "%s: lost periph during "
2054 "registration!\n", __func__);
2055 cam_periph_lock(periph);
2056 return (CAM_REQ_CMP_ERR);
2059 disk_create(softc->disk, DISK_VERSION);
2060 cam_periph_lock(periph);
2063 * Add async callbacks for events of interest.
2064 * I don't bother checking if this fails as,
2065 * in most cases, the system will function just
2066 * fine without them and the only alternative
2067 * would be to not attach the device on failure.
2069 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
2070 AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION,
2071 daasync, periph, periph->path);
2074 * Emit an attribute changed notification just in case
2075 * physical path information arrived before our async
2076 * event handler was registered, but after anyone attaching
2077 * to our disk device polled it.
2079 disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
2082 * Schedule a periodic media polling events.
2084 callout_init_mtx(&softc->mediapoll_c, periph->sim->mtx, 0);
2085 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
2086 (cgd->inq_flags & SID_AEN) == 0 &&
2087 da_poll_period != 0)
2088 callout_reset(&softc->mediapoll_c, da_poll_period * hz,
2089 damediapoll, periph);
2091 xpt_schedule(periph, CAM_PRIORITY_DEV);
2093 return(CAM_REQ_CMP);
2097 dastart(struct cam_periph *periph, union ccb *start_ccb)
2099 struct da_softc *softc;
2101 softc = (struct da_softc *)periph->softc;
2103 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
2106 switch (softc->state) {
2107 case DA_STATE_NORMAL:
2109 struct bio *bp, *bp1;
2112 /* Execute immediate CCB if waiting. */
2113 if (periph->immediate_priority <= periph->pinfo.priority) {
2114 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2115 ("queuing for immediate ccb\n"));
2116 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
2117 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
2119 periph->immediate_priority = CAM_PRIORITY_NONE;
2120 wakeup(&periph->ccb_list);
2121 /* May have more work to do, so ensure we stay scheduled */
2126 /* Run BIO_DELETE if not running yet. */
2127 if (!softc->delete_running &&
2128 (bp = bioq_first(&softc->delete_queue)) != NULL) {
2130 uint64_t count; /* forward compat with WS32 */
2133 * In each of the methods below, while its the caller's
2134 * responsibility to ensure the request will fit into a
2135 * single device request, we might have changed the delete
2136 * method due to the device incorrectly advertising either
2137 * its supported methods or limits.
2139 * To prevent this causing further issues we validate the
2140 * against the methods limits, and warn which would
2141 * otherwise be unnecessary.
2144 if (softc->delete_method == DA_DELETE_UNMAP) {
2145 uint8_t *buf = softc->unmap_buf;
2146 uint64_t lastlba = (uint64_t)-1;
2147 uint32_t lastcount = 0, c;
2148 uint64_t totalcount = 0;
2149 uint32_t off, ranges = 0;
2152 * Currently this doesn't take the UNMAP
2153 * Granularity and Granularity Alignment
2154 * fields into account.
2156 * This could result in both unoptimal unmap
2157 * requests as as well as UNMAP calls unmapping
2158 * fewer LBA's than requested.
2161 softc->delete_running = 1;
2162 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2165 bioq_remove(&softc->delete_queue, bp1);
2167 bioq_insert_tail(&softc->delete_run_queue, bp1);
2168 lba = bp1->bio_pblkno;
2169 count = bp1->bio_bcount / softc->params.secsize;
2171 /* Try to extend the previous range. */
2172 if (lba == lastlba) {
2173 c = min(count, softc->unmap_max_lba -
2176 off = ((ranges - 1) * UNMAP_RANGE_SIZE) +
2178 scsi_ulto4b(lastcount, &buf[off + 8]);
2185 c = min(count, softc->unmap_max_lba);
2186 if (totalcount + c > softc->unmap_max_lba ||
2187 ranges >= softc->unmap_max_ranges) {
2188 xpt_print(periph->path,
2189 "%s issuing short delete %ld > %ld"
2191 da_delete_method_desc[softc->delete_method],
2192 totalcount + c, softc->unmap_max_lba,
2193 ranges, softc->unmap_max_ranges);
2196 off = (ranges * UNMAP_RANGE_SIZE) +
2198 scsi_u64to8b(lba, &buf[off + 0]);
2199 scsi_ulto4b(c, &buf[off + 8]);
2207 bp1 = bioq_first(&softc->delete_queue);
2209 ranges >= softc->unmap_max_ranges ||
2210 totalcount + bp1->bio_bcount /
2211 softc->params.secsize > softc->unmap_max_lba)
2214 scsi_ulto2b(ranges * 16 + 6, &buf[0]);
2215 scsi_ulto2b(ranges * 16, &buf[2]);
2217 scsi_unmap(&start_ccb->csio,
2218 /*retries*/da_retry_count,
2220 /*tag_action*/MSG_SIMPLE_Q_TAG,
2223 /*dxfer_len*/ ranges * 16 + 8,
2224 /*sense_len*/SSD_FULL_SIZE,
2225 da_default_timeout * 1000);
2226 start_ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2228 } else if (softc->delete_method == DA_DELETE_ATA_TRIM) {
2229 uint8_t *buf = softc->unmap_buf;
2230 uint64_t lastlba = (uint64_t)-1;
2231 uint32_t lastcount = 0, c, requestcount;
2232 int ranges = 0, off, block_count;
2234 softc->delete_running = 1;
2235 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2238 bioq_remove(&softc->delete_queue, bp1);
2240 bioq_insert_tail(&softc->delete_run_queue, bp1);
2241 lba = bp1->bio_pblkno;
2242 count = bp1->bio_bcount / softc->params.secsize;
2243 requestcount = count;
2245 /* Try to extend the previous range. */
2246 if (lba == lastlba) {
2247 c = min(count, ATA_DSM_RANGE_MAX - lastcount);
2249 off = (ranges - 1) * 8;
2250 buf[off + 6] = lastcount & 0xff;
2251 buf[off + 7] = (lastcount >> 8) & 0xff;
2257 c = min(count, ATA_DSM_RANGE_MAX);
2260 buf[off + 0] = lba & 0xff;
2261 buf[off + 1] = (lba >> 8) & 0xff;
2262 buf[off + 2] = (lba >> 16) & 0xff;
2263 buf[off + 3] = (lba >> 24) & 0xff;
2264 buf[off + 4] = (lba >> 32) & 0xff;
2265 buf[off + 5] = (lba >> 40) & 0xff;
2266 buf[off + 6] = c & 0xff;
2267 buf[off + 7] = (c >> 8) & 0xff;
2272 if (count != 0 && ranges == softc->trim_max_ranges) {
2273 xpt_print(periph->path,
2274 "%s issuing short delete %ld > %ld",
2275 da_delete_method_desc[softc->delete_method],
2277 (softc->trim_max_ranges - ranges) *
2283 bp1 = bioq_first(&softc->delete_queue);
2285 bp1->bio_bcount / softc->params.secsize >
2286 (softc->trim_max_ranges - ranges) *
2291 block_count = (ranges + ATA_DSM_BLK_RANGES - 1) /
2293 scsi_ata_trim(&start_ccb->csio,
2294 /*retries*/da_retry_count,
2296 /*tag_action*/MSG_SIMPLE_Q_TAG,
2299 /*dxfer_len*/block_count * ATA_DSM_BLK_SIZE,
2300 /*sense_len*/SSD_FULL_SIZE,
2301 da_default_timeout * 1000);
2302 start_ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2304 } else if (softc->delete_method == DA_DELETE_ZERO ||
2305 softc->delete_method == DA_DELETE_WS10 ||
2306 softc->delete_method == DA_DELETE_WS16) {
2307 uint64_t ws_max_blks;
2308 ws_max_blks = softc->ws_max_blks / softc->params.secsize;
2309 softc->delete_running = 1;
2310 lba = bp->bio_pblkno;
2314 bioq_remove(&softc->delete_queue, bp1);
2316 bioq_insert_tail(&softc->delete_run_queue, bp1);
2317 count += bp1->bio_bcount / softc->params.secsize;
2318 if (count > ws_max_blks) {
2319 count = min(count, ws_max_blks);
2320 xpt_print(periph->path,
2321 "%s issuing short delete %ld > %ld",
2322 da_delete_method_desc[softc->delete_method],
2323 count, ws_max_blks);
2326 bp1 = bioq_first(&softc->delete_queue);
2328 lba + count != bp1->bio_pblkno ||
2329 count + bp1->bio_bcount /
2330 softc->params.secsize > ws_max_blks)
2334 scsi_write_same(&start_ccb->csio,
2335 /*retries*/da_retry_count,
2337 /*tag_action*/MSG_SIMPLE_Q_TAG,
2338 /*byte2*/softc->delete_method ==
2339 DA_DELETE_ZERO ? 0 : SWS_UNMAP,
2340 softc->delete_method ==
2341 DA_DELETE_WS16 ? 16 : 10,
2343 /*block_count*/count,
2344 /*data_ptr*/ __DECONST(void *,
2346 /*dxfer_len*/ softc->params.secsize,
2347 /*sense_len*/SSD_FULL_SIZE,
2348 da_default_timeout * 1000);
2349 start_ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2352 bioq_flush(&softc->delete_queue, NULL, 0);
2357 /* Run regular command. */
2358 bp = bioq_takefirst(&softc->bio_queue);
2362 scsi_test_unit_ready(&start_ccb->csio,
2363 /*retries*/ da_retry_count,
2367 da_default_timeout * 1000);
2368 start_ccb->ccb_h.ccb_bp = NULL;
2369 start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
2370 xpt_action(start_ccb);
2372 xpt_release_ccb(start_ccb);
2377 cam_periph_release_locked(periph);
2380 if ((bp->bio_flags & BIO_ORDERED) != 0 ||
2381 (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
2382 softc->flags &= ~DA_FLAG_NEED_OTAG;
2383 softc->ordered_tag_count++;
2384 tag_code = MSG_ORDERED_Q_TAG;
2386 tag_code = MSG_SIMPLE_Q_TAG;
2389 switch (bp->bio_cmd) {
2392 scsi_read_write(&start_ccb->csio,
2393 /*retries*/da_retry_count,
2395 /*tag_action*/tag_code,
2396 /*read_op*/(bp->bio_cmd == BIO_READ ?
2397 SCSI_RW_READ : SCSI_RW_WRITE) |
2398 ((bp->bio_flags & BIO_UNMAPPED) != 0 ?
2401 softc->minimum_cmd_size,
2402 /*lba*/bp->bio_pblkno,
2403 /*block_count*/bp->bio_bcount /
2404 softc->params.secsize,
2405 /*data_ptr*/ (bp->bio_flags &
2406 BIO_UNMAPPED) != 0 ? (void *)bp :
2408 /*dxfer_len*/ bp->bio_bcount,
2409 /*sense_len*/SSD_FULL_SIZE,
2410 da_default_timeout * 1000);
2414 * BIO_FLUSH doesn't currently communicate
2415 * range data, so we synchronize the cache
2416 * over the whole disk. We also force
2417 * ordered tag semantics the flush applies
2418 * to all previously queued I/O.
2420 scsi_synchronize_cache(&start_ccb->csio,
2427 da_default_timeout*1000);
2430 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
2434 * Block out any asyncronous callbacks
2435 * while we touch the pending ccb list.
2437 LIST_INSERT_HEAD(&softc->pending_ccbs,
2438 &start_ccb->ccb_h, periph_links.le);
2439 softc->outstanding_cmds++;
2441 /* We expect a unit attention from this device */
2442 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
2443 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
2444 softc->flags &= ~DA_FLAG_RETRY_UA;
2447 start_ccb->ccb_h.ccb_bp = bp;
2448 xpt_action(start_ccb);
2450 /* May have more work to do, so ensure we stay scheduled */
2454 case DA_STATE_PROBE_RC:
2456 struct scsi_read_capacity_data *rcap;
2458 rcap = (struct scsi_read_capacity_data *)
2459 malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
2461 printf("dastart: Couldn't malloc read_capacity data\n");
2462 /* da_free_periph??? */
2465 scsi_read_capacity(&start_ccb->csio,
2466 /*retries*/da_retry_count,
2472 start_ccb->ccb_h.ccb_bp = NULL;
2473 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC;
2474 xpt_action(start_ccb);
2477 case DA_STATE_PROBE_RC16:
2479 struct scsi_read_capacity_data_long *rcaplong;
2481 rcaplong = (struct scsi_read_capacity_data_long *)
2482 malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
2483 if (rcaplong == NULL) {
2484 printf("dastart: Couldn't malloc read_capacity data\n");
2485 /* da_free_periph??? */
2488 scsi_read_capacity_16(&start_ccb->csio,
2489 /*retries*/ da_retry_count,
2491 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2496 /*sense_len*/ SSD_FULL_SIZE,
2497 /*timeout*/ da_default_timeout * 1000);
2498 start_ccb->ccb_h.ccb_bp = NULL;
2499 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC16;
2500 xpt_action(start_ccb);
2503 case DA_STATE_PROBE_LBP:
2505 struct scsi_vpd_logical_block_prov *lbp;
2507 if (!scsi_vpd_supported_page(periph, SVPD_LBP)) {
2509 * If we get here we don't support any SBC-3 delete
2510 * methods with UNMAP as the Logical Block Provisioning
2511 * VPD page support is required for devices which
2512 * support it according to T10/1799-D Revision 31
2513 * however older revisions of the spec don't mandate
2514 * this so we currently don't remove these methods
2515 * from the available set.
2517 softc->state = DA_STATE_PROBE_BLK_LIMITS;
2521 lbp = (struct scsi_vpd_logical_block_prov *)
2522 malloc(sizeof(*lbp), M_SCSIDA, M_NOWAIT|M_ZERO);
2525 printf("dastart: Couldn't malloc lbp data\n");
2526 /* da_free_periph??? */
2530 scsi_inquiry(&start_ccb->csio,
2531 /*retries*/da_retry_count,
2533 /*tag_action*/MSG_SIMPLE_Q_TAG,
2534 /*inq_buf*/(u_int8_t *)lbp,
2535 /*inq_len*/sizeof(*lbp),
2537 /*page_code*/SVPD_LBP,
2538 /*sense_len*/SSD_MIN_SIZE,
2539 /*timeout*/da_default_timeout * 1000);
2540 start_ccb->ccb_h.ccb_bp = NULL;
2541 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_LBP;
2542 xpt_action(start_ccb);
2545 case DA_STATE_PROBE_BLK_LIMITS:
2547 struct scsi_vpd_block_limits *block_limits;
2549 if (!scsi_vpd_supported_page(periph, SVPD_BLOCK_LIMITS)) {
2550 /* Not supported skip to next probe */
2551 softc->state = DA_STATE_PROBE_BDC;
2555 block_limits = (struct scsi_vpd_block_limits *)
2556 malloc(sizeof(*block_limits), M_SCSIDA, M_NOWAIT|M_ZERO);
2558 if (block_limits == NULL) {
2559 printf("dastart: Couldn't malloc block_limits data\n");
2560 /* da_free_periph??? */
2564 scsi_inquiry(&start_ccb->csio,
2565 /*retries*/da_retry_count,
2567 /*tag_action*/MSG_SIMPLE_Q_TAG,
2568 /*inq_buf*/(u_int8_t *)block_limits,
2569 /*inq_len*/sizeof(*block_limits),
2571 /*page_code*/SVPD_BLOCK_LIMITS,
2572 /*sense_len*/SSD_MIN_SIZE,
2573 /*timeout*/da_default_timeout * 1000);
2574 start_ccb->ccb_h.ccb_bp = NULL;
2575 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BLK_LIMITS;
2576 xpt_action(start_ccb);
2579 case DA_STATE_PROBE_BDC:
2581 struct scsi_vpd_block_characteristics *bdc;
2583 if (!scsi_vpd_supported_page(periph, SVPD_BDC)) {
2584 softc->state = DA_STATE_PROBE_ATA;
2588 bdc = (struct scsi_vpd_block_characteristics *)
2589 malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
2592 printf("dastart: Couldn't malloc bdc data\n");
2593 /* da_free_periph??? */
2597 scsi_inquiry(&start_ccb->csio,
2598 /*retries*/da_retry_count,
2600 /*tag_action*/MSG_SIMPLE_Q_TAG,
2601 /*inq_buf*/(u_int8_t *)bdc,
2602 /*inq_len*/sizeof(*bdc),
2604 /*page_code*/SVPD_BDC,
2605 /*sense_len*/SSD_MIN_SIZE,
2606 /*timeout*/da_default_timeout * 1000);
2607 start_ccb->ccb_h.ccb_bp = NULL;
2608 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BDC;
2609 xpt_action(start_ccb);
2612 case DA_STATE_PROBE_ATA:
2614 struct ata_params *ata_params;
2616 if (!scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
2617 daprobedone(periph, start_ccb);
2621 ata_params = (struct ata_params*)
2622 malloc(sizeof(*ata_params), M_SCSIDA, M_NOWAIT|M_ZERO);
2624 if (ata_params == NULL) {
2625 printf("dastart: Couldn't malloc ata_params data\n");
2626 /* da_free_periph??? */
2630 scsi_ata_identify(&start_ccb->csio,
2631 /*retries*/da_retry_count,
2633 /*tag_action*/MSG_SIMPLE_Q_TAG,
2634 /*data_ptr*/(u_int8_t *)ata_params,
2635 /*dxfer_len*/sizeof(*ata_params),
2636 /*sense_len*/SSD_FULL_SIZE,
2637 /*timeout*/da_default_timeout * 1000);
2638 start_ccb->ccb_h.ccb_bp = NULL;
2639 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA;
2640 xpt_action(start_ccb);
2647 cmd6workaround(union ccb *ccb)
2649 struct scsi_rw_6 cmd6;
2650 struct scsi_rw_10 *cmd10;
2651 struct da_softc *softc;
2656 cdb = ccb->csio.cdb_io.cdb_bytes;
2657 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
2659 if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
2660 da_delete_methods old_method = softc->delete_method;
2663 * Typically there are two reasons for failure here
2664 * 1. Delete method was detected as supported but isn't
2665 * 2. Delete failed due to invalid params e.g. too big
2667 * While we will attempt to choose an alternative delete method
2668 * this may result in short deletes if the existing delete
2669 * requests from geom are big for the new method choosen.
2671 * This method assumes that the error which triggered this
2672 * will not retry the io otherwise a panic will occur
2674 dadeleteflag(softc, old_method, 0);
2675 dadeletemethodchoose(softc, DA_DELETE_DISABLE);
2676 if (softc->delete_method == DA_DELETE_DISABLE)
2677 xpt_print(ccb->ccb_h.path,
2678 "%s failed, disabling BIO_DELETE\n",
2679 da_delete_method_desc[old_method]);
2681 xpt_print(ccb->ccb_h.path,
2682 "%s failed, switching to %s BIO_DELETE\n",
2683 da_delete_method_desc[old_method],
2684 da_delete_method_desc[softc->delete_method]);
2687 while ((bp = bioq_takefirst(&softc->delete_run_queue))
2689 bioq_disksort(&softc->delete_queue, bp);
2691 while ((bp = bioq_takefirst(&softc->delete_run_queue))
2693 bioq_insert_tail(&softc->delete_queue, bp);
2695 bioq_insert_tail(&softc->delete_queue,
2696 (struct bio *)ccb->ccb_h.ccb_bp);
2697 ccb->ccb_h.ccb_bp = NULL;
2701 /* Translation only possible if CDB is an array and cmd is R/W6 */
2702 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
2703 (*cdb != READ_6 && *cdb != WRITE_6))
2706 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
2707 "increasing minimum_cmd_size to 10.\n");
2708 softc->minimum_cmd_size = 10;
2710 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
2711 cmd10 = (struct scsi_rw_10 *)cdb;
2712 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
2714 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
2715 cmd10->reserved = 0;
2716 scsi_ulto2b(cmd6.length, cmd10->length);
2717 cmd10->control = cmd6.control;
2718 ccb->csio.cdb_len = sizeof(*cmd10);
2720 /* Requeue request, unfreezing queue if necessary */
2721 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
2722 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2725 cam_release_devq(ccb->ccb_h.path,
2729 /*getcount_only*/0);
2735 dadone(struct cam_periph *periph, union ccb *done_ccb)
2737 struct da_softc *softc;
2738 struct ccb_scsiio *csio;
2742 softc = (struct da_softc *)periph->softc;
2743 priority = done_ccb->ccb_h.pinfo.priority;
2745 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
2747 csio = &done_ccb->csio;
2748 state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
2750 case DA_CCB_BUFFER_IO:
2753 struct bio *bp, *bp1;
2755 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2756 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2760 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
2765 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
2766 if (error == ERESTART) {
2768 * A retry was scheduled, so
2773 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2778 * return all queued I/O with EIO, so that
2779 * the client can retry these I/Os in the
2780 * proper order should it attempt to recover.
2785 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
2787 * Catastrophic error. Mark our pack as
2791 * XXX See if this is really a media
2794 xpt_print(periph->path,
2795 "Invalidating pack\n");
2796 softc->flags |= DA_FLAG_PACK_INVALID;
2797 queued_error = ENXIO;
2799 bioq_flush(&softc->bio_queue, NULL,
2802 bp->bio_error = error;
2803 bp->bio_resid = bp->bio_bcount;
2804 bp->bio_flags |= BIO_ERROR;
2806 } else if (bp != NULL) {
2807 bp->bio_resid = csio->resid;
2809 if (bp->bio_resid != 0)
2810 bp->bio_flags |= BIO_ERROR;
2812 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2813 cam_release_devq(done_ccb->ccb_h.path,
2817 /*getcount_only*/0);
2818 } else if (bp != NULL) {
2819 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2820 panic("REQ_CMP with QFRZN");
2821 bp->bio_resid = csio->resid;
2822 if (csio->resid > 0)
2823 bp->bio_flags |= BIO_ERROR;
2824 if (softc->error_inject != 0) {
2825 bp->bio_error = softc->error_inject;
2826 bp->bio_resid = bp->bio_bcount;
2827 bp->bio_flags |= BIO_ERROR;
2828 softc->error_inject = 0;
2834 * Block out any asyncronous callbacks
2835 * while we touch the pending ccb list.
2837 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
2838 softc->outstanding_cmds--;
2839 if (softc->outstanding_cmds == 0)
2840 softc->flags |= DA_FLAG_WENT_IDLE;
2842 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
2843 xpt_print(periph->path, "oustanding %d\n",
2844 softc->outstanding_cmds);
2847 if (state == DA_CCB_DELETE) {
2848 while ((bp1 = bioq_takefirst(&softc->delete_run_queue))
2850 bp1->bio_resid = bp->bio_resid;
2851 bp1->bio_error = bp->bio_error;
2852 if (bp->bio_flags & BIO_ERROR)
2853 bp1->bio_flags |= BIO_ERROR;
2856 softc->delete_running = 0;
2860 } else if (bp != NULL)
2864 case DA_CCB_PROBE_RC:
2865 case DA_CCB_PROBE_RC16:
2867 struct scsi_read_capacity_data *rdcap;
2868 struct scsi_read_capacity_data_long *rcaplong;
2869 char announce_buf[80];
2875 if (state == DA_CCB_PROBE_RC)
2876 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
2878 rcaplong = (struct scsi_read_capacity_data_long *)
2881 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
2882 struct disk_params *dp;
2883 uint32_t block_size;
2885 u_int lbppbe; /* LB per physical block exponent. */
2886 u_int lalba; /* Lowest aligned LBA. */
2888 if (state == DA_CCB_PROBE_RC) {
2889 block_size = scsi_4btoul(rdcap->length);
2890 maxsector = scsi_4btoul(rdcap->addr);
2895 * According to SBC-2, if the standard 10
2896 * byte READ CAPACITY command returns 2^32,
2897 * we should issue the 16 byte version of
2898 * the command, since the device in question
2899 * has more sectors than can be represented
2900 * with the short version of the command.
2902 if (maxsector == 0xffffffff) {
2903 free(rdcap, M_SCSIDA);
2904 xpt_release_ccb(done_ccb);
2905 softc->state = DA_STATE_PROBE_RC16;
2906 xpt_schedule(periph, priority);
2910 block_size = scsi_4btoul(rcaplong->length);
2911 maxsector = scsi_8btou64(rcaplong->addr);
2912 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
2913 lalba = scsi_2btoul(rcaplong->lalba_lbp);
2917 * Because GEOM code just will panic us if we
2918 * give them an 'illegal' value we'll avoid that
2921 if (block_size == 0 && maxsector == 0) {
2922 snprintf(announce_buf, sizeof(announce_buf),
2924 } else if (block_size >= MAXPHYS || block_size == 0) {
2925 xpt_print(periph->path,
2926 "unsupportable block size %ju\n",
2927 (uintmax_t) block_size);
2928 announce_buf[0] = '\0';
2929 cam_periph_invalidate(periph);
2932 * We pass rcaplong into dasetgeom(),
2933 * because it will only use it if it is
2936 dasetgeom(periph, block_size, maxsector,
2937 rcaplong, sizeof(*rcaplong));
2938 lbp = (lalba & SRC16_LBPME_A);
2939 dp = &softc->params;
2940 snprintf(announce_buf, sizeof(announce_buf),
2941 "%juMB (%ju %u byte sectors: %dH %dS/T "
2943 (((uintmax_t)dp->secsize *
2944 dp->sectors) / (1024*1024)),
2945 (uintmax_t)dp->sectors,
2946 dp->secsize, dp->heads,
2947 dp->secs_per_track, dp->cylinders);
2952 announce_buf[0] = '\0';
2955 * Retry any UNIT ATTENTION type errors. They
2956 * are expected at boot.
2958 error = daerror(done_ccb, CAM_RETRY_SELTO,
2959 SF_RETRY_UA|SF_NO_PRINT);
2960 if (error == ERESTART) {
2962 * A retry was scheuled, so
2966 } else if (error != 0) {
2968 int sense_key, error_code;
2971 struct ccb_getdev cgd;
2973 /* Don't wedge this device's queue */
2974 status = done_ccb->ccb_h.status;
2975 if ((status & CAM_DEV_QFRZN) != 0)
2976 cam_release_devq(done_ccb->ccb_h.path,
2980 /*getcount_only*/0);
2983 xpt_setup_ccb(&cgd.ccb_h,
2984 done_ccb->ccb_h.path,
2985 CAM_PRIORITY_NORMAL);
2986 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2987 xpt_action((union ccb *)&cgd);
2989 if (scsi_extract_sense_ccb(done_ccb,
2990 &error_code, &sense_key, &asc, &ascq))
2996 * If we tried READ CAPACITY(16) and failed,
2997 * fallback to READ CAPACITY(10).
2999 if ((state == DA_CCB_PROBE_RC16) &&
3000 (softc->flags & DA_FLAG_CAN_RC16) &&
3001 (((csio->ccb_h.status & CAM_STATUS_MASK) ==
3004 (error_code == SSD_CURRENT_ERROR) &&
3005 (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
3006 softc->flags &= ~DA_FLAG_CAN_RC16;
3007 free(rdcap, M_SCSIDA);
3008 xpt_release_ccb(done_ccb);
3009 softc->state = DA_STATE_PROBE_RC;
3010 xpt_schedule(periph, priority);
3014 * Attach to anything that claims to be a
3015 * direct access or optical disk device,
3016 * as long as it doesn't return a "Logical
3017 * unit not supported" (0x25) error.
3019 if ((have_sense) && (asc != 0x25)
3020 && (error_code == SSD_CURRENT_ERROR)) {
3021 const char *sense_key_desc;
3022 const char *asc_desc;
3024 scsi_sense_desc(sense_key, asc, ascq,
3028 snprintf(announce_buf,
3029 sizeof(announce_buf),
3030 "Attempt to query device "
3031 "size failed: %s, %s",
3039 xpt_print(periph->path,
3040 "got CAM status %#x\n",
3041 done_ccb->ccb_h.status);
3044 xpt_print(periph->path, "fatal error, "
3045 "failed to attach to device\n");
3048 * Free up resources.
3050 cam_periph_invalidate(periph);
3054 free(csio->data_ptr, M_SCSIDA);
3055 if (announce_buf[0] != '\0' && ((softc->flags & DA_FLAG_PROBED) == 0)) {
3057 * Create our sysctl variables, now that we know
3058 * we have successfully attached.
3060 /* increase the refcount */
3061 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3062 taskqueue_enqueue(taskqueue_thread,
3063 &softc->sysctl_task);
3064 xpt_announce_periph(periph, announce_buf);
3065 xpt_announce_quirks(periph, softc->quirks,
3068 xpt_print(periph->path, "fatal error, "
3069 "could not acquire reference count\n");
3073 /* Ensure re-probe doesn't see old delete. */
3074 softc->delete_available = 0;
3077 * Based on older SBC-3 spec revisions
3078 * any of the UNMAP methods "may" be
3079 * available via LBP given this flag so
3080 * we flag all of them as availble and
3081 * then remove those which further
3082 * probes confirm aren't available
3085 * We could also check readcap(16) p_type
3086 * flag to exclude one or more invalid
3087 * write same (X) types here
3089 dadeleteflag(softc, DA_DELETE_WS16, 1);
3090 dadeleteflag(softc, DA_DELETE_WS10, 1);
3091 dadeleteflag(softc, DA_DELETE_ZERO, 1);
3092 dadeleteflag(softc, DA_DELETE_UNMAP, 1);
3094 xpt_release_ccb(done_ccb);
3095 softc->state = DA_STATE_PROBE_LBP;
3096 xpt_schedule(periph, priority);
3100 xpt_release_ccb(done_ccb);
3101 softc->state = DA_STATE_PROBE_BDC;
3102 xpt_schedule(periph, priority);
3105 case DA_CCB_PROBE_LBP:
3107 struct scsi_vpd_logical_block_prov *lbp;
3109 lbp = (struct scsi_vpd_logical_block_prov *)csio->data_ptr;
3111 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3113 * T10/1799-D Revision 31 states at least one of these
3114 * must be supported but we don't currently enforce this.
3116 dadeleteflag(softc, DA_DELETE_WS16,
3117 (lbp->flags & SVPD_LBP_WS16));
3118 dadeleteflag(softc, DA_DELETE_WS10,
3119 (lbp->flags & SVPD_LBP_WS10));
3120 dadeleteflag(softc, DA_DELETE_ZERO,
3121 (lbp->flags & SVPD_LBP_WS10));
3122 dadeleteflag(softc, DA_DELETE_UNMAP,
3123 (lbp->flags & SVPD_LBP_UNMAP));
3125 if (lbp->flags & SVPD_LBP_UNMAP) {
3126 free(lbp, M_SCSIDA);
3127 xpt_release_ccb(done_ccb);
3128 softc->state = DA_STATE_PROBE_BLK_LIMITS;
3129 xpt_schedule(periph, priority);
3134 error = daerror(done_ccb, CAM_RETRY_SELTO,
3135 SF_RETRY_UA|SF_NO_PRINT);
3136 if (error == ERESTART)
3138 else if (error != 0) {
3139 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3140 /* Don't wedge this device's queue */
3141 cam_release_devq(done_ccb->ccb_h.path,
3145 /*getcount_only*/0);
3149 * Failure indicates we don't support any SBC-3
3150 * delete methods with UNMAP
3155 free(lbp, M_SCSIDA);
3156 xpt_release_ccb(done_ccb);
3157 softc->state = DA_STATE_PROBE_BDC;
3158 xpt_schedule(periph, priority);
3161 case DA_CCB_PROBE_BLK_LIMITS:
3163 struct scsi_vpd_block_limits *block_limits;
3165 block_limits = (struct scsi_vpd_block_limits *)csio->data_ptr;
3167 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3168 uint32_t max_unmap_lba_cnt = scsi_4btoul(
3169 block_limits->max_unmap_lba_cnt);
3170 uint32_t max_unmap_blk_cnt = scsi_4btoul(
3171 block_limits->max_unmap_blk_cnt);
3172 uint64_t ws_max_blks = scsi_8btou64(
3173 block_limits->max_write_same_length);
3175 * We should already support UNMAP but we check lba
3176 * and block count to be sure
3178 if (max_unmap_lba_cnt != 0x00L &&
3179 max_unmap_blk_cnt != 0x00L) {
3180 softc->unmap_max_lba = max_unmap_lba_cnt;
3181 softc->unmap_max_ranges = min(max_unmap_blk_cnt,
3185 * Unexpected UNMAP limits which means the
3186 * device doesn't actually support UNMAP
3188 dadeleteflag(softc, DA_DELETE_UNMAP, 0);
3191 if (ws_max_blks != 0x00L)
3192 softc->ws_max_blks = ws_max_blks;
3195 error = daerror(done_ccb, CAM_RETRY_SELTO,
3196 SF_RETRY_UA|SF_NO_PRINT);
3197 if (error == ERESTART)
3199 else if (error != 0) {
3200 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3201 /* Don't wedge this device's queue */
3202 cam_release_devq(done_ccb->ccb_h.path,
3206 /*getcount_only*/0);
3210 * Failure here doesn't mean UNMAP is not
3211 * supported as this is an optional page.
3213 softc->unmap_max_lba = 1;
3214 softc->unmap_max_ranges = 1;
3218 free(block_limits, M_SCSIDA);
3219 xpt_release_ccb(done_ccb);
3220 softc->state = DA_STATE_PROBE_BDC;
3221 xpt_schedule(periph, priority);
3224 case DA_CCB_PROBE_BDC:
3226 struct scsi_vpd_block_characteristics *bdc;
3228 bdc = (struct scsi_vpd_block_characteristics *)csio->data_ptr;
3230 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3232 * Disable queue sorting for non-rotational media
3235 if (scsi_2btoul(bdc->medium_rotation_rate) ==
3236 SVPD_BDC_RATE_NONE_ROTATING)
3237 softc->sort_io_queue = 0;
3240 error = daerror(done_ccb, CAM_RETRY_SELTO,
3241 SF_RETRY_UA|SF_NO_PRINT);
3242 if (error == ERESTART)
3244 else if (error != 0) {
3245 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3246 /* Don't wedge this device's queue */
3247 cam_release_devq(done_ccb->ccb_h.path,
3251 /*getcount_only*/0);
3256 free(bdc, M_SCSIDA);
3257 xpt_release_ccb(done_ccb);
3258 softc->state = DA_STATE_PROBE_ATA;
3259 xpt_schedule(periph, priority);
3262 case DA_CCB_PROBE_ATA:
3265 struct ata_params *ata_params;
3268 ata_params = (struct ata_params *)csio->data_ptr;
3269 ptr = (uint16_t *)ata_params;
3271 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3272 for (i = 0; i < sizeof(*ata_params) / 2; i++)
3273 ptr[i] = le16toh(ptr[i]);
3274 if (ata_params->support_dsm & ATA_SUPPORT_DSM_TRIM) {
3275 dadeleteflag(softc, DA_DELETE_ATA_TRIM, 1);
3276 if (ata_params->max_dsm_blocks != 0)
3277 softc->trim_max_ranges = min(
3278 softc->trim_max_ranges,
3279 ata_params->max_dsm_blocks *
3280 ATA_DSM_BLK_RANGES);
3283 * Disable queue sorting for non-rotational media
3286 if (ata_params->media_rotation_rate == 1)
3287 softc->sort_io_queue = 0;
3290 error = daerror(done_ccb, CAM_RETRY_SELTO,
3291 SF_RETRY_UA|SF_NO_PRINT);
3292 if (error == ERESTART)
3294 else if (error != 0) {
3295 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3296 /* Don't wedge this device's queue */
3297 cam_release_devq(done_ccb->ccb_h.path,
3301 /*getcount_only*/0);
3306 free(ata_params, M_SCSIDA);
3307 daprobedone(periph, done_ccb);
3310 case DA_CCB_WAITING:
3312 /* Caller will release the CCB */
3313 wakeup(&done_ccb->ccb_h.cbfcnp);
3317 /* No-op. We're polling */
3321 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3323 if (daerror(done_ccb, CAM_RETRY_SELTO,
3324 SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
3327 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3328 cam_release_devq(done_ccb->ccb_h.path,
3332 /*getcount_only*/0);
3334 xpt_release_ccb(done_ccb);
3335 cam_periph_release_locked(periph);
3341 xpt_release_ccb(done_ccb);
3345 dareprobe(struct cam_periph *periph)
3347 struct da_softc *softc;
3350 softc = (struct da_softc *)periph->softc;
3352 /* Probe in progress; don't interfere. */
3353 if (softc->state != DA_STATE_NORMAL)
3356 status = cam_periph_acquire(periph);
3357 KASSERT(status == CAM_REQ_CMP,
3358 ("dareprobe: cam_periph_acquire failed"));
3360 if (softc->flags & DA_FLAG_CAN_RC16)
3361 softc->state = DA_STATE_PROBE_RC16;
3363 softc->state = DA_STATE_PROBE_RC;
3365 xpt_schedule(periph, CAM_PRIORITY_DEV);
3369 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3371 struct da_softc *softc;
3372 struct cam_periph *periph;
3373 int error, error_code, sense_key, asc, ascq;
3375 periph = xpt_path_periph(ccb->ccb_h.path);
3376 softc = (struct da_softc *)periph->softc;
3379 * Automatically detect devices that do not support
3380 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
3383 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3384 error = cmd6workaround(ccb);
3385 } else if (scsi_extract_sense_ccb(ccb,
3386 &error_code, &sense_key, &asc, &ascq)) {
3387 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3388 error = cmd6workaround(ccb);
3390 * If the target replied with CAPACITY DATA HAS CHANGED UA,
3391 * query the capacity and notify upper layers.
3393 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3394 asc == 0x2A && ascq == 0x09) {
3395 xpt_print(periph->path, "capacity data has changed\n");
3397 sense_flags |= SF_NO_PRINT;
3398 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3399 asc == 0x28 && ascq == 0x00)
3400 disk_media_changed(softc->disk, M_NOWAIT);
3401 else if (sense_key == SSD_KEY_NOT_READY &&
3402 asc == 0x3a && (softc->flags & DA_FLAG_SAW_MEDIA)) {
3403 softc->flags &= ~DA_FLAG_SAW_MEDIA;
3404 disk_media_gone(softc->disk, M_NOWAIT);
3407 if (error == ERESTART)
3412 * Until we have a better way of doing pack validation,
3413 * don't treat UAs as errors.
3415 sense_flags |= SF_RETRY_UA;
3416 return(cam_periph_error(ccb, cam_flags, sense_flags,
3417 &softc->saved_ccb));
3421 damediapoll(void *arg)
3423 struct cam_periph *periph = arg;
3424 struct da_softc *softc = periph->softc;
3426 if (!softc->tur && softc->outstanding_cmds == 0) {
3427 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3432 /* Queue us up again */
3433 if (da_poll_period != 0)
3434 callout_schedule(&softc->mediapoll_c, da_poll_period * hz);
3438 daprevent(struct cam_periph *periph, int action)
3440 struct da_softc *softc;
3444 softc = (struct da_softc *)periph->softc;
3446 if (((action == PR_ALLOW)
3447 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
3448 || ((action == PR_PREVENT)
3449 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
3453 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3455 scsi_prevent(&ccb->csio,
3463 error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
3464 SF_RETRY_UA | SF_QUIET_IR, softc->disk->d_devstat);
3467 if (action == PR_ALLOW)
3468 softc->flags &= ~DA_FLAG_PACK_LOCKED;
3470 softc->flags |= DA_FLAG_PACK_LOCKED;
3473 xpt_release_ccb(ccb);
3477 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
3478 struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
3480 struct ccb_calc_geometry ccg;
3481 struct da_softc *softc;
3482 struct disk_params *dp;
3483 u_int lbppbe, lalba;
3485 softc = (struct da_softc *)periph->softc;
3487 dp = &softc->params;
3488 dp->secsize = block_len;
3489 dp->sectors = maxsector + 1;
3490 if (rcaplong != NULL) {
3491 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
3492 lalba = scsi_2btoul(rcaplong->lalba_lbp);
3493 lalba &= SRC16_LALBA_A;
3500 dp->stripesize = block_len << lbppbe;
3501 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
3503 } else if (softc->quirks & DA_Q_4K) {
3504 dp->stripesize = 4096;
3505 dp->stripeoffset = 0;
3508 dp->stripeoffset = 0;
3511 * Have the controller provide us with a geometry
3512 * for this disk. The only time the geometry
3513 * matters is when we boot and the controller
3514 * is the only one knowledgeable enough to come
3515 * up with something that will make this a bootable
3518 xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3519 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
3520 ccg.block_size = dp->secsize;
3521 ccg.volume_size = dp->sectors;
3523 ccg.secs_per_track = 0;
3525 xpt_action((union ccb*)&ccg);
3526 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3528 * We don't know what went wrong here- but just pick
3529 * a geometry so we don't have nasty things like divide
3533 dp->secs_per_track = 255;
3534 dp->cylinders = dp->sectors / (255 * 255);
3535 if (dp->cylinders == 0) {
3539 dp->heads = ccg.heads;
3540 dp->secs_per_track = ccg.secs_per_track;
3541 dp->cylinders = ccg.cylinders;
3545 * If the user supplied a read capacity buffer, and if it is
3546 * different than the previous buffer, update the data in the EDT.
3547 * If it's the same, we don't bother. This avoids sending an
3548 * update every time someone opens this device.
3550 if ((rcaplong != NULL)
3551 && (bcmp(rcaplong, &softc->rcaplong,
3552 min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
3553 struct ccb_dev_advinfo cdai;
3555 xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3556 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
3557 cdai.buftype = CDAI_TYPE_RCAPLONG;
3558 cdai.flags |= CDAI_FLAG_STORE;
3559 cdai.bufsiz = rcap_len;
3560 cdai.buf = (uint8_t *)rcaplong;
3561 xpt_action((union ccb *)&cdai);
3562 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
3563 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
3564 if (cdai.ccb_h.status != CAM_REQ_CMP) {
3565 xpt_print(periph->path, "%s: failed to set read "
3566 "capacity advinfo\n", __func__);
3567 /* Use cam_error_print() to decode the status */
3568 cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
3571 bcopy(rcaplong, &softc->rcaplong,
3572 min(sizeof(softc->rcaplong), rcap_len));
3576 softc->disk->d_sectorsize = softc->params.secsize;
3577 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
3578 softc->disk->d_stripesize = softc->params.stripesize;
3579 softc->disk->d_stripeoffset = softc->params.stripeoffset;
3580 /* XXX: these are not actually "firmware" values, so they may be wrong */
3581 softc->disk->d_fwsectors = softc->params.secs_per_track;
3582 softc->disk->d_fwheads = softc->params.heads;
3583 softc->disk->d_devstat->block_size = softc->params.secsize;
3584 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
3588 dasendorderedtag(void *arg)
3590 struct da_softc *softc = arg;
3592 if (da_send_ordered) {
3593 if ((softc->ordered_tag_count == 0)
3594 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
3595 softc->flags |= DA_FLAG_NEED_OTAG;
3597 if (softc->outstanding_cmds > 0)
3598 softc->flags &= ~DA_FLAG_WENT_IDLE;
3600 softc->ordered_tag_count = 0;
3602 /* Queue us up again */
3603 callout_reset(&softc->sendordered_c,
3604 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
3605 dasendorderedtag, softc);
3609 * Step through all DA peripheral drivers, and if the device is still open,
3610 * sync the disk cache to physical media.
3613 dashutdown(void * arg, int howto)
3615 struct cam_periph *periph;
3616 struct da_softc *softc;
3620 CAM_PERIPH_FOREACH(periph, &dadriver) {
3621 softc = (struct da_softc *)periph->softc;
3622 if (SCHEDULER_STOPPED()) {
3623 /* If we paniced with the lock held, do not recurse. */
3624 if (!cam_periph_owned(periph) &&
3625 (softc->flags & DA_FLAG_OPEN)) {
3626 dadump(softc->disk, NULL, 0, 0, 0);
3630 cam_periph_lock(periph);
3633 * We only sync the cache if the drive is still open, and
3634 * if the drive is capable of it..
3636 if (((softc->flags & DA_FLAG_OPEN) == 0)
3637 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
3638 cam_periph_unlock(periph);
3642 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3643 scsi_synchronize_cache(&ccb->csio,
3647 /*begin_lba*/0, /* whole disk */
3652 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
3653 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR,
3654 softc->disk->d_devstat);
3656 xpt_print(periph->path, "Synchronize cache failed\n");
3657 xpt_release_ccb(ccb);
3658 cam_periph_unlock(periph);
3662 #else /* !_KERNEL */
3665 * XXX This is only left out of the kernel build to silence warnings. If,
3666 * for some reason this function is used in the kernel, the ifdefs should
3667 * be moved so it is included both in the kernel and userland.
3670 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
3671 void (*cbfcnp)(struct cam_periph *, union ccb *),
3672 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
3673 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
3676 struct scsi_format_unit *scsi_cmd;
3678 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
3679 scsi_cmd->opcode = FORMAT_UNIT;
3680 scsi_cmd->byte2 = byte2;
3681 scsi_ulto2b(ileave, scsi_cmd->interleave);
3686 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
3695 #endif /* _KERNEL */