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