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