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.20 2004/09/18 21:05:43 joerg 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
346 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
349 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "NexIA+*", "*"},
350 /*quirks*/ DA_Q_NO_SYNC_CACHE
356 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJIFILMUSB-DRIVEUNIT",
357 "USB-DRIVEUNIT", "*"},
358 /*quirks*/ DA_Q_NO_SYNC_CACHE
362 * Minolta Dimage E203
364 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MINOLTA", "DiMAGE E203", "*"},
365 /*quirks*/ DA_Q_NO_SYNC_CACHE
372 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Apacer", "HandyDrive", "*"},
373 /*quirks*/ DA_Q_NO_SYNC_CACHE
377 * Daisy Technology PhotoClip on Zoran chip
380 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ZORAN", "COACH", "*"},
381 /*quirks*/ DA_Q_NO_SYNC_CACHE
385 * Sony USB Key-Storage
388 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Storage Media", "*"},
389 /*quirks*/ DA_Q_NO_SYNC_CACHE
391 #endif /* DA_OLD_QUIRKS */
394 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
397 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
398 /*quirks*/ DA_Q_NO_SYNC_CACHE
402 * Jungsoft NEXDISK USB flash key
405 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
406 /*quirks*/ DA_Q_NO_SYNC_CACHE
410 * Creative Nomad MUVO mp3 player (USB)
413 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
414 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
418 static d_open_t daopen;
419 static d_close_t daclose;
420 static d_strategy_t dastrategy;
421 static d_ioctl_t daioctl;
422 static d_dump_t dadump;
423 static periph_init_t dainit;
424 static void daasync(void *callback_arg, u_int32_t code,
425 struct cam_path *path, void *arg);
426 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
427 static periph_ctor_t daregister;
428 static periph_dtor_t dacleanup;
429 static periph_start_t dastart;
430 static periph_oninv_t daoninvalidate;
431 static void dadone(struct cam_periph *periph,
432 union ccb *done_ccb);
433 static int daerror(union ccb *ccb, u_int32_t cam_flags,
434 u_int32_t sense_flags);
435 static void daprevent(struct cam_periph *periph, int action);
436 static void dasetgeom(struct cam_periph *periph,
437 struct scsi_read_capacity_data * rdcap);
438 static timeout_t dasendorderedtag;
439 static void dashutdown(void *arg, int howto);
441 #ifndef DA_DEFAULT_TIMEOUT
442 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
445 #ifndef DA_DEFAULT_RETRY
446 #define DA_DEFAULT_RETRY 4
449 static int da_retry_count = DA_DEFAULT_RETRY;
450 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
451 static struct callout dasendorderedtag_ch;
453 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
454 "CAM Direct Access Disk driver");
455 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
456 &da_retry_count, 0, "Normal I/O retry count");
457 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
458 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
459 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
460 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
463 * DA_ORDEREDTAG_INTERVAL determines how often, relative
464 * to the default timeout, we check to see whether an ordered
465 * tagged transaction is appropriate to prevent simple tag
466 * starvation. Since we'd like to ensure that there is at least
467 * 1/2 of the timeout length left for a starved transaction to
468 * complete after we've sent an ordered tag, we must poll at least
469 * four times in every timeout period. This takes care of the worst
470 * case where a starved transaction starts during an interval that
471 * meets the requirement "don't send an ordered tag" test so it takes
472 * us two intervals to determine that a tag must be sent.
474 #ifndef DA_ORDEREDTAG_INTERVAL
475 #define DA_ORDEREDTAG_INTERVAL 4
478 static struct periph_driver dadriver =
481 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
484 DATA_SET(periphdriver_set, dadriver);
486 /* For 2.2-stable support */
491 static struct cdevsw da_cdevsw = {
493 /* maj */ DA_CDEV_MAJOR,
501 /* write */ physwrite,
505 /* strategy */ dastrategy,
510 static SLIST_HEAD(,da_softc) softc_list;
511 static struct extend_array *daperiphs;
514 daopen(dev_t dev, int flags, int fmt, struct thread *td)
516 struct cam_periph *periph;
517 struct da_softc *softc;
518 struct disklabel *label;
526 periph = cam_extend_get(daperiphs, unit);
530 softc = (struct da_softc *)periph->softc;
532 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
533 ("daopen: dev=%s (unit %d , partition %d)\n", devtoname(dev),
536 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
537 return (error); /* error code from tsleep */
540 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
542 softc->flags |= DA_FLAG_OPEN;
545 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
546 /* Invalidate our pack information. */
547 disk_invalidate(&softc->disk);
548 softc->flags &= ~DA_FLAG_PACK_INVALID;
552 /* Do a read capacity */
554 struct scsi_read_capacity_data *rcap;
557 rcap = malloc(sizeof(*rcap), M_TEMP, M_INTWAIT | M_ZERO);
559 ccb = cam_periph_getccb(periph, /*priority*/1);
560 scsi_read_capacity(&ccb->csio,
567 ccb->ccb_h.ccb_bp = NULL;
569 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
570 /*sense_flags*/SF_RETRY_UA |
572 &softc->device_stats);
574 xpt_release_ccb(ccb);
577 dasetgeom(periph, rcap);
584 struct ccb_getdev cgd;
586 /* Build label for whole disk. */
587 label = &softc->disk.d_label;
588 bzero(label, sizeof(*label));
589 label->d_type = DTYPE_SCSI;
592 * Grab the inquiry data to get the vendor and product names.
593 * Put them in the typename and packname for the label.
595 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
596 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
597 xpt_action((union ccb *)&cgd);
599 strncpy(label->d_typename, cgd.inq_data.vendor,
600 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
601 strncpy(label->d_packname, cgd.inq_data.product,
602 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
604 label->d_secsize = softc->params.secsize;
605 label->d_nsectors = softc->params.secs_per_track;
606 label->d_ntracks = softc->params.heads;
607 label->d_ncylinders = softc->params.cylinders;
608 label->d_secpercyl = softc->params.heads
609 * softc->params.secs_per_track;
610 label->d_secperunit = softc->params.sectors;
612 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
613 (softc->quirks & DA_Q_NO_PREVENT) == 0)
614 daprevent(periph, PR_PREVENT);
617 * Check to see whether or not the blocksize is set yet.
618 * If it isn't, set it and then clear the blocksize
619 * unavailable flag for the device statistics.
621 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0){
622 softc->device_stats.block_size = softc->params.secsize;
623 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
628 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
629 (softc->quirks & DA_Q_NO_PREVENT) == 0)
630 daprevent(periph, PR_ALLOW);
631 softc->flags &= ~DA_FLAG_OPEN;
632 cam_periph_release(periph);
634 cam_periph_unlock(periph);
639 daclose(dev_t dev, int flag, int fmt, struct thread *td)
641 struct cam_periph *periph;
642 struct da_softc *softc;
647 periph = cam_extend_get(daperiphs, unit);
651 softc = (struct da_softc *)periph->softc;
653 if ((error = cam_periph_lock(periph, 0)) != 0) {
654 return (error); /* error code from tsleep */
657 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
660 ccb = cam_periph_getccb(periph, /*priority*/1);
662 scsi_synchronize_cache(&ccb->csio,
666 /*begin_lba*/0,/* Cover the whole disk */
671 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
672 /*sense_flags*/SF_RETRY_UA,
673 &softc->device_stats);
675 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
676 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
677 CAM_SCSI_STATUS_ERROR) {
679 int sense_key, error_code;
681 scsi_extract_sense(&ccb->csio.sense_data,
685 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
686 scsi_sense_print(&ccb->csio);
688 xpt_print_path(periph->path);
689 printf("Synchronize cache failed, status "
690 "== 0x%x, scsi status == 0x%x\n",
691 ccb->csio.ccb_h.status,
692 ccb->csio.scsi_status);
696 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
697 cam_release_devq(ccb->ccb_h.path,
703 xpt_release_ccb(ccb);
707 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
708 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
709 daprevent(periph, PR_ALLOW);
711 * If we've got removeable media, mark the blocksize as
712 * unavailable, since it could change when new media is
715 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
718 softc->flags &= ~DA_FLAG_OPEN;
719 cam_periph_unlock(periph);
720 cam_periph_release(periph);
725 * Actually translate the requested transfer into one the physical driver
726 * can understand. The transfer is described by a buf and will include
727 * only one physical transfer.
730 dastrategy(struct buf *bp)
732 struct cam_periph *periph;
733 struct da_softc *softc;
738 unit = dkunit(bp->b_dev);
739 part = dkpart(bp->b_dev);
740 periph = cam_extend_get(daperiphs, unit);
741 if (periph == NULL) {
745 softc = (struct da_softc *)periph->softc;
748 * check it's not too big a transfer for our adapter
750 scsi_minphys(bp,&sd_switch);
754 * Mask interrupts so that the pack cannot be invalidated until
755 * after we are in the queue. Otherwise, we might not properly
756 * clean up one of the buffers.
761 * If the device has been made invalid, error out
763 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
770 * Place it in the queue of disk activities for this disk
772 bufqdisksort(&softc->buf_queue, bp);
777 * Schedule ourselves for performing the work.
779 xpt_schedule(periph, /* XXX priority */1);
783 bp->b_flags |= B_ERROR;
786 * Correctly set the buf to indicate a completed xfer
788 bp->b_resid = bp->b_bcount;
793 /* For 2.2-stable support */
799 daioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
801 struct cam_periph *periph;
802 struct da_softc *softc;
807 periph = cam_extend_get(daperiphs, unit);
811 softc = (struct da_softc *)periph->softc;
813 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("daioctl\n"));
815 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
816 return (error); /* error code from tsleep */
819 error = cam_periph_ioctl(periph, cmd, addr, daerror);
821 cam_periph_unlock(periph);
827 dadump(dev_t dev, u_int num, u_int blknum, u_int secsize)
829 struct cam_periph *periph;
830 struct da_softc *softc;
834 struct ccb_scsiio csio;
835 int dumppages = MAXDUMPPGS;
838 /* toss any characters present prior to dump */
839 while (cncheckc() != -1)
843 periph = cam_extend_get(daperiphs, unit);
844 if (periph == NULL) {
847 softc = (struct da_softc *)periph->softc;
849 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0)
852 addr = 0; /* starting address */
853 blkcnt = howmany(PAGE_SIZE, secsize);
858 if ((num / blkcnt) < dumppages)
859 dumppages = num / blkcnt;
861 for (i = 0; i < dumppages; ++i) {
862 vm_paddr_t a = addr + (i * PAGE_SIZE);
863 if (is_physical_memory(a))
864 va = pmap_kenter_temporary(trunc_page(a), i);
866 va = pmap_kenter_temporary(trunc_page(0), i);
869 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
870 csio.ccb_h.ccb_state = DA_CCB_DUMP;
871 scsi_read_write(&csio,
877 /*minimum_cmd_size*/ softc->minimum_cmd_size,
880 /*data_ptr*/(u_int8_t *) va,
881 /*dxfer_len*/blkcnt * secsize * dumppages,
882 /*sense_len*/SSD_FULL_SIZE,
883 DA_DEFAULT_TIMEOUT * 1000);
884 xpt_polled_action((union ccb *)&csio);
886 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
887 printf("Aborting dump due to I/O error.\n");
888 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
889 CAM_SCSI_STATUS_ERROR)
890 scsi_sense_print(&csio);
892 printf("status == 0x%x, scsi status == 0x%x\n",
893 csio.ccb_h.status, csio.scsi_status);
897 if (dumpstatus(addr, (off_t)num * softc->params.secsize) < 0)
900 /* update block count */
901 num -= blkcnt * dumppages;
902 blknum += blkcnt * dumppages;
903 addr += PAGE_SIZE * dumppages;
907 * Sync the disk cache contents to the physical media.
909 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
911 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
912 csio.ccb_h.ccb_state = DA_CCB_DUMP;
913 scsi_synchronize_cache(&csio,
917 /*begin_lba*/0,/* Cover the whole disk */
921 xpt_polled_action((union ccb *)&csio);
923 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
924 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
925 CAM_SCSI_STATUS_ERROR) {
927 int sense_key, error_code;
929 scsi_extract_sense(&csio.sense_data,
933 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
934 scsi_sense_print(&csio);
936 xpt_print_path(periph->path);
937 printf("Synchronize cache failed, status "
938 "== 0x%x, scsi status == 0x%x\n",
939 csio.ccb_h.status, csio.scsi_status);
950 struct cam_path *path;
953 * Create our extend array for storing the devices we attach to.
955 daperiphs = cam_extend_new();
956 SLIST_INIT(&softc_list);
957 if (daperiphs == NULL) {
958 printf("da: Failed to alloc extend array!\n");
962 callout_init(&dasendorderedtag_ch);
965 * Install a global async callback. This callback will
966 * receive async callbacks like "new device found".
968 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
969 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
971 if (status == CAM_REQ_CMP) {
972 struct ccb_setasync csa;
974 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
975 csa.ccb_h.func_code = XPT_SASYNC_CB;
976 csa.event_enable = AC_FOUND_DEVICE;
977 csa.callback = daasync;
978 csa.callback_arg = NULL;
979 xpt_action((union ccb *)&csa);
980 status = csa.ccb_h.status;
984 if (status != CAM_REQ_CMP) {
985 printf("da: Failed to attach master async callback "
986 "due to status 0x%x!\n", status);
990 * Schedule a periodic event to occasionally send an
991 * ordered tag to a device.
993 callout_reset(&dasendorderedtag_ch,
994 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
995 dasendorderedtag, NULL);
997 /* Register our shutdown event handler */
998 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
999 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1000 printf("dainit: shutdown event registration failed!\n");
1005 daoninvalidate(struct cam_periph *periph)
1008 struct da_softc *softc;
1010 struct ccb_setasync csa;
1012 softc = (struct da_softc *)periph->softc;
1015 * De-register any async callbacks.
1017 xpt_setup_ccb(&csa.ccb_h, periph->path,
1019 csa.ccb_h.func_code = XPT_SASYNC_CB;
1020 csa.event_enable = 0;
1021 csa.callback = daasync;
1022 csa.callback_arg = periph;
1023 xpt_action((union ccb *)&csa);
1025 softc->flags |= DA_FLAG_PACK_INVALID;
1028 * Although the oninvalidate() routines are always called at
1029 * splsoftcam, we need to be at splbio() here to keep the buffer
1030 * queue from being modified while we traverse it.
1035 * Return all queued I/O with ENXIO.
1036 * XXX Handle any transactions queued to the card
1037 * with XPT_ABORT_CCB.
1039 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
1040 bufq_remove(&softc->buf_queue, q_bp);
1041 q_bp->b_resid = q_bp->b_bcount;
1042 q_bp->b_error = ENXIO;
1043 q_bp->b_flags |= B_ERROR;
1048 SLIST_REMOVE(&softc_list, softc, da_softc, links);
1050 xpt_print_path(periph->path);
1051 printf("lost device\n");
1055 dacleanup(struct cam_periph *periph)
1057 struct da_softc *softc;
1059 softc = (struct da_softc *)periph->softc;
1061 devstat_remove_entry(&softc->device_stats);
1062 cam_extend_release(daperiphs, periph->unit_number);
1063 xpt_print_path(periph->path);
1064 printf("removing device entry\n");
1066 * If we can't free the sysctl tree, oh well...
1068 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1069 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1070 xpt_print_path(periph->path);
1071 printf("can't remove sysctl context\n");
1073 if (softc->disk.d_rawdev) {
1074 disk_destroy(&softc->disk);
1076 free(softc, M_DEVBUF);
1080 daasync(void *callback_arg, u_int32_t code,
1081 struct cam_path *path, void *arg)
1083 struct cam_periph *periph;
1085 periph = (struct cam_periph *)callback_arg;
1087 case AC_FOUND_DEVICE:
1089 struct ccb_getdev *cgd;
1092 cgd = (struct ccb_getdev *)arg;
1094 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1095 && SID_TYPE(&cgd->inq_data) != T_RBC
1096 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1100 * Allocate a peripheral instance for
1101 * this device and start the probe
1104 status = cam_periph_alloc(daregister, daoninvalidate,
1106 "da", CAM_PERIPH_BIO,
1107 cgd->ccb_h.path, daasync,
1108 AC_FOUND_DEVICE, cgd);
1110 if (status != CAM_REQ_CMP
1111 && status != CAM_REQ_INPROG)
1112 printf("daasync: Unable to attach to new device "
1113 "due to status 0x%x\n", status);
1119 struct da_softc *softc;
1120 struct ccb_hdr *ccbh;
1123 softc = (struct da_softc *)periph->softc;
1126 * Don't fail on the expected unit attention
1129 softc->flags |= DA_FLAG_RETRY_UA;
1130 for (ccbh = LIST_FIRST(&softc->pending_ccbs);
1131 ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le))
1132 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1137 cam_periph_async(periph, code, path, arg);
1143 dasysctlinit(void *context, int pending)
1145 struct cam_periph *periph;
1146 struct da_softc *softc;
1147 char tmpstr[80], tmpstr2[80];
1149 periph = (struct cam_periph *)context;
1150 softc = (struct da_softc *)periph->softc;
1152 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1153 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1155 sysctl_ctx_init(&softc->sysctl_ctx);
1156 softc->flags |= DA_FLAG_SCTX_INIT;
1157 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1158 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1159 CTLFLAG_RD, 0, tmpstr);
1160 if (softc->sysctl_tree == NULL) {
1161 printf("dasysctlinit: unable to allocate sysctl tree\n");
1166 * Now register the sysctl handler, so the user can the value on
1169 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1170 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1171 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1172 "Minimum CDB size");
1176 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1180 value = *(int *)arg1;
1182 error = sysctl_handle_int(oidp, &value, 0, req);
1185 || (req->newptr == NULL))
1189 * Acceptable values here are 6, 10 or 12. It's possible we may
1190 * support a 16 byte minimum command size in the future, since
1191 * there are now READ(16) and WRITE(16) commands defined in the
1196 else if ((value > 6)
1199 else if (value > 10)
1202 *(int *)arg1 = value;
1208 daregister(struct cam_periph *periph, void *arg)
1211 struct da_softc *softc;
1212 struct ccb_setasync csa;
1213 struct ccb_pathinq cpi;
1214 struct ccb_getdev *cgd;
1218 cgd = (struct ccb_getdev *)arg;
1219 if (periph == NULL) {
1220 printf("daregister: periph was NULL!!\n");
1221 return(CAM_REQ_CMP_ERR);
1225 printf("daregister: no getdev CCB, can't register device\n");
1226 return(CAM_REQ_CMP_ERR);
1229 softc = malloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
1230 LIST_INIT(&softc->pending_ccbs);
1231 softc->state = DA_STATE_PROBE;
1232 bufq_init(&softc->buf_queue);
1233 if (SID_IS_REMOVABLE(&cgd->inq_data))
1234 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1235 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1236 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1238 periph->softc = softc;
1240 cam_extend_set(daperiphs, periph->unit_number, periph);
1243 * See if this device has any quirks.
1245 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1246 (caddr_t)da_quirk_table,
1247 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1248 sizeof(*da_quirk_table), scsi_inquiry_match);
1251 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1253 softc->quirks = DA_Q_NONE;
1255 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1257 /* Check if the SIM does not want 6 byte commands */
1258 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
1259 cpi.ccb_h.func_code = XPT_PATH_INQ;
1260 xpt_action((union ccb *)&cpi);
1261 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1262 softc->quirks |= DA_Q_NO_6_BYTE;
1265 * RBC devices don't have to support READ(6), only READ(10).
1267 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1268 softc->minimum_cmd_size = 10;
1270 softc->minimum_cmd_size = 6;
1273 * Load the user's default, if any.
1275 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1276 periph->unit_number);
1277 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1280 * 6, 10 and 12 are the currently permissible values.
1282 if (softc->minimum_cmd_size < 6)
1283 softc->minimum_cmd_size = 6;
1284 else if ((softc->minimum_cmd_size > 6)
1285 && (softc->minimum_cmd_size <= 10))
1286 softc->minimum_cmd_size = 10;
1287 else if (softc->minimum_cmd_size > 12)
1288 softc->minimum_cmd_size = 12;
1291 * Block our timeout handler while we
1292 * add this softc to the dev list.
1295 SLIST_INSERT_HEAD(&softc_list, softc, links);
1299 * The DA driver supports a blocksize, but
1300 * we don't know the blocksize until we do
1301 * a read capacity. So, set a flag to
1302 * indicate that the blocksize is
1303 * unavailable right now. We'll clear the
1304 * flag as soon as we've done a read capacity.
1306 devstat_add_entry(&softc->device_stats, "da",
1307 periph->unit_number, 0,
1308 DEVSTAT_BS_UNAVAILABLE,
1309 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
1310 DEVSTAT_PRIORITY_DISK);
1313 * Register this media as a disk
1315 disk_create(periph->unit_number, &softc->disk, 0, &da_cdevsw);
1318 * Add async callbacks for bus reset and
1319 * bus device reset calls. I don't bother
1320 * checking if this fails as, in most cases,
1321 * the system will function just fine without
1322 * them and the only alternative would be to
1323 * not attach the device on failure.
1325 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
1326 csa.ccb_h.func_code = XPT_SASYNC_CB;
1327 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
1328 csa.callback = daasync;
1329 csa.callback_arg = periph;
1330 xpt_action((union ccb *)&csa);
1332 * Lock this peripheral until we are setup.
1333 * This first call can't block
1335 (void)cam_periph_lock(periph, 0);
1336 xpt_schedule(periph, /*priority*/5);
1338 return(CAM_REQ_CMP);
1342 dastart(struct cam_periph *periph, union ccb *start_ccb)
1344 struct da_softc *softc;
1346 softc = (struct da_softc *)periph->softc;
1349 switch (softc->state) {
1350 case DA_STATE_NORMAL:
1352 /* Pull a buffer from the queue and get going on it */
1357 * See if there is a buf with work for us to do..
1360 bp = bufq_first(&softc->buf_queue);
1361 if (periph->immediate_priority <= periph->pinfo.priority) {
1362 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1363 ("queuing for immediate ccb\n"));
1364 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1365 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1367 periph->immediate_priority = CAM_PRIORITY_NONE;
1369 wakeup(&periph->ccb_list);
1370 } else if (bp == NULL) {
1372 xpt_release_ccb(start_ccb);
1377 bufq_remove(&softc->buf_queue, bp);
1379 devstat_start_transaction(&softc->device_stats);
1381 if ((bp->b_flags & B_ORDERED) != 0
1382 || (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1383 softc->flags &= ~DA_FLAG_NEED_OTAG;
1384 softc->ordered_tag_count++;
1385 tag_code = MSG_ORDERED_Q_TAG;
1387 tag_code = MSG_SIMPLE_Q_TAG;
1389 scsi_read_write(&start_ccb->csio,
1390 /*retries*/da_retry_count,
1393 bp->b_flags & B_READ,
1395 softc->minimum_cmd_size,
1397 bp->b_bcount / softc->params.secsize,
1400 /*sense_len*/SSD_FULL_SIZE,
1401 da_default_timeout * 1000);
1402 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1405 * Block out any asyncronous callbacks
1406 * while we touch the pending ccb list.
1409 LIST_INSERT_HEAD(&softc->pending_ccbs,
1410 &start_ccb->ccb_h, periph_links.le);
1413 /* We expect a unit attention from this device */
1414 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1415 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1416 softc->flags &= ~DA_FLAG_RETRY_UA;
1419 start_ccb->ccb_h.ccb_bp = bp;
1420 bp = bufq_first(&softc->buf_queue);
1423 xpt_action(start_ccb);
1427 /* Have more work to do, so ensure we stay scheduled */
1428 xpt_schedule(periph, /* XXX priority */1);
1432 case DA_STATE_PROBE:
1434 struct ccb_scsiio *csio;
1435 struct scsi_read_capacity_data *rcap;
1437 rcap = malloc(sizeof(*rcap), M_TEMP, M_INTWAIT | M_ZERO);
1438 csio = &start_ccb->csio;
1439 scsi_read_capacity(csio,
1446 start_ccb->ccb_h.ccb_bp = NULL;
1447 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1448 xpt_action(start_ccb);
1455 cmd6workaround(union ccb *ccb)
1457 struct scsi_rw_6 cmd6;
1458 struct scsi_rw_10 *cmd10;
1459 struct da_softc *softc;
1463 cdb = ccb->csio.cdb_io.cdb_bytes;
1465 /* Translation only possible if CDB is an array and cmd is R/W6 */
1466 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1467 (*cdb != READ_6 && *cdb != WRITE_6))
1470 xpt_print_path(ccb->ccb_h.path);
1471 printf("READ(6)/WRITE(6) not supported, "
1472 "increasing minimum_cmd_size to 10.\n");
1473 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1474 softc->minimum_cmd_size = 10;
1476 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1477 cmd10 = (struct scsi_rw_10 *)cdb;
1478 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1480 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1481 cmd10->reserved = 0;
1482 scsi_ulto2b(cmd6.length, cmd10->length);
1483 cmd10->control = cmd6.control;
1484 ccb->csio.cdb_len = sizeof(*cmd10);
1486 /* Requeue request, unfreezing queue if necessary */
1487 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1488 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1491 cam_release_devq(ccb->ccb_h.path,
1495 /*getcount_only*/0);
1501 dadone(struct cam_periph *periph, union ccb *done_ccb)
1503 struct da_softc *softc;
1504 struct ccb_scsiio *csio;
1506 softc = (struct da_softc *)periph->softc;
1507 csio = &done_ccb->csio;
1508 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1509 case DA_CCB_BUFFER_IO:
1514 bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
1515 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1520 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1525 /* Retry selection timeouts */
1526 sf |= SF_RETRY_SELTO;
1528 if ((error = daerror(done_ccb, 0, sf)) == ERESTART) {
1530 * A retry was scheuled, so
1540 if (error == ENXIO) {
1542 * Catastrophic error. Mark our pack as
1545 /* XXX See if this is really a media
1548 xpt_print_path(periph->path);
1549 printf("Invalidating pack\n");
1550 softc->flags |= DA_FLAG_PACK_INVALID;
1554 * return all queued I/O with EIO, so that
1555 * the client can retry these I/Os in the
1556 * proper order should it attempt to recover.
1558 while ((q_bp = bufq_first(&softc->buf_queue))
1560 bufq_remove(&softc->buf_queue, q_bp);
1561 q_bp->b_resid = q_bp->b_bcount;
1562 q_bp->b_error = EIO;
1563 q_bp->b_flags |= B_ERROR;
1567 bp->b_error = error;
1568 bp->b_resid = bp->b_bcount;
1569 bp->b_flags |= B_ERROR;
1571 bp->b_resid = csio->resid;
1573 if (bp->b_resid != 0)
1574 bp->b_flags |= B_ERROR;
1576 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1577 cam_release_devq(done_ccb->ccb_h.path,
1581 /*getcount_only*/0);
1583 bp->b_resid = csio->resid;
1584 if (csio->resid > 0)
1585 bp->b_flags |= B_ERROR;
1589 * Block out any asyncronous callbacks
1590 * while we touch the pending ccb list.
1593 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1596 if (softc->device_stats.busy_count == 0)
1597 softc->flags |= DA_FLAG_WENT_IDLE;
1599 devstat_end_transaction_buf(&softc->device_stats, bp);
1605 struct scsi_read_capacity_data *rdcap;
1606 char announce_buf[80];
1608 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1610 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1611 struct disk_params *dp;
1613 dasetgeom(periph, rdcap);
1614 dp = &softc->params;
1615 snprintf(announce_buf, sizeof(announce_buf),
1616 "%luMB (%u %u byte sectors: %dH %dS/T %dC)",
1617 (unsigned long) (((u_int64_t)dp->secsize *
1618 dp->sectors) / (1024*1024)), dp->sectors,
1619 dp->secsize, dp->heads, dp->secs_per_track,
1624 announce_buf[0] = '\0';
1627 * Retry any UNIT ATTENTION type errors. They
1628 * are expected at boot.
1630 error = daerror(done_ccb, 0, SF_RETRY_UA |
1631 SF_RETRY_SELTO | SF_NO_PRINT);
1632 if (error == ERESTART) {
1634 * A retry was scheuled, so
1638 } else if (error != 0) {
1639 struct scsi_sense_data *sense;
1641 int sense_key, error_code;
1644 struct ccb_getdev cgd;
1646 /* Don't wedge this device's queue */
1647 cam_release_devq(done_ccb->ccb_h.path,
1651 /*getcount_only*/0);
1653 status = done_ccb->ccb_h.status;
1655 xpt_setup_ccb(&cgd.ccb_h,
1656 done_ccb->ccb_h.path,
1658 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1659 xpt_action((union ccb *)&cgd);
1661 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1662 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1663 || ((status & CAM_AUTOSNS_VALID) == 0))
1669 sense = &csio->sense_data;
1670 scsi_extract_sense(sense, &error_code,
1675 * Attach to anything that claims to be a
1676 * direct access or optical disk device,
1677 * as long as it doesn't return a "Logical
1678 * unit not supported" (0x25) error.
1680 if ((have_sense) && (asc != 0x25)
1681 && (error_code == SSD_CURRENT_ERROR))
1682 snprintf(announce_buf,
1683 sizeof(announce_buf),
1684 "Attempt to query device "
1685 "size failed: %s, %s",
1686 scsi_sense_key_text[sense_key],
1687 scsi_sense_desc(asc,ascq,
1694 xpt_print_path(periph->path);
1695 printf("got CAM status %#x\n",
1696 done_ccb->ccb_h.status);
1699 xpt_print_path(periph->path);
1700 printf("fatal error, failed"
1701 " to attach to device\n");
1704 * Free up resources.
1706 cam_periph_invalidate(periph);
1710 free(rdcap, M_TEMP);
1711 if (announce_buf[0] != '\0') {
1712 xpt_announce_periph(periph, announce_buf);
1714 * Create our sysctl variables, now that we know
1715 * we have successfully attached.
1717 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1719 softc->state = DA_STATE_NORMAL;
1721 * Since our peripheral may be invalidated by an error
1722 * above or an external event, we must release our CCB
1723 * before releasing the probe lock on the peripheral.
1724 * The peripheral will only go away once the last lock
1725 * is removed, and we need it around for the CCB release
1728 xpt_release_ccb(done_ccb);
1729 cam_periph_unlock(periph);
1732 case DA_CCB_WAITING:
1734 /* Caller will release the CCB */
1735 wakeup(&done_ccb->ccb_h.cbfcnp);
1739 /* No-op. We're polling */
1744 xpt_release_ccb(done_ccb);
1748 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1750 struct da_softc *softc;
1751 struct cam_periph *periph;
1754 periph = xpt_path_periph(ccb->ccb_h.path);
1755 softc = (struct da_softc *)periph->softc;
1758 * Automatically detect devices that do not support
1759 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
1762 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
1763 error = cmd6workaround(ccb);
1764 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
1765 CAM_SCSI_STATUS_ERROR)
1766 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
1767 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1768 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
1769 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
1770 int sense_key, error_code, asc, ascq;
1772 scsi_extract_sense(&ccb->csio.sense_data,
1773 &error_code, &sense_key, &asc, &ascq);
1774 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
1775 error = cmd6workaround(ccb);
1777 if (error == ERESTART)
1782 * Until we have a better way of doing pack validation,
1783 * don't treat UAs as errors.
1785 sense_flags |= SF_RETRY_UA;
1786 return(cam_periph_error(ccb, cam_flags, sense_flags,
1787 &softc->saved_ccb));
1791 daprevent(struct cam_periph *periph, int action)
1793 struct da_softc *softc;
1797 softc = (struct da_softc *)periph->softc;
1799 if (((action == PR_ALLOW)
1800 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
1801 || ((action == PR_PREVENT)
1802 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
1806 ccb = cam_periph_getccb(periph, /*priority*/1);
1808 scsi_prevent(&ccb->csio,
1816 error = cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
1817 /*sense_flags*/0, &softc->device_stats);
1820 if (action == PR_ALLOW)
1821 softc->flags &= ~DA_FLAG_PACK_LOCKED;
1823 softc->flags |= DA_FLAG_PACK_LOCKED;
1826 xpt_release_ccb(ccb);
1830 dasetgeom(struct cam_periph *periph, struct scsi_read_capacity_data * rdcap)
1832 struct ccb_calc_geometry ccg;
1833 struct da_softc *softc;
1834 struct disk_params *dp;
1836 softc = (struct da_softc *)periph->softc;
1838 dp = &softc->params;
1839 dp->secsize = scsi_4btoul(rdcap->length);
1840 dp->sectors = scsi_4btoul(rdcap->addr) + 1;
1842 * Have the controller provide us with a geometry
1843 * for this disk. The only time the geometry
1844 * matters is when we boot and the controller
1845 * is the only one knowledgeable enough to come
1846 * up with something that will make this a bootable
1849 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
1850 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
1851 ccg.block_size = dp->secsize;
1852 ccg.volume_size = dp->sectors;
1854 ccg.secs_per_track = 0;
1856 xpt_action((union ccb*)&ccg);
1857 dp->heads = ccg.heads;
1858 dp->secs_per_track = ccg.secs_per_track;
1859 dp->cylinders = ccg.cylinders;
1863 dasendorderedtag(void *arg)
1865 struct da_softc *softc;
1868 for (softc = SLIST_FIRST(&softc_list);
1870 softc = SLIST_NEXT(softc, links)) {
1872 if ((softc->ordered_tag_count == 0)
1873 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
1874 softc->flags |= DA_FLAG_NEED_OTAG;
1876 if (softc->device_stats.busy_count > 0)
1877 softc->flags &= ~DA_FLAG_WENT_IDLE;
1879 softc->ordered_tag_count = 0;
1882 /* Queue us up again */
1883 callout_reset(&dasendorderedtag_ch,
1884 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
1885 dasendorderedtag, NULL);
1889 * Step through all DA peripheral drivers, and if the device is still open,
1890 * sync the disk cache to physical media.
1893 dashutdown(void * arg, int howto)
1895 struct cam_periph *periph;
1896 struct da_softc *softc;
1898 for (periph = TAILQ_FIRST(&dadriver.units); periph != NULL;
1899 periph = TAILQ_NEXT(periph, unit_links)) {
1901 softc = (struct da_softc *)periph->softc;
1904 * We only sync the cache if the drive is still open, and
1905 * if the drive is capable of it..
1907 if (((softc->flags & DA_FLAG_OPEN) == 0)
1908 || (softc->quirks & DA_Q_NO_SYNC_CACHE))
1911 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
1913 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
1914 scsi_synchronize_cache(&ccb.csio,
1918 /*begin_lba*/0, /* whole disk */
1923 xpt_polled_action(&ccb);
1925 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1926 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
1927 CAM_SCSI_STATUS_ERROR)
1928 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
1929 int error_code, sense_key, asc, ascq;
1931 scsi_extract_sense(&ccb.csio.sense_data,
1932 &error_code, &sense_key,
1935 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1936 scsi_sense_print(&ccb.csio);
1938 xpt_print_path(periph->path);
1939 printf("Synchronize cache failed, status "
1940 "== 0x%x, scsi status == 0x%x\n",
1941 ccb.ccb_h.status, ccb.csio.scsi_status);
1945 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1946 cam_release_devq(ccb.ccb_h.path,
1950 /*getcount_only*/0);
1955 #else /* !_KERNEL */
1958 * XXX This is only left out of the kernel build to silence warnings. If,
1959 * for some reason this function is used in the kernel, the ifdefs should
1960 * be moved so it is included both in the kernel and userland.
1963 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
1964 void (*cbfcnp)(struct cam_periph *, union ccb *),
1965 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
1966 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
1969 struct scsi_format_unit *scsi_cmd;
1971 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
1972 scsi_cmd->opcode = FORMAT_UNIT;
1973 scsi_cmd->byte2 = byte2;
1974 scsi_ulto2b(ileave, scsi_cmd->interleave);
1979 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
1988 #endif /* _KERNEL */