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
31 #include "opt_hw_wdog.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/devicestat.h>
40 #include <sys/eventhandler.h>
41 #include <sys/malloc.h>
44 #include <machine/md_var.h>
50 #include <cam/cam_ccb.h>
51 #include <cam/cam_extend.h>
52 #include <cam/cam_periph.h>
53 #include <cam/cam_xpt_periph.h>
55 #include <cam/scsi/scsi_message.h>
63 DA_FLAG_PACK_INVALID = 0x001,
64 DA_FLAG_NEW_PACK = 0x002,
65 DA_FLAG_PACK_LOCKED = 0x004,
66 DA_FLAG_PACK_REMOVABLE = 0x008,
67 DA_FLAG_TAGGED_QUEUING = 0x010,
68 DA_FLAG_NEED_OTAG = 0x020,
69 DA_FLAG_WENT_IDLE = 0x040,
70 DA_FLAG_RETRY_UA = 0x080,
76 DA_Q_NO_SYNC_CACHE = 0x01,
82 DA_CCB_BUFFER_IO = 0x02,
83 DA_CCB_WAITING = 0x03,
85 DA_CCB_TYPE_MASK = 0x0F,
86 DA_CCB_RETRY_UA = 0x10
89 /* Offsets into our private area for storing information */
90 #define ccb_state ppriv_field0
91 #define ccb_bp ppriv_ptr1
96 u_int8_t secs_per_track;
97 u_int32_t secsize; /* Number of bytes/sector */
98 u_int32_t sectors; /* total number sectors */
102 struct buf_queue_head buf_queue;
103 struct devstat device_stats;
104 SLIST_ENTRY(da_softc) links;
105 LIST_HEAD(, ccb_hdr) pending_ccbs;
109 int minimum_cmd_size;
110 int ordered_tag_count;
111 struct disk_params params;
116 struct da_quirk_entry {
117 struct scsi_inquiry_pattern inq_pat;
121 static struct da_quirk_entry da_quirk_table[] =
125 * This particular Fujitsu drive doesn't like the
126 * synchronize cache command.
127 * Reported by: Tom Jackson <toj@gorilla.net>
129 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
130 /*quirks*/ DA_Q_NO_SYNC_CACHE
135 * This drive doesn't like the synchronize cache command
136 * either. Reported by: Matthew Jacob <mjacob@feral.com>
137 * in NetBSD PR kern/6027, August 24, 1998.
139 {T_DIRECT, SIP_MEDIA_FIXED, "MICROP", "2217*", "*"},
140 /*quirks*/ DA_Q_NO_SYNC_CACHE
144 * This drive doesn't like the synchronize cache command
145 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
148 {T_DIRECT, SIP_MEDIA_FIXED, "MICROP", "2112*", "*"},
149 /*quirks*/ DA_Q_NO_SYNC_CACHE
153 * Doesn't like the synchronize cache command.
154 * Reported by: Blaz Zupan <blaz@gold.amis.net>
156 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
157 /*quirks*/ DA_Q_NO_SYNC_CACHE
161 * Doesn't work correctly with 6 byte reads/writes.
162 * Returns illegal request, and points to byte 9 of the
164 * Reported by: Adam McDougall <bsdx@spawnet.com>
166 {T_DIRECT, SIP_MEDIA_FIXED, "QUANTUM", "VIKING 4*", "*"},
167 /*quirks*/ DA_Q_NO_6_BYTE
173 {T_DIRECT, SIP_MEDIA_FIXED, "QUANTUM", "VIKING 2*", "*"},
174 /*quirks*/ DA_Q_NO_6_BYTE
178 * This USB floppy drive uses the UFI command set. This
179 * command set is a derivative of the ATAPI command set and
180 * does not support READ_6 commands only READ_10. It also does
181 * not support sync cache (0x35).
183 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Y-E DATA", "USB-FDU", "*"},
184 /*quirks*/ DA_Q_NO_6_BYTE|DA_Q_NO_SYNC_CACHE
188 static d_open_t daopen;
189 static d_close_t daclose;
190 static d_strategy_t dastrategy;
191 static d_ioctl_t daioctl;
192 static d_dump_t dadump;
193 static periph_init_t dainit;
194 static void daasync(void *callback_arg, u_int32_t code,
195 struct cam_path *path, void *arg);
196 static periph_ctor_t daregister;
197 static periph_dtor_t dacleanup;
198 static periph_start_t dastart;
199 static periph_oninv_t daoninvalidate;
200 static void dadone(struct cam_periph *periph,
201 union ccb *done_ccb);
202 static int daerror(union ccb *ccb, u_int32_t cam_flags,
203 u_int32_t sense_flags);
204 static void daprevent(struct cam_periph *periph, int action);
205 static void dasetgeom(struct cam_periph *periph,
206 struct scsi_read_capacity_data * rdcap);
207 static timeout_t dasendorderedtag;
208 static void dashutdown(void *arg, int howto);
210 #ifndef DA_DEFAULT_TIMEOUT
211 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
215 * DA_ORDEREDTAG_INTERVAL determines how often, relative
216 * to the default timeout, we check to see whether an ordered
217 * tagged transaction is appropriate to prevent simple tag
218 * starvation. Since we'd like to ensure that there is at least
219 * 1/2 of the timeout length left for a starved transaction to
220 * complete after we've sent an ordered tag, we must poll at least
221 * four times in every timeout period. This takes care of the worst
222 * case where a starved transaction starts during an interval that
223 * meets the requirement "don't send an ordered tag" test so it takes
224 * us two intervals to determine that a tag must be sent.
226 #ifndef DA_ORDEREDTAG_INTERVAL
227 #define DA_ORDEREDTAG_INTERVAL 4
230 static struct periph_driver dadriver =
233 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
236 DATA_SET(periphdriver_set, dadriver);
238 #define DA_CDEV_MAJOR 13
239 #define DA_BDEV_MAJOR 4
241 /* For 2.2-stable support */
246 static struct cdevsw da_cdevsw = {
250 /* write */ physwrite,
254 /* strategy */ dastrategy,
256 /* maj */ DA_CDEV_MAJOR,
260 /* bmaj */ DA_BDEV_MAJOR
263 static struct cdevsw dadisk_cdevsw;
265 static SLIST_HEAD(,da_softc) softc_list;
266 static struct extend_array *daperiphs;
269 daopen(dev_t dev, int flags, int fmt, struct proc *p)
271 struct cam_periph *periph;
272 struct da_softc *softc;
273 struct disklabel *label;
281 periph = cam_extend_get(daperiphs, unit);
285 softc = (struct da_softc *)periph->softc;
287 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
288 ("daopen: dev=%s (unit %d , partition %d)\n", devtoname(dev),
291 if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) {
292 return (error); /* error code from tsleep */
295 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
297 softc->flags |= DA_FLAG_OPEN;
300 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
301 /* Invalidate our pack information. */
302 disk_invalidate(&softc->disk);
303 softc->flags &= ~DA_FLAG_PACK_INVALID;
307 /* Do a read capacity */
309 struct scsi_read_capacity_data *rcap;
312 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
316 ccb = cam_periph_getccb(periph, /*priority*/1);
317 scsi_read_capacity(&ccb->csio,
324 ccb->ccb_h.ccb_bp = NULL;
326 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
327 /*sense_flags*/SF_RETRY_UA |
329 &softc->device_stats);
331 xpt_release_ccb(ccb);
334 dasetgeom(periph, rcap);
341 struct ccb_getdev cgd;
343 /* Build label for whole disk. */
344 label = &softc->disk.d_label;
345 bzero(label, sizeof(*label));
346 label->d_type = DTYPE_SCSI;
349 * Grab the inquiry data to get the vendor and product names.
350 * Put them in the typename and packname for the label.
352 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
353 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
354 xpt_action((union ccb *)&cgd);
356 strncpy(label->d_typename, cgd.inq_data.vendor,
357 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
358 strncpy(label->d_packname, cgd.inq_data.product,
359 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
361 label->d_secsize = softc->params.secsize;
362 label->d_nsectors = softc->params.secs_per_track;
363 label->d_ntracks = softc->params.heads;
364 label->d_ncylinders = softc->params.cylinders;
365 label->d_secpercyl = softc->params.heads
366 * softc->params.secs_per_track;
367 label->d_secperunit = softc->params.sectors;
369 if (((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0)) {
370 daprevent(periph, PR_PREVENT);
374 * Check to see whether or not the blocksize is set yet.
375 * If it isn't, set it and then clear the blocksize
376 * unavailable flag for the device statistics.
378 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0){
379 softc->device_stats.block_size = softc->params.secsize;
380 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
385 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
386 daprevent(periph, PR_ALLOW);
389 cam_periph_unlock(periph);
394 daclose(dev_t dev, int flag, int fmt, struct proc *p)
396 struct cam_periph *periph;
397 struct da_softc *softc;
402 periph = cam_extend_get(daperiphs, unit);
406 softc = (struct da_softc *)periph->softc;
408 if ((error = cam_periph_lock(periph, PRIBIO)) != 0) {
409 return (error); /* error code from tsleep */
412 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
415 ccb = cam_periph_getccb(periph, /*priority*/1);
417 scsi_synchronize_cache(&ccb->csio,
421 /*begin_lba*/0,/* Cover the whole disk */
426 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
427 /*sense_flags*/SF_RETRY_UA,
428 &softc->device_stats);
430 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
431 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
432 CAM_SCSI_STATUS_ERROR) {
434 int sense_key, error_code;
436 scsi_extract_sense(&ccb->csio.sense_data,
440 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
441 scsi_sense_print(&ccb->csio);
443 xpt_print_path(periph->path);
444 printf("Synchronize cache failed, status "
445 "== 0x%x, scsi status == 0x%x\n",
446 ccb->csio.ccb_h.status,
447 ccb->csio.scsi_status);
451 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
452 cam_release_devq(ccb->ccb_h.path,
458 xpt_release_ccb(ccb);
462 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
463 daprevent(periph, PR_ALLOW);
465 * If we've got removeable media, mark the blocksize as
466 * unavailable, since it could change when new media is
469 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
472 softc->flags &= ~DA_FLAG_OPEN;
473 cam_periph_unlock(periph);
474 cam_periph_release(periph);
479 * Actually translate the requested transfer into one the physical driver
480 * can understand. The transfer is described by a buf and will include
481 * only one physical transfer.
484 dastrategy(struct buf *bp)
486 struct cam_periph *periph;
487 struct da_softc *softc;
492 unit = dkunit(bp->b_dev);
493 part = dkpart(bp->b_dev);
494 periph = cam_extend_get(daperiphs, unit);
495 if (periph == NULL) {
499 softc = (struct da_softc *)periph->softc;
502 * check it's not too big a transfer for our adapter
504 scsi_minphys(bp,&sd_switch);
508 * Mask interrupts so that the pack cannot be invalidated until
509 * after we are in the queue. Otherwise, we might not properly
510 * clean up one of the buffers.
515 * If the device has been made invalid, error out
517 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
524 * Place it in the queue of disk activities for this disk
526 bufqdisksort(&softc->buf_queue, bp);
531 * Schedule ourselves for performing the work.
533 xpt_schedule(periph, /* XXX priority */1);
537 bp->b_flags |= B_ERROR;
540 * Correctly set the buf to indicate a completed xfer
542 bp->b_resid = bp->b_bcount;
547 /* For 2.2-stable support */
553 daioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
555 struct cam_periph *periph;
556 struct da_softc *softc;
561 periph = cam_extend_get(daperiphs, unit);
565 softc = (struct da_softc *)periph->softc;
567 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("daioctl\n"));
569 if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) {
570 return (error); /* error code from tsleep */
573 error = cam_periph_ioctl(periph, cmd, addr, daerror);
575 cam_periph_unlock(periph);
583 struct cam_periph *periph;
584 struct da_softc *softc;
588 u_int num; /* number of sectors to write */
592 struct ccb_scsiio csio;
595 /* toss any characters present prior to dump */
596 while (cncheckc() != -1)
601 periph = cam_extend_get(daperiphs, unit);
602 if (periph == NULL) {
605 softc = (struct da_softc *)periph->softc;
607 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0)
610 error = disk_dumpcheck(dev, &num, &blknum, &secsize);
614 addr = 0; /* starting address */
615 blkcnt = howmany(PAGE_SIZE, secsize);
619 if (is_physical_memory(addr)) {
620 pmap_enter(kernel_pmap, (vm_offset_t)CADDR1,
621 trunc_page(addr), VM_PROT_READ, TRUE);
623 pmap_enter(kernel_pmap, (vm_offset_t)CADDR1,
624 trunc_page(0), VM_PROT_READ, TRUE);
627 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
628 csio.ccb_h.ccb_state = DA_CCB_DUMP;
629 scsi_read_write(&csio,
635 /*minimum_cmd_size*/ softc->minimum_cmd_size,
639 /*dxfer_len*/blkcnt * secsize,
640 /*sense_len*/SSD_FULL_SIZE,
641 DA_DEFAULT_TIMEOUT * 1000);
642 xpt_polled_action((union ccb *)&csio);
644 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
645 printf("Aborting dump due to I/O error.\n");
646 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
647 CAM_SCSI_STATUS_ERROR)
648 scsi_sense_print(&csio);
650 printf("status == 0x%x, scsi status == 0x%x\n",
651 csio.ccb_h.status, csio.scsi_status);
655 if (addr % (1024 * 1024) == 0) {
660 /* Count in MB of data left to write */
661 printf("%d ", (num * softc->params.secsize)
665 /* update block count */
670 /* operator aborting dump? */
671 if (cncheckc() != -1)
676 * Sync the disk cache contents to the physical media.
678 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
680 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
681 csio.ccb_h.ccb_state = DA_CCB_DUMP;
682 scsi_synchronize_cache(&csio,
686 /*begin_lba*/0,/* Cover the whole disk */
690 xpt_polled_action((union ccb *)&csio);
692 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
693 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
694 CAM_SCSI_STATUS_ERROR) {
696 int sense_key, error_code;
698 scsi_extract_sense(&csio.sense_data,
702 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
703 scsi_sense_print(&csio);
705 xpt_print_path(periph->path);
706 printf("Synchronize cache failed, status "
707 "== 0x%x, scsi status == 0x%x\n",
708 csio.ccb_h.status, csio.scsi_status);
719 struct cam_path *path;
722 * Create our extend array for storing the devices we attach to.
724 daperiphs = cam_extend_new();
725 SLIST_INIT(&softc_list);
726 if (daperiphs == NULL) {
727 printf("da: Failed to alloc extend array!\n");
732 * Install a global async callback. This callback will
733 * receive async callbacks like "new device found".
735 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
736 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
738 if (status == CAM_REQ_CMP) {
739 struct ccb_setasync csa;
741 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
742 csa.ccb_h.func_code = XPT_SASYNC_CB;
743 csa.event_enable = AC_FOUND_DEVICE;
744 csa.callback = daasync;
745 csa.callback_arg = NULL;
746 xpt_action((union ccb *)&csa);
747 status = csa.ccb_h.status;
751 if (status != CAM_REQ_CMP) {
752 printf("da: Failed to attach master async callback "
753 "due to status 0x%x!\n", status);
757 * Schedule a periodic event to occasioanly send an
758 * ordered tag to a device.
760 timeout(dasendorderedtag, NULL,
761 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL);
763 /* Register our shutdown event handler */
764 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
765 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
766 printf("dainit: shutdown event registration failed!\n");
771 daoninvalidate(struct cam_periph *periph)
774 struct da_softc *softc;
776 struct ccb_setasync csa;
778 softc = (struct da_softc *)periph->softc;
781 * De-register any async callbacks.
783 xpt_setup_ccb(&csa.ccb_h, periph->path,
785 csa.ccb_h.func_code = XPT_SASYNC_CB;
786 csa.event_enable = 0;
787 csa.callback = daasync;
788 csa.callback_arg = periph;
789 xpt_action((union ccb *)&csa);
791 softc->flags |= DA_FLAG_PACK_INVALID;
794 * Although the oninvalidate() routines are always called at
795 * splsoftcam, we need to be at splbio() here to keep the buffer
796 * queue from being modified while we traverse it.
801 * Return all queued I/O with ENXIO.
802 * XXX Handle any transactions queued to the card
803 * with XPT_ABORT_CCB.
805 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
806 bufq_remove(&softc->buf_queue, q_bp);
807 q_bp->b_resid = q_bp->b_bcount;
808 q_bp->b_error = ENXIO;
809 q_bp->b_flags |= B_ERROR;
814 SLIST_REMOVE(&softc_list, softc, da_softc, links);
816 xpt_print_path(periph->path);
817 printf("lost device\n");
821 dacleanup(struct cam_periph *periph)
823 struct da_softc *softc;
825 softc = (struct da_softc *)periph->softc;
827 devstat_remove_entry(&softc->device_stats);
828 cam_extend_release(daperiphs, periph->unit_number);
829 xpt_print_path(periph->path);
830 printf("removing device entry\n");
831 free(softc, M_DEVBUF);
835 daasync(void *callback_arg, u_int32_t code,
836 struct cam_path *path, void *arg)
838 struct cam_periph *periph;
840 periph = (struct cam_periph *)callback_arg;
842 case AC_FOUND_DEVICE:
844 struct ccb_getdev *cgd;
847 cgd = (struct ccb_getdev *)arg;
849 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
850 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
854 * Allocate a peripheral instance for
855 * this device and start the probe
858 status = cam_periph_alloc(daregister, daoninvalidate,
860 "da", CAM_PERIPH_BIO,
861 cgd->ccb_h.path, daasync,
862 AC_FOUND_DEVICE, cgd);
864 if (status != CAM_REQ_CMP
865 && status != CAM_REQ_INPROG)
866 printf("daasync: Unable to attach to new device "
867 "due to status 0x%x\n", status);
873 struct da_softc *softc;
874 struct ccb_hdr *ccbh;
877 softc = (struct da_softc *)periph->softc;
880 * Don't fail on the expected unit attention
883 softc->flags |= DA_FLAG_RETRY_UA;
884 for (ccbh = LIST_FIRST(&softc->pending_ccbs);
885 ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le))
886 ccbh->ccb_state |= DA_CCB_RETRY_UA;
891 cam_periph_async(periph, code, path, arg);
897 daregister(struct cam_periph *periph, void *arg)
900 struct da_softc *softc;
901 struct ccb_setasync csa;
902 struct ccb_getdev *cgd;
905 cgd = (struct ccb_getdev *)arg;
906 if (periph == NULL) {
907 printf("daregister: periph was NULL!!\n");
908 return(CAM_REQ_CMP_ERR);
912 printf("daregister: no getdev CCB, can't register device\n");
913 return(CAM_REQ_CMP_ERR);
916 softc = (struct da_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
919 printf("daregister: Unable to probe new device. "
920 "Unable to allocate softc\n");
921 return(CAM_REQ_CMP_ERR);
924 bzero(softc, sizeof(*softc));
925 LIST_INIT(&softc->pending_ccbs);
926 softc->state = DA_STATE_PROBE;
927 bufq_init(&softc->buf_queue);
928 if (SID_IS_REMOVABLE(&cgd->inq_data))
929 softc->flags |= DA_FLAG_PACK_REMOVABLE;
930 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
931 softc->flags |= DA_FLAG_TAGGED_QUEUING;
933 periph->softc = softc;
935 cam_extend_set(daperiphs, periph->unit_number, periph);
938 * See if this device has any quirks.
940 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
941 (caddr_t)da_quirk_table,
942 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
943 sizeof(*da_quirk_table), scsi_inquiry_match);
946 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
948 softc->quirks = DA_Q_NONE;
950 if (softc->quirks & DA_Q_NO_6_BYTE)
951 softc->minimum_cmd_size = 10;
953 softc->minimum_cmd_size = 6;
956 * Block our timeout handler while we
957 * add this softc to the dev list.
960 SLIST_INSERT_HEAD(&softc_list, softc, links);
964 * The DA driver supports a blocksize, but
965 * we don't know the blocksize until we do
966 * a read capacity. So, set a flag to
967 * indicate that the blocksize is
968 * unavailable right now. We'll clear the
969 * flag as soon as we've done a read capacity.
971 devstat_add_entry(&softc->device_stats, "da",
972 periph->unit_number, 0,
973 DEVSTAT_BS_UNAVAILABLE,
974 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
975 DEVSTAT_PRIORITY_DISK);
978 * Register this media as a disk
980 disk_create(periph->unit_number, &softc->disk, 0,
981 &da_cdevsw, &dadisk_cdevsw);
984 * Add async callbacks for bus reset and
985 * bus device reset calls. I don't bother
986 * checking if this fails as, in most cases,
987 * the system will function just fine without
988 * them and the only alternative would be to
989 * not attach the device on failure.
991 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
992 csa.ccb_h.func_code = XPT_SASYNC_CB;
993 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
994 csa.callback = daasync;
995 csa.callback_arg = periph;
996 xpt_action((union ccb *)&csa);
998 * Lock this peripheral until we are setup.
999 * This first call can't block
1001 (void)cam_periph_lock(periph, PRIBIO);
1002 xpt_schedule(periph, /*priority*/5);
1004 return(CAM_REQ_CMP);
1008 dastart(struct cam_periph *periph, union ccb *start_ccb)
1010 struct da_softc *softc;
1012 softc = (struct da_softc *)periph->softc;
1015 switch (softc->state) {
1016 case DA_STATE_NORMAL:
1018 /* Pull a buffer from the queue and get going on it */
1023 * See if there is a buf with work for us to do..
1026 bp = bufq_first(&softc->buf_queue);
1027 if (periph->immediate_priority <= periph->pinfo.priority) {
1028 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1029 ("queuing for immediate ccb\n"));
1030 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1031 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1033 periph->immediate_priority = CAM_PRIORITY_NONE;
1035 wakeup(&periph->ccb_list);
1036 } else if (bp == NULL) {
1038 xpt_release_ccb(start_ccb);
1043 bufq_remove(&softc->buf_queue, bp);
1045 devstat_start_transaction(&softc->device_stats);
1047 if ((bp->b_flags & B_ORDERED) != 0
1048 || (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1049 softc->flags &= ~DA_FLAG_NEED_OTAG;
1050 softc->ordered_tag_count++;
1051 tag_code = MSG_ORDERED_Q_TAG;
1053 tag_code = MSG_SIMPLE_Q_TAG;
1055 scsi_read_write(&start_ccb->csio,
1059 bp->b_iocmd == BIO_READ,
1061 softc->minimum_cmd_size,
1063 bp->b_bcount / softc->params.secsize,
1066 /*sense_len*/SSD_FULL_SIZE,
1067 DA_DEFAULT_TIMEOUT * 1000);
1068 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1071 * Block out any asyncronous callbacks
1072 * while we touch the pending ccb list.
1075 LIST_INSERT_HEAD(&softc->pending_ccbs,
1076 &start_ccb->ccb_h, periph_links.le);
1079 /* We expect a unit attention from this device */
1080 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1081 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1082 softc->flags &= ~DA_FLAG_RETRY_UA;
1085 start_ccb->ccb_h.ccb_bp = bp;
1086 bp = bufq_first(&softc->buf_queue);
1089 xpt_action(start_ccb);
1093 /* Have more work to do, so ensure we stay scheduled */
1094 xpt_schedule(periph, /* XXX priority */1);
1098 case DA_STATE_PROBE:
1100 struct ccb_scsiio *csio;
1101 struct scsi_read_capacity_data *rcap;
1103 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
1107 printf("dastart: Couldn't malloc read_capacity data\n");
1108 /* da_free_periph??? */
1111 csio = &start_ccb->csio;
1112 scsi_read_capacity(csio,
1119 start_ccb->ccb_h.ccb_bp = NULL;
1120 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1121 xpt_action(start_ccb);
1129 dadone(struct cam_periph *periph, union ccb *done_ccb)
1131 struct da_softc *softc;
1132 struct ccb_scsiio *csio;
1134 softc = (struct da_softc *)periph->softc;
1135 csio = &done_ccb->csio;
1136 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1137 case DA_CCB_BUFFER_IO:
1142 bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
1143 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1148 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1153 /* Retry selection timeouts */
1154 sf |= SF_RETRY_SELTO;
1156 if ((error = daerror(done_ccb, 0, sf)) == ERESTART) {
1158 * A retry was scheuled, so
1168 if (error == ENXIO) {
1170 * Catastrophic error. Mark our pack as
1173 /* XXX See if this is really a media
1176 xpt_print_path(periph->path);
1177 printf("Invalidating pack\n");
1178 softc->flags |= DA_FLAG_PACK_INVALID;
1182 * return all queued I/O with EIO, so that
1183 * the client can retry these I/Os in the
1184 * proper order should it attempt to recover.
1186 while ((q_bp = bufq_first(&softc->buf_queue))
1188 bufq_remove(&softc->buf_queue, q_bp);
1189 q_bp->b_resid = q_bp->b_bcount;
1190 q_bp->b_error = EIO;
1191 q_bp->b_flags |= B_ERROR;
1195 bp->b_error = error;
1196 bp->b_resid = bp->b_bcount;
1197 bp->b_flags |= B_ERROR;
1199 bp->b_resid = csio->resid;
1201 if (bp->b_resid != 0) {
1202 /* Short transfer ??? */
1203 bp->b_flags |= B_ERROR;
1206 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1207 cam_release_devq(done_ccb->ccb_h.path,
1211 /*getcount_only*/0);
1213 bp->b_resid = csio->resid;
1214 if (csio->resid > 0)
1215 bp->b_flags |= B_ERROR;
1219 * Block out any asyncronous callbacks
1220 * while we touch the pending ccb list.
1223 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1226 if (softc->device_stats.busy_count == 0)
1227 softc->flags |= DA_FLAG_WENT_IDLE;
1229 devstat_end_transaction_buf(&softc->device_stats, bp);
1235 struct scsi_read_capacity_data *rdcap;
1236 char announce_buf[80];
1238 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1240 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1241 struct disk_params *dp;
1243 dasetgeom(periph, rdcap);
1244 dp = &softc->params;
1245 snprintf(announce_buf, sizeof(announce_buf),
1246 "%luMB (%u %u byte sectors: %dH %dS/T %dC)",
1247 (unsigned long) (((u_int64_t)dp->secsize *
1248 dp->sectors) / (1024*1024)), dp->sectors,
1249 dp->secsize, dp->heads, dp->secs_per_track,
1254 announce_buf[0] = '\0';
1257 * Retry any UNIT ATTENTION type errors. They
1258 * are expected at boot.
1260 error = daerror(done_ccb, 0, SF_RETRY_UA |
1261 SF_RETRY_SELTO | SF_NO_PRINT);
1262 if (error == ERESTART) {
1264 * A retry was scheuled, so
1268 } else if (error != 0) {
1269 struct scsi_sense_data *sense;
1271 int sense_key, error_code;
1274 struct ccb_getdev cgd;
1276 /* Don't wedge this device's queue */
1277 cam_release_devq(done_ccb->ccb_h.path,
1281 /*getcount_only*/0);
1283 status = done_ccb->ccb_h.status;
1285 xpt_setup_ccb(&cgd.ccb_h,
1286 done_ccb->ccb_h.path,
1288 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1289 xpt_action((union ccb *)&cgd);
1291 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1292 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1293 || ((status & CAM_AUTOSNS_VALID) == 0))
1299 sense = &csio->sense_data;
1300 scsi_extract_sense(sense, &error_code,
1305 * Attach to anything that claims to be a
1306 * direct access or optical disk device,
1307 * as long as it doesn't return a "Logical
1308 * unit not supported" (0x25) error.
1310 if ((have_sense) && (asc != 0x25)
1311 && (error_code == SSD_CURRENT_ERROR))
1312 snprintf(announce_buf,
1313 sizeof(announce_buf),
1314 "Attempt to query device "
1315 "size failed: %s, %s",
1316 scsi_sense_key_text[sense_key],
1317 scsi_sense_desc(asc,ascq,
1324 xpt_print_path(periph->path);
1325 printf("got CAM status %#x\n",
1326 done_ccb->ccb_h.status);
1329 xpt_print_path(periph->path);
1330 printf("fatal error, failed"
1331 " to attach to device\n");
1334 * Free up resources.
1336 cam_periph_invalidate(periph);
1340 free(rdcap, M_TEMP);
1341 if (announce_buf[0] != '\0')
1342 xpt_announce_periph(periph, announce_buf);
1343 softc->state = DA_STATE_NORMAL;
1345 * Since our peripheral may be invalidated by an error
1346 * above or an external event, we must release our CCB
1347 * before releasing the probe lock on the peripheral.
1348 * The peripheral will only go away once the last lock
1349 * is removed, and we need it around for the CCB release
1352 xpt_release_ccb(done_ccb);
1353 cam_periph_unlock(periph);
1356 case DA_CCB_WAITING:
1358 /* Caller will release the CCB */
1359 wakeup(&done_ccb->ccb_h.cbfcnp);
1363 /* No-op. We're polling */
1368 xpt_release_ccb(done_ccb);
1372 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1374 struct da_softc *softc;
1375 struct cam_periph *periph;
1377 periph = xpt_path_periph(ccb->ccb_h.path);
1378 softc = (struct da_softc *)periph->softc;
1382 * Until we have a better way of doing pack validation,
1383 * don't treat UAs as errors.
1385 sense_flags |= SF_RETRY_UA;
1386 return(cam_periph_error(ccb, cam_flags, sense_flags,
1387 &softc->saved_ccb));
1391 daprevent(struct cam_periph *periph, int action)
1393 struct da_softc *softc;
1397 softc = (struct da_softc *)periph->softc;
1399 if (((action == PR_ALLOW)
1400 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
1401 || ((action == PR_PREVENT)
1402 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
1406 ccb = cam_periph_getccb(periph, /*priority*/1);
1408 scsi_prevent(&ccb->csio,
1416 error = cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
1417 /*sense_flags*/0, &softc->device_stats);
1420 if (action == PR_ALLOW)
1421 softc->flags &= ~DA_FLAG_PACK_LOCKED;
1423 softc->flags |= DA_FLAG_PACK_LOCKED;
1426 xpt_release_ccb(ccb);
1430 dasetgeom(struct cam_periph *periph, struct scsi_read_capacity_data * rdcap)
1432 struct ccb_calc_geometry ccg;
1433 struct da_softc *softc;
1434 struct disk_params *dp;
1436 softc = (struct da_softc *)periph->softc;
1438 dp = &softc->params;
1439 dp->secsize = scsi_4btoul(rdcap->length);
1440 dp->sectors = scsi_4btoul(rdcap->addr) + 1;
1442 * Have the controller provide us with a geometry
1443 * for this disk. The only time the geometry
1444 * matters is when we boot and the controller
1445 * is the only one knowledgeable enough to come
1446 * up with something that will make this a bootable
1449 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
1450 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
1451 ccg.block_size = dp->secsize;
1452 ccg.volume_size = dp->sectors;
1454 ccg.secs_per_track = 0;
1456 xpt_action((union ccb*)&ccg);
1457 dp->heads = ccg.heads;
1458 dp->secs_per_track = ccg.secs_per_track;
1459 dp->cylinders = ccg.cylinders;
1463 dasendorderedtag(void *arg)
1465 struct da_softc *softc;
1468 for (softc = SLIST_FIRST(&softc_list);
1470 softc = SLIST_NEXT(softc, links)) {
1472 if ((softc->ordered_tag_count == 0)
1473 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
1474 softc->flags |= DA_FLAG_NEED_OTAG;
1476 if (softc->device_stats.busy_count > 0)
1477 softc->flags &= ~DA_FLAG_WENT_IDLE;
1479 softc->ordered_tag_count = 0;
1482 /* Queue us up again */
1483 timeout(dasendorderedtag, NULL,
1484 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL);
1488 * Step through all DA peripheral drivers, and if the device is still open,
1489 * sync the disk cache to physical media.
1492 dashutdown(void * arg, int howto)
1494 struct cam_periph *periph;
1495 struct da_softc *softc;
1497 for (periph = TAILQ_FIRST(&dadriver.units); periph != NULL;
1498 periph = TAILQ_NEXT(periph, unit_links)) {
1500 softc = (struct da_softc *)periph->softc;
1503 * We only sync the cache if the drive is still open, and
1504 * if the drive is capable of it..
1506 if (((softc->flags & DA_FLAG_OPEN) == 0)
1507 || (softc->quirks & DA_Q_NO_SYNC_CACHE))
1510 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
1512 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
1513 scsi_synchronize_cache(&ccb.csio,
1517 /*begin_lba*/0, /* whole disk */
1522 xpt_polled_action(&ccb);
1524 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1525 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
1526 CAM_SCSI_STATUS_ERROR)
1527 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
1528 int error_code, sense_key, asc, ascq;
1530 scsi_extract_sense(&ccb.csio.sense_data,
1531 &error_code, &sense_key,
1534 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1535 scsi_sense_print(&ccb.csio);
1537 xpt_print_path(periph->path);
1538 printf("Synchronize cache failed, status "
1539 "== 0x%x, scsi status == 0x%x\n",
1540 ccb.ccb_h.status, ccb.csio.scsi_status);
1544 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1545 cam_release_devq(ccb.ccb_h.path,
1549 /*getcount_only*/0);