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>
41 #include <sys/mutex.h>
43 #include <sys/devicestat.h>
44 #include <sys/eventhandler.h>
45 #include <sys/malloc.h>
47 #include <geom/geom_disk.h>
56 #include <cam/cam_ccb.h>
57 #include <cam/cam_periph.h>
58 #include <cam/cam_xpt_periph.h>
59 #include <cam/cam_sim.h>
61 #include <cam/scsi/scsi_message.h>
64 #include <cam/scsi/scsi_da.h>
75 DA_FLAG_PACK_INVALID = 0x001,
76 DA_FLAG_NEW_PACK = 0x002,
77 DA_FLAG_PACK_LOCKED = 0x004,
78 DA_FLAG_PACK_REMOVABLE = 0x008,
79 DA_FLAG_TAGGED_QUEUING = 0x010,
80 DA_FLAG_NEED_OTAG = 0x020,
81 DA_FLAG_WENT_IDLE = 0x040,
82 DA_FLAG_RETRY_UA = 0x080,
84 DA_FLAG_SCTX_INIT = 0x200
89 DA_Q_NO_SYNC_CACHE = 0x01,
90 DA_Q_NO_6_BYTE = 0x02,
91 DA_Q_NO_PREVENT = 0x04
97 DA_CCB_BUFFER_IO = 0x03,
98 DA_CCB_WAITING = 0x04,
100 DA_CCB_TYPE_MASK = 0x0F,
101 DA_CCB_RETRY_UA = 0x10
104 /* Offsets into our private area for storing information */
105 #define ccb_state ppriv_field0
106 #define ccb_bp ppriv_ptr1
111 u_int8_t secs_per_track;
112 u_int32_t secsize; /* Number of bytes/sector */
113 u_int64_t sectors; /* total number sectors */
117 struct bio_queue_head bio_queue;
118 SLIST_ENTRY(da_softc) links;
119 LIST_HEAD(, ccb_hdr) pending_ccbs;
123 int minimum_cmd_size;
124 int ordered_tag_count;
125 int outstanding_cmds;
126 struct disk_params params;
129 struct task sysctl_task;
130 struct sysctl_ctx_list sysctl_ctx;
131 struct sysctl_oid *sysctl_tree;
132 struct callout sendordered_c;
135 struct da_quirk_entry {
136 struct scsi_inquiry_pattern inq_pat;
140 static const char quantum[] = "QUANTUM";
141 static const char microp[] = "MICROP";
143 static struct da_quirk_entry da_quirk_table[] =
145 /* SPI, FC devices */
148 * Fujitsu M2513A MO drives.
149 * Tested devices: M2513A2 firmware versions 1200 & 1300.
150 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
151 * Reported by: W.Scholten <whs@xs4all.nl>
153 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
154 /*quirks*/ DA_Q_NO_SYNC_CACHE
158 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
159 /*quirks*/ DA_Q_NO_SYNC_CACHE
163 * This particular Fujitsu drive doesn't like the
164 * synchronize cache command.
165 * Reported by: Tom Jackson <toj@gorilla.net>
167 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
168 /*quirks*/ DA_Q_NO_SYNC_CACHE
172 * This drive doesn't like the synchronize cache command
173 * either. Reported by: Matthew Jacob <mjacob@feral.com>
174 * in NetBSD PR kern/6027, August 24, 1998.
176 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
177 /*quirks*/ DA_Q_NO_SYNC_CACHE
181 * This drive doesn't like the synchronize cache command
182 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
185 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
186 /*quirks*/ DA_Q_NO_SYNC_CACHE
190 * Doesn't like the synchronize cache command.
191 * Reported by: Blaz Zupan <blaz@gold.amis.net>
193 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
194 /*quirks*/ DA_Q_NO_SYNC_CACHE
198 * Doesn't like the synchronize cache command.
199 * Reported by: Blaz Zupan <blaz@gold.amis.net>
201 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
202 /*quirks*/ DA_Q_NO_SYNC_CACHE
206 * Doesn't like the synchronize cache command.
208 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
209 /*quirks*/ DA_Q_NO_SYNC_CACHE
213 * Doesn't like the synchronize cache command.
214 * Reported by: walter@pelissero.de
216 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
217 /*quirks*/ DA_Q_NO_SYNC_CACHE
221 * Doesn't work correctly with 6 byte reads/writes.
222 * Returns illegal request, and points to byte 9 of the
224 * Reported by: Adam McDougall <bsdx@spawnet.com>
226 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
227 /*quirks*/ DA_Q_NO_6_BYTE
231 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
232 /*quirks*/ DA_Q_NO_6_BYTE
236 * Doesn't like the synchronize cache command.
237 * Reported by: walter@pelissero.de
239 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
240 /*quirks*/ DA_Q_NO_SYNC_CACHE
244 * The CISS RAID controllers do not support SYNC_CACHE
246 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
247 /*quirks*/ DA_Q_NO_SYNC_CACHE
249 /* USB mass storage devices supported by umass(4) */
252 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
255 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
256 /*quirks*/ DA_Q_NO_SYNC_CACHE
260 * Power Quotient Int. (PQI) USB flash key
263 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
264 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
268 * Creative Nomad MUVO mp3 player (USB)
271 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
272 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
276 * Jungsoft NEXDISK USB flash key
279 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
280 /*quirks*/ DA_Q_NO_SYNC_CACHE
284 * FreeDik USB Mini Data Drive
287 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
288 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
292 * Sigmatel USB Flash MP3 Player
295 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
296 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
300 * Neuros USB Digital Audio Computer
303 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
304 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
308 * SEAGRAND NP-900 MP3 Player
311 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
312 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
316 * iRiver iFP MP3 player (with UMS Firmware)
317 * PR: kern/54881, i386/63941, kern/66124
319 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
320 /*quirks*/ DA_Q_NO_SYNC_CACHE
324 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
327 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
328 /*quirks*/ DA_Q_NO_SYNC_CACHE
332 * ZICPlay USB MP3 Player with FM
335 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
336 /*quirks*/ DA_Q_NO_SYNC_CACHE
340 * TEAC USB floppy mechanisms
342 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
343 /*quirks*/ DA_Q_NO_SYNC_CACHE
347 * Kingston DataTraveler II+ USB Pen-Drive.
348 * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
350 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
351 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
355 * Motorola E398 Mobile Phone (TransFlash memory card).
356 * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
359 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
360 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
364 * Qware BeatZkey! Pro
367 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
368 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
372 * Time DPA20B 1GB MP3 Player
375 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
376 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
380 * Samsung USB key 128Mb
383 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
384 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
388 * Kingston DataTraveler 2.0 USB Flash memory.
391 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
392 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
396 * Creative MUVO Slim mp3 player (USB)
399 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
400 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
404 * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
407 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
408 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
412 * SanDisk Micro Cruzer 128MB
415 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
416 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
420 * TOSHIBA TransMemory USB sticks
423 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
424 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
429 * PR: usb/75578, usb/72344, usb/65436
431 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
432 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
436 * Genesys 6-in-1 Card Reader
439 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
440 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
444 * Rekam Digital CAMERA
447 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
448 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
452 * iRiver H10 MP3 player
455 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
456 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
463 {T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
464 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
468 * EasyMP3 EM732X USB 2.0 Flash MP3 Player
471 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
472 "1.0"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
476 static disk_strategy_t dastrategy;
477 static dumper_t dadump;
478 static periph_init_t dainit;
479 static void daasync(void *callback_arg, u_int32_t code,
480 struct cam_path *path, void *arg);
481 static void dasysctlinit(void *context, int pending);
482 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
483 static periph_ctor_t daregister;
484 static periph_dtor_t dacleanup;
485 static periph_start_t dastart;
486 static periph_oninv_t daoninvalidate;
487 static void dadone(struct cam_periph *periph,
488 union ccb *done_ccb);
489 static int daerror(union ccb *ccb, u_int32_t cam_flags,
490 u_int32_t sense_flags);
491 static void daprevent(struct cam_periph *periph, int action);
492 static int dagetcapacity(struct cam_periph *periph);
493 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
495 static timeout_t dasendorderedtag;
496 static void dashutdown(void *arg, int howto);
498 #ifndef DA_DEFAULT_TIMEOUT
499 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
502 #ifndef DA_DEFAULT_RETRY
503 #define DA_DEFAULT_RETRY 4
506 #ifndef DA_DEFAULT_SEND_ORDERED
507 #define DA_DEFAULT_SEND_ORDERED 1
511 static int da_retry_count = DA_DEFAULT_RETRY;
512 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
513 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
515 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
516 "CAM Direct Access Disk driver");
517 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
518 &da_retry_count, 0, "Normal I/O retry count");
519 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
520 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
521 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
522 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
523 SYSCTL_INT(_kern_cam_da, OID_AUTO, da_send_ordered, CTLFLAG_RW,
524 &da_send_ordered, 0, "Send Ordered Tags");
525 TUNABLE_INT("kern.cam.da.da_send_ordered", &da_send_ordered);
528 * DA_ORDEREDTAG_INTERVAL determines how often, relative
529 * to the default timeout, we check to see whether an ordered
530 * tagged transaction is appropriate to prevent simple tag
531 * starvation. Since we'd like to ensure that there is at least
532 * 1/2 of the timeout length left for a starved transaction to
533 * complete after we've sent an ordered tag, we must poll at least
534 * four times in every timeout period. This takes care of the worst
535 * case where a starved transaction starts during an interval that
536 * meets the requirement "don't send an ordered tag" test so it takes
537 * us two intervals to determine that a tag must be sent.
539 #ifndef DA_ORDEREDTAG_INTERVAL
540 #define DA_ORDEREDTAG_INTERVAL 4
543 static struct periph_driver dadriver =
546 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
549 PERIPHDRIVER_DECLARE(da, dadriver);
551 MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
554 daopen(struct disk *dp)
556 struct cam_periph *periph;
557 struct da_softc *softc;
561 periph = (struct cam_periph *)dp->d_drv1;
562 if (periph == NULL) {
566 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
570 cam_periph_lock(periph);
571 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
572 cam_periph_unlock(periph);
573 cam_periph_release(periph);
577 unit = periph->unit_number;
578 softc = (struct da_softc *)periph->softc;
579 softc->flags |= DA_FLAG_OPEN;
581 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
582 ("daopen: disk=%s%d (unit %d)\n", dp->d_name, dp->d_unit,
585 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
586 /* Invalidate our pack information. */
587 softc->flags &= ~DA_FLAG_PACK_INVALID;
590 error = dagetcapacity(periph);
594 softc->disk->d_sectorsize = softc->params.secsize;
595 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
596 /* XXX: these are not actually "firmware" values, so they may be wrong */
597 softc->disk->d_fwsectors = softc->params.secs_per_track;
598 softc->disk->d_fwheads = softc->params.heads;
599 softc->disk->d_devstat->block_size = softc->params.secsize;
600 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
604 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
605 (softc->quirks & DA_Q_NO_PREVENT) == 0)
606 daprevent(periph, PR_PREVENT);
608 softc->flags &= ~DA_FLAG_OPEN;
609 cam_periph_release(periph);
611 cam_periph_unhold(periph);
612 cam_periph_unlock(periph);
617 daclose(struct disk *dp)
619 struct cam_periph *periph;
620 struct da_softc *softc;
623 periph = (struct cam_periph *)dp->d_drv1;
627 cam_periph_lock(periph);
628 if ((error = cam_periph_hold(periph, PRIBIO)) != 0) {
629 cam_periph_unlock(periph);
630 cam_periph_release(periph);
634 softc = (struct da_softc *)periph->softc;
636 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
639 ccb = cam_periph_getccb(periph, /*priority*/1);
641 scsi_synchronize_cache(&ccb->csio,
645 /*begin_lba*/0,/* Cover the whole disk */
650 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
651 /*sense_flags*/SF_RETRY_UA,
652 softc->disk->d_devstat);
654 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
655 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
656 CAM_SCSI_STATUS_ERROR) {
658 int sense_key, error_code;
660 scsi_extract_sense(&ccb->csio.sense_data,
664 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
665 scsi_sense_print(&ccb->csio);
667 xpt_print(periph->path, "Synchronize cache "
668 "failed, status == 0x%x, scsi status == "
669 "0x%x\n", ccb->csio.ccb_h.status,
670 ccb->csio.scsi_status);
674 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
675 cam_release_devq(ccb->ccb_h.path,
681 xpt_release_ccb(ccb);
685 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
686 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
687 daprevent(periph, PR_ALLOW);
689 * If we've got removeable media, mark the blocksize as
690 * unavailable, since it could change when new media is
693 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
696 softc->flags &= ~DA_FLAG_OPEN;
697 cam_periph_unhold(periph);
698 cam_periph_release(periph);
699 cam_periph_unlock(periph);
704 * Actually translate the requested transfer into one the physical driver
705 * can understand. The transfer is described by a buf and will include
706 * only one physical transfer.
709 dastrategy(struct bio *bp)
711 struct cam_periph *periph;
712 struct da_softc *softc;
714 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
715 if (periph == NULL) {
716 biofinish(bp, NULL, ENXIO);
719 softc = (struct da_softc *)periph->softc;
721 cam_periph_lock(periph);
725 * check it's not too big a transfer for our adapter
727 scsi_minphys(bp,&sd_switch);
731 * Mask interrupts so that the pack cannot be invalidated until
732 * after we are in the queue. Otherwise, we might not properly
733 * clean up one of the buffers.
737 * If the device has been made invalid, error out
739 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
740 cam_periph_unlock(periph);
741 biofinish(bp, NULL, ENXIO);
746 * Place it in the queue of disk activities for this disk
748 bioq_disksort(&softc->bio_queue, bp);
751 * Schedule ourselves for performing the work.
753 xpt_schedule(periph, /* XXX priority */1);
754 cam_periph_unlock(periph);
760 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
762 struct cam_periph *periph;
763 struct da_softc *softc;
765 struct ccb_scsiio csio;
772 softc = (struct da_softc *)periph->softc;
773 cam_periph_lock(periph);
774 secsize = softc->params.secsize;
776 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
777 cam_periph_unlock(periph);
782 periph->flags |= CAM_PERIPH_POLLED;
783 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
784 csio.ccb_h.ccb_state = DA_CCB_DUMP;
785 scsi_read_write(&csio,
791 /*minimum_cmd_size*/ softc->minimum_cmd_size,
794 /*data_ptr*/(u_int8_t *) virtual,
796 /*sense_len*/SSD_FULL_SIZE,
797 DA_DEFAULT_TIMEOUT * 1000);
798 xpt_polled_action((union ccb *)&csio);
800 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
801 printf("Aborting dump due to I/O error.\n");
802 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
803 CAM_SCSI_STATUS_ERROR)
804 scsi_sense_print(&csio);
806 printf("status == 0x%x, scsi status == 0x%x\n",
807 csio.ccb_h.status, csio.scsi_status);
808 periph->flags |= CAM_PERIPH_POLLED;
811 cam_periph_unlock(periph);
816 * Sync the disk cache contents to the physical media.
818 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
820 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
821 csio.ccb_h.ccb_state = DA_CCB_DUMP;
822 scsi_synchronize_cache(&csio,
826 /*begin_lba*/0,/* Cover the whole disk */
830 xpt_polled_action((union ccb *)&csio);
832 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
833 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
834 CAM_SCSI_STATUS_ERROR) {
836 int sense_key, error_code;
838 scsi_extract_sense(&csio.sense_data,
842 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
843 scsi_sense_print(&csio);
845 xpt_print(periph->path, "Synchronize cache "
846 "failed, status == 0x%x, scsi status == "
847 "0x%x\n", csio.ccb_h.status,
852 periph->flags &= ~CAM_PERIPH_POLLED;
853 cam_periph_unlock(periph);
863 * Install a global async callback. This callback will
864 * receive async callbacks like "new device found".
866 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
868 if (status != CAM_REQ_CMP) {
869 printf("da: Failed to attach master async callback "
870 "due to status 0x%x!\n", status);
871 } else if (da_send_ordered) {
873 /* Register our shutdown event handler */
874 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
875 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
876 printf("dainit: shutdown event registration failed!\n");
881 daoninvalidate(struct cam_periph *periph)
883 struct da_softc *softc;
885 softc = (struct da_softc *)periph->softc;
888 * De-register any async callbacks.
890 xpt_register_async(0, daasync, periph, periph->path);
892 softc->flags |= DA_FLAG_PACK_INVALID;
895 * Return all queued I/O with ENXIO.
896 * XXX Handle any transactions queued to the card
897 * with XPT_ABORT_CCB.
899 bioq_flush(&softc->bio_queue, NULL, ENXIO);
901 disk_gone(softc->disk);
902 xpt_print(periph->path, "lost device\n");
906 dacleanup(struct cam_periph *periph)
908 struct da_softc *softc;
910 softc = (struct da_softc *)periph->softc;
912 xpt_print(periph->path, "removing device entry\n");
914 * If we can't free the sysctl tree, oh well...
916 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
917 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
918 xpt_print(periph->path, "can't remove sysctl context\n");
921 cam_periph_unlock(periph);
922 disk_destroy(softc->disk);
923 callout_drain(&softc->sendordered_c);
924 cam_periph_lock(periph);
925 free(softc, M_DEVBUF);
929 daasync(void *callback_arg, u_int32_t code,
930 struct cam_path *path, void *arg)
932 struct cam_periph *periph;
934 periph = (struct cam_periph *)callback_arg;
936 case AC_FOUND_DEVICE:
938 struct ccb_getdev *cgd;
942 cgd = (struct ccb_getdev *)arg;
946 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
947 && SID_TYPE(&cgd->inq_data) != T_RBC
948 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
952 * Allocate a peripheral instance for
953 * this device and start the probe
956 sim = xpt_path_sim(cgd->ccb_h.path);
957 status = cam_periph_alloc(daregister, daoninvalidate,
959 "da", CAM_PERIPH_BIO,
960 cgd->ccb_h.path, daasync,
961 AC_FOUND_DEVICE, cgd);
963 if (status != CAM_REQ_CMP
964 && status != CAM_REQ_INPROG)
965 printf("daasync: Unable to attach to new device "
966 "due to status 0x%x\n", status);
972 struct da_softc *softc;
973 struct ccb_hdr *ccbh;
975 softc = (struct da_softc *)periph->softc;
977 * Don't fail on the expected unit attention
980 softc->flags |= DA_FLAG_RETRY_UA;
981 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
982 ccbh->ccb_state |= DA_CCB_RETRY_UA;
986 cam_periph_async(periph, code, path, arg);
992 dasysctlinit(void *context, int pending)
994 struct cam_periph *periph;
995 struct da_softc *softc;
996 char tmpstr[80], tmpstr2[80];
998 periph = (struct cam_periph *)context;
999 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1002 softc = (struct da_softc *)periph->softc;
1003 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1004 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1007 sysctl_ctx_init(&softc->sysctl_ctx);
1008 softc->flags |= DA_FLAG_SCTX_INIT;
1009 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1010 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1011 CTLFLAG_RD, 0, tmpstr);
1012 if (softc->sysctl_tree == NULL) {
1013 printf("dasysctlinit: unable to allocate sysctl tree\n");
1015 cam_periph_release(periph);
1020 * Now register the sysctl handler, so the user can the value on
1023 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1024 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1025 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1026 "Minimum CDB size");
1029 cam_periph_release(periph);
1033 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1037 value = *(int *)arg1;
1039 error = sysctl_handle_int(oidp, &value, 0, req);
1042 || (req->newptr == NULL))
1046 * Acceptable values here are 6, 10, 12 or 16.
1050 else if ((value > 6)
1053 else if ((value > 10)
1056 else if (value > 12)
1059 *(int *)arg1 = value;
1065 daregister(struct cam_periph *periph, void *arg)
1067 struct da_softc *softc;
1068 struct ccb_pathinq cpi;
1069 struct ccb_getdev *cgd;
1073 cgd = (struct ccb_getdev *)arg;
1074 if (periph == NULL) {
1075 printf("daregister: periph was NULL!!\n");
1076 return(CAM_REQ_CMP_ERR);
1080 printf("daregister: no getdev CCB, can't register device\n");
1081 return(CAM_REQ_CMP_ERR);
1084 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
1087 if (softc == NULL) {
1088 printf("daregister: Unable to probe new device. "
1089 "Unable to allocate softc\n");
1090 return(CAM_REQ_CMP_ERR);
1093 LIST_INIT(&softc->pending_ccbs);
1094 softc->state = DA_STATE_PROBE;
1095 bioq_init(&softc->bio_queue);
1096 if (SID_IS_REMOVABLE(&cgd->inq_data))
1097 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1098 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1099 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1101 periph->softc = softc;
1104 * See if this device has any quirks.
1106 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1107 (caddr_t)da_quirk_table,
1108 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1109 sizeof(*da_quirk_table), scsi_inquiry_match);
1112 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1114 softc->quirks = DA_Q_NONE;
1116 /* Check if the SIM does not want 6 byte commands */
1117 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
1118 cpi.ccb_h.func_code = XPT_PATH_INQ;
1119 xpt_action((union ccb *)&cpi);
1120 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1121 softc->quirks |= DA_Q_NO_6_BYTE;
1123 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1126 * RBC devices don't have to support READ(6), only READ(10).
1128 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1129 softc->minimum_cmd_size = 10;
1131 softc->minimum_cmd_size = 6;
1134 * Load the user's default, if any.
1136 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1137 periph->unit_number);
1138 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1141 * 6, 10, 12 and 16 are the currently permissible values.
1143 if (softc->minimum_cmd_size < 6)
1144 softc->minimum_cmd_size = 6;
1145 else if ((softc->minimum_cmd_size > 6)
1146 && (softc->minimum_cmd_size <= 10))
1147 softc->minimum_cmd_size = 10;
1148 else if ((softc->minimum_cmd_size > 10)
1149 && (softc->minimum_cmd_size <= 12))
1150 softc->minimum_cmd_size = 12;
1151 else if (softc->minimum_cmd_size > 12)
1152 softc->minimum_cmd_size = 16;
1155 * Register this media as a disk
1158 mtx_unlock(periph->sim->mtx);
1159 softc->disk = disk_alloc();
1160 softc->disk->d_open = daopen;
1161 softc->disk->d_close = daclose;
1162 softc->disk->d_strategy = dastrategy;
1163 softc->disk->d_dump = dadump;
1164 softc->disk->d_name = "da";
1165 softc->disk->d_drv1 = periph;
1166 softc->disk->d_maxsize = DFLTPHYS; /* XXX: probably not arbitrary */
1167 softc->disk->d_unit = periph->unit_number;
1168 softc->disk->d_flags = 0;
1169 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
1170 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
1171 disk_create(softc->disk, DISK_VERSION);
1172 mtx_lock(periph->sim->mtx);
1175 * Add async callbacks for bus reset and
1176 * bus device reset calls. I don't bother
1177 * checking if this fails as, in most cases,
1178 * the system will function just fine without
1179 * them and the only alternative would be to
1180 * not attach the device on failure.
1182 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
1183 daasync, periph, periph->path);
1186 * Take an exclusive refcount on the periph while dastart is called
1187 * to finish the probe. The reference will be dropped in dadone at
1190 (void)cam_periph_hold(periph, PRIBIO);
1191 xpt_schedule(periph, /*priority*/5);
1194 * Schedule a periodic event to occasionally send an
1195 * ordered tag to a device.
1197 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
1198 callout_reset(&softc->sendordered_c,
1199 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
1200 dasendorderedtag, softc);
1202 return(CAM_REQ_CMP);
1206 dastart(struct cam_periph *periph, union ccb *start_ccb)
1208 struct da_softc *softc;
1210 softc = (struct da_softc *)periph->softc;
1212 switch (softc->state) {
1213 case DA_STATE_NORMAL:
1215 /* Pull a buffer from the queue and get going on it */
1219 * See if there is a buf with work for us to do..
1221 bp = bioq_first(&softc->bio_queue);
1222 if (periph->immediate_priority <= periph->pinfo.priority) {
1223 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1224 ("queuing for immediate ccb\n"));
1225 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1226 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1228 periph->immediate_priority = CAM_PRIORITY_NONE;
1229 wakeup(&periph->ccb_list);
1230 } else if (bp == NULL) {
1231 xpt_release_ccb(start_ccb);
1235 bioq_remove(&softc->bio_queue, bp);
1237 if ((softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1238 softc->flags &= ~DA_FLAG_NEED_OTAG;
1239 softc->ordered_tag_count++;
1240 tag_code = MSG_ORDERED_Q_TAG;
1242 tag_code = MSG_SIMPLE_Q_TAG;
1244 switch (bp->bio_cmd) {
1247 scsi_read_write(&start_ccb->csio,
1248 /*retries*/da_retry_count,
1250 /*tag_action*/tag_code,
1251 /*read_op*/bp->bio_cmd == BIO_READ,
1253 softc->minimum_cmd_size,
1254 /*lba*/bp->bio_pblkno,
1255 /*block_count*/bp->bio_bcount /
1256 softc->params.secsize,
1257 /*data_ptr*/ bp->bio_data,
1258 /*dxfer_len*/ bp->bio_bcount,
1259 /*sense_len*/SSD_FULL_SIZE,
1260 /*timeout*/da_default_timeout*1000);
1263 scsi_synchronize_cache(&start_ccb->csio,
1267 /*begin_lba*/0,/* Cover the whole disk */
1270 /*timeout*/da_default_timeout*1000);
1273 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1276 * Block out any asyncronous callbacks
1277 * while we touch the pending ccb list.
1279 LIST_INSERT_HEAD(&softc->pending_ccbs,
1280 &start_ccb->ccb_h, periph_links.le);
1281 softc->outstanding_cmds++;
1283 /* We expect a unit attention from this device */
1284 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1285 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1286 softc->flags &= ~DA_FLAG_RETRY_UA;
1289 start_ccb->ccb_h.ccb_bp = bp;
1290 bp = bioq_first(&softc->bio_queue);
1292 xpt_action(start_ccb);
1296 /* Have more work to do, so ensure we stay scheduled */
1297 xpt_schedule(periph, /* XXX priority */1);
1301 case DA_STATE_PROBE:
1303 struct ccb_scsiio *csio;
1304 struct scsi_read_capacity_data *rcap;
1306 rcap = (struct scsi_read_capacity_data *)
1307 malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
1309 printf("dastart: Couldn't malloc read_capacity data\n");
1310 /* da_free_periph??? */
1313 csio = &start_ccb->csio;
1314 scsi_read_capacity(csio,
1321 start_ccb->ccb_h.ccb_bp = NULL;
1322 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1323 xpt_action(start_ccb);
1326 case DA_STATE_PROBE2:
1328 struct ccb_scsiio *csio;
1329 struct scsi_read_capacity_data_long *rcaplong;
1331 rcaplong = (struct scsi_read_capacity_data_long *)
1332 malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
1333 if (rcaplong == NULL) {
1334 printf("dastart: Couldn't malloc read_capacity data\n");
1335 /* da_free_periph??? */
1338 csio = &start_ccb->csio;
1339 scsi_read_capacity_16(csio,
1342 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1347 /*sense_len*/ SSD_FULL_SIZE,
1349 start_ccb->ccb_h.ccb_bp = NULL;
1350 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1351 xpt_action(start_ccb);
1358 cmd6workaround(union ccb *ccb)
1360 struct scsi_rw_6 cmd6;
1361 struct scsi_rw_10 *cmd10;
1362 struct da_softc *softc;
1366 cdb = ccb->csio.cdb_io.cdb_bytes;
1368 /* Translation only possible if CDB is an array and cmd is R/W6 */
1369 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1370 (*cdb != READ_6 && *cdb != WRITE_6))
1373 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
1374 "increasing minimum_cmd_size to 10.\n");
1375 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1376 softc->minimum_cmd_size = 10;
1378 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1379 cmd10 = (struct scsi_rw_10 *)cdb;
1380 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1382 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1383 cmd10->reserved = 0;
1384 scsi_ulto2b(cmd6.length, cmd10->length);
1385 cmd10->control = cmd6.control;
1386 ccb->csio.cdb_len = sizeof(*cmd10);
1388 /* Requeue request, unfreezing queue if necessary */
1389 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1390 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1393 cam_release_devq(ccb->ccb_h.path,
1397 /*getcount_only*/0);
1403 dadone(struct cam_periph *periph, union ccb *done_ccb)
1405 struct da_softc *softc;
1406 struct ccb_scsiio *csio;
1408 softc = (struct da_softc *)periph->softc;
1409 csio = &done_ccb->csio;
1410 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1411 case DA_CCB_BUFFER_IO:
1415 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1416 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1420 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1425 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1426 if (error == ERESTART) {
1428 * A retry was scheuled, so
1435 if (error == ENXIO) {
1437 * Catastrophic error. Mark our pack as
1441 * XXX See if this is really a media
1444 xpt_print(periph->path,
1445 "Invalidating pack\n");
1446 softc->flags |= DA_FLAG_PACK_INVALID;
1450 * return all queued I/O with EIO, so that
1451 * the client can retry these I/Os in the
1452 * proper order should it attempt to recover.
1454 bioq_flush(&softc->bio_queue, NULL, EIO);
1455 bp->bio_error = error;
1456 bp->bio_resid = bp->bio_bcount;
1457 bp->bio_flags |= BIO_ERROR;
1459 bp->bio_resid = csio->resid;
1461 if (bp->bio_resid != 0)
1462 bp->bio_flags |= BIO_ERROR;
1464 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1465 cam_release_devq(done_ccb->ccb_h.path,
1469 /*getcount_only*/0);
1471 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1472 panic("REQ_CMP with QFRZN");
1473 bp->bio_resid = csio->resid;
1474 if (csio->resid > 0)
1475 bp->bio_flags |= BIO_ERROR;
1479 * Block out any asyncronous callbacks
1480 * while we touch the pending ccb list.
1482 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1483 softc->outstanding_cmds--;
1484 if (softc->outstanding_cmds == 0)
1485 softc->flags |= DA_FLAG_WENT_IDLE;
1493 struct scsi_read_capacity_data *rdcap;
1494 struct scsi_read_capacity_data_long *rcaplong;
1495 char announce_buf[80];
1499 if (softc->state == DA_STATE_PROBE)
1500 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1502 rcaplong = (struct scsi_read_capacity_data_long *)
1505 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1506 struct disk_params *dp;
1507 uint32_t block_size;
1510 if (softc->state == DA_STATE_PROBE) {
1511 block_size = scsi_4btoul(rdcap->length);
1512 maxsector = scsi_4btoul(rdcap->addr);
1515 * According to SBC-2, if the standard 10
1516 * byte READ CAPACITY command returns 2^32,
1517 * we should issue the 16 byte version of
1518 * the command, since the device in question
1519 * has more sectors than can be represented
1520 * with the short version of the command.
1522 if (maxsector == 0xffffffff) {
1523 softc->state = DA_STATE_PROBE2;
1524 free(rdcap, M_SCSIDA);
1525 xpt_release_ccb(done_ccb);
1526 xpt_schedule(periph, /*priority*/5);
1530 block_size = scsi_4btoul(rcaplong->length);
1531 maxsector = scsi_8btou64(rcaplong->addr);
1535 * Because GEOM code just will panic us if we
1536 * give them an 'illegal' value we'll avoid that
1539 if (block_size >= MAXPHYS || block_size == 0) {
1540 xpt_print(periph->path,
1541 "unsupportable block size %ju\n",
1542 (uintmax_t) block_size);
1543 announce_buf[0] = '\0';
1544 cam_periph_invalidate(periph);
1546 dasetgeom(periph, block_size, maxsector);
1547 dp = &softc->params;
1548 snprintf(announce_buf, sizeof(announce_buf),
1549 "%juMB (%ju %u byte sectors: %dH %dS/T "
1551 (((uintmax_t)dp->secsize *
1552 dp->sectors) / (1024*1024)),
1553 (uintmax_t)dp->sectors,
1554 dp->secsize, dp->heads,
1555 dp->secs_per_track, dp->cylinders);
1560 announce_buf[0] = '\0';
1563 * Retry any UNIT ATTENTION type errors. They
1564 * are expected at boot.
1566 error = daerror(done_ccb, CAM_RETRY_SELTO,
1567 SF_RETRY_UA|SF_NO_PRINT);
1568 if (error == ERESTART) {
1570 * A retry was scheuled, so
1574 } else if (error != 0) {
1575 struct scsi_sense_data *sense;
1577 int sense_key, error_code;
1580 struct ccb_getdev cgd;
1582 /* Don't wedge this device's queue */
1583 status = done_ccb->ccb_h.status;
1584 if ((status & CAM_DEV_QFRZN) != 0)
1585 cam_release_devq(done_ccb->ccb_h.path,
1589 /*getcount_only*/0);
1592 xpt_setup_ccb(&cgd.ccb_h,
1593 done_ccb->ccb_h.path,
1595 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1596 xpt_action((union ccb *)&cgd);
1598 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1599 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1600 || ((status & CAM_AUTOSNS_VALID) == 0))
1606 sense = &csio->sense_data;
1607 scsi_extract_sense(sense, &error_code,
1612 * Attach to anything that claims to be a
1613 * direct access or optical disk device,
1614 * as long as it doesn't return a "Logical
1615 * unit not supported" (0x25) error.
1617 if ((have_sense) && (asc != 0x25)
1618 && (error_code == SSD_CURRENT_ERROR)) {
1619 const char *sense_key_desc;
1620 const char *asc_desc;
1622 scsi_sense_desc(sense_key, asc, ascq,
1626 snprintf(announce_buf,
1627 sizeof(announce_buf),
1628 "Attempt to query device "
1629 "size failed: %s, %s",
1637 xpt_print(periph->path,
1638 "got CAM status %#x\n",
1639 done_ccb->ccb_h.status);
1642 xpt_print(periph->path, "fatal error, "
1643 "failed to attach to device\n");
1646 * Free up resources.
1648 cam_periph_invalidate(periph);
1652 free(csio->data_ptr, M_SCSIDA);
1653 if (announce_buf[0] != '\0') {
1654 xpt_announce_periph(periph, announce_buf);
1656 * Create our sysctl variables, now that we know
1657 * we have successfully attached.
1659 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1661 softc->state = DA_STATE_NORMAL;
1663 * Since our peripheral may be invalidated by an error
1664 * above or an external event, we must release our CCB
1665 * before releasing the probe lock on the peripheral.
1666 * The peripheral will only go away once the last lock
1667 * is removed, and we need it around for the CCB release
1670 xpt_release_ccb(done_ccb);
1671 cam_periph_unhold(periph);
1674 case DA_CCB_WAITING:
1676 /* Caller will release the CCB */
1677 wakeup(&done_ccb->ccb_h.cbfcnp);
1681 /* No-op. We're polling */
1686 xpt_release_ccb(done_ccb);
1690 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1692 struct da_softc *softc;
1693 struct cam_periph *periph;
1696 periph = xpt_path_periph(ccb->ccb_h.path);
1697 softc = (struct da_softc *)periph->softc;
1700 * Automatically detect devices that do not support
1701 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
1704 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
1705 error = cmd6workaround(ccb);
1706 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
1707 CAM_SCSI_STATUS_ERROR)
1708 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
1709 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1710 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
1711 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
1712 int sense_key, error_code, asc, ascq;
1714 scsi_extract_sense(&ccb->csio.sense_data,
1715 &error_code, &sense_key, &asc, &ascq);
1716 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
1717 error = cmd6workaround(ccb);
1719 if (error == ERESTART)
1724 * Until we have a better way of doing pack validation,
1725 * don't treat UAs as errors.
1727 sense_flags |= SF_RETRY_UA;
1728 return(cam_periph_error(ccb, cam_flags, sense_flags,
1729 &softc->saved_ccb));
1733 daprevent(struct cam_periph *periph, int action)
1735 struct da_softc *softc;
1739 softc = (struct da_softc *)periph->softc;
1741 if (((action == PR_ALLOW)
1742 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
1743 || ((action == PR_PREVENT)
1744 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
1748 ccb = cam_periph_getccb(periph, /*priority*/1);
1750 scsi_prevent(&ccb->csio,
1758 error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
1759 SF_RETRY_UA, softc->disk->d_devstat);
1762 if (action == PR_ALLOW)
1763 softc->flags &= ~DA_FLAG_PACK_LOCKED;
1765 softc->flags |= DA_FLAG_PACK_LOCKED;
1768 xpt_release_ccb(ccb);
1772 dagetcapacity(struct cam_periph *periph)
1774 struct da_softc *softc;
1776 struct scsi_read_capacity_data *rcap;
1777 struct scsi_read_capacity_data_long *rcaplong;
1781 u_int32_t sense_flags;
1783 softc = (struct da_softc *)periph->softc;
1787 sense_flags = SF_RETRY_UA;
1788 if (softc->flags & DA_FLAG_PACK_REMOVABLE)
1789 sense_flags |= SF_NO_PRINT;
1791 /* Do a read capacity */
1792 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcaplong),
1798 ccb = cam_periph_getccb(periph, /*priority*/1);
1799 scsi_read_capacity(&ccb->csio,
1806 ccb->ccb_h.ccb_bp = NULL;
1808 error = cam_periph_runccb(ccb, daerror,
1809 /*cam_flags*/CAM_RETRY_SELTO,
1811 softc->disk->d_devstat);
1813 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1814 cam_release_devq(ccb->ccb_h.path,
1818 /*getcount_only*/0);
1821 block_len = scsi_4btoul(rcap->length);
1822 maxsector = scsi_4btoul(rcap->addr);
1824 if (maxsector != 0xffffffff)
1829 rcaplong = (struct scsi_read_capacity_data_long *)rcap;
1831 scsi_read_capacity_16(&ccb->csio,
1834 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1839 /*sense_len*/ SSD_FULL_SIZE,
1841 ccb->ccb_h.ccb_bp = NULL;
1843 error = cam_periph_runccb(ccb, daerror,
1844 /*cam_flags*/CAM_RETRY_SELTO,
1846 softc->disk->d_devstat);
1848 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1849 cam_release_devq(ccb->ccb_h.path,
1853 /*getcount_only*/0);
1856 block_len = scsi_4btoul(rcaplong->length);
1857 maxsector = scsi_8btou64(rcaplong->addr);
1863 dasetgeom(periph, block_len, maxsector);
1865 xpt_release_ccb(ccb);
1867 free(rcap, M_SCSIDA);
1873 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector)
1875 struct ccb_calc_geometry ccg;
1876 struct da_softc *softc;
1877 struct disk_params *dp;
1879 softc = (struct da_softc *)periph->softc;
1881 dp = &softc->params;
1882 dp->secsize = block_len;
1883 dp->sectors = maxsector + 1;
1885 * Have the controller provide us with a geometry
1886 * for this disk. The only time the geometry
1887 * matters is when we boot and the controller
1888 * is the only one knowledgeable enough to come
1889 * up with something that will make this a bootable
1892 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
1893 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
1894 ccg.block_size = dp->secsize;
1895 ccg.volume_size = dp->sectors;
1897 ccg.secs_per_track = 0;
1899 xpt_action((union ccb*)&ccg);
1900 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1902 * We don't know what went wrong here- but just pick
1903 * a geometry so we don't have nasty things like divide
1907 dp->secs_per_track = 255;
1908 dp->cylinders = dp->sectors / (255 * 255);
1909 if (dp->cylinders == 0) {
1913 dp->heads = ccg.heads;
1914 dp->secs_per_track = ccg.secs_per_track;
1915 dp->cylinders = ccg.cylinders;
1920 dasendorderedtag(void *arg)
1922 struct da_softc *softc = arg;
1924 if (da_send_ordered) {
1925 if ((softc->ordered_tag_count == 0)
1926 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
1927 softc->flags |= DA_FLAG_NEED_OTAG;
1929 if (softc->outstanding_cmds > 0)
1930 softc->flags &= ~DA_FLAG_WENT_IDLE;
1932 softc->ordered_tag_count = 0;
1934 /* Queue us up again */
1935 callout_reset(&softc->sendordered_c,
1936 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
1937 dasendorderedtag, softc);
1941 * Step through all DA peripheral drivers, and if the device is still open,
1942 * sync the disk cache to physical media.
1945 dashutdown(void * arg, int howto)
1947 struct cam_periph *periph;
1948 struct da_softc *softc;
1950 TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
1953 cam_periph_lock(periph);
1954 softc = (struct da_softc *)periph->softc;
1957 * We only sync the cache if the drive is still open, and
1958 * if the drive is capable of it..
1960 if (((softc->flags & DA_FLAG_OPEN) == 0)
1961 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
1962 cam_periph_unlock(periph);
1966 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
1968 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
1969 scsi_synchronize_cache(&ccb.csio,
1973 /*begin_lba*/0, /* whole disk */
1978 xpt_polled_action(&ccb);
1980 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1981 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
1982 CAM_SCSI_STATUS_ERROR)
1983 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
1984 int error_code, sense_key, asc, ascq;
1986 scsi_extract_sense(&ccb.csio.sense_data,
1987 &error_code, &sense_key,
1990 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1991 scsi_sense_print(&ccb.csio);
1993 xpt_print(periph->path, "Synchronize "
1994 "cache failed, status == 0x%x, scsi status "
1995 "== 0x%x\n", ccb.ccb_h.status,
1996 ccb.csio.scsi_status);
2000 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
2001 cam_release_devq(ccb.ccb_h.path,
2005 /*getcount_only*/0);
2006 cam_periph_unlock(periph);
2010 #else /* !_KERNEL */
2013 * XXX This is only left out of the kernel build to silence warnings. If,
2014 * for some reason this function is used in the kernel, the ifdefs should
2015 * be moved so it is included both in the kernel and userland.
2018 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2019 void (*cbfcnp)(struct cam_periph *, union ccb *),
2020 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2021 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2024 struct scsi_format_unit *scsi_cmd;
2026 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2027 scsi_cmd->opcode = FORMAT_UNIT;
2028 scsi_cmd->byte2 = byte2;
2029 scsi_ulto2b(ileave, scsi_cmd->interleave);
2034 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2043 #endif /* _KERNEL */