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.59 2008/08/29 20:08:40 dillon 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>
63 #include <sys/camlib.h>
65 #include "../cam_ccb.h"
66 #include "../cam_extend.h"
67 #include "../cam_periph.h"
68 #include "../cam_xpt_periph.h"
69 #include "../cam_sim.h"
71 #include "scsi_message.h"
85 DA_FLAG_PACK_INVALID
= 0x001,
86 DA_FLAG_NEW_PACK
= 0x002,
87 DA_FLAG_PACK_LOCKED
= 0x004,
88 DA_FLAG_PACK_REMOVABLE
= 0x008,
89 DA_FLAG_TAGGED_QUEUING
= 0x010,
90 DA_FLAG_NEED_OTAG
= 0x020,
91 DA_FLAG_WENT_IDLE
= 0x040,
92 DA_FLAG_RETRY_UA
= 0x080,
94 DA_FLAG_SCTX_INIT
= 0x200
99 DA_Q_NO_SYNC_CACHE
= 0x01,
100 DA_Q_NO_6_BYTE
= 0x02,
101 DA_Q_NO_PREVENT
= 0x04
106 DA_CCB_PROBE2
= 0x02,
107 DA_CCB_BUFFER_IO
= 0x03,
108 DA_CCB_WAITING
= 0x04,
110 DA_CCB_TYPE_MASK
= 0x0F,
111 DA_CCB_RETRY_UA
= 0x10
114 /* Offsets into our private area for storing information */
115 #define ccb_state ppriv_field0
116 #define ccb_bio ppriv_ptr1
121 u_int8_t secs_per_track
;
122 u_int32_t secsize
; /* Number of bytes/sector */
123 u_int64_t sectors
; /* total number sectors */
127 struct bio_queue_head bio_queue
;
128 struct devstat device_stats
;
129 SLIST_ENTRY(da_softc
) links
;
130 LIST_HEAD(, ccb_hdr
) pending_ccbs
;
134 int minimum_cmd_size
;
135 int ordered_tag_count
;
136 int outstanding_cmds
;
137 struct disk_params params
;
140 struct task sysctl_task
;
141 struct sysctl_ctx_list sysctl_ctx
;
142 struct sysctl_oid
*sysctl_tree
;
143 struct callout sendordered_c
;
146 struct da_quirk_entry
{
147 struct scsi_inquiry_pattern inq_pat
;
151 static const char quantum
[] = "QUANTUM";
152 static const char microp
[] = "MICROP";
154 static struct da_quirk_entry da_quirk_table
[] =
156 /* SPI, FC devices */
159 * Fujitsu M2513A MO drives.
160 * Tested devices: M2513A2 firmware versions 1200 & 1300.
161 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
162 * Reported by: W.Scholten <whs@xs4all.nl>
164 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "FUJITSU", "M2513A", "*"},
165 /*quirks*/ DA_Q_NO_SYNC_CACHE
169 {T_OPTICAL
, SIP_MEDIA_REMOVABLE
, "FUJITSU", "M2513A", "*"},
170 /*quirks*/ DA_Q_NO_SYNC_CACHE
174 * This particular Fujitsu drive doesn't like the
175 * synchronize cache command.
176 * Reported by: Tom Jackson <toj@gorilla.net>
178 {T_DIRECT
, SIP_MEDIA_FIXED
, "FUJITSU", "M2954*", "*"},
179 /*quirks*/ DA_Q_NO_SYNC_CACHE
183 * This drive doesn't like the synchronize cache command
184 * either. Reported by: Matthew Jacob <mjacob@feral.com>
185 * in NetBSD PR kern/6027, August 24, 1998.
187 {T_DIRECT
, SIP_MEDIA_FIXED
, microp
, "2217*", "*"},
188 /*quirks*/ DA_Q_NO_SYNC_CACHE
192 * This drive doesn't like the synchronize cache command
193 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
196 {T_DIRECT
, SIP_MEDIA_FIXED
, microp
, "2112*", "*"},
197 /*quirks*/ DA_Q_NO_SYNC_CACHE
201 * Doesn't like the synchronize cache command.
202 * Reported by: Blaz Zupan <blaz@gold.amis.net>
204 {T_DIRECT
, SIP_MEDIA_FIXED
, "NEC", "D3847*", "*"},
205 /*quirks*/ DA_Q_NO_SYNC_CACHE
209 * Doesn't like the synchronize cache command.
210 * Reported by: Blaz Zupan <blaz@gold.amis.net>
212 {T_DIRECT
, SIP_MEDIA_FIXED
, quantum
, "MAVERICK 540S", "*"},
213 /*quirks*/ DA_Q_NO_SYNC_CACHE
217 * Doesn't like the synchronize cache command.
219 {T_DIRECT
, SIP_MEDIA_FIXED
, quantum
, "LPS525S", "*"},
220 /*quirks*/ DA_Q_NO_SYNC_CACHE
224 * Doesn't like the synchronize cache command.
225 * Reported by: walter@pelissero.de
227 {T_DIRECT
, SIP_MEDIA_FIXED
, quantum
, "LPS540S", "*"},
228 /*quirks*/ DA_Q_NO_SYNC_CACHE
232 * Doesn't work correctly with 6 byte reads/writes.
233 * Returns illegal request, and points to byte 9 of the
235 * Reported by: Adam McDougall <bsdx@spawnet.com>
237 {T_DIRECT
, SIP_MEDIA_FIXED
, quantum
, "VIKING 4*", "*"},
238 /*quirks*/ DA_Q_NO_6_BYTE
242 {T_DIRECT
, SIP_MEDIA_FIXED
, quantum
, "VIKING 2*", "*"},
243 /*quirks*/ DA_Q_NO_6_BYTE
247 * Doesn't like the synchronize cache command.
248 * Reported by: walter@pelissero.de
250 {T_DIRECT
, SIP_MEDIA_FIXED
, "CONNER", "CP3500*", "*"},
251 /*quirks*/ DA_Q_NO_SYNC_CACHE
255 * The CISS RAID controllers do not support SYNC_CACHE
257 {T_DIRECT
, SIP_MEDIA_FIXED
, "COMPAQ", "RAID*", "*"},
258 /*quirks*/ DA_Q_NO_SYNC_CACHE
261 * USB mass storage devices supported by umass(4)
263 * NOTE: USB attachments automatically set DA_Q_NO_SYNC_CACHE so
264 * it does not have to be specified here.
268 * Creative Nomad MUVO mp3 player (USB)
271 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "CREATIVE", "NOMAD_MUVO", "*"},
272 /*quirks*/ DA_Q_NO_PREVENT
276 * Sigmatel USB Flash MP3 Player
279 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "SigmaTel", "MSCN", "*"},
280 /*quirks*/ DA_Q_NO_PREVENT
284 * SEAGRAND NP-900 MP3 Player
287 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "SEAGRAND", "NP-900*", "*"},
288 /*quirks*/ DA_Q_NO_PREVENT
292 * Creative MUVO Slim mp3 player (USB)
295 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "CREATIVE", "MuVo Slim",
296 "*"}, /*quirks*/ DA_Q_NO_PREVENT
300 * Philips USB Key Audio KEY013
303 {T_DIRECT
, SIP_MEDIA_REMOVABLE
, "PHILIPS", "Key*", "*"},
304 /*quirks*/ DA_Q_NO_PREVENT
308 static d_open_t daopen
;
309 static d_close_t daclose
;
310 static d_strategy_t dastrategy
;
311 static d_dump_t dadump
;
312 static periph_init_t dainit
;
313 static void daasync(void *callback_arg
, u_int32_t code
,
314 struct cam_path
*path
, void *arg
);
315 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS
);
316 static periph_ctor_t daregister
;
317 static periph_dtor_t dacleanup
;
318 static periph_start_t dastart
;
319 static periph_oninv_t daoninvalidate
;
320 static void dadone(struct cam_periph
*periph
,
321 union ccb
*done_ccb
);
322 static int daerror(union ccb
*ccb
, u_int32_t cam_flags
,
323 u_int32_t sense_flags
);
324 static void daprevent(struct cam_periph
*periph
, int action
);
325 static int dagetcapacity(struct cam_periph
*periph
);
326 static int dacheckmedia(struct cam_periph
*periph
);
327 static void dasetgeom(struct cam_periph
*periph
, uint32_t block_len
,
330 static timeout_t dasendorderedtag
;
331 static void dashutdown(void *arg
, int howto
);
333 #ifndef DA_DEFAULT_TIMEOUT
334 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
337 #ifndef DA_DEFAULT_RETRY
338 #define DA_DEFAULT_RETRY 4
341 #ifndef DA_DEFAULT_SEND_ORDERED
342 #define DA_DEFAULT_SEND_ORDERED 1
345 static int da_retry_count
= DA_DEFAULT_RETRY
;
346 static int da_default_timeout
= DA_DEFAULT_TIMEOUT
;
347 static int da_send_ordered
= DA_DEFAULT_SEND_ORDERED
;
348 static struct callout dasendorderedtag_ch
;
350 SYSCTL_NODE(_kern_cam
, OID_AUTO
, da
, CTLFLAG_RD
, 0,
351 "CAM Direct Access Disk driver");
352 SYSCTL_INT(_kern_cam_da
, OID_AUTO
, retry_count
, CTLFLAG_RW
,
353 &da_retry_count
, 0, "Normal I/O retry count");
354 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count
);
355 SYSCTL_INT(_kern_cam_da
, OID_AUTO
, default_timeout
, CTLFLAG_RW
,
356 &da_default_timeout
, 0, "Normal I/O timeout (in seconds)");
357 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout
);
358 SYSCTL_INT(_kern_cam_da
, OID_AUTO
, da_send_ordered
, CTLFLAG_RW
,
359 &da_send_ordered
, 0, "Send Ordered Tags");
360 TUNABLE_INT("kern.cam.da.da_send_ordered", &da_send_ordered
);
363 * DA_ORDEREDTAG_INTERVAL determines how often, relative
364 * to the default timeout, we check to see whether an ordered
365 * tagged transaction is appropriate to prevent simple tag
366 * starvation. Since we'd like to ensure that there is at least
367 * 1/2 of the timeout length left for a starved transaction to
368 * complete after we've sent an ordered tag, we must poll at least
369 * four times in every timeout period. This takes care of the worst
370 * case where a starved transaction starts during an interval that
371 * meets the requirement "don't send an ordered tag" test so it takes
372 * us two intervals to determine that a tag must be sent.
374 #ifndef DA_ORDEREDTAG_INTERVAL
375 #define DA_ORDEREDTAG_INTERVAL 4
378 static struct periph_driver dadriver
=
381 TAILQ_HEAD_INITIALIZER(dadriver
.units
), /* generation */ 0
384 PERIPHDRIVER_DECLARE(da
, dadriver
);
386 static struct dev_ops da_ops
= {
387 { "da", DA_CDEV_MAJOR
, D_DISK
},
391 .d_write
= physwrite
,
392 .d_strategy
= dastrategy
,
396 static struct extend_array
*daperiphs
;
398 MALLOC_DEFINE(M_SCSIDA
, "scsi_da", "scsi_da buffers");
401 daopen(struct dev_open_args
*ap
)
403 cdev_t dev
= ap
->a_head
.a_dev
;
404 struct cam_periph
*periph
;
405 struct da_softc
*softc
;
406 struct disk_info info
;
411 periph
= cam_extend_get(daperiphs
, unit
);
412 if (periph
== NULL
) {
416 if (cam_periph_acquire(periph
) != CAM_REQ_CMP
) {
420 cam_periph_lock(periph
);
421 if ((error
= cam_periph_hold(periph
, PCATCH
)) != 0) {
422 cam_periph_unlock(periph
);
423 cam_periph_release(periph
);
427 unit
= periph
->unit_number
;
428 softc
= (struct da_softc
*)periph
->softc
;
430 CAM_DEBUG(periph
->path
, CAM_DEBUG_TRACE
,
431 ("daopen: dev=%s (unit %d)\n", devtoname(dev
),
434 if ((softc
->flags
& DA_FLAG_PACK_INVALID
) != 0) {
435 /* Invalidate our pack information. */
436 disk_invalidate(&softc
->disk
);
437 softc
->flags
&= ~DA_FLAG_PACK_INVALID
;
440 error
= dacheckmedia(periph
);
441 softc
->flags
|= DA_FLAG_OPEN
;
444 struct ccb_getdev cgd
;
446 /* Build disk information structure */
447 bzero(&info
, sizeof(info
));
448 info
.d_type
= DTYPE_SCSI
;
451 * Grab the inquiry data to get the vendor and product names.
452 * Put them in the typename and packname for the label.
454 xpt_setup_ccb(&cgd
.ccb_h
, periph
->path
, /*priority*/ 1);
455 cgd
.ccb_h
.func_code
= XPT_GDEV_TYPE
;
456 xpt_action((union ccb
*)&cgd
);
459 * Check to see whether or not the blocksize is set yet.
460 * If it isn't, set it and then clear the blocksize
461 * unavailable flag for the device statistics.
463 if ((softc
->device_stats
.flags
& DEVSTAT_BS_UNAVAILABLE
) != 0){
464 softc
->device_stats
.block_size
= softc
->params
.secsize
;
465 softc
->device_stats
.flags
&= ~DEVSTAT_BS_UNAVAILABLE
;
470 if ((softc
->flags
& DA_FLAG_PACK_REMOVABLE
) != 0 &&
471 (softc
->quirks
& DA_Q_NO_PREVENT
) == 0)
472 daprevent(periph
, PR_PREVENT
);
474 softc
->flags
&= ~DA_FLAG_OPEN
;
475 cam_periph_release(periph
);
477 cam_periph_unhold(periph
, 1);
482 daclose(struct dev_close_args
*ap
)
484 cdev_t dev
= ap
->a_head
.a_dev
;
485 struct cam_periph
*periph
;
486 struct da_softc
*softc
;
491 periph
= cam_extend_get(daperiphs
, unit
);
495 cam_periph_lock(periph
);
496 if ((error
= cam_periph_hold(periph
, 0)) != 0) {
497 cam_periph_unlock(periph
);
498 cam_periph_release(periph
);
502 softc
= (struct da_softc
*)periph
->softc
;
504 if ((softc
->quirks
& DA_Q_NO_SYNC_CACHE
) == 0) {
507 ccb
= cam_periph_getccb(periph
, /*priority*/1);
509 scsi_synchronize_cache(&ccb
->csio
,
513 /*begin_lba*/0,/* Cover the whole disk */
518 cam_periph_runccb(ccb
, /*error_routine*/NULL
, /*cam_flags*/0,
519 /*sense_flags*/SF_RETRY_UA
,
520 &softc
->device_stats
);
522 if ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
523 if ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) ==
524 CAM_SCSI_STATUS_ERROR
) {
526 int sense_key
, error_code
;
528 scsi_extract_sense(&ccb
->csio
.sense_data
,
532 if (sense_key
!= SSD_KEY_ILLEGAL_REQUEST
)
533 scsi_sense_print(&ccb
->csio
);
535 xpt_print(periph
->path
, "Synchronize cache "
536 "failed, status == 0x%x, scsi status == "
537 "0x%x\n", ccb
->csio
.ccb_h
.status
,
538 ccb
->csio
.scsi_status
);
542 if ((ccb
->ccb_h
.status
& CAM_DEV_QFRZN
) != 0)
543 cam_release_devq(ccb
->ccb_h
.path
,
549 xpt_release_ccb(ccb
);
553 if ((softc
->flags
& DA_FLAG_PACK_REMOVABLE
) != 0) {
554 if ((softc
->quirks
& DA_Q_NO_PREVENT
) == 0)
555 daprevent(periph
, PR_ALLOW
);
557 * If we've got removeable media, mark the blocksize as
558 * unavailable, since it could change when new media is
561 softc
->device_stats
.flags
|= DEVSTAT_BS_UNAVAILABLE
;
565 * Don't compound any ref counting software bugs with more.
567 if (softc
->flags
& DA_FLAG_OPEN
) {
568 softc
->flags
&= ~DA_FLAG_OPEN
;
569 cam_periph_release(periph
);
571 xpt_print(periph
->path
,
572 "daclose() called on an already closed device!\n");
574 cam_periph_unhold(periph
, 1);
579 * Actually translate the requested transfer into one the physical driver
580 * can understand. The transfer is described by a buf and will include
581 * only one physical transfer.
584 dastrategy(struct dev_strategy_args
*ap
)
586 cdev_t dev
= ap
->a_head
.a_dev
;
587 struct bio
*bio
= ap
->a_bio
;
588 struct buf
*bp
= bio
->bio_buf
;
589 struct cam_periph
*periph
;
590 struct da_softc
*softc
;
596 periph
= cam_extend_get(daperiphs
, unit
);
597 if (periph
== NULL
) {
601 softc
= (struct da_softc
*)periph
->softc
;
603 cam_periph_lock(periph
);
607 * check it's not too big a transfer for our adapter
609 scsi_minphys(bp
, &sd_switch
);
613 * Mask interrupts so that the pack cannot be invalidated until
614 * after we are in the queue. Otherwise, we might not properly
615 * clean up one of the buffers.
619 * If the device has been made invalid, error out
621 if ((softc
->flags
& DA_FLAG_PACK_INVALID
)) {
622 cam_periph_unlock(periph
);
628 * Place it in the queue of disk activities for this disk
630 bioqdisksort(&softc
->bio_queue
, bio
);
633 * Schedule ourselves for performing the work.
635 xpt_schedule(periph
, /* XXX priority */1);
636 cam_periph_unlock(periph
);
640 bp
->b_flags
|= B_ERROR
;
643 * Correctly set the buf to indicate a completed xfer
645 bp
->b_resid
= bp
->b_bcount
;
651 dadump(struct dev_dump_args
*ap
)
653 cdev_t dev
= ap
->a_head
.a_dev
;
654 struct cam_periph
*periph
;
655 struct da_softc
*softc
;
659 struct ccb_scsiio csio
;
660 int dumppages
= MAXDUMPPGS
;
663 /* toss any characters present prior to dump */
664 while (cncheckc() != -1)
668 periph
= cam_extend_get(daperiphs
, unit
);
669 if (periph
== NULL
) {
672 softc
= (struct da_softc
*)periph
->softc
;
674 cam_periph_lock(periph
);
675 if ((softc
->flags
& DA_FLAG_PACK_INVALID
) != 0) {
676 cam_periph_unlock(periph
);
680 addr
= 0; /* starting address */
681 blkcnt
= howmany(PAGE_SIZE
, ap
->a_secsize
);
683 while (ap
->a_count
> 0) {
686 if ((ap
->a_count
/ blkcnt
) < dumppages
)
687 dumppages
= ap
->a_count
/ blkcnt
;
689 for (i
= 0; i
< dumppages
; ++i
) {
690 vm_paddr_t a
= addr
+ (i
* PAGE_SIZE
);
691 if (is_physical_memory(a
))
692 va
= pmap_kenter_temporary(trunc_page(a
), i
);
694 va
= pmap_kenter_temporary(trunc_page(0), i
);
697 xpt_setup_ccb(&csio
.ccb_h
, periph
->path
, /*priority*/1);
698 csio
.ccb_h
.flags
|= CAM_POLLED
;
699 csio
.ccb_h
.ccb_state
= DA_CCB_DUMP
;
700 scsi_read_write(&csio
,
706 /*minimum_cmd_size*/ softc
->minimum_cmd_size
,
709 /*data_ptr*/(u_int8_t
*) va
,
710 /*dxfer_len*/blkcnt
* ap
->a_secsize
* dumppages
,
711 /*sense_len*/SSD_FULL_SIZE
,
712 DA_DEFAULT_TIMEOUT
* 1000);
713 xpt_polled_action((union ccb
*)&csio
);
715 if ((csio
.ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
716 kprintf("Aborting dump due to I/O error.\n");
717 if ((csio
.ccb_h
.status
& CAM_STATUS_MASK
) ==
718 CAM_SCSI_STATUS_ERROR
)
719 scsi_sense_print(&csio
);
721 kprintf("status == 0x%x, scsi status == 0x%x\n",
722 csio
.ccb_h
.status
, csio
.scsi_status
);
726 if (dumpstatus(addr
, (off_t
)ap
->a_count
* softc
->params
.secsize
) < 0)
729 /* update block count */
730 ap
->a_count
-= blkcnt
* dumppages
;
731 ap
->a_blkno
+= blkcnt
* dumppages
;
732 addr
+= PAGE_SIZE
* dumppages
;
736 * Sync the disk cache contents to the physical media.
738 if ((softc
->quirks
& DA_Q_NO_SYNC_CACHE
) == 0) {
740 xpt_setup_ccb(&csio
.ccb_h
, periph
->path
, /*priority*/1);
741 csio
.ccb_h
.ccb_state
= DA_CCB_DUMP
;
742 scsi_synchronize_cache(&csio
,
746 /*begin_lba*/0,/* Cover the whole disk */
750 xpt_polled_action((union ccb
*)&csio
);
752 if ((csio
.ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
753 if ((csio
.ccb_h
.status
& CAM_STATUS_MASK
) ==
754 CAM_SCSI_STATUS_ERROR
) {
756 int sense_key
, error_code
;
758 scsi_extract_sense(&csio
.sense_data
,
762 if (sense_key
!= SSD_KEY_ILLEGAL_REQUEST
)
763 scsi_sense_print(&csio
);
765 xpt_print(periph
->path
, "Synchronize cache "
766 "failed, status == 0x%x, scsi status == "
767 "0x%x\n", csio
.ccb_h
.status
,
772 cam_periph_unlock(periph
);
782 * Create our extend array for storing the devices we attach to.
784 daperiphs
= cam_extend_new();
785 if (daperiphs
== NULL
) {
786 kprintf("da: Failed to alloc extend array!\n");
790 callout_init(&dasendorderedtag_ch
);
793 * Install a global async callback. This callback will
794 * receive async callbacks like "new device found".
796 status
= xpt_register_async(AC_FOUND_DEVICE
, daasync
, NULL
, NULL
);
798 if (status
!= CAM_REQ_CMP
) {
799 kprintf("da: Failed to attach master async callback "
800 "due to status 0x%x!\n", status
);
801 } else if (da_send_ordered
) {
803 /* Register our shutdown event handler */
804 if ((EVENTHANDLER_REGISTER(shutdown_post_sync
, dashutdown
,
805 NULL
, SHUTDOWN_PRI_DEFAULT
)) == NULL
)
806 kprintf("dainit: shutdown event registration failed!\n");
811 daoninvalidate(struct cam_periph
*periph
)
813 struct da_softc
*softc
;
817 softc
= (struct da_softc
*)periph
->softc
;
820 * De-register any async callbacks.
822 xpt_register_async(0, daasync
, periph
, periph
->path
);
824 softc
->flags
|= DA_FLAG_PACK_INVALID
;
827 * Return all queued I/O with ENXIO.
828 * XXX Handle any transactions queued to the card
829 * with XPT_ABORT_CCB.
831 while ((q_bio
= bioq_first(&softc
->bio_queue
)) != NULL
){
832 bioq_remove(&softc
->bio_queue
, q_bio
);
833 q_bp
= q_bio
->bio_buf
;
834 q_bp
->b_resid
= q_bp
->b_bcount
;
835 q_bp
->b_error
= ENXIO
;
836 q_bp
->b_flags
|= B_ERROR
;
839 xpt_print(periph
->path
, "lost device\n");
843 dacleanup(struct cam_periph
*periph
)
845 struct da_softc
*softc
;
847 softc
= (struct da_softc
*)periph
->softc
;
849 devstat_remove_entry(&softc
->device_stats
);
850 cam_extend_release(daperiphs
, periph
->unit_number
);
851 xpt_print(periph
->path
, "removing device entry\n");
853 * If we can't free the sysctl tree, oh well...
855 if ((softc
->flags
& DA_FLAG_SCTX_INIT
) != 0
856 && sysctl_ctx_free(&softc
->sysctl_ctx
) != 0) {
857 xpt_print(periph
->path
, "can't remove sysctl context\n");
859 periph
->softc
= NULL
;
860 if (softc
->disk
.d_rawdev
) {
861 cam_periph_unlock(periph
);
862 disk_destroy(&softc
->disk
);
863 cam_periph_lock(periph
);
866 callout_stop(&softc
->sendordered_c
);
867 kfree(softc
, M_DEVBUF
);
871 daasync(void *callback_arg
, u_int32_t code
,
872 struct cam_path
*path
, void *arg
)
874 struct cam_periph
*periph
;
876 periph
= (struct cam_periph
*)callback_arg
;
879 case AC_FOUND_DEVICE
:
881 struct ccb_getdev
*cgd
;
885 cgd
= (struct ccb_getdev
*)arg
;
889 if (SID_TYPE(&cgd
->inq_data
) != T_DIRECT
890 && SID_TYPE(&cgd
->inq_data
) != T_RBC
891 && SID_TYPE(&cgd
->inq_data
) != T_OPTICAL
)
895 * Don't complain if a valid peripheral is already attached.
897 periph
= cam_periph_find(cgd
->ccb_h
.path
, "da");
898 if (periph
&& (periph
->flags
& CAM_PERIPH_INVALID
) == 0)
902 * Allocate a peripheral instance for
903 * this device and start the probe
906 sim
= xpt_path_sim(cgd
->ccb_h
.path
);
907 status
= cam_periph_alloc(daregister
, daoninvalidate
,
909 "da", CAM_PERIPH_BIO
,
910 cgd
->ccb_h
.path
, daasync
,
911 AC_FOUND_DEVICE
, cgd
);
913 if (status
!= CAM_REQ_CMP
&& status
!= CAM_REQ_INPROG
) {
914 kprintf("daasync: Unable to attach to new device "
915 "due to status 0x%x\n", status
);
922 struct da_softc
*softc
;
923 struct ccb_hdr
*ccbh
;
925 softc
= (struct da_softc
*)periph
->softc
;
927 * Don't fail on the expected unit attention
930 softc
->flags
|= DA_FLAG_RETRY_UA
;
931 LIST_FOREACH(ccbh
, &softc
->pending_ccbs
, periph_links
.le
)
932 ccbh
->ccb_state
|= DA_CCB_RETRY_UA
;
936 cam_periph_async(periph
, code
, path
, arg
);
942 dasysctlinit(void *context
, int pending
)
944 struct cam_periph
*periph
;
945 struct da_softc
*softc
;
946 char tmpstr
[80], tmpstr2
[80];
948 periph
= (struct cam_periph
*)context
;
949 if (cam_periph_acquire(periph
) != CAM_REQ_CMP
)
952 softc
= (struct da_softc
*)periph
->softc
;
953 ksnprintf(tmpstr
, sizeof(tmpstr
), "CAM DA unit %d", periph
->unit_number
);
954 ksnprintf(tmpstr2
, sizeof(tmpstr2
), "%d", periph
->unit_number
);
956 sysctl_ctx_init(&softc
->sysctl_ctx
);
957 softc
->flags
|= DA_FLAG_SCTX_INIT
;
958 softc
->sysctl_tree
= SYSCTL_ADD_NODE(&softc
->sysctl_ctx
,
959 SYSCTL_STATIC_CHILDREN(_kern_cam_da
), OID_AUTO
, tmpstr2
,
960 CTLFLAG_RD
, 0, tmpstr
);
961 if (softc
->sysctl_tree
== NULL
) {
962 kprintf("dasysctlinit: unable to allocate sysctl tree\n");
963 cam_periph_release(periph
);
968 * Now register the sysctl handler, so the user can the value on
971 SYSCTL_ADD_PROC(&softc
->sysctl_ctx
,SYSCTL_CHILDREN(softc
->sysctl_tree
),
972 OID_AUTO
, "minimum_cmd_size", CTLTYPE_INT
| CTLFLAG_RW
,
973 &softc
->minimum_cmd_size
, 0, dacmdsizesysctl
, "I",
976 cam_periph_release(periph
);
980 dacmdsizesysctl(SYSCTL_HANDLER_ARGS
)
984 value
= *(int *)arg1
;
986 error
= sysctl_handle_int(oidp
, &value
, 0, req
);
989 || (req
->newptr
== NULL
))
993 * Acceptable values here are 6, 10 or 12, or 16.
1000 else if ((value
> 10)
1003 else if (value
> 12)
1006 *(int *)arg1
= value
;
1012 daregister(struct cam_periph
*periph
, void *arg
)
1014 struct da_softc
*softc
;
1015 struct ccb_pathinq cpi
;
1016 struct ccb_getdev
*cgd
;
1020 cgd
= (struct ccb_getdev
*)arg
;
1021 if (periph
== NULL
) {
1022 kprintf("daregister: periph was NULL!!\n");
1023 return(CAM_REQ_CMP_ERR
);
1027 kprintf("daregister: no getdev CCB, can't register device\n");
1028 return(CAM_REQ_CMP_ERR
);
1031 softc
= kmalloc(sizeof(*softc
), M_DEVBUF
, M_INTWAIT
| M_ZERO
);
1032 LIST_INIT(&softc
->pending_ccbs
);
1033 softc
->state
= DA_STATE_PROBE
;
1034 bioq_init(&softc
->bio_queue
);
1035 if (SID_IS_REMOVABLE(&cgd
->inq_data
))
1036 softc
->flags
|= DA_FLAG_PACK_REMOVABLE
;
1037 if ((cgd
->inq_data
.flags
& SID_CmdQue
) != 0)
1038 softc
->flags
|= DA_FLAG_TAGGED_QUEUING
;
1040 periph
->softc
= softc
;
1042 cam_extend_set(daperiphs
, periph
->unit_number
, periph
);
1045 * See if this device has any quirks.
1047 match
= cam_quirkmatch((caddr_t
)&cgd
->inq_data
,
1048 (caddr_t
)da_quirk_table
,
1049 sizeof(da_quirk_table
)/sizeof(*da_quirk_table
),
1050 sizeof(*da_quirk_table
), scsi_inquiry_match
);
1053 softc
->quirks
= ((struct da_quirk_entry
*)match
)->quirks
;
1055 softc
->quirks
= DA_Q_NONE
;
1058 * Unconditionally disable the synchronize cache command for
1059 * usb attachments. It's just impossible to determine if the
1060 * device supports it or not and if it doesn't the port can
1063 if (strncmp(periph
->sim
->sim_name
, "umass", 4) == 0) {
1064 softc
->quirks
|= DA_Q_NO_SYNC_CACHE
;
1067 TASK_INIT(&softc
->sysctl_task
, 0, dasysctlinit
, periph
);
1069 /* Check if the SIM does not want 6 byte commands */
1070 xpt_setup_ccb(&cpi
.ccb_h
, periph
->path
, /*priority*/1);
1071 cpi
.ccb_h
.func_code
= XPT_PATH_INQ
;
1072 xpt_action((union ccb
*)&cpi
);
1073 if (cpi
.ccb_h
.status
== CAM_REQ_CMP
&& (cpi
.hba_misc
& PIM_NO_6_BYTE
))
1074 softc
->quirks
|= DA_Q_NO_6_BYTE
;
1077 * RBC devices don't have to support READ(6), only READ(10).
1079 if (softc
->quirks
& DA_Q_NO_6_BYTE
|| SID_TYPE(&cgd
->inq_data
) == T_RBC
)
1080 softc
->minimum_cmd_size
= 10;
1082 softc
->minimum_cmd_size
= 6;
1085 * Load the user's default, if any.
1087 ksnprintf(tmpstr
, sizeof(tmpstr
), "kern.cam.da.%d.minimum_cmd_size",
1088 periph
->unit_number
);
1089 TUNABLE_INT_FETCH(tmpstr
, &softc
->minimum_cmd_size
);
1092 * 6, 10, 12, and 16 are the currently permissible values.
1094 if (softc
->minimum_cmd_size
< 6)
1095 softc
->minimum_cmd_size
= 6;
1096 else if ((softc
->minimum_cmd_size
> 6)
1097 && (softc
->minimum_cmd_size
<= 10))
1098 softc
->minimum_cmd_size
= 10;
1099 else if ((softc
->minimum_cmd_size
> 10)
1100 && (softc
->minimum_cmd_size
<= 12))
1101 softc
->minimum_cmd_size
= 12;
1102 else if (softc
->minimum_cmd_size
> 12)
1103 softc
->minimum_cmd_size
= 16;
1106 * The DA driver supports a blocksize, but
1107 * we don't know the blocksize until we do
1108 * a read capacity. So, set a flag to
1109 * indicate that the blocksize is
1110 * unavailable right now. We'll clear the
1111 * flag as soon as we've done a read capacity.
1113 devstat_add_entry(&softc
->device_stats
, "da",
1114 periph
->unit_number
, 0,
1115 DEVSTAT_BS_UNAVAILABLE
,
1116 SID_TYPE(&cgd
->inq_data
) | DEVSTAT_TYPE_IF_SCSI
,
1117 DEVSTAT_PRIORITY_DISK
);
1120 * Register this media as a disk
1122 CAM_SIM_UNLOCK(periph
->sim
);
1123 disk_create(periph
->unit_number
, &softc
->disk
, &da_ops
);
1124 softc
->disk
.d_rawdev
->si_iosize_max
= MAXPHYS
;
1125 CAM_SIM_LOCK(periph
->sim
);
1128 * Add async callbacks for bus reset and
1129 * bus device reset calls. I don't bother
1130 * checking if this fails as, in most cases,
1131 * the system will function just fine without
1132 * them and the only alternative would be to
1133 * not attach the device on failure.
1135 xpt_register_async(AC_SENT_BDR
| AC_BUS_RESET
| AC_LOST_DEVICE
,
1136 daasync
, periph
, periph
->path
);
1139 * Take an exclusive refcount on the periph while dastart is called
1140 * to finish the probe. The reference will be dropped in dadone at
1143 cam_periph_hold(periph
, 0);
1144 xpt_schedule(periph
, /*priority*/5);
1147 * Schedule a periodic event to occasionally send an
1148 * ordered tag to a device.
1150 callout_init(&softc
->sendordered_c
);
1151 callout_reset(&softc
->sendordered_c
,
1152 (DA_DEFAULT_TIMEOUT
* hz
) / DA_ORDEREDTAG_INTERVAL
,
1153 dasendorderedtag
, softc
);
1155 return(CAM_REQ_CMP
);
1159 dastart(struct cam_periph
*periph
, union ccb
*start_ccb
)
1161 struct da_softc
*softc
;
1163 softc
= (struct da_softc
*)periph
->softc
;
1165 switch (softc
->state
) {
1166 case DA_STATE_NORMAL
:
1168 /* Pull a buffer from the queue and get going on it */
1174 * See if there is a buf with work for us to do..
1176 bio
= bioq_first(&softc
->bio_queue
);
1177 if (periph
->immediate_priority
<= periph
->pinfo
.priority
) {
1178 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE
,
1179 ("queuing for immediate ccb\n"));
1180 start_ccb
->ccb_h
.ccb_state
= DA_CCB_WAITING
;
1181 SLIST_INSERT_HEAD(&periph
->ccb_list
, &start_ccb
->ccb_h
,
1183 periph
->immediate_priority
= CAM_PRIORITY_NONE
;
1184 wakeup(&periph
->ccb_list
);
1187 * Have more work to do, so ensure we stay
1190 xpt_schedule(periph
, /* XXX priority */1);
1195 xpt_release_ccb(start_ccb
);
1200 * We can queue new work.
1202 bioq_remove(&softc
->bio_queue
, bio
);
1205 devstat_start_transaction(&softc
->device_stats
);
1207 if ((bp
->b_flags
& B_ORDERED
) != 0 ||
1208 (softc
->flags
& DA_FLAG_NEED_OTAG
) != 0) {
1209 softc
->flags
&= ~DA_FLAG_NEED_OTAG
;
1210 softc
->ordered_tag_count
++;
1211 tag_code
= MSG_ORDERED_Q_TAG
;
1213 tag_code
= MSG_SIMPLE_Q_TAG
;
1220 * Block read/write op
1222 KKASSERT(bio
->bio_offset
% softc
->params
.secsize
== 0);
1226 da_retry_count
, /* retries */
1229 (bp
->b_cmd
== BUF_CMD_READ
),
1231 softc
->minimum_cmd_size
,
1232 bio
->bio_offset
/ softc
->params
.secsize
,
1233 bp
->b_bcount
/ softc
->params
.secsize
,
1236 SSD_FULL_SIZE
, /* sense_len */
1237 da_default_timeout
* 1000
1242 * Silently complete a flush request if the device
1245 if (softc
->quirks
& DA_Q_NO_SYNC_CACHE
) {
1246 xpt_release_ccb(start_ccb
);
1248 devstat_end_transaction_buf(
1249 &softc
->device_stats
, bp
);
1252 scsi_synchronize_cache(
1255 dadone
, /* cbfcnp */
1258 0, /* count (whole disk) */
1260 da_default_timeout
*1000 /* timeout */
1265 xpt_release_ccb(start_ccb
);
1267 panic("dastart: unrecognized bio cmd %d", bp
->b_cmd
);
1268 break; /* NOT REACHED */
1272 * Block out any asyncronous callbacks
1273 * while we touch the pending ccb list.
1276 start_ccb
->ccb_h
.ccb_state
= DA_CCB_BUFFER_IO
;
1277 LIST_INSERT_HEAD(&softc
->pending_ccbs
,
1278 &start_ccb
->ccb_h
, periph_links
.le
);
1279 softc
->outstanding_cmds
++;
1281 /* We expect a unit attention from this device */
1282 if ((softc
->flags
& DA_FLAG_RETRY_UA
) != 0) {
1283 start_ccb
->ccb_h
.ccb_state
|= DA_CCB_RETRY_UA
;
1284 softc
->flags
&= ~DA_FLAG_RETRY_UA
;
1287 start_ccb
->ccb_h
.ccb_bio
= bio
;
1288 xpt_action(start_ccb
);
1292 * Be sure we stay scheduled if we have more work to do.
1294 bio
= bioq_first(&softc
->bio_queue
);
1296 xpt_schedule(periph
, 1);
1299 case DA_STATE_PROBE
:
1301 struct ccb_scsiio
*csio
;
1302 struct scsi_read_capacity_data
*rcap
;
1304 rcap
= kmalloc(sizeof(*rcap
), M_SCSIDA
, M_INTWAIT
| M_ZERO
);
1305 csio
= &start_ccb
->csio
;
1306 scsi_read_capacity(csio
,
1313 start_ccb
->ccb_h
.ccb_bio
= NULL
;
1314 start_ccb
->ccb_h
.ccb_state
= DA_CCB_PROBE
;
1315 xpt_action(start_ccb
);
1318 case DA_STATE_PROBE2
:
1320 struct ccb_scsiio
*csio
;
1321 struct scsi_read_capacity_data_16
*rcaplong
;
1323 rcaplong
= kmalloc(sizeof(*rcaplong
), M_SCSIDA
,
1324 M_INTWAIT
| M_ZERO
);
1325 if (rcaplong
== NULL
) {
1326 kprintf("dastart: Couldn't allocate read_capacity\n");
1327 /* da_free_periph??? */
1330 csio
= &start_ccb
->csio
;
1331 scsi_read_capacity_16(csio
,
1334 /*tag_action*/ MSG_SIMPLE_Q_TAG
,
1339 /*sense_len*/ SSD_FULL_SIZE
,
1341 start_ccb
->ccb_h
.ccb_bio
= NULL
;
1342 start_ccb
->ccb_h
.ccb_state
= DA_CCB_PROBE2
;
1343 xpt_action(start_ccb
);
1350 cmd6workaround(union ccb
*ccb
)
1352 struct scsi_rw_6 cmd6
;
1353 struct scsi_rw_10
*cmd10
;
1354 struct da_softc
*softc
;
1358 cdb
= ccb
->csio
.cdb_io
.cdb_bytes
;
1360 /* Translation only possible if CDB is an array and cmd is R/W6 */
1361 if ((ccb
->ccb_h
.flags
& CAM_CDB_POINTER
) != 0 ||
1362 (*cdb
!= READ_6
&& *cdb
!= WRITE_6
))
1365 xpt_print(ccb
->ccb_h
.path
, "READ(6)/WRITE(6) not supported, "
1366 "increasing minimum_cmd_size to 10.\n");
1367 softc
= (struct da_softc
*)xpt_path_periph(ccb
->ccb_h
.path
)->softc
;
1368 softc
->minimum_cmd_size
= 10;
1370 bcopy(cdb
, &cmd6
, sizeof(struct scsi_rw_6
));
1371 cmd10
= (struct scsi_rw_10
*)cdb
;
1372 cmd10
->opcode
= (cmd6
.opcode
== READ_6
) ? READ_10
: WRITE_10
;
1374 scsi_ulto4b(scsi_3btoul(cmd6
.addr
), cmd10
->addr
);
1375 cmd10
->reserved
= 0;
1376 scsi_ulto2b(cmd6
.length
, cmd10
->length
);
1377 cmd10
->control
= cmd6
.control
;
1378 ccb
->csio
.cdb_len
= sizeof(*cmd10
);
1380 /* Requeue request, unfreezing queue if necessary */
1381 frozen
= (ccb
->ccb_h
.status
& CAM_DEV_QFRZN
) != 0;
1382 ccb
->ccb_h
.status
= CAM_REQUEUE_REQ
;
1385 cam_release_devq(ccb
->ccb_h
.path
,
1389 /*getcount_only*/0);
1395 dadone(struct cam_periph
*periph
, union ccb
*done_ccb
)
1397 struct da_softc
*softc
;
1398 struct ccb_scsiio
*csio
;
1399 struct disk_info info
;
1401 softc
= (struct da_softc
*)periph
->softc
;
1402 csio
= &done_ccb
->csio
;
1403 switch (csio
->ccb_h
.ccb_state
& DA_CCB_TYPE_MASK
) {
1404 case DA_CCB_BUFFER_IO
:
1409 bio
= (struct bio
*)done_ccb
->ccb_h
.ccb_bio
;
1411 if ((done_ccb
->ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
1415 if ((csio
->ccb_h
.ccb_state
& DA_CCB_RETRY_UA
) != 0)
1420 error
= daerror(done_ccb
, CAM_RETRY_SELTO
, sf
);
1421 if (error
== ERESTART
) {
1423 * A retry was scheuled, so
1432 if (error
== ENXIO
) {
1434 * Catastrophic error. Mark our pack as
1438 * XXX See if this is really a media
1441 xpt_print(periph
->path
,
1442 "Invalidating pack\n");
1443 softc
->flags
|= DA_FLAG_PACK_INVALID
;
1447 * return all queued I/O with EIO, so that
1448 * the client can retry these I/Os in the
1449 * proper order should it attempt to recover.
1451 while ((q_bio
= bioq_first(&softc
->bio_queue
))
1453 bioq_remove(&softc
->bio_queue
, q_bio
);
1454 q_bp
= q_bio
->bio_buf
;
1455 q_bp
->b_resid
= q_bp
->b_bcount
;
1456 q_bp
->b_error
= EIO
;
1457 q_bp
->b_flags
|= B_ERROR
;
1460 bp
->b_error
= error
;
1461 bp
->b_resid
= bp
->b_bcount
;
1462 bp
->b_flags
|= B_ERROR
;
1464 bp
->b_resid
= csio
->resid
;
1466 if (bp
->b_resid
!= 0)
1467 bp
->b_flags
|= B_ERROR
;
1469 if ((done_ccb
->ccb_h
.status
& CAM_DEV_QFRZN
) != 0)
1470 cam_release_devq(done_ccb
->ccb_h
.path
,
1474 /*getcount_only*/0);
1476 if ((done_ccb
->ccb_h
.status
& CAM_DEV_QFRZN
) != 0)
1477 panic("REQ_CMP with QFRZN");
1478 bp
->b_resid
= csio
->resid
;
1479 if (csio
->resid
> 0)
1480 bp
->b_flags
|= B_ERROR
;
1484 * Block out any asyncronous callbacks
1485 * while we touch the pending ccb list.
1487 LIST_REMOVE(&done_ccb
->ccb_h
, periph_links
.le
);
1488 softc
->outstanding_cmds
--;
1489 if (softc
->outstanding_cmds
== 0)
1490 softc
->flags
|= DA_FLAG_WENT_IDLE
;
1492 devstat_end_transaction_buf(&softc
->device_stats
, bp
);
1499 struct scsi_read_capacity_data
*rdcap
;
1500 struct scsi_read_capacity_data_16
*rcaplong
;
1501 char announce_buf
[80];
1505 if (softc
->state
== DA_STATE_PROBE
)
1506 rdcap
=(struct scsi_read_capacity_data
*)csio
->data_ptr
;
1508 rcaplong
= (struct scsi_read_capacity_data_16
*)
1511 bzero(&info
, sizeof(info
));
1512 info
.d_type
= DTYPE_SCSI
;
1513 info
.d_serialno
= xpt_path_serialno(periph
->path
);
1515 if ((csio
->ccb_h
.status
& CAM_STATUS_MASK
) == CAM_REQ_CMP
) {
1516 struct disk_params
*dp
;
1517 uint32_t block_size
;
1520 if (softc
->state
== DA_STATE_PROBE
) {
1521 block_size
= scsi_4btoul(rdcap
->length
);
1522 maxsector
= scsi_4btoul(rdcap
->addr
);
1525 * According to SBC-2, if the standard 10
1526 * byte READ CAPACITY command returns 2^32,
1527 * we should issue the 16 byte version of
1528 * the command, since the device in question
1529 * has more sectors than can be represented
1530 * with the short version of the command.
1532 if (maxsector
== 0xffffffff) {
1533 softc
->state
= DA_STATE_PROBE2
;
1534 kfree(rdcap
, M_SCSIDA
);
1535 xpt_release_ccb(done_ccb
);
1536 xpt_schedule(periph
, /*priority*/5);
1540 block_size
= scsi_4btoul(rcaplong
->length
);
1541 maxsector
= scsi_8btou64(rcaplong
->addr
);
1543 dasetgeom(periph
, block_size
, maxsector
);
1544 dp
= &softc
->params
;
1545 ksnprintf(announce_buf
, sizeof(announce_buf
),
1546 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1547 (uintmax_t) (((uintmax_t)dp
->secsize
*
1548 dp
->sectors
) / (1024*1024)),
1549 (uintmax_t)dp
->sectors
,
1550 dp
->secsize
, dp
->heads
, dp
->secs_per_track
,
1552 CAM_SIM_UNLOCK(periph
->sim
);
1553 info
.d_media_blksize
= softc
->params
.secsize
;
1554 info
.d_media_blocks
= softc
->params
.sectors
;
1555 info
.d_media_size
= 0;
1556 info
.d_secpertrack
= softc
->params
.secs_per_track
;
1557 info
.d_nheads
= softc
->params
.heads
;
1558 info
.d_ncylinders
= softc
->params
.cylinders
;
1559 info
.d_secpercyl
= softc
->params
.heads
*
1560 softc
->params
.secs_per_track
;
1561 info
.d_serialno
= xpt_path_serialno(periph
->path
);
1562 disk_setdiskinfo(&softc
->disk
, &info
);
1563 CAM_SIM_LOCK(periph
->sim
);
1567 announce_buf
[0] = '\0';
1570 * Retry any UNIT ATTENTION type errors. They
1571 * are expected at boot.
1573 error
= daerror(done_ccb
, CAM_RETRY_SELTO
,
1574 SF_RETRY_UA
|SF_NO_PRINT
);
1575 if (error
== ERESTART
) {
1577 * A retry was scheuled, so
1581 } else if (error
!= 0) {
1582 struct scsi_sense_data
*sense
;
1584 int sense_key
, error_code
;
1587 struct ccb_getdev cgd
;
1589 /* Don't wedge this device's queue */
1590 status
= done_ccb
->ccb_h
.status
;
1591 if ((status
& CAM_DEV_QFRZN
) != 0)
1592 cam_release_devq(done_ccb
->ccb_h
.path
,
1596 /*getcount_only*/0);
1599 xpt_setup_ccb(&cgd
.ccb_h
,
1600 done_ccb
->ccb_h
.path
,
1602 cgd
.ccb_h
.func_code
= XPT_GDEV_TYPE
;
1603 xpt_action((union ccb
*)&cgd
);
1605 if (((csio
->ccb_h
.flags
& CAM_SENSE_PHYS
) != 0)
1606 || ((csio
->ccb_h
.flags
& CAM_SENSE_PTR
) != 0)
1607 || ((status
& CAM_AUTOSNS_VALID
) == 0))
1613 sense
= &csio
->sense_data
;
1614 scsi_extract_sense(sense
, &error_code
,
1619 * Attach to anything that claims to be a
1620 * direct access or optical disk device,
1621 * as long as it doesn't return a "Logical
1622 * unit not supported" (0x25) error.
1624 if ((have_sense
) && (asc
!= 0x25)
1625 && (error_code
== SSD_CURRENT_ERROR
)) {
1626 const char *sense_key_desc
;
1627 const char *asc_desc
;
1629 scsi_sense_desc(sense_key
, asc
, ascq
,
1633 ksnprintf(announce_buf
,
1634 sizeof(announce_buf
),
1635 "Attempt to query device "
1636 "size failed: %s, %s",
1639 info
.d_media_blksize
= 512;
1640 disk_setdiskinfo(&softc
->disk
, &info
);
1646 xpt_print(periph
->path
,
1647 "got CAM status %#x\n",
1648 done_ccb
->ccb_h
.status
);
1651 xpt_print(periph
->path
, "fatal error, "
1652 "failed to attach to device\n");
1655 * Free up resources.
1657 cam_periph_invalidate(periph
);
1661 kfree(csio
->data_ptr
, M_SCSIDA
);
1662 if (announce_buf
[0] != '\0') {
1663 xpt_announce_periph(periph
, announce_buf
);
1665 * Create our sysctl variables, now that we know
1666 * we have successfully attached.
1668 taskqueue_enqueue(taskqueue_thread
[mycpuid
],
1669 &softc
->sysctl_task
);
1671 softc
->state
= DA_STATE_NORMAL
;
1673 * Since our peripheral may be invalidated by an error
1674 * above or an external event, we must release our CCB
1675 * before releasing the probe lock on the peripheral.
1676 * The peripheral will only go away once the last lock
1677 * is removed, and we need it around for the CCB release
1680 xpt_release_ccb(done_ccb
);
1681 cam_periph_unhold(periph
, 0);
1684 case DA_CCB_WAITING
:
1686 /* Caller will release the CCB */
1687 wakeup(&done_ccb
->ccb_h
.cbfcnp
);
1691 /* No-op. We're polling */
1696 xpt_release_ccb(done_ccb
);
1700 daerror(union ccb
*ccb
, u_int32_t cam_flags
, u_int32_t sense_flags
)
1702 struct da_softc
*softc
;
1703 struct cam_periph
*periph
;
1706 periph
= xpt_path_periph(ccb
->ccb_h
.path
);
1707 softc
= (struct da_softc
*)periph
->softc
;
1710 * Automatically detect devices that do not support
1711 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
1714 if ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) == CAM_REQ_INVALID
) {
1715 error
= cmd6workaround(ccb
);
1716 } else if (((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) ==
1717 CAM_SCSI_STATUS_ERROR
)
1718 && (ccb
->ccb_h
.status
& CAM_AUTOSNS_VALID
)
1719 && (ccb
->csio
.scsi_status
== SCSI_STATUS_CHECK_COND
)
1720 && ((ccb
->ccb_h
.flags
& CAM_SENSE_PHYS
) == 0)
1721 && ((ccb
->ccb_h
.flags
& CAM_SENSE_PTR
) == 0)) {
1722 int sense_key
, error_code
, asc
, ascq
;
1724 scsi_extract_sense(&ccb
->csio
.sense_data
,
1725 &error_code
, &sense_key
, &asc
, &ascq
);
1726 if (sense_key
== SSD_KEY_ILLEGAL_REQUEST
)
1727 error
= cmd6workaround(ccb
);
1729 if (error
== ERESTART
)
1734 * Until we have a better way of doing pack validation,
1735 * don't treat UAs as errors.
1737 sense_flags
|= SF_RETRY_UA
;
1738 return(cam_periph_error(ccb
, cam_flags
, sense_flags
,
1739 &softc
->saved_ccb
));
1743 daprevent(struct cam_periph
*periph
, int action
)
1745 struct da_softc
*softc
;
1749 softc
= (struct da_softc
*)periph
->softc
;
1751 if (((action
== PR_ALLOW
)
1752 && (softc
->flags
& DA_FLAG_PACK_LOCKED
) == 0)
1753 || ((action
== PR_PREVENT
)
1754 && (softc
->flags
& DA_FLAG_PACK_LOCKED
) != 0)) {
1758 ccb
= cam_periph_getccb(periph
, /*priority*/1);
1760 scsi_prevent(&ccb
->csio
,
1768 error
= cam_periph_runccb(ccb
, /*error_routine*/NULL
, CAM_RETRY_SELTO
,
1769 SF_RETRY_UA
, &softc
->device_stats
);
1772 if (action
== PR_ALLOW
)
1773 softc
->flags
&= ~DA_FLAG_PACK_LOCKED
;
1775 softc
->flags
|= DA_FLAG_PACK_LOCKED
;
1778 xpt_release_ccb(ccb
);
1782 * Check media on open, e.g. card reader devices which had no initial media.
1785 dacheckmedia(struct cam_periph
*periph
)
1787 struct disk_params
*dp
;
1788 struct da_softc
*softc
;
1789 struct disk_info info
;
1792 softc
= (struct da_softc
*)periph
->softc
;
1793 dp
= &softc
->params
;
1795 error
= dagetcapacity(periph
);
1798 * Only reprobe on initial open and if the media is removable.
1800 * NOTE: If we setdiskinfo() it will take the device probe
1801 * a bit of time to probe the slices and partitions,
1802 * and mess up booting. So avoid if nothing has changed.
1805 if (softc
->flags
& DA_FLAG_OPEN
)
1807 if ((softc
->flags
& DA_FLAG_PACK_REMOVABLE
) == 0)
1810 bzero(&info
, sizeof(info
));
1811 info
.d_type
= DTYPE_SCSI
;
1812 info
.d_serialno
= xpt_path_serialno(periph
->path
);
1815 CAM_SIM_UNLOCK(periph
->sim
);
1816 info
.d_media_blksize
= softc
->params
.secsize
;
1817 info
.d_media_blocks
= softc
->params
.sectors
;
1818 info
.d_media_size
= 0;
1819 info
.d_secpertrack
= softc
->params
.secs_per_track
;
1820 info
.d_nheads
= softc
->params
.heads
;
1821 info
.d_ncylinders
= softc
->params
.cylinders
;
1822 info
.d_secpercyl
= softc
->params
.heads
*
1823 softc
->params
.secs_per_track
;
1824 info
.d_serialno
= xpt_path_serialno(periph
->path
);
1825 if (info
.d_media_blocks
!= softc
->disk
.d_info
.d_media_blocks
) {
1826 kprintf("%s%d: open removable media: "
1827 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)\n",
1828 periph
->periph_name
, periph
->unit_number
,
1829 (uintmax_t)(((uintmax_t)dp
->secsize
*
1830 dp
->sectors
) / (1024*1024)),
1831 (uintmax_t)dp
->sectors
, dp
->secsize
,
1832 dp
->heads
, dp
->secs_per_track
, dp
->cylinders
);
1833 disk_setdiskinfo(&softc
->disk
, &info
);
1835 CAM_SIM_LOCK(periph
->sim
);
1837 kprintf("%s%d: open removable media: no media present\n",
1838 periph
->periph_name
, periph
->unit_number
);
1839 info
.d_media_blksize
= 512;
1840 disk_setdiskinfo(&softc
->disk
, &info
);
1846 dagetcapacity(struct cam_periph
*periph
)
1848 struct da_softc
*softc
;
1850 struct scsi_read_capacity_data
*rcap
;
1851 struct scsi_read_capacity_data_16
*rcaplong
;
1856 softc
= (struct da_softc
*)periph
->softc
;
1861 /* Do a read capacity */
1862 rcap
= (struct scsi_read_capacity_data
*)kmalloc(sizeof(*rcaplong
),
1863 M_SCSIDA
, M_INTWAIT
);
1865 ccb
= cam_periph_getccb(periph
, /*priority*/1);
1866 scsi_read_capacity(&ccb
->csio
,
1873 ccb
->ccb_h
.ccb_bio
= NULL
;
1875 error
= cam_periph_runccb(ccb
, daerror
,
1876 /*cam_flags*/CAM_RETRY_SELTO
,
1877 /*sense_flags*/SF_RETRY_UA
,
1878 &softc
->device_stats
);
1880 if ((ccb
->ccb_h
.status
& CAM_DEV_QFRZN
) != 0)
1881 cam_release_devq(ccb
->ccb_h
.path
,
1885 /*getcount_only*/0);
1888 block_len
= scsi_4btoul(rcap
->length
);
1889 maxsector
= scsi_4btoul(rcap
->addr
);
1891 if (maxsector
!= 0xffffffff)
1896 rcaplong
= (struct scsi_read_capacity_data_16
*)rcap
;
1898 scsi_read_capacity_16(&ccb
->csio
,
1901 /*tag_action*/ MSG_SIMPLE_Q_TAG
,
1906 /*sense_len*/ SSD_FULL_SIZE
,
1908 ccb
->ccb_h
.ccb_bio
= NULL
;
1910 error
= cam_periph_runccb(ccb
, daerror
,
1911 /*cam_flags*/CAM_RETRY_SELTO
,
1912 /*sense_flags*/SF_RETRY_UA
,
1913 &softc
->device_stats
);
1915 if ((ccb
->ccb_h
.status
& CAM_DEV_QFRZN
) != 0)
1916 cam_release_devq(ccb
->ccb_h
.path
,
1920 /*getcount_only*/0);
1923 block_len
= scsi_4btoul(rcaplong
->length
);
1924 maxsector
= scsi_8btou64(rcaplong
->addr
);
1930 dasetgeom(periph
, block_len
, maxsector
);
1932 xpt_release_ccb(ccb
);
1934 kfree(rcap
, M_SCSIDA
);
1940 dasetgeom(struct cam_periph
*periph
, uint32_t block_len
, uint64_t maxsector
)
1942 struct ccb_calc_geometry ccg
;
1943 struct da_softc
*softc
;
1944 struct disk_params
*dp
;
1946 softc
= (struct da_softc
*)periph
->softc
;
1948 dp
= &softc
->params
;
1949 dp
->secsize
= block_len
;
1950 dp
->sectors
= maxsector
+ 1;
1952 * Have the controller provide us with a geometry
1953 * for this disk. The only time the geometry
1954 * matters is when we boot and the controller
1955 * is the only one knowledgeable enough to come
1956 * up with something that will make this a bootable
1959 xpt_setup_ccb(&ccg
.ccb_h
, periph
->path
, /*priority*/1);
1960 ccg
.ccb_h
.func_code
= XPT_CALC_GEOMETRY
;
1961 ccg
.block_size
= dp
->secsize
;
1962 ccg
.volume_size
= dp
->sectors
;
1964 ccg
.secs_per_track
= 0;
1966 xpt_action((union ccb
*)&ccg
);
1967 if ((ccg
.ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
1969 * We don't know what went wrong here- but just pick
1970 * a geometry so we don't have nasty things like divide
1974 dp
->secs_per_track
= 255;
1975 dp
->cylinders
= dp
->sectors
/ (255 * 255);
1976 if (dp
->cylinders
== 0) {
1980 dp
->heads
= ccg
.heads
;
1981 dp
->secs_per_track
= ccg
.secs_per_track
;
1982 dp
->cylinders
= ccg
.cylinders
;
1987 dasendorderedtag(void *arg
)
1989 struct da_softc
*softc
= arg
;
1991 if (da_send_ordered
) {
1992 if ((softc
->ordered_tag_count
== 0)
1993 && ((softc
->flags
& DA_FLAG_WENT_IDLE
) == 0)) {
1994 softc
->flags
|= DA_FLAG_NEED_OTAG
;
1996 if (softc
->outstanding_cmds
> 0)
1997 softc
->flags
&= ~DA_FLAG_WENT_IDLE
;
1999 softc
->ordered_tag_count
= 0;
2001 /* Queue us up again */
2002 callout_reset(&softc
->sendordered_c
,
2003 (DA_DEFAULT_TIMEOUT
* hz
) / DA_ORDEREDTAG_INTERVAL
,
2004 dasendorderedtag
, softc
);
2008 * Step through all DA peripheral drivers, and if the device is still open,
2009 * sync the disk cache to physical media.
2012 dashutdown(void * arg
, int howto
)
2014 struct cam_periph
*periph
;
2015 struct da_softc
*softc
;
2017 TAILQ_FOREACH(periph
, &dadriver
.units
, unit_links
) {
2020 cam_periph_lock(periph
);
2021 softc
= (struct da_softc
*)periph
->softc
;
2024 * We only sync the cache if the drive is still open, and
2025 * if the drive is capable of it..
2027 if (((softc
->flags
& DA_FLAG_OPEN
) == 0)
2028 || (softc
->quirks
& DA_Q_NO_SYNC_CACHE
)) {
2029 cam_periph_unlock(periph
);
2033 xpt_setup_ccb(&ccb
.ccb_h
, periph
->path
, /*priority*/1);
2035 ccb
.ccb_h
.ccb_state
= DA_CCB_DUMP
;
2036 scsi_synchronize_cache(&ccb
.csio
,
2040 /*begin_lba*/0, /* whole disk */
2045 xpt_polled_action(&ccb
);
2047 if ((ccb
.ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
2048 if (((ccb
.ccb_h
.status
& CAM_STATUS_MASK
) ==
2049 CAM_SCSI_STATUS_ERROR
)
2050 && (ccb
.csio
.scsi_status
== SCSI_STATUS_CHECK_COND
)){
2051 int error_code
, sense_key
, asc
, ascq
;
2053 scsi_extract_sense(&ccb
.csio
.sense_data
,
2054 &error_code
, &sense_key
,
2057 if (sense_key
!= SSD_KEY_ILLEGAL_REQUEST
)
2058 scsi_sense_print(&ccb
.csio
);
2060 xpt_print(periph
->path
, "Synchronize "
2061 "cache failed, status == 0x%x, scsi status "
2062 "== 0x%x\n", ccb
.ccb_h
.status
,
2063 ccb
.csio
.scsi_status
);
2067 if ((ccb
.ccb_h
.status
& CAM_DEV_QFRZN
) != 0)
2068 cam_release_devq(ccb
.ccb_h
.path
,
2072 /*getcount_only*/0);
2074 cam_periph_unlock(periph
);
2078 #else /* !_KERNEL */
2081 * XXX This is only left out of the kernel build to silence warnings. If,
2082 * for some reason this function is used in the kernel, the ifdefs should
2083 * be moved so it is included both in the kernel and userland.
2086 scsi_format_unit(struct ccb_scsiio
*csio
, u_int32_t retries
,
2087 void (*cbfcnp
)(struct cam_periph
*, union ccb
*),
2088 u_int8_t tag_action
, u_int8_t byte2
, u_int16_t ileave
,
2089 u_int8_t
*data_ptr
, u_int32_t dxfer_len
, u_int8_t sense_len
,
2092 struct scsi_format_unit
*scsi_cmd
;
2094 scsi_cmd
= (struct scsi_format_unit
*)&csio
->cdb_io
.cdb_bytes
;
2095 scsi_cmd
->opcode
= FORMAT_UNIT
;
2096 scsi_cmd
->byte2
= byte2
;
2097 scsi_ulto2b(ileave
, scsi_cmd
->interleave
);
2102 /*flags*/ (dxfer_len
> 0) ? CAM_DIR_OUT
: CAM_DIR_NONE
,
2111 #endif /* _KERNEL */