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