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.10 2003/11/21 22:46:13 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>
51 #include <sys/malloc.h>
57 #include <machine/md_var.h>
70 #include "../cam_ccb.h"
71 #include "../cam_extend.h"
72 #include "../cam_periph.h"
73 #include "../cam_xpt_periph.h"
75 #include "scsi_message.h"
88 DA_FLAG_PACK_INVALID = 0x001,
89 DA_FLAG_NEW_PACK = 0x002,
90 DA_FLAG_PACK_LOCKED = 0x004,
91 DA_FLAG_PACK_REMOVABLE = 0x008,
92 DA_FLAG_TAGGED_QUEUING = 0x010,
93 DA_FLAG_NEED_OTAG = 0x020,
94 DA_FLAG_WENT_IDLE = 0x040,
95 DA_FLAG_RETRY_UA = 0x080,
101 DA_Q_NO_SYNC_CACHE = 0x01,
102 DA_Q_NO_6_BYTE = 0x02
107 DA_CCB_BUFFER_IO = 0x02,
108 DA_CCB_WAITING = 0x03,
110 DA_CCB_TYPE_MASK = 0x0F,
111 DA_CCB_RETRY_UA = 0x10
114 /* Offsets into our private area for storing information */
115 #define ccb_state ppriv_field0
116 #define ccb_bp ppriv_ptr1
121 u_int8_t secs_per_track;
122 u_int32_t secsize; /* Number of bytes/sector */
123 u_int32_t sectors; /* total number sectors */
127 struct buf_queue_head buf_queue;
128 struct devstat device_stats;
129 SLIST_ENTRY(da_softc) links;
130 LIST_HEAD(, ccb_hdr) pending_ccbs;
134 int minimum_cmd_size;
135 int ordered_tag_count;
136 struct disk_params params;
141 struct da_quirk_entry {
142 struct scsi_inquiry_pattern inq_pat;
146 static const char quantum[] = "QUANTUM";
147 static const char microp[] = "MICROP";
149 static struct da_quirk_entry da_quirk_table[] =
153 * Fujitsu M2513A MO drives.
154 * Tested devices: M2513A2 firmware versions 1200 & 1300.
155 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
156 * Reported by: W.Scholten <whs@xs4all.nl>
158 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
159 /*quirks*/ DA_Q_NO_SYNC_CACHE
163 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
164 /*quirks*/ DA_Q_NO_SYNC_CACHE
168 * This particular Fujitsu drive doesn't like the
169 * synchronize cache command.
170 * Reported by: Tom Jackson <toj@gorilla.net>
172 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
173 /*quirks*/ DA_Q_NO_SYNC_CACHE
178 * This drive doesn't like the synchronize cache command
179 * either. Reported by: Matthew Jacob <mjacob@feral.com>
180 * in NetBSD PR kern/6027, August 24, 1998.
182 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
183 /*quirks*/ DA_Q_NO_SYNC_CACHE
187 * This drive doesn't like the synchronize cache command
188 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
191 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
192 /*quirks*/ DA_Q_NO_SYNC_CACHE
196 * Doesn't like the synchronize cache command.
197 * Reported by: Blaz Zupan <blaz@gold.amis.net>
199 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
200 /*quirks*/ DA_Q_NO_SYNC_CACHE
204 * Doesn't like the synchronize cache command.
206 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
207 /*quirks*/ DA_Q_NO_SYNC_CACHE
211 * Doesn't like the synchronize cache command.
213 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
214 /*quirks*/ DA_Q_NO_SYNC_CACHE
218 * Doesn't work correctly with 6 byte reads/writes.
219 * Returns illegal request, and points to byte 9 of the
221 * Reported by: Adam McDougall <bsdx@spawnet.com>
223 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
224 /*quirks*/ DA_Q_NO_6_BYTE
230 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
231 /*quirks*/ DA_Q_NO_6_BYTE
234 /* Below a list of quirks for USB devices supported by umass. */
237 * This USB floppy drive uses the UFI command set. This
238 * command set is a derivative of the ATAPI command set and
239 * does not support READ_6 commands only READ_10. It also does
240 * not support sync cache (0x35).
242 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Y-E DATA", "USB-FDU", "*"},
243 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
246 /* Another USB floppy */
247 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MATSHITA", "FDD CF-VFDU*","*"},
248 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
252 * Sony Memory Stick adapter MSAC-US1 and
253 * Sony PCG-C1VJ Internal Memory Stick Slot (MSC-U01).
254 * Make all sony MS* products use this quirk.
256 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "MS*", "*"},
257 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
261 * Sony Memory Stick adapter for the CLIE series
264 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "CLIE*", "*"},
265 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
269 * Sony DSC cameras (DSC-S30, DSC-S50, DSC-S70)
271 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
272 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
276 * Maxtor 3000LE USB Drive
278 {T_DIRECT, SIP_MEDIA_FIXED, "MAXTOR*", "K040H2*", "*"},
279 /*quirks*/ DA_Q_NO_6_BYTE
283 * LaCie USB drive, among others
285 {T_DIRECT, SIP_MEDIA_FIXED, "Maxtor*", "D080H4*", "*"},
286 /*quirks*/ DA_Q_NO_6_BYTE
289 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "MCF3064AP", "*"},
290 /*quirks*/ DA_Q_NO_6_BYTE
294 * Microtech USB CameraMate
296 {T_DIRECT, SIP_MEDIA_REMOVABLE, "eUSB Compact*",
297 "Compact Flash*", "*"},
298 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
302 * The vendor, product and version strings coming from the
303 * controller are null terminated instead of being padded with
304 * spaces. The trailing wildcard character '*' is required.
306 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SMSC*", "USB FDC*","*"},
307 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
311 * Olympus digital cameras (C-3040ZOOM, C-2040ZOOM, C-1)
313 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "C-*", "*"},
314 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
318 * Olympus digital cameras (D-370)
320 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "D-*", "*"},
321 /*quirks*/ DA_Q_NO_6_BYTE
325 * Olympus digital cameras (E-100RS, E-10).
327 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "E-*", "*"},
328 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
332 * KingByte Pen Drives
334 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NO BRAND", "PEN DRIVE", "*"},
335 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
341 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJIFILMUSB-DRIVEUNIT",
342 "USB-DRIVEUNIT", "*"},
343 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
347 * Nikon Coolpix E775/E995 Cameras
349 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NIKON", "NIKON DSC E*", "*"},
350 /*quirks*/ DA_Q_NO_6_BYTE
354 * Nikon Coolpix E885 Camera
356 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Nikon", "Digital Camera", "*"},
357 /*quirks*/ DA_Q_NO_6_BYTE
361 * SimpleTech FlashLink UCF-100
363 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OEI-USB", "CompactFlash", "*"},
364 /*quirks*/ DA_Q_NO_6_BYTE
368 * Minolta Dimage 2330
370 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MINOLTA", "DIMAGE 2330*", "*"},
371 /*quirks*/ DA_Q_NO_6_BYTE
375 * Minolta Dimage E203
377 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MINOLTA", "DiMAGE E203", "*"},
378 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
382 * DIVA USB Mp3 Player.
385 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DIVA USB", "Media Reader","*"},
386 /*quirks*/ DA_Q_NO_6_BYTE
390 * Daisy Technology PhotoClip USB Camera
392 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Digital", "World DMC","*"},
393 /*quirks*/ DA_Q_NO_6_BYTE
400 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Apacer", "HandyDrive", "*"},
401 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
405 * Daisy Technology PhotoClip on Zoran chip
408 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ZORAN", "COACH", "*"},
409 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
413 * HP 315 Digital Camera
416 {T_DIRECT, SIP_MEDIA_REMOVABLE, "HP", "USB CAMERA", "*"},
417 /*quirks*/ DA_Q_NO_6_BYTE
421 * Fujitsu-Siemens Memorybird pen drive
424 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Fujitsu", "Memorybird", "*"},
425 /*quirks*/ DA_Q_NO_6_BYTE
429 * Sony USB Key-Storage
432 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Storage Media", "*"},
433 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
437 * Lexar Media Jumpdrive
440 {T_DIRECT, SIP_MEDIA_REMOVABLE, "LEXAR", "DIGITAL FILM", "*"},
441 /*quirks*/ DA_Q_NO_6_BYTE
445 * Pentax USB Optio 230 camera
448 {T_DIRECT, SIP_MEDIA_REMOVABLE,
449 "PENTAX", "DIGITAL_CAMERA", "*"},
450 /*quirks*/ DA_Q_NO_6_BYTE
454 * Casio QV-R3 USB camera (uses Pentax chip as above)
457 {T_DIRECT, SIP_MEDIA_REMOVABLE,
458 "CASIO", "DIGITAL_CAMERA", "*"},
459 /*quirks*/ DA_Q_NO_6_BYTE
463 * M-Systems DiskOnKey USB flash key
466 {T_DIRECT, SIP_MEDIA_REMOVABLE, "M-Sys", "DiskOnKey", "*"},
467 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
471 * SanDisk ImageMate (I, II, ...) compact flash
474 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk", "ImageMate*", "*"},
475 /*quirks*/ DA_Q_NO_6_BYTE
479 * Feiya "slider" dual-slot flash reader. The vendor field
480 * is blank so this may match other devices.
483 {T_DIRECT, SIP_MEDIA_REMOVABLE, "", "USB CARD READER", "*"},
484 /*quirks*/ DA_Q_NO_6_BYTE
488 * SmartDisk (Mitsumi) USB floppy drive
491 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MITSUMI", "USB FDD", "*"},
492 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
499 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OTi", "Flash Disk", "*"},
500 /*quirks*/ DA_Q_NO_6_BYTE
504 static d_open_t daopen;
505 static d_close_t daclose;
506 static d_strategy_t dastrategy;
507 static d_ioctl_t daioctl;
508 static d_dump_t dadump;
509 static periph_init_t dainit;
510 static void daasync(void *callback_arg, u_int32_t code,
511 struct cam_path *path, void *arg);
512 static periph_ctor_t daregister;
513 static periph_dtor_t dacleanup;
514 static periph_start_t dastart;
515 static periph_oninv_t daoninvalidate;
516 static void dadone(struct cam_periph *periph,
517 union ccb *done_ccb);
518 static int daerror(union ccb *ccb, u_int32_t cam_flags,
519 u_int32_t sense_flags);
520 static void daprevent(struct cam_periph *periph, int action);
521 static void dasetgeom(struct cam_periph *periph,
522 struct scsi_read_capacity_data * rdcap);
523 static timeout_t dasendorderedtag;
524 static void dashutdown(void *arg, int howto);
526 #ifndef DA_DEFAULT_TIMEOUT
527 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
530 #ifndef DA_DEFAULT_RETRY
531 #define DA_DEFAULT_RETRY 4
534 static int da_retry_count = DA_DEFAULT_RETRY;
535 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
536 static int da_no_6_byte = 0;
538 SYSCTL_NODE(_kern, OID_AUTO, cam, CTLFLAG_RD, 0, "CAM Subsystem");
539 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
540 "CAM Direct Access Disk driver");
541 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
542 &da_retry_count, 0, "Normal I/O retry count");
543 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
544 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
545 SYSCTL_INT(_kern_cam_da, OID_AUTO, no_6_byte, CTLFLAG_RW,
546 &da_no_6_byte, 0, "No 6 bytes commands");
549 * DA_ORDEREDTAG_INTERVAL determines how often, relative
550 * to the default timeout, we check to see whether an ordered
551 * tagged transaction is appropriate to prevent simple tag
552 * starvation. Since we'd like to ensure that there is at least
553 * 1/2 of the timeout length left for a starved transaction to
554 * complete after we've sent an ordered tag, we must poll at least
555 * four times in every timeout period. This takes care of the worst
556 * case where a starved transaction starts during an interval that
557 * meets the requirement "don't send an ordered tag" test so it takes
558 * us two intervals to determine that a tag must be sent.
560 #ifndef DA_ORDEREDTAG_INTERVAL
561 #define DA_ORDEREDTAG_INTERVAL 4
564 static struct periph_driver dadriver =
567 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
570 DATA_SET(periphdriver_set, dadriver);
572 /* For 2.2-stable support */
577 static struct cdevsw da_cdevsw = {
579 /* maj */ DA_CDEV_MAJOR,
587 /* write */ physwrite,
591 /* strategy */ dastrategy,
596 static SLIST_HEAD(,da_softc) softc_list;
597 static struct extend_array *daperiphs;
600 daopen(dev_t dev, int flags, int fmt, struct thread *td)
602 struct cam_periph *periph;
603 struct da_softc *softc;
604 struct disklabel *label;
612 periph = cam_extend_get(daperiphs, unit);
616 softc = (struct da_softc *)periph->softc;
618 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
619 ("daopen: dev=%s (unit %d , partition %d)\n", devtoname(dev),
622 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
623 return (error); /* error code from tsleep */
626 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
628 softc->flags |= DA_FLAG_OPEN;
631 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
632 /* Invalidate our pack information. */
633 disk_invalidate(&softc->disk);
634 softc->flags &= ~DA_FLAG_PACK_INVALID;
638 /* Do a read capacity */
640 struct scsi_read_capacity_data *rcap;
643 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
647 ccb = cam_periph_getccb(periph, /*priority*/1);
648 scsi_read_capacity(&ccb->csio,
655 ccb->ccb_h.ccb_bp = NULL;
657 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
658 /*sense_flags*/SF_RETRY_UA |
660 &softc->device_stats);
662 xpt_release_ccb(ccb);
665 dasetgeom(periph, rcap);
672 struct ccb_getdev cgd;
674 /* Build label for whole disk. */
675 label = &softc->disk.d_label;
676 bzero(label, sizeof(*label));
677 label->d_type = DTYPE_SCSI;
680 * Grab the inquiry data to get the vendor and product names.
681 * Put them in the typename and packname for the label.
683 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
684 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
685 xpt_action((union ccb *)&cgd);
687 strncpy(label->d_typename, cgd.inq_data.vendor,
688 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
689 strncpy(label->d_packname, cgd.inq_data.product,
690 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
692 label->d_secsize = softc->params.secsize;
693 label->d_nsectors = softc->params.secs_per_track;
694 label->d_ntracks = softc->params.heads;
695 label->d_ncylinders = softc->params.cylinders;
696 label->d_secpercyl = softc->params.heads
697 * softc->params.secs_per_track;
698 label->d_secperunit = softc->params.sectors;
700 if (((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0)) {
701 daprevent(periph, PR_PREVENT);
705 * Check to see whether or not the blocksize is set yet.
706 * If it isn't, set it and then clear the blocksize
707 * unavailable flag for the device statistics.
709 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0){
710 softc->device_stats.block_size = softc->params.secsize;
711 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
716 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
717 daprevent(periph, PR_ALLOW);
719 softc->flags &= ~DA_FLAG_OPEN;
720 cam_periph_release(periph);
722 cam_periph_unlock(periph);
727 daclose(dev_t dev, int flag, int fmt, struct thread *td)
729 struct cam_periph *periph;
730 struct da_softc *softc;
735 periph = cam_extend_get(daperiphs, unit);
739 softc = (struct da_softc *)periph->softc;
741 if ((error = cam_periph_lock(periph, 0)) != 0) {
742 return (error); /* error code from tsleep */
745 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
748 ccb = cam_periph_getccb(periph, /*priority*/1);
750 scsi_synchronize_cache(&ccb->csio,
754 /*begin_lba*/0,/* Cover the whole disk */
759 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
760 /*sense_flags*/SF_RETRY_UA,
761 &softc->device_stats);
763 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
764 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
765 CAM_SCSI_STATUS_ERROR) {
767 int sense_key, error_code;
769 scsi_extract_sense(&ccb->csio.sense_data,
773 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
774 scsi_sense_print(&ccb->csio);
776 xpt_print_path(periph->path);
777 printf("Synchronize cache failed, status "
778 "== 0x%x, scsi status == 0x%x\n",
779 ccb->csio.ccb_h.status,
780 ccb->csio.scsi_status);
784 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
785 cam_release_devq(ccb->ccb_h.path,
791 xpt_release_ccb(ccb);
795 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
796 daprevent(periph, PR_ALLOW);
798 * If we've got removeable media, mark the blocksize as
799 * unavailable, since it could change when new media is
802 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
805 softc->flags &= ~DA_FLAG_OPEN;
806 cam_periph_unlock(periph);
807 cam_periph_release(periph);
812 * Actually translate the requested transfer into one the physical driver
813 * can understand. The transfer is described by a buf and will include
814 * only one physical transfer.
817 dastrategy(struct buf *bp)
819 struct cam_periph *periph;
820 struct da_softc *softc;
825 unit = dkunit(bp->b_dev);
826 part = dkpart(bp->b_dev);
827 periph = cam_extend_get(daperiphs, unit);
828 if (periph == NULL) {
832 softc = (struct da_softc *)periph->softc;
835 * check it's not too big a transfer for our adapter
837 scsi_minphys(bp,&sd_switch);
841 * Mask interrupts so that the pack cannot be invalidated until
842 * after we are in the queue. Otherwise, we might not properly
843 * clean up one of the buffers.
848 * If the device has been made invalid, error out
850 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
857 * Place it in the queue of disk activities for this disk
859 bufqdisksort(&softc->buf_queue, bp);
864 * Schedule ourselves for performing the work.
866 xpt_schedule(periph, /* XXX priority */1);
870 bp->b_flags |= B_ERROR;
873 * Correctly set the buf to indicate a completed xfer
875 bp->b_resid = bp->b_bcount;
880 /* For 2.2-stable support */
886 daioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
888 struct cam_periph *periph;
889 struct da_softc *softc;
894 periph = cam_extend_get(daperiphs, unit);
898 softc = (struct da_softc *)periph->softc;
900 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("daioctl\n"));
902 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
903 return (error); /* error code from tsleep */
906 error = cam_periph_ioctl(periph, cmd, addr, daerror);
908 cam_periph_unlock(periph);
916 struct cam_periph *periph;
917 struct da_softc *softc;
921 u_int num; /* number of sectors to write */
925 struct ccb_scsiio csio;
926 int dumppages = MAXDUMPPGS;
930 /* toss any characters present prior to dump */
931 while (cncheckc() != -1)
936 periph = cam_extend_get(daperiphs, unit);
937 if (periph == NULL) {
940 softc = (struct da_softc *)periph->softc;
942 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0)
945 error = disk_dumpcheck(dev, &num, &blknum, &secsize);
949 addr = 0; /* starting address */
950 blkcnt = howmany(PAGE_SIZE, secsize);
955 if ((num / blkcnt) < dumppages)
956 dumppages = num / blkcnt;
958 for (i = 0; i < dumppages; ++i) {
959 vm_offset_t a = addr + (i * PAGE_SIZE);
960 if (is_physical_memory(a))
961 va = pmap_kenter_temporary(trunc_page(a), i);
963 va = pmap_kenter_temporary(trunc_page(0), i);
966 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
967 csio.ccb_h.ccb_state = DA_CCB_DUMP;
968 scsi_read_write(&csio,
974 /*minimum_cmd_size*/ softc->minimum_cmd_size,
977 /*data_ptr*/(u_int8_t *) va,
978 /*dxfer_len*/blkcnt * secsize * dumppages,
979 /*sense_len*/SSD_FULL_SIZE,
980 DA_DEFAULT_TIMEOUT * 1000);
981 xpt_polled_action((union ccb *)&csio);
983 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
984 printf("Aborting dump due to I/O error.\n");
985 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
986 CAM_SCSI_STATUS_ERROR)
987 scsi_sense_print(&csio);
989 printf("status == 0x%x, scsi status == 0x%x\n",
990 csio.ccb_h.status, csio.scsi_status);
994 if (dumpstatus(addr, (off_t)num * softc->params.secsize) < 0)
997 /* update block count */
998 num -= blkcnt * dumppages;
999 blknum += blkcnt * dumppages;
1000 addr += PAGE_SIZE * dumppages;
1004 * Sync the disk cache contents to the physical media.
1006 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1008 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
1009 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1010 scsi_synchronize_cache(&csio,
1014 /*begin_lba*/0,/* Cover the whole disk */
1018 xpt_polled_action((union ccb *)&csio);
1020 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1021 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
1022 CAM_SCSI_STATUS_ERROR) {
1024 int sense_key, error_code;
1026 scsi_extract_sense(&csio.sense_data,
1030 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1031 scsi_sense_print(&csio);
1033 xpt_print_path(periph->path);
1034 printf("Synchronize cache failed, status "
1035 "== 0x%x, scsi status == 0x%x\n",
1036 csio.ccb_h.status, csio.scsi_status);
1047 struct cam_path *path;
1050 * Create our extend array for storing the devices we attach to.
1052 daperiphs = cam_extend_new();
1053 SLIST_INIT(&softc_list);
1054 if (daperiphs == NULL) {
1055 printf("da: Failed to alloc extend array!\n");
1060 * Install a global async callback. This callback will
1061 * receive async callbacks like "new device found".
1063 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
1064 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1066 if (status == CAM_REQ_CMP) {
1067 struct ccb_setasync csa;
1069 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
1070 csa.ccb_h.func_code = XPT_SASYNC_CB;
1071 csa.event_enable = AC_FOUND_DEVICE;
1072 csa.callback = daasync;
1073 csa.callback_arg = NULL;
1074 xpt_action((union ccb *)&csa);
1075 status = csa.ccb_h.status;
1076 xpt_free_path(path);
1079 if (status != CAM_REQ_CMP) {
1080 printf("da: Failed to attach master async callback "
1081 "due to status 0x%x!\n", status);
1085 * Schedule a periodic event to occasionally send an
1086 * ordered tag to a device.
1088 timeout(dasendorderedtag, NULL,
1089 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL);
1091 /* Register our shutdown event handler */
1092 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1093 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1094 printf("dainit: shutdown event registration failed!\n");
1099 daoninvalidate(struct cam_periph *periph)
1102 struct da_softc *softc;
1104 struct ccb_setasync csa;
1106 softc = (struct da_softc *)periph->softc;
1109 * De-register any async callbacks.
1111 xpt_setup_ccb(&csa.ccb_h, periph->path,
1113 csa.ccb_h.func_code = XPT_SASYNC_CB;
1114 csa.event_enable = 0;
1115 csa.callback = daasync;
1116 csa.callback_arg = periph;
1117 xpt_action((union ccb *)&csa);
1119 softc->flags |= DA_FLAG_PACK_INVALID;
1122 * Although the oninvalidate() routines are always called at
1123 * splsoftcam, we need to be at splbio() here to keep the buffer
1124 * queue from being modified while we traverse it.
1129 * Return all queued I/O with ENXIO.
1130 * XXX Handle any transactions queued to the card
1131 * with XPT_ABORT_CCB.
1133 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
1134 bufq_remove(&softc->buf_queue, q_bp);
1135 q_bp->b_resid = q_bp->b_bcount;
1136 q_bp->b_error = ENXIO;
1137 q_bp->b_flags |= B_ERROR;
1142 SLIST_REMOVE(&softc_list, softc, da_softc, links);
1144 xpt_print_path(periph->path);
1145 printf("lost device\n");
1149 dacleanup(struct cam_periph *periph)
1151 struct da_softc *softc;
1153 softc = (struct da_softc *)periph->softc;
1155 devstat_remove_entry(&softc->device_stats);
1156 cam_extend_release(daperiphs, periph->unit_number);
1157 xpt_print_path(periph->path);
1158 printf("removing device entry\n");
1159 if (softc->disk.d_dev) {
1160 disk_destroy(&softc->disk);
1162 free(softc, M_DEVBUF);
1166 daasync(void *callback_arg, u_int32_t code,
1167 struct cam_path *path, void *arg)
1169 struct cam_periph *periph;
1171 periph = (struct cam_periph *)callback_arg;
1173 case AC_FOUND_DEVICE:
1175 struct ccb_getdev *cgd;
1178 cgd = (struct ccb_getdev *)arg;
1180 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1181 && SID_TYPE(&cgd->inq_data) != T_RBC
1182 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1186 * Allocate a peripheral instance for
1187 * this device and start the probe
1190 status = cam_periph_alloc(daregister, daoninvalidate,
1192 "da", CAM_PERIPH_BIO,
1193 cgd->ccb_h.path, daasync,
1194 AC_FOUND_DEVICE, cgd);
1196 if (status != CAM_REQ_CMP
1197 && status != CAM_REQ_INPROG)
1198 printf("daasync: Unable to attach to new device "
1199 "due to status 0x%x\n", status);
1205 struct da_softc *softc;
1206 struct ccb_hdr *ccbh;
1209 softc = (struct da_softc *)periph->softc;
1212 * Don't fail on the expected unit attention
1215 softc->flags |= DA_FLAG_RETRY_UA;
1216 for (ccbh = LIST_FIRST(&softc->pending_ccbs);
1217 ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le))
1218 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1223 cam_periph_async(periph, code, path, arg);
1229 daregister(struct cam_periph *periph, void *arg)
1232 struct da_softc *softc;
1233 struct ccb_setasync csa;
1234 struct ccb_getdev *cgd;
1237 cgd = (struct ccb_getdev *)arg;
1238 if (periph == NULL) {
1239 printf("daregister: periph was NULL!!\n");
1240 return(CAM_REQ_CMP_ERR);
1244 printf("daregister: no getdev CCB, can't register device\n");
1245 return(CAM_REQ_CMP_ERR);
1248 softc = (struct da_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
1250 if (softc == NULL) {
1251 printf("daregister: Unable to probe new device. "
1252 "Unable to allocate softc\n");
1253 return(CAM_REQ_CMP_ERR);
1256 bzero(softc, sizeof(*softc));
1257 LIST_INIT(&softc->pending_ccbs);
1258 softc->state = DA_STATE_PROBE;
1259 bufq_init(&softc->buf_queue);
1260 if (SID_IS_REMOVABLE(&cgd->inq_data))
1261 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1262 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1263 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1265 periph->softc = softc;
1267 cam_extend_set(daperiphs, periph->unit_number, periph);
1270 * See if this device has any quirks.
1272 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1273 (caddr_t)da_quirk_table,
1274 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1275 sizeof(*da_quirk_table), scsi_inquiry_match);
1278 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1280 softc->quirks = DA_Q_NONE;
1282 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1283 softc->minimum_cmd_size = 10;
1285 softc->minimum_cmd_size = 6;
1288 * Block our timeout handler while we
1289 * add this softc to the dev list.
1292 SLIST_INSERT_HEAD(&softc_list, softc, links);
1296 * The DA driver supports a blocksize, but
1297 * we don't know the blocksize until we do
1298 * a read capacity. So, set a flag to
1299 * indicate that the blocksize is
1300 * unavailable right now. We'll clear the
1301 * flag as soon as we've done a read capacity.
1303 devstat_add_entry(&softc->device_stats, "da",
1304 periph->unit_number, 0,
1305 DEVSTAT_BS_UNAVAILABLE,
1306 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
1307 DEVSTAT_PRIORITY_DISK);
1310 * Register this media as a disk
1312 disk_create(periph->unit_number, &softc->disk, 0, &da_cdevsw);
1315 * Add async callbacks for bus reset and
1316 * bus device reset calls. I don't bother
1317 * checking if this fails as, in most cases,
1318 * the system will function just fine without
1319 * them and the only alternative would be to
1320 * not attach the device on failure.
1322 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
1323 csa.ccb_h.func_code = XPT_SASYNC_CB;
1324 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
1325 csa.callback = daasync;
1326 csa.callback_arg = periph;
1327 xpt_action((union ccb *)&csa);
1329 * Lock this peripheral until we are setup.
1330 * This first call can't block
1332 (void)cam_periph_lock(periph, 0);
1333 xpt_schedule(periph, /*priority*/5);
1335 return(CAM_REQ_CMP);
1339 dastart(struct cam_periph *periph, union ccb *start_ccb)
1341 struct da_softc *softc;
1343 softc = (struct da_softc *)periph->softc;
1346 switch (softc->state) {
1347 case DA_STATE_NORMAL:
1349 /* Pull a buffer from the queue and get going on it */
1354 * See if there is a buf with work for us to do..
1357 bp = bufq_first(&softc->buf_queue);
1358 if (periph->immediate_priority <= periph->pinfo.priority) {
1359 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1360 ("queuing for immediate ccb\n"));
1361 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1362 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1364 periph->immediate_priority = CAM_PRIORITY_NONE;
1366 wakeup(&periph->ccb_list);
1367 } else if (bp == NULL) {
1369 xpt_release_ccb(start_ccb);
1374 bufq_remove(&softc->buf_queue, bp);
1376 devstat_start_transaction(&softc->device_stats);
1378 if ((bp->b_flags & B_ORDERED) != 0
1379 || (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1380 softc->flags &= ~DA_FLAG_NEED_OTAG;
1381 softc->ordered_tag_count++;
1382 tag_code = MSG_ORDERED_Q_TAG;
1384 tag_code = MSG_SIMPLE_Q_TAG;
1386 if (da_no_6_byte && softc->minimum_cmd_size == 6)
1387 softc->minimum_cmd_size = 10;
1388 scsi_read_write(&start_ccb->csio,
1389 /*retries*/da_retry_count,
1392 bp->b_flags & B_READ,
1394 softc->minimum_cmd_size,
1396 bp->b_bcount / softc->params.secsize,
1399 /*sense_len*/SSD_FULL_SIZE,
1400 da_default_timeout * 1000);
1401 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1404 * Block out any asyncronous callbacks
1405 * while we touch the pending ccb list.
1408 LIST_INSERT_HEAD(&softc->pending_ccbs,
1409 &start_ccb->ccb_h, periph_links.le);
1412 /* We expect a unit attention from this device */
1413 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1414 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1415 softc->flags &= ~DA_FLAG_RETRY_UA;
1418 start_ccb->ccb_h.ccb_bp = bp;
1419 bp = bufq_first(&softc->buf_queue);
1422 xpt_action(start_ccb);
1426 /* Have more work to do, so ensure we stay scheduled */
1427 xpt_schedule(periph, /* XXX priority */1);
1431 case DA_STATE_PROBE:
1433 struct ccb_scsiio *csio;
1434 struct scsi_read_capacity_data *rcap;
1436 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
1440 printf("dastart: Couldn't malloc read_capacity data\n");
1441 /* da_free_periph??? */
1444 csio = &start_ccb->csio;
1445 scsi_read_capacity(csio,
1452 start_ccb->ccb_h.ccb_bp = NULL;
1453 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1454 xpt_action(start_ccb);
1461 cmd6workaround(union ccb *ccb)
1463 struct scsi_rw_6 cmd6;
1464 struct scsi_rw_10 *cmd10;
1465 struct da_softc *softc;
1469 cdb = ccb->csio.cdb_io.cdb_bytes;
1471 /* Translation only possible if CDB is an array and cmd is R/W6 */
1472 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1473 (*cdb != READ_6 && *cdb != WRITE_6))
1476 xpt_print_path(ccb->ccb_h.path);
1477 printf("READ(6)/WRITE(6) not supported, "
1478 "increasing minimum_cmd_size to 10.\n");
1479 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1480 softc->minimum_cmd_size = 10;
1482 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1483 cmd10 = (struct scsi_rw_10 *)cdb;
1484 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1486 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1487 cmd10->reserved = 0;
1488 scsi_ulto2b(cmd6.length, cmd10->length);
1489 cmd10->control = cmd6.control;
1490 ccb->csio.cdb_len = sizeof(*cmd10);
1492 /* Requeue request, unfreezing queue if necessary */
1493 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1494 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1497 cam_release_devq(ccb->ccb_h.path,
1501 /*getcount_only*/0);
1507 dadone(struct cam_periph *periph, union ccb *done_ccb)
1509 struct da_softc *softc;
1510 struct ccb_scsiio *csio;
1512 softc = (struct da_softc *)periph->softc;
1513 csio = &done_ccb->csio;
1514 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1515 case DA_CCB_BUFFER_IO:
1520 bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
1521 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1526 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1531 /* Retry selection timeouts */
1532 sf |= SF_RETRY_SELTO;
1534 if ((error = daerror(done_ccb, 0, sf)) == ERESTART) {
1536 * A retry was scheuled, so
1546 if (error == ENXIO) {
1548 * Catastrophic error. Mark our pack as
1551 /* XXX See if this is really a media
1554 xpt_print_path(periph->path);
1555 printf("Invalidating pack\n");
1556 softc->flags |= DA_FLAG_PACK_INVALID;
1560 * return all queued I/O with EIO, so that
1561 * the client can retry these I/Os in the
1562 * proper order should it attempt to recover.
1564 while ((q_bp = bufq_first(&softc->buf_queue))
1566 bufq_remove(&softc->buf_queue, q_bp);
1567 q_bp->b_resid = q_bp->b_bcount;
1568 q_bp->b_error = EIO;
1569 q_bp->b_flags |= B_ERROR;
1573 bp->b_error = error;
1574 bp->b_resid = bp->b_bcount;
1575 bp->b_flags |= B_ERROR;
1577 bp->b_resid = csio->resid;
1579 if (bp->b_resid != 0) {
1580 /* Short transfer ??? */
1582 if (cmd6workaround(done_ccb)
1586 bp->b_flags |= B_ERROR;
1589 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1590 cam_release_devq(done_ccb->ccb_h.path,
1594 /*getcount_only*/0);
1596 bp->b_resid = csio->resid;
1597 if (csio->resid > 0) {
1598 /* Short transfer ??? */
1599 #if 0 /* XXX most of the broken umass devices need this ad-hoc work around */
1600 if (cmd6workaround(done_ccb) == ERESTART)
1603 bp->b_flags |= B_ERROR;
1608 * Block out any asyncronous callbacks
1609 * while we touch the pending ccb list.
1612 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1615 if (softc->device_stats.busy_count == 0)
1616 softc->flags |= DA_FLAG_WENT_IDLE;
1618 devstat_end_transaction_buf(&softc->device_stats, bp);
1624 struct scsi_read_capacity_data *rdcap;
1625 char announce_buf[80];
1627 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1629 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1630 struct disk_params *dp;
1632 dasetgeom(periph, rdcap);
1633 dp = &softc->params;
1634 snprintf(announce_buf, sizeof(announce_buf),
1635 "%luMB (%u %u byte sectors: %dH %dS/T %dC)",
1636 (unsigned long) (((u_int64_t)dp->secsize *
1637 dp->sectors) / (1024*1024)), dp->sectors,
1638 dp->secsize, dp->heads, dp->secs_per_track,
1643 announce_buf[0] = '\0';
1646 * Retry any UNIT ATTENTION type errors. They
1647 * are expected at boot.
1649 error = daerror(done_ccb, 0, SF_RETRY_UA |
1650 SF_RETRY_SELTO | SF_NO_PRINT);
1651 if (error == ERESTART) {
1653 * A retry was scheuled, so
1657 } else if (error != 0) {
1658 struct scsi_sense_data *sense;
1660 int sense_key, error_code;
1663 struct ccb_getdev cgd;
1665 /* Don't wedge this device's queue */
1666 cam_release_devq(done_ccb->ccb_h.path,
1670 /*getcount_only*/0);
1672 status = done_ccb->ccb_h.status;
1674 xpt_setup_ccb(&cgd.ccb_h,
1675 done_ccb->ccb_h.path,
1677 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1678 xpt_action((union ccb *)&cgd);
1680 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1681 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1682 || ((status & CAM_AUTOSNS_VALID) == 0))
1688 sense = &csio->sense_data;
1689 scsi_extract_sense(sense, &error_code,
1694 * Attach to anything that claims to be a
1695 * direct access or optical disk device,
1696 * as long as it doesn't return a "Logical
1697 * unit not supported" (0x25) error.
1699 if ((have_sense) && (asc != 0x25)
1700 && (error_code == SSD_CURRENT_ERROR))
1701 snprintf(announce_buf,
1702 sizeof(announce_buf),
1703 "Attempt to query device "
1704 "size failed: %s, %s",
1705 scsi_sense_key_text[sense_key],
1706 scsi_sense_desc(asc,ascq,
1713 xpt_print_path(periph->path);
1714 printf("got CAM status %#x\n",
1715 done_ccb->ccb_h.status);
1718 xpt_print_path(periph->path);
1719 printf("fatal error, failed"
1720 " to attach to device\n");
1723 * Free up resources.
1725 cam_periph_invalidate(periph);
1729 free(rdcap, M_TEMP);
1730 if (announce_buf[0] != '\0')
1731 xpt_announce_periph(periph, announce_buf);
1732 softc->state = DA_STATE_NORMAL;
1734 * Since our peripheral may be invalidated by an error
1735 * above or an external event, we must release our CCB
1736 * before releasing the probe lock on the peripheral.
1737 * The peripheral will only go away once the last lock
1738 * is removed, and we need it around for the CCB release
1741 xpt_release_ccb(done_ccb);
1742 cam_periph_unlock(periph);
1745 case DA_CCB_WAITING:
1747 /* Caller will release the CCB */
1748 wakeup(&done_ccb->ccb_h.cbfcnp);
1752 /* No-op. We're polling */
1757 xpt_release_ccb(done_ccb);
1761 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1763 struct da_softc *softc;
1764 struct cam_periph *periph;
1765 int error, sense_key, error_code, asc, ascq;
1767 periph = xpt_path_periph(ccb->ccb_h.path);
1768 softc = (struct da_softc *)periph->softc;
1771 * Automatically detect devices that do not support
1772 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
1775 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR
1776 && ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND) {
1777 scsi_extract_sense(&ccb->csio.sense_data,
1778 &error_code, &sense_key, &asc, &ascq);
1779 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
1780 error = cmd6workaround(ccb);
1782 if (error == ERESTART)
1787 * Until we have a better way of doing pack validation,
1788 * don't treat UAs as errors.
1790 sense_flags |= SF_RETRY_UA;
1791 return(cam_periph_error(ccb, cam_flags, sense_flags,
1792 &softc->saved_ccb));
1796 daprevent(struct cam_periph *periph, int action)
1798 struct da_softc *softc;
1802 softc = (struct da_softc *)periph->softc;
1804 if (((action == PR_ALLOW)
1805 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
1806 || ((action == PR_PREVENT)
1807 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
1811 ccb = cam_periph_getccb(periph, /*priority*/1);
1813 scsi_prevent(&ccb->csio,
1821 error = cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
1822 /*sense_flags*/0, &softc->device_stats);
1825 if (action == PR_ALLOW)
1826 softc->flags &= ~DA_FLAG_PACK_LOCKED;
1828 softc->flags |= DA_FLAG_PACK_LOCKED;
1831 xpt_release_ccb(ccb);
1835 dasetgeom(struct cam_periph *periph, struct scsi_read_capacity_data * rdcap)
1837 struct ccb_calc_geometry ccg;
1838 struct da_softc *softc;
1839 struct disk_params *dp;
1841 softc = (struct da_softc *)periph->softc;
1843 dp = &softc->params;
1844 dp->secsize = scsi_4btoul(rdcap->length);
1845 dp->sectors = scsi_4btoul(rdcap->addr) + 1;
1847 * Have the controller provide us with a geometry
1848 * for this disk. The only time the geometry
1849 * matters is when we boot and the controller
1850 * is the only one knowledgeable enough to come
1851 * up with something that will make this a bootable
1854 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
1855 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
1856 ccg.block_size = dp->secsize;
1857 ccg.volume_size = dp->sectors;
1859 ccg.secs_per_track = 0;
1861 xpt_action((union ccb*)&ccg);
1862 dp->heads = ccg.heads;
1863 dp->secs_per_track = ccg.secs_per_track;
1864 dp->cylinders = ccg.cylinders;
1868 dasendorderedtag(void *arg)
1870 struct da_softc *softc;
1873 for (softc = SLIST_FIRST(&softc_list);
1875 softc = SLIST_NEXT(softc, links)) {
1877 if ((softc->ordered_tag_count == 0)
1878 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
1879 softc->flags |= DA_FLAG_NEED_OTAG;
1881 if (softc->device_stats.busy_count > 0)
1882 softc->flags &= ~DA_FLAG_WENT_IDLE;
1884 softc->ordered_tag_count = 0;
1887 /* Queue us up again */
1888 timeout(dasendorderedtag, NULL,
1889 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL);
1893 * Step through all DA peripheral drivers, and if the device is still open,
1894 * sync the disk cache to physical media.
1897 dashutdown(void * arg, int howto)
1899 struct cam_periph *periph;
1900 struct da_softc *softc;
1902 for (periph = TAILQ_FIRST(&dadriver.units); periph != NULL;
1903 periph = TAILQ_NEXT(periph, unit_links)) {
1905 softc = (struct da_softc *)periph->softc;
1908 * We only sync the cache if the drive is still open, and
1909 * if the drive is capable of it..
1911 if (((softc->flags & DA_FLAG_OPEN) == 0)
1912 || (softc->quirks & DA_Q_NO_SYNC_CACHE))
1915 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
1917 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
1918 scsi_synchronize_cache(&ccb.csio,
1922 /*begin_lba*/0, /* whole disk */
1927 xpt_polled_action(&ccb);
1929 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1930 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
1931 CAM_SCSI_STATUS_ERROR)
1932 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
1933 int error_code, sense_key, asc, ascq;
1935 scsi_extract_sense(&ccb.csio.sense_data,
1936 &error_code, &sense_key,
1939 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1940 scsi_sense_print(&ccb.csio);
1942 xpt_print_path(periph->path);
1943 printf("Synchronize cache failed, status "
1944 "== 0x%x, scsi status == 0x%x\n",
1945 ccb.ccb_h.status, ccb.csio.scsi_status);
1949 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1950 cam_release_devq(ccb.ccb_h.path,
1954 /*getcount_only*/0);
1959 #else /* !_KERNEL */
1962 * XXX This is only left out of the kernel build to silence warnings. If,
1963 * for some reason this function is used in the kernel, the ifdefs should
1964 * be moved so it is included both in the kernel and userland.
1967 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
1968 void (*cbfcnp)(struct cam_periph *, union ccb *),
1969 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
1970 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
1973 struct scsi_format_unit *scsi_cmd;
1975 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
1976 scsi_cmd->opcode = FORMAT_UNIT;
1977 scsi_cmd->byte2 = byte2;
1978 scsi_ulto2b(ileave, scsi_cmd->interleave);
1983 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
1992 #endif /* _KERNEL */