]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/cam/scsi/scsi_da.c
MFC r228820, r228851:
[FreeBSD/stable/9.git] / sys / 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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33
34 #ifdef _KERNEL
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/bio.h>
38 #include <sys/sysctl.h>
39 #include <sys/taskqueue.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/conf.h>
43 #include <sys/devicestat.h>
44 #include <sys/eventhandler.h>
45 #include <sys/malloc.h>
46 #include <sys/cons.h>
47 #include <geom/geom.h>
48 #include <geom/geom_disk.h>
49 #endif /* _KERNEL */
50
51 #ifndef _KERNEL
52 #include <stdio.h>
53 #include <string.h>
54 #endif /* _KERNEL */
55
56 #include <cam/cam.h>
57 #include <cam/cam_ccb.h>
58 #include <cam/cam_periph.h>
59 #include <cam/cam_xpt_periph.h>
60 #include <cam/cam_sim.h>
61
62 #include <cam/scsi/scsi_message.h>
63
64 #ifndef _KERNEL 
65 #include <cam/scsi/scsi_da.h>
66 #endif /* !_KERNEL */
67
68 #ifdef _KERNEL
69 typedef enum {
70         DA_STATE_PROBE,
71         DA_STATE_PROBE2,
72         DA_STATE_NORMAL
73 } da_state;
74
75 typedef enum {
76         DA_FLAG_PACK_INVALID    = 0x001,
77         DA_FLAG_NEW_PACK        = 0x002,
78         DA_FLAG_PACK_LOCKED     = 0x004,
79         DA_FLAG_PACK_REMOVABLE  = 0x008,
80         DA_FLAG_TAGGED_QUEUING  = 0x010,
81         DA_FLAG_NEED_OTAG       = 0x020,
82         DA_FLAG_WENT_IDLE       = 0x040,
83         DA_FLAG_RETRY_UA        = 0x080,
84         DA_FLAG_OPEN            = 0x100,
85         DA_FLAG_SCTX_INIT       = 0x200
86 } da_flags;
87
88 typedef enum {
89         DA_Q_NONE               = 0x00,
90         DA_Q_NO_SYNC_CACHE      = 0x01,
91         DA_Q_NO_6_BYTE          = 0x02,
92         DA_Q_NO_PREVENT         = 0x04,
93         DA_Q_4K                 = 0x08
94 } da_quirks;
95
96 typedef enum {
97         DA_CCB_PROBE            = 0x01,
98         DA_CCB_PROBE2           = 0x02,
99         DA_CCB_BUFFER_IO        = 0x03,
100         DA_CCB_WAITING          = 0x04,
101         DA_CCB_DUMP             = 0x05,
102         DA_CCB_TYPE_MASK        = 0x0F,
103         DA_CCB_RETRY_UA         = 0x10
104 } da_ccb_state;
105
106 /* Offsets into our private area for storing information */
107 #define ccb_state       ppriv_field0
108 #define ccb_bp          ppriv_ptr1
109
110 struct disk_params {
111         u_int8_t  heads;
112         u_int32_t cylinders;
113         u_int8_t  secs_per_track;
114         u_int32_t secsize;      /* Number of bytes/sector */
115         u_int64_t sectors;      /* total number sectors */
116         u_int     stripesize;
117         u_int     stripeoffset;
118 };
119
120 struct da_softc {
121         struct   bio_queue_head bio_queue;
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      error_inject;
129         int      ordered_tag_count;
130         int      outstanding_cmds;
131         struct   disk_params params;
132         struct   disk *disk;
133         union    ccb saved_ccb;
134         struct task             sysctl_task;
135         struct sysctl_ctx_list  sysctl_ctx;
136         struct sysctl_oid       *sysctl_tree;
137         struct callout          sendordered_c;
138         uint64_t wwpn;
139 };
140
141 struct da_quirk_entry {
142         struct scsi_inquiry_pattern inq_pat;
143         da_quirks quirks;
144 };
145
146 static const char quantum[] = "QUANTUM";
147 static const char microp[] = "MICROP";
148
149 static struct da_quirk_entry da_quirk_table[] =
150 {
151         /* SPI, FC devices */
152         {
153                 /*
154                  * Fujitsu M2513A MO drives.
155                  * Tested devices: M2513A2 firmware versions 1200 & 1300.
156                  * (dip switch selects whether T_DIRECT or T_OPTICAL device)
157                  * Reported by: W.Scholten <whs@xs4all.nl>
158                  */
159                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
160                 /*quirks*/ DA_Q_NO_SYNC_CACHE
161         },
162         {
163                 /* See above. */
164                 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
165                 /*quirks*/ DA_Q_NO_SYNC_CACHE
166         },
167         {
168                 /*
169                  * This particular Fujitsu drive doesn't like the
170                  * synchronize cache command.
171                  * Reported by: Tom Jackson <toj@gorilla.net>
172                  */
173                 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
174                 /*quirks*/ DA_Q_NO_SYNC_CACHE
175         },
176         {
177                 /*
178                  * This drive doesn't like the synchronize cache command
179                  * either.  Reported by: Matthew Jacob <mjacob@feral.com>
180                  * in NetBSD PR kern/6027, August 24, 1998.
181                  */
182                 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
183                 /*quirks*/ DA_Q_NO_SYNC_CACHE
184         },
185         {
186                 /*
187                  * This drive doesn't like the synchronize cache command
188                  * either.  Reported by: Hellmuth Michaelis (hm@kts.org)
189                  * (PR 8882).
190                  */
191                 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
192                 /*quirks*/ DA_Q_NO_SYNC_CACHE
193         },
194         {
195                 /*
196                  * Doesn't like the synchronize cache command.
197                  * Reported by: Blaz Zupan <blaz@gold.amis.net>
198                  */
199                 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
200                 /*quirks*/ DA_Q_NO_SYNC_CACHE
201         },
202         {
203                 /*
204                  * Doesn't like the synchronize cache command.
205                  * Reported by: Blaz Zupan <blaz@gold.amis.net>
206                  */
207                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
208                 /*quirks*/ DA_Q_NO_SYNC_CACHE
209         },
210         {
211                 /*
212                  * Doesn't like the synchronize cache command.
213                  */
214                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
215                 /*quirks*/ DA_Q_NO_SYNC_CACHE
216         },
217         {
218                 /*
219                  * Doesn't like the synchronize cache command.
220                  * Reported by: walter@pelissero.de
221                  */
222                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
223                 /*quirks*/ DA_Q_NO_SYNC_CACHE
224         },
225         {
226                 /*
227                  * Doesn't work correctly with 6 byte reads/writes.
228                  * Returns illegal request, and points to byte 9 of the
229                  * 6-byte CDB.
230                  * Reported by:  Adam McDougall <bsdx@spawnet.com>
231                  */
232                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
233                 /*quirks*/ DA_Q_NO_6_BYTE
234         },
235         {
236                 /* See above. */
237                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
238                 /*quirks*/ DA_Q_NO_6_BYTE
239         },
240         {
241                 /*
242                  * Doesn't like the synchronize cache command.
243                  * Reported by: walter@pelissero.de
244                  */
245                 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
246                 /*quirks*/ DA_Q_NO_SYNC_CACHE
247         },
248         {
249                 /*
250                  * The CISS RAID controllers do not support SYNC_CACHE
251                  */
252                 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
253                 /*quirks*/ DA_Q_NO_SYNC_CACHE
254         },
255         /* USB mass storage devices supported by umass(4) */
256         {
257                 /*
258                  * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
259                  * PR: kern/51675
260                  */
261                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
262                 /*quirks*/ DA_Q_NO_SYNC_CACHE
263         },
264         {
265                 /*
266                  * Power Quotient Int. (PQI) USB flash key
267                  * PR: kern/53067
268                  */
269                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
270                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
271         },
272         {
273                 /*
274                  * Creative Nomad MUVO mp3 player (USB)
275                  * PR: kern/53094
276                  */
277                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
278                 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
279         },
280         {
281                 /*
282                  * Jungsoft NEXDISK USB flash key
283                  * PR: kern/54737
284                  */
285                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
286                 /*quirks*/ DA_Q_NO_SYNC_CACHE
287         },
288         {
289                 /*
290                  * FreeDik USB Mini Data Drive
291                  * PR: kern/54786
292                  */
293                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
294                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
295         },
296         {
297                 /*
298                  * Sigmatel USB Flash MP3 Player
299                  * PR: kern/57046
300                  */
301                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
302                 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
303         },
304         {
305                 /*
306                  * Neuros USB Digital Audio Computer
307                  * PR: kern/63645
308                  */
309                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
310                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
311         },
312         {
313                 /*
314                  * SEAGRAND NP-900 MP3 Player
315                  * PR: kern/64563
316                  */
317                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
318                 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
319         },
320         {
321                 /*
322                  * iRiver iFP MP3 player (with UMS Firmware)
323                  * PR: kern/54881, i386/63941, kern/66124
324                  */
325                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
326                 /*quirks*/ DA_Q_NO_SYNC_CACHE
327         },
328         {
329                 /*
330                  * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
331                  * PR: kern/70158
332                  */
333                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
334                 /*quirks*/ DA_Q_NO_SYNC_CACHE
335         },
336         {
337                 /*
338                  * ZICPlay USB MP3 Player with FM
339                  * PR: kern/75057
340                  */
341                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
342                 /*quirks*/ DA_Q_NO_SYNC_CACHE
343         },
344         {
345                 /*
346                  * TEAC USB floppy mechanisms
347                  */
348                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
349                 /*quirks*/ DA_Q_NO_SYNC_CACHE
350         },
351         {
352                 /*
353                  * Kingston DataTraveler II+ USB Pen-Drive.
354                  * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
355                  */
356                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
357                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
358         },
359         {
360                 /*
361                  * Motorola E398 Mobile Phone (TransFlash memory card).
362                  * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
363                  * PR: usb/89889
364                  */
365                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
366                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
367         },
368         {
369                 /*
370                  * Qware BeatZkey! Pro
371                  * PR: usb/79164
372                  */
373                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
374                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
375         },
376         {
377                 /*
378                  * Time DPA20B 1GB MP3 Player
379                  * PR: usb/81846
380                  */
381                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
382                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
383         },
384         {
385                 /*
386                  * Samsung USB key 128Mb
387                  * PR: usb/90081
388                  */
389                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
390                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
391         },
392         {
393                 /*
394                  * Kingston DataTraveler 2.0 USB Flash memory.
395                  * PR: usb/89196
396                  */
397                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
398                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
399         },
400         {
401                 /*
402                  * Creative MUVO Slim mp3 player (USB)
403                  * PR: usb/86131
404                  */
405                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
406                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
407                 },
408         {
409                 /*
410                  * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
411                  * PR: usb/80487
412                  */
413                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
414                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
415         },
416         {
417                 /*
418                  * SanDisk Micro Cruzer 128MB
419                  * PR: usb/75970
420                  */
421                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
422                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
423         },
424         {
425                 /*
426                  * TOSHIBA TransMemory USB sticks
427                  * PR: kern/94660
428                  */
429                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
430                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
431         },
432         {
433                 /*
434                  * PNY USB Flash keys
435                  * PR: usb/75578, usb/72344, usb/65436 
436                  */
437                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
438                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
439         },
440         {
441                 /*
442                  * Genesys 6-in-1 Card Reader
443                  * PR: usb/94647
444                  */
445                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
446                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
447         },
448         {
449                 /*
450                  * Rekam Digital CAMERA
451                  * PR: usb/98713
452                  */
453                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
454                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
455         },
456         {
457                 /*
458                  * iRiver H10 MP3 player
459                  * PR: usb/102547
460                  */
461                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
462                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
463         },
464         {
465                 /*
466                  * iRiver U10 MP3 player
467                  * PR: usb/92306
468                  */
469                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
470                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
471         },
472         {
473                 /*
474                  * X-Micro Flash Disk
475                  * PR: usb/96901
476                  */
477                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
478                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
479         },
480         {
481                 /*
482                  * EasyMP3 EM732X USB 2.0 Flash MP3 Player
483                  * PR: usb/96546
484                  */
485                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
486                 "1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
487         },
488         {
489                 /*
490                  * Denver MP3 player
491                  * PR: usb/107101
492                  */
493                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
494                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
495         },
496         {
497                 /*
498                  * Philips USB Key Audio KEY013
499                  * PR: usb/68412
500                  */
501                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
502                 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
503         },
504         {
505                 /*
506                  * JNC MP3 Player
507                  * PR: usb/94439
508                  */
509                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
510                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
511         },
512         {
513                 /*
514                  * SAMSUNG MP0402H
515                  * PR: usb/108427
516                  */
517                 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
518                 /*quirks*/ DA_Q_NO_SYNC_CACHE
519         },
520         {
521                 /*
522                  * I/O Magic USB flash - Giga Bank
523                  * PR: usb/108810
524                  */
525                 {T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
526                 /*quirks*/ DA_Q_NO_SYNC_CACHE
527         },
528         {
529                 /*
530                  * JoyFly 128mb USB Flash Drive
531                  * PR: 96133
532                  */
533                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
534                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
535         },
536         {
537                 /*
538                  * ChipsBnk usb stick
539                  * PR: 103702
540                  */
541                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
542                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
543         },
544         {
545                 /*
546                  * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A
547                  * PR: 129858
548                  */
549                 {T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*",
550                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
551         },
552         {
553                 /*
554                  * Samsung YP-U3 mp3-player
555                  * PR: 125398
556                  */
557                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3",
558                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
559         },
560         {
561                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
562                  "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
563         },
564         {
565                 /*
566                  * Sony Cyber-Shot DSC cameras
567                  * PR: usb/137035
568                  */
569                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
570                 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
571         },
572         /* ATA/SATA devices over SAS/USB/... */
573         {
574                 /* Hitachi Advanced Format (4k) drives */
575                 { T_DIRECT, SIP_MEDIA_FIXED, "Hitachi", "H??????????E3*", "*" },
576                 /*quirks*/DA_Q_4K
577         },
578         {
579                 /* Samsung Advanced Format (4k) drives */
580                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD155UI*", "*" },
581                 /*quirks*/DA_Q_4K
582         },
583         {
584                 /* Samsung Advanced Format (4k) drives */
585                 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD155UI*", "*" },
586                 /*quirks*/DA_Q_4K
587         },
588         {
589                 /* Samsung Advanced Format (4k) drives */
590                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD204UI*", "*" },
591                 /*quirks*/DA_Q_4K
592         },
593         {
594                 /* Samsung Advanced Format (4k) drives */
595                 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD204UI*", "*" },
596                 /*quirks*/DA_Q_4K
597         },
598         {
599                 /* Seagate Barracuda Green Advanced Format (4k) drives */
600                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DL*", "*" },
601                 /*quirks*/DA_Q_4K
602         },
603         {
604                 /* Seagate Barracuda Green Advanced Format (4k) drives */
605                 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DL", "*", "*" },
606                 /*quirks*/DA_Q_4K
607         },
608         {
609                 /* Seagate Barracuda Green Advanced Format (4k) drives */
610                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???DM*", "*" },
611                 /*quirks*/DA_Q_4K
612         },
613         {
614                 /* Seagate Barracuda Green Advanced Format (4k) drives */
615                 { T_DIRECT, SIP_MEDIA_FIXED, "ST???DM*", "*", "*" },
616                 /*quirks*/DA_Q_4K
617         },
618         {
619                 /* Seagate Barracuda Green Advanced Format (4k) drives */
620                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DM*", "*" },
621                 /*quirks*/DA_Q_4K
622         },
623         {
624                 /* Seagate Barracuda Green Advanced Format (4k) drives */
625                 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DM", "*", "*" },
626                 /*quirks*/DA_Q_4K
627         },
628         {
629                 /* Seagate Momentus Advanced Format (4k) drives */
630                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500423AS*", "*" },
631                 /*quirks*/DA_Q_4K
632         },
633         {
634                 /* Seagate Momentus Advanced Format (4k) drives */
635                 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "3AS*", "*" },
636                 /*quirks*/DA_Q_4K
637         },
638         {
639                 /* Seagate Momentus Advanced Format (4k) drives */
640                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500424AS*", "*" },
641                 /*quirks*/DA_Q_4K
642         },
643         {
644                 /* Seagate Momentus Advanced Format (4k) drives */
645                 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "4AS*", "*" },
646                 /*quirks*/DA_Q_4K
647         },
648         {
649                 /* Seagate Momentus Advanced Format (4k) drives */
650                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640423AS*", "*" },
651                 /*quirks*/DA_Q_4K
652         },
653         {
654                 /* Seagate Momentus Advanced Format (4k) drives */
655                 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "3AS*", "*" },
656                 /*quirks*/DA_Q_4K
657         },
658         {
659                 /* Seagate Momentus Advanced Format (4k) drives */
660                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640424AS*", "*" },
661                 /*quirks*/DA_Q_4K
662         },
663         {
664                 /* Seagate Momentus Advanced Format (4k) drives */
665                 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "4AS*", "*" },
666                 /*quirks*/DA_Q_4K
667         },
668         {
669                 /* Seagate Momentus Advanced Format (4k) drives */
670                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750420AS*", "*" },
671                 /*quirks*/DA_Q_4K
672         },
673         {
674                 /* Seagate Momentus Advanced Format (4k) drives */
675                 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "0AS*", "*" },
676                 /*quirks*/DA_Q_4K
677         },
678         {
679                 /* Seagate Momentus Advanced Format (4k) drives */
680                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750422AS*", "*" },
681                 /*quirks*/DA_Q_4K
682         },
683         {
684                 /* Seagate Momentus Advanced Format (4k) drives */
685                 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "2AS*", "*" },
686                 /*quirks*/DA_Q_4K
687         },
688         {
689                 /* Seagate Momentus Advanced Format (4k) drives */
690                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750423AS*", "*" },
691                 /*quirks*/DA_Q_4K
692         },
693         {
694                 /* Seagate Momentus Advanced Format (4k) drives */
695                 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "3AS*", "*" },
696                 /*quirks*/DA_Q_4K
697         },
698         {
699                 /* Seagate Momentus Thin Advanced Format (4k) drives */
700                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???LT*", "*" },
701                 /*quirks*/DA_Q_4K
702         },
703         {
704                 /* Seagate Momentus Thin Advanced Format (4k) drives */
705                 { T_DIRECT, SIP_MEDIA_FIXED, "ST???LT*", "*", "*" },
706                 /*quirks*/DA_Q_4K
707         },
708         {
709                 /* WDC Caviar Green Advanced Format (4k) drives */
710                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RS*", "*" },
711                 /*quirks*/DA_Q_4K
712         },
713         {
714                 /* WDC Caviar Green Advanced Format (4k) drives */
715                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RS*", "*" },
716                 /*quirks*/DA_Q_4K
717         },
718         {
719                 /* WDC Caviar Green Advanced Format (4k) drives */
720                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RX*", "*" },
721                 /*quirks*/DA_Q_4K
722         },
723         {
724                 /* WDC Caviar Green Advanced Format (4k) drives */
725                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RX*", "*" },
726                 /*quirks*/DA_Q_4K
727         },
728         {
729                 /* WDC Caviar Green Advanced Format (4k) drives */
730                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RS*", "*" },
731                 /*quirks*/DA_Q_4K
732         },
733         {
734                 /* WDC Caviar Green Advanced Format (4k) drives */
735                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RS*", "*" },
736                 /*quirks*/DA_Q_4K
737         },
738         {
739                 /* WDC Caviar Green Advanced Format (4k) drives */
740                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RX*", "*" },
741                 /*quirks*/DA_Q_4K
742         },
743         {
744                 /* WDC Caviar Green Advanced Format (4k) drives */
745                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RX*", "*" },
746                 /*quirks*/DA_Q_4K
747         },
748         {
749                 /* WDC Scorpio Black Advanced Format (4k) drives */
750                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PKT*", "*" },
751                 /*quirks*/DA_Q_4K
752         },
753         {
754                 /* WDC Scorpio Black Advanced Format (4k) drives */
755                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PKT*", "*" },
756                 /*quirks*/DA_Q_4K
757         },
758         {
759                 /* WDC Scorpio Black Advanced Format (4k) drives */
760                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PKT*", "*" },
761                 /*quirks*/DA_Q_4K
762         },
763         {
764                 /* WDC Scorpio Black Advanced Format (4k) drives */
765                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PKT*", "*" },
766                 /*quirks*/DA_Q_4K
767         },
768         {
769                 /* WDC Scorpio Blue Advanced Format (4k) drives */
770                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PVT*", "*" },
771                 /*quirks*/DA_Q_4K
772         },
773         {
774                 /* WDC Scorpio Blue Advanced Format (4k) drives */
775                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PVT*", "*" },
776                 /*quirks*/DA_Q_4K
777         },
778         {
779                 /* WDC Scorpio Blue Advanced Format (4k) drives */
780                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PVT*", "*" },
781                 /*quirks*/DA_Q_4K
782         },
783         {
784                 /* WDC Scorpio Blue Advanced Format (4k) drives */
785                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PVT*", "*" },
786                 /*quirks*/DA_Q_4K
787         },
788 };
789
790 static  disk_strategy_t dastrategy;
791 static  dumper_t        dadump;
792 static  periph_init_t   dainit;
793 static  void            daasync(void *callback_arg, u_int32_t code,
794                                 struct cam_path *path, void *arg);
795 static  void            dasysctlinit(void *context, int pending);
796 static  int             dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
797 static  periph_ctor_t   daregister;
798 static  periph_dtor_t   dacleanup;
799 static  periph_start_t  dastart;
800 static  periph_oninv_t  daoninvalidate;
801 static  void            dadone(struct cam_periph *periph,
802                                union ccb *done_ccb);
803 static  int             daerror(union ccb *ccb, u_int32_t cam_flags,
804                                 u_int32_t sense_flags);
805 static void             daprevent(struct cam_periph *periph, int action);
806 static int              dagetcapacity(struct cam_periph *periph);
807 static void             dasetgeom(struct cam_periph *periph, uint32_t block_len,
808                                   uint64_t maxsector, u_int lbppbe, u_int lalba);
809 static timeout_t        dasendorderedtag;
810 static void             dashutdown(void *arg, int howto);
811
812 #ifndef DA_DEFAULT_TIMEOUT
813 #define DA_DEFAULT_TIMEOUT 60   /* Timeout in seconds */
814 #endif
815
816 #ifndef DA_DEFAULT_RETRY
817 #define DA_DEFAULT_RETRY        4
818 #endif
819
820 #ifndef DA_DEFAULT_SEND_ORDERED
821 #define DA_DEFAULT_SEND_ORDERED 1
822 #endif
823
824
825 static int da_retry_count = DA_DEFAULT_RETRY;
826 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
827 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
828
829 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
830             "CAM Direct Access Disk driver");
831 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
832            &da_retry_count, 0, "Normal I/O retry count");
833 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
834 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
835            &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
836 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
837 SYSCTL_INT(_kern_cam_da, OID_AUTO, da_send_ordered, CTLFLAG_RW,
838            &da_send_ordered, 0, "Send Ordered Tags");
839 TUNABLE_INT("kern.cam.da.da_send_ordered", &da_send_ordered);
840
841 /*
842  * DA_ORDEREDTAG_INTERVAL determines how often, relative
843  * to the default timeout, we check to see whether an ordered
844  * tagged transaction is appropriate to prevent simple tag
845  * starvation.  Since we'd like to ensure that there is at least
846  * 1/2 of the timeout length left for a starved transaction to
847  * complete after we've sent an ordered tag, we must poll at least
848  * four times in every timeout period.  This takes care of the worst
849  * case where a starved transaction starts during an interval that
850  * meets the requirement "don't send an ordered tag" test so it takes
851  * us two intervals to determine that a tag must be sent.
852  */
853 #ifndef DA_ORDEREDTAG_INTERVAL
854 #define DA_ORDEREDTAG_INTERVAL 4
855 #endif
856
857 static struct periph_driver dadriver =
858 {
859         dainit, "da",
860         TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
861 };
862
863 PERIPHDRIVER_DECLARE(da, dadriver);
864
865 MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
866
867 static int
868 daopen(struct disk *dp)
869 {
870         struct cam_periph *periph;
871         struct da_softc *softc;
872         int unit;
873         int error;
874
875         periph = (struct cam_periph *)dp->d_drv1;
876         if (periph == NULL) {
877                 return (ENXIO); 
878         }
879
880         if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
881                 return (ENXIO);
882         }
883
884         cam_periph_lock(periph);
885         if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
886                 cam_periph_unlock(periph);
887                 cam_periph_release(periph);
888                 return (error);
889         }
890
891         unit = periph->unit_number;
892         softc = (struct da_softc *)periph->softc;
893         softc->flags |= DA_FLAG_OPEN;
894
895         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
896             ("daopen: disk=%s%d (unit %d)\n", dp->d_name, dp->d_unit,
897              unit));
898
899         if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
900                 /* Invalidate our pack information. */
901                 softc->flags &= ~DA_FLAG_PACK_INVALID;
902         }
903
904         error = dagetcapacity(periph);
905
906         if (error == 0) {
907
908                 softc->disk->d_sectorsize = softc->params.secsize;
909                 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
910                 softc->disk->d_stripesize = softc->params.stripesize;
911                 softc->disk->d_stripeoffset = softc->params.stripeoffset;
912                 /* XXX: these are not actually "firmware" values, so they may be wrong */
913                 softc->disk->d_fwsectors = softc->params.secs_per_track;
914                 softc->disk->d_fwheads = softc->params.heads;
915                 softc->disk->d_devstat->block_size = softc->params.secsize;
916                 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
917
918                 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
919                     (softc->quirks & DA_Q_NO_PREVENT) == 0)
920                         daprevent(periph, PR_PREVENT);
921         } else
922                 softc->flags &= ~DA_FLAG_OPEN;
923
924         cam_periph_unhold(periph);
925         cam_periph_unlock(periph);
926
927         if (error != 0) {
928                 cam_periph_release(periph);
929         }
930         return (error);
931 }
932
933 static int
934 daclose(struct disk *dp)
935 {
936         struct  cam_periph *periph;
937         struct  da_softc *softc;
938         int error;
939
940         periph = (struct cam_periph *)dp->d_drv1;
941         if (periph == NULL)
942                 return (0);     
943
944         cam_periph_lock(periph);
945         if ((error = cam_periph_hold(periph, PRIBIO)) != 0) {
946                 cam_periph_unlock(periph);
947                 cam_periph_release(periph);
948                 return (0);
949         }
950
951         softc = (struct da_softc *)periph->softc;
952
953         if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0
954          && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
955                 union   ccb *ccb;
956
957                 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
958
959                 scsi_synchronize_cache(&ccb->csio,
960                                        /*retries*/1,
961                                        /*cbfcnp*/dadone,
962                                        MSG_SIMPLE_Q_TAG,
963                                        /*begin_lba*/0,/* Cover the whole disk */
964                                        /*lb_count*/0,
965                                        SSD_FULL_SIZE,
966                                        5 * 60 * 1000);
967
968                 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
969                                   /*sense_flags*/SF_RETRY_UA,
970                                   softc->disk->d_devstat);
971
972                 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
973                         if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
974                              CAM_SCSI_STATUS_ERROR) {
975                                 int asc, ascq;
976                                 int sense_key, error_code;
977
978                                 scsi_extract_sense_len(&ccb->csio.sense_data,
979                                     ccb->csio.sense_len - ccb->csio.sense_resid,
980                                     &error_code, &sense_key, &asc, &ascq,
981                                     /*show_errors*/ 1);
982                                 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
983                                         scsi_sense_print(&ccb->csio);
984                         } else {
985                                 xpt_print(periph->path, "Synchronize cache "
986                                     "failed, status == 0x%x, scsi status == "
987                                     "0x%x\n", ccb->csio.ccb_h.status,
988                                     ccb->csio.scsi_status);
989                         }
990                 }
991
992                 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
993                         cam_release_devq(ccb->ccb_h.path,
994                                          /*relsim_flags*/0,
995                                          /*reduction*/0,
996                                          /*timeout*/0,
997                                          /*getcount_only*/0);
998
999                 xpt_release_ccb(ccb);
1000
1001         }
1002
1003         if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
1004                 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
1005                         daprevent(periph, PR_ALLOW);
1006                 /*
1007                  * If we've got removeable media, mark the blocksize as
1008                  * unavailable, since it could change when new media is
1009                  * inserted.
1010                  */
1011                 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1012         }
1013
1014         softc->flags &= ~DA_FLAG_OPEN;
1015         cam_periph_unhold(periph);
1016         cam_periph_unlock(periph);
1017         cam_periph_release(periph);
1018         return (0);     
1019 }
1020
1021 /*
1022  * Actually translate the requested transfer into one the physical driver
1023  * can understand.  The transfer is described by a buf and will include
1024  * only one physical transfer.
1025  */
1026 static void
1027 dastrategy(struct bio *bp)
1028 {
1029         struct cam_periph *periph;
1030         struct da_softc *softc;
1031         
1032         periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1033         if (periph == NULL) {
1034                 biofinish(bp, NULL, ENXIO);
1035                 return;
1036         }
1037         softc = (struct da_softc *)periph->softc;
1038
1039         cam_periph_lock(periph);
1040
1041         /*
1042          * If the device has been made invalid, error out
1043          */
1044         if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1045                 cam_periph_unlock(periph);
1046                 biofinish(bp, NULL, ENXIO);
1047                 return;
1048         }
1049         
1050         /*
1051          * Place it in the queue of disk activities for this disk
1052          */
1053         bioq_disksort(&softc->bio_queue, bp);
1054
1055         /*
1056          * Schedule ourselves for performing the work.
1057          */
1058         xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1059         cam_periph_unlock(periph);
1060
1061         return;
1062 }
1063
1064 static int
1065 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1066 {
1067         struct      cam_periph *periph;
1068         struct      da_softc *softc;
1069         u_int       secsize;
1070         struct      ccb_scsiio csio;
1071         struct      disk *dp;
1072
1073         dp = arg;
1074         periph = dp->d_drv1;
1075         if (periph == NULL)
1076                 return (ENXIO);
1077         softc = (struct da_softc *)periph->softc;
1078         cam_periph_lock(periph);
1079         secsize = softc->params.secsize;
1080         
1081         if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1082                 cam_periph_unlock(periph);
1083                 return (ENXIO);
1084         }
1085
1086         if (length > 0) {
1087                 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1088                 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1089                 scsi_read_write(&csio,
1090                                 /*retries*/1,
1091                                 dadone,
1092                                 MSG_ORDERED_Q_TAG,
1093                                 /*read*/FALSE,
1094                                 /*byte2*/0,
1095                                 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1096                                 offset / secsize,
1097                                 length / secsize,
1098                                 /*data_ptr*/(u_int8_t *) virtual,
1099                                 /*dxfer_len*/length,
1100                                 /*sense_len*/SSD_FULL_SIZE,
1101                                 DA_DEFAULT_TIMEOUT * 1000);             
1102                 xpt_polled_action((union ccb *)&csio);
1103
1104                 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1105                         printf("Aborting dump due to I/O error.\n");
1106                         if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
1107                              CAM_SCSI_STATUS_ERROR)
1108                                 scsi_sense_print(&csio);
1109                         else
1110                                 printf("status == 0x%x, scsi status == 0x%x\n",
1111                                        csio.ccb_h.status, csio.scsi_status);
1112                         return(EIO);
1113                 }
1114                 cam_periph_unlock(periph);
1115                 return(0);
1116         }
1117                 
1118         /*
1119          * Sync the disk cache contents to the physical media.
1120          */
1121         if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1122
1123                 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1124                 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1125                 scsi_synchronize_cache(&csio,
1126                                        /*retries*/1,
1127                                        /*cbfcnp*/dadone,
1128                                        MSG_SIMPLE_Q_TAG,
1129                                        /*begin_lba*/0,/* Cover the whole disk */
1130                                        /*lb_count*/0,
1131                                        SSD_FULL_SIZE,
1132                                        5 * 60 * 1000);
1133                 xpt_polled_action((union ccb *)&csio);
1134
1135                 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1136                         if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
1137                              CAM_SCSI_STATUS_ERROR) {
1138                                 int asc, ascq;
1139                                 int sense_key, error_code;
1140
1141                                 scsi_extract_sense_len(&csio.sense_data,
1142                                     csio.sense_len - csio.sense_resid,
1143                                     &error_code, &sense_key, &asc, &ascq,
1144                                     /*show_errors*/ 1);
1145                                 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1146                                         scsi_sense_print(&csio);
1147                         } else {
1148                                 xpt_print(periph->path, "Synchronize cache "
1149                                     "failed, status == 0x%x, scsi status == "
1150                                     "0x%x\n", csio.ccb_h.status,
1151                                     csio.scsi_status);
1152                         }
1153                 }
1154         }
1155         cam_periph_unlock(periph);
1156         return (0);
1157 }
1158
1159 static int
1160 dagetattr(struct bio *bp)
1161 {
1162         int ret = -1;
1163         struct cam_periph *periph;
1164
1165         if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
1166                 return ENXIO;
1167         periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1168         if (periph->path == NULL)
1169                 return ENXIO;
1170
1171         ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1172             periph->path);
1173         if (ret == 0)
1174                 bp->bio_completed = bp->bio_length;
1175         return ret;
1176 }
1177
1178 static void
1179 dainit(void)
1180 {
1181         cam_status status;
1182
1183         /*
1184          * Install a global async callback.  This callback will
1185          * receive async callbacks like "new device found".
1186          */
1187         status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1188
1189         if (status != CAM_REQ_CMP) {
1190                 printf("da: Failed to attach master async callback "
1191                        "due to status 0x%x!\n", status);
1192         } else if (da_send_ordered) {
1193
1194                 /* Register our shutdown event handler */
1195                 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown, 
1196                                            NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1197                     printf("dainit: shutdown event registration failed!\n");
1198         }
1199 }
1200
1201 static void
1202 daoninvalidate(struct cam_periph *periph)
1203 {
1204         struct da_softc *softc;
1205
1206         softc = (struct da_softc *)periph->softc;
1207
1208         /*
1209          * De-register any async callbacks.
1210          */
1211         xpt_register_async(0, daasync, periph, periph->path);
1212
1213         softc->flags |= DA_FLAG_PACK_INVALID;
1214
1215         /*
1216          * Return all queued I/O with ENXIO.
1217          * XXX Handle any transactions queued to the card
1218          *     with XPT_ABORT_CCB.
1219          */
1220         bioq_flush(&softc->bio_queue, NULL, ENXIO);
1221
1222         disk_gone(softc->disk);
1223         xpt_print(periph->path, "lost device - %d outstanding, %d refs\n",
1224                   softc->outstanding_cmds, periph->refcount);
1225 }
1226
1227 static void
1228 dacleanup(struct cam_periph *periph)
1229 {
1230         struct da_softc *softc;
1231
1232         softc = (struct da_softc *)periph->softc;
1233
1234         xpt_print(periph->path, "removing device entry\n");
1235         cam_periph_unlock(periph);
1236
1237         /*
1238          * If we can't free the sysctl tree, oh well...
1239          */
1240         if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1241             && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1242                 xpt_print(periph->path, "can't remove sysctl context\n");
1243         }
1244
1245         disk_destroy(softc->disk);
1246         callout_drain(&softc->sendordered_c);
1247         free(softc, M_DEVBUF);
1248         cam_periph_lock(periph);
1249 }
1250
1251 static void
1252 daasync(void *callback_arg, u_int32_t code,
1253         struct cam_path *path, void *arg)
1254 {
1255         struct cam_periph *periph;
1256
1257         periph = (struct cam_periph *)callback_arg;
1258         switch (code) {
1259         case AC_FOUND_DEVICE:
1260         {
1261                 struct ccb_getdev *cgd;
1262                 cam_status status;
1263  
1264                 cgd = (struct ccb_getdev *)arg;
1265                 if (cgd == NULL)
1266                         break;
1267
1268                 if (cgd->protocol != PROTO_SCSI)
1269                         break;
1270
1271                 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1272                     && SID_TYPE(&cgd->inq_data) != T_RBC
1273                     && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1274                         break;
1275
1276                 /*
1277                  * Allocate a peripheral instance for
1278                  * this device and start the probe
1279                  * process.
1280                  */
1281                 status = cam_periph_alloc(daregister, daoninvalidate,
1282                                           dacleanup, dastart,
1283                                           "da", CAM_PERIPH_BIO,
1284                                           cgd->ccb_h.path, daasync,
1285                                           AC_FOUND_DEVICE, cgd);
1286
1287                 if (status != CAM_REQ_CMP
1288                  && status != CAM_REQ_INPROG)
1289                         printf("daasync: Unable to attach to new device "
1290                                 "due to status 0x%x\n", status);
1291                 return;
1292         }
1293         case AC_ADVINFO_CHANGED:
1294         {
1295                 uintptr_t buftype;
1296
1297                 buftype = (uintptr_t)arg;
1298                 if (buftype == CDAI_TYPE_PHYS_PATH) {
1299                         struct da_softc *softc;
1300
1301                         softc = periph->softc;
1302                         disk_attr_changed(softc->disk, "GEOM::physpath",
1303                                           M_NOWAIT);
1304                 }
1305                 break;
1306         }
1307         case AC_SENT_BDR:
1308         case AC_BUS_RESET:
1309         {
1310                 struct da_softc *softc;
1311                 struct ccb_hdr *ccbh;
1312
1313                 softc = (struct da_softc *)periph->softc;
1314                 /*
1315                  * Don't fail on the expected unit attention
1316                  * that will occur.
1317                  */
1318                 softc->flags |= DA_FLAG_RETRY_UA;
1319                 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1320                         ccbh->ccb_state |= DA_CCB_RETRY_UA;
1321                 break;
1322         }
1323         default:
1324                 break;
1325         }
1326         cam_periph_async(periph, code, path, arg);
1327 }
1328
1329 static void
1330 dasysctlinit(void *context, int pending)
1331 {
1332         struct cam_periph *periph;
1333         struct da_softc *softc;
1334         char tmpstr[80], tmpstr2[80];
1335         struct ccb_trans_settings cts;
1336
1337         periph = (struct cam_periph *)context;
1338         /*
1339          * periph was held for us when this task was enqueued
1340          */
1341         if (periph->flags & CAM_PERIPH_INVALID) {
1342                 cam_periph_release(periph);
1343                 return;
1344         }
1345
1346         softc = (struct da_softc *)periph->softc;
1347         snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1348         snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1349
1350         sysctl_ctx_init(&softc->sysctl_ctx);
1351         softc->flags |= DA_FLAG_SCTX_INIT;
1352         softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1353                 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1354                 CTLFLAG_RD, 0, tmpstr);
1355         if (softc->sysctl_tree == NULL) {
1356                 printf("dasysctlinit: unable to allocate sysctl tree\n");
1357                 cam_periph_release(periph);
1358                 return;
1359         }
1360
1361         /*
1362          * Now register the sysctl handler, so the user can change the value on
1363          * the fly.
1364          */
1365         SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1366                 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1367                 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1368                 "Minimum CDB size");
1369
1370         SYSCTL_ADD_INT(&softc->sysctl_ctx,
1371                        SYSCTL_CHILDREN(softc->sysctl_tree),
1372                        OID_AUTO,
1373                        "error_inject",
1374                        CTLFLAG_RW,
1375                        &softc->error_inject,
1376                        0,
1377                        "error_inject leaf");
1378
1379
1380         /*
1381          * Add some addressing info.
1382          */
1383         memset(&cts, 0, sizeof (cts));
1384         xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
1385         cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1386         cts.type = CTS_TYPE_CURRENT_SETTINGS;
1387         cam_periph_lock(periph);
1388         xpt_action((union ccb *)&cts);
1389         cam_periph_unlock(periph);
1390         if (cts.ccb_h.status != CAM_REQ_CMP) {
1391                 cam_periph_release(periph);
1392                 return;
1393         }
1394         if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
1395                 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1396                 if (fc->valid & CTS_FC_VALID_WWPN) {
1397                         softc->wwpn = fc->wwpn;
1398                         SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1399                             SYSCTL_CHILDREN(softc->sysctl_tree),
1400                             OID_AUTO, "wwpn", CTLFLAG_RD,
1401                             &softc->wwpn, "World Wide Port Name");
1402                 }
1403         }
1404         cam_periph_release(periph);
1405 }
1406
1407 static int
1408 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1409 {
1410         int error, value;
1411
1412         value = *(int *)arg1;
1413
1414         error = sysctl_handle_int(oidp, &value, 0, req);
1415
1416         if ((error != 0)
1417          || (req->newptr == NULL))
1418                 return (error);
1419
1420         /*
1421          * Acceptable values here are 6, 10, 12 or 16.
1422          */
1423         if (value < 6)
1424                 value = 6;
1425         else if ((value > 6)
1426               && (value <= 10))
1427                 value = 10;
1428         else if ((value > 10)
1429               && (value <= 12))
1430                 value = 12;
1431         else if (value > 12)
1432                 value = 16;
1433
1434         *(int *)arg1 = value;
1435
1436         return (0);
1437 }
1438
1439 static cam_status
1440 daregister(struct cam_periph *periph, void *arg)
1441 {
1442         struct da_softc *softc;
1443         struct ccb_pathinq cpi;
1444         struct ccb_getdev *cgd;
1445         char tmpstr[80];
1446         caddr_t match;
1447
1448         cgd = (struct ccb_getdev *)arg;
1449         if (periph == NULL) {
1450                 printf("daregister: periph was NULL!!\n");
1451                 return(CAM_REQ_CMP_ERR);
1452         }
1453
1454         if (cgd == NULL) {
1455                 printf("daregister: no getdev CCB, can't register device\n");
1456                 return(CAM_REQ_CMP_ERR);
1457         }
1458
1459         softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
1460             M_NOWAIT|M_ZERO);
1461
1462         if (softc == NULL) {
1463                 printf("daregister: Unable to probe new device. "
1464                        "Unable to allocate softc\n");                           
1465                 return(CAM_REQ_CMP_ERR);
1466         }
1467
1468         LIST_INIT(&softc->pending_ccbs);
1469         softc->state = DA_STATE_PROBE;
1470         bioq_init(&softc->bio_queue);
1471         if (SID_IS_REMOVABLE(&cgd->inq_data))
1472                 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1473         if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1474                 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1475
1476         periph->softc = softc;
1477
1478         /*
1479          * See if this device has any quirks.
1480          */
1481         match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1482                                (caddr_t)da_quirk_table,
1483                                sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1484                                sizeof(*da_quirk_table), scsi_inquiry_match);
1485
1486         if (match != NULL)
1487                 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1488         else
1489                 softc->quirks = DA_Q_NONE;
1490
1491         /* Check if the SIM does not want 6 byte commands */
1492         bzero(&cpi, sizeof(cpi));
1493         xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1494         cpi.ccb_h.func_code = XPT_PATH_INQ;
1495         xpt_action((union ccb *)&cpi);
1496         if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1497                 softc->quirks |= DA_Q_NO_6_BYTE;
1498
1499         TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1500
1501         /*
1502          * Take an exclusive refcount on the periph while dastart is called
1503          * to finish the probe.  The reference will be dropped in dadone at
1504          * the end of probe.
1505          */
1506         (void)cam_periph_hold(periph, PRIBIO);
1507
1508         /*
1509          * Schedule a periodic event to occasionally send an
1510          * ordered tag to a device.
1511          */
1512         callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
1513         callout_reset(&softc->sendordered_c,
1514             (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
1515             dasendorderedtag, softc);
1516
1517         mtx_unlock(periph->sim->mtx);
1518         /*
1519          * RBC devices don't have to support READ(6), only READ(10).
1520          */
1521         if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1522                 softc->minimum_cmd_size = 10;
1523         else
1524                 softc->minimum_cmd_size = 6;
1525
1526         /*
1527          * Load the user's default, if any.
1528          */
1529         snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1530                  periph->unit_number);
1531         TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1532
1533         /*
1534          * 6, 10, 12 and 16 are the currently permissible values.
1535          */
1536         if (softc->minimum_cmd_size < 6)
1537                 softc->minimum_cmd_size = 6;
1538         else if ((softc->minimum_cmd_size > 6)
1539               && (softc->minimum_cmd_size <= 10))
1540                 softc->minimum_cmd_size = 10;
1541         else if ((softc->minimum_cmd_size > 10)
1542               && (softc->minimum_cmd_size <= 12))
1543                 softc->minimum_cmd_size = 12;
1544         else if (softc->minimum_cmd_size > 12)
1545                 softc->minimum_cmd_size = 16;
1546
1547         /*
1548          * Register this media as a disk.
1549          */
1550         softc->disk = disk_alloc();
1551         softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
1552                           periph->unit_number, 0,
1553                           DEVSTAT_BS_UNAVAILABLE,
1554                           SID_TYPE(&cgd->inq_data) |
1555                           XPORT_DEVSTAT_TYPE(cpi.transport),
1556                           DEVSTAT_PRIORITY_DISK);
1557         softc->disk->d_open = daopen;
1558         softc->disk->d_close = daclose;
1559         softc->disk->d_strategy = dastrategy;
1560         softc->disk->d_dump = dadump;
1561         softc->disk->d_getattr = dagetattr;
1562         softc->disk->d_name = "da";
1563         softc->disk->d_drv1 = periph;
1564         if (cpi.maxio == 0)
1565                 softc->disk->d_maxsize = DFLTPHYS;      /* traditional default */
1566         else if (cpi.maxio > MAXPHYS)
1567                 softc->disk->d_maxsize = MAXPHYS;       /* for safety */
1568         else
1569                 softc->disk->d_maxsize = cpi.maxio;
1570         softc->disk->d_unit = periph->unit_number;
1571         softc->disk->d_flags = 0;
1572         if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
1573                 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
1574         cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
1575             sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
1576         strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
1577         cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
1578             cgd->inq_data.product, sizeof(cgd->inq_data.product),
1579             sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
1580         softc->disk->d_hba_vendor = cpi.hba_vendor;
1581         softc->disk->d_hba_device = cpi.hba_device;
1582         softc->disk->d_hba_subvendor = cpi.hba_subvendor;
1583         softc->disk->d_hba_subdevice = cpi.hba_subdevice;
1584         disk_create(softc->disk, DISK_VERSION);
1585         mtx_lock(periph->sim->mtx);
1586
1587         /*
1588          * Add async callbacks for events of interest.
1589          * I don't bother checking if this fails as,
1590          * in most cases, the system will function just
1591          * fine without them and the only alternative
1592          * would be to not attach the device on failure.
1593          */
1594         xpt_register_async(AC_SENT_BDR | AC_BUS_RESET
1595                          | AC_LOST_DEVICE | AC_ADVINFO_CHANGED,
1596                            daasync, periph, periph->path);
1597
1598         /*
1599          * Emit an attribute changed notification just in case 
1600          * physical path information arrived before our async
1601          * event handler was registered, but after anyone attaching
1602          * to our disk device polled it.
1603          */
1604         disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
1605
1606         xpt_schedule(periph, CAM_PRIORITY_DEV);
1607
1608         return(CAM_REQ_CMP);
1609 }
1610
1611 static void
1612 dastart(struct cam_periph *periph, union ccb *start_ccb)
1613 {
1614         struct da_softc *softc;
1615
1616         softc = (struct da_softc *)periph->softc;
1617
1618         switch (softc->state) {
1619         case DA_STATE_NORMAL:
1620         {
1621                 /* Pull a buffer from the queue and get going on it */          
1622                 struct bio *bp;
1623
1624                 /*
1625                  * See if there is a buf with work for us to do..
1626                  */
1627                 bp = bioq_first(&softc->bio_queue);
1628                 if (periph->immediate_priority <= periph->pinfo.priority) {
1629                         CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1630                                         ("queuing for immediate ccb\n"));
1631                         start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1632                         SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1633                                           periph_links.sle);
1634                         periph->immediate_priority = CAM_PRIORITY_NONE;
1635                         wakeup(&periph->ccb_list);
1636                 } else if (bp == NULL) {
1637                         xpt_release_ccb(start_ccb);
1638                 } else {
1639                         u_int8_t tag_code;
1640
1641                         bioq_remove(&softc->bio_queue, bp);
1642
1643                         if ((bp->bio_flags & BIO_ORDERED) != 0
1644                          || (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1645                                 softc->flags &= ~DA_FLAG_NEED_OTAG;
1646                                 softc->ordered_tag_count++;
1647                                 tag_code = MSG_ORDERED_Q_TAG;
1648                         } else {
1649                                 tag_code = MSG_SIMPLE_Q_TAG;
1650                         }
1651                         switch (bp->bio_cmd) {
1652                         case BIO_READ:
1653                         case BIO_WRITE:
1654                                 scsi_read_write(&start_ccb->csio,
1655                                                 /*retries*/da_retry_count,
1656                                                 /*cbfcnp*/dadone,
1657                                                 /*tag_action*/tag_code,
1658                                                 /*read_op*/bp->bio_cmd
1659                                                         == BIO_READ,
1660                                                 /*byte2*/0,
1661                                                 softc->minimum_cmd_size,
1662                                                 /*lba*/bp->bio_pblkno,
1663                                                 /*block_count*/bp->bio_bcount /
1664                                                 softc->params.secsize,
1665                                                 /*data_ptr*/ bp->bio_data,
1666                                                 /*dxfer_len*/ bp->bio_bcount,
1667                                                 /*sense_len*/SSD_FULL_SIZE,
1668                                                 da_default_timeout * 1000);
1669                                 break;
1670                         case BIO_FLUSH:
1671                                 /*
1672                                  * BIO_FLUSH doesn't currently communicate
1673                                  * range data, so we synchronize the cache
1674                                  * over the whole disk.  We also force
1675                                  * ordered tag semantics the flush applies
1676                                  * to all previously queued I/O.
1677                                  */
1678                                 scsi_synchronize_cache(&start_ccb->csio,
1679                                                        /*retries*/1,
1680                                                        /*cbfcnp*/dadone,
1681                                                        MSG_ORDERED_Q_TAG,
1682                                                        /*begin_lba*/0,
1683                                                        /*lb_count*/0,
1684                                                        SSD_FULL_SIZE,
1685                                                        da_default_timeout*1000);
1686                                 break;
1687                         }
1688                         start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1689
1690                         /*
1691                          * Block out any asyncronous callbacks
1692                          * while we touch the pending ccb list.
1693                          */
1694                         LIST_INSERT_HEAD(&softc->pending_ccbs,
1695                                          &start_ccb->ccb_h, periph_links.le);
1696                         softc->outstanding_cmds++;
1697
1698                         /* We expect a unit attention from this device */
1699                         if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1700                                 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1701                                 softc->flags &= ~DA_FLAG_RETRY_UA;
1702                         }
1703
1704                         start_ccb->ccb_h.ccb_bp = bp;
1705                         bp = bioq_first(&softc->bio_queue);
1706
1707                         xpt_action(start_ccb);
1708                 }
1709                 
1710                 if (bp != NULL) {
1711                         /* Have more work to do, so ensure we stay scheduled */
1712                         xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1713                 }
1714                 break;
1715         }
1716         case DA_STATE_PROBE:
1717         {
1718                 struct ccb_scsiio *csio;
1719                 struct scsi_read_capacity_data *rcap;
1720
1721                 rcap = (struct scsi_read_capacity_data *)
1722                     malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
1723                 if (rcap == NULL) {
1724                         printf("dastart: Couldn't malloc read_capacity data\n");
1725                         /* da_free_periph??? */
1726                         break;
1727                 }
1728                 csio = &start_ccb->csio;
1729                 scsi_read_capacity(csio,
1730                                    /*retries*/4,
1731                                    dadone,
1732                                    MSG_SIMPLE_Q_TAG,
1733                                    rcap,
1734                                    SSD_FULL_SIZE,
1735                                    /*timeout*/5000);
1736                 start_ccb->ccb_h.ccb_bp = NULL;
1737                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1738                 xpt_action(start_ccb);
1739                 break;
1740         }
1741         case DA_STATE_PROBE2:
1742         {
1743                 struct ccb_scsiio *csio;
1744                 struct scsi_read_capacity_data_long *rcaplong;
1745
1746                 rcaplong = (struct scsi_read_capacity_data_long *)
1747                         malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
1748                 if (rcaplong == NULL) {
1749                         printf("dastart: Couldn't malloc read_capacity data\n");
1750                         /* da_free_periph??? */
1751                         break;
1752                 }
1753                 csio = &start_ccb->csio;
1754                 scsi_read_capacity_16(csio,
1755                                       /*retries*/ 4,
1756                                       /*cbfcnp*/ dadone,
1757                                       /*tag_action*/ MSG_SIMPLE_Q_TAG,
1758                                       /*lba*/ 0,
1759                                       /*reladr*/ 0,
1760                                       /*pmi*/ 0,
1761                                       rcaplong,
1762                                       /*sense_len*/ SSD_FULL_SIZE,
1763                                       /*timeout*/ 60000);
1764                 start_ccb->ccb_h.ccb_bp = NULL;
1765                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1766                 xpt_action(start_ccb);  
1767                 break;
1768         }
1769         }
1770 }
1771
1772 static int
1773 cmd6workaround(union ccb *ccb)
1774 {
1775         struct scsi_rw_6 cmd6;
1776         struct scsi_rw_10 *cmd10;
1777         struct da_softc *softc;
1778         u_int8_t *cdb;
1779         int frozen;
1780
1781         cdb = ccb->csio.cdb_io.cdb_bytes;
1782
1783         /* Translation only possible if CDB is an array and cmd is R/W6 */
1784         if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1785             (*cdb != READ_6 && *cdb != WRITE_6))
1786                 return 0;
1787
1788         xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
1789             "increasing minimum_cmd_size to 10.\n");
1790         softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1791         softc->minimum_cmd_size = 10;
1792
1793         bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1794         cmd10 = (struct scsi_rw_10 *)cdb;
1795         cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1796         cmd10->byte2 = 0;
1797         scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1798         cmd10->reserved = 0;
1799         scsi_ulto2b(cmd6.length, cmd10->length);
1800         cmd10->control = cmd6.control;
1801         ccb->csio.cdb_len = sizeof(*cmd10);
1802
1803         /* Requeue request, unfreezing queue if necessary */
1804         frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1805         ccb->ccb_h.status = CAM_REQUEUE_REQ;
1806         xpt_action(ccb);
1807         if (frozen) {
1808                 cam_release_devq(ccb->ccb_h.path,
1809                                  /*relsim_flags*/0,
1810                                  /*reduction*/0,
1811                                  /*timeout*/0,
1812                                  /*getcount_only*/0);
1813         }
1814         return (ERESTART);
1815 }
1816
1817 static void
1818 dadone(struct cam_periph *periph, union ccb *done_ccb)
1819 {
1820         struct da_softc *softc;
1821         struct ccb_scsiio *csio;
1822         u_int32_t  priority;
1823
1824         softc = (struct da_softc *)periph->softc;
1825         priority = done_ccb->ccb_h.pinfo.priority;
1826         csio = &done_ccb->csio;
1827         switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1828         case DA_CCB_BUFFER_IO:
1829         {
1830                 struct bio *bp;
1831
1832                 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1833                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1834                         int error;
1835                         int sf;
1836
1837                         if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1838                                 sf = SF_RETRY_UA;
1839                         else
1840                                 sf = 0;
1841
1842                         error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1843                         if (error == ERESTART) {
1844                                 /*
1845                                  * A retry was scheuled, so
1846                                  * just return.
1847                                  */
1848                                 return;
1849                         }
1850                         if (error != 0) {
1851                                 int queued_error;
1852
1853                                 /*
1854                                  * return all queued I/O with EIO, so that
1855                                  * the client can retry these I/Os in the
1856                                  * proper order should it attempt to recover.
1857                                  */
1858                                 queued_error = EIO;
1859
1860                                 if (error == ENXIO
1861                                  && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
1862                                         /*
1863                                          * Catastrophic error.  Mark our pack as
1864                                          * invalid.
1865                                          */
1866                                         /*
1867                                          * XXX See if this is really a media
1868                                          * XXX change first?
1869                                          */
1870                                         xpt_print(periph->path,
1871                                             "Invalidating pack\n");
1872                                         softc->flags |= DA_FLAG_PACK_INVALID;
1873                                         queued_error = ENXIO;
1874                                 }
1875                                 bioq_flush(&softc->bio_queue, NULL,
1876                                            queued_error);
1877                                 bp->bio_error = error;
1878                                 bp->bio_resid = bp->bio_bcount;
1879                                 bp->bio_flags |= BIO_ERROR;
1880                         } else {
1881                                 bp->bio_resid = csio->resid;
1882                                 bp->bio_error = 0;
1883                                 if (bp->bio_resid != 0)
1884                                         bp->bio_flags |= BIO_ERROR;
1885                         }
1886                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1887                                 cam_release_devq(done_ccb->ccb_h.path,
1888                                                  /*relsim_flags*/0,
1889                                                  /*reduction*/0,
1890                                                  /*timeout*/0,
1891                                                  /*getcount_only*/0);
1892                 } else {
1893                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1894                                 panic("REQ_CMP with QFRZN");
1895                         bp->bio_resid = csio->resid;
1896                         if (csio->resid > 0)
1897                                 bp->bio_flags |= BIO_ERROR;
1898                         if (softc->error_inject != 0) {
1899                                 bp->bio_error = softc->error_inject;
1900                                 bp->bio_resid = bp->bio_bcount;
1901                                 bp->bio_flags |= BIO_ERROR;
1902                                 softc->error_inject = 0;
1903                         }
1904
1905                 }
1906
1907                 /*
1908                  * Block out any asyncronous callbacks
1909                  * while we touch the pending ccb list.
1910                  */
1911                 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1912                 softc->outstanding_cmds--;
1913                 if (softc->outstanding_cmds == 0)
1914                         softc->flags |= DA_FLAG_WENT_IDLE;
1915
1916                 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1917                         xpt_print(periph->path, "oustanding %d\n",
1918                                   softc->outstanding_cmds);
1919                 }
1920
1921                 biodone(bp);
1922                 break;
1923         }
1924         case DA_CCB_PROBE:
1925         case DA_CCB_PROBE2:
1926         {
1927                 struct     scsi_read_capacity_data *rdcap;
1928                 struct     scsi_read_capacity_data_long *rcaplong;
1929                 char       announce_buf[80];
1930
1931                 rdcap = NULL;
1932                 rcaplong = NULL;
1933                 if (softc->state == DA_STATE_PROBE)
1934                         rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1935                 else
1936                         rcaplong = (struct scsi_read_capacity_data_long *)
1937                                 csio->data_ptr;
1938
1939                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1940                         struct disk_params *dp;
1941                         uint32_t block_size;
1942                         uint64_t maxsector;
1943
1944                         if (softc->state == DA_STATE_PROBE) {
1945                                 block_size = scsi_4btoul(rdcap->length);
1946                                 maxsector = scsi_4btoul(rdcap->addr);
1947
1948                                 /*
1949                                  * According to SBC-2, if the standard 10
1950                                  * byte READ CAPACITY command returns 2^32,
1951                                  * we should issue the 16 byte version of
1952                                  * the command, since the device in question
1953                                  * has more sectors than can be represented
1954                                  * with the short version of the command.
1955                                  */
1956                                 if (maxsector == 0xffffffff) {
1957                                         softc->state = DA_STATE_PROBE2;
1958                                         free(rdcap, M_SCSIDA);
1959                                         xpt_release_ccb(done_ccb);
1960                                         xpt_schedule(periph, priority);
1961                                         return;
1962                                 }
1963                         } else {
1964                                 block_size = scsi_4btoul(rcaplong->length);
1965                                 maxsector = scsi_8btou64(rcaplong->addr);
1966                         }
1967
1968                         /*
1969                          * Because GEOM code just will panic us if we
1970                          * give them an 'illegal' value we'll avoid that
1971                          * here.
1972                          */
1973                         if (block_size == 0 && maxsector == 0) {
1974                                 snprintf(announce_buf, sizeof(announce_buf),
1975                                         "0MB (no media?)");
1976                         } else if (block_size >= MAXPHYS || block_size == 0) {
1977                                 xpt_print(periph->path,
1978                                     "unsupportable block size %ju\n",
1979                                     (uintmax_t) block_size);
1980                                 announce_buf[0] = '\0';
1981                                 cam_periph_invalidate(periph);
1982                         } else {
1983                                 dasetgeom(periph, block_size, maxsector, 0, 0);
1984                                 dp = &softc->params;
1985                                 snprintf(announce_buf, sizeof(announce_buf),
1986                                         "%juMB (%ju %u byte sectors: %dH %dS/T "
1987                                         "%dC)", (uintmax_t)
1988                                         (((uintmax_t)dp->secsize *
1989                                         dp->sectors) / (1024*1024)),
1990                                         (uintmax_t)dp->sectors,
1991                                         dp->secsize, dp->heads,
1992                                         dp->secs_per_track, dp->cylinders);
1993                         }
1994                 } else {
1995                         int     error;
1996
1997                         announce_buf[0] = '\0';
1998
1999                         /*
2000                          * Retry any UNIT ATTENTION type errors.  They
2001                          * are expected at boot.
2002                          */
2003                         error = daerror(done_ccb, CAM_RETRY_SELTO,
2004                                         SF_RETRY_UA|SF_NO_PRINT);
2005                         if (error == ERESTART) {
2006                                 /*
2007                                  * A retry was scheuled, so
2008                                  * just return.
2009                                  */
2010                                 return;
2011                         } else if (error != 0) {
2012                                 struct scsi_sense_data *sense;
2013                                 int asc, ascq;
2014                                 int sense_key, error_code;
2015                                 int have_sense;
2016                                 cam_status status;
2017                                 struct ccb_getdev cgd;
2018
2019                                 /* Don't wedge this device's queue */
2020                                 status = done_ccb->ccb_h.status;
2021                                 if ((status & CAM_DEV_QFRZN) != 0)
2022                                         cam_release_devq(done_ccb->ccb_h.path,
2023                                                          /*relsim_flags*/0,
2024                                                          /*reduction*/0,
2025                                                          /*timeout*/0,
2026                                                          /*getcount_only*/0);
2027
2028
2029                                 xpt_setup_ccb(&cgd.ccb_h, 
2030                                               done_ccb->ccb_h.path,
2031                                               CAM_PRIORITY_NORMAL);
2032                                 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2033                                 xpt_action((union ccb *)&cgd);
2034
2035                                 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
2036                                  || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
2037                                  || ((status & CAM_AUTOSNS_VALID) == 0))
2038                                         have_sense = FALSE;
2039                                 else
2040                                         have_sense = TRUE;
2041
2042                                 if (have_sense) {
2043                                         sense = &csio->sense_data;
2044                                         scsi_extract_sense_len(sense,
2045                                             csio->sense_len - csio->sense_resid,
2046                                             &error_code, &sense_key, &asc,
2047                                             &ascq, /*show_errors*/ 1);
2048                                 }
2049                                 /*
2050                                  * Attach to anything that claims to be a
2051                                  * direct access or optical disk device,
2052                                  * as long as it doesn't return a "Logical
2053                                  * unit not supported" (0x25) error.
2054                                  */
2055                                 if ((have_sense) && (asc != 0x25)
2056                                  && (error_code == SSD_CURRENT_ERROR)) {
2057                                         const char *sense_key_desc;
2058                                         const char *asc_desc;
2059
2060                                         scsi_sense_desc(sense_key, asc, ascq,
2061                                                         &cgd.inq_data,
2062                                                         &sense_key_desc,
2063                                                         &asc_desc);
2064                                         snprintf(announce_buf,
2065                                             sizeof(announce_buf),
2066                                                 "Attempt to query device "
2067                                                 "size failed: %s, %s",
2068                                                 sense_key_desc,
2069                                                 asc_desc);
2070                                 } else { 
2071                                         if (have_sense)
2072                                                 scsi_sense_print(
2073                                                         &done_ccb->csio);
2074                                         else {
2075                                                 xpt_print(periph->path,
2076                                                     "got CAM status %#x\n",
2077                                                     done_ccb->ccb_h.status);
2078                                         }
2079
2080                                         xpt_print(periph->path, "fatal error, "
2081                                             "failed to attach to device\n");
2082
2083                                         /*
2084                                          * Free up resources.
2085                                          */
2086                                         cam_periph_invalidate(periph);
2087                                 } 
2088                         }
2089                 }
2090                 free(csio->data_ptr, M_SCSIDA);
2091                 if (announce_buf[0] != '\0') {
2092                         /*
2093                          * Create our sysctl variables, now that we know
2094                          * we have successfully attached.
2095                          */
2096                         /* increase the refcount */
2097                         if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
2098                                 taskqueue_enqueue(taskqueue_thread,
2099                                                   &softc->sysctl_task);
2100                                 xpt_announce_periph(periph, announce_buf);
2101                         } else {
2102                                 xpt_print(periph->path, "fatal error, "
2103                                     "could not acquire reference count\n");
2104                         }
2105                                 
2106                 }
2107                 softc->state = DA_STATE_NORMAL; 
2108                 /*
2109                  * Since our peripheral may be invalidated by an error
2110                  * above or an external event, we must release our CCB
2111                  * before releasing the probe lock on the peripheral.
2112                  * The peripheral will only go away once the last lock
2113                  * is removed, and we need it around for the CCB release
2114                  * operation.
2115                  */
2116                 xpt_release_ccb(done_ccb);
2117                 cam_periph_unhold(periph);
2118                 return;
2119         }
2120         case DA_CCB_WAITING:
2121         {
2122                 /* Caller will release the CCB */
2123                 wakeup(&done_ccb->ccb_h.cbfcnp);
2124                 return;
2125         }
2126         case DA_CCB_DUMP:
2127                 /* No-op.  We're polling */
2128                 return;
2129         default:
2130                 break;
2131         }
2132         xpt_release_ccb(done_ccb);
2133 }
2134
2135 static int
2136 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
2137 {
2138         struct da_softc   *softc;
2139         struct cam_periph *periph;
2140         int error;
2141
2142         periph = xpt_path_periph(ccb->ccb_h.path);
2143         softc = (struct da_softc *)periph->softc;
2144
2145         /*
2146          * Automatically detect devices that do not support
2147          * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
2148          */
2149         error = 0;
2150         if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
2151                 error = cmd6workaround(ccb);
2152         } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
2153                    CAM_SCSI_STATUS_ERROR)
2154          && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
2155          && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
2156          && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
2157          && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
2158                 int sense_key, error_code, asc, ascq;
2159
2160                 scsi_extract_sense(&ccb->csio.sense_data,
2161                                    &error_code, &sense_key, &asc, &ascq);
2162                 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
2163                         error = cmd6workaround(ccb);
2164         }
2165         if (error == ERESTART)
2166                 return (ERESTART);
2167
2168         /*
2169          * XXX
2170          * Until we have a better way of doing pack validation,
2171          * don't treat UAs as errors.
2172          */
2173         sense_flags |= SF_RETRY_UA;
2174         return(cam_periph_error(ccb, cam_flags, sense_flags,
2175                                 &softc->saved_ccb));
2176 }
2177
2178 static void
2179 daprevent(struct cam_periph *periph, int action)
2180 {
2181         struct  da_softc *softc;
2182         union   ccb *ccb;               
2183         int     error;
2184                 
2185         softc = (struct da_softc *)periph->softc;
2186
2187         if (((action == PR_ALLOW)
2188           && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
2189          || ((action == PR_PREVENT)
2190           && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
2191                 return;
2192         }
2193
2194         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2195
2196         scsi_prevent(&ccb->csio,
2197                      /*retries*/1,
2198                      /*cbcfp*/dadone,
2199                      MSG_SIMPLE_Q_TAG,
2200                      action,
2201                      SSD_FULL_SIZE,
2202                      5000);
2203
2204         error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
2205                                   SF_RETRY_UA, softc->disk->d_devstat);
2206
2207         if (error == 0) {
2208                 if (action == PR_ALLOW)
2209                         softc->flags &= ~DA_FLAG_PACK_LOCKED;
2210                 else
2211                         softc->flags |= DA_FLAG_PACK_LOCKED;
2212         }
2213
2214         xpt_release_ccb(ccb);
2215 }
2216
2217 static int
2218 dagetcapacity(struct cam_periph *periph)
2219 {
2220         struct da_softc *softc;
2221         union ccb *ccb;
2222         struct scsi_read_capacity_data *rcap;
2223         struct scsi_read_capacity_data_long *rcaplong;
2224         uint32_t block_len;
2225         uint64_t maxsector;
2226         int error;
2227         u_int32_t sense_flags;
2228
2229         softc = (struct da_softc *)periph->softc;
2230         block_len = 0;
2231         maxsector = 0;
2232         error = 0;
2233         sense_flags = SF_RETRY_UA;
2234         if (softc->flags & DA_FLAG_PACK_REMOVABLE)
2235                 sense_flags |= SF_NO_PRINT;
2236
2237         /* Do a read capacity */
2238         rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcaplong),
2239                                                         M_SCSIDA,
2240                                                         M_NOWAIT);
2241         if (rcap == NULL)
2242                 return (ENOMEM);
2243                 
2244         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2245         scsi_read_capacity(&ccb->csio,
2246                            /*retries*/4,
2247                            /*cbfncp*/dadone,
2248                            MSG_SIMPLE_Q_TAG,
2249                            rcap,
2250                            SSD_FULL_SIZE,
2251                            /*timeout*/60000);
2252         ccb->ccb_h.ccb_bp = NULL;
2253
2254         error = cam_periph_runccb(ccb, daerror,
2255                                   /*cam_flags*/CAM_RETRY_SELTO,
2256                                   sense_flags,
2257                                   softc->disk->d_devstat);
2258
2259         if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2260                 cam_release_devq(ccb->ccb_h.path,
2261                                  /*relsim_flags*/0,
2262                                  /*reduction*/0,
2263                                  /*timeout*/0,
2264                                  /*getcount_only*/0);
2265
2266         if (error == 0) {
2267                 block_len = scsi_4btoul(rcap->length);
2268                 maxsector = scsi_4btoul(rcap->addr);
2269
2270                 if (maxsector != 0xffffffff)
2271                         goto done;
2272         } else
2273                 goto done;
2274
2275         rcaplong = (struct scsi_read_capacity_data_long *)rcap;
2276
2277         scsi_read_capacity_16(&ccb->csio,
2278                               /*retries*/ 4,
2279                               /*cbfcnp*/ dadone,
2280                               /*tag_action*/ MSG_SIMPLE_Q_TAG,
2281                               /*lba*/ 0,
2282                               /*reladr*/ 0,
2283                               /*pmi*/ 0,
2284                               rcaplong,
2285                               /*sense_len*/ SSD_FULL_SIZE,
2286                               /*timeout*/ 60000);
2287         ccb->ccb_h.ccb_bp = NULL;
2288
2289         error = cam_periph_runccb(ccb, daerror,
2290                                   /*cam_flags*/CAM_RETRY_SELTO,
2291                                   sense_flags,
2292                                   softc->disk->d_devstat);
2293
2294         if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2295                 cam_release_devq(ccb->ccb_h.path,
2296                                  /*relsim_flags*/0,
2297                                  /*reduction*/0,
2298                                  /*timeout*/0,
2299                                  /*getcount_only*/0);
2300
2301         if (error == 0) {
2302                 block_len = scsi_4btoul(rcaplong->length);
2303                 maxsector = scsi_8btou64(rcaplong->addr);
2304         }
2305
2306 done:
2307
2308         if (error == 0) {
2309                 if (block_len >= MAXPHYS || block_len == 0) {
2310                         xpt_print(periph->path,
2311                             "unsupportable block size %ju\n",
2312                             (uintmax_t) block_len);
2313                         error = EINVAL;
2314                 } else
2315                         dasetgeom(periph, block_len, maxsector, 0, 0);
2316         }
2317
2318         xpt_release_ccb(ccb);
2319
2320         free(rcap, M_SCSIDA);
2321
2322         return (error);
2323 }
2324
2325 static void
2326 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
2327     u_int lbppbe, u_int lalba)
2328 {
2329         struct ccb_calc_geometry ccg;
2330         struct da_softc *softc;
2331         struct disk_params *dp;
2332
2333         softc = (struct da_softc *)periph->softc;
2334
2335         dp = &softc->params;
2336         dp->secsize = block_len;
2337         dp->sectors = maxsector + 1;
2338         if (lbppbe > 0) {
2339                 dp->stripesize = block_len << lbppbe;
2340                 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
2341                     dp->stripesize;
2342         } else if (softc->quirks & DA_Q_4K) {
2343                 dp->stripesize = 4096;
2344                 dp->stripeoffset = 0;
2345         } else {
2346                 dp->stripesize = 0;
2347                 dp->stripeoffset = 0;
2348         }
2349         /*
2350          * Have the controller provide us with a geometry
2351          * for this disk.  The only time the geometry
2352          * matters is when we boot and the controller
2353          * is the only one knowledgeable enough to come
2354          * up with something that will make this a bootable
2355          * device.
2356          */
2357         xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2358         ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
2359         ccg.block_size = dp->secsize;
2360         ccg.volume_size = dp->sectors;
2361         ccg.heads = 0;
2362         ccg.secs_per_track = 0;
2363         ccg.cylinders = 0;
2364         xpt_action((union ccb*)&ccg);
2365         if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2366                 /*
2367                  * We don't know what went wrong here- but just pick
2368                  * a geometry so we don't have nasty things like divide
2369                  * by zero.
2370                  */
2371                 dp->heads = 255;
2372                 dp->secs_per_track = 255;
2373                 dp->cylinders = dp->sectors / (255 * 255);
2374                 if (dp->cylinders == 0) {
2375                         dp->cylinders = 1;
2376                 }
2377         } else {
2378                 dp->heads = ccg.heads;
2379                 dp->secs_per_track = ccg.secs_per_track;
2380                 dp->cylinders = ccg.cylinders;
2381         }
2382 }
2383
2384 static void
2385 dasendorderedtag(void *arg)
2386 {
2387         struct da_softc *softc = arg;
2388
2389         if (da_send_ordered) {
2390                 if ((softc->ordered_tag_count == 0) 
2391                  && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
2392                         softc->flags |= DA_FLAG_NEED_OTAG;
2393                 }
2394                 if (softc->outstanding_cmds > 0)
2395                         softc->flags &= ~DA_FLAG_WENT_IDLE;
2396
2397                 softc->ordered_tag_count = 0;
2398         }
2399         /* Queue us up again */
2400         callout_reset(&softc->sendordered_c,
2401             (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
2402             dasendorderedtag, softc);
2403 }
2404
2405 /*
2406  * Step through all DA peripheral drivers, and if the device is still open,
2407  * sync the disk cache to physical media.
2408  */
2409 static void
2410 dashutdown(void * arg, int howto)
2411 {
2412         struct cam_periph *periph;
2413         struct da_softc *softc;
2414
2415         TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
2416                 union ccb ccb;
2417
2418                 cam_periph_lock(periph);
2419                 softc = (struct da_softc *)periph->softc;
2420
2421                 /*
2422                  * We only sync the cache if the drive is still open, and
2423                  * if the drive is capable of it..
2424                  */
2425                 if (((softc->flags & DA_FLAG_OPEN) == 0)
2426                  || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
2427                         cam_periph_unlock(periph);
2428                         continue;
2429                 }
2430
2431                 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2432
2433                 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
2434                 scsi_synchronize_cache(&ccb.csio,
2435                                        /*retries*/1,
2436                                        /*cbfcnp*/dadone,
2437                                        MSG_SIMPLE_Q_TAG,
2438                                        /*begin_lba*/0, /* whole disk */
2439                                        /*lb_count*/0,
2440                                        SSD_FULL_SIZE,
2441                                        60 * 60 * 1000);
2442
2443                 xpt_polled_action(&ccb);
2444
2445                 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2446                         if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2447                              CAM_SCSI_STATUS_ERROR)
2448                          && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
2449                                 int error_code, sense_key, asc, ascq;
2450
2451                                 scsi_extract_sense(&ccb.csio.sense_data,
2452                                                    &error_code, &sense_key,
2453                                                    &asc, &ascq);
2454
2455                                 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
2456                                         scsi_sense_print(&ccb.csio);
2457                         } else {
2458                                 xpt_print(periph->path, "Synchronize "
2459                                     "cache failed, status == 0x%x, scsi status "
2460                                     "== 0x%x\n", ccb.ccb_h.status,
2461                                     ccb.csio.scsi_status);
2462                         }
2463                 }
2464
2465                 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
2466                         cam_release_devq(ccb.ccb_h.path,
2467                                          /*relsim_flags*/0,
2468                                          /*reduction*/0,
2469                                          /*timeout*/0,
2470                                          /*getcount_only*/0);
2471                 cam_periph_unlock(periph);
2472         }
2473 }
2474
2475 #else /* !_KERNEL */
2476
2477 /*
2478  * XXX This is only left out of the kernel build to silence warnings.  If,
2479  * for some reason this function is used in the kernel, the ifdefs should
2480  * be moved so it is included both in the kernel and userland.
2481  */
2482 void
2483 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2484                  void (*cbfcnp)(struct cam_periph *, union ccb *),
2485                  u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2486                  u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2487                  u_int32_t timeout)
2488 {
2489         struct scsi_format_unit *scsi_cmd;
2490
2491         scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2492         scsi_cmd->opcode = FORMAT_UNIT;
2493         scsi_cmd->byte2 = byte2;
2494         scsi_ulto2b(ileave, scsi_cmd->interleave);
2495
2496         cam_fill_csio(csio,
2497                       retries,
2498                       cbfcnp,
2499                       /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2500                       tag_action,
2501                       data_ptr,
2502                       dxfer_len,
2503                       sense_len,
2504                       sizeof(*scsi_cmd),
2505                       timeout);
2506 }
2507
2508 #endif /* _KERNEL */