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