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_TAGGED_QUEUING = 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_CAN_LBPME = 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_TYPE_MASK = 0x0F,
105 DA_CCB_RETRY_UA = 0x10
108 /* Offsets into our private area for storing information */
109 #define ccb_state ppriv_field0
110 #define ccb_bp ppriv_ptr1
115 u_int8_t secs_per_track;
116 u_int32_t secsize; /* Number of bytes/sector */
117 u_int64_t sectors; /* total number sectors */
123 struct bio_queue_head bio_queue;
124 SLIST_ENTRY(da_softc) links;
125 LIST_HEAD(, ccb_hdr) pending_ccbs;
129 int minimum_cmd_size;
131 int ordered_tag_count;
132 int outstanding_cmds;
133 struct disk_params params;
136 struct task sysctl_task;
137 struct sysctl_ctx_list sysctl_ctx;
138 struct sysctl_oid *sysctl_tree;
139 struct callout sendordered_c;
143 struct da_quirk_entry {
144 struct scsi_inquiry_pattern inq_pat;
148 static const char quantum[] = "QUANTUM";
149 static const char microp[] = "MICROP";
151 static struct da_quirk_entry da_quirk_table[] =
153 /* SPI, FC devices */
156 * Fujitsu M2513A MO drives.
157 * Tested devices: M2513A2 firmware versions 1200 & 1300.
158 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
159 * Reported by: W.Scholten <whs@xs4all.nl>
161 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
162 /*quirks*/ DA_Q_NO_SYNC_CACHE
166 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
167 /*quirks*/ DA_Q_NO_SYNC_CACHE
171 * This particular Fujitsu drive doesn't like the
172 * synchronize cache command.
173 * Reported by: Tom Jackson <toj@gorilla.net>
175 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
176 /*quirks*/ DA_Q_NO_SYNC_CACHE
180 * This drive doesn't like the synchronize cache command
181 * either. Reported by: Matthew Jacob <mjacob@feral.com>
182 * in NetBSD PR kern/6027, August 24, 1998.
184 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
185 /*quirks*/ DA_Q_NO_SYNC_CACHE
189 * This drive doesn't like the synchronize cache command
190 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
193 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
194 /*quirks*/ DA_Q_NO_SYNC_CACHE
198 * Doesn't like the synchronize cache command.
199 * Reported by: Blaz Zupan <blaz@gold.amis.net>
201 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
202 /*quirks*/ DA_Q_NO_SYNC_CACHE
206 * Doesn't like the synchronize cache command.
207 * Reported by: Blaz Zupan <blaz@gold.amis.net>
209 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
210 /*quirks*/ DA_Q_NO_SYNC_CACHE
214 * Doesn't like the synchronize cache command.
216 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
217 /*quirks*/ DA_Q_NO_SYNC_CACHE
221 * Doesn't like the synchronize cache command.
222 * Reported by: walter@pelissero.de
224 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
225 /*quirks*/ DA_Q_NO_SYNC_CACHE
229 * Doesn't work correctly with 6 byte reads/writes.
230 * Returns illegal request, and points to byte 9 of the
232 * Reported by: Adam McDougall <bsdx@spawnet.com>
234 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
235 /*quirks*/ DA_Q_NO_6_BYTE
239 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
240 /*quirks*/ DA_Q_NO_6_BYTE
244 * Doesn't like the synchronize cache command.
245 * Reported by: walter@pelissero.de
247 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
248 /*quirks*/ DA_Q_NO_SYNC_CACHE
252 * The CISS RAID controllers do not support SYNC_CACHE
254 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
255 /*quirks*/ DA_Q_NO_SYNC_CACHE
257 /* USB mass storage devices supported by umass(4) */
260 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
263 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
264 /*quirks*/ DA_Q_NO_SYNC_CACHE
268 * Power Quotient Int. (PQI) USB flash key
271 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
272 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
276 * Creative Nomad MUVO mp3 player (USB)
279 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
280 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
284 * Jungsoft NEXDISK USB flash key
287 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
288 /*quirks*/ DA_Q_NO_SYNC_CACHE
292 * FreeDik USB Mini Data Drive
295 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
296 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
300 * Sigmatel USB Flash MP3 Player
303 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
304 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
308 * Neuros USB Digital Audio Computer
311 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
312 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
316 * SEAGRAND NP-900 MP3 Player
319 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
320 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
324 * iRiver iFP MP3 player (with UMS Firmware)
325 * PR: kern/54881, i386/63941, kern/66124
327 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
328 /*quirks*/ DA_Q_NO_SYNC_CACHE
332 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
335 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
336 /*quirks*/ DA_Q_NO_SYNC_CACHE
340 * ZICPlay USB MP3 Player with FM
343 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
344 /*quirks*/ DA_Q_NO_SYNC_CACHE
348 * TEAC USB floppy mechanisms
350 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
351 /*quirks*/ DA_Q_NO_SYNC_CACHE
355 * Kingston DataTraveler II+ USB Pen-Drive.
356 * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
358 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
359 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
363 * Motorola E398 Mobile Phone (TransFlash memory card).
364 * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
367 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
368 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
372 * Qware BeatZkey! Pro
375 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
376 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
380 * Time DPA20B 1GB MP3 Player
383 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
384 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
388 * Samsung USB key 128Mb
391 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
392 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
396 * Kingston DataTraveler 2.0 USB Flash memory.
399 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
400 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
404 * Creative MUVO Slim mp3 player (USB)
407 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
408 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
412 * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
415 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
416 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
420 * SanDisk Micro Cruzer 128MB
423 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
424 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
428 * TOSHIBA TransMemory USB sticks
431 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
432 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
437 * PR: usb/75578, usb/72344, usb/65436
439 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
440 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
444 * Genesys 6-in-1 Card Reader
447 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
448 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
452 * Rekam Digital CAMERA
455 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
456 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
460 * iRiver H10 MP3 player
463 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
464 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
468 * iRiver U10 MP3 player
471 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
472 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
479 {T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
480 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
484 * EasyMP3 EM732X USB 2.0 Flash MP3 Player
487 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
488 "1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
495 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
496 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
500 * Philips USB Key Audio KEY013
503 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
504 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
511 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
512 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
519 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
520 /*quirks*/ DA_Q_NO_SYNC_CACHE
524 * I/O Magic USB flash - Giga Bank
527 {T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
528 /*quirks*/ DA_Q_NO_SYNC_CACHE
532 * JoyFly 128mb USB Flash Drive
535 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
536 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
543 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
544 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
548 * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A
551 {T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*",
552 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
556 * Samsung YP-U3 mp3-player
559 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3",
560 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
563 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
564 "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
568 * Sony Cyber-Shot DSC cameras
571 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
572 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
574 /* ATA/SATA devices over SAS/USB/... */
576 /* Hitachi Advanced Format (4k) drives */
577 { T_DIRECT, SIP_MEDIA_FIXED, "Hitachi", "H??????????E3*", "*" },
581 /* Samsung Advanced Format (4k) drives */
582 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD155UI*", "*" },
586 /* Samsung Advanced Format (4k) drives */
587 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD155UI*", "*" },
591 /* Samsung Advanced Format (4k) drives */
592 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD204UI*", "*" },
596 /* Samsung Advanced Format (4k) drives */
597 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD204UI*", "*" },
601 /* Seagate Barracuda Green Advanced Format (4k) drives */
602 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DL*", "*" },
606 /* Seagate Barracuda Green Advanced Format (4k) drives */
607 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DL", "*", "*" },
611 /* Seagate Barracuda Green Advanced Format (4k) drives */
612 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???DM*", "*" },
616 /* Seagate Barracuda Green Advanced Format (4k) drives */
617 { T_DIRECT, SIP_MEDIA_FIXED, "ST???DM*", "*", "*" },
621 /* Seagate Barracuda Green Advanced Format (4k) drives */
622 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DM*", "*" },
626 /* Seagate Barracuda Green Advanced Format (4k) drives */
627 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DM", "*", "*" },
631 /* Seagate Momentus Advanced Format (4k) drives */
632 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500423AS*", "*" },
636 /* Seagate Momentus Advanced Format (4k) drives */
637 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "3AS*", "*" },
641 /* Seagate Momentus Advanced Format (4k) drives */
642 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500424AS*", "*" },
646 /* Seagate Momentus Advanced Format (4k) drives */
647 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "4AS*", "*" },
651 /* Seagate Momentus Advanced Format (4k) drives */
652 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640423AS*", "*" },
656 /* Seagate Momentus Advanced Format (4k) drives */
657 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "3AS*", "*" },
661 /* Seagate Momentus Advanced Format (4k) drives */
662 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640424AS*", "*" },
666 /* Seagate Momentus Advanced Format (4k) drives */
667 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "4AS*", "*" },
671 /* Seagate Momentus Advanced Format (4k) drives */
672 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750420AS*", "*" },
676 /* Seagate Momentus Advanced Format (4k) drives */
677 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "0AS*", "*" },
681 /* Seagate Momentus Advanced Format (4k) drives */
682 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750422AS*", "*" },
686 /* Seagate Momentus Advanced Format (4k) drives */
687 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "2AS*", "*" },
691 /* Seagate Momentus Advanced Format (4k) drives */
692 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750423AS*", "*" },
696 /* Seagate Momentus Advanced Format (4k) drives */
697 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "3AS*", "*" },
701 /* Seagate Momentus Thin Advanced Format (4k) drives */
702 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???LT*", "*" },
706 /* Seagate Momentus Thin Advanced Format (4k) drives */
707 { T_DIRECT, SIP_MEDIA_FIXED, "ST???LT*", "*", "*" },
711 /* WDC Caviar Green Advanced Format (4k) drives */
712 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RS*", "*" },
716 /* WDC Caviar Green Advanced Format (4k) drives */
717 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RS*", "*" },
721 /* WDC Caviar Green Advanced Format (4k) drives */
722 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RX*", "*" },
726 /* WDC Caviar Green Advanced Format (4k) drives */
727 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RX*", "*" },
731 /* WDC Caviar Green Advanced Format (4k) drives */
732 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RS*", "*" },
736 /* WDC Caviar Green Advanced Format (4k) drives */
737 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RS*", "*" },
741 /* WDC Caviar Green Advanced Format (4k) drives */
742 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RX*", "*" },
746 /* WDC Caviar Green Advanced Format (4k) drives */
747 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RX*", "*" },
751 /* WDC Scorpio Black Advanced Format (4k) drives */
752 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PKT*", "*" },
756 /* WDC Scorpio Black Advanced Format (4k) drives */
757 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PKT*", "*" },
761 /* WDC Scorpio Black Advanced Format (4k) drives */
762 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PKT*", "*" },
766 /* WDC Scorpio Black Advanced Format (4k) drives */
767 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PKT*", "*" },
771 /* WDC Scorpio Blue Advanced Format (4k) drives */
772 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PVT*", "*" },
776 /* WDC Scorpio Blue Advanced Format (4k) drives */
777 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PVT*", "*" },
781 /* WDC Scorpio Blue Advanced Format (4k) drives */
782 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PVT*", "*" },
786 /* WDC Scorpio Blue Advanced Format (4k) drives */
787 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PVT*", "*" },
792 static disk_strategy_t dastrategy;
793 static dumper_t dadump;
794 static periph_init_t dainit;
795 static void daasync(void *callback_arg, u_int32_t code,
796 struct cam_path *path, void *arg);
797 static void dasysctlinit(void *context, int pending);
798 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
799 static periph_ctor_t daregister;
800 static periph_dtor_t dacleanup;
801 static periph_start_t dastart;
802 static periph_oninv_t daoninvalidate;
803 static void dadone(struct cam_periph *periph,
804 union ccb *done_ccb);
805 static int daerror(union ccb *ccb, u_int32_t cam_flags,
806 u_int32_t sense_flags);
807 static void daprevent(struct cam_periph *periph, int action);
808 static int dagetcapacity(struct cam_periph *periph);
809 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
810 uint64_t maxsector, u_int lbppbe, u_int lalba);
811 static timeout_t dasendorderedtag;
812 static void dashutdown(void *arg, int howto);
814 #ifndef DA_DEFAULT_TIMEOUT
815 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
818 #ifndef DA_DEFAULT_RETRY
819 #define DA_DEFAULT_RETRY 4
822 #ifndef DA_DEFAULT_SEND_ORDERED
823 #define DA_DEFAULT_SEND_ORDERED 1
827 static int da_retry_count = DA_DEFAULT_RETRY;
828 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
829 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
831 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
832 "CAM Direct Access Disk driver");
833 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
834 &da_retry_count, 0, "Normal I/O retry count");
835 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
836 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
837 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
838 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
839 SYSCTL_INT(_kern_cam_da, OID_AUTO, da_send_ordered, CTLFLAG_RW,
840 &da_send_ordered, 0, "Send Ordered Tags");
841 TUNABLE_INT("kern.cam.da.da_send_ordered", &da_send_ordered);
844 * DA_ORDEREDTAG_INTERVAL determines how often, relative
845 * to the default timeout, we check to see whether an ordered
846 * tagged transaction is appropriate to prevent simple tag
847 * starvation. Since we'd like to ensure that there is at least
848 * 1/2 of the timeout length left for a starved transaction to
849 * complete after we've sent an ordered tag, we must poll at least
850 * four times in every timeout period. This takes care of the worst
851 * case where a starved transaction starts during an interval that
852 * meets the requirement "don't send an ordered tag" test so it takes
853 * us two intervals to determine that a tag must be sent.
855 #ifndef DA_ORDEREDTAG_INTERVAL
856 #define DA_ORDEREDTAG_INTERVAL 4
859 static struct periph_driver dadriver =
862 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
865 PERIPHDRIVER_DECLARE(da, dadriver);
867 MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
870 daopen(struct disk *dp)
872 struct cam_periph *periph;
873 struct da_softc *softc;
877 periph = (struct cam_periph *)dp->d_drv1;
878 if (periph == NULL) {
882 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
886 cam_periph_lock(periph);
887 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
888 cam_periph_unlock(periph);
889 cam_periph_release(periph);
893 unit = periph->unit_number;
894 softc = (struct da_softc *)periph->softc;
895 softc->flags |= DA_FLAG_OPEN;
897 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
898 ("daopen: disk=%s%d (unit %d)\n", dp->d_name, dp->d_unit,
901 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
902 /* Invalidate our pack information. */
903 softc->flags &= ~DA_FLAG_PACK_INVALID;
906 error = dagetcapacity(periph);
910 softc->disk->d_sectorsize = softc->params.secsize;
911 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
912 softc->disk->d_stripesize = softc->params.stripesize;
913 softc->disk->d_stripeoffset = softc->params.stripeoffset;
914 /* XXX: these are not actually "firmware" values, so they may be wrong */
915 softc->disk->d_fwsectors = softc->params.secs_per_track;
916 softc->disk->d_fwheads = softc->params.heads;
917 softc->disk->d_devstat->block_size = softc->params.secsize;
918 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
920 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
921 (softc->quirks & DA_Q_NO_PREVENT) == 0)
922 daprevent(periph, PR_PREVENT);
924 softc->flags &= ~DA_FLAG_OPEN;
926 cam_periph_unhold(periph);
927 cam_periph_unlock(periph);
930 cam_periph_release(periph);
936 daclose(struct disk *dp)
938 struct cam_periph *periph;
939 struct da_softc *softc;
942 periph = (struct cam_periph *)dp->d_drv1;
946 cam_periph_lock(periph);
947 if ((error = cam_periph_hold(periph, PRIBIO)) != 0) {
948 cam_periph_unlock(periph);
949 cam_periph_release(periph);
953 softc = (struct da_softc *)periph->softc;
955 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0
956 && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
959 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
961 scsi_synchronize_cache(&ccb->csio,
965 /*begin_lba*/0,/* Cover the whole disk */
970 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
971 /*sense_flags*/SF_RETRY_UA,
972 softc->disk->d_devstat);
974 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
975 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
976 CAM_SCSI_STATUS_ERROR) {
978 int sense_key, error_code;
980 scsi_extract_sense_len(&ccb->csio.sense_data,
981 ccb->csio.sense_len - ccb->csio.sense_resid,
982 &error_code, &sense_key, &asc, &ascq,
984 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
985 scsi_sense_print(&ccb->csio);
987 xpt_print(periph->path, "Synchronize cache "
988 "failed, status == 0x%x, scsi status == "
989 "0x%x\n", ccb->csio.ccb_h.status,
990 ccb->csio.scsi_status);
994 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
995 cam_release_devq(ccb->ccb_h.path,
1001 xpt_release_ccb(ccb);
1005 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
1006 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
1007 daprevent(periph, PR_ALLOW);
1009 * If we've got removeable media, mark the blocksize as
1010 * unavailable, since it could change when new media is
1013 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1016 softc->flags &= ~DA_FLAG_OPEN;
1017 cam_periph_unhold(periph);
1018 cam_periph_unlock(periph);
1019 cam_periph_release(periph);
1024 * Actually translate the requested transfer into one the physical driver
1025 * can understand. The transfer is described by a buf and will include
1026 * only one physical transfer.
1029 dastrategy(struct bio *bp)
1031 struct cam_periph *periph;
1032 struct da_softc *softc;
1034 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1035 if (periph == NULL) {
1036 biofinish(bp, NULL, ENXIO);
1039 softc = (struct da_softc *)periph->softc;
1041 cam_periph_lock(periph);
1044 * If the device has been made invalid, error out
1046 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1047 cam_periph_unlock(periph);
1048 biofinish(bp, NULL, ENXIO);
1053 * Place it in the queue of disk activities for this disk
1055 bioq_disksort(&softc->bio_queue, bp);
1058 * Schedule ourselves for performing the work.
1060 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1061 cam_periph_unlock(periph);
1067 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1069 struct cam_periph *periph;
1070 struct da_softc *softc;
1072 struct ccb_scsiio csio;
1076 periph = dp->d_drv1;
1079 softc = (struct da_softc *)periph->softc;
1080 cam_periph_lock(periph);
1081 secsize = softc->params.secsize;
1083 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1084 cam_periph_unlock(periph);
1089 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1090 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1091 scsi_read_write(&csio,
1097 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1100 /*data_ptr*/(u_int8_t *) virtual,
1101 /*dxfer_len*/length,
1102 /*sense_len*/SSD_FULL_SIZE,
1103 da_default_timeout * 1000);
1104 xpt_polled_action((union ccb *)&csio);
1105 cam_periph_unlock(periph);
1107 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1108 printf("Aborting dump due to I/O error.\n");
1109 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
1110 CAM_SCSI_STATUS_ERROR)
1111 scsi_sense_print(&csio);
1113 printf("status == 0x%x, scsi status == 0x%x\n",
1114 csio.ccb_h.status, csio.scsi_status);
1121 * Sync the disk cache contents to the physical media.
1123 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1125 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1126 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1127 scsi_synchronize_cache(&csio,
1131 /*begin_lba*/0,/* Cover the whole disk */
1135 xpt_polled_action((union ccb *)&csio);
1137 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1138 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
1139 CAM_SCSI_STATUS_ERROR) {
1141 int sense_key, error_code;
1143 scsi_extract_sense_len(&csio.sense_data,
1144 csio.sense_len - csio.sense_resid,
1145 &error_code, &sense_key, &asc, &ascq,
1147 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1148 scsi_sense_print(&csio);
1150 xpt_print(periph->path, "Synchronize cache "
1151 "failed, status == 0x%x, scsi status == "
1152 "0x%x\n", csio.ccb_h.status,
1157 cam_periph_unlock(periph);
1162 dagetattr(struct bio *bp)
1165 struct cam_periph *periph;
1167 if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
1169 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1170 if (periph->path == NULL)
1173 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1176 bp->bio_completed = bp->bio_length;
1186 * Install a global async callback. This callback will
1187 * receive async callbacks like "new device found".
1189 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1191 if (status != CAM_REQ_CMP) {
1192 printf("da: Failed to attach master async callback "
1193 "due to status 0x%x!\n", status);
1194 } else if (da_send_ordered) {
1196 /* Register our shutdown event handler */
1197 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1198 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1199 printf("dainit: shutdown event registration failed!\n");
1204 daoninvalidate(struct cam_periph *periph)
1206 struct da_softc *softc;
1208 softc = (struct da_softc *)periph->softc;
1211 * De-register any async callbacks.
1213 xpt_register_async(0, daasync, periph, periph->path);
1215 softc->flags |= DA_FLAG_PACK_INVALID;
1218 * Return all queued I/O with ENXIO.
1219 * XXX Handle any transactions queued to the card
1220 * with XPT_ABORT_CCB.
1222 bioq_flush(&softc->bio_queue, NULL, ENXIO);
1224 disk_gone(softc->disk);
1225 xpt_print(periph->path, "lost device - %d outstanding, %d refs\n",
1226 softc->outstanding_cmds, periph->refcount);
1230 dacleanup(struct cam_periph *periph)
1232 struct da_softc *softc;
1234 softc = (struct da_softc *)periph->softc;
1236 xpt_print(periph->path, "removing device entry\n");
1237 cam_periph_unlock(periph);
1240 * If we can't free the sysctl tree, oh well...
1242 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1243 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1244 xpt_print(periph->path, "can't remove sysctl context\n");
1247 disk_destroy(softc->disk);
1248 callout_drain(&softc->sendordered_c);
1249 free(softc, M_DEVBUF);
1250 cam_periph_lock(periph);
1254 daasync(void *callback_arg, u_int32_t code,
1255 struct cam_path *path, void *arg)
1257 struct cam_periph *periph;
1259 periph = (struct cam_periph *)callback_arg;
1261 case AC_FOUND_DEVICE:
1263 struct ccb_getdev *cgd;
1266 cgd = (struct ccb_getdev *)arg;
1270 if (cgd->protocol != PROTO_SCSI)
1273 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1274 && SID_TYPE(&cgd->inq_data) != T_RBC
1275 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1279 * Allocate a peripheral instance for
1280 * this device and start the probe
1283 status = cam_periph_alloc(daregister, daoninvalidate,
1285 "da", CAM_PERIPH_BIO,
1286 cgd->ccb_h.path, daasync,
1287 AC_FOUND_DEVICE, cgd);
1289 if (status != CAM_REQ_CMP
1290 && status != CAM_REQ_INPROG)
1291 printf("daasync: Unable to attach to new device "
1292 "due to status 0x%x\n", status);
1295 case AC_ADVINFO_CHANGED:
1299 buftype = (uintptr_t)arg;
1300 if (buftype == CDAI_TYPE_PHYS_PATH) {
1301 struct da_softc *softc;
1303 softc = periph->softc;
1304 disk_attr_changed(softc->disk, "GEOM::physpath",
1312 struct da_softc *softc;
1313 struct ccb_hdr *ccbh;
1315 softc = (struct da_softc *)periph->softc;
1317 * Don't fail on the expected unit attention
1320 softc->flags |= DA_FLAG_RETRY_UA;
1321 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1322 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1328 cam_periph_async(periph, code, path, arg);
1332 dasysctlinit(void *context, int pending)
1334 struct cam_periph *periph;
1335 struct da_softc *softc;
1336 char tmpstr[80], tmpstr2[80];
1337 struct ccb_trans_settings cts;
1339 periph = (struct cam_periph *)context;
1341 * periph was held for us when this task was enqueued
1343 if (periph->flags & CAM_PERIPH_INVALID) {
1344 cam_periph_release(periph);
1348 softc = (struct da_softc *)periph->softc;
1349 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1350 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1352 sysctl_ctx_init(&softc->sysctl_ctx);
1353 softc->flags |= DA_FLAG_SCTX_INIT;
1354 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1355 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1356 CTLFLAG_RD, 0, tmpstr);
1357 if (softc->sysctl_tree == NULL) {
1358 printf("dasysctlinit: unable to allocate sysctl tree\n");
1359 cam_periph_release(periph);
1364 * Now register the sysctl handler, so the user can change the value on
1367 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1368 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1369 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1370 "Minimum CDB size");
1372 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1373 SYSCTL_CHILDREN(softc->sysctl_tree),
1377 &softc->error_inject,
1379 "error_inject leaf");
1383 * Add some addressing info.
1385 memset(&cts, 0, sizeof (cts));
1386 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
1387 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1388 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1389 cam_periph_lock(periph);
1390 xpt_action((union ccb *)&cts);
1391 cam_periph_unlock(periph);
1392 if (cts.ccb_h.status != CAM_REQ_CMP) {
1393 cam_periph_release(periph);
1396 if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
1397 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1398 if (fc->valid & CTS_FC_VALID_WWPN) {
1399 softc->wwpn = fc->wwpn;
1400 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1401 SYSCTL_CHILDREN(softc->sysctl_tree),
1402 OID_AUTO, "wwpn", CTLFLAG_RD,
1403 &softc->wwpn, "World Wide Port Name");
1406 cam_periph_release(periph);
1410 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1414 value = *(int *)arg1;
1416 error = sysctl_handle_int(oidp, &value, 0, req);
1419 || (req->newptr == NULL))
1423 * Acceptable values here are 6, 10, 12 or 16.
1427 else if ((value > 6)
1430 else if ((value > 10)
1433 else if (value > 12)
1436 *(int *)arg1 = value;
1442 daregister(struct cam_periph *periph, void *arg)
1444 struct da_softc *softc;
1445 struct ccb_pathinq cpi;
1446 struct ccb_getdev *cgd;
1450 cgd = (struct ccb_getdev *)arg;
1451 if (periph == NULL) {
1452 printf("daregister: periph was NULL!!\n");
1453 return(CAM_REQ_CMP_ERR);
1457 printf("daregister: no getdev CCB, can't register device\n");
1458 return(CAM_REQ_CMP_ERR);
1461 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
1464 if (softc == NULL) {
1465 printf("daregister: Unable to probe new device. "
1466 "Unable to allocate softc\n");
1467 return(CAM_REQ_CMP_ERR);
1470 LIST_INIT(&softc->pending_ccbs);
1471 softc->state = DA_STATE_PROBE;
1472 bioq_init(&softc->bio_queue);
1473 if (SID_IS_REMOVABLE(&cgd->inq_data))
1474 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1475 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1476 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1478 periph->softc = softc;
1481 * See if this device has any quirks.
1483 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1484 (caddr_t)da_quirk_table,
1485 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1486 sizeof(*da_quirk_table), scsi_inquiry_match);
1489 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1491 softc->quirks = DA_Q_NONE;
1493 /* Check if the SIM does not want 6 byte commands */
1494 bzero(&cpi, sizeof(cpi));
1495 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1496 cpi.ccb_h.func_code = XPT_PATH_INQ;
1497 xpt_action((union ccb *)&cpi);
1498 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1499 softc->quirks |= DA_Q_NO_6_BYTE;
1501 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1504 * Take an exclusive refcount on the periph while dastart is called
1505 * to finish the probe. The reference will be dropped in dadone at
1508 (void)cam_periph_hold(periph, PRIBIO);
1511 * Schedule a periodic event to occasionally send an
1512 * ordered tag to a device.
1514 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
1515 callout_reset(&softc->sendordered_c,
1516 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
1517 dasendorderedtag, softc);
1519 mtx_unlock(periph->sim->mtx);
1521 * RBC devices don't have to support READ(6), only READ(10).
1523 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1524 softc->minimum_cmd_size = 10;
1526 softc->minimum_cmd_size = 6;
1529 * Load the user's default, if any.
1531 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1532 periph->unit_number);
1533 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1536 * 6, 10, 12 and 16 are the currently permissible values.
1538 if (softc->minimum_cmd_size < 6)
1539 softc->minimum_cmd_size = 6;
1540 else if ((softc->minimum_cmd_size > 6)
1541 && (softc->minimum_cmd_size <= 10))
1542 softc->minimum_cmd_size = 10;
1543 else if ((softc->minimum_cmd_size > 10)
1544 && (softc->minimum_cmd_size <= 12))
1545 softc->minimum_cmd_size = 12;
1546 else if (softc->minimum_cmd_size > 12)
1547 softc->minimum_cmd_size = 16;
1549 /* Predict whether device may support READ CAPACITY(16). */
1550 if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3 ||
1551 (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC &&
1552 (cgd->inq_data.spc3_flags & SPC3_SID_PROTECT))) {
1553 softc->flags |= DA_FLAG_CAN_RC16;
1554 softc->state = DA_STATE_PROBE2;
1558 * Register this media as a disk.
1560 softc->disk = disk_alloc();
1561 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
1562 periph->unit_number, 0,
1563 DEVSTAT_BS_UNAVAILABLE,
1564 SID_TYPE(&cgd->inq_data) |
1565 XPORT_DEVSTAT_TYPE(cpi.transport),
1566 DEVSTAT_PRIORITY_DISK);
1567 softc->disk->d_open = daopen;
1568 softc->disk->d_close = daclose;
1569 softc->disk->d_strategy = dastrategy;
1570 softc->disk->d_dump = dadump;
1571 softc->disk->d_getattr = dagetattr;
1572 softc->disk->d_name = "da";
1573 softc->disk->d_drv1 = periph;
1575 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */
1576 else if (cpi.maxio > MAXPHYS)
1577 softc->disk->d_maxsize = MAXPHYS; /* for safety */
1579 softc->disk->d_maxsize = cpi.maxio;
1580 softc->disk->d_unit = periph->unit_number;
1581 softc->disk->d_flags = 0;
1582 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
1583 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
1584 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
1585 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
1586 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
1587 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
1588 cgd->inq_data.product, sizeof(cgd->inq_data.product),
1589 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
1590 softc->disk->d_hba_vendor = cpi.hba_vendor;
1591 softc->disk->d_hba_device = cpi.hba_device;
1592 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
1593 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
1594 disk_create(softc->disk, DISK_VERSION);
1595 mtx_lock(periph->sim->mtx);
1598 * Add async callbacks for events of interest.
1599 * I don't bother checking if this fails as,
1600 * in most cases, the system will function just
1601 * fine without them and the only alternative
1602 * would be to not attach the device on failure.
1604 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET
1605 | AC_LOST_DEVICE | AC_ADVINFO_CHANGED,
1606 daasync, periph, periph->path);
1609 * Emit an attribute changed notification just in case
1610 * physical path information arrived before our async
1611 * event handler was registered, but after anyone attaching
1612 * to our disk device polled it.
1614 disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
1616 xpt_schedule(periph, CAM_PRIORITY_DEV);
1618 return(CAM_REQ_CMP);
1622 dastart(struct cam_periph *periph, union ccb *start_ccb)
1624 struct da_softc *softc;
1626 softc = (struct da_softc *)periph->softc;
1628 switch (softc->state) {
1629 case DA_STATE_NORMAL:
1631 /* Pull a buffer from the queue and get going on it */
1635 * See if there is a buf with work for us to do..
1637 bp = bioq_first(&softc->bio_queue);
1638 if (periph->immediate_priority <= periph->pinfo.priority) {
1639 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1640 ("queuing for immediate ccb\n"));
1641 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1642 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1644 periph->immediate_priority = CAM_PRIORITY_NONE;
1645 wakeup(&periph->ccb_list);
1646 } else if (bp == NULL) {
1647 xpt_release_ccb(start_ccb);
1651 bioq_remove(&softc->bio_queue, bp);
1653 if ((bp->bio_flags & BIO_ORDERED) != 0
1654 || (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1655 softc->flags &= ~DA_FLAG_NEED_OTAG;
1656 softc->ordered_tag_count++;
1657 tag_code = MSG_ORDERED_Q_TAG;
1659 tag_code = MSG_SIMPLE_Q_TAG;
1661 switch (bp->bio_cmd) {
1664 scsi_read_write(&start_ccb->csio,
1665 /*retries*/da_retry_count,
1667 /*tag_action*/tag_code,
1668 /*read_op*/bp->bio_cmd
1671 softc->minimum_cmd_size,
1672 /*lba*/bp->bio_pblkno,
1673 /*block_count*/bp->bio_bcount /
1674 softc->params.secsize,
1675 /*data_ptr*/ bp->bio_data,
1676 /*dxfer_len*/ bp->bio_bcount,
1677 /*sense_len*/SSD_FULL_SIZE,
1678 da_default_timeout * 1000);
1682 * BIO_FLUSH doesn't currently communicate
1683 * range data, so we synchronize the cache
1684 * over the whole disk. We also force
1685 * ordered tag semantics the flush applies
1686 * to all previously queued I/O.
1688 scsi_synchronize_cache(&start_ccb->csio,
1695 da_default_timeout*1000);
1698 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1701 * Block out any asyncronous callbacks
1702 * while we touch the pending ccb list.
1704 LIST_INSERT_HEAD(&softc->pending_ccbs,
1705 &start_ccb->ccb_h, periph_links.le);
1706 softc->outstanding_cmds++;
1708 /* We expect a unit attention from this device */
1709 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1710 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1711 softc->flags &= ~DA_FLAG_RETRY_UA;
1714 start_ccb->ccb_h.ccb_bp = bp;
1715 bp = bioq_first(&softc->bio_queue);
1717 xpt_action(start_ccb);
1721 /* Have more work to do, so ensure we stay scheduled */
1722 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1726 case DA_STATE_PROBE:
1728 struct ccb_scsiio *csio;
1729 struct scsi_read_capacity_data *rcap;
1731 rcap = (struct scsi_read_capacity_data *)
1732 malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
1734 printf("dastart: Couldn't malloc read_capacity data\n");
1735 /* da_free_periph??? */
1738 csio = &start_ccb->csio;
1739 scsi_read_capacity(csio,
1746 start_ccb->ccb_h.ccb_bp = NULL;
1747 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1748 xpt_action(start_ccb);
1751 case DA_STATE_PROBE2:
1753 struct ccb_scsiio *csio;
1754 struct scsi_read_capacity_data_long *rcaplong;
1756 rcaplong = (struct scsi_read_capacity_data_long *)
1757 malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
1758 if (rcaplong == NULL) {
1759 printf("dastart: Couldn't malloc read_capacity data\n");
1760 /* da_free_periph??? */
1763 csio = &start_ccb->csio;
1764 scsi_read_capacity_16(csio,
1767 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1772 /*sense_len*/ SSD_FULL_SIZE,
1774 start_ccb->ccb_h.ccb_bp = NULL;
1775 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1776 xpt_action(start_ccb);
1783 cmd6workaround(union ccb *ccb)
1785 struct scsi_rw_6 cmd6;
1786 struct scsi_rw_10 *cmd10;
1787 struct da_softc *softc;
1791 cdb = ccb->csio.cdb_io.cdb_bytes;
1793 /* Translation only possible if CDB is an array and cmd is R/W6 */
1794 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1795 (*cdb != READ_6 && *cdb != WRITE_6))
1798 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
1799 "increasing minimum_cmd_size to 10.\n");
1800 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1801 softc->minimum_cmd_size = 10;
1803 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1804 cmd10 = (struct scsi_rw_10 *)cdb;
1805 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1807 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1808 cmd10->reserved = 0;
1809 scsi_ulto2b(cmd6.length, cmd10->length);
1810 cmd10->control = cmd6.control;
1811 ccb->csio.cdb_len = sizeof(*cmd10);
1813 /* Requeue request, unfreezing queue if necessary */
1814 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1815 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1818 cam_release_devq(ccb->ccb_h.path,
1822 /*getcount_only*/0);
1828 dadone(struct cam_periph *periph, union ccb *done_ccb)
1830 struct da_softc *softc;
1831 struct ccb_scsiio *csio;
1834 softc = (struct da_softc *)periph->softc;
1835 priority = done_ccb->ccb_h.pinfo.priority;
1836 csio = &done_ccb->csio;
1837 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1838 case DA_CCB_BUFFER_IO:
1842 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1843 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1847 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1852 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1853 if (error == ERESTART) {
1855 * A retry was scheuled, so
1864 * return all queued I/O with EIO, so that
1865 * the client can retry these I/Os in the
1866 * proper order should it attempt to recover.
1871 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
1873 * Catastrophic error. Mark our pack as
1877 * XXX See if this is really a media
1880 xpt_print(periph->path,
1881 "Invalidating pack\n");
1882 softc->flags |= DA_FLAG_PACK_INVALID;
1883 queued_error = ENXIO;
1885 bioq_flush(&softc->bio_queue, NULL,
1887 bp->bio_error = error;
1888 bp->bio_resid = bp->bio_bcount;
1889 bp->bio_flags |= BIO_ERROR;
1891 bp->bio_resid = csio->resid;
1893 if (bp->bio_resid != 0)
1894 bp->bio_flags |= BIO_ERROR;
1896 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1897 cam_release_devq(done_ccb->ccb_h.path,
1901 /*getcount_only*/0);
1903 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1904 panic("REQ_CMP with QFRZN");
1905 bp->bio_resid = csio->resid;
1906 if (csio->resid > 0)
1907 bp->bio_flags |= BIO_ERROR;
1908 if (softc->error_inject != 0) {
1909 bp->bio_error = softc->error_inject;
1910 bp->bio_resid = bp->bio_bcount;
1911 bp->bio_flags |= BIO_ERROR;
1912 softc->error_inject = 0;
1918 * Block out any asyncronous callbacks
1919 * while we touch the pending ccb list.
1921 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1922 softc->outstanding_cmds--;
1923 if (softc->outstanding_cmds == 0)
1924 softc->flags |= DA_FLAG_WENT_IDLE;
1926 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1927 xpt_print(periph->path, "oustanding %d\n",
1928 softc->outstanding_cmds);
1937 struct scsi_read_capacity_data *rdcap;
1938 struct scsi_read_capacity_data_long *rcaplong;
1939 char announce_buf[80];
1943 if (softc->state == DA_STATE_PROBE)
1944 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1946 rcaplong = (struct scsi_read_capacity_data_long *)
1949 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1950 struct disk_params *dp;
1951 uint32_t block_size;
1953 u_int lbppbe; /* LB per physical block exponent. */
1954 u_int lalba; /* Lowest aligned LBA. */
1956 if (softc->state == DA_STATE_PROBE) {
1957 block_size = scsi_4btoul(rdcap->length);
1958 maxsector = scsi_4btoul(rdcap->addr);
1963 * According to SBC-2, if the standard 10
1964 * byte READ CAPACITY command returns 2^32,
1965 * we should issue the 16 byte version of
1966 * the command, since the device in question
1967 * has more sectors than can be represented
1968 * with the short version of the command.
1970 if (maxsector == 0xffffffff) {
1971 softc->state = DA_STATE_PROBE2;
1972 free(rdcap, M_SCSIDA);
1973 xpt_release_ccb(done_ccb);
1974 xpt_schedule(periph, priority);
1978 block_size = scsi_4btoul(rcaplong->length);
1979 maxsector = scsi_8btou64(rcaplong->addr);
1980 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
1981 lalba = scsi_2btoul(rcaplong->lalba_lbp);
1985 * Because GEOM code just will panic us if we
1986 * give them an 'illegal' value we'll avoid that
1989 if (block_size == 0 && maxsector == 0) {
1990 snprintf(announce_buf, sizeof(announce_buf),
1992 } else if (block_size >= MAXPHYS || block_size == 0) {
1993 xpt_print(periph->path,
1994 "unsupportable block size %ju\n",
1995 (uintmax_t) block_size);
1996 announce_buf[0] = '\0';
1997 cam_periph_invalidate(periph);
1999 dasetgeom(periph, block_size, maxsector,
2000 lbppbe, lalba & SRC16_LALBA);
2001 if (lalba & SRC16_LBPME)
2002 softc->flags |= DA_FLAG_CAN_LBPME;
2004 softc->flags &= ~DA_FLAG_CAN_LBPME;
2005 dp = &softc->params;
2006 snprintf(announce_buf, sizeof(announce_buf),
2007 "%juMB (%ju %u byte sectors: %dH %dS/T "
2009 (((uintmax_t)dp->secsize *
2010 dp->sectors) / (1024*1024)),
2011 (uintmax_t)dp->sectors,
2012 dp->secsize, dp->heads,
2013 dp->secs_per_track, dp->cylinders);
2018 announce_buf[0] = '\0';
2021 * Retry any UNIT ATTENTION type errors. They
2022 * are expected at boot.
2024 error = daerror(done_ccb, CAM_RETRY_SELTO,
2025 SF_RETRY_UA|SF_NO_PRINT);
2026 if (error == ERESTART) {
2028 * A retry was scheuled, so
2032 } else if (error != 0) {
2033 struct scsi_sense_data *sense;
2035 int sense_key, error_code;
2038 struct ccb_getdev cgd;
2040 /* Don't wedge this device's queue */
2041 status = done_ccb->ccb_h.status;
2042 if ((status & CAM_DEV_QFRZN) != 0)
2043 cam_release_devq(done_ccb->ccb_h.path,
2047 /*getcount_only*/0);
2050 xpt_setup_ccb(&cgd.ccb_h,
2051 done_ccb->ccb_h.path,
2052 CAM_PRIORITY_NORMAL);
2053 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2054 xpt_action((union ccb *)&cgd);
2056 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
2057 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
2058 || ((status & CAM_AUTOSNS_VALID) == 0))
2064 sense = &csio->sense_data;
2065 scsi_extract_sense_len(sense,
2066 csio->sense_len - csio->sense_resid,
2067 &error_code, &sense_key, &asc,
2068 &ascq, /*show_errors*/ 1);
2071 * If we tried READ CAPACITY(16) and failed,
2072 * fallback to READ CAPACITY(10).
2074 if ((softc->state == DA_STATE_PROBE2) &&
2075 (softc->flags & DA_FLAG_CAN_RC16) &&
2076 (((csio->ccb_h.status & CAM_STATUS_MASK) ==
2079 (error_code == SSD_CURRENT_ERROR) &&
2080 (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
2081 softc->flags &= ~DA_FLAG_CAN_RC16;
2082 softc->state = DA_STATE_PROBE;
2083 free(rdcap, M_SCSIDA);
2084 xpt_release_ccb(done_ccb);
2085 xpt_schedule(periph, priority);
2089 * Attach to anything that claims to be a
2090 * direct access or optical disk device,
2091 * as long as it doesn't return a "Logical
2092 * unit not supported" (0x25) error.
2094 if ((have_sense) && (asc != 0x25)
2095 && (error_code == SSD_CURRENT_ERROR)) {
2096 const char *sense_key_desc;
2097 const char *asc_desc;
2099 scsi_sense_desc(sense_key, asc, ascq,
2103 snprintf(announce_buf,
2104 sizeof(announce_buf),
2105 "Attempt to query device "
2106 "size failed: %s, %s",
2114 xpt_print(periph->path,
2115 "got CAM status %#x\n",
2116 done_ccb->ccb_h.status);
2119 xpt_print(periph->path, "fatal error, "
2120 "failed to attach to device\n");
2123 * Free up resources.
2125 cam_periph_invalidate(periph);
2129 free(csio->data_ptr, M_SCSIDA);
2130 if (announce_buf[0] != '\0') {
2132 * Create our sysctl variables, now that we know
2133 * we have successfully attached.
2135 /* increase the refcount */
2136 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
2137 taskqueue_enqueue(taskqueue_thread,
2138 &softc->sysctl_task);
2139 xpt_announce_periph(periph, announce_buf);
2141 xpt_print(periph->path, "fatal error, "
2142 "could not acquire reference count\n");
2146 softc->state = DA_STATE_NORMAL;
2148 * Since our peripheral may be invalidated by an error
2149 * above or an external event, we must release our CCB
2150 * before releasing the probe lock on the peripheral.
2151 * The peripheral will only go away once the last lock
2152 * is removed, and we need it around for the CCB release
2155 xpt_release_ccb(done_ccb);
2156 cam_periph_unhold(periph);
2159 case DA_CCB_WAITING:
2161 /* Caller will release the CCB */
2162 wakeup(&done_ccb->ccb_h.cbfcnp);
2166 /* No-op. We're polling */
2171 xpt_release_ccb(done_ccb);
2175 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
2177 struct da_softc *softc;
2178 struct cam_periph *periph;
2181 periph = xpt_path_periph(ccb->ccb_h.path);
2182 softc = (struct da_softc *)periph->softc;
2185 * Automatically detect devices that do not support
2186 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
2189 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
2190 error = cmd6workaround(ccb);
2191 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
2192 CAM_SCSI_STATUS_ERROR)
2193 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
2194 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
2195 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
2196 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
2197 int sense_key, error_code, asc, ascq;
2199 scsi_extract_sense(&ccb->csio.sense_data,
2200 &error_code, &sense_key, &asc, &ascq);
2201 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
2202 error = cmd6workaround(ccb);
2204 if (error == ERESTART)
2209 * Until we have a better way of doing pack validation,
2210 * don't treat UAs as errors.
2212 sense_flags |= SF_RETRY_UA;
2213 return(cam_periph_error(ccb, cam_flags, sense_flags,
2214 &softc->saved_ccb));
2218 daprevent(struct cam_periph *periph, int action)
2220 struct da_softc *softc;
2224 softc = (struct da_softc *)periph->softc;
2226 if (((action == PR_ALLOW)
2227 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
2228 || ((action == PR_PREVENT)
2229 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
2233 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2235 scsi_prevent(&ccb->csio,
2243 error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
2244 SF_RETRY_UA, softc->disk->d_devstat);
2247 if (action == PR_ALLOW)
2248 softc->flags &= ~DA_FLAG_PACK_LOCKED;
2250 softc->flags |= DA_FLAG_PACK_LOCKED;
2253 xpt_release_ccb(ccb);
2257 dagetcapacity(struct cam_periph *periph)
2259 struct da_softc *softc;
2261 struct scsi_read_capacity_data *rcap;
2262 struct scsi_read_capacity_data_long *rcaplong;
2265 int error, rc16failed;
2266 u_int32_t sense_flags;
2267 u_int lbppbe; /* Logical blocks per physical block exponent. */
2268 u_int lalba; /* Lowest aligned LBA. */
2270 softc = (struct da_softc *)periph->softc;
2277 sense_flags = SF_RETRY_UA;
2278 if (softc->flags & DA_FLAG_PACK_REMOVABLE)
2279 sense_flags |= SF_NO_PRINT;
2281 /* Do a read capacity */
2282 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcaplong),
2287 rcaplong = (struct scsi_read_capacity_data_long *)rcap;
2289 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2291 /* Try READ CAPACITY(16) first if we think it should work. */
2292 if (softc->flags & DA_FLAG_CAN_RC16) {
2293 scsi_read_capacity_16(&ccb->csio,
2296 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2301 /*sense_len*/ SSD_FULL_SIZE,
2303 ccb->ccb_h.ccb_bp = NULL;
2305 error = cam_periph_runccb(ccb, daerror,
2306 /*cam_flags*/CAM_RETRY_SELTO,
2308 softc->disk->d_devstat);
2310 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2311 cam_release_devq(ccb->ccb_h.path,
2315 /*getcount_only*/0);
2320 /* If we got ILLEGAL REQUEST, do not prefer RC16 any more. */
2321 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
2323 softc->flags &= ~DA_FLAG_CAN_RC16;
2324 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
2325 CAM_SCSI_STATUS_ERROR) &&
2326 (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND) &&
2327 (ccb->ccb_h.status & CAM_AUTOSNS_VALID) &&
2328 ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0) &&
2329 ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
2330 int sense_key, error_code, asc, ascq;
2332 scsi_extract_sense(&ccb->csio.sense_data,
2333 &error_code, &sense_key, &asc, &ascq);
2334 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
2335 softc->flags &= ~DA_FLAG_CAN_RC16;
2340 /* Do READ CAPACITY(10). */
2341 scsi_read_capacity(&ccb->csio,
2348 ccb->ccb_h.ccb_bp = NULL;
2350 error = cam_periph_runccb(ccb, daerror,
2351 /*cam_flags*/CAM_RETRY_SELTO,
2353 softc->disk->d_devstat);
2355 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2356 cam_release_devq(ccb->ccb_h.path,
2360 /*getcount_only*/0);
2363 block_len = scsi_4btoul(rcap->length);
2364 maxsector = scsi_4btoul(rcap->addr);
2366 if (maxsector != 0xffffffff || rc16failed)
2371 /* If READ CAPACITY(10) returned overflow, use READ CAPACITY(16) */
2372 scsi_read_capacity_16(&ccb->csio,
2375 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2380 /*sense_len*/ SSD_FULL_SIZE,
2382 ccb->ccb_h.ccb_bp = NULL;
2384 error = cam_periph_runccb(ccb, daerror,
2385 /*cam_flags*/CAM_RETRY_SELTO,
2387 softc->disk->d_devstat);
2389 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2390 cam_release_devq(ccb->ccb_h.path,
2394 /*getcount_only*/0);
2398 block_len = scsi_4btoul(rcaplong->length);
2399 maxsector = scsi_8btou64(rcaplong->addr);
2400 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
2401 lalba = scsi_2btoul(rcaplong->lalba_lbp);
2407 if (block_len >= MAXPHYS || block_len == 0) {
2408 xpt_print(periph->path,
2409 "unsupportable block size %ju\n",
2410 (uintmax_t) block_len);
2413 dasetgeom(periph, block_len, maxsector,
2414 lbppbe, lalba & SRC16_LALBA);
2415 if (lalba & SRC16_LBPME)
2416 softc->flags |= DA_FLAG_CAN_LBPME;
2418 softc->flags &= ~DA_FLAG_CAN_LBPME;
2422 xpt_release_ccb(ccb);
2424 free(rcap, M_SCSIDA);
2430 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
2431 u_int lbppbe, u_int lalba)
2433 struct ccb_calc_geometry ccg;
2434 struct da_softc *softc;
2435 struct disk_params *dp;
2437 softc = (struct da_softc *)periph->softc;
2439 dp = &softc->params;
2440 dp->secsize = block_len;
2441 dp->sectors = maxsector + 1;
2443 dp->stripesize = block_len << lbppbe;
2444 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
2446 } else if (softc->quirks & DA_Q_4K) {
2447 dp->stripesize = 4096;
2448 dp->stripeoffset = 0;
2451 dp->stripeoffset = 0;
2454 * Have the controller provide us with a geometry
2455 * for this disk. The only time the geometry
2456 * matters is when we boot and the controller
2457 * is the only one knowledgeable enough to come
2458 * up with something that will make this a bootable
2461 xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2462 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
2463 ccg.block_size = dp->secsize;
2464 ccg.volume_size = dp->sectors;
2466 ccg.secs_per_track = 0;
2468 xpt_action((union ccb*)&ccg);
2469 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2471 * We don't know what went wrong here- but just pick
2472 * a geometry so we don't have nasty things like divide
2476 dp->secs_per_track = 255;
2477 dp->cylinders = dp->sectors / (255 * 255);
2478 if (dp->cylinders == 0) {
2482 dp->heads = ccg.heads;
2483 dp->secs_per_track = ccg.secs_per_track;
2484 dp->cylinders = ccg.cylinders;
2489 dasendorderedtag(void *arg)
2491 struct da_softc *softc = arg;
2493 if (da_send_ordered) {
2494 if ((softc->ordered_tag_count == 0)
2495 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
2496 softc->flags |= DA_FLAG_NEED_OTAG;
2498 if (softc->outstanding_cmds > 0)
2499 softc->flags &= ~DA_FLAG_WENT_IDLE;
2501 softc->ordered_tag_count = 0;
2503 /* Queue us up again */
2504 callout_reset(&softc->sendordered_c,
2505 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2506 dasendorderedtag, softc);
2510 * Step through all DA peripheral drivers, and if the device is still open,
2511 * sync the disk cache to physical media.
2514 dashutdown(void * arg, int howto)
2516 struct cam_periph *periph;
2517 struct da_softc *softc;
2519 TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
2522 cam_periph_lock(periph);
2523 softc = (struct da_softc *)periph->softc;
2526 * We only sync the cache if the drive is still open, and
2527 * if the drive is capable of it..
2529 if (((softc->flags & DA_FLAG_OPEN) == 0)
2530 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
2531 cam_periph_unlock(periph);
2535 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2537 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
2538 scsi_synchronize_cache(&ccb.csio,
2542 /*begin_lba*/0, /* whole disk */
2547 xpt_polled_action(&ccb);
2549 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2550 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2551 CAM_SCSI_STATUS_ERROR)
2552 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
2553 int error_code, sense_key, asc, ascq;
2555 scsi_extract_sense(&ccb.csio.sense_data,
2556 &error_code, &sense_key,
2559 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
2560 scsi_sense_print(&ccb.csio);
2562 xpt_print(periph->path, "Synchronize "
2563 "cache failed, status == 0x%x, scsi status "
2564 "== 0x%x\n", ccb.ccb_h.status,
2565 ccb.csio.scsi_status);
2569 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
2570 cam_release_devq(ccb.ccb_h.path,
2574 /*getcount_only*/0);
2575 cam_periph_unlock(periph);
2579 #else /* !_KERNEL */
2582 * XXX This is only left out of the kernel build to silence warnings. If,
2583 * for some reason this function is used in the kernel, the ifdefs should
2584 * be moved so it is included both in the kernel and userland.
2587 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2588 void (*cbfcnp)(struct cam_periph *, union ccb *),
2589 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2590 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2593 struct scsi_format_unit *scsi_cmd;
2595 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2596 scsi_cmd->opcode = FORMAT_UNIT;
2597 scsi_cmd->byte2 = byte2;
2598 scsi_ulto2b(ileave, scsi_cmd->interleave);
2603 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2612 #endif /* _KERNEL */