2 * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
39 #include <sys/taskqueue.h>
41 #include <sys/mutex.h>
43 #include <sys/devicestat.h>
44 #include <sys/eventhandler.h>
45 #include <sys/malloc.h>
48 #include <sys/reboot.h>
49 #include <geom/geom_disk.h>
58 #include <cam/cam_ccb.h>
59 #include <cam/cam_periph.h>
60 #include <cam/cam_xpt_periph.h>
61 #include <cam/cam_sim.h>
63 #include <cam/ata/ata_all.h>
65 #include <machine/md_var.h> /* geometry translation */
69 #define ATA_MAX_28BIT_LBA 268435455UL
78 ADA_FLAG_CAN_48BIT = 0x0002,
79 ADA_FLAG_CAN_FLUSHCACHE = 0x0004,
80 ADA_FLAG_CAN_NCQ = 0x0008,
81 ADA_FLAG_CAN_DMA = 0x0010,
82 ADA_FLAG_NEED_OTAG = 0x0020,
83 ADA_FLAG_WENT_IDLE = 0x0040,
84 ADA_FLAG_CAN_TRIM = 0x0080,
85 ADA_FLAG_OPEN = 0x0100,
86 ADA_FLAG_SCTX_INIT = 0x0200,
87 ADA_FLAG_CAN_CFA = 0x0400,
88 ADA_FLAG_CAN_POWERMGT = 0x0800,
89 ADA_FLAG_CAN_DMA48 = 0x1000,
90 ADA_FLAG_DIRTY = 0x2000
98 #define ADA_Q_BIT_STRING \
103 ADA_CCB_RAHEAD = 0x01,
104 ADA_CCB_WCACHE = 0x02,
105 ADA_CCB_BUFFER_IO = 0x03,
106 ADA_CCB_WAITING = 0x04,
109 ADA_CCB_TYPE_MASK = 0x0F,
112 /* Offsets into our private area for storing information */
113 #define ccb_state ppriv_field0
114 #define ccb_bp ppriv_ptr1
118 u_int8_t secs_per_track;
120 u_int32_t secsize; /* Number of bytes/logical sector */
121 u_int64_t sectors; /* Total number sectors */
124 #define TRIM_MAX_BLOCKS 8
125 #define TRIM_MAX_RANGES (TRIM_MAX_BLOCKS * ATA_DSM_BLK_RANGES)
126 #define TRIM_MAX_BIOS (TRIM_MAX_RANGES * 4)
127 struct trim_request {
128 uint8_t data[TRIM_MAX_RANGES * ATA_DSM_RANGE_SIZE];
129 struct bio *bps[TRIM_MAX_BIOS];
133 struct bio_queue_head bio_queue;
134 struct bio_queue_head trim_queue;
139 int ordered_tag_count;
140 int outstanding_cmds;
145 #ifdef ADA_TEST_FAILURE
146 int force_read_error;
147 int force_write_error;
148 int periodic_read_error;
149 int periodic_read_count;
151 struct disk_params params;
153 struct task sysctl_task;
154 struct sysctl_ctx_list sysctl_ctx;
155 struct sysctl_oid *sysctl_tree;
156 struct callout sendordered_c;
157 struct trim_request trim_req;
160 struct ada_quirk_entry {
161 struct scsi_inquiry_pattern inq_pat;
165 static struct ada_quirk_entry ada_quirk_table[] =
168 /* Hitachi Advanced Format (4k) drives */
169 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Hitachi H??????????E3*", "*" },
173 /* Samsung Advanced Format (4k) drives */
174 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD155UI*", "*" },
178 /* Samsung Advanced Format (4k) drives */
179 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD204UI*", "*" },
183 /* Seagate Barracuda Green Advanced Format (4k) drives */
184 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DL*", "*" },
188 /* Seagate Barracuda Advanced Format (4k) drives */
189 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???DM*", "*" },
193 /* Seagate Barracuda Advanced Format (4k) drives */
194 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DM*", "*" },
198 /* Seagate Momentus Advanced Format (4k) drives */
199 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500423AS*", "*" },
203 /* Seagate Momentus Advanced Format (4k) drives */
204 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500424AS*", "*" },
208 /* Seagate Momentus Advanced Format (4k) drives */
209 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640423AS*", "*" },
213 /* Seagate Momentus Advanced Format (4k) drives */
214 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640424AS*", "*" },
218 /* Seagate Momentus Advanced Format (4k) drives */
219 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750420AS*", "*" },
223 /* Seagate Momentus Advanced Format (4k) drives */
224 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750422AS*", "*" },
228 /* Seagate Momentus Advanced Format (4k) drives */
229 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750423AS*", "*" },
233 /* Seagate Momentus Thin Advanced Format (4k) drives */
234 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???LT*", "*" },
238 /* WDC Caviar Green Advanced Format (4k) drives */
239 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RS*", "*" },
243 /* WDC Caviar Green Advanced Format (4k) drives */
244 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RX*", "*" },
248 /* WDC Caviar Green Advanced Format (4k) drives */
249 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RS*", "*" },
253 /* WDC Caviar Green Advanced Format (4k) drives */
254 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RX*", "*" },
258 /* WDC Scorpio Black Advanced Format (4k) drives */
259 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PKT*", "*" },
263 /* WDC Scorpio Black Advanced Format (4k) drives */
264 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PKT*", "*" },
268 /* WDC Scorpio Blue Advanced Format (4k) drives */
269 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PVT*", "*" },
273 /* WDC Scorpio Blue Advanced Format (4k) drives */
274 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PVT*", "*" },
280 * Corsair Force 2 SSDs
281 * 4k optimised & trim only works in 4k requests + 4k aligned
283 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair CSSD-F*", "*" },
288 * Corsair Force 3 SSDs
289 * 4k optimised & trim only works in 4k requests + 4k aligned
291 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force 3*", "*" },
296 * Corsair Force GT SSDs
297 * 4k optimised & trim only works in 4k requests + 4k aligned
299 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force GT*", "*" },
305 * 4k optimised & trim only works in 4k requests + 4k aligned
307 { T_DIRECT, SIP_MEDIA_FIXED, "*", "M4-CT???M4SSD2*", "*" },
312 * Crucial RealSSD C300 SSDs
315 { T_DIRECT, SIP_MEDIA_FIXED, "*", "C300-CTFDDAC???MAG*",
316 "*" }, /*quirks*/ADA_Q_4K
320 * Intel 320 Series SSDs
321 * 4k optimised & trim only works in 4k requests + 4k aligned
323 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2CW*", "*" },
328 * Intel 330 Series SSDs
329 * 4k optimised & trim only works in 4k requests + 4k aligned
331 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2CT*", "*" },
336 * Intel 510 Series SSDs
337 * 4k optimised & trim only works in 4k requests + 4k aligned
339 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2MH*", "*" },
344 * Intel 520 Series SSDs
345 * 4k optimised & trim only works in 4k requests + 4k aligned
347 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2BW*", "*" },
352 * Intel X25-M Series SSDs
353 * 4k optimised & trim only works in 4k requests + 4k aligned
355 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2M*", "*" },
360 * Kingston E100 Series SSDs
361 * 4k optimised & trim only works in 4k requests + 4k aligned
363 { T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SE100S3*", "*" },
368 * Kingston HyperX 3k SSDs
369 * 4k optimised & trim only works in 4k requests + 4k aligned
371 { T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SH103S3*", "*" },
376 * Marvell SSDs (entry taken from OpenSolaris)
377 * 4k optimised & trim only works in 4k requests + 4k aligned
379 { T_DIRECT, SIP_MEDIA_FIXED, "*", "MARVELL SD88SA02*", "*" },
385 * 4k optimised & trim only works in 4k requests + 4k aligned
387 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY2*", "*" },
393 * 4k optimised & trim only works in 4k requests + 4k aligned
395 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY3*", "*" },
400 * OCZ Deneva R Series SSDs
401 * 4k optimised & trim only works in 4k requests + 4k aligned
403 { T_DIRECT, SIP_MEDIA_FIXED, "*", "DENRSTE251M45*", "*" },
408 * OCZ Vertex 2 SSDs (inc pro series)
409 * 4k optimised & trim only works in 4k requests + 4k aligned
411 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ?VERTEX2*", "*" },
417 * 4k optimised & trim only works in 4k requests + 4k aligned
419 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX3*", "*" },
425 * 4k optimised & trim only works in 4k requests + 4k aligned
427 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX4*", "*" },
432 * Samsung 830 Series SSDs
435 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG SSD 830 Series*", "*" },
440 * SuperTalent TeraDrive CT SSDs
441 * 4k optimised & trim only works in 4k requests + 4k aligned
443 { T_DIRECT, SIP_MEDIA_FIXED, "*", "FTM??CT25H*", "*" },
448 * XceedIOPS SATA SSDs
451 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SG9XCS2D*", "*" },
457 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
458 /*vendor*/"*", /*product*/"*", /*revision*/"*"
464 static disk_strategy_t adastrategy;
465 static dumper_t adadump;
466 static periph_init_t adainit;
467 static void adaasync(void *callback_arg, u_int32_t code,
468 struct cam_path *path, void *arg);
469 static void adasysctlinit(void *context, int pending);
470 static periph_ctor_t adaregister;
471 static periph_dtor_t adacleanup;
472 static periph_start_t adastart;
473 static periph_oninv_t adaoninvalidate;
474 static void adadone(struct cam_periph *periph,
475 union ccb *done_ccb);
476 static int adaerror(union ccb *ccb, u_int32_t cam_flags,
477 u_int32_t sense_flags);
478 static void adagetparams(struct cam_periph *periph,
479 struct ccb_getdev *cgd);
480 static timeout_t adasendorderedtag;
481 static void adashutdown(void *arg, int howto);
482 static void adasuspend(void *arg);
483 static void adaresume(void *arg);
485 #ifndef ADA_DEFAULT_LEGACY_ALIASES
486 #define ADA_DEFAULT_LEGACY_ALIASES 1
489 #ifndef ADA_DEFAULT_TIMEOUT
490 #define ADA_DEFAULT_TIMEOUT 30 /* Timeout in seconds */
493 #ifndef ADA_DEFAULT_RETRY
494 #define ADA_DEFAULT_RETRY 4
497 #ifndef ADA_DEFAULT_SEND_ORDERED
498 #define ADA_DEFAULT_SEND_ORDERED 1
501 #ifndef ADA_DEFAULT_SPINDOWN_SHUTDOWN
502 #define ADA_DEFAULT_SPINDOWN_SHUTDOWN 1
505 #ifndef ADA_DEFAULT_SPINDOWN_SUSPEND
506 #define ADA_DEFAULT_SPINDOWN_SUSPEND 1
509 #ifndef ADA_DEFAULT_READ_AHEAD
510 #define ADA_DEFAULT_READ_AHEAD 1
513 #ifndef ADA_DEFAULT_WRITE_CACHE
514 #define ADA_DEFAULT_WRITE_CACHE 1
517 #define ADA_RA (softc->read_ahead >= 0 ? \
518 softc->read_ahead : ada_read_ahead)
519 #define ADA_WC (softc->write_cache >= 0 ? \
520 softc->write_cache : ada_write_cache)
521 #define ADA_SIO (softc->sort_io_queue >= 0 ? \
522 softc->sort_io_queue : cam_sort_io_queues)
525 * Most platforms map firmware geometry to actual, but some don't. If
526 * not overridden, default to nothing.
528 #ifndef ata_disk_firmware_geom_adjust
529 #define ata_disk_firmware_geom_adjust(disk)
532 static int ada_legacy_aliases = ADA_DEFAULT_LEGACY_ALIASES;
533 static int ada_retry_count = ADA_DEFAULT_RETRY;
534 static int ada_default_timeout = ADA_DEFAULT_TIMEOUT;
535 static int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED;
536 static int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN;
537 static int ada_spindown_suspend = ADA_DEFAULT_SPINDOWN_SUSPEND;
538 static int ada_read_ahead = ADA_DEFAULT_READ_AHEAD;
539 static int ada_write_cache = ADA_DEFAULT_WRITE_CACHE;
541 static SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD, 0,
542 "CAM Direct Access Disk driver");
543 SYSCTL_INT(_kern_cam_ada, OID_AUTO, legacy_aliases, CTLFLAG_RW,
544 &ada_legacy_aliases, 0, "Create legacy-like device aliases");
545 TUNABLE_INT("kern.cam.ada.legacy_aliases", &ada_legacy_aliases);
546 SYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RW,
547 &ada_retry_count, 0, "Normal I/O retry count");
548 TUNABLE_INT("kern.cam.ada.retry_count", &ada_retry_count);
549 SYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RW,
550 &ada_default_timeout, 0, "Normal I/O timeout (in seconds)");
551 TUNABLE_INT("kern.cam.ada.default_timeout", &ada_default_timeout);
552 SYSCTL_INT(_kern_cam_ada, OID_AUTO, send_ordered, CTLFLAG_RW,
553 &ada_send_ordered, 0, "Send Ordered Tags");
554 TUNABLE_INT("kern.cam.ada.send_ordered", &ada_send_ordered);
555 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RW,
556 &ada_spindown_shutdown, 0, "Spin down upon shutdown");
557 TUNABLE_INT("kern.cam.ada.spindown_shutdown", &ada_spindown_shutdown);
558 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_suspend, CTLFLAG_RW,
559 &ada_spindown_suspend, 0, "Spin down upon suspend");
560 TUNABLE_INT("kern.cam.ada.spindown_suspend", &ada_spindown_suspend);
561 SYSCTL_INT(_kern_cam_ada, OID_AUTO, read_ahead, CTLFLAG_RW,
562 &ada_read_ahead, 0, "Enable disk read-ahead");
563 TUNABLE_INT("kern.cam.ada.read_ahead", &ada_read_ahead);
564 SYSCTL_INT(_kern_cam_ada, OID_AUTO, write_cache, CTLFLAG_RW,
565 &ada_write_cache, 0, "Enable disk write cache");
566 TUNABLE_INT("kern.cam.ada.write_cache", &ada_write_cache);
569 * ADA_ORDEREDTAG_INTERVAL determines how often, relative
570 * to the default timeout, we check to see whether an ordered
571 * tagged transaction is appropriate to prevent simple tag
572 * starvation. Since we'd like to ensure that there is at least
573 * 1/2 of the timeout length left for a starved transaction to
574 * complete after we've sent an ordered tag, we must poll at least
575 * four times in every timeout period. This takes care of the worst
576 * case where a starved transaction starts during an interval that
577 * meets the requirement "don't send an ordered tag" test so it takes
578 * us two intervals to determine that a tag must be sent.
580 #ifndef ADA_ORDEREDTAG_INTERVAL
581 #define ADA_ORDEREDTAG_INTERVAL 4
584 static struct periph_driver adadriver =
587 TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0
590 PERIPHDRIVER_DECLARE(ada, adadriver);
592 static MALLOC_DEFINE(M_ATADA, "ata_da", "ata_da buffers");
595 adaopen(struct disk *dp)
597 struct cam_periph *periph;
598 struct ada_softc *softc;
601 periph = (struct cam_periph *)dp->d_drv1;
602 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
606 cam_periph_lock(periph);
607 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
608 cam_periph_unlock(periph);
609 cam_periph_release(periph);
613 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
616 softc = (struct ada_softc *)periph->softc;
617 softc->flags |= ADA_FLAG_OPEN;
619 cam_periph_unhold(periph);
620 cam_periph_unlock(periph);
625 adaclose(struct disk *dp)
627 struct cam_periph *periph;
628 struct ada_softc *softc;
632 periph = (struct cam_periph *)dp->d_drv1;
633 cam_periph_lock(periph);
634 if (cam_periph_hold(periph, PRIBIO) != 0) {
635 cam_periph_unlock(periph);
636 cam_periph_release(periph);
640 softc = (struct ada_softc *)periph->softc;
642 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
645 /* We only sync the cache if the drive is capable of it. */
646 if ((softc->flags & ADA_FLAG_DIRTY) != 0 &&
647 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) != 0 &&
648 (periph->flags & CAM_PERIPH_INVALID) == 0) {
650 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
651 cam_fill_ataio(&ccb->ataio,
658 ada_default_timeout*1000);
660 if (softc->flags & ADA_FLAG_CAN_48BIT)
661 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
663 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
664 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
665 /*sense_flags*/0, softc->disk->d_devstat);
668 xpt_print(periph->path, "Synchronize cache failed\n");
670 softc->flags &= ~ADA_FLAG_DIRTY;
671 xpt_release_ccb(ccb);
674 softc->flags &= ~ADA_FLAG_OPEN;
675 cam_periph_unhold(periph);
676 cam_periph_unlock(periph);
677 cam_periph_release(periph);
682 adaschedule(struct cam_periph *periph)
684 struct ada_softc *softc = (struct ada_softc *)periph->softc;
687 if (softc->state != ADA_STATE_NORMAL)
690 /* Check if cam_periph_getccb() was called. */
691 prio = periph->immediate_priority;
693 /* Check if we have more work to do. */
694 if (bioq_first(&softc->bio_queue) ||
695 (!softc->trim_running && bioq_first(&softc->trim_queue))) {
696 prio = CAM_PRIORITY_NORMAL;
699 /* Schedule CCB if any of above is true. */
700 if (prio != CAM_PRIORITY_NONE)
701 xpt_schedule(periph, prio);
705 * Actually translate the requested transfer into one the physical driver
706 * can understand. The transfer is described by a buf and will include
707 * only one physical transfer.
710 adastrategy(struct bio *bp)
712 struct cam_periph *periph;
713 struct ada_softc *softc;
715 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
716 softc = (struct ada_softc *)periph->softc;
718 cam_periph_lock(periph);
720 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastrategy(%p)\n", bp));
723 * If the device has been made invalid, error out
725 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
726 cam_periph_unlock(periph);
727 biofinish(bp, NULL, ENXIO);
732 * Place it in the queue of disk activities for this disk
734 if (bp->bio_cmd == BIO_DELETE &&
735 (softc->flags & ADA_FLAG_CAN_TRIM)) {
737 bioq_disksort(&softc->trim_queue, bp);
739 bioq_insert_tail(&softc->trim_queue, bp);
742 bioq_disksort(&softc->bio_queue, bp);
744 bioq_insert_tail(&softc->bio_queue, bp);
748 * Schedule ourselves for performing the work.
751 cam_periph_unlock(periph);
757 adadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
759 struct cam_periph *periph;
760 struct ada_softc *softc;
770 softc = (struct ada_softc *)periph->softc;
771 cam_periph_lock(periph);
772 secsize = softc->params.secsize;
773 lba = offset / secsize;
774 count = length / secsize;
776 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
777 cam_periph_unlock(periph);
782 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
783 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
784 cam_fill_ataio(&ccb.ataio,
789 (u_int8_t *) virtual,
791 ada_default_timeout*1000);
792 if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
793 (lba + count >= ATA_MAX_28BIT_LBA ||
795 ata_48bit_cmd(&ccb.ataio, ATA_WRITE_DMA48,
798 ata_28bit_cmd(&ccb.ataio, ATA_WRITE_DMA,
801 xpt_polled_action(&ccb);
803 error = cam_periph_error(&ccb,
804 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
805 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
806 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
807 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
809 printf("Aborting dump due to I/O error.\n");
811 cam_periph_unlock(periph);
815 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) {
816 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
818 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
819 cam_fill_ataio(&ccb.ataio,
826 ada_default_timeout*1000);
828 if (softc->flags & ADA_FLAG_CAN_48BIT)
829 ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0);
831 ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0);
832 xpt_polled_action(&ccb);
834 error = cam_periph_error(&ccb,
835 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
836 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
837 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
838 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
840 xpt_print(periph->path, "Synchronize cache failed\n");
842 cam_periph_unlock(periph);
852 * Install a global async callback. This callback will
853 * receive async callbacks like "new device found".
855 status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL);
857 if (status != CAM_REQ_CMP) {
858 printf("ada: Failed to attach master async callback "
859 "due to status 0x%x!\n", status);
860 } else if (ada_send_ordered) {
862 /* Register our event handlers */
863 if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend,
864 NULL, EVENTHANDLER_PRI_LAST)) == NULL)
865 printf("adainit: power event registration failed!\n");
866 if ((EVENTHANDLER_REGISTER(power_resume, adaresume,
867 NULL, EVENTHANDLER_PRI_LAST)) == NULL)
868 printf("adainit: power event registration failed!\n");
869 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown,
870 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
871 printf("adainit: shutdown event registration failed!\n");
876 * Callback from GEOM, called when it has finished cleaning up its
880 adadiskgonecb(struct disk *dp)
882 struct cam_periph *periph;
884 periph = (struct cam_periph *)dp->d_drv1;
886 cam_periph_release(periph);
890 adaoninvalidate(struct cam_periph *periph)
892 struct ada_softc *softc;
894 softc = (struct ada_softc *)periph->softc;
897 * De-register any async callbacks.
899 xpt_register_async(0, adaasync, periph, periph->path);
902 * Return all queued I/O with ENXIO.
903 * XXX Handle any transactions queued to the card
904 * with XPT_ABORT_CCB.
906 bioq_flush(&softc->bio_queue, NULL, ENXIO);
907 bioq_flush(&softc->trim_queue, NULL, ENXIO);
909 disk_gone(softc->disk);
910 xpt_print(periph->path, "lost device\n");
914 adacleanup(struct cam_periph *periph)
916 struct ada_softc *softc;
918 softc = (struct ada_softc *)periph->softc;
920 xpt_print(periph->path, "removing device entry\n");
921 cam_periph_unlock(periph);
924 * If we can't free the sysctl tree, oh well...
926 if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0
927 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
928 xpt_print(periph->path, "can't remove sysctl context\n");
931 disk_destroy(softc->disk);
932 callout_drain(&softc->sendordered_c);
933 free(softc, M_DEVBUF);
934 cam_periph_lock(periph);
938 adaasync(void *callback_arg, u_int32_t code,
939 struct cam_path *path, void *arg)
941 struct ccb_getdev cgd;
942 struct cam_periph *periph;
943 struct ada_softc *softc;
945 periph = (struct cam_periph *)callback_arg;
947 case AC_FOUND_DEVICE:
949 struct ccb_getdev *cgd;
952 cgd = (struct ccb_getdev *)arg;
956 if (cgd->protocol != PROTO_ATA)
960 * Allocate a peripheral instance for
961 * this device and start the probe
964 status = cam_periph_alloc(adaregister, adaoninvalidate,
965 adacleanup, adastart,
966 "ada", CAM_PERIPH_BIO,
967 cgd->ccb_h.path, adaasync,
968 AC_FOUND_DEVICE, cgd);
970 if (status != CAM_REQ_CMP
971 && status != CAM_REQ_INPROG)
972 printf("adaasync: Unable to attach to new device "
973 "due to status 0x%x\n", status);
976 case AC_GETDEV_CHANGED:
978 softc = (struct ada_softc *)periph->softc;
979 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
980 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
981 xpt_action((union ccb *)&cgd);
983 if ((cgd.ident_data.capabilities1 & ATA_SUPPORT_DMA) &&
984 (cgd.inq_flags & SID_DMA))
985 softc->flags |= ADA_FLAG_CAN_DMA;
987 softc->flags &= ~ADA_FLAG_CAN_DMA;
988 if (cgd.ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) {
989 softc->flags |= ADA_FLAG_CAN_48BIT;
990 if (cgd.inq_flags & SID_DMA48)
991 softc->flags |= ADA_FLAG_CAN_DMA48;
993 softc->flags &= ~ADA_FLAG_CAN_DMA48;
995 softc->flags &= ~(ADA_FLAG_CAN_48BIT |
997 if ((cgd.ident_data.satacapabilities & ATA_SUPPORT_NCQ) &&
998 (cgd.inq_flags & SID_DMA) && (cgd.inq_flags & SID_CmdQue))
999 softc->flags |= ADA_FLAG_CAN_NCQ;
1001 softc->flags &= ~ADA_FLAG_CAN_NCQ;
1002 if ((cgd.ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) &&
1003 (cgd.inq_flags & SID_DMA))
1004 softc->flags |= ADA_FLAG_CAN_TRIM;
1006 softc->flags &= ~ADA_FLAG_CAN_TRIM;
1008 cam_periph_async(periph, code, path, arg);
1011 case AC_ADVINFO_CHANGED:
1015 buftype = (uintptr_t)arg;
1016 if (buftype == CDAI_TYPE_PHYS_PATH) {
1017 struct ada_softc *softc;
1019 softc = periph->softc;
1020 disk_attr_changed(softc->disk, "GEOM::physpath",
1028 softc = (struct ada_softc *)periph->softc;
1029 cam_periph_async(periph, code, path, arg);
1030 if (softc->state != ADA_STATE_NORMAL)
1032 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1033 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1034 xpt_action((union ccb *)&cgd);
1036 cgd.ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD)
1037 softc->state = ADA_STATE_RAHEAD;
1038 else if (ADA_WC >= 0 &&
1039 cgd.ident_data.support.command1 & ATA_SUPPORT_WRITECACHE)
1040 softc->state = ADA_STATE_WCACHE;
1043 cam_periph_acquire(periph);
1044 xpt_schedule(periph, CAM_PRIORITY_DEV);
1047 cam_periph_async(periph, code, path, arg);
1053 adasysctlinit(void *context, int pending)
1055 struct cam_periph *periph;
1056 struct ada_softc *softc;
1057 char tmpstr[80], tmpstr2[80];
1059 periph = (struct cam_periph *)context;
1061 /* periph was held for us when this task was enqueued */
1062 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
1063 cam_periph_release(periph);
1067 softc = (struct ada_softc *)periph->softc;
1068 snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d", periph->unit_number);
1069 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1071 sysctl_ctx_init(&softc->sysctl_ctx);
1072 softc->flags |= ADA_FLAG_SCTX_INIT;
1073 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1074 SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2,
1075 CTLFLAG_RD, 0, tmpstr);
1076 if (softc->sysctl_tree == NULL) {
1077 printf("adasysctlinit: unable to allocate sysctl tree\n");
1078 cam_periph_release(periph);
1082 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1083 OID_AUTO, "read_ahead", CTLFLAG_RW | CTLFLAG_MPSAFE,
1084 &softc->read_ahead, 0, "Enable disk read ahead.");
1085 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1086 OID_AUTO, "write_cache", CTLFLAG_RW | CTLFLAG_MPSAFE,
1087 &softc->write_cache, 0, "Enable disk write cache.");
1088 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1089 OID_AUTO, "sort_io_queue", CTLFLAG_RW | CTLFLAG_MPSAFE,
1090 &softc->sort_io_queue, 0,
1091 "Sort IO queue to try and optimise disk access patterns");
1092 #ifdef ADA_TEST_FAILURE
1094 * Add a 'door bell' sysctl which allows one to set it from userland
1095 * and cause something bad to happen. For the moment, we only allow
1096 * whacking the next read or write.
1098 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1099 OID_AUTO, "force_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1100 &softc->force_read_error, 0,
1101 "Force a read error for the next N reads.");
1102 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1103 OID_AUTO, "force_write_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1104 &softc->force_write_error, 0,
1105 "Force a write error for the next N writes.");
1106 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1107 OID_AUTO, "periodic_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1108 &softc->periodic_read_error, 0,
1109 "Force a read error every N reads (don't set too low).");
1111 cam_periph_release(periph);
1115 adagetattr(struct bio *bp)
1118 struct cam_periph *periph;
1120 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1121 cam_periph_lock(periph);
1122 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1124 cam_periph_unlock(periph);
1126 bp->bio_completed = bp->bio_length;
1131 adaregister(struct cam_periph *periph, void *arg)
1133 struct ada_softc *softc;
1134 struct ccb_pathinq cpi;
1135 struct ccb_getdev *cgd;
1136 char announce_buf[80], buf1[32];
1137 struct disk_params *dp;
1140 int legacy_id, quirks;
1142 cgd = (struct ccb_getdev *)arg;
1144 printf("adaregister: no getdev CCB, can't register device\n");
1145 return(CAM_REQ_CMP_ERR);
1148 softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF,
1151 if (softc == NULL) {
1152 printf("adaregister: Unable to probe new device. "
1153 "Unable to allocate softc\n");
1154 return(CAM_REQ_CMP_ERR);
1157 bioq_init(&softc->bio_queue);
1158 bioq_init(&softc->trim_queue);
1160 if ((cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA) &&
1161 (cgd->inq_flags & SID_DMA))
1162 softc->flags |= ADA_FLAG_CAN_DMA;
1163 if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) {
1164 softc->flags |= ADA_FLAG_CAN_48BIT;
1165 if (cgd->inq_flags & SID_DMA48)
1166 softc->flags |= ADA_FLAG_CAN_DMA48;
1168 if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE)
1169 softc->flags |= ADA_FLAG_CAN_FLUSHCACHE;
1170 if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT)
1171 softc->flags |= ADA_FLAG_CAN_POWERMGT;
1172 if ((cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ) &&
1173 (cgd->inq_flags & SID_DMA) && (cgd->inq_flags & SID_CmdQue))
1174 softc->flags |= ADA_FLAG_CAN_NCQ;
1175 if ((cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) &&
1176 (cgd->inq_flags & SID_DMA)) {
1177 softc->flags |= ADA_FLAG_CAN_TRIM;
1178 softc->trim_max_ranges = TRIM_MAX_RANGES;
1179 if (cgd->ident_data.max_dsm_blocks != 0) {
1180 softc->trim_max_ranges =
1181 min(cgd->ident_data.max_dsm_blocks *
1182 ATA_DSM_BLK_RANGES, softc->trim_max_ranges);
1185 if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA)
1186 softc->flags |= ADA_FLAG_CAN_CFA;
1188 periph->softc = softc;
1191 * See if this device has any quirks.
1193 match = cam_quirkmatch((caddr_t)&cgd->ident_data,
1194 (caddr_t)ada_quirk_table,
1195 sizeof(ada_quirk_table)/sizeof(*ada_quirk_table),
1196 sizeof(*ada_quirk_table), ata_identify_match);
1198 softc->quirks = ((struct ada_quirk_entry *)match)->quirks;
1200 softc->quirks = ADA_Q_NONE;
1202 bzero(&cpi, sizeof(cpi));
1203 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE);
1204 cpi.ccb_h.func_code = XPT_PATH_INQ;
1205 xpt_action((union ccb *)&cpi);
1207 TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph);
1210 * Register this media as a disk
1212 (void)cam_periph_hold(periph, PRIBIO);
1213 cam_periph_unlock(periph);
1214 snprintf(announce_buf, sizeof(announce_buf),
1215 "kern.cam.ada.%d.quirks", periph->unit_number);
1216 quirks = softc->quirks;
1217 TUNABLE_INT_FETCH(announce_buf, &quirks);
1218 softc->quirks = quirks;
1219 softc->read_ahead = -1;
1220 snprintf(announce_buf, sizeof(announce_buf),
1221 "kern.cam.ada.%d.read_ahead", periph->unit_number);
1222 TUNABLE_INT_FETCH(announce_buf, &softc->read_ahead);
1223 softc->write_cache = -1;
1224 snprintf(announce_buf, sizeof(announce_buf),
1225 "kern.cam.ada.%d.write_cache", periph->unit_number);
1226 TUNABLE_INT_FETCH(announce_buf, &softc->write_cache);
1227 /* Disable queue sorting for non-rotational media by default. */
1228 if (cgd->ident_data.media_rotation_rate == 1)
1229 softc->sort_io_queue = 0;
1231 softc->sort_io_queue = -1;
1232 adagetparams(periph, cgd);
1233 softc->disk = disk_alloc();
1234 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
1235 periph->unit_number, softc->params.secsize,
1236 DEVSTAT_ALL_SUPPORTED,
1237 DEVSTAT_TYPE_DIRECT |
1238 XPORT_DEVSTAT_TYPE(cpi.transport),
1239 DEVSTAT_PRIORITY_DISK);
1240 softc->disk->d_open = adaopen;
1241 softc->disk->d_close = adaclose;
1242 softc->disk->d_strategy = adastrategy;
1243 softc->disk->d_getattr = adagetattr;
1244 softc->disk->d_dump = adadump;
1245 softc->disk->d_gone = adadiskgonecb;
1246 softc->disk->d_name = "ada";
1247 softc->disk->d_drv1 = periph;
1248 maxio = cpi.maxio; /* Honor max I/O size of SIM */
1250 maxio = DFLTPHYS; /* traditional default */
1251 else if (maxio > MAXPHYS)
1252 maxio = MAXPHYS; /* for safety */
1253 if (softc->flags & ADA_FLAG_CAN_48BIT)
1254 maxio = min(maxio, 65536 * softc->params.secsize);
1255 else /* 28bit ATA command limit */
1256 maxio = min(maxio, 256 * softc->params.secsize);
1257 softc->disk->d_maxsize = maxio;
1258 softc->disk->d_unit = periph->unit_number;
1259 softc->disk->d_flags = 0;
1260 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE)
1261 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
1262 if (softc->flags & ADA_FLAG_CAN_TRIM) {
1263 softc->disk->d_flags |= DISKFLAG_CANDELETE;
1264 softc->disk->d_delmaxsize = softc->params.secsize *
1266 softc->trim_max_ranges;
1267 } else if ((softc->flags & ADA_FLAG_CAN_CFA) &&
1268 !(softc->flags & ADA_FLAG_CAN_48BIT)) {
1269 softc->disk->d_flags |= DISKFLAG_CANDELETE;
1270 softc->disk->d_delmaxsize = 256 * softc->params.secsize;
1272 softc->disk->d_delmaxsize = maxio;
1273 if ((cpi.hba_misc & PIM_UNMAPPED) != 0)
1274 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
1275 strlcpy(softc->disk->d_descr, cgd->ident_data.model,
1276 MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model)));
1277 strlcpy(softc->disk->d_ident, cgd->ident_data.serial,
1278 MIN(sizeof(softc->disk->d_ident), sizeof(cgd->ident_data.serial)));
1279 softc->disk->d_hba_vendor = cpi.hba_vendor;
1280 softc->disk->d_hba_device = cpi.hba_device;
1281 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
1282 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
1284 softc->disk->d_sectorsize = softc->params.secsize;
1285 softc->disk->d_mediasize = (off_t)softc->params.sectors *
1286 softc->params.secsize;
1287 if (ata_physical_sector_size(&cgd->ident_data) !=
1288 softc->params.secsize) {
1289 softc->disk->d_stripesize =
1290 ata_physical_sector_size(&cgd->ident_data);
1291 softc->disk->d_stripeoffset = (softc->disk->d_stripesize -
1292 ata_logical_sector_offset(&cgd->ident_data)) %
1293 softc->disk->d_stripesize;
1294 } else if (softc->quirks & ADA_Q_4K) {
1295 softc->disk->d_stripesize = 4096;
1296 softc->disk->d_stripeoffset = 0;
1298 softc->disk->d_fwsectors = softc->params.secs_per_track;
1299 softc->disk->d_fwheads = softc->params.heads;
1300 ata_disk_firmware_geom_adjust(softc->disk);
1302 if (ada_legacy_aliases) {
1303 #ifdef ATA_STATIC_ID
1304 legacy_id = xpt_path_legacy_ata_id(periph->path);
1306 legacy_id = softc->disk->d_unit;
1308 if (legacy_id >= 0) {
1309 snprintf(announce_buf, sizeof(announce_buf),
1310 "kern.devalias.%s%d",
1311 softc->disk->d_name, softc->disk->d_unit);
1312 snprintf(buf1, sizeof(buf1),
1314 setenv(announce_buf, buf1);
1319 * Acquire a reference to the periph before we register with GEOM.
1320 * We'll release this reference once GEOM calls us back (via
1321 * adadiskgonecb()) telling us that our provider has been freed.
1323 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1324 xpt_print(periph->path, "%s: lost periph during "
1325 "registration!\n", __func__);
1326 cam_periph_lock(periph);
1327 return (CAM_REQ_CMP_ERR);
1329 disk_create(softc->disk, DISK_VERSION);
1330 cam_periph_lock(periph);
1331 cam_periph_unhold(periph);
1333 dp = &softc->params;
1334 snprintf(announce_buf, sizeof(announce_buf),
1335 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1336 (uintmax_t)(((uintmax_t)dp->secsize *
1337 dp->sectors) / (1024*1024)),
1338 (uintmax_t)dp->sectors,
1339 dp->secsize, dp->heads,
1340 dp->secs_per_track, dp->cylinders);
1341 xpt_announce_periph(periph, announce_buf);
1342 xpt_announce_quirks(periph, softc->quirks, ADA_Q_BIT_STRING);
1344 printf("%s%d: Previously was known as ad%d\n",
1345 periph->periph_name, periph->unit_number, legacy_id);
1348 * Create our sysctl variables, now that we know
1349 * we have successfully attached.
1351 cam_periph_acquire(periph);
1352 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task);
1355 * Add async callbacks for bus reset and
1356 * bus device reset calls. I don't bother
1357 * checking if this fails as, in most cases,
1358 * the system will function just fine without
1359 * them and the only alternative would be to
1360 * not attach the device on failure.
1362 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
1363 AC_GETDEV_CHANGED | AC_ADVINFO_CHANGED,
1364 adaasync, periph, periph->path);
1367 * Schedule a periodic event to occasionally send an
1368 * ordered tag to a device.
1370 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
1371 callout_reset(&softc->sendordered_c,
1372 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL,
1373 adasendorderedtag, softc);
1376 cgd->ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD) {
1377 softc->state = ADA_STATE_RAHEAD;
1378 cam_periph_acquire(periph);
1379 xpt_schedule(periph, CAM_PRIORITY_DEV);
1380 } else if (ADA_WC >= 0 &&
1381 cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) {
1382 softc->state = ADA_STATE_WCACHE;
1383 cam_periph_acquire(periph);
1384 xpt_schedule(periph, CAM_PRIORITY_DEV);
1386 softc->state = ADA_STATE_NORMAL;
1388 return(CAM_REQ_CMP);
1392 adastart(struct cam_periph *periph, union ccb *start_ccb)
1394 struct ada_softc *softc = (struct ada_softc *)periph->softc;
1395 struct ccb_ataio *ataio = &start_ccb->ataio;
1397 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastart\n"));
1399 switch (softc->state) {
1400 case ADA_STATE_NORMAL:
1405 /* Execute immediate CCB if waiting. */
1406 if (periph->immediate_priority <= periph->pinfo.priority) {
1407 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1408 ("queuing for immediate ccb\n"));
1409 start_ccb->ccb_h.ccb_state = ADA_CCB_WAITING;
1410 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1412 periph->immediate_priority = CAM_PRIORITY_NONE;
1413 wakeup(&periph->ccb_list);
1414 /* Have more work to do, so ensure we stay scheduled */
1415 adaschedule(periph);
1418 /* Run TRIM if not running yet. */
1419 if (!softc->trim_running &&
1420 (bp = bioq_first(&softc->trim_queue)) != 0) {
1421 struct trim_request *req = &softc->trim_req;
1423 uint64_t lastlba = (uint64_t)-1;
1424 int bps = 0, c, lastcount = 0, off, ranges = 0;
1426 softc->trim_running = 1;
1427 bzero(req, sizeof(*req));
1430 uint64_t lba = bp1->bio_pblkno;
1431 int count = bp1->bio_bcount /
1432 softc->params.secsize;
1434 bioq_remove(&softc->trim_queue, bp1);
1436 /* Try to extend the previous range. */
1437 if (lba == lastlba) {
1438 c = min(count, ATA_DSM_RANGE_MAX - lastcount);
1440 off = (ranges - 1) * ATA_DSM_RANGE_SIZE;
1441 req->data[off + 6] = lastcount & 0xff;
1442 req->data[off + 7] =
1443 (lastcount >> 8) & 0xff;
1449 c = min(count, ATA_DSM_RANGE_MAX);
1450 off = ranges * ATA_DSM_RANGE_SIZE;
1451 req->data[off + 0] = lba & 0xff;
1452 req->data[off + 1] = (lba >> 8) & 0xff;
1453 req->data[off + 2] = (lba >> 16) & 0xff;
1454 req->data[off + 3] = (lba >> 24) & 0xff;
1455 req->data[off + 4] = (lba >> 32) & 0xff;
1456 req->data[off + 5] = (lba >> 40) & 0xff;
1457 req->data[off + 6] = c & 0xff;
1458 req->data[off + 7] = (c >> 8) & 0xff;
1464 * Its the caller's responsibility to ensure the
1465 * request will fit so we don't need to check for
1470 req->bps[bps++] = bp1;
1471 bp1 = bioq_first(&softc->trim_queue);
1472 if (bps >= TRIM_MAX_BIOS ||
1474 bp1->bio_bcount / softc->params.secsize >
1475 (softc->trim_max_ranges - ranges) *
1479 cam_fill_ataio(ataio,
1485 ((ranges + ATA_DSM_BLK_RANGES - 1) /
1486 ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE,
1487 ada_default_timeout * 1000);
1488 ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT,
1489 ATA_DSM_TRIM, 0, (ranges + ATA_DSM_BLK_RANGES -
1490 1) / ATA_DSM_BLK_RANGES);
1491 start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM;
1494 /* Run regular command. */
1495 bp = bioq_first(&softc->bio_queue);
1497 xpt_release_ccb(start_ccb);
1500 bioq_remove(&softc->bio_queue, bp);
1502 if ((bp->bio_flags & BIO_ORDERED) != 0
1503 || (softc->flags & ADA_FLAG_NEED_OTAG) != 0) {
1504 softc->flags &= ~ADA_FLAG_NEED_OTAG;
1505 softc->ordered_tag_count++;
1510 switch (bp->bio_cmd) {
1512 softc->flags |= ADA_FLAG_DIRTY;
1516 uint64_t lba = bp->bio_pblkno;
1517 uint16_t count = bp->bio_bcount / softc->params.secsize;
1518 #ifdef ADA_TEST_FAILURE
1522 * Support the failure ioctls. If the command is a
1523 * read, and there are pending forced read errors, or
1524 * if a write and pending write errors, then fail this
1525 * operation with EIO. This is useful for testing
1526 * purposes. Also, support having every Nth read fail.
1528 * This is a rather blunt tool.
1530 if (bp->bio_cmd == BIO_READ) {
1531 if (softc->force_read_error) {
1532 softc->force_read_error--;
1535 if (softc->periodic_read_error > 0) {
1536 if (++softc->periodic_read_count >=
1537 softc->periodic_read_error) {
1538 softc->periodic_read_count = 0;
1543 if (softc->force_write_error) {
1544 softc->force_write_error--;
1549 bp->bio_error = EIO;
1550 bp->bio_flags |= BIO_ERROR;
1552 xpt_release_ccb(start_ccb);
1553 adaschedule(periph);
1557 KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 ||
1558 round_page(bp->bio_bcount + bp->bio_ma_offset) /
1559 PAGE_SIZE == bp->bio_ma_n,
1560 ("Short bio %p", bp));
1561 cam_fill_ataio(ataio,
1564 (bp->bio_cmd == BIO_READ ? CAM_DIR_IN :
1565 CAM_DIR_OUT) | ((bp->bio_flags & BIO_UNMAPPED)
1566 != 0 ? CAM_DATA_BIO : 0),
1568 ((bp->bio_flags & BIO_UNMAPPED) != 0) ? (void *)bp :
1571 ada_default_timeout*1000);
1573 if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) {
1574 if (bp->bio_cmd == BIO_READ) {
1575 ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED,
1578 ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED,
1581 } else if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
1582 (lba + count >= ATA_MAX_28BIT_LBA ||
1584 if (softc->flags & ADA_FLAG_CAN_DMA48) {
1585 if (bp->bio_cmd == BIO_READ) {
1586 ata_48bit_cmd(ataio, ATA_READ_DMA48,
1589 ata_48bit_cmd(ataio, ATA_WRITE_DMA48,
1593 if (bp->bio_cmd == BIO_READ) {
1594 ata_48bit_cmd(ataio, ATA_READ_MUL48,
1597 ata_48bit_cmd(ataio, ATA_WRITE_MUL48,
1604 if (softc->flags & ADA_FLAG_CAN_DMA) {
1605 if (bp->bio_cmd == BIO_READ) {
1606 ata_28bit_cmd(ataio, ATA_READ_DMA,
1609 ata_28bit_cmd(ataio, ATA_WRITE_DMA,
1613 if (bp->bio_cmd == BIO_READ) {
1614 ata_28bit_cmd(ataio, ATA_READ_MUL,
1617 ata_28bit_cmd(ataio, ATA_WRITE_MUL,
1626 uint64_t lba = bp->bio_pblkno;
1627 uint16_t count = bp->bio_bcount / softc->params.secsize;
1629 cam_fill_ataio(ataio,
1636 ada_default_timeout*1000);
1640 ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count);
1644 cam_fill_ataio(ataio,
1651 ada_default_timeout*1000);
1653 if (softc->flags & ADA_FLAG_CAN_48BIT)
1654 ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1656 ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0);
1659 start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO;
1661 start_ccb->ccb_h.ccb_bp = bp;
1662 softc->outstanding_cmds++;
1663 xpt_action(start_ccb);
1665 /* May have more work to do, so ensure we stay scheduled */
1666 adaschedule(periph);
1669 case ADA_STATE_RAHEAD:
1670 case ADA_STATE_WCACHE:
1672 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
1673 softc->state = ADA_STATE_NORMAL;
1674 xpt_release_ccb(start_ccb);
1675 cam_periph_release_locked(periph);
1679 cam_fill_ataio(ataio,
1686 ada_default_timeout*1000);
1688 if (softc->state == ADA_STATE_RAHEAD) {
1689 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_RA ?
1690 ATA_SF_ENAB_RCACHE : ATA_SF_DIS_RCACHE, 0, 0);
1691 start_ccb->ccb_h.ccb_state = ADA_CCB_RAHEAD;
1693 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_WC ?
1694 ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0);
1695 start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE;
1697 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1698 xpt_action(start_ccb);
1705 adadone(struct cam_periph *periph, union ccb *done_ccb)
1707 struct ada_softc *softc;
1708 struct ccb_ataio *ataio;
1709 struct ccb_getdev *cgd;
1710 struct cam_path *path;
1713 softc = (struct ada_softc *)periph->softc;
1714 ataio = &done_ccb->ataio;
1715 path = done_ccb->ccb_h.path;
1717 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("adadone\n"));
1719 state = ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK;
1721 case ADA_CCB_BUFFER_IO:
1727 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1728 error = adaerror(done_ccb, 0, 0);
1729 if (error == ERESTART) {
1730 /* A retry was scheduled, so just return. */
1733 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1734 cam_release_devq(path,
1738 /*getcount_only*/0);
1740 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1741 panic("REQ_CMP with QFRZN");
1744 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1745 bp->bio_error = error;
1747 bp->bio_resid = bp->bio_bcount;
1748 bp->bio_flags |= BIO_ERROR;
1750 if (state == ADA_CCB_TRIM)
1753 bp->bio_resid = ataio->resid;
1754 if (bp->bio_resid > 0)
1755 bp->bio_flags |= BIO_ERROR;
1757 softc->outstanding_cmds--;
1758 if (softc->outstanding_cmds == 0)
1759 softc->flags |= ADA_FLAG_WENT_IDLE;
1760 if (state == ADA_CCB_TRIM) {
1761 struct trim_request *req =
1762 (struct trim_request *)ataio->data_ptr;
1765 for (i = 1; i < TRIM_MAX_BIOS && req->bps[i]; i++) {
1766 struct bio *bp1 = req->bps[i];
1768 bp1->bio_error = bp->bio_error;
1769 if (bp->bio_flags & BIO_ERROR) {
1770 bp1->bio_flags |= BIO_ERROR;
1771 bp1->bio_resid = bp1->bio_bcount;
1776 softc->trim_running = 0;
1778 adaschedule(periph);
1783 case ADA_CCB_RAHEAD:
1785 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1786 if (adaerror(done_ccb, 0, 0) == ERESTART) {
1788 /* Drop freeze taken due to CAM_DEV_QFREEZE */
1789 cam_release_devq(path, 0, 0, 0, FALSE);
1791 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1792 cam_release_devq(path,
1796 /*getcount_only*/0);
1801 * Since our peripheral may be invalidated by an error
1802 * above or an external event, we must release our CCB
1803 * before releasing the reference on the peripheral.
1804 * The peripheral will only go away once the last reference
1805 * is removed, and we need it around for the CCB release
1808 cgd = (struct ccb_getdev *)done_ccb;
1809 xpt_setup_ccb(&cgd->ccb_h, path, CAM_PRIORITY_NORMAL);
1810 cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1811 xpt_action((union ccb *)cgd);
1813 cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) {
1814 softc->state = ADA_STATE_WCACHE;
1815 xpt_release_ccb(done_ccb);
1816 xpt_schedule(periph, CAM_PRIORITY_DEV);
1819 softc->state = ADA_STATE_NORMAL;
1820 xpt_release_ccb(done_ccb);
1821 /* Drop freeze taken due to CAM_DEV_QFREEZE */
1822 cam_release_devq(path, 0, 0, 0, FALSE);
1823 adaschedule(periph);
1824 cam_periph_release_locked(periph);
1827 case ADA_CCB_WCACHE:
1829 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1830 if (adaerror(done_ccb, 0, 0) == ERESTART) {
1832 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1833 cam_release_devq(path,
1837 /*getcount_only*/0);
1841 softc->state = ADA_STATE_NORMAL;
1843 * Since our peripheral may be invalidated by an error
1844 * above or an external event, we must release our CCB
1845 * before releasing the reference on the peripheral.
1846 * The peripheral will only go away once the last reference
1847 * is removed, and we need it around for the CCB release
1850 xpt_release_ccb(done_ccb);
1851 /* Drop freeze taken due to CAM_DEV_QFREEZE */
1852 cam_release_devq(path, 0, 0, 0, FALSE);
1853 adaschedule(periph);
1854 cam_periph_release_locked(periph);
1857 case ADA_CCB_WAITING:
1859 /* Caller will release the CCB */
1860 wakeup(&done_ccb->ccb_h.cbfcnp);
1864 /* No-op. We're polling */
1869 xpt_release_ccb(done_ccb);
1873 adaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1876 return(cam_periph_error(ccb, cam_flags, sense_flags, NULL));
1880 adagetparams(struct cam_periph *periph, struct ccb_getdev *cgd)
1882 struct ada_softc *softc = (struct ada_softc *)periph->softc;
1883 struct disk_params *dp = &softc->params;
1884 u_int64_t lbasize48;
1887 dp->secsize = ata_logical_sector_size(&cgd->ident_data);
1888 if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) &&
1889 cgd->ident_data.current_heads && cgd->ident_data.current_sectors) {
1890 dp->heads = cgd->ident_data.current_heads;
1891 dp->secs_per_track = cgd->ident_data.current_sectors;
1892 dp->cylinders = cgd->ident_data.cylinders;
1893 dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 |
1894 ((u_int32_t)cgd->ident_data.current_size_2 << 16);
1896 dp->heads = cgd->ident_data.heads;
1897 dp->secs_per_track = cgd->ident_data.sectors;
1898 dp->cylinders = cgd->ident_data.cylinders;
1899 dp->sectors = cgd->ident_data.cylinders * dp->heads * dp->secs_per_track;
1901 lbasize = (u_int32_t)cgd->ident_data.lba_size_1 |
1902 ((u_int32_t)cgd->ident_data.lba_size_2 << 16);
1904 /* use the 28bit LBA size if valid or bigger than the CHS mapping */
1905 if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize)
1906 dp->sectors = lbasize;
1908 /* use the 48bit LBA size if valid */
1909 lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) |
1910 ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) |
1911 ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) |
1912 ((u_int64_t)cgd->ident_data.lba_size48_4 << 48);
1913 if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) &&
1914 lbasize48 > ATA_MAX_28BIT_LBA)
1915 dp->sectors = lbasize48;
1919 adasendorderedtag(void *arg)
1921 struct ada_softc *softc = arg;
1923 if (ada_send_ordered) {
1924 if ((softc->ordered_tag_count == 0)
1925 && ((softc->flags & ADA_FLAG_WENT_IDLE) == 0)) {
1926 softc->flags |= ADA_FLAG_NEED_OTAG;
1928 if (softc->outstanding_cmds > 0)
1929 softc->flags &= ~ADA_FLAG_WENT_IDLE;
1931 softc->ordered_tag_count = 0;
1933 /* Queue us up again */
1934 callout_reset(&softc->sendordered_c,
1935 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL,
1936 adasendorderedtag, softc);
1940 * Step through all ADA peripheral drivers, and if the device is still open,
1941 * sync the disk cache to physical media.
1946 struct cam_periph *periph;
1947 struct ada_softc *softc;
1951 CAM_PERIPH_FOREACH(periph, &adadriver) {
1952 softc = (struct ada_softc *)periph->softc;
1953 if (SCHEDULER_STOPPED()) {
1954 /* If we paniced with the lock held, do not recurse. */
1955 if (!cam_periph_owned(periph) &&
1956 (softc->flags & ADA_FLAG_OPEN)) {
1957 adadump(softc->disk, NULL, 0, 0, 0);
1961 cam_periph_lock(periph);
1963 * We only sync the cache if the drive is still open, and
1964 * if the drive is capable of it..
1966 if (((softc->flags & ADA_FLAG_OPEN) == 0) ||
1967 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) {
1968 cam_periph_unlock(periph);
1972 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1973 cam_fill_ataio(&ccb->ataio,
1980 ada_default_timeout*1000);
1981 if (softc->flags & ADA_FLAG_CAN_48BIT)
1982 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1984 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
1986 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
1987 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
1988 softc->disk->d_devstat);
1990 xpt_print(periph->path, "Synchronize cache failed\n");
1991 xpt_release_ccb(ccb);
1992 cam_periph_unlock(periph);
1997 adaspindown(uint8_t cmd, int flags)
1999 struct cam_periph *periph;
2000 struct ada_softc *softc;
2004 CAM_PERIPH_FOREACH(periph, &adadriver) {
2005 /* If we paniced with lock held - not recurse here. */
2006 if (cam_periph_owned(periph))
2008 cam_periph_lock(periph);
2009 softc = (struct ada_softc *)periph->softc;
2011 * We only spin-down the drive if it is capable of it..
2013 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
2014 cam_periph_unlock(periph);
2019 xpt_print(periph->path, "spin-down\n");
2021 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2022 cam_fill_ataio(&ccb->ataio,
2025 CAM_DIR_NONE | flags,
2029 ada_default_timeout*1000);
2030 ata_28bit_cmd(&ccb->ataio, cmd, 0, 0, 0);
2032 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
2033 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
2034 softc->disk->d_devstat);
2036 xpt_print(periph->path, "Spin-down disk failed\n");
2037 xpt_release_ccb(ccb);
2038 cam_periph_unlock(periph);
2043 adashutdown(void *arg, int howto)
2047 if (ada_spindown_shutdown != 0 &&
2048 (howto & (RB_HALT | RB_POWEROFF)) != 0)
2049 adaspindown(ATA_STANDBY_IMMEDIATE, 0);
2053 adasuspend(void *arg)
2057 if (ada_spindown_suspend != 0)
2058 adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE);
2062 adaresume(void *arg)
2064 struct cam_periph *periph;
2065 struct ada_softc *softc;
2067 if (ada_spindown_suspend == 0)
2070 CAM_PERIPH_FOREACH(periph, &adadriver) {
2071 cam_periph_lock(periph);
2072 softc = (struct ada_softc *)periph->softc;
2074 * We only spin-down the drive if it is capable of it..
2076 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
2077 cam_periph_unlock(periph);
2082 xpt_print(periph->path, "resume\n");
2085 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on
2088 cam_release_devq(periph->path,
2092 /*getcount_only*/0);
2094 cam_periph_unlock(periph);
2098 #endif /* _KERNEL */