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
91 DA_Q_NO_SYNC_CACHE = 0x01,
92 DA_Q_NO_6_BYTE = 0x02,
93 DA_Q_NO_PREVENT = 0x04,
100 DA_CCB_BUFFER_IO = 0x03,
101 DA_CCB_WAITING = 0x04,
103 DA_CCB_DELETE = 0x06,
104 DA_CCB_TYPE_MASK = 0x0F,
105 DA_CCB_RETRY_UA = 0x10
115 DA_DELETE_MAX = DA_DELETE_UNMAP
118 static const char *da_delete_method_names[] =
119 { "NONE", "DISABLE", "ZERO", "WS10", "WS16", "UNMAP" };
121 /* Offsets into our private area for storing information */
122 #define ccb_state ppriv_field0
123 #define ccb_bp ppriv_ptr1
128 u_int8_t secs_per_track;
129 u_int32_t secsize; /* Number of bytes/sector */
130 u_int64_t sectors; /* total number sectors */
135 #define UNMAP_MAX_RANGES 512
138 struct bio_queue_head bio_queue;
139 struct bio_queue_head delete_queue;
140 struct bio_queue_head delete_run_queue;
141 SLIST_ENTRY(da_softc) links;
142 LIST_HEAD(, ccb_hdr) pending_ccbs;
146 int minimum_cmd_size;
148 int ordered_tag_count;
149 int outstanding_cmds;
150 int unmap_max_ranges;
153 da_delete_methods delete_method;
154 struct disk_params params;
157 struct task sysctl_task;
158 struct sysctl_ctx_list sysctl_ctx;
159 struct sysctl_oid *sysctl_tree;
160 struct callout sendordered_c;
162 uint8_t unmap_buf[UNMAP_MAX_RANGES * 16 + 8];
165 struct da_quirk_entry {
166 struct scsi_inquiry_pattern inq_pat;
170 static const char quantum[] = "QUANTUM";
171 static const char microp[] = "MICROP";
173 static struct da_quirk_entry da_quirk_table[] =
175 /* SPI, FC devices */
178 * Fujitsu M2513A MO drives.
179 * Tested devices: M2513A2 firmware versions 1200 & 1300.
180 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
181 * Reported by: W.Scholten <whs@xs4all.nl>
183 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
184 /*quirks*/ DA_Q_NO_SYNC_CACHE
188 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
189 /*quirks*/ DA_Q_NO_SYNC_CACHE
193 * This particular Fujitsu drive doesn't like the
194 * synchronize cache command.
195 * Reported by: Tom Jackson <toj@gorilla.net>
197 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
198 /*quirks*/ DA_Q_NO_SYNC_CACHE
202 * This drive doesn't like the synchronize cache command
203 * either. Reported by: Matthew Jacob <mjacob@feral.com>
204 * in NetBSD PR kern/6027, August 24, 1998.
206 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
207 /*quirks*/ DA_Q_NO_SYNC_CACHE
211 * This drive doesn't like the synchronize cache command
212 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
215 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
216 /*quirks*/ DA_Q_NO_SYNC_CACHE
220 * Doesn't like the synchronize cache command.
221 * Reported by: Blaz Zupan <blaz@gold.amis.net>
223 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
224 /*quirks*/ DA_Q_NO_SYNC_CACHE
228 * Doesn't like the synchronize cache command.
229 * Reported by: Blaz Zupan <blaz@gold.amis.net>
231 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
232 /*quirks*/ DA_Q_NO_SYNC_CACHE
236 * Doesn't like the synchronize cache command.
238 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
239 /*quirks*/ DA_Q_NO_SYNC_CACHE
243 * Doesn't like the synchronize cache command.
244 * Reported by: walter@pelissero.de
246 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
247 /*quirks*/ DA_Q_NO_SYNC_CACHE
251 * Doesn't work correctly with 6 byte reads/writes.
252 * Returns illegal request, and points to byte 9 of the
254 * Reported by: Adam McDougall <bsdx@spawnet.com>
256 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
257 /*quirks*/ DA_Q_NO_6_BYTE
261 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
262 /*quirks*/ DA_Q_NO_6_BYTE
266 * Doesn't like the synchronize cache command.
267 * Reported by: walter@pelissero.de
269 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
270 /*quirks*/ DA_Q_NO_SYNC_CACHE
274 * The CISS RAID controllers do not support SYNC_CACHE
276 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
277 /*quirks*/ DA_Q_NO_SYNC_CACHE
279 /* USB mass storage devices supported by umass(4) */
282 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
285 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
286 /*quirks*/ DA_Q_NO_SYNC_CACHE
290 * Power Quotient Int. (PQI) USB flash key
293 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
294 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
298 * Creative Nomad MUVO mp3 player (USB)
301 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
302 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
306 * Jungsoft NEXDISK USB flash key
309 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
310 /*quirks*/ DA_Q_NO_SYNC_CACHE
314 * FreeDik USB Mini Data Drive
317 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
318 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
322 * Sigmatel USB Flash MP3 Player
325 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
326 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
330 * Neuros USB Digital Audio Computer
333 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
334 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
338 * SEAGRAND NP-900 MP3 Player
341 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
342 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
346 * iRiver iFP MP3 player (with UMS Firmware)
347 * PR: kern/54881, i386/63941, kern/66124
349 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
350 /*quirks*/ DA_Q_NO_SYNC_CACHE
354 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
357 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
358 /*quirks*/ DA_Q_NO_SYNC_CACHE
362 * ZICPlay USB MP3 Player with FM
365 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
366 /*quirks*/ DA_Q_NO_SYNC_CACHE
370 * TEAC USB floppy mechanisms
372 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
373 /*quirks*/ DA_Q_NO_SYNC_CACHE
377 * Kingston DataTraveler II+ USB Pen-Drive.
378 * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
380 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
381 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
385 * Motorola E398 Mobile Phone (TransFlash memory card).
386 * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
389 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
390 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
394 * Qware BeatZkey! Pro
397 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
398 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
402 * Time DPA20B 1GB MP3 Player
405 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
406 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
410 * Samsung USB key 128Mb
413 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
414 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
418 * Kingston DataTraveler 2.0 USB Flash memory.
421 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
422 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
426 * Creative MUVO Slim mp3 player (USB)
429 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
430 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
434 * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
437 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
438 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
442 * SanDisk Micro Cruzer 128MB
445 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
446 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
450 * TOSHIBA TransMemory USB sticks
453 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
454 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
459 * PR: usb/75578, usb/72344, usb/65436
461 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
462 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
466 * Genesys 6-in-1 Card Reader
469 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
470 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
474 * Rekam Digital CAMERA
477 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
478 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
482 * iRiver H10 MP3 player
485 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
486 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
490 * iRiver U10 MP3 player
493 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
494 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
501 {T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
502 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
506 * EasyMP3 EM732X USB 2.0 Flash MP3 Player
509 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
510 "1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
517 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
518 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
522 * Philips USB Key Audio KEY013
525 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
526 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
533 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
534 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
541 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
542 /*quirks*/ DA_Q_NO_SYNC_CACHE
546 * I/O Magic USB flash - Giga Bank
549 {T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
550 /*quirks*/ DA_Q_NO_SYNC_CACHE
554 * JoyFly 128mb USB Flash Drive
557 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
558 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
565 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
566 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
570 * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A
573 {T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*",
574 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
578 * Samsung YP-U3 mp3-player
581 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3",
582 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
585 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
586 "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
590 * Sony Cyber-Shot DSC cameras
593 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
594 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
596 /* ATA/SATA devices over SAS/USB/... */
598 /* Hitachi Advanced Format (4k) drives */
599 { T_DIRECT, SIP_MEDIA_FIXED, "Hitachi", "H??????????E3*", "*" },
603 /* Samsung Advanced Format (4k) drives */
604 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD155UI*", "*" },
608 /* Samsung Advanced Format (4k) drives */
609 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD155UI*", "*" },
613 /* Samsung Advanced Format (4k) drives */
614 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD204UI*", "*" },
618 /* Samsung Advanced Format (4k) drives */
619 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD204UI*", "*" },
623 /* Seagate Barracuda Green Advanced Format (4k) drives */
624 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DL*", "*" },
628 /* Seagate Barracuda Green Advanced Format (4k) drives */
629 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DL", "*", "*" },
633 /* Seagate Barracuda Green Advanced Format (4k) drives */
634 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???DM*", "*" },
638 /* Seagate Barracuda Green Advanced Format (4k) drives */
639 { T_DIRECT, SIP_MEDIA_FIXED, "ST???DM*", "*", "*" },
643 /* Seagate Barracuda Green Advanced Format (4k) drives */
644 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DM*", "*" },
648 /* Seagate Barracuda Green Advanced Format (4k) drives */
649 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DM", "*", "*" },
653 /* Seagate Momentus Advanced Format (4k) drives */
654 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500423AS*", "*" },
658 /* Seagate Momentus Advanced Format (4k) drives */
659 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "3AS*", "*" },
663 /* Seagate Momentus Advanced Format (4k) drives */
664 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500424AS*", "*" },
668 /* Seagate Momentus Advanced Format (4k) drives */
669 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "4AS*", "*" },
673 /* Seagate Momentus Advanced Format (4k) drives */
674 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640423AS*", "*" },
678 /* Seagate Momentus Advanced Format (4k) drives */
679 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "3AS*", "*" },
683 /* Seagate Momentus Advanced Format (4k) drives */
684 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640424AS*", "*" },
688 /* Seagate Momentus Advanced Format (4k) drives */
689 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "4AS*", "*" },
693 /* Seagate Momentus Advanced Format (4k) drives */
694 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750420AS*", "*" },
698 /* Seagate Momentus Advanced Format (4k) drives */
699 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "0AS*", "*" },
703 /* Seagate Momentus Advanced Format (4k) drives */
704 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750422AS*", "*" },
708 /* Seagate Momentus Advanced Format (4k) drives */
709 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "2AS*", "*" },
713 /* Seagate Momentus Advanced Format (4k) drives */
714 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750423AS*", "*" },
718 /* Seagate Momentus Advanced Format (4k) drives */
719 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "3AS*", "*" },
723 /* Seagate Momentus Thin Advanced Format (4k) drives */
724 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???LT*", "*" },
728 /* Seagate Momentus Thin Advanced Format (4k) drives */
729 { T_DIRECT, SIP_MEDIA_FIXED, "ST???LT*", "*", "*" },
733 /* WDC Caviar Green Advanced Format (4k) drives */
734 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RS*", "*" },
738 /* WDC Caviar Green Advanced Format (4k) drives */
739 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RS*", "*" },
743 /* WDC Caviar Green Advanced Format (4k) drives */
744 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RX*", "*" },
748 /* WDC Caviar Green Advanced Format (4k) drives */
749 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RX*", "*" },
753 /* WDC Caviar Green Advanced Format (4k) drives */
754 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RS*", "*" },
758 /* WDC Caviar Green Advanced Format (4k) drives */
759 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RS*", "*" },
763 /* WDC Caviar Green Advanced Format (4k) drives */
764 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RX*", "*" },
768 /* WDC Caviar Green Advanced Format (4k) drives */
769 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RX*", "*" },
773 /* WDC Scorpio Black Advanced Format (4k) drives */
774 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PKT*", "*" },
778 /* WDC Scorpio Black Advanced Format (4k) drives */
779 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PKT*", "*" },
783 /* WDC Scorpio Black Advanced Format (4k) drives */
784 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PKT*", "*" },
788 /* WDC Scorpio Black Advanced Format (4k) drives */
789 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PKT*", "*" },
793 /* WDC Scorpio Blue Advanced Format (4k) drives */
794 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PVT*", "*" },
798 /* WDC Scorpio Blue Advanced Format (4k) drives */
799 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PVT*", "*" },
803 /* WDC Scorpio Blue Advanced Format (4k) drives */
804 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PVT*", "*" },
808 /* WDC Scorpio Blue Advanced Format (4k) drives */
809 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PVT*", "*" },
814 * Olympus FE-210 camera
816 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "FE210*",
817 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
823 {T_DIRECT, SIP_MEDIA_REMOVABLE, "LG", "UP3S",
824 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
828 * Laser MP3-2GA13 MP3 player
830 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "(HS) Flash Disk",
831 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
835 static disk_strategy_t dastrategy;
836 static dumper_t dadump;
837 static periph_init_t dainit;
838 static void daasync(void *callback_arg, u_int32_t code,
839 struct cam_path *path, void *arg);
840 static void dasysctlinit(void *context, int pending);
841 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
842 static int dadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
843 static periph_ctor_t daregister;
844 static periph_dtor_t dacleanup;
845 static periph_start_t dastart;
846 static periph_oninv_t daoninvalidate;
847 static void dadone(struct cam_periph *periph,
848 union ccb *done_ccb);
849 static int daerror(union ccb *ccb, u_int32_t cam_flags,
850 u_int32_t sense_flags);
851 static void daprevent(struct cam_periph *periph, int action);
852 static int dagetcapacity(struct cam_periph *periph);
853 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
854 uint64_t maxsector, u_int lbppbe, u_int lalba);
855 static timeout_t dasendorderedtag;
856 static void dashutdown(void *arg, int howto);
858 #ifndef DA_DEFAULT_TIMEOUT
859 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
862 #ifndef DA_DEFAULT_RETRY
863 #define DA_DEFAULT_RETRY 4
866 #ifndef DA_DEFAULT_SEND_ORDERED
867 #define DA_DEFAULT_SEND_ORDERED 1
871 static int da_retry_count = DA_DEFAULT_RETRY;
872 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
873 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
875 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
876 "CAM Direct Access Disk driver");
877 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
878 &da_retry_count, 0, "Normal I/O retry count");
879 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
880 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
881 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
882 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
883 SYSCTL_INT(_kern_cam_da, OID_AUTO, da_send_ordered, CTLFLAG_RW,
884 &da_send_ordered, 0, "Send Ordered Tags");
885 TUNABLE_INT("kern.cam.da.da_send_ordered", &da_send_ordered);
888 * DA_ORDEREDTAG_INTERVAL determines how often, relative
889 * to the default timeout, we check to see whether an ordered
890 * tagged transaction is appropriate to prevent simple tag
891 * starvation. Since we'd like to ensure that there is at least
892 * 1/2 of the timeout length left for a starved transaction to
893 * complete after we've sent an ordered tag, we must poll at least
894 * four times in every timeout period. This takes care of the worst
895 * case where a starved transaction starts during an interval that
896 * meets the requirement "don't send an ordered tag" test so it takes
897 * us two intervals to determine that a tag must be sent.
899 #ifndef DA_ORDEREDTAG_INTERVAL
900 #define DA_ORDEREDTAG_INTERVAL 4
903 static struct periph_driver dadriver =
906 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
909 PERIPHDRIVER_DECLARE(da, dadriver);
911 MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
914 daopen(struct disk *dp)
916 struct cam_periph *periph;
917 struct da_softc *softc;
921 periph = (struct cam_periph *)dp->d_drv1;
922 if (periph == NULL) {
926 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
930 cam_periph_lock(periph);
931 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
932 cam_periph_unlock(periph);
933 cam_periph_release(periph);
937 unit = periph->unit_number;
938 softc = (struct da_softc *)periph->softc;
939 softc->flags |= DA_FLAG_OPEN;
941 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
944 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
945 /* Invalidate our pack information. */
946 softc->flags &= ~DA_FLAG_PACK_INVALID;
949 error = dagetcapacity(periph);
953 softc->disk->d_sectorsize = softc->params.secsize;
954 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
955 softc->disk->d_stripesize = softc->params.stripesize;
956 softc->disk->d_stripeoffset = softc->params.stripeoffset;
957 /* XXX: these are not actually "firmware" values, so they may be wrong */
958 softc->disk->d_fwsectors = softc->params.secs_per_track;
959 softc->disk->d_fwheads = softc->params.heads;
960 softc->disk->d_devstat->block_size = softc->params.secsize;
961 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
962 if (softc->delete_method > DA_DELETE_DISABLE)
963 softc->disk->d_flags |= DISKFLAG_CANDELETE;
965 softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
967 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
968 (softc->quirks & DA_Q_NO_PREVENT) == 0)
969 daprevent(periph, PR_PREVENT);
971 softc->flags &= ~DA_FLAG_OPEN;
973 cam_periph_unhold(periph);
974 cam_periph_unlock(periph);
977 cam_periph_release(periph);
983 daclose(struct disk *dp)
985 struct cam_periph *periph;
986 struct da_softc *softc;
989 periph = (struct cam_periph *)dp->d_drv1;
993 cam_periph_lock(periph);
994 if ((error = cam_periph_hold(periph, PRIBIO)) != 0) {
995 cam_periph_unlock(periph);
996 cam_periph_release(periph);
1000 softc = (struct da_softc *)periph->softc;
1002 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1005 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0
1006 && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
1009 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1011 scsi_synchronize_cache(&ccb->csio,
1015 /*begin_lba*/0,/* Cover the whole disk */
1020 cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
1021 /*sense_flags*/SF_RETRY_UA | SF_QUIET_IR,
1022 softc->disk->d_devstat);
1023 xpt_release_ccb(ccb);
1027 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
1028 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
1029 daprevent(periph, PR_ALLOW);
1031 * If we've got removeable media, mark the blocksize as
1032 * unavailable, since it could change when new media is
1035 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1038 softc->flags &= ~DA_FLAG_OPEN;
1039 cam_periph_unhold(periph);
1040 cam_periph_unlock(periph);
1041 cam_periph_release(periph);
1046 daschedule(struct cam_periph *periph)
1048 struct da_softc *softc = (struct da_softc *)periph->softc;
1051 /* Check if cam_periph_getccb() was called. */
1052 prio = periph->immediate_priority;
1054 /* Check if we have more work to do. */
1055 if (bioq_first(&softc->bio_queue) ||
1056 (!softc->delete_running && bioq_first(&softc->delete_queue))) {
1057 prio = CAM_PRIORITY_NORMAL;
1060 /* Schedule CCB if any of above is true. */
1061 if (prio != CAM_PRIORITY_NONE)
1062 xpt_schedule(periph, prio);
1066 * Actually translate the requested transfer into one the physical driver
1067 * can understand. The transfer is described by a buf and will include
1068 * only one physical transfer.
1071 dastrategy(struct bio *bp)
1073 struct cam_periph *periph;
1074 struct da_softc *softc;
1076 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1077 if (periph == NULL) {
1078 biofinish(bp, NULL, ENXIO);
1081 softc = (struct da_softc *)periph->softc;
1083 cam_periph_lock(periph);
1086 * If the device has been made invalid, error out
1088 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1089 cam_periph_unlock(periph);
1090 biofinish(bp, NULL, ENXIO);
1094 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
1097 * Place it in the queue of disk activities for this disk
1099 if (bp->bio_cmd == BIO_DELETE) {
1100 if (bp->bio_bcount == 0)
1103 bioq_disksort(&softc->delete_queue, bp);
1105 bioq_disksort(&softc->bio_queue, bp);
1108 * Schedule ourselves for performing the work.
1111 cam_periph_unlock(periph);
1117 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1119 struct cam_periph *periph;
1120 struct da_softc *softc;
1122 struct ccb_scsiio csio;
1127 periph = dp->d_drv1;
1130 softc = (struct da_softc *)periph->softc;
1131 cam_periph_lock(periph);
1132 secsize = softc->params.secsize;
1134 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1135 cam_periph_unlock(periph);
1140 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1141 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1142 scsi_read_write(&csio,
1148 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1151 /*data_ptr*/(u_int8_t *) virtual,
1152 /*dxfer_len*/length,
1153 /*sense_len*/SSD_FULL_SIZE,
1154 da_default_timeout * 1000);
1155 xpt_polled_action((union ccb *)&csio);
1157 error = cam_periph_error((union ccb *)&csio,
1158 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1159 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1160 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1161 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1163 printf("Aborting dump due to I/O error.\n");
1164 cam_periph_unlock(periph);
1169 * Sync the disk cache contents to the physical media.
1171 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1173 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1174 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1175 scsi_synchronize_cache(&csio,
1179 /*begin_lba*/0,/* Cover the whole disk */
1183 xpt_polled_action((union ccb *)&csio);
1185 error = cam_periph_error((union ccb *)&csio,
1186 0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL);
1187 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1188 cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1189 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1191 xpt_print(periph->path, "Synchronize cache failed\n");
1193 cam_periph_unlock(periph);
1198 dagetattr(struct bio *bp)
1201 struct cam_periph *periph;
1203 if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
1205 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1206 if (periph->path == NULL)
1209 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1212 bp->bio_completed = bp->bio_length;
1222 * Install a global async callback. This callback will
1223 * receive async callbacks like "new device found".
1225 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1227 if (status != CAM_REQ_CMP) {
1228 printf("da: Failed to attach master async callback "
1229 "due to status 0x%x!\n", status);
1230 } else if (da_send_ordered) {
1232 /* Register our shutdown event handler */
1233 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1234 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1235 printf("dainit: shutdown event registration failed!\n");
1240 daoninvalidate(struct cam_periph *periph)
1242 struct da_softc *softc;
1244 softc = (struct da_softc *)periph->softc;
1247 * De-register any async callbacks.
1249 xpt_register_async(0, daasync, periph, periph->path);
1251 softc->flags |= DA_FLAG_PACK_INVALID;
1254 * Return all queued I/O with ENXIO.
1255 * XXX Handle any transactions queued to the card
1256 * with XPT_ABORT_CCB.
1258 bioq_flush(&softc->bio_queue, NULL, ENXIO);
1259 bioq_flush(&softc->delete_queue, NULL, ENXIO);
1261 disk_gone(softc->disk);
1262 xpt_print(periph->path, "lost device - %d outstanding, %d refs\n",
1263 softc->outstanding_cmds, periph->refcount);
1267 dacleanup(struct cam_periph *periph)
1269 struct da_softc *softc;
1271 softc = (struct da_softc *)periph->softc;
1273 xpt_print(periph->path, "removing device entry\n");
1274 cam_periph_unlock(periph);
1277 * If we can't free the sysctl tree, oh well...
1279 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1280 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1281 xpt_print(periph->path, "can't remove sysctl context\n");
1284 disk_destroy(softc->disk);
1285 callout_drain(&softc->sendordered_c);
1286 free(softc, M_DEVBUF);
1287 cam_periph_lock(periph);
1291 daasync(void *callback_arg, u_int32_t code,
1292 struct cam_path *path, void *arg)
1294 struct cam_periph *periph;
1296 periph = (struct cam_periph *)callback_arg;
1298 case AC_FOUND_DEVICE:
1300 struct ccb_getdev *cgd;
1303 cgd = (struct ccb_getdev *)arg;
1307 if (cgd->protocol != PROTO_SCSI)
1310 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1311 && SID_TYPE(&cgd->inq_data) != T_RBC
1312 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1316 * Allocate a peripheral instance for
1317 * this device and start the probe
1320 status = cam_periph_alloc(daregister, daoninvalidate,
1322 "da", CAM_PERIPH_BIO,
1323 cgd->ccb_h.path, daasync,
1324 AC_FOUND_DEVICE, cgd);
1326 if (status != CAM_REQ_CMP
1327 && status != CAM_REQ_INPROG)
1328 printf("daasync: Unable to attach to new device "
1329 "due to status 0x%x\n", status);
1332 case AC_ADVINFO_CHANGED:
1336 buftype = (uintptr_t)arg;
1337 if (buftype == CDAI_TYPE_PHYS_PATH) {
1338 struct da_softc *softc;
1340 softc = periph->softc;
1341 disk_attr_changed(softc->disk, "GEOM::physpath",
1349 struct da_softc *softc;
1350 struct ccb_hdr *ccbh;
1352 softc = (struct da_softc *)periph->softc;
1354 * Don't fail on the expected unit attention
1357 softc->flags |= DA_FLAG_RETRY_UA;
1358 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1359 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1365 cam_periph_async(periph, code, path, arg);
1369 dasysctlinit(void *context, int pending)
1371 struct cam_periph *periph;
1372 struct da_softc *softc;
1373 char tmpstr[80], tmpstr2[80];
1374 struct ccb_trans_settings cts;
1376 periph = (struct cam_periph *)context;
1378 * periph was held for us when this task was enqueued
1380 if (periph->flags & CAM_PERIPH_INVALID) {
1381 cam_periph_release(periph);
1385 softc = (struct da_softc *)periph->softc;
1386 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1387 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1389 sysctl_ctx_init(&softc->sysctl_ctx);
1390 softc->flags |= DA_FLAG_SCTX_INIT;
1391 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1392 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1393 CTLFLAG_RD, 0, tmpstr);
1394 if (softc->sysctl_tree == NULL) {
1395 printf("dasysctlinit: unable to allocate sysctl tree\n");
1396 cam_periph_release(periph);
1401 * Now register the sysctl handler, so the user can change the value on
1404 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1405 OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RW,
1406 &softc->delete_method, 0, dadeletemethodsysctl, "A",
1407 "BIO_DELETE execution method");
1408 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1409 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1410 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1411 "Minimum CDB size");
1413 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1414 SYSCTL_CHILDREN(softc->sysctl_tree),
1418 &softc->error_inject,
1420 "error_inject leaf");
1424 * Add some addressing info.
1426 memset(&cts, 0, sizeof (cts));
1427 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
1428 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1429 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1430 cam_periph_lock(periph);
1431 xpt_action((union ccb *)&cts);
1432 cam_periph_unlock(periph);
1433 if (cts.ccb_h.status != CAM_REQ_CMP) {
1434 cam_periph_release(periph);
1437 if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
1438 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1439 if (fc->valid & CTS_FC_VALID_WWPN) {
1440 softc->wwpn = fc->wwpn;
1441 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1442 SYSCTL_CHILDREN(softc->sysctl_tree),
1443 OID_AUTO, "wwpn", CTLFLAG_RD,
1444 &softc->wwpn, "World Wide Port Name");
1447 cam_periph_release(periph);
1451 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1455 value = *(int *)arg1;
1457 error = sysctl_handle_int(oidp, &value, 0, req);
1460 || (req->newptr == NULL))
1464 * Acceptable values here are 6, 10, 12 or 16.
1468 else if ((value > 6)
1471 else if ((value > 10)
1474 else if (value > 12)
1477 *(int *)arg1 = value;
1483 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
1490 value = *(int *)arg1;
1491 if (value < 0 || value > DA_DELETE_MAX)
1494 p = da_delete_method_names[value];
1495 strncpy(buf, p, sizeof(buf));
1496 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1497 if (error != 0 || req->newptr == NULL)
1499 for (i = 0; i <= DA_DELETE_MAX; i++) {
1500 if (strcmp(buf, da_delete_method_names[i]) != 0)
1509 daregister(struct cam_periph *periph, void *arg)
1511 struct da_softc *softc;
1512 struct ccb_pathinq cpi;
1513 struct ccb_getdev *cgd;
1517 cgd = (struct ccb_getdev *)arg;
1518 if (periph == NULL) {
1519 printf("daregister: periph was NULL!!\n");
1520 return(CAM_REQ_CMP_ERR);
1524 printf("daregister: no getdev CCB, can't register device\n");
1525 return(CAM_REQ_CMP_ERR);
1528 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
1531 if (softc == NULL) {
1532 printf("daregister: Unable to probe new device. "
1533 "Unable to allocate softc\n");
1534 return(CAM_REQ_CMP_ERR);
1537 LIST_INIT(&softc->pending_ccbs);
1538 softc->state = DA_STATE_PROBE;
1539 bioq_init(&softc->bio_queue);
1540 bioq_init(&softc->delete_queue);
1541 bioq_init(&softc->delete_run_queue);
1542 if (SID_IS_REMOVABLE(&cgd->inq_data))
1543 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1544 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1545 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1546 softc->unmap_max_ranges = UNMAP_MAX_RANGES;
1547 softc->unmap_max_lba = 1024*1024*2;
1549 periph->softc = softc;
1552 * See if this device has any quirks.
1554 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1555 (caddr_t)da_quirk_table,
1556 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1557 sizeof(*da_quirk_table), scsi_inquiry_match);
1560 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1562 softc->quirks = DA_Q_NONE;
1564 /* Check if the SIM does not want 6 byte commands */
1565 bzero(&cpi, sizeof(cpi));
1566 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1567 cpi.ccb_h.func_code = XPT_PATH_INQ;
1568 xpt_action((union ccb *)&cpi);
1569 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1570 softc->quirks |= DA_Q_NO_6_BYTE;
1572 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1575 * Take an exclusive refcount on the periph while dastart is called
1576 * to finish the probe. The reference will be dropped in dadone at
1579 (void)cam_periph_hold(periph, PRIBIO);
1582 * Schedule a periodic event to occasionally send an
1583 * ordered tag to a device.
1585 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
1586 callout_reset(&softc->sendordered_c,
1587 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
1588 dasendorderedtag, softc);
1590 mtx_unlock(periph->sim->mtx);
1592 * RBC devices don't have to support READ(6), only READ(10).
1594 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1595 softc->minimum_cmd_size = 10;
1597 softc->minimum_cmd_size = 6;
1600 * Load the user's default, if any.
1602 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1603 periph->unit_number);
1604 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1607 * 6, 10, 12 and 16 are the currently permissible values.
1609 if (softc->minimum_cmd_size < 6)
1610 softc->minimum_cmd_size = 6;
1611 else if ((softc->minimum_cmd_size > 6)
1612 && (softc->minimum_cmd_size <= 10))
1613 softc->minimum_cmd_size = 10;
1614 else if ((softc->minimum_cmd_size > 10)
1615 && (softc->minimum_cmd_size <= 12))
1616 softc->minimum_cmd_size = 12;
1617 else if (softc->minimum_cmd_size > 12)
1618 softc->minimum_cmd_size = 16;
1620 /* Predict whether device may support READ CAPACITY(16). */
1621 if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3) {
1622 softc->flags |= DA_FLAG_CAN_RC16;
1623 softc->state = DA_STATE_PROBE2;
1627 * Register this media as a disk.
1629 softc->disk = disk_alloc();
1630 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
1631 periph->unit_number, 0,
1632 DEVSTAT_BS_UNAVAILABLE,
1633 SID_TYPE(&cgd->inq_data) |
1634 XPORT_DEVSTAT_TYPE(cpi.transport),
1635 DEVSTAT_PRIORITY_DISK);
1636 softc->disk->d_open = daopen;
1637 softc->disk->d_close = daclose;
1638 softc->disk->d_strategy = dastrategy;
1639 softc->disk->d_dump = dadump;
1640 softc->disk->d_getattr = dagetattr;
1641 softc->disk->d_name = "da";
1642 softc->disk->d_drv1 = periph;
1644 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */
1645 else if (cpi.maxio > MAXPHYS)
1646 softc->disk->d_maxsize = MAXPHYS; /* for safety */
1648 softc->disk->d_maxsize = cpi.maxio;
1649 softc->disk->d_unit = periph->unit_number;
1650 softc->disk->d_flags = 0;
1651 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
1652 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
1653 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
1654 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
1655 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
1656 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
1657 cgd->inq_data.product, sizeof(cgd->inq_data.product),
1658 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
1659 softc->disk->d_hba_vendor = cpi.hba_vendor;
1660 softc->disk->d_hba_device = cpi.hba_device;
1661 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
1662 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
1663 disk_create(softc->disk, DISK_VERSION);
1664 mtx_lock(periph->sim->mtx);
1667 * Add async callbacks for events of interest.
1668 * I don't bother checking if this fails as,
1669 * in most cases, the system will function just
1670 * fine without them and the only alternative
1671 * would be to not attach the device on failure.
1673 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET
1674 | AC_LOST_DEVICE | AC_ADVINFO_CHANGED,
1675 daasync, periph, periph->path);
1678 * Emit an attribute changed notification just in case
1679 * physical path information arrived before our async
1680 * event handler was registered, but after anyone attaching
1681 * to our disk device polled it.
1683 disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
1685 xpt_schedule(periph, CAM_PRIORITY_DEV);
1687 return(CAM_REQ_CMP);
1691 dastart(struct cam_periph *periph, union ccb *start_ccb)
1693 struct da_softc *softc;
1695 softc = (struct da_softc *)periph->softc;
1697 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
1699 switch (softc->state) {
1700 case DA_STATE_NORMAL:
1702 struct bio *bp, *bp1;
1705 /* Execute immediate CCB if waiting. */
1706 if (periph->immediate_priority <= periph->pinfo.priority) {
1707 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1708 ("queuing for immediate ccb\n"));
1709 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1710 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1712 periph->immediate_priority = CAM_PRIORITY_NONE;
1713 wakeup(&periph->ccb_list);
1714 /* May have more work to do, so ensure we stay scheduled */
1719 /* Run BIO_DELETE if not running yet. */
1720 if (!softc->delete_running &&
1721 (bp = bioq_first(&softc->delete_queue)) != NULL) {
1725 if (softc->delete_method == DA_DELETE_UNMAP) {
1726 uint8_t *buf = softc->unmap_buf;
1727 uint64_t lastlba = (uint64_t)-1;
1728 uint32_t lastcount = 0;
1729 int blocks = 0, off, ranges = 0;
1731 softc->delete_running = 1;
1732 bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
1735 bioq_remove(&softc->delete_queue, bp1);
1737 bioq_insert_tail(&softc->delete_run_queue, bp1);
1738 lba = bp1->bio_pblkno;
1739 count = bp1->bio_bcount / softc->params.secsize;
1741 /* Try to extend the previous range. */
1742 if (lba == lastlba) {
1744 off = (ranges - 1) * 16 + 8;
1745 scsi_ulto4b(lastcount, &buf[off + 8]);
1746 } else if (count > 0) {
1747 off = ranges * 16 + 8;
1748 scsi_u64to8b(lba, &buf[off + 0]);
1749 scsi_ulto4b(count, &buf[off + 8]);
1754 lastlba = lba + count;
1755 bp1 = bioq_first(&softc->delete_queue);
1757 ranges >= softc->unmap_max_ranges ||
1758 blocks + bp1->bio_bcount /
1759 softc->params.secsize > softc->unmap_max_lba)
1762 scsi_ulto2b(count * 16 + 6, &buf[0]);
1763 scsi_ulto2b(count * 16, &buf[2]);
1765 scsi_unmap(&start_ccb->csio,
1766 /*retries*/da_retry_count,
1768 /*tag_action*/MSG_SIMPLE_Q_TAG,
1771 /*dxfer_len*/ count * 16 + 8,
1772 /*sense_len*/SSD_FULL_SIZE,
1773 da_default_timeout * 1000);
1774 start_ccb->ccb_h.ccb_state = DA_CCB_DELETE;
1776 } else if (softc->delete_method == DA_DELETE_ZERO ||
1777 softc->delete_method == DA_DELETE_WS10 ||
1778 softc->delete_method == DA_DELETE_WS16) {
1779 softc->delete_running = 1;
1780 lba = bp->bio_pblkno;
1784 bioq_remove(&softc->delete_queue, bp1);
1786 bioq_insert_tail(&softc->delete_run_queue, bp1);
1787 count += bp1->bio_bcount / softc->params.secsize;
1788 bp1 = bioq_first(&softc->delete_queue);
1790 lba + count != bp1->bio_pblkno ||
1791 count + bp1->bio_bcount /
1792 softc->params.secsize > 0xffff)
1796 scsi_write_same(&start_ccb->csio,
1797 /*retries*/da_retry_count,
1799 /*tag_action*/MSG_SIMPLE_Q_TAG,
1800 /*byte2*/softc->delete_method ==
1801 DA_DELETE_ZERO ? 0 : SWS_UNMAP,
1802 softc->delete_method ==
1803 DA_DELETE_WS16 ? 16 : 10,
1805 /*block_count*/count,
1806 /*data_ptr*/ __DECONST(void *,
1808 /*dxfer_len*/ softc->params.secsize,
1809 /*sense_len*/SSD_FULL_SIZE,
1810 da_default_timeout * 1000);
1811 start_ccb->ccb_h.ccb_state = DA_CCB_DELETE;
1814 bioq_flush(&softc->delete_queue, NULL, 0);
1819 /* Run regular command. */
1820 bp = bioq_takefirst(&softc->bio_queue);
1822 xpt_release_ccb(start_ccb);
1826 if ((bp->bio_flags & BIO_ORDERED) != 0 ||
1827 (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1828 softc->flags &= ~DA_FLAG_NEED_OTAG;
1829 softc->ordered_tag_count++;
1830 tag_code = MSG_ORDERED_Q_TAG;
1832 tag_code = MSG_SIMPLE_Q_TAG;
1835 switch (bp->bio_cmd) {
1838 scsi_read_write(&start_ccb->csio,
1839 /*retries*/da_retry_count,
1841 /*tag_action*/tag_code,
1842 /*read_op*/bp->bio_cmd
1845 softc->minimum_cmd_size,
1846 /*lba*/bp->bio_pblkno,
1847 /*block_count*/bp->bio_bcount /
1848 softc->params.secsize,
1849 /*data_ptr*/ bp->bio_data,
1850 /*dxfer_len*/ bp->bio_bcount,
1851 /*sense_len*/SSD_FULL_SIZE,
1852 da_default_timeout * 1000);
1856 * BIO_FLUSH doesn't currently communicate
1857 * range data, so we synchronize the cache
1858 * over the whole disk. We also force
1859 * ordered tag semantics the flush applies
1860 * to all previously queued I/O.
1862 scsi_synchronize_cache(&start_ccb->csio,
1869 da_default_timeout*1000);
1872 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1876 * Block out any asyncronous callbacks
1877 * while we touch the pending ccb list.
1879 LIST_INSERT_HEAD(&softc->pending_ccbs,
1880 &start_ccb->ccb_h, periph_links.le);
1881 softc->outstanding_cmds++;
1883 /* We expect a unit attention from this device */
1884 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1885 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1886 softc->flags &= ~DA_FLAG_RETRY_UA;
1889 start_ccb->ccb_h.ccb_bp = bp;
1890 xpt_action(start_ccb);
1892 /* May have more work to do, so ensure we stay scheduled */
1896 case DA_STATE_PROBE:
1898 struct ccb_scsiio *csio;
1899 struct scsi_read_capacity_data *rcap;
1901 rcap = (struct scsi_read_capacity_data *)
1902 malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
1904 printf("dastart: Couldn't malloc read_capacity data\n");
1905 /* da_free_periph??? */
1908 csio = &start_ccb->csio;
1909 scsi_read_capacity(csio,
1916 start_ccb->ccb_h.ccb_bp = NULL;
1917 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1918 xpt_action(start_ccb);
1921 case DA_STATE_PROBE2:
1923 struct ccb_scsiio *csio;
1924 struct scsi_read_capacity_data_long *rcaplong;
1926 rcaplong = (struct scsi_read_capacity_data_long *)
1927 malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
1928 if (rcaplong == NULL) {
1929 printf("dastart: Couldn't malloc read_capacity data\n");
1930 /* da_free_periph??? */
1933 csio = &start_ccb->csio;
1934 scsi_read_capacity_16(csio,
1937 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1942 /*sense_len*/ SSD_FULL_SIZE,
1944 start_ccb->ccb_h.ccb_bp = NULL;
1945 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1946 xpt_action(start_ccb);
1953 cmd6workaround(union ccb *ccb)
1955 struct scsi_rw_6 cmd6;
1956 struct scsi_rw_10 *cmd10;
1957 struct da_softc *softc;
1962 cdb = ccb->csio.cdb_io.cdb_bytes;
1963 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1965 if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
1966 if (softc->delete_method == DA_DELETE_UNMAP) {
1967 xpt_print(ccb->ccb_h.path, "UNMAP is not supported, "
1968 "switching to WRITE SAME(16) with UNMAP.\n");
1969 softc->delete_method = DA_DELETE_WS16;
1970 } else if (softc->delete_method == DA_DELETE_WS16) {
1971 xpt_print(ccb->ccb_h.path,
1972 "WRITE SAME(16) with UNMAP is not supported, "
1973 "disabling BIO_DELETE.\n");
1974 softc->delete_method = DA_DELETE_DISABLE;
1975 } else if (softc->delete_method == DA_DELETE_WS10) {
1976 xpt_print(ccb->ccb_h.path,
1977 "WRITE SAME(10) with UNMAP is not supported, "
1978 "disabling BIO_DELETE.\n");
1979 softc->delete_method = DA_DELETE_DISABLE;
1980 } else if (softc->delete_method == DA_DELETE_ZERO) {
1981 xpt_print(ccb->ccb_h.path,
1982 "WRITE SAME(10) is not supported, "
1983 "disabling BIO_DELETE.\n");
1984 softc->delete_method = DA_DELETE_DISABLE;
1986 softc->delete_method = DA_DELETE_DISABLE;
1987 while ((bp = bioq_takefirst(&softc->delete_run_queue))
1989 bioq_disksort(&softc->delete_queue, bp);
1990 bioq_insert_tail(&softc->delete_queue,
1991 (struct bio *)ccb->ccb_h.ccb_bp);
1992 ccb->ccb_h.ccb_bp = NULL;
1996 /* Translation only possible if CDB is an array and cmd is R/W6 */
1997 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1998 (*cdb != READ_6 && *cdb != WRITE_6))
2001 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
2002 "increasing minimum_cmd_size to 10.\n");
2003 softc->minimum_cmd_size = 10;
2005 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
2006 cmd10 = (struct scsi_rw_10 *)cdb;
2007 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
2009 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
2010 cmd10->reserved = 0;
2011 scsi_ulto2b(cmd6.length, cmd10->length);
2012 cmd10->control = cmd6.control;
2013 ccb->csio.cdb_len = sizeof(*cmd10);
2015 /* Requeue request, unfreezing queue if necessary */
2016 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
2017 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2020 cam_release_devq(ccb->ccb_h.path,
2024 /*getcount_only*/0);
2030 dadone(struct cam_periph *periph, union ccb *done_ccb)
2032 struct da_softc *softc;
2033 struct ccb_scsiio *csio;
2036 softc = (struct da_softc *)periph->softc;
2037 priority = done_ccb->ccb_h.pinfo.priority;
2039 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
2041 csio = &done_ccb->csio;
2042 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
2043 case DA_CCB_BUFFER_IO:
2046 struct bio *bp, *bp1;
2048 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2049 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2053 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
2058 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
2059 if (error == ERESTART) {
2061 * A retry was scheuled, so
2066 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2071 * return all queued I/O with EIO, so that
2072 * the client can retry these I/Os in the
2073 * proper order should it attempt to recover.
2078 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
2080 * Catastrophic error. Mark our pack as
2084 * XXX See if this is really a media
2087 xpt_print(periph->path,
2088 "Invalidating pack\n");
2089 softc->flags |= DA_FLAG_PACK_INVALID;
2090 queued_error = ENXIO;
2092 bioq_flush(&softc->bio_queue, NULL,
2095 bp->bio_error = error;
2096 bp->bio_resid = bp->bio_bcount;
2097 bp->bio_flags |= BIO_ERROR;
2099 } else if (bp != NULL) {
2100 bp->bio_resid = csio->resid;
2102 if (bp->bio_resid != 0)
2103 bp->bio_flags |= BIO_ERROR;
2105 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2106 cam_release_devq(done_ccb->ccb_h.path,
2110 /*getcount_only*/0);
2111 } else if (bp != NULL) {
2112 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2113 panic("REQ_CMP with QFRZN");
2114 bp->bio_resid = csio->resid;
2115 if (csio->resid > 0)
2116 bp->bio_flags |= BIO_ERROR;
2117 if (softc->error_inject != 0) {
2118 bp->bio_error = softc->error_inject;
2119 bp->bio_resid = bp->bio_bcount;
2120 bp->bio_flags |= BIO_ERROR;
2121 softc->error_inject = 0;
2127 * Block out any asyncronous callbacks
2128 * while we touch the pending ccb list.
2130 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
2131 softc->outstanding_cmds--;
2132 if (softc->outstanding_cmds == 0)
2133 softc->flags |= DA_FLAG_WENT_IDLE;
2135 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
2136 xpt_print(periph->path, "oustanding %d\n",
2137 softc->outstanding_cmds);
2140 if ((csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) ==
2142 while ((bp1 = bioq_takefirst(&softc->delete_run_queue))
2144 bp1->bio_resid = bp->bio_resid;
2145 bp1->bio_error = bp->bio_error;
2146 if (bp->bio_flags & BIO_ERROR)
2147 bp1->bio_flags |= BIO_ERROR;
2150 softc->delete_running = 0;
2154 } else if (bp != NULL)
2161 struct scsi_read_capacity_data *rdcap;
2162 struct scsi_read_capacity_data_long *rcaplong;
2163 char announce_buf[80];
2167 if (softc->state == DA_STATE_PROBE)
2168 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
2170 rcaplong = (struct scsi_read_capacity_data_long *)
2173 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
2174 struct disk_params *dp;
2175 uint32_t block_size;
2177 u_int lbppbe; /* LB per physical block exponent. */
2178 u_int lalba; /* Lowest aligned LBA. */
2180 if (softc->state == DA_STATE_PROBE) {
2181 block_size = scsi_4btoul(rdcap->length);
2182 maxsector = scsi_4btoul(rdcap->addr);
2187 * According to SBC-2, if the standard 10
2188 * byte READ CAPACITY command returns 2^32,
2189 * we should issue the 16 byte version of
2190 * the command, since the device in question
2191 * has more sectors than can be represented
2192 * with the short version of the command.
2194 if (maxsector == 0xffffffff) {
2195 softc->state = DA_STATE_PROBE2;
2196 free(rdcap, M_SCSIDA);
2197 xpt_release_ccb(done_ccb);
2198 xpt_schedule(periph, priority);
2202 block_size = scsi_4btoul(rcaplong->length);
2203 maxsector = scsi_8btou64(rcaplong->addr);
2204 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
2205 lalba = scsi_2btoul(rcaplong->lalba_lbp);
2209 * Because GEOM code just will panic us if we
2210 * give them an 'illegal' value we'll avoid that
2213 if (block_size == 0 && maxsector == 0) {
2214 snprintf(announce_buf, sizeof(announce_buf),
2216 } else if (block_size >= MAXPHYS || block_size == 0) {
2217 xpt_print(periph->path,
2218 "unsupportable block size %ju\n",
2219 (uintmax_t) block_size);
2220 announce_buf[0] = '\0';
2221 cam_periph_invalidate(periph);
2223 dasetgeom(periph, block_size, maxsector,
2224 lbppbe, lalba & SRC16_LALBA);
2225 if ((lalba & SRC16_LBPME) &&
2226 softc->delete_method == DA_DELETE_NONE)
2227 softc->delete_method = DA_DELETE_UNMAP;
2228 dp = &softc->params;
2229 snprintf(announce_buf, sizeof(announce_buf),
2230 "%juMB (%ju %u byte sectors: %dH %dS/T "
2232 (((uintmax_t)dp->secsize *
2233 dp->sectors) / (1024*1024)),
2234 (uintmax_t)dp->sectors,
2235 dp->secsize, dp->heads,
2236 dp->secs_per_track, dp->cylinders);
2241 announce_buf[0] = '\0';
2244 * Retry any UNIT ATTENTION type errors. They
2245 * are expected at boot.
2247 error = daerror(done_ccb, CAM_RETRY_SELTO,
2248 SF_RETRY_UA|SF_NO_PRINT);
2249 if (error == ERESTART) {
2251 * A retry was scheuled, so
2255 } else if (error != 0) {
2256 struct scsi_sense_data *sense;
2258 int sense_key, error_code;
2261 struct ccb_getdev cgd;
2263 /* Don't wedge this device's queue */
2264 status = done_ccb->ccb_h.status;
2265 if ((status & CAM_DEV_QFRZN) != 0)
2266 cam_release_devq(done_ccb->ccb_h.path,
2270 /*getcount_only*/0);
2273 xpt_setup_ccb(&cgd.ccb_h,
2274 done_ccb->ccb_h.path,
2275 CAM_PRIORITY_NORMAL);
2276 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2277 xpt_action((union ccb *)&cgd);
2279 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
2280 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
2281 || ((status & CAM_AUTOSNS_VALID) == 0))
2287 sense = &csio->sense_data;
2288 scsi_extract_sense_len(sense,
2289 csio->sense_len - csio->sense_resid,
2290 &error_code, &sense_key, &asc,
2291 &ascq, /*show_errors*/ 1);
2294 * If we tried READ CAPACITY(16) and failed,
2295 * fallback to READ CAPACITY(10).
2297 if ((softc->state == DA_STATE_PROBE2) &&
2298 (softc->flags & DA_FLAG_CAN_RC16) &&
2299 (((csio->ccb_h.status & CAM_STATUS_MASK) ==
2302 (error_code == SSD_CURRENT_ERROR) &&
2303 (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
2304 softc->flags &= ~DA_FLAG_CAN_RC16;
2305 softc->state = DA_STATE_PROBE;
2306 free(rdcap, M_SCSIDA);
2307 xpt_release_ccb(done_ccb);
2308 xpt_schedule(periph, priority);
2312 * Attach to anything that claims to be a
2313 * direct access or optical disk device,
2314 * as long as it doesn't return a "Logical
2315 * unit not supported" (0x25) error.
2317 if ((have_sense) && (asc != 0x25)
2318 && (error_code == SSD_CURRENT_ERROR)) {
2319 const char *sense_key_desc;
2320 const char *asc_desc;
2322 scsi_sense_desc(sense_key, asc, ascq,
2326 snprintf(announce_buf,
2327 sizeof(announce_buf),
2328 "Attempt to query device "
2329 "size failed: %s, %s",
2337 xpt_print(periph->path,
2338 "got CAM status %#x\n",
2339 done_ccb->ccb_h.status);
2342 xpt_print(periph->path, "fatal error, "
2343 "failed to attach to device\n");
2346 * Free up resources.
2348 cam_periph_invalidate(periph);
2352 free(csio->data_ptr, M_SCSIDA);
2353 if (announce_buf[0] != '\0') {
2355 * Create our sysctl variables, now that we know
2356 * we have successfully attached.
2358 /* increase the refcount */
2359 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
2360 taskqueue_enqueue(taskqueue_thread,
2361 &softc->sysctl_task);
2362 xpt_announce_periph(periph, announce_buf);
2364 xpt_print(periph->path, "fatal error, "
2365 "could not acquire reference count\n");
2369 softc->state = DA_STATE_NORMAL;
2371 * Since our peripheral may be invalidated by an error
2372 * above or an external event, we must release our CCB
2373 * before releasing the probe lock on the peripheral.
2374 * The peripheral will only go away once the last lock
2375 * is removed, and we need it around for the CCB release
2378 xpt_release_ccb(done_ccb);
2379 cam_periph_unhold(periph);
2382 case DA_CCB_WAITING:
2384 /* Caller will release the CCB */
2385 wakeup(&done_ccb->ccb_h.cbfcnp);
2389 /* No-op. We're polling */
2394 xpt_release_ccb(done_ccb);
2398 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
2400 struct da_softc *softc;
2401 struct cam_periph *periph;
2404 periph = xpt_path_periph(ccb->ccb_h.path);
2405 softc = (struct da_softc *)periph->softc;
2408 * Automatically detect devices that do not support
2409 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
2412 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
2413 error = cmd6workaround(ccb);
2414 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
2415 CAM_SCSI_STATUS_ERROR)
2416 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
2417 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
2418 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
2419 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
2420 int sense_key, error_code, asc, ascq;
2422 scsi_extract_sense(&ccb->csio.sense_data,
2423 &error_code, &sense_key, &asc, &ascq);
2424 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
2425 error = cmd6workaround(ccb);
2427 if (error == ERESTART)
2432 * Until we have a better way of doing pack validation,
2433 * don't treat UAs as errors.
2435 sense_flags |= SF_RETRY_UA;
2436 return(cam_periph_error(ccb, cam_flags, sense_flags,
2437 &softc->saved_ccb));
2441 daprevent(struct cam_periph *periph, int action)
2443 struct da_softc *softc;
2447 softc = (struct da_softc *)periph->softc;
2449 if (((action == PR_ALLOW)
2450 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
2451 || ((action == PR_PREVENT)
2452 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
2456 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2458 scsi_prevent(&ccb->csio,
2466 error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
2467 SF_RETRY_UA | SF_QUIET_IR, softc->disk->d_devstat);
2470 if (action == PR_ALLOW)
2471 softc->flags &= ~DA_FLAG_PACK_LOCKED;
2473 softc->flags |= DA_FLAG_PACK_LOCKED;
2476 xpt_release_ccb(ccb);
2480 dagetcapacity(struct cam_periph *periph)
2482 struct da_softc *softc;
2484 struct scsi_read_capacity_data *rcap;
2485 struct scsi_read_capacity_data_long *rcaplong;
2488 int error, rc16failed;
2489 u_int32_t sense_flags;
2490 u_int lbppbe; /* Logical blocks per physical block exponent. */
2491 u_int lalba; /* Lowest aligned LBA. */
2493 softc = (struct da_softc *)periph->softc;
2500 sense_flags = SF_RETRY_UA;
2501 if (softc->flags & DA_FLAG_PACK_REMOVABLE)
2502 sense_flags |= SF_NO_PRINT;
2504 /* Do a read capacity */
2505 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcaplong),
2510 rcaplong = (struct scsi_read_capacity_data_long *)rcap;
2512 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2514 /* Try READ CAPACITY(16) first if we think it should work. */
2515 if (softc->flags & DA_FLAG_CAN_RC16) {
2516 scsi_read_capacity_16(&ccb->csio,
2519 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2524 /*sense_len*/ SSD_FULL_SIZE,
2526 ccb->ccb_h.ccb_bp = NULL;
2528 error = cam_periph_runccb(ccb, daerror,
2529 /*cam_flags*/CAM_RETRY_SELTO,
2531 softc->disk->d_devstat);
2535 /* If we got ILLEGAL REQUEST, do not prefer RC16 any more. */
2536 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
2538 softc->flags &= ~DA_FLAG_CAN_RC16;
2539 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
2540 CAM_SCSI_STATUS_ERROR) &&
2541 (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND) &&
2542 (ccb->ccb_h.status & CAM_AUTOSNS_VALID) &&
2543 ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0) &&
2544 ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
2545 int sense_key, error_code, asc, ascq;
2547 scsi_extract_sense(&ccb->csio.sense_data,
2548 &error_code, &sense_key, &asc, &ascq);
2549 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
2550 softc->flags &= ~DA_FLAG_CAN_RC16;
2555 /* Do READ CAPACITY(10). */
2556 scsi_read_capacity(&ccb->csio,
2563 ccb->ccb_h.ccb_bp = NULL;
2565 error = cam_periph_runccb(ccb, daerror,
2566 /*cam_flags*/CAM_RETRY_SELTO,
2568 softc->disk->d_devstat);
2570 block_len = scsi_4btoul(rcap->length);
2571 maxsector = scsi_4btoul(rcap->addr);
2573 if (maxsector != 0xffffffff || rc16failed)
2578 /* If READ CAPACITY(10) returned overflow, use READ CAPACITY(16) */
2579 scsi_read_capacity_16(&ccb->csio,
2582 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2587 /*sense_len*/ SSD_FULL_SIZE,
2589 ccb->ccb_h.ccb_bp = NULL;
2591 error = cam_periph_runccb(ccb, daerror,
2592 /*cam_flags*/CAM_RETRY_SELTO,
2594 softc->disk->d_devstat);
2597 block_len = scsi_4btoul(rcaplong->length);
2598 maxsector = scsi_8btou64(rcaplong->addr);
2599 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
2600 lalba = scsi_2btoul(rcaplong->lalba_lbp);
2606 if (block_len >= MAXPHYS || block_len == 0) {
2607 xpt_print(periph->path,
2608 "unsupportable block size %ju\n",
2609 (uintmax_t) block_len);
2612 dasetgeom(periph, block_len, maxsector,
2613 lbppbe, lalba & SRC16_LALBA);
2614 if ((lalba & SRC16_LBPME) &&
2615 softc->delete_method == DA_DELETE_NONE)
2616 softc->delete_method = DA_DELETE_UNMAP;
2620 xpt_release_ccb(ccb);
2622 free(rcap, M_SCSIDA);
2628 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
2629 u_int lbppbe, u_int lalba)
2631 struct ccb_calc_geometry ccg;
2632 struct da_softc *softc;
2633 struct disk_params *dp;
2635 softc = (struct da_softc *)periph->softc;
2637 dp = &softc->params;
2638 dp->secsize = block_len;
2639 dp->sectors = maxsector + 1;
2641 dp->stripesize = block_len << lbppbe;
2642 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
2644 } else if (softc->quirks & DA_Q_4K) {
2645 dp->stripesize = 4096;
2646 dp->stripeoffset = 0;
2649 dp->stripeoffset = 0;
2652 * Have the controller provide us with a geometry
2653 * for this disk. The only time the geometry
2654 * matters is when we boot and the controller
2655 * is the only one knowledgeable enough to come
2656 * up with something that will make this a bootable
2659 xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2660 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
2661 ccg.block_size = dp->secsize;
2662 ccg.volume_size = dp->sectors;
2664 ccg.secs_per_track = 0;
2666 xpt_action((union ccb*)&ccg);
2667 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2669 * We don't know what went wrong here- but just pick
2670 * a geometry so we don't have nasty things like divide
2674 dp->secs_per_track = 255;
2675 dp->cylinders = dp->sectors / (255 * 255);
2676 if (dp->cylinders == 0) {
2680 dp->heads = ccg.heads;
2681 dp->secs_per_track = ccg.secs_per_track;
2682 dp->cylinders = ccg.cylinders;
2687 dasendorderedtag(void *arg)
2689 struct da_softc *softc = arg;
2691 if (da_send_ordered) {
2692 if ((softc->ordered_tag_count == 0)
2693 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
2694 softc->flags |= DA_FLAG_NEED_OTAG;
2696 if (softc->outstanding_cmds > 0)
2697 softc->flags &= ~DA_FLAG_WENT_IDLE;
2699 softc->ordered_tag_count = 0;
2701 /* Queue us up again */
2702 callout_reset(&softc->sendordered_c,
2703 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2704 dasendorderedtag, softc);
2708 * Step through all DA peripheral drivers, and if the device is still open,
2709 * sync the disk cache to physical media.
2712 dashutdown(void * arg, int howto)
2714 struct cam_periph *periph;
2715 struct da_softc *softc;
2718 TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
2721 cam_periph_lock(periph);
2722 softc = (struct da_softc *)periph->softc;
2725 * We only sync the cache if the drive is still open, and
2726 * if the drive is capable of it..
2728 if (((softc->flags & DA_FLAG_OPEN) == 0)
2729 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
2730 cam_periph_unlock(periph);
2734 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2736 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
2737 scsi_synchronize_cache(&ccb.csio,
2741 /*begin_lba*/0, /* whole disk */
2746 xpt_polled_action(&ccb);
2748 error = cam_periph_error(&ccb,
2749 0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL);
2750 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
2751 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
2752 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
2754 xpt_print(periph->path, "Synchronize cache failed\n");
2755 cam_periph_unlock(periph);
2759 #else /* !_KERNEL */
2762 * XXX This is only left out of the kernel build to silence warnings. If,
2763 * for some reason this function is used in the kernel, the ifdefs should
2764 * be moved so it is included both in the kernel and userland.
2767 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2768 void (*cbfcnp)(struct cam_periph *, union ccb *),
2769 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2770 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2773 struct scsi_format_unit *scsi_cmd;
2775 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2776 scsi_cmd->opcode = FORMAT_UNIT;
2777 scsi_cmd->byte2 = byte2;
2778 scsi_ulto2b(ileave, scsi_cmd->interleave);
2783 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2792 #endif /* _KERNEL */