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 (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC &&
1545 (cgd->inq_data.spc3_flags & SPC3_SID_PROTECT))) {
1546 softc->flags |= DA_FLAG_CAN_RC16;
1547 softc->state = DA_STATE_PROBE2;
1551 * Register this media as a disk.
1553 softc->disk = disk_alloc();
1554 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
1555 periph->unit_number, 0,
1556 DEVSTAT_BS_UNAVAILABLE,
1557 SID_TYPE(&cgd->inq_data) |
1558 XPORT_DEVSTAT_TYPE(cpi.transport),
1559 DEVSTAT_PRIORITY_DISK);
1560 softc->disk->d_open = daopen;
1561 softc->disk->d_close = daclose;
1562 softc->disk->d_strategy = dastrategy;
1563 softc->disk->d_dump = dadump;
1564 softc->disk->d_getattr = dagetattr;
1565 softc->disk->d_name = "da";
1566 softc->disk->d_drv1 = periph;
1568 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */
1569 else if (cpi.maxio > MAXPHYS)
1570 softc->disk->d_maxsize = MAXPHYS; /* for safety */
1572 softc->disk->d_maxsize = cpi.maxio;
1573 softc->disk->d_unit = periph->unit_number;
1574 softc->disk->d_flags = 0;
1575 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
1576 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
1577 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
1578 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
1579 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
1580 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
1581 cgd->inq_data.product, sizeof(cgd->inq_data.product),
1582 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
1583 softc->disk->d_hba_vendor = cpi.hba_vendor;
1584 softc->disk->d_hba_device = cpi.hba_device;
1585 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
1586 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
1587 disk_create(softc->disk, DISK_VERSION);
1588 mtx_lock(periph->sim->mtx);
1591 * Add async callbacks for events of interest.
1592 * I don't bother checking if this fails as,
1593 * in most cases, the system will function just
1594 * fine without them and the only alternative
1595 * would be to not attach the device on failure.
1597 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET
1598 | AC_LOST_DEVICE | AC_ADVINFO_CHANGED,
1599 daasync, periph, periph->path);
1602 * Emit an attribute changed notification just in case
1603 * physical path information arrived before our async
1604 * event handler was registered, but after anyone attaching
1605 * to our disk device polled it.
1607 disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
1609 xpt_schedule(periph, CAM_PRIORITY_DEV);
1611 return(CAM_REQ_CMP);
1615 dastart(struct cam_periph *periph, union ccb *start_ccb)
1617 struct da_softc *softc;
1619 softc = (struct da_softc *)periph->softc;
1621 switch (softc->state) {
1622 case DA_STATE_NORMAL:
1624 /* Pull a buffer from the queue and get going on it */
1628 * See if there is a buf with work for us to do..
1630 bp = bioq_first(&softc->bio_queue);
1631 if (periph->immediate_priority <= periph->pinfo.priority) {
1632 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1633 ("queuing for immediate ccb\n"));
1634 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1635 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1637 periph->immediate_priority = CAM_PRIORITY_NONE;
1638 wakeup(&periph->ccb_list);
1639 } else if (bp == NULL) {
1640 xpt_release_ccb(start_ccb);
1644 bioq_remove(&softc->bio_queue, bp);
1646 if ((bp->bio_flags & BIO_ORDERED) != 0
1647 || (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1648 softc->flags &= ~DA_FLAG_NEED_OTAG;
1649 softc->ordered_tag_count++;
1650 tag_code = MSG_ORDERED_Q_TAG;
1652 tag_code = MSG_SIMPLE_Q_TAG;
1654 switch (bp->bio_cmd) {
1657 scsi_read_write(&start_ccb->csio,
1658 /*retries*/da_retry_count,
1660 /*tag_action*/tag_code,
1661 /*read_op*/bp->bio_cmd
1664 softc->minimum_cmd_size,
1665 /*lba*/bp->bio_pblkno,
1666 /*block_count*/bp->bio_bcount /
1667 softc->params.secsize,
1668 /*data_ptr*/ bp->bio_data,
1669 /*dxfer_len*/ bp->bio_bcount,
1670 /*sense_len*/SSD_FULL_SIZE,
1671 da_default_timeout * 1000);
1675 * BIO_FLUSH doesn't currently communicate
1676 * range data, so we synchronize the cache
1677 * over the whole disk. We also force
1678 * ordered tag semantics the flush applies
1679 * to all previously queued I/O.
1681 scsi_synchronize_cache(&start_ccb->csio,
1688 da_default_timeout*1000);
1691 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1694 * Block out any asyncronous callbacks
1695 * while we touch the pending ccb list.
1697 LIST_INSERT_HEAD(&softc->pending_ccbs,
1698 &start_ccb->ccb_h, periph_links.le);
1699 softc->outstanding_cmds++;
1701 /* We expect a unit attention from this device */
1702 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1703 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1704 softc->flags &= ~DA_FLAG_RETRY_UA;
1707 start_ccb->ccb_h.ccb_bp = bp;
1708 bp = bioq_first(&softc->bio_queue);
1710 xpt_action(start_ccb);
1714 /* Have more work to do, so ensure we stay scheduled */
1715 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1719 case DA_STATE_PROBE:
1721 struct ccb_scsiio *csio;
1722 struct scsi_read_capacity_data *rcap;
1724 rcap = (struct scsi_read_capacity_data *)
1725 malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
1727 printf("dastart: Couldn't malloc read_capacity data\n");
1728 /* da_free_periph??? */
1731 csio = &start_ccb->csio;
1732 scsi_read_capacity(csio,
1739 start_ccb->ccb_h.ccb_bp = NULL;
1740 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1741 xpt_action(start_ccb);
1744 case DA_STATE_PROBE2:
1746 struct ccb_scsiio *csio;
1747 struct scsi_read_capacity_data_long *rcaplong;
1749 rcaplong = (struct scsi_read_capacity_data_long *)
1750 malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
1751 if (rcaplong == NULL) {
1752 printf("dastart: Couldn't malloc read_capacity data\n");
1753 /* da_free_periph??? */
1756 csio = &start_ccb->csio;
1757 scsi_read_capacity_16(csio,
1760 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1765 /*sense_len*/ SSD_FULL_SIZE,
1767 start_ccb->ccb_h.ccb_bp = NULL;
1768 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1769 xpt_action(start_ccb);
1776 cmd6workaround(union ccb *ccb)
1778 struct scsi_rw_6 cmd6;
1779 struct scsi_rw_10 *cmd10;
1780 struct da_softc *softc;
1784 cdb = ccb->csio.cdb_io.cdb_bytes;
1786 /* Translation only possible if CDB is an array and cmd is R/W6 */
1787 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1788 (*cdb != READ_6 && *cdb != WRITE_6))
1791 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
1792 "increasing minimum_cmd_size to 10.\n");
1793 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1794 softc->minimum_cmd_size = 10;
1796 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1797 cmd10 = (struct scsi_rw_10 *)cdb;
1798 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1800 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1801 cmd10->reserved = 0;
1802 scsi_ulto2b(cmd6.length, cmd10->length);
1803 cmd10->control = cmd6.control;
1804 ccb->csio.cdb_len = sizeof(*cmd10);
1806 /* Requeue request, unfreezing queue if necessary */
1807 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1808 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1811 cam_release_devq(ccb->ccb_h.path,
1815 /*getcount_only*/0);
1821 dadone(struct cam_periph *periph, union ccb *done_ccb)
1823 struct da_softc *softc;
1824 struct ccb_scsiio *csio;
1827 softc = (struct da_softc *)periph->softc;
1828 priority = done_ccb->ccb_h.pinfo.priority;
1829 csio = &done_ccb->csio;
1830 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1831 case DA_CCB_BUFFER_IO:
1835 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1836 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1840 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1845 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1846 if (error == ERESTART) {
1848 * A retry was scheuled, so
1857 * return all queued I/O with EIO, so that
1858 * the client can retry these I/Os in the
1859 * proper order should it attempt to recover.
1864 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
1866 * Catastrophic error. Mark our pack as
1870 * XXX See if this is really a media
1873 xpt_print(periph->path,
1874 "Invalidating pack\n");
1875 softc->flags |= DA_FLAG_PACK_INVALID;
1876 queued_error = ENXIO;
1878 bioq_flush(&softc->bio_queue, NULL,
1880 bp->bio_error = error;
1881 bp->bio_resid = bp->bio_bcount;
1882 bp->bio_flags |= BIO_ERROR;
1884 bp->bio_resid = csio->resid;
1886 if (bp->bio_resid != 0)
1887 bp->bio_flags |= BIO_ERROR;
1889 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1890 cam_release_devq(done_ccb->ccb_h.path,
1894 /*getcount_only*/0);
1896 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1897 panic("REQ_CMP with QFRZN");
1898 bp->bio_resid = csio->resid;
1899 if (csio->resid > 0)
1900 bp->bio_flags |= BIO_ERROR;
1901 if (softc->error_inject != 0) {
1902 bp->bio_error = softc->error_inject;
1903 bp->bio_resid = bp->bio_bcount;
1904 bp->bio_flags |= BIO_ERROR;
1905 softc->error_inject = 0;
1911 * Block out any asyncronous callbacks
1912 * while we touch the pending ccb list.
1914 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1915 softc->outstanding_cmds--;
1916 if (softc->outstanding_cmds == 0)
1917 softc->flags |= DA_FLAG_WENT_IDLE;
1919 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1920 xpt_print(periph->path, "oustanding %d\n",
1921 softc->outstanding_cmds);
1930 struct scsi_read_capacity_data *rdcap;
1931 struct scsi_read_capacity_data_long *rcaplong;
1932 char announce_buf[80];
1936 if (softc->state == DA_STATE_PROBE)
1937 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1939 rcaplong = (struct scsi_read_capacity_data_long *)
1942 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1943 struct disk_params *dp;
1944 uint32_t block_size;
1946 u_int lbppbe; /* LB per physical block exponent. */
1947 u_int lalba; /* Lowest aligned LBA. */
1949 if (softc->state == DA_STATE_PROBE) {
1950 block_size = scsi_4btoul(rdcap->length);
1951 maxsector = scsi_4btoul(rdcap->addr);
1956 * According to SBC-2, if the standard 10
1957 * byte READ CAPACITY command returns 2^32,
1958 * we should issue the 16 byte version of
1959 * the command, since the device in question
1960 * has more sectors than can be represented
1961 * with the short version of the command.
1963 if (maxsector == 0xffffffff) {
1964 softc->state = DA_STATE_PROBE2;
1965 free(rdcap, M_SCSIDA);
1966 xpt_release_ccb(done_ccb);
1967 xpt_schedule(periph, priority);
1971 block_size = scsi_4btoul(rcaplong->length);
1972 maxsector = scsi_8btou64(rcaplong->addr);
1973 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
1974 lalba = scsi_2btoul(rcaplong->lalba_lbp);
1978 * Because GEOM code just will panic us if we
1979 * give them an 'illegal' value we'll avoid that
1982 if (block_size == 0 && maxsector == 0) {
1983 snprintf(announce_buf, sizeof(announce_buf),
1985 } else if (block_size >= MAXPHYS || block_size == 0) {
1986 xpt_print(periph->path,
1987 "unsupportable block size %ju\n",
1988 (uintmax_t) block_size);
1989 announce_buf[0] = '\0';
1990 cam_periph_invalidate(periph);
1992 dasetgeom(periph, block_size, maxsector,
1993 lbppbe, lalba & SRC16_LALBA);
1994 if (lalba & SRC16_LBPME)
1995 softc->flags |= DA_FLAG_CAN_LBPME;
1997 softc->flags &= ~DA_FLAG_CAN_LBPME;
1998 dp = &softc->params;
1999 snprintf(announce_buf, sizeof(announce_buf),
2000 "%juMB (%ju %u byte sectors: %dH %dS/T "
2002 (((uintmax_t)dp->secsize *
2003 dp->sectors) / (1024*1024)),
2004 (uintmax_t)dp->sectors,
2005 dp->secsize, dp->heads,
2006 dp->secs_per_track, dp->cylinders);
2011 announce_buf[0] = '\0';
2014 * Retry any UNIT ATTENTION type errors. They
2015 * are expected at boot.
2017 error = daerror(done_ccb, CAM_RETRY_SELTO,
2018 SF_RETRY_UA|SF_NO_PRINT);
2019 if (error == ERESTART) {
2021 * A retry was scheuled, so
2025 } else if (error != 0) {
2026 struct scsi_sense_data *sense;
2028 int sense_key, error_code;
2031 struct ccb_getdev cgd;
2033 /* Don't wedge this device's queue */
2034 status = done_ccb->ccb_h.status;
2035 if ((status & CAM_DEV_QFRZN) != 0)
2036 cam_release_devq(done_ccb->ccb_h.path,
2040 /*getcount_only*/0);
2043 xpt_setup_ccb(&cgd.ccb_h,
2044 done_ccb->ccb_h.path,
2045 CAM_PRIORITY_NORMAL);
2046 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2047 xpt_action((union ccb *)&cgd);
2049 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
2050 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
2051 || ((status & CAM_AUTOSNS_VALID) == 0))
2057 sense = &csio->sense_data;
2058 scsi_extract_sense_len(sense,
2059 csio->sense_len - csio->sense_resid,
2060 &error_code, &sense_key, &asc,
2061 &ascq, /*show_errors*/ 1);
2064 * If we tried READ CAPACITY(16) and failed,
2065 * fallback to READ CAPACITY(10).
2067 if ((softc->state == DA_STATE_PROBE2) &&
2068 (softc->flags & DA_FLAG_CAN_RC16) &&
2069 (((csio->ccb_h.status & CAM_STATUS_MASK) ==
2072 (error_code == SSD_CURRENT_ERROR) &&
2073 (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
2074 softc->flags &= ~DA_FLAG_CAN_RC16;
2075 softc->state = DA_STATE_PROBE;
2076 free(rdcap, M_SCSIDA);
2077 xpt_release_ccb(done_ccb);
2078 xpt_schedule(periph, priority);
2082 * Attach to anything that claims to be a
2083 * direct access or optical disk device,
2084 * as long as it doesn't return a "Logical
2085 * unit not supported" (0x25) error.
2087 if ((have_sense) && (asc != 0x25)
2088 && (error_code == SSD_CURRENT_ERROR)) {
2089 const char *sense_key_desc;
2090 const char *asc_desc;
2092 scsi_sense_desc(sense_key, asc, ascq,
2096 snprintf(announce_buf,
2097 sizeof(announce_buf),
2098 "Attempt to query device "
2099 "size failed: %s, %s",
2107 xpt_print(periph->path,
2108 "got CAM status %#x\n",
2109 done_ccb->ccb_h.status);
2112 xpt_print(periph->path, "fatal error, "
2113 "failed to attach to device\n");
2116 * Free up resources.
2118 cam_periph_invalidate(periph);
2122 free(csio->data_ptr, M_SCSIDA);
2123 if (announce_buf[0] != '\0') {
2125 * Create our sysctl variables, now that we know
2126 * we have successfully attached.
2128 /* increase the refcount */
2129 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
2130 taskqueue_enqueue(taskqueue_thread,
2131 &softc->sysctl_task);
2132 xpt_announce_periph(periph, announce_buf);
2134 xpt_print(periph->path, "fatal error, "
2135 "could not acquire reference count\n");
2139 softc->state = DA_STATE_NORMAL;
2141 * Since our peripheral may be invalidated by an error
2142 * above or an external event, we must release our CCB
2143 * before releasing the probe lock on the peripheral.
2144 * The peripheral will only go away once the last lock
2145 * is removed, and we need it around for the CCB release
2148 xpt_release_ccb(done_ccb);
2149 cam_periph_unhold(periph);
2152 case DA_CCB_WAITING:
2154 /* Caller will release the CCB */
2155 wakeup(&done_ccb->ccb_h.cbfcnp);
2159 /* No-op. We're polling */
2164 xpt_release_ccb(done_ccb);
2168 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
2170 struct da_softc *softc;
2171 struct cam_periph *periph;
2174 periph = xpt_path_periph(ccb->ccb_h.path);
2175 softc = (struct da_softc *)periph->softc;
2178 * Automatically detect devices that do not support
2179 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
2182 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
2183 error = cmd6workaround(ccb);
2184 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
2185 CAM_SCSI_STATUS_ERROR)
2186 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
2187 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
2188 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
2189 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
2190 int sense_key, error_code, asc, ascq;
2192 scsi_extract_sense(&ccb->csio.sense_data,
2193 &error_code, &sense_key, &asc, &ascq);
2194 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
2195 error = cmd6workaround(ccb);
2197 if (error == ERESTART)
2202 * Until we have a better way of doing pack validation,
2203 * don't treat UAs as errors.
2205 sense_flags |= SF_RETRY_UA;
2206 return(cam_periph_error(ccb, cam_flags, sense_flags,
2207 &softc->saved_ccb));
2211 daprevent(struct cam_periph *periph, int action)
2213 struct da_softc *softc;
2217 softc = (struct da_softc *)periph->softc;
2219 if (((action == PR_ALLOW)
2220 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
2221 || ((action == PR_PREVENT)
2222 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
2226 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2228 scsi_prevent(&ccb->csio,
2236 error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
2237 SF_RETRY_UA, softc->disk->d_devstat);
2240 if (action == PR_ALLOW)
2241 softc->flags &= ~DA_FLAG_PACK_LOCKED;
2243 softc->flags |= DA_FLAG_PACK_LOCKED;
2246 xpt_release_ccb(ccb);
2250 dagetcapacity(struct cam_periph *periph)
2252 struct da_softc *softc;
2254 struct scsi_read_capacity_data *rcap;
2255 struct scsi_read_capacity_data_long *rcaplong;
2258 int error, rc16failed;
2259 u_int32_t sense_flags;
2260 u_int lbppbe; /* Logical blocks per physical block exponent. */
2261 u_int lalba; /* Lowest aligned LBA. */
2263 softc = (struct da_softc *)periph->softc;
2270 sense_flags = SF_RETRY_UA;
2271 if (softc->flags & DA_FLAG_PACK_REMOVABLE)
2272 sense_flags |= SF_NO_PRINT;
2274 /* Do a read capacity */
2275 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcaplong),
2280 rcaplong = (struct scsi_read_capacity_data_long *)rcap;
2282 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2284 /* Try READ CAPACITY(16) first if we think it should work. */
2285 if (softc->flags & DA_FLAG_CAN_RC16) {
2286 scsi_read_capacity_16(&ccb->csio,
2289 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2294 /*sense_len*/ SSD_FULL_SIZE,
2296 ccb->ccb_h.ccb_bp = NULL;
2298 error = cam_periph_runccb(ccb, daerror,
2299 /*cam_flags*/CAM_RETRY_SELTO,
2301 softc->disk->d_devstat);
2305 /* If we got ILLEGAL REQUEST, do not prefer RC16 any more. */
2306 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
2308 softc->flags &= ~DA_FLAG_CAN_RC16;
2309 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
2310 CAM_SCSI_STATUS_ERROR) &&
2311 (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND) &&
2312 (ccb->ccb_h.status & CAM_AUTOSNS_VALID) &&
2313 ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0) &&
2314 ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
2315 int sense_key, error_code, asc, ascq;
2317 scsi_extract_sense(&ccb->csio.sense_data,
2318 &error_code, &sense_key, &asc, &ascq);
2319 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
2320 softc->flags &= ~DA_FLAG_CAN_RC16;
2325 /* Do READ CAPACITY(10). */
2326 scsi_read_capacity(&ccb->csio,
2333 ccb->ccb_h.ccb_bp = NULL;
2335 error = cam_periph_runccb(ccb, daerror,
2336 /*cam_flags*/CAM_RETRY_SELTO,
2338 softc->disk->d_devstat);
2340 block_len = scsi_4btoul(rcap->length);
2341 maxsector = scsi_4btoul(rcap->addr);
2343 if (maxsector != 0xffffffff || rc16failed)
2348 /* If READ CAPACITY(10) returned overflow, use READ CAPACITY(16) */
2349 scsi_read_capacity_16(&ccb->csio,
2352 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2357 /*sense_len*/ SSD_FULL_SIZE,
2359 ccb->ccb_h.ccb_bp = NULL;
2361 error = cam_periph_runccb(ccb, daerror,
2362 /*cam_flags*/CAM_RETRY_SELTO,
2364 softc->disk->d_devstat);
2367 block_len = scsi_4btoul(rcaplong->length);
2368 maxsector = scsi_8btou64(rcaplong->addr);
2369 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
2370 lalba = scsi_2btoul(rcaplong->lalba_lbp);
2376 if (block_len >= MAXPHYS || block_len == 0) {
2377 xpt_print(periph->path,
2378 "unsupportable block size %ju\n",
2379 (uintmax_t) block_len);
2382 dasetgeom(periph, block_len, maxsector,
2383 lbppbe, lalba & SRC16_LALBA);
2384 if (lalba & SRC16_LBPME)
2385 softc->flags |= DA_FLAG_CAN_LBPME;
2387 softc->flags &= ~DA_FLAG_CAN_LBPME;
2391 xpt_release_ccb(ccb);
2393 free(rcap, M_SCSIDA);
2399 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
2400 u_int lbppbe, u_int lalba)
2402 struct ccb_calc_geometry ccg;
2403 struct da_softc *softc;
2404 struct disk_params *dp;
2406 softc = (struct da_softc *)periph->softc;
2408 dp = &softc->params;
2409 dp->secsize = block_len;
2410 dp->sectors = maxsector + 1;
2412 dp->stripesize = block_len << lbppbe;
2413 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
2415 } else if (softc->quirks & DA_Q_4K) {
2416 dp->stripesize = 4096;
2417 dp->stripeoffset = 0;
2420 dp->stripeoffset = 0;
2423 * Have the controller provide us with a geometry
2424 * for this disk. The only time the geometry
2425 * matters is when we boot and the controller
2426 * is the only one knowledgeable enough to come
2427 * up with something that will make this a bootable
2430 xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2431 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
2432 ccg.block_size = dp->secsize;
2433 ccg.volume_size = dp->sectors;
2435 ccg.secs_per_track = 0;
2437 xpt_action((union ccb*)&ccg);
2438 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2440 * We don't know what went wrong here- but just pick
2441 * a geometry so we don't have nasty things like divide
2445 dp->secs_per_track = 255;
2446 dp->cylinders = dp->sectors / (255 * 255);
2447 if (dp->cylinders == 0) {
2451 dp->heads = ccg.heads;
2452 dp->secs_per_track = ccg.secs_per_track;
2453 dp->cylinders = ccg.cylinders;
2458 dasendorderedtag(void *arg)
2460 struct da_softc *softc = arg;
2462 if (da_send_ordered) {
2463 if ((softc->ordered_tag_count == 0)
2464 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
2465 softc->flags |= DA_FLAG_NEED_OTAG;
2467 if (softc->outstanding_cmds > 0)
2468 softc->flags &= ~DA_FLAG_WENT_IDLE;
2470 softc->ordered_tag_count = 0;
2472 /* Queue us up again */
2473 callout_reset(&softc->sendordered_c,
2474 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2475 dasendorderedtag, softc);
2479 * Step through all DA peripheral drivers, and if the device is still open,
2480 * sync the disk cache to physical media.
2483 dashutdown(void * arg, int howto)
2485 struct cam_periph *periph;
2486 struct da_softc *softc;
2488 TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
2491 cam_periph_lock(periph);
2492 softc = (struct da_softc *)periph->softc;
2495 * We only sync the cache if the drive is still open, and
2496 * if the drive is capable of it..
2498 if (((softc->flags & DA_FLAG_OPEN) == 0)
2499 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
2500 cam_periph_unlock(periph);
2504 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2506 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
2507 scsi_synchronize_cache(&ccb.csio,
2511 /*begin_lba*/0, /* whole disk */
2516 xpt_polled_action(&ccb);
2518 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2519 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2520 CAM_SCSI_STATUS_ERROR)
2521 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
2522 int error_code, sense_key, asc, ascq;
2524 scsi_extract_sense(&ccb.csio.sense_data,
2525 &error_code, &sense_key,
2528 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
2529 scsi_sense_print(&ccb.csio);
2531 xpt_print(periph->path, "Synchronize "
2532 "cache failed, status == 0x%x, scsi status "
2533 "== 0x%x\n", ccb.ccb_h.status,
2534 ccb.csio.scsi_status);
2538 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
2539 cam_release_devq(ccb.ccb_h.path,
2543 /*getcount_only*/0);
2544 cam_periph_unlock(periph);
2548 #else /* !_KERNEL */
2551 * XXX This is only left out of the kernel build to silence warnings. If,
2552 * for some reason this function is used in the kernel, the ifdefs should
2553 * be moved so it is included both in the kernel and userland.
2556 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2557 void (*cbfcnp)(struct cam_periph *, union ccb *),
2558 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2559 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2562 struct scsi_format_unit *scsi_cmd;
2564 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2565 scsi_cmd->opcode = FORMAT_UNIT;
2566 scsi_cmd->byte2 = byte2;
2567 scsi_ulto2b(ileave, scsi_cmd->interleave);
2572 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2581 #endif /* _KERNEL */