2 * Implementation of SCSI Direct Access Peripheral driver for CAM.
4 * Copyright (c) 1997 Justin T. Gibbs.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $FreeBSD: src/sys/cam/scsi/scsi_da.c,v 1.42.2.46 2003/10/21 22:18:19 thomas Exp $
29 * $DragonFly: src/sys/bus/cam/scsi/scsi_da.c,v 1.59 2008/08/29 20:08:40 dillon Exp $
32 #include <sys/param.h>
33 #include <sys/bootmaj.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/sysctl.h>
40 #include <sys/taskqueue.h>
43 #include <sys/devicestat.h>
45 #include <sys/dtype.h>
46 #include <sys/eventhandler.h>
47 #include <sys/malloc.h>
51 #include <sys/thread2.h>
63 #include <sys/camlib.h>
65 #include "../cam_ccb.h"
66 #include "../cam_extend.h"
67 #include "../cam_periph.h"
68 #include "../cam_xpt_periph.h"
69 #include "../cam_sim.h"
71 #include "scsi_message.h"
85 DA_FLAG_PACK_INVALID = 0x001,
86 DA_FLAG_NEW_PACK = 0x002,
87 DA_FLAG_PACK_LOCKED = 0x004,
88 DA_FLAG_PACK_REMOVABLE = 0x008,
89 DA_FLAG_TAGGED_QUEUING = 0x010,
90 DA_FLAG_NEED_OTAG = 0x020,
91 DA_FLAG_WENT_IDLE = 0x040,
92 DA_FLAG_RETRY_UA = 0x080,
94 DA_FLAG_SCTX_INIT = 0x200
99 DA_Q_NO_SYNC_CACHE = 0x01,
100 DA_Q_NO_6_BYTE = 0x02,
101 DA_Q_NO_PREVENT = 0x04
106 DA_CCB_PROBE2 = 0x02,
107 DA_CCB_BUFFER_IO = 0x03,
108 DA_CCB_WAITING = 0x04,
110 DA_CCB_TYPE_MASK = 0x0F,
111 DA_CCB_RETRY_UA = 0x10
114 /* Offsets into our private area for storing information */
115 #define ccb_state ppriv_field0
116 #define ccb_bio ppriv_ptr1
121 u_int8_t secs_per_track;
122 u_int32_t secsize; /* Number of bytes/sector */
123 u_int64_t sectors; /* total number sectors */
127 struct bio_queue_head bio_queue;
128 struct devstat device_stats;
129 SLIST_ENTRY(da_softc) links;
130 LIST_HEAD(, ccb_hdr) pending_ccbs;
134 int minimum_cmd_size;
135 int ordered_tag_count;
136 int outstanding_cmds;
137 struct disk_params params;
140 struct task sysctl_task;
141 struct sysctl_ctx_list sysctl_ctx;
142 struct sysctl_oid *sysctl_tree;
143 struct callout sendordered_c;
146 struct da_quirk_entry {
147 struct scsi_inquiry_pattern inq_pat;
151 static const char quantum[] = "QUANTUM";
152 static const char microp[] = "MICROP";
154 static struct da_quirk_entry da_quirk_table[] =
156 /* SPI, FC devices */
159 * Fujitsu M2513A MO drives.
160 * Tested devices: M2513A2 firmware versions 1200 & 1300.
161 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
162 * Reported by: W.Scholten <whs@xs4all.nl>
164 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
165 /*quirks*/ DA_Q_NO_SYNC_CACHE
169 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
170 /*quirks*/ DA_Q_NO_SYNC_CACHE
174 * This particular Fujitsu drive doesn't like the
175 * synchronize cache command.
176 * Reported by: Tom Jackson <toj@gorilla.net>
178 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
179 /*quirks*/ DA_Q_NO_SYNC_CACHE
183 * This drive doesn't like the synchronize cache command
184 * either. Reported by: Matthew Jacob <mjacob@feral.com>
185 * in NetBSD PR kern/6027, August 24, 1998.
187 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
188 /*quirks*/ DA_Q_NO_SYNC_CACHE
192 * This drive doesn't like the synchronize cache command
193 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
196 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
197 /*quirks*/ DA_Q_NO_SYNC_CACHE
201 * Doesn't like the synchronize cache command.
202 * Reported by: Blaz Zupan <blaz@gold.amis.net>
204 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
205 /*quirks*/ DA_Q_NO_SYNC_CACHE
209 * Doesn't like the synchronize cache command.
210 * Reported by: Blaz Zupan <blaz@gold.amis.net>
212 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
213 /*quirks*/ DA_Q_NO_SYNC_CACHE
217 * Doesn't like the synchronize cache command.
219 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
220 /*quirks*/ DA_Q_NO_SYNC_CACHE
224 * Doesn't like the synchronize cache command.
225 * Reported by: walter@pelissero.de
227 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
228 /*quirks*/ DA_Q_NO_SYNC_CACHE
232 * Doesn't work correctly with 6 byte reads/writes.
233 * Returns illegal request, and points to byte 9 of the
235 * Reported by: Adam McDougall <bsdx@spawnet.com>
237 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
238 /*quirks*/ DA_Q_NO_6_BYTE
242 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
243 /*quirks*/ DA_Q_NO_6_BYTE
247 * Doesn't like the synchronize cache command.
248 * Reported by: walter@pelissero.de
250 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
251 /*quirks*/ DA_Q_NO_SYNC_CACHE
255 * The CISS RAID controllers do not support SYNC_CACHE
257 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
258 /*quirks*/ DA_Q_NO_SYNC_CACHE
261 * USB mass storage devices supported by umass(4)
263 * NOTE: USB attachments automatically set DA_Q_NO_SYNC_CACHE so
264 * it does not have to be specified here.
268 * Creative Nomad MUVO mp3 player (USB)
271 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
272 /*quirks*/ DA_Q_NO_PREVENT
276 * Sigmatel USB Flash MP3 Player
279 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
280 /*quirks*/ DA_Q_NO_PREVENT
284 * SEAGRAND NP-900 MP3 Player
287 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
288 /*quirks*/ DA_Q_NO_PREVENT
292 * Creative MUVO Slim mp3 player (USB)
295 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
296 "*"}, /*quirks*/ DA_Q_NO_PREVENT
300 * Philips USB Key Audio KEY013
303 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
304 /*quirks*/ DA_Q_NO_PREVENT
308 static d_open_t daopen;
309 static d_close_t daclose;
310 static d_strategy_t dastrategy;
311 static d_dump_t dadump;
312 static periph_init_t dainit;
313 static void daasync(void *callback_arg, u_int32_t code,
314 struct cam_path *path, void *arg);
315 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
316 static periph_ctor_t daregister;
317 static periph_dtor_t dacleanup;
318 static periph_start_t dastart;
319 static periph_oninv_t daoninvalidate;
320 static void dadone(struct cam_periph *periph,
321 union ccb *done_ccb);
322 static int daerror(union ccb *ccb, u_int32_t cam_flags,
323 u_int32_t sense_flags);
324 static void daprevent(struct cam_periph *periph, int action);
325 static int dagetcapacity(struct cam_periph *periph);
326 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
329 static timeout_t dasendorderedtag;
330 static void dashutdown(void *arg, int howto);
332 #ifndef DA_DEFAULT_TIMEOUT
333 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
336 #ifndef DA_DEFAULT_RETRY
337 #define DA_DEFAULT_RETRY 4
340 #ifndef DA_DEFAULT_SEND_ORDERED
341 #define DA_DEFAULT_SEND_ORDERED 1
344 static int da_retry_count = DA_DEFAULT_RETRY;
345 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
346 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
347 static struct callout dasendorderedtag_ch;
349 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
350 "CAM Direct Access Disk driver");
351 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
352 &da_retry_count, 0, "Normal I/O retry count");
353 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
354 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
355 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
356 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
357 SYSCTL_INT(_kern_cam_da, OID_AUTO, da_send_ordered, CTLFLAG_RW,
358 &da_send_ordered, 0, "Send Ordered Tags");
359 TUNABLE_INT("kern.cam.da.da_send_ordered", &da_send_ordered);
362 * DA_ORDEREDTAG_INTERVAL determines how often, relative
363 * to the default timeout, we check to see whether an ordered
364 * tagged transaction is appropriate to prevent simple tag
365 * starvation. Since we'd like to ensure that there is at least
366 * 1/2 of the timeout length left for a starved transaction to
367 * complete after we've sent an ordered tag, we must poll at least
368 * four times in every timeout period. This takes care of the worst
369 * case where a starved transaction starts during an interval that
370 * meets the requirement "don't send an ordered tag" test so it takes
371 * us two intervals to determine that a tag must be sent.
373 #ifndef DA_ORDEREDTAG_INTERVAL
374 #define DA_ORDEREDTAG_INTERVAL 4
377 static struct periph_driver dadriver =
380 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
383 PERIPHDRIVER_DECLARE(da, dadriver);
385 static struct dev_ops da_ops = {
386 { "da", DA_CDEV_MAJOR, D_DISK },
390 .d_write = physwrite,
391 .d_strategy = dastrategy,
395 static struct extend_array *daperiphs;
397 MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
400 daopen(struct dev_open_args *ap)
402 cdev_t dev = ap->a_head.a_dev;
403 struct cam_periph *periph;
404 struct da_softc *softc;
405 struct disk_info info;
410 periph = cam_extend_get(daperiphs, unit);
411 if (periph == NULL) {
415 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
419 cam_periph_lock(periph);
420 if ((error = cam_periph_hold(periph, PCATCH)) != 0) {
421 cam_periph_unlock(periph);
422 cam_periph_release(periph);
426 unit = periph->unit_number;
427 softc = (struct da_softc *)periph->softc;
428 softc->flags |= DA_FLAG_OPEN;
430 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
431 ("daopen: dev=%s (unit %d)\n", devtoname(dev),
434 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
435 /* Invalidate our pack information. */
436 disk_invalidate(&softc->disk);
437 softc->flags &= ~DA_FLAG_PACK_INVALID;
440 error = dagetcapacity(periph);
443 /* Do a read capacity */
445 struct scsi_read_capacity_data *rcap;
448 rcap = kmalloc(sizeof(*rcap), M_SCSIDA, M_INTWAIT | M_ZERO);
450 ccb = cam_periph_getccb(periph, /*priority*/1);
451 scsi_read_capacity(&ccb->csio,
458 ccb->ccb_h.ccb_bio = NULL;
460 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
461 /*sense_flags*/SF_RETRY_UA |
463 &softc->device_stats);
465 xpt_release_ccb(ccb);
468 dasetgeom(periph, rcap);
471 kfree(rcap, M_SCSIDA);
476 struct ccb_getdev cgd;
478 /* Build disk information structure */
479 bzero(&info, sizeof(info));
480 info.d_type = DTYPE_SCSI;
483 * Grab the inquiry data to get the vendor and product names.
484 * Put them in the typename and packname for the label.
486 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
487 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
488 xpt_action((union ccb *)&cgd);
491 strncpy(label->d_typename, cgd.inq_data.vendor,
492 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
493 strncpy(label->d_packname, cgd.inq_data.product,
494 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
500 info.d_media_blksize = softc->params.secsize;
501 info.d_media_blocks = softc->params.sectors;
502 info.d_media_size = 0;
507 info.d_secpertrack = softc->params.secs_per_track;
508 info.d_nheads = softc->params.heads;
509 info.d_ncylinders = softc->params.cylinders;
510 info.d_secpercyl = softc->params.heads *
511 softc->params.secs_per_track;
512 info.d_serialno = xpt_path_serialno(periph->path);
513 disk_setdiskinfo(&softc->disk, &info);
516 * Check to see whether or not the blocksize is set yet.
517 * If it isn't, set it and then clear the blocksize
518 * unavailable flag for the device statistics.
520 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0){
521 softc->device_stats.block_size = softc->params.secsize;
522 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
527 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
528 (softc->quirks & DA_Q_NO_PREVENT) == 0)
529 daprevent(periph, PR_PREVENT);
531 softc->flags &= ~DA_FLAG_OPEN;
532 cam_periph_release(periph);
534 cam_periph_unhold(periph, 1);
539 daclose(struct dev_close_args *ap)
541 cdev_t dev = ap->a_head.a_dev;
542 struct cam_periph *periph;
543 struct da_softc *softc;
548 periph = cam_extend_get(daperiphs, unit);
552 cam_periph_lock(periph);
553 if ((error = cam_periph_hold(periph, 0)) != 0) {
554 cam_periph_unlock(periph);
555 cam_periph_release(periph);
559 softc = (struct da_softc *)periph->softc;
561 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
564 ccb = cam_periph_getccb(periph, /*priority*/1);
566 scsi_synchronize_cache(&ccb->csio,
570 /*begin_lba*/0,/* Cover the whole disk */
575 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
576 /*sense_flags*/SF_RETRY_UA,
577 &softc->device_stats);
579 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
580 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
581 CAM_SCSI_STATUS_ERROR) {
583 int sense_key, error_code;
585 scsi_extract_sense(&ccb->csio.sense_data,
589 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
590 scsi_sense_print(&ccb->csio);
592 xpt_print(periph->path, "Synchronize cache "
593 "failed, status == 0x%x, scsi status == "
594 "0x%x\n", ccb->csio.ccb_h.status,
595 ccb->csio.scsi_status);
599 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
600 cam_release_devq(ccb->ccb_h.path,
606 xpt_release_ccb(ccb);
610 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
611 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
612 daprevent(periph, PR_ALLOW);
614 * If we've got removeable media, mark the blocksize as
615 * unavailable, since it could change when new media is
618 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
622 * Don't compound any ref counting software bugs with more.
624 if (softc->flags & DA_FLAG_OPEN) {
625 softc->flags &= ~DA_FLAG_OPEN;
626 cam_periph_release(periph);
628 xpt_print(periph->path,
629 "daclose() called on an already closed device!\n");
631 cam_periph_unhold(periph, 1);
636 * Actually translate the requested transfer into one the physical driver
637 * can understand. The transfer is described by a buf and will include
638 * only one physical transfer.
641 dastrategy(struct dev_strategy_args *ap)
643 cdev_t dev = ap->a_head.a_dev;
644 struct bio *bio = ap->a_bio;
645 struct buf *bp = bio->bio_buf;
646 struct cam_periph *periph;
647 struct da_softc *softc;
653 periph = cam_extend_get(daperiphs, unit);
654 if (periph == NULL) {
658 softc = (struct da_softc *)periph->softc;
660 cam_periph_lock(periph);
664 * check it's not too big a transfer for our adapter
666 scsi_minphys(bp, &sd_switch);
670 * Mask interrupts so that the pack cannot be invalidated until
671 * after we are in the queue. Otherwise, we might not properly
672 * clean up one of the buffers.
676 * If the device has been made invalid, error out
678 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
679 cam_periph_unlock(periph);
685 * Place it in the queue of disk activities for this disk
687 bioqdisksort(&softc->bio_queue, bio);
690 * Schedule ourselves for performing the work.
692 xpt_schedule(periph, /* XXX priority */1);
693 cam_periph_unlock(periph);
697 bp->b_flags |= B_ERROR;
700 * Correctly set the buf to indicate a completed xfer
702 bp->b_resid = bp->b_bcount;
708 dadump(struct dev_dump_args *ap)
710 cdev_t dev = ap->a_head.a_dev;
711 struct cam_periph *periph;
712 struct da_softc *softc;
716 struct ccb_scsiio csio;
717 int dumppages = MAXDUMPPGS;
720 /* toss any characters present prior to dump */
721 while (cncheckc() != -1)
725 periph = cam_extend_get(daperiphs, unit);
726 if (periph == NULL) {
729 softc = (struct da_softc *)periph->softc;
731 cam_periph_lock(periph);
732 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
733 cam_periph_unlock(periph);
737 addr = 0; /* starting address */
738 blkcnt = howmany(PAGE_SIZE, ap->a_secsize);
740 while (ap->a_count > 0) {
743 if ((ap->a_count / blkcnt) < dumppages)
744 dumppages = ap->a_count / blkcnt;
746 for (i = 0; i < dumppages; ++i) {
747 vm_paddr_t a = addr + (i * PAGE_SIZE);
748 if (is_physical_memory(a))
749 va = pmap_kenter_temporary(trunc_page(a), i);
751 va = pmap_kenter_temporary(trunc_page(0), i);
754 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
755 csio.ccb_h.flags |= CAM_POLLED;
756 csio.ccb_h.ccb_state = DA_CCB_DUMP;
757 scsi_read_write(&csio,
763 /*minimum_cmd_size*/ softc->minimum_cmd_size,
766 /*data_ptr*/(u_int8_t *) va,
767 /*dxfer_len*/blkcnt * ap->a_secsize * dumppages,
768 /*sense_len*/SSD_FULL_SIZE,
769 DA_DEFAULT_TIMEOUT * 1000);
770 xpt_polled_action((union ccb *)&csio);
772 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
773 kprintf("Aborting dump due to I/O error.\n");
774 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
775 CAM_SCSI_STATUS_ERROR)
776 scsi_sense_print(&csio);
778 kprintf("status == 0x%x, scsi status == 0x%x\n",
779 csio.ccb_h.status, csio.scsi_status);
783 if (dumpstatus(addr, (off_t)ap->a_count * softc->params.secsize) < 0)
786 /* update block count */
787 ap->a_count -= blkcnt * dumppages;
788 ap->a_blkno += blkcnt * dumppages;
789 addr += PAGE_SIZE * dumppages;
793 * Sync the disk cache contents to the physical media.
795 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
797 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
798 csio.ccb_h.ccb_state = DA_CCB_DUMP;
799 scsi_synchronize_cache(&csio,
803 /*begin_lba*/0,/* Cover the whole disk */
807 xpt_polled_action((union ccb *)&csio);
809 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
810 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
811 CAM_SCSI_STATUS_ERROR) {
813 int sense_key, error_code;
815 scsi_extract_sense(&csio.sense_data,
819 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
820 scsi_sense_print(&csio);
822 xpt_print(periph->path, "Synchronize cache "
823 "failed, status == 0x%x, scsi status == "
824 "0x%x\n", csio.ccb_h.status,
829 cam_periph_unlock(periph);
839 * Create our extend array for storing the devices we attach to.
841 daperiphs = cam_extend_new();
842 if (daperiphs == NULL) {
843 kprintf("da: Failed to alloc extend array!\n");
847 callout_init(&dasendorderedtag_ch);
850 * Install a global async callback. This callback will
851 * receive async callbacks like "new device found".
853 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
855 if (status != CAM_REQ_CMP) {
856 kprintf("da: Failed to attach master async callback "
857 "due to status 0x%x!\n", status);
858 } else if (da_send_ordered) {
860 /* Register our shutdown event handler */
861 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
862 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
863 kprintf("dainit: shutdown event registration failed!\n");
868 daoninvalidate(struct cam_periph *periph)
870 struct da_softc *softc;
874 softc = (struct da_softc *)periph->softc;
877 * De-register any async callbacks.
879 xpt_register_async(0, daasync, periph, periph->path);
881 softc->flags |= DA_FLAG_PACK_INVALID;
884 * Return all queued I/O with ENXIO.
885 * XXX Handle any transactions queued to the card
886 * with XPT_ABORT_CCB.
888 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL){
889 bioq_remove(&softc->bio_queue, q_bio);
890 q_bp = q_bio->bio_buf;
891 q_bp->b_resid = q_bp->b_bcount;
892 q_bp->b_error = ENXIO;
893 q_bp->b_flags |= B_ERROR;
896 xpt_print(periph->path, "lost device\n");
900 dacleanup(struct cam_periph *periph)
902 struct da_softc *softc;
904 softc = (struct da_softc *)periph->softc;
906 devstat_remove_entry(&softc->device_stats);
907 cam_extend_release(daperiphs, periph->unit_number);
908 xpt_print(periph->path, "removing device entry\n");
910 * If we can't free the sysctl tree, oh well...
912 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
913 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
914 xpt_print(periph->path, "can't remove sysctl context\n");
916 periph->softc = NULL;
917 if (softc->disk.d_rawdev) {
918 cam_periph_unlock(periph);
919 disk_destroy(&softc->disk);
920 cam_periph_lock(periph);
923 callout_stop(&softc->sendordered_c);
924 kfree(softc, M_DEVBUF);
928 daasync(void *callback_arg, u_int32_t code,
929 struct cam_path *path, void *arg)
931 struct cam_periph *periph;
933 periph = (struct cam_periph *)callback_arg;
936 case AC_FOUND_DEVICE:
938 struct ccb_getdev *cgd;
942 cgd = (struct ccb_getdev *)arg;
946 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
947 && SID_TYPE(&cgd->inq_data) != T_RBC
948 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
952 * Don't complain if a valid peripheral is already attached.
954 periph = cam_periph_find(cgd->ccb_h.path, "da");
955 if (periph && (periph->flags & CAM_PERIPH_INVALID) == 0)
959 * Allocate a peripheral instance for
960 * this device and start the probe
963 sim = xpt_path_sim(cgd->ccb_h.path);
964 status = cam_periph_alloc(daregister, daoninvalidate,
966 "da", CAM_PERIPH_BIO,
967 cgd->ccb_h.path, daasync,
968 AC_FOUND_DEVICE, cgd);
970 if (status != CAM_REQ_CMP && status != CAM_REQ_INPROG) {
971 kprintf("daasync: Unable to attach to new device "
972 "due to status 0x%x\n", status);
979 struct da_softc *softc;
980 struct ccb_hdr *ccbh;
982 softc = (struct da_softc *)periph->softc;
984 * Don't fail on the expected unit attention
987 softc->flags |= DA_FLAG_RETRY_UA;
988 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
989 ccbh->ccb_state |= DA_CCB_RETRY_UA;
993 cam_periph_async(periph, code, path, arg);
999 dasysctlinit(void *context, int pending)
1001 struct cam_periph *periph;
1002 struct da_softc *softc;
1003 char tmpstr[80], tmpstr2[80];
1005 periph = (struct cam_periph *)context;
1006 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1009 softc = (struct da_softc *)periph->softc;
1010 ksnprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1011 ksnprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1013 sysctl_ctx_init(&softc->sysctl_ctx);
1014 softc->flags |= DA_FLAG_SCTX_INIT;
1015 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1016 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1017 CTLFLAG_RD, 0, tmpstr);
1018 if (softc->sysctl_tree == NULL) {
1019 kprintf("dasysctlinit: unable to allocate sysctl tree\n");
1020 cam_periph_release(periph);
1025 * Now register the sysctl handler, so the user can the value on
1028 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1029 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1030 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1031 "Minimum CDB size");
1033 cam_periph_release(periph);
1037 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1041 value = *(int *)arg1;
1043 error = sysctl_handle_int(oidp, &value, 0, req);
1046 || (req->newptr == NULL))
1050 * Acceptable values here are 6, 10 or 12, or 16.
1054 else if ((value > 6)
1057 else if ((value > 10)
1060 else if (value > 12)
1063 *(int *)arg1 = value;
1069 daregister(struct cam_periph *periph, void *arg)
1071 struct da_softc *softc;
1072 struct ccb_pathinq cpi;
1073 struct ccb_getdev *cgd;
1075 struct disk_info info;
1080 cgd = (struct ccb_getdev *)arg;
1081 if (periph == NULL) {
1082 kprintf("daregister: periph was NULL!!\n");
1083 return(CAM_REQ_CMP_ERR);
1087 kprintf("daregister: no getdev CCB, can't register device\n");
1088 return(CAM_REQ_CMP_ERR);
1091 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
1092 LIST_INIT(&softc->pending_ccbs);
1093 softc->state = DA_STATE_PROBE;
1094 bioq_init(&softc->bio_queue);
1095 if (SID_IS_REMOVABLE(&cgd->inq_data))
1096 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1097 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1098 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1100 periph->softc = softc;
1102 cam_extend_set(daperiphs, periph->unit_number, periph);
1105 * See if this device has any quirks.
1107 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1108 (caddr_t)da_quirk_table,
1109 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1110 sizeof(*da_quirk_table), scsi_inquiry_match);
1113 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1115 softc->quirks = DA_Q_NONE;
1118 * Unconditionally disable the synchronize cache command for
1119 * usb attachments. It's just impossible to determine if the
1120 * device supports it or not and if it doesn't the port can
1123 if (strncmp(periph->sim->sim_name, "umass", 4) == 0) {
1124 softc->quirks |= DA_Q_NO_SYNC_CACHE;
1127 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1129 /* Check if the SIM does not want 6 byte commands */
1130 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
1131 cpi.ccb_h.func_code = XPT_PATH_INQ;
1132 xpt_action((union ccb *)&cpi);
1133 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1134 softc->quirks |= DA_Q_NO_6_BYTE;
1137 * RBC devices don't have to support READ(6), only READ(10).
1139 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1140 softc->minimum_cmd_size = 10;
1142 softc->minimum_cmd_size = 6;
1145 * Load the user's default, if any.
1147 ksnprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1148 periph->unit_number);
1149 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1152 * 6, 10, 12, and 16 are the currently permissible values.
1154 if (softc->minimum_cmd_size < 6)
1155 softc->minimum_cmd_size = 6;
1156 else if ((softc->minimum_cmd_size > 6)
1157 && (softc->minimum_cmd_size <= 10))
1158 softc->minimum_cmd_size = 10;
1159 else if ((softc->minimum_cmd_size > 10)
1160 && (softc->minimum_cmd_size <= 12))
1161 softc->minimum_cmd_size = 12;
1162 else if (softc->minimum_cmd_size > 12)
1163 softc->minimum_cmd_size = 16;
1166 * The DA driver supports a blocksize, but
1167 * we don't know the blocksize until we do
1168 * a read capacity. So, set a flag to
1169 * indicate that the blocksize is
1170 * unavailable right now. We'll clear the
1171 * flag as soon as we've done a read capacity.
1173 devstat_add_entry(&softc->device_stats, "da",
1174 periph->unit_number, 0,
1175 DEVSTAT_BS_UNAVAILABLE,
1176 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
1177 DEVSTAT_PRIORITY_DISK);
1180 * Register this media as a disk
1182 CAM_SIM_UNLOCK(periph->sim);
1183 disk_create(periph->unit_number, &softc->disk, &da_ops);
1184 softc->disk.d_rawdev->si_iosize_max = MAXPHYS;
1185 CAM_SIM_LOCK(periph->sim);
1188 * Add async callbacks for bus reset and
1189 * bus device reset calls. I don't bother
1190 * checking if this fails as, in most cases,
1191 * the system will function just fine without
1192 * them and the only alternative would be to
1193 * not attach the device on failure.
1195 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
1196 daasync, periph, periph->path);
1199 * Take an exclusive refcount on the periph while dastart is called
1200 * to finish the probe. The reference will be dropped in dadone at
1203 cam_periph_hold(periph, 0);
1204 xpt_schedule(periph, /*priority*/5);
1207 * Schedule a periodic event to occasionally send an
1208 * ordered tag to a device.
1210 callout_init(&softc->sendordered_c);
1211 callout_reset(&softc->sendordered_c,
1212 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
1213 dasendorderedtag, softc);
1217 * Set diskinfo. It should be ok here as we already did a DA_CCB_PROBE.
1218 * Setting this info will also trigger probing of the device.
1220 CAM_SIM_UNLOCK(periph->sim);
1221 bzero(&info, sizeof(info));
1222 info.d_media_blksize = softc->params.secsize;
1223 info.d_media_blocks = softc->params.sectors;
1224 info.d_media_size = 0;
1225 info.d_secpertrack = softc->params.secs_per_track;
1226 info.d_nheads = softc->params.heads;
1227 info.d_ncylinders = softc->params.cylinders;
1228 info.d_secpercyl = softc->params.heads *
1229 softc->params.secs_per_track;
1230 info.d_serialno = xpt_path_serialno(periph->path);
1231 disk_setdiskinfo(&softc->disk, &info);
1233 CAM_SIM_LOCK(periph->sim);
1236 return(CAM_REQ_CMP);
1240 dastart(struct cam_periph *periph, union ccb *start_ccb)
1242 struct da_softc *softc;
1244 softc = (struct da_softc *)periph->softc;
1246 switch (softc->state) {
1247 case DA_STATE_NORMAL:
1249 /* Pull a buffer from the queue and get going on it */
1255 * See if there is a buf with work for us to do..
1257 bio = bioq_first(&softc->bio_queue);
1258 if (periph->immediate_priority <= periph->pinfo.priority) {
1259 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1260 ("queuing for immediate ccb\n"));
1261 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1262 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1264 periph->immediate_priority = CAM_PRIORITY_NONE;
1265 wakeup(&periph->ccb_list);
1268 * Have more work to do, so ensure we stay
1271 xpt_schedule(periph, /* XXX priority */1);
1276 xpt_release_ccb(start_ccb);
1281 * We can queue new work.
1283 bioq_remove(&softc->bio_queue, bio);
1286 devstat_start_transaction(&softc->device_stats);
1288 if ((bp->b_flags & B_ORDERED) != 0 ||
1289 (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1290 softc->flags &= ~DA_FLAG_NEED_OTAG;
1291 softc->ordered_tag_count++;
1292 tag_code = MSG_ORDERED_Q_TAG;
1294 tag_code = MSG_SIMPLE_Q_TAG;
1301 * Block read/write op
1303 KKASSERT(bio->bio_offset % softc->params.secsize == 0);
1307 da_retry_count, /* retries */
1310 (bp->b_cmd == BUF_CMD_READ),
1312 softc->minimum_cmd_size,
1313 bio->bio_offset / softc->params.secsize,
1314 bp->b_bcount / softc->params.secsize,
1317 SSD_FULL_SIZE, /* sense_len */
1318 da_default_timeout * 1000
1323 * Silently complete a flush request if the device
1326 if (softc->quirks & DA_Q_NO_SYNC_CACHE) {
1327 xpt_release_ccb(start_ccb);
1329 devstat_end_transaction_buf(
1330 &softc->device_stats, bp);
1333 scsi_synchronize_cache(
1336 dadone, /* cbfcnp */
1339 0, /* count (whole disk) */
1341 da_default_timeout*1000 /* timeout */
1346 xpt_release_ccb(start_ccb);
1348 panic("dastart: unrecognized bio cmd %d", bp->b_cmd);
1349 break; /* NOT REACHED */
1353 * Block out any asyncronous callbacks
1354 * while we touch the pending ccb list.
1357 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1358 LIST_INSERT_HEAD(&softc->pending_ccbs,
1359 &start_ccb->ccb_h, periph_links.le);
1360 softc->outstanding_cmds++;
1362 /* We expect a unit attention from this device */
1363 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1364 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1365 softc->flags &= ~DA_FLAG_RETRY_UA;
1368 start_ccb->ccb_h.ccb_bio = bio;
1369 xpt_action(start_ccb);
1373 * Be sure we stay scheduled if we have more work to do.
1375 bio = bioq_first(&softc->bio_queue);
1377 xpt_schedule(periph, 1);
1380 case DA_STATE_PROBE:
1382 struct ccb_scsiio *csio;
1383 struct scsi_read_capacity_data *rcap;
1385 rcap = kmalloc(sizeof(*rcap), M_SCSIDA, M_INTWAIT | M_ZERO);
1386 csio = &start_ccb->csio;
1387 scsi_read_capacity(csio,
1394 start_ccb->ccb_h.ccb_bio = NULL;
1395 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1396 xpt_action(start_ccb);
1399 case DA_STATE_PROBE2:
1401 struct ccb_scsiio *csio;
1402 struct scsi_read_capacity_data_16 *rcaplong;
1404 rcaplong = kmalloc(sizeof(*rcaplong), M_SCSIDA,
1405 M_INTWAIT | M_ZERO);
1406 if (rcaplong == NULL) {
1407 kprintf("dastart: Couldn't allocate read_capacity\n");
1408 /* da_free_periph??? */
1411 csio = &start_ccb->csio;
1412 scsi_read_capacity_16(csio,
1415 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1420 /*sense_len*/ SSD_FULL_SIZE,
1422 start_ccb->ccb_h.ccb_bio = NULL;
1423 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1424 xpt_action(start_ccb);
1431 cmd6workaround(union ccb *ccb)
1433 struct scsi_rw_6 cmd6;
1434 struct scsi_rw_10 *cmd10;
1435 struct da_softc *softc;
1439 cdb = ccb->csio.cdb_io.cdb_bytes;
1441 /* Translation only possible if CDB is an array and cmd is R/W6 */
1442 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1443 (*cdb != READ_6 && *cdb != WRITE_6))
1446 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
1447 "increasing minimum_cmd_size to 10.\n");
1448 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1449 softc->minimum_cmd_size = 10;
1451 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1452 cmd10 = (struct scsi_rw_10 *)cdb;
1453 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1455 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1456 cmd10->reserved = 0;
1457 scsi_ulto2b(cmd6.length, cmd10->length);
1458 cmd10->control = cmd6.control;
1459 ccb->csio.cdb_len = sizeof(*cmd10);
1461 /* Requeue request, unfreezing queue if necessary */
1462 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1463 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1466 cam_release_devq(ccb->ccb_h.path,
1470 /*getcount_only*/0);
1476 dadone(struct cam_periph *periph, union ccb *done_ccb)
1478 struct da_softc *softc;
1479 struct ccb_scsiio *csio;
1480 struct disk_info info;
1482 softc = (struct da_softc *)periph->softc;
1483 csio = &done_ccb->csio;
1484 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1485 case DA_CCB_BUFFER_IO:
1490 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1492 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1496 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1501 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1502 if (error == ERESTART) {
1504 * A retry was scheuled, so
1513 if (error == ENXIO) {
1515 * Catastrophic error. Mark our pack as
1519 * XXX See if this is really a media
1522 xpt_print(periph->path,
1523 "Invalidating pack\n");
1524 softc->flags |= DA_FLAG_PACK_INVALID;
1528 * return all queued I/O with EIO, so that
1529 * the client can retry these I/Os in the
1530 * proper order should it attempt to recover.
1532 while ((q_bio = bioq_first(&softc->bio_queue))
1534 bioq_remove(&softc->bio_queue, q_bio);
1535 q_bp = q_bio->bio_buf;
1536 q_bp->b_resid = q_bp->b_bcount;
1537 q_bp->b_error = EIO;
1538 q_bp->b_flags |= B_ERROR;
1541 bp->b_error = error;
1542 bp->b_resid = bp->b_bcount;
1543 bp->b_flags |= B_ERROR;
1545 bp->b_resid = csio->resid;
1547 if (bp->b_resid != 0)
1548 bp->b_flags |= B_ERROR;
1550 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1551 cam_release_devq(done_ccb->ccb_h.path,
1555 /*getcount_only*/0);
1557 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1558 panic("REQ_CMP with QFRZN");
1559 bp->b_resid = csio->resid;
1560 if (csio->resid > 0)
1561 bp->b_flags |= B_ERROR;
1565 * Block out any asyncronous callbacks
1566 * while we touch the pending ccb list.
1568 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1569 softc->outstanding_cmds--;
1570 if (softc->outstanding_cmds == 0)
1571 softc->flags |= DA_FLAG_WENT_IDLE;
1573 devstat_end_transaction_buf(&softc->device_stats, bp);
1580 struct scsi_read_capacity_data *rdcap;
1581 struct scsi_read_capacity_data_16 *rcaplong;
1582 char announce_buf[80];
1586 if (softc->state == DA_STATE_PROBE)
1587 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1589 rcaplong = (struct scsi_read_capacity_data_16 *)
1592 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1593 struct disk_params *dp;
1594 uint32_t block_size;
1597 if (softc->state == DA_STATE_PROBE) {
1598 block_size = scsi_4btoul(rdcap->length);
1599 maxsector = scsi_4btoul(rdcap->addr);
1602 * According to SBC-2, if the standard 10
1603 * byte READ CAPACITY command returns 2^32,
1604 * we should issue the 16 byte version of
1605 * the command, since the device in question
1606 * has more sectors than can be represented
1607 * with the short version of the command.
1609 if (maxsector == 0xffffffff) {
1610 softc->state = DA_STATE_PROBE2;
1611 kfree(rdcap, M_SCSIDA);
1612 xpt_release_ccb(done_ccb);
1613 xpt_schedule(periph, /*priority*/5);
1617 block_size = scsi_4btoul(rcaplong->length);
1618 maxsector = scsi_8btou64(rcaplong->addr);
1620 dasetgeom(periph, block_size, maxsector);
1621 dp = &softc->params;
1622 ksnprintf(announce_buf, sizeof(announce_buf),
1623 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1624 (uintmax_t) (((uintmax_t)dp->secsize *
1625 dp->sectors) / (1024*1024)),
1626 (uintmax_t)dp->sectors,
1627 dp->secsize, dp->heads, dp->secs_per_track,
1629 CAM_SIM_UNLOCK(periph->sim);
1630 bzero(&info, sizeof(info));
1631 info.d_media_blksize = softc->params.secsize;
1632 info.d_media_blocks = softc->params.sectors;
1633 info.d_media_size = 0;
1634 info.d_secpertrack = softc->params.secs_per_track;
1635 info.d_nheads = softc->params.heads;
1636 info.d_ncylinders = softc->params.cylinders;
1637 info.d_secpercyl = softc->params.heads *
1638 softc->params.secs_per_track;
1639 info.d_serialno = xpt_path_serialno(periph->path);
1640 disk_setdiskinfo(&softc->disk, &info);
1641 CAM_SIM_LOCK(periph->sim);
1645 announce_buf[0] = '\0';
1648 * Retry any UNIT ATTENTION type errors. They
1649 * are expected at boot.
1651 error = daerror(done_ccb, CAM_RETRY_SELTO,
1652 SF_RETRY_UA|SF_NO_PRINT);
1653 if (error == ERESTART) {
1655 * A retry was scheuled, so
1659 } else if (error != 0) {
1660 struct scsi_sense_data *sense;
1662 int sense_key, error_code;
1665 struct ccb_getdev cgd;
1667 /* Don't wedge this device's queue */
1668 status = done_ccb->ccb_h.status;
1669 if ((status & CAM_DEV_QFRZN) != 0)
1670 cam_release_devq(done_ccb->ccb_h.path,
1674 /*getcount_only*/0);
1677 xpt_setup_ccb(&cgd.ccb_h,
1678 done_ccb->ccb_h.path,
1680 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1681 xpt_action((union ccb *)&cgd);
1683 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1684 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1685 || ((status & CAM_AUTOSNS_VALID) == 0))
1691 sense = &csio->sense_data;
1692 scsi_extract_sense(sense, &error_code,
1697 * Attach to anything that claims to be a
1698 * direct access or optical disk device,
1699 * as long as it doesn't return a "Logical
1700 * unit not supported" (0x25) error.
1702 if ((have_sense) && (asc != 0x25)
1703 && (error_code == SSD_CURRENT_ERROR)) {
1704 const char *sense_key_desc;
1705 const char *asc_desc;
1707 scsi_sense_desc(sense_key, asc, ascq,
1711 ksnprintf(announce_buf,
1712 sizeof(announce_buf),
1713 "Attempt to query device "
1714 "size failed: %s, %s",
1722 xpt_print(periph->path,
1723 "got CAM status %#x\n",
1724 done_ccb->ccb_h.status);
1727 xpt_print(periph->path, "fatal error, "
1728 "failed to attach to device\n");
1731 * Free up resources.
1733 cam_periph_invalidate(periph);
1737 kfree(csio->data_ptr, M_SCSIDA);
1738 if (announce_buf[0] != '\0') {
1739 xpt_announce_periph(periph, announce_buf);
1741 * Create our sysctl variables, now that we know
1742 * we have successfully attached.
1744 taskqueue_enqueue(taskqueue_thread[mycpuid],
1745 &softc->sysctl_task);
1747 softc->state = DA_STATE_NORMAL;
1749 * Since our peripheral may be invalidated by an error
1750 * above or an external event, we must release our CCB
1751 * before releasing the probe lock on the peripheral.
1752 * The peripheral will only go away once the last lock
1753 * is removed, and we need it around for the CCB release
1756 xpt_release_ccb(done_ccb);
1757 cam_periph_unhold(periph, 0);
1760 case DA_CCB_WAITING:
1762 /* Caller will release the CCB */
1763 wakeup(&done_ccb->ccb_h.cbfcnp);
1767 /* No-op. We're polling */
1772 xpt_release_ccb(done_ccb);
1776 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1778 struct da_softc *softc;
1779 struct cam_periph *periph;
1782 periph = xpt_path_periph(ccb->ccb_h.path);
1783 softc = (struct da_softc *)periph->softc;
1786 * Automatically detect devices that do not support
1787 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
1790 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
1791 error = cmd6workaround(ccb);
1792 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
1793 CAM_SCSI_STATUS_ERROR)
1794 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
1795 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1796 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
1797 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
1798 int sense_key, error_code, asc, ascq;
1800 scsi_extract_sense(&ccb->csio.sense_data,
1801 &error_code, &sense_key, &asc, &ascq);
1802 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
1803 error = cmd6workaround(ccb);
1805 if (error == ERESTART)
1810 * Until we have a better way of doing pack validation,
1811 * don't treat UAs as errors.
1813 sense_flags |= SF_RETRY_UA;
1814 return(cam_periph_error(ccb, cam_flags, sense_flags,
1815 &softc->saved_ccb));
1819 daprevent(struct cam_periph *periph, int action)
1821 struct da_softc *softc;
1825 softc = (struct da_softc *)periph->softc;
1827 if (((action == PR_ALLOW)
1828 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
1829 || ((action == PR_PREVENT)
1830 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
1834 ccb = cam_periph_getccb(periph, /*priority*/1);
1836 scsi_prevent(&ccb->csio,
1844 error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
1845 SF_RETRY_UA, &softc->device_stats);
1848 if (action == PR_ALLOW)
1849 softc->flags &= ~DA_FLAG_PACK_LOCKED;
1851 softc->flags |= DA_FLAG_PACK_LOCKED;
1854 xpt_release_ccb(ccb);
1858 dagetcapacity(struct cam_periph *periph)
1860 struct da_softc *softc;
1862 struct scsi_read_capacity_data *rcap;
1863 struct scsi_read_capacity_data_16 *rcaplong;
1868 softc = (struct da_softc *)periph->softc;
1873 /* Do a read capacity */
1874 rcap = (struct scsi_read_capacity_data *)kmalloc(sizeof(*rcaplong),
1875 M_SCSIDA, M_INTWAIT);
1877 ccb = cam_periph_getccb(periph, /*priority*/1);
1878 scsi_read_capacity(&ccb->csio,
1885 ccb->ccb_h.ccb_bio = NULL;
1887 error = cam_periph_runccb(ccb, daerror,
1888 /*cam_flags*/CAM_RETRY_SELTO,
1889 /*sense_flags*/SF_RETRY_UA,
1890 &softc->device_stats);
1892 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1893 cam_release_devq(ccb->ccb_h.path,
1897 /*getcount_only*/0);
1900 block_len = scsi_4btoul(rcap->length);
1901 maxsector = scsi_4btoul(rcap->addr);
1903 if (maxsector != 0xffffffff)
1908 rcaplong = (struct scsi_read_capacity_data_16 *)rcap;
1910 scsi_read_capacity_16(&ccb->csio,
1913 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1918 /*sense_len*/ SSD_FULL_SIZE,
1920 ccb->ccb_h.ccb_bio = NULL;
1922 error = cam_periph_runccb(ccb, daerror,
1923 /*cam_flags*/CAM_RETRY_SELTO,
1924 /*sense_flags*/SF_RETRY_UA,
1925 &softc->device_stats);
1927 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1928 cam_release_devq(ccb->ccb_h.path,
1932 /*getcount_only*/0);
1935 block_len = scsi_4btoul(rcaplong->length);
1936 maxsector = scsi_8btou64(rcaplong->addr);
1942 dasetgeom(periph, block_len, maxsector);
1944 xpt_release_ccb(ccb);
1946 kfree(rcap, M_SCSIDA);
1952 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector)
1954 struct ccb_calc_geometry ccg;
1955 struct da_softc *softc;
1956 struct disk_params *dp;
1958 softc = (struct da_softc *)periph->softc;
1960 dp = &softc->params;
1961 dp->secsize = block_len;
1962 dp->sectors = maxsector + 1;
1964 * Have the controller provide us with a geometry
1965 * for this disk. The only time the geometry
1966 * matters is when we boot and the controller
1967 * is the only one knowledgeable enough to come
1968 * up with something that will make this a bootable
1971 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
1972 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
1973 ccg.block_size = dp->secsize;
1974 ccg.volume_size = dp->sectors;
1976 ccg.secs_per_track = 0;
1978 xpt_action((union ccb*)&ccg);
1979 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1981 * We don't know what went wrong here- but just pick
1982 * a geometry so we don't have nasty things like divide
1986 dp->secs_per_track = 255;
1987 dp->cylinders = dp->sectors / (255 * 255);
1988 if (dp->cylinders == 0) {
1992 dp->heads = ccg.heads;
1993 dp->secs_per_track = ccg.secs_per_track;
1994 dp->cylinders = ccg.cylinders;
1999 dasendorderedtag(void *arg)
2001 struct da_softc *softc = arg;
2003 if (da_send_ordered) {
2004 if ((softc->ordered_tag_count == 0)
2005 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
2006 softc->flags |= DA_FLAG_NEED_OTAG;
2008 if (softc->outstanding_cmds > 0)
2009 softc->flags &= ~DA_FLAG_WENT_IDLE;
2011 softc->ordered_tag_count = 0;
2013 /* Queue us up again */
2014 callout_reset(&softc->sendordered_c,
2015 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
2016 dasendorderedtag, softc);
2020 * Step through all DA peripheral drivers, and if the device is still open,
2021 * sync the disk cache to physical media.
2024 dashutdown(void * arg, int howto)
2026 struct cam_periph *periph;
2027 struct da_softc *softc;
2029 TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
2032 cam_periph_lock(periph);
2033 softc = (struct da_softc *)periph->softc;
2036 * We only sync the cache if the drive is still open, and
2037 * if the drive is capable of it..
2039 if (((softc->flags & DA_FLAG_OPEN) == 0)
2040 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
2041 cam_periph_unlock(periph);
2045 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
2047 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
2048 scsi_synchronize_cache(&ccb.csio,
2052 /*begin_lba*/0, /* whole disk */
2057 xpt_polled_action(&ccb);
2059 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2060 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2061 CAM_SCSI_STATUS_ERROR)
2062 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
2063 int error_code, sense_key, asc, ascq;
2065 scsi_extract_sense(&ccb.csio.sense_data,
2066 &error_code, &sense_key,
2069 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
2070 scsi_sense_print(&ccb.csio);
2072 xpt_print(periph->path, "Synchronize "
2073 "cache failed, status == 0x%x, scsi status "
2074 "== 0x%x\n", ccb.ccb_h.status,
2075 ccb.csio.scsi_status);
2079 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
2080 cam_release_devq(ccb.ccb_h.path,
2084 /*getcount_only*/0);
2086 cam_periph_unlock(periph);
2090 #else /* !_KERNEL */
2093 * XXX This is only left out of the kernel build to silence warnings. If,
2094 * for some reason this function is used in the kernel, the ifdefs should
2095 * be moved so it is included both in the kernel and userland.
2098 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2099 void (*cbfcnp)(struct cam_periph *, union ccb *),
2100 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2101 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2104 struct scsi_format_unit *scsi_cmd;
2106 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2107 scsi_cmd->opcode = FORMAT_UNIT;
2108 scsi_cmd->byte2 = byte2;
2109 scsi_ulto2b(ileave, scsi_cmd->interleave);
2114 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2123 #endif /* _KERNEL */