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 <geom/geom.h>
48 #include <geom/geom_disk.h>
57 #include <cam/cam_ccb.h>
58 #include <cam/cam_periph.h>
59 #include <cam/cam_xpt_periph.h>
60 #include <cam/cam_sim.h>
62 #include <cam/scsi/scsi_message.h>
65 #include <cam/scsi/scsi_da.h>
76 DA_FLAG_PACK_INVALID = 0x001,
77 DA_FLAG_NEW_PACK = 0x002,
78 DA_FLAG_PACK_LOCKED = 0x004,
79 DA_FLAG_PACK_REMOVABLE = 0x008,
80 DA_FLAG_SAW_MEDIA = 0x010,
81 DA_FLAG_NEED_OTAG = 0x020,
82 DA_FLAG_WENT_IDLE = 0x040,
83 DA_FLAG_RETRY_UA = 0x080,
85 DA_FLAG_SCTX_INIT = 0x200,
86 DA_FLAG_CAN_RC16 = 0x400,
87 DA_FLAG_PROBED = 0x800
92 DA_Q_NO_SYNC_CACHE = 0x01,
93 DA_Q_NO_6_BYTE = 0x02,
94 DA_Q_NO_PREVENT = 0x04,
100 DA_CCB_PROBE2 = 0x02,
101 DA_CCB_BUFFER_IO = 0x03,
102 DA_CCB_WAITING = 0x04,
104 DA_CCB_DELETE = 0x06,
106 DA_CCB_TYPE_MASK = 0x0F,
107 DA_CCB_RETRY_UA = 0x10
117 DA_DELETE_MAX = DA_DELETE_UNMAP
120 static const char *da_delete_method_names[] =
121 { "NONE", "DISABLE", "ZERO", "WS10", "WS16", "UNMAP" };
123 /* Offsets into our private area for storing information */
124 #define ccb_state ppriv_field0
125 #define ccb_bp ppriv_ptr1
130 u_int8_t secs_per_track;
131 u_int32_t secsize; /* Number of bytes/sector */
132 u_int64_t sectors; /* total number sectors */
137 #define UNMAP_MAX_RANGES 512
140 struct bio_queue_head bio_queue;
141 struct bio_queue_head delete_queue;
142 struct bio_queue_head delete_run_queue;
143 SLIST_ENTRY(da_softc) links;
144 LIST_HEAD(, ccb_hdr) pending_ccbs;
148 int minimum_cmd_size;
150 int ordered_tag_count;
151 int outstanding_cmds;
152 int unmap_max_ranges;
156 da_delete_methods delete_method;
157 struct disk_params params;
160 struct task sysctl_task;
161 struct sysctl_ctx_list sysctl_ctx;
162 struct sysctl_oid *sysctl_tree;
163 struct callout sendordered_c;
165 uint8_t unmap_buf[UNMAP_MAX_RANGES * 16 + 8];
166 struct scsi_read_capacity_data_long rcaplong;
167 struct callout mediapoll_c;
170 struct da_quirk_entry {
171 struct scsi_inquiry_pattern inq_pat;
175 static const char quantum[] = "QUANTUM";
176 static const char microp[] = "MICROP";
178 static struct da_quirk_entry da_quirk_table[] =
180 /* SPI, FC devices */
183 * Fujitsu M2513A MO drives.
184 * Tested devices: M2513A2 firmware versions 1200 & 1300.
185 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
186 * Reported by: W.Scholten <whs@xs4all.nl>
188 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
189 /*quirks*/ DA_Q_NO_SYNC_CACHE
193 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
194 /*quirks*/ DA_Q_NO_SYNC_CACHE
198 * This particular Fujitsu drive doesn't like the
199 * synchronize cache command.
200 * Reported by: Tom Jackson <toj@gorilla.net>
202 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
203 /*quirks*/ DA_Q_NO_SYNC_CACHE
207 * This drive doesn't like the synchronize cache command
208 * either. Reported by: Matthew Jacob <mjacob@feral.com>
209 * in NetBSD PR kern/6027, August 24, 1998.
211 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
212 /*quirks*/ DA_Q_NO_SYNC_CACHE
216 * This drive doesn't like the synchronize cache command
217 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
220 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
221 /*quirks*/ DA_Q_NO_SYNC_CACHE
225 * Doesn't like the synchronize cache command.
226 * Reported by: Blaz Zupan <blaz@gold.amis.net>
228 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
229 /*quirks*/ DA_Q_NO_SYNC_CACHE
233 * Doesn't like the synchronize cache command.
234 * Reported by: Blaz Zupan <blaz@gold.amis.net>
236 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
237 /*quirks*/ DA_Q_NO_SYNC_CACHE
241 * Doesn't like the synchronize cache command.
243 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
244 /*quirks*/ DA_Q_NO_SYNC_CACHE
248 * Doesn't like the synchronize cache command.
249 * Reported by: walter@pelissero.de
251 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
252 /*quirks*/ DA_Q_NO_SYNC_CACHE
256 * Doesn't work correctly with 6 byte reads/writes.
257 * Returns illegal request, and points to byte 9 of the
259 * Reported by: Adam McDougall <bsdx@spawnet.com>
261 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
262 /*quirks*/ DA_Q_NO_6_BYTE
266 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
267 /*quirks*/ DA_Q_NO_6_BYTE
271 * Doesn't like the synchronize cache command.
272 * Reported by: walter@pelissero.de
274 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
275 /*quirks*/ DA_Q_NO_SYNC_CACHE
279 * The CISS RAID controllers do not support SYNC_CACHE
281 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
282 /*quirks*/ DA_Q_NO_SYNC_CACHE
284 /* USB mass storage devices supported by umass(4) */
287 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
290 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
291 /*quirks*/ DA_Q_NO_SYNC_CACHE
295 * Power Quotient Int. (PQI) USB flash key
298 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
299 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
303 * Creative Nomad MUVO mp3 player (USB)
306 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
307 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
311 * Jungsoft NEXDISK USB flash key
314 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
315 /*quirks*/ DA_Q_NO_SYNC_CACHE
319 * FreeDik USB Mini Data Drive
322 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
323 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
327 * Sigmatel USB Flash MP3 Player
330 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
331 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
335 * Neuros USB Digital Audio Computer
338 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
339 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
343 * SEAGRAND NP-900 MP3 Player
346 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
347 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
351 * iRiver iFP MP3 player (with UMS Firmware)
352 * PR: kern/54881, i386/63941, kern/66124
354 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
355 /*quirks*/ DA_Q_NO_SYNC_CACHE
359 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
362 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
363 /*quirks*/ DA_Q_NO_SYNC_CACHE
367 * ZICPlay USB MP3 Player with FM
370 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
371 /*quirks*/ DA_Q_NO_SYNC_CACHE
375 * TEAC USB floppy mechanisms
377 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
378 /*quirks*/ DA_Q_NO_SYNC_CACHE
382 * Kingston DataTraveler II+ USB Pen-Drive.
383 * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
385 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
386 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
394 {T_DIRECT, SIP_MEDIA_REMOVABLE, " ", "USB DISK Pro", "PMAP"},
395 /*quirks*/ DA_Q_NO_SYNC_CACHE
399 * Motorola E398 Mobile Phone (TransFlash memory card).
400 * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
403 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
404 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
408 * Qware BeatZkey! Pro
411 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
412 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
416 * Time DPA20B 1GB MP3 Player
419 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
420 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
424 * Samsung USB key 128Mb
427 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
428 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
432 * Kingston DataTraveler 2.0 USB Flash memory.
435 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
436 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
440 * Creative MUVO Slim mp3 player (USB)
443 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
444 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
448 * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
451 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
452 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
456 * SanDisk Micro Cruzer 128MB
459 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
460 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
464 * TOSHIBA TransMemory USB sticks
467 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
468 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
473 * PR: usb/75578, usb/72344, usb/65436
475 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
476 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
480 * Genesys 6-in-1 Card Reader
483 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
484 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
488 * Rekam Digital CAMERA
491 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
492 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
496 * iRiver H10 MP3 player
499 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
500 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
504 * iRiver U10 MP3 player
507 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
508 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
515 {T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
516 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
520 * EasyMP3 EM732X USB 2.0 Flash MP3 Player
523 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
524 "1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
531 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
532 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
536 * Philips USB Key Audio KEY013
539 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
540 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
547 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
548 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
555 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
556 /*quirks*/ DA_Q_NO_SYNC_CACHE
560 * I/O Magic USB flash - Giga Bank
563 {T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
564 /*quirks*/ DA_Q_NO_SYNC_CACHE
568 * JoyFly 128mb USB Flash Drive
571 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
572 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
579 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
580 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
584 * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A
587 {T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*",
588 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
592 * Samsung YP-U3 mp3-player
595 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3",
596 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
599 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
600 "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
604 * Sony Cyber-Shot DSC cameras
607 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
608 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
610 /* ATA/SATA devices over SAS/USB/... */
612 /* Hitachi Advanced Format (4k) drives */
613 { T_DIRECT, SIP_MEDIA_FIXED, "Hitachi", "H??????????E3*", "*" },
617 /* Samsung Advanced Format (4k) drives */
618 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD155UI*", "*" },
622 /* Samsung Advanced Format (4k) drives */
623 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD155UI*", "*" },
627 /* Samsung Advanced Format (4k) drives */
628 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD204UI*", "*" },
632 /* Samsung Advanced Format (4k) drives */
633 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD204UI*", "*" },
637 /* Seagate Barracuda Green Advanced Format (4k) drives */
638 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DL*", "*" },
642 /* Seagate Barracuda Green Advanced Format (4k) drives */
643 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DL", "*", "*" },
647 /* Seagate Barracuda Green Advanced Format (4k) drives */
648 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???DM*", "*" },
652 /* Seagate Barracuda Green Advanced Format (4k) drives */
653 { T_DIRECT, SIP_MEDIA_FIXED, "ST???DM*", "*", "*" },
657 /* Seagate Barracuda Green Advanced Format (4k) drives */
658 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DM*", "*" },
662 /* Seagate Barracuda Green Advanced Format (4k) drives */
663 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DM", "*", "*" },
667 /* Seagate Momentus Advanced Format (4k) drives */
668 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500423AS*", "*" },
672 /* Seagate Momentus Advanced Format (4k) drives */
673 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "3AS*", "*" },
677 /* Seagate Momentus Advanced Format (4k) drives */
678 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500424AS*", "*" },
682 /* Seagate Momentus Advanced Format (4k) drives */
683 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "4AS*", "*" },
687 /* Seagate Momentus Advanced Format (4k) drives */
688 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640423AS*", "*" },
692 /* Seagate Momentus Advanced Format (4k) drives */
693 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "3AS*", "*" },
697 /* Seagate Momentus Advanced Format (4k) drives */
698 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640424AS*", "*" },
702 /* Seagate Momentus Advanced Format (4k) drives */
703 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "4AS*", "*" },
707 /* Seagate Momentus Advanced Format (4k) drives */
708 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750420AS*", "*" },
712 /* Seagate Momentus Advanced Format (4k) drives */
713 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "0AS*", "*" },
717 /* Seagate Momentus Advanced Format (4k) drives */
718 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750422AS*", "*" },
722 /* Seagate Momentus Advanced Format (4k) drives */
723 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "2AS*", "*" },
727 /* Seagate Momentus Advanced Format (4k) drives */
728 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750423AS*", "*" },
732 /* Seagate Momentus Advanced Format (4k) drives */
733 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "3AS*", "*" },
737 /* Seagate Momentus Thin Advanced Format (4k) drives */
738 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???LT*", "*" },
742 /* Seagate Momentus Thin Advanced Format (4k) drives */
743 { T_DIRECT, SIP_MEDIA_FIXED, "ST???LT*", "*", "*" },
747 /* WDC Caviar Green Advanced Format (4k) drives */
748 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RS*", "*" },
752 /* WDC Caviar Green Advanced Format (4k) drives */
753 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RS*", "*" },
757 /* WDC Caviar Green Advanced Format (4k) drives */
758 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RX*", "*" },
762 /* WDC Caviar Green Advanced Format (4k) drives */
763 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RX*", "*" },
767 /* WDC Caviar Green Advanced Format (4k) drives */
768 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RS*", "*" },
772 /* WDC Caviar Green Advanced Format (4k) drives */
773 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RS*", "*" },
777 /* WDC Caviar Green Advanced Format (4k) drives */
778 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RX*", "*" },
782 /* WDC Caviar Green Advanced Format (4k) drives */
783 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RX*", "*" },
787 /* WDC Scorpio Black Advanced Format (4k) drives */
788 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PKT*", "*" },
792 /* WDC Scorpio Black Advanced Format (4k) drives */
793 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PKT*", "*" },
797 /* WDC Scorpio Black Advanced Format (4k) drives */
798 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PKT*", "*" },
802 /* WDC Scorpio Black Advanced Format (4k) drives */
803 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PKT*", "*" },
807 /* WDC Scorpio Blue Advanced Format (4k) drives */
808 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PVT*", "*" },
812 /* WDC Scorpio Blue Advanced Format (4k) drives */
813 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PVT*", "*" },
817 /* WDC Scorpio Blue Advanced Format (4k) drives */
818 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PVT*", "*" },
822 /* WDC Scorpio Blue Advanced Format (4k) drives */
823 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PVT*", "*" },
828 * Olympus FE-210 camera
830 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "FE210*",
831 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
837 {T_DIRECT, SIP_MEDIA_REMOVABLE, "LG", "UP3S",
838 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
842 * Laser MP3-2GA13 MP3 player
844 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "(HS) Flash Disk",
845 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
849 * LaCie external 250GB Hard drive des by Porsche
850 * Submitted by: Ben Stuyts <ben@altesco.nl>
853 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HM250JI", "*"},
854 /*quirks*/ DA_Q_NO_SYNC_CACHE
858 static disk_strategy_t dastrategy;
859 static dumper_t dadump;
860 static periph_init_t dainit;
861 static void daasync(void *callback_arg, u_int32_t code,
862 struct cam_path *path, void *arg);
863 static void dasysctlinit(void *context, int pending);
864 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
865 static int dadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
866 static int dadeletemethodset(struct da_softc *softc,
867 da_delete_methods delete_method);
868 static periph_ctor_t daregister;
869 static periph_dtor_t dacleanup;
870 static periph_start_t dastart;
871 static periph_oninv_t daoninvalidate;
872 static void dadone(struct cam_periph *periph,
873 union ccb *done_ccb);
874 static int daerror(union ccb *ccb, u_int32_t cam_flags,
875 u_int32_t sense_flags);
876 static void daprevent(struct cam_periph *periph, int action);
877 static void dareprobe(struct cam_periph *periph);
878 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
880 struct scsi_read_capacity_data_long *rcaplong,
882 static timeout_t dasendorderedtag;
883 static void dashutdown(void *arg, int howto);
884 static timeout_t damediapoll;
886 #ifndef DA_DEFAULT_POLL_PERIOD
887 #define DA_DEFAULT_POLL_PERIOD 3
890 #ifndef DA_DEFAULT_TIMEOUT
891 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
894 #ifndef DA_DEFAULT_RETRY
895 #define DA_DEFAULT_RETRY 4
898 #ifndef DA_DEFAULT_SEND_ORDERED
899 #define DA_DEFAULT_SEND_ORDERED 1
903 static int da_poll_period = DA_DEFAULT_POLL_PERIOD;
904 static int da_retry_count = DA_DEFAULT_RETRY;
905 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
906 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
908 static SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
909 "CAM Direct Access Disk driver");
910 SYSCTL_INT(_kern_cam_da, OID_AUTO, poll_period, CTLFLAG_RW,
911 &da_poll_period, 0, "Media polling period in seconds");
912 TUNABLE_INT("kern.cam.da.poll_period", &da_poll_period);
913 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
914 &da_retry_count, 0, "Normal I/O retry count");
915 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
916 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
917 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
918 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
919 SYSCTL_INT(_kern_cam_da, OID_AUTO, send_ordered, CTLFLAG_RW,
920 &da_send_ordered, 0, "Send Ordered Tags");
921 TUNABLE_INT("kern.cam.da.send_ordered", &da_send_ordered);
924 * DA_ORDEREDTAG_INTERVAL determines how often, relative
925 * to the default timeout, we check to see whether an ordered
926 * tagged transaction is appropriate to prevent simple tag
927 * starvation. Since we'd like to ensure that there is at least
928 * 1/2 of the timeout length left for a starved transaction to
929 * complete after we've sent an ordered tag, we must poll at least
930 * four times in every timeout period. This takes care of the worst
931 * case where a starved transaction starts during an interval that
932 * meets the requirement "don't send an ordered tag" test so it takes
933 * us two intervals to determine that a tag must be sent.
935 #ifndef DA_ORDEREDTAG_INTERVAL
936 #define DA_ORDEREDTAG_INTERVAL 4
939 static struct periph_driver dadriver =
942 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
945 PERIPHDRIVER_DECLARE(da, dadriver);
947 static MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
950 daopen(struct disk *dp)
952 struct cam_periph *periph;
953 struct da_softc *softc;
957 periph = (struct cam_periph *)dp->d_drv1;
958 if (periph == NULL) {
962 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
966 cam_periph_lock(periph);
967 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
968 cam_periph_unlock(periph);
969 cam_periph_release(periph);
973 unit = periph->unit_number;
974 softc = (struct da_softc *)periph->softc;
975 softc->flags |= DA_FLAG_OPEN;
977 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
980 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
981 /* Invalidate our pack information. */
982 softc->flags &= ~DA_FLAG_PACK_INVALID;
987 /* Wait for the disk size update. */
988 error = msleep(&softc->disk->d_mediasize, periph->sim->mtx, PRIBIO,
991 xpt_print(periph->path, "unable to retrieve capacity data");
993 if (periph->flags & CAM_PERIPH_INVALID ||
994 softc->disk->d_sectorsize == 0 ||
995 softc->disk->d_mediasize == 0)
998 if (error == 0 && (softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
999 (softc->quirks & DA_Q_NO_PREVENT) == 0)
1000 daprevent(periph, PR_PREVENT);
1003 softc->flags |= DA_FLAG_SAW_MEDIA;
1005 cam_periph_unhold(periph);
1006 cam_periph_unlock(periph);
1009 softc->flags &= ~DA_FLAG_OPEN;
1010 cam_periph_release(periph);
1017 daclose(struct disk *dp)
1019 struct cam_periph *periph;
1020 struct da_softc *softc;
1022 periph = (struct cam_periph *)dp->d_drv1;
1026 cam_periph_lock(periph);
1027 if (cam_periph_hold(periph, PRIBIO) != 0) {
1028 cam_periph_unlock(periph);
1029 cam_periph_release(periph);
1033 softc = (struct da_softc *)periph->softc;
1035 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1038 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0
1039 && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
1042 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1044 scsi_synchronize_cache(&ccb->csio,
1048 /*begin_lba*/0,/* Cover the whole disk */
1053 cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
1054 /*sense_flags*/SF_RETRY_UA | SF_QUIET_IR,
1055 softc->disk->d_devstat);
1056 xpt_release_ccb(ccb);
1060 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
1061 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
1062 daprevent(periph, PR_ALLOW);
1064 * If we've got removeable media, mark the blocksize as
1065 * unavailable, since it could change when new media is
1068 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1071 softc->flags &= ~DA_FLAG_OPEN;
1072 cam_periph_unhold(periph);
1073 cam_periph_unlock(periph);
1074 cam_periph_release(periph);
1079 daschedule(struct cam_periph *periph)
1081 struct da_softc *softc = (struct da_softc *)periph->softc;
1084 /* Check if cam_periph_getccb() was called. */
1085 prio = periph->immediate_priority;
1087 /* Check if we have more work to do. */
1088 if (bioq_first(&softc->bio_queue) ||
1089 (!softc->delete_running && bioq_first(&softc->delete_queue)) ||
1091 prio = CAM_PRIORITY_NORMAL;
1094 /* Schedule CCB if any of above is true. */
1095 if (prio != CAM_PRIORITY_NONE)
1096 xpt_schedule(periph, prio);
1100 * Actually translate the requested transfer into one the physical driver
1101 * can understand. The transfer is described by a buf and will include
1102 * only one physical transfer.
1105 dastrategy(struct bio *bp)
1107 struct cam_periph *periph;
1108 struct da_softc *softc;
1110 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1111 if (periph == NULL) {
1112 biofinish(bp, NULL, ENXIO);
1115 softc = (struct da_softc *)periph->softc;
1117 cam_periph_lock(periph);
1120 * If the device has been made invalid, error out
1122 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1123 cam_periph_unlock(periph);
1124 biofinish(bp, NULL, ENXIO);
1128 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
1131 * Place it in the queue of disk activities for this disk
1133 if (bp->bio_cmd == BIO_DELETE) {
1134 if (bp->bio_bcount == 0)
1137 bioq_disksort(&softc->delete_queue, bp);
1139 bioq_disksort(&softc->bio_queue, bp);
1142 * Schedule ourselves for performing the work.
1145 cam_periph_unlock(periph);
1151 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1153 struct cam_periph *periph;
1154 struct da_softc *softc;
1156 struct ccb_scsiio csio;
1161 periph = dp->d_drv1;
1164 softc = (struct da_softc *)periph->softc;
1165 cam_periph_lock(periph);
1166 secsize = softc->params.secsize;
1168 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1169 cam_periph_unlock(periph);
1174 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1175 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1176 scsi_read_write(&csio,
1182 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1185 /*data_ptr*/(u_int8_t *) virtual,
1186 /*dxfer_len*/length,
1187 /*sense_len*/SSD_FULL_SIZE,
1188 da_default_timeout * 1000);
1189 xpt_polled_action((union ccb *)&csio);
1191 error = cam_periph_error((union ccb *)&csio,
1192 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1193 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1194 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1195 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1197 printf("Aborting dump due to I/O error.\n");
1198 cam_periph_unlock(periph);
1203 * Sync the disk cache contents to the physical media.
1205 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1207 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1208 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1209 scsi_synchronize_cache(&csio,
1213 /*begin_lba*/0,/* Cover the whole disk */
1217 xpt_polled_action((union ccb *)&csio);
1219 error = cam_periph_error((union ccb *)&csio,
1220 0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL);
1221 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1222 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1223 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1225 xpt_print(periph->path, "Synchronize cache failed\n");
1227 cam_periph_unlock(periph);
1232 dagetattr(struct bio *bp)
1235 struct cam_periph *periph;
1237 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1241 cam_periph_lock(periph);
1242 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1244 cam_periph_unlock(periph);
1246 bp->bio_completed = bp->bio_length;
1256 * Install a global async callback. This callback will
1257 * receive async callbacks like "new device found".
1259 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1261 if (status != CAM_REQ_CMP) {
1262 printf("da: Failed to attach master async callback "
1263 "due to status 0x%x!\n", status);
1264 } else if (da_send_ordered) {
1266 /* Register our shutdown event handler */
1267 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1268 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1269 printf("dainit: shutdown event registration failed!\n");
1274 * Callback from GEOM, called when it has finished cleaning up its
1278 dadiskgonecb(struct disk *dp)
1280 struct cam_periph *periph;
1282 periph = (struct cam_periph *)dp->d_drv1;
1284 cam_periph_release(periph);
1288 daoninvalidate(struct cam_periph *periph)
1290 struct da_softc *softc;
1292 softc = (struct da_softc *)periph->softc;
1295 * De-register any async callbacks.
1297 xpt_register_async(0, daasync, periph, periph->path);
1299 softc->flags |= DA_FLAG_PACK_INVALID;
1302 * Return all queued I/O with ENXIO.
1303 * XXX Handle any transactions queued to the card
1304 * with XPT_ABORT_CCB.
1306 bioq_flush(&softc->bio_queue, NULL, ENXIO);
1307 bioq_flush(&softc->delete_queue, NULL, ENXIO);
1310 * Tell GEOM that we've gone away, we'll get a callback when it is
1311 * done cleaning up its resources.
1313 disk_gone(softc->disk);
1315 xpt_print(periph->path, "lost device - %d outstanding, %d refs\n",
1316 softc->outstanding_cmds, periph->refcount);
1320 dacleanup(struct cam_periph *periph)
1322 struct da_softc *softc;
1324 softc = (struct da_softc *)periph->softc;
1326 xpt_print(periph->path, "removing device entry\n");
1327 cam_periph_unlock(periph);
1330 * If we can't free the sysctl tree, oh well...
1332 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1333 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1334 xpt_print(periph->path, "can't remove sysctl context\n");
1337 callout_drain(&softc->mediapoll_c);
1338 disk_destroy(softc->disk);
1339 callout_drain(&softc->sendordered_c);
1340 free(softc, M_DEVBUF);
1341 cam_periph_lock(periph);
1345 daasync(void *callback_arg, u_int32_t code,
1346 struct cam_path *path, void *arg)
1348 struct cam_periph *periph;
1349 struct da_softc *softc;
1351 periph = (struct cam_periph *)callback_arg;
1353 case AC_FOUND_DEVICE:
1355 struct ccb_getdev *cgd;
1358 cgd = (struct ccb_getdev *)arg;
1362 if (cgd->protocol != PROTO_SCSI)
1365 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1366 && SID_TYPE(&cgd->inq_data) != T_RBC
1367 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1371 * Allocate a peripheral instance for
1372 * this device and start the probe
1375 status = cam_periph_alloc(daregister, daoninvalidate,
1377 "da", CAM_PERIPH_BIO,
1378 cgd->ccb_h.path, daasync,
1379 AC_FOUND_DEVICE, cgd);
1381 if (status != CAM_REQ_CMP
1382 && status != CAM_REQ_INPROG)
1383 printf("daasync: Unable to attach to new device "
1384 "due to status 0x%x\n", status);
1387 case AC_ADVINFO_CHANGED:
1391 buftype = (uintptr_t)arg;
1392 if (buftype == CDAI_TYPE_PHYS_PATH) {
1393 struct da_softc *softc;
1395 softc = periph->softc;
1396 disk_attr_changed(softc->disk, "GEOM::physpath",
1401 case AC_UNIT_ATTENTION:
1404 int error_code, sense_key, asc, ascq;
1406 softc = (struct da_softc *)periph->softc;
1407 ccb = (union ccb *)arg;
1410 * Handle all UNIT ATTENTIONs except our own,
1411 * as they will be handled by daerror().
1413 if (xpt_path_periph(ccb->ccb_h.path) != periph &&
1414 scsi_extract_sense_ccb(ccb,
1415 &error_code, &sense_key, &asc, &ascq)) {
1416 if (asc == 0x2A && ascq == 0x09) {
1417 xpt_print(ccb->ccb_h.path,
1418 "capacity data has changed\n");
1420 } else if (asc == 0x28 && ascq == 0x00)
1421 disk_media_changed(softc->disk, M_NOWAIT);
1423 cam_periph_async(periph, code, path, arg);
1427 softc = (struct da_softc *)periph->softc;
1428 if (softc->state == DA_STATE_NORMAL && !softc->tur) {
1429 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
1431 xpt_schedule(periph, CAM_PRIORITY_DEV);
1438 struct ccb_hdr *ccbh;
1440 softc = (struct da_softc *)periph->softc;
1442 * Don't fail on the expected unit attention
1445 softc->flags |= DA_FLAG_RETRY_UA;
1446 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1447 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1453 cam_periph_async(periph, code, path, arg);
1457 dasysctlinit(void *context, int pending)
1459 struct cam_periph *periph;
1460 struct da_softc *softc;
1461 char tmpstr[80], tmpstr2[80];
1462 struct ccb_trans_settings cts;
1464 periph = (struct cam_periph *)context;
1466 * periph was held for us when this task was enqueued
1468 if (periph->flags & CAM_PERIPH_INVALID) {
1469 cam_periph_release(periph);
1473 softc = (struct da_softc *)periph->softc;
1474 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1475 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1477 sysctl_ctx_init(&softc->sysctl_ctx);
1478 softc->flags |= DA_FLAG_SCTX_INIT;
1479 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1480 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1481 CTLFLAG_RD, 0, tmpstr);
1482 if (softc->sysctl_tree == NULL) {
1483 printf("dasysctlinit: unable to allocate sysctl tree\n");
1484 cam_periph_release(periph);
1489 * Now register the sysctl handler, so the user can change the value on
1492 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1493 OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RW,
1494 softc, 0, dadeletemethodsysctl, "A",
1495 "BIO_DELETE execution method");
1496 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1497 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1498 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1499 "Minimum CDB size");
1501 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1502 SYSCTL_CHILDREN(softc->sysctl_tree),
1506 &softc->error_inject,
1508 "error_inject leaf");
1512 * Add some addressing info.
1514 memset(&cts, 0, sizeof (cts));
1515 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1516 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1517 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1518 cam_periph_lock(periph);
1519 xpt_action((union ccb *)&cts);
1520 cam_periph_unlock(periph);
1521 if (cts.ccb_h.status != CAM_REQ_CMP) {
1522 cam_periph_release(periph);
1525 if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
1526 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1527 if (fc->valid & CTS_FC_VALID_WWPN) {
1528 softc->wwpn = fc->wwpn;
1529 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1530 SYSCTL_CHILDREN(softc->sysctl_tree),
1531 OID_AUTO, "wwpn", CTLFLAG_RD,
1532 &softc->wwpn, "World Wide Port Name");
1535 cam_periph_release(periph);
1539 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1543 value = *(int *)arg1;
1545 error = sysctl_handle_int(oidp, &value, 0, req);
1548 || (req->newptr == NULL))
1552 * Acceptable values here are 6, 10, 12 or 16.
1556 else if ((value > 6)
1559 else if ((value > 10)
1562 else if (value > 12)
1565 *(int *)arg1 = value;
1571 dadeletemethodset(struct da_softc *softc, da_delete_methods delete_method)
1574 if (delete_method < 0 || delete_method > DA_DELETE_MAX)
1577 softc->delete_method = delete_method;
1579 if (softc->delete_method > DA_DELETE_DISABLE)
1580 softc->disk->d_flags |= DISKFLAG_CANDELETE;
1582 softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
1588 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
1592 struct da_softc *softc;
1593 int i, error, value;
1595 softc = (struct da_softc *)arg1;
1597 value = softc->delete_method;
1598 if (value < 0 || value > DA_DELETE_MAX)
1601 p = da_delete_method_names[value];
1602 strncpy(buf, p, sizeof(buf));
1603 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1604 if (error != 0 || req->newptr == NULL)
1606 for (i = 0; i <= DA_DELETE_MAX; i++) {
1607 if (strcmp(buf, da_delete_method_names[i]) != 0)
1609 return dadeletemethodset(softc, i);
1615 daregister(struct cam_periph *periph, void *arg)
1617 struct da_softc *softc;
1618 struct ccb_pathinq cpi;
1619 struct ccb_getdev *cgd;
1623 cgd = (struct ccb_getdev *)arg;
1625 printf("daregister: no getdev CCB, can't register device\n");
1626 return(CAM_REQ_CMP_ERR);
1629 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
1632 if (softc == NULL) {
1633 printf("daregister: Unable to probe new device. "
1634 "Unable to allocate softc\n");
1635 return(CAM_REQ_CMP_ERR);
1638 LIST_INIT(&softc->pending_ccbs);
1639 softc->state = DA_STATE_PROBE;
1640 bioq_init(&softc->bio_queue);
1641 bioq_init(&softc->delete_queue);
1642 bioq_init(&softc->delete_run_queue);
1643 if (SID_IS_REMOVABLE(&cgd->inq_data))
1644 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1645 softc->unmap_max_ranges = UNMAP_MAX_RANGES;
1646 softc->unmap_max_lba = 1024*1024*2;
1648 periph->softc = softc;
1651 * See if this device has any quirks.
1653 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1654 (caddr_t)da_quirk_table,
1655 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1656 sizeof(*da_quirk_table), scsi_inquiry_match);
1659 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1661 softc->quirks = DA_Q_NONE;
1663 /* Check if the SIM does not want 6 byte commands */
1664 bzero(&cpi, sizeof(cpi));
1665 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1666 cpi.ccb_h.func_code = XPT_PATH_INQ;
1667 xpt_action((union ccb *)&cpi);
1668 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1669 softc->quirks |= DA_Q_NO_6_BYTE;
1671 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1674 * Take an exclusive refcount on the periph while dastart is called
1675 * to finish the probe. The reference will be dropped in dadone at
1678 (void)cam_periph_hold(periph, PRIBIO);
1681 * Schedule a periodic event to occasionally send an
1682 * ordered tag to a device.
1684 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
1685 callout_reset(&softc->sendordered_c,
1686 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
1687 dasendorderedtag, softc);
1689 mtx_unlock(periph->sim->mtx);
1691 * RBC devices don't have to support READ(6), only READ(10).
1693 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1694 softc->minimum_cmd_size = 10;
1696 softc->minimum_cmd_size = 6;
1699 * Load the user's default, if any.
1701 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1702 periph->unit_number);
1703 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1706 * 6, 10, 12 and 16 are the currently permissible values.
1708 if (softc->minimum_cmd_size < 6)
1709 softc->minimum_cmd_size = 6;
1710 else if ((softc->minimum_cmd_size > 6)
1711 && (softc->minimum_cmd_size <= 10))
1712 softc->minimum_cmd_size = 10;
1713 else if ((softc->minimum_cmd_size > 10)
1714 && (softc->minimum_cmd_size <= 12))
1715 softc->minimum_cmd_size = 12;
1716 else if (softc->minimum_cmd_size > 12)
1717 softc->minimum_cmd_size = 16;
1719 /* Predict whether device may support READ CAPACITY(16). */
1720 if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3) {
1721 softc->flags |= DA_FLAG_CAN_RC16;
1722 softc->state = DA_STATE_PROBE2;
1726 * Register this media as a disk.
1728 softc->disk = disk_alloc();
1729 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
1730 periph->unit_number, 0,
1731 DEVSTAT_BS_UNAVAILABLE,
1732 SID_TYPE(&cgd->inq_data) |
1733 XPORT_DEVSTAT_TYPE(cpi.transport),
1734 DEVSTAT_PRIORITY_DISK);
1735 softc->disk->d_open = daopen;
1736 softc->disk->d_close = daclose;
1737 softc->disk->d_strategy = dastrategy;
1738 softc->disk->d_dump = dadump;
1739 softc->disk->d_getattr = dagetattr;
1740 softc->disk->d_gone = dadiskgonecb;
1741 softc->disk->d_name = "da";
1742 softc->disk->d_drv1 = periph;
1744 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */
1745 else if (cpi.maxio > MAXPHYS)
1746 softc->disk->d_maxsize = MAXPHYS; /* for safety */
1748 softc->disk->d_maxsize = cpi.maxio;
1749 softc->disk->d_unit = periph->unit_number;
1750 softc->disk->d_flags = 0;
1751 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
1752 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
1753 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
1754 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
1755 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
1756 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
1757 cgd->inq_data.product, sizeof(cgd->inq_data.product),
1758 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
1759 softc->disk->d_hba_vendor = cpi.hba_vendor;
1760 softc->disk->d_hba_device = cpi.hba_device;
1761 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
1762 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
1765 * Acquire a reference to the periph before we register with GEOM.
1766 * We'll release this reference once GEOM calls us back (via
1767 * dadiskgonecb()) telling us that our provider has been freed.
1769 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1770 xpt_print(periph->path, "%s: lost periph during "
1771 "registration!\n", __func__);
1772 mtx_lock(periph->sim->mtx);
1773 return (CAM_REQ_CMP_ERR);
1776 disk_create(softc->disk, DISK_VERSION);
1777 mtx_lock(periph->sim->mtx);
1780 * Add async callbacks for events of interest.
1781 * I don't bother checking if this fails as,
1782 * in most cases, the system will function just
1783 * fine without them and the only alternative
1784 * would be to not attach the device on failure.
1786 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
1787 AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION,
1788 daasync, periph, periph->path);
1791 * Emit an attribute changed notification just in case
1792 * physical path information arrived before our async
1793 * event handler was registered, but after anyone attaching
1794 * to our disk device polled it.
1796 disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
1799 * Schedule a periodic media polling events.
1801 callout_init_mtx(&softc->mediapoll_c, periph->sim->mtx, 0);
1802 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
1803 (cgd->inq_flags & SID_AEN) == 0 &&
1804 da_poll_period != 0)
1805 callout_reset(&softc->mediapoll_c, da_poll_period * hz,
1806 damediapoll, periph);
1808 xpt_schedule(periph, CAM_PRIORITY_DEV);
1810 return(CAM_REQ_CMP);
1814 dastart(struct cam_periph *periph, union ccb *start_ccb)
1816 struct da_softc *softc;
1818 softc = (struct da_softc *)periph->softc;
1820 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
1822 switch (softc->state) {
1823 case DA_STATE_NORMAL:
1825 struct bio *bp, *bp1;
1828 /* Execute immediate CCB if waiting. */
1829 if (periph->immediate_priority <= periph->pinfo.priority) {
1830 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1831 ("queuing for immediate ccb\n"));
1832 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1833 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1835 periph->immediate_priority = CAM_PRIORITY_NONE;
1836 wakeup(&periph->ccb_list);
1837 /* May have more work to do, so ensure we stay scheduled */
1842 /* Run BIO_DELETE if not running yet. */
1843 if (!softc->delete_running &&
1844 (bp = bioq_first(&softc->delete_queue)) != NULL) {
1848 if (softc->delete_method == DA_DELETE_UNMAP) {
1849 uint8_t *buf = softc->unmap_buf;
1850 uint64_t lastlba = (uint64_t)-1;
1851 uint32_t lastcount = 0;
1852 int blocks = 0, off, ranges = 0;
1854 softc->delete_running = 1;
1855 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
1858 bioq_remove(&softc->delete_queue, bp1);
1860 bioq_insert_tail(&softc->delete_run_queue, bp1);
1861 lba = bp1->bio_pblkno;
1862 count = bp1->bio_bcount / softc->params.secsize;
1864 /* Try to extend the previous range. */
1865 if (lba == lastlba) {
1867 off = (ranges - 1) * 16 + 8;
1868 scsi_ulto4b(lastcount, &buf[off + 8]);
1869 } else if (count > 0) {
1870 off = ranges * 16 + 8;
1871 scsi_u64to8b(lba, &buf[off + 0]);
1872 scsi_ulto4b(count, &buf[off + 8]);
1877 lastlba = lba + count;
1878 bp1 = bioq_first(&softc->delete_queue);
1880 ranges >= softc->unmap_max_ranges ||
1881 blocks + bp1->bio_bcount /
1882 softc->params.secsize > softc->unmap_max_lba)
1885 scsi_ulto2b(ranges * 16 + 6, &buf[0]);
1886 scsi_ulto2b(ranges * 16, &buf[2]);
1888 scsi_unmap(&start_ccb->csio,
1889 /*retries*/da_retry_count,
1891 /*tag_action*/MSG_SIMPLE_Q_TAG,
1894 /*dxfer_len*/ ranges * 16 + 8,
1895 /*sense_len*/SSD_FULL_SIZE,
1896 da_default_timeout * 1000);
1897 start_ccb->ccb_h.ccb_state = DA_CCB_DELETE;
1899 } else if (softc->delete_method == DA_DELETE_ZERO ||
1900 softc->delete_method == DA_DELETE_WS10 ||
1901 softc->delete_method == DA_DELETE_WS16) {
1902 softc->delete_running = 1;
1903 lba = bp->bio_pblkno;
1907 bioq_remove(&softc->delete_queue, bp1);
1909 bioq_insert_tail(&softc->delete_run_queue, bp1);
1910 count += bp1->bio_bcount / softc->params.secsize;
1911 bp1 = bioq_first(&softc->delete_queue);
1913 lba + count != bp1->bio_pblkno ||
1914 count + bp1->bio_bcount /
1915 softc->params.secsize > 0xffff)
1919 scsi_write_same(&start_ccb->csio,
1920 /*retries*/da_retry_count,
1922 /*tag_action*/MSG_SIMPLE_Q_TAG,
1923 /*byte2*/softc->delete_method ==
1924 DA_DELETE_ZERO ? 0 : SWS_UNMAP,
1925 softc->delete_method ==
1926 DA_DELETE_WS16 ? 16 : 10,
1928 /*block_count*/count,
1929 /*data_ptr*/ __DECONST(void *,
1931 /*dxfer_len*/ softc->params.secsize,
1932 /*sense_len*/SSD_FULL_SIZE,
1933 da_default_timeout * 1000);
1934 start_ccb->ccb_h.ccb_state = DA_CCB_DELETE;
1937 bioq_flush(&softc->delete_queue, NULL, 0);
1942 /* Run regular command. */
1943 bp = bioq_takefirst(&softc->bio_queue);
1947 scsi_test_unit_ready(&start_ccb->csio,
1948 /*retries*/ da_retry_count,
1952 da_default_timeout * 1000);
1953 start_ccb->ccb_h.ccb_bp = NULL;
1954 start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
1955 xpt_action(start_ccb);
1957 xpt_release_ccb(start_ccb);
1962 cam_periph_release_locked(periph);
1965 if ((bp->bio_flags & BIO_ORDERED) != 0 ||
1966 (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1967 softc->flags &= ~DA_FLAG_NEED_OTAG;
1968 softc->ordered_tag_count++;
1969 tag_code = MSG_ORDERED_Q_TAG;
1971 tag_code = MSG_SIMPLE_Q_TAG;
1974 switch (bp->bio_cmd) {
1977 scsi_read_write(&start_ccb->csio,
1978 /*retries*/da_retry_count,
1980 /*tag_action*/tag_code,
1981 /*read_op*/bp->bio_cmd
1984 softc->minimum_cmd_size,
1985 /*lba*/bp->bio_pblkno,
1986 /*block_count*/bp->bio_bcount /
1987 softc->params.secsize,
1988 /*data_ptr*/ bp->bio_data,
1989 /*dxfer_len*/ bp->bio_bcount,
1990 /*sense_len*/SSD_FULL_SIZE,
1991 da_default_timeout * 1000);
1995 * BIO_FLUSH doesn't currently communicate
1996 * range data, so we synchronize the cache
1997 * over the whole disk. We also force
1998 * ordered tag semantics the flush applies
1999 * to all previously queued I/O.
2001 scsi_synchronize_cache(&start_ccb->csio,
2008 da_default_timeout*1000);
2011 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
2015 * Block out any asyncronous callbacks
2016 * while we touch the pending ccb list.
2018 LIST_INSERT_HEAD(&softc->pending_ccbs,
2019 &start_ccb->ccb_h, periph_links.le);
2020 softc->outstanding_cmds++;
2022 /* We expect a unit attention from this device */
2023 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
2024 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
2025 softc->flags &= ~DA_FLAG_RETRY_UA;
2028 start_ccb->ccb_h.ccb_bp = bp;
2029 xpt_action(start_ccb);
2031 /* May have more work to do, so ensure we stay scheduled */
2035 case DA_STATE_PROBE:
2037 struct scsi_read_capacity_data *rcap;
2039 rcap = (struct scsi_read_capacity_data *)
2040 malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
2042 printf("dastart: Couldn't malloc read_capacity data\n");
2043 /* da_free_periph??? */
2046 scsi_read_capacity(&start_ccb->csio,
2047 /*retries*/da_retry_count,
2053 start_ccb->ccb_h.ccb_bp = NULL;
2054 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
2055 xpt_action(start_ccb);
2058 case DA_STATE_PROBE2:
2060 struct scsi_read_capacity_data_long *rcaplong;
2062 rcaplong = (struct scsi_read_capacity_data_long *)
2063 malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
2064 if (rcaplong == NULL) {
2065 printf("dastart: Couldn't malloc read_capacity data\n");
2066 /* da_free_periph??? */
2069 scsi_read_capacity_16(&start_ccb->csio,
2070 /*retries*/ da_retry_count,
2072 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2077 /*sense_len*/ SSD_FULL_SIZE,
2078 /*timeout*/ da_default_timeout * 1000);
2079 start_ccb->ccb_h.ccb_bp = NULL;
2080 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
2081 xpt_action(start_ccb);
2088 cmd6workaround(union ccb *ccb)
2090 struct scsi_rw_6 cmd6;
2091 struct scsi_rw_10 *cmd10;
2092 struct da_softc *softc;
2097 cdb = ccb->csio.cdb_io.cdb_bytes;
2098 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
2100 if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
2101 if (softc->delete_method == DA_DELETE_UNMAP) {
2102 xpt_print(ccb->ccb_h.path, "UNMAP is not supported, "
2103 "switching to WRITE SAME(16) with UNMAP.\n");
2104 dadeletemethodset(softc, DA_DELETE_WS16);
2105 } else if (softc->delete_method == DA_DELETE_WS16) {
2106 xpt_print(ccb->ccb_h.path,
2107 "WRITE SAME(16) with UNMAP is not supported, "
2108 "disabling BIO_DELETE.\n");
2109 dadeletemethodset(softc, DA_DELETE_DISABLE);
2110 } else if (softc->delete_method == DA_DELETE_WS10) {
2111 xpt_print(ccb->ccb_h.path,
2112 "WRITE SAME(10) with UNMAP is not supported, "
2113 "disabling BIO_DELETE.\n");
2114 dadeletemethodset(softc, DA_DELETE_DISABLE);
2115 } else if (softc->delete_method == DA_DELETE_ZERO) {
2116 xpt_print(ccb->ccb_h.path,
2117 "WRITE SAME(10) is not supported, "
2118 "disabling BIO_DELETE.\n");
2119 dadeletemethodset(softc, DA_DELETE_DISABLE);
2121 dadeletemethodset(softc, DA_DELETE_DISABLE);
2122 while ((bp = bioq_takefirst(&softc->delete_run_queue))
2124 bioq_disksort(&softc->delete_queue, bp);
2125 bioq_insert_tail(&softc->delete_queue,
2126 (struct bio *)ccb->ccb_h.ccb_bp);
2127 ccb->ccb_h.ccb_bp = NULL;
2131 /* Translation only possible if CDB is an array and cmd is R/W6 */
2132 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
2133 (*cdb != READ_6 && *cdb != WRITE_6))
2136 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
2137 "increasing minimum_cmd_size to 10.\n");
2138 softc->minimum_cmd_size = 10;
2140 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
2141 cmd10 = (struct scsi_rw_10 *)cdb;
2142 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
2144 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
2145 cmd10->reserved = 0;
2146 scsi_ulto2b(cmd6.length, cmd10->length);
2147 cmd10->control = cmd6.control;
2148 ccb->csio.cdb_len = sizeof(*cmd10);
2150 /* Requeue request, unfreezing queue if necessary */
2151 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
2152 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2155 cam_release_devq(ccb->ccb_h.path,
2159 /*getcount_only*/0);
2165 dadone(struct cam_periph *periph, union ccb *done_ccb)
2167 struct da_softc *softc;
2168 struct ccb_scsiio *csio;
2171 softc = (struct da_softc *)periph->softc;
2172 priority = done_ccb->ccb_h.pinfo.priority;
2174 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
2176 csio = &done_ccb->csio;
2177 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
2178 case DA_CCB_BUFFER_IO:
2181 struct bio *bp, *bp1;
2183 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2184 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2188 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
2193 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
2194 if (error == ERESTART) {
2196 * A retry was scheuled, so
2201 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2206 * return all queued I/O with EIO, so that
2207 * the client can retry these I/Os in the
2208 * proper order should it attempt to recover.
2213 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
2215 * Catastrophic error. Mark our pack as
2219 * XXX See if this is really a media
2222 xpt_print(periph->path,
2223 "Invalidating pack\n");
2224 softc->flags |= DA_FLAG_PACK_INVALID;
2225 queued_error = ENXIO;
2227 bioq_flush(&softc->bio_queue, NULL,
2230 bp->bio_error = error;
2231 bp->bio_resid = bp->bio_bcount;
2232 bp->bio_flags |= BIO_ERROR;
2234 } else if (bp != NULL) {
2235 bp->bio_resid = csio->resid;
2237 if (bp->bio_resid != 0)
2238 bp->bio_flags |= BIO_ERROR;
2240 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2241 cam_release_devq(done_ccb->ccb_h.path,
2245 /*getcount_only*/0);
2246 } else if (bp != NULL) {
2247 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2248 panic("REQ_CMP with QFRZN");
2249 bp->bio_resid = csio->resid;
2250 if (csio->resid > 0)
2251 bp->bio_flags |= BIO_ERROR;
2252 if (softc->error_inject != 0) {
2253 bp->bio_error = softc->error_inject;
2254 bp->bio_resid = bp->bio_bcount;
2255 bp->bio_flags |= BIO_ERROR;
2256 softc->error_inject = 0;
2262 * Block out any asyncronous callbacks
2263 * while we touch the pending ccb list.
2265 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
2266 softc->outstanding_cmds--;
2267 if (softc->outstanding_cmds == 0)
2268 softc->flags |= DA_FLAG_WENT_IDLE;
2270 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
2271 xpt_print(periph->path, "oustanding %d\n",
2272 softc->outstanding_cmds);
2275 if ((csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) ==
2277 while ((bp1 = bioq_takefirst(&softc->delete_run_queue))
2279 bp1->bio_resid = bp->bio_resid;
2280 bp1->bio_error = bp->bio_error;
2281 if (bp->bio_flags & BIO_ERROR)
2282 bp1->bio_flags |= BIO_ERROR;
2285 softc->delete_running = 0;
2289 } else if (bp != NULL)
2296 struct scsi_read_capacity_data *rdcap;
2297 struct scsi_read_capacity_data_long *rcaplong;
2298 char announce_buf[80];
2302 if (softc->state == DA_STATE_PROBE)
2303 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
2305 rcaplong = (struct scsi_read_capacity_data_long *)
2308 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
2309 struct disk_params *dp;
2310 uint32_t block_size;
2312 u_int lbppbe; /* LB per physical block exponent. */
2313 u_int lalba; /* Lowest aligned LBA. */
2315 if (softc->state == DA_STATE_PROBE) {
2316 block_size = scsi_4btoul(rdcap->length);
2317 maxsector = scsi_4btoul(rdcap->addr);
2322 * According to SBC-2, if the standard 10
2323 * byte READ CAPACITY command returns 2^32,
2324 * we should issue the 16 byte version of
2325 * the command, since the device in question
2326 * has more sectors than can be represented
2327 * with the short version of the command.
2329 if (maxsector == 0xffffffff) {
2330 softc->state = DA_STATE_PROBE2;
2331 free(rdcap, M_SCSIDA);
2332 xpt_release_ccb(done_ccb);
2333 xpt_schedule(periph, priority);
2337 block_size = scsi_4btoul(rcaplong->length);
2338 maxsector = scsi_8btou64(rcaplong->addr);
2339 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
2340 lalba = scsi_2btoul(rcaplong->lalba_lbp);
2344 * Because GEOM code just will panic us if we
2345 * give them an 'illegal' value we'll avoid that
2348 if (block_size == 0 && maxsector == 0) {
2349 snprintf(announce_buf, sizeof(announce_buf),
2351 } else if (block_size >= MAXPHYS || block_size == 0) {
2352 xpt_print(periph->path,
2353 "unsupportable block size %ju\n",
2354 (uintmax_t) block_size);
2355 announce_buf[0] = '\0';
2356 cam_periph_invalidate(periph);
2359 * We pass rcaplong into dasetgeom(),
2360 * because it will only use it if it is
2363 dasetgeom(periph, block_size, maxsector,
2364 rcaplong, sizeof(*rcaplong));
2365 if ((lalba & SRC16_LBPME_A)
2366 && softc->delete_method == DA_DELETE_NONE)
2367 dadeletemethodset(softc, DA_DELETE_UNMAP);
2368 dp = &softc->params;
2369 snprintf(announce_buf, sizeof(announce_buf),
2370 "%juMB (%ju %u byte sectors: %dH %dS/T "
2372 (((uintmax_t)dp->secsize *
2373 dp->sectors) / (1024*1024)),
2374 (uintmax_t)dp->sectors,
2375 dp->secsize, dp->heads,
2376 dp->secs_per_track, dp->cylinders);
2381 announce_buf[0] = '\0';
2384 * Retry any UNIT ATTENTION type errors. They
2385 * are expected at boot.
2387 error = daerror(done_ccb, CAM_RETRY_SELTO,
2388 SF_RETRY_UA|SF_NO_PRINT);
2389 if (error == ERESTART) {
2391 * A retry was scheuled, so
2395 } else if (error != 0) {
2397 int sense_key, error_code;
2400 struct ccb_getdev cgd;
2402 /* Don't wedge this device's queue */
2403 status = done_ccb->ccb_h.status;
2404 if ((status & CAM_DEV_QFRZN) != 0)
2405 cam_release_devq(done_ccb->ccb_h.path,
2409 /*getcount_only*/0);
2412 xpt_setup_ccb(&cgd.ccb_h,
2413 done_ccb->ccb_h.path,
2414 CAM_PRIORITY_NORMAL);
2415 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2416 xpt_action((union ccb *)&cgd);
2418 if (scsi_extract_sense_ccb(done_ccb,
2419 &error_code, &sense_key, &asc, &ascq))
2425 * If we tried READ CAPACITY(16) and failed,
2426 * fallback to READ CAPACITY(10).
2428 if ((softc->state == DA_STATE_PROBE2) &&
2429 (softc->flags & DA_FLAG_CAN_RC16) &&
2430 (((csio->ccb_h.status & CAM_STATUS_MASK) ==
2433 (error_code == SSD_CURRENT_ERROR) &&
2434 (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
2435 softc->flags &= ~DA_FLAG_CAN_RC16;
2436 softc->state = DA_STATE_PROBE;
2437 free(rdcap, M_SCSIDA);
2438 xpt_release_ccb(done_ccb);
2439 xpt_schedule(periph, priority);
2443 * Attach to anything that claims to be a
2444 * direct access or optical disk device,
2445 * as long as it doesn't return a "Logical
2446 * unit not supported" (0x25) error.
2448 if ((have_sense) && (asc != 0x25)
2449 && (error_code == SSD_CURRENT_ERROR)) {
2450 const char *sense_key_desc;
2451 const char *asc_desc;
2453 scsi_sense_desc(sense_key, asc, ascq,
2457 snprintf(announce_buf,
2458 sizeof(announce_buf),
2459 "Attempt to query device "
2460 "size failed: %s, %s",
2468 xpt_print(periph->path,
2469 "got CAM status %#x\n",
2470 done_ccb->ccb_h.status);
2473 xpt_print(periph->path, "fatal error, "
2474 "failed to attach to device\n");
2477 * Free up resources.
2479 cam_periph_invalidate(periph);
2483 free(csio->data_ptr, M_SCSIDA);
2484 if (announce_buf[0] != '\0' && ((softc->flags & DA_FLAG_PROBED) == 0)) {
2486 * Create our sysctl variables, now that we know
2487 * we have successfully attached.
2489 /* increase the refcount */
2490 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
2491 taskqueue_enqueue(taskqueue_thread,
2492 &softc->sysctl_task);
2493 xpt_announce_periph(periph, announce_buf);
2495 xpt_print(periph->path, "fatal error, "
2496 "could not acquire reference count\n");
2500 * Since our peripheral may be invalidated by an error
2501 * above or an external event, we must release our CCB
2502 * before releasing the probe lock on the peripheral.
2503 * The peripheral will only go away once the last lock
2504 * is removed, and we need it around for the CCB release
2507 xpt_release_ccb(done_ccb);
2508 softc->state = DA_STATE_NORMAL;
2509 wakeup(&softc->disk->d_mediasize);
2510 if ((softc->flags & DA_FLAG_PROBED) == 0) {
2511 softc->flags |= DA_FLAG_PROBED;
2512 cam_periph_unhold(periph);
2514 cam_periph_release_locked(periph);
2517 case DA_CCB_WAITING:
2519 /* Caller will release the CCB */
2520 wakeup(&done_ccb->ccb_h.cbfcnp);
2524 /* No-op. We're polling */
2528 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2530 if (daerror(done_ccb, CAM_RETRY_SELTO,
2531 SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
2534 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2535 cam_release_devq(done_ccb->ccb_h.path,
2539 /*getcount_only*/0);
2541 xpt_release_ccb(done_ccb);
2542 cam_periph_release_locked(periph);
2548 xpt_release_ccb(done_ccb);
2552 dareprobe(struct cam_periph *periph)
2554 struct da_softc *softc;
2557 softc = (struct da_softc *)periph->softc;
2559 /* Probe in progress; don't interfere. */
2560 if ((softc->flags & DA_FLAG_PROBED) == 0)
2563 status = cam_periph_acquire(periph);
2564 KASSERT(status == CAM_REQ_CMP,
2565 ("dareprobe: cam_periph_acquire failed"));
2567 if (softc->flags & DA_FLAG_CAN_RC16)
2568 softc->state = DA_STATE_PROBE2;
2570 softc->state = DA_STATE_PROBE;
2572 xpt_schedule(periph, CAM_PRIORITY_DEV);
2576 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
2578 struct da_softc *softc;
2579 struct cam_periph *periph;
2580 int error, error_code, sense_key, asc, ascq;
2582 periph = xpt_path_periph(ccb->ccb_h.path);
2583 softc = (struct da_softc *)periph->softc;
2586 * Automatically detect devices that do not support
2587 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
2590 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
2591 error = cmd6workaround(ccb);
2592 } else if (scsi_extract_sense_ccb(ccb,
2593 &error_code, &sense_key, &asc, &ascq)) {
2594 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
2595 error = cmd6workaround(ccb);
2597 * If the target replied with CAPACITY DATA HAS CHANGED UA,
2598 * query the capacity and notify upper layers.
2600 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
2601 asc == 0x2A && ascq == 0x09) {
2602 xpt_print(periph->path, "capacity data has changed\n");
2604 sense_flags |= SF_NO_PRINT;
2605 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
2606 asc == 0x28 && ascq == 0x00)
2607 disk_media_changed(softc->disk, M_NOWAIT);
2608 else if (sense_key == SSD_KEY_NOT_READY &&
2609 asc == 0x3a && (softc->flags & DA_FLAG_SAW_MEDIA)) {
2610 softc->flags &= ~DA_FLAG_SAW_MEDIA;
2611 disk_media_gone(softc->disk, M_NOWAIT);
2614 if (error == ERESTART)
2619 * Until we have a better way of doing pack validation,
2620 * don't treat UAs as errors.
2622 sense_flags |= SF_RETRY_UA;
2623 return(cam_periph_error(ccb, cam_flags, sense_flags,
2624 &softc->saved_ccb));
2628 damediapoll(void *arg)
2630 struct cam_periph *periph = arg;
2631 struct da_softc *softc = periph->softc;
2633 if (softc->state == DA_STATE_NORMAL && !softc->tur) {
2634 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
2639 /* Queue us up again */
2640 if (da_poll_period != 0)
2641 callout_schedule(&softc->mediapoll_c, da_poll_period * hz);
2645 daprevent(struct cam_periph *periph, int action)
2647 struct da_softc *softc;
2651 softc = (struct da_softc *)periph->softc;
2653 if (((action == PR_ALLOW)
2654 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
2655 || ((action == PR_PREVENT)
2656 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
2660 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2662 scsi_prevent(&ccb->csio,
2670 error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
2671 SF_RETRY_UA | SF_QUIET_IR, softc->disk->d_devstat);
2674 if (action == PR_ALLOW)
2675 softc->flags &= ~DA_FLAG_PACK_LOCKED;
2677 softc->flags |= DA_FLAG_PACK_LOCKED;
2680 xpt_release_ccb(ccb);
2684 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
2685 struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
2687 struct ccb_calc_geometry ccg;
2688 struct da_softc *softc;
2689 struct disk_params *dp;
2690 u_int lbppbe, lalba;
2692 softc = (struct da_softc *)periph->softc;
2694 dp = &softc->params;
2695 dp->secsize = block_len;
2696 dp->sectors = maxsector + 1;
2697 if (rcaplong != NULL) {
2698 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
2699 lalba = scsi_2btoul(rcaplong->lalba_lbp);
2700 lalba &= SRC16_LALBA_A;
2707 dp->stripesize = block_len << lbppbe;
2708 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
2710 } else if (softc->quirks & DA_Q_4K) {
2711 dp->stripesize = 4096;
2712 dp->stripeoffset = 0;
2715 dp->stripeoffset = 0;
2718 * Have the controller provide us with a geometry
2719 * for this disk. The only time the geometry
2720 * matters is when we boot and the controller
2721 * is the only one knowledgeable enough to come
2722 * up with something that will make this a bootable
2725 xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2726 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
2727 ccg.block_size = dp->secsize;
2728 ccg.volume_size = dp->sectors;
2730 ccg.secs_per_track = 0;
2732 xpt_action((union ccb*)&ccg);
2733 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2735 * We don't know what went wrong here- but just pick
2736 * a geometry so we don't have nasty things like divide
2740 dp->secs_per_track = 255;
2741 dp->cylinders = dp->sectors / (255 * 255);
2742 if (dp->cylinders == 0) {
2746 dp->heads = ccg.heads;
2747 dp->secs_per_track = ccg.secs_per_track;
2748 dp->cylinders = ccg.cylinders;
2752 * If the user supplied a read capacity buffer, and if it is
2753 * different than the previous buffer, update the data in the EDT.
2754 * If it's the same, we don't bother. This avoids sending an
2755 * update every time someone opens this device.
2757 if ((rcaplong != NULL)
2758 && (bcmp(rcaplong, &softc->rcaplong,
2759 min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
2760 struct ccb_dev_advinfo cdai;
2762 xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2763 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
2764 cdai.buftype = CDAI_TYPE_RCAPLONG;
2765 cdai.flags |= CDAI_FLAG_STORE;
2766 cdai.bufsiz = rcap_len;
2767 cdai.buf = (uint8_t *)rcaplong;
2768 xpt_action((union ccb *)&cdai);
2769 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
2770 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
2771 if (cdai.ccb_h.status != CAM_REQ_CMP) {
2772 xpt_print(periph->path, "%s: failed to set read "
2773 "capacity advinfo\n", __func__);
2774 /* Use cam_error_print() to decode the status */
2775 cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
2778 bcopy(rcaplong, &softc->rcaplong,
2779 min(sizeof(softc->rcaplong), rcap_len));
2783 softc->disk->d_sectorsize = softc->params.secsize;
2784 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
2785 softc->disk->d_stripesize = softc->params.stripesize;
2786 softc->disk->d_stripeoffset = softc->params.stripeoffset;
2787 /* XXX: these are not actually "firmware" values, so they may be wrong */
2788 softc->disk->d_fwsectors = softc->params.secs_per_track;
2789 softc->disk->d_fwheads = softc->params.heads;
2790 softc->disk->d_devstat->block_size = softc->params.secsize;
2791 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
2792 if (softc->delete_method > DA_DELETE_DISABLE)
2793 softc->disk->d_flags |= DISKFLAG_CANDELETE;
2795 softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
2799 dasendorderedtag(void *arg)
2801 struct da_softc *softc = arg;
2803 if (da_send_ordered) {
2804 if ((softc->ordered_tag_count == 0)
2805 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
2806 softc->flags |= DA_FLAG_NEED_OTAG;
2808 if (softc->outstanding_cmds > 0)
2809 softc->flags &= ~DA_FLAG_WENT_IDLE;
2811 softc->ordered_tag_count = 0;
2813 /* Queue us up again */
2814 callout_reset(&softc->sendordered_c,
2815 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2816 dasendorderedtag, softc);
2820 * Step through all DA peripheral drivers, and if the device is still open,
2821 * sync the disk cache to physical media.
2824 dashutdown(void * arg, int howto)
2826 struct cam_periph *periph;
2827 struct da_softc *softc;
2830 TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
2833 cam_periph_lock(periph);
2834 softc = (struct da_softc *)periph->softc;
2837 * We only sync the cache if the drive is still open, and
2838 * if the drive is capable of it..
2840 if (((softc->flags & DA_FLAG_OPEN) == 0)
2841 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
2842 cam_periph_unlock(periph);
2846 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2848 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
2849 scsi_synchronize_cache(&ccb.csio,
2853 /*begin_lba*/0, /* whole disk */
2858 xpt_polled_action(&ccb);
2860 error = cam_periph_error(&ccb,
2861 0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL);
2862 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
2863 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
2864 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
2866 xpt_print(periph->path, "Synchronize cache failed\n");
2867 cam_periph_unlock(periph);
2871 #else /* !_KERNEL */
2874 * XXX This is only left out of the kernel build to silence warnings. If,
2875 * for some reason this function is used in the kernel, the ifdefs should
2876 * be moved so it is included both in the kernel and userland.
2879 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2880 void (*cbfcnp)(struct cam_periph *, union ccb *),
2881 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2882 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2885 struct scsi_format_unit *scsi_cmd;
2887 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2888 scsi_cmd->opcode = FORMAT_UNIT;
2889 scsi_cmd->byte2 = byte2;
2890 scsi_ulto2b(ileave, scsi_cmd->interleave);
2895 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2904 #endif /* _KERNEL */