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>
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_PACK_INVALID = 0x001,
79 ADA_FLAG_CAN_48BIT = 0x002,
80 ADA_FLAG_CAN_FLUSHCACHE = 0x004,
81 ADA_FLAG_CAN_NCQ = 0x008,
82 ADA_FLAG_CAN_DMA = 0x010,
83 ADA_FLAG_NEED_OTAG = 0x020,
84 ADA_FLAG_WENT_IDLE = 0x040,
85 ADA_FLAG_CAN_TRIM = 0x080,
86 ADA_FLAG_OPEN = 0x100,
87 ADA_FLAG_SCTX_INIT = 0x200,
88 ADA_FLAG_CAN_CFA = 0x400,
89 ADA_FLAG_CAN_POWERMGT = 0x800
98 ADA_CCB_RAHEAD = 0x01,
99 ADA_CCB_WCACHE = 0x02,
100 ADA_CCB_BUFFER_IO = 0x03,
101 ADA_CCB_WAITING = 0x04,
104 ADA_CCB_TYPE_MASK = 0x0F,
107 /* Offsets into our private area for storing information */
108 #define ccb_state ppriv_field0
109 #define ccb_bp ppriv_ptr1
113 u_int8_t secs_per_track;
115 u_int32_t secsize; /* Number of bytes/logical sector */
116 u_int64_t sectors; /* Total number sectors */
119 #define TRIM_MAX_BLOCKS 8
120 #define TRIM_MAX_RANGES (TRIM_MAX_BLOCKS * 64)
121 #define TRIM_MAX_BIOS (TRIM_MAX_RANGES * 4)
122 struct trim_request {
123 uint8_t data[TRIM_MAX_RANGES * 8];
124 struct bio *bps[TRIM_MAX_BIOS];
128 struct bio_queue_head bio_queue;
129 struct bio_queue_head trim_queue;
134 int ordered_tag_count;
135 int outstanding_cmds;
140 #ifdef ADA_TEST_FAILURE
141 int force_read_error;
142 int force_write_error;
143 int periodic_read_error;
144 int periodic_read_count;
146 struct disk_params params;
148 struct task sysctl_task;
149 struct sysctl_ctx_list sysctl_ctx;
150 struct sysctl_oid *sysctl_tree;
151 struct callout sendordered_c;
152 struct trim_request trim_req;
155 struct ada_quirk_entry {
156 struct scsi_inquiry_pattern inq_pat;
160 static struct ada_quirk_entry ada_quirk_table[] =
163 /* Hitachi Advanced Format (4k) drives */
164 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Hitachi H??????????E3*", "*" },
168 /* Samsung Advanced Format (4k) drives */
169 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD155UI*", "*" },
173 /* Samsung Advanced Format (4k) drives */
174 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD204UI*", "*" },
178 /* Seagate Barracuda Green Advanced Format (4k) drives */
179 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DL*", "*" },
183 /* Seagate Barracuda Advanced Format (4k) drives */
184 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???DM*", "*" },
188 /* Seagate Barracuda Advanced Format (4k) drives */
189 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DM*", "*" },
193 /* Seagate Momentus Advanced Format (4k) drives */
194 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500423AS*", "*" },
198 /* Seagate Momentus Advanced Format (4k) drives */
199 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500424AS*", "*" },
203 /* Seagate Momentus Advanced Format (4k) drives */
204 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640423AS*", "*" },
208 /* Seagate Momentus Advanced Format (4k) drives */
209 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640424AS*", "*" },
213 /* Seagate Momentus Advanced Format (4k) drives */
214 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750420AS*", "*" },
218 /* Seagate Momentus Advanced Format (4k) drives */
219 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750422AS*", "*" },
223 /* Seagate Momentus Advanced Format (4k) drives */
224 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750423AS*", "*" },
228 /* Seagate Momentus Thin Advanced Format (4k) drives */
229 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???LT*", "*" },
233 /* WDC Caviar Green Advanced Format (4k) drives */
234 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RS*", "*" },
238 /* WDC Caviar Green Advanced Format (4k) drives */
239 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RX*", "*" },
243 /* WDC Caviar Green Advanced Format (4k) drives */
244 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RS*", "*" },
248 /* WDC Caviar Green Advanced Format (4k) drives */
249 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RX*", "*" },
253 /* WDC Scorpio Black Advanced Format (4k) drives */
254 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PKT*", "*" },
258 /* WDC Scorpio Black Advanced Format (4k) drives */
259 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PKT*", "*" },
263 /* WDC Scorpio Blue Advanced Format (4k) drives */
264 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PVT*", "*" },
268 /* WDC Scorpio Blue Advanced Format (4k) drives */
269 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PVT*", "*" },
274 * Corsair Force 2 SSDs
275 * 4k optimised & trim only works in 4k requests + 4k aligned
276 * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk>
279 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair CSSD-F*", "*" },
284 * Corsair Force 3 SSDs
285 * 4k optimised & trim only works in 4k requests + 4k aligned
286 * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk>
289 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force 3*", "*" },
295 * 4k optimised & trim only works in 4k requests + 4k aligned
296 * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk>
299 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY3*", "*" },
304 * OCZ Vertex 2 SSDs (inc pro series)
305 * 4k optimised & trim only works in 4k requests + 4k aligned
306 * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk>
309 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ?VERTEX2*", "*" },
315 * 4k optimised & trim only works in 4k requests + 4k aligned
316 * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk>
319 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX3*", "*" },
324 * SuperTalent TeraDrive CT SSDs
325 * 4k optimised & trim only works in 4k requests + 4k aligned
326 * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk>
329 { T_DIRECT, SIP_MEDIA_FIXED, "*", "FTM??CT25H*", "*" },
334 * Crucial RealSSD C300 SSDs
336 * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk>
339 { T_DIRECT, SIP_MEDIA_FIXED, "*", "C300-CTFDDAC???MAG*",
340 "*" }, /*quirks*/ADA_Q_4K
344 * XceedIOPS SATA SSDs
346 * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk>
349 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SG9XCS2D*", "*" },
354 * Intel 330 Series SSDs
355 * 4k optimised & trim only works in 4k requests + 4k aligned
356 * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk>
359 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2ct*", "*" },
364 * OCZ Deneva R Series SSDs
365 * 4k optimised & trim only works in 4k requests + 4k aligned
366 * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk>
369 { T_DIRECT, SIP_MEDIA_FIXED, "*", "DENRSTE251M45*", "*" },
374 * Kingston HyperX 3k SSDs
375 * 4k optimised & trim only works in 4k requests + 4k aligned
376 * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk>
379 { T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SH103S3*", "*" },
385 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
386 /*vendor*/"*", /*product*/"*", /*revision*/"*"
392 static disk_strategy_t adastrategy;
393 static dumper_t adadump;
394 static periph_init_t adainit;
395 static void adaasync(void *callback_arg, u_int32_t code,
396 struct cam_path *path, void *arg);
397 static void adasysctlinit(void *context, int pending);
398 static periph_ctor_t adaregister;
399 static periph_dtor_t adacleanup;
400 static periph_start_t adastart;
401 static periph_oninv_t adaoninvalidate;
402 static void adadone(struct cam_periph *periph,
403 union ccb *done_ccb);
404 static int adaerror(union ccb *ccb, u_int32_t cam_flags,
405 u_int32_t sense_flags);
406 static void adagetparams(struct cam_periph *periph,
407 struct ccb_getdev *cgd);
408 static timeout_t adasendorderedtag;
409 static void adashutdown(void *arg, int howto);
410 static void adasuspend(void *arg);
411 static void adaresume(void *arg);
413 #ifndef ADA_DEFAULT_LEGACY_ALIASES
414 #define ADA_DEFAULT_LEGACY_ALIASES 1
417 #ifndef ADA_DEFAULT_TIMEOUT
418 #define ADA_DEFAULT_TIMEOUT 30 /* Timeout in seconds */
421 #ifndef ADA_DEFAULT_RETRY
422 #define ADA_DEFAULT_RETRY 4
425 #ifndef ADA_DEFAULT_SEND_ORDERED
426 #define ADA_DEFAULT_SEND_ORDERED 1
429 #ifndef ADA_DEFAULT_SPINDOWN_SHUTDOWN
430 #define ADA_DEFAULT_SPINDOWN_SHUTDOWN 1
433 #ifndef ADA_DEFAULT_SPINDOWN_SUSPEND
434 #define ADA_DEFAULT_SPINDOWN_SUSPEND 1
437 #ifndef ADA_DEFAULT_READ_AHEAD
438 #define ADA_DEFAULT_READ_AHEAD 1
441 #ifndef ADA_DEFAULT_WRITE_CACHE
442 #define ADA_DEFAULT_WRITE_CACHE 1
445 #define ADA_RA (softc->read_ahead >= 0 ? \
446 softc->read_ahead : ada_read_ahead)
447 #define ADA_WC (softc->write_cache >= 0 ? \
448 softc->write_cache : ada_write_cache)
449 #define ADA_SIO (softc->sort_io_queue >= 0 ? \
450 softc->sort_io_queue : cam_sort_io_queues)
453 * Most platforms map firmware geometry to actual, but some don't. If
454 * not overridden, default to nothing.
456 #ifndef ata_disk_firmware_geom_adjust
457 #define ata_disk_firmware_geom_adjust(disk)
460 static int ada_legacy_aliases = ADA_DEFAULT_LEGACY_ALIASES;
461 static int ada_retry_count = ADA_DEFAULT_RETRY;
462 static int ada_default_timeout = ADA_DEFAULT_TIMEOUT;
463 static int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED;
464 static int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN;
465 static int ada_spindown_suspend = ADA_DEFAULT_SPINDOWN_SUSPEND;
466 static int ada_read_ahead = ADA_DEFAULT_READ_AHEAD;
467 static int ada_write_cache = ADA_DEFAULT_WRITE_CACHE;
469 static SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD, 0,
470 "CAM Direct Access Disk driver");
471 SYSCTL_INT(_kern_cam_ada, OID_AUTO, legacy_aliases, CTLFLAG_RW,
472 &ada_legacy_aliases, 0, "Create legacy-like device aliases");
473 TUNABLE_INT("kern.cam.ada.legacy_aliases", &ada_legacy_aliases);
474 SYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RW,
475 &ada_retry_count, 0, "Normal I/O retry count");
476 TUNABLE_INT("kern.cam.ada.retry_count", &ada_retry_count);
477 SYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RW,
478 &ada_default_timeout, 0, "Normal I/O timeout (in seconds)");
479 TUNABLE_INT("kern.cam.ada.default_timeout", &ada_default_timeout);
480 SYSCTL_INT(_kern_cam_ada, OID_AUTO, send_ordered, CTLFLAG_RW,
481 &ada_send_ordered, 0, "Send Ordered Tags");
482 TUNABLE_INT("kern.cam.ada.send_ordered", &ada_send_ordered);
483 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RW,
484 &ada_spindown_shutdown, 0, "Spin down upon shutdown");
485 TUNABLE_INT("kern.cam.ada.spindown_shutdown", &ada_spindown_shutdown);
486 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_suspend, CTLFLAG_RW,
487 &ada_spindown_suspend, 0, "Spin down upon suspend");
488 TUNABLE_INT("kern.cam.ada.spindown_suspend", &ada_spindown_suspend);
489 SYSCTL_INT(_kern_cam_ada, OID_AUTO, read_ahead, CTLFLAG_RW,
490 &ada_read_ahead, 0, "Enable disk read-ahead");
491 TUNABLE_INT("kern.cam.ada.read_ahead", &ada_read_ahead);
492 SYSCTL_INT(_kern_cam_ada, OID_AUTO, write_cache, CTLFLAG_RW,
493 &ada_write_cache, 0, "Enable disk write cache");
494 TUNABLE_INT("kern.cam.ada.write_cache", &ada_write_cache);
497 * ADA_ORDEREDTAG_INTERVAL determines how often, relative
498 * to the default timeout, we check to see whether an ordered
499 * tagged transaction is appropriate to prevent simple tag
500 * starvation. Since we'd like to ensure that there is at least
501 * 1/2 of the timeout length left for a starved transaction to
502 * complete after we've sent an ordered tag, we must poll at least
503 * four times in every timeout period. This takes care of the worst
504 * case where a starved transaction starts during an interval that
505 * meets the requirement "don't send an ordered tag" test so it takes
506 * us two intervals to determine that a tag must be sent.
508 #ifndef ADA_ORDEREDTAG_INTERVAL
509 #define ADA_ORDEREDTAG_INTERVAL 4
512 static struct periph_driver adadriver =
515 TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0
518 PERIPHDRIVER_DECLARE(ada, adadriver);
520 static MALLOC_DEFINE(M_ATADA, "ata_da", "ata_da buffers");
523 adaopen(struct disk *dp)
525 struct cam_periph *periph;
526 struct ada_softc *softc;
529 periph = (struct cam_periph *)dp->d_drv1;
530 if (periph == NULL) {
534 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
538 cam_periph_lock(periph);
539 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
540 cam_periph_unlock(periph);
541 cam_periph_release(periph);
545 softc = (struct ada_softc *)periph->softc;
546 softc->flags |= ADA_FLAG_OPEN;
548 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
551 if ((softc->flags & ADA_FLAG_PACK_INVALID) != 0) {
552 /* Invalidate our pack information. */
553 softc->flags &= ~ADA_FLAG_PACK_INVALID;
556 cam_periph_unhold(periph);
557 cam_periph_unlock(periph);
562 adaclose(struct disk *dp)
564 struct cam_periph *periph;
565 struct ada_softc *softc;
568 periph = (struct cam_periph *)dp->d_drv1;
572 cam_periph_lock(periph);
573 if (cam_periph_hold(periph, PRIBIO) != 0) {
574 cam_periph_unlock(periph);
575 cam_periph_release(periph);
579 softc = (struct ada_softc *)periph->softc;
581 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
584 /* We only sync the cache if the drive is capable of it. */
585 if ((softc->flags & ADA_FLAG_CAN_FLUSHCACHE) != 0 &&
586 (softc->flags & ADA_FLAG_PACK_INVALID) == 0) {
588 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
589 cam_fill_ataio(&ccb->ataio,
596 ada_default_timeout*1000);
598 if (softc->flags & ADA_FLAG_CAN_48BIT)
599 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
601 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
602 cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
603 /*sense_flags*/0, softc->disk->d_devstat);
605 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
606 xpt_print(periph->path, "Synchronize cache failed\n");
607 xpt_release_ccb(ccb);
610 softc->flags &= ~ADA_FLAG_OPEN;
611 cam_periph_unhold(periph);
612 cam_periph_unlock(periph);
613 cam_periph_release(periph);
618 adaschedule(struct cam_periph *periph)
620 struct ada_softc *softc = (struct ada_softc *)periph->softc;
623 /* Check if cam_periph_getccb() was called. */
624 prio = periph->immediate_priority;
626 /* Check if we have more work to do. */
627 if (bioq_first(&softc->bio_queue) ||
628 (!softc->trim_running && bioq_first(&softc->trim_queue))) {
629 prio = CAM_PRIORITY_NORMAL;
632 /* Schedule CCB if any of above is true. */
633 if (prio != CAM_PRIORITY_NONE)
634 xpt_schedule(periph, prio);
638 * Actually translate the requested transfer into one the physical driver
639 * can understand. The transfer is described by a buf and will include
640 * only one physical transfer.
643 adastrategy(struct bio *bp)
645 struct cam_periph *periph;
646 struct ada_softc *softc;
648 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
649 if (periph == NULL) {
650 biofinish(bp, NULL, ENXIO);
653 softc = (struct ada_softc *)periph->softc;
655 cam_periph_lock(periph);
657 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastrategy(%p)\n", bp));
660 * If the device has been made invalid, error out
662 if ((softc->flags & ADA_FLAG_PACK_INVALID)) {
663 cam_periph_unlock(periph);
664 biofinish(bp, NULL, ENXIO);
669 * Place it in the queue of disk activities for this disk
671 if (bp->bio_cmd == BIO_DELETE &&
672 (softc->flags & ADA_FLAG_CAN_TRIM)) {
674 bioq_disksort(&softc->trim_queue, bp);
676 bioq_insert_tail(&softc->trim_queue, bp);
679 bioq_disksort(&softc->bio_queue, bp);
681 bioq_insert_tail(&softc->bio_queue, bp);
685 * Schedule ourselves for performing the work.
688 cam_periph_unlock(periph);
694 adadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
696 struct cam_periph *periph;
697 struct ada_softc *softc;
709 softc = (struct ada_softc *)periph->softc;
710 cam_periph_lock(periph);
711 secsize = softc->params.secsize;
712 lba = offset / secsize;
713 count = length / secsize;
715 if ((softc->flags & ADA_FLAG_PACK_INVALID) != 0) {
716 cam_periph_unlock(periph);
721 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
722 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
723 cam_fill_ataio(&ccb.ataio,
728 (u_int8_t *) virtual,
730 ada_default_timeout*1000);
731 if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
732 (lba + count >= ATA_MAX_28BIT_LBA ||
734 ata_48bit_cmd(&ccb.ataio, ATA_WRITE_DMA48,
737 ata_28bit_cmd(&ccb.ataio, ATA_WRITE_DMA,
740 xpt_polled_action(&ccb);
742 error = cam_periph_error(&ccb,
743 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
744 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
745 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
746 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
748 printf("Aborting dump due to I/O error.\n");
750 cam_periph_unlock(periph);
754 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) {
755 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
757 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
758 cam_fill_ataio(&ccb.ataio,
765 ada_default_timeout*1000);
767 if (softc->flags & ADA_FLAG_CAN_48BIT)
768 ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0);
770 ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0);
771 xpt_polled_action(&ccb);
773 error = cam_periph_error(&ccb,
774 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
775 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
776 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
777 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
779 xpt_print(periph->path, "Synchronize cache failed\n");
781 cam_periph_unlock(periph);
791 * Install a global async callback. This callback will
792 * receive async callbacks like "new device found".
794 status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL);
796 if (status != CAM_REQ_CMP) {
797 printf("ada: Failed to attach master async callback "
798 "due to status 0x%x!\n", status);
799 } else if (ada_send_ordered) {
801 /* Register our event handlers */
802 if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend,
803 NULL, EVENTHANDLER_PRI_LAST)) == NULL)
804 printf("adainit: power event registration failed!\n");
805 if ((EVENTHANDLER_REGISTER(power_resume, adaresume,
806 NULL, EVENTHANDLER_PRI_LAST)) == NULL)
807 printf("adainit: power event registration failed!\n");
808 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown,
809 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
810 printf("adainit: shutdown event registration failed!\n");
815 adaoninvalidate(struct cam_periph *periph)
817 struct ada_softc *softc;
819 softc = (struct ada_softc *)periph->softc;
822 * De-register any async callbacks.
824 xpt_register_async(0, adaasync, periph, periph->path);
826 softc->flags |= ADA_FLAG_PACK_INVALID;
829 * Return all queued I/O with ENXIO.
830 * XXX Handle any transactions queued to the card
831 * with XPT_ABORT_CCB.
833 bioq_flush(&softc->bio_queue, NULL, ENXIO);
834 bioq_flush(&softc->trim_queue, NULL, ENXIO);
836 disk_gone(softc->disk);
837 xpt_print(periph->path, "lost device\n");
841 adacleanup(struct cam_periph *periph)
843 struct ada_softc *softc;
845 softc = (struct ada_softc *)periph->softc;
847 xpt_print(periph->path, "removing device entry\n");
848 cam_periph_unlock(periph);
851 * If we can't free the sysctl tree, oh well...
853 if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0
854 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
855 xpt_print(periph->path, "can't remove sysctl context\n");
858 disk_destroy(softc->disk);
859 callout_drain(&softc->sendordered_c);
860 free(softc, M_DEVBUF);
861 cam_periph_lock(periph);
865 adaasync(void *callback_arg, u_int32_t code,
866 struct cam_path *path, void *arg)
868 struct ccb_getdev cgd;
869 struct cam_periph *periph;
870 struct ada_softc *softc;
872 periph = (struct cam_periph *)callback_arg;
874 case AC_FOUND_DEVICE:
876 struct ccb_getdev *cgd;
879 cgd = (struct ccb_getdev *)arg;
883 if (cgd->protocol != PROTO_ATA)
887 * Allocate a peripheral instance for
888 * this device and start the probe
891 status = cam_periph_alloc(adaregister, adaoninvalidate,
892 adacleanup, adastart,
893 "ada", CAM_PERIPH_BIO,
894 cgd->ccb_h.path, adaasync,
895 AC_FOUND_DEVICE, cgd);
897 if (status != CAM_REQ_CMP
898 && status != CAM_REQ_INPROG)
899 printf("adaasync: Unable to attach to new device "
900 "due to status 0x%x\n", status);
903 case AC_GETDEV_CHANGED:
905 softc = (struct ada_softc *)periph->softc;
906 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
907 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
908 xpt_action((union ccb *)&cgd);
910 if ((cgd.ident_data.capabilities1 & ATA_SUPPORT_DMA) &&
911 (cgd.inq_flags & SID_DMA))
912 softc->flags |= ADA_FLAG_CAN_DMA;
914 softc->flags &= ~ADA_FLAG_CAN_DMA;
915 if ((cgd.ident_data.satacapabilities & ATA_SUPPORT_NCQ) &&
916 (cgd.inq_flags & SID_DMA) && (cgd.inq_flags & SID_CmdQue))
917 softc->flags |= ADA_FLAG_CAN_NCQ;
919 softc->flags &= ~ADA_FLAG_CAN_NCQ;
920 if ((cgd.ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) &&
921 (cgd.inq_flags & SID_DMA))
922 softc->flags |= ADA_FLAG_CAN_TRIM;
924 softc->flags &= ~ADA_FLAG_CAN_TRIM;
926 cam_periph_async(periph, code, path, arg);
929 case AC_ADVINFO_CHANGED:
933 buftype = (uintptr_t)arg;
934 if (buftype == CDAI_TYPE_PHYS_PATH) {
935 struct ada_softc *softc;
937 softc = periph->softc;
938 disk_attr_changed(softc->disk, "GEOM::physpath",
946 softc = (struct ada_softc *)periph->softc;
947 cam_periph_async(periph, code, path, arg);
948 if (softc->state != ADA_STATE_NORMAL)
950 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
951 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
952 xpt_action((union ccb *)&cgd);
954 cgd.ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD)
955 softc->state = ADA_STATE_RAHEAD;
956 else if (ADA_WC >= 0 &&
957 cgd.ident_data.support.command1 & ATA_SUPPORT_WRITECACHE)
958 softc->state = ADA_STATE_WCACHE;
961 cam_periph_acquire(periph);
962 cam_freeze_devq_arg(periph->path,
963 RELSIM_RELEASE_RUNLEVEL, CAM_RL_DEV + 1);
964 xpt_schedule(periph, CAM_PRIORITY_DEV);
967 cam_periph_async(periph, code, path, arg);
973 adasysctlinit(void *context, int pending)
975 struct cam_periph *periph;
976 struct ada_softc *softc;
977 char tmpstr[80], tmpstr2[80];
979 periph = (struct cam_periph *)context;
981 /* periph was held for us when this task was enqueued */
982 if (periph->flags & CAM_PERIPH_INVALID) {
983 cam_periph_release(periph);
987 softc = (struct ada_softc *)periph->softc;
988 snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d", periph->unit_number);
989 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
991 sysctl_ctx_init(&softc->sysctl_ctx);
992 softc->flags |= ADA_FLAG_SCTX_INIT;
993 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
994 SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2,
995 CTLFLAG_RD, 0, tmpstr);
996 if (softc->sysctl_tree == NULL) {
997 printf("adasysctlinit: unable to allocate sysctl tree\n");
998 cam_periph_release(periph);
1002 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1003 OID_AUTO, "read_ahead", CTLFLAG_RW | CTLFLAG_MPSAFE,
1004 &softc->read_ahead, 0, "Enable disk read ahead.");
1005 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1006 OID_AUTO, "write_cache", CTLFLAG_RW | CTLFLAG_MPSAFE,
1007 &softc->write_cache, 0, "Enable disk write cache.");
1008 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1009 OID_AUTO, "sort_io_queue", CTLFLAG_RW | CTLFLAG_MPSAFE,
1010 &softc->sort_io_queue, 0,
1011 "Sort IO queue to try and optimise disk access patterns");
1012 #ifdef ADA_TEST_FAILURE
1014 * Add a 'door bell' sysctl which allows one to set it from userland
1015 * and cause something bad to happen. For the moment, we only allow
1016 * whacking the next read or write.
1018 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1019 OID_AUTO, "force_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1020 &softc->force_read_error, 0,
1021 "Force a read error for the next N reads.");
1022 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1023 OID_AUTO, "force_write_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1024 &softc->force_write_error, 0,
1025 "Force a write error for the next N writes.");
1026 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1027 OID_AUTO, "periodic_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1028 &softc->periodic_read_error, 0,
1029 "Force a read error every N reads (don't set too low).");
1031 cam_periph_release(periph);
1035 adagetattr(struct bio *bp)
1038 struct cam_periph *periph;
1040 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1044 cam_periph_lock(periph);
1045 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1047 cam_periph_unlock(periph);
1049 bp->bio_completed = bp->bio_length;
1054 adaregister(struct cam_periph *periph, void *arg)
1056 struct ada_softc *softc;
1057 struct ccb_pathinq cpi;
1058 struct ccb_getdev *cgd;
1059 char announce_buf[80], buf1[32];
1060 struct disk_params *dp;
1063 int legacy_id, quirks;
1065 cgd = (struct ccb_getdev *)arg;
1067 printf("adaregister: no getdev CCB, can't register device\n");
1068 return(CAM_REQ_CMP_ERR);
1071 softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF,
1074 if (softc == NULL) {
1075 printf("adaregister: Unable to probe new device. "
1076 "Unable to allocate softc\n");
1077 return(CAM_REQ_CMP_ERR);
1080 bioq_init(&softc->bio_queue);
1081 bioq_init(&softc->trim_queue);
1083 if ((cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA) &&
1084 (cgd->inq_flags & SID_DMA))
1085 softc->flags |= ADA_FLAG_CAN_DMA;
1086 if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48)
1087 softc->flags |= ADA_FLAG_CAN_48BIT;
1088 if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE)
1089 softc->flags |= ADA_FLAG_CAN_FLUSHCACHE;
1090 if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT)
1091 softc->flags |= ADA_FLAG_CAN_POWERMGT;
1092 if ((cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ) &&
1093 (cgd->inq_flags & SID_DMA) && (cgd->inq_flags & SID_CmdQue))
1094 softc->flags |= ADA_FLAG_CAN_NCQ;
1095 if ((cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) &&
1096 (cgd->inq_flags & SID_DMA)) {
1097 softc->flags |= ADA_FLAG_CAN_TRIM;
1098 softc->trim_max_ranges = TRIM_MAX_RANGES;
1099 if (cgd->ident_data.max_dsm_blocks != 0) {
1100 softc->trim_max_ranges =
1101 min(cgd->ident_data.max_dsm_blocks * 64,
1102 softc->trim_max_ranges);
1105 if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA)
1106 softc->flags |= ADA_FLAG_CAN_CFA;
1108 periph->softc = softc;
1111 * See if this device has any quirks.
1113 match = cam_quirkmatch((caddr_t)&cgd->ident_data,
1114 (caddr_t)ada_quirk_table,
1115 sizeof(ada_quirk_table)/sizeof(*ada_quirk_table),
1116 sizeof(*ada_quirk_table), ata_identify_match);
1118 softc->quirks = ((struct ada_quirk_entry *)match)->quirks;
1120 softc->quirks = ADA_Q_NONE;
1122 bzero(&cpi, sizeof(cpi));
1123 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE);
1124 cpi.ccb_h.func_code = XPT_PATH_INQ;
1125 xpt_action((union ccb *)&cpi);
1127 TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph);
1130 * Register this media as a disk
1132 (void)cam_periph_hold(periph, PRIBIO);
1133 mtx_unlock(periph->sim->mtx);
1134 snprintf(announce_buf, sizeof(announce_buf),
1135 "kern.cam.ada.%d.quirks", periph->unit_number);
1136 quirks = softc->quirks;
1137 TUNABLE_INT_FETCH(announce_buf, &quirks);
1138 softc->quirks = quirks;
1139 softc->read_ahead = -1;
1140 snprintf(announce_buf, sizeof(announce_buf),
1141 "kern.cam.ada.%d.read_ahead", periph->unit_number);
1142 TUNABLE_INT_FETCH(announce_buf, &softc->read_ahead);
1143 softc->write_cache = -1;
1144 snprintf(announce_buf, sizeof(announce_buf),
1145 "kern.cam.ada.%d.write_cache", periph->unit_number);
1146 TUNABLE_INT_FETCH(announce_buf, &softc->write_cache);
1147 softc->sort_io_queue = -1;
1148 adagetparams(periph, cgd);
1149 softc->disk = disk_alloc();
1150 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
1151 periph->unit_number, softc->params.secsize,
1152 DEVSTAT_ALL_SUPPORTED,
1153 DEVSTAT_TYPE_DIRECT |
1154 XPORT_DEVSTAT_TYPE(cpi.transport),
1155 DEVSTAT_PRIORITY_DISK);
1156 softc->disk->d_open = adaopen;
1157 softc->disk->d_close = adaclose;
1158 softc->disk->d_strategy = adastrategy;
1159 softc->disk->d_getattr = adagetattr;
1160 softc->disk->d_dump = adadump;
1161 softc->disk->d_name = "ada";
1162 softc->disk->d_drv1 = periph;
1163 maxio = cpi.maxio; /* Honor max I/O size of SIM */
1165 maxio = DFLTPHYS; /* traditional default */
1166 else if (maxio > MAXPHYS)
1167 maxio = MAXPHYS; /* for safety */
1168 if (softc->flags & ADA_FLAG_CAN_48BIT)
1169 maxio = min(maxio, 65536 * softc->params.secsize);
1170 else /* 28bit ATA command limit */
1171 maxio = min(maxio, 256 * softc->params.secsize);
1172 softc->disk->d_maxsize = maxio;
1173 softc->disk->d_unit = periph->unit_number;
1174 softc->disk->d_flags = 0;
1175 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE)
1176 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
1177 if ((softc->flags & ADA_FLAG_CAN_TRIM) ||
1178 ((softc->flags & ADA_FLAG_CAN_CFA) &&
1179 !(softc->flags & ADA_FLAG_CAN_48BIT)))
1180 softc->disk->d_flags |= DISKFLAG_CANDELETE;
1181 if ((cpi.hba_misc & PIM_UNMAPPED) != 0)
1182 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
1183 strlcpy(softc->disk->d_descr, cgd->ident_data.model,
1184 MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model)));
1185 strlcpy(softc->disk->d_ident, cgd->ident_data.serial,
1186 MIN(sizeof(softc->disk->d_ident), sizeof(cgd->ident_data.serial)));
1187 softc->disk->d_hba_vendor = cpi.hba_vendor;
1188 softc->disk->d_hba_device = cpi.hba_device;
1189 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
1190 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
1192 softc->disk->d_sectorsize = softc->params.secsize;
1193 softc->disk->d_mediasize = (off_t)softc->params.sectors *
1194 softc->params.secsize;
1195 if (ata_physical_sector_size(&cgd->ident_data) !=
1196 softc->params.secsize) {
1197 softc->disk->d_stripesize =
1198 ata_physical_sector_size(&cgd->ident_data);
1199 softc->disk->d_stripeoffset = (softc->disk->d_stripesize -
1200 ata_logical_sector_offset(&cgd->ident_data)) %
1201 softc->disk->d_stripesize;
1202 } else if (softc->quirks & ADA_Q_4K) {
1203 softc->disk->d_stripesize = 4096;
1204 softc->disk->d_stripeoffset = 0;
1206 softc->disk->d_fwsectors = softc->params.secs_per_track;
1207 softc->disk->d_fwheads = softc->params.heads;
1208 ata_disk_firmware_geom_adjust(softc->disk);
1210 if (ada_legacy_aliases) {
1211 #ifdef ATA_STATIC_ID
1212 legacy_id = xpt_path_legacy_ata_id(periph->path);
1214 legacy_id = softc->disk->d_unit;
1216 if (legacy_id >= 0) {
1217 snprintf(announce_buf, sizeof(announce_buf),
1218 "kern.devalias.%s%d",
1219 softc->disk->d_name, softc->disk->d_unit);
1220 snprintf(buf1, sizeof(buf1),
1222 setenv(announce_buf, buf1);
1226 disk_create(softc->disk, DISK_VERSION);
1227 mtx_lock(periph->sim->mtx);
1228 cam_periph_unhold(periph);
1230 dp = &softc->params;
1231 snprintf(announce_buf, sizeof(announce_buf),
1232 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1233 (uintmax_t)(((uintmax_t)dp->secsize *
1234 dp->sectors) / (1024*1024)),
1235 (uintmax_t)dp->sectors,
1236 dp->secsize, dp->heads,
1237 dp->secs_per_track, dp->cylinders);
1238 xpt_announce_periph(periph, announce_buf);
1240 printf("%s%d: Previously was known as ad%d\n",
1241 periph->periph_name, periph->unit_number, legacy_id);
1244 * Create our sysctl variables, now that we know
1245 * we have successfully attached.
1247 cam_periph_acquire(periph);
1248 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task);
1251 * Add async callbacks for bus reset and
1252 * bus device reset calls. I don't bother
1253 * checking if this fails as, in most cases,
1254 * the system will function just fine without
1255 * them and the only alternative would be to
1256 * not attach the device on failure.
1258 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
1259 AC_GETDEV_CHANGED | AC_ADVINFO_CHANGED,
1260 adaasync, periph, periph->path);
1263 * Schedule a periodic event to occasionally send an
1264 * ordered tag to a device.
1266 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
1267 callout_reset(&softc->sendordered_c,
1268 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL,
1269 adasendorderedtag, softc);
1272 cgd->ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD) {
1273 softc->state = ADA_STATE_RAHEAD;
1274 cam_periph_acquire(periph);
1275 cam_freeze_devq_arg(periph->path,
1276 RELSIM_RELEASE_RUNLEVEL, CAM_RL_DEV + 1);
1277 xpt_schedule(periph, CAM_PRIORITY_DEV);
1278 } else if (ADA_WC >= 0 &&
1279 cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) {
1280 softc->state = ADA_STATE_WCACHE;
1281 cam_periph_acquire(periph);
1282 cam_freeze_devq_arg(periph->path,
1283 RELSIM_RELEASE_RUNLEVEL, CAM_RL_DEV + 1);
1284 xpt_schedule(periph, CAM_PRIORITY_DEV);
1286 softc->state = ADA_STATE_NORMAL;
1288 return(CAM_REQ_CMP);
1292 adastart(struct cam_periph *periph, union ccb *start_ccb)
1294 struct ada_softc *softc = (struct ada_softc *)periph->softc;
1295 struct ccb_ataio *ataio = &start_ccb->ataio;
1297 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastart\n"));
1299 switch (softc->state) {
1300 case ADA_STATE_NORMAL:
1305 /* Execute immediate CCB if waiting. */
1306 if (periph->immediate_priority <= periph->pinfo.priority) {
1307 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1308 ("queuing for immediate ccb\n"));
1309 start_ccb->ccb_h.ccb_state = ADA_CCB_WAITING;
1310 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1312 periph->immediate_priority = CAM_PRIORITY_NONE;
1313 wakeup(&periph->ccb_list);
1314 /* Have more work to do, so ensure we stay scheduled */
1315 adaschedule(periph);
1318 /* Run TRIM if not running yet. */
1319 if (!softc->trim_running &&
1320 (bp = bioq_first(&softc->trim_queue)) != 0) {
1321 struct trim_request *req = &softc->trim_req;
1323 uint64_t lastlba = (uint64_t)-1;
1324 int bps = 0, c, lastcount = 0, off, ranges = 0;
1326 softc->trim_running = 1;
1327 bzero(req, sizeof(*req));
1330 uint64_t lba = bp1->bio_pblkno;
1331 int count = bp1->bio_bcount /
1332 softc->params.secsize;
1334 bioq_remove(&softc->trim_queue, bp1);
1336 /* Try to extend the previous range. */
1337 if (lba == lastlba) {
1338 c = min(count, 0xffff - lastcount);
1340 off = (ranges - 1) * 8;
1341 req->data[off + 6] = lastcount & 0xff;
1342 req->data[off + 7] =
1343 (lastcount >> 8) & 0xff;
1349 c = min(count, 0xffff);
1351 req->data[off + 0] = lba & 0xff;
1352 req->data[off + 1] = (lba >> 8) & 0xff;
1353 req->data[off + 2] = (lba >> 16) & 0xff;
1354 req->data[off + 3] = (lba >> 24) & 0xff;
1355 req->data[off + 4] = (lba >> 32) & 0xff;
1356 req->data[off + 5] = (lba >> 40) & 0xff;
1357 req->data[off + 6] = c & 0xff;
1358 req->data[off + 7] = (c >> 8) & 0xff;
1365 req->bps[bps++] = bp1;
1366 bp1 = bioq_first(&softc->trim_queue);
1367 if (bps >= TRIM_MAX_BIOS ||
1369 bp1->bio_bcount / softc->params.secsize >
1370 (softc->trim_max_ranges - ranges) * 0xffff)
1373 cam_fill_ataio(ataio,
1379 ((ranges + 63) / 64) * 512,
1380 ada_default_timeout * 1000);
1381 ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT,
1382 ATA_DSM_TRIM, 0, (ranges + 63) / 64);
1383 start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM;
1386 /* Run regular command. */
1387 bp = bioq_first(&softc->bio_queue);
1389 xpt_release_ccb(start_ccb);
1392 bioq_remove(&softc->bio_queue, bp);
1394 if ((bp->bio_flags & BIO_ORDERED) != 0
1395 || (softc->flags & ADA_FLAG_NEED_OTAG) != 0) {
1396 softc->flags &= ~ADA_FLAG_NEED_OTAG;
1397 softc->ordered_tag_count++;
1402 switch (bp->bio_cmd) {
1406 uint64_t lba = bp->bio_pblkno;
1407 uint16_t count = bp->bio_bcount / softc->params.secsize;
1408 #ifdef ADA_TEST_FAILURE
1412 * Support the failure ioctls. If the command is a
1413 * read, and there are pending forced read errors, or
1414 * if a write and pending write errors, then fail this
1415 * operation with EIO. This is useful for testing
1416 * purposes. Also, support having every Nth read fail.
1418 * This is a rather blunt tool.
1420 if (bp->bio_cmd == BIO_READ) {
1421 if (softc->force_read_error) {
1422 softc->force_read_error--;
1425 if (softc->periodic_read_error > 0) {
1426 if (++softc->periodic_read_count >=
1427 softc->periodic_read_error) {
1428 softc->periodic_read_count = 0;
1433 if (softc->force_write_error) {
1434 softc->force_write_error--;
1439 bp->bio_error = EIO;
1440 bp->bio_flags |= BIO_ERROR;
1442 xpt_release_ccb(start_ccb);
1443 adaschedule(periph);
1447 KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 ||
1448 round_page(bp->bio_bcount + bp->bio_ma_offset) /
1449 PAGE_SIZE == bp->bio_ma_n,
1450 ("Short bio %p", bp));
1451 cam_fill_ataio(ataio,
1454 (bp->bio_cmd == BIO_READ ? CAM_DIR_IN :
1455 CAM_DIR_OUT) | ((bp->bio_flags & BIO_UNMAPPED)
1456 != 0 ? CAM_DATA_BIO : 0),
1458 ((bp->bio_flags & BIO_UNMAPPED) != 0) ? (void *)bp :
1461 ada_default_timeout*1000);
1463 if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) {
1464 if (bp->bio_cmd == BIO_READ) {
1465 ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED,
1468 ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED,
1471 } else if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
1472 (lba + count >= ATA_MAX_28BIT_LBA ||
1474 if (softc->flags & ADA_FLAG_CAN_DMA) {
1475 if (bp->bio_cmd == BIO_READ) {
1476 ata_48bit_cmd(ataio, ATA_READ_DMA48,
1479 ata_48bit_cmd(ataio, ATA_WRITE_DMA48,
1483 if (bp->bio_cmd == BIO_READ) {
1484 ata_48bit_cmd(ataio, ATA_READ_MUL48,
1487 ata_48bit_cmd(ataio, ATA_WRITE_MUL48,
1494 if (softc->flags & ADA_FLAG_CAN_DMA) {
1495 if (bp->bio_cmd == BIO_READ) {
1496 ata_28bit_cmd(ataio, ATA_READ_DMA,
1499 ata_28bit_cmd(ataio, ATA_WRITE_DMA,
1503 if (bp->bio_cmd == BIO_READ) {
1504 ata_28bit_cmd(ataio, ATA_READ_MUL,
1507 ata_28bit_cmd(ataio, ATA_WRITE_MUL,
1516 uint64_t lba = bp->bio_pblkno;
1517 uint16_t count = bp->bio_bcount / softc->params.secsize;
1519 cam_fill_ataio(ataio,
1526 ada_default_timeout*1000);
1530 ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count);
1534 cam_fill_ataio(ataio,
1541 ada_default_timeout*1000);
1543 if (softc->flags & ADA_FLAG_CAN_48BIT)
1544 ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1546 ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0);
1549 start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO;
1551 start_ccb->ccb_h.ccb_bp = bp;
1552 softc->outstanding_cmds++;
1553 xpt_action(start_ccb);
1555 /* May have more work to do, so ensure we stay scheduled */
1556 adaschedule(periph);
1559 case ADA_STATE_RAHEAD:
1560 case ADA_STATE_WCACHE:
1562 if (softc->flags & ADA_FLAG_PACK_INVALID) {
1563 softc->state = ADA_STATE_NORMAL;
1564 xpt_release_ccb(start_ccb);
1565 cam_release_devq(periph->path,
1566 RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_DEV + 1, FALSE);
1567 adaschedule(periph);
1568 cam_periph_release_locked(periph);
1572 cam_fill_ataio(ataio,
1579 ada_default_timeout*1000);
1581 if (softc->state == ADA_STATE_RAHEAD) {
1582 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_RA ?
1583 ATA_SF_ENAB_RCACHE : ATA_SF_DIS_RCACHE, 0, 0);
1584 start_ccb->ccb_h.ccb_state = ADA_CCB_RAHEAD;
1586 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_WC ?
1587 ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0);
1588 start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE;
1590 xpt_action(start_ccb);
1597 adadone(struct cam_periph *periph, union ccb *done_ccb)
1599 struct ada_softc *softc;
1600 struct ccb_ataio *ataio;
1601 struct ccb_getdev *cgd;
1603 softc = (struct ada_softc *)periph->softc;
1604 ataio = &done_ccb->ataio;
1606 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adadone\n"));
1608 switch (ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) {
1609 case ADA_CCB_BUFFER_IO:
1614 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1615 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1618 error = adaerror(done_ccb, 0, 0);
1619 if (error == ERESTART) {
1620 /* A retry was scheduled, so just return. */
1624 if (error == ENXIO &&
1625 (softc->flags & ADA_FLAG_PACK_INVALID) == 0) {
1627 * Catastrophic error. Mark our pack as
1631 * XXX See if this is really a media
1634 xpt_print(periph->path,
1635 "Invalidating pack\n");
1636 softc->flags |= ADA_FLAG_PACK_INVALID;
1638 bp->bio_error = error;
1639 bp->bio_resid = bp->bio_bcount;
1640 bp->bio_flags |= BIO_ERROR;
1642 bp->bio_resid = ataio->resid;
1644 if (bp->bio_resid != 0)
1645 bp->bio_flags |= BIO_ERROR;
1647 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1648 cam_release_devq(done_ccb->ccb_h.path,
1652 /*getcount_only*/0);
1654 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1655 panic("REQ_CMP with QFRZN");
1656 bp->bio_resid = ataio->resid;
1657 if (ataio->resid > 0)
1658 bp->bio_flags |= BIO_ERROR;
1660 softc->outstanding_cmds--;
1661 if (softc->outstanding_cmds == 0)
1662 softc->flags |= ADA_FLAG_WENT_IDLE;
1663 if ((ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) ==
1665 struct trim_request *req =
1666 (struct trim_request *)ataio->data_ptr;
1669 for (i = 1; i < TRIM_MAX_BIOS && req->bps[i]; i++) {
1670 struct bio *bp1 = req->bps[i];
1672 bp1->bio_resid = bp->bio_resid;
1673 bp1->bio_error = bp->bio_error;
1674 if (bp->bio_flags & BIO_ERROR)
1675 bp1->bio_flags |= BIO_ERROR;
1678 softc->trim_running = 0;
1680 adaschedule(periph);
1685 case ADA_CCB_RAHEAD:
1687 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1688 if (adaerror(done_ccb, 0, 0) == ERESTART) {
1690 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1691 cam_release_devq(done_ccb->ccb_h.path,
1695 /*getcount_only*/0);
1700 * Since our peripheral may be invalidated by an error
1701 * above or an external event, we must release our CCB
1702 * before releasing the reference on the peripheral.
1703 * The peripheral will only go away once the last reference
1704 * is removed, and we need it around for the CCB release
1707 cgd = (struct ccb_getdev *)done_ccb;
1708 xpt_setup_ccb(&cgd->ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1709 cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1710 xpt_action((union ccb *)cgd);
1712 cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) {
1713 softc->state = ADA_STATE_WCACHE;
1714 xpt_release_ccb(done_ccb);
1715 xpt_schedule(periph, CAM_PRIORITY_DEV);
1718 softc->state = ADA_STATE_NORMAL;
1719 xpt_release_ccb(done_ccb);
1720 cam_release_devq(periph->path,
1721 RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_DEV + 1, FALSE);
1722 adaschedule(periph);
1723 cam_periph_release_locked(periph);
1726 case ADA_CCB_WCACHE:
1728 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1729 if (adaerror(done_ccb, 0, 0) == ERESTART) {
1731 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1732 cam_release_devq(done_ccb->ccb_h.path,
1736 /*getcount_only*/0);
1740 softc->state = ADA_STATE_NORMAL;
1742 * Since our peripheral may be invalidated by an error
1743 * above or an external event, we must release our CCB
1744 * before releasing the reference on the peripheral.
1745 * The peripheral will only go away once the last reference
1746 * is removed, and we need it around for the CCB release
1749 xpt_release_ccb(done_ccb);
1750 cam_release_devq(periph->path,
1751 RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_DEV + 1, FALSE);
1752 adaschedule(periph);
1753 cam_periph_release_locked(periph);
1756 case ADA_CCB_WAITING:
1758 /* Caller will release the CCB */
1759 wakeup(&done_ccb->ccb_h.cbfcnp);
1763 /* No-op. We're polling */
1768 xpt_release_ccb(done_ccb);
1772 adaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1775 return(cam_periph_error(ccb, cam_flags, sense_flags, NULL));
1779 adagetparams(struct cam_periph *periph, struct ccb_getdev *cgd)
1781 struct ada_softc *softc = (struct ada_softc *)periph->softc;
1782 struct disk_params *dp = &softc->params;
1783 u_int64_t lbasize48;
1786 dp->secsize = ata_logical_sector_size(&cgd->ident_data);
1787 if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) &&
1788 cgd->ident_data.current_heads && cgd->ident_data.current_sectors) {
1789 dp->heads = cgd->ident_data.current_heads;
1790 dp->secs_per_track = cgd->ident_data.current_sectors;
1791 dp->cylinders = cgd->ident_data.cylinders;
1792 dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 |
1793 ((u_int32_t)cgd->ident_data.current_size_2 << 16);
1795 dp->heads = cgd->ident_data.heads;
1796 dp->secs_per_track = cgd->ident_data.sectors;
1797 dp->cylinders = cgd->ident_data.cylinders;
1798 dp->sectors = cgd->ident_data.cylinders * dp->heads * dp->secs_per_track;
1800 lbasize = (u_int32_t)cgd->ident_data.lba_size_1 |
1801 ((u_int32_t)cgd->ident_data.lba_size_2 << 16);
1803 /* use the 28bit LBA size if valid or bigger than the CHS mapping */
1804 if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize)
1805 dp->sectors = lbasize;
1807 /* use the 48bit LBA size if valid */
1808 lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) |
1809 ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) |
1810 ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) |
1811 ((u_int64_t)cgd->ident_data.lba_size48_4 << 48);
1812 if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) &&
1813 lbasize48 > ATA_MAX_28BIT_LBA)
1814 dp->sectors = lbasize48;
1818 adasendorderedtag(void *arg)
1820 struct ada_softc *softc = arg;
1822 if (ada_send_ordered) {
1823 if ((softc->ordered_tag_count == 0)
1824 && ((softc->flags & ADA_FLAG_WENT_IDLE) == 0)) {
1825 softc->flags |= ADA_FLAG_NEED_OTAG;
1827 if (softc->outstanding_cmds > 0)
1828 softc->flags &= ~ADA_FLAG_WENT_IDLE;
1830 softc->ordered_tag_count = 0;
1832 /* Queue us up again */
1833 callout_reset(&softc->sendordered_c,
1834 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL,
1835 adasendorderedtag, softc);
1839 * Step through all ADA peripheral drivers, and if the device is still open,
1840 * sync the disk cache to physical media.
1845 struct cam_periph *periph;
1846 struct ada_softc *softc;
1850 CAM_PERIPH_FOREACH(periph, &adadriver) {
1851 /* If we paniced with lock held - not recurse here. */
1852 if (cam_periph_owned(periph))
1854 cam_periph_lock(periph);
1855 softc = (struct ada_softc *)periph->softc;
1857 * We only sync the cache if the drive is still open, and
1858 * if the drive is capable of it..
1860 if (((softc->flags & ADA_FLAG_OPEN) == 0) ||
1861 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) {
1862 cam_periph_unlock(periph);
1866 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1867 cam_fill_ataio(&ccb->ataio,
1874 ada_default_timeout*1000);
1875 if (softc->flags & ADA_FLAG_CAN_48BIT)
1876 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1878 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
1880 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
1881 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
1882 softc->disk->d_devstat);
1884 xpt_print(periph->path, "Synchronize cache failed\n");
1885 xpt_release_ccb(ccb);
1886 cam_periph_unlock(periph);
1891 adaspindown(uint8_t cmd, int flags)
1893 struct cam_periph *periph;
1894 struct ada_softc *softc;
1898 CAM_PERIPH_FOREACH(periph, &adadriver) {
1899 /* If we paniced with lock held - not recurse here. */
1900 if (cam_periph_owned(periph))
1902 cam_periph_lock(periph);
1903 softc = (struct ada_softc *)periph->softc;
1905 * We only spin-down the drive if it is capable of it..
1907 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
1908 cam_periph_unlock(periph);
1913 xpt_print(periph->path, "spin-down\n");
1915 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1916 cam_fill_ataio(&ccb->ataio,
1919 CAM_DIR_NONE | flags,
1923 ada_default_timeout*1000);
1924 ata_28bit_cmd(&ccb->ataio, cmd, 0, 0, 0);
1926 error = cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
1927 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
1928 softc->disk->d_devstat);
1930 xpt_print(periph->path, "Spin-down disk failed\n");
1931 xpt_release_ccb(ccb);
1932 cam_periph_unlock(periph);
1937 adashutdown(void *arg, int howto)
1941 if (ada_spindown_shutdown != 0 &&
1942 (howto & (RB_HALT | RB_POWEROFF)) != 0)
1943 adaspindown(ATA_STANDBY_IMMEDIATE, 0);
1947 adasuspend(void *arg)
1951 if (ada_spindown_suspend != 0)
1952 adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE);
1956 adaresume(void *arg)
1958 struct cam_periph *periph;
1959 struct ada_softc *softc;
1961 if (ada_spindown_suspend == 0)
1964 CAM_PERIPH_FOREACH(periph, &adadriver) {
1965 cam_periph_lock(periph);
1966 softc = (struct ada_softc *)periph->softc;
1968 * We only spin-down the drive if it is capable of it..
1970 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
1971 cam_periph_unlock(periph);
1976 xpt_print(periph->path, "resume\n");
1979 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on
1982 cam_release_devq(periph->path,
1986 /*getcount_only*/0);
1988 cam_periph_unlock(periph);
1992 #endif /* _KERNEL */