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 FE-210 camera
946 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "FE210*",
947 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
953 {T_DIRECT, SIP_MEDIA_REMOVABLE, "LG", "UP3S",
954 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
958 * Laser MP3-2GA13 MP3 player
960 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "(HS) Flash Disk",
961 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
965 * LaCie external 250GB Hard drive des by Porsche
966 * Submitted by: Ben Stuyts <ben@altesco.nl>
969 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HM250JI", "*"},
970 /*quirks*/ DA_Q_NO_SYNC_CACHE
975 * Corsair Force 2 SSDs
976 * 4k optimised & trim only works in 4k requests + 4k aligned
978 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair CSSD-F*", "*" },
983 * Corsair Force 3 SSDs
984 * 4k optimised & trim only works in 4k requests + 4k aligned
986 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force 3*", "*" },
991 * Corsair Neutron GTX SSDs
992 * 4k optimised & trim only works in 4k requests + 4k aligned
994 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Neutron GTX*", "*" },
999 * Corsair Force GT & GS SSDs
1000 * 4k optimised & trim only works in 4k requests + 4k aligned
1002 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force G*", "*" },
1008 * 4k optimised & trim only works in 4k requests + 4k aligned
1010 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "M4-CT???M4SSD2*", "*" },
1015 * Crucial RealSSD C300 SSDs
1018 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "C300-CTFDDAC???MAG*",
1019 "*" }, /*quirks*/DA_Q_4K
1023 * Intel 320 Series SSDs
1024 * 4k optimised & trim only works in 4k requests + 4k aligned
1026 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2CW*", "*" },
1031 * Intel 330 Series SSDs
1032 * 4k optimised & trim only works in 4k requests + 4k aligned
1034 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2CT*", "*" },
1039 * Intel 510 Series SSDs
1040 * 4k optimised & trim only works in 4k requests + 4k aligned
1042 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2MH*", "*" },
1047 * Intel 520 Series SSDs
1048 * 4k optimised & trim only works in 4k requests + 4k aligned
1050 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2BW*", "*" },
1055 * Intel X25-M Series SSDs
1056 * 4k optimised & trim only works in 4k requests + 4k aligned
1058 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2M*", "*" },
1063 * Kingston E100 Series SSDs
1064 * 4k optimised & trim only works in 4k requests + 4k aligned
1066 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SE100S3*", "*" },
1071 * Kingston HyperX 3k SSDs
1072 * 4k optimised & trim only works in 4k requests + 4k aligned
1074 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SH103S3*", "*" },
1079 * Marvell SSDs (entry taken from OpenSolaris)
1080 * 4k optimised & trim only works in 4k requests + 4k aligned
1082 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "MARVELL SD88SA02*", "*" },
1087 * OCZ Agility 2 SSDs
1088 * 4k optimised & trim only works in 4k requests + 4k aligned
1090 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY2*", "*" },
1095 * OCZ Agility 3 SSDs
1096 * 4k optimised & trim only works in 4k requests + 4k aligned
1098 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-AGILITY3*", "*" },
1103 * OCZ Deneva R Series SSDs
1104 * 4k optimised & trim only works in 4k requests + 4k aligned
1106 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "DENRSTE251M45*", "*" },
1111 * OCZ Vertex 2 SSDs (inc pro series)
1112 * 4k optimised & trim only works in 4k requests + 4k aligned
1114 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ?VERTEX2*", "*" },
1120 * 4k optimised & trim only works in 4k requests + 4k aligned
1122 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX3*", "*" },
1128 * 4k optimised & trim only works in 4k requests + 4k aligned
1130 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX4*", "*" },
1135 * Samsung 830 Series SSDs
1136 * 4k optimised & trim only works in 4k requests + 4k aligned
1138 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG SSD 830 Series*", "*" },
1144 * 4k optimised & trim only works in 4k requests + 4k aligned
1146 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 840*", "*" },
1152 * 4k optimised & trim only works in 4k requests + 4k aligned
1154 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 850*", "*" },
1159 * Samsung 843T Series SSDs (MZ7WD*)
1160 * Samsung PM851 Series SSDs (MZ7TE*)
1161 * Samsung PM853T Series SSDs (MZ7GE*)
1162 * Samsung SM863 Series SSDs (MZ7KM*)
1165 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG MZ7*", "*" },
1170 * SuperTalent TeraDrive CT SSDs
1171 * 4k optimised & trim only works in 4k requests + 4k aligned
1173 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "FTM??CT25H*", "*" },
1178 * XceedIOPS SATA SSDs
1181 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SG9XCS2D*", "*" },
1186 * Hama Innostor USB-Stick
1188 { T_DIRECT, SIP_MEDIA_REMOVABLE, "Innostor", "Innostor*", "*" },
1189 /*quirks*/DA_Q_NO_RC16
1193 * MX-ES USB Drive by Mach Xtreme
1195 { T_DIRECT, SIP_MEDIA_REMOVABLE, "MX", "MXUB3*", "*"},
1196 /*quirks*/DA_Q_NO_RC16
1200 static disk_strategy_t dastrategy;
1201 static dumper_t dadump;
1202 static periph_init_t dainit;
1203 static void daasync(void *callback_arg, u_int32_t code,
1204 struct cam_path *path, void *arg);
1205 static void dasysctlinit(void *context, int pending);
1206 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
1207 static int dadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
1208 static int dadeletemaxsysctl(SYSCTL_HANDLER_ARGS);
1209 static void dadeletemethodset(struct da_softc *softc,
1210 da_delete_methods delete_method);
1211 static off_t dadeletemaxsize(struct da_softc *softc,
1212 da_delete_methods delete_method);
1213 static void dadeletemethodchoose(struct da_softc *softc,
1214 da_delete_methods default_method);
1215 static void daprobedone(struct cam_periph *periph, union ccb *ccb);
1217 static periph_ctor_t daregister;
1218 static periph_dtor_t dacleanup;
1219 static periph_start_t dastart;
1220 static periph_oninv_t daoninvalidate;
1221 static void dadone(struct cam_periph *periph,
1222 union ccb *done_ccb);
1223 static int daerror(union ccb *ccb, u_int32_t cam_flags,
1224 u_int32_t sense_flags);
1225 static void daprevent(struct cam_periph *periph, int action);
1226 static void dareprobe(struct cam_periph *periph);
1227 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
1229 struct scsi_read_capacity_data_long *rcaplong,
1231 static timeout_t dasendorderedtag;
1232 static void dashutdown(void *arg, int howto);
1233 static timeout_t damediapoll;
1235 #ifndef DA_DEFAULT_POLL_PERIOD
1236 #define DA_DEFAULT_POLL_PERIOD 3
1239 #ifndef DA_DEFAULT_TIMEOUT
1240 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
1243 #ifndef DA_DEFAULT_RETRY
1244 #define DA_DEFAULT_RETRY 4
1247 #ifndef DA_DEFAULT_SEND_ORDERED
1248 #define DA_DEFAULT_SEND_ORDERED 1
1251 #define DA_SIO (softc->sort_io_queue >= 0 ? \
1252 softc->sort_io_queue : cam_sort_io_queues)
1254 static int da_poll_period = DA_DEFAULT_POLL_PERIOD;
1255 static int da_retry_count = DA_DEFAULT_RETRY;
1256 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
1257 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
1259 static SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
1260 "CAM Direct Access Disk driver");
1261 SYSCTL_INT(_kern_cam_da, OID_AUTO, poll_period, CTLFLAG_RW,
1262 &da_poll_period, 0, "Media polling period in seconds");
1263 TUNABLE_INT("kern.cam.da.poll_period", &da_poll_period);
1264 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
1265 &da_retry_count, 0, "Normal I/O retry count");
1266 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
1267 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
1268 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
1269 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
1270 SYSCTL_INT(_kern_cam_da, OID_AUTO, send_ordered, CTLFLAG_RW,
1271 &da_send_ordered, 0, "Send Ordered Tags");
1272 TUNABLE_INT("kern.cam.da.send_ordered", &da_send_ordered);
1275 * DA_ORDEREDTAG_INTERVAL determines how often, relative
1276 * to the default timeout, we check to see whether an ordered
1277 * tagged transaction is appropriate to prevent simple tag
1278 * starvation. Since we'd like to ensure that there is at least
1279 * 1/2 of the timeout length left for a starved transaction to
1280 * complete after we've sent an ordered tag, we must poll at least
1281 * four times in every timeout period. This takes care of the worst
1282 * case where a starved transaction starts during an interval that
1283 * meets the requirement "don't send an ordered tag" test so it takes
1284 * us two intervals to determine that a tag must be sent.
1286 #ifndef DA_ORDEREDTAG_INTERVAL
1287 #define DA_ORDEREDTAG_INTERVAL 4
1290 static struct periph_driver dadriver =
1293 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
1296 PERIPHDRIVER_DECLARE(da, dadriver);
1298 static MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
1301 daopen(struct disk *dp)
1303 struct cam_periph *periph;
1304 struct da_softc *softc;
1307 periph = (struct cam_periph *)dp->d_drv1;
1308 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1312 cam_periph_lock(periph);
1313 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
1314 cam_periph_unlock(periph);
1315 cam_periph_release(periph);
1319 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1322 softc = (struct da_softc *)periph->softc;
1325 /* Wait for the disk size update. */
1326 error = cam_periph_sleep(periph, &softc->disk->d_mediasize, PRIBIO,
1329 xpt_print(periph->path, "unable to retrieve capacity data\n");
1331 if (periph->flags & CAM_PERIPH_INVALID)
1334 if (error == 0 && (softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1335 (softc->quirks & DA_Q_NO_PREVENT) == 0)
1336 daprevent(periph, PR_PREVENT);
1339 softc->flags &= ~DA_FLAG_PACK_INVALID;
1340 softc->flags |= DA_FLAG_OPEN;
1343 cam_periph_unhold(periph);
1344 cam_periph_unlock(periph);
1347 cam_periph_release(periph);
1353 daclose(struct disk *dp)
1355 struct cam_periph *periph;
1356 struct da_softc *softc;
1360 periph = (struct cam_periph *)dp->d_drv1;
1361 softc = (struct da_softc *)periph->softc;
1362 cam_periph_lock(periph);
1363 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1366 if (cam_periph_hold(periph, PRIBIO) == 0) {
1368 /* Flush disk cache. */
1369 if ((softc->flags & DA_FLAG_DIRTY) != 0 &&
1370 (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0 &&
1371 (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
1372 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1373 scsi_synchronize_cache(&ccb->csio, /*retries*/1,
1374 /*cbfcnp*/dadone, MSG_SIMPLE_Q_TAG,
1375 /*begin_lba*/0, /*lb_count*/0, SSD_FULL_SIZE,
1377 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
1378 /*sense_flags*/SF_RETRY_UA | SF_QUIET_IR,
1379 softc->disk->d_devstat);
1380 softc->flags &= ~DA_FLAG_DIRTY;
1381 xpt_release_ccb(ccb);
1384 /* Allow medium removal. */
1385 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1386 (softc->quirks & DA_Q_NO_PREVENT) == 0)
1387 daprevent(periph, PR_ALLOW);
1389 cam_periph_unhold(periph);
1393 * If we've got removeable media, mark the blocksize as
1394 * unavailable, since it could change when new media is
1397 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0)
1398 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1400 softc->flags &= ~DA_FLAG_OPEN;
1401 while (softc->refcount != 0)
1402 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "daclose", 1);
1403 cam_periph_unlock(periph);
1404 cam_periph_release(periph);
1409 daschedule(struct cam_periph *periph)
1411 struct da_softc *softc = (struct da_softc *)periph->softc;
1413 if (softc->state != DA_STATE_NORMAL)
1416 /* Check if we have more work to do. */
1417 if (bioq_first(&softc->bio_queue) ||
1418 (!softc->delete_running && bioq_first(&softc->delete_queue)) ||
1420 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1425 * Actually translate the requested transfer into one the physical driver
1426 * can understand. The transfer is described by a buf and will include
1427 * only one physical transfer.
1430 dastrategy(struct bio *bp)
1432 struct cam_periph *periph;
1433 struct da_softc *softc;
1435 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1436 softc = (struct da_softc *)periph->softc;
1438 cam_periph_lock(periph);
1441 * If the device has been made invalid, error out
1443 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1444 cam_periph_unlock(periph);
1445 biofinish(bp, NULL, ENXIO);
1449 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
1452 * Place it in the queue of disk activities for this disk
1454 if (bp->bio_cmd == BIO_DELETE) {
1455 bioq_disksort(&softc->delete_queue, bp);
1456 } else if (DA_SIO) {
1457 bioq_disksort(&softc->bio_queue, bp);
1459 bioq_insert_tail(&softc->bio_queue, bp);
1463 * Schedule ourselves for performing the work.
1466 cam_periph_unlock(periph);
1472 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1474 struct cam_periph *periph;
1475 struct da_softc *softc;
1477 struct ccb_scsiio csio;
1482 periph = dp->d_drv1;
1483 softc = (struct da_softc *)periph->softc;
1484 cam_periph_lock(periph);
1485 secsize = softc->params.secsize;
1487 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1488 cam_periph_unlock(periph);
1493 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1494 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1495 scsi_read_write(&csio,
1499 /*read*/SCSI_RW_WRITE,
1501 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1504 /*data_ptr*/(u_int8_t *) virtual,
1505 /*dxfer_len*/length,
1506 /*sense_len*/SSD_FULL_SIZE,
1507 da_default_timeout * 1000);
1508 xpt_polled_action((union ccb *)&csio);
1510 error = cam_periph_error((union ccb *)&csio,
1511 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1512 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1513 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1514 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1516 printf("Aborting dump due to I/O error.\n");
1517 cam_periph_unlock(periph);
1522 * Sync the disk cache contents to the physical media.
1524 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1526 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1527 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1528 scsi_synchronize_cache(&csio,
1532 /*begin_lba*/0,/* Cover the whole disk */
1536 xpt_polled_action((union ccb *)&csio);
1538 error = cam_periph_error((union ccb *)&csio,
1539 0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL);
1540 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1541 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1542 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1544 xpt_print(periph->path, "Synchronize cache failed\n");
1546 cam_periph_unlock(periph);
1551 dagetattr(struct bio *bp)
1554 struct cam_periph *periph;
1556 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1557 cam_periph_lock(periph);
1558 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1560 cam_periph_unlock(periph);
1562 bp->bio_completed = bp->bio_length;
1572 * Install a global async callback. This callback will
1573 * receive async callbacks like "new device found".
1575 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1577 if (status != CAM_REQ_CMP) {
1578 printf("da: Failed to attach master async callback "
1579 "due to status 0x%x!\n", status);
1580 } else if (da_send_ordered) {
1582 /* Register our shutdown event handler */
1583 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1584 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1585 printf("dainit: shutdown event registration failed!\n");
1590 * Callback from GEOM, called when it has finished cleaning up its
1594 dadiskgonecb(struct disk *dp)
1596 struct cam_periph *periph;
1598 periph = (struct cam_periph *)dp->d_drv1;
1599 cam_periph_release(periph);
1603 daoninvalidate(struct cam_periph *periph)
1605 struct da_softc *softc;
1607 softc = (struct da_softc *)periph->softc;
1610 * De-register any async callbacks.
1612 xpt_register_async(0, daasync, periph, periph->path);
1614 softc->flags |= DA_FLAG_PACK_INVALID;
1617 * Return all queued I/O with ENXIO.
1618 * XXX Handle any transactions queued to the card
1619 * with XPT_ABORT_CCB.
1621 bioq_flush(&softc->bio_queue, NULL, ENXIO);
1622 bioq_flush(&softc->delete_queue, NULL, ENXIO);
1625 * Tell GEOM that we've gone away, we'll get a callback when it is
1626 * done cleaning up its resources.
1628 disk_gone(softc->disk);
1632 dacleanup(struct cam_periph *periph)
1634 struct da_softc *softc;
1636 softc = (struct da_softc *)periph->softc;
1638 cam_periph_unlock(periph);
1641 * If we can't free the sysctl tree, oh well...
1643 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1644 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1645 xpt_print(periph->path, "can't remove sysctl context\n");
1648 callout_drain(&softc->mediapoll_c);
1649 disk_destroy(softc->disk);
1650 callout_drain(&softc->sendordered_c);
1651 free(softc, M_DEVBUF);
1652 cam_periph_lock(periph);
1656 daasync(void *callback_arg, u_int32_t code,
1657 struct cam_path *path, void *arg)
1659 struct cam_periph *periph;
1660 struct da_softc *softc;
1662 periph = (struct cam_periph *)callback_arg;
1664 case AC_FOUND_DEVICE:
1666 struct ccb_getdev *cgd;
1669 cgd = (struct ccb_getdev *)arg;
1673 if (cgd->protocol != PROTO_SCSI)
1675 if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED)
1677 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1678 && SID_TYPE(&cgd->inq_data) != T_RBC
1679 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1683 * Allocate a peripheral instance for
1684 * this device and start the probe
1687 status = cam_periph_alloc(daregister, daoninvalidate,
1689 "da", CAM_PERIPH_BIO,
1691 AC_FOUND_DEVICE, cgd);
1693 if (status != CAM_REQ_CMP
1694 && status != CAM_REQ_INPROG)
1695 printf("daasync: Unable to attach to new device "
1696 "due to status 0x%x\n", status);
1699 case AC_ADVINFO_CHANGED:
1703 buftype = (uintptr_t)arg;
1704 if (buftype == CDAI_TYPE_PHYS_PATH) {
1705 struct da_softc *softc;
1707 softc = periph->softc;
1708 disk_attr_changed(softc->disk, "GEOM::physpath",
1713 case AC_UNIT_ATTENTION:
1716 int error_code, sense_key, asc, ascq;
1718 softc = (struct da_softc *)periph->softc;
1719 ccb = (union ccb *)arg;
1722 * Handle all UNIT ATTENTIONs except our own,
1723 * as they will be handled by daerror().
1725 if (xpt_path_periph(ccb->ccb_h.path) != periph &&
1726 scsi_extract_sense_ccb(ccb,
1727 &error_code, &sense_key, &asc, &ascq)) {
1728 if (asc == 0x2A && ascq == 0x09) {
1729 xpt_print(ccb->ccb_h.path,
1730 "Capacity data has changed\n");
1731 softc->flags &= ~DA_FLAG_PROBED;
1733 } else if (asc == 0x28 && ascq == 0x00) {
1734 softc->flags &= ~DA_FLAG_PROBED;
1735 disk_media_changed(softc->disk, M_NOWAIT);
1736 } else if (asc == 0x3F && ascq == 0x03) {
1737 xpt_print(ccb->ccb_h.path,
1738 "INQUIRY data has changed\n");
1739 softc->flags &= ~DA_FLAG_PROBED;
1743 cam_periph_async(periph, code, path, arg);
1747 softc = (struct da_softc *)periph->softc;
1749 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
1758 struct ccb_hdr *ccbh;
1760 softc = (struct da_softc *)periph->softc;
1762 * Don't fail on the expected unit attention
1765 softc->flags |= DA_FLAG_RETRY_UA;
1766 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1767 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1770 case AC_INQ_CHANGED:
1771 softc = (struct da_softc *)periph->softc;
1772 softc->flags &= ~DA_FLAG_PROBED;
1778 cam_periph_async(periph, code, path, arg);
1782 dasysctlinit(void *context, int pending)
1784 struct cam_periph *periph;
1785 struct da_softc *softc;
1786 char tmpstr[80], tmpstr2[80];
1787 struct ccb_trans_settings cts;
1789 periph = (struct cam_periph *)context;
1791 * periph was held for us when this task was enqueued
1793 if (periph->flags & CAM_PERIPH_INVALID) {
1794 cam_periph_release(periph);
1798 softc = (struct da_softc *)periph->softc;
1799 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1800 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1802 sysctl_ctx_init(&softc->sysctl_ctx);
1803 softc->flags |= DA_FLAG_SCTX_INIT;
1804 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1805 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1806 CTLFLAG_RD, 0, tmpstr);
1807 if (softc->sysctl_tree == NULL) {
1808 printf("dasysctlinit: unable to allocate sysctl tree\n");
1809 cam_periph_release(periph);
1814 * Now register the sysctl handler, so the user can change the value on
1817 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1818 OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RWTUN,
1819 softc, 0, dadeletemethodsysctl, "A",
1820 "BIO_DELETE execution method");
1821 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1822 OID_AUTO, "delete_max", CTLTYPE_U64 | CTLFLAG_RW,
1823 softc, 0, dadeletemaxsysctl, "Q",
1824 "Maximum BIO_DELETE size");
1825 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1826 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1827 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1828 "Minimum CDB size");
1829 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1830 OID_AUTO, "sort_io_queue", CTLFLAG_RW, &softc->sort_io_queue, 0,
1831 "Sort IO queue to try and optimise disk access patterns");
1833 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1834 SYSCTL_CHILDREN(softc->sysctl_tree),
1838 &softc->error_inject,
1840 "error_inject leaf");
1844 * Add some addressing info.
1846 memset(&cts, 0, sizeof (cts));
1847 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1848 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1849 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1850 cam_periph_lock(periph);
1851 xpt_action((union ccb *)&cts);
1852 cam_periph_unlock(periph);
1853 if (cts.ccb_h.status != CAM_REQ_CMP) {
1854 cam_periph_release(periph);
1857 if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
1858 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1859 if (fc->valid & CTS_FC_VALID_WWPN) {
1860 softc->wwpn = fc->wwpn;
1861 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1862 SYSCTL_CHILDREN(softc->sysctl_tree),
1863 OID_AUTO, "wwpn", CTLFLAG_RD,
1864 &softc->wwpn, "World Wide Port Name");
1867 cam_periph_release(periph);
1871 dadeletemaxsysctl(SYSCTL_HANDLER_ARGS)
1875 struct da_softc *softc;
1877 softc = (struct da_softc *)arg1;
1879 value = softc->disk->d_delmaxsize;
1880 error = sysctl_handle_64(oidp, &value, 0, req);
1881 if ((error != 0) || (req->newptr == NULL))
1884 /* only accept values smaller than the calculated value */
1885 if (value > dadeletemaxsize(softc, softc->delete_method)) {
1888 softc->disk->d_delmaxsize = value;
1894 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1898 value = *(int *)arg1;
1900 error = sysctl_handle_int(oidp, &value, 0, req);
1903 || (req->newptr == NULL))
1907 * Acceptable values here are 6, 10, 12 or 16.
1911 else if ((value > 6)
1914 else if ((value > 10)
1917 else if (value > 12)
1920 *(int *)arg1 = value;
1926 dadeletemethodset(struct da_softc *softc, da_delete_methods delete_method)
1929 softc->delete_method = delete_method;
1930 softc->disk->d_delmaxsize = dadeletemaxsize(softc, delete_method);
1931 softc->delete_func = da_delete_functions[delete_method];
1933 if (softc->delete_method > DA_DELETE_DISABLE)
1934 softc->disk->d_flags |= DISKFLAG_CANDELETE;
1936 softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
1940 dadeletemaxsize(struct da_softc *softc, da_delete_methods delete_method)
1944 switch(delete_method) {
1945 case DA_DELETE_UNMAP:
1946 sectors = (off_t)softc->unmap_max_lba;
1948 case DA_DELETE_ATA_TRIM:
1949 sectors = (off_t)ATA_DSM_RANGE_MAX * softc->trim_max_ranges;
1951 case DA_DELETE_WS16:
1952 sectors = omin(softc->ws_max_blks, WS16_MAX_BLKS);
1954 case DA_DELETE_ZERO:
1955 case DA_DELETE_WS10:
1956 sectors = omin(softc->ws_max_blks, WS10_MAX_BLKS);
1962 return (off_t)softc->params.secsize *
1963 omin(sectors, softc->params.sectors);
1967 daprobedone(struct cam_periph *periph, union ccb *ccb)
1969 struct da_softc *softc;
1971 softc = (struct da_softc *)periph->softc;
1973 dadeletemethodchoose(softc, DA_DELETE_NONE);
1975 if (bootverbose && (softc->flags & DA_FLAG_ANNOUNCED) == 0) {
1979 snprintf(buf, sizeof(buf), "Delete methods: <");
1981 for (i = 0; i <= DA_DELETE_MAX; i++) {
1982 if ((softc->delete_available & (1 << i)) == 0 &&
1983 i != softc->delete_method)
1986 strlcat(buf, ",", sizeof(buf));
1987 strlcat(buf, da_delete_method_names[i],
1989 if (i == softc->delete_method)
1990 strlcat(buf, "(*)", sizeof(buf));
1993 strlcat(buf, ">", sizeof(buf));
1994 printf("%s%d: %s\n", periph->periph_name,
1995 periph->unit_number, buf);
1999 * Since our peripheral may be invalidated by an error
2000 * above or an external event, we must release our CCB
2001 * before releasing the probe lock on the peripheral.
2002 * The peripheral will only go away once the last lock
2003 * is removed, and we need it around for the CCB release
2006 xpt_release_ccb(ccb);
2007 softc->state = DA_STATE_NORMAL;
2008 softc->flags |= DA_FLAG_PROBED;
2010 wakeup(&softc->disk->d_mediasize);
2011 if ((softc->flags & DA_FLAG_ANNOUNCED) == 0) {
2012 softc->flags |= DA_FLAG_ANNOUNCED;
2013 cam_periph_unhold(periph);
2015 cam_periph_release_locked(periph);
2019 dadeletemethodchoose(struct da_softc *softc, da_delete_methods default_method)
2023 /* If available, prefer the method requested by user. */
2024 i = softc->delete_method_pref;
2025 methods = softc->delete_available | (1 << DA_DELETE_DISABLE);
2026 if (methods & (1 << i)) {
2027 dadeletemethodset(softc, i);
2031 /* Use the pre-defined order to choose the best performing delete. */
2032 for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
2033 if (i == DA_DELETE_ZERO)
2035 if (softc->delete_available & (1 << i)) {
2036 dadeletemethodset(softc, i);
2041 /* Fallback to default. */
2042 dadeletemethodset(softc, default_method);
2046 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
2050 struct da_softc *softc;
2051 int i, error, methods, value;
2053 softc = (struct da_softc *)arg1;
2055 value = softc->delete_method;
2056 if (value < 0 || value > DA_DELETE_MAX)
2059 p = da_delete_method_names[value];
2060 strncpy(buf, p, sizeof(buf));
2061 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
2062 if (error != 0 || req->newptr == NULL)
2064 methods = softc->delete_available | (1 << DA_DELETE_DISABLE);
2065 for (i = 0; i <= DA_DELETE_MAX; i++) {
2066 if (strcmp(buf, da_delete_method_names[i]) == 0)
2069 if (i > DA_DELETE_MAX)
2071 softc->delete_method_pref = i;
2072 dadeletemethodchoose(softc, DA_DELETE_NONE);
2077 daregister(struct cam_periph *periph, void *arg)
2079 struct da_softc *softc;
2080 struct ccb_pathinq cpi;
2081 struct ccb_getdev *cgd;
2085 cgd = (struct ccb_getdev *)arg;
2087 printf("daregister: no getdev CCB, can't register device\n");
2088 return(CAM_REQ_CMP_ERR);
2091 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
2094 if (softc == NULL) {
2095 printf("daregister: Unable to probe new device. "
2096 "Unable to allocate softc\n");
2097 return(CAM_REQ_CMP_ERR);
2100 LIST_INIT(&softc->pending_ccbs);
2101 softc->state = DA_STATE_PROBE_WP;
2102 bioq_init(&softc->bio_queue);
2103 bioq_init(&softc->delete_queue);
2104 bioq_init(&softc->delete_run_queue);
2105 if (SID_IS_REMOVABLE(&cgd->inq_data))
2106 softc->flags |= DA_FLAG_PACK_REMOVABLE;
2107 softc->unmap_max_ranges = UNMAP_MAX_RANGES;
2108 softc->unmap_max_lba = UNMAP_RANGE_MAX;
2109 softc->ws_max_blks = WS16_MAX_BLKS;
2110 softc->trim_max_ranges = ATA_TRIM_MAX_RANGES;
2111 softc->sort_io_queue = -1;
2113 periph->softc = softc;
2116 * See if this device has any quirks.
2118 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
2119 (caddr_t)da_quirk_table,
2120 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
2121 sizeof(*da_quirk_table), scsi_inquiry_match);
2124 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
2126 softc->quirks = DA_Q_NONE;
2128 /* Check if the SIM does not want 6 byte commands */
2129 bzero(&cpi, sizeof(cpi));
2130 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2131 cpi.ccb_h.func_code = XPT_PATH_INQ;
2132 xpt_action((union ccb *)&cpi);
2133 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
2134 softc->quirks |= DA_Q_NO_6_BYTE;
2136 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
2139 * Take an exclusive refcount on the periph while dastart is called
2140 * to finish the probe. The reference will be dropped in dadone at
2143 (void)cam_periph_hold(periph, PRIBIO);
2146 * Schedule a periodic event to occasionally send an
2147 * ordered tag to a device.
2149 callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0);
2150 callout_reset(&softc->sendordered_c,
2151 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2152 dasendorderedtag, softc);
2154 cam_periph_unlock(periph);
2156 * RBC devices don't have to support READ(6), only READ(10).
2158 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
2159 softc->minimum_cmd_size = 10;
2161 softc->minimum_cmd_size = 6;
2164 * Load the user's default, if any.
2166 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
2167 periph->unit_number);
2168 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
2171 * 6, 10, 12 and 16 are the currently permissible values.
2173 if (softc->minimum_cmd_size < 6)
2174 softc->minimum_cmd_size = 6;
2175 else if ((softc->minimum_cmd_size > 6)
2176 && (softc->minimum_cmd_size <= 10))
2177 softc->minimum_cmd_size = 10;
2178 else if ((softc->minimum_cmd_size > 10)
2179 && (softc->minimum_cmd_size <= 12))
2180 softc->minimum_cmd_size = 12;
2181 else if (softc->minimum_cmd_size > 12)
2182 softc->minimum_cmd_size = 16;
2184 /* Predict whether device may support READ CAPACITY(16). */
2185 if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3 &&
2186 (softc->quirks & DA_Q_NO_RC16) == 0) {
2187 softc->flags |= DA_FLAG_CAN_RC16;
2191 * Register this media as a disk.
2193 softc->disk = disk_alloc();
2194 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
2195 periph->unit_number, 0,
2196 DEVSTAT_BS_UNAVAILABLE,
2197 SID_TYPE(&cgd->inq_data) |
2198 XPORT_DEVSTAT_TYPE(cpi.transport),
2199 DEVSTAT_PRIORITY_DISK);
2200 softc->disk->d_open = daopen;
2201 softc->disk->d_close = daclose;
2202 softc->disk->d_strategy = dastrategy;
2203 softc->disk->d_dump = dadump;
2204 softc->disk->d_getattr = dagetattr;
2205 softc->disk->d_gone = dadiskgonecb;
2206 softc->disk->d_name = "da";
2207 softc->disk->d_drv1 = periph;
2209 softc->maxio = DFLTPHYS; /* traditional default */
2210 else if (cpi.maxio > MAXPHYS)
2211 softc->maxio = MAXPHYS; /* for safety */
2213 softc->maxio = cpi.maxio;
2214 softc->disk->d_maxsize = softc->maxio;
2215 softc->disk->d_unit = periph->unit_number;
2216 softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION;
2217 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
2218 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
2219 if ((cpi.hba_misc & PIM_UNMAPPED) != 0)
2220 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
2221 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
2222 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
2223 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
2224 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
2225 cgd->inq_data.product, sizeof(cgd->inq_data.product),
2226 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
2227 softc->disk->d_hba_vendor = cpi.hba_vendor;
2228 softc->disk->d_hba_device = cpi.hba_device;
2229 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
2230 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
2233 * Acquire a reference to the periph before we register with GEOM.
2234 * We'll release this reference once GEOM calls us back (via
2235 * dadiskgonecb()) telling us that our provider has been freed.
2237 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
2238 xpt_print(periph->path, "%s: lost periph during "
2239 "registration!\n", __func__);
2240 cam_periph_lock(periph);
2241 return (CAM_REQ_CMP_ERR);
2244 disk_create(softc->disk, DISK_VERSION);
2245 cam_periph_lock(periph);
2248 * Add async callbacks for events of interest.
2249 * I don't bother checking if this fails as,
2250 * in most cases, the system will function just
2251 * fine without them and the only alternative
2252 * would be to not attach the device on failure.
2254 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
2255 AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION |
2256 AC_INQ_CHANGED, daasync, periph, periph->path);
2259 * Emit an attribute changed notification just in case
2260 * physical path information arrived before our async
2261 * event handler was registered, but after anyone attaching
2262 * to our disk device polled it.
2264 disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
2267 * Schedule a periodic media polling events.
2269 callout_init_mtx(&softc->mediapoll_c, cam_periph_mtx(periph), 0);
2270 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
2271 (cgd->inq_flags & SID_AEN) == 0 &&
2272 da_poll_period != 0)
2273 callout_reset(&softc->mediapoll_c, da_poll_period * hz,
2274 damediapoll, periph);
2276 xpt_schedule(periph, CAM_PRIORITY_DEV);
2278 return(CAM_REQ_CMP);
2282 dastart(struct cam_periph *periph, union ccb *start_ccb)
2284 struct da_softc *softc;
2286 softc = (struct da_softc *)periph->softc;
2288 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
2291 switch (softc->state) {
2292 case DA_STATE_NORMAL:
2297 /* Run BIO_DELETE if not running yet. */
2298 if (!softc->delete_running &&
2299 (bp = bioq_first(&softc->delete_queue)) != NULL) {
2300 if (softc->delete_func != NULL) {
2301 softc->delete_func(periph, start_ccb, bp);
2304 bioq_flush(&softc->delete_queue, NULL, 0);
2309 /* Run regular command. */
2310 bp = bioq_takefirst(&softc->bio_queue);
2314 scsi_test_unit_ready(&start_ccb->csio,
2315 /*retries*/ da_retry_count,
2319 da_default_timeout * 1000);
2320 start_ccb->ccb_h.ccb_bp = NULL;
2321 start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
2322 xpt_action(start_ccb);
2324 xpt_release_ccb(start_ccb);
2329 cam_periph_release_locked(periph);
2332 if ((bp->bio_flags & BIO_ORDERED) != 0 ||
2333 (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
2334 softc->flags &= ~DA_FLAG_NEED_OTAG;
2335 softc->flags |= DA_FLAG_WAS_OTAG;
2336 tag_code = MSG_ORDERED_Q_TAG;
2338 tag_code = MSG_SIMPLE_Q_TAG;
2341 switch (bp->bio_cmd) {
2348 if (bp->bio_cmd == BIO_WRITE) {
2349 softc->flags |= DA_FLAG_DIRTY;
2350 rw_op = SCSI_RW_WRITE;
2352 rw_op = SCSI_RW_READ;
2355 data_ptr = bp->bio_data;
2356 if ((bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0) {
2357 rw_op |= SCSI_RW_BIO;
2361 scsi_read_write(&start_ccb->csio,
2362 /*retries*/da_retry_count,
2364 /*tag_action*/tag_code,
2367 softc->minimum_cmd_size,
2368 /*lba*/bp->bio_pblkno,
2369 /*block_count*/bp->bio_bcount /
2370 softc->params.secsize,
2372 /*dxfer_len*/ bp->bio_bcount,
2373 /*sense_len*/SSD_FULL_SIZE,
2374 da_default_timeout * 1000);
2379 * BIO_FLUSH doesn't currently communicate
2380 * range data, so we synchronize the cache
2381 * over the whole disk. We also force
2382 * ordered tag semantics the flush applies
2383 * to all previously queued I/O.
2385 scsi_synchronize_cache(&start_ccb->csio,
2392 da_default_timeout*1000);
2395 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
2396 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
2399 LIST_INSERT_HEAD(&softc->pending_ccbs,
2400 &start_ccb->ccb_h, periph_links.le);
2402 /* We expect a unit attention from this device */
2403 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
2404 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
2405 softc->flags &= ~DA_FLAG_RETRY_UA;
2408 start_ccb->ccb_h.ccb_bp = bp;
2410 cam_periph_unlock(periph);
2411 xpt_action(start_ccb);
2412 cam_periph_lock(periph);
2415 /* May have more work to do, so ensure we stay scheduled */
2419 case DA_STATE_PROBE_WP:
2425 mode_buf = malloc(mode_buf_len, M_SCSIDA, M_NOWAIT);
2426 if (mode_buf == NULL) {
2427 xpt_print(periph->path, "Unable to send mode sense - "
2428 "malloc failure\n");
2429 softc->state = DA_STATE_PROBE_RC;
2432 scsi_mode_sense_len(&start_ccb->csio,
2433 /*retries*/ da_retry_count,
2435 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2437 /*pc*/ SMS_PAGE_CTRL_CURRENT,
2438 /*page*/ SMS_ALL_PAGES_PAGE,
2439 /*param_buf*/ mode_buf,
2440 /*param_len*/ mode_buf_len,
2441 /*minimum_cmd_size*/ softc->minimum_cmd_size,
2442 /*sense_len*/ SSD_FULL_SIZE,
2443 /*timeout*/ da_default_timeout * 1000);
2444 start_ccb->ccb_h.ccb_bp = NULL;
2445 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_WP;
2446 xpt_action(start_ccb);
2449 case DA_STATE_PROBE_RC:
2451 struct scsi_read_capacity_data *rcap;
2453 rcap = (struct scsi_read_capacity_data *)
2454 malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
2456 printf("dastart: Couldn't malloc read_capacity data\n");
2457 /* da_free_periph??? */
2460 scsi_read_capacity(&start_ccb->csio,
2461 /*retries*/da_retry_count,
2467 start_ccb->ccb_h.ccb_bp = NULL;
2468 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC;
2469 xpt_action(start_ccb);
2472 case DA_STATE_PROBE_RC16:
2474 struct scsi_read_capacity_data_long *rcaplong;
2476 rcaplong = (struct scsi_read_capacity_data_long *)
2477 malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
2478 if (rcaplong == NULL) {
2479 printf("dastart: Couldn't malloc read_capacity data\n");
2480 /* da_free_periph??? */
2483 scsi_read_capacity_16(&start_ccb->csio,
2484 /*retries*/ da_retry_count,
2486 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2490 /*rcap_buf*/ (uint8_t *)rcaplong,
2491 /*rcap_buf_len*/ sizeof(*rcaplong),
2492 /*sense_len*/ SSD_FULL_SIZE,
2493 /*timeout*/ da_default_timeout * 1000);
2494 start_ccb->ccb_h.ccb_bp = NULL;
2495 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC16;
2496 xpt_action(start_ccb);
2499 case DA_STATE_PROBE_LBP:
2501 struct scsi_vpd_logical_block_prov *lbp;
2503 if (!scsi_vpd_supported_page(periph, SVPD_LBP)) {
2505 * If we get here we don't support any SBC-3 delete
2506 * methods with UNMAP as the Logical Block Provisioning
2507 * VPD page support is required for devices which
2508 * support it according to T10/1799-D Revision 31
2509 * however older revisions of the spec don't mandate
2510 * this so we currently don't remove these methods
2511 * from the available set.
2513 softc->state = DA_STATE_PROBE_BLK_LIMITS;
2517 lbp = (struct scsi_vpd_logical_block_prov *)
2518 malloc(sizeof(*lbp), M_SCSIDA, M_NOWAIT|M_ZERO);
2521 printf("dastart: Couldn't malloc lbp data\n");
2522 /* da_free_periph??? */
2526 scsi_inquiry(&start_ccb->csio,
2527 /*retries*/da_retry_count,
2529 /*tag_action*/MSG_SIMPLE_Q_TAG,
2530 /*inq_buf*/(u_int8_t *)lbp,
2531 /*inq_len*/sizeof(*lbp),
2533 /*page_code*/SVPD_LBP,
2534 /*sense_len*/SSD_MIN_SIZE,
2535 /*timeout*/da_default_timeout * 1000);
2536 start_ccb->ccb_h.ccb_bp = NULL;
2537 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_LBP;
2538 xpt_action(start_ccb);
2541 case DA_STATE_PROBE_BLK_LIMITS:
2543 struct scsi_vpd_block_limits *block_limits;
2545 if (!scsi_vpd_supported_page(periph, SVPD_BLOCK_LIMITS)) {
2546 /* Not supported skip to next probe */
2547 softc->state = DA_STATE_PROBE_BDC;
2551 block_limits = (struct scsi_vpd_block_limits *)
2552 malloc(sizeof(*block_limits), M_SCSIDA, M_NOWAIT|M_ZERO);
2554 if (block_limits == NULL) {
2555 printf("dastart: Couldn't malloc block_limits data\n");
2556 /* da_free_periph??? */
2560 scsi_inquiry(&start_ccb->csio,
2561 /*retries*/da_retry_count,
2563 /*tag_action*/MSG_SIMPLE_Q_TAG,
2564 /*inq_buf*/(u_int8_t *)block_limits,
2565 /*inq_len*/sizeof(*block_limits),
2567 /*page_code*/SVPD_BLOCK_LIMITS,
2568 /*sense_len*/SSD_MIN_SIZE,
2569 /*timeout*/da_default_timeout * 1000);
2570 start_ccb->ccb_h.ccb_bp = NULL;
2571 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BLK_LIMITS;
2572 xpt_action(start_ccb);
2575 case DA_STATE_PROBE_BDC:
2577 struct scsi_vpd_block_characteristics *bdc;
2579 if (!scsi_vpd_supported_page(periph, SVPD_BDC)) {
2580 softc->state = DA_STATE_PROBE_ATA;
2584 bdc = (struct scsi_vpd_block_characteristics *)
2585 malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
2588 printf("dastart: Couldn't malloc bdc data\n");
2589 /* da_free_periph??? */
2593 scsi_inquiry(&start_ccb->csio,
2594 /*retries*/da_retry_count,
2596 /*tag_action*/MSG_SIMPLE_Q_TAG,
2597 /*inq_buf*/(u_int8_t *)bdc,
2598 /*inq_len*/sizeof(*bdc),
2600 /*page_code*/SVPD_BDC,
2601 /*sense_len*/SSD_MIN_SIZE,
2602 /*timeout*/da_default_timeout * 1000);
2603 start_ccb->ccb_h.ccb_bp = NULL;
2604 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BDC;
2605 xpt_action(start_ccb);
2608 case DA_STATE_PROBE_ATA:
2610 struct ata_params *ata_params;
2612 if (!scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
2613 daprobedone(periph, start_ccb);
2617 ata_params = (struct ata_params*)
2618 malloc(sizeof(*ata_params), M_SCSIDA, M_NOWAIT|M_ZERO);
2620 if (ata_params == NULL) {
2621 printf("dastart: Couldn't malloc ata_params data\n");
2622 /* da_free_periph??? */
2626 scsi_ata_identify(&start_ccb->csio,
2627 /*retries*/da_retry_count,
2629 /*tag_action*/MSG_SIMPLE_Q_TAG,
2630 /*data_ptr*/(u_int8_t *)ata_params,
2631 /*dxfer_len*/sizeof(*ata_params),
2632 /*sense_len*/SSD_FULL_SIZE,
2633 /*timeout*/da_default_timeout * 1000);
2634 start_ccb->ccb_h.ccb_bp = NULL;
2635 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA;
2636 xpt_action(start_ccb);
2643 * In each of the methods below, while its the caller's
2644 * responsibility to ensure the request will fit into a
2645 * single device request, we might have changed the delete
2646 * method due to the device incorrectly advertising either
2647 * its supported methods or limits.
2649 * To prevent this causing further issues we validate the
2650 * against the methods limits, and warn which would
2651 * otherwise be unnecessary.
2654 da_delete_unmap(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2656 struct da_softc *softc = (struct da_softc *)periph->softc;;
2658 uint8_t *buf = softc->unmap_buf;
2659 uint64_t lba, lastlba = (uint64_t)-1;
2660 uint64_t totalcount = 0;
2662 uint32_t lastcount = 0, c;
2663 uint32_t off, ranges = 0;
2666 * Currently this doesn't take the UNMAP
2667 * Granularity and Granularity Alignment
2668 * fields into account.
2670 * This could result in both unoptimal unmap
2671 * requests as as well as UNMAP calls unmapping
2672 * fewer LBA's than requested.
2675 softc->delete_running = 1;
2676 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2679 bioq_remove(&softc->delete_queue, bp1);
2681 bioq_insert_tail(&softc->delete_run_queue, bp1);
2682 lba = bp1->bio_pblkno;
2683 count = bp1->bio_bcount / softc->params.secsize;
2685 /* Try to extend the previous range. */
2686 if (lba == lastlba) {
2687 c = omin(count, UNMAP_RANGE_MAX - lastcount);
2689 off = ((ranges - 1) * UNMAP_RANGE_SIZE) +
2691 scsi_ulto4b(lastcount, &buf[off + 8]);
2698 c = omin(count, UNMAP_RANGE_MAX);
2699 if (totalcount + c > softc->unmap_max_lba ||
2700 ranges >= softc->unmap_max_ranges) {
2701 xpt_print(periph->path,
2702 "%s issuing short delete %ld > %ld"
2704 da_delete_method_desc[softc->delete_method],
2705 totalcount + c, softc->unmap_max_lba,
2706 ranges, softc->unmap_max_ranges);
2709 off = (ranges * UNMAP_RANGE_SIZE) + UNMAP_HEAD_SIZE;
2710 scsi_u64to8b(lba, &buf[off + 0]);
2711 scsi_ulto4b(c, &buf[off + 8]);
2719 bp1 = bioq_first(&softc->delete_queue);
2720 if (bp1 == NULL || ranges >= softc->unmap_max_ranges ||
2721 totalcount + bp1->bio_bcount /
2722 softc->params.secsize > softc->unmap_max_lba)
2725 scsi_ulto2b(ranges * 16 + 6, &buf[0]);
2726 scsi_ulto2b(ranges * 16, &buf[2]);
2728 scsi_unmap(&ccb->csio,
2729 /*retries*/da_retry_count,
2731 /*tag_action*/MSG_SIMPLE_Q_TAG,
2734 /*dxfer_len*/ ranges * 16 + 8,
2735 /*sense_len*/SSD_FULL_SIZE,
2736 da_default_timeout * 1000);
2737 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2738 ccb->ccb_h.flags |= CAM_UNLOCKED;
2742 da_delete_trim(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2744 struct da_softc *softc = (struct da_softc *)periph->softc;
2746 uint8_t *buf = softc->unmap_buf;
2747 uint64_t lastlba = (uint64_t)-1;
2750 uint32_t lastcount = 0, c, requestcount;
2751 int ranges = 0, off, block_count;
2753 softc->delete_running = 1;
2754 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2757 bioq_remove(&softc->delete_queue, bp1);
2759 bioq_insert_tail(&softc->delete_run_queue, bp1);
2760 lba = bp1->bio_pblkno;
2761 count = bp1->bio_bcount / softc->params.secsize;
2762 requestcount = count;
2764 /* Try to extend the previous range. */
2765 if (lba == lastlba) {
2766 c = omin(count, ATA_DSM_RANGE_MAX - lastcount);
2768 off = (ranges - 1) * 8;
2769 buf[off + 6] = lastcount & 0xff;
2770 buf[off + 7] = (lastcount >> 8) & 0xff;
2776 c = omin(count, ATA_DSM_RANGE_MAX);
2779 buf[off + 0] = lba & 0xff;
2780 buf[off + 1] = (lba >> 8) & 0xff;
2781 buf[off + 2] = (lba >> 16) & 0xff;
2782 buf[off + 3] = (lba >> 24) & 0xff;
2783 buf[off + 4] = (lba >> 32) & 0xff;
2784 buf[off + 5] = (lba >> 40) & 0xff;
2785 buf[off + 6] = c & 0xff;
2786 buf[off + 7] = (c >> 8) & 0xff;
2791 if (count != 0 && ranges == softc->trim_max_ranges) {
2792 xpt_print(periph->path,
2793 "%s issuing short delete %ld > %ld\n",
2794 da_delete_method_desc[softc->delete_method],
2796 (softc->trim_max_ranges - ranges) *
2802 bp1 = bioq_first(&softc->delete_queue);
2803 if (bp1 == NULL || bp1->bio_bcount / softc->params.secsize >
2804 (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX)
2808 block_count = (ranges + ATA_DSM_BLK_RANGES - 1) / ATA_DSM_BLK_RANGES;
2809 scsi_ata_trim(&ccb->csio,
2810 /*retries*/da_retry_count,
2812 /*tag_action*/MSG_SIMPLE_Q_TAG,
2815 /*dxfer_len*/block_count * ATA_DSM_BLK_SIZE,
2816 /*sense_len*/SSD_FULL_SIZE,
2817 da_default_timeout * 1000);
2818 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2819 ccb->ccb_h.flags |= CAM_UNLOCKED;
2823 * We calculate ws_max_blks here based off d_delmaxsize instead
2824 * of using softc->ws_max_blks as it is absolute max for the
2825 * device not the protocol max which may well be lower.
2828 da_delete_ws(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2830 struct da_softc *softc;
2832 uint64_t ws_max_blks;
2834 uint64_t count; /* forward compat with WS32 */
2836 softc = (struct da_softc *)periph->softc;
2837 ws_max_blks = softc->disk->d_delmaxsize / softc->params.secsize;
2838 softc->delete_running = 1;
2839 lba = bp->bio_pblkno;
2843 bioq_remove(&softc->delete_queue, bp1);
2845 bioq_insert_tail(&softc->delete_run_queue, bp1);
2846 count += bp1->bio_bcount / softc->params.secsize;
2847 if (count > ws_max_blks) {
2848 xpt_print(periph->path,
2849 "%s issuing short delete %ld > %ld\n",
2850 da_delete_method_desc[softc->delete_method],
2851 count, ws_max_blks);
2852 count = omin(count, ws_max_blks);
2855 bp1 = bioq_first(&softc->delete_queue);
2856 if (bp1 == NULL || lba + count != bp1->bio_pblkno ||
2857 count + bp1->bio_bcount /
2858 softc->params.secsize > ws_max_blks)
2862 scsi_write_same(&ccb->csio,
2863 /*retries*/da_retry_count,
2865 /*tag_action*/MSG_SIMPLE_Q_TAG,
2866 /*byte2*/softc->delete_method ==
2867 DA_DELETE_ZERO ? 0 : SWS_UNMAP,
2868 softc->delete_method == DA_DELETE_WS16 ? 16 : 10,
2870 /*block_count*/count,
2871 /*data_ptr*/ __DECONST(void *, zero_region),
2872 /*dxfer_len*/ softc->params.secsize,
2873 /*sense_len*/SSD_FULL_SIZE,
2874 da_default_timeout * 1000);
2875 ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2876 ccb->ccb_h.flags |= CAM_UNLOCKED;
2880 cmd6workaround(union ccb *ccb)
2882 struct scsi_rw_6 cmd6;
2883 struct scsi_rw_10 *cmd10;
2884 struct da_softc *softc;
2889 cdb = ccb->csio.cdb_io.cdb_bytes;
2890 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
2892 if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
2893 da_delete_methods old_method = softc->delete_method;
2896 * Typically there are two reasons for failure here
2897 * 1. Delete method was detected as supported but isn't
2898 * 2. Delete failed due to invalid params e.g. too big
2900 * While we will attempt to choose an alternative delete method
2901 * this may result in short deletes if the existing delete
2902 * requests from geom are big for the new method chosen.
2904 * This method assumes that the error which triggered this
2905 * will not retry the io otherwise a panic will occur
2907 dadeleteflag(softc, old_method, 0);
2908 dadeletemethodchoose(softc, DA_DELETE_DISABLE);
2909 if (softc->delete_method == DA_DELETE_DISABLE)
2910 xpt_print(ccb->ccb_h.path,
2911 "%s failed, disabling BIO_DELETE\n",
2912 da_delete_method_desc[old_method]);
2914 xpt_print(ccb->ccb_h.path,
2915 "%s failed, switching to %s BIO_DELETE\n",
2916 da_delete_method_desc[old_method],
2917 da_delete_method_desc[softc->delete_method]);
2919 while ((bp = bioq_takefirst(&softc->delete_run_queue)) != NULL)
2920 bioq_disksort(&softc->delete_queue, bp);
2921 bioq_disksort(&softc->delete_queue,
2922 (struct bio *)ccb->ccb_h.ccb_bp);
2923 ccb->ccb_h.ccb_bp = NULL;
2927 /* Detect unsupported PREVENT ALLOW MEDIUM REMOVAL. */
2928 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
2929 (*cdb == PREVENT_ALLOW) &&
2930 (softc->quirks & DA_Q_NO_PREVENT) == 0) {
2932 xpt_print(ccb->ccb_h.path,
2933 "PREVENT ALLOW MEDIUM REMOVAL not supported.\n");
2934 softc->quirks |= DA_Q_NO_PREVENT;
2938 /* Detect unsupported SYNCHRONIZE CACHE(10). */
2939 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
2940 (*cdb == SYNCHRONIZE_CACHE) &&
2941 (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
2943 xpt_print(ccb->ccb_h.path,
2944 "SYNCHRONIZE CACHE(10) not supported.\n");
2945 softc->quirks |= DA_Q_NO_SYNC_CACHE;
2946 softc->disk->d_flags &= ~DISKFLAG_CANFLUSHCACHE;
2950 /* Translation only possible if CDB is an array and cmd is R/W6 */
2951 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
2952 (*cdb != READ_6 && *cdb != WRITE_6))
2955 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
2956 "increasing minimum_cmd_size to 10.\n");
2957 softc->minimum_cmd_size = 10;
2959 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
2960 cmd10 = (struct scsi_rw_10 *)cdb;
2961 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
2963 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
2964 cmd10->reserved = 0;
2965 scsi_ulto2b(cmd6.length, cmd10->length);
2966 cmd10->control = cmd6.control;
2967 ccb->csio.cdb_len = sizeof(*cmd10);
2969 /* Requeue request, unfreezing queue if necessary */
2970 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
2971 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2974 cam_release_devq(ccb->ccb_h.path,
2978 /*getcount_only*/0);
2984 dadone(struct cam_periph *periph, union ccb *done_ccb)
2986 struct da_softc *softc;
2987 struct ccb_scsiio *csio;
2991 softc = (struct da_softc *)periph->softc;
2992 priority = done_ccb->ccb_h.pinfo.priority;
2994 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
2996 csio = &done_ccb->csio;
2997 state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
2999 case DA_CCB_BUFFER_IO:
3002 struct bio *bp, *bp1;
3004 cam_periph_lock(periph);
3005 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
3006 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3010 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
3015 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
3016 if (error == ERESTART) {
3018 * A retry was scheduled, so
3021 cam_periph_unlock(periph);
3024 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
3029 * return all queued I/O with EIO, so that
3030 * the client can retry these I/Os in the
3031 * proper order should it attempt to recover.
3036 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
3038 * Catastrophic error. Mark our pack as
3042 * XXX See if this is really a media
3045 xpt_print(periph->path,
3046 "Invalidating pack\n");
3047 softc->flags |= DA_FLAG_PACK_INVALID;
3048 queued_error = ENXIO;
3050 bioq_flush(&softc->bio_queue, NULL,
3053 bp->bio_error = error;
3054 bp->bio_resid = bp->bio_bcount;
3055 bp->bio_flags |= BIO_ERROR;
3057 } else if (bp != NULL) {
3058 if (state == DA_CCB_DELETE)
3061 bp->bio_resid = csio->resid;
3063 if (bp->bio_resid != 0)
3064 bp->bio_flags |= BIO_ERROR;
3066 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3067 cam_release_devq(done_ccb->ccb_h.path,
3071 /*getcount_only*/0);
3072 } else if (bp != NULL) {
3073 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3074 panic("REQ_CMP with QFRZN");
3075 if (state == DA_CCB_DELETE)
3078 bp->bio_resid = csio->resid;
3079 if (csio->resid > 0)
3080 bp->bio_flags |= BIO_ERROR;
3081 if (softc->error_inject != 0) {
3082 bp->bio_error = softc->error_inject;
3083 bp->bio_resid = bp->bio_bcount;
3084 bp->bio_flags |= BIO_ERROR;
3085 softc->error_inject = 0;
3089 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
3090 if (LIST_EMPTY(&softc->pending_ccbs))
3091 softc->flags |= DA_FLAG_WAS_OTAG;
3093 xpt_release_ccb(done_ccb);
3094 if (state == DA_CCB_DELETE) {
3095 TAILQ_HEAD(, bio) queue;
3098 TAILQ_CONCAT(&queue, &softc->delete_run_queue.queue, bio_queue);
3099 softc->delete_run_queue.insert_point = NULL;
3101 * Normally, the xpt_release_ccb() above would make sure
3102 * that when we have more work to do, that work would
3103 * get kicked off. However, we specifically keep
3104 * delete_running set to 0 before the call above to
3105 * allow other I/O to progress when many BIO_DELETE
3106 * requests are pushed down. We set delete_running to 0
3107 * and call daschedule again so that we don't stall if
3108 * there are no other I/Os pending apart from BIO_DELETEs.
3110 softc->delete_running = 0;
3112 cam_periph_unlock(periph);
3113 while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
3114 TAILQ_REMOVE(&queue, bp1, bio_queue);
3115 bp1->bio_error = bp->bio_error;
3116 if (bp->bio_flags & BIO_ERROR) {
3117 bp1->bio_flags |= BIO_ERROR;
3118 bp1->bio_resid = bp1->bio_bcount;
3124 cam_periph_unlock(periph);
3129 case DA_CCB_PROBE_WP:
3131 struct scsi_mode_header_6 *mode_hdr6;
3132 struct scsi_mode_header_10 *mode_hdr10;
3135 if (softc->minimum_cmd_size > 6) {
3136 mode_hdr10 = (struct scsi_mode_header_10 *)csio->data_ptr;
3137 dev_spec = mode_hdr10->dev_spec;
3139 mode_hdr6 = (struct scsi_mode_header_6 *)csio->data_ptr;
3140 dev_spec = mode_hdr6->dev_spec;
3142 if (cam_ccb_status(done_ccb) == CAM_REQ_CMP) {
3143 if ((dev_spec & 0x80) != 0)
3144 softc->disk->d_flags |= DISKFLAG_WRITE_PROTECT;
3146 softc->disk->d_flags &= ~DISKFLAG_WRITE_PROTECT;
3150 error = daerror(done_ccb, CAM_RETRY_SELTO,
3151 SF_RETRY_UA|SF_NO_PRINT);
3152 if (error == ERESTART)
3154 else if (error != 0) {
3155 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3156 /* Don't wedge this device's queue */
3157 cam_release_devq(done_ccb->ccb_h.path,
3161 /*getcount_only*/0);
3166 free(csio->data_ptr, M_SCSIDA);
3167 xpt_release_ccb(done_ccb);
3168 if ((softc->flags & DA_FLAG_CAN_RC16) != 0)
3169 softc->state = DA_STATE_PROBE_RC16;
3171 softc->state = DA_STATE_PROBE_RC;
3172 xpt_schedule(periph, priority);
3175 case DA_CCB_PROBE_RC:
3176 case DA_CCB_PROBE_RC16:
3178 struct scsi_read_capacity_data *rdcap;
3179 struct scsi_read_capacity_data_long *rcaplong;
3180 char announce_buf[80];
3186 if (state == DA_CCB_PROBE_RC)
3187 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
3189 rcaplong = (struct scsi_read_capacity_data_long *)
3192 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3193 struct disk_params *dp;
3194 uint32_t block_size;
3196 u_int lbppbe; /* LB per physical block exponent. */
3197 u_int lalba; /* Lowest aligned LBA. */
3199 if (state == DA_CCB_PROBE_RC) {
3200 block_size = scsi_4btoul(rdcap->length);
3201 maxsector = scsi_4btoul(rdcap->addr);
3206 * According to SBC-2, if the standard 10
3207 * byte READ CAPACITY command returns 2^32,
3208 * we should issue the 16 byte version of
3209 * the command, since the device in question
3210 * has more sectors than can be represented
3211 * with the short version of the command.
3213 if (maxsector == 0xffffffff) {
3214 free(rdcap, M_SCSIDA);
3215 xpt_release_ccb(done_ccb);
3216 softc->state = DA_STATE_PROBE_RC16;
3217 xpt_schedule(periph, priority);
3221 block_size = scsi_4btoul(rcaplong->length);
3222 maxsector = scsi_8btou64(rcaplong->addr);
3223 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
3224 lalba = scsi_2btoul(rcaplong->lalba_lbp);
3228 * Because GEOM code just will panic us if we
3229 * give them an 'illegal' value we'll avoid that
3232 if (block_size == 0) {
3237 if (block_size >= MAXPHYS) {
3238 xpt_print(periph->path,
3239 "unsupportable block size %ju\n",
3240 (uintmax_t) block_size);
3241 announce_buf[0] = '\0';
3242 cam_periph_invalidate(periph);
3245 * We pass rcaplong into dasetgeom(),
3246 * because it will only use it if it is
3249 dasetgeom(periph, block_size, maxsector,
3250 rcaplong, sizeof(*rcaplong));
3251 lbp = (lalba & SRC16_LBPME_A);
3252 dp = &softc->params;
3253 snprintf(announce_buf, sizeof(announce_buf),
3254 "%juMB (%ju %u byte sectors)",
3255 ((uintmax_t)dp->secsize * dp->sectors) /
3257 (uintmax_t)dp->sectors, dp->secsize);
3262 announce_buf[0] = '\0';
3265 * Retry any UNIT ATTENTION type errors. They
3266 * are expected at boot.
3268 error = daerror(done_ccb, CAM_RETRY_SELTO,
3269 SF_RETRY_UA|SF_NO_PRINT);
3270 if (error == ERESTART) {
3272 * A retry was scheuled, so
3276 } else if (error != 0) {
3278 int sense_key, error_code;
3281 struct ccb_getdev cgd;
3283 /* Don't wedge this device's queue */
3284 status = done_ccb->ccb_h.status;
3285 if ((status & CAM_DEV_QFRZN) != 0)
3286 cam_release_devq(done_ccb->ccb_h.path,
3290 /*getcount_only*/0);
3293 xpt_setup_ccb(&cgd.ccb_h,
3294 done_ccb->ccb_h.path,
3295 CAM_PRIORITY_NORMAL);
3296 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
3297 xpt_action((union ccb *)&cgd);
3299 if (scsi_extract_sense_ccb(done_ccb,
3300 &error_code, &sense_key, &asc, &ascq))
3306 * If we tried READ CAPACITY(16) and failed,
3307 * fallback to READ CAPACITY(10).
3309 if ((state == DA_CCB_PROBE_RC16) &&
3310 (softc->flags & DA_FLAG_CAN_RC16) &&
3311 (((csio->ccb_h.status & CAM_STATUS_MASK) ==
3314 (error_code == SSD_CURRENT_ERROR) &&
3315 (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
3316 softc->flags &= ~DA_FLAG_CAN_RC16;
3317 free(rdcap, M_SCSIDA);
3318 xpt_release_ccb(done_ccb);
3319 softc->state = DA_STATE_PROBE_RC;
3320 xpt_schedule(periph, priority);
3324 * Attach to anything that claims to be a
3325 * direct access or optical disk device,
3326 * as long as it doesn't return a "Logical
3327 * unit not supported" (0x25) error.
3329 if ((have_sense) && (asc != 0x25)
3330 && (error_code == SSD_CURRENT_ERROR)) {
3331 const char *sense_key_desc;
3332 const char *asc_desc;
3334 dasetgeom(periph, 512, -1, NULL, 0);
3335 scsi_sense_desc(sense_key, asc, ascq,
3339 snprintf(announce_buf,
3340 sizeof(announce_buf),
3341 "Attempt to query device "
3342 "size failed: %s, %s",
3350 xpt_print(periph->path,
3351 "got CAM status %#x\n",
3352 done_ccb->ccb_h.status);
3355 xpt_print(periph->path, "fatal error, "
3356 "failed to attach to device\n");
3359 * Free up resources.
3361 cam_periph_invalidate(periph);
3365 free(csio->data_ptr, M_SCSIDA);
3366 if (announce_buf[0] != '\0' &&
3367 ((softc->flags & DA_FLAG_ANNOUNCED) == 0)) {
3369 * Create our sysctl variables, now that we know
3370 * we have successfully attached.
3372 /* increase the refcount */
3373 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3374 taskqueue_enqueue(taskqueue_thread,
3375 &softc->sysctl_task);
3376 xpt_announce_periph(periph, announce_buf);
3377 xpt_announce_quirks(periph, softc->quirks,
3380 xpt_print(periph->path, "fatal error, "
3381 "could not acquire reference count\n");
3385 /* We already probed the device. */
3386 if (softc->flags & DA_FLAG_PROBED) {
3387 daprobedone(periph, done_ccb);
3391 /* Ensure re-probe doesn't see old delete. */
3392 softc->delete_available = 0;
3393 dadeleteflag(softc, DA_DELETE_ZERO, 1);
3394 if (lbp && (softc->quirks & DA_Q_NO_UNMAP) == 0) {
3396 * Based on older SBC-3 spec revisions
3397 * any of the UNMAP methods "may" be
3398 * available via LBP given this flag so
3399 * we flag all of them as available and
3400 * then remove those which further
3401 * probes confirm aren't available
3404 * We could also check readcap(16) p_type
3405 * flag to exclude one or more invalid
3406 * write same (X) types here
3408 dadeleteflag(softc, DA_DELETE_WS16, 1);
3409 dadeleteflag(softc, DA_DELETE_WS10, 1);
3410 dadeleteflag(softc, DA_DELETE_UNMAP, 1);
3412 xpt_release_ccb(done_ccb);
3413 softc->state = DA_STATE_PROBE_LBP;
3414 xpt_schedule(periph, priority);
3418 xpt_release_ccb(done_ccb);
3419 softc->state = DA_STATE_PROBE_BDC;
3420 xpt_schedule(periph, priority);
3423 case DA_CCB_PROBE_LBP:
3425 struct scsi_vpd_logical_block_prov *lbp;
3427 lbp = (struct scsi_vpd_logical_block_prov *)csio->data_ptr;
3429 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3431 * T10/1799-D Revision 31 states at least one of these
3432 * must be supported but we don't currently enforce this.
3434 dadeleteflag(softc, DA_DELETE_WS16,
3435 (lbp->flags & SVPD_LBP_WS16));
3436 dadeleteflag(softc, DA_DELETE_WS10,
3437 (lbp->flags & SVPD_LBP_WS10));
3438 dadeleteflag(softc, DA_DELETE_UNMAP,
3439 (lbp->flags & SVPD_LBP_UNMAP));
3442 error = daerror(done_ccb, CAM_RETRY_SELTO,
3443 SF_RETRY_UA|SF_NO_PRINT);
3444 if (error == ERESTART)
3446 else if (error != 0) {
3447 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3448 /* Don't wedge this device's queue */
3449 cam_release_devq(done_ccb->ccb_h.path,
3453 /*getcount_only*/0);
3457 * Failure indicates we don't support any SBC-3
3458 * delete methods with UNMAP
3463 free(lbp, M_SCSIDA);
3464 xpt_release_ccb(done_ccb);
3465 softc->state = DA_STATE_PROBE_BLK_LIMITS;
3466 xpt_schedule(periph, priority);
3469 case DA_CCB_PROBE_BLK_LIMITS:
3471 struct scsi_vpd_block_limits *block_limits;
3473 block_limits = (struct scsi_vpd_block_limits *)csio->data_ptr;
3475 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3476 uint32_t max_txfer_len = scsi_4btoul(
3477 block_limits->max_txfer_len);
3478 uint32_t max_unmap_lba_cnt = scsi_4btoul(
3479 block_limits->max_unmap_lba_cnt);
3480 uint32_t max_unmap_blk_cnt = scsi_4btoul(
3481 block_limits->max_unmap_blk_cnt);
3482 uint64_t ws_max_blks = scsi_8btou64(
3483 block_limits->max_write_same_length);
3485 if (max_txfer_len != 0) {
3486 softc->disk->d_maxsize = MIN(softc->maxio,
3487 (off_t)max_txfer_len * softc->params.secsize);
3491 * We should already support UNMAP but we check lba
3492 * and block count to be sure
3494 if (max_unmap_lba_cnt != 0x00L &&
3495 max_unmap_blk_cnt != 0x00L) {
3496 softc->unmap_max_lba = max_unmap_lba_cnt;
3497 softc->unmap_max_ranges = min(max_unmap_blk_cnt,
3501 * Unexpected UNMAP limits which means the
3502 * device doesn't actually support UNMAP
3504 dadeleteflag(softc, DA_DELETE_UNMAP, 0);
3507 if (ws_max_blks != 0x00L)
3508 softc->ws_max_blks = ws_max_blks;
3511 error = daerror(done_ccb, CAM_RETRY_SELTO,
3512 SF_RETRY_UA|SF_NO_PRINT);
3513 if (error == ERESTART)
3515 else if (error != 0) {
3516 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3517 /* Don't wedge this device's queue */
3518 cam_release_devq(done_ccb->ccb_h.path,
3522 /*getcount_only*/0);
3526 * Failure here doesn't mean UNMAP is not
3527 * supported as this is an optional page.
3529 softc->unmap_max_lba = 1;
3530 softc->unmap_max_ranges = 1;
3534 free(block_limits, M_SCSIDA);
3535 xpt_release_ccb(done_ccb);
3536 softc->state = DA_STATE_PROBE_BDC;
3537 xpt_schedule(periph, priority);
3540 case DA_CCB_PROBE_BDC:
3542 struct scsi_vpd_block_characteristics *bdc;
3544 bdc = (struct scsi_vpd_block_characteristics *)csio->data_ptr;
3546 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3548 * Disable queue sorting for non-rotational media
3551 u_int16_t old_rate = softc->disk->d_rotation_rate;
3553 softc->disk->d_rotation_rate =
3554 scsi_2btoul(bdc->medium_rotation_rate);
3555 if (softc->disk->d_rotation_rate ==
3556 SVPD_BDC_RATE_NON_ROTATING) {
3557 softc->sort_io_queue = 0;
3559 if (softc->disk->d_rotation_rate != old_rate) {
3560 disk_attr_changed(softc->disk,
3561 "GEOM::rotation_rate", M_NOWAIT);
3565 error = daerror(done_ccb, CAM_RETRY_SELTO,
3566 SF_RETRY_UA|SF_NO_PRINT);
3567 if (error == ERESTART)
3569 else if (error != 0) {
3570 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3571 /* Don't wedge this device's queue */
3572 cam_release_devq(done_ccb->ccb_h.path,
3576 /*getcount_only*/0);
3581 free(bdc, M_SCSIDA);
3582 xpt_release_ccb(done_ccb);
3583 softc->state = DA_STATE_PROBE_ATA;
3584 xpt_schedule(periph, priority);
3587 case DA_CCB_PROBE_ATA:
3590 struct ata_params *ata_params;
3593 ata_params = (struct ata_params *)csio->data_ptr;
3594 ptr = (uint16_t *)ata_params;
3596 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3599 for (i = 0; i < sizeof(*ata_params) / 2; i++)
3600 ptr[i] = le16toh(ptr[i]);
3601 if (ata_params->support_dsm & ATA_SUPPORT_DSM_TRIM &&
3602 (softc->quirks & DA_Q_NO_UNMAP) == 0) {
3603 dadeleteflag(softc, DA_DELETE_ATA_TRIM, 1);
3604 if (ata_params->max_dsm_blocks != 0)
3605 softc->trim_max_ranges = min(
3606 softc->trim_max_ranges,
3607 ata_params->max_dsm_blocks *
3608 ATA_DSM_BLK_RANGES);
3611 * Disable queue sorting for non-rotational media
3614 old_rate = softc->disk->d_rotation_rate;
3615 softc->disk->d_rotation_rate =
3616 ata_params->media_rotation_rate;
3617 if (softc->disk->d_rotation_rate ==
3618 ATA_RATE_NON_ROTATING) {
3619 softc->sort_io_queue = 0;
3622 if (softc->disk->d_rotation_rate != old_rate) {
3623 disk_attr_changed(softc->disk,
3624 "GEOM::rotation_rate", M_NOWAIT);
3628 error = daerror(done_ccb, CAM_RETRY_SELTO,
3629 SF_RETRY_UA|SF_NO_PRINT);
3630 if (error == ERESTART)
3632 else if (error != 0) {
3633 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3634 /* Don't wedge this device's queue */
3635 cam_release_devq(done_ccb->ccb_h.path,
3639 /*getcount_only*/0);
3644 free(ata_params, M_SCSIDA);
3645 daprobedone(periph, done_ccb);
3649 /* No-op. We're polling */
3653 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3655 if (daerror(done_ccb, CAM_RETRY_SELTO,
3656 SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
3659 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3660 cam_release_devq(done_ccb->ccb_h.path,
3664 /*getcount_only*/0);
3666 xpt_release_ccb(done_ccb);
3667 cam_periph_release_locked(periph);
3673 xpt_release_ccb(done_ccb);
3677 dareprobe(struct cam_periph *periph)
3679 struct da_softc *softc;
3682 softc = (struct da_softc *)periph->softc;
3684 /* Probe in progress; don't interfere. */
3685 if (softc->state != DA_STATE_NORMAL)
3688 status = cam_periph_acquire(periph);
3689 KASSERT(status == CAM_REQ_CMP,
3690 ("dareprobe: cam_periph_acquire failed"));
3692 softc->state = DA_STATE_PROBE_WP;
3693 xpt_schedule(periph, CAM_PRIORITY_DEV);
3697 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3699 struct da_softc *softc;
3700 struct cam_periph *periph;
3701 int error, error_code, sense_key, asc, ascq;
3703 periph = xpt_path_periph(ccb->ccb_h.path);
3704 softc = (struct da_softc *)periph->softc;
3707 * Automatically detect devices that do not support
3708 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
3711 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3712 error = cmd6workaround(ccb);
3713 } else if (scsi_extract_sense_ccb(ccb,
3714 &error_code, &sense_key, &asc, &ascq)) {
3715 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3716 error = cmd6workaround(ccb);
3718 * If the target replied with CAPACITY DATA HAS CHANGED UA,
3719 * query the capacity and notify upper layers.
3721 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3722 asc == 0x2A && ascq == 0x09) {
3723 xpt_print(periph->path, "Capacity data has changed\n");
3724 softc->flags &= ~DA_FLAG_PROBED;
3726 sense_flags |= SF_NO_PRINT;
3727 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3728 asc == 0x28 && ascq == 0x00) {
3729 softc->flags &= ~DA_FLAG_PROBED;
3730 disk_media_changed(softc->disk, M_NOWAIT);
3731 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3732 asc == 0x3F && ascq == 0x03) {
3733 xpt_print(periph->path, "INQUIRY data has changed\n");
3734 softc->flags &= ~DA_FLAG_PROBED;
3736 sense_flags |= SF_NO_PRINT;
3737 } else if (sense_key == SSD_KEY_NOT_READY &&
3738 asc == 0x3a && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
3739 softc->flags |= DA_FLAG_PACK_INVALID;
3740 disk_media_gone(softc->disk, M_NOWAIT);
3743 if (error == ERESTART)
3748 * Until we have a better way of doing pack validation,
3749 * don't treat UAs as errors.
3751 sense_flags |= SF_RETRY_UA;
3753 if (softc->quirks & DA_Q_RETRY_BUSY)
3754 sense_flags |= SF_RETRY_BUSY;
3755 return(cam_periph_error(ccb, cam_flags, sense_flags,
3756 &softc->saved_ccb));
3760 damediapoll(void *arg)
3762 struct cam_periph *periph = arg;
3763 struct da_softc *softc = periph->softc;
3765 if (!softc->tur && LIST_EMPTY(&softc->pending_ccbs)) {
3766 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3771 /* Queue us up again */
3772 if (da_poll_period != 0)
3773 callout_schedule(&softc->mediapoll_c, da_poll_period * hz);
3777 daprevent(struct cam_periph *periph, int action)
3779 struct da_softc *softc;
3783 softc = (struct da_softc *)periph->softc;
3785 if (((action == PR_ALLOW)
3786 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
3787 || ((action == PR_PREVENT)
3788 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
3792 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3794 scsi_prevent(&ccb->csio,
3802 error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
3803 SF_RETRY_UA | SF_NO_PRINT, softc->disk->d_devstat);
3806 if (action == PR_ALLOW)
3807 softc->flags &= ~DA_FLAG_PACK_LOCKED;
3809 softc->flags |= DA_FLAG_PACK_LOCKED;
3812 xpt_release_ccb(ccb);
3816 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
3817 struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
3819 struct ccb_calc_geometry ccg;
3820 struct da_softc *softc;
3821 struct disk_params *dp;
3822 u_int lbppbe, lalba;
3825 softc = (struct da_softc *)periph->softc;
3827 dp = &softc->params;
3828 dp->secsize = block_len;
3829 dp->sectors = maxsector + 1;
3830 if (rcaplong != NULL) {
3831 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
3832 lalba = scsi_2btoul(rcaplong->lalba_lbp);
3833 lalba &= SRC16_LALBA_A;
3840 dp->stripesize = block_len << lbppbe;
3841 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
3843 } else if (softc->quirks & DA_Q_4K) {
3844 dp->stripesize = 4096;
3845 dp->stripeoffset = 0;
3848 dp->stripeoffset = 0;
3851 * Have the controller provide us with a geometry
3852 * for this disk. The only time the geometry
3853 * matters is when we boot and the controller
3854 * is the only one knowledgeable enough to come
3855 * up with something that will make this a bootable
3858 xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3859 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
3860 ccg.block_size = dp->secsize;
3861 ccg.volume_size = dp->sectors;
3863 ccg.secs_per_track = 0;
3865 xpt_action((union ccb*)&ccg);
3866 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3868 * We don't know what went wrong here- but just pick
3869 * a geometry so we don't have nasty things like divide
3873 dp->secs_per_track = 255;
3874 dp->cylinders = dp->sectors / (255 * 255);
3875 if (dp->cylinders == 0) {
3879 dp->heads = ccg.heads;
3880 dp->secs_per_track = ccg.secs_per_track;
3881 dp->cylinders = ccg.cylinders;
3885 * If the user supplied a read capacity buffer, and if it is
3886 * different than the previous buffer, update the data in the EDT.
3887 * If it's the same, we don't bother. This avoids sending an
3888 * update every time someone opens this device.
3890 if ((rcaplong != NULL)
3891 && (bcmp(rcaplong, &softc->rcaplong,
3892 min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
3893 struct ccb_dev_advinfo cdai;
3895 xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3896 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
3897 cdai.buftype = CDAI_TYPE_RCAPLONG;
3898 cdai.flags = CDAI_FLAG_STORE;
3899 cdai.bufsiz = rcap_len;
3900 cdai.buf = (uint8_t *)rcaplong;
3901 xpt_action((union ccb *)&cdai);
3902 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
3903 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
3904 if (cdai.ccb_h.status != CAM_REQ_CMP) {
3905 xpt_print(periph->path, "%s: failed to set read "
3906 "capacity advinfo\n", __func__);
3907 /* Use cam_error_print() to decode the status */
3908 cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
3911 bcopy(rcaplong, &softc->rcaplong,
3912 min(sizeof(softc->rcaplong), rcap_len));
3916 softc->disk->d_sectorsize = softc->params.secsize;
3917 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
3918 softc->disk->d_stripesize = softc->params.stripesize;
3919 softc->disk->d_stripeoffset = softc->params.stripeoffset;
3920 /* XXX: these are not actually "firmware" values, so they may be wrong */
3921 softc->disk->d_fwsectors = softc->params.secs_per_track;
3922 softc->disk->d_fwheads = softc->params.heads;
3923 softc->disk->d_devstat->block_size = softc->params.secsize;
3924 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
3926 error = disk_resize(softc->disk, M_NOWAIT);
3928 xpt_print(periph->path, "disk_resize(9) failed, error = %d\n", error);
3932 dasendorderedtag(void *arg)
3934 struct da_softc *softc = arg;
3936 if (da_send_ordered) {
3937 if (!LIST_EMPTY(&softc->pending_ccbs)) {
3938 if ((softc->flags & DA_FLAG_WAS_OTAG) == 0)
3939 softc->flags |= DA_FLAG_NEED_OTAG;
3940 softc->flags &= ~DA_FLAG_WAS_OTAG;
3943 /* Queue us up again */
3944 callout_reset(&softc->sendordered_c,
3945 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
3946 dasendorderedtag, softc);
3950 * Step through all DA peripheral drivers, and if the device is still open,
3951 * sync the disk cache to physical media.
3954 dashutdown(void * arg, int howto)
3956 struct cam_periph *periph;
3957 struct da_softc *softc;
3961 CAM_PERIPH_FOREACH(periph, &dadriver) {
3962 softc = (struct da_softc *)periph->softc;
3963 if (SCHEDULER_STOPPED()) {
3964 /* If we paniced with the lock held, do not recurse. */
3965 if (!cam_periph_owned(periph) &&
3966 (softc->flags & DA_FLAG_OPEN)) {
3967 dadump(softc->disk, NULL, 0, 0, 0);
3971 cam_periph_lock(periph);
3974 * We only sync the cache if the drive is still open, and
3975 * if the drive is capable of it..
3977 if (((softc->flags & DA_FLAG_OPEN) == 0)
3978 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
3979 cam_periph_unlock(periph);
3983 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3984 scsi_synchronize_cache(&ccb->csio,
3988 /*begin_lba*/0, /* whole disk */
3993 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
3994 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR,
3995 softc->disk->d_devstat);
3997 xpt_print(periph->path, "Synchronize cache failed\n");
3998 xpt_release_ccb(ccb);
3999 cam_periph_unlock(periph);
4003 #else /* !_KERNEL */
4006 * XXX These are only left out of the kernel build to silence warnings. If,
4007 * for some reason these functions are used in the kernel, the ifdefs should
4008 * be moved so they are included both in the kernel and userland.
4011 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
4012 void (*cbfcnp)(struct cam_periph *, union ccb *),
4013 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
4014 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4017 struct scsi_format_unit *scsi_cmd;
4019 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
4020 scsi_cmd->opcode = FORMAT_UNIT;
4021 scsi_cmd->byte2 = byte2;
4022 scsi_ulto2b(ileave, scsi_cmd->interleave);
4027 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
4037 scsi_read_defects(struct ccb_scsiio *csio, uint32_t retries,
4038 void (*cbfcnp)(struct cam_periph *, union ccb *),
4039 uint8_t tag_action, uint8_t list_format,
4040 uint32_t addr_desc_index, uint8_t *data_ptr,
4041 uint32_t dxfer_len, int minimum_cmd_size,
4042 uint8_t sense_len, uint32_t timeout)
4047 * These conditions allow using the 10 byte command. Otherwise we
4048 * need to use the 12 byte command.
4050 if ((minimum_cmd_size <= 10)
4051 && (addr_desc_index == 0)
4052 && (dxfer_len <= SRDD10_MAX_LENGTH)) {
4053 struct scsi_read_defect_data_10 *cdb10;
4055 cdb10 = (struct scsi_read_defect_data_10 *)
4056 &csio->cdb_io.cdb_bytes;
4058 cdb_len = sizeof(*cdb10);
4059 bzero(cdb10, cdb_len);
4060 cdb10->opcode = READ_DEFECT_DATA_10;
4061 cdb10->format = list_format;
4062 scsi_ulto2b(dxfer_len, cdb10->alloc_length);
4064 struct scsi_read_defect_data_12 *cdb12;
4066 cdb12 = (struct scsi_read_defect_data_12 *)
4067 &csio->cdb_io.cdb_bytes;
4069 cdb_len = sizeof(*cdb12);
4070 bzero(cdb12, cdb_len);
4071 cdb12->opcode = READ_DEFECT_DATA_12;
4072 cdb12->format = list_format;
4073 scsi_ulto4b(dxfer_len, cdb12->alloc_length);
4074 scsi_ulto4b(addr_desc_index, cdb12->address_descriptor_index);
4080 /*flags*/ CAM_DIR_IN,
4090 scsi_sanitize(struct ccb_scsiio *csio, u_int32_t retries,
4091 void (*cbfcnp)(struct cam_periph *, union ccb *),
4092 u_int8_t tag_action, u_int8_t byte2, u_int16_t control,
4093 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4096 struct scsi_sanitize *scsi_cmd;
4098 scsi_cmd = (struct scsi_sanitize *)&csio->cdb_io.cdb_bytes;
4099 scsi_cmd->opcode = SANITIZE;
4100 scsi_cmd->byte2 = byte2;
4101 scsi_cmd->control = control;
4102 scsi_ulto2b(dxfer_len, scsi_cmd->length);
4107 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
4116 #endif /* _KERNEL */