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
32 #include "opt_hw_wdog.h"
35 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
43 #include <sys/devicestat.h>
46 #include <sys/eventhandler.h>
47 #include <sys/malloc.h>
50 #include <machine/md_var.h>
61 #include <cam/cam_ccb.h>
62 #include <cam/cam_extend.h>
63 #include <cam/cam_periph.h>
64 #include <cam/cam_xpt_periph.h>
66 #include <cam/scsi/scsi_message.h>
69 #include <cam/scsi/scsi_da.h>
79 DA_FLAG_PACK_INVALID = 0x001,
80 DA_FLAG_NEW_PACK = 0x002,
81 DA_FLAG_PACK_LOCKED = 0x004,
82 DA_FLAG_PACK_REMOVABLE = 0x008,
83 DA_FLAG_TAGGED_QUEUING = 0x010,
84 DA_FLAG_NEED_OTAG = 0x020,
85 DA_FLAG_WENT_IDLE = 0x040,
86 DA_FLAG_RETRY_UA = 0x080,
92 DA_Q_NO_SYNC_CACHE = 0x01,
98 DA_CCB_BUFFER_IO = 0x02,
99 DA_CCB_WAITING = 0x03,
101 DA_CCB_TYPE_MASK = 0x0F,
102 DA_CCB_RETRY_UA = 0x10
105 /* Offsets into our private area for storing information */
106 #define ccb_state ppriv_field0
107 #define ccb_bp ppriv_ptr1
112 u_int8_t secs_per_track;
113 u_int32_t secsize; /* Number of bytes/sector */
114 u_int32_t sectors; /* total number sectors */
118 struct bio_queue_head bio_queue;
119 struct devstat device_stats;
120 SLIST_ENTRY(da_softc) links;
121 LIST_HEAD(, ccb_hdr) pending_ccbs;
125 int minimum_cmd_size;
126 int ordered_tag_count;
127 struct disk_params params;
133 struct da_quirk_entry {
134 struct scsi_inquiry_pattern inq_pat;
138 static const char quantum[] = "QUANTUM";
139 static const char microp[] = "MICROP";
141 static struct da_quirk_entry da_quirk_table[] =
145 * This particular Fujitsu drive doesn't like the
146 * synchronize cache command.
147 * Reported by: Tom Jackson <toj@gorilla.net>
149 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
150 /*quirks*/ DA_Q_NO_SYNC_CACHE
155 * This drive doesn't like the synchronize cache command
156 * either. Reported by: Matthew Jacob <mjacob@feral.com>
157 * in NetBSD PR kern/6027, August 24, 1998.
159 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
160 /*quirks*/ DA_Q_NO_SYNC_CACHE
164 * This drive doesn't like the synchronize cache command
165 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
168 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
169 /*quirks*/ DA_Q_NO_SYNC_CACHE
173 * Doesn't like the synchronize cache command.
174 * Reported by: Blaz Zupan <blaz@gold.amis.net>
176 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
177 /*quirks*/ DA_Q_NO_SYNC_CACHE
181 * Doesn't like the synchronize cache command.
183 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
184 /*quirks*/ DA_Q_NO_SYNC_CACHE
188 * Doesn't like the synchronize cache command.
190 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
191 /*quirks*/ DA_Q_NO_SYNC_CACHE
195 * Doesn't work correctly with 6 byte reads/writes.
196 * Returns illegal request, and points to byte 9 of the
198 * Reported by: Adam McDougall <bsdx@spawnet.com>
200 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
201 /*quirks*/ DA_Q_NO_6_BYTE
207 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
208 /*quirks*/ DA_Q_NO_6_BYTE
211 /* Below a list of quirks for USB devices supported by umass. */
214 * This USB floppy drive uses the UFI command set. This
215 * command set is a derivative of the ATAPI command set and
216 * does not support READ_6 commands only READ_10. It also does
217 * not support sync cache (0x35).
219 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Y-E DATA", "USB-FDU", "*"},
220 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
223 /* Another USB floppy */
224 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MATSHITA", "FDD CF-VFDU*","*"},
225 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
229 * Sony Memory Stick adapter MSAC-US1,
230 * does not support READ_6 commands only READ_10. It also does
231 * not support sync cache (0x35).
232 * Sony PCG-C1VJ Internal Memory Stick Slot (MSC-U01) also
233 * has this quirk. Make all sony MS* products use this
234 * quirk. Reported by: TERAMOTO Masahiro
235 * <teramoto@comm.eng.osaka-u.ac.jp> (PR 23378).
237 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "MS*", "*"},
238 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
242 * Sony DSC cameras (DSC-S30, DSC-S50, DSC-S70)
243 * do not support READ_6 commands, only READ_10.
245 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
246 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
249 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "MCF3064AP", "*"},
250 /*quirks*/ DA_Q_NO_6_BYTE
254 static d_open_t daopen;
255 static d_close_t daclose;
256 static d_strategy_t dastrategy;
257 static d_ioctl_t daioctl;
258 static d_dump_t dadump;
259 static periph_init_t dainit;
260 static void daasync(void *callback_arg, u_int32_t code,
261 struct cam_path *path, void *arg);
262 static periph_ctor_t daregister;
263 static periph_dtor_t dacleanup;
264 static periph_start_t dastart;
265 static periph_oninv_t daoninvalidate;
266 static void dadone(struct cam_periph *periph,
267 union ccb *done_ccb);
268 static int daerror(union ccb *ccb, u_int32_t cam_flags,
269 u_int32_t sense_flags);
270 static void daprevent(struct cam_periph *periph, int action);
271 static void dasetgeom(struct cam_periph *periph,
272 struct scsi_read_capacity_data * rdcap);
273 static timeout_t dasendorderedtag;
274 static void dashutdown(void *arg, int howto);
276 #ifndef DA_DEFAULT_TIMEOUT
277 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
281 * DA_ORDEREDTAG_INTERVAL determines how often, relative
282 * to the default timeout, we check to see whether an ordered
283 * tagged transaction is appropriate to prevent simple tag
284 * starvation. Since we'd like to ensure that there is at least
285 * 1/2 of the timeout length left for a starved transaction to
286 * complete after we've sent an ordered tag, we must poll at least
287 * four times in every timeout period. This takes care of the worst
288 * case where a starved transaction starts during an interval that
289 * meets the requirement "don't send an ordered tag" test so it takes
290 * us two intervals to determine that a tag must be sent.
292 #ifndef DA_ORDEREDTAG_INTERVAL
293 #define DA_ORDEREDTAG_INTERVAL 4
296 static struct periph_driver dadriver =
299 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
302 PERIPHDRIVER_DECLARE(da, dadriver);
304 #define DA_CDEV_MAJOR 13
306 /* For 2.2-stable support */
311 static struct cdevsw da_cdevsw = {
315 /* write */ physwrite,
319 /* strategy */ dastrategy,
321 /* maj */ DA_CDEV_MAJOR,
327 static struct cdevsw dadisk_cdevsw;
329 static SLIST_HEAD(,da_softc) softc_list;
330 static struct extend_array *daperiphs;
333 daopen(dev_t dev, int flags, int fmt, struct proc *p)
335 struct cam_periph *periph;
336 struct da_softc *softc;
337 struct disklabel *label;
338 struct scsi_read_capacity_data *rcap;
348 periph = cam_extend_get(daperiphs, unit);
349 if (periph == NULL) {
354 softc = (struct da_softc *)periph->softc;
356 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
357 ("daopen: dev=%s (unit %d , partition %d)\n", devtoname(dev),
360 if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0)
361 return (error); /* error code from tsleep */
363 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
365 softc->flags |= DA_FLAG_OPEN;
367 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
368 /* Invalidate our pack information. */
369 disk_invalidate(&softc->disk);
370 softc->flags &= ~DA_FLAG_PACK_INVALID;
374 /* Do a read capacity */
375 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
379 ccb = cam_periph_getccb(periph, /*priority*/1);
380 scsi_read_capacity(&ccb->csio,
387 ccb->ccb_h.ccb_bp = NULL;
389 error = cam_periph_runccb(ccb, daerror,
390 /*cam_flags*/CAM_RETRY_SELTO,
391 /*sense_flags*/SF_RETRY_UA,
392 &softc->device_stats);
394 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
395 cam_release_devq(ccb->ccb_h.path,
400 xpt_release_ccb(ccb);
403 dasetgeom(periph, rcap);
408 struct ccb_getdev cgd;
410 /* Build label for whole disk. */
411 label = &softc->disk.d_label;
412 bzero(label, sizeof(*label));
413 label->d_type = DTYPE_SCSI;
416 * Grab the inquiry data to get the vendor and product names.
417 * Put them in the typename and packname for the label.
419 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
420 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
421 xpt_action((union ccb *)&cgd);
423 strncpy(label->d_typename, cgd.inq_data.vendor,
424 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
425 strncpy(label->d_packname, cgd.inq_data.product,
426 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
428 label->d_secsize = softc->params.secsize;
429 label->d_nsectors = softc->params.secs_per_track;
430 label->d_ntracks = softc->params.heads;
431 label->d_ncylinders = softc->params.cylinders;
432 label->d_secpercyl = softc->params.heads
433 * softc->params.secs_per_track;
434 label->d_secperunit = softc->params.sectors;
437 * Check to see whether or not the blocksize is set yet.
438 * If it isn't, set it and then clear the blocksize
439 * unavailable flag for the device statistics.
441 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0){
442 softc->device_stats.block_size = softc->params.secsize;
443 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
448 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0)
449 daprevent(periph, PR_PREVENT);
451 cam_periph_unlock(periph);
456 daclose(dev_t dev, int flag, int fmt, struct proc *p)
458 struct cam_periph *periph;
459 struct da_softc *softc;
464 periph = cam_extend_get(daperiphs, unit);
468 softc = (struct da_softc *)periph->softc;
470 if ((error = cam_periph_lock(periph, PRIBIO)) != 0) {
471 return (error); /* error code from tsleep */
474 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
477 ccb = cam_periph_getccb(periph, /*priority*/1);
479 scsi_synchronize_cache(&ccb->csio,
483 /*begin_lba*/0,/* Cover the whole disk */
488 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
489 /*sense_flags*/SF_RETRY_UA,
490 &softc->device_stats);
492 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
493 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
494 CAM_SCSI_STATUS_ERROR) {
496 int sense_key, error_code;
498 scsi_extract_sense(&ccb->csio.sense_data,
502 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
503 scsi_sense_print(&ccb->csio);
505 xpt_print_path(periph->path);
506 printf("Synchronize cache failed, status "
507 "== 0x%x, scsi status == 0x%x\n",
508 ccb->csio.ccb_h.status,
509 ccb->csio.scsi_status);
513 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
514 cam_release_devq(ccb->ccb_h.path,
520 xpt_release_ccb(ccb);
524 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
525 daprevent(periph, PR_ALLOW);
527 * If we've got removeable media, mark the blocksize as
528 * unavailable, since it could change when new media is
531 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
534 softc->flags &= ~DA_FLAG_OPEN;
535 cam_periph_unlock(periph);
536 cam_periph_release(periph);
541 * Actually translate the requested transfer into one the physical driver
542 * can understand. The transfer is described by a buf and will include
543 * only one physical transfer.
546 dastrategy(struct bio *bp)
548 struct cam_periph *periph;
549 struct da_softc *softc;
554 unit = dkunit(bp->bio_dev);
555 part = dkpart(bp->bio_dev);
556 periph = cam_extend_get(daperiphs, unit);
557 if (periph == NULL) {
558 bp->bio_error = ENXIO;
561 softc = (struct da_softc *)periph->softc;
564 * check it's not too big a transfer for our adapter
566 scsi_minphys(bp,&sd_switch);
570 * Mask interrupts so that the pack cannot be invalidated until
571 * after we are in the queue. Otherwise, we might not properly
572 * clean up one of the buffers.
577 * If the device has been made invalid, error out
579 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
581 bp->bio_error = ENXIO;
586 * Place it in the queue of disk activities for this disk
588 bioqdisksort(&softc->bio_queue, bp);
593 * Schedule ourselves for performing the work.
595 xpt_schedule(periph, /* XXX priority */1);
599 bp->bio_flags |= BIO_ERROR;
602 * Correctly set the buf to indicate a completed xfer
604 bp->bio_resid = bp->bio_bcount;
609 /* For 2.2-stable support */
615 daioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
617 struct cam_periph *periph;
618 struct da_softc *softc;
623 periph = cam_extend_get(daperiphs, unit);
627 softc = (struct da_softc *)periph->softc;
629 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("daioctl\n"));
631 if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) {
632 return (error); /* error code from tsleep */
635 error = cam_periph_ioctl(periph, cmd, addr, daerror);
637 cam_periph_unlock(periph);
645 struct cam_periph *periph;
646 struct da_softc *softc;
650 u_int num; /* number of sectors to write */
654 struct ccb_scsiio csio;
655 int dumppages = MAXDUMPPGS;
659 /* toss any characters present prior to dump */
660 while (cncheckc() != -1)
665 periph = cam_extend_get(daperiphs, unit);
666 if (periph == NULL) {
669 softc = (struct da_softc *)periph->softc;
671 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0)
674 error = disk_dumpcheck(dev, &num, &blknum, &secsize);
678 addr = 0; /* starting address */
679 blkcnt = howmany(PAGE_SIZE, secsize);
684 if ((num / blkcnt) < dumppages)
685 dumppages = num / blkcnt;
687 for (i = 0; i < dumppages; ++i) {
688 vm_offset_t a = addr + (i * PAGE_SIZE);
689 if (is_physical_memory(a))
690 va = pmap_kenter_temporary(trunc_page(a), i);
692 va = pmap_kenter_temporary(trunc_page(0), i);
695 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
696 csio.ccb_h.ccb_state = DA_CCB_DUMP;
697 scsi_read_write(&csio,
703 /*minimum_cmd_size*/ softc->minimum_cmd_size,
706 /*data_ptr*/(u_int8_t *) va,
707 /*dxfer_len*/blkcnt * secsize * dumppages,
708 /*sense_len*/SSD_FULL_SIZE,
709 DA_DEFAULT_TIMEOUT * 1000);
710 xpt_polled_action((union ccb *)&csio);
712 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
713 printf("Aborting dump due to I/O error.\n");
714 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
715 CAM_SCSI_STATUS_ERROR)
716 scsi_sense_print(&csio);
718 printf("status == 0x%x, scsi status == 0x%x\n",
719 csio.ccb_h.status, csio.scsi_status);
723 if (dumpstatus(addr, (long)(num * softc->params.secsize)) < 0)
726 /* update block count */
727 num -= blkcnt * dumppages;
728 blknum += blkcnt * dumppages;
729 addr += PAGE_SIZE * dumppages;
733 * Sync the disk cache contents to the physical media.
735 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
737 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
738 csio.ccb_h.ccb_state = DA_CCB_DUMP;
739 scsi_synchronize_cache(&csio,
743 /*begin_lba*/0,/* Cover the whole disk */
747 xpt_polled_action((union ccb *)&csio);
749 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
750 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
751 CAM_SCSI_STATUS_ERROR) {
753 int sense_key, error_code;
755 scsi_extract_sense(&csio.sense_data,
759 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
760 scsi_sense_print(&csio);
762 xpt_print_path(periph->path);
763 printf("Synchronize cache failed, status "
764 "== 0x%x, scsi status == 0x%x\n",
765 csio.ccb_h.status, csio.scsi_status);
776 struct cam_path *path;
779 * Create our extend array for storing the devices we attach to.
781 daperiphs = cam_extend_new();
782 SLIST_INIT(&softc_list);
783 if (daperiphs == NULL) {
784 printf("da: Failed to alloc extend array!\n");
789 * Install a global async callback. This callback will
790 * receive async callbacks like "new device found".
792 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
793 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
795 if (status == CAM_REQ_CMP) {
796 struct ccb_setasync csa;
798 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
799 csa.ccb_h.func_code = XPT_SASYNC_CB;
800 csa.event_enable = AC_FOUND_DEVICE;
801 csa.callback = daasync;
802 csa.callback_arg = NULL;
803 xpt_action((union ccb *)&csa);
804 status = csa.ccb_h.status;
808 if (status != CAM_REQ_CMP) {
809 printf("da: Failed to attach master async callback "
810 "due to status 0x%x!\n", status);
814 * Schedule a periodic event to occasioanly send an
815 * ordered tag to a device.
817 timeout(dasendorderedtag, NULL,
818 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL);
820 /* Register our shutdown event handler */
821 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
822 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
823 printf("dainit: shutdown event registration failed!\n");
828 daoninvalidate(struct cam_periph *periph)
831 struct da_softc *softc;
833 struct ccb_setasync csa;
835 softc = (struct da_softc *)periph->softc;
838 * De-register any async callbacks.
840 xpt_setup_ccb(&csa.ccb_h, periph->path,
842 csa.ccb_h.func_code = XPT_SASYNC_CB;
843 csa.event_enable = 0;
844 csa.callback = daasync;
845 csa.callback_arg = periph;
846 xpt_action((union ccb *)&csa);
848 softc->flags |= DA_FLAG_PACK_INVALID;
851 * Although the oninvalidate() routines are always called at
852 * splsoftcam, we need to be at splbio() here to keep the buffer
853 * queue from being modified while we traverse it.
858 * Return all queued I/O with ENXIO.
859 * XXX Handle any transactions queued to the card
860 * with XPT_ABORT_CCB.
862 while ((q_bp = bioq_first(&softc->bio_queue)) != NULL){
863 bioq_remove(&softc->bio_queue, q_bp);
864 q_bp->bio_resid = q_bp->bio_bcount;
865 q_bp->bio_error = ENXIO;
866 q_bp->bio_flags |= BIO_ERROR;
871 SLIST_REMOVE(&softc_list, softc, da_softc, links);
873 xpt_print_path(periph->path);
874 printf("lost device\n");
878 dacleanup(struct cam_periph *periph)
880 struct da_softc *softc;
882 softc = (struct da_softc *)periph->softc;
884 devstat_remove_entry(&softc->device_stats);
885 cam_extend_release(daperiphs, periph->unit_number);
886 xpt_print_path(periph->path);
887 printf("removing device entry\n");
889 disk_destroy(softc->dev);
891 free(softc, M_DEVBUF);
895 daasync(void *callback_arg, u_int32_t code,
896 struct cam_path *path, void *arg)
898 struct cam_periph *periph;
900 periph = (struct cam_periph *)callback_arg;
902 case AC_FOUND_DEVICE:
904 struct ccb_getdev *cgd;
907 cgd = (struct ccb_getdev *)arg;
909 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
910 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
914 * Allocate a peripheral instance for
915 * this device and start the probe
918 status = cam_periph_alloc(daregister, daoninvalidate,
920 "da", CAM_PERIPH_BIO,
921 cgd->ccb_h.path, daasync,
922 AC_FOUND_DEVICE, cgd);
924 if (status != CAM_REQ_CMP
925 && status != CAM_REQ_INPROG)
926 printf("daasync: Unable to attach to new device "
927 "due to status 0x%x\n", status);
933 struct da_softc *softc;
934 struct ccb_hdr *ccbh;
937 softc = (struct da_softc *)periph->softc;
940 * Don't fail on the expected unit attention
943 softc->flags |= DA_FLAG_RETRY_UA;
944 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
945 ccbh->ccb_state |= DA_CCB_RETRY_UA;
950 cam_periph_async(periph, code, path, arg);
956 daregister(struct cam_periph *periph, void *arg)
959 struct da_softc *softc;
960 struct ccb_setasync csa;
961 struct ccb_getdev *cgd;
964 cgd = (struct ccb_getdev *)arg;
965 if (periph == NULL) {
966 printf("daregister: periph was NULL!!\n");
967 return(CAM_REQ_CMP_ERR);
971 printf("daregister: no getdev CCB, can't register device\n");
972 return(CAM_REQ_CMP_ERR);
975 softc = (struct da_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
978 printf("daregister: Unable to probe new device. "
979 "Unable to allocate softc\n");
980 return(CAM_REQ_CMP_ERR);
983 bzero(softc, sizeof(*softc));
984 LIST_INIT(&softc->pending_ccbs);
985 softc->state = DA_STATE_PROBE;
986 bioq_init(&softc->bio_queue);
987 if (SID_IS_REMOVABLE(&cgd->inq_data))
988 softc->flags |= DA_FLAG_PACK_REMOVABLE;
989 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
990 softc->flags |= DA_FLAG_TAGGED_QUEUING;
992 periph->softc = softc;
994 cam_extend_set(daperiphs, periph->unit_number, periph);
997 * See if this device has any quirks.
999 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1000 (caddr_t)da_quirk_table,
1001 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1002 sizeof(*da_quirk_table), scsi_inquiry_match);
1005 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1007 softc->quirks = DA_Q_NONE;
1009 if (softc->quirks & DA_Q_NO_6_BYTE)
1010 softc->minimum_cmd_size = 10;
1012 softc->minimum_cmd_size = 6;
1015 * Block our timeout handler while we
1016 * add this softc to the dev list.
1019 SLIST_INSERT_HEAD(&softc_list, softc, links);
1023 * The DA driver supports a blocksize, but
1024 * we don't know the blocksize until we do
1025 * a read capacity. So, set a flag to
1026 * indicate that the blocksize is
1027 * unavailable right now. We'll clear the
1028 * flag as soon as we've done a read capacity.
1030 devstat_add_entry(&softc->device_stats, "da",
1031 periph->unit_number, 0,
1032 DEVSTAT_BS_UNAVAILABLE,
1033 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
1034 DEVSTAT_PRIORITY_DISK);
1037 * Register this media as a disk
1039 softc->dev = disk_create(periph->unit_number, &softc->disk, 0,
1040 &da_cdevsw, &dadisk_cdevsw);
1043 * Add async callbacks for bus reset and
1044 * bus device reset calls. I don't bother
1045 * checking if this fails as, in most cases,
1046 * the system will function just fine without
1047 * them and the only alternative would be to
1048 * not attach the device on failure.
1050 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
1051 csa.ccb_h.func_code = XPT_SASYNC_CB;
1052 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
1053 csa.callback = daasync;
1054 csa.callback_arg = periph;
1055 xpt_action((union ccb *)&csa);
1057 * Lock this peripheral until we are setup.
1058 * This first call can't block
1060 (void)cam_periph_lock(periph, PRIBIO);
1061 xpt_schedule(periph, /*priority*/5);
1063 return(CAM_REQ_CMP);
1067 dastart(struct cam_periph *periph, union ccb *start_ccb)
1069 struct da_softc *softc;
1071 softc = (struct da_softc *)periph->softc;
1074 switch (softc->state) {
1075 case DA_STATE_NORMAL:
1077 /* Pull a buffer from the queue and get going on it */
1082 * See if there is a buf with work for us to do..
1085 bp = bioq_first(&softc->bio_queue);
1086 if (periph->immediate_priority <= periph->pinfo.priority) {
1087 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1088 ("queuing for immediate ccb\n"));
1089 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1090 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1092 periph->immediate_priority = CAM_PRIORITY_NONE;
1094 wakeup(&periph->ccb_list);
1095 } else if (bp == NULL) {
1097 xpt_release_ccb(start_ccb);
1102 bioq_remove(&softc->bio_queue, bp);
1104 devstat_start_transaction(&softc->device_stats);
1106 if ((bp->bio_flags & BIO_ORDERED) != 0
1107 || (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1108 softc->flags &= ~DA_FLAG_NEED_OTAG;
1109 softc->ordered_tag_count++;
1110 tag_code = MSG_ORDERED_Q_TAG;
1112 tag_code = MSG_SIMPLE_Q_TAG;
1114 scsi_read_write(&start_ccb->csio,
1118 bp->bio_cmd == BIO_READ,
1120 softc->minimum_cmd_size,
1122 bp->bio_bcount / softc->params.secsize,
1125 /*sense_len*/SSD_FULL_SIZE,
1126 DA_DEFAULT_TIMEOUT * 1000);
1127 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1130 * Block out any asyncronous callbacks
1131 * while we touch the pending ccb list.
1134 LIST_INSERT_HEAD(&softc->pending_ccbs,
1135 &start_ccb->ccb_h, periph_links.le);
1138 /* We expect a unit attention from this device */
1139 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1140 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1141 softc->flags &= ~DA_FLAG_RETRY_UA;
1144 start_ccb->ccb_h.ccb_bp = bp;
1145 bp = bioq_first(&softc->bio_queue);
1148 xpt_action(start_ccb);
1152 /* Have more work to do, so ensure we stay scheduled */
1153 xpt_schedule(periph, /* XXX priority */1);
1157 case DA_STATE_PROBE:
1159 struct ccb_scsiio *csio;
1160 struct scsi_read_capacity_data *rcap;
1162 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
1166 printf("dastart: Couldn't malloc read_capacity data\n");
1167 /* da_free_periph??? */
1170 csio = &start_ccb->csio;
1171 scsi_read_capacity(csio,
1178 start_ccb->ccb_h.ccb_bp = NULL;
1179 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1180 xpt_action(start_ccb);
1188 dadone(struct cam_periph *periph, union ccb *done_ccb)
1190 struct da_softc *softc;
1191 struct ccb_scsiio *csio;
1193 softc = (struct da_softc *)periph->softc;
1194 csio = &done_ccb->csio;
1195 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1196 case DA_CCB_BUFFER_IO:
1201 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1202 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1207 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1212 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1213 if (error == ERESTART) {
1215 * A retry was scheuled, so
1225 if (error == ENXIO) {
1227 * Catastrophic error. Mark our pack as
1230 /* XXX See if this is really a media
1233 xpt_print_path(periph->path);
1234 printf("Invalidating pack\n");
1235 softc->flags |= DA_FLAG_PACK_INVALID;
1239 * return all queued I/O with EIO, so that
1240 * the client can retry these I/Os in the
1241 * proper order should it attempt to recover.
1243 while ((q_bp = bioq_first(&softc->bio_queue))
1245 bioq_remove(&softc->bio_queue, q_bp);
1246 q_bp->bio_resid = q_bp->bio_bcount;
1247 q_bp->bio_error = EIO;
1248 q_bp->bio_flags |= BIO_ERROR;
1252 bp->bio_error = error;
1253 bp->bio_resid = bp->bio_bcount;
1254 bp->bio_flags |= BIO_ERROR;
1256 bp->bio_resid = csio->resid;
1258 if (bp->bio_resid != 0) {
1259 /* Short transfer ??? */
1260 bp->bio_flags |= BIO_ERROR;
1263 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1264 cam_release_devq(done_ccb->ccb_h.path,
1268 /*getcount_only*/0);
1270 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1271 panic("REQ_CMP with QFRZN");
1272 bp->bio_resid = csio->resid;
1273 if (csio->resid > 0)
1274 bp->bio_flags |= BIO_ERROR;
1278 * Block out any asyncronous callbacks
1279 * while we touch the pending ccb list.
1282 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1285 if (softc->device_stats.busy_count == 0)
1286 softc->flags |= DA_FLAG_WENT_IDLE;
1288 devstat_end_transaction_bio(&softc->device_stats, bp);
1294 struct scsi_read_capacity_data *rdcap;
1295 char announce_buf[80];
1297 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1299 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1300 struct disk_params *dp;
1302 dasetgeom(periph, rdcap);
1303 dp = &softc->params;
1304 snprintf(announce_buf, sizeof(announce_buf),
1305 "%luMB (%u %u byte sectors: %dH %dS/T %dC)",
1306 (unsigned long) (((u_int64_t)dp->secsize *
1307 dp->sectors) / (1024*1024)), dp->sectors,
1308 dp->secsize, dp->heads, dp->secs_per_track,
1313 announce_buf[0] = '\0';
1316 * Retry any UNIT ATTENTION type errors. They
1317 * are expected at boot.
1319 error = daerror(done_ccb, CAM_RETRY_SELTO,
1320 SF_RETRY_UA|SF_NO_PRINT);
1321 if (error == ERESTART) {
1323 * A retry was scheuled, so
1327 } else if (error != 0) {
1328 struct scsi_sense_data *sense;
1330 int sense_key, error_code;
1333 struct ccb_getdev cgd;
1335 /* Don't wedge this device's queue */
1336 status = done_ccb->ccb_h.status;
1337 if ((status & CAM_DEV_QFRZN) != 0)
1338 cam_release_devq(done_ccb->ccb_h.path,
1342 /*getcount_only*/0);
1345 xpt_setup_ccb(&cgd.ccb_h,
1346 done_ccb->ccb_h.path,
1348 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1349 xpt_action((union ccb *)&cgd);
1351 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1352 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1353 || ((status & CAM_AUTOSNS_VALID) == 0))
1359 sense = &csio->sense_data;
1360 scsi_extract_sense(sense, &error_code,
1365 * Attach to anything that claims to be a
1366 * direct access or optical disk device,
1367 * as long as it doesn't return a "Logical
1368 * unit not supported" (0x25) error.
1370 if ((have_sense) && (asc != 0x25)
1371 && (error_code == SSD_CURRENT_ERROR)) {
1372 const char *sense_key_desc;
1373 const char *asc_desc;
1375 scsi_sense_desc(sense_key, asc, ascq,
1379 snprintf(announce_buf,
1380 sizeof(announce_buf),
1381 "Attempt to query device "
1382 "size failed: %s, %s",
1390 xpt_print_path(periph->path);
1391 printf("got CAM status %#x\n",
1392 done_ccb->ccb_h.status);
1395 xpt_print_path(periph->path);
1396 printf("fatal error, failed"
1397 " to attach to device\n");
1400 * Free up resources.
1402 cam_periph_invalidate(periph);
1406 free(rdcap, M_TEMP);
1407 if (announce_buf[0] != '\0')
1408 xpt_announce_periph(periph, announce_buf);
1409 softc->state = DA_STATE_NORMAL;
1411 * Since our peripheral may be invalidated by an error
1412 * above or an external event, we must release our CCB
1413 * before releasing the probe lock on the peripheral.
1414 * The peripheral will only go away once the last lock
1415 * is removed, and we need it around for the CCB release
1418 xpt_release_ccb(done_ccb);
1419 cam_periph_unlock(periph);
1422 case DA_CCB_WAITING:
1424 /* Caller will release the CCB */
1425 wakeup(&done_ccb->ccb_h.cbfcnp);
1429 /* No-op. We're polling */
1434 xpt_release_ccb(done_ccb);
1438 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1440 struct da_softc *softc;
1441 struct cam_periph *periph;
1443 periph = xpt_path_periph(ccb->ccb_h.path);
1444 softc = (struct da_softc *)periph->softc;
1448 * Until we have a better way of doing pack validation,
1449 * don't treat UAs as errors.
1451 sense_flags |= SF_RETRY_UA;
1452 return(cam_periph_error(ccb, cam_flags, sense_flags,
1453 &softc->saved_ccb));
1457 daprevent(struct cam_periph *periph, int action)
1459 struct da_softc *softc;
1463 softc = (struct da_softc *)periph->softc;
1465 if (((action == PR_ALLOW)
1466 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
1467 || ((action == PR_PREVENT)
1468 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
1472 ccb = cam_periph_getccb(periph, /*priority*/1);
1474 scsi_prevent(&ccb->csio,
1482 error = cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
1483 /*sense_flags*/0, &softc->device_stats);
1486 if (action == PR_ALLOW)
1487 softc->flags &= ~DA_FLAG_PACK_LOCKED;
1489 softc->flags |= DA_FLAG_PACK_LOCKED;
1492 xpt_release_ccb(ccb);
1496 dasetgeom(struct cam_periph *periph, struct scsi_read_capacity_data * rdcap)
1498 struct ccb_calc_geometry ccg;
1499 struct da_softc *softc;
1500 struct disk_params *dp;
1502 softc = (struct da_softc *)periph->softc;
1504 dp = &softc->params;
1505 dp->secsize = scsi_4btoul(rdcap->length);
1506 dp->sectors = scsi_4btoul(rdcap->addr) + 1;
1508 * Have the controller provide us with a geometry
1509 * for this disk. The only time the geometry
1510 * matters is when we boot and the controller
1511 * is the only one knowledgeable enough to come
1512 * up with something that will make this a bootable
1515 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
1516 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
1517 ccg.block_size = dp->secsize;
1518 ccg.volume_size = dp->sectors;
1520 ccg.secs_per_track = 0;
1522 xpt_action((union ccb*)&ccg);
1523 dp->heads = ccg.heads;
1524 dp->secs_per_track = ccg.secs_per_track;
1525 dp->cylinders = ccg.cylinders;
1529 dasendorderedtag(void *arg)
1531 struct da_softc *softc;
1534 for (softc = SLIST_FIRST(&softc_list);
1536 softc = SLIST_NEXT(softc, links)) {
1538 if ((softc->ordered_tag_count == 0)
1539 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
1540 softc->flags |= DA_FLAG_NEED_OTAG;
1542 if (softc->device_stats.busy_count > 0)
1543 softc->flags &= ~DA_FLAG_WENT_IDLE;
1545 softc->ordered_tag_count = 0;
1548 /* Queue us up again */
1549 timeout(dasendorderedtag, NULL,
1550 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL);
1554 * Step through all DA peripheral drivers, and if the device is still open,
1555 * sync the disk cache to physical media.
1558 dashutdown(void * arg, int howto)
1560 struct cam_periph *periph;
1561 struct da_softc *softc;
1563 TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
1565 softc = (struct da_softc *)periph->softc;
1568 * We only sync the cache if the drive is still open, and
1569 * if the drive is capable of it..
1571 if (((softc->flags & DA_FLAG_OPEN) == 0)
1572 || (softc->quirks & DA_Q_NO_SYNC_CACHE))
1575 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
1577 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
1578 scsi_synchronize_cache(&ccb.csio,
1582 /*begin_lba*/0, /* whole disk */
1587 xpt_polled_action(&ccb);
1589 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1590 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
1591 CAM_SCSI_STATUS_ERROR)
1592 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
1593 int error_code, sense_key, asc, ascq;
1595 scsi_extract_sense(&ccb.csio.sense_data,
1596 &error_code, &sense_key,
1599 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1600 scsi_sense_print(&ccb.csio);
1602 xpt_print_path(periph->path);
1603 printf("Synchronize cache failed, status "
1604 "== 0x%x, scsi status == 0x%x\n",
1605 ccb.ccb_h.status, ccb.csio.scsi_status);
1609 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1610 cam_release_devq(ccb.ccb_h.path,
1614 /*getcount_only*/0);
1619 #else /* !_KERNEL */
1622 * XXX This is only left out of the kernel build to silence warnings. If,
1623 * for some reason this function is used in the kernel, the ifdefs should
1624 * be moved so it is included both in the kernel and userland.
1627 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
1628 void (*cbfcnp)(struct cam_periph *, union ccb *),
1629 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
1630 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
1633 struct scsi_format_unit *scsi_cmd;
1635 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
1636 scsi_cmd->opcode = FORMAT_UNIT;
1637 scsi_cmd->byte2 = byte2;
1638 scsi_ulto2b(ileave, scsi_cmd->interleave);
1643 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
1652 #endif /* _KERNEL */