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>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/sysctl.h>
41 #include <sys/taskqueue.h>
44 #include <sys/devicestat.h>
46 #include <sys/dtype.h>
47 #include <sys/eventhandler.h>
48 #include <sys/malloc.h>
53 #include <sys/thread2.h>
54 #include <sys/mplock2.h>
67 #include <sys/camlib.h>
69 #include "../cam_ccb.h"
70 #include "../cam_extend.h"
71 #include "../cam_periph.h"
72 #include "../cam_xpt_periph.h"
73 #include "../cam_sim.h"
75 #include "scsi_message.h"
89 DA_FLAG_PACK_INVALID = 0x001,
90 DA_FLAG_NEW_PACK = 0x002,
91 DA_FLAG_PACK_LOCKED = 0x004,
92 DA_FLAG_PACK_REMOVABLE = 0x008,
93 DA_FLAG_TAGGED_QUEUING = 0x010,
94 DA_FLAG_NEED_OTAG = 0x020,
95 DA_FLAG_WENT_IDLE = 0x040,
96 DA_FLAG_RETRY_UA = 0x080,
98 DA_FLAG_SCTX_INIT = 0x200
103 DA_Q_NO_SYNC_CACHE = 0x01,
104 DA_Q_NO_6_BYTE = 0x02,
105 DA_Q_NO_PREVENT = 0x04
110 DA_CCB_PROBE2 = 0x02,
111 DA_CCB_BUFFER_IO = 0x03,
112 DA_CCB_WAITING = 0x04,
114 DA_CCB_TYPE_MASK = 0x0F,
115 DA_CCB_RETRY_UA = 0x10
118 /* Offsets into our private area for storing information */
119 #define ccb_state ppriv_field0
120 #define ccb_bio ppriv_ptr1
125 u_int8_t secs_per_track;
126 u_int32_t secsize; /* Number of bytes/sector */
127 u_int64_t sectors; /* total number sectors */
131 struct bio_queue_head bio_queue;
132 struct devstat device_stats;
133 SLIST_ENTRY(da_softc) links;
134 LIST_HEAD(, ccb_hdr) pending_ccbs;
138 int minimum_cmd_size;
139 int ordered_tag_count;
140 int outstanding_cmds;
141 struct disk_params params;
144 struct task sysctl_task;
145 struct sysctl_ctx_list sysctl_ctx;
146 struct sysctl_oid *sysctl_tree;
147 struct callout sendordered_c;
150 struct da_quirk_entry {
151 struct scsi_inquiry_pattern inq_pat;
155 static const char quantum[] = "QUANTUM";
156 static const char microp[] = "MICROP";
158 static struct da_quirk_entry da_quirk_table[] =
160 /* SPI, FC devices */
163 * Fujitsu M2513A MO drives.
164 * Tested devices: M2513A2 firmware versions 1200 & 1300.
165 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
166 * Reported by: W.Scholten <whs@xs4all.nl>
168 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
169 /*quirks*/ DA_Q_NO_SYNC_CACHE
173 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
174 /*quirks*/ DA_Q_NO_SYNC_CACHE
178 * This particular Fujitsu drive doesn't like the
179 * synchronize cache command.
180 * Reported by: Tom Jackson <toj@gorilla.net>
182 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
183 /*quirks*/ DA_Q_NO_SYNC_CACHE
187 * This drive doesn't like the synchronize cache command
188 * either. Reported by: Matthew Jacob <mjacob@feral.com>
189 * in NetBSD PR kern/6027, August 24, 1998.
191 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
192 /*quirks*/ DA_Q_NO_SYNC_CACHE
196 * This drive doesn't like the synchronize cache command
197 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
200 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
201 /*quirks*/ DA_Q_NO_SYNC_CACHE
205 * Doesn't like the synchronize cache command.
206 * Reported by: Blaz Zupan <blaz@gold.amis.net>
208 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
209 /*quirks*/ DA_Q_NO_SYNC_CACHE
213 * Doesn't like the synchronize cache command.
214 * Reported by: Blaz Zupan <blaz@gold.amis.net>
216 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
217 /*quirks*/ DA_Q_NO_SYNC_CACHE
221 * Doesn't like the synchronize cache command.
223 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
224 /*quirks*/ DA_Q_NO_SYNC_CACHE
228 * Doesn't like the synchronize cache command.
229 * Reported by: walter@pelissero.de
231 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
232 /*quirks*/ DA_Q_NO_SYNC_CACHE
236 * Doesn't work correctly with 6 byte reads/writes.
237 * Returns illegal request, and points to byte 9 of the
239 * Reported by: Adam McDougall <bsdx@spawnet.com>
241 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
242 /*quirks*/ DA_Q_NO_6_BYTE
246 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
247 /*quirks*/ DA_Q_NO_6_BYTE
251 * Doesn't like the synchronize cache command.
252 * Reported by: walter@pelissero.de
254 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
255 /*quirks*/ DA_Q_NO_SYNC_CACHE
259 * The CISS RAID controllers do not support SYNC_CACHE
261 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
262 /*quirks*/ DA_Q_NO_SYNC_CACHE
265 * USB mass storage devices supported by umass(4)
267 * NOTE: USB attachments automatically set DA_Q_NO_SYNC_CACHE so
268 * it does not have to be specified here.
272 * Creative Nomad MUVO mp3 player (USB)
275 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
276 /*quirks*/ DA_Q_NO_PREVENT
280 * Sigmatel USB Flash MP3 Player
283 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
284 /*quirks*/ DA_Q_NO_PREVENT
288 * SEAGRAND NP-900 MP3 Player
291 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
292 /*quirks*/ DA_Q_NO_PREVENT
296 * Creative MUVO Slim mp3 player (USB)
299 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
300 "*"}, /*quirks*/ DA_Q_NO_PREVENT
304 * Philips USB Key Audio KEY013
307 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
308 /*quirks*/ DA_Q_NO_PREVENT
312 static d_open_t daopen;
313 static d_close_t daclose;
314 static d_strategy_t dastrategy;
315 static d_dump_t dadump;
316 static periph_init_t dainit;
317 static void daasync(void *callback_arg, u_int32_t code,
318 struct cam_path *path, void *arg);
319 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
320 static periph_ctor_t daregister;
321 static periph_dtor_t dacleanup;
322 static periph_start_t dastart;
323 static periph_oninv_t daoninvalidate;
324 static void dadone(struct cam_periph *periph,
325 union ccb *done_ccb);
326 static int daerror(union ccb *ccb, u_int32_t cam_flags,
327 u_int32_t sense_flags);
328 static void daprevent(struct cam_periph *periph, int action);
329 static int dagetcapacity(struct cam_periph *periph);
330 static int dacheckmedia(struct cam_periph *periph);
331 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
334 static timeout_t dasendorderedtag;
335 static void dashutdown(void *arg, int howto);
337 #ifndef DA_DEFAULT_TIMEOUT
338 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
341 #ifndef DA_DEFAULT_RETRY
342 #define DA_DEFAULT_RETRY 4
345 #ifndef DA_DEFAULT_SEND_ORDERED
346 #define DA_DEFAULT_SEND_ORDERED 1
349 static int da_retry_count = DA_DEFAULT_RETRY;
350 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
351 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
352 static struct callout dasendorderedtag_ch;
354 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
355 "CAM Direct Access Disk driver");
356 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
357 &da_retry_count, 0, "Normal I/O retry count");
358 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
359 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
360 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
361 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
362 SYSCTL_INT(_kern_cam_da, OID_AUTO, da_send_ordered, CTLFLAG_RW,
363 &da_send_ordered, 0, "Send Ordered Tags");
364 TUNABLE_INT("kern.cam.da.da_send_ordered", &da_send_ordered);
367 * DA_ORDEREDTAG_INTERVAL determines how often, relative
368 * to the default timeout, we check to see whether an ordered
369 * tagged transaction is appropriate to prevent simple tag
370 * starvation. Since we'd like to ensure that there is at least
371 * 1/2 of the timeout length left for a starved transaction to
372 * complete after we've sent an ordered tag, we must poll at least
373 * four times in every timeout period. This takes care of the worst
374 * case where a starved transaction starts during an interval that
375 * meets the requirement "don't send an ordered tag" test so it takes
376 * us two intervals to determine that a tag must be sent.
378 #ifndef DA_ORDEREDTAG_INTERVAL
379 #define DA_ORDEREDTAG_INTERVAL 4
382 static struct periph_driver dadriver =
385 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
388 PERIPHDRIVER_DECLARE(da, dadriver);
390 static struct dev_ops da_ops = {
391 { "da", DA_CDEV_MAJOR, D_DISK | D_MPSAFE },
395 .d_write = physwrite,
396 .d_strategy = dastrategy,
400 static struct extend_array *daperiphs;
402 MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
405 daopen(struct dev_open_args *ap)
407 cdev_t dev = ap->a_head.a_dev;
408 struct cam_periph *periph;
409 struct da_softc *softc;
410 struct disk_info info;
415 periph = cam_extend_get(daperiphs, unit);
416 if (periph == NULL) {
420 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
424 cam_periph_lock(periph);
425 if ((error = cam_periph_hold(periph, PCATCH)) != 0) {
426 cam_periph_unlock(periph);
427 cam_periph_release(periph);
431 unit = periph->unit_number;
432 softc = (struct da_softc *)periph->softc;
434 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
435 ("daopen: dev=%s (unit %d)\n", devtoname(dev),
438 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
439 /* Invalidate our pack information. */
440 disk_invalidate(&softc->disk);
441 softc->flags &= ~DA_FLAG_PACK_INVALID;
444 error = dacheckmedia(periph);
445 softc->flags |= DA_FLAG_OPEN;
448 struct ccb_getdev cgd;
450 /* Build disk information structure */
451 bzero(&info, sizeof(info));
452 info.d_type = DTYPE_SCSI;
455 * Grab the inquiry data to get the vendor and product names.
456 * Put them in the typename and packname for the label.
458 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
459 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
460 xpt_action((union ccb *)&cgd);
463 * Check to see whether or not the blocksize is set yet.
464 * If it isn't, set it and then clear the blocksize
465 * unavailable flag for the device statistics.
467 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0){
468 softc->device_stats.block_size = softc->params.secsize;
469 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
474 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
475 (softc->quirks & DA_Q_NO_PREVENT) == 0)
476 daprevent(periph, PR_PREVENT);
478 softc->flags &= ~DA_FLAG_OPEN;
479 cam_periph_release(periph);
481 cam_periph_unhold(periph, 1);
486 daclose(struct dev_close_args *ap)
488 cdev_t dev = ap->a_head.a_dev;
489 struct cam_periph *periph;
490 struct da_softc *softc;
495 periph = cam_extend_get(daperiphs, unit);
499 cam_periph_lock(periph);
500 if ((error = cam_periph_hold(periph, 0)) != 0) {
501 cam_periph_unlock(periph);
502 cam_periph_release(periph);
506 softc = (struct da_softc *)periph->softc;
508 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
511 ccb = cam_periph_getccb(periph, /*priority*/1);
513 scsi_synchronize_cache(&ccb->csio,
517 /*begin_lba*/0,/* Cover the whole disk */
522 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
523 /*sense_flags*/SF_RETRY_UA,
524 &softc->device_stats);
526 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
527 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
528 CAM_SCSI_STATUS_ERROR) {
530 int sense_key, error_code;
532 scsi_extract_sense(&ccb->csio.sense_data,
536 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
537 scsi_sense_print(&ccb->csio);
539 xpt_print(periph->path, "Synchronize cache "
540 "failed, status == 0x%x, scsi status == "
541 "0x%x\n", ccb->csio.ccb_h.status,
542 ccb->csio.scsi_status);
546 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
547 cam_release_devq(ccb->ccb_h.path,
553 xpt_release_ccb(ccb);
557 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
558 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
559 daprevent(periph, PR_ALLOW);
561 * If we've got removeable media, mark the blocksize as
562 * unavailable, since it could change when new media is
565 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
569 * Don't compound any ref counting software bugs with more.
571 if (softc->flags & DA_FLAG_OPEN) {
572 softc->flags &= ~DA_FLAG_OPEN;
573 cam_periph_release(periph);
575 xpt_print(periph->path,
576 "daclose() called on an already closed device!\n");
578 cam_periph_unhold(periph, 1);
583 * Actually translate the requested transfer into one the physical driver
584 * can understand. The transfer is described by a buf and will include
585 * only one physical transfer.
588 dastrategy(struct dev_strategy_args *ap)
590 cdev_t dev = ap->a_head.a_dev;
591 struct bio *bio = ap->a_bio;
592 struct buf *bp = bio->bio_buf;
593 struct cam_periph *periph;
594 struct da_softc *softc;
600 periph = cam_extend_get(daperiphs, unit);
601 if (periph == NULL) {
605 softc = (struct da_softc *)periph->softc;
607 cam_periph_lock(periph);
611 * check it's not too big a transfer for our adapter
613 scsi_minphys(bp, &sd_switch);
617 * Mask interrupts so that the pack cannot be invalidated until
618 * after we are in the queue. Otherwise, we might not properly
619 * clean up one of the buffers.
623 * If the device has been made invalid, error out
625 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
626 cam_periph_unlock(periph);
632 * Place it in the queue of disk activities for this disk
634 bioqdisksort(&softc->bio_queue, bio);
637 * Schedule ourselves for performing the work.
639 xpt_schedule(periph, /* XXX priority */1);
640 cam_periph_unlock(periph);
644 bp->b_flags |= B_ERROR;
647 * Correctly set the buf to indicate a completed xfer
649 bp->b_resid = bp->b_bcount;
655 dadump(struct dev_dump_args *ap)
657 cdev_t dev = ap->a_head.a_dev;
658 struct cam_periph *periph;
659 struct da_softc *softc;
662 struct ccb_scsiio csio;
665 periph = cam_extend_get(daperiphs, unit);
669 softc = (struct da_softc *)periph->softc;
670 cam_periph_lock(periph);
671 secsize = softc->params.secsize; /* XXX: or ap->a_secsize? */
673 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
674 cam_periph_unlock(periph);
679 * because length == 0 means we are supposed to flush cache, we only
680 * try to write something if length > 0.
682 if (ap->a_length > 0) {
683 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
684 csio.ccb_h.flags |= CAM_POLLED;
685 csio.ccb_h.ccb_state = DA_CCB_DUMP;
686 scsi_read_write(&csio,
692 /*minimum_cmd_size*/ softc->minimum_cmd_size,
693 ap->a_offset / secsize,
694 ap->a_length / secsize,
695 /*data_ptr*/(u_int8_t *) ap->a_virtual,
696 /*dxfer_len*/ap->a_length,
697 /*sense_len*/SSD_FULL_SIZE,
698 DA_DEFAULT_TIMEOUT * 1000);
699 xpt_polled_action((union ccb *)&csio);
701 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
702 kprintf("Aborting dump due to I/O error.\n");
703 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
704 CAM_SCSI_STATUS_ERROR)
705 scsi_sense_print(&csio);
707 kprintf("status == 0x%x, scsi status == 0x%x\n",
708 csio.ccb_h.status, csio.scsi_status);
711 cam_periph_unlock(periph);
716 * Sync the disk cache contents to the physical media.
718 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
720 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
721 csio.ccb_h.ccb_state = DA_CCB_DUMP;
722 scsi_synchronize_cache(&csio,
726 /*begin_lba*/0,/* Cover the whole disk */
730 xpt_polled_action((union ccb *)&csio);
732 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
733 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
734 CAM_SCSI_STATUS_ERROR) {
736 int sense_key, error_code;
738 scsi_extract_sense(&csio.sense_data,
742 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
743 scsi_sense_print(&csio);
745 xpt_print(periph->path, "Synchronize cache "
746 "failed, status == 0x%x, scsi status == "
747 "0x%x\n", csio.ccb_h.status,
752 cam_periph_unlock(periph);
762 * Create our extend array for storing the devices we attach to.
764 daperiphs = cam_extend_new();
765 if (daperiphs == NULL) {
766 kprintf("da: Failed to alloc extend array!\n");
770 callout_init(&dasendorderedtag_ch);
773 * Install a global async callback. This callback will
774 * receive async callbacks like "new device found".
776 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
778 if (status != CAM_REQ_CMP) {
779 kprintf("da: Failed to attach master async callback "
780 "due to status 0x%x!\n", status);
781 } else if (da_send_ordered) {
783 /* Register our shutdown event handler */
784 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
785 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
786 kprintf("dainit: shutdown event registration failed!\n");
791 daoninvalidate(struct cam_periph *periph)
793 struct da_softc *softc;
797 softc = (struct da_softc *)periph->softc;
800 * De-register any async callbacks.
802 xpt_register_async(0, daasync, periph, periph->path);
804 softc->flags |= DA_FLAG_PACK_INVALID;
807 * Return all queued I/O with ENXIO.
808 * XXX Handle any transactions queued to the card
809 * with XPT_ABORT_CCB.
811 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL){
812 bioq_remove(&softc->bio_queue, q_bio);
813 q_bp = q_bio->bio_buf;
814 q_bp->b_resid = q_bp->b_bcount;
815 q_bp->b_error = ENXIO;
816 q_bp->b_flags |= B_ERROR;
819 xpt_print(periph->path, "lost device\n");
823 dacleanup(struct cam_periph *periph)
825 struct da_softc *softc;
827 softc = (struct da_softc *)periph->softc;
829 devstat_remove_entry(&softc->device_stats);
830 cam_extend_release(daperiphs, periph->unit_number);
831 xpt_print(periph->path, "removing device entry\n");
833 * If we can't free the sysctl tree, oh well...
835 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
836 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
837 xpt_print(periph->path, "can't remove sysctl context\n");
839 periph->softc = NULL;
840 if (softc->disk.d_rawdev) {
841 cam_periph_unlock(periph);
842 disk_destroy(&softc->disk);
843 cam_periph_lock(periph);
846 callout_stop(&softc->sendordered_c);
847 kfree(softc, M_DEVBUF);
851 daasync(void *callback_arg, u_int32_t code,
852 struct cam_path *path, void *arg)
854 struct cam_periph *periph;
856 periph = (struct cam_periph *)callback_arg;
859 case AC_FOUND_DEVICE:
861 struct ccb_getdev *cgd;
865 cgd = (struct ccb_getdev *)arg;
869 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
870 && SID_TYPE(&cgd->inq_data) != T_RBC
871 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
875 * Don't complain if a valid peripheral is already attached.
877 periph = cam_periph_find(cgd->ccb_h.path, "da");
878 if (periph && (periph->flags & CAM_PERIPH_INVALID) == 0)
882 * Allocate a peripheral instance for
883 * this device and start the probe
886 sim = xpt_path_sim(cgd->ccb_h.path);
887 status = cam_periph_alloc(daregister, daoninvalidate,
889 "da", CAM_PERIPH_BIO,
890 cgd->ccb_h.path, daasync,
891 AC_FOUND_DEVICE, cgd);
893 if (status != CAM_REQ_CMP && status != CAM_REQ_INPROG) {
894 kprintf("daasync: Unable to attach to new device "
895 "due to status 0x%x\n", status);
902 struct da_softc *softc;
903 struct ccb_hdr *ccbh;
905 softc = (struct da_softc *)periph->softc;
907 * Don't fail on the expected unit attention
910 softc->flags |= DA_FLAG_RETRY_UA;
911 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
912 ccbh->ccb_state |= DA_CCB_RETRY_UA;
916 cam_periph_async(periph, code, path, arg);
922 dasysctlinit(void *context, int pending)
924 struct cam_periph *periph;
925 struct da_softc *softc;
926 char tmpstr[80], tmpstr2[80];
929 periph = (struct cam_periph *)context;
930 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
935 softc = (struct da_softc *)periph->softc;
936 ksnprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
937 ksnprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
939 sysctl_ctx_init(&softc->sysctl_ctx);
940 softc->flags |= DA_FLAG_SCTX_INIT;
941 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
942 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
943 CTLFLAG_RD, 0, tmpstr);
944 if (softc->sysctl_tree == NULL) {
945 kprintf("dasysctlinit: unable to allocate sysctl tree\n");
946 cam_periph_release(periph);
952 * Now register the sysctl handler, so the user can the value on
955 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
956 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
957 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
960 cam_periph_release(periph);
965 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
969 value = *(int *)arg1;
971 error = sysctl_handle_int(oidp, &value, 0, req);
974 || (req->newptr == NULL))
978 * Acceptable values here are 6, 10 or 12, or 16.
985 else if ((value > 10)
991 *(int *)arg1 = value;
997 daregister(struct cam_periph *periph, void *arg)
999 struct da_softc *softc;
1000 struct ccb_pathinq cpi;
1001 struct ccb_getdev *cgd;
1005 cgd = (struct ccb_getdev *)arg;
1006 if (periph == NULL) {
1007 kprintf("daregister: periph was NULL!!\n");
1008 return(CAM_REQ_CMP_ERR);
1012 kprintf("daregister: no getdev CCB, can't register device\n");
1013 return(CAM_REQ_CMP_ERR);
1016 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
1017 LIST_INIT(&softc->pending_ccbs);
1018 softc->state = DA_STATE_PROBE;
1019 bioq_init(&softc->bio_queue);
1020 if (SID_IS_REMOVABLE(&cgd->inq_data))
1021 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1022 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1023 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1025 periph->softc = softc;
1027 cam_extend_set(daperiphs, periph->unit_number, periph);
1030 * See if this device has any quirks.
1032 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1033 (caddr_t)da_quirk_table,
1034 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1035 sizeof(*da_quirk_table), scsi_inquiry_match);
1038 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1040 softc->quirks = DA_Q_NONE;
1043 * Unconditionally disable the synchronize cache command for
1044 * usb attachments. It's just impossible to determine if the
1045 * device supports it or not and if it doesn't the port can
1048 if (strncmp(periph->sim->sim_name, "umass", 4) == 0) {
1049 softc->quirks |= DA_Q_NO_SYNC_CACHE;
1052 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1054 /* Check if the SIM does not want 6 byte commands */
1055 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
1056 cpi.ccb_h.func_code = XPT_PATH_INQ;
1057 xpt_action((union ccb *)&cpi);
1058 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1059 softc->quirks |= DA_Q_NO_6_BYTE;
1062 * RBC devices don't have to support READ(6), only READ(10).
1064 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1065 softc->minimum_cmd_size = 10;
1067 softc->minimum_cmd_size = 6;
1070 * Load the user's default, if any.
1072 ksnprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1073 periph->unit_number);
1074 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1077 * 6, 10, 12, and 16 are the currently permissible values.
1079 if (softc->minimum_cmd_size < 6)
1080 softc->minimum_cmd_size = 6;
1081 else if ((softc->minimum_cmd_size > 6)
1082 && (softc->minimum_cmd_size <= 10))
1083 softc->minimum_cmd_size = 10;
1084 else if ((softc->minimum_cmd_size > 10)
1085 && (softc->minimum_cmd_size <= 12))
1086 softc->minimum_cmd_size = 12;
1087 else if (softc->minimum_cmd_size > 12)
1088 softc->minimum_cmd_size = 16;
1091 * The DA driver supports a blocksize, but
1092 * we don't know the blocksize until we do
1093 * a read capacity. So, set a flag to
1094 * indicate that the blocksize is
1095 * unavailable right now. We'll clear the
1096 * flag as soon as we've done a read capacity.
1098 devstat_add_entry(&softc->device_stats, "da",
1099 periph->unit_number, 0,
1100 DEVSTAT_BS_UNAVAILABLE,
1101 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
1102 DEVSTAT_PRIORITY_DISK);
1105 * Register this media as a disk
1107 CAM_SIM_UNLOCK(periph->sim);
1108 disk_create(periph->unit_number, &softc->disk, &da_ops);
1109 softc->disk.d_rawdev->si_iosize_max = MAXPHYS;
1110 CAM_SIM_LOCK(periph->sim);
1113 * Add async callbacks for bus reset and
1114 * bus device reset calls. I don't bother
1115 * checking if this fails as, in most cases,
1116 * the system will function just fine without
1117 * them and the only alternative would be to
1118 * not attach the device on failure.
1120 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
1121 daasync, periph, periph->path);
1124 * Take an exclusive refcount on the periph while dastart is called
1125 * to finish the probe. The reference will be dropped in dadone at
1128 cam_periph_hold(periph, 0);
1129 xpt_schedule(periph, /*priority*/5);
1132 * Schedule a periodic event to occasionally send an
1133 * ordered tag to a device.
1135 callout_init(&softc->sendordered_c);
1136 callout_reset(&softc->sendordered_c,
1137 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
1138 dasendorderedtag, softc);
1140 return(CAM_REQ_CMP);
1144 dastart(struct cam_periph *periph, union ccb *start_ccb)
1146 struct da_softc *softc;
1148 softc = (struct da_softc *)periph->softc;
1150 switch (softc->state) {
1151 case DA_STATE_NORMAL:
1153 /* Pull a buffer from the queue and get going on it */
1159 * See if there is a buf with work for us to do..
1161 bio = bioq_first(&softc->bio_queue);
1162 if (periph->immediate_priority <= periph->pinfo.priority) {
1163 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1164 ("queuing for immediate ccb\n"));
1165 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1166 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1168 periph->immediate_priority = CAM_PRIORITY_NONE;
1169 wakeup(&periph->ccb_list);
1172 * Have more work to do, so ensure we stay
1175 xpt_schedule(periph, /* XXX priority */1);
1180 xpt_release_ccb(start_ccb);
1185 * We can queue new work.
1187 bioq_remove(&softc->bio_queue, bio);
1190 devstat_start_transaction(&softc->device_stats);
1192 if ((bp->b_flags & B_ORDERED) != 0 ||
1193 (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1194 softc->flags &= ~DA_FLAG_NEED_OTAG;
1195 softc->ordered_tag_count++;
1196 tag_code = MSG_ORDERED_Q_TAG;
1198 tag_code = MSG_SIMPLE_Q_TAG;
1205 * Block read/write op
1207 KKASSERT(bio->bio_offset % softc->params.secsize == 0);
1211 da_retry_count, /* retries */
1214 (bp->b_cmd == BUF_CMD_READ),
1216 softc->minimum_cmd_size,
1217 bio->bio_offset / softc->params.secsize,
1218 bp->b_bcount / softc->params.secsize,
1221 SSD_FULL_SIZE, /* sense_len */
1222 da_default_timeout * 1000
1227 * Silently complete a flush request if the device
1230 if (softc->quirks & DA_Q_NO_SYNC_CACHE) {
1231 xpt_release_ccb(start_ccb);
1233 devstat_end_transaction_buf(
1234 &softc->device_stats, bp);
1237 scsi_synchronize_cache(
1240 dadone, /* cbfcnp */
1243 0, /* count (whole disk) */
1245 da_default_timeout*1000 /* timeout */
1250 xpt_release_ccb(start_ccb);
1252 panic("dastart: unrecognized bio cmd %d", bp->b_cmd);
1253 break; /* NOT REACHED */
1257 * Block out any asyncronous callbacks
1258 * while we touch the pending ccb list.
1261 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1262 LIST_INSERT_HEAD(&softc->pending_ccbs,
1263 &start_ccb->ccb_h, periph_links.le);
1264 softc->outstanding_cmds++;
1266 /* We expect a unit attention from this device */
1267 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1268 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1269 softc->flags &= ~DA_FLAG_RETRY_UA;
1272 start_ccb->ccb_h.ccb_bio = bio;
1273 xpt_action(start_ccb);
1277 * Be sure we stay scheduled if we have more work to do.
1279 bio = bioq_first(&softc->bio_queue);
1281 xpt_schedule(periph, 1);
1284 case DA_STATE_PROBE:
1286 struct ccb_scsiio *csio;
1287 struct scsi_read_capacity_data *rcap;
1289 rcap = kmalloc(sizeof(*rcap), M_SCSIDA, M_INTWAIT | M_ZERO);
1290 csio = &start_ccb->csio;
1291 scsi_read_capacity(csio,
1298 start_ccb->ccb_h.ccb_bio = NULL;
1299 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1300 xpt_action(start_ccb);
1303 case DA_STATE_PROBE2:
1305 struct ccb_scsiio *csio;
1306 struct scsi_read_capacity_data_16 *rcaplong;
1308 rcaplong = kmalloc(sizeof(*rcaplong), M_SCSIDA,
1309 M_INTWAIT | M_ZERO);
1310 if (rcaplong == NULL) {
1311 kprintf("dastart: Couldn't allocate read_capacity\n");
1312 /* da_free_periph??? */
1315 csio = &start_ccb->csio;
1316 scsi_read_capacity_16(csio,
1319 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1324 /*sense_len*/ SSD_FULL_SIZE,
1326 start_ccb->ccb_h.ccb_bio = NULL;
1327 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1328 xpt_action(start_ccb);
1335 cmd6workaround(union ccb *ccb)
1337 struct scsi_rw_6 cmd6;
1338 struct scsi_rw_10 *cmd10;
1339 struct da_softc *softc;
1343 cdb = ccb->csio.cdb_io.cdb_bytes;
1345 /* Translation only possible if CDB is an array and cmd is R/W6 */
1346 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1347 (*cdb != READ_6 && *cdb != WRITE_6))
1350 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
1351 "increasing minimum_cmd_size to 10.\n");
1352 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1353 softc->minimum_cmd_size = 10;
1355 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1356 cmd10 = (struct scsi_rw_10 *)cdb;
1357 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1359 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1360 cmd10->reserved = 0;
1361 scsi_ulto2b(cmd6.length, cmd10->length);
1362 cmd10->control = cmd6.control;
1363 ccb->csio.cdb_len = sizeof(*cmd10);
1365 /* Requeue request, unfreezing queue if necessary */
1366 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1367 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1370 cam_release_devq(ccb->ccb_h.path,
1374 /*getcount_only*/0);
1380 dadone(struct cam_periph *periph, union ccb *done_ccb)
1382 struct da_softc *softc;
1383 struct ccb_scsiio *csio;
1384 struct disk_info info;
1386 softc = (struct da_softc *)periph->softc;
1387 csio = &done_ccb->csio;
1388 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1389 case DA_CCB_BUFFER_IO:
1394 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1396 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1400 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1405 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1406 if (error == ERESTART) {
1408 * A retry was scheuled, so
1417 if (error == ENXIO) {
1419 * Catastrophic error. Mark our pack as
1423 * XXX See if this is really a media
1426 xpt_print(periph->path,
1427 "Invalidating pack\n");
1428 softc->flags |= DA_FLAG_PACK_INVALID;
1432 * return all queued I/O with EIO, so that
1433 * the client can retry these I/Os in the
1434 * proper order should it attempt to recover.
1436 while ((q_bio = bioq_first(&softc->bio_queue))
1438 bioq_remove(&softc->bio_queue, q_bio);
1439 q_bp = q_bio->bio_buf;
1440 q_bp->b_resid = q_bp->b_bcount;
1441 q_bp->b_error = EIO;
1442 q_bp->b_flags |= B_ERROR;
1445 bp->b_error = error;
1446 bp->b_resid = bp->b_bcount;
1447 bp->b_flags |= B_ERROR;
1449 bp->b_resid = csio->resid;
1451 if (bp->b_resid != 0)
1452 bp->b_flags |= B_ERROR;
1454 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1455 cam_release_devq(done_ccb->ccb_h.path,
1459 /*getcount_only*/0);
1461 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1462 panic("REQ_CMP with QFRZN");
1463 bp->b_resid = csio->resid;
1464 if (csio->resid > 0)
1465 bp->b_flags |= B_ERROR;
1469 * Block out any asyncronous callbacks
1470 * while we touch the pending ccb list.
1472 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1473 softc->outstanding_cmds--;
1474 if (softc->outstanding_cmds == 0)
1475 softc->flags |= DA_FLAG_WENT_IDLE;
1477 devstat_end_transaction_buf(&softc->device_stats, bp);
1484 struct scsi_read_capacity_data *rdcap;
1485 struct scsi_read_capacity_data_16 *rcaplong;
1486 char announce_buf[80];
1490 if (softc->state == DA_STATE_PROBE)
1491 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1493 rcaplong = (struct scsi_read_capacity_data_16 *)
1496 bzero(&info, sizeof(info));
1497 info.d_type = DTYPE_SCSI;
1498 info.d_serialno = xpt_path_serialno(periph->path);
1500 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1501 struct disk_params *dp;
1502 uint32_t block_size;
1505 if (softc->state == DA_STATE_PROBE) {
1506 block_size = scsi_4btoul(rdcap->length);
1507 maxsector = scsi_4btoul(rdcap->addr);
1510 * According to SBC-2, if the standard 10
1511 * byte READ CAPACITY command returns 2^32,
1512 * we should issue the 16 byte version of
1513 * the command, since the device in question
1514 * has more sectors than can be represented
1515 * with the short version of the command.
1517 if (maxsector == 0xffffffff) {
1518 softc->state = DA_STATE_PROBE2;
1519 kfree(rdcap, M_SCSIDA);
1520 xpt_release_ccb(done_ccb);
1521 xpt_schedule(periph, /*priority*/5);
1525 block_size = scsi_4btoul(rcaplong->length);
1526 maxsector = scsi_8btou64(rcaplong->addr);
1528 dasetgeom(periph, block_size, maxsector);
1529 dp = &softc->params;
1530 ksnprintf(announce_buf, sizeof(announce_buf),
1531 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1532 (uintmax_t) (((uintmax_t)dp->secsize *
1533 dp->sectors) / (1024*1024)),
1534 (uintmax_t)dp->sectors,
1535 dp->secsize, dp->heads, dp->secs_per_track,
1537 CAM_SIM_UNLOCK(periph->sim);
1538 info.d_media_blksize = softc->params.secsize;
1539 info.d_media_blocks = softc->params.sectors;
1540 info.d_media_size = 0;
1541 info.d_secpertrack = softc->params.secs_per_track;
1542 info.d_nheads = softc->params.heads;
1543 info.d_ncylinders = softc->params.cylinders;
1544 info.d_secpercyl = softc->params.heads *
1545 softc->params.secs_per_track;
1546 info.d_serialno = xpt_path_serialno(periph->path);
1547 disk_setdiskinfo(&softc->disk, &info);
1548 CAM_SIM_LOCK(periph->sim);
1552 announce_buf[0] = '\0';
1555 * Retry any UNIT ATTENTION type errors. They
1556 * are expected at boot.
1558 error = daerror(done_ccb, CAM_RETRY_SELTO,
1559 SF_RETRY_UA|SF_NO_PRINT);
1560 if (error == ERESTART) {
1562 * A retry was scheuled, so
1566 } else if (error != 0) {
1567 struct scsi_sense_data *sense;
1569 int sense_key, error_code;
1572 struct ccb_getdev cgd;
1574 /* Don't wedge this device's queue */
1575 status = done_ccb->ccb_h.status;
1576 if ((status & CAM_DEV_QFRZN) != 0)
1577 cam_release_devq(done_ccb->ccb_h.path,
1581 /*getcount_only*/0);
1584 xpt_setup_ccb(&cgd.ccb_h,
1585 done_ccb->ccb_h.path,
1587 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1588 xpt_action((union ccb *)&cgd);
1590 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1591 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1592 || ((status & CAM_AUTOSNS_VALID) == 0))
1598 sense = &csio->sense_data;
1599 scsi_extract_sense(sense, &error_code,
1604 * Attach to anything that claims to be a
1605 * direct access or optical disk device,
1606 * as long as it doesn't return a "Logical
1607 * unit not supported" (0x25) error.
1609 if ((have_sense) && (asc != 0x25)
1610 && (error_code == SSD_CURRENT_ERROR)) {
1611 const char *sense_key_desc;
1612 const char *asc_desc;
1614 scsi_sense_desc(sense_key, asc, ascq,
1618 ksnprintf(announce_buf,
1619 sizeof(announce_buf),
1620 "Attempt to query device "
1621 "size failed: %s, %s",
1624 info.d_media_blksize = 512;
1625 disk_setdiskinfo(&softc->disk, &info);
1631 xpt_print(periph->path,
1632 "got CAM status %#x\n",
1633 done_ccb->ccb_h.status);
1636 xpt_print(periph->path, "fatal error, "
1637 "failed to attach to device\n");
1640 * Free up resources.
1642 cam_periph_invalidate(periph);
1646 kfree(csio->data_ptr, M_SCSIDA);
1647 if (announce_buf[0] != '\0') {
1648 xpt_announce_periph(periph, announce_buf);
1650 * Create our sysctl variables, now that we know
1651 * we have successfully attached.
1653 taskqueue_enqueue(taskqueue_thread[mycpuid],
1654 &softc->sysctl_task);
1656 softc->state = DA_STATE_NORMAL;
1658 * Since our peripheral may be invalidated by an error
1659 * above or an external event, we must release our CCB
1660 * before releasing the probe lock on the peripheral.
1661 * The peripheral will only go away once the last lock
1662 * is removed, and we need it around for the CCB release
1665 xpt_release_ccb(done_ccb);
1666 cam_periph_unhold(periph, 0);
1669 case DA_CCB_WAITING:
1671 /* Caller will release the CCB */
1672 wakeup(&done_ccb->ccb_h.cbfcnp);
1676 /* No-op. We're polling */
1681 xpt_release_ccb(done_ccb);
1685 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1687 struct da_softc *softc;
1688 struct cam_periph *periph;
1691 periph = xpt_path_periph(ccb->ccb_h.path);
1692 softc = (struct da_softc *)periph->softc;
1695 * Automatically detect devices that do not support
1696 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
1699 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
1700 error = cmd6workaround(ccb);
1701 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
1702 CAM_SCSI_STATUS_ERROR)
1703 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
1704 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1705 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
1706 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
1707 int sense_key, error_code, asc, ascq;
1709 scsi_extract_sense(&ccb->csio.sense_data,
1710 &error_code, &sense_key, &asc, &ascq);
1711 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
1712 error = cmd6workaround(ccb);
1714 if (error == ERESTART)
1719 * Until we have a better way of doing pack validation,
1720 * don't treat UAs as errors.
1722 sense_flags |= SF_RETRY_UA;
1723 return(cam_periph_error(ccb, cam_flags, sense_flags,
1724 &softc->saved_ccb));
1728 daprevent(struct cam_periph *periph, int action)
1730 struct da_softc *softc;
1734 softc = (struct da_softc *)periph->softc;
1736 if (((action == PR_ALLOW)
1737 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
1738 || ((action == PR_PREVENT)
1739 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
1743 ccb = cam_periph_getccb(periph, /*priority*/1);
1745 scsi_prevent(&ccb->csio,
1753 error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
1754 SF_RETRY_UA, &softc->device_stats);
1757 if (action == PR_ALLOW)
1758 softc->flags &= ~DA_FLAG_PACK_LOCKED;
1760 softc->flags |= DA_FLAG_PACK_LOCKED;
1763 xpt_release_ccb(ccb);
1767 * Check media on open, e.g. card reader devices which had no initial media.
1770 dacheckmedia(struct cam_periph *periph)
1772 struct disk_params *dp;
1773 struct da_softc *softc;
1774 struct disk_info info;
1777 softc = (struct da_softc *)periph->softc;
1778 dp = &softc->params;
1780 error = dagetcapacity(periph);
1783 * Only reprobe on initial open and if the media is removable.
1785 * NOTE: If we setdiskinfo() it will take the device probe
1786 * a bit of time to probe the slices and partitions,
1787 * and mess up booting. So avoid if nothing has changed.
1790 if (softc->flags & DA_FLAG_OPEN)
1792 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) == 0)
1795 bzero(&info, sizeof(info));
1796 info.d_type = DTYPE_SCSI;
1797 info.d_serialno = xpt_path_serialno(periph->path);
1800 CAM_SIM_UNLOCK(periph->sim);
1801 info.d_media_blksize = softc->params.secsize;
1802 info.d_media_blocks = softc->params.sectors;
1803 info.d_media_size = 0;
1804 info.d_secpertrack = softc->params.secs_per_track;
1805 info.d_nheads = softc->params.heads;
1806 info.d_ncylinders = softc->params.cylinders;
1807 info.d_secpercyl = softc->params.heads *
1808 softc->params.secs_per_track;
1809 info.d_serialno = xpt_path_serialno(periph->path);
1810 if (info.d_media_blocks != softc->disk.d_info.d_media_blocks) {
1811 kprintf("%s%d: open removable media: "
1812 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)\n",
1813 periph->periph_name, periph->unit_number,
1814 (uintmax_t)(((uintmax_t)dp->secsize *
1815 dp->sectors) / (1024*1024)),
1816 (uintmax_t)dp->sectors, dp->secsize,
1817 dp->heads, dp->secs_per_track, dp->cylinders);
1818 disk_setdiskinfo(&softc->disk, &info);
1820 CAM_SIM_LOCK(periph->sim);
1822 kprintf("%s%d: open removable media: no media present\n",
1823 periph->periph_name, periph->unit_number);
1824 info.d_media_blksize = 512;
1825 disk_setdiskinfo(&softc->disk, &info);
1831 dagetcapacity(struct cam_periph *periph)
1833 struct da_softc *softc;
1835 struct scsi_read_capacity_data *rcap;
1836 struct scsi_read_capacity_data_16 *rcaplong;
1841 softc = (struct da_softc *)periph->softc;
1846 /* Do a read capacity */
1847 rcap = (struct scsi_read_capacity_data *)kmalloc(sizeof(*rcaplong),
1848 M_SCSIDA, M_INTWAIT);
1850 ccb = cam_periph_getccb(periph, /*priority*/1);
1851 scsi_read_capacity(&ccb->csio,
1858 ccb->ccb_h.ccb_bio = NULL;
1860 error = cam_periph_runccb(ccb, daerror,
1861 /*cam_flags*/CAM_RETRY_SELTO,
1862 /*sense_flags*/SF_RETRY_UA,
1863 &softc->device_stats);
1865 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1866 cam_release_devq(ccb->ccb_h.path,
1870 /*getcount_only*/0);
1873 block_len = scsi_4btoul(rcap->length);
1874 maxsector = scsi_4btoul(rcap->addr);
1876 if (maxsector != 0xffffffff)
1881 rcaplong = (struct scsi_read_capacity_data_16 *)rcap;
1883 scsi_read_capacity_16(&ccb->csio,
1886 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1891 /*sense_len*/ SSD_FULL_SIZE,
1893 ccb->ccb_h.ccb_bio = NULL;
1895 error = cam_periph_runccb(ccb, daerror,
1896 /*cam_flags*/CAM_RETRY_SELTO,
1897 /*sense_flags*/SF_RETRY_UA,
1898 &softc->device_stats);
1900 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1901 cam_release_devq(ccb->ccb_h.path,
1905 /*getcount_only*/0);
1908 block_len = scsi_4btoul(rcaplong->length);
1909 maxsector = scsi_8btou64(rcaplong->addr);
1915 dasetgeom(periph, block_len, maxsector);
1917 xpt_release_ccb(ccb);
1919 kfree(rcap, M_SCSIDA);
1925 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector)
1927 struct ccb_calc_geometry ccg;
1928 struct da_softc *softc;
1929 struct disk_params *dp;
1931 softc = (struct da_softc *)periph->softc;
1933 dp = &softc->params;
1934 dp->secsize = block_len;
1935 dp->sectors = maxsector + 1;
1937 * Have the controller provide us with a geometry
1938 * for this disk. The only time the geometry
1939 * matters is when we boot and the controller
1940 * is the only one knowledgeable enough to come
1941 * up with something that will make this a bootable
1944 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
1945 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
1946 ccg.block_size = dp->secsize;
1947 ccg.volume_size = dp->sectors;
1949 ccg.secs_per_track = 0;
1951 xpt_action((union ccb*)&ccg);
1952 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1954 * We don't know what went wrong here- but just pick
1955 * a geometry so we don't have nasty things like divide
1959 dp->secs_per_track = 255;
1960 dp->cylinders = dp->sectors / (255 * 255);
1961 if (dp->cylinders == 0) {
1965 dp->heads = ccg.heads;
1966 dp->secs_per_track = ccg.secs_per_track;
1967 dp->cylinders = ccg.cylinders;
1972 dasendorderedtag(void *arg)
1974 struct da_softc *softc = arg;
1976 if (da_send_ordered) {
1977 if ((softc->ordered_tag_count == 0)
1978 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
1979 softc->flags |= DA_FLAG_NEED_OTAG;
1981 if (softc->outstanding_cmds > 0)
1982 softc->flags &= ~DA_FLAG_WENT_IDLE;
1984 softc->ordered_tag_count = 0;
1986 /* Queue us up again */
1987 callout_reset(&softc->sendordered_c,
1988 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
1989 dasendorderedtag, softc);
1993 * Step through all DA peripheral drivers, and if the device is still open,
1994 * sync the disk cache to physical media.
1997 dashutdown(void * arg, int howto)
1999 struct cam_periph *periph;
2000 struct da_softc *softc;
2002 TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
2005 cam_periph_lock(periph);
2006 softc = (struct da_softc *)periph->softc;
2009 * We only sync the cache if the drive is still open, and
2010 * if the drive is capable of it..
2012 if (((softc->flags & DA_FLAG_OPEN) == 0)
2013 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
2014 cam_periph_unlock(periph);
2018 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
2020 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
2021 scsi_synchronize_cache(&ccb.csio,
2025 /*begin_lba*/0, /* whole disk */
2030 xpt_polled_action(&ccb);
2032 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2033 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2034 CAM_SCSI_STATUS_ERROR)
2035 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
2036 int error_code, sense_key, asc, ascq;
2038 scsi_extract_sense(&ccb.csio.sense_data,
2039 &error_code, &sense_key,
2042 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
2043 scsi_sense_print(&ccb.csio);
2045 xpt_print(periph->path, "Synchronize "
2046 "cache failed, status == 0x%x, scsi status "
2047 "== 0x%x\n", ccb.ccb_h.status,
2048 ccb.csio.scsi_status);
2052 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
2053 cam_release_devq(ccb.ccb_h.path,
2057 /*getcount_only*/0);
2059 cam_periph_unlock(periph);
2063 #else /* !_KERNEL */
2066 * XXX This is only left out of the kernel build to silence warnings. If,
2067 * for some reason this function is used in the kernel, the ifdefs should
2068 * be moved so it is included both in the kernel and userland.
2071 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2072 void (*cbfcnp)(struct cam_periph *, union ccb *),
2073 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2074 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2077 struct scsi_format_unit *scsi_cmd;
2079 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2080 scsi_cmd->opcode = FORMAT_UNIT;
2081 scsi_cmd->byte2 = byte2;
2082 scsi_ulto2b(ileave, scsi_cmd->interleave);
2087 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2096 #endif /* _KERNEL */