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;
135 struct da_quirk_entry {
136 struct scsi_inquiry_pattern inq_pat;
140 static const char quantum[] = "QUANTUM";
141 static const char microp[] = "MICROP";
143 static struct da_quirk_entry da_quirk_table[] =
145 /* SPI, FC devices */
148 * Fujitsu M2513A MO drives.
149 * Tested devices: M2513A2 firmware versions 1200 & 1300.
150 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
151 * Reported by: W.Scholten <whs@xs4all.nl>
153 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
154 /*quirks*/ DA_Q_NO_SYNC_CACHE
158 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
159 /*quirks*/ DA_Q_NO_SYNC_CACHE
163 * This particular Fujitsu drive doesn't like the
164 * synchronize cache command.
165 * Reported by: Tom Jackson <toj@gorilla.net>
167 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
168 /*quirks*/ DA_Q_NO_SYNC_CACHE
172 * This drive doesn't like the synchronize cache command
173 * either. Reported by: Matthew Jacob <mjacob@feral.com>
174 * in NetBSD PR kern/6027, August 24, 1998.
176 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
177 /*quirks*/ DA_Q_NO_SYNC_CACHE
181 * This drive doesn't like the synchronize cache command
182 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
185 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
186 /*quirks*/ DA_Q_NO_SYNC_CACHE
190 * Doesn't like the synchronize cache command.
191 * Reported by: Blaz Zupan <blaz@gold.amis.net>
193 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
194 /*quirks*/ DA_Q_NO_SYNC_CACHE
198 * Doesn't like the synchronize cache command.
199 * Reported by: Blaz Zupan <blaz@gold.amis.net>
201 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
202 /*quirks*/ DA_Q_NO_SYNC_CACHE
206 * Doesn't like the synchronize cache command.
208 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
209 /*quirks*/ DA_Q_NO_SYNC_CACHE
213 * Doesn't like the synchronize cache command.
214 * Reported by: walter@pelissero.de
216 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
217 /*quirks*/ DA_Q_NO_SYNC_CACHE
221 * Doesn't work correctly with 6 byte reads/writes.
222 * Returns illegal request, and points to byte 9 of the
224 * Reported by: Adam McDougall <bsdx@spawnet.com>
226 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
227 /*quirks*/ DA_Q_NO_6_BYTE
231 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
232 /*quirks*/ DA_Q_NO_6_BYTE
236 * Doesn't like the synchronize cache command.
237 * Reported by: walter@pelissero.de
239 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
240 /*quirks*/ DA_Q_NO_SYNC_CACHE
244 * The CISS RAID controllers do not support SYNC_CACHE
246 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
247 /*quirks*/ DA_Q_NO_SYNC_CACHE
249 /* USB mass storage devices supported by umass(4) */
252 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
255 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
256 /*quirks*/ DA_Q_NO_SYNC_CACHE
260 * Power Quotient Int. (PQI) USB flash key
263 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
264 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
268 * Creative Nomad MUVO mp3 player (USB)
271 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
272 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
276 * Jungsoft NEXDISK USB flash key
279 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
280 /*quirks*/ DA_Q_NO_SYNC_CACHE
284 * FreeDik USB Mini Data Drive
287 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
288 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
292 * Sigmatel USB Flash MP3 Player
295 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
296 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
300 * Neuros USB Digital Audio Computer
303 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
304 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
308 * SEAGRAND NP-900 MP3 Player
311 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
312 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
316 * iRiver iFP MP3 player (with UMS Firmware)
317 * PR: kern/54881, i386/63941, kern/66124
319 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
320 /*quirks*/ DA_Q_NO_SYNC_CACHE
324 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
327 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
328 /*quirks*/ DA_Q_NO_SYNC_CACHE
332 * ZICPlay USB MP3 Player with FM
335 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
336 /*quirks*/ DA_Q_NO_SYNC_CACHE
340 * TEAC USB floppy mechanisms
342 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
343 /*quirks*/ DA_Q_NO_SYNC_CACHE
347 * Kingston DataTraveler II+ USB Pen-Drive.
348 * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
350 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
351 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
355 * Motorola E398 Mobile Phone (TransFlash memory card).
356 * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
359 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
360 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
364 * Qware BeatZkey! Pro
367 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
368 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
372 * Time DPA20B 1GB MP3 Player
375 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
376 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
380 * Samsung USB key 128Mb
383 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
384 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
388 * Kingston DataTraveler 2.0 USB Flash memory.
391 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
392 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
396 * Creative MUVO Slim mp3 player (USB)
399 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
400 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
404 * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
407 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
408 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
412 * SanDisk Micro Cruzer 128MB
415 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
416 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
420 * TOSHIBA TransMemory USB sticks
423 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
424 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
429 * PR: usb/75578, usb/72344, usb/65436
431 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
432 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
436 * Genesys 6-in-1 Card Reader
439 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
440 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
444 * Rekam Digital CAMERA
447 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
448 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
452 * iRiver H10 MP3 player
455 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
456 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
460 * iRiver U10 MP3 player
463 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
464 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
471 {T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
472 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
476 * EasyMP3 EM732X USB 2.0 Flash MP3 Player
479 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
480 "1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
487 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
488 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
492 * Philips USB Key Audio KEY013
495 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
496 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
503 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
504 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
511 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
512 /*quirks*/ DA_Q_NO_SYNC_CACHE
516 * I/O Magic USB flash - Giga Bank
519 {T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
520 /*quirks*/ DA_Q_NO_SYNC_CACHE
524 * JoyFly 128mb USB Flash Drive
527 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
528 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
535 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
536 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
540 * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A
543 {T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*",
544 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
548 * Samsung YP-U3 mp3-player
551 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3",
552 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
555 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
556 "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
560 * Sony Cyber-Shot DSC cameras
563 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
564 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
568 static disk_strategy_t dastrategy;
569 static dumper_t dadump;
570 static periph_init_t dainit;
571 static void daasync(void *callback_arg, u_int32_t code,
572 struct cam_path *path, void *arg);
573 static void dasysctlinit(void *context, int pending);
574 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
575 static periph_ctor_t daregister;
576 static periph_dtor_t dacleanup;
577 static periph_start_t dastart;
578 static periph_oninv_t daoninvalidate;
579 static void dadone(struct cam_periph *periph,
580 union ccb *done_ccb);
581 static int daerror(union ccb *ccb, u_int32_t cam_flags,
582 u_int32_t sense_flags);
583 static void daprevent(struct cam_periph *periph, int action);
584 static int dagetcapacity(struct cam_periph *periph);
585 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
587 static timeout_t dasendorderedtag;
588 static void dashutdown(void *arg, int howto);
590 #ifndef DA_DEFAULT_TIMEOUT
591 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
594 #ifndef DA_DEFAULT_RETRY
595 #define DA_DEFAULT_RETRY 4
598 #ifndef DA_DEFAULT_SEND_ORDERED
599 #define DA_DEFAULT_SEND_ORDERED 1
603 static int da_retry_count = DA_DEFAULT_RETRY;
604 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
605 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
607 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
608 "CAM Direct Access Disk driver");
609 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
610 &da_retry_count, 0, "Normal I/O retry count");
611 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
612 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
613 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
614 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
615 SYSCTL_INT(_kern_cam_da, OID_AUTO, da_send_ordered, CTLFLAG_RW,
616 &da_send_ordered, 0, "Send Ordered Tags");
617 TUNABLE_INT("kern.cam.da.da_send_ordered", &da_send_ordered);
620 * DA_ORDEREDTAG_INTERVAL determines how often, relative
621 * to the default timeout, we check to see whether an ordered
622 * tagged transaction is appropriate to prevent simple tag
623 * starvation. Since we'd like to ensure that there is at least
624 * 1/2 of the timeout length left for a starved transaction to
625 * complete after we've sent an ordered tag, we must poll at least
626 * four times in every timeout period. This takes care of the worst
627 * case where a starved transaction starts during an interval that
628 * meets the requirement "don't send an ordered tag" test so it takes
629 * us two intervals to determine that a tag must be sent.
631 #ifndef DA_ORDEREDTAG_INTERVAL
632 #define DA_ORDEREDTAG_INTERVAL 4
635 static struct periph_driver dadriver =
638 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
641 PERIPHDRIVER_DECLARE(da, dadriver);
643 MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
646 daopen(struct disk *dp)
648 struct cam_periph *periph;
649 struct da_softc *softc;
653 periph = (struct cam_periph *)dp->d_drv1;
654 if (periph == NULL) {
658 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
662 cam_periph_lock(periph);
663 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
664 cam_periph_unlock(periph);
665 cam_periph_release(periph);
669 unit = periph->unit_number;
670 softc = (struct da_softc *)periph->softc;
671 softc->flags |= DA_FLAG_OPEN;
673 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
674 ("daopen: disk=%s%d (unit %d)\n", dp->d_name, dp->d_unit,
677 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
678 /* Invalidate our pack information. */
679 softc->flags &= ~DA_FLAG_PACK_INVALID;
682 error = dagetcapacity(periph);
686 softc->disk->d_sectorsize = softc->params.secsize;
687 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
688 /* XXX: these are not actually "firmware" values, so they may be wrong */
689 softc->disk->d_fwsectors = softc->params.secs_per_track;
690 softc->disk->d_fwheads = softc->params.heads;
691 softc->disk->d_devstat->block_size = softc->params.secsize;
692 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
694 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
695 (softc->quirks & DA_Q_NO_PREVENT) == 0)
696 daprevent(periph, PR_PREVENT);
698 softc->flags &= ~DA_FLAG_OPEN;
700 cam_periph_unhold(periph);
701 cam_periph_unlock(periph);
704 cam_periph_release(periph);
710 daclose(struct disk *dp)
712 struct cam_periph *periph;
713 struct da_softc *softc;
716 periph = (struct cam_periph *)dp->d_drv1;
720 cam_periph_lock(periph);
721 if ((error = cam_periph_hold(periph, PRIBIO)) != 0) {
722 cam_periph_unlock(periph);
723 cam_periph_release(periph);
727 softc = (struct da_softc *)periph->softc;
729 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
732 ccb = cam_periph_getccb(periph, /*priority*/1);
734 scsi_synchronize_cache(&ccb->csio,
738 /*begin_lba*/0,/* Cover the whole disk */
743 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
744 /*sense_flags*/SF_RETRY_UA,
745 softc->disk->d_devstat);
747 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
748 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
749 CAM_SCSI_STATUS_ERROR) {
751 int sense_key, error_code;
753 scsi_extract_sense(&ccb->csio.sense_data,
757 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
758 scsi_sense_print(&ccb->csio);
760 xpt_print(periph->path, "Synchronize cache "
761 "failed, status == 0x%x, scsi status == "
762 "0x%x\n", ccb->csio.ccb_h.status,
763 ccb->csio.scsi_status);
767 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
768 cam_release_devq(ccb->ccb_h.path,
774 xpt_release_ccb(ccb);
778 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
779 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
780 daprevent(periph, PR_ALLOW);
782 * If we've got removeable media, mark the blocksize as
783 * unavailable, since it could change when new media is
786 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
789 softc->flags &= ~DA_FLAG_OPEN;
790 cam_periph_unhold(periph);
791 cam_periph_unlock(periph);
792 cam_periph_release(periph);
797 * Actually translate the requested transfer into one the physical driver
798 * can understand. The transfer is described by a buf and will include
799 * only one physical transfer.
802 dastrategy(struct bio *bp)
804 struct cam_periph *periph;
805 struct da_softc *softc;
807 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
808 if (periph == NULL) {
809 biofinish(bp, NULL, ENXIO);
812 softc = (struct da_softc *)periph->softc;
814 cam_periph_lock(periph);
818 * check it's not too big a transfer for our adapter
820 scsi_minphys(bp,&sd_switch);
824 * Mask interrupts so that the pack cannot be invalidated until
825 * after we are in the queue. Otherwise, we might not properly
826 * clean up one of the buffers.
830 * If the device has been made invalid, error out
832 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
833 cam_periph_unlock(periph);
834 biofinish(bp, NULL, ENXIO);
839 * Place it in the queue of disk activities for this disk
841 bioq_disksort(&softc->bio_queue, bp);
844 * Schedule ourselves for performing the work.
846 xpt_schedule(periph, /* XXX priority */1);
847 cam_periph_unlock(periph);
853 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
855 struct cam_periph *periph;
856 struct da_softc *softc;
858 struct ccb_scsiio csio;
865 softc = (struct da_softc *)periph->softc;
866 cam_periph_lock(periph);
867 secsize = softc->params.secsize;
869 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
870 cam_periph_unlock(periph);
875 periph->flags |= CAM_PERIPH_POLLED;
876 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
877 csio.ccb_h.ccb_state = DA_CCB_DUMP;
878 scsi_read_write(&csio,
884 /*minimum_cmd_size*/ softc->minimum_cmd_size,
887 /*data_ptr*/(u_int8_t *) virtual,
889 /*sense_len*/SSD_FULL_SIZE,
890 DA_DEFAULT_TIMEOUT * 1000);
891 xpt_polled_action((union ccb *)&csio);
893 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
894 printf("Aborting dump due to I/O error.\n");
895 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
896 CAM_SCSI_STATUS_ERROR)
897 scsi_sense_print(&csio);
899 printf("status == 0x%x, scsi status == 0x%x\n",
900 csio.ccb_h.status, csio.scsi_status);
901 periph->flags |= CAM_PERIPH_POLLED;
904 cam_periph_unlock(periph);
909 * Sync the disk cache contents to the physical media.
911 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
913 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
914 csio.ccb_h.ccb_state = DA_CCB_DUMP;
915 scsi_synchronize_cache(&csio,
919 /*begin_lba*/0,/* Cover the whole disk */
923 xpt_polled_action((union ccb *)&csio);
925 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
926 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
927 CAM_SCSI_STATUS_ERROR) {
929 int sense_key, error_code;
931 scsi_extract_sense(&csio.sense_data,
935 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
936 scsi_sense_print(&csio);
938 xpt_print(periph->path, "Synchronize cache "
939 "failed, status == 0x%x, scsi status == "
940 "0x%x\n", csio.ccb_h.status,
945 periph->flags &= ~CAM_PERIPH_POLLED;
946 cam_periph_unlock(periph);
956 * Install a global async callback. This callback will
957 * receive async callbacks like "new device found".
959 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
961 if (status != CAM_REQ_CMP) {
962 printf("da: Failed to attach master async callback "
963 "due to status 0x%x!\n", status);
964 } else if (da_send_ordered) {
966 /* Register our shutdown event handler */
967 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
968 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
969 printf("dainit: shutdown event registration failed!\n");
974 daoninvalidate(struct cam_periph *periph)
976 struct da_softc *softc;
978 softc = (struct da_softc *)periph->softc;
981 * De-register any async callbacks.
983 xpt_register_async(0, daasync, periph, periph->path);
985 softc->flags |= DA_FLAG_PACK_INVALID;
988 * Return all queued I/O with ENXIO.
989 * XXX Handle any transactions queued to the card
990 * with XPT_ABORT_CCB.
992 bioq_flush(&softc->bio_queue, NULL, ENXIO);
994 disk_gone(softc->disk);
995 xpt_print(periph->path, "lost device\n");
999 dacleanup(struct cam_periph *periph)
1001 struct da_softc *softc;
1003 softc = (struct da_softc *)periph->softc;
1005 xpt_print(periph->path, "removing device entry\n");
1006 cam_periph_unlock(periph);
1009 * If we can't free the sysctl tree, oh well...
1011 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1012 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1013 xpt_print(periph->path, "can't remove sysctl context\n");
1016 disk_destroy(softc->disk);
1017 callout_drain(&softc->sendordered_c);
1018 free(softc, M_DEVBUF);
1019 cam_periph_lock(periph);
1023 daasync(void *callback_arg, u_int32_t code,
1024 struct cam_path *path, void *arg)
1026 struct cam_periph *periph;
1028 periph = (struct cam_periph *)callback_arg;
1030 case AC_FOUND_DEVICE:
1032 struct ccb_getdev *cgd;
1035 cgd = (struct ccb_getdev *)arg;
1039 if (cgd->protocol != PROTO_SCSI)
1042 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1043 && SID_TYPE(&cgd->inq_data) != T_RBC
1044 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1048 * Allocate a peripheral instance for
1049 * this device and start the probe
1052 status = cam_periph_alloc(daregister, daoninvalidate,
1054 "da", CAM_PERIPH_BIO,
1055 cgd->ccb_h.path, daasync,
1056 AC_FOUND_DEVICE, cgd);
1058 if (status != CAM_REQ_CMP
1059 && status != CAM_REQ_INPROG)
1060 printf("daasync: Unable to attach to new device "
1061 "due to status 0x%x\n", status);
1067 struct da_softc *softc;
1068 struct ccb_hdr *ccbh;
1070 softc = (struct da_softc *)periph->softc;
1072 * Don't fail on the expected unit attention
1075 softc->flags |= DA_FLAG_RETRY_UA;
1076 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1077 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1081 cam_periph_async(periph, code, path, arg);
1087 dasysctlinit(void *context, int pending)
1089 struct cam_periph *periph;
1090 struct da_softc *softc;
1091 char tmpstr[80], tmpstr2[80];
1093 periph = (struct cam_periph *)context;
1094 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1097 softc = (struct da_softc *)periph->softc;
1098 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1099 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1101 sysctl_ctx_init(&softc->sysctl_ctx);
1102 softc->flags |= DA_FLAG_SCTX_INIT;
1103 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1104 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1105 CTLFLAG_RD, 0, tmpstr);
1106 if (softc->sysctl_tree == NULL) {
1107 printf("dasysctlinit: unable to allocate sysctl tree\n");
1108 cam_periph_release(periph);
1113 * Now register the sysctl handler, so the user can the value on
1116 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1117 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1118 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1119 "Minimum CDB size");
1121 cam_periph_release(periph);
1125 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1129 value = *(int *)arg1;
1131 error = sysctl_handle_int(oidp, &value, 0, req);
1134 || (req->newptr == NULL))
1138 * Acceptable values here are 6, 10, 12 or 16.
1142 else if ((value > 6)
1145 else if ((value > 10)
1148 else if (value > 12)
1151 *(int *)arg1 = value;
1157 daregister(struct cam_periph *periph, void *arg)
1159 struct da_softc *softc;
1160 struct ccb_pathinq cpi;
1161 struct ccb_getdev *cgd;
1165 cgd = (struct ccb_getdev *)arg;
1166 if (periph == NULL) {
1167 printf("daregister: periph was NULL!!\n");
1168 return(CAM_REQ_CMP_ERR);
1172 printf("daregister: no getdev CCB, can't register device\n");
1173 return(CAM_REQ_CMP_ERR);
1176 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
1179 if (softc == NULL) {
1180 printf("daregister: Unable to probe new device. "
1181 "Unable to allocate softc\n");
1182 return(CAM_REQ_CMP_ERR);
1185 LIST_INIT(&softc->pending_ccbs);
1186 softc->state = DA_STATE_PROBE;
1187 bioq_init(&softc->bio_queue);
1188 if (SID_IS_REMOVABLE(&cgd->inq_data))
1189 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1190 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1191 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1193 periph->softc = softc;
1196 * See if this device has any quirks.
1198 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1199 (caddr_t)da_quirk_table,
1200 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1201 sizeof(*da_quirk_table), scsi_inquiry_match);
1204 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1206 softc->quirks = DA_Q_NONE;
1208 /* Check if the SIM does not want 6 byte commands */
1209 bzero(&cpi, sizeof(cpi));
1210 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
1211 cpi.ccb_h.func_code = XPT_PATH_INQ;
1212 xpt_action((union ccb *)&cpi);
1213 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1214 softc->quirks |= DA_Q_NO_6_BYTE;
1216 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1219 * RBC devices don't have to support READ(6), only READ(10).
1221 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1222 softc->minimum_cmd_size = 10;
1224 softc->minimum_cmd_size = 6;
1227 * Load the user's default, if any.
1229 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1230 periph->unit_number);
1231 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1234 * 6, 10, 12 and 16 are the currently permissible values.
1236 if (softc->minimum_cmd_size < 6)
1237 softc->minimum_cmd_size = 6;
1238 else if ((softc->minimum_cmd_size > 6)
1239 && (softc->minimum_cmd_size <= 10))
1240 softc->minimum_cmd_size = 10;
1241 else if ((softc->minimum_cmd_size > 10)
1242 && (softc->minimum_cmd_size <= 12))
1243 softc->minimum_cmd_size = 12;
1244 else if (softc->minimum_cmd_size > 12)
1245 softc->minimum_cmd_size = 16;
1248 * Register this media as a disk
1251 mtx_unlock(periph->sim->mtx);
1252 softc->disk = disk_alloc();
1253 softc->disk->d_open = daopen;
1254 softc->disk->d_close = daclose;
1255 softc->disk->d_strategy = dastrategy;
1256 softc->disk->d_dump = dadump;
1257 softc->disk->d_name = "da";
1258 softc->disk->d_drv1 = periph;
1260 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */
1261 else if (cpi.maxio > MAXPHYS)
1262 softc->disk->d_maxsize = MAXPHYS; /* for safety */
1264 softc->disk->d_maxsize = cpi.maxio;
1265 softc->disk->d_unit = periph->unit_number;
1266 softc->disk->d_flags = 0;
1267 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
1268 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
1269 strlcpy(softc->disk->d_ident, cgd->serial_num,
1270 MIN(sizeof(softc->disk->d_ident), cgd->serial_num_len + 1));
1271 disk_create(softc->disk, DISK_VERSION);
1272 mtx_lock(periph->sim->mtx);
1275 * Add async callbacks for bus reset and
1276 * bus device reset calls. I don't bother
1277 * checking if this fails as, in most cases,
1278 * the system will function just fine without
1279 * them and the only alternative would be to
1280 * not attach the device on failure.
1282 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
1283 daasync, periph, periph->path);
1286 * Take an exclusive refcount on the periph while dastart is called
1287 * to finish the probe. The reference will be dropped in dadone at
1290 (void)cam_periph_hold(periph, PRIBIO);
1291 xpt_schedule(periph, /*priority*/5);
1294 * Schedule a periodic event to occasionally send an
1295 * ordered tag to a device.
1297 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
1298 callout_reset(&softc->sendordered_c,
1299 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
1300 dasendorderedtag, softc);
1302 return(CAM_REQ_CMP);
1306 dastart(struct cam_periph *periph, union ccb *start_ccb)
1308 struct da_softc *softc;
1310 softc = (struct da_softc *)periph->softc;
1312 switch (softc->state) {
1313 case DA_STATE_NORMAL:
1315 /* Pull a buffer from the queue and get going on it */
1319 * See if there is a buf with work for us to do..
1321 bp = bioq_first(&softc->bio_queue);
1322 if (periph->immediate_priority <= periph->pinfo.priority) {
1323 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1324 ("queuing for immediate ccb\n"));
1325 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1326 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1328 periph->immediate_priority = CAM_PRIORITY_NONE;
1329 wakeup(&periph->ccb_list);
1330 } else if (bp == NULL) {
1331 xpt_release_ccb(start_ccb);
1335 bioq_remove(&softc->bio_queue, bp);
1337 if ((softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1338 softc->flags &= ~DA_FLAG_NEED_OTAG;
1339 softc->ordered_tag_count++;
1340 tag_code = MSG_ORDERED_Q_TAG;
1342 tag_code = MSG_SIMPLE_Q_TAG;
1344 switch (bp->bio_cmd) {
1347 scsi_read_write(&start_ccb->csio,
1348 /*retries*/da_retry_count,
1350 /*tag_action*/tag_code,
1351 /*read_op*/bp->bio_cmd == BIO_READ,
1353 softc->minimum_cmd_size,
1354 /*lba*/bp->bio_pblkno,
1355 /*block_count*/bp->bio_bcount /
1356 softc->params.secsize,
1357 /*data_ptr*/ bp->bio_data,
1358 /*dxfer_len*/ bp->bio_bcount,
1359 /*sense_len*/SSD_FULL_SIZE,
1360 /*timeout*/da_default_timeout*1000);
1363 scsi_synchronize_cache(&start_ccb->csio,
1367 /*begin_lba*/0,/* Cover the whole disk */
1370 /*timeout*/da_default_timeout*1000);
1373 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1376 * Block out any asyncronous callbacks
1377 * while we touch the pending ccb list.
1379 LIST_INSERT_HEAD(&softc->pending_ccbs,
1380 &start_ccb->ccb_h, periph_links.le);
1381 softc->outstanding_cmds++;
1383 /* We expect a unit attention from this device */
1384 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1385 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1386 softc->flags &= ~DA_FLAG_RETRY_UA;
1389 start_ccb->ccb_h.ccb_bp = bp;
1390 bp = bioq_first(&softc->bio_queue);
1392 xpt_action(start_ccb);
1396 /* Have more work to do, so ensure we stay scheduled */
1397 xpt_schedule(periph, /* XXX priority */1);
1401 case DA_STATE_PROBE:
1403 struct ccb_scsiio *csio;
1404 struct scsi_read_capacity_data *rcap;
1406 rcap = (struct scsi_read_capacity_data *)
1407 malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
1409 printf("dastart: Couldn't malloc read_capacity data\n");
1410 /* da_free_periph??? */
1413 csio = &start_ccb->csio;
1414 scsi_read_capacity(csio,
1421 start_ccb->ccb_h.ccb_bp = NULL;
1422 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1423 xpt_action(start_ccb);
1426 case DA_STATE_PROBE2:
1428 struct ccb_scsiio *csio;
1429 struct scsi_read_capacity_data_long *rcaplong;
1431 rcaplong = (struct scsi_read_capacity_data_long *)
1432 malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
1433 if (rcaplong == NULL) {
1434 printf("dastart: Couldn't malloc read_capacity data\n");
1435 /* da_free_periph??? */
1438 csio = &start_ccb->csio;
1439 scsi_read_capacity_16(csio,
1442 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1447 /*sense_len*/ SSD_FULL_SIZE,
1449 start_ccb->ccb_h.ccb_bp = NULL;
1450 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1451 xpt_action(start_ccb);
1458 cmd6workaround(union ccb *ccb)
1460 struct scsi_rw_6 cmd6;
1461 struct scsi_rw_10 *cmd10;
1462 struct da_softc *softc;
1466 cdb = ccb->csio.cdb_io.cdb_bytes;
1468 /* Translation only possible if CDB is an array and cmd is R/W6 */
1469 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1470 (*cdb != READ_6 && *cdb != WRITE_6))
1473 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
1474 "increasing minimum_cmd_size to 10.\n");
1475 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1476 softc->minimum_cmd_size = 10;
1478 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1479 cmd10 = (struct scsi_rw_10 *)cdb;
1480 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1482 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1483 cmd10->reserved = 0;
1484 scsi_ulto2b(cmd6.length, cmd10->length);
1485 cmd10->control = cmd6.control;
1486 ccb->csio.cdb_len = sizeof(*cmd10);
1488 /* Requeue request, unfreezing queue if necessary */
1489 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1490 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1493 cam_release_devq(ccb->ccb_h.path,
1497 /*getcount_only*/0);
1503 dadone(struct cam_periph *periph, union ccb *done_ccb)
1505 struct da_softc *softc;
1506 struct ccb_scsiio *csio;
1508 softc = (struct da_softc *)periph->softc;
1509 csio = &done_ccb->csio;
1510 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1511 case DA_CCB_BUFFER_IO:
1515 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1516 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1520 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1525 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1526 if (error == ERESTART) {
1528 * A retry was scheuled, so
1535 if (error == ENXIO) {
1537 * Catastrophic error. Mark our pack as
1541 * XXX See if this is really a media
1544 xpt_print(periph->path,
1545 "Invalidating pack\n");
1546 softc->flags |= DA_FLAG_PACK_INVALID;
1550 * return all queued I/O with EIO, so that
1551 * the client can retry these I/Os in the
1552 * proper order should it attempt to recover.
1554 bioq_flush(&softc->bio_queue, NULL, EIO);
1555 bp->bio_error = error;
1556 bp->bio_resid = bp->bio_bcount;
1557 bp->bio_flags |= BIO_ERROR;
1559 bp->bio_resid = csio->resid;
1561 if (bp->bio_resid != 0)
1562 bp->bio_flags |= BIO_ERROR;
1564 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1565 cam_release_devq(done_ccb->ccb_h.path,
1569 /*getcount_only*/0);
1571 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1572 panic("REQ_CMP with QFRZN");
1573 bp->bio_resid = csio->resid;
1574 if (csio->resid > 0)
1575 bp->bio_flags |= BIO_ERROR;
1579 * Block out any asyncronous callbacks
1580 * while we touch the pending ccb list.
1582 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1583 softc->outstanding_cmds--;
1584 if (softc->outstanding_cmds == 0)
1585 softc->flags |= DA_FLAG_WENT_IDLE;
1593 struct scsi_read_capacity_data *rdcap;
1594 struct scsi_read_capacity_data_long *rcaplong;
1595 char announce_buf[80];
1599 if (softc->state == DA_STATE_PROBE)
1600 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1602 rcaplong = (struct scsi_read_capacity_data_long *)
1605 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1606 struct disk_params *dp;
1607 uint32_t block_size;
1610 if (softc->state == DA_STATE_PROBE) {
1611 block_size = scsi_4btoul(rdcap->length);
1612 maxsector = scsi_4btoul(rdcap->addr);
1615 * According to SBC-2, if the standard 10
1616 * byte READ CAPACITY command returns 2^32,
1617 * we should issue the 16 byte version of
1618 * the command, since the device in question
1619 * has more sectors than can be represented
1620 * with the short version of the command.
1622 if (maxsector == 0xffffffff) {
1623 softc->state = DA_STATE_PROBE2;
1624 free(rdcap, M_SCSIDA);
1625 xpt_release_ccb(done_ccb);
1626 xpt_schedule(periph, /*priority*/5);
1630 block_size = scsi_4btoul(rcaplong->length);
1631 maxsector = scsi_8btou64(rcaplong->addr);
1635 * Because GEOM code just will panic us if we
1636 * give them an 'illegal' value we'll avoid that
1639 if (block_size >= MAXPHYS || block_size == 0) {
1640 xpt_print(periph->path,
1641 "unsupportable block size %ju\n",
1642 (uintmax_t) block_size);
1643 announce_buf[0] = '\0';
1644 cam_periph_invalidate(periph);
1646 dasetgeom(periph, block_size, maxsector);
1647 dp = &softc->params;
1648 snprintf(announce_buf, sizeof(announce_buf),
1649 "%juMB (%ju %u byte sectors: %dH %dS/T "
1651 (((uintmax_t)dp->secsize *
1652 dp->sectors) / (1024*1024)),
1653 (uintmax_t)dp->sectors,
1654 dp->secsize, dp->heads,
1655 dp->secs_per_track, dp->cylinders);
1660 announce_buf[0] = '\0';
1663 * Retry any UNIT ATTENTION type errors. They
1664 * are expected at boot.
1666 error = daerror(done_ccb, CAM_RETRY_SELTO,
1667 SF_RETRY_UA|SF_NO_PRINT);
1668 if (error == ERESTART) {
1670 * A retry was scheuled, so
1674 } else if (error != 0) {
1675 struct scsi_sense_data *sense;
1677 int sense_key, error_code;
1680 struct ccb_getdev cgd;
1682 /* Don't wedge this device's queue */
1683 status = done_ccb->ccb_h.status;
1684 if ((status & CAM_DEV_QFRZN) != 0)
1685 cam_release_devq(done_ccb->ccb_h.path,
1689 /*getcount_only*/0);
1692 xpt_setup_ccb(&cgd.ccb_h,
1693 done_ccb->ccb_h.path,
1695 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1696 xpt_action((union ccb *)&cgd);
1698 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1699 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1700 || ((status & CAM_AUTOSNS_VALID) == 0))
1706 sense = &csio->sense_data;
1707 scsi_extract_sense(sense, &error_code,
1712 * Attach to anything that claims to be a
1713 * direct access or optical disk device,
1714 * as long as it doesn't return a "Logical
1715 * unit not supported" (0x25) error.
1717 if ((have_sense) && (asc != 0x25)
1718 && (error_code == SSD_CURRENT_ERROR)) {
1719 const char *sense_key_desc;
1720 const char *asc_desc;
1722 scsi_sense_desc(sense_key, asc, ascq,
1726 snprintf(announce_buf,
1727 sizeof(announce_buf),
1728 "Attempt to query device "
1729 "size failed: %s, %s",
1737 xpt_print(periph->path,
1738 "got CAM status %#x\n",
1739 done_ccb->ccb_h.status);
1742 xpt_print(periph->path, "fatal error, "
1743 "failed to attach to device\n");
1746 * Free up resources.
1748 cam_periph_invalidate(periph);
1752 free(csio->data_ptr, M_SCSIDA);
1753 if (announce_buf[0] != '\0') {
1754 xpt_announce_periph(periph, announce_buf);
1756 * Create our sysctl variables, now that we know
1757 * we have successfully attached.
1759 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1761 softc->state = DA_STATE_NORMAL;
1763 * Since our peripheral may be invalidated by an error
1764 * above or an external event, we must release our CCB
1765 * before releasing the probe lock on the peripheral.
1766 * The peripheral will only go away once the last lock
1767 * is removed, and we need it around for the CCB release
1770 xpt_release_ccb(done_ccb);
1771 cam_periph_unhold(periph);
1774 case DA_CCB_WAITING:
1776 /* Caller will release the CCB */
1777 wakeup(&done_ccb->ccb_h.cbfcnp);
1781 /* No-op. We're polling */
1786 xpt_release_ccb(done_ccb);
1790 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1792 struct da_softc *softc;
1793 struct cam_periph *periph;
1796 periph = xpt_path_periph(ccb->ccb_h.path);
1797 softc = (struct da_softc *)periph->softc;
1800 * Automatically detect devices that do not support
1801 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
1804 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
1805 error = cmd6workaround(ccb);
1806 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
1807 CAM_SCSI_STATUS_ERROR)
1808 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
1809 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1810 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
1811 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
1812 int sense_key, error_code, asc, ascq;
1814 scsi_extract_sense(&ccb->csio.sense_data,
1815 &error_code, &sense_key, &asc, &ascq);
1816 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
1817 error = cmd6workaround(ccb);
1819 if (error == ERESTART)
1824 * Until we have a better way of doing pack validation,
1825 * don't treat UAs as errors.
1827 sense_flags |= SF_RETRY_UA;
1828 return(cam_periph_error(ccb, cam_flags, sense_flags,
1829 &softc->saved_ccb));
1833 daprevent(struct cam_periph *periph, int action)
1835 struct da_softc *softc;
1839 softc = (struct da_softc *)periph->softc;
1841 if (((action == PR_ALLOW)
1842 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
1843 || ((action == PR_PREVENT)
1844 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
1848 ccb = cam_periph_getccb(periph, /*priority*/1);
1850 scsi_prevent(&ccb->csio,
1858 error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
1859 SF_RETRY_UA, softc->disk->d_devstat);
1862 if (action == PR_ALLOW)
1863 softc->flags &= ~DA_FLAG_PACK_LOCKED;
1865 softc->flags |= DA_FLAG_PACK_LOCKED;
1868 xpt_release_ccb(ccb);
1872 dagetcapacity(struct cam_periph *periph)
1874 struct da_softc *softc;
1876 struct scsi_read_capacity_data *rcap;
1877 struct scsi_read_capacity_data_long *rcaplong;
1881 u_int32_t sense_flags;
1883 softc = (struct da_softc *)periph->softc;
1887 sense_flags = SF_RETRY_UA;
1888 if (softc->flags & DA_FLAG_PACK_REMOVABLE)
1889 sense_flags |= SF_NO_PRINT;
1891 /* Do a read capacity */
1892 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcaplong),
1898 ccb = cam_periph_getccb(periph, /*priority*/1);
1899 scsi_read_capacity(&ccb->csio,
1906 ccb->ccb_h.ccb_bp = NULL;
1908 error = cam_periph_runccb(ccb, daerror,
1909 /*cam_flags*/CAM_RETRY_SELTO,
1911 softc->disk->d_devstat);
1913 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1914 cam_release_devq(ccb->ccb_h.path,
1918 /*getcount_only*/0);
1921 block_len = scsi_4btoul(rcap->length);
1922 maxsector = scsi_4btoul(rcap->addr);
1924 if (maxsector != 0xffffffff)
1929 rcaplong = (struct scsi_read_capacity_data_long *)rcap;
1931 scsi_read_capacity_16(&ccb->csio,
1934 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1939 /*sense_len*/ SSD_FULL_SIZE,
1941 ccb->ccb_h.ccb_bp = NULL;
1943 error = cam_periph_runccb(ccb, daerror,
1944 /*cam_flags*/CAM_RETRY_SELTO,
1946 softc->disk->d_devstat);
1948 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1949 cam_release_devq(ccb->ccb_h.path,
1953 /*getcount_only*/0);
1956 block_len = scsi_4btoul(rcaplong->length);
1957 maxsector = scsi_8btou64(rcaplong->addr);
1963 dasetgeom(periph, block_len, maxsector);
1965 xpt_release_ccb(ccb);
1967 free(rcap, M_SCSIDA);
1973 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector)
1975 struct ccb_calc_geometry ccg;
1976 struct da_softc *softc;
1977 struct disk_params *dp;
1979 softc = (struct da_softc *)periph->softc;
1981 dp = &softc->params;
1982 dp->secsize = block_len;
1983 dp->sectors = maxsector + 1;
1985 * Have the controller provide us with a geometry
1986 * for this disk. The only time the geometry
1987 * matters is when we boot and the controller
1988 * is the only one knowledgeable enough to come
1989 * up with something that will make this a bootable
1992 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
1993 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
1994 ccg.block_size = dp->secsize;
1995 ccg.volume_size = dp->sectors;
1997 ccg.secs_per_track = 0;
1999 xpt_action((union ccb*)&ccg);
2000 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2002 * We don't know what went wrong here- but just pick
2003 * a geometry so we don't have nasty things like divide
2007 dp->secs_per_track = 255;
2008 dp->cylinders = dp->sectors / (255 * 255);
2009 if (dp->cylinders == 0) {
2013 dp->heads = ccg.heads;
2014 dp->secs_per_track = ccg.secs_per_track;
2015 dp->cylinders = ccg.cylinders;
2020 dasendorderedtag(void *arg)
2022 struct da_softc *softc = arg;
2024 if (da_send_ordered) {
2025 if ((softc->ordered_tag_count == 0)
2026 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
2027 softc->flags |= DA_FLAG_NEED_OTAG;
2029 if (softc->outstanding_cmds > 0)
2030 softc->flags &= ~DA_FLAG_WENT_IDLE;
2032 softc->ordered_tag_count = 0;
2034 /* Queue us up again */
2035 callout_reset(&softc->sendordered_c,
2036 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
2037 dasendorderedtag, softc);
2041 * Step through all DA peripheral drivers, and if the device is still open,
2042 * sync the disk cache to physical media.
2045 dashutdown(void * arg, int howto)
2047 struct cam_periph *periph;
2048 struct da_softc *softc;
2050 TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
2053 cam_periph_lock(periph);
2054 softc = (struct da_softc *)periph->softc;
2057 * We only sync the cache if the drive is still open, and
2058 * if the drive is capable of it..
2060 if (((softc->flags & DA_FLAG_OPEN) == 0)
2061 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
2062 cam_periph_unlock(periph);
2066 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
2068 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
2069 scsi_synchronize_cache(&ccb.csio,
2073 /*begin_lba*/0, /* whole disk */
2078 xpt_polled_action(&ccb);
2080 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2081 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2082 CAM_SCSI_STATUS_ERROR)
2083 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
2084 int error_code, sense_key, asc, ascq;
2086 scsi_extract_sense(&ccb.csio.sense_data,
2087 &error_code, &sense_key,
2090 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
2091 scsi_sense_print(&ccb.csio);
2093 xpt_print(periph->path, "Synchronize "
2094 "cache failed, status == 0x%x, scsi status "
2095 "== 0x%x\n", ccb.ccb_h.status,
2096 ccb.csio.scsi_status);
2100 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
2101 cam_release_devq(ccb.ccb_h.path,
2105 /*getcount_only*/0);
2106 cam_periph_unlock(periph);
2110 #else /* !_KERNEL */
2113 * XXX This is only left out of the kernel build to silence warnings. If,
2114 * for some reason this function is used in the kernel, the ifdefs should
2115 * be moved so it is included both in the kernel and userland.
2118 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2119 void (*cbfcnp)(struct cam_periph *, union ccb *),
2120 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2121 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2124 struct scsi_format_unit *scsi_cmd;
2126 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2127 scsi_cmd->opcode = FORMAT_UNIT;
2128 scsi_cmd->byte2 = byte2;
2129 scsi_ulto2b(ileave, scsi_cmd->interleave);
2134 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2143 #endif /* _KERNEL */