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