CAM Enhancements for AHCI driver work, import some structures from OpenBSD.
[dragonfly.git] / sys / bus / cam / scsi / scsi_da.c
blob4a8714370b4be45a67be7e3792fef5726418c3c8
1 /*
2 * Implementation of SCSI Direct Access Peripheral driver for CAM.
4 * Copyright (c) 1997 Justin T. Gibbs.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
26 * SUCH DAMAGE.
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>
35 #ifdef _KERNEL
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/buf.h>
39 #include <sys/sysctl.h>
40 #include <sys/taskqueue.h>
41 #include <sys/lock.h>
42 #include <sys/conf.h>
43 #include <sys/devicestat.h>
44 #include <sys/disk.h>
45 #include <sys/dtype.h>
46 #include <sys/eventhandler.h>
47 #include <sys/malloc.h>
48 #include <sys/cons.h>
49 #include <sys/proc.h>
50 #include <sys/buf2.h>
51 #include <sys/thread2.h>
52 #endif /* _KERNEL */
54 #ifdef _KERNEL
55 #include <vm/pmap.h>
56 #endif
58 #ifndef _KERNEL
59 #include <stdio.h>
60 #include <string.h>
61 #endif /* _KERNEL */
63 #include "../cam.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"
72 #ifndef _KERNEL
73 #include "scsi_da.h"
74 #endif /* !_KERNEL */
76 #ifdef _KERNEL
77 typedef enum {
78 DA_STATE_PROBE,
79 DA_STATE_PROBE2,
80 DA_STATE_NORMAL
81 } da_state;
83 typedef enum {
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,
92 DA_FLAG_OPEN = 0x100,
93 DA_FLAG_SCTX_INIT = 0x200
94 } da_flags;
96 typedef enum {
97 DA_Q_NONE = 0x00,
98 DA_Q_NO_SYNC_CACHE = 0x01,
99 DA_Q_NO_6_BYTE = 0x02,
100 DA_Q_NO_PREVENT = 0x04
101 } da_quirks;
103 typedef enum {
104 DA_CCB_PROBE = 0x01,
105 DA_CCB_PROBE2 = 0x02,
106 DA_CCB_BUFFER_IO = 0x03,
107 DA_CCB_WAITING = 0x04,
108 DA_CCB_DUMP = 0x05,
109 DA_CCB_TYPE_MASK = 0x0F,
110 DA_CCB_RETRY_UA = 0x10
111 } da_ccb_state;
113 /* Offsets into our private area for storing information */
114 #define ccb_state ppriv_field0
115 #define ccb_bio ppriv_ptr1
117 struct disk_params {
118 u_int8_t heads;
119 u_int32_t cylinders;
120 u_int8_t secs_per_track;
121 u_int32_t secsize; /* Number of bytes/sector */
122 u_int64_t sectors; /* total number sectors */
125 struct da_softc {
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;
130 da_state state;
131 da_flags flags;
132 da_quirks quirks;
133 int minimum_cmd_size;
134 int ordered_tag_count;
135 int outstanding_cmds;
136 struct disk_params params;
137 struct disk disk;
138 union ccb saved_ccb;
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;
147 da_quirks quirks;
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
167 /* See above. */
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)
193 * (PR 8882).
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
233 * 6-byte CDB.
234 * Reported by: Adam McDougall <bsdx@spawnet.com>
236 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
237 /*quirks*/ DA_Q_NO_6_BYTE
240 /* See above. */
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
263 * PR: kern/51675
265 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
266 /*quirks*/ DA_Q_NO_SYNC_CACHE
270 * Power Quotient Int. (PQI) USB flash key
271 * PR: kern/53067
273 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
274 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
278 * Creative Nomad MUVO mp3 player (USB)
279 * PR: kern/53094
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
287 * PR: kern/54737
289 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
290 /*quirks*/ DA_Q_NO_SYNC_CACHE
294 * FreeDik USB Mini Data Drive
295 * PR: kern/54786
297 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
298 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
302 * Sigmatel USB Flash MP3 Player
303 * PR: kern/57046
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
311 * PR: kern/63645
313 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
314 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
318 * SEAGRAND NP-900 MP3 Player
319 * PR: kern/64563
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
335 * PR: kern/70158
337 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
338 /*quirks*/ DA_Q_NO_SYNC_CACHE
342 * ZICPlay USB MP3 Player with FM
343 * PR: kern/75057
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>
367 * PR: usb/89889
369 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
370 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
374 * Qware BeatZkey! Pro
375 * PR: usb/79164
377 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
378 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
382 * Time DPA20B 1GB MP3 Player
383 * PR: usb/81846
385 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
386 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
390 * Samsung USB key 128Mb
391 * PR: usb/90081
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.
399 * PR: usb/89196
401 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
402 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
406 * Creative MUVO Slim mp3 player (USB)
407 * PR: usb/86131
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)
415 * PR: usb/80487
417 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
418 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
422 * SanDisk Micro Cruzer 128MB
423 * PR: usb/75970
425 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
426 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
430 * TOSHIBA TransMemory USB sticks
431 * PR: kern/94660
433 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
434 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
438 * PNY USB Flash keys
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
447 * PR: usb/94647
449 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
450 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
454 * Rekam Digital CAMERA
455 * PR: usb/98713
457 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
458 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
462 * iRiver H10 MP3 player
463 * PR: usb/102547
465 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
466 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
470 * iRiver U10 MP3 player
471 * PR: usb/92306
473 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
474 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
478 * X-Micro Flash Disk
479 * PR: usb/96901
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
487 * PR: usb/96546
489 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
490 "1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
494 * Denver MP3 player
495 * PR: usb/107101
497 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
498 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
502 * Philips USB Key Audio KEY013
503 * PR: usb/68412
505 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
506 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
510 * JNC MP3 Player
511 * PR: usb/94439
513 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
514 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
518 * SAMSUNG MP0402H
519 * PR: usb/108427
521 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
522 /*quirks*/ DA_Q_NO_SYNC_CACHE
526 * I/O Magic USB flash - Giga Bank
527 * PR: usb/108810
529 {T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
530 /*quirks*/ DA_Q_NO_SYNC_CACHE
534 * JoyFly 128mb USB Flash Drive
535 * PR: 96133
537 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
538 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
542 * ChipsBnk usb stick
543 * PR: 103702
545 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
546 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
550 * Samsung YP-U3 mp3-player
551 * PR: 125398
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,
588 uint64_t maxsector);
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 */
595 #endif
597 #ifndef DA_DEFAULT_RETRY
598 #define DA_DEFAULT_RETRY 4
599 #endif
601 #ifndef DA_DEFAULT_SEND_ORDERED
602 #define DA_DEFAULT_SEND_ORDERED 1
603 #endif
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
636 #endif
638 static struct periph_driver dadriver =
640 dainit, "da",
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 },
648 .d_open = daopen,
649 .d_close = daclose,
650 .d_read = physread,
651 .d_write = physwrite,
652 .d_strategy = dastrategy,
653 .d_dump = dadump
656 static struct extend_array *daperiphs;
658 MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
660 static int
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;
667 int unit;
668 int error;
670 unit = dkunit(dev);
671 periph = cam_extend_get(daperiphs, unit);
672 if (periph == NULL) {
673 return (ENXIO);
676 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
677 return(ENXIO);
680 cam_periph_lock(periph);
681 if ((error = cam_periph_hold(periph, PCATCH)) != 0) {
682 cam_periph_unlock(periph);
683 cam_periph_release(periph);
684 return (error);
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),
693 unit));
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);
703 #if 0
704 /* Do a read capacity */
706 struct scsi_read_capacity_data *rcap;
707 union ccb *ccb;
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,
713 /*retries*/1,
714 /*cbfncp*/dadone,
715 MSG_SIMPLE_Q_TAG,
716 rcap,
717 SSD_FULL_SIZE,
718 /*timeout*/60000);
719 ccb->ccb_h.ccb_bio = NULL;
721 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
722 /*sense_flags*/SF_RETRY_UA |
723 SF_RETRY_SELTO,
724 &softc->device_stats);
726 xpt_release_ccb(ccb);
728 if (error == 0) {
729 dasetgeom(periph, rcap);
732 kfree(rcap, M_SCSIDA);
734 #endif
736 if (error == 0) {
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);
751 #if 0
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)));
756 #endif
759 * Mandatory fields
761 info.d_media_blksize = softc->params.secsize;
762 info.d_media_blocks = softc->params.sectors;
763 info.d_media_size = 0;
766 * Optional fields
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;
786 if (error == 0) {
787 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
788 (softc->quirks & DA_Q_NO_PREVENT) == 0)
789 daprevent(periph, PR_PREVENT);
790 } else {
791 softc->flags &= ~DA_FLAG_OPEN;
792 cam_periph_release(periph);
794 cam_periph_unhold(periph, 1);
795 return (error);
798 static int
799 daclose(struct dev_close_args *ap)
801 cdev_t dev = ap->a_head.a_dev;
802 struct cam_periph *periph;
803 struct da_softc *softc;
804 int unit;
805 int error;
807 unit = dkunit(dev);
808 periph = cam_extend_get(daperiphs, unit);
809 if (periph == NULL)
810 return (ENXIO);
812 cam_periph_lock(periph);
813 if ((error = cam_periph_hold(periph, 0)) != 0) {
814 cam_periph_unlock(periph);
815 cam_periph_release(periph);
816 return (error);
819 softc = (struct da_softc *)periph->softc;
821 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
822 union ccb *ccb;
824 ccb = cam_periph_getccb(periph, /*priority*/1);
826 scsi_synchronize_cache(&ccb->csio,
827 /*retries*/1,
828 /*cbfcnp*/dadone,
829 MSG_SIMPLE_Q_TAG,
830 /*begin_lba*/0,/* Cover the whole disk */
831 /*lb_count*/0,
832 SSD_FULL_SIZE,
833 5 * 60 * 1000);
835 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
836 /*sense_flags*/SF_RETRY_UA,
837 &softc->device_stats);
839 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
840 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
841 CAM_SCSI_STATUS_ERROR) {
842 int asc, ascq;
843 int sense_key, error_code;
845 scsi_extract_sense(&ccb->csio.sense_data,
846 &error_code,
847 &sense_key,
848 &asc, &ascq);
849 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
850 scsi_sense_print(&ccb->csio);
851 } else {
852 xpt_print(periph->path, "Synchronize cache "
853 "failed, status == 0x%x, scsi status == "
854 "0x%x\n", ccb->csio.ccb_h.status,
855 ccb->csio.scsi_status);
859 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
860 cam_release_devq(ccb->ccb_h.path,
861 /*relsim_flags*/0,
862 /*reduction*/0,
863 /*timeout*/0,
864 /*getcount_only*/0);
866 xpt_release_ccb(ccb);
870 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
871 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
872 daprevent(periph, PR_ALLOW);
874 * If we've got removeable media, mark the blocksize as
875 * unavailable, since it could change when new media is
876 * inserted.
878 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
882 * Don't compound any ref counting software bugs with more.
884 if (softc->flags & DA_FLAG_OPEN) {
885 softc->flags &= ~DA_FLAG_OPEN;
886 cam_periph_release(periph);
887 } else {
888 xpt_print(periph->path,
889 "daclose() called on an already closed device!\n");
891 cam_periph_unhold(periph, 1);
892 return (0);
896 * Actually translate the requested transfer into one the physical driver
897 * can understand. The transfer is described by a buf and will include
898 * only one physical transfer.
900 static int
901 dastrategy(struct dev_strategy_args *ap)
903 cdev_t dev = ap->a_head.a_dev;
904 struct bio *bio = ap->a_bio;
905 struct buf *bp = bio->bio_buf;
906 struct cam_periph *periph;
907 struct da_softc *softc;
908 u_int unit;
909 u_int part;
911 unit = dkunit(dev);
912 part = dkpart(dev);
913 periph = cam_extend_get(daperiphs, unit);
914 if (periph == NULL) {
915 bp->b_error = ENXIO;
916 goto bad;
918 softc = (struct da_softc *)periph->softc;
920 cam_periph_lock(periph);
922 #if 0
924 * check it's not too big a transfer for our adapter
926 scsi_minphys(bp, &sd_switch);
927 #endif
930 * Mask interrupts so that the pack cannot be invalidated until
931 * after we are in the queue. Otherwise, we might not properly
932 * clean up one of the buffers.
936 * If the device has been made invalid, error out
938 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
939 cam_periph_unlock(periph);
940 bp->b_error = ENXIO;
941 goto bad;
945 * Place it in the queue of disk activities for this disk
947 bioqdisksort(&softc->bio_queue, bio);
950 * Schedule ourselves for performing the work.
952 xpt_schedule(periph, /* XXX priority */1);
953 cam_periph_unlock(periph);
955 return(0);
956 bad:
957 bp->b_flags |= B_ERROR;
960 * Correctly set the buf to indicate a completed xfer
962 bp->b_resid = bp->b_bcount;
963 biodone(bio);
964 return(0);
967 static int
968 dadump(struct dev_dump_args *ap)
970 cdev_t dev = ap->a_head.a_dev;
971 struct cam_periph *periph;
972 struct da_softc *softc;
973 u_int unit;
974 long blkcnt;
975 vm_paddr_t addr;
976 struct ccb_scsiio csio;
977 int dumppages = MAXDUMPPGS;
978 int i;
980 /* toss any characters present prior to dump */
981 while (cncheckc() != -1)
984 unit = dkunit(dev);
985 periph = cam_extend_get(daperiphs, unit);
986 if (periph == NULL) {
987 return (ENXIO);
989 softc = (struct da_softc *)periph->softc;
991 cam_periph_lock(periph);
992 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
993 cam_periph_unlock(periph);
994 return (ENXIO);
997 addr = 0; /* starting address */
998 blkcnt = howmany(PAGE_SIZE, ap->a_secsize);
1000 while (ap->a_count > 0) {
1001 caddr_t va = NULL;
1003 if ((ap->a_count / blkcnt) < dumppages)
1004 dumppages = ap->a_count / blkcnt;
1006 for (i = 0; i < dumppages; ++i) {
1007 vm_paddr_t a = addr + (i * PAGE_SIZE);
1008 if (is_physical_memory(a))
1009 va = pmap_kenter_temporary(trunc_page(a), i);
1010 else
1011 va = pmap_kenter_temporary(trunc_page(0), i);
1014 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
1015 csio.ccb_h.flags |= CAM_POLLED;
1016 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1017 scsi_read_write(&csio,
1018 /*retries*/1,
1019 dadone,
1020 MSG_ORDERED_Q_TAG,
1021 /*read*/FALSE,
1022 /*byte2*/0,
1023 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1024 ap->a_blkno,
1025 blkcnt * dumppages,
1026 /*data_ptr*/(u_int8_t *) va,
1027 /*dxfer_len*/blkcnt * ap->a_secsize * dumppages,
1028 /*sense_len*/SSD_FULL_SIZE,
1029 DA_DEFAULT_TIMEOUT * 1000);
1030 xpt_polled_action((union ccb *)&csio);
1032 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1033 kprintf("Aborting dump due to I/O error.\n");
1034 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
1035 CAM_SCSI_STATUS_ERROR)
1036 scsi_sense_print(&csio);
1037 else
1038 kprintf("status == 0x%x, scsi status == 0x%x\n",
1039 csio.ccb_h.status, csio.scsi_status);
1040 return(EIO);
1043 if (dumpstatus(addr, (off_t)ap->a_count * softc->params.secsize) < 0)
1044 return (EINTR);
1046 /* update block count */
1047 ap->a_count -= blkcnt * dumppages;
1048 ap->a_blkno += blkcnt * dumppages;
1049 addr += PAGE_SIZE * dumppages;
1053 * Sync the disk cache contents to the physical media.
1055 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1057 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
1058 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1059 scsi_synchronize_cache(&csio,
1060 /*retries*/1,
1061 /*cbfcnp*/dadone,
1062 MSG_SIMPLE_Q_TAG,
1063 /*begin_lba*/0,/* Cover the whole disk */
1064 /*lb_count*/0,
1065 SSD_FULL_SIZE,
1066 5 * 60 * 1000);
1067 xpt_polled_action((union ccb *)&csio);
1069 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1070 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
1071 CAM_SCSI_STATUS_ERROR) {
1072 int asc, ascq;
1073 int sense_key, error_code;
1075 scsi_extract_sense(&csio.sense_data,
1076 &error_code,
1077 &sense_key,
1078 &asc, &ascq);
1079 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1080 scsi_sense_print(&csio);
1081 } else {
1082 xpt_print(periph->path, "Synchronize cache "
1083 "failed, status == 0x%x, scsi status == "
1084 "0x%x\n", csio.ccb_h.status,
1085 csio.scsi_status);
1089 cam_periph_unlock(periph);
1090 return (0);
1093 static void
1094 dainit(void)
1096 cam_status status;
1099 * Create our extend array for storing the devices we attach to.
1101 daperiphs = cam_extend_new();
1102 if (daperiphs == NULL) {
1103 kprintf("da: Failed to alloc extend array!\n");
1104 return;
1107 callout_init(&dasendorderedtag_ch);
1110 * Install a global async callback. This callback will
1111 * receive async callbacks like "new device found".
1113 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1115 if (status != CAM_REQ_CMP) {
1116 kprintf("da: Failed to attach master async callback "
1117 "due to status 0x%x!\n", status);
1118 } else if (da_send_ordered) {
1120 /* Register our shutdown event handler */
1121 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1122 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1123 kprintf("dainit: shutdown event registration failed!\n");
1127 static void
1128 daoninvalidate(struct cam_periph *periph)
1130 struct da_softc *softc;
1131 struct bio *q_bio;
1132 struct buf *q_bp;
1134 softc = (struct da_softc *)periph->softc;
1137 * De-register any async callbacks.
1139 xpt_register_async(0, daasync, periph, periph->path);
1141 softc->flags |= DA_FLAG_PACK_INVALID;
1144 * Return all queued I/O with ENXIO.
1145 * XXX Handle any transactions queued to the card
1146 * with XPT_ABORT_CCB.
1148 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL){
1149 bioq_remove(&softc->bio_queue, q_bio);
1150 q_bp = q_bio->bio_buf;
1151 q_bp->b_resid = q_bp->b_bcount;
1152 q_bp->b_error = ENXIO;
1153 q_bp->b_flags |= B_ERROR;
1154 biodone(q_bio);
1156 xpt_print(periph->path, "lost device\n");
1159 static void
1160 dacleanup(struct cam_periph *periph)
1162 struct da_softc *softc;
1164 softc = (struct da_softc *)periph->softc;
1166 devstat_remove_entry(&softc->device_stats);
1167 cam_extend_release(daperiphs, periph->unit_number);
1168 xpt_print(periph->path, "removing device entry\n");
1170 * If we can't free the sysctl tree, oh well...
1172 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1173 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1174 xpt_print(periph->path, "can't remove sysctl context\n");
1176 periph->softc = NULL;
1177 if (softc->disk.d_rawdev) {
1178 cam_periph_unlock(periph);
1179 disk_destroy(&softc->disk);
1180 cam_periph_lock(periph);
1183 callout_stop(&softc->sendordered_c);
1184 kfree(softc, M_DEVBUF);
1187 static void
1188 daasync(void *callback_arg, u_int32_t code,
1189 struct cam_path *path, void *arg)
1191 struct cam_periph *periph;
1193 periph = (struct cam_periph *)callback_arg;
1194 switch (code) {
1195 case AC_FOUND_DEVICE:
1197 struct ccb_getdev *cgd;
1198 struct cam_sim *sim;
1199 cam_status status;
1201 cgd = (struct ccb_getdev *)arg;
1202 if (cgd == NULL)
1203 break;
1205 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1206 && SID_TYPE(&cgd->inq_data) != T_RBC
1207 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1208 break;
1211 * Allocate a peripheral instance for
1212 * this device and start the probe
1213 * process.
1215 sim = xpt_path_sim(cgd->ccb_h.path);
1216 status = cam_periph_alloc(daregister, daoninvalidate,
1217 dacleanup, dastart,
1218 "da", CAM_PERIPH_BIO,
1219 cgd->ccb_h.path, daasync,
1220 AC_FOUND_DEVICE, cgd);
1222 if (status != CAM_REQ_CMP
1223 && status != CAM_REQ_INPROG)
1224 kprintf("daasync: Unable to attach to new device "
1225 "due to status 0x%x\n", status);
1226 break;
1228 case AC_SENT_BDR:
1229 case AC_BUS_RESET:
1231 struct da_softc *softc;
1232 struct ccb_hdr *ccbh;
1234 softc = (struct da_softc *)periph->softc;
1236 * Don't fail on the expected unit attention
1237 * that will occur.
1239 softc->flags |= DA_FLAG_RETRY_UA;
1240 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1241 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1242 /* FALLTHROUGH*/
1244 default:
1245 cam_periph_async(periph, code, path, arg);
1246 break;
1250 static void
1251 dasysctlinit(void *context, int pending)
1253 struct cam_periph *periph;
1254 struct da_softc *softc;
1255 char tmpstr[80], tmpstr2[80];
1257 periph = (struct cam_periph *)context;
1258 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1259 return;
1261 softc = (struct da_softc *)periph->softc;
1262 ksnprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1263 ksnprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1265 sysctl_ctx_init(&softc->sysctl_ctx);
1266 softc->flags |= DA_FLAG_SCTX_INIT;
1267 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1268 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1269 CTLFLAG_RD, 0, tmpstr);
1270 if (softc->sysctl_tree == NULL) {
1271 kprintf("dasysctlinit: unable to allocate sysctl tree\n");
1272 cam_periph_release(periph);
1273 return;
1277 * Now register the sysctl handler, so the user can the value on
1278 * the fly.
1280 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1281 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1282 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1283 "Minimum CDB size");
1285 cam_periph_release(periph);
1288 static int
1289 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1291 int error, value;
1293 value = *(int *)arg1;
1295 error = sysctl_handle_int(oidp, &value, 0, req);
1297 if ((error != 0)
1298 || (req->newptr == NULL))
1299 return (error);
1302 * Acceptable values here are 6, 10 or 12, or 16.
1304 if (value < 6)
1305 value = 6;
1306 else if ((value > 6)
1307 && (value <= 10))
1308 value = 10;
1309 else if ((value > 10)
1310 && (value <= 12))
1311 value = 12;
1312 else if (value > 12)
1313 value = 16;
1315 *(int *)arg1 = value;
1317 return (0);
1320 static cam_status
1321 daregister(struct cam_periph *periph, void *arg)
1323 struct da_softc *softc;
1324 struct ccb_pathinq cpi;
1325 struct ccb_getdev *cgd;
1326 char tmpstr[80];
1327 caddr_t match;
1329 cgd = (struct ccb_getdev *)arg;
1330 if (periph == NULL) {
1331 kprintf("daregister: periph was NULL!!\n");
1332 return(CAM_REQ_CMP_ERR);
1335 if (cgd == NULL) {
1336 kprintf("daregister: no getdev CCB, can't register device\n");
1337 return(CAM_REQ_CMP_ERR);
1340 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
1341 LIST_INIT(&softc->pending_ccbs);
1342 softc->state = DA_STATE_PROBE;
1343 bioq_init(&softc->bio_queue);
1344 if (SID_IS_REMOVABLE(&cgd->inq_data))
1345 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1346 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1347 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1349 periph->softc = softc;
1351 cam_extend_set(daperiphs, periph->unit_number, periph);
1354 * See if this device has any quirks.
1356 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1357 (caddr_t)da_quirk_table,
1358 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1359 sizeof(*da_quirk_table), scsi_inquiry_match);
1361 if (match != NULL)
1362 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1363 else
1364 softc->quirks = DA_Q_NONE;
1366 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1368 /* Check if the SIM does not want 6 byte commands */
1369 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
1370 cpi.ccb_h.func_code = XPT_PATH_INQ;
1371 xpt_action((union ccb *)&cpi);
1372 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1373 softc->quirks |= DA_Q_NO_6_BYTE;
1376 * RBC devices don't have to support READ(6), only READ(10).
1378 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1379 softc->minimum_cmd_size = 10;
1380 else
1381 softc->minimum_cmd_size = 6;
1384 * Load the user's default, if any.
1386 ksnprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1387 periph->unit_number);
1388 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1391 * 6, 10, 12, and 16 are the currently permissible values.
1393 if (softc->minimum_cmd_size < 6)
1394 softc->minimum_cmd_size = 6;
1395 else if ((softc->minimum_cmd_size > 6)
1396 && (softc->minimum_cmd_size <= 10))
1397 softc->minimum_cmd_size = 10;
1398 else if ((softc->minimum_cmd_size > 10)
1399 && (softc->minimum_cmd_size <= 12))
1400 softc->minimum_cmd_size = 12;
1401 else if (softc->minimum_cmd_size > 12)
1402 softc->minimum_cmd_size = 16;
1405 * The DA driver supports a blocksize, but
1406 * we don't know the blocksize until we do
1407 * a read capacity. So, set a flag to
1408 * indicate that the blocksize is
1409 * unavailable right now. We'll clear the
1410 * flag as soon as we've done a read capacity.
1412 devstat_add_entry(&softc->device_stats, "da",
1413 periph->unit_number, 0,
1414 DEVSTAT_BS_UNAVAILABLE,
1415 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
1416 DEVSTAT_PRIORITY_DISK);
1419 * Register this media as a disk
1422 CAM_SIM_UNLOCK(periph->sim);
1423 disk_create(periph->unit_number, &softc->disk, &da_ops);
1424 softc->disk.d_rawdev->si_iosize_max = MAXPHYS;
1425 CAM_SIM_LOCK(periph->sim);
1428 * Add async callbacks for bus reset and
1429 * bus device reset calls. I don't bother
1430 * checking if this fails as, in most cases,
1431 * the system will function just fine without
1432 * them and the only alternative would be to
1433 * not attach the device on failure.
1435 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
1436 daasync, periph, periph->path);
1439 * Take an exclusive refcount on the periph while dastart is called
1440 * to finish the probe. The reference will be dropped in dadone at
1441 * the end of probe.
1443 cam_periph_hold(periph, 0);
1444 xpt_schedule(periph, /*priority*/5);
1447 * Schedule a periodic event to occasionally send an
1448 * ordered tag to a device.
1450 callout_init(&softc->sendordered_c);
1451 callout_reset(&softc->sendordered_c,
1452 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
1453 dasendorderedtag, softc);
1455 return(CAM_REQ_CMP);
1458 static void
1459 dastart(struct cam_periph *periph, union ccb *start_ccb)
1461 struct da_softc *softc;
1463 softc = (struct da_softc *)periph->softc;
1465 switch (softc->state) {
1466 case DA_STATE_NORMAL:
1468 /* Pull a buffer from the queue and get going on it */
1469 struct bio *bio;
1470 struct buf *bp;
1471 u_int8_t tag_code;
1474 * See if there is a buf with work for us to do..
1476 bio = bioq_first(&softc->bio_queue);
1477 if (periph->immediate_priority <= periph->pinfo.priority) {
1478 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1479 ("queuing for immediate ccb\n"));
1480 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1481 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1482 periph_links.sle);
1483 periph->immediate_priority = CAM_PRIORITY_NONE;
1484 wakeup(&periph->ccb_list);
1485 if (bio != NULL) {
1487 * Have more work to do, so ensure we stay
1488 * scheduled
1490 xpt_schedule(periph, /* XXX priority */1);
1492 break;
1494 if (bio == NULL) {
1495 xpt_release_ccb(start_ccb);
1496 break;
1500 * We can queue new work.
1502 bioq_remove(&softc->bio_queue, bio);
1503 bp = bio->bio_buf;
1505 devstat_start_transaction(&softc->device_stats);
1507 if ((bp->b_flags & B_ORDERED) != 0 ||
1508 (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1509 softc->flags &= ~DA_FLAG_NEED_OTAG;
1510 softc->ordered_tag_count++;
1511 tag_code = MSG_ORDERED_Q_TAG;
1512 } else {
1513 tag_code = MSG_SIMPLE_Q_TAG;
1516 switch(bp->b_cmd) {
1517 case BUF_CMD_READ:
1518 case BUF_CMD_WRITE:
1520 * Block read/write op
1522 KKASSERT(bio->bio_offset % softc->params.secsize == 0);
1524 scsi_read_write(
1525 &start_ccb->csio,
1526 da_retry_count, /* retries */
1527 dadone,
1528 tag_code,
1529 (bp->b_cmd == BUF_CMD_READ),
1530 0, /* byte2 */
1531 softc->minimum_cmd_size,
1532 bio->bio_offset / softc->params.secsize,
1533 bp->b_bcount / softc->params.secsize,
1534 bp->b_data,
1535 bp->b_bcount,
1536 SSD_FULL_SIZE, /* sense_len */
1537 da_default_timeout * 1000
1539 break;
1540 case BUF_CMD_FLUSH:
1541 scsi_synchronize_cache(
1542 &start_ccb->csio,
1543 1, /* retries */
1544 dadone, /* cbfcnp */
1545 MSG_SIMPLE_Q_TAG,
1546 0, /* lba */
1547 0, /* count (whole disk) */
1548 SSD_FULL_SIZE,
1549 da_default_timeout*1000 /* timeout */
1551 break;
1552 default:
1553 panic("dastart: unrecognized bio cmd %d", bp->b_cmd);
1554 break; /* NOT REACHED */
1558 * Block out any asyncronous callbacks
1559 * while we touch the pending ccb list.
1561 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1562 LIST_INSERT_HEAD(&softc->pending_ccbs,
1563 &start_ccb->ccb_h, periph_links.le);
1564 softc->outstanding_cmds++;
1566 /* We expect a unit attention from this device */
1567 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1568 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1569 softc->flags &= ~DA_FLAG_RETRY_UA;
1572 start_ccb->ccb_h.ccb_bio = bio;
1573 xpt_action(start_ccb);
1576 * Be sure we stay scheduled if we have more work to do.
1578 bio = bioq_first(&softc->bio_queue);
1579 if (bio != NULL)
1580 xpt_schedule(periph, 1);
1581 break;
1583 case DA_STATE_PROBE:
1585 struct ccb_scsiio *csio;
1586 struct scsi_read_capacity_data *rcap;
1588 rcap = kmalloc(sizeof(*rcap), M_SCSIDA, M_INTWAIT | M_ZERO);
1589 csio = &start_ccb->csio;
1590 scsi_read_capacity(csio,
1591 /*retries*/4,
1592 dadone,
1593 MSG_SIMPLE_Q_TAG,
1594 rcap,
1595 SSD_FULL_SIZE,
1596 /*timeout*/5000);
1597 start_ccb->ccb_h.ccb_bio = NULL;
1598 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1599 xpt_action(start_ccb);
1600 break;
1602 case DA_STATE_PROBE2:
1604 struct ccb_scsiio *csio;
1605 struct scsi_read_capacity_data_16 *rcaplong;
1607 rcaplong = kmalloc(sizeof(*rcaplong), M_SCSIDA,
1608 M_INTWAIT | M_ZERO);
1609 if (rcaplong == NULL) {
1610 kprintf("dastart: Couldn't allocate read_capacity\n");
1611 /* da_free_periph??? */
1612 break;
1614 csio = &start_ccb->csio;
1615 scsi_read_capacity_16(csio,
1616 /*retries*/ 4,
1617 /*cbfcnp*/ dadone,
1618 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1619 /*lba*/ 0,
1620 /*reladr*/ 0,
1621 /*pmi*/ 0,
1622 rcaplong,
1623 /*sense_len*/ SSD_FULL_SIZE,
1624 /*timeout*/ 60000);
1625 start_ccb->ccb_h.ccb_bio = NULL;
1626 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1627 xpt_action(start_ccb);
1628 break;
1633 static int
1634 cmd6workaround(union ccb *ccb)
1636 struct scsi_rw_6 cmd6;
1637 struct scsi_rw_10 *cmd10;
1638 struct da_softc *softc;
1639 u_int8_t *cdb;
1640 int frozen;
1642 cdb = ccb->csio.cdb_io.cdb_bytes;
1644 /* Translation only possible if CDB is an array and cmd is R/W6 */
1645 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1646 (*cdb != READ_6 && *cdb != WRITE_6))
1647 return 0;
1649 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
1650 "increasing minimum_cmd_size to 10.\n");
1651 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1652 softc->minimum_cmd_size = 10;
1654 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1655 cmd10 = (struct scsi_rw_10 *)cdb;
1656 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1657 cmd10->byte2 = 0;
1658 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1659 cmd10->reserved = 0;
1660 scsi_ulto2b(cmd6.length, cmd10->length);
1661 cmd10->control = cmd6.control;
1662 ccb->csio.cdb_len = sizeof(*cmd10);
1664 /* Requeue request, unfreezing queue if necessary */
1665 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1666 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1667 xpt_action(ccb);
1668 if (frozen) {
1669 cam_release_devq(ccb->ccb_h.path,
1670 /*relsim_flags*/0,
1671 /*reduction*/0,
1672 /*timeout*/0,
1673 /*getcount_only*/0);
1675 return (ERESTART);
1678 static void
1679 dadone(struct cam_periph *periph, union ccb *done_ccb)
1681 struct da_softc *softc;
1682 struct ccb_scsiio *csio;
1684 softc = (struct da_softc *)periph->softc;
1685 csio = &done_ccb->csio;
1686 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1687 case DA_CCB_BUFFER_IO:
1689 struct buf *bp;
1690 struct bio *bio;
1692 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1693 bp = bio->bio_buf;
1694 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1695 int error;
1696 int sf;
1698 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1699 sf = SF_RETRY_UA;
1700 else
1701 sf = 0;
1703 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1704 if (error == ERESTART) {
1706 * A retry was scheuled, so
1707 * just return.
1709 return;
1711 if (error != 0) {
1712 struct bio *q_bio;
1713 struct buf *q_bp;
1715 if (error == ENXIO) {
1717 * Catastrophic error. Mark our pack as
1718 * invalid.
1721 * XXX See if this is really a media
1722 * XXX change first?
1724 xpt_print(periph->path,
1725 "Invalidating pack\n");
1726 softc->flags |= DA_FLAG_PACK_INVALID;
1730 * return all queued I/O with EIO, so that
1731 * the client can retry these I/Os in the
1732 * proper order should it attempt to recover.
1734 while ((q_bio = bioq_first(&softc->bio_queue))
1735 != NULL) {
1736 bioq_remove(&softc->bio_queue, q_bio);
1737 q_bp = q_bio->bio_buf;
1738 q_bp->b_resid = q_bp->b_bcount;
1739 q_bp->b_error = EIO;
1740 q_bp->b_flags |= B_ERROR;
1741 biodone(q_bio);
1743 bp->b_error = error;
1744 bp->b_resid = bp->b_bcount;
1745 bp->b_flags |= B_ERROR;
1746 } else {
1747 bp->b_resid = csio->resid;
1748 bp->b_error = 0;
1749 if (bp->b_resid != 0)
1750 bp->b_flags |= B_ERROR;
1752 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1753 cam_release_devq(done_ccb->ccb_h.path,
1754 /*relsim_flags*/0,
1755 /*reduction*/0,
1756 /*timeout*/0,
1757 /*getcount_only*/0);
1758 } else {
1759 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1760 panic("REQ_CMP with QFRZN");
1761 bp->b_resid = csio->resid;
1762 if (csio->resid > 0)
1763 bp->b_flags |= B_ERROR;
1767 * Block out any asyncronous callbacks
1768 * while we touch the pending ccb list.
1770 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1771 softc->outstanding_cmds--;
1772 if (softc->outstanding_cmds == 0)
1773 softc->flags |= DA_FLAG_WENT_IDLE;
1775 devstat_end_transaction_buf(&softc->device_stats, bp);
1776 biodone(bio);
1777 break;
1779 case DA_CCB_PROBE:
1780 case DA_CCB_PROBE2:
1782 struct scsi_read_capacity_data *rdcap;
1783 struct scsi_read_capacity_data_16 *rcaplong;
1784 char announce_buf[80];
1786 rdcap = NULL;
1787 rcaplong = NULL;
1788 if (softc->state == DA_STATE_PROBE)
1789 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1790 else
1791 rcaplong = (struct scsi_read_capacity_data_16 *)
1792 csio->data_ptr;
1794 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1795 struct disk_params *dp;
1796 uint32_t block_size;
1797 uint64_t maxsector;
1799 if (softc->state == DA_STATE_PROBE) {
1800 block_size = scsi_4btoul(rdcap->length);
1801 maxsector = scsi_4btoul(rdcap->addr);
1804 * According to SBC-2, if the standard 10
1805 * byte READ CAPACITY command returns 2^32,
1806 * we should issue the 16 byte version of
1807 * the command, since the device in question
1808 * has more sectors than can be represented
1809 * with the short version of the command.
1811 if (maxsector == 0xffffffff) {
1812 softc->state = DA_STATE_PROBE2;
1813 kfree(rdcap, M_SCSIDA);
1814 xpt_release_ccb(done_ccb);
1815 xpt_schedule(periph, /*priority*/5);
1816 return;
1818 } else {
1819 block_size = scsi_4btoul(rcaplong->length);
1820 maxsector = scsi_8btou64(rcaplong->addr);
1822 dasetgeom(periph, block_size, maxsector);
1823 dp = &softc->params;
1824 ksnprintf(announce_buf, sizeof(announce_buf),
1825 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1826 (uintmax_t) (((uintmax_t)dp->secsize *
1827 dp->sectors) / (1024*1024)),
1828 (uintmax_t)dp->sectors,
1829 dp->secsize, dp->heads, dp->secs_per_track,
1830 dp->cylinders);
1831 } else {
1832 int error;
1834 announce_buf[0] = '\0';
1837 * Retry any UNIT ATTENTION type errors. They
1838 * are expected at boot.
1840 error = daerror(done_ccb, CAM_RETRY_SELTO,
1841 SF_RETRY_UA|SF_NO_PRINT);
1842 if (error == ERESTART) {
1844 * A retry was scheuled, so
1845 * just return.
1847 return;
1848 } else if (error != 0) {
1849 struct scsi_sense_data *sense;
1850 int asc, ascq;
1851 int sense_key, error_code;
1852 int have_sense;
1853 cam_status status;
1854 struct ccb_getdev cgd;
1856 /* Don't wedge this device's queue */
1857 status = done_ccb->ccb_h.status;
1858 if ((status & CAM_DEV_QFRZN) != 0)
1859 cam_release_devq(done_ccb->ccb_h.path,
1860 /*relsim_flags*/0,
1861 /*reduction*/0,
1862 /*timeout*/0,
1863 /*getcount_only*/0);
1866 xpt_setup_ccb(&cgd.ccb_h,
1867 done_ccb->ccb_h.path,
1868 /* priority */ 1);
1869 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1870 xpt_action((union ccb *)&cgd);
1872 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1873 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1874 || ((status & CAM_AUTOSNS_VALID) == 0))
1875 have_sense = FALSE;
1876 else
1877 have_sense = TRUE;
1879 if (have_sense) {
1880 sense = &csio->sense_data;
1881 scsi_extract_sense(sense, &error_code,
1882 &sense_key,
1883 &asc, &ascq);
1886 * Attach to anything that claims to be a
1887 * direct access or optical disk device,
1888 * as long as it doesn't return a "Logical
1889 * unit not supported" (0x25) error.
1891 if ((have_sense) && (asc != 0x25)
1892 && (error_code == SSD_CURRENT_ERROR)) {
1893 const char *sense_key_desc;
1894 const char *asc_desc;
1896 scsi_sense_desc(sense_key, asc, ascq,
1897 &cgd.inq_data,
1898 &sense_key_desc,
1899 &asc_desc);
1900 ksnprintf(announce_buf,
1901 sizeof(announce_buf),
1902 "Attempt to query device "
1903 "size failed: %s, %s",
1904 sense_key_desc,
1905 asc_desc);
1906 } else {
1907 if (have_sense)
1908 scsi_sense_print(
1909 &done_ccb->csio);
1910 else {
1911 xpt_print(periph->path,
1912 "got CAM status %#x\n",
1913 done_ccb->ccb_h.status);
1916 xpt_print(periph->path, "fatal error, "
1917 "failed to attach to device\n");
1920 * Free up resources.
1922 cam_periph_invalidate(periph);
1926 kfree(csio->data_ptr, M_SCSIDA);
1927 if (announce_buf[0] != '\0') {
1928 xpt_announce_periph(periph, announce_buf);
1930 * Create our sysctl variables, now that we know
1931 * we have successfully attached.
1933 taskqueue_enqueue(taskqueue_thread[mycpuid],
1934 &softc->sysctl_task);
1936 softc->state = DA_STATE_NORMAL;
1938 * Since our peripheral may be invalidated by an error
1939 * above or an external event, we must release our CCB
1940 * before releasing the probe lock on the peripheral.
1941 * The peripheral will only go away once the last lock
1942 * is removed, and we need it around for the CCB release
1943 * operation.
1945 xpt_release_ccb(done_ccb);
1946 cam_periph_unhold(periph, 0);
1947 return;
1949 case DA_CCB_WAITING:
1951 /* Caller will release the CCB */
1952 wakeup(&done_ccb->ccb_h.cbfcnp);
1953 return;
1955 case DA_CCB_DUMP:
1956 /* No-op. We're polling */
1957 return;
1958 default:
1959 break;
1961 xpt_release_ccb(done_ccb);
1964 static int
1965 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1967 struct da_softc *softc;
1968 struct cam_periph *periph;
1969 int error;
1971 periph = xpt_path_periph(ccb->ccb_h.path);
1972 softc = (struct da_softc *)periph->softc;
1975 * Automatically detect devices that do not support
1976 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
1978 error = 0;
1979 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
1980 error = cmd6workaround(ccb);
1981 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
1982 CAM_SCSI_STATUS_ERROR)
1983 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
1984 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1985 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
1986 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
1987 int sense_key, error_code, asc, ascq;
1989 scsi_extract_sense(&ccb->csio.sense_data,
1990 &error_code, &sense_key, &asc, &ascq);
1991 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
1992 error = cmd6workaround(ccb);
1994 if (error == ERESTART)
1995 return (ERESTART);
1998 * XXX
1999 * Until we have a better way of doing pack validation,
2000 * don't treat UAs as errors.
2002 sense_flags |= SF_RETRY_UA;
2003 return(cam_periph_error(ccb, cam_flags, sense_flags,
2004 &softc->saved_ccb));
2007 static void
2008 daprevent(struct cam_periph *periph, int action)
2010 struct da_softc *softc;
2011 union ccb *ccb;
2012 int error;
2014 softc = (struct da_softc *)periph->softc;
2016 if (((action == PR_ALLOW)
2017 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
2018 || ((action == PR_PREVENT)
2019 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
2020 return;
2023 ccb = cam_periph_getccb(periph, /*priority*/1);
2025 scsi_prevent(&ccb->csio,
2026 /*retries*/1,
2027 /*cbcfp*/dadone,
2028 MSG_SIMPLE_Q_TAG,
2029 action,
2030 SSD_FULL_SIZE,
2031 5000);
2033 error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
2034 SF_RETRY_UA, &softc->device_stats);
2036 if (error == 0) {
2037 if (action == PR_ALLOW)
2038 softc->flags &= ~DA_FLAG_PACK_LOCKED;
2039 else
2040 softc->flags |= DA_FLAG_PACK_LOCKED;
2043 xpt_release_ccb(ccb);
2046 static int
2047 dagetcapacity(struct cam_periph *periph)
2049 struct da_softc *softc;
2050 union ccb *ccb;
2051 struct scsi_read_capacity_data *rcap;
2052 struct scsi_read_capacity_data_16 *rcaplong;
2053 uint32_t block_len;
2054 uint64_t maxsector;
2055 int error;
2057 softc = (struct da_softc *)periph->softc;
2058 block_len = 0;
2059 maxsector = 0;
2060 error = 0;
2062 /* Do a read capacity */
2063 rcap = (struct scsi_read_capacity_data *)kmalloc(sizeof(*rcaplong),
2064 M_SCSIDA, M_INTWAIT);
2066 ccb = cam_periph_getccb(periph, /*priority*/1);
2067 scsi_read_capacity(&ccb->csio,
2068 /*retries*/4,
2069 /*cbfncp*/dadone,
2070 MSG_SIMPLE_Q_TAG,
2071 rcap,
2072 SSD_FULL_SIZE,
2073 /*timeout*/60000);
2074 ccb->ccb_h.ccb_bio = NULL;
2076 error = cam_periph_runccb(ccb, daerror,
2077 /*cam_flags*/CAM_RETRY_SELTO,
2078 /*sense_flags*/SF_RETRY_UA,
2079 &softc->device_stats);
2081 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2082 cam_release_devq(ccb->ccb_h.path,
2083 /*relsim_flags*/0,
2084 /*reduction*/0,
2085 /*timeout*/0,
2086 /*getcount_only*/0);
2088 if (error == 0) {
2089 block_len = scsi_4btoul(rcap->length);
2090 maxsector = scsi_4btoul(rcap->addr);
2092 if (maxsector != 0xffffffff)
2093 goto done;
2094 } else
2095 goto done;
2097 rcaplong = (struct scsi_read_capacity_data_16 *)rcap;
2099 scsi_read_capacity_16(&ccb->csio,
2100 /*retries*/ 4,
2101 /*cbfcnp*/ dadone,
2102 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2103 /*lba*/ 0,
2104 /*reladr*/ 0,
2105 /*pmi*/ 0,
2106 rcaplong,
2107 /*sense_len*/ SSD_FULL_SIZE,
2108 /*timeout*/ 60000);
2109 ccb->ccb_h.ccb_bio = NULL;
2111 error = cam_periph_runccb(ccb, daerror,
2112 /*cam_flags*/CAM_RETRY_SELTO,
2113 /*sense_flags*/SF_RETRY_UA,
2114 &softc->device_stats);
2116 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2117 cam_release_devq(ccb->ccb_h.path,
2118 /*relsim_flags*/0,
2119 /*reduction*/0,
2120 /*timeout*/0,
2121 /*getcount_only*/0);
2123 if (error == 0) {
2124 block_len = scsi_4btoul(rcaplong->length);
2125 maxsector = scsi_8btou64(rcaplong->addr);
2128 done:
2130 if (error == 0)
2131 dasetgeom(periph, block_len, maxsector);
2133 xpt_release_ccb(ccb);
2135 kfree(rcap, M_SCSIDA);
2137 return (error);
2140 static void
2141 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector)
2143 struct ccb_calc_geometry ccg;
2144 struct da_softc *softc;
2145 struct disk_params *dp;
2147 softc = (struct da_softc *)periph->softc;
2149 dp = &softc->params;
2150 dp->secsize = block_len;
2151 dp->sectors = maxsector + 1;
2153 * Have the controller provide us with a geometry
2154 * for this disk. The only time the geometry
2155 * matters is when we boot and the controller
2156 * is the only one knowledgeable enough to come
2157 * up with something that will make this a bootable
2158 * device.
2160 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
2161 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
2162 ccg.block_size = dp->secsize;
2163 ccg.volume_size = dp->sectors;
2164 ccg.heads = 0;
2165 ccg.secs_per_track = 0;
2166 ccg.cylinders = 0;
2167 xpt_action((union ccb*)&ccg);
2168 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2170 * We don't know what went wrong here- but just pick
2171 * a geometry so we don't have nasty things like divide
2172 * by zero.
2174 dp->heads = 255;
2175 dp->secs_per_track = 255;
2176 dp->cylinders = dp->sectors / (255 * 255);
2177 if (dp->cylinders == 0) {
2178 dp->cylinders = 1;
2180 } else {
2181 dp->heads = ccg.heads;
2182 dp->secs_per_track = ccg.secs_per_track;
2183 dp->cylinders = ccg.cylinders;
2187 static void
2188 dasendorderedtag(void *arg)
2190 struct da_softc *softc = arg;
2192 if (da_send_ordered) {
2193 if ((softc->ordered_tag_count == 0)
2194 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
2195 softc->flags |= DA_FLAG_NEED_OTAG;
2197 if (softc->outstanding_cmds > 0)
2198 softc->flags &= ~DA_FLAG_WENT_IDLE;
2200 softc->ordered_tag_count = 0;
2202 /* Queue us up again */
2203 callout_reset(&softc->sendordered_c,
2204 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
2205 dasendorderedtag, softc);
2209 * Step through all DA peripheral drivers, and if the device is still open,
2210 * sync the disk cache to physical media.
2212 static void
2213 dashutdown(void * arg, int howto)
2215 struct cam_periph *periph;
2216 struct da_softc *softc;
2218 TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
2219 union ccb ccb;
2221 cam_periph_lock(periph);
2222 softc = (struct da_softc *)periph->softc;
2225 * We only sync the cache if the drive is still open, and
2226 * if the drive is capable of it..
2228 if (((softc->flags & DA_FLAG_OPEN) == 0)
2229 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
2230 cam_periph_unlock(periph);
2231 continue;
2234 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
2236 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
2237 scsi_synchronize_cache(&ccb.csio,
2238 /*retries*/1,
2239 /*cbfcnp*/dadone,
2240 MSG_SIMPLE_Q_TAG,
2241 /*begin_lba*/0, /* whole disk */
2242 /*lb_count*/0,
2243 SSD_FULL_SIZE,
2244 60 * 60 * 1000);
2246 xpt_polled_action(&ccb);
2248 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2249 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2250 CAM_SCSI_STATUS_ERROR)
2251 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
2252 int error_code, sense_key, asc, ascq;
2254 scsi_extract_sense(&ccb.csio.sense_data,
2255 &error_code, &sense_key,
2256 &asc, &ascq);
2258 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
2259 scsi_sense_print(&ccb.csio);
2260 } else {
2261 xpt_print(periph->path, "Synchronize "
2262 "cache failed, status == 0x%x, scsi status "
2263 "== 0x%x\n", ccb.ccb_h.status,
2264 ccb.csio.scsi_status);
2268 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
2269 cam_release_devq(ccb.ccb_h.path,
2270 /*relsim_flags*/0,
2271 /*reduction*/0,
2272 /*timeout*/0,
2273 /*getcount_only*/0);
2275 cam_periph_unlock(periph);
2279 #else /* !_KERNEL */
2282 * XXX This is only left out of the kernel build to silence warnings. If,
2283 * for some reason this function is used in the kernel, the ifdefs should
2284 * be moved so it is included both in the kernel and userland.
2286 void
2287 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2288 void (*cbfcnp)(struct cam_periph *, union ccb *),
2289 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2290 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2291 u_int32_t timeout)
2293 struct scsi_format_unit *scsi_cmd;
2295 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2296 scsi_cmd->opcode = FORMAT_UNIT;
2297 scsi_cmd->byte2 = byte2;
2298 scsi_ulto2b(ileave, scsi_cmd->interleave);
2300 cam_fill_csio(csio,
2301 retries,
2302 cbfcnp,
2303 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2304 tag_action,
2305 data_ptr,
2306 dxfer_len,
2307 sense_len,
2308 sizeof(*scsi_cmd),
2309 timeout);
2312 #endif /* _KERNEL */