2 * Implementation of SCSI Direct Access Peripheral driver for CAM.
4 * Copyright (c) 1997 Justin T. Gibbs.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
39 #include <sys/taskqueue.h>
41 #include <sys/mutex.h>
43 #include <sys/devicestat.h>
44 #include <sys/eventhandler.h>
45 #include <sys/malloc.h>
47 #include <sys/endian.h>
49 #include <geom/geom.h>
50 #include <geom/geom_disk.h>
59 #include <cam/cam_ccb.h>
60 #include <cam/cam_periph.h>
61 #include <cam/cam_xpt_periph.h>
62 #include <cam/cam_sim.h>
64 #include <cam/scsi/scsi_message.h>
67 #include <cam/scsi/scsi_da.h>
75 DA_STATE_PROBE_BLK_LIMITS,
82 DA_FLAG_PACK_INVALID = 0x001,
83 DA_FLAG_NEW_PACK = 0x002,
84 DA_FLAG_PACK_LOCKED = 0x004,
85 DA_FLAG_PACK_REMOVABLE = 0x008,
86 DA_FLAG_NEED_OTAG = 0x020,
87 DA_FLAG_WAS_OTAG = 0x040,
88 DA_FLAG_RETRY_UA = 0x080,
90 DA_FLAG_SCTX_INIT = 0x200,
91 DA_FLAG_CAN_RC16 = 0x400,
92 DA_FLAG_PROBED = 0x800,
93 DA_FLAG_DIRTY = 0x1000,
94 DA_FLAG_ANNOUNCED = 0x2000
99 DA_Q_NO_SYNC_CACHE = 0x01,
100 DA_Q_NO_6_BYTE = 0x02,
101 DA_Q_NO_PREVENT = 0x04,
107 #define DA_Q_BIT_STRING \
109 "\001NO_SYNC_CACHE" \
116 DA_CCB_PROBE_RC = 0x01,
117 DA_CCB_PROBE_RC16 = 0x02,
118 DA_CCB_PROBE_LBP = 0x03,
119 DA_CCB_PROBE_BLK_LIMITS = 0x04,
120 DA_CCB_PROBE_BDC = 0x05,
121 DA_CCB_PROBE_ATA = 0x06,
122 DA_CCB_BUFFER_IO = 0x07,
124 DA_CCB_DELETE = 0x0B,
126 DA_CCB_TYPE_MASK = 0x0F,
127 DA_CCB_RETRY_UA = 0x10
131 * Order here is important for method choice
133 * We prefer ATA_TRIM as tests run against a Sandforce 2281 SSD attached to
134 * LSI 2008 (mps) controller (FW: v12, Drv: v14) resulted 20% quicker deletes
135 * using ATA_TRIM than the corresponding UNMAP results for a real world mysql
136 * import taking 5mins.
147 DA_DELETE_MIN = DA_DELETE_ATA_TRIM,
148 DA_DELETE_MAX = DA_DELETE_ZERO
151 typedef void da_delete_func_t (struct cam_periph *periph, union ccb *ccb,
153 static da_delete_func_t da_delete_trim;
154 static da_delete_func_t da_delete_unmap;
155 static da_delete_func_t da_delete_ws;
157 static const void * da_delete_functions[] = {
167 static const char *da_delete_method_names[] =
168 { "NONE", "DISABLE", "ATA_TRIM", "UNMAP", "WS16", "WS10", "ZERO" };
169 static const char *da_delete_method_desc[] =
170 { "NONE", "DISABLED", "ATA TRIM", "UNMAP", "WRITE SAME(16) with UNMAP",
171 "WRITE SAME(10) with UNMAP", "ZERO" };
173 /* Offsets into our private area for storing information */
174 #define ccb_state ppriv_field0
175 #define ccb_bp ppriv_ptr1
180 u_int8_t secs_per_track;
181 u_int32_t secsize; /* Number of bytes/sector */
182 u_int64_t sectors; /* total number sectors */
187 #define UNMAP_RANGE_MAX 0xffffffff
188 #define UNMAP_HEAD_SIZE 8
189 #define UNMAP_RANGE_SIZE 16
190 #define UNMAP_MAX_RANGES 2048 /* Protocol Max is 4095 */
191 #define UNMAP_BUF_SIZE ((UNMAP_MAX_RANGES * UNMAP_RANGE_SIZE) + \
194 #define WS10_MAX_BLKS 0xffff
195 #define WS16_MAX_BLKS 0xffffffff
196 #define ATA_TRIM_MAX_RANGES ((UNMAP_BUF_SIZE / \
197 (ATA_DSM_RANGE_SIZE * ATA_DSM_BLK_SIZE)) * ATA_DSM_BLK_SIZE)
200 struct bio_queue_head bio_queue;
201 struct bio_queue_head delete_queue;
202 struct bio_queue_head delete_run_queue;
203 LIST_HEAD(, ccb_hdr) pending_ccbs;
204 int tur; /* TEST UNIT READY should be sent */
205 int refcount; /* Active xpt_action() calls */
210 int minimum_cmd_size;
214 int delete_available; /* Delete methods possibly available */
216 uint32_t unmap_max_ranges;
217 uint32_t unmap_max_lba; /* Max LBAs in UNMAP req */
218 uint64_t ws_max_blks;
219 da_delete_methods delete_method;
220 da_delete_func_t *delete_func;
221 struct disk_params params;
224 struct task sysctl_task;
225 struct sysctl_ctx_list sysctl_ctx;
226 struct sysctl_oid *sysctl_tree;
227 struct callout sendordered_c;
229 uint8_t unmap_buf[UNMAP_BUF_SIZE];
230 struct scsi_read_capacity_data_long rcaplong;
231 struct callout mediapoll_c;
234 #define dadeleteflag(softc, delete_method, enable) \
236 softc->delete_available |= (1 << delete_method); \
238 softc->delete_available &= ~(1 << delete_method); \
241 struct da_quirk_entry {
242 struct scsi_inquiry_pattern inq_pat;
246 static const char quantum[] = "QUANTUM";
247 static const char microp[] = "MICROP";
249 static struct da_quirk_entry da_quirk_table[] =
251 /* SPI, FC devices */
254 * Fujitsu M2513A MO drives.
255 * Tested devices: M2513A2 firmware versions 1200 & 1300.
256 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
257 * Reported by: W.Scholten <whs@xs4all.nl>
259 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
260 /*quirks*/ DA_Q_NO_SYNC_CACHE
264 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
265 /*quirks*/ DA_Q_NO_SYNC_CACHE
269 * This particular Fujitsu drive doesn't like the
270 * synchronize cache command.
271 * Reported by: Tom Jackson <toj@gorilla.net>
273 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
274 /*quirks*/ DA_Q_NO_SYNC_CACHE
278 * This drive doesn't like the synchronize cache command
279 * either. Reported by: Matthew Jacob <mjacob@feral.com>
280 * in NetBSD PR kern/6027, August 24, 1998.
282 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
283 /*quirks*/ DA_Q_NO_SYNC_CACHE
287 * This drive doesn't like the synchronize cache command
288 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
291 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
292 /*quirks*/ DA_Q_NO_SYNC_CACHE
296 * Doesn't like the synchronize cache command.
297 * Reported by: Blaz Zupan <blaz@gold.amis.net>
299 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
300 /*quirks*/ DA_Q_NO_SYNC_CACHE
304 * Doesn't like the synchronize cache command.
305 * Reported by: Blaz Zupan <blaz@gold.amis.net>
307 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
308 /*quirks*/ DA_Q_NO_SYNC_CACHE
312 * Doesn't like the synchronize cache command.
314 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
315 /*quirks*/ DA_Q_NO_SYNC_CACHE
319 * Doesn't like the synchronize cache command.
320 * Reported by: walter@pelissero.de
322 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
323 /*quirks*/ DA_Q_NO_SYNC_CACHE
327 * Doesn't work correctly with 6 byte reads/writes.
328 * Returns illegal request, and points to byte 9 of the
330 * Reported by: Adam McDougall <bsdx@spawnet.com>
332 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
333 /*quirks*/ DA_Q_NO_6_BYTE
337 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
338 /*quirks*/ DA_Q_NO_6_BYTE
342 * Doesn't like the synchronize cache command.
343 * Reported by: walter@pelissero.de
345 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
346 /*quirks*/ DA_Q_NO_SYNC_CACHE
350 * The CISS RAID controllers do not support SYNC_CACHE
352 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
353 /*quirks*/ DA_Q_NO_SYNC_CACHE
357 * The STEC SSDs sometimes hang on UNMAP.
359 {T_DIRECT, SIP_MEDIA_FIXED, "STEC", "*", "*"},
360 /*quirks*/ DA_Q_NO_UNMAP
362 /* USB mass storage devices supported by umass(4) */
365 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
368 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
369 /*quirks*/ DA_Q_NO_SYNC_CACHE
373 * Power Quotient Int. (PQI) USB flash key
376 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
377 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
381 * Creative Nomad MUVO mp3 player (USB)
384 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
385 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
389 * Jungsoft NEXDISK USB flash key
392 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
393 /*quirks*/ DA_Q_NO_SYNC_CACHE
397 * FreeDik USB Mini Data Drive
400 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
401 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
405 * Sigmatel USB Flash MP3 Player
408 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
409 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
413 * Neuros USB Digital Audio Computer
416 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
417 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
421 * SEAGRAND NP-900 MP3 Player
424 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
425 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
429 * iRiver iFP MP3 player (with UMS Firmware)
430 * PR: kern/54881, i386/63941, kern/66124
432 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
433 /*quirks*/ DA_Q_NO_SYNC_CACHE
437 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
440 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
441 /*quirks*/ DA_Q_NO_SYNC_CACHE
445 * ZICPlay USB MP3 Player with FM
448 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
449 /*quirks*/ DA_Q_NO_SYNC_CACHE
453 * TEAC USB floppy mechanisms
455 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
456 /*quirks*/ DA_Q_NO_SYNC_CACHE
460 * Kingston DataTraveler II+ USB Pen-Drive.
461 * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
463 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
464 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
472 {T_DIRECT, SIP_MEDIA_REMOVABLE, " ", "USB DISK Pro", "PMAP"},
473 /*quirks*/ DA_Q_NO_SYNC_CACHE
477 * Motorola E398 Mobile Phone (TransFlash memory card).
478 * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
481 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
482 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
486 * Qware BeatZkey! Pro
489 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
490 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
494 * Time DPA20B 1GB MP3 Player
497 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
498 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
502 * Samsung USB key 128Mb
505 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
506 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
510 * Kingston DataTraveler 2.0 USB Flash memory.
513 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
514 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
518 * Creative MUVO Slim mp3 player (USB)
521 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
522 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
526 * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
529 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
530 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
534 * SanDisk Micro Cruzer 128MB
537 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
538 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
542 * TOSHIBA TransMemory USB sticks
545 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
546 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
550 * PNY USB 3.0 Flash Drives
552 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PNY", "USB 3.0 FD*",
553 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_RC16
558 * PR: usb/75578, usb/72344, usb/65436
560 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
561 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
565 * Genesys 6-in-1 Card Reader
568 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
569 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
573 * Rekam Digital CAMERA
576 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
577 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
581 * iRiver H10 MP3 player
584 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
585 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
589 * iRiver U10 MP3 player
592 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
593 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
600 {T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
601 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
605 * EasyMP3 EM732X USB 2.0 Flash MP3 Player
608 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
609 "1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
616 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
617 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
621 * Philips USB Key Audio KEY013
624 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
625 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
632 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
633 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
640 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
641 /*quirks*/ DA_Q_NO_SYNC_CACHE
645 * I/O Magic USB flash - Giga Bank
648 {T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
649 /*quirks*/ DA_Q_NO_SYNC_CACHE
653 * JoyFly 128mb USB Flash Drive
656 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
657 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
664 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
665 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
669 * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A
672 {T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*",
673 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
677 * Samsung YP-U3 mp3-player
680 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3",
681 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
684 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
685 "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
689 * Sony Cyber-Shot DSC cameras
692 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
693 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
696 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler G3",
697 "1.00"}, /*quirks*/ DA_Q_NO_PREVENT
700 /* At least several Transcent USB sticks lie on RC16. */
701 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JetFlash", "Transcend*",
702 "*"}, /*quirks*/ DA_Q_NO_RC16
704 /* ATA/SATA devices over SAS/USB/... */
706 /* Hitachi Advanced Format (4k) drives */
707 { T_DIRECT, SIP_MEDIA_FIXED, "Hitachi", "H??????????E3*", "*" },
711 /* Samsung Advanced Format (4k) drives */
712 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD155UI*", "*" },
716 /* Samsung Advanced Format (4k) drives */
717 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD155UI*", "*" },
721 /* Samsung Advanced Format (4k) drives */
722 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD204UI*", "*" },
726 /* Samsung Advanced Format (4k) drives */
727 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD204UI*", "*" },
731 /* Seagate Barracuda Green Advanced Format (4k) drives */
732 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DL*", "*" },
736 /* Seagate Barracuda Green Advanced Format (4k) drives */
737 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DL", "*", "*" },
741 /* Seagate Barracuda Green Advanced Format (4k) drives */
742 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???DM*", "*" },
746 /* Seagate Barracuda Green Advanced Format (4k) drives */
747 { T_DIRECT, SIP_MEDIA_FIXED, "ST???DM*", "*", "*" },
751 /* Seagate Barracuda Green Advanced Format (4k) drives */
752 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DM*", "*" },
756 /* Seagate Barracuda Green Advanced Format (4k) drives */
757 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DM", "*", "*" },
761 /* Seagate Momentus Advanced Format (4k) drives */
762 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500423AS*", "*" },
766 /* Seagate Momentus Advanced Format (4k) drives */
767 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "3AS*", "*" },
771 /* Seagate Momentus Advanced Format (4k) drives */
772 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500424AS*", "*" },
776 /* Seagate Momentus Advanced Format (4k) drives */
777 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "4AS*", "*" },
781 /* Seagate Momentus Advanced Format (4k) drives */
782 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640423AS*", "*" },
786 /* Seagate Momentus Advanced Format (4k) drives */
787 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "3AS*", "*" },
791 /* Seagate Momentus Advanced Format (4k) drives */
792 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640424AS*", "*" },
796 /* Seagate Momentus Advanced Format (4k) drives */
797 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "4AS*", "*" },
801 /* Seagate Momentus Advanced Format (4k) drives */
802 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750420AS*", "*" },
806 /* Seagate Momentus Advanced Format (4k) drives */
807 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "0AS*", "*" },
811 /* Seagate Momentus Advanced Format (4k) drives */
812 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750422AS*", "*" },
816 /* Seagate Momentus Advanced Format (4k) drives */
817 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "2AS*", "*" },
821 /* Seagate Momentus Advanced Format (4k) drives */
822 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750423AS*", "*" },
826 /* Seagate Momentus Advanced Format (4k) drives */
827 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "3AS*", "*" },
831 /* Seagate Momentus Thin Advanced Format (4k) drives */
832 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???LT*", "*" },
836 /* Seagate Momentus Thin Advanced Format (4k) drives */
837 { T_DIRECT, SIP_MEDIA_FIXED, "ST???LT*", "*", "*" },
841 /* WDC Caviar Green Advanced Format (4k) drives */
842 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RS*", "*" },
846 /* WDC Caviar Green Advanced Format (4k) drives */
847 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RS*", "*" },
851 /* WDC Caviar Green Advanced Format (4k) drives */
852 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RX*", "*" },
856 /* WDC Caviar Green Advanced Format (4k) drives */
857 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RX*", "*" },
861 /* WDC Caviar Green Advanced Format (4k) drives */
862 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RS*", "*" },
866 /* WDC Caviar Green Advanced Format (4k) drives */
867 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RS*", "*" },
871 /* WDC Caviar Green Advanced Format (4k) drives */
872 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RX*", "*" },
876 /* WDC Caviar Green Advanced Format (4k) drives */
877 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RX*", "*" },
881 /* WDC Scorpio Black Advanced Format (4k) drives */
882 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PKT*", "*" },
886 /* WDC Scorpio Black Advanced Format (4k) drives */
887 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PKT*", "*" },
891 /* WDC Scorpio Black Advanced Format (4k) drives */
892 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PKT*", "*" },
896 /* WDC Scorpio Black Advanced Format (4k) drives */
897 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PKT*", "*" },
901 /* WDC Scorpio Blue Advanced Format (4k) drives */
902 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PVT*", "*" },
906 /* WDC Scorpio Blue Advanced Format (4k) drives */
907 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PVT*", "*" },
911 /* WDC Scorpio Blue Advanced Format (4k) drives */
912 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PVT*", "*" },
916 /* WDC Scorpio Blue Advanced Format (4k) drives */
917 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PVT*", "*" },
922 * Olympus FE-210 camera
924 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "FE210*",
925 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
931 {T_DIRECT, SIP_MEDIA_REMOVABLE, "LG", "UP3S",
932 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
936 * Laser MP3-2GA13 MP3 player
938 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "(HS) Flash Disk",
939 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
943 * LaCie external 250GB Hard drive des by Porsche
944 * Submitted by: Ben Stuyts <ben@altesco.nl>
947 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HM250JI", "*"},
948 /*quirks*/ DA_Q_NO_SYNC_CACHE
953 * Corsair Force 2 SSDs
954 * 4k optimised & trim only works in 4k requests + 4k aligned
956 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair CSSD-F*", "*" },
961 * Corsair Force 3 SSDs
962 * 4k optimised & trim only works in 4k requests + 4k aligned
964 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force 3*", "*" },
969 * Corsair Neutron GTX SSDs
970 * 4k optimised & trim only works in 4k requests + 4k aligned
972 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Neutron GTX*", "*" },
977 * Corsair Force GT & GS SSDs
978 * 4k optimised & trim only works in 4k requests + 4k aligned
980 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force G*", "*" },
986 * 4k optimised & trim only works in 4k requests + 4k aligned
988 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "M4-CT???M4SSD2*", "*" },
993 * Crucial RealSSD C300 SSDs
996 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "C300-CTFDDAC???MAG*",
997 "*" }, /*quirks*/DA_Q_4K
1001 * Intel 320 Series SSDs
1002 * 4k optimised & trim only works in 4k requests + 4k aligned
1004 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2CW*", "*" },
1009 * Intel 330 Series SSDs
1010 * 4k optimised & trim only works in 4k requests + 4k aligned
1012 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2CT*", "*" },
1017 * Intel 510 Series SSDs
1018 * 4k optimised & trim only works in 4k requests + 4k aligned
1020 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2MH*", "*" },
1025 * Intel 520 Series SSDs
1026 * 4k optimised & trim only works in 4k requests + 4k aligned
1028 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2BW*", "*" },
1033 * Intel X25-M Series SSDs
1034 * 4k optimised & trim only works in 4k requests + 4k aligned
1036 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2M*", "*" },
1041 * Kingston E100 Series SSDs
1042 * 4k optimised & trim only works in 4k requests + 4k aligned
1044 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SE100S3*", "*" },
1049 * Kingston HyperX 3k SSDs
1050 * 4k optimised & trim only works in 4k requests + 4k aligned
1052 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SH103S3*", "*" },
1057 * Marvell SSDs (entry taken from OpenSolaris)
1058 * 4k optimised & trim only works in 4k requests + 4k aligned
1060 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "MARVELL SD88SA02*", "*" },
1065 * OCZ Agility 2 SSDs
1066 * 4k optimised & trim only works in 4k requests + 4k aligned
1068 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY2*", "*" },
1073 * OCZ Agility 3 SSDs
1074 * 4k optimised & trim only works in 4k requests + 4k aligned
1076 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-AGILITY3*", "*" },
1081 * OCZ Deneva R Series SSDs
1082 * 4k optimised & trim only works in 4k requests + 4k aligned
1084 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "DENRSTE251M45*", "*" },
1089 * OCZ Vertex 2 SSDs (inc pro series)
1090 * 4k optimised & trim only works in 4k requests + 4k aligned
1092 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ?VERTEX2*", "*" },
1098 * 4k optimised & trim only works in 4k requests + 4k aligned
1100 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX3*", "*" },
1106 * 4k optimised & trim only works in 4k requests + 4k aligned
1108 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX4*", "*" },
1113 * Samsung 830 Series SSDs
1114 * 4k optimised & trim only works in 4k requests + 4k aligned
1116 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG SSD 830 Series*", "*" },
1122 * 4k optimised & trim only works in 4k requests + 4k aligned
1124 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 840*", "*" },
1129 * Samsung 843T Series SSDs
1132 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG MZ7WD*", "*" },
1138 * 4k optimised & trim only works in 4k requests + 4k aligned
1140 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 850*", "*" },
1145 * Samsung PM853T Series SSDs
1148 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG MZ7GE*", "*" },
1153 * SuperTalent TeraDrive CT SSDs
1154 * 4k optimised & trim only works in 4k requests + 4k aligned
1156 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "FTM??CT25H*", "*" },
1161 * XceedIOPS SATA SSDs
1164 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SG9XCS2D*", "*" },
1169 static disk_strategy_t dastrategy;
1170 static dumper_t dadump;
1171 static periph_init_t dainit;
1172 static void daasync(void *callback_arg, u_int32_t code,
1173 struct cam_path *path, void *arg);
1174 static void dasysctlinit(void *context, int pending);
1175 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
1176 static int dadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
1177 static int dadeletemaxsysctl(SYSCTL_HANDLER_ARGS);
1178 static void dadeletemethodset(struct da_softc *softc,
1179 da_delete_methods delete_method);
1180 static off_t dadeletemaxsize(struct da_softc *softc,
1181 da_delete_methods delete_method);
1182 static void dadeletemethodchoose(struct da_softc *softc,
1183 da_delete_methods default_method);
1184 static void daprobedone(struct cam_periph *periph, union ccb *ccb);
1186 static periph_ctor_t daregister;
1187 static periph_dtor_t dacleanup;
1188 static periph_start_t dastart;
1189 static periph_oninv_t daoninvalidate;
1190 static void dadone(struct cam_periph *periph,
1191 union ccb *done_ccb);
1192 static int daerror(union ccb *ccb, u_int32_t cam_flags,
1193 u_int32_t sense_flags);
1194 static void daprevent(struct cam_periph *periph, int action);
1195 static void dareprobe(struct cam_periph *periph);
1196 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
1198 struct scsi_read_capacity_data_long *rcaplong,
1200 static timeout_t dasendorderedtag;
1201 static void dashutdown(void *arg, int howto);
1202 static timeout_t damediapoll;
1204 #ifndef DA_DEFAULT_POLL_PERIOD
1205 #define DA_DEFAULT_POLL_PERIOD 3
1208 #ifndef DA_DEFAULT_TIMEOUT
1209 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
1212 #ifndef DA_DEFAULT_RETRY
1213 #define DA_DEFAULT_RETRY 4
1216 #ifndef DA_DEFAULT_SEND_ORDERED
1217 #define DA_DEFAULT_SEND_ORDERED 1
1220 #define DA_SIO (softc->sort_io_queue >= 0 ? \
1221 softc->sort_io_queue : cam_sort_io_queues)
1223 static int da_poll_period = DA_DEFAULT_POLL_PERIOD;
1224 static int da_retry_count = DA_DEFAULT_RETRY;
1225 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
1226 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
1228 static SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
1229 "CAM Direct Access Disk driver");
1230 SYSCTL_INT(_kern_cam_da, OID_AUTO, poll_period, CTLFLAG_RW,
1231 &da_poll_period, 0, "Media polling period in seconds");
1232 TUNABLE_INT("kern.cam.da.poll_period", &da_poll_period);
1233 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
1234 &da_retry_count, 0, "Normal I/O retry count");
1235 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
1236 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
1237 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
1238 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
1239 SYSCTL_INT(_kern_cam_da, OID_AUTO, send_ordered, CTLFLAG_RW,
1240 &da_send_ordered, 0, "Send Ordered Tags");
1241 TUNABLE_INT("kern.cam.da.send_ordered", &da_send_ordered);
1244 * DA_ORDEREDTAG_INTERVAL determines how often, relative
1245 * to the default timeout, we check to see whether an ordered
1246 * tagged transaction is appropriate to prevent simple tag
1247 * starvation. Since we'd like to ensure that there is at least
1248 * 1/2 of the timeout length left for a starved transaction to
1249 * complete after we've sent an ordered tag, we must poll at least
1250 * four times in every timeout period. This takes care of the worst
1251 * case where a starved transaction starts during an interval that
1252 * meets the requirement "don't send an ordered tag" test so it takes
1253 * us two intervals to determine that a tag must be sent.
1255 #ifndef DA_ORDEREDTAG_INTERVAL
1256 #define DA_ORDEREDTAG_INTERVAL 4
1259 static struct periph_driver dadriver =
1262 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
1265 PERIPHDRIVER_DECLARE(da, dadriver);
1267 static MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
1270 daopen(struct disk *dp)
1272 struct cam_periph *periph;
1273 struct da_softc *softc;
1276 periph = (struct cam_periph *)dp->d_drv1;
1277 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1281 cam_periph_lock(periph);
1282 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
1283 cam_periph_unlock(periph);
1284 cam_periph_release(periph);
1288 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1291 softc = (struct da_softc *)periph->softc;
1294 /* Wait for the disk size update. */
1295 error = cam_periph_sleep(periph, &softc->disk->d_mediasize, PRIBIO,
1298 xpt_print(periph->path, "unable to retrieve capacity data\n");
1300 if (periph->flags & CAM_PERIPH_INVALID)
1303 if (error == 0 && (softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1304 (softc->quirks & DA_Q_NO_PREVENT) == 0)
1305 daprevent(periph, PR_PREVENT);
1308 softc->flags &= ~DA_FLAG_PACK_INVALID;
1309 softc->flags |= DA_FLAG_OPEN;
1312 cam_periph_unhold(periph);
1313 cam_periph_unlock(periph);
1316 cam_periph_release(periph);
1322 daclose(struct disk *dp)
1324 struct cam_periph *periph;
1325 struct da_softc *softc;
1329 periph = (struct cam_periph *)dp->d_drv1;
1330 softc = (struct da_softc *)periph->softc;
1331 cam_periph_lock(periph);
1332 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1335 if (cam_periph_hold(periph, PRIBIO) == 0) {
1337 /* Flush disk cache. */
1338 if ((softc->flags & DA_FLAG_DIRTY) != 0 &&
1339 (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0 &&
1340 (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
1341 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1342 scsi_synchronize_cache(&ccb->csio, /*retries*/1,
1343 /*cbfcnp*/dadone, MSG_SIMPLE_Q_TAG,
1344 /*begin_lba*/0, /*lb_count*/0, SSD_FULL_SIZE,
1346 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
1347 /*sense_flags*/SF_RETRY_UA | SF_QUIET_IR,
1348 softc->disk->d_devstat);
1350 softc->flags &= ~DA_FLAG_DIRTY;
1351 xpt_release_ccb(ccb);
1354 /* Allow medium removal. */
1355 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1356 (softc->quirks & DA_Q_NO_PREVENT) == 0)
1357 daprevent(periph, PR_ALLOW);
1359 cam_periph_unhold(periph);
1363 * If we've got removeable media, mark the blocksize as
1364 * unavailable, since it could change when new media is
1367 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0)
1368 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1370 softc->flags &= ~DA_FLAG_OPEN;
1371 while (softc->refcount != 0)
1372 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "daclose", 1);
1373 cam_periph_unlock(periph);
1374 cam_periph_release(periph);
1379 daschedule(struct cam_periph *periph)
1381 struct da_softc *softc = (struct da_softc *)periph->softc;
1383 if (softc->state != DA_STATE_NORMAL)
1386 /* Check if we have more work to do. */
1387 if (bioq_first(&softc->bio_queue) ||
1388 (!softc->delete_running && bioq_first(&softc->delete_queue)) ||
1390 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1395 * Actually translate the requested transfer into one the physical driver
1396 * can understand. The transfer is described by a buf and will include
1397 * only one physical transfer.
1400 dastrategy(struct bio *bp)
1402 struct cam_periph *periph;
1403 struct da_softc *softc;
1405 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1406 softc = (struct da_softc *)periph->softc;
1408 cam_periph_lock(periph);
1411 * If the device has been made invalid, error out
1413 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1414 cam_periph_unlock(periph);
1415 biofinish(bp, NULL, ENXIO);
1419 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
1422 * Place it in the queue of disk activities for this disk
1424 if (bp->bio_cmd == BIO_DELETE) {
1425 bioq_disksort(&softc->delete_queue, bp);
1426 } else if (DA_SIO) {
1427 bioq_disksort(&softc->bio_queue, bp);
1429 bioq_insert_tail(&softc->bio_queue, bp);
1433 * Schedule ourselves for performing the work.
1436 cam_periph_unlock(periph);
1442 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1444 struct cam_periph *periph;
1445 struct da_softc *softc;
1447 struct ccb_scsiio csio;
1452 periph = dp->d_drv1;
1453 softc = (struct da_softc *)periph->softc;
1454 cam_periph_lock(periph);
1455 secsize = softc->params.secsize;
1457 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1458 cam_periph_unlock(periph);
1463 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1464 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1465 scsi_read_write(&csio,
1469 /*read*/SCSI_RW_WRITE,
1471 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1474 /*data_ptr*/(u_int8_t *) virtual,
1475 /*dxfer_len*/length,
1476 /*sense_len*/SSD_FULL_SIZE,
1477 da_default_timeout * 1000);
1478 xpt_polled_action((union ccb *)&csio);
1480 error = cam_periph_error((union ccb *)&csio,
1481 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1482 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1483 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1484 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1486 printf("Aborting dump due to I/O error.\n");
1487 cam_periph_unlock(periph);
1492 * Sync the disk cache contents to the physical media.
1494 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1496 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1497 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1498 scsi_synchronize_cache(&csio,
1502 /*begin_lba*/0,/* Cover the whole disk */
1506 xpt_polled_action((union ccb *)&csio);
1508 error = cam_periph_error((union ccb *)&csio,
1509 0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL);
1510 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1511 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1512 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1514 xpt_print(periph->path, "Synchronize cache failed\n");
1516 cam_periph_unlock(periph);
1521 dagetattr(struct bio *bp)
1524 struct cam_periph *periph;
1526 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1527 cam_periph_lock(periph);
1528 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1530 cam_periph_unlock(periph);
1532 bp->bio_completed = bp->bio_length;
1542 * Install a global async callback. This callback will
1543 * receive async callbacks like "new device found".
1545 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1547 if (status != CAM_REQ_CMP) {
1548 printf("da: Failed to attach master async callback "
1549 "due to status 0x%x!\n", status);
1550 } else if (da_send_ordered) {
1552 /* Register our shutdown event handler */
1553 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1554 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1555 printf("dainit: shutdown event registration failed!\n");
1560 * Callback from GEOM, called when it has finished cleaning up its
1564 dadiskgonecb(struct disk *dp)
1566 struct cam_periph *periph;
1568 periph = (struct cam_periph *)dp->d_drv1;
1569 cam_periph_release(periph);
1573 daoninvalidate(struct cam_periph *periph)
1575 struct da_softc *softc;
1577 softc = (struct da_softc *)periph->softc;
1580 * De-register any async callbacks.
1582 xpt_register_async(0, daasync, periph, periph->path);
1584 softc->flags |= DA_FLAG_PACK_INVALID;
1587 * Return all queued I/O with ENXIO.
1588 * XXX Handle any transactions queued to the card
1589 * with XPT_ABORT_CCB.
1591 bioq_flush(&softc->bio_queue, NULL, ENXIO);
1592 bioq_flush(&softc->delete_queue, NULL, ENXIO);
1595 * Tell GEOM that we've gone away, we'll get a callback when it is
1596 * done cleaning up its resources.
1598 disk_gone(softc->disk);
1602 dacleanup(struct cam_periph *periph)
1604 struct da_softc *softc;
1606 softc = (struct da_softc *)periph->softc;
1608 cam_periph_unlock(periph);
1611 * If we can't free the sysctl tree, oh well...
1613 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1614 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1615 xpt_print(periph->path, "can't remove sysctl context\n");
1618 callout_drain(&softc->mediapoll_c);
1619 disk_destroy(softc->disk);
1620 callout_drain(&softc->sendordered_c);
1621 free(softc, M_DEVBUF);
1622 cam_periph_lock(periph);
1626 daasync(void *callback_arg, u_int32_t code,
1627 struct cam_path *path, void *arg)
1629 struct cam_periph *periph;
1630 struct da_softc *softc;
1632 periph = (struct cam_periph *)callback_arg;
1634 case AC_FOUND_DEVICE:
1636 struct ccb_getdev *cgd;
1639 cgd = (struct ccb_getdev *)arg;
1643 if (cgd->protocol != PROTO_SCSI)
1646 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1647 && SID_TYPE(&cgd->inq_data) != T_RBC
1648 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1652 * Allocate a peripheral instance for
1653 * this device and start the probe
1656 status = cam_periph_alloc(daregister, daoninvalidate,
1658 "da", CAM_PERIPH_BIO,
1660 AC_FOUND_DEVICE, cgd);
1662 if (status != CAM_REQ_CMP
1663 && status != CAM_REQ_INPROG)
1664 printf("daasync: Unable to attach to new device "
1665 "due to status 0x%x\n", status);
1668 case AC_ADVINFO_CHANGED:
1672 buftype = (uintptr_t)arg;
1673 if (buftype == CDAI_TYPE_PHYS_PATH) {
1674 struct da_softc *softc;
1676 softc = periph->softc;
1677 disk_attr_changed(softc->disk, "GEOM::physpath",
1682 case AC_UNIT_ATTENTION:
1685 int error_code, sense_key, asc, ascq;
1687 softc = (struct da_softc *)periph->softc;
1688 ccb = (union ccb *)arg;
1691 * Handle all UNIT ATTENTIONs except our own,
1692 * as they will be handled by daerror().
1694 if (xpt_path_periph(ccb->ccb_h.path) != periph &&
1695 scsi_extract_sense_ccb(ccb,
1696 &error_code, &sense_key, &asc, &ascq)) {
1697 if (asc == 0x2A && ascq == 0x09) {
1698 xpt_print(ccb->ccb_h.path,
1699 "Capacity data has changed\n");
1700 softc->flags &= ~DA_FLAG_PROBED;
1702 } else if (asc == 0x28 && ascq == 0x00) {
1703 softc->flags &= ~DA_FLAG_PROBED;
1704 disk_media_changed(softc->disk, M_NOWAIT);
1705 } else if (asc == 0x3F && ascq == 0x03) {
1706 xpt_print(ccb->ccb_h.path,
1707 "INQUIRY data has changed\n");
1708 softc->flags &= ~DA_FLAG_PROBED;
1712 cam_periph_async(periph, code, path, arg);
1716 softc = (struct da_softc *)periph->softc;
1718 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
1727 struct ccb_hdr *ccbh;
1729 softc = (struct da_softc *)periph->softc;
1731 * Don't fail on the expected unit attention
1734 softc->flags |= DA_FLAG_RETRY_UA;
1735 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1736 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1742 cam_periph_async(periph, code, path, arg);
1746 dasysctlinit(void *context, int pending)
1748 struct cam_periph *periph;
1749 struct da_softc *softc;
1750 char tmpstr[80], tmpstr2[80];
1751 struct ccb_trans_settings cts;
1753 periph = (struct cam_periph *)context;
1755 * periph was held for us when this task was enqueued
1757 if (periph->flags & CAM_PERIPH_INVALID) {
1758 cam_periph_release(periph);
1762 softc = (struct da_softc *)periph->softc;
1763 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1764 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1766 sysctl_ctx_init(&softc->sysctl_ctx);
1767 softc->flags |= DA_FLAG_SCTX_INIT;
1768 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1769 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1770 CTLFLAG_RD, 0, tmpstr);
1771 if (softc->sysctl_tree == NULL) {
1772 printf("dasysctlinit: unable to allocate sysctl tree\n");
1773 cam_periph_release(periph);
1778 * Now register the sysctl handler, so the user can change the value on
1781 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1782 OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RW,
1783 softc, 0, dadeletemethodsysctl, "A",
1784 "BIO_DELETE execution method");
1785 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1786 OID_AUTO, "delete_max", CTLTYPE_U64 | CTLFLAG_RW,
1787 softc, 0, dadeletemaxsysctl, "Q",
1788 "Maximum BIO_DELETE size");
1789 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1790 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1791 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1792 "Minimum CDB size");
1793 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1794 OID_AUTO, "sort_io_queue", CTLFLAG_RW, &softc->sort_io_queue, 0,
1795 "Sort IO queue to try and optimise disk access patterns");
1797 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1798 SYSCTL_CHILDREN(softc->sysctl_tree),
1802 &softc->error_inject,
1804 "error_inject leaf");
1808 * Add some addressing info.
1810 memset(&cts, 0, sizeof (cts));
1811 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1812 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1813 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1814 cam_periph_lock(periph);
1815 xpt_action((union ccb *)&cts);
1816 cam_periph_unlock(periph);
1817 if (cts.ccb_h.status != CAM_REQ_CMP) {
1818 cam_periph_release(periph);
1821 if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
1822 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1823 if (fc->valid & CTS_FC_VALID_WWPN) {
1824 softc->wwpn = fc->wwpn;
1825 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1826 SYSCTL_CHILDREN(softc->sysctl_tree),
1827 OID_AUTO, "wwpn", CTLFLAG_RD,
1828 &softc->wwpn, "World Wide Port Name");
1831 cam_periph_release(periph);
1835 dadeletemaxsysctl(SYSCTL_HANDLER_ARGS)
1839 struct da_softc *softc;
1841 softc = (struct da_softc *)arg1;
1843 value = softc->disk->d_delmaxsize;
1844 error = sysctl_handle_64(oidp, &value, 0, req);
1845 if ((error != 0) || (req->newptr == NULL))
1848 /* only accept values smaller than the calculated value */
1849 if (value > dadeletemaxsize(softc, softc->delete_method)) {
1852 softc->disk->d_delmaxsize = value;
1858 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1862 value = *(int *)arg1;
1864 error = sysctl_handle_int(oidp, &value, 0, req);
1867 || (req->newptr == NULL))
1871 * Acceptable values here are 6, 10, 12 or 16.
1875 else if ((value > 6)
1878 else if ((value > 10)
1881 else if (value > 12)
1884 *(int *)arg1 = value;
1890 dadeletemethodset(struct da_softc *softc, da_delete_methods delete_method)
1894 softc->delete_method = delete_method;
1895 softc->disk->d_delmaxsize = dadeletemaxsize(softc, delete_method);
1896 softc->delete_func = da_delete_functions[delete_method];
1898 if (softc->delete_method > DA_DELETE_DISABLE)
1899 softc->disk->d_flags |= DISKFLAG_CANDELETE;
1901 softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
1905 dadeletemaxsize(struct da_softc *softc, da_delete_methods delete_method)
1909 switch(delete_method) {
1910 case DA_DELETE_UNMAP:
1911 sectors = (off_t)softc->unmap_max_lba;
1913 case DA_DELETE_ATA_TRIM:
1914 sectors = (off_t)ATA_DSM_RANGE_MAX * softc->trim_max_ranges;
1916 case DA_DELETE_WS16:
1917 sectors = omin(softc->ws_max_blks, WS16_MAX_BLKS);
1919 case DA_DELETE_ZERO:
1920 case DA_DELETE_WS10:
1921 sectors = omin(softc->ws_max_blks, WS10_MAX_BLKS);
1927 return (off_t)softc->params.secsize *
1928 omin(sectors, softc->params.sectors);
1932 daprobedone(struct cam_periph *periph, union ccb *ccb)
1934 struct da_softc *softc;
1936 softc = (struct da_softc *)periph->softc;
1938 dadeletemethodchoose(softc, DA_DELETE_NONE);
1940 if (bootverbose && (softc->flags & DA_FLAG_ANNOUNCED) == 0) {
1944 snprintf(buf, sizeof(buf), "Delete methods: <");
1946 for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
1947 if (softc->delete_available & (1 << i)) {
1949 strlcat(buf, ",", sizeof(buf));
1953 strlcat(buf, da_delete_method_names[i],
1955 if (i == softc->delete_method) {
1956 strlcat(buf, "(*)", sizeof(buf));
1961 if (softc->delete_method == DA_DELETE_NONE)
1962 strlcat(buf, "NONE(*)", sizeof(buf));
1964 strlcat(buf, "DISABLED(*)", sizeof(buf));
1966 strlcat(buf, ">", sizeof(buf));
1967 printf("%s%d: %s\n", periph->periph_name,
1968 periph->unit_number, buf);
1972 * Since our peripheral may be invalidated by an error
1973 * above or an external event, we must release our CCB
1974 * before releasing the probe lock on the peripheral.
1975 * The peripheral will only go away once the last lock
1976 * is removed, and we need it around for the CCB release
1979 xpt_release_ccb(ccb);
1980 softc->state = DA_STATE_NORMAL;
1981 softc->flags |= DA_FLAG_PROBED;
1983 wakeup(&softc->disk->d_mediasize);
1984 if ((softc->flags & DA_FLAG_ANNOUNCED) == 0) {
1985 softc->flags |= DA_FLAG_ANNOUNCED;
1986 cam_periph_unhold(periph);
1988 cam_periph_release_locked(periph);
1992 dadeletemethodchoose(struct da_softc *softc, da_delete_methods default_method)
1994 int i, delete_method;
1996 delete_method = default_method;
1999 * Use the pre-defined order to choose the best
2000 * performing delete.
2002 for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
2003 if (softc->delete_available & (1 << i)) {
2004 dadeletemethodset(softc, i);
2008 dadeletemethodset(softc, delete_method);
2012 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
2016 struct da_softc *softc;
2017 int i, error, methods, value;
2019 softc = (struct da_softc *)arg1;
2021 value = softc->delete_method;
2022 if (value < 0 || value > DA_DELETE_MAX)
2025 p = da_delete_method_names[value];
2026 strncpy(buf, p, sizeof(buf));
2027 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2028 if (error != 0 || req->newptr == NULL)
2030 methods = softc->delete_available | (1 << DA_DELETE_DISABLE);
2031 for (i = 0; i <= DA_DELETE_MAX; i++) {
2032 if (!(methods & (1 << i)) ||
2033 strcmp(buf, da_delete_method_names[i]) != 0)
2035 dadeletemethodset(softc, i);
2042 daregister(struct cam_periph *periph, void *arg)
2044 struct da_softc *softc;
2045 struct ccb_pathinq cpi;
2046 struct ccb_getdev *cgd;
2050 cgd = (struct ccb_getdev *)arg;
2052 printf("daregister: no getdev CCB, can't register device\n");
2053 return(CAM_REQ_CMP_ERR);
2056 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
2059 if (softc == NULL) {
2060 printf("daregister: Unable to probe new device. "
2061 "Unable to allocate softc\n");
2062 return(CAM_REQ_CMP_ERR);
2065 LIST_INIT(&softc->pending_ccbs);
2066 softc->state = DA_STATE_PROBE_RC;
2067 bioq_init(&softc->bio_queue);
2068 bioq_init(&softc->delete_queue);
2069 bioq_init(&softc->delete_run_queue);
2070 if (SID_IS_REMOVABLE(&cgd->inq_data))
2071 softc->flags |= DA_FLAG_PACK_REMOVABLE;
2072 softc->unmap_max_ranges = UNMAP_MAX_RANGES;
2073 softc->unmap_max_lba = UNMAP_RANGE_MAX;
2074 softc->ws_max_blks = WS16_MAX_BLKS;
2075 softc->trim_max_ranges = ATA_TRIM_MAX_RANGES;
2076 softc->sort_io_queue = -1;
2078 periph->softc = softc;
2081 * See if this device has any quirks.
2083 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
2084 (caddr_t)da_quirk_table,
2085 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
2086 sizeof(*da_quirk_table), scsi_inquiry_match);
2089 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
2091 softc->quirks = DA_Q_NONE;
2093 /* Check if the SIM does not want 6 byte commands */
2094 bzero(&cpi, sizeof(cpi));
2095 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2096 cpi.ccb_h.func_code = XPT_PATH_INQ;
2097 xpt_action((union ccb *)&cpi);
2098 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
2099 softc->quirks |= DA_Q_NO_6_BYTE;
2101 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
2104 * Take an exclusive refcount on the periph while dastart is called
2105 * to finish the probe. The reference will be dropped in dadone at
2108 (void)cam_periph_hold(periph, PRIBIO);
2111 * Schedule a periodic event to occasionally send an
2112 * ordered tag to a device.
2114 callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0);
2115 callout_reset(&softc->sendordered_c,
2116 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2117 dasendorderedtag, softc);
2119 cam_periph_unlock(periph);
2121 * RBC devices don't have to support READ(6), only READ(10).
2123 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
2124 softc->minimum_cmd_size = 10;
2126 softc->minimum_cmd_size = 6;
2129 * Load the user's default, if any.
2131 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
2132 periph->unit_number);
2133 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
2136 * 6, 10, 12 and 16 are the currently permissible values.
2138 if (softc->minimum_cmd_size < 6)
2139 softc->minimum_cmd_size = 6;
2140 else if ((softc->minimum_cmd_size > 6)
2141 && (softc->minimum_cmd_size <= 10))
2142 softc->minimum_cmd_size = 10;
2143 else if ((softc->minimum_cmd_size > 10)
2144 && (softc->minimum_cmd_size <= 12))
2145 softc->minimum_cmd_size = 12;
2146 else if (softc->minimum_cmd_size > 12)
2147 softc->minimum_cmd_size = 16;
2149 /* Predict whether device may support READ CAPACITY(16). */
2150 if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3 &&
2151 (softc->quirks & DA_Q_NO_RC16) == 0) {
2152 softc->flags |= DA_FLAG_CAN_RC16;
2153 softc->state = DA_STATE_PROBE_RC16;
2157 * Register this media as a disk.
2159 softc->disk = disk_alloc();
2160 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
2161 periph->unit_number, 0,
2162 DEVSTAT_BS_UNAVAILABLE,
2163 SID_TYPE(&cgd->inq_data) |
2164 XPORT_DEVSTAT_TYPE(cpi.transport),
2165 DEVSTAT_PRIORITY_DISK);
2166 softc->disk->d_open = daopen;
2167 softc->disk->d_close = daclose;
2168 softc->disk->d_strategy = dastrategy;
2169 softc->disk->d_dump = dadump;
2170 softc->disk->d_getattr = dagetattr;
2171 softc->disk->d_gone = dadiskgonecb;
2172 softc->disk->d_name = "da";
2173 softc->disk->d_drv1 = periph;
2175 softc->maxio = DFLTPHYS; /* traditional default */
2176 else if (cpi.maxio > MAXPHYS)
2177 softc->maxio = MAXPHYS; /* for safety */
2179 softc->maxio = cpi.maxio;
2180 softc->disk->d_maxsize = softc->maxio;
2181 softc->disk->d_unit = periph->unit_number;
2182 softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION;
2183 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
2184 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
2185 if ((cpi.hba_misc & PIM_UNMAPPED) != 0)
2186 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
2187 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
2188 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
2189 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
2190 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
2191 cgd->inq_data.product, sizeof(cgd->inq_data.product),
2192 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
2193 softc->disk->d_hba_vendor = cpi.hba_vendor;
2194 softc->disk->d_hba_device = cpi.hba_device;
2195 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
2196 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
2199 * Acquire a reference to the periph before we register with GEOM.
2200 * We'll release this reference once GEOM calls us back (via
2201 * dadiskgonecb()) telling us that our provider has been freed.
2203 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
2204 xpt_print(periph->path, "%s: lost periph during "
2205 "registration!\n", __func__);
2206 cam_periph_lock(periph);
2207 return (CAM_REQ_CMP_ERR);
2210 disk_create(softc->disk, DISK_VERSION);
2211 cam_periph_lock(periph);
2214 * Add async callbacks for events of interest.
2215 * I don't bother checking if this fails as,
2216 * in most cases, the system will function just
2217 * fine without them and the only alternative
2218 * would be to not attach the device on failure.
2220 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
2221 AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION,
2222 daasync, periph, periph->path);
2225 * Emit an attribute changed notification just in case
2226 * physical path information arrived before our async
2227 * event handler was registered, but after anyone attaching
2228 * to our disk device polled it.
2230 disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
2233 * Schedule a periodic media polling events.
2235 callout_init_mtx(&softc->mediapoll_c, cam_periph_mtx(periph), 0);
2236 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
2237 (cgd->inq_flags & SID_AEN) == 0 &&
2238 da_poll_period != 0)
2239 callout_reset(&softc->mediapoll_c, da_poll_period * hz,
2240 damediapoll, periph);
2242 xpt_schedule(periph, CAM_PRIORITY_DEV);
2244 return(CAM_REQ_CMP);
2248 dastart(struct cam_periph *periph, union ccb *start_ccb)
2250 struct da_softc *softc;
2252 softc = (struct da_softc *)periph->softc;
2254 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
2257 switch (softc->state) {
2258 case DA_STATE_NORMAL:
2263 /* Run BIO_DELETE if not running yet. */
2264 if (!softc->delete_running &&
2265 (bp = bioq_first(&softc->delete_queue)) != NULL) {
2266 if (softc->delete_func != NULL) {
2267 softc->delete_func(periph, start_ccb, bp);
2270 bioq_flush(&softc->delete_queue, NULL, 0);
2275 /* Run regular command. */
2276 bp = bioq_takefirst(&softc->bio_queue);
2280 scsi_test_unit_ready(&start_ccb->csio,
2281 /*retries*/ da_retry_count,
2285 da_default_timeout * 1000);
2286 start_ccb->ccb_h.ccb_bp = NULL;
2287 start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
2288 xpt_action(start_ccb);
2290 xpt_release_ccb(start_ccb);
2295 cam_periph_release_locked(periph);
2298 if ((bp->bio_flags & BIO_ORDERED) != 0 ||
2299 (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
2300 softc->flags &= ~DA_FLAG_NEED_OTAG;
2301 softc->flags |= DA_FLAG_WAS_OTAG;
2302 tag_code = MSG_ORDERED_Q_TAG;
2304 tag_code = MSG_SIMPLE_Q_TAG;
2307 switch (bp->bio_cmd) {
2309 softc->flags |= DA_FLAG_DIRTY;
2312 scsi_read_write(&start_ccb->csio,
2313 /*retries*/da_retry_count,
2315 /*tag_action*/tag_code,
2316 /*read_op*/(bp->bio_cmd == BIO_READ ?
2317 SCSI_RW_READ : SCSI_RW_WRITE) |
2318 ((bp->bio_flags & BIO_UNMAPPED) != 0 ?
2321 softc->minimum_cmd_size,
2322 /*lba*/bp->bio_pblkno,
2323 /*block_count*/bp->bio_bcount /
2324 softc->params.secsize,
2325 /*data_ptr*/ (bp->bio_flags &
2326 BIO_UNMAPPED) != 0 ? (void *)bp :
2328 /*dxfer_len*/ bp->bio_bcount,
2329 /*sense_len*/SSD_FULL_SIZE,
2330 da_default_timeout * 1000);
2334 * BIO_FLUSH doesn't currently communicate
2335 * range data, so we synchronize the cache
2336 * over the whole disk. We also force
2337 * ordered tag semantics the flush applies
2338 * to all previously queued I/O.
2340 scsi_synchronize_cache(&start_ccb->csio,
2347 da_default_timeout*1000);
2350 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
2351 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
2354 LIST_INSERT_HEAD(&softc->pending_ccbs,
2355 &start_ccb->ccb_h, periph_links.le);
2357 /* We expect a unit attention from this device */
2358 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
2359 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
2360 softc->flags &= ~DA_FLAG_RETRY_UA;
2363 start_ccb->ccb_h.ccb_bp = bp;
2365 cam_periph_unlock(periph);
2366 xpt_action(start_ccb);
2367 cam_periph_lock(periph);
2370 /* May have more work to do, so ensure we stay scheduled */
2374 case DA_STATE_PROBE_RC:
2376 struct scsi_read_capacity_data *rcap;
2378 rcap = (struct scsi_read_capacity_data *)
2379 malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
2381 printf("dastart: Couldn't malloc read_capacity data\n");
2382 /* da_free_periph??? */
2385 scsi_read_capacity(&start_ccb->csio,
2386 /*retries*/da_retry_count,
2392 start_ccb->ccb_h.ccb_bp = NULL;
2393 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC;
2394 xpt_action(start_ccb);
2397 case DA_STATE_PROBE_RC16:
2399 struct scsi_read_capacity_data_long *rcaplong;
2401 rcaplong = (struct scsi_read_capacity_data_long *)
2402 malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
2403 if (rcaplong == NULL) {
2404 printf("dastart: Couldn't malloc read_capacity data\n");
2405 /* da_free_periph??? */
2408 scsi_read_capacity_16(&start_ccb->csio,
2409 /*retries*/ da_retry_count,
2411 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2415 /*rcap_buf*/ (uint8_t *)rcaplong,
2416 /*rcap_buf_len*/ sizeof(*rcaplong),
2417 /*sense_len*/ SSD_FULL_SIZE,
2418 /*timeout*/ da_default_timeout * 1000);
2419 start_ccb->ccb_h.ccb_bp = NULL;
2420 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC16;
2421 xpt_action(start_ccb);
2424 case DA_STATE_PROBE_LBP:
2426 struct scsi_vpd_logical_block_prov *lbp;
2428 if (!scsi_vpd_supported_page(periph, SVPD_LBP)) {
2430 * If we get here we don't support any SBC-3 delete
2431 * methods with UNMAP as the Logical Block Provisioning
2432 * VPD page support is required for devices which
2433 * support it according to T10/1799-D Revision 31
2434 * however older revisions of the spec don't mandate
2435 * this so we currently don't remove these methods
2436 * from the available set.
2438 softc->state = DA_STATE_PROBE_BLK_LIMITS;
2442 lbp = (struct scsi_vpd_logical_block_prov *)
2443 malloc(sizeof(*lbp), M_SCSIDA, M_NOWAIT|M_ZERO);
2446 printf("dastart: Couldn't malloc lbp data\n");
2447 /* da_free_periph??? */
2451 scsi_inquiry(&start_ccb->csio,
2452 /*retries*/da_retry_count,
2454 /*tag_action*/MSG_SIMPLE_Q_TAG,
2455 /*inq_buf*/(u_int8_t *)lbp,
2456 /*inq_len*/sizeof(*lbp),
2458 /*page_code*/SVPD_LBP,
2459 /*sense_len*/SSD_MIN_SIZE,
2460 /*timeout*/da_default_timeout * 1000);
2461 start_ccb->ccb_h.ccb_bp = NULL;
2462 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_LBP;
2463 xpt_action(start_ccb);
2466 case DA_STATE_PROBE_BLK_LIMITS:
2468 struct scsi_vpd_block_limits *block_limits;
2470 if (!scsi_vpd_supported_page(periph, SVPD_BLOCK_LIMITS)) {
2471 /* Not supported skip to next probe */
2472 softc->state = DA_STATE_PROBE_BDC;
2476 block_limits = (struct scsi_vpd_block_limits *)
2477 malloc(sizeof(*block_limits), M_SCSIDA, M_NOWAIT|M_ZERO);
2479 if (block_limits == NULL) {
2480 printf("dastart: Couldn't malloc block_limits data\n");
2481 /* da_free_periph??? */
2485 scsi_inquiry(&start_ccb->csio,
2486 /*retries*/da_retry_count,
2488 /*tag_action*/MSG_SIMPLE_Q_TAG,
2489 /*inq_buf*/(u_int8_t *)block_limits,
2490 /*inq_len*/sizeof(*block_limits),
2492 /*page_code*/SVPD_BLOCK_LIMITS,
2493 /*sense_len*/SSD_MIN_SIZE,
2494 /*timeout*/da_default_timeout * 1000);
2495 start_ccb->ccb_h.ccb_bp = NULL;
2496 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BLK_LIMITS;
2497 xpt_action(start_ccb);
2500 case DA_STATE_PROBE_BDC:
2502 struct scsi_vpd_block_characteristics *bdc;
2504 if (!scsi_vpd_supported_page(periph, SVPD_BDC)) {
2505 softc->state = DA_STATE_PROBE_ATA;
2509 bdc = (struct scsi_vpd_block_characteristics *)
2510 malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
2513 printf("dastart: Couldn't malloc bdc data\n");
2514 /* da_free_periph??? */
2518 scsi_inquiry(&start_ccb->csio,
2519 /*retries*/da_retry_count,
2521 /*tag_action*/MSG_SIMPLE_Q_TAG,
2522 /*inq_buf*/(u_int8_t *)bdc,
2523 /*inq_len*/sizeof(*bdc),
2525 /*page_code*/SVPD_BDC,
2526 /*sense_len*/SSD_MIN_SIZE,
2527 /*timeout*/da_default_timeout * 1000);
2528 start_ccb->ccb_h.ccb_bp = NULL;
2529 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BDC;
2530 xpt_action(start_ccb);
2533 case DA_STATE_PROBE_ATA:
2535 struct ata_params *ata_params;
2537 if (!scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
2538 daprobedone(periph, start_ccb);
2542 ata_params = (struct ata_params*)
2543 malloc(sizeof(*ata_params), M_SCSIDA, M_NOWAIT|M_ZERO);
2545 if (ata_params == NULL) {
2546 printf("dastart: Couldn't malloc ata_params data\n");
2547 /* da_free_periph??? */
2551 scsi_ata_identify(&start_ccb->csio,
2552 /*retries*/da_retry_count,
2554 /*tag_action*/MSG_SIMPLE_Q_TAG,
2555 /*data_ptr*/(u_int8_t *)ata_params,
2556 /*dxfer_len*/sizeof(*ata_params),
2557 /*sense_len*/SSD_FULL_SIZE,
2558 /*timeout*/da_default_timeout * 1000);
2559 start_ccb->ccb_h.ccb_bp = NULL;
2560 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA;
2561 xpt_action(start_ccb);
2568 * In each of the methods below, while its the caller's
2569 * responsibility to ensure the request will fit into a
2570 * single device request, we might have changed the delete
2571 * method due to the device incorrectly advertising either
2572 * its supported methods or limits.
2574 * To prevent this causing further issues we validate the
2575 * against the methods limits, and warn which would
2576 * otherwise be unnecessary.
2579 da_delete_unmap(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2581 struct da_softc *softc = (struct da_softc *)periph->softc;;
2583 uint8_t *buf = softc->unmap_buf;
2584 uint64_t lba, lastlba = (uint64_t)-1;
2585 uint64_t totalcount = 0;
2587 uint32_t lastcount = 0, c;
2588 uint32_t off, ranges = 0;
2591 * Currently this doesn't take the UNMAP
2592 * Granularity and Granularity Alignment
2593 * fields into account.
2595 * This could result in both unoptimal unmap
2596 * requests as as well as UNMAP calls unmapping
2597 * fewer LBA's than requested.
2600 softc->delete_running = 1;
2601 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2604 bioq_remove(&softc->delete_queue, bp1);
2606 bioq_insert_tail(&softc->delete_run_queue, bp1);
2607 lba = bp1->bio_pblkno;
2608 count = bp1->bio_bcount / softc->params.secsize;
2610 /* Try to extend the previous range. */
2611 if (lba == lastlba) {
2612 c = omin(count, UNMAP_RANGE_MAX - lastcount);
2614 off = ((ranges - 1) * UNMAP_RANGE_SIZE) +
2616 scsi_ulto4b(lastcount, &buf[off + 8]);
2623 c = omin(count, UNMAP_RANGE_MAX);
2624 if (totalcount + c > softc->unmap_max_lba ||
2625 ranges >= softc->unmap_max_ranges) {
2626 xpt_print(periph->path,
2627 "%s issuing short delete %ld > %ld"
2629 da_delete_method_desc[softc->delete_method],
2630 totalcount + c, softc->unmap_max_lba,
2631 ranges, softc->unmap_max_ranges);
2634 off = (ranges * UNMAP_RANGE_SIZE) + UNMAP_HEAD_SIZE;
2635 scsi_u64to8b(lba, &buf[off + 0]);
2636 scsi_ulto4b(c, &buf[off + 8]);
2644 bp1 = bioq_first(&softc->delete_queue);
2645 if (bp1 == NULL || ranges >= softc->unmap_max_ranges ||
2646 totalcount + bp1->bio_bcount /
2647 softc->params.secsize > softc->unmap_max_lba)
2650 scsi_ulto2b(ranges * 16 + 6, &buf[0]);
2651 scsi_ulto2b(ranges * 16, &buf[2]);
2653 scsi_unmap(&ccb->csio,
2654 /*retries*/da_retry_count,
2656 /*tag_action*/MSG_SIMPLE_Q_TAG,
2659 /*dxfer_len*/ ranges * 16 + 8,
2660 /*sense_len*/SSD_FULL_SIZE,
2661 da_default_timeout * 1000);
2662 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2663 ccb->ccb_h.flags |= CAM_UNLOCKED;
2667 da_delete_trim(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2669 struct da_softc *softc = (struct da_softc *)periph->softc;
2671 uint8_t *buf = softc->unmap_buf;
2672 uint64_t lastlba = (uint64_t)-1;
2675 uint32_t lastcount = 0, c, requestcount;
2676 int ranges = 0, off, block_count;
2678 softc->delete_running = 1;
2679 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2682 bioq_remove(&softc->delete_queue, bp1);
2684 bioq_insert_tail(&softc->delete_run_queue, bp1);
2685 lba = bp1->bio_pblkno;
2686 count = bp1->bio_bcount / softc->params.secsize;
2687 requestcount = count;
2689 /* Try to extend the previous range. */
2690 if (lba == lastlba) {
2691 c = omin(count, ATA_DSM_RANGE_MAX - lastcount);
2693 off = (ranges - 1) * 8;
2694 buf[off + 6] = lastcount & 0xff;
2695 buf[off + 7] = (lastcount >> 8) & 0xff;
2701 c = omin(count, ATA_DSM_RANGE_MAX);
2704 buf[off + 0] = lba & 0xff;
2705 buf[off + 1] = (lba >> 8) & 0xff;
2706 buf[off + 2] = (lba >> 16) & 0xff;
2707 buf[off + 3] = (lba >> 24) & 0xff;
2708 buf[off + 4] = (lba >> 32) & 0xff;
2709 buf[off + 5] = (lba >> 40) & 0xff;
2710 buf[off + 6] = c & 0xff;
2711 buf[off + 7] = (c >> 8) & 0xff;
2716 if (count != 0 && ranges == softc->trim_max_ranges) {
2717 xpt_print(periph->path,
2718 "%s issuing short delete %ld > %ld\n",
2719 da_delete_method_desc[softc->delete_method],
2721 (softc->trim_max_ranges - ranges) *
2727 bp1 = bioq_first(&softc->delete_queue);
2728 if (bp1 == NULL || bp1->bio_bcount / softc->params.secsize >
2729 (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX)
2733 block_count = (ranges + ATA_DSM_BLK_RANGES - 1) / ATA_DSM_BLK_RANGES;
2734 scsi_ata_trim(&ccb->csio,
2735 /*retries*/da_retry_count,
2737 /*tag_action*/MSG_SIMPLE_Q_TAG,
2740 /*dxfer_len*/block_count * ATA_DSM_BLK_SIZE,
2741 /*sense_len*/SSD_FULL_SIZE,
2742 da_default_timeout * 1000);
2743 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2744 ccb->ccb_h.flags |= CAM_UNLOCKED;
2748 * We calculate ws_max_blks here based off d_delmaxsize instead
2749 * of using softc->ws_max_blks as it is absolute max for the
2750 * device not the protocol max which may well be lower.
2753 da_delete_ws(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2755 struct da_softc *softc;
2757 uint64_t ws_max_blks;
2759 uint64_t count; /* forward compat with WS32 */
2761 softc = (struct da_softc *)periph->softc;
2762 ws_max_blks = softc->disk->d_delmaxsize / softc->params.secsize;
2763 softc->delete_running = 1;
2764 lba = bp->bio_pblkno;
2768 bioq_remove(&softc->delete_queue, bp1);
2770 bioq_insert_tail(&softc->delete_run_queue, bp1);
2771 count += bp1->bio_bcount / softc->params.secsize;
2772 if (count > ws_max_blks) {
2773 xpt_print(periph->path,
2774 "%s issuing short delete %ld > %ld\n",
2775 da_delete_method_desc[softc->delete_method],
2776 count, ws_max_blks);
2777 count = omin(count, ws_max_blks);
2780 bp1 = bioq_first(&softc->delete_queue);
2781 if (bp1 == NULL || lba + count != bp1->bio_pblkno ||
2782 count + bp1->bio_bcount /
2783 softc->params.secsize > ws_max_blks)
2787 scsi_write_same(&ccb->csio,
2788 /*retries*/da_retry_count,
2790 /*tag_action*/MSG_SIMPLE_Q_TAG,
2791 /*byte2*/softc->delete_method ==
2792 DA_DELETE_ZERO ? 0 : SWS_UNMAP,
2793 softc->delete_method == DA_DELETE_WS16 ? 16 : 10,
2795 /*block_count*/count,
2796 /*data_ptr*/ __DECONST(void *, zero_region),
2797 /*dxfer_len*/ softc->params.secsize,
2798 /*sense_len*/SSD_FULL_SIZE,
2799 da_default_timeout * 1000);
2800 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2801 ccb->ccb_h.flags |= CAM_UNLOCKED;
2805 cmd6workaround(union ccb *ccb)
2807 struct scsi_rw_6 cmd6;
2808 struct scsi_rw_10 *cmd10;
2809 struct da_softc *softc;
2814 cdb = ccb->csio.cdb_io.cdb_bytes;
2815 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
2817 if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
2818 da_delete_methods old_method = softc->delete_method;
2821 * Typically there are two reasons for failure here
2822 * 1. Delete method was detected as supported but isn't
2823 * 2. Delete failed due to invalid params e.g. too big
2825 * While we will attempt to choose an alternative delete method
2826 * this may result in short deletes if the existing delete
2827 * requests from geom are big for the new method choosen.
2829 * This method assumes that the error which triggered this
2830 * will not retry the io otherwise a panic will occur
2832 dadeleteflag(softc, old_method, 0);
2833 dadeletemethodchoose(softc, DA_DELETE_DISABLE);
2834 if (softc->delete_method == DA_DELETE_DISABLE)
2835 xpt_print(ccb->ccb_h.path,
2836 "%s failed, disabling BIO_DELETE\n",
2837 da_delete_method_desc[old_method]);
2839 xpt_print(ccb->ccb_h.path,
2840 "%s failed, switching to %s BIO_DELETE\n",
2841 da_delete_method_desc[old_method],
2842 da_delete_method_desc[softc->delete_method]);
2844 while ((bp = bioq_takefirst(&softc->delete_run_queue)) != NULL)
2845 bioq_disksort(&softc->delete_queue, bp);
2846 bioq_disksort(&softc->delete_queue,
2847 (struct bio *)ccb->ccb_h.ccb_bp);
2848 ccb->ccb_h.ccb_bp = NULL;
2852 /* Detect unsupported PREVENT ALLOW MEDIUM REMOVAL. */
2853 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
2854 (*cdb == PREVENT_ALLOW) &&
2855 (softc->quirks & DA_Q_NO_PREVENT) == 0) {
2857 xpt_print(ccb->ccb_h.path,
2858 "PREVENT ALLOW MEDIUM REMOVAL not supported.\n");
2859 softc->quirks |= DA_Q_NO_PREVENT;
2863 /* Detect unsupported SYNCHRONIZE CACHE(10). */
2864 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
2865 (*cdb == SYNCHRONIZE_CACHE) &&
2866 (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
2868 xpt_print(ccb->ccb_h.path,
2869 "SYNCHRONIZE CACHE(10) not supported.\n");
2870 softc->quirks |= DA_Q_NO_SYNC_CACHE;
2871 softc->disk->d_flags &= ~DISKFLAG_CANFLUSHCACHE;
2875 /* Translation only possible if CDB is an array and cmd is R/W6 */
2876 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
2877 (*cdb != READ_6 && *cdb != WRITE_6))
2880 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
2881 "increasing minimum_cmd_size to 10.\n");
2882 softc->minimum_cmd_size = 10;
2884 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
2885 cmd10 = (struct scsi_rw_10 *)cdb;
2886 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
2888 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
2889 cmd10->reserved = 0;
2890 scsi_ulto2b(cmd6.length, cmd10->length);
2891 cmd10->control = cmd6.control;
2892 ccb->csio.cdb_len = sizeof(*cmd10);
2894 /* Requeue request, unfreezing queue if necessary */
2895 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
2896 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2899 cam_release_devq(ccb->ccb_h.path,
2903 /*getcount_only*/0);
2909 dadone(struct cam_periph *periph, union ccb *done_ccb)
2911 struct da_softc *softc;
2912 struct ccb_scsiio *csio;
2916 softc = (struct da_softc *)periph->softc;
2917 priority = done_ccb->ccb_h.pinfo.priority;
2919 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
2921 csio = &done_ccb->csio;
2922 state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
2924 case DA_CCB_BUFFER_IO:
2927 struct bio *bp, *bp1;
2929 cam_periph_lock(periph);
2930 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2931 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2935 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
2940 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
2941 if (error == ERESTART) {
2943 * A retry was scheduled, so
2946 cam_periph_unlock(periph);
2949 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2954 * return all queued I/O with EIO, so that
2955 * the client can retry these I/Os in the
2956 * proper order should it attempt to recover.
2961 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
2963 * Catastrophic error. Mark our pack as
2967 * XXX See if this is really a media
2970 xpt_print(periph->path,
2971 "Invalidating pack\n");
2972 softc->flags |= DA_FLAG_PACK_INVALID;
2973 queued_error = ENXIO;
2975 bioq_flush(&softc->bio_queue, NULL,
2978 bp->bio_error = error;
2979 bp->bio_resid = bp->bio_bcount;
2980 bp->bio_flags |= BIO_ERROR;
2982 } else if (bp != NULL) {
2983 if (state == DA_CCB_DELETE)
2986 bp->bio_resid = csio->resid;
2988 if (bp->bio_resid != 0)
2989 bp->bio_flags |= BIO_ERROR;
2991 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2992 cam_release_devq(done_ccb->ccb_h.path,
2996 /*getcount_only*/0);
2997 } else if (bp != NULL) {
2998 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2999 panic("REQ_CMP with QFRZN");
3000 if (state == DA_CCB_DELETE)
3003 bp->bio_resid = csio->resid;
3004 if (csio->resid > 0)
3005 bp->bio_flags |= BIO_ERROR;
3006 if (softc->error_inject != 0) {
3007 bp->bio_error = softc->error_inject;
3008 bp->bio_resid = bp->bio_bcount;
3009 bp->bio_flags |= BIO_ERROR;
3010 softc->error_inject = 0;
3014 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
3015 if (LIST_EMPTY(&softc->pending_ccbs))
3016 softc->flags |= DA_FLAG_WAS_OTAG;
3018 xpt_release_ccb(done_ccb);
3019 if (state == DA_CCB_DELETE) {
3020 TAILQ_HEAD(, bio) queue;
3023 TAILQ_CONCAT(&queue, &softc->delete_run_queue.queue, bio_queue);
3024 softc->delete_run_queue.insert_point = NULL;
3025 softc->delete_running = 0;
3027 cam_periph_unlock(periph);
3028 while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
3029 TAILQ_REMOVE(&queue, bp1, bio_queue);
3030 bp1->bio_error = bp->bio_error;
3031 if (bp->bio_flags & BIO_ERROR) {
3032 bp1->bio_flags |= BIO_ERROR;
3033 bp1->bio_resid = bp1->bio_bcount;
3039 cam_periph_unlock(periph);
3044 case DA_CCB_PROBE_RC:
3045 case DA_CCB_PROBE_RC16:
3047 struct scsi_read_capacity_data *rdcap;
3048 struct scsi_read_capacity_data_long *rcaplong;
3049 char announce_buf[80];
3055 if (state == DA_CCB_PROBE_RC)
3056 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
3058 rcaplong = (struct scsi_read_capacity_data_long *)
3061 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3062 struct disk_params *dp;
3063 uint32_t block_size;
3065 u_int lbppbe; /* LB per physical block exponent. */
3066 u_int lalba; /* Lowest aligned LBA. */
3068 if (state == DA_CCB_PROBE_RC) {
3069 block_size = scsi_4btoul(rdcap->length);
3070 maxsector = scsi_4btoul(rdcap->addr);
3075 * According to SBC-2, if the standard 10
3076 * byte READ CAPACITY command returns 2^32,
3077 * we should issue the 16 byte version of
3078 * the command, since the device in question
3079 * has more sectors than can be represented
3080 * with the short version of the command.
3082 if (maxsector == 0xffffffff) {
3083 free(rdcap, M_SCSIDA);
3084 xpt_release_ccb(done_ccb);
3085 softc->state = DA_STATE_PROBE_RC16;
3086 xpt_schedule(periph, priority);
3090 block_size = scsi_4btoul(rcaplong->length);
3091 maxsector = scsi_8btou64(rcaplong->addr);
3092 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
3093 lalba = scsi_2btoul(rcaplong->lalba_lbp);
3097 * Because GEOM code just will panic us if we
3098 * give them an 'illegal' value we'll avoid that
3101 if (block_size == 0) {
3106 if (block_size >= MAXPHYS) {
3107 xpt_print(periph->path,
3108 "unsupportable block size %ju\n",
3109 (uintmax_t) block_size);
3110 announce_buf[0] = '\0';
3111 cam_periph_invalidate(periph);
3114 * We pass rcaplong into dasetgeom(),
3115 * because it will only use it if it is
3118 dasetgeom(periph, block_size, maxsector,
3119 rcaplong, sizeof(*rcaplong));
3120 lbp = (lalba & SRC16_LBPME_A);
3121 dp = &softc->params;
3122 snprintf(announce_buf, sizeof(announce_buf),
3123 "%juMB (%ju %u byte sectors: %dH %dS/T "
3125 (((uintmax_t)dp->secsize *
3126 dp->sectors) / (1024*1024)),
3127 (uintmax_t)dp->sectors,
3128 dp->secsize, dp->heads,
3129 dp->secs_per_track, dp->cylinders);
3134 announce_buf[0] = '\0';
3137 * Retry any UNIT ATTENTION type errors. They
3138 * are expected at boot.
3140 error = daerror(done_ccb, CAM_RETRY_SELTO,
3141 SF_RETRY_UA|SF_NO_PRINT);
3142 if (error == ERESTART) {
3144 * A retry was scheuled, so
3148 } else if (error != 0) {
3150 int sense_key, error_code;
3153 struct ccb_getdev cgd;
3155 /* Don't wedge this device's queue */
3156 status = done_ccb->ccb_h.status;
3157 if ((status & CAM_DEV_QFRZN) != 0)
3158 cam_release_devq(done_ccb->ccb_h.path,
3162 /*getcount_only*/0);
3165 xpt_setup_ccb(&cgd.ccb_h,
3166 done_ccb->ccb_h.path,
3167 CAM_PRIORITY_NORMAL);
3168 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
3169 xpt_action((union ccb *)&cgd);
3171 if (scsi_extract_sense_ccb(done_ccb,
3172 &error_code, &sense_key, &asc, &ascq))
3178 * If we tried READ CAPACITY(16) and failed,
3179 * fallback to READ CAPACITY(10).
3181 if ((state == DA_CCB_PROBE_RC16) &&
3182 (softc->flags & DA_FLAG_CAN_RC16) &&
3183 (((csio->ccb_h.status & CAM_STATUS_MASK) ==
3186 (error_code == SSD_CURRENT_ERROR) &&
3187 (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
3188 softc->flags &= ~DA_FLAG_CAN_RC16;
3189 free(rdcap, M_SCSIDA);
3190 xpt_release_ccb(done_ccb);
3191 softc->state = DA_STATE_PROBE_RC;
3192 xpt_schedule(periph, priority);
3196 * Attach to anything that claims to be a
3197 * direct access or optical disk device,
3198 * as long as it doesn't return a "Logical
3199 * unit not supported" (0x25) error.
3201 if ((have_sense) && (asc != 0x25)
3202 && (error_code == SSD_CURRENT_ERROR)) {
3203 const char *sense_key_desc;
3204 const char *asc_desc;
3206 dasetgeom(periph, 512, -1, NULL, 0);
3207 scsi_sense_desc(sense_key, asc, ascq,
3211 snprintf(announce_buf,
3212 sizeof(announce_buf),
3213 "Attempt to query device "
3214 "size failed: %s, %s",
3222 xpt_print(periph->path,
3223 "got CAM status %#x\n",
3224 done_ccb->ccb_h.status);
3227 xpt_print(periph->path, "fatal error, "
3228 "failed to attach to device\n");
3231 * Free up resources.
3233 cam_periph_invalidate(periph);
3237 free(csio->data_ptr, M_SCSIDA);
3238 if (announce_buf[0] != '\0' &&
3239 ((softc->flags & DA_FLAG_ANNOUNCED) == 0)) {
3241 * Create our sysctl variables, now that we know
3242 * we have successfully attached.
3244 /* increase the refcount */
3245 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3246 taskqueue_enqueue(taskqueue_thread,
3247 &softc->sysctl_task);
3248 xpt_announce_periph(periph, announce_buf);
3249 xpt_announce_quirks(periph, softc->quirks,
3252 xpt_print(periph->path, "fatal error, "
3253 "could not acquire reference count\n");
3257 /* We already probed the device. */
3258 if (softc->flags & DA_FLAG_PROBED) {
3259 daprobedone(periph, done_ccb);
3263 /* Ensure re-probe doesn't see old delete. */
3264 softc->delete_available = 0;
3265 if (lbp && (softc->quirks & DA_Q_NO_UNMAP) == 0) {
3267 * Based on older SBC-3 spec revisions
3268 * any of the UNMAP methods "may" be
3269 * available via LBP given this flag so
3270 * we flag all of them as availble and
3271 * then remove those which further
3272 * probes confirm aren't available
3275 * We could also check readcap(16) p_type
3276 * flag to exclude one or more invalid
3277 * write same (X) types here
3279 dadeleteflag(softc, DA_DELETE_WS16, 1);
3280 dadeleteflag(softc, DA_DELETE_WS10, 1);
3281 dadeleteflag(softc, DA_DELETE_ZERO, 1);
3282 dadeleteflag(softc, DA_DELETE_UNMAP, 1);
3284 xpt_release_ccb(done_ccb);
3285 softc->state = DA_STATE_PROBE_LBP;
3286 xpt_schedule(periph, priority);
3290 xpt_release_ccb(done_ccb);
3291 softc->state = DA_STATE_PROBE_BDC;
3292 xpt_schedule(periph, priority);
3295 case DA_CCB_PROBE_LBP:
3297 struct scsi_vpd_logical_block_prov *lbp;
3299 lbp = (struct scsi_vpd_logical_block_prov *)csio->data_ptr;
3301 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3303 * T10/1799-D Revision 31 states at least one of these
3304 * must be supported but we don't currently enforce this.
3306 dadeleteflag(softc, DA_DELETE_WS16,
3307 (lbp->flags & SVPD_LBP_WS16));
3308 dadeleteflag(softc, DA_DELETE_WS10,
3309 (lbp->flags & SVPD_LBP_WS10));
3310 dadeleteflag(softc, DA_DELETE_ZERO,
3311 (lbp->flags & SVPD_LBP_WS10));
3312 dadeleteflag(softc, DA_DELETE_UNMAP,
3313 (lbp->flags & SVPD_LBP_UNMAP));
3316 error = daerror(done_ccb, CAM_RETRY_SELTO,
3317 SF_RETRY_UA|SF_NO_PRINT);
3318 if (error == ERESTART)
3320 else if (error != 0) {
3321 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3322 /* Don't wedge this device's queue */
3323 cam_release_devq(done_ccb->ccb_h.path,
3327 /*getcount_only*/0);
3331 * Failure indicates we don't support any SBC-3
3332 * delete methods with UNMAP
3337 free(lbp, M_SCSIDA);
3338 xpt_release_ccb(done_ccb);
3339 softc->state = DA_STATE_PROBE_BLK_LIMITS;
3340 xpt_schedule(periph, priority);
3343 case DA_CCB_PROBE_BLK_LIMITS:
3345 struct scsi_vpd_block_limits *block_limits;
3347 block_limits = (struct scsi_vpd_block_limits *)csio->data_ptr;
3349 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3350 uint32_t max_txfer_len = scsi_4btoul(
3351 block_limits->max_txfer_len);
3352 uint32_t max_unmap_lba_cnt = scsi_4btoul(
3353 block_limits->max_unmap_lba_cnt);
3354 uint32_t max_unmap_blk_cnt = scsi_4btoul(
3355 block_limits->max_unmap_blk_cnt);
3356 uint64_t ws_max_blks = scsi_8btou64(
3357 block_limits->max_write_same_length);
3359 if (max_txfer_len != 0) {
3360 softc->disk->d_maxsize = MIN(softc->maxio,
3361 (off_t)max_txfer_len * softc->params.secsize);
3365 * We should already support UNMAP but we check lba
3366 * and block count to be sure
3368 if (max_unmap_lba_cnt != 0x00L &&
3369 max_unmap_blk_cnt != 0x00L) {
3370 softc->unmap_max_lba = max_unmap_lba_cnt;
3371 softc->unmap_max_ranges = min(max_unmap_blk_cnt,
3375 * Unexpected UNMAP limits which means the
3376 * device doesn't actually support UNMAP
3378 dadeleteflag(softc, DA_DELETE_UNMAP, 0);
3381 if (ws_max_blks != 0x00L)
3382 softc->ws_max_blks = ws_max_blks;
3385 error = daerror(done_ccb, CAM_RETRY_SELTO,
3386 SF_RETRY_UA|SF_NO_PRINT);
3387 if (error == ERESTART)
3389 else if (error != 0) {
3390 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3391 /* Don't wedge this device's queue */
3392 cam_release_devq(done_ccb->ccb_h.path,
3396 /*getcount_only*/0);
3400 * Failure here doesn't mean UNMAP is not
3401 * supported as this is an optional page.
3403 softc->unmap_max_lba = 1;
3404 softc->unmap_max_ranges = 1;
3408 free(block_limits, M_SCSIDA);
3409 xpt_release_ccb(done_ccb);
3410 softc->state = DA_STATE_PROBE_BDC;
3411 xpt_schedule(periph, priority);
3414 case DA_CCB_PROBE_BDC:
3416 struct scsi_vpd_block_characteristics *bdc;
3418 bdc = (struct scsi_vpd_block_characteristics *)csio->data_ptr;
3420 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3422 * Disable queue sorting for non-rotational media
3425 u_int old_rate = softc->disk->d_rotation_rate;
3427 softc->disk->d_rotation_rate =
3428 scsi_2btoul(bdc->medium_rotation_rate);
3429 if (softc->disk->d_rotation_rate ==
3430 SVPD_BDC_RATE_NON_ROTATING) {
3431 softc->sort_io_queue = 0;
3433 if (softc->disk->d_rotation_rate != old_rate) {
3434 disk_attr_changed(softc->disk,
3435 "GEOM::rotation_rate", M_NOWAIT);
3439 error = daerror(done_ccb, CAM_RETRY_SELTO,
3440 SF_RETRY_UA|SF_NO_PRINT);
3441 if (error == ERESTART)
3443 else if (error != 0) {
3444 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3445 /* Don't wedge this device's queue */
3446 cam_release_devq(done_ccb->ccb_h.path,
3450 /*getcount_only*/0);
3455 free(bdc, M_SCSIDA);
3456 xpt_release_ccb(done_ccb);
3457 softc->state = DA_STATE_PROBE_ATA;
3458 xpt_schedule(periph, priority);
3461 case DA_CCB_PROBE_ATA:
3464 struct ata_params *ata_params;
3467 ata_params = (struct ata_params *)csio->data_ptr;
3468 ptr = (uint16_t *)ata_params;
3470 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3473 for (i = 0; i < sizeof(*ata_params) / 2; i++)
3474 ptr[i] = le16toh(ptr[i]);
3475 if (ata_params->support_dsm & ATA_SUPPORT_DSM_TRIM &&
3476 (softc->quirks & DA_Q_NO_UNMAP) == 0) {
3477 dadeleteflag(softc, DA_DELETE_ATA_TRIM, 1);
3478 if (ata_params->max_dsm_blocks != 0)
3479 softc->trim_max_ranges = min(
3480 softc->trim_max_ranges,
3481 ata_params->max_dsm_blocks *
3482 ATA_DSM_BLK_RANGES);
3485 * Disable queue sorting for non-rotational media
3488 old_rate = softc->disk->d_rotation_rate;
3489 softc->disk->d_rotation_rate =
3490 ata_params->media_rotation_rate;
3491 if (softc->disk->d_rotation_rate ==
3492 ATA_RATE_NON_ROTATING) {
3493 softc->sort_io_queue = 0;
3496 if (softc->disk->d_rotation_rate != old_rate) {
3497 disk_attr_changed(softc->disk,
3498 "GEOM::rotation_rate", M_NOWAIT);
3502 error = daerror(done_ccb, CAM_RETRY_SELTO,
3503 SF_RETRY_UA|SF_NO_PRINT);
3504 if (error == ERESTART)
3506 else if (error != 0) {
3507 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3508 /* Don't wedge this device's queue */
3509 cam_release_devq(done_ccb->ccb_h.path,
3513 /*getcount_only*/0);
3518 free(ata_params, M_SCSIDA);
3519 daprobedone(periph, done_ccb);
3523 /* No-op. We're polling */
3527 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3529 if (daerror(done_ccb, CAM_RETRY_SELTO,
3530 SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
3533 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3534 cam_release_devq(done_ccb->ccb_h.path,
3538 /*getcount_only*/0);
3540 xpt_release_ccb(done_ccb);
3541 cam_periph_release_locked(periph);
3547 xpt_release_ccb(done_ccb);
3551 dareprobe(struct cam_periph *periph)
3553 struct da_softc *softc;
3556 softc = (struct da_softc *)periph->softc;
3558 /* Probe in progress; don't interfere. */
3559 if (softc->state != DA_STATE_NORMAL)
3562 status = cam_periph_acquire(periph);
3563 KASSERT(status == CAM_REQ_CMP,
3564 ("dareprobe: cam_periph_acquire failed"));
3566 if (softc->flags & DA_FLAG_CAN_RC16)
3567 softc->state = DA_STATE_PROBE_RC16;
3569 softc->state = DA_STATE_PROBE_RC;
3571 xpt_schedule(periph, CAM_PRIORITY_DEV);
3575 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3577 struct da_softc *softc;
3578 struct cam_periph *periph;
3579 int error, error_code, sense_key, asc, ascq;
3581 periph = xpt_path_periph(ccb->ccb_h.path);
3582 softc = (struct da_softc *)periph->softc;
3585 * Automatically detect devices that do not support
3586 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
3589 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3590 error = cmd6workaround(ccb);
3591 } else if (scsi_extract_sense_ccb(ccb,
3592 &error_code, &sense_key, &asc, &ascq)) {
3593 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3594 error = cmd6workaround(ccb);
3596 * If the target replied with CAPACITY DATA HAS CHANGED UA,
3597 * query the capacity and notify upper layers.
3599 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3600 asc == 0x2A && ascq == 0x09) {
3601 xpt_print(periph->path, "Capacity data has changed\n");
3602 softc->flags &= ~DA_FLAG_PROBED;
3604 sense_flags |= SF_NO_PRINT;
3605 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3606 asc == 0x28 && ascq == 0x00) {
3607 softc->flags &= ~DA_FLAG_PROBED;
3608 disk_media_changed(softc->disk, M_NOWAIT);
3609 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3610 asc == 0x3F && ascq == 0x03) {
3611 xpt_print(periph->path, "INQUIRY data has changed\n");
3612 softc->flags &= ~DA_FLAG_PROBED;
3614 sense_flags |= SF_NO_PRINT;
3615 } else if (sense_key == SSD_KEY_NOT_READY &&
3616 asc == 0x3a && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
3617 softc->flags |= DA_FLAG_PACK_INVALID;
3618 disk_media_gone(softc->disk, M_NOWAIT);
3621 if (error == ERESTART)
3626 * Until we have a better way of doing pack validation,
3627 * don't treat UAs as errors.
3629 sense_flags |= SF_RETRY_UA;
3630 return(cam_periph_error(ccb, cam_flags, sense_flags,
3631 &softc->saved_ccb));
3635 damediapoll(void *arg)
3637 struct cam_periph *periph = arg;
3638 struct da_softc *softc = periph->softc;
3640 if (!softc->tur && LIST_EMPTY(&softc->pending_ccbs)) {
3641 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3646 /* Queue us up again */
3647 if (da_poll_period != 0)
3648 callout_schedule(&softc->mediapoll_c, da_poll_period * hz);
3652 daprevent(struct cam_periph *periph, int action)
3654 struct da_softc *softc;
3658 softc = (struct da_softc *)periph->softc;
3660 if (((action == PR_ALLOW)
3661 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
3662 || ((action == PR_PREVENT)
3663 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
3667 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3669 scsi_prevent(&ccb->csio,
3677 error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
3678 SF_RETRY_UA | SF_NO_PRINT, softc->disk->d_devstat);
3681 if (action == PR_ALLOW)
3682 softc->flags &= ~DA_FLAG_PACK_LOCKED;
3684 softc->flags |= DA_FLAG_PACK_LOCKED;
3687 xpt_release_ccb(ccb);
3691 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
3692 struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
3694 struct ccb_calc_geometry ccg;
3695 struct da_softc *softc;
3696 struct disk_params *dp;
3697 u_int lbppbe, lalba;
3700 softc = (struct da_softc *)periph->softc;
3702 dp = &softc->params;
3703 dp->secsize = block_len;
3704 dp->sectors = maxsector + 1;
3705 if (rcaplong != NULL) {
3706 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
3707 lalba = scsi_2btoul(rcaplong->lalba_lbp);
3708 lalba &= SRC16_LALBA_A;
3715 dp->stripesize = block_len << lbppbe;
3716 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
3718 } else if (softc->quirks & DA_Q_4K) {
3719 dp->stripesize = 4096;
3720 dp->stripeoffset = 0;
3723 dp->stripeoffset = 0;
3726 * Have the controller provide us with a geometry
3727 * for this disk. The only time the geometry
3728 * matters is when we boot and the controller
3729 * is the only one knowledgeable enough to come
3730 * up with something that will make this a bootable
3733 xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3734 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
3735 ccg.block_size = dp->secsize;
3736 ccg.volume_size = dp->sectors;
3738 ccg.secs_per_track = 0;
3740 xpt_action((union ccb*)&ccg);
3741 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3743 * We don't know what went wrong here- but just pick
3744 * a geometry so we don't have nasty things like divide
3748 dp->secs_per_track = 255;
3749 dp->cylinders = dp->sectors / (255 * 255);
3750 if (dp->cylinders == 0) {
3754 dp->heads = ccg.heads;
3755 dp->secs_per_track = ccg.secs_per_track;
3756 dp->cylinders = ccg.cylinders;
3760 * If the user supplied a read capacity buffer, and if it is
3761 * different than the previous buffer, update the data in the EDT.
3762 * If it's the same, we don't bother. This avoids sending an
3763 * update every time someone opens this device.
3765 if ((rcaplong != NULL)
3766 && (bcmp(rcaplong, &softc->rcaplong,
3767 min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
3768 struct ccb_dev_advinfo cdai;
3770 xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3771 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
3772 cdai.buftype = CDAI_TYPE_RCAPLONG;
3773 cdai.flags |= CDAI_FLAG_STORE;
3774 cdai.bufsiz = rcap_len;
3775 cdai.buf = (uint8_t *)rcaplong;
3776 xpt_action((union ccb *)&cdai);
3777 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
3778 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
3779 if (cdai.ccb_h.status != CAM_REQ_CMP) {
3780 xpt_print(periph->path, "%s: failed to set read "
3781 "capacity advinfo\n", __func__);
3782 /* Use cam_error_print() to decode the status */
3783 cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
3786 bcopy(rcaplong, &softc->rcaplong,
3787 min(sizeof(softc->rcaplong), rcap_len));
3791 softc->disk->d_sectorsize = softc->params.secsize;
3792 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
3793 softc->disk->d_stripesize = softc->params.stripesize;
3794 softc->disk->d_stripeoffset = softc->params.stripeoffset;
3795 /* XXX: these are not actually "firmware" values, so they may be wrong */
3796 softc->disk->d_fwsectors = softc->params.secs_per_track;
3797 softc->disk->d_fwheads = softc->params.heads;
3798 softc->disk->d_devstat->block_size = softc->params.secsize;
3799 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
3801 error = disk_resize(softc->disk, M_NOWAIT);
3803 xpt_print(periph->path, "disk_resize(9) failed, error = %d\n", error);
3807 dasendorderedtag(void *arg)
3809 struct da_softc *softc = arg;
3811 if (da_send_ordered) {
3812 if (!LIST_EMPTY(&softc->pending_ccbs)) {
3813 if ((softc->flags & DA_FLAG_WAS_OTAG) == 0)
3814 softc->flags |= DA_FLAG_NEED_OTAG;
3815 softc->flags &= ~DA_FLAG_WAS_OTAG;
3818 /* Queue us up again */
3819 callout_reset(&softc->sendordered_c,
3820 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
3821 dasendorderedtag, softc);
3825 * Step through all DA peripheral drivers, and if the device is still open,
3826 * sync the disk cache to physical media.
3829 dashutdown(void * arg, int howto)
3831 struct cam_periph *periph;
3832 struct da_softc *softc;
3836 CAM_PERIPH_FOREACH(periph, &dadriver) {
3837 softc = (struct da_softc *)periph->softc;
3838 if (SCHEDULER_STOPPED()) {
3839 /* If we paniced with the lock held, do not recurse. */
3840 if (!cam_periph_owned(periph) &&
3841 (softc->flags & DA_FLAG_OPEN)) {
3842 dadump(softc->disk, NULL, 0, 0, 0);
3846 cam_periph_lock(periph);
3849 * We only sync the cache if the drive is still open, and
3850 * if the drive is capable of it..
3852 if (((softc->flags & DA_FLAG_OPEN) == 0)
3853 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
3854 cam_periph_unlock(periph);
3858 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3859 scsi_synchronize_cache(&ccb->csio,
3863 /*begin_lba*/0, /* whole disk */
3868 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
3869 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR,
3870 softc->disk->d_devstat);
3872 xpt_print(periph->path, "Synchronize cache failed\n");
3873 xpt_release_ccb(ccb);
3874 cam_periph_unlock(periph);
3878 #else /* !_KERNEL */
3881 * XXX This is only left out of the kernel build to silence warnings. If,
3882 * for some reason this function is used in the kernel, the ifdefs should
3883 * be moved so it is included both in the kernel and userland.
3886 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
3887 void (*cbfcnp)(struct cam_periph *, union ccb *),
3888 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
3889 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
3892 struct scsi_format_unit *scsi_cmd;
3894 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
3895 scsi_cmd->opcode = FORMAT_UNIT;
3896 scsi_cmd->byte2 = byte2;
3897 scsi_ulto2b(ileave, scsi_cmd->interleave);
3902 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
3912 scsi_sanitize(struct ccb_scsiio *csio, u_int32_t retries,
3913 void (*cbfcnp)(struct cam_periph *, union ccb *),
3914 u_int8_t tag_action, u_int8_t byte2, u_int16_t control,
3915 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
3918 struct scsi_sanitize *scsi_cmd;
3920 scsi_cmd = (struct scsi_sanitize *)&csio->cdb_io.cdb_bytes;
3921 scsi_cmd->opcode = SANITIZE;
3922 scsi_cmd->byte2 = byte2;
3923 scsi_cmd->control = control;
3924 scsi_ulto2b(dxfer_len, scsi_cmd->length);
3929 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
3938 #endif /* _KERNEL */