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 $
31 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
39 #include <sys/taskqueue.h>
42 #include <sys/devicestat.h>
44 #include <sys/dtype.h>
45 #include <sys/eventhandler.h>
46 #include <sys/malloc.h>
49 #include <sys/ioctl_compat.h>
52 #include <sys/thread2.h>
53 #include <sys/mplock2.h>
66 #include <sys/camlib.h>
68 #include "../cam_ccb.h"
69 #include "../cam_extend.h"
70 #include "../cam_periph.h"
71 #include "../cam_xpt_periph.h"
72 #include "../cam_sim.h"
74 #include "scsi_message.h"
88 DA_FLAG_PACK_INVALID = 0x001,
89 DA_FLAG_NEW_PACK = 0x002,
90 DA_FLAG_PACK_LOCKED = 0x004,
91 DA_FLAG_PACK_REMOVABLE = 0x008,
92 DA_FLAG_TAGGED_QUEUING = 0x010,
93 DA_FLAG_NEED_OTAG = 0x020,
94 DA_FLAG_WENT_IDLE = 0x040,
95 DA_FLAG_RETRY_UA = 0x080,
97 DA_FLAG_SCTX_INIT = 0x200,
98 DA_FLAG_RD_LIMIT = 0x400,
99 DA_FLAG_WR_LIMIT = 0x800,
100 DA_FLAG_CAN_TRIM = 0x1000
105 DA_Q_NO_SYNC_CACHE = 0x01,
106 DA_Q_NO_6_BYTE = 0x02,
107 DA_Q_NO_PREVENT = 0x04
112 DA_CCB_PROBE2 = 0x02,
113 DA_CCB_BUFFER_IO = 0x03,
114 DA_CCB_WAITING = 0x04,
117 DA_CCB_TYPE_MASK = 0x0F,
118 DA_CCB_RETRY_UA = 0x10
121 /* Offsets into our private area for storing information */
122 #define ccb_state ppriv_field0
123 #define ccb_bio ppriv_ptr1
128 u_int8_t secs_per_track;
129 u_int32_t secsize; /* Number of bytes/sector */
130 u_int64_t sectors; /* total number sectors */
133 #define TRIM_MAX_BLOCKS 8
134 #define TRIM_MAX_RANGES TRIM_MAX_BLOCKS * 64
135 struct trim_request {
136 uint8_t data[TRIM_MAX_RANGES * 8];
137 struct bio *bios[TRIM_MAX_RANGES];
141 struct bio_queue_head bio_queue_rd;
142 struct bio_queue_head bio_queue_wr;
143 struct bio_queue_head bio_queue_trim;
144 struct devstat device_stats;
145 SLIST_ENTRY(da_softc) links;
146 LIST_HEAD(, ccb_hdr) pending_ccbs;
150 int minimum_cmd_size;
151 int ordered_tag_count;
152 int outstanding_cmds_rd;
153 int outstanding_cmds_wr;
157 struct disk_params params;
160 struct task sysctl_task;
161 struct sysctl_ctx_list sysctl_ctx;
162 struct sysctl_oid *sysctl_tree;
163 struct callout sendordered_c;
164 struct trim_request trim_req;
167 struct da_quirk_entry {
168 struct scsi_inquiry_pattern inq_pat;
172 static const char quantum[] = "QUANTUM";
173 static const char microp[] = "MICROP";
175 static struct da_quirk_entry da_quirk_table[] =
177 /* SPI, FC devices */
180 * Fujitsu M2513A MO drives.
181 * Tested devices: M2513A2 firmware versions 1200 & 1300.
182 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
183 * Reported by: W.Scholten <whs@xs4all.nl>
185 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
186 /*quirks*/ DA_Q_NO_SYNC_CACHE
190 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
191 /*quirks*/ DA_Q_NO_SYNC_CACHE
195 * This particular Fujitsu drive doesn't like the
196 * synchronize cache command.
197 * Reported by: Tom Jackson <toj@gorilla.net>
199 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
200 /*quirks*/ DA_Q_NO_SYNC_CACHE
204 * This drive doesn't like the synchronize cache command
205 * either. Reported by: Matthew Jacob <mjacob@feral.com>
206 * in NetBSD PR kern/6027, August 24, 1998.
208 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
209 /*quirks*/ DA_Q_NO_SYNC_CACHE
213 * This drive doesn't like the synchronize cache command
214 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
217 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
218 /*quirks*/ DA_Q_NO_SYNC_CACHE
222 * Doesn't like the synchronize cache command.
223 * Reported by: Blaz Zupan <blaz@gold.amis.net>
225 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
226 /*quirks*/ DA_Q_NO_SYNC_CACHE
230 * Doesn't like the synchronize cache command.
231 * Reported by: Blaz Zupan <blaz@gold.amis.net>
233 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
234 /*quirks*/ DA_Q_NO_SYNC_CACHE
238 * Doesn't like the synchronize cache command.
240 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
241 /*quirks*/ DA_Q_NO_SYNC_CACHE
245 * Doesn't like the synchronize cache command.
246 * Reported by: walter@pelissero.de
248 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
249 /*quirks*/ DA_Q_NO_SYNC_CACHE
253 * Doesn't work correctly with 6 byte reads/writes.
254 * Returns illegal request, and points to byte 9 of the
256 * Reported by: Adam McDougall <bsdx@spawnet.com>
258 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
259 /*quirks*/ DA_Q_NO_6_BYTE
263 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
264 /*quirks*/ DA_Q_NO_6_BYTE
268 * Doesn't like the synchronize cache command.
269 * Reported by: walter@pelissero.de
271 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
272 /*quirks*/ DA_Q_NO_SYNC_CACHE
276 * The CISS RAID controllers do not support SYNC_CACHE
278 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
279 /*quirks*/ DA_Q_NO_SYNC_CACHE
283 * The same goes for the mly(4) controllers
285 {T_DIRECT, SIP_MEDIA_FIXED, "MLY*", "*", "MYLX"},
286 /*quirks*/ DA_Q_NO_SYNC_CACHE
289 * USB mass storage devices supported by umass(4)
291 * NOTE: USB attachments automatically set DA_Q_NO_SYNC_CACHE so
292 * it does not have to be specified here.
296 * Creative Nomad MUVO mp3 player (USB)
299 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
300 /*quirks*/ DA_Q_NO_PREVENT
304 * Sigmatel USB Flash MP3 Player
307 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
308 /*quirks*/ DA_Q_NO_PREVENT
312 * SEAGRAND NP-900 MP3 Player
315 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
316 /*quirks*/ DA_Q_NO_PREVENT
320 * Creative MUVO Slim mp3 player (USB)
323 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
324 "*"}, /*quirks*/ DA_Q_NO_PREVENT
328 * Philips USB Key Audio KEY013
331 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
332 /*quirks*/ DA_Q_NO_PREVENT
336 static d_open_t daopen;
337 static d_close_t daclose;
338 static d_strategy_t dastrategy;
339 static d_dump_t dadump;
340 static d_ioctl_t daioctl;
341 static periph_init_t dainit;
342 static void daasync(void *callback_arg, u_int32_t code,
343 struct cam_path *path, void *arg);
344 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
345 static periph_ctor_t daregister;
346 static periph_dtor_t dacleanup;
347 static periph_start_t dastart;
348 static periph_oninv_t daoninvalidate;
349 static void dadone(struct cam_periph *periph,
350 union ccb *done_ccb);
351 static int daerror(union ccb *ccb, u_int32_t cam_flags,
352 u_int32_t sense_flags);
353 static void daprevent(struct cam_periph *periph, int action);
354 static int dagetcapacity(struct cam_periph *periph);
355 static int dacheckmedia(struct cam_periph *periph);
356 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
358 static void daflushbioq(struct bio_queue_head *bioq, int error);
359 static timeout_t dasendorderedtag;
360 static void dashutdown(void *arg, int howto);
362 #ifndef DA_DEFAULT_TIMEOUT
363 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
366 #ifndef DA_DEFAULT_RETRY
367 #define DA_DEFAULT_RETRY 4
370 #ifndef DA_DEFAULT_SEND_ORDERED
371 #define DA_DEFAULT_SEND_ORDERED 1
374 static int da_retry_count = DA_DEFAULT_RETRY;
375 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
376 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
377 static struct callout dasendorderedtag_ch;
379 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
380 "CAM Direct Access Disk driver");
381 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
382 &da_retry_count, 0, "Normal I/O retry count");
383 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
384 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
385 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
386 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
387 SYSCTL_INT(_kern_cam_da, OID_AUTO, da_send_ordered, CTLFLAG_RW,
388 &da_send_ordered, 0, "Send Ordered Tags");
389 TUNABLE_INT("kern.cam.da.da_send_ordered", &da_send_ordered);
392 * DA_ORDEREDTAG_INTERVAL determines how often, relative
393 * to the default timeout, we check to see whether an ordered
394 * tagged transaction is appropriate to prevent simple tag
395 * starvation. Since we'd like to ensure that there is at least
396 * 1/2 of the timeout length left for a starved transaction to
397 * complete after we've sent an ordered tag, we must poll at least
398 * four times in every timeout period. This takes care of the worst
399 * case where a starved transaction starts during an interval that
400 * meets the requirement "don't send an ordered tag" test so it takes
401 * us two intervals to determine that a tag must be sent.
403 #ifndef DA_ORDEREDTAG_INTERVAL
404 #define DA_ORDEREDTAG_INTERVAL 4
407 static struct periph_driver dadriver =
410 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
413 PERIPHDRIVER_DECLARE(da, dadriver);
415 static struct dev_ops da_ops = {
416 { "da", 0, D_DISK | D_MPSAFE },
420 .d_write = physwrite,
421 .d_strategy = dastrategy,
426 static struct extend_array *daperiphs;
428 MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
431 daioctl(struct dev_ioctl_args *ap)
436 struct cam_periph *periph;
439 off_t *del_num = (off_t*)ap->a_data;
443 cdev_t dev = ap->a_head.a_dev;
447 periph = cam_extend_get(daperiphs, unit);
455 bytes_left = del_num[1];
456 bytes_start = del_num[0];
458 /* TRIM occurs on 512-byte sectors. */
459 KKASSERT((bytes_left % 512) == 0);
460 KKASSERT((bytes_start% 512) == 0);
463 /* Break TRIM up into int-sized commands because of b_bcount */
467 * Rather than than squezing out more blocks in b_bcount
468 * and having to break up the TRIM request in da_start(),
469 * we ensure we can always TRIM this many bytes with one
470 * TRIM command (this happens if the device only
471 * supports one TRIM block).
473 * With min TRIM blksize of 1, TRIM command free
474 * 4194240 blks(64*65535): each LBA range can address
475 * 65535 blks and there 64 such ranges in a 512-byte
476 * block. And, 4194240 * 512 = 0x7FFF8000
479 byte_count = MIN(bytes_left,0x7FFF8000);
480 bp = getnewbuf(0,0,0,1);
482 bp->b_cmd = BUF_CMD_FREEBLKS;
483 bp->b_bio1.bio_offset = bytes_start;
484 bp->b_bcount = byte_count;
485 bp->b_bio1.bio_flags |= BIO_SYNC;
486 bp->b_bio1.bio_done = biodone_sync;
488 dev_dstrategy(ap->a_head.a_dev, &bp->b_bio1);
490 if (biowait(&bp->b_bio1, "TRIM")) {
491 kprintf("Error:%d\n", bp->b_error);
492 return(bp->b_error ? bp->b_error : EIO);
495 bytes_left -= byte_count;
496 bytes_start += byte_count;
508 daopen(struct dev_open_args *ap)
510 cdev_t dev = ap->a_head.a_dev;
511 struct cam_periph *periph;
512 struct da_softc *softc;
513 struct disk_info info;
518 periph = cam_extend_get(daperiphs, unit);
519 if (periph == NULL) {
523 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
527 cam_periph_lock(periph);
528 if ((error = cam_periph_hold(periph, PCATCH)) != 0) {
529 cam_periph_unlock(periph);
530 cam_periph_release(periph);
534 unit = periph->unit_number;
535 softc = (struct da_softc *)periph->softc;
537 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
538 ("daopen: dev=%s (unit %d)\n", devtoname(dev),
541 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
542 /* Invalidate our pack information. */
543 disk_invalidate(&softc->disk);
544 softc->flags &= ~DA_FLAG_PACK_INVALID;
547 error = dacheckmedia(periph);
548 softc->flags |= DA_FLAG_OPEN;
551 struct ccb_getdev cgd;
553 /* Build disk information structure */
554 bzero(&info, sizeof(info));
555 info.d_type = DTYPE_SCSI;
558 * Grab the inquiry data to get the vendor and product names.
559 * Put them in the typename and packname for the label.
561 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
562 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
563 xpt_action((union ccb *)&cgd);
566 * Check to see whether or not the blocksize is set yet.
567 * If it isn't, set it and then clear the blocksize
568 * unavailable flag for the device statistics.
570 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0){
571 softc->device_stats.block_size = softc->params.secsize;
572 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
577 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
578 (softc->quirks & DA_Q_NO_PREVENT) == 0)
579 daprevent(periph, PR_PREVENT);
581 softc->flags &= ~DA_FLAG_OPEN;
582 cam_periph_release(periph);
584 cam_periph_unhold(periph, 1);
589 daclose(struct dev_close_args *ap)
591 cdev_t dev = ap->a_head.a_dev;
592 struct cam_periph *periph;
593 struct da_softc *softc;
598 periph = cam_extend_get(daperiphs, unit);
602 cam_periph_lock(periph);
603 if ((error = cam_periph_hold(periph, 0)) != 0) {
604 cam_periph_unlock(periph);
605 cam_periph_release(periph);
609 softc = (struct da_softc *)periph->softc;
611 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
614 ccb = cam_periph_getccb(periph, /*priority*/1);
616 scsi_synchronize_cache(&ccb->csio,
620 /*begin_lba*/0,/* Cover the whole disk */
625 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
626 /*sense_flags*/SF_RETRY_UA,
627 &softc->device_stats);
629 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
630 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
631 CAM_SCSI_STATUS_ERROR) {
633 int sense_key, error_code;
635 scsi_extract_sense(&ccb->csio.sense_data,
639 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
640 scsi_sense_print(&ccb->csio);
642 xpt_print(periph->path, "Synchronize cache "
643 "failed, status == 0x%x, scsi status == "
644 "0x%x\n", ccb->csio.ccb_h.status,
645 ccb->csio.scsi_status);
649 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
650 cam_release_devq(ccb->ccb_h.path,
656 xpt_release_ccb(ccb);
660 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
661 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
662 daprevent(periph, PR_ALLOW);
664 * If we've got removeable media, mark the blocksize as
665 * unavailable, since it could change when new media is
668 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
672 * Don't compound any ref counting software bugs with more.
674 if (softc->flags & DA_FLAG_OPEN) {
675 softc->flags &= ~DA_FLAG_OPEN;
676 cam_periph_release(periph);
678 xpt_print(periph->path,
679 "daclose() called on an already closed device!\n");
681 cam_periph_unhold(periph, 1);
686 * Actually translate the requested transfer into one the physical driver
687 * can understand. The transfer is described by a buf and will include
688 * only one physical transfer.
691 dastrategy(struct dev_strategy_args *ap)
693 cdev_t dev = ap->a_head.a_dev;
694 struct bio *bio = ap->a_bio;
695 struct buf *bp = bio->bio_buf;
696 struct cam_periph *periph;
697 struct da_softc *softc;
701 periph = cam_extend_get(daperiphs, unit);
702 if (periph == NULL) {
706 softc = (struct da_softc *)periph->softc;
708 cam_periph_lock(periph);
712 * check it's not too big a transfer for our adapter
714 scsi_minphys(bp, &sd_switch);
718 * Mask interrupts so that the pack cannot be invalidated until
719 * after we are in the queue. Otherwise, we might not properly
720 * clean up one of the buffers.
724 * If the device has been made invalid, error out
726 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
727 cam_periph_unlock(periph);
733 * Place it in the queue of disk activities for this disk
735 if (bp->b_cmd == BUF_CMD_WRITE || bp->b_cmd == BUF_CMD_FLUSH)
736 bioqdisksort(&softc->bio_queue_wr, bio);
737 else if (bp->b_cmd == BUF_CMD_FREEBLKS)
738 bioqdisksort(&softc->bio_queue_trim, bio);
740 bioqdisksort(&softc->bio_queue_rd, bio);
743 * Schedule ourselves for performing the work.
745 xpt_schedule(periph, /* XXX priority */1);
746 cam_periph_unlock(periph);
750 bp->b_flags |= B_ERROR;
753 * Correctly set the buf to indicate a completed xfer
755 bp->b_resid = bp->b_bcount;
761 dadump(struct dev_dump_args *ap)
763 cdev_t dev = ap->a_head.a_dev;
764 struct cam_periph *periph;
765 struct da_softc *softc;
768 struct ccb_scsiio csio;
771 periph = cam_extend_get(daperiphs, unit);
775 softc = (struct da_softc *)periph->softc;
776 cam_periph_lock(periph);
777 secsize = softc->params.secsize; /* XXX: or ap->a_secsize? */
779 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
780 cam_periph_unlock(periph);
785 * because length == 0 means we are supposed to flush cache, we only
786 * try to write something if length > 0.
788 if (ap->a_length > 0) {
789 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
790 csio.ccb_h.flags |= CAM_POLLED;
791 csio.ccb_h.ccb_state = DA_CCB_DUMP;
792 scsi_read_write(&csio,
798 /*minimum_cmd_size*/ softc->minimum_cmd_size,
799 ap->a_offset / secsize,
800 ap->a_length / secsize,
801 /*data_ptr*/(u_int8_t *) ap->a_virtual,
802 /*dxfer_len*/ap->a_length,
803 /*sense_len*/SSD_FULL_SIZE,
804 DA_DEFAULT_TIMEOUT * 1000);
805 xpt_polled_action((union ccb *)&csio);
807 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
808 kprintf("Aborting dump due to I/O error.\n");
809 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
810 CAM_SCSI_STATUS_ERROR)
811 scsi_sense_print(&csio);
813 kprintf("status == 0x%x, scsi status == 0x%x\n",
814 csio.ccb_h.status, csio.scsi_status);
817 cam_periph_unlock(periph);
822 * Sync the disk cache contents to the physical media.
824 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
826 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
827 csio.ccb_h.ccb_state = DA_CCB_DUMP;
828 scsi_synchronize_cache(&csio,
832 /*begin_lba*/0,/* Cover the whole disk */
836 xpt_polled_action((union ccb *)&csio);
838 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
839 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
840 CAM_SCSI_STATUS_ERROR) {
842 int sense_key, error_code;
844 scsi_extract_sense(&csio.sense_data,
848 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
849 scsi_sense_print(&csio);
851 xpt_print(periph->path, "Synchronize cache "
852 "failed, status == 0x%x, scsi status == "
853 "0x%x\n", csio.ccb_h.status,
858 cam_periph_unlock(periph);
868 * Create our extend array for storing the devices we attach to.
870 daperiphs = cam_extend_new();
871 if (daperiphs == NULL) {
872 kprintf("da: Failed to alloc extend array!\n");
876 callout_init(&dasendorderedtag_ch);
879 * Install a global async callback. This callback will
880 * receive async callbacks like "new device found".
882 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
884 if (status != CAM_REQ_CMP) {
885 kprintf("da: Failed to attach master async callback "
886 "due to status 0x%x!\n", status);
887 } else if (da_send_ordered) {
889 /* Register our shutdown event handler */
890 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
891 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
892 kprintf("dainit: shutdown event registration failed!\n");
897 daoninvalidate(struct cam_periph *periph)
899 struct da_softc *softc;
901 softc = (struct da_softc *)periph->softc;
904 * De-register any async callbacks.
906 xpt_register_async(0, daasync, periph, periph->path);
908 softc->flags |= DA_FLAG_PACK_INVALID;
911 * Return all queued I/O with ENXIO.
912 * XXX Handle any transactions queued to the card
913 * with XPT_ABORT_CCB.
915 daflushbioq(&softc->bio_queue_trim, ENXIO);
916 daflushbioq(&softc->bio_queue_wr, ENXIO);
917 daflushbioq(&softc->bio_queue_rd, ENXIO);
918 xpt_print(periph->path, "lost device\n");
922 daflushbioq(struct bio_queue_head *bioq, int error)
927 while ((q_bio = bioq_first(bioq)) != NULL){
928 bioq_remove(bioq, q_bio);
929 q_bp = q_bio->bio_buf;
930 q_bp->b_resid = q_bp->b_bcount;
931 q_bp->b_error = error;
932 q_bp->b_flags |= B_ERROR;
938 dacleanup(struct cam_periph *periph)
940 struct da_softc *softc;
942 softc = (struct da_softc *)periph->softc;
944 devstat_remove_entry(&softc->device_stats);
945 cam_extend_release(daperiphs, periph->unit_number);
946 xpt_print(periph->path, "removing device entry\n");
948 * If we can't free the sysctl tree, oh well...
950 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
951 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
952 xpt_print(periph->path, "can't remove sysctl context\n");
954 periph->softc = NULL;
955 if (softc->disk.d_rawdev) {
956 cam_periph_unlock(periph);
957 disk_destroy(&softc->disk);
958 cam_periph_lock(periph);
961 callout_stop(&softc->sendordered_c);
962 kfree(softc, M_DEVBUF);
966 daasync(void *callback_arg, u_int32_t code,
967 struct cam_path *path, void *arg)
969 struct cam_periph *periph;
971 periph = (struct cam_periph *)callback_arg;
974 case AC_FOUND_DEVICE:
976 struct ccb_getdev *cgd;
979 cgd = (struct ccb_getdev *)arg;
983 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
984 && SID_TYPE(&cgd->inq_data) != T_RBC
985 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
989 * Don't complain if a valid peripheral is already attached.
991 periph = cam_periph_find(cgd->ccb_h.path, "da");
992 if (periph && (periph->flags & CAM_PERIPH_INVALID) == 0)
996 * Allocate a peripheral instance for
997 * this device and start the probe
1000 status = cam_periph_alloc(daregister, daoninvalidate,
1002 "da", CAM_PERIPH_BIO,
1003 cgd->ccb_h.path, daasync,
1004 AC_FOUND_DEVICE, cgd);
1006 if (status != CAM_REQ_CMP && status != CAM_REQ_INPROG) {
1007 kprintf("daasync: Unable to attach to new device "
1008 "due to status 0x%x\n", status);
1015 struct da_softc *softc;
1016 struct ccb_hdr *ccbh;
1018 softc = (struct da_softc *)periph->softc;
1020 * Don't fail on the expected unit attention
1023 softc->flags |= DA_FLAG_RETRY_UA;
1024 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1025 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1029 cam_periph_async(periph, code, path, arg);
1035 dasysctlinit(void *context, int pending)
1037 struct cam_periph *periph;
1038 struct da_softc *softc;
1039 char tmpstr[80], tmpstr2[80];
1042 periph = (struct cam_periph *)context;
1043 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1048 softc = (struct da_softc *)periph->softc;
1049 ksnprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1050 ksnprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1052 sysctl_ctx_init(&softc->sysctl_ctx);
1053 softc->flags |= DA_FLAG_SCTX_INIT;
1054 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1055 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1056 CTLFLAG_RD, 0, tmpstr);
1057 if (softc->sysctl_tree == NULL) {
1058 kprintf("dasysctlinit: unable to allocate sysctl tree\n");
1059 cam_periph_release(periph);
1065 * Now register the sysctl handler, so the user can the value on
1068 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1069 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1070 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1071 "Minimum CDB size");
1073 /* Only create the option if the device supports TRIM */
1074 if (softc->disk.d_info.d_trimflag) {
1075 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1076 SYSCTL_CHILDREN(softc->sysctl_tree),
1080 &softc->trim_enabled,
1082 "Enable TRIM for this device (SSD))");
1085 cam_periph_release(periph);
1090 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1094 value = *(int *)arg1;
1096 error = sysctl_handle_int(oidp, &value, 0, req);
1099 || (req->newptr == NULL))
1103 * Acceptable values here are 6, 10 or 12, or 16.
1107 else if ((value > 6)
1110 else if ((value > 10)
1113 else if (value > 12)
1116 *(int *)arg1 = value;
1122 daregister(struct cam_periph *periph, void *arg)
1124 struct da_softc *softc;
1125 struct ccb_pathinq cpi;
1126 struct ccb_getdev *cgd;
1130 cgd = (struct ccb_getdev *)arg;
1131 if (periph == NULL) {
1132 kprintf("daregister: periph was NULL!!\n");
1133 return(CAM_REQ_CMP_ERR);
1137 kprintf("daregister: no getdev CCB, can't register device\n");
1138 return(CAM_REQ_CMP_ERR);
1141 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
1142 LIST_INIT(&softc->pending_ccbs);
1143 softc->state = DA_STATE_PROBE;
1144 bioq_init(&softc->bio_queue_trim);
1145 bioq_init(&softc->bio_queue_rd);
1146 bioq_init(&softc->bio_queue_wr);
1147 if (SID_IS_REMOVABLE(&cgd->inq_data))
1148 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1149 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1150 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1152 /* Used to get TRIM status from AHCI driver */
1153 if (cgd->inq_data.vendor_specific1[0] == 1) {
1155 * max number of lba ranges an SSD can handle in a single
1156 * TRIM command. vendor_specific1[1] is the num of 512-byte
1157 * blocks the SSD reports that can be passed in a TRIM cmd.
1159 softc->trim_max_ranges =
1160 min(cgd->inq_data.vendor_specific1[1] * 64, TRIM_MAX_RANGES);
1163 periph->softc = softc;
1165 cam_extend_set(daperiphs, periph->unit_number, periph);
1168 * See if this device has any quirks.
1170 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1171 (caddr_t)da_quirk_table,
1172 NELEM(da_quirk_table),
1173 sizeof(*da_quirk_table), scsi_inquiry_match);
1176 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1178 softc->quirks = DA_Q_NONE;
1181 * Unconditionally disable the synchronize cache command for
1182 * usb attachments. It's just impossible to determine if the
1183 * device supports it or not and if it doesn't the port can
1186 if (strncmp(periph->sim->sim_name, "umass", 4) == 0) {
1187 softc->quirks |= DA_Q_NO_SYNC_CACHE;
1190 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1192 /* Check if the SIM does not want 6 byte commands */
1193 bzero(&cpi, sizeof(cpi));
1194 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
1195 cpi.ccb_h.func_code = XPT_PATH_INQ;
1196 xpt_action((union ccb *)&cpi);
1197 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1198 softc->quirks |= DA_Q_NO_6_BYTE;
1201 * RBC devices don't have to support READ(6), only READ(10).
1203 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1204 softc->minimum_cmd_size = 10;
1206 softc->minimum_cmd_size = 6;
1209 * Load the user's default, if any.
1211 ksnprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1212 periph->unit_number);
1213 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1216 * 6, 10, 12, and 16 are the currently permissible values.
1218 if (softc->minimum_cmd_size < 6)
1219 softc->minimum_cmd_size = 6;
1220 else if ((softc->minimum_cmd_size > 6)
1221 && (softc->minimum_cmd_size <= 10))
1222 softc->minimum_cmd_size = 10;
1223 else if ((softc->minimum_cmd_size > 10)
1224 && (softc->minimum_cmd_size <= 12))
1225 softc->minimum_cmd_size = 12;
1226 else if (softc->minimum_cmd_size > 12)
1227 softc->minimum_cmd_size = 16;
1230 * The DA driver supports a blocksize, but
1231 * we don't know the blocksize until we do
1232 * a read capacity. So, set a flag to
1233 * indicate that the blocksize is
1234 * unavailable right now. We'll clear the
1235 * flag as soon as we've done a read capacity.
1237 devstat_add_entry(&softc->device_stats, "da",
1238 periph->unit_number, 0,
1239 DEVSTAT_BS_UNAVAILABLE,
1240 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
1241 DEVSTAT_PRIORITY_DISK);
1244 * Register this media as a disk
1246 CAM_SIM_UNLOCK(periph->sim);
1247 disk_create(periph->unit_number, &softc->disk, &da_ops);
1248 if (cpi.maxio == 0 || cpi.maxio > MAXPHYS)
1249 softc->disk.d_rawdev->si_iosize_max = MAXPHYS;
1251 softc->disk.d_rawdev->si_iosize_max = cpi.maxio;
1252 CAM_SIM_LOCK(periph->sim);
1255 * Add async callbacks for bus reset and
1256 * bus device reset calls. I don't bother
1257 * checking if this fails as, in most cases,
1258 * the system will function just fine without
1259 * them and the only alternative would be to
1260 * not attach the device on failure.
1262 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
1263 daasync, periph, periph->path);
1266 * Take an exclusive refcount on the periph while dastart is called
1267 * to finish the probe. The reference will be dropped in dadone at
1270 cam_periph_hold(periph, 0);
1271 xpt_schedule(periph, /*priority*/5);
1274 * Schedule a periodic event to occasionally send an
1275 * ordered tag to a device.
1277 callout_init(&softc->sendordered_c);
1278 callout_reset(&softc->sendordered_c,
1279 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
1280 dasendorderedtag, softc);
1284 return(CAM_REQ_CMP);
1288 dastart(struct cam_periph *periph, union ccb *start_ccb)
1290 struct da_softc *softc;
1292 softc = (struct da_softc *)periph->softc;
1294 switch (softc->state) {
1295 case DA_STATE_NORMAL:
1297 /* Pull a buffer from the queue and get going on it */
1306 * See if there is a buf with work for us to do..
1308 bio_rd = bioq_first(&softc->bio_queue_rd);
1309 bio_wr = bioq_first(&softc->bio_queue_wr);
1311 if (periph->immediate_priority <= periph->pinfo.priority) {
1312 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1313 ("queuing for immediate ccb\n"));
1314 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1315 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1317 periph->immediate_priority = CAM_PRIORITY_NONE;
1318 wakeup(&periph->ccb_list);
1319 if (bio_rd || bio_wr) {
1321 * Have more work to do, so ensure we stay
1324 xpt_schedule(periph, /* XXX priority */1);
1329 /* Run the trim command if not already running */
1330 if (!softc->trim_running &&
1331 (bio = bioq_first(&softc->bio_queue_trim)) != NULL) {
1332 struct trim_request *req = &softc->trim_req;
1334 int bps = 0, ranges = 0;
1336 softc->trim_running = 1;
1337 bzero(req, sizeof(*req));
1344 count = bp->b_bcount / softc->params.secsize;
1345 lba = bio1->bio_offset/softc->params.secsize;
1347 kprintf("trim lba:%llu boff:%llu count:%d\n",
1348 (unsigned long long) lba,
1349 (unsigned long long) bio1->bio_offset,
1352 bioq_remove(&softc->bio_queue_trim, bio1);
1354 int c = min(count, 0xffff);
1355 int off = ranges * 8;
1357 req->data[off + 0] = lba & 0xff;
1358 req->data[off + 1] = (lba >> 8) & 0xff;
1359 req->data[off + 2] = (lba >> 16) & 0xff;
1360 req->data[off + 3] = (lba >> 24) & 0xff;
1361 req->data[off + 4] = (lba >> 32) & 0xff;
1362 req->data[off + 5] = (lba >> 40) & 0xff;
1363 req->data[off + 6] = c & 0xff;
1364 req->data[off + 7] = (c >> 8) & 0xff;
1370 /* Try to merge multiple TRIM requests */
1371 req->bios[bps++] = bio1;
1372 bio1 = bioq_first(&softc->bio_queue_trim);
1374 bio1->bio_buf->b_bcount / softc->params.secsize >
1375 (softc->trim_max_ranges - ranges) * 0xffff)
1380 cam_fill_csio(&start_ccb->csio,
1386 ((ranges +63)/64)*512,
1388 sizeof(struct scsi_rw_6),
1389 da_default_timeout*2);
1391 start_ccb->ccb_h.ccb_state = DA_CCB_TRIM;
1392 LIST_INSERT_HEAD(&softc->pending_ccbs,
1393 &start_ccb->ccb_h, periph_links.le);
1394 start_ccb->csio.ccb_h.func_code = XPT_TRIM;
1395 start_ccb->ccb_h.ccb_bio = bio;
1396 devstat_start_transaction(&softc->device_stats);
1397 xpt_action(start_ccb);
1398 xpt_schedule(periph, 1);
1403 * Select a read or write buffer to queue. Limit the number
1404 * of tags dedicated to reading or writing, giving reads
1407 * Writes to modern hard drives go into the HDs cache and
1408 * return completion nearly instantly. That is until the
1409 * cache becomes full. When the HDs cache becomes full
1410 * write commands will begin to stall. If all available
1411 * tags are taken up by writes which saturate the drive
1412 * reads will become tag-starved.
1414 * A similar situation can occur with reads. With many
1415 * parallel readers all tags can be taken up by reads
1416 * and prevent any writes from draining, even if the HD's
1417 * cache is not full.
1419 limit = periph->sim->max_tagged_dev_openings * 2 / 3 + 1;
1423 static long savets2;
1424 if (1 || time_second != savets2 || (ticks != savets && (softc->outstanding_cmds_rd || softc->outstanding_cmds_wr))) {
1425 kprintf("%d %d (%d)\n",
1426 softc->outstanding_cmds_rd,
1427 softc->outstanding_cmds_wr,
1430 savets2 = time_second;
1433 if (bio_rd && softc->outstanding_cmds_rd < limit) {
1435 bioq_remove(&softc->bio_queue_rd, bio);
1436 } else if (bio_wr && softc->outstanding_cmds_wr < limit) {
1438 bioq_remove(&softc->bio_queue_wr, bio);
1441 softc->flags |= DA_FLAG_RD_LIMIT;
1443 softc->flags |= DA_FLAG_WR_LIMIT;
1444 xpt_release_ccb(start_ccb);
1449 * We can queue new work.
1453 devstat_start_transaction(&softc->device_stats);
1455 if ((bp->b_flags & B_ORDERED) != 0 ||
1456 (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1457 softc->flags &= ~DA_FLAG_NEED_OTAG;
1458 softc->ordered_tag_count++;
1459 tag_code = MSG_ORDERED_Q_TAG;
1461 tag_code = MSG_SIMPLE_Q_TAG;
1468 * Block read/write op
1470 KKASSERT(bio->bio_offset % softc->params.secsize == 0);
1474 da_retry_count, /* retries */
1477 (bp->b_cmd == BUF_CMD_READ),
1479 softc->minimum_cmd_size,
1480 bio->bio_offset / softc->params.secsize,
1481 bp->b_bcount / softc->params.secsize,
1484 SSD_FULL_SIZE, /* sense_len */
1485 da_default_timeout * 1000
1490 * Silently complete a flush request if the device
1493 if (softc->quirks & DA_Q_NO_SYNC_CACHE) {
1494 xpt_release_ccb(start_ccb);
1496 devstat_end_transaction_buf(
1497 &softc->device_stats, bp);
1500 scsi_synchronize_cache(
1503 dadone, /* cbfcnp */
1506 0, /* count (whole disk) */
1508 da_default_timeout*1000 /* timeout */
1512 case BUF_CMD_FREEBLKS:
1513 if (softc->disk.d_info.d_trimflag & DA_FLAG_CAN_TRIM){
1514 start_ccb->csio.ccb_h.func_code = XPT_TRIM;
1518 xpt_release_ccb(start_ccb);
1520 panic("dastart: unrecognized bio cmd %d", bp->b_cmd);
1521 break; /* NOT REACHED */
1525 * Block out any asyncronous callbacks
1526 * while we touch the pending ccb list.
1529 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1530 LIST_INSERT_HEAD(&softc->pending_ccbs,
1531 &start_ccb->ccb_h, periph_links.le);
1532 if (bp->b_cmd == BUF_CMD_WRITE ||
1533 bp->b_cmd == BUF_CMD_FLUSH) {
1534 ++softc->outstanding_cmds_wr;
1536 ++softc->outstanding_cmds_rd;
1539 /* We expect a unit attention from this device */
1540 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1541 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1542 softc->flags &= ~DA_FLAG_RETRY_UA;
1545 start_ccb->ccb_h.ccb_bio = bio;
1546 xpt_action(start_ccb);
1550 * Be sure we stay scheduled if we have more work to do.
1552 if (bioq_first(&softc->bio_queue_rd) ||
1553 bioq_first(&softc->bio_queue_wr)) {
1554 xpt_schedule(periph, 1);
1558 case DA_STATE_PROBE:
1560 struct ccb_scsiio *csio;
1561 struct scsi_read_capacity_data *rcap;
1563 rcap = kmalloc(sizeof(*rcap), M_SCSIDA, M_INTWAIT | M_ZERO);
1564 csio = &start_ccb->csio;
1565 scsi_read_capacity(csio,
1572 start_ccb->ccb_h.ccb_bio = NULL;
1573 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1574 xpt_action(start_ccb);
1577 case DA_STATE_PROBE2:
1579 struct ccb_scsiio *csio;
1580 struct scsi_read_capacity_data_16 *rcaplong;
1582 rcaplong = kmalloc(sizeof(*rcaplong), M_SCSIDA,
1583 M_INTWAIT | M_ZERO);
1584 csio = &start_ccb->csio;
1585 scsi_read_capacity_16(csio,
1588 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1593 /*sense_len*/ SSD_FULL_SIZE,
1595 start_ccb->ccb_h.ccb_bio = NULL;
1596 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1597 xpt_action(start_ccb);
1604 cmd6workaround(union ccb *ccb)
1606 struct scsi_rw_6 cmd6;
1607 struct scsi_rw_10 *cmd10;
1608 struct da_softc *softc;
1612 cdb = ccb->csio.cdb_io.cdb_bytes;
1614 /* Translation only possible if CDB is an array and cmd is R/W6 */
1615 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1616 (*cdb != READ_6 && *cdb != WRITE_6))
1619 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
1620 "increasing minimum_cmd_size to 10.\n");
1621 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1622 softc->minimum_cmd_size = 10;
1624 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1625 cmd10 = (struct scsi_rw_10 *)cdb;
1626 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1628 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1629 cmd10->reserved = 0;
1630 scsi_ulto2b(cmd6.length, cmd10->length);
1631 cmd10->control = cmd6.control;
1632 ccb->csio.cdb_len = sizeof(*cmd10);
1634 /* Requeue request, unfreezing queue if necessary */
1635 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1636 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1639 cam_release_devq(ccb->ccb_h.path,
1643 /*getcount_only*/0);
1649 dadone(struct cam_periph *periph, union ccb *done_ccb)
1651 struct da_softc *softc;
1652 struct ccb_scsiio *csio;
1653 struct disk_info info;
1655 softc = (struct da_softc *)periph->softc;
1656 csio = &done_ccb->csio;
1657 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1658 case DA_CCB_BUFFER_IO:
1665 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1667 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1671 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1676 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1677 if (error == ERESTART) {
1679 * A retry was scheuled, so
1685 if (error == ENXIO) {
1687 * Catastrophic error. Mark our pack as
1691 * XXX See if this is really a media
1694 xpt_print(periph->path,
1695 "Invalidating pack\n");
1696 softc->flags |= DA_FLAG_PACK_INVALID;
1700 * Return all queued write I/O's with EIO
1701 * so the client can retry these I/Os in the
1702 * proper order should it attempt to recover.
1704 * Leave read I/O's alone.
1706 daflushbioq(&softc->bio_queue_wr, EIO);
1707 bp->b_error = error;
1708 bp->b_resid = bp->b_bcount;
1709 bp->b_flags |= B_ERROR;
1711 bp->b_resid = csio->resid;
1713 if (bp->b_resid != 0)
1714 bp->b_flags |= B_ERROR;
1716 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1717 cam_release_devq(done_ccb->ccb_h.path,
1721 /*getcount_only*/0);
1723 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1724 panic("REQ_CMP with QFRZN");
1725 bp->b_resid = csio->resid;
1726 if (csio->resid > 0)
1727 bp->b_flags |= B_ERROR;
1731 * Block out any asyncronous callbacks
1732 * while we touch the pending ccb list.
1734 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1735 if (bp->b_cmd == BUF_CMD_WRITE || bp->b_cmd == BUF_CMD_FLUSH) {
1736 --softc->outstanding_cmds_wr;
1737 if (softc->flags & DA_FLAG_WR_LIMIT) {
1738 softc->flags &= ~DA_FLAG_WR_LIMIT;
1742 --softc->outstanding_cmds_rd;
1743 if (softc->flags & DA_FLAG_RD_LIMIT) {
1744 softc->flags &= ~DA_FLAG_RD_LIMIT;
1748 if (softc->outstanding_cmds_rd +
1749 softc->outstanding_cmds_wr == 0) {
1750 softc->flags |= DA_FLAG_WENT_IDLE;
1753 devstat_end_transaction_buf(&softc->device_stats, bp);
1754 if ((csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) ==
1756 struct trim_request *req =
1757 (struct trim_request *) csio->data_ptr;
1760 for (i = 1; i < softc->trim_max_ranges &&
1761 req->bios[i]; i++) {
1762 struct bio *bp1 = req->bios[i];
1764 bp1->bio_buf->b_resid = bp->b_resid;
1765 bp1->bio_buf->b_error = bp->b_error;
1766 if (bp->b_flags & B_ERROR)
1767 bp1->bio_buf->b_flags |= B_ERROR;
1770 softc->trim_running = 0;
1772 xpt_schedule(periph,1);
1778 xpt_schedule(periph, /*priority*/1);
1785 struct scsi_read_capacity_data *rdcap;
1786 struct scsi_read_capacity_data_16 *rcaplong;
1787 char announce_buf[80];
1791 if (softc->state == DA_STATE_PROBE)
1792 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1794 rcaplong = (struct scsi_read_capacity_data_16 *)
1797 bzero(&info, sizeof(info));
1798 info.d_type = DTYPE_SCSI;
1799 info.d_serialno = xpt_path_serialno(periph->path);
1801 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1802 struct disk_params *dp;
1803 uint32_t block_size;
1806 if (softc->state == DA_STATE_PROBE) {
1807 block_size = scsi_4btoul(rdcap->length);
1808 maxsector = scsi_4btoul(rdcap->addr);
1811 * According to SBC-2, if the standard 10
1812 * byte READ CAPACITY command returns 2^32,
1813 * we should issue the 16 byte version of
1814 * the command, since the device in question
1815 * has more sectors than can be represented
1816 * with the short version of the command.
1818 if (maxsector == 0xffffffff) {
1819 softc->state = DA_STATE_PROBE2;
1820 kfree(rdcap, M_SCSIDA);
1821 xpt_release_ccb(done_ccb);
1822 xpt_schedule(periph, /*priority*/5);
1826 block_size = scsi_4btoul(rcaplong->length);
1827 maxsector = scsi_8btou64(rcaplong->addr);
1829 dasetgeom(periph, block_size, maxsector);
1830 dp = &softc->params;
1831 ksnprintf(announce_buf, sizeof(announce_buf),
1832 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1833 (uintmax_t) (((uintmax_t)dp->secsize *
1834 dp->sectors) / (1024*1024)),
1835 (uintmax_t)dp->sectors,
1836 dp->secsize, dp->heads, dp->secs_per_track,
1839 CAM_SIM_UNLOCK(periph->sim);
1840 info.d_media_blksize = softc->params.secsize;
1841 info.d_media_blocks = softc->params.sectors;
1842 info.d_media_size = 0;
1843 info.d_secpertrack = softc->params.secs_per_track;
1844 info.d_nheads = softc->params.heads;
1845 info.d_ncylinders = softc->params.cylinders;
1846 info.d_secpercyl = softc->params.heads *
1847 softc->params.secs_per_track;
1848 info.d_serialno = xpt_path_serialno(periph->path);
1849 disk_setdiskinfo(&softc->disk, &info);
1850 CAM_SIM_LOCK(periph->sim);
1854 announce_buf[0] = '\0';
1857 * Retry any UNIT ATTENTION type errors. They
1858 * are expected at boot.
1860 error = daerror(done_ccb, CAM_RETRY_SELTO,
1861 SF_RETRY_UA|SF_NO_PRINT);
1862 if (error == ERESTART) {
1864 * A retry was scheuled, so
1868 } else if (error != 0) {
1869 struct scsi_sense_data *sense;
1871 int sense_key, error_code;
1874 struct ccb_getdev cgd;
1876 /* Don't wedge this device's queue */
1877 status = done_ccb->ccb_h.status;
1878 if ((status & CAM_DEV_QFRZN) != 0)
1879 cam_release_devq(done_ccb->ccb_h.path,
1883 /*getcount_only*/0);
1886 xpt_setup_ccb(&cgd.ccb_h,
1887 done_ccb->ccb_h.path,
1889 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1890 xpt_action((union ccb *)&cgd);
1892 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1893 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1894 || ((status & CAM_AUTOSNS_VALID) == 0))
1900 sense = &csio->sense_data;
1901 scsi_extract_sense(sense, &error_code,
1906 * Attach to anything that claims to be a
1907 * direct access or optical disk device,
1908 * as long as it doesn't return a "Logical
1909 * unit not supported" (0x25) error.
1911 if ((have_sense) && (asc != 0x25)
1912 && (error_code == SSD_CURRENT_ERROR)) {
1913 const char *sense_key_desc;
1914 const char *asc_desc;
1916 scsi_sense_desc(sense_key, asc, ascq,
1920 ksnprintf(announce_buf,
1921 sizeof(announce_buf),
1922 "Attempt to query device "
1923 "size failed: %s, %s",
1926 info.d_media_blksize = 512;
1927 disk_setdiskinfo(&softc->disk, &info);
1933 xpt_print(periph->path,
1934 "got CAM status %#x\n",
1935 done_ccb->ccb_h.status);
1938 xpt_print(periph->path, "fatal error, "
1939 "failed to attach to device\n");
1942 * Free up resources.
1944 cam_periph_invalidate(periph);
1948 kfree(csio->data_ptr, M_SCSIDA);
1949 if (announce_buf[0] != '\0') {
1950 xpt_announce_periph(periph, announce_buf);
1952 * Create our sysctl variables, now that we know
1953 * we have successfully attached.
1955 taskqueue_enqueue(taskqueue_thread[mycpuid],
1956 &softc->sysctl_task);
1959 if (softc->trim_max_ranges) {
1960 softc->disk.d_info.d_trimflag |= DA_FLAG_CAN_TRIM;
1961 kprintf("%s%d: supports TRIM\n",
1962 periph->periph_name,
1963 periph->unit_number);
1965 softc->state = DA_STATE_NORMAL;
1967 * Since our peripheral may be invalidated by an error
1968 * above or an external event, we must release our CCB
1969 * before releasing the probe lock on the peripheral.
1970 * The peripheral will only go away once the last lock
1971 * is removed, and we need it around for the CCB release
1974 xpt_release_ccb(done_ccb);
1975 cam_periph_unhold(periph, 0);
1978 case DA_CCB_WAITING:
1980 /* Caller will release the CCB */
1981 wakeup(&done_ccb->ccb_h.cbfcnp);
1985 /* No-op. We're polling */
1990 xpt_release_ccb(done_ccb);
1994 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1996 struct da_softc *softc;
1997 struct cam_periph *periph;
2000 periph = xpt_path_periph(ccb->ccb_h.path);
2001 softc = (struct da_softc *)periph->softc;
2004 * Automatically detect devices that do not support
2005 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
2008 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
2009 error = cmd6workaround(ccb);
2010 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
2011 CAM_SCSI_STATUS_ERROR)
2012 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
2013 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
2014 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
2015 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
2016 int sense_key, error_code, asc, ascq;
2018 scsi_extract_sense(&ccb->csio.sense_data,
2019 &error_code, &sense_key, &asc, &ascq);
2020 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
2021 error = cmd6workaround(ccb);
2023 if (error == ERESTART)
2028 * Until we have a better way of doing pack validation,
2029 * don't treat UAs as errors.
2031 sense_flags |= SF_RETRY_UA;
2032 return(cam_periph_error(ccb, cam_flags, sense_flags,
2033 &softc->saved_ccb));
2037 daprevent(struct cam_periph *periph, int action)
2039 struct da_softc *softc;
2043 softc = (struct da_softc *)periph->softc;
2045 if (((action == PR_ALLOW)
2046 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
2047 || ((action == PR_PREVENT)
2048 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
2052 ccb = cam_periph_getccb(periph, /*priority*/1);
2054 scsi_prevent(&ccb->csio,
2062 error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
2063 SF_RETRY_UA, &softc->device_stats);
2066 if (action == PR_ALLOW)
2067 softc->flags &= ~DA_FLAG_PACK_LOCKED;
2069 softc->flags |= DA_FLAG_PACK_LOCKED;
2072 xpt_release_ccb(ccb);
2076 * Check media on open, e.g. card reader devices which had no initial media.
2079 dacheckmedia(struct cam_periph *periph)
2081 struct disk_params *dp;
2082 struct da_softc *softc;
2083 struct disk_info info;
2086 softc = (struct da_softc *)periph->softc;
2087 dp = &softc->params;
2089 error = dagetcapacity(periph);
2092 * Only reprobe on initial open and if the media is removable.
2094 * NOTE: If we setdiskinfo() it will take the device probe
2095 * a bit of time to probe the slices and partitions,
2096 * and mess up booting. So avoid if nothing has changed.
2099 if (softc->flags & DA_FLAG_OPEN)
2101 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) == 0)
2104 bzero(&info, sizeof(info));
2105 info.d_type = DTYPE_SCSI;
2106 info.d_serialno = xpt_path_serialno(periph->path);
2109 CAM_SIM_UNLOCK(periph->sim);
2110 info.d_media_blksize = softc->params.secsize;
2111 info.d_media_blocks = softc->params.sectors;
2112 info.d_media_size = 0;
2113 info.d_secpertrack = softc->params.secs_per_track;
2114 info.d_nheads = softc->params.heads;
2115 info.d_ncylinders = softc->params.cylinders;
2116 info.d_secpercyl = softc->params.heads *
2117 softc->params.secs_per_track;
2118 info.d_serialno = xpt_path_serialno(periph->path);
2119 if (info.d_media_blocks != softc->disk.d_info.d_media_blocks) {
2120 kprintf("%s%d: open removable media: "
2121 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)\n",
2122 periph->periph_name, periph->unit_number,
2123 (uintmax_t)(((uintmax_t)dp->secsize *
2124 dp->sectors) / (1024*1024)),
2125 (uintmax_t)dp->sectors, dp->secsize,
2126 dp->heads, dp->secs_per_track, dp->cylinders);
2127 disk_setdiskinfo(&softc->disk, &info);
2129 CAM_SIM_LOCK(periph->sim);
2131 kprintf("%s%d: open removable media: no media present\n",
2132 periph->periph_name, periph->unit_number);
2133 info.d_media_blksize = 512;
2134 disk_setdiskinfo(&softc->disk, &info);
2140 dagetcapacity(struct cam_periph *periph)
2142 struct da_softc *softc;
2144 struct scsi_read_capacity_data *rcap;
2145 struct scsi_read_capacity_data_16 *rcaplong;
2150 softc = (struct da_softc *)periph->softc;
2155 /* Do a read capacity */
2156 rcap = (struct scsi_read_capacity_data *)kmalloc(sizeof(*rcaplong),
2157 M_SCSIDA, M_INTWAIT);
2159 ccb = cam_periph_getccb(periph, /*priority*/1);
2160 scsi_read_capacity(&ccb->csio,
2167 ccb->ccb_h.ccb_bio = NULL;
2169 error = cam_periph_runccb(ccb, daerror,
2170 /*cam_flags*/CAM_RETRY_SELTO,
2171 /*sense_flags*/SF_RETRY_UA,
2172 &softc->device_stats);
2174 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2175 cam_release_devq(ccb->ccb_h.path,
2179 /*getcount_only*/0);
2182 block_len = scsi_4btoul(rcap->length);
2183 maxsector = scsi_4btoul(rcap->addr);
2185 if (maxsector != 0xffffffff)
2190 rcaplong = (struct scsi_read_capacity_data_16 *)rcap;
2192 scsi_read_capacity_16(&ccb->csio,
2195 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2200 /*sense_len*/ SSD_FULL_SIZE,
2202 ccb->ccb_h.ccb_bio = NULL;
2204 error = cam_periph_runccb(ccb, daerror,
2205 /*cam_flags*/CAM_RETRY_SELTO,
2206 /*sense_flags*/SF_RETRY_UA,
2207 &softc->device_stats);
2209 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2210 cam_release_devq(ccb->ccb_h.path,
2214 /*getcount_only*/0);
2217 block_len = scsi_4btoul(rcaplong->length);
2218 maxsector = scsi_8btou64(rcaplong->addr);
2224 dasetgeom(periph, block_len, maxsector);
2226 xpt_release_ccb(ccb);
2228 kfree(rcap, M_SCSIDA);
2234 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector)
2236 struct ccb_calc_geometry ccg;
2237 struct da_softc *softc;
2238 struct disk_params *dp;
2240 softc = (struct da_softc *)periph->softc;
2242 dp = &softc->params;
2243 dp->secsize = block_len;
2244 dp->sectors = maxsector + 1;
2246 * Have the controller provide us with a geometry
2247 * for this disk. The only time the geometry
2248 * matters is when we boot and the controller
2249 * is the only one knowledgeable enough to come
2250 * up with something that will make this a bootable
2253 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
2254 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
2255 ccg.block_size = dp->secsize;
2256 ccg.volume_size = dp->sectors;
2258 ccg.secs_per_track = 0;
2260 xpt_action((union ccb*)&ccg);
2261 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2263 * We don't know what went wrong here- but just pick
2264 * a geometry so we don't have nasty things like divide
2268 dp->secs_per_track = 255;
2269 dp->cylinders = dp->sectors / (255 * 255);
2270 if (dp->cylinders == 0) {
2274 dp->heads = ccg.heads;
2275 dp->secs_per_track = ccg.secs_per_track;
2276 dp->cylinders = ccg.cylinders;
2281 dasendorderedtag(void *arg)
2283 struct da_softc *softc = arg;
2285 if (da_send_ordered) {
2286 if ((softc->ordered_tag_count == 0)
2287 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
2288 softc->flags |= DA_FLAG_NEED_OTAG;
2290 if (softc->outstanding_cmds_rd || softc->outstanding_cmds_wr)
2291 softc->flags &= ~DA_FLAG_WENT_IDLE;
2293 softc->ordered_tag_count = 0;
2295 /* Queue us up again */
2296 callout_reset(&softc->sendordered_c,
2297 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
2298 dasendorderedtag, softc);
2302 * Step through all DA peripheral drivers, and if the device is still open,
2303 * sync the disk cache to physical media.
2306 dashutdown(void * arg, int howto)
2308 struct cam_periph *periph;
2309 struct da_softc *softc;
2311 TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
2314 cam_periph_lock(periph);
2315 softc = (struct da_softc *)periph->softc;
2318 * We only sync the cache if the drive is still open, and
2319 * if the drive is capable of it..
2321 if (((softc->flags & DA_FLAG_OPEN) == 0)
2322 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
2323 cam_periph_unlock(periph);
2327 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
2329 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
2330 scsi_synchronize_cache(&ccb.csio,
2334 /*begin_lba*/0, /* whole disk */
2339 xpt_polled_action(&ccb);
2341 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2342 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2343 CAM_SCSI_STATUS_ERROR)
2344 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
2345 int error_code, sense_key, asc, ascq;
2347 scsi_extract_sense(&ccb.csio.sense_data,
2348 &error_code, &sense_key,
2351 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
2352 scsi_sense_print(&ccb.csio);
2354 xpt_print(periph->path, "Synchronize "
2355 "cache failed, status == 0x%x, scsi status "
2356 "== 0x%x\n", ccb.ccb_h.status,
2357 ccb.csio.scsi_status);
2361 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
2362 cam_release_devq(ccb.ccb_h.path,
2366 /*getcount_only*/0);
2368 cam_periph_unlock(periph);
2372 #else /* !_KERNEL */
2375 * XXX This is only left out of the kernel build to silence warnings. If,
2376 * for some reason this function is used in the kernel, the ifdefs should
2377 * be moved so it is included both in the kernel and userland.
2380 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2381 void (*cbfcnp)(struct cam_periph *, union ccb *),
2382 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2383 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2386 struct scsi_format_unit *scsi_cmd;
2388 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2389 scsi_cmd->opcode = FORMAT_UNIT;
2390 scsi_cmd->byte2 = byte2;
2391 scsi_ulto2b(ileave, scsi_cmd->interleave);
2396 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2405 #endif /* _KERNEL */