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.23 2005/06/02 20:40:31 dillon Exp $
33 #include "opt_hw_wdog.h"
36 #include <sys/param.h>
37 #include <sys/bootmaj.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/sysctl.h>
44 #include <sys/taskqueue.h>
47 #include <sys/devicestat.h>
51 #include <sys/eventhandler.h>
52 #include <sys/malloc.h>
57 #include <sys/thread2.h>
59 #include <machine/md_var.h>
72 #include "../cam_ccb.h"
73 #include "../cam_extend.h"
74 #include "../cam_periph.h"
75 #include "../cam_xpt_periph.h"
77 #include "scsi_message.h"
90 DA_FLAG_PACK_INVALID = 0x001,
91 DA_FLAG_NEW_PACK = 0x002,
92 DA_FLAG_PACK_LOCKED = 0x004,
93 DA_FLAG_PACK_REMOVABLE = 0x008,
94 DA_FLAG_TAGGED_QUEUING = 0x010,
95 DA_FLAG_NEED_OTAG = 0x020,
96 DA_FLAG_WENT_IDLE = 0x040,
97 DA_FLAG_RETRY_UA = 0x080,
99 DA_FLAG_SCTX_INIT = 0x200
104 DA_Q_NO_SYNC_CACHE = 0x01,
105 DA_Q_NO_6_BYTE = 0x02,
106 DA_Q_NO_PREVENT = 0x04
111 DA_CCB_BUFFER_IO = 0x02,
112 DA_CCB_WAITING = 0x03,
114 DA_CCB_TYPE_MASK = 0x0F,
115 DA_CCB_RETRY_UA = 0x10
118 /* Offsets into our private area for storing information */
119 #define ccb_state ppriv_field0
120 #define ccb_bp ppriv_ptr1
125 u_int8_t secs_per_track;
126 u_int32_t secsize; /* Number of bytes/sector */
127 u_int32_t sectors; /* total number sectors */
131 struct buf_queue_head buf_queue;
132 struct devstat device_stats;
133 SLIST_ENTRY(da_softc) links;
134 LIST_HEAD(, ccb_hdr) pending_ccbs;
138 int minimum_cmd_size;
139 int ordered_tag_count;
140 struct disk_params params;
143 struct task sysctl_task;
144 struct sysctl_ctx_list sysctl_ctx;
145 struct sysctl_oid *sysctl_tree;
148 struct da_quirk_entry {
149 struct scsi_inquiry_pattern inq_pat;
153 static const char quantum[] = "QUANTUM";
154 static const char microp[] = "MICROP";
156 static struct da_quirk_entry da_quirk_table[] =
158 /* SPI, FC devices */
161 * Fujitsu M2513A MO drives.
162 * Tested devices: M2513A2 firmware versions 1200 & 1300.
163 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
164 * Reported by: W.Scholten <whs@xs4all.nl>
166 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
167 /*quirks*/ DA_Q_NO_SYNC_CACHE
171 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
172 /*quirks*/ DA_Q_NO_SYNC_CACHE
176 * This particular Fujitsu drive doesn't like the
177 * synchronize cache command.
178 * Reported by: Tom Jackson <toj@gorilla.net>
180 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
181 /*quirks*/ DA_Q_NO_SYNC_CACHE
186 * This drive doesn't like the synchronize cache command
187 * either. Reported by: Matthew Jacob <mjacob@feral.com>
188 * in NetBSD PR kern/6027, August 24, 1998.
190 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
191 /*quirks*/ DA_Q_NO_SYNC_CACHE
195 * This drive doesn't like the synchronize cache command
196 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
199 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
200 /*quirks*/ DA_Q_NO_SYNC_CACHE
204 * Doesn't like the synchronize cache command.
205 * Reported by: Blaz Zupan <blaz@gold.amis.net>
207 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
208 /*quirks*/ DA_Q_NO_SYNC_CACHE
212 * Doesn't like the synchronize cache command.
214 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
215 /*quirks*/ DA_Q_NO_SYNC_CACHE
219 * Doesn't like the synchronize cache command.
221 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
222 /*quirks*/ DA_Q_NO_SYNC_CACHE
226 * Doesn't work correctly with 6 byte reads/writes.
227 * Returns illegal request, and points to byte 9 of the
229 * Reported by: Adam McDougall <bsdx@spawnet.com>
231 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
232 /*quirks*/ DA_Q_NO_6_BYTE
236 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
237 /*quirks*/ DA_Q_NO_6_BYTE
239 /* XXX USB floppy quirks temporarily enabled for 4.9R */
240 /* USB floppy devices supported by umass(4) */
243 * This USB floppy drive uses the UFI command set. This
244 * command set is a derivative of the ATAPI command set and
245 * does not support READ_6 commands only READ_10. It also does
246 * not support sync cache (0x35).
248 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Y-E DATA", "USB-FDU", "*"},
249 /*quirks*/ DA_Q_NO_SYNC_CACHE
252 /* Another USB floppy */
253 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MATSHITA", "FDD CF-VFDU*","*"},
254 /*quirks*/ DA_Q_NO_SYNC_CACHE
258 * The vendor, product and version strings coming from the
259 * controller are null terminated instead of being padded with
260 * spaces. The trailing wildcard character '*' is required.
262 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SMSC*", "USB FDC*","*"},
263 /*quirks*/ DA_Q_NO_SYNC_CACHE
267 * SmartDisk (Mitsumi) USB floppy drive
270 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MITSUMI", "USB FDD", "*"},
271 /*quirks*/ DA_Q_NO_SYNC_CACHE
275 * The CISS RAID controllers do not support SYNC_CACHE
277 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
278 /*quirks*/ DA_Q_NO_SYNC_CACHE
281 /* USB mass storage devices supported by umass(4) */
284 * Sony Memory Stick adapter MSAC-US1 and
285 * Sony PCG-C1VJ Internal Memory Stick Slot (MSC-U01).
286 * Make all sony MS* products use this quirk.
288 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "MS*", "*"},
289 /*quirks*/ DA_Q_NO_SYNC_CACHE
293 * Sony Memory Stick adapter for the CLIE series
296 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "CLIE*", "*"},
297 /*quirks*/ DA_Q_NO_SYNC_CACHE
301 * Intelligent Stick USB disk-on-key
304 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB Card",
305 "IntelligentStick*", "*"},
306 /*quirks*/ DA_Q_NO_SYNC_CACHE
310 * Sony DSC cameras (DSC-S30, DSC-S50, DSC-S70)
312 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
313 /*quirks*/ DA_Q_NO_SYNC_CACHE
317 * Microtech USB CameraMate
319 {T_DIRECT, SIP_MEDIA_REMOVABLE, "eUSB Compact*",
320 "Compact Flash*", "*"},
321 /*quirks*/ DA_Q_NO_SYNC_CACHE
325 * Olympus digital cameras (C-3040ZOOM, C-2040ZOOM, C-1)
327 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "C-*", "*"},
328 /*quirks*/ DA_Q_NO_SYNC_CACHE
332 * Olympus digital cameras (E-100RS, E-10).
334 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "E-*", "*"},
335 /*quirks*/ DA_Q_NO_SYNC_CACHE
339 * KingByte Pen Drives
341 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NO BRAND", "PEN DRIVE", "*"},
342 /*quirks*/ DA_Q_NO_SYNC_CACHE
346 * iRiver iFP MP3 player (with UMS Firmware)
347 * PR: kern/54881, i386/63941, kern/66124
349 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
350 /*quirks*/ DA_Q_NO_SYNC_CACHE
354 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
357 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "NexIA+*", "*"},
358 /*quirks*/ DA_Q_NO_SYNC_CACHE
364 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJIFILMUSB-DRIVEUNIT",
365 "USB-DRIVEUNIT", "*"},
366 /*quirks*/ DA_Q_NO_SYNC_CACHE
370 * Minolta Dimage E203
372 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MINOLTA", "DiMAGE E203", "*"},
373 /*quirks*/ DA_Q_NO_SYNC_CACHE
380 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Apacer", "HandyDrive", "*"},
381 /*quirks*/ DA_Q_NO_SYNC_CACHE
385 * Daisy Technology PhotoClip on Zoran chip
388 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ZORAN", "COACH", "*"},
389 /*quirks*/ DA_Q_NO_SYNC_CACHE
393 * Sony USB Key-Storage
396 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Storage Media", "*"},
397 /*quirks*/ DA_Q_NO_SYNC_CACHE
399 #endif /* DA_OLD_QUIRKS */
402 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
405 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
406 /*quirks*/ DA_Q_NO_SYNC_CACHE
410 * Jungsoft NEXDISK USB flash key
413 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
414 /*quirks*/ DA_Q_NO_SYNC_CACHE
418 * Creative Nomad MUVO mp3 player (USB)
421 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
422 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
426 static d_open_t daopen;
427 static d_close_t daclose;
428 static d_strategy_t dastrategy;
429 static d_ioctl_t daioctl;
430 static d_dump_t dadump;
431 static periph_init_t dainit;
432 static void daasync(void *callback_arg, u_int32_t code,
433 struct cam_path *path, void *arg);
434 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
435 static periph_ctor_t daregister;
436 static periph_dtor_t dacleanup;
437 static periph_start_t dastart;
438 static periph_oninv_t daoninvalidate;
439 static void dadone(struct cam_periph *periph,
440 union ccb *done_ccb);
441 static int daerror(union ccb *ccb, u_int32_t cam_flags,
442 u_int32_t sense_flags);
443 static void daprevent(struct cam_periph *periph, int action);
444 static void dasetgeom(struct cam_periph *periph,
445 struct scsi_read_capacity_data * rdcap);
446 static timeout_t dasendorderedtag;
447 static void dashutdown(void *arg, int howto);
449 #ifndef DA_DEFAULT_TIMEOUT
450 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
453 #ifndef DA_DEFAULT_RETRY
454 #define DA_DEFAULT_RETRY 4
457 static int da_retry_count = DA_DEFAULT_RETRY;
458 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
459 static struct callout dasendorderedtag_ch;
461 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
462 "CAM Direct Access Disk driver");
463 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
464 &da_retry_count, 0, "Normal I/O retry count");
465 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
466 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
467 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
468 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
471 * DA_ORDEREDTAG_INTERVAL determines how often, relative
472 * to the default timeout, we check to see whether an ordered
473 * tagged transaction is appropriate to prevent simple tag
474 * starvation. Since we'd like to ensure that there is at least
475 * 1/2 of the timeout length left for a starved transaction to
476 * complete after we've sent an ordered tag, we must poll at least
477 * four times in every timeout period. This takes care of the worst
478 * case where a starved transaction starts during an interval that
479 * meets the requirement "don't send an ordered tag" test so it takes
480 * us two intervals to determine that a tag must be sent.
482 #ifndef DA_ORDEREDTAG_INTERVAL
483 #define DA_ORDEREDTAG_INTERVAL 4
486 static struct periph_driver dadriver =
489 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
492 DATA_SET(periphdriver_set, dadriver);
494 /* For 2.2-stable support */
499 static struct cdevsw da_cdevsw = {
501 /* maj */ DA_CDEV_MAJOR,
509 /* write */ physwrite,
513 /* strategy */ dastrategy,
518 static SLIST_HEAD(,da_softc) softc_list;
519 static struct extend_array *daperiphs;
522 daopen(dev_t dev, int flags, int fmt, struct thread *td)
524 struct cam_periph *periph;
525 struct da_softc *softc;
526 struct disklabel *label;
533 periph = cam_extend_get(daperiphs, unit);
537 softc = (struct da_softc *)periph->softc;
539 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
540 ("daopen: dev=%s (unit %d , partition %d)\n", devtoname(dev),
543 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
544 return (error); /* error code from tsleep */
547 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
549 softc->flags |= DA_FLAG_OPEN;
552 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
553 /* Invalidate our pack information. */
554 disk_invalidate(&softc->disk);
555 softc->flags &= ~DA_FLAG_PACK_INVALID;
559 /* Do a read capacity */
561 struct scsi_read_capacity_data *rcap;
564 rcap = malloc(sizeof(*rcap), M_TEMP, M_INTWAIT | M_ZERO);
566 ccb = cam_periph_getccb(periph, /*priority*/1);
567 scsi_read_capacity(&ccb->csio,
574 ccb->ccb_h.ccb_bp = NULL;
576 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
577 /*sense_flags*/SF_RETRY_UA |
579 &softc->device_stats);
581 xpt_release_ccb(ccb);
584 dasetgeom(periph, rcap);
591 struct ccb_getdev cgd;
593 /* Build label for whole disk. */
594 label = &softc->disk.d_label;
595 bzero(label, sizeof(*label));
596 label->d_type = DTYPE_SCSI;
599 * Grab the inquiry data to get the vendor and product names.
600 * Put them in the typename and packname for the label.
602 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
603 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
604 xpt_action((union ccb *)&cgd);
606 strncpy(label->d_typename, cgd.inq_data.vendor,
607 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
608 strncpy(label->d_packname, cgd.inq_data.product,
609 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
611 label->d_secsize = softc->params.secsize;
612 label->d_nsectors = softc->params.secs_per_track;
613 label->d_ntracks = softc->params.heads;
614 label->d_ncylinders = softc->params.cylinders;
615 label->d_secpercyl = softc->params.heads
616 * softc->params.secs_per_track;
617 label->d_secperunit = softc->params.sectors;
619 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
620 (softc->quirks & DA_Q_NO_PREVENT) == 0)
621 daprevent(periph, PR_PREVENT);
624 * Check to see whether or not the blocksize is set yet.
625 * If it isn't, set it and then clear the blocksize
626 * unavailable flag for the device statistics.
628 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0){
629 softc->device_stats.block_size = softc->params.secsize;
630 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
635 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
636 (softc->quirks & DA_Q_NO_PREVENT) == 0)
637 daprevent(periph, PR_ALLOW);
638 softc->flags &= ~DA_FLAG_OPEN;
639 cam_periph_release(periph);
641 cam_periph_unlock(periph);
646 daclose(dev_t dev, int flag, int fmt, struct thread *td)
648 struct cam_periph *periph;
649 struct da_softc *softc;
654 periph = cam_extend_get(daperiphs, unit);
658 softc = (struct da_softc *)periph->softc;
660 if ((error = cam_periph_lock(periph, 0)) != 0) {
661 return (error); /* error code from tsleep */
664 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
667 ccb = cam_periph_getccb(periph, /*priority*/1);
669 scsi_synchronize_cache(&ccb->csio,
673 /*begin_lba*/0,/* Cover the whole disk */
678 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
679 /*sense_flags*/SF_RETRY_UA,
680 &softc->device_stats);
682 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
683 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
684 CAM_SCSI_STATUS_ERROR) {
686 int sense_key, error_code;
688 scsi_extract_sense(&ccb->csio.sense_data,
692 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
693 scsi_sense_print(&ccb->csio);
695 xpt_print_path(periph->path);
696 printf("Synchronize cache failed, status "
697 "== 0x%x, scsi status == 0x%x\n",
698 ccb->csio.ccb_h.status,
699 ccb->csio.scsi_status);
703 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
704 cam_release_devq(ccb->ccb_h.path,
710 xpt_release_ccb(ccb);
714 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
715 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
716 daprevent(periph, PR_ALLOW);
718 * If we've got removeable media, mark the blocksize as
719 * unavailable, since it could change when new media is
722 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
725 softc->flags &= ~DA_FLAG_OPEN;
726 cam_periph_unlock(periph);
727 cam_periph_release(periph);
732 * Actually translate the requested transfer into one the physical driver
733 * can understand. The transfer is described by a buf and will include
734 * only one physical transfer.
737 dastrategy(struct buf *bp)
739 struct cam_periph *periph;
740 struct da_softc *softc;
744 unit = dkunit(bp->b_dev);
745 part = dkpart(bp->b_dev);
746 periph = cam_extend_get(daperiphs, unit);
747 if (periph == NULL) {
751 softc = (struct da_softc *)periph->softc;
754 * check it's not too big a transfer for our adapter
756 scsi_minphys(bp,&sd_switch);
760 * Mask interrupts so that the pack cannot be invalidated until
761 * after we are in the queue. Otherwise, we might not properly
762 * clean up one of the buffers.
767 * If the device has been made invalid, error out
769 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
776 * Place it in the queue of disk activities for this disk
778 bufqdisksort(&softc->buf_queue, bp);
783 * Schedule ourselves for performing the work.
785 xpt_schedule(periph, /* XXX priority */1);
789 bp->b_flags |= B_ERROR;
792 * Correctly set the buf to indicate a completed xfer
794 bp->b_resid = bp->b_bcount;
799 /* For 2.2-stable support */
805 daioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
807 struct cam_periph *periph;
808 struct da_softc *softc;
813 periph = cam_extend_get(daperiphs, unit);
817 softc = (struct da_softc *)periph->softc;
819 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("daioctl\n"));
821 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
822 return (error); /* error code from tsleep */
825 error = cam_periph_ioctl(periph, cmd, addr, daerror);
827 cam_periph_unlock(periph);
833 dadump(dev_t dev, u_int num, u_int blknum, u_int secsize)
835 struct cam_periph *periph;
836 struct da_softc *softc;
840 struct ccb_scsiio csio;
841 int dumppages = MAXDUMPPGS;
844 /* toss any characters present prior to dump */
845 while (cncheckc() != -1)
849 periph = cam_extend_get(daperiphs, unit);
850 if (periph == NULL) {
853 softc = (struct da_softc *)periph->softc;
855 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0)
858 addr = 0; /* starting address */
859 blkcnt = howmany(PAGE_SIZE, secsize);
864 if ((num / blkcnt) < dumppages)
865 dumppages = num / blkcnt;
867 for (i = 0; i < dumppages; ++i) {
868 vm_paddr_t a = addr + (i * PAGE_SIZE);
869 if (is_physical_memory(a))
870 va = pmap_kenter_temporary(trunc_page(a), i);
872 va = pmap_kenter_temporary(trunc_page(0), i);
875 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
876 csio.ccb_h.ccb_state = DA_CCB_DUMP;
877 scsi_read_write(&csio,
883 /*minimum_cmd_size*/ softc->minimum_cmd_size,
886 /*data_ptr*/(u_int8_t *) va,
887 /*dxfer_len*/blkcnt * secsize * dumppages,
888 /*sense_len*/SSD_FULL_SIZE,
889 DA_DEFAULT_TIMEOUT * 1000);
890 xpt_polled_action((union ccb *)&csio);
892 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
893 printf("Aborting dump due to I/O error.\n");
894 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
895 CAM_SCSI_STATUS_ERROR)
896 scsi_sense_print(&csio);
898 printf("status == 0x%x, scsi status == 0x%x\n",
899 csio.ccb_h.status, csio.scsi_status);
903 if (dumpstatus(addr, (off_t)num * softc->params.secsize) < 0)
906 /* update block count */
907 num -= blkcnt * dumppages;
908 blknum += blkcnt * dumppages;
909 addr += PAGE_SIZE * dumppages;
913 * Sync the disk cache contents to the physical media.
915 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
917 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
918 csio.ccb_h.ccb_state = DA_CCB_DUMP;
919 scsi_synchronize_cache(&csio,
923 /*begin_lba*/0,/* Cover the whole disk */
927 xpt_polled_action((union ccb *)&csio);
929 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
930 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
931 CAM_SCSI_STATUS_ERROR) {
933 int sense_key, error_code;
935 scsi_extract_sense(&csio.sense_data,
939 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
940 scsi_sense_print(&csio);
942 xpt_print_path(periph->path);
943 printf("Synchronize cache failed, status "
944 "== 0x%x, scsi status == 0x%x\n",
945 csio.ccb_h.status, csio.scsi_status);
956 struct cam_path *path;
959 * Create our extend array for storing the devices we attach to.
961 daperiphs = cam_extend_new();
962 SLIST_INIT(&softc_list);
963 if (daperiphs == NULL) {
964 printf("da: Failed to alloc extend array!\n");
968 callout_init(&dasendorderedtag_ch);
971 * Install a global async callback. This callback will
972 * receive async callbacks like "new device found".
974 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
975 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
977 if (status == CAM_REQ_CMP) {
978 struct ccb_setasync csa;
980 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
981 csa.ccb_h.func_code = XPT_SASYNC_CB;
982 csa.event_enable = AC_FOUND_DEVICE;
983 csa.callback = daasync;
984 csa.callback_arg = NULL;
985 xpt_action((union ccb *)&csa);
986 status = csa.ccb_h.status;
990 if (status != CAM_REQ_CMP) {
991 printf("da: Failed to attach master async callback "
992 "due to status 0x%x!\n", status);
996 * Schedule a periodic event to occasionally send an
997 * ordered tag to a device.
999 callout_reset(&dasendorderedtag_ch,
1000 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
1001 dasendorderedtag, NULL);
1003 /* Register our shutdown event handler */
1004 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1005 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1006 printf("dainit: shutdown event registration failed!\n");
1011 daoninvalidate(struct cam_periph *periph)
1013 struct da_softc *softc;
1015 struct ccb_setasync csa;
1017 softc = (struct da_softc *)periph->softc;
1020 * De-register any async callbacks.
1022 xpt_setup_ccb(&csa.ccb_h, periph->path,
1024 csa.ccb_h.func_code = XPT_SASYNC_CB;
1025 csa.event_enable = 0;
1026 csa.callback = daasync;
1027 csa.callback_arg = periph;
1028 xpt_action((union ccb *)&csa);
1030 softc->flags |= DA_FLAG_PACK_INVALID;
1033 * Use a critical section to keep the buffer queue from being
1034 * modified while we traverse it.
1039 * Return all queued I/O with ENXIO.
1040 * XXX Handle any transactions queued to the card
1041 * with XPT_ABORT_CCB.
1043 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
1044 bufq_remove(&softc->buf_queue, q_bp);
1045 q_bp->b_resid = q_bp->b_bcount;
1046 q_bp->b_error = ENXIO;
1047 q_bp->b_flags |= B_ERROR;
1052 SLIST_REMOVE(&softc_list, softc, da_softc, links);
1054 xpt_print_path(periph->path);
1055 printf("lost device\n");
1059 dacleanup(struct cam_periph *periph)
1061 struct da_softc *softc;
1063 softc = (struct da_softc *)periph->softc;
1065 devstat_remove_entry(&softc->device_stats);
1066 cam_extend_release(daperiphs, periph->unit_number);
1067 xpt_print_path(periph->path);
1068 printf("removing device entry\n");
1070 * If we can't free the sysctl tree, oh well...
1072 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1073 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1074 xpt_print_path(periph->path);
1075 printf("can't remove sysctl context\n");
1077 if (softc->disk.d_rawdev) {
1078 disk_destroy(&softc->disk);
1080 free(softc, M_DEVBUF);
1084 daasync(void *callback_arg, u_int32_t code,
1085 struct cam_path *path, void *arg)
1087 struct cam_periph *periph;
1089 periph = (struct cam_periph *)callback_arg;
1091 case AC_FOUND_DEVICE:
1093 struct ccb_getdev *cgd;
1096 cgd = (struct ccb_getdev *)arg;
1098 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1099 && SID_TYPE(&cgd->inq_data) != T_RBC
1100 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1104 * Allocate a peripheral instance for
1105 * this device and start the probe
1108 status = cam_periph_alloc(daregister, daoninvalidate,
1110 "da", CAM_PERIPH_BIO,
1111 cgd->ccb_h.path, daasync,
1112 AC_FOUND_DEVICE, cgd);
1114 if (status != CAM_REQ_CMP
1115 && status != CAM_REQ_INPROG)
1116 printf("daasync: Unable to attach to new device "
1117 "due to status 0x%x\n", status);
1123 struct da_softc *softc;
1124 struct ccb_hdr *ccbh;
1126 softc = (struct da_softc *)periph->softc;
1129 * Don't fail on the expected unit attention
1132 softc->flags |= DA_FLAG_RETRY_UA;
1133 for (ccbh = LIST_FIRST(&softc->pending_ccbs);
1134 ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le))
1135 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1140 cam_periph_async(periph, code, path, arg);
1146 dasysctlinit(void *context, int pending)
1148 struct cam_periph *periph;
1149 struct da_softc *softc;
1150 char tmpstr[80], tmpstr2[80];
1152 periph = (struct cam_periph *)context;
1153 softc = (struct da_softc *)periph->softc;
1155 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1156 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1158 sysctl_ctx_init(&softc->sysctl_ctx);
1159 softc->flags |= DA_FLAG_SCTX_INIT;
1160 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1161 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1162 CTLFLAG_RD, 0, tmpstr);
1163 if (softc->sysctl_tree == NULL) {
1164 printf("dasysctlinit: unable to allocate sysctl tree\n");
1169 * Now register the sysctl handler, so the user can the value on
1172 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1173 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1174 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1175 "Minimum CDB size");
1179 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1183 value = *(int *)arg1;
1185 error = sysctl_handle_int(oidp, &value, 0, req);
1188 || (req->newptr == NULL))
1192 * Acceptable values here are 6, 10 or 12. It's possible we may
1193 * support a 16 byte minimum command size in the future, since
1194 * there are now READ(16) and WRITE(16) commands defined in the
1199 else if ((value > 6)
1202 else if (value > 10)
1205 *(int *)arg1 = value;
1211 daregister(struct cam_periph *periph, void *arg)
1213 struct da_softc *softc;
1214 struct ccb_setasync csa;
1215 struct ccb_pathinq cpi;
1216 struct ccb_getdev *cgd;
1220 cgd = (struct ccb_getdev *)arg;
1221 if (periph == NULL) {
1222 printf("daregister: periph was NULL!!\n");
1223 return(CAM_REQ_CMP_ERR);
1227 printf("daregister: no getdev CCB, can't register device\n");
1228 return(CAM_REQ_CMP_ERR);
1231 softc = malloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
1232 LIST_INIT(&softc->pending_ccbs);
1233 softc->state = DA_STATE_PROBE;
1234 bufq_init(&softc->buf_queue);
1235 if (SID_IS_REMOVABLE(&cgd->inq_data))
1236 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1237 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1238 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1240 periph->softc = softc;
1242 cam_extend_set(daperiphs, periph->unit_number, periph);
1245 * See if this device has any quirks.
1247 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1248 (caddr_t)da_quirk_table,
1249 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1250 sizeof(*da_quirk_table), scsi_inquiry_match);
1253 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1255 softc->quirks = DA_Q_NONE;
1257 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1259 /* Check if the SIM does not want 6 byte commands */
1260 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
1261 cpi.ccb_h.func_code = XPT_PATH_INQ;
1262 xpt_action((union ccb *)&cpi);
1263 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1264 softc->quirks |= DA_Q_NO_6_BYTE;
1267 * RBC devices don't have to support READ(6), only READ(10).
1269 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1270 softc->minimum_cmd_size = 10;
1272 softc->minimum_cmd_size = 6;
1275 * Load the user's default, if any.
1277 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1278 periph->unit_number);
1279 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1282 * 6, 10 and 12 are the currently permissible values.
1284 if (softc->minimum_cmd_size < 6)
1285 softc->minimum_cmd_size = 6;
1286 else if ((softc->minimum_cmd_size > 6)
1287 && (softc->minimum_cmd_size <= 10))
1288 softc->minimum_cmd_size = 10;
1289 else if (softc->minimum_cmd_size > 12)
1290 softc->minimum_cmd_size = 12;
1293 * Block our timeout handler while we
1294 * add this softc to the dev list.
1297 SLIST_INSERT_HEAD(&softc_list, softc, links);
1301 * The DA driver supports a blocksize, but
1302 * we don't know the blocksize until we do
1303 * a read capacity. So, set a flag to
1304 * indicate that the blocksize is
1305 * unavailable right now. We'll clear the
1306 * flag as soon as we've done a read capacity.
1308 devstat_add_entry(&softc->device_stats, "da",
1309 periph->unit_number, 0,
1310 DEVSTAT_BS_UNAVAILABLE,
1311 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
1312 DEVSTAT_PRIORITY_DISK);
1315 * Register this media as a disk
1317 disk_create(periph->unit_number, &softc->disk, 0, &da_cdevsw);
1320 * Add async callbacks for bus reset and
1321 * bus device reset calls. I don't bother
1322 * checking if this fails as, in most cases,
1323 * the system will function just fine without
1324 * them and the only alternative would be to
1325 * not attach the device on failure.
1327 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
1328 csa.ccb_h.func_code = XPT_SASYNC_CB;
1329 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
1330 csa.callback = daasync;
1331 csa.callback_arg = periph;
1332 xpt_action((union ccb *)&csa);
1334 * Lock this peripheral until we are setup.
1335 * This first call can't block
1337 (void)cam_periph_lock(periph, 0);
1338 xpt_schedule(periph, /*priority*/5);
1340 return(CAM_REQ_CMP);
1344 dastart(struct cam_periph *periph, union ccb *start_ccb)
1346 struct da_softc *softc;
1348 softc = (struct da_softc *)periph->softc;
1351 switch (softc->state) {
1352 case DA_STATE_NORMAL:
1354 /* Pull a buffer from the queue and get going on it */
1358 * See if there is a buf with work for us to do..
1361 bp = bufq_first(&softc->buf_queue);
1362 if (periph->immediate_priority <= periph->pinfo.priority) {
1363 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1364 ("queuing for immediate ccb\n"));
1365 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1366 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1368 periph->immediate_priority = CAM_PRIORITY_NONE;
1370 wakeup(&periph->ccb_list);
1371 } else if (bp == NULL) {
1373 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.
1408 LIST_INSERT_HEAD(&softc->pending_ccbs,
1409 &start_ccb->ccb_h, periph_links.le);
1411 /* We expect a unit attention from this device */
1412 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1413 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1414 softc->flags &= ~DA_FLAG_RETRY_UA;
1417 start_ccb->ccb_h.ccb_bp = bp;
1418 bp = bufq_first(&softc->buf_queue);
1421 xpt_action(start_ccb);
1425 /* Have more work to do, so ensure we stay scheduled */
1426 xpt_schedule(periph, /* XXX priority */1);
1430 case DA_STATE_PROBE:
1432 struct ccb_scsiio *csio;
1433 struct scsi_read_capacity_data *rcap;
1435 rcap = malloc(sizeof(*rcap), M_TEMP, M_INTWAIT | M_ZERO);
1436 csio = &start_ccb->csio;
1437 scsi_read_capacity(csio,
1444 start_ccb->ccb_h.ccb_bp = NULL;
1445 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1446 xpt_action(start_ccb);
1453 cmd6workaround(union ccb *ccb)
1455 struct scsi_rw_6 cmd6;
1456 struct scsi_rw_10 *cmd10;
1457 struct da_softc *softc;
1461 cdb = ccb->csio.cdb_io.cdb_bytes;
1463 /* Translation only possible if CDB is an array and cmd is R/W6 */
1464 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1465 (*cdb != READ_6 && *cdb != WRITE_6))
1468 xpt_print_path(ccb->ccb_h.path);
1469 printf("READ(6)/WRITE(6) not supported, "
1470 "increasing minimum_cmd_size to 10.\n");
1471 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1472 softc->minimum_cmd_size = 10;
1474 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1475 cmd10 = (struct scsi_rw_10 *)cdb;
1476 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1478 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1479 cmd10->reserved = 0;
1480 scsi_ulto2b(cmd6.length, cmd10->length);
1481 cmd10->control = cmd6.control;
1482 ccb->csio.cdb_len = sizeof(*cmd10);
1484 /* Requeue request, unfreezing queue if necessary */
1485 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1486 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1489 cam_release_devq(ccb->ccb_h.path,
1493 /*getcount_only*/0);
1499 dadone(struct cam_periph *periph, union ccb *done_ccb)
1501 struct da_softc *softc;
1502 struct ccb_scsiio *csio;
1504 softc = (struct da_softc *)periph->softc;
1505 csio = &done_ccb->csio;
1506 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1507 case DA_CCB_BUFFER_IO:
1511 bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
1512 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1516 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1521 /* Retry selection timeouts */
1522 sf |= SF_RETRY_SELTO;
1524 if ((error = daerror(done_ccb, 0, sf)) == ERESTART) {
1526 * A retry was scheuled, so
1536 if (error == ENXIO) {
1538 * Catastrophic error. Mark our pack as
1541 /* XXX See if this is really a media
1544 xpt_print_path(periph->path);
1545 printf("Invalidating pack\n");
1546 softc->flags |= DA_FLAG_PACK_INVALID;
1550 * return all queued I/O with EIO, so that
1551 * the client can retry these I/Os in the
1552 * proper order should it attempt to recover.
1554 while ((q_bp = bufq_first(&softc->buf_queue))
1556 bufq_remove(&softc->buf_queue, q_bp);
1557 q_bp->b_resid = q_bp->b_bcount;
1558 q_bp->b_error = EIO;
1559 q_bp->b_flags |= B_ERROR;
1563 bp->b_error = error;
1564 bp->b_resid = bp->b_bcount;
1565 bp->b_flags |= B_ERROR;
1567 bp->b_resid = csio->resid;
1569 if (bp->b_resid != 0)
1570 bp->b_flags |= B_ERROR;
1572 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1573 cam_release_devq(done_ccb->ccb_h.path,
1577 /*getcount_only*/0);
1579 bp->b_resid = csio->resid;
1580 if (csio->resid > 0)
1581 bp->b_flags |= B_ERROR;
1585 * Block out any asyncronous callbacks
1586 * while we touch the pending ccb list.
1589 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1592 if (softc->device_stats.busy_count == 0)
1593 softc->flags |= DA_FLAG_WENT_IDLE;
1595 devstat_end_transaction_buf(&softc->device_stats, bp);
1601 struct scsi_read_capacity_data *rdcap;
1602 char announce_buf[80];
1604 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1606 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1607 struct disk_params *dp;
1609 dasetgeom(periph, rdcap);
1610 dp = &softc->params;
1611 snprintf(announce_buf, sizeof(announce_buf),
1612 "%luMB (%u %u byte sectors: %dH %dS/T %dC)",
1613 (unsigned long) (((u_int64_t)dp->secsize *
1614 dp->sectors) / (1024*1024)), dp->sectors,
1615 dp->secsize, dp->heads, dp->secs_per_track,
1620 announce_buf[0] = '\0';
1623 * Retry any UNIT ATTENTION type errors. They
1624 * are expected at boot.
1626 error = daerror(done_ccb, 0, SF_RETRY_UA |
1627 SF_RETRY_SELTO | SF_NO_PRINT);
1628 if (error == ERESTART) {
1630 * A retry was scheuled, so
1634 } else if (error != 0) {
1635 struct scsi_sense_data *sense;
1637 int sense_key, error_code;
1640 struct ccb_getdev cgd;
1642 /* Don't wedge this device's queue */
1643 cam_release_devq(done_ccb->ccb_h.path,
1647 /*getcount_only*/0);
1649 status = done_ccb->ccb_h.status;
1651 xpt_setup_ccb(&cgd.ccb_h,
1652 done_ccb->ccb_h.path,
1654 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1655 xpt_action((union ccb *)&cgd);
1657 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1658 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1659 || ((status & CAM_AUTOSNS_VALID) == 0))
1665 sense = &csio->sense_data;
1666 scsi_extract_sense(sense, &error_code,
1671 * Attach to anything that claims to be a
1672 * direct access or optical disk device,
1673 * as long as it doesn't return a "Logical
1674 * unit not supported" (0x25) error.
1676 if ((have_sense) && (asc != 0x25)
1677 && (error_code == SSD_CURRENT_ERROR))
1678 snprintf(announce_buf,
1679 sizeof(announce_buf),
1680 "Attempt to query device "
1681 "size failed: %s, %s",
1682 scsi_sense_key_text[sense_key],
1683 scsi_sense_desc(asc,ascq,
1690 xpt_print_path(periph->path);
1691 printf("got CAM status %#x\n",
1692 done_ccb->ccb_h.status);
1695 xpt_print_path(periph->path);
1696 printf("fatal error, failed"
1697 " to attach to device\n");
1700 * Free up resources.
1702 cam_periph_invalidate(periph);
1706 free(rdcap, M_TEMP);
1707 if (announce_buf[0] != '\0') {
1708 xpt_announce_periph(periph, announce_buf);
1710 * Create our sysctl variables, now that we know
1711 * we have successfully attached.
1713 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1715 softc->state = DA_STATE_NORMAL;
1717 * Since our peripheral may be invalidated by an error
1718 * above or an external event, we must release our CCB
1719 * before releasing the probe lock on the peripheral.
1720 * The peripheral will only go away once the last lock
1721 * is removed, and we need it around for the CCB release
1724 xpt_release_ccb(done_ccb);
1725 cam_periph_unlock(periph);
1728 case DA_CCB_WAITING:
1730 /* Caller will release the CCB */
1731 wakeup(&done_ccb->ccb_h.cbfcnp);
1735 /* No-op. We're polling */
1740 xpt_release_ccb(done_ccb);
1744 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1746 struct da_softc *softc;
1747 struct cam_periph *periph;
1750 periph = xpt_path_periph(ccb->ccb_h.path);
1751 softc = (struct da_softc *)periph->softc;
1754 * Automatically detect devices that do not support
1755 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
1758 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
1759 error = cmd6workaround(ccb);
1760 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
1761 CAM_SCSI_STATUS_ERROR)
1762 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
1763 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1764 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
1765 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
1766 int sense_key, error_code, asc, ascq;
1768 scsi_extract_sense(&ccb->csio.sense_data,
1769 &error_code, &sense_key, &asc, &ascq);
1770 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
1771 error = cmd6workaround(ccb);
1773 if (error == ERESTART)
1778 * Until we have a better way of doing pack validation,
1779 * don't treat UAs as errors.
1781 sense_flags |= SF_RETRY_UA;
1782 return(cam_periph_error(ccb, cam_flags, sense_flags,
1783 &softc->saved_ccb));
1787 daprevent(struct cam_periph *periph, int action)
1789 struct da_softc *softc;
1793 softc = (struct da_softc *)periph->softc;
1795 if (((action == PR_ALLOW)
1796 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
1797 || ((action == PR_PREVENT)
1798 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
1802 ccb = cam_periph_getccb(periph, /*priority*/1);
1804 scsi_prevent(&ccb->csio,
1812 error = cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
1813 /*sense_flags*/0, &softc->device_stats);
1816 if (action == PR_ALLOW)
1817 softc->flags &= ~DA_FLAG_PACK_LOCKED;
1819 softc->flags |= DA_FLAG_PACK_LOCKED;
1822 xpt_release_ccb(ccb);
1826 dasetgeom(struct cam_periph *periph, struct scsi_read_capacity_data * rdcap)
1828 struct ccb_calc_geometry ccg;
1829 struct da_softc *softc;
1830 struct disk_params *dp;
1832 softc = (struct da_softc *)periph->softc;
1834 dp = &softc->params;
1835 dp->secsize = scsi_4btoul(rdcap->length);
1836 dp->sectors = scsi_4btoul(rdcap->addr) + 1;
1838 * Have the controller provide us with a geometry
1839 * for this disk. The only time the geometry
1840 * matters is when we boot and the controller
1841 * is the only one knowledgeable enough to come
1842 * up with something that will make this a bootable
1845 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
1846 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
1847 ccg.block_size = dp->secsize;
1848 ccg.volume_size = dp->sectors;
1850 ccg.secs_per_track = 0;
1852 xpt_action((union ccb*)&ccg);
1853 dp->heads = ccg.heads;
1854 dp->secs_per_track = ccg.secs_per_track;
1855 dp->cylinders = ccg.cylinders;
1859 dasendorderedtag(void *arg)
1861 struct da_softc *softc;
1863 for (softc = SLIST_FIRST(&softc_list);
1865 softc = SLIST_NEXT(softc, links)) {
1867 if ((softc->ordered_tag_count == 0)
1868 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
1869 softc->flags |= DA_FLAG_NEED_OTAG;
1871 if (softc->device_stats.busy_count > 0)
1872 softc->flags &= ~DA_FLAG_WENT_IDLE;
1874 softc->ordered_tag_count = 0;
1877 /* Queue us up again */
1878 callout_reset(&dasendorderedtag_ch,
1879 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
1880 dasendorderedtag, NULL);
1884 * Step through all DA peripheral drivers, and if the device is still open,
1885 * sync the disk cache to physical media.
1888 dashutdown(void * arg, int howto)
1890 struct cam_periph *periph;
1891 struct da_softc *softc;
1893 for (periph = TAILQ_FIRST(&dadriver.units); periph != NULL;
1894 periph = TAILQ_NEXT(periph, unit_links)) {
1896 softc = (struct da_softc *)periph->softc;
1899 * We only sync the cache if the drive is still open, and
1900 * if the drive is capable of it..
1902 if (((softc->flags & DA_FLAG_OPEN) == 0)
1903 || (softc->quirks & DA_Q_NO_SYNC_CACHE))
1906 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
1908 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
1909 scsi_synchronize_cache(&ccb.csio,
1913 /*begin_lba*/0, /* whole disk */
1918 xpt_polled_action(&ccb);
1920 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1921 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
1922 CAM_SCSI_STATUS_ERROR)
1923 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
1924 int error_code, sense_key, asc, ascq;
1926 scsi_extract_sense(&ccb.csio.sense_data,
1927 &error_code, &sense_key,
1930 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1931 scsi_sense_print(&ccb.csio);
1933 xpt_print_path(periph->path);
1934 printf("Synchronize cache failed, status "
1935 "== 0x%x, scsi status == 0x%x\n",
1936 ccb.ccb_h.status, ccb.csio.scsi_status);
1940 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1941 cam_release_devq(ccb.ccb_h.path,
1945 /*getcount_only*/0);
1950 #else /* !_KERNEL */
1953 * XXX This is only left out of the kernel build to silence warnings. If,
1954 * for some reason this function is used in the kernel, the ifdefs should
1955 * be moved so it is included both in the kernel and userland.
1958 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
1959 void (*cbfcnp)(struct cam_periph *, union ccb *),
1960 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
1961 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
1964 struct scsi_format_unit *scsi_cmd;
1966 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
1967 scsi_cmd->opcode = FORMAT_UNIT;
1968 scsi_cmd->byte2 = byte2;
1969 scsi_ulto2b(ileave, scsi_cmd->interleave);
1974 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
1983 #endif /* _KERNEL */