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>
76 DA_STATE_PROBE_BLK_LIMITS,
83 DA_FLAG_PACK_INVALID = 0x001,
84 DA_FLAG_NEW_PACK = 0x002,
85 DA_FLAG_PACK_LOCKED = 0x004,
86 DA_FLAG_PACK_REMOVABLE = 0x008,
87 DA_FLAG_NEED_OTAG = 0x020,
88 DA_FLAG_WAS_OTAG = 0x040,
89 DA_FLAG_RETRY_UA = 0x080,
91 DA_FLAG_SCTX_INIT = 0x200,
92 DA_FLAG_CAN_RC16 = 0x400,
93 DA_FLAG_PROBED = 0x800,
94 DA_FLAG_DIRTY = 0x1000,
95 DA_FLAG_ANNOUNCED = 0x2000
100 DA_Q_NO_SYNC_CACHE = 0x01,
101 DA_Q_NO_6_BYTE = 0x02,
102 DA_Q_NO_PREVENT = 0x04,
105 DA_Q_NO_UNMAP = 0x20,
106 DA_Q_RETRY_BUSY = 0x40
109 #define DA_Q_BIT_STRING \
111 "\001NO_SYNC_CACHE" \
120 DA_CCB_PROBE_RC = 0x01,
121 DA_CCB_PROBE_RC16 = 0x02,
122 DA_CCB_PROBE_LBP = 0x03,
123 DA_CCB_PROBE_BLK_LIMITS = 0x04,
124 DA_CCB_PROBE_BDC = 0x05,
125 DA_CCB_PROBE_ATA = 0x06,
126 DA_CCB_BUFFER_IO = 0x07,
128 DA_CCB_DELETE = 0x0B,
130 DA_CCB_PROBE_WP = 0x12,
131 DA_CCB_TYPE_MASK = 0x1F,
132 DA_CCB_RETRY_UA = 0x20
136 * Order here is important for method choice
138 * We prefer ATA_TRIM as tests run against a Sandforce 2281 SSD attached to
139 * LSI 2008 (mps) controller (FW: v12, Drv: v14) resulted 20% quicker deletes
140 * using ATA_TRIM than the corresponding UNMAP results for a real world mysql
141 * import taking 5mins.
152 DA_DELETE_MIN = DA_DELETE_ATA_TRIM,
153 DA_DELETE_MAX = DA_DELETE_ZERO
156 typedef void da_delete_func_t (struct cam_periph *periph, union ccb *ccb,
158 static da_delete_func_t da_delete_trim;
159 static da_delete_func_t da_delete_unmap;
160 static da_delete_func_t da_delete_ws;
162 static const void * da_delete_functions[] = {
172 static const char *da_delete_method_names[] =
173 { "NONE", "DISABLE", "ATA_TRIM", "UNMAP", "WS16", "WS10", "ZERO" };
174 static const char *da_delete_method_desc[] =
175 { "NONE", "DISABLED", "ATA TRIM", "UNMAP", "WRITE SAME(16) with UNMAP",
176 "WRITE SAME(10) with UNMAP", "ZERO" };
178 /* Offsets into our private area for storing information */
179 #define ccb_state ppriv_field0
180 #define ccb_bp ppriv_ptr1
185 u_int8_t secs_per_track;
186 u_int32_t secsize; /* Number of bytes/sector */
187 u_int64_t sectors; /* total number sectors */
192 #define UNMAP_RANGE_MAX 0xffffffff
193 #define UNMAP_HEAD_SIZE 8
194 #define UNMAP_RANGE_SIZE 16
195 #define UNMAP_MAX_RANGES 2048 /* Protocol Max is 4095 */
196 #define UNMAP_BUF_SIZE ((UNMAP_MAX_RANGES * UNMAP_RANGE_SIZE) + \
199 #define WS10_MAX_BLKS 0xffff
200 #define WS16_MAX_BLKS 0xffffffff
201 #define ATA_TRIM_MAX_RANGES ((UNMAP_BUF_SIZE / \
202 (ATA_DSM_RANGE_SIZE * ATA_DSM_BLK_SIZE)) * ATA_DSM_BLK_SIZE)
205 struct bio_queue_head bio_queue;
206 struct bio_queue_head delete_queue;
207 struct bio_queue_head delete_run_queue;
208 LIST_HEAD(, ccb_hdr) pending_ccbs;
209 int tur; /* TEST UNIT READY should be sent */
210 int refcount; /* Active xpt_action() calls */
215 int minimum_cmd_size;
219 int delete_available; /* Delete methods possibly available */
221 uint32_t unmap_max_ranges;
222 uint32_t unmap_max_lba; /* Max LBAs in UNMAP req */
223 uint64_t ws_max_blks;
224 da_delete_methods delete_method_pref;
225 da_delete_methods delete_method;
226 da_delete_func_t *delete_func;
227 struct disk_params params;
230 struct task sysctl_task;
231 struct sysctl_ctx_list sysctl_ctx;
232 struct sysctl_oid *sysctl_tree;
233 struct callout sendordered_c;
235 uint8_t unmap_buf[UNMAP_BUF_SIZE];
236 struct scsi_read_capacity_data_long rcaplong;
237 struct callout mediapoll_c;
240 #define dadeleteflag(softc, delete_method, enable) \
242 softc->delete_available |= (1 << delete_method); \
244 softc->delete_available &= ~(1 << delete_method); \
247 struct da_quirk_entry {
248 struct scsi_inquiry_pattern inq_pat;
252 static const char quantum[] = "QUANTUM";
253 static const char microp[] = "MICROP";
255 static struct da_quirk_entry da_quirk_table[] =
257 /* SPI, FC devices */
260 * Fujitsu M2513A MO drives.
261 * Tested devices: M2513A2 firmware versions 1200 & 1300.
262 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
263 * Reported by: W.Scholten <whs@xs4all.nl>
265 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
266 /*quirks*/ DA_Q_NO_SYNC_CACHE
270 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
271 /*quirks*/ DA_Q_NO_SYNC_CACHE
275 * This particular Fujitsu drive doesn't like the
276 * synchronize cache command.
277 * Reported by: Tom Jackson <toj@gorilla.net>
279 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
280 /*quirks*/ DA_Q_NO_SYNC_CACHE
284 * This drive doesn't like the synchronize cache command
285 * either. Reported by: Matthew Jacob <mjacob@feral.com>
286 * in NetBSD PR kern/6027, August 24, 1998.
288 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
289 /*quirks*/ DA_Q_NO_SYNC_CACHE
293 * This drive doesn't like the synchronize cache command
294 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
297 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
298 /*quirks*/ DA_Q_NO_SYNC_CACHE
302 * Doesn't like the synchronize cache command.
303 * Reported by: Blaz Zupan <blaz@gold.amis.net>
305 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
306 /*quirks*/ DA_Q_NO_SYNC_CACHE
310 * Doesn't like the synchronize cache command.
311 * Reported by: Blaz Zupan <blaz@gold.amis.net>
313 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
314 /*quirks*/ DA_Q_NO_SYNC_CACHE
318 * Doesn't like the synchronize cache command.
320 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
321 /*quirks*/ DA_Q_NO_SYNC_CACHE
325 * Doesn't like the synchronize cache command.
326 * Reported by: walter@pelissero.de
328 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
329 /*quirks*/ DA_Q_NO_SYNC_CACHE
333 * Doesn't work correctly with 6 byte reads/writes.
334 * Returns illegal request, and points to byte 9 of the
336 * Reported by: Adam McDougall <bsdx@spawnet.com>
338 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
339 /*quirks*/ DA_Q_NO_6_BYTE
343 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
344 /*quirks*/ DA_Q_NO_6_BYTE
348 * Doesn't like the synchronize cache command.
349 * Reported by: walter@pelissero.de
351 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
352 /*quirks*/ DA_Q_NO_SYNC_CACHE
356 * The CISS RAID controllers do not support SYNC_CACHE
358 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
359 /*quirks*/ DA_Q_NO_SYNC_CACHE
363 * The STEC SSDs sometimes hang on UNMAP.
365 {T_DIRECT, SIP_MEDIA_FIXED, "STEC", "*", "*"},
366 /*quirks*/ DA_Q_NO_UNMAP
370 * VMware returns BUSY status when storage has transient
371 * connectivity problems, so better wait.
373 {T_DIRECT, SIP_MEDIA_FIXED, "VMware*", "*", "*"},
374 /*quirks*/ DA_Q_RETRY_BUSY
376 /* USB mass storage devices supported by umass(4) */
379 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
382 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
383 /*quirks*/ DA_Q_NO_SYNC_CACHE
387 * Power Quotient Int. (PQI) USB flash key
390 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
391 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
395 * Creative Nomad MUVO mp3 player (USB)
398 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
399 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
403 * Jungsoft NEXDISK USB flash key
406 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
407 /*quirks*/ DA_Q_NO_SYNC_CACHE
411 * FreeDik USB Mini Data Drive
414 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
415 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
419 * Sigmatel USB Flash MP3 Player
422 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
423 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
427 * Neuros USB Digital Audio Computer
430 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
431 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
435 * SEAGRAND NP-900 MP3 Player
438 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
439 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
443 * iRiver iFP MP3 player (with UMS Firmware)
444 * PR: kern/54881, i386/63941, kern/66124
446 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
447 /*quirks*/ DA_Q_NO_SYNC_CACHE
451 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
454 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
455 /*quirks*/ DA_Q_NO_SYNC_CACHE
459 * ZICPlay USB MP3 Player with FM
462 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
463 /*quirks*/ DA_Q_NO_SYNC_CACHE
467 * TEAC USB floppy mechanisms
469 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
470 /*quirks*/ DA_Q_NO_SYNC_CACHE
474 * Kingston DataTraveler II+ USB Pen-Drive.
475 * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
477 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
478 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
486 {T_DIRECT, SIP_MEDIA_REMOVABLE, " ", "USB DISK Pro", "PMAP"},
487 /*quirks*/ DA_Q_NO_SYNC_CACHE
491 * Motorola E398 Mobile Phone (TransFlash memory card).
492 * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
495 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
496 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
500 * Qware BeatZkey! Pro
503 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
504 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
508 * Time DPA20B 1GB MP3 Player
511 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
512 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
516 * Samsung USB key 128Mb
519 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
520 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
524 * Kingston DataTraveler 2.0 USB Flash memory.
527 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
528 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
532 * Creative MUVO Slim mp3 player (USB)
535 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
536 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
540 * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
543 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
544 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
548 * SanDisk Micro Cruzer 128MB
551 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
552 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
556 * TOSHIBA TransMemory USB sticks
559 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
560 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
564 * PNY USB 3.0 Flash Drives
566 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PNY", "USB 3.0 FD*",
567 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_RC16
572 * PR: usb/75578, usb/72344, usb/65436
574 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
575 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
579 * Genesys 6-in-1 Card Reader
582 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
583 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
587 * Rekam Digital CAMERA
590 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
591 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
595 * iRiver H10 MP3 player
598 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
599 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
603 * iRiver U10 MP3 player
606 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
607 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
614 {T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
615 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
619 * EasyMP3 EM732X USB 2.0 Flash MP3 Player
622 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
623 "1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
630 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
631 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
635 * Philips USB Key Audio KEY013
638 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
639 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
646 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
647 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
654 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
655 /*quirks*/ DA_Q_NO_SYNC_CACHE
659 * I/O Magic USB flash - Giga Bank
662 {T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
663 /*quirks*/ DA_Q_NO_SYNC_CACHE
667 * JoyFly 128mb USB Flash Drive
670 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
671 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
678 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
679 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
683 * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A
686 {T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*",
687 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
691 * Samsung YP-U3 mp3-player
694 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3",
695 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
698 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
699 "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
703 * Sony Cyber-Shot DSC cameras
706 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
707 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
710 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler G3",
711 "1.00"}, /*quirks*/ DA_Q_NO_PREVENT
714 /* At least several Transcent USB sticks lie on RC16. */
715 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JetFlash", "Transcend*",
716 "*"}, /*quirks*/ DA_Q_NO_RC16
720 * I-O Data USB Flash Disk
723 {T_DIRECT, SIP_MEDIA_REMOVABLE, "I-O DATA", "USB Flash Disk*",
724 "*"}, /*quirks*/ DA_Q_NO_RC16
726 /* ATA/SATA devices over SAS/USB/... */
728 /* Hitachi Advanced Format (4k) drives */
729 { T_DIRECT, SIP_MEDIA_FIXED, "Hitachi", "H??????????E3*", "*" },
733 /* Samsung Advanced Format (4k) drives */
734 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD155UI*", "*" },
738 /* Samsung Advanced Format (4k) drives */
739 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD155UI*", "*" },
743 /* Samsung Advanced Format (4k) drives */
744 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD204UI*", "*" },
748 /* Samsung Advanced Format (4k) drives */
749 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD204UI*", "*" },
753 /* Seagate Barracuda Green Advanced Format (4k) drives */
754 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DL*", "*" },
758 /* Seagate Barracuda Green Advanced Format (4k) drives */
759 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DL", "*", "*" },
763 /* Seagate Barracuda Green Advanced Format (4k) drives */
764 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???DM*", "*" },
768 /* Seagate Barracuda Green Advanced Format (4k) drives */
769 { T_DIRECT, SIP_MEDIA_FIXED, "ST???DM*", "*", "*" },
773 /* Seagate Barracuda Green Advanced Format (4k) drives */
774 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DM*", "*" },
778 /* Seagate Barracuda Green Advanced Format (4k) drives */
779 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DM", "*", "*" },
783 /* Seagate Momentus Advanced Format (4k) drives */
784 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500423AS*", "*" },
788 /* Seagate Momentus Advanced Format (4k) drives */
789 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "3AS*", "*" },
793 /* Seagate Momentus Advanced Format (4k) drives */
794 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500424AS*", "*" },
798 /* Seagate Momentus Advanced Format (4k) drives */
799 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "4AS*", "*" },
803 /* Seagate Momentus Advanced Format (4k) drives */
804 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640423AS*", "*" },
808 /* Seagate Momentus Advanced Format (4k) drives */
809 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "3AS*", "*" },
813 /* Seagate Momentus Advanced Format (4k) drives */
814 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640424AS*", "*" },
818 /* Seagate Momentus Advanced Format (4k) drives */
819 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "4AS*", "*" },
823 /* Seagate Momentus Advanced Format (4k) drives */
824 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750420AS*", "*" },
828 /* Seagate Momentus Advanced Format (4k) drives */
829 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "0AS*", "*" },
833 /* Seagate Momentus Advanced Format (4k) drives */
834 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750422AS*", "*" },
838 /* Seagate Momentus Advanced Format (4k) drives */
839 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "2AS*", "*" },
843 /* Seagate Momentus Advanced Format (4k) drives */
844 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750423AS*", "*" },
848 /* Seagate Momentus Advanced Format (4k) drives */
849 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "3AS*", "*" },
853 /* Seagate Momentus Thin Advanced Format (4k) drives */
854 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???LT*", "*" },
858 /* Seagate Momentus Thin Advanced Format (4k) drives */
859 { T_DIRECT, SIP_MEDIA_FIXED, "ST???LT*", "*", "*" },
863 /* WDC Caviar Green Advanced Format (4k) drives */
864 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RS*", "*" },
868 /* WDC Caviar Green Advanced Format (4k) drives */
869 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RS*", "*" },
873 /* WDC Caviar Green Advanced Format (4k) drives */
874 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RX*", "*" },
878 /* WDC Caviar Green Advanced Format (4k) drives */
879 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RX*", "*" },
883 /* WDC Caviar Green Advanced Format (4k) drives */
884 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RS*", "*" },
888 /* WDC Caviar Green Advanced Format (4k) drives */
889 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RS*", "*" },
893 /* WDC Caviar Green Advanced Format (4k) drives */
894 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RX*", "*" },
898 /* WDC Caviar Green Advanced Format (4k) drives */
899 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RX*", "*" },
903 /* WDC Scorpio Black Advanced Format (4k) drives */
904 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PKT*", "*" },
908 /* WDC Scorpio Black Advanced Format (4k) drives */
909 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PKT*", "*" },
913 /* WDC Scorpio Black Advanced Format (4k) drives */
914 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PKT*", "*" },
918 /* WDC Scorpio Black Advanced Format (4k) drives */
919 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PKT*", "*" },
923 /* WDC Scorpio Blue Advanced Format (4k) drives */
924 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PVT*", "*" },
928 /* WDC Scorpio Blue Advanced Format (4k) drives */
929 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PVT*", "*" },
933 /* WDC Scorpio Blue Advanced Format (4k) drives */
934 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PVT*", "*" },
938 /* WDC Scorpio Blue Advanced Format (4k) drives */
939 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PVT*", "*" },
944 * Olympus digital cameras (C-3040ZOOM, C-2040ZOOM, C-1)
947 { T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "C*", "*"},
948 /*quirks*/ DA_Q_NO_6_BYTE | DA_Q_NO_SYNC_CACHE
952 * Olympus digital cameras (D-370)
955 { T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "D*", "*"},
956 /*quirks*/ DA_Q_NO_6_BYTE
960 * Olympus digital cameras (E-100RS, E-10).
963 { T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "E*", "*"},
964 /*quirks*/ DA_Q_NO_6_BYTE | DA_Q_NO_SYNC_CACHE
968 * Olympus FE-210 camera
970 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "FE210*",
971 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
977 {T_DIRECT, SIP_MEDIA_REMOVABLE, "LG", "UP3S",
978 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
982 * Laser MP3-2GA13 MP3 player
984 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "(HS) Flash Disk",
985 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
989 * LaCie external 250GB Hard drive des by Porsche
990 * Submitted by: Ben Stuyts <ben@altesco.nl>
993 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HM250JI", "*"},
994 /*quirks*/ DA_Q_NO_SYNC_CACHE
999 * Corsair Force 2 SSDs
1000 * 4k optimised & trim only works in 4k requests + 4k aligned
1002 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair CSSD-F*", "*" },
1007 * Corsair Force 3 SSDs
1008 * 4k optimised & trim only works in 4k requests + 4k aligned
1010 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force 3*", "*" },
1015 * Corsair Neutron GTX SSDs
1016 * 4k optimised & trim only works in 4k requests + 4k aligned
1018 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Neutron GTX*", "*" },
1023 * Corsair Force GT & GS SSDs
1024 * 4k optimised & trim only works in 4k requests + 4k aligned
1026 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force G*", "*" },
1032 * 4k optimised & trim only works in 4k requests + 4k aligned
1034 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "M4-CT???M4SSD2*", "*" },
1039 * Crucial RealSSD C300 SSDs
1042 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "C300-CTFDDAC???MAG*",
1043 "*" }, /*quirks*/DA_Q_4K
1047 * Intel 320 Series SSDs
1048 * 4k optimised & trim only works in 4k requests + 4k aligned
1050 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2CW*", "*" },
1055 * Intel 330 Series SSDs
1056 * 4k optimised & trim only works in 4k requests + 4k aligned
1058 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2CT*", "*" },
1063 * Intel 510 Series SSDs
1064 * 4k optimised & trim only works in 4k requests + 4k aligned
1066 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2MH*", "*" },
1071 * Intel 520 Series SSDs
1072 * 4k optimised & trim only works in 4k requests + 4k aligned
1074 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2BW*", "*" },
1079 * Intel X25-M Series SSDs
1080 * 4k optimised & trim only works in 4k requests + 4k aligned
1082 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2M*", "*" },
1087 * Kingston E100 Series SSDs
1088 * 4k optimised & trim only works in 4k requests + 4k aligned
1090 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SE100S3*", "*" },
1095 * Kingston HyperX 3k SSDs
1096 * 4k optimised & trim only works in 4k requests + 4k aligned
1098 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SH103S3*", "*" },
1103 * Marvell SSDs (entry taken from OpenSolaris)
1104 * 4k optimised & trim only works in 4k requests + 4k aligned
1106 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "MARVELL SD88SA02*", "*" },
1111 * OCZ Agility 2 SSDs
1112 * 4k optimised & trim only works in 4k requests + 4k aligned
1114 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY2*", "*" },
1119 * OCZ Agility 3 SSDs
1120 * 4k optimised & trim only works in 4k requests + 4k aligned
1122 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-AGILITY3*", "*" },
1127 * OCZ Deneva R Series SSDs
1128 * 4k optimised & trim only works in 4k requests + 4k aligned
1130 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "DENRSTE251M45*", "*" },
1135 * OCZ Vertex 2 SSDs (inc pro series)
1136 * 4k optimised & trim only works in 4k requests + 4k aligned
1138 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ?VERTEX2*", "*" },
1144 * 4k optimised & trim only works in 4k requests + 4k aligned
1146 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX3*", "*" },
1152 * 4k optimised & trim only works in 4k requests + 4k aligned
1154 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX4*", "*" },
1159 * Samsung 830 Series SSDs
1160 * 4k optimised & trim only works in 4k requests + 4k aligned
1162 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG SSD 830 Series*", "*" },
1168 * 4k optimised & trim only works in 4k requests + 4k aligned
1170 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 840*", "*" },
1176 * 4k optimised & trim only works in 4k requests + 4k aligned
1178 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 850*", "*" },
1183 * Samsung 843T Series SSDs (MZ7WD*)
1184 * Samsung PM851 Series SSDs (MZ7TE*)
1185 * Samsung PM853T Series SSDs (MZ7GE*)
1186 * Samsung SM863 Series SSDs (MZ7KM*)
1189 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG MZ7*", "*" },
1194 * SuperTalent TeraDrive CT SSDs
1195 * 4k optimised & trim only works in 4k requests + 4k aligned
1197 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "FTM??CT25H*", "*" },
1202 * XceedIOPS SATA SSDs
1205 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SG9XCS2D*", "*" },
1210 * Hama Innostor USB-Stick
1212 { T_DIRECT, SIP_MEDIA_REMOVABLE, "Innostor", "Innostor*", "*" },
1213 /*quirks*/DA_Q_NO_RC16
1217 * MX-ES USB Drive by Mach Xtreme
1219 { T_DIRECT, SIP_MEDIA_REMOVABLE, "MX", "MXUB3*", "*"},
1220 /*quirks*/DA_Q_NO_RC16
1224 static disk_strategy_t dastrategy;
1225 static dumper_t dadump;
1226 static periph_init_t dainit;
1227 static void daasync(void *callback_arg, u_int32_t code,
1228 struct cam_path *path, void *arg);
1229 static void dasysctlinit(void *context, int pending);
1230 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
1231 static int dadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
1232 static int dadeletemaxsysctl(SYSCTL_HANDLER_ARGS);
1233 static void dadeletemethodset(struct da_softc *softc,
1234 da_delete_methods delete_method);
1235 static off_t dadeletemaxsize(struct da_softc *softc,
1236 da_delete_methods delete_method);
1237 static void dadeletemethodchoose(struct da_softc *softc,
1238 da_delete_methods default_method);
1239 static void daprobedone(struct cam_periph *periph, union ccb *ccb);
1241 static periph_ctor_t daregister;
1242 static periph_dtor_t dacleanup;
1243 static periph_start_t dastart;
1244 static periph_oninv_t daoninvalidate;
1245 static void dadone(struct cam_periph *periph,
1246 union ccb *done_ccb);
1247 static int daerror(union ccb *ccb, u_int32_t cam_flags,
1248 u_int32_t sense_flags);
1249 static void daprevent(struct cam_periph *periph, int action);
1250 static void dareprobe(struct cam_periph *periph);
1251 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
1253 struct scsi_read_capacity_data_long *rcaplong,
1255 static timeout_t dasendorderedtag;
1256 static void dashutdown(void *arg, int howto);
1257 static timeout_t damediapoll;
1259 #ifndef DA_DEFAULT_POLL_PERIOD
1260 #define DA_DEFAULT_POLL_PERIOD 3
1263 #ifndef DA_DEFAULT_TIMEOUT
1264 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
1267 #ifndef DA_DEFAULT_RETRY
1268 #define DA_DEFAULT_RETRY 4
1271 #ifndef DA_DEFAULT_SEND_ORDERED
1272 #define DA_DEFAULT_SEND_ORDERED 1
1275 #define DA_SIO (softc->sort_io_queue >= 0 ? \
1276 softc->sort_io_queue : cam_sort_io_queues)
1278 static int da_poll_period = DA_DEFAULT_POLL_PERIOD;
1279 static int da_retry_count = DA_DEFAULT_RETRY;
1280 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
1281 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
1283 static SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
1284 "CAM Direct Access Disk driver");
1285 SYSCTL_INT(_kern_cam_da, OID_AUTO, poll_period, CTLFLAG_RW,
1286 &da_poll_period, 0, "Media polling period in seconds");
1287 TUNABLE_INT("kern.cam.da.poll_period", &da_poll_period);
1288 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
1289 &da_retry_count, 0, "Normal I/O retry count");
1290 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
1291 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
1292 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
1293 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
1294 SYSCTL_INT(_kern_cam_da, OID_AUTO, send_ordered, CTLFLAG_RW,
1295 &da_send_ordered, 0, "Send Ordered Tags");
1296 TUNABLE_INT("kern.cam.da.send_ordered", &da_send_ordered);
1299 * DA_ORDEREDTAG_INTERVAL determines how often, relative
1300 * to the default timeout, we check to see whether an ordered
1301 * tagged transaction is appropriate to prevent simple tag
1302 * starvation. Since we'd like to ensure that there is at least
1303 * 1/2 of the timeout length left for a starved transaction to
1304 * complete after we've sent an ordered tag, we must poll at least
1305 * four times in every timeout period. This takes care of the worst
1306 * case where a starved transaction starts during an interval that
1307 * meets the requirement "don't send an ordered tag" test so it takes
1308 * us two intervals to determine that a tag must be sent.
1310 #ifndef DA_ORDEREDTAG_INTERVAL
1311 #define DA_ORDEREDTAG_INTERVAL 4
1314 static struct periph_driver dadriver =
1317 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
1320 PERIPHDRIVER_DECLARE(da, dadriver);
1322 static MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
1325 daopen(struct disk *dp)
1327 struct cam_periph *periph;
1328 struct da_softc *softc;
1331 periph = (struct cam_periph *)dp->d_drv1;
1332 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1336 cam_periph_lock(periph);
1337 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
1338 cam_periph_unlock(periph);
1339 cam_periph_release(periph);
1343 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1346 softc = (struct da_softc *)periph->softc;
1349 /* Wait for the disk size update. */
1350 error = cam_periph_sleep(periph, &softc->disk->d_mediasize, PRIBIO,
1353 xpt_print(periph->path, "unable to retrieve capacity data\n");
1355 if (periph->flags & CAM_PERIPH_INVALID)
1358 if (error == 0 && (softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1359 (softc->quirks & DA_Q_NO_PREVENT) == 0)
1360 daprevent(periph, PR_PREVENT);
1363 softc->flags &= ~DA_FLAG_PACK_INVALID;
1364 softc->flags |= DA_FLAG_OPEN;
1367 cam_periph_unhold(periph);
1368 cam_periph_unlock(periph);
1371 cam_periph_release(periph);
1377 daclose(struct disk *dp)
1379 struct cam_periph *periph;
1380 struct da_softc *softc;
1384 periph = (struct cam_periph *)dp->d_drv1;
1385 softc = (struct da_softc *)periph->softc;
1386 cam_periph_lock(periph);
1387 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1390 if (cam_periph_hold(periph, PRIBIO) == 0) {
1392 /* Flush disk cache. */
1393 if ((softc->flags & DA_FLAG_DIRTY) != 0 &&
1394 (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0 &&
1395 (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
1396 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1397 scsi_synchronize_cache(&ccb->csio, /*retries*/1,
1398 /*cbfcnp*/dadone, MSG_SIMPLE_Q_TAG,
1399 /*begin_lba*/0, /*lb_count*/0, SSD_FULL_SIZE,
1401 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
1402 /*sense_flags*/SF_RETRY_UA | SF_QUIET_IR,
1403 softc->disk->d_devstat);
1404 softc->flags &= ~DA_FLAG_DIRTY;
1405 xpt_release_ccb(ccb);
1408 /* Allow medium removal. */
1409 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1410 (softc->quirks & DA_Q_NO_PREVENT) == 0)
1411 daprevent(periph, PR_ALLOW);
1413 cam_periph_unhold(periph);
1417 * If we've got removeable media, mark the blocksize as
1418 * unavailable, since it could change when new media is
1421 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0)
1422 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1424 softc->flags &= ~DA_FLAG_OPEN;
1425 while (softc->refcount != 0)
1426 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "daclose", 1);
1427 cam_periph_unlock(periph);
1428 cam_periph_release(periph);
1433 daschedule(struct cam_periph *periph)
1435 struct da_softc *softc = (struct da_softc *)periph->softc;
1437 if (softc->state != DA_STATE_NORMAL)
1440 /* Check if we have more work to do. */
1441 if (bioq_first(&softc->bio_queue) ||
1442 (!softc->delete_running && bioq_first(&softc->delete_queue)) ||
1444 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1449 * Actually translate the requested transfer into one the physical driver
1450 * can understand. The transfer is described by a buf and will include
1451 * only one physical transfer.
1454 dastrategy(struct bio *bp)
1456 struct cam_periph *periph;
1457 struct da_softc *softc;
1459 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1460 softc = (struct da_softc *)periph->softc;
1462 cam_periph_lock(periph);
1465 * If the device has been made invalid, error out
1467 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1468 cam_periph_unlock(periph);
1469 biofinish(bp, NULL, ENXIO);
1473 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
1476 * Place it in the queue of disk activities for this disk
1478 if (bp->bio_cmd == BIO_DELETE) {
1479 bioq_disksort(&softc->delete_queue, bp);
1480 } else if (DA_SIO) {
1481 bioq_disksort(&softc->bio_queue, bp);
1483 bioq_insert_tail(&softc->bio_queue, bp);
1487 * Schedule ourselves for performing the work.
1490 cam_periph_unlock(periph);
1496 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1498 struct cam_periph *periph;
1499 struct da_softc *softc;
1501 struct ccb_scsiio csio;
1506 periph = dp->d_drv1;
1507 softc = (struct da_softc *)periph->softc;
1508 cam_periph_lock(periph);
1509 secsize = softc->params.secsize;
1511 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1512 cam_periph_unlock(periph);
1517 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1518 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1519 scsi_read_write(&csio,
1523 /*read*/SCSI_RW_WRITE,
1525 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1528 /*data_ptr*/(u_int8_t *) virtual,
1529 /*dxfer_len*/length,
1530 /*sense_len*/SSD_FULL_SIZE,
1531 da_default_timeout * 1000);
1532 xpt_polled_action((union ccb *)&csio);
1534 error = cam_periph_error((union ccb *)&csio,
1535 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1536 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1537 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1538 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1540 printf("Aborting dump due to I/O error.\n");
1541 cam_periph_unlock(periph);
1546 * Sync the disk cache contents to the physical media.
1548 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1550 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1551 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1552 scsi_synchronize_cache(&csio,
1556 /*begin_lba*/0,/* Cover the whole disk */
1560 xpt_polled_action((union ccb *)&csio);
1562 error = cam_periph_error((union ccb *)&csio,
1563 0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL);
1564 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1565 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1566 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1568 xpt_print(periph->path, "Synchronize cache failed\n");
1570 cam_periph_unlock(periph);
1575 dagetattr(struct bio *bp)
1578 struct cam_periph *periph;
1580 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1581 cam_periph_lock(periph);
1582 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1584 cam_periph_unlock(periph);
1586 bp->bio_completed = bp->bio_length;
1596 * Install a global async callback. This callback will
1597 * receive async callbacks like "new device found".
1599 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1601 if (status != CAM_REQ_CMP) {
1602 printf("da: Failed to attach master async callback "
1603 "due to status 0x%x!\n", status);
1604 } else if (da_send_ordered) {
1606 /* Register our shutdown event handler */
1607 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1608 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1609 printf("dainit: shutdown event registration failed!\n");
1614 * Callback from GEOM, called when it has finished cleaning up its
1618 dadiskgonecb(struct disk *dp)
1620 struct cam_periph *periph;
1622 periph = (struct cam_periph *)dp->d_drv1;
1623 cam_periph_release(periph);
1627 daoninvalidate(struct cam_periph *periph)
1629 struct da_softc *softc;
1631 softc = (struct da_softc *)periph->softc;
1634 * De-register any async callbacks.
1636 xpt_register_async(0, daasync, periph, periph->path);
1638 softc->flags |= DA_FLAG_PACK_INVALID;
1641 * Return all queued I/O with ENXIO.
1642 * XXX Handle any transactions queued to the card
1643 * with XPT_ABORT_CCB.
1645 bioq_flush(&softc->bio_queue, NULL, ENXIO);
1646 bioq_flush(&softc->delete_queue, NULL, ENXIO);
1649 * Tell GEOM that we've gone away, we'll get a callback when it is
1650 * done cleaning up its resources.
1652 disk_gone(softc->disk);
1656 dacleanup(struct cam_periph *periph)
1658 struct da_softc *softc;
1660 softc = (struct da_softc *)periph->softc;
1662 cam_periph_unlock(periph);
1665 * If we can't free the sysctl tree, oh well...
1667 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1668 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1669 xpt_print(periph->path, "can't remove sysctl context\n");
1672 callout_drain(&softc->mediapoll_c);
1673 disk_destroy(softc->disk);
1674 callout_drain(&softc->sendordered_c);
1675 free(softc, M_DEVBUF);
1676 cam_periph_lock(periph);
1680 daasync(void *callback_arg, u_int32_t code,
1681 struct cam_path *path, void *arg)
1683 struct cam_periph *periph;
1684 struct da_softc *softc;
1686 periph = (struct cam_periph *)callback_arg;
1688 case AC_FOUND_DEVICE:
1690 struct ccb_getdev *cgd;
1693 cgd = (struct ccb_getdev *)arg;
1697 if (cgd->protocol != PROTO_SCSI)
1699 if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED)
1701 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1702 && SID_TYPE(&cgd->inq_data) != T_RBC
1703 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1707 * Allocate a peripheral instance for
1708 * this device and start the probe
1711 status = cam_periph_alloc(daregister, daoninvalidate,
1713 "da", CAM_PERIPH_BIO,
1715 AC_FOUND_DEVICE, cgd);
1717 if (status != CAM_REQ_CMP
1718 && status != CAM_REQ_INPROG)
1719 printf("daasync: Unable to attach to new device "
1720 "due to status 0x%x\n", status);
1723 case AC_ADVINFO_CHANGED:
1727 buftype = (uintptr_t)arg;
1728 if (buftype == CDAI_TYPE_PHYS_PATH) {
1729 struct da_softc *softc;
1731 softc = periph->softc;
1732 disk_attr_changed(softc->disk, "GEOM::physpath",
1737 case AC_UNIT_ATTENTION:
1740 int error_code, sense_key, asc, ascq;
1742 softc = (struct da_softc *)periph->softc;
1743 ccb = (union ccb *)arg;
1746 * Handle all UNIT ATTENTIONs except our own,
1747 * as they will be handled by daerror().
1749 if (xpt_path_periph(ccb->ccb_h.path) != periph &&
1750 scsi_extract_sense_ccb(ccb,
1751 &error_code, &sense_key, &asc, &ascq)) {
1752 if (asc == 0x2A && ascq == 0x09) {
1753 xpt_print(ccb->ccb_h.path,
1754 "Capacity data has changed\n");
1755 softc->flags &= ~DA_FLAG_PROBED;
1757 } else if (asc == 0x28 && ascq == 0x00) {
1758 softc->flags &= ~DA_FLAG_PROBED;
1759 disk_media_changed(softc->disk, M_NOWAIT);
1760 } else if (asc == 0x3F && ascq == 0x03) {
1761 xpt_print(ccb->ccb_h.path,
1762 "INQUIRY data has changed\n");
1763 softc->flags &= ~DA_FLAG_PROBED;
1767 cam_periph_async(periph, code, path, arg);
1771 softc = (struct da_softc *)periph->softc;
1773 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
1782 struct ccb_hdr *ccbh;
1784 softc = (struct da_softc *)periph->softc;
1786 * Don't fail on the expected unit attention
1789 softc->flags |= DA_FLAG_RETRY_UA;
1790 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1791 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1794 case AC_INQ_CHANGED:
1795 softc = (struct da_softc *)periph->softc;
1796 softc->flags &= ~DA_FLAG_PROBED;
1802 cam_periph_async(periph, code, path, arg);
1806 dasysctlinit(void *context, int pending)
1808 struct cam_periph *periph;
1809 struct da_softc *softc;
1810 char tmpstr[80], tmpstr2[80];
1811 struct ccb_trans_settings cts;
1813 periph = (struct cam_periph *)context;
1815 * periph was held for us when this task was enqueued
1817 if (periph->flags & CAM_PERIPH_INVALID) {
1818 cam_periph_release(periph);
1822 softc = (struct da_softc *)periph->softc;
1823 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1824 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1826 sysctl_ctx_init(&softc->sysctl_ctx);
1827 softc->flags |= DA_FLAG_SCTX_INIT;
1828 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1829 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1830 CTLFLAG_RD, 0, tmpstr);
1831 if (softc->sysctl_tree == NULL) {
1832 printf("dasysctlinit: unable to allocate sysctl tree\n");
1833 cam_periph_release(periph);
1838 * Now register the sysctl handler, so the user can change the value on
1841 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1842 OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RWTUN,
1843 softc, 0, dadeletemethodsysctl, "A",
1844 "BIO_DELETE execution method");
1845 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1846 OID_AUTO, "delete_max", CTLTYPE_U64 | CTLFLAG_RW,
1847 softc, 0, dadeletemaxsysctl, "Q",
1848 "Maximum BIO_DELETE size");
1849 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1850 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1851 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1852 "Minimum CDB size");
1853 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1854 OID_AUTO, "sort_io_queue", CTLFLAG_RW, &softc->sort_io_queue, 0,
1855 "Sort IO queue to try and optimise disk access patterns");
1857 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1858 SYSCTL_CHILDREN(softc->sysctl_tree),
1862 &softc->error_inject,
1864 "error_inject leaf");
1868 * Add some addressing info.
1870 memset(&cts, 0, sizeof (cts));
1871 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1872 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1873 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1874 cam_periph_lock(periph);
1875 xpt_action((union ccb *)&cts);
1876 cam_periph_unlock(periph);
1877 if (cts.ccb_h.status != CAM_REQ_CMP) {
1878 cam_periph_release(periph);
1881 if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
1882 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1883 if (fc->valid & CTS_FC_VALID_WWPN) {
1884 softc->wwpn = fc->wwpn;
1885 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1886 SYSCTL_CHILDREN(softc->sysctl_tree),
1887 OID_AUTO, "wwpn", CTLFLAG_RD,
1888 &softc->wwpn, "World Wide Port Name");
1891 cam_periph_release(periph);
1895 dadeletemaxsysctl(SYSCTL_HANDLER_ARGS)
1899 struct da_softc *softc;
1901 softc = (struct da_softc *)arg1;
1903 value = softc->disk->d_delmaxsize;
1904 error = sysctl_handle_64(oidp, &value, 0, req);
1905 if ((error != 0) || (req->newptr == NULL))
1908 /* only accept values smaller than the calculated value */
1909 if (value > dadeletemaxsize(softc, softc->delete_method)) {
1912 softc->disk->d_delmaxsize = value;
1918 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1922 value = *(int *)arg1;
1924 error = sysctl_handle_int(oidp, &value, 0, req);
1927 || (req->newptr == NULL))
1931 * Acceptable values here are 6, 10, 12 or 16.
1935 else if ((value > 6)
1938 else if ((value > 10)
1941 else if (value > 12)
1944 *(int *)arg1 = value;
1950 dadeletemethodset(struct da_softc *softc, da_delete_methods delete_method)
1953 softc->delete_method = delete_method;
1954 softc->disk->d_delmaxsize = dadeletemaxsize(softc, delete_method);
1955 softc->delete_func = da_delete_functions[delete_method];
1957 if (softc->delete_method > DA_DELETE_DISABLE)
1958 softc->disk->d_flags |= DISKFLAG_CANDELETE;
1960 softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
1964 dadeletemaxsize(struct da_softc *softc, da_delete_methods delete_method)
1968 switch(delete_method) {
1969 case DA_DELETE_UNMAP:
1970 sectors = (off_t)softc->unmap_max_lba;
1972 case DA_DELETE_ATA_TRIM:
1973 sectors = (off_t)ATA_DSM_RANGE_MAX * softc->trim_max_ranges;
1975 case DA_DELETE_WS16:
1976 sectors = omin(softc->ws_max_blks, WS16_MAX_BLKS);
1978 case DA_DELETE_ZERO:
1979 case DA_DELETE_WS10:
1980 sectors = omin(softc->ws_max_blks, WS10_MAX_BLKS);
1986 return (off_t)softc->params.secsize *
1987 omin(sectors, softc->params.sectors);
1991 daprobedone(struct cam_periph *periph, union ccb *ccb)
1993 struct da_softc *softc;
1995 softc = (struct da_softc *)periph->softc;
1997 dadeletemethodchoose(softc, DA_DELETE_NONE);
1999 if (bootverbose && (softc->flags & DA_FLAG_ANNOUNCED) == 0) {
2003 snprintf(buf, sizeof(buf), "Delete methods: <");
2005 for (i = 0; i <= DA_DELETE_MAX; i++) {
2006 if ((softc->delete_available & (1 << i)) == 0 &&
2007 i != softc->delete_method)
2010 strlcat(buf, ",", sizeof(buf));
2011 strlcat(buf, da_delete_method_names[i],
2013 if (i == softc->delete_method)
2014 strlcat(buf, "(*)", sizeof(buf));
2017 strlcat(buf, ">", sizeof(buf));
2018 printf("%s%d: %s\n", periph->periph_name,
2019 periph->unit_number, buf);
2023 * Since our peripheral may be invalidated by an error
2024 * above or an external event, we must release our CCB
2025 * before releasing the probe lock on the peripheral.
2026 * The peripheral will only go away once the last lock
2027 * is removed, and we need it around for the CCB release
2030 xpt_release_ccb(ccb);
2031 softc->state = DA_STATE_NORMAL;
2032 softc->flags |= DA_FLAG_PROBED;
2034 wakeup(&softc->disk->d_mediasize);
2035 if ((softc->flags & DA_FLAG_ANNOUNCED) == 0) {
2036 softc->flags |= DA_FLAG_ANNOUNCED;
2037 cam_periph_unhold(periph);
2039 cam_periph_release_locked(periph);
2043 dadeletemethodchoose(struct da_softc *softc, da_delete_methods default_method)
2047 /* If available, prefer the method requested by user. */
2048 i = softc->delete_method_pref;
2049 methods = softc->delete_available | (1 << DA_DELETE_DISABLE);
2050 if (methods & (1 << i)) {
2051 dadeletemethodset(softc, i);
2055 /* Use the pre-defined order to choose the best performing delete. */
2056 for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
2057 if (i == DA_DELETE_ZERO)
2059 if (softc->delete_available & (1 << i)) {
2060 dadeletemethodset(softc, i);
2065 /* Fallback to default. */
2066 dadeletemethodset(softc, default_method);
2070 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
2074 struct da_softc *softc;
2075 int i, error, methods, value;
2077 softc = (struct da_softc *)arg1;
2079 value = softc->delete_method;
2080 if (value < 0 || value > DA_DELETE_MAX)
2083 p = da_delete_method_names[value];
2084 strncpy(buf, p, sizeof(buf));
2085 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2086 if (error != 0 || req->newptr == NULL)
2088 methods = softc->delete_available | (1 << DA_DELETE_DISABLE);
2089 for (i = 0; i <= DA_DELETE_MAX; i++) {
2090 if (strcmp(buf, da_delete_method_names[i]) == 0)
2093 if (i > DA_DELETE_MAX)
2095 softc->delete_method_pref = i;
2096 dadeletemethodchoose(softc, DA_DELETE_NONE);
2101 daregister(struct cam_periph *periph, void *arg)
2103 struct da_softc *softc;
2104 struct ccb_pathinq cpi;
2105 struct ccb_getdev *cgd;
2109 cgd = (struct ccb_getdev *)arg;
2111 printf("daregister: no getdev CCB, can't register device\n");
2112 return(CAM_REQ_CMP_ERR);
2115 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
2118 if (softc == NULL) {
2119 printf("daregister: Unable to probe new device. "
2120 "Unable to allocate softc\n");
2121 return(CAM_REQ_CMP_ERR);
2124 LIST_INIT(&softc->pending_ccbs);
2125 softc->state = DA_STATE_PROBE_WP;
2126 bioq_init(&softc->bio_queue);
2127 bioq_init(&softc->delete_queue);
2128 bioq_init(&softc->delete_run_queue);
2129 if (SID_IS_REMOVABLE(&cgd->inq_data))
2130 softc->flags |= DA_FLAG_PACK_REMOVABLE;
2131 softc->unmap_max_ranges = UNMAP_MAX_RANGES;
2132 softc->unmap_max_lba = UNMAP_RANGE_MAX;
2133 softc->ws_max_blks = WS16_MAX_BLKS;
2134 softc->trim_max_ranges = ATA_TRIM_MAX_RANGES;
2135 softc->sort_io_queue = -1;
2137 periph->softc = softc;
2140 * See if this device has any quirks.
2142 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
2143 (caddr_t)da_quirk_table,
2144 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
2145 sizeof(*da_quirk_table), scsi_inquiry_match);
2148 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
2150 softc->quirks = DA_Q_NONE;
2152 /* Check if the SIM does not want 6 byte commands */
2153 bzero(&cpi, sizeof(cpi));
2154 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2155 cpi.ccb_h.func_code = XPT_PATH_INQ;
2156 xpt_action((union ccb *)&cpi);
2157 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
2158 softc->quirks |= DA_Q_NO_6_BYTE;
2160 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
2163 * Take an exclusive refcount on the periph while dastart is called
2164 * to finish the probe. The reference will be dropped in dadone at
2167 (void)cam_periph_hold(periph, PRIBIO);
2170 * Schedule a periodic event to occasionally send an
2171 * ordered tag to a device.
2173 callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0);
2174 callout_reset(&softc->sendordered_c,
2175 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2176 dasendorderedtag, softc);
2178 cam_periph_unlock(periph);
2180 * RBC devices don't have to support READ(6), only READ(10).
2182 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
2183 softc->minimum_cmd_size = 10;
2185 softc->minimum_cmd_size = 6;
2188 * Load the user's default, if any.
2190 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
2191 periph->unit_number);
2192 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
2195 * 6, 10, 12 and 16 are the currently permissible values.
2197 if (softc->minimum_cmd_size < 6)
2198 softc->minimum_cmd_size = 6;
2199 else if ((softc->minimum_cmd_size > 6)
2200 && (softc->minimum_cmd_size <= 10))
2201 softc->minimum_cmd_size = 10;
2202 else if ((softc->minimum_cmd_size > 10)
2203 && (softc->minimum_cmd_size <= 12))
2204 softc->minimum_cmd_size = 12;
2205 else if (softc->minimum_cmd_size > 12)
2206 softc->minimum_cmd_size = 16;
2208 /* Predict whether device may support READ CAPACITY(16). */
2209 if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3 &&
2210 (softc->quirks & DA_Q_NO_RC16) == 0) {
2211 softc->flags |= DA_FLAG_CAN_RC16;
2215 * Register this media as a disk.
2217 softc->disk = disk_alloc();
2218 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
2219 periph->unit_number, 0,
2220 DEVSTAT_BS_UNAVAILABLE,
2221 SID_TYPE(&cgd->inq_data) |
2222 XPORT_DEVSTAT_TYPE(cpi.transport),
2223 DEVSTAT_PRIORITY_DISK);
2224 softc->disk->d_open = daopen;
2225 softc->disk->d_close = daclose;
2226 softc->disk->d_strategy = dastrategy;
2227 softc->disk->d_dump = dadump;
2228 softc->disk->d_getattr = dagetattr;
2229 softc->disk->d_gone = dadiskgonecb;
2230 softc->disk->d_name = "da";
2231 softc->disk->d_drv1 = periph;
2233 softc->maxio = DFLTPHYS; /* traditional default */
2234 else if (cpi.maxio > MAXPHYS)
2235 softc->maxio = MAXPHYS; /* for safety */
2237 softc->maxio = cpi.maxio;
2238 softc->disk->d_maxsize = softc->maxio;
2239 softc->disk->d_unit = periph->unit_number;
2240 softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION;
2241 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
2242 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
2243 if ((cpi.hba_misc & PIM_UNMAPPED) != 0)
2244 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
2245 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
2246 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
2247 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
2248 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
2249 cgd->inq_data.product, sizeof(cgd->inq_data.product),
2250 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
2251 softc->disk->d_hba_vendor = cpi.hba_vendor;
2252 softc->disk->d_hba_device = cpi.hba_device;
2253 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
2254 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
2257 * Acquire a reference to the periph before we register with GEOM.
2258 * We'll release this reference once GEOM calls us back (via
2259 * dadiskgonecb()) telling us that our provider has been freed.
2261 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
2262 xpt_print(periph->path, "%s: lost periph during "
2263 "registration!\n", __func__);
2264 cam_periph_lock(periph);
2265 return (CAM_REQ_CMP_ERR);
2268 disk_create(softc->disk, DISK_VERSION);
2269 cam_periph_lock(periph);
2272 * Add async callbacks for events of interest.
2273 * I don't bother checking if this fails as,
2274 * in most cases, the system will function just
2275 * fine without them and the only alternative
2276 * would be to not attach the device on failure.
2278 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
2279 AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION |
2280 AC_INQ_CHANGED, daasync, periph, periph->path);
2283 * Emit an attribute changed notification just in case
2284 * physical path information arrived before our async
2285 * event handler was registered, but after anyone attaching
2286 * to our disk device polled it.
2288 disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
2291 * Schedule a periodic media polling events.
2293 callout_init_mtx(&softc->mediapoll_c, cam_periph_mtx(periph), 0);
2294 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
2295 (cgd->inq_flags & SID_AEN) == 0 &&
2296 da_poll_period != 0)
2297 callout_reset(&softc->mediapoll_c, da_poll_period * hz,
2298 damediapoll, periph);
2300 xpt_schedule(periph, CAM_PRIORITY_DEV);
2302 return(CAM_REQ_CMP);
2306 dastart(struct cam_periph *periph, union ccb *start_ccb)
2308 struct da_softc *softc;
2310 softc = (struct da_softc *)periph->softc;
2312 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
2315 switch (softc->state) {
2316 case DA_STATE_NORMAL:
2321 /* Run BIO_DELETE if not running yet. */
2322 if (!softc->delete_running &&
2323 (bp = bioq_first(&softc->delete_queue)) != NULL) {
2324 if (softc->delete_func != NULL) {
2325 softc->delete_func(periph, start_ccb, bp);
2328 bioq_flush(&softc->delete_queue, NULL, 0);
2333 /* Run regular command. */
2334 bp = bioq_takefirst(&softc->bio_queue);
2338 scsi_test_unit_ready(&start_ccb->csio,
2339 /*retries*/ da_retry_count,
2343 da_default_timeout * 1000);
2344 start_ccb->ccb_h.ccb_bp = NULL;
2345 start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
2346 xpt_action(start_ccb);
2348 xpt_release_ccb(start_ccb);
2353 cam_periph_release_locked(periph);
2356 if ((bp->bio_flags & BIO_ORDERED) != 0 ||
2357 (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
2358 softc->flags &= ~DA_FLAG_NEED_OTAG;
2359 softc->flags |= DA_FLAG_WAS_OTAG;
2360 tag_code = MSG_ORDERED_Q_TAG;
2362 tag_code = MSG_SIMPLE_Q_TAG;
2365 switch (bp->bio_cmd) {
2372 if (bp->bio_cmd == BIO_WRITE) {
2373 softc->flags |= DA_FLAG_DIRTY;
2374 rw_op = SCSI_RW_WRITE;
2376 rw_op = SCSI_RW_READ;
2379 data_ptr = bp->bio_data;
2380 if ((bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0) {
2381 rw_op |= SCSI_RW_BIO;
2385 scsi_read_write(&start_ccb->csio,
2386 /*retries*/da_retry_count,
2388 /*tag_action*/tag_code,
2391 softc->minimum_cmd_size,
2392 /*lba*/bp->bio_pblkno,
2393 /*block_count*/bp->bio_bcount /
2394 softc->params.secsize,
2396 /*dxfer_len*/ bp->bio_bcount,
2397 /*sense_len*/SSD_FULL_SIZE,
2398 da_default_timeout * 1000);
2403 * BIO_FLUSH doesn't currently communicate
2404 * range data, so we synchronize the cache
2405 * over the whole disk. We also force
2406 * ordered tag semantics the flush applies
2407 * to all previously queued I/O.
2409 scsi_synchronize_cache(&start_ccb->csio,
2416 da_default_timeout*1000);
2419 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
2420 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
2423 LIST_INSERT_HEAD(&softc->pending_ccbs,
2424 &start_ccb->ccb_h, periph_links.le);
2426 /* We expect a unit attention from this device */
2427 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
2428 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
2429 softc->flags &= ~DA_FLAG_RETRY_UA;
2432 start_ccb->ccb_h.ccb_bp = bp;
2434 cam_periph_unlock(periph);
2435 xpt_action(start_ccb);
2436 cam_periph_lock(periph);
2439 /* May have more work to do, so ensure we stay scheduled */
2443 case DA_STATE_PROBE_WP:
2449 mode_buf = malloc(mode_buf_len, M_SCSIDA, M_NOWAIT);
2450 if (mode_buf == NULL) {
2451 xpt_print(periph->path, "Unable to send mode sense - "
2452 "malloc failure\n");
2453 softc->state = DA_STATE_PROBE_RC;
2456 scsi_mode_sense_len(&start_ccb->csio,
2457 /*retries*/ da_retry_count,
2459 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2461 /*pc*/ SMS_PAGE_CTRL_CURRENT,
2462 /*page*/ SMS_ALL_PAGES_PAGE,
2463 /*param_buf*/ mode_buf,
2464 /*param_len*/ mode_buf_len,
2465 /*minimum_cmd_size*/ softc->minimum_cmd_size,
2466 /*sense_len*/ SSD_FULL_SIZE,
2467 /*timeout*/ da_default_timeout * 1000);
2468 start_ccb->ccb_h.ccb_bp = NULL;
2469 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_WP;
2470 xpt_action(start_ccb);
2473 case DA_STATE_PROBE_RC:
2475 struct scsi_read_capacity_data *rcap;
2477 rcap = (struct scsi_read_capacity_data *)
2478 malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
2480 printf("dastart: Couldn't malloc read_capacity data\n");
2481 /* da_free_periph??? */
2484 scsi_read_capacity(&start_ccb->csio,
2485 /*retries*/da_retry_count,
2491 start_ccb->ccb_h.ccb_bp = NULL;
2492 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC;
2493 xpt_action(start_ccb);
2496 case DA_STATE_PROBE_RC16:
2498 struct scsi_read_capacity_data_long *rcaplong;
2500 rcaplong = (struct scsi_read_capacity_data_long *)
2501 malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
2502 if (rcaplong == NULL) {
2503 printf("dastart: Couldn't malloc read_capacity data\n");
2504 /* da_free_periph??? */
2507 scsi_read_capacity_16(&start_ccb->csio,
2508 /*retries*/ da_retry_count,
2510 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2514 /*rcap_buf*/ (uint8_t *)rcaplong,
2515 /*rcap_buf_len*/ sizeof(*rcaplong),
2516 /*sense_len*/ SSD_FULL_SIZE,
2517 /*timeout*/ da_default_timeout * 1000);
2518 start_ccb->ccb_h.ccb_bp = NULL;
2519 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC16;
2520 xpt_action(start_ccb);
2523 case DA_STATE_PROBE_LBP:
2525 struct scsi_vpd_logical_block_prov *lbp;
2527 if (!scsi_vpd_supported_page(periph, SVPD_LBP)) {
2529 * If we get here we don't support any SBC-3 delete
2530 * methods with UNMAP as the Logical Block Provisioning
2531 * VPD page support is required for devices which
2532 * support it according to T10/1799-D Revision 31
2533 * however older revisions of the spec don't mandate
2534 * this so we currently don't remove these methods
2535 * from the available set.
2537 softc->state = DA_STATE_PROBE_BLK_LIMITS;
2541 lbp = (struct scsi_vpd_logical_block_prov *)
2542 malloc(sizeof(*lbp), M_SCSIDA, M_NOWAIT|M_ZERO);
2545 printf("dastart: Couldn't malloc lbp data\n");
2546 /* da_free_periph??? */
2550 scsi_inquiry(&start_ccb->csio,
2551 /*retries*/da_retry_count,
2553 /*tag_action*/MSG_SIMPLE_Q_TAG,
2554 /*inq_buf*/(u_int8_t *)lbp,
2555 /*inq_len*/sizeof(*lbp),
2557 /*page_code*/SVPD_LBP,
2558 /*sense_len*/SSD_MIN_SIZE,
2559 /*timeout*/da_default_timeout * 1000);
2560 start_ccb->ccb_h.ccb_bp = NULL;
2561 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_LBP;
2562 xpt_action(start_ccb);
2565 case DA_STATE_PROBE_BLK_LIMITS:
2567 struct scsi_vpd_block_limits *block_limits;
2569 if (!scsi_vpd_supported_page(periph, SVPD_BLOCK_LIMITS)) {
2570 /* Not supported skip to next probe */
2571 softc->state = DA_STATE_PROBE_BDC;
2575 block_limits = (struct scsi_vpd_block_limits *)
2576 malloc(sizeof(*block_limits), M_SCSIDA, M_NOWAIT|M_ZERO);
2578 if (block_limits == NULL) {
2579 printf("dastart: Couldn't malloc block_limits data\n");
2580 /* da_free_periph??? */
2584 scsi_inquiry(&start_ccb->csio,
2585 /*retries*/da_retry_count,
2587 /*tag_action*/MSG_SIMPLE_Q_TAG,
2588 /*inq_buf*/(u_int8_t *)block_limits,
2589 /*inq_len*/sizeof(*block_limits),
2591 /*page_code*/SVPD_BLOCK_LIMITS,
2592 /*sense_len*/SSD_MIN_SIZE,
2593 /*timeout*/da_default_timeout * 1000);
2594 start_ccb->ccb_h.ccb_bp = NULL;
2595 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BLK_LIMITS;
2596 xpt_action(start_ccb);
2599 case DA_STATE_PROBE_BDC:
2601 struct scsi_vpd_block_characteristics *bdc;
2603 if (!scsi_vpd_supported_page(periph, SVPD_BDC)) {
2604 softc->state = DA_STATE_PROBE_ATA;
2608 bdc = (struct scsi_vpd_block_characteristics *)
2609 malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
2612 printf("dastart: Couldn't malloc bdc data\n");
2613 /* da_free_periph??? */
2617 scsi_inquiry(&start_ccb->csio,
2618 /*retries*/da_retry_count,
2620 /*tag_action*/MSG_SIMPLE_Q_TAG,
2621 /*inq_buf*/(u_int8_t *)bdc,
2622 /*inq_len*/sizeof(*bdc),
2624 /*page_code*/SVPD_BDC,
2625 /*sense_len*/SSD_MIN_SIZE,
2626 /*timeout*/da_default_timeout * 1000);
2627 start_ccb->ccb_h.ccb_bp = NULL;
2628 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BDC;
2629 xpt_action(start_ccb);
2632 case DA_STATE_PROBE_ATA:
2634 struct ata_params *ata_params;
2636 if (!scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
2637 daprobedone(periph, start_ccb);
2641 ata_params = (struct ata_params*)
2642 malloc(sizeof(*ata_params), M_SCSIDA, M_NOWAIT|M_ZERO);
2644 if (ata_params == NULL) {
2645 printf("dastart: Couldn't malloc ata_params data\n");
2646 /* da_free_periph??? */
2650 scsi_ata_identify(&start_ccb->csio,
2651 /*retries*/da_retry_count,
2653 /*tag_action*/MSG_SIMPLE_Q_TAG,
2654 /*data_ptr*/(u_int8_t *)ata_params,
2655 /*dxfer_len*/sizeof(*ata_params),
2656 /*sense_len*/SSD_FULL_SIZE,
2657 /*timeout*/da_default_timeout * 1000);
2658 start_ccb->ccb_h.ccb_bp = NULL;
2659 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA;
2660 xpt_action(start_ccb);
2667 * In each of the methods below, while its the caller's
2668 * responsibility to ensure the request will fit into a
2669 * single device request, we might have changed the delete
2670 * method due to the device incorrectly advertising either
2671 * its supported methods or limits.
2673 * To prevent this causing further issues we validate the
2674 * against the methods limits, and warn which would
2675 * otherwise be unnecessary.
2678 da_delete_unmap(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2680 struct da_softc *softc = (struct da_softc *)periph->softc;;
2682 uint8_t *buf = softc->unmap_buf;
2683 uint64_t lba, lastlba = (uint64_t)-1;
2684 uint64_t totalcount = 0;
2686 uint32_t lastcount = 0, c;
2687 uint32_t off, ranges = 0;
2690 * Currently this doesn't take the UNMAP
2691 * Granularity and Granularity Alignment
2692 * fields into account.
2694 * This could result in both unoptimal unmap
2695 * requests as as well as UNMAP calls unmapping
2696 * fewer LBA's than requested.
2699 softc->delete_running = 1;
2700 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2703 bioq_remove(&softc->delete_queue, bp1);
2705 bioq_insert_tail(&softc->delete_run_queue, bp1);
2706 lba = bp1->bio_pblkno;
2707 count = bp1->bio_bcount / softc->params.secsize;
2709 /* Try to extend the previous range. */
2710 if (lba == lastlba) {
2711 c = omin(count, UNMAP_RANGE_MAX - lastcount);
2713 off = ((ranges - 1) * UNMAP_RANGE_SIZE) +
2715 scsi_ulto4b(lastcount, &buf[off + 8]);
2722 c = omin(count, UNMAP_RANGE_MAX);
2723 if (totalcount + c > softc->unmap_max_lba ||
2724 ranges >= softc->unmap_max_ranges) {
2725 xpt_print(periph->path,
2726 "%s issuing short delete %ld > %ld"
2728 da_delete_method_desc[softc->delete_method],
2729 totalcount + c, softc->unmap_max_lba,
2730 ranges, softc->unmap_max_ranges);
2733 off = (ranges * UNMAP_RANGE_SIZE) + UNMAP_HEAD_SIZE;
2734 scsi_u64to8b(lba, &buf[off + 0]);
2735 scsi_ulto4b(c, &buf[off + 8]);
2743 bp1 = bioq_first(&softc->delete_queue);
2744 if (bp1 == NULL || ranges >= softc->unmap_max_ranges ||
2745 totalcount + bp1->bio_bcount /
2746 softc->params.secsize > softc->unmap_max_lba)
2749 scsi_ulto2b(ranges * 16 + 6, &buf[0]);
2750 scsi_ulto2b(ranges * 16, &buf[2]);
2752 scsi_unmap(&ccb->csio,
2753 /*retries*/da_retry_count,
2755 /*tag_action*/MSG_SIMPLE_Q_TAG,
2758 /*dxfer_len*/ ranges * 16 + 8,
2759 /*sense_len*/SSD_FULL_SIZE,
2760 da_default_timeout * 1000);
2761 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2762 ccb->ccb_h.flags |= CAM_UNLOCKED;
2766 da_delete_trim(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2768 struct da_softc *softc = (struct da_softc *)periph->softc;
2770 uint8_t *buf = softc->unmap_buf;
2771 uint64_t lastlba = (uint64_t)-1;
2774 uint32_t lastcount = 0, c, requestcount;
2775 int ranges = 0, off, block_count;
2777 softc->delete_running = 1;
2778 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2781 bioq_remove(&softc->delete_queue, bp1);
2783 bioq_insert_tail(&softc->delete_run_queue, bp1);
2784 lba = bp1->bio_pblkno;
2785 count = bp1->bio_bcount / softc->params.secsize;
2786 requestcount = count;
2788 /* Try to extend the previous range. */
2789 if (lba == lastlba) {
2790 c = omin(count, ATA_DSM_RANGE_MAX - lastcount);
2792 off = (ranges - 1) * 8;
2793 buf[off + 6] = lastcount & 0xff;
2794 buf[off + 7] = (lastcount >> 8) & 0xff;
2800 c = omin(count, ATA_DSM_RANGE_MAX);
2803 buf[off + 0] = lba & 0xff;
2804 buf[off + 1] = (lba >> 8) & 0xff;
2805 buf[off + 2] = (lba >> 16) & 0xff;
2806 buf[off + 3] = (lba >> 24) & 0xff;
2807 buf[off + 4] = (lba >> 32) & 0xff;
2808 buf[off + 5] = (lba >> 40) & 0xff;
2809 buf[off + 6] = c & 0xff;
2810 buf[off + 7] = (c >> 8) & 0xff;
2815 if (count != 0 && ranges == softc->trim_max_ranges) {
2816 xpt_print(periph->path,
2817 "%s issuing short delete %ld > %ld\n",
2818 da_delete_method_desc[softc->delete_method],
2820 (softc->trim_max_ranges - ranges) *
2826 bp1 = bioq_first(&softc->delete_queue);
2827 if (bp1 == NULL || bp1->bio_bcount / softc->params.secsize >
2828 (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX)
2832 block_count = (ranges + ATA_DSM_BLK_RANGES - 1) / ATA_DSM_BLK_RANGES;
2833 scsi_ata_trim(&ccb->csio,
2834 /*retries*/da_retry_count,
2836 /*tag_action*/MSG_SIMPLE_Q_TAG,
2839 /*dxfer_len*/block_count * ATA_DSM_BLK_SIZE,
2840 /*sense_len*/SSD_FULL_SIZE,
2841 da_default_timeout * 1000);
2842 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2843 ccb->ccb_h.flags |= CAM_UNLOCKED;
2847 * We calculate ws_max_blks here based off d_delmaxsize instead
2848 * of using softc->ws_max_blks as it is absolute max for the
2849 * device not the protocol max which may well be lower.
2852 da_delete_ws(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2854 struct da_softc *softc;
2856 uint64_t ws_max_blks;
2858 uint64_t count; /* forward compat with WS32 */
2860 softc = (struct da_softc *)periph->softc;
2861 ws_max_blks = softc->disk->d_delmaxsize / softc->params.secsize;
2862 softc->delete_running = 1;
2863 lba = bp->bio_pblkno;
2867 bioq_remove(&softc->delete_queue, bp1);
2869 bioq_insert_tail(&softc->delete_run_queue, bp1);
2870 count += bp1->bio_bcount / softc->params.secsize;
2871 if (count > ws_max_blks) {
2872 xpt_print(periph->path,
2873 "%s issuing short delete %ld > %ld\n",
2874 da_delete_method_desc[softc->delete_method],
2875 count, ws_max_blks);
2876 count = omin(count, ws_max_blks);
2879 bp1 = bioq_first(&softc->delete_queue);
2880 if (bp1 == NULL || lba + count != bp1->bio_pblkno ||
2881 count + bp1->bio_bcount /
2882 softc->params.secsize > ws_max_blks)
2886 scsi_write_same(&ccb->csio,
2887 /*retries*/da_retry_count,
2889 /*tag_action*/MSG_SIMPLE_Q_TAG,
2890 /*byte2*/softc->delete_method ==
2891 DA_DELETE_ZERO ? 0 : SWS_UNMAP,
2892 softc->delete_method == DA_DELETE_WS16 ? 16 : 10,
2894 /*block_count*/count,
2895 /*data_ptr*/ __DECONST(void *, zero_region),
2896 /*dxfer_len*/ softc->params.secsize,
2897 /*sense_len*/SSD_FULL_SIZE,
2898 da_default_timeout * 1000);
2899 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2900 ccb->ccb_h.flags |= CAM_UNLOCKED;
2904 cmd6workaround(union ccb *ccb)
2906 struct scsi_rw_6 cmd6;
2907 struct scsi_rw_10 *cmd10;
2908 struct da_softc *softc;
2913 cdb = ccb->csio.cdb_io.cdb_bytes;
2914 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
2916 if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
2917 da_delete_methods old_method = softc->delete_method;
2920 * Typically there are two reasons for failure here
2921 * 1. Delete method was detected as supported but isn't
2922 * 2. Delete failed due to invalid params e.g. too big
2924 * While we will attempt to choose an alternative delete method
2925 * this may result in short deletes if the existing delete
2926 * requests from geom are big for the new method chosen.
2928 * This method assumes that the error which triggered this
2929 * will not retry the io otherwise a panic will occur
2931 dadeleteflag(softc, old_method, 0);
2932 dadeletemethodchoose(softc, DA_DELETE_DISABLE);
2933 if (softc->delete_method == DA_DELETE_DISABLE)
2934 xpt_print(ccb->ccb_h.path,
2935 "%s failed, disabling BIO_DELETE\n",
2936 da_delete_method_desc[old_method]);
2938 xpt_print(ccb->ccb_h.path,
2939 "%s failed, switching to %s BIO_DELETE\n",
2940 da_delete_method_desc[old_method],
2941 da_delete_method_desc[softc->delete_method]);
2943 while ((bp = bioq_takefirst(&softc->delete_run_queue)) != NULL)
2944 bioq_disksort(&softc->delete_queue, bp);
2945 bioq_disksort(&softc->delete_queue,
2946 (struct bio *)ccb->ccb_h.ccb_bp);
2947 ccb->ccb_h.ccb_bp = NULL;
2951 /* Detect unsupported PREVENT ALLOW MEDIUM REMOVAL. */
2952 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
2953 (*cdb == PREVENT_ALLOW) &&
2954 (softc->quirks & DA_Q_NO_PREVENT) == 0) {
2956 xpt_print(ccb->ccb_h.path,
2957 "PREVENT ALLOW MEDIUM REMOVAL not supported.\n");
2958 softc->quirks |= DA_Q_NO_PREVENT;
2962 /* Detect unsupported SYNCHRONIZE CACHE(10). */
2963 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
2964 (*cdb == SYNCHRONIZE_CACHE) &&
2965 (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
2967 xpt_print(ccb->ccb_h.path,
2968 "SYNCHRONIZE CACHE(10) not supported.\n");
2969 softc->quirks |= DA_Q_NO_SYNC_CACHE;
2970 softc->disk->d_flags &= ~DISKFLAG_CANFLUSHCACHE;
2974 /* Translation only possible if CDB is an array and cmd is R/W6 */
2975 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
2976 (*cdb != READ_6 && *cdb != WRITE_6))
2979 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
2980 "increasing minimum_cmd_size to 10.\n");
2981 softc->minimum_cmd_size = 10;
2983 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
2984 cmd10 = (struct scsi_rw_10 *)cdb;
2985 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
2987 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
2988 cmd10->reserved = 0;
2989 scsi_ulto2b(cmd6.length, cmd10->length);
2990 cmd10->control = cmd6.control;
2991 ccb->csio.cdb_len = sizeof(*cmd10);
2993 /* Requeue request, unfreezing queue if necessary */
2994 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
2995 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2998 cam_release_devq(ccb->ccb_h.path,
3002 /*getcount_only*/0);
3008 dadone(struct cam_periph *periph, union ccb *done_ccb)
3010 struct da_softc *softc;
3011 struct ccb_scsiio *csio;
3015 softc = (struct da_softc *)periph->softc;
3016 priority = done_ccb->ccb_h.pinfo.priority;
3018 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
3020 csio = &done_ccb->csio;
3021 state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
3023 case DA_CCB_BUFFER_IO:
3026 struct bio *bp, *bp1;
3028 cam_periph_lock(periph);
3029 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
3030 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3034 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
3039 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
3040 if (error == ERESTART) {
3042 * A retry was scheduled, so
3045 cam_periph_unlock(periph);
3048 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
3053 * return all queued I/O with EIO, so that
3054 * the client can retry these I/Os in the
3055 * proper order should it attempt to recover.
3060 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
3062 * Catastrophic error. Mark our pack as
3066 * XXX See if this is really a media
3069 xpt_print(periph->path,
3070 "Invalidating pack\n");
3071 softc->flags |= DA_FLAG_PACK_INVALID;
3072 queued_error = ENXIO;
3074 bioq_flush(&softc->bio_queue, NULL,
3077 bp->bio_error = error;
3078 bp->bio_resid = bp->bio_bcount;
3079 bp->bio_flags |= BIO_ERROR;
3081 } else if (bp != NULL) {
3082 if (state == DA_CCB_DELETE)
3085 bp->bio_resid = csio->resid;
3087 if (bp->bio_resid != 0)
3088 bp->bio_flags |= BIO_ERROR;
3090 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3091 cam_release_devq(done_ccb->ccb_h.path,
3095 /*getcount_only*/0);
3096 } else if (bp != NULL) {
3097 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3098 panic("REQ_CMP with QFRZN");
3099 if (state == DA_CCB_DELETE)
3102 bp->bio_resid = csio->resid;
3103 if (csio->resid > 0)
3104 bp->bio_flags |= BIO_ERROR;
3105 if (softc->error_inject != 0) {
3106 bp->bio_error = softc->error_inject;
3107 bp->bio_resid = bp->bio_bcount;
3108 bp->bio_flags |= BIO_ERROR;
3109 softc->error_inject = 0;
3113 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
3114 if (LIST_EMPTY(&softc->pending_ccbs))
3115 softc->flags |= DA_FLAG_WAS_OTAG;
3117 xpt_release_ccb(done_ccb);
3118 if (state == DA_CCB_DELETE) {
3119 TAILQ_HEAD(, bio) queue;
3122 TAILQ_CONCAT(&queue, &softc->delete_run_queue.queue, bio_queue);
3123 softc->delete_run_queue.insert_point = NULL;
3125 * Normally, the xpt_release_ccb() above would make sure
3126 * that when we have more work to do, that work would
3127 * get kicked off. However, we specifically keep
3128 * delete_running set to 0 before the call above to
3129 * allow other I/O to progress when many BIO_DELETE
3130 * requests are pushed down. We set delete_running to 0
3131 * and call daschedule again so that we don't stall if
3132 * there are no other I/Os pending apart from BIO_DELETEs.
3134 softc->delete_running = 0;
3136 cam_periph_unlock(periph);
3137 while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
3138 TAILQ_REMOVE(&queue, bp1, bio_queue);
3139 bp1->bio_error = bp->bio_error;
3140 if (bp->bio_flags & BIO_ERROR) {
3141 bp1->bio_flags |= BIO_ERROR;
3142 bp1->bio_resid = bp1->bio_bcount;
3148 cam_periph_unlock(periph);
3153 case DA_CCB_PROBE_WP:
3155 struct scsi_mode_header_6 *mode_hdr6;
3156 struct scsi_mode_header_10 *mode_hdr10;
3159 if (softc->minimum_cmd_size > 6) {
3160 mode_hdr10 = (struct scsi_mode_header_10 *)csio->data_ptr;
3161 dev_spec = mode_hdr10->dev_spec;
3163 mode_hdr6 = (struct scsi_mode_header_6 *)csio->data_ptr;
3164 dev_spec = mode_hdr6->dev_spec;
3166 if (cam_ccb_status(done_ccb) == CAM_REQ_CMP) {
3167 if ((dev_spec & 0x80) != 0)
3168 softc->disk->d_flags |= DISKFLAG_WRITE_PROTECT;
3170 softc->disk->d_flags &= ~DISKFLAG_WRITE_PROTECT;
3174 error = daerror(done_ccb, CAM_RETRY_SELTO,
3175 SF_RETRY_UA|SF_NO_PRINT);
3176 if (error == ERESTART)
3178 else if (error != 0) {
3179 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3180 /* Don't wedge this device's queue */
3181 cam_release_devq(done_ccb->ccb_h.path,
3185 /*getcount_only*/0);
3190 free(csio->data_ptr, M_SCSIDA);
3191 xpt_release_ccb(done_ccb);
3192 if ((softc->flags & DA_FLAG_CAN_RC16) != 0)
3193 softc->state = DA_STATE_PROBE_RC16;
3195 softc->state = DA_STATE_PROBE_RC;
3196 xpt_schedule(periph, priority);
3199 case DA_CCB_PROBE_RC:
3200 case DA_CCB_PROBE_RC16:
3202 struct scsi_read_capacity_data *rdcap;
3203 struct scsi_read_capacity_data_long *rcaplong;
3204 char announce_buf[80];
3210 if (state == DA_CCB_PROBE_RC)
3211 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
3213 rcaplong = (struct scsi_read_capacity_data_long *)
3216 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3217 struct disk_params *dp;
3218 uint32_t block_size;
3220 u_int lbppbe; /* LB per physical block exponent. */
3221 u_int lalba; /* Lowest aligned LBA. */
3223 if (state == DA_CCB_PROBE_RC) {
3224 block_size = scsi_4btoul(rdcap->length);
3225 maxsector = scsi_4btoul(rdcap->addr);
3230 * According to SBC-2, if the standard 10
3231 * byte READ CAPACITY command returns 2^32,
3232 * we should issue the 16 byte version of
3233 * the command, since the device in question
3234 * has more sectors than can be represented
3235 * with the short version of the command.
3237 if (maxsector == 0xffffffff) {
3238 free(rdcap, M_SCSIDA);
3239 xpt_release_ccb(done_ccb);
3240 softc->state = DA_STATE_PROBE_RC16;
3241 xpt_schedule(periph, priority);
3245 block_size = scsi_4btoul(rcaplong->length);
3246 maxsector = scsi_8btou64(rcaplong->addr);
3247 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
3248 lalba = scsi_2btoul(rcaplong->lalba_lbp);
3252 * Because GEOM code just will panic us if we
3253 * give them an 'illegal' value we'll avoid that
3256 if (block_size == 0) {
3261 if (block_size >= MAXPHYS) {
3262 xpt_print(periph->path,
3263 "unsupportable block size %ju\n",
3264 (uintmax_t) block_size);
3265 announce_buf[0] = '\0';
3266 cam_periph_invalidate(periph);
3269 * We pass rcaplong into dasetgeom(),
3270 * because it will only use it if it is
3273 dasetgeom(periph, block_size, maxsector,
3274 rcaplong, sizeof(*rcaplong));
3275 lbp = (lalba & SRC16_LBPME_A);
3276 dp = &softc->params;
3277 snprintf(announce_buf, sizeof(announce_buf),
3278 "%juMB (%ju %u byte sectors)",
3279 ((uintmax_t)dp->secsize * dp->sectors) /
3281 (uintmax_t)dp->sectors, dp->secsize);
3286 announce_buf[0] = '\0';
3289 * Retry any UNIT ATTENTION type errors. They
3290 * are expected at boot.
3292 error = daerror(done_ccb, CAM_RETRY_SELTO,
3293 SF_RETRY_UA|SF_NO_PRINT);
3294 if (error == ERESTART) {
3296 * A retry was scheuled, so
3300 } else if (error != 0) {
3302 int sense_key, error_code;
3305 struct ccb_getdev cgd;
3307 /* Don't wedge this device's queue */
3308 status = done_ccb->ccb_h.status;
3309 if ((status & CAM_DEV_QFRZN) != 0)
3310 cam_release_devq(done_ccb->ccb_h.path,
3314 /*getcount_only*/0);
3317 xpt_setup_ccb(&cgd.ccb_h,
3318 done_ccb->ccb_h.path,
3319 CAM_PRIORITY_NORMAL);
3320 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
3321 xpt_action((union ccb *)&cgd);
3323 if (scsi_extract_sense_ccb(done_ccb,
3324 &error_code, &sense_key, &asc, &ascq))
3330 * If we tried READ CAPACITY(16) and failed,
3331 * fallback to READ CAPACITY(10).
3333 if ((state == DA_CCB_PROBE_RC16) &&
3334 (softc->flags & DA_FLAG_CAN_RC16) &&
3335 (((csio->ccb_h.status & CAM_STATUS_MASK) ==
3338 (error_code == SSD_CURRENT_ERROR) &&
3339 (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
3340 softc->flags &= ~DA_FLAG_CAN_RC16;
3341 free(rdcap, M_SCSIDA);
3342 xpt_release_ccb(done_ccb);
3343 softc->state = DA_STATE_PROBE_RC;
3344 xpt_schedule(periph, priority);
3348 * Attach to anything that claims to be a
3349 * direct access or optical disk device,
3350 * as long as it doesn't return a "Logical
3351 * unit not supported" (0x25) error.
3353 if ((have_sense) && (asc != 0x25)
3354 && (error_code == SSD_CURRENT_ERROR)) {
3355 const char *sense_key_desc;
3356 const char *asc_desc;
3358 dasetgeom(periph, 512, -1, NULL, 0);
3359 scsi_sense_desc(sense_key, asc, ascq,
3363 snprintf(announce_buf,
3364 sizeof(announce_buf),
3365 "Attempt to query device "
3366 "size failed: %s, %s",
3374 xpt_print(periph->path,
3375 "got CAM status %#x\n",
3376 done_ccb->ccb_h.status);
3379 xpt_print(periph->path, "fatal error, "
3380 "failed to attach to device\n");
3383 * Free up resources.
3385 cam_periph_invalidate(periph);
3389 free(csio->data_ptr, M_SCSIDA);
3390 if (announce_buf[0] != '\0' &&
3391 ((softc->flags & DA_FLAG_ANNOUNCED) == 0)) {
3393 * Create our sysctl variables, now that we know
3394 * we have successfully attached.
3396 /* increase the refcount */
3397 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3398 taskqueue_enqueue(taskqueue_thread,
3399 &softc->sysctl_task);
3400 xpt_announce_periph(periph, announce_buf);
3401 xpt_announce_quirks(periph, softc->quirks,
3404 xpt_print(periph->path, "fatal error, "
3405 "could not acquire reference count\n");
3409 /* We already probed the device. */
3410 if (softc->flags & DA_FLAG_PROBED) {
3411 daprobedone(periph, done_ccb);
3415 /* Ensure re-probe doesn't see old delete. */
3416 softc->delete_available = 0;
3417 dadeleteflag(softc, DA_DELETE_ZERO, 1);
3418 if (lbp && (softc->quirks & DA_Q_NO_UNMAP) == 0) {
3420 * Based on older SBC-3 spec revisions
3421 * any of the UNMAP methods "may" be
3422 * available via LBP given this flag so
3423 * we flag all of them as available and
3424 * then remove those which further
3425 * probes confirm aren't available
3428 * We could also check readcap(16) p_type
3429 * flag to exclude one or more invalid
3430 * write same (X) types here
3432 dadeleteflag(softc, DA_DELETE_WS16, 1);
3433 dadeleteflag(softc, DA_DELETE_WS10, 1);
3434 dadeleteflag(softc, DA_DELETE_UNMAP, 1);
3436 xpt_release_ccb(done_ccb);
3437 softc->state = DA_STATE_PROBE_LBP;
3438 xpt_schedule(periph, priority);
3442 xpt_release_ccb(done_ccb);
3443 softc->state = DA_STATE_PROBE_BDC;
3444 xpt_schedule(periph, priority);
3447 case DA_CCB_PROBE_LBP:
3449 struct scsi_vpd_logical_block_prov *lbp;
3451 lbp = (struct scsi_vpd_logical_block_prov *)csio->data_ptr;
3453 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3455 * T10/1799-D Revision 31 states at least one of these
3456 * must be supported but we don't currently enforce this.
3458 dadeleteflag(softc, DA_DELETE_WS16,
3459 (lbp->flags & SVPD_LBP_WS16));
3460 dadeleteflag(softc, DA_DELETE_WS10,
3461 (lbp->flags & SVPD_LBP_WS10));
3462 dadeleteflag(softc, DA_DELETE_UNMAP,
3463 (lbp->flags & SVPD_LBP_UNMAP));
3466 error = daerror(done_ccb, CAM_RETRY_SELTO,
3467 SF_RETRY_UA|SF_NO_PRINT);
3468 if (error == ERESTART)
3470 else if (error != 0) {
3471 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3472 /* Don't wedge this device's queue */
3473 cam_release_devq(done_ccb->ccb_h.path,
3477 /*getcount_only*/0);
3481 * Failure indicates we don't support any SBC-3
3482 * delete methods with UNMAP
3487 free(lbp, M_SCSIDA);
3488 xpt_release_ccb(done_ccb);
3489 softc->state = DA_STATE_PROBE_BLK_LIMITS;
3490 xpt_schedule(periph, priority);
3493 case DA_CCB_PROBE_BLK_LIMITS:
3495 struct scsi_vpd_block_limits *block_limits;
3497 block_limits = (struct scsi_vpd_block_limits *)csio->data_ptr;
3499 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3500 uint32_t max_txfer_len = scsi_4btoul(
3501 block_limits->max_txfer_len);
3502 uint32_t max_unmap_lba_cnt = scsi_4btoul(
3503 block_limits->max_unmap_lba_cnt);
3504 uint32_t max_unmap_blk_cnt = scsi_4btoul(
3505 block_limits->max_unmap_blk_cnt);
3506 uint64_t ws_max_blks = scsi_8btou64(
3507 block_limits->max_write_same_length);
3509 if (max_txfer_len != 0) {
3510 softc->disk->d_maxsize = MIN(softc->maxio,
3511 (off_t)max_txfer_len * softc->params.secsize);
3515 * We should already support UNMAP but we check lba
3516 * and block count to be sure
3518 if (max_unmap_lba_cnt != 0x00L &&
3519 max_unmap_blk_cnt != 0x00L) {
3520 softc->unmap_max_lba = max_unmap_lba_cnt;
3521 softc->unmap_max_ranges = min(max_unmap_blk_cnt,
3525 * Unexpected UNMAP limits which means the
3526 * device doesn't actually support UNMAP
3528 dadeleteflag(softc, DA_DELETE_UNMAP, 0);
3531 if (ws_max_blks != 0x00L)
3532 softc->ws_max_blks = ws_max_blks;
3535 error = daerror(done_ccb, CAM_RETRY_SELTO,
3536 SF_RETRY_UA|SF_NO_PRINT);
3537 if (error == ERESTART)
3539 else if (error != 0) {
3540 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3541 /* Don't wedge this device's queue */
3542 cam_release_devq(done_ccb->ccb_h.path,
3546 /*getcount_only*/0);
3550 * Failure here doesn't mean UNMAP is not
3551 * supported as this is an optional page.
3553 softc->unmap_max_lba = 1;
3554 softc->unmap_max_ranges = 1;
3558 free(block_limits, M_SCSIDA);
3559 xpt_release_ccb(done_ccb);
3560 softc->state = DA_STATE_PROBE_BDC;
3561 xpt_schedule(periph, priority);
3564 case DA_CCB_PROBE_BDC:
3566 struct scsi_vpd_block_characteristics *bdc;
3568 bdc = (struct scsi_vpd_block_characteristics *)csio->data_ptr;
3570 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3572 * Disable queue sorting for non-rotational media
3575 u_int16_t old_rate = softc->disk->d_rotation_rate;
3577 softc->disk->d_rotation_rate =
3578 scsi_2btoul(bdc->medium_rotation_rate);
3579 if (softc->disk->d_rotation_rate ==
3580 SVPD_BDC_RATE_NON_ROTATING) {
3581 softc->sort_io_queue = 0;
3583 if (softc->disk->d_rotation_rate != old_rate) {
3584 disk_attr_changed(softc->disk,
3585 "GEOM::rotation_rate", M_NOWAIT);
3589 error = daerror(done_ccb, CAM_RETRY_SELTO,
3590 SF_RETRY_UA|SF_NO_PRINT);
3591 if (error == ERESTART)
3593 else if (error != 0) {
3594 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3595 /* Don't wedge this device's queue */
3596 cam_release_devq(done_ccb->ccb_h.path,
3600 /*getcount_only*/0);
3605 free(bdc, M_SCSIDA);
3606 xpt_release_ccb(done_ccb);
3607 softc->state = DA_STATE_PROBE_ATA;
3608 xpt_schedule(periph, priority);
3611 case DA_CCB_PROBE_ATA:
3614 struct ata_params *ata_params;
3617 ata_params = (struct ata_params *)csio->data_ptr;
3618 ptr = (uint16_t *)ata_params;
3620 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3623 for (i = 0; i < sizeof(*ata_params) / 2; i++)
3624 ptr[i] = le16toh(ptr[i]);
3625 if (ata_params->support_dsm & ATA_SUPPORT_DSM_TRIM &&
3626 (softc->quirks & DA_Q_NO_UNMAP) == 0) {
3627 dadeleteflag(softc, DA_DELETE_ATA_TRIM, 1);
3628 if (ata_params->max_dsm_blocks != 0)
3629 softc->trim_max_ranges = min(
3630 softc->trim_max_ranges,
3631 ata_params->max_dsm_blocks *
3632 ATA_DSM_BLK_RANGES);
3635 * Disable queue sorting for non-rotational media
3638 old_rate = softc->disk->d_rotation_rate;
3639 softc->disk->d_rotation_rate =
3640 ata_params->media_rotation_rate;
3641 if (softc->disk->d_rotation_rate ==
3642 ATA_RATE_NON_ROTATING) {
3643 softc->sort_io_queue = 0;
3646 if (softc->disk->d_rotation_rate != old_rate) {
3647 disk_attr_changed(softc->disk,
3648 "GEOM::rotation_rate", M_NOWAIT);
3652 error = daerror(done_ccb, CAM_RETRY_SELTO,
3653 SF_RETRY_UA|SF_NO_PRINT);
3654 if (error == ERESTART)
3656 else if (error != 0) {
3657 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3658 /* Don't wedge this device's queue */
3659 cam_release_devq(done_ccb->ccb_h.path,
3663 /*getcount_only*/0);
3668 free(ata_params, M_SCSIDA);
3669 daprobedone(periph, done_ccb);
3673 /* No-op. We're polling */
3677 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3679 if (daerror(done_ccb, CAM_RETRY_SELTO,
3680 SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
3683 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3684 cam_release_devq(done_ccb->ccb_h.path,
3688 /*getcount_only*/0);
3690 xpt_release_ccb(done_ccb);
3691 cam_periph_release_locked(periph);
3697 xpt_release_ccb(done_ccb);
3701 dareprobe(struct cam_periph *periph)
3703 struct da_softc *softc;
3706 softc = (struct da_softc *)periph->softc;
3708 /* Probe in progress; don't interfere. */
3709 if (softc->state != DA_STATE_NORMAL)
3712 status = cam_periph_acquire(periph);
3713 KASSERT(status == CAM_REQ_CMP,
3714 ("dareprobe: cam_periph_acquire failed"));
3716 softc->state = DA_STATE_PROBE_WP;
3717 xpt_schedule(periph, CAM_PRIORITY_DEV);
3721 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3723 struct da_softc *softc;
3724 struct cam_periph *periph;
3725 int error, error_code, sense_key, asc, ascq;
3727 periph = xpt_path_periph(ccb->ccb_h.path);
3728 softc = (struct da_softc *)periph->softc;
3731 * Automatically detect devices that do not support
3732 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
3735 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3736 error = cmd6workaround(ccb);
3737 } else if (scsi_extract_sense_ccb(ccb,
3738 &error_code, &sense_key, &asc, &ascq)) {
3739 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3740 error = cmd6workaround(ccb);
3742 * If the target replied with CAPACITY DATA HAS CHANGED UA,
3743 * query the capacity and notify upper layers.
3745 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3746 asc == 0x2A && ascq == 0x09) {
3747 xpt_print(periph->path, "Capacity data has changed\n");
3748 softc->flags &= ~DA_FLAG_PROBED;
3750 sense_flags |= SF_NO_PRINT;
3751 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3752 asc == 0x28 && ascq == 0x00) {
3753 softc->flags &= ~DA_FLAG_PROBED;
3754 disk_media_changed(softc->disk, M_NOWAIT);
3755 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3756 asc == 0x3F && ascq == 0x03) {
3757 xpt_print(periph->path, "INQUIRY data has changed\n");
3758 softc->flags &= ~DA_FLAG_PROBED;
3760 sense_flags |= SF_NO_PRINT;
3761 } else if (sense_key == SSD_KEY_NOT_READY &&
3762 asc == 0x3a && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
3763 softc->flags |= DA_FLAG_PACK_INVALID;
3764 disk_media_gone(softc->disk, M_NOWAIT);
3767 if (error == ERESTART)
3772 * Until we have a better way of doing pack validation,
3773 * don't treat UAs as errors.
3775 sense_flags |= SF_RETRY_UA;
3777 if (softc->quirks & DA_Q_RETRY_BUSY)
3778 sense_flags |= SF_RETRY_BUSY;
3779 return(cam_periph_error(ccb, cam_flags, sense_flags,
3780 &softc->saved_ccb));
3784 damediapoll(void *arg)
3786 struct cam_periph *periph = arg;
3787 struct da_softc *softc = periph->softc;
3789 if (!softc->tur && LIST_EMPTY(&softc->pending_ccbs)) {
3790 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3795 /* Queue us up again */
3796 if (da_poll_period != 0)
3797 callout_schedule(&softc->mediapoll_c, da_poll_period * hz);
3801 daprevent(struct cam_periph *periph, int action)
3803 struct da_softc *softc;
3807 softc = (struct da_softc *)periph->softc;
3809 if (((action == PR_ALLOW)
3810 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
3811 || ((action == PR_PREVENT)
3812 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
3816 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3818 scsi_prevent(&ccb->csio,
3826 error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
3827 SF_RETRY_UA | SF_NO_PRINT, softc->disk->d_devstat);
3830 if (action == PR_ALLOW)
3831 softc->flags &= ~DA_FLAG_PACK_LOCKED;
3833 softc->flags |= DA_FLAG_PACK_LOCKED;
3836 xpt_release_ccb(ccb);
3840 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
3841 struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
3843 struct ccb_calc_geometry ccg;
3844 struct da_softc *softc;
3845 struct disk_params *dp;
3846 u_int lbppbe, lalba;
3849 softc = (struct da_softc *)periph->softc;
3851 dp = &softc->params;
3852 dp->secsize = block_len;
3853 dp->sectors = maxsector + 1;
3854 if (rcaplong != NULL) {
3855 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
3856 lalba = scsi_2btoul(rcaplong->lalba_lbp);
3857 lalba &= SRC16_LALBA_A;
3864 dp->stripesize = block_len << lbppbe;
3865 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
3867 } else if (softc->quirks & DA_Q_4K) {
3868 dp->stripesize = 4096;
3869 dp->stripeoffset = 0;
3872 dp->stripeoffset = 0;
3875 * Have the controller provide us with a geometry
3876 * for this disk. The only time the geometry
3877 * matters is when we boot and the controller
3878 * is the only one knowledgeable enough to come
3879 * up with something that will make this a bootable
3882 xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3883 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
3884 ccg.block_size = dp->secsize;
3885 ccg.volume_size = dp->sectors;
3887 ccg.secs_per_track = 0;
3889 xpt_action((union ccb*)&ccg);
3890 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3892 * We don't know what went wrong here- but just pick
3893 * a geometry so we don't have nasty things like divide
3897 dp->secs_per_track = 255;
3898 dp->cylinders = dp->sectors / (255 * 255);
3899 if (dp->cylinders == 0) {
3903 dp->heads = ccg.heads;
3904 dp->secs_per_track = ccg.secs_per_track;
3905 dp->cylinders = ccg.cylinders;
3909 * If the user supplied a read capacity buffer, and if it is
3910 * different than the previous buffer, update the data in the EDT.
3911 * If it's the same, we don't bother. This avoids sending an
3912 * update every time someone opens this device.
3914 if ((rcaplong != NULL)
3915 && (bcmp(rcaplong, &softc->rcaplong,
3916 min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
3917 struct ccb_dev_advinfo cdai;
3919 xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3920 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
3921 cdai.buftype = CDAI_TYPE_RCAPLONG;
3922 cdai.flags = CDAI_FLAG_STORE;
3923 cdai.bufsiz = rcap_len;
3924 cdai.buf = (uint8_t *)rcaplong;
3925 xpt_action((union ccb *)&cdai);
3926 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
3927 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
3928 if (cdai.ccb_h.status != CAM_REQ_CMP) {
3929 xpt_print(periph->path, "%s: failed to set read "
3930 "capacity advinfo\n", __func__);
3931 /* Use cam_error_print() to decode the status */
3932 cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
3935 bcopy(rcaplong, &softc->rcaplong,
3936 min(sizeof(softc->rcaplong), rcap_len));
3940 softc->disk->d_sectorsize = softc->params.secsize;
3941 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
3942 softc->disk->d_stripesize = softc->params.stripesize;
3943 softc->disk->d_stripeoffset = softc->params.stripeoffset;
3944 /* XXX: these are not actually "firmware" values, so they may be wrong */
3945 softc->disk->d_fwsectors = softc->params.secs_per_track;
3946 softc->disk->d_fwheads = softc->params.heads;
3947 softc->disk->d_devstat->block_size = softc->params.secsize;
3948 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
3950 error = disk_resize(softc->disk, M_NOWAIT);
3952 xpt_print(periph->path, "disk_resize(9) failed, error = %d\n", error);
3956 dasendorderedtag(void *arg)
3958 struct da_softc *softc = arg;
3960 if (da_send_ordered) {
3961 if (!LIST_EMPTY(&softc->pending_ccbs)) {
3962 if ((softc->flags & DA_FLAG_WAS_OTAG) == 0)
3963 softc->flags |= DA_FLAG_NEED_OTAG;
3964 softc->flags &= ~DA_FLAG_WAS_OTAG;
3967 /* Queue us up again */
3968 callout_reset(&softc->sendordered_c,
3969 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
3970 dasendorderedtag, softc);
3974 * Step through all DA peripheral drivers, and if the device is still open,
3975 * sync the disk cache to physical media.
3978 dashutdown(void * arg, int howto)
3980 struct cam_periph *periph;
3981 struct da_softc *softc;
3985 CAM_PERIPH_FOREACH(periph, &dadriver) {
3986 softc = (struct da_softc *)periph->softc;
3987 if (SCHEDULER_STOPPED()) {
3988 /* If we paniced with the lock held, do not recurse. */
3989 if (!cam_periph_owned(periph) &&
3990 (softc->flags & DA_FLAG_OPEN)) {
3991 dadump(softc->disk, NULL, 0, 0, 0);
3995 cam_periph_lock(periph);
3998 * We only sync the cache if the drive is still open, and
3999 * if the drive is capable of it..
4001 if (((softc->flags & DA_FLAG_OPEN) == 0)
4002 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
4003 cam_periph_unlock(periph);
4007 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
4008 scsi_synchronize_cache(&ccb->csio,
4012 /*begin_lba*/0, /* whole disk */
4017 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
4018 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR,
4019 softc->disk->d_devstat);
4021 xpt_print(periph->path, "Synchronize cache failed\n");
4022 xpt_release_ccb(ccb);
4023 cam_periph_unlock(periph);
4027 #else /* !_KERNEL */
4030 * XXX These are only left out of the kernel build to silence warnings. If,
4031 * for some reason these functions are used in the kernel, the ifdefs should
4032 * be moved so they are included both in the kernel and userland.
4035 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
4036 void (*cbfcnp)(struct cam_periph *, union ccb *),
4037 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
4038 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4041 struct scsi_format_unit *scsi_cmd;
4043 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
4044 scsi_cmd->opcode = FORMAT_UNIT;
4045 scsi_cmd->byte2 = byte2;
4046 scsi_ulto2b(ileave, scsi_cmd->interleave);
4051 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
4061 scsi_read_defects(struct ccb_scsiio *csio, uint32_t retries,
4062 void (*cbfcnp)(struct cam_periph *, union ccb *),
4063 uint8_t tag_action, uint8_t list_format,
4064 uint32_t addr_desc_index, uint8_t *data_ptr,
4065 uint32_t dxfer_len, int minimum_cmd_size,
4066 uint8_t sense_len, uint32_t timeout)
4071 * These conditions allow using the 10 byte command. Otherwise we
4072 * need to use the 12 byte command.
4074 if ((minimum_cmd_size <= 10)
4075 && (addr_desc_index == 0)
4076 && (dxfer_len <= SRDD10_MAX_LENGTH)) {
4077 struct scsi_read_defect_data_10 *cdb10;
4079 cdb10 = (struct scsi_read_defect_data_10 *)
4080 &csio->cdb_io.cdb_bytes;
4082 cdb_len = sizeof(*cdb10);
4083 bzero(cdb10, cdb_len);
4084 cdb10->opcode = READ_DEFECT_DATA_10;
4085 cdb10->format = list_format;
4086 scsi_ulto2b(dxfer_len, cdb10->alloc_length);
4088 struct scsi_read_defect_data_12 *cdb12;
4090 cdb12 = (struct scsi_read_defect_data_12 *)
4091 &csio->cdb_io.cdb_bytes;
4093 cdb_len = sizeof(*cdb12);
4094 bzero(cdb12, cdb_len);
4095 cdb12->opcode = READ_DEFECT_DATA_12;
4096 cdb12->format = list_format;
4097 scsi_ulto4b(dxfer_len, cdb12->alloc_length);
4098 scsi_ulto4b(addr_desc_index, cdb12->address_descriptor_index);
4104 /*flags*/ CAM_DIR_IN,
4114 scsi_sanitize(struct ccb_scsiio *csio, u_int32_t retries,
4115 void (*cbfcnp)(struct cam_periph *, union ccb *),
4116 u_int8_t tag_action, u_int8_t byte2, u_int16_t control,
4117 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4120 struct scsi_sanitize *scsi_cmd;
4122 scsi_cmd = (struct scsi_sanitize *)&csio->cdb_io.cdb_bytes;
4123 scsi_cmd->opcode = SANITIZE;
4124 scsi_cmd->byte2 = byte2;
4125 scsi_cmd->control = control;
4126 scsi_ulto2b(dxfer_len, scsi_cmd->length);
4131 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
4140 #endif /* _KERNEL */