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 $
29 * $DragonFly: src/sys/bus/cam/scsi/scsi_da.c,v 1.57 2008/07/12 20:36:06 pavalos Exp $
32 #include <sys/param.h>
33 #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>
51 #include <sys/thread2.h>
64 #include "../cam_ccb.h"
65 #include "../cam_extend.h"
66 #include "../cam_periph.h"
67 #include "../cam_xpt_periph.h"
68 #include "../cam_sim.h"
70 #include "scsi_message.h"
84 DA_FLAG_PACK_INVALID
= 0x001,
85 DA_FLAG_NEW_PACK
= 0x002,
86 DA_FLAG_PACK_LOCKED
= 0x004,
87 DA_FLAG_PACK_REMOVABLE
= 0x008,
88 DA_FLAG_TAGGED_QUEUING
= 0x010,
89 DA_FLAG_NEED_OTAG
= 0x020,
90 DA_FLAG_WENT_IDLE
= 0x040,
91 DA_FLAG_RETRY_UA
= 0x080,
93 DA_FLAG_SCTX_INIT
= 0x200
98 DA_Q_NO_SYNC_CACHE
= 0x01,
99 DA_Q_NO_6_BYTE
= 0x02,
100 DA_Q_NO_PREVENT
= 0x04
105 DA_CCB_PROBE2
= 0x02,
106 DA_CCB_BUFFER_IO
= 0x03,
107 DA_CCB_WAITING
= 0x04,
109 DA_CCB_TYPE_MASK
= 0x0F,
110 DA_CCB_RETRY_UA
= 0x10
113 /* Offsets into our private area for storing information */
114 #define ccb_state ppriv_field0
115 #define ccb_bio ppriv_ptr1
120 u_int8_t secs_per_track
;
121 u_int32_t secsize
; /* Number of bytes/sector */
122 u_int64_t sectors
; /* total number sectors */
126 struct bio_queue_head bio_queue
;
127 struct devstat device_stats
;
128 SLIST_ENTRY(da_softc
) links
;
129 LIST_HEAD(, ccb_hdr
) pending_ccbs
;
133 int minimum_cmd_size
;
134 int ordered_tag_count
;
135 int outstanding_cmds
;
136 struct disk_params params
;
139 struct task sysctl_task
;
140 struct sysctl_ctx_list sysctl_ctx
;
141 struct sysctl_oid
*sysctl_tree
;
142 struct callout sendordered_c
;
145 struct da_quirk_entry
{
146 struct scsi_inquiry_pattern inq_pat
;
150 static const char quantum
[] = "QUANTUM";
151 static const char microp
[] = "MICROP";
153 static struct da_quirk_entry da_quirk_table
[] =
155 /* SPI, FC devices */
158 * Fujitsu M2513A MO drives.
159 * Tested devices: M2513A2 firmware versions 1200 & 1300.
160 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
161 * Reported by: W.Scholten <whs@xs4all.nl>
163 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "FUJITSU", "M2513A", "*"},
164 /*quirks*/ DA_Q_NO_SYNC_CACHE
168 {T_OPTICAL
, SIP_MEDIA_REMOVABLE
, "FUJITSU", "M2513A", "*"},
169 /*quirks*/ DA_Q_NO_SYNC_CACHE
173 * This particular Fujitsu drive doesn't like the
174 * synchronize cache command.
175 * Reported by: Tom Jackson <toj@gorilla.net>
177 {T_DIRECT
, SIP_MEDIA_FIXED
, "FUJITSU", "M2954*", "*"},
178 /*quirks*/ DA_Q_NO_SYNC_CACHE
182 * This drive doesn't like the synchronize cache command
183 * either. Reported by: Matthew Jacob <mjacob@feral.com>
184 * in NetBSD PR kern/6027, August 24, 1998.
186 {T_DIRECT
, SIP_MEDIA_FIXED
, microp
, "2217*", "*"},
187 /*quirks*/ DA_Q_NO_SYNC_CACHE
191 * This drive doesn't like the synchronize cache command
192 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
195 {T_DIRECT
, SIP_MEDIA_FIXED
, microp
, "2112*", "*"},
196 /*quirks*/ DA_Q_NO_SYNC_CACHE
200 * Doesn't like the synchronize cache command.
201 * Reported by: Blaz Zupan <blaz@gold.amis.net>
203 {T_DIRECT
, SIP_MEDIA_FIXED
, "NEC", "D3847*", "*"},
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
, quantum
, "MAVERICK 540S", "*"},
212 /*quirks*/ DA_Q_NO_SYNC_CACHE
216 * Doesn't like the synchronize cache command.
218 {T_DIRECT
, SIP_MEDIA_FIXED
, quantum
, "LPS525S", "*"},
219 /*quirks*/ DA_Q_NO_SYNC_CACHE
223 * Doesn't like the synchronize cache command.
224 * Reported by: walter@pelissero.de
226 {T_DIRECT
, SIP_MEDIA_FIXED
, quantum
, "LPS540S", "*"},
227 /*quirks*/ DA_Q_NO_SYNC_CACHE
231 * Doesn't work correctly with 6 byte reads/writes.
232 * Returns illegal request, and points to byte 9 of the
234 * Reported by: Adam McDougall <bsdx@spawnet.com>
236 {T_DIRECT
, SIP_MEDIA_FIXED
, quantum
, "VIKING 4*", "*"},
237 /*quirks*/ DA_Q_NO_6_BYTE
241 {T_DIRECT
, SIP_MEDIA_FIXED
, quantum
, "VIKING 2*", "*"},
242 /*quirks*/ DA_Q_NO_6_BYTE
246 * Doesn't like the synchronize cache command.
247 * Reported by: walter@pelissero.de
249 {T_DIRECT
, SIP_MEDIA_FIXED
, "CONNER", "CP3500*", "*"},
250 /*quirks*/ DA_Q_NO_SYNC_CACHE
254 * The CISS RAID controllers do not support SYNC_CACHE
256 {T_DIRECT
, SIP_MEDIA_FIXED
, "COMPAQ", "RAID*", "*"},
257 /*quirks*/ DA_Q_NO_SYNC_CACHE
259 /* USB mass storage devices supported by umass(4) */
262 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
265 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "EXATEL", "i-BEAD10*", "*"},
266 /*quirks*/ DA_Q_NO_SYNC_CACHE
270 * Power Quotient Int. (PQI) USB flash key
273 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "Generic*", "USB Flash Disk*",
274 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
278 * Creative Nomad MUVO mp3 player (USB)
281 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "CREATIVE", "NOMAD_MUVO", "*"},
282 /*quirks*/ DA_Q_NO_SYNC_CACHE
|DA_Q_NO_PREVENT
286 * Jungsoft NEXDISK USB flash key
289 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "JUNGSOFT", "NEXDISK*", "*"},
290 /*quirks*/ DA_Q_NO_SYNC_CACHE
294 * FreeDik USB Mini Data Drive
297 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "FreeDik*", "Mini Data Drive",
298 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
302 * Sigmatel USB Flash MP3 Player
305 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "SigmaTel", "MSCN", "*"},
306 /*quirks*/ DA_Q_NO_SYNC_CACHE
|DA_Q_NO_PREVENT
310 * Neuros USB Digital Audio Computer
313 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "NEUROS", "dig. audio comp.",
314 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
318 * SEAGRAND NP-900 MP3 Player
321 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "SEAGRAND", "NP-900*", "*"},
322 /*quirks*/ DA_Q_NO_SYNC_CACHE
|DA_Q_NO_PREVENT
326 * iRiver iFP MP3 player (with UMS Firmware)
327 * PR: kern/54881, i386/63941, kern/66124
329 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "iRiver", "iFP*", "*"},
330 /*quirks*/ DA_Q_NO_SYNC_CACHE
334 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
337 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "FL" , "Nex*", "*"},
338 /*quirks*/ DA_Q_NO_SYNC_CACHE
342 * ZICPlay USB MP3 Player with FM
345 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "ACTIONS*" , "USB DISK*", "*"},
346 /*quirks*/ DA_Q_NO_SYNC_CACHE
350 * TEAC USB floppy mechanisms
352 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "TEAC" , "FD-05*", "*"},
353 /*quirks*/ DA_Q_NO_SYNC_CACHE
357 * Kingston DataTraveler II+ USB Pen-Drive.
358 * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
360 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "Kingston" , "DataTraveler II+",
361 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
365 * Motorola E398 Mobile Phone (TransFlash memory card).
366 * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
369 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "Motorola" , "Motorola Phone",
370 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
374 * Qware BeatZkey! Pro
377 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "GENERIC", "USB DISK DEVICE",
378 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
382 * Time DPA20B 1GB MP3 Player
385 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "USB2.0*", "(FS) FLASH DISK*",
386 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
390 * Samsung USB key 128Mb
393 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "USB-DISK", "FreeDik-FlashUsb",
394 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
398 * Kingston DataTraveler 2.0 USB Flash memory.
401 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "Kingston", "DataTraveler 2.0",
402 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
406 * Creative MUVO Slim mp3 player (USB)
409 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "CREATIVE", "MuVo Slim",
410 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
|DA_Q_NO_PREVENT
414 * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
417 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "Generic*", "MUSIC DISK",
418 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
422 * SanDisk Micro Cruzer 128MB
425 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "SanDisk" , "Micro Cruzer",
426 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
430 * TOSHIBA TransMemory USB sticks
433 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "TOSHIBA", "TransMemory",
434 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
439 * PR: usb/75578, usb/72344, usb/65436
441 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "*" , "USB DISK*",
442 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
446 * Genesys 6-in-1 Card Reader
449 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "Generic*", "STORAGE DEVICE*",
450 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
454 * Rekam Digital CAMERA
457 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "CAMERA*", "4MP-9J6*",
458 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
462 * iRiver H10 MP3 player
465 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "iriver", "H10*",
466 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
470 * iRiver U10 MP3 player
473 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "iriver", "U10*",
474 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
481 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "X-Micro", "Flash Disk",
482 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
486 * EasyMP3 EM732X USB 2.0 Flash MP3 Player
489 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "EM732X", "MP3 Player*",
490 "1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
497 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "DENVER", "MP3 PLAYER",
498 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
502 * Philips USB Key Audio KEY013
505 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "PHILIPS", "Key*", "*"},
506 /*quirks*/ DA_Q_NO_SYNC_CACHE
| DA_Q_NO_PREVENT
513 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "JNC*" , "MP3 Player*",
514 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
521 {T_DIRECT
, SIP_MEDIA_FIXED
, "SAMSUNG", "MP0402H", "*"},
522 /*quirks*/ DA_Q_NO_SYNC_CACHE
526 * I/O Magic USB flash - Giga Bank
529 {T_DIRECT
, SIP_MEDIA_FIXED
, "GS-Magic", "stor*", "*"},
530 /*quirks*/ DA_Q_NO_SYNC_CACHE
534 * JoyFly 128mb USB Flash Drive
537 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "USB 2.0", "Flash Disk*",
538 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
545 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "ChipsBnk", "USB*",
546 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
550 * Samsung YP-U3 mp3-player
553 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "Samsung", "YP-U3",
554 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
557 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "Netac", "OnlyDisk*",
558 "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
562 * USB 2.0 (HS) Flash Disk MP3 player
564 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "USB 2.0", "(HS) Flash Disk", "*"},
565 /*quirks*/ DA_Q_NO_SYNC_CACHE
569 static d_open_t daopen
;
570 static d_close_t daclose
;
571 static d_strategy_t dastrategy
;
572 static d_dump_t dadump
;
573 static periph_init_t dainit
;
574 static void daasync(void *callback_arg
, u_int32_t code
,
575 struct cam_path
*path
, void *arg
);
576 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS
);
577 static periph_ctor_t daregister
;
578 static periph_dtor_t dacleanup
;
579 static periph_start_t dastart
;
580 static periph_oninv_t daoninvalidate
;
581 static void dadone(struct cam_periph
*periph
,
582 union ccb
*done_ccb
);
583 static int daerror(union ccb
*ccb
, u_int32_t cam_flags
,
584 u_int32_t sense_flags
);
585 static void daprevent(struct cam_periph
*periph
, int action
);
586 static int dagetcapacity(struct cam_periph
*periph
);
587 static void dasetgeom(struct cam_periph
*periph
, uint32_t block_len
,
590 static timeout_t dasendorderedtag
;
591 static void dashutdown(void *arg
, int howto
);
593 #ifndef DA_DEFAULT_TIMEOUT
594 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
597 #ifndef DA_DEFAULT_RETRY
598 #define DA_DEFAULT_RETRY 4
601 #ifndef DA_DEFAULT_SEND_ORDERED
602 #define DA_DEFAULT_SEND_ORDERED 1
605 static int da_retry_count
= DA_DEFAULT_RETRY
;
606 static int da_default_timeout
= DA_DEFAULT_TIMEOUT
;
607 static int da_send_ordered
= DA_DEFAULT_SEND_ORDERED
;
608 static struct callout dasendorderedtag_ch
;
610 SYSCTL_NODE(_kern_cam
, OID_AUTO
, da
, CTLFLAG_RD
, 0,
611 "CAM Direct Access Disk driver");
612 SYSCTL_INT(_kern_cam_da
, OID_AUTO
, retry_count
, CTLFLAG_RW
,
613 &da_retry_count
, 0, "Normal I/O retry count");
614 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count
);
615 SYSCTL_INT(_kern_cam_da
, OID_AUTO
, default_timeout
, CTLFLAG_RW
,
616 &da_default_timeout
, 0, "Normal I/O timeout (in seconds)");
617 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout
);
618 SYSCTL_INT(_kern_cam_da
, OID_AUTO
, da_send_ordered
, CTLFLAG_RW
,
619 &da_send_ordered
, 0, "Send Ordered Tags");
620 TUNABLE_INT("kern.cam.da.da_send_ordered", &da_send_ordered
);
623 * DA_ORDEREDTAG_INTERVAL determines how often, relative
624 * to the default timeout, we check to see whether an ordered
625 * tagged transaction is appropriate to prevent simple tag
626 * starvation. Since we'd like to ensure that there is at least
627 * 1/2 of the timeout length left for a starved transaction to
628 * complete after we've sent an ordered tag, we must poll at least
629 * four times in every timeout period. This takes care of the worst
630 * case where a starved transaction starts during an interval that
631 * meets the requirement "don't send an ordered tag" test so it takes
632 * us two intervals to determine that a tag must be sent.
634 #ifndef DA_ORDEREDTAG_INTERVAL
635 #define DA_ORDEREDTAG_INTERVAL 4
638 static struct periph_driver dadriver
=
641 TAILQ_HEAD_INITIALIZER(dadriver
.units
), /* generation */ 0
644 PERIPHDRIVER_DECLARE(da
, dadriver
);
646 static struct dev_ops da_ops
= {
647 { "da", DA_CDEV_MAJOR
, D_DISK
},
651 .d_write
= physwrite
,
652 .d_strategy
= dastrategy
,
656 static struct extend_array
*daperiphs
;
658 MALLOC_DEFINE(M_SCSIDA
, "scsi_da", "scsi_da buffers");
661 daopen(struct dev_open_args
*ap
)
663 cdev_t dev
= ap
->a_head
.a_dev
;
664 struct cam_periph
*periph
;
665 struct da_softc
*softc
;
666 struct disk_info info
;
671 periph
= cam_extend_get(daperiphs
, unit
);
672 if (periph
== NULL
) {
676 if (cam_periph_acquire(periph
) != CAM_REQ_CMP
) {
680 cam_periph_lock(periph
);
681 if ((error
= cam_periph_hold(periph
, PCATCH
)) != 0) {
682 cam_periph_unlock(periph
);
683 cam_periph_release(periph
);
687 unit
= periph
->unit_number
;
688 softc
= (struct da_softc
*)periph
->softc
;
689 softc
->flags
|= DA_FLAG_OPEN
;
691 CAM_DEBUG(periph
->path
, CAM_DEBUG_TRACE
,
692 ("daopen: dev=%s (unit %d)\n", devtoname(dev
),
695 if ((softc
->flags
& DA_FLAG_PACK_INVALID
) != 0) {
696 /* Invalidate our pack information. */
697 disk_invalidate(&softc
->disk
);
698 softc
->flags
&= ~DA_FLAG_PACK_INVALID
;
701 error
= dagetcapacity(periph
);
704 /* Do a read capacity */
706 struct scsi_read_capacity_data
*rcap
;
709 rcap
= kmalloc(sizeof(*rcap
), M_SCSIDA
, M_INTWAIT
| M_ZERO
);
711 ccb
= cam_periph_getccb(periph
, /*priority*/1);
712 scsi_read_capacity(&ccb
->csio
,
719 ccb
->ccb_h
.ccb_bio
= NULL
;
721 error
= cam_periph_runccb(ccb
, daerror
, /*cam_flags*/0,
722 /*sense_flags*/SF_RETRY_UA
|
724 &softc
->device_stats
);
726 xpt_release_ccb(ccb
);
729 dasetgeom(periph
, rcap
);
732 kfree(rcap
, M_SCSIDA
);
737 struct ccb_getdev cgd
;
739 /* Build disk information structure */
740 bzero(&info
, sizeof(info
));
741 info
.d_type
= DTYPE_SCSI
;
744 * Grab the inquiry data to get the vendor and product names.
745 * Put them in the typename and packname for the label.
747 xpt_setup_ccb(&cgd
.ccb_h
, periph
->path
, /*priority*/ 1);
748 cgd
.ccb_h
.func_code
= XPT_GDEV_TYPE
;
749 xpt_action((union ccb
*)&cgd
);
752 strncpy(label
->d_typename
, cgd
.inq_data
.vendor
,
753 min(SID_VENDOR_SIZE
, sizeof(label
->d_typename
)));
754 strncpy(label
->d_packname
, cgd
.inq_data
.product
,
755 min(SID_PRODUCT_SIZE
, sizeof(label
->d_packname
)));
761 info
.d_media_blksize
= softc
->params
.secsize
;
762 info
.d_media_blocks
= softc
->params
.sectors
;
763 info
.d_media_size
= 0;
768 info
.d_secpertrack
= softc
->params
.secs_per_track
;
769 info
.d_nheads
= softc
->params
.heads
;
770 info
.d_ncylinders
= softc
->params
.cylinders
;
771 info
.d_secpercyl
= softc
->params
.heads
*
772 softc
->params
.secs_per_track
;
773 disk_setdiskinfo(&softc
->disk
, &info
);
776 * Check to see whether or not the blocksize is set yet.
777 * If it isn't, set it and then clear the blocksize
778 * unavailable flag for the device statistics.
780 if ((softc
->device_stats
.flags
& DEVSTAT_BS_UNAVAILABLE
) != 0){
781 softc
->device_stats
.block_size
= softc
->params
.secsize
;
782 softc
->device_stats
.flags
&= ~DEVSTAT_BS_UNAVAILABLE
;
787 if ((softc
->flags
& DA_FLAG_PACK_REMOVABLE
) != 0 &&
788 (softc
->quirks
& DA_Q_NO_PREVENT
) == 0)
789 daprevent(periph
, PR_PREVENT
);
791 softc
->flags
&= ~DA_FLAG_OPEN
;
792 cam_periph_release(periph
);
794 cam_periph_unhold(periph
);
795 cam_periph_unlock(periph
);
800 daclose(struct dev_close_args
*ap
)
802 cdev_t dev
= ap
->a_head
.a_dev
;
803 struct cam_periph
*periph
;
804 struct da_softc
*softc
;
809 periph
= cam_extend_get(daperiphs
, unit
);
813 cam_periph_lock(periph
);
814 if ((error
= cam_periph_hold(periph
, 0)) != 0) {
815 cam_periph_unlock(periph
);
816 cam_periph_release(periph
);
820 softc
= (struct da_softc
*)periph
->softc
;
822 if ((softc
->quirks
& DA_Q_NO_SYNC_CACHE
) == 0) {
825 ccb
= cam_periph_getccb(periph
, /*priority*/1);
827 scsi_synchronize_cache(&ccb
->csio
,
831 /*begin_lba*/0,/* Cover the whole disk */
836 cam_periph_runccb(ccb
, /*error_routine*/NULL
, /*cam_flags*/0,
837 /*sense_flags*/SF_RETRY_UA
,
838 &softc
->device_stats
);
840 if ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
841 if ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) ==
842 CAM_SCSI_STATUS_ERROR
) {
844 int sense_key
, error_code
;
846 scsi_extract_sense(&ccb
->csio
.sense_data
,
850 if (sense_key
!= SSD_KEY_ILLEGAL_REQUEST
)
851 scsi_sense_print(&ccb
->csio
);
853 xpt_print(periph
->path
, "Synchronize cache "
854 "failed, status == 0x%x, scsi status == "
855 "0x%x\n", ccb
->csio
.ccb_h
.status
,
856 ccb
->csio
.scsi_status
);
860 if ((ccb
->ccb_h
.status
& CAM_DEV_QFRZN
) != 0)
861 cam_release_devq(ccb
->ccb_h
.path
,
867 xpt_release_ccb(ccb
);
871 if ((softc
->flags
& DA_FLAG_PACK_REMOVABLE
) != 0) {
872 if ((softc
->quirks
& DA_Q_NO_PREVENT
) == 0)
873 daprevent(periph
, PR_ALLOW
);
875 * If we've got removeable media, mark the blocksize as
876 * unavailable, since it could change when new media is
879 softc
->device_stats
.flags
|= DEVSTAT_BS_UNAVAILABLE
;
883 * Don't compound any ref counting software bugs with more.
885 if (softc
->flags
& DA_FLAG_OPEN
) {
886 softc
->flags
&= ~DA_FLAG_OPEN
;
887 cam_periph_release(periph
);
889 xpt_print(periph
->path
,
890 "daclose() called on an already closed device!\n");
892 cam_periph_unhold(periph
);
893 cam_periph_unlock(periph
);
898 * Actually translate the requested transfer into one the physical driver
899 * can understand. The transfer is described by a buf and will include
900 * only one physical transfer.
903 dastrategy(struct dev_strategy_args
*ap
)
905 cdev_t dev
= ap
->a_head
.a_dev
;
906 struct bio
*bio
= ap
->a_bio
;
907 struct buf
*bp
= bio
->bio_buf
;
908 struct cam_periph
*periph
;
909 struct da_softc
*softc
;
915 periph
= cam_extend_get(daperiphs
, unit
);
916 if (periph
== NULL
) {
920 softc
= (struct da_softc
*)periph
->softc
;
922 cam_periph_lock(periph
);
926 * check it's not too big a transfer for our adapter
928 scsi_minphys(bp
, &sd_switch
);
932 * Mask interrupts so that the pack cannot be invalidated until
933 * after we are in the queue. Otherwise, we might not properly
934 * clean up one of the buffers.
938 * If the device has been made invalid, error out
940 if ((softc
->flags
& DA_FLAG_PACK_INVALID
)) {
941 cam_periph_unlock(periph
);
947 * Place it in the queue of disk activities for this disk
949 bioqdisksort(&softc
->bio_queue
, bio
);
952 * Schedule ourselves for performing the work.
954 xpt_schedule(periph
, /* XXX priority */1);
955 cam_periph_unlock(periph
);
959 bp
->b_flags
|= B_ERROR
;
962 * Correctly set the buf to indicate a completed xfer
964 bp
->b_resid
= bp
->b_bcount
;
970 dadump(struct dev_dump_args
*ap
)
972 cdev_t dev
= ap
->a_head
.a_dev
;
973 struct cam_periph
*periph
;
974 struct da_softc
*softc
;
978 struct ccb_scsiio csio
;
979 int dumppages
= MAXDUMPPGS
;
982 /* toss any characters present prior to dump */
983 while (cncheckc() != -1)
987 periph
= cam_extend_get(daperiphs
, unit
);
988 if (periph
== NULL
) {
991 softc
= (struct da_softc
*)periph
->softc
;
993 cam_periph_lock(periph
);
994 if ((softc
->flags
& DA_FLAG_PACK_INVALID
) != 0) {
995 cam_periph_unlock(periph
);
999 addr
= 0; /* starting address */
1000 blkcnt
= howmany(PAGE_SIZE
, ap
->a_secsize
);
1002 while (ap
->a_count
> 0) {
1005 if ((ap
->a_count
/ blkcnt
) < dumppages
)
1006 dumppages
= ap
->a_count
/ blkcnt
;
1008 for (i
= 0; i
< dumppages
; ++i
) {
1009 vm_paddr_t a
= addr
+ (i
* PAGE_SIZE
);
1010 if (is_physical_memory(a
))
1011 va
= pmap_kenter_temporary(trunc_page(a
), i
);
1013 va
= pmap_kenter_temporary(trunc_page(0), i
);
1016 periph
->flags
|= CAM_PERIPH_POLLED
;
1017 xpt_setup_ccb(&csio
.ccb_h
, periph
->path
, /*priority*/1);
1018 csio
.ccb_h
.ccb_state
= DA_CCB_DUMP
;
1019 scsi_read_write(&csio
,
1025 /*minimum_cmd_size*/ softc
->minimum_cmd_size
,
1028 /*data_ptr*/(u_int8_t
*) va
,
1029 /*dxfer_len*/blkcnt
* ap
->a_secsize
* dumppages
,
1030 /*sense_len*/SSD_FULL_SIZE
,
1031 DA_DEFAULT_TIMEOUT
* 1000);
1032 xpt_polled_action((union ccb
*)&csio
);
1034 if ((csio
.ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
1035 kprintf("Aborting dump due to I/O error.\n");
1036 if ((csio
.ccb_h
.status
& CAM_STATUS_MASK
) ==
1037 CAM_SCSI_STATUS_ERROR
)
1038 scsi_sense_print(&csio
);
1040 kprintf("status == 0x%x, scsi status == 0x%x\n",
1041 csio
.ccb_h
.status
, csio
.scsi_status
);
1042 periph
->flags
|= CAM_PERIPH_POLLED
;
1046 if (dumpstatus(addr
, (off_t
)ap
->a_count
* softc
->params
.secsize
) < 0)
1049 /* update block count */
1050 ap
->a_count
-= blkcnt
* dumppages
;
1051 ap
->a_blkno
+= blkcnt
* dumppages
;
1052 addr
+= PAGE_SIZE
* dumppages
;
1056 * Sync the disk cache contents to the physical media.
1058 if ((softc
->quirks
& DA_Q_NO_SYNC_CACHE
) == 0) {
1060 xpt_setup_ccb(&csio
.ccb_h
, periph
->path
, /*priority*/1);
1061 csio
.ccb_h
.ccb_state
= DA_CCB_DUMP
;
1062 scsi_synchronize_cache(&csio
,
1066 /*begin_lba*/0,/* Cover the whole disk */
1070 xpt_polled_action((union ccb
*)&csio
);
1072 if ((csio
.ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
1073 if ((csio
.ccb_h
.status
& CAM_STATUS_MASK
) ==
1074 CAM_SCSI_STATUS_ERROR
) {
1076 int sense_key
, error_code
;
1078 scsi_extract_sense(&csio
.sense_data
,
1082 if (sense_key
!= SSD_KEY_ILLEGAL_REQUEST
)
1083 scsi_sense_print(&csio
);
1085 xpt_print(periph
->path
, "Synchronize cache "
1086 "failed, status == 0x%x, scsi status == "
1087 "0x%x\n", csio
.ccb_h
.status
,
1092 periph
->flags
&= ~CAM_PERIPH_POLLED
;
1093 cam_periph_unlock(periph
);
1103 * Create our extend array for storing the devices we attach to.
1105 daperiphs
= cam_extend_new();
1106 if (daperiphs
== NULL
) {
1107 kprintf("da: Failed to alloc extend array!\n");
1111 callout_init(&dasendorderedtag_ch
);
1114 * Install a global async callback. This callback will
1115 * receive async callbacks like "new device found".
1117 status
= xpt_register_async(AC_FOUND_DEVICE
, daasync
, NULL
, NULL
);
1119 if (status
!= CAM_REQ_CMP
) {
1120 kprintf("da: Failed to attach master async callback "
1121 "due to status 0x%x!\n", status
);
1122 } else if (da_send_ordered
) {
1124 /* Register our shutdown event handler */
1125 if ((EVENTHANDLER_REGISTER(shutdown_post_sync
, dashutdown
,
1126 NULL
, SHUTDOWN_PRI_DEFAULT
)) == NULL
)
1127 kprintf("dainit: shutdown event registration failed!\n");
1132 daoninvalidate(struct cam_periph
*periph
)
1134 struct da_softc
*softc
;
1138 softc
= (struct da_softc
*)periph
->softc
;
1141 * De-register any async callbacks.
1143 xpt_register_async(0, daasync
, periph
, periph
->path
);
1145 softc
->flags
|= DA_FLAG_PACK_INVALID
;
1148 * Return all queued I/O with ENXIO.
1149 * XXX Handle any transactions queued to the card
1150 * with XPT_ABORT_CCB.
1152 while ((q_bio
= bioq_first(&softc
->bio_queue
)) != NULL
){
1153 bioq_remove(&softc
->bio_queue
, q_bio
);
1154 q_bp
= q_bio
->bio_buf
;
1155 q_bp
->b_resid
= q_bp
->b_bcount
;
1156 q_bp
->b_error
= ENXIO
;
1157 q_bp
->b_flags
|= B_ERROR
;
1160 xpt_print(periph
->path
, "lost device\n");
1164 dacleanup(struct cam_periph
*periph
)
1166 struct da_softc
*softc
;
1168 softc
= (struct da_softc
*)periph
->softc
;
1170 devstat_remove_entry(&softc
->device_stats
);
1171 cam_extend_release(daperiphs
, periph
->unit_number
);
1172 xpt_print(periph
->path
, "removing device entry\n");
1174 * If we can't free the sysctl tree, oh well...
1176 if ((softc
->flags
& DA_FLAG_SCTX_INIT
) != 0
1177 && sysctl_ctx_free(&softc
->sysctl_ctx
) != 0) {
1178 xpt_print(periph
->path
, "can't remove sysctl context\n");
1180 if (softc
->disk
.d_rawdev
) {
1181 cam_periph_unlock(periph
);
1182 disk_destroy(&softc
->disk
);
1183 cam_periph_lock(periph
);
1186 callout_stop(&softc
->sendordered_c
);
1187 kfree(softc
, M_DEVBUF
);
1191 daasync(void *callback_arg
, u_int32_t code
,
1192 struct cam_path
*path
, void *arg
)
1194 struct cam_periph
*periph
;
1196 periph
= (struct cam_periph
*)callback_arg
;
1198 case AC_FOUND_DEVICE
:
1200 struct ccb_getdev
*cgd
;
1201 struct cam_sim
*sim
;
1204 cgd
= (struct ccb_getdev
*)arg
;
1208 if (SID_TYPE(&cgd
->inq_data
) != T_DIRECT
1209 && SID_TYPE(&cgd
->inq_data
) != T_RBC
1210 && SID_TYPE(&cgd
->inq_data
) != T_OPTICAL
)
1214 * Allocate a peripheral instance for
1215 * this device and start the probe
1218 sim
= xpt_path_sim(cgd
->ccb_h
.path
);
1219 status
= cam_periph_alloc(daregister
, daoninvalidate
,
1221 "da", CAM_PERIPH_BIO
,
1222 cgd
->ccb_h
.path
, daasync
,
1223 AC_FOUND_DEVICE
, cgd
);
1225 if (status
!= CAM_REQ_CMP
1226 && status
!= CAM_REQ_INPROG
)
1227 kprintf("daasync: Unable to attach to new device "
1228 "due to status 0x%x\n", status
);
1234 struct da_softc
*softc
;
1235 struct ccb_hdr
*ccbh
;
1237 softc
= (struct da_softc
*)periph
->softc
;
1239 * Don't fail on the expected unit attention
1242 softc
->flags
|= DA_FLAG_RETRY_UA
;
1243 LIST_FOREACH(ccbh
, &softc
->pending_ccbs
, periph_links
.le
)
1244 ccbh
->ccb_state
|= DA_CCB_RETRY_UA
;
1248 cam_periph_async(periph
, code
, path
, arg
);
1254 dasysctlinit(void *context
, int pending
)
1256 struct cam_periph
*periph
;
1257 struct da_softc
*softc
;
1258 char tmpstr
[80], tmpstr2
[80];
1260 periph
= (struct cam_periph
*)context
;
1261 if (cam_periph_acquire(periph
) != CAM_REQ_CMP
)
1264 softc
= (struct da_softc
*)periph
->softc
;
1265 ksnprintf(tmpstr
, sizeof(tmpstr
), "CAM DA unit %d", periph
->unit_number
);
1266 ksnprintf(tmpstr2
, sizeof(tmpstr2
), "%d", periph
->unit_number
);
1268 sysctl_ctx_init(&softc
->sysctl_ctx
);
1269 softc
->flags
|= DA_FLAG_SCTX_INIT
;
1270 softc
->sysctl_tree
= SYSCTL_ADD_NODE(&softc
->sysctl_ctx
,
1271 SYSCTL_STATIC_CHILDREN(_kern_cam_da
), OID_AUTO
, tmpstr2
,
1272 CTLFLAG_RD
, 0, tmpstr
);
1273 if (softc
->sysctl_tree
== NULL
) {
1274 kprintf("dasysctlinit: unable to allocate sysctl tree\n");
1275 cam_periph_release(periph
);
1280 * Now register the sysctl handler, so the user can the value on
1283 SYSCTL_ADD_PROC(&softc
->sysctl_ctx
,SYSCTL_CHILDREN(softc
->sysctl_tree
),
1284 OID_AUTO
, "minimum_cmd_size", CTLTYPE_INT
| CTLFLAG_RW
,
1285 &softc
->minimum_cmd_size
, 0, dacmdsizesysctl
, "I",
1286 "Minimum CDB size");
1288 cam_periph_release(periph
);
1292 dacmdsizesysctl(SYSCTL_HANDLER_ARGS
)
1296 value
= *(int *)arg1
;
1298 error
= sysctl_handle_int(oidp
, &value
, 0, req
);
1301 || (req
->newptr
== NULL
))
1305 * Acceptable values here are 6, 10 or 12, or 16.
1309 else if ((value
> 6)
1312 else if ((value
> 10)
1315 else if (value
> 12)
1318 *(int *)arg1
= value
;
1324 daregister(struct cam_periph
*periph
, void *arg
)
1326 struct da_softc
*softc
;
1327 struct ccb_pathinq cpi
;
1328 struct ccb_getdev
*cgd
;
1332 cgd
= (struct ccb_getdev
*)arg
;
1333 if (periph
== NULL
) {
1334 kprintf("daregister: periph was NULL!!\n");
1335 return(CAM_REQ_CMP_ERR
);
1339 kprintf("daregister: no getdev CCB, can't register device\n");
1340 return(CAM_REQ_CMP_ERR
);
1343 softc
= kmalloc(sizeof(*softc
), M_DEVBUF
, M_INTWAIT
| M_ZERO
);
1344 LIST_INIT(&softc
->pending_ccbs
);
1345 softc
->state
= DA_STATE_PROBE
;
1346 bioq_init(&softc
->bio_queue
);
1347 if (SID_IS_REMOVABLE(&cgd
->inq_data
))
1348 softc
->flags
|= DA_FLAG_PACK_REMOVABLE
;
1349 if ((cgd
->inq_data
.flags
& SID_CmdQue
) != 0)
1350 softc
->flags
|= DA_FLAG_TAGGED_QUEUING
;
1352 periph
->softc
= softc
;
1354 cam_extend_set(daperiphs
, periph
->unit_number
, periph
);
1357 * See if this device has any quirks.
1359 match
= cam_quirkmatch((caddr_t
)&cgd
->inq_data
,
1360 (caddr_t
)da_quirk_table
,
1361 sizeof(da_quirk_table
)/sizeof(*da_quirk_table
),
1362 sizeof(*da_quirk_table
), scsi_inquiry_match
);
1365 softc
->quirks
= ((struct da_quirk_entry
*)match
)->quirks
;
1367 softc
->quirks
= DA_Q_NONE
;
1369 TASK_INIT(&softc
->sysctl_task
, 0, dasysctlinit
, periph
);
1371 /* Check if the SIM does not want 6 byte commands */
1372 xpt_setup_ccb(&cpi
.ccb_h
, periph
->path
, /*priority*/1);
1373 cpi
.ccb_h
.func_code
= XPT_PATH_INQ
;
1374 xpt_action((union ccb
*)&cpi
);
1375 if (cpi
.ccb_h
.status
== CAM_REQ_CMP
&& (cpi
.hba_misc
& PIM_NO_6_BYTE
))
1376 softc
->quirks
|= DA_Q_NO_6_BYTE
;
1379 * RBC devices don't have to support READ(6), only READ(10).
1381 if (softc
->quirks
& DA_Q_NO_6_BYTE
|| SID_TYPE(&cgd
->inq_data
) == T_RBC
)
1382 softc
->minimum_cmd_size
= 10;
1384 softc
->minimum_cmd_size
= 6;
1387 * Load the user's default, if any.
1389 ksnprintf(tmpstr
, sizeof(tmpstr
), "kern.cam.da.%d.minimum_cmd_size",
1390 periph
->unit_number
);
1391 TUNABLE_INT_FETCH(tmpstr
, &softc
->minimum_cmd_size
);
1394 * 6, 10, 12, and 16 are the currently permissible values.
1396 if (softc
->minimum_cmd_size
< 6)
1397 softc
->minimum_cmd_size
= 6;
1398 else if ((softc
->minimum_cmd_size
> 6)
1399 && (softc
->minimum_cmd_size
<= 10))
1400 softc
->minimum_cmd_size
= 10;
1401 else if ((softc
->minimum_cmd_size
> 10)
1402 && (softc
->minimum_cmd_size
<= 12))
1403 softc
->minimum_cmd_size
= 12;
1404 else if (softc
->minimum_cmd_size
> 12)
1405 softc
->minimum_cmd_size
= 16;
1408 * The DA driver supports a blocksize, but
1409 * we don't know the blocksize until we do
1410 * a read capacity. So, set a flag to
1411 * indicate that the blocksize is
1412 * unavailable right now. We'll clear the
1413 * flag as soon as we've done a read capacity.
1415 devstat_add_entry(&softc
->device_stats
, "da",
1416 periph
->unit_number
, 0,
1417 DEVSTAT_BS_UNAVAILABLE
,
1418 SID_TYPE(&cgd
->inq_data
) | DEVSTAT_TYPE_IF_SCSI
,
1419 DEVSTAT_PRIORITY_DISK
);
1422 * Register this media as a disk
1425 CAM_SIM_UNLOCK(periph
->sim
);
1426 disk_create(periph
->unit_number
, &softc
->disk
, &da_ops
);
1427 softc
->disk
.d_rawdev
->si_iosize_max
= MAXPHYS
;
1428 CAM_SIM_LOCK(periph
->sim
);
1431 * Add async callbacks for bus reset and
1432 * bus device reset calls. I don't bother
1433 * checking if this fails as, in most cases,
1434 * the system will function just fine without
1435 * them and the only alternative would be to
1436 * not attach the device on failure.
1438 xpt_register_async(AC_SENT_BDR
| AC_BUS_RESET
| AC_LOST_DEVICE
,
1439 daasync
, periph
, periph
->path
);
1442 * Take an exclusive refcount on the periph while dastart is called
1443 * to finish the probe. The reference will be dropped in dadone at
1446 cam_periph_hold(periph
, 0);
1447 xpt_schedule(periph
, /*priority*/5);
1450 * Schedule a periodic event to occasionally send an
1451 * ordered tag to a device.
1453 callout_init(&softc
->sendordered_c
);
1454 callout_reset(&softc
->sendordered_c
,
1455 (DA_DEFAULT_TIMEOUT
* hz
) / DA_ORDEREDTAG_INTERVAL
,
1456 dasendorderedtag
, softc
);
1458 return(CAM_REQ_CMP
);
1462 dastart(struct cam_periph
*periph
, union ccb
*start_ccb
)
1464 struct da_softc
*softc
;
1466 softc
= (struct da_softc
*)periph
->softc
;
1468 switch (softc
->state
) {
1469 case DA_STATE_NORMAL
:
1471 /* Pull a buffer from the queue and get going on it */
1476 * See if there is a buf with work for us to do..
1478 bio
= bioq_first(&softc
->bio_queue
);
1479 if (periph
->immediate_priority
<= periph
->pinfo
.priority
) {
1480 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE
,
1481 ("queuing for immediate ccb\n"));
1482 start_ccb
->ccb_h
.ccb_state
= DA_CCB_WAITING
;
1483 SLIST_INSERT_HEAD(&periph
->ccb_list
, &start_ccb
->ccb_h
,
1485 periph
->immediate_priority
= CAM_PRIORITY_NONE
;
1486 wakeup(&periph
->ccb_list
);
1487 } else if (bio
== NULL
) {
1488 xpt_release_ccb(start_ccb
);
1492 bioq_remove(&softc
->bio_queue
, bio
);
1495 devstat_start_transaction(&softc
->device_stats
);
1497 if ((bp
->b_flags
& B_ORDERED
) != 0
1498 || (softc
->flags
& DA_FLAG_NEED_OTAG
) != 0) {
1499 softc
->flags
&= ~DA_FLAG_NEED_OTAG
;
1500 softc
->ordered_tag_count
++;
1501 tag_code
= MSG_ORDERED_Q_TAG
;
1503 tag_code
= MSG_SIMPLE_Q_TAG
;
1506 KKASSERT(bio
->bio_offset
% softc
->params
.secsize
== 0);
1508 scsi_read_write(&start_ccb
->csio
,
1509 /*retries*/da_retry_count
,
1512 (bp
->b_cmd
== BUF_CMD_READ
),
1514 softc
->minimum_cmd_size
,
1515 bio
->bio_offset
/ softc
->params
.secsize
,
1516 bp
->b_bcount
/ softc
->params
.secsize
,
1519 /*sense_len*/SSD_FULL_SIZE
,
1520 da_default_timeout
* 1000);
1521 start_ccb
->ccb_h
.ccb_state
= DA_CCB_BUFFER_IO
;
1524 * Block out any asyncronous callbacks
1525 * while we touch the pending ccb list.
1527 LIST_INSERT_HEAD(&softc
->pending_ccbs
,
1528 &start_ccb
->ccb_h
, periph_links
.le
);
1530 softc
->outstanding_cmds
++;
1531 /* We expect a unit attention from this device */
1532 if ((softc
->flags
& DA_FLAG_RETRY_UA
) != 0) {
1533 start_ccb
->ccb_h
.ccb_state
|= DA_CCB_RETRY_UA
;
1534 softc
->flags
&= ~DA_FLAG_RETRY_UA
;
1537 start_ccb
->ccb_h
.ccb_bio
= bio
;
1538 bio
= bioq_first(&softc
->bio_queue
);
1540 xpt_action(start_ccb
);
1544 /* Have more work to do, so ensure we stay scheduled */
1545 xpt_schedule(periph
, /* XXX priority */1);
1549 case DA_STATE_PROBE
:
1551 struct ccb_scsiio
*csio
;
1552 struct scsi_read_capacity_data
*rcap
;
1554 rcap
= kmalloc(sizeof(*rcap
), M_SCSIDA
, M_INTWAIT
| M_ZERO
);
1555 csio
= &start_ccb
->csio
;
1556 scsi_read_capacity(csio
,
1563 start_ccb
->ccb_h
.ccb_bio
= NULL
;
1564 start_ccb
->ccb_h
.ccb_state
= DA_CCB_PROBE
;
1565 xpt_action(start_ccb
);
1568 case DA_STATE_PROBE2
:
1570 struct ccb_scsiio
*csio
;
1571 struct scsi_read_capacity_data_long
*rcaplong
;
1573 rcaplong
= (struct scsi_read_capacity_data_long
*)
1574 kmalloc(sizeof(*rcaplong
), M_SCSIDA
, M_INTWAIT
| M_ZERO
);
1575 if (rcaplong
== NULL
) {
1576 kprintf("dastart: Couldn't allocate read_capacity\n");
1577 /* da_free_periph??? */
1580 csio
= &start_ccb
->csio
;
1581 scsi_read_capacity_16(csio
,
1584 /*tag_action*/ MSG_SIMPLE_Q_TAG
,
1589 /*sense_len*/ SSD_FULL_SIZE
,
1591 start_ccb
->ccb_h
.ccb_bio
= NULL
;
1592 start_ccb
->ccb_h
.ccb_state
= DA_CCB_PROBE2
;
1593 xpt_action(start_ccb
);
1600 cmd6workaround(union ccb
*ccb
)
1602 struct scsi_rw_6 cmd6
;
1603 struct scsi_rw_10
*cmd10
;
1604 struct da_softc
*softc
;
1608 cdb
= ccb
->csio
.cdb_io
.cdb_bytes
;
1610 /* Translation only possible if CDB is an array and cmd is R/W6 */
1611 if ((ccb
->ccb_h
.flags
& CAM_CDB_POINTER
) != 0 ||
1612 (*cdb
!= READ_6
&& *cdb
!= WRITE_6
))
1615 xpt_print(ccb
->ccb_h
.path
, "READ(6)/WRITE(6) not supported, "
1616 "increasing minimum_cmd_size to 10.\n");
1617 softc
= (struct da_softc
*)xpt_path_periph(ccb
->ccb_h
.path
)->softc
;
1618 softc
->minimum_cmd_size
= 10;
1620 bcopy(cdb
, &cmd6
, sizeof(struct scsi_rw_6
));
1621 cmd10
= (struct scsi_rw_10
*)cdb
;
1622 cmd10
->opcode
= (cmd6
.opcode
== READ_6
) ? READ_10
: WRITE_10
;
1624 scsi_ulto4b(scsi_3btoul(cmd6
.addr
), cmd10
->addr
);
1625 cmd10
->reserved
= 0;
1626 scsi_ulto2b(cmd6
.length
, cmd10
->length
);
1627 cmd10
->control
= cmd6
.control
;
1628 ccb
->csio
.cdb_len
= sizeof(*cmd10
);
1630 /* Requeue request, unfreezing queue if necessary */
1631 frozen
= (ccb
->ccb_h
.status
& CAM_DEV_QFRZN
) != 0;
1632 ccb
->ccb_h
.status
= CAM_REQUEUE_REQ
;
1635 cam_release_devq(ccb
->ccb_h
.path
,
1639 /*getcount_only*/0);
1645 dadone(struct cam_periph
*periph
, union ccb
*done_ccb
)
1647 struct da_softc
*softc
;
1648 struct ccb_scsiio
*csio
;
1650 softc
= (struct da_softc
*)periph
->softc
;
1651 csio
= &done_ccb
->csio
;
1652 switch (csio
->ccb_h
.ccb_state
& DA_CCB_TYPE_MASK
) {
1653 case DA_CCB_BUFFER_IO
:
1658 bio
= (struct bio
*)done_ccb
->ccb_h
.ccb_bio
;
1660 if ((done_ccb
->ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
1664 if ((csio
->ccb_h
.ccb_state
& DA_CCB_RETRY_UA
) != 0)
1669 error
= daerror(done_ccb
, CAM_RETRY_SELTO
, sf
);
1670 if (error
== ERESTART
) {
1672 * A retry was scheuled, so
1681 if (error
== ENXIO
) {
1683 * Catastrophic error. Mark our pack as
1687 * XXX See if this is really a media
1690 xpt_print(periph
->path
,
1691 "Invalidating pack\n");
1692 softc
->flags
|= DA_FLAG_PACK_INVALID
;
1696 * return all queued I/O with EIO, so that
1697 * the client can retry these I/Os in the
1698 * proper order should it attempt to recover.
1700 while ((q_bio
= bioq_first(&softc
->bio_queue
))
1702 bioq_remove(&softc
->bio_queue
, q_bio
);
1703 q_bp
= q_bio
->bio_buf
;
1704 q_bp
->b_resid
= q_bp
->b_bcount
;
1705 q_bp
->b_error
= EIO
;
1706 q_bp
->b_flags
|= B_ERROR
;
1709 bp
->b_error
= error
;
1710 bp
->b_resid
= bp
->b_bcount
;
1711 bp
->b_flags
|= B_ERROR
;
1713 bp
->b_resid
= csio
->resid
;
1715 if (bp
->b_resid
!= 0)
1716 bp
->b_flags
|= B_ERROR
;
1718 if ((done_ccb
->ccb_h
.status
& CAM_DEV_QFRZN
) != 0)
1719 cam_release_devq(done_ccb
->ccb_h
.path
,
1723 /*getcount_only*/0);
1725 if ((done_ccb
->ccb_h
.status
& CAM_DEV_QFRZN
) != 0)
1726 panic("REQ_CMP with QFRZN");
1727 bp
->b_resid
= csio
->resid
;
1728 if (csio
->resid
> 0)
1729 bp
->b_flags
|= B_ERROR
;
1733 * Block out any asyncronous callbacks
1734 * while we touch the pending ccb list.
1736 LIST_REMOVE(&done_ccb
->ccb_h
, periph_links
.le
);
1737 softc
->outstanding_cmds
--;
1738 if (softc
->outstanding_cmds
== 0)
1739 softc
->flags
|= DA_FLAG_WENT_IDLE
;
1741 devstat_end_transaction_buf(&softc
->device_stats
, bp
);
1748 struct scsi_read_capacity_data
*rdcap
;
1749 struct scsi_read_capacity_data_long
*rcaplong
;
1750 char announce_buf
[80];
1754 if (softc
->state
== DA_STATE_PROBE
)
1755 rdcap
=(struct scsi_read_capacity_data
*)csio
->data_ptr
;
1757 rcaplong
= (struct scsi_read_capacity_data_long
*)
1760 if ((csio
->ccb_h
.status
& CAM_STATUS_MASK
) == CAM_REQ_CMP
) {
1761 struct disk_params
*dp
;
1762 uint32_t block_size
;
1765 if (softc
->state
== DA_STATE_PROBE
) {
1766 block_size
= scsi_4btoul(rdcap
->length
);
1767 maxsector
= scsi_4btoul(rdcap
->addr
);
1770 * According to SBC-2, if the standard 10
1771 * byte READ CAPACITY command returns 2^32,
1772 * we should issue the 16 byte version of
1773 * the command, since the device in question
1774 * has more sectors than can be represented
1775 * with the short version of the command.
1777 if (maxsector
== 0xffffffff) {
1778 softc
->state
= DA_STATE_PROBE2
;
1779 kfree(rdcap
, M_SCSIDA
);
1780 xpt_release_ccb(done_ccb
);
1781 xpt_schedule(periph
, /*priority*/5);
1785 block_size
= scsi_4btoul(rcaplong
->length
);
1786 maxsector
= scsi_8btou64(rcaplong
->addr
);
1788 dasetgeom(periph
, block_size
, maxsector
);
1789 dp
= &softc
->params
;
1790 ksnprintf(announce_buf
, sizeof(announce_buf
),
1791 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1792 (uintmax_t) (((uintmax_t)dp
->secsize
*
1793 dp
->sectors
) / (1024*1024)),
1794 (uintmax_t)dp
->sectors
,
1795 dp
->secsize
, dp
->heads
, dp
->secs_per_track
,
1800 announce_buf
[0] = '\0';
1803 * Retry any UNIT ATTENTION type errors. They
1804 * are expected at boot.
1806 error
= daerror(done_ccb
, CAM_RETRY_SELTO
,
1807 SF_RETRY_UA
|SF_NO_PRINT
);
1808 if (error
== ERESTART
) {
1810 * A retry was scheuled, so
1814 } else if (error
!= 0) {
1815 struct scsi_sense_data
*sense
;
1817 int sense_key
, error_code
;
1820 struct ccb_getdev cgd
;
1822 /* Don't wedge this device's queue */
1823 status
= done_ccb
->ccb_h
.status
;
1824 if ((status
& CAM_DEV_QFRZN
) != 0)
1825 cam_release_devq(done_ccb
->ccb_h
.path
,
1829 /*getcount_only*/0);
1832 xpt_setup_ccb(&cgd
.ccb_h
,
1833 done_ccb
->ccb_h
.path
,
1835 cgd
.ccb_h
.func_code
= XPT_GDEV_TYPE
;
1836 xpt_action((union ccb
*)&cgd
);
1838 if (((csio
->ccb_h
.flags
& CAM_SENSE_PHYS
) != 0)
1839 || ((csio
->ccb_h
.flags
& CAM_SENSE_PTR
) != 0)
1840 || ((status
& CAM_AUTOSNS_VALID
) == 0))
1846 sense
= &csio
->sense_data
;
1847 scsi_extract_sense(sense
, &error_code
,
1852 * Attach to anything that claims to be a
1853 * direct access or optical disk device,
1854 * as long as it doesn't return a "Logical
1855 * unit not supported" (0x25) error.
1857 if ((have_sense
) && (asc
!= 0x25)
1858 && (error_code
== SSD_CURRENT_ERROR
)) {
1859 const char *sense_key_desc
;
1860 const char *asc_desc
;
1862 scsi_sense_desc(sense_key
, asc
, ascq
,
1866 ksnprintf(announce_buf
,
1867 sizeof(announce_buf
),
1868 "Attempt to query device "
1869 "size failed: %s, %s",
1877 xpt_print(periph
->path
,
1878 "got CAM status %#x\n",
1879 done_ccb
->ccb_h
.status
);
1882 xpt_print(periph
->path
, "fatal error, "
1883 "failed to attach to device\n");
1886 * Free up resources.
1888 cam_periph_invalidate(periph
);
1892 kfree(csio
->data_ptr
, M_SCSIDA
);
1893 if (announce_buf
[0] != '\0') {
1894 xpt_announce_periph(periph
, announce_buf
);
1896 * Create our sysctl variables, now that we know
1897 * we have successfully attached.
1899 taskqueue_enqueue(taskqueue_thread
[mycpuid
],
1900 &softc
->sysctl_task
);
1902 softc
->state
= DA_STATE_NORMAL
;
1904 * Since our peripheral may be invalidated by an error
1905 * above or an external event, we must release our CCB
1906 * before releasing the probe lock on the peripheral.
1907 * The peripheral will only go away once the last lock
1908 * is removed, and we need it around for the CCB release
1911 xpt_release_ccb(done_ccb
);
1912 cam_periph_unhold(periph
);
1915 case DA_CCB_WAITING
:
1917 /* Caller will release the CCB */
1918 wakeup(&done_ccb
->ccb_h
.cbfcnp
);
1922 /* No-op. We're polling */
1927 xpt_release_ccb(done_ccb
);
1931 daerror(union ccb
*ccb
, u_int32_t cam_flags
, u_int32_t sense_flags
)
1933 struct da_softc
*softc
;
1934 struct cam_periph
*periph
;
1937 periph
= xpt_path_periph(ccb
->ccb_h
.path
);
1938 softc
= (struct da_softc
*)periph
->softc
;
1941 * Automatically detect devices that do not support
1942 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
1945 if ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) == CAM_REQ_INVALID
) {
1946 error
= cmd6workaround(ccb
);
1947 } else if (((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) ==
1948 CAM_SCSI_STATUS_ERROR
)
1949 && (ccb
->ccb_h
.status
& CAM_AUTOSNS_VALID
)
1950 && (ccb
->csio
.scsi_status
== SCSI_STATUS_CHECK_COND
)
1951 && ((ccb
->ccb_h
.flags
& CAM_SENSE_PHYS
) == 0)
1952 && ((ccb
->ccb_h
.flags
& CAM_SENSE_PTR
) == 0)) {
1953 int sense_key
, error_code
, asc
, ascq
;
1955 scsi_extract_sense(&ccb
->csio
.sense_data
,
1956 &error_code
, &sense_key
, &asc
, &ascq
);
1957 if (sense_key
== SSD_KEY_ILLEGAL_REQUEST
)
1958 error
= cmd6workaround(ccb
);
1960 if (error
== ERESTART
)
1965 * Until we have a better way of doing pack validation,
1966 * don't treat UAs as errors.
1968 sense_flags
|= SF_RETRY_UA
;
1969 return(cam_periph_error(ccb
, cam_flags
, sense_flags
,
1970 &softc
->saved_ccb
));
1974 daprevent(struct cam_periph
*periph
, int action
)
1976 struct da_softc
*softc
;
1980 softc
= (struct da_softc
*)periph
->softc
;
1982 if (((action
== PR_ALLOW
)
1983 && (softc
->flags
& DA_FLAG_PACK_LOCKED
) == 0)
1984 || ((action
== PR_PREVENT
)
1985 && (softc
->flags
& DA_FLAG_PACK_LOCKED
) != 0)) {
1989 ccb
= cam_periph_getccb(periph
, /*priority*/1);
1991 scsi_prevent(&ccb
->csio
,
1999 error
= cam_periph_runccb(ccb
, /*error_routine*/NULL
, CAM_RETRY_SELTO
,
2000 SF_RETRY_UA
, &softc
->device_stats
);
2003 if (action
== PR_ALLOW
)
2004 softc
->flags
&= ~DA_FLAG_PACK_LOCKED
;
2006 softc
->flags
|= DA_FLAG_PACK_LOCKED
;
2009 xpt_release_ccb(ccb
);
2013 dagetcapacity(struct cam_periph
*periph
)
2015 struct da_softc
*softc
;
2017 struct scsi_read_capacity_data
*rcap
;
2018 struct scsi_read_capacity_data_long
*rcaplong
;
2023 softc
= (struct da_softc
*)periph
->softc
;
2028 /* Do a read capacity */
2029 rcap
= (struct scsi_read_capacity_data
*)kmalloc(sizeof(*rcaplong
),
2030 M_SCSIDA
, M_INTWAIT
);
2032 ccb
= cam_periph_getccb(periph
, /*priority*/1);
2033 scsi_read_capacity(&ccb
->csio
,
2040 ccb
->ccb_h
.ccb_bio
= NULL
;
2042 error
= cam_periph_runccb(ccb
, daerror
,
2043 /*cam_flags*/CAM_RETRY_SELTO
,
2044 /*sense_flags*/SF_RETRY_UA
,
2045 &softc
->device_stats
);
2047 if ((ccb
->ccb_h
.status
& CAM_DEV_QFRZN
) != 0)
2048 cam_release_devq(ccb
->ccb_h
.path
,
2052 /*getcount_only*/0);
2055 block_len
= scsi_4btoul(rcap
->length
);
2056 maxsector
= scsi_4btoul(rcap
->addr
);
2058 if (maxsector
!= 0xffffffff)
2063 rcaplong
= (struct scsi_read_capacity_data_long
*)rcap
;
2065 scsi_read_capacity_16(&ccb
->csio
,
2068 /*tag_action*/ MSG_SIMPLE_Q_TAG
,
2073 /*sense_len*/ SSD_FULL_SIZE
,
2075 ccb
->ccb_h
.ccb_bio
= NULL
;
2077 error
= cam_periph_runccb(ccb
, daerror
,
2078 /*cam_flags*/CAM_RETRY_SELTO
,
2079 /*sense_flags*/SF_RETRY_UA
,
2080 &softc
->device_stats
);
2082 if ((ccb
->ccb_h
.status
& CAM_DEV_QFRZN
) != 0)
2083 cam_release_devq(ccb
->ccb_h
.path
,
2087 /*getcount_only*/0);
2090 block_len
= scsi_4btoul(rcaplong
->length
);
2091 maxsector
= scsi_8btou64(rcaplong
->addr
);
2097 dasetgeom(periph
, block_len
, maxsector
);
2099 xpt_release_ccb(ccb
);
2101 kfree(rcap
, M_SCSIDA
);
2107 dasetgeom(struct cam_periph
*periph
, uint32_t block_len
, uint64_t maxsector
)
2109 struct ccb_calc_geometry ccg
;
2110 struct da_softc
*softc
;
2111 struct disk_params
*dp
;
2113 softc
= (struct da_softc
*)periph
->softc
;
2115 dp
= &softc
->params
;
2116 dp
->secsize
= block_len
;
2117 dp
->sectors
= maxsector
+ 1;
2119 * Have the controller provide us with a geometry
2120 * for this disk. The only time the geometry
2121 * matters is when we boot and the controller
2122 * is the only one knowledgeable enough to come
2123 * up with something that will make this a bootable
2126 xpt_setup_ccb(&ccg
.ccb_h
, periph
->path
, /*priority*/1);
2127 ccg
.ccb_h
.func_code
= XPT_CALC_GEOMETRY
;
2128 ccg
.block_size
= dp
->secsize
;
2129 ccg
.volume_size
= dp
->sectors
;
2131 ccg
.secs_per_track
= 0;
2133 xpt_action((union ccb
*)&ccg
);
2134 if ((ccg
.ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
2136 * We don't know what went wrong here- but just pick
2137 * a geometry so we don't have nasty things like divide
2141 dp
->secs_per_track
= 255;
2142 dp
->cylinders
= dp
->sectors
/ (255 * 255);
2143 if (dp
->cylinders
== 0) {
2147 dp
->heads
= ccg
.heads
;
2148 dp
->secs_per_track
= ccg
.secs_per_track
;
2149 dp
->cylinders
= ccg
.cylinders
;
2154 dasendorderedtag(void *arg
)
2156 struct da_softc
*softc
= arg
;
2158 if (da_send_ordered
) {
2159 if ((softc
->ordered_tag_count
== 0)
2160 && ((softc
->flags
& DA_FLAG_WENT_IDLE
) == 0)) {
2161 softc
->flags
|= DA_FLAG_NEED_OTAG
;
2163 if (softc
->outstanding_cmds
> 0)
2164 softc
->flags
&= ~DA_FLAG_WENT_IDLE
;
2166 softc
->ordered_tag_count
= 0;
2168 /* Queue us up again */
2169 callout_reset(&softc
->sendordered_c
,
2170 (DA_DEFAULT_TIMEOUT
* hz
) / DA_ORDEREDTAG_INTERVAL
,
2171 dasendorderedtag
, softc
);
2175 * Step through all DA peripheral drivers, and if the device is still open,
2176 * sync the disk cache to physical media.
2179 dashutdown(void * arg
, int howto
)
2181 struct cam_periph
*periph
;
2182 struct da_softc
*softc
;
2184 TAILQ_FOREACH(periph
, &dadriver
.units
, unit_links
) {
2187 cam_periph_lock(periph
);
2188 softc
= (struct da_softc
*)periph
->softc
;
2191 * We only sync the cache if the drive is still open, and
2192 * if the drive is capable of it..
2194 if (((softc
->flags
& DA_FLAG_OPEN
) == 0)
2195 || (softc
->quirks
& DA_Q_NO_SYNC_CACHE
)) {
2196 cam_periph_unlock(periph
);
2200 xpt_setup_ccb(&ccb
.ccb_h
, periph
->path
, /*priority*/1);
2202 ccb
.ccb_h
.ccb_state
= DA_CCB_DUMP
;
2203 scsi_synchronize_cache(&ccb
.csio
,
2207 /*begin_lba*/0, /* whole disk */
2212 xpt_polled_action(&ccb
);
2214 if ((ccb
.ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
2215 if (((ccb
.ccb_h
.status
& CAM_STATUS_MASK
) ==
2216 CAM_SCSI_STATUS_ERROR
)
2217 && (ccb
.csio
.scsi_status
== SCSI_STATUS_CHECK_COND
)){
2218 int error_code
, sense_key
, asc
, ascq
;
2220 scsi_extract_sense(&ccb
.csio
.sense_data
,
2221 &error_code
, &sense_key
,
2224 if (sense_key
!= SSD_KEY_ILLEGAL_REQUEST
)
2225 scsi_sense_print(&ccb
.csio
);
2227 xpt_print(periph
->path
, "Synchronize "
2228 "cache failed, status == 0x%x, scsi status "
2229 "== 0x%x\n", ccb
.ccb_h
.status
,
2230 ccb
.csio
.scsi_status
);
2234 if ((ccb
.ccb_h
.status
& CAM_DEV_QFRZN
) != 0)
2235 cam_release_devq(ccb
.ccb_h
.path
,
2239 /*getcount_only*/0);
2241 cam_periph_unlock(periph
);
2245 #else /* !_KERNEL */
2248 * XXX This is only left out of the kernel build to silence warnings. If,
2249 * for some reason this function is used in the kernel, the ifdefs should
2250 * be moved so it is included both in the kernel and userland.
2253 scsi_format_unit(struct ccb_scsiio
*csio
, u_int32_t retries
,
2254 void (*cbfcnp
)(struct cam_periph
*, union ccb
*),
2255 u_int8_t tag_action
, u_int8_t byte2
, u_int16_t ileave
,
2256 u_int8_t
*data_ptr
, u_int32_t dxfer_len
, u_int8_t sense_len
,
2259 struct scsi_format_unit
*scsi_cmd
;
2261 scsi_cmd
= (struct scsi_format_unit
*)&csio
->cdb_io
.cdb_bytes
;
2262 scsi_cmd
->opcode
= FORMAT_UNIT
;
2263 scsi_cmd
->byte2
= byte2
;
2264 scsi_ulto2b(ileave
, scsi_cmd
->interleave
);
2269 /*flags*/ (dxfer_len
> 0) ? CAM_DIR_OUT
: CAM_DIR_NONE
,
2278 #endif /* _KERNEL */