<sys/vmmeter.h> need not be included twice.
[dragonfly.git] / sys / bus / cam / scsi / scsi_da.c
CommitLineData
984263bc
MD
1/*
2 * Implementation of SCSI Direct Access Peripheral driver for CAM.
3 *
4 * Copyright (c) 1997 Justin T. Gibbs.
5 * All rights reserved.
6 *
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.
15 *
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.
27 *
62ade751 28 * $FreeBSD: src/sys/cam/scsi/scsi_da.c,v 1.42.2.46 2003/10/21 22:18:19 thomas Exp $
654cdffd 29 * $DragonFly: src/sys/bus/cam/scsi/scsi_da.c,v 1.22 2005/04/25 15:14:54 joerg Exp $
984263bc
MD
30 */
31
32#ifdef _KERNEL
33#include "opt_hw_wdog.h"
34#endif /* _KERNEL */
35
36#include <sys/param.h>
fabb8ceb 37#include <sys/bootmaj.h>
984263bc
MD
38
39#ifdef _KERNEL
40#include <sys/systm.h>
41#include <sys/kernel.h>
42#include <sys/buf.h>
43#include <sys/sysctl.h>
62ade751 44#include <sys/taskqueue.h>
984263bc
MD
45#endif /* _KERNEL */
46
47#include <sys/devicestat.h>
48#include <sys/conf.h>
d9f3f6fa 49#ifdef _KERNEL
984263bc
MD
50#include <sys/disk.h>
51#include <sys/eventhandler.h>
52#include <sys/malloc.h>
53#include <sys/cons.h>
3020e3be 54#include <sys/proc.h>
05220613 55#endif
3020e3be 56#include <sys/buf2.h>
984263bc
MD
57
58#include <machine/md_var.h>
59
05220613 60#ifdef _KERNEL
984263bc
MD
61#include <vm/vm.h>
62#include <vm/pmap.h>
05220613 63#endif
984263bc
MD
64
65#ifndef _KERNEL
66#include <stdio.h>
67#include <string.h>
68#endif /* _KERNEL */
69
1f2de5d4
MD
70#include "../cam.h"
71#include "../cam_ccb.h"
72#include "../cam_extend.h"
73#include "../cam_periph.h"
74#include "../cam_xpt_periph.h"
984263bc 75
1f2de5d4 76#include "scsi_message.h"
984263bc
MD
77
78#ifndef _KERNEL
1f2de5d4 79#include "scsi_da.h"
984263bc
MD
80#endif /* !_KERNEL */
81
82#ifdef _KERNEL
83typedef enum {
84 DA_STATE_PROBE,
85 DA_STATE_NORMAL
86} da_state;
87
88typedef enum {
89 DA_FLAG_PACK_INVALID = 0x001,
90 DA_FLAG_NEW_PACK = 0x002,
91 DA_FLAG_PACK_LOCKED = 0x004,
92 DA_FLAG_PACK_REMOVABLE = 0x008,
93 DA_FLAG_TAGGED_QUEUING = 0x010,
94 DA_FLAG_NEED_OTAG = 0x020,
95 DA_FLAG_WENT_IDLE = 0x040,
96 DA_FLAG_RETRY_UA = 0x080,
62ade751
MD
97 DA_FLAG_OPEN = 0x100,
98 DA_FLAG_SCTX_INIT = 0x200
984263bc
MD
99} da_flags;
100
101typedef enum {
102 DA_Q_NONE = 0x00,
103 DA_Q_NO_SYNC_CACHE = 0x01,
62ade751
MD
104 DA_Q_NO_6_BYTE = 0x02,
105 DA_Q_NO_PREVENT = 0x04
984263bc
MD
106} da_quirks;
107
108typedef enum {
109 DA_CCB_PROBE = 0x01,
110 DA_CCB_BUFFER_IO = 0x02,
111 DA_CCB_WAITING = 0x03,
112 DA_CCB_DUMP = 0x04,
113 DA_CCB_TYPE_MASK = 0x0F,
114 DA_CCB_RETRY_UA = 0x10
115} da_ccb_state;
116
117/* Offsets into our private area for storing information */
118#define ccb_state ppriv_field0
119#define ccb_bp ppriv_ptr1
120
121struct disk_params {
122 u_int8_t heads;
123 u_int16_t cylinders;
124 u_int8_t secs_per_track;
125 u_int32_t secsize; /* Number of bytes/sector */
126 u_int32_t sectors; /* total number sectors */
127};
128
129struct da_softc {
130 struct buf_queue_head buf_queue;
131 struct devstat device_stats;
132 SLIST_ENTRY(da_softc) links;
133 LIST_HEAD(, ccb_hdr) pending_ccbs;
134 da_state state;
135 da_flags flags;
136 da_quirks quirks;
137 int minimum_cmd_size;
138 int ordered_tag_count;
139 struct disk_params params;
140 struct disk disk;
141 union ccb saved_ccb;
62ade751
MD
142 struct task sysctl_task;
143 struct sysctl_ctx_list sysctl_ctx;
144 struct sysctl_oid *sysctl_tree;
984263bc
MD
145};
146
147struct da_quirk_entry {
148 struct scsi_inquiry_pattern inq_pat;
149 da_quirks quirks;
150};
151
152static const char quantum[] = "QUANTUM";
153static const char microp[] = "MICROP";
154
155static struct da_quirk_entry da_quirk_table[] =
156{
62ade751 157 /* SPI, FC devices */
984263bc
MD
158 {
159 /*
160 * Fujitsu M2513A MO drives.
161 * Tested devices: M2513A2 firmware versions 1200 & 1300.
162 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
163 * Reported by: W.Scholten <whs@xs4all.nl>
164 */
165 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
166 /*quirks*/ DA_Q_NO_SYNC_CACHE
167 },
168 {
169 /* See above. */
170 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
171 /*quirks*/ DA_Q_NO_SYNC_CACHE
172 },
173 {
174 /*
175 * This particular Fujitsu drive doesn't like the
176 * synchronize cache command.
177 * Reported by: Tom Jackson <toj@gorilla.net>
178 */
179 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
180 /*quirks*/ DA_Q_NO_SYNC_CACHE
181
182 },
183 {
184 /*
185 * This drive doesn't like the synchronize cache command
186 * either. Reported by: Matthew Jacob <mjacob@feral.com>
187 * in NetBSD PR kern/6027, August 24, 1998.
188 */
189 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
190 /*quirks*/ DA_Q_NO_SYNC_CACHE
191 },
192 {
193 /*
194 * This drive doesn't like the synchronize cache command
195 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
196 * (PR 8882).
197 */
198 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
199 /*quirks*/ DA_Q_NO_SYNC_CACHE
200 },
201 {
202 /*
203 * Doesn't like the synchronize cache command.
204 * Reported by: Blaz Zupan <blaz@gold.amis.net>
205 */
206 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
207 /*quirks*/ DA_Q_NO_SYNC_CACHE
208 },
209 {
210 /*
211 * Doesn't like the synchronize cache command.
212 */
213 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
214 /*quirks*/ DA_Q_NO_SYNC_CACHE
215 },
216 {
217 /*
218 * Doesn't like the synchronize cache command.
219 */
220 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
221 /*quirks*/ DA_Q_NO_SYNC_CACHE
222 },
223 {
224 /*
225 * Doesn't work correctly with 6 byte reads/writes.
226 * Returns illegal request, and points to byte 9 of the
227 * 6-byte CDB.
228 * Reported by: Adam McDougall <bsdx@spawnet.com>
229 */
230 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
231 /*quirks*/ DA_Q_NO_6_BYTE
232 },
233 {
62ade751 234 /* See above. */
984263bc
MD
235 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
236 /*quirks*/ DA_Q_NO_6_BYTE
237 },
62ade751
MD
238 /* XXX USB floppy quirks temporarily enabled for 4.9R */
239 /* USB floppy devices supported by umass(4) */
984263bc
MD
240 {
241 /*
242 * This USB floppy drive uses the UFI command set. This
243 * command set is a derivative of the ATAPI command set and
244 * does not support READ_6 commands only READ_10. It also does
245 * not support sync cache (0x35).
246 */
247 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Y-E DATA", "USB-FDU", "*"},
62ade751 248 /*quirks*/ DA_Q_NO_SYNC_CACHE
984263bc
MD
249 },
250 {
251 /* Another USB floppy */
252 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MATSHITA", "FDD CF-VFDU*","*"},
62ade751
MD
253 /*quirks*/ DA_Q_NO_SYNC_CACHE
254 },
255 {
256 /*
257 * The vendor, product and version strings coming from the
258 * controller are null terminated instead of being padded with
259 * spaces. The trailing wildcard character '*' is required.
260 */
261 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SMSC*", "USB FDC*","*"},
262 /*quirks*/ DA_Q_NO_SYNC_CACHE
263 },
264 {
265 /*
266 * SmartDisk (Mitsumi) USB floppy drive
267 * PR: kern/50226
268 */
269 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MITSUMI", "USB FDD", "*"},
270 /*quirks*/ DA_Q_NO_SYNC_CACHE
984263bc 271 },
654cdffd
JS
272 {
273 /*
274 * The CISS RAID controllers do not support SYNC_CACHE
275 */
276 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
277 /*quirks*/ DA_Q_NO_SYNC_CACHE
278 },
62ade751
MD
279#ifdef DA_OLD_QUIRKS
280 /* USB mass storage devices supported by umass(4) */
984263bc
MD
281 {
282 /*
283 * Sony Memory Stick adapter MSAC-US1 and
284 * Sony PCG-C1VJ Internal Memory Stick Slot (MSC-U01).
285 * Make all sony MS* products use this quirk.
286 */
287 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "MS*", "*"},
62ade751 288 /*quirks*/ DA_Q_NO_SYNC_CACHE
984263bc
MD
289 },
290 {
291 /*
292 * Sony Memory Stick adapter for the CLIE series
293 * of PalmOS PDA's
294 */
295 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "CLIE*", "*"},
62ade751 296 /*quirks*/ DA_Q_NO_SYNC_CACHE
984263bc
MD
297 },
298 {
299 /*
62ade751
MD
300 * Intelligent Stick USB disk-on-key
301 * PR: kern/53005
984263bc 302 */
62ade751
MD
303 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB Card",
304 "IntelligentStick*", "*"},
305 /*quirks*/ DA_Q_NO_SYNC_CACHE
984263bc
MD
306 },
307 {
308 /*
62ade751 309 * Sony DSC cameras (DSC-S30, DSC-S50, DSC-S70)
984263bc 310 */
62ade751
MD
311 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
312 /*quirks*/ DA_Q_NO_SYNC_CACHE
984263bc
MD
313 },
314 {
315 /*
316 * Microtech USB CameraMate
317 */
318 {T_DIRECT, SIP_MEDIA_REMOVABLE, "eUSB Compact*",
319 "Compact Flash*", "*"},
62ade751 320 /*quirks*/ DA_Q_NO_SYNC_CACHE
984263bc
MD
321 },
322 {
323 /*
324 * Olympus digital cameras (C-3040ZOOM, C-2040ZOOM, C-1)
325 */
326 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "C-*", "*"},
62ade751 327 /*quirks*/ DA_Q_NO_SYNC_CACHE
984263bc
MD
328 },
329 {
330 /*
331 * Olympus digital cameras (E-100RS, E-10).
332 */
333 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "E-*", "*"},
62ade751 334 /*quirks*/ DA_Q_NO_SYNC_CACHE
984263bc
MD
335 },
336 {
337 /*
338 * KingByte Pen Drives
339 */
340 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NO BRAND", "PEN DRIVE", "*"},
62ade751 341 /*quirks*/ DA_Q_NO_SYNC_CACHE
984263bc 342 },
34096067
DR
343 {
344 /*
345 * iRiver iFP MP3 player (with UMS Firmware)
346 * PR: kern/54881, i386/63941, kern/66124
347 */
348 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
349 /*quirks*/ DA_Q_NO_SYNC_CACHE
350 },
3100cda6
DR
351 {
352 /*
353 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
354 * PR: kern/70158
355 */
356 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "NexIA+*", "*"},
357 /*quirks*/ DA_Q_NO_SYNC_CACHE
358 },
984263bc
MD
359 {
360 /*
361 * FujiFilm Camera
362 */
363 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJIFILMUSB-DRIVEUNIT",
364 "USB-DRIVEUNIT", "*"},
62ade751 365 /*quirks*/ DA_Q_NO_SYNC_CACHE
984263bc 366 },
984263bc
MD
367 {
368 /*
369 * Minolta Dimage E203
370 */
371 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MINOLTA", "DiMAGE E203", "*"},
62ade751 372 /*quirks*/ DA_Q_NO_SYNC_CACHE
984263bc
MD
373 },
374 {
375 /*
376 * Apacer HandyDrive
377 * PR: kern/43627
378 */
379 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Apacer", "HandyDrive", "*"},
62ade751 380 /*quirks*/ DA_Q_NO_SYNC_CACHE
984263bc
MD
381 },
382 {
383 /*
384 * Daisy Technology PhotoClip on Zoran chip
385 * PR: kern/43580
386 */
387 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ZORAN", "COACH", "*"},
62ade751 388 /*quirks*/ DA_Q_NO_SYNC_CACHE
984263bc
MD
389 },
390 {
391 /*
392 * Sony USB Key-Storage
393 * PR: kern/46386
394 */
395 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Storage Media", "*"},
62ade751 396 /*quirks*/ DA_Q_NO_SYNC_CACHE
984263bc 397 },
62ade751 398#endif /* DA_OLD_QUIRKS */
984263bc
MD
399 {
400 /*
62ade751
MD
401 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
402 * PR: kern/51675
984263bc 403 */
62ade751
MD
404 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
405 /*quirks*/ DA_Q_NO_SYNC_CACHE
984263bc
MD
406 },
407 {
408 /*
62ade751
MD
409 * Jungsoft NEXDISK USB flash key
410 * PR: kern/54737
984263bc 411 */
62ade751
MD
412 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
413 /*quirks*/ DA_Q_NO_SYNC_CACHE
984263bc
MD
414 },
415 {
416 /*
62ade751
MD
417 * Creative Nomad MUVO mp3 player (USB)
418 * PR: kern/53094
984263bc 419 */
62ade751
MD
420 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
421 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
984263bc 422 },
984263bc
MD
423};
424
425static d_open_t daopen;
426static d_close_t daclose;
427static d_strategy_t dastrategy;
428static d_ioctl_t daioctl;
429static d_dump_t dadump;
430static periph_init_t dainit;
431static void daasync(void *callback_arg, u_int32_t code,
432 struct cam_path *path, void *arg);
62ade751 433static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
984263bc
MD
434static periph_ctor_t daregister;
435static periph_dtor_t dacleanup;
436static periph_start_t dastart;
437static periph_oninv_t daoninvalidate;
438static void dadone(struct cam_periph *periph,
439 union ccb *done_ccb);
440static int daerror(union ccb *ccb, u_int32_t cam_flags,
441 u_int32_t sense_flags);
442static void daprevent(struct cam_periph *periph, int action);
443static void dasetgeom(struct cam_periph *periph,
444 struct scsi_read_capacity_data * rdcap);
445static timeout_t dasendorderedtag;
446static void dashutdown(void *arg, int howto);
447
448#ifndef DA_DEFAULT_TIMEOUT
449#define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
450#endif
451
452#ifndef DA_DEFAULT_RETRY
453#define DA_DEFAULT_RETRY 4
454#endif
455
456static int da_retry_count = DA_DEFAULT_RETRY;
457static int da_default_timeout = DA_DEFAULT_TIMEOUT;
3690a379 458static struct callout dasendorderedtag_ch;
984263bc 459
984263bc
MD
460SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
461 "CAM Direct Access Disk driver");
462SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
463 &da_retry_count, 0, "Normal I/O retry count");
62ade751 464TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
984263bc
MD
465SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
466 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
62ade751 467TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
984263bc
MD
468
469/*
470 * DA_ORDEREDTAG_INTERVAL determines how often, relative
471 * to the default timeout, we check to see whether an ordered
472 * tagged transaction is appropriate to prevent simple tag
473 * starvation. Since we'd like to ensure that there is at least
474 * 1/2 of the timeout length left for a starved transaction to
475 * complete after we've sent an ordered tag, we must poll at least
476 * four times in every timeout period. This takes care of the worst
477 * case where a starved transaction starts during an interval that
478 * meets the requirement "don't send an ordered tag" test so it takes
479 * us two intervals to determine that a tag must be sent.
480 */
481#ifndef DA_ORDEREDTAG_INTERVAL
482#define DA_ORDEREDTAG_INTERVAL 4
483#endif
484
485static struct periph_driver dadriver =
486{
487 dainit, "da",
488 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
489};
490
491DATA_SET(periphdriver_set, dadriver);
492
984263bc
MD
493/* For 2.2-stable support */
494#ifndef D_DISK
495#define D_DISK 0
496#endif
497
498static struct cdevsw da_cdevsw = {
fabb8ceb
MD
499 /* name */ "da",
500 /* maj */ DA_CDEV_MAJOR,
501 /* flags */ D_DISK,
502 /* port */ NULL,
455fcd7e 503 /* clone */ NULL,
fabb8ceb 504
984263bc
MD
505 /* open */ daopen,
506 /* close */ daclose,
507 /* read */ physread,
508 /* write */ physwrite,
509 /* ioctl */ daioctl,
510 /* poll */ nopoll,
511 /* mmap */ nommap,
512 /* strategy */ dastrategy,
984263bc 513 /* dump */ dadump,
fabb8ceb 514 /* psize */ nopsize
984263bc
MD
515};
516
984263bc
MD
517static SLIST_HEAD(,da_softc) softc_list;
518static struct extend_array *daperiphs;
519
520static int
41c20dac 521daopen(dev_t dev, int flags, int fmt, struct thread *td)
984263bc
MD
522{
523 struct cam_periph *periph;
524 struct da_softc *softc;
525 struct disklabel *label;
526 int unit;
527 int part;
528 int error;
529 int s;
530
531 unit = dkunit(dev);
532 part = dkpart(dev);
533 periph = cam_extend_get(daperiphs, unit);
534 if (periph == NULL)
535 return (ENXIO);
536
537 softc = (struct da_softc *)periph->softc;
538
539 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
540 ("daopen: dev=%s (unit %d , partition %d)\n", devtoname(dev),
541 unit, part));
542
377d4740 543 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
984263bc
MD
544 return (error); /* error code from tsleep */
545 }
546
547 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
548 return(ENXIO);
549 softc->flags |= DA_FLAG_OPEN;
550
551 s = splsoftcam();
552 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
553 /* Invalidate our pack information. */
554 disk_invalidate(&softc->disk);
555 softc->flags &= ~DA_FLAG_PACK_INVALID;
556 }
557 splx(s);
558
559 /* Do a read capacity */
560 {
561 struct scsi_read_capacity_data *rcap;
562 union ccb *ccb;
563
898d961b 564 rcap = malloc(sizeof(*rcap), M_TEMP, M_INTWAIT | M_ZERO);
984263bc
MD
565
566 ccb = cam_periph_getccb(periph, /*priority*/1);
567 scsi_read_capacity(&ccb->csio,
568 /*retries*/1,
569 /*cbfncp*/dadone,
570 MSG_SIMPLE_Q_TAG,
571 rcap,
572 SSD_FULL_SIZE,
573 /*timeout*/60000);
574 ccb->ccb_h.ccb_bp = NULL;
575
576 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
577 /*sense_flags*/SF_RETRY_UA |
578 SF_RETRY_SELTO,
579 &softc->device_stats);
580
581 xpt_release_ccb(ccb);
582
583 if (error == 0) {
584 dasetgeom(periph, rcap);
585 }
586
587 free(rcap, M_TEMP);
588 }
589
590 if (error == 0) {
591 struct ccb_getdev cgd;
592
593 /* Build label for whole disk. */
594 label = &softc->disk.d_label;
595 bzero(label, sizeof(*label));
596 label->d_type = DTYPE_SCSI;
597
598 /*
599 * Grab the inquiry data to get the vendor and product names.
600 * Put them in the typename and packname for the label.
601 */
602 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
603 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
604 xpt_action((union ccb *)&cgd);
605
606 strncpy(label->d_typename, cgd.inq_data.vendor,
607 min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
608 strncpy(label->d_packname, cgd.inq_data.product,
609 min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
610
611 label->d_secsize = softc->params.secsize;
612 label->d_nsectors = softc->params.secs_per_track;
613 label->d_ntracks = softc->params.heads;
614 label->d_ncylinders = softc->params.cylinders;
615 label->d_secpercyl = softc->params.heads
616 * softc->params.secs_per_track;
617 label->d_secperunit = softc->params.sectors;
618
62ade751
MD
619 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
620 (softc->quirks & DA_Q_NO_PREVENT) == 0)
984263bc 621 daprevent(periph, PR_PREVENT);
984263bc
MD
622
623 /*
624 * Check to see whether or not the blocksize is set yet.
625 * If it isn't, set it and then clear the blocksize
626 * unavailable flag for the device statistics.
627 */
628 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0){
629 softc->device_stats.block_size = softc->params.secsize;
630 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
631 }
632 }
633
634 if (error != 0) {
62ade751
MD
635 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
636 (softc->quirks & DA_Q_NO_PREVENT) == 0)
984263bc 637 daprevent(periph, PR_ALLOW);
984263bc
MD
638 softc->flags &= ~DA_FLAG_OPEN;
639 cam_periph_release(periph);
640 }
641 cam_periph_unlock(periph);
642 return (error);
643}
644
645static int
41c20dac 646daclose(dev_t dev, int flag, int fmt, struct thread *td)
984263bc
MD
647{
648 struct cam_periph *periph;
649 struct da_softc *softc;
650 int unit;
651 int error;
652
653 unit = dkunit(dev);
654 periph = cam_extend_get(daperiphs, unit);
655 if (periph == NULL)
656 return (ENXIO);
657
658 softc = (struct da_softc *)periph->softc;
659
377d4740 660 if ((error = cam_periph_lock(periph, 0)) != 0) {
984263bc
MD
661 return (error); /* error code from tsleep */
662 }
663
664 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
665 union ccb *ccb;
666
667 ccb = cam_periph_getccb(periph, /*priority*/1);
668
669 scsi_synchronize_cache(&ccb->csio,
670 /*retries*/1,
671 /*cbfcnp*/dadone,
672 MSG_SIMPLE_Q_TAG,
673 /*begin_lba*/0,/* Cover the whole disk */
674 /*lb_count*/0,
675 SSD_FULL_SIZE,
676 5 * 60 * 1000);
677
678 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
679 /*sense_flags*/SF_RETRY_UA,
680 &softc->device_stats);
681
682 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
683 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
684 CAM_SCSI_STATUS_ERROR) {
685 int asc, ascq;
686 int sense_key, error_code;
687
688 scsi_extract_sense(&ccb->csio.sense_data,
689 &error_code,
690 &sense_key,
691 &asc, &ascq);
692 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
693 scsi_sense_print(&ccb->csio);
694 } else {
695 xpt_print_path(periph->path);
696 printf("Synchronize cache failed, status "
697 "== 0x%x, scsi status == 0x%x\n",
698 ccb->csio.ccb_h.status,
699 ccb->csio.scsi_status);
700 }
701 }
702
703 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
704 cam_release_devq(ccb->ccb_h.path,
705 /*relsim_flags*/0,
706 /*reduction*/0,
707 /*timeout*/0,
708 /*getcount_only*/0);
709
710 xpt_release_ccb(ccb);
711
712 }
713
714 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
62ade751
MD
715 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
716 daprevent(periph, PR_ALLOW);
984263bc
MD
717 /*
718 * If we've got removeable media, mark the blocksize as
719 * unavailable, since it could change when new media is
720 * inserted.
721 */
722 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
723 }
724
725 softc->flags &= ~DA_FLAG_OPEN;
726 cam_periph_unlock(periph);
727 cam_periph_release(periph);
728 return (0);
729}
730
731/*
732 * Actually translate the requested transfer into one the physical driver
733 * can understand. The transfer is described by a buf and will include
734 * only one physical transfer.
735 */
736static void
737dastrategy(struct buf *bp)
738{
739 struct cam_periph *periph;
740 struct da_softc *softc;
741 u_int unit;
742 u_int part;
743 int s;
744
745 unit = dkunit(bp->b_dev);
746 part = dkpart(bp->b_dev);
747 periph = cam_extend_get(daperiphs, unit);
748 if (periph == NULL) {
749 bp->b_error = ENXIO;
750 goto bad;
751 }
752 softc = (struct da_softc *)periph->softc;
753#if 0
754 /*
755 * check it's not too big a transfer for our adapter
756 */
757 scsi_minphys(bp,&sd_switch);
758#endif
759
760 /*
761 * Mask interrupts so that the pack cannot be invalidated until
762 * after we are in the queue. Otherwise, we might not properly
763 * clean up one of the buffers.
764 */
765 s = splbio();
766
767 /*
768 * If the device has been made invalid, error out
769 */
770 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
771 splx(s);
772 bp->b_error = ENXIO;
773 goto bad;
774 }
775
776 /*
777 * Place it in the queue of disk activities for this disk
778 */
779 bufqdisksort(&softc->buf_queue, bp);
780
781 splx(s);
782
783 /*
784 * Schedule ourselves for performing the work.
785 */
786 xpt_schedule(periph, /* XXX priority */1);
787
788 return;
789bad:
790 bp->b_flags |= B_ERROR;
791
792 /*
793 * Correctly set the buf to indicate a completed xfer
794 */
795 bp->b_resid = bp->b_bcount;
796 biodone(bp);
797 return;
798}
799
800/* For 2.2-stable support */
801#ifndef ENOIOCTL
802#define ENOIOCTL -1
803#endif
804
805static int
41c20dac 806daioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
984263bc
MD
807{
808 struct cam_periph *periph;
809 struct da_softc *softc;
810 int unit;
811 int error;
812
813 unit = dkunit(dev);
814 periph = cam_extend_get(daperiphs, unit);
815 if (periph == NULL)
816 return (ENXIO);
817
818 softc = (struct da_softc *)periph->softc;
819
820 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("daioctl\n"));
821
377d4740 822 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
984263bc
MD
823 return (error); /* error code from tsleep */
824 }
825
826 error = cam_periph_ioctl(periph, cmd, addr, daerror);
827
828 cam_periph_unlock(periph);
829
830 return (error);
831}
832
833static int
e4c9c0c8 834dadump(dev_t dev, u_int num, u_int blknum, u_int secsize)
984263bc
MD
835{
836 struct cam_periph *periph;
837 struct da_softc *softc;
838 u_int unit;
984263bc 839 long blkcnt;
62ade751 840 vm_paddr_t addr;
984263bc
MD
841 struct ccb_scsiio csio;
842 int dumppages = MAXDUMPPGS;
984263bc
MD
843 int i;
844
845 /* toss any characters present prior to dump */
846 while (cncheckc() != -1)
847 ;
848
849 unit = dkunit(dev);
984263bc
MD
850 periph = cam_extend_get(daperiphs, unit);
851 if (periph == NULL) {
852 return (ENXIO);
853 }
854 softc = (struct da_softc *)periph->softc;
855
856 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0)
857 return (ENXIO);
858
984263bc
MD
859 addr = 0; /* starting address */
860 blkcnt = howmany(PAGE_SIZE, secsize);
861
862 while (num > 0) {
863 caddr_t va = NULL;
864
865 if ((num / blkcnt) < dumppages)
866 dumppages = num / blkcnt;
867
868 for (i = 0; i < dumppages; ++i) {
62ade751 869 vm_paddr_t a = addr + (i * PAGE_SIZE);
984263bc
MD
870 if (is_physical_memory(a))
871 va = pmap_kenter_temporary(trunc_page(a), i);
872 else
873 va = pmap_kenter_temporary(trunc_page(0), i);
874 }
875
876 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
877 csio.ccb_h.ccb_state = DA_CCB_DUMP;
878 scsi_read_write(&csio,
879 /*retries*/1,
880 dadone,
881 MSG_ORDERED_Q_TAG,
882 /*read*/FALSE,
883 /*byte2*/0,
884 /*minimum_cmd_size*/ softc->minimum_cmd_size,
885 blknum,
886 blkcnt * dumppages,
887 /*data_ptr*/(u_int8_t *) va,
888 /*dxfer_len*/blkcnt * secsize * dumppages,
889 /*sense_len*/SSD_FULL_SIZE,
890 DA_DEFAULT_TIMEOUT * 1000);
891 xpt_polled_action((union ccb *)&csio);
892
893 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
894 printf("Aborting dump due to I/O error.\n");
895 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
896 CAM_SCSI_STATUS_ERROR)
897 scsi_sense_print(&csio);
898 else
899 printf("status == 0x%x, scsi status == 0x%x\n",
900 csio.ccb_h.status, csio.scsi_status);
901 return(EIO);
902 }
903
904 if (dumpstatus(addr, (off_t)num * softc->params.secsize) < 0)
905 return (EINTR);
906
907 /* update block count */
908 num -= blkcnt * dumppages;
909 blknum += blkcnt * dumppages;
910 addr += PAGE_SIZE * dumppages;
911 }
912
913 /*
914 * Sync the disk cache contents to the physical media.
915 */
916 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
917
918 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
919 csio.ccb_h.ccb_state = DA_CCB_DUMP;
920 scsi_synchronize_cache(&csio,
921 /*retries*/1,
922 /*cbfcnp*/dadone,
923 MSG_SIMPLE_Q_TAG,
924 /*begin_lba*/0,/* Cover the whole disk */
925 /*lb_count*/0,
926 SSD_FULL_SIZE,
927 5 * 60 * 1000);
928 xpt_polled_action((union ccb *)&csio);
929
930 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
931 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
932 CAM_SCSI_STATUS_ERROR) {
933 int asc, ascq;
934 int sense_key, error_code;
935
936 scsi_extract_sense(&csio.sense_data,
937 &error_code,
938 &sense_key,
939 &asc, &ascq);
940 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
941 scsi_sense_print(&csio);
942 } else {
943 xpt_print_path(periph->path);
944 printf("Synchronize cache failed, status "
945 "== 0x%x, scsi status == 0x%x\n",
946 csio.ccb_h.status, csio.scsi_status);
947 }
948 }
949 }
950 return (0);
951}
952
953static void
954dainit(void)
955{
956 cam_status status;
957 struct cam_path *path;
958
959 /*
960 * Create our extend array for storing the devices we attach to.
961 */
962 daperiphs = cam_extend_new();
963 SLIST_INIT(&softc_list);
964 if (daperiphs == NULL) {
965 printf("da: Failed to alloc extend array!\n");
966 return;
967 }
3690a379
JS
968
969 callout_init(&dasendorderedtag_ch);
970
984263bc
MD
971 /*
972 * Install a global async callback. This callback will
973 * receive async callbacks like "new device found".
974 */
975 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
976 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
977
978 if (status == CAM_REQ_CMP) {
979 struct ccb_setasync csa;
980
981 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
982 csa.ccb_h.func_code = XPT_SASYNC_CB;
983 csa.event_enable = AC_FOUND_DEVICE;
984 csa.callback = daasync;
985 csa.callback_arg = NULL;
986 xpt_action((union ccb *)&csa);
987 status = csa.ccb_h.status;
988 xpt_free_path(path);
989 }
990
991 if (status != CAM_REQ_CMP) {
992 printf("da: Failed to attach master async callback "
993 "due to status 0x%x!\n", status);
994 } else {
995
996 /*
997 * Schedule a periodic event to occasionally send an
998 * ordered tag to a device.
999 */
3690a379
JS
1000 callout_reset(&dasendorderedtag_ch,
1001 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
1002 dasendorderedtag, NULL);
984263bc
MD
1003
1004 /* Register our shutdown event handler */
1005 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1006 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1007 printf("dainit: shutdown event registration failed!\n");
1008 }
1009}
1010
1011static void
1012daoninvalidate(struct cam_periph *periph)
1013{
1014 int s;
1015 struct da_softc *softc;
1016 struct buf *q_bp;
1017 struct ccb_setasync csa;
1018
1019 softc = (struct da_softc *)periph->softc;
1020
1021 /*
1022 * De-register any async callbacks.
1023 */
1024 xpt_setup_ccb(&csa.ccb_h, periph->path,
1025 /* priority */ 5);
1026 csa.ccb_h.func_code = XPT_SASYNC_CB;
1027 csa.event_enable = 0;
1028 csa.callback = daasync;
1029 csa.callback_arg = periph;
1030 xpt_action((union ccb *)&csa);
1031
1032 softc->flags |= DA_FLAG_PACK_INVALID;
1033
1034 /*
1035 * Although the oninvalidate() routines are always called at
1036 * splsoftcam, we need to be at splbio() here to keep the buffer
1037 * queue from being modified while we traverse it.
1038 */
1039 s = splbio();
1040
1041 /*
1042 * Return all queued I/O with ENXIO.
1043 * XXX Handle any transactions queued to the card
1044 * with XPT_ABORT_CCB.
1045 */
1046 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
1047 bufq_remove(&softc->buf_queue, q_bp);
1048 q_bp->b_resid = q_bp->b_bcount;
1049 q_bp->b_error = ENXIO;
1050 q_bp->b_flags |= B_ERROR;
1051 biodone(q_bp);
1052 }
1053 splx(s);
1054
1055 SLIST_REMOVE(&softc_list, softc, da_softc, links);
1056
1057 xpt_print_path(periph->path);
1058 printf("lost device\n");
1059}
1060
1061static void
1062dacleanup(struct cam_periph *periph)
1063{
1064 struct da_softc *softc;
1065
1066 softc = (struct da_softc *)periph->softc;
1067
1068 devstat_remove_entry(&softc->device_stats);
1069 cam_extend_release(daperiphs, periph->unit_number);
1070 xpt_print_path(periph->path);
1071 printf("removing device entry\n");
62ade751
MD
1072 /*
1073 * If we can't free the sysctl tree, oh well...
1074 */
1075 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1076 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1077 xpt_print_path(periph->path);
1078 printf("can't remove sysctl context\n");
1079 }
e4c9c0c8 1080 if (softc->disk.d_rawdev) {
335dda38 1081 disk_destroy(&softc->disk);
984263bc
MD
1082 }
1083 free(softc, M_DEVBUF);
1084}
1085
1086static void
1087daasync(void *callback_arg, u_int32_t code,
1088 struct cam_path *path, void *arg)
1089{
1090 struct cam_periph *periph;
1091
1092 periph = (struct cam_periph *)callback_arg;
1093 switch (code) {
1094 case AC_FOUND_DEVICE:
1095 {
1096 struct ccb_getdev *cgd;
1097 cam_status status;
1098
1099 cgd = (struct ccb_getdev *)arg;
1100
1101 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1102 && SID_TYPE(&cgd->inq_data) != T_RBC
1103 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1104 break;
1105
1106 /*
1107 * Allocate a peripheral instance for
1108 * this device and start the probe
1109 * process.
1110 */
1111 status = cam_periph_alloc(daregister, daoninvalidate,
1112 dacleanup, dastart,
1113 "da", CAM_PERIPH_BIO,
1114 cgd->ccb_h.path, daasync,
1115 AC_FOUND_DEVICE, cgd);
1116
1117 if (status != CAM_REQ_CMP
1118 && status != CAM_REQ_INPROG)
1119 printf("daasync: Unable to attach to new device "
1120 "due to status 0x%x\n", status);
1121 break;
1122 }
1123 case AC_SENT_BDR:
1124 case AC_BUS_RESET:
1125 {
1126 struct da_softc *softc;
1127 struct ccb_hdr *ccbh;
1128 int s;
1129
1130 softc = (struct da_softc *)periph->softc;
1131 s = splsoftcam();
1132 /*
1133 * Don't fail on the expected unit attention
1134 * that will occur.
1135 */
1136 softc->flags |= DA_FLAG_RETRY_UA;
1137 for (ccbh = LIST_FIRST(&softc->pending_ccbs);
1138 ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le))
1139 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1140 splx(s);
1141 /* FALLTHROUGH*/
1142 }
1143 default:
1144 cam_periph_async(periph, code, path, arg);
1145 break;
1146 }
1147}
1148
62ade751
MD
1149static void
1150dasysctlinit(void *context, int pending)
1151{
1152 struct cam_periph *periph;
1153 struct da_softc *softc;
1154 char tmpstr[80], tmpstr2[80];
1155
1156 periph = (struct cam_periph *)context;
1157 softc = (struct da_softc *)periph->softc;
1158
1159 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1160 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1161
1162 sysctl_ctx_init(&softc->sysctl_ctx);
1163 softc->flags |= DA_FLAG_SCTX_INIT;
1164 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1165 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1166 CTLFLAG_RD, 0, tmpstr);
1167 if (softc->sysctl_tree == NULL) {
1168 printf("dasysctlinit: unable to allocate sysctl tree\n");
1169 return;
1170 }
1171
1172 /*
1173 * Now register the sysctl handler, so the user can the value on
1174 * the fly.
1175 */
1176 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1177 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1178 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1179 "Minimum CDB size");
1180}
1181
1182static int
1183dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1184{
1185 int error, value;
1186
1187 value = *(int *)arg1;
1188
1189 error = sysctl_handle_int(oidp, &value, 0, req);
1190
1191 if ((error != 0)
1192 || (req->newptr == NULL))
1193 return (error);
1194
1195 /*
1196 * Acceptable values here are 6, 10 or 12. It's possible we may
1197 * support a 16 byte minimum command size in the future, since
1198 * there are now READ(16) and WRITE(16) commands defined in the
1199 * SBC-2 spec.
1200 */
1201 if (value < 6)
1202 value = 6;
1203 else if ((value > 6)
1204 && (value <= 10))
1205 value = 10;
1206 else if (value > 10)
1207 value = 12;
1208
1209 *(int *)arg1 = value;
1210
1211 return (0);
1212}
1213
984263bc
MD
1214static cam_status
1215daregister(struct cam_periph *periph, void *arg)
1216{
1217 int s;
1218 struct da_softc *softc;
1219 struct ccb_setasync csa;
62ade751 1220 struct ccb_pathinq cpi;
984263bc 1221 struct ccb_getdev *cgd;
62ade751 1222 char tmpstr[80];
984263bc
MD
1223 caddr_t match;
1224
1225 cgd = (struct ccb_getdev *)arg;
1226 if (periph == NULL) {
1227 printf("daregister: periph was NULL!!\n");
1228 return(CAM_REQ_CMP_ERR);
1229 }
1230
1231 if (cgd == NULL) {
1232 printf("daregister: no getdev CCB, can't register device\n");
1233 return(CAM_REQ_CMP_ERR);
1234 }
1235
ad86f6b6 1236 softc = malloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
984263bc
MD
1237 LIST_INIT(&softc->pending_ccbs);
1238 softc->state = DA_STATE_PROBE;
1239 bufq_init(&softc->buf_queue);
1240 if (SID_IS_REMOVABLE(&cgd->inq_data))
1241 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1242 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1243 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1244
1245 periph->softc = softc;
1246
1247 cam_extend_set(daperiphs, periph->unit_number, periph);
1248
1249 /*
1250 * See if this device has any quirks.
1251 */
1252 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1253 (caddr_t)da_quirk_table,
1254 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1255 sizeof(*da_quirk_table), scsi_inquiry_match);
1256
1257 if (match != NULL)
1258 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1259 else
1260 softc->quirks = DA_Q_NONE;
1261
62ade751
MD
1262 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1263
1264 /* Check if the SIM does not want 6 byte commands */
1265 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
1266 cpi.ccb_h.func_code = XPT_PATH_INQ;
1267 xpt_action((union ccb *)&cpi);
1268 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1269 softc->quirks |= DA_Q_NO_6_BYTE;
1270
1271 /*
1272 * RBC devices don't have to support READ(6), only READ(10).
1273 */
984263bc
MD
1274 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1275 softc->minimum_cmd_size = 10;
1276 else
1277 softc->minimum_cmd_size = 6;
1278
62ade751
MD
1279 /*
1280 * Load the user's default, if any.
1281 */
1282 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1283 periph->unit_number);
1284 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1285
1286 /*
1287 * 6, 10 and 12 are the currently permissible values.
1288 */
1289 if (softc->minimum_cmd_size < 6)
1290 softc->minimum_cmd_size = 6;
1291 else if ((softc->minimum_cmd_size > 6)
1292 && (softc->minimum_cmd_size <= 10))
1293 softc->minimum_cmd_size = 10;
1294 else if (softc->minimum_cmd_size > 12)
1295 softc->minimum_cmd_size = 12;
1296
984263bc
MD
1297 /*
1298 * Block our timeout handler while we
1299 * add this softc to the dev list.
1300 */
1301 s = splsoftclock();
1302 SLIST_INSERT_HEAD(&softc_list, softc, links);
1303 splx(s);
1304
1305 /*
1306 * The DA driver supports a blocksize, but
1307 * we don't know the blocksize until we do
1308 * a read capacity. So, set a flag to
1309 * indicate that the blocksize is
1310 * unavailable right now. We'll clear the
1311 * flag as soon as we've done a read capacity.
1312 */
1313 devstat_add_entry(&softc->device_stats, "da",
1314 periph->unit_number, 0,
1315 DEVSTAT_BS_UNAVAILABLE,
1316 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
1317 DEVSTAT_PRIORITY_DISK);
1318
1319 /*
1320 * Register this media as a disk
1321 */
335dda38 1322 disk_create(periph->unit_number, &softc->disk, 0, &da_cdevsw);
984263bc
MD
1323
1324 /*
1325 * Add async callbacks for bus reset and
1326 * bus device reset calls. I don't bother
1327 * checking if this fails as, in most cases,
1328 * the system will function just fine without
1329 * them and the only alternative would be to
1330 * not attach the device on failure.
1331 */
1332 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
1333 csa.ccb_h.func_code = XPT_SASYNC_CB;
1334 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
1335 csa.callback = daasync;
1336 csa.callback_arg = periph;
1337 xpt_action((union ccb *)&csa);
1338 /*
1339 * Lock this peripheral until we are setup.
1340 * This first call can't block
1341 */
377d4740 1342 (void)cam_periph_lock(periph, 0);
984263bc
MD
1343 xpt_schedule(periph, /*priority*/5);
1344
1345 return(CAM_REQ_CMP);
1346}
1347
1348static void
1349dastart(struct cam_periph *periph, union ccb *start_ccb)
1350{
1351 struct da_softc *softc;
1352
1353 softc = (struct da_softc *)periph->softc;
1354
1355
1356 switch (softc->state) {
1357 case DA_STATE_NORMAL:
1358 {
1359 /* Pull a buffer from the queue and get going on it */
1360 struct buf *bp;
1361 int s;
1362
1363 /*
1364 * See if there is a buf with work for us to do..
1365 */
1366 s = splbio();
1367 bp = bufq_first(&softc->buf_queue);
1368 if (periph->immediate_priority <= periph->pinfo.priority) {
1369 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1370 ("queuing for immediate ccb\n"));
1371 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1372 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1373 periph_links.sle);
1374 periph->immediate_priority = CAM_PRIORITY_NONE;
1375 splx(s);
1376 wakeup(&periph->ccb_list);
1377 } else if (bp == NULL) {
1378 splx(s);
1379 xpt_release_ccb(start_ccb);
1380 } else {
1381 int oldspl;
1382 u_int8_t tag_code;
1383
1384 bufq_remove(&softc->buf_queue, bp);
1385
1386 devstat_start_transaction(&softc->device_stats);
1387
1388 if ((bp->b_flags & B_ORDERED) != 0
1389 || (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1390 softc->flags &= ~DA_FLAG_NEED_OTAG;
1391 softc->ordered_tag_count++;
1392 tag_code = MSG_ORDERED_Q_TAG;
1393 } else {
1394 tag_code = MSG_SIMPLE_Q_TAG;
1395 }
984263bc
MD
1396 scsi_read_write(&start_ccb->csio,
1397 /*retries*/da_retry_count,
1398 dadone,
1399 tag_code,
1400 bp->b_flags & B_READ,
1401 /*byte2*/0,
1402 softc->minimum_cmd_size,
1403 bp->b_pblkno,
1404 bp->b_bcount / softc->params.secsize,
1405 bp->b_data,
1406 bp->b_bcount,
1407 /*sense_len*/SSD_FULL_SIZE,
1408 da_default_timeout * 1000);
1409 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1410
1411 /*
1412 * Block out any asyncronous callbacks
1413 * while we touch the pending ccb list.
1414 */
1415 oldspl = splcam();
1416 LIST_INSERT_HEAD(&softc->pending_ccbs,
1417 &start_ccb->ccb_h, periph_links.le);
1418 splx(oldspl);
1419
1420 /* We expect a unit attention from this device */
1421 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1422 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1423 softc->flags &= ~DA_FLAG_RETRY_UA;
1424 }
1425
1426 start_ccb->ccb_h.ccb_bp = bp;
1427 bp = bufq_first(&softc->buf_queue);
1428 splx(s);
1429
1430 xpt_action(start_ccb);
1431 }
1432
1433 if (bp != NULL) {
1434 /* Have more work to do, so ensure we stay scheduled */
1435 xpt_schedule(periph, /* XXX priority */1);
1436 }
1437 break;
1438 }
1439 case DA_STATE_PROBE:
1440 {
1441 struct ccb_scsiio *csio;
1442 struct scsi_read_capacity_data *rcap;
1443
898d961b 1444 rcap = malloc(sizeof(*rcap), M_TEMP, M_INTWAIT | M_ZERO);
984263bc
MD
1445 csio = &start_ccb->csio;
1446 scsi_read_capacity(csio,
1447 /*retries*/4,
1448 dadone,
1449 MSG_SIMPLE_Q_TAG,
1450 rcap,
1451 SSD_FULL_SIZE,
1452 /*timeout*/5000);
1453 start_ccb->ccb_h.ccb_bp = NULL;
1454 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1455 xpt_action(start_ccb);
1456 break;
1457 }
1458 }
1459}
1460
1461static int
1462cmd6workaround(union ccb *ccb)
1463{
1464 struct scsi_rw_6 cmd6;
1465 struct scsi_rw_10 *cmd10;
1466 struct da_softc *softc;
1467 u_int8_t *cdb;
1468 int frozen;
1469
1470 cdb = ccb->csio.cdb_io.cdb_bytes;
1471
1472 /* Translation only possible if CDB is an array and cmd is R/W6 */
1473 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1474 (*cdb != READ_6 && *cdb != WRITE_6))
1475 return 0;
1476
1477 xpt_print_path(ccb->ccb_h.path);
1478 printf("READ(6)/WRITE(6) not supported, "
1479 "increasing minimum_cmd_size to 10.\n");
1480 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1481 softc->minimum_cmd_size = 10;
1482
1483 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1484 cmd10 = (struct scsi_rw_10 *)cdb;
1485 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1486 cmd10->byte2 = 0;
1487 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1488 cmd10->reserved = 0;
1489 scsi_ulto2b(cmd6.length, cmd10->length);
1490 cmd10->control = cmd6.control;
1491 ccb->csio.cdb_len = sizeof(*cmd10);
1492
1493 /* Requeue request, unfreezing queue if necessary */
1494 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1495 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1496 xpt_action(ccb);
1497 if (frozen) {
1498 cam_release_devq(ccb->ccb_h.path,
1499 /*relsim_flags*/0,
1500 /*reduction*/0,
1501 /*timeout*/0,
1502 /*getcount_only*/0);
1503 }
1504 return (ERESTART);
1505}
1506
1507static void
1508dadone(struct cam_periph *periph, union ccb *done_ccb)
1509{
1510 struct da_softc *softc;
1511 struct ccb_scsiio *csio;
1512
1513 softc = (struct da_softc *)periph->softc;
1514 csio = &done_ccb->csio;
1515 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1516 case DA_CCB_BUFFER_IO:
1517 {
1518 struct buf *bp;
1519 int oldspl;
1520
1521 bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
1522 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1523 int error;
1524 int s;
1525 int sf;
1526
1527 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1528 sf = SF_RETRY_UA;
1529 else
1530 sf = 0;
1531
1532 /* Retry selection timeouts */
1533 sf |= SF_RETRY_SELTO;
1534
1535 if ((error = daerror(done_ccb, 0, sf)) == ERESTART) {
1536 /*
1537 * A retry was scheuled, so
1538 * just return.
1539 */
1540 return;
1541 }
1542 if (error != 0) {
1543 struct buf *q_bp;
1544
1545 s = splbio();
1546
1547 if (error == ENXIO) {
1548 /*
1549 * Catastrophic error. Mark our pack as
1550 * invalid.
1551 */
1552 /* XXX See if this is really a media
1553 * change first.
1554 */
1555 xpt_print_path(periph->path);
1556 printf("Invalidating pack\n");
1557 softc->flags |= DA_FLAG_PACK_INVALID;
1558 }
1559
1560 /*
1561 * return all queued I/O with EIO, so that
1562 * the client can retry these I/Os in the
1563 * proper order should it attempt to recover.
1564 */
1565 while ((q_bp = bufq_first(&softc->buf_queue))
1566 != NULL) {
1567 bufq_remove(&softc->buf_queue, q_bp);
1568 q_bp->b_resid = q_bp->b_bcount;
1569 q_bp->b_error = EIO;
1570 q_bp->b_flags |= B_ERROR;
1571 biodone(q_bp);
1572 }
1573 splx(s);
1574 bp->b_error = error;
1575 bp->b_resid = bp->b_bcount;
1576 bp->b_flags |= B_ERROR;
1577 } else {
1578 bp->b_resid = csio->resid;
1579 bp->b_error = 0;
62ade751 1580 if (bp->b_resid != 0)
984263bc 1581 bp->b_flags |= B_ERROR;
984263bc
MD
1582 }
1583 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1584 cam_release_devq(done_ccb->ccb_h.path,
1585 /*relsim_flags*/0,
1586 /*reduction*/0,
1587 /*timeout*/0,
1588 /*getcount_only*/0);
1589 } else {
1590 bp->b_resid = csio->resid;
62ade751 1591 if (csio->resid > 0)
984263bc 1592 bp->b_flags |= B_ERROR;
984263bc
MD
1593 }
1594
1595 /*
1596 * Block out any asyncronous callbacks
1597 * while we touch the pending ccb list.
1598 */
1599 oldspl = splcam();
1600 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1601 splx(oldspl);
1602
1603 if (softc->device_stats.busy_count == 0)
1604 softc->flags |= DA_FLAG_WENT_IDLE;
1605
1606 devstat_end_transaction_buf(&softc->device_stats, bp);
1607 biodone(bp);
1608 break;
1609 }
1610 case DA_CCB_PROBE:
1611 {
1612 struct scsi_read_capacity_data *rdcap;
1613 char announce_buf[80];
1614
1615 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
1616
1617 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1618 struct disk_params *dp;
1619
1620 dasetgeom(periph, rdcap);
1621 dp = &softc->params;
1622 snprintf(announce_buf, sizeof(announce_buf),
1623 "%luMB (%u %u byte sectors: %dH %dS/T %dC)",
1624 (unsigned long) (((u_int64_t)dp->secsize *
1625 dp->sectors) / (1024*1024)), dp->sectors,
1626 dp->secsize, dp->heads, dp->secs_per_track,
1627 dp->cylinders);
1628 } else {
1629 int error;
1630
1631 announce_buf[0] = '\0';
1632
1633 /*
1634 * Retry any UNIT ATTENTION type errors. They
1635 * are expected at boot.
1636 */
1637 error = daerror(done_ccb, 0, SF_RETRY_UA |
1638 SF_RETRY_SELTO | SF_NO_PRINT);
1639 if (error == ERESTART) {
1640 /*
1641 * A retry was scheuled, so
1642 * just return.
1643 */
1644 return;
1645 } else if (error != 0) {
1646 struct scsi_sense_data *sense;
1647 int asc, ascq;
1648 int sense_key, error_code;
1649 int have_sense;
1650 cam_status status;
1651 struct ccb_getdev cgd;
1652
1653 /* Don't wedge this device's queue */
1654 cam_release_devq(done_ccb->ccb_h.path,
1655 /*relsim_flags*/0,
1656 /*reduction*/0,
1657 /*timeout*/0,
1658 /*getcount_only*/0);
1659
1660 status = done_ccb->ccb_h.status;
1661
1662 xpt_setup_ccb(&cgd.ccb_h,
1663 done_ccb->ccb_h.path,
1664 /* priority */ 1);
1665 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1666 xpt_action((union ccb *)&cgd);
1667
1668 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1669 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1670 || ((status & CAM_AUTOSNS_VALID) == 0))
1671 have_sense = FALSE;
1672 else
1673 have_sense = TRUE;
1674
1675 if (have_sense) {
1676 sense = &csio->sense_data;
1677 scsi_extract_sense(sense, &error_code,
1678 &sense_key,
1679 &asc, &ascq);
1680 }
1681 /*
1682 * Attach to anything that claims to be a
1683 * direct access or optical disk device,
1684 * as long as it doesn't return a "Logical
1685 * unit not supported" (0x25) error.
1686 */
1687 if ((have_sense) && (asc != 0x25)
1688 && (error_code == SSD_CURRENT_ERROR))
1689 snprintf(announce_buf,
1690 sizeof(announce_buf),
1691 "Attempt to query device "
1692 "size failed: %s, %s",
1693 scsi_sense_key_text[sense_key],
1694 scsi_sense_desc(asc,ascq,
1695 &cgd.inq_data));
1696 else {
1697 if (have_sense)
1698 scsi_sense_print(
1699 &done_ccb->csio);
1700 else {
1701 xpt_print_path(periph->path);
1702 printf("got CAM status %#x\n",
1703 done_ccb->ccb_h.status);
1704 }
1705
1706 xpt_print_path(periph->path);
1707 printf("fatal error, failed"
1708 " to attach to device\n");
1709
1710 /*
1711 * Free up resources.
1712 */
1713 cam_periph_invalidate(periph);
1714 }
1715 }
1716 }
1717 free(rdcap, M_TEMP);
62ade751 1718 if (announce_buf[0] != '\0') {
984263bc 1719 xpt_announce_periph(periph, announce_buf);
62ade751
MD
1720 /*
1721 * Create our sysctl variables, now that we know
1722 * we have successfully attached.
1723 */
1724 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1725 }
984263bc
MD
1726 softc->state = DA_STATE_NORMAL;
1727 /*
1728 * Since our peripheral may be invalidated by an error
1729 * above or an external event, we must release our CCB
1730 * before releasing the probe lock on the peripheral.
1731 * The peripheral will only go away once the last lock
1732 * is removed, and we need it around for the CCB release
1733 * operation.
1734 */
1735 xpt_release_ccb(done_ccb);
1736 cam_periph_unlock(periph);
1737 return;
1738 }
1739 case DA_CCB_WAITING:
1740 {
1741 /* Caller will release the CCB */
1742 wakeup(&done_ccb->ccb_h.cbfcnp);
1743 return;
1744 }
1745 case DA_CCB_DUMP:
1746 /* No-op. We're polling */
1747 return;
1748 default:
1749 break;
1750 }
1751 xpt_release_ccb(done_ccb);
1752}
1753
1754static int
1755daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1756{
1757 struct da_softc *softc;
1758 struct cam_periph *periph;
62ade751 1759 int error;
984263bc
MD
1760
1761 periph = xpt_path_periph(ccb->ccb_h.path);
1762 softc = (struct da_softc *)periph->softc;
1763
1764 /*
1765 * Automatically detect devices that do not support
1766 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
1767 */
1768 error = 0;
62ade751
MD
1769 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
1770 error = cmd6workaround(ccb);
1771 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
1772 CAM_SCSI_STATUS_ERROR)
1773 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
1774 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1775 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
1776 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
1777 int sense_key, error_code, asc, ascq;
1778
984263bc
MD
1779 scsi_extract_sense(&ccb->csio.sense_data,
1780 &error_code, &sense_key, &asc, &ascq);
1781 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
1782 error = cmd6workaround(ccb);
1783 }
1784 if (error == ERESTART)
1785 return (ERESTART);
1786
1787 /*
1788 * XXX
1789 * Until we have a better way of doing pack validation,
1790 * don't treat UAs as errors.
1791 */
1792 sense_flags |= SF_RETRY_UA;
1793 return(cam_periph_error(ccb, cam_flags, sense_flags,
1794 &softc->saved_ccb));
1795}
1796
1797static void
1798daprevent(struct cam_periph *periph, int action)
1799{
1800 struct da_softc *softc;
1801 union ccb *ccb;
1802 int error;
1803
1804 softc = (struct da_softc *)periph->softc;
1805
1806 if (((action == PR_ALLOW)
1807 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
1808 || ((action == PR_PREVENT)
1809 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
1810 return;
1811 }
1812
1813 ccb = cam_periph_getccb(periph, /*priority*/1);
1814
1815 scsi_prevent(&ccb->csio,
1816 /*retries*/1,
1817 /*cbcfp*/dadone,
1818 MSG_SIMPLE_Q_TAG,
1819 action,
1820 SSD_FULL_SIZE,
1821 5000);
1822
1823 error = cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
1824 /*sense_flags*/0, &softc->device_stats);
1825
1826 if (error == 0) {
1827 if (action == PR_ALLOW)
1828 softc->flags &= ~DA_FLAG_PACK_LOCKED;
1829 else
1830 softc->flags |= DA_FLAG_PACK_LOCKED;
1831 }
1832
1833 xpt_release_ccb(ccb);
1834}
1835
1836static void
1837dasetgeom(struct cam_periph *periph, struct scsi_read_capacity_data * rdcap)
1838{
1839 struct ccb_calc_geometry ccg;
1840 struct da_softc *softc;
1841 struct disk_params *dp;
1842
1843 softc = (struct da_softc *)periph->softc;
1844
1845 dp = &softc->params;
1846 dp->secsize = scsi_4btoul(rdcap->length);
1847 dp->sectors = scsi_4btoul(rdcap->addr) + 1;
1848 /*
1849 * Have the controller provide us with a geometry
1850 * for this disk. The only time the geometry
1851 * matters is when we boot and the controller
1852 * is the only one knowledgeable enough to come
1853 * up with something that will make this a bootable
1854 * device.
1855 */
1856 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
1857 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
1858 ccg.block_size = dp->secsize;
1859 ccg.volume_size = dp->sectors;
1860 ccg.heads = 0;
1861 ccg.secs_per_track = 0;
1862 ccg.cylinders = 0;
1863 xpt_action((union ccb*)&ccg);
1864 dp->heads = ccg.heads;
1865 dp->secs_per_track = ccg.secs_per_track;
1866 dp->cylinders = ccg.cylinders;
1867}
1868
1869static void
1870dasendorderedtag(void *arg)
1871{
1872 struct da_softc *softc;
1873 int s;
1874
1875 for (softc = SLIST_FIRST(&softc_list);
1876 softc != NULL;
1877 softc = SLIST_NEXT(softc, links)) {
1878 s = splsoftcam();
1879 if ((softc->ordered_tag_count == 0)
1880 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
1881 softc->flags |= DA_FLAG_NEED_OTAG;
1882 }
1883 if (softc->device_stats.busy_count > 0)
1884 softc->flags &= ~DA_FLAG_WENT_IDLE;
1885
1886 softc->ordered_tag_count = 0;
1887 splx(s);
1888 }
1889 /* Queue us up again */
3690a379
JS
1890 callout_reset(&dasendorderedtag_ch,
1891 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
1892 dasendorderedtag, NULL);
984263bc
MD
1893}
1894
1895/*
1896 * Step through all DA peripheral drivers, and if the device is still open,
1897 * sync the disk cache to physical media.
1898 */
1899static void
1900dashutdown(void * arg, int howto)
1901{
1902 struct cam_periph *periph;
1903 struct da_softc *softc;
1904
1905 for (periph = TAILQ_FIRST(&dadriver.units); periph != NULL;
1906 periph = TAILQ_NEXT(periph, unit_links)) {
1907 union ccb ccb;
1908 softc = (struct da_softc *)periph->softc;
1909
1910 /*
1911 * We only sync the cache if the drive is still open, and
1912 * if the drive is capable of it..
1913 */
1914 if (((softc->flags & DA_FLAG_OPEN) == 0)
1915 || (softc->quirks & DA_Q_NO_SYNC_CACHE))
1916 continue;
1917
1918 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
1919
1920 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
1921 scsi_synchronize_cache(&ccb.csio,
1922 /*retries*/1,
1923 /*cbfcnp*/dadone,
1924 MSG_SIMPLE_Q_TAG,
1925 /*begin_lba*/0, /* whole disk */
1926 /*lb_count*/0,
1927 SSD_FULL_SIZE,
1928 5 * 60 * 1000);
1929
1930 xpt_polled_action(&ccb);
1931
1932 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1933 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
1934 CAM_SCSI_STATUS_ERROR)
1935 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
1936 int error_code, sense_key, asc, ascq;
1937
1938 scsi_extract_sense(&ccb.csio.sense_data,
1939 &error_code, &sense_key,
1940 &asc, &ascq);
1941
1942 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1943 scsi_sense_print(&ccb.csio);
1944 } else {
1945 xpt_print_path(periph->path);
1946 printf("Synchronize cache failed, status "
1947 "== 0x%x, scsi status == 0x%x\n",
1948 ccb.ccb_h.status, ccb.csio.scsi_status);
1949 }
1950 }
1951
1952 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1953 cam_release_devq(ccb.ccb_h.path,
1954 /*relsim_flags*/0,
1955 /*reduction*/0,
1956 /*timeout*/0,
1957 /*getcount_only*/0);
1958
1959 }
1960}
1961
1962#else /* !_KERNEL */
1963
1964/*
1965 * XXX This is only left out of the kernel build to silence warnings. If,
1966 * for some reason this function is used in the kernel, the ifdefs should
1967 * be moved so it is included both in the kernel and userland.
1968 */
1969void
1970scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
1971 void (*cbfcnp)(struct cam_periph *, union ccb *),
1972 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
1973 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
1974 u_int32_t timeout)
1975{
1976 struct scsi_format_unit *scsi_cmd;
1977
1978 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
1979 scsi_cmd->opcode = FORMAT_UNIT;
1980 scsi_cmd->byte2 = byte2;
1981 scsi_ulto2b(ileave, scsi_cmd->interleave);
1982
1983 cam_fill_csio(csio,
1984 retries,
1985 cbfcnp,
1986 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
1987 tag_action,
1988 data_ptr,
1989 dxfer_len,
1990 sense_len,
1991 sizeof(*scsi_cmd),
1992 timeout);
1993}
1994
1995#endif /* _KERNEL */