Merge branch 'vendor/MDOCML'
[dragonfly.git] / sys / bus / cam / scsi / scsi_da.c
1 /*
2  * Implementation of SCSI Direct Access Peripheral driver for CAM.
3  *
4  * Copyright (c) 1997 Justin T. Gibbs.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer,
12  *    without modification, immediately at the beginning of the file.
13  * 2. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD: src/sys/cam/scsi/scsi_da.c,v 1.42.2.46 2003/10/21 22:18:19 thomas Exp $
29  */
30
31 #include <sys/param.h>
32
33 #ifdef _KERNEL
34
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/buf.h>
38 #include <sys/sysctl.h>
39 #include <sys/taskqueue.h>
40 #include <sys/lock.h>
41 #include <sys/conf.h>
42 #include <sys/devicestat.h>
43 #include <sys/disk.h>
44 #include <sys/dtype.h>
45 #include <sys/eventhandler.h>
46 #include <sys/malloc.h>
47 #include <sys/cons.h>
48 #include <sys/proc.h>
49 #include <sys/ioctl_compat.h>
50
51 #include <sys/buf2.h>
52 #include <sys/thread2.h>
53 #include <sys/mplock2.h>
54
55 #endif /* _KERNEL */
56
57 #ifdef _KERNEL
58 #include <vm/pmap.h>
59 #endif
60
61 #ifndef _KERNEL
62 #include <stdio.h>
63 #include <string.h>
64 #endif /* _KERNEL */
65
66 #include <sys/camlib.h>
67 #include "../cam.h"
68 #include "../cam_ccb.h"
69 #include "../cam_extend.h"
70 #include "../cam_periph.h"
71 #include "../cam_xpt_periph.h"
72 #include "../cam_sim.h"
73
74 #include "scsi_message.h"
75
76 #ifndef _KERNEL 
77 #include "scsi_da.h"
78 #endif /* !_KERNEL */
79
80 #ifdef _KERNEL
81 typedef enum {
82         DA_STATE_PROBE,
83         DA_STATE_PROBE2,
84         DA_STATE_NORMAL
85 } da_state;
86
87 typedef enum {
88         DA_FLAG_PACK_INVALID    = 0x001,
89         DA_FLAG_NEW_PACK        = 0x002,
90         DA_FLAG_PACK_LOCKED     = 0x004,
91         DA_FLAG_PACK_REMOVABLE  = 0x008,
92         DA_FLAG_TAGGED_QUEUING  = 0x010,
93         DA_FLAG_NEED_OTAG       = 0x020,
94         DA_FLAG_WENT_IDLE       = 0x040,
95         DA_FLAG_RETRY_UA        = 0x080,
96         DA_FLAG_OPEN            = 0x100,
97         DA_FLAG_SCTX_INIT       = 0x200,
98         DA_FLAG_RD_LIMIT        = 0x400,
99         DA_FLAG_WR_LIMIT        = 0x800,
100         DA_FLAG_CAN_TRIM        = 0x1000
101 } da_flags;
102
103 typedef enum {
104         DA_Q_NONE               = 0x00,
105         DA_Q_NO_SYNC_CACHE      = 0x01,
106         DA_Q_NO_6_BYTE          = 0x02,
107         DA_Q_NO_PREVENT         = 0x04
108 } da_quirks;
109
110 typedef enum {
111         DA_CCB_PROBE            = 0x01,
112         DA_CCB_PROBE2           = 0x02,
113         DA_CCB_BUFFER_IO        = 0x03,
114         DA_CCB_WAITING          = 0x04,
115         DA_CCB_DUMP             = 0x05,
116         DA_CCB_TRIM             = 0x06,
117         DA_CCB_TYPE_MASK        = 0x0F,
118         DA_CCB_RETRY_UA         = 0x10
119 } da_ccb_state;
120
121 /* Offsets into our private area for storing information */
122 #define ccb_state       ppriv_field0
123 #define ccb_bio         ppriv_ptr1
124
125 struct disk_params {
126         u_int8_t  heads;
127         u_int32_t cylinders;
128         u_int8_t  secs_per_track;
129         u_int32_t secsize;      /* Number of bytes/sector */
130         u_int64_t sectors;      /* total number sectors */
131 };
132
133 #define TRIM_MAX_BLOCKS 8
134 #define TRIM_MAX_RANGES TRIM_MAX_BLOCKS * 64
135 struct trim_request {
136         uint8_t         data[TRIM_MAX_RANGES * 8];
137         struct bio      *bios[TRIM_MAX_RANGES];
138 };
139
140 struct da_softc {
141         struct   bio_queue_head bio_queue_rd;
142         struct   bio_queue_head bio_queue_wr;
143         struct   bio_queue_head bio_queue_trim;
144         struct   devstat device_stats;
145         SLIST_ENTRY(da_softc) links;
146         LIST_HEAD(, ccb_hdr) pending_ccbs;
147         da_state state;
148         da_flags flags; 
149         da_quirks quirks;
150         int      minimum_cmd_size;
151         int      ordered_tag_count;
152         int      outstanding_cmds_rd;
153         int      outstanding_cmds_wr;
154         int      trim_max_ranges;
155         int      trim_running;
156         int      trim_enabled;
157         struct   disk_params params;
158         struct   disk disk;
159         union    ccb saved_ccb;
160         struct task             sysctl_task;
161         struct sysctl_ctx_list  sysctl_ctx;
162         struct sysctl_oid       *sysctl_tree;
163         struct callout          sendordered_c;
164         struct trim_request     trim_req;
165 };
166
167 struct da_quirk_entry {
168         struct scsi_inquiry_pattern inq_pat;
169         da_quirks quirks;
170 };
171
172 static const char quantum[] = "QUANTUM";
173 static const char microp[] = "MICROP";
174
175 static struct da_quirk_entry da_quirk_table[] =
176 {
177         /* SPI, FC devices */
178         {
179                 /*
180                  * Fujitsu M2513A MO drives.
181                  * Tested devices: M2513A2 firmware versions 1200 & 1300.
182                  * (dip switch selects whether T_DIRECT or T_OPTICAL device)
183                  * Reported by: W.Scholten <whs@xs4all.nl>
184                  */
185                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
186                 /*quirks*/ DA_Q_NO_SYNC_CACHE
187         },
188         {
189                 /* See above. */
190                 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
191                 /*quirks*/ DA_Q_NO_SYNC_CACHE
192         },
193         {
194                 /*
195                  * This particular Fujitsu drive doesn't like the
196                  * synchronize cache command.
197                  * Reported by: Tom Jackson <toj@gorilla.net>
198                  */
199                 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
200                 /*quirks*/ DA_Q_NO_SYNC_CACHE
201         },
202         {
203                 /*
204                  * This drive doesn't like the synchronize cache command
205                  * either.  Reported by: Matthew Jacob <mjacob@feral.com>
206                  * in NetBSD PR kern/6027, August 24, 1998.
207                  */
208                 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
209                 /*quirks*/ DA_Q_NO_SYNC_CACHE
210         },
211         {
212                 /*
213                  * This drive doesn't like the synchronize cache command
214                  * either.  Reported by: Hellmuth Michaelis (hm@kts.org)
215                  * (PR 8882).
216                  */
217                 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
218                 /*quirks*/ DA_Q_NO_SYNC_CACHE
219         },
220         {
221                 /*
222                  * Doesn't like the synchronize cache command.
223                  * Reported by: Blaz Zupan <blaz@gold.amis.net>
224                  */
225                 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
226                 /*quirks*/ DA_Q_NO_SYNC_CACHE
227         },
228         {
229                 /*
230                  * Doesn't like the synchronize cache command.
231                  * Reported by: Blaz Zupan <blaz@gold.amis.net>
232                  */
233                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
234                 /*quirks*/ DA_Q_NO_SYNC_CACHE
235         },
236         {
237                 /*
238                  * Doesn't like the synchronize cache command.
239                  */
240                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
241                 /*quirks*/ DA_Q_NO_SYNC_CACHE
242         },
243         {
244                 /*
245                  * Doesn't like the synchronize cache command.
246                  * Reported by: walter@pelissero.de
247                  */
248                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
249                 /*quirks*/ DA_Q_NO_SYNC_CACHE
250         },
251         {
252                 /*
253                  * Doesn't work correctly with 6 byte reads/writes.
254                  * Returns illegal request, and points to byte 9 of the
255                  * 6-byte CDB.
256                  * Reported by:  Adam McDougall <bsdx@spawnet.com>
257                  */
258                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
259                 /*quirks*/ DA_Q_NO_6_BYTE
260         },
261         {
262                 /* See above. */
263                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
264                 /*quirks*/ DA_Q_NO_6_BYTE
265         },
266         {
267                 /*
268                  * Doesn't like the synchronize cache command.
269                  * Reported by: walter@pelissero.de
270                  */
271                 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
272                 /*quirks*/ DA_Q_NO_SYNC_CACHE
273         },
274         {
275                 /*
276                  * The CISS RAID controllers do not support SYNC_CACHE
277                  */
278                 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
279                 /*quirks*/ DA_Q_NO_SYNC_CACHE
280         },
281         {
282                 /*
283                  * The same goes for the mly(4) controllers
284                  */
285                 {T_DIRECT, SIP_MEDIA_FIXED, "MLY*", "*", "MYLX"},
286                 /*quirks*/ DA_Q_NO_SYNC_CACHE
287         },
288         /*
289          * USB mass storage devices supported by umass(4)
290          *
291          * NOTE: USB attachments automatically set DA_Q_NO_SYNC_CACHE so
292          *       it does not have to be specified here.
293          */
294         {
295                 /*
296                  * Creative Nomad MUVO mp3 player (USB)
297                  * PR: kern/53094
298                  */
299                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
300                 /*quirks*/ DA_Q_NO_PREVENT
301         },
302         {
303                 /*
304                  * Sigmatel USB Flash MP3 Player
305                  * PR: kern/57046
306                  */
307                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
308                 /*quirks*/ DA_Q_NO_PREVENT
309         },
310         {
311                 /*
312                  * SEAGRAND NP-900 MP3 Player
313                  * PR: kern/64563
314                  */
315                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
316                 /*quirks*/ DA_Q_NO_PREVENT
317         },
318         {
319                 /*
320                  * Creative MUVO Slim mp3 player (USB)
321                  * PR: usb/86131
322                  */
323                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
324                 "*"}, /*quirks*/ DA_Q_NO_PREVENT
325         },
326         {
327                 /*
328                  * Philips USB Key Audio KEY013
329                  * PR: usb/68412
330                  */
331                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
332                 /*quirks*/ DA_Q_NO_PREVENT
333         },
334 };
335
336 static  d_open_t        daopen;
337 static  d_close_t       daclose;
338 static  d_strategy_t    dastrategy;
339 static  d_dump_t        dadump;
340 static  d_ioctl_t       daioctl;
341 static  periph_init_t   dainit;
342 static  void            daasync(void *callback_arg, u_int32_t code,
343                                 struct cam_path *path, void *arg);
344 static  int             dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
345 static  periph_ctor_t   daregister;
346 static  periph_dtor_t   dacleanup;
347 static  periph_start_t  dastart;
348 static  periph_oninv_t  daoninvalidate;
349 static  void            dadone(struct cam_periph *periph,
350                                union ccb *done_ccb);
351 static  int             daerror(union ccb *ccb, u_int32_t cam_flags,
352                                 u_int32_t sense_flags);
353 static void             daprevent(struct cam_periph *periph, int action);
354 static int              dagetcapacity(struct cam_periph *periph);
355 static int              dacheckmedia(struct cam_periph *periph);
356 static void             dasetgeom(struct cam_periph *periph, uint32_t block_len,
357                                   uint64_t maxsector);
358 static void             daflushbioq(struct bio_queue_head *bioq, int error);
359 static timeout_t        dasendorderedtag;
360 static void             dashutdown(void *arg, int howto);
361
362 #ifndef DA_DEFAULT_TIMEOUT
363 #define DA_DEFAULT_TIMEOUT 60   /* Timeout in seconds */
364 #endif
365
366 #ifndef DA_DEFAULT_RETRY
367 #define DA_DEFAULT_RETRY        4
368 #endif
369
370 #ifndef DA_DEFAULT_SEND_ORDERED
371 #define DA_DEFAULT_SEND_ORDERED 1
372 #endif
373
374 static int da_retry_count = DA_DEFAULT_RETRY;
375 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
376 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
377 static struct callout dasendorderedtag_ch;
378
379 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
380             "CAM Direct Access Disk driver");
381 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
382            &da_retry_count, 0, "Normal I/O retry count");
383 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
384 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
385            &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
386 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
387 SYSCTL_INT(_kern_cam_da, OID_AUTO, da_send_ordered, CTLFLAG_RW,
388            &da_send_ordered, 0, "Send Ordered Tags");
389 TUNABLE_INT("kern.cam.da.da_send_ordered", &da_send_ordered);
390
391 /*
392  * DA_ORDEREDTAG_INTERVAL determines how often, relative
393  * to the default timeout, we check to see whether an ordered
394  * tagged transaction is appropriate to prevent simple tag
395  * starvation.  Since we'd like to ensure that there is at least
396  * 1/2 of the timeout length left for a starved transaction to
397  * complete after we've sent an ordered tag, we must poll at least
398  * four times in every timeout period.  This takes care of the worst
399  * case where a starved transaction starts during an interval that
400  * meets the requirement "don't send an ordered tag" test so it takes
401  * us two intervals to determine that a tag must be sent.
402  */
403 #ifndef DA_ORDEREDTAG_INTERVAL
404 #define DA_ORDEREDTAG_INTERVAL 4
405 #endif
406
407 static struct periph_driver dadriver =
408 {
409         dainit, "da",
410         TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
411 };
412
413 PERIPHDRIVER_DECLARE(da, dadriver);
414
415 static struct dev_ops da_ops = {
416         { "da", 0, D_DISK | D_MPSAFE },
417         .d_open =       daopen,
418         .d_close =      daclose,
419         .d_read =       physread,
420         .d_write =      physwrite,
421         .d_strategy =   dastrategy,
422         .d_dump =       dadump,
423         .d_ioctl =      daioctl
424 };
425
426 static struct extend_array *daperiphs;
427
428 MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
429
430 static int
431 daioctl(struct dev_ioctl_args *ap)
432 {       
433         int unit;
434         int error = 0;  
435         struct buf *bp;         
436         struct cam_periph *periph;
437         int byte_count;
438
439         off_t *del_num = (off_t*)ap->a_data;
440         off_t bytes_left;
441         off_t bytes_start;              
442
443         cdev_t dev = ap->a_head.a_dev;
444
445
446         unit = dkunit(dev);
447         periph = cam_extend_get(daperiphs, unit);
448         if (periph == NULL)
449                 return(ENXIO);
450         
451         switch (ap->a_cmd) {
452         case IOCTLTRIM:
453         {
454
455                 bytes_left = del_num[1];
456                 bytes_start = del_num[0];
457
458                 /* TRIM occurs on 512-byte sectors. */
459                 KKASSERT((bytes_left % 512) == 0);
460                 KKASSERT((bytes_start% 512) == 0);
461
462                 
463                 /* Break TRIM up into int-sized commands because of b_bcount */
464                 while(bytes_left) {
465
466                         /* 
467                          * Rather than than squezing out more blocks in b_bcount 
468                          * and having to break up the TRIM request in da_start(),
469                          * we ensure we can always TRIM this many bytes with one 
470                          * TRIM command (this happens if the device only 
471                          * supports one TRIM block). 
472                          *  
473                          * With min TRIM blksize of 1, TRIM command free
474                          * 4194240 blks(64*65535): each LBA range can address 
475                          * 65535 blks and there 64 such ranges in a 512-byte 
476                          * block. And, 4194240 * 512 = 0x7FFF8000
477                          * 
478                          */
479                         byte_count = MIN(bytes_left,0x7FFF8000);
480                         bp = getnewbuf(0,0,0,1);
481                 
482                         bp->b_cmd = BUF_CMD_FREEBLKS;
483                         bp->b_bio1.bio_offset = bytes_start;
484                         bp->b_bcount = byte_count;
485                         bp->b_bio1.bio_flags |= BIO_SYNC;
486                         bp->b_bio1.bio_done = biodone_sync;
487                 
488                         dev_dstrategy(ap->a_head.a_dev, &bp->b_bio1);
489                 
490                         if (biowait(&bp->b_bio1, "TRIM")) {
491                                 kprintf("Error:%d\n", bp->b_error);
492                                 return(bp->b_error ? bp->b_error : EIO);
493                         }
494                         brelse(bp);
495                         bytes_left -= byte_count;
496                         bytes_start += byte_count;      
497                 }
498                 break;
499         }
500         default:
501                 return(EINVAL);
502         }       
503         
504         return(error);
505 }
506
507 static int
508 daopen(struct dev_open_args *ap)
509 {
510         cdev_t dev = ap->a_head.a_dev;
511         struct cam_periph *periph;
512         struct da_softc *softc;
513         struct disk_info info;
514         int unit;
515         int error;
516
517         unit = dkunit(dev);
518         periph = cam_extend_get(daperiphs, unit);
519         if (periph == NULL) {
520                 return (ENXIO); 
521         }
522
523         if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
524                 return(ENXIO);
525         }
526
527         cam_periph_lock(periph);
528         if ((error = cam_periph_hold(periph, PCATCH)) != 0) {
529                 cam_periph_unlock(periph);
530                 cam_periph_release(periph);
531                 return (error);
532         }
533
534         unit = periph->unit_number;
535         softc = (struct da_softc *)periph->softc;
536
537         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
538             ("daopen: dev=%s (unit %d)\n", devtoname(dev),
539              unit));
540
541         if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
542                 /* Invalidate our pack information. */
543                 disk_invalidate(&softc->disk);
544                 softc->flags &= ~DA_FLAG_PACK_INVALID;
545         }
546
547         error = dacheckmedia(periph);
548         softc->flags |= DA_FLAG_OPEN;
549
550         if (error == 0) {
551                 struct ccb_getdev cgd;
552
553                 /* Build disk information structure */
554                 bzero(&info, sizeof(info));
555                 info.d_type = DTYPE_SCSI;
556
557                 /*
558                  * Grab the inquiry data to get the vendor and product names.
559                  * Put them in the typename and packname for the label.
560                  */
561                 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
562                 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
563                 xpt_action((union ccb *)&cgd);
564
565                 /*
566                  * Check to see whether or not the blocksize is set yet.
567                  * If it isn't, set it and then clear the blocksize
568                  * unavailable flag for the device statistics.
569                  */
570                 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0){
571                         softc->device_stats.block_size = softc->params.secsize;
572                         softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
573                 }
574         }
575         
576         if (error == 0) {
577                 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
578                     (softc->quirks & DA_Q_NO_PREVENT) == 0)
579                         daprevent(periph, PR_PREVENT);
580         } else {
581                 softc->flags &= ~DA_FLAG_OPEN;
582                 cam_periph_release(periph);
583         }
584         cam_periph_unhold(periph, 1);
585         return (error);
586 }
587
588 static int
589 daclose(struct dev_close_args *ap)
590 {
591         cdev_t dev = ap->a_head.a_dev;
592         struct  cam_periph *periph;
593         struct  da_softc *softc;
594         int     unit;
595         int     error;
596
597         unit = dkunit(dev);
598         periph = cam_extend_get(daperiphs, unit);
599         if (periph == NULL)
600                 return (ENXIO); 
601
602         cam_periph_lock(periph);
603         if ((error = cam_periph_hold(periph, 0)) != 0) {
604                 cam_periph_unlock(periph);
605                 cam_periph_release(periph);
606                 return (error);
607         }
608
609         softc = (struct da_softc *)periph->softc;
610
611         if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
612                 union   ccb *ccb;
613
614                 ccb = cam_periph_getccb(periph, /*priority*/1);
615
616                 scsi_synchronize_cache(&ccb->csio,
617                                        /*retries*/1,
618                                        /*cbfcnp*/dadone,
619                                        MSG_SIMPLE_Q_TAG,
620                                        /*begin_lba*/0,/* Cover the whole disk */
621                                        /*lb_count*/0,
622                                        SSD_FULL_SIZE,
623                                        5 * 60 * 1000);
624
625                 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
626                                   /*sense_flags*/SF_RETRY_UA,
627                                   &softc->device_stats);
628
629                 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
630                         if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
631                              CAM_SCSI_STATUS_ERROR) {
632                                 int asc, ascq;
633                                 int sense_key, error_code;
634
635                                 scsi_extract_sense(&ccb->csio.sense_data,
636                                                    &error_code,
637                                                    &sense_key, 
638                                                    &asc, &ascq);
639                                 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
640                                         scsi_sense_print(&ccb->csio);
641                         } else {
642                                 xpt_print(periph->path, "Synchronize cache "
643                                     "failed, status == 0x%x, scsi status == "
644                                     "0x%x\n", ccb->csio.ccb_h.status,
645                                     ccb->csio.scsi_status);
646                         }
647                 }
648
649                 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
650                         cam_release_devq(ccb->ccb_h.path,
651                                          /*relsim_flags*/0,
652                                          /*reduction*/0,
653                                          /*timeout*/0,
654                                          /*getcount_only*/0);
655
656                 xpt_release_ccb(ccb);
657
658         }
659
660         if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
661                 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
662                         daprevent(periph, PR_ALLOW);
663                 /*
664                  * If we've got removeable media, mark the blocksize as
665                  * unavailable, since it could change when new media is
666                  * inserted.
667                  */
668                 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
669         }
670
671         /*
672          * Don't compound any ref counting software bugs with more.
673          */
674         if (softc->flags & DA_FLAG_OPEN) {
675                 softc->flags &= ~DA_FLAG_OPEN;
676                 cam_periph_release(periph);
677         } else {
678                 xpt_print(periph->path,
679                           "daclose() called on an already closed device!\n");
680         }
681         cam_periph_unhold(periph, 1);
682         return (0);     
683 }
684
685 /*
686  * Actually translate the requested transfer into one the physical driver
687  * can understand.  The transfer is described by a buf and will include
688  * only one physical transfer.
689  */
690 static int
691 dastrategy(struct dev_strategy_args *ap)
692 {
693         cdev_t dev = ap->a_head.a_dev;
694         struct bio *bio = ap->a_bio;
695         struct buf *bp = bio->bio_buf;
696         struct cam_periph *periph;
697         struct da_softc *softc;
698         u_int  unit;
699         
700         unit = dkunit(dev);
701         periph = cam_extend_get(daperiphs, unit);
702         if (periph == NULL) {
703                 bp->b_error = ENXIO;
704                 goto bad;               
705         }
706         softc = (struct da_softc *)periph->softc;
707
708         cam_periph_lock(periph);
709
710 #if 0
711         /*
712          * check it's not too big a transfer for our adapter
713          */
714         scsi_minphys(bp, &sd_switch);
715 #endif
716
717         /*
718          * Mask interrupts so that the pack cannot be invalidated until
719          * after we are in the queue.  Otherwise, we might not properly
720          * clean up one of the buffers.
721          */
722         
723         /*
724          * If the device has been made invalid, error out
725          */
726         if ((softc->flags & DA_FLAG_PACK_INVALID)) {
727                 cam_periph_unlock(periph);
728                 bp->b_error = ENXIO;
729                 goto bad;
730         }
731         
732         /*
733          * Place it in the queue of disk activities for this disk
734          */
735         if (bp->b_cmd == BUF_CMD_WRITE || bp->b_cmd == BUF_CMD_FLUSH)
736                 bioqdisksort(&softc->bio_queue_wr, bio);
737         else if (bp->b_cmd == BUF_CMD_FREEBLKS) 
738                 bioqdisksort(&softc->bio_queue_trim, bio);
739         else
740                 bioqdisksort(&softc->bio_queue_rd, bio);
741         
742         /*
743          * Schedule ourselves for performing the work.
744          */
745         xpt_schedule(periph, /* XXX priority */1);
746         cam_periph_unlock(periph);
747
748         return(0);
749 bad:
750         bp->b_flags |= B_ERROR;
751
752         /*
753          * Correctly set the buf to indicate a completed xfer
754          */
755         bp->b_resid = bp->b_bcount;
756         biodone(bio);
757         return(0);
758 }
759
760 static int
761 dadump(struct dev_dump_args *ap)
762 {
763         cdev_t dev = ap->a_head.a_dev;
764         struct      cam_periph *periph;
765         struct      da_softc *softc;
766         u_int       unit;
767         u_int32_t   secsize;
768         struct      ccb_scsiio csio;
769
770         unit = dkunit(dev);
771         periph = cam_extend_get(daperiphs, unit);
772         if (periph == NULL)
773                 return (ENXIO);
774
775         softc = (struct da_softc *)periph->softc;
776         cam_periph_lock(periph);
777         secsize = softc->params.secsize; /* XXX: or ap->a_secsize? */
778
779         if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
780                 cam_periph_unlock(periph);
781                 return (ENXIO);
782         }
783
784         /*
785          * because length == 0 means we are supposed to flush cache, we only
786          * try to write something if length > 0.
787          */
788         if (ap->a_length > 0) {
789                 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
790                 csio.ccb_h.flags |= CAM_POLLED;
791                 csio.ccb_h.ccb_state = DA_CCB_DUMP;
792                 scsi_read_write(&csio,
793                                 /*retries*/1,
794                                 dadone,
795                                 MSG_ORDERED_Q_TAG,
796                                 /*read*/FALSE,
797                                 /*byte2*/0,
798                                 /*minimum_cmd_size*/ softc->minimum_cmd_size,
799                                 ap->a_offset / secsize,
800                                 ap->a_length / secsize,
801                                 /*data_ptr*/(u_int8_t *) ap->a_virtual,
802                                 /*dxfer_len*/ap->a_length,
803                                 /*sense_len*/SSD_FULL_SIZE,
804                                 DA_DEFAULT_TIMEOUT * 1000);             
805                 xpt_polled_action((union ccb *)&csio);
806
807                 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
808                         kprintf("Aborting dump due to I/O error.\n");
809                         if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
810                              CAM_SCSI_STATUS_ERROR)
811                                 scsi_sense_print(&csio);
812                         else
813                                 kprintf("status == 0x%x, scsi status == 0x%x\n",
814                                        csio.ccb_h.status, csio.scsi_status);
815                         return(EIO);
816                 }
817                 cam_periph_unlock(periph);
818                 return 0;
819         }
820
821         /*
822          * Sync the disk cache contents to the physical media.
823          */
824         if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
825
826                 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
827                 csio.ccb_h.ccb_state = DA_CCB_DUMP;
828                 scsi_synchronize_cache(&csio,
829                                        /*retries*/1,
830                                        /*cbfcnp*/dadone,
831                                        MSG_SIMPLE_Q_TAG,
832                                        /*begin_lba*/0,/* Cover the whole disk */
833                                        /*lb_count*/0,
834                                        SSD_FULL_SIZE,
835                                        5 * 60 * 1000);
836                 xpt_polled_action((union ccb *)&csio);
837
838                 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
839                         if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
840                              CAM_SCSI_STATUS_ERROR) {
841                                 int asc, ascq;
842                                 int sense_key, error_code;
843
844                                 scsi_extract_sense(&csio.sense_data,
845                                                    &error_code,
846                                                    &sense_key, 
847                                                    &asc, &ascq);
848                                 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
849                                         scsi_sense_print(&csio);
850                         } else {
851                                 xpt_print(periph->path, "Synchronize cache "
852                                     "failed, status == 0x%x, scsi status == "
853                                     "0x%x\n", csio.ccb_h.status,
854                                     csio.scsi_status);
855                         }
856                 }
857         }
858         cam_periph_unlock(periph);
859         return (0);
860 }
861
862 static void
863 dainit(void)
864 {
865         cam_status status;
866
867         /*
868          * Create our extend array for storing the devices we attach to.
869          */
870         daperiphs = cam_extend_new();
871         if (daperiphs == NULL) {
872                 kprintf("da: Failed to alloc extend array!\n");
873                 return;
874         }
875
876         callout_init(&dasendorderedtag_ch);
877
878         /*
879          * Install a global async callback.  This callback will
880          * receive async callbacks like "new device found".
881          */
882         status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
883
884         if (status != CAM_REQ_CMP) {
885                 kprintf("da: Failed to attach master async callback "
886                        "due to status 0x%x!\n", status);
887         } else if (da_send_ordered) {
888
889                 /* Register our shutdown event handler */
890                 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown, 
891                                            NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
892                         kprintf("%s: shutdown event registration failed!\n",
893                             __func__);
894         }
895 }
896
897 static void
898 daoninvalidate(struct cam_periph *periph)
899 {
900         struct da_softc *softc;
901
902         softc = (struct da_softc *)periph->softc;
903
904         /*
905          * De-register any async callbacks.
906          */
907         xpt_register_async(0, daasync, periph, periph->path);
908
909         softc->flags |= DA_FLAG_PACK_INVALID;
910
911         /*
912          * Return all queued I/O with ENXIO.
913          * XXX Handle any transactions queued to the card
914          *     with XPT_ABORT_CCB.
915          */
916         daflushbioq(&softc->bio_queue_trim, ENXIO);
917         daflushbioq(&softc->bio_queue_wr, ENXIO);
918         daflushbioq(&softc->bio_queue_rd, ENXIO);
919         xpt_print(periph->path, "lost device\n");
920 }
921
922 static void
923 daflushbioq(struct bio_queue_head *bioq, int error)
924 {
925         struct bio *q_bio;
926         struct buf *q_bp;
927
928         while ((q_bio = bioq_first(bioq)) != NULL){
929                 bioq_remove(bioq, q_bio);
930                 q_bp = q_bio->bio_buf;
931                 q_bp->b_resid = q_bp->b_bcount;
932                 q_bp->b_error = error;
933                 q_bp->b_flags |= B_ERROR;
934                 biodone(q_bio);
935         }
936 }
937
938 static void
939 dacleanup(struct cam_periph *periph)
940 {
941         struct da_softc *softc;
942
943         softc = (struct da_softc *)periph->softc;
944
945         devstat_remove_entry(&softc->device_stats);
946         cam_extend_release(daperiphs, periph->unit_number);
947         xpt_print(periph->path, "removing device entry\n");
948         /*
949          * If we can't free the sysctl tree, oh well...
950          */
951         if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
952             && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
953                 xpt_print(periph->path, "can't remove sysctl context\n");
954         }
955         periph->softc = NULL;
956         if (softc->disk.d_rawdev) {
957                 cam_periph_unlock(periph);
958                 disk_destroy(&softc->disk);
959                 cam_periph_lock(periph);
960         }
961
962         callout_stop(&softc->sendordered_c);
963         kfree(softc, M_DEVBUF);
964 }
965
966 static void
967 daasync(void *callback_arg, u_int32_t code,
968         struct cam_path *path, void *arg)
969 {
970         struct cam_periph *periph;
971
972         periph = (struct cam_periph *)callback_arg;
973
974         switch (code) {
975         case AC_FOUND_DEVICE:
976         {
977                 struct ccb_getdev *cgd;
978                 cam_status status;
979  
980                 cgd = (struct ccb_getdev *)arg;
981                 if (cgd == NULL)
982                         break;
983
984                 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
985                     && SID_TYPE(&cgd->inq_data) != T_RBC
986                     && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
987                         break;
988
989                 /*
990                  * Don't complain if a valid peripheral is already attached.
991                  */
992                 periph = cam_periph_find(cgd->ccb_h.path, "da");
993                 if (periph && (periph->flags & CAM_PERIPH_INVALID) == 0)
994                         break;
995
996                 /*
997                  * Allocate a peripheral instance for
998                  * this device and start the probe
999                  * process.
1000                  */
1001                 status = cam_periph_alloc(daregister, daoninvalidate,
1002                                           dacleanup, dastart,
1003                                           "da", CAM_PERIPH_BIO,
1004                                           cgd->ccb_h.path, daasync,
1005                                           AC_FOUND_DEVICE, cgd);
1006
1007                 if (status != CAM_REQ_CMP && status != CAM_REQ_INPROG) {
1008                         kprintf("%s: Unable to attach to new device "
1009                             "due to status 0x%x\n", __func__, status);
1010                 }
1011                 break;
1012         }
1013         case AC_SENT_BDR:
1014         case AC_BUS_RESET:
1015         {
1016                 struct da_softc *softc;
1017                 struct ccb_hdr *ccbh;
1018
1019                 softc = (struct da_softc *)periph->softc;
1020                 /*
1021                  * Don't fail on the expected unit attention
1022                  * that will occur.
1023                  */
1024                 softc->flags |= DA_FLAG_RETRY_UA;
1025                 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1026                         ccbh->ccb_state |= DA_CCB_RETRY_UA;
1027                 /* FALLTHROUGH*/
1028         }
1029         default:
1030                 cam_periph_async(periph, code, path, arg);
1031                 break;
1032         }
1033 }
1034
1035 static void
1036 dasysctlinit(void *context, int pending)
1037 {
1038         struct cam_periph *periph;
1039         struct da_softc *softc;
1040         char tmpstr[80], tmpstr2[80];
1041
1042         get_mplock();
1043         periph = (struct cam_periph *)context;
1044         if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1045                 rel_mplock();
1046                 return;
1047         }
1048
1049         softc = (struct da_softc *)periph->softc;
1050         ksnprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1051         ksnprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1052
1053         sysctl_ctx_init(&softc->sysctl_ctx);
1054         softc->flags |= DA_FLAG_SCTX_INIT;
1055         softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1056                 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1057                 CTLFLAG_RD, 0, tmpstr);
1058         if (softc->sysctl_tree == NULL) {
1059                 kprintf("%s: unable to allocate sysctl tree\n", __func__);
1060                 cam_periph_release(periph);
1061                 rel_mplock();
1062                 return;
1063         }
1064
1065         /*
1066          * Now register the sysctl handler, so the user can the value on
1067          * the fly.
1068          */
1069         SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1070                 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1071                 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1072                 "Minimum CDB size");
1073
1074         /* Only create the option if the device supports TRIM */
1075         if (softc->disk.d_info.d_trimflag) {
1076                 SYSCTL_ADD_INT(&softc->sysctl_ctx, 
1077                     SYSCTL_CHILDREN(softc->sysctl_tree),
1078                     OID_AUTO,
1079                     "trim_enabled",
1080                     CTLFLAG_RW,
1081                     &softc->trim_enabled,
1082                     0,
1083                     "Enable TRIM for this device (SSD))");
1084         }
1085
1086         cam_periph_release(periph);
1087         rel_mplock();
1088 }
1089
1090 static int
1091 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1092 {
1093         int error, value;
1094
1095         value = *(int *)arg1;
1096
1097         error = sysctl_handle_int(oidp, &value, 0, req);
1098
1099         if ((error != 0)
1100          || (req->newptr == NULL))
1101                 return (error);
1102
1103         /*
1104          * Acceptable values here are 6, 10 or 12, or 16.
1105          */
1106         if (value < 6)
1107                 value = 6;
1108         else if ((value > 6)
1109               && (value <= 10))
1110                 value = 10;
1111         else if ((value > 10)
1112               && (value <= 12))
1113                 value = 12;
1114         else if (value > 12)
1115                 value = 16;
1116
1117         *(int *)arg1 = value;
1118
1119         return (0);
1120 }
1121
1122 static cam_status
1123 daregister(struct cam_periph *periph, void *arg)
1124 {
1125         struct da_softc *softc;
1126         struct ccb_pathinq cpi;
1127         struct ccb_getdev *cgd;
1128         char tmpstr[80];
1129         caddr_t match;
1130
1131         cgd = (struct ccb_getdev *)arg;
1132         if (periph == NULL) {
1133                 kprintf("%s: periph was NULL!!\n", __func__);
1134                 return(CAM_REQ_CMP_ERR);
1135         }
1136
1137         if (cgd == NULL) {
1138                 kprintf("%s: no getdev CCB, can't register device\n",
1139                     __func__);
1140                 return(CAM_REQ_CMP_ERR);
1141         }
1142
1143         softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
1144         LIST_INIT(&softc->pending_ccbs);
1145         softc->state = DA_STATE_PROBE;
1146         bioq_init(&softc->bio_queue_trim);
1147         bioq_init(&softc->bio_queue_rd);
1148         bioq_init(&softc->bio_queue_wr);
1149         if (SID_IS_REMOVABLE(&cgd->inq_data))
1150                 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1151         if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1152                 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1153
1154         /* Used to get TRIM status from AHCI driver */
1155         if (cgd->inq_data.vendor_specific1[0] == 1) {
1156                 /*
1157                  * max number of lba ranges an SSD can handle in a single
1158                  * TRIM command. vendor_specific1[1] is the num of 512-byte 
1159                  * blocks the SSD reports that can be passed in a TRIM cmd. 
1160                  */
1161                 softc->trim_max_ranges =
1162                    min(cgd->inq_data.vendor_specific1[1] * 64, TRIM_MAX_RANGES);
1163         }
1164
1165         periph->softc = softc;
1166         
1167         cam_extend_set(daperiphs, periph->unit_number, periph);
1168
1169         /*
1170          * See if this device has any quirks.
1171          */
1172         match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1173                                (caddr_t)da_quirk_table,
1174                                NELEM(da_quirk_table),
1175                                sizeof(*da_quirk_table), scsi_inquiry_match);
1176
1177         if (match != NULL)
1178                 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1179         else
1180                 softc->quirks = DA_Q_NONE;
1181
1182         /*
1183          * Unconditionally disable the synchronize cache command for
1184          * usb attachments.  It's just impossible to determine if the
1185          * device supports it or not and if it doesn't the port can
1186          * brick.
1187          */
1188         if (strncmp(periph->sim->sim_name, "umass", 4) == 0) {
1189                 softc->quirks |= DA_Q_NO_SYNC_CACHE;
1190         }
1191
1192         TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1193
1194         /* Check if the SIM does not want 6 byte commands */
1195         bzero(&cpi, sizeof(cpi));
1196         xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
1197         cpi.ccb_h.func_code = XPT_PATH_INQ;
1198         xpt_action((union ccb *)&cpi);
1199         if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1200                 softc->quirks |= DA_Q_NO_6_BYTE;
1201
1202         /*
1203          * RBC devices don't have to support READ(6), only READ(10).
1204          */
1205         if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1206                 softc->minimum_cmd_size = 10;
1207         else
1208                 softc->minimum_cmd_size = 6;
1209
1210         /*
1211          * Load the user's default, if any.
1212          */
1213         ksnprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1214                  periph->unit_number);
1215         TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1216
1217         /*
1218          * 6, 10, 12, and 16 are the currently permissible values.
1219          */
1220         if (softc->minimum_cmd_size < 6)
1221                 softc->minimum_cmd_size = 6;
1222         else if ((softc->minimum_cmd_size > 6)
1223               && (softc->minimum_cmd_size <= 10))
1224                 softc->minimum_cmd_size = 10;
1225         else if ((softc->minimum_cmd_size > 10)
1226               && (softc->minimum_cmd_size <= 12))
1227                 softc->minimum_cmd_size = 12;
1228         else if (softc->minimum_cmd_size > 12)
1229                 softc->minimum_cmd_size = 16;
1230
1231         /*
1232          * The DA driver supports a blocksize, but
1233          * we don't know the blocksize until we do 
1234          * a read capacity.  So, set a flag to
1235          * indicate that the blocksize is 
1236          * unavailable right now.  We'll clear the
1237          * flag as soon as we've done a read capacity.
1238          */
1239         devstat_add_entry(&softc->device_stats, "da", 
1240                           periph->unit_number, 0,
1241                           DEVSTAT_BS_UNAVAILABLE,
1242                           SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
1243                           DEVSTAT_PRIORITY_DISK);
1244
1245         /*
1246          * Register this media as a disk
1247          */
1248         CAM_SIM_UNLOCK(periph->sim);
1249         disk_create(periph->unit_number, &softc->disk, &da_ops);
1250         if (cpi.maxio == 0 || cpi.maxio > MAXPHYS)
1251                 softc->disk.d_rawdev->si_iosize_max = MAXPHYS;
1252         else
1253                 softc->disk.d_rawdev->si_iosize_max = cpi.maxio;
1254         if (bootverbose) {
1255                 kprintf("%s%d: si_iosize_max:%d\n",
1256                     periph->periph_name,
1257                     periph->unit_number,
1258                     softc->disk.d_rawdev->si_iosize_max);
1259         }
1260         CAM_SIM_LOCK(periph->sim);
1261
1262         /*
1263          * Add async callbacks for bus reset and
1264          * bus device reset calls.  I don't bother
1265          * checking if this fails as, in most cases,
1266          * the system will function just fine without
1267          * them and the only alternative would be to
1268          * not attach the device on failure.
1269          */
1270         xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
1271                            daasync, periph, periph->path);
1272
1273         /*
1274          * Take an exclusive refcount on the periph while dastart is called
1275          * to finish the probe.  The reference will be dropped in dadone at
1276          * the end of probe.
1277          */
1278         cam_periph_hold(periph, 0);
1279         xpt_schedule(periph, /*priority*/5);
1280
1281         /*
1282          * Schedule a periodic event to occasionally send an
1283          * ordered tag to a device.
1284          */
1285         callout_init(&softc->sendordered_c);
1286         callout_reset(&softc->sendordered_c,
1287             (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
1288             dasendorderedtag, softc);
1289
1290         
1291
1292         return(CAM_REQ_CMP);
1293 }
1294
1295 static void
1296 dastart(struct cam_periph *periph, union ccb *start_ccb)
1297 {
1298         struct da_softc *softc;
1299
1300         softc = (struct da_softc *)periph->softc;
1301
1302         switch (softc->state) {
1303         case DA_STATE_NORMAL:
1304         {
1305                 /* Pull a buffer from the queue and get going on it */          
1306                 struct bio *bio;
1307                 struct bio *bio_rd;
1308                 struct bio *bio_wr;
1309                 struct buf *bp;
1310                 u_int8_t tag_code;
1311                 int limit;
1312
1313                 /*
1314                  * See if there is a buf with work for us to do..
1315                  */
1316                 bio_rd = bioq_first(&softc->bio_queue_rd);
1317                 bio_wr = bioq_first(&softc->bio_queue_wr);
1318
1319                 if (periph->immediate_priority <= periph->pinfo.priority) {
1320                         CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1321                                         ("queuing for immediate ccb\n"));
1322                         start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1323                         SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1324                                           periph_links.sle);
1325                         periph->immediate_priority = CAM_PRIORITY_NONE;
1326                         wakeup(&periph->ccb_list);
1327                         if (bio_rd || bio_wr) {
1328                                 /*
1329                                  * Have more work to do, so ensure we stay
1330                                  * scheduled
1331                                  */
1332                                 xpt_schedule(periph, /* XXX priority */1);
1333                         }
1334                         break;
1335                 }
1336
1337                 /* Run the trim command if not already running */
1338                 if (!softc->trim_running &&
1339                    (bio = bioq_first(&softc->bio_queue_trim)) != NULL) {
1340                         struct trim_request *req = &softc->trim_req;
1341                         struct bio *bio1;
1342                         int bps = 0, ranges = 0;
1343
1344                         softc->trim_running = 1;
1345                         bzero(req, sizeof(*req));
1346                         bio1 = bio;
1347                         while (1) {
1348                                 uint64_t lba;
1349                                 int count;
1350
1351                                 bp = bio1->bio_buf;
1352                                 count = bp->b_bcount / softc->params.secsize;
1353                                 lba = bio1->bio_offset/softc->params.secsize;
1354
1355                                 kprintf("trim lba:%llu boff:%llu count:%d\n",
1356                                     (unsigned long long) lba,
1357                                     (unsigned long long) bio1->bio_offset,
1358                                     count);
1359
1360                                 bioq_remove(&softc->bio_queue_trim, bio1);
1361                                 while (count > 0) {
1362                                         int c = min(count, 0xffff);
1363                                         int off = ranges * 8;
1364
1365                                         req->data[off + 0] = lba & 0xff;
1366                                         req->data[off + 1] = (lba >> 8) & 0xff;
1367                                         req->data[off + 2] = (lba >> 16) & 0xff;
1368                                         req->data[off + 3] = (lba >> 24) & 0xff;
1369                                         req->data[off + 4] = (lba >> 32) & 0xff;
1370                                         req->data[off + 5] = (lba >> 40) & 0xff;
1371                                         req->data[off + 6] = c & 0xff;
1372                                         req->data[off + 7] = (c >> 8) & 0xff;
1373                                         lba += c;
1374                                         count -= c;
1375                                         ranges++;
1376                                 }
1377
1378                                 /* Try to merge multiple TRIM requests */
1379                                 req->bios[bps++] = bio1;
1380                                 bio1 = bioq_first(&softc->bio_queue_trim);
1381                                 if (bio1 == NULL ||
1382                                     bio1->bio_buf->b_bcount / softc->params.secsize >
1383                                     (softc->trim_max_ranges - ranges) * 0xffff)
1384                                         break;
1385                         }
1386
1387
1388                         cam_fill_csio(&start_ccb->csio,
1389                             1/*retries*/,
1390                             dadone,
1391                             CAM_DIR_OUT,
1392                             MSG_SIMPLE_Q_TAG,
1393                             req->data,
1394                             ((ranges +63)/64)*512,
1395                             SSD_FULL_SIZE,
1396                             sizeof(struct scsi_rw_6),
1397                             da_default_timeout*2);
1398
1399                         start_ccb->ccb_h.ccb_state = DA_CCB_TRIM;
1400                         LIST_INSERT_HEAD(&softc->pending_ccbs,
1401                             &start_ccb->ccb_h, periph_links.le);
1402                         start_ccb->csio.ccb_h.func_code = XPT_TRIM;
1403                         start_ccb->ccb_h.ccb_bio = bio;
1404                         devstat_start_transaction(&softc->device_stats);
1405                         xpt_action(start_ccb);
1406                         xpt_schedule(periph, 1);
1407                         break;
1408                 }
1409
1410                 /*
1411                  * Select a read or write buffer to queue.  Limit the number
1412                  * of tags dedicated to reading or writing, giving reads
1413                  * precedence.
1414                  *
1415                  * Writes to modern hard drives go into the HDs cache and
1416                  * return completion nearly instantly.  That is until the
1417                  * cache becomes full.  When the HDs cache becomes full
1418                  * write commands will begin to stall.  If all available
1419                  * tags are taken up by writes which saturate the drive
1420                  * reads will become tag-starved.
1421                  *
1422                  * A similar situation can occur with reads.  With many
1423                  * parallel readers all tags can be taken up by reads
1424                  * and prevent any writes from draining, even if the HD's
1425                  * cache is not full.
1426                  */
1427                 limit = periph->sim->max_tagged_dev_openings * 2 / 3 + 1;
1428 #if 0
1429                 /* DEBUGGING */
1430                 static int savets;
1431                 static long savets2;
1432                 if (1 || time_uptime != savets2 || (ticks != savets && (softc->outstanding_cmds_rd || softc->outstanding_cmds_wr))) {
1433                         kprintf("%d %d (%d)\n",
1434                                 softc->outstanding_cmds_rd,
1435                                 softc->outstanding_cmds_wr,
1436                                 limit);
1437                         savets = ticks;
1438                         savets2 = time_uptime;
1439                 }
1440 #endif
1441                 if (bio_rd && softc->outstanding_cmds_rd < limit) {
1442                         bio = bio_rd;
1443                         bioq_remove(&softc->bio_queue_rd, bio);
1444                 } else if (bio_wr && softc->outstanding_cmds_wr < limit) {
1445                         bio = bio_wr;
1446                         bioq_remove(&softc->bio_queue_wr, bio);
1447                 } else {
1448                         if (bio_rd)
1449                                 softc->flags |= DA_FLAG_RD_LIMIT;
1450                         if (bio_wr)
1451                                 softc->flags |= DA_FLAG_WR_LIMIT;
1452                         xpt_release_ccb(start_ccb);
1453                         break;
1454                 }
1455
1456                 /*
1457                  * We can queue new work.
1458                  */
1459                 bp = bio->bio_buf;
1460
1461                 devstat_start_transaction(&softc->device_stats);
1462
1463                 if ((bp->b_flags & B_ORDERED) != 0 ||
1464                     (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1465                         softc->flags &= ~DA_FLAG_NEED_OTAG;
1466                         softc->ordered_tag_count++;
1467                         tag_code = MSG_ORDERED_Q_TAG;
1468                 } else {
1469                         tag_code = MSG_SIMPLE_Q_TAG;
1470                 }
1471
1472                 switch(bp->b_cmd) {
1473                 case BUF_CMD_READ:
1474                 case BUF_CMD_WRITE:
1475                         /*
1476                          * Block read/write op
1477                          */
1478                         KKASSERT(bio->bio_offset % softc->params.secsize == 0);
1479
1480                         scsi_read_write(
1481                                 &start_ccb->csio,
1482                                 da_retry_count,         /* retries */
1483                                 dadone,
1484                                 tag_code,
1485                                 (bp->b_cmd == BUF_CMD_READ),
1486                                 0,                      /* byte2 */
1487                                 softc->minimum_cmd_size,
1488                                 bio->bio_offset / softc->params.secsize,
1489                                 bp->b_bcount / softc->params.secsize,
1490                                 bp->b_data,
1491                                 bp->b_bcount,
1492                                 SSD_FULL_SIZE,          /* sense_len */
1493                                 da_default_timeout * 1000
1494                         );
1495                         break;
1496                 case BUF_CMD_FLUSH:
1497                         /*
1498                          * Silently complete a flush request if the device
1499                          * cannot handle it.
1500                          */
1501                         if (softc->quirks & DA_Q_NO_SYNC_CACHE) {
1502                                 xpt_release_ccb(start_ccb);
1503                                 start_ccb = NULL;
1504                                 devstat_end_transaction_buf(
1505                                         &softc->device_stats, bp);
1506                                 biodone(bio);
1507                         } else {
1508                                 scsi_synchronize_cache(
1509                                         &start_ccb->csio,
1510                                         1,              /* retries */
1511                                         dadone,         /* cbfcnp */
1512                                         MSG_SIMPLE_Q_TAG,
1513                                         0,              /* lba */
1514                                         0,              /* count (whole disk) */
1515                                         SSD_FULL_SIZE,
1516                                         da_default_timeout*1000 /* timeout */
1517                                 );
1518                         }
1519                         break;
1520                 case BUF_CMD_FREEBLKS:
1521                         if (softc->disk.d_info.d_trimflag & DA_FLAG_CAN_TRIM){
1522                                 start_ccb->csio.ccb_h.func_code = XPT_TRIM;
1523                                 break;
1524                         }
1525                 default:
1526                         xpt_release_ccb(start_ccb);
1527                         start_ccb = NULL;
1528                         panic("dastart: unrecognized bio cmd %d", bp->b_cmd);
1529                         break; /* NOT REACHED */
1530                 }
1531
1532                 /*
1533                  * Block out any asyncronous callbacks
1534                  * while we touch the pending ccb list.
1535                  */
1536                 if (start_ccb) {
1537                         start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1538                         LIST_INSERT_HEAD(&softc->pending_ccbs,
1539                                          &start_ccb->ccb_h, periph_links.le);
1540                         if (bp->b_cmd == BUF_CMD_WRITE ||
1541                             bp->b_cmd == BUF_CMD_FLUSH) {
1542                                 ++softc->outstanding_cmds_wr;
1543                         } else {
1544                                 ++softc->outstanding_cmds_rd;
1545                         }
1546
1547                         /* We expect a unit attention from this device */
1548                         if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1549                                 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1550                                 softc->flags &= ~DA_FLAG_RETRY_UA;
1551                         }
1552
1553                         start_ccb->ccb_h.ccb_bio = bio;
1554                         xpt_action(start_ccb);
1555                 }
1556                 
1557                 /*
1558                  * Be sure we stay scheduled if we have more work to do.
1559                  */
1560                 if (bioq_first(&softc->bio_queue_rd) ||
1561                     bioq_first(&softc->bio_queue_wr)) {
1562                         xpt_schedule(periph, 1);
1563                 }
1564                 break;
1565         }
1566         case DA_STATE_PROBE:
1567         {
1568                 struct ccb_scsiio *csio;
1569                 struct scsi_read_capacity_data *rcap;
1570
1571                 rcap = kmalloc(sizeof(*rcap), M_SCSIDA, M_INTWAIT | M_ZERO);
1572                 csio = &start_ccb->csio;
1573                 scsi_read_capacity(csio,
1574                                    /*retries*/4,
1575                                    dadone,
1576                                    MSG_SIMPLE_Q_TAG,
1577                                    rcap,
1578                                    SSD_FULL_SIZE,
1579                                    /*timeout*/5000);
1580                 start_ccb->ccb_h.ccb_bio = NULL;
1581                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1582                 xpt_action(start_ccb);
1583                 break;
1584         }
1585         case DA_STATE_PROBE2:
1586         {
1587                 struct ccb_scsiio *csio;
1588                 struct scsi_read_capacity_data_16 *rcaplong;
1589
1590                 rcaplong = kmalloc(sizeof(*rcaplong), M_SCSIDA,
1591                                    M_INTWAIT | M_ZERO);
1592                 csio = &start_ccb->csio;
1593                 scsi_read_capacity_16(csio,
1594                                     /*retries*/ 4,
1595                                     /*cbfcnp*/ dadone,
1596                                     /*tag_action*/ MSG_SIMPLE_Q_TAG,
1597                                     /*lba*/ 0,
1598                                     /*reladr*/ 0,
1599                                     /*pmi*/ 0,
1600                                     rcaplong,
1601                                     /*sense_len*/ SSD_FULL_SIZE,
1602                                     /*timeout*/ 60000);
1603                 start_ccb->ccb_h.ccb_bio = NULL;
1604                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1605                 xpt_action(start_ccb);  
1606                 break;
1607         }
1608         }
1609 }
1610
1611 static int
1612 cmd6workaround(union ccb *ccb)
1613 {
1614         struct scsi_rw_6 cmd6;
1615         struct scsi_rw_10 *cmd10;
1616         struct da_softc *softc;
1617         u_int8_t *cdb;
1618         int frozen;
1619
1620         cdb = ccb->csio.cdb_io.cdb_bytes;
1621
1622         /* Translation only possible if CDB is an array and cmd is R/W6 */
1623         if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1624             (*cdb != READ_6 && *cdb != WRITE_6))
1625                 return 0;
1626
1627         xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
1628             "increasing minimum_cmd_size to 10.\n");
1629         softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1630         softc->minimum_cmd_size = 10;
1631
1632         bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1633         cmd10 = (struct scsi_rw_10 *)cdb;
1634         cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1635         cmd10->byte2 = 0;
1636         scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1637         cmd10->reserved = 0;
1638         scsi_ulto2b(cmd6.length, cmd10->length);
1639         cmd10->control = cmd6.control;
1640         ccb->csio.cdb_len = sizeof(*cmd10);
1641
1642         /* Requeue request, unfreezing queue if necessary */
1643         frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1644         ccb->ccb_h.status = CAM_REQUEUE_REQ;
1645         xpt_action(ccb);
1646         if (frozen) {
1647                 cam_release_devq(ccb->ccb_h.path,
1648                                  /*relsim_flags*/0,
1649                                  /*reduction*/0,
1650                                  /*timeout*/0,
1651                                  /*getcount_only*/0);
1652         }
1653         return (ERESTART);
1654 }
1655
1656 static void
1657 dadone(struct cam_periph *periph, union ccb *done_ccb)
1658 {
1659         struct da_softc *softc;
1660         struct ccb_scsiio *csio;
1661         struct disk_info info;
1662
1663         softc = (struct da_softc *)periph->softc;
1664         csio = &done_ccb->csio;
1665         switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1666         case DA_CCB_BUFFER_IO:
1667         case DA_CCB_TRIM:
1668         {
1669                 struct buf *bp;
1670                 struct bio *bio;
1671                 int mustsched = 0;
1672
1673                 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1674                 bp = bio->bio_buf;
1675                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1676                         int error;
1677                         int sf;
1678                         
1679                         if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1680                                 sf = SF_RETRY_UA;
1681                         else
1682                                 sf = 0;
1683
1684                         error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1685                         if (error == ERESTART) {
1686                                 /*
1687                                  * A retry was scheuled, so
1688                                  * just return.
1689                                  */
1690                                 return;
1691                         }
1692                         if (error != 0) {
1693                                 if (error == ENXIO) {
1694                                         /*
1695                                          * Catastrophic error.  Mark our pack as
1696                                          * invalid.
1697                                          */
1698                                         /*
1699                                          * XXX See if this is really a media
1700                                          * XXX change first?
1701                                          */
1702                                         xpt_print(periph->path,
1703                                             "Invalidating pack\n");
1704                                         softc->flags |= DA_FLAG_PACK_INVALID;
1705                                 }
1706
1707                                 /*
1708                                  * Return all queued write I/O's with EIO
1709                                  * so the client can retry these I/Os in the
1710                                  * proper order should it attempt to recover.
1711                                  *
1712                                  * Leave read I/O's alone.
1713                                  */
1714                                 daflushbioq(&softc->bio_queue_wr, EIO);
1715                                 bp->b_error = error;
1716                                 bp->b_resid = bp->b_bcount;
1717                                 bp->b_flags |= B_ERROR;
1718                         } else {
1719                                 bp->b_resid = csio->resid;
1720                                 bp->b_error = 0;
1721                                 if (bp->b_resid != 0)
1722                                         bp->b_flags |= B_ERROR;
1723                         }
1724                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1725                                 cam_release_devq(done_ccb->ccb_h.path,
1726                                                  /*relsim_flags*/0,
1727                                                  /*reduction*/0,
1728                                                  /*timeout*/0,
1729                                                  /*getcount_only*/0);
1730                 } else {
1731                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1732                                 panic("REQ_CMP with QFRZN");
1733                         bp->b_resid = csio->resid;
1734                         if (csio->resid > 0)
1735                                 bp->b_flags |= B_ERROR;
1736                 }
1737
1738                 /*
1739                  * Block out any asyncronous callbacks
1740                  * while we touch the pending ccb list.
1741                  */
1742                 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1743                 if (bp->b_cmd == BUF_CMD_WRITE || bp->b_cmd == BUF_CMD_FLUSH) {
1744                         --softc->outstanding_cmds_wr;
1745                         if (softc->flags & DA_FLAG_WR_LIMIT) {
1746                                 softc->flags &= ~DA_FLAG_WR_LIMIT;
1747                                 mustsched = 1;
1748                         }
1749                 } else {
1750                         --softc->outstanding_cmds_rd;
1751                         if (softc->flags & DA_FLAG_RD_LIMIT) {
1752                                 softc->flags &= ~DA_FLAG_RD_LIMIT;
1753                                 mustsched = 1;
1754                         }
1755                 }
1756                 if (softc->outstanding_cmds_rd +
1757                     softc->outstanding_cmds_wr == 0) {
1758                         softc->flags |= DA_FLAG_WENT_IDLE;
1759                 }
1760
1761                 devstat_end_transaction_buf(&softc->device_stats, bp);
1762                 if ((csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) ==
1763                     DA_CCB_TRIM) {
1764                         struct trim_request *req =
1765                             (struct trim_request *) csio->data_ptr;
1766                         int i;
1767
1768                         for (i = 1; i < softc->trim_max_ranges &&
1769                             req->bios[i]; i++) {
1770                                 struct bio *bp1 = req->bios[i];
1771
1772                                 bp1->bio_buf->b_resid = bp->b_resid;
1773                                 bp1->bio_buf->b_error = bp->b_error;
1774                                 if (bp->b_flags & B_ERROR)
1775                                         bp1->bio_buf->b_flags |= B_ERROR;
1776                                 biodone(bp1);
1777                         }
1778                         softc->trim_running = 0;
1779                         biodone(bio);
1780                         xpt_schedule(periph,1);
1781                 } else
1782                         biodone(bio);
1783
1784
1785                 if (mustsched)
1786                         xpt_schedule(periph, /*priority*/1);
1787
1788                 break;
1789         }
1790         case DA_CCB_PROBE:
1791         case DA_CCB_PROBE2:
1792         {
1793                 struct     scsi_read_capacity_data *rdcap;
1794                 struct     scsi_read_capacity_data_16 *rcaplong;
1795                 char       announce_buf[80];
1796
1797                 rdcap = NULL;
1798                 rcaplong = NULL;
1799                 if (softc->state == DA_STATE_PROBE)
1800                         rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1801                 else
1802                         rcaplong = (struct scsi_read_capacity_data_16 *)
1803                                     csio->data_ptr;
1804
1805                 bzero(&info, sizeof(info));
1806                 info.d_type = DTYPE_SCSI;
1807                 info.d_serialno = xpt_path_serialno(periph->path);
1808                 
1809                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1810                         struct disk_params *dp;
1811                         uint32_t block_size;
1812                         uint64_t maxsector;
1813
1814                         if (softc->state == DA_STATE_PROBE) {
1815                                 block_size = scsi_4btoul(rdcap->length);
1816                                 maxsector = scsi_4btoul(rdcap->addr);
1817
1818                                 /*
1819                                  * According to SBC-2, if the standard 10
1820                                  * byte READ CAPACITY command returns 2^32,
1821                                  * we should issue the 16 byte version of
1822                                  * the command, since the device in question
1823                                  * has more sectors than can be represented
1824                                  * with the short version of the command.
1825                                  */
1826                                 if (maxsector == 0xffffffff) {
1827                                         softc->state = DA_STATE_PROBE2;
1828                                         kfree(rdcap, M_SCSIDA);
1829                                         xpt_release_ccb(done_ccb);
1830                                         xpt_schedule(periph, /*priority*/5);
1831                                         return;
1832                                 }
1833                         } else {
1834                                 block_size = scsi_4btoul(rcaplong->length);
1835                                 maxsector = scsi_8btou64(rcaplong->addr);
1836                         }
1837                         dasetgeom(periph, block_size, maxsector);
1838                         dp = &softc->params;
1839                         ksnprintf(announce_buf, sizeof(announce_buf),
1840                                 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1841                                 (uintmax_t) (((uintmax_t)dp->secsize *
1842                                 dp->sectors) / (1024*1024)),
1843                                 (uintmax_t)dp->sectors,
1844                                 dp->secsize, dp->heads, dp->secs_per_track,
1845                                 dp->cylinders);
1846                         
1847                         CAM_SIM_UNLOCK(periph->sim);
1848                         info.d_media_blksize = softc->params.secsize;
1849                         info.d_media_blocks = softc->params.sectors;
1850                         info.d_media_size = 0;
1851                         info.d_secpertrack = softc->params.secs_per_track;
1852                         info.d_nheads = softc->params.heads;
1853                         info.d_ncylinders = softc->params.cylinders;
1854                         info.d_secpercyl = softc->params.heads *
1855                                                 softc->params.secs_per_track;
1856                         info.d_serialno = xpt_path_serialno(periph->path);
1857                         disk_setdiskinfo(&softc->disk, &info);
1858                         CAM_SIM_LOCK(periph->sim);
1859                 } else {
1860                         int     error;
1861
1862                         announce_buf[0] = '\0';
1863
1864                         /*
1865                          * Retry any UNIT ATTENTION type errors.  They
1866                          * are expected at boot.
1867                          */
1868                         error = daerror(done_ccb, CAM_RETRY_SELTO,
1869                                         SF_RETRY_UA|SF_NO_PRINT);
1870                         if (error == ERESTART) {
1871                                 /*
1872                                  * A retry was scheuled, so
1873                                  * just return.
1874                                  */
1875                                 return;
1876                         } else if (error != 0) {
1877                                 struct scsi_sense_data *sense;
1878                                 int asc, ascq;
1879                                 int sense_key, error_code;
1880                                 int have_sense;
1881                                 cam_status status;
1882                                 struct ccb_getdev cgd;
1883
1884                                 /* Don't wedge this device's queue */
1885                                 status = done_ccb->ccb_h.status;
1886                                 if ((status & CAM_DEV_QFRZN) != 0)
1887                                         cam_release_devq(done_ccb->ccb_h.path,
1888                                                          /*relsim_flags*/0,
1889                                                          /*reduction*/0,
1890                                                          /*timeout*/0,
1891                                                          /*getcount_only*/0);
1892
1893
1894                                 xpt_setup_ccb(&cgd.ccb_h, 
1895                                               done_ccb->ccb_h.path,
1896                                               /* priority */ 1);
1897                                 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1898                                 xpt_action((union ccb *)&cgd);
1899
1900                                 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1901                                  || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1902                                  || ((status & CAM_AUTOSNS_VALID) == 0))
1903                                         have_sense = FALSE;
1904                                 else
1905                                         have_sense = TRUE;
1906
1907                                 if (have_sense) {
1908                                         sense = &csio->sense_data;
1909                                         scsi_extract_sense(sense, &error_code,
1910                                                            &sense_key, 
1911                                                            &asc, &ascq);
1912                                 }
1913                                 /*
1914                                  * Attach to anything that claims to be a
1915                                  * direct access or optical disk device,
1916                                  * as long as it doesn't return a "Logical
1917                                  * unit not supported" (0x25) error.
1918                                  */
1919                                 if ((have_sense) && (asc != 0x25)
1920                                  && (error_code == SSD_CURRENT_ERROR)) {
1921                                         const char *sense_key_desc;
1922                                         const char *asc_desc;
1923
1924                                         scsi_sense_desc(sense_key, asc, ascq,
1925                                                         &cgd.inq_data,
1926                                                         &sense_key_desc,
1927                                                         &asc_desc);
1928                                         ksnprintf(announce_buf,
1929                                             sizeof(announce_buf),
1930                                                 "Attempt to query device "
1931                                                 "size failed: %s, %s",
1932                                                 sense_key_desc,
1933                                                 asc_desc);
1934                                         info.d_media_blksize = 512;
1935                                         disk_setdiskinfo(&softc->disk, &info);
1936                                 } else {
1937                                         if (have_sense)
1938                                                 scsi_sense_print(
1939                                                         &done_ccb->csio);
1940                                         else {
1941                                                 xpt_print(periph->path,
1942                                                     "got CAM status %#x\n",
1943                                                     done_ccb->ccb_h.status);
1944                                         }
1945
1946                                         xpt_print(periph->path, "fatal error, "
1947                                             "failed to attach to device\n");
1948
1949                                         /*
1950                                          * Free up resources.
1951                                          */
1952                                         cam_periph_invalidate(periph);
1953                                 } 
1954                         }
1955                 }
1956                 kfree(csio->data_ptr, M_SCSIDA);
1957                 if (announce_buf[0] != '\0') {
1958                         xpt_announce_periph(periph, announce_buf);
1959                         /*
1960                          * Create our sysctl variables, now that we know
1961                          * we have successfully attached.
1962                          */
1963                         taskqueue_enqueue(taskqueue_thread[mycpuid],
1964                             &softc->sysctl_task);
1965                 }
1966
1967                 if (softc->trim_max_ranges) {
1968                         softc->disk.d_info.d_trimflag |= DA_FLAG_CAN_TRIM;
1969                         kprintf("%s%d: supports TRIM\n",
1970                             periph->periph_name,
1971                             periph->unit_number);
1972                 }
1973                 softc->state = DA_STATE_NORMAL;
1974                 /*
1975                  * Since our peripheral may be invalidated by an error
1976                  * above or an external event, we must release our CCB
1977                  * before releasing the probe lock on the peripheral.
1978                  * The peripheral will only go away once the last lock
1979                  * is removed, and we need it around for the CCB release
1980                  * operation.
1981                  */
1982                 xpt_release_ccb(done_ccb);
1983                 cam_periph_unhold(periph, 0);
1984                 return;
1985         }
1986         case DA_CCB_WAITING:
1987         {
1988                 /* Caller will release the CCB */
1989                 wakeup(&done_ccb->ccb_h.cbfcnp);
1990                 return;
1991         }
1992         case DA_CCB_DUMP:
1993                 /* No-op.  We're polling */
1994                 return;
1995         default:
1996                 break;
1997         }
1998         xpt_release_ccb(done_ccb);
1999 }
2000
2001 static int
2002 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
2003 {
2004         struct da_softc   *softc;
2005         struct cam_periph *periph;
2006         int error;
2007
2008         periph = xpt_path_periph(ccb->ccb_h.path);
2009         softc = (struct da_softc *)periph->softc;
2010
2011         /*
2012          * Automatically detect devices that do not support
2013          * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
2014          */
2015         error = 0;
2016         if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
2017                 error = cmd6workaround(ccb);
2018         } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
2019                    CAM_SCSI_STATUS_ERROR)
2020          && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
2021          && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
2022          && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
2023          && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
2024                 int sense_key, error_code, asc, ascq;
2025
2026                 scsi_extract_sense(&ccb->csio.sense_data,
2027                                    &error_code, &sense_key, &asc, &ascq);
2028                 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
2029                         error = cmd6workaround(ccb);
2030         }
2031         if (error == ERESTART)
2032                 return (ERESTART);
2033
2034         /*
2035          * XXX
2036          * Until we have a better way of doing pack validation,
2037          * don't treat UAs as errors.
2038          */
2039         sense_flags |= SF_RETRY_UA;
2040         return(cam_periph_error(ccb, cam_flags, sense_flags,
2041                                 &softc->saved_ccb));
2042 }
2043
2044 static void
2045 daprevent(struct cam_periph *periph, int action)
2046 {
2047         struct  da_softc *softc;
2048         union   ccb *ccb;               
2049         int     error;
2050                 
2051         softc = (struct da_softc *)periph->softc;
2052
2053         if (((action == PR_ALLOW)
2054           && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
2055          || ((action == PR_PREVENT)
2056           && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
2057                 return;
2058         }
2059
2060         ccb = cam_periph_getccb(periph, /*priority*/1);
2061
2062         scsi_prevent(&ccb->csio,
2063                      /*retries*/1,
2064                      /*cbcfp*/dadone,
2065                      MSG_SIMPLE_Q_TAG,
2066                      action,
2067                      SSD_FULL_SIZE,
2068                      5000);
2069
2070         error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
2071                                   SF_RETRY_UA, &softc->device_stats);
2072
2073         if (error == 0) {
2074                 if (action == PR_ALLOW)
2075                         softc->flags &= ~DA_FLAG_PACK_LOCKED;
2076                 else
2077                         softc->flags |= DA_FLAG_PACK_LOCKED;
2078         }
2079
2080         xpt_release_ccb(ccb);
2081 }
2082
2083 /*
2084  * Check media on open, e.g. card reader devices which had no initial media.
2085  */
2086 static int
2087 dacheckmedia(struct cam_periph *periph)
2088 {
2089         struct disk_params *dp;
2090         struct da_softc *softc;
2091         struct disk_info info;
2092         int error;
2093
2094         softc = (struct da_softc *)periph->softc;
2095         dp = &softc->params;
2096
2097         error = dagetcapacity(periph);
2098
2099         /*
2100          * Only reprobe on initial open and if the media is removable.
2101          *
2102          * NOTE: If we setdiskinfo() it will take the device probe
2103          *       a bit of time to probe the slices and partitions,
2104          *       and mess up booting.  So avoid if nothing has changed.
2105          *       XXX
2106          */
2107         if (softc->flags & DA_FLAG_OPEN)
2108                 return (error);
2109         if ((softc->flags & DA_FLAG_PACK_REMOVABLE) == 0)
2110                 return (error);
2111
2112         bzero(&info, sizeof(info));
2113         info.d_type = DTYPE_SCSI;
2114         info.d_serialno = xpt_path_serialno(periph->path);
2115
2116         if (error == 0) {
2117                 CAM_SIM_UNLOCK(periph->sim);
2118                 info.d_media_blksize = softc->params.secsize;
2119                 info.d_media_blocks = softc->params.sectors;
2120                 info.d_media_size = 0;
2121                 info.d_secpertrack = softc->params.secs_per_track;
2122                 info.d_nheads = softc->params.heads;
2123                 info.d_ncylinders = softc->params.cylinders;
2124                 info.d_secpercyl = softc->params.heads *
2125                                         softc->params.secs_per_track;
2126                 info.d_serialno = xpt_path_serialno(periph->path);
2127                 if (info.d_media_blocks != softc->disk.d_info.d_media_blocks) {
2128                         kprintf("%s%d: open removable media: "
2129                                 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)\n",
2130                                 periph->periph_name, periph->unit_number,
2131                                 (uintmax_t)(((uintmax_t)dp->secsize *
2132                                              dp->sectors) / (1024*1024)),
2133                                 (uintmax_t)dp->sectors, dp->secsize,
2134                                 dp->heads, dp->secs_per_track, dp->cylinders);
2135                         disk_setdiskinfo(&softc->disk, &info);
2136                 }
2137                 CAM_SIM_LOCK(periph->sim);
2138         } else {
2139                 kprintf("%s%d: open removable media: no media present\n",
2140                         periph->periph_name, periph->unit_number);
2141                 info.d_media_blksize = 512;
2142                 disk_setdiskinfo(&softc->disk, &info);
2143         }
2144         return (error);
2145 }
2146
2147 static int
2148 dagetcapacity(struct cam_periph *periph)
2149 {
2150         struct da_softc *softc;
2151         union ccb *ccb;
2152         struct scsi_read_capacity_data *rcap;
2153         struct scsi_read_capacity_data_16 *rcaplong;
2154         uint32_t block_len;
2155         uint64_t maxsector;
2156         int error;
2157  
2158         softc = (struct da_softc *)periph->softc;
2159         block_len = 0;
2160         maxsector = 0;
2161         error = 0;
2162  
2163         /* Do a read capacity */
2164         rcap = (struct scsi_read_capacity_data *)kmalloc(sizeof(*rcaplong),
2165                                                          M_SCSIDA, M_INTWAIT);
2166                 
2167         ccb = cam_periph_getccb(periph, /*priority*/1);
2168         scsi_read_capacity(&ccb->csio,
2169                            /*retries*/4,
2170                            /*cbfncp*/dadone,
2171                            MSG_SIMPLE_Q_TAG,
2172                            rcap,
2173                            SSD_FULL_SIZE,
2174                            /*timeout*/60000);
2175         ccb->ccb_h.ccb_bio = NULL;
2176  
2177         error = cam_periph_runccb(ccb, daerror,
2178                                   /*cam_flags*/CAM_RETRY_SELTO,
2179                                   /*sense_flags*/SF_RETRY_UA,
2180                                   &softc->device_stats);
2181  
2182         if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2183                 cam_release_devq(ccb->ccb_h.path,
2184                                  /*relsim_flags*/0,
2185                                  /*reduction*/0,
2186                                  /*timeout*/0,
2187                                  /*getcount_only*/0);
2188  
2189         if (error == 0) {
2190                 block_len = scsi_4btoul(rcap->length);
2191                 maxsector = scsi_4btoul(rcap->addr);
2192
2193                 if (maxsector != 0xffffffff)
2194                         goto done;
2195         } else
2196                 goto done;
2197  
2198         rcaplong = (struct scsi_read_capacity_data_16 *)rcap;
2199  
2200         scsi_read_capacity_16(&ccb->csio,
2201                               /*retries*/ 4,
2202                               /*cbfcnp*/ dadone,
2203                               /*tag_action*/ MSG_SIMPLE_Q_TAG,
2204                               /*lba*/ 0,
2205                               /*reladr*/ 0,
2206                               /*pmi*/ 0,
2207                               rcaplong,
2208                               /*sense_len*/ SSD_FULL_SIZE,
2209                               /*timeout*/ 60000);
2210         ccb->ccb_h.ccb_bio = NULL;
2211  
2212         error = cam_periph_runccb(ccb, daerror,
2213                                   /*cam_flags*/CAM_RETRY_SELTO,
2214                                   /*sense_flags*/SF_RETRY_UA,
2215                                   &softc->device_stats);
2216  
2217         if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2218                 cam_release_devq(ccb->ccb_h.path,
2219                                  /*relsim_flags*/0,
2220                                  /*reduction*/0,
2221                                  /*timeout*/0,
2222                                  /*getcount_only*/0);
2223  
2224         if (error == 0) {
2225                 block_len = scsi_4btoul(rcaplong->length);
2226                 maxsector = scsi_8btou64(rcaplong->addr);
2227         }
2228
2229 done:
2230
2231         if (error == 0)
2232                 dasetgeom(periph, block_len, maxsector);
2233  
2234         xpt_release_ccb(ccb);
2235  
2236         kfree(rcap, M_SCSIDA);
2237  
2238         return (error);
2239 }
2240
2241 static void
2242 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector)
2243 {
2244         struct ccb_calc_geometry ccg;
2245         struct da_softc *softc;
2246         struct disk_params *dp;
2247
2248         softc = (struct da_softc *)periph->softc;
2249
2250         dp = &softc->params;
2251         dp->secsize = block_len;
2252         dp->sectors = maxsector + 1;
2253         /*
2254          * Have the controller provide us with a geometry
2255          * for this disk.  The only time the geometry
2256          * matters is when we boot and the controller
2257          * is the only one knowledgeable enough to come
2258          * up with something that will make this a bootable
2259          * device.
2260          */
2261         xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
2262         ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
2263         ccg.block_size = dp->secsize;
2264         ccg.volume_size = dp->sectors;
2265         ccg.heads = 0;
2266         ccg.secs_per_track = 0;
2267         ccg.cylinders = 0;
2268         xpt_action((union ccb*)&ccg);
2269         if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2270                 /*
2271                  * We don't know what went wrong here- but just pick
2272                  * a geometry so we don't have nasty things like divide
2273                  * by zero.
2274                  */
2275                 dp->heads = 255;
2276                 dp->secs_per_track = 255;
2277                 dp->cylinders = dp->sectors / (255 * 255);
2278                 if (dp->cylinders == 0) {
2279                         dp->cylinders = 1;
2280                 }
2281         } else {
2282                 dp->heads = ccg.heads;
2283                 dp->secs_per_track = ccg.secs_per_track;
2284                 dp->cylinders = ccg.cylinders;
2285         }
2286 }
2287
2288 static void
2289 dasendorderedtag(void *arg)
2290 {
2291         struct da_softc *softc = arg;
2292
2293         if (da_send_ordered) {
2294                 if ((softc->ordered_tag_count == 0)
2295                  && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
2296                         softc->flags |= DA_FLAG_NEED_OTAG;
2297                 }
2298                 if (softc->outstanding_cmds_rd || softc->outstanding_cmds_wr)
2299                         softc->flags &= ~DA_FLAG_WENT_IDLE;
2300
2301                 softc->ordered_tag_count = 0;
2302         }
2303         /* Queue us up again */
2304         callout_reset(&softc->sendordered_c,
2305             (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
2306             dasendorderedtag, softc);
2307 }
2308
2309 /*
2310  * Step through all DA peripheral drivers, and if the device is still open,
2311  * sync the disk cache to physical media.
2312  */
2313 static void
2314 dashutdown(void * arg, int howto)
2315 {
2316         struct cam_periph *periph;
2317         struct da_softc *softc;
2318
2319         TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
2320                 union ccb ccb;
2321
2322                 cam_periph_lock(periph);
2323                 softc = (struct da_softc *)periph->softc;
2324
2325                 /*
2326                  * We only sync the cache if the drive is still open, and
2327                  * if the drive is capable of it..
2328                  */
2329                 if (((softc->flags & DA_FLAG_OPEN) == 0)
2330                  || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
2331                         cam_periph_unlock(periph);
2332                         continue;
2333                 }
2334
2335                 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
2336
2337                 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
2338                 scsi_synchronize_cache(&ccb.csio,
2339                                        /*retries*/1,
2340                                        /*cbfcnp*/dadone,
2341                                        MSG_SIMPLE_Q_TAG,
2342                                        /*begin_lba*/0, /* whole disk */
2343                                        /*lb_count*/0,
2344                                        SSD_FULL_SIZE,
2345                                        60 * 60 * 1000);
2346
2347                 xpt_polled_action(&ccb);
2348
2349                 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2350                         if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2351                              CAM_SCSI_STATUS_ERROR)
2352                          && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
2353                                 int error_code, sense_key, asc, ascq;
2354
2355                                 scsi_extract_sense(&ccb.csio.sense_data,
2356                                                    &error_code, &sense_key,
2357                                                    &asc, &ascq);
2358
2359                                 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
2360                                         scsi_sense_print(&ccb.csio);
2361                         } else {
2362                                 xpt_print(periph->path, "Synchronize "
2363                                     "cache failed, status == 0x%x, scsi status "
2364                                     "== 0x%x\n", ccb.ccb_h.status,
2365                                     ccb.csio.scsi_status);
2366                         }
2367                 }
2368
2369                 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
2370                         cam_release_devq(ccb.ccb_h.path,
2371                                          /*relsim_flags*/0,
2372                                          /*reduction*/0,
2373                                          /*timeout*/0,
2374                                          /*getcount_only*/0);
2375
2376                 cam_periph_unlock(periph);
2377         }
2378 }
2379
2380 #else /* !_KERNEL */
2381
2382 /*
2383  * XXX This is only left out of the kernel build to silence warnings.  If,
2384  * for some reason this function is used in the kernel, the ifdefs should
2385  * be moved so it is included both in the kernel and userland.
2386  */
2387 void
2388 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2389                  void (*cbfcnp)(struct cam_periph *, union ccb *),
2390                  u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2391                  u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2392                  u_int32_t timeout)
2393 {
2394         struct scsi_format_unit *scsi_cmd;
2395
2396         scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2397         scsi_cmd->opcode = FORMAT_UNIT;
2398         scsi_cmd->byte2 = byte2;
2399         scsi_ulto2b(ileave, scsi_cmd->interleave);
2400
2401         cam_fill_csio(csio,
2402                       retries,
2403                       cbfcnp,
2404                       /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2405                       tag_action,
2406                       data_ptr,
2407                       dxfer_len,
2408                       sense_len,
2409                       sizeof(*scsi_cmd),
2410                       timeout);
2411 }
2412
2413 #endif /* _KERNEL */