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
28 * $FreeBSD: src/sys/cam/scsi/scsi_da.c,v 1.42.2.36 2003/05/17 21:48:30 njl Exp $
29 * $DragonFly: src/sys/bus/cam/scsi/scsi_da.c,v 1.7 2003/07/22 17:03:18 dillon Exp $
33 #include "opt_hw_wdog.h"
36 #include <sys/param.h>
37 #include <sys/bootmaj.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/sysctl.h>
46 #include <sys/devicestat.h>
49 #include <sys/eventhandler.h>
50 #include <sys/malloc.h>
55 #include <machine/md_var.h>
66 #include <cam/cam_ccb.h>
67 #include <cam/cam_extend.h>
68 #include <cam/cam_periph.h>
69 #include <cam/cam_xpt_periph.h>
71 #include <cam/scsi/scsi_message.h>
74 #include <cam/scsi/scsi_da.h>
84 DA_FLAG_PACK_INVALID = 0x001,
85 DA_FLAG_NEW_PACK = 0x002,
86 DA_FLAG_PACK_LOCKED = 0x004,
87 DA_FLAG_PACK_REMOVABLE = 0x008,
88 DA_FLAG_TAGGED_QUEUING = 0x010,
89 DA_FLAG_NEED_OTAG = 0x020,
90 DA_FLAG_WENT_IDLE = 0x040,
91 DA_FLAG_RETRY_UA = 0x080,
97 DA_Q_NO_SYNC_CACHE = 0x01,
103 DA_CCB_BUFFER_IO = 0x02,
104 DA_CCB_WAITING = 0x03,
106 DA_CCB_TYPE_MASK = 0x0F,
107 DA_CCB_RETRY_UA = 0x10
110 /* Offsets into our private area for storing information */
111 #define ccb_state ppriv_field0
112 #define ccb_bp ppriv_ptr1
117 u_int8_t secs_per_track;
118 u_int32_t secsize; /* Number of bytes/sector */
119 u_int32_t sectors; /* total number sectors */
123 struct buf_queue_head buf_queue;
124 struct devstat device_stats;
125 SLIST_ENTRY(da_softc) links;
126 LIST_HEAD(, ccb_hdr) pending_ccbs;
130 int minimum_cmd_size;
131 int ordered_tag_count;
132 struct disk_params params;
137 struct da_quirk_entry {
138 struct scsi_inquiry_pattern inq_pat;
142 static const char quantum[] = "QUANTUM";
143 static const char microp[] = "MICROP";
145 static struct da_quirk_entry da_quirk_table[] =
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
174 * This drive doesn't like the synchronize cache command
175 * either. Reported by: Matthew Jacob <mjacob@feral.com>
176 * in NetBSD PR kern/6027, August 24, 1998.
178 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
179 /*quirks*/ DA_Q_NO_SYNC_CACHE
183 * This drive doesn't like the synchronize cache command
184 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
187 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
188 /*quirks*/ DA_Q_NO_SYNC_CACHE
192 * Doesn't like the synchronize cache command.
193 * Reported by: Blaz Zupan <blaz@gold.amis.net>
195 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
196 /*quirks*/ DA_Q_NO_SYNC_CACHE
200 * Doesn't like the synchronize cache command.
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 work correctly with 6 byte reads/writes.
215 * Returns illegal request, and points to byte 9 of the
217 * Reported by: Adam McDougall <bsdx@spawnet.com>
219 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
220 /*quirks*/ DA_Q_NO_6_BYTE
226 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
227 /*quirks*/ DA_Q_NO_6_BYTE
230 /* Below a list of quirks for USB devices supported by umass. */
233 * This USB floppy drive uses the UFI command set. This
234 * command set is a derivative of the ATAPI command set and
235 * does not support READ_6 commands only READ_10. It also does
236 * not support sync cache (0x35).
238 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Y-E DATA", "USB-FDU", "*"},
239 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
242 /* Another USB floppy */
243 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MATSHITA", "FDD CF-VFDU*","*"},
244 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
248 * Sony Memory Stick adapter MSAC-US1 and
249 * Sony PCG-C1VJ Internal Memory Stick Slot (MSC-U01).
250 * Make all sony MS* products use this quirk.
252 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "MS*", "*"},
253 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
257 * Sony Memory Stick adapter for the CLIE series
260 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "CLIE*", "*"},
261 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
265 * Sony DSC cameras (DSC-S30, DSC-S50, DSC-S70)
267 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
268 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
272 * Maxtor 3000LE USB Drive
274 {T_DIRECT, SIP_MEDIA_FIXED, "MAXTOR*", "K040H2*", "*"},
275 /*quirks*/ DA_Q_NO_6_BYTE
279 * LaCie USB drive, among others
281 {T_DIRECT, SIP_MEDIA_FIXED, "Maxtor*", "D080H4*", "*"},
282 /*quirks*/ DA_Q_NO_6_BYTE
285 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "MCF3064AP", "*"},
286 /*quirks*/ DA_Q_NO_6_BYTE
290 * Microtech USB CameraMate
292 {T_DIRECT, SIP_MEDIA_REMOVABLE, "eUSB Compact*",
293 "Compact Flash*", "*"},
294 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
298 * The vendor, product and version strings coming from the
299 * controller are null terminated instead of being padded with
300 * spaces. The trailing wildcard character '*' is required.
302 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SMSC*", "USB FDC*","*"},
303 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
307 * Olympus digital cameras (C-3040ZOOM, C-2040ZOOM, C-1)
309 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "C-*", "*"},
310 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
314 * Olympus digital cameras (D-370)
316 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "D-*", "*"},
317 /*quirks*/ DA_Q_NO_6_BYTE
321 * Olympus digital cameras (E-100RS, E-10).
323 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "E-*", "*"},
324 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
328 * KingByte Pen Drives
330 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NO BRAND", "PEN DRIVE", "*"},
331 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
337 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJIFILMUSB-DRIVEUNIT",
338 "USB-DRIVEUNIT", "*"},
339 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
343 * Nikon Coolpix E775/E995 Cameras
345 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NIKON", "NIKON DSC E*", "*"},
346 /*quirks*/ DA_Q_NO_6_BYTE
350 * Nikon Coolpix E885 Camera
352 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Nikon", "Digital Camera", "*"},
353 /*quirks*/ DA_Q_NO_6_BYTE
357 * SimpleTech FlashLink UCF-100
359 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OEI-USB", "CompactFlash", "*"},
360 /*quirks*/ DA_Q_NO_6_BYTE
364 * Minolta Dimage 2330
366 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MINOLTA", "DIMAGE 2330*", "*"},
367 /*quirks*/ DA_Q_NO_6_BYTE
371 * Minolta Dimage E203
373 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MINOLTA", "DiMAGE E203", "*"},
374 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
378 * DIVA USB Mp3 Player.
381 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DIVA USB", "Media Reader","*"},
382 /*quirks*/ DA_Q_NO_6_BYTE
386 * Daisy Technology PhotoClip USB Camera
388 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Digital", "World DMC","*"},
389 /*quirks*/ DA_Q_NO_6_BYTE
396 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Apacer", "HandyDrive", "*"},
397 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
401 * Daisy Technology PhotoClip on Zoran chip
404 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ZORAN", "COACH", "*"},
405 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
409 * HP 315 Digital Camera
412 {T_DIRECT, SIP_MEDIA_REMOVABLE, "HP", "USB CAMERA", "*"},
413 /*quirks*/ DA_Q_NO_6_BYTE
417 * Fujitsu-Siemens Memorybird pen drive
420 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Fujitsu", "Memorybird", "*"},
421 /*quirks*/ DA_Q_NO_6_BYTE
425 * Sony USB Key-Storage
428 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Storage Media", "*"},
429 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
433 * Lexar Media Jumpdrive
436 {T_DIRECT, SIP_MEDIA_REMOVABLE, "LEXAR", "DIGITAL FILM", "*"},
437 /*quirks*/ DA_Q_NO_6_BYTE
441 * Pentax USB Optio 230 camera
444 {T_DIRECT, SIP_MEDIA_REMOVABLE,
445 "PENTAX", "DIGITAL_CAMERA", "*"},
446 /*quirks*/ DA_Q_NO_6_BYTE
450 * Casio QV-R3 USB camera (uses Pentax chip as above)
453 {T_DIRECT, SIP_MEDIA_REMOVABLE,
454 "CASIO", "DIGITAL_CAMERA", "*"},
455 /*quirks*/ DA_Q_NO_6_BYTE
459 * M-Systems DiskOnKey USB flash key
462 {T_DIRECT, SIP_MEDIA_REMOVABLE, "M-Sys", "DiskOnKey", "*"},
463 /*quirks*/ DA_Q_NO_6_BYTE
467 * SanDisk ImageMate (I, II, ...) compact flash
470 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk", "ImageMate*", "*"},
471 /*quirks*/ DA_Q_NO_6_BYTE
475 * Feiya "slider" dual-slot flash reader. The vendor field
476 * is blank so this may match other devices.
479 {T_DIRECT, SIP_MEDIA_REMOVABLE, "", "USB CARD READER", "*"},
480 /*quirks*/ DA_Q_NO_6_BYTE
484 * SmartDisk (Mitsumi) USB floppy drive
487 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MITSUMI", "USB FDD", "*"},
488 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
495 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OTi", "Flash Disk", "*"},
496 /*quirks*/ DA_Q_NO_6_BYTE
500 static d_open_t daopen;
501 static d_close_t daclose;
502 static d_strategy_t dastrategy;
503 static d_ioctl_t daioctl;
504 static d_dump_t dadump;
505 static periph_init_t dainit;
506 static void daasync(void *callback_arg, u_int32_t code,
507 struct cam_path *path, void *arg);
508 static periph_ctor_t daregister;
509 static periph_dtor_t dacleanup;
510 static periph_start_t dastart;
511 static periph_oninv_t daoninvalidate;
512 static void dadone(struct cam_periph *periph,
513 union ccb *done_ccb);
514 static int daerror(union ccb *ccb, u_int32_t cam_flags,
515 u_int32_t sense_flags);
516 static void daprevent(struct cam_periph *periph, int action);
517 static void dasetgeom(struct cam_periph *periph,
518 struct scsi_read_capacity_data * rdcap);
519 static timeout_t dasendorderedtag;
520 static void dashutdown(void *arg, int howto);
522 #ifndef DA_DEFAULT_TIMEOUT
523 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
526 #ifndef DA_DEFAULT_RETRY
527 #define DA_DEFAULT_RETRY 4
530 static int da_retry_count = DA_DEFAULT_RETRY;
531 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
532 static int da_no_6_byte = 0;
534 SYSCTL_NODE(_kern, OID_AUTO, cam, CTLFLAG_RD, 0, "CAM Subsystem");
535 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
536 "CAM Direct Access Disk driver");
537 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
538 &da_retry_count, 0, "Normal I/O retry count");
539 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
540 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
541 SYSCTL_INT(_kern_cam_da, OID_AUTO, no_6_byte, CTLFLAG_RW,
542 &da_no_6_byte, 0, "No 6 bytes commands");
545 * DA_ORDEREDTAG_INTERVAL determines how often, relative
546 * to the default timeout, we check to see whether an ordered
547 * tagged transaction is appropriate to prevent simple tag
548 * starvation. Since we'd like to ensure that there is at least
549 * 1/2 of the timeout length left for a starved transaction to
550 * complete after we've sent an ordered tag, we must poll at least
551 * four times in every timeout period. This takes care of the worst
552 * case where a starved transaction starts during an interval that
553 * meets the requirement "don't send an ordered tag" test so it takes
554 * us two intervals to determine that a tag must be sent.
556 #ifndef DA_ORDEREDTAG_INTERVAL
557 #define DA_ORDEREDTAG_INTERVAL 4
560 static struct periph_driver dadriver =
563 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
566 DATA_SET(periphdriver_set, dadriver);
568 /* For 2.2-stable support */
573 static struct cdevsw da_cdevsw = {
575 /* maj */ DA_CDEV_MAJOR,
583 /* write */ physwrite,
587 /* strategy */ dastrategy,
592 static SLIST_HEAD(,da_softc) softc_list;
593 static struct extend_array *daperiphs;
596 daopen(dev_t dev, int flags, int fmt, struct thread *td)
598 struct cam_periph *periph;
599 struct da_softc *softc;
600 struct disklabel *label;
608 periph = cam_extend_get(daperiphs, unit);
612 softc = (struct da_softc *)periph->softc;
614 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
615 ("daopen: dev=%s (unit %d , partition %d)\n", devtoname(dev),
618 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
619 return (error); /* error code from tsleep */
622 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
624 softc->flags |= DA_FLAG_OPEN;
627 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
628 /* Invalidate our pack information. */
629 disk_invalidate(&softc->disk);
630 softc->flags &= ~DA_FLAG_PACK_INVALID;
634 /* Do a read capacity */
636 struct scsi_read_capacity_data *rcap;
639 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
643 ccb = cam_periph_getccb(periph, /*priority*/1);
644 scsi_read_capacity(&ccb->csio,
651 ccb->ccb_h.ccb_bp = NULL;
653 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
654 /*sense_flags*/SF_RETRY_UA |
656 &softc->device_stats);
658 xpt_release_ccb(ccb);
661 dasetgeom(periph, rcap);
668 struct ccb_getdev cgd;
670 /* Build label for whole disk. */
671 label = &softc->disk.d_label;
672 bzero(label, sizeof(*label));
673 label->d_type = DTYPE_SCSI;
676 * Grab the inquiry data to get the vendor and product names.
677 * Put them in the typename and packname for the label.
679 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
680 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
681 xpt_action((union ccb *)&cgd);
683 strncpy(label->d_typename, cgd.inq_data.vendor,
684 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
685 strncpy(label->d_packname, cgd.inq_data.product,
686 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
688 label->d_secsize = softc->params.secsize;
689 label->d_nsectors = softc->params.secs_per_track;
690 label->d_ntracks = softc->params.heads;
691 label->d_ncylinders = softc->params.cylinders;
692 label->d_secpercyl = softc->params.heads
693 * softc->params.secs_per_track;
694 label->d_secperunit = softc->params.sectors;
696 if (((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0)) {
697 daprevent(periph, PR_PREVENT);
701 * Check to see whether or not the blocksize is set yet.
702 * If it isn't, set it and then clear the blocksize
703 * unavailable flag for the device statistics.
705 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0){
706 softc->device_stats.block_size = softc->params.secsize;
707 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
712 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
713 daprevent(periph, PR_ALLOW);
715 softc->flags &= ~DA_FLAG_OPEN;
716 cam_periph_release(periph);
718 cam_periph_unlock(periph);
723 daclose(dev_t dev, int flag, int fmt, struct thread *td)
725 struct cam_periph *periph;
726 struct da_softc *softc;
731 periph = cam_extend_get(daperiphs, unit);
735 softc = (struct da_softc *)periph->softc;
737 if ((error = cam_periph_lock(periph, 0)) != 0) {
738 return (error); /* error code from tsleep */
741 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
744 ccb = cam_periph_getccb(periph, /*priority*/1);
746 scsi_synchronize_cache(&ccb->csio,
750 /*begin_lba*/0,/* Cover the whole disk */
755 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
756 /*sense_flags*/SF_RETRY_UA,
757 &softc->device_stats);
759 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
760 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
761 CAM_SCSI_STATUS_ERROR) {
763 int sense_key, error_code;
765 scsi_extract_sense(&ccb->csio.sense_data,
769 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
770 scsi_sense_print(&ccb->csio);
772 xpt_print_path(periph->path);
773 printf("Synchronize cache failed, status "
774 "== 0x%x, scsi status == 0x%x\n",
775 ccb->csio.ccb_h.status,
776 ccb->csio.scsi_status);
780 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
781 cam_release_devq(ccb->ccb_h.path,
787 xpt_release_ccb(ccb);
791 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
792 daprevent(periph, PR_ALLOW);
794 * If we've got removeable media, mark the blocksize as
795 * unavailable, since it could change when new media is
798 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
801 softc->flags &= ~DA_FLAG_OPEN;
802 cam_periph_unlock(periph);
803 cam_periph_release(periph);
808 * Actually translate the requested transfer into one the physical driver
809 * can understand. The transfer is described by a buf and will include
810 * only one physical transfer.
813 dastrategy(struct buf *bp)
815 struct cam_periph *periph;
816 struct da_softc *softc;
821 unit = dkunit(bp->b_dev);
822 part = dkpart(bp->b_dev);
823 periph = cam_extend_get(daperiphs, unit);
824 if (periph == NULL) {
828 softc = (struct da_softc *)periph->softc;
831 * check it's not too big a transfer for our adapter
833 scsi_minphys(bp,&sd_switch);
837 * Mask interrupts so that the pack cannot be invalidated until
838 * after we are in the queue. Otherwise, we might not properly
839 * clean up one of the buffers.
844 * If the device has been made invalid, error out
846 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
853 * Place it in the queue of disk activities for this disk
855 bufqdisksort(&softc->buf_queue, bp);
860 * Schedule ourselves for performing the work.
862 xpt_schedule(periph, /* XXX priority */1);
866 bp->b_flags |= B_ERROR;
869 * Correctly set the buf to indicate a completed xfer
871 bp->b_resid = bp->b_bcount;
876 /* For 2.2-stable support */
882 daioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
884 struct cam_periph *periph;
885 struct da_softc *softc;
890 periph = cam_extend_get(daperiphs, unit);
894 softc = (struct da_softc *)periph->softc;
896 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("daioctl\n"));
898 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
899 return (error); /* error code from tsleep */
902 error = cam_periph_ioctl(periph, cmd, addr, daerror);
904 cam_periph_unlock(periph);
912 struct cam_periph *periph;
913 struct da_softc *softc;
917 u_int num; /* number of sectors to write */
921 struct ccb_scsiio csio;
922 int dumppages = MAXDUMPPGS;
926 /* toss any characters present prior to dump */
927 while (cncheckc() != -1)
932 periph = cam_extend_get(daperiphs, unit);
933 if (periph == NULL) {
936 softc = (struct da_softc *)periph->softc;
938 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0)
941 error = disk_dumpcheck(dev, &num, &blknum, &secsize);
945 addr = 0; /* starting address */
946 blkcnt = howmany(PAGE_SIZE, secsize);
951 if ((num / blkcnt) < dumppages)
952 dumppages = num / blkcnt;
954 for (i = 0; i < dumppages; ++i) {
955 vm_offset_t a = addr + (i * PAGE_SIZE);
956 if (is_physical_memory(a))
957 va = pmap_kenter_temporary(trunc_page(a), i);
959 va = pmap_kenter_temporary(trunc_page(0), i);
962 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
963 csio.ccb_h.ccb_state = DA_CCB_DUMP;
964 scsi_read_write(&csio,
970 /*minimum_cmd_size*/ softc->minimum_cmd_size,
973 /*data_ptr*/(u_int8_t *) va,
974 /*dxfer_len*/blkcnt * secsize * dumppages,
975 /*sense_len*/SSD_FULL_SIZE,
976 DA_DEFAULT_TIMEOUT * 1000);
977 xpt_polled_action((union ccb *)&csio);
979 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
980 printf("Aborting dump due to I/O error.\n");
981 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
982 CAM_SCSI_STATUS_ERROR)
983 scsi_sense_print(&csio);
985 printf("status == 0x%x, scsi status == 0x%x\n",
986 csio.ccb_h.status, csio.scsi_status);
990 if (dumpstatus(addr, (off_t)num * softc->params.secsize) < 0)
993 /* update block count */
994 num -= blkcnt * dumppages;
995 blknum += blkcnt * dumppages;
996 addr += PAGE_SIZE * dumppages;
1000 * Sync the disk cache contents to the physical media.
1002 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1004 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
1005 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1006 scsi_synchronize_cache(&csio,
1010 /*begin_lba*/0,/* Cover the whole disk */
1014 xpt_polled_action((union ccb *)&csio);
1016 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1017 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
1018 CAM_SCSI_STATUS_ERROR) {
1020 int sense_key, error_code;
1022 scsi_extract_sense(&csio.sense_data,
1026 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1027 scsi_sense_print(&csio);
1029 xpt_print_path(periph->path);
1030 printf("Synchronize cache failed, status "
1031 "== 0x%x, scsi status == 0x%x\n",
1032 csio.ccb_h.status, csio.scsi_status);
1043 struct cam_path *path;
1046 * Create our extend array for storing the devices we attach to.
1048 daperiphs = cam_extend_new();
1049 SLIST_INIT(&softc_list);
1050 if (daperiphs == NULL) {
1051 printf("da: Failed to alloc extend array!\n");
1056 * Install a global async callback. This callback will
1057 * receive async callbacks like "new device found".
1059 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
1060 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1062 if (status == CAM_REQ_CMP) {
1063 struct ccb_setasync csa;
1065 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
1066 csa.ccb_h.func_code = XPT_SASYNC_CB;
1067 csa.event_enable = AC_FOUND_DEVICE;
1068 csa.callback = daasync;
1069 csa.callback_arg = NULL;
1070 xpt_action((union ccb *)&csa);
1071 status = csa.ccb_h.status;
1072 xpt_free_path(path);
1075 if (status != CAM_REQ_CMP) {
1076 printf("da: Failed to attach master async callback "
1077 "due to status 0x%x!\n", status);
1081 * Schedule a periodic event to occasionally send an
1082 * ordered tag to a device.
1084 timeout(dasendorderedtag, NULL,
1085 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL);
1087 /* Register our shutdown event handler */
1088 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1089 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1090 printf("dainit: shutdown event registration failed!\n");
1095 daoninvalidate(struct cam_periph *periph)
1098 struct da_softc *softc;
1100 struct ccb_setasync csa;
1102 softc = (struct da_softc *)periph->softc;
1105 * De-register any async callbacks.
1107 xpt_setup_ccb(&csa.ccb_h, periph->path,
1109 csa.ccb_h.func_code = XPT_SASYNC_CB;
1110 csa.event_enable = 0;
1111 csa.callback = daasync;
1112 csa.callback_arg = periph;
1113 xpt_action((union ccb *)&csa);
1115 softc->flags |= DA_FLAG_PACK_INVALID;
1118 * Although the oninvalidate() routines are always called at
1119 * splsoftcam, we need to be at splbio() here to keep the buffer
1120 * queue from being modified while we traverse it.
1125 * Return all queued I/O with ENXIO.
1126 * XXX Handle any transactions queued to the card
1127 * with XPT_ABORT_CCB.
1129 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
1130 bufq_remove(&softc->buf_queue, q_bp);
1131 q_bp->b_resid = q_bp->b_bcount;
1132 q_bp->b_error = ENXIO;
1133 q_bp->b_flags |= B_ERROR;
1138 SLIST_REMOVE(&softc_list, softc, da_softc, links);
1140 xpt_print_path(periph->path);
1141 printf("lost device\n");
1145 dacleanup(struct cam_periph *periph)
1147 struct da_softc *softc;
1149 softc = (struct da_softc *)periph->softc;
1151 devstat_remove_entry(&softc->device_stats);
1152 cam_extend_release(daperiphs, periph->unit_number);
1153 xpt_print_path(periph->path);
1154 printf("removing device entry\n");
1155 if (softc->disk.d_dev) {
1156 disk_destroy(&softc->disk);
1158 free(softc, M_DEVBUF);
1162 daasync(void *callback_arg, u_int32_t code,
1163 struct cam_path *path, void *arg)
1165 struct cam_periph *periph;
1167 periph = (struct cam_periph *)callback_arg;
1169 case AC_FOUND_DEVICE:
1171 struct ccb_getdev *cgd;
1174 cgd = (struct ccb_getdev *)arg;
1176 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1177 && SID_TYPE(&cgd->inq_data) != T_RBC
1178 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1182 * Allocate a peripheral instance for
1183 * this device and start the probe
1186 status = cam_periph_alloc(daregister, daoninvalidate,
1188 "da", CAM_PERIPH_BIO,
1189 cgd->ccb_h.path, daasync,
1190 AC_FOUND_DEVICE, cgd);
1192 if (status != CAM_REQ_CMP
1193 && status != CAM_REQ_INPROG)
1194 printf("daasync: Unable to attach to new device "
1195 "due to status 0x%x\n", status);
1201 struct da_softc *softc;
1202 struct ccb_hdr *ccbh;
1205 softc = (struct da_softc *)periph->softc;
1208 * Don't fail on the expected unit attention
1211 softc->flags |= DA_FLAG_RETRY_UA;
1212 for (ccbh = LIST_FIRST(&softc->pending_ccbs);
1213 ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le))
1214 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1219 cam_periph_async(periph, code, path, arg);
1225 daregister(struct cam_periph *periph, void *arg)
1228 struct da_softc *softc;
1229 struct ccb_setasync csa;
1230 struct ccb_getdev *cgd;
1233 cgd = (struct ccb_getdev *)arg;
1234 if (periph == NULL) {
1235 printf("daregister: periph was NULL!!\n");
1236 return(CAM_REQ_CMP_ERR);
1240 printf("daregister: no getdev CCB, can't register device\n");
1241 return(CAM_REQ_CMP_ERR);
1244 softc = (struct da_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
1246 if (softc == NULL) {
1247 printf("daregister: Unable to probe new device. "
1248 "Unable to allocate softc\n");
1249 return(CAM_REQ_CMP_ERR);
1252 bzero(softc, sizeof(*softc));
1253 LIST_INIT(&softc->pending_ccbs);
1254 softc->state = DA_STATE_PROBE;
1255 bufq_init(&softc->buf_queue);
1256 if (SID_IS_REMOVABLE(&cgd->inq_data))
1257 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1258 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1259 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1261 periph->softc = softc;
1263 cam_extend_set(daperiphs, periph->unit_number, periph);
1266 * See if this device has any quirks.
1268 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1269 (caddr_t)da_quirk_table,
1270 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1271 sizeof(*da_quirk_table), scsi_inquiry_match);
1274 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1276 softc->quirks = DA_Q_NONE;
1278 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1279 softc->minimum_cmd_size = 10;
1281 softc->minimum_cmd_size = 6;
1284 * Block our timeout handler while we
1285 * add this softc to the dev list.
1288 SLIST_INSERT_HEAD(&softc_list, softc, links);
1292 * The DA driver supports a blocksize, but
1293 * we don't know the blocksize until we do
1294 * a read capacity. So, set a flag to
1295 * indicate that the blocksize is
1296 * unavailable right now. We'll clear the
1297 * flag as soon as we've done a read capacity.
1299 devstat_add_entry(&softc->device_stats, "da",
1300 periph->unit_number, 0,
1301 DEVSTAT_BS_UNAVAILABLE,
1302 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
1303 DEVSTAT_PRIORITY_DISK);
1306 * Register this media as a disk
1308 disk_create(periph->unit_number, &softc->disk, 0, &da_cdevsw);
1311 * Add async callbacks for bus reset and
1312 * bus device reset calls. I don't bother
1313 * checking if this fails as, in most cases,
1314 * the system will function just fine without
1315 * them and the only alternative would be to
1316 * not attach the device on failure.
1318 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
1319 csa.ccb_h.func_code = XPT_SASYNC_CB;
1320 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
1321 csa.callback = daasync;
1322 csa.callback_arg = periph;
1323 xpt_action((union ccb *)&csa);
1325 * Lock this peripheral until we are setup.
1326 * This first call can't block
1328 (void)cam_periph_lock(periph, 0);
1329 xpt_schedule(periph, /*priority*/5);
1331 return(CAM_REQ_CMP);
1335 dastart(struct cam_periph *periph, union ccb *start_ccb)
1337 struct da_softc *softc;
1339 softc = (struct da_softc *)periph->softc;
1342 switch (softc->state) {
1343 case DA_STATE_NORMAL:
1345 /* Pull a buffer from the queue and get going on it */
1350 * See if there is a buf with work for us to do..
1353 bp = bufq_first(&softc->buf_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;
1362 wakeup(&periph->ccb_list);
1363 } else if (bp == NULL) {
1365 xpt_release_ccb(start_ccb);
1370 bufq_remove(&softc->buf_queue, bp);
1372 devstat_start_transaction(&softc->device_stats);
1374 if ((bp->b_flags & B_ORDERED) != 0
1375 || (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1376 softc->flags &= ~DA_FLAG_NEED_OTAG;
1377 softc->ordered_tag_count++;
1378 tag_code = MSG_ORDERED_Q_TAG;
1380 tag_code = MSG_SIMPLE_Q_TAG;
1382 if (da_no_6_byte && softc->minimum_cmd_size == 6)
1383 softc->minimum_cmd_size = 10;
1384 scsi_read_write(&start_ccb->csio,
1385 /*retries*/da_retry_count,
1388 bp->b_flags & B_READ,
1390 softc->minimum_cmd_size,
1392 bp->b_bcount / softc->params.secsize,
1395 /*sense_len*/SSD_FULL_SIZE,
1396 da_default_timeout * 1000);
1397 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1400 * Block out any asyncronous callbacks
1401 * while we touch the pending ccb list.
1404 LIST_INSERT_HEAD(&softc->pending_ccbs,
1405 &start_ccb->ccb_h, periph_links.le);
1408 /* We expect a unit attention from this device */
1409 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1410 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1411 softc->flags &= ~DA_FLAG_RETRY_UA;
1414 start_ccb->ccb_h.ccb_bp = bp;
1415 bp = bufq_first(&softc->buf_queue);
1418 xpt_action(start_ccb);
1422 /* Have more work to do, so ensure we stay scheduled */
1423 xpt_schedule(periph, /* XXX priority */1);
1427 case DA_STATE_PROBE:
1429 struct ccb_scsiio *csio;
1430 struct scsi_read_capacity_data *rcap;
1432 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
1436 printf("dastart: Couldn't malloc read_capacity data\n");
1437 /* da_free_periph??? */
1440 csio = &start_ccb->csio;
1441 scsi_read_capacity(csio,
1448 start_ccb->ccb_h.ccb_bp = NULL;
1449 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1450 xpt_action(start_ccb);
1457 cmd6workaround(union ccb *ccb)
1459 struct scsi_rw_6 cmd6;
1460 struct scsi_rw_10 *cmd10;
1461 struct da_softc *softc;
1465 cdb = ccb->csio.cdb_io.cdb_bytes;
1467 /* Translation only possible if CDB is an array and cmd is R/W6 */
1468 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1469 (*cdb != READ_6 && *cdb != WRITE_6))
1472 xpt_print_path(ccb->ccb_h.path);
1473 printf("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:
1516 bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
1517 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1522 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1527 /* Retry selection timeouts */
1528 sf |= SF_RETRY_SELTO;
1530 if ((error = daerror(done_ccb, 0, sf)) == ERESTART) {
1532 * A retry was scheuled, so
1542 if (error == ENXIO) {
1544 * Catastrophic error. Mark our pack as
1547 /* XXX See if this is really a media
1550 xpt_print_path(periph->path);
1551 printf("Invalidating pack\n");
1552 softc->flags |= DA_FLAG_PACK_INVALID;
1556 * return all queued I/O with EIO, so that
1557 * the client can retry these I/Os in the
1558 * proper order should it attempt to recover.
1560 while ((q_bp = bufq_first(&softc->buf_queue))
1562 bufq_remove(&softc->buf_queue, q_bp);
1563 q_bp->b_resid = q_bp->b_bcount;
1564 q_bp->b_error = EIO;
1565 q_bp->b_flags |= B_ERROR;
1569 bp->b_error = error;
1570 bp->b_resid = bp->b_bcount;
1571 bp->b_flags |= B_ERROR;
1573 bp->b_resid = csio->resid;
1575 if (bp->b_resid != 0) {
1576 /* Short transfer ??? */
1578 if (cmd6workaround(done_ccb)
1582 bp->b_flags |= B_ERROR;
1585 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1586 cam_release_devq(done_ccb->ccb_h.path,
1590 /*getcount_only*/0);
1592 bp->b_resid = csio->resid;
1593 if (csio->resid > 0) {
1594 /* Short transfer ??? */
1595 #if 0 /* XXX most of the broken umass devices need this ad-hoc work around */
1596 if (cmd6workaround(done_ccb) == ERESTART)
1599 bp->b_flags |= B_ERROR;
1604 * Block out any asyncronous callbacks
1605 * while we touch the pending ccb list.
1608 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1611 if (softc->device_stats.busy_count == 0)
1612 softc->flags |= DA_FLAG_WENT_IDLE;
1614 devstat_end_transaction_buf(&softc->device_stats, bp);
1620 struct scsi_read_capacity_data *rdcap;
1621 char announce_buf[80];
1623 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1625 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1626 struct disk_params *dp;
1628 dasetgeom(periph, rdcap);
1629 dp = &softc->params;
1630 snprintf(announce_buf, sizeof(announce_buf),
1631 "%luMB (%u %u byte sectors: %dH %dS/T %dC)",
1632 (unsigned long) (((u_int64_t)dp->secsize *
1633 dp->sectors) / (1024*1024)), dp->sectors,
1634 dp->secsize, dp->heads, dp->secs_per_track,
1639 announce_buf[0] = '\0';
1642 * Retry any UNIT ATTENTION type errors. They
1643 * are expected at boot.
1645 error = daerror(done_ccb, 0, SF_RETRY_UA |
1646 SF_RETRY_SELTO | SF_NO_PRINT);
1647 if (error == ERESTART) {
1649 * A retry was scheuled, so
1653 } else if (error != 0) {
1654 struct scsi_sense_data *sense;
1656 int sense_key, error_code;
1659 struct ccb_getdev cgd;
1661 /* Don't wedge this device's queue */
1662 cam_release_devq(done_ccb->ccb_h.path,
1666 /*getcount_only*/0);
1668 status = done_ccb->ccb_h.status;
1670 xpt_setup_ccb(&cgd.ccb_h,
1671 done_ccb->ccb_h.path,
1673 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1674 xpt_action((union ccb *)&cgd);
1676 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1677 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1678 || ((status & CAM_AUTOSNS_VALID) == 0))
1684 sense = &csio->sense_data;
1685 scsi_extract_sense(sense, &error_code,
1690 * Attach to anything that claims to be a
1691 * direct access or optical disk device,
1692 * as long as it doesn't return a "Logical
1693 * unit not supported" (0x25) error.
1695 if ((have_sense) && (asc != 0x25)
1696 && (error_code == SSD_CURRENT_ERROR))
1697 snprintf(announce_buf,
1698 sizeof(announce_buf),
1699 "Attempt to query device "
1700 "size failed: %s, %s",
1701 scsi_sense_key_text[sense_key],
1702 scsi_sense_desc(asc,ascq,
1709 xpt_print_path(periph->path);
1710 printf("got CAM status %#x\n",
1711 done_ccb->ccb_h.status);
1714 xpt_print_path(periph->path);
1715 printf("fatal error, failed"
1716 " to attach to device\n");
1719 * Free up resources.
1721 cam_periph_invalidate(periph);
1725 free(rdcap, M_TEMP);
1726 if (announce_buf[0] != '\0')
1727 xpt_announce_periph(periph, announce_buf);
1728 softc->state = DA_STATE_NORMAL;
1730 * Since our peripheral may be invalidated by an error
1731 * above or an external event, we must release our CCB
1732 * before releasing the probe lock on the peripheral.
1733 * The peripheral will only go away once the last lock
1734 * is removed, and we need it around for the CCB release
1737 xpt_release_ccb(done_ccb);
1738 cam_periph_unlock(periph);
1741 case DA_CCB_WAITING:
1743 /* Caller will release the CCB */
1744 wakeup(&done_ccb->ccb_h.cbfcnp);
1748 /* No-op. We're polling */
1753 xpt_release_ccb(done_ccb);
1757 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1759 struct da_softc *softc;
1760 struct cam_periph *periph;
1761 int error, sense_key, error_code, asc, ascq;
1763 periph = xpt_path_periph(ccb->ccb_h.path);
1764 softc = (struct da_softc *)periph->softc;
1767 * Automatically detect devices that do not support
1768 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
1771 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR
1772 && ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND) {
1773 scsi_extract_sense(&ccb->csio.sense_data,
1774 &error_code, &sense_key, &asc, &ascq);
1775 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
1776 error = cmd6workaround(ccb);
1778 if (error == ERESTART)
1783 * Until we have a better way of doing pack validation,
1784 * don't treat UAs as errors.
1786 sense_flags |= SF_RETRY_UA;
1787 return(cam_periph_error(ccb, cam_flags, sense_flags,
1788 &softc->saved_ccb));
1792 daprevent(struct cam_periph *periph, int action)
1794 struct da_softc *softc;
1798 softc = (struct da_softc *)periph->softc;
1800 if (((action == PR_ALLOW)
1801 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
1802 || ((action == PR_PREVENT)
1803 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
1807 ccb = cam_periph_getccb(periph, /*priority*/1);
1809 scsi_prevent(&ccb->csio,
1817 error = cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
1818 /*sense_flags*/0, &softc->device_stats);
1821 if (action == PR_ALLOW)
1822 softc->flags &= ~DA_FLAG_PACK_LOCKED;
1824 softc->flags |= DA_FLAG_PACK_LOCKED;
1827 xpt_release_ccb(ccb);
1831 dasetgeom(struct cam_periph *periph, struct scsi_read_capacity_data * rdcap)
1833 struct ccb_calc_geometry ccg;
1834 struct da_softc *softc;
1835 struct disk_params *dp;
1837 softc = (struct da_softc *)periph->softc;
1839 dp = &softc->params;
1840 dp->secsize = scsi_4btoul(rdcap->length);
1841 dp->sectors = scsi_4btoul(rdcap->addr) + 1;
1843 * Have the controller provide us with a geometry
1844 * for this disk. The only time the geometry
1845 * matters is when we boot and the controller
1846 * is the only one knowledgeable enough to come
1847 * up with something that will make this a bootable
1850 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
1851 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
1852 ccg.block_size = dp->secsize;
1853 ccg.volume_size = dp->sectors;
1855 ccg.secs_per_track = 0;
1857 xpt_action((union ccb*)&ccg);
1858 dp->heads = ccg.heads;
1859 dp->secs_per_track = ccg.secs_per_track;
1860 dp->cylinders = ccg.cylinders;
1864 dasendorderedtag(void *arg)
1866 struct da_softc *softc;
1869 for (softc = SLIST_FIRST(&softc_list);
1871 softc = SLIST_NEXT(softc, links)) {
1873 if ((softc->ordered_tag_count == 0)
1874 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
1875 softc->flags |= DA_FLAG_NEED_OTAG;
1877 if (softc->device_stats.busy_count > 0)
1878 softc->flags &= ~DA_FLAG_WENT_IDLE;
1880 softc->ordered_tag_count = 0;
1883 /* Queue us up again */
1884 timeout(dasendorderedtag, NULL,
1885 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL);
1889 * Step through all DA peripheral drivers, and if the device is still open,
1890 * sync the disk cache to physical media.
1893 dashutdown(void * arg, int howto)
1895 struct cam_periph *periph;
1896 struct da_softc *softc;
1898 for (periph = TAILQ_FIRST(&dadriver.units); periph != NULL;
1899 periph = TAILQ_NEXT(periph, unit_links)) {
1901 softc = (struct da_softc *)periph->softc;
1904 * We only sync the cache if the drive is still open, and
1905 * if the drive is capable of it..
1907 if (((softc->flags & DA_FLAG_OPEN) == 0)
1908 || (softc->quirks & DA_Q_NO_SYNC_CACHE))
1911 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
1913 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
1914 scsi_synchronize_cache(&ccb.csio,
1918 /*begin_lba*/0, /* whole disk */
1923 xpt_polled_action(&ccb);
1925 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1926 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
1927 CAM_SCSI_STATUS_ERROR)
1928 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
1929 int error_code, sense_key, asc, ascq;
1931 scsi_extract_sense(&ccb.csio.sense_data,
1932 &error_code, &sense_key,
1935 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1936 scsi_sense_print(&ccb.csio);
1938 xpt_print_path(periph->path);
1939 printf("Synchronize cache failed, status "
1940 "== 0x%x, scsi status == 0x%x\n",
1941 ccb.ccb_h.status, ccb.csio.scsi_status);
1945 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1946 cam_release_devq(ccb.ccb_h.path,
1950 /*getcount_only*/0);
1955 #else /* !_KERNEL */
1958 * XXX This is only left out of the kernel build to silence warnings. If,
1959 * for some reason this function is used in the kernel, the ifdefs should
1960 * be moved so it is included both in the kernel and userland.
1963 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
1964 void (*cbfcnp)(struct cam_periph *, union ccb *),
1965 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
1966 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
1969 struct scsi_format_unit *scsi_cmd;
1971 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
1972 scsi_cmd->opcode = FORMAT_UNIT;
1973 scsi_cmd->byte2 = byte2;
1974 scsi_ulto2b(ileave, scsi_cmd->interleave);
1979 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
1988 #endif /* _KERNEL */