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.18 2004/09/13 23:57:41 drhodus 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>
50 #include <sys/eventhandler.h>
52 #include <sys/malloc.h>
58 #include <machine/md_var.h>
71 #include "../cam_ccb.h"
72 #include "../cam_extend.h"
73 #include "../cam_periph.h"
74 #include "../cam_xpt_periph.h"
76 #include "scsi_message.h"
89 DA_FLAG_PACK_INVALID = 0x001,
90 DA_FLAG_NEW_PACK = 0x002,
91 DA_FLAG_PACK_LOCKED = 0x004,
92 DA_FLAG_PACK_REMOVABLE = 0x008,
93 DA_FLAG_TAGGED_QUEUING = 0x010,
94 DA_FLAG_NEED_OTAG = 0x020,
95 DA_FLAG_WENT_IDLE = 0x040,
96 DA_FLAG_RETRY_UA = 0x080,
98 DA_FLAG_SCTX_INIT = 0x200
103 DA_Q_NO_SYNC_CACHE = 0x01,
104 DA_Q_NO_6_BYTE = 0x02,
105 DA_Q_NO_PREVENT = 0x04
110 DA_CCB_BUFFER_IO = 0x02,
111 DA_CCB_WAITING = 0x03,
113 DA_CCB_TYPE_MASK = 0x0F,
114 DA_CCB_RETRY_UA = 0x10
117 /* Offsets into our private area for storing information */
118 #define ccb_state ppriv_field0
119 #define ccb_bp ppriv_ptr1
124 u_int8_t secs_per_track;
125 u_int32_t secsize; /* Number of bytes/sector */
126 u_int32_t sectors; /* total number sectors */
130 struct buf_queue_head buf_queue;
131 struct devstat device_stats;
132 SLIST_ENTRY(da_softc) links;
133 LIST_HEAD(, ccb_hdr) pending_ccbs;
137 int minimum_cmd_size;
138 int ordered_tag_count;
139 struct disk_params params;
142 struct task sysctl_task;
143 struct sysctl_ctx_list sysctl_ctx;
144 struct sysctl_oid *sysctl_tree;
147 struct da_quirk_entry {
148 struct scsi_inquiry_pattern inq_pat;
152 static const char quantum[] = "QUANTUM";
153 static const char microp[] = "MICROP";
155 static struct da_quirk_entry da_quirk_table[] =
157 /* SPI, FC devices */
160 * Fujitsu M2513A MO drives.
161 * Tested devices: M2513A2 firmware versions 1200 & 1300.
162 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
163 * Reported by: W.Scholten <whs@xs4all.nl>
165 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
166 /*quirks*/ DA_Q_NO_SYNC_CACHE
170 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
171 /*quirks*/ DA_Q_NO_SYNC_CACHE
175 * This particular Fujitsu drive doesn't like the
176 * synchronize cache command.
177 * Reported by: Tom Jackson <toj@gorilla.net>
179 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
180 /*quirks*/ DA_Q_NO_SYNC_CACHE
185 * This drive doesn't like the synchronize cache command
186 * either. Reported by: Matthew Jacob <mjacob@feral.com>
187 * in NetBSD PR kern/6027, August 24, 1998.
189 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
190 /*quirks*/ DA_Q_NO_SYNC_CACHE
194 * This drive doesn't like the synchronize cache command
195 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
198 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
199 /*quirks*/ DA_Q_NO_SYNC_CACHE
203 * Doesn't like the synchronize cache command.
204 * Reported by: Blaz Zupan <blaz@gold.amis.net>
206 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
207 /*quirks*/ DA_Q_NO_SYNC_CACHE
211 * Doesn't like the synchronize cache command.
213 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
214 /*quirks*/ DA_Q_NO_SYNC_CACHE
218 * Doesn't like the synchronize cache command.
220 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
221 /*quirks*/ DA_Q_NO_SYNC_CACHE
225 * Doesn't work correctly with 6 byte reads/writes.
226 * Returns illegal request, and points to byte 9 of the
228 * Reported by: Adam McDougall <bsdx@spawnet.com>
230 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
231 /*quirks*/ DA_Q_NO_6_BYTE
235 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
236 /*quirks*/ DA_Q_NO_6_BYTE
238 /* XXX USB floppy quirks temporarily enabled for 4.9R */
239 /* USB floppy devices supported by umass(4) */
242 * This USB floppy drive uses the UFI command set. This
243 * command set is a derivative of the ATAPI command set and
244 * does not support READ_6 commands only READ_10. It also does
245 * not support sync cache (0x35).
247 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Y-E DATA", "USB-FDU", "*"},
248 /*quirks*/ DA_Q_NO_SYNC_CACHE
251 /* Another USB floppy */
252 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MATSHITA", "FDD CF-VFDU*","*"},
253 /*quirks*/ DA_Q_NO_SYNC_CACHE
257 * The vendor, product and version strings coming from the
258 * controller are null terminated instead of being padded with
259 * spaces. The trailing wildcard character '*' is required.
261 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SMSC*", "USB FDC*","*"},
262 /*quirks*/ DA_Q_NO_SYNC_CACHE
266 * SmartDisk (Mitsumi) USB floppy drive
269 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MITSUMI", "USB FDD", "*"},
270 /*quirks*/ DA_Q_NO_SYNC_CACHE
273 /* USB mass storage devices supported by umass(4) */
276 * Sony Memory Stick adapter MSAC-US1 and
277 * Sony PCG-C1VJ Internal Memory Stick Slot (MSC-U01).
278 * Make all sony MS* products use this quirk.
280 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "MS*", "*"},
281 /*quirks*/ DA_Q_NO_SYNC_CACHE
285 * Sony Memory Stick adapter for the CLIE series
288 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "CLIE*", "*"},
289 /*quirks*/ DA_Q_NO_SYNC_CACHE
293 * Intelligent Stick USB disk-on-key
296 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB Card",
297 "IntelligentStick*", "*"},
298 /*quirks*/ DA_Q_NO_SYNC_CACHE
302 * Sony DSC cameras (DSC-S30, DSC-S50, DSC-S70)
304 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
305 /*quirks*/ DA_Q_NO_SYNC_CACHE
309 * Microtech USB CameraMate
311 {T_DIRECT, SIP_MEDIA_REMOVABLE, "eUSB Compact*",
312 "Compact Flash*", "*"},
313 /*quirks*/ DA_Q_NO_SYNC_CACHE
317 * Olympus digital cameras (C-3040ZOOM, C-2040ZOOM, C-1)
319 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "C-*", "*"},
320 /*quirks*/ DA_Q_NO_SYNC_CACHE
324 * Olympus digital cameras (E-100RS, E-10).
326 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "E-*", "*"},
327 /*quirks*/ DA_Q_NO_SYNC_CACHE
331 * KingByte Pen Drives
333 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NO BRAND", "PEN DRIVE", "*"},
334 /*quirks*/ DA_Q_NO_SYNC_CACHE
338 * iRiver iFP MP3 player (with UMS Firmware)
339 * PR: kern/54881, i386/63941, kern/66124
341 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
342 /*quirks*/ DA_Q_NO_SYNC_CACHE
348 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJIFILMUSB-DRIVEUNIT",
349 "USB-DRIVEUNIT", "*"},
350 /*quirks*/ DA_Q_NO_SYNC_CACHE
354 * Minolta Dimage E203
356 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MINOLTA", "DiMAGE E203", "*"},
357 /*quirks*/ DA_Q_NO_SYNC_CACHE
364 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Apacer", "HandyDrive", "*"},
365 /*quirks*/ DA_Q_NO_SYNC_CACHE
369 * Daisy Technology PhotoClip on Zoran chip
372 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ZORAN", "COACH", "*"},
373 /*quirks*/ DA_Q_NO_SYNC_CACHE
377 * Sony USB Key-Storage
380 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Storage Media", "*"},
381 /*quirks*/ DA_Q_NO_SYNC_CACHE
383 #endif /* DA_OLD_QUIRKS */
386 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
389 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
390 /*quirks*/ DA_Q_NO_SYNC_CACHE
394 * Jungsoft NEXDISK USB flash key
397 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
398 /*quirks*/ DA_Q_NO_SYNC_CACHE
402 * Creative Nomad MUVO mp3 player (USB)
405 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
406 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
410 static d_open_t daopen;
411 static d_close_t daclose;
412 static d_strategy_t dastrategy;
413 static d_ioctl_t daioctl;
414 static d_dump_t dadump;
415 static periph_init_t dainit;
416 static void daasync(void *callback_arg, u_int32_t code,
417 struct cam_path *path, void *arg);
418 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
419 static periph_ctor_t daregister;
420 static periph_dtor_t dacleanup;
421 static periph_start_t dastart;
422 static periph_oninv_t daoninvalidate;
423 static void dadone(struct cam_periph *periph,
424 union ccb *done_ccb);
425 static int daerror(union ccb *ccb, u_int32_t cam_flags,
426 u_int32_t sense_flags);
427 static void daprevent(struct cam_periph *periph, int action);
428 static void dasetgeom(struct cam_periph *periph,
429 struct scsi_read_capacity_data * rdcap);
430 static timeout_t dasendorderedtag;
431 static void dashutdown(void *arg, int howto);
433 #ifndef DA_DEFAULT_TIMEOUT
434 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
437 #ifndef DA_DEFAULT_RETRY
438 #define DA_DEFAULT_RETRY 4
441 static int da_retry_count = DA_DEFAULT_RETRY;
442 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
444 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
445 "CAM Direct Access Disk driver");
446 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
447 &da_retry_count, 0, "Normal I/O retry count");
448 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
449 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
450 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
451 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
454 * DA_ORDEREDTAG_INTERVAL determines how often, relative
455 * to the default timeout, we check to see whether an ordered
456 * tagged transaction is appropriate to prevent simple tag
457 * starvation. Since we'd like to ensure that there is at least
458 * 1/2 of the timeout length left for a starved transaction to
459 * complete after we've sent an ordered tag, we must poll at least
460 * four times in every timeout period. This takes care of the worst
461 * case where a starved transaction starts during an interval that
462 * meets the requirement "don't send an ordered tag" test so it takes
463 * us two intervals to determine that a tag must be sent.
465 #ifndef DA_ORDEREDTAG_INTERVAL
466 #define DA_ORDEREDTAG_INTERVAL 4
469 static struct periph_driver dadriver =
472 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
475 DATA_SET(periphdriver_set, dadriver);
477 /* For 2.2-stable support */
482 static struct cdevsw da_cdevsw = {
484 /* maj */ DA_CDEV_MAJOR,
492 /* write */ physwrite,
496 /* strategy */ dastrategy,
501 static SLIST_HEAD(,da_softc) softc_list;
502 static struct extend_array *daperiphs;
505 daopen(dev_t dev, int flags, int fmt, struct thread *td)
507 struct cam_periph *periph;
508 struct da_softc *softc;
509 struct disklabel *label;
517 periph = cam_extend_get(daperiphs, unit);
521 softc = (struct da_softc *)periph->softc;
523 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
524 ("daopen: dev=%s (unit %d , partition %d)\n", devtoname(dev),
527 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
528 return (error); /* error code from tsleep */
531 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
533 softc->flags |= DA_FLAG_OPEN;
536 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
537 /* Invalidate our pack information. */
538 disk_invalidate(&softc->disk);
539 softc->flags &= ~DA_FLAG_PACK_INVALID;
543 /* Do a read capacity */
545 struct scsi_read_capacity_data *rcap;
548 rcap = malloc(sizeof(*rcap), M_TEMP, M_INTWAIT | M_ZERO);
550 ccb = cam_periph_getccb(periph, /*priority*/1);
551 scsi_read_capacity(&ccb->csio,
558 ccb->ccb_h.ccb_bp = NULL;
560 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
561 /*sense_flags*/SF_RETRY_UA |
563 &softc->device_stats);
565 xpt_release_ccb(ccb);
568 dasetgeom(periph, rcap);
575 struct ccb_getdev cgd;
577 /* Build label for whole disk. */
578 label = &softc->disk.d_label;
579 bzero(label, sizeof(*label));
580 label->d_type = DTYPE_SCSI;
583 * Grab the inquiry data to get the vendor and product names.
584 * Put them in the typename and packname for the label.
586 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
587 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
588 xpt_action((union ccb *)&cgd);
590 strncpy(label->d_typename, cgd.inq_data.vendor,
591 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
592 strncpy(label->d_packname, cgd.inq_data.product,
593 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
595 label->d_secsize = softc->params.secsize;
596 label->d_nsectors = softc->params.secs_per_track;
597 label->d_ntracks = softc->params.heads;
598 label->d_ncylinders = softc->params.cylinders;
599 label->d_secpercyl = softc->params.heads
600 * softc->params.secs_per_track;
601 label->d_secperunit = softc->params.sectors;
603 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
604 (softc->quirks & DA_Q_NO_PREVENT) == 0)
605 daprevent(periph, PR_PREVENT);
608 * Check to see whether or not the blocksize is set yet.
609 * If it isn't, set it and then clear the blocksize
610 * unavailable flag for the device statistics.
612 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0){
613 softc->device_stats.block_size = softc->params.secsize;
614 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
619 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
620 (softc->quirks & DA_Q_NO_PREVENT) == 0)
621 daprevent(periph, PR_ALLOW);
622 softc->flags &= ~DA_FLAG_OPEN;
623 cam_periph_release(periph);
625 cam_periph_unlock(periph);
630 daclose(dev_t dev, int flag, int fmt, struct thread *td)
632 struct cam_periph *periph;
633 struct da_softc *softc;
638 periph = cam_extend_get(daperiphs, unit);
642 softc = (struct da_softc *)periph->softc;
644 if ((error = cam_periph_lock(periph, 0)) != 0) {
645 return (error); /* error code from tsleep */
648 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
651 ccb = cam_periph_getccb(periph, /*priority*/1);
653 scsi_synchronize_cache(&ccb->csio,
657 /*begin_lba*/0,/* Cover the whole disk */
662 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
663 /*sense_flags*/SF_RETRY_UA,
664 &softc->device_stats);
666 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
667 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
668 CAM_SCSI_STATUS_ERROR) {
670 int sense_key, error_code;
672 scsi_extract_sense(&ccb->csio.sense_data,
676 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
677 scsi_sense_print(&ccb->csio);
679 xpt_print_path(periph->path);
680 printf("Synchronize cache failed, status "
681 "== 0x%x, scsi status == 0x%x\n",
682 ccb->csio.ccb_h.status,
683 ccb->csio.scsi_status);
687 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
688 cam_release_devq(ccb->ccb_h.path,
694 xpt_release_ccb(ccb);
698 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
699 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
700 daprevent(periph, PR_ALLOW);
702 * If we've got removeable media, mark the blocksize as
703 * unavailable, since it could change when new media is
706 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
709 softc->flags &= ~DA_FLAG_OPEN;
710 cam_periph_unlock(periph);
711 cam_periph_release(periph);
716 * Actually translate the requested transfer into one the physical driver
717 * can understand. The transfer is described by a buf and will include
718 * only one physical transfer.
721 dastrategy(struct buf *bp)
723 struct cam_periph *periph;
724 struct da_softc *softc;
729 unit = dkunit(bp->b_dev);
730 part = dkpart(bp->b_dev);
731 periph = cam_extend_get(daperiphs, unit);
732 if (periph == NULL) {
736 softc = (struct da_softc *)periph->softc;
739 * check it's not too big a transfer for our adapter
741 scsi_minphys(bp,&sd_switch);
745 * Mask interrupts so that the pack cannot be invalidated until
746 * after we are in the queue. Otherwise, we might not properly
747 * clean up one of the buffers.
752 * If the device has been made invalid, error out
754 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
761 * Place it in the queue of disk activities for this disk
763 bufqdisksort(&softc->buf_queue, bp);
768 * Schedule ourselves for performing the work.
770 xpt_schedule(periph, /* XXX priority */1);
774 bp->b_flags |= B_ERROR;
777 * Correctly set the buf to indicate a completed xfer
779 bp->b_resid = bp->b_bcount;
784 /* For 2.2-stable support */
790 daioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
792 struct cam_periph *periph;
793 struct da_softc *softc;
798 periph = cam_extend_get(daperiphs, unit);
802 softc = (struct da_softc *)periph->softc;
804 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("daioctl\n"));
806 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
807 return (error); /* error code from tsleep */
810 error = cam_periph_ioctl(periph, cmd, addr, daerror);
812 cam_periph_unlock(periph);
818 dadump(dev_t dev, u_int num, u_int blknum, u_int secsize)
820 struct cam_periph *periph;
821 struct da_softc *softc;
825 struct ccb_scsiio csio;
826 int dumppages = MAXDUMPPGS;
829 /* toss any characters present prior to dump */
830 while (cncheckc() != -1)
834 periph = cam_extend_get(daperiphs, unit);
835 if (periph == NULL) {
838 softc = (struct da_softc *)periph->softc;
840 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0)
843 addr = 0; /* starting address */
844 blkcnt = howmany(PAGE_SIZE, secsize);
849 if ((num / blkcnt) < dumppages)
850 dumppages = num / blkcnt;
852 for (i = 0; i < dumppages; ++i) {
853 vm_paddr_t a = addr + (i * PAGE_SIZE);
854 if (is_physical_memory(a))
855 va = pmap_kenter_temporary(trunc_page(a), i);
857 va = pmap_kenter_temporary(trunc_page(0), i);
860 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
861 csio.ccb_h.ccb_state = DA_CCB_DUMP;
862 scsi_read_write(&csio,
868 /*minimum_cmd_size*/ softc->minimum_cmd_size,
871 /*data_ptr*/(u_int8_t *) va,
872 /*dxfer_len*/blkcnt * secsize * dumppages,
873 /*sense_len*/SSD_FULL_SIZE,
874 DA_DEFAULT_TIMEOUT * 1000);
875 xpt_polled_action((union ccb *)&csio);
877 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
878 printf("Aborting dump due to I/O error.\n");
879 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
880 CAM_SCSI_STATUS_ERROR)
881 scsi_sense_print(&csio);
883 printf("status == 0x%x, scsi status == 0x%x\n",
884 csio.ccb_h.status, csio.scsi_status);
888 if (dumpstatus(addr, (off_t)num * softc->params.secsize) < 0)
891 /* update block count */
892 num -= blkcnt * dumppages;
893 blknum += blkcnt * dumppages;
894 addr += PAGE_SIZE * dumppages;
898 * Sync the disk cache contents to the physical media.
900 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
902 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
903 csio.ccb_h.ccb_state = DA_CCB_DUMP;
904 scsi_synchronize_cache(&csio,
908 /*begin_lba*/0,/* Cover the whole disk */
912 xpt_polled_action((union ccb *)&csio);
914 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
915 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
916 CAM_SCSI_STATUS_ERROR) {
918 int sense_key, error_code;
920 scsi_extract_sense(&csio.sense_data,
924 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
925 scsi_sense_print(&csio);
927 xpt_print_path(periph->path);
928 printf("Synchronize cache failed, status "
929 "== 0x%x, scsi status == 0x%x\n",
930 csio.ccb_h.status, csio.scsi_status);
941 struct cam_path *path;
944 * Create our extend array for storing the devices we attach to.
946 daperiphs = cam_extend_new();
947 SLIST_INIT(&softc_list);
948 if (daperiphs == NULL) {
949 printf("da: Failed to alloc extend array!\n");
954 * Install a global async callback. This callback will
955 * receive async callbacks like "new device found".
957 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
958 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
960 if (status == CAM_REQ_CMP) {
961 struct ccb_setasync csa;
963 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
964 csa.ccb_h.func_code = XPT_SASYNC_CB;
965 csa.event_enable = AC_FOUND_DEVICE;
966 csa.callback = daasync;
967 csa.callback_arg = NULL;
968 xpt_action((union ccb *)&csa);
969 status = csa.ccb_h.status;
973 if (status != CAM_REQ_CMP) {
974 printf("da: Failed to attach master async callback "
975 "due to status 0x%x!\n", status);
979 * Schedule a periodic event to occasionally send an
980 * ordered tag to a device.
982 timeout(dasendorderedtag, NULL,
983 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL);
985 /* Register our shutdown event handler */
986 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
987 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
988 printf("dainit: shutdown event registration failed!\n");
993 daoninvalidate(struct cam_periph *periph)
996 struct da_softc *softc;
998 struct ccb_setasync csa;
1000 softc = (struct da_softc *)periph->softc;
1003 * De-register any async callbacks.
1005 xpt_setup_ccb(&csa.ccb_h, periph->path,
1007 csa.ccb_h.func_code = XPT_SASYNC_CB;
1008 csa.event_enable = 0;
1009 csa.callback = daasync;
1010 csa.callback_arg = periph;
1011 xpt_action((union ccb *)&csa);
1013 softc->flags |= DA_FLAG_PACK_INVALID;
1016 * Although the oninvalidate() routines are always called at
1017 * splsoftcam, we need to be at splbio() here to keep the buffer
1018 * queue from being modified while we traverse it.
1023 * Return all queued I/O with ENXIO.
1024 * XXX Handle any transactions queued to the card
1025 * with XPT_ABORT_CCB.
1027 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
1028 bufq_remove(&softc->buf_queue, q_bp);
1029 q_bp->b_resid = q_bp->b_bcount;
1030 q_bp->b_error = ENXIO;
1031 q_bp->b_flags |= B_ERROR;
1036 SLIST_REMOVE(&softc_list, softc, da_softc, links);
1038 xpt_print_path(periph->path);
1039 printf("lost device\n");
1043 dacleanup(struct cam_periph *periph)
1045 struct da_softc *softc;
1047 softc = (struct da_softc *)periph->softc;
1049 devstat_remove_entry(&softc->device_stats);
1050 cam_extend_release(daperiphs, periph->unit_number);
1051 xpt_print_path(periph->path);
1052 printf("removing device entry\n");
1054 * If we can't free the sysctl tree, oh well...
1056 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1057 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1058 xpt_print_path(periph->path);
1059 printf("can't remove sysctl context\n");
1061 if (softc->disk.d_rawdev) {
1062 disk_destroy(&softc->disk);
1064 free(softc, M_DEVBUF);
1068 daasync(void *callback_arg, u_int32_t code,
1069 struct cam_path *path, void *arg)
1071 struct cam_periph *periph;
1073 periph = (struct cam_periph *)callback_arg;
1075 case AC_FOUND_DEVICE:
1077 struct ccb_getdev *cgd;
1080 cgd = (struct ccb_getdev *)arg;
1082 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1083 && SID_TYPE(&cgd->inq_data) != T_RBC
1084 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1088 * Allocate a peripheral instance for
1089 * this device and start the probe
1092 status = cam_periph_alloc(daregister, daoninvalidate,
1094 "da", CAM_PERIPH_BIO,
1095 cgd->ccb_h.path, daasync,
1096 AC_FOUND_DEVICE, cgd);
1098 if (status != CAM_REQ_CMP
1099 && status != CAM_REQ_INPROG)
1100 printf("daasync: Unable to attach to new device "
1101 "due to status 0x%x\n", status);
1107 struct da_softc *softc;
1108 struct ccb_hdr *ccbh;
1111 softc = (struct da_softc *)periph->softc;
1114 * Don't fail on the expected unit attention
1117 softc->flags |= DA_FLAG_RETRY_UA;
1118 for (ccbh = LIST_FIRST(&softc->pending_ccbs);
1119 ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le))
1120 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1125 cam_periph_async(periph, code, path, arg);
1131 dasysctlinit(void *context, int pending)
1133 struct cam_periph *periph;
1134 struct da_softc *softc;
1135 char tmpstr[80], tmpstr2[80];
1137 periph = (struct cam_periph *)context;
1138 softc = (struct da_softc *)periph->softc;
1140 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1141 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1143 sysctl_ctx_init(&softc->sysctl_ctx);
1144 softc->flags |= DA_FLAG_SCTX_INIT;
1145 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1146 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1147 CTLFLAG_RD, 0, tmpstr);
1148 if (softc->sysctl_tree == NULL) {
1149 printf("dasysctlinit: unable to allocate sysctl tree\n");
1154 * Now register the sysctl handler, so the user can the value on
1157 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1158 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1159 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1160 "Minimum CDB size");
1164 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1168 value = *(int *)arg1;
1170 error = sysctl_handle_int(oidp, &value, 0, req);
1173 || (req->newptr == NULL))
1177 * Acceptable values here are 6, 10 or 12. It's possible we may
1178 * support a 16 byte minimum command size in the future, since
1179 * there are now READ(16) and WRITE(16) commands defined in the
1184 else if ((value > 6)
1187 else if (value > 10)
1190 *(int *)arg1 = value;
1196 daregister(struct cam_periph *periph, void *arg)
1199 struct da_softc *softc;
1200 struct ccb_setasync csa;
1201 struct ccb_pathinq cpi;
1202 struct ccb_getdev *cgd;
1206 cgd = (struct ccb_getdev *)arg;
1207 if (periph == NULL) {
1208 printf("daregister: periph was NULL!!\n");
1209 return(CAM_REQ_CMP_ERR);
1213 printf("daregister: no getdev CCB, can't register device\n");
1214 return(CAM_REQ_CMP_ERR);
1217 softc = malloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
1218 LIST_INIT(&softc->pending_ccbs);
1219 softc->state = DA_STATE_PROBE;
1220 bufq_init(&softc->buf_queue);
1221 if (SID_IS_REMOVABLE(&cgd->inq_data))
1222 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1223 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1224 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1226 periph->softc = softc;
1228 cam_extend_set(daperiphs, periph->unit_number, periph);
1231 * See if this device has any quirks.
1233 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1234 (caddr_t)da_quirk_table,
1235 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1236 sizeof(*da_quirk_table), scsi_inquiry_match);
1239 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1241 softc->quirks = DA_Q_NONE;
1243 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1245 /* Check if the SIM does not want 6 byte commands */
1246 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
1247 cpi.ccb_h.func_code = XPT_PATH_INQ;
1248 xpt_action((union ccb *)&cpi);
1249 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1250 softc->quirks |= DA_Q_NO_6_BYTE;
1253 * RBC devices don't have to support READ(6), only READ(10).
1255 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1256 softc->minimum_cmd_size = 10;
1258 softc->minimum_cmd_size = 6;
1261 * Load the user's default, if any.
1263 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1264 periph->unit_number);
1265 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1268 * 6, 10 and 12 are the currently permissible values.
1270 if (softc->minimum_cmd_size < 6)
1271 softc->minimum_cmd_size = 6;
1272 else if ((softc->minimum_cmd_size > 6)
1273 && (softc->minimum_cmd_size <= 10))
1274 softc->minimum_cmd_size = 10;
1275 else if (softc->minimum_cmd_size > 12)
1276 softc->minimum_cmd_size = 12;
1279 * Block our timeout handler while we
1280 * add this softc to the dev list.
1283 SLIST_INSERT_HEAD(&softc_list, softc, links);
1287 * The DA driver supports a blocksize, but
1288 * we don't know the blocksize until we do
1289 * a read capacity. So, set a flag to
1290 * indicate that the blocksize is
1291 * unavailable right now. We'll clear the
1292 * flag as soon as we've done a read capacity.
1294 devstat_add_entry(&softc->device_stats, "da",
1295 periph->unit_number, 0,
1296 DEVSTAT_BS_UNAVAILABLE,
1297 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
1298 DEVSTAT_PRIORITY_DISK);
1301 * Register this media as a disk
1303 disk_create(periph->unit_number, &softc->disk, 0, &da_cdevsw);
1306 * Add async callbacks for bus reset and
1307 * bus device reset calls. I don't bother
1308 * checking if this fails as, in most cases,
1309 * the system will function just fine without
1310 * them and the only alternative would be to
1311 * not attach the device on failure.
1313 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
1314 csa.ccb_h.func_code = XPT_SASYNC_CB;
1315 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
1316 csa.callback = daasync;
1317 csa.callback_arg = periph;
1318 xpt_action((union ccb *)&csa);
1320 * Lock this peripheral until we are setup.
1321 * This first call can't block
1323 (void)cam_periph_lock(periph, 0);
1324 xpt_schedule(periph, /*priority*/5);
1326 return(CAM_REQ_CMP);
1330 dastart(struct cam_periph *periph, union ccb *start_ccb)
1332 struct da_softc *softc;
1334 softc = (struct da_softc *)periph->softc;
1337 switch (softc->state) {
1338 case DA_STATE_NORMAL:
1340 /* Pull a buffer from the queue and get going on it */
1345 * See if there is a buf with work for us to do..
1348 bp = bufq_first(&softc->buf_queue);
1349 if (periph->immediate_priority <= periph->pinfo.priority) {
1350 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1351 ("queuing for immediate ccb\n"));
1352 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1353 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1355 periph->immediate_priority = CAM_PRIORITY_NONE;
1357 wakeup(&periph->ccb_list);
1358 } else if (bp == NULL) {
1360 xpt_release_ccb(start_ccb);
1365 bufq_remove(&softc->buf_queue, bp);
1367 devstat_start_transaction(&softc->device_stats);
1369 if ((bp->b_flags & B_ORDERED) != 0
1370 || (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1371 softc->flags &= ~DA_FLAG_NEED_OTAG;
1372 softc->ordered_tag_count++;
1373 tag_code = MSG_ORDERED_Q_TAG;
1375 tag_code = MSG_SIMPLE_Q_TAG;
1377 scsi_read_write(&start_ccb->csio,
1378 /*retries*/da_retry_count,
1381 bp->b_flags & B_READ,
1383 softc->minimum_cmd_size,
1385 bp->b_bcount / softc->params.secsize,
1388 /*sense_len*/SSD_FULL_SIZE,
1389 da_default_timeout * 1000);
1390 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1393 * Block out any asyncronous callbacks
1394 * while we touch the pending ccb list.
1397 LIST_INSERT_HEAD(&softc->pending_ccbs,
1398 &start_ccb->ccb_h, periph_links.le);
1401 /* We expect a unit attention from this device */
1402 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1403 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1404 softc->flags &= ~DA_FLAG_RETRY_UA;
1407 start_ccb->ccb_h.ccb_bp = bp;
1408 bp = bufq_first(&softc->buf_queue);
1411 xpt_action(start_ccb);
1415 /* Have more work to do, so ensure we stay scheduled */
1416 xpt_schedule(periph, /* XXX priority */1);
1420 case DA_STATE_PROBE:
1422 struct ccb_scsiio *csio;
1423 struct scsi_read_capacity_data *rcap;
1425 rcap = malloc(sizeof(*rcap), M_TEMP, M_INTWAIT | M_ZERO);
1426 csio = &start_ccb->csio;
1427 scsi_read_capacity(csio,
1434 start_ccb->ccb_h.ccb_bp = NULL;
1435 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1436 xpt_action(start_ccb);
1443 cmd6workaround(union ccb *ccb)
1445 struct scsi_rw_6 cmd6;
1446 struct scsi_rw_10 *cmd10;
1447 struct da_softc *softc;
1451 cdb = ccb->csio.cdb_io.cdb_bytes;
1453 /* Translation only possible if CDB is an array and cmd is R/W6 */
1454 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1455 (*cdb != READ_6 && *cdb != WRITE_6))
1458 xpt_print_path(ccb->ccb_h.path);
1459 printf("READ(6)/WRITE(6) not supported, "
1460 "increasing minimum_cmd_size to 10.\n");
1461 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1462 softc->minimum_cmd_size = 10;
1464 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1465 cmd10 = (struct scsi_rw_10 *)cdb;
1466 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1468 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1469 cmd10->reserved = 0;
1470 scsi_ulto2b(cmd6.length, cmd10->length);
1471 cmd10->control = cmd6.control;
1472 ccb->csio.cdb_len = sizeof(*cmd10);
1474 /* Requeue request, unfreezing queue if necessary */
1475 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1476 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1479 cam_release_devq(ccb->ccb_h.path,
1483 /*getcount_only*/0);
1489 dadone(struct cam_periph *periph, union ccb *done_ccb)
1491 struct da_softc *softc;
1492 struct ccb_scsiio *csio;
1494 softc = (struct da_softc *)periph->softc;
1495 csio = &done_ccb->csio;
1496 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1497 case DA_CCB_BUFFER_IO:
1502 bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
1503 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1508 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1513 /* Retry selection timeouts */
1514 sf |= SF_RETRY_SELTO;
1516 if ((error = daerror(done_ccb, 0, sf)) == ERESTART) {
1518 * A retry was scheuled, so
1528 if (error == ENXIO) {
1530 * Catastrophic error. Mark our pack as
1533 /* XXX See if this is really a media
1536 xpt_print_path(periph->path);
1537 printf("Invalidating pack\n");
1538 softc->flags |= DA_FLAG_PACK_INVALID;
1542 * return all queued I/O with EIO, so that
1543 * the client can retry these I/Os in the
1544 * proper order should it attempt to recover.
1546 while ((q_bp = bufq_first(&softc->buf_queue))
1548 bufq_remove(&softc->buf_queue, q_bp);
1549 q_bp->b_resid = q_bp->b_bcount;
1550 q_bp->b_error = EIO;
1551 q_bp->b_flags |= B_ERROR;
1555 bp->b_error = error;
1556 bp->b_resid = bp->b_bcount;
1557 bp->b_flags |= B_ERROR;
1559 bp->b_resid = csio->resid;
1561 if (bp->b_resid != 0)
1562 bp->b_flags |= B_ERROR;
1564 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1565 cam_release_devq(done_ccb->ccb_h.path,
1569 /*getcount_only*/0);
1571 bp->b_resid = csio->resid;
1572 if (csio->resid > 0)
1573 bp->b_flags |= B_ERROR;
1577 * Block out any asyncronous callbacks
1578 * while we touch the pending ccb list.
1581 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1584 if (softc->device_stats.busy_count == 0)
1585 softc->flags |= DA_FLAG_WENT_IDLE;
1587 devstat_end_transaction_buf(&softc->device_stats, bp);
1593 struct scsi_read_capacity_data *rdcap;
1594 char announce_buf[80];
1596 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1598 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1599 struct disk_params *dp;
1601 dasetgeom(periph, rdcap);
1602 dp = &softc->params;
1603 snprintf(announce_buf, sizeof(announce_buf),
1604 "%luMB (%u %u byte sectors: %dH %dS/T %dC)",
1605 (unsigned long) (((u_int64_t)dp->secsize *
1606 dp->sectors) / (1024*1024)), dp->sectors,
1607 dp->secsize, dp->heads, dp->secs_per_track,
1612 announce_buf[0] = '\0';
1615 * Retry any UNIT ATTENTION type errors. They
1616 * are expected at boot.
1618 error = daerror(done_ccb, 0, SF_RETRY_UA |
1619 SF_RETRY_SELTO | SF_NO_PRINT);
1620 if (error == ERESTART) {
1622 * A retry was scheuled, so
1626 } else if (error != 0) {
1627 struct scsi_sense_data *sense;
1629 int sense_key, error_code;
1632 struct ccb_getdev cgd;
1634 /* Don't wedge this device's queue */
1635 cam_release_devq(done_ccb->ccb_h.path,
1639 /*getcount_only*/0);
1641 status = done_ccb->ccb_h.status;
1643 xpt_setup_ccb(&cgd.ccb_h,
1644 done_ccb->ccb_h.path,
1646 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1647 xpt_action((union ccb *)&cgd);
1649 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1650 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1651 || ((status & CAM_AUTOSNS_VALID) == 0))
1657 sense = &csio->sense_data;
1658 scsi_extract_sense(sense, &error_code,
1663 * Attach to anything that claims to be a
1664 * direct access or optical disk device,
1665 * as long as it doesn't return a "Logical
1666 * unit not supported" (0x25) error.
1668 if ((have_sense) && (asc != 0x25)
1669 && (error_code == SSD_CURRENT_ERROR))
1670 snprintf(announce_buf,
1671 sizeof(announce_buf),
1672 "Attempt to query device "
1673 "size failed: %s, %s",
1674 scsi_sense_key_text[sense_key],
1675 scsi_sense_desc(asc,ascq,
1682 xpt_print_path(periph->path);
1683 printf("got CAM status %#x\n",
1684 done_ccb->ccb_h.status);
1687 xpt_print_path(periph->path);
1688 printf("fatal error, failed"
1689 " to attach to device\n");
1692 * Free up resources.
1694 cam_periph_invalidate(periph);
1698 free(rdcap, M_TEMP);
1699 if (announce_buf[0] != '\0') {
1700 xpt_announce_periph(periph, announce_buf);
1702 * Create our sysctl variables, now that we know
1703 * we have successfully attached.
1705 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1707 softc->state = DA_STATE_NORMAL;
1709 * Since our peripheral may be invalidated by an error
1710 * above or an external event, we must release our CCB
1711 * before releasing the probe lock on the peripheral.
1712 * The peripheral will only go away once the last lock
1713 * is removed, and we need it around for the CCB release
1716 xpt_release_ccb(done_ccb);
1717 cam_periph_unlock(periph);
1720 case DA_CCB_WAITING:
1722 /* Caller will release the CCB */
1723 wakeup(&done_ccb->ccb_h.cbfcnp);
1727 /* No-op. We're polling */
1732 xpt_release_ccb(done_ccb);
1736 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1738 struct da_softc *softc;
1739 struct cam_periph *periph;
1742 periph = xpt_path_periph(ccb->ccb_h.path);
1743 softc = (struct da_softc *)periph->softc;
1746 * Automatically detect devices that do not support
1747 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
1750 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
1751 error = cmd6workaround(ccb);
1752 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
1753 CAM_SCSI_STATUS_ERROR)
1754 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
1755 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1756 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
1757 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
1758 int sense_key, error_code, asc, ascq;
1760 scsi_extract_sense(&ccb->csio.sense_data,
1761 &error_code, &sense_key, &asc, &ascq);
1762 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
1763 error = cmd6workaround(ccb);
1765 if (error == ERESTART)
1770 * Until we have a better way of doing pack validation,
1771 * don't treat UAs as errors.
1773 sense_flags |= SF_RETRY_UA;
1774 return(cam_periph_error(ccb, cam_flags, sense_flags,
1775 &softc->saved_ccb));
1779 daprevent(struct cam_periph *periph, int action)
1781 struct da_softc *softc;
1785 softc = (struct da_softc *)periph->softc;
1787 if (((action == PR_ALLOW)
1788 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
1789 || ((action == PR_PREVENT)
1790 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
1794 ccb = cam_periph_getccb(periph, /*priority*/1);
1796 scsi_prevent(&ccb->csio,
1804 error = cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
1805 /*sense_flags*/0, &softc->device_stats);
1808 if (action == PR_ALLOW)
1809 softc->flags &= ~DA_FLAG_PACK_LOCKED;
1811 softc->flags |= DA_FLAG_PACK_LOCKED;
1814 xpt_release_ccb(ccb);
1818 dasetgeom(struct cam_periph *periph, struct scsi_read_capacity_data * rdcap)
1820 struct ccb_calc_geometry ccg;
1821 struct da_softc *softc;
1822 struct disk_params *dp;
1824 softc = (struct da_softc *)periph->softc;
1826 dp = &softc->params;
1827 dp->secsize = scsi_4btoul(rdcap->length);
1828 dp->sectors = scsi_4btoul(rdcap->addr) + 1;
1830 * Have the controller provide us with a geometry
1831 * for this disk. The only time the geometry
1832 * matters is when we boot and the controller
1833 * is the only one knowledgeable enough to come
1834 * up with something that will make this a bootable
1837 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
1838 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
1839 ccg.block_size = dp->secsize;
1840 ccg.volume_size = dp->sectors;
1842 ccg.secs_per_track = 0;
1844 xpt_action((union ccb*)&ccg);
1845 dp->heads = ccg.heads;
1846 dp->secs_per_track = ccg.secs_per_track;
1847 dp->cylinders = ccg.cylinders;
1851 dasendorderedtag(void *arg)
1853 struct da_softc *softc;
1856 for (softc = SLIST_FIRST(&softc_list);
1858 softc = SLIST_NEXT(softc, links)) {
1860 if ((softc->ordered_tag_count == 0)
1861 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
1862 softc->flags |= DA_FLAG_NEED_OTAG;
1864 if (softc->device_stats.busy_count > 0)
1865 softc->flags &= ~DA_FLAG_WENT_IDLE;
1867 softc->ordered_tag_count = 0;
1870 /* Queue us up again */
1871 timeout(dasendorderedtag, NULL,
1872 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL);
1876 * Step through all DA peripheral drivers, and if the device is still open,
1877 * sync the disk cache to physical media.
1880 dashutdown(void * arg, int howto)
1882 struct cam_periph *periph;
1883 struct da_softc *softc;
1885 for (periph = TAILQ_FIRST(&dadriver.units); periph != NULL;
1886 periph = TAILQ_NEXT(periph, unit_links)) {
1888 softc = (struct da_softc *)periph->softc;
1891 * We only sync the cache if the drive is still open, and
1892 * if the drive is capable of it..
1894 if (((softc->flags & DA_FLAG_OPEN) == 0)
1895 || (softc->quirks & DA_Q_NO_SYNC_CACHE))
1898 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
1900 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
1901 scsi_synchronize_cache(&ccb.csio,
1905 /*begin_lba*/0, /* whole disk */
1910 xpt_polled_action(&ccb);
1912 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1913 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
1914 CAM_SCSI_STATUS_ERROR)
1915 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
1916 int error_code, sense_key, asc, ascq;
1918 scsi_extract_sense(&ccb.csio.sense_data,
1919 &error_code, &sense_key,
1922 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1923 scsi_sense_print(&ccb.csio);
1925 xpt_print_path(periph->path);
1926 printf("Synchronize cache failed, status "
1927 "== 0x%x, scsi status == 0x%x\n",
1928 ccb.ccb_h.status, ccb.csio.scsi_status);
1932 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1933 cam_release_devq(ccb.ccb_h.path,
1937 /*getcount_only*/0);
1942 #else /* !_KERNEL */
1945 * XXX This is only left out of the kernel build to silence warnings. If,
1946 * for some reason this function is used in the kernel, the ifdefs should
1947 * be moved so it is included both in the kernel and userland.
1950 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
1951 void (*cbfcnp)(struct cam_periph *, union ccb *),
1952 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
1953 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
1956 struct scsi_format_unit *scsi_cmd;
1958 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
1959 scsi_cmd->opcode = FORMAT_UNIT;
1960 scsi_cmd->byte2 = byte2;
1961 scsi_ulto2b(ileave, scsi_cmd->interleave);
1966 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
1975 #endif /* _KERNEL */