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