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 * Olympus FE-210 camera
794 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "FE210*",
795 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
801 {T_DIRECT, SIP_MEDIA_REMOVABLE, "LG", "UP3S",
802 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
806 * Laser MP3-2GA13 MP3 player
808 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "(HS) Flash Disk",
809 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
813 static disk_strategy_t dastrategy;
814 static dumper_t dadump;
815 static periph_init_t dainit;
816 static void daasync(void *callback_arg, u_int32_t code,
817 struct cam_path *path, void *arg);
818 static void dasysctlinit(void *context, int pending);
819 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
820 static periph_ctor_t daregister;
821 static periph_dtor_t dacleanup;
822 static periph_start_t dastart;
823 static periph_oninv_t daoninvalidate;
824 static void dadone(struct cam_periph *periph,
825 union ccb *done_ccb);
826 static int daerror(union ccb *ccb, u_int32_t cam_flags,
827 u_int32_t sense_flags);
828 static void daprevent(struct cam_periph *periph, int action);
829 static int dagetcapacity(struct cam_periph *periph);
830 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
831 uint64_t maxsector, u_int lbppbe, u_int lalba);
832 static timeout_t dasendorderedtag;
833 static void dashutdown(void *arg, int howto);
835 #ifndef DA_DEFAULT_TIMEOUT
836 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
839 #ifndef DA_DEFAULT_RETRY
840 #define DA_DEFAULT_RETRY 4
843 #ifndef DA_DEFAULT_SEND_ORDERED
844 #define DA_DEFAULT_SEND_ORDERED 1
848 static int da_retry_count = DA_DEFAULT_RETRY;
849 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
850 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
852 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
853 "CAM Direct Access Disk driver");
854 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
855 &da_retry_count, 0, "Normal I/O retry count");
856 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
857 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
858 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
859 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
860 SYSCTL_INT(_kern_cam_da, OID_AUTO, da_send_ordered, CTLFLAG_RW,
861 &da_send_ordered, 0, "Send Ordered Tags");
862 TUNABLE_INT("kern.cam.da.da_send_ordered", &da_send_ordered);
865 * DA_ORDEREDTAG_INTERVAL determines how often, relative
866 * to the default timeout, we check to see whether an ordered
867 * tagged transaction is appropriate to prevent simple tag
868 * starvation. Since we'd like to ensure that there is at least
869 * 1/2 of the timeout length left for a starved transaction to
870 * complete after we've sent an ordered tag, we must poll at least
871 * four times in every timeout period. This takes care of the worst
872 * case where a starved transaction starts during an interval that
873 * meets the requirement "don't send an ordered tag" test so it takes
874 * us two intervals to determine that a tag must be sent.
876 #ifndef DA_ORDEREDTAG_INTERVAL
877 #define DA_ORDEREDTAG_INTERVAL 4
880 static struct periph_driver dadriver =
883 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
886 PERIPHDRIVER_DECLARE(da, dadriver);
888 MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
891 daopen(struct disk *dp)
893 struct cam_periph *periph;
894 struct da_softc *softc;
898 periph = (struct cam_periph *)dp->d_drv1;
899 if (periph == NULL) {
903 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
907 cam_periph_lock(periph);
908 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
909 cam_periph_unlock(periph);
910 cam_periph_release(periph);
914 unit = periph->unit_number;
915 softc = (struct da_softc *)periph->softc;
916 softc->flags |= DA_FLAG_OPEN;
918 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
919 ("daopen: disk=%s%d (unit %d)\n", dp->d_name, dp->d_unit,
922 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
923 /* Invalidate our pack information. */
924 softc->flags &= ~DA_FLAG_PACK_INVALID;
927 error = dagetcapacity(periph);
931 softc->disk->d_sectorsize = softc->params.secsize;
932 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
933 softc->disk->d_stripesize = softc->params.stripesize;
934 softc->disk->d_stripeoffset = softc->params.stripeoffset;
935 /* XXX: these are not actually "firmware" values, so they may be wrong */
936 softc->disk->d_fwsectors = softc->params.secs_per_track;
937 softc->disk->d_fwheads = softc->params.heads;
938 softc->disk->d_devstat->block_size = softc->params.secsize;
939 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
941 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
942 (softc->quirks & DA_Q_NO_PREVENT) == 0)
943 daprevent(periph, PR_PREVENT);
945 softc->flags &= ~DA_FLAG_OPEN;
947 cam_periph_unhold(periph);
948 cam_periph_unlock(periph);
951 cam_periph_release(periph);
957 daclose(struct disk *dp)
959 struct cam_periph *periph;
960 struct da_softc *softc;
963 periph = (struct cam_periph *)dp->d_drv1;
967 cam_periph_lock(periph);
968 if ((error = cam_periph_hold(periph, PRIBIO)) != 0) {
969 cam_periph_unlock(periph);
970 cam_periph_release(periph);
974 softc = (struct da_softc *)periph->softc;
976 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0
977 && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
980 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
982 scsi_synchronize_cache(&ccb->csio,
986 /*begin_lba*/0,/* Cover the whole disk */
991 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
992 /*sense_flags*/SF_RETRY_UA,
993 softc->disk->d_devstat);
995 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
996 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
997 CAM_SCSI_STATUS_ERROR) {
999 int sense_key, error_code;
1001 scsi_extract_sense_len(&ccb->csio.sense_data,
1002 ccb->csio.sense_len - ccb->csio.sense_resid,
1003 &error_code, &sense_key, &asc, &ascq,
1005 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1006 scsi_sense_print(&ccb->csio);
1008 xpt_print(periph->path, "Synchronize cache "
1009 "failed, status == 0x%x, scsi status == "
1010 "0x%x\n", ccb->csio.ccb_h.status,
1011 ccb->csio.scsi_status);
1015 xpt_release_ccb(ccb);
1019 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
1020 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
1021 daprevent(periph, PR_ALLOW);
1023 * If we've got removeable media, mark the blocksize as
1024 * unavailable, since it could change when new media is
1027 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1030 softc->flags &= ~DA_FLAG_OPEN;
1031 cam_periph_unhold(periph);
1032 cam_periph_unlock(periph);
1033 cam_periph_release(periph);
1038 * Actually translate the requested transfer into one the physical driver
1039 * can understand. The transfer is described by a buf and will include
1040 * only one physical transfer.
1043 dastrategy(struct bio *bp)
1045 struct cam_periph *periph;
1046 struct da_softc *softc;
1048 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1049 if (periph == NULL) {
1050 biofinish(bp, NULL, ENXIO);
1053 softc = (struct da_softc *)periph->softc;
1055 cam_periph_lock(periph);
1058 * If the device has been made invalid, error out
1060 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1061 cam_periph_unlock(periph);
1062 biofinish(bp, NULL, ENXIO);
1067 * Place it in the queue of disk activities for this disk
1069 bioq_disksort(&softc->bio_queue, bp);
1072 * Schedule ourselves for performing the work.
1074 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1075 cam_periph_unlock(periph);
1081 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1083 struct cam_periph *periph;
1084 struct da_softc *softc;
1086 struct ccb_scsiio csio;
1090 periph = dp->d_drv1;
1093 softc = (struct da_softc *)periph->softc;
1094 cam_periph_lock(periph);
1095 secsize = softc->params.secsize;
1097 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1098 cam_periph_unlock(periph);
1103 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1104 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1105 scsi_read_write(&csio,
1111 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1114 /*data_ptr*/(u_int8_t *) virtual,
1115 /*dxfer_len*/length,
1116 /*sense_len*/SSD_FULL_SIZE,
1117 da_default_timeout * 1000);
1118 xpt_polled_action((union ccb *)&csio);
1119 cam_periph_unlock(periph);
1121 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1122 printf("Aborting dump due to I/O error.\n");
1123 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
1124 CAM_SCSI_STATUS_ERROR)
1125 scsi_sense_print(&csio);
1127 printf("status == 0x%x, scsi status == 0x%x\n",
1128 csio.ccb_h.status, csio.scsi_status);
1135 * Sync the disk cache contents to the physical media.
1137 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1139 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1140 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1141 scsi_synchronize_cache(&csio,
1145 /*begin_lba*/0,/* Cover the whole disk */
1149 xpt_polled_action((union ccb *)&csio);
1151 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1152 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
1153 CAM_SCSI_STATUS_ERROR) {
1155 int sense_key, error_code;
1157 scsi_extract_sense_len(&csio.sense_data,
1158 csio.sense_len - csio.sense_resid,
1159 &error_code, &sense_key, &asc, &ascq,
1161 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1162 scsi_sense_print(&csio);
1164 xpt_print(periph->path, "Synchronize cache "
1165 "failed, status == 0x%x, scsi status == "
1166 "0x%x\n", csio.ccb_h.status,
1171 cam_periph_unlock(periph);
1176 dagetattr(struct bio *bp)
1179 struct cam_periph *periph;
1181 if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
1183 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1184 if (periph->path == NULL)
1187 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1190 bp->bio_completed = bp->bio_length;
1200 * Install a global async callback. This callback will
1201 * receive async callbacks like "new device found".
1203 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1205 if (status != CAM_REQ_CMP) {
1206 printf("da: Failed to attach master async callback "
1207 "due to status 0x%x!\n", status);
1208 } else if (da_send_ordered) {
1210 /* Register our shutdown event handler */
1211 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1212 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1213 printf("dainit: shutdown event registration failed!\n");
1218 daoninvalidate(struct cam_periph *periph)
1220 struct da_softc *softc;
1222 softc = (struct da_softc *)periph->softc;
1225 * De-register any async callbacks.
1227 xpt_register_async(0, daasync, periph, periph->path);
1229 softc->flags |= DA_FLAG_PACK_INVALID;
1232 * Return all queued I/O with ENXIO.
1233 * XXX Handle any transactions queued to the card
1234 * with XPT_ABORT_CCB.
1236 bioq_flush(&softc->bio_queue, NULL, ENXIO);
1238 disk_gone(softc->disk);
1239 xpt_print(periph->path, "lost device - %d outstanding, %d refs\n",
1240 softc->outstanding_cmds, periph->refcount);
1244 dacleanup(struct cam_periph *periph)
1246 struct da_softc *softc;
1248 softc = (struct da_softc *)periph->softc;
1250 xpt_print(periph->path, "removing device entry\n");
1251 cam_periph_unlock(periph);
1254 * If we can't free the sysctl tree, oh well...
1256 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1257 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1258 xpt_print(periph->path, "can't remove sysctl context\n");
1261 disk_destroy(softc->disk);
1262 callout_drain(&softc->sendordered_c);
1263 free(softc, M_DEVBUF);
1264 cam_periph_lock(periph);
1268 daasync(void *callback_arg, u_int32_t code,
1269 struct cam_path *path, void *arg)
1271 struct cam_periph *periph;
1273 periph = (struct cam_periph *)callback_arg;
1275 case AC_FOUND_DEVICE:
1277 struct ccb_getdev *cgd;
1280 cgd = (struct ccb_getdev *)arg;
1284 if (cgd->protocol != PROTO_SCSI)
1287 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1288 && SID_TYPE(&cgd->inq_data) != T_RBC
1289 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1293 * Allocate a peripheral instance for
1294 * this device and start the probe
1297 status = cam_periph_alloc(daregister, daoninvalidate,
1299 "da", CAM_PERIPH_BIO,
1300 cgd->ccb_h.path, daasync,
1301 AC_FOUND_DEVICE, cgd);
1303 if (status != CAM_REQ_CMP
1304 && status != CAM_REQ_INPROG)
1305 printf("daasync: Unable to attach to new device "
1306 "due to status 0x%x\n", status);
1309 case AC_ADVINFO_CHANGED:
1313 buftype = (uintptr_t)arg;
1314 if (buftype == CDAI_TYPE_PHYS_PATH) {
1315 struct da_softc *softc;
1317 softc = periph->softc;
1318 disk_attr_changed(softc->disk, "GEOM::physpath",
1326 struct da_softc *softc;
1327 struct ccb_hdr *ccbh;
1329 softc = (struct da_softc *)periph->softc;
1331 * Don't fail on the expected unit attention
1334 softc->flags |= DA_FLAG_RETRY_UA;
1335 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1336 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1342 cam_periph_async(periph, code, path, arg);
1346 dasysctlinit(void *context, int pending)
1348 struct cam_periph *periph;
1349 struct da_softc *softc;
1350 char tmpstr[80], tmpstr2[80];
1351 struct ccb_trans_settings cts;
1353 periph = (struct cam_periph *)context;
1355 * periph was held for us when this task was enqueued
1357 if (periph->flags & CAM_PERIPH_INVALID) {
1358 cam_periph_release(periph);
1362 softc = (struct da_softc *)periph->softc;
1363 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1364 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1366 sysctl_ctx_init(&softc->sysctl_ctx);
1367 softc->flags |= DA_FLAG_SCTX_INIT;
1368 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1369 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1370 CTLFLAG_RD, 0, tmpstr);
1371 if (softc->sysctl_tree == NULL) {
1372 printf("dasysctlinit: unable to allocate sysctl tree\n");
1373 cam_periph_release(periph);
1378 * Now register the sysctl handler, so the user can change the value on
1381 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1382 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1383 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1384 "Minimum CDB size");
1386 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1387 SYSCTL_CHILDREN(softc->sysctl_tree),
1391 &softc->error_inject,
1393 "error_inject leaf");
1397 * Add some addressing info.
1399 memset(&cts, 0, sizeof (cts));
1400 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
1401 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1402 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1403 cam_periph_lock(periph);
1404 xpt_action((union ccb *)&cts);
1405 cam_periph_unlock(periph);
1406 if (cts.ccb_h.status != CAM_REQ_CMP) {
1407 cam_periph_release(periph);
1410 if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
1411 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1412 if (fc->valid & CTS_FC_VALID_WWPN) {
1413 softc->wwpn = fc->wwpn;
1414 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1415 SYSCTL_CHILDREN(softc->sysctl_tree),
1416 OID_AUTO, "wwpn", CTLFLAG_RD,
1417 &softc->wwpn, "World Wide Port Name");
1420 cam_periph_release(periph);
1424 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1428 value = *(int *)arg1;
1430 error = sysctl_handle_int(oidp, &value, 0, req);
1433 || (req->newptr == NULL))
1437 * Acceptable values here are 6, 10, 12 or 16.
1441 else if ((value > 6)
1444 else if ((value > 10)
1447 else if (value > 12)
1450 *(int *)arg1 = value;
1456 daregister(struct cam_periph *periph, void *arg)
1458 struct da_softc *softc;
1459 struct ccb_pathinq cpi;
1460 struct ccb_getdev *cgd;
1464 cgd = (struct ccb_getdev *)arg;
1465 if (periph == NULL) {
1466 printf("daregister: periph was NULL!!\n");
1467 return(CAM_REQ_CMP_ERR);
1471 printf("daregister: no getdev CCB, can't register device\n");
1472 return(CAM_REQ_CMP_ERR);
1475 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
1478 if (softc == NULL) {
1479 printf("daregister: Unable to probe new device. "
1480 "Unable to allocate softc\n");
1481 return(CAM_REQ_CMP_ERR);
1484 LIST_INIT(&softc->pending_ccbs);
1485 softc->state = DA_STATE_PROBE;
1486 bioq_init(&softc->bio_queue);
1487 if (SID_IS_REMOVABLE(&cgd->inq_data))
1488 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1489 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1490 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1492 periph->softc = softc;
1495 * See if this device has any quirks.
1497 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1498 (caddr_t)da_quirk_table,
1499 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1500 sizeof(*da_quirk_table), scsi_inquiry_match);
1503 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1505 softc->quirks = DA_Q_NONE;
1507 /* Check if the SIM does not want 6 byte commands */
1508 bzero(&cpi, sizeof(cpi));
1509 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1510 cpi.ccb_h.func_code = XPT_PATH_INQ;
1511 xpt_action((union ccb *)&cpi);
1512 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1513 softc->quirks |= DA_Q_NO_6_BYTE;
1515 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1518 * Take an exclusive refcount on the periph while dastart is called
1519 * to finish the probe. The reference will be dropped in dadone at
1522 (void)cam_periph_hold(periph, PRIBIO);
1525 * Schedule a periodic event to occasionally send an
1526 * ordered tag to a device.
1528 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
1529 callout_reset(&softc->sendordered_c,
1530 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
1531 dasendorderedtag, softc);
1533 mtx_unlock(periph->sim->mtx);
1535 * RBC devices don't have to support READ(6), only READ(10).
1537 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1538 softc->minimum_cmd_size = 10;
1540 softc->minimum_cmd_size = 6;
1543 * Load the user's default, if any.
1545 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1546 periph->unit_number);
1547 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1550 * 6, 10, 12 and 16 are the currently permissible values.
1552 if (softc->minimum_cmd_size < 6)
1553 softc->minimum_cmd_size = 6;
1554 else if ((softc->minimum_cmd_size > 6)
1555 && (softc->minimum_cmd_size <= 10))
1556 softc->minimum_cmd_size = 10;
1557 else if ((softc->minimum_cmd_size > 10)
1558 && (softc->minimum_cmd_size <= 12))
1559 softc->minimum_cmd_size = 12;
1560 else if (softc->minimum_cmd_size > 12)
1561 softc->minimum_cmd_size = 16;
1563 /* Predict whether device may support READ CAPACITY(16). */
1564 if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3) {
1565 softc->flags |= DA_FLAG_CAN_RC16;
1566 softc->state = DA_STATE_PROBE2;
1570 * Register this media as a disk.
1572 softc->disk = disk_alloc();
1573 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
1574 periph->unit_number, 0,
1575 DEVSTAT_BS_UNAVAILABLE,
1576 SID_TYPE(&cgd->inq_data) |
1577 XPORT_DEVSTAT_TYPE(cpi.transport),
1578 DEVSTAT_PRIORITY_DISK);
1579 softc->disk->d_open = daopen;
1580 softc->disk->d_close = daclose;
1581 softc->disk->d_strategy = dastrategy;
1582 softc->disk->d_dump = dadump;
1583 softc->disk->d_getattr = dagetattr;
1584 softc->disk->d_name = "da";
1585 softc->disk->d_drv1 = periph;
1587 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */
1588 else if (cpi.maxio > MAXPHYS)
1589 softc->disk->d_maxsize = MAXPHYS; /* for safety */
1591 softc->disk->d_maxsize = cpi.maxio;
1592 softc->disk->d_unit = periph->unit_number;
1593 softc->disk->d_flags = 0;
1594 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
1595 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
1596 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
1597 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
1598 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
1599 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
1600 cgd->inq_data.product, sizeof(cgd->inq_data.product),
1601 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
1602 softc->disk->d_hba_vendor = cpi.hba_vendor;
1603 softc->disk->d_hba_device = cpi.hba_device;
1604 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
1605 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
1606 disk_create(softc->disk, DISK_VERSION);
1607 mtx_lock(periph->sim->mtx);
1610 * Add async callbacks for events of interest.
1611 * I don't bother checking if this fails as,
1612 * in most cases, the system will function just
1613 * fine without them and the only alternative
1614 * would be to not attach the device on failure.
1616 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET
1617 | AC_LOST_DEVICE | AC_ADVINFO_CHANGED,
1618 daasync, periph, periph->path);
1621 * Emit an attribute changed notification just in case
1622 * physical path information arrived before our async
1623 * event handler was registered, but after anyone attaching
1624 * to our disk device polled it.
1626 disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
1628 xpt_schedule(periph, CAM_PRIORITY_DEV);
1630 return(CAM_REQ_CMP);
1634 dastart(struct cam_periph *periph, union ccb *start_ccb)
1636 struct da_softc *softc;
1638 softc = (struct da_softc *)periph->softc;
1640 switch (softc->state) {
1641 case DA_STATE_NORMAL:
1643 /* Pull a buffer from the queue and get going on it */
1647 * See if there is a buf with work for us to do..
1649 bp = bioq_first(&softc->bio_queue);
1650 if (periph->immediate_priority <= periph->pinfo.priority) {
1651 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1652 ("queuing for immediate ccb\n"));
1653 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1654 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1656 periph->immediate_priority = CAM_PRIORITY_NONE;
1657 wakeup(&periph->ccb_list);
1658 } else if (bp == NULL) {
1659 xpt_release_ccb(start_ccb);
1663 bioq_remove(&softc->bio_queue, bp);
1665 if ((bp->bio_flags & BIO_ORDERED) != 0
1666 || (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1667 softc->flags &= ~DA_FLAG_NEED_OTAG;
1668 softc->ordered_tag_count++;
1669 tag_code = MSG_ORDERED_Q_TAG;
1671 tag_code = MSG_SIMPLE_Q_TAG;
1673 switch (bp->bio_cmd) {
1676 scsi_read_write(&start_ccb->csio,
1677 /*retries*/da_retry_count,
1679 /*tag_action*/tag_code,
1680 /*read_op*/bp->bio_cmd
1683 softc->minimum_cmd_size,
1684 /*lba*/bp->bio_pblkno,
1685 /*block_count*/bp->bio_bcount /
1686 softc->params.secsize,
1687 /*data_ptr*/ bp->bio_data,
1688 /*dxfer_len*/ bp->bio_bcount,
1689 /*sense_len*/SSD_FULL_SIZE,
1690 da_default_timeout * 1000);
1694 * BIO_FLUSH doesn't currently communicate
1695 * range data, so we synchronize the cache
1696 * over the whole disk. We also force
1697 * ordered tag semantics the flush applies
1698 * to all previously queued I/O.
1700 scsi_synchronize_cache(&start_ccb->csio,
1707 da_default_timeout*1000);
1710 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1713 * Block out any asyncronous callbacks
1714 * while we touch the pending ccb list.
1716 LIST_INSERT_HEAD(&softc->pending_ccbs,
1717 &start_ccb->ccb_h, periph_links.le);
1718 softc->outstanding_cmds++;
1720 /* We expect a unit attention from this device */
1721 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1722 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1723 softc->flags &= ~DA_FLAG_RETRY_UA;
1726 start_ccb->ccb_h.ccb_bp = bp;
1727 bp = bioq_first(&softc->bio_queue);
1729 xpt_action(start_ccb);
1733 /* Have more work to do, so ensure we stay scheduled */
1734 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1738 case DA_STATE_PROBE:
1740 struct ccb_scsiio *csio;
1741 struct scsi_read_capacity_data *rcap;
1743 rcap = (struct scsi_read_capacity_data *)
1744 malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
1746 printf("dastart: Couldn't malloc read_capacity data\n");
1747 /* da_free_periph??? */
1750 csio = &start_ccb->csio;
1751 scsi_read_capacity(csio,
1758 start_ccb->ccb_h.ccb_bp = NULL;
1759 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1760 xpt_action(start_ccb);
1763 case DA_STATE_PROBE2:
1765 struct ccb_scsiio *csio;
1766 struct scsi_read_capacity_data_long *rcaplong;
1768 rcaplong = (struct scsi_read_capacity_data_long *)
1769 malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
1770 if (rcaplong == NULL) {
1771 printf("dastart: Couldn't malloc read_capacity data\n");
1772 /* da_free_periph??? */
1775 csio = &start_ccb->csio;
1776 scsi_read_capacity_16(csio,
1779 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1784 /*sense_len*/ SSD_FULL_SIZE,
1786 start_ccb->ccb_h.ccb_bp = NULL;
1787 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1788 xpt_action(start_ccb);
1795 cmd6workaround(union ccb *ccb)
1797 struct scsi_rw_6 cmd6;
1798 struct scsi_rw_10 *cmd10;
1799 struct da_softc *softc;
1803 cdb = ccb->csio.cdb_io.cdb_bytes;
1805 /* Translation only possible if CDB is an array and cmd is R/W6 */
1806 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1807 (*cdb != READ_6 && *cdb != WRITE_6))
1810 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
1811 "increasing minimum_cmd_size to 10.\n");
1812 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1813 softc->minimum_cmd_size = 10;
1815 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1816 cmd10 = (struct scsi_rw_10 *)cdb;
1817 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1819 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1820 cmd10->reserved = 0;
1821 scsi_ulto2b(cmd6.length, cmd10->length);
1822 cmd10->control = cmd6.control;
1823 ccb->csio.cdb_len = sizeof(*cmd10);
1825 /* Requeue request, unfreezing queue if necessary */
1826 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1827 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1830 cam_release_devq(ccb->ccb_h.path,
1834 /*getcount_only*/0);
1840 dadone(struct cam_periph *periph, union ccb *done_ccb)
1842 struct da_softc *softc;
1843 struct ccb_scsiio *csio;
1846 softc = (struct da_softc *)periph->softc;
1847 priority = done_ccb->ccb_h.pinfo.priority;
1848 csio = &done_ccb->csio;
1849 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1850 case DA_CCB_BUFFER_IO:
1854 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1855 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1859 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1864 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1865 if (error == ERESTART) {
1867 * A retry was scheuled, so
1876 * return all queued I/O with EIO, so that
1877 * the client can retry these I/Os in the
1878 * proper order should it attempt to recover.
1883 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
1885 * Catastrophic error. Mark our pack as
1889 * XXX See if this is really a media
1892 xpt_print(periph->path,
1893 "Invalidating pack\n");
1894 softc->flags |= DA_FLAG_PACK_INVALID;
1895 queued_error = ENXIO;
1897 bioq_flush(&softc->bio_queue, NULL,
1899 bp->bio_error = error;
1900 bp->bio_resid = bp->bio_bcount;
1901 bp->bio_flags |= BIO_ERROR;
1903 bp->bio_resid = csio->resid;
1905 if (bp->bio_resid != 0)
1906 bp->bio_flags |= BIO_ERROR;
1908 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1909 cam_release_devq(done_ccb->ccb_h.path,
1913 /*getcount_only*/0);
1915 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1916 panic("REQ_CMP with QFRZN");
1917 bp->bio_resid = csio->resid;
1918 if (csio->resid > 0)
1919 bp->bio_flags |= BIO_ERROR;
1920 if (softc->error_inject != 0) {
1921 bp->bio_error = softc->error_inject;
1922 bp->bio_resid = bp->bio_bcount;
1923 bp->bio_flags |= BIO_ERROR;
1924 softc->error_inject = 0;
1930 * Block out any asyncronous callbacks
1931 * while we touch the pending ccb list.
1933 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1934 softc->outstanding_cmds--;
1935 if (softc->outstanding_cmds == 0)
1936 softc->flags |= DA_FLAG_WENT_IDLE;
1938 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1939 xpt_print(periph->path, "oustanding %d\n",
1940 softc->outstanding_cmds);
1949 struct scsi_read_capacity_data *rdcap;
1950 struct scsi_read_capacity_data_long *rcaplong;
1951 char announce_buf[80];
1955 if (softc->state == DA_STATE_PROBE)
1956 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1958 rcaplong = (struct scsi_read_capacity_data_long *)
1961 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1962 struct disk_params *dp;
1963 uint32_t block_size;
1965 u_int lbppbe; /* LB per physical block exponent. */
1966 u_int lalba; /* Lowest aligned LBA. */
1968 if (softc->state == DA_STATE_PROBE) {
1969 block_size = scsi_4btoul(rdcap->length);
1970 maxsector = scsi_4btoul(rdcap->addr);
1975 * According to SBC-2, if the standard 10
1976 * byte READ CAPACITY command returns 2^32,
1977 * we should issue the 16 byte version of
1978 * the command, since the device in question
1979 * has more sectors than can be represented
1980 * with the short version of the command.
1982 if (maxsector == 0xffffffff) {
1983 softc->state = DA_STATE_PROBE2;
1984 free(rdcap, M_SCSIDA);
1985 xpt_release_ccb(done_ccb);
1986 xpt_schedule(periph, priority);
1990 block_size = scsi_4btoul(rcaplong->length);
1991 maxsector = scsi_8btou64(rcaplong->addr);
1992 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
1993 lalba = scsi_2btoul(rcaplong->lalba_lbp);
1997 * Because GEOM code just will panic us if we
1998 * give them an 'illegal' value we'll avoid that
2001 if (block_size == 0 && maxsector == 0) {
2002 snprintf(announce_buf, sizeof(announce_buf),
2004 } else if (block_size >= MAXPHYS || block_size == 0) {
2005 xpt_print(periph->path,
2006 "unsupportable block size %ju\n",
2007 (uintmax_t) block_size);
2008 announce_buf[0] = '\0';
2009 cam_periph_invalidate(periph);
2011 dasetgeom(periph, block_size, maxsector,
2012 lbppbe, lalba & SRC16_LALBA);
2013 if (lalba & SRC16_LBPME)
2014 softc->flags |= DA_FLAG_CAN_LBPME;
2016 softc->flags &= ~DA_FLAG_CAN_LBPME;
2017 dp = &softc->params;
2018 snprintf(announce_buf, sizeof(announce_buf),
2019 "%juMB (%ju %u byte sectors: %dH %dS/T "
2021 (((uintmax_t)dp->secsize *
2022 dp->sectors) / (1024*1024)),
2023 (uintmax_t)dp->sectors,
2024 dp->secsize, dp->heads,
2025 dp->secs_per_track, dp->cylinders);
2030 announce_buf[0] = '\0';
2033 * Retry any UNIT ATTENTION type errors. They
2034 * are expected at boot.
2036 error = daerror(done_ccb, CAM_RETRY_SELTO,
2037 SF_RETRY_UA|SF_NO_PRINT);
2038 if (error == ERESTART) {
2040 * A retry was scheuled, so
2044 } else if (error != 0) {
2045 struct scsi_sense_data *sense;
2047 int sense_key, error_code;
2050 struct ccb_getdev cgd;
2052 /* Don't wedge this device's queue */
2053 status = done_ccb->ccb_h.status;
2054 if ((status & CAM_DEV_QFRZN) != 0)
2055 cam_release_devq(done_ccb->ccb_h.path,
2059 /*getcount_only*/0);
2062 xpt_setup_ccb(&cgd.ccb_h,
2063 done_ccb->ccb_h.path,
2064 CAM_PRIORITY_NORMAL);
2065 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2066 xpt_action((union ccb *)&cgd);
2068 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
2069 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
2070 || ((status & CAM_AUTOSNS_VALID) == 0))
2076 sense = &csio->sense_data;
2077 scsi_extract_sense_len(sense,
2078 csio->sense_len - csio->sense_resid,
2079 &error_code, &sense_key, &asc,
2080 &ascq, /*show_errors*/ 1);
2083 * If we tried READ CAPACITY(16) and failed,
2084 * fallback to READ CAPACITY(10).
2086 if ((softc->state == DA_STATE_PROBE2) &&
2087 (softc->flags & DA_FLAG_CAN_RC16) &&
2088 (((csio->ccb_h.status & CAM_STATUS_MASK) ==
2091 (error_code == SSD_CURRENT_ERROR) &&
2092 (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
2093 softc->flags &= ~DA_FLAG_CAN_RC16;
2094 softc->state = DA_STATE_PROBE;
2095 free(rdcap, M_SCSIDA);
2096 xpt_release_ccb(done_ccb);
2097 xpt_schedule(periph, priority);
2101 * Attach to anything that claims to be a
2102 * direct access or optical disk device,
2103 * as long as it doesn't return a "Logical
2104 * unit not supported" (0x25) error.
2106 if ((have_sense) && (asc != 0x25)
2107 && (error_code == SSD_CURRENT_ERROR)) {
2108 const char *sense_key_desc;
2109 const char *asc_desc;
2111 scsi_sense_desc(sense_key, asc, ascq,
2115 snprintf(announce_buf,
2116 sizeof(announce_buf),
2117 "Attempt to query device "
2118 "size failed: %s, %s",
2126 xpt_print(periph->path,
2127 "got CAM status %#x\n",
2128 done_ccb->ccb_h.status);
2131 xpt_print(periph->path, "fatal error, "
2132 "failed to attach to device\n");
2135 * Free up resources.
2137 cam_periph_invalidate(periph);
2141 free(csio->data_ptr, M_SCSIDA);
2142 if (announce_buf[0] != '\0') {
2144 * Create our sysctl variables, now that we know
2145 * we have successfully attached.
2147 /* increase the refcount */
2148 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
2149 taskqueue_enqueue(taskqueue_thread,
2150 &softc->sysctl_task);
2151 xpt_announce_periph(periph, announce_buf);
2153 xpt_print(periph->path, "fatal error, "
2154 "could not acquire reference count\n");
2158 softc->state = DA_STATE_NORMAL;
2160 * Since our peripheral may be invalidated by an error
2161 * above or an external event, we must release our CCB
2162 * before releasing the probe lock on the peripheral.
2163 * The peripheral will only go away once the last lock
2164 * is removed, and we need it around for the CCB release
2167 xpt_release_ccb(done_ccb);
2168 cam_periph_unhold(periph);
2171 case DA_CCB_WAITING:
2173 /* Caller will release the CCB */
2174 wakeup(&done_ccb->ccb_h.cbfcnp);
2178 /* No-op. We're polling */
2183 xpt_release_ccb(done_ccb);
2187 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
2189 struct da_softc *softc;
2190 struct cam_periph *periph;
2193 periph = xpt_path_periph(ccb->ccb_h.path);
2194 softc = (struct da_softc *)periph->softc;
2197 * Automatically detect devices that do not support
2198 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
2201 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
2202 error = cmd6workaround(ccb);
2203 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
2204 CAM_SCSI_STATUS_ERROR)
2205 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
2206 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
2207 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
2208 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
2209 int sense_key, error_code, asc, ascq;
2211 scsi_extract_sense(&ccb->csio.sense_data,
2212 &error_code, &sense_key, &asc, &ascq);
2213 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
2214 error = cmd6workaround(ccb);
2216 if (error == ERESTART)
2221 * Until we have a better way of doing pack validation,
2222 * don't treat UAs as errors.
2224 sense_flags |= SF_RETRY_UA;
2225 return(cam_periph_error(ccb, cam_flags, sense_flags,
2226 &softc->saved_ccb));
2230 daprevent(struct cam_periph *periph, int action)
2232 struct da_softc *softc;
2236 softc = (struct da_softc *)periph->softc;
2238 if (((action == PR_ALLOW)
2239 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
2240 || ((action == PR_PREVENT)
2241 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
2245 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2247 scsi_prevent(&ccb->csio,
2255 error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
2256 SF_RETRY_UA, softc->disk->d_devstat);
2259 if (action == PR_ALLOW)
2260 softc->flags &= ~DA_FLAG_PACK_LOCKED;
2262 softc->flags |= DA_FLAG_PACK_LOCKED;
2265 xpt_release_ccb(ccb);
2269 dagetcapacity(struct cam_periph *periph)
2271 struct da_softc *softc;
2273 struct scsi_read_capacity_data *rcap;
2274 struct scsi_read_capacity_data_long *rcaplong;
2277 int error, rc16failed;
2278 u_int32_t sense_flags;
2279 u_int lbppbe; /* Logical blocks per physical block exponent. */
2280 u_int lalba; /* Lowest aligned LBA. */
2282 softc = (struct da_softc *)periph->softc;
2289 sense_flags = SF_RETRY_UA;
2290 if (softc->flags & DA_FLAG_PACK_REMOVABLE)
2291 sense_flags |= SF_NO_PRINT;
2293 /* Do a read capacity */
2294 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcaplong),
2299 rcaplong = (struct scsi_read_capacity_data_long *)rcap;
2301 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2303 /* Try READ CAPACITY(16) first if we think it should work. */
2304 if (softc->flags & DA_FLAG_CAN_RC16) {
2305 scsi_read_capacity_16(&ccb->csio,
2308 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2313 /*sense_len*/ SSD_FULL_SIZE,
2315 ccb->ccb_h.ccb_bp = NULL;
2317 error = cam_periph_runccb(ccb, daerror,
2318 /*cam_flags*/CAM_RETRY_SELTO,
2320 softc->disk->d_devstat);
2324 /* If we got ILLEGAL REQUEST, do not prefer RC16 any more. */
2325 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
2327 softc->flags &= ~DA_FLAG_CAN_RC16;
2328 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
2329 CAM_SCSI_STATUS_ERROR) &&
2330 (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND) &&
2331 (ccb->ccb_h.status & CAM_AUTOSNS_VALID) &&
2332 ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0) &&
2333 ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
2334 int sense_key, error_code, asc, ascq;
2336 scsi_extract_sense(&ccb->csio.sense_data,
2337 &error_code, &sense_key, &asc, &ascq);
2338 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
2339 softc->flags &= ~DA_FLAG_CAN_RC16;
2344 /* Do READ CAPACITY(10). */
2345 scsi_read_capacity(&ccb->csio,
2352 ccb->ccb_h.ccb_bp = NULL;
2354 error = cam_periph_runccb(ccb, daerror,
2355 /*cam_flags*/CAM_RETRY_SELTO,
2357 softc->disk->d_devstat);
2359 block_len = scsi_4btoul(rcap->length);
2360 maxsector = scsi_4btoul(rcap->addr);
2362 if (maxsector != 0xffffffff || rc16failed)
2367 /* If READ CAPACITY(10) returned overflow, use READ CAPACITY(16) */
2368 scsi_read_capacity_16(&ccb->csio,
2371 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2376 /*sense_len*/ SSD_FULL_SIZE,
2378 ccb->ccb_h.ccb_bp = NULL;
2380 error = cam_periph_runccb(ccb, daerror,
2381 /*cam_flags*/CAM_RETRY_SELTO,
2383 softc->disk->d_devstat);
2386 block_len = scsi_4btoul(rcaplong->length);
2387 maxsector = scsi_8btou64(rcaplong->addr);
2388 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
2389 lalba = scsi_2btoul(rcaplong->lalba_lbp);
2395 if (block_len >= MAXPHYS || block_len == 0) {
2396 xpt_print(periph->path,
2397 "unsupportable block size %ju\n",
2398 (uintmax_t) block_len);
2401 dasetgeom(periph, block_len, maxsector,
2402 lbppbe, lalba & SRC16_LALBA);
2403 if (lalba & SRC16_LBPME)
2404 softc->flags |= DA_FLAG_CAN_LBPME;
2406 softc->flags &= ~DA_FLAG_CAN_LBPME;
2410 xpt_release_ccb(ccb);
2412 free(rcap, M_SCSIDA);
2418 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
2419 u_int lbppbe, u_int lalba)
2421 struct ccb_calc_geometry ccg;
2422 struct da_softc *softc;
2423 struct disk_params *dp;
2425 softc = (struct da_softc *)periph->softc;
2427 dp = &softc->params;
2428 dp->secsize = block_len;
2429 dp->sectors = maxsector + 1;
2431 dp->stripesize = block_len << lbppbe;
2432 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
2434 } else if (softc->quirks & DA_Q_4K) {
2435 dp->stripesize = 4096;
2436 dp->stripeoffset = 0;
2439 dp->stripeoffset = 0;
2442 * Have the controller provide us with a geometry
2443 * for this disk. The only time the geometry
2444 * matters is when we boot and the controller
2445 * is the only one knowledgeable enough to come
2446 * up with something that will make this a bootable
2449 xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2450 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
2451 ccg.block_size = dp->secsize;
2452 ccg.volume_size = dp->sectors;
2454 ccg.secs_per_track = 0;
2456 xpt_action((union ccb*)&ccg);
2457 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2459 * We don't know what went wrong here- but just pick
2460 * a geometry so we don't have nasty things like divide
2464 dp->secs_per_track = 255;
2465 dp->cylinders = dp->sectors / (255 * 255);
2466 if (dp->cylinders == 0) {
2470 dp->heads = ccg.heads;
2471 dp->secs_per_track = ccg.secs_per_track;
2472 dp->cylinders = ccg.cylinders;
2477 dasendorderedtag(void *arg)
2479 struct da_softc *softc = arg;
2481 if (da_send_ordered) {
2482 if ((softc->ordered_tag_count == 0)
2483 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
2484 softc->flags |= DA_FLAG_NEED_OTAG;
2486 if (softc->outstanding_cmds > 0)
2487 softc->flags &= ~DA_FLAG_WENT_IDLE;
2489 softc->ordered_tag_count = 0;
2491 /* Queue us up again */
2492 callout_reset(&softc->sendordered_c,
2493 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2494 dasendorderedtag, softc);
2498 * Step through all DA peripheral drivers, and if the device is still open,
2499 * sync the disk cache to physical media.
2502 dashutdown(void * arg, int howto)
2504 struct cam_periph *periph;
2505 struct da_softc *softc;
2507 TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
2510 cam_periph_lock(periph);
2511 softc = (struct da_softc *)periph->softc;
2514 * We only sync the cache if the drive is still open, and
2515 * if the drive is capable of it..
2517 if (((softc->flags & DA_FLAG_OPEN) == 0)
2518 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
2519 cam_periph_unlock(periph);
2523 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2525 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
2526 scsi_synchronize_cache(&ccb.csio,
2530 /*begin_lba*/0, /* whole disk */
2535 xpt_polled_action(&ccb);
2537 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2538 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2539 CAM_SCSI_STATUS_ERROR)
2540 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
2541 int error_code, sense_key, asc, ascq;
2543 scsi_extract_sense(&ccb.csio.sense_data,
2544 &error_code, &sense_key,
2547 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
2548 scsi_sense_print(&ccb.csio);
2550 xpt_print(periph->path, "Synchronize "
2551 "cache failed, status == 0x%x, scsi status "
2552 "== 0x%x\n", ccb.ccb_h.status,
2553 ccb.csio.scsi_status);
2557 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
2558 cam_release_devq(ccb.ccb_h.path,
2562 /*getcount_only*/0);
2563 cam_periph_unlock(periph);
2567 #else /* !_KERNEL */
2570 * XXX This is only left out of the kernel build to silence warnings. If,
2571 * for some reason this function is used in the kernel, the ifdefs should
2572 * be moved so it is included both in the kernel and userland.
2575 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2576 void (*cbfcnp)(struct cam_periph *, union ccb *),
2577 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2578 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2581 struct scsi_format_unit *scsi_cmd;
2583 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2584 scsi_cmd->opcode = FORMAT_UNIT;
2585 scsi_cmd->byte2 = byte2;
2586 scsi_ulto2b(ileave, scsi_cmd->interleave);
2591 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2600 #endif /* _KERNEL */