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*", "*" },
274 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
275 /*vendor*/"*", /*product*/"*", /*revision*/"*"
281 static disk_strategy_t adastrategy;
282 static dumper_t adadump;
283 static periph_init_t adainit;
284 static void adaasync(void *callback_arg, u_int32_t code,
285 struct cam_path *path, void *arg);
286 static void adasysctlinit(void *context, int pending);
287 static periph_ctor_t adaregister;
288 static periph_dtor_t adacleanup;
289 static periph_start_t adastart;
290 static periph_oninv_t adaoninvalidate;
291 static void adadone(struct cam_periph *periph,
292 union ccb *done_ccb);
293 static int adaerror(union ccb *ccb, u_int32_t cam_flags,
294 u_int32_t sense_flags);
295 static void adagetparams(struct cam_periph *periph,
296 struct ccb_getdev *cgd);
297 static timeout_t adasendorderedtag;
298 static void adashutdown(void *arg, int howto);
299 static void adasuspend(void *arg);
300 static void adaresume(void *arg);
302 #ifndef ADA_DEFAULT_LEGACY_ALIASES
304 #define ADA_DEFAULT_LEGACY_ALIASES 1
306 #define ADA_DEFAULT_LEGACY_ALIASES 0
310 #ifndef ADA_DEFAULT_TIMEOUT
311 #define ADA_DEFAULT_TIMEOUT 30 /* Timeout in seconds */
314 #ifndef ADA_DEFAULT_RETRY
315 #define ADA_DEFAULT_RETRY 4
318 #ifndef ADA_DEFAULT_SEND_ORDERED
319 #define ADA_DEFAULT_SEND_ORDERED 1
322 #ifndef ADA_DEFAULT_SPINDOWN_SHUTDOWN
323 #define ADA_DEFAULT_SPINDOWN_SHUTDOWN 1
326 #ifndef ADA_DEFAULT_SPINDOWN_SUSPEND
327 #define ADA_DEFAULT_SPINDOWN_SUSPEND 1
330 #ifndef ADA_DEFAULT_READ_AHEAD
331 #define ADA_DEFAULT_READ_AHEAD 1
334 #ifndef ADA_DEFAULT_WRITE_CACHE
335 #define ADA_DEFAULT_WRITE_CACHE 1
338 #define ADA_RA (softc->read_ahead >= 0 ? \
339 softc->read_ahead : ada_read_ahead)
340 #define ADA_WC (softc->write_cache >= 0 ? \
341 softc->write_cache : ada_write_cache)
344 * Most platforms map firmware geometry to actual, but some don't. If
345 * not overridden, default to nothing.
347 #ifndef ata_disk_firmware_geom_adjust
348 #define ata_disk_firmware_geom_adjust(disk)
351 static int ada_legacy_aliases = ADA_DEFAULT_LEGACY_ALIASES;
352 static int ada_retry_count = ADA_DEFAULT_RETRY;
353 static int ada_default_timeout = ADA_DEFAULT_TIMEOUT;
354 static int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED;
355 static int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN;
356 static int ada_spindown_suspend = ADA_DEFAULT_SPINDOWN_SUSPEND;
357 static int ada_read_ahead = ADA_DEFAULT_READ_AHEAD;
358 static int ada_write_cache = ADA_DEFAULT_WRITE_CACHE;
360 static SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD, 0,
361 "CAM Direct Access Disk driver");
362 SYSCTL_INT(_kern_cam_ada, OID_AUTO, legacy_aliases, CTLFLAG_RW,
363 &ada_legacy_aliases, 0, "Create legacy-like device aliases");
364 TUNABLE_INT("kern.cam.ada.legacy_aliases", &ada_legacy_aliases);
365 SYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RW,
366 &ada_retry_count, 0, "Normal I/O retry count");
367 TUNABLE_INT("kern.cam.ada.retry_count", &ada_retry_count);
368 SYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RW,
369 &ada_default_timeout, 0, "Normal I/O timeout (in seconds)");
370 TUNABLE_INT("kern.cam.ada.default_timeout", &ada_default_timeout);
371 SYSCTL_INT(_kern_cam_ada, OID_AUTO, ada_send_ordered, CTLFLAG_RW,
372 &ada_send_ordered, 0, "Send Ordered Tags");
373 TUNABLE_INT("kern.cam.ada.ada_send_ordered", &ada_send_ordered);
374 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RW,
375 &ada_spindown_shutdown, 0, "Spin down upon shutdown");
376 TUNABLE_INT("kern.cam.ada.spindown_shutdown", &ada_spindown_shutdown);
377 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_suspend, CTLFLAG_RW,
378 &ada_spindown_suspend, 0, "Spin down upon suspend");
379 TUNABLE_INT("kern.cam.ada.spindown_suspend", &ada_spindown_suspend);
380 SYSCTL_INT(_kern_cam_ada, OID_AUTO, read_ahead, CTLFLAG_RW,
381 &ada_read_ahead, 0, "Enable disk read-ahead");
382 TUNABLE_INT("kern.cam.ada.read_ahead", &ada_read_ahead);
383 SYSCTL_INT(_kern_cam_ada, OID_AUTO, write_cache, CTLFLAG_RW,
384 &ada_write_cache, 0, "Enable disk write cache");
385 TUNABLE_INT("kern.cam.ada.write_cache", &ada_write_cache);
388 * ADA_ORDEREDTAG_INTERVAL determines how often, relative
389 * to the default timeout, we check to see whether an ordered
390 * tagged transaction is appropriate to prevent simple tag
391 * starvation. Since we'd like to ensure that there is at least
392 * 1/2 of the timeout length left for a starved transaction to
393 * complete after we've sent an ordered tag, we must poll at least
394 * four times in every timeout period. This takes care of the worst
395 * case where a starved transaction starts during an interval that
396 * meets the requirement "don't send an ordered tag" test so it takes
397 * us two intervals to determine that a tag must be sent.
399 #ifndef ADA_ORDEREDTAG_INTERVAL
400 #define ADA_ORDEREDTAG_INTERVAL 4
403 static struct periph_driver adadriver =
406 TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0
409 PERIPHDRIVER_DECLARE(ada, adadriver);
411 static MALLOC_DEFINE(M_ATADA, "ata_da", "ata_da buffers");
414 adaopen(struct disk *dp)
416 struct cam_periph *periph;
417 struct ada_softc *softc;
420 periph = (struct cam_periph *)dp->d_drv1;
421 if (periph == NULL) {
425 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
429 cam_periph_lock(periph);
430 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
431 cam_periph_unlock(periph);
432 cam_periph_release(periph);
436 softc = (struct ada_softc *)periph->softc;
437 softc->flags |= ADA_FLAG_OPEN;
439 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
440 ("adaopen: disk=%s%d (unit %d)\n", dp->d_name, dp->d_unit,
441 periph->unit_number));
443 if ((softc->flags & ADA_FLAG_PACK_INVALID) != 0) {
444 /* Invalidate our pack information. */
445 softc->flags &= ~ADA_FLAG_PACK_INVALID;
448 cam_periph_unhold(periph);
449 cam_periph_unlock(periph);
454 adaclose(struct disk *dp)
456 struct cam_periph *periph;
457 struct ada_softc *softc;
460 periph = (struct cam_periph *)dp->d_drv1;
464 cam_periph_lock(periph);
465 if (cam_periph_hold(periph, PRIBIO) != 0) {
466 cam_periph_unlock(periph);
467 cam_periph_release(periph);
471 softc = (struct ada_softc *)periph->softc;
472 /* We only sync the cache if the drive is capable of it. */
473 if ((softc->flags & ADA_FLAG_CAN_FLUSHCACHE) != 0 &&
474 (softc->flags & ADA_FLAG_PACK_INVALID) == 0) {
476 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
477 cam_fill_ataio(&ccb->ataio,
484 ada_default_timeout*1000);
486 if (softc->flags & ADA_FLAG_CAN_48BIT)
487 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
489 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
490 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
491 /*sense_flags*/0, softc->disk->d_devstat);
493 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
494 xpt_print(periph->path, "Synchronize cache failed\n");
495 xpt_release_ccb(ccb);
498 softc->flags &= ~ADA_FLAG_OPEN;
499 cam_periph_unhold(periph);
500 cam_periph_unlock(periph);
501 cam_periph_release(periph);
506 adaschedule(struct cam_periph *periph)
508 struct ada_softc *softc = (struct ada_softc *)periph->softc;
511 /* Check if cam_periph_getccb() was called. */
512 prio = periph->immediate_priority;
514 /* Check if we have more work to do. */
515 if (bioq_first(&softc->bio_queue) ||
516 (!softc->trim_running && bioq_first(&softc->trim_queue))) {
517 prio = CAM_PRIORITY_NORMAL;
520 /* Schedule CCB if any of above is true. */
521 if (prio != CAM_PRIORITY_NONE)
522 xpt_schedule(periph, prio);
526 * Actually translate the requested transfer into one the physical driver
527 * can understand. The transfer is described by a buf and will include
528 * only one physical transfer.
531 adastrategy(struct bio *bp)
533 struct cam_periph *periph;
534 struct ada_softc *softc;
536 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
537 if (periph == NULL) {
538 biofinish(bp, NULL, ENXIO);
541 softc = (struct ada_softc *)periph->softc;
543 cam_periph_lock(periph);
546 * If the device has been made invalid, error out
548 if ((softc->flags & ADA_FLAG_PACK_INVALID)) {
549 cam_periph_unlock(periph);
550 biofinish(bp, NULL, ENXIO);
555 * Place it in the queue of disk activities for this disk
557 if (bp->bio_cmd == BIO_DELETE &&
558 (softc->flags & ADA_FLAG_CAN_TRIM))
559 bioq_disksort(&softc->trim_queue, bp);
561 bioq_disksort(&softc->bio_queue, bp);
564 * Schedule ourselves for performing the work.
567 cam_periph_unlock(periph);
573 adadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
575 struct cam_periph *periph;
576 struct ada_softc *softc;
587 softc = (struct ada_softc *)periph->softc;
588 cam_periph_lock(periph);
589 secsize = softc->params.secsize;
590 lba = offset / secsize;
591 count = length / secsize;
593 if ((softc->flags & ADA_FLAG_PACK_INVALID) != 0) {
594 cam_periph_unlock(periph);
599 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
600 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
601 cam_fill_ataio(&ccb.ataio,
606 (u_int8_t *) virtual,
608 ada_default_timeout*1000);
609 if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
610 (lba + count >= ATA_MAX_28BIT_LBA ||
612 ata_48bit_cmd(&ccb.ataio, ATA_WRITE_DMA48,
615 ata_28bit_cmd(&ccb.ataio, ATA_WRITE_DMA,
618 xpt_polled_action(&ccb);
620 if ((ccb.ataio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
621 printf("Aborting dump due to I/O error.\n");
622 cam_periph_unlock(periph);
625 cam_periph_unlock(periph);
629 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) {
630 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
632 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
633 cam_fill_ataio(&ccb.ataio,
640 ada_default_timeout*1000);
642 if (softc->flags & ADA_FLAG_CAN_48BIT)
643 ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0);
645 ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0);
646 xpt_polled_action(&ccb);
648 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
649 xpt_print(periph->path, "Synchronize cache failed\n");
651 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
652 cam_release_devq(ccb.ccb_h.path,
658 cam_periph_unlock(periph);
668 * Install a global async callback. This callback will
669 * receive async callbacks like "new device found".
671 status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL);
673 if (status != CAM_REQ_CMP) {
674 printf("ada: Failed to attach master async callback "
675 "due to status 0x%x!\n", status);
676 } else if (ada_send_ordered) {
678 /* Register our event handlers */
679 if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend,
680 NULL, EVENTHANDLER_PRI_LAST)) == NULL)
681 printf("adainit: power event registration failed!\n");
682 if ((EVENTHANDLER_REGISTER(power_resume, adaresume,
683 NULL, EVENTHANDLER_PRI_LAST)) == NULL)
684 printf("adainit: power event registration failed!\n");
685 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown,
686 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
687 printf("adainit: shutdown event registration failed!\n");
692 adaoninvalidate(struct cam_periph *periph)
694 struct ada_softc *softc;
696 softc = (struct ada_softc *)periph->softc;
699 * De-register any async callbacks.
701 xpt_register_async(0, adaasync, periph, periph->path);
703 softc->flags |= ADA_FLAG_PACK_INVALID;
706 * Return all queued I/O with ENXIO.
707 * XXX Handle any transactions queued to the card
708 * with XPT_ABORT_CCB.
710 bioq_flush(&softc->bio_queue, NULL, ENXIO);
711 bioq_flush(&softc->trim_queue, NULL, ENXIO);
713 disk_gone(softc->disk);
714 xpt_print(periph->path, "lost device\n");
718 adacleanup(struct cam_periph *periph)
720 struct ada_softc *softc;
722 softc = (struct ada_softc *)periph->softc;
724 xpt_print(periph->path, "removing device entry\n");
725 cam_periph_unlock(periph);
728 * If we can't free the sysctl tree, oh well...
730 if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0
731 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
732 xpt_print(periph->path, "can't remove sysctl context\n");
735 disk_destroy(softc->disk);
736 callout_drain(&softc->sendordered_c);
737 free(softc, M_DEVBUF);
738 cam_periph_lock(periph);
742 adaasync(void *callback_arg, u_int32_t code,
743 struct cam_path *path, void *arg)
745 struct cam_periph *periph;
746 struct ada_softc *softc;
748 periph = (struct cam_periph *)callback_arg;
750 case AC_FOUND_DEVICE:
752 struct ccb_getdev *cgd;
755 cgd = (struct ccb_getdev *)arg;
759 if (cgd->protocol != PROTO_ATA)
763 * Allocate a peripheral instance for
764 * this device and start the probe
767 status = cam_periph_alloc(adaregister, adaoninvalidate,
768 adacleanup, adastart,
769 "ada", CAM_PERIPH_BIO,
770 cgd->ccb_h.path, adaasync,
771 AC_FOUND_DEVICE, cgd);
773 if (status != CAM_REQ_CMP
774 && status != CAM_REQ_INPROG)
775 printf("adaasync: Unable to attach to new device "
776 "due to status 0x%x\n", status);
779 case AC_ADVINFO_CHANGED:
783 buftype = (uintptr_t)arg;
784 if (buftype == CDAI_TYPE_PHYS_PATH) {
785 struct ada_softc *softc;
787 softc = periph->softc;
788 disk_attr_changed(softc->disk, "GEOM::physpath",
796 struct ccb_getdev cgd;
798 softc = (struct ada_softc *)periph->softc;
799 cam_periph_async(periph, code, path, arg);
800 if (softc->state != ADA_STATE_NORMAL)
802 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
803 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
804 xpt_action((union ccb *)&cgd);
806 cgd.ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD)
807 softc->state = ADA_STATE_RAHEAD;
808 else if (ADA_WC >= 0 &&
809 cgd.ident_data.support.command1 & ATA_SUPPORT_WRITECACHE)
810 softc->state = ADA_STATE_WCACHE;
813 cam_periph_acquire(periph);
814 cam_freeze_devq_arg(periph->path,
815 RELSIM_RELEASE_RUNLEVEL, CAM_RL_DEV + 1);
816 xpt_schedule(periph, CAM_PRIORITY_DEV);
819 cam_periph_async(periph, code, path, arg);
825 adasysctlinit(void *context, int pending)
827 struct cam_periph *periph;
828 struct ada_softc *softc;
829 char tmpstr[80], tmpstr2[80];
831 periph = (struct cam_periph *)context;
833 /* periph was held for us when this task was enqueued */
834 if (periph->flags & CAM_PERIPH_INVALID) {
835 cam_periph_release(periph);
839 softc = (struct ada_softc *)periph->softc;
840 snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d", periph->unit_number);
841 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
843 sysctl_ctx_init(&softc->sysctl_ctx);
844 softc->flags |= ADA_FLAG_SCTX_INIT;
845 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
846 SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2,
847 CTLFLAG_RD, 0, tmpstr);
848 if (softc->sysctl_tree == NULL) {
849 printf("adasysctlinit: unable to allocate sysctl tree\n");
850 cam_periph_release(periph);
854 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
855 OID_AUTO, "read_ahead", CTLFLAG_RW | CTLFLAG_MPSAFE,
856 &softc->read_ahead, 0, "Enable disk read ahead.");
857 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
858 OID_AUTO, "write_cache", CTLFLAG_RW | CTLFLAG_MPSAFE,
859 &softc->write_cache, 0, "Enable disk write cache.");
860 #ifdef ADA_TEST_FAILURE
862 * Add a 'door bell' sysctl which allows one to set it from userland
863 * and cause something bad to happen. For the moment, we only allow
864 * whacking the next read or write.
866 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
867 OID_AUTO, "force_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
868 &softc->force_read_error, 0,
869 "Force a read error for the next N reads.");
870 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
871 OID_AUTO, "force_write_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
872 &softc->force_write_error, 0,
873 "Force a write error for the next N writes.");
874 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
875 OID_AUTO, "periodic_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
876 &softc->periodic_read_error, 0,
877 "Force a read error every N reads (don't set too low).");
879 cam_periph_release(periph);
883 adagetattr(struct bio *bp)
886 struct cam_periph *periph;
888 if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
890 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
891 if (periph->path == NULL)
894 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
897 bp->bio_completed = bp->bio_length;
902 adaregister(struct cam_periph *periph, void *arg)
904 struct ada_softc *softc;
905 struct ccb_pathinq cpi;
906 struct ccb_getdev *cgd;
907 char announce_buf[80], buf1[32];
908 struct disk_params *dp;
911 int legacy_id, quirks;
913 cgd = (struct ccb_getdev *)arg;
914 if (periph == NULL) {
915 printf("adaregister: periph was NULL!!\n");
916 return(CAM_REQ_CMP_ERR);
920 printf("adaregister: no getdev CCB, can't register device\n");
921 return(CAM_REQ_CMP_ERR);
924 softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF,
928 printf("adaregister: Unable to probe new device. "
929 "Unable to allocate softc\n");
930 return(CAM_REQ_CMP_ERR);
933 bioq_init(&softc->bio_queue);
934 bioq_init(&softc->trim_queue);
936 if (cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA &&
937 (cgd->inq_flags & SID_DMA))
938 softc->flags |= ADA_FLAG_CAN_DMA;
939 if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48)
940 softc->flags |= ADA_FLAG_CAN_48BIT;
941 if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE)
942 softc->flags |= ADA_FLAG_CAN_FLUSHCACHE;
943 if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT)
944 softc->flags |= ADA_FLAG_CAN_POWERMGT;
945 if (cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ &&
946 (cgd->inq_flags & SID_DMA) && (cgd->inq_flags & SID_CmdQue))
947 softc->flags |= ADA_FLAG_CAN_NCQ;
948 if (cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) {
949 softc->flags |= ADA_FLAG_CAN_TRIM;
950 softc->trim_max_ranges = TRIM_MAX_RANGES;
951 if (cgd->ident_data.max_dsm_blocks != 0) {
952 softc->trim_max_ranges =
953 min(cgd->ident_data.max_dsm_blocks * 64,
954 softc->trim_max_ranges);
957 if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA)
958 softc->flags |= ADA_FLAG_CAN_CFA;
960 periph->softc = softc;
963 * See if this device has any quirks.
965 match = cam_quirkmatch((caddr_t)&cgd->ident_data,
966 (caddr_t)ada_quirk_table,
967 sizeof(ada_quirk_table)/sizeof(*ada_quirk_table),
968 sizeof(*ada_quirk_table), ata_identify_match);
970 softc->quirks = ((struct ada_quirk_entry *)match)->quirks;
972 softc->quirks = ADA_Q_NONE;
974 bzero(&cpi, sizeof(cpi));
975 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE);
976 cpi.ccb_h.func_code = XPT_PATH_INQ;
977 xpt_action((union ccb *)&cpi);
979 TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph);
982 * Register this media as a disk
984 (void)cam_periph_hold(periph, PRIBIO);
985 mtx_unlock(periph->sim->mtx);
986 snprintf(announce_buf, sizeof(announce_buf),
987 "kern.cam.ada.%d.quirks", periph->unit_number);
988 quirks = softc->quirks;
989 TUNABLE_INT_FETCH(announce_buf, &quirks);
990 softc->quirks = quirks;
991 softc->read_ahead = -1;
992 snprintf(announce_buf, sizeof(announce_buf),
993 "kern.cam.ada.%d.read_ahead", periph->unit_number);
994 TUNABLE_INT_FETCH(announce_buf, &softc->read_ahead);
995 softc->write_cache = -1;
996 snprintf(announce_buf, sizeof(announce_buf),
997 "kern.cam.ada.%d.write_cache", periph->unit_number);
998 TUNABLE_INT_FETCH(announce_buf, &softc->write_cache);
999 adagetparams(periph, cgd);
1000 softc->disk = disk_alloc();
1001 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
1002 periph->unit_number, softc->params.secsize,
1003 DEVSTAT_ALL_SUPPORTED,
1004 DEVSTAT_TYPE_DIRECT |
1005 XPORT_DEVSTAT_TYPE(cpi.transport),
1006 DEVSTAT_PRIORITY_DISK);
1007 softc->disk->d_open = adaopen;
1008 softc->disk->d_close = adaclose;
1009 softc->disk->d_strategy = adastrategy;
1010 softc->disk->d_getattr = adagetattr;
1011 softc->disk->d_dump = adadump;
1012 softc->disk->d_name = "ada";
1013 softc->disk->d_drv1 = periph;
1014 maxio = cpi.maxio; /* Honor max I/O size of SIM */
1016 maxio = DFLTPHYS; /* traditional default */
1017 else if (maxio > MAXPHYS)
1018 maxio = MAXPHYS; /* for safety */
1019 if (softc->flags & ADA_FLAG_CAN_48BIT)
1020 maxio = min(maxio, 65536 * softc->params.secsize);
1021 else /* 28bit ATA command limit */
1022 maxio = min(maxio, 256 * softc->params.secsize);
1023 softc->disk->d_maxsize = maxio;
1024 softc->disk->d_unit = periph->unit_number;
1025 softc->disk->d_flags = 0;
1026 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE)
1027 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
1028 if ((softc->flags & ADA_FLAG_CAN_TRIM) ||
1029 ((softc->flags & ADA_FLAG_CAN_CFA) &&
1030 !(softc->flags & ADA_FLAG_CAN_48BIT)))
1031 softc->disk->d_flags |= DISKFLAG_CANDELETE;
1032 strlcpy(softc->disk->d_descr, cgd->ident_data.model,
1033 MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model)));
1034 softc->disk->d_hba_vendor = cpi.hba_vendor;
1035 softc->disk->d_hba_device = cpi.hba_device;
1036 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
1037 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
1039 softc->disk->d_sectorsize = softc->params.secsize;
1040 softc->disk->d_mediasize = (off_t)softc->params.sectors *
1041 softc->params.secsize;
1042 if (ata_physical_sector_size(&cgd->ident_data) !=
1043 softc->params.secsize) {
1044 softc->disk->d_stripesize =
1045 ata_physical_sector_size(&cgd->ident_data);
1046 softc->disk->d_stripeoffset = (softc->disk->d_stripesize -
1047 ata_logical_sector_offset(&cgd->ident_data)) %
1048 softc->disk->d_stripesize;
1049 } else if (softc->quirks & ADA_Q_4K) {
1050 softc->disk->d_stripesize = 4096;
1051 softc->disk->d_stripeoffset = 0;
1053 softc->disk->d_fwsectors = softc->params.secs_per_track;
1054 softc->disk->d_fwheads = softc->params.heads;
1055 ata_disk_firmware_geom_adjust(softc->disk);
1057 if (ada_legacy_aliases) {
1058 #ifdef ATA_STATIC_ID
1059 legacy_id = xpt_path_legacy_ata_id(periph->path);
1061 legacy_id = softc->disk->d_unit;
1063 if (legacy_id >= 0) {
1064 snprintf(announce_buf, sizeof(announce_buf),
1065 "kern.devalias.%s%d",
1066 softc->disk->d_name, softc->disk->d_unit);
1067 snprintf(buf1, sizeof(buf1),
1069 setenv(announce_buf, buf1);
1073 disk_create(softc->disk, DISK_VERSION);
1074 mtx_lock(periph->sim->mtx);
1075 cam_periph_unhold(periph);
1077 dp = &softc->params;
1078 snprintf(announce_buf, sizeof(announce_buf),
1079 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1080 (uintmax_t)(((uintmax_t)dp->secsize *
1081 dp->sectors) / (1024*1024)),
1082 (uintmax_t)dp->sectors,
1083 dp->secsize, dp->heads,
1084 dp->secs_per_track, dp->cylinders);
1085 xpt_announce_periph(periph, announce_buf);
1087 printf("%s%d: Previously was known as ad%d\n",
1088 periph->periph_name, periph->unit_number, legacy_id);
1091 * Create our sysctl variables, now that we know
1092 * we have successfully attached.
1094 cam_periph_acquire(periph);
1095 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task);
1098 * Add async callbacks for bus reset and
1099 * bus device reset calls. I don't bother
1100 * checking if this fails as, in most cases,
1101 * the system will function just fine without
1102 * them and the only alternative would be to
1103 * not attach the device on failure.
1105 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
1106 AC_ADVINFO_CHANGED, adaasync, periph, periph->path);
1109 * Schedule a periodic event to occasionally send an
1110 * ordered tag to a device.
1112 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
1113 callout_reset(&softc->sendordered_c,
1114 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL,
1115 adasendorderedtag, softc);
1118 cgd->ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD) {
1119 softc->state = ADA_STATE_RAHEAD;
1120 cam_periph_acquire(periph);
1121 cam_freeze_devq_arg(periph->path,
1122 RELSIM_RELEASE_RUNLEVEL, CAM_RL_DEV + 1);
1123 xpt_schedule(periph, CAM_PRIORITY_DEV);
1124 } else if (ADA_WC >= 0 &&
1125 cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) {
1126 softc->state = ADA_STATE_WCACHE;
1127 cam_periph_acquire(periph);
1128 cam_freeze_devq_arg(periph->path,
1129 RELSIM_RELEASE_RUNLEVEL, CAM_RL_DEV + 1);
1130 xpt_schedule(periph, CAM_PRIORITY_DEV);
1132 softc->state = ADA_STATE_NORMAL;
1134 return(CAM_REQ_CMP);
1138 adastart(struct cam_periph *periph, union ccb *start_ccb)
1140 struct ada_softc *softc = (struct ada_softc *)periph->softc;
1141 struct ccb_ataio *ataio = &start_ccb->ataio;
1143 switch (softc->state) {
1144 case ADA_STATE_NORMAL:
1149 /* Execute immediate CCB if waiting. */
1150 if (periph->immediate_priority <= periph->pinfo.priority) {
1151 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1152 ("queuing for immediate ccb\n"));
1153 start_ccb->ccb_h.ccb_state = ADA_CCB_WAITING;
1154 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1156 periph->immediate_priority = CAM_PRIORITY_NONE;
1157 wakeup(&periph->ccb_list);
1158 /* Have more work to do, so ensure we stay scheduled */
1159 adaschedule(periph);
1162 /* Run TRIM if not running yet. */
1163 if (!softc->trim_running &&
1164 (bp = bioq_first(&softc->trim_queue)) != 0) {
1165 struct trim_request *req = &softc->trim_req;
1167 uint64_t lastlba = (uint64_t)-1;
1168 int bps = 0, c, lastcount = 0, off, ranges = 0;
1170 softc->trim_running = 1;
1171 bzero(req, sizeof(*req));
1174 uint64_t lba = bp1->bio_pblkno;
1175 int count = bp1->bio_bcount /
1176 softc->params.secsize;
1178 bioq_remove(&softc->trim_queue, bp1);
1180 /* Try to extend the previous range. */
1181 if (lba == lastlba) {
1182 c = min(count, 0xffff - lastcount);
1184 off = (ranges - 1) * 8;
1185 req->data[off + 6] = lastcount & 0xff;
1186 req->data[off + 7] =
1187 (lastcount >> 8) & 0xff;
1193 c = min(count, 0xffff);
1195 req->data[off + 0] = lba & 0xff;
1196 req->data[off + 1] = (lba >> 8) & 0xff;
1197 req->data[off + 2] = (lba >> 16) & 0xff;
1198 req->data[off + 3] = (lba >> 24) & 0xff;
1199 req->data[off + 4] = (lba >> 32) & 0xff;
1200 req->data[off + 5] = (lba >> 40) & 0xff;
1201 req->data[off + 6] = c & 0xff;
1202 req->data[off + 7] = (c >> 8) & 0xff;
1209 req->bps[bps++] = bp1;
1210 bp1 = bioq_first(&softc->trim_queue);
1211 if (bps >= TRIM_MAX_BIOS ||
1213 bp1->bio_bcount / softc->params.secsize >
1214 (softc->trim_max_ranges - ranges) * 0xffff)
1217 cam_fill_ataio(ataio,
1223 ((ranges + 63) / 64) * 512,
1224 ada_default_timeout * 1000);
1225 ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT,
1226 ATA_DSM_TRIM, 0, (ranges + 63) / 64);
1227 start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM;
1230 /* Run regular command. */
1231 bp = bioq_first(&softc->bio_queue);
1233 xpt_release_ccb(start_ccb);
1236 bioq_remove(&softc->bio_queue, bp);
1238 if ((bp->bio_flags & BIO_ORDERED) != 0
1239 || (softc->flags & ADA_FLAG_NEED_OTAG) != 0) {
1240 softc->flags &= ~ADA_FLAG_NEED_OTAG;
1241 softc->ordered_tag_count++;
1246 switch (bp->bio_cmd) {
1250 uint64_t lba = bp->bio_pblkno;
1251 uint16_t count = bp->bio_bcount / softc->params.secsize;
1252 #ifdef ADA_TEST_FAILURE
1256 * Support the failure ioctls. If the command is a
1257 * read, and there are pending forced read errors, or
1258 * if a write and pending write errors, then fail this
1259 * operation with EIO. This is useful for testing
1260 * purposes. Also, support having every Nth read fail.
1262 * This is a rather blunt tool.
1264 if (bp->bio_cmd == BIO_READ) {
1265 if (softc->force_read_error) {
1266 softc->force_read_error--;
1269 if (softc->periodic_read_error > 0) {
1270 if (++softc->periodic_read_count >=
1271 softc->periodic_read_error) {
1272 softc->periodic_read_count = 0;
1277 if (softc->force_write_error) {
1278 softc->force_write_error--;
1283 bp->bio_error = EIO;
1284 bp->bio_flags |= BIO_ERROR;
1286 xpt_release_ccb(start_ccb);
1287 adaschedule(periph);
1291 cam_fill_ataio(ataio,
1294 bp->bio_cmd == BIO_READ ?
1295 CAM_DIR_IN : CAM_DIR_OUT,
1299 ada_default_timeout*1000);
1301 if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) {
1302 if (bp->bio_cmd == BIO_READ) {
1303 ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED,
1306 ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED,
1309 } else if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
1310 (lba + count >= ATA_MAX_28BIT_LBA ||
1312 if (softc->flags & ADA_FLAG_CAN_DMA) {
1313 if (bp->bio_cmd == BIO_READ) {
1314 ata_48bit_cmd(ataio, ATA_READ_DMA48,
1317 ata_48bit_cmd(ataio, ATA_WRITE_DMA48,
1321 if (bp->bio_cmd == BIO_READ) {
1322 ata_48bit_cmd(ataio, ATA_READ_MUL48,
1325 ata_48bit_cmd(ataio, ATA_WRITE_MUL48,
1332 if (softc->flags & ADA_FLAG_CAN_DMA) {
1333 if (bp->bio_cmd == BIO_READ) {
1334 ata_28bit_cmd(ataio, ATA_READ_DMA,
1337 ata_28bit_cmd(ataio, ATA_WRITE_DMA,
1341 if (bp->bio_cmd == BIO_READ) {
1342 ata_28bit_cmd(ataio, ATA_READ_MUL,
1345 ata_28bit_cmd(ataio, ATA_WRITE_MUL,
1354 uint64_t lba = bp->bio_pblkno;
1355 uint16_t count = bp->bio_bcount / softc->params.secsize;
1357 cam_fill_ataio(ataio,
1364 ada_default_timeout*1000);
1368 ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count);
1372 cam_fill_ataio(ataio,
1379 ada_default_timeout*1000);
1381 if (softc->flags & ADA_FLAG_CAN_48BIT)
1382 ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1384 ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0);
1387 start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO;
1389 start_ccb->ccb_h.ccb_bp = bp;
1390 softc->outstanding_cmds++;
1391 xpt_action(start_ccb);
1393 /* May have more work to do, so ensure we stay scheduled */
1394 adaschedule(periph);
1397 case ADA_STATE_RAHEAD:
1398 case ADA_STATE_WCACHE:
1400 if (softc->flags & ADA_FLAG_PACK_INVALID) {
1401 softc->state = ADA_STATE_NORMAL;
1402 xpt_release_ccb(start_ccb);
1403 cam_release_devq(periph->path,
1404 RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_DEV + 1, FALSE);
1405 adaschedule(periph);
1406 cam_periph_release_locked(periph);
1410 cam_fill_ataio(ataio,
1417 ada_default_timeout*1000);
1419 if (softc->state == ADA_STATE_RAHEAD) {
1420 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_RA ?
1421 ATA_SF_ENAB_RCACHE : ATA_SF_DIS_RCACHE, 0, 0);
1422 start_ccb->ccb_h.ccb_state = ADA_CCB_RAHEAD;
1424 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_WC ?
1425 ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0);
1426 start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE;
1428 xpt_action(start_ccb);
1435 adadone(struct cam_periph *periph, union ccb *done_ccb)
1437 struct ada_softc *softc;
1438 struct ccb_ataio *ataio;
1439 struct ccb_getdev *cgd;
1441 softc = (struct ada_softc *)periph->softc;
1442 ataio = &done_ccb->ataio;
1443 switch (ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) {
1444 case ADA_CCB_BUFFER_IO:
1449 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1450 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1453 error = adaerror(done_ccb, 0, 0);
1454 if (error == ERESTART) {
1455 /* A retry was scheduled, so just return. */
1459 if (error == ENXIO &&
1460 (softc->flags & ADA_FLAG_PACK_INVALID) == 0) {
1462 * Catastrophic error. Mark our pack as
1466 * XXX See if this is really a media
1469 xpt_print(periph->path,
1470 "Invalidating pack\n");
1471 softc->flags |= ADA_FLAG_PACK_INVALID;
1473 bp->bio_error = error;
1474 bp->bio_resid = bp->bio_bcount;
1475 bp->bio_flags |= BIO_ERROR;
1477 bp->bio_resid = ataio->resid;
1479 if (bp->bio_resid != 0)
1480 bp->bio_flags |= BIO_ERROR;
1482 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1483 cam_release_devq(done_ccb->ccb_h.path,
1487 /*getcount_only*/0);
1489 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1490 panic("REQ_CMP with QFRZN");
1491 bp->bio_resid = ataio->resid;
1492 if (ataio->resid > 0)
1493 bp->bio_flags |= BIO_ERROR;
1495 softc->outstanding_cmds--;
1496 if (softc->outstanding_cmds == 0)
1497 softc->flags |= ADA_FLAG_WENT_IDLE;
1498 if ((ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) ==
1500 struct trim_request *req =
1501 (struct trim_request *)ataio->data_ptr;
1504 for (i = 1; i < TRIM_MAX_BIOS && req->bps[i]; i++) {
1505 struct bio *bp1 = req->bps[i];
1507 bp1->bio_resid = bp->bio_resid;
1508 bp1->bio_error = bp->bio_error;
1509 if (bp->bio_flags & BIO_ERROR)
1510 bp1->bio_flags |= BIO_ERROR;
1513 softc->trim_running = 0;
1515 adaschedule(periph);
1520 case ADA_CCB_RAHEAD:
1522 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1523 if (adaerror(done_ccb, 0, 0) == ERESTART) {
1525 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1526 cam_release_devq(done_ccb->ccb_h.path,
1530 /*getcount_only*/0);
1535 * Since our peripheral may be invalidated by an error
1536 * above or an external event, we must release our CCB
1537 * before releasing the reference on the peripheral.
1538 * The peripheral will only go away once the last reference
1539 * is removed, and we need it around for the CCB release
1542 cgd = (struct ccb_getdev *)done_ccb;
1543 xpt_setup_ccb(&cgd->ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1544 cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1545 xpt_action((union ccb *)cgd);
1547 cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) {
1548 softc->state = ADA_STATE_WCACHE;
1549 xpt_release_ccb(done_ccb);
1550 xpt_schedule(periph, CAM_PRIORITY_DEV);
1553 softc->state = ADA_STATE_NORMAL;
1554 xpt_release_ccb(done_ccb);
1555 cam_release_devq(periph->path,
1556 RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_DEV + 1, FALSE);
1557 adaschedule(periph);
1558 cam_periph_release_locked(periph);
1561 case ADA_CCB_WCACHE:
1563 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1564 if (adaerror(done_ccb, 0, 0) == ERESTART) {
1566 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1567 cam_release_devq(done_ccb->ccb_h.path,
1571 /*getcount_only*/0);
1575 softc->state = ADA_STATE_NORMAL;
1577 * Since our peripheral may be invalidated by an error
1578 * above or an external event, we must release our CCB
1579 * before releasing the reference on the peripheral.
1580 * The peripheral will only go away once the last reference
1581 * is removed, and we need it around for the CCB release
1584 xpt_release_ccb(done_ccb);
1585 cam_release_devq(periph->path,
1586 RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_DEV + 1, FALSE);
1587 adaschedule(periph);
1588 cam_periph_release_locked(periph);
1591 case ADA_CCB_WAITING:
1593 /* Caller will release the CCB */
1594 wakeup(&done_ccb->ccb_h.cbfcnp);
1598 /* No-op. We're polling */
1603 xpt_release_ccb(done_ccb);
1607 adaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1610 return(cam_periph_error(ccb, cam_flags, sense_flags, NULL));
1614 adagetparams(struct cam_periph *periph, struct ccb_getdev *cgd)
1616 struct ada_softc *softc = (struct ada_softc *)periph->softc;
1617 struct disk_params *dp = &softc->params;
1618 u_int64_t lbasize48;
1621 dp->secsize = ata_logical_sector_size(&cgd->ident_data);
1622 if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) &&
1623 cgd->ident_data.current_heads && cgd->ident_data.current_sectors) {
1624 dp->heads = cgd->ident_data.current_heads;
1625 dp->secs_per_track = cgd->ident_data.current_sectors;
1626 dp->cylinders = cgd->ident_data.cylinders;
1627 dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 |
1628 ((u_int32_t)cgd->ident_data.current_size_2 << 16);
1630 dp->heads = cgd->ident_data.heads;
1631 dp->secs_per_track = cgd->ident_data.sectors;
1632 dp->cylinders = cgd->ident_data.cylinders;
1633 dp->sectors = cgd->ident_data.cylinders * dp->heads * dp->secs_per_track;
1635 lbasize = (u_int32_t)cgd->ident_data.lba_size_1 |
1636 ((u_int32_t)cgd->ident_data.lba_size_2 << 16);
1638 /* use the 28bit LBA size if valid or bigger than the CHS mapping */
1639 if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize)
1640 dp->sectors = lbasize;
1642 /* use the 48bit LBA size if valid */
1643 lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) |
1644 ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) |
1645 ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) |
1646 ((u_int64_t)cgd->ident_data.lba_size48_4 << 48);
1647 if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) &&
1648 lbasize48 > ATA_MAX_28BIT_LBA)
1649 dp->sectors = lbasize48;
1653 adasendorderedtag(void *arg)
1655 struct ada_softc *softc = arg;
1657 if (ada_send_ordered) {
1658 if ((softc->ordered_tag_count == 0)
1659 && ((softc->flags & ADA_FLAG_WENT_IDLE) == 0)) {
1660 softc->flags |= ADA_FLAG_NEED_OTAG;
1662 if (softc->outstanding_cmds > 0)
1663 softc->flags &= ~ADA_FLAG_WENT_IDLE;
1665 softc->ordered_tag_count = 0;
1667 /* Queue us up again */
1668 callout_reset(&softc->sendordered_c,
1669 (ada_default_timeout * hz) / ADA_ORDEREDTAG_INTERVAL,
1670 adasendorderedtag, softc);
1674 * Step through all ADA peripheral drivers, and if the device is still open,
1675 * sync the disk cache to physical media.
1680 struct cam_periph *periph;
1681 struct ada_softc *softc;
1683 TAILQ_FOREACH(periph, &adadriver.units, unit_links) {
1686 /* If we paniced with lock held - not recurse here. */
1687 if (cam_periph_owned(periph))
1689 cam_periph_lock(periph);
1690 softc = (struct ada_softc *)periph->softc;
1692 * We only sync the cache if the drive is still open, and
1693 * if the drive is capable of it..
1695 if (((softc->flags & ADA_FLAG_OPEN) == 0) ||
1696 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) {
1697 cam_periph_unlock(periph);
1701 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1703 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
1704 cam_fill_ataio(&ccb.ataio,
1711 ada_default_timeout*1000);
1713 if (softc->flags & ADA_FLAG_CAN_48BIT)
1714 ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1716 ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0);
1717 xpt_polled_action(&ccb);
1719 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1720 xpt_print(periph->path, "Synchronize cache failed\n");
1722 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1723 cam_release_devq(ccb.ccb_h.path,
1727 /*getcount_only*/0);
1728 cam_periph_unlock(periph);
1733 adaspindown(uint8_t cmd, int flags)
1735 struct cam_periph *periph;
1736 struct ada_softc *softc;
1738 TAILQ_FOREACH(periph, &adadriver.units, unit_links) {
1741 /* If we paniced with lock held - not recurse here. */
1742 if (cam_periph_owned(periph))
1744 cam_periph_lock(periph);
1745 softc = (struct ada_softc *)periph->softc;
1747 * We only spin-down the drive if it is capable of it..
1749 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
1750 cam_periph_unlock(periph);
1755 xpt_print(periph->path, "spin-down\n");
1757 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1759 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
1760 cam_fill_ataio(&ccb.ataio,
1763 CAM_DIR_NONE | flags,
1767 ada_default_timeout*1000);
1769 ata_28bit_cmd(&ccb.ataio, cmd, 0, 0, 0);
1770 xpt_polled_action(&ccb);
1772 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1773 xpt_print(periph->path, "Spin-down disk failed\n");
1775 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1776 cam_release_devq(ccb.ccb_h.path,
1780 /*getcount_only*/0);
1781 cam_periph_unlock(periph);
1786 adashutdown(void *arg, int howto)
1790 if (ada_spindown_shutdown != 0 &&
1791 (howto & (RB_HALT | RB_POWEROFF)) != 0)
1792 adaspindown(ATA_STANDBY_IMMEDIATE, 0);
1796 adasuspend(void *arg)
1800 if (ada_spindown_suspend != 0)
1801 adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE);
1805 adaresume(void *arg)
1807 struct cam_periph *periph;
1808 struct ada_softc *softc;
1810 if (ada_spindown_suspend == 0)
1813 TAILQ_FOREACH(periph, &adadriver.units, unit_links) {
1814 cam_periph_lock(periph);
1815 softc = (struct ada_softc *)periph->softc;
1817 * We only spin-down the drive if it is capable of it..
1819 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
1820 cam_periph_unlock(periph);
1825 xpt_print(periph->path, "resume\n");
1828 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on
1831 cam_release_devq(periph->path,
1835 /*getcount_only*/0);
1837 cam_periph_unlock(periph);
1841 #endif /* _KERNEL */