kernel - Fix TRIM bugs in UFS
[dragonfly.git] / sys / bus / cam / scsi / scsi_da.c
blobd57f4ad23e91a56af7feaa012dc65d48154f9cc4
1 /*
2 * Implementation of SCSI Direct Access Peripheral driver for CAM.
4 * Copyright (c) 1997 Justin T. Gibbs.
5 * All rights reserved.
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.
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.
28 * $FreeBSD: src/sys/cam/scsi/scsi_da.c,v 1.42.2.46 2003/10/21 22:18:19 thomas Exp $
31 #include <sys/param.h>
33 #ifdef _KERNEL
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/buf.h>
38 #include <sys/sysctl.h>
39 #include <sys/taskqueue.h>
40 #include <sys/lock.h>
41 #include <sys/conf.h>
42 #include <sys/devicestat.h>
43 #include <sys/disk.h>
44 #include <sys/dtype.h>
45 #include <sys/eventhandler.h>
46 #include <sys/malloc.h>
47 #include <sys/cons.h>
48 #include <sys/proc.h>
49 #include <sys/ioctl_compat.h>
51 #include <sys/buf2.h>
52 #include <sys/thread2.h>
53 #include <sys/mplock2.h>
55 #endif /* _KERNEL */
57 #ifdef _KERNEL
58 #include <vm/pmap.h>
59 #endif
61 #ifndef _KERNEL
62 #include <stdio.h>
63 #include <string.h>
64 #endif /* _KERNEL */
66 #include <sys/camlib.h>
67 #include "../cam.h"
68 #include "../cam_ccb.h"
69 #include "../cam_extend.h"
70 #include "../cam_periph.h"
71 #include "../cam_xpt_periph.h"
72 #include "../cam_sim.h"
74 #include "scsi_message.h"
76 #ifndef _KERNEL
77 #include "scsi_da.h"
78 #endif /* !_KERNEL */
80 #ifdef _KERNEL
81 typedef enum {
82 DA_STATE_PROBE,
83 DA_STATE_PROBE2,
84 DA_STATE_NORMAL
85 } da_state;
87 typedef enum {
88 DA_FLAG_PACK_INVALID = 0x001,
89 DA_FLAG_NEW_PACK = 0x002,
90 DA_FLAG_PACK_LOCKED = 0x004,
91 DA_FLAG_PACK_REMOVABLE = 0x008,
92 DA_FLAG_TAGGED_QUEUING = 0x010,
93 DA_FLAG_NEED_OTAG = 0x020,
94 DA_FLAG_WENT_IDLE = 0x040,
95 DA_FLAG_RETRY_UA = 0x080,
96 DA_FLAG_OPEN = 0x100,
97 DA_FLAG_SCTX_INIT = 0x200,
98 DA_FLAG_RD_LIMIT = 0x400,
99 DA_FLAG_WR_LIMIT = 0x800,
100 DA_FLAG_CAN_TRIM = 0x1000
101 } da_flags;
103 typedef enum {
104 DA_Q_NONE = 0x00,
105 DA_Q_NO_SYNC_CACHE = 0x01,
106 DA_Q_NO_6_BYTE = 0x02,
107 DA_Q_NO_PREVENT = 0x04
108 } da_quirks;
110 typedef enum {
111 DA_CCB_PROBE = 0x01,
112 DA_CCB_PROBE2 = 0x02,
113 DA_CCB_BUFFER_IO = 0x03,
114 DA_CCB_WAITING = 0x04,
115 DA_CCB_DUMP = 0x05,
116 DA_CCB_TRIM = 0x06,
117 DA_CCB_TYPE_MASK = 0x0F,
118 DA_CCB_RETRY_UA = 0x10
119 } da_ccb_state;
121 /* Offsets into our private area for storing information */
122 #define ccb_state ppriv_field0
123 #define ccb_bio ppriv_ptr1
125 struct disk_params {
126 u_int8_t heads;
127 u_int32_t cylinders;
128 u_int8_t secs_per_track;
129 u_int32_t secsize; /* Number of bytes/sector */
130 u_int64_t sectors; /* total number sectors */
133 #define TRIM_MAX_BLOCKS 8
134 #define TRIM_MAX_RANGES TRIM_MAX_BLOCKS * 64
135 struct trim_request {
136 uint8_t data[TRIM_MAX_RANGES * 8];
137 struct bio *bios[TRIM_MAX_RANGES];
140 struct da_softc {
141 struct bio_queue_head bio_queue_rd;
142 struct bio_queue_head bio_queue_wr;
143 struct bio_queue_head bio_queue_trim;
144 struct devstat device_stats;
145 SLIST_ENTRY(da_softc) links;
146 LIST_HEAD(, ccb_hdr) pending_ccbs;
147 da_state state;
148 da_flags flags;
149 da_quirks quirks;
150 int minimum_cmd_size;
151 int ordered_tag_count;
152 int outstanding_cmds_rd;
153 int outstanding_cmds_wr;
154 int trim_max_ranges;
155 int trim_running;
156 int trim_enabled;
157 struct disk_params params;
158 struct disk disk;
159 union ccb saved_ccb;
160 struct task sysctl_task;
161 struct sysctl_ctx_list sysctl_ctx;
162 struct sysctl_oid *sysctl_tree;
163 struct callout sendordered_c;
164 struct trim_request trim_req;
167 struct da_quirk_entry {
168 struct scsi_inquiry_pattern inq_pat;
169 da_quirks quirks;
172 static const char quantum[] = "QUANTUM";
173 static const char microp[] = "MICROP";
175 static struct da_quirk_entry da_quirk_table[] =
177 /* SPI, FC devices */
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>
185 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
186 /*quirks*/ DA_Q_NO_SYNC_CACHE
189 /* See above. */
190 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
191 /*quirks*/ DA_Q_NO_SYNC_CACHE
195 * This particular Fujitsu drive doesn't like the
196 * synchronize cache command.
197 * Reported by: Tom Jackson <toj@gorilla.net>
199 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
200 /*quirks*/ DA_Q_NO_SYNC_CACHE
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.
208 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
209 /*quirks*/ DA_Q_NO_SYNC_CACHE
213 * This drive doesn't like the synchronize cache command
214 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
215 * (PR 8882).
217 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
218 /*quirks*/ DA_Q_NO_SYNC_CACHE
222 * Doesn't like the synchronize cache command.
223 * Reported by: Blaz Zupan <blaz@gold.amis.net>
225 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
226 /*quirks*/ DA_Q_NO_SYNC_CACHE
230 * Doesn't like the synchronize cache command.
231 * Reported by: Blaz Zupan <blaz@gold.amis.net>
233 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
234 /*quirks*/ DA_Q_NO_SYNC_CACHE
238 * Doesn't like the synchronize cache command.
240 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
241 /*quirks*/ DA_Q_NO_SYNC_CACHE
245 * Doesn't like the synchronize cache command.
246 * Reported by: walter@pelissero.de
248 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
249 /*quirks*/ DA_Q_NO_SYNC_CACHE
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>
258 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
259 /*quirks*/ DA_Q_NO_6_BYTE
262 /* See above. */
263 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
264 /*quirks*/ DA_Q_NO_6_BYTE
268 * Doesn't like the synchronize cache command.
269 * Reported by: walter@pelissero.de
271 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
272 /*quirks*/ DA_Q_NO_SYNC_CACHE
276 * The CISS RAID controllers do not support SYNC_CACHE
278 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
279 /*quirks*/ DA_Q_NO_SYNC_CACHE
283 * The same goes for the mly(4) controllers
285 {T_DIRECT, SIP_MEDIA_FIXED, "MLY*", "*", "MYLX"},
286 /*quirks*/ DA_Q_NO_SYNC_CACHE
289 * USB mass storage devices supported by umass(4)
291 * NOTE: USB attachments automatically set DA_Q_NO_SYNC_CACHE so
292 * it does not have to be specified here.
296 * Creative Nomad MUVO mp3 player (USB)
297 * PR: kern/53094
299 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
300 /*quirks*/ DA_Q_NO_PREVENT
304 * Sigmatel USB Flash MP3 Player
305 * PR: kern/57046
307 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
308 /*quirks*/ DA_Q_NO_PREVENT
312 * SEAGRAND NP-900 MP3 Player
313 * PR: kern/64563
315 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
316 /*quirks*/ DA_Q_NO_PREVENT
320 * Creative MUVO Slim mp3 player (USB)
321 * PR: usb/86131
323 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
324 "*"}, /*quirks*/ DA_Q_NO_PREVENT
328 * Philips USB Key Audio KEY013
329 * PR: usb/68412
331 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
332 /*quirks*/ DA_Q_NO_PREVENT
336 static d_open_t daopen;
337 static d_close_t daclose;
338 static d_strategy_t dastrategy;
339 static d_dump_t dadump;
340 static d_ioctl_t daioctl;
341 static periph_init_t dainit;
342 static void daasync(void *callback_arg, u_int32_t code,
343 struct cam_path *path, void *arg);
344 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
345 static periph_ctor_t daregister;
346 static periph_dtor_t dacleanup;
347 static periph_start_t dastart;
348 static periph_oninv_t daoninvalidate;
349 static void dadone(struct cam_periph *periph,
350 union ccb *done_ccb);
351 static int daerror(union ccb *ccb, u_int32_t cam_flags,
352 u_int32_t sense_flags);
353 static void daprevent(struct cam_periph *periph, int action);
354 static int dagetcapacity(struct cam_periph *periph);
355 static int dacheckmedia(struct cam_periph *periph);
356 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
357 uint64_t maxsector);
358 static void daflushbioq(struct bio_queue_head *bioq, int error);
359 static timeout_t dasendorderedtag;
360 static void dashutdown(void *arg, int howto);
362 #ifndef DA_DEFAULT_TIMEOUT
363 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
364 #endif
366 #ifndef DA_DEFAULT_RETRY
367 #define DA_DEFAULT_RETRY 4
368 #endif
370 #ifndef DA_DEFAULT_SEND_ORDERED
371 #define DA_DEFAULT_SEND_ORDERED 1
372 #endif
374 static int da_retry_count = DA_DEFAULT_RETRY;
375 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
376 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
377 static struct callout dasendorderedtag_ch;
379 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
380 "CAM Direct Access Disk driver");
381 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
382 &da_retry_count, 0, "Normal I/O retry count");
383 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
384 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
385 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
386 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
387 SYSCTL_INT(_kern_cam_da, OID_AUTO, da_send_ordered, CTLFLAG_RW,
388 &da_send_ordered, 0, "Send Ordered Tags");
389 TUNABLE_INT("kern.cam.da.da_send_ordered", &da_send_ordered);
392 * DA_ORDEREDTAG_INTERVAL determines how often, relative
393 * to the default timeout, we check to see whether an ordered
394 * tagged transaction is appropriate to prevent simple tag
395 * starvation. Since we'd like to ensure that there is at least
396 * 1/2 of the timeout length left for a starved transaction to
397 * complete after we've sent an ordered tag, we must poll at least
398 * four times in every timeout period. This takes care of the worst
399 * case where a starved transaction starts during an interval that
400 * meets the requirement "don't send an ordered tag" test so it takes
401 * us two intervals to determine that a tag must be sent.
403 #ifndef DA_ORDEREDTAG_INTERVAL
404 #define DA_ORDEREDTAG_INTERVAL 4
405 #endif
407 static struct periph_driver dadriver =
409 dainit, "da",
410 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
413 PERIPHDRIVER_DECLARE(da, dadriver);
415 static struct dev_ops da_ops = {
416 { "da", 0, D_DISK | D_MPSAFE },
417 .d_open = daopen,
418 .d_close = daclose,
419 .d_read = physread,
420 .d_write = physwrite,
421 .d_strategy = dastrategy,
422 .d_dump = dadump,
423 .d_ioctl = daioctl
426 static struct extend_array *daperiphs;
428 MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
430 static int
431 daioctl(struct dev_ioctl_args *ap)
433 int unit;
434 int error = 0;
435 struct buf *bp;
436 struct cam_periph *periph;
437 int byte_count;
439 off_t *del_num = (off_t*)ap->a_data;
440 off_t bytes_left;
441 off_t bytes_start;
443 cdev_t dev = ap->a_head.a_dev;
446 unit = dkunit(dev);
447 periph = cam_extend_get(daperiphs, unit);
448 if (periph == NULL)
449 return(ENXIO);
451 switch (ap->a_cmd) {
452 case IOCTLTRIM:
455 bytes_left = del_num[1];
456 bytes_start = del_num[0];
458 /* TRIM occurs on 512-byte sectors. */
459 KKASSERT((bytes_left % 512) == 0);
460 KKASSERT((bytes_start% 512) == 0);
463 /* Break TRIM up into int-sized commands because of b_bcount */
464 while(bytes_left) {
467 * Rather than than squezing out more blocks in b_bcount
468 * and having to break up the TRIM request in da_start(),
469 * we ensure we can always TRIM this many bytes with one
470 * TRIM command (this happens if the device only
471 * supports one TRIM block).
473 * With min TRIM blksize of 1, TRIM command free
474 * 4194240 blks(64*65535): each LBA range can address
475 * 65535 blks and there 64 such ranges in a 512-byte
476 * block. And, 4194240 * 512 = 0x7FFF8000
479 byte_count = MIN(bytes_left,0x7FFF8000);
480 bp = getnewbuf(0, 0, 0, 1, NULL);
482 bp->b_cmd = BUF_CMD_FREEBLKS;
483 bp->b_bio1.bio_offset = bytes_start;
484 bp->b_bcount = byte_count;
485 bp->b_bio1.bio_flags |= BIO_SYNC;
486 bp->b_bio1.bio_done = biodone_sync;
488 dev_dstrategy(ap->a_head.a_dev, &bp->b_bio1);
490 if (biowait(&bp->b_bio1, "TRIM")) {
491 kprintf("Error:%d\n", bp->b_error);
492 brelse(bp);
493 return(bp->b_error ? bp->b_error : EIO);
495 brelse(bp);
496 bytes_left -= byte_count;
497 bytes_start += byte_count;
499 break;
501 default:
502 return(EINVAL);
505 return(error);
508 static int
509 daopen(struct dev_open_args *ap)
511 cdev_t dev = ap->a_head.a_dev;
512 struct cam_periph *periph;
513 struct da_softc *softc;
514 struct disk_info info;
515 int unit;
516 int error;
518 unit = dkunit(dev);
519 periph = cam_extend_get(daperiphs, unit);
520 if (periph == NULL) {
521 return (ENXIO);
524 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
525 return(ENXIO);
528 cam_periph_lock(periph);
529 if ((error = cam_periph_hold(periph, PCATCH)) != 0) {
530 cam_periph_unlock(periph);
531 cam_periph_release(periph);
532 return (error);
535 unit = periph->unit_number;
536 softc = (struct da_softc *)periph->softc;
538 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
539 ("daopen: dev=%s (unit %d)\n", devtoname(dev),
540 unit));
542 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
543 /* Invalidate our pack information. */
544 disk_invalidate(&softc->disk);
545 softc->flags &= ~DA_FLAG_PACK_INVALID;
548 error = dacheckmedia(periph);
549 softc->flags |= DA_FLAG_OPEN;
551 if (error == 0) {
552 struct ccb_getdev cgd;
554 /* Build disk information structure */
555 bzero(&info, sizeof(info));
556 info.d_type = DTYPE_SCSI;
559 * Grab the inquiry data to get the vendor and product names.
560 * Put them in the typename and packname for the label.
562 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
563 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
564 xpt_action((union ccb *)&cgd);
567 * Check to see whether or not the blocksize is set yet.
568 * If it isn't, set it and then clear the blocksize
569 * unavailable flag for the device statistics.
571 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0){
572 softc->device_stats.block_size = softc->params.secsize;
573 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
577 if (error == 0) {
578 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
579 (softc->quirks & DA_Q_NO_PREVENT) == 0)
580 daprevent(periph, PR_PREVENT);
581 } else {
582 softc->flags &= ~DA_FLAG_OPEN;
583 cam_periph_release(periph);
585 cam_periph_unhold(periph, 1);
586 return (error);
589 static int
590 daclose(struct dev_close_args *ap)
592 cdev_t dev = ap->a_head.a_dev;
593 struct cam_periph *periph;
594 struct da_softc *softc;
595 int unit;
596 int error;
598 unit = dkunit(dev);
599 periph = cam_extend_get(daperiphs, unit);
600 if (periph == NULL)
601 return (ENXIO);
603 cam_periph_lock(periph);
604 if ((error = cam_periph_hold(periph, 0)) != 0) {
605 cam_periph_unlock(periph);
606 cam_periph_release(periph);
607 return (error);
610 softc = (struct da_softc *)periph->softc;
612 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
613 union ccb *ccb;
615 ccb = cam_periph_getccb(periph, /*priority*/1);
617 scsi_synchronize_cache(&ccb->csio,
618 /*retries*/1,
619 /*cbfcnp*/dadone,
620 MSG_SIMPLE_Q_TAG,
621 /*begin_lba*/0,/* Cover the whole disk */
622 /*lb_count*/0,
623 SSD_FULL_SIZE,
624 5 * 60 * 1000);
626 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
627 /*sense_flags*/SF_RETRY_UA,
628 &softc->device_stats);
630 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
631 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
632 CAM_SCSI_STATUS_ERROR) {
633 int asc, ascq;
634 int sense_key, error_code;
636 scsi_extract_sense(&ccb->csio.sense_data,
637 &error_code,
638 &sense_key,
639 &asc, &ascq);
640 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
641 scsi_sense_print(&ccb->csio);
642 } else {
643 xpt_print(periph->path, "Synchronize cache "
644 "failed, status == 0x%x, scsi status == "
645 "0x%x\n", ccb->csio.ccb_h.status,
646 ccb->csio.scsi_status);
650 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
651 cam_release_devq(ccb->ccb_h.path,
652 /*relsim_flags*/0,
653 /*reduction*/0,
654 /*timeout*/0,
655 /*getcount_only*/0);
657 xpt_release_ccb(ccb);
661 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
662 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
663 daprevent(periph, PR_ALLOW);
665 * If we've got removeable media, mark the blocksize as
666 * unavailable, since it could change when new media is
667 * inserted.
669 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
673 * Don't compound any ref counting software bugs with more.
675 if (softc->flags & DA_FLAG_OPEN) {
676 softc->flags &= ~DA_FLAG_OPEN;
677 cam_periph_release(periph);
678 } else {
679 xpt_print(periph->path,
680 "daclose() called on an already closed device!\n");
682 cam_periph_unhold(periph, 1);
683 return (0);
687 * Actually translate the requested transfer into one the physical driver
688 * can understand. The transfer is described by a buf and will include
689 * only one physical transfer.
691 static int
692 dastrategy(struct dev_strategy_args *ap)
694 cdev_t dev = ap->a_head.a_dev;
695 struct bio *bio = ap->a_bio;
696 struct buf *bp = bio->bio_buf;
697 struct cam_periph *periph;
698 struct da_softc *softc;
699 u_int unit;
701 unit = dkunit(dev);
702 periph = cam_extend_get(daperiphs, unit);
703 if (periph == NULL) {
704 bp->b_error = ENXIO;
705 goto bad;
707 softc = (struct da_softc *)periph->softc;
709 cam_periph_lock(periph);
711 #if 0
713 * check it's not too big a transfer for our adapter
715 scsi_minphys(bp, &sd_switch);
716 #endif
719 * Mask interrupts so that the pack cannot be invalidated until
720 * after we are in the queue. Otherwise, we might not properly
721 * clean up one of the buffers.
725 * If the device has been made invalid, error out
727 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
728 cam_periph_unlock(periph);
729 bp->b_error = ENXIO;
730 goto bad;
734 * Place it in the queue of disk activities for this disk
736 if (bp->b_cmd == BUF_CMD_WRITE || bp->b_cmd == BUF_CMD_FLUSH)
737 bioqdisksort(&softc->bio_queue_wr, bio);
738 else if (bp->b_cmd == BUF_CMD_FREEBLKS)
739 bioqdisksort(&softc->bio_queue_trim, bio);
740 else
741 bioqdisksort(&softc->bio_queue_rd, bio);
744 * Schedule ourselves for performing the work.
746 xpt_schedule(periph, /* XXX priority */1);
747 cam_periph_unlock(periph);
749 return(0);
750 bad:
751 bp->b_flags |= B_ERROR;
754 * Correctly set the buf to indicate a completed xfer
756 bp->b_resid = bp->b_bcount;
757 biodone(bio);
758 return(0);
761 static int
762 dadump(struct dev_dump_args *ap)
764 cdev_t dev = ap->a_head.a_dev;
765 struct cam_periph *periph;
766 struct da_softc *softc;
767 u_int unit;
768 u_int32_t secsize;
769 struct ccb_scsiio csio;
771 unit = dkunit(dev);
772 periph = cam_extend_get(daperiphs, unit);
773 if (periph == NULL)
774 return (ENXIO);
776 softc = (struct da_softc *)periph->softc;
777 cam_periph_lock(periph);
778 secsize = softc->params.secsize; /* XXX: or ap->a_secsize? */
780 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
781 cam_periph_unlock(periph);
782 return (ENXIO);
786 * because length == 0 means we are supposed to flush cache, we only
787 * try to write something if length > 0.
789 if (ap->a_length > 0) {
790 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
791 csio.ccb_h.flags |= CAM_POLLED;
792 csio.ccb_h.ccb_state = DA_CCB_DUMP;
793 scsi_read_write(&csio,
794 /*retries*/1,
795 dadone,
796 MSG_ORDERED_Q_TAG,
797 /*read*/FALSE,
798 /*byte2*/0,
799 /*minimum_cmd_size*/ softc->minimum_cmd_size,
800 ap->a_offset / secsize,
801 ap->a_length / secsize,
802 /*data_ptr*/(u_int8_t *) ap->a_virtual,
803 /*dxfer_len*/ap->a_length,
804 /*sense_len*/SSD_FULL_SIZE,
805 DA_DEFAULT_TIMEOUT * 1000);
806 xpt_polled_action((union ccb *)&csio);
808 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
809 kprintf("Aborting dump due to I/O error.\n");
810 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
811 CAM_SCSI_STATUS_ERROR)
812 scsi_sense_print(&csio);
813 else
814 kprintf("status == 0x%x, scsi status == 0x%x\n",
815 csio.ccb_h.status, csio.scsi_status);
816 return(EIO);
818 cam_periph_unlock(periph);
819 return 0;
823 * Sync the disk cache contents to the physical media.
825 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
827 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
828 csio.ccb_h.ccb_state = DA_CCB_DUMP;
829 scsi_synchronize_cache(&csio,
830 /*retries*/1,
831 /*cbfcnp*/dadone,
832 MSG_SIMPLE_Q_TAG,
833 /*begin_lba*/0,/* Cover the whole disk */
834 /*lb_count*/0,
835 SSD_FULL_SIZE,
836 5 * 60 * 1000);
837 xpt_polled_action((union ccb *)&csio);
839 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
840 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
841 CAM_SCSI_STATUS_ERROR) {
842 int asc, ascq;
843 int sense_key, error_code;
845 scsi_extract_sense(&csio.sense_data,
846 &error_code,
847 &sense_key,
848 &asc, &ascq);
849 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
850 scsi_sense_print(&csio);
851 } else {
852 xpt_print(periph->path, "Synchronize cache "
853 "failed, status == 0x%x, scsi status == "
854 "0x%x\n", csio.ccb_h.status,
855 csio.scsi_status);
859 cam_periph_unlock(periph);
860 return (0);
863 static void
864 dainit(void)
866 cam_status status;
869 * Create our extend array for storing the devices we attach to.
871 daperiphs = cam_extend_new();
872 if (daperiphs == NULL) {
873 kprintf("da: Failed to alloc extend array!\n");
874 return;
877 callout_init(&dasendorderedtag_ch);
880 * Install a global async callback. This callback will
881 * receive async callbacks like "new device found".
883 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
885 if (status != CAM_REQ_CMP) {
886 kprintf("da: Failed to attach master async callback "
887 "due to status 0x%x!\n", status);
888 } else if (da_send_ordered) {
890 /* Register our shutdown event handler */
891 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
892 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
893 kprintf("%s: shutdown event registration failed!\n",
894 __func__);
898 static void
899 daoninvalidate(struct cam_periph *periph)
901 struct da_softc *softc;
903 softc = (struct da_softc *)periph->softc;
906 * De-register any async callbacks.
908 xpt_register_async(0, daasync, periph, periph->path);
910 softc->flags |= DA_FLAG_PACK_INVALID;
913 * Return all queued I/O with ENXIO.
914 * XXX Handle any transactions queued to the card
915 * with XPT_ABORT_CCB.
917 daflushbioq(&softc->bio_queue_trim, ENXIO);
918 daflushbioq(&softc->bio_queue_wr, ENXIO);
919 daflushbioq(&softc->bio_queue_rd, ENXIO);
920 xpt_print(periph->path, "lost device\n");
923 static void
924 daflushbioq(struct bio_queue_head *bioq, int error)
926 struct bio *q_bio;
927 struct buf *q_bp;
929 while ((q_bio = bioq_first(bioq)) != NULL){
930 bioq_remove(bioq, q_bio);
931 q_bp = q_bio->bio_buf;
932 q_bp->b_resid = q_bp->b_bcount;
933 q_bp->b_error = error;
934 q_bp->b_flags |= B_ERROR;
935 biodone(q_bio);
939 static void
940 dacleanup(struct cam_periph *periph)
942 struct da_softc *softc;
944 softc = (struct da_softc *)periph->softc;
946 devstat_remove_entry(&softc->device_stats);
947 cam_extend_release(daperiphs, periph->unit_number);
948 xpt_print(periph->path, "removing device entry\n");
950 * If we can't free the sysctl tree, oh well...
952 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
953 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
954 xpt_print(periph->path, "can't remove sysctl context\n");
956 periph->softc = NULL;
957 if (softc->disk.d_rawdev) {
958 cam_periph_unlock(periph);
959 disk_destroy(&softc->disk);
960 cam_periph_lock(periph);
963 callout_stop(&softc->sendordered_c);
964 kfree(softc, M_DEVBUF);
967 static void
968 daasync(void *callback_arg, u_int32_t code,
969 struct cam_path *path, void *arg)
971 struct cam_periph *periph;
973 periph = (struct cam_periph *)callback_arg;
975 switch (code) {
976 case AC_FOUND_DEVICE:
978 struct ccb_getdev *cgd;
979 cam_status status;
981 cgd = (struct ccb_getdev *)arg;
982 if (cgd == NULL)
983 break;
985 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
986 && SID_TYPE(&cgd->inq_data) != T_RBC
987 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
988 break;
991 * Don't complain if a valid peripheral is already attached.
993 periph = cam_periph_find(cgd->ccb_h.path, "da");
994 if (periph && (periph->flags & CAM_PERIPH_INVALID) == 0)
995 break;
998 * Allocate a peripheral instance for
999 * this device and start the probe
1000 * process.
1002 status = cam_periph_alloc(daregister, daoninvalidate,
1003 dacleanup, dastart,
1004 "da", CAM_PERIPH_BIO,
1005 cgd->ccb_h.path, daasync,
1006 AC_FOUND_DEVICE, cgd);
1008 if (status != CAM_REQ_CMP && status != CAM_REQ_INPROG) {
1009 kprintf("%s: Unable to attach to new device "
1010 "due to status 0x%x\n", __func__, status);
1012 break;
1014 case AC_SENT_BDR:
1015 case AC_BUS_RESET:
1017 struct da_softc *softc;
1018 struct ccb_hdr *ccbh;
1020 softc = (struct da_softc *)periph->softc;
1022 * Don't fail on the expected unit attention
1023 * that will occur.
1025 softc->flags |= DA_FLAG_RETRY_UA;
1026 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1027 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1028 /* FALLTHROUGH*/
1030 default:
1031 cam_periph_async(periph, code, path, arg);
1032 break;
1036 static void
1037 dasysctlinit(void *context, int pending)
1039 struct cam_periph *periph;
1040 struct da_softc *softc;
1041 char tmpstr[80], tmpstr2[80];
1043 get_mplock();
1044 periph = (struct cam_periph *)context;
1045 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1046 rel_mplock();
1047 return;
1050 softc = (struct da_softc *)periph->softc;
1051 ksnprintf(tmpstr, sizeof(tmpstr),
1052 "CAM DA unit %d", periph->unit_number);
1053 ksnprintf(tmpstr2, sizeof(tmpstr2),
1054 "%d", periph->unit_number);
1056 sysctl_ctx_free(&softc->sysctl_ctx);
1057 sysctl_ctx_init(&softc->sysctl_ctx);
1058 softc->flags |= DA_FLAG_SCTX_INIT;
1059 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1060 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1061 CTLFLAG_RD, 0, tmpstr);
1062 if (softc->sysctl_tree == NULL) {
1063 kprintf("%s: unable to allocate sysctl tree\n", __func__);
1064 cam_periph_release(periph);
1065 rel_mplock();
1066 return;
1070 * Now register the sysctl handler, so the user can the value on
1071 * the fly.
1073 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1074 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1075 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1076 "Minimum CDB size");
1078 /* Only create the option if the device supports TRIM */
1079 if (softc->disk.d_info.d_trimflag) {
1080 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1081 SYSCTL_CHILDREN(softc->sysctl_tree),
1082 OID_AUTO,
1083 "trim_enabled",
1084 CTLFLAG_RW,
1085 &softc->trim_enabled,
1087 "Enable TRIM for this device (SSD))");
1090 cam_periph_release(periph);
1091 rel_mplock();
1094 static int
1095 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1097 int error, value;
1099 value = *(int *)arg1;
1101 error = sysctl_handle_int(oidp, &value, 0, req);
1103 if ((error != 0)
1104 || (req->newptr == NULL))
1105 return (error);
1108 * Acceptable values here are 6, 10 or 12, or 16.
1110 if (value < 6)
1111 value = 6;
1112 else if ((value > 6)
1113 && (value <= 10))
1114 value = 10;
1115 else if ((value > 10)
1116 && (value <= 12))
1117 value = 12;
1118 else if (value > 12)
1119 value = 16;
1121 *(int *)arg1 = value;
1123 return (0);
1126 static cam_status
1127 daregister(struct cam_periph *periph, void *arg)
1129 struct da_softc *softc;
1130 struct ccb_pathinq cpi;
1131 struct ccb_getdev *cgd;
1132 char tmpstr[80];
1133 caddr_t match;
1135 cgd = (struct ccb_getdev *)arg;
1136 if (periph == NULL) {
1137 kprintf("%s: periph was NULL!!\n", __func__);
1138 return(CAM_REQ_CMP_ERR);
1141 if (cgd == NULL) {
1142 kprintf("%s: no getdev CCB, can't register device\n",
1143 __func__);
1144 return(CAM_REQ_CMP_ERR);
1147 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
1148 sysctl_ctx_init(&softc->sysctl_ctx);
1149 LIST_INIT(&softc->pending_ccbs);
1150 softc->state = DA_STATE_PROBE;
1151 bioq_init(&softc->bio_queue_trim);
1152 bioq_init(&softc->bio_queue_rd);
1153 bioq_init(&softc->bio_queue_wr);
1154 if (SID_IS_REMOVABLE(&cgd->inq_data))
1155 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1156 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1157 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1159 /* Used to get TRIM status from AHCI driver */
1160 if (cgd->inq_data.vendor_specific1[0] == 1) {
1162 * max number of lba ranges an SSD can handle in a single
1163 * TRIM command. vendor_specific1[1] is the num of 512-byte
1164 * blocks the SSD reports that can be passed in a TRIM cmd.
1166 softc->trim_max_ranges =
1167 min(cgd->inq_data.vendor_specific1[1] * 64, TRIM_MAX_RANGES);
1170 periph->softc = softc;
1172 cam_extend_set(daperiphs, periph->unit_number, periph);
1175 * See if this device has any quirks.
1177 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1178 (caddr_t)da_quirk_table,
1179 NELEM(da_quirk_table),
1180 sizeof(*da_quirk_table), scsi_inquiry_match);
1182 if (match != NULL)
1183 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1184 else
1185 softc->quirks = DA_Q_NONE;
1188 * Unconditionally disable the synchronize cache command for
1189 * usb attachments. It's just impossible to determine if the
1190 * device supports it or not and if it doesn't the port can
1191 * brick.
1193 if (strncmp(periph->sim->sim_name, "umass", 4) == 0) {
1194 softc->quirks |= DA_Q_NO_SYNC_CACHE;
1197 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1199 /* Check if the SIM does not want 6 byte commands */
1200 bzero(&cpi, sizeof(cpi));
1201 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
1202 cpi.ccb_h.func_code = XPT_PATH_INQ;
1203 xpt_action((union ccb *)&cpi);
1204 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1205 softc->quirks |= DA_Q_NO_6_BYTE;
1208 * RBC devices don't have to support READ(6), only READ(10).
1210 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1211 softc->minimum_cmd_size = 10;
1212 else
1213 softc->minimum_cmd_size = 6;
1216 * Load the user's default, if any.
1218 ksnprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1219 periph->unit_number);
1220 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1223 * 6, 10, 12, and 16 are the currently permissible values.
1225 if (softc->minimum_cmd_size < 6)
1226 softc->minimum_cmd_size = 6;
1227 else if ((softc->minimum_cmd_size > 6)
1228 && (softc->minimum_cmd_size <= 10))
1229 softc->minimum_cmd_size = 10;
1230 else if ((softc->minimum_cmd_size > 10)
1231 && (softc->minimum_cmd_size <= 12))
1232 softc->minimum_cmd_size = 12;
1233 else if (softc->minimum_cmd_size > 12)
1234 softc->minimum_cmd_size = 16;
1237 * The DA driver supports a blocksize, but
1238 * we don't know the blocksize until we do
1239 * a read capacity. So, set a flag to
1240 * indicate that the blocksize is
1241 * unavailable right now. We'll clear the
1242 * flag as soon as we've done a read capacity.
1244 devstat_add_entry(&softc->device_stats, "da",
1245 periph->unit_number, 0,
1246 DEVSTAT_BS_UNAVAILABLE,
1247 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
1248 DEVSTAT_PRIORITY_DISK);
1251 * Register this media as a disk
1253 CAM_SIM_UNLOCK(periph->sim);
1254 disk_create(periph->unit_number, &softc->disk, &da_ops);
1255 if (cpi.maxio == 0 || cpi.maxio > MAXPHYS)
1256 softc->disk.d_rawdev->si_iosize_max = MAXPHYS;
1257 else
1258 softc->disk.d_rawdev->si_iosize_max = cpi.maxio;
1259 if (bootverbose) {
1260 kprintf("%s%d: si_iosize_max:%d\n",
1261 periph->periph_name,
1262 periph->unit_number,
1263 softc->disk.d_rawdev->si_iosize_max);
1265 CAM_SIM_LOCK(periph->sim);
1268 * Add async callbacks for bus reset and
1269 * bus device reset calls. I don't bother
1270 * checking if this fails as, in most cases,
1271 * the system will function just fine without
1272 * them and the only alternative would be to
1273 * not attach the device on failure.
1275 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
1276 daasync, periph, periph->path);
1279 * Take an exclusive refcount on the periph while dastart is called
1280 * to finish the probe. The reference will be dropped in dadone at
1281 * the end of probe.
1283 cam_periph_hold(periph, 0);
1284 xpt_schedule(periph, /*priority*/5);
1287 * Schedule a periodic event to occasionally send an
1288 * ordered tag to a device.
1290 callout_init(&softc->sendordered_c);
1291 callout_reset(&softc->sendordered_c,
1292 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
1293 dasendorderedtag, softc);
1297 return(CAM_REQ_CMP);
1300 static void
1301 dastart(struct cam_periph *periph, union ccb *start_ccb)
1303 struct da_softc *softc;
1305 softc = (struct da_softc *)periph->softc;
1307 switch (softc->state) {
1308 case DA_STATE_NORMAL:
1310 /* Pull a buffer from the queue and get going on it */
1311 struct bio *bio;
1312 struct bio *bio_rd;
1313 struct bio *bio_wr;
1314 struct buf *bp;
1315 u_int8_t tag_code;
1316 int limit;
1319 * See if there is a buf with work for us to do..
1321 bio_rd = bioq_first(&softc->bio_queue_rd);
1322 bio_wr = bioq_first(&softc->bio_queue_wr);
1324 if (periph->immediate_priority <= periph->pinfo.priority) {
1325 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1326 ("queuing for immediate ccb\n"));
1327 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1328 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1329 periph_links.sle);
1330 periph->immediate_priority = CAM_PRIORITY_NONE;
1331 wakeup(&periph->ccb_list);
1332 if (bio_rd || bio_wr) {
1334 * Have more work to do, so ensure we stay
1335 * scheduled
1337 xpt_schedule(periph, /* XXX priority */1);
1339 break;
1342 /* Run the trim command if not already running */
1343 if (!softc->trim_running &&
1344 (bio = bioq_first(&softc->bio_queue_trim)) != NULL) {
1345 struct trim_request *req = &softc->trim_req;
1346 struct bio *bio1;
1347 int bps = 0, ranges = 0;
1349 softc->trim_running = 1;
1350 bzero(req, sizeof(*req));
1351 bio1 = bio;
1352 while (1) {
1353 uint64_t lba;
1354 int count;
1356 bp = bio1->bio_buf;
1357 count = bp->b_bcount / softc->params.secsize;
1358 lba = bio1->bio_offset/softc->params.secsize;
1360 kprintf("trim lba:%llu boff:%llu count:%d\n",
1361 (unsigned long long) lba,
1362 (unsigned long long) bio1->bio_offset,
1363 count);
1365 bioq_remove(&softc->bio_queue_trim, bio1);
1366 while (count > 0) {
1367 int c = min(count, 0xffff);
1368 int off = ranges * 8;
1370 req->data[off + 0] = lba & 0xff;
1371 req->data[off + 1] = (lba >> 8) & 0xff;
1372 req->data[off + 2] = (lba >> 16) & 0xff;
1373 req->data[off + 3] = (lba >> 24) & 0xff;
1374 req->data[off + 4] = (lba >> 32) & 0xff;
1375 req->data[off + 5] = (lba >> 40) & 0xff;
1376 req->data[off + 6] = c & 0xff;
1377 req->data[off + 7] = (c >> 8) & 0xff;
1378 lba += c;
1379 count -= c;
1380 ranges++;
1383 /* Try to merge multiple TRIM requests */
1384 req->bios[bps++] = bio1;
1385 bio1 = bioq_first(&softc->bio_queue_trim);
1386 if (bio1 == NULL ||
1387 bio1->bio_buf->b_bcount / softc->params.secsize >
1388 (softc->trim_max_ranges - ranges) * 0xffff)
1389 break;
1393 cam_fill_csio(&start_ccb->csio,
1394 1/*retries*/,
1395 dadone,
1396 CAM_DIR_OUT,
1397 MSG_SIMPLE_Q_TAG,
1398 req->data,
1399 ((ranges +63)/64)*512,
1400 SSD_FULL_SIZE,
1401 sizeof(struct scsi_rw_6),
1402 da_default_timeout*2);
1404 start_ccb->ccb_h.ccb_state = DA_CCB_TRIM;
1405 LIST_INSERT_HEAD(&softc->pending_ccbs,
1406 &start_ccb->ccb_h, periph_links.le);
1407 start_ccb->csio.ccb_h.func_code = XPT_TRIM;
1408 start_ccb->ccb_h.ccb_bio = bio;
1409 devstat_start_transaction(&softc->device_stats);
1410 xpt_action(start_ccb);
1411 xpt_schedule(periph, 1);
1412 break;
1416 * Select a read or write buffer to queue. Limit the number
1417 * of tags dedicated to reading or writing, giving reads
1418 * precedence.
1420 * Writes to modern hard drives go into the HDs cache and
1421 * return completion nearly instantly. That is until the
1422 * cache becomes full. When the HDs cache becomes full
1423 * write commands will begin to stall. If all available
1424 * tags are taken up by writes which saturate the drive
1425 * reads will become tag-starved.
1427 * A similar situation can occur with reads. With many
1428 * parallel readers all tags can be taken up by reads
1429 * and prevent any writes from draining, even if the HD's
1430 * cache is not full.
1432 limit = periph->sim->max_tagged_dev_openings * 2 / 3 + 1;
1433 #if 0
1434 /* DEBUGGING */
1435 static int savets;
1436 static long savets2;
1437 if (1 || time_uptime != savets2 || (ticks != savets && (softc->outstanding_cmds_rd || softc->outstanding_cmds_wr))) {
1438 kprintf("%d %d (%d)\n",
1439 softc->outstanding_cmds_rd,
1440 softc->outstanding_cmds_wr,
1441 limit);
1442 savets = ticks;
1443 savets2 = time_uptime;
1445 #endif
1446 if (bio_rd && softc->outstanding_cmds_rd < limit) {
1447 bio = bio_rd;
1448 bioq_remove(&softc->bio_queue_rd, bio);
1449 } else if (bio_wr && softc->outstanding_cmds_wr < limit) {
1450 bio = bio_wr;
1451 bioq_remove(&softc->bio_queue_wr, bio);
1452 } else {
1453 if (bio_rd)
1454 softc->flags |= DA_FLAG_RD_LIMIT;
1455 if (bio_wr)
1456 softc->flags |= DA_FLAG_WR_LIMIT;
1457 xpt_release_ccb(start_ccb);
1458 break;
1462 * We can queue new work.
1464 bp = bio->bio_buf;
1466 devstat_start_transaction(&softc->device_stats);
1468 if ((bp->b_flags & B_ORDERED) != 0 ||
1469 (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1470 softc->flags &= ~DA_FLAG_NEED_OTAG;
1471 softc->ordered_tag_count++;
1472 tag_code = MSG_ORDERED_Q_TAG;
1473 } else {
1474 tag_code = MSG_SIMPLE_Q_TAG;
1477 switch(bp->b_cmd) {
1478 case BUF_CMD_READ:
1479 case BUF_CMD_WRITE:
1481 * Block read/write op
1483 KKASSERT(bio->bio_offset % softc->params.secsize == 0);
1485 scsi_read_write(
1486 &start_ccb->csio,
1487 da_retry_count, /* retries */
1488 dadone,
1489 tag_code,
1490 (bp->b_cmd == BUF_CMD_READ),
1491 0, /* byte2 */
1492 softc->minimum_cmd_size,
1493 bio->bio_offset / softc->params.secsize,
1494 bp->b_bcount / softc->params.secsize,
1495 bp->b_data,
1496 bp->b_bcount,
1497 SSD_FULL_SIZE, /* sense_len */
1498 da_default_timeout * 1000
1500 break;
1501 case BUF_CMD_FLUSH:
1503 * Silently complete a flush request if the device
1504 * cannot handle it.
1506 if (softc->quirks & DA_Q_NO_SYNC_CACHE) {
1507 xpt_release_ccb(start_ccb);
1508 start_ccb = NULL;
1509 devstat_end_transaction_buf(
1510 &softc->device_stats, bp);
1511 biodone(bio);
1512 } else {
1513 scsi_synchronize_cache(
1514 &start_ccb->csio,
1515 1, /* retries */
1516 dadone, /* cbfcnp */
1517 MSG_SIMPLE_Q_TAG,
1518 0, /* lba */
1519 0, /* count (whole disk) */
1520 SSD_FULL_SIZE,
1521 da_default_timeout*1000 /* timeout */
1524 break;
1525 case BUF_CMD_FREEBLKS:
1526 if (softc->disk.d_info.d_trimflag & DA_FLAG_CAN_TRIM){
1527 start_ccb->csio.ccb_h.func_code = XPT_TRIM;
1528 break;
1530 default:
1531 xpt_release_ccb(start_ccb);
1532 start_ccb = NULL;
1533 panic("dastart: unrecognized bio cmd %d", bp->b_cmd);
1534 break; /* NOT REACHED */
1538 * Block out any asyncronous callbacks
1539 * while we touch the pending ccb list.
1541 if (start_ccb) {
1542 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1543 LIST_INSERT_HEAD(&softc->pending_ccbs,
1544 &start_ccb->ccb_h, periph_links.le);
1545 if (bp->b_cmd == BUF_CMD_WRITE ||
1546 bp->b_cmd == BUF_CMD_FLUSH) {
1547 ++softc->outstanding_cmds_wr;
1548 } else {
1549 ++softc->outstanding_cmds_rd;
1552 /* We expect a unit attention from this device */
1553 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1554 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1555 softc->flags &= ~DA_FLAG_RETRY_UA;
1558 start_ccb->ccb_h.ccb_bio = bio;
1559 xpt_action(start_ccb);
1563 * Be sure we stay scheduled if we have more work to do.
1565 if (bioq_first(&softc->bio_queue_rd) ||
1566 bioq_first(&softc->bio_queue_wr)) {
1567 xpt_schedule(periph, 1);
1569 break;
1571 case DA_STATE_PROBE:
1573 struct ccb_scsiio *csio;
1574 struct scsi_read_capacity_data *rcap;
1576 rcap = kmalloc(sizeof(*rcap), M_SCSIDA, M_INTWAIT | M_ZERO);
1577 csio = &start_ccb->csio;
1578 scsi_read_capacity(csio,
1579 /*retries*/4,
1580 dadone,
1581 MSG_SIMPLE_Q_TAG,
1582 rcap,
1583 SSD_FULL_SIZE,
1584 /*timeout*/5000);
1585 start_ccb->ccb_h.ccb_bio = NULL;
1586 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1587 xpt_action(start_ccb);
1588 break;
1590 case DA_STATE_PROBE2:
1592 struct ccb_scsiio *csio;
1593 struct scsi_read_capacity_data_16 *rcaplong;
1595 rcaplong = kmalloc(sizeof(*rcaplong), M_SCSIDA,
1596 M_INTWAIT | M_ZERO);
1597 csio = &start_ccb->csio;
1598 scsi_read_capacity_16(csio,
1599 /*retries*/ 4,
1600 /*cbfcnp*/ dadone,
1601 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1602 /*lba*/ 0,
1603 /*reladr*/ 0,
1604 /*pmi*/ 0,
1605 rcaplong,
1606 /*sense_len*/ SSD_FULL_SIZE,
1607 /*timeout*/ 60000);
1608 start_ccb->ccb_h.ccb_bio = NULL;
1609 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1610 xpt_action(start_ccb);
1611 break;
1616 static int
1617 cmd6workaround(union ccb *ccb)
1619 struct scsi_rw_6 cmd6;
1620 struct scsi_rw_10 *cmd10;
1621 struct da_softc *softc;
1622 u_int8_t *cdb;
1623 int frozen;
1625 cdb = ccb->csio.cdb_io.cdb_bytes;
1627 /* Translation only possible if CDB is an array and cmd is R/W6 */
1628 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1629 (*cdb != READ_6 && *cdb != WRITE_6))
1630 return 0;
1632 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
1633 "increasing minimum_cmd_size to 10.\n");
1634 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1635 softc->minimum_cmd_size = 10;
1637 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1638 cmd10 = (struct scsi_rw_10 *)cdb;
1639 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1640 cmd10->byte2 = 0;
1641 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1642 cmd10->reserved = 0;
1643 scsi_ulto2b(cmd6.length, cmd10->length);
1644 cmd10->control = cmd6.control;
1645 ccb->csio.cdb_len = sizeof(*cmd10);
1647 /* Requeue request, unfreezing queue if necessary */
1648 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1649 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1650 xpt_action(ccb);
1651 if (frozen) {
1652 cam_release_devq(ccb->ccb_h.path,
1653 /*relsim_flags*/0,
1654 /*reduction*/0,
1655 /*timeout*/0,
1656 /*getcount_only*/0);
1658 return (ERESTART);
1661 static void
1662 dadone(struct cam_periph *periph, union ccb *done_ccb)
1664 struct da_softc *softc;
1665 struct ccb_scsiio *csio;
1666 struct disk_info info;
1668 softc = (struct da_softc *)periph->softc;
1669 csio = &done_ccb->csio;
1670 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1671 case DA_CCB_BUFFER_IO:
1672 case DA_CCB_TRIM:
1674 struct buf *bp;
1675 struct bio *bio;
1676 int mustsched = 0;
1678 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1679 bp = bio->bio_buf;
1680 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1681 int error;
1682 int sf;
1684 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1685 sf = SF_RETRY_UA;
1686 else
1687 sf = 0;
1689 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1690 if (error == ERESTART) {
1692 * A retry was scheuled, so
1693 * just return.
1695 return;
1697 if (error != 0) {
1698 if (error == ENXIO) {
1700 * Catastrophic error. Mark our pack as
1701 * invalid.
1704 * XXX See if this is really a media
1705 * XXX change first?
1707 xpt_print(periph->path,
1708 "Invalidating pack\n");
1709 softc->flags |= DA_FLAG_PACK_INVALID;
1713 * Return all queued write I/O's with EIO
1714 * so the client can retry these I/Os in the
1715 * proper order should it attempt to recover.
1717 * Leave read I/O's alone.
1719 daflushbioq(&softc->bio_queue_wr, EIO);
1720 bp->b_error = error;
1721 bp->b_resid = bp->b_bcount;
1722 bp->b_flags |= B_ERROR;
1723 } else {
1724 bp->b_resid = csio->resid;
1725 bp->b_error = 0;
1726 if (bp->b_resid != 0)
1727 bp->b_flags |= B_ERROR;
1729 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1730 cam_release_devq(done_ccb->ccb_h.path,
1731 /*relsim_flags*/0,
1732 /*reduction*/0,
1733 /*timeout*/0,
1734 /*getcount_only*/0);
1735 } else {
1736 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1737 panic("REQ_CMP with QFRZN");
1738 bp->b_resid = csio->resid;
1739 if (csio->resid > 0)
1740 bp->b_flags |= B_ERROR;
1744 * Block out any asyncronous callbacks
1745 * while we touch the pending ccb list.
1747 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1748 if (bp->b_cmd == BUF_CMD_WRITE || bp->b_cmd == BUF_CMD_FLUSH) {
1749 --softc->outstanding_cmds_wr;
1750 if (softc->flags & DA_FLAG_WR_LIMIT) {
1751 softc->flags &= ~DA_FLAG_WR_LIMIT;
1752 mustsched = 1;
1754 } else {
1755 --softc->outstanding_cmds_rd;
1756 if (softc->flags & DA_FLAG_RD_LIMIT) {
1757 softc->flags &= ~DA_FLAG_RD_LIMIT;
1758 mustsched = 1;
1761 if (softc->outstanding_cmds_rd +
1762 softc->outstanding_cmds_wr == 0) {
1763 softc->flags |= DA_FLAG_WENT_IDLE;
1766 devstat_end_transaction_buf(&softc->device_stats, bp);
1767 if ((csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) ==
1768 DA_CCB_TRIM) {
1769 struct trim_request *req =
1770 (struct trim_request *) csio->data_ptr;
1771 int i;
1773 for (i = 1; i < softc->trim_max_ranges &&
1774 req->bios[i]; i++) {
1775 struct bio *bp1 = req->bios[i];
1777 bp1->bio_buf->b_resid = bp->b_resid;
1778 bp1->bio_buf->b_error = bp->b_error;
1779 if (bp->b_flags & B_ERROR)
1780 bp1->bio_buf->b_flags |= B_ERROR;
1781 biodone(bp1);
1783 softc->trim_running = 0;
1784 biodone(bio);
1785 xpt_schedule(periph,1);
1786 } else
1787 biodone(bio);
1790 if (mustsched)
1791 xpt_schedule(periph, /*priority*/1);
1793 break;
1795 case DA_CCB_PROBE:
1796 case DA_CCB_PROBE2:
1798 struct scsi_read_capacity_data *rdcap;
1799 struct scsi_read_capacity_data_16 *rcaplong;
1800 char announce_buf[80];
1802 rdcap = NULL;
1803 rcaplong = NULL;
1804 if (softc->state == DA_STATE_PROBE)
1805 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1806 else
1807 rcaplong = (struct scsi_read_capacity_data_16 *)
1808 csio->data_ptr;
1810 bzero(&info, sizeof(info));
1811 info.d_type = DTYPE_SCSI;
1812 info.d_serialno = xpt_path_serialno(periph->path);
1814 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1815 struct disk_params *dp;
1816 uint32_t block_size;
1817 uint64_t maxsector;
1819 if (softc->state == DA_STATE_PROBE) {
1820 block_size = scsi_4btoul(rdcap->length);
1821 maxsector = scsi_4btoul(rdcap->addr);
1824 * According to SBC-2, if the standard 10
1825 * byte READ CAPACITY command returns 2^32,
1826 * we should issue the 16 byte version of
1827 * the command, since the device in question
1828 * has more sectors than can be represented
1829 * with the short version of the command.
1831 if (maxsector == 0xffffffff) {
1832 softc->state = DA_STATE_PROBE2;
1833 kfree(rdcap, M_SCSIDA);
1834 xpt_release_ccb(done_ccb);
1835 xpt_schedule(periph, /*priority*/5);
1836 return;
1838 } else {
1839 block_size = scsi_4btoul(rcaplong->length);
1840 maxsector = scsi_8btou64(rcaplong->addr);
1842 dasetgeom(periph, block_size, maxsector);
1843 dp = &softc->params;
1844 ksnprintf(announce_buf, sizeof(announce_buf),
1845 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1846 (uintmax_t) (((uintmax_t)dp->secsize *
1847 dp->sectors) / (1024*1024)),
1848 (uintmax_t)dp->sectors,
1849 dp->secsize, dp->heads, dp->secs_per_track,
1850 dp->cylinders);
1852 CAM_SIM_UNLOCK(periph->sim);
1853 info.d_media_blksize = softc->params.secsize;
1854 info.d_media_blocks = softc->params.sectors;
1855 info.d_media_size = 0;
1856 info.d_secpertrack = softc->params.secs_per_track;
1857 info.d_nheads = softc->params.heads;
1858 info.d_ncylinders = softc->params.cylinders;
1859 info.d_secpercyl = softc->params.heads *
1860 softc->params.secs_per_track;
1861 info.d_serialno = xpt_path_serialno(periph->path);
1862 disk_setdiskinfo(&softc->disk, &info);
1863 CAM_SIM_LOCK(periph->sim);
1864 } else {
1865 int error;
1867 announce_buf[0] = '\0';
1870 * Retry any UNIT ATTENTION type errors. They
1871 * are expected at boot.
1873 error = daerror(done_ccb, CAM_RETRY_SELTO,
1874 SF_RETRY_UA|SF_NO_PRINT);
1875 if (error == ERESTART) {
1877 * A retry was scheuled, so
1878 * just return.
1880 return;
1881 } else if (error != 0) {
1882 struct scsi_sense_data *sense;
1883 int asc, ascq;
1884 int sense_key, error_code;
1885 int have_sense;
1886 cam_status status;
1887 struct ccb_getdev cgd;
1889 /* Don't wedge this device's queue */
1890 status = done_ccb->ccb_h.status;
1891 if ((status & CAM_DEV_QFRZN) != 0)
1892 cam_release_devq(done_ccb->ccb_h.path,
1893 /*relsim_flags*/0,
1894 /*reduction*/0,
1895 /*timeout*/0,
1896 /*getcount_only*/0);
1899 xpt_setup_ccb(&cgd.ccb_h,
1900 done_ccb->ccb_h.path,
1901 /* priority */ 1);
1902 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1903 xpt_action((union ccb *)&cgd);
1905 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1906 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1907 || ((status & CAM_AUTOSNS_VALID) == 0))
1908 have_sense = FALSE;
1909 else
1910 have_sense = TRUE;
1912 if (have_sense) {
1913 sense = &csio->sense_data;
1914 scsi_extract_sense(sense, &error_code,
1915 &sense_key,
1916 &asc, &ascq);
1919 * Attach to anything that claims to be a
1920 * direct access or optical disk device,
1921 * as long as it doesn't return a "Logical
1922 * unit not supported" (0x25) error.
1924 if ((have_sense) && (asc != 0x25)
1925 && (error_code == SSD_CURRENT_ERROR)) {
1926 const char *sense_key_desc;
1927 const char *asc_desc;
1929 scsi_sense_desc(sense_key, asc, ascq,
1930 &cgd.inq_data,
1931 &sense_key_desc,
1932 &asc_desc);
1933 ksnprintf(announce_buf,
1934 sizeof(announce_buf),
1935 "Attempt to query device "
1936 "size failed: %s, %s",
1937 sense_key_desc,
1938 asc_desc);
1939 info.d_media_blksize = 512;
1940 disk_setdiskinfo(&softc->disk, &info);
1941 } else {
1942 if (have_sense)
1943 scsi_sense_print(
1944 &done_ccb->csio);
1945 else {
1946 xpt_print(periph->path,
1947 "got CAM status %#x\n",
1948 done_ccb->ccb_h.status);
1951 xpt_print(periph->path, "fatal error, "
1952 "failed to attach to device\n");
1955 * Free up resources.
1957 cam_periph_invalidate(periph);
1961 kfree(csio->data_ptr, M_SCSIDA);
1962 if (announce_buf[0] != '\0') {
1963 xpt_announce_periph(periph, announce_buf);
1965 * Create our sysctl variables, now that we know
1966 * we have successfully attached.
1968 taskqueue_enqueue(taskqueue_thread[mycpuid],
1969 &softc->sysctl_task);
1972 if (softc->trim_max_ranges) {
1973 softc->disk.d_info.d_trimflag |= DA_FLAG_CAN_TRIM;
1974 kprintf("%s%d: supports TRIM\n",
1975 periph->periph_name,
1976 periph->unit_number);
1978 softc->state = DA_STATE_NORMAL;
1980 * Since our peripheral may be invalidated by an error
1981 * above or an external event, we must release our CCB
1982 * before releasing the probe lock on the peripheral.
1983 * The peripheral will only go away once the last lock
1984 * is removed, and we need it around for the CCB release
1985 * operation.
1987 xpt_release_ccb(done_ccb);
1988 cam_periph_unhold(periph, 0);
1989 return;
1991 case DA_CCB_WAITING:
1993 /* Caller will release the CCB */
1994 wakeup(&done_ccb->ccb_h.cbfcnp);
1995 return;
1997 case DA_CCB_DUMP:
1998 /* No-op. We're polling */
1999 return;
2000 default:
2001 break;
2003 xpt_release_ccb(done_ccb);
2006 static int
2007 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
2009 struct da_softc *softc;
2010 struct cam_periph *periph;
2011 int error;
2013 periph = xpt_path_periph(ccb->ccb_h.path);
2014 softc = (struct da_softc *)periph->softc;
2017 * Automatically detect devices that do not support
2018 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
2020 error = 0;
2021 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
2022 error = cmd6workaround(ccb);
2023 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
2024 CAM_SCSI_STATUS_ERROR)
2025 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
2026 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
2027 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
2028 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
2029 int sense_key, error_code, asc, ascq;
2031 scsi_extract_sense(&ccb->csio.sense_data,
2032 &error_code, &sense_key, &asc, &ascq);
2033 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
2034 error = cmd6workaround(ccb);
2036 if (error == ERESTART)
2037 return (ERESTART);
2040 * XXX
2041 * Until we have a better way of doing pack validation,
2042 * don't treat UAs as errors.
2044 sense_flags |= SF_RETRY_UA;
2045 return(cam_periph_error(ccb, cam_flags, sense_flags,
2046 &softc->saved_ccb));
2049 static void
2050 daprevent(struct cam_periph *periph, int action)
2052 struct da_softc *softc;
2053 union ccb *ccb;
2054 int error;
2056 softc = (struct da_softc *)periph->softc;
2058 if (((action == PR_ALLOW)
2059 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
2060 || ((action == PR_PREVENT)
2061 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
2062 return;
2065 ccb = cam_periph_getccb(periph, /*priority*/1);
2067 scsi_prevent(&ccb->csio,
2068 /*retries*/1,
2069 /*cbcfp*/dadone,
2070 MSG_SIMPLE_Q_TAG,
2071 action,
2072 SSD_FULL_SIZE,
2073 5000);
2075 error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
2076 SF_RETRY_UA, &softc->device_stats);
2078 if (error == 0) {
2079 if (action == PR_ALLOW)
2080 softc->flags &= ~DA_FLAG_PACK_LOCKED;
2081 else
2082 softc->flags |= DA_FLAG_PACK_LOCKED;
2085 xpt_release_ccb(ccb);
2089 * Check media on open, e.g. card reader devices which had no initial media.
2091 static int
2092 dacheckmedia(struct cam_periph *periph)
2094 struct disk_params *dp;
2095 struct da_softc *softc;
2096 struct disk_info info;
2097 int error;
2099 softc = (struct da_softc *)periph->softc;
2100 dp = &softc->params;
2102 error = dagetcapacity(periph);
2105 * Only reprobe on initial open and if the media is removable.
2107 * NOTE: If we setdiskinfo() it will take the device probe
2108 * a bit of time to probe the slices and partitions,
2109 * and mess up booting. So avoid if nothing has changed.
2110 * XXX
2112 if (softc->flags & DA_FLAG_OPEN)
2113 return (error);
2114 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) == 0)
2115 return (error);
2117 bzero(&info, sizeof(info));
2118 info.d_type = DTYPE_SCSI;
2119 info.d_serialno = xpt_path_serialno(periph->path);
2121 if (error == 0) {
2122 CAM_SIM_UNLOCK(periph->sim);
2123 info.d_media_blksize = softc->params.secsize;
2124 info.d_media_blocks = softc->params.sectors;
2125 info.d_media_size = 0;
2126 info.d_secpertrack = softc->params.secs_per_track;
2127 info.d_nheads = softc->params.heads;
2128 info.d_ncylinders = softc->params.cylinders;
2129 info.d_secpercyl = softc->params.heads *
2130 softc->params.secs_per_track;
2131 info.d_serialno = xpt_path_serialno(periph->path);
2132 if (info.d_media_blocks != softc->disk.d_info.d_media_blocks) {
2133 kprintf("%s%d: open removable media: "
2134 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)\n",
2135 periph->periph_name, periph->unit_number,
2136 (uintmax_t)(((uintmax_t)dp->secsize *
2137 dp->sectors) / (1024*1024)),
2138 (uintmax_t)dp->sectors, dp->secsize,
2139 dp->heads, dp->secs_per_track, dp->cylinders);
2140 disk_setdiskinfo(&softc->disk, &info);
2142 CAM_SIM_LOCK(periph->sim);
2143 } else {
2144 kprintf("%s%d: open removable media: no media present\n",
2145 periph->periph_name, periph->unit_number);
2146 info.d_media_blksize = 512;
2147 disk_setdiskinfo(&softc->disk, &info);
2149 return (error);
2152 static int
2153 dagetcapacity(struct cam_periph *periph)
2155 struct da_softc *softc;
2156 union ccb *ccb;
2157 struct scsi_read_capacity_data *rcap;
2158 struct scsi_read_capacity_data_16 *rcaplong;
2159 uint32_t block_len;
2160 uint64_t maxsector;
2161 int error;
2163 softc = (struct da_softc *)periph->softc;
2164 block_len = 0;
2165 maxsector = 0;
2166 error = 0;
2168 /* Do a read capacity */
2169 rcap = (struct scsi_read_capacity_data *)kmalloc(sizeof(*rcaplong),
2170 M_SCSIDA, M_INTWAIT);
2172 ccb = cam_periph_getccb(periph, /*priority*/1);
2173 scsi_read_capacity(&ccb->csio,
2174 /*retries*/4,
2175 /*cbfncp*/dadone,
2176 MSG_SIMPLE_Q_TAG,
2177 rcap,
2178 SSD_FULL_SIZE,
2179 /*timeout*/60000);
2180 ccb->ccb_h.ccb_bio = NULL;
2182 error = cam_periph_runccb(ccb, daerror,
2183 /*cam_flags*/CAM_RETRY_SELTO,
2184 /*sense_flags*/SF_RETRY_UA,
2185 &softc->device_stats);
2187 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2188 cam_release_devq(ccb->ccb_h.path,
2189 /*relsim_flags*/0,
2190 /*reduction*/0,
2191 /*timeout*/0,
2192 /*getcount_only*/0);
2194 if (error == 0) {
2195 block_len = scsi_4btoul(rcap->length);
2196 maxsector = scsi_4btoul(rcap->addr);
2198 if (maxsector != 0xffffffff)
2199 goto done;
2200 } else
2201 goto done;
2203 rcaplong = (struct scsi_read_capacity_data_16 *)rcap;
2205 scsi_read_capacity_16(&ccb->csio,
2206 /*retries*/ 4,
2207 /*cbfcnp*/ dadone,
2208 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2209 /*lba*/ 0,
2210 /*reladr*/ 0,
2211 /*pmi*/ 0,
2212 rcaplong,
2213 /*sense_len*/ SSD_FULL_SIZE,
2214 /*timeout*/ 60000);
2215 ccb->ccb_h.ccb_bio = NULL;
2217 error = cam_periph_runccb(ccb, daerror,
2218 /*cam_flags*/CAM_RETRY_SELTO,
2219 /*sense_flags*/SF_RETRY_UA,
2220 &softc->device_stats);
2222 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2223 cam_release_devq(ccb->ccb_h.path,
2224 /*relsim_flags*/0,
2225 /*reduction*/0,
2226 /*timeout*/0,
2227 /*getcount_only*/0);
2229 if (error == 0) {
2230 block_len = scsi_4btoul(rcaplong->length);
2231 maxsector = scsi_8btou64(rcaplong->addr);
2234 done:
2236 if (error == 0)
2237 dasetgeom(periph, block_len, maxsector);
2239 xpt_release_ccb(ccb);
2241 kfree(rcap, M_SCSIDA);
2243 return (error);
2246 static void
2247 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector)
2249 struct ccb_calc_geometry ccg;
2250 struct da_softc *softc;
2251 struct disk_params *dp;
2253 softc = (struct da_softc *)periph->softc;
2255 dp = &softc->params;
2256 dp->secsize = block_len;
2257 dp->sectors = maxsector + 1;
2259 * Have the controller provide us with a geometry
2260 * for this disk. The only time the geometry
2261 * matters is when we boot and the controller
2262 * is the only one knowledgeable enough to come
2263 * up with something that will make this a bootable
2264 * device.
2266 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
2267 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
2268 ccg.block_size = dp->secsize;
2269 ccg.volume_size = dp->sectors;
2270 ccg.heads = 0;
2271 ccg.secs_per_track = 0;
2272 ccg.cylinders = 0;
2273 xpt_action((union ccb*)&ccg);
2274 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2276 * We don't know what went wrong here- but just pick
2277 * a geometry so we don't have nasty things like divide
2278 * by zero.
2280 dp->heads = 255;
2281 dp->secs_per_track = 255;
2282 dp->cylinders = dp->sectors / (255 * 255);
2283 if (dp->cylinders == 0) {
2284 dp->cylinders = 1;
2286 } else {
2287 dp->heads = ccg.heads;
2288 dp->secs_per_track = ccg.secs_per_track;
2289 dp->cylinders = ccg.cylinders;
2293 static void
2294 dasendorderedtag(void *arg)
2296 struct da_softc *softc = arg;
2298 if (da_send_ordered) {
2299 if ((softc->ordered_tag_count == 0)
2300 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
2301 softc->flags |= DA_FLAG_NEED_OTAG;
2303 if (softc->outstanding_cmds_rd || softc->outstanding_cmds_wr)
2304 softc->flags &= ~DA_FLAG_WENT_IDLE;
2306 softc->ordered_tag_count = 0;
2308 /* Queue us up again */
2309 callout_reset(&softc->sendordered_c,
2310 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
2311 dasendorderedtag, softc);
2315 * Step through all DA peripheral drivers, and if the device is still open,
2316 * sync the disk cache to physical media.
2318 static void
2319 dashutdown(void * arg, int howto)
2321 struct cam_periph *periph;
2322 struct da_softc *softc;
2324 TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
2325 union ccb ccb;
2327 cam_periph_lock(periph);
2328 softc = (struct da_softc *)periph->softc;
2331 * We only sync the cache if the drive is still open, and
2332 * if the drive is capable of it..
2334 if (((softc->flags & DA_FLAG_OPEN) == 0)
2335 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
2336 cam_periph_unlock(periph);
2337 continue;
2340 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
2342 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
2343 scsi_synchronize_cache(&ccb.csio,
2344 /*retries*/1,
2345 /*cbfcnp*/dadone,
2346 MSG_SIMPLE_Q_TAG,
2347 /*begin_lba*/0, /* whole disk */
2348 /*lb_count*/0,
2349 SSD_FULL_SIZE,
2350 60 * 60 * 1000);
2352 xpt_polled_action(&ccb);
2354 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2355 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2356 CAM_SCSI_STATUS_ERROR)
2357 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
2358 int error_code, sense_key, asc, ascq;
2360 scsi_extract_sense(&ccb.csio.sense_data,
2361 &error_code, &sense_key,
2362 &asc, &ascq);
2364 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
2365 scsi_sense_print(&ccb.csio);
2366 } else {
2367 xpt_print(periph->path, "Synchronize "
2368 "cache failed, status == 0x%x, scsi status "
2369 "== 0x%x\n", ccb.ccb_h.status,
2370 ccb.csio.scsi_status);
2374 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
2375 cam_release_devq(ccb.ccb_h.path,
2376 /*relsim_flags*/0,
2377 /*reduction*/0,
2378 /*timeout*/0,
2379 /*getcount_only*/0);
2381 cam_periph_unlock(periph);
2385 #else /* !_KERNEL */
2388 * XXX This is only left out of the kernel build to silence warnings. If,
2389 * for some reason this function is used in the kernel, the ifdefs should
2390 * be moved so it is included both in the kernel and userland.
2392 void
2393 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2394 void (*cbfcnp)(struct cam_periph *, union ccb *),
2395 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2396 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2397 u_int32_t timeout)
2399 struct scsi_format_unit *scsi_cmd;
2401 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2402 scsi_cmd->opcode = FORMAT_UNIT;
2403 scsi_cmd->byte2 = byte2;
2404 scsi_ulto2b(ileave, scsi_cmd->interleave);
2406 cam_fill_csio(csio,
2407 retries,
2408 cbfcnp,
2409 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2410 tag_action,
2411 data_ptr,
2412 dxfer_len,
2413 sense_len,
2414 sizeof(*scsi_cmd),
2415 timeout);
2418 #endif /* _KERNEL */