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.40 2007/07/23 19:20:43 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/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 struct disk_params params;
148 struct task sysctl_task;
149 struct sysctl_ctx_list sysctl_ctx;
150 struct sysctl_oid *sysctl_tree;
153 struct da_quirk_entry {
154 struct scsi_inquiry_pattern inq_pat;
158 static const char quantum[] = "QUANTUM";
159 static const char microp[] = "MICROP";
161 static struct da_quirk_entry da_quirk_table[] =
163 /* SPI, FC devices */
166 * Fujitsu M2513A MO drives.
167 * Tested devices: M2513A2 firmware versions 1200 & 1300.
168 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
169 * Reported by: W.Scholten <whs@xs4all.nl>
171 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
172 /*quirks*/ DA_Q_NO_SYNC_CACHE
176 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
177 /*quirks*/ DA_Q_NO_SYNC_CACHE
181 * This particular Fujitsu drive doesn't like the
182 * synchronize cache command.
183 * Reported by: Tom Jackson <toj@gorilla.net>
185 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
186 /*quirks*/ DA_Q_NO_SYNC_CACHE
191 * This drive doesn't like the synchronize cache command
192 * either. Reported by: Matthew Jacob <mjacob@feral.com>
193 * in NetBSD PR kern/6027, August 24, 1998.
195 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
196 /*quirks*/ DA_Q_NO_SYNC_CACHE
200 * This drive doesn't like the synchronize cache command
201 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
204 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
205 /*quirks*/ DA_Q_NO_SYNC_CACHE
209 * Doesn't like the synchronize cache command.
210 * Reported by: Blaz Zupan <blaz@gold.amis.net>
212 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
213 /*quirks*/ DA_Q_NO_SYNC_CACHE
217 * Doesn't like the synchronize cache command.
219 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
220 /*quirks*/ DA_Q_NO_SYNC_CACHE
224 * Doesn't like the synchronize cache command.
226 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
227 /*quirks*/ DA_Q_NO_SYNC_CACHE
231 * Doesn't work correctly with 6 byte reads/writes.
232 * Returns illegal request, and points to byte 9 of the
234 * Reported by: Adam McDougall <bsdx@spawnet.com>
236 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
237 /*quirks*/ DA_Q_NO_6_BYTE
241 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
242 /*quirks*/ DA_Q_NO_6_BYTE
244 /* XXX USB floppy quirks temporarily enabled for 4.9R */
245 /* USB floppy devices supported by umass(4) */
248 * This USB floppy drive uses the UFI command set. This
249 * command set is a derivative of the ATAPI command set and
250 * does not support READ_6 commands only READ_10. It also does
251 * not support sync cache (0x35).
253 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Y-E DATA", "USB-FDU", "*"},
254 /*quirks*/ DA_Q_NO_SYNC_CACHE
257 /* Another USB floppy */
258 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MATSHITA", "FDD CF-VFDU*","*"},
259 /*quirks*/ DA_Q_NO_SYNC_CACHE
263 * The vendor, product and version strings coming from the
264 * controller are null terminated instead of being padded with
265 * spaces. The trailing wildcard character '*' is required.
267 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SMSC*", "USB FDC*","*"},
268 /*quirks*/ DA_Q_NO_SYNC_CACHE
272 * SmartDisk (Mitsumi) USB floppy drive
275 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MITSUMI", "USB FDD", "*"},
276 /*quirks*/ DA_Q_NO_SYNC_CACHE
280 * The CISS RAID controllers do not support SYNC_CACHE
282 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
283 /*quirks*/ DA_Q_NO_SYNC_CACHE
286 /* USB mass storage devices supported by umass(4) */
289 * Sony Memory Stick adapter MSAC-US1 and
290 * Sony PCG-C1VJ Internal Memory Stick Slot (MSC-U01).
291 * Make all sony MS* products use this quirk.
293 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "MS*", "*"},
294 /*quirks*/ DA_Q_NO_SYNC_CACHE
298 * Sony Memory Stick adapter for the CLIE series
301 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "CLIE*", "*"},
302 /*quirks*/ DA_Q_NO_SYNC_CACHE
306 * Intelligent Stick USB disk-on-key
309 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB Card",
310 "IntelligentStick*", "*"},
311 /*quirks*/ DA_Q_NO_SYNC_CACHE
315 * Sony DSC cameras (DSC-S30, DSC-S50, DSC-S70)
317 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
318 /*quirks*/ DA_Q_NO_SYNC_CACHE
322 * Microtech USB CameraMate
324 {T_DIRECT, SIP_MEDIA_REMOVABLE, "eUSB Compact*",
325 "Compact Flash*", "*"},
326 /*quirks*/ DA_Q_NO_SYNC_CACHE
330 * Olympus digital cameras (C-3040ZOOM, C-2040ZOOM, C-1)
332 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "C-*", "*"},
333 /*quirks*/ DA_Q_NO_SYNC_CACHE
337 * Olympus digital cameras (E-100RS, E-10).
339 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "E-*", "*"},
340 /*quirks*/ DA_Q_NO_SYNC_CACHE
344 * KingByte Pen Drives
346 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NO BRAND", "PEN DRIVE", "*"},
347 /*quirks*/ DA_Q_NO_SYNC_CACHE
351 * iRiver iFP MP3 player (with UMS Firmware)
352 * PR: kern/54881, i386/63941, kern/66124
354 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
355 /*quirks*/ DA_Q_NO_SYNC_CACHE
359 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
362 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "NexIA+*", "*"},
363 /*quirks*/ DA_Q_NO_SYNC_CACHE
369 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJIFILMUSB-DRIVEUNIT",
370 "USB-DRIVEUNIT", "*"},
371 /*quirks*/ DA_Q_NO_SYNC_CACHE
375 * Minolta Dimage E203
377 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MINOLTA", "DiMAGE E203", "*"},
378 /*quirks*/ DA_Q_NO_SYNC_CACHE
385 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Apacer", "HandyDrive", "*"},
386 /*quirks*/ DA_Q_NO_SYNC_CACHE
390 * Daisy Technology PhotoClip on Zoran chip
393 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ZORAN", "COACH", "*"},
394 /*quirks*/ DA_Q_NO_SYNC_CACHE
398 * Sony USB Key-Storage
401 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Storage Media", "*"},
402 /*quirks*/ DA_Q_NO_SYNC_CACHE
404 #endif /* DA_OLD_QUIRKS */
407 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
410 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
411 /*quirks*/ DA_Q_NO_SYNC_CACHE
415 * Jungsoft NEXDISK USB flash key
418 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
419 /*quirks*/ DA_Q_NO_SYNC_CACHE
423 * Creative Nomad MUVO mp3 player (USB)
426 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
427 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
431 static d_open_t daopen;
432 static d_close_t daclose;
433 static d_strategy_t dastrategy;
434 static d_ioctl_t daioctl;
435 static d_dump_t dadump;
436 static periph_init_t dainit;
437 static void daasync(void *callback_arg, u_int32_t code,
438 struct cam_path *path, void *arg);
439 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
440 static periph_ctor_t daregister;
441 static periph_dtor_t dacleanup;
442 static periph_start_t dastart;
443 static periph_oninv_t daoninvalidate;
444 static void dadone(struct cam_periph *periph,
445 union ccb *done_ccb);
446 static int daerror(union ccb *ccb, u_int32_t cam_flags,
447 u_int32_t sense_flags);
448 static void daprevent(struct cam_periph *periph, int action);
449 static int dagetcapacity(struct cam_periph *periph);
450 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
453 static timeout_t dasendorderedtag;
454 static void dashutdown(void *arg, int howto);
456 #ifndef DA_DEFAULT_TIMEOUT
457 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
460 #ifndef DA_DEFAULT_RETRY
461 #define DA_DEFAULT_RETRY 4
464 static int da_retry_count = DA_DEFAULT_RETRY;
465 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
466 static struct callout dasendorderedtag_ch;
468 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
469 "CAM Direct Access Disk driver");
470 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
471 &da_retry_count, 0, "Normal I/O retry count");
472 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
473 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
474 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
475 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
478 * DA_ORDEREDTAG_INTERVAL determines how often, relative
479 * to the default timeout, we check to see whether an ordered
480 * tagged transaction is appropriate to prevent simple tag
481 * starvation. Since we'd like to ensure that there is at least
482 * 1/2 of the timeout length left for a starved transaction to
483 * complete after we've sent an ordered tag, we must poll at least
484 * four times in every timeout period. This takes care of the worst
485 * case where a starved transaction starts during an interval that
486 * meets the requirement "don't send an ordered tag" test so it takes
487 * us two intervals to determine that a tag must be sent.
489 #ifndef DA_ORDEREDTAG_INTERVAL
490 #define DA_ORDEREDTAG_INTERVAL 4
493 static struct periph_driver dadriver =
496 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
499 DATA_SET(periphdriver_set, dadriver);
501 /* For 2.2-stable support */
506 static struct dev_ops da_ops = {
507 { "da", DA_CDEV_MAJOR, D_DISK },
511 .d_write = physwrite,
513 .d_strategy = dastrategy,
517 static SLIST_HEAD(,da_softc) softc_list;
518 static struct extend_array *daperiphs;
521 daopen(struct dev_open_args *ap)
523 cdev_t dev = ap->a_head.a_dev;
524 struct cam_periph *periph;
525 struct da_softc *softc;
526 struct disk_info info;
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 ((softc->flags & DA_FLAG_OPEN) == 0) {
548 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
550 softc->flags |= DA_FLAG_OPEN;
554 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
555 /* Invalidate our pack information. */
556 disk_invalidate(&softc->disk);
557 softc->flags &= ~DA_FLAG_PACK_INVALID;
561 error = dagetcapacity(periph);
564 /* Do a read capacity */
566 struct scsi_read_capacity_data *rcap;
569 rcap = kmalloc(sizeof(*rcap), M_TEMP, M_INTWAIT | M_ZERO);
571 ccb = cam_periph_getccb(periph, /*priority*/1);
572 scsi_read_capacity(&ccb->csio,
579 ccb->ccb_h.ccb_bio = NULL;
581 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
582 /*sense_flags*/SF_RETRY_UA |
584 &softc->device_stats);
586 xpt_release_ccb(ccb);
589 dasetgeom(periph, rcap);
597 struct ccb_getdev cgd;
599 /* Build disk information structure */
600 bzero(&info, sizeof(info));
601 info.d_type = DTYPE_SCSI;
604 * Grab the inquiry data to get the vendor and product names.
605 * Put them in the typename and packname for the label.
607 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
608 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
609 xpt_action((union ccb *)&cgd);
612 strncpy(label->d_typename, cgd.inq_data.vendor,
613 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
614 strncpy(label->d_packname, cgd.inq_data.product,
615 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
621 info.d_media_blksize = softc->params.secsize;
622 info.d_media_blocks = softc->params.sectors;
623 info.d_media_size = 0;
628 info.d_secpertrack = softc->params.secs_per_track;
629 info.d_nheads = softc->params.heads;
630 info.d_ncylinders = softc->params.cylinders;
631 info.d_secpercyl = softc->params.heads *
632 softc->params.secs_per_track;
633 disk_setdiskinfo(&softc->disk, &info);
635 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
636 (softc->quirks & DA_Q_NO_PREVENT) == 0)
637 daprevent(periph, PR_PREVENT);
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_ALLOW);
654 softc->flags &= ~DA_FLAG_OPEN;
655 cam_periph_release(periph);
657 cam_periph_unlock(periph);
662 daclose(struct dev_close_args *ap)
664 cdev_t dev = ap->a_head.a_dev;
665 struct cam_periph *periph;
666 struct da_softc *softc;
671 periph = cam_extend_get(daperiphs, unit);
675 softc = (struct da_softc *)periph->softc;
677 if ((error = cam_periph_lock(periph, 0)) != 0) {
678 return (error); /* error code from tsleep */
681 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
684 ccb = cam_periph_getccb(periph, /*priority*/1);
686 scsi_synchronize_cache(&ccb->csio,
690 /*begin_lba*/0,/* Cover the whole disk */
695 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
696 /*sense_flags*/SF_RETRY_UA,
697 &softc->device_stats);
699 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
700 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
701 CAM_SCSI_STATUS_ERROR) {
703 int sense_key, error_code;
705 scsi_extract_sense(&ccb->csio.sense_data,
709 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
710 scsi_sense_print(&ccb->csio);
712 xpt_print_path(periph->path);
713 kprintf("Synchronize cache failed, status "
714 "== 0x%x, scsi status == 0x%x\n",
715 ccb->csio.ccb_h.status,
716 ccb->csio.scsi_status);
720 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
721 cam_release_devq(ccb->ccb_h.path,
727 xpt_release_ccb(ccb);
731 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
732 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
733 daprevent(periph, PR_ALLOW);
735 * If we've got removeable media, mark the blocksize as
736 * unavailable, since it could change when new media is
739 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
743 * Don't compound any ref counting software bugs with more.
745 if (softc->flags & DA_FLAG_OPEN) {
746 softc->flags &= ~DA_FLAG_OPEN;
747 cam_periph_release(periph);
749 xpt_print_path(periph->path);
750 kprintf("daclose() called on an already closed device!\n");
752 cam_periph_unlock(periph);
757 * Actually translate the requested transfer into one the physical driver
758 * can understand. The transfer is described by a buf and will include
759 * only one physical transfer.
762 dastrategy(struct dev_strategy_args *ap)
764 cdev_t dev = ap->a_head.a_dev;
765 struct bio *bio = ap->a_bio;
766 struct buf *bp = bio->bio_buf;
767 struct cam_periph *periph;
768 struct da_softc *softc;
774 periph = cam_extend_get(daperiphs, unit);
775 if (periph == NULL) {
779 softc = (struct da_softc *)periph->softc;
782 * check it's not too big a transfer for our adapter
784 scsi_minphys(bp, &sd_switch);
788 * Mask interrupts so that the pack cannot be invalidated until
789 * after we are in the queue. Otherwise, we might not properly
790 * clean up one of the buffers.
795 * If the device has been made invalid, error out
797 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
804 * Place it in the queue of disk activities for this disk
806 bioqdisksort(&softc->bio_queue, bio);
811 * Schedule ourselves for performing the work.
813 xpt_schedule(periph, /* XXX priority */1);
817 bp->b_flags |= B_ERROR;
820 * Correctly set the buf to indicate a completed xfer
822 bp->b_resid = bp->b_bcount;
827 /* For 2.2-stable support */
833 daioctl(struct dev_ioctl_args *ap)
835 cdev_t dev = ap->a_head.a_dev;
836 struct cam_periph *periph;
837 struct da_softc *softc;
842 periph = cam_extend_get(daperiphs, unit);
846 softc = (struct da_softc *)periph->softc;
848 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("daioctl\n"));
850 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
851 return (error); /* error code from tsleep */
854 error = cam_periph_ioctl(periph, ap->a_cmd, ap->a_data, daerror);
856 cam_periph_unlock(periph);
862 dadump(struct dev_dump_args *ap)
864 cdev_t dev = ap->a_head.a_dev;
865 struct cam_periph *periph;
866 struct da_softc *softc;
870 struct ccb_scsiio csio;
871 int dumppages = MAXDUMPPGS;
874 /* toss any characters present prior to dump */
875 while (cncheckc() != -1)
879 periph = cam_extend_get(daperiphs, unit);
880 if (periph == NULL) {
883 softc = (struct da_softc *)periph->softc;
885 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0)
888 addr = 0; /* starting address */
889 blkcnt = howmany(PAGE_SIZE, ap->a_secsize);
891 while (ap->a_count > 0) {
894 if ((ap->a_count / blkcnt) < dumppages)
895 dumppages = ap->a_count / blkcnt;
897 for (i = 0; i < dumppages; ++i) {
898 vm_paddr_t a = addr + (i * PAGE_SIZE);
899 if (is_physical_memory(a))
900 va = pmap_kenter_temporary(trunc_page(a), i);
902 va = pmap_kenter_temporary(trunc_page(0), i);
905 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
906 csio.ccb_h.ccb_state = DA_CCB_DUMP;
907 scsi_read_write(&csio,
913 /*minimum_cmd_size*/ softc->minimum_cmd_size,
916 /*data_ptr*/(u_int8_t *) va,
917 /*dxfer_len*/blkcnt * ap->a_secsize * dumppages,
918 /*sense_len*/SSD_FULL_SIZE,
919 DA_DEFAULT_TIMEOUT * 1000);
920 xpt_polled_action((union ccb *)&csio);
922 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
923 kprintf("Aborting dump due to I/O error.\n");
924 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
925 CAM_SCSI_STATUS_ERROR)
926 scsi_sense_print(&csio);
928 kprintf("status == 0x%x, scsi status == 0x%x\n",
929 csio.ccb_h.status, csio.scsi_status);
933 if (dumpstatus(addr, (off_t)ap->a_count * softc->params.secsize) < 0)
936 /* update block count */
937 ap->a_count -= blkcnt * dumppages;
938 ap->a_blkno += blkcnt * dumppages;
939 addr += PAGE_SIZE * dumppages;
943 * Sync the disk cache contents to the physical media.
945 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
947 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
948 csio.ccb_h.ccb_state = DA_CCB_DUMP;
949 scsi_synchronize_cache(&csio,
953 /*begin_lba*/0,/* Cover the whole disk */
957 xpt_polled_action((union ccb *)&csio);
959 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
960 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
961 CAM_SCSI_STATUS_ERROR) {
963 int sense_key, error_code;
965 scsi_extract_sense(&csio.sense_data,
969 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
970 scsi_sense_print(&csio);
972 xpt_print_path(periph->path);
973 kprintf("Synchronize cache failed, status "
974 "== 0x%x, scsi status == 0x%x\n",
975 csio.ccb_h.status, csio.scsi_status);
986 struct cam_path *path;
989 * Create our extend array for storing the devices we attach to.
991 daperiphs = cam_extend_new();
992 SLIST_INIT(&softc_list);
993 if (daperiphs == NULL) {
994 kprintf("da: Failed to alloc extend array!\n");
998 callout_init(&dasendorderedtag_ch);
1001 * Install a global async callback. This callback will
1002 * receive async callbacks like "new device found".
1004 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
1005 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1007 if (status == CAM_REQ_CMP) {
1008 struct ccb_setasync csa;
1010 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
1011 csa.ccb_h.func_code = XPT_SASYNC_CB;
1012 csa.event_enable = AC_FOUND_DEVICE;
1013 csa.callback = daasync;
1014 csa.callback_arg = NULL;
1015 xpt_action((union ccb *)&csa);
1016 status = csa.ccb_h.status;
1017 xpt_free_path(path);
1020 if (status != CAM_REQ_CMP) {
1021 kprintf("da: Failed to attach master async callback "
1022 "due to status 0x%x!\n", status);
1026 * Schedule a periodic event to occasionally send an
1027 * ordered tag to a device.
1029 callout_reset(&dasendorderedtag_ch,
1030 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
1031 dasendorderedtag, NULL);
1033 /* Register our shutdown event handler */
1034 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1035 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1036 kprintf("dainit: shutdown event registration failed!\n");
1041 daoninvalidate(struct cam_periph *periph)
1043 struct da_softc *softc;
1046 struct ccb_setasync csa;
1048 softc = (struct da_softc *)periph->softc;
1051 * De-register any async callbacks.
1053 xpt_setup_ccb(&csa.ccb_h, periph->path,
1055 csa.ccb_h.func_code = XPT_SASYNC_CB;
1056 csa.event_enable = 0;
1057 csa.callback = daasync;
1058 csa.callback_arg = periph;
1059 xpt_action((union ccb *)&csa);
1061 softc->flags |= DA_FLAG_PACK_INVALID;
1064 * Use a critical section to keep the buffer queue from being
1065 * modified while we traverse it.
1070 * Return all queued I/O with ENXIO.
1071 * XXX Handle any transactions queued to the card
1072 * with XPT_ABORT_CCB.
1074 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL){
1075 bioq_remove(&softc->bio_queue, q_bio);
1076 q_bp = q_bio->bio_buf;
1077 q_bp->b_resid = q_bp->b_bcount;
1078 q_bp->b_error = ENXIO;
1079 q_bp->b_flags |= B_ERROR;
1084 SLIST_REMOVE(&softc_list, softc, da_softc, links);
1086 xpt_print_path(periph->path);
1087 kprintf("lost device\n");
1091 dacleanup(struct cam_periph *periph)
1093 struct da_softc *softc;
1095 softc = (struct da_softc *)periph->softc;
1097 devstat_remove_entry(&softc->device_stats);
1098 cam_extend_release(daperiphs, periph->unit_number);
1099 xpt_print_path(periph->path);
1100 kprintf("removing device entry\n");
1102 * If we can't free the sysctl tree, oh well...
1104 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1105 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1106 xpt_print_path(periph->path);
1107 kprintf("can't remove sysctl context\n");
1109 if (softc->disk.d_rawdev) {
1110 disk_destroy(&softc->disk);
1112 kfree(softc, M_DEVBUF);
1116 daasync(void *callback_arg, u_int32_t code,
1117 struct cam_path *path, void *arg)
1119 struct cam_periph *periph;
1121 periph = (struct cam_periph *)callback_arg;
1123 case AC_FOUND_DEVICE:
1125 struct ccb_getdev *cgd;
1128 cgd = (struct ccb_getdev *)arg;
1130 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1131 && SID_TYPE(&cgd->inq_data) != T_RBC
1132 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1136 * Allocate a peripheral instance for
1137 * this device and start the probe
1140 status = cam_periph_alloc(daregister, daoninvalidate,
1142 "da", CAM_PERIPH_BIO,
1143 cgd->ccb_h.path, daasync,
1144 AC_FOUND_DEVICE, cgd);
1146 if (status != CAM_REQ_CMP
1147 && status != CAM_REQ_INPROG)
1148 kprintf("daasync: Unable to attach to new device "
1149 "due to status 0x%x\n", status);
1155 struct da_softc *softc;
1156 struct ccb_hdr *ccbh;
1158 softc = (struct da_softc *)periph->softc;
1161 * Don't fail on the expected unit attention
1164 softc->flags |= DA_FLAG_RETRY_UA;
1165 for (ccbh = LIST_FIRST(&softc->pending_ccbs);
1166 ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le))
1167 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1172 cam_periph_async(periph, code, path, arg);
1178 dasysctlinit(void *context, int pending)
1180 struct cam_periph *periph;
1181 struct da_softc *softc;
1182 char tmpstr[80], tmpstr2[80];
1184 periph = (struct cam_periph *)context;
1185 softc = (struct da_softc *)periph->softc;
1187 ksnprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1188 ksnprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1190 sysctl_ctx_init(&softc->sysctl_ctx);
1191 softc->flags |= DA_FLAG_SCTX_INIT;
1192 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1193 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1194 CTLFLAG_RD, 0, tmpstr);
1195 if (softc->sysctl_tree == NULL) {
1196 kprintf("dasysctlinit: unable to allocate sysctl tree\n");
1201 * Now register the sysctl handler, so the user can the value on
1204 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1205 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1206 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1207 "Minimum CDB size");
1211 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1215 value = *(int *)arg1;
1217 error = sysctl_handle_int(oidp, &value, 0, req);
1220 || (req->newptr == NULL))
1224 * Acceptable values here are 6, 10 or 12, or 16.
1228 else if ((value > 6)
1231 else if ((value > 10)
1234 else if (value > 12)
1237 *(int *)arg1 = value;
1243 daregister(struct cam_periph *periph, void *arg)
1245 struct da_softc *softc;
1246 struct ccb_setasync csa;
1247 struct ccb_pathinq cpi;
1248 struct ccb_getdev *cgd;
1252 cgd = (struct ccb_getdev *)arg;
1253 if (periph == NULL) {
1254 kprintf("daregister: periph was NULL!!\n");
1255 return(CAM_REQ_CMP_ERR);
1259 kprintf("daregister: no getdev CCB, can't register device\n");
1260 return(CAM_REQ_CMP_ERR);
1263 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
1264 LIST_INIT(&softc->pending_ccbs);
1265 softc->state = DA_STATE_PROBE;
1266 bioq_init(&softc->bio_queue);
1267 if (SID_IS_REMOVABLE(&cgd->inq_data))
1268 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1269 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1270 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1272 periph->softc = softc;
1274 cam_extend_set(daperiphs, periph->unit_number, periph);
1277 * See if this device has any quirks.
1279 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1280 (caddr_t)da_quirk_table,
1281 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1282 sizeof(*da_quirk_table), scsi_inquiry_match);
1285 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1287 softc->quirks = DA_Q_NONE;
1289 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1291 /* Check if the SIM does not want 6 byte commands */
1292 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
1293 cpi.ccb_h.func_code = XPT_PATH_INQ;
1294 xpt_action((union ccb *)&cpi);
1295 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1296 softc->quirks |= DA_Q_NO_6_BYTE;
1299 * RBC devices don't have to support READ(6), only READ(10).
1301 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1302 softc->minimum_cmd_size = 10;
1304 softc->minimum_cmd_size = 6;
1307 * Load the user's default, if any.
1309 ksnprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1310 periph->unit_number);
1311 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1314 * 6, 10, 12, and 16 are the currently permissible values.
1316 if (softc->minimum_cmd_size < 6)
1317 softc->minimum_cmd_size = 6;
1318 else if ((softc->minimum_cmd_size > 6)
1319 && (softc->minimum_cmd_size <= 10))
1320 softc->minimum_cmd_size = 10;
1321 else if ((softc->minimum_cmd_size > 10)
1322 && (softc->minimum_cmd_size <= 12))
1323 softc->minimum_cmd_size = 12;
1324 else if (softc->minimum_cmd_size > 12)
1325 softc->minimum_cmd_size = 16;
1328 * Block our timeout handler while we
1329 * add this softc to the dev list.
1332 SLIST_INSERT_HEAD(&softc_list, softc, links);
1336 * The DA driver supports a blocksize, but
1337 * we don't know the blocksize until we do
1338 * a read capacity. So, set a flag to
1339 * indicate that the blocksize is
1340 * unavailable right now. We'll clear the
1341 * flag as soon as we've done a read capacity.
1343 devstat_add_entry(&softc->device_stats, "da",
1344 periph->unit_number, 0,
1345 DEVSTAT_BS_UNAVAILABLE,
1346 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
1347 DEVSTAT_PRIORITY_DISK);
1350 * Register this media as a disk
1352 disk_create(periph->unit_number, &softc->disk, &da_ops);
1353 softc->disk.d_rawdev->si_iosize_max = MAXPHYS;
1356 * Add async callbacks for bus reset and
1357 * bus device reset calls. I don't bother
1358 * checking if this fails as, in most cases,
1359 * the system will function just fine without
1360 * them and the only alternative would be to
1361 * not attach the device on failure.
1363 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
1364 csa.ccb_h.func_code = XPT_SASYNC_CB;
1365 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
1366 csa.callback = daasync;
1367 csa.callback_arg = periph;
1368 xpt_action((union ccb *)&csa);
1370 * Lock this peripheral until we are setup.
1371 * This first call can't block
1373 cam_periph_lock(periph, 0);
1374 xpt_schedule(periph, /*priority*/5);
1376 return(CAM_REQ_CMP);
1380 dastart(struct cam_periph *periph, union ccb *start_ccb)
1382 struct da_softc *softc;
1384 softc = (struct da_softc *)periph->softc;
1387 switch (softc->state) {
1388 case DA_STATE_NORMAL:
1390 /* Pull a buffer from the queue and get going on it */
1395 * See if there is a buf with work for us to do..
1398 bio = bioq_first(&softc->bio_queue);
1399 if (periph->immediate_priority <= periph->pinfo.priority) {
1400 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1401 ("queuing for immediate ccb\n"));
1402 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1403 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1405 periph->immediate_priority = CAM_PRIORITY_NONE;
1407 wakeup(&periph->ccb_list);
1408 } else if (bio == NULL) {
1410 xpt_release_ccb(start_ccb);
1414 bioq_remove(&softc->bio_queue, bio);
1417 devstat_start_transaction(&softc->device_stats);
1419 if ((bp->b_flags & B_ORDERED) != 0
1420 || (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1421 softc->flags &= ~DA_FLAG_NEED_OTAG;
1422 softc->ordered_tag_count++;
1423 tag_code = MSG_ORDERED_Q_TAG;
1425 tag_code = MSG_SIMPLE_Q_TAG;
1428 KKASSERT(bio->bio_offset % softc->params.secsize == 0);
1430 scsi_read_write(&start_ccb->csio,
1431 /*retries*/da_retry_count,
1434 (bp->b_cmd == BUF_CMD_READ),
1436 softc->minimum_cmd_size,
1437 bio->bio_offset / softc->params.secsize,
1438 bp->b_bcount / softc->params.secsize,
1441 /*sense_len*/SSD_FULL_SIZE,
1442 da_default_timeout * 1000);
1443 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1446 * Block out any asyncronous callbacks
1447 * while we touch the pending ccb list.
1449 LIST_INSERT_HEAD(&softc->pending_ccbs,
1450 &start_ccb->ccb_h, periph_links.le);
1452 /* We expect a unit attention from this device */
1453 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1454 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1455 softc->flags &= ~DA_FLAG_RETRY_UA;
1458 start_ccb->ccb_h.ccb_bio = bio;
1459 bio = bioq_first(&softc->bio_queue);
1462 xpt_action(start_ccb);
1466 /* Have more work to do, so ensure we stay scheduled */
1467 xpt_schedule(periph, /* XXX priority */1);
1471 case DA_STATE_PROBE:
1473 struct ccb_scsiio *csio;
1474 struct scsi_read_capacity_data *rcap;
1476 rcap = kmalloc(sizeof(*rcap), M_TEMP, M_INTWAIT | M_ZERO);
1477 csio = &start_ccb->csio;
1478 scsi_read_capacity(csio,
1485 start_ccb->ccb_h.ccb_bio = NULL;
1486 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1487 xpt_action(start_ccb);
1490 case DA_STATE_PROBE2:
1492 struct ccb_scsiio *csio;
1493 struct scsi_read_capacity_data_long *rcaplong;
1495 rcaplong = (struct scsi_read_capacity_data_long *)
1496 kmalloc(sizeof(*rcaplong), M_TEMP, M_INTWAIT);
1497 if (rcaplong == NULL) {
1498 kprintf("dastart: Couldn't allocate read_capacity\n");
1499 /* da_free_periph??? */
1502 csio = &start_ccb->csio;
1503 scsi_read_capacity_16(csio,
1506 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1511 /*sense_len*/ SSD_FULL_SIZE,
1513 start_ccb->ccb_h.ccb_bio = NULL;
1514 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1515 xpt_action(start_ccb);
1522 cmd6workaround(union ccb *ccb)
1524 struct scsi_rw_6 cmd6;
1525 struct scsi_rw_10 *cmd10;
1526 struct da_softc *softc;
1530 cdb = ccb->csio.cdb_io.cdb_bytes;
1532 /* Translation only possible if CDB is an array and cmd is R/W6 */
1533 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1534 (*cdb != READ_6 && *cdb != WRITE_6))
1537 xpt_print_path(ccb->ccb_h.path);
1538 kprintf("READ(6)/WRITE(6) not supported, "
1539 "increasing minimum_cmd_size to 10.\n");
1540 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1541 softc->minimum_cmd_size = 10;
1543 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1544 cmd10 = (struct scsi_rw_10 *)cdb;
1545 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1547 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1548 cmd10->reserved = 0;
1549 scsi_ulto2b(cmd6.length, cmd10->length);
1550 cmd10->control = cmd6.control;
1551 ccb->csio.cdb_len = sizeof(*cmd10);
1553 /* Requeue request, unfreezing queue if necessary */
1554 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1555 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1558 cam_release_devq(ccb->ccb_h.path,
1562 /*getcount_only*/0);
1568 dadone(struct cam_periph *periph, union ccb *done_ccb)
1570 struct da_softc *softc;
1571 struct ccb_scsiio *csio;
1573 softc = (struct da_softc *)periph->softc;
1574 csio = &done_ccb->csio;
1575 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1576 case DA_CCB_BUFFER_IO:
1581 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1583 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1587 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1592 /* Retry selection timeouts */
1593 sf |= SF_RETRY_SELTO;
1595 if ((error = daerror(done_ccb, 0, sf)) == ERESTART) {
1597 * A retry was scheuled, so
1608 if (error == ENXIO) {
1610 * Catastrophic error. Mark our pack as
1613 /* XXX See if this is really a media
1616 xpt_print_path(periph->path);
1617 kprintf("Invalidating pack\n");
1618 softc->flags |= DA_FLAG_PACK_INVALID;
1622 * return all queued I/O with EIO, so that
1623 * the client can retry these I/Os in the
1624 * proper order should it attempt to recover.
1626 while ((q_bio = bioq_first(&softc->bio_queue))
1628 bioq_remove(&softc->bio_queue, q_bio);
1629 q_bp = q_bio->bio_buf;
1630 q_bp->b_resid = q_bp->b_bcount;
1631 q_bp->b_error = EIO;
1632 q_bp->b_flags |= B_ERROR;
1636 bp->b_error = error;
1637 bp->b_resid = bp->b_bcount;
1638 bp->b_flags |= B_ERROR;
1640 bp->b_resid = csio->resid;
1642 if (bp->b_resid != 0)
1643 bp->b_flags |= B_ERROR;
1645 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1646 cam_release_devq(done_ccb->ccb_h.path,
1650 /*getcount_only*/0);
1652 bp->b_resid = csio->resid;
1653 if (csio->resid > 0)
1654 bp->b_flags |= B_ERROR;
1658 * Block out any asyncronous callbacks
1659 * while we touch the pending ccb list.
1662 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1665 if (softc->device_stats.busy_count == 0)
1666 softc->flags |= DA_FLAG_WENT_IDLE;
1668 devstat_end_transaction_buf(&softc->device_stats, bp);
1675 struct scsi_read_capacity_data *rdcap;
1676 struct scsi_read_capacity_data_long *rcaplong;
1677 char announce_buf[80];
1681 if (softc->state == DA_STATE_PROBE)
1682 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1684 rcaplong = (struct scsi_read_capacity_data_long *)
1687 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1688 struct disk_params *dp;
1689 uint32_t block_size;
1692 if (softc->state == DA_STATE_PROBE) {
1693 block_size = scsi_4btoul(rdcap->length);
1694 maxsector = scsi_4btoul(rdcap->addr);
1697 * According to SBC-2, if the standard 10
1698 * byte READ CAPACITY command returns 2^32,
1699 * we should issue the 16 byte version of
1700 * the command, since the device in question
1701 * has more sectors than can be represented
1702 * with the short version of the command.
1704 if (maxsector == 0xffffffff) {
1705 softc->state = DA_STATE_PROBE2;
1706 kfree(rdcap, M_TEMP);
1707 xpt_release_ccb(done_ccb);
1708 xpt_schedule(periph, /*priority*/5);
1712 block_size = scsi_4btoul(rcaplong->length);
1713 maxsector = scsi_8btou64(rcaplong->addr);
1715 dasetgeom(periph, block_size, maxsector);
1716 dp = &softc->params;
1717 ksnprintf(announce_buf, sizeof(announce_buf),
1718 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1719 (uintmax_t) (((uintmax_t)dp->secsize *
1720 dp->sectors) / (1024*1024)),
1721 (uintmax_t)dp->sectors,
1722 dp->secsize, dp->heads, dp->secs_per_track,
1727 announce_buf[0] = '\0';
1730 * Retry any UNIT ATTENTION type errors. They
1731 * are expected at boot.
1733 error = daerror(done_ccb, 0, SF_RETRY_UA |
1734 SF_RETRY_SELTO | SF_NO_PRINT);
1735 if (error == ERESTART) {
1737 * A retry was scheuled, so
1741 } else if (error != 0) {
1742 struct scsi_sense_data *sense;
1744 int sense_key, error_code;
1747 struct ccb_getdev cgd;
1749 /* Don't wedge this device's queue */
1750 cam_release_devq(done_ccb->ccb_h.path,
1754 /*getcount_only*/0);
1756 status = done_ccb->ccb_h.status;
1758 xpt_setup_ccb(&cgd.ccb_h,
1759 done_ccb->ccb_h.path,
1761 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1762 xpt_action((union ccb *)&cgd);
1764 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1765 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1766 || ((status & CAM_AUTOSNS_VALID) == 0))
1772 sense = &csio->sense_data;
1773 scsi_extract_sense(sense, &error_code,
1778 * Attach to anything that claims to be a
1779 * direct access or optical disk device,
1780 * as long as it doesn't return a "Logical
1781 * unit not supported" (0x25) error.
1783 if ((have_sense) && (asc != 0x25)
1784 && (error_code == SSD_CURRENT_ERROR))
1785 ksnprintf(announce_buf,
1786 sizeof(announce_buf),
1787 "Attempt to query device "
1788 "size failed: %s, %s",
1789 scsi_sense_key_text[sense_key],
1790 scsi_sense_desc(asc,ascq,
1797 xpt_print_path(periph->path);
1798 kprintf("got CAM status %#x\n",
1799 done_ccb->ccb_h.status);
1802 xpt_print_path(periph->path);
1803 kprintf("fatal error, failed"
1804 " to attach to device\n");
1807 * Free up resources.
1809 cam_periph_invalidate(periph);
1813 kfree(csio->data_ptr, M_TEMP);
1814 if (announce_buf[0] != '\0') {
1815 xpt_announce_periph(periph, announce_buf);
1817 * Create our sysctl variables, now that we know
1818 * we have successfully attached.
1820 taskqueue_enqueue(taskqueue_thread[mycpuid],
1821 &softc->sysctl_task);
1823 softc->state = DA_STATE_NORMAL;
1825 * Since our peripheral may be invalidated by an error
1826 * above or an external event, we must release our CCB
1827 * before releasing the probe lock on the peripheral.
1828 * The peripheral will only go away once the last lock
1829 * is removed, and we need it around for the CCB release
1832 xpt_release_ccb(done_ccb);
1833 cam_periph_unlock(periph);
1836 case DA_CCB_WAITING:
1838 /* Caller will release the CCB */
1839 wakeup(&done_ccb->ccb_h.cbfcnp);
1843 /* No-op. We're polling */
1848 xpt_release_ccb(done_ccb);
1852 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1854 struct da_softc *softc;
1855 struct cam_periph *periph;
1858 periph = xpt_path_periph(ccb->ccb_h.path);
1859 softc = (struct da_softc *)periph->softc;
1862 * Automatically detect devices that do not support
1863 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
1866 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
1867 error = cmd6workaround(ccb);
1868 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
1869 CAM_SCSI_STATUS_ERROR)
1870 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
1871 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1872 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
1873 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
1874 int sense_key, error_code, asc, ascq;
1876 scsi_extract_sense(&ccb->csio.sense_data,
1877 &error_code, &sense_key, &asc, &ascq);
1878 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
1879 error = cmd6workaround(ccb);
1881 if (error == ERESTART)
1886 * Until we have a better way of doing pack validation,
1887 * don't treat UAs as errors.
1889 sense_flags |= SF_RETRY_UA;
1890 return(cam_periph_error(ccb, cam_flags, sense_flags,
1891 &softc->saved_ccb));
1895 daprevent(struct cam_periph *periph, int action)
1897 struct da_softc *softc;
1901 softc = (struct da_softc *)periph->softc;
1903 if (((action == PR_ALLOW)
1904 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
1905 || ((action == PR_PREVENT)
1906 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
1910 ccb = cam_periph_getccb(periph, /*priority*/1);
1912 scsi_prevent(&ccb->csio,
1920 error = cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
1921 /*sense_flags*/0, &softc->device_stats);
1924 if (action == PR_ALLOW)
1925 softc->flags &= ~DA_FLAG_PACK_LOCKED;
1927 softc->flags |= DA_FLAG_PACK_LOCKED;
1930 xpt_release_ccb(ccb);
1934 dagetcapacity(struct cam_periph *periph)
1936 struct da_softc *softc;
1938 struct scsi_read_capacity_data *rcap;
1939 struct scsi_read_capacity_data_long *rcaplong;
1944 softc = (struct da_softc *)periph->softc;
1949 /* Do a read capacity */
1950 rcap = (void *)kmalloc(sizeof(*rcaplong), M_TEMP, M_INTWAIT);
1952 ccb = cam_periph_getccb(periph, /*priority*/1);
1953 scsi_read_capacity(&ccb->csio,
1960 ccb->ccb_h.ccb_bio = NULL;
1962 error = cam_periph_runccb(ccb, daerror,
1963 /*cam_flags*/SF_RETRY_SELTO,
1964 /*sense_flags*/SF_RETRY_UA,
1965 &softc->device_stats);
1967 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1968 cam_release_devq(ccb->ccb_h.path,
1972 /*getcount_only*/0);
1975 block_len = scsi_4btoul(rcap->length);
1976 maxsector = scsi_4btoul(rcap->addr);
1978 if (maxsector != 0xffffffff)
1983 rcaplong = (struct scsi_read_capacity_data_long *)rcap;
1985 scsi_read_capacity_16(&ccb->csio,
1988 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1993 /*sense_len*/ SSD_FULL_SIZE,
1995 ccb->ccb_h.ccb_bio = NULL;
1997 error = cam_periph_runccb(ccb, daerror,
1998 /*cam_flags*/SF_RETRY_SELTO,
1999 /*sense_flags*/SF_RETRY_UA,
2000 &softc->device_stats);
2002 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2003 cam_release_devq(ccb->ccb_h.path,
2007 /*getcount_only*/0);
2010 block_len = scsi_4btoul(rcaplong->length);
2011 maxsector = scsi_8btou64(rcaplong->addr);
2017 dasetgeom(periph, block_len, maxsector);
2019 xpt_release_ccb(ccb);
2021 kfree(rcap, M_TEMP);
2027 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector)
2029 struct ccb_calc_geometry ccg;
2030 struct da_softc *softc;
2031 struct disk_params *dp;
2033 softc = (struct da_softc *)periph->softc;
2035 dp = &softc->params;
2036 dp->secsize = block_len;
2037 dp->sectors = maxsector + 1;
2039 * Have the controller provide us with a geometry
2040 * for this disk. The only time the geometry
2041 * matters is when we boot and the controller
2042 * is the only one knowledgeable enough to come
2043 * up with something that will make this a bootable
2046 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
2047 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
2048 ccg.block_size = dp->secsize;
2049 ccg.volume_size = dp->sectors;
2051 ccg.secs_per_track = 0;
2053 xpt_action((union ccb*)&ccg);
2054 dp->heads = ccg.heads;
2055 dp->secs_per_track = ccg.secs_per_track;
2056 dp->cylinders = ccg.cylinders;
2060 dasendorderedtag(void *arg)
2062 struct da_softc *softc;
2064 for (softc = SLIST_FIRST(&softc_list);
2066 softc = SLIST_NEXT(softc, links)) {
2068 if ((softc->ordered_tag_count == 0)
2069 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
2070 softc->flags |= DA_FLAG_NEED_OTAG;
2072 if (softc->device_stats.busy_count > 0)
2073 softc->flags &= ~DA_FLAG_WENT_IDLE;
2075 softc->ordered_tag_count = 0;
2078 /* Queue us up again */
2079 callout_reset(&dasendorderedtag_ch,
2080 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2081 dasendorderedtag, NULL);
2085 * Step through all DA peripheral drivers, and if the device is still open,
2086 * sync the disk cache to physical media.
2089 dashutdown(void * arg, int howto)
2091 struct cam_periph *periph;
2092 struct da_softc *softc;
2094 TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
2097 softc = (struct da_softc *)periph->softc;
2100 * We only sync the cache if the drive is still open, and
2101 * if the drive is capable of it..
2103 if (((softc->flags & DA_FLAG_OPEN) == 0)
2104 || (softc->quirks & DA_Q_NO_SYNC_CACHE))
2107 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
2109 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
2110 scsi_synchronize_cache(&ccb.csio,
2114 /*begin_lba*/0, /* whole disk */
2119 xpt_polled_action(&ccb);
2121 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2122 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2123 CAM_SCSI_STATUS_ERROR)
2124 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
2125 int error_code, sense_key, asc, ascq;
2127 scsi_extract_sense(&ccb.csio.sense_data,
2128 &error_code, &sense_key,
2131 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
2132 scsi_sense_print(&ccb.csio);
2134 xpt_print_path(periph->path);
2135 kprintf("Synchronize cache failed, status "
2136 "== 0x%x, scsi status == 0x%x\n",
2137 ccb.ccb_h.status, ccb.csio.scsi_status);
2141 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
2142 cam_release_devq(ccb.ccb_h.path,
2146 /*getcount_only*/0);
2151 #else /* !_KERNEL */
2154 * XXX This is only left out of the kernel build to silence warnings. If,
2155 * for some reason this function is used in the kernel, the ifdefs should
2156 * be moved so it is included both in the kernel and userland.
2159 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2160 void (*cbfcnp)(struct cam_periph *, union ccb *),
2161 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2162 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2165 struct scsi_format_unit *scsi_cmd;
2167 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2168 scsi_cmd->opcode = FORMAT_UNIT;
2169 scsi_cmd->byte2 = byte2;
2170 scsi_ulto2b(ileave, scsi_cmd->interleave);
2175 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2184 #endif /* _KERNEL */