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.53 2008/01/13 10:05:21 matthias 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.00"}, /*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
558 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
559 "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
563 static d_open_t daopen;
564 static d_close_t daclose;
565 static d_strategy_t dastrategy;
566 static d_dump_t dadump;
567 static periph_init_t dainit;
568 static void daasync(void *callback_arg, u_int32_t code,
569 struct cam_path *path, void *arg);
570 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
571 static periph_ctor_t daregister;
572 static periph_dtor_t dacleanup;
573 static periph_start_t dastart;
574 static periph_oninv_t daoninvalidate;
575 static void dadone(struct cam_periph *periph,
576 union ccb *done_ccb);
577 static int daerror(union ccb *ccb, u_int32_t cam_flags,
578 u_int32_t sense_flags);
579 static void daprevent(struct cam_periph *periph, int action);
580 static int dagetcapacity(struct cam_periph *periph);
581 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
584 static timeout_t dasendorderedtag;
585 static void dashutdown(void *arg, int howto);
587 #ifndef DA_DEFAULT_TIMEOUT
588 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
591 #ifndef DA_DEFAULT_RETRY
592 #define DA_DEFAULT_RETRY 4
595 #ifndef DA_DEFAULT_SEND_ORDERED
596 #define DA_DEFAULT_SEND_ORDERED 1
599 static int da_retry_count = DA_DEFAULT_RETRY;
600 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
601 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
602 static struct callout dasendorderedtag_ch;
604 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
605 "CAM Direct Access Disk driver");
606 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
607 &da_retry_count, 0, "Normal I/O retry count");
608 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
609 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
610 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
611 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
612 SYSCTL_INT(_kern_cam_da, OID_AUTO, da_send_ordered, CTLFLAG_RW,
613 &da_send_ordered, 0, "Send Ordered Tags");
614 TUNABLE_INT("kern.cam.da.da_send_ordered", &da_send_ordered);
617 * DA_ORDEREDTAG_INTERVAL determines how often, relative
618 * to the default timeout, we check to see whether an ordered
619 * tagged transaction is appropriate to prevent simple tag
620 * starvation. Since we'd like to ensure that there is at least
621 * 1/2 of the timeout length left for a starved transaction to
622 * complete after we've sent an ordered tag, we must poll at least
623 * four times in every timeout period. This takes care of the worst
624 * case where a starved transaction starts during an interval that
625 * meets the requirement "don't send an ordered tag" test so it takes
626 * us two intervals to determine that a tag must be sent.
628 #ifndef DA_ORDEREDTAG_INTERVAL
629 #define DA_ORDEREDTAG_INTERVAL 4
632 static struct periph_driver dadriver =
635 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
638 PERIPHDRIVER_DECLARE(da, dadriver);
640 static struct dev_ops da_ops = {
641 { "da", DA_CDEV_MAJOR, D_DISK },
645 .d_write = physwrite,
646 .d_strategy = dastrategy,
650 static SLIST_HEAD(,da_softc) softc_list;
651 static struct extend_array *daperiphs;
654 daopen(struct dev_open_args *ap)
656 cdev_t dev = ap->a_head.a_dev;
657 struct cam_periph *periph;
658 struct da_softc *softc;
659 struct disk_info info;
665 periph = cam_extend_get(daperiphs, unit);
666 if (periph == NULL) {
671 softc = (struct da_softc *)periph->softc;
673 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
674 ("daopen: dev=%s (unit %d)\n", devtoname(dev),
677 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
679 return (error); /* error code from tsleep */
682 if ((softc->flags & DA_FLAG_OPEN) == 0) {
683 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
687 softc->flags |= DA_FLAG_OPEN;
690 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
691 /* Invalidate our pack information. */
692 disk_invalidate(&softc->disk);
693 softc->flags &= ~DA_FLAG_PACK_INVALID;
697 error = dagetcapacity(periph);
700 /* Do a read capacity */
702 struct scsi_read_capacity_data *rcap;
705 rcap = kmalloc(sizeof(*rcap), M_TEMP, M_INTWAIT | M_ZERO);
707 ccb = cam_periph_getccb(periph, /*priority*/1);
708 scsi_read_capacity(&ccb->csio,
715 ccb->ccb_h.ccb_bio = NULL;
717 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
718 /*sense_flags*/SF_RETRY_UA |
720 &softc->device_stats);
722 xpt_release_ccb(ccb);
725 dasetgeom(periph, rcap);
733 struct ccb_getdev cgd;
735 /* Build disk information structure */
736 bzero(&info, sizeof(info));
737 info.d_type = DTYPE_SCSI;
740 * Grab the inquiry data to get the vendor and product names.
741 * Put them in the typename and packname for the label.
743 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
744 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
745 xpt_action((union ccb *)&cgd);
748 strncpy(label->d_typename, cgd.inq_data.vendor,
749 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
750 strncpy(label->d_packname, cgd.inq_data.product,
751 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
757 info.d_media_blksize = softc->params.secsize;
758 info.d_media_blocks = softc->params.sectors;
759 info.d_media_size = 0;
764 info.d_secpertrack = softc->params.secs_per_track;
765 info.d_nheads = softc->params.heads;
766 info.d_ncylinders = softc->params.cylinders;
767 info.d_secpercyl = softc->params.heads *
768 softc->params.secs_per_track;
769 disk_setdiskinfo(&softc->disk, &info);
772 * Check to see whether or not the blocksize is set yet.
773 * If it isn't, set it and then clear the blocksize
774 * unavailable flag for the device statistics.
776 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0){
777 softc->device_stats.block_size = softc->params.secsize;
778 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
783 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
784 (softc->quirks & DA_Q_NO_PREVENT) == 0)
785 daprevent(periph, PR_PREVENT);
787 softc->flags &= ~DA_FLAG_OPEN;
788 cam_periph_release(periph);
790 cam_periph_unlock(periph);
795 daclose(struct dev_close_args *ap)
797 cdev_t dev = ap->a_head.a_dev;
798 struct cam_periph *periph;
799 struct da_softc *softc;
804 periph = cam_extend_get(daperiphs, unit);
808 softc = (struct da_softc *)periph->softc;
810 if ((error = cam_periph_lock(periph, 0)) != 0) {
811 return (error); /* error code from tsleep */
814 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
817 ccb = cam_periph_getccb(periph, /*priority*/1);
819 scsi_synchronize_cache(&ccb->csio,
823 /*begin_lba*/0,/* Cover the whole disk */
828 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
829 /*sense_flags*/SF_RETRY_UA,
830 &softc->device_stats);
832 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
833 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
834 CAM_SCSI_STATUS_ERROR) {
836 int sense_key, error_code;
838 scsi_extract_sense(&ccb->csio.sense_data,
842 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
843 scsi_sense_print(&ccb->csio);
845 xpt_print_path(periph->path);
846 kprintf("Synchronize cache failed, status "
847 "== 0x%x, scsi status == 0x%x\n",
848 ccb->csio.ccb_h.status,
849 ccb->csio.scsi_status);
853 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
854 cam_release_devq(ccb->ccb_h.path,
860 xpt_release_ccb(ccb);
864 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
865 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
866 daprevent(periph, PR_ALLOW);
868 * If we've got removeable media, mark the blocksize as
869 * unavailable, since it could change when new media is
872 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
876 * Don't compound any ref counting software bugs with more.
878 if (softc->flags & DA_FLAG_OPEN) {
879 softc->flags &= ~DA_FLAG_OPEN;
880 cam_periph_release(periph);
882 xpt_print_path(periph->path);
883 kprintf("daclose() called on an already closed device!\n");
885 cam_periph_unlock(periph);
890 * Actually translate the requested transfer into one the physical driver
891 * can understand. The transfer is described by a buf and will include
892 * only one physical transfer.
895 dastrategy(struct dev_strategy_args *ap)
897 cdev_t dev = ap->a_head.a_dev;
898 struct bio *bio = ap->a_bio;
899 struct buf *bp = bio->bio_buf;
900 struct cam_periph *periph;
901 struct da_softc *softc;
907 periph = cam_extend_get(daperiphs, unit);
908 if (periph == NULL) {
912 softc = (struct da_softc *)periph->softc;
915 * check it's not too big a transfer for our adapter
917 scsi_minphys(bp, &sd_switch);
921 * Mask interrupts so that the pack cannot be invalidated until
922 * after we are in the queue. Otherwise, we might not properly
923 * clean up one of the buffers.
928 * If the device has been made invalid, error out
930 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
937 * Place it in the queue of disk activities for this disk
939 bioqdisksort(&softc->bio_queue, bio);
944 * Schedule ourselves for performing the work.
946 xpt_schedule(periph, /* XXX priority */1);
950 bp->b_flags |= B_ERROR;
953 * Correctly set the buf to indicate a completed xfer
955 bp->b_resid = bp->b_bcount;
961 dadump(struct dev_dump_args *ap)
963 cdev_t dev = ap->a_head.a_dev;
964 struct cam_periph *periph;
965 struct da_softc *softc;
969 struct ccb_scsiio csio;
970 int dumppages = MAXDUMPPGS;
973 /* toss any characters present prior to dump */
974 while (cncheckc() != -1)
978 periph = cam_extend_get(daperiphs, unit);
979 if (periph == NULL) {
982 softc = (struct da_softc *)periph->softc;
984 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0)
987 addr = 0; /* starting address */
988 blkcnt = howmany(PAGE_SIZE, ap->a_secsize);
990 while (ap->a_count > 0) {
993 if ((ap->a_count / blkcnt) < dumppages)
994 dumppages = ap->a_count / blkcnt;
996 for (i = 0; i < dumppages; ++i) {
997 vm_paddr_t a = addr + (i * PAGE_SIZE);
998 if (is_physical_memory(a))
999 va = pmap_kenter_temporary(trunc_page(a), i);
1001 va = pmap_kenter_temporary(trunc_page(0), i);
1004 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
1005 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1006 scsi_read_write(&csio,
1012 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1015 /*data_ptr*/(u_int8_t *) va,
1016 /*dxfer_len*/blkcnt * ap->a_secsize * dumppages,
1017 /*sense_len*/SSD_FULL_SIZE,
1018 DA_DEFAULT_TIMEOUT * 1000);
1019 xpt_polled_action((union ccb *)&csio);
1021 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1022 kprintf("Aborting dump due to I/O error.\n");
1023 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
1024 CAM_SCSI_STATUS_ERROR)
1025 scsi_sense_print(&csio);
1027 kprintf("status == 0x%x, scsi status == 0x%x\n",
1028 csio.ccb_h.status, csio.scsi_status);
1032 if (dumpstatus(addr, (off_t)ap->a_count * softc->params.secsize) < 0)
1035 /* update block count */
1036 ap->a_count -= blkcnt * dumppages;
1037 ap->a_blkno += blkcnt * dumppages;
1038 addr += PAGE_SIZE * dumppages;
1042 * Sync the disk cache contents to the physical media.
1044 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1046 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
1047 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1048 scsi_synchronize_cache(&csio,
1052 /*begin_lba*/0,/* Cover the whole disk */
1056 xpt_polled_action((union ccb *)&csio);
1058 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1059 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
1060 CAM_SCSI_STATUS_ERROR) {
1062 int sense_key, error_code;
1064 scsi_extract_sense(&csio.sense_data,
1068 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1069 scsi_sense_print(&csio);
1071 xpt_print_path(periph->path);
1072 kprintf("Synchronize cache failed, status "
1073 "== 0x%x, scsi status == 0x%x\n",
1074 csio.ccb_h.status, csio.scsi_status);
1085 struct cam_path *path;
1088 * Create our extend array for storing the devices we attach to.
1090 daperiphs = cam_extend_new();
1091 SLIST_INIT(&softc_list);
1092 if (daperiphs == NULL) {
1093 kprintf("da: Failed to alloc extend array!\n");
1097 callout_init(&dasendorderedtag_ch);
1100 * Install a global async callback. This callback will
1101 * receive async callbacks like "new device found".
1103 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
1104 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1106 if (status == CAM_REQ_CMP) {
1107 struct ccb_setasync csa;
1109 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
1110 csa.ccb_h.func_code = XPT_SASYNC_CB;
1111 csa.event_enable = AC_FOUND_DEVICE;
1112 csa.callback = daasync;
1113 csa.callback_arg = NULL;
1114 xpt_action((union ccb *)&csa);
1115 status = csa.ccb_h.status;
1116 xpt_free_path(path);
1119 if (status != CAM_REQ_CMP) {
1120 kprintf("da: Failed to attach master async callback "
1121 "due to status 0x%x!\n", status);
1122 } else if (da_send_ordered) {
1125 * Schedule a periodic event to occasionally send an
1126 * ordered tag to a device.
1128 callout_reset(&dasendorderedtag_ch,
1129 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
1130 dasendorderedtag, NULL);
1132 /* Register our shutdown event handler */
1133 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1134 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1135 kprintf("dainit: shutdown event registration failed!\n");
1140 daoninvalidate(struct cam_periph *periph)
1142 struct da_softc *softc;
1145 struct ccb_setasync csa;
1147 softc = (struct da_softc *)periph->softc;
1150 * De-register any async callbacks.
1152 xpt_setup_ccb(&csa.ccb_h, periph->path,
1154 csa.ccb_h.func_code = XPT_SASYNC_CB;
1155 csa.event_enable = 0;
1156 csa.callback = daasync;
1157 csa.callback_arg = periph;
1158 xpt_action((union ccb *)&csa);
1160 softc->flags |= DA_FLAG_PACK_INVALID;
1163 * Use a critical section to keep the buffer queue from being
1164 * modified while we traverse it.
1169 * Return all queued I/O with ENXIO.
1170 * XXX Handle any transactions queued to the card
1171 * with XPT_ABORT_CCB.
1173 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL){
1174 bioq_remove(&softc->bio_queue, q_bio);
1175 q_bp = q_bio->bio_buf;
1176 q_bp->b_resid = q_bp->b_bcount;
1177 q_bp->b_error = ENXIO;
1178 q_bp->b_flags |= B_ERROR;
1183 SLIST_REMOVE(&softc_list, softc, da_softc, links);
1185 xpt_print_path(periph->path);
1186 kprintf("lost device\n");
1190 dacleanup(struct cam_periph *periph)
1192 struct da_softc *softc;
1194 softc = (struct da_softc *)periph->softc;
1196 devstat_remove_entry(&softc->device_stats);
1197 cam_extend_release(daperiphs, periph->unit_number);
1198 xpt_print_path(periph->path);
1199 kprintf("removing device entry\n");
1201 * If we can't free the sysctl tree, oh well...
1203 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1204 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1205 xpt_print_path(periph->path);
1206 kprintf("can't remove sysctl context\n");
1208 if (softc->disk.d_rawdev) {
1209 disk_destroy(&softc->disk);
1211 kfree(softc, M_DEVBUF);
1215 daasync(void *callback_arg, u_int32_t code,
1216 struct cam_path *path, void *arg)
1218 struct cam_periph *periph;
1220 periph = (struct cam_periph *)callback_arg;
1222 case AC_FOUND_DEVICE:
1224 struct ccb_getdev *cgd;
1227 cgd = (struct ccb_getdev *)arg;
1231 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1232 && SID_TYPE(&cgd->inq_data) != T_RBC
1233 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1237 * Allocate a peripheral instance for
1238 * this device and start the probe
1241 status = cam_periph_alloc(daregister, daoninvalidate,
1243 "da", CAM_PERIPH_BIO,
1244 cgd->ccb_h.path, daasync,
1245 AC_FOUND_DEVICE, cgd);
1247 if (status != CAM_REQ_CMP
1248 && status != CAM_REQ_INPROG)
1249 kprintf("daasync: Unable to attach to new device "
1250 "due to status 0x%x\n", status);
1256 struct da_softc *softc;
1257 struct ccb_hdr *ccbh;
1259 softc = (struct da_softc *)periph->softc;
1262 * Don't fail on the expected unit attention
1265 softc->flags |= DA_FLAG_RETRY_UA;
1266 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1267 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1272 cam_periph_async(periph, code, path, arg);
1278 dasysctlinit(void *context, int pending)
1280 struct cam_periph *periph;
1281 struct da_softc *softc;
1282 char tmpstr[80], tmpstr2[80];
1284 periph = (struct cam_periph *)context;
1285 softc = (struct da_softc *)periph->softc;
1287 ksnprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1288 ksnprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1290 sysctl_ctx_init(&softc->sysctl_ctx);
1291 softc->flags |= DA_FLAG_SCTX_INIT;
1292 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1293 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1294 CTLFLAG_RD, 0, tmpstr);
1295 if (softc->sysctl_tree == NULL) {
1296 kprintf("dasysctlinit: unable to allocate sysctl tree\n");
1301 * Now register the sysctl handler, so the user can the value on
1304 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1305 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1306 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1307 "Minimum CDB size");
1311 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1315 value = *(int *)arg1;
1317 error = sysctl_handle_int(oidp, &value, 0, req);
1320 || (req->newptr == NULL))
1324 * Acceptable values here are 6, 10 or 12, or 16.
1328 else if ((value > 6)
1331 else if ((value > 10)
1334 else if (value > 12)
1337 *(int *)arg1 = value;
1343 daregister(struct cam_periph *periph, void *arg)
1345 struct da_softc *softc;
1346 struct ccb_setasync csa;
1347 struct ccb_pathinq cpi;
1348 struct ccb_getdev *cgd;
1352 cgd = (struct ccb_getdev *)arg;
1353 if (periph == NULL) {
1354 kprintf("daregister: periph was NULL!!\n");
1355 return(CAM_REQ_CMP_ERR);
1359 kprintf("daregister: no getdev CCB, can't register device\n");
1360 return(CAM_REQ_CMP_ERR);
1363 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
1364 LIST_INIT(&softc->pending_ccbs);
1365 softc->state = DA_STATE_PROBE;
1366 bioq_init(&softc->bio_queue);
1367 if (SID_IS_REMOVABLE(&cgd->inq_data))
1368 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1369 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1370 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1372 periph->softc = softc;
1374 cam_extend_set(daperiphs, periph->unit_number, periph);
1377 * See if this device has any quirks.
1379 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1380 (caddr_t)da_quirk_table,
1381 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1382 sizeof(*da_quirk_table), scsi_inquiry_match);
1385 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1387 softc->quirks = DA_Q_NONE;
1389 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1391 /* Check if the SIM does not want 6 byte commands */
1392 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
1393 cpi.ccb_h.func_code = XPT_PATH_INQ;
1394 xpt_action((union ccb *)&cpi);
1395 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1396 softc->quirks |= DA_Q_NO_6_BYTE;
1399 * RBC devices don't have to support READ(6), only READ(10).
1401 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1402 softc->minimum_cmd_size = 10;
1404 softc->minimum_cmd_size = 6;
1407 * Load the user's default, if any.
1409 ksnprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1410 periph->unit_number);
1411 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1414 * 6, 10, 12, and 16 are the currently permissible values.
1416 if (softc->minimum_cmd_size < 6)
1417 softc->minimum_cmd_size = 6;
1418 else if ((softc->minimum_cmd_size > 6)
1419 && (softc->minimum_cmd_size <= 10))
1420 softc->minimum_cmd_size = 10;
1421 else if ((softc->minimum_cmd_size > 10)
1422 && (softc->minimum_cmd_size <= 12))
1423 softc->minimum_cmd_size = 12;
1424 else if (softc->minimum_cmd_size > 12)
1425 softc->minimum_cmd_size = 16;
1428 * Block our timeout handler while we
1429 * add this softc to the dev list.
1432 SLIST_INSERT_HEAD(&softc_list, softc, links);
1436 * The DA driver supports a blocksize, but
1437 * we don't know the blocksize until we do
1438 * a read capacity. So, set a flag to
1439 * indicate that the blocksize is
1440 * unavailable right now. We'll clear the
1441 * flag as soon as we've done a read capacity.
1443 devstat_add_entry(&softc->device_stats, "da",
1444 periph->unit_number, 0,
1445 DEVSTAT_BS_UNAVAILABLE,
1446 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
1447 DEVSTAT_PRIORITY_DISK);
1450 * Register this media as a disk
1452 disk_create(periph->unit_number, &softc->disk, &da_ops);
1453 softc->disk.d_rawdev->si_iosize_max = MAXPHYS;
1456 * Add async callbacks for bus reset and
1457 * bus device reset calls. I don't bother
1458 * checking if this fails as, in most cases,
1459 * the system will function just fine without
1460 * them and the only alternative would be to
1461 * not attach the device on failure.
1463 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
1464 csa.ccb_h.func_code = XPT_SASYNC_CB;
1465 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
1466 csa.callback = daasync;
1467 csa.callback_arg = periph;
1468 xpt_action((union ccb *)&csa);
1470 * Lock this peripheral until we are setup.
1471 * This first call can't block
1473 cam_periph_lock(periph, 0);
1474 xpt_schedule(periph, /*priority*/5);
1476 return(CAM_REQ_CMP);
1480 dastart(struct cam_periph *periph, union ccb *start_ccb)
1482 struct da_softc *softc;
1484 softc = (struct da_softc *)periph->softc;
1487 switch (softc->state) {
1488 case DA_STATE_NORMAL:
1490 /* Pull a buffer from the queue and get going on it */
1495 * See if there is a buf with work for us to do..
1498 bio = bioq_first(&softc->bio_queue);
1499 if (periph->immediate_priority <= periph->pinfo.priority) {
1500 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1501 ("queuing for immediate ccb\n"));
1502 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1503 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1505 periph->immediate_priority = CAM_PRIORITY_NONE;
1507 wakeup(&periph->ccb_list);
1508 } else if (bio == NULL) {
1510 xpt_release_ccb(start_ccb);
1514 bioq_remove(&softc->bio_queue, bio);
1517 devstat_start_transaction(&softc->device_stats);
1519 if ((bp->b_flags & B_ORDERED) != 0
1520 || (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1521 softc->flags &= ~DA_FLAG_NEED_OTAG;
1522 softc->ordered_tag_count++;
1523 tag_code = MSG_ORDERED_Q_TAG;
1525 tag_code = MSG_SIMPLE_Q_TAG;
1528 KKASSERT(bio->bio_offset % softc->params.secsize == 0);
1530 scsi_read_write(&start_ccb->csio,
1531 /*retries*/da_retry_count,
1534 (bp->b_cmd == BUF_CMD_READ),
1536 softc->minimum_cmd_size,
1537 bio->bio_offset / softc->params.secsize,
1538 bp->b_bcount / softc->params.secsize,
1541 /*sense_len*/SSD_FULL_SIZE,
1542 da_default_timeout * 1000);
1543 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1546 * Block out any asyncronous callbacks
1547 * while we touch the pending ccb list.
1549 LIST_INSERT_HEAD(&softc->pending_ccbs,
1550 &start_ccb->ccb_h, periph_links.le);
1552 softc->outstanding_cmds++;
1553 /* We expect a unit attention from this device */
1554 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1555 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1556 softc->flags &= ~DA_FLAG_RETRY_UA;
1559 start_ccb->ccb_h.ccb_bio = bio;
1560 bio = bioq_first(&softc->bio_queue);
1563 xpt_action(start_ccb);
1567 /* Have more work to do, so ensure we stay scheduled */
1568 xpt_schedule(periph, /* XXX priority */1);
1572 case DA_STATE_PROBE:
1574 struct ccb_scsiio *csio;
1575 struct scsi_read_capacity_data *rcap;
1577 rcap = kmalloc(sizeof(*rcap), M_TEMP, M_INTWAIT | M_ZERO);
1578 csio = &start_ccb->csio;
1579 scsi_read_capacity(csio,
1586 start_ccb->ccb_h.ccb_bio = NULL;
1587 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1588 xpt_action(start_ccb);
1591 case DA_STATE_PROBE2:
1593 struct ccb_scsiio *csio;
1594 struct scsi_read_capacity_data_long *rcaplong;
1596 rcaplong = (struct scsi_read_capacity_data_long *)
1597 kmalloc(sizeof(*rcaplong), M_TEMP, M_INTWAIT);
1598 if (rcaplong == NULL) {
1599 kprintf("dastart: Couldn't allocate read_capacity\n");
1600 /* da_free_periph??? */
1603 csio = &start_ccb->csio;
1604 scsi_read_capacity_16(csio,
1607 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1612 /*sense_len*/ SSD_FULL_SIZE,
1614 start_ccb->ccb_h.ccb_bio = NULL;
1615 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1616 xpt_action(start_ccb);
1623 cmd6workaround(union ccb *ccb)
1625 struct scsi_rw_6 cmd6;
1626 struct scsi_rw_10 *cmd10;
1627 struct da_softc *softc;
1631 cdb = ccb->csio.cdb_io.cdb_bytes;
1633 /* Translation only possible if CDB is an array and cmd is R/W6 */
1634 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1635 (*cdb != READ_6 && *cdb != WRITE_6))
1638 xpt_print_path(ccb->ccb_h.path);
1639 kprintf("READ(6)/WRITE(6) not supported, "
1640 "increasing minimum_cmd_size to 10.\n");
1641 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1642 softc->minimum_cmd_size = 10;
1644 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1645 cmd10 = (struct scsi_rw_10 *)cdb;
1646 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1648 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1649 cmd10->reserved = 0;
1650 scsi_ulto2b(cmd6.length, cmd10->length);
1651 cmd10->control = cmd6.control;
1652 ccb->csio.cdb_len = sizeof(*cmd10);
1654 /* Requeue request, unfreezing queue if necessary */
1655 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1656 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1659 cam_release_devq(ccb->ccb_h.path,
1663 /*getcount_only*/0);
1669 dadone(struct cam_periph *periph, union ccb *done_ccb)
1671 struct da_softc *softc;
1672 struct ccb_scsiio *csio;
1674 softc = (struct da_softc *)periph->softc;
1675 csio = &done_ccb->csio;
1676 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1677 case DA_CCB_BUFFER_IO:
1682 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1684 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1688 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1693 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1694 if (error == ERESTART) {
1696 * A retry was scheuled, so
1707 if (error == ENXIO) {
1709 * Catastrophic error. Mark our pack as
1712 /* XXX See if this is really a media
1715 xpt_print_path(periph->path);
1716 kprintf("Invalidating pack\n");
1717 softc->flags |= DA_FLAG_PACK_INVALID;
1721 * return all queued I/O with EIO, so that
1722 * the client can retry these I/Os in the
1723 * proper order should it attempt to recover.
1725 while ((q_bio = bioq_first(&softc->bio_queue))
1727 bioq_remove(&softc->bio_queue, q_bio);
1728 q_bp = q_bio->bio_buf;
1729 q_bp->b_resid = q_bp->b_bcount;
1730 q_bp->b_error = EIO;
1731 q_bp->b_flags |= B_ERROR;
1735 bp->b_error = error;
1736 bp->b_resid = bp->b_bcount;
1737 bp->b_flags |= B_ERROR;
1739 bp->b_resid = csio->resid;
1741 if (bp->b_resid != 0)
1742 bp->b_flags |= B_ERROR;
1744 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1745 cam_release_devq(done_ccb->ccb_h.path,
1749 /*getcount_only*/0);
1751 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1752 panic("REQ_CMP with QFRZN");
1753 bp->b_resid = csio->resid;
1754 if (csio->resid > 0)
1755 bp->b_flags |= B_ERROR;
1759 * Block out any asyncronous callbacks
1760 * while we touch the pending ccb list.
1763 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1764 softc->outstanding_cmds--;
1765 if (softc->outstanding_cmds == 0)
1766 softc->flags |= DA_FLAG_WENT_IDLE;
1769 devstat_end_transaction_buf(&softc->device_stats, bp);
1776 struct scsi_read_capacity_data *rdcap;
1777 struct scsi_read_capacity_data_long *rcaplong;
1778 char announce_buf[80];
1782 if (softc->state == DA_STATE_PROBE)
1783 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1785 rcaplong = (struct scsi_read_capacity_data_long *)
1788 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1789 struct disk_params *dp;
1790 uint32_t block_size;
1793 if (softc->state == DA_STATE_PROBE) {
1794 block_size = scsi_4btoul(rdcap->length);
1795 maxsector = scsi_4btoul(rdcap->addr);
1798 * According to SBC-2, if the standard 10
1799 * byte READ CAPACITY command returns 2^32,
1800 * we should issue the 16 byte version of
1801 * the command, since the device in question
1802 * has more sectors than can be represented
1803 * with the short version of the command.
1805 if (maxsector == 0xffffffff) {
1806 softc->state = DA_STATE_PROBE2;
1807 kfree(rdcap, M_TEMP);
1808 xpt_release_ccb(done_ccb);
1809 xpt_schedule(periph, /*priority*/5);
1813 block_size = scsi_4btoul(rcaplong->length);
1814 maxsector = scsi_8btou64(rcaplong->addr);
1816 dasetgeom(periph, block_size, maxsector);
1817 dp = &softc->params;
1818 ksnprintf(announce_buf, sizeof(announce_buf),
1819 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1820 (uintmax_t) (((uintmax_t)dp->secsize *
1821 dp->sectors) / (1024*1024)),
1822 (uintmax_t)dp->sectors,
1823 dp->secsize, dp->heads, dp->secs_per_track,
1828 announce_buf[0] = '\0';
1831 * Retry any UNIT ATTENTION type errors. They
1832 * are expected at boot.
1834 error = daerror(done_ccb, CAM_RETRY_SELTO,
1835 SF_RETRY_UA|SF_NO_PRINT);
1836 if (error == ERESTART) {
1838 * A retry was scheuled, so
1842 } else if (error != 0) {
1843 struct scsi_sense_data *sense;
1845 int sense_key, error_code;
1848 struct ccb_getdev cgd;
1850 /* Don't wedge this device's queue */
1851 status = done_ccb->ccb_h.status;
1852 if ((status & CAM_DEV_QFRZN) != 0)
1853 cam_release_devq(done_ccb->ccb_h.path,
1857 /*getcount_only*/0);
1860 xpt_setup_ccb(&cgd.ccb_h,
1861 done_ccb->ccb_h.path,
1863 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1864 xpt_action((union ccb *)&cgd);
1866 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1867 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1868 || ((status & CAM_AUTOSNS_VALID) == 0))
1874 sense = &csio->sense_data;
1875 scsi_extract_sense(sense, &error_code,
1880 * Attach to anything that claims to be a
1881 * direct access or optical disk device,
1882 * as long as it doesn't return a "Logical
1883 * unit not supported" (0x25) error.
1885 if ((have_sense) && (asc != 0x25)
1886 && (error_code == SSD_CURRENT_ERROR)) {
1887 const char *sense_key_desc;
1888 const char *asc_desc;
1890 scsi_sense_desc(sense_key, asc, ascq,
1894 ksnprintf(announce_buf,
1895 sizeof(announce_buf),
1896 "Attempt to query device "
1897 "size failed: %s, %s",
1905 xpt_print_path(periph->path);
1906 kprintf("got CAM status %#x\n",
1907 done_ccb->ccb_h.status);
1910 xpt_print_path(periph->path);
1911 kprintf("fatal error, failed"
1912 " to attach to device\n");
1915 * Free up resources.
1917 cam_periph_invalidate(periph);
1921 kfree(csio->data_ptr, M_TEMP);
1922 if (announce_buf[0] != '\0') {
1923 xpt_announce_periph(periph, announce_buf);
1925 * Create our sysctl variables, now that we know
1926 * we have successfully attached.
1928 taskqueue_enqueue(taskqueue_thread[mycpuid],
1929 &softc->sysctl_task);
1931 softc->state = DA_STATE_NORMAL;
1933 * Since our peripheral may be invalidated by an error
1934 * above or an external event, we must release our CCB
1935 * before releasing the probe lock on the peripheral.
1936 * The peripheral will only go away once the last lock
1937 * is removed, and we need it around for the CCB release
1940 xpt_release_ccb(done_ccb);
1941 cam_periph_unlock(periph);
1944 case DA_CCB_WAITING:
1946 /* Caller will release the CCB */
1947 wakeup(&done_ccb->ccb_h.cbfcnp);
1951 /* No-op. We're polling */
1956 xpt_release_ccb(done_ccb);
1960 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1962 struct da_softc *softc;
1963 struct cam_periph *periph;
1966 periph = xpt_path_periph(ccb->ccb_h.path);
1967 softc = (struct da_softc *)periph->softc;
1970 * Automatically detect devices that do not support
1971 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
1974 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
1975 error = cmd6workaround(ccb);
1976 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
1977 CAM_SCSI_STATUS_ERROR)
1978 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
1979 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1980 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
1981 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
1982 int sense_key, error_code, asc, ascq;
1984 scsi_extract_sense(&ccb->csio.sense_data,
1985 &error_code, &sense_key, &asc, &ascq);
1986 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
1987 error = cmd6workaround(ccb);
1989 if (error == ERESTART)
1994 * Until we have a better way of doing pack validation,
1995 * don't treat UAs as errors.
1997 sense_flags |= SF_RETRY_UA;
1998 return(cam_periph_error(ccb, cam_flags, sense_flags,
1999 &softc->saved_ccb));
2003 daprevent(struct cam_periph *periph, int action)
2005 struct da_softc *softc;
2009 softc = (struct da_softc *)periph->softc;
2011 if (((action == PR_ALLOW)
2012 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
2013 || ((action == PR_PREVENT)
2014 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
2018 ccb = cam_periph_getccb(periph, /*priority*/1);
2020 scsi_prevent(&ccb->csio,
2028 error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
2029 SF_RETRY_UA, &softc->device_stats);
2032 if (action == PR_ALLOW)
2033 softc->flags &= ~DA_FLAG_PACK_LOCKED;
2035 softc->flags |= DA_FLAG_PACK_LOCKED;
2038 xpt_release_ccb(ccb);
2042 dagetcapacity(struct cam_periph *periph)
2044 struct da_softc *softc;
2046 struct scsi_read_capacity_data *rcap;
2047 struct scsi_read_capacity_data_long *rcaplong;
2052 softc = (struct da_softc *)periph->softc;
2057 /* Do a read capacity */
2058 rcap = (void *)kmalloc(sizeof(*rcaplong), M_TEMP, M_INTWAIT);
2060 ccb = cam_periph_getccb(periph, /*priority*/1);
2061 scsi_read_capacity(&ccb->csio,
2068 ccb->ccb_h.ccb_bio = NULL;
2070 error = cam_periph_runccb(ccb, daerror,
2071 /*cam_flags*/CAM_RETRY_SELTO,
2072 /*sense_flags*/SF_RETRY_UA,
2073 &softc->device_stats);
2075 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2076 cam_release_devq(ccb->ccb_h.path,
2080 /*getcount_only*/0);
2083 block_len = scsi_4btoul(rcap->length);
2084 maxsector = scsi_4btoul(rcap->addr);
2086 if (maxsector != 0xffffffff)
2091 rcaplong = (struct scsi_read_capacity_data_long *)rcap;
2093 scsi_read_capacity_16(&ccb->csio,
2096 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2101 /*sense_len*/ SSD_FULL_SIZE,
2103 ccb->ccb_h.ccb_bio = NULL;
2105 error = cam_periph_runccb(ccb, daerror,
2106 /*cam_flags*/CAM_RETRY_SELTO,
2107 /*sense_flags*/SF_RETRY_UA,
2108 &softc->device_stats);
2110 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2111 cam_release_devq(ccb->ccb_h.path,
2115 /*getcount_only*/0);
2118 block_len = scsi_4btoul(rcaplong->length);
2119 maxsector = scsi_8btou64(rcaplong->addr);
2125 dasetgeom(periph, block_len, maxsector);
2127 xpt_release_ccb(ccb);
2129 kfree(rcap, M_TEMP);
2135 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector)
2137 struct ccb_calc_geometry ccg;
2138 struct da_softc *softc;
2139 struct disk_params *dp;
2141 softc = (struct da_softc *)periph->softc;
2143 dp = &softc->params;
2144 dp->secsize = block_len;
2145 dp->sectors = maxsector + 1;
2147 * Have the controller provide us with a geometry
2148 * for this disk. The only time the geometry
2149 * matters is when we boot and the controller
2150 * is the only one knowledgeable enough to come
2151 * up with something that will make this a bootable
2154 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
2155 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
2156 ccg.block_size = dp->secsize;
2157 ccg.volume_size = dp->sectors;
2159 ccg.secs_per_track = 0;
2161 xpt_action((union ccb*)&ccg);
2162 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2164 * We don't know what went wrong here- but just pick
2165 * a geometry so we don't have nasty things like divide
2169 dp->secs_per_track = 255;
2170 dp->cylinders = dp->sectors / (255 * 255);
2171 if (dp->cylinders == 0) {
2175 dp->heads = ccg.heads;
2176 dp->secs_per_track = ccg.secs_per_track;
2177 dp->cylinders = ccg.cylinders;
2182 dasendorderedtag(void *arg)
2184 struct da_softc *softc;
2186 if (da_send_ordered) {
2187 SLIST_FOREACH(softc, &softc_list, links) {
2189 if ((softc->ordered_tag_count == 0)
2190 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
2191 softc->flags |= DA_FLAG_NEED_OTAG;
2193 if (softc->outstanding_cmds > 0)
2194 softc->flags &= ~DA_FLAG_WENT_IDLE;
2195 softc->ordered_tag_count = 0;
2198 /* Queue us up again */
2199 callout_reset(&dasendorderedtag_ch,
2200 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2201 dasendorderedtag, NULL);
2206 * Step through all DA peripheral drivers, and if the device is still open,
2207 * sync the disk cache to physical media.
2210 dashutdown(void * arg, int howto)
2212 struct cam_periph *periph;
2213 struct da_softc *softc;
2215 TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
2218 softc = (struct da_softc *)periph->softc;
2221 * We only sync the cache if the drive is still open, and
2222 * if the drive is capable of it..
2224 if (((softc->flags & DA_FLAG_OPEN) == 0)
2225 || (softc->quirks & DA_Q_NO_SYNC_CACHE))
2228 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
2230 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
2231 scsi_synchronize_cache(&ccb.csio,
2235 /*begin_lba*/0, /* whole disk */
2240 xpt_polled_action(&ccb);
2242 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2243 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2244 CAM_SCSI_STATUS_ERROR)
2245 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
2246 int error_code, sense_key, asc, ascq;
2248 scsi_extract_sense(&ccb.csio.sense_data,
2249 &error_code, &sense_key,
2252 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
2253 scsi_sense_print(&ccb.csio);
2255 xpt_print_path(periph->path);
2256 kprintf("Synchronize cache failed, status "
2257 "== 0x%x, scsi status == 0x%x\n",
2258 ccb.ccb_h.status, ccb.csio.scsi_status);
2262 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
2263 cam_release_devq(ccb.ccb_h.path,
2267 /*getcount_only*/0);
2272 #else /* !_KERNEL */
2275 * XXX This is only left out of the kernel build to silence warnings. If,
2276 * for some reason this function is used in the kernel, the ifdefs should
2277 * be moved so it is included both in the kernel and userland.
2280 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2281 void (*cbfcnp)(struct cam_periph *, union ccb *),
2282 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2283 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2286 struct scsi_format_unit *scsi_cmd;
2288 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2289 scsi_cmd->opcode = FORMAT_UNIT;
2290 scsi_cmd->byte2 = byte2;
2291 scsi_ulto2b(ileave, scsi_cmd->interleave);
2296 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2305 #endif /* _KERNEL */