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(struct da_softc) links;
121 LIST_HEAD(, struct ccb_hdr) pending_ccbs;
125 int minimum_cmd_size;
126 int ordered_tag_count;
127 struct disk_params params;
132 struct da_quirk_entry {
133 struct scsi_inquiry_pattern inq_pat;
137 static struct da_quirk_entry da_quirk_table[] =
141 * This particular Fujitsu drive doesn't like the
142 * synchronize cache command.
143 * Reported by: Tom Jackson <toj@gorilla.net>
145 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
146 /*quirks*/ DA_Q_NO_SYNC_CACHE
151 * This drive doesn't like the synchronize cache command
152 * either. Reported by: Matthew Jacob <mjacob@feral.com>
153 * in NetBSD PR kern/6027, August 24, 1998.
155 {T_DIRECT, SIP_MEDIA_FIXED, "MICROP", "2217*", "*"},
156 /*quirks*/ DA_Q_NO_SYNC_CACHE
160 * This drive doesn't like the synchronize cache command
161 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
164 {T_DIRECT, SIP_MEDIA_FIXED, "MICROP", "2112*", "*"},
165 /*quirks*/ DA_Q_NO_SYNC_CACHE
169 * Doesn't like the synchronize cache command.
170 * Reported by: Blaz Zupan <blaz@gold.amis.net>
172 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
173 /*quirks*/ DA_Q_NO_SYNC_CACHE
177 * Doesn't work correctly with 6 byte reads/writes.
178 * Returns illegal request, and points to byte 9 of the
180 * Reported by: Adam McDougall <bsdx@spawnet.com>
182 {T_DIRECT, SIP_MEDIA_FIXED, "QUANTUM", "VIKING 4*", "*"},
183 /*quirks*/ DA_Q_NO_6_BYTE
189 {T_DIRECT, SIP_MEDIA_FIXED, "QUANTUM", "VIKING 2*", "*"},
190 /*quirks*/ DA_Q_NO_6_BYTE
194 * This USB floppy drive uses the UFI command set. This
195 * command set is a derivative of the ATAPI command set and
196 * does not support READ_6 commands only READ_10. It also does
197 * not support sync cache (0x35).
199 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Y-E DATA", "USB-FDU", "*"},
200 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
204 static d_open_t daopen;
205 static d_close_t daclose;
206 static d_strategy_t dastrategy;
207 static d_ioctl_t daioctl;
208 static d_dump_t dadump;
209 static periph_init_t dainit;
210 static void daasync(void *callback_arg, u_int32_t code,
211 struct cam_path *path, void *arg);
212 static periph_ctor_t daregister;
213 static periph_dtor_t dacleanup;
214 static periph_start_t dastart;
215 static periph_oninv_t daoninvalidate;
216 static void dadone(struct cam_periph *periph,
217 union ccb *done_ccb);
218 static int daerror(union ccb *ccb, u_int32_t cam_flags,
219 u_int32_t sense_flags);
220 static void daprevent(struct cam_periph *periph, int action);
221 static void dasetgeom(struct cam_periph *periph,
222 struct scsi_read_capacity_data * rdcap);
223 static timeout_t dasendorderedtag;
224 static void dashutdown(void *arg, int howto);
226 #ifndef DA_DEFAULT_TIMEOUT
227 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
231 * DA_ORDEREDTAG_INTERVAL determines how often, relative
232 * to the default timeout, we check to see whether an ordered
233 * tagged transaction is appropriate to prevent simple tag
234 * starvation. Since we'd like to ensure that there is at least
235 * 1/2 of the timeout length left for a starved transaction to
236 * complete after we've sent an ordered tag, we must poll at least
237 * four times in every timeout period. This takes care of the worst
238 * case where a starved transaction starts during an interval that
239 * meets the requirement "don't send an ordered tag" test so it takes
240 * us two intervals to determine that a tag must be sent.
242 #ifndef DA_ORDEREDTAG_INTERVAL
243 #define DA_ORDEREDTAG_INTERVAL 4
246 static struct periph_driver dadriver =
249 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
252 DATA_SET(periphdriver_set, dadriver);
254 #define DA_CDEV_MAJOR 13
255 #define DA_BDEV_MAJOR 4
257 /* For 2.2-stable support */
262 static struct cdevsw da_cdevsw = {
266 /* write */ physwrite,
270 /* strategy */ dastrategy,
272 /* maj */ DA_CDEV_MAJOR,
276 /* bmaj */ DA_BDEV_MAJOR
279 static struct cdevsw dadisk_cdevsw;
281 static SLIST_HEAD(, struct da_softc) softc_list;
282 static struct extend_array *daperiphs;
285 daopen(dev_t dev, int flags, int fmt, struct proc *p)
287 struct cam_periph *periph;
288 struct da_softc *softc;
289 struct disklabel *label;
297 periph = cam_extend_get(daperiphs, unit);
301 softc = (struct da_softc *)periph->softc;
303 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
304 ("daopen: dev=%s (unit %d , partition %d)\n", devtoname(dev),
307 if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) {
308 return (error); /* error code from tsleep */
311 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
313 softc->flags |= DA_FLAG_OPEN;
316 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
317 /* Invalidate our pack information. */
318 disk_invalidate(&softc->disk);
319 softc->flags &= ~DA_FLAG_PACK_INVALID;
323 /* Do a read capacity */
325 struct scsi_read_capacity_data *rcap;
328 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
332 ccb = cam_periph_getccb(periph, /*priority*/1);
333 scsi_read_capacity(&ccb->csio,
340 ccb->ccb_h.ccb_bp = NULL;
342 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
343 /*sense_flags*/SF_RETRY_UA |
345 &softc->device_stats);
347 xpt_release_ccb(ccb);
350 dasetgeom(periph, rcap);
357 struct ccb_getdev cgd;
359 /* Build label for whole disk. */
360 label = &softc->disk.d_label;
361 bzero(label, sizeof(*label));
362 label->d_type = DTYPE_SCSI;
365 * Grab the inquiry data to get the vendor and product names.
366 * Put them in the typename and packname for the label.
368 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
369 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
370 xpt_action((union ccb *)&cgd);
372 strncpy(label->d_typename, cgd.inq_data.vendor,
373 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
374 strncpy(label->d_packname, cgd.inq_data.product,
375 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
377 label->d_secsize = softc->params.secsize;
378 label->d_nsectors = softc->params.secs_per_track;
379 label->d_ntracks = softc->params.heads;
380 label->d_ncylinders = softc->params.cylinders;
381 label->d_secpercyl = softc->params.heads
382 * softc->params.secs_per_track;
383 label->d_secperunit = softc->params.sectors;
385 if (((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0)) {
386 daprevent(periph, PR_PREVENT);
390 * Check to see whether or not the blocksize is set yet.
391 * If it isn't, set it and then clear the blocksize
392 * unavailable flag for the device statistics.
394 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0){
395 softc->device_stats.block_size = softc->params.secsize;
396 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
401 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
402 daprevent(periph, PR_ALLOW);
405 cam_periph_unlock(periph);
410 daclose(dev_t dev, int flag, int fmt, struct proc *p)
412 struct cam_periph *periph;
413 struct da_softc *softc;
418 periph = cam_extend_get(daperiphs, unit);
422 softc = (struct da_softc *)periph->softc;
424 if ((error = cam_periph_lock(periph, PRIBIO)) != 0) {
425 return (error); /* error code from tsleep */
428 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
431 ccb = cam_periph_getccb(periph, /*priority*/1);
433 scsi_synchronize_cache(&ccb->csio,
437 /*begin_lba*/0,/* Cover the whole disk */
442 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
443 /*sense_flags*/SF_RETRY_UA,
444 &softc->device_stats);
446 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
447 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
448 CAM_SCSI_STATUS_ERROR) {
450 int sense_key, error_code;
452 scsi_extract_sense(&ccb->csio.sense_data,
456 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
457 scsi_sense_print(&ccb->csio);
459 xpt_print_path(periph->path);
460 printf("Synchronize cache failed, status "
461 "== 0x%x, scsi status == 0x%x\n",
462 ccb->csio.ccb_h.status,
463 ccb->csio.scsi_status);
467 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
468 cam_release_devq(ccb->ccb_h.path,
474 xpt_release_ccb(ccb);
478 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
479 daprevent(periph, PR_ALLOW);
481 * If we've got removeable media, mark the blocksize as
482 * unavailable, since it could change when new media is
485 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
488 softc->flags &= ~DA_FLAG_OPEN;
489 cam_periph_unlock(periph);
490 cam_periph_release(periph);
495 * Actually translate the requested transfer into one the physical driver
496 * can understand. The transfer is described by a buf and will include
497 * only one physical transfer.
500 dastrategy(struct bio *bp)
502 struct cam_periph *periph;
503 struct da_softc *softc;
508 unit = dkunit(bp->bio_dev);
509 part = dkpart(bp->bio_dev);
510 periph = cam_extend_get(daperiphs, unit);
511 if (periph == NULL) {
512 bp->bio_error = ENXIO;
515 softc = (struct da_softc *)periph->softc;
518 * check it's not too big a transfer for our adapter
520 scsi_minphys(bp,&sd_switch);
524 * Mask interrupts so that the pack cannot be invalidated until
525 * after we are in the queue. Otherwise, we might not properly
526 * clean up one of the buffers.
531 * If the device has been made invalid, error out
533 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
535 bp->bio_error = ENXIO;
540 * Place it in the queue of disk activities for this disk
542 bioqdisksort(&softc->bio_queue, bp);
547 * Schedule ourselves for performing the work.
549 xpt_schedule(periph, /* XXX priority */1);
553 bp->bio_flags |= BIO_ERROR;
556 * Correctly set the buf to indicate a completed xfer
558 bp->bio_resid = bp->bio_bcount;
563 /* For 2.2-stable support */
569 daioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
571 struct cam_periph *periph;
572 struct da_softc *softc;
577 periph = cam_extend_get(daperiphs, unit);
581 softc = (struct da_softc *)periph->softc;
583 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("daioctl\n"));
585 if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) {
586 return (error); /* error code from tsleep */
589 error = cam_periph_ioctl(periph, cmd, addr, daerror);
591 cam_periph_unlock(periph);
599 struct cam_periph *periph;
600 struct da_softc *softc;
604 u_int num; /* number of sectors to write */
608 struct ccb_scsiio csio;
611 /* toss any characters present prior to dump */
612 while (cncheckc() != -1)
617 periph = cam_extend_get(daperiphs, unit);
618 if (periph == NULL) {
621 softc = (struct da_softc *)periph->softc;
623 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0)
626 error = disk_dumpcheck(dev, &num, &blknum, &secsize);
630 addr = 0; /* starting address */
631 blkcnt = howmany(PAGE_SIZE, secsize);
635 if (is_physical_memory(addr)) {
636 pmap_kenter((vm_offset_t)CADDR1, trunc_page(addr));
638 pmap_kenter((vm_offset_t)CADDR1, trunc_page(0));
641 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
642 csio.ccb_h.ccb_state = DA_CCB_DUMP;
643 scsi_read_write(&csio,
649 /*minimum_cmd_size*/ softc->minimum_cmd_size,
653 /*dxfer_len*/blkcnt * secsize,
654 /*sense_len*/SSD_FULL_SIZE,
655 DA_DEFAULT_TIMEOUT * 1000);
656 xpt_polled_action((union ccb *)&csio);
658 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
659 printf("Aborting dump due to I/O error.\n");
660 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
661 CAM_SCSI_STATUS_ERROR)
662 scsi_sense_print(&csio);
664 printf("status == 0x%x, scsi status == 0x%x\n",
665 csio.ccb_h.status, csio.scsi_status);
669 if (addr % (1024 * 1024) == 0) {
674 /* Count in MB of data left to write */
675 printf("%d ", (num * softc->params.secsize)
679 /* update block count */
684 /* operator aborting dump? */
685 if (cncheckc() != -1)
690 * Sync the disk cache contents to the physical media.
692 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
694 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
695 csio.ccb_h.ccb_state = DA_CCB_DUMP;
696 scsi_synchronize_cache(&csio,
700 /*begin_lba*/0,/* Cover the whole disk */
704 xpt_polled_action((union ccb *)&csio);
706 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
707 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
708 CAM_SCSI_STATUS_ERROR) {
710 int sense_key, error_code;
712 scsi_extract_sense(&csio.sense_data,
716 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
717 scsi_sense_print(&csio);
719 xpt_print_path(periph->path);
720 printf("Synchronize cache failed, status "
721 "== 0x%x, scsi status == 0x%x\n",
722 csio.ccb_h.status, csio.scsi_status);
733 struct cam_path *path;
736 * Create our extend array for storing the devices we attach to.
738 daperiphs = cam_extend_new();
739 SLIST_INIT(&softc_list);
740 if (daperiphs == NULL) {
741 printf("da: Failed to alloc extend array!\n");
746 * Install a global async callback. This callback will
747 * receive async callbacks like "new device found".
749 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
750 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
752 if (status == CAM_REQ_CMP) {
753 struct ccb_setasync csa;
755 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
756 csa.ccb_h.func_code = XPT_SASYNC_CB;
757 csa.event_enable = AC_FOUND_DEVICE;
758 csa.callback = daasync;
759 csa.callback_arg = NULL;
760 xpt_action((union ccb *)&csa);
761 status = csa.ccb_h.status;
765 if (status != CAM_REQ_CMP) {
766 printf("da: Failed to attach master async callback "
767 "due to status 0x%x!\n", status);
771 * Schedule a periodic event to occasioanly send an
772 * ordered tag to a device.
774 timeout(dasendorderedtag, NULL,
775 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL);
777 /* Register our shutdown event handler */
778 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
779 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
780 printf("dainit: shutdown event registration failed!\n");
785 daoninvalidate(struct cam_periph *periph)
788 struct da_softc *softc;
790 struct ccb_setasync csa;
792 softc = (struct da_softc *)periph->softc;
795 * De-register any async callbacks.
797 xpt_setup_ccb(&csa.ccb_h, periph->path,
799 csa.ccb_h.func_code = XPT_SASYNC_CB;
800 csa.event_enable = 0;
801 csa.callback = daasync;
802 csa.callback_arg = periph;
803 xpt_action((union ccb *)&csa);
805 softc->flags |= DA_FLAG_PACK_INVALID;
808 * Although the oninvalidate() routines are always called at
809 * splsoftcam, we need to be at splbio() here to keep the buffer
810 * queue from being modified while we traverse it.
815 * Return all queued I/O with ENXIO.
816 * XXX Handle any transactions queued to the card
817 * with XPT_ABORT_CCB.
819 while ((q_bp = bioq_first(&softc->bio_queue)) != NULL){
820 bioq_remove(&softc->bio_queue, q_bp);
821 q_bp->bio_resid = q_bp->bio_bcount;
822 q_bp->bio_error = ENXIO;
823 q_bp->bio_flags |= BIO_ERROR;
828 SLIST_REMOVE(&softc_list, softc, struct da_softc, links);
830 xpt_print_path(periph->path);
831 printf("lost device\n");
835 dacleanup(struct cam_periph *periph)
837 struct da_softc *softc;
839 softc = (struct da_softc *)periph->softc;
841 devstat_remove_entry(&softc->device_stats);
842 cam_extend_release(daperiphs, periph->unit_number);
843 xpt_print_path(periph->path);
844 printf("removing device entry\n");
845 free(softc, M_DEVBUF);
849 daasync(void *callback_arg, u_int32_t code,
850 struct cam_path *path, void *arg)
852 struct cam_periph *periph;
854 periph = (struct cam_periph *)callback_arg;
856 case AC_FOUND_DEVICE:
858 struct ccb_getdev *cgd;
861 cgd = (struct ccb_getdev *)arg;
863 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
864 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
868 * Allocate a peripheral instance for
869 * this device and start the probe
872 status = cam_periph_alloc(daregister, daoninvalidate,
874 "da", CAM_PERIPH_BIO,
875 cgd->ccb_h.path, daasync,
876 AC_FOUND_DEVICE, cgd);
878 if (status != CAM_REQ_CMP
879 && status != CAM_REQ_INPROG)
880 printf("daasync: Unable to attach to new device "
881 "due to status 0x%x\n", status);
887 struct da_softc *softc;
888 struct ccb_hdr *ccbh;
891 softc = (struct da_softc *)periph->softc;
894 * Don't fail on the expected unit attention
897 softc->flags |= DA_FLAG_RETRY_UA;
898 for (ccbh = LIST_FIRST(&softc->pending_ccbs);
899 ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le))
900 ccbh->ccb_state |= DA_CCB_RETRY_UA;
905 cam_periph_async(periph, code, path, arg);
911 daregister(struct cam_periph *periph, void *arg)
914 struct da_softc *softc;
915 struct ccb_setasync csa;
916 struct ccb_getdev *cgd;
919 cgd = (struct ccb_getdev *)arg;
920 if (periph == NULL) {
921 printf("daregister: periph was NULL!!\n");
922 return(CAM_REQ_CMP_ERR);
926 printf("daregister: no getdev CCB, can't register device\n");
927 return(CAM_REQ_CMP_ERR);
930 softc = (struct da_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
933 printf("daregister: Unable to probe new device. "
934 "Unable to allocate softc\n");
935 return(CAM_REQ_CMP_ERR);
938 bzero(softc, sizeof(*softc));
939 LIST_INIT(&softc->pending_ccbs);
940 softc->state = DA_STATE_PROBE;
941 bioq_init(&softc->bio_queue);
942 if (SID_IS_REMOVABLE(&cgd->inq_data))
943 softc->flags |= DA_FLAG_PACK_REMOVABLE;
944 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
945 softc->flags |= DA_FLAG_TAGGED_QUEUING;
947 periph->softc = softc;
949 cam_extend_set(daperiphs, periph->unit_number, periph);
952 * See if this device has any quirks.
954 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
955 (caddr_t)da_quirk_table,
956 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
957 sizeof(*da_quirk_table), scsi_inquiry_match);
960 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
962 softc->quirks = DA_Q_NONE;
964 if (softc->quirks & DA_Q_NO_6_BYTE)
965 softc->minimum_cmd_size = 10;
967 softc->minimum_cmd_size = 6;
970 * Block our timeout handler while we
971 * add this softc to the dev list.
974 SLIST_INSERT_HEAD(&softc_list, softc, links);
978 * The DA driver supports a blocksize, but
979 * we don't know the blocksize until we do
980 * a read capacity. So, set a flag to
981 * indicate that the blocksize is
982 * unavailable right now. We'll clear the
983 * flag as soon as we've done a read capacity.
985 devstat_add_entry(&softc->device_stats, "da",
986 periph->unit_number, 0,
987 DEVSTAT_BS_UNAVAILABLE,
988 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
989 DEVSTAT_PRIORITY_DISK);
992 * Register this media as a disk
994 disk_create(periph->unit_number, &softc->disk, 0,
995 &da_cdevsw, &dadisk_cdevsw);
998 * Add async callbacks for bus reset and
999 * bus device reset calls. I don't bother
1000 * checking if this fails as, in most cases,
1001 * the system will function just fine without
1002 * them and the only alternative would be to
1003 * not attach the device on failure.
1005 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
1006 csa.ccb_h.func_code = XPT_SASYNC_CB;
1007 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
1008 csa.callback = daasync;
1009 csa.callback_arg = periph;
1010 xpt_action((union ccb *)&csa);
1012 * Lock this peripheral until we are setup.
1013 * This first call can't block
1015 (void)cam_periph_lock(periph, PRIBIO);
1016 xpt_schedule(periph, /*priority*/5);
1018 return(CAM_REQ_CMP);
1022 dastart(struct cam_periph *periph, union ccb *start_ccb)
1024 struct da_softc *softc;
1026 softc = (struct da_softc *)periph->softc;
1029 switch (softc->state) {
1030 case DA_STATE_NORMAL:
1032 /* Pull a buffer from the queue and get going on it */
1037 * See if there is a buf with work for us to do..
1040 bp = bioq_first(&softc->bio_queue);
1041 if (periph->immediate_priority <= periph->pinfo.priority) {
1042 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1043 ("queuing for immediate ccb\n"));
1044 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1045 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1047 periph->immediate_priority = CAM_PRIORITY_NONE;
1049 wakeup(&periph->ccb_list);
1050 } else if (bp == NULL) {
1052 xpt_release_ccb(start_ccb);
1057 bioq_remove(&softc->bio_queue, bp);
1059 devstat_start_transaction(&softc->device_stats);
1061 if ((bp->bio_flags & BIO_ORDERED) != 0
1062 || (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1063 softc->flags &= ~DA_FLAG_NEED_OTAG;
1064 softc->ordered_tag_count++;
1065 tag_code = MSG_ORDERED_Q_TAG;
1067 tag_code = MSG_SIMPLE_Q_TAG;
1069 scsi_read_write(&start_ccb->csio,
1073 bp->bio_cmd == BIO_READ,
1075 softc->minimum_cmd_size,
1077 bp->bio_bcount / softc->params.secsize,
1080 /*sense_len*/SSD_FULL_SIZE,
1081 DA_DEFAULT_TIMEOUT * 1000);
1082 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1085 * Block out any asyncronous callbacks
1086 * while we touch the pending ccb list.
1089 LIST_INSERT_HEAD(&softc->pending_ccbs,
1090 &start_ccb->ccb_h, periph_links.le);
1093 /* We expect a unit attention from this device */
1094 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1095 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1096 softc->flags &= ~DA_FLAG_RETRY_UA;
1099 start_ccb->ccb_h.ccb_bp = bp;
1100 bp = bioq_first(&softc->bio_queue);
1103 xpt_action(start_ccb);
1107 /* Have more work to do, so ensure we stay scheduled */
1108 xpt_schedule(periph, /* XXX priority */1);
1112 case DA_STATE_PROBE:
1114 struct ccb_scsiio *csio;
1115 struct scsi_read_capacity_data *rcap;
1117 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
1121 printf("dastart: Couldn't malloc read_capacity data\n");
1122 /* da_free_periph??? */
1125 csio = &start_ccb->csio;
1126 scsi_read_capacity(csio,
1133 start_ccb->ccb_h.ccb_bp = NULL;
1134 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1135 xpt_action(start_ccb);
1143 dadone(struct cam_periph *periph, union ccb *done_ccb)
1145 struct da_softc *softc;
1146 struct ccb_scsiio *csio;
1148 softc = (struct da_softc *)periph->softc;
1149 csio = &done_ccb->csio;
1150 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1151 case DA_CCB_BUFFER_IO:
1156 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1157 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1162 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1167 /* Retry selection timeouts */
1168 sf |= SF_RETRY_SELTO;
1170 if ((error = daerror(done_ccb, 0, sf)) == ERESTART) {
1172 * A retry was scheuled, so
1182 if (error == ENXIO) {
1184 * Catastrophic error. Mark our pack as
1187 /* XXX See if this is really a media
1190 xpt_print_path(periph->path);
1191 printf("Invalidating pack\n");
1192 softc->flags |= DA_FLAG_PACK_INVALID;
1196 * return all queued I/O with EIO, so that
1197 * the client can retry these I/Os in the
1198 * proper order should it attempt to recover.
1200 while ((q_bp = bioq_first(&softc->bio_queue))
1202 bioq_remove(&softc->bio_queue, q_bp);
1203 q_bp->bio_resid = q_bp->bio_bcount;
1204 q_bp->bio_error = EIO;
1205 q_bp->bio_flags |= BIO_ERROR;
1209 bp->bio_error = error;
1210 bp->bio_resid = bp->bio_bcount;
1211 bp->bio_flags |= BIO_ERROR;
1213 bp->bio_resid = csio->resid;
1215 if (bp->bio_resid != 0) {
1216 /* Short transfer ??? */
1217 bp->bio_flags |= BIO_ERROR;
1220 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1221 cam_release_devq(done_ccb->ccb_h.path,
1225 /*getcount_only*/0);
1227 bp->bio_resid = csio->resid;
1228 if (csio->resid > 0)
1229 bp->bio_flags |= BIO_ERROR;
1233 * Block out any asyncronous callbacks
1234 * while we touch the pending ccb list.
1237 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1240 if (softc->device_stats.busy_count == 0)
1241 softc->flags |= DA_FLAG_WENT_IDLE;
1243 devstat_end_transaction_bio(&softc->device_stats, bp);
1249 struct scsi_read_capacity_data *rdcap;
1250 char announce_buf[80];
1252 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1254 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1255 struct disk_params *dp;
1257 dasetgeom(periph, rdcap);
1258 dp = &softc->params;
1259 snprintf(announce_buf, sizeof(announce_buf),
1260 "%luMB (%u %u byte sectors: %dH %dS/T %dC)",
1261 (unsigned long) (((u_int64_t)dp->secsize *
1262 dp->sectors) / (1024*1024)), dp->sectors,
1263 dp->secsize, dp->heads, dp->secs_per_track,
1268 announce_buf[0] = '\0';
1271 * Retry any UNIT ATTENTION type errors. They
1272 * are expected at boot.
1274 error = daerror(done_ccb, 0, SF_RETRY_UA |
1275 SF_RETRY_SELTO | SF_NO_PRINT);
1276 if (error == ERESTART) {
1278 * A retry was scheuled, so
1282 } else if (error != 0) {
1283 struct scsi_sense_data *sense;
1285 int sense_key, error_code;
1288 struct ccb_getdev cgd;
1290 /* Don't wedge this device's queue */
1291 cam_release_devq(done_ccb->ccb_h.path,
1295 /*getcount_only*/0);
1297 status = done_ccb->ccb_h.status;
1299 xpt_setup_ccb(&cgd.ccb_h,
1300 done_ccb->ccb_h.path,
1302 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1303 xpt_action((union ccb *)&cgd);
1305 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1306 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1307 || ((status & CAM_AUTOSNS_VALID) == 0))
1313 sense = &csio->sense_data;
1314 scsi_extract_sense(sense, &error_code,
1319 * Attach to anything that claims to be a
1320 * direct access or optical disk device,
1321 * as long as it doesn't return a "Logical
1322 * unit not supported" (0x25) error.
1324 if ((have_sense) && (asc != 0x25)
1325 && (error_code == SSD_CURRENT_ERROR))
1326 snprintf(announce_buf,
1327 sizeof(announce_buf),
1328 "Attempt to query device "
1329 "size failed: %s, %s",
1330 scsi_sense_key_text[sense_key],
1331 scsi_sense_desc(asc,ascq,
1338 xpt_print_path(periph->path);
1339 printf("got CAM status %#x\n",
1340 done_ccb->ccb_h.status);
1343 xpt_print_path(periph->path);
1344 printf("fatal error, failed"
1345 " to attach to device\n");
1348 * Free up resources.
1350 cam_periph_invalidate(periph);
1354 free(rdcap, M_TEMP);
1355 if (announce_buf[0] != '\0')
1356 xpt_announce_periph(periph, announce_buf);
1357 softc->state = DA_STATE_NORMAL;
1359 * Since our peripheral may be invalidated by an error
1360 * above or an external event, we must release our CCB
1361 * before releasing the probe lock on the peripheral.
1362 * The peripheral will only go away once the last lock
1363 * is removed, and we need it around for the CCB release
1366 xpt_release_ccb(done_ccb);
1367 cam_periph_unlock(periph);
1370 case DA_CCB_WAITING:
1372 /* Caller will release the CCB */
1373 wakeup(&done_ccb->ccb_h.cbfcnp);
1377 /* No-op. We're polling */
1382 xpt_release_ccb(done_ccb);
1386 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1388 struct da_softc *softc;
1389 struct cam_periph *periph;
1391 periph = xpt_path_periph(ccb->ccb_h.path);
1392 softc = (struct da_softc *)periph->softc;
1396 * Until we have a better way of doing pack validation,
1397 * don't treat UAs as errors.
1399 sense_flags |= SF_RETRY_UA;
1400 return(cam_periph_error(ccb, cam_flags, sense_flags,
1401 &softc->saved_ccb));
1405 daprevent(struct cam_periph *periph, int action)
1407 struct da_softc *softc;
1411 softc = (struct da_softc *)periph->softc;
1413 if (((action == PR_ALLOW)
1414 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
1415 || ((action == PR_PREVENT)
1416 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
1420 ccb = cam_periph_getccb(periph, /*priority*/1);
1422 scsi_prevent(&ccb->csio,
1430 error = cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
1431 /*sense_flags*/0, &softc->device_stats);
1434 if (action == PR_ALLOW)
1435 softc->flags &= ~DA_FLAG_PACK_LOCKED;
1437 softc->flags |= DA_FLAG_PACK_LOCKED;
1440 xpt_release_ccb(ccb);
1444 dasetgeom(struct cam_periph *periph, struct scsi_read_capacity_data * rdcap)
1446 struct ccb_calc_geometry ccg;
1447 struct da_softc *softc;
1448 struct disk_params *dp;
1450 softc = (struct da_softc *)periph->softc;
1452 dp = &softc->params;
1453 dp->secsize = scsi_4btoul(rdcap->length);
1454 dp->sectors = scsi_4btoul(rdcap->addr) + 1;
1456 * Have the controller provide us with a geometry
1457 * for this disk. The only time the geometry
1458 * matters is when we boot and the controller
1459 * is the only one knowledgeable enough to come
1460 * up with something that will make this a bootable
1463 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
1464 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
1465 ccg.block_size = dp->secsize;
1466 ccg.volume_size = dp->sectors;
1468 ccg.secs_per_track = 0;
1470 xpt_action((union ccb*)&ccg);
1471 dp->heads = ccg.heads;
1472 dp->secs_per_track = ccg.secs_per_track;
1473 dp->cylinders = ccg.cylinders;
1477 dasendorderedtag(void *arg)
1479 struct da_softc *softc;
1482 for (softc = SLIST_FIRST(&softc_list);
1484 softc = SLIST_NEXT(softc, links)) {
1486 if ((softc->ordered_tag_count == 0)
1487 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
1488 softc->flags |= DA_FLAG_NEED_OTAG;
1490 if (softc->device_stats.busy_count > 0)
1491 softc->flags &= ~DA_FLAG_WENT_IDLE;
1493 softc->ordered_tag_count = 0;
1496 /* Queue us up again */
1497 timeout(dasendorderedtag, NULL,
1498 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL);
1502 * Step through all DA peripheral drivers, and if the device is still open,
1503 * sync the disk cache to physical media.
1506 dashutdown(void * arg, int howto)
1508 struct cam_periph *periph;
1509 struct da_softc *softc;
1511 for (periph = TAILQ_FIRST(&dadriver.units); periph != NULL;
1512 periph = TAILQ_NEXT(periph, unit_links)) {
1514 softc = (struct da_softc *)periph->softc;
1517 * We only sync the cache if the drive is still open, and
1518 * if the drive is capable of it..
1520 if (((softc->flags & DA_FLAG_OPEN) == 0)
1521 || (softc->quirks & DA_Q_NO_SYNC_CACHE))
1524 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
1526 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
1527 scsi_synchronize_cache(&ccb.csio,
1531 /*begin_lba*/0, /* whole disk */
1536 xpt_polled_action(&ccb);
1538 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1539 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
1540 CAM_SCSI_STATUS_ERROR)
1541 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
1542 int error_code, sense_key, asc, ascq;
1544 scsi_extract_sense(&ccb.csio.sense_data,
1545 &error_code, &sense_key,
1548 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1549 scsi_sense_print(&ccb.csio);
1551 xpt_print_path(periph->path);
1552 printf("Synchronize cache failed, status "
1553 "== 0x%x, scsi status == 0x%x\n",
1554 ccb.ccb_h.status, ccb.csio.scsi_status);
1558 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1559 cam_release_devq(ccb.ccb_h.path,
1563 /*getcount_only*/0);
1568 #else /* !_KERNEL */
1571 * XXX This is only left out of the kernel build to silence warnings. If,
1572 * for some reason this function is used in the kernel, the ifdefs should
1573 * be moved so it is included both in the kernel and userland.
1576 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
1577 void (*cbfcnp)(struct cam_periph *, union ccb *),
1578 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
1579 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
1582 struct scsi_format_unit *scsi_cmd;
1584 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
1585 scsi_cmd->opcode = FORMAT_UNIT;
1586 scsi_cmd->byte2 = byte2;
1587 scsi_ulto2b(ileave, scsi_cmd->interleave);
1592 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
1601 #endif /* _KERNEL */