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