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*", "*" },
1137 * SuperTalent TeraDrive CT SSDs
1138 * 4k optimised & trim only works in 4k requests + 4k aligned
1140 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "FTM??CT25H*", "*" },
1145 * XceedIOPS SATA SSDs
1148 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SG9XCS2D*", "*" },
1153 static disk_strategy_t dastrategy;
1154 static dumper_t dadump;
1155 static periph_init_t dainit;
1156 static void daasync(void *callback_arg, u_int32_t code,
1157 struct cam_path *path, void *arg);
1158 static void dasysctlinit(void *context, int pending);
1159 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
1160 static int dadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
1161 static int dadeletemaxsysctl(SYSCTL_HANDLER_ARGS);
1162 static void dadeletemethodset(struct da_softc *softc,
1163 da_delete_methods delete_method);
1164 static off_t dadeletemaxsize(struct da_softc *softc,
1165 da_delete_methods delete_method);
1166 static void dadeletemethodchoose(struct da_softc *softc,
1167 da_delete_methods default_method);
1168 static void daprobedone(struct cam_periph *periph, union ccb *ccb);
1170 static periph_ctor_t daregister;
1171 static periph_dtor_t dacleanup;
1172 static periph_start_t dastart;
1173 static periph_oninv_t daoninvalidate;
1174 static void dadone(struct cam_periph *periph,
1175 union ccb *done_ccb);
1176 static int daerror(union ccb *ccb, u_int32_t cam_flags,
1177 u_int32_t sense_flags);
1178 static void daprevent(struct cam_periph *periph, int action);
1179 static void dareprobe(struct cam_periph *periph);
1180 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
1182 struct scsi_read_capacity_data_long *rcaplong,
1184 static timeout_t dasendorderedtag;
1185 static void dashutdown(void *arg, int howto);
1186 static timeout_t damediapoll;
1188 #ifndef DA_DEFAULT_POLL_PERIOD
1189 #define DA_DEFAULT_POLL_PERIOD 3
1192 #ifndef DA_DEFAULT_TIMEOUT
1193 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
1196 #ifndef DA_DEFAULT_RETRY
1197 #define DA_DEFAULT_RETRY 4
1200 #ifndef DA_DEFAULT_SEND_ORDERED
1201 #define DA_DEFAULT_SEND_ORDERED 1
1204 #define DA_SIO (softc->sort_io_queue >= 0 ? \
1205 softc->sort_io_queue : cam_sort_io_queues)
1207 static int da_poll_period = DA_DEFAULT_POLL_PERIOD;
1208 static int da_retry_count = DA_DEFAULT_RETRY;
1209 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
1210 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
1212 static SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
1213 "CAM Direct Access Disk driver");
1214 SYSCTL_INT(_kern_cam_da, OID_AUTO, poll_period, CTLFLAG_RWTUN,
1215 &da_poll_period, 0, "Media polling period in seconds");
1216 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RWTUN,
1217 &da_retry_count, 0, "Normal I/O retry count");
1218 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RWTUN,
1219 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
1220 SYSCTL_INT(_kern_cam_da, OID_AUTO, send_ordered, CTLFLAG_RWTUN,
1221 &da_send_ordered, 0, "Send Ordered Tags");
1224 * DA_ORDEREDTAG_INTERVAL determines how often, relative
1225 * to the default timeout, we check to see whether an ordered
1226 * tagged transaction is appropriate to prevent simple tag
1227 * starvation. Since we'd like to ensure that there is at least
1228 * 1/2 of the timeout length left for a starved transaction to
1229 * complete after we've sent an ordered tag, we must poll at least
1230 * four times in every timeout period. This takes care of the worst
1231 * case where a starved transaction starts during an interval that
1232 * meets the requirement "don't send an ordered tag" test so it takes
1233 * us two intervals to determine that a tag must be sent.
1235 #ifndef DA_ORDEREDTAG_INTERVAL
1236 #define DA_ORDEREDTAG_INTERVAL 4
1239 static struct periph_driver dadriver =
1242 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
1245 PERIPHDRIVER_DECLARE(da, dadriver);
1247 static MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
1250 daopen(struct disk *dp)
1252 struct cam_periph *periph;
1253 struct da_softc *softc;
1256 periph = (struct cam_periph *)dp->d_drv1;
1257 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1261 cam_periph_lock(periph);
1262 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
1263 cam_periph_unlock(periph);
1264 cam_periph_release(periph);
1268 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1271 softc = (struct da_softc *)periph->softc;
1274 /* Wait for the disk size update. */
1275 error = cam_periph_sleep(periph, &softc->disk->d_mediasize, PRIBIO,
1278 xpt_print(periph->path, "unable to retrieve capacity data\n");
1280 if (periph->flags & CAM_PERIPH_INVALID)
1283 if (error == 0 && (softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1284 (softc->quirks & DA_Q_NO_PREVENT) == 0)
1285 daprevent(periph, PR_PREVENT);
1288 softc->flags &= ~DA_FLAG_PACK_INVALID;
1289 softc->flags |= DA_FLAG_OPEN;
1292 cam_periph_unhold(periph);
1293 cam_periph_unlock(periph);
1296 cam_periph_release(periph);
1302 daclose(struct disk *dp)
1304 struct cam_periph *periph;
1305 struct da_softc *softc;
1309 periph = (struct cam_periph *)dp->d_drv1;
1310 softc = (struct da_softc *)periph->softc;
1311 cam_periph_lock(periph);
1312 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1315 if (cam_periph_hold(periph, PRIBIO) == 0) {
1317 /* Flush disk cache. */
1318 if ((softc->flags & DA_FLAG_DIRTY) != 0 &&
1319 (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0 &&
1320 (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
1321 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1322 scsi_synchronize_cache(&ccb->csio, /*retries*/1,
1323 /*cbfcnp*/dadone, MSG_SIMPLE_Q_TAG,
1324 /*begin_lba*/0, /*lb_count*/0, SSD_FULL_SIZE,
1326 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
1327 /*sense_flags*/SF_RETRY_UA | SF_QUIET_IR,
1328 softc->disk->d_devstat);
1330 softc->flags &= ~DA_FLAG_DIRTY;
1331 xpt_release_ccb(ccb);
1334 /* Allow medium removal. */
1335 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1336 (softc->quirks & DA_Q_NO_PREVENT) == 0)
1337 daprevent(periph, PR_ALLOW);
1339 cam_periph_unhold(periph);
1343 * If we've got removeable media, mark the blocksize as
1344 * unavailable, since it could change when new media is
1347 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0)
1348 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1350 softc->flags &= ~DA_FLAG_OPEN;
1351 while (softc->refcount != 0)
1352 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "daclose", 1);
1353 cam_periph_unlock(periph);
1354 cam_periph_release(periph);
1359 daschedule(struct cam_periph *periph)
1361 struct da_softc *softc = (struct da_softc *)periph->softc;
1363 if (softc->state != DA_STATE_NORMAL)
1366 /* Check if we have more work to do. */
1367 if (bioq_first(&softc->bio_queue) ||
1368 (!softc->delete_running && bioq_first(&softc->delete_queue)) ||
1370 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1375 * Actually translate the requested transfer into one the physical driver
1376 * can understand. The transfer is described by a buf and will include
1377 * only one physical transfer.
1380 dastrategy(struct bio *bp)
1382 struct cam_periph *periph;
1383 struct da_softc *softc;
1385 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1386 softc = (struct da_softc *)periph->softc;
1388 cam_periph_lock(periph);
1391 * If the device has been made invalid, error out
1393 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1394 cam_periph_unlock(periph);
1395 biofinish(bp, NULL, ENXIO);
1399 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
1402 * Place it in the queue of disk activities for this disk
1404 if (bp->bio_cmd == BIO_DELETE) {
1405 bioq_disksort(&softc->delete_queue, bp);
1406 } else if (DA_SIO) {
1407 bioq_disksort(&softc->bio_queue, bp);
1409 bioq_insert_tail(&softc->bio_queue, bp);
1413 * Schedule ourselves for performing the work.
1416 cam_periph_unlock(periph);
1422 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1424 struct cam_periph *periph;
1425 struct da_softc *softc;
1427 struct ccb_scsiio csio;
1432 periph = dp->d_drv1;
1433 softc = (struct da_softc *)periph->softc;
1434 cam_periph_lock(periph);
1435 secsize = softc->params.secsize;
1437 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1438 cam_periph_unlock(periph);
1443 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1444 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1445 scsi_read_write(&csio,
1449 /*read*/SCSI_RW_WRITE,
1451 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1454 /*data_ptr*/(u_int8_t *) virtual,
1455 /*dxfer_len*/length,
1456 /*sense_len*/SSD_FULL_SIZE,
1457 da_default_timeout * 1000);
1458 xpt_polled_action((union ccb *)&csio);
1460 error = cam_periph_error((union ccb *)&csio,
1461 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1462 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1463 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1464 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1466 printf("Aborting dump due to I/O error.\n");
1467 cam_periph_unlock(periph);
1472 * Sync the disk cache contents to the physical media.
1474 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1476 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1477 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1478 scsi_synchronize_cache(&csio,
1482 /*begin_lba*/0,/* Cover the whole disk */
1486 xpt_polled_action((union ccb *)&csio);
1488 error = cam_periph_error((union ccb *)&csio,
1489 0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL);
1490 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1491 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1492 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1494 xpt_print(periph->path, "Synchronize cache failed\n");
1496 cam_periph_unlock(periph);
1501 dagetattr(struct bio *bp)
1504 struct cam_periph *periph;
1506 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1507 cam_periph_lock(periph);
1508 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1510 cam_periph_unlock(periph);
1512 bp->bio_completed = bp->bio_length;
1522 * Install a global async callback. This callback will
1523 * receive async callbacks like "new device found".
1525 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1527 if (status != CAM_REQ_CMP) {
1528 printf("da: Failed to attach master async callback "
1529 "due to status 0x%x!\n", status);
1530 } else if (da_send_ordered) {
1532 /* Register our shutdown event handler */
1533 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1534 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1535 printf("dainit: shutdown event registration failed!\n");
1540 * Callback from GEOM, called when it has finished cleaning up its
1544 dadiskgonecb(struct disk *dp)
1546 struct cam_periph *periph;
1548 periph = (struct cam_periph *)dp->d_drv1;
1549 cam_periph_release(periph);
1553 daoninvalidate(struct cam_periph *periph)
1555 struct da_softc *softc;
1557 softc = (struct da_softc *)periph->softc;
1560 * De-register any async callbacks.
1562 xpt_register_async(0, daasync, periph, periph->path);
1564 softc->flags |= DA_FLAG_PACK_INVALID;
1567 * Return all queued I/O with ENXIO.
1568 * XXX Handle any transactions queued to the card
1569 * with XPT_ABORT_CCB.
1571 bioq_flush(&softc->bio_queue, NULL, ENXIO);
1572 bioq_flush(&softc->delete_queue, NULL, ENXIO);
1575 * Tell GEOM that we've gone away, we'll get a callback when it is
1576 * done cleaning up its resources.
1578 disk_gone(softc->disk);
1582 dacleanup(struct cam_periph *periph)
1584 struct da_softc *softc;
1586 softc = (struct da_softc *)periph->softc;
1588 cam_periph_unlock(periph);
1591 * If we can't free the sysctl tree, oh well...
1593 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1594 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1595 xpt_print(periph->path, "can't remove sysctl context\n");
1598 callout_drain(&softc->mediapoll_c);
1599 disk_destroy(softc->disk);
1600 callout_drain(&softc->sendordered_c);
1601 free(softc, M_DEVBUF);
1602 cam_periph_lock(periph);
1606 daasync(void *callback_arg, u_int32_t code,
1607 struct cam_path *path, void *arg)
1609 struct cam_periph *periph;
1610 struct da_softc *softc;
1612 periph = (struct cam_periph *)callback_arg;
1614 case AC_FOUND_DEVICE:
1616 struct ccb_getdev *cgd;
1619 cgd = (struct ccb_getdev *)arg;
1623 if (cgd->protocol != PROTO_SCSI)
1626 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1627 && SID_TYPE(&cgd->inq_data) != T_RBC
1628 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1632 * Allocate a peripheral instance for
1633 * this device and start the probe
1636 status = cam_periph_alloc(daregister, daoninvalidate,
1638 "da", CAM_PERIPH_BIO,
1640 AC_FOUND_DEVICE, cgd);
1642 if (status != CAM_REQ_CMP
1643 && status != CAM_REQ_INPROG)
1644 printf("daasync: Unable to attach to new device "
1645 "due to status 0x%x\n", status);
1648 case AC_ADVINFO_CHANGED:
1652 buftype = (uintptr_t)arg;
1653 if (buftype == CDAI_TYPE_PHYS_PATH) {
1654 struct da_softc *softc;
1656 softc = periph->softc;
1657 disk_attr_changed(softc->disk, "GEOM::physpath",
1662 case AC_UNIT_ATTENTION:
1665 int error_code, sense_key, asc, ascq;
1667 softc = (struct da_softc *)periph->softc;
1668 ccb = (union ccb *)arg;
1671 * Handle all UNIT ATTENTIONs except our own,
1672 * as they will be handled by daerror().
1674 if (xpt_path_periph(ccb->ccb_h.path) != periph &&
1675 scsi_extract_sense_ccb(ccb,
1676 &error_code, &sense_key, &asc, &ascq)) {
1677 if (asc == 0x2A && ascq == 0x09) {
1678 xpt_print(ccb->ccb_h.path,
1679 "Capacity data has changed\n");
1680 softc->flags &= ~DA_FLAG_PROBED;
1682 } else if (asc == 0x28 && ascq == 0x00) {
1683 softc->flags &= ~DA_FLAG_PROBED;
1684 disk_media_changed(softc->disk, M_NOWAIT);
1685 } else if (asc == 0x3F && ascq == 0x03) {
1686 xpt_print(ccb->ccb_h.path,
1687 "INQUIRY data has changed\n");
1688 softc->flags &= ~DA_FLAG_PROBED;
1692 cam_periph_async(periph, code, path, arg);
1696 softc = (struct da_softc *)periph->softc;
1698 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
1707 struct ccb_hdr *ccbh;
1709 softc = (struct da_softc *)periph->softc;
1711 * Don't fail on the expected unit attention
1714 softc->flags |= DA_FLAG_RETRY_UA;
1715 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1716 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1722 cam_periph_async(periph, code, path, arg);
1726 dasysctlinit(void *context, int pending)
1728 struct cam_periph *periph;
1729 struct da_softc *softc;
1730 char tmpstr[80], tmpstr2[80];
1731 struct ccb_trans_settings cts;
1733 periph = (struct cam_periph *)context;
1735 * periph was held for us when this task was enqueued
1737 if (periph->flags & CAM_PERIPH_INVALID) {
1738 cam_periph_release(periph);
1742 softc = (struct da_softc *)periph->softc;
1743 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1744 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1746 sysctl_ctx_init(&softc->sysctl_ctx);
1747 softc->flags |= DA_FLAG_SCTX_INIT;
1748 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1749 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1750 CTLFLAG_RD, 0, tmpstr);
1751 if (softc->sysctl_tree == NULL) {
1752 printf("dasysctlinit: unable to allocate sysctl tree\n");
1753 cam_periph_release(periph);
1758 * Now register the sysctl handler, so the user can change the value on
1761 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1762 OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RW,
1763 softc, 0, dadeletemethodsysctl, "A",
1764 "BIO_DELETE execution method");
1765 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1766 OID_AUTO, "delete_max", CTLTYPE_U64 | CTLFLAG_RW,
1767 softc, 0, dadeletemaxsysctl, "Q",
1768 "Maximum BIO_DELETE size");
1769 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1770 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1771 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1772 "Minimum CDB size");
1773 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1774 OID_AUTO, "sort_io_queue", CTLFLAG_RW, &softc->sort_io_queue, 0,
1775 "Sort IO queue to try and optimise disk access patterns");
1777 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1778 SYSCTL_CHILDREN(softc->sysctl_tree),
1782 &softc->error_inject,
1784 "error_inject leaf");
1788 * Add some addressing info.
1790 memset(&cts, 0, sizeof (cts));
1791 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1792 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1793 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1794 cam_periph_lock(periph);
1795 xpt_action((union ccb *)&cts);
1796 cam_periph_unlock(periph);
1797 if (cts.ccb_h.status != CAM_REQ_CMP) {
1798 cam_periph_release(periph);
1801 if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
1802 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1803 if (fc->valid & CTS_FC_VALID_WWPN) {
1804 softc->wwpn = fc->wwpn;
1805 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1806 SYSCTL_CHILDREN(softc->sysctl_tree),
1807 OID_AUTO, "wwpn", CTLFLAG_RD,
1808 &softc->wwpn, "World Wide Port Name");
1811 cam_periph_release(periph);
1815 dadeletemaxsysctl(SYSCTL_HANDLER_ARGS)
1819 struct da_softc *softc;
1821 softc = (struct da_softc *)arg1;
1823 value = softc->disk->d_delmaxsize;
1824 error = sysctl_handle_64(oidp, &value, 0, req);
1825 if ((error != 0) || (req->newptr == NULL))
1828 /* only accept values smaller than the calculated value */
1829 if (value > dadeletemaxsize(softc, softc->delete_method)) {
1832 softc->disk->d_delmaxsize = value;
1838 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1842 value = *(int *)arg1;
1844 error = sysctl_handle_int(oidp, &value, 0, req);
1847 || (req->newptr == NULL))
1851 * Acceptable values here are 6, 10, 12 or 16.
1855 else if ((value > 6)
1858 else if ((value > 10)
1861 else if (value > 12)
1864 *(int *)arg1 = value;
1870 dadeletemethodset(struct da_softc *softc, da_delete_methods delete_method)
1874 softc->delete_method = delete_method;
1875 softc->disk->d_delmaxsize = dadeletemaxsize(softc, delete_method);
1876 softc->delete_func = da_delete_functions[delete_method];
1878 if (softc->delete_method > DA_DELETE_DISABLE)
1879 softc->disk->d_flags |= DISKFLAG_CANDELETE;
1881 softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
1885 dadeletemaxsize(struct da_softc *softc, da_delete_methods delete_method)
1889 switch(delete_method) {
1890 case DA_DELETE_UNMAP:
1891 sectors = (off_t)softc->unmap_max_lba;
1893 case DA_DELETE_ATA_TRIM:
1894 sectors = (off_t)ATA_DSM_RANGE_MAX * softc->trim_max_ranges;
1896 case DA_DELETE_WS16:
1897 sectors = (off_t)min(softc->ws_max_blks, WS16_MAX_BLKS);
1899 case DA_DELETE_ZERO:
1900 case DA_DELETE_WS10:
1901 sectors = (off_t)min(softc->ws_max_blks, WS10_MAX_BLKS);
1907 return (off_t)softc->params.secsize *
1908 min(sectors, (off_t)softc->params.sectors);
1912 daprobedone(struct cam_periph *periph, union ccb *ccb)
1914 struct da_softc *softc;
1916 softc = (struct da_softc *)periph->softc;
1918 dadeletemethodchoose(softc, DA_DELETE_NONE);
1920 if (bootverbose && (softc->flags & DA_FLAG_ANNOUNCED) == 0) {
1924 snprintf(buf, sizeof(buf), "Delete methods: <");
1926 for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
1927 if (softc->delete_available & (1 << i)) {
1929 strlcat(buf, ",", sizeof(buf));
1933 strlcat(buf, da_delete_method_names[i],
1935 if (i == softc->delete_method) {
1936 strlcat(buf, "(*)", sizeof(buf));
1941 if (softc->delete_method == DA_DELETE_NONE)
1942 strlcat(buf, "NONE(*)", sizeof(buf));
1944 strlcat(buf, "DISABLED(*)", sizeof(buf));
1946 strlcat(buf, ">", sizeof(buf));
1947 printf("%s%d: %s\n", periph->periph_name,
1948 periph->unit_number, buf);
1952 * Since our peripheral may be invalidated by an error
1953 * above or an external event, we must release our CCB
1954 * before releasing the probe lock on the peripheral.
1955 * The peripheral will only go away once the last lock
1956 * is removed, and we need it around for the CCB release
1959 xpt_release_ccb(ccb);
1960 softc->state = DA_STATE_NORMAL;
1961 softc->flags |= DA_FLAG_PROBED;
1963 wakeup(&softc->disk->d_mediasize);
1964 if ((softc->flags & DA_FLAG_ANNOUNCED) == 0) {
1965 softc->flags |= DA_FLAG_ANNOUNCED;
1966 cam_periph_unhold(periph);
1968 cam_periph_release_locked(periph);
1972 dadeletemethodchoose(struct da_softc *softc, da_delete_methods default_method)
1974 int i, delete_method;
1976 delete_method = default_method;
1979 * Use the pre-defined order to choose the best
1980 * performing delete.
1982 for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
1983 if (softc->delete_available & (1 << i)) {
1984 dadeletemethodset(softc, i);
1988 dadeletemethodset(softc, delete_method);
1992 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
1996 struct da_softc *softc;
1997 int i, error, methods, value;
1999 softc = (struct da_softc *)arg1;
2001 value = softc->delete_method;
2002 if (value < 0 || value > DA_DELETE_MAX)
2005 p = da_delete_method_names[value];
2006 strncpy(buf, p, sizeof(buf));
2007 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2008 if (error != 0 || req->newptr == NULL)
2010 methods = softc->delete_available | (1 << DA_DELETE_DISABLE);
2011 for (i = 0; i <= DA_DELETE_MAX; i++) {
2012 if (!(methods & (1 << i)) ||
2013 strcmp(buf, da_delete_method_names[i]) != 0)
2015 dadeletemethodset(softc, i);
2022 daregister(struct cam_periph *periph, void *arg)
2024 struct da_softc *softc;
2025 struct ccb_pathinq cpi;
2026 struct ccb_getdev *cgd;
2030 cgd = (struct ccb_getdev *)arg;
2032 printf("daregister: no getdev CCB, can't register device\n");
2033 return(CAM_REQ_CMP_ERR);
2036 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
2039 if (softc == NULL) {
2040 printf("daregister: Unable to probe new device. "
2041 "Unable to allocate softc\n");
2042 return(CAM_REQ_CMP_ERR);
2045 LIST_INIT(&softc->pending_ccbs);
2046 softc->state = DA_STATE_PROBE_RC;
2047 bioq_init(&softc->bio_queue);
2048 bioq_init(&softc->delete_queue);
2049 bioq_init(&softc->delete_run_queue);
2050 if (SID_IS_REMOVABLE(&cgd->inq_data))
2051 softc->flags |= DA_FLAG_PACK_REMOVABLE;
2052 softc->unmap_max_ranges = UNMAP_MAX_RANGES;
2053 softc->unmap_max_lba = UNMAP_RANGE_MAX;
2054 softc->ws_max_blks = WS16_MAX_BLKS;
2055 softc->trim_max_ranges = ATA_TRIM_MAX_RANGES;
2056 softc->sort_io_queue = -1;
2058 periph->softc = softc;
2061 * See if this device has any quirks.
2063 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
2064 (caddr_t)da_quirk_table,
2065 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
2066 sizeof(*da_quirk_table), scsi_inquiry_match);
2069 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
2071 softc->quirks = DA_Q_NONE;
2073 /* Check if the SIM does not want 6 byte commands */
2074 bzero(&cpi, sizeof(cpi));
2075 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2076 cpi.ccb_h.func_code = XPT_PATH_INQ;
2077 xpt_action((union ccb *)&cpi);
2078 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
2079 softc->quirks |= DA_Q_NO_6_BYTE;
2081 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
2084 * Take an exclusive refcount on the periph while dastart is called
2085 * to finish the probe. The reference will be dropped in dadone at
2088 (void)cam_periph_hold(periph, PRIBIO);
2091 * Schedule a periodic event to occasionally send an
2092 * ordered tag to a device.
2094 callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0);
2095 callout_reset(&softc->sendordered_c,
2096 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2097 dasendorderedtag, softc);
2099 cam_periph_unlock(periph);
2101 * RBC devices don't have to support READ(6), only READ(10).
2103 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
2104 softc->minimum_cmd_size = 10;
2106 softc->minimum_cmd_size = 6;
2109 * Load the user's default, if any.
2111 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
2112 periph->unit_number);
2113 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
2116 * 6, 10, 12 and 16 are the currently permissible values.
2118 if (softc->minimum_cmd_size < 6)
2119 softc->minimum_cmd_size = 6;
2120 else if ((softc->minimum_cmd_size > 6)
2121 && (softc->minimum_cmd_size <= 10))
2122 softc->minimum_cmd_size = 10;
2123 else if ((softc->minimum_cmd_size > 10)
2124 && (softc->minimum_cmd_size <= 12))
2125 softc->minimum_cmd_size = 12;
2126 else if (softc->minimum_cmd_size > 12)
2127 softc->minimum_cmd_size = 16;
2129 /* Predict whether device may support READ CAPACITY(16). */
2130 if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3 &&
2131 (softc->quirks & DA_Q_NO_RC16) == 0) {
2132 softc->flags |= DA_FLAG_CAN_RC16;
2133 softc->state = DA_STATE_PROBE_RC16;
2137 * Register this media as a disk.
2139 softc->disk = disk_alloc();
2140 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
2141 periph->unit_number, 0,
2142 DEVSTAT_BS_UNAVAILABLE,
2143 SID_TYPE(&cgd->inq_data) |
2144 XPORT_DEVSTAT_TYPE(cpi.transport),
2145 DEVSTAT_PRIORITY_DISK);
2146 softc->disk->d_open = daopen;
2147 softc->disk->d_close = daclose;
2148 softc->disk->d_strategy = dastrategy;
2149 softc->disk->d_dump = dadump;
2150 softc->disk->d_getattr = dagetattr;
2151 softc->disk->d_gone = dadiskgonecb;
2152 softc->disk->d_name = "da";
2153 softc->disk->d_drv1 = periph;
2155 softc->maxio = DFLTPHYS; /* traditional default */
2156 else if (cpi.maxio > MAXPHYS)
2157 softc->maxio = MAXPHYS; /* for safety */
2159 softc->maxio = cpi.maxio;
2160 softc->disk->d_maxsize = softc->maxio;
2161 softc->disk->d_unit = periph->unit_number;
2162 softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION;
2163 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
2164 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
2165 if ((cpi.hba_misc & PIM_UNMAPPED) != 0)
2166 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
2167 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
2168 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
2169 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
2170 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
2171 cgd->inq_data.product, sizeof(cgd->inq_data.product),
2172 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
2173 softc->disk->d_hba_vendor = cpi.hba_vendor;
2174 softc->disk->d_hba_device = cpi.hba_device;
2175 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
2176 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
2179 * Acquire a reference to the periph before we register with GEOM.
2180 * We'll release this reference once GEOM calls us back (via
2181 * dadiskgonecb()) telling us that our provider has been freed.
2183 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
2184 xpt_print(periph->path, "%s: lost periph during "
2185 "registration!\n", __func__);
2186 cam_periph_lock(periph);
2187 return (CAM_REQ_CMP_ERR);
2190 disk_create(softc->disk, DISK_VERSION);
2191 cam_periph_lock(periph);
2194 * Add async callbacks for events of interest.
2195 * I don't bother checking if this fails as,
2196 * in most cases, the system will function just
2197 * fine without them and the only alternative
2198 * would be to not attach the device on failure.
2200 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
2201 AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION,
2202 daasync, periph, periph->path);
2205 * Emit an attribute changed notification just in case
2206 * physical path information arrived before our async
2207 * event handler was registered, but after anyone attaching
2208 * to our disk device polled it.
2210 disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
2213 * Schedule a periodic media polling events.
2215 callout_init_mtx(&softc->mediapoll_c, cam_periph_mtx(periph), 0);
2216 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
2217 (cgd->inq_flags & SID_AEN) == 0 &&
2218 da_poll_period != 0)
2219 callout_reset(&softc->mediapoll_c, da_poll_period * hz,
2220 damediapoll, periph);
2222 xpt_schedule(periph, CAM_PRIORITY_DEV);
2224 return(CAM_REQ_CMP);
2228 dastart(struct cam_periph *periph, union ccb *start_ccb)
2230 struct da_softc *softc;
2232 softc = (struct da_softc *)periph->softc;
2234 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
2237 switch (softc->state) {
2238 case DA_STATE_NORMAL:
2243 /* Run BIO_DELETE if not running yet. */
2244 if (!softc->delete_running &&
2245 (bp = bioq_first(&softc->delete_queue)) != NULL) {
2246 if (softc->delete_func != NULL) {
2247 softc->delete_func(periph, start_ccb, bp);
2250 bioq_flush(&softc->delete_queue, NULL, 0);
2255 /* Run regular command. */
2256 bp = bioq_takefirst(&softc->bio_queue);
2260 scsi_test_unit_ready(&start_ccb->csio,
2261 /*retries*/ da_retry_count,
2265 da_default_timeout * 1000);
2266 start_ccb->ccb_h.ccb_bp = NULL;
2267 start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
2268 xpt_action(start_ccb);
2270 xpt_release_ccb(start_ccb);
2275 cam_periph_release_locked(periph);
2278 if ((bp->bio_flags & BIO_ORDERED) != 0 ||
2279 (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
2280 softc->flags &= ~DA_FLAG_NEED_OTAG;
2281 softc->flags |= DA_FLAG_WAS_OTAG;
2282 tag_code = MSG_ORDERED_Q_TAG;
2284 tag_code = MSG_SIMPLE_Q_TAG;
2287 switch (bp->bio_cmd) {
2289 softc->flags |= DA_FLAG_DIRTY;
2292 scsi_read_write(&start_ccb->csio,
2293 /*retries*/da_retry_count,
2295 /*tag_action*/tag_code,
2296 /*read_op*/(bp->bio_cmd == BIO_READ ?
2297 SCSI_RW_READ : SCSI_RW_WRITE) |
2298 ((bp->bio_flags & BIO_UNMAPPED) != 0 ?
2301 softc->minimum_cmd_size,
2302 /*lba*/bp->bio_pblkno,
2303 /*block_count*/bp->bio_bcount /
2304 softc->params.secsize,
2305 /*data_ptr*/ (bp->bio_flags &
2306 BIO_UNMAPPED) != 0 ? (void *)bp :
2308 /*dxfer_len*/ bp->bio_bcount,
2309 /*sense_len*/SSD_FULL_SIZE,
2310 da_default_timeout * 1000);
2314 * BIO_FLUSH doesn't currently communicate
2315 * range data, so we synchronize the cache
2316 * over the whole disk. We also force
2317 * ordered tag semantics the flush applies
2318 * to all previously queued I/O.
2320 scsi_synchronize_cache(&start_ccb->csio,
2327 da_default_timeout*1000);
2330 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
2331 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
2334 LIST_INSERT_HEAD(&softc->pending_ccbs,
2335 &start_ccb->ccb_h, periph_links.le);
2337 /* We expect a unit attention from this device */
2338 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
2339 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
2340 softc->flags &= ~DA_FLAG_RETRY_UA;
2343 start_ccb->ccb_h.ccb_bp = bp;
2345 cam_periph_unlock(periph);
2346 xpt_action(start_ccb);
2347 cam_periph_lock(periph);
2350 /* May have more work to do, so ensure we stay scheduled */
2354 case DA_STATE_PROBE_RC:
2356 struct scsi_read_capacity_data *rcap;
2358 rcap = (struct scsi_read_capacity_data *)
2359 malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
2361 printf("dastart: Couldn't malloc read_capacity data\n");
2362 /* da_free_periph??? */
2365 scsi_read_capacity(&start_ccb->csio,
2366 /*retries*/da_retry_count,
2372 start_ccb->ccb_h.ccb_bp = NULL;
2373 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC;
2374 xpt_action(start_ccb);
2377 case DA_STATE_PROBE_RC16:
2379 struct scsi_read_capacity_data_long *rcaplong;
2381 rcaplong = (struct scsi_read_capacity_data_long *)
2382 malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
2383 if (rcaplong == NULL) {
2384 printf("dastart: Couldn't malloc read_capacity data\n");
2385 /* da_free_periph??? */
2388 scsi_read_capacity_16(&start_ccb->csio,
2389 /*retries*/ da_retry_count,
2391 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2395 /*rcap_buf*/ (uint8_t *)rcaplong,
2396 /*rcap_buf_len*/ sizeof(*rcaplong),
2397 /*sense_len*/ SSD_FULL_SIZE,
2398 /*timeout*/ da_default_timeout * 1000);
2399 start_ccb->ccb_h.ccb_bp = NULL;
2400 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC16;
2401 xpt_action(start_ccb);
2404 case DA_STATE_PROBE_LBP:
2406 struct scsi_vpd_logical_block_prov *lbp;
2408 if (!scsi_vpd_supported_page(periph, SVPD_LBP)) {
2410 * If we get here we don't support any SBC-3 delete
2411 * methods with UNMAP as the Logical Block Provisioning
2412 * VPD page support is required for devices which
2413 * support it according to T10/1799-D Revision 31
2414 * however older revisions of the spec don't mandate
2415 * this so we currently don't remove these methods
2416 * from the available set.
2418 softc->state = DA_STATE_PROBE_BLK_LIMITS;
2422 lbp = (struct scsi_vpd_logical_block_prov *)
2423 malloc(sizeof(*lbp), M_SCSIDA, M_NOWAIT|M_ZERO);
2426 printf("dastart: Couldn't malloc lbp data\n");
2427 /* da_free_periph??? */
2431 scsi_inquiry(&start_ccb->csio,
2432 /*retries*/da_retry_count,
2434 /*tag_action*/MSG_SIMPLE_Q_TAG,
2435 /*inq_buf*/(u_int8_t *)lbp,
2436 /*inq_len*/sizeof(*lbp),
2438 /*page_code*/SVPD_LBP,
2439 /*sense_len*/SSD_MIN_SIZE,
2440 /*timeout*/da_default_timeout * 1000);
2441 start_ccb->ccb_h.ccb_bp = NULL;
2442 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_LBP;
2443 xpt_action(start_ccb);
2446 case DA_STATE_PROBE_BLK_LIMITS:
2448 struct scsi_vpd_block_limits *block_limits;
2450 if (!scsi_vpd_supported_page(periph, SVPD_BLOCK_LIMITS)) {
2451 /* Not supported skip to next probe */
2452 softc->state = DA_STATE_PROBE_BDC;
2456 block_limits = (struct scsi_vpd_block_limits *)
2457 malloc(sizeof(*block_limits), M_SCSIDA, M_NOWAIT|M_ZERO);
2459 if (block_limits == NULL) {
2460 printf("dastart: Couldn't malloc block_limits data\n");
2461 /* da_free_periph??? */
2465 scsi_inquiry(&start_ccb->csio,
2466 /*retries*/da_retry_count,
2468 /*tag_action*/MSG_SIMPLE_Q_TAG,
2469 /*inq_buf*/(u_int8_t *)block_limits,
2470 /*inq_len*/sizeof(*block_limits),
2472 /*page_code*/SVPD_BLOCK_LIMITS,
2473 /*sense_len*/SSD_MIN_SIZE,
2474 /*timeout*/da_default_timeout * 1000);
2475 start_ccb->ccb_h.ccb_bp = NULL;
2476 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BLK_LIMITS;
2477 xpt_action(start_ccb);
2480 case DA_STATE_PROBE_BDC:
2482 struct scsi_vpd_block_characteristics *bdc;
2484 if (!scsi_vpd_supported_page(periph, SVPD_BDC)) {
2485 softc->state = DA_STATE_PROBE_ATA;
2489 bdc = (struct scsi_vpd_block_characteristics *)
2490 malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
2493 printf("dastart: Couldn't malloc bdc data\n");
2494 /* da_free_periph??? */
2498 scsi_inquiry(&start_ccb->csio,
2499 /*retries*/da_retry_count,
2501 /*tag_action*/MSG_SIMPLE_Q_TAG,
2502 /*inq_buf*/(u_int8_t *)bdc,
2503 /*inq_len*/sizeof(*bdc),
2505 /*page_code*/SVPD_BDC,
2506 /*sense_len*/SSD_MIN_SIZE,
2507 /*timeout*/da_default_timeout * 1000);
2508 start_ccb->ccb_h.ccb_bp = NULL;
2509 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BDC;
2510 xpt_action(start_ccb);
2513 case DA_STATE_PROBE_ATA:
2515 struct ata_params *ata_params;
2517 if (!scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
2518 daprobedone(periph, start_ccb);
2522 ata_params = (struct ata_params*)
2523 malloc(sizeof(*ata_params), M_SCSIDA, M_NOWAIT|M_ZERO);
2525 if (ata_params == NULL) {
2526 printf("dastart: Couldn't malloc ata_params data\n");
2527 /* da_free_periph??? */
2531 scsi_ata_identify(&start_ccb->csio,
2532 /*retries*/da_retry_count,
2534 /*tag_action*/MSG_SIMPLE_Q_TAG,
2535 /*data_ptr*/(u_int8_t *)ata_params,
2536 /*dxfer_len*/sizeof(*ata_params),
2537 /*sense_len*/SSD_FULL_SIZE,
2538 /*timeout*/da_default_timeout * 1000);
2539 start_ccb->ccb_h.ccb_bp = NULL;
2540 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA;
2541 xpt_action(start_ccb);
2548 * In each of the methods below, while its the caller's
2549 * responsibility to ensure the request will fit into a
2550 * single device request, we might have changed the delete
2551 * method due to the device incorrectly advertising either
2552 * its supported methods or limits.
2554 * To prevent this causing further issues we validate the
2555 * against the methods limits, and warn which would
2556 * otherwise be unnecessary.
2559 da_delete_unmap(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2561 struct da_softc *softc = (struct da_softc *)periph->softc;;
2563 uint8_t *buf = softc->unmap_buf;
2564 uint64_t lba, lastlba = (uint64_t)-1;
2565 uint64_t totalcount = 0;
2567 uint32_t lastcount = 0, c;
2568 uint32_t off, ranges = 0;
2571 * Currently this doesn't take the UNMAP
2572 * Granularity and Granularity Alignment
2573 * fields into account.
2575 * This could result in both unoptimal unmap
2576 * requests as as well as UNMAP calls unmapping
2577 * fewer LBA's than requested.
2580 softc->delete_running = 1;
2581 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2584 bioq_remove(&softc->delete_queue, bp1);
2586 bioq_insert_tail(&softc->delete_run_queue, bp1);
2587 lba = bp1->bio_pblkno;
2588 count = bp1->bio_bcount / softc->params.secsize;
2590 /* Try to extend the previous range. */
2591 if (lba == lastlba) {
2592 c = omin(count, UNMAP_RANGE_MAX - lastcount);
2594 off = ((ranges - 1) * UNMAP_RANGE_SIZE) +
2596 scsi_ulto4b(lastcount, &buf[off + 8]);
2603 c = omin(count, UNMAP_RANGE_MAX);
2604 if (totalcount + c > softc->unmap_max_lba ||
2605 ranges >= softc->unmap_max_ranges) {
2606 xpt_print(periph->path,
2607 "%s issuing short delete %ld > %ld"
2609 da_delete_method_desc[softc->delete_method],
2610 totalcount + c, softc->unmap_max_lba,
2611 ranges, softc->unmap_max_ranges);
2614 off = (ranges * UNMAP_RANGE_SIZE) + UNMAP_HEAD_SIZE;
2615 scsi_u64to8b(lba, &buf[off + 0]);
2616 scsi_ulto4b(c, &buf[off + 8]);
2624 bp1 = bioq_first(&softc->delete_queue);
2625 if (bp1 == NULL || ranges >= softc->unmap_max_ranges ||
2626 totalcount + bp1->bio_bcount /
2627 softc->params.secsize > softc->unmap_max_lba)
2630 scsi_ulto2b(ranges * 16 + 6, &buf[0]);
2631 scsi_ulto2b(ranges * 16, &buf[2]);
2633 scsi_unmap(&ccb->csio,
2634 /*retries*/da_retry_count,
2636 /*tag_action*/MSG_SIMPLE_Q_TAG,
2639 /*dxfer_len*/ ranges * 16 + 8,
2640 /*sense_len*/SSD_FULL_SIZE,
2641 da_default_timeout * 1000);
2642 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2643 ccb->ccb_h.flags |= CAM_UNLOCKED;
2647 da_delete_trim(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2649 struct da_softc *softc = (struct da_softc *)periph->softc;
2651 uint8_t *buf = softc->unmap_buf;
2652 uint64_t lastlba = (uint64_t)-1;
2655 uint32_t lastcount = 0, c, requestcount;
2656 int ranges = 0, off, block_count;
2658 softc->delete_running = 1;
2659 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2662 bioq_remove(&softc->delete_queue, bp1);
2664 bioq_insert_tail(&softc->delete_run_queue, bp1);
2665 lba = bp1->bio_pblkno;
2666 count = bp1->bio_bcount / softc->params.secsize;
2667 requestcount = count;
2669 /* Try to extend the previous range. */
2670 if (lba == lastlba) {
2671 c = min(count, ATA_DSM_RANGE_MAX - lastcount);
2673 off = (ranges - 1) * 8;
2674 buf[off + 6] = lastcount & 0xff;
2675 buf[off + 7] = (lastcount >> 8) & 0xff;
2681 c = min(count, ATA_DSM_RANGE_MAX);
2684 buf[off + 0] = lba & 0xff;
2685 buf[off + 1] = (lba >> 8) & 0xff;
2686 buf[off + 2] = (lba >> 16) & 0xff;
2687 buf[off + 3] = (lba >> 24) & 0xff;
2688 buf[off + 4] = (lba >> 32) & 0xff;
2689 buf[off + 5] = (lba >> 40) & 0xff;
2690 buf[off + 6] = c & 0xff;
2691 buf[off + 7] = (c >> 8) & 0xff;
2696 if (count != 0 && ranges == softc->trim_max_ranges) {
2697 xpt_print(periph->path,
2698 "%s issuing short delete %ld > %ld\n",
2699 da_delete_method_desc[softc->delete_method],
2701 (softc->trim_max_ranges - ranges) *
2707 bp1 = bioq_first(&softc->delete_queue);
2708 if (bp1 == NULL || bp1->bio_bcount / softc->params.secsize >
2709 (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX)
2713 block_count = (ranges + ATA_DSM_BLK_RANGES - 1) / ATA_DSM_BLK_RANGES;
2714 scsi_ata_trim(&ccb->csio,
2715 /*retries*/da_retry_count,
2717 /*tag_action*/MSG_SIMPLE_Q_TAG,
2720 /*dxfer_len*/block_count * ATA_DSM_BLK_SIZE,
2721 /*sense_len*/SSD_FULL_SIZE,
2722 da_default_timeout * 1000);
2723 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2724 ccb->ccb_h.flags |= CAM_UNLOCKED;
2728 * We calculate ws_max_blks here based off d_delmaxsize instead
2729 * of using softc->ws_max_blks as it is absolute max for the
2730 * device not the protocol max which may well be lower.
2733 da_delete_ws(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2735 struct da_softc *softc;
2737 uint64_t ws_max_blks;
2739 uint64_t count; /* forward compat with WS32 */
2741 softc = (struct da_softc *)periph->softc;
2742 ws_max_blks = softc->disk->d_delmaxsize / softc->params.secsize;
2743 softc->delete_running = 1;
2744 lba = bp->bio_pblkno;
2748 bioq_remove(&softc->delete_queue, bp1);
2750 bioq_insert_tail(&softc->delete_run_queue, bp1);
2751 count += bp1->bio_bcount / softc->params.secsize;
2752 if (count > ws_max_blks) {
2753 xpt_print(periph->path,
2754 "%s issuing short delete %ld > %ld\n",
2755 da_delete_method_desc[softc->delete_method],
2756 count, ws_max_blks);
2757 count = min(count, ws_max_blks);
2760 bp1 = bioq_first(&softc->delete_queue);
2761 if (bp1 == NULL || lba + count != bp1->bio_pblkno ||
2762 count + bp1->bio_bcount /
2763 softc->params.secsize > ws_max_blks)
2767 scsi_write_same(&ccb->csio,
2768 /*retries*/da_retry_count,
2770 /*tag_action*/MSG_SIMPLE_Q_TAG,
2771 /*byte2*/softc->delete_method ==
2772 DA_DELETE_ZERO ? 0 : SWS_UNMAP,
2773 softc->delete_method == DA_DELETE_WS16 ? 16 : 10,
2775 /*block_count*/count,
2776 /*data_ptr*/ __DECONST(void *, zero_region),
2777 /*dxfer_len*/ softc->params.secsize,
2778 /*sense_len*/SSD_FULL_SIZE,
2779 da_default_timeout * 1000);
2780 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2781 ccb->ccb_h.flags |= CAM_UNLOCKED;
2785 cmd6workaround(union ccb *ccb)
2787 struct scsi_rw_6 cmd6;
2788 struct scsi_rw_10 *cmd10;
2789 struct da_softc *softc;
2794 cdb = ccb->csio.cdb_io.cdb_bytes;
2795 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
2797 if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
2798 da_delete_methods old_method = softc->delete_method;
2801 * Typically there are two reasons for failure here
2802 * 1. Delete method was detected as supported but isn't
2803 * 2. Delete failed due to invalid params e.g. too big
2805 * While we will attempt to choose an alternative delete method
2806 * this may result in short deletes if the existing delete
2807 * requests from geom are big for the new method choosen.
2809 * This method assumes that the error which triggered this
2810 * will not retry the io otherwise a panic will occur
2812 dadeleteflag(softc, old_method, 0);
2813 dadeletemethodchoose(softc, DA_DELETE_DISABLE);
2814 if (softc->delete_method == DA_DELETE_DISABLE)
2815 xpt_print(ccb->ccb_h.path,
2816 "%s failed, disabling BIO_DELETE\n",
2817 da_delete_method_desc[old_method]);
2819 xpt_print(ccb->ccb_h.path,
2820 "%s failed, switching to %s BIO_DELETE\n",
2821 da_delete_method_desc[old_method],
2822 da_delete_method_desc[softc->delete_method]);
2824 while ((bp = bioq_takefirst(&softc->delete_run_queue)) != NULL)
2825 bioq_disksort(&softc->delete_queue, bp);
2826 bioq_disksort(&softc->delete_queue,
2827 (struct bio *)ccb->ccb_h.ccb_bp);
2828 ccb->ccb_h.ccb_bp = NULL;
2832 /* Detect unsupported PREVENT ALLOW MEDIUM REMOVAL. */
2833 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
2834 (*cdb == PREVENT_ALLOW) &&
2835 (softc->quirks & DA_Q_NO_PREVENT) == 0) {
2837 xpt_print(ccb->ccb_h.path,
2838 "PREVENT ALLOW MEDIUM REMOVAL not supported.\n");
2839 softc->quirks |= DA_Q_NO_PREVENT;
2843 /* Detect unsupported SYNCHRONIZE CACHE(10). */
2844 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
2845 (*cdb == SYNCHRONIZE_CACHE) &&
2846 (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
2848 xpt_print(ccb->ccb_h.path,
2849 "SYNCHRONIZE CACHE(10) not supported.\n");
2850 softc->quirks |= DA_Q_NO_SYNC_CACHE;
2851 softc->disk->d_flags &= ~DISKFLAG_CANFLUSHCACHE;
2855 /* Translation only possible if CDB is an array and cmd is R/W6 */
2856 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
2857 (*cdb != READ_6 && *cdb != WRITE_6))
2860 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
2861 "increasing minimum_cmd_size to 10.\n");
2862 softc->minimum_cmd_size = 10;
2864 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
2865 cmd10 = (struct scsi_rw_10 *)cdb;
2866 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
2868 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
2869 cmd10->reserved = 0;
2870 scsi_ulto2b(cmd6.length, cmd10->length);
2871 cmd10->control = cmd6.control;
2872 ccb->csio.cdb_len = sizeof(*cmd10);
2874 /* Requeue request, unfreezing queue if necessary */
2875 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
2876 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2879 cam_release_devq(ccb->ccb_h.path,
2883 /*getcount_only*/0);
2889 dadone(struct cam_periph *periph, union ccb *done_ccb)
2891 struct da_softc *softc;
2892 struct ccb_scsiio *csio;
2896 softc = (struct da_softc *)periph->softc;
2897 priority = done_ccb->ccb_h.pinfo.priority;
2899 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
2901 csio = &done_ccb->csio;
2902 state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
2904 case DA_CCB_BUFFER_IO:
2907 struct bio *bp, *bp1;
2909 cam_periph_lock(periph);
2910 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2911 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2915 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
2920 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
2921 if (error == ERESTART) {
2923 * A retry was scheduled, so
2926 cam_periph_unlock(periph);
2929 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2934 * return all queued I/O with EIO, so that
2935 * the client can retry these I/Os in the
2936 * proper order should it attempt to recover.
2941 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
2943 * Catastrophic error. Mark our pack as
2947 * XXX See if this is really a media
2950 xpt_print(periph->path,
2951 "Invalidating pack\n");
2952 softc->flags |= DA_FLAG_PACK_INVALID;
2953 queued_error = ENXIO;
2955 bioq_flush(&softc->bio_queue, NULL,
2958 bp->bio_error = error;
2959 bp->bio_resid = bp->bio_bcount;
2960 bp->bio_flags |= BIO_ERROR;
2962 } else if (bp != NULL) {
2963 if (state == DA_CCB_DELETE)
2966 bp->bio_resid = csio->resid;
2968 if (bp->bio_resid != 0)
2969 bp->bio_flags |= BIO_ERROR;
2971 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2972 cam_release_devq(done_ccb->ccb_h.path,
2976 /*getcount_only*/0);
2977 } else if (bp != NULL) {
2978 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2979 panic("REQ_CMP with QFRZN");
2980 if (state == DA_CCB_DELETE)
2983 bp->bio_resid = csio->resid;
2984 if (csio->resid > 0)
2985 bp->bio_flags |= BIO_ERROR;
2986 if (softc->error_inject != 0) {
2987 bp->bio_error = softc->error_inject;
2988 bp->bio_resid = bp->bio_bcount;
2989 bp->bio_flags |= BIO_ERROR;
2990 softc->error_inject = 0;
2994 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
2995 if (LIST_EMPTY(&softc->pending_ccbs))
2996 softc->flags |= DA_FLAG_WAS_OTAG;
2998 xpt_release_ccb(done_ccb);
2999 if (state == DA_CCB_DELETE) {
3000 TAILQ_HEAD(, bio) queue;
3003 TAILQ_CONCAT(&queue, &softc->delete_run_queue.queue, bio_queue);
3004 softc->delete_run_queue.insert_point = NULL;
3005 softc->delete_running = 0;
3007 cam_periph_unlock(periph);
3008 while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
3009 TAILQ_REMOVE(&queue, bp1, bio_queue);
3010 bp1->bio_error = bp->bio_error;
3011 if (bp->bio_flags & BIO_ERROR) {
3012 bp1->bio_flags |= BIO_ERROR;
3013 bp1->bio_resid = bp1->bio_bcount;
3019 cam_periph_unlock(periph);
3024 case DA_CCB_PROBE_RC:
3025 case DA_CCB_PROBE_RC16:
3027 struct scsi_read_capacity_data *rdcap;
3028 struct scsi_read_capacity_data_long *rcaplong;
3029 char announce_buf[80];
3035 if (state == DA_CCB_PROBE_RC)
3036 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
3038 rcaplong = (struct scsi_read_capacity_data_long *)
3041 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3042 struct disk_params *dp;
3043 uint32_t block_size;
3045 u_int lalba; /* Lowest aligned LBA. */
3047 if (state == DA_CCB_PROBE_RC) {
3048 block_size = scsi_4btoul(rdcap->length);
3049 maxsector = scsi_4btoul(rdcap->addr);
3053 * According to SBC-2, if the standard 10
3054 * byte READ CAPACITY command returns 2^32,
3055 * we should issue the 16 byte version of
3056 * the command, since the device in question
3057 * has more sectors than can be represented
3058 * with the short version of the command.
3060 if (maxsector == 0xffffffff) {
3061 free(rdcap, M_SCSIDA);
3062 xpt_release_ccb(done_ccb);
3063 softc->state = DA_STATE_PROBE_RC16;
3064 xpt_schedule(periph, priority);
3068 block_size = scsi_4btoul(rcaplong->length);
3069 maxsector = scsi_8btou64(rcaplong->addr);
3070 lalba = scsi_2btoul(rcaplong->lalba_lbp);
3074 * Because GEOM code just will panic us if we
3075 * give them an 'illegal' value we'll avoid that
3078 if (block_size == 0 && maxsector == 0) {
3082 if (block_size >= MAXPHYS || block_size == 0) {
3083 xpt_print(periph->path,
3084 "unsupportable block size %ju\n",
3085 (uintmax_t) block_size);
3086 announce_buf[0] = '\0';
3087 cam_periph_invalidate(periph);
3090 * We pass rcaplong into dasetgeom(),
3091 * because it will only use it if it is
3094 dasetgeom(periph, block_size, maxsector,
3095 rcaplong, sizeof(*rcaplong));
3096 lbp = (lalba & SRC16_LBPME_A);
3097 dp = &softc->params;
3098 snprintf(announce_buf, sizeof(announce_buf),
3099 "%juMB (%ju %u byte sectors: %dH %dS/T "
3101 (((uintmax_t)dp->secsize *
3102 dp->sectors) / (1024*1024)),
3103 (uintmax_t)dp->sectors,
3104 dp->secsize, dp->heads,
3105 dp->secs_per_track, dp->cylinders);
3110 announce_buf[0] = '\0';
3113 * Retry any UNIT ATTENTION type errors. They
3114 * are expected at boot.
3116 error = daerror(done_ccb, CAM_RETRY_SELTO,
3117 SF_RETRY_UA|SF_NO_PRINT);
3118 if (error == ERESTART) {
3120 * A retry was scheuled, so
3124 } else if (error != 0) {
3126 int sense_key, error_code;
3129 struct ccb_getdev cgd;
3131 /* Don't wedge this device's queue */
3132 status = done_ccb->ccb_h.status;
3133 if ((status & CAM_DEV_QFRZN) != 0)
3134 cam_release_devq(done_ccb->ccb_h.path,
3138 /*getcount_only*/0);
3141 xpt_setup_ccb(&cgd.ccb_h,
3142 done_ccb->ccb_h.path,
3143 CAM_PRIORITY_NORMAL);
3144 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
3145 xpt_action((union ccb *)&cgd);
3147 if (scsi_extract_sense_ccb(done_ccb,
3148 &error_code, &sense_key, &asc, &ascq))
3154 * If we tried READ CAPACITY(16) and failed,
3155 * fallback to READ CAPACITY(10).
3157 if ((state == DA_CCB_PROBE_RC16) &&
3158 (softc->flags & DA_FLAG_CAN_RC16) &&
3159 (((csio->ccb_h.status & CAM_STATUS_MASK) ==
3162 (error_code == SSD_CURRENT_ERROR) &&
3163 (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
3164 softc->flags &= ~DA_FLAG_CAN_RC16;
3165 free(rdcap, M_SCSIDA);
3166 xpt_release_ccb(done_ccb);
3167 softc->state = DA_STATE_PROBE_RC;
3168 xpt_schedule(periph, priority);
3172 * Attach to anything that claims to be a
3173 * direct access or optical disk device,
3174 * as long as it doesn't return a "Logical
3175 * unit not supported" (0x25) error.
3177 if ((have_sense) && (asc != 0x25)
3178 && (error_code == SSD_CURRENT_ERROR)) {
3179 const char *sense_key_desc;
3180 const char *asc_desc;
3182 dasetgeom(periph, 512, -1, NULL, 0);
3183 scsi_sense_desc(sense_key, asc, ascq,
3187 snprintf(announce_buf,
3188 sizeof(announce_buf),
3189 "Attempt to query device "
3190 "size failed: %s, %s",
3198 xpt_print(periph->path,
3199 "got CAM status %#x\n",
3200 done_ccb->ccb_h.status);
3203 xpt_print(periph->path, "fatal error, "
3204 "failed to attach to device\n");
3207 * Free up resources.
3209 cam_periph_invalidate(periph);
3213 free(csio->data_ptr, M_SCSIDA);
3214 if (announce_buf[0] != '\0' &&
3215 ((softc->flags & DA_FLAG_ANNOUNCED) == 0)) {
3217 * Create our sysctl variables, now that we know
3218 * we have successfully attached.
3220 /* increase the refcount */
3221 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3222 taskqueue_enqueue(taskqueue_thread,
3223 &softc->sysctl_task);
3224 xpt_announce_periph(periph, announce_buf);
3225 xpt_announce_quirks(periph, softc->quirks,
3228 xpt_print(periph->path, "fatal error, "
3229 "could not acquire reference count\n");
3233 /* We already probed the device. */
3234 if (softc->flags & DA_FLAG_PROBED) {
3235 daprobedone(periph, done_ccb);
3239 /* Ensure re-probe doesn't see old delete. */
3240 softc->delete_available = 0;
3241 if (lbp && (softc->quirks & DA_Q_NO_UNMAP) == 0) {
3243 * Based on older SBC-3 spec revisions
3244 * any of the UNMAP methods "may" be
3245 * available via LBP given this flag so
3246 * we flag all of them as availble and
3247 * then remove those which further
3248 * probes confirm aren't available
3251 * We could also check readcap(16) p_type
3252 * flag to exclude one or more invalid
3253 * write same (X) types here
3255 dadeleteflag(softc, DA_DELETE_WS16, 1);
3256 dadeleteflag(softc, DA_DELETE_WS10, 1);
3257 dadeleteflag(softc, DA_DELETE_ZERO, 1);
3258 dadeleteflag(softc, DA_DELETE_UNMAP, 1);
3260 xpt_release_ccb(done_ccb);
3261 softc->state = DA_STATE_PROBE_LBP;
3262 xpt_schedule(periph, priority);
3266 xpt_release_ccb(done_ccb);
3267 softc->state = DA_STATE_PROBE_BDC;
3268 xpt_schedule(periph, priority);
3271 case DA_CCB_PROBE_LBP:
3273 struct scsi_vpd_logical_block_prov *lbp;
3275 lbp = (struct scsi_vpd_logical_block_prov *)csio->data_ptr;
3277 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3279 * T10/1799-D Revision 31 states at least one of these
3280 * must be supported but we don't currently enforce this.
3282 dadeleteflag(softc, DA_DELETE_WS16,
3283 (lbp->flags & SVPD_LBP_WS16));
3284 dadeleteflag(softc, DA_DELETE_WS10,
3285 (lbp->flags & SVPD_LBP_WS10));
3286 dadeleteflag(softc, DA_DELETE_ZERO,
3287 (lbp->flags & SVPD_LBP_WS10));
3288 dadeleteflag(softc, DA_DELETE_UNMAP,
3289 (lbp->flags & SVPD_LBP_UNMAP));
3292 error = daerror(done_ccb, CAM_RETRY_SELTO,
3293 SF_RETRY_UA|SF_NO_PRINT);
3294 if (error == ERESTART)
3296 else if (error != 0) {
3297 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3298 /* Don't wedge this device's queue */
3299 cam_release_devq(done_ccb->ccb_h.path,
3303 /*getcount_only*/0);
3307 * Failure indicates we don't support any SBC-3
3308 * delete methods with UNMAP
3313 free(lbp, M_SCSIDA);
3314 xpt_release_ccb(done_ccb);
3315 softc->state = DA_STATE_PROBE_BLK_LIMITS;
3316 xpt_schedule(periph, priority);
3319 case DA_CCB_PROBE_BLK_LIMITS:
3321 struct scsi_vpd_block_limits *block_limits;
3323 block_limits = (struct scsi_vpd_block_limits *)csio->data_ptr;
3325 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3326 uint32_t max_txfer_len = scsi_4btoul(
3327 block_limits->max_txfer_len);
3328 uint32_t max_unmap_lba_cnt = scsi_4btoul(
3329 block_limits->max_unmap_lba_cnt);
3330 uint32_t max_unmap_blk_cnt = scsi_4btoul(
3331 block_limits->max_unmap_blk_cnt);
3332 uint64_t ws_max_blks = scsi_8btou64(
3333 block_limits->max_write_same_length);
3335 if (max_txfer_len != 0) {
3336 softc->disk->d_maxsize = MIN(softc->maxio,
3337 (off_t)max_txfer_len * softc->params.secsize);
3341 * We should already support UNMAP but we check lba
3342 * and block count to be sure
3344 if (max_unmap_lba_cnt != 0x00L &&
3345 max_unmap_blk_cnt != 0x00L) {
3346 softc->unmap_max_lba = max_unmap_lba_cnt;
3347 softc->unmap_max_ranges = min(max_unmap_blk_cnt,
3351 * Unexpected UNMAP limits which means the
3352 * device doesn't actually support UNMAP
3354 dadeleteflag(softc, DA_DELETE_UNMAP, 0);
3357 if (ws_max_blks != 0x00L)
3358 softc->ws_max_blks = ws_max_blks;
3361 error = daerror(done_ccb, CAM_RETRY_SELTO,
3362 SF_RETRY_UA|SF_NO_PRINT);
3363 if (error == ERESTART)
3365 else if (error != 0) {
3366 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3367 /* Don't wedge this device's queue */
3368 cam_release_devq(done_ccb->ccb_h.path,
3372 /*getcount_only*/0);
3376 * Failure here doesn't mean UNMAP is not
3377 * supported as this is an optional page.
3379 softc->unmap_max_lba = 1;
3380 softc->unmap_max_ranges = 1;
3384 free(block_limits, M_SCSIDA);
3385 xpt_release_ccb(done_ccb);
3386 softc->state = DA_STATE_PROBE_BDC;
3387 xpt_schedule(periph, priority);
3390 case DA_CCB_PROBE_BDC:
3392 struct scsi_vpd_block_characteristics *bdc;
3394 bdc = (struct scsi_vpd_block_characteristics *)csio->data_ptr;
3396 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3398 * Disable queue sorting for non-rotational media
3401 u_int16_t old_rate = softc->disk->d_rotation_rate;
3403 softc->disk->d_rotation_rate =
3404 scsi_2btoul(bdc->medium_rotation_rate);
3405 if (softc->disk->d_rotation_rate ==
3406 SVPD_BDC_RATE_NON_ROTATING) {
3407 softc->sort_io_queue = 0;
3409 if (softc->disk->d_rotation_rate != old_rate) {
3410 disk_attr_changed(softc->disk,
3411 "GEOM::rotation_rate", M_NOWAIT);
3415 error = daerror(done_ccb, CAM_RETRY_SELTO,
3416 SF_RETRY_UA|SF_NO_PRINT);
3417 if (error == ERESTART)
3419 else if (error != 0) {
3420 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3421 /* Don't wedge this device's queue */
3422 cam_release_devq(done_ccb->ccb_h.path,
3426 /*getcount_only*/0);
3431 free(bdc, M_SCSIDA);
3432 xpt_release_ccb(done_ccb);
3433 softc->state = DA_STATE_PROBE_ATA;
3434 xpt_schedule(periph, priority);
3437 case DA_CCB_PROBE_ATA:
3440 struct ata_params *ata_params;
3443 ata_params = (struct ata_params *)csio->data_ptr;
3444 ptr = (uint16_t *)ata_params;
3446 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3449 for (i = 0; i < sizeof(*ata_params) / 2; i++)
3450 ptr[i] = le16toh(ptr[i]);
3451 if (ata_params->support_dsm & ATA_SUPPORT_DSM_TRIM &&
3452 (softc->quirks & DA_Q_NO_UNMAP) == 0) {
3453 dadeleteflag(softc, DA_DELETE_ATA_TRIM, 1);
3454 if (ata_params->max_dsm_blocks != 0)
3455 softc->trim_max_ranges = min(
3456 softc->trim_max_ranges,
3457 ata_params->max_dsm_blocks *
3458 ATA_DSM_BLK_RANGES);
3461 * Disable queue sorting for non-rotational media
3464 old_rate = softc->disk->d_rotation_rate;
3465 softc->disk->d_rotation_rate =
3466 ata_params->media_rotation_rate;
3467 if (softc->disk->d_rotation_rate ==
3468 ATA_RATE_NON_ROTATING) {
3469 softc->sort_io_queue = 0;
3472 if (softc->disk->d_rotation_rate != old_rate) {
3473 disk_attr_changed(softc->disk,
3474 "GEOM::rotation_rate", M_NOWAIT);
3478 error = daerror(done_ccb, CAM_RETRY_SELTO,
3479 SF_RETRY_UA|SF_NO_PRINT);
3480 if (error == ERESTART)
3482 else if (error != 0) {
3483 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3484 /* Don't wedge this device's queue */
3485 cam_release_devq(done_ccb->ccb_h.path,
3489 /*getcount_only*/0);
3494 free(ata_params, M_SCSIDA);
3495 daprobedone(periph, done_ccb);
3499 /* No-op. We're polling */
3503 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3505 if (daerror(done_ccb, CAM_RETRY_SELTO,
3506 SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
3509 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3510 cam_release_devq(done_ccb->ccb_h.path,
3514 /*getcount_only*/0);
3516 xpt_release_ccb(done_ccb);
3517 cam_periph_release_locked(periph);
3523 xpt_release_ccb(done_ccb);
3527 dareprobe(struct cam_periph *periph)
3529 struct da_softc *softc;
3532 softc = (struct da_softc *)periph->softc;
3534 /* Probe in progress; don't interfere. */
3535 if (softc->state != DA_STATE_NORMAL)
3538 status = cam_periph_acquire(periph);
3539 KASSERT(status == CAM_REQ_CMP,
3540 ("dareprobe: cam_periph_acquire failed"));
3542 if (softc->flags & DA_FLAG_CAN_RC16)
3543 softc->state = DA_STATE_PROBE_RC16;
3545 softc->state = DA_STATE_PROBE_RC;
3547 xpt_schedule(periph, CAM_PRIORITY_DEV);
3551 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3553 struct da_softc *softc;
3554 struct cam_periph *periph;
3555 int error, error_code, sense_key, asc, ascq;
3557 periph = xpt_path_periph(ccb->ccb_h.path);
3558 softc = (struct da_softc *)periph->softc;
3561 * Automatically detect devices that do not support
3562 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
3565 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3566 error = cmd6workaround(ccb);
3567 } else if (scsi_extract_sense_ccb(ccb,
3568 &error_code, &sense_key, &asc, &ascq)) {
3569 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3570 error = cmd6workaround(ccb);
3572 * If the target replied with CAPACITY DATA HAS CHANGED UA,
3573 * query the capacity and notify upper layers.
3575 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3576 asc == 0x2A && ascq == 0x09) {
3577 xpt_print(periph->path, "Capacity data has changed\n");
3578 softc->flags &= ~DA_FLAG_PROBED;
3580 sense_flags |= SF_NO_PRINT;
3581 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3582 asc == 0x28 && ascq == 0x00) {
3583 softc->flags &= ~DA_FLAG_PROBED;
3584 disk_media_changed(softc->disk, M_NOWAIT);
3585 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3586 asc == 0x3F && ascq == 0x03) {
3587 xpt_print(periph->path, "INQUIRY data has changed\n");
3588 softc->flags &= ~DA_FLAG_PROBED;
3590 sense_flags |= SF_NO_PRINT;
3591 } else if (sense_key == SSD_KEY_NOT_READY &&
3592 asc == 0x3a && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
3593 softc->flags |= DA_FLAG_PACK_INVALID;
3594 disk_media_gone(softc->disk, M_NOWAIT);
3597 if (error == ERESTART)
3602 * Until we have a better way of doing pack validation,
3603 * don't treat UAs as errors.
3605 sense_flags |= SF_RETRY_UA;
3606 return(cam_periph_error(ccb, cam_flags, sense_flags,
3607 &softc->saved_ccb));
3611 damediapoll(void *arg)
3613 struct cam_periph *periph = arg;
3614 struct da_softc *softc = periph->softc;
3616 if (!softc->tur && LIST_EMPTY(&softc->pending_ccbs)) {
3617 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3622 /* Queue us up again */
3623 if (da_poll_period != 0)
3624 callout_schedule(&softc->mediapoll_c, da_poll_period * hz);
3628 daprevent(struct cam_periph *periph, int action)
3630 struct da_softc *softc;
3634 softc = (struct da_softc *)periph->softc;
3636 if (((action == PR_ALLOW)
3637 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
3638 || ((action == PR_PREVENT)
3639 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
3643 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3645 scsi_prevent(&ccb->csio,
3653 error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
3654 SF_RETRY_UA | SF_NO_PRINT, softc->disk->d_devstat);
3657 if (action == PR_ALLOW)
3658 softc->flags &= ~DA_FLAG_PACK_LOCKED;
3660 softc->flags |= DA_FLAG_PACK_LOCKED;
3663 xpt_release_ccb(ccb);
3667 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
3668 struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
3670 struct ccb_calc_geometry ccg;
3671 struct da_softc *softc;
3672 struct disk_params *dp;
3673 u_int lbppbe, lalba;
3676 softc = (struct da_softc *)periph->softc;
3678 dp = &softc->params;
3679 dp->secsize = block_len;
3680 dp->sectors = maxsector + 1;
3681 if (rcaplong != NULL) {
3682 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
3683 lalba = scsi_2btoul(rcaplong->lalba_lbp);
3684 lalba &= SRC16_LALBA_A;
3691 dp->stripesize = block_len << lbppbe;
3692 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
3694 } else if (softc->quirks & DA_Q_4K) {
3695 dp->stripesize = 4096;
3696 dp->stripeoffset = 0;
3699 dp->stripeoffset = 0;
3702 * Have the controller provide us with a geometry
3703 * for this disk. The only time the geometry
3704 * matters is when we boot and the controller
3705 * is the only one knowledgeable enough to come
3706 * up with something that will make this a bootable
3709 xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3710 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
3711 ccg.block_size = dp->secsize;
3712 ccg.volume_size = dp->sectors;
3714 ccg.secs_per_track = 0;
3716 xpt_action((union ccb*)&ccg);
3717 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3719 * We don't know what went wrong here- but just pick
3720 * a geometry so we don't have nasty things like divide
3724 dp->secs_per_track = 255;
3725 dp->cylinders = dp->sectors / (255 * 255);
3726 if (dp->cylinders == 0) {
3730 dp->heads = ccg.heads;
3731 dp->secs_per_track = ccg.secs_per_track;
3732 dp->cylinders = ccg.cylinders;
3736 * If the user supplied a read capacity buffer, and if it is
3737 * different than the previous buffer, update the data in the EDT.
3738 * If it's the same, we don't bother. This avoids sending an
3739 * update every time someone opens this device.
3741 if ((rcaplong != NULL)
3742 && (bcmp(rcaplong, &softc->rcaplong,
3743 min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
3744 struct ccb_dev_advinfo cdai;
3746 xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3747 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
3748 cdai.buftype = CDAI_TYPE_RCAPLONG;
3749 cdai.flags |= CDAI_FLAG_STORE;
3750 cdai.bufsiz = rcap_len;
3751 cdai.buf = (uint8_t *)rcaplong;
3752 xpt_action((union ccb *)&cdai);
3753 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
3754 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
3755 if (cdai.ccb_h.status != CAM_REQ_CMP) {
3756 xpt_print(periph->path, "%s: failed to set read "
3757 "capacity advinfo\n", __func__);
3758 /* Use cam_error_print() to decode the status */
3759 cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
3762 bcopy(rcaplong, &softc->rcaplong,
3763 min(sizeof(softc->rcaplong), rcap_len));
3767 softc->disk->d_sectorsize = softc->params.secsize;
3768 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
3769 softc->disk->d_stripesize = softc->params.stripesize;
3770 softc->disk->d_stripeoffset = softc->params.stripeoffset;
3771 /* XXX: these are not actually "firmware" values, so they may be wrong */
3772 softc->disk->d_fwsectors = softc->params.secs_per_track;
3773 softc->disk->d_fwheads = softc->params.heads;
3774 softc->disk->d_devstat->block_size = softc->params.secsize;
3775 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
3777 error = disk_resize(softc->disk, M_NOWAIT);
3779 xpt_print(periph->path, "disk_resize(9) failed, error = %d\n", error);
3783 dasendorderedtag(void *arg)
3785 struct da_softc *softc = arg;
3787 if (da_send_ordered) {
3788 if (!LIST_EMPTY(&softc->pending_ccbs)) {
3789 if ((softc->flags & DA_FLAG_WAS_OTAG) == 0)
3790 softc->flags |= DA_FLAG_NEED_OTAG;
3791 softc->flags &= ~DA_FLAG_WAS_OTAG;
3794 /* Queue us up again */
3795 callout_reset(&softc->sendordered_c,
3796 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
3797 dasendorderedtag, softc);
3801 * Step through all DA peripheral drivers, and if the device is still open,
3802 * sync the disk cache to physical media.
3805 dashutdown(void * arg, int howto)
3807 struct cam_periph *periph;
3808 struct da_softc *softc;
3812 CAM_PERIPH_FOREACH(periph, &dadriver) {
3813 softc = (struct da_softc *)periph->softc;
3814 if (SCHEDULER_STOPPED()) {
3815 /* If we paniced with the lock held, do not recurse. */
3816 if (!cam_periph_owned(periph) &&
3817 (softc->flags & DA_FLAG_OPEN)) {
3818 dadump(softc->disk, NULL, 0, 0, 0);
3822 cam_periph_lock(periph);
3825 * We only sync the cache if the drive is still open, and
3826 * if the drive is capable of it..
3828 if (((softc->flags & DA_FLAG_OPEN) == 0)
3829 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
3830 cam_periph_unlock(periph);
3834 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3835 scsi_synchronize_cache(&ccb->csio,
3839 /*begin_lba*/0, /* whole disk */
3844 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
3845 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR,
3846 softc->disk->d_devstat);
3848 xpt_print(periph->path, "Synchronize cache failed\n");
3849 xpt_release_ccb(ccb);
3850 cam_periph_unlock(periph);
3854 #else /* !_KERNEL */
3857 * XXX This is only left out of the kernel build to silence warnings. If,
3858 * for some reason this function is used in the kernel, the ifdefs should
3859 * be moved so it is included both in the kernel and userland.
3862 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
3863 void (*cbfcnp)(struct cam_periph *, union ccb *),
3864 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
3865 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
3868 struct scsi_format_unit *scsi_cmd;
3870 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
3871 scsi_cmd->opcode = FORMAT_UNIT;
3872 scsi_cmd->byte2 = byte2;
3873 scsi_ulto2b(ileave, scsi_cmd->interleave);
3878 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
3888 scsi_sanitize(struct ccb_scsiio *csio, u_int32_t retries,
3889 void (*cbfcnp)(struct cam_periph *, union ccb *),
3890 u_int8_t tag_action, u_int8_t byte2, u_int16_t control,
3891 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
3894 struct scsi_sanitize *scsi_cmd;
3896 scsi_cmd = (struct scsi_sanitize *)&csio->cdb_io.cdb_bytes;
3897 scsi_cmd->opcode = SANITIZE;
3898 scsi_cmd->byte2 = byte2;
3899 scsi_cmd->control = control;
3900 scsi_ulto2b(dxfer_len, scsi_cmd->length);
3905 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
3914 #endif /* _KERNEL */