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>
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
104 DA_Q_NO_SYNC_CACHE = 0x01,
105 DA_Q_NO_6_BYTE = 0x02,
106 DA_Q_NO_PREVENT = 0x04
111 DA_CCB_PROBE2 = 0x02,
112 DA_CCB_BUFFER_IO = 0x03,
113 DA_CCB_WAITING = 0x04,
115 DA_CCB_TYPE_MASK = 0x0F,
116 DA_CCB_RETRY_UA = 0x10
119 /* Offsets into our private area for storing information */
120 #define ccb_state ppriv_field0
121 #define ccb_bio ppriv_ptr1
126 u_int8_t secs_per_track;
127 u_int32_t secsize; /* Number of bytes/sector */
128 u_int64_t sectors; /* total number sectors */
132 struct bio_queue_head bio_queue_rd;
133 struct bio_queue_head bio_queue_wr;
134 struct devstat device_stats;
135 SLIST_ENTRY(da_softc) links;
136 LIST_HEAD(, ccb_hdr) pending_ccbs;
140 int minimum_cmd_size;
141 int ordered_tag_count;
142 int outstanding_cmds_rd;
143 int outstanding_cmds_wr;
144 struct disk_params params;
147 struct task sysctl_task;
148 struct sysctl_ctx_list sysctl_ctx;
149 struct sysctl_oid *sysctl_tree;
150 struct callout sendordered_c;
153 struct da_quirk_entry {
154 struct scsi_inquiry_pattern inq_pat;
158 static const char quantum[] = "QUANTUM";
159 static const char microp[] = "MICROP";
161 static struct da_quirk_entry da_quirk_table[] =
163 /* SPI, FC devices */
166 * Fujitsu M2513A MO drives.
167 * Tested devices: M2513A2 firmware versions 1200 & 1300.
168 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
169 * Reported by: W.Scholten <whs@xs4all.nl>
171 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
172 /*quirks*/ DA_Q_NO_SYNC_CACHE
176 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
177 /*quirks*/ DA_Q_NO_SYNC_CACHE
181 * This particular Fujitsu drive doesn't like the
182 * synchronize cache command.
183 * Reported by: Tom Jackson <toj@gorilla.net>
185 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
186 /*quirks*/ DA_Q_NO_SYNC_CACHE
190 * This drive doesn't like the synchronize cache command
191 * either. Reported by: Matthew Jacob <mjacob@feral.com>
192 * in NetBSD PR kern/6027, August 24, 1998.
194 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
195 /*quirks*/ DA_Q_NO_SYNC_CACHE
199 * This drive doesn't like the synchronize cache command
200 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
203 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
204 /*quirks*/ DA_Q_NO_SYNC_CACHE
208 * Doesn't like the synchronize cache command.
209 * Reported by: Blaz Zupan <blaz@gold.amis.net>
211 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
212 /*quirks*/ DA_Q_NO_SYNC_CACHE
216 * Doesn't like the synchronize cache command.
217 * Reported by: Blaz Zupan <blaz@gold.amis.net>
219 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
220 /*quirks*/ DA_Q_NO_SYNC_CACHE
224 * Doesn't like the synchronize cache command.
226 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
227 /*quirks*/ DA_Q_NO_SYNC_CACHE
231 * Doesn't like the synchronize cache command.
232 * Reported by: walter@pelissero.de
234 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
235 /*quirks*/ DA_Q_NO_SYNC_CACHE
239 * Doesn't work correctly with 6 byte reads/writes.
240 * Returns illegal request, and points to byte 9 of the
242 * Reported by: Adam McDougall <bsdx@spawnet.com>
244 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
245 /*quirks*/ DA_Q_NO_6_BYTE
249 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
250 /*quirks*/ DA_Q_NO_6_BYTE
254 * Doesn't like the synchronize cache command.
255 * Reported by: walter@pelissero.de
257 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
258 /*quirks*/ DA_Q_NO_SYNC_CACHE
262 * The CISS RAID controllers do not support SYNC_CACHE
264 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
265 /*quirks*/ DA_Q_NO_SYNC_CACHE
269 * The same goes for the mly(4) controllers
271 {T_DIRECT, SIP_MEDIA_FIXED, "MLY*", "*", "MYLX"},
272 /*quirks*/ DA_Q_NO_SYNC_CACHE
275 * USB mass storage devices supported by umass(4)
277 * NOTE: USB attachments automatically set DA_Q_NO_SYNC_CACHE so
278 * it does not have to be specified here.
282 * Creative Nomad MUVO mp3 player (USB)
285 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
286 /*quirks*/ DA_Q_NO_PREVENT
290 * Sigmatel USB Flash MP3 Player
293 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
294 /*quirks*/ DA_Q_NO_PREVENT
298 * SEAGRAND NP-900 MP3 Player
301 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
302 /*quirks*/ DA_Q_NO_PREVENT
306 * Creative MUVO Slim mp3 player (USB)
309 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
310 "*"}, /*quirks*/ DA_Q_NO_PREVENT
314 * Philips USB Key Audio KEY013
317 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
318 /*quirks*/ DA_Q_NO_PREVENT
322 static d_open_t daopen;
323 static d_close_t daclose;
324 static d_strategy_t dastrategy;
325 static d_dump_t dadump;
326 static periph_init_t dainit;
327 static void daasync(void *callback_arg, u_int32_t code,
328 struct cam_path *path, void *arg);
329 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
330 static periph_ctor_t daregister;
331 static periph_dtor_t dacleanup;
332 static periph_start_t dastart;
333 static periph_oninv_t daoninvalidate;
334 static void dadone(struct cam_periph *periph,
335 union ccb *done_ccb);
336 static int daerror(union ccb *ccb, u_int32_t cam_flags,
337 u_int32_t sense_flags);
338 static void daprevent(struct cam_periph *periph, int action);
339 static int dagetcapacity(struct cam_periph *periph);
340 static int dacheckmedia(struct cam_periph *periph);
341 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
343 static void daflushbioq(struct bio_queue_head *bioq, int error);
344 static timeout_t dasendorderedtag;
345 static void dashutdown(void *arg, int howto);
347 #ifndef DA_DEFAULT_TIMEOUT
348 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
351 #ifndef DA_DEFAULT_RETRY
352 #define DA_DEFAULT_RETRY 4
355 #ifndef DA_DEFAULT_SEND_ORDERED
356 #define DA_DEFAULT_SEND_ORDERED 1
359 static int da_retry_count = DA_DEFAULT_RETRY;
360 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
361 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
362 static struct callout dasendorderedtag_ch;
364 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
365 "CAM Direct Access Disk driver");
366 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
367 &da_retry_count, 0, "Normal I/O retry count");
368 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
369 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
370 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
371 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
372 SYSCTL_INT(_kern_cam_da, OID_AUTO, da_send_ordered, CTLFLAG_RW,
373 &da_send_ordered, 0, "Send Ordered Tags");
374 TUNABLE_INT("kern.cam.da.da_send_ordered", &da_send_ordered);
377 * DA_ORDEREDTAG_INTERVAL determines how often, relative
378 * to the default timeout, we check to see whether an ordered
379 * tagged transaction is appropriate to prevent simple tag
380 * starvation. Since we'd like to ensure that there is at least
381 * 1/2 of the timeout length left for a starved transaction to
382 * complete after we've sent an ordered tag, we must poll at least
383 * four times in every timeout period. This takes care of the worst
384 * case where a starved transaction starts during an interval that
385 * meets the requirement "don't send an ordered tag" test so it takes
386 * us two intervals to determine that a tag must be sent.
388 #ifndef DA_ORDEREDTAG_INTERVAL
389 #define DA_ORDEREDTAG_INTERVAL 4
392 static struct periph_driver dadriver =
395 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
398 PERIPHDRIVER_DECLARE(da, dadriver);
400 static struct dev_ops da_ops = {
401 { "da", DA_CDEV_MAJOR, D_DISK | D_MPSAFE },
405 .d_write = physwrite,
406 .d_strategy = dastrategy,
410 static struct extend_array *daperiphs;
412 MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
415 daopen(struct dev_open_args *ap)
417 cdev_t dev = ap->a_head.a_dev;
418 struct cam_periph *periph;
419 struct da_softc *softc;
420 struct disk_info info;
425 periph = cam_extend_get(daperiphs, unit);
426 if (periph == NULL) {
430 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
434 cam_periph_lock(periph);
435 if ((error = cam_periph_hold(periph, PCATCH)) != 0) {
436 cam_periph_unlock(periph);
437 cam_periph_release(periph);
441 unit = periph->unit_number;
442 softc = (struct da_softc *)periph->softc;
444 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
445 ("daopen: dev=%s (unit %d)\n", devtoname(dev),
448 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
449 /* Invalidate our pack information. */
450 disk_invalidate(&softc->disk);
451 softc->flags &= ~DA_FLAG_PACK_INVALID;
454 error = dacheckmedia(periph);
455 softc->flags |= DA_FLAG_OPEN;
458 struct ccb_getdev cgd;
460 /* Build disk information structure */
461 bzero(&info, sizeof(info));
462 info.d_type = DTYPE_SCSI;
465 * Grab the inquiry data to get the vendor and product names.
466 * Put them in the typename and packname for the label.
468 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
469 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
470 xpt_action((union ccb *)&cgd);
473 * Check to see whether or not the blocksize is set yet.
474 * If it isn't, set it and then clear the blocksize
475 * unavailable flag for the device statistics.
477 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0){
478 softc->device_stats.block_size = softc->params.secsize;
479 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
484 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
485 (softc->quirks & DA_Q_NO_PREVENT) == 0)
486 daprevent(periph, PR_PREVENT);
488 softc->flags &= ~DA_FLAG_OPEN;
489 cam_periph_release(periph);
491 cam_periph_unhold(periph, 1);
496 daclose(struct dev_close_args *ap)
498 cdev_t dev = ap->a_head.a_dev;
499 struct cam_periph *periph;
500 struct da_softc *softc;
505 periph = cam_extend_get(daperiphs, unit);
509 cam_periph_lock(periph);
510 if ((error = cam_periph_hold(periph, 0)) != 0) {
511 cam_periph_unlock(periph);
512 cam_periph_release(periph);
516 softc = (struct da_softc *)periph->softc;
518 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
521 ccb = cam_periph_getccb(periph, /*priority*/1);
523 scsi_synchronize_cache(&ccb->csio,
527 /*begin_lba*/0,/* Cover the whole disk */
532 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
533 /*sense_flags*/SF_RETRY_UA,
534 &softc->device_stats);
536 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
537 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
538 CAM_SCSI_STATUS_ERROR) {
540 int sense_key, error_code;
542 scsi_extract_sense(&ccb->csio.sense_data,
546 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
547 scsi_sense_print(&ccb->csio);
549 xpt_print(periph->path, "Synchronize cache "
550 "failed, status == 0x%x, scsi status == "
551 "0x%x\n", ccb->csio.ccb_h.status,
552 ccb->csio.scsi_status);
556 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
557 cam_release_devq(ccb->ccb_h.path,
563 xpt_release_ccb(ccb);
567 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
568 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
569 daprevent(periph, PR_ALLOW);
571 * If we've got removeable media, mark the blocksize as
572 * unavailable, since it could change when new media is
575 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
579 * Don't compound any ref counting software bugs with more.
581 if (softc->flags & DA_FLAG_OPEN) {
582 softc->flags &= ~DA_FLAG_OPEN;
583 cam_periph_release(periph);
585 xpt_print(periph->path,
586 "daclose() called on an already closed device!\n");
588 cam_periph_unhold(periph, 1);
593 * Actually translate the requested transfer into one the physical driver
594 * can understand. The transfer is described by a buf and will include
595 * only one physical transfer.
598 dastrategy(struct dev_strategy_args *ap)
600 cdev_t dev = ap->a_head.a_dev;
601 struct bio *bio = ap->a_bio;
602 struct buf *bp = bio->bio_buf;
603 struct cam_periph *periph;
604 struct da_softc *softc;
610 periph = cam_extend_get(daperiphs, unit);
611 if (periph == NULL) {
615 softc = (struct da_softc *)periph->softc;
617 cam_periph_lock(periph);
621 * check it's not too big a transfer for our adapter
623 scsi_minphys(bp, &sd_switch);
627 * Mask interrupts so that the pack cannot be invalidated until
628 * after we are in the queue. Otherwise, we might not properly
629 * clean up one of the buffers.
633 * If the device has been made invalid, error out
635 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
636 cam_periph_unlock(periph);
642 * Place it in the queue of disk activities for this disk
644 if (bp->b_cmd == BUF_CMD_WRITE || bp->b_cmd == BUF_CMD_FLUSH)
645 bioqdisksort(&softc->bio_queue_wr, bio);
647 bioqdisksort(&softc->bio_queue_rd, bio);
650 * Schedule ourselves for performing the work.
652 xpt_schedule(periph, /* XXX priority */1);
653 cam_periph_unlock(periph);
657 bp->b_flags |= B_ERROR;
660 * Correctly set the buf to indicate a completed xfer
662 bp->b_resid = bp->b_bcount;
668 dadump(struct dev_dump_args *ap)
670 cdev_t dev = ap->a_head.a_dev;
671 struct cam_periph *periph;
672 struct da_softc *softc;
675 struct ccb_scsiio csio;
678 periph = cam_extend_get(daperiphs, unit);
682 softc = (struct da_softc *)periph->softc;
683 cam_periph_lock(periph);
684 secsize = softc->params.secsize; /* XXX: or ap->a_secsize? */
686 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
687 cam_periph_unlock(periph);
692 * because length == 0 means we are supposed to flush cache, we only
693 * try to write something if length > 0.
695 if (ap->a_length > 0) {
696 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
697 csio.ccb_h.flags |= CAM_POLLED;
698 csio.ccb_h.ccb_state = DA_CCB_DUMP;
699 scsi_read_write(&csio,
705 /*minimum_cmd_size*/ softc->minimum_cmd_size,
706 ap->a_offset / secsize,
707 ap->a_length / secsize,
708 /*data_ptr*/(u_int8_t *) ap->a_virtual,
709 /*dxfer_len*/ap->a_length,
710 /*sense_len*/SSD_FULL_SIZE,
711 DA_DEFAULT_TIMEOUT * 1000);
712 xpt_polled_action((union ccb *)&csio);
714 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
715 kprintf("Aborting dump due to I/O error.\n");
716 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
717 CAM_SCSI_STATUS_ERROR)
718 scsi_sense_print(&csio);
720 kprintf("status == 0x%x, scsi status == 0x%x\n",
721 csio.ccb_h.status, csio.scsi_status);
724 cam_periph_unlock(periph);
729 * Sync the disk cache contents to the physical media.
731 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
733 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
734 csio.ccb_h.ccb_state = DA_CCB_DUMP;
735 scsi_synchronize_cache(&csio,
739 /*begin_lba*/0,/* Cover the whole disk */
743 xpt_polled_action((union ccb *)&csio);
745 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
746 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
747 CAM_SCSI_STATUS_ERROR) {
749 int sense_key, error_code;
751 scsi_extract_sense(&csio.sense_data,
755 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
756 scsi_sense_print(&csio);
758 xpt_print(periph->path, "Synchronize cache "
759 "failed, status == 0x%x, scsi status == "
760 "0x%x\n", csio.ccb_h.status,
765 cam_periph_unlock(periph);
775 * Create our extend array for storing the devices we attach to.
777 daperiphs = cam_extend_new();
778 if (daperiphs == NULL) {
779 kprintf("da: Failed to alloc extend array!\n");
783 callout_init(&dasendorderedtag_ch);
786 * Install a global async callback. This callback will
787 * receive async callbacks like "new device found".
789 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
791 if (status != CAM_REQ_CMP) {
792 kprintf("da: Failed to attach master async callback "
793 "due to status 0x%x!\n", status);
794 } else if (da_send_ordered) {
796 /* Register our shutdown event handler */
797 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
798 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
799 kprintf("dainit: shutdown event registration failed!\n");
804 daoninvalidate(struct cam_periph *periph)
806 struct da_softc *softc;
808 softc = (struct da_softc *)periph->softc;
811 * De-register any async callbacks.
813 xpt_register_async(0, daasync, periph, periph->path);
815 softc->flags |= DA_FLAG_PACK_INVALID;
818 * Return all queued I/O with ENXIO.
819 * XXX Handle any transactions queued to the card
820 * with XPT_ABORT_CCB.
822 daflushbioq(&softc->bio_queue_wr, ENXIO);
823 daflushbioq(&softc->bio_queue_rd, ENXIO);
824 xpt_print(periph->path, "lost device\n");
828 daflushbioq(struct bio_queue_head *bioq, int error)
833 while ((q_bio = bioq_first(bioq)) != NULL){
834 bioq_remove(bioq, q_bio);
835 q_bp = q_bio->bio_buf;
836 q_bp->b_resid = q_bp->b_bcount;
837 q_bp->b_error = error;
838 q_bp->b_flags |= B_ERROR;
844 dacleanup(struct cam_periph *periph)
846 struct da_softc *softc;
848 softc = (struct da_softc *)periph->softc;
850 devstat_remove_entry(&softc->device_stats);
851 cam_extend_release(daperiphs, periph->unit_number);
852 xpt_print(periph->path, "removing device entry\n");
854 * If we can't free the sysctl tree, oh well...
856 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
857 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
858 xpt_print(periph->path, "can't remove sysctl context\n");
860 periph->softc = NULL;
861 if (softc->disk.d_rawdev) {
862 cam_periph_unlock(periph);
863 disk_destroy(&softc->disk);
864 cam_periph_lock(periph);
867 callout_stop(&softc->sendordered_c);
868 kfree(softc, M_DEVBUF);
872 daasync(void *callback_arg, u_int32_t code,
873 struct cam_path *path, void *arg)
875 struct cam_periph *periph;
877 periph = (struct cam_periph *)callback_arg;
880 case AC_FOUND_DEVICE:
882 struct ccb_getdev *cgd;
886 cgd = (struct ccb_getdev *)arg;
890 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
891 && SID_TYPE(&cgd->inq_data) != T_RBC
892 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
896 * Don't complain if a valid peripheral is already attached.
898 periph = cam_periph_find(cgd->ccb_h.path, "da");
899 if (periph && (periph->flags & CAM_PERIPH_INVALID) == 0)
903 * Allocate a peripheral instance for
904 * this device and start the probe
907 sim = xpt_path_sim(cgd->ccb_h.path);
908 status = cam_periph_alloc(daregister, daoninvalidate,
910 "da", CAM_PERIPH_BIO,
911 cgd->ccb_h.path, daasync,
912 AC_FOUND_DEVICE, cgd);
914 if (status != CAM_REQ_CMP && status != CAM_REQ_INPROG) {
915 kprintf("daasync: Unable to attach to new device "
916 "due to status 0x%x\n", status);
923 struct da_softc *softc;
924 struct ccb_hdr *ccbh;
926 softc = (struct da_softc *)periph->softc;
928 * Don't fail on the expected unit attention
931 softc->flags |= DA_FLAG_RETRY_UA;
932 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
933 ccbh->ccb_state |= DA_CCB_RETRY_UA;
937 cam_periph_async(periph, code, path, arg);
943 dasysctlinit(void *context, int pending)
945 struct cam_periph *periph;
946 struct da_softc *softc;
947 char tmpstr[80], tmpstr2[80];
950 periph = (struct cam_periph *)context;
951 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
956 softc = (struct da_softc *)periph->softc;
957 ksnprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
958 ksnprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
960 sysctl_ctx_init(&softc->sysctl_ctx);
961 softc->flags |= DA_FLAG_SCTX_INIT;
962 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
963 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
964 CTLFLAG_RD, 0, tmpstr);
965 if (softc->sysctl_tree == NULL) {
966 kprintf("dasysctlinit: unable to allocate sysctl tree\n");
967 cam_periph_release(periph);
973 * Now register the sysctl handler, so the user can the value on
976 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
977 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
978 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
981 cam_periph_release(periph);
986 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
990 value = *(int *)arg1;
992 error = sysctl_handle_int(oidp, &value, 0, req);
995 || (req->newptr == NULL))
999 * Acceptable values here are 6, 10 or 12, or 16.
1003 else if ((value > 6)
1006 else if ((value > 10)
1009 else if (value > 12)
1012 *(int *)arg1 = value;
1018 daregister(struct cam_periph *periph, void *arg)
1020 struct da_softc *softc;
1021 struct ccb_pathinq cpi;
1022 struct ccb_getdev *cgd;
1026 cgd = (struct ccb_getdev *)arg;
1027 if (periph == NULL) {
1028 kprintf("daregister: periph was NULL!!\n");
1029 return(CAM_REQ_CMP_ERR);
1033 kprintf("daregister: no getdev CCB, can't register device\n");
1034 return(CAM_REQ_CMP_ERR);
1037 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
1038 LIST_INIT(&softc->pending_ccbs);
1039 softc->state = DA_STATE_PROBE;
1040 bioq_init(&softc->bio_queue_rd);
1041 bioq_init(&softc->bio_queue_wr);
1042 if (SID_IS_REMOVABLE(&cgd->inq_data))
1043 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1044 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1045 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1047 periph->softc = softc;
1049 cam_extend_set(daperiphs, periph->unit_number, periph);
1052 * See if this device has any quirks.
1054 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1055 (caddr_t)da_quirk_table,
1056 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1057 sizeof(*da_quirk_table), scsi_inquiry_match);
1060 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1062 softc->quirks = DA_Q_NONE;
1065 * Unconditionally disable the synchronize cache command for
1066 * usb attachments. It's just impossible to determine if the
1067 * device supports it or not and if it doesn't the port can
1070 if (strncmp(periph->sim->sim_name, "umass", 4) == 0) {
1071 softc->quirks |= DA_Q_NO_SYNC_CACHE;
1074 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1076 /* Check if the SIM does not want 6 byte commands */
1077 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
1078 cpi.ccb_h.func_code = XPT_PATH_INQ;
1079 xpt_action((union ccb *)&cpi);
1080 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1081 softc->quirks |= DA_Q_NO_6_BYTE;
1084 * RBC devices don't have to support READ(6), only READ(10).
1086 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1087 softc->minimum_cmd_size = 10;
1089 softc->minimum_cmd_size = 6;
1092 * Load the user's default, if any.
1094 ksnprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1095 periph->unit_number);
1096 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1099 * 6, 10, 12, and 16 are the currently permissible values.
1101 if (softc->minimum_cmd_size < 6)
1102 softc->minimum_cmd_size = 6;
1103 else if ((softc->minimum_cmd_size > 6)
1104 && (softc->minimum_cmd_size <= 10))
1105 softc->minimum_cmd_size = 10;
1106 else if ((softc->minimum_cmd_size > 10)
1107 && (softc->minimum_cmd_size <= 12))
1108 softc->minimum_cmd_size = 12;
1109 else if (softc->minimum_cmd_size > 12)
1110 softc->minimum_cmd_size = 16;
1113 * The DA driver supports a blocksize, but
1114 * we don't know the blocksize until we do
1115 * a read capacity. So, set a flag to
1116 * indicate that the blocksize is
1117 * unavailable right now. We'll clear the
1118 * flag as soon as we've done a read capacity.
1120 devstat_add_entry(&softc->device_stats, "da",
1121 periph->unit_number, 0,
1122 DEVSTAT_BS_UNAVAILABLE,
1123 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
1124 DEVSTAT_PRIORITY_DISK);
1127 * Register this media as a disk
1129 CAM_SIM_UNLOCK(periph->sim);
1130 disk_create(periph->unit_number, &softc->disk, &da_ops);
1131 softc->disk.d_rawdev->si_iosize_max = MAXPHYS;
1132 CAM_SIM_LOCK(periph->sim);
1135 * Add async callbacks for bus reset and
1136 * bus device reset calls. I don't bother
1137 * checking if this fails as, in most cases,
1138 * the system will function just fine without
1139 * them and the only alternative would be to
1140 * not attach the device on failure.
1142 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
1143 daasync, periph, periph->path);
1146 * Take an exclusive refcount on the periph while dastart is called
1147 * to finish the probe. The reference will be dropped in dadone at
1150 cam_periph_hold(periph, 0);
1151 xpt_schedule(periph, /*priority*/5);
1154 * Schedule a periodic event to occasionally send an
1155 * ordered tag to a device.
1157 callout_init(&softc->sendordered_c);
1158 callout_reset(&softc->sendordered_c,
1159 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
1160 dasendorderedtag, softc);
1162 return(CAM_REQ_CMP);
1166 dastart(struct cam_periph *periph, union ccb *start_ccb)
1168 struct da_softc *softc;
1170 softc = (struct da_softc *)periph->softc;
1172 switch (softc->state) {
1173 case DA_STATE_NORMAL:
1175 /* Pull a buffer from the queue and get going on it */
1184 * See if there is a buf with work for us to do..
1186 bio_rd = bioq_first(&softc->bio_queue_rd);
1187 bio_wr = bioq_first(&softc->bio_queue_wr);
1189 if (periph->immediate_priority <= periph->pinfo.priority) {
1190 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1191 ("queuing for immediate ccb\n"));
1192 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1193 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1195 periph->immediate_priority = CAM_PRIORITY_NONE;
1196 wakeup(&periph->ccb_list);
1197 if (bio_rd || bio_wr) {
1199 * Have more work to do, so ensure we stay
1202 xpt_schedule(periph, /* XXX priority */1);
1208 * Select a read or write buffer to queue. Limit the number
1209 * of tags dedicated to reading or writing, giving reads
1212 * Writes to modern hard drives go into the HDs cache and
1213 * return completion nearly instantly. That is until the
1214 * cache becomes full. When the HDs cache becomes full
1215 * write commands will begin to stall. If all available
1216 * tags are taken up by writes which saturate the drive
1217 * reads will become tag-starved.
1219 * A similar situation can occur with reads. With many
1220 * parallel readers all tags can be taken up by reads
1221 * and prevent any writes from draining, even if the HD's
1222 * cache is not full.
1224 limit = periph->sim->max_tagged_dev_openings * 2 / 3 + 1;
1228 static long savets2;
1229 if (1 || time_second != savets2 || (ticks != savets && (softc->outstanding_cmds_rd || softc->outstanding_cmds_wr))) {
1230 kprintf("%d %d (%d)\n",
1231 softc->outstanding_cmds_rd,
1232 softc->outstanding_cmds_wr,
1235 savets2 = time_second;
1238 if (bio_rd && softc->outstanding_cmds_rd < limit) {
1240 bioq_remove(&softc->bio_queue_rd, bio);
1241 } else if (bio_wr && softc->outstanding_cmds_wr < limit) {
1243 bioq_remove(&softc->bio_queue_wr, bio);
1246 softc->flags |= DA_FLAG_RD_LIMIT;
1248 softc->flags |= DA_FLAG_WR_LIMIT;
1249 xpt_release_ccb(start_ccb);
1254 * We can queue new work.
1258 devstat_start_transaction(&softc->device_stats);
1260 if ((bp->b_flags & B_ORDERED) != 0 ||
1261 (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1262 softc->flags &= ~DA_FLAG_NEED_OTAG;
1263 softc->ordered_tag_count++;
1264 tag_code = MSG_ORDERED_Q_TAG;
1266 tag_code = MSG_SIMPLE_Q_TAG;
1273 * Block read/write op
1275 KKASSERT(bio->bio_offset % softc->params.secsize == 0);
1279 da_retry_count, /* retries */
1282 (bp->b_cmd == BUF_CMD_READ),
1284 softc->minimum_cmd_size,
1285 bio->bio_offset / softc->params.secsize,
1286 bp->b_bcount / softc->params.secsize,
1289 SSD_FULL_SIZE, /* sense_len */
1290 da_default_timeout * 1000
1295 * Silently complete a flush request if the device
1298 if (softc->quirks & DA_Q_NO_SYNC_CACHE) {
1299 xpt_release_ccb(start_ccb);
1301 devstat_end_transaction_buf(
1302 &softc->device_stats, bp);
1305 scsi_synchronize_cache(
1308 dadone, /* cbfcnp */
1311 0, /* count (whole disk) */
1313 da_default_timeout*1000 /* timeout */
1318 xpt_release_ccb(start_ccb);
1320 panic("dastart: unrecognized bio cmd %d", bp->b_cmd);
1321 break; /* NOT REACHED */
1325 * Block out any asyncronous callbacks
1326 * while we touch the pending ccb list.
1329 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1330 LIST_INSERT_HEAD(&softc->pending_ccbs,
1331 &start_ccb->ccb_h, periph_links.le);
1332 if (bp->b_cmd == BUF_CMD_WRITE ||
1333 bp->b_cmd == BUF_CMD_FLUSH) {
1334 ++softc->outstanding_cmds_wr;
1336 ++softc->outstanding_cmds_rd;
1339 /* We expect a unit attention from this device */
1340 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1341 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1342 softc->flags &= ~DA_FLAG_RETRY_UA;
1345 start_ccb->ccb_h.ccb_bio = bio;
1346 xpt_action(start_ccb);
1350 * Be sure we stay scheduled if we have more work to do.
1352 if (bioq_first(&softc->bio_queue_rd) ||
1353 bioq_first(&softc->bio_queue_wr)) {
1354 xpt_schedule(periph, 1);
1358 case DA_STATE_PROBE:
1360 struct ccb_scsiio *csio;
1361 struct scsi_read_capacity_data *rcap;
1363 rcap = kmalloc(sizeof(*rcap), M_SCSIDA, M_INTWAIT | M_ZERO);
1364 csio = &start_ccb->csio;
1365 scsi_read_capacity(csio,
1372 start_ccb->ccb_h.ccb_bio = NULL;
1373 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1374 xpt_action(start_ccb);
1377 case DA_STATE_PROBE2:
1379 struct ccb_scsiio *csio;
1380 struct scsi_read_capacity_data_16 *rcaplong;
1382 rcaplong = kmalloc(sizeof(*rcaplong), M_SCSIDA,
1383 M_INTWAIT | M_ZERO);
1384 if (rcaplong == NULL) {
1385 kprintf("dastart: Couldn't allocate read_capacity\n");
1386 /* da_free_periph??? */
1389 csio = &start_ccb->csio;
1390 scsi_read_capacity_16(csio,
1393 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1398 /*sense_len*/ SSD_FULL_SIZE,
1400 start_ccb->ccb_h.ccb_bio = NULL;
1401 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1402 xpt_action(start_ccb);
1409 cmd6workaround(union ccb *ccb)
1411 struct scsi_rw_6 cmd6;
1412 struct scsi_rw_10 *cmd10;
1413 struct da_softc *softc;
1417 cdb = ccb->csio.cdb_io.cdb_bytes;
1419 /* Translation only possible if CDB is an array and cmd is R/W6 */
1420 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1421 (*cdb != READ_6 && *cdb != WRITE_6))
1424 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
1425 "increasing minimum_cmd_size to 10.\n");
1426 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1427 softc->minimum_cmd_size = 10;
1429 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1430 cmd10 = (struct scsi_rw_10 *)cdb;
1431 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1433 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1434 cmd10->reserved = 0;
1435 scsi_ulto2b(cmd6.length, cmd10->length);
1436 cmd10->control = cmd6.control;
1437 ccb->csio.cdb_len = sizeof(*cmd10);
1439 /* Requeue request, unfreezing queue if necessary */
1440 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1441 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1444 cam_release_devq(ccb->ccb_h.path,
1448 /*getcount_only*/0);
1454 dadone(struct cam_periph *periph, union ccb *done_ccb)
1456 struct da_softc *softc;
1457 struct ccb_scsiio *csio;
1458 struct disk_info info;
1460 softc = (struct da_softc *)periph->softc;
1461 csio = &done_ccb->csio;
1462 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1463 case DA_CCB_BUFFER_IO:
1469 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1471 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1475 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1480 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1481 if (error == ERESTART) {
1483 * A retry was scheuled, so
1489 if (error == ENXIO) {
1491 * Catastrophic error. Mark our pack as
1495 * XXX See if this is really a media
1498 xpt_print(periph->path,
1499 "Invalidating pack\n");
1500 softc->flags |= DA_FLAG_PACK_INVALID;
1504 * Return all queued write I/O's with EIO
1505 * so the client can retry these I/Os in the
1506 * proper order should it attempt to recover.
1508 * Leave read I/O's alone.
1510 daflushbioq(&softc->bio_queue_wr, EIO);
1511 bp->b_error = error;
1512 bp->b_resid = bp->b_bcount;
1513 bp->b_flags |= B_ERROR;
1515 bp->b_resid = csio->resid;
1517 if (bp->b_resid != 0)
1518 bp->b_flags |= B_ERROR;
1520 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1521 cam_release_devq(done_ccb->ccb_h.path,
1525 /*getcount_only*/0);
1527 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1528 panic("REQ_CMP with QFRZN");
1529 bp->b_resid = csio->resid;
1530 if (csio->resid > 0)
1531 bp->b_flags |= B_ERROR;
1535 * Block out any asyncronous callbacks
1536 * while we touch the pending ccb list.
1538 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1539 if (bp->b_cmd == BUF_CMD_WRITE || bp->b_cmd == BUF_CMD_FLUSH) {
1540 --softc->outstanding_cmds_wr;
1541 if (softc->flags & DA_FLAG_WR_LIMIT) {
1542 softc->flags &= ~DA_FLAG_WR_LIMIT;
1546 --softc->outstanding_cmds_rd;
1547 if (softc->flags & DA_FLAG_RD_LIMIT) {
1548 softc->flags &= ~DA_FLAG_RD_LIMIT;
1552 if (softc->outstanding_cmds_rd +
1553 softc->outstanding_cmds_wr == 0) {
1554 softc->flags |= DA_FLAG_WENT_IDLE;
1557 devstat_end_transaction_buf(&softc->device_stats, bp);
1561 xpt_schedule(periph, /*priority*/1);
1568 struct scsi_read_capacity_data *rdcap;
1569 struct scsi_read_capacity_data_16 *rcaplong;
1570 char announce_buf[80];
1574 if (softc->state == DA_STATE_PROBE)
1575 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1577 rcaplong = (struct scsi_read_capacity_data_16 *)
1580 bzero(&info, sizeof(info));
1581 info.d_type = DTYPE_SCSI;
1582 info.d_serialno = xpt_path_serialno(periph->path);
1584 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1585 struct disk_params *dp;
1586 uint32_t block_size;
1589 if (softc->state == DA_STATE_PROBE) {
1590 block_size = scsi_4btoul(rdcap->length);
1591 maxsector = scsi_4btoul(rdcap->addr);
1594 * According to SBC-2, if the standard 10
1595 * byte READ CAPACITY command returns 2^32,
1596 * we should issue the 16 byte version of
1597 * the command, since the device in question
1598 * has more sectors than can be represented
1599 * with the short version of the command.
1601 if (maxsector == 0xffffffff) {
1602 softc->state = DA_STATE_PROBE2;
1603 kfree(rdcap, M_SCSIDA);
1604 xpt_release_ccb(done_ccb);
1605 xpt_schedule(periph, /*priority*/5);
1609 block_size = scsi_4btoul(rcaplong->length);
1610 maxsector = scsi_8btou64(rcaplong->addr);
1612 dasetgeom(periph, block_size, maxsector);
1613 dp = &softc->params;
1614 ksnprintf(announce_buf, sizeof(announce_buf),
1615 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1616 (uintmax_t) (((uintmax_t)dp->secsize *
1617 dp->sectors) / (1024*1024)),
1618 (uintmax_t)dp->sectors,
1619 dp->secsize, dp->heads, dp->secs_per_track,
1621 CAM_SIM_UNLOCK(periph->sim);
1622 info.d_media_blksize = softc->params.secsize;
1623 info.d_media_blocks = softc->params.sectors;
1624 info.d_media_size = 0;
1625 info.d_secpertrack = softc->params.secs_per_track;
1626 info.d_nheads = softc->params.heads;
1627 info.d_ncylinders = softc->params.cylinders;
1628 info.d_secpercyl = softc->params.heads *
1629 softc->params.secs_per_track;
1630 info.d_serialno = xpt_path_serialno(periph->path);
1631 disk_setdiskinfo(&softc->disk, &info);
1632 CAM_SIM_LOCK(periph->sim);
1636 announce_buf[0] = '\0';
1639 * Retry any UNIT ATTENTION type errors. They
1640 * are expected at boot.
1642 error = daerror(done_ccb, CAM_RETRY_SELTO,
1643 SF_RETRY_UA|SF_NO_PRINT);
1644 if (error == ERESTART) {
1646 * A retry was scheuled, so
1650 } else if (error != 0) {
1651 struct scsi_sense_data *sense;
1653 int sense_key, error_code;
1656 struct ccb_getdev cgd;
1658 /* Don't wedge this device's queue */
1659 status = done_ccb->ccb_h.status;
1660 if ((status & CAM_DEV_QFRZN) != 0)
1661 cam_release_devq(done_ccb->ccb_h.path,
1665 /*getcount_only*/0);
1668 xpt_setup_ccb(&cgd.ccb_h,
1669 done_ccb->ccb_h.path,
1671 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1672 xpt_action((union ccb *)&cgd);
1674 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1675 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1676 || ((status & CAM_AUTOSNS_VALID) == 0))
1682 sense = &csio->sense_data;
1683 scsi_extract_sense(sense, &error_code,
1688 * Attach to anything that claims to be a
1689 * direct access or optical disk device,
1690 * as long as it doesn't return a "Logical
1691 * unit not supported" (0x25) error.
1693 if ((have_sense) && (asc != 0x25)
1694 && (error_code == SSD_CURRENT_ERROR)) {
1695 const char *sense_key_desc;
1696 const char *asc_desc;
1698 scsi_sense_desc(sense_key, asc, ascq,
1702 ksnprintf(announce_buf,
1703 sizeof(announce_buf),
1704 "Attempt to query device "
1705 "size failed: %s, %s",
1708 info.d_media_blksize = 512;
1709 disk_setdiskinfo(&softc->disk, &info);
1715 xpt_print(periph->path,
1716 "got CAM status %#x\n",
1717 done_ccb->ccb_h.status);
1720 xpt_print(periph->path, "fatal error, "
1721 "failed to attach to device\n");
1724 * Free up resources.
1726 cam_periph_invalidate(periph);
1730 kfree(csio->data_ptr, M_SCSIDA);
1731 if (announce_buf[0] != '\0') {
1732 xpt_announce_periph(periph, announce_buf);
1734 * Create our sysctl variables, now that we know
1735 * we have successfully attached.
1737 taskqueue_enqueue(taskqueue_thread[mycpuid],
1738 &softc->sysctl_task);
1740 softc->state = DA_STATE_NORMAL;
1742 * Since our peripheral may be invalidated by an error
1743 * above or an external event, we must release our CCB
1744 * before releasing the probe lock on the peripheral.
1745 * The peripheral will only go away once the last lock
1746 * is removed, and we need it around for the CCB release
1749 xpt_release_ccb(done_ccb);
1750 cam_periph_unhold(periph, 0);
1753 case DA_CCB_WAITING:
1755 /* Caller will release the CCB */
1756 wakeup(&done_ccb->ccb_h.cbfcnp);
1760 /* No-op. We're polling */
1765 xpt_release_ccb(done_ccb);
1769 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1771 struct da_softc *softc;
1772 struct cam_periph *periph;
1775 periph = xpt_path_periph(ccb->ccb_h.path);
1776 softc = (struct da_softc *)periph->softc;
1779 * Automatically detect devices that do not support
1780 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
1783 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
1784 error = cmd6workaround(ccb);
1785 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
1786 CAM_SCSI_STATUS_ERROR)
1787 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
1788 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1789 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
1790 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
1791 int sense_key, error_code, asc, ascq;
1793 scsi_extract_sense(&ccb->csio.sense_data,
1794 &error_code, &sense_key, &asc, &ascq);
1795 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
1796 error = cmd6workaround(ccb);
1798 if (error == ERESTART)
1803 * Until we have a better way of doing pack validation,
1804 * don't treat UAs as errors.
1806 sense_flags |= SF_RETRY_UA;
1807 return(cam_periph_error(ccb, cam_flags, sense_flags,
1808 &softc->saved_ccb));
1812 daprevent(struct cam_periph *periph, int action)
1814 struct da_softc *softc;
1818 softc = (struct da_softc *)periph->softc;
1820 if (((action == PR_ALLOW)
1821 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
1822 || ((action == PR_PREVENT)
1823 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
1827 ccb = cam_periph_getccb(periph, /*priority*/1);
1829 scsi_prevent(&ccb->csio,
1837 error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
1838 SF_RETRY_UA, &softc->device_stats);
1841 if (action == PR_ALLOW)
1842 softc->flags &= ~DA_FLAG_PACK_LOCKED;
1844 softc->flags |= DA_FLAG_PACK_LOCKED;
1847 xpt_release_ccb(ccb);
1851 * Check media on open, e.g. card reader devices which had no initial media.
1854 dacheckmedia(struct cam_periph *periph)
1856 struct disk_params *dp;
1857 struct da_softc *softc;
1858 struct disk_info info;
1861 softc = (struct da_softc *)periph->softc;
1862 dp = &softc->params;
1864 error = dagetcapacity(periph);
1867 * Only reprobe on initial open and if the media is removable.
1869 * NOTE: If we setdiskinfo() it will take the device probe
1870 * a bit of time to probe the slices and partitions,
1871 * and mess up booting. So avoid if nothing has changed.
1874 if (softc->flags & DA_FLAG_OPEN)
1876 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) == 0)
1879 bzero(&info, sizeof(info));
1880 info.d_type = DTYPE_SCSI;
1881 info.d_serialno = xpt_path_serialno(periph->path);
1884 CAM_SIM_UNLOCK(periph->sim);
1885 info.d_media_blksize = softc->params.secsize;
1886 info.d_media_blocks = softc->params.sectors;
1887 info.d_media_size = 0;
1888 info.d_secpertrack = softc->params.secs_per_track;
1889 info.d_nheads = softc->params.heads;
1890 info.d_ncylinders = softc->params.cylinders;
1891 info.d_secpercyl = softc->params.heads *
1892 softc->params.secs_per_track;
1893 info.d_serialno = xpt_path_serialno(periph->path);
1894 if (info.d_media_blocks != softc->disk.d_info.d_media_blocks) {
1895 kprintf("%s%d: open removable media: "
1896 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)\n",
1897 periph->periph_name, periph->unit_number,
1898 (uintmax_t)(((uintmax_t)dp->secsize *
1899 dp->sectors) / (1024*1024)),
1900 (uintmax_t)dp->sectors, dp->secsize,
1901 dp->heads, dp->secs_per_track, dp->cylinders);
1902 disk_setdiskinfo(&softc->disk, &info);
1904 CAM_SIM_LOCK(periph->sim);
1906 kprintf("%s%d: open removable media: no media present\n",
1907 periph->periph_name, periph->unit_number);
1908 info.d_media_blksize = 512;
1909 disk_setdiskinfo(&softc->disk, &info);
1915 dagetcapacity(struct cam_periph *periph)
1917 struct da_softc *softc;
1919 struct scsi_read_capacity_data *rcap;
1920 struct scsi_read_capacity_data_16 *rcaplong;
1925 softc = (struct da_softc *)periph->softc;
1930 /* Do a read capacity */
1931 rcap = (struct scsi_read_capacity_data *)kmalloc(sizeof(*rcaplong),
1932 M_SCSIDA, M_INTWAIT);
1934 ccb = cam_periph_getccb(periph, /*priority*/1);
1935 scsi_read_capacity(&ccb->csio,
1942 ccb->ccb_h.ccb_bio = NULL;
1944 error = cam_periph_runccb(ccb, daerror,
1945 /*cam_flags*/CAM_RETRY_SELTO,
1946 /*sense_flags*/SF_RETRY_UA,
1947 &softc->device_stats);
1949 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1950 cam_release_devq(ccb->ccb_h.path,
1954 /*getcount_only*/0);
1957 block_len = scsi_4btoul(rcap->length);
1958 maxsector = scsi_4btoul(rcap->addr);
1960 if (maxsector != 0xffffffff)
1965 rcaplong = (struct scsi_read_capacity_data_16 *)rcap;
1967 scsi_read_capacity_16(&ccb->csio,
1970 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1975 /*sense_len*/ SSD_FULL_SIZE,
1977 ccb->ccb_h.ccb_bio = NULL;
1979 error = cam_periph_runccb(ccb, daerror,
1980 /*cam_flags*/CAM_RETRY_SELTO,
1981 /*sense_flags*/SF_RETRY_UA,
1982 &softc->device_stats);
1984 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1985 cam_release_devq(ccb->ccb_h.path,
1989 /*getcount_only*/0);
1992 block_len = scsi_4btoul(rcaplong->length);
1993 maxsector = scsi_8btou64(rcaplong->addr);
1999 dasetgeom(periph, block_len, maxsector);
2001 xpt_release_ccb(ccb);
2003 kfree(rcap, M_SCSIDA);
2009 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector)
2011 struct ccb_calc_geometry ccg;
2012 struct da_softc *softc;
2013 struct disk_params *dp;
2015 softc = (struct da_softc *)periph->softc;
2017 dp = &softc->params;
2018 dp->secsize = block_len;
2019 dp->sectors = maxsector + 1;
2021 * Have the controller provide us with a geometry
2022 * for this disk. The only time the geometry
2023 * matters is when we boot and the controller
2024 * is the only one knowledgeable enough to come
2025 * up with something that will make this a bootable
2028 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
2029 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
2030 ccg.block_size = dp->secsize;
2031 ccg.volume_size = dp->sectors;
2033 ccg.secs_per_track = 0;
2035 xpt_action((union ccb*)&ccg);
2036 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2038 * We don't know what went wrong here- but just pick
2039 * a geometry so we don't have nasty things like divide
2043 dp->secs_per_track = 255;
2044 dp->cylinders = dp->sectors / (255 * 255);
2045 if (dp->cylinders == 0) {
2049 dp->heads = ccg.heads;
2050 dp->secs_per_track = ccg.secs_per_track;
2051 dp->cylinders = ccg.cylinders;
2056 dasendorderedtag(void *arg)
2058 struct da_softc *softc = arg;
2060 if (da_send_ordered) {
2061 if ((softc->ordered_tag_count == 0)
2062 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
2063 softc->flags |= DA_FLAG_NEED_OTAG;
2065 if (softc->outstanding_cmds_rd || softc->outstanding_cmds_wr)
2066 softc->flags &= ~DA_FLAG_WENT_IDLE;
2068 softc->ordered_tag_count = 0;
2070 /* Queue us up again */
2071 callout_reset(&softc->sendordered_c,
2072 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
2073 dasendorderedtag, softc);
2077 * Step through all DA peripheral drivers, and if the device is still open,
2078 * sync the disk cache to physical media.
2081 dashutdown(void * arg, int howto)
2083 struct cam_periph *periph;
2084 struct da_softc *softc;
2086 TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
2089 cam_periph_lock(periph);
2090 softc = (struct da_softc *)periph->softc;
2093 * We only sync the cache if the drive is still open, and
2094 * if the drive is capable of it..
2096 if (((softc->flags & DA_FLAG_OPEN) == 0)
2097 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
2098 cam_periph_unlock(periph);
2102 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
2104 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
2105 scsi_synchronize_cache(&ccb.csio,
2109 /*begin_lba*/0, /* whole disk */
2114 xpt_polled_action(&ccb);
2116 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2117 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2118 CAM_SCSI_STATUS_ERROR)
2119 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
2120 int error_code, sense_key, asc, ascq;
2122 scsi_extract_sense(&ccb.csio.sense_data,
2123 &error_code, &sense_key,
2126 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
2127 scsi_sense_print(&ccb.csio);
2129 xpt_print(periph->path, "Synchronize "
2130 "cache failed, status == 0x%x, scsi status "
2131 "== 0x%x\n", ccb.ccb_h.status,
2132 ccb.csio.scsi_status);
2136 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
2137 cam_release_devq(ccb.ccb_h.path,
2141 /*getcount_only*/0);
2143 cam_periph_unlock(periph);
2147 #else /* !_KERNEL */
2150 * XXX This is only left out of the kernel build to silence warnings. If,
2151 * for some reason this function is used in the kernel, the ifdefs should
2152 * be moved so it is included both in the kernel and userland.
2155 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2156 void (*cbfcnp)(struct cam_periph *, union ccb *),
2157 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2158 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2161 struct scsi_format_unit *scsi_cmd;
2163 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2164 scsi_cmd->opcode = FORMAT_UNIT;
2165 scsi_cmd->byte2 = byte2;
2166 scsi_ulto2b(ileave, scsi_cmd->interleave);
2171 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2180 #endif /* _KERNEL */