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.46 2003/10/21 22:18:19 thomas Exp $
29 * $DragonFly: src/sys/bus/cam/scsi/scsi_da.c,v 1.27 2006/02/17 19:17:42 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>
44 #include <sys/taskqueue.h>
47 #include <sys/devicestat.h>
51 #include <sys/eventhandler.h>
52 #include <sys/malloc.h>
58 #include <sys/thread2.h>
61 #include <machine/md_var.h>
74 #include "../cam_ccb.h"
75 #include "../cam_extend.h"
76 #include "../cam_periph.h"
77 #include "../cam_xpt_periph.h"
79 #include "scsi_message.h"
92 DA_FLAG_PACK_INVALID = 0x001,
93 DA_FLAG_NEW_PACK = 0x002,
94 DA_FLAG_PACK_LOCKED = 0x004,
95 DA_FLAG_PACK_REMOVABLE = 0x008,
96 DA_FLAG_TAGGED_QUEUING = 0x010,
97 DA_FLAG_NEED_OTAG = 0x020,
98 DA_FLAG_WENT_IDLE = 0x040,
99 DA_FLAG_RETRY_UA = 0x080,
100 DA_FLAG_OPEN = 0x100,
101 DA_FLAG_SCTX_INIT = 0x200
106 DA_Q_NO_SYNC_CACHE = 0x01,
107 DA_Q_NO_6_BYTE = 0x02,
108 DA_Q_NO_PREVENT = 0x04
113 DA_CCB_BUFFER_IO = 0x02,
114 DA_CCB_WAITING = 0x03,
116 DA_CCB_TYPE_MASK = 0x0F,
117 DA_CCB_RETRY_UA = 0x10
120 /* Offsets into our private area for storing information */
121 #define ccb_state ppriv_field0
122 #define ccb_bio ppriv_ptr1
127 u_int8_t secs_per_track;
128 u_int32_t secsize; /* Number of bytes/sector */
129 u_int32_t sectors; /* total number sectors */
133 struct bio_queue_head bio_queue;
134 struct devstat device_stats;
135 SLIST_ENTRY(da_softc) links;
136 LIST_HEAD(, ccb_hdr) pending_ccbs;
140 int minimum_cmd_size;
141 int ordered_tag_count;
142 struct disk_params params;
145 struct task sysctl_task;
146 struct sysctl_ctx_list sysctl_ctx;
147 struct sysctl_oid *sysctl_tree;
150 struct da_quirk_entry {
151 struct scsi_inquiry_pattern inq_pat;
155 static const char quantum[] = "QUANTUM";
156 static const char microp[] = "MICROP";
158 static struct da_quirk_entry da_quirk_table[] =
160 /* SPI, FC devices */
163 * Fujitsu M2513A MO drives.
164 * Tested devices: M2513A2 firmware versions 1200 & 1300.
165 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
166 * Reported by: W.Scholten <whs@xs4all.nl>
168 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
169 /*quirks*/ DA_Q_NO_SYNC_CACHE
173 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
174 /*quirks*/ DA_Q_NO_SYNC_CACHE
178 * This particular Fujitsu drive doesn't like the
179 * synchronize cache command.
180 * Reported by: Tom Jackson <toj@gorilla.net>
182 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
183 /*quirks*/ DA_Q_NO_SYNC_CACHE
188 * This drive doesn't like the synchronize cache command
189 * either. Reported by: Matthew Jacob <mjacob@feral.com>
190 * in NetBSD PR kern/6027, August 24, 1998.
192 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
193 /*quirks*/ DA_Q_NO_SYNC_CACHE
197 * This drive doesn't like the synchronize cache command
198 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
201 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
202 /*quirks*/ DA_Q_NO_SYNC_CACHE
206 * Doesn't like the synchronize cache command.
207 * Reported by: Blaz Zupan <blaz@gold.amis.net>
209 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
210 /*quirks*/ DA_Q_NO_SYNC_CACHE
214 * Doesn't like the synchronize cache command.
216 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
217 /*quirks*/ DA_Q_NO_SYNC_CACHE
221 * Doesn't like the synchronize cache command.
223 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
224 /*quirks*/ DA_Q_NO_SYNC_CACHE
228 * Doesn't work correctly with 6 byte reads/writes.
229 * Returns illegal request, and points to byte 9 of the
231 * Reported by: Adam McDougall <bsdx@spawnet.com>
233 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
234 /*quirks*/ DA_Q_NO_6_BYTE
238 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
239 /*quirks*/ DA_Q_NO_6_BYTE
241 /* XXX USB floppy quirks temporarily enabled for 4.9R */
242 /* USB floppy devices supported by umass(4) */
245 * This USB floppy drive uses the UFI command set. This
246 * command set is a derivative of the ATAPI command set and
247 * does not support READ_6 commands only READ_10. It also does
248 * not support sync cache (0x35).
250 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Y-E DATA", "USB-FDU", "*"},
251 /*quirks*/ DA_Q_NO_SYNC_CACHE
254 /* Another USB floppy */
255 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MATSHITA", "FDD CF-VFDU*","*"},
256 /*quirks*/ DA_Q_NO_SYNC_CACHE
260 * The vendor, product and version strings coming from the
261 * controller are null terminated instead of being padded with
262 * spaces. The trailing wildcard character '*' is required.
264 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SMSC*", "USB FDC*","*"},
265 /*quirks*/ DA_Q_NO_SYNC_CACHE
269 * SmartDisk (Mitsumi) USB floppy drive
272 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MITSUMI", "USB FDD", "*"},
273 /*quirks*/ DA_Q_NO_SYNC_CACHE
277 * The CISS RAID controllers do not support SYNC_CACHE
279 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
280 /*quirks*/ DA_Q_NO_SYNC_CACHE
283 /* USB mass storage devices supported by umass(4) */
286 * Sony Memory Stick adapter MSAC-US1 and
287 * Sony PCG-C1VJ Internal Memory Stick Slot (MSC-U01).
288 * Make all sony MS* products use this quirk.
290 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "MS*", "*"},
291 /*quirks*/ DA_Q_NO_SYNC_CACHE
295 * Sony Memory Stick adapter for the CLIE series
298 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "CLIE*", "*"},
299 /*quirks*/ DA_Q_NO_SYNC_CACHE
303 * Intelligent Stick USB disk-on-key
306 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB Card",
307 "IntelligentStick*", "*"},
308 /*quirks*/ DA_Q_NO_SYNC_CACHE
312 * Sony DSC cameras (DSC-S30, DSC-S50, DSC-S70)
314 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
315 /*quirks*/ DA_Q_NO_SYNC_CACHE
319 * Microtech USB CameraMate
321 {T_DIRECT, SIP_MEDIA_REMOVABLE, "eUSB Compact*",
322 "Compact Flash*", "*"},
323 /*quirks*/ DA_Q_NO_SYNC_CACHE
327 * Olympus digital cameras (C-3040ZOOM, C-2040ZOOM, C-1)
329 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "C-*", "*"},
330 /*quirks*/ DA_Q_NO_SYNC_CACHE
334 * Olympus digital cameras (E-100RS, E-10).
336 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "E-*", "*"},
337 /*quirks*/ DA_Q_NO_SYNC_CACHE
341 * KingByte Pen Drives
343 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NO BRAND", "PEN DRIVE", "*"},
344 /*quirks*/ DA_Q_NO_SYNC_CACHE
348 * iRiver iFP MP3 player (with UMS Firmware)
349 * PR: kern/54881, i386/63941, kern/66124
351 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
352 /*quirks*/ DA_Q_NO_SYNC_CACHE
356 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
359 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "NexIA+*", "*"},
360 /*quirks*/ DA_Q_NO_SYNC_CACHE
366 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJIFILMUSB-DRIVEUNIT",
367 "USB-DRIVEUNIT", "*"},
368 /*quirks*/ DA_Q_NO_SYNC_CACHE
372 * Minolta Dimage E203
374 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MINOLTA", "DiMAGE E203", "*"},
375 /*quirks*/ DA_Q_NO_SYNC_CACHE
382 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Apacer", "HandyDrive", "*"},
383 /*quirks*/ DA_Q_NO_SYNC_CACHE
387 * Daisy Technology PhotoClip on Zoran chip
390 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ZORAN", "COACH", "*"},
391 /*quirks*/ DA_Q_NO_SYNC_CACHE
395 * Sony USB Key-Storage
398 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Storage Media", "*"},
399 /*quirks*/ DA_Q_NO_SYNC_CACHE
401 #endif /* DA_OLD_QUIRKS */
404 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
407 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
408 /*quirks*/ DA_Q_NO_SYNC_CACHE
412 * Jungsoft NEXDISK USB flash key
415 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
416 /*quirks*/ DA_Q_NO_SYNC_CACHE
420 * Creative Nomad MUVO mp3 player (USB)
423 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
424 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
428 static d_open_t daopen;
429 static d_close_t daclose;
430 static d_strategy_t dastrategy;
431 static d_ioctl_t daioctl;
432 static d_dump_t dadump;
433 static periph_init_t dainit;
434 static void daasync(void *callback_arg, u_int32_t code,
435 struct cam_path *path, void *arg);
436 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
437 static periph_ctor_t daregister;
438 static periph_dtor_t dacleanup;
439 static periph_start_t dastart;
440 static periph_oninv_t daoninvalidate;
441 static void dadone(struct cam_periph *periph,
442 union ccb *done_ccb);
443 static int daerror(union ccb *ccb, u_int32_t cam_flags,
444 u_int32_t sense_flags);
445 static void daprevent(struct cam_periph *periph, int action);
446 static void dasetgeom(struct cam_periph *periph,
447 struct scsi_read_capacity_data * rdcap);
448 static timeout_t dasendorderedtag;
449 static void dashutdown(void *arg, int howto);
451 #ifndef DA_DEFAULT_TIMEOUT
452 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
455 #ifndef DA_DEFAULT_RETRY
456 #define DA_DEFAULT_RETRY 4
459 static int da_retry_count = DA_DEFAULT_RETRY;
460 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
461 static struct callout dasendorderedtag_ch;
463 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
464 "CAM Direct Access Disk driver");
465 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
466 &da_retry_count, 0, "Normal I/O retry count");
467 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
468 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
469 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
470 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
473 * DA_ORDEREDTAG_INTERVAL determines how often, relative
474 * to the default timeout, we check to see whether an ordered
475 * tagged transaction is appropriate to prevent simple tag
476 * starvation. Since we'd like to ensure that there is at least
477 * 1/2 of the timeout length left for a starved transaction to
478 * complete after we've sent an ordered tag, we must poll at least
479 * four times in every timeout period. This takes care of the worst
480 * case where a starved transaction starts during an interval that
481 * meets the requirement "don't send an ordered tag" test so it takes
482 * us two intervals to determine that a tag must be sent.
484 #ifndef DA_ORDEREDTAG_INTERVAL
485 #define DA_ORDEREDTAG_INTERVAL 4
488 static struct periph_driver dadriver =
491 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
494 DATA_SET(periphdriver_set, dadriver);
496 /* For 2.2-stable support */
501 static struct cdevsw da_cdevsw = {
503 /* maj */ DA_CDEV_MAJOR,
511 /* write */ physwrite,
515 /* strategy */ dastrategy,
520 static SLIST_HEAD(,da_softc) softc_list;
521 static struct extend_array *daperiphs;
524 daopen(dev_t dev, int flags, int fmt, struct thread *td)
526 struct cam_periph *periph;
527 struct da_softc *softc;
528 struct disklabel *label;
535 periph = cam_extend_get(daperiphs, unit);
539 softc = (struct da_softc *)periph->softc;
541 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
542 ("daopen: dev=%s (unit %d , partition %d)\n", devtoname(dev),
545 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
546 return (error); /* error code from tsleep */
549 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
551 softc->flags |= DA_FLAG_OPEN;
554 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
555 /* Invalidate our pack information. */
556 disk_invalidate(&softc->disk);
557 softc->flags &= ~DA_FLAG_PACK_INVALID;
561 /* Do a read capacity */
563 struct scsi_read_capacity_data *rcap;
566 rcap = malloc(sizeof(*rcap), M_TEMP, M_INTWAIT | M_ZERO);
568 ccb = cam_periph_getccb(periph, /*priority*/1);
569 scsi_read_capacity(&ccb->csio,
576 ccb->ccb_h.ccb_bio = NULL;
578 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
579 /*sense_flags*/SF_RETRY_UA |
581 &softc->device_stats);
583 xpt_release_ccb(ccb);
586 dasetgeom(periph, rcap);
593 struct ccb_getdev cgd;
595 /* Build label for whole disk. */
596 label = &softc->disk.d_label;
597 bzero(label, sizeof(*label));
598 label->d_type = DTYPE_SCSI;
601 * Grab the inquiry data to get the vendor and product names.
602 * Put them in the typename and packname for the label.
604 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
605 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
606 xpt_action((union ccb *)&cgd);
608 strncpy(label->d_typename, cgd.inq_data.vendor,
609 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
610 strncpy(label->d_packname, cgd.inq_data.product,
611 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
613 label->d_secsize = softc->params.secsize;
614 label->d_nsectors = softc->params.secs_per_track;
615 label->d_ntracks = softc->params.heads;
616 label->d_ncylinders = softc->params.cylinders;
617 label->d_secpercyl = softc->params.heads
618 * softc->params.secs_per_track;
619 label->d_secperunit = softc->params.sectors;
621 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
622 (softc->quirks & DA_Q_NO_PREVENT) == 0)
623 daprevent(periph, PR_PREVENT);
626 * Check to see whether or not the blocksize is set yet.
627 * If it isn't, set it and then clear the blocksize
628 * unavailable flag for the device statistics.
630 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0){
631 softc->device_stats.block_size = softc->params.secsize;
632 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
637 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
638 (softc->quirks & DA_Q_NO_PREVENT) == 0)
639 daprevent(periph, PR_ALLOW);
640 softc->flags &= ~DA_FLAG_OPEN;
641 cam_periph_release(periph);
643 cam_periph_unlock(periph);
648 daclose(dev_t dev, int flag, int fmt, struct thread *td)
650 struct cam_periph *periph;
651 struct da_softc *softc;
656 periph = cam_extend_get(daperiphs, unit);
660 softc = (struct da_softc *)periph->softc;
662 if ((error = cam_periph_lock(periph, 0)) != 0) {
663 return (error); /* error code from tsleep */
666 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
669 ccb = cam_periph_getccb(periph, /*priority*/1);
671 scsi_synchronize_cache(&ccb->csio,
675 /*begin_lba*/0,/* Cover the whole disk */
680 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
681 /*sense_flags*/SF_RETRY_UA,
682 &softc->device_stats);
684 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
685 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
686 CAM_SCSI_STATUS_ERROR) {
688 int sense_key, error_code;
690 scsi_extract_sense(&ccb->csio.sense_data,
694 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
695 scsi_sense_print(&ccb->csio);
697 xpt_print_path(periph->path);
698 printf("Synchronize cache failed, status "
699 "== 0x%x, scsi status == 0x%x\n",
700 ccb->csio.ccb_h.status,
701 ccb->csio.scsi_status);
705 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
706 cam_release_devq(ccb->ccb_h.path,
712 xpt_release_ccb(ccb);
716 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
717 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
718 daprevent(periph, PR_ALLOW);
720 * If we've got removeable media, mark the blocksize as
721 * unavailable, since it could change when new media is
724 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
727 softc->flags &= ~DA_FLAG_OPEN;
728 cam_periph_unlock(periph);
729 cam_periph_release(periph);
734 * Actually translate the requested transfer into one the physical driver
735 * can understand. The transfer is described by a buf and will include
736 * only one physical transfer.
739 dastrategy(dev_t dev, struct bio *bio)
741 struct buf *bp = bio->bio_buf;
742 struct cam_periph *periph;
743 struct da_softc *softc;
749 periph = cam_extend_get(daperiphs, unit);
750 if (periph == NULL) {
754 softc = (struct da_softc *)periph->softc;
757 * check it's not too big a transfer for our adapter
759 scsi_minphys(bp, &sd_switch);
763 * Mask interrupts so that the pack cannot be invalidated until
764 * after we are in the queue. Otherwise, we might not properly
765 * clean up one of the buffers.
770 * If the device has been made invalid, error out
772 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
779 * Place it in the queue of disk activities for this disk
781 bioqdisksort(&softc->bio_queue, bio);
786 * Schedule ourselves for performing the work.
788 xpt_schedule(periph, /* XXX priority */1);
792 bp->b_flags |= B_ERROR;
795 * Correctly set the buf to indicate a completed xfer
797 bp->b_resid = bp->b_bcount;
801 /* For 2.2-stable support */
807 daioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
809 struct cam_periph *periph;
810 struct da_softc *softc;
815 periph = cam_extend_get(daperiphs, unit);
819 softc = (struct da_softc *)periph->softc;
821 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("daioctl\n"));
823 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
824 return (error); /* error code from tsleep */
827 error = cam_periph_ioctl(periph, cmd, addr, daerror);
829 cam_periph_unlock(periph);
835 dadump(dev_t dev, u_int num, u_int blknum, u_int secsize)
837 struct cam_periph *periph;
838 struct da_softc *softc;
842 struct ccb_scsiio csio;
843 int dumppages = MAXDUMPPGS;
846 /* toss any characters present prior to dump */
847 while (cncheckc() != -1)
851 periph = cam_extend_get(daperiphs, unit);
852 if (periph == NULL) {
855 softc = (struct da_softc *)periph->softc;
857 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0)
860 addr = 0; /* starting address */
861 blkcnt = howmany(PAGE_SIZE, secsize);
866 if ((num / blkcnt) < dumppages)
867 dumppages = num / blkcnt;
869 for (i = 0; i < dumppages; ++i) {
870 vm_paddr_t a = addr + (i * PAGE_SIZE);
871 if (is_physical_memory(a))
872 va = pmap_kenter_temporary(trunc_page(a), i);
874 va = pmap_kenter_temporary(trunc_page(0), i);
877 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
878 csio.ccb_h.ccb_state = DA_CCB_DUMP;
879 scsi_read_write(&csio,
885 /*minimum_cmd_size*/ softc->minimum_cmd_size,
888 /*data_ptr*/(u_int8_t *) va,
889 /*dxfer_len*/blkcnt * secsize * dumppages,
890 /*sense_len*/SSD_FULL_SIZE,
891 DA_DEFAULT_TIMEOUT * 1000);
892 xpt_polled_action((union ccb *)&csio);
894 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
895 printf("Aborting dump due to I/O error.\n");
896 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
897 CAM_SCSI_STATUS_ERROR)
898 scsi_sense_print(&csio);
900 printf("status == 0x%x, scsi status == 0x%x\n",
901 csio.ccb_h.status, csio.scsi_status);
905 if (dumpstatus(addr, (off_t)num * softc->params.secsize) < 0)
908 /* update block count */
909 num -= blkcnt * dumppages;
910 blknum += blkcnt * dumppages;
911 addr += PAGE_SIZE * dumppages;
915 * Sync the disk cache contents to the physical media.
917 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
919 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
920 csio.ccb_h.ccb_state = DA_CCB_DUMP;
921 scsi_synchronize_cache(&csio,
925 /*begin_lba*/0,/* Cover the whole disk */
929 xpt_polled_action((union ccb *)&csio);
931 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
932 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
933 CAM_SCSI_STATUS_ERROR) {
935 int sense_key, error_code;
937 scsi_extract_sense(&csio.sense_data,
941 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
942 scsi_sense_print(&csio);
944 xpt_print_path(periph->path);
945 printf("Synchronize cache failed, status "
946 "== 0x%x, scsi status == 0x%x\n",
947 csio.ccb_h.status, csio.scsi_status);
958 struct cam_path *path;
961 * Create our extend array for storing the devices we attach to.
963 daperiphs = cam_extend_new();
964 SLIST_INIT(&softc_list);
965 if (daperiphs == NULL) {
966 printf("da: Failed to alloc extend array!\n");
970 callout_init(&dasendorderedtag_ch);
973 * Install a global async callback. This callback will
974 * receive async callbacks like "new device found".
976 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
977 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
979 if (status == CAM_REQ_CMP) {
980 struct ccb_setasync csa;
982 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
983 csa.ccb_h.func_code = XPT_SASYNC_CB;
984 csa.event_enable = AC_FOUND_DEVICE;
985 csa.callback = daasync;
986 csa.callback_arg = NULL;
987 xpt_action((union ccb *)&csa);
988 status = csa.ccb_h.status;
992 if (status != CAM_REQ_CMP) {
993 printf("da: Failed to attach master async callback "
994 "due to status 0x%x!\n", status);
998 * Schedule a periodic event to occasionally send an
999 * ordered tag to a device.
1001 callout_reset(&dasendorderedtag_ch,
1002 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
1003 dasendorderedtag, NULL);
1005 /* Register our shutdown event handler */
1006 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1007 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1008 printf("dainit: shutdown event registration failed!\n");
1013 daoninvalidate(struct cam_periph *periph)
1015 struct da_softc *softc;
1018 struct ccb_setasync csa;
1020 softc = (struct da_softc *)periph->softc;
1023 * De-register any async callbacks.
1025 xpt_setup_ccb(&csa.ccb_h, periph->path,
1027 csa.ccb_h.func_code = XPT_SASYNC_CB;
1028 csa.event_enable = 0;
1029 csa.callback = daasync;
1030 csa.callback_arg = periph;
1031 xpt_action((union ccb *)&csa);
1033 softc->flags |= DA_FLAG_PACK_INVALID;
1036 * Use a critical section to keep the buffer queue from being
1037 * modified while we traverse it.
1042 * Return all queued I/O with ENXIO.
1043 * XXX Handle any transactions queued to the card
1044 * with XPT_ABORT_CCB.
1046 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL){
1047 bioq_remove(&softc->bio_queue, q_bio);
1048 q_bp = q_bio->bio_buf;
1049 q_bp->b_resid = q_bp->b_bcount;
1050 q_bp->b_error = ENXIO;
1051 q_bp->b_flags |= B_ERROR;
1056 SLIST_REMOVE(&softc_list, softc, da_softc, links);
1058 xpt_print_path(periph->path);
1059 printf("lost device\n");
1063 dacleanup(struct cam_periph *periph)
1065 struct da_softc *softc;
1067 softc = (struct da_softc *)periph->softc;
1069 devstat_remove_entry(&softc->device_stats);
1070 cam_extend_release(daperiphs, periph->unit_number);
1071 xpt_print_path(periph->path);
1072 printf("removing device entry\n");
1074 * If we can't free the sysctl tree, oh well...
1076 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1077 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1078 xpt_print_path(periph->path);
1079 printf("can't remove sysctl context\n");
1081 if (softc->disk.d_rawdev) {
1082 disk_destroy(&softc->disk);
1084 free(softc, M_DEVBUF);
1088 daasync(void *callback_arg, u_int32_t code,
1089 struct cam_path *path, void *arg)
1091 struct cam_periph *periph;
1093 periph = (struct cam_periph *)callback_arg;
1095 case AC_FOUND_DEVICE:
1097 struct ccb_getdev *cgd;
1100 cgd = (struct ccb_getdev *)arg;
1102 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1103 && SID_TYPE(&cgd->inq_data) != T_RBC
1104 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1108 * Allocate a peripheral instance for
1109 * this device and start the probe
1112 status = cam_periph_alloc(daregister, daoninvalidate,
1114 "da", CAM_PERIPH_BIO,
1115 cgd->ccb_h.path, daasync,
1116 AC_FOUND_DEVICE, cgd);
1118 if (status != CAM_REQ_CMP
1119 && status != CAM_REQ_INPROG)
1120 printf("daasync: Unable to attach to new device "
1121 "due to status 0x%x\n", status);
1127 struct da_softc *softc;
1128 struct ccb_hdr *ccbh;
1130 softc = (struct da_softc *)periph->softc;
1133 * Don't fail on the expected unit attention
1136 softc->flags |= DA_FLAG_RETRY_UA;
1137 for (ccbh = LIST_FIRST(&softc->pending_ccbs);
1138 ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le))
1139 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1144 cam_periph_async(periph, code, path, arg);
1150 dasysctlinit(void *context, int pending)
1152 struct cam_periph *periph;
1153 struct da_softc *softc;
1154 char tmpstr[80], tmpstr2[80];
1156 periph = (struct cam_periph *)context;
1157 softc = (struct da_softc *)periph->softc;
1159 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1160 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1162 sysctl_ctx_init(&softc->sysctl_ctx);
1163 softc->flags |= DA_FLAG_SCTX_INIT;
1164 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1165 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1166 CTLFLAG_RD, 0, tmpstr);
1167 if (softc->sysctl_tree == NULL) {
1168 printf("dasysctlinit: unable to allocate sysctl tree\n");
1173 * Now register the sysctl handler, so the user can the value on
1176 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1177 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1178 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1179 "Minimum CDB size");
1183 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1187 value = *(int *)arg1;
1189 error = sysctl_handle_int(oidp, &value, 0, req);
1192 || (req->newptr == NULL))
1196 * Acceptable values here are 6, 10 or 12. It's possible we may
1197 * support a 16 byte minimum command size in the future, since
1198 * there are now READ(16) and WRITE(16) commands defined in the
1203 else if ((value > 6)
1206 else if (value > 10)
1209 *(int *)arg1 = value;
1215 daregister(struct cam_periph *periph, void *arg)
1217 struct da_softc *softc;
1218 struct ccb_setasync csa;
1219 struct ccb_pathinq cpi;
1220 struct ccb_getdev *cgd;
1224 cgd = (struct ccb_getdev *)arg;
1225 if (periph == NULL) {
1226 printf("daregister: periph was NULL!!\n");
1227 return(CAM_REQ_CMP_ERR);
1231 printf("daregister: no getdev CCB, can't register device\n");
1232 return(CAM_REQ_CMP_ERR);
1235 softc = malloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
1236 LIST_INIT(&softc->pending_ccbs);
1237 softc->state = DA_STATE_PROBE;
1238 bioq_init(&softc->bio_queue);
1239 if (SID_IS_REMOVABLE(&cgd->inq_data))
1240 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1241 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1242 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1244 periph->softc = softc;
1246 cam_extend_set(daperiphs, periph->unit_number, periph);
1249 * See if this device has any quirks.
1251 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1252 (caddr_t)da_quirk_table,
1253 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1254 sizeof(*da_quirk_table), scsi_inquiry_match);
1257 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1259 softc->quirks = DA_Q_NONE;
1261 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1263 /* Check if the SIM does not want 6 byte commands */
1264 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
1265 cpi.ccb_h.func_code = XPT_PATH_INQ;
1266 xpt_action((union ccb *)&cpi);
1267 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1268 softc->quirks |= DA_Q_NO_6_BYTE;
1271 * RBC devices don't have to support READ(6), only READ(10).
1273 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1274 softc->minimum_cmd_size = 10;
1276 softc->minimum_cmd_size = 6;
1279 * Load the user's default, if any.
1281 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1282 periph->unit_number);
1283 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1286 * 6, 10 and 12 are the currently permissible values.
1288 if (softc->minimum_cmd_size < 6)
1289 softc->minimum_cmd_size = 6;
1290 else if ((softc->minimum_cmd_size > 6)
1291 && (softc->minimum_cmd_size <= 10))
1292 softc->minimum_cmd_size = 10;
1293 else if (softc->minimum_cmd_size > 12)
1294 softc->minimum_cmd_size = 12;
1297 * Block our timeout handler while we
1298 * add this softc to the dev list.
1301 SLIST_INSERT_HEAD(&softc_list, softc, links);
1305 * The DA driver supports a blocksize, but
1306 * we don't know the blocksize until we do
1307 * a read capacity. So, set a flag to
1308 * indicate that the blocksize is
1309 * unavailable right now. We'll clear the
1310 * flag as soon as we've done a read capacity.
1312 devstat_add_entry(&softc->device_stats, "da",
1313 periph->unit_number, 0,
1314 DEVSTAT_BS_UNAVAILABLE,
1315 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
1316 DEVSTAT_PRIORITY_DISK);
1319 * Register this media as a disk
1321 disk_create(periph->unit_number, &softc->disk, 0, &da_cdevsw);
1324 * Add async callbacks for bus reset and
1325 * bus device reset calls. I don't bother
1326 * checking if this fails as, in most cases,
1327 * the system will function just fine without
1328 * them and the only alternative would be to
1329 * not attach the device on failure.
1331 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
1332 csa.ccb_h.func_code = XPT_SASYNC_CB;
1333 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
1334 csa.callback = daasync;
1335 csa.callback_arg = periph;
1336 xpt_action((union ccb *)&csa);
1338 * Lock this peripheral until we are setup.
1339 * This first call can't block
1341 cam_periph_lock(periph, 0);
1342 xpt_schedule(periph, /*priority*/5);
1344 return(CAM_REQ_CMP);
1348 dastart(struct cam_periph *periph, union ccb *start_ccb)
1350 struct da_softc *softc;
1352 softc = (struct da_softc *)periph->softc;
1355 switch (softc->state) {
1356 case DA_STATE_NORMAL:
1358 /* Pull a buffer from the queue and get going on it */
1363 * See if there is a buf with work for us to do..
1366 bio = bioq_first(&softc->bio_queue);
1367 if (periph->immediate_priority <= periph->pinfo.priority) {
1368 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1369 ("queuing for immediate ccb\n"));
1370 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1371 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1373 periph->immediate_priority = CAM_PRIORITY_NONE;
1375 wakeup(&periph->ccb_list);
1376 } else if (bio == NULL) {
1378 xpt_release_ccb(start_ccb);
1382 bioq_remove(&softc->bio_queue, bio);
1385 devstat_start_transaction(&softc->device_stats);
1387 if ((bp->b_flags & B_ORDERED) != 0
1388 || (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1389 softc->flags &= ~DA_FLAG_NEED_OTAG;
1390 softc->ordered_tag_count++;
1391 tag_code = MSG_ORDERED_Q_TAG;
1393 tag_code = MSG_SIMPLE_Q_TAG;
1395 scsi_read_write(&start_ccb->csio,
1396 /*retries*/da_retry_count,
1399 bp->b_flags & B_READ,
1401 softc->minimum_cmd_size,
1403 bp->b_bcount / softc->params.secsize,
1406 /*sense_len*/SSD_FULL_SIZE,
1407 da_default_timeout * 1000);
1408 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1411 * Block out any asyncronous callbacks
1412 * while we touch the pending ccb list.
1414 LIST_INSERT_HEAD(&softc->pending_ccbs,
1415 &start_ccb->ccb_h, periph_links.le);
1417 /* We expect a unit attention from this device */
1418 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1419 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1420 softc->flags &= ~DA_FLAG_RETRY_UA;
1423 start_ccb->ccb_h.ccb_bio = bio;
1424 bio = bioq_first(&softc->bio_queue);
1427 xpt_action(start_ccb);
1431 /* Have more work to do, so ensure we stay scheduled */
1432 xpt_schedule(periph, /* XXX priority */1);
1436 case DA_STATE_PROBE:
1438 struct ccb_scsiio *csio;
1439 struct scsi_read_capacity_data *rcap;
1441 rcap = malloc(sizeof(*rcap), M_TEMP, M_INTWAIT | M_ZERO);
1442 csio = &start_ccb->csio;
1443 scsi_read_capacity(csio,
1450 start_ccb->ccb_h.ccb_bio = NULL;
1451 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1452 xpt_action(start_ccb);
1459 cmd6workaround(union ccb *ccb)
1461 struct scsi_rw_6 cmd6;
1462 struct scsi_rw_10 *cmd10;
1463 struct da_softc *softc;
1467 cdb = ccb->csio.cdb_io.cdb_bytes;
1469 /* Translation only possible if CDB is an array and cmd is R/W6 */
1470 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1471 (*cdb != READ_6 && *cdb != WRITE_6))
1474 xpt_print_path(ccb->ccb_h.path);
1475 printf("READ(6)/WRITE(6) not supported, "
1476 "increasing minimum_cmd_size to 10.\n");
1477 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1478 softc->minimum_cmd_size = 10;
1480 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1481 cmd10 = (struct scsi_rw_10 *)cdb;
1482 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1484 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1485 cmd10->reserved = 0;
1486 scsi_ulto2b(cmd6.length, cmd10->length);
1487 cmd10->control = cmd6.control;
1488 ccb->csio.cdb_len = sizeof(*cmd10);
1490 /* Requeue request, unfreezing queue if necessary */
1491 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1492 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1495 cam_release_devq(ccb->ccb_h.path,
1499 /*getcount_only*/0);
1505 dadone(struct cam_periph *periph, union ccb *done_ccb)
1507 struct da_softc *softc;
1508 struct ccb_scsiio *csio;
1510 softc = (struct da_softc *)periph->softc;
1511 csio = &done_ccb->csio;
1512 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1513 case DA_CCB_BUFFER_IO:
1518 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1520 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1524 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1529 /* Retry selection timeouts */
1530 sf |= SF_RETRY_SELTO;
1532 if ((error = daerror(done_ccb, 0, sf)) == ERESTART) {
1534 * A retry was scheuled, so
1545 if (error == ENXIO) {
1547 * Catastrophic error. Mark our pack as
1550 /* XXX See if this is really a media
1553 xpt_print_path(periph->path);
1554 printf("Invalidating pack\n");
1555 softc->flags |= DA_FLAG_PACK_INVALID;
1559 * return all queued I/O with EIO, so that
1560 * the client can retry these I/Os in the
1561 * proper order should it attempt to recover.
1563 while ((q_bio = bioq_first(&softc->bio_queue))
1565 bioq_remove(&softc->bio_queue, q_bio);
1566 q_bp = q_bio->bio_buf;
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 bp->b_flags |= B_ERROR;
1582 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1583 cam_release_devq(done_ccb->ccb_h.path,
1587 /*getcount_only*/0);
1589 bp->b_resid = csio->resid;
1590 if (csio->resid > 0)
1591 bp->b_flags |= B_ERROR;
1595 * Block out any asyncronous callbacks
1596 * while we touch the pending ccb list.
1599 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1602 if (softc->device_stats.busy_count == 0)
1603 softc->flags |= DA_FLAG_WENT_IDLE;
1605 devstat_end_transaction_buf(&softc->device_stats, bp);
1611 struct scsi_read_capacity_data *rdcap;
1612 char announce_buf[80];
1614 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1616 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1617 struct disk_params *dp;
1619 dasetgeom(periph, rdcap);
1620 dp = &softc->params;
1621 snprintf(announce_buf, sizeof(announce_buf),
1622 "%luMB (%u %u byte sectors: %dH %dS/T %dC)",
1623 (unsigned long) (((u_int64_t)dp->secsize *
1624 dp->sectors) / (1024*1024)), dp->sectors,
1625 dp->secsize, dp->heads, dp->secs_per_track,
1630 announce_buf[0] = '\0';
1633 * Retry any UNIT ATTENTION type errors. They
1634 * are expected at boot.
1636 error = daerror(done_ccb, 0, SF_RETRY_UA |
1637 SF_RETRY_SELTO | SF_NO_PRINT);
1638 if (error == ERESTART) {
1640 * A retry was scheuled, so
1644 } else if (error != 0) {
1645 struct scsi_sense_data *sense;
1647 int sense_key, error_code;
1650 struct ccb_getdev cgd;
1652 /* Don't wedge this device's queue */
1653 cam_release_devq(done_ccb->ccb_h.path,
1657 /*getcount_only*/0);
1659 status = done_ccb->ccb_h.status;
1661 xpt_setup_ccb(&cgd.ccb_h,
1662 done_ccb->ccb_h.path,
1664 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1665 xpt_action((union ccb *)&cgd);
1667 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1668 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1669 || ((status & CAM_AUTOSNS_VALID) == 0))
1675 sense = &csio->sense_data;
1676 scsi_extract_sense(sense, &error_code,
1681 * Attach to anything that claims to be a
1682 * direct access or optical disk device,
1683 * as long as it doesn't return a "Logical
1684 * unit not supported" (0x25) error.
1686 if ((have_sense) && (asc != 0x25)
1687 && (error_code == SSD_CURRENT_ERROR))
1688 snprintf(announce_buf,
1689 sizeof(announce_buf),
1690 "Attempt to query device "
1691 "size failed: %s, %s",
1692 scsi_sense_key_text[sense_key],
1693 scsi_sense_desc(asc,ascq,
1700 xpt_print_path(periph->path);
1701 printf("got CAM status %#x\n",
1702 done_ccb->ccb_h.status);
1705 xpt_print_path(periph->path);
1706 printf("fatal error, failed"
1707 " to attach to device\n");
1710 * Free up resources.
1712 cam_periph_invalidate(periph);
1716 free(rdcap, M_TEMP);
1717 if (announce_buf[0] != '\0') {
1718 xpt_announce_periph(periph, announce_buf);
1720 * Create our sysctl variables, now that we know
1721 * we have successfully attached.
1723 taskqueue_enqueue(taskqueue_thread[mycpuid],
1724 &softc->sysctl_task);
1726 softc->state = DA_STATE_NORMAL;
1728 * Since our peripheral may be invalidated by an error
1729 * above or an external event, we must release our CCB
1730 * before releasing the probe lock on the peripheral.
1731 * The peripheral will only go away once the last lock
1732 * is removed, and we need it around for the CCB release
1735 xpt_release_ccb(done_ccb);
1736 cam_periph_unlock(periph);
1739 case DA_CCB_WAITING:
1741 /* Caller will release the CCB */
1742 wakeup(&done_ccb->ccb_h.cbfcnp);
1746 /* No-op. We're polling */
1751 xpt_release_ccb(done_ccb);
1755 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1757 struct da_softc *softc;
1758 struct cam_periph *periph;
1761 periph = xpt_path_periph(ccb->ccb_h.path);
1762 softc = (struct da_softc *)periph->softc;
1765 * Automatically detect devices that do not support
1766 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
1769 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
1770 error = cmd6workaround(ccb);
1771 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
1772 CAM_SCSI_STATUS_ERROR)
1773 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
1774 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1775 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
1776 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
1777 int sense_key, error_code, asc, ascq;
1779 scsi_extract_sense(&ccb->csio.sense_data,
1780 &error_code, &sense_key, &asc, &ascq);
1781 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
1782 error = cmd6workaround(ccb);
1784 if (error == ERESTART)
1789 * Until we have a better way of doing pack validation,
1790 * don't treat UAs as errors.
1792 sense_flags |= SF_RETRY_UA;
1793 return(cam_periph_error(ccb, cam_flags, sense_flags,
1794 &softc->saved_ccb));
1798 daprevent(struct cam_periph *periph, int action)
1800 struct da_softc *softc;
1804 softc = (struct da_softc *)periph->softc;
1806 if (((action == PR_ALLOW)
1807 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
1808 || ((action == PR_PREVENT)
1809 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
1813 ccb = cam_periph_getccb(periph, /*priority*/1);
1815 scsi_prevent(&ccb->csio,
1823 error = cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
1824 /*sense_flags*/0, &softc->device_stats);
1827 if (action == PR_ALLOW)
1828 softc->flags &= ~DA_FLAG_PACK_LOCKED;
1830 softc->flags |= DA_FLAG_PACK_LOCKED;
1833 xpt_release_ccb(ccb);
1837 dasetgeom(struct cam_periph *periph, struct scsi_read_capacity_data * rdcap)
1839 struct ccb_calc_geometry ccg;
1840 struct da_softc *softc;
1841 struct disk_params *dp;
1843 softc = (struct da_softc *)periph->softc;
1845 dp = &softc->params;
1846 dp->secsize = scsi_4btoul(rdcap->length);
1847 dp->sectors = scsi_4btoul(rdcap->addr) + 1;
1849 * Have the controller provide us with a geometry
1850 * for this disk. The only time the geometry
1851 * matters is when we boot and the controller
1852 * is the only one knowledgeable enough to come
1853 * up with something that will make this a bootable
1856 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
1857 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
1858 ccg.block_size = dp->secsize;
1859 ccg.volume_size = dp->sectors;
1861 ccg.secs_per_track = 0;
1863 xpt_action((union ccb*)&ccg);
1864 dp->heads = ccg.heads;
1865 dp->secs_per_track = ccg.secs_per_track;
1866 dp->cylinders = ccg.cylinders;
1870 dasendorderedtag(void *arg)
1872 struct da_softc *softc;
1874 for (softc = SLIST_FIRST(&softc_list);
1876 softc = SLIST_NEXT(softc, links)) {
1878 if ((softc->ordered_tag_count == 0)
1879 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
1880 softc->flags |= DA_FLAG_NEED_OTAG;
1882 if (softc->device_stats.busy_count > 0)
1883 softc->flags &= ~DA_FLAG_WENT_IDLE;
1885 softc->ordered_tag_count = 0;
1888 /* Queue us up again */
1889 callout_reset(&dasendorderedtag_ch,
1890 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
1891 dasendorderedtag, NULL);
1895 * Step through all DA peripheral drivers, and if the device is still open,
1896 * sync the disk cache to physical media.
1899 dashutdown(void * arg, int howto)
1901 struct cam_periph *periph;
1902 struct da_softc *softc;
1904 for (periph = TAILQ_FIRST(&dadriver.units); periph != NULL;
1905 periph = TAILQ_NEXT(periph, unit_links)) {
1907 softc = (struct da_softc *)periph->softc;
1910 * We only sync the cache if the drive is still open, and
1911 * if the drive is capable of it..
1913 if (((softc->flags & DA_FLAG_OPEN) == 0)
1914 || (softc->quirks & DA_Q_NO_SYNC_CACHE))
1917 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
1919 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
1920 scsi_synchronize_cache(&ccb.csio,
1924 /*begin_lba*/0, /* whole disk */
1929 xpt_polled_action(&ccb);
1931 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1932 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
1933 CAM_SCSI_STATUS_ERROR)
1934 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
1935 int error_code, sense_key, asc, ascq;
1937 scsi_extract_sense(&ccb.csio.sense_data,
1938 &error_code, &sense_key,
1941 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1942 scsi_sense_print(&ccb.csio);
1944 xpt_print_path(periph->path);
1945 printf("Synchronize cache failed, status "
1946 "== 0x%x, scsi status == 0x%x\n",
1947 ccb.ccb_h.status, ccb.csio.scsi_status);
1951 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1952 cam_release_devq(ccb.ccb_h.path,
1956 /*getcount_only*/0);
1961 #else /* !_KERNEL */
1964 * XXX This is only left out of the kernel build to silence warnings. If,
1965 * for some reason this function is used in the kernel, the ifdefs should
1966 * be moved so it is included both in the kernel and userland.
1969 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
1970 void (*cbfcnp)(struct cam_periph *, union ccb *),
1971 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
1972 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
1975 struct scsi_format_unit *scsi_cmd;
1977 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
1978 scsi_cmd->opcode = FORMAT_UNIT;
1979 scsi_cmd->byte2 = byte2;
1980 scsi_ulto2b(ileave, scsi_cmd->interleave);
1985 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
1994 #endif /* _KERNEL */