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>
32 #include <sys/bootmaj.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/sysctl.h>
40 #include <sys/taskqueue.h>
43 #include <sys/devicestat.h>
45 #include <sys/dtype.h>
46 #include <sys/eventhandler.h>
47 #include <sys/malloc.h>
50 #include <sys/ioctl_compat.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,
99 DA_FLAG_RD_LIMIT = 0x400,
100 DA_FLAG_WR_LIMIT = 0x800,
101 DA_FLAG_CAN_TRIM = 0x1000
106 DA_Q_NO_SYNC_CACHE = 0x01,
107 DA_Q_NO_6_BYTE = 0x02,
108 DA_Q_NO_PREVENT = 0x04
113 DA_CCB_PROBE2 = 0x02,
114 DA_CCB_BUFFER_IO = 0x03,
115 DA_CCB_WAITING = 0x04,
118 DA_CCB_TYPE_MASK = 0x0F,
119 DA_CCB_RETRY_UA = 0x10
122 /* Offsets into our private area for storing information */
123 #define ccb_state ppriv_field0
124 #define ccb_bio ppriv_ptr1
129 u_int8_t secs_per_track;
130 u_int32_t secsize; /* Number of bytes/sector */
131 u_int64_t sectors; /* total number sectors */
134 #define TRIM_MAX_BLOCKS 8
135 #define TRIM_MAX_RANGES TRIM_MAX_BLOCKS * 64
136 struct trim_request {
137 uint8_t data[TRIM_MAX_RANGES * 8];
138 struct bio *bios[TRIM_MAX_RANGES];
142 struct bio_queue_head bio_queue_rd;
143 struct bio_queue_head bio_queue_wr;
144 struct bio_queue_head bio_queue_trim;
145 struct devstat device_stats;
146 SLIST_ENTRY(da_softc) links;
147 LIST_HEAD(, ccb_hdr) pending_ccbs;
151 int minimum_cmd_size;
152 int ordered_tag_count;
153 int outstanding_cmds_rd;
154 int outstanding_cmds_wr;
158 struct disk_params params;
161 struct task sysctl_task;
162 struct sysctl_ctx_list sysctl_ctx;
163 struct sysctl_oid *sysctl_tree;
164 struct callout sendordered_c;
165 struct trim_request trim_req;
168 struct da_quirk_entry {
169 struct scsi_inquiry_pattern inq_pat;
173 static const char quantum[] = "QUANTUM";
174 static const char microp[] = "MICROP";
176 static struct da_quirk_entry da_quirk_table[] =
178 /* SPI, FC devices */
181 * Fujitsu M2513A MO drives.
182 * Tested devices: M2513A2 firmware versions 1200 & 1300.
183 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
184 * Reported by: W.Scholten <whs@xs4all.nl>
186 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
187 /*quirks*/ DA_Q_NO_SYNC_CACHE
191 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
192 /*quirks*/ DA_Q_NO_SYNC_CACHE
196 * This particular Fujitsu drive doesn't like the
197 * synchronize cache command.
198 * Reported by: Tom Jackson <toj@gorilla.net>
200 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
201 /*quirks*/ DA_Q_NO_SYNC_CACHE
205 * This drive doesn't like the synchronize cache command
206 * either. Reported by: Matthew Jacob <mjacob@feral.com>
207 * in NetBSD PR kern/6027, August 24, 1998.
209 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
210 /*quirks*/ DA_Q_NO_SYNC_CACHE
214 * This drive doesn't like the synchronize cache command
215 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
218 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
219 /*quirks*/ DA_Q_NO_SYNC_CACHE
223 * Doesn't like the synchronize cache command.
224 * Reported by: Blaz Zupan <blaz@gold.amis.net>
226 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
227 /*quirks*/ DA_Q_NO_SYNC_CACHE
231 * Doesn't like the synchronize cache command.
232 * Reported by: Blaz Zupan <blaz@gold.amis.net>
234 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
235 /*quirks*/ DA_Q_NO_SYNC_CACHE
239 * Doesn't like the synchronize cache command.
241 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
242 /*quirks*/ DA_Q_NO_SYNC_CACHE
246 * Doesn't like the synchronize cache command.
247 * Reported by: walter@pelissero.de
249 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
250 /*quirks*/ DA_Q_NO_SYNC_CACHE
254 * Doesn't work correctly with 6 byte reads/writes.
255 * Returns illegal request, and points to byte 9 of the
257 * Reported by: Adam McDougall <bsdx@spawnet.com>
259 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
260 /*quirks*/ DA_Q_NO_6_BYTE
264 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
265 /*quirks*/ DA_Q_NO_6_BYTE
269 * Doesn't like the synchronize cache command.
270 * Reported by: walter@pelissero.de
272 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
273 /*quirks*/ DA_Q_NO_SYNC_CACHE
277 * The CISS RAID controllers do not support SYNC_CACHE
279 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
280 /*quirks*/ DA_Q_NO_SYNC_CACHE
284 * The same goes for the mly(4) controllers
286 {T_DIRECT, SIP_MEDIA_FIXED, "MLY*", "*", "MYLX"},
287 /*quirks*/ DA_Q_NO_SYNC_CACHE
290 * USB mass storage devices supported by umass(4)
292 * NOTE: USB attachments automatically set DA_Q_NO_SYNC_CACHE so
293 * it does not have to be specified here.
297 * Creative Nomad MUVO mp3 player (USB)
300 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
301 /*quirks*/ DA_Q_NO_PREVENT
305 * Sigmatel USB Flash MP3 Player
308 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
309 /*quirks*/ DA_Q_NO_PREVENT
313 * SEAGRAND NP-900 MP3 Player
316 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
317 /*quirks*/ DA_Q_NO_PREVENT
321 * Creative MUVO Slim mp3 player (USB)
324 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
325 "*"}, /*quirks*/ DA_Q_NO_PREVENT
329 * Philips USB Key Audio KEY013
332 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
333 /*quirks*/ DA_Q_NO_PREVENT
337 static d_open_t daopen;
338 static d_close_t daclose;
339 static d_strategy_t dastrategy;
340 static d_dump_t dadump;
341 static d_ioctl_t daioctl;
342 static periph_init_t dainit;
343 static void daasync(void *callback_arg, u_int32_t code,
344 struct cam_path *path, void *arg);
345 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
346 static periph_ctor_t daregister;
347 static periph_dtor_t dacleanup;
348 static periph_start_t dastart;
349 static periph_oninv_t daoninvalidate;
350 static void dadone(struct cam_periph *periph,
351 union ccb *done_ccb);
352 static int daerror(union ccb *ccb, u_int32_t cam_flags,
353 u_int32_t sense_flags);
354 static void daprevent(struct cam_periph *periph, int action);
355 static int dagetcapacity(struct cam_periph *periph);
356 static int dacheckmedia(struct cam_periph *periph);
357 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
359 static void daflushbioq(struct bio_queue_head *bioq, int error);
360 static timeout_t dasendorderedtag;
361 static void dashutdown(void *arg, int howto);
363 #ifndef DA_DEFAULT_TIMEOUT
364 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
367 #ifndef DA_DEFAULT_RETRY
368 #define DA_DEFAULT_RETRY 4
371 #ifndef DA_DEFAULT_SEND_ORDERED
372 #define DA_DEFAULT_SEND_ORDERED 1
375 static int da_retry_count = DA_DEFAULT_RETRY;
376 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
377 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
378 static struct callout dasendorderedtag_ch;
380 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
381 "CAM Direct Access Disk driver");
382 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
383 &da_retry_count, 0, "Normal I/O retry count");
384 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
385 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
386 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
387 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
388 SYSCTL_INT(_kern_cam_da, OID_AUTO, da_send_ordered, CTLFLAG_RW,
389 &da_send_ordered, 0, "Send Ordered Tags");
390 TUNABLE_INT("kern.cam.da.da_send_ordered", &da_send_ordered);
393 * DA_ORDEREDTAG_INTERVAL determines how often, relative
394 * to the default timeout, we check to see whether an ordered
395 * tagged transaction is appropriate to prevent simple tag
396 * starvation. Since we'd like to ensure that there is at least
397 * 1/2 of the timeout length left for a starved transaction to
398 * complete after we've sent an ordered tag, we must poll at least
399 * four times in every timeout period. This takes care of the worst
400 * case where a starved transaction starts during an interval that
401 * meets the requirement "don't send an ordered tag" test so it takes
402 * us two intervals to determine that a tag must be sent.
404 #ifndef DA_ORDEREDTAG_INTERVAL
405 #define DA_ORDEREDTAG_INTERVAL 4
408 static struct periph_driver dadriver =
411 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
414 PERIPHDRIVER_DECLARE(da, dadriver);
416 static struct dev_ops da_ops = {
417 { "da", DA_CDEV_MAJOR, D_DISK | D_MPSAFE },
421 .d_write = physwrite,
422 .d_strategy = dastrategy,
427 static struct extend_array *daperiphs;
429 MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
432 daioctl(struct dev_ioctl_args *ap)
437 struct cam_periph *periph;
439 struct da_softc * softc;
441 off_t *del_num = (off_t*)ap->a_data;
445 cdev_t dev = ap->a_head.a_dev;
449 periph = cam_extend_get(daperiphs, unit);
452 softc = (struct da_softc *)periph->softc;
458 bytes_left = del_num[1];
459 bytes_start = del_num[0];
461 /* TRIM occurs on 512-byte sectors. */
462 KKASSERT((bytes_left % 512) == 0);
463 KKASSERT((bytes_start% 512) == 0);
466 /* Break TRIM up into int-sized commands because of b_bcount */
470 * Rather than than squezing out more blocks in b_bcount
471 * and having to break up the TRIM request in da_start(),
472 * we ensure we can always TRIM this many bytes with one
473 * TRIM command (this happens if the device only
474 * supports one TRIM block).
476 * With min TRIM blksize of 1, TRIM command free
477 * 4194240 blks(64*65535): each LBA range can address
478 * 65535 blks and there 64 such ranges in a 512-byte
479 * block. And, 4194240 * 512 = 0x7FFF8000
482 byte_count = MIN(bytes_left,0x7FFF8000);
483 bp = getnewbuf(0,0,0,1);
485 bp->b_cmd = BUF_CMD_FREEBLKS;
486 bp->b_bio1.bio_offset = bytes_start;
487 bp->b_bcount = byte_count;
488 bp->b_bio1.bio_flags |= BIO_SYNC;
489 bp->b_bio1.bio_done = biodone_sync;
491 dev_dstrategy(ap->a_head.a_dev, &bp->b_bio1);
493 if (biowait(&bp->b_bio1, "TRIM")) {
494 kprintf("Error:%d\n", bp->b_error);
495 return(bp->b_error ? bp->b_error : EIO);
498 bytes_left -= byte_count;
499 bytes_start += byte_count;
511 daopen(struct dev_open_args *ap)
513 cdev_t dev = ap->a_head.a_dev;
514 struct cam_periph *periph;
515 struct da_softc *softc;
516 struct disk_info info;
521 periph = cam_extend_get(daperiphs, unit);
522 if (periph == NULL) {
526 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
530 cam_periph_lock(periph);
531 if ((error = cam_periph_hold(periph, PCATCH)) != 0) {
532 cam_periph_unlock(periph);
533 cam_periph_release(periph);
537 unit = periph->unit_number;
538 softc = (struct da_softc *)periph->softc;
540 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
541 ("daopen: dev=%s (unit %d)\n", devtoname(dev),
544 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
545 /* Invalidate our pack information. */
546 disk_invalidate(&softc->disk);
547 softc->flags &= ~DA_FLAG_PACK_INVALID;
550 error = dacheckmedia(periph);
551 softc->flags |= DA_FLAG_OPEN;
554 struct ccb_getdev cgd;
556 /* Build disk information structure */
557 bzero(&info, sizeof(info));
558 info.d_type = DTYPE_SCSI;
561 * Grab the inquiry data to get the vendor and product names.
562 * Put them in the typename and packname for the label.
564 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
565 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
566 xpt_action((union ccb *)&cgd);
569 * Check to see whether or not the blocksize is set yet.
570 * If it isn't, set it and then clear the blocksize
571 * unavailable flag for the device statistics.
573 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0){
574 softc->device_stats.block_size = softc->params.secsize;
575 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
580 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
581 (softc->quirks & DA_Q_NO_PREVENT) == 0)
582 daprevent(periph, PR_PREVENT);
584 softc->flags &= ~DA_FLAG_OPEN;
585 cam_periph_release(periph);
587 cam_periph_unhold(periph, 1);
592 daclose(struct dev_close_args *ap)
594 cdev_t dev = ap->a_head.a_dev;
595 struct cam_periph *periph;
596 struct da_softc *softc;
601 periph = cam_extend_get(daperiphs, unit);
605 cam_periph_lock(periph);
606 if ((error = cam_periph_hold(periph, 0)) != 0) {
607 cam_periph_unlock(periph);
608 cam_periph_release(periph);
612 softc = (struct da_softc *)periph->softc;
614 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
617 ccb = cam_periph_getccb(periph, /*priority*/1);
619 scsi_synchronize_cache(&ccb->csio,
623 /*begin_lba*/0,/* Cover the whole disk */
628 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
629 /*sense_flags*/SF_RETRY_UA,
630 &softc->device_stats);
632 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
633 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
634 CAM_SCSI_STATUS_ERROR) {
636 int sense_key, error_code;
638 scsi_extract_sense(&ccb->csio.sense_data,
642 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
643 scsi_sense_print(&ccb->csio);
645 xpt_print(periph->path, "Synchronize cache "
646 "failed, status == 0x%x, scsi status == "
647 "0x%x\n", ccb->csio.ccb_h.status,
648 ccb->csio.scsi_status);
652 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
653 cam_release_devq(ccb->ccb_h.path,
659 xpt_release_ccb(ccb);
663 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
664 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
665 daprevent(periph, PR_ALLOW);
667 * If we've got removeable media, mark the blocksize as
668 * unavailable, since it could change when new media is
671 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
675 * Don't compound any ref counting software bugs with more.
677 if (softc->flags & DA_FLAG_OPEN) {
678 softc->flags &= ~DA_FLAG_OPEN;
679 cam_periph_release(periph);
681 xpt_print(periph->path,
682 "daclose() called on an already closed device!\n");
684 cam_periph_unhold(periph, 1);
689 * Actually translate the requested transfer into one the physical driver
690 * can understand. The transfer is described by a buf and will include
691 * only one physical transfer.
694 dastrategy(struct dev_strategy_args *ap)
696 cdev_t dev = ap->a_head.a_dev;
697 struct bio *bio = ap->a_bio;
698 struct buf *bp = bio->bio_buf;
699 struct cam_periph *periph;
700 struct da_softc *softc;
706 periph = cam_extend_get(daperiphs, unit);
707 if (periph == NULL) {
711 softc = (struct da_softc *)periph->softc;
713 cam_periph_lock(periph);
717 * check it's not too big a transfer for our adapter
719 scsi_minphys(bp, &sd_switch);
723 * Mask interrupts so that the pack cannot be invalidated until
724 * after we are in the queue. Otherwise, we might not properly
725 * clean up one of the buffers.
729 * If the device has been made invalid, error out
731 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
732 cam_periph_unlock(periph);
738 * Place it in the queue of disk activities for this disk
740 if (bp->b_cmd == BUF_CMD_WRITE || bp->b_cmd == BUF_CMD_FLUSH)
741 bioqdisksort(&softc->bio_queue_wr, bio);
742 else if (bp->b_cmd == BUF_CMD_FREEBLKS)
743 bioqdisksort(&softc->bio_queue_trim, bio);
745 bioqdisksort(&softc->bio_queue_rd, bio);
748 * Schedule ourselves for performing the work.
750 xpt_schedule(periph, /* XXX priority */1);
751 cam_periph_unlock(periph);
755 bp->b_flags |= B_ERROR;
758 * Correctly set the buf to indicate a completed xfer
760 bp->b_resid = bp->b_bcount;
766 dadump(struct dev_dump_args *ap)
768 cdev_t dev = ap->a_head.a_dev;
769 struct cam_periph *periph;
770 struct da_softc *softc;
773 struct ccb_scsiio csio;
776 periph = cam_extend_get(daperiphs, unit);
780 softc = (struct da_softc *)periph->softc;
781 cam_periph_lock(periph);
782 secsize = softc->params.secsize; /* XXX: or ap->a_secsize? */
784 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
785 cam_periph_unlock(periph);
790 * because length == 0 means we are supposed to flush cache, we only
791 * try to write something if length > 0.
793 if (ap->a_length > 0) {
794 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
795 csio.ccb_h.flags |= CAM_POLLED;
796 csio.ccb_h.ccb_state = DA_CCB_DUMP;
797 scsi_read_write(&csio,
803 /*minimum_cmd_size*/ softc->minimum_cmd_size,
804 ap->a_offset / secsize,
805 ap->a_length / secsize,
806 /*data_ptr*/(u_int8_t *) ap->a_virtual,
807 /*dxfer_len*/ap->a_length,
808 /*sense_len*/SSD_FULL_SIZE,
809 DA_DEFAULT_TIMEOUT * 1000);
810 xpt_polled_action((union ccb *)&csio);
812 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
813 kprintf("Aborting dump due to I/O error.\n");
814 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
815 CAM_SCSI_STATUS_ERROR)
816 scsi_sense_print(&csio);
818 kprintf("status == 0x%x, scsi status == 0x%x\n",
819 csio.ccb_h.status, csio.scsi_status);
822 cam_periph_unlock(periph);
827 * Sync the disk cache contents to the physical media.
829 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
831 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
832 csio.ccb_h.ccb_state = DA_CCB_DUMP;
833 scsi_synchronize_cache(&csio,
837 /*begin_lba*/0,/* Cover the whole disk */
841 xpt_polled_action((union ccb *)&csio);
843 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
844 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
845 CAM_SCSI_STATUS_ERROR) {
847 int sense_key, error_code;
849 scsi_extract_sense(&csio.sense_data,
853 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
854 scsi_sense_print(&csio);
856 xpt_print(periph->path, "Synchronize cache "
857 "failed, status == 0x%x, scsi status == "
858 "0x%x\n", csio.ccb_h.status,
863 cam_periph_unlock(periph);
873 * Create our extend array for storing the devices we attach to.
875 daperiphs = cam_extend_new();
876 if (daperiphs == NULL) {
877 kprintf("da: Failed to alloc extend array!\n");
881 callout_init(&dasendorderedtag_ch);
884 * Install a global async callback. This callback will
885 * receive async callbacks like "new device found".
887 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
889 if (status != CAM_REQ_CMP) {
890 kprintf("da: Failed to attach master async callback "
891 "due to status 0x%x!\n", status);
892 } else if (da_send_ordered) {
894 /* Register our shutdown event handler */
895 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
896 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
897 kprintf("dainit: shutdown event registration failed!\n");
902 daoninvalidate(struct cam_periph *periph)
904 struct da_softc *softc;
906 softc = (struct da_softc *)periph->softc;
909 * De-register any async callbacks.
911 xpt_register_async(0, daasync, periph, periph->path);
913 softc->flags |= DA_FLAG_PACK_INVALID;
916 * Return all queued I/O with ENXIO.
917 * XXX Handle any transactions queued to the card
918 * with XPT_ABORT_CCB.
920 daflushbioq(&softc->bio_queue_trim, ENXIO);
921 daflushbioq(&softc->bio_queue_wr, ENXIO);
922 daflushbioq(&softc->bio_queue_rd, ENXIO);
923 xpt_print(periph->path, "lost device\n");
927 daflushbioq(struct bio_queue_head *bioq, int error)
932 while ((q_bio = bioq_first(bioq)) != NULL){
933 bioq_remove(bioq, q_bio);
934 q_bp = q_bio->bio_buf;
935 q_bp->b_resid = q_bp->b_bcount;
936 q_bp->b_error = error;
937 q_bp->b_flags |= B_ERROR;
943 dacleanup(struct cam_periph *periph)
945 struct da_softc *softc;
947 softc = (struct da_softc *)periph->softc;
949 devstat_remove_entry(&softc->device_stats);
950 cam_extend_release(daperiphs, periph->unit_number);
951 xpt_print(periph->path, "removing device entry\n");
953 * If we can't free the sysctl tree, oh well...
955 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
956 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
957 xpt_print(periph->path, "can't remove sysctl context\n");
959 periph->softc = NULL;
960 if (softc->disk.d_rawdev) {
961 cam_periph_unlock(periph);
962 disk_destroy(&softc->disk);
963 cam_periph_lock(periph);
966 callout_stop(&softc->sendordered_c);
967 kfree(softc, M_DEVBUF);
971 daasync(void *callback_arg, u_int32_t code,
972 struct cam_path *path, void *arg)
974 struct cam_periph *periph;
976 periph = (struct cam_periph *)callback_arg;
979 case AC_FOUND_DEVICE:
981 struct ccb_getdev *cgd;
985 cgd = (struct ccb_getdev *)arg;
989 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
990 && SID_TYPE(&cgd->inq_data) != T_RBC
991 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
995 * Don't complain if a valid peripheral is already attached.
997 periph = cam_periph_find(cgd->ccb_h.path, "da");
998 if (periph && (periph->flags & CAM_PERIPH_INVALID) == 0)
1002 * Allocate a peripheral instance for
1003 * this device and start the probe
1006 sim = xpt_path_sim(cgd->ccb_h.path);
1007 status = cam_periph_alloc(daregister, daoninvalidate,
1009 "da", CAM_PERIPH_BIO,
1010 cgd->ccb_h.path, daasync,
1011 AC_FOUND_DEVICE, cgd);
1013 if (status != CAM_REQ_CMP && status != CAM_REQ_INPROG) {
1014 kprintf("daasync: Unable to attach to new device "
1015 "due to status 0x%x\n", status);
1022 struct da_softc *softc;
1023 struct ccb_hdr *ccbh;
1025 softc = (struct da_softc *)periph->softc;
1027 * Don't fail on the expected unit attention
1030 softc->flags |= DA_FLAG_RETRY_UA;
1031 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1032 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1036 cam_periph_async(periph, code, path, arg);
1042 dasysctlinit(void *context, int pending)
1044 struct cam_periph *periph;
1045 struct da_softc *softc;
1046 char tmpstr[80], tmpstr2[80];
1049 periph = (struct cam_periph *)context;
1050 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1055 softc = (struct da_softc *)periph->softc;
1056 ksnprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1057 ksnprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1059 sysctl_ctx_init(&softc->sysctl_ctx);
1060 softc->flags |= DA_FLAG_SCTX_INIT;
1061 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1062 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1063 CTLFLAG_RD, 0, tmpstr);
1064 if (softc->sysctl_tree == NULL) {
1065 kprintf("dasysctlinit: unable to allocate sysctl tree\n");
1066 cam_periph_release(periph);
1072 * Now register the sysctl handler, so the user can the value on
1075 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1076 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1077 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1078 "Minimum CDB size");
1080 /* Only create the option if the device supports TRIM */
1081 if (softc->disk.d_info.d_trimflag) {
1082 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1083 SYSCTL_CHILDREN(softc->sysctl_tree),
1087 &softc->trim_enabled,
1089 "Enable TRIM for this device (SSD))");
1092 cam_periph_release(periph);
1097 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1101 value = *(int *)arg1;
1103 error = sysctl_handle_int(oidp, &value, 0, req);
1106 || (req->newptr == NULL))
1110 * Acceptable values here are 6, 10 or 12, or 16.
1114 else if ((value > 6)
1117 else if ((value > 10)
1120 else if (value > 12)
1123 *(int *)arg1 = value;
1129 daregister(struct cam_periph *periph, void *arg)
1131 struct da_softc *softc;
1132 struct ccb_pathinq cpi;
1133 struct ccb_getdev *cgd;
1137 cgd = (struct ccb_getdev *)arg;
1138 if (periph == NULL) {
1139 kprintf("daregister: periph was NULL!!\n");
1140 return(CAM_REQ_CMP_ERR);
1144 kprintf("daregister: no getdev CCB, can't register device\n");
1145 return(CAM_REQ_CMP_ERR);
1148 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
1149 LIST_INIT(&softc->pending_ccbs);
1150 softc->state = DA_STATE_PROBE;
1151 bioq_init(&softc->bio_queue_trim);
1152 bioq_init(&softc->bio_queue_rd);
1153 bioq_init(&softc->bio_queue_wr);
1154 if (SID_IS_REMOVABLE(&cgd->inq_data))
1155 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1156 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1157 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1159 /* Used to get TRIM status from AHCI driver */
1160 if (cgd->inq_data.vendor_specific1[0] == 1) {
1162 * max number of lba ranges an SSD can handle in a single
1163 * TRIM command. vendor_specific1[1] is the num of 512-byte
1164 * blocks the SSD reports that can be passed in a TRIM cmd.
1166 softc->trim_max_ranges =
1167 min(cgd->inq_data.vendor_specific1[1] * 64, TRIM_MAX_RANGES);
1170 periph->softc = softc;
1172 cam_extend_set(daperiphs, periph->unit_number, periph);
1175 * See if this device has any quirks.
1177 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1178 (caddr_t)da_quirk_table,
1179 NELEM(da_quirk_table),
1180 sizeof(*da_quirk_table), scsi_inquiry_match);
1183 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1185 softc->quirks = DA_Q_NONE;
1188 * Unconditionally disable the synchronize cache command for
1189 * usb attachments. It's just impossible to determine if the
1190 * device supports it or not and if it doesn't the port can
1193 if (strncmp(periph->sim->sim_name, "umass", 4) == 0) {
1194 softc->quirks |= DA_Q_NO_SYNC_CACHE;
1197 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1199 /* Check if the SIM does not want 6 byte commands */
1200 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
1201 cpi.ccb_h.func_code = XPT_PATH_INQ;
1202 xpt_action((union ccb *)&cpi);
1203 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1204 softc->quirks |= DA_Q_NO_6_BYTE;
1207 * RBC devices don't have to support READ(6), only READ(10).
1209 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1210 softc->minimum_cmd_size = 10;
1212 softc->minimum_cmd_size = 6;
1215 * Load the user's default, if any.
1217 ksnprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1218 periph->unit_number);
1219 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1222 * 6, 10, 12, and 16 are the currently permissible values.
1224 if (softc->minimum_cmd_size < 6)
1225 softc->minimum_cmd_size = 6;
1226 else if ((softc->minimum_cmd_size > 6)
1227 && (softc->minimum_cmd_size <= 10))
1228 softc->minimum_cmd_size = 10;
1229 else if ((softc->minimum_cmd_size > 10)
1230 && (softc->minimum_cmd_size <= 12))
1231 softc->minimum_cmd_size = 12;
1232 else if (softc->minimum_cmd_size > 12)
1233 softc->minimum_cmd_size = 16;
1236 * The DA driver supports a blocksize, but
1237 * we don't know the blocksize until we do
1238 * a read capacity. So, set a flag to
1239 * indicate that the blocksize is
1240 * unavailable right now. We'll clear the
1241 * flag as soon as we've done a read capacity.
1243 devstat_add_entry(&softc->device_stats, "da",
1244 periph->unit_number, 0,
1245 DEVSTAT_BS_UNAVAILABLE,
1246 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
1247 DEVSTAT_PRIORITY_DISK);
1250 * Register this media as a disk
1252 CAM_SIM_UNLOCK(periph->sim);
1253 disk_create(periph->unit_number, &softc->disk, &da_ops);
1254 softc->disk.d_rawdev->si_iosize_max = MAXPHYS;
1255 CAM_SIM_LOCK(periph->sim);
1258 * Add async callbacks for bus reset and
1259 * bus device reset calls. I don't bother
1260 * checking if this fails as, in most cases,
1261 * the system will function just fine without
1262 * them and the only alternative would be to
1263 * not attach the device on failure.
1265 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
1266 daasync, periph, periph->path);
1269 * Take an exclusive refcount on the periph while dastart is called
1270 * to finish the probe. The reference will be dropped in dadone at
1273 cam_periph_hold(periph, 0);
1274 xpt_schedule(periph, /*priority*/5);
1277 * Schedule a periodic event to occasionally send an
1278 * ordered tag to a device.
1280 callout_init(&softc->sendordered_c);
1281 callout_reset(&softc->sendordered_c,
1282 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
1283 dasendorderedtag, softc);
1287 return(CAM_REQ_CMP);
1291 dastart(struct cam_periph *periph, union ccb *start_ccb)
1293 struct da_softc *softc;
1295 softc = (struct da_softc *)periph->softc;
1297 switch (softc->state) {
1298 case DA_STATE_NORMAL:
1300 /* Pull a buffer from the queue and get going on it */
1309 * See if there is a buf with work for us to do..
1311 bio_rd = bioq_first(&softc->bio_queue_rd);
1312 bio_wr = bioq_first(&softc->bio_queue_wr);
1314 if (periph->immediate_priority <= periph->pinfo.priority) {
1315 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1316 ("queuing for immediate ccb\n"));
1317 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1318 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1320 periph->immediate_priority = CAM_PRIORITY_NONE;
1321 wakeup(&periph->ccb_list);
1322 if (bio_rd || bio_wr) {
1324 * Have more work to do, so ensure we stay
1327 xpt_schedule(periph, /* XXX priority */1);
1332 /* Run the trim command if not already running */
1333 if (!softc->trim_running &&
1334 (bio = bioq_first(&softc->bio_queue_trim)) != 0) {
1335 struct trim_request *req = &softc->trim_req;
1337 int bps = 0, ranges = 0;
1339 softc->trim_running = 1;
1340 bzero(req, sizeof(*req));
1347 count = bp->b_bcount / softc->params.secsize;
1348 lba = bio1->bio_offset/softc->params.secsize;
1350 kprintf("trim lba:%llu boff:%llu count:%d\n",
1351 (unsigned long long) lba,
1352 (unsigned long long) bio1->bio_offset,
1355 bioq_remove(&softc->bio_queue_trim, bio1);
1357 int c = min(count, 0xffff);
1358 int off = ranges * 8;
1360 req->data[off + 0] = lba & 0xff;
1361 req->data[off + 1] = (lba >> 8) & 0xff;
1362 req->data[off + 2] = (lba >> 16) & 0xff;
1363 req->data[off + 3] = (lba >> 24) & 0xff;
1364 req->data[off + 4] = (lba >> 32) & 0xff;
1365 req->data[off + 5] = (lba >> 40) & 0xff;
1366 req->data[off + 6] = c & 0xff;
1367 req->data[off + 7] = (c >> 8) & 0xff;
1373 /* Try to merge multiple TRIM requests */
1374 req->bios[bps++] = bio1;
1375 bio1 = bioq_first(&softc->bio_queue_trim);
1377 bio1->bio_buf->b_bcount / softc->params.secsize >
1378 (softc->trim_max_ranges - ranges) * 0xffff)
1383 cam_fill_csio(&start_ccb->csio,
1389 ((ranges +63)/64)*512,
1391 sizeof(struct scsi_rw_6),
1392 da_default_timeout*2);
1394 start_ccb->ccb_h.ccb_state = DA_CCB_TRIM;
1395 LIST_INSERT_HEAD(&softc->pending_ccbs,
1396 &start_ccb->ccb_h, periph_links.le);
1397 start_ccb->csio.ccb_h.func_code = XPT_TRIM;
1398 start_ccb->ccb_h.ccb_bio = bio;
1399 devstat_start_transaction(&softc->device_stats);
1400 xpt_action(start_ccb);
1401 xpt_schedule(periph, 1);
1406 * Select a read or write buffer to queue. Limit the number
1407 * of tags dedicated to reading or writing, giving reads
1410 * Writes to modern hard drives go into the HDs cache and
1411 * return completion nearly instantly. That is until the
1412 * cache becomes full. When the HDs cache becomes full
1413 * write commands will begin to stall. If all available
1414 * tags are taken up by writes which saturate the drive
1415 * reads will become tag-starved.
1417 * A similar situation can occur with reads. With many
1418 * parallel readers all tags can be taken up by reads
1419 * and prevent any writes from draining, even if the HD's
1420 * cache is not full.
1422 limit = periph->sim->max_tagged_dev_openings * 2 / 3 + 1;
1426 static long savets2;
1427 if (1 || time_second != savets2 || (ticks != savets && (softc->outstanding_cmds_rd || softc->outstanding_cmds_wr))) {
1428 kprintf("%d %d (%d)\n",
1429 softc->outstanding_cmds_rd,
1430 softc->outstanding_cmds_wr,
1433 savets2 = time_second;
1436 if (bio_rd && softc->outstanding_cmds_rd < limit) {
1438 bioq_remove(&softc->bio_queue_rd, bio);
1439 } else if (bio_wr && softc->outstanding_cmds_wr < limit) {
1441 bioq_remove(&softc->bio_queue_wr, bio);
1444 softc->flags |= DA_FLAG_RD_LIMIT;
1446 softc->flags |= DA_FLAG_WR_LIMIT;
1447 xpt_release_ccb(start_ccb);
1452 * We can queue new work.
1456 devstat_start_transaction(&softc->device_stats);
1458 if ((bp->b_flags & B_ORDERED) != 0 ||
1459 (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1460 softc->flags &= ~DA_FLAG_NEED_OTAG;
1461 softc->ordered_tag_count++;
1462 tag_code = MSG_ORDERED_Q_TAG;
1464 tag_code = MSG_SIMPLE_Q_TAG;
1471 * Block read/write op
1473 KKASSERT(bio->bio_offset % softc->params.secsize == 0);
1477 da_retry_count, /* retries */
1480 (bp->b_cmd == BUF_CMD_READ),
1482 softc->minimum_cmd_size,
1483 bio->bio_offset / softc->params.secsize,
1484 bp->b_bcount / softc->params.secsize,
1487 SSD_FULL_SIZE, /* sense_len */
1488 da_default_timeout * 1000
1493 * Silently complete a flush request if the device
1496 if (softc->quirks & DA_Q_NO_SYNC_CACHE) {
1497 xpt_release_ccb(start_ccb);
1499 devstat_end_transaction_buf(
1500 &softc->device_stats, bp);
1503 scsi_synchronize_cache(
1506 dadone, /* cbfcnp */
1509 0, /* count (whole disk) */
1511 da_default_timeout*1000 /* timeout */
1515 case BUF_CMD_FREEBLKS:
1516 if (softc->disk.d_info.d_trimflag & DA_FLAG_CAN_TRIM){
1517 start_ccb->csio.ccb_h.func_code = XPT_TRIM;
1521 xpt_release_ccb(start_ccb);
1523 panic("dastart: unrecognized bio cmd %d", bp->b_cmd);
1524 break; /* NOT REACHED */
1528 * Block out any asyncronous callbacks
1529 * while we touch the pending ccb list.
1532 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1533 LIST_INSERT_HEAD(&softc->pending_ccbs,
1534 &start_ccb->ccb_h, periph_links.le);
1535 if (bp->b_cmd == BUF_CMD_WRITE ||
1536 bp->b_cmd == BUF_CMD_FLUSH) {
1537 ++softc->outstanding_cmds_wr;
1539 ++softc->outstanding_cmds_rd;
1542 /* We expect a unit attention from this device */
1543 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1544 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1545 softc->flags &= ~DA_FLAG_RETRY_UA;
1548 start_ccb->ccb_h.ccb_bio = bio;
1549 xpt_action(start_ccb);
1553 * Be sure we stay scheduled if we have more work to do.
1555 if (bioq_first(&softc->bio_queue_rd) ||
1556 bioq_first(&softc->bio_queue_wr)) {
1557 xpt_schedule(periph, 1);
1561 case DA_STATE_PROBE:
1563 struct ccb_scsiio *csio;
1564 struct scsi_read_capacity_data *rcap;
1566 rcap = kmalloc(sizeof(*rcap), M_SCSIDA, M_INTWAIT | M_ZERO);
1567 csio = &start_ccb->csio;
1568 scsi_read_capacity(csio,
1575 start_ccb->ccb_h.ccb_bio = NULL;
1576 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1577 xpt_action(start_ccb);
1580 case DA_STATE_PROBE2:
1582 struct ccb_scsiio *csio;
1583 struct scsi_read_capacity_data_16 *rcaplong;
1585 rcaplong = kmalloc(sizeof(*rcaplong), M_SCSIDA,
1586 M_INTWAIT | M_ZERO);
1587 if (rcaplong == NULL) {
1588 kprintf("dastart: Couldn't allocate read_capacity\n");
1589 /* da_free_periph??? */
1592 csio = &start_ccb->csio;
1593 scsi_read_capacity_16(csio,
1596 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1601 /*sense_len*/ SSD_FULL_SIZE,
1603 start_ccb->ccb_h.ccb_bio = NULL;
1604 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1605 xpt_action(start_ccb);
1612 cmd6workaround(union ccb *ccb)
1614 struct scsi_rw_6 cmd6;
1615 struct scsi_rw_10 *cmd10;
1616 struct da_softc *softc;
1620 cdb = ccb->csio.cdb_io.cdb_bytes;
1622 /* Translation only possible if CDB is an array and cmd is R/W6 */
1623 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1624 (*cdb != READ_6 && *cdb != WRITE_6))
1627 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
1628 "increasing minimum_cmd_size to 10.\n");
1629 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1630 softc->minimum_cmd_size = 10;
1632 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1633 cmd10 = (struct scsi_rw_10 *)cdb;
1634 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1636 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1637 cmd10->reserved = 0;
1638 scsi_ulto2b(cmd6.length, cmd10->length);
1639 cmd10->control = cmd6.control;
1640 ccb->csio.cdb_len = sizeof(*cmd10);
1642 /* Requeue request, unfreezing queue if necessary */
1643 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1644 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1647 cam_release_devq(ccb->ccb_h.path,
1651 /*getcount_only*/0);
1657 dadone(struct cam_periph *periph, union ccb *done_ccb)
1659 struct da_softc *softc;
1660 struct ccb_scsiio *csio;
1661 struct disk_info info;
1663 softc = (struct da_softc *)periph->softc;
1664 csio = &done_ccb->csio;
1665 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1666 case DA_CCB_BUFFER_IO:
1673 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1675 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1679 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1684 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1685 if (error == ERESTART) {
1687 * A retry was scheuled, so
1693 if (error == ENXIO) {
1695 * Catastrophic error. Mark our pack as
1699 * XXX See if this is really a media
1702 xpt_print(periph->path,
1703 "Invalidating pack\n");
1704 softc->flags |= DA_FLAG_PACK_INVALID;
1708 * Return all queued write I/O's with EIO
1709 * so the client can retry these I/Os in the
1710 * proper order should it attempt to recover.
1712 * Leave read I/O's alone.
1714 daflushbioq(&softc->bio_queue_wr, EIO);
1715 bp->b_error = error;
1716 bp->b_resid = bp->b_bcount;
1717 bp->b_flags |= B_ERROR;
1719 bp->b_resid = csio->resid;
1721 if (bp->b_resid != 0)
1722 bp->b_flags |= B_ERROR;
1724 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1725 cam_release_devq(done_ccb->ccb_h.path,
1729 /*getcount_only*/0);
1731 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1732 panic("REQ_CMP with QFRZN");
1733 bp->b_resid = csio->resid;
1734 if (csio->resid > 0)
1735 bp->b_flags |= B_ERROR;
1739 * Block out any asyncronous callbacks
1740 * while we touch the pending ccb list.
1742 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1743 if (bp->b_cmd == BUF_CMD_WRITE || bp->b_cmd == BUF_CMD_FLUSH) {
1744 --softc->outstanding_cmds_wr;
1745 if (softc->flags & DA_FLAG_WR_LIMIT) {
1746 softc->flags &= ~DA_FLAG_WR_LIMIT;
1750 --softc->outstanding_cmds_rd;
1751 if (softc->flags & DA_FLAG_RD_LIMIT) {
1752 softc->flags &= ~DA_FLAG_RD_LIMIT;
1756 if (softc->outstanding_cmds_rd +
1757 softc->outstanding_cmds_wr == 0) {
1758 softc->flags |= DA_FLAG_WENT_IDLE;
1761 devstat_end_transaction_buf(&softc->device_stats, bp);
1762 if ((csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) ==
1764 struct trim_request *req =
1765 (struct trim_request *) csio->data_ptr;
1768 for (i = 1; i < softc->trim_max_ranges &&
1769 req->bios[i]; i++) {
1770 struct bio *bp1 = req->bios[i];
1772 bp1->bio_buf->b_resid = bp->b_resid;
1773 bp1->bio_buf->b_error = bp->b_error;
1774 if (bp->b_flags & B_ERROR)
1775 bp1->bio_buf->b_flags |= B_ERROR;
1778 softc->trim_running = 0;
1780 xpt_schedule(periph,1);
1786 xpt_schedule(periph, /*priority*/1);
1793 struct scsi_read_capacity_data *rdcap;
1794 struct scsi_read_capacity_data_16 *rcaplong;
1795 char announce_buf[80];
1799 if (softc->state == DA_STATE_PROBE)
1800 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1802 rcaplong = (struct scsi_read_capacity_data_16 *)
1805 bzero(&info, sizeof(info));
1806 info.d_type = DTYPE_SCSI;
1807 info.d_serialno = xpt_path_serialno(periph->path);
1809 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1810 struct disk_params *dp;
1811 uint32_t block_size;
1814 if (softc->state == DA_STATE_PROBE) {
1815 block_size = scsi_4btoul(rdcap->length);
1816 maxsector = scsi_4btoul(rdcap->addr);
1819 * According to SBC-2, if the standard 10
1820 * byte READ CAPACITY command returns 2^32,
1821 * we should issue the 16 byte version of
1822 * the command, since the device in question
1823 * has more sectors than can be represented
1824 * with the short version of the command.
1826 if (maxsector == 0xffffffff) {
1827 softc->state = DA_STATE_PROBE2;
1828 kfree(rdcap, M_SCSIDA);
1829 xpt_release_ccb(done_ccb);
1830 xpt_schedule(periph, /*priority*/5);
1834 block_size = scsi_4btoul(rcaplong->length);
1835 maxsector = scsi_8btou64(rcaplong->addr);
1837 dasetgeom(periph, block_size, maxsector);
1838 dp = &softc->params;
1839 ksnprintf(announce_buf, sizeof(announce_buf),
1840 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1841 (uintmax_t) (((uintmax_t)dp->secsize *
1842 dp->sectors) / (1024*1024)),
1843 (uintmax_t)dp->sectors,
1844 dp->secsize, dp->heads, dp->secs_per_track,
1847 CAM_SIM_UNLOCK(periph->sim);
1848 info.d_media_blksize = softc->params.secsize;
1849 info.d_media_blocks = softc->params.sectors;
1850 info.d_media_size = 0;
1851 info.d_secpertrack = softc->params.secs_per_track;
1852 info.d_nheads = softc->params.heads;
1853 info.d_ncylinders = softc->params.cylinders;
1854 info.d_secpercyl = softc->params.heads *
1855 softc->params.secs_per_track;
1856 info.d_serialno = xpt_path_serialno(periph->path);
1857 disk_setdiskinfo(&softc->disk, &info);
1858 CAM_SIM_LOCK(periph->sim);
1862 announce_buf[0] = '\0';
1865 * Retry any UNIT ATTENTION type errors. They
1866 * are expected at boot.
1868 error = daerror(done_ccb, CAM_RETRY_SELTO,
1869 SF_RETRY_UA|SF_NO_PRINT);
1870 if (error == ERESTART) {
1872 * A retry was scheuled, so
1876 } else if (error != 0) {
1877 struct scsi_sense_data *sense;
1879 int sense_key, error_code;
1882 struct ccb_getdev cgd;
1884 /* Don't wedge this device's queue */
1885 status = done_ccb->ccb_h.status;
1886 if ((status & CAM_DEV_QFRZN) != 0)
1887 cam_release_devq(done_ccb->ccb_h.path,
1891 /*getcount_only*/0);
1894 xpt_setup_ccb(&cgd.ccb_h,
1895 done_ccb->ccb_h.path,
1897 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1898 xpt_action((union ccb *)&cgd);
1900 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1901 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1902 || ((status & CAM_AUTOSNS_VALID) == 0))
1908 sense = &csio->sense_data;
1909 scsi_extract_sense(sense, &error_code,
1914 * Attach to anything that claims to be a
1915 * direct access or optical disk device,
1916 * as long as it doesn't return a "Logical
1917 * unit not supported" (0x25) error.
1919 if ((have_sense) && (asc != 0x25)
1920 && (error_code == SSD_CURRENT_ERROR)) {
1921 const char *sense_key_desc;
1922 const char *asc_desc;
1924 scsi_sense_desc(sense_key, asc, ascq,
1928 ksnprintf(announce_buf,
1929 sizeof(announce_buf),
1930 "Attempt to query device "
1931 "size failed: %s, %s",
1934 info.d_media_blksize = 512;
1935 disk_setdiskinfo(&softc->disk, &info);
1941 xpt_print(periph->path,
1942 "got CAM status %#x\n",
1943 done_ccb->ccb_h.status);
1946 xpt_print(periph->path, "fatal error, "
1947 "failed to attach to device\n");
1950 * Free up resources.
1952 cam_periph_invalidate(periph);
1956 kfree(csio->data_ptr, M_SCSIDA);
1957 if (announce_buf[0] != '\0') {
1958 xpt_announce_periph(periph, announce_buf);
1960 * Create our sysctl variables, now that we know
1961 * we have successfully attached.
1963 taskqueue_enqueue(taskqueue_thread[mycpuid],
1964 &softc->sysctl_task);
1967 if (softc->trim_max_ranges) {
1968 softc->disk.d_info.d_trimflag |= DA_FLAG_CAN_TRIM;
1969 kprintf("%s%d: supports TRIM\n",
1970 periph->periph_name,
1971 periph->unit_number);
1973 softc->state = DA_STATE_NORMAL;
1975 * Since our peripheral may be invalidated by an error
1976 * above or an external event, we must release our CCB
1977 * before releasing the probe lock on the peripheral.
1978 * The peripheral will only go away once the last lock
1979 * is removed, and we need it around for the CCB release
1982 xpt_release_ccb(done_ccb);
1983 cam_periph_unhold(periph, 0);
1986 case DA_CCB_WAITING:
1988 /* Caller will release the CCB */
1989 wakeup(&done_ccb->ccb_h.cbfcnp);
1993 /* No-op. We're polling */
1998 xpt_release_ccb(done_ccb);
2002 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
2004 struct da_softc *softc;
2005 struct cam_periph *periph;
2008 periph = xpt_path_periph(ccb->ccb_h.path);
2009 softc = (struct da_softc *)periph->softc;
2012 * Automatically detect devices that do not support
2013 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
2016 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
2017 error = cmd6workaround(ccb);
2018 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
2019 CAM_SCSI_STATUS_ERROR)
2020 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
2021 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
2022 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
2023 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
2024 int sense_key, error_code, asc, ascq;
2026 scsi_extract_sense(&ccb->csio.sense_data,
2027 &error_code, &sense_key, &asc, &ascq);
2028 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
2029 error = cmd6workaround(ccb);
2031 if (error == ERESTART)
2036 * Until we have a better way of doing pack validation,
2037 * don't treat UAs as errors.
2039 sense_flags |= SF_RETRY_UA;
2040 return(cam_periph_error(ccb, cam_flags, sense_flags,
2041 &softc->saved_ccb));
2045 daprevent(struct cam_periph *periph, int action)
2047 struct da_softc *softc;
2051 softc = (struct da_softc *)periph->softc;
2053 if (((action == PR_ALLOW)
2054 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
2055 || ((action == PR_PREVENT)
2056 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
2060 ccb = cam_periph_getccb(periph, /*priority*/1);
2062 scsi_prevent(&ccb->csio,
2070 error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
2071 SF_RETRY_UA, &softc->device_stats);
2074 if (action == PR_ALLOW)
2075 softc->flags &= ~DA_FLAG_PACK_LOCKED;
2077 softc->flags |= DA_FLAG_PACK_LOCKED;
2080 xpt_release_ccb(ccb);
2084 * Check media on open, e.g. card reader devices which had no initial media.
2087 dacheckmedia(struct cam_periph *periph)
2089 struct disk_params *dp;
2090 struct da_softc *softc;
2091 struct disk_info info;
2094 softc = (struct da_softc *)periph->softc;
2095 dp = &softc->params;
2097 error = dagetcapacity(periph);
2100 * Only reprobe on initial open and if the media is removable.
2102 * NOTE: If we setdiskinfo() it will take the device probe
2103 * a bit of time to probe the slices and partitions,
2104 * and mess up booting. So avoid if nothing has changed.
2107 if (softc->flags & DA_FLAG_OPEN)
2109 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) == 0)
2112 bzero(&info, sizeof(info));
2113 info.d_type = DTYPE_SCSI;
2114 info.d_serialno = xpt_path_serialno(periph->path);
2117 CAM_SIM_UNLOCK(periph->sim);
2118 info.d_media_blksize = softc->params.secsize;
2119 info.d_media_blocks = softc->params.sectors;
2120 info.d_media_size = 0;
2121 info.d_secpertrack = softc->params.secs_per_track;
2122 info.d_nheads = softc->params.heads;
2123 info.d_ncylinders = softc->params.cylinders;
2124 info.d_secpercyl = softc->params.heads *
2125 softc->params.secs_per_track;
2126 info.d_serialno = xpt_path_serialno(periph->path);
2127 if (info.d_media_blocks != softc->disk.d_info.d_media_blocks) {
2128 kprintf("%s%d: open removable media: "
2129 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)\n",
2130 periph->periph_name, periph->unit_number,
2131 (uintmax_t)(((uintmax_t)dp->secsize *
2132 dp->sectors) / (1024*1024)),
2133 (uintmax_t)dp->sectors, dp->secsize,
2134 dp->heads, dp->secs_per_track, dp->cylinders);
2135 disk_setdiskinfo(&softc->disk, &info);
2137 CAM_SIM_LOCK(periph->sim);
2139 kprintf("%s%d: open removable media: no media present\n",
2140 periph->periph_name, periph->unit_number);
2141 info.d_media_blksize = 512;
2142 disk_setdiskinfo(&softc->disk, &info);
2148 dagetcapacity(struct cam_periph *periph)
2150 struct da_softc *softc;
2152 struct scsi_read_capacity_data *rcap;
2153 struct scsi_read_capacity_data_16 *rcaplong;
2158 softc = (struct da_softc *)periph->softc;
2163 /* Do a read capacity */
2164 rcap = (struct scsi_read_capacity_data *)kmalloc(sizeof(*rcaplong),
2165 M_SCSIDA, M_INTWAIT);
2167 ccb = cam_periph_getccb(periph, /*priority*/1);
2168 scsi_read_capacity(&ccb->csio,
2175 ccb->ccb_h.ccb_bio = NULL;
2177 error = cam_periph_runccb(ccb, daerror,
2178 /*cam_flags*/CAM_RETRY_SELTO,
2179 /*sense_flags*/SF_RETRY_UA,
2180 &softc->device_stats);
2182 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2183 cam_release_devq(ccb->ccb_h.path,
2187 /*getcount_only*/0);
2190 block_len = scsi_4btoul(rcap->length);
2191 maxsector = scsi_4btoul(rcap->addr);
2193 if (maxsector != 0xffffffff)
2198 rcaplong = (struct scsi_read_capacity_data_16 *)rcap;
2200 scsi_read_capacity_16(&ccb->csio,
2203 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2208 /*sense_len*/ SSD_FULL_SIZE,
2210 ccb->ccb_h.ccb_bio = NULL;
2212 error = cam_periph_runccb(ccb, daerror,
2213 /*cam_flags*/CAM_RETRY_SELTO,
2214 /*sense_flags*/SF_RETRY_UA,
2215 &softc->device_stats);
2217 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2218 cam_release_devq(ccb->ccb_h.path,
2222 /*getcount_only*/0);
2225 block_len = scsi_4btoul(rcaplong->length);
2226 maxsector = scsi_8btou64(rcaplong->addr);
2232 dasetgeom(periph, block_len, maxsector);
2234 xpt_release_ccb(ccb);
2236 kfree(rcap, M_SCSIDA);
2242 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector)
2244 struct ccb_calc_geometry ccg;
2245 struct da_softc *softc;
2246 struct disk_params *dp;
2248 softc = (struct da_softc *)periph->softc;
2250 dp = &softc->params;
2251 dp->secsize = block_len;
2252 dp->sectors = maxsector + 1;
2254 * Have the controller provide us with a geometry
2255 * for this disk. The only time the geometry
2256 * matters is when we boot and the controller
2257 * is the only one knowledgeable enough to come
2258 * up with something that will make this a bootable
2261 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
2262 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
2263 ccg.block_size = dp->secsize;
2264 ccg.volume_size = dp->sectors;
2266 ccg.secs_per_track = 0;
2268 xpt_action((union ccb*)&ccg);
2269 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2271 * We don't know what went wrong here- but just pick
2272 * a geometry so we don't have nasty things like divide
2276 dp->secs_per_track = 255;
2277 dp->cylinders = dp->sectors / (255 * 255);
2278 if (dp->cylinders == 0) {
2282 dp->heads = ccg.heads;
2283 dp->secs_per_track = ccg.secs_per_track;
2284 dp->cylinders = ccg.cylinders;
2289 dasendorderedtag(void *arg)
2291 struct da_softc *softc = arg;
2293 if (da_send_ordered) {
2294 if ((softc->ordered_tag_count == 0)
2295 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
2296 softc->flags |= DA_FLAG_NEED_OTAG;
2298 if (softc->outstanding_cmds_rd || softc->outstanding_cmds_wr)
2299 softc->flags &= ~DA_FLAG_WENT_IDLE;
2301 softc->ordered_tag_count = 0;
2303 /* Queue us up again */
2304 callout_reset(&softc->sendordered_c,
2305 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
2306 dasendorderedtag, softc);
2310 * Step through all DA peripheral drivers, and if the device is still open,
2311 * sync the disk cache to physical media.
2314 dashutdown(void * arg, int howto)
2316 struct cam_periph *periph;
2317 struct da_softc *softc;
2319 TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
2322 cam_periph_lock(periph);
2323 softc = (struct da_softc *)periph->softc;
2326 * We only sync the cache if the drive is still open, and
2327 * if the drive is capable of it..
2329 if (((softc->flags & DA_FLAG_OPEN) == 0)
2330 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
2331 cam_periph_unlock(periph);
2335 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
2337 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
2338 scsi_synchronize_cache(&ccb.csio,
2342 /*begin_lba*/0, /* whole disk */
2347 xpt_polled_action(&ccb);
2349 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2350 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2351 CAM_SCSI_STATUS_ERROR)
2352 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
2353 int error_code, sense_key, asc, ascq;
2355 scsi_extract_sense(&ccb.csio.sense_data,
2356 &error_code, &sense_key,
2359 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
2360 scsi_sense_print(&ccb.csio);
2362 xpt_print(periph->path, "Synchronize "
2363 "cache failed, status == 0x%x, scsi status "
2364 "== 0x%x\n", ccb.ccb_h.status,
2365 ccb.csio.scsi_status);
2369 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
2370 cam_release_devq(ccb.ccb_h.path,
2374 /*getcount_only*/0);
2376 cam_periph_unlock(periph);
2380 #else /* !_KERNEL */
2383 * XXX This is only left out of the kernel build to silence warnings. If,
2384 * for some reason this function is used in the kernel, the ifdefs should
2385 * be moved so it is included both in the kernel and userland.
2388 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2389 void (*cbfcnp)(struct cam_periph *, union ccb *),
2390 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2391 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2394 struct scsi_format_unit *scsi_cmd;
2396 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2397 scsi_cmd->opcode = FORMAT_UNIT;
2398 scsi_cmd->byte2 = byte2;
2399 scsi_ulto2b(ileave, scsi_cmd->interleave);
2404 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2413 #endif /* _KERNEL */