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.15 2004/03/15 02:27:56 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>
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
340 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJIFILMUSB-DRIVEUNIT",
341 "USB-DRIVEUNIT", "*"},
342 /*quirks*/ DA_Q_NO_SYNC_CACHE
346 * Minolta Dimage E203
348 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MINOLTA", "DiMAGE E203", "*"},
349 /*quirks*/ DA_Q_NO_SYNC_CACHE
356 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Apacer", "HandyDrive", "*"},
357 /*quirks*/ DA_Q_NO_SYNC_CACHE
361 * Daisy Technology PhotoClip on Zoran chip
364 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ZORAN", "COACH", "*"},
365 /*quirks*/ DA_Q_NO_SYNC_CACHE
369 * Sony USB Key-Storage
372 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Storage Media", "*"},
373 /*quirks*/ DA_Q_NO_SYNC_CACHE
375 #endif /* DA_OLD_QUIRKS */
378 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
381 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
382 /*quirks*/ DA_Q_NO_SYNC_CACHE
386 * Jungsoft NEXDISK USB flash key
389 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
390 /*quirks*/ DA_Q_NO_SYNC_CACHE
394 * Creative Nomad MUVO mp3 player (USB)
397 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
398 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
402 static d_open_t daopen;
403 static d_close_t daclose;
404 static d_strategy_t dastrategy;
405 static d_ioctl_t daioctl;
406 static d_dump_t dadump;
407 static periph_init_t dainit;
408 static void daasync(void *callback_arg, u_int32_t code,
409 struct cam_path *path, void *arg);
410 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
411 static periph_ctor_t daregister;
412 static periph_dtor_t dacleanup;
413 static periph_start_t dastart;
414 static periph_oninv_t daoninvalidate;
415 static void dadone(struct cam_periph *periph,
416 union ccb *done_ccb);
417 static int daerror(union ccb *ccb, u_int32_t cam_flags,
418 u_int32_t sense_flags);
419 static void daprevent(struct cam_periph *periph, int action);
420 static void dasetgeom(struct cam_periph *periph,
421 struct scsi_read_capacity_data * rdcap);
422 static timeout_t dasendorderedtag;
423 static void dashutdown(void *arg, int howto);
425 #ifndef DA_DEFAULT_TIMEOUT
426 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
429 #ifndef DA_DEFAULT_RETRY
430 #define DA_DEFAULT_RETRY 4
433 static int da_retry_count = DA_DEFAULT_RETRY;
434 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
436 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
437 "CAM Direct Access Disk driver");
438 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
439 &da_retry_count, 0, "Normal I/O retry count");
440 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
441 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
442 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
443 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
446 * DA_ORDEREDTAG_INTERVAL determines how often, relative
447 * to the default timeout, we check to see whether an ordered
448 * tagged transaction is appropriate to prevent simple tag
449 * starvation. Since we'd like to ensure that there is at least
450 * 1/2 of the timeout length left for a starved transaction to
451 * complete after we've sent an ordered tag, we must poll at least
452 * four times in every timeout period. This takes care of the worst
453 * case where a starved transaction starts during an interval that
454 * meets the requirement "don't send an ordered tag" test so it takes
455 * us two intervals to determine that a tag must be sent.
457 #ifndef DA_ORDEREDTAG_INTERVAL
458 #define DA_ORDEREDTAG_INTERVAL 4
461 static struct periph_driver dadriver =
464 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
467 DATA_SET(periphdriver_set, dadriver);
469 /* For 2.2-stable support */
474 static struct cdevsw da_cdevsw = {
476 /* maj */ DA_CDEV_MAJOR,
484 /* write */ physwrite,
488 /* strategy */ dastrategy,
493 static SLIST_HEAD(,da_softc) softc_list;
494 static struct extend_array *daperiphs;
497 daopen(dev_t dev, int flags, int fmt, struct thread *td)
499 struct cam_periph *periph;
500 struct da_softc *softc;
501 struct disklabel *label;
509 periph = cam_extend_get(daperiphs, unit);
513 softc = (struct da_softc *)periph->softc;
515 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
516 ("daopen: dev=%s (unit %d , partition %d)\n", devtoname(dev),
519 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
520 return (error); /* error code from tsleep */
523 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
525 softc->flags |= DA_FLAG_OPEN;
528 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
529 /* Invalidate our pack information. */
530 disk_invalidate(&softc->disk);
531 softc->flags &= ~DA_FLAG_PACK_INVALID;
535 /* Do a read capacity */
537 struct scsi_read_capacity_data *rcap;
540 rcap = malloc(sizeof(*rcap), M_TEMP, M_INTWAIT | M_ZERO);
542 ccb = cam_periph_getccb(periph, /*priority*/1);
543 scsi_read_capacity(&ccb->csio,
550 ccb->ccb_h.ccb_bp = NULL;
552 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
553 /*sense_flags*/SF_RETRY_UA |
555 &softc->device_stats);
557 xpt_release_ccb(ccb);
560 dasetgeom(periph, rcap);
567 struct ccb_getdev cgd;
569 /* Build label for whole disk. */
570 label = &softc->disk.d_label;
571 bzero(label, sizeof(*label));
572 label->d_type = DTYPE_SCSI;
575 * Grab the inquiry data to get the vendor and product names.
576 * Put them in the typename and packname for the label.
578 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
579 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
580 xpt_action((union ccb *)&cgd);
582 strncpy(label->d_typename, cgd.inq_data.vendor,
583 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
584 strncpy(label->d_packname, cgd.inq_data.product,
585 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
587 label->d_secsize = softc->params.secsize;
588 label->d_nsectors = softc->params.secs_per_track;
589 label->d_ntracks = softc->params.heads;
590 label->d_ncylinders = softc->params.cylinders;
591 label->d_secpercyl = softc->params.heads
592 * softc->params.secs_per_track;
593 label->d_secperunit = softc->params.sectors;
595 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
596 (softc->quirks & DA_Q_NO_PREVENT) == 0)
597 daprevent(periph, PR_PREVENT);
600 * Check to see whether or not the blocksize is set yet.
601 * If it isn't, set it and then clear the blocksize
602 * unavailable flag for the device statistics.
604 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0){
605 softc->device_stats.block_size = softc->params.secsize;
606 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
611 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
612 (softc->quirks & DA_Q_NO_PREVENT) == 0)
613 daprevent(periph, PR_ALLOW);
614 softc->flags &= ~DA_FLAG_OPEN;
615 cam_periph_release(periph);
617 cam_periph_unlock(periph);
622 daclose(dev_t dev, int flag, int fmt, struct thread *td)
624 struct cam_periph *periph;
625 struct da_softc *softc;
630 periph = cam_extend_get(daperiphs, unit);
634 softc = (struct da_softc *)periph->softc;
636 if ((error = cam_periph_lock(periph, 0)) != 0) {
637 return (error); /* error code from tsleep */
640 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
643 ccb = cam_periph_getccb(periph, /*priority*/1);
645 scsi_synchronize_cache(&ccb->csio,
649 /*begin_lba*/0,/* Cover the whole disk */
654 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
655 /*sense_flags*/SF_RETRY_UA,
656 &softc->device_stats);
658 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
659 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
660 CAM_SCSI_STATUS_ERROR) {
662 int sense_key, error_code;
664 scsi_extract_sense(&ccb->csio.sense_data,
668 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
669 scsi_sense_print(&ccb->csio);
671 xpt_print_path(periph->path);
672 printf("Synchronize cache failed, status "
673 "== 0x%x, scsi status == 0x%x\n",
674 ccb->csio.ccb_h.status,
675 ccb->csio.scsi_status);
679 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
680 cam_release_devq(ccb->ccb_h.path,
686 xpt_release_ccb(ccb);
690 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
691 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
692 daprevent(periph, PR_ALLOW);
694 * If we've got removeable media, mark the blocksize as
695 * unavailable, since it could change when new media is
698 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
701 softc->flags &= ~DA_FLAG_OPEN;
702 cam_periph_unlock(periph);
703 cam_periph_release(periph);
708 * Actually translate the requested transfer into one the physical driver
709 * can understand. The transfer is described by a buf and will include
710 * only one physical transfer.
713 dastrategy(struct buf *bp)
715 struct cam_periph *periph;
716 struct da_softc *softc;
721 unit = dkunit(bp->b_dev);
722 part = dkpart(bp->b_dev);
723 periph = cam_extend_get(daperiphs, unit);
724 if (periph == NULL) {
728 softc = (struct da_softc *)periph->softc;
731 * check it's not too big a transfer for our adapter
733 scsi_minphys(bp,&sd_switch);
737 * Mask interrupts so that the pack cannot be invalidated until
738 * after we are in the queue. Otherwise, we might not properly
739 * clean up one of the buffers.
744 * If the device has been made invalid, error out
746 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
753 * Place it in the queue of disk activities for this disk
755 bufqdisksort(&softc->buf_queue, bp);
760 * Schedule ourselves for performing the work.
762 xpt_schedule(periph, /* XXX priority */1);
766 bp->b_flags |= B_ERROR;
769 * Correctly set the buf to indicate a completed xfer
771 bp->b_resid = bp->b_bcount;
776 /* For 2.2-stable support */
782 daioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
784 struct cam_periph *periph;
785 struct da_softc *softc;
790 periph = cam_extend_get(daperiphs, unit);
794 softc = (struct da_softc *)periph->softc;
796 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("daioctl\n"));
798 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
799 return (error); /* error code from tsleep */
802 error = cam_periph_ioctl(periph, cmd, addr, daerror);
804 cam_periph_unlock(periph);
812 struct cam_periph *periph;
813 struct da_softc *softc;
817 u_int num; /* number of sectors to write */
821 struct ccb_scsiio csio;
822 int dumppages = MAXDUMPPGS;
826 /* toss any characters present prior to dump */
827 while (cncheckc() != -1)
832 periph = cam_extend_get(daperiphs, unit);
833 if (periph == NULL) {
836 softc = (struct da_softc *)periph->softc;
838 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0)
841 error = disk_dumpcheck(dev, &num, &blknum, &secsize);
845 addr = 0; /* starting address */
846 blkcnt = howmany(PAGE_SIZE, secsize);
851 if ((num / blkcnt) < dumppages)
852 dumppages = num / blkcnt;
854 for (i = 0; i < dumppages; ++i) {
855 vm_paddr_t a = addr + (i * PAGE_SIZE);
856 if (is_physical_memory(a))
857 va = pmap_kenter_temporary(trunc_page(a), i);
859 va = pmap_kenter_temporary(trunc_page(0), i);
862 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
863 csio.ccb_h.ccb_state = DA_CCB_DUMP;
864 scsi_read_write(&csio,
870 /*minimum_cmd_size*/ softc->minimum_cmd_size,
873 /*data_ptr*/(u_int8_t *) va,
874 /*dxfer_len*/blkcnt * secsize * dumppages,
875 /*sense_len*/SSD_FULL_SIZE,
876 DA_DEFAULT_TIMEOUT * 1000);
877 xpt_polled_action((union ccb *)&csio);
879 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
880 printf("Aborting dump due to I/O error.\n");
881 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
882 CAM_SCSI_STATUS_ERROR)
883 scsi_sense_print(&csio);
885 printf("status == 0x%x, scsi status == 0x%x\n",
886 csio.ccb_h.status, csio.scsi_status);
890 if (dumpstatus(addr, (off_t)num * softc->params.secsize) < 0)
893 /* update block count */
894 num -= blkcnt * dumppages;
895 blknum += blkcnt * dumppages;
896 addr += PAGE_SIZE * dumppages;
900 * Sync the disk cache contents to the physical media.
902 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
904 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
905 csio.ccb_h.ccb_state = DA_CCB_DUMP;
906 scsi_synchronize_cache(&csio,
910 /*begin_lba*/0,/* Cover the whole disk */
914 xpt_polled_action((union ccb *)&csio);
916 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
917 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
918 CAM_SCSI_STATUS_ERROR) {
920 int sense_key, error_code;
922 scsi_extract_sense(&csio.sense_data,
926 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
927 scsi_sense_print(&csio);
929 xpt_print_path(periph->path);
930 printf("Synchronize cache failed, status "
931 "== 0x%x, scsi status == 0x%x\n",
932 csio.ccb_h.status, csio.scsi_status);
943 struct cam_path *path;
946 * Create our extend array for storing the devices we attach to.
948 daperiphs = cam_extend_new();
949 SLIST_INIT(&softc_list);
950 if (daperiphs == NULL) {
951 printf("da: Failed to alloc extend array!\n");
956 * Install a global async callback. This callback will
957 * receive async callbacks like "new device found".
959 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
960 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
962 if (status == CAM_REQ_CMP) {
963 struct ccb_setasync csa;
965 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
966 csa.ccb_h.func_code = XPT_SASYNC_CB;
967 csa.event_enable = AC_FOUND_DEVICE;
968 csa.callback = daasync;
969 csa.callback_arg = NULL;
970 xpt_action((union ccb *)&csa);
971 status = csa.ccb_h.status;
975 if (status != CAM_REQ_CMP) {
976 printf("da: Failed to attach master async callback "
977 "due to status 0x%x!\n", status);
981 * Schedule a periodic event to occasionally send an
982 * ordered tag to a device.
984 timeout(dasendorderedtag, NULL,
985 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL);
987 /* Register our shutdown event handler */
988 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
989 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
990 printf("dainit: shutdown event registration failed!\n");
995 daoninvalidate(struct cam_periph *periph)
998 struct da_softc *softc;
1000 struct ccb_setasync csa;
1002 softc = (struct da_softc *)periph->softc;
1005 * De-register any async callbacks.
1007 xpt_setup_ccb(&csa.ccb_h, periph->path,
1009 csa.ccb_h.func_code = XPT_SASYNC_CB;
1010 csa.event_enable = 0;
1011 csa.callback = daasync;
1012 csa.callback_arg = periph;
1013 xpt_action((union ccb *)&csa);
1015 softc->flags |= DA_FLAG_PACK_INVALID;
1018 * Although the oninvalidate() routines are always called at
1019 * splsoftcam, we need to be at splbio() here to keep the buffer
1020 * queue from being modified while we traverse it.
1025 * Return all queued I/O with ENXIO.
1026 * XXX Handle any transactions queued to the card
1027 * with XPT_ABORT_CCB.
1029 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
1030 bufq_remove(&softc->buf_queue, q_bp);
1031 q_bp->b_resid = q_bp->b_bcount;
1032 q_bp->b_error = ENXIO;
1033 q_bp->b_flags |= B_ERROR;
1038 SLIST_REMOVE(&softc_list, softc, da_softc, links);
1040 xpt_print_path(periph->path);
1041 printf("lost device\n");
1045 dacleanup(struct cam_periph *periph)
1047 struct da_softc *softc;
1049 softc = (struct da_softc *)periph->softc;
1051 devstat_remove_entry(&softc->device_stats);
1052 cam_extend_release(daperiphs, periph->unit_number);
1053 xpt_print_path(periph->path);
1054 printf("removing device entry\n");
1056 * If we can't free the sysctl tree, oh well...
1058 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1059 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1060 xpt_print_path(periph->path);
1061 printf("can't remove sysctl context\n");
1063 if (softc->disk.d_dev) {
1064 disk_destroy(&softc->disk);
1066 free(softc, M_DEVBUF);
1070 daasync(void *callback_arg, u_int32_t code,
1071 struct cam_path *path, void *arg)
1073 struct cam_periph *periph;
1075 periph = (struct cam_periph *)callback_arg;
1077 case AC_FOUND_DEVICE:
1079 struct ccb_getdev *cgd;
1082 cgd = (struct ccb_getdev *)arg;
1084 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1085 && SID_TYPE(&cgd->inq_data) != T_RBC
1086 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1090 * Allocate a peripheral instance for
1091 * this device and start the probe
1094 status = cam_periph_alloc(daregister, daoninvalidate,
1096 "da", CAM_PERIPH_BIO,
1097 cgd->ccb_h.path, daasync,
1098 AC_FOUND_DEVICE, cgd);
1100 if (status != CAM_REQ_CMP
1101 && status != CAM_REQ_INPROG)
1102 printf("daasync: Unable to attach to new device "
1103 "due to status 0x%x\n", status);
1109 struct da_softc *softc;
1110 struct ccb_hdr *ccbh;
1113 softc = (struct da_softc *)periph->softc;
1116 * Don't fail on the expected unit attention
1119 softc->flags |= DA_FLAG_RETRY_UA;
1120 for (ccbh = LIST_FIRST(&softc->pending_ccbs);
1121 ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le))
1122 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1127 cam_periph_async(periph, code, path, arg);
1133 dasysctlinit(void *context, int pending)
1135 struct cam_periph *periph;
1136 struct da_softc *softc;
1137 char tmpstr[80], tmpstr2[80];
1139 periph = (struct cam_periph *)context;
1140 softc = (struct da_softc *)periph->softc;
1142 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1143 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1145 sysctl_ctx_init(&softc->sysctl_ctx);
1146 softc->flags |= DA_FLAG_SCTX_INIT;
1147 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1148 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1149 CTLFLAG_RD, 0, tmpstr);
1150 if (softc->sysctl_tree == NULL) {
1151 printf("dasysctlinit: unable to allocate sysctl tree\n");
1156 * Now register the sysctl handler, so the user can the value on
1159 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1160 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1161 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1162 "Minimum CDB size");
1166 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1170 value = *(int *)arg1;
1172 error = sysctl_handle_int(oidp, &value, 0, req);
1175 || (req->newptr == NULL))
1179 * Acceptable values here are 6, 10 or 12. It's possible we may
1180 * support a 16 byte minimum command size in the future, since
1181 * there are now READ(16) and WRITE(16) commands defined in the
1186 else if ((value > 6)
1189 else if (value > 10)
1192 *(int *)arg1 = value;
1198 daregister(struct cam_periph *periph, void *arg)
1201 struct da_softc *softc;
1202 struct ccb_setasync csa;
1203 struct ccb_pathinq cpi;
1204 struct ccb_getdev *cgd;
1208 cgd = (struct ccb_getdev *)arg;
1209 if (periph == NULL) {
1210 printf("daregister: periph was NULL!!\n");
1211 return(CAM_REQ_CMP_ERR);
1215 printf("daregister: no getdev CCB, can't register device\n");
1216 return(CAM_REQ_CMP_ERR);
1219 softc = malloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
1220 LIST_INIT(&softc->pending_ccbs);
1221 softc->state = DA_STATE_PROBE;
1222 bufq_init(&softc->buf_queue);
1223 if (SID_IS_REMOVABLE(&cgd->inq_data))
1224 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1225 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1226 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1228 periph->softc = softc;
1230 cam_extend_set(daperiphs, periph->unit_number, periph);
1233 * See if this device has any quirks.
1235 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1236 (caddr_t)da_quirk_table,
1237 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1238 sizeof(*da_quirk_table), scsi_inquiry_match);
1241 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1243 softc->quirks = DA_Q_NONE;
1245 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1247 /* Check if the SIM does not want 6 byte commands */
1248 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
1249 cpi.ccb_h.func_code = XPT_PATH_INQ;
1250 xpt_action((union ccb *)&cpi);
1251 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1252 softc->quirks |= DA_Q_NO_6_BYTE;
1255 * RBC devices don't have to support READ(6), only READ(10).
1257 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1258 softc->minimum_cmd_size = 10;
1260 softc->minimum_cmd_size = 6;
1263 * Load the user's default, if any.
1265 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1266 periph->unit_number);
1267 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1270 * 6, 10 and 12 are the currently permissible values.
1272 if (softc->minimum_cmd_size < 6)
1273 softc->minimum_cmd_size = 6;
1274 else if ((softc->minimum_cmd_size > 6)
1275 && (softc->minimum_cmd_size <= 10))
1276 softc->minimum_cmd_size = 10;
1277 else if (softc->minimum_cmd_size > 12)
1278 softc->minimum_cmd_size = 12;
1281 * Block our timeout handler while we
1282 * add this softc to the dev list.
1285 SLIST_INSERT_HEAD(&softc_list, softc, links);
1289 * The DA driver supports a blocksize, but
1290 * we don't know the blocksize until we do
1291 * a read capacity. So, set a flag to
1292 * indicate that the blocksize is
1293 * unavailable right now. We'll clear the
1294 * flag as soon as we've done a read capacity.
1296 devstat_add_entry(&softc->device_stats, "da",
1297 periph->unit_number, 0,
1298 DEVSTAT_BS_UNAVAILABLE,
1299 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
1300 DEVSTAT_PRIORITY_DISK);
1303 * Register this media as a disk
1305 disk_create(periph->unit_number, &softc->disk, 0, &da_cdevsw);
1308 * Add async callbacks for bus reset and
1309 * bus device reset calls. I don't bother
1310 * checking if this fails as, in most cases,
1311 * the system will function just fine without
1312 * them and the only alternative would be to
1313 * not attach the device on failure.
1315 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
1316 csa.ccb_h.func_code = XPT_SASYNC_CB;
1317 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
1318 csa.callback = daasync;
1319 csa.callback_arg = periph;
1320 xpt_action((union ccb *)&csa);
1322 * Lock this peripheral until we are setup.
1323 * This first call can't block
1325 (void)cam_periph_lock(periph, 0);
1326 xpt_schedule(periph, /*priority*/5);
1328 return(CAM_REQ_CMP);
1332 dastart(struct cam_periph *periph, union ccb *start_ccb)
1334 struct da_softc *softc;
1336 softc = (struct da_softc *)periph->softc;
1339 switch (softc->state) {
1340 case DA_STATE_NORMAL:
1342 /* Pull a buffer from the queue and get going on it */
1347 * See if there is a buf with work for us to do..
1350 bp = bufq_first(&softc->buf_queue);
1351 if (periph->immediate_priority <= periph->pinfo.priority) {
1352 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1353 ("queuing for immediate ccb\n"));
1354 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1355 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1357 periph->immediate_priority = CAM_PRIORITY_NONE;
1359 wakeup(&periph->ccb_list);
1360 } else if (bp == NULL) {
1362 xpt_release_ccb(start_ccb);
1367 bufq_remove(&softc->buf_queue, bp);
1369 devstat_start_transaction(&softc->device_stats);
1371 if ((bp->b_flags & B_ORDERED) != 0
1372 || (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1373 softc->flags &= ~DA_FLAG_NEED_OTAG;
1374 softc->ordered_tag_count++;
1375 tag_code = MSG_ORDERED_Q_TAG;
1377 tag_code = MSG_SIMPLE_Q_TAG;
1379 scsi_read_write(&start_ccb->csio,
1380 /*retries*/da_retry_count,
1383 bp->b_flags & B_READ,
1385 softc->minimum_cmd_size,
1387 bp->b_bcount / softc->params.secsize,
1390 /*sense_len*/SSD_FULL_SIZE,
1391 da_default_timeout * 1000);
1392 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1395 * Block out any asyncronous callbacks
1396 * while we touch the pending ccb list.
1399 LIST_INSERT_HEAD(&softc->pending_ccbs,
1400 &start_ccb->ccb_h, periph_links.le);
1403 /* We expect a unit attention from this device */
1404 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1405 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1406 softc->flags &= ~DA_FLAG_RETRY_UA;
1409 start_ccb->ccb_h.ccb_bp = bp;
1410 bp = bufq_first(&softc->buf_queue);
1413 xpt_action(start_ccb);
1417 /* Have more work to do, so ensure we stay scheduled */
1418 xpt_schedule(periph, /* XXX priority */1);
1422 case DA_STATE_PROBE:
1424 struct ccb_scsiio *csio;
1425 struct scsi_read_capacity_data *rcap;
1427 rcap = malloc(sizeof(*rcap), M_TEMP, M_INTWAIT | M_ZERO);
1428 csio = &start_ccb->csio;
1429 scsi_read_capacity(csio,
1436 start_ccb->ccb_h.ccb_bp = NULL;
1437 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1438 xpt_action(start_ccb);
1445 cmd6workaround(union ccb *ccb)
1447 struct scsi_rw_6 cmd6;
1448 struct scsi_rw_10 *cmd10;
1449 struct da_softc *softc;
1453 cdb = ccb->csio.cdb_io.cdb_bytes;
1455 /* Translation only possible if CDB is an array and cmd is R/W6 */
1456 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1457 (*cdb != READ_6 && *cdb != WRITE_6))
1460 xpt_print_path(ccb->ccb_h.path);
1461 printf("READ(6)/WRITE(6) not supported, "
1462 "increasing minimum_cmd_size to 10.\n");
1463 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1464 softc->minimum_cmd_size = 10;
1466 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1467 cmd10 = (struct scsi_rw_10 *)cdb;
1468 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1470 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1471 cmd10->reserved = 0;
1472 scsi_ulto2b(cmd6.length, cmd10->length);
1473 cmd10->control = cmd6.control;
1474 ccb->csio.cdb_len = sizeof(*cmd10);
1476 /* Requeue request, unfreezing queue if necessary */
1477 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1478 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1481 cam_release_devq(ccb->ccb_h.path,
1485 /*getcount_only*/0);
1491 dadone(struct cam_periph *periph, union ccb *done_ccb)
1493 struct da_softc *softc;
1494 struct ccb_scsiio *csio;
1496 softc = (struct da_softc *)periph->softc;
1497 csio = &done_ccb->csio;
1498 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1499 case DA_CCB_BUFFER_IO:
1504 bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
1505 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1510 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1515 /* Retry selection timeouts */
1516 sf |= SF_RETRY_SELTO;
1518 if ((error = daerror(done_ccb, 0, sf)) == ERESTART) {
1520 * A retry was scheuled, so
1530 if (error == ENXIO) {
1532 * Catastrophic error. Mark our pack as
1535 /* XXX See if this is really a media
1538 xpt_print_path(periph->path);
1539 printf("Invalidating pack\n");
1540 softc->flags |= DA_FLAG_PACK_INVALID;
1544 * return all queued I/O with EIO, so that
1545 * the client can retry these I/Os in the
1546 * proper order should it attempt to recover.
1548 while ((q_bp = bufq_first(&softc->buf_queue))
1550 bufq_remove(&softc->buf_queue, q_bp);
1551 q_bp->b_resid = q_bp->b_bcount;
1552 q_bp->b_error = EIO;
1553 q_bp->b_flags |= B_ERROR;
1557 bp->b_error = error;
1558 bp->b_resid = bp->b_bcount;
1559 bp->b_flags |= B_ERROR;
1561 bp->b_resid = csio->resid;
1563 if (bp->b_resid != 0)
1564 bp->b_flags |= B_ERROR;
1566 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1567 cam_release_devq(done_ccb->ccb_h.path,
1571 /*getcount_only*/0);
1573 bp->b_resid = csio->resid;
1574 if (csio->resid > 0)
1575 bp->b_flags |= B_ERROR;
1579 * Block out any asyncronous callbacks
1580 * while we touch the pending ccb list.
1583 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1586 if (softc->device_stats.busy_count == 0)
1587 softc->flags |= DA_FLAG_WENT_IDLE;
1589 devstat_end_transaction_buf(&softc->device_stats, bp);
1595 struct scsi_read_capacity_data *rdcap;
1596 char announce_buf[80];
1598 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1600 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1601 struct disk_params *dp;
1603 dasetgeom(periph, rdcap);
1604 dp = &softc->params;
1605 snprintf(announce_buf, sizeof(announce_buf),
1606 "%luMB (%u %u byte sectors: %dH %dS/T %dC)",
1607 (unsigned long) (((u_int64_t)dp->secsize *
1608 dp->sectors) / (1024*1024)), dp->sectors,
1609 dp->secsize, dp->heads, dp->secs_per_track,
1614 announce_buf[0] = '\0';
1617 * Retry any UNIT ATTENTION type errors. They
1618 * are expected at boot.
1620 error = daerror(done_ccb, 0, SF_RETRY_UA |
1621 SF_RETRY_SELTO | SF_NO_PRINT);
1622 if (error == ERESTART) {
1624 * A retry was scheuled, so
1628 } else if (error != 0) {
1629 struct scsi_sense_data *sense;
1631 int sense_key, error_code;
1634 struct ccb_getdev cgd;
1636 /* Don't wedge this device's queue */
1637 cam_release_devq(done_ccb->ccb_h.path,
1641 /*getcount_only*/0);
1643 status = done_ccb->ccb_h.status;
1645 xpt_setup_ccb(&cgd.ccb_h,
1646 done_ccb->ccb_h.path,
1648 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1649 xpt_action((union ccb *)&cgd);
1651 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1652 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1653 || ((status & CAM_AUTOSNS_VALID) == 0))
1659 sense = &csio->sense_data;
1660 scsi_extract_sense(sense, &error_code,
1665 * Attach to anything that claims to be a
1666 * direct access or optical disk device,
1667 * as long as it doesn't return a "Logical
1668 * unit not supported" (0x25) error.
1670 if ((have_sense) && (asc != 0x25)
1671 && (error_code == SSD_CURRENT_ERROR))
1672 snprintf(announce_buf,
1673 sizeof(announce_buf),
1674 "Attempt to query device "
1675 "size failed: %s, %s",
1676 scsi_sense_key_text[sense_key],
1677 scsi_sense_desc(asc,ascq,
1684 xpt_print_path(periph->path);
1685 printf("got CAM status %#x\n",
1686 done_ccb->ccb_h.status);
1689 xpt_print_path(periph->path);
1690 printf("fatal error, failed"
1691 " to attach to device\n");
1694 * Free up resources.
1696 cam_periph_invalidate(periph);
1700 free(rdcap, M_TEMP);
1701 if (announce_buf[0] != '\0') {
1702 xpt_announce_periph(periph, announce_buf);
1704 * Create our sysctl variables, now that we know
1705 * we have successfully attached.
1707 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1709 softc->state = DA_STATE_NORMAL;
1711 * Since our peripheral may be invalidated by an error
1712 * above or an external event, we must release our CCB
1713 * before releasing the probe lock on the peripheral.
1714 * The peripheral will only go away once the last lock
1715 * is removed, and we need it around for the CCB release
1718 xpt_release_ccb(done_ccb);
1719 cam_periph_unlock(periph);
1722 case DA_CCB_WAITING:
1724 /* Caller will release the CCB */
1725 wakeup(&done_ccb->ccb_h.cbfcnp);
1729 /* No-op. We're polling */
1734 xpt_release_ccb(done_ccb);
1738 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1740 struct da_softc *softc;
1741 struct cam_periph *periph;
1744 periph = xpt_path_periph(ccb->ccb_h.path);
1745 softc = (struct da_softc *)periph->softc;
1748 * Automatically detect devices that do not support
1749 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
1752 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
1753 error = cmd6workaround(ccb);
1754 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
1755 CAM_SCSI_STATUS_ERROR)
1756 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
1757 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1758 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
1759 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
1760 int sense_key, error_code, asc, ascq;
1762 scsi_extract_sense(&ccb->csio.sense_data,
1763 &error_code, &sense_key, &asc, &ascq);
1764 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
1765 error = cmd6workaround(ccb);
1767 if (error == ERESTART)
1772 * Until we have a better way of doing pack validation,
1773 * don't treat UAs as errors.
1775 sense_flags |= SF_RETRY_UA;
1776 return(cam_periph_error(ccb, cam_flags, sense_flags,
1777 &softc->saved_ccb));
1781 daprevent(struct cam_periph *periph, int action)
1783 struct da_softc *softc;
1787 softc = (struct da_softc *)periph->softc;
1789 if (((action == PR_ALLOW)
1790 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
1791 || ((action == PR_PREVENT)
1792 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
1796 ccb = cam_periph_getccb(periph, /*priority*/1);
1798 scsi_prevent(&ccb->csio,
1806 error = cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
1807 /*sense_flags*/0, &softc->device_stats);
1810 if (action == PR_ALLOW)
1811 softc->flags &= ~DA_FLAG_PACK_LOCKED;
1813 softc->flags |= DA_FLAG_PACK_LOCKED;
1816 xpt_release_ccb(ccb);
1820 dasetgeom(struct cam_periph *periph, struct scsi_read_capacity_data * rdcap)
1822 struct ccb_calc_geometry ccg;
1823 struct da_softc *softc;
1824 struct disk_params *dp;
1826 softc = (struct da_softc *)periph->softc;
1828 dp = &softc->params;
1829 dp->secsize = scsi_4btoul(rdcap->length);
1830 dp->sectors = scsi_4btoul(rdcap->addr) + 1;
1832 * Have the controller provide us with a geometry
1833 * for this disk. The only time the geometry
1834 * matters is when we boot and the controller
1835 * is the only one knowledgeable enough to come
1836 * up with something that will make this a bootable
1839 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
1840 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
1841 ccg.block_size = dp->secsize;
1842 ccg.volume_size = dp->sectors;
1844 ccg.secs_per_track = 0;
1846 xpt_action((union ccb*)&ccg);
1847 dp->heads = ccg.heads;
1848 dp->secs_per_track = ccg.secs_per_track;
1849 dp->cylinders = ccg.cylinders;
1853 dasendorderedtag(void *arg)
1855 struct da_softc *softc;
1858 for (softc = SLIST_FIRST(&softc_list);
1860 softc = SLIST_NEXT(softc, links)) {
1862 if ((softc->ordered_tag_count == 0)
1863 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
1864 softc->flags |= DA_FLAG_NEED_OTAG;
1866 if (softc->device_stats.busy_count > 0)
1867 softc->flags &= ~DA_FLAG_WENT_IDLE;
1869 softc->ordered_tag_count = 0;
1872 /* Queue us up again */
1873 timeout(dasendorderedtag, NULL,
1874 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL);
1878 * Step through all DA peripheral drivers, and if the device is still open,
1879 * sync the disk cache to physical media.
1882 dashutdown(void * arg, int howto)
1884 struct cam_periph *periph;
1885 struct da_softc *softc;
1887 for (periph = TAILQ_FIRST(&dadriver.units); periph != NULL;
1888 periph = TAILQ_NEXT(periph, unit_links)) {
1890 softc = (struct da_softc *)periph->softc;
1893 * We only sync the cache if the drive is still open, and
1894 * if the drive is capable of it..
1896 if (((softc->flags & DA_FLAG_OPEN) == 0)
1897 || (softc->quirks & DA_Q_NO_SYNC_CACHE))
1900 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
1902 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
1903 scsi_synchronize_cache(&ccb.csio,
1907 /*begin_lba*/0, /* whole disk */
1912 xpt_polled_action(&ccb);
1914 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1915 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
1916 CAM_SCSI_STATUS_ERROR)
1917 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
1918 int error_code, sense_key, asc, ascq;
1920 scsi_extract_sense(&ccb.csio.sense_data,
1921 &error_code, &sense_key,
1924 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1925 scsi_sense_print(&ccb.csio);
1927 xpt_print_path(periph->path);
1928 printf("Synchronize cache failed, status "
1929 "== 0x%x, scsi status == 0x%x\n",
1930 ccb.ccb_h.status, ccb.csio.scsi_status);
1934 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1935 cam_release_devq(ccb.ccb_h.path,
1939 /*getcount_only*/0);
1944 #else /* !_KERNEL */
1947 * XXX This is only left out of the kernel build to silence warnings. If,
1948 * for some reason this function is used in the kernel, the ifdefs should
1949 * be moved so it is included both in the kernel and userland.
1952 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
1953 void (*cbfcnp)(struct cam_periph *, union ccb *),
1954 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
1955 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
1958 struct scsi_format_unit *scsi_cmd;
1960 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
1961 scsi_cmd->opcode = FORMAT_UNIT;
1962 scsi_cmd->byte2 = byte2;
1963 scsi_ulto2b(ileave, scsi_cmd->interleave);
1968 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
1977 #endif /* _KERNEL */