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
90 DA_Q_NO_SYNC_CACHE = 0x01,
91 DA_Q_NO_6_BYTE = 0x02,
92 DA_Q_NO_PREVENT = 0x04
98 DA_CCB_BUFFER_IO = 0x03,
99 DA_CCB_WAITING = 0x04,
101 DA_CCB_TYPE_MASK = 0x0F,
102 DA_CCB_RETRY_UA = 0x10
105 /* Offsets into our private area for storing information */
106 #define ccb_state ppriv_field0
107 #define ccb_bp ppriv_ptr1
112 u_int8_t secs_per_track;
113 u_int32_t secsize; /* Number of bytes/sector */
114 u_int64_t sectors; /* total number sectors */
118 struct bio_queue_head bio_queue;
119 SLIST_ENTRY(da_softc) links;
120 LIST_HEAD(, ccb_hdr) pending_ccbs;
124 int minimum_cmd_size;
125 int ordered_tag_count;
126 int outstanding_cmds;
127 struct disk_params params;
130 struct task sysctl_task;
131 struct sysctl_ctx_list sysctl_ctx;
132 struct sysctl_oid *sysctl_tree;
133 struct callout sendordered_c;
137 struct da_quirk_entry {
138 struct scsi_inquiry_pattern inq_pat;
142 static const char quantum[] = "QUANTUM";
143 static const char microp[] = "MICROP";
145 static struct da_quirk_entry da_quirk_table[] =
147 /* SPI, FC devices */
150 * Fujitsu M2513A MO drives.
151 * Tested devices: M2513A2 firmware versions 1200 & 1300.
152 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
153 * Reported by: W.Scholten <whs@xs4all.nl>
155 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
156 /*quirks*/ DA_Q_NO_SYNC_CACHE
160 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
161 /*quirks*/ DA_Q_NO_SYNC_CACHE
165 * This particular Fujitsu drive doesn't like the
166 * synchronize cache command.
167 * Reported by: Tom Jackson <toj@gorilla.net>
169 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
170 /*quirks*/ DA_Q_NO_SYNC_CACHE
174 * This drive doesn't like the synchronize cache command
175 * either. Reported by: Matthew Jacob <mjacob@feral.com>
176 * in NetBSD PR kern/6027, August 24, 1998.
178 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
179 /*quirks*/ DA_Q_NO_SYNC_CACHE
183 * This drive doesn't like the synchronize cache command
184 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
187 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
188 /*quirks*/ DA_Q_NO_SYNC_CACHE
192 * Doesn't like the synchronize cache command.
193 * Reported by: Blaz Zupan <blaz@gold.amis.net>
195 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
196 /*quirks*/ DA_Q_NO_SYNC_CACHE
200 * Doesn't like the synchronize cache command.
201 * Reported by: Blaz Zupan <blaz@gold.amis.net>
203 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
204 /*quirks*/ DA_Q_NO_SYNC_CACHE
208 * Doesn't like the synchronize cache command.
210 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
211 /*quirks*/ DA_Q_NO_SYNC_CACHE
215 * Doesn't like the synchronize cache command.
216 * Reported by: walter@pelissero.de
218 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
219 /*quirks*/ DA_Q_NO_SYNC_CACHE
223 * Doesn't work correctly with 6 byte reads/writes.
224 * Returns illegal request, and points to byte 9 of the
226 * Reported by: Adam McDougall <bsdx@spawnet.com>
228 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
229 /*quirks*/ DA_Q_NO_6_BYTE
233 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
234 /*quirks*/ DA_Q_NO_6_BYTE
238 * Doesn't like the synchronize cache command.
239 * Reported by: walter@pelissero.de
241 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
242 /*quirks*/ DA_Q_NO_SYNC_CACHE
246 * The CISS RAID controllers do not support SYNC_CACHE
248 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
249 /*quirks*/ DA_Q_NO_SYNC_CACHE
251 /* USB mass storage devices supported by umass(4) */
254 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
257 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
258 /*quirks*/ DA_Q_NO_SYNC_CACHE
262 * Power Quotient Int. (PQI) USB flash key
265 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
266 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
270 * Creative Nomad MUVO mp3 player (USB)
273 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
274 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
278 * Jungsoft NEXDISK USB flash key
281 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
282 /*quirks*/ DA_Q_NO_SYNC_CACHE
286 * FreeDik USB Mini Data Drive
289 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
290 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
294 * Sigmatel USB Flash MP3 Player
297 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
298 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
302 * Neuros USB Digital Audio Computer
305 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
306 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
310 * SEAGRAND NP-900 MP3 Player
313 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
314 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
318 * iRiver iFP MP3 player (with UMS Firmware)
319 * PR: kern/54881, i386/63941, kern/66124
321 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
322 /*quirks*/ DA_Q_NO_SYNC_CACHE
326 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
329 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
330 /*quirks*/ DA_Q_NO_SYNC_CACHE
334 * ZICPlay USB MP3 Player with FM
337 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
338 /*quirks*/ DA_Q_NO_SYNC_CACHE
342 * TEAC USB floppy mechanisms
344 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
345 /*quirks*/ DA_Q_NO_SYNC_CACHE
349 * Kingston DataTraveler II+ USB Pen-Drive.
350 * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
352 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
353 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
357 * Motorola E398 Mobile Phone (TransFlash memory card).
358 * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
361 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
362 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
366 * Qware BeatZkey! Pro
369 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
370 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
374 * Time DPA20B 1GB MP3 Player
377 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
378 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
382 * Samsung USB key 128Mb
385 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
386 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
390 * Kingston DataTraveler 2.0 USB Flash memory.
393 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
394 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
398 * Creative MUVO Slim mp3 player (USB)
401 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
402 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
406 * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
409 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
410 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
414 * SanDisk Micro Cruzer 128MB
417 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
418 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
422 * TOSHIBA TransMemory USB sticks
425 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
426 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
431 * PR: usb/75578, usb/72344, usb/65436
433 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
434 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
438 * Genesys 6-in-1 Card Reader
441 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
442 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
446 * Rekam Digital CAMERA
449 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
450 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
454 * iRiver H10 MP3 player
457 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
458 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
462 * iRiver U10 MP3 player
465 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
466 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
473 {T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
474 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
478 * EasyMP3 EM732X USB 2.0 Flash MP3 Player
481 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
482 "1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
489 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
490 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
494 * Philips USB Key Audio KEY013
497 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
498 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
505 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
506 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
513 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
514 /*quirks*/ DA_Q_NO_SYNC_CACHE
518 * I/O Magic USB flash - Giga Bank
521 {T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
522 /*quirks*/ DA_Q_NO_SYNC_CACHE
526 * JoyFly 128mb USB Flash Drive
529 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
530 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
537 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
538 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
542 * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A
545 {T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*",
546 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
550 * Samsung YP-U3 mp3-player
553 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3",
554 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
557 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
558 "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
562 * Sony Cyber-Shot DSC cameras
565 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
566 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
570 static disk_strategy_t dastrategy;
571 static dumper_t dadump;
572 static periph_init_t dainit;
573 static void daasync(void *callback_arg, u_int32_t code,
574 struct cam_path *path, void *arg);
575 static void dasysctlinit(void *context, int pending);
576 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
577 static periph_ctor_t daregister;
578 static periph_dtor_t dacleanup;
579 static periph_start_t dastart;
580 static periph_oninv_t daoninvalidate;
581 static void dadone(struct cam_periph *periph,
582 union ccb *done_ccb);
583 static int daerror(union ccb *ccb, u_int32_t cam_flags,
584 u_int32_t sense_flags);
585 static void daprevent(struct cam_periph *periph, int action);
586 static int dagetcapacity(struct cam_periph *periph);
587 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
589 static timeout_t dasendorderedtag;
590 static void dashutdown(void *arg, int howto);
592 #ifndef DA_DEFAULT_TIMEOUT
593 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
596 #ifndef DA_DEFAULT_RETRY
597 #define DA_DEFAULT_RETRY 4
600 #ifndef DA_DEFAULT_SEND_ORDERED
601 #define DA_DEFAULT_SEND_ORDERED 1
605 static int da_retry_count = DA_DEFAULT_RETRY;
606 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
607 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
609 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
610 "CAM Direct Access Disk driver");
611 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
612 &da_retry_count, 0, "Normal I/O retry count");
613 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
614 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
615 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
616 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
617 SYSCTL_INT(_kern_cam_da, OID_AUTO, da_send_ordered, CTLFLAG_RW,
618 &da_send_ordered, 0, "Send Ordered Tags");
619 TUNABLE_INT("kern.cam.da.da_send_ordered", &da_send_ordered);
622 * DA_ORDEREDTAG_INTERVAL determines how often, relative
623 * to the default timeout, we check to see whether an ordered
624 * tagged transaction is appropriate to prevent simple tag
625 * starvation. Since we'd like to ensure that there is at least
626 * 1/2 of the timeout length left for a starved transaction to
627 * complete after we've sent an ordered tag, we must poll at least
628 * four times in every timeout period. This takes care of the worst
629 * case where a starved transaction starts during an interval that
630 * meets the requirement "don't send an ordered tag" test so it takes
631 * us two intervals to determine that a tag must be sent.
633 #ifndef DA_ORDEREDTAG_INTERVAL
634 #define DA_ORDEREDTAG_INTERVAL 4
637 static struct periph_driver dadriver =
640 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
643 PERIPHDRIVER_DECLARE(da, dadriver);
645 MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
648 daopen(struct disk *dp)
650 struct cam_periph *periph;
651 struct da_softc *softc;
655 periph = (struct cam_periph *)dp->d_drv1;
656 if (periph == NULL) {
660 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
664 cam_periph_lock(periph);
665 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
666 cam_periph_unlock(periph);
667 cam_periph_release(periph);
671 unit = periph->unit_number;
672 softc = (struct da_softc *)periph->softc;
673 softc->flags |= DA_FLAG_OPEN;
675 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
676 ("daopen: disk=%s%d (unit %d)\n", dp->d_name, dp->d_unit,
679 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
680 /* Invalidate our pack information. */
681 softc->flags &= ~DA_FLAG_PACK_INVALID;
684 error = dagetcapacity(periph);
688 softc->disk->d_sectorsize = softc->params.secsize;
689 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
690 /* XXX: these are not actually "firmware" values, so they may be wrong */
691 softc->disk->d_fwsectors = softc->params.secs_per_track;
692 softc->disk->d_fwheads = softc->params.heads;
693 softc->disk->d_devstat->block_size = softc->params.secsize;
694 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
696 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
697 (softc->quirks & DA_Q_NO_PREVENT) == 0)
698 daprevent(periph, PR_PREVENT);
700 softc->flags &= ~DA_FLAG_OPEN;
702 cam_periph_unhold(periph);
703 cam_periph_unlock(periph);
706 cam_periph_release(periph);
712 daclose(struct disk *dp)
714 struct cam_periph *periph;
715 struct da_softc *softc;
718 periph = (struct cam_periph *)dp->d_drv1;
722 cam_periph_lock(periph);
723 if ((error = cam_periph_hold(periph, PRIBIO)) != 0) {
724 cam_periph_unlock(periph);
725 cam_periph_release(periph);
729 softc = (struct da_softc *)periph->softc;
731 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0
732 && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
735 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
737 scsi_synchronize_cache(&ccb->csio,
741 /*begin_lba*/0,/* Cover the whole disk */
746 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
747 /*sense_flags*/SF_RETRY_UA,
748 softc->disk->d_devstat);
750 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
751 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
752 CAM_SCSI_STATUS_ERROR) {
754 int sense_key, error_code;
756 scsi_extract_sense(&ccb->csio.sense_data,
760 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
761 scsi_sense_print(&ccb->csio);
763 xpt_print(periph->path, "Synchronize cache "
764 "failed, status == 0x%x, scsi status == "
765 "0x%x\n", ccb->csio.ccb_h.status,
766 ccb->csio.scsi_status);
770 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
771 cam_release_devq(ccb->ccb_h.path,
777 xpt_release_ccb(ccb);
781 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
782 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
783 daprevent(periph, PR_ALLOW);
785 * If we've got removeable media, mark the blocksize as
786 * unavailable, since it could change when new media is
789 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
792 softc->flags &= ~DA_FLAG_OPEN;
793 cam_periph_unhold(periph);
794 cam_periph_unlock(periph);
795 cam_periph_release(periph);
800 * Actually translate the requested transfer into one the physical driver
801 * can understand. The transfer is described by a buf and will include
802 * only one physical transfer.
805 dastrategy(struct bio *bp)
807 struct cam_periph *periph;
808 struct da_softc *softc;
810 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
811 if (periph == NULL) {
812 biofinish(bp, NULL, ENXIO);
815 softc = (struct da_softc *)periph->softc;
817 cam_periph_lock(periph);
820 * If the device has been made invalid, error out
822 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
823 cam_periph_unlock(periph);
824 biofinish(bp, NULL, ENXIO);
829 * Place it in the queue of disk activities for this disk
831 bioq_disksort(&softc->bio_queue, bp);
834 * Schedule ourselves for performing the work.
836 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
837 cam_periph_unlock(periph);
843 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
845 struct cam_periph *periph;
846 struct da_softc *softc;
848 struct ccb_scsiio csio;
855 softc = (struct da_softc *)periph->softc;
856 cam_periph_lock(periph);
857 secsize = softc->params.secsize;
859 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
860 cam_periph_unlock(periph);
865 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
866 csio.ccb_h.ccb_state = DA_CCB_DUMP;
867 scsi_read_write(&csio,
873 /*minimum_cmd_size*/ softc->minimum_cmd_size,
876 /*data_ptr*/(u_int8_t *) virtual,
878 /*sense_len*/SSD_FULL_SIZE,
879 DA_DEFAULT_TIMEOUT * 1000);
880 xpt_polled_action((union ccb *)&csio);
882 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
883 printf("Aborting dump due to I/O error.\n");
884 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
885 CAM_SCSI_STATUS_ERROR)
886 scsi_sense_print(&csio);
888 printf("status == 0x%x, scsi status == 0x%x\n",
889 csio.ccb_h.status, csio.scsi_status);
892 cam_periph_unlock(periph);
897 * Sync the disk cache contents to the physical media.
899 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
901 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
902 csio.ccb_h.ccb_state = DA_CCB_DUMP;
903 scsi_synchronize_cache(&csio,
907 /*begin_lba*/0,/* Cover the whole disk */
911 xpt_polled_action((union ccb *)&csio);
913 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
914 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
915 CAM_SCSI_STATUS_ERROR) {
917 int sense_key, error_code;
919 scsi_extract_sense(&csio.sense_data,
923 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
924 scsi_sense_print(&csio);
926 xpt_print(periph->path, "Synchronize cache "
927 "failed, status == 0x%x, scsi status == "
928 "0x%x\n", csio.ccb_h.status,
933 cam_periph_unlock(periph);
938 dagetattr(struct bio *bp)
941 struct cam_periph *periph;
943 if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
945 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
946 if (periph->path == NULL)
949 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
952 bp->bio_completed = bp->bio_length;
962 * Install a global async callback. This callback will
963 * receive async callbacks like "new device found".
965 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
967 if (status != CAM_REQ_CMP) {
968 printf("da: Failed to attach master async callback "
969 "due to status 0x%x!\n", status);
970 } else if (da_send_ordered) {
972 /* Register our shutdown event handler */
973 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
974 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
975 printf("dainit: shutdown event registration failed!\n");
980 daoninvalidate(struct cam_periph *periph)
982 struct da_softc *softc;
984 softc = (struct da_softc *)periph->softc;
987 * De-register any async callbacks.
989 xpt_register_async(0, daasync, periph, periph->path);
991 softc->flags |= DA_FLAG_PACK_INVALID;
994 * Return all queued I/O with ENXIO.
995 * XXX Handle any transactions queued to the card
996 * with XPT_ABORT_CCB.
998 bioq_flush(&softc->bio_queue, NULL, ENXIO);
1000 disk_gone(softc->disk);
1001 xpt_print(periph->path, "lost device - %d outstanding\n",
1002 softc->outstanding_cmds);
1006 dacleanup(struct cam_periph *periph)
1008 struct da_softc *softc;
1010 softc = (struct da_softc *)periph->softc;
1012 xpt_print(periph->path, "removing device entry\n");
1013 cam_periph_unlock(periph);
1016 * If we can't free the sysctl tree, oh well...
1018 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1019 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1020 xpt_print(periph->path, "can't remove sysctl context\n");
1023 disk_destroy(softc->disk);
1024 callout_drain(&softc->sendordered_c);
1025 free(softc, M_DEVBUF);
1026 cam_periph_lock(periph);
1030 daasync(void *callback_arg, u_int32_t code,
1031 struct cam_path *path, void *arg)
1033 struct cam_periph *periph;
1035 periph = (struct cam_periph *)callback_arg;
1037 case AC_FOUND_DEVICE:
1039 struct ccb_getdev *cgd;
1042 cgd = (struct ccb_getdev *)arg;
1046 if (cgd->protocol != PROTO_SCSI)
1049 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1050 && SID_TYPE(&cgd->inq_data) != T_RBC
1051 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1055 * Allocate a peripheral instance for
1056 * this device and start the probe
1059 status = cam_periph_alloc(daregister, daoninvalidate,
1061 "da", CAM_PERIPH_BIO,
1062 cgd->ccb_h.path, daasync,
1063 AC_FOUND_DEVICE, cgd);
1065 if (status != CAM_REQ_CMP
1066 && status != CAM_REQ_INPROG)
1067 printf("daasync: Unable to attach to new device "
1068 "due to status 0x%x\n", status);
1071 case AC_ADVINFO_CHANGED:
1075 buftype = (uintptr_t)arg;
1076 if (buftype == CDAI_TYPE_PHYS_PATH) {
1077 struct da_softc *softc;
1079 softc = periph->softc;
1080 disk_attr_changed(softc->disk, "GEOM::physpath",
1088 struct da_softc *softc;
1089 struct ccb_hdr *ccbh;
1091 softc = (struct da_softc *)periph->softc;
1093 * Don't fail on the expected unit attention
1096 softc->flags |= DA_FLAG_RETRY_UA;
1097 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1098 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1104 cam_periph_async(periph, code, path, arg);
1108 dasysctlinit(void *context, int pending)
1110 struct cam_periph *periph;
1111 struct da_softc *softc;
1112 char tmpstr[80], tmpstr2[80];
1113 struct ccb_trans_settings cts;
1115 periph = (struct cam_periph *)context;
1117 * periph was held for us when this task was enqueued
1119 if (periph->flags & CAM_PERIPH_INVALID) {
1120 cam_periph_release(periph);
1124 softc = (struct da_softc *)periph->softc;
1125 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1126 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1128 sysctl_ctx_init(&softc->sysctl_ctx);
1129 softc->flags |= DA_FLAG_SCTX_INIT;
1130 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1131 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1132 CTLFLAG_RD, 0, tmpstr);
1133 if (softc->sysctl_tree == NULL) {
1134 printf("dasysctlinit: unable to allocate sysctl tree\n");
1135 cam_periph_release(periph);
1140 * Now register the sysctl handler, so the user can change the value on
1143 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1144 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1145 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1146 "Minimum CDB size");
1149 * Add some addressing info.
1151 memset(&cts, 0, sizeof (cts));
1152 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
1153 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1154 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1155 cam_periph_lock(periph);
1156 xpt_action((union ccb *)&cts);
1157 cam_periph_unlock(periph);
1158 if (cts.ccb_h.status != CAM_REQ_CMP) {
1159 cam_periph_release(periph);
1162 if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
1163 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1164 if (fc->valid & CTS_FC_VALID_WWPN) {
1165 softc->wwpn = fc->wwpn;
1166 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1167 SYSCTL_CHILDREN(softc->sysctl_tree),
1168 OID_AUTO, "wwpn", CTLFLAG_RD,
1169 &softc->wwpn, "World Wide Port Name");
1172 cam_periph_release(periph);
1176 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1180 value = *(int *)arg1;
1182 error = sysctl_handle_int(oidp, &value, 0, req);
1185 || (req->newptr == NULL))
1189 * Acceptable values here are 6, 10, 12 or 16.
1193 else if ((value > 6)
1196 else if ((value > 10)
1199 else if (value > 12)
1202 *(int *)arg1 = value;
1208 daregister(struct cam_periph *periph, void *arg)
1210 struct da_softc *softc;
1211 struct ccb_pathinq cpi;
1212 struct ccb_getdev *cgd;
1216 cgd = (struct ccb_getdev *)arg;
1217 if (periph == NULL) {
1218 printf("daregister: periph was NULL!!\n");
1219 return(CAM_REQ_CMP_ERR);
1223 printf("daregister: no getdev CCB, can't register device\n");
1224 return(CAM_REQ_CMP_ERR);
1227 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
1230 if (softc == NULL) {
1231 printf("daregister: Unable to probe new device. "
1232 "Unable to allocate softc\n");
1233 return(CAM_REQ_CMP_ERR);
1236 LIST_INIT(&softc->pending_ccbs);
1237 softc->state = DA_STATE_PROBE;
1238 bioq_init(&softc->bio_queue);
1239 if (SID_IS_REMOVABLE(&cgd->inq_data))
1240 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1241 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1242 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1244 periph->softc = softc;
1247 * See if this device has any quirks.
1249 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1250 (caddr_t)da_quirk_table,
1251 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1252 sizeof(*da_quirk_table), scsi_inquiry_match);
1255 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1257 softc->quirks = DA_Q_NONE;
1259 /* Check if the SIM does not want 6 byte commands */
1260 bzero(&cpi, sizeof(cpi));
1261 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1262 cpi.ccb_h.func_code = XPT_PATH_INQ;
1263 xpt_action((union ccb *)&cpi);
1264 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1265 softc->quirks |= DA_Q_NO_6_BYTE;
1267 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1270 * Take an exclusive refcount on the periph while dastart is called
1271 * to finish the probe. The reference will be dropped in dadone at
1274 (void)cam_periph_hold(periph, PRIBIO);
1277 * Schedule a periodic event to occasionally send an
1278 * ordered tag to a device.
1280 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
1281 callout_reset(&softc->sendordered_c,
1282 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
1283 dasendorderedtag, softc);
1285 mtx_unlock(periph->sim->mtx);
1287 * RBC devices don't have to support READ(6), only READ(10).
1289 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1290 softc->minimum_cmd_size = 10;
1292 softc->minimum_cmd_size = 6;
1295 * Load the user's default, if any.
1297 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1298 periph->unit_number);
1299 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1302 * 6, 10, 12 and 16 are the currently permissible values.
1304 if (softc->minimum_cmd_size < 6)
1305 softc->minimum_cmd_size = 6;
1306 else if ((softc->minimum_cmd_size > 6)
1307 && (softc->minimum_cmd_size <= 10))
1308 softc->minimum_cmd_size = 10;
1309 else if ((softc->minimum_cmd_size > 10)
1310 && (softc->minimum_cmd_size <= 12))
1311 softc->minimum_cmd_size = 12;
1312 else if (softc->minimum_cmd_size > 12)
1313 softc->minimum_cmd_size = 16;
1316 * Register this media as a disk.
1318 softc->disk = disk_alloc();
1319 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
1320 periph->unit_number, 0,
1321 DEVSTAT_BS_UNAVAILABLE,
1322 SID_TYPE(&cgd->inq_data) |
1323 XPORT_DEVSTAT_TYPE(cpi.transport),
1324 DEVSTAT_PRIORITY_DISK);
1325 softc->disk->d_open = daopen;
1326 softc->disk->d_close = daclose;
1327 softc->disk->d_strategy = dastrategy;
1328 softc->disk->d_dump = dadump;
1329 softc->disk->d_getattr = dagetattr;
1330 softc->disk->d_name = "da";
1331 softc->disk->d_drv1 = periph;
1333 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */
1334 else if (cpi.maxio > MAXPHYS)
1335 softc->disk->d_maxsize = MAXPHYS; /* for safety */
1337 softc->disk->d_maxsize = cpi.maxio;
1338 softc->disk->d_unit = periph->unit_number;
1339 softc->disk->d_flags = 0;
1340 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
1341 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
1342 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
1343 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
1344 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
1345 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
1346 cgd->inq_data.product, sizeof(cgd->inq_data.product),
1347 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
1348 softc->disk->d_hba_vendor = cpi.hba_vendor;
1349 softc->disk->d_hba_device = cpi.hba_device;
1350 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
1351 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
1352 disk_create(softc->disk, DISK_VERSION);
1353 mtx_lock(periph->sim->mtx);
1356 * Add async callbacks for events of interest.
1357 * I don't bother checking if this fails as,
1358 * in most cases, the system will function just
1359 * fine without them and the only alternative
1360 * would be to not attach the device on failure.
1362 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET
1363 | AC_LOST_DEVICE | AC_ADVINFO_CHANGED,
1364 daasync, periph, periph->path);
1367 * Emit an attribute changed notification just in case
1368 * physical path information arrived before our async
1369 * event handler was registered, but after anyone attaching
1370 * to our disk device polled it.
1372 disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
1374 xpt_schedule(periph, CAM_PRIORITY_DEV);
1376 return(CAM_REQ_CMP);
1380 dastart(struct cam_periph *periph, union ccb *start_ccb)
1382 struct da_softc *softc;
1384 softc = (struct da_softc *)periph->softc;
1386 switch (softc->state) {
1387 case DA_STATE_NORMAL:
1389 /* Pull a buffer from the queue and get going on it */
1393 * See if there is a buf with work for us to do..
1395 bp = bioq_first(&softc->bio_queue);
1396 if (periph->immediate_priority <= periph->pinfo.priority) {
1397 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1398 ("queuing for immediate ccb\n"));
1399 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1400 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1402 periph->immediate_priority = CAM_PRIORITY_NONE;
1403 wakeup(&periph->ccb_list);
1404 } else if (bp == NULL) {
1405 xpt_release_ccb(start_ccb);
1409 bioq_remove(&softc->bio_queue, bp);
1411 if ((bp->bio_flags & BIO_ORDERED) != 0
1412 || (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1413 softc->flags &= ~DA_FLAG_NEED_OTAG;
1414 softc->ordered_tag_count++;
1415 tag_code = MSG_ORDERED_Q_TAG;
1417 tag_code = MSG_SIMPLE_Q_TAG;
1419 switch (bp->bio_cmd) {
1422 scsi_read_write(&start_ccb->csio,
1423 /*retries*/da_retry_count,
1425 /*tag_action*/tag_code,
1426 /*read_op*/bp->bio_cmd
1429 softc->minimum_cmd_size,
1430 /*lba*/bp->bio_pblkno,
1431 /*block_count*/bp->bio_bcount /
1432 softc->params.secsize,
1433 /*data_ptr*/ bp->bio_data,
1434 /*dxfer_len*/ bp->bio_bcount,
1435 /*sense_len*/SSD_FULL_SIZE,
1436 da_default_timeout * 1000);
1440 * BIO_FLUSH doesn't currently communicate
1441 * range data, so we synchronize the cache
1442 * over the whole disk. We also force
1443 * ordered tag semantics the flush applies
1444 * to all previously queued I/O.
1446 scsi_synchronize_cache(&start_ccb->csio,
1453 da_default_timeout*1000);
1456 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1459 * Block out any asyncronous callbacks
1460 * while we touch the pending ccb list.
1462 LIST_INSERT_HEAD(&softc->pending_ccbs,
1463 &start_ccb->ccb_h, periph_links.le);
1464 softc->outstanding_cmds++;
1466 /* We expect a unit attention from this device */
1467 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1468 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1469 softc->flags &= ~DA_FLAG_RETRY_UA;
1472 start_ccb->ccb_h.ccb_bp = bp;
1473 bp = bioq_first(&softc->bio_queue);
1475 xpt_action(start_ccb);
1479 /* Have more work to do, so ensure we stay scheduled */
1480 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1484 case DA_STATE_PROBE:
1486 struct ccb_scsiio *csio;
1487 struct scsi_read_capacity_data *rcap;
1489 rcap = (struct scsi_read_capacity_data *)
1490 malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
1492 printf("dastart: Couldn't malloc read_capacity data\n");
1493 /* da_free_periph??? */
1496 csio = &start_ccb->csio;
1497 scsi_read_capacity(csio,
1504 start_ccb->ccb_h.ccb_bp = NULL;
1505 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1506 xpt_action(start_ccb);
1509 case DA_STATE_PROBE2:
1511 struct ccb_scsiio *csio;
1512 struct scsi_read_capacity_data_long *rcaplong;
1514 rcaplong = (struct scsi_read_capacity_data_long *)
1515 malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
1516 if (rcaplong == NULL) {
1517 printf("dastart: Couldn't malloc read_capacity data\n");
1518 /* da_free_periph??? */
1521 csio = &start_ccb->csio;
1522 scsi_read_capacity_16(csio,
1525 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1530 /*sense_len*/ SSD_FULL_SIZE,
1532 start_ccb->ccb_h.ccb_bp = NULL;
1533 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1534 xpt_action(start_ccb);
1541 cmd6workaround(union ccb *ccb)
1543 struct scsi_rw_6 cmd6;
1544 struct scsi_rw_10 *cmd10;
1545 struct da_softc *softc;
1549 cdb = ccb->csio.cdb_io.cdb_bytes;
1551 /* Translation only possible if CDB is an array and cmd is R/W6 */
1552 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1553 (*cdb != READ_6 && *cdb != WRITE_6))
1556 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
1557 "increasing minimum_cmd_size to 10.\n");
1558 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1559 softc->minimum_cmd_size = 10;
1561 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1562 cmd10 = (struct scsi_rw_10 *)cdb;
1563 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1565 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1566 cmd10->reserved = 0;
1567 scsi_ulto2b(cmd6.length, cmd10->length);
1568 cmd10->control = cmd6.control;
1569 ccb->csio.cdb_len = sizeof(*cmd10);
1571 /* Requeue request, unfreezing queue if necessary */
1572 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1573 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1576 cam_release_devq(ccb->ccb_h.path,
1580 /*getcount_only*/0);
1586 dadone(struct cam_periph *periph, union ccb *done_ccb)
1588 struct da_softc *softc;
1589 struct ccb_scsiio *csio;
1592 softc = (struct da_softc *)periph->softc;
1593 priority = done_ccb->ccb_h.pinfo.priority;
1594 csio = &done_ccb->csio;
1595 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1596 case DA_CCB_BUFFER_IO:
1600 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1601 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1605 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1610 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1611 if (error == ERESTART) {
1613 * A retry was scheuled, so
1622 * return all queued I/O with EIO, so that
1623 * the client can retry these I/Os in the
1624 * proper order should it attempt to recover.
1629 && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
1631 * Catastrophic error. Mark our pack as
1635 * XXX See if this is really a media
1638 xpt_print(periph->path,
1639 "Invalidating pack\n");
1640 softc->flags |= DA_FLAG_PACK_INVALID;
1641 queued_error = ENXIO;
1643 bioq_flush(&softc->bio_queue, NULL,
1645 bp->bio_error = error;
1646 bp->bio_resid = bp->bio_bcount;
1647 bp->bio_flags |= BIO_ERROR;
1649 bp->bio_resid = csio->resid;
1651 if (bp->bio_resid != 0)
1652 bp->bio_flags |= BIO_ERROR;
1654 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1655 cam_release_devq(done_ccb->ccb_h.path,
1659 /*getcount_only*/0);
1661 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1662 panic("REQ_CMP with QFRZN");
1663 bp->bio_resid = csio->resid;
1664 if (csio->resid > 0)
1665 bp->bio_flags |= BIO_ERROR;
1669 * Block out any asyncronous callbacks
1670 * while we touch the pending ccb list.
1672 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1673 softc->outstanding_cmds--;
1674 if (softc->outstanding_cmds == 0)
1675 softc->flags |= DA_FLAG_WENT_IDLE;
1677 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1678 xpt_print(periph->path, "oustanding %d\n",
1679 softc->outstanding_cmds);
1688 struct scsi_read_capacity_data *rdcap;
1689 struct scsi_read_capacity_data_long *rcaplong;
1690 char announce_buf[80];
1694 if (softc->state == DA_STATE_PROBE)
1695 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1697 rcaplong = (struct scsi_read_capacity_data_long *)
1700 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1701 struct disk_params *dp;
1702 uint32_t block_size;
1705 if (softc->state == DA_STATE_PROBE) {
1706 block_size = scsi_4btoul(rdcap->length);
1707 maxsector = scsi_4btoul(rdcap->addr);
1710 * According to SBC-2, if the standard 10
1711 * byte READ CAPACITY command returns 2^32,
1712 * we should issue the 16 byte version of
1713 * the command, since the device in question
1714 * has more sectors than can be represented
1715 * with the short version of the command.
1717 if (maxsector == 0xffffffff) {
1718 softc->state = DA_STATE_PROBE2;
1719 free(rdcap, M_SCSIDA);
1720 xpt_release_ccb(done_ccb);
1721 xpt_schedule(periph, priority);
1725 block_size = scsi_4btoul(rcaplong->length);
1726 maxsector = scsi_8btou64(rcaplong->addr);
1730 * Because GEOM code just will panic us if we
1731 * give them an 'illegal' value we'll avoid that
1734 if (block_size == 0 && maxsector == 0) {
1735 snprintf(announce_buf, sizeof(announce_buf),
1737 } else if (block_size >= MAXPHYS || block_size == 0) {
1738 xpt_print(periph->path,
1739 "unsupportable block size %ju\n",
1740 (uintmax_t) block_size);
1741 announce_buf[0] = '\0';
1742 cam_periph_invalidate(periph);
1744 dasetgeom(periph, block_size, maxsector);
1745 dp = &softc->params;
1746 snprintf(announce_buf, sizeof(announce_buf),
1747 "%juMB (%ju %u byte sectors: %dH %dS/T "
1749 (((uintmax_t)dp->secsize *
1750 dp->sectors) / (1024*1024)),
1751 (uintmax_t)dp->sectors,
1752 dp->secsize, dp->heads,
1753 dp->secs_per_track, dp->cylinders);
1758 announce_buf[0] = '\0';
1761 * Retry any UNIT ATTENTION type errors. They
1762 * are expected at boot.
1764 error = daerror(done_ccb, CAM_RETRY_SELTO,
1765 SF_RETRY_UA|SF_NO_PRINT);
1766 if (error == ERESTART) {
1768 * A retry was scheuled, so
1772 } else if (error != 0) {
1773 struct scsi_sense_data *sense;
1775 int sense_key, error_code;
1778 struct ccb_getdev cgd;
1780 /* Don't wedge this device's queue */
1781 status = done_ccb->ccb_h.status;
1782 if ((status & CAM_DEV_QFRZN) != 0)
1783 cam_release_devq(done_ccb->ccb_h.path,
1787 /*getcount_only*/0);
1790 xpt_setup_ccb(&cgd.ccb_h,
1791 done_ccb->ccb_h.path,
1792 CAM_PRIORITY_NORMAL);
1793 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1794 xpt_action((union ccb *)&cgd);
1796 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1797 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1798 || ((status & CAM_AUTOSNS_VALID) == 0))
1804 sense = &csio->sense_data;
1805 scsi_extract_sense(sense, &error_code,
1810 * Attach to anything that claims to be a
1811 * direct access or optical disk device,
1812 * as long as it doesn't return a "Logical
1813 * unit not supported" (0x25) error.
1815 if ((have_sense) && (asc != 0x25)
1816 && (error_code == SSD_CURRENT_ERROR)) {
1817 const char *sense_key_desc;
1818 const char *asc_desc;
1820 scsi_sense_desc(sense_key, asc, ascq,
1824 snprintf(announce_buf,
1825 sizeof(announce_buf),
1826 "Attempt to query device "
1827 "size failed: %s, %s",
1835 xpt_print(periph->path,
1836 "got CAM status %#x\n",
1837 done_ccb->ccb_h.status);
1840 xpt_print(periph->path, "fatal error, "
1841 "failed to attach to device\n");
1844 * Free up resources.
1846 cam_periph_invalidate(periph);
1850 free(csio->data_ptr, M_SCSIDA);
1851 if (announce_buf[0] != '\0') {
1852 xpt_announce_periph(periph, announce_buf);
1854 * Create our sysctl variables, now that we know
1855 * we have successfully attached.
1857 (void) cam_periph_acquire(periph); /* increase the refcount */
1858 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1860 softc->state = DA_STATE_NORMAL;
1862 * Since our peripheral may be invalidated by an error
1863 * above or an external event, we must release our CCB
1864 * before releasing the probe lock on the peripheral.
1865 * The peripheral will only go away once the last lock
1866 * is removed, and we need it around for the CCB release
1869 xpt_release_ccb(done_ccb);
1870 cam_periph_unhold(periph);
1873 case DA_CCB_WAITING:
1875 /* Caller will release the CCB */
1876 wakeup(&done_ccb->ccb_h.cbfcnp);
1880 /* No-op. We're polling */
1885 xpt_release_ccb(done_ccb);
1889 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1891 struct da_softc *softc;
1892 struct cam_periph *periph;
1895 periph = xpt_path_periph(ccb->ccb_h.path);
1896 softc = (struct da_softc *)periph->softc;
1899 * Automatically detect devices that do not support
1900 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
1903 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
1904 error = cmd6workaround(ccb);
1905 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
1906 CAM_SCSI_STATUS_ERROR)
1907 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
1908 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1909 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
1910 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
1911 int sense_key, error_code, asc, ascq;
1913 scsi_extract_sense(&ccb->csio.sense_data,
1914 &error_code, &sense_key, &asc, &ascq);
1915 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
1916 error = cmd6workaround(ccb);
1918 if (error == ERESTART)
1923 * Until we have a better way of doing pack validation,
1924 * don't treat UAs as errors.
1926 sense_flags |= SF_RETRY_UA;
1927 return(cam_periph_error(ccb, cam_flags, sense_flags,
1928 &softc->saved_ccb));
1932 daprevent(struct cam_periph *periph, int action)
1934 struct da_softc *softc;
1938 softc = (struct da_softc *)periph->softc;
1940 if (((action == PR_ALLOW)
1941 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
1942 || ((action == PR_PREVENT)
1943 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
1947 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1949 scsi_prevent(&ccb->csio,
1957 error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
1958 SF_RETRY_UA, softc->disk->d_devstat);
1961 if (action == PR_ALLOW)
1962 softc->flags &= ~DA_FLAG_PACK_LOCKED;
1964 softc->flags |= DA_FLAG_PACK_LOCKED;
1967 xpt_release_ccb(ccb);
1971 dagetcapacity(struct cam_periph *periph)
1973 struct da_softc *softc;
1975 struct scsi_read_capacity_data *rcap;
1976 struct scsi_read_capacity_data_long *rcaplong;
1980 u_int32_t sense_flags;
1982 softc = (struct da_softc *)periph->softc;
1986 sense_flags = SF_RETRY_UA;
1987 if (softc->flags & DA_FLAG_PACK_REMOVABLE)
1988 sense_flags |= SF_NO_PRINT;
1990 /* Do a read capacity */
1991 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcaplong),
1997 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1998 scsi_read_capacity(&ccb->csio,
2005 ccb->ccb_h.ccb_bp = NULL;
2007 error = cam_periph_runccb(ccb, daerror,
2008 /*cam_flags*/CAM_RETRY_SELTO,
2010 softc->disk->d_devstat);
2012 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2013 cam_release_devq(ccb->ccb_h.path,
2017 /*getcount_only*/0);
2020 block_len = scsi_4btoul(rcap->length);
2021 maxsector = scsi_4btoul(rcap->addr);
2023 if (maxsector != 0xffffffff)
2028 rcaplong = (struct scsi_read_capacity_data_long *)rcap;
2030 scsi_read_capacity_16(&ccb->csio,
2033 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2038 /*sense_len*/ SSD_FULL_SIZE,
2040 ccb->ccb_h.ccb_bp = NULL;
2042 error = cam_periph_runccb(ccb, daerror,
2043 /*cam_flags*/CAM_RETRY_SELTO,
2045 softc->disk->d_devstat);
2047 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2048 cam_release_devq(ccb->ccb_h.path,
2052 /*getcount_only*/0);
2055 block_len = scsi_4btoul(rcaplong->length);
2056 maxsector = scsi_8btou64(rcaplong->addr);
2062 if (block_len >= MAXPHYS || block_len == 0) {
2063 xpt_print(periph->path,
2064 "unsupportable block size %ju\n",
2065 (uintmax_t) block_len);
2068 dasetgeom(periph, block_len, maxsector);
2071 xpt_release_ccb(ccb);
2073 free(rcap, M_SCSIDA);
2079 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector)
2081 struct ccb_calc_geometry ccg;
2082 struct da_softc *softc;
2083 struct disk_params *dp;
2085 softc = (struct da_softc *)periph->softc;
2087 dp = &softc->params;
2088 dp->secsize = block_len;
2089 dp->sectors = maxsector + 1;
2091 * Have the controller provide us with a geometry
2092 * for this disk. The only time the geometry
2093 * matters is when we boot and the controller
2094 * is the only one knowledgeable enough to come
2095 * up with something that will make this a bootable
2098 xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2099 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
2100 ccg.block_size = dp->secsize;
2101 ccg.volume_size = dp->sectors;
2103 ccg.secs_per_track = 0;
2105 xpt_action((union ccb*)&ccg);
2106 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2108 * We don't know what went wrong here- but just pick
2109 * a geometry so we don't have nasty things like divide
2113 dp->secs_per_track = 255;
2114 dp->cylinders = dp->sectors / (255 * 255);
2115 if (dp->cylinders == 0) {
2119 dp->heads = ccg.heads;
2120 dp->secs_per_track = ccg.secs_per_track;
2121 dp->cylinders = ccg.cylinders;
2126 dasendorderedtag(void *arg)
2128 struct da_softc *softc = arg;
2130 if (da_send_ordered) {
2131 if ((softc->ordered_tag_count == 0)
2132 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
2133 softc->flags |= DA_FLAG_NEED_OTAG;
2135 if (softc->outstanding_cmds > 0)
2136 softc->flags &= ~DA_FLAG_WENT_IDLE;
2138 softc->ordered_tag_count = 0;
2140 /* Queue us up again */
2141 callout_reset(&softc->sendordered_c,
2142 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
2143 dasendorderedtag, softc);
2147 * Step through all DA peripheral drivers, and if the device is still open,
2148 * sync the disk cache to physical media.
2151 dashutdown(void * arg, int howto)
2153 struct cam_periph *periph;
2154 struct da_softc *softc;
2156 TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
2159 cam_periph_lock(periph);
2160 softc = (struct da_softc *)periph->softc;
2163 * We only sync the cache if the drive is still open, and
2164 * if the drive is capable of it..
2166 if (((softc->flags & DA_FLAG_OPEN) == 0)
2167 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
2168 cam_periph_unlock(periph);
2172 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2174 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
2175 scsi_synchronize_cache(&ccb.csio,
2179 /*begin_lba*/0, /* whole disk */
2184 xpt_polled_action(&ccb);
2186 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2187 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2188 CAM_SCSI_STATUS_ERROR)
2189 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
2190 int error_code, sense_key, asc, ascq;
2192 scsi_extract_sense(&ccb.csio.sense_data,
2193 &error_code, &sense_key,
2196 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
2197 scsi_sense_print(&ccb.csio);
2199 xpt_print(periph->path, "Synchronize "
2200 "cache failed, status == 0x%x, scsi status "
2201 "== 0x%x\n", ccb.ccb_h.status,
2202 ccb.csio.scsi_status);
2206 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
2207 cam_release_devq(ccb.ccb_h.path,
2211 /*getcount_only*/0);
2212 cam_periph_unlock(periph);
2216 #else /* !_KERNEL */
2219 * XXX This is only left out of the kernel build to silence warnings. If,
2220 * for some reason this function is used in the kernel, the ifdefs should
2221 * be moved so it is included both in the kernel and userland.
2224 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2225 void (*cbfcnp)(struct cam_periph *, union ccb *),
2226 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2227 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2230 struct scsi_format_unit *scsi_cmd;
2232 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2233 scsi_cmd->opcode = FORMAT_UNIT;
2234 scsi_cmd->byte2 = byte2;
2235 scsi_ulto2b(ileave, scsi_cmd->interleave);
2240 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2249 #endif /* _KERNEL */