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 xpt_release_ccb(ccb);
998 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
999 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
1000 daprevent(periph, PR_ALLOW);
1002 * If we've got removeable media, mark the blocksize as
1003 * unavailable, since it could change when new media is
1006 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1009 softc->flags &= ~DA_FLAG_OPEN;
1010 cam_periph_unhold(periph);
1011 cam_periph_unlock(periph);
1012 cam_periph_release(periph);
1017 * Actually translate the requested transfer into one the physical driver
1018 * can understand. The transfer is described by a buf and will include
1019 * only one physical transfer.
1022 dastrategy(struct bio *bp)
1024 struct cam_periph *periph;
1025 struct da_softc *softc;
1027 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1028 if (periph == NULL) {
1029 biofinish(bp, NULL, ENXIO);
1032 softc = (struct da_softc *)periph->softc;
1034 cam_periph_lock(periph);
1037 * If the device has been made invalid, error out
1039 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1040 cam_periph_unlock(periph);
1041 biofinish(bp, NULL, ENXIO);
1046 * Place it in the queue of disk activities for this disk
1048 bioq_disksort(&softc->bio_queue, bp);
1051 * Schedule ourselves for performing the work.
1053 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1054 cam_periph_unlock(periph);
1060 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1062 struct cam_periph *periph;
1063 struct da_softc *softc;
1065 struct ccb_scsiio csio;
1069 periph = dp->d_drv1;
1072 softc = (struct da_softc *)periph->softc;
1073 cam_periph_lock(periph);
1074 secsize = softc->params.secsize;
1076 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1077 cam_periph_unlock(periph);
1082 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1083 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1084 scsi_read_write(&csio,
1090 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1093 /*data_ptr*/(u_int8_t *) virtual,
1094 /*dxfer_len*/length,
1095 /*sense_len*/SSD_FULL_SIZE,
1096 da_default_timeout * 1000);
1097 xpt_polled_action((union ccb *)&csio);
1098 cam_periph_unlock(periph);
1100 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1101 printf("Aborting dump due to I/O error.\n");
1102 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
1103 CAM_SCSI_STATUS_ERROR)
1104 scsi_sense_print(&csio);
1106 printf("status == 0x%x, scsi status == 0x%x\n",
1107 csio.ccb_h.status, csio.scsi_status);
1114 * Sync the disk cache contents to the physical media.
1116 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1118 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1119 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1120 scsi_synchronize_cache(&csio,
1124 /*begin_lba*/0,/* Cover the whole disk */
1128 xpt_polled_action((union ccb *)&csio);
1130 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1131 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
1132 CAM_SCSI_STATUS_ERROR) {
1134 int sense_key, error_code;
1136 scsi_extract_sense_len(&csio.sense_data,
1137 csio.sense_len - csio.sense_resid,
1138 &error_code, &sense_key, &asc, &ascq,
1140 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1141 scsi_sense_print(&csio);
1143 xpt_print(periph->path, "Synchronize cache "
1144 "failed, status == 0x%x, scsi status == "
1145 "0x%x\n", csio.ccb_h.status,
1150 cam_periph_unlock(periph);
1155 dagetattr(struct bio *bp)
1158 struct cam_periph *periph;
1160 if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
1162 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1163 if (periph->path == NULL)
1166 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1169 bp->bio_completed = bp->bio_length;
1179 * Install a global async callback. This callback will
1180 * receive async callbacks like "new device found".
1182 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1184 if (status != CAM_REQ_CMP) {
1185 printf("da: Failed to attach master async callback "
1186 "due to status 0x%x!\n", status);
1187 } else if (da_send_ordered) {
1189 /* Register our shutdown event handler */
1190 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1191 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1192 printf("dainit: shutdown event registration failed!\n");
1197 daoninvalidate(struct cam_periph *periph)
1199 struct da_softc *softc;
1201 softc = (struct da_softc *)periph->softc;
1204 * De-register any async callbacks.
1206 xpt_register_async(0, daasync, periph, periph->path);
1208 softc->flags |= DA_FLAG_PACK_INVALID;
1211 * Return all queued I/O with ENXIO.
1212 * XXX Handle any transactions queued to the card
1213 * with XPT_ABORT_CCB.
1215 bioq_flush(&softc->bio_queue, NULL, ENXIO);
1217 disk_gone(softc->disk);
1218 xpt_print(periph->path, "lost device - %d outstanding, %d refs\n",
1219 softc->outstanding_cmds, periph->refcount);
1223 dacleanup(struct cam_periph *periph)
1225 struct da_softc *softc;
1227 softc = (struct da_softc *)periph->softc;
1229 xpt_print(periph->path, "removing device entry\n");
1230 cam_periph_unlock(periph);
1233 * If we can't free the sysctl tree, oh well...
1235 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1236 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1237 xpt_print(periph->path, "can't remove sysctl context\n");
1240 disk_destroy(softc->disk);
1241 callout_drain(&softc->sendordered_c);
1242 free(softc, M_DEVBUF);
1243 cam_periph_lock(periph);
1247 daasync(void *callback_arg, u_int32_t code,
1248 struct cam_path *path, void *arg)
1250 struct cam_periph *periph;
1252 periph = (struct cam_periph *)callback_arg;
1254 case AC_FOUND_DEVICE:
1256 struct ccb_getdev *cgd;
1259 cgd = (struct ccb_getdev *)arg;
1263 if (cgd->protocol != PROTO_SCSI)
1266 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1267 && SID_TYPE(&cgd->inq_data) != T_RBC
1268 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1272 * Allocate a peripheral instance for
1273 * this device and start the probe
1276 status = cam_periph_alloc(daregister, daoninvalidate,
1278 "da", CAM_PERIPH_BIO,
1279 cgd->ccb_h.path, daasync,
1280 AC_FOUND_DEVICE, cgd);
1282 if (status != CAM_REQ_CMP
1283 && status != CAM_REQ_INPROG)
1284 printf("daasync: Unable to attach to new device "
1285 "due to status 0x%x\n", status);
1288 case AC_ADVINFO_CHANGED:
1292 buftype = (uintptr_t)arg;
1293 if (buftype == CDAI_TYPE_PHYS_PATH) {
1294 struct da_softc *softc;
1296 softc = periph->softc;
1297 disk_attr_changed(softc->disk, "GEOM::physpath",
1305 struct da_softc *softc;
1306 struct ccb_hdr *ccbh;
1308 softc = (struct da_softc *)periph->softc;
1310 * Don't fail on the expected unit attention
1313 softc->flags |= DA_FLAG_RETRY_UA;
1314 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1315 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1321 cam_periph_async(periph, code, path, arg);
1325 dasysctlinit(void *context, int pending)
1327 struct cam_periph *periph;
1328 struct da_softc *softc;
1329 char tmpstr[80], tmpstr2[80];
1330 struct ccb_trans_settings cts;
1332 periph = (struct cam_periph *)context;
1334 * periph was held for us when this task was enqueued
1336 if (periph->flags & CAM_PERIPH_INVALID) {
1337 cam_periph_release(periph);
1341 softc = (struct da_softc *)periph->softc;
1342 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1343 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1345 sysctl_ctx_init(&softc->sysctl_ctx);
1346 softc->flags |= DA_FLAG_SCTX_INIT;
1347 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1348 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1349 CTLFLAG_RD, 0, tmpstr);
1350 if (softc->sysctl_tree == NULL) {
1351 printf("dasysctlinit: unable to allocate sysctl tree\n");
1352 cam_periph_release(periph);
1357 * Now register the sysctl handler, so the user can change the value on
1360 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1361 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1362 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1363 "Minimum CDB size");
1365 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1366 SYSCTL_CHILDREN(softc->sysctl_tree),
1370 &softc->error_inject,
1372 "error_inject leaf");
1376 * Add some addressing info.
1378 memset(&cts, 0, sizeof (cts));
1379 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
1380 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1381 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1382 cam_periph_lock(periph);
1383 xpt_action((union ccb *)&cts);
1384 cam_periph_unlock(periph);
1385 if (cts.ccb_h.status != CAM_REQ_CMP) {
1386 cam_periph_release(periph);
1389 if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
1390 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1391 if (fc->valid & CTS_FC_VALID_WWPN) {
1392 softc->wwpn = fc->wwpn;
1393 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1394 SYSCTL_CHILDREN(softc->sysctl_tree),
1395 OID_AUTO, "wwpn", CTLFLAG_RD,
1396 &softc->wwpn, "World Wide Port Name");
1399 cam_periph_release(periph);
1403 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1407 value = *(int *)arg1;
1409 error = sysctl_handle_int(oidp, &value, 0, req);
1412 || (req->newptr == NULL))
1416 * Acceptable values here are 6, 10, 12 or 16.
1420 else if ((value > 6)
1423 else if ((value > 10)
1426 else if (value > 12)
1429 *(int *)arg1 = value;
1435 daregister(struct cam_periph *periph, void *arg)
1437 struct da_softc *softc;
1438 struct ccb_pathinq cpi;
1439 struct ccb_getdev *cgd;
1443 cgd = (struct ccb_getdev *)arg;
1444 if (periph == NULL) {
1445 printf("daregister: periph was NULL!!\n");
1446 return(CAM_REQ_CMP_ERR);
1450 printf("daregister: no getdev CCB, can't register device\n");
1451 return(CAM_REQ_CMP_ERR);
1454 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
1457 if (softc == NULL) {
1458 printf("daregister: Unable to probe new device. "
1459 "Unable to allocate softc\n");
1460 return(CAM_REQ_CMP_ERR);
1463 LIST_INIT(&softc->pending_ccbs);
1464 softc->state = DA_STATE_PROBE;
1465 bioq_init(&softc->bio_queue);
1466 if (SID_IS_REMOVABLE(&cgd->inq_data))
1467 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1468 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1469 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1471 periph->softc = softc;
1474 * See if this device has any quirks.
1476 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1477 (caddr_t)da_quirk_table,
1478 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1479 sizeof(*da_quirk_table), scsi_inquiry_match);
1482 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1484 softc->quirks = DA_Q_NONE;
1486 /* Check if the SIM does not want 6 byte commands */
1487 bzero(&cpi, sizeof(cpi));
1488 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1489 cpi.ccb_h.func_code = XPT_PATH_INQ;
1490 xpt_action((union ccb *)&cpi);
1491 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1492 softc->quirks |= DA_Q_NO_6_BYTE;
1494 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1497 * Take an exclusive refcount on the periph while dastart is called
1498 * to finish the probe. The reference will be dropped in dadone at
1501 (void)cam_periph_hold(periph, PRIBIO);
1504 * Schedule a periodic event to occasionally send an
1505 * ordered tag to a device.
1507 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
1508 callout_reset(&softc->sendordered_c,
1509 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
1510 dasendorderedtag, softc);
1512 mtx_unlock(periph->sim->mtx);
1514 * RBC devices don't have to support READ(6), only READ(10).
1516 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1517 softc->minimum_cmd_size = 10;
1519 softc->minimum_cmd_size = 6;
1522 * Load the user's default, if any.
1524 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1525 periph->unit_number);
1526 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1529 * 6, 10, 12 and 16 are the currently permissible values.
1531 if (softc->minimum_cmd_size < 6)
1532 softc->minimum_cmd_size = 6;
1533 else if ((softc->minimum_cmd_size > 6)
1534 && (softc->minimum_cmd_size <= 10))
1535 softc->minimum_cmd_size = 10;
1536 else if ((softc->minimum_cmd_size > 10)
1537 && (softc->minimum_cmd_size <= 12))
1538 softc->minimum_cmd_size = 12;
1539 else if (softc->minimum_cmd_size > 12)
1540 softc->minimum_cmd_size = 16;
1542 /* Predict whether device may support READ CAPACITY(16). */
1543 if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3) {
1544 softc->flags |= DA_FLAG_CAN_RC16;
1545 softc->state = DA_STATE_PROBE2;
1549 * Register this media as a disk.
1551 softc->disk = disk_alloc();
1552 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
1553 periph->unit_number, 0,
1554 DEVSTAT_BS_UNAVAILABLE,
1555 SID_TYPE(&cgd->inq_data) |
1556 XPORT_DEVSTAT_TYPE(cpi.transport),
1557 DEVSTAT_PRIORITY_DISK);
1558 softc->disk->d_open = daopen;
1559 softc->disk->d_close = daclose;
1560 softc->disk->d_strategy = dastrategy;
1561 softc->disk->d_dump = dadump;
1562 softc->disk->d_getattr = dagetattr;
1563 softc->disk->d_name = "da";
1564 softc->disk->d_drv1 = periph;
1566 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */
1567 else if (cpi.maxio > MAXPHYS)
1568 softc->disk->d_maxsize = MAXPHYS; /* for safety */
1570 softc->disk->d_maxsize = cpi.maxio;
1571 softc->disk->d_unit = periph->unit_number;
1572 softc->disk->d_flags = 0;
1573 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
1574 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
1575 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
1576 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
1577 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
1578 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
1579 cgd->inq_data.product, sizeof(cgd->inq_data.product),
1580 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
1581 softc->disk->d_hba_vendor = cpi.hba_vendor;
1582 softc->disk->d_hba_device = cpi.hba_device;
1583 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
1584 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
1585 disk_create(softc->disk, DISK_VERSION);
1586 mtx_lock(periph->sim->mtx);
1589 * Add async callbacks for events of interest.
1590 * I don't bother checking if this fails as,
1591 * in most cases, the system will function just
1592 * fine without them and the only alternative
1593 * would be to not attach the device on failure.
1595 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET
1596 | AC_LOST_DEVICE | AC_ADVINFO_CHANGED,
1597 daasync, periph, periph->path);
1600 * Emit an attribute changed notification just in case
1601 * physical path information arrived before our async
1602 * event handler was registered, but after anyone attaching
1603 * to our disk device polled it.
1605 disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
1607 xpt_schedule(periph, CAM_PRIORITY_DEV);
1609 return(CAM_REQ_CMP);
1613 dastart(struct cam_periph *periph, union ccb *start_ccb)
1615 struct da_softc *softc;
1617 softc = (struct da_softc *)periph->softc;
1619 switch (softc->state) {
1620 case DA_STATE_NORMAL:
1622 /* Pull a buffer from the queue and get going on it */
1626 * See if there is a buf with work for us to do..
1628 bp = bioq_first(&softc->bio_queue);
1629 if (periph->immediate_priority <= periph->pinfo.priority) {
1630 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1631 ("queuing for immediate ccb\n"));
1632 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1633 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1635 periph->immediate_priority = CAM_PRIORITY_NONE;
1636 wakeup(&periph->ccb_list);
1637 } else if (bp == NULL) {
1638 xpt_release_ccb(start_ccb);
1642 bioq_remove(&softc->bio_queue, bp);
1644 if ((bp->bio_flags & BIO_ORDERED) != 0
1645 || (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1646 softc->flags &= ~DA_FLAG_NEED_OTAG;
1647 softc->ordered_tag_count++;
1648 tag_code = MSG_ORDERED_Q_TAG;
1650 tag_code = MSG_SIMPLE_Q_TAG;
1652 switch (bp->bio_cmd) {
1655 scsi_read_write(&start_ccb->csio,
1656 /*retries*/da_retry_count,
1658 /*tag_action*/tag_code,
1659 /*read_op*/bp->bio_cmd
1662 softc->minimum_cmd_size,
1663 /*lba*/bp->bio_pblkno,
1664 /*block_count*/bp->bio_bcount /
1665 softc->params.secsize,
1666 /*data_ptr*/ bp->bio_data,
1667 /*dxfer_len*/ bp->bio_bcount,
1668 /*sense_len*/SSD_FULL_SIZE,
1669 da_default_timeout * 1000);
1673 * BIO_FLUSH doesn't currently communicate
1674 * range data, so we synchronize the cache
1675 * over the whole disk. We also force
1676 * ordered tag semantics the flush applies
1677 * to all previously queued I/O.
1679 scsi_synchronize_cache(&start_ccb->csio,
1686 da_default_timeout*1000);
1689 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1692 * Block out any asyncronous callbacks
1693 * while we touch the pending ccb list.
1695 LIST_INSERT_HEAD(&softc->pending_ccbs,
1696 &start_ccb->ccb_h, periph_links.le);
1697 softc->outstanding_cmds++;
1699 /* We expect a unit attention from this device */
1700 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1701 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1702 softc->flags &= ~DA_FLAG_RETRY_UA;
1705 start_ccb->ccb_h.ccb_bp = bp;
1706 bp = bioq_first(&softc->bio_queue);
1708 xpt_action(start_ccb);
1712 /* Have more work to do, so ensure we stay scheduled */
1713 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1717 case DA_STATE_PROBE:
1719 struct ccb_scsiio *csio;
1720 struct scsi_read_capacity_data *rcap;
1722 rcap = (struct scsi_read_capacity_data *)
1723 malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
1725 printf("dastart: Couldn't malloc read_capacity data\n");
1726 /* da_free_periph??? */
1729 csio = &start_ccb->csio;
1730 scsi_read_capacity(csio,
1737 start_ccb->ccb_h.ccb_bp = NULL;
1738 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1739 xpt_action(start_ccb);
1742 case DA_STATE_PROBE2:
1744 struct ccb_scsiio *csio;
1745 struct scsi_read_capacity_data_long *rcaplong;
1747 rcaplong = (struct scsi_read_capacity_data_long *)
1748 malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
1749 if (rcaplong == NULL) {
1750 printf("dastart: Couldn't malloc read_capacity data\n");
1751 /* da_free_periph??? */
1754 csio = &start_ccb->csio;
1755 scsi_read_capacity_16(csio,
1758 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1763 /*sense_len*/ SSD_FULL_SIZE,
1765 start_ccb->ccb_h.ccb_bp = NULL;
1766 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1767 xpt_action(start_ccb);
1774 cmd6workaround(union ccb *ccb)
1776 struct scsi_rw_6 cmd6;
1777 struct scsi_rw_10 *cmd10;
1778 struct da_softc *softc;
1782 cdb = ccb->csio.cdb_io.cdb_bytes;
1784 /* Translation only possible if CDB is an array and cmd is R/W6 */
1785 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1786 (*cdb != READ_6 && *cdb != WRITE_6))
1789 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
1790 "increasing minimum_cmd_size to 10.\n");
1791 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1792 softc->minimum_cmd_size = 10;
1794 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1795 cmd10 = (struct scsi_rw_10 *)cdb;
1796 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1798 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1799 cmd10->reserved = 0;
1800 scsi_ulto2b(cmd6.length, cmd10->length);
1801 cmd10->control = cmd6.control;
1802 ccb->csio.cdb_len = sizeof(*cmd10);
1804 /* Requeue request, unfreezing queue if necessary */
1805 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1806 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1809 cam_release_devq(ccb->ccb_h.path,
1813 /*getcount_only*/0);
1819 dadone(struct cam_periph *periph, union ccb *done_ccb)
1821 struct da_softc *softc;
1822 struct ccb_scsiio *csio;
1825 softc = (struct da_softc *)periph->softc;
1826 priority = done_ccb->ccb_h.pinfo.priority;
1827 csio = &done_ccb->csio;
1828 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1829 case DA_CCB_BUFFER_IO:
1833 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1834 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1838 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1843 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1844 if (error == ERESTART) {
1846 * A retry was scheuled, so
1855 * return all queued I/O with EIO, so that
1856 * the client can retry these I/Os in the
1857 * proper order should it attempt to recover.
1862 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
1864 * Catastrophic error. Mark our pack as
1868 * XXX See if this is really a media
1871 xpt_print(periph->path,
1872 "Invalidating pack\n");
1873 softc->flags |= DA_FLAG_PACK_INVALID;
1874 queued_error = ENXIO;
1876 bioq_flush(&softc->bio_queue, NULL,
1878 bp->bio_error = error;
1879 bp->bio_resid = bp->bio_bcount;
1880 bp->bio_flags |= BIO_ERROR;
1882 bp->bio_resid = csio->resid;
1884 if (bp->bio_resid != 0)
1885 bp->bio_flags |= BIO_ERROR;
1887 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1888 cam_release_devq(done_ccb->ccb_h.path,
1892 /*getcount_only*/0);
1894 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1895 panic("REQ_CMP with QFRZN");
1896 bp->bio_resid = csio->resid;
1897 if (csio->resid > 0)
1898 bp->bio_flags |= BIO_ERROR;
1899 if (softc->error_inject != 0) {
1900 bp->bio_error = softc->error_inject;
1901 bp->bio_resid = bp->bio_bcount;
1902 bp->bio_flags |= BIO_ERROR;
1903 softc->error_inject = 0;
1909 * Block out any asyncronous callbacks
1910 * while we touch the pending ccb list.
1912 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1913 softc->outstanding_cmds--;
1914 if (softc->outstanding_cmds == 0)
1915 softc->flags |= DA_FLAG_WENT_IDLE;
1917 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1918 xpt_print(periph->path, "oustanding %d\n",
1919 softc->outstanding_cmds);
1928 struct scsi_read_capacity_data *rdcap;
1929 struct scsi_read_capacity_data_long *rcaplong;
1930 char announce_buf[80];
1934 if (softc->state == DA_STATE_PROBE)
1935 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1937 rcaplong = (struct scsi_read_capacity_data_long *)
1940 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1941 struct disk_params *dp;
1942 uint32_t block_size;
1944 u_int lbppbe; /* LB per physical block exponent. */
1945 u_int lalba; /* Lowest aligned LBA. */
1947 if (softc->state == DA_STATE_PROBE) {
1948 block_size = scsi_4btoul(rdcap->length);
1949 maxsector = scsi_4btoul(rdcap->addr);
1954 * According to SBC-2, if the standard 10
1955 * byte READ CAPACITY command returns 2^32,
1956 * we should issue the 16 byte version of
1957 * the command, since the device in question
1958 * has more sectors than can be represented
1959 * with the short version of the command.
1961 if (maxsector == 0xffffffff) {
1962 softc->state = DA_STATE_PROBE2;
1963 free(rdcap, M_SCSIDA);
1964 xpt_release_ccb(done_ccb);
1965 xpt_schedule(periph, priority);
1969 block_size = scsi_4btoul(rcaplong->length);
1970 maxsector = scsi_8btou64(rcaplong->addr);
1971 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
1972 lalba = scsi_2btoul(rcaplong->lalba_lbp);
1976 * Because GEOM code just will panic us if we
1977 * give them an 'illegal' value we'll avoid that
1980 if (block_size == 0 && maxsector == 0) {
1981 snprintf(announce_buf, sizeof(announce_buf),
1983 } else if (block_size >= MAXPHYS || block_size == 0) {
1984 xpt_print(periph->path,
1985 "unsupportable block size %ju\n",
1986 (uintmax_t) block_size);
1987 announce_buf[0] = '\0';
1988 cam_periph_invalidate(periph);
1990 dasetgeom(periph, block_size, maxsector,
1991 lbppbe, lalba & SRC16_LALBA);
1992 if (lalba & SRC16_LBPME)
1993 softc->flags |= DA_FLAG_CAN_LBPME;
1995 softc->flags &= ~DA_FLAG_CAN_LBPME;
1996 dp = &softc->params;
1997 snprintf(announce_buf, sizeof(announce_buf),
1998 "%juMB (%ju %u byte sectors: %dH %dS/T "
2000 (((uintmax_t)dp->secsize *
2001 dp->sectors) / (1024*1024)),
2002 (uintmax_t)dp->sectors,
2003 dp->secsize, dp->heads,
2004 dp->secs_per_track, dp->cylinders);
2009 announce_buf[0] = '\0';
2012 * Retry any UNIT ATTENTION type errors. They
2013 * are expected at boot.
2015 error = daerror(done_ccb, CAM_RETRY_SELTO,
2016 SF_RETRY_UA|SF_NO_PRINT);
2017 if (error == ERESTART) {
2019 * A retry was scheuled, so
2023 } else if (error != 0) {
2024 struct scsi_sense_data *sense;
2026 int sense_key, error_code;
2029 struct ccb_getdev cgd;
2031 /* Don't wedge this device's queue */
2032 status = done_ccb->ccb_h.status;
2033 if ((status & CAM_DEV_QFRZN) != 0)
2034 cam_release_devq(done_ccb->ccb_h.path,
2038 /*getcount_only*/0);
2041 xpt_setup_ccb(&cgd.ccb_h,
2042 done_ccb->ccb_h.path,
2043 CAM_PRIORITY_NORMAL);
2044 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2045 xpt_action((union ccb *)&cgd);
2047 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
2048 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
2049 || ((status & CAM_AUTOSNS_VALID) == 0))
2055 sense = &csio->sense_data;
2056 scsi_extract_sense_len(sense,
2057 csio->sense_len - csio->sense_resid,
2058 &error_code, &sense_key, &asc,
2059 &ascq, /*show_errors*/ 1);
2062 * If we tried READ CAPACITY(16) and failed,
2063 * fallback to READ CAPACITY(10).
2065 if ((softc->state == DA_STATE_PROBE2) &&
2066 (softc->flags & DA_FLAG_CAN_RC16) &&
2067 (((csio->ccb_h.status & CAM_STATUS_MASK) ==
2070 (error_code == SSD_CURRENT_ERROR) &&
2071 (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
2072 softc->flags &= ~DA_FLAG_CAN_RC16;
2073 softc->state = DA_STATE_PROBE;
2074 free(rdcap, M_SCSIDA);
2075 xpt_release_ccb(done_ccb);
2076 xpt_schedule(periph, priority);
2080 * Attach to anything that claims to be a
2081 * direct access or optical disk device,
2082 * as long as it doesn't return a "Logical
2083 * unit not supported" (0x25) error.
2085 if ((have_sense) && (asc != 0x25)
2086 && (error_code == SSD_CURRENT_ERROR)) {
2087 const char *sense_key_desc;
2088 const char *asc_desc;
2090 scsi_sense_desc(sense_key, asc, ascq,
2094 snprintf(announce_buf,
2095 sizeof(announce_buf),
2096 "Attempt to query device "
2097 "size failed: %s, %s",
2105 xpt_print(periph->path,
2106 "got CAM status %#x\n",
2107 done_ccb->ccb_h.status);
2110 xpt_print(periph->path, "fatal error, "
2111 "failed to attach to device\n");
2114 * Free up resources.
2116 cam_periph_invalidate(periph);
2120 free(csio->data_ptr, M_SCSIDA);
2121 if (announce_buf[0] != '\0') {
2123 * Create our sysctl variables, now that we know
2124 * we have successfully attached.
2126 /* increase the refcount */
2127 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
2128 taskqueue_enqueue(taskqueue_thread,
2129 &softc->sysctl_task);
2130 xpt_announce_periph(periph, announce_buf);
2132 xpt_print(periph->path, "fatal error, "
2133 "could not acquire reference count\n");
2137 softc->state = DA_STATE_NORMAL;
2139 * Since our peripheral may be invalidated by an error
2140 * above or an external event, we must release our CCB
2141 * before releasing the probe lock on the peripheral.
2142 * The peripheral will only go away once the last lock
2143 * is removed, and we need it around for the CCB release
2146 xpt_release_ccb(done_ccb);
2147 cam_periph_unhold(periph);
2150 case DA_CCB_WAITING:
2152 /* Caller will release the CCB */
2153 wakeup(&done_ccb->ccb_h.cbfcnp);
2157 /* No-op. We're polling */
2162 xpt_release_ccb(done_ccb);
2166 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
2168 struct da_softc *softc;
2169 struct cam_periph *periph;
2172 periph = xpt_path_periph(ccb->ccb_h.path);
2173 softc = (struct da_softc *)periph->softc;
2176 * Automatically detect devices that do not support
2177 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
2180 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
2181 error = cmd6workaround(ccb);
2182 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
2183 CAM_SCSI_STATUS_ERROR)
2184 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
2185 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
2186 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
2187 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
2188 int sense_key, error_code, asc, ascq;
2190 scsi_extract_sense(&ccb->csio.sense_data,
2191 &error_code, &sense_key, &asc, &ascq);
2192 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
2193 error = cmd6workaround(ccb);
2195 if (error == ERESTART)
2200 * Until we have a better way of doing pack validation,
2201 * don't treat UAs as errors.
2203 sense_flags |= SF_RETRY_UA;
2204 return(cam_periph_error(ccb, cam_flags, sense_flags,
2205 &softc->saved_ccb));
2209 daprevent(struct cam_periph *periph, int action)
2211 struct da_softc *softc;
2215 softc = (struct da_softc *)periph->softc;
2217 if (((action == PR_ALLOW)
2218 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
2219 || ((action == PR_PREVENT)
2220 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
2224 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2226 scsi_prevent(&ccb->csio,
2234 error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
2235 SF_RETRY_UA, softc->disk->d_devstat);
2238 if (action == PR_ALLOW)
2239 softc->flags &= ~DA_FLAG_PACK_LOCKED;
2241 softc->flags |= DA_FLAG_PACK_LOCKED;
2244 xpt_release_ccb(ccb);
2248 dagetcapacity(struct cam_periph *periph)
2250 struct da_softc *softc;
2252 struct scsi_read_capacity_data *rcap;
2253 struct scsi_read_capacity_data_long *rcaplong;
2256 int error, rc16failed;
2257 u_int32_t sense_flags;
2258 u_int lbppbe; /* Logical blocks per physical block exponent. */
2259 u_int lalba; /* Lowest aligned LBA. */
2261 softc = (struct da_softc *)periph->softc;
2268 sense_flags = SF_RETRY_UA;
2269 if (softc->flags & DA_FLAG_PACK_REMOVABLE)
2270 sense_flags |= SF_NO_PRINT;
2272 /* Do a read capacity */
2273 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcaplong),
2278 rcaplong = (struct scsi_read_capacity_data_long *)rcap;
2280 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2282 /* Try READ CAPACITY(16) first if we think it should work. */
2283 if (softc->flags & DA_FLAG_CAN_RC16) {
2284 scsi_read_capacity_16(&ccb->csio,
2287 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2292 /*sense_len*/ SSD_FULL_SIZE,
2294 ccb->ccb_h.ccb_bp = NULL;
2296 error = cam_periph_runccb(ccb, daerror,
2297 /*cam_flags*/CAM_RETRY_SELTO,
2299 softc->disk->d_devstat);
2303 /* If we got ILLEGAL REQUEST, do not prefer RC16 any more. */
2304 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
2306 softc->flags &= ~DA_FLAG_CAN_RC16;
2307 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
2308 CAM_SCSI_STATUS_ERROR) &&
2309 (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND) &&
2310 (ccb->ccb_h.status & CAM_AUTOSNS_VALID) &&
2311 ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0) &&
2312 ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
2313 int sense_key, error_code, asc, ascq;
2315 scsi_extract_sense(&ccb->csio.sense_data,
2316 &error_code, &sense_key, &asc, &ascq);
2317 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
2318 softc->flags &= ~DA_FLAG_CAN_RC16;
2323 /* Do READ CAPACITY(10). */
2324 scsi_read_capacity(&ccb->csio,
2331 ccb->ccb_h.ccb_bp = NULL;
2333 error = cam_periph_runccb(ccb, daerror,
2334 /*cam_flags*/CAM_RETRY_SELTO,
2336 softc->disk->d_devstat);
2338 block_len = scsi_4btoul(rcap->length);
2339 maxsector = scsi_4btoul(rcap->addr);
2341 if (maxsector != 0xffffffff || rc16failed)
2346 /* If READ CAPACITY(10) returned overflow, use READ CAPACITY(16) */
2347 scsi_read_capacity_16(&ccb->csio,
2350 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2355 /*sense_len*/ SSD_FULL_SIZE,
2357 ccb->ccb_h.ccb_bp = NULL;
2359 error = cam_periph_runccb(ccb, daerror,
2360 /*cam_flags*/CAM_RETRY_SELTO,
2362 softc->disk->d_devstat);
2365 block_len = scsi_4btoul(rcaplong->length);
2366 maxsector = scsi_8btou64(rcaplong->addr);
2367 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
2368 lalba = scsi_2btoul(rcaplong->lalba_lbp);
2374 if (block_len >= MAXPHYS || block_len == 0) {
2375 xpt_print(periph->path,
2376 "unsupportable block size %ju\n",
2377 (uintmax_t) block_len);
2380 dasetgeom(periph, block_len, maxsector,
2381 lbppbe, lalba & SRC16_LALBA);
2382 if (lalba & SRC16_LBPME)
2383 softc->flags |= DA_FLAG_CAN_LBPME;
2385 softc->flags &= ~DA_FLAG_CAN_LBPME;
2389 xpt_release_ccb(ccb);
2391 free(rcap, M_SCSIDA);
2397 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
2398 u_int lbppbe, u_int lalba)
2400 struct ccb_calc_geometry ccg;
2401 struct da_softc *softc;
2402 struct disk_params *dp;
2404 softc = (struct da_softc *)periph->softc;
2406 dp = &softc->params;
2407 dp->secsize = block_len;
2408 dp->sectors = maxsector + 1;
2410 dp->stripesize = block_len << lbppbe;
2411 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
2413 } else if (softc->quirks & DA_Q_4K) {
2414 dp->stripesize = 4096;
2415 dp->stripeoffset = 0;
2418 dp->stripeoffset = 0;
2421 * Have the controller provide us with a geometry
2422 * for this disk. The only time the geometry
2423 * matters is when we boot and the controller
2424 * is the only one knowledgeable enough to come
2425 * up with something that will make this a bootable
2428 xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2429 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
2430 ccg.block_size = dp->secsize;
2431 ccg.volume_size = dp->sectors;
2433 ccg.secs_per_track = 0;
2435 xpt_action((union ccb*)&ccg);
2436 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2438 * We don't know what went wrong here- but just pick
2439 * a geometry so we don't have nasty things like divide
2443 dp->secs_per_track = 255;
2444 dp->cylinders = dp->sectors / (255 * 255);
2445 if (dp->cylinders == 0) {
2449 dp->heads = ccg.heads;
2450 dp->secs_per_track = ccg.secs_per_track;
2451 dp->cylinders = ccg.cylinders;
2456 dasendorderedtag(void *arg)
2458 struct da_softc *softc = arg;
2460 if (da_send_ordered) {
2461 if ((softc->ordered_tag_count == 0)
2462 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
2463 softc->flags |= DA_FLAG_NEED_OTAG;
2465 if (softc->outstanding_cmds > 0)
2466 softc->flags &= ~DA_FLAG_WENT_IDLE;
2468 softc->ordered_tag_count = 0;
2470 /* Queue us up again */
2471 callout_reset(&softc->sendordered_c,
2472 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2473 dasendorderedtag, softc);
2477 * Step through all DA peripheral drivers, and if the device is still open,
2478 * sync the disk cache to physical media.
2481 dashutdown(void * arg, int howto)
2483 struct cam_periph *periph;
2484 struct da_softc *softc;
2486 TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
2489 cam_periph_lock(periph);
2490 softc = (struct da_softc *)periph->softc;
2493 * We only sync the cache if the drive is still open, and
2494 * if the drive is capable of it..
2496 if (((softc->flags & DA_FLAG_OPEN) == 0)
2497 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
2498 cam_periph_unlock(periph);
2502 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2504 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
2505 scsi_synchronize_cache(&ccb.csio,
2509 /*begin_lba*/0, /* whole disk */
2514 xpt_polled_action(&ccb);
2516 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2517 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2518 CAM_SCSI_STATUS_ERROR)
2519 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
2520 int error_code, sense_key, asc, ascq;
2522 scsi_extract_sense(&ccb.csio.sense_data,
2523 &error_code, &sense_key,
2526 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
2527 scsi_sense_print(&ccb.csio);
2529 xpt_print(periph->path, "Synchronize "
2530 "cache failed, status == 0x%x, scsi status "
2531 "== 0x%x\n", ccb.ccb_h.status,
2532 ccb.csio.scsi_status);
2536 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
2537 cam_release_devq(ccb.ccb_h.path,
2541 /*getcount_only*/0);
2542 cam_periph_unlock(periph);
2546 #else /* !_KERNEL */
2549 * XXX This is only left out of the kernel build to silence warnings. If,
2550 * for some reason this function is used in the kernel, the ifdefs should
2551 * be moved so it is included both in the kernel and userland.
2554 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2555 void (*cbfcnp)(struct cam_periph *, union ccb *),
2556 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2557 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2560 struct scsi_format_unit *scsi_cmd;
2562 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2563 scsi_cmd->opcode = FORMAT_UNIT;
2564 scsi_cmd->byte2 = byte2;
2565 scsi_ulto2b(ileave, scsi_cmd->interleave);
2570 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2579 #endif /* _KERNEL */