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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
39 #include <sys/taskqueue.h>
42 #include <sys/devicestat.h>
44 #include <sys/eventhandler.h>
45 #include <sys/malloc.h>
48 #include <machine/md_var.h>
53 #include <geom/geom_disk.h>
61 #include <cam/cam_ccb.h>
62 #include <cam/cam_periph.h>
63 #include <cam/cam_xpt_periph.h>
65 #include <cam/scsi/scsi_message.h>
68 #include <cam/scsi/scsi_da.h>
79 DA_FLAG_PACK_INVALID = 0x001,
80 DA_FLAG_NEW_PACK = 0x002,
81 DA_FLAG_PACK_LOCKED = 0x004,
82 DA_FLAG_PACK_REMOVABLE = 0x008,
83 DA_FLAG_TAGGED_QUEUING = 0x010,
84 DA_FLAG_NEED_OTAG = 0x020,
85 DA_FLAG_WENT_IDLE = 0x040,
86 DA_FLAG_RETRY_UA = 0x080,
88 DA_FLAG_SCTX_INIT = 0x200
93 DA_Q_NO_SYNC_CACHE = 0x01,
94 DA_Q_NO_6_BYTE = 0x02,
95 DA_Q_NO_PREVENT = 0x04
100 DA_CCB_PROBE2 = 0x02,
101 DA_CCB_BUFFER_IO = 0x03,
102 DA_CCB_WAITING = 0x04,
104 DA_CCB_TYPE_MASK = 0x0F,
105 DA_CCB_RETRY_UA = 0x10
108 /* Offsets into our private area for storing information */
109 #define ccb_state ppriv_field0
110 #define ccb_bp ppriv_ptr1
115 u_int8_t secs_per_track;
116 u_int32_t secsize; /* Number of bytes/sector */
117 u_int64_t sectors; /* total number sectors */
121 struct bio_queue_head bio_queue;
122 SLIST_ENTRY(da_softc) links;
123 LIST_HEAD(, ccb_hdr) pending_ccbs;
127 int minimum_cmd_size;
128 int ordered_tag_count;
129 int outstanding_cmds;
130 struct disk_params params;
133 struct task sysctl_task;
134 struct sysctl_ctx_list sysctl_ctx;
135 struct sysctl_oid *sysctl_tree;
138 struct da_quirk_entry {
139 struct scsi_inquiry_pattern inq_pat;
143 static const char quantum[] = "QUANTUM";
144 static const char microp[] = "MICROP";
146 static struct da_quirk_entry da_quirk_table[] =
148 /* SPI, FC devices */
151 * Fujitsu M2513A MO drives.
152 * Tested devices: M2513A2 firmware versions 1200 & 1300.
153 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
154 * Reported by: W.Scholten <whs@xs4all.nl>
156 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
157 /*quirks*/ DA_Q_NO_SYNC_CACHE
161 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
162 /*quirks*/ DA_Q_NO_SYNC_CACHE
166 * This particular Fujitsu drive doesn't like the
167 * synchronize cache command.
168 * Reported by: Tom Jackson <toj@gorilla.net>
170 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
171 /*quirks*/ DA_Q_NO_SYNC_CACHE
176 * This drive doesn't like the synchronize cache command
177 * either. Reported by: Matthew Jacob <mjacob@feral.com>
178 * in NetBSD PR kern/6027, August 24, 1998.
180 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
181 /*quirks*/ DA_Q_NO_SYNC_CACHE
185 * This drive doesn't like the synchronize cache command
186 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
189 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
190 /*quirks*/ DA_Q_NO_SYNC_CACHE
194 * Doesn't like the synchronize cache command.
195 * Reported by: Blaz Zupan <blaz@gold.amis.net>
197 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
198 /*quirks*/ DA_Q_NO_SYNC_CACHE
202 * Doesn't like the synchronize cache command.
203 * Reported by: Blaz Zupan <blaz@gold.amis.net>
205 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
206 /*quirks*/ DA_Q_NO_SYNC_CACHE
210 * Doesn't like the synchronize cache command.
212 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
213 /*quirks*/ DA_Q_NO_SYNC_CACHE
217 * Doesn't like the synchronize cache command.
218 * Reported by: walter@pelissero.de
220 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
221 /*quirks*/ DA_Q_NO_SYNC_CACHE
225 * Doesn't work correctly with 6 byte reads/writes.
226 * Returns illegal request, and points to byte 9 of the
228 * Reported by: Adam McDougall <bsdx@spawnet.com>
230 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
231 /*quirks*/ DA_Q_NO_6_BYTE
235 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
236 /*quirks*/ DA_Q_NO_6_BYTE
240 * Doesn't like the synchronize cache command.
241 * Reported by: walter@pelissero.de
243 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
244 /*quirks*/ DA_Q_NO_SYNC_CACHE
248 * The CISS RAID controllers do not support SYNC_CACHE
250 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
251 /*quirks*/ DA_Q_NO_SYNC_CACHE
253 /* USB mass storage devices supported by umass(4) */
256 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
259 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
260 /*quirks*/ DA_Q_NO_SYNC_CACHE
264 * Power Quotient Int. (PQI) USB flash key
267 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
268 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
272 * Creative Nomad MUVO mp3 player (USB)
275 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
276 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
280 * Jungsoft NEXDISK USB flash key
283 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
284 /*quirks*/ DA_Q_NO_SYNC_CACHE
288 * FreeDik USB Mini Data Drive
291 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
292 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
296 * Sigmatel USB Flash MP3 Player
299 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
300 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
304 * Neuros USB Digital Audio Computer
307 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
308 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
312 * SEAGRAND NP-900 MP3 Player
315 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
316 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
320 * iRiver iFP MP3 player (with UMS Firmware)
321 * PR: kern/54881, i386/63941, kern/66124
323 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
324 /*quirks*/ DA_Q_NO_SYNC_CACHE
328 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
331 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
332 /*quirks*/ DA_Q_NO_SYNC_CACHE
336 * ZICPlay USB MP3 Player with FM
339 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
340 /*quirks*/ DA_Q_NO_SYNC_CACHE
344 * TEAC USB floppy mechanisms
346 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
347 /*quirks*/ DA_Q_NO_SYNC_CACHE
351 * Kingston DataTraveler II+ USB Pen-Drive.
352 * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
354 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
355 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
359 * Motorola E398 Mobile Phone (TransFlash memory card).
360 * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
363 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
364 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
368 * Qware BeatZkey! Pro
371 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
372 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
376 * Time DPA20B 1GB MP3 Player
379 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
380 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
384 * Samsung USB key 128Mb
387 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
388 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
392 * Kingston DataTraveler 2.0 USB Flash memory.
395 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
396 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
400 * Creative MUVO Slim mp3 player (USB)
403 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
404 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
408 * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
411 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
412 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
416 * SanDisk Micro Cruzer 128MB
419 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
420 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
424 * TOSHIBA TransMemory USB sticks
427 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
428 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
433 * PR: usb/75578, usb/72344, usb/65436
435 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
436 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
440 * Genesys 6-in-1 Card Reader
443 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
444 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
448 static disk_strategy_t dastrategy;
449 static dumper_t dadump;
450 static periph_init_t dainit;
451 static void daasync(void *callback_arg, u_int32_t code,
452 struct cam_path *path, void *arg);
453 static void dasysctlinit(void *context, int pending);
454 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
455 static periph_ctor_t daregister;
456 static periph_dtor_t dacleanup;
457 static periph_start_t dastart;
458 static periph_oninv_t daoninvalidate;
459 static void dadone(struct cam_periph *periph,
460 union ccb *done_ccb);
461 static int daerror(union ccb *ccb, u_int32_t cam_flags,
462 u_int32_t sense_flags);
463 static void daprevent(struct cam_periph *periph, int action);
464 static int dagetcapacity(struct cam_periph *periph);
465 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
467 static timeout_t dasendorderedtag;
468 static void dashutdown(void *arg, int howto);
470 #ifndef DA_DEFAULT_TIMEOUT
471 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
474 #ifndef DA_DEFAULT_RETRY
475 #define DA_DEFAULT_RETRY 4
478 static int da_retry_count = DA_DEFAULT_RETRY;
479 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
481 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
482 "CAM Direct Access Disk driver");
483 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
484 &da_retry_count, 0, "Normal I/O retry count");
485 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
486 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
487 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
488 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
491 * DA_ORDEREDTAG_INTERVAL determines how often, relative
492 * to the default timeout, we check to see whether an ordered
493 * tagged transaction is appropriate to prevent simple tag
494 * starvation. Since we'd like to ensure that there is at least
495 * 1/2 of the timeout length left for a starved transaction to
496 * complete after we've sent an ordered tag, we must poll at least
497 * four times in every timeout period. This takes care of the worst
498 * case where a starved transaction starts during an interval that
499 * meets the requirement "don't send an ordered tag" test so it takes
500 * us two intervals to determine that a tag must be sent.
502 #ifndef DA_ORDEREDTAG_INTERVAL
503 #define DA_ORDEREDTAG_INTERVAL 4
506 static struct periph_driver dadriver =
509 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
512 PERIPHDRIVER_DECLARE(da, dadriver);
514 static SLIST_HEAD(,da_softc) softc_list;
517 daopen(struct disk *dp)
519 struct cam_periph *periph;
520 struct da_softc *softc;
526 periph = (struct cam_periph *)dp->d_drv1;
527 if (periph == NULL) {
531 unit = periph->unit_number;
533 softc = (struct da_softc *)periph->softc;
535 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
536 ("daopen: disk=%s%d (unit %d)\n", dp->d_name, dp->d_unit,
539 if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0)
540 return (error); /* error code from tsleep */
542 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
544 softc->flags |= DA_FLAG_OPEN;
546 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
547 /* Invalidate our pack information. */
548 softc->flags &= ~DA_FLAG_PACK_INVALID;
552 error = dagetcapacity(periph);
556 softc->disk->d_sectorsize = softc->params.secsize;
557 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
558 /* XXX: these are not actually "firmware" values, so they may be wrong */
559 softc->disk->d_fwsectors = softc->params.secs_per_track;
560 softc->disk->d_fwheads = softc->params.heads;
561 softc->disk->d_devstat->block_size = softc->params.secsize;
562 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
566 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
567 (softc->quirks & DA_Q_NO_PREVENT) == 0)
568 daprevent(periph, PR_PREVENT);
570 softc->flags &= ~DA_FLAG_OPEN;
571 cam_periph_release(periph);
573 cam_periph_unlock(periph);
578 daclose(struct disk *dp)
580 struct cam_periph *periph;
581 struct da_softc *softc;
584 periph = (struct cam_periph *)dp->d_drv1;
588 softc = (struct da_softc *)periph->softc;
590 if ((error = cam_periph_lock(periph, PRIBIO)) != 0) {
591 return (error); /* error code from tsleep */
594 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
597 ccb = cam_periph_getccb(periph, /*priority*/1);
599 scsi_synchronize_cache(&ccb->csio,
603 /*begin_lba*/0,/* Cover the whole disk */
608 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
609 /*sense_flags*/SF_RETRY_UA,
610 softc->disk->d_devstat);
612 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
613 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
614 CAM_SCSI_STATUS_ERROR) {
616 int sense_key, error_code;
618 scsi_extract_sense(&ccb->csio.sense_data,
622 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
623 scsi_sense_print(&ccb->csio);
625 xpt_print_path(periph->path);
626 printf("Synchronize cache failed, status "
627 "== 0x%x, scsi status == 0x%x\n",
628 ccb->csio.ccb_h.status,
629 ccb->csio.scsi_status);
633 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
634 cam_release_devq(ccb->ccb_h.path,
640 xpt_release_ccb(ccb);
644 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
645 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
646 daprevent(periph, PR_ALLOW);
648 * If we've got removeable media, mark the blocksize as
649 * unavailable, since it could change when new media is
652 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
655 softc->flags &= ~DA_FLAG_OPEN;
656 cam_periph_unlock(periph);
657 cam_periph_release(periph);
662 * Actually translate the requested transfer into one the physical driver
663 * can understand. The transfer is described by a buf and will include
664 * only one physical transfer.
667 dastrategy(struct bio *bp)
669 struct cam_periph *periph;
670 struct da_softc *softc;
673 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
674 if (periph == NULL) {
675 biofinish(bp, NULL, ENXIO);
678 softc = (struct da_softc *)periph->softc;
681 * check it's not too big a transfer for our adapter
683 scsi_minphys(bp,&sd_switch);
687 * Mask interrupts so that the pack cannot be invalidated until
688 * after we are in the queue. Otherwise, we might not properly
689 * clean up one of the buffers.
694 * If the device has been made invalid, error out
696 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
698 biofinish(bp, NULL, ENXIO);
703 * Place it in the queue of disk activities for this disk
705 bioq_disksort(&softc->bio_queue, bp);
710 * Schedule ourselves for performing the work.
712 xpt_schedule(periph, /* XXX priority */1);
718 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
720 struct cam_periph *periph;
721 struct da_softc *softc;
723 struct ccb_scsiio csio;
730 softc = (struct da_softc *)periph->softc;
731 secsize = softc->params.secsize;
733 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0)
737 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
738 csio.ccb_h.ccb_state = DA_CCB_DUMP;
739 scsi_read_write(&csio,
745 /*minimum_cmd_size*/ softc->minimum_cmd_size,
748 /*data_ptr*/(u_int8_t *) virtual,
750 /*sense_len*/SSD_FULL_SIZE,
751 DA_DEFAULT_TIMEOUT * 1000);
752 xpt_polled_action((union ccb *)&csio);
754 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
755 printf("Aborting dump due to I/O error.\n");
756 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
757 CAM_SCSI_STATUS_ERROR)
758 scsi_sense_print(&csio);
760 printf("status == 0x%x, scsi status == 0x%x\n",
761 csio.ccb_h.status, csio.scsi_status);
768 * Sync the disk cache contents to the physical media.
770 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
772 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
773 csio.ccb_h.ccb_state = DA_CCB_DUMP;
774 scsi_synchronize_cache(&csio,
778 /*begin_lba*/0,/* Cover the whole disk */
782 xpt_polled_action((union ccb *)&csio);
784 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
785 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
786 CAM_SCSI_STATUS_ERROR) {
788 int sense_key, error_code;
790 scsi_extract_sense(&csio.sense_data,
794 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
795 scsi_sense_print(&csio);
797 xpt_print_path(periph->path);
798 printf("Synchronize cache failed, status "
799 "== 0x%x, scsi status == 0x%x\n",
800 csio.ccb_h.status, csio.scsi_status);
811 struct cam_path *path;
813 SLIST_INIT(&softc_list);
816 * Install a global async callback. This callback will
817 * receive async callbacks like "new device found".
819 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
820 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
822 if (status == CAM_REQ_CMP) {
823 struct ccb_setasync csa;
825 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
826 csa.ccb_h.func_code = XPT_SASYNC_CB;
827 csa.event_enable = AC_FOUND_DEVICE;
828 csa.callback = daasync;
829 csa.callback_arg = NULL;
830 xpt_action((union ccb *)&csa);
831 status = csa.ccb_h.status;
835 if (status != CAM_REQ_CMP) {
836 printf("da: Failed to attach master async callback "
837 "due to status 0x%x!\n", status);
841 * Schedule a periodic event to occasionally send an
842 * ordered tag to a device.
844 timeout(dasendorderedtag, NULL,
845 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL);
847 /* Register our shutdown event handler */
848 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
849 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
850 printf("dainit: shutdown event registration failed!\n");
855 daoninvalidate(struct cam_periph *periph)
858 struct da_softc *softc;
859 struct ccb_setasync csa;
861 softc = (struct da_softc *)periph->softc;
864 * De-register any async callbacks.
866 xpt_setup_ccb(&csa.ccb_h, periph->path,
868 csa.ccb_h.func_code = XPT_SASYNC_CB;
869 csa.event_enable = 0;
870 csa.callback = daasync;
871 csa.callback_arg = periph;
872 xpt_action((union ccb *)&csa);
874 softc->flags |= DA_FLAG_PACK_INVALID;
877 * Although the oninvalidate() routines are always called at
878 * splsoftcam, we need to be at splbio() here to keep the buffer
879 * queue from being modified while we traverse it.
884 * Return all queued I/O with ENXIO.
885 * XXX Handle any transactions queued to the card
886 * with XPT_ABORT_CCB.
888 bioq_flush(&softc->bio_queue, NULL, ENXIO);
891 SLIST_REMOVE(&softc_list, softc, da_softc, links);
893 disk_gone(softc->disk);
894 xpt_print_path(periph->path);
895 printf("lost device\n");
899 dacleanup(struct cam_periph *periph)
901 struct da_softc *softc;
903 softc = (struct da_softc *)periph->softc;
905 xpt_print_path(periph->path);
906 printf("removing device entry\n");
908 * If we can't free the sysctl tree, oh well...
910 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
911 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
912 xpt_print_path(periph->path);
913 printf("can't remove sysctl context\n");
915 disk_destroy(softc->disk);
916 free(softc, M_DEVBUF);
920 daasync(void *callback_arg, u_int32_t code,
921 struct cam_path *path, void *arg)
923 struct cam_periph *periph;
925 periph = (struct cam_periph *)callback_arg;
927 case AC_FOUND_DEVICE:
929 struct ccb_getdev *cgd;
932 cgd = (struct ccb_getdev *)arg;
936 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
937 && SID_TYPE(&cgd->inq_data) != T_RBC
938 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
942 * Allocate a peripheral instance for
943 * this device and start the probe
946 status = cam_periph_alloc(daregister, daoninvalidate,
948 "da", CAM_PERIPH_BIO,
949 cgd->ccb_h.path, daasync,
950 AC_FOUND_DEVICE, cgd);
952 if (status != CAM_REQ_CMP
953 && status != CAM_REQ_INPROG)
954 printf("daasync: Unable to attach to new device "
955 "due to status 0x%x\n", status);
961 struct da_softc *softc;
962 struct ccb_hdr *ccbh;
965 softc = (struct da_softc *)periph->softc;
968 * Don't fail on the expected unit attention
971 softc->flags |= DA_FLAG_RETRY_UA;
972 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
973 ccbh->ccb_state |= DA_CCB_RETRY_UA;
978 cam_periph_async(periph, code, path, arg);
984 dasysctlinit(void *context, int pending)
986 struct cam_periph *periph;
987 struct da_softc *softc;
988 char tmpstr[80], tmpstr2[80];
990 periph = (struct cam_periph *)context;
991 softc = (struct da_softc *)periph->softc;
993 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
994 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
997 sysctl_ctx_init(&softc->sysctl_ctx);
998 softc->flags |= DA_FLAG_SCTX_INIT;
999 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1000 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1001 CTLFLAG_RD, 0, tmpstr);
1002 if (softc->sysctl_tree == NULL) {
1003 printf("dasysctlinit: unable to allocate sysctl tree\n");
1008 * Now register the sysctl handler, so the user can the value on
1011 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1012 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1013 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1014 "Minimum CDB size");
1020 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1024 value = *(int *)arg1;
1026 error = sysctl_handle_int(oidp, &value, 0, req);
1029 || (req->newptr == NULL))
1033 * Acceptable values here are 6, 10, 12 or 16.
1037 else if ((value > 6)
1040 else if ((value > 10)
1043 else if (value > 12)
1046 *(int *)arg1 = value;
1052 daregister(struct cam_periph *periph, void *arg)
1055 struct da_softc *softc;
1056 struct ccb_setasync csa;
1057 struct ccb_pathinq cpi;
1058 struct ccb_getdev *cgd;
1062 cgd = (struct ccb_getdev *)arg;
1063 if (periph == NULL) {
1064 printf("daregister: periph was NULL!!\n");
1065 return(CAM_REQ_CMP_ERR);
1069 printf("daregister: no getdev CCB, can't register device\n");
1070 return(CAM_REQ_CMP_ERR);
1073 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
1076 if (softc == NULL) {
1077 printf("daregister: Unable to probe new device. "
1078 "Unable to allocate softc\n");
1079 return(CAM_REQ_CMP_ERR);
1082 LIST_INIT(&softc->pending_ccbs);
1083 softc->state = DA_STATE_PROBE;
1084 bioq_init(&softc->bio_queue);
1085 if (SID_IS_REMOVABLE(&cgd->inq_data))
1086 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1087 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1088 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1090 periph->softc = softc;
1093 * See if this device has any quirks.
1095 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1096 (caddr_t)da_quirk_table,
1097 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1098 sizeof(*da_quirk_table), scsi_inquiry_match);
1101 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1103 softc->quirks = DA_Q_NONE;
1105 /* Check if the SIM does not want 6 byte commands */
1106 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
1107 cpi.ccb_h.func_code = XPT_PATH_INQ;
1108 xpt_action((union ccb *)&cpi);
1109 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1110 softc->quirks |= DA_Q_NO_6_BYTE;
1112 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1115 * RBC devices don't have to support READ(6), only READ(10).
1117 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1118 softc->minimum_cmd_size = 10;
1120 softc->minimum_cmd_size = 6;
1123 * Load the user's default, if any.
1125 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1126 periph->unit_number);
1127 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1130 * 6, 10, 12 and 16 are the currently permissible values.
1132 if (softc->minimum_cmd_size < 6)
1133 softc->minimum_cmd_size = 6;
1134 else if ((softc->minimum_cmd_size > 6)
1135 && (softc->minimum_cmd_size <= 10))
1136 softc->minimum_cmd_size = 10;
1137 else if ((softc->minimum_cmd_size > 10)
1138 && (softc->minimum_cmd_size <= 12))
1139 softc->minimum_cmd_size = 12;
1140 else if (softc->minimum_cmd_size > 12)
1141 softc->minimum_cmd_size = 16;
1144 * Block our timeout handler while we
1145 * add this softc to the dev list.
1148 SLIST_INSERT_HEAD(&softc_list, softc, links);
1152 * Register this media as a disk
1155 softc->disk = disk_alloc();
1156 softc->disk->d_open = daopen;
1157 softc->disk->d_close = daclose;
1158 softc->disk->d_strategy = dastrategy;
1159 softc->disk->d_dump = dadump;
1160 softc->disk->d_name = "da";
1161 softc->disk->d_drv1 = periph;
1162 softc->disk->d_maxsize = DFLTPHYS; /* XXX: probably not arbitrary */
1163 softc->disk->d_unit = periph->unit_number;
1164 softc->disk->d_flags = DISKFLAG_NEEDSGIANT;
1165 disk_create(softc->disk, DISK_VERSION);
1168 * Add async callbacks for bus reset and
1169 * bus device reset calls. I don't bother
1170 * checking if this fails as, in most cases,
1171 * the system will function just fine without
1172 * them and the only alternative would be to
1173 * not attach the device on failure.
1175 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
1176 csa.ccb_h.func_code = XPT_SASYNC_CB;
1177 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
1178 csa.callback = daasync;
1179 csa.callback_arg = periph;
1180 xpt_action((union ccb *)&csa);
1182 * Lock this peripheral until we are setup.
1183 * This first call can't block
1185 (void)cam_periph_lock(periph, PRIBIO);
1186 xpt_schedule(periph, /*priority*/5);
1188 return(CAM_REQ_CMP);
1192 dastart(struct cam_periph *periph, union ccb *start_ccb)
1194 struct da_softc *softc;
1196 softc = (struct da_softc *)periph->softc;
1199 switch (softc->state) {
1200 case DA_STATE_NORMAL:
1202 /* Pull a buffer from the queue and get going on it */
1207 * See if there is a buf with work for us to do..
1210 bp = bioq_first(&softc->bio_queue);
1211 if (periph->immediate_priority <= periph->pinfo.priority) {
1212 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1213 ("queuing for immediate ccb\n"));
1214 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1215 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1217 periph->immediate_priority = CAM_PRIORITY_NONE;
1219 wakeup(&periph->ccb_list);
1220 } else if (bp == NULL) {
1222 xpt_release_ccb(start_ccb);
1227 bioq_remove(&softc->bio_queue, bp);
1229 if ((softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1230 softc->flags &= ~DA_FLAG_NEED_OTAG;
1231 softc->ordered_tag_count++;
1232 tag_code = MSG_ORDERED_Q_TAG;
1234 tag_code = MSG_SIMPLE_Q_TAG;
1236 scsi_read_write(&start_ccb->csio,
1237 /*retries*/da_retry_count,
1239 /*tag_action*/tag_code,
1240 /*read_op*/bp->bio_cmd == BIO_READ,
1242 softc->minimum_cmd_size,
1243 /*lba*/bp->bio_pblkno,
1244 /*block_count*/bp->bio_bcount /
1245 softc->params.secsize,
1246 /*data_ptr*/ bp->bio_data,
1247 /*dxfer_len*/ bp->bio_bcount,
1248 /*sense_len*/SSD_FULL_SIZE,
1249 /*timeout*/da_default_timeout*1000);
1250 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1253 * Block out any asyncronous callbacks
1254 * while we touch the pending ccb list.
1257 LIST_INSERT_HEAD(&softc->pending_ccbs,
1258 &start_ccb->ccb_h, periph_links.le);
1259 softc->outstanding_cmds++;
1262 /* We expect a unit attention from this device */
1263 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1264 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1265 softc->flags &= ~DA_FLAG_RETRY_UA;
1268 start_ccb->ccb_h.ccb_bp = bp;
1269 bp = bioq_first(&softc->bio_queue);
1272 xpt_action(start_ccb);
1276 /* Have more work to do, so ensure we stay scheduled */
1277 xpt_schedule(periph, /* XXX priority */1);
1281 case DA_STATE_PROBE:
1283 struct ccb_scsiio *csio;
1284 struct scsi_read_capacity_data *rcap;
1286 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
1290 printf("dastart: Couldn't malloc read_capacity data\n");
1291 /* da_free_periph??? */
1294 csio = &start_ccb->csio;
1295 scsi_read_capacity(csio,
1302 start_ccb->ccb_h.ccb_bp = NULL;
1303 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1304 xpt_action(start_ccb);
1307 case DA_STATE_PROBE2:
1309 struct ccb_scsiio *csio;
1310 struct scsi_read_capacity_data_long *rcaplong;
1312 rcaplong = (struct scsi_read_capacity_data_long *)
1313 malloc(sizeof(*rcaplong), M_TEMP, M_NOWAIT);
1314 if (rcaplong == NULL) {
1315 printf("dastart: Couldn't malloc read_capacity data\n");
1316 /* da_free_periph??? */
1319 csio = &start_ccb->csio;
1320 scsi_read_capacity_16(csio,
1323 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1328 /*sense_len*/ SSD_FULL_SIZE,
1330 start_ccb->ccb_h.ccb_bp = NULL;
1331 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1332 xpt_action(start_ccb);
1339 cmd6workaround(union ccb *ccb)
1341 struct scsi_rw_6 cmd6;
1342 struct scsi_rw_10 *cmd10;
1343 struct da_softc *softc;
1347 cdb = ccb->csio.cdb_io.cdb_bytes;
1349 /* Translation only possible if CDB is an array and cmd is R/W6 */
1350 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1351 (*cdb != READ_6 && *cdb != WRITE_6))
1354 xpt_print_path(ccb->ccb_h.path);
1355 printf("READ(6)/WRITE(6) not supported, "
1356 "increasing minimum_cmd_size to 10.\n");
1357 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1358 softc->minimum_cmd_size = 10;
1360 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1361 cmd10 = (struct scsi_rw_10 *)cdb;
1362 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1364 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1365 cmd10->reserved = 0;
1366 scsi_ulto2b(cmd6.length, cmd10->length);
1367 cmd10->control = cmd6.control;
1368 ccb->csio.cdb_len = sizeof(*cmd10);
1370 /* Requeue request, unfreezing queue if necessary */
1371 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1372 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1375 cam_release_devq(ccb->ccb_h.path,
1379 /*getcount_only*/0);
1385 dadone(struct cam_periph *periph, union ccb *done_ccb)
1387 struct da_softc *softc;
1388 struct ccb_scsiio *csio;
1390 softc = (struct da_softc *)periph->softc;
1391 csio = &done_ccb->csio;
1392 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1393 case DA_CCB_BUFFER_IO:
1398 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1399 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1404 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1409 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1410 if (error == ERESTART) {
1412 * A retry was scheuled, so
1421 if (error == ENXIO) {
1423 * Catastrophic error. Mark our pack as
1426 /* XXX See if this is really a media
1429 xpt_print_path(periph->path);
1430 printf("Invalidating pack\n");
1431 softc->flags |= DA_FLAG_PACK_INVALID;
1435 * return all queued I/O with EIO, so that
1436 * the client can retry these I/Os in the
1437 * proper order should it attempt to recover.
1439 bioq_flush(&softc->bio_queue, NULL, EIO);
1441 bp->bio_error = error;
1442 bp->bio_resid = bp->bio_bcount;
1443 bp->bio_flags |= BIO_ERROR;
1445 bp->bio_resid = csio->resid;
1447 if (bp->bio_resid != 0)
1448 bp->bio_flags |= BIO_ERROR;
1450 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1451 cam_release_devq(done_ccb->ccb_h.path,
1455 /*getcount_only*/0);
1457 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1458 panic("REQ_CMP with QFRZN");
1459 bp->bio_resid = csio->resid;
1460 if (csio->resid > 0)
1461 bp->bio_flags |= BIO_ERROR;
1465 * Block out any asyncronous callbacks
1466 * while we touch the pending ccb list.
1469 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1470 softc->outstanding_cmds--;
1471 if (softc->outstanding_cmds == 0)
1472 softc->flags |= DA_FLAG_WENT_IDLE;
1481 struct scsi_read_capacity_data *rdcap;
1482 struct scsi_read_capacity_data_long *rcaplong;
1483 char announce_buf[80];
1487 if (softc->state == DA_STATE_PROBE)
1488 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1490 rcaplong = (struct scsi_read_capacity_data_long *)
1493 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1494 struct disk_params *dp;
1495 uint32_t block_size;
1498 if (softc->state == DA_STATE_PROBE) {
1499 block_size = scsi_4btoul(rdcap->length);
1500 maxsector = scsi_4btoul(rdcap->addr);
1503 * According to SBC-2, if the standard 10
1504 * byte READ CAPACITY command returns 2^32,
1505 * we should issue the 16 byte version of
1506 * the command, since the device in question
1507 * has more sectors than can be represented
1508 * with the short version of the command.
1510 if (maxsector == 0xffffffff) {
1511 softc->state = DA_STATE_PROBE2;
1512 free(rdcap, M_TEMP);
1513 xpt_release_ccb(done_ccb);
1514 xpt_schedule(periph, /*priority*/5);
1518 block_size = scsi_4btoul(rcaplong->length);
1519 maxsector = scsi_8btou64(rcaplong->addr);
1521 dasetgeom(periph, block_size, maxsector);
1522 dp = &softc->params;
1523 snprintf(announce_buf, sizeof(announce_buf),
1524 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1525 (uintmax_t) (((uintmax_t)dp->secsize *
1526 dp->sectors) / (1024*1024)),
1527 (uintmax_t)dp->sectors,
1528 dp->secsize, dp->heads, dp->secs_per_track,
1533 announce_buf[0] = '\0';
1536 * Retry any UNIT ATTENTION type errors. They
1537 * are expected at boot.
1539 error = daerror(done_ccb, CAM_RETRY_SELTO,
1540 SF_RETRY_UA|SF_NO_PRINT);
1541 if (error == ERESTART) {
1543 * A retry was scheuled, so
1547 } else if (error != 0) {
1548 struct scsi_sense_data *sense;
1550 int sense_key, error_code;
1553 struct ccb_getdev cgd;
1555 /* Don't wedge this device's queue */
1556 status = done_ccb->ccb_h.status;
1557 if ((status & CAM_DEV_QFRZN) != 0)
1558 cam_release_devq(done_ccb->ccb_h.path,
1562 /*getcount_only*/0);
1565 xpt_setup_ccb(&cgd.ccb_h,
1566 done_ccb->ccb_h.path,
1568 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1569 xpt_action((union ccb *)&cgd);
1571 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1572 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1573 || ((status & CAM_AUTOSNS_VALID) == 0))
1579 sense = &csio->sense_data;
1580 scsi_extract_sense(sense, &error_code,
1585 * Attach to anything that claims to be a
1586 * direct access or optical disk device,
1587 * as long as it doesn't return a "Logical
1588 * unit not supported" (0x25) error.
1590 if ((have_sense) && (asc != 0x25)
1591 && (error_code == SSD_CURRENT_ERROR)) {
1592 const char *sense_key_desc;
1593 const char *asc_desc;
1595 scsi_sense_desc(sense_key, asc, ascq,
1599 snprintf(announce_buf,
1600 sizeof(announce_buf),
1601 "Attempt to query device "
1602 "size failed: %s, %s",
1610 xpt_print_path(periph->path);
1611 printf("got CAM status %#x\n",
1612 done_ccb->ccb_h.status);
1615 xpt_print_path(periph->path);
1616 printf("fatal error, failed"
1617 " to attach to device\n");
1620 * Free up resources.
1622 cam_periph_invalidate(periph);
1626 free(csio->data_ptr, M_TEMP);
1627 if (announce_buf[0] != '\0') {
1628 xpt_announce_periph(periph, announce_buf);
1630 * Create our sysctl variables, now that we know
1631 * we have successfully attached.
1633 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1635 softc->state = DA_STATE_NORMAL;
1637 * Since our peripheral may be invalidated by an error
1638 * above or an external event, we must release our CCB
1639 * before releasing the probe lock on the peripheral.
1640 * The peripheral will only go away once the last lock
1641 * is removed, and we need it around for the CCB release
1644 xpt_release_ccb(done_ccb);
1645 cam_periph_unlock(periph);
1648 case DA_CCB_WAITING:
1650 /* Caller will release the CCB */
1651 wakeup(&done_ccb->ccb_h.cbfcnp);
1655 /* No-op. We're polling */
1660 xpt_release_ccb(done_ccb);
1664 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1666 struct da_softc *softc;
1667 struct cam_periph *periph;
1670 periph = xpt_path_periph(ccb->ccb_h.path);
1671 softc = (struct da_softc *)periph->softc;
1674 * Automatically detect devices that do not support
1675 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
1678 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
1679 error = cmd6workaround(ccb);
1680 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
1681 CAM_SCSI_STATUS_ERROR)
1682 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
1683 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1684 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
1685 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
1686 int sense_key, error_code, asc, ascq;
1688 scsi_extract_sense(&ccb->csio.sense_data,
1689 &error_code, &sense_key, &asc, &ascq);
1690 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
1691 error = cmd6workaround(ccb);
1693 if (error == ERESTART)
1698 * Until we have a better way of doing pack validation,
1699 * don't treat UAs as errors.
1701 sense_flags |= SF_RETRY_UA;
1702 return(cam_periph_error(ccb, cam_flags, sense_flags,
1703 &softc->saved_ccb));
1707 daprevent(struct cam_periph *periph, int action)
1709 struct da_softc *softc;
1713 softc = (struct da_softc *)periph->softc;
1715 if (((action == PR_ALLOW)
1716 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
1717 || ((action == PR_PREVENT)
1718 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
1722 ccb = cam_periph_getccb(periph, /*priority*/1);
1724 scsi_prevent(&ccb->csio,
1732 error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
1733 SF_RETRY_UA, softc->disk->d_devstat);
1736 if (action == PR_ALLOW)
1737 softc->flags &= ~DA_FLAG_PACK_LOCKED;
1739 softc->flags |= DA_FLAG_PACK_LOCKED;
1742 xpt_release_ccb(ccb);
1746 dagetcapacity(struct cam_periph *periph)
1748 struct da_softc *softc;
1750 struct scsi_read_capacity_data *rcap;
1751 struct scsi_read_capacity_data_long *rcaplong;
1756 softc = (struct da_softc *)periph->softc;
1761 /* Do a read capacity */
1762 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcaplong),
1766 ccb = cam_periph_getccb(periph, /*priority*/1);
1767 scsi_read_capacity(&ccb->csio,
1774 ccb->ccb_h.ccb_bp = NULL;
1776 error = cam_periph_runccb(ccb, daerror,
1777 /*cam_flags*/CAM_RETRY_SELTO,
1778 /*sense_flags*/SF_RETRY_UA,
1779 softc->disk->d_devstat);
1781 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1782 cam_release_devq(ccb->ccb_h.path,
1786 /*getcount_only*/0);
1789 block_len = scsi_4btoul(rcap->length);
1790 maxsector = scsi_4btoul(rcap->addr);
1792 if (maxsector != 0xffffffff)
1797 rcaplong = (struct scsi_read_capacity_data_long *)rcap;
1799 scsi_read_capacity_16(&ccb->csio,
1802 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1807 /*sense_len*/ SSD_FULL_SIZE,
1809 ccb->ccb_h.ccb_bp = NULL;
1811 error = cam_periph_runccb(ccb, daerror,
1812 /*cam_flags*/CAM_RETRY_SELTO,
1813 /*sense_flags*/SF_RETRY_UA,
1814 softc->disk->d_devstat);
1816 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1817 cam_release_devq(ccb->ccb_h.path,
1821 /*getcount_only*/0);
1824 block_len = scsi_4btoul(rcaplong->length);
1825 maxsector = scsi_8btou64(rcaplong->addr);
1831 dasetgeom(periph, block_len, maxsector);
1833 xpt_release_ccb(ccb);
1841 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector)
1843 struct ccb_calc_geometry ccg;
1844 struct da_softc *softc;
1845 struct disk_params *dp;
1847 softc = (struct da_softc *)periph->softc;
1849 dp = &softc->params;
1850 dp->secsize = block_len;
1851 dp->sectors = maxsector + 1;
1853 * Have the controller provide us with a geometry
1854 * for this disk. The only time the geometry
1855 * matters is when we boot and the controller
1856 * is the only one knowledgeable enough to come
1857 * up with something that will make this a bootable
1860 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
1861 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
1862 ccg.block_size = dp->secsize;
1863 ccg.volume_size = dp->sectors;
1865 ccg.secs_per_track = 0;
1867 xpt_action((union ccb*)&ccg);
1868 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1870 * We don't know what went wrong here- but just pick
1871 * a geometry so we don't have nasty things like divide
1875 dp->secs_per_track = 255;
1876 dp->cylinders = dp->sectors / (255 * 255);
1877 if (dp->cylinders == 0) {
1881 dp->heads = ccg.heads;
1882 dp->secs_per_track = ccg.secs_per_track;
1883 dp->cylinders = ccg.cylinders;
1888 dasendorderedtag(void *arg)
1890 struct da_softc *softc;
1893 for (softc = SLIST_FIRST(&softc_list);
1895 softc = SLIST_NEXT(softc, links)) {
1897 if ((softc->ordered_tag_count == 0)
1898 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
1899 softc->flags |= DA_FLAG_NEED_OTAG;
1901 if (softc->outstanding_cmds > 0)
1902 softc->flags &= ~DA_FLAG_WENT_IDLE;
1904 softc->ordered_tag_count = 0;
1907 /* Queue us up again */
1908 timeout(dasendorderedtag, NULL,
1909 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL);
1913 * Step through all DA peripheral drivers, and if the device is still open,
1914 * sync the disk cache to physical media.
1917 dashutdown(void * arg, int howto)
1919 struct cam_periph *periph;
1920 struct da_softc *softc;
1922 TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
1924 softc = (struct da_softc *)periph->softc;
1927 * We only sync the cache if the drive is still open, and
1928 * if the drive is capable of it..
1930 if (((softc->flags & DA_FLAG_OPEN) == 0)
1931 || (softc->quirks & DA_Q_NO_SYNC_CACHE))
1934 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
1936 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
1937 scsi_synchronize_cache(&ccb.csio,
1941 /*begin_lba*/0, /* whole disk */
1946 xpt_polled_action(&ccb);
1948 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1949 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
1950 CAM_SCSI_STATUS_ERROR)
1951 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
1952 int error_code, sense_key, asc, ascq;
1954 scsi_extract_sense(&ccb.csio.sense_data,
1955 &error_code, &sense_key,
1958 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1959 scsi_sense_print(&ccb.csio);
1961 xpt_print_path(periph->path);
1962 printf("Synchronize cache failed, status "
1963 "== 0x%x, scsi status == 0x%x\n",
1964 ccb.ccb_h.status, ccb.csio.scsi_status);
1968 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1969 cam_release_devq(ccb.ccb_h.path,
1973 /*getcount_only*/0);
1978 #else /* !_KERNEL */
1981 * XXX This is only left out of the kernel build to silence warnings. If,
1982 * for some reason this function is used in the kernel, the ifdefs should
1983 * be moved so it is included both in the kernel and userland.
1986 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
1987 void (*cbfcnp)(struct cam_periph *, union ccb *),
1988 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
1989 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
1992 struct scsi_format_unit *scsi_cmd;
1994 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
1995 scsi_cmd->opcode = FORMAT_UNIT;
1996 scsi_cmd->byte2 = byte2;
1997 scsi_ulto2b(ileave, scsi_cmd->interleave);
2002 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2011 #endif /* _KERNEL */