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_disk.h>
56 #include <cam/cam_ccb.h>
57 #include <cam/cam_periph.h>
58 #include <cam/cam_xpt_periph.h>
59 #include <cam/cam_sim.h>
61 #include <cam/scsi/scsi_message.h>
64 #include <cam/scsi/scsi_da.h>
75 DA_FLAG_PACK_INVALID = 0x001,
76 DA_FLAG_NEW_PACK = 0x002,
77 DA_FLAG_PACK_LOCKED = 0x004,
78 DA_FLAG_PACK_REMOVABLE = 0x008,
79 DA_FLAG_TAGGED_QUEUING = 0x010,
80 DA_FLAG_NEED_OTAG = 0x020,
81 DA_FLAG_WENT_IDLE = 0x040,
82 DA_FLAG_RETRY_UA = 0x080,
84 DA_FLAG_SCTX_INIT = 0x200
89 DA_Q_NO_SYNC_CACHE = 0x01,
90 DA_Q_NO_6_BYTE = 0x02,
91 DA_Q_NO_PREVENT = 0x04
97 DA_CCB_BUFFER_IO = 0x03,
98 DA_CCB_WAITING = 0x04,
100 DA_CCB_TYPE_MASK = 0x0F,
101 DA_CCB_RETRY_UA = 0x10
104 /* Offsets into our private area for storing information */
105 #define ccb_state ppriv_field0
106 #define ccb_bp ppriv_ptr1
111 u_int8_t secs_per_track;
112 u_int32_t secsize; /* Number of bytes/sector */
113 u_int64_t sectors; /* total number sectors */
117 struct bio_queue_head bio_queue;
118 SLIST_ENTRY(da_softc) links;
119 LIST_HEAD(, ccb_hdr) pending_ccbs;
123 int minimum_cmd_size;
124 int ordered_tag_count;
125 int outstanding_cmds;
126 struct disk_params params;
129 struct task sysctl_task;
130 struct sysctl_ctx_list sysctl_ctx;
131 struct sysctl_oid *sysctl_tree;
132 struct callout sendordered_c;
136 struct da_quirk_entry {
137 struct scsi_inquiry_pattern inq_pat;
141 static const char quantum[] = "QUANTUM";
142 static const char microp[] = "MICROP";
144 static struct da_quirk_entry da_quirk_table[] =
146 /* SPI, FC devices */
149 * Fujitsu M2513A MO drives.
150 * Tested devices: M2513A2 firmware versions 1200 & 1300.
151 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
152 * Reported by: W.Scholten <whs@xs4all.nl>
154 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
155 /*quirks*/ DA_Q_NO_SYNC_CACHE
159 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
160 /*quirks*/ DA_Q_NO_SYNC_CACHE
164 * This particular Fujitsu drive doesn't like the
165 * synchronize cache command.
166 * Reported by: Tom Jackson <toj@gorilla.net>
168 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
169 /*quirks*/ DA_Q_NO_SYNC_CACHE
173 * This drive doesn't like the synchronize cache command
174 * either. Reported by: Matthew Jacob <mjacob@feral.com>
175 * in NetBSD PR kern/6027, August 24, 1998.
177 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
178 /*quirks*/ DA_Q_NO_SYNC_CACHE
182 * This drive doesn't like the synchronize cache command
183 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
186 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
187 /*quirks*/ DA_Q_NO_SYNC_CACHE
191 * Doesn't like the synchronize cache command.
192 * Reported by: Blaz Zupan <blaz@gold.amis.net>
194 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
195 /*quirks*/ DA_Q_NO_SYNC_CACHE
199 * Doesn't like the synchronize cache command.
200 * Reported by: Blaz Zupan <blaz@gold.amis.net>
202 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
203 /*quirks*/ DA_Q_NO_SYNC_CACHE
207 * Doesn't like the synchronize cache command.
209 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
210 /*quirks*/ DA_Q_NO_SYNC_CACHE
214 * Doesn't like the synchronize cache command.
215 * Reported by: walter@pelissero.de
217 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
218 /*quirks*/ DA_Q_NO_SYNC_CACHE
222 * Doesn't work correctly with 6 byte reads/writes.
223 * Returns illegal request, and points to byte 9 of the
225 * Reported by: Adam McDougall <bsdx@spawnet.com>
227 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
228 /*quirks*/ DA_Q_NO_6_BYTE
232 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
233 /*quirks*/ DA_Q_NO_6_BYTE
237 * Doesn't like the synchronize cache command.
238 * Reported by: walter@pelissero.de
240 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
241 /*quirks*/ DA_Q_NO_SYNC_CACHE
245 * The CISS RAID controllers do not support SYNC_CACHE
247 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
248 /*quirks*/ DA_Q_NO_SYNC_CACHE
250 /* USB mass storage devices supported by umass(4) */
253 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
256 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
257 /*quirks*/ DA_Q_NO_SYNC_CACHE
261 * Power Quotient Int. (PQI) USB flash key
264 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
265 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
269 * Creative Nomad MUVO mp3 player (USB)
272 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
273 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
277 * Jungsoft NEXDISK USB flash key
280 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
281 /*quirks*/ DA_Q_NO_SYNC_CACHE
285 * FreeDik USB Mini Data Drive
288 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
289 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
293 * Sigmatel USB Flash MP3 Player
296 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
297 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
301 * Neuros USB Digital Audio Computer
304 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
305 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
309 * SEAGRAND NP-900 MP3 Player
312 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
313 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
317 * iRiver iFP MP3 player (with UMS Firmware)
318 * PR: kern/54881, i386/63941, kern/66124
320 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
321 /*quirks*/ DA_Q_NO_SYNC_CACHE
325 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
328 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
329 /*quirks*/ DA_Q_NO_SYNC_CACHE
333 * ZICPlay USB MP3 Player with FM
336 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
337 /*quirks*/ DA_Q_NO_SYNC_CACHE
341 * TEAC USB floppy mechanisms
343 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
344 /*quirks*/ DA_Q_NO_SYNC_CACHE
348 * Kingston DataTraveler II+ USB Pen-Drive.
349 * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
351 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
352 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
356 * Motorola E398 Mobile Phone (TransFlash memory card).
357 * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
360 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
361 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
365 * Qware BeatZkey! Pro
368 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
369 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
373 * Time DPA20B 1GB MP3 Player
376 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
377 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
381 * Samsung USB key 128Mb
384 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
385 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
389 * Kingston DataTraveler 2.0 USB Flash memory.
392 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
393 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
397 * Creative MUVO Slim mp3 player (USB)
400 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
401 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
405 * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
408 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
409 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
413 * SanDisk Micro Cruzer 128MB
416 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
417 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
421 * TOSHIBA TransMemory USB sticks
424 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
425 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
430 * PR: usb/75578, usb/72344, usb/65436
432 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
433 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
437 * Genesys 6-in-1 Card Reader
440 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
441 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
445 * Rekam Digital CAMERA
448 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
449 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
453 * iRiver H10 MP3 player
456 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
457 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
461 * iRiver U10 MP3 player
464 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
465 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
472 {T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
473 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
477 * EasyMP3 EM732X USB 2.0 Flash MP3 Player
480 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
481 "1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
488 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
489 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
493 * Philips USB Key Audio KEY013
496 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
497 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
504 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
505 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
512 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
513 /*quirks*/ DA_Q_NO_SYNC_CACHE
517 * I/O Magic USB flash - Giga Bank
520 {T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
521 /*quirks*/ DA_Q_NO_SYNC_CACHE
525 * JoyFly 128mb USB Flash Drive
528 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
529 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
536 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
537 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
541 * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A
544 {T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*",
545 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
549 * Samsung YP-U3 mp3-player
552 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3",
553 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
556 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
557 "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
561 * Sony Cyber-Shot DSC cameras
564 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
565 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
569 static disk_strategy_t dastrategy;
570 static dumper_t dadump;
571 static periph_init_t dainit;
572 static void daasync(void *callback_arg, u_int32_t code,
573 struct cam_path *path, void *arg);
574 static void dasysctlinit(void *context, int pending);
575 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
576 static periph_ctor_t daregister;
577 static periph_dtor_t dacleanup;
578 static periph_start_t dastart;
579 static periph_oninv_t daoninvalidate;
580 static void dadone(struct cam_periph *periph,
581 union ccb *done_ccb);
582 static int daerror(union ccb *ccb, u_int32_t cam_flags,
583 u_int32_t sense_flags);
584 static void daprevent(struct cam_periph *periph, int action);
585 static int dagetcapacity(struct cam_periph *periph);
586 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
588 static timeout_t dasendorderedtag;
589 static void dashutdown(void *arg, int howto);
591 #ifndef DA_DEFAULT_TIMEOUT
592 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
595 #ifndef DA_DEFAULT_RETRY
596 #define DA_DEFAULT_RETRY 4
599 #ifndef DA_DEFAULT_SEND_ORDERED
600 #define DA_DEFAULT_SEND_ORDERED 1
604 static int da_retry_count = DA_DEFAULT_RETRY;
605 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
606 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
608 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
609 "CAM Direct Access Disk driver");
610 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
611 &da_retry_count, 0, "Normal I/O retry count");
612 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
613 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
614 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
615 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
616 SYSCTL_INT(_kern_cam_da, OID_AUTO, da_send_ordered, CTLFLAG_RW,
617 &da_send_ordered, 0, "Send Ordered Tags");
618 TUNABLE_INT("kern.cam.da.da_send_ordered", &da_send_ordered);
621 * DA_ORDEREDTAG_INTERVAL determines how often, relative
622 * to the default timeout, we check to see whether an ordered
623 * tagged transaction is appropriate to prevent simple tag
624 * starvation. Since we'd like to ensure that there is at least
625 * 1/2 of the timeout length left for a starved transaction to
626 * complete after we've sent an ordered tag, we must poll at least
627 * four times in every timeout period. This takes care of the worst
628 * case where a starved transaction starts during an interval that
629 * meets the requirement "don't send an ordered tag" test so it takes
630 * us two intervals to determine that a tag must be sent.
632 #ifndef DA_ORDEREDTAG_INTERVAL
633 #define DA_ORDEREDTAG_INTERVAL 4
636 static struct periph_driver dadriver =
639 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
642 PERIPHDRIVER_DECLARE(da, dadriver);
644 MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
647 daopen(struct disk *dp)
649 struct cam_periph *periph;
650 struct da_softc *softc;
654 periph = (struct cam_periph *)dp->d_drv1;
655 if (periph == NULL) {
659 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
663 cam_periph_lock(periph);
664 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
665 cam_periph_unlock(periph);
666 cam_periph_release(periph);
670 unit = periph->unit_number;
671 softc = (struct da_softc *)periph->softc;
672 softc->flags |= DA_FLAG_OPEN;
674 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
675 ("daopen: disk=%s%d (unit %d)\n", dp->d_name, dp->d_unit,
678 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
679 /* Invalidate our pack information. */
680 softc->flags &= ~DA_FLAG_PACK_INVALID;
683 error = dagetcapacity(periph);
687 softc->disk->d_sectorsize = softc->params.secsize;
688 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
689 /* XXX: these are not actually "firmware" values, so they may be wrong */
690 softc->disk->d_fwsectors = softc->params.secs_per_track;
691 softc->disk->d_fwheads = softc->params.heads;
692 softc->disk->d_devstat->block_size = softc->params.secsize;
693 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
695 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
696 (softc->quirks & DA_Q_NO_PREVENT) == 0)
697 daprevent(periph, PR_PREVENT);
699 softc->flags &= ~DA_FLAG_OPEN;
701 cam_periph_unhold(periph);
702 cam_periph_unlock(periph);
705 cam_periph_release(periph);
711 daclose(struct disk *dp)
713 struct cam_periph *periph;
714 struct da_softc *softc;
717 periph = (struct cam_periph *)dp->d_drv1;
721 cam_periph_lock(periph);
722 if ((error = cam_periph_hold(periph, PRIBIO)) != 0) {
723 cam_periph_unlock(periph);
724 cam_periph_release(periph);
728 softc = (struct da_softc *)periph->softc;
730 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
733 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
735 scsi_synchronize_cache(&ccb->csio,
739 /*begin_lba*/0,/* Cover the whole disk */
744 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
745 /*sense_flags*/SF_RETRY_UA,
746 softc->disk->d_devstat);
748 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
749 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
750 CAM_SCSI_STATUS_ERROR) {
752 int sense_key, error_code;
754 scsi_extract_sense(&ccb->csio.sense_data,
758 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
759 scsi_sense_print(&ccb->csio);
761 xpt_print(periph->path, "Synchronize cache "
762 "failed, status == 0x%x, scsi status == "
763 "0x%x\n", ccb->csio.ccb_h.status,
764 ccb->csio.scsi_status);
768 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
769 cam_release_devq(ccb->ccb_h.path,
775 xpt_release_ccb(ccb);
779 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
780 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
781 daprevent(periph, PR_ALLOW);
783 * If we've got removeable media, mark the blocksize as
784 * unavailable, since it could change when new media is
787 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
790 softc->flags &= ~DA_FLAG_OPEN;
791 cam_periph_unhold(periph);
792 cam_periph_unlock(periph);
793 cam_periph_release(periph);
798 * Actually translate the requested transfer into one the physical driver
799 * can understand. The transfer is described by a buf and will include
800 * only one physical transfer.
803 dastrategy(struct bio *bp)
805 struct cam_periph *periph;
806 struct da_softc *softc;
808 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
809 if (periph == NULL) {
810 biofinish(bp, NULL, ENXIO);
813 softc = (struct da_softc *)periph->softc;
815 cam_periph_lock(periph);
818 * If the device has been made invalid, error out
820 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
821 cam_periph_unlock(periph);
822 biofinish(bp, NULL, ENXIO);
827 * Place it in the queue of disk activities for this disk
829 bioq_disksort(&softc->bio_queue, bp);
832 * Schedule ourselves for performing the work.
834 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
835 cam_periph_unlock(periph);
841 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
843 struct cam_periph *periph;
844 struct da_softc *softc;
846 struct ccb_scsiio csio;
853 softc = (struct da_softc *)periph->softc;
854 cam_periph_lock(periph);
855 secsize = softc->params.secsize;
857 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
858 cam_periph_unlock(periph);
863 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
864 csio.ccb_h.ccb_state = DA_CCB_DUMP;
865 scsi_read_write(&csio,
871 /*minimum_cmd_size*/ softc->minimum_cmd_size,
874 /*data_ptr*/(u_int8_t *) virtual,
876 /*sense_len*/SSD_FULL_SIZE,
877 DA_DEFAULT_TIMEOUT * 1000);
878 xpt_polled_action((union ccb *)&csio);
880 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
881 printf("Aborting dump due to I/O error.\n");
882 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
883 CAM_SCSI_STATUS_ERROR)
884 scsi_sense_print(&csio);
886 printf("status == 0x%x, scsi status == 0x%x\n",
887 csio.ccb_h.status, csio.scsi_status);
890 cam_periph_unlock(periph);
895 * Sync the disk cache contents to the physical media.
897 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
899 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
900 csio.ccb_h.ccb_state = DA_CCB_DUMP;
901 scsi_synchronize_cache(&csio,
905 /*begin_lba*/0,/* Cover the whole disk */
909 xpt_polled_action((union ccb *)&csio);
911 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
912 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
913 CAM_SCSI_STATUS_ERROR) {
915 int sense_key, error_code;
917 scsi_extract_sense(&csio.sense_data,
921 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
922 scsi_sense_print(&csio);
924 xpt_print(periph->path, "Synchronize cache "
925 "failed, status == 0x%x, scsi status == "
926 "0x%x\n", csio.ccb_h.status,
931 cam_periph_unlock(periph);
941 * Install a global async callback. This callback will
942 * receive async callbacks like "new device found".
944 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
946 if (status != CAM_REQ_CMP) {
947 printf("da: Failed to attach master async callback "
948 "due to status 0x%x!\n", status);
949 } else if (da_send_ordered) {
951 /* Register our shutdown event handler */
952 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
953 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
954 printf("dainit: shutdown event registration failed!\n");
959 daoninvalidate(struct cam_periph *periph)
961 struct da_softc *softc;
963 softc = (struct da_softc *)periph->softc;
966 * De-register any async callbacks.
968 xpt_register_async(0, daasync, periph, periph->path);
970 softc->flags |= DA_FLAG_PACK_INVALID;
973 * Return all queued I/O with ENXIO.
974 * XXX Handle any transactions queued to the card
975 * with XPT_ABORT_CCB.
977 bioq_flush(&softc->bio_queue, NULL, ENXIO);
979 disk_gone(softc->disk);
980 xpt_print(periph->path, "lost device\n");
984 dacleanup(struct cam_periph *periph)
986 struct da_softc *softc;
988 softc = (struct da_softc *)periph->softc;
990 xpt_print(periph->path, "removing device entry\n");
991 cam_periph_unlock(periph);
994 * If we can't free the sysctl tree, oh well...
996 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
997 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
998 xpt_print(periph->path, "can't remove sysctl context\n");
1001 disk_destroy(softc->disk);
1002 callout_drain(&softc->sendordered_c);
1003 free(softc, M_DEVBUF);
1004 cam_periph_lock(periph);
1008 daasync(void *callback_arg, u_int32_t code,
1009 struct cam_path *path, void *arg)
1011 struct cam_periph *periph;
1013 periph = (struct cam_periph *)callback_arg;
1015 case AC_FOUND_DEVICE:
1017 struct ccb_getdev *cgd;
1020 cgd = (struct ccb_getdev *)arg;
1024 if (cgd->protocol != PROTO_SCSI)
1027 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1028 && SID_TYPE(&cgd->inq_data) != T_RBC
1029 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1033 * Allocate a peripheral instance for
1034 * this device and start the probe
1037 status = cam_periph_alloc(daregister, daoninvalidate,
1039 "da", CAM_PERIPH_BIO,
1040 cgd->ccb_h.path, daasync,
1041 AC_FOUND_DEVICE, cgd);
1043 if (status != CAM_REQ_CMP
1044 && status != CAM_REQ_INPROG)
1045 printf("daasync: Unable to attach to new device "
1046 "due to status 0x%x\n", status);
1052 struct da_softc *softc;
1053 struct ccb_hdr *ccbh;
1055 softc = (struct da_softc *)periph->softc;
1057 * Don't fail on the expected unit attention
1060 softc->flags |= DA_FLAG_RETRY_UA;
1061 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1062 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1066 cam_periph_async(periph, code, path, arg);
1072 dasysctlinit(void *context, int pending)
1074 struct cam_periph *periph;
1075 struct da_softc *softc;
1076 char tmpstr[80], tmpstr2[80];
1077 struct ccb_trans_settings cts;
1079 periph = (struct cam_periph *)context;
1081 * periph was held for us when this task was enqueued
1083 if (periph->flags & CAM_PERIPH_INVALID) {
1084 cam_periph_release(periph);
1088 softc = (struct da_softc *)periph->softc;
1089 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1090 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1092 sysctl_ctx_init(&softc->sysctl_ctx);
1093 softc->flags |= DA_FLAG_SCTX_INIT;
1094 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1095 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1096 CTLFLAG_RD, 0, tmpstr);
1097 if (softc->sysctl_tree == NULL) {
1098 printf("dasysctlinit: unable to allocate sysctl tree\n");
1099 cam_periph_release(periph);
1104 * Now register the sysctl handler, so the user can change the value on
1107 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1108 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1109 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1110 "Minimum CDB size");
1113 * Add some addressing info.
1115 memset(&cts, 0, sizeof (cts));
1116 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
1117 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1118 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1119 cam_periph_lock(periph);
1120 xpt_action((union ccb *)&cts);
1121 cam_periph_unlock(periph);
1122 if (cts.ccb_h.status != CAM_REQ_CMP) {
1123 cam_periph_release(periph);
1126 if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
1127 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1128 if (fc->valid & CTS_FC_VALID_WWPN) {
1129 softc->wwpn = fc->wwpn;
1130 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1131 SYSCTL_CHILDREN(softc->sysctl_tree),
1132 OID_AUTO, "wwpn", CTLFLAG_RD,
1133 &softc->wwpn, "World Wide Port Name");
1136 cam_periph_release(periph);
1140 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1144 value = *(int *)arg1;
1146 error = sysctl_handle_int(oidp, &value, 0, req);
1149 || (req->newptr == NULL))
1153 * Acceptable values here are 6, 10, 12 or 16.
1157 else if ((value > 6)
1160 else if ((value > 10)
1163 else if (value > 12)
1166 *(int *)arg1 = value;
1172 daregister(struct cam_periph *periph, void *arg)
1174 struct da_softc *softc;
1175 struct ccb_pathinq cpi;
1176 struct ccb_getdev *cgd;
1180 cgd = (struct ccb_getdev *)arg;
1181 if (periph == NULL) {
1182 printf("daregister: periph was NULL!!\n");
1183 return(CAM_REQ_CMP_ERR);
1187 printf("daregister: no getdev CCB, can't register device\n");
1188 return(CAM_REQ_CMP_ERR);
1191 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
1194 if (softc == NULL) {
1195 printf("daregister: Unable to probe new device. "
1196 "Unable to allocate softc\n");
1197 return(CAM_REQ_CMP_ERR);
1200 LIST_INIT(&softc->pending_ccbs);
1201 softc->state = DA_STATE_PROBE;
1202 bioq_init(&softc->bio_queue);
1203 if (SID_IS_REMOVABLE(&cgd->inq_data))
1204 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1205 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1206 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1208 periph->softc = softc;
1211 * See if this device has any quirks.
1213 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1214 (caddr_t)da_quirk_table,
1215 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1216 sizeof(*da_quirk_table), scsi_inquiry_match);
1219 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1221 softc->quirks = DA_Q_NONE;
1223 /* Check if the SIM does not want 6 byte commands */
1224 bzero(&cpi, sizeof(cpi));
1225 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1226 cpi.ccb_h.func_code = XPT_PATH_INQ;
1227 xpt_action((union ccb *)&cpi);
1228 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1229 softc->quirks |= DA_Q_NO_6_BYTE;
1231 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1234 * RBC devices don't have to support READ(6), only READ(10).
1236 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1237 softc->minimum_cmd_size = 10;
1239 softc->minimum_cmd_size = 6;
1242 * Load the user's default, if any.
1244 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1245 periph->unit_number);
1246 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1249 * 6, 10, 12 and 16 are the currently permissible values.
1251 if (softc->minimum_cmd_size < 6)
1252 softc->minimum_cmd_size = 6;
1253 else if ((softc->minimum_cmd_size > 6)
1254 && (softc->minimum_cmd_size <= 10))
1255 softc->minimum_cmd_size = 10;
1256 else if ((softc->minimum_cmd_size > 10)
1257 && (softc->minimum_cmd_size <= 12))
1258 softc->minimum_cmd_size = 12;
1259 else if (softc->minimum_cmd_size > 12)
1260 softc->minimum_cmd_size = 16;
1263 * Register this media as a disk
1267 * Add async callbacks for bus reset and
1268 * bus device reset calls. I don't bother
1269 * checking if this fails as, in most cases,
1270 * the system will function just fine without
1271 * them and the only alternative would be to
1272 * not attach the device on failure.
1274 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
1275 daasync, periph, periph->path);
1278 * Take an exclusive refcount on the periph while dastart is called
1279 * to finish the probe. The reference will be dropped in dadone at
1282 (void)cam_periph_hold(periph, PRIBIO);
1285 * Schedule a periodic event to occasionally send an
1286 * ordered tag to a device.
1288 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
1289 callout_reset(&softc->sendordered_c,
1290 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
1291 dasendorderedtag, softc);
1293 mtx_unlock(periph->sim->mtx);
1294 softc->disk = disk_alloc();
1295 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
1296 periph->unit_number, 0,
1297 DEVSTAT_BS_UNAVAILABLE,
1298 SID_TYPE(&cgd->inq_data) |
1299 XPORT_DEVSTAT_TYPE(cpi.transport),
1300 DEVSTAT_PRIORITY_DISK);
1301 softc->disk->d_open = daopen;
1302 softc->disk->d_close = daclose;
1303 softc->disk->d_strategy = dastrategy;
1304 softc->disk->d_dump = dadump;
1305 softc->disk->d_name = "da";
1306 softc->disk->d_drv1 = periph;
1308 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */
1309 else if (cpi.maxio > MAXPHYS)
1310 softc->disk->d_maxsize = MAXPHYS; /* for safety */
1312 softc->disk->d_maxsize = cpi.maxio;
1313 softc->disk->d_unit = periph->unit_number;
1314 softc->disk->d_flags = 0;
1315 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
1316 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
1317 strlcpy(softc->disk->d_ident, cgd->serial_num,
1318 MIN(sizeof(softc->disk->d_ident), cgd->serial_num_len + 1));
1319 cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
1320 sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
1321 strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
1322 cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
1323 cgd->inq_data.product, sizeof(cgd->inq_data.product),
1324 sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
1325 softc->disk->d_hba_vendor = cpi.hba_vendor;
1326 softc->disk->d_hba_device = cpi.hba_device;
1327 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
1328 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
1329 disk_create(softc->disk, DISK_VERSION);
1330 mtx_lock(periph->sim->mtx);
1332 xpt_schedule(periph, CAM_PRIORITY_DEV);
1334 return(CAM_REQ_CMP);
1338 dastart(struct cam_periph *periph, union ccb *start_ccb)
1340 struct da_softc *softc;
1342 softc = (struct da_softc *)periph->softc;
1344 switch (softc->state) {
1345 case DA_STATE_NORMAL:
1347 /* Pull a buffer from the queue and get going on it */
1351 * See if there is a buf with work for us to do..
1353 bp = bioq_first(&softc->bio_queue);
1354 if (periph->immediate_priority <= periph->pinfo.priority) {
1355 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1356 ("queuing for immediate ccb\n"));
1357 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1358 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1360 periph->immediate_priority = CAM_PRIORITY_NONE;
1361 wakeup(&periph->ccb_list);
1362 } else if (bp == NULL) {
1363 xpt_release_ccb(start_ccb);
1367 bioq_remove(&softc->bio_queue, bp);
1369 if ((bp->bio_flags & BIO_ORDERED) != 0
1370 || (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1371 softc->flags &= ~DA_FLAG_NEED_OTAG;
1372 softc->ordered_tag_count++;
1373 tag_code = MSG_ORDERED_Q_TAG;
1375 tag_code = MSG_SIMPLE_Q_TAG;
1377 switch (bp->bio_cmd) {
1380 scsi_read_write(&start_ccb->csio,
1381 /*retries*/da_retry_count,
1383 /*tag_action*/tag_code,
1384 /*read_op*/bp->bio_cmd
1387 softc->minimum_cmd_size,
1388 /*lba*/bp->bio_pblkno,
1389 /*block_count*/bp->bio_bcount /
1390 softc->params.secsize,
1391 /*data_ptr*/ bp->bio_data,
1392 /*dxfer_len*/ bp->bio_bcount,
1393 /*sense_len*/SSD_FULL_SIZE,
1394 da_default_timeout * 1000);
1398 * BIO_FLUSH doesn't currently communicate
1399 * range data, so we synchronize the cache
1400 * over the whole disk. We also force
1401 * ordered tag semantics the flush applies
1402 * to all previously queued I/O.
1404 scsi_synchronize_cache(&start_ccb->csio,
1411 da_default_timeout*1000);
1414 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1417 * Block out any asyncronous callbacks
1418 * while we touch the pending ccb list.
1420 LIST_INSERT_HEAD(&softc->pending_ccbs,
1421 &start_ccb->ccb_h, periph_links.le);
1422 softc->outstanding_cmds++;
1424 /* We expect a unit attention from this device */
1425 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1426 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1427 softc->flags &= ~DA_FLAG_RETRY_UA;
1430 start_ccb->ccb_h.ccb_bp = bp;
1431 bp = bioq_first(&softc->bio_queue);
1433 xpt_action(start_ccb);
1437 /* Have more work to do, so ensure we stay scheduled */
1438 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1442 case DA_STATE_PROBE:
1444 struct ccb_scsiio *csio;
1445 struct scsi_read_capacity_data *rcap;
1447 rcap = (struct scsi_read_capacity_data *)
1448 malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
1450 printf("dastart: Couldn't malloc read_capacity data\n");
1451 /* da_free_periph??? */
1454 csio = &start_ccb->csio;
1455 scsi_read_capacity(csio,
1462 start_ccb->ccb_h.ccb_bp = NULL;
1463 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1464 xpt_action(start_ccb);
1467 case DA_STATE_PROBE2:
1469 struct ccb_scsiio *csio;
1470 struct scsi_read_capacity_data_long *rcaplong;
1472 rcaplong = (struct scsi_read_capacity_data_long *)
1473 malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
1474 if (rcaplong == NULL) {
1475 printf("dastart: Couldn't malloc read_capacity data\n");
1476 /* da_free_periph??? */
1479 csio = &start_ccb->csio;
1480 scsi_read_capacity_16(csio,
1483 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1488 /*sense_len*/ SSD_FULL_SIZE,
1490 start_ccb->ccb_h.ccb_bp = NULL;
1491 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1492 xpt_action(start_ccb);
1499 cmd6workaround(union ccb *ccb)
1501 struct scsi_rw_6 cmd6;
1502 struct scsi_rw_10 *cmd10;
1503 struct da_softc *softc;
1507 cdb = ccb->csio.cdb_io.cdb_bytes;
1509 /* Translation only possible if CDB is an array and cmd is R/W6 */
1510 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1511 (*cdb != READ_6 && *cdb != WRITE_6))
1514 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
1515 "increasing minimum_cmd_size to 10.\n");
1516 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1517 softc->minimum_cmd_size = 10;
1519 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1520 cmd10 = (struct scsi_rw_10 *)cdb;
1521 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1523 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1524 cmd10->reserved = 0;
1525 scsi_ulto2b(cmd6.length, cmd10->length);
1526 cmd10->control = cmd6.control;
1527 ccb->csio.cdb_len = sizeof(*cmd10);
1529 /* Requeue request, unfreezing queue if necessary */
1530 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1531 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1534 cam_release_devq(ccb->ccb_h.path,
1538 /*getcount_only*/0);
1544 dadone(struct cam_periph *periph, union ccb *done_ccb)
1546 struct da_softc *softc;
1547 struct ccb_scsiio *csio;
1550 softc = (struct da_softc *)periph->softc;
1551 priority = done_ccb->ccb_h.pinfo.priority;
1552 csio = &done_ccb->csio;
1553 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1554 case DA_CCB_BUFFER_IO:
1558 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1559 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1563 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1568 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1569 if (error == ERESTART) {
1571 * A retry was scheuled, so
1578 if (error == ENXIO) {
1580 * Catastrophic error. Mark our pack as
1584 * XXX See if this is really a media
1587 xpt_print(periph->path,
1588 "Invalidating pack\n");
1589 softc->flags |= DA_FLAG_PACK_INVALID;
1593 * return all queued I/O with EIO, so that
1594 * the client can retry these I/Os in the
1595 * proper order should it attempt to recover.
1597 bioq_flush(&softc->bio_queue, NULL, EIO);
1598 bp->bio_error = error;
1599 bp->bio_resid = bp->bio_bcount;
1600 bp->bio_flags |= BIO_ERROR;
1602 bp->bio_resid = csio->resid;
1604 if (bp->bio_resid != 0)
1605 bp->bio_flags |= BIO_ERROR;
1607 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1608 cam_release_devq(done_ccb->ccb_h.path,
1612 /*getcount_only*/0);
1614 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1615 panic("REQ_CMP with QFRZN");
1616 bp->bio_resid = csio->resid;
1617 if (csio->resid > 0)
1618 bp->bio_flags |= BIO_ERROR;
1622 * Block out any asyncronous callbacks
1623 * while we touch the pending ccb list.
1625 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1626 softc->outstanding_cmds--;
1627 if (softc->outstanding_cmds == 0)
1628 softc->flags |= DA_FLAG_WENT_IDLE;
1636 struct scsi_read_capacity_data *rdcap;
1637 struct scsi_read_capacity_data_long *rcaplong;
1638 char announce_buf[80];
1642 if (softc->state == DA_STATE_PROBE)
1643 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1645 rcaplong = (struct scsi_read_capacity_data_long *)
1648 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1649 struct disk_params *dp;
1650 uint32_t block_size;
1653 if (softc->state == DA_STATE_PROBE) {
1654 block_size = scsi_4btoul(rdcap->length);
1655 maxsector = scsi_4btoul(rdcap->addr);
1658 * According to SBC-2, if the standard 10
1659 * byte READ CAPACITY command returns 2^32,
1660 * we should issue the 16 byte version of
1661 * the command, since the device in question
1662 * has more sectors than can be represented
1663 * with the short version of the command.
1665 if (maxsector == 0xffffffff) {
1666 softc->state = DA_STATE_PROBE2;
1667 free(rdcap, M_SCSIDA);
1668 xpt_release_ccb(done_ccb);
1669 xpt_schedule(periph, priority);
1673 block_size = scsi_4btoul(rcaplong->length);
1674 maxsector = scsi_8btou64(rcaplong->addr);
1678 * Because GEOM code just will panic us if we
1679 * give them an 'illegal' value we'll avoid that
1682 if (block_size == 0 && maxsector == 0) {
1683 snprintf(announce_buf, sizeof(announce_buf),
1685 } else if (block_size >= MAXPHYS || block_size == 0) {
1686 xpt_print(periph->path,
1687 "unsupportable block size %ju\n",
1688 (uintmax_t) block_size);
1689 announce_buf[0] = '\0';
1690 cam_periph_invalidate(periph);
1692 dasetgeom(periph, block_size, maxsector);
1693 dp = &softc->params;
1694 snprintf(announce_buf, sizeof(announce_buf),
1695 "%juMB (%ju %u byte sectors: %dH %dS/T "
1697 (((uintmax_t)dp->secsize *
1698 dp->sectors) / (1024*1024)),
1699 (uintmax_t)dp->sectors,
1700 dp->secsize, dp->heads,
1701 dp->secs_per_track, dp->cylinders);
1706 announce_buf[0] = '\0';
1709 * Retry any UNIT ATTENTION type errors. They
1710 * are expected at boot.
1712 error = daerror(done_ccb, CAM_RETRY_SELTO,
1713 SF_RETRY_UA|SF_NO_PRINT);
1714 if (error == ERESTART) {
1716 * A retry was scheuled, so
1720 } else if (error != 0) {
1721 struct scsi_sense_data *sense;
1723 int sense_key, error_code;
1726 struct ccb_getdev cgd;
1728 /* Don't wedge this device's queue */
1729 status = done_ccb->ccb_h.status;
1730 if ((status & CAM_DEV_QFRZN) != 0)
1731 cam_release_devq(done_ccb->ccb_h.path,
1735 /*getcount_only*/0);
1738 xpt_setup_ccb(&cgd.ccb_h,
1739 done_ccb->ccb_h.path,
1740 CAM_PRIORITY_NORMAL);
1741 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1742 xpt_action((union ccb *)&cgd);
1744 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1745 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1746 || ((status & CAM_AUTOSNS_VALID) == 0))
1752 sense = &csio->sense_data;
1753 scsi_extract_sense(sense, &error_code,
1758 * Attach to anything that claims to be a
1759 * direct access or optical disk device,
1760 * as long as it doesn't return a "Logical
1761 * unit not supported" (0x25) error.
1763 if ((have_sense) && (asc != 0x25)
1764 && (error_code == SSD_CURRENT_ERROR)) {
1765 const char *sense_key_desc;
1766 const char *asc_desc;
1768 scsi_sense_desc(sense_key, asc, ascq,
1772 snprintf(announce_buf,
1773 sizeof(announce_buf),
1774 "Attempt to query device "
1775 "size failed: %s, %s",
1783 xpt_print(periph->path,
1784 "got CAM status %#x\n",
1785 done_ccb->ccb_h.status);
1788 xpt_print(periph->path, "fatal error, "
1789 "failed to attach to device\n");
1792 * Free up resources.
1794 cam_periph_invalidate(periph);
1798 free(csio->data_ptr, M_SCSIDA);
1799 if (announce_buf[0] != '\0') {
1800 xpt_announce_periph(periph, announce_buf);
1802 * Create our sysctl variables, now that we know
1803 * we have successfully attached.
1805 (void) cam_periph_acquire(periph); /* increase the refcount */
1806 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1808 softc->state = DA_STATE_NORMAL;
1810 * Since our peripheral may be invalidated by an error
1811 * above or an external event, we must release our CCB
1812 * before releasing the probe lock on the peripheral.
1813 * The peripheral will only go away once the last lock
1814 * is removed, and we need it around for the CCB release
1817 xpt_release_ccb(done_ccb);
1818 cam_periph_unhold(periph);
1821 case DA_CCB_WAITING:
1823 /* Caller will release the CCB */
1824 wakeup(&done_ccb->ccb_h.cbfcnp);
1828 /* No-op. We're polling */
1833 xpt_release_ccb(done_ccb);
1837 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1839 struct da_softc *softc;
1840 struct cam_periph *periph;
1843 periph = xpt_path_periph(ccb->ccb_h.path);
1844 softc = (struct da_softc *)periph->softc;
1847 * Automatically detect devices that do not support
1848 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
1851 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
1852 error = cmd6workaround(ccb);
1853 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
1854 CAM_SCSI_STATUS_ERROR)
1855 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
1856 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1857 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
1858 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
1859 int sense_key, error_code, asc, ascq;
1861 scsi_extract_sense(&ccb->csio.sense_data,
1862 &error_code, &sense_key, &asc, &ascq);
1863 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
1864 error = cmd6workaround(ccb);
1866 if (error == ERESTART)
1871 * Until we have a better way of doing pack validation,
1872 * don't treat UAs as errors.
1874 sense_flags |= SF_RETRY_UA;
1875 return(cam_periph_error(ccb, cam_flags, sense_flags,
1876 &softc->saved_ccb));
1880 daprevent(struct cam_periph *periph, int action)
1882 struct da_softc *softc;
1886 softc = (struct da_softc *)periph->softc;
1888 if (((action == PR_ALLOW)
1889 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
1890 || ((action == PR_PREVENT)
1891 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
1895 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1897 scsi_prevent(&ccb->csio,
1905 error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
1906 SF_RETRY_UA, softc->disk->d_devstat);
1909 if (action == PR_ALLOW)
1910 softc->flags &= ~DA_FLAG_PACK_LOCKED;
1912 softc->flags |= DA_FLAG_PACK_LOCKED;
1915 xpt_release_ccb(ccb);
1919 dagetcapacity(struct cam_periph *periph)
1921 struct da_softc *softc;
1923 struct scsi_read_capacity_data *rcap;
1924 struct scsi_read_capacity_data_long *rcaplong;
1928 u_int32_t sense_flags;
1930 softc = (struct da_softc *)periph->softc;
1934 sense_flags = SF_RETRY_UA;
1935 if (softc->flags & DA_FLAG_PACK_REMOVABLE)
1936 sense_flags |= SF_NO_PRINT;
1938 /* Do a read capacity */
1939 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcaplong),
1945 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1946 scsi_read_capacity(&ccb->csio,
1953 ccb->ccb_h.ccb_bp = NULL;
1955 error = cam_periph_runccb(ccb, daerror,
1956 /*cam_flags*/CAM_RETRY_SELTO,
1958 softc->disk->d_devstat);
1960 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1961 cam_release_devq(ccb->ccb_h.path,
1965 /*getcount_only*/0);
1968 block_len = scsi_4btoul(rcap->length);
1969 maxsector = scsi_4btoul(rcap->addr);
1971 if (maxsector != 0xffffffff)
1976 rcaplong = (struct scsi_read_capacity_data_long *)rcap;
1978 scsi_read_capacity_16(&ccb->csio,
1981 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1986 /*sense_len*/ SSD_FULL_SIZE,
1988 ccb->ccb_h.ccb_bp = NULL;
1990 error = cam_periph_runccb(ccb, daerror,
1991 /*cam_flags*/CAM_RETRY_SELTO,
1993 softc->disk->d_devstat);
1995 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1996 cam_release_devq(ccb->ccb_h.path,
2000 /*getcount_only*/0);
2003 block_len = scsi_4btoul(rcaplong->length);
2004 maxsector = scsi_8btou64(rcaplong->addr);
2010 if (block_len >= MAXPHYS || block_len == 0) {
2011 xpt_print(periph->path,
2012 "unsupportable block size %ju\n",
2013 (uintmax_t) block_len);
2016 dasetgeom(periph, block_len, maxsector);
2019 xpt_release_ccb(ccb);
2021 free(rcap, M_SCSIDA);
2027 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector)
2029 struct ccb_calc_geometry ccg;
2030 struct da_softc *softc;
2031 struct disk_params *dp;
2033 softc = (struct da_softc *)periph->softc;
2035 dp = &softc->params;
2036 dp->secsize = block_len;
2037 dp->sectors = maxsector + 1;
2039 * Have the controller provide us with a geometry
2040 * for this disk. The only time the geometry
2041 * matters is when we boot and the controller
2042 * is the only one knowledgeable enough to come
2043 * up with something that will make this a bootable
2046 xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2047 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
2048 ccg.block_size = dp->secsize;
2049 ccg.volume_size = dp->sectors;
2051 ccg.secs_per_track = 0;
2053 xpt_action((union ccb*)&ccg);
2054 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2056 * We don't know what went wrong here- but just pick
2057 * a geometry so we don't have nasty things like divide
2061 dp->secs_per_track = 255;
2062 dp->cylinders = dp->sectors / (255 * 255);
2063 if (dp->cylinders == 0) {
2067 dp->heads = ccg.heads;
2068 dp->secs_per_track = ccg.secs_per_track;
2069 dp->cylinders = ccg.cylinders;
2074 dasendorderedtag(void *arg)
2076 struct da_softc *softc = arg;
2078 if (da_send_ordered) {
2079 if ((softc->ordered_tag_count == 0)
2080 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
2081 softc->flags |= DA_FLAG_NEED_OTAG;
2083 if (softc->outstanding_cmds > 0)
2084 softc->flags &= ~DA_FLAG_WENT_IDLE;
2086 softc->ordered_tag_count = 0;
2088 /* Queue us up again */
2089 callout_reset(&softc->sendordered_c,
2090 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
2091 dasendorderedtag, softc);
2095 * Step through all DA peripheral drivers, and if the device is still open,
2096 * sync the disk cache to physical media.
2099 dashutdown(void * arg, int howto)
2101 struct cam_periph *periph;
2102 struct da_softc *softc;
2104 TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
2107 cam_periph_lock(periph);
2108 softc = (struct da_softc *)periph->softc;
2111 * We only sync the cache if the drive is still open, and
2112 * if the drive is capable of it..
2114 if (((softc->flags & DA_FLAG_OPEN) == 0)
2115 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
2116 cam_periph_unlock(periph);
2120 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2122 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
2123 scsi_synchronize_cache(&ccb.csio,
2127 /*begin_lba*/0, /* whole disk */
2132 xpt_polled_action(&ccb);
2134 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2135 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2136 CAM_SCSI_STATUS_ERROR)
2137 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
2138 int error_code, sense_key, asc, ascq;
2140 scsi_extract_sense(&ccb.csio.sense_data,
2141 &error_code, &sense_key,
2144 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
2145 scsi_sense_print(&ccb.csio);
2147 xpt_print(periph->path, "Synchronize "
2148 "cache failed, status == 0x%x, scsi status "
2149 "== 0x%x\n", ccb.ccb_h.status,
2150 ccb.csio.scsi_status);
2154 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
2155 cam_release_devq(ccb.ccb_h.path,
2159 /*getcount_only*/0);
2160 cam_periph_unlock(periph);
2164 #else /* !_KERNEL */
2167 * XXX This is only left out of the kernel build to silence warnings. If,
2168 * for some reason this function is used in the kernel, the ifdefs should
2169 * be moved so it is included both in the kernel and userland.
2172 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2173 void (*cbfcnp)(struct cam_periph *, union ccb *),
2174 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2175 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2178 struct scsi_format_unit *scsi_cmd;
2180 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2181 scsi_cmd->opcode = FORMAT_UNIT;
2182 scsi_cmd->byte2 = byte2;
2183 scsi_ulto2b(ileave, scsi_cmd->interleave);
2188 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2197 #endif /* _KERNEL */