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$");
33 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/sysctl.h>
40 #include <sys/taskqueue.h>
42 #include <sys/mutex.h>
44 #include <sys/devicestat.h>
45 #include <sys/eventhandler.h>
46 #include <sys/malloc.h>
49 #include <sys/reboot.h>
50 #include <geom/geom_disk.h>
59 #include <cam/cam_ccb.h>
60 #include <cam/cam_periph.h>
61 #include <cam/cam_xpt_periph.h>
62 #include <cam/cam_sim.h>
64 #include <cam/ata/ata_all.h>
66 #include <machine/md_var.h> /* geometry translation */
70 #define ATA_MAX_28BIT_LBA 268435455UL
79 ADA_FLAG_CAN_48BIT = 0x0002,
80 ADA_FLAG_CAN_FLUSHCACHE = 0x0004,
81 ADA_FLAG_CAN_NCQ = 0x0008,
82 ADA_FLAG_CAN_DMA = 0x0010,
83 ADA_FLAG_NEED_OTAG = 0x0020,
84 ADA_FLAG_WENT_IDLE = 0x0040,
85 ADA_FLAG_CAN_TRIM = 0x0080,
86 ADA_FLAG_OPEN = 0x0100,
87 ADA_FLAG_SCTX_INIT = 0x0200,
88 ADA_FLAG_CAN_CFA = 0x0400,
89 ADA_FLAG_CAN_POWERMGT = 0x0800,
90 ADA_FLAG_CAN_DMA48 = 0x1000,
91 ADA_FLAG_DIRTY = 0x2000
99 #define ADA_Q_BIT_STRING \
104 ADA_CCB_RAHEAD = 0x01,
105 ADA_CCB_WCACHE = 0x02,
106 ADA_CCB_BUFFER_IO = 0x03,
107 ADA_CCB_WAITING = 0x04,
110 ADA_CCB_TYPE_MASK = 0x0F,
113 /* Offsets into our private area for storing information */
114 #define ccb_state ppriv_field0
115 #define ccb_bp ppriv_ptr1
119 u_int8_t secs_per_track;
121 u_int32_t secsize; /* Number of bytes/logical sector */
122 u_int64_t sectors; /* Total number sectors */
125 #define TRIM_MAX_BLOCKS 8
126 #define TRIM_MAX_RANGES (TRIM_MAX_BLOCKS * ATA_DSM_BLK_RANGES)
127 #define TRIM_MAX_BIOS (TRIM_MAX_RANGES * 4)
128 struct trim_request {
129 uint8_t data[TRIM_MAX_RANGES * ATA_DSM_RANGE_SIZE];
130 struct bio *bps[TRIM_MAX_BIOS];
134 struct bio_queue_head bio_queue;
135 struct bio_queue_head trim_queue;
140 int ordered_tag_count;
141 int outstanding_cmds;
146 #ifdef ADA_TEST_FAILURE
147 int force_read_error;
148 int force_write_error;
149 int periodic_read_error;
150 int periodic_read_count;
152 struct disk_params params;
154 struct task sysctl_task;
155 struct sysctl_ctx_list sysctl_ctx;
156 struct sysctl_oid *sysctl_tree;
157 struct callout sendordered_c;
158 struct trim_request trim_req;
161 struct ada_quirk_entry {
162 struct scsi_inquiry_pattern inq_pat;
166 static struct ada_quirk_entry ada_quirk_table[] =
169 /* Hitachi Advanced Format (4k) drives */
170 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Hitachi H??????????E3*", "*" },
174 /* Samsung Advanced Format (4k) drives */
175 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD155UI*", "*" },
179 /* Samsung Advanced Format (4k) drives */
180 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD204UI*", "*" },
184 /* Seagate Barracuda Green Advanced Format (4k) drives */
185 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DL*", "*" },
189 /* Seagate Barracuda Advanced Format (4k) drives */
190 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???DM*", "*" },
194 /* Seagate Barracuda Advanced Format (4k) drives */
195 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DM*", "*" },
199 /* Seagate Momentus Advanced Format (4k) drives */
200 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500423AS*", "*" },
204 /* Seagate Momentus Advanced Format (4k) drives */
205 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500424AS*", "*" },
209 /* Seagate Momentus Advanced Format (4k) drives */
210 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640423AS*", "*" },
214 /* Seagate Momentus Advanced Format (4k) drives */
215 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640424AS*", "*" },
219 /* Seagate Momentus Advanced Format (4k) drives */
220 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750420AS*", "*" },
224 /* Seagate Momentus Advanced Format (4k) drives */
225 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750422AS*", "*" },
229 /* Seagate Momentus Advanced Format (4k) drives */
230 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750423AS*", "*" },
234 /* Seagate Momentus Thin Advanced Format (4k) drives */
235 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???LT*", "*" },
239 /* WDC Caviar Green Advanced Format (4k) drives */
240 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RS*", "*" },
244 /* WDC Caviar Green Advanced Format (4k) drives */
245 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RX*", "*" },
249 /* WDC Caviar Green Advanced Format (4k) drives */
250 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RS*", "*" },
254 /* WDC Caviar Green Advanced Format (4k) drives */
255 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RX*", "*" },
259 /* WDC Scorpio Black Advanced Format (4k) drives */
260 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PKT*", "*" },
264 /* WDC Scorpio Black Advanced Format (4k) drives */
265 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PKT*", "*" },
269 /* WDC Scorpio Blue Advanced Format (4k) drives */
270 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PVT*", "*" },
274 /* WDC Scorpio Blue Advanced Format (4k) drives */
275 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PVT*", "*" },
281 * Corsair Force 2 SSDs
282 * 4k optimised & trim only works in 4k requests + 4k aligned
284 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair CSSD-F*", "*" },
289 * Corsair Force 3 SSDs
290 * 4k optimised & trim only works in 4k requests + 4k aligned
292 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force 3*", "*" },
297 * Corsair Force GT SSDs
298 * 4k optimised & trim only works in 4k requests + 4k aligned
300 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force GT*", "*" },
306 * 4k optimised & trim only works in 4k requests + 4k aligned
308 { T_DIRECT, SIP_MEDIA_FIXED, "*", "M4-CT???M4SSD2*", "*" },
313 * Crucial RealSSD C300 SSDs
316 { T_DIRECT, SIP_MEDIA_FIXED, "*", "C300-CTFDDAC???MAG*",
317 "*" }, /*quirks*/ADA_Q_4K
321 * Intel 320 Series SSDs
322 * 4k optimised & trim only works in 4k requests + 4k aligned
324 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSA2CW*", "*" },
329 * Intel 330 Series SSDs
330 * 4k optimised & trim only works in 4k requests + 4k aligned
332 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2CT*", "*" },
337 * Intel 510 Series SSDs
338 * 4k optimised & trim only works in 4k requests + 4k aligned
340 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2MH*", "*" },
345 * Intel 520 Series SSDs
346 * 4k optimised & trim only works in 4k requests + 4k aligned
348 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2BW*", "*" },
353 * Kingston E100 Series SSDs
354 * 4k optimised & trim only works in 4k requests + 4k aligned
356 { T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SE100S3*", "*" },
361 * Kingston HyperX 3k SSDs
362 * 4k optimised & trim only works in 4k requests + 4k aligned
364 { T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SH103S3*", "*" },
370 * 4k optimised & trim only works in 4k requests + 4k aligned
372 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY3*", "*" },
377 * OCZ Deneva R Series SSDs
378 * 4k optimised & trim only works in 4k requests + 4k aligned
380 { T_DIRECT, SIP_MEDIA_FIXED, "*", "DENRSTE251M45*", "*" },
385 * OCZ Vertex 2 SSDs (inc pro series)
386 * 4k optimised & trim only works in 4k requests + 4k aligned
388 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ?VERTEX2*", "*" },
394 * 4k optimised & trim only works in 4k requests + 4k aligned
396 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX3*", "*" },
402 * 4k optimised & trim only works in 4k requests + 4k aligned
404 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX4*", "*" },
409 * Samsung 830 Series SSDs
412 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG SSD 830 Series*", "*" },
417 * SuperTalent TeraDrive CT SSDs
418 * 4k optimised & trim only works in 4k requests + 4k aligned
420 { T_DIRECT, SIP_MEDIA_FIXED, "*", "FTM??CT25H*", "*" },
425 * XceedIOPS SATA SSDs
428 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SG9XCS2D*", "*" },
434 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
435 /*vendor*/"*", /*product*/"*", /*revision*/"*"
441 static disk_strategy_t adastrategy;
442 static dumper_t adadump;
443 static periph_init_t adainit;
444 static void adaasync(void *callback_arg, u_int32_t code,
445 struct cam_path *path, void *arg);
446 static void adasysctlinit(void *context, int pending);
447 static periph_ctor_t adaregister;
448 static periph_dtor_t adacleanup;
449 static periph_start_t adastart;
450 static periph_oninv_t adaoninvalidate;
451 static void adadone(struct cam_periph *periph,
452 union ccb *done_ccb);
453 static int adaerror(union ccb *ccb, u_int32_t cam_flags,
454 u_int32_t sense_flags);
455 static void adagetparams(struct cam_periph *periph,
456 struct ccb_getdev *cgd);
457 static timeout_t adasendorderedtag;
458 static void adashutdown(void *arg, int howto);
459 static void adasuspend(void *arg);
460 static void adaresume(void *arg);
462 #ifndef ADA_DEFAULT_LEGACY_ALIASES
464 #define ADA_DEFAULT_LEGACY_ALIASES 1
466 #define ADA_DEFAULT_LEGACY_ALIASES 0
470 #ifndef ADA_DEFAULT_TIMEOUT
471 #define ADA_DEFAULT_TIMEOUT 30 /* Timeout in seconds */
474 #ifndef ADA_DEFAULT_RETRY
475 #define ADA_DEFAULT_RETRY 4
478 #ifndef ADA_DEFAULT_SEND_ORDERED
479 #define ADA_DEFAULT_SEND_ORDERED 1
482 #ifndef ADA_DEFAULT_SPINDOWN_SHUTDOWN
483 #define ADA_DEFAULT_SPINDOWN_SHUTDOWN 1
486 #ifndef ADA_DEFAULT_SPINDOWN_SUSPEND
487 #define ADA_DEFAULT_SPINDOWN_SUSPEND 1
490 #ifndef ADA_DEFAULT_READ_AHEAD
491 #define ADA_DEFAULT_READ_AHEAD 1
494 #ifndef ADA_DEFAULT_WRITE_CACHE
495 #define ADA_DEFAULT_WRITE_CACHE 1
498 #define ADA_RA (softc->read_ahead >= 0 ? \
499 softc->read_ahead : ada_read_ahead)
500 #define ADA_WC (softc->write_cache >= 0 ? \
501 softc->write_cache : ada_write_cache)
502 #define ADA_SIO (softc->sort_io_queue >= 0 ? \
503 softc->sort_io_queue : cam_sort_io_queues)
506 * Most platforms map firmware geometry to actual, but some don't. If
507 * not overridden, default to nothing.
509 #ifndef ata_disk_firmware_geom_adjust
510 #define ata_disk_firmware_geom_adjust(disk)
513 static int ada_legacy_aliases = ADA_DEFAULT_LEGACY_ALIASES;
514 static int ada_retry_count = ADA_DEFAULT_RETRY;
515 static int ada_default_timeout = ADA_DEFAULT_TIMEOUT;
516 static int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED;
517 static int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN;
518 static int ada_spindown_suspend = ADA_DEFAULT_SPINDOWN_SUSPEND;
519 static int ada_read_ahead = ADA_DEFAULT_READ_AHEAD;
520 static int ada_write_cache = ADA_DEFAULT_WRITE_CACHE;
522 static SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD, 0,
523 "CAM Direct Access Disk driver");
524 SYSCTL_INT(_kern_cam_ada, OID_AUTO, legacy_aliases, CTLFLAG_RW,
525 &ada_legacy_aliases, 0, "Create legacy-like device aliases");
526 TUNABLE_INT("kern.cam.ada.legacy_aliases", &ada_legacy_aliases);
527 SYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RW,
528 &ada_retry_count, 0, "Normal I/O retry count");
529 TUNABLE_INT("kern.cam.ada.retry_count", &ada_retry_count);
530 SYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RW,
531 &ada_default_timeout, 0, "Normal I/O timeout (in seconds)");
532 TUNABLE_INT("kern.cam.ada.default_timeout", &ada_default_timeout);
533 SYSCTL_INT(_kern_cam_ada, OID_AUTO, send_ordered, CTLFLAG_RW,
534 &ada_send_ordered, 0, "Send Ordered Tags");
535 TUNABLE_INT("kern.cam.ada.send_ordered", &ada_send_ordered);
536 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RW,
537 &ada_spindown_shutdown, 0, "Spin down upon shutdown");
538 TUNABLE_INT("kern.cam.ada.spindown_shutdown", &ada_spindown_shutdown);
539 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_suspend, CTLFLAG_RW,
540 &ada_spindown_suspend, 0, "Spin down upon suspend");
541 TUNABLE_INT("kern.cam.ada.spindown_suspend", &ada_spindown_suspend);
542 SYSCTL_INT(_kern_cam_ada, OID_AUTO, read_ahead, CTLFLAG_RW,
543 &ada_read_ahead, 0, "Enable disk read-ahead");
544 TUNABLE_INT("kern.cam.ada.read_ahead", &ada_read_ahead);
545 SYSCTL_INT(_kern_cam_ada, OID_AUTO, write_cache, CTLFLAG_RW,
546 &ada_write_cache, 0, "Enable disk write cache");
547 TUNABLE_INT("kern.cam.ada.write_cache", &ada_write_cache);
550 * ADA_ORDEREDTAG_INTERVAL determines how often, relative
551 * to the default timeout, we check to see whether an ordered
552 * tagged transaction is appropriate to prevent simple tag
553 * starvation. Since we'd like to ensure that there is at least
554 * 1/2 of the timeout length left for a starved transaction to
555 * complete after we've sent an ordered tag, we must poll at least
556 * four times in every timeout period. This takes care of the worst
557 * case where a starved transaction starts during an interval that
558 * meets the requirement "don't send an ordered tag" test so it takes
559 * us two intervals to determine that a tag must be sent.
561 #ifndef ADA_ORDEREDTAG_INTERVAL
562 #define ADA_ORDEREDTAG_INTERVAL 4
565 static struct periph_driver adadriver =
568 TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0
571 PERIPHDRIVER_DECLARE(ada, adadriver);
573 static MALLOC_DEFINE(M_ATADA, "ata_da", "ata_da buffers");
576 adaopen(struct disk *dp)
578 struct cam_periph *periph;
579 struct ada_softc *softc;
582 periph = (struct cam_periph *)dp->d_drv1;
583 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
587 cam_periph_lock(periph);
588 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
589 cam_periph_unlock(periph);
590 cam_periph_release(periph);
594 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
597 softc = (struct ada_softc *)periph->softc;
598 softc->flags |= ADA_FLAG_OPEN;
600 cam_periph_unhold(periph);
601 cam_periph_unlock(periph);
606 adaclose(struct disk *dp)
608 struct cam_periph *periph;
609 struct ada_softc *softc;
613 periph = (struct cam_periph *)dp->d_drv1;
614 cam_periph_lock(periph);
615 if (cam_periph_hold(periph, PRIBIO) != 0) {
616 cam_periph_unlock(periph);
617 cam_periph_release(periph);
621 softc = (struct ada_softc *)periph->softc;
623 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
626 /* We only sync the cache if the drive is capable of it. */
627 if ((softc->flags & ADA_FLAG_DIRTY) != 0 &&
628 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) != 0 &&
629 (periph->flags & CAM_PERIPH_INVALID) == 0) {
631 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
632 cam_fill_ataio(&ccb->ataio,
639 ada_default_timeout*1000);
641 if (softc->flags & ADA_FLAG_CAN_48BIT)
642 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
644 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
645 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
646 /*sense_flags*/0, softc->disk->d_devstat);
649 xpt_print(periph->path, "Synchronize cache failed\n");
651 softc->flags &= ~ADA_FLAG_DIRTY;
652 xpt_release_ccb(ccb);
655 softc->flags &= ~ADA_FLAG_OPEN;
656 cam_periph_unhold(periph);
657 cam_periph_unlock(periph);
658 cam_periph_release(periph);
663 adaschedule(struct cam_periph *periph)
665 struct ada_softc *softc = (struct ada_softc *)periph->softc;
668 if (softc->state != ADA_STATE_NORMAL)
671 /* Check if cam_periph_getccb() was called. */
672 prio = periph->immediate_priority;
674 /* Check if we have more work to do. */
675 if (bioq_first(&softc->bio_queue) ||
676 (!softc->trim_running && bioq_first(&softc->trim_queue))) {
677 prio = CAM_PRIORITY_NORMAL;
680 /* Schedule CCB if any of above is true. */
681 if (prio != CAM_PRIORITY_NONE)
682 xpt_schedule(periph, prio);
686 * Actually translate the requested transfer into one the physical driver
687 * can understand. The transfer is described by a buf and will include
688 * only one physical transfer.
691 adastrategy(struct bio *bp)
693 struct cam_periph *periph;
694 struct ada_softc *softc;
696 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
697 softc = (struct ada_softc *)periph->softc;
699 cam_periph_lock(periph);
701 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastrategy(%p)\n", bp));
704 * If the device has been made invalid, error out
706 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
707 cam_periph_unlock(periph);
708 biofinish(bp, NULL, ENXIO);
713 * Place it in the queue of disk activities for this disk
715 if (bp->bio_cmd == BIO_DELETE &&
716 (softc->flags & ADA_FLAG_CAN_TRIM)) {
718 bioq_disksort(&softc->trim_queue, bp);
720 bioq_insert_tail(&softc->trim_queue, bp);
723 bioq_disksort(&softc->bio_queue, bp);
725 bioq_insert_tail(&softc->bio_queue, bp);
729 * Schedule ourselves for performing the work.
732 cam_periph_unlock(periph);
738 adadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
740 struct cam_periph *periph;
741 struct ada_softc *softc;
751 softc = (struct ada_softc *)periph->softc;
752 cam_periph_lock(periph);
753 secsize = softc->params.secsize;
754 lba = offset / secsize;
755 count = length / secsize;
757 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
758 cam_periph_unlock(periph);
763 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
764 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
765 cam_fill_ataio(&ccb.ataio,
770 (u_int8_t *) virtual,
772 ada_default_timeout*1000);
773 if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
774 (lba + count >= ATA_MAX_28BIT_LBA ||
776 ata_48bit_cmd(&ccb.ataio, ATA_WRITE_DMA48,
779 ata_28bit_cmd(&ccb.ataio, ATA_WRITE_DMA,
782 xpt_polled_action(&ccb);
784 error = cam_periph_error(&ccb,
785 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
786 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
787 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
788 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
790 printf("Aborting dump due to I/O error.\n");
792 cam_periph_unlock(periph);
796 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) {
797 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
799 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
800 cam_fill_ataio(&ccb.ataio,
807 ada_default_timeout*1000);
809 if (softc->flags & ADA_FLAG_CAN_48BIT)
810 ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0);
812 ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0);
813 xpt_polled_action(&ccb);
815 error = cam_periph_error(&ccb,
816 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
817 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
818 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
819 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
821 xpt_print(periph->path, "Synchronize cache failed\n");
823 cam_periph_unlock(periph);
833 * Install a global async callback. This callback will
834 * receive async callbacks like "new device found".
836 status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL);
838 if (status != CAM_REQ_CMP) {
839 printf("ada: Failed to attach master async callback "
840 "due to status 0x%x!\n", status);
841 } else if (ada_send_ordered) {
843 /* Register our event handlers */
844 if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend,
845 NULL, EVENTHANDLER_PRI_LAST)) == NULL)
846 printf("adainit: power event registration failed!\n");
847 if ((EVENTHANDLER_REGISTER(power_resume, adaresume,
848 NULL, EVENTHANDLER_PRI_LAST)) == NULL)
849 printf("adainit: power event registration failed!\n");
850 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown,
851 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
852 printf("adainit: shutdown event registration failed!\n");
857 * Callback from GEOM, called when it has finished cleaning up its
861 adadiskgonecb(struct disk *dp)
863 struct cam_periph *periph;
865 periph = (struct cam_periph *)dp->d_drv1;
867 cam_periph_release(periph);
871 adaoninvalidate(struct cam_periph *periph)
873 struct ada_softc *softc;
875 softc = (struct ada_softc *)periph->softc;
878 * De-register any async callbacks.
880 xpt_register_async(0, adaasync, periph, periph->path);
883 * Return all queued I/O with ENXIO.
884 * XXX Handle any transactions queued to the card
885 * with XPT_ABORT_CCB.
887 bioq_flush(&softc->bio_queue, NULL, ENXIO);
888 bioq_flush(&softc->trim_queue, NULL, ENXIO);
890 disk_gone(softc->disk);
891 xpt_print(periph->path, "lost device\n");
895 adacleanup(struct cam_periph *periph)
897 struct ada_softc *softc;
899 softc = (struct ada_softc *)periph->softc;
901 xpt_print(periph->path, "removing device entry\n");
902 cam_periph_unlock(periph);
905 * If we can't free the sysctl tree, oh well...
907 if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0
908 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
909 xpt_print(periph->path, "can't remove sysctl context\n");
912 disk_destroy(softc->disk);
913 callout_drain(&softc->sendordered_c);
914 free(softc, M_DEVBUF);
915 cam_periph_lock(periph);
919 adaasync(void *callback_arg, u_int32_t code,
920 struct cam_path *path, void *arg)
922 struct ccb_getdev cgd;
923 struct cam_periph *periph;
924 struct ada_softc *softc;
926 periph = (struct cam_periph *)callback_arg;
928 case AC_FOUND_DEVICE:
930 struct ccb_getdev *cgd;
933 cgd = (struct ccb_getdev *)arg;
937 if (cgd->protocol != PROTO_ATA)
941 * Allocate a peripheral instance for
942 * this device and start the probe
945 status = cam_periph_alloc(adaregister, adaoninvalidate,
946 adacleanup, adastart,
947 "ada", CAM_PERIPH_BIO,
948 cgd->ccb_h.path, adaasync,
949 AC_FOUND_DEVICE, cgd);
951 if (status != CAM_REQ_CMP
952 && status != CAM_REQ_INPROG)
953 printf("adaasync: Unable to attach to new device "
954 "due to status 0x%x\n", status);
957 case AC_GETDEV_CHANGED:
959 softc = (struct ada_softc *)periph->softc;
960 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
961 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
962 xpt_action((union ccb *)&cgd);
964 if ((cgd.ident_data.capabilities1 & ATA_SUPPORT_DMA) &&
965 (cgd.inq_flags & SID_DMA))
966 softc->flags |= ADA_FLAG_CAN_DMA;
968 softc->flags &= ~ADA_FLAG_CAN_DMA;
969 if (cgd.ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) {
970 softc->flags |= ADA_FLAG_CAN_48BIT;
971 if (cgd.inq_flags & SID_DMA48)
972 softc->flags |= ADA_FLAG_CAN_DMA48;
974 softc->flags &= ~ADA_FLAG_CAN_DMA48;
976 softc->flags &= ~(ADA_FLAG_CAN_48BIT |
978 if ((cgd.ident_data.satacapabilities & ATA_SUPPORT_NCQ) &&
979 (cgd.inq_flags & SID_DMA) && (cgd.inq_flags & SID_CmdQue))
980 softc->flags |= ADA_FLAG_CAN_NCQ;
982 softc->flags &= ~ADA_FLAG_CAN_NCQ;
983 if ((cgd.ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) &&
984 (cgd.inq_flags & SID_DMA))
985 softc->flags |= ADA_FLAG_CAN_TRIM;
987 softc->flags &= ~ADA_FLAG_CAN_TRIM;
989 cam_periph_async(periph, code, path, arg);
992 case AC_ADVINFO_CHANGED:
996 buftype = (uintptr_t)arg;
997 if (buftype == CDAI_TYPE_PHYS_PATH) {
998 struct ada_softc *softc;
1000 softc = periph->softc;
1001 disk_attr_changed(softc->disk, "GEOM::physpath",
1009 softc = (struct ada_softc *)periph->softc;
1010 cam_periph_async(periph, code, path, arg);
1011 if (softc->state != ADA_STATE_NORMAL)
1013 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1014 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1015 xpt_action((union ccb *)&cgd);
1017 cgd.ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD)
1018 softc->state = ADA_STATE_RAHEAD;
1019 else if (ADA_WC >= 0 &&
1020 cgd.ident_data.support.command1 & ATA_SUPPORT_WRITECACHE)
1021 softc->state = ADA_STATE_WCACHE;
1024 cam_periph_acquire(periph);
1025 cam_freeze_devq_arg(periph->path,
1026 RELSIM_RELEASE_RUNLEVEL, CAM_RL_DEV + 1);
1027 xpt_schedule(periph, CAM_PRIORITY_DEV);
1030 cam_periph_async(periph, code, path, arg);
1036 adasysctlinit(void *context, int pending)
1038 struct cam_periph *periph;
1039 struct ada_softc *softc;
1040 char tmpstr[80], tmpstr2[80];
1042 periph = (struct cam_periph *)context;
1044 /* periph was held for us when this task was enqueued */
1045 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
1046 cam_periph_release(periph);
1050 softc = (struct ada_softc *)periph->softc;
1051 snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d", periph->unit_number);
1052 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1054 sysctl_ctx_init(&softc->sysctl_ctx);
1055 softc->flags |= ADA_FLAG_SCTX_INIT;
1056 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1057 SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2,
1058 CTLFLAG_RD, 0, tmpstr);
1059 if (softc->sysctl_tree == NULL) {
1060 printf("adasysctlinit: unable to allocate sysctl tree\n");
1061 cam_periph_release(periph);
1065 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1066 OID_AUTO, "read_ahead", CTLFLAG_RW | CTLFLAG_MPSAFE,
1067 &softc->read_ahead, 0, "Enable disk read ahead.");
1068 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1069 OID_AUTO, "write_cache", CTLFLAG_RW | CTLFLAG_MPSAFE,
1070 &softc->write_cache, 0, "Enable disk write cache.");
1071 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1072 OID_AUTO, "sort_io_queue", CTLFLAG_RW | CTLFLAG_MPSAFE,
1073 &softc->sort_io_queue, 0,
1074 "Sort IO queue to try and optimise disk access patterns");
1075 #ifdef ADA_TEST_FAILURE
1077 * Add a 'door bell' sysctl which allows one to set it from userland
1078 * and cause something bad to happen. For the moment, we only allow
1079 * whacking the next read or write.
1081 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1082 OID_AUTO, "force_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1083 &softc->force_read_error, 0,
1084 "Force a read error for the next N reads.");
1085 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1086 OID_AUTO, "force_write_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1087 &softc->force_write_error, 0,
1088 "Force a write error for the next N writes.");
1089 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1090 OID_AUTO, "periodic_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1091 &softc->periodic_read_error, 0,
1092 "Force a read error every N reads (don't set too low).");
1094 cam_periph_release(periph);
1098 adagetattr(struct bio *bp)
1101 struct cam_periph *periph;
1103 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1104 cam_periph_lock(periph);
1105 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1107 cam_periph_unlock(periph);
1109 bp->bio_completed = bp->bio_length;
1114 adaregister(struct cam_periph *periph, void *arg)
1116 struct ada_softc *softc;
1117 struct ccb_pathinq cpi;
1118 struct ccb_getdev *cgd;
1119 char announce_buf[80], buf1[32];
1120 struct disk_params *dp;
1123 int legacy_id, quirks;
1125 cgd = (struct ccb_getdev *)arg;
1127 printf("adaregister: no getdev CCB, can't register device\n");
1128 return(CAM_REQ_CMP_ERR);
1131 softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF,
1134 if (softc == NULL) {
1135 printf("adaregister: Unable to probe new device. "
1136 "Unable to allocate softc\n");
1137 return(CAM_REQ_CMP_ERR);
1140 bioq_init(&softc->bio_queue);
1141 bioq_init(&softc->trim_queue);
1143 if ((cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA) &&
1144 (cgd->inq_flags & SID_DMA))
1145 softc->flags |= ADA_FLAG_CAN_DMA;
1146 if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) {
1147 softc->flags |= ADA_FLAG_CAN_48BIT;
1148 if (cgd->inq_flags & SID_DMA48)
1149 softc->flags |= ADA_FLAG_CAN_DMA48;
1151 if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE)
1152 softc->flags |= ADA_FLAG_CAN_FLUSHCACHE;
1153 if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT)
1154 softc->flags |= ADA_FLAG_CAN_POWERMGT;
1155 if ((cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ) &&
1156 (cgd->inq_flags & SID_DMA) && (cgd->inq_flags & SID_CmdQue))
1157 softc->flags |= ADA_FLAG_CAN_NCQ;
1158 if ((cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) &&
1159 (cgd->inq_flags & SID_DMA)) {
1160 softc->flags |= ADA_FLAG_CAN_TRIM;
1161 softc->trim_max_ranges = TRIM_MAX_RANGES;
1162 if (cgd->ident_data.max_dsm_blocks != 0) {
1163 softc->trim_max_ranges =
1164 min(cgd->ident_data.max_dsm_blocks *
1165 ATA_DSM_BLK_RANGES, softc->trim_max_ranges);
1168 if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA)
1169 softc->flags |= ADA_FLAG_CAN_CFA;
1171 periph->softc = softc;
1174 * See if this device has any quirks.
1176 match = cam_quirkmatch((caddr_t)&cgd->ident_data,
1177 (caddr_t)ada_quirk_table,
1178 sizeof(ada_quirk_table)/sizeof(*ada_quirk_table),
1179 sizeof(*ada_quirk_table), ata_identify_match);
1181 softc->quirks = ((struct ada_quirk_entry *)match)->quirks;
1183 softc->quirks = ADA_Q_NONE;
1185 bzero(&cpi, sizeof(cpi));
1186 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE);
1187 cpi.ccb_h.func_code = XPT_PATH_INQ;
1188 xpt_action((union ccb *)&cpi);
1190 TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph);
1193 * Register this media as a disk
1195 (void)cam_periph_hold(periph, PRIBIO);
1196 cam_periph_unlock(periph);
1197 snprintf(announce_buf, sizeof(announce_buf),
1198 "kern.cam.ada.%d.quirks", periph->unit_number);
1199 quirks = softc->quirks;
1200 TUNABLE_INT_FETCH(announce_buf, &quirks);
1201 softc->quirks = quirks;
1202 softc->read_ahead = -1;
1203 snprintf(announce_buf, sizeof(announce_buf),
1204 "kern.cam.ada.%d.read_ahead", periph->unit_number);
1205 TUNABLE_INT_FETCH(announce_buf, &softc->read_ahead);
1206 softc->write_cache = -1;
1207 snprintf(announce_buf, sizeof(announce_buf),
1208 "kern.cam.ada.%d.write_cache", periph->unit_number);
1209 TUNABLE_INT_FETCH(announce_buf, &softc->write_cache);
1210 /* Disable queue sorting for non-rotational media by default. */
1211 if (cgd->ident_data.media_rotation_rate == 1)
1212 softc->sort_io_queue = 0;
1214 softc->sort_io_queue = -1;
1215 adagetparams(periph, cgd);
1216 softc->disk = disk_alloc();
1217 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
1218 periph->unit_number, softc->params.secsize,
1219 DEVSTAT_ALL_SUPPORTED,
1220 DEVSTAT_TYPE_DIRECT |
1221 XPORT_DEVSTAT_TYPE(cpi.transport),
1222 DEVSTAT_PRIORITY_DISK);
1223 softc->disk->d_open = adaopen;
1224 softc->disk->d_close = adaclose;
1225 softc->disk->d_strategy = adastrategy;
1226 softc->disk->d_getattr = adagetattr;
1227 softc->disk->d_dump = adadump;
1228 softc->disk->d_gone = adadiskgonecb;
1229 softc->disk->d_name = "ada";
1230 softc->disk->d_drv1 = periph;
1231 maxio = cpi.maxio; /* Honor max I/O size of SIM */
1233 maxio = DFLTPHYS; /* traditional default */
1234 else if (maxio > MAXPHYS)
1235 maxio = MAXPHYS; /* for safety */
1236 if (softc->flags & ADA_FLAG_CAN_48BIT)
1237 maxio = min(maxio, 65536 * softc->params.secsize);
1238 else /* 28bit ATA command limit */
1239 maxio = min(maxio, 256 * softc->params.secsize);
1240 softc->disk->d_maxsize = maxio;
1241 softc->disk->d_unit = periph->unit_number;
1242 softc->disk->d_flags = 0;
1243 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE)
1244 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
1245 if (softc->flags & ADA_FLAG_CAN_TRIM) {
1246 softc->disk->d_flags |= DISKFLAG_CANDELETE;
1247 softc->disk->d_delmaxsize = softc->params.secsize *
1249 softc->trim_max_ranges;
1250 } else if ((softc->flags & ADA_FLAG_CAN_CFA) &&
1251 !(softc->flags & ADA_FLAG_CAN_48BIT)) {
1252 softc->disk->d_flags |= DISKFLAG_CANDELETE;
1253 softc->disk->d_delmaxsize = 256 * softc->params.secsize;
1255 softc->disk->d_delmaxsize = maxio;
1256 if ((cpi.hba_misc & PIM_UNMAPPED) != 0)
1257 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
1258 strlcpy(softc->disk->d_descr, cgd->ident_data.model,
1259 MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model)));
1260 strlcpy(softc->disk->d_ident, cgd->ident_data.serial,
1261 MIN(sizeof(softc->disk->d_ident), sizeof(cgd->ident_data.serial)));
1262 softc->disk->d_hba_vendor = cpi.hba_vendor;
1263 softc->disk->d_hba_device = cpi.hba_device;
1264 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
1265 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
1267 softc->disk->d_sectorsize = softc->params.secsize;
1268 softc->disk->d_mediasize = (off_t)softc->params.sectors *
1269 softc->params.secsize;
1270 if (ata_physical_sector_size(&cgd->ident_data) !=
1271 softc->params.secsize) {
1272 softc->disk->d_stripesize =
1273 ata_physical_sector_size(&cgd->ident_data);
1274 softc->disk->d_stripeoffset = (softc->disk->d_stripesize -
1275 ata_logical_sector_offset(&cgd->ident_data)) %
1276 softc->disk->d_stripesize;
1277 } else if (softc->quirks & ADA_Q_4K) {
1278 softc->disk->d_stripesize = 4096;
1279 softc->disk->d_stripeoffset = 0;
1281 softc->disk->d_fwsectors = softc->params.secs_per_track;
1282 softc->disk->d_fwheads = softc->params.heads;
1283 ata_disk_firmware_geom_adjust(softc->disk);
1285 if (ada_legacy_aliases) {
1286 #ifdef ATA_STATIC_ID
1287 legacy_id = xpt_path_legacy_ata_id(periph->path);
1289 legacy_id = softc->disk->d_unit;
1291 if (legacy_id >= 0) {
1292 snprintf(announce_buf, sizeof(announce_buf),
1293 "kern.devalias.%s%d",
1294 softc->disk->d_name, softc->disk->d_unit);
1295 snprintf(buf1, sizeof(buf1),
1297 setenv(announce_buf, buf1);
1302 * Acquire a reference to the periph before we register with GEOM.
1303 * We'll release this reference once GEOM calls us back (via
1304 * adadiskgonecb()) telling us that our provider has been freed.
1306 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1307 xpt_print(periph->path, "%s: lost periph during "
1308 "registration!\n", __func__);
1309 cam_periph_lock(periph);
1310 return (CAM_REQ_CMP_ERR);
1312 disk_create(softc->disk, DISK_VERSION);
1313 cam_periph_lock(periph);
1314 cam_periph_unhold(periph);
1316 dp = &softc->params;
1317 snprintf(announce_buf, sizeof(announce_buf),
1318 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1319 (uintmax_t)(((uintmax_t)dp->secsize *
1320 dp->sectors) / (1024*1024)),
1321 (uintmax_t)dp->sectors,
1322 dp->secsize, dp->heads,
1323 dp->secs_per_track, dp->cylinders);
1324 xpt_announce_periph(periph, announce_buf);
1325 xpt_announce_quirks(periph, softc->quirks, ADA_Q_BIT_STRING);
1327 printf("%s%d: Previously was known as ad%d\n",
1328 periph->periph_name, periph->unit_number, legacy_id);
1331 * Create our sysctl variables, now that we know
1332 * we have successfully attached.
1334 cam_periph_acquire(periph);
1335 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task);
1338 * Add async callbacks for bus reset and
1339 * bus device reset calls. I don't bother
1340 * checking if this fails as, in most cases,
1341 * the system will function just fine without
1342 * them and the only alternative would be to
1343 * not attach the device on failure.
1345 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
1346 AC_GETDEV_CHANGED | AC_ADVINFO_CHANGED,
1347 adaasync, periph, periph->path);
1350 * Schedule a periodic event to occasionally send an
1351 * ordered tag to a device.
1353 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
1354 callout_reset(&softc->sendordered_c,
1355 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL,
1356 adasendorderedtag, softc);
1359 cgd->ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD) {
1360 softc->state = ADA_STATE_RAHEAD;
1361 cam_periph_acquire(periph);
1362 cam_freeze_devq_arg(periph->path,
1363 RELSIM_RELEASE_RUNLEVEL, CAM_RL_DEV + 1);
1364 xpt_schedule(periph, CAM_PRIORITY_DEV);
1365 } else if (ADA_WC >= 0 &&
1366 cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) {
1367 softc->state = ADA_STATE_WCACHE;
1368 cam_periph_acquire(periph);
1369 cam_freeze_devq_arg(periph->path,
1370 RELSIM_RELEASE_RUNLEVEL, CAM_RL_DEV + 1);
1371 xpt_schedule(periph, CAM_PRIORITY_DEV);
1373 softc->state = ADA_STATE_NORMAL;
1375 return(CAM_REQ_CMP);
1379 adastart(struct cam_periph *periph, union ccb *start_ccb)
1381 struct ada_softc *softc = (struct ada_softc *)periph->softc;
1382 struct ccb_ataio *ataio = &start_ccb->ataio;
1384 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastart\n"));
1386 switch (softc->state) {
1387 case ADA_STATE_NORMAL:
1392 /* Execute immediate CCB if waiting. */
1393 if (periph->immediate_priority <= periph->pinfo.priority) {
1394 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1395 ("queuing for immediate ccb\n"));
1396 start_ccb->ccb_h.ccb_state = ADA_CCB_WAITING;
1397 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1399 periph->immediate_priority = CAM_PRIORITY_NONE;
1400 wakeup(&periph->ccb_list);
1401 /* Have more work to do, so ensure we stay scheduled */
1402 adaschedule(periph);
1405 /* Run TRIM if not running yet. */
1406 if (!softc->trim_running &&
1407 (bp = bioq_first(&softc->trim_queue)) != 0) {
1408 struct trim_request *req = &softc->trim_req;
1410 uint64_t lastlba = (uint64_t)-1;
1411 int bps = 0, c, lastcount = 0, off, ranges = 0;
1413 softc->trim_running = 1;
1414 bzero(req, sizeof(*req));
1417 uint64_t lba = bp1->bio_pblkno;
1418 int count = bp1->bio_bcount /
1419 softc->params.secsize;
1421 bioq_remove(&softc->trim_queue, bp1);
1423 /* Try to extend the previous range. */
1424 if (lba == lastlba) {
1425 c = min(count, ATA_DSM_RANGE_MAX - lastcount);
1427 off = (ranges - 1) * ATA_DSM_RANGE_SIZE;
1428 req->data[off + 6] = lastcount & 0xff;
1429 req->data[off + 7] =
1430 (lastcount >> 8) & 0xff;
1436 c = min(count, ATA_DSM_RANGE_MAX);
1437 off = ranges * ATA_DSM_RANGE_SIZE;
1438 req->data[off + 0] = lba & 0xff;
1439 req->data[off + 1] = (lba >> 8) & 0xff;
1440 req->data[off + 2] = (lba >> 16) & 0xff;
1441 req->data[off + 3] = (lba >> 24) & 0xff;
1442 req->data[off + 4] = (lba >> 32) & 0xff;
1443 req->data[off + 5] = (lba >> 40) & 0xff;
1444 req->data[off + 6] = c & 0xff;
1445 req->data[off + 7] = (c >> 8) & 0xff;
1451 * Its the caller's responsibility to ensure the
1452 * request will fit so we don't need to check for
1457 req->bps[bps++] = bp1;
1458 bp1 = bioq_first(&softc->trim_queue);
1459 if (bps >= TRIM_MAX_BIOS ||
1461 bp1->bio_bcount / softc->params.secsize >
1462 (softc->trim_max_ranges - ranges) *
1466 cam_fill_ataio(ataio,
1472 ((ranges + ATA_DSM_BLK_RANGES - 1) /
1473 ATA_DSM_BLK_RANGES) * ATA_DSM_BLK_SIZE,
1474 ada_default_timeout * 1000);
1475 ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT,
1476 ATA_DSM_TRIM, 0, (ranges + ATA_DSM_BLK_RANGES -
1477 1) / ATA_DSM_BLK_RANGES);
1478 start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM;
1481 /* Run regular command. */
1482 bp = bioq_first(&softc->bio_queue);
1484 xpt_release_ccb(start_ccb);
1487 bioq_remove(&softc->bio_queue, bp);
1489 if ((bp->bio_flags & BIO_ORDERED) != 0
1490 || (softc->flags & ADA_FLAG_NEED_OTAG) != 0) {
1491 softc->flags &= ~ADA_FLAG_NEED_OTAG;
1492 softc->ordered_tag_count++;
1497 switch (bp->bio_cmd) {
1499 softc->flags |= ADA_FLAG_DIRTY;
1503 uint64_t lba = bp->bio_pblkno;
1504 uint16_t count = bp->bio_bcount / softc->params.secsize;
1505 #ifdef ADA_TEST_FAILURE
1509 * Support the failure ioctls. If the command is a
1510 * read, and there are pending forced read errors, or
1511 * if a write and pending write errors, then fail this
1512 * operation with EIO. This is useful for testing
1513 * purposes. Also, support having every Nth read fail.
1515 * This is a rather blunt tool.
1517 if (bp->bio_cmd == BIO_READ) {
1518 if (softc->force_read_error) {
1519 softc->force_read_error--;
1522 if (softc->periodic_read_error > 0) {
1523 if (++softc->periodic_read_count >=
1524 softc->periodic_read_error) {
1525 softc->periodic_read_count = 0;
1530 if (softc->force_write_error) {
1531 softc->force_write_error--;
1536 bp->bio_error = EIO;
1537 bp->bio_flags |= BIO_ERROR;
1539 xpt_release_ccb(start_ccb);
1540 adaschedule(periph);
1544 KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 ||
1545 round_page(bp->bio_bcount + bp->bio_ma_offset) /
1546 PAGE_SIZE == bp->bio_ma_n,
1547 ("Short bio %p", bp));
1548 cam_fill_ataio(ataio,
1551 (bp->bio_cmd == BIO_READ ? CAM_DIR_IN :
1552 CAM_DIR_OUT) | ((bp->bio_flags & BIO_UNMAPPED)
1553 != 0 ? CAM_DATA_BIO : 0),
1555 ((bp->bio_flags & BIO_UNMAPPED) != 0) ? (void *)bp :
1558 ada_default_timeout*1000);
1560 if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) {
1561 if (bp->bio_cmd == BIO_READ) {
1562 ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED,
1565 ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED,
1568 } else if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
1569 (lba + count >= ATA_MAX_28BIT_LBA ||
1571 if (softc->flags & ADA_FLAG_CAN_DMA48) {
1572 if (bp->bio_cmd == BIO_READ) {
1573 ata_48bit_cmd(ataio, ATA_READ_DMA48,
1576 ata_48bit_cmd(ataio, ATA_WRITE_DMA48,
1580 if (bp->bio_cmd == BIO_READ) {
1581 ata_48bit_cmd(ataio, ATA_READ_MUL48,
1584 ata_48bit_cmd(ataio, ATA_WRITE_MUL48,
1591 if (softc->flags & ADA_FLAG_CAN_DMA) {
1592 if (bp->bio_cmd == BIO_READ) {
1593 ata_28bit_cmd(ataio, ATA_READ_DMA,
1596 ata_28bit_cmd(ataio, ATA_WRITE_DMA,
1600 if (bp->bio_cmd == BIO_READ) {
1601 ata_28bit_cmd(ataio, ATA_READ_MUL,
1604 ata_28bit_cmd(ataio, ATA_WRITE_MUL,
1613 uint64_t lba = bp->bio_pblkno;
1614 uint16_t count = bp->bio_bcount / softc->params.secsize;
1616 cam_fill_ataio(ataio,
1623 ada_default_timeout*1000);
1627 ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count);
1631 cam_fill_ataio(ataio,
1638 ada_default_timeout*1000);
1640 if (softc->flags & ADA_FLAG_CAN_48BIT)
1641 ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1643 ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0);
1646 start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO;
1648 start_ccb->ccb_h.ccb_bp = bp;
1649 softc->outstanding_cmds++;
1650 xpt_action(start_ccb);
1652 /* May have more work to do, so ensure we stay scheduled */
1653 adaschedule(periph);
1656 case ADA_STATE_RAHEAD:
1657 case ADA_STATE_WCACHE:
1659 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
1660 softc->state = ADA_STATE_NORMAL;
1661 xpt_release_ccb(start_ccb);
1662 cam_release_devq(periph->path,
1663 RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_DEV + 1, FALSE);
1664 adaschedule(periph);
1665 cam_periph_release_locked(periph);
1669 cam_fill_ataio(ataio,
1676 ada_default_timeout*1000);
1678 if (softc->state == ADA_STATE_RAHEAD) {
1679 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_RA ?
1680 ATA_SF_ENAB_RCACHE : ATA_SF_DIS_RCACHE, 0, 0);
1681 start_ccb->ccb_h.ccb_state = ADA_CCB_RAHEAD;
1683 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_WC ?
1684 ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0);
1685 start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE;
1687 xpt_action(start_ccb);
1694 adadone(struct cam_periph *periph, union ccb *done_ccb)
1696 struct ada_softc *softc;
1697 struct ccb_ataio *ataio;
1698 struct ccb_getdev *cgd;
1700 softc = (struct ada_softc *)periph->softc;
1701 ataio = &done_ccb->ataio;
1703 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adadone\n"));
1705 switch (ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) {
1706 case ADA_CCB_BUFFER_IO:
1711 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1712 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1715 error = adaerror(done_ccb, 0, 0);
1716 if (error == ERESTART) {
1717 /* A retry was scheduled, so just return. */
1721 bp->bio_error = error;
1722 bp->bio_resid = bp->bio_bcount;
1723 bp->bio_flags |= BIO_ERROR;
1725 bp->bio_resid = ataio->resid;
1727 if (bp->bio_resid != 0)
1728 bp->bio_flags |= BIO_ERROR;
1730 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1731 cam_release_devq(done_ccb->ccb_h.path,
1735 /*getcount_only*/0);
1737 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1738 panic("REQ_CMP with QFRZN");
1739 bp->bio_resid = ataio->resid;
1740 if (ataio->resid > 0)
1741 bp->bio_flags |= BIO_ERROR;
1743 softc->outstanding_cmds--;
1744 if (softc->outstanding_cmds == 0)
1745 softc->flags |= ADA_FLAG_WENT_IDLE;
1746 if ((ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) ==
1748 struct trim_request *req =
1749 (struct trim_request *)ataio->data_ptr;
1752 for (i = 1; i < TRIM_MAX_BIOS && req->bps[i]; i++) {
1753 struct bio *bp1 = req->bps[i];
1755 bp1->bio_resid = bp->bio_resid;
1756 bp1->bio_error = bp->bio_error;
1757 if (bp->bio_flags & BIO_ERROR)
1758 bp1->bio_flags |= BIO_ERROR;
1761 softc->trim_running = 0;
1763 adaschedule(periph);
1768 case ADA_CCB_RAHEAD:
1770 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1771 if (adaerror(done_ccb, 0, 0) == ERESTART) {
1773 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1774 cam_release_devq(done_ccb->ccb_h.path,
1778 /*getcount_only*/0);
1783 * Since our peripheral may be invalidated by an error
1784 * above or an external event, we must release our CCB
1785 * before releasing the reference on the peripheral.
1786 * The peripheral will only go away once the last reference
1787 * is removed, and we need it around for the CCB release
1790 cgd = (struct ccb_getdev *)done_ccb;
1791 xpt_setup_ccb(&cgd->ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1792 cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1793 xpt_action((union ccb *)cgd);
1795 cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) {
1796 softc->state = ADA_STATE_WCACHE;
1797 xpt_release_ccb(done_ccb);
1798 xpt_schedule(periph, CAM_PRIORITY_DEV);
1801 softc->state = ADA_STATE_NORMAL;
1802 xpt_release_ccb(done_ccb);
1803 cam_release_devq(periph->path,
1804 RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_DEV + 1, FALSE);
1805 adaschedule(periph);
1806 cam_periph_release_locked(periph);
1809 case ADA_CCB_WCACHE:
1811 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1812 if (adaerror(done_ccb, 0, 0) == ERESTART) {
1814 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1815 cam_release_devq(done_ccb->ccb_h.path,
1819 /*getcount_only*/0);
1823 softc->state = ADA_STATE_NORMAL;
1825 * Since our peripheral may be invalidated by an error
1826 * above or an external event, we must release our CCB
1827 * before releasing the reference on the peripheral.
1828 * The peripheral will only go away once the last reference
1829 * is removed, and we need it around for the CCB release
1832 xpt_release_ccb(done_ccb);
1833 cam_release_devq(periph->path,
1834 RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_DEV + 1, FALSE);
1835 adaschedule(periph);
1836 cam_periph_release_locked(periph);
1839 case ADA_CCB_WAITING:
1841 /* Caller will release the CCB */
1842 wakeup(&done_ccb->ccb_h.cbfcnp);
1846 /* No-op. We're polling */
1851 xpt_release_ccb(done_ccb);
1855 adaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1858 return(cam_periph_error(ccb, cam_flags, sense_flags, NULL));
1862 adagetparams(struct cam_periph *periph, struct ccb_getdev *cgd)
1864 struct ada_softc *softc = (struct ada_softc *)periph->softc;
1865 struct disk_params *dp = &softc->params;
1866 u_int64_t lbasize48;
1869 dp->secsize = ata_logical_sector_size(&cgd->ident_data);
1870 if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) &&
1871 cgd->ident_data.current_heads && cgd->ident_data.current_sectors) {
1872 dp->heads = cgd->ident_data.current_heads;
1873 dp->secs_per_track = cgd->ident_data.current_sectors;
1874 dp->cylinders = cgd->ident_data.cylinders;
1875 dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 |
1876 ((u_int32_t)cgd->ident_data.current_size_2 << 16);
1878 dp->heads = cgd->ident_data.heads;
1879 dp->secs_per_track = cgd->ident_data.sectors;
1880 dp->cylinders = cgd->ident_data.cylinders;
1881 dp->sectors = cgd->ident_data.cylinders * dp->heads * dp->secs_per_track;
1883 lbasize = (u_int32_t)cgd->ident_data.lba_size_1 |
1884 ((u_int32_t)cgd->ident_data.lba_size_2 << 16);
1886 /* use the 28bit LBA size if valid or bigger than the CHS mapping */
1887 if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize)
1888 dp->sectors = lbasize;
1890 /* use the 48bit LBA size if valid */
1891 lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) |
1892 ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) |
1893 ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) |
1894 ((u_int64_t)cgd->ident_data.lba_size48_4 << 48);
1895 if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) &&
1896 lbasize48 > ATA_MAX_28BIT_LBA)
1897 dp->sectors = lbasize48;
1901 adasendorderedtag(void *arg)
1903 struct ada_softc *softc = arg;
1905 if (ada_send_ordered) {
1906 if ((softc->ordered_tag_count == 0)
1907 && ((softc->flags & ADA_FLAG_WENT_IDLE) == 0)) {
1908 softc->flags |= ADA_FLAG_NEED_OTAG;
1910 if (softc->outstanding_cmds > 0)
1911 softc->flags &= ~ADA_FLAG_WENT_IDLE;
1913 softc->ordered_tag_count = 0;
1915 /* Queue us up again */
1916 callout_reset(&softc->sendordered_c,
1917 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL,
1918 adasendorderedtag, softc);
1922 * Step through all ADA peripheral drivers, and if the device is still open,
1923 * sync the disk cache to physical media.
1928 struct cam_periph *periph;
1929 struct ada_softc *softc;
1933 CAM_PERIPH_FOREACH(periph, &adadriver) {
1934 softc = (struct ada_softc *)periph->softc;
1935 if (SCHEDULER_STOPPED()) {
1936 /* If we paniced with the lock held, do not recurse. */
1937 if (!cam_periph_owned(periph) &&
1938 (softc->flags & ADA_FLAG_OPEN)) {
1939 adadump(softc->disk, NULL, 0, 0, 0);
1943 cam_periph_lock(periph);
1945 * We only sync the cache if the drive is still open, and
1946 * if the drive is capable of it..
1948 if (((softc->flags & ADA_FLAG_OPEN) == 0) ||
1949 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) {
1950 cam_periph_unlock(periph);
1954 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1955 cam_fill_ataio(&ccb->ataio,
1962 ada_default_timeout*1000);
1963 if (softc->flags & ADA_FLAG_CAN_48BIT)
1964 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1966 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
1968 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
1969 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
1970 softc->disk->d_devstat);
1972 xpt_print(periph->path, "Synchronize cache failed\n");
1973 xpt_release_ccb(ccb);
1974 cam_periph_unlock(periph);
1979 adaspindown(uint8_t cmd, int flags)
1981 struct cam_periph *periph;
1982 struct ada_softc *softc;
1986 CAM_PERIPH_FOREACH(periph, &adadriver) {
1987 /* If we paniced with lock held - not recurse here. */
1988 if (cam_periph_owned(periph))
1990 cam_periph_lock(periph);
1991 softc = (struct ada_softc *)periph->softc;
1993 * We only spin-down the drive if it is capable of it..
1995 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
1996 cam_periph_unlock(periph);
2001 xpt_print(periph->path, "spin-down\n");
2003 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2004 cam_fill_ataio(&ccb->ataio,
2007 CAM_DIR_NONE | flags,
2011 ada_default_timeout*1000);
2012 ata_28bit_cmd(&ccb->ataio, cmd, 0, 0, 0);
2014 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
2015 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
2016 softc->disk->d_devstat);
2018 xpt_print(periph->path, "Spin-down disk failed\n");
2019 xpt_release_ccb(ccb);
2020 cam_periph_unlock(periph);
2025 adashutdown(void *arg, int howto)
2029 if (ada_spindown_shutdown != 0 &&
2030 (howto & (RB_HALT | RB_POWEROFF)) != 0)
2031 adaspindown(ATA_STANDBY_IMMEDIATE, 0);
2035 adasuspend(void *arg)
2039 if (ada_spindown_suspend != 0)
2040 adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE);
2044 adaresume(void *arg)
2046 struct cam_periph *periph;
2047 struct ada_softc *softc;
2049 if (ada_spindown_suspend == 0)
2052 CAM_PERIPH_FOREACH(periph, &adadriver) {
2053 cam_periph_lock(periph);
2054 softc = (struct ada_softc *)periph->softc;
2056 * We only spin-down the drive if it is capable of it..
2058 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
2059 cam_periph_unlock(periph);
2064 xpt_print(periph->path, "resume\n");
2067 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on
2070 cam_release_devq(periph->path,
2074 /*getcount_only*/0);
2076 cam_periph_unlock(periph);
2080 #endif /* _KERNEL */