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.52 2007/12/02 16:38:11 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
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.
218 * Reported by: Blaz Zupan <blaz@gold.amis.net>
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 like the synchronize cache command.
233 * Reported by: walter@pelissero.de
235 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
236 /*quirks*/ DA_Q_NO_SYNC_CACHE
240 * Doesn't work correctly with 6 byte reads/writes.
241 * Returns illegal request, and points to byte 9 of the
243 * Reported by: Adam McDougall <bsdx@spawnet.com>
245 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
246 /*quirks*/ DA_Q_NO_6_BYTE
250 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
251 /*quirks*/ DA_Q_NO_6_BYTE
255 * Doesn't like the synchronize cache command.
256 * Reported by: walter@pelissero.de
258 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
259 /*quirks*/ DA_Q_NO_SYNC_CACHE
263 * The CISS RAID controllers do not support SYNC_CACHE
265 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
266 /*quirks*/ DA_Q_NO_SYNC_CACHE
268 /* USB mass storage devices supported by umass(4) */
271 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
274 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
275 /*quirks*/ DA_Q_NO_SYNC_CACHE
279 * Power Quotient Int. (PQI) USB flash key
282 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
283 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
287 * Creative Nomad MUVO mp3 player (USB)
290 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
291 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
295 * Jungsoft NEXDISK USB flash key
298 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
299 /*quirks*/ DA_Q_NO_SYNC_CACHE
303 * FreeDik USB Mini Data Drive
306 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
307 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
311 * Sigmatel USB Flash MP3 Player
314 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
315 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
319 * Neuros USB Digital Audio Computer
322 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
323 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
327 * SEAGRAND NP-900 MP3 Player
330 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
331 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
335 * iRiver iFP MP3 player (with UMS Firmware)
336 * PR: kern/54881, i386/63941, kern/66124
338 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
339 /*quirks*/ DA_Q_NO_SYNC_CACHE
343 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
346 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
347 /*quirks*/ DA_Q_NO_SYNC_CACHE
351 * ZICPlay USB MP3 Player with FM
354 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
355 /*quirks*/ DA_Q_NO_SYNC_CACHE
359 * TEAC USB floppy mechanisms
361 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
362 /*quirks*/ DA_Q_NO_SYNC_CACHE
366 * Kingston DataTraveler II+ USB Pen-Drive.
367 * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
369 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
370 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
374 * Motorola E398 Mobile Phone (TransFlash memory card).
375 * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
378 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
379 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
383 * Qware BeatZkey! Pro
386 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
387 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
391 * Time DPA20B 1GB MP3 Player
394 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
395 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
399 * Samsung USB key 128Mb
402 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
403 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
407 * Kingston DataTraveler 2.0 USB Flash memory.
410 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
411 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
415 * Creative MUVO Slim mp3 player (USB)
418 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
419 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
423 * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
426 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
427 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
431 * SanDisk Micro Cruzer 128MB
434 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
435 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
439 * TOSHIBA TransMemory USB sticks
442 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
443 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
448 * PR: usb/75578, usb/72344, usb/65436
450 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
451 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
455 * Genesys 6-in-1 Card Reader
458 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
459 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
463 * Rekam Digital CAMERA
466 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
467 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
471 * iRiver H10 MP3 player
474 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
475 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
479 * iRiver U10 MP3 player
482 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
483 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
490 {T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
491 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
495 * EasyMP3 EM732X USB 2.0 Flash MP3 Player
498 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
499 "1.0"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
506 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
507 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
511 * Philips USB Key Audio KEY013
514 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
515 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
522 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
523 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
530 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
531 /*quirks*/ DA_Q_NO_SYNC_CACHE
535 * I/O Magic USB flash - Giga Bank
538 {T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
539 /*quirks*/ DA_Q_NO_SYNC_CACHE
543 * JoyFly 128mb USB Flash Drive
546 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
547 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
554 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
555 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
559 static d_open_t daopen;
560 static d_close_t daclose;
561 static d_strategy_t dastrategy;
562 static d_dump_t dadump;
563 static periph_init_t dainit;
564 static void daasync(void *callback_arg, u_int32_t code,
565 struct cam_path *path, void *arg);
566 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
567 static periph_ctor_t daregister;
568 static periph_dtor_t dacleanup;
569 static periph_start_t dastart;
570 static periph_oninv_t daoninvalidate;
571 static void dadone(struct cam_periph *periph,
572 union ccb *done_ccb);
573 static int daerror(union ccb *ccb, u_int32_t cam_flags,
574 u_int32_t sense_flags);
575 static void daprevent(struct cam_periph *periph, int action);
576 static int dagetcapacity(struct cam_periph *periph);
577 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
580 static timeout_t dasendorderedtag;
581 static void dashutdown(void *arg, int howto);
583 #ifndef DA_DEFAULT_TIMEOUT
584 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
587 #ifndef DA_DEFAULT_RETRY
588 #define DA_DEFAULT_RETRY 4
591 #ifndef DA_DEFAULT_SEND_ORDERED
592 #define DA_DEFAULT_SEND_ORDERED 1
595 static int da_retry_count = DA_DEFAULT_RETRY;
596 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
597 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
598 static struct callout dasendorderedtag_ch;
600 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
601 "CAM Direct Access Disk driver");
602 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
603 &da_retry_count, 0, "Normal I/O retry count");
604 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
605 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
606 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
607 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
608 SYSCTL_INT(_kern_cam_da, OID_AUTO, da_send_ordered, CTLFLAG_RW,
609 &da_send_ordered, 0, "Send Ordered Tags");
610 TUNABLE_INT("kern.cam.da.da_send_ordered", &da_send_ordered);
613 * DA_ORDEREDTAG_INTERVAL determines how often, relative
614 * to the default timeout, we check to see whether an ordered
615 * tagged transaction is appropriate to prevent simple tag
616 * starvation. Since we'd like to ensure that there is at least
617 * 1/2 of the timeout length left for a starved transaction to
618 * complete after we've sent an ordered tag, we must poll at least
619 * four times in every timeout period. This takes care of the worst
620 * case where a starved transaction starts during an interval that
621 * meets the requirement "don't send an ordered tag" test so it takes
622 * us two intervals to determine that a tag must be sent.
624 #ifndef DA_ORDEREDTAG_INTERVAL
625 #define DA_ORDEREDTAG_INTERVAL 4
628 static struct periph_driver dadriver =
631 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
634 PERIPHDRIVER_DECLARE(da, dadriver);
636 static struct dev_ops da_ops = {
637 { "da", DA_CDEV_MAJOR, D_DISK },
641 .d_write = physwrite,
642 .d_strategy = dastrategy,
646 static SLIST_HEAD(,da_softc) softc_list;
647 static struct extend_array *daperiphs;
650 daopen(struct dev_open_args *ap)
652 cdev_t dev = ap->a_head.a_dev;
653 struct cam_periph *periph;
654 struct da_softc *softc;
655 struct disk_info info;
661 periph = cam_extend_get(daperiphs, unit);
662 if (periph == NULL) {
667 softc = (struct da_softc *)periph->softc;
669 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
670 ("daopen: dev=%s (unit %d)\n", devtoname(dev),
673 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
675 return (error); /* error code from tsleep */
678 if ((softc->flags & DA_FLAG_OPEN) == 0) {
679 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
683 softc->flags |= DA_FLAG_OPEN;
686 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
687 /* Invalidate our pack information. */
688 disk_invalidate(&softc->disk);
689 softc->flags &= ~DA_FLAG_PACK_INVALID;
693 error = dagetcapacity(periph);
696 /* Do a read capacity */
698 struct scsi_read_capacity_data *rcap;
701 rcap = kmalloc(sizeof(*rcap), M_TEMP, M_INTWAIT | M_ZERO);
703 ccb = cam_periph_getccb(periph, /*priority*/1);
704 scsi_read_capacity(&ccb->csio,
711 ccb->ccb_h.ccb_bio = NULL;
713 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
714 /*sense_flags*/SF_RETRY_UA |
716 &softc->device_stats);
718 xpt_release_ccb(ccb);
721 dasetgeom(periph, rcap);
729 struct ccb_getdev cgd;
731 /* Build disk information structure */
732 bzero(&info, sizeof(info));
733 info.d_type = DTYPE_SCSI;
736 * Grab the inquiry data to get the vendor and product names.
737 * Put them in the typename and packname for the label.
739 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
740 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
741 xpt_action((union ccb *)&cgd);
744 strncpy(label->d_typename, cgd.inq_data.vendor,
745 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
746 strncpy(label->d_packname, cgd.inq_data.product,
747 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
753 info.d_media_blksize = softc->params.secsize;
754 info.d_media_blocks = softc->params.sectors;
755 info.d_media_size = 0;
760 info.d_secpertrack = softc->params.secs_per_track;
761 info.d_nheads = softc->params.heads;
762 info.d_ncylinders = softc->params.cylinders;
763 info.d_secpercyl = softc->params.heads *
764 softc->params.secs_per_track;
765 disk_setdiskinfo(&softc->disk, &info);
768 * Check to see whether or not the blocksize is set yet.
769 * If it isn't, set it and then clear the blocksize
770 * unavailable flag for the device statistics.
772 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0){
773 softc->device_stats.block_size = softc->params.secsize;
774 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
779 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
780 (softc->quirks & DA_Q_NO_PREVENT) == 0)
781 daprevent(periph, PR_PREVENT);
783 softc->flags &= ~DA_FLAG_OPEN;
784 cam_periph_release(periph);
786 cam_periph_unlock(periph);
791 daclose(struct dev_close_args *ap)
793 cdev_t dev = ap->a_head.a_dev;
794 struct cam_periph *periph;
795 struct da_softc *softc;
800 periph = cam_extend_get(daperiphs, unit);
804 softc = (struct da_softc *)periph->softc;
806 if ((error = cam_periph_lock(periph, 0)) != 0) {
807 return (error); /* error code from tsleep */
810 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
813 ccb = cam_periph_getccb(periph, /*priority*/1);
815 scsi_synchronize_cache(&ccb->csio,
819 /*begin_lba*/0,/* Cover the whole disk */
824 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
825 /*sense_flags*/SF_RETRY_UA,
826 &softc->device_stats);
828 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
829 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
830 CAM_SCSI_STATUS_ERROR) {
832 int sense_key, error_code;
834 scsi_extract_sense(&ccb->csio.sense_data,
838 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
839 scsi_sense_print(&ccb->csio);
841 xpt_print_path(periph->path);
842 kprintf("Synchronize cache failed, status "
843 "== 0x%x, scsi status == 0x%x\n",
844 ccb->csio.ccb_h.status,
845 ccb->csio.scsi_status);
849 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
850 cam_release_devq(ccb->ccb_h.path,
856 xpt_release_ccb(ccb);
860 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
861 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
862 daprevent(periph, PR_ALLOW);
864 * If we've got removeable media, mark the blocksize as
865 * unavailable, since it could change when new media is
868 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
872 * Don't compound any ref counting software bugs with more.
874 if (softc->flags & DA_FLAG_OPEN) {
875 softc->flags &= ~DA_FLAG_OPEN;
876 cam_periph_release(periph);
878 xpt_print_path(periph->path);
879 kprintf("daclose() called on an already closed device!\n");
881 cam_periph_unlock(periph);
886 * Actually translate the requested transfer into one the physical driver
887 * can understand. The transfer is described by a buf and will include
888 * only one physical transfer.
891 dastrategy(struct dev_strategy_args *ap)
893 cdev_t dev = ap->a_head.a_dev;
894 struct bio *bio = ap->a_bio;
895 struct buf *bp = bio->bio_buf;
896 struct cam_periph *periph;
897 struct da_softc *softc;
903 periph = cam_extend_get(daperiphs, unit);
904 if (periph == NULL) {
908 softc = (struct da_softc *)periph->softc;
911 * check it's not too big a transfer for our adapter
913 scsi_minphys(bp, &sd_switch);
917 * Mask interrupts so that the pack cannot be invalidated until
918 * after we are in the queue. Otherwise, we might not properly
919 * clean up one of the buffers.
924 * If the device has been made invalid, error out
926 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
933 * Place it in the queue of disk activities for this disk
935 bioqdisksort(&softc->bio_queue, bio);
940 * Schedule ourselves for performing the work.
942 xpt_schedule(periph, /* XXX priority */1);
946 bp->b_flags |= B_ERROR;
949 * Correctly set the buf to indicate a completed xfer
951 bp->b_resid = bp->b_bcount;
957 dadump(struct dev_dump_args *ap)
959 cdev_t dev = ap->a_head.a_dev;
960 struct cam_periph *periph;
961 struct da_softc *softc;
965 struct ccb_scsiio csio;
966 int dumppages = MAXDUMPPGS;
969 /* toss any characters present prior to dump */
970 while (cncheckc() != -1)
974 periph = cam_extend_get(daperiphs, unit);
975 if (periph == NULL) {
978 softc = (struct da_softc *)periph->softc;
980 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0)
983 addr = 0; /* starting address */
984 blkcnt = howmany(PAGE_SIZE, ap->a_secsize);
986 while (ap->a_count > 0) {
989 if ((ap->a_count / blkcnt) < dumppages)
990 dumppages = ap->a_count / blkcnt;
992 for (i = 0; i < dumppages; ++i) {
993 vm_paddr_t a = addr + (i * PAGE_SIZE);
994 if (is_physical_memory(a))
995 va = pmap_kenter_temporary(trunc_page(a), i);
997 va = pmap_kenter_temporary(trunc_page(0), i);
1000 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
1001 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1002 scsi_read_write(&csio,
1008 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1011 /*data_ptr*/(u_int8_t *) va,
1012 /*dxfer_len*/blkcnt * ap->a_secsize * dumppages,
1013 /*sense_len*/SSD_FULL_SIZE,
1014 DA_DEFAULT_TIMEOUT * 1000);
1015 xpt_polled_action((union ccb *)&csio);
1017 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1018 kprintf("Aborting dump due to I/O error.\n");
1019 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
1020 CAM_SCSI_STATUS_ERROR)
1021 scsi_sense_print(&csio);
1023 kprintf("status == 0x%x, scsi status == 0x%x\n",
1024 csio.ccb_h.status, csio.scsi_status);
1028 if (dumpstatus(addr, (off_t)ap->a_count * softc->params.secsize) < 0)
1031 /* update block count */
1032 ap->a_count -= blkcnt * dumppages;
1033 ap->a_blkno += blkcnt * dumppages;
1034 addr += PAGE_SIZE * dumppages;
1038 * Sync the disk cache contents to the physical media.
1040 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1042 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
1043 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1044 scsi_synchronize_cache(&csio,
1048 /*begin_lba*/0,/* Cover the whole disk */
1052 xpt_polled_action((union ccb *)&csio);
1054 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1055 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
1056 CAM_SCSI_STATUS_ERROR) {
1058 int sense_key, error_code;
1060 scsi_extract_sense(&csio.sense_data,
1064 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1065 scsi_sense_print(&csio);
1067 xpt_print_path(periph->path);
1068 kprintf("Synchronize cache failed, status "
1069 "== 0x%x, scsi status == 0x%x\n",
1070 csio.ccb_h.status, csio.scsi_status);
1081 struct cam_path *path;
1084 * Create our extend array for storing the devices we attach to.
1086 daperiphs = cam_extend_new();
1087 SLIST_INIT(&softc_list);
1088 if (daperiphs == NULL) {
1089 kprintf("da: Failed to alloc extend array!\n");
1093 callout_init(&dasendorderedtag_ch);
1096 * Install a global async callback. This callback will
1097 * receive async callbacks like "new device found".
1099 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
1100 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1102 if (status == CAM_REQ_CMP) {
1103 struct ccb_setasync csa;
1105 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
1106 csa.ccb_h.func_code = XPT_SASYNC_CB;
1107 csa.event_enable = AC_FOUND_DEVICE;
1108 csa.callback = daasync;
1109 csa.callback_arg = NULL;
1110 xpt_action((union ccb *)&csa);
1111 status = csa.ccb_h.status;
1112 xpt_free_path(path);
1115 if (status != CAM_REQ_CMP) {
1116 kprintf("da: Failed to attach master async callback "
1117 "due to status 0x%x!\n", status);
1118 } else if (da_send_ordered) {
1121 * Schedule a periodic event to occasionally send an
1122 * ordered tag to a device.
1124 callout_reset(&dasendorderedtag_ch,
1125 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
1126 dasendorderedtag, NULL);
1128 /* Register our shutdown event handler */
1129 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1130 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1131 kprintf("dainit: shutdown event registration failed!\n");
1136 daoninvalidate(struct cam_periph *periph)
1138 struct da_softc *softc;
1141 struct ccb_setasync csa;
1143 softc = (struct da_softc *)periph->softc;
1146 * De-register any async callbacks.
1148 xpt_setup_ccb(&csa.ccb_h, periph->path,
1150 csa.ccb_h.func_code = XPT_SASYNC_CB;
1151 csa.event_enable = 0;
1152 csa.callback = daasync;
1153 csa.callback_arg = periph;
1154 xpt_action((union ccb *)&csa);
1156 softc->flags |= DA_FLAG_PACK_INVALID;
1159 * Use a critical section to keep the buffer queue from being
1160 * modified while we traverse it.
1165 * Return all queued I/O with ENXIO.
1166 * XXX Handle any transactions queued to the card
1167 * with XPT_ABORT_CCB.
1169 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL){
1170 bioq_remove(&softc->bio_queue, q_bio);
1171 q_bp = q_bio->bio_buf;
1172 q_bp->b_resid = q_bp->b_bcount;
1173 q_bp->b_error = ENXIO;
1174 q_bp->b_flags |= B_ERROR;
1179 SLIST_REMOVE(&softc_list, softc, da_softc, links);
1181 xpt_print_path(periph->path);
1182 kprintf("lost device\n");
1186 dacleanup(struct cam_periph *periph)
1188 struct da_softc *softc;
1190 softc = (struct da_softc *)periph->softc;
1192 devstat_remove_entry(&softc->device_stats);
1193 cam_extend_release(daperiphs, periph->unit_number);
1194 xpt_print_path(periph->path);
1195 kprintf("removing device entry\n");
1197 * If we can't free the sysctl tree, oh well...
1199 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1200 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1201 xpt_print_path(periph->path);
1202 kprintf("can't remove sysctl context\n");
1204 if (softc->disk.d_rawdev) {
1205 disk_destroy(&softc->disk);
1207 kfree(softc, M_DEVBUF);
1211 daasync(void *callback_arg, u_int32_t code,
1212 struct cam_path *path, void *arg)
1214 struct cam_periph *periph;
1216 periph = (struct cam_periph *)callback_arg;
1218 case AC_FOUND_DEVICE:
1220 struct ccb_getdev *cgd;
1223 cgd = (struct ccb_getdev *)arg;
1227 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1228 && SID_TYPE(&cgd->inq_data) != T_RBC
1229 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1233 * Allocate a peripheral instance for
1234 * this device and start the probe
1237 status = cam_periph_alloc(daregister, daoninvalidate,
1239 "da", CAM_PERIPH_BIO,
1240 cgd->ccb_h.path, daasync,
1241 AC_FOUND_DEVICE, cgd);
1243 if (status != CAM_REQ_CMP
1244 && status != CAM_REQ_INPROG)
1245 kprintf("daasync: Unable to attach to new device "
1246 "due to status 0x%x\n", status);
1252 struct da_softc *softc;
1253 struct ccb_hdr *ccbh;
1255 softc = (struct da_softc *)periph->softc;
1258 * Don't fail on the expected unit attention
1261 softc->flags |= DA_FLAG_RETRY_UA;
1262 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1263 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1268 cam_periph_async(periph, code, path, arg);
1274 dasysctlinit(void *context, int pending)
1276 struct cam_periph *periph;
1277 struct da_softc *softc;
1278 char tmpstr[80], tmpstr2[80];
1280 periph = (struct cam_periph *)context;
1281 softc = (struct da_softc *)periph->softc;
1283 ksnprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1284 ksnprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1286 sysctl_ctx_init(&softc->sysctl_ctx);
1287 softc->flags |= DA_FLAG_SCTX_INIT;
1288 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1289 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1290 CTLFLAG_RD, 0, tmpstr);
1291 if (softc->sysctl_tree == NULL) {
1292 kprintf("dasysctlinit: unable to allocate sysctl tree\n");
1297 * Now register the sysctl handler, so the user can the value on
1300 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1301 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1302 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1303 "Minimum CDB size");
1307 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1311 value = *(int *)arg1;
1313 error = sysctl_handle_int(oidp, &value, 0, req);
1316 || (req->newptr == NULL))
1320 * Acceptable values here are 6, 10 or 12, or 16.
1324 else if ((value > 6)
1327 else if ((value > 10)
1330 else if (value > 12)
1333 *(int *)arg1 = value;
1339 daregister(struct cam_periph *periph, void *arg)
1341 struct da_softc *softc;
1342 struct ccb_setasync csa;
1343 struct ccb_pathinq cpi;
1344 struct ccb_getdev *cgd;
1348 cgd = (struct ccb_getdev *)arg;
1349 if (periph == NULL) {
1350 kprintf("daregister: periph was NULL!!\n");
1351 return(CAM_REQ_CMP_ERR);
1355 kprintf("daregister: no getdev CCB, can't register device\n");
1356 return(CAM_REQ_CMP_ERR);
1359 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
1360 LIST_INIT(&softc->pending_ccbs);
1361 softc->state = DA_STATE_PROBE;
1362 bioq_init(&softc->bio_queue);
1363 if (SID_IS_REMOVABLE(&cgd->inq_data))
1364 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1365 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1366 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1368 periph->softc = softc;
1370 cam_extend_set(daperiphs, periph->unit_number, periph);
1373 * See if this device has any quirks.
1375 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1376 (caddr_t)da_quirk_table,
1377 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1378 sizeof(*da_quirk_table), scsi_inquiry_match);
1381 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1383 softc->quirks = DA_Q_NONE;
1385 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1387 /* Check if the SIM does not want 6 byte commands */
1388 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
1389 cpi.ccb_h.func_code = XPT_PATH_INQ;
1390 xpt_action((union ccb *)&cpi);
1391 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1392 softc->quirks |= DA_Q_NO_6_BYTE;
1395 * RBC devices don't have to support READ(6), only READ(10).
1397 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1398 softc->minimum_cmd_size = 10;
1400 softc->minimum_cmd_size = 6;
1403 * Load the user's default, if any.
1405 ksnprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1406 periph->unit_number);
1407 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1410 * 6, 10, 12, and 16 are the currently permissible values.
1412 if (softc->minimum_cmd_size < 6)
1413 softc->minimum_cmd_size = 6;
1414 else if ((softc->minimum_cmd_size > 6)
1415 && (softc->minimum_cmd_size <= 10))
1416 softc->minimum_cmd_size = 10;
1417 else if ((softc->minimum_cmd_size > 10)
1418 && (softc->minimum_cmd_size <= 12))
1419 softc->minimum_cmd_size = 12;
1420 else if (softc->minimum_cmd_size > 12)
1421 softc->minimum_cmd_size = 16;
1424 * Block our timeout handler while we
1425 * add this softc to the dev list.
1428 SLIST_INSERT_HEAD(&softc_list, softc, links);
1432 * The DA driver supports a blocksize, but
1433 * we don't know the blocksize until we do
1434 * a read capacity. So, set a flag to
1435 * indicate that the blocksize is
1436 * unavailable right now. We'll clear the
1437 * flag as soon as we've done a read capacity.
1439 devstat_add_entry(&softc->device_stats, "da",
1440 periph->unit_number, 0,
1441 DEVSTAT_BS_UNAVAILABLE,
1442 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
1443 DEVSTAT_PRIORITY_DISK);
1446 * Register this media as a disk
1448 disk_create(periph->unit_number, &softc->disk, &da_ops);
1449 softc->disk.d_rawdev->si_iosize_max = MAXPHYS;
1452 * Add async callbacks for bus reset and
1453 * bus device reset calls. I don't bother
1454 * checking if this fails as, in most cases,
1455 * the system will function just fine without
1456 * them and the only alternative would be to
1457 * not attach the device on failure.
1459 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
1460 csa.ccb_h.func_code = XPT_SASYNC_CB;
1461 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
1462 csa.callback = daasync;
1463 csa.callback_arg = periph;
1464 xpt_action((union ccb *)&csa);
1466 * Lock this peripheral until we are setup.
1467 * This first call can't block
1469 cam_periph_lock(periph, 0);
1470 xpt_schedule(periph, /*priority*/5);
1472 return(CAM_REQ_CMP);
1476 dastart(struct cam_periph *periph, union ccb *start_ccb)
1478 struct da_softc *softc;
1480 softc = (struct da_softc *)periph->softc;
1483 switch (softc->state) {
1484 case DA_STATE_NORMAL:
1486 /* Pull a buffer from the queue and get going on it */
1491 * See if there is a buf with work for us to do..
1494 bio = bioq_first(&softc->bio_queue);
1495 if (periph->immediate_priority <= periph->pinfo.priority) {
1496 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1497 ("queuing for immediate ccb\n"));
1498 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1499 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1501 periph->immediate_priority = CAM_PRIORITY_NONE;
1503 wakeup(&periph->ccb_list);
1504 } else if (bio == NULL) {
1506 xpt_release_ccb(start_ccb);
1510 bioq_remove(&softc->bio_queue, bio);
1513 devstat_start_transaction(&softc->device_stats);
1515 if ((bp->b_flags & B_ORDERED) != 0
1516 || (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1517 softc->flags &= ~DA_FLAG_NEED_OTAG;
1518 softc->ordered_tag_count++;
1519 tag_code = MSG_ORDERED_Q_TAG;
1521 tag_code = MSG_SIMPLE_Q_TAG;
1524 KKASSERT(bio->bio_offset % softc->params.secsize == 0);
1526 scsi_read_write(&start_ccb->csio,
1527 /*retries*/da_retry_count,
1530 (bp->b_cmd == BUF_CMD_READ),
1532 softc->minimum_cmd_size,
1533 bio->bio_offset / softc->params.secsize,
1534 bp->b_bcount / softc->params.secsize,
1537 /*sense_len*/SSD_FULL_SIZE,
1538 da_default_timeout * 1000);
1539 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1542 * Block out any asyncronous callbacks
1543 * while we touch the pending ccb list.
1545 LIST_INSERT_HEAD(&softc->pending_ccbs,
1546 &start_ccb->ccb_h, periph_links.le);
1548 softc->outstanding_cmds++;
1549 /* We expect a unit attention from this device */
1550 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1551 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1552 softc->flags &= ~DA_FLAG_RETRY_UA;
1555 start_ccb->ccb_h.ccb_bio = bio;
1556 bio = bioq_first(&softc->bio_queue);
1559 xpt_action(start_ccb);
1563 /* Have more work to do, so ensure we stay scheduled */
1564 xpt_schedule(periph, /* XXX priority */1);
1568 case DA_STATE_PROBE:
1570 struct ccb_scsiio *csio;
1571 struct scsi_read_capacity_data *rcap;
1573 rcap = kmalloc(sizeof(*rcap), M_TEMP, M_INTWAIT | M_ZERO);
1574 csio = &start_ccb->csio;
1575 scsi_read_capacity(csio,
1582 start_ccb->ccb_h.ccb_bio = NULL;
1583 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1584 xpt_action(start_ccb);
1587 case DA_STATE_PROBE2:
1589 struct ccb_scsiio *csio;
1590 struct scsi_read_capacity_data_long *rcaplong;
1592 rcaplong = (struct scsi_read_capacity_data_long *)
1593 kmalloc(sizeof(*rcaplong), M_TEMP, M_INTWAIT);
1594 if (rcaplong == NULL) {
1595 kprintf("dastart: Couldn't allocate read_capacity\n");
1596 /* da_free_periph??? */
1599 csio = &start_ccb->csio;
1600 scsi_read_capacity_16(csio,
1603 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1608 /*sense_len*/ SSD_FULL_SIZE,
1610 start_ccb->ccb_h.ccb_bio = NULL;
1611 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1612 xpt_action(start_ccb);
1619 cmd6workaround(union ccb *ccb)
1621 struct scsi_rw_6 cmd6;
1622 struct scsi_rw_10 *cmd10;
1623 struct da_softc *softc;
1627 cdb = ccb->csio.cdb_io.cdb_bytes;
1629 /* Translation only possible if CDB is an array and cmd is R/W6 */
1630 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1631 (*cdb != READ_6 && *cdb != WRITE_6))
1634 xpt_print_path(ccb->ccb_h.path);
1635 kprintf("READ(6)/WRITE(6) not supported, "
1636 "increasing minimum_cmd_size to 10.\n");
1637 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1638 softc->minimum_cmd_size = 10;
1640 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1641 cmd10 = (struct scsi_rw_10 *)cdb;
1642 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1644 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1645 cmd10->reserved = 0;
1646 scsi_ulto2b(cmd6.length, cmd10->length);
1647 cmd10->control = cmd6.control;
1648 ccb->csio.cdb_len = sizeof(*cmd10);
1650 /* Requeue request, unfreezing queue if necessary */
1651 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1652 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1655 cam_release_devq(ccb->ccb_h.path,
1659 /*getcount_only*/0);
1665 dadone(struct cam_periph *periph, union ccb *done_ccb)
1667 struct da_softc *softc;
1668 struct ccb_scsiio *csio;
1670 softc = (struct da_softc *)periph->softc;
1671 csio = &done_ccb->csio;
1672 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1673 case DA_CCB_BUFFER_IO:
1678 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1680 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1684 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1689 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1690 if (error == ERESTART) {
1692 * A retry was scheuled, so
1703 if (error == ENXIO) {
1705 * Catastrophic error. Mark our pack as
1708 /* XXX See if this is really a media
1711 xpt_print_path(periph->path);
1712 kprintf("Invalidating pack\n");
1713 softc->flags |= DA_FLAG_PACK_INVALID;
1717 * return all queued I/O with EIO, so that
1718 * the client can retry these I/Os in the
1719 * proper order should it attempt to recover.
1721 while ((q_bio = bioq_first(&softc->bio_queue))
1723 bioq_remove(&softc->bio_queue, q_bio);
1724 q_bp = q_bio->bio_buf;
1725 q_bp->b_resid = q_bp->b_bcount;
1726 q_bp->b_error = EIO;
1727 q_bp->b_flags |= B_ERROR;
1731 bp->b_error = error;
1732 bp->b_resid = bp->b_bcount;
1733 bp->b_flags |= B_ERROR;
1735 bp->b_resid = csio->resid;
1737 if (bp->b_resid != 0)
1738 bp->b_flags |= B_ERROR;
1740 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1741 cam_release_devq(done_ccb->ccb_h.path,
1745 /*getcount_only*/0);
1747 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1748 panic("REQ_CMP with QFRZN");
1749 bp->b_resid = csio->resid;
1750 if (csio->resid > 0)
1751 bp->b_flags |= B_ERROR;
1755 * Block out any asyncronous callbacks
1756 * while we touch the pending ccb list.
1759 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1760 softc->outstanding_cmds--;
1761 if (softc->outstanding_cmds == 0)
1762 softc->flags |= DA_FLAG_WENT_IDLE;
1765 devstat_end_transaction_buf(&softc->device_stats, bp);
1772 struct scsi_read_capacity_data *rdcap;
1773 struct scsi_read_capacity_data_long *rcaplong;
1774 char announce_buf[80];
1778 if (softc->state == DA_STATE_PROBE)
1779 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1781 rcaplong = (struct scsi_read_capacity_data_long *)
1784 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1785 struct disk_params *dp;
1786 uint32_t block_size;
1789 if (softc->state == DA_STATE_PROBE) {
1790 block_size = scsi_4btoul(rdcap->length);
1791 maxsector = scsi_4btoul(rdcap->addr);
1794 * According to SBC-2, if the standard 10
1795 * byte READ CAPACITY command returns 2^32,
1796 * we should issue the 16 byte version of
1797 * the command, since the device in question
1798 * has more sectors than can be represented
1799 * with the short version of the command.
1801 if (maxsector == 0xffffffff) {
1802 softc->state = DA_STATE_PROBE2;
1803 kfree(rdcap, M_TEMP);
1804 xpt_release_ccb(done_ccb);
1805 xpt_schedule(periph, /*priority*/5);
1809 block_size = scsi_4btoul(rcaplong->length);
1810 maxsector = scsi_8btou64(rcaplong->addr);
1812 dasetgeom(periph, block_size, maxsector);
1813 dp = &softc->params;
1814 ksnprintf(announce_buf, sizeof(announce_buf),
1815 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1816 (uintmax_t) (((uintmax_t)dp->secsize *
1817 dp->sectors) / (1024*1024)),
1818 (uintmax_t)dp->sectors,
1819 dp->secsize, dp->heads, dp->secs_per_track,
1824 announce_buf[0] = '\0';
1827 * Retry any UNIT ATTENTION type errors. They
1828 * are expected at boot.
1830 error = daerror(done_ccb, CAM_RETRY_SELTO,
1831 SF_RETRY_UA|SF_NO_PRINT);
1832 if (error == ERESTART) {
1834 * A retry was scheuled, so
1838 } else if (error != 0) {
1839 struct scsi_sense_data *sense;
1841 int sense_key, error_code;
1844 struct ccb_getdev cgd;
1846 /* Don't wedge this device's queue */
1847 status = done_ccb->ccb_h.status;
1848 if ((status & CAM_DEV_QFRZN) != 0)
1849 cam_release_devq(done_ccb->ccb_h.path,
1853 /*getcount_only*/0);
1856 xpt_setup_ccb(&cgd.ccb_h,
1857 done_ccb->ccb_h.path,
1859 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1860 xpt_action((union ccb *)&cgd);
1862 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1863 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1864 || ((status & CAM_AUTOSNS_VALID) == 0))
1870 sense = &csio->sense_data;
1871 scsi_extract_sense(sense, &error_code,
1876 * Attach to anything that claims to be a
1877 * direct access or optical disk device,
1878 * as long as it doesn't return a "Logical
1879 * unit not supported" (0x25) error.
1881 if ((have_sense) && (asc != 0x25)
1882 && (error_code == SSD_CURRENT_ERROR)) {
1883 const char *sense_key_desc;
1884 const char *asc_desc;
1886 scsi_sense_desc(sense_key, asc, ascq,
1890 ksnprintf(announce_buf,
1891 sizeof(announce_buf),
1892 "Attempt to query device "
1893 "size failed: %s, %s",
1901 xpt_print_path(periph->path);
1902 kprintf("got CAM status %#x\n",
1903 done_ccb->ccb_h.status);
1906 xpt_print_path(periph->path);
1907 kprintf("fatal error, failed"
1908 " to attach to device\n");
1911 * Free up resources.
1913 cam_periph_invalidate(periph);
1917 kfree(csio->data_ptr, M_TEMP);
1918 if (announce_buf[0] != '\0') {
1919 xpt_announce_periph(periph, announce_buf);
1921 * Create our sysctl variables, now that we know
1922 * we have successfully attached.
1924 taskqueue_enqueue(taskqueue_thread[mycpuid],
1925 &softc->sysctl_task);
1927 softc->state = DA_STATE_NORMAL;
1929 * Since our peripheral may be invalidated by an error
1930 * above or an external event, we must release our CCB
1931 * before releasing the probe lock on the peripheral.
1932 * The peripheral will only go away once the last lock
1933 * is removed, and we need it around for the CCB release
1936 xpt_release_ccb(done_ccb);
1937 cam_periph_unlock(periph);
1940 case DA_CCB_WAITING:
1942 /* Caller will release the CCB */
1943 wakeup(&done_ccb->ccb_h.cbfcnp);
1947 /* No-op. We're polling */
1952 xpt_release_ccb(done_ccb);
1956 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1958 struct da_softc *softc;
1959 struct cam_periph *periph;
1962 periph = xpt_path_periph(ccb->ccb_h.path);
1963 softc = (struct da_softc *)periph->softc;
1966 * Automatically detect devices that do not support
1967 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
1970 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
1971 error = cmd6workaround(ccb);
1972 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
1973 CAM_SCSI_STATUS_ERROR)
1974 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
1975 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1976 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
1977 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
1978 int sense_key, error_code, asc, ascq;
1980 scsi_extract_sense(&ccb->csio.sense_data,
1981 &error_code, &sense_key, &asc, &ascq);
1982 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
1983 error = cmd6workaround(ccb);
1985 if (error == ERESTART)
1990 * Until we have a better way of doing pack validation,
1991 * don't treat UAs as errors.
1993 sense_flags |= SF_RETRY_UA;
1994 return(cam_periph_error(ccb, cam_flags, sense_flags,
1995 &softc->saved_ccb));
1999 daprevent(struct cam_periph *periph, int action)
2001 struct da_softc *softc;
2005 softc = (struct da_softc *)periph->softc;
2007 if (((action == PR_ALLOW)
2008 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
2009 || ((action == PR_PREVENT)
2010 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
2014 ccb = cam_periph_getccb(periph, /*priority*/1);
2016 scsi_prevent(&ccb->csio,
2024 error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
2025 SF_RETRY_UA, &softc->device_stats);
2028 if (action == PR_ALLOW)
2029 softc->flags &= ~DA_FLAG_PACK_LOCKED;
2031 softc->flags |= DA_FLAG_PACK_LOCKED;
2034 xpt_release_ccb(ccb);
2038 dagetcapacity(struct cam_periph *periph)
2040 struct da_softc *softc;
2042 struct scsi_read_capacity_data *rcap;
2043 struct scsi_read_capacity_data_long *rcaplong;
2048 softc = (struct da_softc *)periph->softc;
2053 /* Do a read capacity */
2054 rcap = (void *)kmalloc(sizeof(*rcaplong), M_TEMP, M_INTWAIT);
2056 ccb = cam_periph_getccb(periph, /*priority*/1);
2057 scsi_read_capacity(&ccb->csio,
2064 ccb->ccb_h.ccb_bio = NULL;
2066 error = cam_periph_runccb(ccb, daerror,
2067 /*cam_flags*/CAM_RETRY_SELTO,
2068 /*sense_flags*/SF_RETRY_UA,
2069 &softc->device_stats);
2071 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2072 cam_release_devq(ccb->ccb_h.path,
2076 /*getcount_only*/0);
2079 block_len = scsi_4btoul(rcap->length);
2080 maxsector = scsi_4btoul(rcap->addr);
2082 if (maxsector != 0xffffffff)
2087 rcaplong = (struct scsi_read_capacity_data_long *)rcap;
2089 scsi_read_capacity_16(&ccb->csio,
2092 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2097 /*sense_len*/ SSD_FULL_SIZE,
2099 ccb->ccb_h.ccb_bio = NULL;
2101 error = cam_periph_runccb(ccb, daerror,
2102 /*cam_flags*/CAM_RETRY_SELTO,
2103 /*sense_flags*/SF_RETRY_UA,
2104 &softc->device_stats);
2106 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2107 cam_release_devq(ccb->ccb_h.path,
2111 /*getcount_only*/0);
2114 block_len = scsi_4btoul(rcaplong->length);
2115 maxsector = scsi_8btou64(rcaplong->addr);
2121 dasetgeom(periph, block_len, maxsector);
2123 xpt_release_ccb(ccb);
2125 kfree(rcap, M_TEMP);
2131 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector)
2133 struct ccb_calc_geometry ccg;
2134 struct da_softc *softc;
2135 struct disk_params *dp;
2137 softc = (struct da_softc *)periph->softc;
2139 dp = &softc->params;
2140 dp->secsize = block_len;
2141 dp->sectors = maxsector + 1;
2143 * Have the controller provide us with a geometry
2144 * for this disk. The only time the geometry
2145 * matters is when we boot and the controller
2146 * is the only one knowledgeable enough to come
2147 * up with something that will make this a bootable
2150 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
2151 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
2152 ccg.block_size = dp->secsize;
2153 ccg.volume_size = dp->sectors;
2155 ccg.secs_per_track = 0;
2157 xpt_action((union ccb*)&ccg);
2158 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2160 * We don't know what went wrong here- but just pick
2161 * a geometry so we don't have nasty things like divide
2165 dp->secs_per_track = 255;
2166 dp->cylinders = dp->sectors / (255 * 255);
2167 if (dp->cylinders == 0) {
2171 dp->heads = ccg.heads;
2172 dp->secs_per_track = ccg.secs_per_track;
2173 dp->cylinders = ccg.cylinders;
2178 dasendorderedtag(void *arg)
2180 struct da_softc *softc;
2182 if (da_send_ordered) {
2183 SLIST_FOREACH(softc, &softc_list, links) {
2185 if ((softc->ordered_tag_count == 0)
2186 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
2187 softc->flags |= DA_FLAG_NEED_OTAG;
2189 if (softc->outstanding_cmds > 0)
2190 softc->flags &= ~DA_FLAG_WENT_IDLE;
2191 softc->ordered_tag_count = 0;
2194 /* Queue us up again */
2195 callout_reset(&dasendorderedtag_ch,
2196 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2197 dasendorderedtag, NULL);
2202 * Step through all DA peripheral drivers, and if the device is still open,
2203 * sync the disk cache to physical media.
2206 dashutdown(void * arg, int howto)
2208 struct cam_periph *periph;
2209 struct da_softc *softc;
2211 TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
2214 softc = (struct da_softc *)periph->softc;
2217 * We only sync the cache if the drive is still open, and
2218 * if the drive is capable of it..
2220 if (((softc->flags & DA_FLAG_OPEN) == 0)
2221 || (softc->quirks & DA_Q_NO_SYNC_CACHE))
2224 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
2226 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
2227 scsi_synchronize_cache(&ccb.csio,
2231 /*begin_lba*/0, /* whole disk */
2236 xpt_polled_action(&ccb);
2238 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2239 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2240 CAM_SCSI_STATUS_ERROR)
2241 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
2242 int error_code, sense_key, asc, ascq;
2244 scsi_extract_sense(&ccb.csio.sense_data,
2245 &error_code, &sense_key,
2248 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
2249 scsi_sense_print(&ccb.csio);
2251 xpt_print_path(periph->path);
2252 kprintf("Synchronize cache failed, status "
2253 "== 0x%x, scsi status == 0x%x\n",
2254 ccb.ccb_h.status, ccb.csio.scsi_status);
2258 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
2259 cam_release_devq(ccb.ccb_h.path,
2263 /*getcount_only*/0);
2268 #else /* !_KERNEL */
2271 * XXX This is only left out of the kernel build to silence warnings. If,
2272 * for some reason this function is used in the kernel, the ifdefs should
2273 * be moved so it is included both in the kernel and userland.
2276 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2277 void (*cbfcnp)(struct cam_periph *, union ccb *),
2278 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2279 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2282 struct scsi_format_unit *scsi_cmd;
2284 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2285 scsi_cmd->opcode = FORMAT_UNIT;
2286 scsi_cmd->byte2 = byte2;
2287 scsi_ulto2b(ileave, scsi_cmd->interleave);
2292 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2301 #endif /* _KERNEL */