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;
149 int minimum_cmd_size;
151 int ordered_tag_count;
152 int outstanding_cmds;
153 int unmap_max_ranges;
157 da_delete_methods delete_method;
158 struct disk_params params;
161 struct task sysctl_task;
162 struct sysctl_ctx_list sysctl_ctx;
163 struct sysctl_oid *sysctl_tree;
164 struct callout sendordered_c;
166 uint8_t unmap_buf[UNMAP_MAX_RANGES * 16 + 8];
167 struct scsi_read_capacity_data_long rcaplong;
168 struct callout mediapoll_c;
171 struct da_quirk_entry {
172 struct scsi_inquiry_pattern inq_pat;
176 static const char quantum[] = "QUANTUM";
177 static const char microp[] = "MICROP";
179 static struct da_quirk_entry da_quirk_table[] =
181 /* SPI, FC devices */
184 * Fujitsu M2513A MO drives.
185 * Tested devices: M2513A2 firmware versions 1200 & 1300.
186 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
187 * Reported by: W.Scholten <whs@xs4all.nl>
189 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
190 /*quirks*/ DA_Q_NO_SYNC_CACHE
194 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
195 /*quirks*/ DA_Q_NO_SYNC_CACHE
199 * This particular Fujitsu drive doesn't like the
200 * synchronize cache command.
201 * Reported by: Tom Jackson <toj@gorilla.net>
203 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
204 /*quirks*/ DA_Q_NO_SYNC_CACHE
208 * This drive doesn't like the synchronize cache command
209 * either. Reported by: Matthew Jacob <mjacob@feral.com>
210 * in NetBSD PR kern/6027, August 24, 1998.
212 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
213 /*quirks*/ DA_Q_NO_SYNC_CACHE
217 * This drive doesn't like the synchronize cache command
218 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
221 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
222 /*quirks*/ DA_Q_NO_SYNC_CACHE
226 * Doesn't like the synchronize cache command.
227 * Reported by: Blaz Zupan <blaz@gold.amis.net>
229 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
230 /*quirks*/ DA_Q_NO_SYNC_CACHE
234 * Doesn't like the synchronize cache command.
235 * Reported by: Blaz Zupan <blaz@gold.amis.net>
237 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
238 /*quirks*/ DA_Q_NO_SYNC_CACHE
242 * Doesn't like the synchronize cache command.
244 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
245 /*quirks*/ DA_Q_NO_SYNC_CACHE
249 * Doesn't like the synchronize cache command.
250 * Reported by: walter@pelissero.de
252 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
253 /*quirks*/ DA_Q_NO_SYNC_CACHE
257 * Doesn't work correctly with 6 byte reads/writes.
258 * Returns illegal request, and points to byte 9 of the
260 * Reported by: Adam McDougall <bsdx@spawnet.com>
262 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
263 /*quirks*/ DA_Q_NO_6_BYTE
267 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
268 /*quirks*/ DA_Q_NO_6_BYTE
272 * Doesn't like the synchronize cache command.
273 * Reported by: walter@pelissero.de
275 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
276 /*quirks*/ DA_Q_NO_SYNC_CACHE
280 * The CISS RAID controllers do not support SYNC_CACHE
282 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
283 /*quirks*/ DA_Q_NO_SYNC_CACHE
285 /* USB mass storage devices supported by umass(4) */
288 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
291 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
292 /*quirks*/ DA_Q_NO_SYNC_CACHE
296 * Power Quotient Int. (PQI) USB flash key
299 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
300 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
304 * Creative Nomad MUVO mp3 player (USB)
307 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
308 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
312 * Jungsoft NEXDISK USB flash key
315 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
316 /*quirks*/ DA_Q_NO_SYNC_CACHE
320 * FreeDik USB Mini Data Drive
323 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
324 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
328 * Sigmatel USB Flash MP3 Player
331 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
332 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
336 * Neuros USB Digital Audio Computer
339 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
340 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
344 * SEAGRAND NP-900 MP3 Player
347 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
348 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
352 * iRiver iFP MP3 player (with UMS Firmware)
353 * PR: kern/54881, i386/63941, kern/66124
355 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
356 /*quirks*/ DA_Q_NO_SYNC_CACHE
360 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
363 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
364 /*quirks*/ DA_Q_NO_SYNC_CACHE
368 * ZICPlay USB MP3 Player with FM
371 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
372 /*quirks*/ DA_Q_NO_SYNC_CACHE
376 * TEAC USB floppy mechanisms
378 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
379 /*quirks*/ DA_Q_NO_SYNC_CACHE
383 * Kingston DataTraveler II+ USB Pen-Drive.
384 * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
386 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
387 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
395 {T_DIRECT, SIP_MEDIA_REMOVABLE, " ", "USB DISK Pro", "PMAP"},
396 /*quirks*/ DA_Q_NO_SYNC_CACHE
400 * Motorola E398 Mobile Phone (TransFlash memory card).
401 * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
404 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
405 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
409 * Qware BeatZkey! Pro
412 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
413 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
417 * Time DPA20B 1GB MP3 Player
420 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
421 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
425 * Samsung USB key 128Mb
428 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
429 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
433 * Kingston DataTraveler 2.0 USB Flash memory.
436 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
437 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
441 * Creative MUVO Slim mp3 player (USB)
444 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
445 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
449 * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
452 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
453 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
457 * SanDisk Micro Cruzer 128MB
460 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
461 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
465 * TOSHIBA TransMemory USB sticks
468 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
469 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
474 * PR: usb/75578, usb/72344, usb/65436
476 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
477 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
481 * Genesys 6-in-1 Card Reader
484 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
485 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
489 * Rekam Digital CAMERA
492 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
493 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
497 * iRiver H10 MP3 player
500 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
501 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
505 * iRiver U10 MP3 player
508 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
509 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
516 {T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
517 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
521 * EasyMP3 EM732X USB 2.0 Flash MP3 Player
524 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
525 "1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
532 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
533 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
537 * Philips USB Key Audio KEY013
540 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
541 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
548 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
549 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
556 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
557 /*quirks*/ DA_Q_NO_SYNC_CACHE
561 * I/O Magic USB flash - Giga Bank
564 {T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
565 /*quirks*/ DA_Q_NO_SYNC_CACHE
569 * JoyFly 128mb USB Flash Drive
572 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
573 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
580 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
581 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
585 * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A
588 {T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*",
589 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
593 * Samsung YP-U3 mp3-player
596 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3",
597 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
600 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
601 "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
605 * Sony Cyber-Shot DSC cameras
608 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
609 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
612 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler G3",
613 "1.00"}, /*quirks*/ DA_Q_NO_PREVENT
615 /* ATA/SATA devices over SAS/USB/... */
617 /* Hitachi Advanced Format (4k) drives */
618 { T_DIRECT, SIP_MEDIA_FIXED, "Hitachi", "H??????????E3*", "*" },
622 /* Samsung Advanced Format (4k) drives */
623 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD155UI*", "*" },
627 /* Samsung Advanced Format (4k) drives */
628 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD155UI*", "*" },
632 /* Samsung Advanced Format (4k) drives */
633 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD204UI*", "*" },
637 /* Samsung Advanced Format (4k) drives */
638 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD204UI*", "*" },
642 /* Seagate Barracuda Green Advanced Format (4k) drives */
643 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DL*", "*" },
647 /* Seagate Barracuda Green Advanced Format (4k) drives */
648 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DL", "*", "*" },
652 /* Seagate Barracuda Green Advanced Format (4k) drives */
653 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???DM*", "*" },
657 /* Seagate Barracuda Green Advanced Format (4k) drives */
658 { T_DIRECT, SIP_MEDIA_FIXED, "ST???DM*", "*", "*" },
662 /* Seagate Barracuda Green Advanced Format (4k) drives */
663 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DM*", "*" },
667 /* Seagate Barracuda Green Advanced Format (4k) drives */
668 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DM", "*", "*" },
672 /* Seagate Momentus Advanced Format (4k) drives */
673 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500423AS*", "*" },
677 /* Seagate Momentus Advanced Format (4k) drives */
678 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "3AS*", "*" },
682 /* Seagate Momentus Advanced Format (4k) drives */
683 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500424AS*", "*" },
687 /* Seagate Momentus Advanced Format (4k) drives */
688 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "4AS*", "*" },
692 /* Seagate Momentus Advanced Format (4k) drives */
693 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640423AS*", "*" },
697 /* Seagate Momentus Advanced Format (4k) drives */
698 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "3AS*", "*" },
702 /* Seagate Momentus Advanced Format (4k) drives */
703 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640424AS*", "*" },
707 /* Seagate Momentus Advanced Format (4k) drives */
708 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "4AS*", "*" },
712 /* Seagate Momentus Advanced Format (4k) drives */
713 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750420AS*", "*" },
717 /* Seagate Momentus Advanced Format (4k) drives */
718 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "0AS*", "*" },
722 /* Seagate Momentus Advanced Format (4k) drives */
723 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750422AS*", "*" },
727 /* Seagate Momentus Advanced Format (4k) drives */
728 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "2AS*", "*" },
732 /* Seagate Momentus Advanced Format (4k) drives */
733 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750423AS*", "*" },
737 /* Seagate Momentus Advanced Format (4k) drives */
738 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "3AS*", "*" },
742 /* Seagate Momentus Thin Advanced Format (4k) drives */
743 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???LT*", "*" },
747 /* Seagate Momentus Thin Advanced Format (4k) drives */
748 { T_DIRECT, SIP_MEDIA_FIXED, "ST???LT*", "*", "*" },
752 /* WDC Caviar Green Advanced Format (4k) drives */
753 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RS*", "*" },
757 /* WDC Caviar Green Advanced Format (4k) drives */
758 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RS*", "*" },
762 /* WDC Caviar Green Advanced Format (4k) drives */
763 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RX*", "*" },
767 /* WDC Caviar Green Advanced Format (4k) drives */
768 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RX*", "*" },
772 /* WDC Caviar Green Advanced Format (4k) drives */
773 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RS*", "*" },
777 /* WDC Caviar Green Advanced Format (4k) drives */
778 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RS*", "*" },
782 /* WDC Caviar Green Advanced Format (4k) drives */
783 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RX*", "*" },
787 /* WDC Caviar Green Advanced Format (4k) drives */
788 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RX*", "*" },
792 /* WDC Scorpio Black Advanced Format (4k) drives */
793 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PKT*", "*" },
797 /* WDC Scorpio Black Advanced Format (4k) drives */
798 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PKT*", "*" },
802 /* WDC Scorpio Black Advanced Format (4k) drives */
803 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PKT*", "*" },
807 /* WDC Scorpio Black Advanced Format (4k) drives */
808 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PKT*", "*" },
812 /* WDC Scorpio Blue Advanced Format (4k) drives */
813 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PVT*", "*" },
817 /* WDC Scorpio Blue Advanced Format (4k) drives */
818 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PVT*", "*" },
822 /* WDC Scorpio Blue Advanced Format (4k) drives */
823 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PVT*", "*" },
827 /* WDC Scorpio Blue Advanced Format (4k) drives */
828 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PVT*", "*" },
833 * Olympus FE-210 camera
835 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "FE210*",
836 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
842 {T_DIRECT, SIP_MEDIA_REMOVABLE, "LG", "UP3S",
843 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
847 * Laser MP3-2GA13 MP3 player
849 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "(HS) Flash Disk",
850 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
854 * LaCie external 250GB Hard drive des by Porsche
855 * Submitted by: Ben Stuyts <ben@altesco.nl>
858 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HM250JI", "*"},
859 /*quirks*/ DA_Q_NO_SYNC_CACHE
863 static disk_strategy_t dastrategy;
864 static dumper_t dadump;
865 static periph_init_t dainit;
866 static void daasync(void *callback_arg, u_int32_t code,
867 struct cam_path *path, void *arg);
868 static void dasysctlinit(void *context, int pending);
869 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
870 static int dadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
871 static int dadeletemethodset(struct da_softc *softc,
872 da_delete_methods delete_method);
873 static periph_ctor_t daregister;
874 static periph_dtor_t dacleanup;
875 static periph_start_t dastart;
876 static periph_oninv_t daoninvalidate;
877 static void dadone(struct cam_periph *periph,
878 union ccb *done_ccb);
879 static int daerror(union ccb *ccb, u_int32_t cam_flags,
880 u_int32_t sense_flags);
881 static void daprevent(struct cam_periph *periph, int action);
882 static void dareprobe(struct cam_periph *periph);
883 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
885 struct scsi_read_capacity_data_long *rcaplong,
887 static timeout_t dasendorderedtag;
888 static void dashutdown(void *arg, int howto);
889 static timeout_t damediapoll;
891 #ifndef DA_DEFAULT_POLL_PERIOD
892 #define DA_DEFAULT_POLL_PERIOD 3
895 #ifndef DA_DEFAULT_TIMEOUT
896 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
899 #ifndef DA_DEFAULT_RETRY
900 #define DA_DEFAULT_RETRY 4
903 #ifndef DA_DEFAULT_SEND_ORDERED
904 #define DA_DEFAULT_SEND_ORDERED 1
907 #define DA_SIO (softc->sort_io_queue >= 0 ? \
908 softc->sort_io_queue : cam_sort_io_queues)
910 static int da_poll_period = DA_DEFAULT_POLL_PERIOD;
911 static int da_retry_count = DA_DEFAULT_RETRY;
912 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
913 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
915 static SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
916 "CAM Direct Access Disk driver");
917 SYSCTL_INT(_kern_cam_da, OID_AUTO, poll_period, CTLFLAG_RW,
918 &da_poll_period, 0, "Media polling period in seconds");
919 TUNABLE_INT("kern.cam.da.poll_period", &da_poll_period);
920 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
921 &da_retry_count, 0, "Normal I/O retry count");
922 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
923 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
924 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
925 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
926 SYSCTL_INT(_kern_cam_da, OID_AUTO, send_ordered, CTLFLAG_RW,
927 &da_send_ordered, 0, "Send Ordered Tags");
928 TUNABLE_INT("kern.cam.da.send_ordered", &da_send_ordered);
931 * DA_ORDEREDTAG_INTERVAL determines how often, relative
932 * to the default timeout, we check to see whether an ordered
933 * tagged transaction is appropriate to prevent simple tag
934 * starvation. Since we'd like to ensure that there is at least
935 * 1/2 of the timeout length left for a starved transaction to
936 * complete after we've sent an ordered tag, we must poll at least
937 * four times in every timeout period. This takes care of the worst
938 * case where a starved transaction starts during an interval that
939 * meets the requirement "don't send an ordered tag" test so it takes
940 * us two intervals to determine that a tag must be sent.
942 #ifndef DA_ORDEREDTAG_INTERVAL
943 #define DA_ORDEREDTAG_INTERVAL 4
946 static struct periph_driver dadriver =
949 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
952 PERIPHDRIVER_DECLARE(da, dadriver);
954 static MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
957 daopen(struct disk *dp)
959 struct cam_periph *periph;
960 struct da_softc *softc;
964 periph = (struct cam_periph *)dp->d_drv1;
965 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
969 cam_periph_lock(periph);
970 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
971 cam_periph_unlock(periph);
972 cam_periph_release(periph);
976 unit = periph->unit_number;
977 softc = (struct da_softc *)periph->softc;
978 softc->flags |= DA_FLAG_OPEN;
980 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
983 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
984 /* Invalidate our pack information. */
985 softc->flags &= ~DA_FLAG_PACK_INVALID;
990 /* Wait for the disk size update. */
991 error = cam_periph_sleep(periph, &softc->disk->d_mediasize, PRIBIO,
994 xpt_print(periph->path, "unable to retrieve capacity data");
996 if (periph->flags & CAM_PERIPH_INVALID ||
997 softc->disk->d_sectorsize == 0 ||
998 softc->disk->d_mediasize == 0)
1001 if (error == 0 && (softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1002 (softc->quirks & DA_Q_NO_PREVENT) == 0)
1003 daprevent(periph, PR_PREVENT);
1006 softc->flags |= DA_FLAG_SAW_MEDIA;
1008 cam_periph_unhold(periph);
1009 cam_periph_unlock(periph);
1012 softc->flags &= ~DA_FLAG_OPEN;
1013 cam_periph_release(periph);
1020 daclose(struct disk *dp)
1022 struct cam_periph *periph;
1023 struct da_softc *softc;
1025 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 if (softc->state != DA_STATE_NORMAL)
1087 /* Check if cam_periph_getccb() was called. */
1088 prio = periph->immediate_priority;
1090 /* Check if we have more work to do. */
1091 if (bioq_first(&softc->bio_queue) ||
1092 (!softc->delete_running && bioq_first(&softc->delete_queue)) ||
1094 prio = CAM_PRIORITY_NORMAL;
1097 /* Schedule CCB if any of above is true. */
1098 if (prio != CAM_PRIORITY_NONE)
1099 xpt_schedule(periph, prio);
1103 * Actually translate the requested transfer into one the physical driver
1104 * can understand. The transfer is described by a buf and will include
1105 * only one physical transfer.
1108 dastrategy(struct bio *bp)
1110 struct cam_periph *periph;
1111 struct da_softc *softc;
1113 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1114 softc = (struct da_softc *)periph->softc;
1116 cam_periph_lock(periph);
1119 * If the device has been made invalid, error out
1121 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1122 cam_periph_unlock(periph);
1123 biofinish(bp, NULL, ENXIO);
1127 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
1130 * Place it in the queue of disk activities for this disk
1132 if (bp->bio_cmd == BIO_DELETE) {
1133 if (bp->bio_bcount == 0)
1136 bioq_disksort(&softc->delete_queue, bp);
1138 bioq_insert_tail(&softc->delete_queue, bp);
1139 } else if (DA_SIO) {
1140 bioq_disksort(&softc->bio_queue, bp);
1142 bioq_insert_tail(&softc->bio_queue, bp);
1146 * Schedule ourselves for performing the work.
1149 cam_periph_unlock(periph);
1155 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1157 struct cam_periph *periph;
1158 struct da_softc *softc;
1160 struct ccb_scsiio csio;
1165 periph = dp->d_drv1;
1166 softc = (struct da_softc *)periph->softc;
1167 cam_periph_lock(periph);
1168 secsize = softc->params.secsize;
1170 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1171 cam_periph_unlock(periph);
1176 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1177 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1178 scsi_read_write(&csio,
1184 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1187 /*data_ptr*/(u_int8_t *) virtual,
1188 /*dxfer_len*/length,
1189 /*sense_len*/SSD_FULL_SIZE,
1190 da_default_timeout * 1000);
1191 xpt_polled_action((union ccb *)&csio);
1193 error = cam_periph_error((union ccb *)&csio,
1194 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1195 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1196 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1197 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1199 printf("Aborting dump due to I/O error.\n");
1200 cam_periph_unlock(periph);
1205 * Sync the disk cache contents to the physical media.
1207 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1209 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1210 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1211 scsi_synchronize_cache(&csio,
1215 /*begin_lba*/0,/* Cover the whole disk */
1219 xpt_polled_action((union ccb *)&csio);
1221 error = cam_periph_error((union ccb *)&csio,
1222 0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL);
1223 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1224 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1225 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1227 xpt_print(periph->path, "Synchronize cache failed\n");
1229 cam_periph_unlock(periph);
1234 dagetattr(struct bio *bp)
1237 struct cam_periph *periph;
1239 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1240 cam_periph_lock(periph);
1241 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1243 cam_periph_unlock(periph);
1245 bp->bio_completed = bp->bio_length;
1255 * Install a global async callback. This callback will
1256 * receive async callbacks like "new device found".
1258 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1260 if (status != CAM_REQ_CMP) {
1261 printf("da: Failed to attach master async callback "
1262 "due to status 0x%x!\n", status);
1263 } else if (da_send_ordered) {
1265 /* Register our shutdown event handler */
1266 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1267 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1268 printf("dainit: shutdown event registration failed!\n");
1273 * Callback from GEOM, called when it has finished cleaning up its
1277 dadiskgonecb(struct disk *dp)
1279 struct cam_periph *periph;
1281 periph = (struct cam_periph *)dp->d_drv1;
1282 cam_periph_release(periph);
1286 daoninvalidate(struct cam_periph *periph)
1288 struct da_softc *softc;
1290 softc = (struct da_softc *)periph->softc;
1293 * De-register any async callbacks.
1295 xpt_register_async(0, daasync, periph, periph->path);
1297 softc->flags |= DA_FLAG_PACK_INVALID;
1300 * Return all queued I/O with ENXIO.
1301 * XXX Handle any transactions queued to the card
1302 * with XPT_ABORT_CCB.
1304 bioq_flush(&softc->bio_queue, NULL, ENXIO);
1305 bioq_flush(&softc->delete_queue, NULL, ENXIO);
1308 * Tell GEOM that we've gone away, we'll get a callback when it is
1309 * done cleaning up its resources.
1311 disk_gone(softc->disk);
1313 xpt_print(periph->path, "lost device - %d outstanding, %d refs\n",
1314 softc->outstanding_cmds, periph->refcount);
1318 dacleanup(struct cam_periph *periph)
1320 struct da_softc *softc;
1322 softc = (struct da_softc *)periph->softc;
1324 xpt_print(periph->path, "removing device entry\n");
1325 cam_periph_unlock(periph);
1328 * If we can't free the sysctl tree, oh well...
1330 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1331 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1332 xpt_print(periph->path, "can't remove sysctl context\n");
1335 callout_drain(&softc->mediapoll_c);
1336 disk_destroy(softc->disk);
1337 callout_drain(&softc->sendordered_c);
1338 free(softc, M_DEVBUF);
1339 cam_periph_lock(periph);
1343 daasync(void *callback_arg, u_int32_t code,
1344 struct cam_path *path, void *arg)
1346 struct cam_periph *periph;
1347 struct da_softc *softc;
1349 periph = (struct cam_periph *)callback_arg;
1351 case AC_FOUND_DEVICE:
1353 struct ccb_getdev *cgd;
1356 cgd = (struct ccb_getdev *)arg;
1360 if (cgd->protocol != PROTO_SCSI)
1363 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1364 && SID_TYPE(&cgd->inq_data) != T_RBC
1365 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1369 * Allocate a peripheral instance for
1370 * this device and start the probe
1373 status = cam_periph_alloc(daregister, daoninvalidate,
1375 "da", CAM_PERIPH_BIO,
1376 cgd->ccb_h.path, daasync,
1377 AC_FOUND_DEVICE, cgd);
1379 if (status != CAM_REQ_CMP
1380 && status != CAM_REQ_INPROG)
1381 printf("daasync: Unable to attach to new device "
1382 "due to status 0x%x\n", status);
1385 case AC_ADVINFO_CHANGED:
1389 buftype = (uintptr_t)arg;
1390 if (buftype == CDAI_TYPE_PHYS_PATH) {
1391 struct da_softc *softc;
1393 softc = periph->softc;
1394 disk_attr_changed(softc->disk, "GEOM::physpath",
1399 case AC_UNIT_ATTENTION:
1402 int error_code, sense_key, asc, ascq;
1404 softc = (struct da_softc *)periph->softc;
1405 ccb = (union ccb *)arg;
1408 * Handle all UNIT ATTENTIONs except our own,
1409 * as they will be handled by daerror().
1411 if (xpt_path_periph(ccb->ccb_h.path) != periph &&
1412 scsi_extract_sense_ccb(ccb,
1413 &error_code, &sense_key, &asc, &ascq)) {
1414 if (asc == 0x2A && ascq == 0x09) {
1415 xpt_print(ccb->ccb_h.path,
1416 "capacity data has changed\n");
1418 } else if (asc == 0x28 && ascq == 0x00)
1419 disk_media_changed(softc->disk, M_NOWAIT);
1421 cam_periph_async(periph, code, path, arg);
1425 softc = (struct da_softc *)periph->softc;
1427 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
1436 struct ccb_hdr *ccbh;
1438 softc = (struct da_softc *)periph->softc;
1440 * Don't fail on the expected unit attention
1443 softc->flags |= DA_FLAG_RETRY_UA;
1444 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1445 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1451 cam_periph_async(periph, code, path, arg);
1455 dasysctlinit(void *context, int pending)
1457 struct cam_periph *periph;
1458 struct da_softc *softc;
1459 char tmpstr[80], tmpstr2[80];
1460 struct ccb_trans_settings cts;
1462 periph = (struct cam_periph *)context;
1464 * periph was held for us when this task was enqueued
1466 if (periph->flags & CAM_PERIPH_INVALID) {
1467 cam_periph_release(periph);
1471 softc = (struct da_softc *)periph->softc;
1472 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1473 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1475 sysctl_ctx_init(&softc->sysctl_ctx);
1476 softc->flags |= DA_FLAG_SCTX_INIT;
1477 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1478 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1479 CTLFLAG_RD, 0, tmpstr);
1480 if (softc->sysctl_tree == NULL) {
1481 printf("dasysctlinit: unable to allocate sysctl tree\n");
1482 cam_periph_release(periph);
1487 * Now register the sysctl handler, so the user can change the value on
1490 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1491 OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RW,
1492 softc, 0, dadeletemethodsysctl, "A",
1493 "BIO_DELETE execution method");
1494 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1495 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1496 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1497 "Minimum CDB size");
1498 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1499 OID_AUTO, "sort_io_queue", CTLFLAG_RW, &softc->sort_io_queue, 0,
1500 "Sort IO queue to try and optimise disk access patterns");
1502 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1503 SYSCTL_CHILDREN(softc->sysctl_tree),
1507 &softc->error_inject,
1509 "error_inject leaf");
1513 * Add some addressing info.
1515 memset(&cts, 0, sizeof (cts));
1516 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1517 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1518 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1519 cam_periph_lock(periph);
1520 xpt_action((union ccb *)&cts);
1521 cam_periph_unlock(periph);
1522 if (cts.ccb_h.status != CAM_REQ_CMP) {
1523 cam_periph_release(periph);
1526 if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
1527 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1528 if (fc->valid & CTS_FC_VALID_WWPN) {
1529 softc->wwpn = fc->wwpn;
1530 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1531 SYSCTL_CHILDREN(softc->sysctl_tree),
1532 OID_AUTO, "wwpn", CTLFLAG_RD,
1533 &softc->wwpn, "World Wide Port Name");
1536 cam_periph_release(periph);
1540 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1544 value = *(int *)arg1;
1546 error = sysctl_handle_int(oidp, &value, 0, req);
1549 || (req->newptr == NULL))
1553 * Acceptable values here are 6, 10, 12 or 16.
1557 else if ((value > 6)
1560 else if ((value > 10)
1563 else if (value > 12)
1566 *(int *)arg1 = value;
1572 dadeletemethodset(struct da_softc *softc, da_delete_methods delete_method)
1575 if (delete_method < 0 || delete_method > DA_DELETE_MAX)
1578 softc->delete_method = delete_method;
1580 if (softc->delete_method > DA_DELETE_DISABLE)
1581 softc->disk->d_flags |= DISKFLAG_CANDELETE;
1583 softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
1589 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
1593 struct da_softc *softc;
1594 int i, error, value;
1596 softc = (struct da_softc *)arg1;
1598 value = softc->delete_method;
1599 if (value < 0 || value > DA_DELETE_MAX)
1602 p = da_delete_method_names[value];
1603 strncpy(buf, p, sizeof(buf));
1604 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1605 if (error != 0 || req->newptr == NULL)
1607 for (i = 0; i <= DA_DELETE_MAX; i++) {
1608 if (strcmp(buf, da_delete_method_names[i]) != 0)
1610 return dadeletemethodset(softc, i);
1616 daregister(struct cam_periph *periph, void *arg)
1618 struct da_softc *softc;
1619 struct ccb_pathinq cpi;
1620 struct ccb_getdev *cgd;
1624 cgd = (struct ccb_getdev *)arg;
1626 printf("daregister: no getdev CCB, can't register device\n");
1627 return(CAM_REQ_CMP_ERR);
1630 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
1633 if (softc == NULL) {
1634 printf("daregister: Unable to probe new device. "
1635 "Unable to allocate softc\n");
1636 return(CAM_REQ_CMP_ERR);
1639 LIST_INIT(&softc->pending_ccbs);
1640 softc->state = DA_STATE_PROBE;
1641 bioq_init(&softc->bio_queue);
1642 bioq_init(&softc->delete_queue);
1643 bioq_init(&softc->delete_run_queue);
1644 if (SID_IS_REMOVABLE(&cgd->inq_data))
1645 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1646 softc->unmap_max_ranges = UNMAP_MAX_RANGES;
1647 softc->unmap_max_lba = 1024*1024*2;
1648 softc->sort_io_queue = -1;
1650 periph->softc = softc;
1653 * See if this device has any quirks.
1655 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1656 (caddr_t)da_quirk_table,
1657 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1658 sizeof(*da_quirk_table), scsi_inquiry_match);
1661 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1663 softc->quirks = DA_Q_NONE;
1665 /* Check if the SIM does not want 6 byte commands */
1666 bzero(&cpi, sizeof(cpi));
1667 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1668 cpi.ccb_h.func_code = XPT_PATH_INQ;
1669 xpt_action((union ccb *)&cpi);
1670 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1671 softc->quirks |= DA_Q_NO_6_BYTE;
1673 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1676 * Take an exclusive refcount on the periph while dastart is called
1677 * to finish the probe. The reference will be dropped in dadone at
1680 (void)cam_periph_hold(periph, PRIBIO);
1683 * Schedule a periodic event to occasionally send an
1684 * ordered tag to a device.
1686 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
1687 callout_reset(&softc->sendordered_c,
1688 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
1689 dasendorderedtag, softc);
1691 cam_periph_unlock(periph);
1693 * RBC devices don't have to support READ(6), only READ(10).
1695 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1696 softc->minimum_cmd_size = 10;
1698 softc->minimum_cmd_size = 6;
1701 * Load the user's default, if any.
1703 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1704 periph->unit_number);
1705 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1708 * 6, 10, 12 and 16 are the currently permissible values.
1710 if (softc->minimum_cmd_size < 6)
1711 softc->minimum_cmd_size = 6;
1712 else if ((softc->minimum_cmd_size > 6)
1713 && (softc->minimum_cmd_size <= 10))
1714 softc->minimum_cmd_size = 10;
1715 else if ((softc->minimum_cmd_size > 10)
1716 && (softc->minimum_cmd_size <= 12))
1717 softc->minimum_cmd_size = 12;
1718 else if (softc->minimum_cmd_size > 12)
1719 softc->minimum_cmd_size = 16;
1721 /* Predict whether device may support READ CAPACITY(16). */
1722 if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3) {
1723 softc->flags |= DA_FLAG_CAN_RC16;
1724 softc->state = DA_STATE_PROBE2;
1728 * Register this media as a disk.
1730 softc->disk = disk_alloc();
1731 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
1732 periph->unit_number, 0,
1733 DEVSTAT_BS_UNAVAILABLE,
1734 SID_TYPE(&cgd->inq_data) |
1735 XPORT_DEVSTAT_TYPE(cpi.transport),
1736 DEVSTAT_PRIORITY_DISK);
1737 softc->disk->d_open = daopen;
1738 softc->disk->d_close = daclose;
1739 softc->disk->d_strategy = dastrategy;
1740 softc->disk->d_dump = dadump;
1741 softc->disk->d_getattr = dagetattr;
1742 softc->disk->d_gone = dadiskgonecb;
1743 softc->disk->d_name = "da";
1744 softc->disk->d_drv1 = periph;
1746 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */
1747 else if (cpi.maxio > MAXPHYS)
1748 softc->disk->d_maxsize = MAXPHYS; /* for safety */
1750 softc->disk->d_maxsize = cpi.maxio;
1751 softc->disk->d_unit = periph->unit_number;
1752 softc->disk->d_flags = 0;
1753 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
1754 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
1755 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
1756 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
1757 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
1758 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
1759 cgd->inq_data.product, sizeof(cgd->inq_data.product),
1760 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
1761 softc->disk->d_hba_vendor = cpi.hba_vendor;
1762 softc->disk->d_hba_device = cpi.hba_device;
1763 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
1764 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
1767 * Acquire a reference to the periph before we register with GEOM.
1768 * We'll release this reference once GEOM calls us back (via
1769 * dadiskgonecb()) telling us that our provider has been freed.
1771 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1772 xpt_print(periph->path, "%s: lost periph during "
1773 "registration!\n", __func__);
1774 cam_periph_lock(periph);
1775 return (CAM_REQ_CMP_ERR);
1778 disk_create(softc->disk, DISK_VERSION);
1779 cam_periph_lock(periph);
1782 * Add async callbacks for events of interest.
1783 * I don't bother checking if this fails as,
1784 * in most cases, the system will function just
1785 * fine without them and the only alternative
1786 * would be to not attach the device on failure.
1788 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
1789 AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION,
1790 daasync, periph, periph->path);
1793 * Emit an attribute changed notification just in case
1794 * physical path information arrived before our async
1795 * event handler was registered, but after anyone attaching
1796 * to our disk device polled it.
1798 disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
1801 * Schedule a periodic media polling events.
1803 callout_init_mtx(&softc->mediapoll_c, periph->sim->mtx, 0);
1804 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
1805 (cgd->inq_flags & SID_AEN) == 0 &&
1806 da_poll_period != 0)
1807 callout_reset(&softc->mediapoll_c, da_poll_period * hz,
1808 damediapoll, periph);
1810 xpt_schedule(periph, CAM_PRIORITY_DEV);
1812 return(CAM_REQ_CMP);
1816 dastart(struct cam_periph *periph, union ccb *start_ccb)
1818 struct da_softc *softc;
1820 softc = (struct da_softc *)periph->softc;
1822 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
1824 switch (softc->state) {
1825 case DA_STATE_NORMAL:
1827 struct bio *bp, *bp1;
1830 /* Execute immediate CCB if waiting. */
1831 if (periph->immediate_priority <= periph->pinfo.priority) {
1832 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1833 ("queuing for immediate ccb\n"));
1834 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1835 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1837 periph->immediate_priority = CAM_PRIORITY_NONE;
1838 wakeup(&periph->ccb_list);
1839 /* May have more work to do, so ensure we stay scheduled */
1844 /* Run BIO_DELETE if not running yet. */
1845 if (!softc->delete_running &&
1846 (bp = bioq_first(&softc->delete_queue)) != NULL) {
1850 if (softc->delete_method == DA_DELETE_UNMAP) {
1851 uint8_t *buf = softc->unmap_buf;
1852 uint64_t lastlba = (uint64_t)-1;
1853 uint32_t lastcount = 0;
1854 int blocks = 0, off, ranges = 0;
1856 softc->delete_running = 1;
1857 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
1860 bioq_remove(&softc->delete_queue, bp1);
1862 bioq_insert_tail(&softc->delete_run_queue, bp1);
1863 lba = bp1->bio_pblkno;
1864 count = bp1->bio_bcount / softc->params.secsize;
1866 /* Try to extend the previous range. */
1867 if (lba == lastlba) {
1869 off = (ranges - 1) * 16 + 8;
1870 scsi_ulto4b(lastcount, &buf[off + 8]);
1871 } else if (count > 0) {
1872 off = ranges * 16 + 8;
1873 scsi_u64to8b(lba, &buf[off + 0]);
1874 scsi_ulto4b(count, &buf[off + 8]);
1879 lastlba = lba + count;
1880 bp1 = bioq_first(&softc->delete_queue);
1882 ranges >= softc->unmap_max_ranges ||
1883 blocks + bp1->bio_bcount /
1884 softc->params.secsize > softc->unmap_max_lba)
1887 scsi_ulto2b(ranges * 16 + 6, &buf[0]);
1888 scsi_ulto2b(ranges * 16, &buf[2]);
1890 scsi_unmap(&start_ccb->csio,
1891 /*retries*/da_retry_count,
1893 /*tag_action*/MSG_SIMPLE_Q_TAG,
1896 /*dxfer_len*/ ranges * 16 + 8,
1897 /*sense_len*/SSD_FULL_SIZE,
1898 da_default_timeout * 1000);
1899 start_ccb->ccb_h.ccb_state = DA_CCB_DELETE;
1901 } else if (softc->delete_method == DA_DELETE_ZERO ||
1902 softc->delete_method == DA_DELETE_WS10 ||
1903 softc->delete_method == DA_DELETE_WS16) {
1904 softc->delete_running = 1;
1905 lba = bp->bio_pblkno;
1909 bioq_remove(&softc->delete_queue, bp1);
1911 bioq_insert_tail(&softc->delete_run_queue, bp1);
1912 count += bp1->bio_bcount / softc->params.secsize;
1913 bp1 = bioq_first(&softc->delete_queue);
1915 lba + count != bp1->bio_pblkno ||
1916 count + bp1->bio_bcount /
1917 softc->params.secsize > 0xffff)
1921 scsi_write_same(&start_ccb->csio,
1922 /*retries*/da_retry_count,
1924 /*tag_action*/MSG_SIMPLE_Q_TAG,
1925 /*byte2*/softc->delete_method ==
1926 DA_DELETE_ZERO ? 0 : SWS_UNMAP,
1927 softc->delete_method ==
1928 DA_DELETE_WS16 ? 16 : 10,
1930 /*block_count*/count,
1931 /*data_ptr*/ __DECONST(void *,
1933 /*dxfer_len*/ softc->params.secsize,
1934 /*sense_len*/SSD_FULL_SIZE,
1935 da_default_timeout * 1000);
1936 start_ccb->ccb_h.ccb_state = DA_CCB_DELETE;
1939 bioq_flush(&softc->delete_queue, NULL, 0);
1944 /* Run regular command. */
1945 bp = bioq_takefirst(&softc->bio_queue);
1949 scsi_test_unit_ready(&start_ccb->csio,
1950 /*retries*/ da_retry_count,
1954 da_default_timeout * 1000);
1955 start_ccb->ccb_h.ccb_bp = NULL;
1956 start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
1957 xpt_action(start_ccb);
1959 xpt_release_ccb(start_ccb);
1964 cam_periph_release_locked(periph);
1967 if ((bp->bio_flags & BIO_ORDERED) != 0 ||
1968 (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1969 softc->flags &= ~DA_FLAG_NEED_OTAG;
1970 softc->ordered_tag_count++;
1971 tag_code = MSG_ORDERED_Q_TAG;
1973 tag_code = MSG_SIMPLE_Q_TAG;
1976 switch (bp->bio_cmd) {
1979 scsi_read_write(&start_ccb->csio,
1980 /*retries*/da_retry_count,
1982 /*tag_action*/tag_code,
1983 /*read_op*/bp->bio_cmd
1986 softc->minimum_cmd_size,
1987 /*lba*/bp->bio_pblkno,
1988 /*block_count*/bp->bio_bcount /
1989 softc->params.secsize,
1990 /*data_ptr*/ bp->bio_data,
1991 /*dxfer_len*/ bp->bio_bcount,
1992 /*sense_len*/SSD_FULL_SIZE,
1993 da_default_timeout * 1000);
1997 * BIO_FLUSH doesn't currently communicate
1998 * range data, so we synchronize the cache
1999 * over the whole disk. We also force
2000 * ordered tag semantics the flush applies
2001 * to all previously queued I/O.
2003 scsi_synchronize_cache(&start_ccb->csio,
2010 da_default_timeout*1000);
2013 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
2017 * Block out any asyncronous callbacks
2018 * while we touch the pending ccb list.
2020 LIST_INSERT_HEAD(&softc->pending_ccbs,
2021 &start_ccb->ccb_h, periph_links.le);
2022 softc->outstanding_cmds++;
2024 /* We expect a unit attention from this device */
2025 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
2026 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
2027 softc->flags &= ~DA_FLAG_RETRY_UA;
2030 start_ccb->ccb_h.ccb_bp = bp;
2031 xpt_action(start_ccb);
2033 /* May have more work to do, so ensure we stay scheduled */
2037 case DA_STATE_PROBE:
2039 struct scsi_read_capacity_data *rcap;
2041 rcap = (struct scsi_read_capacity_data *)
2042 malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
2044 printf("dastart: Couldn't malloc read_capacity data\n");
2045 /* da_free_periph??? */
2048 scsi_read_capacity(&start_ccb->csio,
2049 /*retries*/da_retry_count,
2055 start_ccb->ccb_h.ccb_bp = NULL;
2056 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
2057 xpt_action(start_ccb);
2060 case DA_STATE_PROBE2:
2062 struct scsi_read_capacity_data_long *rcaplong;
2064 rcaplong = (struct scsi_read_capacity_data_long *)
2065 malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
2066 if (rcaplong == NULL) {
2067 printf("dastart: Couldn't malloc read_capacity data\n");
2068 /* da_free_periph??? */
2071 scsi_read_capacity_16(&start_ccb->csio,
2072 /*retries*/ da_retry_count,
2074 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2079 /*sense_len*/ SSD_FULL_SIZE,
2080 /*timeout*/ da_default_timeout * 1000);
2081 start_ccb->ccb_h.ccb_bp = NULL;
2082 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
2083 xpt_action(start_ccb);
2090 cmd6workaround(union ccb *ccb)
2092 struct scsi_rw_6 cmd6;
2093 struct scsi_rw_10 *cmd10;
2094 struct da_softc *softc;
2099 cdb = ccb->csio.cdb_io.cdb_bytes;
2100 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
2102 if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
2103 if (softc->delete_method == DA_DELETE_UNMAP) {
2104 xpt_print(ccb->ccb_h.path, "UNMAP is not supported, "
2105 "switching to WRITE SAME(16) with UNMAP.\n");
2106 dadeletemethodset(softc, DA_DELETE_WS16);
2107 } else if (softc->delete_method == DA_DELETE_WS16) {
2108 xpt_print(ccb->ccb_h.path,
2109 "WRITE SAME(16) with UNMAP is not supported, "
2110 "disabling BIO_DELETE.\n");
2111 dadeletemethodset(softc, DA_DELETE_DISABLE);
2112 } else if (softc->delete_method == DA_DELETE_WS10) {
2113 xpt_print(ccb->ccb_h.path,
2114 "WRITE SAME(10) with UNMAP is not supported, "
2115 "disabling BIO_DELETE.\n");
2116 dadeletemethodset(softc, DA_DELETE_DISABLE);
2117 } else if (softc->delete_method == DA_DELETE_ZERO) {
2118 xpt_print(ccb->ccb_h.path,
2119 "WRITE SAME(10) is not supported, "
2120 "disabling BIO_DELETE.\n");
2121 dadeletemethodset(softc, DA_DELETE_DISABLE);
2123 dadeletemethodset(softc, DA_DELETE_DISABLE);
2126 while ((bp = bioq_takefirst(&softc->delete_run_queue))
2128 bioq_disksort(&softc->delete_queue, bp);
2130 while ((bp = bioq_takefirst(&softc->delete_run_queue))
2132 bioq_insert_tail(&softc->delete_queue, bp);
2134 bioq_insert_tail(&softc->delete_queue,
2135 (struct bio *)ccb->ccb_h.ccb_bp);
2136 ccb->ccb_h.ccb_bp = NULL;
2140 /* Translation only possible if CDB is an array and cmd is R/W6 */
2141 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
2142 (*cdb != READ_6 && *cdb != WRITE_6))
2145 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
2146 "increasing minimum_cmd_size to 10.\n");
2147 softc->minimum_cmd_size = 10;
2149 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
2150 cmd10 = (struct scsi_rw_10 *)cdb;
2151 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
2153 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
2154 cmd10->reserved = 0;
2155 scsi_ulto2b(cmd6.length, cmd10->length);
2156 cmd10->control = cmd6.control;
2157 ccb->csio.cdb_len = sizeof(*cmd10);
2159 /* Requeue request, unfreezing queue if necessary */
2160 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
2161 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2164 cam_release_devq(ccb->ccb_h.path,
2168 /*getcount_only*/0);
2174 dadone(struct cam_periph *periph, union ccb *done_ccb)
2176 struct da_softc *softc;
2177 struct ccb_scsiio *csio;
2181 softc = (struct da_softc *)periph->softc;
2182 priority = done_ccb->ccb_h.pinfo.priority;
2184 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
2186 csio = &done_ccb->csio;
2187 state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
2189 case DA_CCB_BUFFER_IO:
2192 struct bio *bp, *bp1;
2194 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2195 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2199 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
2204 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
2205 if (error == ERESTART) {
2207 * A retry was scheuled, so
2212 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2217 * return all queued I/O with EIO, so that
2218 * the client can retry these I/Os in the
2219 * proper order should it attempt to recover.
2224 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
2226 * Catastrophic error. Mark our pack as
2230 * XXX See if this is really a media
2233 xpt_print(periph->path,
2234 "Invalidating pack\n");
2235 softc->flags |= DA_FLAG_PACK_INVALID;
2236 queued_error = ENXIO;
2238 bioq_flush(&softc->bio_queue, NULL,
2241 bp->bio_error = error;
2242 bp->bio_resid = bp->bio_bcount;
2243 bp->bio_flags |= BIO_ERROR;
2245 } else if (bp != NULL) {
2246 bp->bio_resid = csio->resid;
2248 if (bp->bio_resid != 0)
2249 bp->bio_flags |= BIO_ERROR;
2251 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2252 cam_release_devq(done_ccb->ccb_h.path,
2256 /*getcount_only*/0);
2257 } else if (bp != NULL) {
2258 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2259 panic("REQ_CMP with QFRZN");
2260 bp->bio_resid = csio->resid;
2261 if (csio->resid > 0)
2262 bp->bio_flags |= BIO_ERROR;
2263 if (softc->error_inject != 0) {
2264 bp->bio_error = softc->error_inject;
2265 bp->bio_resid = bp->bio_bcount;
2266 bp->bio_flags |= BIO_ERROR;
2267 softc->error_inject = 0;
2273 * Block out any asyncronous callbacks
2274 * while we touch the pending ccb list.
2276 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
2277 softc->outstanding_cmds--;
2278 if (softc->outstanding_cmds == 0)
2279 softc->flags |= DA_FLAG_WENT_IDLE;
2281 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
2282 xpt_print(periph->path, "oustanding %d\n",
2283 softc->outstanding_cmds);
2286 if (state == DA_CCB_DELETE) {
2287 while ((bp1 = bioq_takefirst(&softc->delete_run_queue))
2289 bp1->bio_resid = bp->bio_resid;
2290 bp1->bio_error = bp->bio_error;
2291 if (bp->bio_flags & BIO_ERROR)
2292 bp1->bio_flags |= BIO_ERROR;
2295 softc->delete_running = 0;
2299 } else if (bp != NULL)
2306 struct scsi_read_capacity_data *rdcap;
2307 struct scsi_read_capacity_data_long *rcaplong;
2308 char announce_buf[80];
2312 if (state == DA_CCB_PROBE)
2313 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
2315 rcaplong = (struct scsi_read_capacity_data_long *)
2318 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
2319 struct disk_params *dp;
2320 uint32_t block_size;
2322 u_int lbppbe; /* LB per physical block exponent. */
2323 u_int lalba; /* Lowest aligned LBA. */
2325 if (state == DA_CCB_PROBE) {
2326 block_size = scsi_4btoul(rdcap->length);
2327 maxsector = scsi_4btoul(rdcap->addr);
2332 * According to SBC-2, if the standard 10
2333 * byte READ CAPACITY command returns 2^32,
2334 * we should issue the 16 byte version of
2335 * the command, since the device in question
2336 * has more sectors than can be represented
2337 * with the short version of the command.
2339 if (maxsector == 0xffffffff) {
2340 softc->state = DA_STATE_PROBE2;
2341 free(rdcap, M_SCSIDA);
2342 xpt_release_ccb(done_ccb);
2343 xpt_schedule(periph, priority);
2347 block_size = scsi_4btoul(rcaplong->length);
2348 maxsector = scsi_8btou64(rcaplong->addr);
2349 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
2350 lalba = scsi_2btoul(rcaplong->lalba_lbp);
2354 * Because GEOM code just will panic us if we
2355 * give them an 'illegal' value we'll avoid that
2358 if (block_size == 0 && maxsector == 0) {
2359 snprintf(announce_buf, sizeof(announce_buf),
2361 } else if (block_size >= MAXPHYS || block_size == 0) {
2362 xpt_print(periph->path,
2363 "unsupportable block size %ju\n",
2364 (uintmax_t) block_size);
2365 announce_buf[0] = '\0';
2366 cam_periph_invalidate(periph);
2369 * We pass rcaplong into dasetgeom(),
2370 * because it will only use it if it is
2373 dasetgeom(periph, block_size, maxsector,
2374 rcaplong, sizeof(*rcaplong));
2375 if ((lalba & SRC16_LBPME_A)
2376 && softc->delete_method == DA_DELETE_NONE)
2377 dadeletemethodset(softc, DA_DELETE_UNMAP);
2378 dp = &softc->params;
2379 snprintf(announce_buf, sizeof(announce_buf),
2380 "%juMB (%ju %u byte sectors: %dH %dS/T "
2382 (((uintmax_t)dp->secsize *
2383 dp->sectors) / (1024*1024)),
2384 (uintmax_t)dp->sectors,
2385 dp->secsize, dp->heads,
2386 dp->secs_per_track, dp->cylinders);
2391 announce_buf[0] = '\0';
2394 * Retry any UNIT ATTENTION type errors. They
2395 * are expected at boot.
2397 error = daerror(done_ccb, CAM_RETRY_SELTO,
2398 SF_RETRY_UA|SF_NO_PRINT);
2399 if (error == ERESTART) {
2401 * A retry was scheuled, so
2405 } else if (error != 0) {
2407 int sense_key, error_code;
2410 struct ccb_getdev cgd;
2412 /* Don't wedge this device's queue */
2413 status = done_ccb->ccb_h.status;
2414 if ((status & CAM_DEV_QFRZN) != 0)
2415 cam_release_devq(done_ccb->ccb_h.path,
2419 /*getcount_only*/0);
2422 xpt_setup_ccb(&cgd.ccb_h,
2423 done_ccb->ccb_h.path,
2424 CAM_PRIORITY_NORMAL);
2425 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2426 xpt_action((union ccb *)&cgd);
2428 if (scsi_extract_sense_ccb(done_ccb,
2429 &error_code, &sense_key, &asc, &ascq))
2435 * If we tried READ CAPACITY(16) and failed,
2436 * fallback to READ CAPACITY(10).
2438 if ((state == DA_CCB_PROBE2) &&
2439 (softc->flags & DA_FLAG_CAN_RC16) &&
2440 (((csio->ccb_h.status & CAM_STATUS_MASK) ==
2443 (error_code == SSD_CURRENT_ERROR) &&
2444 (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
2445 softc->flags &= ~DA_FLAG_CAN_RC16;
2446 softc->state = DA_STATE_PROBE;
2447 free(rdcap, M_SCSIDA);
2448 xpt_release_ccb(done_ccb);
2449 xpt_schedule(periph, priority);
2453 * Attach to anything that claims to be a
2454 * direct access or optical disk device,
2455 * as long as it doesn't return a "Logical
2456 * unit not supported" (0x25) error.
2458 if ((have_sense) && (asc != 0x25)
2459 && (error_code == SSD_CURRENT_ERROR)) {
2460 const char *sense_key_desc;
2461 const char *asc_desc;
2463 scsi_sense_desc(sense_key, asc, ascq,
2467 snprintf(announce_buf,
2468 sizeof(announce_buf),
2469 "Attempt to query device "
2470 "size failed: %s, %s",
2478 xpt_print(periph->path,
2479 "got CAM status %#x\n",
2480 done_ccb->ccb_h.status);
2483 xpt_print(periph->path, "fatal error, "
2484 "failed to attach to device\n");
2487 * Free up resources.
2489 cam_periph_invalidate(periph);
2493 free(csio->data_ptr, M_SCSIDA);
2494 if (announce_buf[0] != '\0' && ((softc->flags & DA_FLAG_PROBED) == 0)) {
2496 * Create our sysctl variables, now that we know
2497 * we have successfully attached.
2499 /* increase the refcount */
2500 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
2501 taskqueue_enqueue(taskqueue_thread,
2502 &softc->sysctl_task);
2503 xpt_announce_periph(periph, announce_buf);
2505 xpt_print(periph->path, "fatal error, "
2506 "could not acquire reference count\n");
2510 * Since our peripheral may be invalidated by an error
2511 * above or an external event, we must release our CCB
2512 * before releasing the probe lock on the peripheral.
2513 * The peripheral will only go away once the last lock
2514 * is removed, and we need it around for the CCB release
2517 xpt_release_ccb(done_ccb);
2518 softc->state = DA_STATE_NORMAL;
2520 wakeup(&softc->disk->d_mediasize);
2521 if ((softc->flags & DA_FLAG_PROBED) == 0) {
2522 softc->flags |= DA_FLAG_PROBED;
2523 cam_periph_unhold(periph);
2525 cam_periph_release_locked(periph);
2528 case DA_CCB_WAITING:
2530 /* Caller will release the CCB */
2531 wakeup(&done_ccb->ccb_h.cbfcnp);
2535 /* No-op. We're polling */
2539 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2541 if (daerror(done_ccb, CAM_RETRY_SELTO,
2542 SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
2545 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2546 cam_release_devq(done_ccb->ccb_h.path,
2550 /*getcount_only*/0);
2552 xpt_release_ccb(done_ccb);
2553 cam_periph_release_locked(periph);
2559 xpt_release_ccb(done_ccb);
2563 dareprobe(struct cam_periph *periph)
2565 struct da_softc *softc;
2568 softc = (struct da_softc *)periph->softc;
2570 /* Probe in progress; don't interfere. */
2571 if ((softc->flags & DA_FLAG_PROBED) == 0)
2574 status = cam_periph_acquire(periph);
2575 KASSERT(status == CAM_REQ_CMP,
2576 ("dareprobe: cam_periph_acquire failed"));
2578 if (softc->flags & DA_FLAG_CAN_RC16)
2579 softc->state = DA_STATE_PROBE2;
2581 softc->state = DA_STATE_PROBE;
2583 xpt_schedule(periph, CAM_PRIORITY_DEV);
2587 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
2589 struct da_softc *softc;
2590 struct cam_periph *periph;
2591 int error, error_code, sense_key, asc, ascq;
2593 periph = xpt_path_periph(ccb->ccb_h.path);
2594 softc = (struct da_softc *)periph->softc;
2597 * Automatically detect devices that do not support
2598 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
2601 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
2602 error = cmd6workaround(ccb);
2603 } else if (scsi_extract_sense_ccb(ccb,
2604 &error_code, &sense_key, &asc, &ascq)) {
2605 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
2606 error = cmd6workaround(ccb);
2608 * If the target replied with CAPACITY DATA HAS CHANGED UA,
2609 * query the capacity and notify upper layers.
2611 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
2612 asc == 0x2A && ascq == 0x09) {
2613 xpt_print(periph->path, "capacity data has changed\n");
2615 sense_flags |= SF_NO_PRINT;
2616 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
2617 asc == 0x28 && ascq == 0x00)
2618 disk_media_changed(softc->disk, M_NOWAIT);
2619 else if (sense_key == SSD_KEY_NOT_READY &&
2620 asc == 0x3a && (softc->flags & DA_FLAG_SAW_MEDIA)) {
2621 softc->flags &= ~DA_FLAG_SAW_MEDIA;
2622 disk_media_gone(softc->disk, M_NOWAIT);
2625 if (error == ERESTART)
2630 * Until we have a better way of doing pack validation,
2631 * don't treat UAs as errors.
2633 sense_flags |= SF_RETRY_UA;
2634 return(cam_periph_error(ccb, cam_flags, sense_flags,
2635 &softc->saved_ccb));
2639 damediapoll(void *arg)
2641 struct cam_periph *periph = arg;
2642 struct da_softc *softc = periph->softc;
2644 if (!softc->tur && softc->outstanding_cmds == 0) {
2645 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
2650 /* Queue us up again */
2651 if (da_poll_period != 0)
2652 callout_schedule(&softc->mediapoll_c, da_poll_period * hz);
2656 daprevent(struct cam_periph *periph, int action)
2658 struct da_softc *softc;
2662 softc = (struct da_softc *)periph->softc;
2664 if (((action == PR_ALLOW)
2665 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
2666 || ((action == PR_PREVENT)
2667 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
2671 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2673 scsi_prevent(&ccb->csio,
2681 error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
2682 SF_RETRY_UA | SF_QUIET_IR, softc->disk->d_devstat);
2685 if (action == PR_ALLOW)
2686 softc->flags &= ~DA_FLAG_PACK_LOCKED;
2688 softc->flags |= DA_FLAG_PACK_LOCKED;
2691 xpt_release_ccb(ccb);
2695 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
2696 struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
2698 struct ccb_calc_geometry ccg;
2699 struct da_softc *softc;
2700 struct disk_params *dp;
2701 u_int lbppbe, lalba;
2703 softc = (struct da_softc *)periph->softc;
2705 dp = &softc->params;
2706 dp->secsize = block_len;
2707 dp->sectors = maxsector + 1;
2708 if (rcaplong != NULL) {
2709 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
2710 lalba = scsi_2btoul(rcaplong->lalba_lbp);
2711 lalba &= SRC16_LALBA_A;
2718 dp->stripesize = block_len << lbppbe;
2719 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
2721 } else if (softc->quirks & DA_Q_4K) {
2722 dp->stripesize = 4096;
2723 dp->stripeoffset = 0;
2726 dp->stripeoffset = 0;
2729 * Have the controller provide us with a geometry
2730 * for this disk. The only time the geometry
2731 * matters is when we boot and the controller
2732 * is the only one knowledgeable enough to come
2733 * up with something that will make this a bootable
2736 xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2737 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
2738 ccg.block_size = dp->secsize;
2739 ccg.volume_size = dp->sectors;
2741 ccg.secs_per_track = 0;
2743 xpt_action((union ccb*)&ccg);
2744 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2746 * We don't know what went wrong here- but just pick
2747 * a geometry so we don't have nasty things like divide
2751 dp->secs_per_track = 255;
2752 dp->cylinders = dp->sectors / (255 * 255);
2753 if (dp->cylinders == 0) {
2757 dp->heads = ccg.heads;
2758 dp->secs_per_track = ccg.secs_per_track;
2759 dp->cylinders = ccg.cylinders;
2763 * If the user supplied a read capacity buffer, and if it is
2764 * different than the previous buffer, update the data in the EDT.
2765 * If it's the same, we don't bother. This avoids sending an
2766 * update every time someone opens this device.
2768 if ((rcaplong != NULL)
2769 && (bcmp(rcaplong, &softc->rcaplong,
2770 min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
2771 struct ccb_dev_advinfo cdai;
2773 xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2774 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
2775 cdai.buftype = CDAI_TYPE_RCAPLONG;
2776 cdai.flags |= CDAI_FLAG_STORE;
2777 cdai.bufsiz = rcap_len;
2778 cdai.buf = (uint8_t *)rcaplong;
2779 xpt_action((union ccb *)&cdai);
2780 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
2781 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
2782 if (cdai.ccb_h.status != CAM_REQ_CMP) {
2783 xpt_print(periph->path, "%s: failed to set read "
2784 "capacity advinfo\n", __func__);
2785 /* Use cam_error_print() to decode the status */
2786 cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
2789 bcopy(rcaplong, &softc->rcaplong,
2790 min(sizeof(softc->rcaplong), rcap_len));
2794 softc->disk->d_sectorsize = softc->params.secsize;
2795 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
2796 softc->disk->d_stripesize = softc->params.stripesize;
2797 softc->disk->d_stripeoffset = softc->params.stripeoffset;
2798 /* XXX: these are not actually "firmware" values, so they may be wrong */
2799 softc->disk->d_fwsectors = softc->params.secs_per_track;
2800 softc->disk->d_fwheads = softc->params.heads;
2801 softc->disk->d_devstat->block_size = softc->params.secsize;
2802 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
2803 if (softc->delete_method > DA_DELETE_DISABLE)
2804 softc->disk->d_flags |= DISKFLAG_CANDELETE;
2806 softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
2810 dasendorderedtag(void *arg)
2812 struct da_softc *softc = arg;
2814 if (da_send_ordered) {
2815 if ((softc->ordered_tag_count == 0)
2816 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
2817 softc->flags |= DA_FLAG_NEED_OTAG;
2819 if (softc->outstanding_cmds > 0)
2820 softc->flags &= ~DA_FLAG_WENT_IDLE;
2822 softc->ordered_tag_count = 0;
2824 /* Queue us up again */
2825 callout_reset(&softc->sendordered_c,
2826 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2827 dasendorderedtag, softc);
2831 * Step through all DA peripheral drivers, and if the device is still open,
2832 * sync the disk cache to physical media.
2835 dashutdown(void * arg, int howto)
2837 struct cam_periph *periph;
2838 struct da_softc *softc;
2842 CAM_PERIPH_FOREACH(periph, &dadriver) {
2843 cam_periph_lock(periph);
2844 softc = (struct da_softc *)periph->softc;
2847 * We only sync the cache if the drive is still open, and
2848 * if the drive is capable of it..
2850 if (((softc->flags & DA_FLAG_OPEN) == 0)
2851 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
2852 cam_periph_unlock(periph);
2856 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2857 scsi_synchronize_cache(&ccb->csio,
2861 /*begin_lba*/0, /* whole disk */
2866 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
2867 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR,
2868 softc->disk->d_devstat);
2870 xpt_print(periph->path, "Synchronize cache failed\n");
2871 xpt_release_ccb(ccb);
2872 cam_periph_unlock(periph);
2876 #else /* !_KERNEL */
2879 * XXX This is only left out of the kernel build to silence warnings. If,
2880 * for some reason this function is used in the kernel, the ifdefs should
2881 * be moved so it is included both in the kernel and userland.
2884 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2885 void (*cbfcnp)(struct cam_periph *, union ccb *),
2886 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2887 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2890 struct scsi_format_unit *scsi_cmd;
2892 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2893 scsi_cmd->opcode = FORMAT_UNIT;
2894 scsi_cmd->byte2 = byte2;
2895 scsi_ulto2b(ileave, scsi_cmd->interleave);
2900 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2909 #endif /* _KERNEL */