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 Green Advanced Format (4k) drives */
237 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RS*", "*" },
241 /* WDC Caviar Green Advanced Format (4k) drives */
242 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RX*", "*" },
246 /* WDC Caviar Green Advanced Format (4k) drives */
247 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RS*", "*" },
251 /* WDC Caviar Green Advanced Format (4k) drives */
252 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RX*", "*" },
256 /* WDC Scorpio Black Advanced Format (4k) drives */
257 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PKT*", "*" },
261 /* WDC Scorpio Black Advanced Format (4k) drives */
262 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PKT*", "*" },
266 /* WDC Scorpio Blue Advanced Format (4k) drives */
267 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PVT*", "*" },
271 /* WDC Scorpio Blue Advanced Format (4k) drives */
272 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PVT*", "*" },
278 * Corsair Force 2 SSDs
279 * 4k optimised & trim only works in 4k requests + 4k aligned
281 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair CSSD-F*", "*" },
286 * Corsair Force 3 SSDs
287 * 4k optimised & trim only works in 4k requests + 4k aligned
289 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force 3*", "*" },
294 * Corsair Neutron GTX SSDs
295 * 4k optimised & trim only works in 4k requests + 4k aligned
297 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Neutron GTX*", "*" },
302 * Corsair Force GT & GS SSDs
303 * 4k optimised & trim only works in 4k requests + 4k aligned
305 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force G*", "*" },
311 * 4k optimised & trim only works in 4k requests + 4k aligned
313 { T_DIRECT, SIP_MEDIA_FIXED, "*", "M4-CT???M4SSD2*", "*" },
318 * Crucial RealSSD C300 SSDs
321 { T_DIRECT, SIP_MEDIA_FIXED, "*", "C300-CTFDDAC???MAG*",
322 "*" }, /*quirks*/ADA_Q_4K
326 * Intel 320 Series SSDs
327 * 4k optimised & trim only works in 4k requests + 4k aligned
329 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2CW*", "*" },
334 * Intel 330 Series SSDs
335 * 4k optimised & trim only works in 4k requests + 4k aligned
337 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2CT*", "*" },
342 * Intel 510 Series SSDs
343 * 4k optimised & trim only works in 4k requests + 4k aligned
345 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2MH*", "*" },
350 * Intel 520 Series SSDs
351 * 4k optimised & trim only works in 4k requests + 4k aligned
353 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2BW*", "*" },
358 * Intel X25-M Series SSDs
359 * 4k optimised & trim only works in 4k requests + 4k aligned
361 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2M*", "*" },
366 * Kingston E100 Series SSDs
367 * 4k optimised & trim only works in 4k requests + 4k aligned
369 { T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SE100S3*", "*" },
374 * Kingston HyperX 3k SSDs
375 * 4k optimised & trim only works in 4k requests + 4k aligned
377 { T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SH103S3*", "*" },
382 * Marvell SSDs (entry taken from OpenSolaris)
383 * 4k optimised & trim only works in 4k requests + 4k aligned
385 { T_DIRECT, SIP_MEDIA_FIXED, "*", "MARVELL SD88SA02*", "*" },
391 * 4k optimised & trim only works in 4k requests + 4k aligned
393 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY2*", "*" },
399 * 4k optimised & trim only works in 4k requests + 4k aligned
401 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY3*", "*" },
406 * OCZ Deneva R Series SSDs
407 * 4k optimised & trim only works in 4k requests + 4k aligned
409 { T_DIRECT, SIP_MEDIA_FIXED, "*", "DENRSTE251M45*", "*" },
414 * OCZ Vertex 2 SSDs (inc pro series)
415 * 4k optimised & trim only works in 4k requests + 4k aligned
417 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ?VERTEX2*", "*" },
423 * 4k optimised & trim only works in 4k requests + 4k aligned
425 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX3*", "*" },
431 * 4k optimised & trim only works in 4k requests + 4k aligned
433 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX4*", "*" },
438 * Samsung 830 Series SSDs
441 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG SSD 830 Series*", "*" },
449 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Samsung SSD 840*", "*" },
454 * Samsung 843T Series SSDs
457 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG MZ7WD*", "*" },
465 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Samsung SSD 850*", "*" },
470 * Samsung PM853T Series SSDs
473 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG MZ7GE*", "*" },
478 * SuperTalent TeraDrive CT SSDs
479 * 4k optimised & trim only works in 4k requests + 4k aligned
481 { T_DIRECT, SIP_MEDIA_FIXED, "*", "FTM??CT25H*", "*" },
486 * XceedIOPS SATA SSDs
489 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SG9XCS2D*", "*" },
495 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
496 /*vendor*/"*", /*product*/"*", /*revision*/"*"
502 static disk_strategy_t adastrategy;
503 static dumper_t adadump;
504 static periph_init_t adainit;
505 static void adaasync(void *callback_arg, u_int32_t code,
506 struct cam_path *path, void *arg);
507 static void adasysctlinit(void *context, int pending);
508 static periph_ctor_t adaregister;
509 static periph_dtor_t adacleanup;
510 static periph_start_t adastart;
511 static periph_oninv_t adaoninvalidate;
512 static void adadone(struct cam_periph *periph,
513 union ccb *done_ccb);
514 static int adaerror(union ccb *ccb, u_int32_t cam_flags,
515 u_int32_t sense_flags);
516 static void adagetparams(struct cam_periph *periph,
517 struct ccb_getdev *cgd);
518 static timeout_t adasendorderedtag;
519 static void adashutdown(void *arg, int howto);
520 static void adasuspend(void *arg);
521 static void adaresume(void *arg);
523 #ifndef ADA_DEFAULT_LEGACY_ALIASES
524 #define ADA_DEFAULT_LEGACY_ALIASES 1
527 #ifndef ADA_DEFAULT_TIMEOUT
528 #define ADA_DEFAULT_TIMEOUT 30 /* Timeout in seconds */
531 #ifndef ADA_DEFAULT_RETRY
532 #define ADA_DEFAULT_RETRY 4
535 #ifndef ADA_DEFAULT_SEND_ORDERED
536 #define ADA_DEFAULT_SEND_ORDERED 1
539 #ifndef ADA_DEFAULT_SPINDOWN_SHUTDOWN
540 #define ADA_DEFAULT_SPINDOWN_SHUTDOWN 1
543 #ifndef ADA_DEFAULT_SPINDOWN_SUSPEND
544 #define ADA_DEFAULT_SPINDOWN_SUSPEND 1
547 #ifndef ADA_DEFAULT_READ_AHEAD
548 #define ADA_DEFAULT_READ_AHEAD 1
551 #ifndef ADA_DEFAULT_WRITE_CACHE
552 #define ADA_DEFAULT_WRITE_CACHE 1
555 #define ADA_RA (softc->read_ahead >= 0 ? \
556 softc->read_ahead : ada_read_ahead)
557 #define ADA_WC (softc->write_cache >= 0 ? \
558 softc->write_cache : ada_write_cache)
559 #define ADA_SIO (softc->sort_io_queue >= 0 ? \
560 softc->sort_io_queue : cam_sort_io_queues)
563 * Most platforms map firmware geometry to actual, but some don't. If
564 * not overridden, default to nothing.
566 #ifndef ata_disk_firmware_geom_adjust
567 #define ata_disk_firmware_geom_adjust(disk)
570 static int ada_legacy_aliases = ADA_DEFAULT_LEGACY_ALIASES;
571 static int ada_retry_count = ADA_DEFAULT_RETRY;
572 static int ada_default_timeout = ADA_DEFAULT_TIMEOUT;
573 static int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED;
574 static int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN;
575 static int ada_spindown_suspend = ADA_DEFAULT_SPINDOWN_SUSPEND;
576 static int ada_read_ahead = ADA_DEFAULT_READ_AHEAD;
577 static int ada_write_cache = ADA_DEFAULT_WRITE_CACHE;
579 static SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD, 0,
580 "CAM Direct Access Disk driver");
581 SYSCTL_INT(_kern_cam_ada, OID_AUTO, legacy_aliases, CTLFLAG_RWTUN,
582 &ada_legacy_aliases, 0, "Create legacy-like device aliases");
583 SYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RWTUN,
584 &ada_retry_count, 0, "Normal I/O retry count");
585 SYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RWTUN,
586 &ada_default_timeout, 0, "Normal I/O timeout (in seconds)");
587 SYSCTL_INT(_kern_cam_ada, OID_AUTO, send_ordered, CTLFLAG_RWTUN,
588 &ada_send_ordered, 0, "Send Ordered Tags");
589 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RWTUN,
590 &ada_spindown_shutdown, 0, "Spin down upon shutdown");
591 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_suspend, CTLFLAG_RWTUN,
592 &ada_spindown_suspend, 0, "Spin down upon suspend");
593 SYSCTL_INT(_kern_cam_ada, OID_AUTO, read_ahead, CTLFLAG_RWTUN,
594 &ada_read_ahead, 0, "Enable disk read-ahead");
595 SYSCTL_INT(_kern_cam_ada, OID_AUTO, write_cache, CTLFLAG_RWTUN,
596 &ada_write_cache, 0, "Enable disk write cache");
599 * ADA_ORDEREDTAG_INTERVAL determines how often, relative
600 * to the default timeout, we check to see whether an ordered
601 * tagged transaction is appropriate to prevent simple tag
602 * starvation. Since we'd like to ensure that there is at least
603 * 1/2 of the timeout length left for a starved transaction to
604 * complete after we've sent an ordered tag, we must poll at least
605 * four times in every timeout period. This takes care of the worst
606 * case where a starved transaction starts during an interval that
607 * meets the requirement "don't send an ordered tag" test so it takes
608 * us two intervals to determine that a tag must be sent.
610 #ifndef ADA_ORDEREDTAG_INTERVAL
611 #define ADA_ORDEREDTAG_INTERVAL 4
614 static struct periph_driver adadriver =
617 TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0
620 PERIPHDRIVER_DECLARE(ada, adadriver);
622 static MALLOC_DEFINE(M_ATADA, "ata_da", "ata_da buffers");
625 adaopen(struct disk *dp)
627 struct cam_periph *periph;
628 struct ada_softc *softc;
631 periph = (struct cam_periph *)dp->d_drv1;
632 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
636 cam_periph_lock(periph);
637 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
638 cam_periph_unlock(periph);
639 cam_periph_release(periph);
643 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
646 softc = (struct ada_softc *)periph->softc;
647 softc->flags |= ADA_FLAG_OPEN;
649 cam_periph_unhold(periph);
650 cam_periph_unlock(periph);
655 adaclose(struct disk *dp)
657 struct cam_periph *periph;
658 struct ada_softc *softc;
662 periph = (struct cam_periph *)dp->d_drv1;
663 softc = (struct ada_softc *)periph->softc;
664 cam_periph_lock(periph);
666 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
669 /* We only sync the cache if the drive is capable of it. */
670 if ((softc->flags & ADA_FLAG_DIRTY) != 0 &&
671 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) != 0 &&
672 (periph->flags & CAM_PERIPH_INVALID) == 0 &&
673 cam_periph_hold(periph, PRIBIO) == 0) {
675 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
676 cam_fill_ataio(&ccb->ataio,
683 ada_default_timeout*1000);
685 if (softc->flags & ADA_FLAG_CAN_48BIT)
686 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
688 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
689 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
690 /*sense_flags*/0, softc->disk->d_devstat);
693 xpt_print(periph->path, "Synchronize cache failed\n");
695 softc->flags &= ~ADA_FLAG_DIRTY;
696 xpt_release_ccb(ccb);
697 cam_periph_unhold(periph);
700 softc->flags &= ~ADA_FLAG_OPEN;
702 while (softc->refcount != 0)
703 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "adaclose", 1);
704 cam_periph_unlock(periph);
705 cam_periph_release(periph);
710 adaschedule(struct cam_periph *periph)
712 struct ada_softc *softc = (struct ada_softc *)periph->softc;
714 if (softc->state != ADA_STATE_NORMAL)
717 /* Check if we have more work to do. */
718 if (bioq_first(&softc->bio_queue) ||
719 (!softc->trim_running && bioq_first(&softc->trim_queue))) {
720 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
725 * Actually translate the requested transfer into one the physical driver
726 * can understand. The transfer is described by a buf and will include
727 * only one physical transfer.
730 adastrategy(struct bio *bp)
732 struct cam_periph *periph;
733 struct ada_softc *softc;
735 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
736 softc = (struct ada_softc *)periph->softc;
738 cam_periph_lock(periph);
740 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastrategy(%p)\n", bp));
743 * If the device has been made invalid, error out
745 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
746 cam_periph_unlock(periph);
747 biofinish(bp, NULL, ENXIO);
752 * Place it in the queue of disk activities for this disk
754 if (bp->bio_cmd == BIO_DELETE) {
755 KASSERT((softc->flags & ADA_FLAG_CAN_TRIM) ||
756 ((softc->flags & ADA_FLAG_CAN_CFA) &&
757 !(softc->flags & ADA_FLAG_CAN_48BIT)),
758 ("BIO_DELETE but no supported TRIM method."));
759 bioq_disksort(&softc->trim_queue, bp);
762 bioq_disksort(&softc->bio_queue, bp);
764 bioq_insert_tail(&softc->bio_queue, bp);
768 * Schedule ourselves for performing the work.
771 cam_periph_unlock(periph);
777 adadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
779 struct cam_periph *periph;
780 struct ada_softc *softc;
790 softc = (struct ada_softc *)periph->softc;
791 cam_periph_lock(periph);
792 secsize = softc->params.secsize;
793 lba = offset / secsize;
794 count = length / secsize;
796 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
797 cam_periph_unlock(periph);
802 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
803 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
804 cam_fill_ataio(&ccb.ataio,
809 (u_int8_t *) virtual,
811 ada_default_timeout*1000);
812 if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
813 (lba + count >= ATA_MAX_28BIT_LBA ||
815 ata_48bit_cmd(&ccb.ataio, ATA_WRITE_DMA48,
818 ata_28bit_cmd(&ccb.ataio, ATA_WRITE_DMA,
821 xpt_polled_action(&ccb);
823 error = cam_periph_error(&ccb,
824 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
825 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
826 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
827 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
829 printf("Aborting dump due to I/O error.\n");
831 cam_periph_unlock(periph);
835 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) {
836 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
838 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
839 cam_fill_ataio(&ccb.ataio,
846 ada_default_timeout*1000);
848 if (softc->flags & ADA_FLAG_CAN_48BIT)
849 ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0);
851 ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0);
852 xpt_polled_action(&ccb);
854 error = cam_periph_error(&ccb,
855 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
856 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
857 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
858 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
860 xpt_print(periph->path, "Synchronize cache failed\n");
862 cam_periph_unlock(periph);
872 * Install a global async callback. This callback will
873 * receive async callbacks like "new device found".
875 status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL);
877 if (status != CAM_REQ_CMP) {
878 printf("ada: Failed to attach master async callback "
879 "due to status 0x%x!\n", status);
880 } else if (ada_send_ordered) {
882 /* Register our event handlers */
883 if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend,
884 NULL, EVENTHANDLER_PRI_LAST)) == NULL)
885 printf("adainit: power event registration failed!\n");
886 if ((EVENTHANDLER_REGISTER(power_resume, adaresume,
887 NULL, EVENTHANDLER_PRI_LAST)) == NULL)
888 printf("adainit: power event registration failed!\n");
889 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown,
890 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
891 printf("adainit: shutdown event registration failed!\n");
896 * Callback from GEOM, called when it has finished cleaning up its
900 adadiskgonecb(struct disk *dp)
902 struct cam_periph *periph;
904 periph = (struct cam_periph *)dp->d_drv1;
906 cam_periph_release(periph);
910 adaoninvalidate(struct cam_periph *periph)
912 struct ada_softc *softc;
914 softc = (struct ada_softc *)periph->softc;
917 * De-register any async callbacks.
919 xpt_register_async(0, adaasync, periph, periph->path);
922 * Return all queued I/O with ENXIO.
923 * XXX Handle any transactions queued to the card
924 * with XPT_ABORT_CCB.
926 bioq_flush(&softc->bio_queue, NULL, ENXIO);
927 bioq_flush(&softc->trim_queue, NULL, ENXIO);
929 disk_gone(softc->disk);
933 adacleanup(struct cam_periph *periph)
935 struct ada_softc *softc;
937 softc = (struct ada_softc *)periph->softc;
939 cam_periph_unlock(periph);
942 * If we can't free the sysctl tree, oh well...
944 if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0
945 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
946 xpt_print(periph->path, "can't remove sysctl context\n");
949 disk_destroy(softc->disk);
950 callout_drain(&softc->sendordered_c);
951 free(softc, M_DEVBUF);
952 cam_periph_lock(periph);
956 adaasync(void *callback_arg, u_int32_t code,
957 struct cam_path *path, void *arg)
959 struct ccb_getdev cgd;
960 struct cam_periph *periph;
961 struct ada_softc *softc;
963 periph = (struct cam_periph *)callback_arg;
965 case AC_FOUND_DEVICE:
967 struct ccb_getdev *cgd;
970 cgd = (struct ccb_getdev *)arg;
974 if (cgd->protocol != PROTO_ATA)
978 * Allocate a peripheral instance for
979 * this device and start the probe
982 status = cam_periph_alloc(adaregister, adaoninvalidate,
983 adacleanup, adastart,
984 "ada", CAM_PERIPH_BIO,
986 AC_FOUND_DEVICE, cgd);
988 if (status != CAM_REQ_CMP
989 && status != CAM_REQ_INPROG)
990 printf("adaasync: Unable to attach to new device "
991 "due to status 0x%x\n", status);
994 case AC_GETDEV_CHANGED:
996 softc = (struct ada_softc *)periph->softc;
997 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
998 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
999 xpt_action((union ccb *)&cgd);
1001 if ((cgd.ident_data.capabilities1 & ATA_SUPPORT_DMA) &&
1002 (cgd.inq_flags & SID_DMA))
1003 softc->flags |= ADA_FLAG_CAN_DMA;
1005 softc->flags &= ~ADA_FLAG_CAN_DMA;
1006 if (cgd.ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) {
1007 softc->flags |= ADA_FLAG_CAN_48BIT;
1008 if (cgd.inq_flags & SID_DMA48)
1009 softc->flags |= ADA_FLAG_CAN_DMA48;
1011 softc->flags &= ~ADA_FLAG_CAN_DMA48;
1013 softc->flags &= ~(ADA_FLAG_CAN_48BIT |
1014 ADA_FLAG_CAN_DMA48);
1015 if ((cgd.ident_data.satacapabilities & ATA_SUPPORT_NCQ) &&
1016 (cgd.inq_flags & SID_DMA) && (cgd.inq_flags & SID_CmdQue))
1017 softc->flags |= ADA_FLAG_CAN_NCQ;
1019 softc->flags &= ~ADA_FLAG_CAN_NCQ;
1020 if ((cgd.ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) &&
1021 (cgd.inq_flags & SID_DMA))
1022 softc->flags |= ADA_FLAG_CAN_TRIM;
1024 softc->flags &= ~ADA_FLAG_CAN_TRIM;
1026 cam_periph_async(periph, code, path, arg);
1029 case AC_ADVINFO_CHANGED:
1033 buftype = (uintptr_t)arg;
1034 if (buftype == CDAI_TYPE_PHYS_PATH) {
1035 struct ada_softc *softc;
1037 softc = periph->softc;
1038 disk_attr_changed(softc->disk, "GEOM::physpath",
1046 softc = (struct ada_softc *)periph->softc;
1047 cam_periph_async(periph, code, path, arg);
1048 if (softc->state != ADA_STATE_NORMAL)
1050 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1051 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1052 xpt_action((union ccb *)&cgd);
1054 cgd.ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD)
1055 softc->state = ADA_STATE_RAHEAD;
1056 else if (ADA_WC >= 0 &&
1057 cgd.ident_data.support.command1 & ATA_SUPPORT_WRITECACHE)
1058 softc->state = ADA_STATE_WCACHE;
1061 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1062 softc->state = ADA_STATE_NORMAL;
1064 xpt_schedule(periph, CAM_PRIORITY_DEV);
1067 cam_periph_async(periph, code, path, arg);
1073 adasysctlinit(void *context, int pending)
1075 struct cam_periph *periph;
1076 struct ada_softc *softc;
1077 char tmpstr[80], tmpstr2[80];
1079 periph = (struct cam_periph *)context;
1081 /* periph was held for us when this task was enqueued */
1082 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
1083 cam_periph_release(periph);
1087 softc = (struct ada_softc *)periph->softc;
1088 snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d", periph->unit_number);
1089 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1091 sysctl_ctx_init(&softc->sysctl_ctx);
1092 softc->flags |= ADA_FLAG_SCTX_INIT;
1093 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1094 SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2,
1095 CTLFLAG_RD, 0, tmpstr);
1096 if (softc->sysctl_tree == NULL) {
1097 printf("adasysctlinit: unable to allocate sysctl tree\n");
1098 cam_periph_release(periph);
1102 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1103 OID_AUTO, "read_ahead", CTLFLAG_RW | CTLFLAG_MPSAFE,
1104 &softc->read_ahead, 0, "Enable disk read ahead.");
1105 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1106 OID_AUTO, "write_cache", CTLFLAG_RW | CTLFLAG_MPSAFE,
1107 &softc->write_cache, 0, "Enable disk write cache.");
1108 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1109 OID_AUTO, "sort_io_queue", CTLFLAG_RW | CTLFLAG_MPSAFE,
1110 &softc->sort_io_queue, 0,
1111 "Sort IO queue to try and optimise disk access patterns");
1112 #ifdef ADA_TEST_FAILURE
1114 * Add a 'door bell' sysctl which allows one to set it from userland
1115 * and cause something bad to happen. For the moment, we only allow
1116 * whacking the next read or write.
1118 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1119 OID_AUTO, "force_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1120 &softc->force_read_error, 0,
1121 "Force a read error for the next N reads.");
1122 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1123 OID_AUTO, "force_write_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1124 &softc->force_write_error, 0,
1125 "Force a write error for the next N writes.");
1126 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1127 OID_AUTO, "periodic_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1128 &softc->periodic_read_error, 0,
1129 "Force a read error every N reads (don't set too low).");
1131 cam_periph_release(periph);
1135 adagetattr(struct bio *bp)
1138 struct cam_periph *periph;
1140 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1141 cam_periph_lock(periph);
1142 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1144 cam_periph_unlock(periph);
1146 bp->bio_completed = bp->bio_length;
1151 adaregister(struct cam_periph *periph, void *arg)
1153 struct ada_softc *softc;
1154 struct ccb_pathinq cpi;
1155 struct ccb_getdev *cgd;
1156 char announce_buf[80], buf1[32];
1157 struct disk_params *dp;
1160 int legacy_id, quirks;
1162 cgd = (struct ccb_getdev *)arg;
1164 printf("adaregister: no getdev CCB, can't register device\n");
1165 return(CAM_REQ_CMP_ERR);
1168 softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF,
1171 if (softc == NULL) {
1172 printf("adaregister: Unable to probe new device. "
1173 "Unable to allocate softc\n");
1174 return(CAM_REQ_CMP_ERR);
1177 bioq_init(&softc->bio_queue);
1178 bioq_init(&softc->trim_queue);
1180 if ((cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA) &&
1181 (cgd->inq_flags & SID_DMA))
1182 softc->flags |= ADA_FLAG_CAN_DMA;
1183 if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) {
1184 softc->flags |= ADA_FLAG_CAN_48BIT;
1185 if (cgd->inq_flags & SID_DMA48)
1186 softc->flags |= ADA_FLAG_CAN_DMA48;
1188 if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE)
1189 softc->flags |= ADA_FLAG_CAN_FLUSHCACHE;
1190 if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT)
1191 softc->flags |= ADA_FLAG_CAN_POWERMGT;
1192 if ((cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ) &&
1193 (cgd->inq_flags & SID_DMA) && (cgd->inq_flags & SID_CmdQue))
1194 softc->flags |= ADA_FLAG_CAN_NCQ;
1195 if ((cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) &&
1196 (cgd->inq_flags & SID_DMA)) {
1197 softc->flags |= ADA_FLAG_CAN_TRIM;
1198 softc->trim_max_ranges = TRIM_MAX_RANGES;
1199 if (cgd->ident_data.max_dsm_blocks != 0) {
1200 softc->trim_max_ranges =
1201 min(cgd->ident_data.max_dsm_blocks *
1202 ATA_DSM_BLK_RANGES, softc->trim_max_ranges);
1205 if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA)
1206 softc->flags |= ADA_FLAG_CAN_CFA;
1208 periph->softc = softc;
1211 * See if this device has any quirks.
1213 match = cam_quirkmatch((caddr_t)&cgd->ident_data,
1214 (caddr_t)ada_quirk_table,
1215 sizeof(ada_quirk_table)/sizeof(*ada_quirk_table),
1216 sizeof(*ada_quirk_table), ata_identify_match);
1218 softc->quirks = ((struct ada_quirk_entry *)match)->quirks;
1220 softc->quirks = ADA_Q_NONE;
1222 bzero(&cpi, sizeof(cpi));
1223 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE);
1224 cpi.ccb_h.func_code = XPT_PATH_INQ;
1225 xpt_action((union ccb *)&cpi);
1227 TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph);
1230 * Register this media as a disk
1232 (void)cam_periph_hold(periph, PRIBIO);
1233 cam_periph_unlock(periph);
1234 snprintf(announce_buf, sizeof(announce_buf),
1235 "kern.cam.ada.%d.quirks", periph->unit_number);
1236 quirks = softc->quirks;
1237 TUNABLE_INT_FETCH(announce_buf, &quirks);
1238 softc->quirks = quirks;
1239 softc->read_ahead = -1;
1240 snprintf(announce_buf, sizeof(announce_buf),
1241 "kern.cam.ada.%d.read_ahead", periph->unit_number);
1242 TUNABLE_INT_FETCH(announce_buf, &softc->read_ahead);
1243 softc->write_cache = -1;
1244 snprintf(announce_buf, sizeof(announce_buf),
1245 "kern.cam.ada.%d.write_cache", periph->unit_number);
1246 TUNABLE_INT_FETCH(announce_buf, &softc->write_cache);
1247 /* Disable queue sorting for non-rotational media by default. */
1248 if (cgd->ident_data.media_rotation_rate == ATA_RATE_NON_ROTATING)
1249 softc->sort_io_queue = 0;
1251 softc->sort_io_queue = -1;
1252 adagetparams(periph, cgd);
1253 softc->disk = disk_alloc();
1254 softc->disk->d_rotation_rate = cgd->ident_data.media_rotation_rate;
1255 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
1256 periph->unit_number, softc->params.secsize,
1257 DEVSTAT_ALL_SUPPORTED,
1258 DEVSTAT_TYPE_DIRECT |
1259 XPORT_DEVSTAT_TYPE(cpi.transport),
1260 DEVSTAT_PRIORITY_DISK);
1261 softc->disk->d_open = adaopen;
1262 softc->disk->d_close = adaclose;
1263 softc->disk->d_strategy = adastrategy;
1264 softc->disk->d_getattr = adagetattr;
1265 softc->disk->d_dump = adadump;
1266 softc->disk->d_gone = adadiskgonecb;
1267 softc->disk->d_name = "ada";
1268 softc->disk->d_drv1 = periph;
1269 maxio = cpi.maxio; /* Honor max I/O size of SIM */
1271 maxio = DFLTPHYS; /* traditional default */
1272 else if (maxio > MAXPHYS)
1273 maxio = MAXPHYS; /* for safety */
1274 if (softc->flags & ADA_FLAG_CAN_48BIT)
1275 maxio = min(maxio, 65536 * softc->params.secsize);
1276 else /* 28bit ATA command limit */
1277 maxio = min(maxio, 256 * softc->params.secsize);
1278 softc->disk->d_maxsize = maxio;
1279 softc->disk->d_unit = periph->unit_number;
1280 softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION;
1281 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE)
1282 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
1283 if (softc->flags & ADA_FLAG_CAN_TRIM) {
1284 softc->disk->d_flags |= DISKFLAG_CANDELETE;
1285 softc->disk->d_delmaxsize = softc->params.secsize *
1287 softc->trim_max_ranges;
1288 } else if ((softc->flags & ADA_FLAG_CAN_CFA) &&
1289 !(softc->flags & ADA_FLAG_CAN_48BIT)) {
1290 softc->disk->d_flags |= DISKFLAG_CANDELETE;
1291 softc->disk->d_delmaxsize = 256 * softc->params.secsize;
1293 softc->disk->d_delmaxsize = maxio;
1294 if ((cpi.hba_misc & PIM_UNMAPPED) != 0)
1295 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
1296 strlcpy(softc->disk->d_descr, cgd->ident_data.model,
1297 MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model)));
1298 strlcpy(softc->disk->d_ident, cgd->ident_data.serial,
1299 MIN(sizeof(softc->disk->d_ident), sizeof(cgd->ident_data.serial)));
1300 softc->disk->d_hba_vendor = cpi.hba_vendor;
1301 softc->disk->d_hba_device = cpi.hba_device;
1302 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
1303 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
1305 softc->disk->d_sectorsize = softc->params.secsize;
1306 softc->disk->d_mediasize = (off_t)softc->params.sectors *
1307 softc->params.secsize;
1308 if (ata_physical_sector_size(&cgd->ident_data) !=
1309 softc->params.secsize) {
1310 softc->disk->d_stripesize =
1311 ata_physical_sector_size(&cgd->ident_data);
1312 softc->disk->d_stripeoffset = (softc->disk->d_stripesize -
1313 ata_logical_sector_offset(&cgd->ident_data)) %
1314 softc->disk->d_stripesize;
1315 } else if (softc->quirks & ADA_Q_4K) {
1316 softc->disk->d_stripesize = 4096;
1317 softc->disk->d_stripeoffset = 0;
1319 softc->disk->d_fwsectors = softc->params.secs_per_track;
1320 softc->disk->d_fwheads = softc->params.heads;
1321 ata_disk_firmware_geom_adjust(softc->disk);
1323 if (ada_legacy_aliases) {
1324 #ifdef ATA_STATIC_ID
1325 legacy_id = xpt_path_legacy_ata_id(periph->path);
1327 legacy_id = softc->disk->d_unit;
1329 if (legacy_id >= 0) {
1330 snprintf(announce_buf, sizeof(announce_buf),
1331 "kern.devalias.%s%d",
1332 softc->disk->d_name, softc->disk->d_unit);
1333 snprintf(buf1, sizeof(buf1),
1335 kern_setenv(announce_buf, buf1);
1340 * Acquire a reference to the periph before we register with GEOM.
1341 * We'll release this reference once GEOM calls us back (via
1342 * adadiskgonecb()) telling us that our provider has been freed.
1344 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1345 xpt_print(periph->path, "%s: lost periph during "
1346 "registration!\n", __func__);
1347 cam_periph_lock(periph);
1348 return (CAM_REQ_CMP_ERR);
1350 disk_create(softc->disk, DISK_VERSION);
1351 cam_periph_lock(periph);
1352 cam_periph_unhold(periph);
1354 dp = &softc->params;
1355 snprintf(announce_buf, sizeof(announce_buf),
1356 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1357 (uintmax_t)(((uintmax_t)dp->secsize *
1358 dp->sectors) / (1024*1024)),
1359 (uintmax_t)dp->sectors,
1360 dp->secsize, dp->heads,
1361 dp->secs_per_track, dp->cylinders);
1362 xpt_announce_periph(periph, announce_buf);
1363 xpt_announce_quirks(periph, softc->quirks, ADA_Q_BIT_STRING);
1365 printf("%s%d: Previously was known as ad%d\n",
1366 periph->periph_name, periph->unit_number, legacy_id);
1369 * Create our sysctl variables, now that we know
1370 * we have successfully attached.
1372 if (cam_periph_acquire(periph) == CAM_REQ_CMP)
1373 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task);
1376 * Add async callbacks for bus reset and
1377 * bus device reset calls. I don't bother
1378 * checking if this fails as, in most cases,
1379 * the system will function just fine without
1380 * them and the only alternative would be to
1381 * not attach the device on failure.
1383 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
1384 AC_GETDEV_CHANGED | AC_ADVINFO_CHANGED,
1385 adaasync, periph, periph->path);
1388 * Schedule a periodic event to occasionally send an
1389 * ordered tag to a device.
1391 callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0);
1392 callout_reset(&softc->sendordered_c,
1393 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL,
1394 adasendorderedtag, softc);
1397 cgd->ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD) {
1398 softc->state = ADA_STATE_RAHEAD;
1399 } else if (ADA_WC >= 0 &&
1400 cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) {
1401 softc->state = ADA_STATE_WCACHE;
1403 softc->state = ADA_STATE_NORMAL;
1404 return(CAM_REQ_CMP);
1406 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
1407 softc->state = ADA_STATE_NORMAL;
1409 xpt_schedule(periph, CAM_PRIORITY_DEV);
1410 return(CAM_REQ_CMP);
1414 ada_dsmtrim(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio)
1416 struct trim_request *req = &softc->trim_req;
1417 uint64_t lastlba = (uint64_t)-1;
1418 int c, lastcount = 0, off, ranges = 0;
1420 bzero(req, sizeof(*req));
1421 TAILQ_INIT(&req->bps);
1423 uint64_t lba = bp->bio_pblkno;
1424 int count = bp->bio_bcount / softc->params.secsize;
1426 bioq_remove(&softc->trim_queue, bp);
1428 /* Try to extend the previous range. */
1429 if (lba == lastlba) {
1430 c = min(count, ATA_DSM_RANGE_MAX - lastcount);
1432 off = (ranges - 1) * ATA_DSM_RANGE_SIZE;
1433 req->data[off + 6] = lastcount & 0xff;
1434 req->data[off + 7] =
1435 (lastcount >> 8) & 0xff;
1441 c = min(count, ATA_DSM_RANGE_MAX);
1442 off = ranges * ATA_DSM_RANGE_SIZE;
1443 req->data[off + 0] = lba & 0xff;
1444 req->data[off + 1] = (lba >> 8) & 0xff;
1445 req->data[off + 2] = (lba >> 16) & 0xff;
1446 req->data[off + 3] = (lba >> 24) & 0xff;
1447 req->data[off + 4] = (lba >> 32) & 0xff;
1448 req->data[off + 5] = (lba >> 40) & 0xff;
1449 req->data[off + 6] = c & 0xff;
1450 req->data[off + 7] = (c >> 8) & 0xff;
1456 * Its the caller's responsibility to ensure the
1457 * request will fit so we don't need to check for
1462 TAILQ_INSERT_TAIL(&req->bps, bp, bio_queue);
1463 bp = bioq_first(&softc->trim_queue);
1465 bp->bio_bcount / softc->params.secsize >
1466 (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX)
1469 cam_fill_ataio(ataio,
1475 ((ranges + ATA_DSM_BLK_RANGES - 1) /
1476 ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE,
1477 ada_default_timeout * 1000);
1478 ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT,
1479 ATA_DSM_TRIM, 0, (ranges + ATA_DSM_BLK_RANGES -
1480 1) / ATA_DSM_BLK_RANGES);
1484 ada_cfaerase(struct ada_softc *softc, struct bio *bp, struct ccb_ataio *ataio)
1486 struct trim_request *req = &softc->trim_req;
1487 uint64_t lba = bp->bio_pblkno;
1488 uint16_t count = bp->bio_bcount / softc->params.secsize;
1490 bzero(req, sizeof(*req));
1491 TAILQ_INIT(&req->bps);
1492 bioq_remove(&softc->trim_queue, bp);
1493 TAILQ_INSERT_TAIL(&req->bps, bp, bio_queue);
1495 cam_fill_ataio(ataio,
1502 ada_default_timeout*1000);
1506 ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count);
1510 adastart(struct cam_periph *periph, union ccb *start_ccb)
1512 struct ada_softc *softc = (struct ada_softc *)periph->softc;
1513 struct ccb_ataio *ataio = &start_ccb->ataio;
1515 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastart\n"));
1517 switch (softc->state) {
1518 case ADA_STATE_NORMAL:
1523 /* Run TRIM if not running yet. */
1524 if (!softc->trim_running &&
1525 (bp = bioq_first(&softc->trim_queue)) != 0) {
1526 if (softc->flags & ADA_FLAG_CAN_TRIM) {
1527 ada_dsmtrim(softc, bp, ataio);
1528 } else if ((softc->flags & ADA_FLAG_CAN_CFA) &&
1529 !(softc->flags & ADA_FLAG_CAN_48BIT)) {
1530 ada_cfaerase(softc, bp, ataio);
1532 panic("adastart: BIO_DELETE without method, not possible.");
1534 softc->trim_running = 1;
1535 start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM;
1536 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
1539 /* Run regular command. */
1540 bp = bioq_first(&softc->bio_queue);
1542 xpt_release_ccb(start_ccb);
1545 bioq_remove(&softc->bio_queue, bp);
1547 if ((bp->bio_flags & BIO_ORDERED) != 0
1548 || (softc->flags & ADA_FLAG_NEED_OTAG) != 0) {
1549 softc->flags &= ~ADA_FLAG_NEED_OTAG;
1550 softc->flags |= ADA_FLAG_WAS_OTAG;
1555 switch (bp->bio_cmd) {
1557 softc->flags |= ADA_FLAG_DIRTY;
1561 uint64_t lba = bp->bio_pblkno;
1562 uint16_t count = bp->bio_bcount / softc->params.secsize;
1563 #ifdef ADA_TEST_FAILURE
1567 * Support the failure ioctls. If the command is a
1568 * read, and there are pending forced read errors, or
1569 * if a write and pending write errors, then fail this
1570 * operation with EIO. This is useful for testing
1571 * purposes. Also, support having every Nth read fail.
1573 * This is a rather blunt tool.
1575 if (bp->bio_cmd == BIO_READ) {
1576 if (softc->force_read_error) {
1577 softc->force_read_error--;
1580 if (softc->periodic_read_error > 0) {
1581 if (++softc->periodic_read_count >=
1582 softc->periodic_read_error) {
1583 softc->periodic_read_count = 0;
1588 if (softc->force_write_error) {
1589 softc->force_write_error--;
1594 bp->bio_error = EIO;
1595 bp->bio_flags |= BIO_ERROR;
1597 xpt_release_ccb(start_ccb);
1598 adaschedule(periph);
1602 KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 ||
1603 round_page(bp->bio_bcount + bp->bio_ma_offset) /
1604 PAGE_SIZE == bp->bio_ma_n,
1605 ("Short bio %p", bp));
1606 cam_fill_ataio(ataio,
1609 (bp->bio_cmd == BIO_READ ? CAM_DIR_IN :
1610 CAM_DIR_OUT) | ((bp->bio_flags & BIO_UNMAPPED)
1611 != 0 ? CAM_DATA_BIO : 0),
1613 ((bp->bio_flags & BIO_UNMAPPED) != 0) ? (void *)bp :
1616 ada_default_timeout*1000);
1618 if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) {
1619 if (bp->bio_cmd == BIO_READ) {
1620 ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED,
1623 ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED,
1626 } else if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
1627 (lba + count >= ATA_MAX_28BIT_LBA ||
1629 if (softc->flags & ADA_FLAG_CAN_DMA48) {
1630 if (bp->bio_cmd == BIO_READ) {
1631 ata_48bit_cmd(ataio, ATA_READ_DMA48,
1634 ata_48bit_cmd(ataio, ATA_WRITE_DMA48,
1638 if (bp->bio_cmd == BIO_READ) {
1639 ata_48bit_cmd(ataio, ATA_READ_MUL48,
1642 ata_48bit_cmd(ataio, ATA_WRITE_MUL48,
1649 if (softc->flags & ADA_FLAG_CAN_DMA) {
1650 if (bp->bio_cmd == BIO_READ) {
1651 ata_28bit_cmd(ataio, ATA_READ_DMA,
1654 ata_28bit_cmd(ataio, ATA_WRITE_DMA,
1658 if (bp->bio_cmd == BIO_READ) {
1659 ata_28bit_cmd(ataio, ATA_READ_MUL,
1662 ata_28bit_cmd(ataio, ATA_WRITE_MUL,
1670 cam_fill_ataio(ataio,
1677 ada_default_timeout*1000);
1679 if (softc->flags & ADA_FLAG_CAN_48BIT)
1680 ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1682 ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0);
1685 start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO;
1686 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
1688 start_ccb->ccb_h.ccb_bp = bp;
1689 softc->outstanding_cmds++;
1691 cam_periph_unlock(periph);
1692 xpt_action(start_ccb);
1693 cam_periph_lock(periph);
1696 /* May have more work to do, so ensure we stay scheduled */
1697 adaschedule(periph);
1700 case ADA_STATE_RAHEAD:
1701 case ADA_STATE_WCACHE:
1703 cam_fill_ataio(ataio,
1710 ada_default_timeout*1000);
1712 if (softc->state == ADA_STATE_RAHEAD) {
1713 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_RA ?
1714 ATA_SF_ENAB_RCACHE : ATA_SF_DIS_RCACHE, 0, 0);
1715 start_ccb->ccb_h.ccb_state = ADA_CCB_RAHEAD;
1717 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_WC ?
1718 ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0);
1719 start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE;
1721 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1722 xpt_action(start_ccb);
1729 adadone(struct cam_periph *periph, union ccb *done_ccb)
1731 struct ada_softc *softc;
1732 struct ccb_ataio *ataio;
1733 struct ccb_getdev *cgd;
1734 struct cam_path *path;
1737 softc = (struct ada_softc *)periph->softc;
1738 ataio = &done_ccb->ataio;
1739 path = done_ccb->ccb_h.path;
1741 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("adadone\n"));
1743 state = ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK;
1745 case ADA_CCB_BUFFER_IO:
1751 cam_periph_lock(periph);
1752 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1753 error = adaerror(done_ccb, 0, 0);
1754 if (error == ERESTART) {
1755 /* A retry was scheduled, so just return. */
1756 cam_periph_unlock(periph);
1759 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1760 cam_release_devq(path,
1764 /*getcount_only*/0);
1766 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1767 panic("REQ_CMP with QFRZN");
1770 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1771 bp->bio_error = error;
1773 bp->bio_resid = bp->bio_bcount;
1774 bp->bio_flags |= BIO_ERROR;
1776 if (state == ADA_CCB_TRIM)
1779 bp->bio_resid = ataio->resid;
1780 if (bp->bio_resid > 0)
1781 bp->bio_flags |= BIO_ERROR;
1783 softc->outstanding_cmds--;
1784 if (softc->outstanding_cmds == 0)
1785 softc->flags |= ADA_FLAG_WAS_OTAG;
1786 xpt_release_ccb(done_ccb);
1787 if (state == ADA_CCB_TRIM) {
1788 TAILQ_HEAD(, bio) queue;
1792 TAILQ_CONCAT(&queue, &softc->trim_req.bps, bio_queue);
1794 * Normally, the xpt_release_ccb() above would make sure
1795 * that when we have more work to do, that work would
1796 * get kicked off. However, we specifically keep
1797 * trim_running set to 0 before the call above to allow
1798 * other I/O to progress when many BIO_DELETE requests
1799 * are pushed down. We set trim_running to 0 and call
1800 * daschedule again so that we don't stall if there are
1801 * no other I/Os pending apart from BIO_DELETEs.
1803 softc->trim_running = 0;
1804 adaschedule(periph);
1805 cam_periph_unlock(periph);
1806 while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
1807 TAILQ_REMOVE(&queue, bp1, bio_queue);
1808 bp1->bio_error = error;
1810 bp1->bio_flags |= BIO_ERROR;
1811 bp1->bio_resid = bp1->bio_bcount;
1817 cam_periph_unlock(periph);
1822 case ADA_CCB_RAHEAD:
1824 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1825 if (adaerror(done_ccb, 0, 0) == ERESTART) {
1827 /* Drop freeze taken due to CAM_DEV_QFREEZE */
1828 cam_release_devq(path, 0, 0, 0, FALSE);
1830 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1831 cam_release_devq(path,
1835 /*getcount_only*/0);
1840 * Since our peripheral may be invalidated by an error
1841 * above or an external event, we must release our CCB
1842 * before releasing the reference on the peripheral.
1843 * The peripheral will only go away once the last reference
1844 * is removed, and we need it around for the CCB release
1847 cgd = (struct ccb_getdev *)done_ccb;
1848 xpt_setup_ccb(&cgd->ccb_h, path, CAM_PRIORITY_NORMAL);
1849 cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1850 xpt_action((union ccb *)cgd);
1852 cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) {
1853 softc->state = ADA_STATE_WCACHE;
1854 xpt_release_ccb(done_ccb);
1855 xpt_schedule(periph, CAM_PRIORITY_DEV);
1858 softc->state = ADA_STATE_NORMAL;
1859 xpt_release_ccb(done_ccb);
1860 /* Drop freeze taken due to CAM_DEV_QFREEZE */
1861 cam_release_devq(path, 0, 0, 0, FALSE);
1862 adaschedule(periph);
1863 cam_periph_release_locked(periph);
1866 case ADA_CCB_WCACHE:
1868 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1869 if (adaerror(done_ccb, 0, 0) == ERESTART) {
1871 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1872 cam_release_devq(path,
1876 /*getcount_only*/0);
1880 softc->state = ADA_STATE_NORMAL;
1882 * Since our peripheral may be invalidated by an error
1883 * above or an external event, we must release our CCB
1884 * before releasing the reference on the peripheral.
1885 * The peripheral will only go away once the last reference
1886 * is removed, and we need it around for the CCB release
1889 xpt_release_ccb(done_ccb);
1890 /* Drop freeze taken due to CAM_DEV_QFREEZE */
1891 cam_release_devq(path, 0, 0, 0, FALSE);
1892 adaschedule(periph);
1893 cam_periph_release_locked(periph);
1897 /* No-op. We're polling */
1902 xpt_release_ccb(done_ccb);
1906 adaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1909 return(cam_periph_error(ccb, cam_flags, sense_flags, NULL));
1913 adagetparams(struct cam_periph *periph, struct ccb_getdev *cgd)
1915 struct ada_softc *softc = (struct ada_softc *)periph->softc;
1916 struct disk_params *dp = &softc->params;
1917 u_int64_t lbasize48;
1920 dp->secsize = ata_logical_sector_size(&cgd->ident_data);
1921 if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) &&
1922 cgd->ident_data.current_heads && cgd->ident_data.current_sectors) {
1923 dp->heads = cgd->ident_data.current_heads;
1924 dp->secs_per_track = cgd->ident_data.current_sectors;
1925 dp->cylinders = cgd->ident_data.cylinders;
1926 dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 |
1927 ((u_int32_t)cgd->ident_data.current_size_2 << 16);
1929 dp->heads = cgd->ident_data.heads;
1930 dp->secs_per_track = cgd->ident_data.sectors;
1931 dp->cylinders = cgd->ident_data.cylinders;
1932 dp->sectors = cgd->ident_data.cylinders * dp->heads * dp->secs_per_track;
1934 lbasize = (u_int32_t)cgd->ident_data.lba_size_1 |
1935 ((u_int32_t)cgd->ident_data.lba_size_2 << 16);
1937 /* use the 28bit LBA size if valid or bigger than the CHS mapping */
1938 if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize)
1939 dp->sectors = lbasize;
1941 /* use the 48bit LBA size if valid */
1942 lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) |
1943 ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) |
1944 ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) |
1945 ((u_int64_t)cgd->ident_data.lba_size48_4 << 48);
1946 if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) &&
1947 lbasize48 > ATA_MAX_28BIT_LBA)
1948 dp->sectors = lbasize48;
1952 adasendorderedtag(void *arg)
1954 struct ada_softc *softc = arg;
1956 if (ada_send_ordered) {
1957 if (softc->outstanding_cmds > 0) {
1958 if ((softc->flags & ADA_FLAG_WAS_OTAG) == 0)
1959 softc->flags |= ADA_FLAG_NEED_OTAG;
1960 softc->flags &= ~ADA_FLAG_WAS_OTAG;
1963 /* Queue us up again */
1964 callout_reset(&softc->sendordered_c,
1965 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL,
1966 adasendorderedtag, softc);
1970 * Step through all ADA peripheral drivers, and if the device is still open,
1971 * sync the disk cache to physical media.
1976 struct cam_periph *periph;
1977 struct ada_softc *softc;
1981 CAM_PERIPH_FOREACH(periph, &adadriver) {
1982 softc = (struct ada_softc *)periph->softc;
1983 if (SCHEDULER_STOPPED()) {
1984 /* If we paniced with the lock held, do not recurse. */
1985 if (!cam_periph_owned(periph) &&
1986 (softc->flags & ADA_FLAG_OPEN)) {
1987 adadump(softc->disk, NULL, 0, 0, 0);
1991 cam_periph_lock(periph);
1993 * We only sync the cache if the drive is still open, and
1994 * if the drive is capable of it..
1996 if (((softc->flags & ADA_FLAG_OPEN) == 0) ||
1997 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) {
1998 cam_periph_unlock(periph);
2002 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2003 cam_fill_ataio(&ccb->ataio,
2010 ada_default_timeout*1000);
2011 if (softc->flags & ADA_FLAG_CAN_48BIT)
2012 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
2014 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
2016 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
2017 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
2018 softc->disk->d_devstat);
2020 xpt_print(periph->path, "Synchronize cache failed\n");
2021 xpt_release_ccb(ccb);
2022 cam_periph_unlock(periph);
2027 adaspindown(uint8_t cmd, int flags)
2029 struct cam_periph *periph;
2030 struct ada_softc *softc;
2034 CAM_PERIPH_FOREACH(periph, &adadriver) {
2035 /* If we paniced with lock held - not recurse here. */
2036 if (cam_periph_owned(periph))
2038 cam_periph_lock(periph);
2039 softc = (struct ada_softc *)periph->softc;
2041 * We only spin-down the drive if it is capable of it..
2043 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
2044 cam_periph_unlock(periph);
2049 xpt_print(periph->path, "spin-down\n");
2051 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2052 cam_fill_ataio(&ccb->ataio,
2055 CAM_DIR_NONE | flags,
2059 ada_default_timeout*1000);
2060 ata_28bit_cmd(&ccb->ataio, cmd, 0, 0, 0);
2062 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
2063 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
2064 softc->disk->d_devstat);
2066 xpt_print(periph->path, "Spin-down disk failed\n");
2067 xpt_release_ccb(ccb);
2068 cam_periph_unlock(periph);
2073 adashutdown(void *arg, int howto)
2077 if (ada_spindown_shutdown != 0 &&
2078 (howto & (RB_HALT | RB_POWEROFF)) != 0)
2079 adaspindown(ATA_STANDBY_IMMEDIATE, 0);
2083 adasuspend(void *arg)
2087 if (ada_spindown_suspend != 0)
2088 adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE);
2092 adaresume(void *arg)
2094 struct cam_periph *periph;
2095 struct ada_softc *softc;
2097 if (ada_spindown_suspend == 0)
2100 CAM_PERIPH_FOREACH(periph, &adadriver) {
2101 cam_periph_lock(periph);
2102 softc = (struct ada_softc *)periph->softc;
2104 * We only spin-down the drive if it is capable of it..
2106 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
2107 cam_periph_unlock(periph);
2112 xpt_print(periph->path, "resume\n");
2115 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on
2118 cam_release_devq(periph->path,
2122 /*getcount_only*/0);
2124 cam_periph_unlock(periph);
2128 #endif /* _KERNEL */