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>
48 #include <geom/geom.h>
49 #include <geom/geom_disk.h>
58 #include <cam/cam_ccb.h>
59 #include <cam/cam_periph.h>
60 #include <cam/cam_xpt_periph.h>
61 #include <cam/cam_sim.h>
63 #include <cam/scsi/scsi_message.h>
66 #include <cam/scsi/scsi_da.h>
74 DA_STATE_PROBE_BLK_LIMITS,
81 DA_FLAG_PACK_INVALID = 0x001,
82 DA_FLAG_NEW_PACK = 0x002,
83 DA_FLAG_PACK_LOCKED = 0x004,
84 DA_FLAG_PACK_REMOVABLE = 0x008,
85 DA_FLAG_SAW_MEDIA = 0x010,
86 DA_FLAG_NEED_OTAG = 0x020,
87 DA_FLAG_WENT_IDLE = 0x040,
88 DA_FLAG_RETRY_UA = 0x080,
90 DA_FLAG_SCTX_INIT = 0x200,
91 DA_FLAG_CAN_RC16 = 0x400,
92 DA_FLAG_PROBED = 0x800
97 DA_Q_NO_SYNC_CACHE = 0x01,
98 DA_Q_NO_6_BYTE = 0x02,
99 DA_Q_NO_PREVENT = 0x04,
103 #define DA_Q_BIT_STRING \
105 "\001NO_SYNC_CACHE" \
111 DA_CCB_PROBE_RC = 0x01,
112 DA_CCB_PROBE_RC16 = 0x02,
113 DA_CCB_PROBE_LBP = 0x03,
114 DA_CCB_PROBE_BLK_LIMITS = 0x04,
115 DA_CCB_PROBE_BDC = 0x05,
116 DA_CCB_PROBE_ATA = 0x06,
117 DA_CCB_BUFFER_IO = 0x07,
118 DA_CCB_WAITING = 0x08,
120 DA_CCB_DELETE = 0x0B,
122 DA_CCB_TYPE_MASK = 0x0F,
123 DA_CCB_RETRY_UA = 0x10
127 * Order here is important for method choice
129 * We prefer ATA_TRIM as tests run against a Sandforce 2281 SSD attached to
130 * LSI 2008 (mps) controller (FW: v12, Drv: v14) resulted 20% quicker deletes
131 * using ATA_TRIM than the corresponding UNMAP results for a real world mysql
132 * import taking 5mins.
143 DA_DELETE_MIN = DA_DELETE_ATA_TRIM,
144 DA_DELETE_MAX = DA_DELETE_ZERO
147 static const char *da_delete_method_names[] =
148 { "NONE", "DISABLE", "ATA_TRIM", "UNMAP", "WS16", "WS10", "ZERO" };
149 static const char *da_delete_method_desc[] =
150 { "NONE", "DISABLED", "ATA TRIM", "UNMAP", "WRITE SAME(16) with UNMAP",
151 "WRITE SAME(10) with UNMAP", "ZERO" };
153 /* Offsets into our private area for storing information */
154 #define ccb_state ppriv_field0
155 #define ccb_bp ppriv_ptr1
160 u_int8_t secs_per_track;
161 u_int32_t secsize; /* Number of bytes/sector */
162 u_int64_t sectors; /* total number sectors */
167 #define UNMAP_RANGE_MAX 0xffffffff
168 #define UNMAP_HEAD_SIZE 8
169 #define UNMAP_RANGE_SIZE 16
170 #define UNMAP_MAX_RANGES 2048 /* Protocol Max is 4095 */
171 #define UNMAP_BUF_SIZE ((UNMAP_MAX_RANGES * UNMAP_RANGE_SIZE) + \
174 #define WS10_MAX_BLKS 0xffff
175 #define WS16_MAX_BLKS 0xffffffff
176 #define ATA_TRIM_MAX_RANGES ((UNMAP_BUF_SIZE / \
177 (ATA_DSM_RANGE_SIZE * ATA_DSM_BLK_SIZE)) * ATA_DSM_BLK_SIZE)
180 struct bio_queue_head bio_queue;
181 struct bio_queue_head delete_queue;
182 struct bio_queue_head delete_run_queue;
183 SLIST_ENTRY(da_softc) links;
184 LIST_HEAD(, ccb_hdr) pending_ccbs;
189 int minimum_cmd_size;
191 int ordered_tag_count;
192 int outstanding_cmds;
196 int delete_available; /* Delete methods possibly available */
197 uint32_t unmap_max_ranges;
198 uint32_t unmap_max_lba;
199 uint64_t ws_max_blks;
200 da_delete_methods delete_method;
201 struct disk_params params;
204 struct task sysctl_task;
205 struct sysctl_ctx_list sysctl_ctx;
206 struct sysctl_oid *sysctl_tree;
207 struct callout sendordered_c;
209 uint8_t unmap_buf[UNMAP_BUF_SIZE];
210 struct scsi_read_capacity_data_long rcaplong;
211 struct callout mediapoll_c;
214 #define dadeleteflag(softc, delete_method, enable) \
216 softc->delete_available |= (1 << delete_method); \
218 softc->delete_available &= ~(1 << delete_method); \
221 struct da_quirk_entry {
222 struct scsi_inquiry_pattern inq_pat;
226 static const char quantum[] = "QUANTUM";
227 static const char microp[] = "MICROP";
229 static struct da_quirk_entry da_quirk_table[] =
231 /* SPI, FC devices */
234 * Fujitsu M2513A MO drives.
235 * Tested devices: M2513A2 firmware versions 1200 & 1300.
236 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
237 * Reported by: W.Scholten <whs@xs4all.nl>
239 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
240 /*quirks*/ DA_Q_NO_SYNC_CACHE
244 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
245 /*quirks*/ DA_Q_NO_SYNC_CACHE
249 * This particular Fujitsu drive doesn't like the
250 * synchronize cache command.
251 * Reported by: Tom Jackson <toj@gorilla.net>
253 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
254 /*quirks*/ DA_Q_NO_SYNC_CACHE
258 * This drive doesn't like the synchronize cache command
259 * either. Reported by: Matthew Jacob <mjacob@feral.com>
260 * in NetBSD PR kern/6027, August 24, 1998.
262 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
263 /*quirks*/ DA_Q_NO_SYNC_CACHE
267 * This drive doesn't like the synchronize cache command
268 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
271 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
272 /*quirks*/ DA_Q_NO_SYNC_CACHE
276 * Doesn't like the synchronize cache command.
277 * Reported by: Blaz Zupan <blaz@gold.amis.net>
279 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
280 /*quirks*/ DA_Q_NO_SYNC_CACHE
284 * Doesn't like the synchronize cache command.
285 * Reported by: Blaz Zupan <blaz@gold.amis.net>
287 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
288 /*quirks*/ DA_Q_NO_SYNC_CACHE
292 * Doesn't like the synchronize cache command.
294 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
295 /*quirks*/ DA_Q_NO_SYNC_CACHE
299 * Doesn't like the synchronize cache command.
300 * Reported by: walter@pelissero.de
302 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
303 /*quirks*/ DA_Q_NO_SYNC_CACHE
307 * Doesn't work correctly with 6 byte reads/writes.
308 * Returns illegal request, and points to byte 9 of the
310 * Reported by: Adam McDougall <bsdx@spawnet.com>
312 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
313 /*quirks*/ DA_Q_NO_6_BYTE
317 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
318 /*quirks*/ DA_Q_NO_6_BYTE
322 * Doesn't like the synchronize cache command.
323 * Reported by: walter@pelissero.de
325 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
326 /*quirks*/ DA_Q_NO_SYNC_CACHE
330 * The CISS RAID controllers do not support SYNC_CACHE
332 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
333 /*quirks*/ DA_Q_NO_SYNC_CACHE
335 /* USB mass storage devices supported by umass(4) */
338 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
341 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
342 /*quirks*/ DA_Q_NO_SYNC_CACHE
346 * Power Quotient Int. (PQI) USB flash key
349 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
350 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
354 * Creative Nomad MUVO mp3 player (USB)
357 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
358 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
362 * Jungsoft NEXDISK USB flash key
365 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
366 /*quirks*/ DA_Q_NO_SYNC_CACHE
370 * FreeDik USB Mini Data Drive
373 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
374 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
378 * Sigmatel USB Flash MP3 Player
381 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
382 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
386 * Neuros USB Digital Audio Computer
389 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
390 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
394 * SEAGRAND NP-900 MP3 Player
397 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
398 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
402 * iRiver iFP MP3 player (with UMS Firmware)
403 * PR: kern/54881, i386/63941, kern/66124
405 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
406 /*quirks*/ DA_Q_NO_SYNC_CACHE
410 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
413 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
414 /*quirks*/ DA_Q_NO_SYNC_CACHE
418 * ZICPlay USB MP3 Player with FM
421 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
422 /*quirks*/ DA_Q_NO_SYNC_CACHE
426 * TEAC USB floppy mechanisms
428 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
429 /*quirks*/ DA_Q_NO_SYNC_CACHE
433 * Kingston DataTraveler II+ USB Pen-Drive.
434 * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
436 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
437 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
445 {T_DIRECT, SIP_MEDIA_REMOVABLE, " ", "USB DISK Pro", "PMAP"},
446 /*quirks*/ DA_Q_NO_SYNC_CACHE
450 * Motorola E398 Mobile Phone (TransFlash memory card).
451 * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
454 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
455 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
459 * Qware BeatZkey! Pro
462 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
463 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
467 * Time DPA20B 1GB MP3 Player
470 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
471 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
475 * Samsung USB key 128Mb
478 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
479 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
483 * Kingston DataTraveler 2.0 USB Flash memory.
486 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
487 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
491 * Creative MUVO Slim mp3 player (USB)
494 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
495 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
499 * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
502 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
503 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
507 * SanDisk Micro Cruzer 128MB
510 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
511 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
515 * TOSHIBA TransMemory USB sticks
518 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
519 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
524 * PR: usb/75578, usb/72344, usb/65436
526 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
527 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
531 * Genesys 6-in-1 Card Reader
534 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
535 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
539 * Rekam Digital CAMERA
542 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
543 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
547 * iRiver H10 MP3 player
550 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
551 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
555 * iRiver U10 MP3 player
558 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
559 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
566 {T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
567 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
571 * EasyMP3 EM732X USB 2.0 Flash MP3 Player
574 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
575 "1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
582 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
583 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
587 * Philips USB Key Audio KEY013
590 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
591 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
598 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
599 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
606 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
607 /*quirks*/ DA_Q_NO_SYNC_CACHE
611 * I/O Magic USB flash - Giga Bank
614 {T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
615 /*quirks*/ DA_Q_NO_SYNC_CACHE
619 * JoyFly 128mb USB Flash Drive
622 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
623 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
630 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
631 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
635 * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A
638 {T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*",
639 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
643 * Samsung YP-U3 mp3-player
646 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3",
647 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
650 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
651 "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
655 * Sony Cyber-Shot DSC cameras
658 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
659 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
662 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler G3",
663 "1.00"}, /*quirks*/ DA_Q_NO_PREVENT
665 /* ATA/SATA devices over SAS/USB/... */
667 /* Hitachi Advanced Format (4k) drives */
668 { T_DIRECT, SIP_MEDIA_FIXED, "Hitachi", "H??????????E3*", "*" },
672 /* Samsung Advanced Format (4k) drives */
673 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD155UI*", "*" },
677 /* Samsung Advanced Format (4k) drives */
678 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD155UI*", "*" },
682 /* Samsung Advanced Format (4k) drives */
683 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD204UI*", "*" },
687 /* Samsung Advanced Format (4k) drives */
688 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD204UI*", "*" },
692 /* Seagate Barracuda Green Advanced Format (4k) drives */
693 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DL*", "*" },
697 /* Seagate Barracuda Green Advanced Format (4k) drives */
698 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DL", "*", "*" },
702 /* Seagate Barracuda Green Advanced Format (4k) drives */
703 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???DM*", "*" },
707 /* Seagate Barracuda Green Advanced Format (4k) drives */
708 { T_DIRECT, SIP_MEDIA_FIXED, "ST???DM*", "*", "*" },
712 /* Seagate Barracuda Green Advanced Format (4k) drives */
713 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DM*", "*" },
717 /* Seagate Barracuda Green Advanced Format (4k) drives */
718 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DM", "*", "*" },
722 /* Seagate Momentus Advanced Format (4k) drives */
723 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500423AS*", "*" },
727 /* Seagate Momentus Advanced Format (4k) drives */
728 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "3AS*", "*" },
732 /* Seagate Momentus Advanced Format (4k) drives */
733 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500424AS*", "*" },
737 /* Seagate Momentus Advanced Format (4k) drives */
738 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "4AS*", "*" },
742 /* Seagate Momentus Advanced Format (4k) drives */
743 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640423AS*", "*" },
747 /* Seagate Momentus Advanced Format (4k) drives */
748 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "3AS*", "*" },
752 /* Seagate Momentus Advanced Format (4k) drives */
753 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640424AS*", "*" },
757 /* Seagate Momentus Advanced Format (4k) drives */
758 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "4AS*", "*" },
762 /* Seagate Momentus Advanced Format (4k) drives */
763 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750420AS*", "*" },
767 /* Seagate Momentus Advanced Format (4k) drives */
768 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "0AS*", "*" },
772 /* Seagate Momentus Advanced Format (4k) drives */
773 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750422AS*", "*" },
777 /* Seagate Momentus Advanced Format (4k) drives */
778 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "2AS*", "*" },
782 /* Seagate Momentus Advanced Format (4k) drives */
783 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750423AS*", "*" },
787 /* Seagate Momentus Advanced Format (4k) drives */
788 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "3AS*", "*" },
792 /* Seagate Momentus Thin Advanced Format (4k) drives */
793 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???LT*", "*" },
797 /* Seagate Momentus Thin Advanced Format (4k) drives */
798 { T_DIRECT, SIP_MEDIA_FIXED, "ST???LT*", "*", "*" },
802 /* WDC Caviar Green Advanced Format (4k) drives */
803 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RS*", "*" },
807 /* WDC Caviar Green Advanced Format (4k) drives */
808 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RS*", "*" },
812 /* WDC Caviar Green Advanced Format (4k) drives */
813 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RX*", "*" },
817 /* WDC Caviar Green Advanced Format (4k) drives */
818 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RX*", "*" },
822 /* WDC Caviar Green Advanced Format (4k) drives */
823 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RS*", "*" },
827 /* WDC Caviar Green Advanced Format (4k) drives */
828 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RS*", "*" },
832 /* WDC Caviar Green Advanced Format (4k) drives */
833 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RX*", "*" },
837 /* WDC Caviar Green Advanced Format (4k) drives */
838 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RX*", "*" },
842 /* WDC Scorpio Black Advanced Format (4k) drives */
843 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PKT*", "*" },
847 /* WDC Scorpio Black Advanced Format (4k) drives */
848 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PKT*", "*" },
852 /* WDC Scorpio Black Advanced Format (4k) drives */
853 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PKT*", "*" },
857 /* WDC Scorpio Black Advanced Format (4k) drives */
858 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PKT*", "*" },
862 /* WDC Scorpio Blue Advanced Format (4k) drives */
863 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PVT*", "*" },
867 /* WDC Scorpio Blue Advanced Format (4k) drives */
868 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PVT*", "*" },
872 /* WDC Scorpio Blue Advanced Format (4k) drives */
873 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PVT*", "*" },
877 /* WDC Scorpio Blue Advanced Format (4k) drives */
878 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PVT*", "*" },
883 * Olympus FE-210 camera
885 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "FE210*",
886 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
892 {T_DIRECT, SIP_MEDIA_REMOVABLE, "LG", "UP3S",
893 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
897 * Laser MP3-2GA13 MP3 player
899 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "(HS) Flash Disk",
900 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
904 * LaCie external 250GB Hard drive des by Porsche
905 * Submitted by: Ben Stuyts <ben@altesco.nl>
908 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HM250JI", "*"},
909 /*quirks*/ DA_Q_NO_SYNC_CACHE
913 static disk_strategy_t dastrategy;
914 static dumper_t dadump;
915 static periph_init_t dainit;
916 static void daasync(void *callback_arg, u_int32_t code,
917 struct cam_path *path, void *arg);
918 static void dasysctlinit(void *context, int pending);
919 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
920 static int dadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
921 static void dadeletemethodset(struct da_softc *softc,
922 da_delete_methods delete_method);
923 static void dadeletemethodchoose(struct da_softc *softc,
924 da_delete_methods default_method);
925 static void daprobedone(struct cam_periph *periph, union ccb *ccb);
927 static periph_ctor_t daregister;
928 static periph_dtor_t dacleanup;
929 static periph_start_t dastart;
930 static periph_oninv_t daoninvalidate;
931 static void dadone(struct cam_periph *periph,
932 union ccb *done_ccb);
933 static int daerror(union ccb *ccb, u_int32_t cam_flags,
934 u_int32_t sense_flags);
935 static void daprevent(struct cam_periph *periph, int action);
936 static void dareprobe(struct cam_periph *periph);
937 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
939 struct scsi_read_capacity_data_long *rcaplong,
941 static timeout_t dasendorderedtag;
942 static void dashutdown(void *arg, int howto);
943 static timeout_t damediapoll;
945 #ifndef DA_DEFAULT_POLL_PERIOD
946 #define DA_DEFAULT_POLL_PERIOD 3
949 #ifndef DA_DEFAULT_TIMEOUT
950 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
953 #ifndef DA_DEFAULT_RETRY
954 #define DA_DEFAULT_RETRY 4
957 #ifndef DA_DEFAULT_SEND_ORDERED
958 #define DA_DEFAULT_SEND_ORDERED 1
961 #define DA_SIO (softc->sort_io_queue >= 0 ? \
962 softc->sort_io_queue : cam_sort_io_queues)
964 static int da_poll_period = DA_DEFAULT_POLL_PERIOD;
965 static int da_retry_count = DA_DEFAULT_RETRY;
966 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
967 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
969 static SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
970 "CAM Direct Access Disk driver");
971 SYSCTL_INT(_kern_cam_da, OID_AUTO, poll_period, CTLFLAG_RW,
972 &da_poll_period, 0, "Media polling period in seconds");
973 TUNABLE_INT("kern.cam.da.poll_period", &da_poll_period);
974 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
975 &da_retry_count, 0, "Normal I/O retry count");
976 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
977 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
978 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
979 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
980 SYSCTL_INT(_kern_cam_da, OID_AUTO, send_ordered, CTLFLAG_RW,
981 &da_send_ordered, 0, "Send Ordered Tags");
982 TUNABLE_INT("kern.cam.da.send_ordered", &da_send_ordered);
985 * DA_ORDEREDTAG_INTERVAL determines how often, relative
986 * to the default timeout, we check to see whether an ordered
987 * tagged transaction is appropriate to prevent simple tag
988 * starvation. Since we'd like to ensure that there is at least
989 * 1/2 of the timeout length left for a starved transaction to
990 * complete after we've sent an ordered tag, we must poll at least
991 * four times in every timeout period. This takes care of the worst
992 * case where a starved transaction starts during an interval that
993 * meets the requirement "don't send an ordered tag" test so it takes
994 * us two intervals to determine that a tag must be sent.
996 #ifndef DA_ORDEREDTAG_INTERVAL
997 #define DA_ORDEREDTAG_INTERVAL 4
1000 static struct periph_driver dadriver =
1003 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
1006 PERIPHDRIVER_DECLARE(da, dadriver);
1008 static MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
1011 daopen(struct disk *dp)
1013 struct cam_periph *periph;
1014 struct da_softc *softc;
1018 periph = (struct cam_periph *)dp->d_drv1;
1019 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1023 cam_periph_lock(periph);
1024 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
1025 cam_periph_unlock(periph);
1026 cam_periph_release(periph);
1030 unit = periph->unit_number;
1031 softc = (struct da_softc *)periph->softc;
1032 softc->flags |= DA_FLAG_OPEN;
1034 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1037 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1038 /* Invalidate our pack information. */
1039 softc->flags &= ~DA_FLAG_PACK_INVALID;
1044 /* Wait for the disk size update. */
1045 error = cam_periph_sleep(periph, &softc->disk->d_mediasize, PRIBIO,
1048 xpt_print(periph->path, "unable to retrieve capacity data");
1050 if (periph->flags & CAM_PERIPH_INVALID ||
1051 softc->disk->d_sectorsize == 0 ||
1052 softc->disk->d_mediasize == 0)
1055 if (error == 0 && (softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1056 (softc->quirks & DA_Q_NO_PREVENT) == 0)
1057 daprevent(periph, PR_PREVENT);
1060 softc->flags |= DA_FLAG_SAW_MEDIA;
1062 cam_periph_unhold(periph);
1063 cam_periph_unlock(periph);
1066 softc->flags &= ~DA_FLAG_OPEN;
1067 cam_periph_release(periph);
1074 daclose(struct disk *dp)
1076 struct cam_periph *periph;
1077 struct da_softc *softc;
1079 periph = (struct cam_periph *)dp->d_drv1;
1080 cam_periph_lock(periph);
1081 if (cam_periph_hold(periph, PRIBIO) != 0) {
1082 cam_periph_unlock(periph);
1083 cam_periph_release(periph);
1087 softc = (struct da_softc *)periph->softc;
1089 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1092 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0
1093 && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
1096 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1098 scsi_synchronize_cache(&ccb->csio,
1102 /*begin_lba*/0,/* Cover the whole disk */
1107 cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
1108 /*sense_flags*/SF_RETRY_UA | SF_QUIET_IR,
1109 softc->disk->d_devstat);
1110 xpt_release_ccb(ccb);
1114 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
1115 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
1116 daprevent(periph, PR_ALLOW);
1118 * If we've got removeable media, mark the blocksize as
1119 * unavailable, since it could change when new media is
1122 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1125 softc->flags &= ~DA_FLAG_OPEN;
1126 cam_periph_unhold(periph);
1127 cam_periph_unlock(periph);
1128 cam_periph_release(periph);
1133 daschedule(struct cam_periph *periph)
1135 struct da_softc *softc = (struct da_softc *)periph->softc;
1138 if (softc->state != DA_STATE_NORMAL)
1141 /* Check if cam_periph_getccb() was called. */
1142 prio = periph->immediate_priority;
1144 /* Check if we have more work to do. */
1145 if (bioq_first(&softc->bio_queue) ||
1146 (!softc->delete_running && bioq_first(&softc->delete_queue)) ||
1148 prio = CAM_PRIORITY_NORMAL;
1151 /* Schedule CCB if any of above is true. */
1152 if (prio != CAM_PRIORITY_NONE)
1153 xpt_schedule(periph, prio);
1157 * Actually translate the requested transfer into one the physical driver
1158 * can understand. The transfer is described by a buf and will include
1159 * only one physical transfer.
1162 dastrategy(struct bio *bp)
1164 struct cam_periph *periph;
1165 struct da_softc *softc;
1167 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1168 softc = (struct da_softc *)periph->softc;
1170 cam_periph_lock(periph);
1173 * If the device has been made invalid, error out
1175 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1176 cam_periph_unlock(periph);
1177 biofinish(bp, NULL, ENXIO);
1181 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
1184 * Place it in the queue of disk activities for this disk
1186 if (bp->bio_cmd == BIO_DELETE) {
1187 if (bp->bio_bcount == 0)
1190 bioq_disksort(&softc->delete_queue, bp);
1192 bioq_insert_tail(&softc->delete_queue, bp);
1193 } else if (DA_SIO) {
1194 bioq_disksort(&softc->bio_queue, bp);
1196 bioq_insert_tail(&softc->bio_queue, bp);
1200 * Schedule ourselves for performing the work.
1203 cam_periph_unlock(periph);
1209 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1211 struct cam_periph *periph;
1212 struct da_softc *softc;
1214 struct ccb_scsiio csio;
1219 periph = dp->d_drv1;
1220 softc = (struct da_softc *)periph->softc;
1221 cam_periph_lock(periph);
1222 secsize = softc->params.secsize;
1224 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1225 cam_periph_unlock(periph);
1230 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1231 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1232 scsi_read_write(&csio,
1238 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1241 /*data_ptr*/(u_int8_t *) virtual,
1242 /*dxfer_len*/length,
1243 /*sense_len*/SSD_FULL_SIZE,
1244 da_default_timeout * 1000);
1245 xpt_polled_action((union ccb *)&csio);
1247 error = cam_periph_error((union ccb *)&csio,
1248 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1249 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1250 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1251 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1253 printf("Aborting dump due to I/O error.\n");
1254 cam_periph_unlock(periph);
1259 * Sync the disk cache contents to the physical media.
1261 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1263 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1264 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1265 scsi_synchronize_cache(&csio,
1269 /*begin_lba*/0,/* Cover the whole disk */
1273 xpt_polled_action((union ccb *)&csio);
1275 error = cam_periph_error((union ccb *)&csio,
1276 0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL);
1277 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1278 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1279 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1281 xpt_print(periph->path, "Synchronize cache failed\n");
1283 cam_periph_unlock(periph);
1288 dagetattr(struct bio *bp)
1291 struct cam_periph *periph;
1293 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1294 cam_periph_lock(periph);
1295 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1297 cam_periph_unlock(periph);
1299 bp->bio_completed = bp->bio_length;
1309 * Install a global async callback. This callback will
1310 * receive async callbacks like "new device found".
1312 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1314 if (status != CAM_REQ_CMP) {
1315 printf("da: Failed to attach master async callback "
1316 "due to status 0x%x!\n", status);
1317 } else if (da_send_ordered) {
1319 /* Register our shutdown event handler */
1320 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1321 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1322 printf("dainit: shutdown event registration failed!\n");
1327 * Callback from GEOM, called when it has finished cleaning up its
1331 dadiskgonecb(struct disk *dp)
1333 struct cam_periph *periph;
1335 periph = (struct cam_periph *)dp->d_drv1;
1336 cam_periph_release(periph);
1340 daoninvalidate(struct cam_periph *periph)
1342 struct da_softc *softc;
1344 softc = (struct da_softc *)periph->softc;
1347 * De-register any async callbacks.
1349 xpt_register_async(0, daasync, periph, periph->path);
1351 softc->flags |= DA_FLAG_PACK_INVALID;
1354 * Return all queued I/O with ENXIO.
1355 * XXX Handle any transactions queued to the card
1356 * with XPT_ABORT_CCB.
1358 bioq_flush(&softc->bio_queue, NULL, ENXIO);
1359 bioq_flush(&softc->delete_queue, NULL, ENXIO);
1362 * Tell GEOM that we've gone away, we'll get a callback when it is
1363 * done cleaning up its resources.
1365 disk_gone(softc->disk);
1367 xpt_print(periph->path, "lost device - %d outstanding, %d refs\n",
1368 softc->outstanding_cmds, periph->refcount);
1372 dacleanup(struct cam_periph *periph)
1374 struct da_softc *softc;
1376 softc = (struct da_softc *)periph->softc;
1378 xpt_print(periph->path, "removing device entry\n");
1379 cam_periph_unlock(periph);
1382 * If we can't free the sysctl tree, oh well...
1384 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1385 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1386 xpt_print(periph->path, "can't remove sysctl context\n");
1389 callout_drain(&softc->mediapoll_c);
1390 disk_destroy(softc->disk);
1391 callout_drain(&softc->sendordered_c);
1392 free(softc, M_DEVBUF);
1393 cam_periph_lock(periph);
1397 daasync(void *callback_arg, u_int32_t code,
1398 struct cam_path *path, void *arg)
1400 struct cam_periph *periph;
1401 struct da_softc *softc;
1403 periph = (struct cam_periph *)callback_arg;
1405 case AC_FOUND_DEVICE:
1407 struct ccb_getdev *cgd;
1410 cgd = (struct ccb_getdev *)arg;
1414 if (cgd->protocol != PROTO_SCSI)
1417 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1418 && SID_TYPE(&cgd->inq_data) != T_RBC
1419 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1423 * Allocate a peripheral instance for
1424 * this device and start the probe
1427 status = cam_periph_alloc(daregister, daoninvalidate,
1429 "da", CAM_PERIPH_BIO,
1430 cgd->ccb_h.path, daasync,
1431 AC_FOUND_DEVICE, cgd);
1433 if (status != CAM_REQ_CMP
1434 && status != CAM_REQ_INPROG)
1435 printf("daasync: Unable to attach to new device "
1436 "due to status 0x%x\n", status);
1439 case AC_ADVINFO_CHANGED:
1443 buftype = (uintptr_t)arg;
1444 if (buftype == CDAI_TYPE_PHYS_PATH) {
1445 struct da_softc *softc;
1447 softc = periph->softc;
1448 disk_attr_changed(softc->disk, "GEOM::physpath",
1453 case AC_UNIT_ATTENTION:
1456 int error_code, sense_key, asc, ascq;
1458 softc = (struct da_softc *)periph->softc;
1459 ccb = (union ccb *)arg;
1462 * Handle all UNIT ATTENTIONs except our own,
1463 * as they will be handled by daerror().
1465 if (xpt_path_periph(ccb->ccb_h.path) != periph &&
1466 scsi_extract_sense_ccb(ccb,
1467 &error_code, &sense_key, &asc, &ascq)) {
1468 if (asc == 0x2A && ascq == 0x09) {
1469 xpt_print(ccb->ccb_h.path,
1470 "capacity data has changed\n");
1472 } else if (asc == 0x28 && ascq == 0x00)
1473 disk_media_changed(softc->disk, M_NOWAIT);
1475 cam_periph_async(periph, code, path, arg);
1479 softc = (struct da_softc *)periph->softc;
1481 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
1490 struct ccb_hdr *ccbh;
1492 softc = (struct da_softc *)periph->softc;
1494 * Don't fail on the expected unit attention
1497 softc->flags |= DA_FLAG_RETRY_UA;
1498 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1499 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1505 cam_periph_async(periph, code, path, arg);
1509 dasysctlinit(void *context, int pending)
1511 struct cam_periph *periph;
1512 struct da_softc *softc;
1513 char tmpstr[80], tmpstr2[80];
1514 struct ccb_trans_settings cts;
1516 periph = (struct cam_periph *)context;
1518 * periph was held for us when this task was enqueued
1520 if (periph->flags & CAM_PERIPH_INVALID) {
1521 cam_periph_release(periph);
1525 softc = (struct da_softc *)periph->softc;
1526 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1527 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1529 sysctl_ctx_init(&softc->sysctl_ctx);
1530 softc->flags |= DA_FLAG_SCTX_INIT;
1531 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1532 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1533 CTLFLAG_RD, 0, tmpstr);
1534 if (softc->sysctl_tree == NULL) {
1535 printf("dasysctlinit: unable to allocate sysctl tree\n");
1536 cam_periph_release(periph);
1541 * Now register the sysctl handler, so the user can change the value on
1544 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1545 OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RW,
1546 softc, 0, dadeletemethodsysctl, "A",
1547 "BIO_DELETE execution method");
1548 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1549 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1550 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1551 "Minimum CDB size");
1552 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1553 OID_AUTO, "sort_io_queue", CTLFLAG_RW, &softc->sort_io_queue, 0,
1554 "Sort IO queue to try and optimise disk access patterns");
1556 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1557 SYSCTL_CHILDREN(softc->sysctl_tree),
1561 &softc->error_inject,
1563 "error_inject leaf");
1567 * Add some addressing info.
1569 memset(&cts, 0, sizeof (cts));
1570 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1571 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1572 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1573 cam_periph_lock(periph);
1574 xpt_action((union ccb *)&cts);
1575 cam_periph_unlock(periph);
1576 if (cts.ccb_h.status != CAM_REQ_CMP) {
1577 cam_periph_release(periph);
1580 if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
1581 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1582 if (fc->valid & CTS_FC_VALID_WWPN) {
1583 softc->wwpn = fc->wwpn;
1584 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1585 SYSCTL_CHILDREN(softc->sysctl_tree),
1586 OID_AUTO, "wwpn", CTLFLAG_RD,
1587 &softc->wwpn, "World Wide Port Name");
1590 cam_periph_release(periph);
1594 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1598 value = *(int *)arg1;
1600 error = sysctl_handle_int(oidp, &value, 0, req);
1603 || (req->newptr == NULL))
1607 * Acceptable values here are 6, 10, 12 or 16.
1611 else if ((value > 6)
1614 else if ((value > 10)
1617 else if (value > 12)
1620 *(int *)arg1 = value;
1626 dadeletemethodset(struct da_softc *softc, da_delete_methods delete_method)
1630 softc->delete_method = delete_method;
1632 if (softc->delete_method > DA_DELETE_DISABLE)
1633 softc->disk->d_flags |= DISKFLAG_CANDELETE;
1635 softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
1639 daprobedone(struct cam_periph *periph, union ccb *ccb)
1641 struct da_softc *softc;
1643 softc = (struct da_softc *)periph->softc;
1645 dadeletemethodchoose(softc, DA_DELETE_NONE);
1647 if (bootverbose && (softc->flags & DA_FLAG_PROBED) == 0) {
1651 snprintf(buf, sizeof(buf), "Delete methods: <");
1653 for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
1654 if (softc->delete_available & (1 << i)) {
1656 strlcat(buf, ",", sizeof(buf));
1660 strlcat(buf, da_delete_method_names[i],
1662 if (i == softc->delete_method) {
1663 strlcat(buf, "(*)", sizeof(buf));
1668 if (softc->delete_method == DA_DELETE_NONE)
1669 strlcat(buf, "NONE(*)", sizeof(buf));
1671 strlcat(buf, "DISABLED(*)", sizeof(buf));
1673 strlcat(buf, ">", sizeof(buf));
1674 printf("%s%d: %s\n", periph->periph_name,
1675 periph->unit_number, buf);
1679 * Since our peripheral may be invalidated by an error
1680 * above or an external event, we must release our CCB
1681 * before releasing the probe lock on the peripheral.
1682 * The peripheral will only go away once the last lock
1683 * is removed, and we need it around for the CCB release
1686 xpt_release_ccb(ccb);
1687 softc->state = DA_STATE_NORMAL;
1689 wakeup(&softc->disk->d_mediasize);
1690 if ((softc->flags & DA_FLAG_PROBED) == 0) {
1691 softc->flags |= DA_FLAG_PROBED;
1692 cam_periph_unhold(periph);
1694 cam_periph_release_locked(periph);
1698 dadeletemethodchoose(struct da_softc *softc, da_delete_methods default_method)
1700 int i, delete_method;
1702 delete_method = default_method;
1705 * Use the pre-defined order to choose the best
1706 * performing delete.
1708 for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
1709 if (softc->delete_available & (1 << i)) {
1710 dadeletemethodset(softc, i);
1714 dadeletemethodset(softc, delete_method);
1718 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
1722 struct da_softc *softc;
1723 int i, error, value;
1725 softc = (struct da_softc *)arg1;
1727 value = softc->delete_method;
1728 if (value < 0 || value > DA_DELETE_MAX)
1731 p = da_delete_method_names[value];
1732 strncpy(buf, p, sizeof(buf));
1733 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1734 if (error != 0 || req->newptr == NULL)
1736 for (i = 0; i <= DA_DELETE_MAX; i++) {
1737 if (!(softc->delete_available & (1 << i)) ||
1738 strcmp(buf, da_delete_method_names[i]) != 0)
1740 dadeletemethodset(softc, i);
1747 daregister(struct cam_periph *periph, void *arg)
1749 struct da_softc *softc;
1750 struct ccb_pathinq cpi;
1751 struct ccb_getdev *cgd;
1755 cgd = (struct ccb_getdev *)arg;
1757 printf("daregister: no getdev CCB, can't register device\n");
1758 return(CAM_REQ_CMP_ERR);
1761 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
1764 if (softc == NULL) {
1765 printf("daregister: Unable to probe new device. "
1766 "Unable to allocate softc\n");
1767 return(CAM_REQ_CMP_ERR);
1770 LIST_INIT(&softc->pending_ccbs);
1771 softc->state = DA_STATE_PROBE_RC;
1772 bioq_init(&softc->bio_queue);
1773 bioq_init(&softc->delete_queue);
1774 bioq_init(&softc->delete_run_queue);
1775 if (SID_IS_REMOVABLE(&cgd->inq_data))
1776 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1777 softc->unmap_max_ranges = UNMAP_MAX_RANGES;
1778 softc->unmap_max_lba = UNMAP_RANGE_MAX;
1779 softc->ws_max_blks = WS16_MAX_BLKS;
1780 softc->trim_max_ranges = ATA_TRIM_MAX_RANGES;
1781 softc->sort_io_queue = -1;
1783 periph->softc = softc;
1786 * See if this device has any quirks.
1788 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1789 (caddr_t)da_quirk_table,
1790 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1791 sizeof(*da_quirk_table), scsi_inquiry_match);
1794 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1796 softc->quirks = DA_Q_NONE;
1798 /* Check if the SIM does not want 6 byte commands */
1799 bzero(&cpi, sizeof(cpi));
1800 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1801 cpi.ccb_h.func_code = XPT_PATH_INQ;
1802 xpt_action((union ccb *)&cpi);
1803 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1804 softc->quirks |= DA_Q_NO_6_BYTE;
1806 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1809 * Take an exclusive refcount on the periph while dastart is called
1810 * to finish the probe. The reference will be dropped in dadone at
1813 (void)cam_periph_hold(periph, PRIBIO);
1816 * Schedule a periodic event to occasionally send an
1817 * ordered tag to a device.
1819 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
1820 callout_reset(&softc->sendordered_c,
1821 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
1822 dasendorderedtag, softc);
1824 cam_periph_unlock(periph);
1826 * RBC devices don't have to support READ(6), only READ(10).
1828 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1829 softc->minimum_cmd_size = 10;
1831 softc->minimum_cmd_size = 6;
1834 * Load the user's default, if any.
1836 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1837 periph->unit_number);
1838 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1841 * 6, 10, 12 and 16 are the currently permissible values.
1843 if (softc->minimum_cmd_size < 6)
1844 softc->minimum_cmd_size = 6;
1845 else if ((softc->minimum_cmd_size > 6)
1846 && (softc->minimum_cmd_size <= 10))
1847 softc->minimum_cmd_size = 10;
1848 else if ((softc->minimum_cmd_size > 10)
1849 && (softc->minimum_cmd_size <= 12))
1850 softc->minimum_cmd_size = 12;
1851 else if (softc->minimum_cmd_size > 12)
1852 softc->minimum_cmd_size = 16;
1854 /* Predict whether device may support READ CAPACITY(16). */
1855 if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3) {
1856 softc->flags |= DA_FLAG_CAN_RC16;
1857 softc->state = DA_STATE_PROBE_RC16;
1861 * Register this media as a disk.
1863 softc->disk = disk_alloc();
1864 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
1865 periph->unit_number, 0,
1866 DEVSTAT_BS_UNAVAILABLE,
1867 SID_TYPE(&cgd->inq_data) |
1868 XPORT_DEVSTAT_TYPE(cpi.transport),
1869 DEVSTAT_PRIORITY_DISK);
1870 softc->disk->d_open = daopen;
1871 softc->disk->d_close = daclose;
1872 softc->disk->d_strategy = dastrategy;
1873 softc->disk->d_dump = dadump;
1874 softc->disk->d_getattr = dagetattr;
1875 softc->disk->d_gone = dadiskgonecb;
1876 softc->disk->d_name = "da";
1877 softc->disk->d_drv1 = periph;
1879 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */
1880 else if (cpi.maxio > MAXPHYS)
1881 softc->disk->d_maxsize = MAXPHYS; /* for safety */
1883 softc->disk->d_maxsize = cpi.maxio;
1884 softc->disk->d_unit = periph->unit_number;
1885 softc->disk->d_flags = 0;
1886 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
1887 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
1888 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
1889 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
1890 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
1891 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
1892 cgd->inq_data.product, sizeof(cgd->inq_data.product),
1893 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
1894 softc->disk->d_hba_vendor = cpi.hba_vendor;
1895 softc->disk->d_hba_device = cpi.hba_device;
1896 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
1897 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
1900 * Acquire a reference to the periph before we register with GEOM.
1901 * We'll release this reference once GEOM calls us back (via
1902 * dadiskgonecb()) telling us that our provider has been freed.
1904 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1905 xpt_print(periph->path, "%s: lost periph during "
1906 "registration!\n", __func__);
1907 cam_periph_lock(periph);
1908 return (CAM_REQ_CMP_ERR);
1911 disk_create(softc->disk, DISK_VERSION);
1912 cam_periph_lock(periph);
1915 * Add async callbacks for events of interest.
1916 * I don't bother checking if this fails as,
1917 * in most cases, the system will function just
1918 * fine without them and the only alternative
1919 * would be to not attach the device on failure.
1921 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
1922 AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION,
1923 daasync, periph, periph->path);
1926 * Emit an attribute changed notification just in case
1927 * physical path information arrived before our async
1928 * event handler was registered, but after anyone attaching
1929 * to our disk device polled it.
1931 disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
1934 * Schedule a periodic media polling events.
1936 callout_init_mtx(&softc->mediapoll_c, periph->sim->mtx, 0);
1937 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
1938 (cgd->inq_flags & SID_AEN) == 0 &&
1939 da_poll_period != 0)
1940 callout_reset(&softc->mediapoll_c, da_poll_period * hz,
1941 damediapoll, periph);
1943 xpt_schedule(periph, CAM_PRIORITY_DEV);
1945 return(CAM_REQ_CMP);
1949 dastart(struct cam_periph *periph, union ccb *start_ccb)
1951 struct da_softc *softc;
1953 softc = (struct da_softc *)periph->softc;
1955 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
1958 switch (softc->state) {
1959 case DA_STATE_NORMAL:
1961 struct bio *bp, *bp1;
1964 /* Execute immediate CCB if waiting. */
1965 if (periph->immediate_priority <= periph->pinfo.priority) {
1966 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1967 ("queuing for immediate ccb\n"));
1968 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1969 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1971 periph->immediate_priority = CAM_PRIORITY_NONE;
1972 wakeup(&periph->ccb_list);
1973 /* May have more work to do, so ensure we stay scheduled */
1978 /* Run BIO_DELETE if not running yet. */
1979 if (!softc->delete_running &&
1980 (bp = bioq_first(&softc->delete_queue)) != NULL) {
1982 uint64_t count; /* forward compat with WS32 */
1985 * In each of the methods below, while its the caller's
1986 * responsibility to ensure the request will fit into a
1987 * single device request, we might have changed the delete
1988 * method due to the device incorrectly advertising either
1989 * its supported methods or limits.
1991 * To prevent this causing further issues we validate the
1992 * against the methods limits, and warn which would
1993 * otherwise be unnecessary.
1996 if (softc->delete_method == DA_DELETE_UNMAP) {
1997 uint8_t *buf = softc->unmap_buf;
1998 uint64_t lastlba = (uint64_t)-1;
1999 uint32_t lastcount = 0, c;
2000 uint64_t totalcount = 0;
2001 uint32_t off, ranges = 0;
2004 * Currently this doesn't take the UNMAP
2005 * Granularity and Granularity Alignment
2006 * fields into account.
2008 * This could result in both unoptimal unmap
2009 * requests as as well as UNMAP calls unmapping
2010 * fewer LBA's than requested.
2013 softc->delete_running = 1;
2014 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2017 bioq_remove(&softc->delete_queue, bp1);
2019 bioq_insert_tail(&softc->delete_run_queue, bp1);
2020 lba = bp1->bio_pblkno;
2021 count = bp1->bio_bcount / softc->params.secsize;
2023 /* Try to extend the previous range. */
2024 if (lba == lastlba) {
2025 c = min(count, softc->unmap_max_lba -
2028 off = ((ranges - 1) * UNMAP_RANGE_SIZE) +
2030 scsi_ulto4b(lastcount, &buf[off + 8]);
2037 c = min(count, softc->unmap_max_lba);
2038 if (totalcount + c > softc->unmap_max_lba ||
2039 ranges >= softc->unmap_max_ranges) {
2040 xpt_print(periph->path,
2041 "%s issuing short delete %ld > %ld"
2043 da_delete_method_desc[softc->delete_method],
2044 totalcount + c, softc->unmap_max_lba,
2045 ranges, softc->unmap_max_ranges);
2048 off = (ranges * UNMAP_RANGE_SIZE) +
2050 scsi_u64to8b(lba, &buf[off + 0]);
2051 scsi_ulto4b(c, &buf[off + 8]);
2059 bp1 = bioq_first(&softc->delete_queue);
2061 ranges >= softc->unmap_max_ranges ||
2062 totalcount + bp1->bio_bcount /
2063 softc->params.secsize > softc->unmap_max_lba)
2066 scsi_ulto2b(ranges * 16 + 6, &buf[0]);
2067 scsi_ulto2b(ranges * 16, &buf[2]);
2069 scsi_unmap(&start_ccb->csio,
2070 /*retries*/da_retry_count,
2072 /*tag_action*/MSG_SIMPLE_Q_TAG,
2075 /*dxfer_len*/ ranges * 16 + 8,
2076 /*sense_len*/SSD_FULL_SIZE,
2077 da_default_timeout * 1000);
2078 start_ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2080 } else if (softc->delete_method == DA_DELETE_ATA_TRIM) {
2081 uint8_t *buf = softc->unmap_buf;
2082 uint64_t lastlba = (uint64_t)-1;
2083 uint32_t lastcount = 0, c, requestcount;
2084 int ranges = 0, off, block_count;
2086 softc->delete_running = 1;
2087 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2090 bioq_remove(&softc->delete_queue, bp1);
2092 bioq_insert_tail(&softc->delete_run_queue, bp1);
2093 lba = bp1->bio_pblkno;
2094 count = bp1->bio_bcount / softc->params.secsize;
2095 requestcount = count;
2097 /* Try to extend the previous range. */
2098 if (lba == lastlba) {
2099 c = min(count, ATA_DSM_RANGE_MAX - lastcount);
2101 off = (ranges - 1) * 8;
2102 buf[off + 6] = lastcount & 0xff;
2103 buf[off + 7] = (lastcount >> 8) & 0xff;
2109 c = min(count, ATA_DSM_RANGE_MAX);
2112 buf[off + 0] = lba & 0xff;
2113 buf[off + 1] = (lba >> 8) & 0xff;
2114 buf[off + 2] = (lba >> 16) & 0xff;
2115 buf[off + 3] = (lba >> 24) & 0xff;
2116 buf[off + 4] = (lba >> 32) & 0xff;
2117 buf[off + 5] = (lba >> 40) & 0xff;
2118 buf[off + 6] = c & 0xff;
2119 buf[off + 7] = (c >> 8) & 0xff;
2124 if (count != 0 && ranges == softc->trim_max_ranges) {
2125 xpt_print(periph->path,
2126 "%s issuing short delete %ld > %ld",
2127 da_delete_method_desc[softc->delete_method],
2129 (softc->trim_max_ranges - ranges) *
2135 bp1 = bioq_first(&softc->delete_queue);
2137 bp1->bio_bcount / softc->params.secsize >
2138 (softc->trim_max_ranges - ranges) *
2143 block_count = (ranges + ATA_DSM_BLK_RANGES - 1) /
2145 scsi_ata_trim(&start_ccb->csio,
2146 /*retries*/da_retry_count,
2148 /*tag_action*/MSG_SIMPLE_Q_TAG,
2151 /*dxfer_len*/block_count * ATA_DSM_BLK_SIZE,
2152 /*sense_len*/SSD_FULL_SIZE,
2153 da_default_timeout * 1000);
2154 start_ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2156 } else if (softc->delete_method == DA_DELETE_ZERO ||
2157 softc->delete_method == DA_DELETE_WS10 ||
2158 softc->delete_method == DA_DELETE_WS16) {
2159 uint64_t ws_max_blks;
2160 ws_max_blks = softc->ws_max_blks / softc->params.secsize;
2161 softc->delete_running = 1;
2162 lba = bp->bio_pblkno;
2166 bioq_remove(&softc->delete_queue, bp1);
2168 bioq_insert_tail(&softc->delete_run_queue, bp1);
2169 count += bp1->bio_bcount / softc->params.secsize;
2170 if (count > ws_max_blks) {
2171 count = min(count, ws_max_blks);
2172 xpt_print(periph->path,
2173 "%s issuing short delete %ld > %ld",
2174 da_delete_method_desc[softc->delete_method],
2175 count, ws_max_blks);
2178 bp1 = bioq_first(&softc->delete_queue);
2180 lba + count != bp1->bio_pblkno ||
2181 count + bp1->bio_bcount /
2182 softc->params.secsize > ws_max_blks)
2186 scsi_write_same(&start_ccb->csio,
2187 /*retries*/da_retry_count,
2189 /*tag_action*/MSG_SIMPLE_Q_TAG,
2190 /*byte2*/softc->delete_method ==
2191 DA_DELETE_ZERO ? 0 : SWS_UNMAP,
2192 softc->delete_method ==
2193 DA_DELETE_WS16 ? 16 : 10,
2195 /*block_count*/count,
2196 /*data_ptr*/ __DECONST(void *,
2198 /*dxfer_len*/ softc->params.secsize,
2199 /*sense_len*/SSD_FULL_SIZE,
2200 da_default_timeout * 1000);
2201 start_ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2204 bioq_flush(&softc->delete_queue, NULL, 0);
2209 /* Run regular command. */
2210 bp = bioq_takefirst(&softc->bio_queue);
2214 scsi_test_unit_ready(&start_ccb->csio,
2215 /*retries*/ da_retry_count,
2219 da_default_timeout * 1000);
2220 start_ccb->ccb_h.ccb_bp = NULL;
2221 start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
2222 xpt_action(start_ccb);
2224 xpt_release_ccb(start_ccb);
2229 cam_periph_release_locked(periph);
2232 if ((bp->bio_flags & BIO_ORDERED) != 0 ||
2233 (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
2234 softc->flags &= ~DA_FLAG_NEED_OTAG;
2235 softc->ordered_tag_count++;
2236 tag_code = MSG_ORDERED_Q_TAG;
2238 tag_code = MSG_SIMPLE_Q_TAG;
2241 switch (bp->bio_cmd) {
2244 scsi_read_write(&start_ccb->csio,
2245 /*retries*/da_retry_count,
2247 /*tag_action*/tag_code,
2248 /*read_op*/bp->bio_cmd
2251 softc->minimum_cmd_size,
2252 /*lba*/bp->bio_pblkno,
2253 /*block_count*/bp->bio_bcount /
2254 softc->params.secsize,
2255 /*data_ptr*/ bp->bio_data,
2256 /*dxfer_len*/ bp->bio_bcount,
2257 /*sense_len*/SSD_FULL_SIZE,
2258 da_default_timeout * 1000);
2262 * BIO_FLUSH doesn't currently communicate
2263 * range data, so we synchronize the cache
2264 * over the whole disk. We also force
2265 * ordered tag semantics the flush applies
2266 * to all previously queued I/O.
2268 scsi_synchronize_cache(&start_ccb->csio,
2275 da_default_timeout*1000);
2278 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
2282 * Block out any asyncronous callbacks
2283 * while we touch the pending ccb list.
2285 LIST_INSERT_HEAD(&softc->pending_ccbs,
2286 &start_ccb->ccb_h, periph_links.le);
2287 softc->outstanding_cmds++;
2289 /* We expect a unit attention from this device */
2290 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
2291 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
2292 softc->flags &= ~DA_FLAG_RETRY_UA;
2295 start_ccb->ccb_h.ccb_bp = bp;
2296 xpt_action(start_ccb);
2298 /* May have more work to do, so ensure we stay scheduled */
2302 case DA_STATE_PROBE_RC:
2304 struct scsi_read_capacity_data *rcap;
2306 rcap = (struct scsi_read_capacity_data *)
2307 malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
2309 printf("dastart: Couldn't malloc read_capacity data\n");
2310 /* da_free_periph??? */
2313 scsi_read_capacity(&start_ccb->csio,
2314 /*retries*/da_retry_count,
2320 start_ccb->ccb_h.ccb_bp = NULL;
2321 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC;
2322 xpt_action(start_ccb);
2325 case DA_STATE_PROBE_RC16:
2327 struct scsi_read_capacity_data_long *rcaplong;
2329 rcaplong = (struct scsi_read_capacity_data_long *)
2330 malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
2331 if (rcaplong == NULL) {
2332 printf("dastart: Couldn't malloc read_capacity data\n");
2333 /* da_free_periph??? */
2336 scsi_read_capacity_16(&start_ccb->csio,
2337 /*retries*/ da_retry_count,
2339 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2344 /*sense_len*/ SSD_FULL_SIZE,
2345 /*timeout*/ da_default_timeout * 1000);
2346 start_ccb->ccb_h.ccb_bp = NULL;
2347 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC16;
2348 xpt_action(start_ccb);
2351 case DA_STATE_PROBE_LBP:
2353 struct scsi_vpd_logical_block_prov *lbp;
2355 if (!scsi_vpd_supported_page(periph, SVPD_LBP)) {
2357 * If we get here we don't support any SBC-3 delete
2358 * methods with UNMAP as the Logical Block Provisioning
2359 * VPD page support is required for devices which
2360 * support it according to T10/1799-D Revision 31
2361 * however older revisions of the spec don't mandate
2362 * this so we currently don't remove these methods
2363 * from the available set.
2365 softc->state = DA_STATE_PROBE_BLK_LIMITS;
2369 lbp = (struct scsi_vpd_logical_block_prov *)
2370 malloc(sizeof(*lbp), M_SCSIDA, M_NOWAIT|M_ZERO);
2373 printf("dastart: Couldn't malloc lbp data\n");
2374 /* da_free_periph??? */
2378 scsi_inquiry(&start_ccb->csio,
2379 /*retries*/da_retry_count,
2381 /*tag_action*/MSG_SIMPLE_Q_TAG,
2382 /*inq_buf*/(u_int8_t *)lbp,
2383 /*inq_len*/sizeof(*lbp),
2385 /*page_code*/SVPD_LBP,
2386 /*sense_len*/SSD_MIN_SIZE,
2387 /*timeout*/da_default_timeout * 1000);
2388 start_ccb->ccb_h.ccb_bp = NULL;
2389 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_LBP;
2390 xpt_action(start_ccb);
2393 case DA_STATE_PROBE_BLK_LIMITS:
2395 struct scsi_vpd_block_limits *block_limits;
2397 if (!scsi_vpd_supported_page(periph, SVPD_BLOCK_LIMITS)) {
2398 /* Not supported skip to next probe */
2399 softc->state = DA_STATE_PROBE_BDC;
2403 block_limits = (struct scsi_vpd_block_limits *)
2404 malloc(sizeof(*block_limits), M_SCSIDA, M_NOWAIT|M_ZERO);
2406 if (block_limits == NULL) {
2407 printf("dastart: Couldn't malloc block_limits data\n");
2408 /* da_free_periph??? */
2412 scsi_inquiry(&start_ccb->csio,
2413 /*retries*/da_retry_count,
2415 /*tag_action*/MSG_SIMPLE_Q_TAG,
2416 /*inq_buf*/(u_int8_t *)block_limits,
2417 /*inq_len*/sizeof(*block_limits),
2419 /*page_code*/SVPD_BLOCK_LIMITS,
2420 /*sense_len*/SSD_MIN_SIZE,
2421 /*timeout*/da_default_timeout * 1000);
2422 start_ccb->ccb_h.ccb_bp = NULL;
2423 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BLK_LIMITS;
2424 xpt_action(start_ccb);
2427 case DA_STATE_PROBE_BDC:
2429 struct scsi_vpd_block_characteristics *bdc;
2431 if (!scsi_vpd_supported_page(periph, SVPD_BDC)) {
2432 softc->state = DA_STATE_PROBE_ATA;
2436 bdc = (struct scsi_vpd_block_characteristics *)
2437 malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
2440 printf("dastart: Couldn't malloc bdc data\n");
2441 /* da_free_periph??? */
2445 scsi_inquiry(&start_ccb->csio,
2446 /*retries*/da_retry_count,
2448 /*tag_action*/MSG_SIMPLE_Q_TAG,
2449 /*inq_buf*/(u_int8_t *)bdc,
2450 /*inq_len*/sizeof(*bdc),
2452 /*page_code*/SVPD_BDC,
2453 /*sense_len*/SSD_MIN_SIZE,
2454 /*timeout*/da_default_timeout * 1000);
2455 start_ccb->ccb_h.ccb_bp = NULL;
2456 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BDC;
2457 xpt_action(start_ccb);
2460 case DA_STATE_PROBE_ATA:
2462 struct ata_params *ata_params;
2464 if (!scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
2465 daprobedone(periph, start_ccb);
2469 ata_params = (struct ata_params*)
2470 malloc(sizeof(*ata_params), M_SCSIDA, M_NOWAIT|M_ZERO);
2472 if (ata_params == NULL) {
2473 printf("dastart: Couldn't malloc ata_params data\n");
2474 /* da_free_periph??? */
2478 scsi_ata_identify(&start_ccb->csio,
2479 /*retries*/da_retry_count,
2481 /*tag_action*/MSG_SIMPLE_Q_TAG,
2482 /*data_ptr*/(u_int8_t *)ata_params,
2483 /*dxfer_len*/sizeof(*ata_params),
2484 /*sense_len*/SSD_FULL_SIZE,
2485 /*timeout*/da_default_timeout * 1000);
2486 start_ccb->ccb_h.ccb_bp = NULL;
2487 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA;
2488 xpt_action(start_ccb);
2495 cmd6workaround(union ccb *ccb)
2497 struct scsi_rw_6 cmd6;
2498 struct scsi_rw_10 *cmd10;
2499 struct da_softc *softc;
2504 cdb = ccb->csio.cdb_io.cdb_bytes;
2505 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
2507 if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
2508 da_delete_methods old_method = softc->delete_method;
2511 * Typically there are two reasons for failure here
2512 * 1. Delete method was detected as supported but isn't
2513 * 2. Delete failed due to invalid params e.g. too big
2515 * While we will attempt to choose an alternative delete method
2516 * this may result in short deletes if the existing delete
2517 * requests from geom are big for the new method choosen.
2519 * This method assumes that the error which triggered this
2520 * will not retry the io otherwise a panic will occur
2522 dadeleteflag(softc, old_method, 0);
2523 dadeletemethodchoose(softc, DA_DELETE_DISABLE);
2524 if (softc->delete_method == DA_DELETE_DISABLE)
2525 xpt_print(ccb->ccb_h.path,
2526 "%s failed, disabling BIO_DELETE\n",
2527 da_delete_method_desc[old_method]);
2529 xpt_print(ccb->ccb_h.path,
2530 "%s failed, switching to %s BIO_DELETE\n",
2531 da_delete_method_desc[old_method],
2532 da_delete_method_desc[softc->delete_method]);
2535 while ((bp = bioq_takefirst(&softc->delete_run_queue))
2537 bioq_disksort(&softc->delete_queue, bp);
2539 while ((bp = bioq_takefirst(&softc->delete_run_queue))
2541 bioq_insert_tail(&softc->delete_queue, bp);
2543 bioq_insert_tail(&softc->delete_queue,
2544 (struct bio *)ccb->ccb_h.ccb_bp);
2545 ccb->ccb_h.ccb_bp = NULL;
2549 /* Translation only possible if CDB is an array and cmd is R/W6 */
2550 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
2551 (*cdb != READ_6 && *cdb != WRITE_6))
2554 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
2555 "increasing minimum_cmd_size to 10.\n");
2556 softc->minimum_cmd_size = 10;
2558 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
2559 cmd10 = (struct scsi_rw_10 *)cdb;
2560 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
2562 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
2563 cmd10->reserved = 0;
2564 scsi_ulto2b(cmd6.length, cmd10->length);
2565 cmd10->control = cmd6.control;
2566 ccb->csio.cdb_len = sizeof(*cmd10);
2568 /* Requeue request, unfreezing queue if necessary */
2569 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
2570 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2573 cam_release_devq(ccb->ccb_h.path,
2577 /*getcount_only*/0);
2583 dadone(struct cam_periph *periph, union ccb *done_ccb)
2585 struct da_softc *softc;
2586 struct ccb_scsiio *csio;
2590 softc = (struct da_softc *)periph->softc;
2591 priority = done_ccb->ccb_h.pinfo.priority;
2593 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
2595 csio = &done_ccb->csio;
2596 state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
2598 case DA_CCB_BUFFER_IO:
2601 struct bio *bp, *bp1;
2603 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2604 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2608 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
2613 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
2614 if (error == ERESTART) {
2616 * A retry was scheduled, so
2621 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2626 * return all queued I/O with EIO, so that
2627 * the client can retry these I/Os in the
2628 * proper order should it attempt to recover.
2633 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
2635 * Catastrophic error. Mark our pack as
2639 * XXX See if this is really a media
2642 xpt_print(periph->path,
2643 "Invalidating pack\n");
2644 softc->flags |= DA_FLAG_PACK_INVALID;
2645 queued_error = ENXIO;
2647 bioq_flush(&softc->bio_queue, NULL,
2650 bp->bio_error = error;
2651 bp->bio_resid = bp->bio_bcount;
2652 bp->bio_flags |= BIO_ERROR;
2654 } else if (bp != NULL) {
2655 bp->bio_resid = csio->resid;
2657 if (bp->bio_resid != 0)
2658 bp->bio_flags |= BIO_ERROR;
2660 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2661 cam_release_devq(done_ccb->ccb_h.path,
2665 /*getcount_only*/0);
2666 } else if (bp != NULL) {
2667 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2668 panic("REQ_CMP with QFRZN");
2669 bp->bio_resid = csio->resid;
2670 if (csio->resid > 0)
2671 bp->bio_flags |= BIO_ERROR;
2672 if (softc->error_inject != 0) {
2673 bp->bio_error = softc->error_inject;
2674 bp->bio_resid = bp->bio_bcount;
2675 bp->bio_flags |= BIO_ERROR;
2676 softc->error_inject = 0;
2682 * Block out any asyncronous callbacks
2683 * while we touch the pending ccb list.
2685 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
2686 softc->outstanding_cmds--;
2687 if (softc->outstanding_cmds == 0)
2688 softc->flags |= DA_FLAG_WENT_IDLE;
2690 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
2691 xpt_print(periph->path, "oustanding %d\n",
2692 softc->outstanding_cmds);
2695 if (state == DA_CCB_DELETE) {
2696 while ((bp1 = bioq_takefirst(&softc->delete_run_queue))
2698 bp1->bio_resid = bp->bio_resid;
2699 bp1->bio_error = bp->bio_error;
2700 if (bp->bio_flags & BIO_ERROR)
2701 bp1->bio_flags |= BIO_ERROR;
2704 softc->delete_running = 0;
2708 } else if (bp != NULL)
2712 case DA_CCB_PROBE_RC:
2713 case DA_CCB_PROBE_RC16:
2715 struct scsi_read_capacity_data *rdcap;
2716 struct scsi_read_capacity_data_long *rcaplong;
2717 char announce_buf[80];
2723 if (state == DA_CCB_PROBE_RC)
2724 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
2726 rcaplong = (struct scsi_read_capacity_data_long *)
2729 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
2730 struct disk_params *dp;
2731 uint32_t block_size;
2733 u_int lbppbe; /* LB per physical block exponent. */
2734 u_int lalba; /* Lowest aligned LBA. */
2736 if (state == DA_CCB_PROBE_RC) {
2737 block_size = scsi_4btoul(rdcap->length);
2738 maxsector = scsi_4btoul(rdcap->addr);
2743 * According to SBC-2, if the standard 10
2744 * byte READ CAPACITY command returns 2^32,
2745 * we should issue the 16 byte version of
2746 * the command, since the device in question
2747 * has more sectors than can be represented
2748 * with the short version of the command.
2750 if (maxsector == 0xffffffff) {
2751 free(rdcap, M_SCSIDA);
2752 xpt_release_ccb(done_ccb);
2753 softc->state = DA_STATE_PROBE_RC16;
2754 xpt_schedule(periph, priority);
2758 block_size = scsi_4btoul(rcaplong->length);
2759 maxsector = scsi_8btou64(rcaplong->addr);
2760 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
2761 lalba = scsi_2btoul(rcaplong->lalba_lbp);
2765 * Because GEOM code just will panic us if we
2766 * give them an 'illegal' value we'll avoid that
2769 if (block_size == 0 && maxsector == 0) {
2770 snprintf(announce_buf, sizeof(announce_buf),
2772 } else if (block_size >= MAXPHYS || block_size == 0) {
2773 xpt_print(periph->path,
2774 "unsupportable block size %ju\n",
2775 (uintmax_t) block_size);
2776 announce_buf[0] = '\0';
2777 cam_periph_invalidate(periph);
2780 * We pass rcaplong into dasetgeom(),
2781 * because it will only use it if it is
2784 dasetgeom(periph, block_size, maxsector,
2785 rcaplong, sizeof(*rcaplong));
2786 lbp = (lalba & SRC16_LBPME_A);
2787 dp = &softc->params;
2788 snprintf(announce_buf, sizeof(announce_buf),
2789 "%juMB (%ju %u byte sectors: %dH %dS/T "
2791 (((uintmax_t)dp->secsize *
2792 dp->sectors) / (1024*1024)),
2793 (uintmax_t)dp->sectors,
2794 dp->secsize, dp->heads,
2795 dp->secs_per_track, dp->cylinders);
2800 announce_buf[0] = '\0';
2803 * Retry any UNIT ATTENTION type errors. They
2804 * are expected at boot.
2806 error = daerror(done_ccb, CAM_RETRY_SELTO,
2807 SF_RETRY_UA|SF_NO_PRINT);
2808 if (error == ERESTART) {
2810 * A retry was scheuled, so
2814 } else if (error != 0) {
2816 int sense_key, error_code;
2819 struct ccb_getdev cgd;
2821 /* Don't wedge this device's queue */
2822 status = done_ccb->ccb_h.status;
2823 if ((status & CAM_DEV_QFRZN) != 0)
2824 cam_release_devq(done_ccb->ccb_h.path,
2828 /*getcount_only*/0);
2831 xpt_setup_ccb(&cgd.ccb_h,
2832 done_ccb->ccb_h.path,
2833 CAM_PRIORITY_NORMAL);
2834 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2835 xpt_action((union ccb *)&cgd);
2837 if (scsi_extract_sense_ccb(done_ccb,
2838 &error_code, &sense_key, &asc, &ascq))
2844 * If we tried READ CAPACITY(16) and failed,
2845 * fallback to READ CAPACITY(10).
2847 if ((state == DA_CCB_PROBE_RC16) &&
2848 (softc->flags & DA_FLAG_CAN_RC16) &&
2849 (((csio->ccb_h.status & CAM_STATUS_MASK) ==
2852 (error_code == SSD_CURRENT_ERROR) &&
2853 (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
2854 softc->flags &= ~DA_FLAG_CAN_RC16;
2855 free(rdcap, M_SCSIDA);
2856 xpt_release_ccb(done_ccb);
2857 softc->state = DA_STATE_PROBE_RC;
2858 xpt_schedule(periph, priority);
2862 * Attach to anything that claims to be a
2863 * direct access or optical disk device,
2864 * as long as it doesn't return a "Logical
2865 * unit not supported" (0x25) error.
2867 if ((have_sense) && (asc != 0x25)
2868 && (error_code == SSD_CURRENT_ERROR)) {
2869 const char *sense_key_desc;
2870 const char *asc_desc;
2872 scsi_sense_desc(sense_key, asc, ascq,
2876 snprintf(announce_buf,
2877 sizeof(announce_buf),
2878 "Attempt to query device "
2879 "size failed: %s, %s",
2887 xpt_print(periph->path,
2888 "got CAM status %#x\n",
2889 done_ccb->ccb_h.status);
2892 xpt_print(periph->path, "fatal error, "
2893 "failed to attach to device\n");
2896 * Free up resources.
2898 cam_periph_invalidate(periph);
2902 free(csio->data_ptr, M_SCSIDA);
2903 if (announce_buf[0] != '\0' && ((softc->flags & DA_FLAG_PROBED) == 0)) {
2905 * Create our sysctl variables, now that we know
2906 * we have successfully attached.
2908 /* increase the refcount */
2909 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
2910 taskqueue_enqueue(taskqueue_thread,
2911 &softc->sysctl_task);
2912 xpt_announce_periph(periph, announce_buf);
2913 xpt_announce_quirks(periph, softc->quirks,
2916 xpt_print(periph->path, "fatal error, "
2917 "could not acquire reference count\n");
2921 /* Ensure re-probe doesn't see old delete. */
2922 softc->delete_available = 0;
2925 * Based on older SBC-3 spec revisions
2926 * any of the UNMAP methods "may" be
2927 * available via LBP given this flag so
2928 * we flag all of them as availble and
2929 * then remove those which further
2930 * probes confirm aren't available
2933 * We could also check readcap(16) p_type
2934 * flag to exclude one or more invalid
2935 * write same (X) types here
2937 dadeleteflag(softc, DA_DELETE_WS16, 1);
2938 dadeleteflag(softc, DA_DELETE_WS10, 1);
2939 dadeleteflag(softc, DA_DELETE_ZERO, 1);
2940 dadeleteflag(softc, DA_DELETE_UNMAP, 1);
2942 xpt_release_ccb(done_ccb);
2943 softc->state = DA_STATE_PROBE_LBP;
2944 xpt_schedule(periph, priority);
2948 xpt_release_ccb(done_ccb);
2949 softc->state = DA_STATE_PROBE_BDC;
2950 xpt_schedule(periph, priority);
2953 case DA_CCB_PROBE_LBP:
2955 struct scsi_vpd_logical_block_prov *lbp;
2957 lbp = (struct scsi_vpd_logical_block_prov *)csio->data_ptr;
2959 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
2961 * T10/1799-D Revision 31 states at least one of these
2962 * must be supported but we don't currently enforce this.
2964 dadeleteflag(softc, DA_DELETE_WS16,
2965 (lbp->flags & SVPD_LBP_WS16));
2966 dadeleteflag(softc, DA_DELETE_WS10,
2967 (lbp->flags & SVPD_LBP_WS10));
2968 dadeleteflag(softc, DA_DELETE_ZERO,
2969 (lbp->flags & SVPD_LBP_WS10));
2970 dadeleteflag(softc, DA_DELETE_UNMAP,
2971 (lbp->flags & SVPD_LBP_UNMAP));
2973 if (lbp->flags & SVPD_LBP_UNMAP) {
2974 free(lbp, M_SCSIDA);
2975 xpt_release_ccb(done_ccb);
2976 softc->state = DA_STATE_PROBE_BLK_LIMITS;
2977 xpt_schedule(periph, priority);
2982 error = daerror(done_ccb, CAM_RETRY_SELTO,
2983 SF_RETRY_UA|SF_NO_PRINT);
2984 if (error == ERESTART)
2986 else if (error != 0) {
2987 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
2988 /* Don't wedge this device's queue */
2989 cam_release_devq(done_ccb->ccb_h.path,
2993 /*getcount_only*/0);
2997 * Failure indicates we don't support any SBC-3
2998 * delete methods with UNMAP
3003 free(lbp, M_SCSIDA);
3004 xpt_release_ccb(done_ccb);
3005 softc->state = DA_STATE_PROBE_BDC;
3006 xpt_schedule(periph, priority);
3009 case DA_CCB_PROBE_BLK_LIMITS:
3011 struct scsi_vpd_block_limits *block_limits;
3013 block_limits = (struct scsi_vpd_block_limits *)csio->data_ptr;
3015 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3016 uint32_t max_unmap_lba_cnt = scsi_4btoul(
3017 block_limits->max_unmap_lba_cnt);
3018 uint32_t max_unmap_blk_cnt = scsi_4btoul(
3019 block_limits->max_unmap_blk_cnt);
3020 uint64_t ws_max_blks = scsi_8btou64(
3021 block_limits->max_write_same_length);
3023 * We should already support UNMAP but we check lba
3024 * and block count to be sure
3026 if (max_unmap_lba_cnt != 0x00L &&
3027 max_unmap_blk_cnt != 0x00L) {
3028 softc->unmap_max_lba = max_unmap_lba_cnt;
3029 softc->unmap_max_ranges = min(max_unmap_blk_cnt,
3033 * Unexpected UNMAP limits which means the
3034 * device doesn't actually support UNMAP
3036 dadeleteflag(softc, DA_DELETE_UNMAP, 0);
3039 if (ws_max_blks != 0x00L)
3040 softc->ws_max_blks = ws_max_blks;
3043 error = daerror(done_ccb, CAM_RETRY_SELTO,
3044 SF_RETRY_UA|SF_NO_PRINT);
3045 if (error == ERESTART)
3047 else if (error != 0) {
3048 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3049 /* Don't wedge this device's queue */
3050 cam_release_devq(done_ccb->ccb_h.path,
3054 /*getcount_only*/0);
3058 * Failure here doesn't mean UNMAP is not
3059 * supported as this is an optional page.
3061 softc->unmap_max_lba = 1;
3062 softc->unmap_max_ranges = 1;
3066 free(block_limits, M_SCSIDA);
3067 xpt_release_ccb(done_ccb);
3068 softc->state = DA_STATE_PROBE_BDC;
3069 xpt_schedule(periph, priority);
3072 case DA_CCB_PROBE_BDC:
3074 struct scsi_vpd_block_characteristics *bdc;
3076 bdc = (struct scsi_vpd_block_characteristics *)csio->data_ptr;
3078 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3080 * Disable queue sorting for non-rotational media
3083 if (scsi_2btoul(bdc->medium_rotation_rate) ==
3084 SVPD_BDC_RATE_NONE_ROTATING)
3085 softc->sort_io_queue = 0;
3088 error = daerror(done_ccb, CAM_RETRY_SELTO,
3089 SF_RETRY_UA|SF_NO_PRINT);
3090 if (error == ERESTART)
3092 else if (error != 0) {
3093 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3094 /* Don't wedge this device's queue */
3095 cam_release_devq(done_ccb->ccb_h.path,
3099 /*getcount_only*/0);
3104 free(bdc, M_SCSIDA);
3105 xpt_release_ccb(done_ccb);
3106 softc->state = DA_STATE_PROBE_ATA;
3107 xpt_schedule(periph, priority);
3110 case DA_CCB_PROBE_ATA:
3113 struct ata_params *ata_params;
3116 ata_params = (struct ata_params *)csio->data_ptr;
3117 ptr = (uint16_t *)ata_params;
3119 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3120 for (i = 0; i < sizeof(*ata_params) / 2; i++)
3121 ptr[i] = le16toh(ptr[i]);
3122 if (ata_params->support_dsm & ATA_SUPPORT_DSM_TRIM) {
3123 dadeleteflag(softc, DA_DELETE_ATA_TRIM, 1);
3124 if (ata_params->max_dsm_blocks != 0)
3125 softc->trim_max_ranges = min(
3126 softc->trim_max_ranges,
3127 ata_params->max_dsm_blocks *
3128 ATA_DSM_BLK_RANGES);
3131 * Disable queue sorting for non-rotational media
3134 if (ata_params->media_rotation_rate == 1)
3135 softc->sort_io_queue = 0;
3138 error = daerror(done_ccb, CAM_RETRY_SELTO,
3139 SF_RETRY_UA|SF_NO_PRINT);
3140 if (error == ERESTART)
3142 else if (error != 0) {
3143 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3144 /* Don't wedge this device's queue */
3145 cam_release_devq(done_ccb->ccb_h.path,
3149 /*getcount_only*/0);
3154 free(ata_params, M_SCSIDA);
3155 daprobedone(periph, done_ccb);
3158 case DA_CCB_WAITING:
3160 /* Caller will release the CCB */
3161 wakeup(&done_ccb->ccb_h.cbfcnp);
3165 /* No-op. We're polling */
3169 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3171 if (daerror(done_ccb, CAM_RETRY_SELTO,
3172 SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
3175 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3176 cam_release_devq(done_ccb->ccb_h.path,
3180 /*getcount_only*/0);
3182 xpt_release_ccb(done_ccb);
3183 cam_periph_release_locked(periph);
3189 xpt_release_ccb(done_ccb);
3193 dareprobe(struct cam_periph *periph)
3195 struct da_softc *softc;
3198 softc = (struct da_softc *)periph->softc;
3200 /* Probe in progress; don't interfere. */
3201 if (softc->state != DA_STATE_NORMAL)
3204 status = cam_periph_acquire(periph);
3205 KASSERT(status == CAM_REQ_CMP,
3206 ("dareprobe: cam_periph_acquire failed"));
3208 if (softc->flags & DA_FLAG_CAN_RC16)
3209 softc->state = DA_STATE_PROBE_RC16;
3211 softc->state = DA_STATE_PROBE_RC;
3213 xpt_schedule(periph, CAM_PRIORITY_DEV);
3217 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3219 struct da_softc *softc;
3220 struct cam_periph *periph;
3221 int error, error_code, sense_key, asc, ascq;
3223 periph = xpt_path_periph(ccb->ccb_h.path);
3224 softc = (struct da_softc *)periph->softc;
3227 * Automatically detect devices that do not support
3228 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
3231 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3232 error = cmd6workaround(ccb);
3233 } else if (scsi_extract_sense_ccb(ccb,
3234 &error_code, &sense_key, &asc, &ascq)) {
3235 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3236 error = cmd6workaround(ccb);
3238 * If the target replied with CAPACITY DATA HAS CHANGED UA,
3239 * query the capacity and notify upper layers.
3241 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3242 asc == 0x2A && ascq == 0x09) {
3243 xpt_print(periph->path, "capacity data has changed\n");
3245 sense_flags |= SF_NO_PRINT;
3246 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3247 asc == 0x28 && ascq == 0x00)
3248 disk_media_changed(softc->disk, M_NOWAIT);
3249 else if (sense_key == SSD_KEY_NOT_READY &&
3250 asc == 0x3a && (softc->flags & DA_FLAG_SAW_MEDIA)) {
3251 softc->flags &= ~DA_FLAG_SAW_MEDIA;
3252 disk_media_gone(softc->disk, M_NOWAIT);
3255 if (error == ERESTART)
3260 * Until we have a better way of doing pack validation,
3261 * don't treat UAs as errors.
3263 sense_flags |= SF_RETRY_UA;
3264 return(cam_periph_error(ccb, cam_flags, sense_flags,
3265 &softc->saved_ccb));
3269 damediapoll(void *arg)
3271 struct cam_periph *periph = arg;
3272 struct da_softc *softc = periph->softc;
3274 if (!softc->tur && softc->outstanding_cmds == 0) {
3275 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3280 /* Queue us up again */
3281 if (da_poll_period != 0)
3282 callout_schedule(&softc->mediapoll_c, da_poll_period * hz);
3286 daprevent(struct cam_periph *periph, int action)
3288 struct da_softc *softc;
3292 softc = (struct da_softc *)periph->softc;
3294 if (((action == PR_ALLOW)
3295 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
3296 || ((action == PR_PREVENT)
3297 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
3301 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3303 scsi_prevent(&ccb->csio,
3311 error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
3312 SF_RETRY_UA | SF_QUIET_IR, softc->disk->d_devstat);
3315 if (action == PR_ALLOW)
3316 softc->flags &= ~DA_FLAG_PACK_LOCKED;
3318 softc->flags |= DA_FLAG_PACK_LOCKED;
3321 xpt_release_ccb(ccb);
3325 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
3326 struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
3328 struct ccb_calc_geometry ccg;
3329 struct da_softc *softc;
3330 struct disk_params *dp;
3331 u_int lbppbe, lalba;
3333 softc = (struct da_softc *)periph->softc;
3335 dp = &softc->params;
3336 dp->secsize = block_len;
3337 dp->sectors = maxsector + 1;
3338 if (rcaplong != NULL) {
3339 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
3340 lalba = scsi_2btoul(rcaplong->lalba_lbp);
3341 lalba &= SRC16_LALBA_A;
3348 dp->stripesize = block_len << lbppbe;
3349 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
3351 } else if (softc->quirks & DA_Q_4K) {
3352 dp->stripesize = 4096;
3353 dp->stripeoffset = 0;
3356 dp->stripeoffset = 0;
3359 * Have the controller provide us with a geometry
3360 * for this disk. The only time the geometry
3361 * matters is when we boot and the controller
3362 * is the only one knowledgeable enough to come
3363 * up with something that will make this a bootable
3366 xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3367 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
3368 ccg.block_size = dp->secsize;
3369 ccg.volume_size = dp->sectors;
3371 ccg.secs_per_track = 0;
3373 xpt_action((union ccb*)&ccg);
3374 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3376 * We don't know what went wrong here- but just pick
3377 * a geometry so we don't have nasty things like divide
3381 dp->secs_per_track = 255;
3382 dp->cylinders = dp->sectors / (255 * 255);
3383 if (dp->cylinders == 0) {
3387 dp->heads = ccg.heads;
3388 dp->secs_per_track = ccg.secs_per_track;
3389 dp->cylinders = ccg.cylinders;
3393 * If the user supplied a read capacity buffer, and if it is
3394 * different than the previous buffer, update the data in the EDT.
3395 * If it's the same, we don't bother. This avoids sending an
3396 * update every time someone opens this device.
3398 if ((rcaplong != NULL)
3399 && (bcmp(rcaplong, &softc->rcaplong,
3400 min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
3401 struct ccb_dev_advinfo cdai;
3403 xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3404 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
3405 cdai.buftype = CDAI_TYPE_RCAPLONG;
3406 cdai.flags |= CDAI_FLAG_STORE;
3407 cdai.bufsiz = rcap_len;
3408 cdai.buf = (uint8_t *)rcaplong;
3409 xpt_action((union ccb *)&cdai);
3410 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
3411 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
3412 if (cdai.ccb_h.status != CAM_REQ_CMP) {
3413 xpt_print(periph->path, "%s: failed to set read "
3414 "capacity advinfo\n", __func__);
3415 /* Use cam_error_print() to decode the status */
3416 cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
3419 bcopy(rcaplong, &softc->rcaplong,
3420 min(sizeof(softc->rcaplong), rcap_len));
3424 softc->disk->d_sectorsize = softc->params.secsize;
3425 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
3426 softc->disk->d_stripesize = softc->params.stripesize;
3427 softc->disk->d_stripeoffset = softc->params.stripeoffset;
3428 /* XXX: these are not actually "firmware" values, so they may be wrong */
3429 softc->disk->d_fwsectors = softc->params.secs_per_track;
3430 softc->disk->d_fwheads = softc->params.heads;
3431 softc->disk->d_devstat->block_size = softc->params.secsize;
3432 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
3436 dasendorderedtag(void *arg)
3438 struct da_softc *softc = arg;
3440 if (da_send_ordered) {
3441 if ((softc->ordered_tag_count == 0)
3442 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
3443 softc->flags |= DA_FLAG_NEED_OTAG;
3445 if (softc->outstanding_cmds > 0)
3446 softc->flags &= ~DA_FLAG_WENT_IDLE;
3448 softc->ordered_tag_count = 0;
3450 /* Queue us up again */
3451 callout_reset(&softc->sendordered_c,
3452 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
3453 dasendorderedtag, softc);
3457 * Step through all DA peripheral drivers, and if the device is still open,
3458 * sync the disk cache to physical media.
3461 dashutdown(void * arg, int howto)
3463 struct cam_periph *periph;
3464 struct da_softc *softc;
3468 CAM_PERIPH_FOREACH(periph, &dadriver) {
3469 cam_periph_lock(periph);
3470 softc = (struct da_softc *)periph->softc;
3473 * We only sync the cache if the drive is still open, and
3474 * if the drive is capable of it..
3476 if (((softc->flags & DA_FLAG_OPEN) == 0)
3477 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
3478 cam_periph_unlock(periph);
3482 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3483 scsi_synchronize_cache(&ccb->csio,
3487 /*begin_lba*/0, /* whole disk */
3492 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
3493 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR,
3494 softc->disk->d_devstat);
3496 xpt_print(periph->path, "Synchronize cache failed\n");
3497 xpt_release_ccb(ccb);
3498 cam_periph_unlock(periph);
3502 #else /* !_KERNEL */
3505 * XXX This is only left out of the kernel build to silence warnings. If,
3506 * for some reason this function is used in the kernel, the ifdefs should
3507 * be moved so it is included both in the kernel and userland.
3510 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
3511 void (*cbfcnp)(struct cam_periph *, union ccb *),
3512 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
3513 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
3516 struct scsi_format_unit *scsi_cmd;
3518 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
3519 scsi_cmd->opcode = FORMAT_UNIT;
3520 scsi_cmd->byte2 = byte2;
3521 scsi_ulto2b(ileave, scsi_cmd->interleave);
3526 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
3535 #endif /* _KERNEL */