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_WAS_OTAG = 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,
108 ADA_CCB_TYPE_MASK = 0x0F,
111 /* Offsets into our private area for storing information */
112 #define ccb_state ppriv_field0
113 #define ccb_bp ppriv_ptr1
117 u_int8_t secs_per_track;
119 u_int32_t secsize; /* Number of bytes/logical sector */
120 u_int64_t sectors; /* Total number sectors */
123 #define TRIM_MAX_BLOCKS 8
124 #define TRIM_MAX_RANGES (TRIM_MAX_BLOCKS * ATA_DSM_BLK_RANGES)
125 struct trim_request {
126 uint8_t data[TRIM_MAX_RANGES * ATA_DSM_RANGE_SIZE];
127 TAILQ_HEAD(, bio) bps;
131 struct bio_queue_head bio_queue;
132 struct bio_queue_head trim_queue;
133 int outstanding_cmds; /* Number of active commands */
134 int refcount; /* Active xpt_action() calls */
143 #ifdef ADA_TEST_FAILURE
144 int force_read_error;
145 int force_write_error;
146 int periodic_read_error;
147 int periodic_read_count;
149 struct disk_params params;
151 struct task sysctl_task;
152 struct sysctl_ctx_list sysctl_ctx;
153 struct sysctl_oid *sysctl_tree;
154 struct callout sendordered_c;
155 struct trim_request trim_req;
158 struct ada_quirk_entry {
159 struct scsi_inquiry_pattern inq_pat;
163 static struct ada_quirk_entry ada_quirk_table[] =
166 /* Hitachi Advanced Format (4k) drives */
167 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Hitachi H??????????E3*", "*" },
171 /* Samsung Advanced Format (4k) drives */
172 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD155UI*", "*" },
176 /* Samsung Advanced Format (4k) drives */
177 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD204UI*", "*" },
181 /* Seagate Barracuda Green Advanced Format (4k) drives */
182 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DL*", "*" },
186 /* Seagate Barracuda Advanced Format (4k) drives */
187 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???DM*", "*" },
191 /* Seagate Barracuda Advanced Format (4k) drives */
192 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DM*", "*" },
196 /* Seagate Momentus Advanced Format (4k) drives */
197 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500423AS*", "*" },
201 /* Seagate Momentus Advanced Format (4k) drives */
202 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500424AS*", "*" },
206 /* Seagate Momentus Advanced Format (4k) drives */
207 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640423AS*", "*" },
211 /* Seagate Momentus Advanced Format (4k) drives */
212 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640424AS*", "*" },
216 /* Seagate Momentus Advanced Format (4k) drives */
217 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750420AS*", "*" },
221 /* Seagate Momentus Advanced Format (4k) drives */
222 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750422AS*", "*" },
226 /* Seagate Momentus Advanced Format (4k) drives */
227 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750423AS*", "*" },
231 /* Seagate Momentus Thin Advanced Format (4k) drives */
232 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???LT*", "*" },
236 /* WDC Caviar Red Advanced Format (4k) drives */
237 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????CX*", "*" },
241 /* WDC Caviar Green Advanced Format (4k) drives */
242 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RS*", "*" },
246 /* WDC Caviar Green/Red Advanced Format (4k) drives */
247 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RX*", "*" },
251 /* WDC Caviar Red Advanced Format (4k) drives */
252 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????CX*", "*" },
256 /* WDC Caviar Black Advanced Format (4k) drives */
257 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????EX*", "*" },
261 /* WDC Caviar Green Advanced Format (4k) drives */
262 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RS*", "*" },
266 /* WDC Caviar Green Advanced Format (4k) drives */
267 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RX*", "*" },
271 /* WDC Scorpio Black Advanced Format (4k) drives */
272 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PKT*", "*" },
276 /* WDC Scorpio Black Advanced Format (4k) drives */
277 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PKT*", "*" },
281 /* WDC Scorpio Blue Advanced Format (4k) drives */
282 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PVT*", "*" },
286 /* WDC Scorpio Blue Advanced Format (4k) drives */
287 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PVT*", "*" },
293 * Corsair Force 2 SSDs
294 * 4k optimised & trim only works in 4k requests + 4k aligned
296 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair CSSD-F*", "*" },
301 * Corsair Force 3 SSDs
302 * 4k optimised & trim only works in 4k requests + 4k aligned
304 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force 3*", "*" },
309 * Corsair Neutron GTX SSDs
310 * 4k optimised & trim only works in 4k requests + 4k aligned
312 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Neutron GTX*", "*" },
317 * Corsair Force GT & GS SSDs
318 * 4k optimised & trim only works in 4k requests + 4k aligned
320 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force G*", "*" },
326 * 4k optimised & trim only works in 4k requests + 4k aligned
328 { T_DIRECT, SIP_MEDIA_FIXED, "*", "M4-CT???M4SSD2*", "*" },
333 * Crucial RealSSD C300 SSDs
336 { T_DIRECT, SIP_MEDIA_FIXED, "*", "C300-CTFDDAC???MAG*",
337 "*" }, /*quirks*/ADA_Q_4K
341 * Intel 320 Series SSDs
342 * 4k optimised & trim only works in 4k requests + 4k aligned
344 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2CW*", "*" },
349 * Intel 330 Series SSDs
350 * 4k optimised & trim only works in 4k requests + 4k aligned
352 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2CT*", "*" },
357 * Intel 510 Series SSDs
358 * 4k optimised & trim only works in 4k requests + 4k aligned
360 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2MH*", "*" },
365 * Intel 520 Series SSDs
366 * 4k optimised & trim only works in 4k requests + 4k aligned
368 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2BW*", "*" },
373 * Intel X25-M Series SSDs
374 * 4k optimised & trim only works in 4k requests + 4k aligned
376 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2M*", "*" },
381 * Kingston E100 Series SSDs
382 * 4k optimised & trim only works in 4k requests + 4k aligned
384 { T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SE100S3*", "*" },
389 * Kingston HyperX 3k SSDs
390 * 4k optimised & trim only works in 4k requests + 4k aligned
392 { T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SH103S3*", "*" },
397 * Marvell SSDs (entry taken from OpenSolaris)
398 * 4k optimised & trim only works in 4k requests + 4k aligned
400 { T_DIRECT, SIP_MEDIA_FIXED, "*", "MARVELL SD88SA02*", "*" },
406 * 4k optimised & trim only works in 4k requests + 4k aligned
408 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY2*", "*" },
414 * 4k optimised & trim only works in 4k requests + 4k aligned
416 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY3*", "*" },
421 * OCZ Deneva R Series SSDs
422 * 4k optimised & trim only works in 4k requests + 4k aligned
424 { T_DIRECT, SIP_MEDIA_FIXED, "*", "DENRSTE251M45*", "*" },
429 * OCZ Vertex 2 SSDs (inc pro series)
430 * 4k optimised & trim only works in 4k requests + 4k aligned
432 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ?VERTEX2*", "*" },
438 * 4k optimised & trim only works in 4k requests + 4k aligned
440 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX3*", "*" },
446 * 4k optimised & trim only works in 4k requests + 4k aligned
448 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX4*", "*" },
453 * Samsung 830 Series SSDs
456 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG SSD 830 Series*", "*" },
464 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Samsung SSD 840*", "*" },
469 * Samsung 843T Series SSDs
472 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG MZ7WD*", "*" },
480 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Samsung SSD 850*", "*" },
485 * Samsung PM853T Series SSDs
488 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG MZ7GE*", "*" },
493 * SuperTalent TeraDrive CT SSDs
494 * 4k optimised & trim only works in 4k requests + 4k aligned
496 { T_DIRECT, SIP_MEDIA_FIXED, "*", "FTM??CT25H*", "*" },
501 * XceedIOPS SATA SSDs
504 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SG9XCS2D*", "*" },
510 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
511 /*vendor*/"*", /*product*/"*", /*revision*/"*"
517 static disk_strategy_t adastrategy;
518 static dumper_t adadump;
519 static periph_init_t adainit;
520 static void adaasync(void *callback_arg, u_int32_t code,
521 struct cam_path *path, void *arg);
522 static void adasysctlinit(void *context, int pending);
523 static periph_ctor_t adaregister;
524 static periph_dtor_t adacleanup;
525 static periph_start_t adastart;
526 static periph_oninv_t adaoninvalidate;
527 static void adadone(struct cam_periph *periph,
528 union ccb *done_ccb);
529 static int adaerror(union ccb *ccb, u_int32_t cam_flags,
530 u_int32_t sense_flags);
531 static void adagetparams(struct cam_periph *periph,
532 struct ccb_getdev *cgd);
533 static timeout_t adasendorderedtag;
534 static void adashutdown(void *arg, int howto);
535 static void adasuspend(void *arg);
536 static void adaresume(void *arg);
538 #ifndef ADA_DEFAULT_LEGACY_ALIASES
539 #define ADA_DEFAULT_LEGACY_ALIASES 1
542 #ifndef ADA_DEFAULT_TIMEOUT
543 #define ADA_DEFAULT_TIMEOUT 30 /* Timeout in seconds */
546 #ifndef ADA_DEFAULT_RETRY
547 #define ADA_DEFAULT_RETRY 4
550 #ifndef ADA_DEFAULT_SEND_ORDERED
551 #define ADA_DEFAULT_SEND_ORDERED 1
554 #ifndef ADA_DEFAULT_SPINDOWN_SHUTDOWN
555 #define ADA_DEFAULT_SPINDOWN_SHUTDOWN 1
558 #ifndef ADA_DEFAULT_SPINDOWN_SUSPEND
559 #define ADA_DEFAULT_SPINDOWN_SUSPEND 1
562 #ifndef ADA_DEFAULT_READ_AHEAD
563 #define ADA_DEFAULT_READ_AHEAD 1
566 #ifndef ADA_DEFAULT_WRITE_CACHE
567 #define ADA_DEFAULT_WRITE_CACHE 1
570 #define ADA_RA (softc->read_ahead >= 0 ? \
571 softc->read_ahead : ada_read_ahead)
572 #define ADA_WC (softc->write_cache >= 0 ? \
573 softc->write_cache : ada_write_cache)
574 #define ADA_SIO (softc->sort_io_queue >= 0 ? \
575 softc->sort_io_queue : cam_sort_io_queues)
578 * Most platforms map firmware geometry to actual, but some don't. If
579 * not overridden, default to nothing.
581 #ifndef ata_disk_firmware_geom_adjust
582 #define ata_disk_firmware_geom_adjust(disk)
585 static int ada_legacy_aliases = ADA_DEFAULT_LEGACY_ALIASES;
586 static int ada_retry_count = ADA_DEFAULT_RETRY;
587 static int ada_default_timeout = ADA_DEFAULT_TIMEOUT;
588 static int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED;
589 static int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN;
590 static int ada_spindown_suspend = ADA_DEFAULT_SPINDOWN_SUSPEND;
591 static int ada_read_ahead = ADA_DEFAULT_READ_AHEAD;
592 static int ada_write_cache = ADA_DEFAULT_WRITE_CACHE;
594 static SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD, 0,
595 "CAM Direct Access Disk driver");
596 SYSCTL_INT(_kern_cam_ada, OID_AUTO, legacy_aliases, CTLFLAG_RW,
597 &ada_legacy_aliases, 0, "Create legacy-like device aliases");
598 TUNABLE_INT("kern.cam.ada.legacy_aliases", &ada_legacy_aliases);
599 SYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RW,
600 &ada_retry_count, 0, "Normal I/O retry count");
601 TUNABLE_INT("kern.cam.ada.retry_count", &ada_retry_count);
602 SYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RW,
603 &ada_default_timeout, 0, "Normal I/O timeout (in seconds)");
604 TUNABLE_INT("kern.cam.ada.default_timeout", &ada_default_timeout);
605 SYSCTL_INT(_kern_cam_ada, OID_AUTO, send_ordered, CTLFLAG_RW,
606 &ada_send_ordered, 0, "Send Ordered Tags");
607 TUNABLE_INT("kern.cam.ada.send_ordered", &ada_send_ordered);
608 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RW,
609 &ada_spindown_shutdown, 0, "Spin down upon shutdown");
610 TUNABLE_INT("kern.cam.ada.spindown_shutdown", &ada_spindown_shutdown);
611 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_suspend, CTLFLAG_RW,
612 &ada_spindown_suspend, 0, "Spin down upon suspend");
613 TUNABLE_INT("kern.cam.ada.spindown_suspend", &ada_spindown_suspend);
614 SYSCTL_INT(_kern_cam_ada, OID_AUTO, read_ahead, CTLFLAG_RW,
615 &ada_read_ahead, 0, "Enable disk read-ahead");
616 TUNABLE_INT("kern.cam.ada.read_ahead", &ada_read_ahead);
617 SYSCTL_INT(_kern_cam_ada, OID_AUTO, write_cache, CTLFLAG_RW,
618 &ada_write_cache, 0, "Enable disk write cache");
619 TUNABLE_INT("kern.cam.ada.write_cache", &ada_write_cache);
622 * ADA_ORDEREDTAG_INTERVAL determines how often, relative
623 * to the default timeout, we check to see whether an ordered
624 * tagged transaction is appropriate to prevent simple tag
625 * starvation. Since we'd like to ensure that there is at least
626 * 1/2 of the timeout length left for a starved transaction to
627 * complete after we've sent an ordered tag, we must poll at least
628 * four times in every timeout period. This takes care of the worst
629 * case where a starved transaction starts during an interval that
630 * meets the requirement "don't send an ordered tag" test so it takes
631 * us two intervals to determine that a tag must be sent.
633 #ifndef ADA_ORDEREDTAG_INTERVAL
634 #define ADA_ORDEREDTAG_INTERVAL 4
637 static struct periph_driver adadriver =
640 TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0
643 PERIPHDRIVER_DECLARE(ada, adadriver);
646 adaopen(struct disk *dp)
648 struct cam_periph *periph;
649 struct ada_softc *softc;
652 periph = (struct cam_periph *)dp->d_drv1;
653 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
657 cam_periph_lock(periph);
658 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
659 cam_periph_unlock(periph);
660 cam_periph_release(periph);
664 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
667 softc = (struct ada_softc *)periph->softc;
668 softc->flags |= ADA_FLAG_OPEN;
670 cam_periph_unhold(periph);
671 cam_periph_unlock(periph);
676 adaclose(struct disk *dp)
678 struct cam_periph *periph;
679 struct ada_softc *softc;
683 periph = (struct cam_periph *)dp->d_drv1;
684 softc = (struct ada_softc *)periph->softc;
685 cam_periph_lock(periph);
687 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
690 /* We only sync the cache if the drive is capable of it. */
691 if ((softc->flags & ADA_FLAG_DIRTY) != 0 &&
692 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) != 0 &&
693 (periph->flags & CAM_PERIPH_INVALID) == 0 &&
694 cam_periph_hold(periph, PRIBIO) == 0) {
696 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
697 cam_fill_ataio(&ccb->ataio,
704 ada_default_timeout*1000);
706 if (softc->flags & ADA_FLAG_CAN_48BIT)
707 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
709 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
710 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
711 /*sense_flags*/0, softc->disk->d_devstat);
714 xpt_print(periph->path, "Synchronize cache failed\n");
716 softc->flags &= ~ADA_FLAG_DIRTY;
717 xpt_release_ccb(ccb);
718 cam_periph_unhold(periph);
721 softc->flags &= ~ADA_FLAG_OPEN;
723 while (softc->refcount != 0)
724 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "adaclose", 1);
725 cam_periph_unlock(periph);
726 cam_periph_release(periph);
731 adaschedule(struct cam_periph *periph)
733 struct ada_softc *softc = (struct ada_softc *)periph->softc;
735 if (softc->state != ADA_STATE_NORMAL)
738 /* Check if we have more work to do. */
739 if (bioq_first(&softc->bio_queue) ||
740 (!softc->trim_running && bioq_first(&softc->trim_queue))) {
741 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
746 * Actually translate the requested transfer into one the physical driver
747 * can understand. The transfer is described by a buf and will include
748 * only one physical transfer.
751 adastrategy(struct bio *bp)
753 struct cam_periph *periph;
754 struct ada_softc *softc;
756 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
757 softc = (struct ada_softc *)periph->softc;
759 cam_periph_lock(periph);
761 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastrategy(%p)\n", bp));
764 * If the device has been made invalid, error out
766 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
767 cam_periph_unlock(periph);
768 biofinish(bp, NULL, ENXIO);
773 * Place it in the queue of disk activities for this disk
775 if (bp->bio_cmd == BIO_DELETE) {
776 bioq_disksort(&softc->trim_queue, bp);
779 bioq_disksort(&softc->bio_queue, bp);
781 bioq_insert_tail(&softc->bio_queue, bp);
785 * Schedule ourselves for performing the work.
788 cam_periph_unlock(periph);
794 adadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
796 struct cam_periph *periph;
797 struct ada_softc *softc;
807 softc = (struct ada_softc *)periph->softc;
808 cam_periph_lock(periph);
809 secsize = softc->params.secsize;
810 lba = offset / secsize;
811 count = length / secsize;
813 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
814 cam_periph_unlock(periph);
819 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
820 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
821 cam_fill_ataio(&ccb.ataio,
826 (u_int8_t *) virtual,
828 ada_default_timeout*1000);
829 if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
830 (lba + count >= ATA_MAX_28BIT_LBA ||
832 ata_48bit_cmd(&ccb.ataio, ATA_WRITE_DMA48,
835 ata_28bit_cmd(&ccb.ataio, ATA_WRITE_DMA,
838 xpt_polled_action(&ccb);
840 error = cam_periph_error(&ccb,
841 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
842 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
843 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
844 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
846 printf("Aborting dump due to I/O error.\n");
848 cam_periph_unlock(periph);
852 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) {
853 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
855 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
856 cam_fill_ataio(&ccb.ataio,
863 ada_default_timeout*1000);
865 if (softc->flags & ADA_FLAG_CAN_48BIT)
866 ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0);
868 ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0);
869 xpt_polled_action(&ccb);
871 error = cam_periph_error(&ccb,
872 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
873 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
874 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
875 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
877 xpt_print(periph->path, "Synchronize cache failed\n");
879 cam_periph_unlock(periph);
889 * Install a global async callback. This callback will
890 * receive async callbacks like "new device found".
892 status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL);
894 if (status != CAM_REQ_CMP) {
895 printf("ada: Failed to attach master async callback "
896 "due to status 0x%x!\n", status);
897 } else if (ada_send_ordered) {
899 /* Register our event handlers */
900 if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend,
901 NULL, EVENTHANDLER_PRI_LAST)) == NULL)
902 printf("adainit: power event registration failed!\n");
903 if ((EVENTHANDLER_REGISTER(power_resume, adaresume,
904 NULL, EVENTHANDLER_PRI_LAST)) == NULL)
905 printf("adainit: power event registration failed!\n");
906 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown,
907 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
908 printf("adainit: shutdown event registration failed!\n");
913 * Callback from GEOM, called when it has finished cleaning up its
917 adadiskgonecb(struct disk *dp)
919 struct cam_periph *periph;
921 periph = (struct cam_periph *)dp->d_drv1;
923 cam_periph_release(periph);
927 adaoninvalidate(struct cam_periph *periph)
929 struct ada_softc *softc;
931 softc = (struct ada_softc *)periph->softc;
934 * De-register any async callbacks.
936 xpt_register_async(0, adaasync, periph, periph->path);
939 * Return all queued I/O with ENXIO.
940 * XXX Handle any transactions queued to the card
941 * with XPT_ABORT_CCB.
943 bioq_flush(&softc->bio_queue, NULL, ENXIO);
944 bioq_flush(&softc->trim_queue, NULL, ENXIO);
946 disk_gone(softc->disk);
950 adacleanup(struct cam_periph *periph)
952 struct ada_softc *softc;
954 softc = (struct ada_softc *)periph->softc;
956 cam_periph_unlock(periph);
959 * If we can't free the sysctl tree, oh well...
961 if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0
962 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
963 xpt_print(periph->path, "can't remove sysctl context\n");
966 disk_destroy(softc->disk);
967 callout_drain(&softc->sendordered_c);
968 free(softc, M_DEVBUF);
969 cam_periph_lock(periph);
973 adaasync(void *callback_arg, u_int32_t code,
974 struct cam_path *path, void *arg)
976 struct ccb_getdev cgd;
977 struct cam_periph *periph;
978 struct ada_softc *softc;
980 periph = (struct cam_periph *)callback_arg;
982 case AC_FOUND_DEVICE:
984 struct ccb_getdev *cgd;
987 cgd = (struct ccb_getdev *)arg;
991 if (cgd->protocol != PROTO_ATA)
995 * Allocate a peripheral instance for
996 * this device and start the probe
999 status = cam_periph_alloc(adaregister, adaoninvalidate,
1000 adacleanup, adastart,
1001 "ada", CAM_PERIPH_BIO,
1003 AC_FOUND_DEVICE, cgd);
1005 if (status != CAM_REQ_CMP
1006 && status != CAM_REQ_INPROG)
1007 printf("adaasync: Unable to attach to new device "
1008 "due to status 0x%x\n", status);
1011 case AC_GETDEV_CHANGED:
1013 softc = (struct ada_softc *)periph->softc;
1014 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1015 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1016 xpt_action((union ccb *)&cgd);
1018 if ((cgd.ident_data.capabilities1 & ATA_SUPPORT_DMA) &&
1019 (cgd.inq_flags & SID_DMA))
1020 softc->flags |= ADA_FLAG_CAN_DMA;
1022 softc->flags &= ~ADA_FLAG_CAN_DMA;
1023 if (cgd.ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) {
1024 softc->flags |= ADA_FLAG_CAN_48BIT;
1025 if (cgd.inq_flags & SID_DMA48)
1026 softc->flags |= ADA_FLAG_CAN_DMA48;
1028 softc->flags &= ~ADA_FLAG_CAN_DMA48;
1030 softc->flags &= ~(ADA_FLAG_CAN_48BIT |
1031 ADA_FLAG_CAN_DMA48);
1032 if ((cgd.ident_data.satacapabilities & ATA_SUPPORT_NCQ) &&
1033 (cgd.inq_flags & SID_DMA) && (cgd.inq_flags & SID_CmdQue))
1034 softc->flags |= ADA_FLAG_CAN_NCQ;
1036 softc->flags &= ~ADA_FLAG_CAN_NCQ;
1037 if ((cgd.ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) &&
1038 (cgd.inq_flags & SID_DMA))
1039 softc->flags |= ADA_FLAG_CAN_TRIM;
1041 softc->flags &= ~ADA_FLAG_CAN_TRIM;
1043 cam_periph_async(periph, code, path, arg);
1046 case AC_ADVINFO_CHANGED:
1050 buftype = (uintptr_t)arg;
1051 if (buftype == CDAI_TYPE_PHYS_PATH) {
1052 struct ada_softc *softc;
1054 softc = periph->softc;
1055 disk_attr_changed(softc->disk, "GEOM::physpath",
1063 softc = (struct ada_softc *)periph->softc;
1064 cam_periph_async(periph, code, path, arg);
1065 if (softc->state != ADA_STATE_NORMAL)
1067 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1068 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1069 xpt_action((union ccb *)&cgd);
1071 cgd.ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD)
1072 softc->state = ADA_STATE_RAHEAD;
1073 else if (ADA_WC >= 0 &&
1074 cgd.ident_data.support.command1 & ATA_SUPPORT_WRITECACHE)
1075 softc->state = ADA_STATE_WCACHE;
1078 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1079 softc->state = ADA_STATE_NORMAL;
1081 xpt_schedule(periph, CAM_PRIORITY_DEV);
1084 cam_periph_async(periph, code, path, arg);
1090 adasysctlinit(void *context, int pending)
1092 struct cam_periph *periph;
1093 struct ada_softc *softc;
1094 char tmpstr[80], tmpstr2[80];
1096 periph = (struct cam_periph *)context;
1098 /* periph was held for us when this task was enqueued */
1099 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
1100 cam_periph_release(periph);
1104 softc = (struct ada_softc *)periph->softc;
1105 snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d", periph->unit_number);
1106 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1108 sysctl_ctx_init(&softc->sysctl_ctx);
1109 softc->flags |= ADA_FLAG_SCTX_INIT;
1110 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1111 SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2,
1112 CTLFLAG_RD, 0, tmpstr);
1113 if (softc->sysctl_tree == NULL) {
1114 printf("adasysctlinit: unable to allocate sysctl tree\n");
1115 cam_periph_release(periph);
1119 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1120 OID_AUTO, "read_ahead", CTLFLAG_RW | CTLFLAG_MPSAFE,
1121 &softc->read_ahead, 0, "Enable disk read ahead.");
1122 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1123 OID_AUTO, "write_cache", CTLFLAG_RW | CTLFLAG_MPSAFE,
1124 &softc->write_cache, 0, "Enable disk write cache.");
1125 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1126 OID_AUTO, "sort_io_queue", CTLFLAG_RW | CTLFLAG_MPSAFE,
1127 &softc->sort_io_queue, 0,
1128 "Sort IO queue to try and optimise disk access patterns");
1129 #ifdef ADA_TEST_FAILURE
1131 * Add a 'door bell' sysctl which allows one to set it from userland
1132 * and cause something bad to happen. For the moment, we only allow
1133 * whacking the next read or write.
1135 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1136 OID_AUTO, "force_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1137 &softc->force_read_error, 0,
1138 "Force a read error for the next N reads.");
1139 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1140 OID_AUTO, "force_write_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1141 &softc->force_write_error, 0,
1142 "Force a write error for the next N writes.");
1143 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1144 OID_AUTO, "periodic_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1145 &softc->periodic_read_error, 0,
1146 "Force a read error every N reads (don't set too low).");
1148 cam_periph_release(periph);
1152 adagetattr(struct bio *bp)
1155 struct cam_periph *periph;
1157 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1158 cam_periph_lock(periph);
1159 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1161 cam_periph_unlock(periph);
1163 bp->bio_completed = bp->bio_length;
1168 adaregister(struct cam_periph *periph, void *arg)
1170 struct ada_softc *softc;
1171 struct ccb_pathinq cpi;
1172 struct ccb_getdev *cgd;
1173 char announce_buf[80], buf1[32];
1174 struct disk_params *dp;
1177 int legacy_id, quirks;
1179 cgd = (struct ccb_getdev *)arg;
1181 printf("adaregister: no getdev CCB, can't register device\n");
1182 return(CAM_REQ_CMP_ERR);
1185 softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF,
1188 if (softc == NULL) {
1189 printf("adaregister: Unable to probe new device. "
1190 "Unable to allocate softc\n");
1191 return(CAM_REQ_CMP_ERR);
1194 bioq_init(&softc->bio_queue);
1195 bioq_init(&softc->trim_queue);
1197 if ((cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA) &&
1198 (cgd->inq_flags & SID_DMA))
1199 softc->flags |= ADA_FLAG_CAN_DMA;
1200 if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) {
1201 softc->flags |= ADA_FLAG_CAN_48BIT;
1202 if (cgd->inq_flags & SID_DMA48)
1203 softc->flags |= ADA_FLAG_CAN_DMA48;
1205 if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE)
1206 softc->flags |= ADA_FLAG_CAN_FLUSHCACHE;
1207 if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT)
1208 softc->flags |= ADA_FLAG_CAN_POWERMGT;
1209 if ((cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ) &&
1210 (cgd->inq_flags & SID_DMA) && (cgd->inq_flags & SID_CmdQue))
1211 softc->flags |= ADA_FLAG_CAN_NCQ;
1212 if ((cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) &&
1213 (cgd->inq_flags & SID_DMA)) {
1214 softc->flags |= ADA_FLAG_CAN_TRIM;
1215 softc->trim_max_ranges = TRIM_MAX_RANGES;
1216 if (cgd->ident_data.max_dsm_blocks != 0) {
1217 softc->trim_max_ranges =
1218 min(cgd->ident_data.max_dsm_blocks *
1219 ATA_DSM_BLK_RANGES, softc->trim_max_ranges);
1222 if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA)
1223 softc->flags |= ADA_FLAG_CAN_CFA;
1225 periph->softc = softc;
1228 * See if this device has any quirks.
1230 match = cam_quirkmatch((caddr_t)&cgd->ident_data,
1231 (caddr_t)ada_quirk_table,
1232 sizeof(ada_quirk_table)/sizeof(*ada_quirk_table),
1233 sizeof(*ada_quirk_table), ata_identify_match);
1235 softc->quirks = ((struct ada_quirk_entry *)match)->quirks;
1237 softc->quirks = ADA_Q_NONE;
1239 bzero(&cpi, sizeof(cpi));
1240 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE);
1241 cpi.ccb_h.func_code = XPT_PATH_INQ;
1242 xpt_action((union ccb *)&cpi);
1244 TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph);
1247 * Register this media as a disk
1249 (void)cam_periph_hold(periph, PRIBIO);
1250 cam_periph_unlock(periph);
1251 snprintf(announce_buf, sizeof(announce_buf),
1252 "kern.cam.ada.%d.quirks", periph->unit_number);
1253 quirks = softc->quirks;
1254 TUNABLE_INT_FETCH(announce_buf, &quirks);
1255 softc->quirks = quirks;
1256 softc->read_ahead = -1;
1257 snprintf(announce_buf, sizeof(announce_buf),
1258 "kern.cam.ada.%d.read_ahead", periph->unit_number);
1259 TUNABLE_INT_FETCH(announce_buf, &softc->read_ahead);
1260 softc->write_cache = -1;
1261 snprintf(announce_buf, sizeof(announce_buf),
1262 "kern.cam.ada.%d.write_cache", periph->unit_number);
1263 TUNABLE_INT_FETCH(announce_buf, &softc->write_cache);
1264 /* Disable queue sorting for non-rotational media by default. */
1265 if (cgd->ident_data.media_rotation_rate == ATA_RATE_NON_ROTATING)
1266 softc->sort_io_queue = 0;
1268 softc->sort_io_queue = -1;
1269 adagetparams(periph, cgd);
1270 softc->disk = disk_alloc();
1271 softc->disk->d_rotation_rate = cgd->ident_data.media_rotation_rate;
1272 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
1273 periph->unit_number, softc->params.secsize,
1274 DEVSTAT_ALL_SUPPORTED,
1275 DEVSTAT_TYPE_DIRECT |
1276 XPORT_DEVSTAT_TYPE(cpi.transport),
1277 DEVSTAT_PRIORITY_DISK);
1278 softc->disk->d_open = adaopen;
1279 softc->disk->d_close = adaclose;
1280 softc->disk->d_strategy = adastrategy;
1281 softc->disk->d_getattr = adagetattr;
1282 softc->disk->d_dump = adadump;
1283 softc->disk->d_gone = adadiskgonecb;
1284 softc->disk->d_name = "ada";
1285 softc->disk->d_drv1 = periph;
1286 maxio = cpi.maxio; /* Honor max I/O size of SIM */
1288 maxio = DFLTPHYS; /* traditional default */
1289 else if (maxio > MAXPHYS)
1290 maxio = MAXPHYS; /* for safety */
1291 if (softc->flags & ADA_FLAG_CAN_48BIT)
1292 maxio = min(maxio, 65536 * softc->params.secsize);
1293 else /* 28bit ATA command limit */
1294 maxio = min(maxio, 256 * softc->params.secsize);
1295 softc->disk->d_maxsize = maxio;
1296 softc->disk->d_unit = periph->unit_number;
1297 softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION;
1298 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE)
1299 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
1300 if (softc->flags & ADA_FLAG_CAN_TRIM) {
1301 softc->disk->d_flags |= DISKFLAG_CANDELETE;
1302 softc->disk->d_delmaxsize = softc->params.secsize *
1304 softc->trim_max_ranges;
1305 } else if ((softc->flags & ADA_FLAG_CAN_CFA) &&
1306 !(softc->flags & ADA_FLAG_CAN_48BIT)) {
1307 softc->disk->d_flags |= DISKFLAG_CANDELETE;
1308 softc->disk->d_delmaxsize = 256 * softc->params.secsize;
1310 softc->disk->d_delmaxsize = maxio;
1311 if ((cpi.hba_misc & PIM_UNMAPPED) != 0)
1312 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
1313 strlcpy(softc->disk->d_descr, cgd->ident_data.model,
1314 MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model)));
1315 strlcpy(softc->disk->d_ident, cgd->ident_data.serial,
1316 MIN(sizeof(softc->disk->d_ident), sizeof(cgd->ident_data.serial)));
1317 softc->disk->d_hba_vendor = cpi.hba_vendor;
1318 softc->disk->d_hba_device = cpi.hba_device;
1319 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
1320 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
1322 softc->disk->d_sectorsize = softc->params.secsize;
1323 softc->disk->d_mediasize = (off_t)softc->params.sectors *
1324 softc->params.secsize;
1325 if (ata_physical_sector_size(&cgd->ident_data) !=
1326 softc->params.secsize) {
1327 softc->disk->d_stripesize =
1328 ata_physical_sector_size(&cgd->ident_data);
1329 softc->disk->d_stripeoffset = (softc->disk->d_stripesize -
1330 ata_logical_sector_offset(&cgd->ident_data)) %
1331 softc->disk->d_stripesize;
1332 } else if (softc->quirks & ADA_Q_4K) {
1333 softc->disk->d_stripesize = 4096;
1334 softc->disk->d_stripeoffset = 0;
1336 softc->disk->d_fwsectors = softc->params.secs_per_track;
1337 softc->disk->d_fwheads = softc->params.heads;
1338 ata_disk_firmware_geom_adjust(softc->disk);
1340 if (ada_legacy_aliases) {
1341 #ifdef ATA_STATIC_ID
1342 legacy_id = xpt_path_legacy_ata_id(periph->path);
1344 legacy_id = softc->disk->d_unit;
1346 if (legacy_id >= 0) {
1347 snprintf(announce_buf, sizeof(announce_buf),
1348 "kern.devalias.%s%d",
1349 softc->disk->d_name, softc->disk->d_unit);
1350 snprintf(buf1, sizeof(buf1),
1352 setenv(announce_buf, buf1);
1357 * Acquire a reference to the periph before we register with GEOM.
1358 * We'll release this reference once GEOM calls us back (via
1359 * adadiskgonecb()) telling us that our provider has been freed.
1361 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1362 xpt_print(periph->path, "%s: lost periph during "
1363 "registration!\n", __func__);
1364 cam_periph_lock(periph);
1365 return (CAM_REQ_CMP_ERR);
1367 disk_create(softc->disk, DISK_VERSION);
1368 cam_periph_lock(periph);
1369 cam_periph_unhold(periph);
1371 dp = &softc->params;
1372 snprintf(announce_buf, sizeof(announce_buf),
1373 "%juMB (%ju %u byte sectors)",
1374 ((uintmax_t)dp->secsize * dp->sectors) / (1024 * 1024),
1375 (uintmax_t)dp->sectors, dp->secsize);
1376 xpt_announce_periph(periph, announce_buf);
1377 xpt_announce_quirks(periph, softc->quirks, ADA_Q_BIT_STRING);
1379 printf("%s%d: Previously was known as ad%d\n",
1380 periph->periph_name, periph->unit_number, legacy_id);
1383 * Create our sysctl variables, now that we know
1384 * we have successfully attached.
1386 if (cam_periph_acquire(periph) == CAM_REQ_CMP)
1387 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task);
1390 * Add async callbacks for bus reset and
1391 * bus device reset calls. I don't bother
1392 * checking if this fails as, in most cases,
1393 * the system will function just fine without
1394 * them and the only alternative would be to
1395 * not attach the device on failure.
1397 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
1398 AC_GETDEV_CHANGED | AC_ADVINFO_CHANGED,
1399 adaasync, periph, periph->path);
1402 * Schedule a periodic event to occasionally send an
1403 * ordered tag to a device.
1405 callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0);
1406 callout_reset(&softc->sendordered_c,
1407 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL,
1408 adasendorderedtag, softc);
1411 cgd->ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD) {
1412 softc->state = ADA_STATE_RAHEAD;
1413 } else if (ADA_WC >= 0 &&
1414 cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) {
1415 softc->state = ADA_STATE_WCACHE;
1417 softc->state = ADA_STATE_NORMAL;
1418 return(CAM_REQ_CMP);
1420 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1421 softc->state = ADA_STATE_NORMAL;
1423 xpt_schedule(periph, CAM_PRIORITY_DEV);
1424 return(CAM_REQ_CMP);
1428 ada_dsmtrim(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio)
1430 struct trim_request *req = &softc->trim_req;
1431 uint64_t lastlba = (uint64_t)-1;
1432 int c, lastcount = 0, off, ranges = 0;
1434 bzero(req, sizeof(*req));
1435 TAILQ_INIT(&req->bps);
1437 uint64_t lba = bp->bio_pblkno;
1438 int count = bp->bio_bcount / softc->params.secsize;
1440 bioq_remove(&softc->trim_queue, bp);
1442 /* Try to extend the previous range. */
1443 if (lba == lastlba) {
1444 c = min(count, ATA_DSM_RANGE_MAX - lastcount);
1446 off = (ranges - 1) * ATA_DSM_RANGE_SIZE;
1447 req->data[off + 6] = lastcount & 0xff;
1448 req->data[off + 7] =
1449 (lastcount >> 8) & 0xff;
1455 c = min(count, ATA_DSM_RANGE_MAX);
1456 off = ranges * ATA_DSM_RANGE_SIZE;
1457 req->data[off + 0] = lba & 0xff;
1458 req->data[off + 1] = (lba >> 8) & 0xff;
1459 req->data[off + 2] = (lba >> 16) & 0xff;
1460 req->data[off + 3] = (lba >> 24) & 0xff;
1461 req->data[off + 4] = (lba >> 32) & 0xff;
1462 req->data[off + 5] = (lba >> 40) & 0xff;
1463 req->data[off + 6] = c & 0xff;
1464 req->data[off + 7] = (c >> 8) & 0xff;
1470 * Its the caller's responsibility to ensure the
1471 * request will fit so we don't need to check for
1476 TAILQ_INSERT_TAIL(&req->bps, bp, bio_queue);
1477 bp = bioq_first(&softc->trim_queue);
1479 bp->bio_bcount / softc->params.secsize >
1480 (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX)
1483 cam_fill_ataio(ataio,
1489 ((ranges + ATA_DSM_BLK_RANGES - 1) /
1490 ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE,
1491 ada_default_timeout * 1000);
1492 ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT,
1493 ATA_DSM_TRIM, 0, (ranges + ATA_DSM_BLK_RANGES -
1494 1) / ATA_DSM_BLK_RANGES);
1498 ada_cfaerase(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio)
1500 struct trim_request *req = &softc->trim_req;
1501 uint64_t lba = bp->bio_pblkno;
1502 uint16_t count = bp->bio_bcount / softc->params.secsize;
1504 bzero(req, sizeof(*req));
1505 TAILQ_INIT(&req->bps);
1506 bioq_remove(&softc->trim_queue, bp);
1507 TAILQ_INSERT_TAIL(&req->bps, bp, bio_queue);
1509 cam_fill_ataio(ataio,
1516 ada_default_timeout*1000);
1520 ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count);
1524 adastart(struct cam_periph *periph, union ccb *start_ccb)
1526 struct ada_softc *softc = (struct ada_softc *)periph->softc;
1527 struct ccb_ataio *ataio = &start_ccb->ataio;
1529 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastart\n"));
1531 switch (softc->state) {
1532 case ADA_STATE_NORMAL:
1537 /* Run TRIM if not running yet. */
1538 if (!softc->trim_running &&
1539 (bp = bioq_first(&softc->trim_queue)) != 0) {
1540 if (softc->flags & ADA_FLAG_CAN_TRIM) {
1541 ada_dsmtrim(softc, bp, ataio);
1542 } else if ((softc->flags & ADA_FLAG_CAN_CFA) &&
1543 !(softc->flags & ADA_FLAG_CAN_48BIT)) {
1544 ada_cfaerase(softc, bp, ataio);
1546 /* This can happen if DMA was disabled. */
1547 bioq_remove(&softc->trim_queue, bp);
1548 biofinish(bp, NULL, EOPNOTSUPP);
1549 xpt_release_ccb(start_ccb);
1550 adaschedule(periph);
1553 softc->trim_running = 1;
1554 start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM;
1555 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
1558 /* Run regular command. */
1559 bp = bioq_first(&softc->bio_queue);
1561 xpt_release_ccb(start_ccb);
1564 bioq_remove(&softc->bio_queue, bp);
1566 if ((bp->bio_flags & BIO_ORDERED) != 0
1567 || (softc->flags & ADA_FLAG_NEED_OTAG) != 0) {
1568 softc->flags &= ~ADA_FLAG_NEED_OTAG;
1569 softc->flags |= ADA_FLAG_WAS_OTAG;
1574 switch (bp->bio_cmd) {
1578 uint64_t lba = bp->bio_pblkno;
1579 uint16_t count = bp->bio_bcount / softc->params.secsize;
1583 if (bp->bio_cmd == BIO_WRITE) {
1584 softc->flags |= ADA_FLAG_DIRTY;
1585 rw_op = CAM_DIR_OUT;
1590 data_ptr = bp->bio_data;
1591 if ((bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0) {
1592 rw_op |= CAM_DATA_BIO;
1596 #ifdef ADA_TEST_FAILURE
1600 * Support the failure ioctls. If the command is a
1601 * read, and there are pending forced read errors, or
1602 * if a write and pending write errors, then fail this
1603 * operation with EIO. This is useful for testing
1604 * purposes. Also, support having every Nth read fail.
1606 * This is a rather blunt tool.
1608 if (bp->bio_cmd == BIO_READ) {
1609 if (softc->force_read_error) {
1610 softc->force_read_error--;
1613 if (softc->periodic_read_error > 0) {
1614 if (++softc->periodic_read_count >=
1615 softc->periodic_read_error) {
1616 softc->periodic_read_count = 0;
1621 if (softc->force_write_error) {
1622 softc->force_write_error--;
1627 biofinish(bp, NULL, EIO);
1628 xpt_release_ccb(start_ccb);
1629 adaschedule(periph);
1633 KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 ||
1634 round_page(bp->bio_bcount + bp->bio_ma_offset) /
1635 PAGE_SIZE == bp->bio_ma_n,
1636 ("Short bio %p", bp));
1637 cam_fill_ataio(ataio,
1644 ada_default_timeout*1000);
1646 if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) {
1647 if (bp->bio_cmd == BIO_READ) {
1648 ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED,
1651 ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED,
1654 } else if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
1655 (lba + count >= ATA_MAX_28BIT_LBA ||
1657 if (softc->flags & ADA_FLAG_CAN_DMA48) {
1658 if (bp->bio_cmd == BIO_READ) {
1659 ata_48bit_cmd(ataio, ATA_READ_DMA48,
1662 ata_48bit_cmd(ataio, ATA_WRITE_DMA48,
1666 if (bp->bio_cmd == BIO_READ) {
1667 ata_48bit_cmd(ataio, ATA_READ_MUL48,
1670 ata_48bit_cmd(ataio, ATA_WRITE_MUL48,
1677 if (softc->flags & ADA_FLAG_CAN_DMA) {
1678 if (bp->bio_cmd == BIO_READ) {
1679 ata_28bit_cmd(ataio, ATA_READ_DMA,
1682 ata_28bit_cmd(ataio, ATA_WRITE_DMA,
1686 if (bp->bio_cmd == BIO_READ) {
1687 ata_28bit_cmd(ataio, ATA_READ_MUL,
1690 ata_28bit_cmd(ataio, ATA_WRITE_MUL,
1698 cam_fill_ataio(ataio,
1705 ada_default_timeout*1000);
1707 if (softc->flags & ADA_FLAG_CAN_48BIT)
1708 ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1710 ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0);
1713 start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO;
1714 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
1716 start_ccb->ccb_h.ccb_bp = bp;
1717 softc->outstanding_cmds++;
1719 cam_periph_unlock(periph);
1720 xpt_action(start_ccb);
1721 cam_periph_lock(periph);
1724 /* May have more work to do, so ensure we stay scheduled */
1725 adaschedule(periph);
1728 case ADA_STATE_RAHEAD:
1729 case ADA_STATE_WCACHE:
1731 cam_fill_ataio(ataio,
1738 ada_default_timeout*1000);
1740 if (softc->state == ADA_STATE_RAHEAD) {
1741 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_RA ?
1742 ATA_SF_ENAB_RCACHE : ATA_SF_DIS_RCACHE, 0, 0);
1743 start_ccb->ccb_h.ccb_state = ADA_CCB_RAHEAD;
1745 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_WC ?
1746 ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0);
1747 start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE;
1749 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1750 xpt_action(start_ccb);
1757 adadone(struct cam_periph *periph, union ccb *done_ccb)
1759 struct ada_softc *softc;
1760 struct ccb_ataio *ataio;
1761 struct ccb_getdev *cgd;
1762 struct cam_path *path;
1765 softc = (struct ada_softc *)periph->softc;
1766 ataio = &done_ccb->ataio;
1767 path = done_ccb->ccb_h.path;
1769 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("adadone\n"));
1771 state = ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK;
1773 case ADA_CCB_BUFFER_IO:
1779 cam_periph_lock(periph);
1780 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1781 error = adaerror(done_ccb, 0, 0);
1782 if (error == ERESTART) {
1783 /* A retry was scheduled, so just return. */
1784 cam_periph_unlock(periph);
1787 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1788 cam_release_devq(path,
1792 /*getcount_only*/0);
1794 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1795 panic("REQ_CMP with QFRZN");
1798 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1799 bp->bio_error = error;
1801 bp->bio_resid = bp->bio_bcount;
1802 bp->bio_flags |= BIO_ERROR;
1804 if (state == ADA_CCB_TRIM)
1807 bp->bio_resid = ataio->resid;
1808 if (bp->bio_resid > 0)
1809 bp->bio_flags |= BIO_ERROR;
1811 softc->outstanding_cmds--;
1812 if (softc->outstanding_cmds == 0)
1813 softc->flags |= ADA_FLAG_WAS_OTAG;
1814 xpt_release_ccb(done_ccb);
1815 if (state == ADA_CCB_TRIM) {
1816 TAILQ_HEAD(, bio) queue;
1820 TAILQ_CONCAT(&queue, &softc->trim_req.bps, bio_queue);
1822 * Normally, the xpt_release_ccb() above would make sure
1823 * that when we have more work to do, that work would
1824 * get kicked off. However, we specifically keep
1825 * trim_running set to 0 before the call above to allow
1826 * other I/O to progress when many BIO_DELETE requests
1827 * are pushed down. We set trim_running to 0 and call
1828 * daschedule again so that we don't stall if there are
1829 * no other I/Os pending apart from BIO_DELETEs.
1831 softc->trim_running = 0;
1832 adaschedule(periph);
1833 cam_periph_unlock(periph);
1834 while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
1835 TAILQ_REMOVE(&queue, bp1, bio_queue);
1836 bp1->bio_error = error;
1838 bp1->bio_flags |= BIO_ERROR;
1839 bp1->bio_resid = bp1->bio_bcount;
1845 cam_periph_unlock(periph);
1850 case ADA_CCB_RAHEAD:
1852 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1853 if (adaerror(done_ccb, 0, 0) == ERESTART) {
1855 /* Drop freeze taken due to CAM_DEV_QFREEZE */
1856 cam_release_devq(path, 0, 0, 0, FALSE);
1858 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1859 cam_release_devq(path,
1863 /*getcount_only*/0);
1868 * Since our peripheral may be invalidated by an error
1869 * above or an external event, we must release our CCB
1870 * before releasing the reference on the peripheral.
1871 * The peripheral will only go away once the last reference
1872 * is removed, and we need it around for the CCB release
1875 cgd = (struct ccb_getdev *)done_ccb;
1876 xpt_setup_ccb(&cgd->ccb_h, path, CAM_PRIORITY_NORMAL);
1877 cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1878 xpt_action((union ccb *)cgd);
1880 cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) {
1881 softc->state = ADA_STATE_WCACHE;
1882 xpt_release_ccb(done_ccb);
1883 xpt_schedule(periph, CAM_PRIORITY_DEV);
1886 softc->state = ADA_STATE_NORMAL;
1887 xpt_release_ccb(done_ccb);
1888 /* Drop freeze taken due to CAM_DEV_QFREEZE */
1889 cam_release_devq(path, 0, 0, 0, FALSE);
1890 adaschedule(periph);
1891 cam_periph_release_locked(periph);
1894 case ADA_CCB_WCACHE:
1896 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1897 if (adaerror(done_ccb, 0, 0) == ERESTART) {
1899 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1900 cam_release_devq(path,
1904 /*getcount_only*/0);
1908 softc->state = ADA_STATE_NORMAL;
1910 * Since our peripheral may be invalidated by an error
1911 * above or an external event, we must release our CCB
1912 * before releasing the reference on the peripheral.
1913 * The peripheral will only go away once the last reference
1914 * is removed, and we need it around for the CCB release
1917 xpt_release_ccb(done_ccb);
1918 /* Drop freeze taken due to CAM_DEV_QFREEZE */
1919 cam_release_devq(path, 0, 0, 0, FALSE);
1920 adaschedule(periph);
1921 cam_periph_release_locked(periph);
1925 /* No-op. We're polling */
1930 xpt_release_ccb(done_ccb);
1934 adaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1937 return(cam_periph_error(ccb, cam_flags, sense_flags, NULL));
1941 adagetparams(struct cam_periph *periph, struct ccb_getdev *cgd)
1943 struct ada_softc *softc = (struct ada_softc *)periph->softc;
1944 struct disk_params *dp = &softc->params;
1945 u_int64_t lbasize48;
1948 dp->secsize = ata_logical_sector_size(&cgd->ident_data);
1949 if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) &&
1950 cgd->ident_data.current_heads && cgd->ident_data.current_sectors) {
1951 dp->heads = cgd->ident_data.current_heads;
1952 dp->secs_per_track = cgd->ident_data.current_sectors;
1953 dp->cylinders = cgd->ident_data.cylinders;
1954 dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 |
1955 ((u_int32_t)cgd->ident_data.current_size_2 << 16);
1957 dp->heads = cgd->ident_data.heads;
1958 dp->secs_per_track = cgd->ident_data.sectors;
1959 dp->cylinders = cgd->ident_data.cylinders;
1960 dp->sectors = cgd->ident_data.cylinders * dp->heads * dp->secs_per_track;
1962 lbasize = (u_int32_t)cgd->ident_data.lba_size_1 |
1963 ((u_int32_t)cgd->ident_data.lba_size_2 << 16);
1965 /* use the 28bit LBA size if valid or bigger than the CHS mapping */
1966 if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize)
1967 dp->sectors = lbasize;
1969 /* use the 48bit LBA size if valid */
1970 lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) |
1971 ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) |
1972 ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) |
1973 ((u_int64_t)cgd->ident_data.lba_size48_4 << 48);
1974 if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) &&
1975 lbasize48 > ATA_MAX_28BIT_LBA)
1976 dp->sectors = lbasize48;
1980 adasendorderedtag(void *arg)
1982 struct ada_softc *softc = arg;
1984 if (ada_send_ordered) {
1985 if (softc->outstanding_cmds > 0) {
1986 if ((softc->flags & ADA_FLAG_WAS_OTAG) == 0)
1987 softc->flags |= ADA_FLAG_NEED_OTAG;
1988 softc->flags &= ~ADA_FLAG_WAS_OTAG;
1991 /* Queue us up again */
1992 callout_reset(&softc->sendordered_c,
1993 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL,
1994 adasendorderedtag, softc);
1998 * Step through all ADA peripheral drivers, and if the device is still open,
1999 * sync the disk cache to physical media.
2004 struct cam_periph *periph;
2005 struct ada_softc *softc;
2009 CAM_PERIPH_FOREACH(periph, &adadriver) {
2010 softc = (struct ada_softc *)periph->softc;
2011 if (SCHEDULER_STOPPED()) {
2012 /* If we paniced with the lock held, do not recurse. */
2013 if (!cam_periph_owned(periph) &&
2014 (softc->flags & ADA_FLAG_OPEN)) {
2015 adadump(softc->disk, NULL, 0, 0, 0);
2019 cam_periph_lock(periph);
2021 * We only sync the cache if the drive is still open, and
2022 * if the drive is capable of it..
2024 if (((softc->flags & ADA_FLAG_OPEN) == 0) ||
2025 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) {
2026 cam_periph_unlock(periph);
2030 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2031 cam_fill_ataio(&ccb->ataio,
2038 ada_default_timeout*1000);
2039 if (softc->flags & ADA_FLAG_CAN_48BIT)
2040 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
2042 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
2044 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
2045 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
2046 softc->disk->d_devstat);
2048 xpt_print(periph->path, "Synchronize cache failed\n");
2049 xpt_release_ccb(ccb);
2050 cam_periph_unlock(periph);
2055 adaspindown(uint8_t cmd, int flags)
2057 struct cam_periph *periph;
2058 struct ada_softc *softc;
2062 CAM_PERIPH_FOREACH(periph, &adadriver) {
2063 /* If we paniced with lock held - not recurse here. */
2064 if (cam_periph_owned(periph))
2066 cam_periph_lock(periph);
2067 softc = (struct ada_softc *)periph->softc;
2069 * We only spin-down the drive if it is capable of it..
2071 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
2072 cam_periph_unlock(periph);
2077 xpt_print(periph->path, "spin-down\n");
2079 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2080 cam_fill_ataio(&ccb->ataio,
2083 CAM_DIR_NONE | flags,
2087 ada_default_timeout*1000);
2088 ata_28bit_cmd(&ccb->ataio, cmd, 0, 0, 0);
2090 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
2091 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
2092 softc->disk->d_devstat);
2094 xpt_print(periph->path, "Spin-down disk failed\n");
2095 xpt_release_ccb(ccb);
2096 cam_periph_unlock(periph);
2101 adashutdown(void *arg, int howto)
2105 if (ada_spindown_shutdown != 0 &&
2106 (howto & (RB_HALT | RB_POWEROFF)) != 0)
2107 adaspindown(ATA_STANDBY_IMMEDIATE, 0);
2111 adasuspend(void *arg)
2115 if (ada_spindown_suspend != 0)
2116 adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE);
2120 adaresume(void *arg)
2122 struct cam_periph *periph;
2123 struct ada_softc *softc;
2125 if (ada_spindown_suspend == 0)
2128 CAM_PERIPH_FOREACH(periph, &adadriver) {
2129 cam_periph_lock(periph);
2130 softc = (struct ada_softc *)periph->softc;
2132 * We only spin-down the drive if it is capable of it..
2134 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
2135 cam_periph_unlock(periph);
2140 xpt_print(periph->path, "resume\n");
2143 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on
2146 cam_release_devq(periph->path,
2150 /*getcount_only*/0);
2152 cam_periph_unlock(periph);
2156 #endif /* _KERNEL */