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.48 2007/11/25 04:42:38 pavalos 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/dtype.h>
52 #include <sys/eventhandler.h>
53 #include <sys/malloc.h>
59 #include <sys/thread2.h>
62 #include <machine/md_var.h>
75 #include "../cam_ccb.h"
76 #include "../cam_extend.h"
77 #include "../cam_periph.h"
78 #include "../cam_xpt_periph.h"
80 #include "scsi_message.h"
94 DA_FLAG_PACK_INVALID = 0x001,
95 DA_FLAG_NEW_PACK = 0x002,
96 DA_FLAG_PACK_LOCKED = 0x004,
97 DA_FLAG_PACK_REMOVABLE = 0x008,
98 DA_FLAG_TAGGED_QUEUING = 0x010,
99 DA_FLAG_NEED_OTAG = 0x020,
100 DA_FLAG_WENT_IDLE = 0x040,
101 DA_FLAG_RETRY_UA = 0x080,
102 DA_FLAG_OPEN = 0x100,
103 DA_FLAG_SCTX_INIT = 0x200
108 DA_Q_NO_SYNC_CACHE = 0x01,
109 DA_Q_NO_6_BYTE = 0x02,
110 DA_Q_NO_PREVENT = 0x04
115 DA_CCB_PROBE2 = 0x02,
116 DA_CCB_BUFFER_IO = 0x03,
117 DA_CCB_WAITING = 0x04,
119 DA_CCB_TYPE_MASK = 0x0F,
120 DA_CCB_RETRY_UA = 0x10
123 /* Offsets into our private area for storing information */
124 #define ccb_state ppriv_field0
125 #define ccb_bio ppriv_ptr1
130 u_int8_t secs_per_track;
131 u_int32_t secsize; /* Number of bytes/sector */
132 u_int64_t sectors; /* total number sectors */
136 struct bio_queue_head bio_queue;
137 struct devstat device_stats;
138 SLIST_ENTRY(da_softc) links;
139 LIST_HEAD(, ccb_hdr) pending_ccbs;
143 int minimum_cmd_size;
144 int ordered_tag_count;
145 int outstanding_cmds;
146 struct disk_params params;
149 struct task sysctl_task;
150 struct sysctl_ctx_list sysctl_ctx;
151 struct sysctl_oid *sysctl_tree;
154 struct da_quirk_entry {
155 struct scsi_inquiry_pattern inq_pat;
159 static const char quantum[] = "QUANTUM";
160 static const char microp[] = "MICROP";
162 static struct da_quirk_entry da_quirk_table[] =
164 /* SPI, FC devices */
167 * Fujitsu M2513A MO drives.
168 * Tested devices: M2513A2 firmware versions 1200 & 1300.
169 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
170 * Reported by: W.Scholten <whs@xs4all.nl>
172 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
173 /*quirks*/ DA_Q_NO_SYNC_CACHE
177 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
178 /*quirks*/ DA_Q_NO_SYNC_CACHE
182 * This particular Fujitsu drive doesn't like the
183 * synchronize cache command.
184 * Reported by: Tom Jackson <toj@gorilla.net>
186 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
187 /*quirks*/ DA_Q_NO_SYNC_CACHE
192 * This drive doesn't like the synchronize cache command
193 * either. Reported by: Matthew Jacob <mjacob@feral.com>
194 * in NetBSD PR kern/6027, August 24, 1998.
196 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
197 /*quirks*/ DA_Q_NO_SYNC_CACHE
201 * This drive doesn't like the synchronize cache command
202 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
205 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
206 /*quirks*/ DA_Q_NO_SYNC_CACHE
210 * Doesn't like the synchronize cache command.
211 * Reported by: Blaz Zupan <blaz@gold.amis.net>
213 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
214 /*quirks*/ DA_Q_NO_SYNC_CACHE
218 * Doesn't like the synchronize cache command.
220 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
221 /*quirks*/ DA_Q_NO_SYNC_CACHE
225 * Doesn't like the synchronize cache command.
227 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
228 /*quirks*/ DA_Q_NO_SYNC_CACHE
232 * Doesn't work correctly with 6 byte reads/writes.
233 * Returns illegal request, and points to byte 9 of the
235 * Reported by: Adam McDougall <bsdx@spawnet.com>
237 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
238 /*quirks*/ DA_Q_NO_6_BYTE
242 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
243 /*quirks*/ DA_Q_NO_6_BYTE
245 /* XXX USB floppy quirks temporarily enabled for 4.9R */
246 /* USB floppy devices supported by umass(4) */
249 * This USB floppy drive uses the UFI command set. This
250 * command set is a derivative of the ATAPI command set and
251 * does not support READ_6 commands only READ_10. It also does
252 * not support sync cache (0x35).
254 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Y-E DATA", "USB-FDU", "*"},
255 /*quirks*/ DA_Q_NO_SYNC_CACHE
258 /* Another USB floppy */
259 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MATSHITA", "FDD CF-VFDU*","*"},
260 /*quirks*/ DA_Q_NO_SYNC_CACHE
264 * The vendor, product and version strings coming from the
265 * controller are null terminated instead of being padded with
266 * spaces. The trailing wildcard character '*' is required.
268 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SMSC*", "USB FDC*","*"},
269 /*quirks*/ DA_Q_NO_SYNC_CACHE
273 * SmartDisk (Mitsumi) USB floppy drive
276 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MITSUMI", "USB FDD", "*"},
277 /*quirks*/ DA_Q_NO_SYNC_CACHE
281 * The CISS RAID controllers do not support SYNC_CACHE
283 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
284 /*quirks*/ DA_Q_NO_SYNC_CACHE
287 /* USB mass storage devices supported by umass(4) */
290 * Sony Memory Stick adapter MSAC-US1 and
291 * Sony PCG-C1VJ Internal Memory Stick Slot (MSC-U01).
292 * Make all sony MS* products use this quirk.
294 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "MS*", "*"},
295 /*quirks*/ DA_Q_NO_SYNC_CACHE
299 * Sony Memory Stick adapter for the CLIE series
302 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "CLIE*", "*"},
303 /*quirks*/ DA_Q_NO_SYNC_CACHE
307 * Intelligent Stick USB disk-on-key
310 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB Card",
311 "IntelligentStick*", "*"},
312 /*quirks*/ DA_Q_NO_SYNC_CACHE
316 * Sony DSC cameras (DSC-S30, DSC-S50, DSC-S70)
318 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
319 /*quirks*/ DA_Q_NO_SYNC_CACHE
323 * Microtech USB CameraMate
325 {T_DIRECT, SIP_MEDIA_REMOVABLE, "eUSB Compact*",
326 "Compact Flash*", "*"},
327 /*quirks*/ DA_Q_NO_SYNC_CACHE
331 * Olympus digital cameras (C-3040ZOOM, C-2040ZOOM, C-1)
333 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "C-*", "*"},
334 /*quirks*/ DA_Q_NO_SYNC_CACHE
338 * Olympus digital cameras (E-100RS, E-10).
340 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "E-*", "*"},
341 /*quirks*/ DA_Q_NO_SYNC_CACHE
345 * KingByte Pen Drives
347 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NO BRAND", "PEN DRIVE", "*"},
348 /*quirks*/ DA_Q_NO_SYNC_CACHE
352 * iRiver iFP MP3 player (with UMS Firmware)
353 * PR: kern/54881, i386/63941, kern/66124
355 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
356 /*quirks*/ DA_Q_NO_SYNC_CACHE
360 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
363 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "NexIA+*", "*"},
364 /*quirks*/ DA_Q_NO_SYNC_CACHE
370 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJIFILMUSB-DRIVEUNIT",
371 "USB-DRIVEUNIT", "*"},
372 /*quirks*/ DA_Q_NO_SYNC_CACHE
376 * Minolta Dimage E203
378 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MINOLTA", "DiMAGE E203", "*"},
379 /*quirks*/ DA_Q_NO_SYNC_CACHE
386 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Apacer", "HandyDrive", "*"},
387 /*quirks*/ DA_Q_NO_SYNC_CACHE
391 * Daisy Technology PhotoClip on Zoran chip
394 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ZORAN", "COACH", "*"},
395 /*quirks*/ DA_Q_NO_SYNC_CACHE
399 * Sony USB Key-Storage
402 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Storage Media", "*"},
403 /*quirks*/ DA_Q_NO_SYNC_CACHE
405 #endif /* DA_OLD_QUIRKS */
408 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
411 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
412 /*quirks*/ DA_Q_NO_SYNC_CACHE
416 * Jungsoft NEXDISK USB flash key
419 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
420 /*quirks*/ DA_Q_NO_SYNC_CACHE
424 * Creative Nomad MUVO mp3 player (USB)
427 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
428 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
432 static d_open_t daopen;
433 static d_close_t daclose;
434 static d_strategy_t dastrategy;
435 static d_ioctl_t daioctl;
436 static d_dump_t dadump;
437 static periph_init_t dainit;
438 static void daasync(void *callback_arg, u_int32_t code,
439 struct cam_path *path, void *arg);
440 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
441 static periph_ctor_t daregister;
442 static periph_dtor_t dacleanup;
443 static periph_start_t dastart;
444 static periph_oninv_t daoninvalidate;
445 static void dadone(struct cam_periph *periph,
446 union ccb *done_ccb);
447 static int daerror(union ccb *ccb, u_int32_t cam_flags,
448 u_int32_t sense_flags);
449 static void daprevent(struct cam_periph *periph, int action);
450 static int dagetcapacity(struct cam_periph *periph);
451 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
454 static timeout_t dasendorderedtag;
455 static void dashutdown(void *arg, int howto);
457 #ifndef DA_DEFAULT_TIMEOUT
458 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
461 #ifndef DA_DEFAULT_RETRY
462 #define DA_DEFAULT_RETRY 4
465 static int da_retry_count = DA_DEFAULT_RETRY;
466 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
467 static struct callout dasendorderedtag_ch;
469 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
470 "CAM Direct Access Disk driver");
471 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
472 &da_retry_count, 0, "Normal I/O retry count");
473 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
474 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
475 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
476 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
479 * DA_ORDEREDTAG_INTERVAL determines how often, relative
480 * to the default timeout, we check to see whether an ordered
481 * tagged transaction is appropriate to prevent simple tag
482 * starvation. Since we'd like to ensure that there is at least
483 * 1/2 of the timeout length left for a starved transaction to
484 * complete after we've sent an ordered tag, we must poll at least
485 * four times in every timeout period. This takes care of the worst
486 * case where a starved transaction starts during an interval that
487 * meets the requirement "don't send an ordered tag" test so it takes
488 * us two intervals to determine that a tag must be sent.
490 #ifndef DA_ORDEREDTAG_INTERVAL
491 #define DA_ORDEREDTAG_INTERVAL 4
494 static struct periph_driver dadriver =
497 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
500 PERIPHDRIVER_DECLARE(da, dadriver);
502 /* For 2.2-stable support */
507 static struct dev_ops da_ops = {
508 { "da", DA_CDEV_MAJOR, D_DISK },
512 .d_write = physwrite,
514 .d_strategy = dastrategy,
518 static SLIST_HEAD(,da_softc) softc_list;
519 static struct extend_array *daperiphs;
522 daopen(struct dev_open_args *ap)
524 cdev_t dev = ap->a_head.a_dev;
525 struct cam_periph *periph;
526 struct da_softc *softc;
527 struct disk_info info;
533 periph = cam_extend_get(daperiphs, unit);
534 if (periph == NULL) {
539 softc = (struct da_softc *)periph->softc;
541 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
542 ("daopen: dev=%s (unit %d)\n", devtoname(dev),
545 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
547 return (error); /* error code from tsleep */
550 if ((softc->flags & DA_FLAG_OPEN) == 0) {
551 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
555 softc->flags |= DA_FLAG_OPEN;
558 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
559 /* Invalidate our pack information. */
560 disk_invalidate(&softc->disk);
561 softc->flags &= ~DA_FLAG_PACK_INVALID;
565 error = dagetcapacity(periph);
568 /* Do a read capacity */
570 struct scsi_read_capacity_data *rcap;
573 rcap = kmalloc(sizeof(*rcap), M_TEMP, M_INTWAIT | M_ZERO);
575 ccb = cam_periph_getccb(periph, /*priority*/1);
576 scsi_read_capacity(&ccb->csio,
583 ccb->ccb_h.ccb_bio = NULL;
585 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
586 /*sense_flags*/SF_RETRY_UA |
588 &softc->device_stats);
590 xpt_release_ccb(ccb);
593 dasetgeom(periph, rcap);
601 struct ccb_getdev cgd;
603 /* Build disk information structure */
604 bzero(&info, sizeof(info));
605 info.d_type = DTYPE_SCSI;
608 * Grab the inquiry data to get the vendor and product names.
609 * Put them in the typename and packname for the label.
611 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
612 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
613 xpt_action((union ccb *)&cgd);
616 strncpy(label->d_typename, cgd.inq_data.vendor,
617 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
618 strncpy(label->d_packname, cgd.inq_data.product,
619 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
625 info.d_media_blksize = softc->params.secsize;
626 info.d_media_blocks = softc->params.sectors;
627 info.d_media_size = 0;
632 info.d_secpertrack = softc->params.secs_per_track;
633 info.d_nheads = softc->params.heads;
634 info.d_ncylinders = softc->params.cylinders;
635 info.d_secpercyl = softc->params.heads *
636 softc->params.secs_per_track;
637 disk_setdiskinfo(&softc->disk, &info);
640 * Check to see whether or not the blocksize is set yet.
641 * If it isn't, set it and then clear the blocksize
642 * unavailable flag for the device statistics.
644 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0){
645 softc->device_stats.block_size = softc->params.secsize;
646 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
651 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
652 (softc->quirks & DA_Q_NO_PREVENT) == 0)
653 daprevent(periph, PR_PREVENT);
655 softc->flags &= ~DA_FLAG_OPEN;
656 cam_periph_release(periph);
658 cam_periph_unlock(periph);
663 daclose(struct dev_close_args *ap)
665 cdev_t dev = ap->a_head.a_dev;
666 struct cam_periph *periph;
667 struct da_softc *softc;
672 periph = cam_extend_get(daperiphs, unit);
676 softc = (struct da_softc *)periph->softc;
678 if ((error = cam_periph_lock(periph, 0)) != 0) {
679 return (error); /* error code from tsleep */
682 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
685 ccb = cam_periph_getccb(periph, /*priority*/1);
687 scsi_synchronize_cache(&ccb->csio,
691 /*begin_lba*/0,/* Cover the whole disk */
696 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
697 /*sense_flags*/SF_RETRY_UA,
698 &softc->device_stats);
700 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
701 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
702 CAM_SCSI_STATUS_ERROR) {
704 int sense_key, error_code;
706 scsi_extract_sense(&ccb->csio.sense_data,
710 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
711 scsi_sense_print(&ccb->csio);
713 xpt_print_path(periph->path);
714 kprintf("Synchronize cache failed, status "
715 "== 0x%x, scsi status == 0x%x\n",
716 ccb->csio.ccb_h.status,
717 ccb->csio.scsi_status);
721 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
722 cam_release_devq(ccb->ccb_h.path,
728 xpt_release_ccb(ccb);
732 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
733 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
734 daprevent(periph, PR_ALLOW);
736 * If we've got removeable media, mark the blocksize as
737 * unavailable, since it could change when new media is
740 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
744 * Don't compound any ref counting software bugs with more.
746 if (softc->flags & DA_FLAG_OPEN) {
747 softc->flags &= ~DA_FLAG_OPEN;
748 cam_periph_release(periph);
750 xpt_print_path(periph->path);
751 kprintf("daclose() called on an already closed device!\n");
753 cam_periph_unlock(periph);
758 * Actually translate the requested transfer into one the physical driver
759 * can understand. The transfer is described by a buf and will include
760 * only one physical transfer.
763 dastrategy(struct dev_strategy_args *ap)
765 cdev_t dev = ap->a_head.a_dev;
766 struct bio *bio = ap->a_bio;
767 struct buf *bp = bio->bio_buf;
768 struct cam_periph *periph;
769 struct da_softc *softc;
775 periph = cam_extend_get(daperiphs, unit);
776 if (periph == NULL) {
780 softc = (struct da_softc *)periph->softc;
783 * check it's not too big a transfer for our adapter
785 scsi_minphys(bp, &sd_switch);
789 * Mask interrupts so that the pack cannot be invalidated until
790 * after we are in the queue. Otherwise, we might not properly
791 * clean up one of the buffers.
796 * If the device has been made invalid, error out
798 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
805 * Place it in the queue of disk activities for this disk
807 bioqdisksort(&softc->bio_queue, bio);
812 * Schedule ourselves for performing the work.
814 xpt_schedule(periph, /* XXX priority */1);
818 bp->b_flags |= B_ERROR;
821 * Correctly set the buf to indicate a completed xfer
823 bp->b_resid = bp->b_bcount;
828 /* For 2.2-stable support */
834 daioctl(struct dev_ioctl_args *ap)
836 cdev_t dev = ap->a_head.a_dev;
837 struct cam_periph *periph;
838 struct da_softc *softc;
843 periph = cam_extend_get(daperiphs, unit);
847 softc = (struct da_softc *)periph->softc;
849 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("daioctl\n"));
851 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
852 return (error); /* error code from tsleep */
855 error = cam_periph_ioctl(periph, ap->a_cmd, ap->a_data, daerror);
857 cam_periph_unlock(periph);
863 dadump(struct dev_dump_args *ap)
865 cdev_t dev = ap->a_head.a_dev;
866 struct cam_periph *periph;
867 struct da_softc *softc;
871 struct ccb_scsiio csio;
872 int dumppages = MAXDUMPPGS;
875 /* toss any characters present prior to dump */
876 while (cncheckc() != -1)
880 periph = cam_extend_get(daperiphs, unit);
881 if (periph == NULL) {
884 softc = (struct da_softc *)periph->softc;
886 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0)
889 addr = 0; /* starting address */
890 blkcnt = howmany(PAGE_SIZE, ap->a_secsize);
892 while (ap->a_count > 0) {
895 if ((ap->a_count / blkcnt) < dumppages)
896 dumppages = ap->a_count / blkcnt;
898 for (i = 0; i < dumppages; ++i) {
899 vm_paddr_t a = addr + (i * PAGE_SIZE);
900 if (is_physical_memory(a))
901 va = pmap_kenter_temporary(trunc_page(a), i);
903 va = pmap_kenter_temporary(trunc_page(0), i);
906 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
907 csio.ccb_h.ccb_state = DA_CCB_DUMP;
908 scsi_read_write(&csio,
914 /*minimum_cmd_size*/ softc->minimum_cmd_size,
917 /*data_ptr*/(u_int8_t *) va,
918 /*dxfer_len*/blkcnt * ap->a_secsize * dumppages,
919 /*sense_len*/SSD_FULL_SIZE,
920 DA_DEFAULT_TIMEOUT * 1000);
921 xpt_polled_action((union ccb *)&csio);
923 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
924 kprintf("Aborting dump due to I/O error.\n");
925 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
926 CAM_SCSI_STATUS_ERROR)
927 scsi_sense_print(&csio);
929 kprintf("status == 0x%x, scsi status == 0x%x\n",
930 csio.ccb_h.status, csio.scsi_status);
934 if (dumpstatus(addr, (off_t)ap->a_count * softc->params.secsize) < 0)
937 /* update block count */
938 ap->a_count -= blkcnt * dumppages;
939 ap->a_blkno += blkcnt * dumppages;
940 addr += PAGE_SIZE * dumppages;
944 * Sync the disk cache contents to the physical media.
946 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
948 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
949 csio.ccb_h.ccb_state = DA_CCB_DUMP;
950 scsi_synchronize_cache(&csio,
954 /*begin_lba*/0,/* Cover the whole disk */
958 xpt_polled_action((union ccb *)&csio);
960 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
961 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
962 CAM_SCSI_STATUS_ERROR) {
964 int sense_key, error_code;
966 scsi_extract_sense(&csio.sense_data,
970 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
971 scsi_sense_print(&csio);
973 xpt_print_path(periph->path);
974 kprintf("Synchronize cache failed, status "
975 "== 0x%x, scsi status == 0x%x\n",
976 csio.ccb_h.status, csio.scsi_status);
987 struct cam_path *path;
990 * Create our extend array for storing the devices we attach to.
992 daperiphs = cam_extend_new();
993 SLIST_INIT(&softc_list);
994 if (daperiphs == NULL) {
995 kprintf("da: Failed to alloc extend array!\n");
999 callout_init(&dasendorderedtag_ch);
1002 * Install a global async callback. This callback will
1003 * receive async callbacks like "new device found".
1005 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
1006 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1008 if (status == CAM_REQ_CMP) {
1009 struct ccb_setasync csa;
1011 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
1012 csa.ccb_h.func_code = XPT_SASYNC_CB;
1013 csa.event_enable = AC_FOUND_DEVICE;
1014 csa.callback = daasync;
1015 csa.callback_arg = NULL;
1016 xpt_action((union ccb *)&csa);
1017 status = csa.ccb_h.status;
1018 xpt_free_path(path);
1021 if (status != CAM_REQ_CMP) {
1022 kprintf("da: Failed to attach master async callback "
1023 "due to status 0x%x!\n", status);
1027 * Schedule a periodic event to occasionally send an
1028 * ordered tag to a device.
1030 callout_reset(&dasendorderedtag_ch,
1031 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
1032 dasendorderedtag, NULL);
1034 /* Register our shutdown event handler */
1035 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1036 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1037 kprintf("dainit: shutdown event registration failed!\n");
1042 daoninvalidate(struct cam_periph *periph)
1044 struct da_softc *softc;
1047 struct ccb_setasync csa;
1049 softc = (struct da_softc *)periph->softc;
1052 * De-register any async callbacks.
1054 xpt_setup_ccb(&csa.ccb_h, periph->path,
1056 csa.ccb_h.func_code = XPT_SASYNC_CB;
1057 csa.event_enable = 0;
1058 csa.callback = daasync;
1059 csa.callback_arg = periph;
1060 xpt_action((union ccb *)&csa);
1062 softc->flags |= DA_FLAG_PACK_INVALID;
1065 * Use a critical section to keep the buffer queue from being
1066 * modified while we traverse it.
1071 * Return all queued I/O with ENXIO.
1072 * XXX Handle any transactions queued to the card
1073 * with XPT_ABORT_CCB.
1075 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL){
1076 bioq_remove(&softc->bio_queue, q_bio);
1077 q_bp = q_bio->bio_buf;
1078 q_bp->b_resid = q_bp->b_bcount;
1079 q_bp->b_error = ENXIO;
1080 q_bp->b_flags |= B_ERROR;
1085 SLIST_REMOVE(&softc_list, softc, da_softc, links);
1087 xpt_print_path(periph->path);
1088 kprintf("lost device\n");
1092 dacleanup(struct cam_periph *periph)
1094 struct da_softc *softc;
1096 softc = (struct da_softc *)periph->softc;
1098 devstat_remove_entry(&softc->device_stats);
1099 cam_extend_release(daperiphs, periph->unit_number);
1100 xpt_print_path(periph->path);
1101 kprintf("removing device entry\n");
1103 * If we can't free the sysctl tree, oh well...
1105 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1106 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1107 xpt_print_path(periph->path);
1108 kprintf("can't remove sysctl context\n");
1110 if (softc->disk.d_rawdev) {
1111 disk_destroy(&softc->disk);
1113 kfree(softc, M_DEVBUF);
1117 daasync(void *callback_arg, u_int32_t code,
1118 struct cam_path *path, void *arg)
1120 struct cam_periph *periph;
1122 periph = (struct cam_periph *)callback_arg;
1124 case AC_FOUND_DEVICE:
1126 struct ccb_getdev *cgd;
1129 cgd = (struct ccb_getdev *)arg;
1133 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1134 && SID_TYPE(&cgd->inq_data) != T_RBC
1135 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1139 * Allocate a peripheral instance for
1140 * this device and start the probe
1143 status = cam_periph_alloc(daregister, daoninvalidate,
1145 "da", CAM_PERIPH_BIO,
1146 cgd->ccb_h.path, daasync,
1147 AC_FOUND_DEVICE, cgd);
1149 if (status != CAM_REQ_CMP
1150 && status != CAM_REQ_INPROG)
1151 kprintf("daasync: Unable to attach to new device "
1152 "due to status 0x%x\n", status);
1158 struct da_softc *softc;
1159 struct ccb_hdr *ccbh;
1161 softc = (struct da_softc *)periph->softc;
1164 * Don't fail on the expected unit attention
1167 softc->flags |= DA_FLAG_RETRY_UA;
1168 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1169 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1174 cam_periph_async(periph, code, path, arg);
1180 dasysctlinit(void *context, int pending)
1182 struct cam_periph *periph;
1183 struct da_softc *softc;
1184 char tmpstr[80], tmpstr2[80];
1186 periph = (struct cam_periph *)context;
1187 softc = (struct da_softc *)periph->softc;
1189 ksnprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1190 ksnprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1192 sysctl_ctx_init(&softc->sysctl_ctx);
1193 softc->flags |= DA_FLAG_SCTX_INIT;
1194 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1195 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1196 CTLFLAG_RD, 0, tmpstr);
1197 if (softc->sysctl_tree == NULL) {
1198 kprintf("dasysctlinit: unable to allocate sysctl tree\n");
1203 * Now register the sysctl handler, so the user can the value on
1206 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1207 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1208 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1209 "Minimum CDB size");
1213 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1217 value = *(int *)arg1;
1219 error = sysctl_handle_int(oidp, &value, 0, req);
1222 || (req->newptr == NULL))
1226 * Acceptable values here are 6, 10 or 12, or 16.
1230 else if ((value > 6)
1233 else if ((value > 10)
1236 else if (value > 12)
1239 *(int *)arg1 = value;
1245 daregister(struct cam_periph *periph, void *arg)
1247 struct da_softc *softc;
1248 struct ccb_setasync csa;
1249 struct ccb_pathinq cpi;
1250 struct ccb_getdev *cgd;
1254 cgd = (struct ccb_getdev *)arg;
1255 if (periph == NULL) {
1256 kprintf("daregister: periph was NULL!!\n");
1257 return(CAM_REQ_CMP_ERR);
1261 kprintf("daregister: no getdev CCB, can't register device\n");
1262 return(CAM_REQ_CMP_ERR);
1265 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
1266 LIST_INIT(&softc->pending_ccbs);
1267 softc->state = DA_STATE_PROBE;
1268 bioq_init(&softc->bio_queue);
1269 if (SID_IS_REMOVABLE(&cgd->inq_data))
1270 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1271 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1272 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1274 periph->softc = softc;
1276 cam_extend_set(daperiphs, periph->unit_number, periph);
1279 * See if this device has any quirks.
1281 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1282 (caddr_t)da_quirk_table,
1283 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1284 sizeof(*da_quirk_table), scsi_inquiry_match);
1287 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1289 softc->quirks = DA_Q_NONE;
1291 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1293 /* Check if the SIM does not want 6 byte commands */
1294 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
1295 cpi.ccb_h.func_code = XPT_PATH_INQ;
1296 xpt_action((union ccb *)&cpi);
1297 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1298 softc->quirks |= DA_Q_NO_6_BYTE;
1301 * RBC devices don't have to support READ(6), only READ(10).
1303 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1304 softc->minimum_cmd_size = 10;
1306 softc->minimum_cmd_size = 6;
1309 * Load the user's default, if any.
1311 ksnprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1312 periph->unit_number);
1313 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1316 * 6, 10, 12, and 16 are the currently permissible values.
1318 if (softc->minimum_cmd_size < 6)
1319 softc->minimum_cmd_size = 6;
1320 else if ((softc->minimum_cmd_size > 6)
1321 && (softc->minimum_cmd_size <= 10))
1322 softc->minimum_cmd_size = 10;
1323 else if ((softc->minimum_cmd_size > 10)
1324 && (softc->minimum_cmd_size <= 12))
1325 softc->minimum_cmd_size = 12;
1326 else if (softc->minimum_cmd_size > 12)
1327 softc->minimum_cmd_size = 16;
1330 * Block our timeout handler while we
1331 * add this softc to the dev list.
1334 SLIST_INSERT_HEAD(&softc_list, softc, links);
1338 * The DA driver supports a blocksize, but
1339 * we don't know the blocksize until we do
1340 * a read capacity. So, set a flag to
1341 * indicate that the blocksize is
1342 * unavailable right now. We'll clear the
1343 * flag as soon as we've done a read capacity.
1345 devstat_add_entry(&softc->device_stats, "da",
1346 periph->unit_number, 0,
1347 DEVSTAT_BS_UNAVAILABLE,
1348 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
1349 DEVSTAT_PRIORITY_DISK);
1352 * Register this media as a disk
1354 disk_create(periph->unit_number, &softc->disk, &da_ops);
1355 softc->disk.d_rawdev->si_iosize_max = MAXPHYS;
1358 * Add async callbacks for bus reset and
1359 * bus device reset calls. I don't bother
1360 * checking if this fails as, in most cases,
1361 * the system will function just fine without
1362 * them and the only alternative would be to
1363 * not attach the device on failure.
1365 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
1366 csa.ccb_h.func_code = XPT_SASYNC_CB;
1367 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
1368 csa.callback = daasync;
1369 csa.callback_arg = periph;
1370 xpt_action((union ccb *)&csa);
1372 * Lock this peripheral until we are setup.
1373 * This first call can't block
1375 cam_periph_lock(periph, 0);
1376 xpt_schedule(periph, /*priority*/5);
1378 return(CAM_REQ_CMP);
1382 dastart(struct cam_periph *periph, union ccb *start_ccb)
1384 struct da_softc *softc;
1386 softc = (struct da_softc *)periph->softc;
1389 switch (softc->state) {
1390 case DA_STATE_NORMAL:
1392 /* Pull a buffer from the queue and get going on it */
1397 * See if there is a buf with work for us to do..
1400 bio = bioq_first(&softc->bio_queue);
1401 if (periph->immediate_priority <= periph->pinfo.priority) {
1402 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1403 ("queuing for immediate ccb\n"));
1404 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1405 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1407 periph->immediate_priority = CAM_PRIORITY_NONE;
1409 wakeup(&periph->ccb_list);
1410 } else if (bio == NULL) {
1412 xpt_release_ccb(start_ccb);
1416 bioq_remove(&softc->bio_queue, bio);
1419 devstat_start_transaction(&softc->device_stats);
1421 if ((bp->b_flags & B_ORDERED) != 0
1422 || (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1423 softc->flags &= ~DA_FLAG_NEED_OTAG;
1424 softc->ordered_tag_count++;
1425 tag_code = MSG_ORDERED_Q_TAG;
1427 tag_code = MSG_SIMPLE_Q_TAG;
1430 KKASSERT(bio->bio_offset % softc->params.secsize == 0);
1432 scsi_read_write(&start_ccb->csio,
1433 /*retries*/da_retry_count,
1436 (bp->b_cmd == BUF_CMD_READ),
1438 softc->minimum_cmd_size,
1439 bio->bio_offset / softc->params.secsize,
1440 bp->b_bcount / softc->params.secsize,
1443 /*sense_len*/SSD_FULL_SIZE,
1444 da_default_timeout * 1000);
1445 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1448 * Block out any asyncronous callbacks
1449 * while we touch the pending ccb list.
1451 LIST_INSERT_HEAD(&softc->pending_ccbs,
1452 &start_ccb->ccb_h, periph_links.le);
1454 softc->outstanding_cmds++;
1455 /* We expect a unit attention from this device */
1456 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1457 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1458 softc->flags &= ~DA_FLAG_RETRY_UA;
1461 start_ccb->ccb_h.ccb_bio = bio;
1462 bio = bioq_first(&softc->bio_queue);
1465 xpt_action(start_ccb);
1469 /* Have more work to do, so ensure we stay scheduled */
1470 xpt_schedule(periph, /* XXX priority */1);
1474 case DA_STATE_PROBE:
1476 struct ccb_scsiio *csio;
1477 struct scsi_read_capacity_data *rcap;
1479 rcap = kmalloc(sizeof(*rcap), M_TEMP, M_INTWAIT | M_ZERO);
1480 csio = &start_ccb->csio;
1481 scsi_read_capacity(csio,
1488 start_ccb->ccb_h.ccb_bio = NULL;
1489 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1490 xpt_action(start_ccb);
1493 case DA_STATE_PROBE2:
1495 struct ccb_scsiio *csio;
1496 struct scsi_read_capacity_data_long *rcaplong;
1498 rcaplong = (struct scsi_read_capacity_data_long *)
1499 kmalloc(sizeof(*rcaplong), M_TEMP, M_INTWAIT);
1500 if (rcaplong == NULL) {
1501 kprintf("dastart: Couldn't allocate read_capacity\n");
1502 /* da_free_periph??? */
1505 csio = &start_ccb->csio;
1506 scsi_read_capacity_16(csio,
1509 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1514 /*sense_len*/ SSD_FULL_SIZE,
1516 start_ccb->ccb_h.ccb_bio = NULL;
1517 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1518 xpt_action(start_ccb);
1525 cmd6workaround(union ccb *ccb)
1527 struct scsi_rw_6 cmd6;
1528 struct scsi_rw_10 *cmd10;
1529 struct da_softc *softc;
1533 cdb = ccb->csio.cdb_io.cdb_bytes;
1535 /* Translation only possible if CDB is an array and cmd is R/W6 */
1536 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1537 (*cdb != READ_6 && *cdb != WRITE_6))
1540 xpt_print_path(ccb->ccb_h.path);
1541 kprintf("READ(6)/WRITE(6) not supported, "
1542 "increasing minimum_cmd_size to 10.\n");
1543 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1544 softc->minimum_cmd_size = 10;
1546 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1547 cmd10 = (struct scsi_rw_10 *)cdb;
1548 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1550 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1551 cmd10->reserved = 0;
1552 scsi_ulto2b(cmd6.length, cmd10->length);
1553 cmd10->control = cmd6.control;
1554 ccb->csio.cdb_len = sizeof(*cmd10);
1556 /* Requeue request, unfreezing queue if necessary */
1557 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1558 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1561 cam_release_devq(ccb->ccb_h.path,
1565 /*getcount_only*/0);
1571 dadone(struct cam_periph *periph, union ccb *done_ccb)
1573 struct da_softc *softc;
1574 struct ccb_scsiio *csio;
1576 softc = (struct da_softc *)periph->softc;
1577 csio = &done_ccb->csio;
1578 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1579 case DA_CCB_BUFFER_IO:
1584 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1586 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1590 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1595 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1596 if (error == ERESTART) {
1598 * A retry was scheuled, so
1609 if (error == ENXIO) {
1611 * Catastrophic error. Mark our pack as
1614 /* XXX See if this is really a media
1617 xpt_print_path(periph->path);
1618 kprintf("Invalidating pack\n");
1619 softc->flags |= DA_FLAG_PACK_INVALID;
1623 * return all queued I/O with EIO, so that
1624 * the client can retry these I/Os in the
1625 * proper order should it attempt to recover.
1627 while ((q_bio = bioq_first(&softc->bio_queue))
1629 bioq_remove(&softc->bio_queue, q_bio);
1630 q_bp = q_bio->bio_buf;
1631 q_bp->b_resid = q_bp->b_bcount;
1632 q_bp->b_error = EIO;
1633 q_bp->b_flags |= B_ERROR;
1637 bp->b_error = error;
1638 bp->b_resid = bp->b_bcount;
1639 bp->b_flags |= B_ERROR;
1641 bp->b_resid = csio->resid;
1643 if (bp->b_resid != 0)
1644 bp->b_flags |= B_ERROR;
1646 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1647 cam_release_devq(done_ccb->ccb_h.path,
1651 /*getcount_only*/0);
1653 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1654 panic("REQ_CMP with QFRZN");
1655 bp->b_resid = csio->resid;
1656 if (csio->resid > 0)
1657 bp->b_flags |= B_ERROR;
1661 * Block out any asyncronous callbacks
1662 * while we touch the pending ccb list.
1665 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1666 softc->outstanding_cmds--;
1667 if (softc->outstanding_cmds == 0)
1668 softc->flags |= DA_FLAG_WENT_IDLE;
1671 devstat_end_transaction_buf(&softc->device_stats, bp);
1678 struct scsi_read_capacity_data *rdcap;
1679 struct scsi_read_capacity_data_long *rcaplong;
1680 char announce_buf[80];
1684 if (softc->state == DA_STATE_PROBE)
1685 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1687 rcaplong = (struct scsi_read_capacity_data_long *)
1690 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1691 struct disk_params *dp;
1692 uint32_t block_size;
1695 if (softc->state == DA_STATE_PROBE) {
1696 block_size = scsi_4btoul(rdcap->length);
1697 maxsector = scsi_4btoul(rdcap->addr);
1700 * According to SBC-2, if the standard 10
1701 * byte READ CAPACITY command returns 2^32,
1702 * we should issue the 16 byte version of
1703 * the command, since the device in question
1704 * has more sectors than can be represented
1705 * with the short version of the command.
1707 if (maxsector == 0xffffffff) {
1708 softc->state = DA_STATE_PROBE2;
1709 kfree(rdcap, M_TEMP);
1710 xpt_release_ccb(done_ccb);
1711 xpt_schedule(periph, /*priority*/5);
1715 block_size = scsi_4btoul(rcaplong->length);
1716 maxsector = scsi_8btou64(rcaplong->addr);
1718 dasetgeom(periph, block_size, maxsector);
1719 dp = &softc->params;
1720 ksnprintf(announce_buf, sizeof(announce_buf),
1721 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1722 (uintmax_t) (((uintmax_t)dp->secsize *
1723 dp->sectors) / (1024*1024)),
1724 (uintmax_t)dp->sectors,
1725 dp->secsize, dp->heads, dp->secs_per_track,
1730 announce_buf[0] = '\0';
1733 * Retry any UNIT ATTENTION type errors. They
1734 * are expected at boot.
1736 error = daerror(done_ccb, CAM_RETRY_SELTO,
1737 SF_RETRY_UA|SF_NO_PRINT);
1738 if (error == ERESTART) {
1740 * A retry was scheuled, so
1744 } else if (error != 0) {
1745 struct scsi_sense_data *sense;
1747 int sense_key, error_code;
1750 struct ccb_getdev cgd;
1752 /* Don't wedge this device's queue */
1753 status = done_ccb->ccb_h.status;
1754 if ((status & CAM_DEV_QFRZN) != 0)
1755 cam_release_devq(done_ccb->ccb_h.path,
1759 /*getcount_only*/0);
1762 xpt_setup_ccb(&cgd.ccb_h,
1763 done_ccb->ccb_h.path,
1765 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1766 xpt_action((union ccb *)&cgd);
1768 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1769 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1770 || ((status & CAM_AUTOSNS_VALID) == 0))
1776 sense = &csio->sense_data;
1777 scsi_extract_sense(sense, &error_code,
1782 * Attach to anything that claims to be a
1783 * direct access or optical disk device,
1784 * as long as it doesn't return a "Logical
1785 * unit not supported" (0x25) error.
1787 if ((have_sense) && (asc != 0x25)
1788 && (error_code == SSD_CURRENT_ERROR)) {
1789 const char *sense_key_desc;
1790 const char *asc_desc;
1792 scsi_sense_desc(sense_key, asc, ascq,
1796 ksnprintf(announce_buf,
1797 sizeof(announce_buf),
1798 "Attempt to query device "
1799 "size failed: %s, %s",
1807 xpt_print_path(periph->path);
1808 kprintf("got CAM status %#x\n",
1809 done_ccb->ccb_h.status);
1812 xpt_print_path(periph->path);
1813 kprintf("fatal error, failed"
1814 " to attach to device\n");
1817 * Free up resources.
1819 cam_periph_invalidate(periph);
1823 kfree(csio->data_ptr, M_TEMP);
1824 if (announce_buf[0] != '\0') {
1825 xpt_announce_periph(periph, announce_buf);
1827 * Create our sysctl variables, now that we know
1828 * we have successfully attached.
1830 taskqueue_enqueue(taskqueue_thread[mycpuid],
1831 &softc->sysctl_task);
1833 softc->state = DA_STATE_NORMAL;
1835 * Since our peripheral may be invalidated by an error
1836 * above or an external event, we must release our CCB
1837 * before releasing the probe lock on the peripheral.
1838 * The peripheral will only go away once the last lock
1839 * is removed, and we need it around for the CCB release
1842 xpt_release_ccb(done_ccb);
1843 cam_periph_unlock(periph);
1846 case DA_CCB_WAITING:
1848 /* Caller will release the CCB */
1849 wakeup(&done_ccb->ccb_h.cbfcnp);
1853 /* No-op. We're polling */
1858 xpt_release_ccb(done_ccb);
1862 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1864 struct da_softc *softc;
1865 struct cam_periph *periph;
1868 periph = xpt_path_periph(ccb->ccb_h.path);
1869 softc = (struct da_softc *)periph->softc;
1872 * Automatically detect devices that do not support
1873 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
1876 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
1877 error = cmd6workaround(ccb);
1878 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
1879 CAM_SCSI_STATUS_ERROR)
1880 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
1881 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1882 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
1883 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
1884 int sense_key, error_code, asc, ascq;
1886 scsi_extract_sense(&ccb->csio.sense_data,
1887 &error_code, &sense_key, &asc, &ascq);
1888 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
1889 error = cmd6workaround(ccb);
1891 if (error == ERESTART)
1896 * Until we have a better way of doing pack validation,
1897 * don't treat UAs as errors.
1899 sense_flags |= SF_RETRY_UA;
1900 return(cam_periph_error(ccb, cam_flags, sense_flags,
1901 &softc->saved_ccb));
1905 daprevent(struct cam_periph *periph, int action)
1907 struct da_softc *softc;
1911 softc = (struct da_softc *)periph->softc;
1913 if (((action == PR_ALLOW)
1914 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
1915 || ((action == PR_PREVENT)
1916 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
1920 ccb = cam_periph_getccb(periph, /*priority*/1);
1922 scsi_prevent(&ccb->csio,
1930 error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
1931 SF_RETRY_UA, &softc->device_stats);
1934 if (action == PR_ALLOW)
1935 softc->flags &= ~DA_FLAG_PACK_LOCKED;
1937 softc->flags |= DA_FLAG_PACK_LOCKED;
1940 xpt_release_ccb(ccb);
1944 dagetcapacity(struct cam_periph *periph)
1946 struct da_softc *softc;
1948 struct scsi_read_capacity_data *rcap;
1949 struct scsi_read_capacity_data_long *rcaplong;
1954 softc = (struct da_softc *)periph->softc;
1959 /* Do a read capacity */
1960 rcap = (void *)kmalloc(sizeof(*rcaplong), M_TEMP, M_INTWAIT);
1962 ccb = cam_periph_getccb(periph, /*priority*/1);
1963 scsi_read_capacity(&ccb->csio,
1970 ccb->ccb_h.ccb_bio = NULL;
1972 error = cam_periph_runccb(ccb, daerror,
1973 /*cam_flags*/CAM_RETRY_SELTO,
1974 /*sense_flags*/SF_RETRY_UA,
1975 &softc->device_stats);
1977 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1978 cam_release_devq(ccb->ccb_h.path,
1982 /*getcount_only*/0);
1985 block_len = scsi_4btoul(rcap->length);
1986 maxsector = scsi_4btoul(rcap->addr);
1988 if (maxsector != 0xffffffff)
1993 rcaplong = (struct scsi_read_capacity_data_long *)rcap;
1995 scsi_read_capacity_16(&ccb->csio,
1998 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2003 /*sense_len*/ SSD_FULL_SIZE,
2005 ccb->ccb_h.ccb_bio = NULL;
2007 error = cam_periph_runccb(ccb, daerror,
2008 /*cam_flags*/CAM_RETRY_SELTO,
2009 /*sense_flags*/SF_RETRY_UA,
2010 &softc->device_stats);
2012 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2013 cam_release_devq(ccb->ccb_h.path,
2017 /*getcount_only*/0);
2020 block_len = scsi_4btoul(rcaplong->length);
2021 maxsector = scsi_8btou64(rcaplong->addr);
2027 dasetgeom(periph, block_len, maxsector);
2029 xpt_release_ccb(ccb);
2031 kfree(rcap, M_TEMP);
2037 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector)
2039 struct ccb_calc_geometry ccg;
2040 struct da_softc *softc;
2041 struct disk_params *dp;
2043 softc = (struct da_softc *)periph->softc;
2045 dp = &softc->params;
2046 dp->secsize = block_len;
2047 dp->sectors = maxsector + 1;
2049 * Have the controller provide us with a geometry
2050 * for this disk. The only time the geometry
2051 * matters is when we boot and the controller
2052 * is the only one knowledgeable enough to come
2053 * up with something that will make this a bootable
2056 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
2057 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
2058 ccg.block_size = dp->secsize;
2059 ccg.volume_size = dp->sectors;
2061 ccg.secs_per_track = 0;
2063 xpt_action((union ccb*)&ccg);
2064 dp->heads = ccg.heads;
2065 dp->secs_per_track = ccg.secs_per_track;
2066 dp->cylinders = ccg.cylinders;
2070 dasendorderedtag(void *arg)
2072 struct da_softc *softc;
2074 for (softc = SLIST_FIRST(&softc_list);
2076 softc = SLIST_NEXT(softc, links)) {
2078 if ((softc->ordered_tag_count == 0)
2079 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
2080 softc->flags |= DA_FLAG_NEED_OTAG;
2082 if (softc->outstanding_cmds > 0)
2083 softc->flags &= ~DA_FLAG_WENT_IDLE;
2085 softc->ordered_tag_count = 0;
2088 /* Queue us up again */
2089 callout_reset(&dasendorderedtag_ch,
2090 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2091 dasendorderedtag, NULL);
2095 * Step through all DA peripheral drivers, and if the device is still open,
2096 * sync the disk cache to physical media.
2099 dashutdown(void * arg, int howto)
2101 struct cam_periph *periph;
2102 struct da_softc *softc;
2104 TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
2107 softc = (struct da_softc *)periph->softc;
2110 * We only sync the cache if the drive is still open, and
2111 * if the drive is capable of it..
2113 if (((softc->flags & DA_FLAG_OPEN) == 0)
2114 || (softc->quirks & DA_Q_NO_SYNC_CACHE))
2117 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
2119 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
2120 scsi_synchronize_cache(&ccb.csio,
2124 /*begin_lba*/0, /* whole disk */
2129 xpt_polled_action(&ccb);
2131 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2132 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2133 CAM_SCSI_STATUS_ERROR)
2134 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
2135 int error_code, sense_key, asc, ascq;
2137 scsi_extract_sense(&ccb.csio.sense_data,
2138 &error_code, &sense_key,
2141 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
2142 scsi_sense_print(&ccb.csio);
2144 xpt_print_path(periph->path);
2145 kprintf("Synchronize cache failed, status "
2146 "== 0x%x, scsi status == 0x%x\n",
2147 ccb.ccb_h.status, ccb.csio.scsi_status);
2151 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
2152 cam_release_devq(ccb.ccb_h.path,
2156 /*getcount_only*/0);
2161 #else /* !_KERNEL */
2164 * XXX This is only left out of the kernel build to silence warnings. If,
2165 * for some reason this function is used in the kernel, the ifdefs should
2166 * be moved so it is included both in the kernel and userland.
2169 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2170 void (*cbfcnp)(struct cam_periph *, union ccb *),
2171 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2172 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2175 struct scsi_format_unit *scsi_cmd;
2177 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2178 scsi_cmd->opcode = FORMAT_UNIT;
2179 scsi_cmd->byte2 = byte2;
2180 scsi_ulto2b(ileave, scsi_cmd->interleave);
2185 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2194 #endif /* _KERNEL */