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
77 ADA_FLAG_PACK_INVALID = 0x001,
78 ADA_FLAG_CAN_48BIT = 0x002,
79 ADA_FLAG_CAN_FLUSHCACHE = 0x004,
80 ADA_FLAG_CAN_NCQ = 0x008,
81 ADA_FLAG_CAN_DMA = 0x010,
82 ADA_FLAG_NEED_OTAG = 0x020,
83 ADA_FLAG_WENT_IDLE = 0x040,
84 ADA_FLAG_CAN_TRIM = 0x080,
85 ADA_FLAG_OPEN = 0x100,
86 ADA_FLAG_SCTX_INIT = 0x200,
87 ADA_FLAG_CAN_CFA = 0x400,
88 ADA_FLAG_CAN_POWERMGT = 0x800
97 ADA_CCB_WCACHE = 0x01,
98 ADA_CCB_BUFFER_IO = 0x03,
99 ADA_CCB_WAITING = 0x04,
102 ADA_CCB_TYPE_MASK = 0x0F,
105 /* Offsets into our private area for storing information */
106 #define ccb_state ppriv_field0
107 #define ccb_bp ppriv_ptr1
111 u_int8_t secs_per_track;
113 u_int32_t secsize; /* Number of bytes/logical sector */
114 u_int64_t sectors; /* Total number sectors */
117 #define TRIM_MAX_BLOCKS 8
118 #define TRIM_MAX_RANGES (TRIM_MAX_BLOCKS * 64)
119 #define TRIM_MAX_BIOS (TRIM_MAX_RANGES * 4)
120 struct trim_request {
121 uint8_t data[TRIM_MAX_RANGES * 8];
122 struct bio *bps[TRIM_MAX_BIOS];
126 struct bio_queue_head bio_queue;
127 struct bio_queue_head trim_queue;
131 int ordered_tag_count;
132 int outstanding_cmds;
136 #ifdef ADA_TEST_FAILURE
137 int force_read_error;
138 int force_write_error;
139 int periodic_read_error;
140 int periodic_read_count;
142 struct disk_params params;
144 struct task sysctl_task;
145 struct sysctl_ctx_list sysctl_ctx;
146 struct sysctl_oid *sysctl_tree;
147 struct callout sendordered_c;
148 struct trim_request trim_req;
151 struct ada_quirk_entry {
152 struct scsi_inquiry_pattern inq_pat;
156 static struct ada_quirk_entry ada_quirk_table[] =
159 /* Hitachi Advanced Format (4k) drives */
160 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Hitachi H??????????E3*", "*" },
164 /* Samsung Advanced Format (4k) drives */
165 { T_DIRECT, SIP_MEDIA_FIXED, "*", "SAMSUNG HD204UI*", "*" },
169 /* Seagate Barracuda Green Advanced Format (4k) drives */
170 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DL*", "*" },
174 /* Seagate Momentus Advanced Format (4k) drives */
175 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500423AS*", "*" },
179 /* Seagate Momentus Advanced Format (4k) drives */
180 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500424AS*", "*" },
184 /* Seagate Momentus Advanced Format (4k) drives */
185 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750420AS*", "*" },
189 /* Seagate Momentus Advanced Format (4k) drives */
190 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750422AS*", "*" },
194 /* Seagate Momentus Thin Advanced Format (4k) drives */
195 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???LT*", "*" },
199 /* WDC Caviar Green Advanced Format (4k) drives */
200 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RS*", "*" },
204 /* WDC Caviar Green Advanced Format (4k) drives */
205 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RX*", "*" },
209 /* WDC Caviar Green Advanced Format (4k) drives */
210 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RS*", "*" },
214 /* WDC Caviar Green Advanced Format (4k) drives */
215 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RX*", "*" },
219 /* WDC Scorpio Black Advanced Format (4k) drives */
220 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PKT*", "*" },
224 /* WDC Scorpio Black Advanced Format (4k) drives */
225 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PKT*", "*" },
229 /* WDC Scorpio Blue Advanced Format (4k) drives */
230 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PVT*", "*" },
234 /* WDC Scorpio Blue Advanced Format (4k) drives */
235 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PVT*", "*" },
241 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
242 /*vendor*/"*", /*product*/"*", /*revision*/"*"
248 static disk_strategy_t adastrategy;
249 static dumper_t adadump;
250 static periph_init_t adainit;
251 static void adaasync(void *callback_arg, u_int32_t code,
252 struct cam_path *path, void *arg);
253 static void adasysctlinit(void *context, int pending);
254 static periph_ctor_t adaregister;
255 static periph_dtor_t adacleanup;
256 static periph_start_t adastart;
257 static periph_oninv_t adaoninvalidate;
258 static void adadone(struct cam_periph *periph,
259 union ccb *done_ccb);
260 static int adaerror(union ccb *ccb, u_int32_t cam_flags,
261 u_int32_t sense_flags);
262 static void adagetparams(struct cam_periph *periph,
263 struct ccb_getdev *cgd);
264 static timeout_t adasendorderedtag;
265 static void adashutdown(void *arg, int howto);
266 static void adasuspend(void *arg);
267 static void adaresume(void *arg);
269 #ifndef ADA_DEFAULT_LEGACY_ALIASES
271 #define ADA_DEFAULT_LEGACY_ALIASES 1
273 #define ADA_DEFAULT_LEGACY_ALIASES 0
277 #ifndef ADA_DEFAULT_TIMEOUT
278 #define ADA_DEFAULT_TIMEOUT 30 /* Timeout in seconds */
281 #ifndef ADA_DEFAULT_RETRY
282 #define ADA_DEFAULT_RETRY 4
285 #ifndef ADA_DEFAULT_SEND_ORDERED
286 #define ADA_DEFAULT_SEND_ORDERED 1
289 #ifndef ADA_DEFAULT_SPINDOWN_SHUTDOWN
290 #define ADA_DEFAULT_SPINDOWN_SHUTDOWN 1
293 #ifndef ADA_DEFAULT_SPINDOWN_SUSPEND
294 #define ADA_DEFAULT_SPINDOWN_SUSPEND 1
297 #ifndef ADA_DEFAULT_WRITE_CACHE
298 #define ADA_DEFAULT_WRITE_CACHE 1
302 * Most platforms map firmware geometry to actual, but some don't. If
303 * not overridden, default to nothing.
305 #ifndef ata_disk_firmware_geom_adjust
306 #define ata_disk_firmware_geom_adjust(disk)
309 static int ada_legacy_aliases = ADA_DEFAULT_LEGACY_ALIASES;
310 static int ada_retry_count = ADA_DEFAULT_RETRY;
311 static int ada_default_timeout = ADA_DEFAULT_TIMEOUT;
312 static int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED;
313 static int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN;
314 static int ada_spindown_suspend = ADA_DEFAULT_SPINDOWN_SUSPEND;
315 static int ada_write_cache = ADA_DEFAULT_WRITE_CACHE;
317 SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD, 0,
318 "CAM Direct Access Disk driver");
319 SYSCTL_INT(_kern_cam_ada, OID_AUTO, legacy_aliases, CTLFLAG_RW,
320 &ada_legacy_aliases, 0, "Create legacy-like device aliases");
321 TUNABLE_INT("kern.cam.ada.legacy_aliases", &ada_legacy_aliases);
322 SYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RW,
323 &ada_retry_count, 0, "Normal I/O retry count");
324 TUNABLE_INT("kern.cam.ada.retry_count", &ada_retry_count);
325 SYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RW,
326 &ada_default_timeout, 0, "Normal I/O timeout (in seconds)");
327 TUNABLE_INT("kern.cam.ada.default_timeout", &ada_default_timeout);
328 SYSCTL_INT(_kern_cam_ada, OID_AUTO, ada_send_ordered, CTLFLAG_RW,
329 &ada_send_ordered, 0, "Send Ordered Tags");
330 TUNABLE_INT("kern.cam.ada.ada_send_ordered", &ada_send_ordered);
331 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RW,
332 &ada_spindown_shutdown, 0, "Spin down upon shutdown");
333 TUNABLE_INT("kern.cam.ada.spindown_shutdown", &ada_spindown_shutdown);
334 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_suspend, CTLFLAG_RW,
335 &ada_spindown_suspend, 0, "Spin down upon suspend");
336 TUNABLE_INT("kern.cam.ada.spindown_suspend", &ada_spindown_suspend);
337 SYSCTL_INT(_kern_cam_ada, OID_AUTO, write_cache, CTLFLAG_RW,
338 &ada_write_cache, 0, "Enable disk write cache");
339 TUNABLE_INT("kern.cam.ada.write_cache", &ada_write_cache);
342 * ADA_ORDEREDTAG_INTERVAL determines how often, relative
343 * to the default timeout, we check to see whether an ordered
344 * tagged transaction is appropriate to prevent simple tag
345 * starvation. Since we'd like to ensure that there is at least
346 * 1/2 of the timeout length left for a starved transaction to
347 * complete after we've sent an ordered tag, we must poll at least
348 * four times in every timeout period. This takes care of the worst
349 * case where a starved transaction starts during an interval that
350 * meets the requirement "don't send an ordered tag" test so it takes
351 * us two intervals to determine that a tag must be sent.
353 #ifndef ADA_ORDEREDTAG_INTERVAL
354 #define ADA_ORDEREDTAG_INTERVAL 4
357 static struct periph_driver adadriver =
360 TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0
363 PERIPHDRIVER_DECLARE(ada, adadriver);
365 MALLOC_DEFINE(M_ATADA, "ata_da", "ata_da buffers");
368 adaopen(struct disk *dp)
370 struct cam_periph *periph;
371 struct ada_softc *softc;
374 periph = (struct cam_periph *)dp->d_drv1;
375 if (periph == NULL) {
379 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
383 cam_periph_lock(periph);
384 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
385 cam_periph_unlock(periph);
386 cam_periph_release(periph);
390 softc = (struct ada_softc *)periph->softc;
391 softc->flags |= ADA_FLAG_OPEN;
393 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
394 ("adaopen: disk=%s%d (unit %d)\n", dp->d_name, dp->d_unit,
395 periph->unit_number));
397 if ((softc->flags & ADA_FLAG_PACK_INVALID) != 0) {
398 /* Invalidate our pack information. */
399 softc->flags &= ~ADA_FLAG_PACK_INVALID;
402 cam_periph_unhold(periph);
403 cam_periph_unlock(periph);
408 adaclose(struct disk *dp)
410 struct cam_periph *periph;
411 struct ada_softc *softc;
415 periph = (struct cam_periph *)dp->d_drv1;
419 cam_periph_lock(periph);
420 if ((error = cam_periph_hold(periph, PRIBIO)) != 0) {
421 cam_periph_unlock(periph);
422 cam_periph_release(periph);
426 softc = (struct ada_softc *)periph->softc;
427 /* We only sync the cache if the drive is capable of it. */
428 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) {
430 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
431 cam_fill_ataio(&ccb->ataio,
438 ada_default_timeout*1000);
440 if (softc->flags & ADA_FLAG_CAN_48BIT)
441 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
443 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
444 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
445 /*sense_flags*/0, softc->disk->d_devstat);
447 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
448 xpt_print(periph->path, "Synchronize cache failed\n");
450 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
451 cam_release_devq(ccb->ccb_h.path,
456 xpt_release_ccb(ccb);
459 softc->flags &= ~ADA_FLAG_OPEN;
460 cam_periph_unhold(periph);
461 cam_periph_unlock(periph);
462 cam_periph_release(periph);
467 adaschedule(struct cam_periph *periph)
469 struct ada_softc *softc = (struct ada_softc *)periph->softc;
471 if (bioq_first(&softc->bio_queue) ||
472 (!softc->trim_running && bioq_first(&softc->trim_queue))) {
473 /* Have more work to do, so ensure we stay scheduled */
474 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
479 * Actually translate the requested transfer into one the physical driver
480 * can understand. The transfer is described by a buf and will include
481 * only one physical transfer.
484 adastrategy(struct bio *bp)
486 struct cam_periph *periph;
487 struct ada_softc *softc;
489 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
490 if (periph == NULL) {
491 biofinish(bp, NULL, ENXIO);
494 softc = (struct ada_softc *)periph->softc;
496 cam_periph_lock(periph);
499 * If the device has been made invalid, error out
501 if ((softc->flags & ADA_FLAG_PACK_INVALID)) {
502 cam_periph_unlock(periph);
503 biofinish(bp, NULL, ENXIO);
508 * Place it in the queue of disk activities for this disk
510 if (bp->bio_cmd == BIO_DELETE &&
511 (softc->flags & ADA_FLAG_CAN_TRIM))
512 bioq_disksort(&softc->trim_queue, bp);
514 bioq_disksort(&softc->bio_queue, bp);
517 * Schedule ourselves for performing the work.
520 cam_periph_unlock(periph);
526 adadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
528 struct cam_periph *periph;
529 struct ada_softc *softc;
540 softc = (struct ada_softc *)periph->softc;
541 cam_periph_lock(periph);
542 secsize = softc->params.secsize;
543 lba = offset / secsize;
544 count = length / secsize;
546 if ((softc->flags & ADA_FLAG_PACK_INVALID) != 0) {
547 cam_periph_unlock(periph);
552 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
553 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
554 cam_fill_ataio(&ccb.ataio,
559 (u_int8_t *) virtual,
561 ada_default_timeout*1000);
562 if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
563 (lba + count >= ATA_MAX_28BIT_LBA ||
565 ata_48bit_cmd(&ccb.ataio, ATA_WRITE_DMA48,
568 ata_28bit_cmd(&ccb.ataio, ATA_WRITE_DMA,
571 xpt_polled_action(&ccb);
573 if ((ccb.ataio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
574 printf("Aborting dump due to I/O error.\n");
575 cam_periph_unlock(periph);
578 cam_periph_unlock(periph);
582 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) {
583 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
585 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
586 cam_fill_ataio(&ccb.ataio,
593 ada_default_timeout*1000);
595 if (softc->flags & ADA_FLAG_CAN_48BIT)
596 ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0);
598 ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0);
599 xpt_polled_action(&ccb);
601 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
602 xpt_print(periph->path, "Synchronize cache failed\n");
604 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
605 cam_release_devq(ccb.ccb_h.path,
611 cam_periph_unlock(periph);
621 * Install a global async callback. This callback will
622 * receive async callbacks like "new device found".
624 status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL);
626 if (status != CAM_REQ_CMP) {
627 printf("ada: Failed to attach master async callback "
628 "due to status 0x%x!\n", status);
629 } else if (ada_send_ordered) {
631 /* Register our event handlers */
632 if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend,
633 NULL, EVENTHANDLER_PRI_LAST)) == NULL)
634 printf("adainit: power event registration failed!\n");
635 if ((EVENTHANDLER_REGISTER(power_resume, adaresume,
636 NULL, EVENTHANDLER_PRI_LAST)) == NULL)
637 printf("adainit: power event registration failed!\n");
638 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown,
639 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
640 printf("adainit: shutdown event registration failed!\n");
645 adaoninvalidate(struct cam_periph *periph)
647 struct ada_softc *softc;
649 softc = (struct ada_softc *)periph->softc;
652 * De-register any async callbacks.
654 xpt_register_async(0, adaasync, periph, periph->path);
656 softc->flags |= ADA_FLAG_PACK_INVALID;
659 * Return all queued I/O with ENXIO.
660 * XXX Handle any transactions queued to the card
661 * with XPT_ABORT_CCB.
663 bioq_flush(&softc->bio_queue, NULL, ENXIO);
664 bioq_flush(&softc->trim_queue, NULL, ENXIO);
666 disk_gone(softc->disk);
667 xpt_print(periph->path, "lost device\n");
671 adacleanup(struct cam_periph *periph)
673 struct ada_softc *softc;
675 softc = (struct ada_softc *)periph->softc;
677 xpt_print(periph->path, "removing device entry\n");
678 cam_periph_unlock(periph);
681 * If we can't free the sysctl tree, oh well...
683 if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0
684 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
685 xpt_print(periph->path, "can't remove sysctl context\n");
688 disk_destroy(softc->disk);
689 callout_drain(&softc->sendordered_c);
690 free(softc, M_DEVBUF);
691 cam_periph_lock(periph);
695 adaasync(void *callback_arg, u_int32_t code,
696 struct cam_path *path, void *arg)
698 struct cam_periph *periph;
699 struct ada_softc *softc;
701 periph = (struct cam_periph *)callback_arg;
703 case AC_FOUND_DEVICE:
705 struct ccb_getdev *cgd;
708 cgd = (struct ccb_getdev *)arg;
712 if (cgd->protocol != PROTO_ATA)
716 * Allocate a peripheral instance for
717 * this device and start the probe
720 status = cam_periph_alloc(adaregister, adaoninvalidate,
721 adacleanup, adastart,
722 "ada", CAM_PERIPH_BIO,
723 cgd->ccb_h.path, adaasync,
724 AC_FOUND_DEVICE, cgd);
726 if (status != CAM_REQ_CMP
727 && status != CAM_REQ_INPROG)
728 printf("adaasync: Unable to attach to new device "
729 "due to status 0x%x\n", status);
735 struct ccb_getdev cgd;
737 softc = (struct ada_softc *)periph->softc;
738 cam_periph_async(periph, code, path, arg);
739 if (ada_write_cache < 0 && softc->write_cache < 0)
741 if (softc->state != ADA_STATE_NORMAL)
743 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
744 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
745 xpt_action((union ccb *)&cgd);
746 if ((cgd.ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) == 0)
748 softc->state = ADA_STATE_WCACHE;
749 cam_periph_acquire(periph);
750 cam_freeze_devq_arg(periph->path,
751 RELSIM_RELEASE_RUNLEVEL, CAM_RL_DEV + 1);
752 xpt_schedule(periph, CAM_PRIORITY_DEV);
755 cam_periph_async(periph, code, path, arg);
761 adasysctlinit(void *context, int pending)
763 struct cam_periph *periph;
764 struct ada_softc *softc;
765 char tmpstr[80], tmpstr2[80];
767 periph = (struct cam_periph *)context;
769 /* periph was held for us when this task was enqueued */
770 if (periph->flags & CAM_PERIPH_INVALID) {
771 cam_periph_release(periph);
775 softc = (struct ada_softc *)periph->softc;
776 snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d", periph->unit_number);
777 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
779 sysctl_ctx_init(&softc->sysctl_ctx);
780 softc->flags |= ADA_FLAG_SCTX_INIT;
781 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
782 SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2,
783 CTLFLAG_RD, 0, tmpstr);
784 if (softc->sysctl_tree == NULL) {
785 printf("adasysctlinit: unable to allocate sysctl tree\n");
786 cam_periph_release(periph);
790 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
791 OID_AUTO, "write_cache", CTLFLAG_RW | CTLFLAG_MPSAFE,
792 &softc->write_cache, 0, "Enable disk write cache.");
793 #ifdef ADA_TEST_FAILURE
795 * Add a 'door bell' sysctl which allows one to set it from userland
796 * and cause something bad to happen. For the moment, we only allow
797 * whacking the next read or write.
799 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
800 OID_AUTO, "force_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
801 &softc->force_read_error, 0,
802 "Force a read error for the next N reads.");
803 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
804 OID_AUTO, "force_write_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
805 &softc->force_write_error, 0,
806 "Force a write error for the next N writes.");
807 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
808 OID_AUTO, "periodic_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
809 &softc->periodic_read_error, 0,
810 "Force a read error every N reads (don't set too low).");
812 cam_periph_release(periph);
816 adagetattr(struct bio *bp)
819 struct cam_periph *periph;
821 if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
823 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
824 if (periph->path == NULL)
827 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
830 bp->bio_completed = bp->bio_length;
835 adaregister(struct cam_periph *periph, void *arg)
837 struct ada_softc *softc;
838 struct ccb_pathinq cpi;
839 struct ccb_getdev *cgd;
840 char announce_buf[80], buf1[32];
841 struct disk_params *dp;
844 int legacy_id, quirks;
846 cgd = (struct ccb_getdev *)arg;
847 if (periph == NULL) {
848 printf("adaregister: periph was NULL!!\n");
849 return(CAM_REQ_CMP_ERR);
853 printf("adaregister: no getdev CCB, can't register device\n");
854 return(CAM_REQ_CMP_ERR);
857 softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF,
861 printf("adaregister: Unable to probe new device. "
862 "Unable to allocate softc\n");
863 return(CAM_REQ_CMP_ERR);
866 bioq_init(&softc->bio_queue);
867 bioq_init(&softc->trim_queue);
869 if (cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA &&
870 (cgd->inq_flags & SID_DMA))
871 softc->flags |= ADA_FLAG_CAN_DMA;
872 if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48)
873 softc->flags |= ADA_FLAG_CAN_48BIT;
874 if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE)
875 softc->flags |= ADA_FLAG_CAN_FLUSHCACHE;
876 if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT)
877 softc->flags |= ADA_FLAG_CAN_POWERMGT;
878 if (cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ &&
879 (cgd->inq_flags & SID_DMA) && (cgd->inq_flags & SID_CmdQue))
880 softc->flags |= ADA_FLAG_CAN_NCQ;
881 if (cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) {
882 softc->flags |= ADA_FLAG_CAN_TRIM;
883 softc->trim_max_ranges = TRIM_MAX_RANGES;
884 if (cgd->ident_data.max_dsm_blocks != 0) {
885 softc->trim_max_ranges =
886 min(cgd->ident_data.max_dsm_blocks * 64,
887 softc->trim_max_ranges);
890 if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA)
891 softc->flags |= ADA_FLAG_CAN_CFA;
893 periph->softc = softc;
896 * See if this device has any quirks.
898 match = cam_quirkmatch((caddr_t)&cgd->ident_data,
899 (caddr_t)ada_quirk_table,
900 sizeof(ada_quirk_table)/sizeof(*ada_quirk_table),
901 sizeof(*ada_quirk_table), ata_identify_match);
903 softc->quirks = ((struct ada_quirk_entry *)match)->quirks;
905 softc->quirks = ADA_Q_NONE;
907 bzero(&cpi, sizeof(cpi));
908 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE);
909 cpi.ccb_h.func_code = XPT_PATH_INQ;
910 xpt_action((union ccb *)&cpi);
912 TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph);
915 * Register this media as a disk
917 (void)cam_periph_hold(periph, PRIBIO);
918 mtx_unlock(periph->sim->mtx);
919 snprintf(announce_buf, sizeof(announce_buf),
920 "kern.cam.ada.%d.quirks", periph->unit_number);
921 quirks = softc->quirks;
922 TUNABLE_INT_FETCH(announce_buf, &quirks);
923 softc->quirks = quirks;
924 softc->write_cache = -1;
925 snprintf(announce_buf, sizeof(announce_buf),
926 "kern.cam.ada.%d.write_cache", periph->unit_number);
927 TUNABLE_INT_FETCH(announce_buf, &softc->write_cache);
928 adagetparams(periph, cgd);
929 softc->disk = disk_alloc();
930 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
931 periph->unit_number, softc->params.secsize,
932 DEVSTAT_ALL_SUPPORTED,
933 DEVSTAT_TYPE_DIRECT |
934 XPORT_DEVSTAT_TYPE(cpi.transport),
935 DEVSTAT_PRIORITY_DISK);
936 softc->disk->d_open = adaopen;
937 softc->disk->d_close = adaclose;
938 softc->disk->d_strategy = adastrategy;
939 softc->disk->d_getattr = adagetattr;
940 softc->disk->d_dump = adadump;
941 softc->disk->d_name = "ada";
942 softc->disk->d_drv1 = periph;
943 maxio = cpi.maxio; /* Honor max I/O size of SIM */
945 maxio = DFLTPHYS; /* traditional default */
946 else if (maxio > MAXPHYS)
947 maxio = MAXPHYS; /* for safety */
948 if (softc->flags & ADA_FLAG_CAN_48BIT)
949 maxio = min(maxio, 65536 * softc->params.secsize);
950 else /* 28bit ATA command limit */
951 maxio = min(maxio, 256 * softc->params.secsize);
952 softc->disk->d_maxsize = maxio;
953 softc->disk->d_unit = periph->unit_number;
954 softc->disk->d_flags = 0;
955 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE)
956 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
957 if ((softc->flags & ADA_FLAG_CAN_TRIM) ||
958 ((softc->flags & ADA_FLAG_CAN_CFA) &&
959 !(softc->flags & ADA_FLAG_CAN_48BIT)))
960 softc->disk->d_flags |= DISKFLAG_CANDELETE;
961 strlcpy(softc->disk->d_descr, cgd->ident_data.model,
962 MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model)));
963 softc->disk->d_hba_vendor = cpi.hba_vendor;
964 softc->disk->d_hba_device = cpi.hba_device;
965 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
966 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
968 softc->disk->d_sectorsize = softc->params.secsize;
969 softc->disk->d_mediasize = (off_t)softc->params.sectors *
970 softc->params.secsize;
971 if (ata_physical_sector_size(&cgd->ident_data) !=
972 softc->params.secsize) {
973 softc->disk->d_stripesize =
974 ata_physical_sector_size(&cgd->ident_data);
975 softc->disk->d_stripeoffset = (softc->disk->d_stripesize -
976 ata_logical_sector_offset(&cgd->ident_data)) %
977 softc->disk->d_stripesize;
978 } else if (softc->quirks & ADA_Q_4K) {
979 softc->disk->d_stripesize = 4096;
980 softc->disk->d_stripeoffset = 0;
982 softc->disk->d_fwsectors = softc->params.secs_per_track;
983 softc->disk->d_fwheads = softc->params.heads;
984 ata_disk_firmware_geom_adjust(softc->disk);
986 if (ada_legacy_aliases) {
988 legacy_id = xpt_path_legacy_ata_id(periph->path);
990 legacy_id = softc->disk->d_unit;
992 if (legacy_id >= 0) {
993 snprintf(announce_buf, sizeof(announce_buf),
994 "kern.devalias.%s%d",
995 softc->disk->d_name, softc->disk->d_unit);
996 snprintf(buf1, sizeof(buf1),
998 setenv(announce_buf, buf1);
1002 disk_create(softc->disk, DISK_VERSION);
1003 mtx_lock(periph->sim->mtx);
1004 cam_periph_unhold(periph);
1006 dp = &softc->params;
1007 snprintf(announce_buf, sizeof(announce_buf),
1008 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1009 (uintmax_t)(((uintmax_t)dp->secsize *
1010 dp->sectors) / (1024*1024)),
1011 (uintmax_t)dp->sectors,
1012 dp->secsize, dp->heads,
1013 dp->secs_per_track, dp->cylinders);
1014 xpt_announce_periph(periph, announce_buf);
1016 printf("%s%d: Previously was known as ad%d\n",
1017 periph->periph_name, periph->unit_number, legacy_id);
1020 * Create our sysctl variables, now that we know
1021 * we have successfully attached.
1023 cam_periph_acquire(periph);
1024 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task);
1027 * Add async callbacks for bus reset and
1028 * bus device reset calls. I don't bother
1029 * checking if this fails as, in most cases,
1030 * the system will function just fine without
1031 * them and the only alternative would be to
1032 * not attach the device on failure.
1034 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
1035 adaasync, periph, periph->path);
1038 * Schedule a periodic event to occasionally send an
1039 * ordered tag to a device.
1041 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
1042 callout_reset(&softc->sendordered_c,
1043 (ADA_DEFAULT_TIMEOUT * hz) / ADA_ORDEREDTAG_INTERVAL,
1044 adasendorderedtag, softc);
1046 if ((ada_write_cache >= 0 || softc->write_cache >= 0) &&
1047 cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) {
1048 softc->state = ADA_STATE_WCACHE;
1049 cam_periph_acquire(periph);
1050 cam_freeze_devq_arg(periph->path,
1051 RELSIM_RELEASE_RUNLEVEL, CAM_RL_DEV + 1);
1052 xpt_schedule(periph, CAM_PRIORITY_DEV);
1054 softc->state = ADA_STATE_NORMAL;
1056 return(CAM_REQ_CMP);
1060 adastart(struct cam_periph *periph, union ccb *start_ccb)
1062 struct ada_softc *softc = (struct ada_softc *)periph->softc;
1063 struct ccb_ataio *ataio = &start_ccb->ataio;
1065 switch (softc->state) {
1066 case ADA_STATE_NORMAL:
1071 /* Execute immediate CCB if waiting. */
1072 if (periph->immediate_priority <= periph->pinfo.priority) {
1073 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1074 ("queuing for immediate ccb\n"));
1075 start_ccb->ccb_h.ccb_state = ADA_CCB_WAITING;
1076 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1078 periph->immediate_priority = CAM_PRIORITY_NONE;
1079 wakeup(&periph->ccb_list);
1080 /* Have more work to do, so ensure we stay scheduled */
1081 adaschedule(periph);
1084 /* Run TRIM if not running yet. */
1085 if (!softc->trim_running &&
1086 (bp = bioq_first(&softc->trim_queue)) != 0) {
1087 struct trim_request *req = &softc->trim_req;
1089 uint64_t lastlba = (uint64_t)-1;
1090 int bps = 0, c, lastcount = 0, off, ranges = 0;
1092 softc->trim_running = 1;
1093 bzero(req, sizeof(*req));
1096 uint64_t lba = bp1->bio_pblkno;
1097 int count = bp1->bio_bcount /
1098 softc->params.secsize;
1100 bioq_remove(&softc->trim_queue, bp1);
1102 /* Try to extend the previous range. */
1103 if (lba == lastlba) {
1104 c = min(count, 0xffff - lastcount);
1106 off = (ranges - 1) * 8;
1107 req->data[off + 6] = lastcount & 0xff;
1108 req->data[off + 7] =
1109 (lastcount >> 8) & 0xff;
1115 c = min(count, 0xffff);
1117 req->data[off + 0] = lba & 0xff;
1118 req->data[off + 1] = (lba >> 8) & 0xff;
1119 req->data[off + 2] = (lba >> 16) & 0xff;
1120 req->data[off + 3] = (lba >> 24) & 0xff;
1121 req->data[off + 4] = (lba >> 32) & 0xff;
1122 req->data[off + 5] = (lba >> 40) & 0xff;
1123 req->data[off + 6] = c & 0xff;
1124 req->data[off + 7] = (c >> 8) & 0xff;
1131 req->bps[bps++] = bp1;
1132 bp1 = bioq_first(&softc->trim_queue);
1133 if (bps >= TRIM_MAX_BIOS ||
1135 bp1->bio_bcount / softc->params.secsize >
1136 (softc->trim_max_ranges - ranges) * 0xffff)
1139 cam_fill_ataio(ataio,
1145 ((ranges + 63) / 64) * 512,
1146 ada_default_timeout * 1000);
1147 ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT,
1148 ATA_DSM_TRIM, 0, (ranges + 63) / 64);
1149 start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM;
1152 /* Run regular command. */
1153 bp = bioq_first(&softc->bio_queue);
1155 xpt_release_ccb(start_ccb);
1158 bioq_remove(&softc->bio_queue, bp);
1160 if ((bp->bio_flags & BIO_ORDERED) != 0
1161 || (softc->flags & ADA_FLAG_NEED_OTAG) != 0) {
1162 softc->flags &= ~ADA_FLAG_NEED_OTAG;
1163 softc->ordered_tag_count++;
1168 switch (bp->bio_cmd) {
1172 uint64_t lba = bp->bio_pblkno;
1173 uint16_t count = bp->bio_bcount / softc->params.secsize;
1174 #ifdef ADA_TEST_FAILURE
1178 * Support the failure ioctls. If the command is a
1179 * read, and there are pending forced read errors, or
1180 * if a write and pending write errors, then fail this
1181 * operation with EIO. This is useful for testing
1182 * purposes. Also, support having every Nth read fail.
1184 * This is a rather blunt tool.
1186 if (bp->bio_cmd == BIO_READ) {
1187 if (softc->force_read_error) {
1188 softc->force_read_error--;
1191 if (softc->periodic_read_error > 0) {
1192 if (++softc->periodic_read_count >=
1193 softc->periodic_read_error) {
1194 softc->periodic_read_count = 0;
1199 if (softc->force_write_error) {
1200 softc->force_write_error--;
1205 bp->bio_error = EIO;
1206 bp->bio_flags |= BIO_ERROR;
1208 xpt_release_ccb(start_ccb);
1209 adaschedule(periph);
1213 cam_fill_ataio(ataio,
1216 bp->bio_cmd == BIO_READ ?
1217 CAM_DIR_IN : CAM_DIR_OUT,
1221 ada_default_timeout*1000);
1223 if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) {
1224 if (bp->bio_cmd == BIO_READ) {
1225 ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED,
1228 ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED,
1231 } else if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
1232 (lba + count >= ATA_MAX_28BIT_LBA ||
1234 if (softc->flags & ADA_FLAG_CAN_DMA) {
1235 if (bp->bio_cmd == BIO_READ) {
1236 ata_48bit_cmd(ataio, ATA_READ_DMA48,
1239 ata_48bit_cmd(ataio, ATA_WRITE_DMA48,
1243 if (bp->bio_cmd == BIO_READ) {
1244 ata_48bit_cmd(ataio, ATA_READ_MUL48,
1247 ata_48bit_cmd(ataio, ATA_WRITE_MUL48,
1254 if (softc->flags & ADA_FLAG_CAN_DMA) {
1255 if (bp->bio_cmd == BIO_READ) {
1256 ata_28bit_cmd(ataio, ATA_READ_DMA,
1259 ata_28bit_cmd(ataio, ATA_WRITE_DMA,
1263 if (bp->bio_cmd == BIO_READ) {
1264 ata_28bit_cmd(ataio, ATA_READ_MUL,
1267 ata_28bit_cmd(ataio, ATA_WRITE_MUL,
1276 uint64_t lba = bp->bio_pblkno;
1277 uint16_t count = bp->bio_bcount / softc->params.secsize;
1279 cam_fill_ataio(ataio,
1286 ada_default_timeout*1000);
1290 ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count);
1294 cam_fill_ataio(ataio,
1301 ada_default_timeout*1000);
1303 if (softc->flags & ADA_FLAG_CAN_48BIT)
1304 ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1306 ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0);
1309 start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO;
1311 start_ccb->ccb_h.ccb_bp = bp;
1312 softc->outstanding_cmds++;
1313 xpt_action(start_ccb);
1315 /* May have more work to do, so ensure we stay scheduled */
1316 adaschedule(periph);
1319 case ADA_STATE_WCACHE:
1321 cam_fill_ataio(ataio,
1328 ada_default_timeout*1000);
1330 ata_28bit_cmd(ataio, ATA_SETFEATURES, (softc->write_cache > 0 ||
1331 (softc->write_cache < 0 && ada_write_cache)) ?
1332 ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0);
1333 start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE;
1334 xpt_action(start_ccb);
1341 adadone(struct cam_periph *periph, union ccb *done_ccb)
1343 struct ada_softc *softc;
1344 struct ccb_ataio *ataio;
1346 softc = (struct ada_softc *)periph->softc;
1347 ataio = &done_ccb->ataio;
1348 switch (ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) {
1349 case ADA_CCB_BUFFER_IO:
1354 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1355 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1358 error = adaerror(done_ccb, 0, 0);
1359 if (error == ERESTART) {
1360 /* A retry was scheduled, so just return. */
1364 if (error == ENXIO) {
1366 * Catastrophic error. Mark our pack as
1370 * XXX See if this is really a media
1373 xpt_print(periph->path,
1374 "Invalidating pack\n");
1375 softc->flags |= ADA_FLAG_PACK_INVALID;
1377 bp->bio_error = error;
1378 bp->bio_resid = bp->bio_bcount;
1379 bp->bio_flags |= BIO_ERROR;
1381 bp->bio_resid = ataio->resid;
1383 if (bp->bio_resid != 0)
1384 bp->bio_flags |= BIO_ERROR;
1386 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1387 cam_release_devq(done_ccb->ccb_h.path,
1391 /*getcount_only*/0);
1393 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1394 panic("REQ_CMP with QFRZN");
1395 bp->bio_resid = ataio->resid;
1396 if (ataio->resid > 0)
1397 bp->bio_flags |= BIO_ERROR;
1399 softc->outstanding_cmds--;
1400 if (softc->outstanding_cmds == 0)
1401 softc->flags |= ADA_FLAG_WENT_IDLE;
1402 if ((ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) ==
1404 struct trim_request *req =
1405 (struct trim_request *)ataio->data_ptr;
1408 for (i = 1; i < TRIM_MAX_BIOS && req->bps[i]; i++) {
1409 struct bio *bp1 = req->bps[i];
1411 bp1->bio_resid = bp->bio_resid;
1412 bp1->bio_error = bp->bio_error;
1413 if (bp->bio_flags & BIO_ERROR)
1414 bp1->bio_flags |= BIO_ERROR;
1417 softc->trim_running = 0;
1419 adaschedule(periph);
1424 case ADA_CCB_WCACHE:
1426 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1427 if (adaerror(done_ccb, 0, 0) == ERESTART) {
1429 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1430 cam_release_devq(done_ccb->ccb_h.path,
1434 /*getcount_only*/0);
1438 softc->state = ADA_STATE_NORMAL;
1440 * Since our peripheral may be invalidated by an error
1441 * above or an external event, we must release our CCB
1442 * before releasing the reference on the peripheral.
1443 * The peripheral will only go away once the last reference
1444 * is removed, and we need it around for the CCB release
1447 xpt_release_ccb(done_ccb);
1448 cam_release_devq(periph->path,
1449 RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_DEV + 1, FALSE);
1450 adaschedule(periph);
1451 cam_periph_release_locked(periph);
1454 case ADA_CCB_WAITING:
1456 /* Caller will release the CCB */
1457 wakeup(&done_ccb->ccb_h.cbfcnp);
1461 /* No-op. We're polling */
1466 xpt_release_ccb(done_ccb);
1470 adaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1473 return(cam_periph_error(ccb, cam_flags, sense_flags, NULL));
1477 adagetparams(struct cam_periph *periph, struct ccb_getdev *cgd)
1479 struct ada_softc *softc = (struct ada_softc *)periph->softc;
1480 struct disk_params *dp = &softc->params;
1481 u_int64_t lbasize48;
1484 dp->secsize = ata_logical_sector_size(&cgd->ident_data);
1485 if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) &&
1486 cgd->ident_data.current_heads && cgd->ident_data.current_sectors) {
1487 dp->heads = cgd->ident_data.current_heads;
1488 dp->secs_per_track = cgd->ident_data.current_sectors;
1489 dp->cylinders = cgd->ident_data.cylinders;
1490 dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 |
1491 ((u_int32_t)cgd->ident_data.current_size_2 << 16);
1493 dp->heads = cgd->ident_data.heads;
1494 dp->secs_per_track = cgd->ident_data.sectors;
1495 dp->cylinders = cgd->ident_data.cylinders;
1496 dp->sectors = cgd->ident_data.cylinders * dp->heads * dp->secs_per_track;
1498 lbasize = (u_int32_t)cgd->ident_data.lba_size_1 |
1499 ((u_int32_t)cgd->ident_data.lba_size_2 << 16);
1501 /* use the 28bit LBA size if valid or bigger than the CHS mapping */
1502 if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize)
1503 dp->sectors = lbasize;
1505 /* use the 48bit LBA size if valid */
1506 lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) |
1507 ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) |
1508 ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) |
1509 ((u_int64_t)cgd->ident_data.lba_size48_4 << 48);
1510 if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) &&
1511 lbasize48 > ATA_MAX_28BIT_LBA)
1512 dp->sectors = lbasize48;
1516 adasendorderedtag(void *arg)
1518 struct ada_softc *softc = arg;
1520 if (ada_send_ordered) {
1521 if ((softc->ordered_tag_count == 0)
1522 && ((softc->flags & ADA_FLAG_WENT_IDLE) == 0)) {
1523 softc->flags |= ADA_FLAG_NEED_OTAG;
1525 if (softc->outstanding_cmds > 0)
1526 softc->flags &= ~ADA_FLAG_WENT_IDLE;
1528 softc->ordered_tag_count = 0;
1530 /* Queue us up again */
1531 callout_reset(&softc->sendordered_c,
1532 (ADA_DEFAULT_TIMEOUT * hz) / ADA_ORDEREDTAG_INTERVAL,
1533 adasendorderedtag, softc);
1537 * Step through all ADA peripheral drivers, and if the device is still open,
1538 * sync the disk cache to physical media.
1543 struct cam_periph *periph;
1544 struct ada_softc *softc;
1546 TAILQ_FOREACH(periph, &adadriver.units, unit_links) {
1549 /* If we paniced with lock held - not recurse here. */
1550 if (cam_periph_owned(periph))
1552 cam_periph_lock(periph);
1553 softc = (struct ada_softc *)periph->softc;
1555 * We only sync the cache if the drive is still open, and
1556 * if the drive is capable of it..
1558 if (((softc->flags & ADA_FLAG_OPEN) == 0) ||
1559 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) {
1560 cam_periph_unlock(periph);
1564 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1566 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
1567 cam_fill_ataio(&ccb.ataio,
1574 ada_default_timeout*1000);
1576 if (softc->flags & ADA_FLAG_CAN_48BIT)
1577 ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1579 ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0);
1580 xpt_polled_action(&ccb);
1582 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1583 xpt_print(periph->path, "Synchronize cache failed\n");
1585 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1586 cam_release_devq(ccb.ccb_h.path,
1590 /*getcount_only*/0);
1591 cam_periph_unlock(periph);
1596 adaspindown(uint8_t cmd, int flags)
1598 struct cam_periph *periph;
1599 struct ada_softc *softc;
1601 TAILQ_FOREACH(periph, &adadriver.units, unit_links) {
1604 /* If we paniced with lock held - not recurse here. */
1605 if (cam_periph_owned(periph))
1607 cam_periph_lock(periph);
1608 softc = (struct ada_softc *)periph->softc;
1610 * We only spin-down the drive if it is capable of it..
1612 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
1613 cam_periph_unlock(periph);
1618 xpt_print(periph->path, "spin-down\n");
1620 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1622 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
1623 cam_fill_ataio(&ccb.ataio,
1626 CAM_DIR_NONE | flags,
1630 ada_default_timeout*1000);
1632 ata_28bit_cmd(&ccb.ataio, cmd, 0, 0, 0);
1633 xpt_polled_action(&ccb);
1635 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1636 xpt_print(periph->path, "Spin-down disk failed\n");
1638 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1639 cam_release_devq(ccb.ccb_h.path,
1643 /*getcount_only*/0);
1644 cam_periph_unlock(periph);
1649 adashutdown(void *arg, int howto)
1653 if (ada_spindown_shutdown != 0 &&
1654 (howto & (RB_HALT | RB_POWEROFF)) != 0)
1655 adaspindown(ATA_STANDBY_IMMEDIATE, 0);
1659 adasuspend(void *arg)
1663 if (ada_spindown_suspend != 0)
1664 adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE);
1668 adaresume(void *arg)
1670 struct cam_periph *periph;
1671 struct ada_softc *softc;
1673 if (ada_spindown_suspend == 0)
1676 TAILQ_FOREACH(periph, &adadriver.units, unit_links) {
1677 cam_periph_lock(periph);
1678 softc = (struct ada_softc *)periph->softc;
1680 * We only spin-down the drive if it is capable of it..
1682 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
1683 cam_periph_unlock(periph);
1688 xpt_print(periph->path, "resume\n");
1691 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on
1694 cam_release_devq(periph->path,
1698 /*getcount_only*/0);
1700 cam_periph_unlock(periph);
1704 #endif /* _KERNEL */