]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/cam/scsi/scsi_da.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.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 <sys/endian.h>
48 #include <sys/proc.h>
49 #include <geom/geom.h>
50 #include <geom/geom_disk.h>
51 #endif /* _KERNEL */
52
53 #ifndef _KERNEL
54 #include <stdio.h>
55 #include <string.h>
56 #endif /* _KERNEL */
57
58 #include <cam/cam.h>
59 #include <cam/cam_ccb.h>
60 #include <cam/cam_periph.h>
61 #include <cam/cam_xpt_periph.h>
62 #include <cam/cam_sim.h>
63
64 #include <cam/scsi/scsi_message.h>
65
66 #ifndef _KERNEL 
67 #include <cam/scsi/scsi_da.h>
68 #endif /* !_KERNEL */
69
70 #ifdef _KERNEL
71 typedef enum {
72         DA_STATE_PROBE_RC,
73         DA_STATE_PROBE_RC16,
74         DA_STATE_PROBE_LBP,
75         DA_STATE_PROBE_BLK_LIMITS,
76         DA_STATE_PROBE_BDC,
77         DA_STATE_PROBE_ATA,
78         DA_STATE_NORMAL
79 } da_state;
80
81 typedef enum {
82         DA_FLAG_PACK_INVALID    = 0x001,
83         DA_FLAG_NEW_PACK        = 0x002,
84         DA_FLAG_PACK_LOCKED     = 0x004,
85         DA_FLAG_PACK_REMOVABLE  = 0x008,
86         DA_FLAG_NEED_OTAG       = 0x020,
87         DA_FLAG_WENT_IDLE       = 0x040,
88         DA_FLAG_RETRY_UA        = 0x080,
89         DA_FLAG_OPEN            = 0x100,
90         DA_FLAG_SCTX_INIT       = 0x200,
91         DA_FLAG_CAN_RC16        = 0x400,
92         DA_FLAG_PROBED          = 0x800,
93         DA_FLAG_DIRTY           = 0x1000
94 } da_flags;
95
96 typedef enum {
97         DA_Q_NONE               = 0x00,
98         DA_Q_NO_SYNC_CACHE      = 0x01,
99         DA_Q_NO_6_BYTE          = 0x02,
100         DA_Q_NO_PREVENT         = 0x04,
101         DA_Q_4K                 = 0x08,
102         DA_Q_NO_RC16            = 0x10
103 } da_quirks;
104
105 #define DA_Q_BIT_STRING         \
106         "\020"                  \
107         "\001NO_SYNC_CACHE"     \
108         "\002NO_6_BYTE"         \
109         "\003NO_PREVENT"        \
110         "\0044K"                \
111         "\005NO_RC16"
112
113 typedef enum {
114         DA_CCB_PROBE_RC         = 0x01,
115         DA_CCB_PROBE_RC16       = 0x02,
116         DA_CCB_PROBE_LBP        = 0x03,
117         DA_CCB_PROBE_BLK_LIMITS = 0x04,
118         DA_CCB_PROBE_BDC        = 0x05,
119         DA_CCB_PROBE_ATA        = 0x06,
120         DA_CCB_BUFFER_IO        = 0x07,
121         DA_CCB_WAITING          = 0x08,
122         DA_CCB_DUMP             = 0x0A,
123         DA_CCB_DELETE           = 0x0B,
124         DA_CCB_TUR              = 0x0C,
125         DA_CCB_TYPE_MASK        = 0x0F,
126         DA_CCB_RETRY_UA         = 0x10
127 } da_ccb_state;
128
129 /*
130  * Order here is important for method choice
131  *
132  * We prefer ATA_TRIM as tests run against a Sandforce 2281 SSD attached to
133  * LSI 2008 (mps) controller (FW: v12, Drv: v14) resulted 20% quicker deletes
134  * using ATA_TRIM than the corresponding UNMAP results for a real world mysql
135  * import taking 5mins.
136  *
137  */
138 typedef enum {
139         DA_DELETE_NONE,
140         DA_DELETE_DISABLE,
141         DA_DELETE_ATA_TRIM,
142         DA_DELETE_UNMAP,
143         DA_DELETE_WS16,
144         DA_DELETE_WS10,
145         DA_DELETE_ZERO,
146         DA_DELETE_MIN = DA_DELETE_ATA_TRIM,
147         DA_DELETE_MAX = DA_DELETE_ZERO
148 } da_delete_methods;
149
150 typedef void da_delete_func_t (struct cam_periph *periph, union ccb *ccb,
151                               struct bio *bp);
152 static da_delete_func_t da_delete_trim;
153 static da_delete_func_t da_delete_unmap;
154 static da_delete_func_t da_delete_ws;
155
156 static const void * da_delete_functions[] = {
157         NULL,
158         NULL,
159         da_delete_trim,
160         da_delete_unmap,
161         da_delete_ws,
162         da_delete_ws,
163         da_delete_ws
164 };
165
166 static const char *da_delete_method_names[] =
167     { "NONE", "DISABLE", "ATA_TRIM", "UNMAP", "WS16", "WS10", "ZERO" };
168 static const char *da_delete_method_desc[] =
169     { "NONE", "DISABLED", "ATA TRIM", "UNMAP", "WRITE SAME(16) with UNMAP",
170       "WRITE SAME(10) with UNMAP", "ZERO" };
171
172 /* Offsets into our private area for storing information */
173 #define ccb_state       ppriv_field0
174 #define ccb_bp          ppriv_ptr1
175
176 struct disk_params {
177         u_int8_t  heads;
178         u_int32_t cylinders;
179         u_int8_t  secs_per_track;
180         u_int32_t secsize;      /* Number of bytes/sector */
181         u_int64_t sectors;      /* total number sectors */
182         u_int     stripesize;
183         u_int     stripeoffset;
184 };
185
186 #define UNMAP_RANGE_MAX         0xffffffff
187 #define UNMAP_HEAD_SIZE         8
188 #define UNMAP_RANGE_SIZE        16
189 #define UNMAP_MAX_RANGES        2048 /* Protocol Max is 4095 */
190 #define UNMAP_BUF_SIZE          ((UNMAP_MAX_RANGES * UNMAP_RANGE_SIZE) + \
191                                 UNMAP_HEAD_SIZE)
192
193 #define WS10_MAX_BLKS           0xffff
194 #define WS16_MAX_BLKS           0xffffffff
195 #define ATA_TRIM_MAX_RANGES     ((UNMAP_BUF_SIZE / \
196         (ATA_DSM_RANGE_SIZE * ATA_DSM_BLK_SIZE)) * ATA_DSM_BLK_SIZE)
197
198 struct da_softc {
199         struct   bio_queue_head bio_queue;
200         struct   bio_queue_head delete_queue;
201         struct   bio_queue_head delete_run_queue;
202         SLIST_ENTRY(da_softc) links;
203         LIST_HEAD(, ccb_hdr) pending_ccbs;
204         da_state state;
205         da_flags flags; 
206         da_quirks quirks;
207         int      sort_io_queue;
208         int      minimum_cmd_size;
209         int      error_inject;
210         int      ordered_tag_count;
211         int      outstanding_cmds;
212         int      trim_max_ranges;
213         int      delete_running;
214         int      tur;
215         int      delete_available;      /* Delete methods possibly available */
216         uint32_t                unmap_max_ranges;
217         uint32_t                unmap_max_lba;
218         uint64_t                ws_max_blks;
219         da_delete_methods       delete_method;
220         da_delete_func_t        *delete_func;
221         struct   disk_params params;
222         struct   disk *disk;
223         union    ccb saved_ccb;
224         struct task             sysctl_task;
225         struct sysctl_ctx_list  sysctl_ctx;
226         struct sysctl_oid       *sysctl_tree;
227         struct callout          sendordered_c;
228         uint64_t wwpn;
229         uint8_t  unmap_buf[UNMAP_BUF_SIZE];
230         struct scsi_read_capacity_data_long rcaplong;
231         struct callout          mediapoll_c;
232 };
233
234 #define dadeleteflag(softc, delete_method, enable)                      \
235         if (enable) {                                                   \
236                 softc->delete_available |= (1 << delete_method);        \
237         } else {                                                        \
238                 softc->delete_available &= ~(1 << delete_method);       \
239         }
240
241 struct da_quirk_entry {
242         struct scsi_inquiry_pattern inq_pat;
243         da_quirks quirks;
244 };
245
246 static const char quantum[] = "QUANTUM";
247 static const char microp[] = "MICROP";
248
249 static struct da_quirk_entry da_quirk_table[] =
250 {
251         /* SPI, FC devices */
252         {
253                 /*
254                  * Fujitsu M2513A MO drives.
255                  * Tested devices: M2513A2 firmware versions 1200 & 1300.
256                  * (dip switch selects whether T_DIRECT or T_OPTICAL device)
257                  * Reported by: W.Scholten <whs@xs4all.nl>
258                  */
259                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
260                 /*quirks*/ DA_Q_NO_SYNC_CACHE
261         },
262         {
263                 /* See above. */
264                 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
265                 /*quirks*/ DA_Q_NO_SYNC_CACHE
266         },
267         {
268                 /*
269                  * This particular Fujitsu drive doesn't like the
270                  * synchronize cache command.
271                  * Reported by: Tom Jackson <toj@gorilla.net>
272                  */
273                 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
274                 /*quirks*/ DA_Q_NO_SYNC_CACHE
275         },
276         {
277                 /*
278                  * This drive doesn't like the synchronize cache command
279                  * either.  Reported by: Matthew Jacob <mjacob@feral.com>
280                  * in NetBSD PR kern/6027, August 24, 1998.
281                  */
282                 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
283                 /*quirks*/ DA_Q_NO_SYNC_CACHE
284         },
285         {
286                 /*
287                  * This drive doesn't like the synchronize cache command
288                  * either.  Reported by: Hellmuth Michaelis (hm@kts.org)
289                  * (PR 8882).
290                  */
291                 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
292                 /*quirks*/ DA_Q_NO_SYNC_CACHE
293         },
294         {
295                 /*
296                  * Doesn't like the synchronize cache command.
297                  * Reported by: Blaz Zupan <blaz@gold.amis.net>
298                  */
299                 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
300                 /*quirks*/ DA_Q_NO_SYNC_CACHE
301         },
302         {
303                 /*
304                  * Doesn't like the synchronize cache command.
305                  * Reported by: Blaz Zupan <blaz@gold.amis.net>
306                  */
307                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
308                 /*quirks*/ DA_Q_NO_SYNC_CACHE
309         },
310         {
311                 /*
312                  * Doesn't like the synchronize cache command.
313                  */
314                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
315                 /*quirks*/ DA_Q_NO_SYNC_CACHE
316         },
317         {
318                 /*
319                  * Doesn't like the synchronize cache command.
320                  * Reported by: walter@pelissero.de
321                  */
322                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
323                 /*quirks*/ DA_Q_NO_SYNC_CACHE
324         },
325         {
326                 /*
327                  * Doesn't work correctly with 6 byte reads/writes.
328                  * Returns illegal request, and points to byte 9 of the
329                  * 6-byte CDB.
330                  * Reported by:  Adam McDougall <bsdx@spawnet.com>
331                  */
332                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
333                 /*quirks*/ DA_Q_NO_6_BYTE
334         },
335         {
336                 /* See above. */
337                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
338                 /*quirks*/ DA_Q_NO_6_BYTE
339         },
340         {
341                 /*
342                  * Doesn't like the synchronize cache command.
343                  * Reported by: walter@pelissero.de
344                  */
345                 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
346                 /*quirks*/ DA_Q_NO_SYNC_CACHE
347         },
348         {
349                 /*
350                  * The CISS RAID controllers do not support SYNC_CACHE
351                  */
352                 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
353                 /*quirks*/ DA_Q_NO_SYNC_CACHE
354         },
355         /* USB mass storage devices supported by umass(4) */
356         {
357                 /*
358                  * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
359                  * PR: kern/51675
360                  */
361                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
362                 /*quirks*/ DA_Q_NO_SYNC_CACHE
363         },
364         {
365                 /*
366                  * Power Quotient Int. (PQI) USB flash key
367                  * PR: kern/53067
368                  */
369                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
370                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
371         },
372         {
373                 /*
374                  * Creative Nomad MUVO mp3 player (USB)
375                  * PR: kern/53094
376                  */
377                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
378                 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
379         },
380         {
381                 /*
382                  * Jungsoft NEXDISK USB flash key
383                  * PR: kern/54737
384                  */
385                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
386                 /*quirks*/ DA_Q_NO_SYNC_CACHE
387         },
388         {
389                 /*
390                  * FreeDik USB Mini Data Drive
391                  * PR: kern/54786
392                  */
393                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
394                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
395         },
396         {
397                 /*
398                  * Sigmatel USB Flash MP3 Player
399                  * PR: kern/57046
400                  */
401                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
402                 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
403         },
404         {
405                 /*
406                  * Neuros USB Digital Audio Computer
407                  * PR: kern/63645
408                  */
409                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
410                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
411         },
412         {
413                 /*
414                  * SEAGRAND NP-900 MP3 Player
415                  * PR: kern/64563
416                  */
417                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
418                 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
419         },
420         {
421                 /*
422                  * iRiver iFP MP3 player (with UMS Firmware)
423                  * PR: kern/54881, i386/63941, kern/66124
424                  */
425                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
426                 /*quirks*/ DA_Q_NO_SYNC_CACHE
427         },
428         {
429                 /*
430                  * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
431                  * PR: kern/70158
432                  */
433                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
434                 /*quirks*/ DA_Q_NO_SYNC_CACHE
435         },
436         {
437                 /*
438                  * ZICPlay USB MP3 Player with FM
439                  * PR: kern/75057
440                  */
441                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
442                 /*quirks*/ DA_Q_NO_SYNC_CACHE
443         },
444         {
445                 /*
446                  * TEAC USB floppy mechanisms
447                  */
448                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
449                 /*quirks*/ DA_Q_NO_SYNC_CACHE
450         },
451         {
452                 /*
453                  * Kingston DataTraveler II+ USB Pen-Drive.
454                  * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
455                  */
456                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
457                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
458         },
459         {
460                 /*
461                  * USB DISK Pro PMAP
462                  * Reported by: jhs
463                  * PR: usb/96381
464                  */
465                 {T_DIRECT, SIP_MEDIA_REMOVABLE, " ", "USB DISK Pro", "PMAP"},
466                 /*quirks*/ DA_Q_NO_SYNC_CACHE
467         },
468         {
469                 /*
470                  * Motorola E398 Mobile Phone (TransFlash memory card).
471                  * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
472                  * PR: usb/89889
473                  */
474                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
475                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
476         },
477         {
478                 /*
479                  * Qware BeatZkey! Pro
480                  * PR: usb/79164
481                  */
482                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
483                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
484         },
485         {
486                 /*
487                  * Time DPA20B 1GB MP3 Player
488                  * PR: usb/81846
489                  */
490                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
491                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
492         },
493         {
494                 /*
495                  * Samsung USB key 128Mb
496                  * PR: usb/90081
497                  */
498                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
499                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
500         },
501         {
502                 /*
503                  * Kingston DataTraveler 2.0 USB Flash memory.
504                  * PR: usb/89196
505                  */
506                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
507                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
508         },
509         {
510                 /*
511                  * Creative MUVO Slim mp3 player (USB)
512                  * PR: usb/86131
513                  */
514                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
515                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
516                 },
517         {
518                 /*
519                  * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
520                  * PR: usb/80487
521                  */
522                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
523                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
524         },
525         {
526                 /*
527                  * SanDisk Micro Cruzer 128MB
528                  * PR: usb/75970
529                  */
530                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
531                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
532         },
533         {
534                 /*
535                  * TOSHIBA TransMemory USB sticks
536                  * PR: kern/94660
537                  */
538                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
539                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
540         },
541         {
542                 /*
543                  * PNY USB Flash keys
544                  * PR: usb/75578, usb/72344, usb/65436 
545                  */
546                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
547                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
548         },
549         {
550                 /*
551                  * Genesys 6-in-1 Card Reader
552                  * PR: usb/94647
553                  */
554                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
555                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
556         },
557         {
558                 /*
559                  * Rekam Digital CAMERA
560                  * PR: usb/98713
561                  */
562                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
563                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
564         },
565         {
566                 /*
567                  * iRiver H10 MP3 player
568                  * PR: usb/102547
569                  */
570                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
571                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
572         },
573         {
574                 /*
575                  * iRiver U10 MP3 player
576                  * PR: usb/92306
577                  */
578                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
579                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
580         },
581         {
582                 /*
583                  * X-Micro Flash Disk
584                  * PR: usb/96901
585                  */
586                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
587                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
588         },
589         {
590                 /*
591                  * EasyMP3 EM732X USB 2.0 Flash MP3 Player
592                  * PR: usb/96546
593                  */
594                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
595                 "1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
596         },
597         {
598                 /*
599                  * Denver MP3 player
600                  * PR: usb/107101
601                  */
602                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
603                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
604         },
605         {
606                 /*
607                  * Philips USB Key Audio KEY013
608                  * PR: usb/68412
609                  */
610                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
611                 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
612         },
613         {
614                 /*
615                  * JNC MP3 Player
616                  * PR: usb/94439
617                  */
618                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
619                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
620         },
621         {
622                 /*
623                  * SAMSUNG MP0402H
624                  * PR: usb/108427
625                  */
626                 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
627                 /*quirks*/ DA_Q_NO_SYNC_CACHE
628         },
629         {
630                 /*
631                  * I/O Magic USB flash - Giga Bank
632                  * PR: usb/108810
633                  */
634                 {T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
635                 /*quirks*/ DA_Q_NO_SYNC_CACHE
636         },
637         {
638                 /*
639                  * JoyFly 128mb USB Flash Drive
640                  * PR: 96133
641                  */
642                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
643                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
644         },
645         {
646                 /*
647                  * ChipsBnk usb stick
648                  * PR: 103702
649                  */
650                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
651                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
652         },
653         {
654                 /*
655                  * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A
656                  * PR: 129858
657                  */
658                 {T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*",
659                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
660         },
661         {
662                 /*
663                  * Samsung YP-U3 mp3-player
664                  * PR: 125398
665                  */
666                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3",
667                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
668         },
669         {
670                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
671                  "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
672         },
673         {
674                 /*
675                  * Sony Cyber-Shot DSC cameras
676                  * PR: usb/137035
677                  */
678                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
679                 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
680         },
681         {
682                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler G3",
683                  "1.00"}, /*quirks*/ DA_Q_NO_PREVENT
684         },
685         {
686                 /* At least several Transcent USB sticks lie on RC16. */
687                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JetFlash", "Transcend*",
688                  "*"}, /*quirks*/ DA_Q_NO_RC16
689         },
690         /* ATA/SATA devices over SAS/USB/... */
691         {
692                 /* Hitachi Advanced Format (4k) drives */
693                 { T_DIRECT, SIP_MEDIA_FIXED, "Hitachi", "H??????????E3*", "*" },
694                 /*quirks*/DA_Q_4K
695         },
696         {
697                 /* Samsung Advanced Format (4k) drives */
698                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD155UI*", "*" },
699                 /*quirks*/DA_Q_4K
700         },
701         {
702                 /* Samsung Advanced Format (4k) drives */
703                 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD155UI*", "*" },
704                 /*quirks*/DA_Q_4K
705         },
706         {
707                 /* Samsung Advanced Format (4k) drives */
708                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD204UI*", "*" },
709                 /*quirks*/DA_Q_4K
710         },
711         {
712                 /* Samsung Advanced Format (4k) drives */
713                 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD204UI*", "*" },
714                 /*quirks*/DA_Q_4K
715         },
716         {
717                 /* Seagate Barracuda Green Advanced Format (4k) drives */
718                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DL*", "*" },
719                 /*quirks*/DA_Q_4K
720         },
721         {
722                 /* Seagate Barracuda Green Advanced Format (4k) drives */
723                 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DL", "*", "*" },
724                 /*quirks*/DA_Q_4K
725         },
726         {
727                 /* Seagate Barracuda Green Advanced Format (4k) drives */
728                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???DM*", "*" },
729                 /*quirks*/DA_Q_4K
730         },
731         {
732                 /* Seagate Barracuda Green Advanced Format (4k) drives */
733                 { T_DIRECT, SIP_MEDIA_FIXED, "ST???DM*", "*", "*" },
734                 /*quirks*/DA_Q_4K
735         },
736         {
737                 /* Seagate Barracuda Green Advanced Format (4k) drives */
738                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DM*", "*" },
739                 /*quirks*/DA_Q_4K
740         },
741         {
742                 /* Seagate Barracuda Green Advanced Format (4k) drives */
743                 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DM", "*", "*" },
744                 /*quirks*/DA_Q_4K
745         },
746         {
747                 /* Seagate Momentus Advanced Format (4k) drives */
748                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500423AS*", "*" },
749                 /*quirks*/DA_Q_4K
750         },
751         {
752                 /* Seagate Momentus Advanced Format (4k) drives */
753                 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "3AS*", "*" },
754                 /*quirks*/DA_Q_4K
755         },
756         {
757                 /* Seagate Momentus Advanced Format (4k) drives */
758                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500424AS*", "*" },
759                 /*quirks*/DA_Q_4K
760         },
761         {
762                 /* Seagate Momentus Advanced Format (4k) drives */
763                 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "4AS*", "*" },
764                 /*quirks*/DA_Q_4K
765         },
766         {
767                 /* Seagate Momentus Advanced Format (4k) drives */
768                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640423AS*", "*" },
769                 /*quirks*/DA_Q_4K
770         },
771         {
772                 /* Seagate Momentus Advanced Format (4k) drives */
773                 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "3AS*", "*" },
774                 /*quirks*/DA_Q_4K
775         },
776         {
777                 /* Seagate Momentus Advanced Format (4k) drives */
778                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640424AS*", "*" },
779                 /*quirks*/DA_Q_4K
780         },
781         {
782                 /* Seagate Momentus Advanced Format (4k) drives */
783                 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "4AS*", "*" },
784                 /*quirks*/DA_Q_4K
785         },
786         {
787                 /* Seagate Momentus Advanced Format (4k) drives */
788                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750420AS*", "*" },
789                 /*quirks*/DA_Q_4K
790         },
791         {
792                 /* Seagate Momentus Advanced Format (4k) drives */
793                 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "0AS*", "*" },
794                 /*quirks*/DA_Q_4K
795         },
796         {
797                 /* Seagate Momentus Advanced Format (4k) drives */
798                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750422AS*", "*" },
799                 /*quirks*/DA_Q_4K
800         },
801         {
802                 /* Seagate Momentus Advanced Format (4k) drives */
803                 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "2AS*", "*" },
804                 /*quirks*/DA_Q_4K
805         },
806         {
807                 /* Seagate Momentus Advanced Format (4k) drives */
808                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750423AS*", "*" },
809                 /*quirks*/DA_Q_4K
810         },
811         {
812                 /* Seagate Momentus Advanced Format (4k) drives */
813                 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "3AS*", "*" },
814                 /*quirks*/DA_Q_4K
815         },
816         {
817                 /* Seagate Momentus Thin Advanced Format (4k) drives */
818                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???LT*", "*" },
819                 /*quirks*/DA_Q_4K
820         },
821         {
822                 /* Seagate Momentus Thin Advanced Format (4k) drives */
823                 { T_DIRECT, SIP_MEDIA_FIXED, "ST???LT*", "*", "*" },
824                 /*quirks*/DA_Q_4K
825         },
826         {
827                 /* WDC Caviar Green Advanced Format (4k) drives */
828                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RS*", "*" },
829                 /*quirks*/DA_Q_4K
830         },
831         {
832                 /* WDC Caviar Green Advanced Format (4k) drives */
833                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RS*", "*" },
834                 /*quirks*/DA_Q_4K
835         },
836         {
837                 /* WDC Caviar Green Advanced Format (4k) drives */
838                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RX*", "*" },
839                 /*quirks*/DA_Q_4K
840         },
841         {
842                 /* WDC Caviar Green Advanced Format (4k) drives */
843                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RX*", "*" },
844                 /*quirks*/DA_Q_4K
845         },
846         {
847                 /* WDC Caviar Green Advanced Format (4k) drives */
848                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RS*", "*" },
849                 /*quirks*/DA_Q_4K
850         },
851         {
852                 /* WDC Caviar Green Advanced Format (4k) drives */
853                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RS*", "*" },
854                 /*quirks*/DA_Q_4K
855         },
856         {
857                 /* WDC Caviar Green Advanced Format (4k) drives */
858                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RX*", "*" },
859                 /*quirks*/DA_Q_4K
860         },
861         {
862                 /* WDC Caviar Green Advanced Format (4k) drives */
863                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RX*", "*" },
864                 /*quirks*/DA_Q_4K
865         },
866         {
867                 /* WDC Scorpio Black Advanced Format (4k) drives */
868                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PKT*", "*" },
869                 /*quirks*/DA_Q_4K
870         },
871         {
872                 /* WDC Scorpio Black Advanced Format (4k) drives */
873                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PKT*", "*" },
874                 /*quirks*/DA_Q_4K
875         },
876         {
877                 /* WDC Scorpio Black Advanced Format (4k) drives */
878                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PKT*", "*" },
879                 /*quirks*/DA_Q_4K
880         },
881         {
882                 /* WDC Scorpio Black Advanced Format (4k) drives */
883                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PKT*", "*" },
884                 /*quirks*/DA_Q_4K
885         },
886         {
887                 /* WDC Scorpio Blue Advanced Format (4k) drives */
888                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PVT*", "*" },
889                 /*quirks*/DA_Q_4K
890         },
891         {
892                 /* WDC Scorpio Blue Advanced Format (4k) drives */
893                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PVT*", "*" },
894                 /*quirks*/DA_Q_4K
895         },
896         {
897                 /* WDC Scorpio Blue Advanced Format (4k) drives */
898                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PVT*", "*" },
899                 /*quirks*/DA_Q_4K
900         },
901         {
902                 /* WDC Scorpio Blue Advanced Format (4k) drives */
903                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PVT*", "*" },
904                 /*quirks*/DA_Q_4K
905         },
906         {
907                 /*
908                  * Olympus FE-210 camera
909                  */
910                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "FE210*",
911                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
912         },
913         {
914                 /*
915                  * LG UP3S MP3 player
916                  */
917                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "LG", "UP3S",
918                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
919         },
920         {
921                 /*
922                  * Laser MP3-2GA13 MP3 player
923                  */
924                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "(HS) Flash Disk",
925                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
926         },
927         {
928                 /*
929                  * LaCie external 250GB Hard drive des by Porsche
930                  * Submitted by: Ben Stuyts <ben@altesco.nl>
931                  * PR: 121474
932                  */
933                 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HM250JI", "*"},
934                 /*quirks*/ DA_Q_NO_SYNC_CACHE
935         },
936         /* SATA SSDs */
937         {
938                 /*
939                  * Corsair Force 2 SSDs
940                  * 4k optimised & trim only works in 4k requests + 4k aligned
941                  */
942                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair CSSD-F*", "*" },
943                 /*quirks*/DA_Q_4K
944         },
945         {
946                 /*
947                  * Corsair Force 3 SSDs
948                  * 4k optimised & trim only works in 4k requests + 4k aligned
949                  */
950                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force 3*", "*" },
951                 /*quirks*/DA_Q_4K
952         },
953         {
954                 /*
955                  * Corsair Force GT SSDs
956                  * 4k optimised & trim only works in 4k requests + 4k aligned
957                  */
958                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force GT*", "*" },
959                 /*quirks*/DA_Q_4K
960         },
961         {
962                 /*
963                  * Crucial M4 SSDs
964                  * 4k optimised & trim only works in 4k requests + 4k aligned
965                  */
966                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "M4-CT???M4SSD2*", "*" },
967                 /*quirks*/DA_Q_4K
968         },
969         {
970                 /*
971                  * Crucial RealSSD C300 SSDs
972                  * 4k optimised
973                  */
974                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "C300-CTFDDAC???MAG*",
975                 "*" }, /*quirks*/DA_Q_4K
976         },
977         {
978                 /*
979                  * Intel 320 Series SSDs
980                  * 4k optimised & trim only works in 4k requests + 4k aligned
981                  */
982                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2CW*", "*" },
983                 /*quirks*/DA_Q_4K
984         },
985         {
986                 /*
987                  * Intel 330 Series SSDs
988                  * 4k optimised & trim only works in 4k requests + 4k aligned
989                  */
990                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2CT*", "*" },
991                 /*quirks*/DA_Q_4K
992         },
993         {
994                 /*
995                  * Intel 510 Series SSDs
996                  * 4k optimised & trim only works in 4k requests + 4k aligned
997                  */
998                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2MH*", "*" },
999                 /*quirks*/DA_Q_4K
1000         },
1001         {
1002                 /*
1003                  * Intel 520 Series SSDs
1004                  * 4k optimised & trim only works in 4k requests + 4k aligned
1005                  */
1006                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2BW*", "*" },
1007                 /*quirks*/DA_Q_4K
1008         },
1009         {
1010                 /*
1011                  * Intel X25-M Series SSDs
1012                  * 4k optimised & trim only works in 4k requests + 4k aligned
1013                  */
1014                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2M*", "*" },
1015                 /*quirks*/DA_Q_4K
1016         },
1017         {
1018                 /*
1019                  * Kingston E100 Series SSDs
1020                  * 4k optimised & trim only works in 4k requests + 4k aligned
1021                  */
1022                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SE100S3*", "*" },
1023                 /*quirks*/DA_Q_4K
1024         },
1025         {
1026                 /*
1027                  * Kingston HyperX 3k SSDs
1028                  * 4k optimised & trim only works in 4k requests + 4k aligned
1029                  */
1030                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SH103S3*", "*" },
1031                 /*quirks*/DA_Q_4K
1032         },
1033         {
1034                 /*
1035                  * Marvell SSDs (entry taken from OpenSolaris)
1036                  * 4k optimised & trim only works in 4k requests + 4k aligned
1037                  */
1038                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "MARVELL SD88SA02*", "*" },
1039                 /*quirks*/DA_Q_4K
1040         },
1041         {
1042                 /*
1043                  * OCZ Agility 2 SSDs
1044                  * 4k optimised & trim only works in 4k requests + 4k aligned
1045                  */
1046                 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY2*", "*" },
1047                 /*quirks*/DA_Q_4K
1048         },
1049         {
1050                 /*
1051                  * OCZ Agility 3 SSDs
1052                  * 4k optimised & trim only works in 4k requests + 4k aligned
1053                  */
1054                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-AGILITY3*", "*" },
1055                 /*quirks*/DA_Q_4K
1056         },
1057         {
1058                 /*
1059                  * OCZ Deneva R Series SSDs
1060                  * 4k optimised & trim only works in 4k requests + 4k aligned
1061                  */
1062                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "DENRSTE251M45*", "*" },
1063                 /*quirks*/DA_Q_4K
1064         },
1065         {
1066                 /*
1067                  * OCZ Vertex 2 SSDs (inc pro series)
1068                  * 4k optimised & trim only works in 4k requests + 4k aligned
1069                  */
1070                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ?VERTEX2*", "*" },
1071                 /*quirks*/DA_Q_4K
1072         },
1073         {
1074                 /*
1075                  * OCZ Vertex 3 SSDs
1076                  * 4k optimised & trim only works in 4k requests + 4k aligned
1077                  */
1078                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX3*", "*" },
1079                 /*quirks*/DA_Q_4K
1080         },
1081         {
1082                 /*
1083                  * OCZ Vertex 4 SSDs
1084                  * 4k optimised & trim only works in 4k requests + 4k aligned
1085                  */
1086                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX4*", "*" },
1087                 /*quirks*/DA_Q_4K
1088         },
1089         {
1090                 /*
1091                  * Samsung 830 Series SSDs
1092                  * 4k optimised & trim only works in 4k requests + 4k aligned
1093                  */
1094                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG SSD 830 Series*", "*" },
1095                 /*quirks*/DA_Q_4K
1096         },
1097         {
1098                 /*
1099                  * SuperTalent TeraDrive CT SSDs
1100                  * 4k optimised & trim only works in 4k requests + 4k aligned
1101                  */
1102                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "FTM??CT25H*", "*" },
1103                 /*quirks*/DA_Q_4K
1104         },
1105         {
1106                 /*
1107                  * XceedIOPS SATA SSDs
1108                  * 4k optimised
1109                  */
1110                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SG9XCS2D*", "*" },
1111                 /*quirks*/DA_Q_4K
1112         },
1113 };
1114
1115 static  disk_strategy_t dastrategy;
1116 static  dumper_t        dadump;
1117 static  periph_init_t   dainit;
1118 static  void            daasync(void *callback_arg, u_int32_t code,
1119                                 struct cam_path *path, void *arg);
1120 static  void            dasysctlinit(void *context, int pending);
1121 static  int             dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
1122 static  int             dadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
1123 static  int             dadeletemaxsysctl(SYSCTL_HANDLER_ARGS);
1124 static  void            dadeletemethodset(struct da_softc *softc,
1125                                           da_delete_methods delete_method);
1126 static  off_t           dadeletemaxsize(struct da_softc *softc,
1127                                         da_delete_methods delete_method);
1128 static  void            dadeletemethodchoose(struct da_softc *softc,
1129                                              da_delete_methods default_method);
1130 static  void            daprobedone(struct cam_periph *periph, union ccb *ccb);
1131
1132 static  periph_ctor_t   daregister;
1133 static  periph_dtor_t   dacleanup;
1134 static  periph_start_t  dastart;
1135 static  periph_oninv_t  daoninvalidate;
1136 static  void            dadone(struct cam_periph *periph,
1137                                union ccb *done_ccb);
1138 static  int             daerror(union ccb *ccb, u_int32_t cam_flags,
1139                                 u_int32_t sense_flags);
1140 static void             daprevent(struct cam_periph *periph, int action);
1141 static void             dareprobe(struct cam_periph *periph);
1142 static void             dasetgeom(struct cam_periph *periph, uint32_t block_len,
1143                                   uint64_t maxsector,
1144                                   struct scsi_read_capacity_data_long *rcaplong,
1145                                   size_t rcap_size);
1146 static timeout_t        dasendorderedtag;
1147 static void             dashutdown(void *arg, int howto);
1148 static timeout_t        damediapoll;
1149
1150 #ifndef DA_DEFAULT_POLL_PERIOD
1151 #define DA_DEFAULT_POLL_PERIOD  3
1152 #endif
1153
1154 #ifndef DA_DEFAULT_TIMEOUT
1155 #define DA_DEFAULT_TIMEOUT 60   /* Timeout in seconds */
1156 #endif
1157
1158 #ifndef DA_DEFAULT_RETRY
1159 #define DA_DEFAULT_RETRY        4
1160 #endif
1161
1162 #ifndef DA_DEFAULT_SEND_ORDERED
1163 #define DA_DEFAULT_SEND_ORDERED 1
1164 #endif
1165
1166 #define DA_SIO (softc->sort_io_queue >= 0 ? \
1167     softc->sort_io_queue : cam_sort_io_queues)
1168
1169 static int da_poll_period = DA_DEFAULT_POLL_PERIOD;
1170 static int da_retry_count = DA_DEFAULT_RETRY;
1171 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
1172 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
1173
1174 static SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
1175             "CAM Direct Access Disk driver");
1176 SYSCTL_INT(_kern_cam_da, OID_AUTO, poll_period, CTLFLAG_RW,
1177            &da_poll_period, 0, "Media polling period in seconds");
1178 TUNABLE_INT("kern.cam.da.poll_period", &da_poll_period);
1179 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
1180            &da_retry_count, 0, "Normal I/O retry count");
1181 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
1182 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
1183            &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
1184 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
1185 SYSCTL_INT(_kern_cam_da, OID_AUTO, send_ordered, CTLFLAG_RW,
1186            &da_send_ordered, 0, "Send Ordered Tags");
1187 TUNABLE_INT("kern.cam.da.send_ordered", &da_send_ordered);
1188
1189 /*
1190  * DA_ORDEREDTAG_INTERVAL determines how often, relative
1191  * to the default timeout, we check to see whether an ordered
1192  * tagged transaction is appropriate to prevent simple tag
1193  * starvation.  Since we'd like to ensure that there is at least
1194  * 1/2 of the timeout length left for a starved transaction to
1195  * complete after we've sent an ordered tag, we must poll at least
1196  * four times in every timeout period.  This takes care of the worst
1197  * case where a starved transaction starts during an interval that
1198  * meets the requirement "don't send an ordered tag" test so it takes
1199  * us two intervals to determine that a tag must be sent.
1200  */
1201 #ifndef DA_ORDEREDTAG_INTERVAL
1202 #define DA_ORDEREDTAG_INTERVAL 4
1203 #endif
1204
1205 static struct periph_driver dadriver =
1206 {
1207         dainit, "da",
1208         TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
1209 };
1210
1211 PERIPHDRIVER_DECLARE(da, dadriver);
1212
1213 static MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
1214
1215 static int
1216 daopen(struct disk *dp)
1217 {
1218         struct cam_periph *periph;
1219         struct da_softc *softc;
1220         int error;
1221
1222         periph = (struct cam_periph *)dp->d_drv1;
1223         if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1224                 return (ENXIO);
1225         }
1226
1227         cam_periph_lock(periph);
1228         if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
1229                 cam_periph_unlock(periph);
1230                 cam_periph_release(periph);
1231                 return (error);
1232         }
1233
1234         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1235             ("daopen\n"));
1236
1237         softc = (struct da_softc *)periph->softc;
1238         dareprobe(periph);
1239
1240         /* Wait for the disk size update.  */
1241         error = cam_periph_sleep(periph, &softc->disk->d_mediasize, PRIBIO,
1242             "dareprobe", 0);
1243         if (error != 0)
1244                 xpt_print(periph->path, "unable to retrieve capacity data");
1245
1246         if (periph->flags & CAM_PERIPH_INVALID)
1247                 error = ENXIO;
1248
1249         if (error == 0 && (softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
1250             (softc->quirks & DA_Q_NO_PREVENT) == 0)
1251                 daprevent(periph, PR_PREVENT);
1252
1253         if (error == 0) {
1254                 softc->flags &= ~DA_FLAG_PACK_INVALID;
1255                 softc->flags |= DA_FLAG_OPEN;
1256         }
1257
1258         cam_periph_unhold(periph);
1259         cam_periph_unlock(periph);
1260
1261         if (error != 0)
1262                 cam_periph_release(periph);
1263
1264         return (error);
1265 }
1266
1267 static int
1268 daclose(struct disk *dp)
1269 {
1270         struct  cam_periph *periph;
1271         struct  da_softc *softc;
1272         int error;
1273
1274         periph = (struct cam_periph *)dp->d_drv1;
1275         cam_periph_lock(periph);
1276         if (cam_periph_hold(periph, PRIBIO) != 0) {
1277                 cam_periph_unlock(periph);
1278                 cam_periph_release(periph);
1279                 return (0);
1280         }
1281
1282         softc = (struct da_softc *)periph->softc;
1283
1284         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
1285             ("daclose\n"));
1286
1287         if ((softc->flags & DA_FLAG_DIRTY) != 0 &&
1288             (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0 &&
1289             (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
1290                 union   ccb *ccb;
1291
1292                 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1293
1294                 scsi_synchronize_cache(&ccb->csio,
1295                                        /*retries*/1,
1296                                        /*cbfcnp*/dadone,
1297                                        MSG_SIMPLE_Q_TAG,
1298                                        /*begin_lba*/0,/* Cover the whole disk */
1299                                        /*lb_count*/0,
1300                                        SSD_FULL_SIZE,
1301                                        5 * 60 * 1000);
1302
1303                 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
1304                                   /*sense_flags*/SF_RETRY_UA | SF_QUIET_IR,
1305                                   softc->disk->d_devstat);
1306                 if (error == 0)
1307                         softc->flags &= ~DA_FLAG_DIRTY;
1308                 xpt_release_ccb(ccb);
1309
1310         }
1311
1312         if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
1313                 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
1314                         daprevent(periph, PR_ALLOW);
1315                 /*
1316                  * If we've got removeable media, mark the blocksize as
1317                  * unavailable, since it could change when new media is
1318                  * inserted.
1319                  */
1320                 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1321         }
1322
1323         softc->flags &= ~DA_FLAG_OPEN;
1324         cam_periph_unhold(periph);
1325         cam_periph_unlock(periph);
1326         cam_periph_release(periph);
1327         return (0);     
1328 }
1329
1330 static void
1331 daschedule(struct cam_periph *periph)
1332 {
1333         struct da_softc *softc = (struct da_softc *)periph->softc;
1334         uint32_t prio;
1335
1336         if (softc->state != DA_STATE_NORMAL)
1337                 return;
1338
1339         /* Check if cam_periph_getccb() was called. */
1340         prio = periph->immediate_priority;
1341
1342         /* Check if we have more work to do. */
1343         if (bioq_first(&softc->bio_queue) ||
1344             (!softc->delete_running && bioq_first(&softc->delete_queue)) ||
1345             softc->tur) {
1346                 prio = CAM_PRIORITY_NORMAL;
1347         }
1348
1349         /* Schedule CCB if any of above is true. */
1350         if (prio != CAM_PRIORITY_NONE)
1351                 xpt_schedule(periph, prio);
1352 }
1353
1354 /*
1355  * Actually translate the requested transfer into one the physical driver
1356  * can understand.  The transfer is described by a buf and will include
1357  * only one physical transfer.
1358  */
1359 static void
1360 dastrategy(struct bio *bp)
1361 {
1362         struct cam_periph *periph;
1363         struct da_softc *softc;
1364         
1365         periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1366         softc = (struct da_softc *)periph->softc;
1367
1368         cam_periph_lock(periph);
1369
1370         /*
1371          * If the device has been made invalid, error out
1372          */
1373         if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1374                 cam_periph_unlock(periph);
1375                 biofinish(bp, NULL, ENXIO);
1376                 return;
1377         }
1378
1379         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
1380
1381         /*
1382          * Place it in the queue of disk activities for this disk
1383          */
1384         if (bp->bio_cmd == BIO_DELETE) {
1385                 if (bp->bio_bcount == 0)
1386                         biodone(bp);
1387                 else if (DA_SIO)
1388                         bioq_disksort(&softc->delete_queue, bp);
1389                 else
1390                         bioq_insert_tail(&softc->delete_queue, bp);
1391         } else if (DA_SIO) {
1392                 bioq_disksort(&softc->bio_queue, bp);
1393         } else {
1394                 bioq_insert_tail(&softc->bio_queue, bp);
1395         }
1396
1397         /*
1398          * Schedule ourselves for performing the work.
1399          */
1400         daschedule(periph);
1401         cam_periph_unlock(periph);
1402
1403         return;
1404 }
1405
1406 static int
1407 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1408 {
1409         struct      cam_periph *periph;
1410         struct      da_softc *softc;
1411         u_int       secsize;
1412         struct      ccb_scsiio csio;
1413         struct      disk *dp;
1414         int         error = 0;
1415
1416         dp = arg;
1417         periph = dp->d_drv1;
1418         softc = (struct da_softc *)periph->softc;
1419         cam_periph_lock(periph);
1420         secsize = softc->params.secsize;
1421         
1422         if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1423                 cam_periph_unlock(periph);
1424                 return (ENXIO);
1425         }
1426
1427         if (length > 0) {
1428                 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1429                 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1430                 scsi_read_write(&csio,
1431                                 /*retries*/0,
1432                                 dadone,
1433                                 MSG_ORDERED_Q_TAG,
1434                                 /*read*/SCSI_RW_WRITE,
1435                                 /*byte2*/0,
1436                                 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1437                                 offset / secsize,
1438                                 length / secsize,
1439                                 /*data_ptr*/(u_int8_t *) virtual,
1440                                 /*dxfer_len*/length,
1441                                 /*sense_len*/SSD_FULL_SIZE,
1442                                 da_default_timeout * 1000);
1443                 xpt_polled_action((union ccb *)&csio);
1444
1445                 error = cam_periph_error((union ccb *)&csio,
1446                     0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1447                 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1448                         cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1449                             /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1450                 if (error != 0)
1451                         printf("Aborting dump due to I/O error.\n");
1452                 cam_periph_unlock(periph);
1453                 return (error);
1454         }
1455                 
1456         /*
1457          * Sync the disk cache contents to the physical media.
1458          */
1459         if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1460
1461                 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1462                 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1463                 scsi_synchronize_cache(&csio,
1464                                        /*retries*/0,
1465                                        /*cbfcnp*/dadone,
1466                                        MSG_SIMPLE_Q_TAG,
1467                                        /*begin_lba*/0,/* Cover the whole disk */
1468                                        /*lb_count*/0,
1469                                        SSD_FULL_SIZE,
1470                                        5 * 60 * 1000);
1471                 xpt_polled_action((union ccb *)&csio);
1472
1473                 error = cam_periph_error((union ccb *)&csio,
1474                     0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL);
1475                 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
1476                         cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
1477                             /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1478                 if (error != 0)
1479                         xpt_print(periph->path, "Synchronize cache failed\n");
1480         }
1481         cam_periph_unlock(periph);
1482         return (error);
1483 }
1484
1485 static int
1486 dagetattr(struct bio *bp)
1487 {
1488         int ret;
1489         struct cam_periph *periph;
1490
1491         periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1492         cam_periph_lock(periph);
1493         ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1494             periph->path);
1495         cam_periph_unlock(periph);
1496         if (ret == 0)
1497                 bp->bio_completed = bp->bio_length;
1498         return ret;
1499 }
1500
1501 static void
1502 dainit(void)
1503 {
1504         cam_status status;
1505
1506         /*
1507          * Install a global async callback.  This callback will
1508          * receive async callbacks like "new device found".
1509          */
1510         status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1511
1512         if (status != CAM_REQ_CMP) {
1513                 printf("da: Failed to attach master async callback "
1514                        "due to status 0x%x!\n", status);
1515         } else if (da_send_ordered) {
1516
1517                 /* Register our shutdown event handler */
1518                 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown, 
1519                                            NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1520                     printf("dainit: shutdown event registration failed!\n");
1521         }
1522 }
1523
1524 /*
1525  * Callback from GEOM, called when it has finished cleaning up its
1526  * resources.
1527  */
1528 static void
1529 dadiskgonecb(struct disk *dp)
1530 {
1531         struct cam_periph *periph;
1532
1533         periph = (struct cam_periph *)dp->d_drv1;
1534         cam_periph_release(periph);
1535 }
1536
1537 static void
1538 daoninvalidate(struct cam_periph *periph)
1539 {
1540         struct da_softc *softc;
1541
1542         softc = (struct da_softc *)periph->softc;
1543
1544         /*
1545          * De-register any async callbacks.
1546          */
1547         xpt_register_async(0, daasync, periph, periph->path);
1548
1549         softc->flags |= DA_FLAG_PACK_INVALID;
1550
1551         /*
1552          * Return all queued I/O with ENXIO.
1553          * XXX Handle any transactions queued to the card
1554          *     with XPT_ABORT_CCB.
1555          */
1556         bioq_flush(&softc->bio_queue, NULL, ENXIO);
1557         bioq_flush(&softc->delete_queue, NULL, ENXIO);
1558
1559         /*
1560          * Tell GEOM that we've gone away, we'll get a callback when it is
1561          * done cleaning up its resources.
1562          */
1563         disk_gone(softc->disk);
1564 }
1565
1566 static void
1567 dacleanup(struct cam_periph *periph)
1568 {
1569         struct da_softc *softc;
1570
1571         softc = (struct da_softc *)periph->softc;
1572
1573         cam_periph_unlock(periph);
1574
1575         /*
1576          * If we can't free the sysctl tree, oh well...
1577          */
1578         if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1579             && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1580                 xpt_print(periph->path, "can't remove sysctl context\n");
1581         }
1582
1583         callout_drain(&softc->mediapoll_c);
1584         disk_destroy(softc->disk);
1585         callout_drain(&softc->sendordered_c);
1586         free(softc, M_DEVBUF);
1587         cam_periph_lock(periph);
1588 }
1589
1590 static void
1591 daasync(void *callback_arg, u_int32_t code,
1592         struct cam_path *path, void *arg)
1593 {
1594         struct cam_periph *periph;
1595         struct da_softc *softc;
1596
1597         periph = (struct cam_periph *)callback_arg;
1598         switch (code) {
1599         case AC_FOUND_DEVICE:
1600         {
1601                 struct ccb_getdev *cgd;
1602                 cam_status status;
1603  
1604                 cgd = (struct ccb_getdev *)arg;
1605                 if (cgd == NULL)
1606                         break;
1607
1608                 if (cgd->protocol != PROTO_SCSI)
1609                         break;
1610
1611                 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1612                     && SID_TYPE(&cgd->inq_data) != T_RBC
1613                     && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1614                         break;
1615
1616                 /*
1617                  * Allocate a peripheral instance for
1618                  * this device and start the probe
1619                  * process.
1620                  */
1621                 status = cam_periph_alloc(daregister, daoninvalidate,
1622                                           dacleanup, dastart,
1623                                           "da", CAM_PERIPH_BIO,
1624                                           cgd->ccb_h.path, daasync,
1625                                           AC_FOUND_DEVICE, cgd);
1626
1627                 if (status != CAM_REQ_CMP
1628                  && status != CAM_REQ_INPROG)
1629                         printf("daasync: Unable to attach to new device "
1630                                 "due to status 0x%x\n", status);
1631                 return;
1632         }
1633         case AC_ADVINFO_CHANGED:
1634         {
1635                 uintptr_t buftype;
1636
1637                 buftype = (uintptr_t)arg;
1638                 if (buftype == CDAI_TYPE_PHYS_PATH) {
1639                         struct da_softc *softc;
1640
1641                         softc = periph->softc;
1642                         disk_attr_changed(softc->disk, "GEOM::physpath",
1643                                           M_NOWAIT);
1644                 }
1645                 break;
1646         }
1647         case AC_UNIT_ATTENTION:
1648         {
1649                 union ccb *ccb;
1650                 int error_code, sense_key, asc, ascq;
1651
1652                 softc = (struct da_softc *)periph->softc;
1653                 ccb = (union ccb *)arg;
1654
1655                 /*
1656                  * Handle all UNIT ATTENTIONs except our own,
1657                  * as they will be handled by daerror().
1658                  */
1659                 if (xpt_path_periph(ccb->ccb_h.path) != periph &&
1660                     scsi_extract_sense_ccb(ccb,
1661                      &error_code, &sense_key, &asc, &ascq)) {
1662                         if (asc == 0x2A && ascq == 0x09) {
1663                                 xpt_print(ccb->ccb_h.path,
1664                                     "capacity data has changed\n");
1665                                 dareprobe(periph);
1666                         } else if (asc == 0x28 && ascq == 0x00)
1667                                 disk_media_changed(softc->disk, M_NOWAIT);
1668                 }
1669                 cam_periph_async(periph, code, path, arg);
1670                 break;
1671         }
1672         case AC_SCSI_AEN:
1673                 softc = (struct da_softc *)periph->softc;
1674                 if (!softc->tur) {
1675                         if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
1676                                 softc->tur = 1;
1677                                 daschedule(periph);
1678                         }
1679                 }
1680                 /* FALLTHROUGH */
1681         case AC_SENT_BDR:
1682         case AC_BUS_RESET:
1683         {
1684                 struct ccb_hdr *ccbh;
1685
1686                 softc = (struct da_softc *)periph->softc;
1687                 /*
1688                  * Don't fail on the expected unit attention
1689                  * that will occur.
1690                  */
1691                 softc->flags |= DA_FLAG_RETRY_UA;
1692                 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1693                         ccbh->ccb_state |= DA_CCB_RETRY_UA;
1694                 break;
1695         }
1696         default:
1697                 break;
1698         }
1699         cam_periph_async(periph, code, path, arg);
1700 }
1701
1702 static void
1703 dasysctlinit(void *context, int pending)
1704 {
1705         struct cam_periph *periph;
1706         struct da_softc *softc;
1707         char tmpstr[80], tmpstr2[80];
1708         struct ccb_trans_settings cts;
1709
1710         periph = (struct cam_periph *)context;
1711         /*
1712          * periph was held for us when this task was enqueued
1713          */
1714         if (periph->flags & CAM_PERIPH_INVALID) {
1715                 cam_periph_release(periph);
1716                 return;
1717         }
1718
1719         softc = (struct da_softc *)periph->softc;
1720         snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1721         snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1722
1723         sysctl_ctx_init(&softc->sysctl_ctx);
1724         softc->flags |= DA_FLAG_SCTX_INIT;
1725         softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1726                 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1727                 CTLFLAG_RD, 0, tmpstr);
1728         if (softc->sysctl_tree == NULL) {
1729                 printf("dasysctlinit: unable to allocate sysctl tree\n");
1730                 cam_periph_release(periph);
1731                 return;
1732         }
1733
1734         /*
1735          * Now register the sysctl handler, so the user can change the value on
1736          * the fly.
1737          */
1738         SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1739                 OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RW,
1740                 softc, 0, dadeletemethodsysctl, "A",
1741                 "BIO_DELETE execution method");
1742         SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1743                 OID_AUTO, "delete_max", CTLTYPE_U64 | CTLFLAG_RW,
1744                 softc, 0, dadeletemaxsysctl, "Q",
1745                 "Maximum BIO_DELETE size");
1746         SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1747                 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1748                 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1749                 "Minimum CDB size");
1750         SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1751                 OID_AUTO, "sort_io_queue", CTLFLAG_RW, &softc->sort_io_queue, 0,
1752                 "Sort IO queue to try and optimise disk access patterns");
1753
1754         SYSCTL_ADD_INT(&softc->sysctl_ctx,
1755                        SYSCTL_CHILDREN(softc->sysctl_tree),
1756                        OID_AUTO,
1757                        "error_inject",
1758                        CTLFLAG_RW,
1759                        &softc->error_inject,
1760                        0,
1761                        "error_inject leaf");
1762
1763
1764         /*
1765          * Add some addressing info.
1766          */
1767         memset(&cts, 0, sizeof (cts));
1768         xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1769         cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1770         cts.type = CTS_TYPE_CURRENT_SETTINGS;
1771         cam_periph_lock(periph);
1772         xpt_action((union ccb *)&cts);
1773         cam_periph_unlock(periph);
1774         if (cts.ccb_h.status != CAM_REQ_CMP) {
1775                 cam_periph_release(periph);
1776                 return;
1777         }
1778         if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
1779                 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1780                 if (fc->valid & CTS_FC_VALID_WWPN) {
1781                         softc->wwpn = fc->wwpn;
1782                         SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
1783                             SYSCTL_CHILDREN(softc->sysctl_tree),
1784                             OID_AUTO, "wwpn", CTLFLAG_RD,
1785                             &softc->wwpn, "World Wide Port Name");
1786                 }
1787         }
1788         cam_periph_release(periph);
1789 }
1790
1791 static int
1792 dadeletemaxsysctl(SYSCTL_HANDLER_ARGS)
1793 {
1794         int error;
1795         uint64_t value;
1796         struct da_softc *softc;
1797
1798         softc = (struct da_softc *)arg1;
1799
1800         value = softc->disk->d_delmaxsize;
1801         error = sysctl_handle_64(oidp, &value, 0, req);
1802         if ((error != 0) || (req->newptr == NULL))
1803                 return (error);
1804
1805         /* only accept values smaller than the calculated value */
1806         if (value > dadeletemaxsize(softc, softc->delete_method)) {
1807                 return (EINVAL);
1808         }
1809         softc->disk->d_delmaxsize = value;
1810
1811         return (0);
1812 }
1813
1814 static int
1815 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1816 {
1817         int error, value;
1818
1819         value = *(int *)arg1;
1820
1821         error = sysctl_handle_int(oidp, &value, 0, req);
1822
1823         if ((error != 0)
1824          || (req->newptr == NULL))
1825                 return (error);
1826
1827         /*
1828          * Acceptable values here are 6, 10, 12 or 16.
1829          */
1830         if (value < 6)
1831                 value = 6;
1832         else if ((value > 6)
1833               && (value <= 10))
1834                 value = 10;
1835         else if ((value > 10)
1836               && (value <= 12))
1837                 value = 12;
1838         else if (value > 12)
1839                 value = 16;
1840
1841         *(int *)arg1 = value;
1842
1843         return (0);
1844 }
1845
1846 static void
1847 dadeletemethodset(struct da_softc *softc, da_delete_methods delete_method)
1848 {
1849
1850
1851         softc->delete_method = delete_method;
1852         softc->disk->d_delmaxsize = dadeletemaxsize(softc, delete_method);
1853         softc->delete_func = da_delete_functions[delete_method];
1854
1855         if (softc->delete_method > DA_DELETE_DISABLE)
1856                 softc->disk->d_flags |= DISKFLAG_CANDELETE;
1857         else
1858                 softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
1859 }
1860
1861 static off_t
1862 dadeletemaxsize(struct da_softc *softc, da_delete_methods delete_method)
1863 {
1864         off_t sectors;
1865
1866         switch(delete_method) {
1867         case DA_DELETE_UNMAP:
1868                 sectors = (off_t)softc->unmap_max_lba * softc->unmap_max_ranges;
1869                 break;
1870         case DA_DELETE_ATA_TRIM:
1871                 sectors = (off_t)ATA_DSM_RANGE_MAX * softc->trim_max_ranges;
1872                 break;
1873         case DA_DELETE_WS16:
1874                 sectors = (off_t)min(softc->ws_max_blks, WS16_MAX_BLKS);
1875                 break;
1876         case DA_DELETE_ZERO:
1877         case DA_DELETE_WS10:
1878                 sectors = (off_t)min(softc->ws_max_blks, WS10_MAX_BLKS);
1879                 break;
1880         default:
1881                 return 0;
1882         }
1883
1884         return (off_t)softc->params.secsize *
1885             min(sectors, (off_t)softc->params.sectors);
1886 }
1887
1888 static void
1889 daprobedone(struct cam_periph *periph, union ccb *ccb)
1890 {
1891         struct da_softc *softc;
1892
1893         softc = (struct da_softc *)periph->softc;
1894
1895         dadeletemethodchoose(softc, DA_DELETE_NONE);
1896
1897         if (bootverbose && (softc->flags & DA_FLAG_PROBED) == 0) {
1898                 char buf[80];
1899                 int i, sep;
1900
1901                 snprintf(buf, sizeof(buf), "Delete methods: <");
1902                 sep = 0;
1903                 for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
1904                         if (softc->delete_available & (1 << i)) {
1905                                 if (sep) {
1906                                         strlcat(buf, ",", sizeof(buf));
1907                                 } else {
1908                                     sep = 1;
1909                                 }
1910                                 strlcat(buf, da_delete_method_names[i],
1911                                     sizeof(buf));
1912                                 if (i == softc->delete_method) {
1913                                         strlcat(buf, "(*)", sizeof(buf));
1914                                 }
1915                         }
1916                 }
1917                 if (sep == 0) {
1918                         if (softc->delete_method == DA_DELETE_NONE) 
1919                                 strlcat(buf, "NONE(*)", sizeof(buf));
1920                         else
1921                                 strlcat(buf, "DISABLED(*)", sizeof(buf));
1922                 }
1923                 strlcat(buf, ">", sizeof(buf));
1924                 printf("%s%d: %s\n", periph->periph_name,
1925                     periph->unit_number, buf);
1926         }
1927
1928         /*
1929          * Since our peripheral may be invalidated by an error
1930          * above or an external event, we must release our CCB
1931          * before releasing the probe lock on the peripheral.
1932          * The peripheral will only go away once the last lock
1933          * is removed, and we need it around for the CCB release
1934          * operation.
1935          */
1936         xpt_release_ccb(ccb);
1937         softc->state = DA_STATE_NORMAL;
1938         daschedule(periph);
1939         wakeup(&softc->disk->d_mediasize);
1940         if ((softc->flags & DA_FLAG_PROBED) == 0) {
1941                 softc->flags |= DA_FLAG_PROBED;
1942                 cam_periph_unhold(periph);
1943         } else
1944                 cam_periph_release_locked(periph);
1945 }
1946
1947 static void
1948 dadeletemethodchoose(struct da_softc *softc, da_delete_methods default_method)
1949 {
1950         int i, delete_method;
1951
1952         delete_method = default_method;
1953
1954         /*
1955          * Use the pre-defined order to choose the best
1956          * performing delete.
1957          */
1958         for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
1959                 if (softc->delete_available & (1 << i)) {
1960                         dadeletemethodset(softc, i);
1961                         return;
1962                 }
1963         }
1964         dadeletemethodset(softc, delete_method);
1965 }
1966
1967 static int
1968 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
1969 {
1970         char buf[16];
1971         const char *p;
1972         struct da_softc *softc;
1973         int i, error, value;
1974
1975         softc = (struct da_softc *)arg1;
1976
1977         value = softc->delete_method;
1978         if (value < 0 || value > DA_DELETE_MAX)
1979                 p = "UNKNOWN";
1980         else
1981                 p = da_delete_method_names[value];
1982         strncpy(buf, p, sizeof(buf));
1983         error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1984         if (error != 0 || req->newptr == NULL)
1985                 return (error);
1986         for (i = 0; i <= DA_DELETE_MAX; i++) {
1987                 if (!(softc->delete_available & (1 << i)) ||
1988                     strcmp(buf, da_delete_method_names[i]) != 0)
1989                         continue;
1990                 dadeletemethodset(softc, i);
1991                 return (0);
1992         }
1993         return (EINVAL);
1994 }
1995
1996 static cam_status
1997 daregister(struct cam_periph *periph, void *arg)
1998 {
1999         struct da_softc *softc;
2000         struct ccb_pathinq cpi;
2001         struct ccb_getdev *cgd;
2002         char tmpstr[80];
2003         caddr_t match;
2004
2005         cgd = (struct ccb_getdev *)arg;
2006         if (cgd == NULL) {
2007                 printf("daregister: no getdev CCB, can't register device\n");
2008                 return(CAM_REQ_CMP_ERR);
2009         }
2010
2011         softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
2012             M_NOWAIT|M_ZERO);
2013
2014         if (softc == NULL) {
2015                 printf("daregister: Unable to probe new device. "
2016                        "Unable to allocate softc\n");                           
2017                 return(CAM_REQ_CMP_ERR);
2018         }
2019
2020         LIST_INIT(&softc->pending_ccbs);
2021         softc->state = DA_STATE_PROBE_RC;
2022         bioq_init(&softc->bio_queue);
2023         bioq_init(&softc->delete_queue);
2024         bioq_init(&softc->delete_run_queue);
2025         if (SID_IS_REMOVABLE(&cgd->inq_data))
2026                 softc->flags |= DA_FLAG_PACK_REMOVABLE;
2027         softc->unmap_max_ranges = UNMAP_MAX_RANGES;
2028         softc->unmap_max_lba = UNMAP_RANGE_MAX;
2029         softc->ws_max_blks = WS16_MAX_BLKS;
2030         softc->trim_max_ranges = ATA_TRIM_MAX_RANGES;
2031         softc->sort_io_queue = -1;
2032
2033         periph->softc = softc;
2034
2035         /*
2036          * See if this device has any quirks.
2037          */
2038         match = cam_quirkmatch((caddr_t)&cgd->inq_data,
2039                                (caddr_t)da_quirk_table,
2040                                sizeof(da_quirk_table)/sizeof(*da_quirk_table),
2041                                sizeof(*da_quirk_table), scsi_inquiry_match);
2042
2043         if (match != NULL)
2044                 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
2045         else
2046                 softc->quirks = DA_Q_NONE;
2047
2048         /* Check if the SIM does not want 6 byte commands */
2049         bzero(&cpi, sizeof(cpi));
2050         xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2051         cpi.ccb_h.func_code = XPT_PATH_INQ;
2052         xpt_action((union ccb *)&cpi);
2053         if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
2054                 softc->quirks |= DA_Q_NO_6_BYTE;
2055
2056         TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
2057
2058         /*
2059          * Take an exclusive refcount on the periph while dastart is called
2060          * to finish the probe.  The reference will be dropped in dadone at
2061          * the end of probe.
2062          */
2063         (void)cam_periph_hold(periph, PRIBIO);
2064
2065         /*
2066          * Schedule a periodic event to occasionally send an
2067          * ordered tag to a device.
2068          */
2069         callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
2070         callout_reset(&softc->sendordered_c,
2071             (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2072             dasendorderedtag, softc);
2073
2074         cam_periph_unlock(periph);
2075         /*
2076          * RBC devices don't have to support READ(6), only READ(10).
2077          */
2078         if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
2079                 softc->minimum_cmd_size = 10;
2080         else
2081                 softc->minimum_cmd_size = 6;
2082
2083         /*
2084          * Load the user's default, if any.
2085          */
2086         snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
2087                  periph->unit_number);
2088         TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
2089
2090         /*
2091          * 6, 10, 12 and 16 are the currently permissible values.
2092          */
2093         if (softc->minimum_cmd_size < 6)
2094                 softc->minimum_cmd_size = 6;
2095         else if ((softc->minimum_cmd_size > 6)
2096               && (softc->minimum_cmd_size <= 10))
2097                 softc->minimum_cmd_size = 10;
2098         else if ((softc->minimum_cmd_size > 10)
2099               && (softc->minimum_cmd_size <= 12))
2100                 softc->minimum_cmd_size = 12;
2101         else if (softc->minimum_cmd_size > 12)
2102                 softc->minimum_cmd_size = 16;
2103
2104         /* Predict whether device may support READ CAPACITY(16). */
2105         if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3 &&
2106             (softc->quirks & DA_Q_NO_RC16) == 0) {
2107                 softc->flags |= DA_FLAG_CAN_RC16;
2108                 softc->state = DA_STATE_PROBE_RC16;
2109         }
2110
2111         /*
2112          * Register this media as a disk.
2113          */
2114         softc->disk = disk_alloc();
2115         softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
2116                           periph->unit_number, 0,
2117                           DEVSTAT_BS_UNAVAILABLE,
2118                           SID_TYPE(&cgd->inq_data) |
2119                           XPORT_DEVSTAT_TYPE(cpi.transport),
2120                           DEVSTAT_PRIORITY_DISK);
2121         softc->disk->d_open = daopen;
2122         softc->disk->d_close = daclose;
2123         softc->disk->d_strategy = dastrategy;
2124         softc->disk->d_dump = dadump;
2125         softc->disk->d_getattr = dagetattr;
2126         softc->disk->d_gone = dadiskgonecb;
2127         softc->disk->d_name = "da";
2128         softc->disk->d_drv1 = periph;
2129         if (cpi.maxio == 0)
2130                 softc->disk->d_maxsize = DFLTPHYS;      /* traditional default */
2131         else if (cpi.maxio > MAXPHYS)
2132                 softc->disk->d_maxsize = MAXPHYS;       /* for safety */
2133         else
2134                 softc->disk->d_maxsize = cpi.maxio;
2135         softc->disk->d_unit = periph->unit_number;
2136         softc->disk->d_flags = 0;
2137         if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
2138                 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
2139         if ((cpi.hba_misc & PIM_UNMAPPED) != 0)
2140                 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
2141         cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
2142             sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
2143         strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
2144         cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
2145             cgd->inq_data.product, sizeof(cgd->inq_data.product),
2146             sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
2147         softc->disk->d_hba_vendor = cpi.hba_vendor;
2148         softc->disk->d_hba_device = cpi.hba_device;
2149         softc->disk->d_hba_subvendor = cpi.hba_subvendor;
2150         softc->disk->d_hba_subdevice = cpi.hba_subdevice;
2151
2152         /*
2153          * Acquire a reference to the periph before we register with GEOM.
2154          * We'll release this reference once GEOM calls us back (via
2155          * dadiskgonecb()) telling us that our provider has been freed.
2156          */
2157         if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
2158                 xpt_print(periph->path, "%s: lost periph during "
2159                           "registration!\n", __func__);
2160                 cam_periph_lock(periph);
2161                 return (CAM_REQ_CMP_ERR);
2162         }
2163
2164         disk_create(softc->disk, DISK_VERSION);
2165         cam_periph_lock(periph);
2166
2167         /*
2168          * Add async callbacks for events of interest.
2169          * I don't bother checking if this fails as,
2170          * in most cases, the system will function just
2171          * fine without them and the only alternative
2172          * would be to not attach the device on failure.
2173          */
2174         xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
2175             AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION,
2176             daasync, periph, periph->path);
2177
2178         /*
2179          * Emit an attribute changed notification just in case 
2180          * physical path information arrived before our async
2181          * event handler was registered, but after anyone attaching
2182          * to our disk device polled it.
2183          */
2184         disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
2185
2186         /*
2187          * Schedule a periodic media polling events.
2188          */
2189         callout_init_mtx(&softc->mediapoll_c, periph->sim->mtx, 0);
2190         if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
2191             (cgd->inq_flags & SID_AEN) == 0 &&
2192             da_poll_period != 0)
2193                 callout_reset(&softc->mediapoll_c, da_poll_period * hz,
2194                     damediapoll, periph);
2195
2196         xpt_schedule(periph, CAM_PRIORITY_DEV);
2197
2198         return(CAM_REQ_CMP);
2199 }
2200
2201 static void
2202 dastart(struct cam_periph *periph, union ccb *start_ccb)
2203 {
2204         struct da_softc *softc;
2205
2206         softc = (struct da_softc *)periph->softc;
2207
2208         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
2209
2210 skipstate:
2211         switch (softc->state) {
2212         case DA_STATE_NORMAL:
2213         {
2214                 struct bio *bp;
2215                 uint8_t tag_code;
2216
2217                 /* Execute immediate CCB if waiting. */
2218                 if (periph->immediate_priority <= periph->pinfo.priority) {
2219                         CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
2220                                         ("queuing for immediate ccb\n"));
2221                         start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
2222                         SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
2223                                           periph_links.sle);
2224                         periph->immediate_priority = CAM_PRIORITY_NONE;
2225                         wakeup(&periph->ccb_list);
2226                         /* May have more work to do, so ensure we stay scheduled */
2227                         daschedule(periph);
2228                         break;
2229                 }
2230
2231                 /* Run BIO_DELETE if not running yet. */
2232                 if (!softc->delete_running &&
2233                     (bp = bioq_first(&softc->delete_queue)) != NULL) {
2234                         if (softc->delete_func != NULL) {
2235                                 softc->delete_func(periph, start_ccb, bp);
2236                                 goto out;
2237                         } else {
2238                                 bioq_flush(&softc->delete_queue, NULL, 0);
2239                                 /* FALLTHROUGH */
2240                         }
2241                 }
2242
2243                 /* Run regular command. */
2244                 bp = bioq_takefirst(&softc->bio_queue);
2245                 if (bp == NULL) {
2246                         if (softc->tur) {
2247                                 softc->tur = 0;
2248                                 scsi_test_unit_ready(&start_ccb->csio,
2249                                      /*retries*/ da_retry_count,
2250                                      dadone,
2251                                      MSG_SIMPLE_Q_TAG,
2252                                      SSD_FULL_SIZE,
2253                                      da_default_timeout * 1000);
2254                                 start_ccb->ccb_h.ccb_bp = NULL;
2255                                 start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
2256                                 xpt_action(start_ccb);
2257                         } else
2258                                 xpt_release_ccb(start_ccb);
2259                         break;
2260                 }
2261                 if (softc->tur) {
2262                         softc->tur = 0;
2263                         cam_periph_release_locked(periph);
2264                 }
2265
2266                 if ((bp->bio_flags & BIO_ORDERED) != 0 ||
2267                     (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
2268                         softc->flags &= ~DA_FLAG_NEED_OTAG;
2269                         softc->ordered_tag_count++;
2270                         tag_code = MSG_ORDERED_Q_TAG;
2271                 } else {
2272                         tag_code = MSG_SIMPLE_Q_TAG;
2273                 }
2274
2275                 switch (bp->bio_cmd) {
2276                 case BIO_WRITE:
2277                         softc->flags |= DA_FLAG_DIRTY;
2278                         /* FALLTHROUGH */
2279                 case BIO_READ:
2280                         scsi_read_write(&start_ccb->csio,
2281                                         /*retries*/da_retry_count,
2282                                         /*cbfcnp*/dadone,
2283                                         /*tag_action*/tag_code,
2284                                         /*read_op*/(bp->bio_cmd == BIO_READ ?
2285                                         SCSI_RW_READ : SCSI_RW_WRITE) |
2286                                         ((bp->bio_flags & BIO_UNMAPPED) != 0 ?
2287                                         SCSI_RW_BIO : 0),
2288                                         /*byte2*/0,
2289                                         softc->minimum_cmd_size,
2290                                         /*lba*/bp->bio_pblkno,
2291                                         /*block_count*/bp->bio_bcount /
2292                                         softc->params.secsize,
2293                                         /*data_ptr*/ (bp->bio_flags &
2294                                         BIO_UNMAPPED) != 0 ? (void *)bp :
2295                                         bp->bio_data,
2296                                         /*dxfer_len*/ bp->bio_bcount,
2297                                         /*sense_len*/SSD_FULL_SIZE,
2298                                         da_default_timeout * 1000);
2299                         break;
2300                 case BIO_FLUSH:
2301                         /*
2302                          * BIO_FLUSH doesn't currently communicate
2303                          * range data, so we synchronize the cache
2304                          * over the whole disk.  We also force
2305                          * ordered tag semantics the flush applies
2306                          * to all previously queued I/O.
2307                          */
2308                         scsi_synchronize_cache(&start_ccb->csio,
2309                                                /*retries*/1,
2310                                                /*cbfcnp*/dadone,
2311                                                MSG_ORDERED_Q_TAG,
2312                                                /*begin_lba*/0,
2313                                                /*lb_count*/0,
2314                                                SSD_FULL_SIZE,
2315                                                da_default_timeout*1000);
2316                         break;
2317                 }
2318                 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
2319
2320 out:
2321                 /*
2322                  * Block out any asynchronous callbacks
2323                  * while we touch the pending ccb list.
2324                  */
2325                 LIST_INSERT_HEAD(&softc->pending_ccbs,
2326                                  &start_ccb->ccb_h, periph_links.le);
2327                 softc->outstanding_cmds++;
2328
2329                 /* We expect a unit attention from this device */
2330                 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
2331                         start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
2332                         softc->flags &= ~DA_FLAG_RETRY_UA;
2333                 }
2334
2335                 start_ccb->ccb_h.ccb_bp = bp;
2336                 xpt_action(start_ccb);
2337
2338                 /* May have more work to do, so ensure we stay scheduled */
2339                 daschedule(periph);
2340                 break;
2341         }
2342         case DA_STATE_PROBE_RC:
2343         {
2344                 struct scsi_read_capacity_data *rcap;
2345
2346                 rcap = (struct scsi_read_capacity_data *)
2347                     malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
2348                 if (rcap == NULL) {
2349                         printf("dastart: Couldn't malloc read_capacity data\n");
2350                         /* da_free_periph??? */
2351                         break;
2352                 }
2353                 scsi_read_capacity(&start_ccb->csio,
2354                                    /*retries*/da_retry_count,
2355                                    dadone,
2356                                    MSG_SIMPLE_Q_TAG,
2357                                    rcap,
2358                                    SSD_FULL_SIZE,
2359                                    /*timeout*/5000);
2360                 start_ccb->ccb_h.ccb_bp = NULL;
2361                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC;
2362                 xpt_action(start_ccb);
2363                 break;
2364         }
2365         case DA_STATE_PROBE_RC16:
2366         {
2367                 struct scsi_read_capacity_data_long *rcaplong;
2368
2369                 rcaplong = (struct scsi_read_capacity_data_long *)
2370                         malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
2371                 if (rcaplong == NULL) {
2372                         printf("dastart: Couldn't malloc read_capacity data\n");
2373                         /* da_free_periph??? */
2374                         break;
2375                 }
2376                 scsi_read_capacity_16(&start_ccb->csio,
2377                                       /*retries*/ da_retry_count,
2378                                       /*cbfcnp*/ dadone,
2379                                       /*tag_action*/ MSG_SIMPLE_Q_TAG,
2380                                       /*lba*/ 0,
2381                                       /*reladr*/ 0,
2382                                       /*pmi*/ 0,
2383                                       /*rcap_buf*/ (uint8_t *)rcaplong,
2384                                       /*rcap_buf_len*/ sizeof(*rcaplong),
2385                                       /*sense_len*/ SSD_FULL_SIZE,
2386                                       /*timeout*/ da_default_timeout * 1000);
2387                 start_ccb->ccb_h.ccb_bp = NULL;
2388                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC16;
2389                 xpt_action(start_ccb);
2390                 break;
2391         }
2392         case DA_STATE_PROBE_LBP:
2393         {
2394                 struct scsi_vpd_logical_block_prov *lbp;
2395
2396                 if (!scsi_vpd_supported_page(periph, SVPD_LBP)) {
2397                         /*
2398                          * If we get here we don't support any SBC-3 delete
2399                          * methods with UNMAP as the Logical Block Provisioning
2400                          * VPD page support is required for devices which
2401                          * support it according to T10/1799-D Revision 31
2402                          * however older revisions of the spec don't mandate
2403                          * this so we currently don't remove these methods
2404                          * from the available set.
2405                          */
2406                         softc->state = DA_STATE_PROBE_BLK_LIMITS;
2407                         goto skipstate;
2408                 }
2409
2410                 lbp = (struct scsi_vpd_logical_block_prov *)
2411                         malloc(sizeof(*lbp), M_SCSIDA, M_NOWAIT|M_ZERO);
2412
2413                 if (lbp == NULL) {
2414                         printf("dastart: Couldn't malloc lbp data\n");
2415                         /* da_free_periph??? */
2416                         break;
2417                 }
2418
2419                 scsi_inquiry(&start_ccb->csio,
2420                              /*retries*/da_retry_count,
2421                              /*cbfcnp*/dadone,
2422                              /*tag_action*/MSG_SIMPLE_Q_TAG,
2423                              /*inq_buf*/(u_int8_t *)lbp,
2424                              /*inq_len*/sizeof(*lbp),
2425                              /*evpd*/TRUE,
2426                              /*page_code*/SVPD_LBP,
2427                              /*sense_len*/SSD_MIN_SIZE,
2428                              /*timeout*/da_default_timeout * 1000);
2429                 start_ccb->ccb_h.ccb_bp = NULL;
2430                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_LBP;
2431                 xpt_action(start_ccb);
2432                 break;
2433         }
2434         case DA_STATE_PROBE_BLK_LIMITS:
2435         {
2436                 struct scsi_vpd_block_limits *block_limits;
2437
2438                 if (!scsi_vpd_supported_page(periph, SVPD_BLOCK_LIMITS)) {
2439                         /* Not supported skip to next probe */
2440                         softc->state = DA_STATE_PROBE_BDC;
2441                         goto skipstate;
2442                 }
2443
2444                 block_limits = (struct scsi_vpd_block_limits *)
2445                         malloc(sizeof(*block_limits), M_SCSIDA, M_NOWAIT|M_ZERO);
2446
2447                 if (block_limits == NULL) {
2448                         printf("dastart: Couldn't malloc block_limits data\n");
2449                         /* da_free_periph??? */
2450                         break;
2451                 }
2452
2453                 scsi_inquiry(&start_ccb->csio,
2454                              /*retries*/da_retry_count,
2455                              /*cbfcnp*/dadone,
2456                              /*tag_action*/MSG_SIMPLE_Q_TAG,
2457                              /*inq_buf*/(u_int8_t *)block_limits,
2458                              /*inq_len*/sizeof(*block_limits),
2459                              /*evpd*/TRUE,
2460                              /*page_code*/SVPD_BLOCK_LIMITS,
2461                              /*sense_len*/SSD_MIN_SIZE,
2462                              /*timeout*/da_default_timeout * 1000);
2463                 start_ccb->ccb_h.ccb_bp = NULL;
2464                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BLK_LIMITS;
2465                 xpt_action(start_ccb);
2466                 break;
2467         }
2468         case DA_STATE_PROBE_BDC:
2469         {
2470                 struct scsi_vpd_block_characteristics *bdc;
2471
2472                 if (!scsi_vpd_supported_page(periph, SVPD_BDC)) {
2473                         softc->state = DA_STATE_PROBE_ATA;
2474                         goto skipstate;
2475                 }
2476
2477                 bdc = (struct scsi_vpd_block_characteristics *)
2478                         malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
2479
2480                 if (bdc == NULL) {
2481                         printf("dastart: Couldn't malloc bdc data\n");
2482                         /* da_free_periph??? */
2483                         break;
2484                 }
2485
2486                 scsi_inquiry(&start_ccb->csio,
2487                              /*retries*/da_retry_count,
2488                              /*cbfcnp*/dadone,
2489                              /*tag_action*/MSG_SIMPLE_Q_TAG,
2490                              /*inq_buf*/(u_int8_t *)bdc,
2491                              /*inq_len*/sizeof(*bdc),
2492                              /*evpd*/TRUE,
2493                              /*page_code*/SVPD_BDC,
2494                              /*sense_len*/SSD_MIN_SIZE,
2495                              /*timeout*/da_default_timeout * 1000);
2496                 start_ccb->ccb_h.ccb_bp = NULL;
2497                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BDC;
2498                 xpt_action(start_ccb);
2499                 break;
2500         }
2501         case DA_STATE_PROBE_ATA:
2502         {
2503                 struct ata_params *ata_params;
2504
2505                 if (!scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
2506                         daprobedone(periph, start_ccb);
2507                         break;
2508                 }
2509
2510                 ata_params = (struct ata_params*)
2511                         malloc(sizeof(*ata_params), M_SCSIDA, M_NOWAIT|M_ZERO);
2512
2513                 if (ata_params == NULL) {
2514                         printf("dastart: Couldn't malloc ata_params data\n");
2515                         /* da_free_periph??? */
2516                         break;
2517                 }
2518
2519                 scsi_ata_identify(&start_ccb->csio,
2520                                   /*retries*/da_retry_count,
2521                                   /*cbfcnp*/dadone,
2522                                   /*tag_action*/MSG_SIMPLE_Q_TAG,
2523                                   /*data_ptr*/(u_int8_t *)ata_params,
2524                                   /*dxfer_len*/sizeof(*ata_params),
2525                                   /*sense_len*/SSD_FULL_SIZE,
2526                                   /*timeout*/da_default_timeout * 1000);
2527                 start_ccb->ccb_h.ccb_bp = NULL;
2528                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA;
2529                 xpt_action(start_ccb);
2530                 break;
2531         }
2532         }
2533 }
2534
2535 /*
2536  * In each of the methods below, while its the caller's
2537  * responsibility to ensure the request will fit into a
2538  * single device request, we might have changed the delete
2539  * method due to the device incorrectly advertising either
2540  * its supported methods or limits.
2541  * 
2542  * To prevent this causing further issues we validate the
2543  * against the methods limits, and warn which would
2544  * otherwise be unnecessary.
2545  */
2546 static void
2547 da_delete_unmap(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2548 {
2549         struct da_softc *softc = (struct da_softc *)periph->softc;;
2550         struct bio *bp1;
2551         uint8_t *buf = softc->unmap_buf;
2552         uint64_t lba, lastlba = (uint64_t)-1;
2553         uint64_t totalcount = 0;
2554         uint64_t count;
2555         uint32_t lastcount = 0, c;
2556         uint32_t off, ranges = 0;
2557
2558         /*
2559          * Currently this doesn't take the UNMAP
2560          * Granularity and Granularity Alignment
2561          * fields into account.
2562          *
2563          * This could result in both unoptimal unmap
2564          * requests as as well as UNMAP calls unmapping
2565          * fewer LBA's than requested.
2566          */
2567
2568         softc->delete_running = 1;
2569         bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2570         bp1 = bp;
2571         do {
2572                 bioq_remove(&softc->delete_queue, bp1);
2573                 if (bp1 != bp)
2574                         bioq_insert_tail(&softc->delete_run_queue, bp1);
2575                 lba = bp1->bio_pblkno;
2576                 count = bp1->bio_bcount / softc->params.secsize;
2577
2578                 /* Try to extend the previous range. */
2579                 if (lba == lastlba) {
2580                         c = min(count, softc->unmap_max_lba - lastcount);
2581                         lastcount += c;
2582                         off = ((ranges - 1) * UNMAP_RANGE_SIZE) +
2583                               UNMAP_HEAD_SIZE;
2584                         scsi_ulto4b(lastcount, &buf[off + 8]);
2585                         count -= c;
2586                         lba +=c;
2587                         totalcount += c;
2588                 }
2589
2590                 while (count > 0) {
2591                         c = min(count, softc->unmap_max_lba);
2592                         if (totalcount + c > softc->unmap_max_lba ||
2593                             ranges >= softc->unmap_max_ranges) {
2594                                 xpt_print(periph->path,
2595                                     "%s issuing short delete %ld > %ld"
2596                                     "|| %d >= %d",
2597                                     da_delete_method_desc[softc->delete_method],
2598                                     totalcount + c, softc->unmap_max_lba,
2599                                     ranges, softc->unmap_max_ranges);
2600                                 break;
2601                         }
2602                         off = (ranges * UNMAP_RANGE_SIZE) + UNMAP_HEAD_SIZE;
2603                         scsi_u64to8b(lba, &buf[off + 0]);
2604                         scsi_ulto4b(c, &buf[off + 8]);
2605                         lba += c;
2606                         totalcount += c;
2607                         ranges++;
2608                         count -= c;
2609                         lastcount = c;
2610                 }
2611                 lastlba = lba;
2612                 bp1 = bioq_first(&softc->delete_queue);
2613                 if (bp1 == NULL || ranges >= softc->unmap_max_ranges ||
2614                     totalcount + bp1->bio_bcount /
2615                     softc->params.secsize > softc->unmap_max_lba)
2616                         break;
2617         } while (1);
2618         scsi_ulto2b(ranges * 16 + 6, &buf[0]);
2619         scsi_ulto2b(ranges * 16, &buf[2]);
2620
2621         scsi_unmap(&ccb->csio,
2622                    /*retries*/da_retry_count,
2623                    /*cbfcnp*/dadone,
2624                    /*tag_action*/MSG_SIMPLE_Q_TAG,
2625                    /*byte2*/0,
2626                    /*data_ptr*/ buf,
2627                    /*dxfer_len*/ ranges * 16 + 8,
2628                    /*sense_len*/SSD_FULL_SIZE,
2629                    da_default_timeout * 1000);
2630         ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2631 }
2632
2633 static void
2634 da_delete_trim(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2635 {
2636         struct da_softc *softc = (struct da_softc *)periph->softc;
2637         struct bio *bp1;
2638         uint8_t *buf = softc->unmap_buf;
2639         uint64_t lastlba = (uint64_t)-1;
2640         uint64_t count;
2641         uint64_t lba;
2642         uint32_t lastcount = 0, c, requestcount;
2643         int ranges = 0, off, block_count;
2644
2645         softc->delete_running = 1;
2646         bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
2647         bp1 = bp;
2648         do {
2649                 bioq_remove(&softc->delete_queue, bp1);
2650                 if (bp1 != bp)
2651                         bioq_insert_tail(&softc->delete_run_queue, bp1);
2652                 lba = bp1->bio_pblkno;
2653                 count = bp1->bio_bcount / softc->params.secsize;
2654                 requestcount = count;
2655
2656                 /* Try to extend the previous range. */
2657                 if (lba == lastlba) {
2658                         c = min(count, ATA_DSM_RANGE_MAX - lastcount);
2659                         lastcount += c;
2660                         off = (ranges - 1) * 8;
2661                         buf[off + 6] = lastcount & 0xff;
2662                         buf[off + 7] = (lastcount >> 8) & 0xff;
2663                         count -= c;
2664                         lba += c;
2665                 }
2666
2667                 while (count > 0) {
2668                         c = min(count, ATA_DSM_RANGE_MAX);
2669                         off = ranges * 8;
2670
2671                         buf[off + 0] = lba & 0xff;
2672                         buf[off + 1] = (lba >> 8) & 0xff;
2673                         buf[off + 2] = (lba >> 16) & 0xff;
2674                         buf[off + 3] = (lba >> 24) & 0xff;
2675                         buf[off + 4] = (lba >> 32) & 0xff;
2676                         buf[off + 5] = (lba >> 40) & 0xff;
2677                         buf[off + 6] = c & 0xff;
2678                         buf[off + 7] = (c >> 8) & 0xff;
2679                         lba += c;
2680                         ranges++;
2681                         count -= c;
2682                         lastcount = c;
2683                         if (count != 0 && ranges == softc->trim_max_ranges) {
2684                                 xpt_print(periph->path,
2685                                     "%s issuing short delete %ld > %ld",
2686                                     da_delete_method_desc[softc->delete_method],
2687                                     requestcount,
2688                                     (softc->trim_max_ranges - ranges) *
2689                                     ATA_DSM_RANGE_MAX);
2690                                 break;
2691                         }
2692                 }
2693                 lastlba = lba;
2694                 bp1 = bioq_first(&softc->delete_queue);
2695                 if (bp1 == NULL || bp1->bio_bcount / softc->params.secsize >
2696                     (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX)
2697                         break;
2698         } while (1);
2699
2700         block_count = (ranges + ATA_DSM_BLK_RANGES - 1) / ATA_DSM_BLK_RANGES;
2701         scsi_ata_trim(&ccb->csio,
2702                       /*retries*/da_retry_count,
2703                       /*cbfcnp*/dadone,
2704                       /*tag_action*/MSG_SIMPLE_Q_TAG,
2705                       block_count,
2706                       /*data_ptr*/buf,
2707                       /*dxfer_len*/block_count * ATA_DSM_BLK_SIZE,
2708                       /*sense_len*/SSD_FULL_SIZE,
2709                       da_default_timeout * 1000);
2710         ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2711 }
2712
2713 /*
2714  * We calculate ws_max_blks here based off d_delmaxsize instead
2715  * of using softc->ws_max_blks as it is absolute max for the
2716  * device not the protocol max which may well be lower
2717  */
2718 static void
2719 da_delete_ws(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
2720 {
2721         struct da_softc *softc;
2722         struct bio *bp1;
2723         uint64_t ws_max_blks;
2724         uint64_t lba;
2725         uint64_t count; /* forward compat with WS32 */
2726
2727         softc = (struct da_softc *)periph->softc;
2728         ws_max_blks = softc->disk->d_delmaxsize / softc->params.secsize;
2729         softc->delete_running = 1;
2730         lba = bp->bio_pblkno;
2731         count = 0;
2732         bp1 = bp;
2733         do {
2734                 bioq_remove(&softc->delete_queue, bp1);
2735                 if (bp1 != bp)
2736                         bioq_insert_tail(&softc->delete_run_queue, bp1);
2737                 count += bp1->bio_bcount / softc->params.secsize;
2738                 if (count > ws_max_blks) {
2739                         count = min(count, ws_max_blks);
2740                         xpt_print(periph->path,
2741                             "%s issuing short delete %ld > %ld",
2742                             da_delete_method_desc[softc->delete_method],
2743                             count, ws_max_blks);
2744                         break;
2745                 }
2746                 bp1 = bioq_first(&softc->delete_queue);
2747                 if (bp1 == NULL || lba + count != bp1->bio_pblkno ||
2748                     count + bp1->bio_bcount /
2749                     softc->params.secsize > ws_max_blks)
2750                         break;
2751         } while (1);
2752
2753         scsi_write_same(&ccb->csio,
2754                         /*retries*/da_retry_count,
2755                         /*cbfcnp*/dadone,
2756                         /*tag_action*/MSG_SIMPLE_Q_TAG,
2757                         /*byte2*/softc->delete_method ==
2758                             DA_DELETE_ZERO ? 0 : SWS_UNMAP,
2759                         softc->delete_method == DA_DELETE_WS16 ? 16 : 10,
2760                         /*lba*/lba,
2761                         /*block_count*/count,
2762                         /*data_ptr*/ __DECONST(void *, zero_region),
2763                         /*dxfer_len*/ softc->params.secsize,
2764                         /*sense_len*/SSD_FULL_SIZE,
2765                         da_default_timeout * 1000);
2766         ccb->ccb_h.ccb_state = DA_CCB_DELETE;
2767 }
2768
2769 static int
2770 cmd6workaround(union ccb *ccb)
2771 {
2772         struct scsi_rw_6 cmd6;
2773         struct scsi_rw_10 *cmd10;
2774         struct da_softc *softc;
2775         u_int8_t *cdb;
2776         struct bio *bp;
2777         int frozen;
2778
2779         cdb = ccb->csio.cdb_io.cdb_bytes;
2780         softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
2781
2782         if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
2783                 da_delete_methods old_method = softc->delete_method;
2784
2785                 /*
2786                  * Typically there are two reasons for failure here
2787                  * 1. Delete method was detected as supported but isn't
2788                  * 2. Delete failed due to invalid params e.g. too big
2789                  *
2790                  * While we will attempt to choose an alternative delete method
2791                  * this may result in short deletes if the existing delete
2792                  * requests from geom are big for the new method choosen.
2793                  *
2794                  * This method assumes that the error which triggered this
2795                  * will not retry the io otherwise a panic will occur
2796                  */
2797                 dadeleteflag(softc, old_method, 0);
2798                 dadeletemethodchoose(softc, DA_DELETE_DISABLE);
2799                 if (softc->delete_method == DA_DELETE_DISABLE)
2800                         xpt_print(ccb->ccb_h.path,
2801                                   "%s failed, disabling BIO_DELETE\n",
2802                                   da_delete_method_desc[old_method]);
2803                 else
2804                         xpt_print(ccb->ccb_h.path,
2805                                   "%s failed, switching to %s BIO_DELETE\n",
2806                                   da_delete_method_desc[old_method],
2807                                   da_delete_method_desc[softc->delete_method]);
2808
2809                 if (DA_SIO) {
2810                         while ((bp = bioq_takefirst(&softc->delete_run_queue))
2811                             != NULL)
2812                                 bioq_disksort(&softc->delete_queue, bp);
2813                 } else {
2814                         while ((bp = bioq_takefirst(&softc->delete_run_queue))
2815                             != NULL)
2816                                 bioq_insert_tail(&softc->delete_queue, bp);
2817                 }
2818                 bioq_insert_tail(&softc->delete_queue,
2819                     (struct bio *)ccb->ccb_h.ccb_bp);
2820                 ccb->ccb_h.ccb_bp = NULL;
2821                 return (0);
2822         }
2823
2824         /* Detect unsupported PREVENT ALLOW MEDIUM REMOVAL. */
2825         if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
2826             (*cdb == PREVENT_ALLOW) &&
2827             (softc->quirks & DA_Q_NO_PREVENT) == 0) {
2828                 if (bootverbose)
2829                         xpt_print(ccb->ccb_h.path,
2830                             "PREVENT ALLOW MEDIUM REMOVAL not supported.\n");
2831                 softc->quirks |= DA_Q_NO_PREVENT;
2832                 return (0);
2833         }
2834
2835         /* Detect unsupported SYNCHRONIZE CACHE(10). */
2836         if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
2837             (*cdb == SYNCHRONIZE_CACHE) &&
2838             (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
2839                 if (bootverbose)
2840                         xpt_print(ccb->ccb_h.path,
2841                             "SYNCHRONIZE CACHE(10) not supported.\n");
2842                 softc->quirks |= DA_Q_NO_SYNC_CACHE;
2843                 softc->disk->d_flags &= ~DISKFLAG_CANFLUSHCACHE;
2844                 return (0);
2845         }
2846
2847         /* Translation only possible if CDB is an array and cmd is R/W6 */
2848         if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
2849             (*cdb != READ_6 && *cdb != WRITE_6))
2850                 return 0;
2851
2852         xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
2853             "increasing minimum_cmd_size to 10.\n");
2854         softc->minimum_cmd_size = 10;
2855
2856         bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
2857         cmd10 = (struct scsi_rw_10 *)cdb;
2858         cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
2859         cmd10->byte2 = 0;
2860         scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
2861         cmd10->reserved = 0;
2862         scsi_ulto2b(cmd6.length, cmd10->length);
2863         cmd10->control = cmd6.control;
2864         ccb->csio.cdb_len = sizeof(*cmd10);
2865
2866         /* Requeue request, unfreezing queue if necessary */
2867         frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
2868         ccb->ccb_h.status = CAM_REQUEUE_REQ;
2869         xpt_action(ccb);
2870         if (frozen) {
2871                 cam_release_devq(ccb->ccb_h.path,
2872                                  /*relsim_flags*/0,
2873                                  /*reduction*/0,
2874                                  /*timeout*/0,
2875                                  /*getcount_only*/0);
2876         }
2877         return (ERESTART);
2878 }
2879
2880 static void
2881 dadone(struct cam_periph *periph, union ccb *done_ccb)
2882 {
2883         struct da_softc *softc;
2884         struct ccb_scsiio *csio;
2885         u_int32_t  priority;
2886         da_ccb_state state;
2887
2888         softc = (struct da_softc *)periph->softc;
2889         priority = done_ccb->ccb_h.pinfo.priority;
2890
2891         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
2892
2893         csio = &done_ccb->csio;
2894         state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
2895         switch (state) {
2896         case DA_CCB_BUFFER_IO:
2897         case DA_CCB_DELETE:
2898         {
2899                 struct bio *bp, *bp1;
2900
2901                 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2902                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2903                         int error;
2904                         int sf;
2905
2906                         if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
2907                                 sf = SF_RETRY_UA;
2908                         else
2909                                 sf = 0;
2910
2911                         error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
2912                         if (error == ERESTART) {
2913                                 /*
2914                                  * A retry was scheduled, so
2915                                  * just return.
2916                                  */
2917                                 return;
2918                         }
2919                         bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2920                         if (error != 0) {
2921                                 int queued_error;
2922
2923                                 /*
2924                                  * return all queued I/O with EIO, so that
2925                                  * the client can retry these I/Os in the
2926                                  * proper order should it attempt to recover.
2927                                  */
2928                                 queued_error = EIO;
2929
2930                                 if (error == ENXIO
2931                                  && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
2932                                         /*
2933                                          * Catastrophic error.  Mark our pack as
2934                                          * invalid.
2935                                          */
2936                                         /*
2937                                          * XXX See if this is really a media
2938                                          * XXX change first?
2939                                          */
2940                                         xpt_print(periph->path,
2941                                             "Invalidating pack\n");
2942                                         softc->flags |= DA_FLAG_PACK_INVALID;
2943                                         queued_error = ENXIO;
2944                                 }
2945                                 bioq_flush(&softc->bio_queue, NULL,
2946                                            queued_error);
2947                                 if (bp != NULL) {
2948                                         bp->bio_error = error;
2949                                         bp->bio_resid = bp->bio_bcount;
2950                                         bp->bio_flags |= BIO_ERROR;
2951                                 }
2952                         } else if (bp != NULL) {
2953                                 if (state == DA_CCB_DELETE)
2954                                         bp->bio_resid = 0;
2955                                 else
2956                                         bp->bio_resid = csio->resid;
2957                                 bp->bio_error = 0;
2958                                 if (bp->bio_resid != 0)
2959                                         bp->bio_flags |= BIO_ERROR;
2960                         }
2961                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2962                                 cam_release_devq(done_ccb->ccb_h.path,
2963                                                  /*relsim_flags*/0,
2964                                                  /*reduction*/0,
2965                                                  /*timeout*/0,
2966                                                  /*getcount_only*/0);
2967                 } else if (bp != NULL) {
2968                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2969                                 panic("REQ_CMP with QFRZN");
2970                         if (state == DA_CCB_DELETE)
2971                                 bp->bio_resid = 0;
2972                         else
2973                                 bp->bio_resid = csio->resid;
2974                         if (csio->resid > 0)
2975                                 bp->bio_flags |= BIO_ERROR;
2976                         if (softc->error_inject != 0) {
2977                                 bp->bio_error = softc->error_inject;
2978                                 bp->bio_resid = bp->bio_bcount;
2979                                 bp->bio_flags |= BIO_ERROR;
2980                                 softc->error_inject = 0;
2981                         }
2982                 }
2983
2984                 /*
2985                  * Block out any asynchronous callbacks
2986                  * while we touch the pending ccb list.
2987                  */
2988                 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
2989                 softc->outstanding_cmds--;
2990                 if (softc->outstanding_cmds == 0)
2991                         softc->flags |= DA_FLAG_WENT_IDLE;
2992
2993                 if (state == DA_CCB_DELETE) {
2994                         while ((bp1 = bioq_takefirst(&softc->delete_run_queue))
2995                             != NULL) {
2996                                 bp1->bio_error = bp->bio_error;
2997                                 if (bp->bio_flags & BIO_ERROR) {
2998                                         bp1->bio_flags |= BIO_ERROR;
2999                                         bp1->bio_resid = bp1->bio_bcount;
3000                                 } else
3001                                         bp1->bio_resid = 0;
3002                                 biodone(bp1);
3003                         }
3004                         softc->delete_running = 0;
3005                         if (bp != NULL)
3006                                 biodone(bp);
3007                         daschedule(periph);
3008                 } else if (bp != NULL)
3009                         biodone(bp);
3010                 break;
3011         }
3012         case DA_CCB_PROBE_RC:
3013         case DA_CCB_PROBE_RC16:
3014         {
3015                 struct     scsi_read_capacity_data *rdcap;
3016                 struct     scsi_read_capacity_data_long *rcaplong;
3017                 char       announce_buf[80];
3018                 int        lbp;
3019
3020                 lbp = 0;
3021                 rdcap = NULL;
3022                 rcaplong = NULL;
3023                 if (state == DA_CCB_PROBE_RC)
3024                         rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
3025                 else
3026                         rcaplong = (struct scsi_read_capacity_data_long *)
3027                                 csio->data_ptr;
3028
3029                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3030                         struct disk_params *dp;
3031                         uint32_t block_size;
3032                         uint64_t maxsector;
3033                         u_int lbppbe;   /* LB per physical block exponent. */
3034                         u_int lalba;    /* Lowest aligned LBA. */
3035
3036                         if (state == DA_CCB_PROBE_RC) {
3037                                 block_size = scsi_4btoul(rdcap->length);
3038                                 maxsector = scsi_4btoul(rdcap->addr);
3039                                 lbppbe = 0;
3040                                 lalba = 0;
3041
3042                                 /*
3043                                  * According to SBC-2, if the standard 10
3044                                  * byte READ CAPACITY command returns 2^32,
3045                                  * we should issue the 16 byte version of
3046                                  * the command, since the device in question
3047                                  * has more sectors than can be represented
3048                                  * with the short version of the command.
3049                                  */
3050                                 if (maxsector == 0xffffffff) {
3051                                         free(rdcap, M_SCSIDA);
3052                                         xpt_release_ccb(done_ccb);
3053                                         softc->state = DA_STATE_PROBE_RC16;
3054                                         xpt_schedule(periph, priority);
3055                                         return;
3056                                 }
3057                         } else {
3058                                 block_size = scsi_4btoul(rcaplong->length);
3059                                 maxsector = scsi_8btou64(rcaplong->addr);
3060                                 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
3061                                 lalba = scsi_2btoul(rcaplong->lalba_lbp);
3062                         }
3063
3064                         /*
3065                          * Because GEOM code just will panic us if we
3066                          * give them an 'illegal' value we'll avoid that
3067                          * here.
3068                          */
3069                         if (block_size == 0 && maxsector == 0) {
3070                                 block_size = 512;
3071                                 maxsector = -1;
3072                         }
3073                         if (block_size >= MAXPHYS || block_size == 0) {
3074                                 xpt_print(periph->path,
3075                                     "unsupportable block size %ju\n",
3076                                     (uintmax_t) block_size);
3077                                 announce_buf[0] = '\0';
3078                                 cam_periph_invalidate(periph);
3079                         } else {
3080                                 /*
3081                                  * We pass rcaplong into dasetgeom(),
3082                                  * because it will only use it if it is
3083                                  * non-NULL.
3084                                  */
3085                                 dasetgeom(periph, block_size, maxsector,
3086                                           rcaplong, sizeof(*rcaplong));
3087                                 lbp = (lalba & SRC16_LBPME_A);
3088                                 dp = &softc->params;
3089                                 snprintf(announce_buf, sizeof(announce_buf),
3090                                         "%juMB (%ju %u byte sectors: %dH %dS/T "
3091                                         "%dC)", (uintmax_t)
3092                                         (((uintmax_t)dp->secsize *
3093                                         dp->sectors) / (1024*1024)),
3094                                         (uintmax_t)dp->sectors,
3095                                         dp->secsize, dp->heads,
3096                                         dp->secs_per_track, dp->cylinders);
3097                         }
3098                 } else {
3099                         int     error;
3100
3101                         announce_buf[0] = '\0';
3102
3103                         /*
3104                          * Retry any UNIT ATTENTION type errors.  They
3105                          * are expected at boot.
3106                          */
3107                         error = daerror(done_ccb, CAM_RETRY_SELTO,
3108                                         SF_RETRY_UA|SF_NO_PRINT);
3109                         if (error == ERESTART) {
3110                                 /*
3111                                  * A retry was scheuled, so
3112                                  * just return.
3113                                  */
3114                                 return;
3115                         } else if (error != 0) {
3116                                 int asc, ascq;
3117                                 int sense_key, error_code;
3118                                 int have_sense;
3119                                 cam_status status;
3120                                 struct ccb_getdev cgd;
3121
3122                                 /* Don't wedge this device's queue */
3123                                 status = done_ccb->ccb_h.status;
3124                                 if ((status & CAM_DEV_QFRZN) != 0)
3125                                         cam_release_devq(done_ccb->ccb_h.path,
3126                                                          /*relsim_flags*/0,
3127                                                          /*reduction*/0,
3128                                                          /*timeout*/0,
3129                                                          /*getcount_only*/0);
3130
3131
3132                                 xpt_setup_ccb(&cgd.ccb_h, 
3133                                               done_ccb->ccb_h.path,
3134                                               CAM_PRIORITY_NORMAL);
3135                                 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
3136                                 xpt_action((union ccb *)&cgd);
3137
3138                                 if (scsi_extract_sense_ccb(done_ccb,
3139                                     &error_code, &sense_key, &asc, &ascq))
3140                                         have_sense = TRUE;
3141                                 else
3142                                         have_sense = FALSE;
3143
3144                                 /*
3145                                  * If we tried READ CAPACITY(16) and failed,
3146                                  * fallback to READ CAPACITY(10).
3147                                  */
3148                                 if ((state == DA_CCB_PROBE_RC16) &&
3149                                     (softc->flags & DA_FLAG_CAN_RC16) &&
3150                                     (((csio->ccb_h.status & CAM_STATUS_MASK) ==
3151                                         CAM_REQ_INVALID) ||
3152                                      ((have_sense) &&
3153                                       (error_code == SSD_CURRENT_ERROR) &&
3154                                       (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
3155                                         softc->flags &= ~DA_FLAG_CAN_RC16;
3156                                         free(rdcap, M_SCSIDA);
3157                                         xpt_release_ccb(done_ccb);
3158                                         softc->state = DA_STATE_PROBE_RC;
3159                                         xpt_schedule(periph, priority);
3160                                         return;
3161                                 } else
3162                                 /*
3163                                  * Attach to anything that claims to be a
3164                                  * direct access or optical disk device,
3165                                  * as long as it doesn't return a "Logical
3166                                  * unit not supported" (0x25) error.
3167                                  */
3168                                 if ((have_sense) && (asc != 0x25)
3169                                  && (error_code == SSD_CURRENT_ERROR)) {
3170                                         const char *sense_key_desc;
3171                                         const char *asc_desc;
3172
3173                                         dasetgeom(periph, 512, -1, NULL, 0);
3174                                         scsi_sense_desc(sense_key, asc, ascq,
3175                                                         &cgd.inq_data,
3176                                                         &sense_key_desc,
3177                                                         &asc_desc);
3178                                         snprintf(announce_buf,
3179                                             sizeof(announce_buf),
3180                                                 "Attempt to query device "
3181                                                 "size failed: %s, %s",
3182                                                 sense_key_desc,
3183                                                 asc_desc);
3184                                 } else { 
3185                                         if (have_sense)
3186                                                 scsi_sense_print(
3187                                                         &done_ccb->csio);
3188                                         else {
3189                                                 xpt_print(periph->path,
3190                                                     "got CAM status %#x\n",
3191                                                     done_ccb->ccb_h.status);
3192                                         }
3193
3194                                         xpt_print(periph->path, "fatal error, "
3195                                             "failed to attach to device\n");
3196
3197                                         /*
3198                                          * Free up resources.
3199                                          */
3200                                         cam_periph_invalidate(periph);
3201                                 } 
3202                         }
3203                 }
3204                 free(csio->data_ptr, M_SCSIDA);
3205                 if (announce_buf[0] != '\0' && ((softc->flags & DA_FLAG_PROBED) == 0)) {
3206                         /*
3207                          * Create our sysctl variables, now that we know
3208                          * we have successfully attached.
3209                          */
3210                         /* increase the refcount */
3211                         if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3212                                 taskqueue_enqueue(taskqueue_thread,
3213                                                   &softc->sysctl_task);
3214                                 xpt_announce_periph(periph, announce_buf);
3215                                 xpt_announce_quirks(periph, softc->quirks,
3216                                     DA_Q_BIT_STRING);
3217                         } else {
3218                                 xpt_print(periph->path, "fatal error, "
3219                                     "could not acquire reference count\n");
3220                         }
3221                 }
3222
3223                 /* Ensure re-probe doesn't see old delete. */
3224                 softc->delete_available = 0;
3225                 if (lbp) {
3226                         /*
3227                          * Based on older SBC-3 spec revisions
3228                          * any of the UNMAP methods "may" be
3229                          * available via LBP given this flag so
3230                          * we flag all of them as availble and
3231                          * then remove those which further
3232                          * probes confirm aren't available
3233                          * later.
3234                          *
3235                          * We could also check readcap(16) p_type
3236                          * flag to exclude one or more invalid
3237                          * write same (X) types here
3238                          */
3239                         dadeleteflag(softc, DA_DELETE_WS16, 1);
3240                         dadeleteflag(softc, DA_DELETE_WS10, 1);
3241                         dadeleteflag(softc, DA_DELETE_ZERO, 1);
3242                         dadeleteflag(softc, DA_DELETE_UNMAP, 1);
3243
3244                         xpt_release_ccb(done_ccb);
3245                         softc->state = DA_STATE_PROBE_LBP;
3246                         xpt_schedule(periph, priority);
3247                         return;
3248                 }
3249
3250                 xpt_release_ccb(done_ccb);
3251                 softc->state = DA_STATE_PROBE_BDC;
3252                 xpt_schedule(periph, priority);
3253                 return;
3254         }
3255         case DA_CCB_PROBE_LBP:
3256         {
3257                 struct scsi_vpd_logical_block_prov *lbp;
3258
3259                 lbp = (struct scsi_vpd_logical_block_prov *)csio->data_ptr;
3260
3261                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3262                         /*
3263                          * T10/1799-D Revision 31 states at least one of these
3264                          * must be supported but we don't currently enforce this.
3265                          */
3266                         dadeleteflag(softc, DA_DELETE_WS16,
3267                                      (lbp->flags & SVPD_LBP_WS16));
3268                         dadeleteflag(softc, DA_DELETE_WS10,
3269                                      (lbp->flags & SVPD_LBP_WS10));
3270                         dadeleteflag(softc, DA_DELETE_ZERO,
3271                                      (lbp->flags & SVPD_LBP_WS10));
3272                         dadeleteflag(softc, DA_DELETE_UNMAP,
3273                                      (lbp->flags & SVPD_LBP_UNMAP));
3274
3275                         if (lbp->flags & SVPD_LBP_UNMAP) {
3276                                 free(lbp, M_SCSIDA);
3277                                 xpt_release_ccb(done_ccb);
3278                                 softc->state = DA_STATE_PROBE_BLK_LIMITS;
3279                                 xpt_schedule(periph, priority);
3280                                 return;
3281                         }
3282                 } else {
3283                         int error;
3284                         error = daerror(done_ccb, CAM_RETRY_SELTO,
3285                                         SF_RETRY_UA|SF_NO_PRINT);
3286                         if (error == ERESTART)
3287                                 return;
3288                         else if (error != 0) {
3289                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3290                                         /* Don't wedge this device's queue */
3291                                         cam_release_devq(done_ccb->ccb_h.path,
3292                                                          /*relsim_flags*/0,
3293                                                          /*reduction*/0,
3294                                                          /*timeout*/0,
3295                                                          /*getcount_only*/0);
3296                                 }
3297
3298                                 /*
3299                                  * Failure indicates we don't support any SBC-3
3300                                  * delete methods with UNMAP
3301                                  */
3302                         }
3303                 }
3304
3305                 free(lbp, M_SCSIDA);
3306                 xpt_release_ccb(done_ccb);
3307                 softc->state = DA_STATE_PROBE_BDC;
3308                 xpt_schedule(periph, priority);
3309                 return;
3310         }
3311         case DA_CCB_PROBE_BLK_LIMITS:
3312         {
3313                 struct scsi_vpd_block_limits *block_limits;
3314
3315                 block_limits = (struct scsi_vpd_block_limits *)csio->data_ptr;
3316
3317                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3318                         uint32_t max_unmap_lba_cnt = scsi_4btoul(
3319                                 block_limits->max_unmap_lba_cnt);
3320                         uint32_t max_unmap_blk_cnt = scsi_4btoul(
3321                                 block_limits->max_unmap_blk_cnt);
3322                         uint64_t ws_max_blks = scsi_8btou64(
3323                                 block_limits->max_write_same_length);
3324                         /*
3325                          * We should already support UNMAP but we check lba
3326                          * and block count to be sure
3327                          */
3328                         if (max_unmap_lba_cnt != 0x00L &&
3329                             max_unmap_blk_cnt != 0x00L) {
3330                                 softc->unmap_max_lba = max_unmap_lba_cnt;
3331                                 softc->unmap_max_ranges = min(max_unmap_blk_cnt,
3332                                         UNMAP_MAX_RANGES);
3333                         } else {
3334                                 /*
3335                                  * Unexpected UNMAP limits which means the
3336                                  * device doesn't actually support UNMAP
3337                                  */
3338                                 dadeleteflag(softc, DA_DELETE_UNMAP, 0);
3339                         }
3340
3341                         if (ws_max_blks != 0x00L)
3342                                 softc->ws_max_blks = ws_max_blks;
3343                 } else {
3344                         int error;
3345                         error = daerror(done_ccb, CAM_RETRY_SELTO,
3346                                         SF_RETRY_UA|SF_NO_PRINT);
3347                         if (error == ERESTART)
3348                                 return;
3349                         else if (error != 0) {
3350                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3351                                         /* Don't wedge this device's queue */
3352                                         cam_release_devq(done_ccb->ccb_h.path,
3353                                                          /*relsim_flags*/0,
3354                                                          /*reduction*/0,
3355                                                          /*timeout*/0,
3356                                                          /*getcount_only*/0);
3357                                 }
3358
3359                                 /*
3360                                  * Failure here doesn't mean UNMAP is not
3361                                  * supported as this is an optional page.
3362                                  */
3363                                 softc->unmap_max_lba = 1;
3364                                 softc->unmap_max_ranges = 1;
3365                         }
3366                 }
3367
3368                 free(block_limits, M_SCSIDA);
3369                 xpt_release_ccb(done_ccb);
3370                 softc->state = DA_STATE_PROBE_BDC;
3371                 xpt_schedule(periph, priority);
3372                 return;
3373         }
3374         case DA_CCB_PROBE_BDC:
3375         {
3376                 struct scsi_vpd_block_characteristics *bdc;
3377
3378                 bdc = (struct scsi_vpd_block_characteristics *)csio->data_ptr;
3379
3380                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3381                         /*
3382                          * Disable queue sorting for non-rotational media
3383                          * by default.
3384                          */
3385                         if (scsi_2btoul(bdc->medium_rotation_rate) ==
3386                             SVPD_BDC_RATE_NONE_ROTATING)
3387                                 softc->sort_io_queue = 0;
3388                 } else {
3389                         int error;
3390                         error = daerror(done_ccb, CAM_RETRY_SELTO,
3391                                         SF_RETRY_UA|SF_NO_PRINT);
3392                         if (error == ERESTART)
3393                                 return;
3394                         else if (error != 0) {
3395                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3396                                         /* Don't wedge this device's queue */
3397                                         cam_release_devq(done_ccb->ccb_h.path,
3398                                                          /*relsim_flags*/0,
3399                                                          /*reduction*/0,
3400                                                          /*timeout*/0,
3401                                                          /*getcount_only*/0);
3402                                 }
3403                         }
3404                 }
3405
3406                 free(bdc, M_SCSIDA);
3407                 xpt_release_ccb(done_ccb);
3408                 softc->state = DA_STATE_PROBE_ATA;
3409                 xpt_schedule(periph, priority);
3410                 return;
3411         }
3412         case DA_CCB_PROBE_ATA:
3413         {
3414                 int i;
3415                 struct ata_params *ata_params;
3416                 int16_t *ptr;
3417
3418                 ata_params = (struct ata_params *)csio->data_ptr;
3419                 ptr = (uint16_t *)ata_params;
3420
3421                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3422                         for (i = 0; i < sizeof(*ata_params) / 2; i++)
3423                                 ptr[i] = le16toh(ptr[i]);
3424                         if (ata_params->support_dsm & ATA_SUPPORT_DSM_TRIM) {
3425                                 dadeleteflag(softc, DA_DELETE_ATA_TRIM, 1);
3426                                 if (ata_params->max_dsm_blocks != 0)
3427                                         softc->trim_max_ranges = min(
3428                                           softc->trim_max_ranges,
3429                                           ata_params->max_dsm_blocks *
3430                                           ATA_DSM_BLK_RANGES);
3431                         }
3432                         /*
3433                          * Disable queue sorting for non-rotational media
3434                          * by default.
3435                          */
3436                         if (ata_params->media_rotation_rate == 1)
3437                                 softc->sort_io_queue = 0;
3438                 } else {
3439                         int error;
3440                         error = daerror(done_ccb, CAM_RETRY_SELTO,
3441                                         SF_RETRY_UA|SF_NO_PRINT);
3442                         if (error == ERESTART)
3443                                 return;
3444                         else if (error != 0) {
3445                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3446                                         /* Don't wedge this device's queue */
3447                                         cam_release_devq(done_ccb->ccb_h.path,
3448                                                          /*relsim_flags*/0,
3449                                                          /*reduction*/0,
3450                                                          /*timeout*/0,
3451                                                          /*getcount_only*/0);
3452                                 }
3453                         }
3454                 }
3455
3456                 free(ata_params, M_SCSIDA);
3457                 daprobedone(periph, done_ccb);
3458                 return;
3459         }
3460         case DA_CCB_WAITING:
3461         {
3462                 /* Caller will release the CCB */
3463                 wakeup(&done_ccb->ccb_h.cbfcnp);
3464                 return;
3465         }
3466         case DA_CCB_DUMP:
3467                 /* No-op.  We're polling */
3468                 return;
3469         case DA_CCB_TUR:
3470         {
3471                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3472
3473                         if (daerror(done_ccb, CAM_RETRY_SELTO,
3474                             SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
3475                             ERESTART)
3476                                 return;
3477                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3478                                 cam_release_devq(done_ccb->ccb_h.path,
3479                                                  /*relsim_flags*/0,
3480                                                  /*reduction*/0,
3481                                                  /*timeout*/0,
3482                                                  /*getcount_only*/0);
3483                 }
3484                 xpt_release_ccb(done_ccb);
3485                 cam_periph_release_locked(periph);
3486                 return;
3487         }
3488         default:
3489                 break;
3490         }
3491         xpt_release_ccb(done_ccb);
3492 }
3493
3494 static void
3495 dareprobe(struct cam_periph *periph)
3496 {
3497         struct da_softc   *softc;
3498         cam_status status;
3499
3500         softc = (struct da_softc *)periph->softc;
3501
3502         /* Probe in progress; don't interfere. */
3503         if (softc->state != DA_STATE_NORMAL)
3504                 return;
3505
3506         status = cam_periph_acquire(periph);
3507         KASSERT(status == CAM_REQ_CMP,
3508             ("dareprobe: cam_periph_acquire failed"));
3509
3510         if (softc->flags & DA_FLAG_CAN_RC16)
3511                 softc->state = DA_STATE_PROBE_RC16;
3512         else
3513                 softc->state = DA_STATE_PROBE_RC;
3514
3515         xpt_schedule(periph, CAM_PRIORITY_DEV);
3516 }
3517
3518 static int
3519 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
3520 {
3521         struct da_softc   *softc;
3522         struct cam_periph *periph;
3523         int error, error_code, sense_key, asc, ascq;
3524
3525         periph = xpt_path_periph(ccb->ccb_h.path);
3526         softc = (struct da_softc *)periph->softc;
3527
3528         /*
3529          * Automatically detect devices that do not support
3530          * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
3531          */
3532         error = 0;
3533         if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
3534                 error = cmd6workaround(ccb);
3535         } else if (scsi_extract_sense_ccb(ccb,
3536             &error_code, &sense_key, &asc, &ascq)) {
3537                 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
3538                         error = cmd6workaround(ccb);
3539                 /*
3540                  * If the target replied with CAPACITY DATA HAS CHANGED UA,
3541                  * query the capacity and notify upper layers.
3542                  */
3543                 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3544                     asc == 0x2A && ascq == 0x09) {
3545                         xpt_print(periph->path, "capacity data has changed\n");
3546                         dareprobe(periph);
3547                         sense_flags |= SF_NO_PRINT;
3548                 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
3549                     asc == 0x28 && ascq == 0x00)
3550                         disk_media_changed(softc->disk, M_NOWAIT);
3551                 else if (sense_key == SSD_KEY_NOT_READY &&
3552                     asc == 0x3a && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
3553                         softc->flags |= DA_FLAG_PACK_INVALID;
3554                         disk_media_gone(softc->disk, M_NOWAIT);
3555                 }
3556         }
3557         if (error == ERESTART)
3558                 return (ERESTART);
3559
3560         /*
3561          * XXX
3562          * Until we have a better way of doing pack validation,
3563          * don't treat UAs as errors.
3564          */
3565         sense_flags |= SF_RETRY_UA;
3566         return(cam_periph_error(ccb, cam_flags, sense_flags,
3567                                 &softc->saved_ccb));
3568 }
3569
3570 static void
3571 damediapoll(void *arg)
3572 {
3573         struct cam_periph *periph = arg;
3574         struct da_softc *softc = periph->softc;
3575
3576         if (!softc->tur && softc->outstanding_cmds == 0) {
3577                 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
3578                         softc->tur = 1;
3579                         daschedule(periph);
3580                 }
3581         }
3582         /* Queue us up again */
3583         if (da_poll_period != 0)
3584                 callout_schedule(&softc->mediapoll_c, da_poll_period * hz);
3585 }
3586
3587 static void
3588 daprevent(struct cam_periph *periph, int action)
3589 {
3590         struct  da_softc *softc;
3591         union   ccb *ccb;               
3592         int     error;
3593                 
3594         softc = (struct da_softc *)periph->softc;
3595
3596         if (((action == PR_ALLOW)
3597           && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
3598          || ((action == PR_PREVENT)
3599           && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
3600                 return;
3601         }
3602
3603         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3604
3605         scsi_prevent(&ccb->csio,
3606                      /*retries*/1,
3607                      /*cbcfp*/dadone,
3608                      MSG_SIMPLE_Q_TAG,
3609                      action,
3610                      SSD_FULL_SIZE,
3611                      5000);
3612
3613         error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
3614             SF_RETRY_UA | SF_NO_PRINT, softc->disk->d_devstat);
3615
3616         if (error == 0) {
3617                 if (action == PR_ALLOW)
3618                         softc->flags &= ~DA_FLAG_PACK_LOCKED;
3619                 else
3620                         softc->flags |= DA_FLAG_PACK_LOCKED;
3621         }
3622
3623         xpt_release_ccb(ccb);
3624 }
3625
3626 static void
3627 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
3628           struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
3629 {
3630         struct ccb_calc_geometry ccg;
3631         struct da_softc *softc;
3632         struct disk_params *dp;
3633         u_int lbppbe, lalba;
3634         int error;
3635
3636         softc = (struct da_softc *)periph->softc;
3637
3638         dp = &softc->params;
3639         dp->secsize = block_len;
3640         dp->sectors = maxsector + 1;
3641         if (rcaplong != NULL) {
3642                 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
3643                 lalba = scsi_2btoul(rcaplong->lalba_lbp);
3644                 lalba &= SRC16_LALBA_A;
3645         } else {
3646                 lbppbe = 0;
3647                 lalba = 0;
3648         }
3649
3650         if (lbppbe > 0) {
3651                 dp->stripesize = block_len << lbppbe;
3652                 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
3653                     dp->stripesize;
3654         } else if (softc->quirks & DA_Q_4K) {
3655                 dp->stripesize = 4096;
3656                 dp->stripeoffset = 0;
3657         } else {
3658                 dp->stripesize = 0;
3659                 dp->stripeoffset = 0;
3660         }
3661         /*
3662          * Have the controller provide us with a geometry
3663          * for this disk.  The only time the geometry
3664          * matters is when we boot and the controller
3665          * is the only one knowledgeable enough to come
3666          * up with something that will make this a bootable
3667          * device.
3668          */
3669         xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3670         ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
3671         ccg.block_size = dp->secsize;
3672         ccg.volume_size = dp->sectors;
3673         ccg.heads = 0;
3674         ccg.secs_per_track = 0;
3675         ccg.cylinders = 0;
3676         xpt_action((union ccb*)&ccg);
3677         if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3678                 /*
3679                  * We don't know what went wrong here- but just pick
3680                  * a geometry so we don't have nasty things like divide
3681                  * by zero.
3682                  */
3683                 dp->heads = 255;
3684                 dp->secs_per_track = 255;
3685                 dp->cylinders = dp->sectors / (255 * 255);
3686                 if (dp->cylinders == 0) {
3687                         dp->cylinders = 1;
3688                 }
3689         } else {
3690                 dp->heads = ccg.heads;
3691                 dp->secs_per_track = ccg.secs_per_track;
3692                 dp->cylinders = ccg.cylinders;
3693         }
3694
3695         /*
3696          * If the user supplied a read capacity buffer, and if it is
3697          * different than the previous buffer, update the data in the EDT.
3698          * If it's the same, we don't bother.  This avoids sending an
3699          * update every time someone opens this device.
3700          */
3701         if ((rcaplong != NULL)
3702          && (bcmp(rcaplong, &softc->rcaplong,
3703                   min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
3704                 struct ccb_dev_advinfo cdai;
3705
3706                 xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
3707                 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
3708                 cdai.buftype = CDAI_TYPE_RCAPLONG;
3709                 cdai.flags |= CDAI_FLAG_STORE;
3710                 cdai.bufsiz = rcap_len;
3711                 cdai.buf = (uint8_t *)rcaplong;
3712                 xpt_action((union ccb *)&cdai);
3713                 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
3714                         cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
3715                 if (cdai.ccb_h.status != CAM_REQ_CMP) {
3716                         xpt_print(periph->path, "%s: failed to set read "
3717                                   "capacity advinfo\n", __func__);
3718                         /* Use cam_error_print() to decode the status */
3719                         cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
3720                                         CAM_EPF_ALL);
3721                 } else {
3722                         bcopy(rcaplong, &softc->rcaplong,
3723                               min(sizeof(softc->rcaplong), rcap_len));
3724                 }
3725         }
3726
3727         softc->disk->d_sectorsize = softc->params.secsize;
3728         softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
3729         softc->disk->d_stripesize = softc->params.stripesize;
3730         softc->disk->d_stripeoffset = softc->params.stripeoffset;
3731         /* XXX: these are not actually "firmware" values, so they may be wrong */
3732         softc->disk->d_fwsectors = softc->params.secs_per_track;
3733         softc->disk->d_fwheads = softc->params.heads;
3734         softc->disk->d_devstat->block_size = softc->params.secsize;
3735         softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
3736
3737         error = disk_resize(softc->disk, M_NOWAIT);
3738         if (error != 0)
3739                 xpt_print(periph->path, "disk_resize(9) failed, error = %d\n", error);
3740 }
3741
3742 static void
3743 dasendorderedtag(void *arg)
3744 {
3745         struct da_softc *softc = arg;
3746
3747         if (da_send_ordered) {
3748                 if ((softc->ordered_tag_count == 0) 
3749                  && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
3750                         softc->flags |= DA_FLAG_NEED_OTAG;
3751                 }
3752                 if (softc->outstanding_cmds > 0)
3753                         softc->flags &= ~DA_FLAG_WENT_IDLE;
3754
3755                 softc->ordered_tag_count = 0;
3756         }
3757         /* Queue us up again */
3758         callout_reset(&softc->sendordered_c,
3759             (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
3760             dasendorderedtag, softc);
3761 }
3762
3763 /*
3764  * Step through all DA peripheral drivers, and if the device is still open,
3765  * sync the disk cache to physical media.
3766  */
3767 static void
3768 dashutdown(void * arg, int howto)
3769 {
3770         struct cam_periph *periph;
3771         struct da_softc *softc;
3772         union ccb *ccb;
3773         int error;
3774
3775         CAM_PERIPH_FOREACH(periph, &dadriver) {
3776                 softc = (struct da_softc *)periph->softc;
3777                 if (SCHEDULER_STOPPED()) {
3778                         /* If we paniced with the lock held, do not recurse. */
3779                         if (!cam_periph_owned(periph) &&
3780                             (softc->flags & DA_FLAG_OPEN)) {
3781                                 dadump(softc->disk, NULL, 0, 0, 0);
3782                         }
3783                         continue;
3784                 }
3785                 cam_periph_lock(periph);
3786
3787                 /*
3788                  * We only sync the cache if the drive is still open, and
3789                  * if the drive is capable of it..
3790                  */
3791                 if (((softc->flags & DA_FLAG_OPEN) == 0)
3792                  || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
3793                         cam_periph_unlock(periph);
3794                         continue;
3795                 }
3796
3797                 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
3798                 scsi_synchronize_cache(&ccb->csio,
3799                                        /*retries*/0,
3800                                        /*cbfcnp*/dadone,
3801                                        MSG_SIMPLE_Q_TAG,
3802                                        /*begin_lba*/0, /* whole disk */
3803                                        /*lb_count*/0,
3804                                        SSD_FULL_SIZE,
3805                                        60 * 60 * 1000);
3806
3807                 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
3808                     /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR,
3809                     softc->disk->d_devstat);
3810                 if (error != 0)
3811                         xpt_print(periph->path, "Synchronize cache failed\n");
3812                 xpt_release_ccb(ccb);
3813                 cam_periph_unlock(periph);
3814         }
3815 }
3816
3817 #else /* !_KERNEL */
3818
3819 /*
3820  * XXX This is only left out of the kernel build to silence warnings.  If,
3821  * for some reason this function is used in the kernel, the ifdefs should
3822  * be moved so it is included both in the kernel and userland.
3823  */
3824 void
3825 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
3826                  void (*cbfcnp)(struct cam_periph *, union ccb *),
3827                  u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
3828                  u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
3829                  u_int32_t timeout)
3830 {
3831         struct scsi_format_unit *scsi_cmd;
3832
3833         scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
3834         scsi_cmd->opcode = FORMAT_UNIT;
3835         scsi_cmd->byte2 = byte2;
3836         scsi_ulto2b(ileave, scsi_cmd->interleave);
3837
3838         cam_fill_csio(csio,
3839                       retries,
3840                       cbfcnp,
3841                       /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
3842                       tag_action,
3843                       data_ptr,
3844                       dxfer_len,
3845                       sense_len,
3846                       sizeof(*scsi_cmd),
3847                       timeout);
3848 }
3849
3850 void
3851 scsi_sanitize(struct ccb_scsiio *csio, u_int32_t retries,
3852               void (*cbfcnp)(struct cam_periph *, union ccb *),
3853               u_int8_t tag_action, u_int8_t byte2, u_int16_t control,
3854               u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
3855               u_int32_t timeout)
3856 {
3857         struct scsi_sanitize *scsi_cmd;
3858
3859         scsi_cmd = (struct scsi_sanitize *)&csio->cdb_io.cdb_bytes;
3860         scsi_cmd->opcode = SANITIZE;
3861         scsi_cmd->byte2 = byte2;
3862         scsi_cmd->control = control;
3863         scsi_ulto2b(dxfer_len, scsi_cmd->length);
3864
3865         cam_fill_csio(csio,
3866                       retries,
3867                       cbfcnp,
3868                       /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
3869                       tag_action,
3870                       data_ptr,
3871                       dxfer_len,
3872                       sense_len,
3873                       sizeof(*scsi_cmd),
3874                       timeout);
3875 }
3876
3877 #endif /* _KERNEL */