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;
133 int ordered_tag_count;
134 int outstanding_cmds;
139 #ifdef ADA_TEST_FAILURE
140 int force_read_error;
141 int force_write_error;
142 int periodic_read_error;
143 int periodic_read_count;
145 struct disk_params params;
147 struct task sysctl_task;
148 struct sysctl_ctx_list sysctl_ctx;
149 struct sysctl_oid *sysctl_tree;
150 struct callout sendordered_c;
151 struct trim_request trim_req;
154 struct ada_quirk_entry {
155 struct scsi_inquiry_pattern inq_pat;
159 static struct ada_quirk_entry ada_quirk_table[] =
162 /* Hitachi Advanced Format (4k) drives */
163 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Hitachi H??????????E3*", "*" },
167 /* Samsung Advanced Format (4k) drives */
168 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD155UI*", "*" },
172 /* Samsung Advanced Format (4k) drives */
173 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD204UI*", "*" },
177 /* Seagate Barracuda Green Advanced Format (4k) drives */
178 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DL*", "*" },
182 /* Seagate Barracuda Advanced Format (4k) drives */
183 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???DM*", "*" },
187 /* Seagate Barracuda Advanced Format (4k) drives */
188 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DM*", "*" },
192 /* Seagate Momentus Advanced Format (4k) drives */
193 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500423AS*", "*" },
197 /* Seagate Momentus Advanced Format (4k) drives */
198 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500424AS*", "*" },
202 /* Seagate Momentus Advanced Format (4k) drives */
203 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640423AS*", "*" },
207 /* Seagate Momentus Advanced Format (4k) drives */
208 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9640424AS*", "*" },
212 /* Seagate Momentus Advanced Format (4k) drives */
213 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750420AS*", "*" },
217 /* Seagate Momentus Advanced Format (4k) drives */
218 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750422AS*", "*" },
222 /* Seagate Momentus Advanced Format (4k) drives */
223 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750423AS*", "*" },
227 /* Seagate Momentus Thin Advanced Format (4k) drives */
228 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???LT*", "*" },
232 /* WDC Caviar Green Advanced Format (4k) drives */
233 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RS*", "*" },
237 /* WDC Caviar Green Advanced Format (4k) drives */
238 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RX*", "*" },
242 /* WDC Caviar Green Advanced Format (4k) drives */
243 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RS*", "*" },
247 /* WDC Caviar Green Advanced Format (4k) drives */
248 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RX*", "*" },
252 /* WDC Scorpio Black Advanced Format (4k) drives */
253 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PKT*", "*" },
257 /* WDC Scorpio Black Advanced Format (4k) drives */
258 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PKT*", "*" },
262 /* WDC Scorpio Blue Advanced Format (4k) drives */
263 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PVT*", "*" },
267 /* WDC Scorpio Blue Advanced Format (4k) drives */
268 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PVT*", "*" },
273 * Corsair Force 2 SSDs
274 * 4k optimised & trim only works in 4k requests + 4k aligned
275 * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk>
278 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair CSSD-F*", "*" },
283 * Corsair Force 3 SSDs
284 * 4k optimised & trim only works in 4k requests + 4k aligned
285 * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk>
288 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Force 3*", "*" },
294 * 4k optimised & trim only works in 4k requests + 4k aligned
295 * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk>
298 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY3*", "*" },
303 * OCZ Vertex 2 SSDs (inc pro series)
304 * 4k optimised & trim only works in 4k requests + 4k aligned
305 * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk>
308 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ?VERTEX2*", "*" },
314 * 4k optimised & trim only works in 4k requests + 4k aligned
315 * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk>
318 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-VERTEX3*", "*" },
323 * SuperTalent TeraDrive CT SSDs
324 * 4k optimised & trim only works in 4k requests + 4k aligned
325 * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk>
328 { T_DIRECT, SIP_MEDIA_FIXED, "*", "FTM??CT25H*", "*" },
333 * Crucial RealSSD C300 SSDs
335 * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk>
338 { T_DIRECT, SIP_MEDIA_FIXED, "*", "C300-CTFDDAC???MAG*",
339 "*" }, /*quirks*/ADA_Q_4K
343 * XceedIOPS SATA SSDs
345 * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk>
348 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SG9XCS2D*", "*" },
353 * Intel 330 Series SSDs
354 * 4k optimised & trim only works in 4k requests + 4k aligned
355 * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk>
358 { T_DIRECT, SIP_MEDIA_FIXED, "*", "INTEL SSDSC2ct*", "*" },
363 * OCZ Deneva R Series SSDs
364 * 4k optimised & trim only works in 4k requests + 4k aligned
365 * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk>
368 { T_DIRECT, SIP_MEDIA_FIXED, "*", "DENRSTE251M45*", "*" },
373 * Kingston HyperX 3k SSDs
374 * 4k optimised & trim only works in 4k requests + 4k aligned
375 * Submitted by: Steven Hartland <steven.hartland@multiplay.co.uk>
378 { T_DIRECT, SIP_MEDIA_FIXED, "*", "KINGSTON SH103S3*", "*" },
384 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
385 /*vendor*/"*", /*product*/"*", /*revision*/"*"
391 static disk_strategy_t adastrategy;
392 static dumper_t adadump;
393 static periph_init_t adainit;
394 static void adaasync(void *callback_arg, u_int32_t code,
395 struct cam_path *path, void *arg);
396 static void adasysctlinit(void *context, int pending);
397 static periph_ctor_t adaregister;
398 static periph_dtor_t adacleanup;
399 static periph_start_t adastart;
400 static periph_oninv_t adaoninvalidate;
401 static void adadone(struct cam_periph *periph,
402 union ccb *done_ccb);
403 static int adaerror(union ccb *ccb, u_int32_t cam_flags,
404 u_int32_t sense_flags);
405 static void adagetparams(struct cam_periph *periph,
406 struct ccb_getdev *cgd);
407 static timeout_t adasendorderedtag;
408 static void adashutdown(void *arg, int howto);
409 static void adasuspend(void *arg);
410 static void adaresume(void *arg);
412 #ifndef ADA_DEFAULT_LEGACY_ALIASES
414 #define ADA_DEFAULT_LEGACY_ALIASES 1
416 #define ADA_DEFAULT_LEGACY_ALIASES 0
420 #ifndef ADA_DEFAULT_TIMEOUT
421 #define ADA_DEFAULT_TIMEOUT 30 /* Timeout in seconds */
424 #ifndef ADA_DEFAULT_RETRY
425 #define ADA_DEFAULT_RETRY 4
428 #ifndef ADA_DEFAULT_SEND_ORDERED
429 #define ADA_DEFAULT_SEND_ORDERED 1
432 #ifndef ADA_DEFAULT_SPINDOWN_SHUTDOWN
433 #define ADA_DEFAULT_SPINDOWN_SHUTDOWN 1
436 #ifndef ADA_DEFAULT_SPINDOWN_SUSPEND
437 #define ADA_DEFAULT_SPINDOWN_SUSPEND 1
440 #ifndef ADA_DEFAULT_READ_AHEAD
441 #define ADA_DEFAULT_READ_AHEAD 1
444 #ifndef ADA_DEFAULT_WRITE_CACHE
445 #define ADA_DEFAULT_WRITE_CACHE 1
448 #define ADA_RA (softc->read_ahead >= 0 ? \
449 softc->read_ahead : ada_read_ahead)
450 #define ADA_WC (softc->write_cache >= 0 ? \
451 softc->write_cache : ada_write_cache)
454 * Most platforms map firmware geometry to actual, but some don't. If
455 * not overridden, default to nothing.
457 #ifndef ata_disk_firmware_geom_adjust
458 #define ata_disk_firmware_geom_adjust(disk)
461 static int ada_legacy_aliases = ADA_DEFAULT_LEGACY_ALIASES;
462 static int ada_retry_count = ADA_DEFAULT_RETRY;
463 static int ada_default_timeout = ADA_DEFAULT_TIMEOUT;
464 static int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED;
465 static int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN;
466 static int ada_spindown_suspend = ADA_DEFAULT_SPINDOWN_SUSPEND;
467 static int ada_read_ahead = ADA_DEFAULT_READ_AHEAD;
468 static int ada_write_cache = ADA_DEFAULT_WRITE_CACHE;
470 static SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD, 0,
471 "CAM Direct Access Disk driver");
472 SYSCTL_INT(_kern_cam_ada, OID_AUTO, legacy_aliases, CTLFLAG_RW,
473 &ada_legacy_aliases, 0, "Create legacy-like device aliases");
474 TUNABLE_INT("kern.cam.ada.legacy_aliases", &ada_legacy_aliases);
475 SYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RW,
476 &ada_retry_count, 0, "Normal I/O retry count");
477 TUNABLE_INT("kern.cam.ada.retry_count", &ada_retry_count);
478 SYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RW,
479 &ada_default_timeout, 0, "Normal I/O timeout (in seconds)");
480 TUNABLE_INT("kern.cam.ada.default_timeout", &ada_default_timeout);
481 SYSCTL_INT(_kern_cam_ada, OID_AUTO, send_ordered, CTLFLAG_RW,
482 &ada_send_ordered, 0, "Send Ordered Tags");
483 TUNABLE_INT("kern.cam.ada.send_ordered", &ada_send_ordered);
484 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RW,
485 &ada_spindown_shutdown, 0, "Spin down upon shutdown");
486 TUNABLE_INT("kern.cam.ada.spindown_shutdown", &ada_spindown_shutdown);
487 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_suspend, CTLFLAG_RW,
488 &ada_spindown_suspend, 0, "Spin down upon suspend");
489 TUNABLE_INT("kern.cam.ada.spindown_suspend", &ada_spindown_suspend);
490 SYSCTL_INT(_kern_cam_ada, OID_AUTO, read_ahead, CTLFLAG_RW,
491 &ada_read_ahead, 0, "Enable disk read-ahead");
492 TUNABLE_INT("kern.cam.ada.read_ahead", &ada_read_ahead);
493 SYSCTL_INT(_kern_cam_ada, OID_AUTO, write_cache, CTLFLAG_RW,
494 &ada_write_cache, 0, "Enable disk write cache");
495 TUNABLE_INT("kern.cam.ada.write_cache", &ada_write_cache);
498 * ADA_ORDEREDTAG_INTERVAL determines how often, relative
499 * to the default timeout, we check to see whether an ordered
500 * tagged transaction is appropriate to prevent simple tag
501 * starvation. Since we'd like to ensure that there is at least
502 * 1/2 of the timeout length left for a starved transaction to
503 * complete after we've sent an ordered tag, we must poll at least
504 * four times in every timeout period. This takes care of the worst
505 * case where a starved transaction starts during an interval that
506 * meets the requirement "don't send an ordered tag" test so it takes
507 * us two intervals to determine that a tag must be sent.
509 #ifndef ADA_ORDEREDTAG_INTERVAL
510 #define ADA_ORDEREDTAG_INTERVAL 4
513 static struct periph_driver adadriver =
516 TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0
519 PERIPHDRIVER_DECLARE(ada, adadriver);
521 MALLOC_DEFINE(M_ATADA, "ata_da", "ata_da buffers");
524 adaopen(struct disk *dp)
526 struct cam_periph *periph;
527 struct ada_softc *softc;
530 periph = (struct cam_periph *)dp->d_drv1;
531 if (periph == NULL) {
535 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
539 cam_periph_lock(periph);
540 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
541 cam_periph_unlock(periph);
542 cam_periph_release(periph);
546 softc = (struct ada_softc *)periph->softc;
547 softc->flags |= ADA_FLAG_OPEN;
549 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
552 if ((softc->flags & ADA_FLAG_PACK_INVALID) != 0) {
553 /* Invalidate our pack information. */
554 softc->flags &= ~ADA_FLAG_PACK_INVALID;
557 cam_periph_unhold(periph);
558 cam_periph_unlock(periph);
563 adaclose(struct disk *dp)
565 struct cam_periph *periph;
566 struct ada_softc *softc;
569 periph = (struct cam_periph *)dp->d_drv1;
573 cam_periph_lock(periph);
574 if (cam_periph_hold(periph, PRIBIO) != 0) {
575 cam_periph_unlock(periph);
576 cam_periph_release(periph);
580 softc = (struct ada_softc *)periph->softc;
582 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
585 /* We only sync the cache if the drive is capable of it. */
586 if ((softc->flags & ADA_FLAG_CAN_FLUSHCACHE) != 0 &&
587 (softc->flags & ADA_FLAG_PACK_INVALID) == 0) {
589 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
590 cam_fill_ataio(&ccb->ataio,
597 ada_default_timeout*1000);
599 if (softc->flags & ADA_FLAG_CAN_48BIT)
600 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
602 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
603 cam_periph_runccb(ccb, adaerror, /*cam_flags*/0,
604 /*sense_flags*/0, softc->disk->d_devstat);
606 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
607 xpt_print(periph->path, "Synchronize cache failed\n");
608 xpt_release_ccb(ccb);
611 softc->flags &= ~ADA_FLAG_OPEN;
612 cam_periph_unhold(periph);
613 cam_periph_unlock(periph);
614 cam_periph_release(periph);
619 adaschedule(struct cam_periph *periph)
621 struct ada_softc *softc = (struct ada_softc *)periph->softc;
624 /* Check if cam_periph_getccb() was called. */
625 prio = periph->immediate_priority;
627 /* Check if we have more work to do. */
628 if (bioq_first(&softc->bio_queue) ||
629 (!softc->trim_running && bioq_first(&softc->trim_queue))) {
630 prio = CAM_PRIORITY_NORMAL;
633 /* Schedule CCB if any of above is true. */
634 if (prio != CAM_PRIORITY_NONE)
635 xpt_schedule(periph, prio);
639 * Actually translate the requested transfer into one the physical driver
640 * can understand. The transfer is described by a buf and will include
641 * only one physical transfer.
644 adastrategy(struct bio *bp)
646 struct cam_periph *periph;
647 struct ada_softc *softc;
649 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
650 if (periph == NULL) {
651 biofinish(bp, NULL, ENXIO);
654 softc = (struct ada_softc *)periph->softc;
656 cam_periph_lock(periph);
658 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastrategy(%p)\n", bp));
661 * If the device has been made invalid, error out
663 if ((softc->flags & ADA_FLAG_PACK_INVALID)) {
664 cam_periph_unlock(periph);
665 biofinish(bp, NULL, ENXIO);
670 * Place it in the queue of disk activities for this disk
672 if (bp->bio_cmd == BIO_DELETE &&
673 (softc->flags & ADA_FLAG_CAN_TRIM))
674 bioq_disksort(&softc->trim_queue, bp);
676 bioq_disksort(&softc->bio_queue, bp);
679 * Schedule ourselves for performing the work.
682 cam_periph_unlock(periph);
688 adadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
690 struct cam_periph *periph;
691 struct ada_softc *softc;
703 softc = (struct ada_softc *)periph->softc;
704 cam_periph_lock(periph);
705 secsize = softc->params.secsize;
706 lba = offset / secsize;
707 count = length / secsize;
709 if ((softc->flags & ADA_FLAG_PACK_INVALID) != 0) {
710 cam_periph_unlock(periph);
715 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
716 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
717 cam_fill_ataio(&ccb.ataio,
722 (u_int8_t *) virtual,
724 ada_default_timeout*1000);
725 if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
726 (lba + count >= ATA_MAX_28BIT_LBA ||
728 ata_48bit_cmd(&ccb.ataio, ATA_WRITE_DMA48,
731 ata_28bit_cmd(&ccb.ataio, ATA_WRITE_DMA,
734 xpt_polled_action(&ccb);
736 error = cam_periph_error(&ccb,
737 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
738 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
739 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
740 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
742 printf("Aborting dump due to I/O error.\n");
744 cam_periph_unlock(periph);
748 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) {
749 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
751 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
752 cam_fill_ataio(&ccb.ataio,
759 ada_default_timeout*1000);
761 if (softc->flags & ADA_FLAG_CAN_48BIT)
762 ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0);
764 ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0);
765 xpt_polled_action(&ccb);
767 error = cam_periph_error(&ccb,
768 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
769 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
770 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
771 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
773 xpt_print(periph->path, "Synchronize cache failed\n");
775 cam_periph_unlock(periph);
785 * Install a global async callback. This callback will
786 * receive async callbacks like "new device found".
788 status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL);
790 if (status != CAM_REQ_CMP) {
791 printf("ada: Failed to attach master async callback "
792 "due to status 0x%x!\n", status);
793 } else if (ada_send_ordered) {
795 /* Register our event handlers */
796 if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend,
797 NULL, EVENTHANDLER_PRI_LAST)) == NULL)
798 printf("adainit: power event registration failed!\n");
799 if ((EVENTHANDLER_REGISTER(power_resume, adaresume,
800 NULL, EVENTHANDLER_PRI_LAST)) == NULL)
801 printf("adainit: power event registration failed!\n");
802 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown,
803 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
804 printf("adainit: shutdown event registration failed!\n");
809 adaoninvalidate(struct cam_periph *periph)
811 struct ada_softc *softc;
813 softc = (struct ada_softc *)periph->softc;
816 * De-register any async callbacks.
818 xpt_register_async(0, adaasync, periph, periph->path);
820 softc->flags |= ADA_FLAG_PACK_INVALID;
823 * Return all queued I/O with ENXIO.
824 * XXX Handle any transactions queued to the card
825 * with XPT_ABORT_CCB.
827 bioq_flush(&softc->bio_queue, NULL, ENXIO);
828 bioq_flush(&softc->trim_queue, NULL, ENXIO);
830 disk_gone(softc->disk);
831 xpt_print(periph->path, "lost device\n");
835 adacleanup(struct cam_periph *periph)
837 struct ada_softc *softc;
839 softc = (struct ada_softc *)periph->softc;
841 xpt_print(periph->path, "removing device entry\n");
842 cam_periph_unlock(periph);
845 * If we can't free the sysctl tree, oh well...
847 if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0
848 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
849 xpt_print(periph->path, "can't remove sysctl context\n");
852 disk_destroy(softc->disk);
853 callout_drain(&softc->sendordered_c);
854 free(softc, M_DEVBUF);
855 cam_periph_lock(periph);
859 adaasync(void *callback_arg, u_int32_t code,
860 struct cam_path *path, void *arg)
862 struct ccb_getdev cgd;
863 struct cam_periph *periph;
864 struct ada_softc *softc;
866 periph = (struct cam_periph *)callback_arg;
868 case AC_FOUND_DEVICE:
870 struct ccb_getdev *cgd;
873 cgd = (struct ccb_getdev *)arg;
877 if (cgd->protocol != PROTO_ATA)
881 * Allocate a peripheral instance for
882 * this device and start the probe
885 status = cam_periph_alloc(adaregister, adaoninvalidate,
886 adacleanup, adastart,
887 "ada", CAM_PERIPH_BIO,
888 cgd->ccb_h.path, adaasync,
889 AC_FOUND_DEVICE, cgd);
891 if (status != CAM_REQ_CMP
892 && status != CAM_REQ_INPROG)
893 printf("adaasync: Unable to attach to new device "
894 "due to status 0x%x\n", status);
897 case AC_GETDEV_CHANGED:
899 softc = (struct ada_softc *)periph->softc;
900 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
901 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
902 xpt_action((union ccb *)&cgd);
904 if ((cgd.ident_data.capabilities1 & ATA_SUPPORT_DMA) &&
905 (cgd.inq_flags & SID_DMA))
906 softc->flags |= ADA_FLAG_CAN_DMA;
908 softc->flags &= ~ADA_FLAG_CAN_DMA;
909 if ((cgd.ident_data.satacapabilities & ATA_SUPPORT_NCQ) &&
910 (cgd.inq_flags & SID_DMA) && (cgd.inq_flags & SID_CmdQue))
911 softc->flags |= ADA_FLAG_CAN_NCQ;
913 softc->flags &= ~ADA_FLAG_CAN_NCQ;
914 if ((cgd.ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) &&
915 (cgd.inq_flags & SID_DMA))
916 softc->flags |= ADA_FLAG_CAN_TRIM;
918 softc->flags &= ~ADA_FLAG_CAN_TRIM;
920 cam_periph_async(periph, code, path, arg);
923 case AC_ADVINFO_CHANGED:
927 buftype = (uintptr_t)arg;
928 if (buftype == CDAI_TYPE_PHYS_PATH) {
929 struct ada_softc *softc;
931 softc = periph->softc;
932 disk_attr_changed(softc->disk, "GEOM::physpath",
940 softc = (struct ada_softc *)periph->softc;
941 cam_periph_async(periph, code, path, arg);
942 if (softc->state != ADA_STATE_NORMAL)
944 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
945 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
946 xpt_action((union ccb *)&cgd);
948 cgd.ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD)
949 softc->state = ADA_STATE_RAHEAD;
950 else if (ADA_WC >= 0 &&
951 cgd.ident_data.support.command1 & ATA_SUPPORT_WRITECACHE)
952 softc->state = ADA_STATE_WCACHE;
955 cam_periph_acquire(periph);
956 cam_freeze_devq_arg(periph->path,
957 RELSIM_RELEASE_RUNLEVEL, CAM_RL_DEV + 1);
958 xpt_schedule(periph, CAM_PRIORITY_DEV);
961 cam_periph_async(periph, code, path, arg);
967 adasysctlinit(void *context, int pending)
969 struct cam_periph *periph;
970 struct ada_softc *softc;
971 char tmpstr[80], tmpstr2[80];
973 periph = (struct cam_periph *)context;
975 /* periph was held for us when this task was enqueued */
976 if (periph->flags & CAM_PERIPH_INVALID) {
977 cam_periph_release(periph);
981 softc = (struct ada_softc *)periph->softc;
982 snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d", periph->unit_number);
983 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
985 sysctl_ctx_init(&softc->sysctl_ctx);
986 softc->flags |= ADA_FLAG_SCTX_INIT;
987 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
988 SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2,
989 CTLFLAG_RD, 0, tmpstr);
990 if (softc->sysctl_tree == NULL) {
991 printf("adasysctlinit: unable to allocate sysctl tree\n");
992 cam_periph_release(periph);
996 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
997 OID_AUTO, "read_ahead", CTLFLAG_RW | CTLFLAG_MPSAFE,
998 &softc->read_ahead, 0, "Enable disk read ahead.");
999 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1000 OID_AUTO, "write_cache", CTLFLAG_RW | CTLFLAG_MPSAFE,
1001 &softc->write_cache, 0, "Enable disk write cache.");
1002 #ifdef ADA_TEST_FAILURE
1004 * Add a 'door bell' sysctl which allows one to set it from userland
1005 * and cause something bad to happen. For the moment, we only allow
1006 * whacking the next read or write.
1008 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1009 OID_AUTO, "force_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1010 &softc->force_read_error, 0,
1011 "Force a read error for the next N reads.");
1012 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1013 OID_AUTO, "force_write_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1014 &softc->force_write_error, 0,
1015 "Force a write error for the next N writes.");
1016 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1017 OID_AUTO, "periodic_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
1018 &softc->periodic_read_error, 0,
1019 "Force a read error every N reads (don't set too low).");
1021 cam_periph_release(periph);
1025 adagetattr(struct bio *bp)
1028 struct cam_periph *periph;
1030 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1034 cam_periph_lock(periph);
1035 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
1037 cam_periph_unlock(periph);
1039 bp->bio_completed = bp->bio_length;
1044 adaregister(struct cam_periph *periph, void *arg)
1046 struct ada_softc *softc;
1047 struct ccb_pathinq cpi;
1048 struct ccb_getdev *cgd;
1049 char announce_buf[80], buf1[32];
1050 struct disk_params *dp;
1053 int legacy_id, quirks;
1055 cgd = (struct ccb_getdev *)arg;
1057 printf("adaregister: no getdev CCB, can't register device\n");
1058 return(CAM_REQ_CMP_ERR);
1061 softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF,
1064 if (softc == NULL) {
1065 printf("adaregister: Unable to probe new device. "
1066 "Unable to allocate softc\n");
1067 return(CAM_REQ_CMP_ERR);
1070 bioq_init(&softc->bio_queue);
1071 bioq_init(&softc->trim_queue);
1073 if ((cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA) &&
1074 (cgd->inq_flags & SID_DMA))
1075 softc->flags |= ADA_FLAG_CAN_DMA;
1076 if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48)
1077 softc->flags |= ADA_FLAG_CAN_48BIT;
1078 if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE)
1079 softc->flags |= ADA_FLAG_CAN_FLUSHCACHE;
1080 if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT)
1081 softc->flags |= ADA_FLAG_CAN_POWERMGT;
1082 if ((cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ) &&
1083 (cgd->inq_flags & SID_DMA) && (cgd->inq_flags & SID_CmdQue))
1084 softc->flags |= ADA_FLAG_CAN_NCQ;
1085 if ((cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) &&
1086 (cgd->inq_flags & SID_DMA)) {
1087 softc->flags |= ADA_FLAG_CAN_TRIM;
1088 softc->trim_max_ranges = TRIM_MAX_RANGES;
1089 if (cgd->ident_data.max_dsm_blocks != 0) {
1090 softc->trim_max_ranges =
1091 min(cgd->ident_data.max_dsm_blocks * 64,
1092 softc->trim_max_ranges);
1095 if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA)
1096 softc->flags |= ADA_FLAG_CAN_CFA;
1098 periph->softc = softc;
1101 * See if this device has any quirks.
1103 match = cam_quirkmatch((caddr_t)&cgd->ident_data,
1104 (caddr_t)ada_quirk_table,
1105 sizeof(ada_quirk_table)/sizeof(*ada_quirk_table),
1106 sizeof(*ada_quirk_table), ata_identify_match);
1108 softc->quirks = ((struct ada_quirk_entry *)match)->quirks;
1110 softc->quirks = ADA_Q_NONE;
1112 bzero(&cpi, sizeof(cpi));
1113 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE);
1114 cpi.ccb_h.func_code = XPT_PATH_INQ;
1115 xpt_action((union ccb *)&cpi);
1117 TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph);
1120 * Register this media as a disk
1122 (void)cam_periph_hold(periph, PRIBIO);
1123 mtx_unlock(periph->sim->mtx);
1124 snprintf(announce_buf, sizeof(announce_buf),
1125 "kern.cam.ada.%d.quirks", periph->unit_number);
1126 quirks = softc->quirks;
1127 TUNABLE_INT_FETCH(announce_buf, &quirks);
1128 softc->quirks = quirks;
1129 softc->read_ahead = -1;
1130 snprintf(announce_buf, sizeof(announce_buf),
1131 "kern.cam.ada.%d.read_ahead", periph->unit_number);
1132 TUNABLE_INT_FETCH(announce_buf, &softc->read_ahead);
1133 softc->write_cache = -1;
1134 snprintf(announce_buf, sizeof(announce_buf),
1135 "kern.cam.ada.%d.write_cache", periph->unit_number);
1136 TUNABLE_INT_FETCH(announce_buf, &softc->write_cache);
1137 adagetparams(periph, cgd);
1138 softc->disk = disk_alloc();
1139 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
1140 periph->unit_number, softc->params.secsize,
1141 DEVSTAT_ALL_SUPPORTED,
1142 DEVSTAT_TYPE_DIRECT |
1143 XPORT_DEVSTAT_TYPE(cpi.transport),
1144 DEVSTAT_PRIORITY_DISK);
1145 softc->disk->d_open = adaopen;
1146 softc->disk->d_close = adaclose;
1147 softc->disk->d_strategy = adastrategy;
1148 softc->disk->d_getattr = adagetattr;
1149 softc->disk->d_dump = adadump;
1150 softc->disk->d_name = "ada";
1151 softc->disk->d_drv1 = periph;
1152 maxio = cpi.maxio; /* Honor max I/O size of SIM */
1154 maxio = DFLTPHYS; /* traditional default */
1155 else if (maxio > MAXPHYS)
1156 maxio = MAXPHYS; /* for safety */
1157 if (softc->flags & ADA_FLAG_CAN_48BIT)
1158 maxio = min(maxio, 65536 * softc->params.secsize);
1159 else /* 28bit ATA command limit */
1160 maxio = min(maxio, 256 * softc->params.secsize);
1161 softc->disk->d_maxsize = maxio;
1162 softc->disk->d_unit = periph->unit_number;
1163 softc->disk->d_flags = 0;
1164 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE)
1165 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
1166 if ((softc->flags & ADA_FLAG_CAN_TRIM) ||
1167 ((softc->flags & ADA_FLAG_CAN_CFA) &&
1168 !(softc->flags & ADA_FLAG_CAN_48BIT)))
1169 softc->disk->d_flags |= DISKFLAG_CANDELETE;
1170 strlcpy(softc->disk->d_descr, cgd->ident_data.model,
1171 MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model)));
1172 strlcpy(softc->disk->d_ident, cgd->ident_data.serial,
1173 MIN(sizeof(softc->disk->d_ident), sizeof(cgd->ident_data.serial)));
1174 softc->disk->d_hba_vendor = cpi.hba_vendor;
1175 softc->disk->d_hba_device = cpi.hba_device;
1176 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
1177 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
1179 softc->disk->d_sectorsize = softc->params.secsize;
1180 softc->disk->d_mediasize = (off_t)softc->params.sectors *
1181 softc->params.secsize;
1182 if (ata_physical_sector_size(&cgd->ident_data) !=
1183 softc->params.secsize) {
1184 softc->disk->d_stripesize =
1185 ata_physical_sector_size(&cgd->ident_data);
1186 softc->disk->d_stripeoffset = (softc->disk->d_stripesize -
1187 ata_logical_sector_offset(&cgd->ident_data)) %
1188 softc->disk->d_stripesize;
1189 } else if (softc->quirks & ADA_Q_4K) {
1190 softc->disk->d_stripesize = 4096;
1191 softc->disk->d_stripeoffset = 0;
1193 softc->disk->d_fwsectors = softc->params.secs_per_track;
1194 softc->disk->d_fwheads = softc->params.heads;
1195 ata_disk_firmware_geom_adjust(softc->disk);
1197 if (ada_legacy_aliases) {
1198 #ifdef ATA_STATIC_ID
1199 legacy_id = xpt_path_legacy_ata_id(periph->path);
1201 legacy_id = softc->disk->d_unit;
1203 if (legacy_id >= 0) {
1204 snprintf(announce_buf, sizeof(announce_buf),
1205 "kern.devalias.%s%d",
1206 softc->disk->d_name, softc->disk->d_unit);
1207 snprintf(buf1, sizeof(buf1),
1209 setenv(announce_buf, buf1);
1213 disk_create(softc->disk, DISK_VERSION);
1214 mtx_lock(periph->sim->mtx);
1215 cam_periph_unhold(periph);
1217 dp = &softc->params;
1218 snprintf(announce_buf, sizeof(announce_buf),
1219 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1220 (uintmax_t)(((uintmax_t)dp->secsize *
1221 dp->sectors) / (1024*1024)),
1222 (uintmax_t)dp->sectors,
1223 dp->secsize, dp->heads,
1224 dp->secs_per_track, dp->cylinders);
1225 xpt_announce_periph(periph, announce_buf);
1227 printf("%s%d: Previously was known as ad%d\n",
1228 periph->periph_name, periph->unit_number, legacy_id);
1231 * Create our sysctl variables, now that we know
1232 * we have successfully attached.
1234 cam_periph_acquire(periph);
1235 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task);
1238 * Add async callbacks for bus reset and
1239 * bus device reset calls. I don't bother
1240 * checking if this fails as, in most cases,
1241 * the system will function just fine without
1242 * them and the only alternative would be to
1243 * not attach the device on failure.
1245 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
1246 AC_GETDEV_CHANGED | AC_ADVINFO_CHANGED,
1247 adaasync, periph, periph->path);
1250 * Schedule a periodic event to occasionally send an
1251 * ordered tag to a device.
1253 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
1254 callout_reset(&softc->sendordered_c,
1255 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL,
1256 adasendorderedtag, softc);
1259 cgd->ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD) {
1260 softc->state = ADA_STATE_RAHEAD;
1261 cam_periph_acquire(periph);
1262 cam_freeze_devq_arg(periph->path,
1263 RELSIM_RELEASE_RUNLEVEL, CAM_RL_DEV + 1);
1264 xpt_schedule(periph, CAM_PRIORITY_DEV);
1265 } else if (ADA_WC >= 0 &&
1266 cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) {
1267 softc->state = ADA_STATE_WCACHE;
1268 cam_periph_acquire(periph);
1269 cam_freeze_devq_arg(periph->path,
1270 RELSIM_RELEASE_RUNLEVEL, CAM_RL_DEV + 1);
1271 xpt_schedule(periph, CAM_PRIORITY_DEV);
1273 softc->state = ADA_STATE_NORMAL;
1275 return(CAM_REQ_CMP);
1279 adastart(struct cam_periph *periph, union ccb *start_ccb)
1281 struct ada_softc *softc = (struct ada_softc *)periph->softc;
1282 struct ccb_ataio *ataio = &start_ccb->ataio;
1284 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adastart\n"));
1286 switch (softc->state) {
1287 case ADA_STATE_NORMAL:
1292 /* Execute immediate CCB if waiting. */
1293 if (periph->immediate_priority <= periph->pinfo.priority) {
1294 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
1295 ("queuing for immediate ccb\n"));
1296 start_ccb->ccb_h.ccb_state = ADA_CCB_WAITING;
1297 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1299 periph->immediate_priority = CAM_PRIORITY_NONE;
1300 wakeup(&periph->ccb_list);
1301 /* Have more work to do, so ensure we stay scheduled */
1302 adaschedule(periph);
1305 /* Run TRIM if not running yet. */
1306 if (!softc->trim_running &&
1307 (bp = bioq_first(&softc->trim_queue)) != 0) {
1308 struct trim_request *req = &softc->trim_req;
1310 uint64_t lastlba = (uint64_t)-1;
1311 int bps = 0, c, lastcount = 0, off, ranges = 0;
1313 softc->trim_running = 1;
1314 bzero(req, sizeof(*req));
1317 uint64_t lba = bp1->bio_pblkno;
1318 int count = bp1->bio_bcount /
1319 softc->params.secsize;
1321 bioq_remove(&softc->trim_queue, bp1);
1323 /* Try to extend the previous range. */
1324 if (lba == lastlba) {
1325 c = min(count, 0xffff - lastcount);
1327 off = (ranges - 1) * 8;
1328 req->data[off + 6] = lastcount & 0xff;
1329 req->data[off + 7] =
1330 (lastcount >> 8) & 0xff;
1336 c = min(count, 0xffff);
1338 req->data[off + 0] = lba & 0xff;
1339 req->data[off + 1] = (lba >> 8) & 0xff;
1340 req->data[off + 2] = (lba >> 16) & 0xff;
1341 req->data[off + 3] = (lba >> 24) & 0xff;
1342 req->data[off + 4] = (lba >> 32) & 0xff;
1343 req->data[off + 5] = (lba >> 40) & 0xff;
1344 req->data[off + 6] = c & 0xff;
1345 req->data[off + 7] = (c >> 8) & 0xff;
1352 req->bps[bps++] = bp1;
1353 bp1 = bioq_first(&softc->trim_queue);
1354 if (bps >= TRIM_MAX_BIOS ||
1356 bp1->bio_bcount / softc->params.secsize >
1357 (softc->trim_max_ranges - ranges) * 0xffff)
1360 cam_fill_ataio(ataio,
1366 ((ranges + 63) / 64) * 512,
1367 ada_default_timeout * 1000);
1368 ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT,
1369 ATA_DSM_TRIM, 0, (ranges + 63) / 64);
1370 start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM;
1373 /* Run regular command. */
1374 bp = bioq_first(&softc->bio_queue);
1376 xpt_release_ccb(start_ccb);
1379 bioq_remove(&softc->bio_queue, bp);
1381 if ((bp->bio_flags & BIO_ORDERED) != 0
1382 || (softc->flags & ADA_FLAG_NEED_OTAG) != 0) {
1383 softc->flags &= ~ADA_FLAG_NEED_OTAG;
1384 softc->ordered_tag_count++;
1389 switch (bp->bio_cmd) {
1393 uint64_t lba = bp->bio_pblkno;
1394 uint16_t count = bp->bio_bcount / softc->params.secsize;
1395 #ifdef ADA_TEST_FAILURE
1399 * Support the failure ioctls. If the command is a
1400 * read, and there are pending forced read errors, or
1401 * if a write and pending write errors, then fail this
1402 * operation with EIO. This is useful for testing
1403 * purposes. Also, support having every Nth read fail.
1405 * This is a rather blunt tool.
1407 if (bp->bio_cmd == BIO_READ) {
1408 if (softc->force_read_error) {
1409 softc->force_read_error--;
1412 if (softc->periodic_read_error > 0) {
1413 if (++softc->periodic_read_count >=
1414 softc->periodic_read_error) {
1415 softc->periodic_read_count = 0;
1420 if (softc->force_write_error) {
1421 softc->force_write_error--;
1426 bp->bio_error = EIO;
1427 bp->bio_flags |= BIO_ERROR;
1429 xpt_release_ccb(start_ccb);
1430 adaschedule(periph);
1434 cam_fill_ataio(ataio,
1437 bp->bio_cmd == BIO_READ ?
1438 CAM_DIR_IN : CAM_DIR_OUT,
1442 ada_default_timeout*1000);
1444 if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) {
1445 if (bp->bio_cmd == BIO_READ) {
1446 ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED,
1449 ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED,
1452 } else if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
1453 (lba + count >= ATA_MAX_28BIT_LBA ||
1455 if (softc->flags & ADA_FLAG_CAN_DMA) {
1456 if (bp->bio_cmd == BIO_READ) {
1457 ata_48bit_cmd(ataio, ATA_READ_DMA48,
1460 ata_48bit_cmd(ataio, ATA_WRITE_DMA48,
1464 if (bp->bio_cmd == BIO_READ) {
1465 ata_48bit_cmd(ataio, ATA_READ_MUL48,
1468 ata_48bit_cmd(ataio, ATA_WRITE_MUL48,
1475 if (softc->flags & ADA_FLAG_CAN_DMA) {
1476 if (bp->bio_cmd == BIO_READ) {
1477 ata_28bit_cmd(ataio, ATA_READ_DMA,
1480 ata_28bit_cmd(ataio, ATA_WRITE_DMA,
1484 if (bp->bio_cmd == BIO_READ) {
1485 ata_28bit_cmd(ataio, ATA_READ_MUL,
1488 ata_28bit_cmd(ataio, ATA_WRITE_MUL,
1497 uint64_t lba = bp->bio_pblkno;
1498 uint16_t count = bp->bio_bcount / softc->params.secsize;
1500 cam_fill_ataio(ataio,
1507 ada_default_timeout*1000);
1511 ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count);
1515 cam_fill_ataio(ataio,
1522 ada_default_timeout*1000);
1524 if (softc->flags & ADA_FLAG_CAN_48BIT)
1525 ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1527 ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0);
1530 start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO;
1532 start_ccb->ccb_h.ccb_bp = bp;
1533 softc->outstanding_cmds++;
1534 xpt_action(start_ccb);
1536 /* May have more work to do, so ensure we stay scheduled */
1537 adaschedule(periph);
1540 case ADA_STATE_RAHEAD:
1541 case ADA_STATE_WCACHE:
1543 if (softc->flags & ADA_FLAG_PACK_INVALID) {
1544 softc->state = ADA_STATE_NORMAL;
1545 xpt_release_ccb(start_ccb);
1546 cam_release_devq(periph->path,
1547 RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_DEV + 1, FALSE);
1548 adaschedule(periph);
1549 cam_periph_release_locked(periph);
1553 cam_fill_ataio(ataio,
1560 ada_default_timeout*1000);
1562 if (softc->state == ADA_STATE_RAHEAD) {
1563 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_RA ?
1564 ATA_SF_ENAB_RCACHE : ATA_SF_DIS_RCACHE, 0, 0);
1565 start_ccb->ccb_h.ccb_state = ADA_CCB_RAHEAD;
1567 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_WC ?
1568 ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0);
1569 start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE;
1571 xpt_action(start_ccb);
1578 adadone(struct cam_periph *periph, union ccb *done_ccb)
1580 struct ada_softc *softc;
1581 struct ccb_ataio *ataio;
1582 struct ccb_getdev *cgd;
1584 softc = (struct ada_softc *)periph->softc;
1585 ataio = &done_ccb->ataio;
1587 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("adadone\n"));
1589 switch (ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) {
1590 case ADA_CCB_BUFFER_IO:
1595 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1596 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1599 error = adaerror(done_ccb, 0, 0);
1600 if (error == ERESTART) {
1601 /* A retry was scheduled, so just return. */
1605 if (error == ENXIO &&
1606 (softc->flags & ADA_FLAG_PACK_INVALID) == 0) {
1608 * Catastrophic error. Mark our pack as
1612 * XXX See if this is really a media
1615 xpt_print(periph->path,
1616 "Invalidating pack\n");
1617 softc->flags |= ADA_FLAG_PACK_INVALID;
1619 bp->bio_error = error;
1620 bp->bio_resid = bp->bio_bcount;
1621 bp->bio_flags |= BIO_ERROR;
1623 bp->bio_resid = ataio->resid;
1625 if (bp->bio_resid != 0)
1626 bp->bio_flags |= BIO_ERROR;
1628 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1629 cam_release_devq(done_ccb->ccb_h.path,
1633 /*getcount_only*/0);
1635 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1636 panic("REQ_CMP with QFRZN");
1637 bp->bio_resid = ataio->resid;
1638 if (ataio->resid > 0)
1639 bp->bio_flags |= BIO_ERROR;
1641 softc->outstanding_cmds--;
1642 if (softc->outstanding_cmds == 0)
1643 softc->flags |= ADA_FLAG_WENT_IDLE;
1644 if ((ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) ==
1646 struct trim_request *req =
1647 (struct trim_request *)ataio->data_ptr;
1650 for (i = 1; i < TRIM_MAX_BIOS && req->bps[i]; i++) {
1651 struct bio *bp1 = req->bps[i];
1653 bp1->bio_resid = bp->bio_resid;
1654 bp1->bio_error = bp->bio_error;
1655 if (bp->bio_flags & BIO_ERROR)
1656 bp1->bio_flags |= BIO_ERROR;
1659 softc->trim_running = 0;
1661 adaschedule(periph);
1666 case ADA_CCB_RAHEAD:
1668 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1669 if (adaerror(done_ccb, 0, 0) == ERESTART) {
1671 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1672 cam_release_devq(done_ccb->ccb_h.path,
1676 /*getcount_only*/0);
1681 * Since our peripheral may be invalidated by an error
1682 * above or an external event, we must release our CCB
1683 * before releasing the reference on the peripheral.
1684 * The peripheral will only go away once the last reference
1685 * is removed, and we need it around for the CCB release
1688 cgd = (struct ccb_getdev *)done_ccb;
1689 xpt_setup_ccb(&cgd->ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1690 cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1691 xpt_action((union ccb *)cgd);
1693 cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) {
1694 softc->state = ADA_STATE_WCACHE;
1695 xpt_release_ccb(done_ccb);
1696 xpt_schedule(periph, CAM_PRIORITY_DEV);
1699 softc->state = ADA_STATE_NORMAL;
1700 xpt_release_ccb(done_ccb);
1701 cam_release_devq(periph->path,
1702 RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_DEV + 1, FALSE);
1703 adaschedule(periph);
1704 cam_periph_release_locked(periph);
1707 case ADA_CCB_WCACHE:
1709 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1710 if (adaerror(done_ccb, 0, 0) == ERESTART) {
1712 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1713 cam_release_devq(done_ccb->ccb_h.path,
1717 /*getcount_only*/0);
1721 softc->state = ADA_STATE_NORMAL;
1723 * Since our peripheral may be invalidated by an error
1724 * above or an external event, we must release our CCB
1725 * before releasing the reference on the peripheral.
1726 * The peripheral will only go away once the last reference
1727 * is removed, and we need it around for the CCB release
1730 xpt_release_ccb(done_ccb);
1731 cam_release_devq(periph->path,
1732 RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_DEV + 1, FALSE);
1733 adaschedule(periph);
1734 cam_periph_release_locked(periph);
1737 case ADA_CCB_WAITING:
1739 /* Caller will release the CCB */
1740 wakeup(&done_ccb->ccb_h.cbfcnp);
1744 /* No-op. We're polling */
1749 xpt_release_ccb(done_ccb);
1753 adaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1756 return(cam_periph_error(ccb, cam_flags, sense_flags, NULL));
1760 adagetparams(struct cam_periph *periph, struct ccb_getdev *cgd)
1762 struct ada_softc *softc = (struct ada_softc *)periph->softc;
1763 struct disk_params *dp = &softc->params;
1764 u_int64_t lbasize48;
1767 dp->secsize = ata_logical_sector_size(&cgd->ident_data);
1768 if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) &&
1769 cgd->ident_data.current_heads && cgd->ident_data.current_sectors) {
1770 dp->heads = cgd->ident_data.current_heads;
1771 dp->secs_per_track = cgd->ident_data.current_sectors;
1772 dp->cylinders = cgd->ident_data.cylinders;
1773 dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 |
1774 ((u_int32_t)cgd->ident_data.current_size_2 << 16);
1776 dp->heads = cgd->ident_data.heads;
1777 dp->secs_per_track = cgd->ident_data.sectors;
1778 dp->cylinders = cgd->ident_data.cylinders;
1779 dp->sectors = cgd->ident_data.cylinders * dp->heads * dp->secs_per_track;
1781 lbasize = (u_int32_t)cgd->ident_data.lba_size_1 |
1782 ((u_int32_t)cgd->ident_data.lba_size_2 << 16);
1784 /* use the 28bit LBA size if valid or bigger than the CHS mapping */
1785 if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize)
1786 dp->sectors = lbasize;
1788 /* use the 48bit LBA size if valid */
1789 lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) |
1790 ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) |
1791 ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) |
1792 ((u_int64_t)cgd->ident_data.lba_size48_4 << 48);
1793 if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) &&
1794 lbasize48 > ATA_MAX_28BIT_LBA)
1795 dp->sectors = lbasize48;
1799 adasendorderedtag(void *arg)
1801 struct ada_softc *softc = arg;
1803 if (ada_send_ordered) {
1804 if ((softc->ordered_tag_count == 0)
1805 && ((softc->flags & ADA_FLAG_WENT_IDLE) == 0)) {
1806 softc->flags |= ADA_FLAG_NEED_OTAG;
1808 if (softc->outstanding_cmds > 0)
1809 softc->flags &= ~ADA_FLAG_WENT_IDLE;
1811 softc->ordered_tag_count = 0;
1813 /* Queue us up again */
1814 callout_reset(&softc->sendordered_c,
1815 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL,
1816 adasendorderedtag, softc);
1820 * Step through all ADA peripheral drivers, and if the device is still open,
1821 * sync the disk cache to physical media.
1826 struct cam_periph *periph;
1827 struct ada_softc *softc;
1830 TAILQ_FOREACH(periph, &adadriver.units, unit_links) {
1833 /* If we paniced with lock held - not recurse here. */
1834 if (cam_periph_owned(periph))
1836 cam_periph_lock(periph);
1837 softc = (struct ada_softc *)periph->softc;
1839 * We only sync the cache if the drive is still open, and
1840 * if the drive is capable of it..
1842 if (((softc->flags & ADA_FLAG_OPEN) == 0) ||
1843 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) {
1844 cam_periph_unlock(periph);
1848 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1850 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
1851 cam_fill_ataio(&ccb.ataio,
1858 ada_default_timeout*1000);
1860 if (softc->flags & ADA_FLAG_CAN_48BIT)
1861 ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1863 ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0);
1864 xpt_polled_action(&ccb);
1866 error = cam_periph_error(&ccb,
1867 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1868 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1869 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
1870 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1872 xpt_print(periph->path, "Synchronize cache failed\n");
1873 cam_periph_unlock(periph);
1878 adaspindown(uint8_t cmd, int flags)
1880 struct cam_periph *periph;
1881 struct ada_softc *softc;
1884 TAILQ_FOREACH(periph, &adadriver.units, unit_links) {
1887 /* If we paniced with lock held - not recurse here. */
1888 if (cam_periph_owned(periph))
1890 cam_periph_lock(periph);
1891 softc = (struct ada_softc *)periph->softc;
1893 * We only spin-down the drive if it is capable of it..
1895 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
1896 cam_periph_unlock(periph);
1901 xpt_print(periph->path, "spin-down\n");
1903 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1905 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
1906 cam_fill_ataio(&ccb.ataio,
1909 CAM_DIR_NONE | flags,
1913 ada_default_timeout*1000);
1915 ata_28bit_cmd(&ccb.ataio, cmd, 0, 0, 0);
1916 xpt_polled_action(&ccb);
1918 error = cam_periph_error(&ccb,
1919 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1920 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1921 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
1922 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
1924 xpt_print(periph->path, "Spin-down disk failed\n");
1925 cam_periph_unlock(periph);
1930 adashutdown(void *arg, int howto)
1934 if (ada_spindown_shutdown != 0 &&
1935 (howto & (RB_HALT | RB_POWEROFF)) != 0)
1936 adaspindown(ATA_STANDBY_IMMEDIATE, 0);
1940 adasuspend(void *arg)
1944 if (ada_spindown_suspend != 0)
1945 adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE);
1949 adaresume(void *arg)
1951 struct cam_periph *periph;
1952 struct ada_softc *softc;
1954 if (ada_spindown_suspend == 0)
1957 TAILQ_FOREACH(periph, &adadriver.units, unit_links) {
1958 cam_periph_lock(periph);
1959 softc = (struct ada_softc *)periph->softc;
1961 * We only spin-down the drive if it is capable of it..
1963 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
1964 cam_periph_unlock(periph);
1969 xpt_print(periph->path, "resume\n");
1972 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on
1975 cam_release_devq(periph->path,
1979 /*getcount_only*/0);
1981 cam_periph_unlock(periph);
1985 #endif /* _KERNEL */