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_NEED_OTAG = 0x020,
81 DA_FLAG_WENT_IDLE = 0x040,
82 DA_FLAG_RETRY_UA = 0x080,
84 DA_FLAG_SCTX_INIT = 0x200,
85 DA_FLAG_CAN_RC16 = 0x400
90 DA_Q_NO_SYNC_CACHE = 0x01,
91 DA_Q_NO_6_BYTE = 0x02,
92 DA_Q_NO_PREVENT = 0x04,
99 DA_CCB_BUFFER_IO = 0x03,
100 DA_CCB_WAITING = 0x04,
102 DA_CCB_DELETE = 0x06,
103 DA_CCB_TYPE_MASK = 0x0F,
104 DA_CCB_RETRY_UA = 0x10
114 DA_DELETE_MAX = DA_DELETE_UNMAP
117 static const char *da_delete_method_names[] =
118 { "NONE", "DISABLE", "ZERO", "WS10", "WS16", "UNMAP" };
120 /* Offsets into our private area for storing information */
121 #define ccb_state ppriv_field0
122 #define ccb_bp ppriv_ptr1
127 u_int8_t secs_per_track;
128 u_int32_t secsize; /* Number of bytes/sector */
129 u_int64_t sectors; /* total number sectors */
134 #define UNMAP_MAX_RANGES 512
137 struct bio_queue_head bio_queue;
138 struct bio_queue_head delete_queue;
139 struct bio_queue_head delete_run_queue;
140 SLIST_ENTRY(da_softc) links;
141 LIST_HEAD(, ccb_hdr) pending_ccbs;
145 int minimum_cmd_size;
147 int ordered_tag_count;
148 int outstanding_cmds;
149 int unmap_max_ranges;
152 da_delete_methods delete_method;
153 struct disk_params params;
156 struct task sysctl_task;
157 struct sysctl_ctx_list sysctl_ctx;
158 struct sysctl_oid *sysctl_tree;
159 struct callout sendordered_c;
161 uint8_t unmap_buf[UNMAP_MAX_RANGES * 16 + 8];
164 struct da_quirk_entry {
165 struct scsi_inquiry_pattern inq_pat;
169 static const char quantum[] = "QUANTUM";
170 static const char microp[] = "MICROP";
172 static struct da_quirk_entry da_quirk_table[] =
174 /* SPI, FC devices */
177 * Fujitsu M2513A MO drives.
178 * Tested devices: M2513A2 firmware versions 1200 & 1300.
179 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
180 * Reported by: W.Scholten <whs@xs4all.nl>
182 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
183 /*quirks*/ DA_Q_NO_SYNC_CACHE
187 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
188 /*quirks*/ DA_Q_NO_SYNC_CACHE
192 * This particular Fujitsu drive doesn't like the
193 * synchronize cache command.
194 * Reported by: Tom Jackson <toj@gorilla.net>
196 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
197 /*quirks*/ DA_Q_NO_SYNC_CACHE
201 * This drive doesn't like the synchronize cache command
202 * either. Reported by: Matthew Jacob <mjacob@feral.com>
203 * in NetBSD PR kern/6027, August 24, 1998.
205 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
206 /*quirks*/ DA_Q_NO_SYNC_CACHE
210 * This drive doesn't like the synchronize cache command
211 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
214 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
215 /*quirks*/ DA_Q_NO_SYNC_CACHE
219 * Doesn't like the synchronize cache command.
220 * Reported by: Blaz Zupan <blaz@gold.amis.net>
222 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
223 /*quirks*/ DA_Q_NO_SYNC_CACHE
227 * Doesn't like the synchronize cache command.
228 * Reported by: Blaz Zupan <blaz@gold.amis.net>
230 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
231 /*quirks*/ DA_Q_NO_SYNC_CACHE
235 * Doesn't like the synchronize cache command.
237 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
238 /*quirks*/ DA_Q_NO_SYNC_CACHE
242 * Doesn't like the synchronize cache command.
243 * Reported by: walter@pelissero.de
245 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
246 /*quirks*/ DA_Q_NO_SYNC_CACHE
250 * Doesn't work correctly with 6 byte reads/writes.
251 * Returns illegal request, and points to byte 9 of the
253 * Reported by: Adam McDougall <bsdx@spawnet.com>
255 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
256 /*quirks*/ DA_Q_NO_6_BYTE
260 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
261 /*quirks*/ DA_Q_NO_6_BYTE
265 * Doesn't like the synchronize cache command.
266 * Reported by: walter@pelissero.de
268 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
269 /*quirks*/ DA_Q_NO_SYNC_CACHE
273 * The CISS RAID controllers do not support SYNC_CACHE
275 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
276 /*quirks*/ DA_Q_NO_SYNC_CACHE
278 /* USB mass storage devices supported by umass(4) */
281 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
284 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
285 /*quirks*/ DA_Q_NO_SYNC_CACHE
289 * Power Quotient Int. (PQI) USB flash key
292 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
293 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
297 * Creative Nomad MUVO mp3 player (USB)
300 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
301 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
305 * Jungsoft NEXDISK USB flash key
308 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
309 /*quirks*/ DA_Q_NO_SYNC_CACHE
313 * FreeDik USB Mini Data Drive
316 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
317 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
321 * Sigmatel USB Flash MP3 Player
324 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
325 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
329 * Neuros USB Digital Audio Computer
332 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
333 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
337 * SEAGRAND NP-900 MP3 Player
340 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
341 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
345 * iRiver iFP MP3 player (with UMS Firmware)
346 * PR: kern/54881, i386/63941, kern/66124
348 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
349 /*quirks*/ DA_Q_NO_SYNC_CACHE
353 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
356 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
357 /*quirks*/ DA_Q_NO_SYNC_CACHE
361 * ZICPlay USB MP3 Player with FM
364 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
365 /*quirks*/ DA_Q_NO_SYNC_CACHE
369 * TEAC USB floppy mechanisms
371 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
372 /*quirks*/ DA_Q_NO_SYNC_CACHE
376 * Kingston DataTraveler II+ USB Pen-Drive.
377 * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
379 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
380 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
384 * Motorola E398 Mobile Phone (TransFlash memory card).
385 * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
388 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
389 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
393 * Qware BeatZkey! Pro
396 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
397 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
401 * Time DPA20B 1GB MP3 Player
404 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
405 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
409 * Samsung USB key 128Mb
412 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
413 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
417 * Kingston DataTraveler 2.0 USB Flash memory.
420 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
421 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
425 * Creative MUVO Slim mp3 player (USB)
428 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
429 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
433 * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
436 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
437 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
441 * SanDisk Micro Cruzer 128MB
444 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
445 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
449 * TOSHIBA TransMemory USB sticks
452 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
453 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
458 * PR: usb/75578, usb/72344, usb/65436
460 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
461 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
465 * Genesys 6-in-1 Card Reader
468 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
469 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
473 * Rekam Digital CAMERA
476 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
477 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
481 * iRiver H10 MP3 player
484 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
485 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
489 * iRiver U10 MP3 player
492 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
493 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
500 {T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
501 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
505 * EasyMP3 EM732X USB 2.0 Flash MP3 Player
508 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
509 "1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
516 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
517 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
521 * Philips USB Key Audio KEY013
524 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
525 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
532 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
533 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
540 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
541 /*quirks*/ DA_Q_NO_SYNC_CACHE
545 * I/O Magic USB flash - Giga Bank
548 {T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
549 /*quirks*/ DA_Q_NO_SYNC_CACHE
553 * JoyFly 128mb USB Flash Drive
556 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
557 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
564 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
565 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
569 * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A
572 {T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*",
573 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
577 * Samsung YP-U3 mp3-player
580 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3",
581 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
584 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
585 "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
589 * Sony Cyber-Shot DSC cameras
592 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
593 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
595 /* ATA/SATA devices over SAS/USB/... */
597 /* Hitachi Advanced Format (4k) drives */
598 { T_DIRECT, SIP_MEDIA_FIXED, "Hitachi", "H??????????E3*", "*" },
602 /* Samsung Advanced Format (4k) drives */
603 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD155UI*", "*" },
607 /* Samsung Advanced Format (4k) drives */
608 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD155UI*", "*" },
612 /* Samsung Advanced Format (4k) drives */
613 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD204UI*", "*" },
617 /* Samsung Advanced Format (4k) drives */
618 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD204UI*", "*" },
622 /* Seagate Barracuda Green Advanced Format (4k) drives */
623 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DL*", "*" },
627 /* Seagate Barracuda Green Advanced Format (4k) drives */
628 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DL", "*", "*" },
632 /* Seagate Barracuda Green Advanced Format (4k) drives */
633 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???DM*", "*" },
637 /* Seagate Barracuda Green Advanced Format (4k) drives */
638 { T_DIRECT, SIP_MEDIA_FIXED, "ST???DM*", "*", "*" },
642 /* Seagate Barracuda Green Advanced Format (4k) drives */
643 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DM*", "*" },
647 /* Seagate Barracuda Green Advanced Format (4k) drives */
648 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DM", "*", "*" },
652 /* Seagate Momentus Advanced Format (4k) drives */
653 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500423AS*", "*" },
657 /* Seagate Momentus Advanced Format (4k) drives */
658 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "3AS*", "*" },
662 /* Seagate Momentus Advanced Format (4k) drives */
663 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500424AS*", "*" },
667 /* Seagate Momentus Advanced Format (4k) drives */
668 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "4AS*", "*" },
672 /* Seagate Momentus Advanced Format (4k) drives */
673 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640423AS*", "*" },
677 /* Seagate Momentus Advanced Format (4k) drives */
678 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "3AS*", "*" },
682 /* Seagate Momentus Advanced Format (4k) drives */
683 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640424AS*", "*" },
687 /* Seagate Momentus Advanced Format (4k) drives */
688 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "4AS*", "*" },
692 /* Seagate Momentus Advanced Format (4k) drives */
693 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750420AS*", "*" },
697 /* Seagate Momentus Advanced Format (4k) drives */
698 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "0AS*", "*" },
702 /* Seagate Momentus Advanced Format (4k) drives */
703 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750422AS*", "*" },
707 /* Seagate Momentus Advanced Format (4k) drives */
708 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "2AS*", "*" },
712 /* Seagate Momentus Advanced Format (4k) drives */
713 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750423AS*", "*" },
717 /* Seagate Momentus Advanced Format (4k) drives */
718 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "3AS*", "*" },
722 /* Seagate Momentus Thin Advanced Format (4k) drives */
723 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???LT*", "*" },
727 /* Seagate Momentus Thin Advanced Format (4k) drives */
728 { T_DIRECT, SIP_MEDIA_FIXED, "ST???LT*", "*", "*" },
732 /* WDC Caviar Green Advanced Format (4k) drives */
733 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RS*", "*" },
737 /* WDC Caviar Green Advanced Format (4k) drives */
738 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RS*", "*" },
742 /* WDC Caviar Green Advanced Format (4k) drives */
743 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RX*", "*" },
747 /* WDC Caviar Green Advanced Format (4k) drives */
748 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RX*", "*" },
752 /* WDC Caviar Green Advanced Format (4k) drives */
753 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RS*", "*" },
757 /* WDC Caviar Green Advanced Format (4k) drives */
758 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RS*", "*" },
762 /* WDC Caviar Green Advanced Format (4k) drives */
763 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RX*", "*" },
767 /* WDC Caviar Green Advanced Format (4k) drives */
768 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RX*", "*" },
772 /* WDC Scorpio Black Advanced Format (4k) drives */
773 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PKT*", "*" },
777 /* WDC Scorpio Black Advanced Format (4k) drives */
778 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PKT*", "*" },
782 /* WDC Scorpio Black Advanced Format (4k) drives */
783 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PKT*", "*" },
787 /* WDC Scorpio Black Advanced Format (4k) drives */
788 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PKT*", "*" },
792 /* WDC Scorpio Blue Advanced Format (4k) drives */
793 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PVT*", "*" },
797 /* WDC Scorpio Blue Advanced Format (4k) drives */
798 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PVT*", "*" },
802 /* WDC Scorpio Blue Advanced Format (4k) drives */
803 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PVT*", "*" },
807 /* WDC Scorpio Blue Advanced Format (4k) drives */
808 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PVT*", "*" },
813 * Olympus FE-210 camera
815 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "FE210*",
816 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
822 {T_DIRECT, SIP_MEDIA_REMOVABLE, "LG", "UP3S",
823 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
827 * Laser MP3-2GA13 MP3 player
829 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "(HS) Flash Disk",
830 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
834 static disk_strategy_t dastrategy;
835 static dumper_t dadump;
836 static periph_init_t dainit;
837 static void daasync(void *callback_arg, u_int32_t code,
838 struct cam_path *path, void *arg);
839 static void dasysctlinit(void *context, int pending);
840 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
841 static int dadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
842 static periph_ctor_t daregister;
843 static periph_dtor_t dacleanup;
844 static periph_start_t dastart;
845 static periph_oninv_t daoninvalidate;
846 static void dadone(struct cam_periph *periph,
847 union ccb *done_ccb);
848 static int daerror(union ccb *ccb, u_int32_t cam_flags,
849 u_int32_t sense_flags);
850 static void daprevent(struct cam_periph *periph, int action);
851 static int dagetcapacity(struct cam_periph *periph);
852 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
853 uint64_t maxsector, u_int lbppbe, u_int lalba);
854 static timeout_t dasendorderedtag;
855 static void dashutdown(void *arg, int howto);
857 #ifndef DA_DEFAULT_TIMEOUT
858 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
861 #ifndef DA_DEFAULT_RETRY
862 #define DA_DEFAULT_RETRY 4
865 #ifndef DA_DEFAULT_SEND_ORDERED
866 #define DA_DEFAULT_SEND_ORDERED 1
870 static int da_retry_count = DA_DEFAULT_RETRY;
871 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
872 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
874 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
875 "CAM Direct Access Disk driver");
876 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
877 &da_retry_count, 0, "Normal I/O retry count");
878 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
879 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
880 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
881 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
882 SYSCTL_INT(_kern_cam_da, OID_AUTO, da_send_ordered, CTLFLAG_RW,
883 &da_send_ordered, 0, "Send Ordered Tags");
884 TUNABLE_INT("kern.cam.da.da_send_ordered", &da_send_ordered);
887 * DA_ORDEREDTAG_INTERVAL determines how often, relative
888 * to the default timeout, we check to see whether an ordered
889 * tagged transaction is appropriate to prevent simple tag
890 * starvation. Since we'd like to ensure that there is at least
891 * 1/2 of the timeout length left for a starved transaction to
892 * complete after we've sent an ordered tag, we must poll at least
893 * four times in every timeout period. This takes care of the worst
894 * case where a starved transaction starts during an interval that
895 * meets the requirement "don't send an ordered tag" test so it takes
896 * us two intervals to determine that a tag must be sent.
898 #ifndef DA_ORDEREDTAG_INTERVAL
899 #define DA_ORDEREDTAG_INTERVAL 4
902 static struct periph_driver dadriver =
905 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
908 PERIPHDRIVER_DECLARE(da, dadriver);
910 MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
913 daopen(struct disk *dp)
915 struct cam_periph *periph;
916 struct da_softc *softc;
920 periph = (struct cam_periph *)dp->d_drv1;
921 if (periph == NULL) {
925 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
929 cam_periph_lock(periph);
930 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
931 cam_periph_unlock(periph);
932 cam_periph_release(periph);
936 unit = periph->unit_number;
937 softc = (struct da_softc *)periph->softc;
938 softc->flags |= DA_FLAG_OPEN;
940 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
943 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
944 /* Invalidate our pack information. */
945 softc->flags &= ~DA_FLAG_PACK_INVALID;
948 error = dagetcapacity(periph);
952 softc->disk->d_sectorsize = softc->params.secsize;
953 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
954 softc->disk->d_stripesize = softc->params.stripesize;
955 softc->disk->d_stripeoffset = softc->params.stripeoffset;
956 /* XXX: these are not actually "firmware" values, so they may be wrong */
957 softc->disk->d_fwsectors = softc->params.secs_per_track;
958 softc->disk->d_fwheads = softc->params.heads;
959 softc->disk->d_devstat->block_size = softc->params.secsize;
960 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
961 if (softc->delete_method > DA_DELETE_DISABLE)
962 softc->disk->d_flags |= DISKFLAG_CANDELETE;
964 softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
966 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
967 (softc->quirks & DA_Q_NO_PREVENT) == 0)
968 daprevent(periph, PR_PREVENT);
970 softc->flags &= ~DA_FLAG_OPEN;
972 cam_periph_unhold(periph);
973 cam_periph_unlock(periph);
976 cam_periph_release(periph);
982 daclose(struct disk *dp)
984 struct cam_periph *periph;
985 struct da_softc *softc;
987 periph = (struct cam_periph *)dp->d_drv1;
991 cam_periph_lock(periph);
992 if (cam_periph_hold(periph, PRIBIO) != 0) {
993 cam_periph_unlock(periph);
994 cam_periph_release(periph);
998 softc = (struct da_softc *)periph->softc;
1000 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1003 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0
1004 && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
1007 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1009 scsi_synchronize_cache(&ccb->csio,
1013 /*begin_lba*/0,/* Cover the whole disk */
1018 cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
1019 /*sense_flags*/SF_RETRY_UA | SF_QUIET_IR,
1020 softc->disk->d_devstat);
1021 xpt_release_ccb(ccb);
1025 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
1026 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
1027 daprevent(periph, PR_ALLOW);
1029 * If we've got removeable media, mark the blocksize as
1030 * unavailable, since it could change when new media is
1033 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1036 softc->flags &= ~DA_FLAG_OPEN;
1037 cam_periph_unhold(periph);
1038 cam_periph_unlock(periph);
1039 cam_periph_release(periph);
1044 daschedule(struct cam_periph *periph)
1046 struct da_softc *softc = (struct da_softc *)periph->softc;
1049 /* Check if cam_periph_getccb() was called. */
1050 prio = periph->immediate_priority;
1052 /* Check if we have more work to do. */
1053 if (bioq_first(&softc->bio_queue) ||
1054 (!softc->delete_running && bioq_first(&softc->delete_queue))) {
1055 prio = CAM_PRIORITY_NORMAL;
1058 /* Schedule CCB if any of above is true. */
1059 if (prio != CAM_PRIORITY_NONE)
1060 xpt_schedule(periph, prio);
1064 * Actually translate the requested transfer into one the physical driver
1065 * can understand. The transfer is described by a buf and will include
1066 * only one physical transfer.
1069 dastrategy(struct bio *bp)
1071 struct cam_periph *periph;
1072 struct da_softc *softc;
1074 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1075 if (periph == NULL) {
1076 biofinish(bp, NULL, ENXIO);
1079 softc = (struct da_softc *)periph->softc;
1081 cam_periph_lock(periph);
1084 * If the device has been made invalid, error out
1086 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1087 cam_periph_unlock(periph);
1088 biofinish(bp, NULL, ENXIO);
1092 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
1095 * Place it in the queue of disk activities for this disk
1097 if (bp->bio_cmd == BIO_DELETE) {
1098 if (bp->bio_bcount == 0)
1101 bioq_disksort(&softc->delete_queue, bp);
1103 bioq_disksort(&softc->bio_queue, bp);
1106 * Schedule ourselves for performing the work.
1109 cam_periph_unlock(periph);
1115 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1117 struct cam_periph *periph;
1118 struct da_softc *softc;
1120 struct ccb_scsiio csio;
1125 periph = dp->d_drv1;
1128 softc = (struct da_softc *)periph->softc;
1129 cam_periph_lock(periph);
1130 secsize = softc->params.secsize;
1132 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1133 cam_periph_unlock(periph);
1138 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1139 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1140 scsi_read_write(&csio,
1146 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1149 /*data_ptr*/(u_int8_t *) virtual,
1150 /*dxfer_len*/length,
1151 /*sense_len*/SSD_FULL_SIZE,
1152 da_default_timeout * 1000);
1153 xpt_polled_action((union ccb *)&csio);
1155 error = cam_periph_error((union ccb *)&csio,
1156 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1157 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1158 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1159 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1161 printf("Aborting dump due to I/O error.\n");
1162 cam_periph_unlock(periph);
1167 * Sync the disk cache contents to the physical media.
1169 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1171 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1172 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1173 scsi_synchronize_cache(&csio,
1177 /*begin_lba*/0,/* Cover the whole disk */
1181 xpt_polled_action((union ccb *)&csio);
1183 error = cam_periph_error((union ccb *)&csio,
1184 0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL);
1185 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1186 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1187 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1189 xpt_print(periph->path, "Synchronize cache failed\n");
1191 cam_periph_unlock(periph);
1196 dagetattr(struct bio *bp)
1199 struct cam_periph *periph;
1201 if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
1203 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1204 if (periph->path == NULL)
1207 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1210 bp->bio_completed = bp->bio_length;
1220 * Install a global async callback. This callback will
1221 * receive async callbacks like "new device found".
1223 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1225 if (status != CAM_REQ_CMP) {
1226 printf("da: Failed to attach master async callback "
1227 "due to status 0x%x!\n", status);
1228 } else if (da_send_ordered) {
1230 /* Register our shutdown event handler */
1231 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1232 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1233 printf("dainit: shutdown event registration failed!\n");
1238 daoninvalidate(struct cam_periph *periph)
1240 struct da_softc *softc;
1242 softc = (struct da_softc *)periph->softc;
1245 * De-register any async callbacks.
1247 xpt_register_async(0, daasync, periph, periph->path);
1249 softc->flags |= DA_FLAG_PACK_INVALID;
1252 * Return all queued I/O with ENXIO.
1253 * XXX Handle any transactions queued to the card
1254 * with XPT_ABORT_CCB.
1256 bioq_flush(&softc->bio_queue, NULL, ENXIO);
1257 bioq_flush(&softc->delete_queue, NULL, ENXIO);
1259 disk_gone(softc->disk);
1260 xpt_print(periph->path, "lost device - %d outstanding, %d refs\n",
1261 softc->outstanding_cmds, periph->refcount);
1265 dacleanup(struct cam_periph *periph)
1267 struct da_softc *softc;
1269 softc = (struct da_softc *)periph->softc;
1271 xpt_print(periph->path, "removing device entry\n");
1272 cam_periph_unlock(periph);
1275 * If we can't free the sysctl tree, oh well...
1277 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1278 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1279 xpt_print(periph->path, "can't remove sysctl context\n");
1282 disk_destroy(softc->disk);
1283 callout_drain(&softc->sendordered_c);
1284 free(softc, M_DEVBUF);
1285 cam_periph_lock(periph);
1289 daasync(void *callback_arg, u_int32_t code,
1290 struct cam_path *path, void *arg)
1292 struct cam_periph *periph;
1294 periph = (struct cam_periph *)callback_arg;
1296 case AC_FOUND_DEVICE:
1298 struct ccb_getdev *cgd;
1301 cgd = (struct ccb_getdev *)arg;
1305 if (cgd->protocol != PROTO_SCSI)
1308 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1309 && SID_TYPE(&cgd->inq_data) != T_RBC
1310 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1314 * Allocate a peripheral instance for
1315 * this device and start the probe
1318 status = cam_periph_alloc(daregister, daoninvalidate,
1320 "da", CAM_PERIPH_BIO,
1321 cgd->ccb_h.path, daasync,
1322 AC_FOUND_DEVICE, cgd);
1324 if (status != CAM_REQ_CMP
1325 && status != CAM_REQ_INPROG)
1326 printf("daasync: Unable to attach to new device "
1327 "due to status 0x%x\n", status);
1330 case AC_ADVINFO_CHANGED:
1334 buftype = (uintptr_t)arg;
1335 if (buftype == CDAI_TYPE_PHYS_PATH) {
1336 struct da_softc *softc;
1338 softc = periph->softc;
1339 disk_attr_changed(softc->disk, "GEOM::physpath",
1347 struct da_softc *softc;
1348 struct ccb_hdr *ccbh;
1350 softc = (struct da_softc *)periph->softc;
1352 * Don't fail on the expected unit attention
1355 softc->flags |= DA_FLAG_RETRY_UA;
1356 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1357 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1363 cam_periph_async(periph, code, path, arg);
1367 dasysctlinit(void *context, int pending)
1369 struct cam_periph *periph;
1370 struct da_softc *softc;
1371 char tmpstr[80], tmpstr2[80];
1372 struct ccb_trans_settings cts;
1374 periph = (struct cam_periph *)context;
1376 * periph was held for us when this task was enqueued
1378 if (periph->flags & CAM_PERIPH_INVALID) {
1379 cam_periph_release(periph);
1383 softc = (struct da_softc *)periph->softc;
1384 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1385 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1387 sysctl_ctx_init(&softc->sysctl_ctx);
1388 softc->flags |= DA_FLAG_SCTX_INIT;
1389 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1390 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1391 CTLFLAG_RD, 0, tmpstr);
1392 if (softc->sysctl_tree == NULL) {
1393 printf("dasysctlinit: unable to allocate sysctl tree\n");
1394 cam_periph_release(periph);
1399 * Now register the sysctl handler, so the user can change the value on
1402 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1403 OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RW,
1404 &softc->delete_method, 0, dadeletemethodsysctl, "A",
1405 "BIO_DELETE execution method");
1406 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1407 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1408 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1409 "Minimum CDB size");
1411 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1412 SYSCTL_CHILDREN(softc->sysctl_tree),
1416 &softc->error_inject,
1418 "error_inject leaf");
1422 * Add some addressing info.
1424 memset(&cts, 0, sizeof (cts));
1425 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
1426 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1427 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1428 cam_periph_lock(periph);
1429 xpt_action((union ccb *)&cts);
1430 cam_periph_unlock(periph);
1431 if (cts.ccb_h.status != CAM_REQ_CMP) {
1432 cam_periph_release(periph);
1435 if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
1436 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1437 if (fc->valid & CTS_FC_VALID_WWPN) {
1438 softc->wwpn = fc->wwpn;
1439 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1440 SYSCTL_CHILDREN(softc->sysctl_tree),
1441 OID_AUTO, "wwpn", CTLFLAG_RD,
1442 &softc->wwpn, "World Wide Port Name");
1445 cam_periph_release(periph);
1449 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1453 value = *(int *)arg1;
1455 error = sysctl_handle_int(oidp, &value, 0, req);
1458 || (req->newptr == NULL))
1462 * Acceptable values here are 6, 10, 12 or 16.
1466 else if ((value > 6)
1469 else if ((value > 10)
1472 else if (value > 12)
1475 *(int *)arg1 = value;
1481 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
1488 value = *(int *)arg1;
1489 if (value < 0 || value > DA_DELETE_MAX)
1492 p = da_delete_method_names[value];
1493 strncpy(buf, p, sizeof(buf));
1494 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1495 if (error != 0 || req->newptr == NULL)
1497 for (i = 0; i <= DA_DELETE_MAX; i++) {
1498 if (strcmp(buf, da_delete_method_names[i]) != 0)
1507 daregister(struct cam_periph *periph, void *arg)
1509 struct da_softc *softc;
1510 struct ccb_pathinq cpi;
1511 struct ccb_getdev *cgd;
1515 cgd = (struct ccb_getdev *)arg;
1516 if (periph == NULL) {
1517 printf("daregister: periph was NULL!!\n");
1518 return(CAM_REQ_CMP_ERR);
1522 printf("daregister: no getdev CCB, can't register device\n");
1523 return(CAM_REQ_CMP_ERR);
1526 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
1529 if (softc == NULL) {
1530 printf("daregister: Unable to probe new device. "
1531 "Unable to allocate softc\n");
1532 return(CAM_REQ_CMP_ERR);
1535 LIST_INIT(&softc->pending_ccbs);
1536 softc->state = DA_STATE_PROBE;
1537 bioq_init(&softc->bio_queue);
1538 bioq_init(&softc->delete_queue);
1539 bioq_init(&softc->delete_run_queue);
1540 if (SID_IS_REMOVABLE(&cgd->inq_data))
1541 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1542 softc->unmap_max_ranges = UNMAP_MAX_RANGES;
1543 softc->unmap_max_lba = 1024*1024*2;
1545 periph->softc = softc;
1548 * See if this device has any quirks.
1550 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1551 (caddr_t)da_quirk_table,
1552 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1553 sizeof(*da_quirk_table), scsi_inquiry_match);
1556 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1558 softc->quirks = DA_Q_NONE;
1560 /* Check if the SIM does not want 6 byte commands */
1561 bzero(&cpi, sizeof(cpi));
1562 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1563 cpi.ccb_h.func_code = XPT_PATH_INQ;
1564 xpt_action((union ccb *)&cpi);
1565 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1566 softc->quirks |= DA_Q_NO_6_BYTE;
1568 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1571 * Take an exclusive refcount on the periph while dastart is called
1572 * to finish the probe. The reference will be dropped in dadone at
1575 (void)cam_periph_hold(periph, PRIBIO);
1578 * Schedule a periodic event to occasionally send an
1579 * ordered tag to a device.
1581 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
1582 callout_reset(&softc->sendordered_c,
1583 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
1584 dasendorderedtag, softc);
1586 mtx_unlock(periph->sim->mtx);
1588 * RBC devices don't have to support READ(6), only READ(10).
1590 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1591 softc->minimum_cmd_size = 10;
1593 softc->minimum_cmd_size = 6;
1596 * Load the user's default, if any.
1598 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1599 periph->unit_number);
1600 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1603 * 6, 10, 12 and 16 are the currently permissible values.
1605 if (softc->minimum_cmd_size < 6)
1606 softc->minimum_cmd_size = 6;
1607 else if ((softc->minimum_cmd_size > 6)
1608 && (softc->minimum_cmd_size <= 10))
1609 softc->minimum_cmd_size = 10;
1610 else if ((softc->minimum_cmd_size > 10)
1611 && (softc->minimum_cmd_size <= 12))
1612 softc->minimum_cmd_size = 12;
1613 else if (softc->minimum_cmd_size > 12)
1614 softc->minimum_cmd_size = 16;
1616 /* Predict whether device may support READ CAPACITY(16). */
1617 if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3) {
1618 softc->flags |= DA_FLAG_CAN_RC16;
1619 softc->state = DA_STATE_PROBE2;
1623 * Register this media as a disk.
1625 softc->disk = disk_alloc();
1626 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
1627 periph->unit_number, 0,
1628 DEVSTAT_BS_UNAVAILABLE,
1629 SID_TYPE(&cgd->inq_data) |
1630 XPORT_DEVSTAT_TYPE(cpi.transport),
1631 DEVSTAT_PRIORITY_DISK);
1632 softc->disk->d_open = daopen;
1633 softc->disk->d_close = daclose;
1634 softc->disk->d_strategy = dastrategy;
1635 softc->disk->d_dump = dadump;
1636 softc->disk->d_getattr = dagetattr;
1637 softc->disk->d_name = "da";
1638 softc->disk->d_drv1 = periph;
1640 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */
1641 else if (cpi.maxio > MAXPHYS)
1642 softc->disk->d_maxsize = MAXPHYS; /* for safety */
1644 softc->disk->d_maxsize = cpi.maxio;
1645 softc->disk->d_unit = periph->unit_number;
1646 softc->disk->d_flags = 0;
1647 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
1648 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
1649 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
1650 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
1651 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
1652 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
1653 cgd->inq_data.product, sizeof(cgd->inq_data.product),
1654 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
1655 softc->disk->d_hba_vendor = cpi.hba_vendor;
1656 softc->disk->d_hba_device = cpi.hba_device;
1657 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
1658 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
1659 disk_create(softc->disk, DISK_VERSION);
1660 mtx_lock(periph->sim->mtx);
1663 * Add async callbacks for events of interest.
1664 * I don't bother checking if this fails as,
1665 * in most cases, the system will function just
1666 * fine without them and the only alternative
1667 * would be to not attach the device on failure.
1669 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET
1670 | AC_LOST_DEVICE | AC_ADVINFO_CHANGED,
1671 daasync, periph, periph->path);
1674 * Emit an attribute changed notification just in case
1675 * physical path information arrived before our async
1676 * event handler was registered, but after anyone attaching
1677 * to our disk device polled it.
1679 disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
1681 xpt_schedule(periph, CAM_PRIORITY_DEV);
1683 return(CAM_REQ_CMP);
1687 dastart(struct cam_periph *periph, union ccb *start_ccb)
1689 struct da_softc *softc;
1691 softc = (struct da_softc *)periph->softc;
1693 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
1695 switch (softc->state) {
1696 case DA_STATE_NORMAL:
1698 struct bio *bp, *bp1;
1701 /* Execute immediate CCB if waiting. */
1702 if (periph->immediate_priority <= periph->pinfo.priority) {
1703 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1704 ("queuing for immediate ccb\n"));
1705 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1706 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1708 periph->immediate_priority = CAM_PRIORITY_NONE;
1709 wakeup(&periph->ccb_list);
1710 /* May have more work to do, so ensure we stay scheduled */
1715 /* Run BIO_DELETE if not running yet. */
1716 if (!softc->delete_running &&
1717 (bp = bioq_first(&softc->delete_queue)) != NULL) {
1721 if (softc->delete_method == DA_DELETE_UNMAP) {
1722 uint8_t *buf = softc->unmap_buf;
1723 uint64_t lastlba = (uint64_t)-1;
1724 uint32_t lastcount = 0;
1725 int blocks = 0, off, ranges = 0;
1727 softc->delete_running = 1;
1728 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
1731 bioq_remove(&softc->delete_queue, bp1);
1733 bioq_insert_tail(&softc->delete_run_queue, bp1);
1734 lba = bp1->bio_pblkno;
1735 count = bp1->bio_bcount / softc->params.secsize;
1737 /* Try to extend the previous range. */
1738 if (lba == lastlba) {
1740 off = (ranges - 1) * 16 + 8;
1741 scsi_ulto4b(lastcount, &buf[off + 8]);
1742 } else if (count > 0) {
1743 off = ranges * 16 + 8;
1744 scsi_u64to8b(lba, &buf[off + 0]);
1745 scsi_ulto4b(count, &buf[off + 8]);
1750 lastlba = lba + count;
1751 bp1 = bioq_first(&softc->delete_queue);
1753 ranges >= softc->unmap_max_ranges ||
1754 blocks + bp1->bio_bcount /
1755 softc->params.secsize > softc->unmap_max_lba)
1758 scsi_ulto2b(count * 16 + 6, &buf[0]);
1759 scsi_ulto2b(count * 16, &buf[2]);
1761 scsi_unmap(&start_ccb->csio,
1762 /*retries*/da_retry_count,
1764 /*tag_action*/MSG_SIMPLE_Q_TAG,
1767 /*dxfer_len*/ count * 16 + 8,
1768 /*sense_len*/SSD_FULL_SIZE,
1769 da_default_timeout * 1000);
1770 start_ccb->ccb_h.ccb_state = DA_CCB_DELETE;
1772 } else if (softc->delete_method == DA_DELETE_ZERO ||
1773 softc->delete_method == DA_DELETE_WS10 ||
1774 softc->delete_method == DA_DELETE_WS16) {
1775 softc->delete_running = 1;
1776 lba = bp->bio_pblkno;
1780 bioq_remove(&softc->delete_queue, bp1);
1782 bioq_insert_tail(&softc->delete_run_queue, bp1);
1783 count += bp1->bio_bcount / softc->params.secsize;
1784 bp1 = bioq_first(&softc->delete_queue);
1786 lba + count != bp1->bio_pblkno ||
1787 count + bp1->bio_bcount /
1788 softc->params.secsize > 0xffff)
1792 scsi_write_same(&start_ccb->csio,
1793 /*retries*/da_retry_count,
1795 /*tag_action*/MSG_SIMPLE_Q_TAG,
1796 /*byte2*/softc->delete_method ==
1797 DA_DELETE_ZERO ? 0 : SWS_UNMAP,
1798 softc->delete_method ==
1799 DA_DELETE_WS16 ? 16 : 10,
1801 /*block_count*/count,
1802 /*data_ptr*/ __DECONST(void *,
1804 /*dxfer_len*/ softc->params.secsize,
1805 /*sense_len*/SSD_FULL_SIZE,
1806 da_default_timeout * 1000);
1807 start_ccb->ccb_h.ccb_state = DA_CCB_DELETE;
1810 bioq_flush(&softc->delete_queue, NULL, 0);
1815 /* Run regular command. */
1816 bp = bioq_takefirst(&softc->bio_queue);
1818 xpt_release_ccb(start_ccb);
1822 if ((bp->bio_flags & BIO_ORDERED) != 0 ||
1823 (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1824 softc->flags &= ~DA_FLAG_NEED_OTAG;
1825 softc->ordered_tag_count++;
1826 tag_code = MSG_ORDERED_Q_TAG;
1828 tag_code = MSG_SIMPLE_Q_TAG;
1831 switch (bp->bio_cmd) {
1834 scsi_read_write(&start_ccb->csio,
1835 /*retries*/da_retry_count,
1837 /*tag_action*/tag_code,
1838 /*read_op*/bp->bio_cmd
1841 softc->minimum_cmd_size,
1842 /*lba*/bp->bio_pblkno,
1843 /*block_count*/bp->bio_bcount /
1844 softc->params.secsize,
1845 /*data_ptr*/ bp->bio_data,
1846 /*dxfer_len*/ bp->bio_bcount,
1847 /*sense_len*/SSD_FULL_SIZE,
1848 da_default_timeout * 1000);
1852 * BIO_FLUSH doesn't currently communicate
1853 * range data, so we synchronize the cache
1854 * over the whole disk. We also force
1855 * ordered tag semantics the flush applies
1856 * to all previously queued I/O.
1858 scsi_synchronize_cache(&start_ccb->csio,
1865 da_default_timeout*1000);
1868 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1872 * Block out any asyncronous callbacks
1873 * while we touch the pending ccb list.
1875 LIST_INSERT_HEAD(&softc->pending_ccbs,
1876 &start_ccb->ccb_h, periph_links.le);
1877 softc->outstanding_cmds++;
1879 /* We expect a unit attention from this device */
1880 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1881 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1882 softc->flags &= ~DA_FLAG_RETRY_UA;
1885 start_ccb->ccb_h.ccb_bp = bp;
1886 xpt_action(start_ccb);
1888 /* May have more work to do, so ensure we stay scheduled */
1892 case DA_STATE_PROBE:
1894 struct ccb_scsiio *csio;
1895 struct scsi_read_capacity_data *rcap;
1897 rcap = (struct scsi_read_capacity_data *)
1898 malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
1900 printf("dastart: Couldn't malloc read_capacity data\n");
1901 /* da_free_periph??? */
1904 csio = &start_ccb->csio;
1905 scsi_read_capacity(csio,
1912 start_ccb->ccb_h.ccb_bp = NULL;
1913 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1914 xpt_action(start_ccb);
1917 case DA_STATE_PROBE2:
1919 struct ccb_scsiio *csio;
1920 struct scsi_read_capacity_data_long *rcaplong;
1922 rcaplong = (struct scsi_read_capacity_data_long *)
1923 malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
1924 if (rcaplong == NULL) {
1925 printf("dastart: Couldn't malloc read_capacity data\n");
1926 /* da_free_periph??? */
1929 csio = &start_ccb->csio;
1930 scsi_read_capacity_16(csio,
1933 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1938 /*sense_len*/ SSD_FULL_SIZE,
1940 start_ccb->ccb_h.ccb_bp = NULL;
1941 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1942 xpt_action(start_ccb);
1949 cmd6workaround(union ccb *ccb)
1951 struct scsi_rw_6 cmd6;
1952 struct scsi_rw_10 *cmd10;
1953 struct da_softc *softc;
1958 cdb = ccb->csio.cdb_io.cdb_bytes;
1959 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1961 if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
1962 if (softc->delete_method == DA_DELETE_UNMAP) {
1963 xpt_print(ccb->ccb_h.path, "UNMAP is not supported, "
1964 "switching to WRITE SAME(16) with UNMAP.\n");
1965 softc->delete_method = DA_DELETE_WS16;
1966 } else if (softc->delete_method == DA_DELETE_WS16) {
1967 xpt_print(ccb->ccb_h.path,
1968 "WRITE SAME(16) with UNMAP is not supported, "
1969 "disabling BIO_DELETE.\n");
1970 softc->delete_method = DA_DELETE_DISABLE;
1971 } else if (softc->delete_method == DA_DELETE_WS10) {
1972 xpt_print(ccb->ccb_h.path,
1973 "WRITE SAME(10) with UNMAP is not supported, "
1974 "disabling BIO_DELETE.\n");
1975 softc->delete_method = DA_DELETE_DISABLE;
1976 } else if (softc->delete_method == DA_DELETE_ZERO) {
1977 xpt_print(ccb->ccb_h.path,
1978 "WRITE SAME(10) is not supported, "
1979 "disabling BIO_DELETE.\n");
1980 softc->delete_method = DA_DELETE_DISABLE;
1982 softc->delete_method = DA_DELETE_DISABLE;
1983 while ((bp = bioq_takefirst(&softc->delete_run_queue))
1985 bioq_disksort(&softc->delete_queue, bp);
1986 bioq_insert_tail(&softc->delete_queue,
1987 (struct bio *)ccb->ccb_h.ccb_bp);
1988 ccb->ccb_h.ccb_bp = NULL;
1992 /* Translation only possible if CDB is an array and cmd is R/W6 */
1993 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1994 (*cdb != READ_6 && *cdb != WRITE_6))
1997 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
1998 "increasing minimum_cmd_size to 10.\n");
1999 softc->minimum_cmd_size = 10;
2001 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
2002 cmd10 = (struct scsi_rw_10 *)cdb;
2003 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
2005 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
2006 cmd10->reserved = 0;
2007 scsi_ulto2b(cmd6.length, cmd10->length);
2008 cmd10->control = cmd6.control;
2009 ccb->csio.cdb_len = sizeof(*cmd10);
2011 /* Requeue request, unfreezing queue if necessary */
2012 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
2013 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2016 cam_release_devq(ccb->ccb_h.path,
2020 /*getcount_only*/0);
2026 dadone(struct cam_periph *periph, union ccb *done_ccb)
2028 struct da_softc *softc;
2029 struct ccb_scsiio *csio;
2032 softc = (struct da_softc *)periph->softc;
2033 priority = done_ccb->ccb_h.pinfo.priority;
2035 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
2037 csio = &done_ccb->csio;
2038 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
2039 case DA_CCB_BUFFER_IO:
2042 struct bio *bp, *bp1;
2044 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2045 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2049 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
2054 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
2055 if (error == ERESTART) {
2057 * A retry was scheuled, so
2062 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2067 * return all queued I/O with EIO, so that
2068 * the client can retry these I/Os in the
2069 * proper order should it attempt to recover.
2074 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
2076 * Catastrophic error. Mark our pack as
2080 * XXX See if this is really a media
2083 xpt_print(periph->path,
2084 "Invalidating pack\n");
2085 softc->flags |= DA_FLAG_PACK_INVALID;
2086 queued_error = ENXIO;
2088 bioq_flush(&softc->bio_queue, NULL,
2091 bp->bio_error = error;
2092 bp->bio_resid = bp->bio_bcount;
2093 bp->bio_flags |= BIO_ERROR;
2095 } else if (bp != NULL) {
2096 bp->bio_resid = csio->resid;
2098 if (bp->bio_resid != 0)
2099 bp->bio_flags |= BIO_ERROR;
2101 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2102 cam_release_devq(done_ccb->ccb_h.path,
2106 /*getcount_only*/0);
2107 } else if (bp != NULL) {
2108 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2109 panic("REQ_CMP with QFRZN");
2110 bp->bio_resid = csio->resid;
2111 if (csio->resid > 0)
2112 bp->bio_flags |= BIO_ERROR;
2113 if (softc->error_inject != 0) {
2114 bp->bio_error = softc->error_inject;
2115 bp->bio_resid = bp->bio_bcount;
2116 bp->bio_flags |= BIO_ERROR;
2117 softc->error_inject = 0;
2123 * Block out any asyncronous callbacks
2124 * while we touch the pending ccb list.
2126 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
2127 softc->outstanding_cmds--;
2128 if (softc->outstanding_cmds == 0)
2129 softc->flags |= DA_FLAG_WENT_IDLE;
2131 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
2132 xpt_print(periph->path, "oustanding %d\n",
2133 softc->outstanding_cmds);
2136 if ((csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) ==
2138 while ((bp1 = bioq_takefirst(&softc->delete_run_queue))
2140 bp1->bio_resid = bp->bio_resid;
2141 bp1->bio_error = bp->bio_error;
2142 if (bp->bio_flags & BIO_ERROR)
2143 bp1->bio_flags |= BIO_ERROR;
2146 softc->delete_running = 0;
2150 } else if (bp != NULL)
2157 struct scsi_read_capacity_data *rdcap;
2158 struct scsi_read_capacity_data_long *rcaplong;
2159 char announce_buf[80];
2163 if (softc->state == DA_STATE_PROBE)
2164 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
2166 rcaplong = (struct scsi_read_capacity_data_long *)
2169 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
2170 struct disk_params *dp;
2171 uint32_t block_size;
2173 u_int lbppbe; /* LB per physical block exponent. */
2174 u_int lalba; /* Lowest aligned LBA. */
2176 if (softc->state == DA_STATE_PROBE) {
2177 block_size = scsi_4btoul(rdcap->length);
2178 maxsector = scsi_4btoul(rdcap->addr);
2183 * According to SBC-2, if the standard 10
2184 * byte READ CAPACITY command returns 2^32,
2185 * we should issue the 16 byte version of
2186 * the command, since the device in question
2187 * has more sectors than can be represented
2188 * with the short version of the command.
2190 if (maxsector == 0xffffffff) {
2191 softc->state = DA_STATE_PROBE2;
2192 free(rdcap, M_SCSIDA);
2193 xpt_release_ccb(done_ccb);
2194 xpt_schedule(periph, priority);
2198 block_size = scsi_4btoul(rcaplong->length);
2199 maxsector = scsi_8btou64(rcaplong->addr);
2200 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
2201 lalba = scsi_2btoul(rcaplong->lalba_lbp);
2205 * Because GEOM code just will panic us if we
2206 * give them an 'illegal' value we'll avoid that
2209 if (block_size == 0 && maxsector == 0) {
2210 snprintf(announce_buf, sizeof(announce_buf),
2212 } else if (block_size >= MAXPHYS || block_size == 0) {
2213 xpt_print(periph->path,
2214 "unsupportable block size %ju\n",
2215 (uintmax_t) block_size);
2216 announce_buf[0] = '\0';
2217 cam_periph_invalidate(periph);
2219 dasetgeom(periph, block_size, maxsector,
2220 lbppbe, lalba & SRC16_LALBA);
2221 if ((lalba & SRC16_LBPME) &&
2222 softc->delete_method == DA_DELETE_NONE)
2223 softc->delete_method = DA_DELETE_UNMAP;
2224 dp = &softc->params;
2225 snprintf(announce_buf, sizeof(announce_buf),
2226 "%juMB (%ju %u byte sectors: %dH %dS/T "
2228 (((uintmax_t)dp->secsize *
2229 dp->sectors) / (1024*1024)),
2230 (uintmax_t)dp->sectors,
2231 dp->secsize, dp->heads,
2232 dp->secs_per_track, dp->cylinders);
2237 announce_buf[0] = '\0';
2240 * Retry any UNIT ATTENTION type errors. They
2241 * are expected at boot.
2243 error = daerror(done_ccb, CAM_RETRY_SELTO,
2244 SF_RETRY_UA|SF_NO_PRINT);
2245 if (error == ERESTART) {
2247 * A retry was scheuled, so
2251 } else if (error != 0) {
2252 struct scsi_sense_data *sense;
2254 int sense_key, error_code;
2257 struct ccb_getdev cgd;
2259 /* Don't wedge this device's queue */
2260 status = done_ccb->ccb_h.status;
2261 if ((status & CAM_DEV_QFRZN) != 0)
2262 cam_release_devq(done_ccb->ccb_h.path,
2266 /*getcount_only*/0);
2269 xpt_setup_ccb(&cgd.ccb_h,
2270 done_ccb->ccb_h.path,
2271 CAM_PRIORITY_NORMAL);
2272 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2273 xpt_action((union ccb *)&cgd);
2275 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
2276 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
2277 || ((status & CAM_AUTOSNS_VALID) == 0))
2283 sense = &csio->sense_data;
2284 scsi_extract_sense_len(sense,
2285 csio->sense_len - csio->sense_resid,
2286 &error_code, &sense_key, &asc,
2287 &ascq, /*show_errors*/ 1);
2290 * If we tried READ CAPACITY(16) and failed,
2291 * fallback to READ CAPACITY(10).
2293 if ((softc->state == DA_STATE_PROBE2) &&
2294 (softc->flags & DA_FLAG_CAN_RC16) &&
2295 (((csio->ccb_h.status & CAM_STATUS_MASK) ==
2298 (error_code == SSD_CURRENT_ERROR) &&
2299 (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
2300 softc->flags &= ~DA_FLAG_CAN_RC16;
2301 softc->state = DA_STATE_PROBE;
2302 free(rdcap, M_SCSIDA);
2303 xpt_release_ccb(done_ccb);
2304 xpt_schedule(periph, priority);
2308 * Attach to anything that claims to be a
2309 * direct access or optical disk device,
2310 * as long as it doesn't return a "Logical
2311 * unit not supported" (0x25) error.
2313 if ((have_sense) && (asc != 0x25)
2314 && (error_code == SSD_CURRENT_ERROR)) {
2315 const char *sense_key_desc;
2316 const char *asc_desc;
2318 scsi_sense_desc(sense_key, asc, ascq,
2322 snprintf(announce_buf,
2323 sizeof(announce_buf),
2324 "Attempt to query device "
2325 "size failed: %s, %s",
2333 xpt_print(periph->path,
2334 "got CAM status %#x\n",
2335 done_ccb->ccb_h.status);
2338 xpt_print(periph->path, "fatal error, "
2339 "failed to attach to device\n");
2342 * Free up resources.
2344 cam_periph_invalidate(periph);
2348 free(csio->data_ptr, M_SCSIDA);
2349 if (announce_buf[0] != '\0') {
2351 * Create our sysctl variables, now that we know
2352 * we have successfully attached.
2354 /* increase the refcount */
2355 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
2356 taskqueue_enqueue(taskqueue_thread,
2357 &softc->sysctl_task);
2358 xpt_announce_periph(periph, announce_buf);
2360 xpt_print(periph->path, "fatal error, "
2361 "could not acquire reference count\n");
2365 softc->state = DA_STATE_NORMAL;
2367 * Since our peripheral may be invalidated by an error
2368 * above or an external event, we must release our CCB
2369 * before releasing the probe lock on the peripheral.
2370 * The peripheral will only go away once the last lock
2371 * is removed, and we need it around for the CCB release
2374 xpt_release_ccb(done_ccb);
2375 cam_periph_unhold(periph);
2378 case DA_CCB_WAITING:
2380 /* Caller will release the CCB */
2381 wakeup(&done_ccb->ccb_h.cbfcnp);
2385 /* No-op. We're polling */
2390 xpt_release_ccb(done_ccb);
2394 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
2396 struct da_softc *softc;
2397 struct cam_periph *periph;
2400 periph = xpt_path_periph(ccb->ccb_h.path);
2401 softc = (struct da_softc *)periph->softc;
2404 * Automatically detect devices that do not support
2405 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
2408 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
2409 error = cmd6workaround(ccb);
2410 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
2411 CAM_SCSI_STATUS_ERROR)
2412 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
2413 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
2414 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
2415 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
2416 int sense_key, error_code, asc, ascq;
2418 scsi_extract_sense(&ccb->csio.sense_data,
2419 &error_code, &sense_key, &asc, &ascq);
2420 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
2421 error = cmd6workaround(ccb);
2423 if (error == ERESTART)
2428 * Until we have a better way of doing pack validation,
2429 * don't treat UAs as errors.
2431 sense_flags |= SF_RETRY_UA;
2432 return(cam_periph_error(ccb, cam_flags, sense_flags,
2433 &softc->saved_ccb));
2437 daprevent(struct cam_periph *periph, int action)
2439 struct da_softc *softc;
2443 softc = (struct da_softc *)periph->softc;
2445 if (((action == PR_ALLOW)
2446 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
2447 || ((action == PR_PREVENT)
2448 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
2452 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2454 scsi_prevent(&ccb->csio,
2462 error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
2463 SF_RETRY_UA | SF_QUIET_IR, softc->disk->d_devstat);
2466 if (action == PR_ALLOW)
2467 softc->flags &= ~DA_FLAG_PACK_LOCKED;
2469 softc->flags |= DA_FLAG_PACK_LOCKED;
2472 xpt_release_ccb(ccb);
2476 dagetcapacity(struct cam_periph *periph)
2478 struct da_softc *softc;
2480 struct scsi_read_capacity_data *rcap;
2481 struct scsi_read_capacity_data_long *rcaplong;
2484 int error, rc16failed;
2485 u_int32_t sense_flags;
2486 u_int lbppbe; /* Logical blocks per physical block exponent. */
2487 u_int lalba; /* Lowest aligned LBA. */
2489 softc = (struct da_softc *)periph->softc;
2496 sense_flags = SF_RETRY_UA;
2497 if (softc->flags & DA_FLAG_PACK_REMOVABLE)
2498 sense_flags |= SF_NO_PRINT;
2500 /* Do a read capacity */
2501 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcaplong),
2506 rcaplong = (struct scsi_read_capacity_data_long *)rcap;
2508 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2510 /* Try READ CAPACITY(16) first if we think it should work. */
2511 if (softc->flags & DA_FLAG_CAN_RC16) {
2512 scsi_read_capacity_16(&ccb->csio,
2515 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2520 /*sense_len*/ SSD_FULL_SIZE,
2522 ccb->ccb_h.ccb_bp = NULL;
2524 error = cam_periph_runccb(ccb, daerror,
2525 /*cam_flags*/CAM_RETRY_SELTO,
2527 softc->disk->d_devstat);
2531 /* If we got ILLEGAL REQUEST, do not prefer RC16 any more. */
2532 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
2534 softc->flags &= ~DA_FLAG_CAN_RC16;
2535 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
2536 CAM_SCSI_STATUS_ERROR) &&
2537 (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND) &&
2538 (ccb->ccb_h.status & CAM_AUTOSNS_VALID) &&
2539 ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0) &&
2540 ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
2541 int sense_key, error_code, asc, ascq;
2543 scsi_extract_sense(&ccb->csio.sense_data,
2544 &error_code, &sense_key, &asc, &ascq);
2545 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
2546 softc->flags &= ~DA_FLAG_CAN_RC16;
2551 /* Do READ CAPACITY(10). */
2552 scsi_read_capacity(&ccb->csio,
2559 ccb->ccb_h.ccb_bp = NULL;
2561 error = cam_periph_runccb(ccb, daerror,
2562 /*cam_flags*/CAM_RETRY_SELTO,
2564 softc->disk->d_devstat);
2566 block_len = scsi_4btoul(rcap->length);
2567 maxsector = scsi_4btoul(rcap->addr);
2569 if (maxsector != 0xffffffff || rc16failed)
2574 /* If READ CAPACITY(10) returned overflow, use READ CAPACITY(16) */
2575 scsi_read_capacity_16(&ccb->csio,
2578 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2583 /*sense_len*/ SSD_FULL_SIZE,
2585 ccb->ccb_h.ccb_bp = NULL;
2587 error = cam_periph_runccb(ccb, daerror,
2588 /*cam_flags*/CAM_RETRY_SELTO,
2590 softc->disk->d_devstat);
2593 block_len = scsi_4btoul(rcaplong->length);
2594 maxsector = scsi_8btou64(rcaplong->addr);
2595 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
2596 lalba = scsi_2btoul(rcaplong->lalba_lbp);
2602 if (block_len >= MAXPHYS || block_len == 0) {
2603 xpt_print(periph->path,
2604 "unsupportable block size %ju\n",
2605 (uintmax_t) block_len);
2608 dasetgeom(periph, block_len, maxsector,
2609 lbppbe, lalba & SRC16_LALBA);
2610 if ((lalba & SRC16_LBPME) &&
2611 softc->delete_method == DA_DELETE_NONE)
2612 softc->delete_method = DA_DELETE_UNMAP;
2616 xpt_release_ccb(ccb);
2618 free(rcap, M_SCSIDA);
2624 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
2625 u_int lbppbe, u_int lalba)
2627 struct ccb_calc_geometry ccg;
2628 struct da_softc *softc;
2629 struct disk_params *dp;
2631 softc = (struct da_softc *)periph->softc;
2633 dp = &softc->params;
2634 dp->secsize = block_len;
2635 dp->sectors = maxsector + 1;
2637 dp->stripesize = block_len << lbppbe;
2638 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
2640 } else if (softc->quirks & DA_Q_4K) {
2641 dp->stripesize = 4096;
2642 dp->stripeoffset = 0;
2645 dp->stripeoffset = 0;
2648 * Have the controller provide us with a geometry
2649 * for this disk. The only time the geometry
2650 * matters is when we boot and the controller
2651 * is the only one knowledgeable enough to come
2652 * up with something that will make this a bootable
2655 xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2656 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
2657 ccg.block_size = dp->secsize;
2658 ccg.volume_size = dp->sectors;
2660 ccg.secs_per_track = 0;
2662 xpt_action((union ccb*)&ccg);
2663 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2665 * We don't know what went wrong here- but just pick
2666 * a geometry so we don't have nasty things like divide
2670 dp->secs_per_track = 255;
2671 dp->cylinders = dp->sectors / (255 * 255);
2672 if (dp->cylinders == 0) {
2676 dp->heads = ccg.heads;
2677 dp->secs_per_track = ccg.secs_per_track;
2678 dp->cylinders = ccg.cylinders;
2683 dasendorderedtag(void *arg)
2685 struct da_softc *softc = arg;
2687 if (da_send_ordered) {
2688 if ((softc->ordered_tag_count == 0)
2689 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
2690 softc->flags |= DA_FLAG_NEED_OTAG;
2692 if (softc->outstanding_cmds > 0)
2693 softc->flags &= ~DA_FLAG_WENT_IDLE;
2695 softc->ordered_tag_count = 0;
2697 /* Queue us up again */
2698 callout_reset(&softc->sendordered_c,
2699 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2700 dasendorderedtag, softc);
2704 * Step through all DA peripheral drivers, and if the device is still open,
2705 * sync the disk cache to physical media.
2708 dashutdown(void * arg, int howto)
2710 struct cam_periph *periph;
2711 struct da_softc *softc;
2714 TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
2717 cam_periph_lock(periph);
2718 softc = (struct da_softc *)periph->softc;
2721 * We only sync the cache if the drive is still open, and
2722 * if the drive is capable of it..
2724 if (((softc->flags & DA_FLAG_OPEN) == 0)
2725 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
2726 cam_periph_unlock(periph);
2730 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2732 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
2733 scsi_synchronize_cache(&ccb.csio,
2737 /*begin_lba*/0, /* whole disk */
2742 xpt_polled_action(&ccb);
2744 error = cam_periph_error(&ccb,
2745 0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL);
2746 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
2747 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
2748 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
2750 xpt_print(periph->path, "Synchronize cache failed\n");
2751 cam_periph_unlock(periph);
2755 #else /* !_KERNEL */
2758 * XXX This is only left out of the kernel build to silence warnings. If,
2759 * for some reason this function is used in the kernel, the ifdefs should
2760 * be moved so it is included both in the kernel and userland.
2763 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2764 void (*cbfcnp)(struct cam_periph *, union ccb *),
2765 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2766 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2769 struct scsi_format_unit *scsi_cmd;
2771 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2772 scsi_cmd->opcode = FORMAT_UNIT;
2773 scsi_cmd->byte2 = byte2;
2774 scsi_ulto2b(ileave, scsi_cmd->interleave);
2779 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2788 #endif /* _KERNEL */