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