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 HD204UI*", "*" },
172 /* Seagate Barracuda Green Advanced Format (4k) drives */
173 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST????DL*", "*" },
177 /* Seagate Momentus Advanced Format (4k) drives */
178 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500423AS*", "*" },
182 /* Seagate Momentus Advanced Format (4k) drives */
183 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9500424AS*", "*" },
187 /* Seagate Momentus Advanced Format (4k) drives */
188 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750420AS*", "*" },
192 /* Seagate Momentus Advanced Format (4k) drives */
193 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST9750422AS*", "*" },
197 /* Seagate Momentus Thin Advanced Format (4k) drives */
198 { T_DIRECT, SIP_MEDIA_FIXED, "*", "ST???LT*", "*" },
202 /* WDC Caviar Green Advanced Format (4k) drives */
203 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RS*", "*" },
207 /* WDC Caviar Green Advanced Format (4k) drives */
208 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD????RX*", "*" },
212 /* WDC Caviar Green Advanced Format (4k) drives */
213 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RS*", "*" },
217 /* WDC Caviar Green Advanced Format (4k) drives */
218 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD??????RX*", "*" },
222 /* WDC Scorpio Black Advanced Format (4k) drives */
223 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PKT*", "*" },
227 /* WDC Scorpio Black Advanced Format (4k) drives */
228 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PKT*", "*" },
232 /* WDC Scorpio Blue Advanced Format (4k) drives */
233 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD???PVT*", "*" },
237 /* WDC Scorpio Blue Advanced Format (4k) drives */
238 { T_DIRECT, SIP_MEDIA_FIXED, "*", "WDC WD?????PVT*", "*" },
244 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
245 /*vendor*/"*", /*product*/"*", /*revision*/"*"
251 static disk_strategy_t adastrategy;
252 static dumper_t adadump;
253 static periph_init_t adainit;
254 static void adaasync(void *callback_arg, u_int32_t code,
255 struct cam_path *path, void *arg);
256 static void adasysctlinit(void *context, int pending);
257 static periph_ctor_t adaregister;
258 static periph_dtor_t adacleanup;
259 static periph_start_t adastart;
260 static periph_oninv_t adaoninvalidate;
261 static void adadone(struct cam_periph *periph,
262 union ccb *done_ccb);
263 static int adaerror(union ccb *ccb, u_int32_t cam_flags,
264 u_int32_t sense_flags);
265 static void adagetparams(struct cam_periph *periph,
266 struct ccb_getdev *cgd);
267 static timeout_t adasendorderedtag;
268 static void adashutdown(void *arg, int howto);
269 static void adasuspend(void *arg);
270 static void adaresume(void *arg);
272 #ifndef ADA_DEFAULT_LEGACY_ALIASES
274 #define ADA_DEFAULT_LEGACY_ALIASES 1
276 #define ADA_DEFAULT_LEGACY_ALIASES 0
280 #ifndef ADA_DEFAULT_TIMEOUT
281 #define ADA_DEFAULT_TIMEOUT 30 /* Timeout in seconds */
284 #ifndef ADA_DEFAULT_RETRY
285 #define ADA_DEFAULT_RETRY 4
288 #ifndef ADA_DEFAULT_SEND_ORDERED
289 #define ADA_DEFAULT_SEND_ORDERED 1
292 #ifndef ADA_DEFAULT_SPINDOWN_SHUTDOWN
293 #define ADA_DEFAULT_SPINDOWN_SHUTDOWN 1
296 #ifndef ADA_DEFAULT_SPINDOWN_SUSPEND
297 #define ADA_DEFAULT_SPINDOWN_SUSPEND 1
300 #ifndef ADA_DEFAULT_READ_AHEAD
301 #define ADA_DEFAULT_READ_AHEAD 1
304 #ifndef ADA_DEFAULT_WRITE_CACHE
305 #define ADA_DEFAULT_WRITE_CACHE 1
308 #define ADA_RA (softc->read_ahead >= 0 ? \
309 softc->read_ahead : ada_read_ahead)
310 #define ADA_WC (softc->write_cache >= 0 ? \
311 softc->write_cache : ada_write_cache)
314 * Most platforms map firmware geometry to actual, but some don't. If
315 * not overridden, default to nothing.
317 #ifndef ata_disk_firmware_geom_adjust
318 #define ata_disk_firmware_geom_adjust(disk)
321 static int ada_legacy_aliases = ADA_DEFAULT_LEGACY_ALIASES;
322 static int ada_retry_count = ADA_DEFAULT_RETRY;
323 static int ada_default_timeout = ADA_DEFAULT_TIMEOUT;
324 static int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED;
325 static int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN;
326 static int ada_spindown_suspend = ADA_DEFAULT_SPINDOWN_SUSPEND;
327 static int ada_read_ahead = ADA_DEFAULT_READ_AHEAD;
328 static int ada_write_cache = ADA_DEFAULT_WRITE_CACHE;
330 SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD, 0,
331 "CAM Direct Access Disk driver");
332 SYSCTL_INT(_kern_cam_ada, OID_AUTO, legacy_aliases, CTLFLAG_RW,
333 &ada_legacy_aliases, 0, "Create legacy-like device aliases");
334 TUNABLE_INT("kern.cam.ada.legacy_aliases", &ada_legacy_aliases);
335 SYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RW,
336 &ada_retry_count, 0, "Normal I/O retry count");
337 TUNABLE_INT("kern.cam.ada.retry_count", &ada_retry_count);
338 SYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RW,
339 &ada_default_timeout, 0, "Normal I/O timeout (in seconds)");
340 TUNABLE_INT("kern.cam.ada.default_timeout", &ada_default_timeout);
341 SYSCTL_INT(_kern_cam_ada, OID_AUTO, ada_send_ordered, CTLFLAG_RW,
342 &ada_send_ordered, 0, "Send Ordered Tags");
343 TUNABLE_INT("kern.cam.ada.ada_send_ordered", &ada_send_ordered);
344 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RW,
345 &ada_spindown_shutdown, 0, "Spin down upon shutdown");
346 TUNABLE_INT("kern.cam.ada.spindown_shutdown", &ada_spindown_shutdown);
347 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_suspend, CTLFLAG_RW,
348 &ada_spindown_suspend, 0, "Spin down upon suspend");
349 TUNABLE_INT("kern.cam.ada.spindown_suspend", &ada_spindown_suspend);
350 SYSCTL_INT(_kern_cam_ada, OID_AUTO, read_ahead, CTLFLAG_RW,
351 &ada_read_ahead, 0, "Enable disk read-ahead");
352 TUNABLE_INT("kern.cam.ada.read_ahead", &ada_read_ahead);
353 SYSCTL_INT(_kern_cam_ada, OID_AUTO, write_cache, CTLFLAG_RW,
354 &ada_write_cache, 0, "Enable disk write cache");
355 TUNABLE_INT("kern.cam.ada.write_cache", &ada_write_cache);
358 * ADA_ORDEREDTAG_INTERVAL determines how often, relative
359 * to the default timeout, we check to see whether an ordered
360 * tagged transaction is appropriate to prevent simple tag
361 * starvation. Since we'd like to ensure that there is at least
362 * 1/2 of the timeout length left for a starved transaction to
363 * complete after we've sent an ordered tag, we must poll at least
364 * four times in every timeout period. This takes care of the worst
365 * case where a starved transaction starts during an interval that
366 * meets the requirement "don't send an ordered tag" test so it takes
367 * us two intervals to determine that a tag must be sent.
369 #ifndef ADA_ORDEREDTAG_INTERVAL
370 #define ADA_ORDEREDTAG_INTERVAL 4
373 static struct periph_driver adadriver =
376 TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0
379 PERIPHDRIVER_DECLARE(ada, adadriver);
381 MALLOC_DEFINE(M_ATADA, "ata_da", "ata_da buffers");
384 adaopen(struct disk *dp)
386 struct cam_periph *periph;
387 struct ada_softc *softc;
390 periph = (struct cam_periph *)dp->d_drv1;
391 if (periph == NULL) {
395 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
399 cam_periph_lock(periph);
400 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
401 cam_periph_unlock(periph);
402 cam_periph_release(periph);
406 softc = (struct ada_softc *)periph->softc;
407 softc->flags |= ADA_FLAG_OPEN;
409 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
410 ("adaopen: disk=%s%d (unit %d)\n", dp->d_name, dp->d_unit,
411 periph->unit_number));
413 if ((softc->flags & ADA_FLAG_PACK_INVALID) != 0) {
414 /* Invalidate our pack information. */
415 softc->flags &= ~ADA_FLAG_PACK_INVALID;
418 cam_periph_unhold(periph);
419 cam_periph_unlock(periph);
424 adaclose(struct disk *dp)
426 struct cam_periph *periph;
427 struct ada_softc *softc;
431 periph = (struct cam_periph *)dp->d_drv1;
435 cam_periph_lock(periph);
436 if ((error = cam_periph_hold(periph, PRIBIO)) != 0) {
437 cam_periph_unlock(periph);
438 cam_periph_release(periph);
442 softc = (struct ada_softc *)periph->softc;
443 /* We only sync the cache if the drive is capable of it. */
444 if ((softc->flags & ADA_FLAG_CAN_FLUSHCACHE) != 0 &&
445 (softc->flags & ADA_FLAG_PACK_INVALID) == 0) {
447 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
448 cam_fill_ataio(&ccb->ataio,
455 ada_default_timeout*1000);
457 if (softc->flags & ADA_FLAG_CAN_48BIT)
458 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
460 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
461 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
462 /*sense_flags*/0, softc->disk->d_devstat);
464 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
465 xpt_print(periph->path, "Synchronize cache failed\n");
467 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
468 cam_release_devq(ccb->ccb_h.path,
473 xpt_release_ccb(ccb);
476 softc->flags &= ~ADA_FLAG_OPEN;
477 cam_periph_unhold(periph);
478 cam_periph_unlock(periph);
479 cam_periph_release(periph);
484 adaschedule(struct cam_periph *periph)
486 struct ada_softc *softc = (struct ada_softc *)periph->softc;
489 /* Check if cam_periph_getccb() was called. */
490 prio = periph->immediate_priority;
492 /* Check if we have more work to do. */
493 if (bioq_first(&softc->bio_queue) ||
494 (!softc->trim_running && bioq_first(&softc->trim_queue))) {
495 prio = CAM_PRIORITY_NORMAL;
498 /* Schedule CCB if any of above is true. */
499 if (prio != CAM_PRIORITY_NONE)
500 xpt_schedule(periph, prio);
504 * Actually translate the requested transfer into one the physical driver
505 * can understand. The transfer is described by a buf and will include
506 * only one physical transfer.
509 adastrategy(struct bio *bp)
511 struct cam_periph *periph;
512 struct ada_softc *softc;
514 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
515 if (periph == NULL) {
516 biofinish(bp, NULL, ENXIO);
519 softc = (struct ada_softc *)periph->softc;
521 cam_periph_lock(periph);
524 * If the device has been made invalid, error out
526 if ((softc->flags & ADA_FLAG_PACK_INVALID)) {
527 cam_periph_unlock(periph);
528 biofinish(bp, NULL, ENXIO);
533 * Place it in the queue of disk activities for this disk
535 if (bp->bio_cmd == BIO_DELETE &&
536 (softc->flags & ADA_FLAG_CAN_TRIM))
537 bioq_disksort(&softc->trim_queue, bp);
539 bioq_disksort(&softc->bio_queue, bp);
542 * Schedule ourselves for performing the work.
545 cam_periph_unlock(periph);
551 adadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
553 struct cam_periph *periph;
554 struct ada_softc *softc;
565 softc = (struct ada_softc *)periph->softc;
566 cam_periph_lock(periph);
567 secsize = softc->params.secsize;
568 lba = offset / secsize;
569 count = length / secsize;
571 if ((softc->flags & ADA_FLAG_PACK_INVALID) != 0) {
572 cam_periph_unlock(periph);
577 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
578 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
579 cam_fill_ataio(&ccb.ataio,
584 (u_int8_t *) virtual,
586 ada_default_timeout*1000);
587 if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
588 (lba + count >= ATA_MAX_28BIT_LBA ||
590 ata_48bit_cmd(&ccb.ataio, ATA_WRITE_DMA48,
593 ata_28bit_cmd(&ccb.ataio, ATA_WRITE_DMA,
596 xpt_polled_action(&ccb);
598 if ((ccb.ataio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
599 printf("Aborting dump due to I/O error.\n");
600 cam_periph_unlock(periph);
603 cam_periph_unlock(periph);
607 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) {
608 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
610 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
611 cam_fill_ataio(&ccb.ataio,
618 ada_default_timeout*1000);
620 if (softc->flags & ADA_FLAG_CAN_48BIT)
621 ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0);
623 ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0);
624 xpt_polled_action(&ccb);
626 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
627 xpt_print(periph->path, "Synchronize cache failed\n");
629 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
630 cam_release_devq(ccb.ccb_h.path,
636 cam_periph_unlock(periph);
646 * Install a global async callback. This callback will
647 * receive async callbacks like "new device found".
649 status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL);
651 if (status != CAM_REQ_CMP) {
652 printf("ada: Failed to attach master async callback "
653 "due to status 0x%x!\n", status);
654 } else if (ada_send_ordered) {
656 /* Register our event handlers */
657 if ((EVENTHANDLER_REGISTER(power_suspend, adasuspend,
658 NULL, EVENTHANDLER_PRI_LAST)) == NULL)
659 printf("adainit: power event registration failed!\n");
660 if ((EVENTHANDLER_REGISTER(power_resume, adaresume,
661 NULL, EVENTHANDLER_PRI_LAST)) == NULL)
662 printf("adainit: power event registration failed!\n");
663 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown,
664 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
665 printf("adainit: shutdown event registration failed!\n");
670 adaoninvalidate(struct cam_periph *periph)
672 struct ada_softc *softc;
674 softc = (struct ada_softc *)periph->softc;
677 * De-register any async callbacks.
679 xpt_register_async(0, adaasync, periph, periph->path);
681 softc->flags |= ADA_FLAG_PACK_INVALID;
684 * Return all queued I/O with ENXIO.
685 * XXX Handle any transactions queued to the card
686 * with XPT_ABORT_CCB.
688 bioq_flush(&softc->bio_queue, NULL, ENXIO);
689 bioq_flush(&softc->trim_queue, NULL, ENXIO);
691 disk_gone(softc->disk);
692 xpt_print(periph->path, "lost device\n");
696 adacleanup(struct cam_periph *periph)
698 struct ada_softc *softc;
700 softc = (struct ada_softc *)periph->softc;
702 xpt_print(periph->path, "removing device entry\n");
703 cam_periph_unlock(periph);
706 * If we can't free the sysctl tree, oh well...
708 if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0
709 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
710 xpt_print(periph->path, "can't remove sysctl context\n");
713 disk_destroy(softc->disk);
714 callout_drain(&softc->sendordered_c);
715 free(softc, M_DEVBUF);
716 cam_periph_lock(periph);
720 adaasync(void *callback_arg, u_int32_t code,
721 struct cam_path *path, void *arg)
723 struct cam_periph *periph;
724 struct ada_softc *softc;
726 periph = (struct cam_periph *)callback_arg;
728 case AC_FOUND_DEVICE:
730 struct ccb_getdev *cgd;
733 cgd = (struct ccb_getdev *)arg;
737 if (cgd->protocol != PROTO_ATA)
741 * Allocate a peripheral instance for
742 * this device and start the probe
745 status = cam_periph_alloc(adaregister, adaoninvalidate,
746 adacleanup, adastart,
747 "ada", CAM_PERIPH_BIO,
748 cgd->ccb_h.path, adaasync,
749 AC_FOUND_DEVICE, cgd);
751 if (status != CAM_REQ_CMP
752 && status != CAM_REQ_INPROG)
753 printf("adaasync: Unable to attach to new device "
754 "due to status 0x%x\n", status);
760 struct ccb_getdev cgd;
762 softc = (struct ada_softc *)periph->softc;
763 cam_periph_async(periph, code, path, arg);
764 if (softc->state != ADA_STATE_NORMAL)
766 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
767 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
768 xpt_action((union ccb *)&cgd);
770 cgd.ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD)
771 softc->state = ADA_STATE_RAHEAD;
772 else if (ADA_WC >= 0 &&
773 cgd.ident_data.support.command1 & ATA_SUPPORT_WRITECACHE)
774 softc->state = ADA_STATE_WCACHE;
777 cam_periph_acquire(periph);
778 cam_freeze_devq_arg(periph->path,
779 RELSIM_RELEASE_RUNLEVEL, CAM_RL_DEV + 1);
780 xpt_schedule(periph, CAM_PRIORITY_DEV);
783 cam_periph_async(periph, code, path, arg);
789 adasysctlinit(void *context, int pending)
791 struct cam_periph *periph;
792 struct ada_softc *softc;
793 char tmpstr[80], tmpstr2[80];
795 periph = (struct cam_periph *)context;
797 /* periph was held for us when this task was enqueued */
798 if (periph->flags & CAM_PERIPH_INVALID) {
799 cam_periph_release(periph);
803 softc = (struct ada_softc *)periph->softc;
804 snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d", periph->unit_number);
805 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
807 sysctl_ctx_init(&softc->sysctl_ctx);
808 softc->flags |= ADA_FLAG_SCTX_INIT;
809 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
810 SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2,
811 CTLFLAG_RD, 0, tmpstr);
812 if (softc->sysctl_tree == NULL) {
813 printf("adasysctlinit: unable to allocate sysctl tree\n");
814 cam_periph_release(periph);
818 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
819 OID_AUTO, "read_ahead", CTLFLAG_RW | CTLFLAG_MPSAFE,
820 &softc->read_ahead, 0, "Enable disk read ahead.");
821 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
822 OID_AUTO, "write_cache", CTLFLAG_RW | CTLFLAG_MPSAFE,
823 &softc->write_cache, 0, "Enable disk write cache.");
824 #ifdef ADA_TEST_FAILURE
826 * Add a 'door bell' sysctl which allows one to set it from userland
827 * and cause something bad to happen. For the moment, we only allow
828 * whacking the next read or write.
830 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
831 OID_AUTO, "force_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
832 &softc->force_read_error, 0,
833 "Force a read error for the next N reads.");
834 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
835 OID_AUTO, "force_write_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
836 &softc->force_write_error, 0,
837 "Force a write error for the next N writes.");
838 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
839 OID_AUTO, "periodic_read_error", CTLFLAG_RW | CTLFLAG_MPSAFE,
840 &softc->periodic_read_error, 0,
841 "Force a read error every N reads (don't set too low).");
843 cam_periph_release(periph);
847 adagetattr(struct bio *bp)
850 struct cam_periph *periph;
852 if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
854 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
855 if (periph->path == NULL)
858 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
861 bp->bio_completed = bp->bio_length;
866 adaregister(struct cam_periph *periph, void *arg)
868 struct ada_softc *softc;
869 struct ccb_pathinq cpi;
870 struct ccb_getdev *cgd;
871 char announce_buf[80], buf1[32];
872 struct disk_params *dp;
875 int legacy_id, quirks;
877 cgd = (struct ccb_getdev *)arg;
878 if (periph == NULL) {
879 printf("adaregister: periph was NULL!!\n");
880 return(CAM_REQ_CMP_ERR);
884 printf("adaregister: no getdev CCB, can't register device\n");
885 return(CAM_REQ_CMP_ERR);
888 softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF,
892 printf("adaregister: Unable to probe new device. "
893 "Unable to allocate softc\n");
894 return(CAM_REQ_CMP_ERR);
897 bioq_init(&softc->bio_queue);
898 bioq_init(&softc->trim_queue);
900 if (cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA &&
901 (cgd->inq_flags & SID_DMA))
902 softc->flags |= ADA_FLAG_CAN_DMA;
903 if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48)
904 softc->flags |= ADA_FLAG_CAN_48BIT;
905 if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE)
906 softc->flags |= ADA_FLAG_CAN_FLUSHCACHE;
907 if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT)
908 softc->flags |= ADA_FLAG_CAN_POWERMGT;
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;
912 if (cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) {
913 softc->flags |= ADA_FLAG_CAN_TRIM;
914 softc->trim_max_ranges = TRIM_MAX_RANGES;
915 if (cgd->ident_data.max_dsm_blocks != 0) {
916 softc->trim_max_ranges =
917 min(cgd->ident_data.max_dsm_blocks * 64,
918 softc->trim_max_ranges);
921 if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA)
922 softc->flags |= ADA_FLAG_CAN_CFA;
924 periph->softc = softc;
927 * See if this device has any quirks.
929 match = cam_quirkmatch((caddr_t)&cgd->ident_data,
930 (caddr_t)ada_quirk_table,
931 sizeof(ada_quirk_table)/sizeof(*ada_quirk_table),
932 sizeof(*ada_quirk_table), ata_identify_match);
934 softc->quirks = ((struct ada_quirk_entry *)match)->quirks;
936 softc->quirks = ADA_Q_NONE;
938 bzero(&cpi, sizeof(cpi));
939 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE);
940 cpi.ccb_h.func_code = XPT_PATH_INQ;
941 xpt_action((union ccb *)&cpi);
943 TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph);
946 * Register this media as a disk
948 (void)cam_periph_hold(periph, PRIBIO);
949 mtx_unlock(periph->sim->mtx);
950 snprintf(announce_buf, sizeof(announce_buf),
951 "kern.cam.ada.%d.quirks", periph->unit_number);
952 quirks = softc->quirks;
953 TUNABLE_INT_FETCH(announce_buf, &quirks);
954 softc->quirks = quirks;
955 softc->read_ahead = -1;
956 snprintf(announce_buf, sizeof(announce_buf),
957 "kern.cam.ada.%d.read_ahead", periph->unit_number);
958 TUNABLE_INT_FETCH(announce_buf, &softc->read_ahead);
959 softc->write_cache = -1;
960 snprintf(announce_buf, sizeof(announce_buf),
961 "kern.cam.ada.%d.write_cache", periph->unit_number);
962 TUNABLE_INT_FETCH(announce_buf, &softc->write_cache);
963 adagetparams(periph, cgd);
964 softc->disk = disk_alloc();
965 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
966 periph->unit_number, softc->params.secsize,
967 DEVSTAT_ALL_SUPPORTED,
968 DEVSTAT_TYPE_DIRECT |
969 XPORT_DEVSTAT_TYPE(cpi.transport),
970 DEVSTAT_PRIORITY_DISK);
971 softc->disk->d_open = adaopen;
972 softc->disk->d_close = adaclose;
973 softc->disk->d_strategy = adastrategy;
974 softc->disk->d_getattr = adagetattr;
975 softc->disk->d_dump = adadump;
976 softc->disk->d_name = "ada";
977 softc->disk->d_drv1 = periph;
978 maxio = cpi.maxio; /* Honor max I/O size of SIM */
980 maxio = DFLTPHYS; /* traditional default */
981 else if (maxio > MAXPHYS)
982 maxio = MAXPHYS; /* for safety */
983 if (softc->flags & ADA_FLAG_CAN_48BIT)
984 maxio = min(maxio, 65536 * softc->params.secsize);
985 else /* 28bit ATA command limit */
986 maxio = min(maxio, 256 * softc->params.secsize);
987 softc->disk->d_maxsize = maxio;
988 softc->disk->d_unit = periph->unit_number;
989 softc->disk->d_flags = 0;
990 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE)
991 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
992 if ((softc->flags & ADA_FLAG_CAN_TRIM) ||
993 ((softc->flags & ADA_FLAG_CAN_CFA) &&
994 !(softc->flags & ADA_FLAG_CAN_48BIT)))
995 softc->disk->d_flags |= DISKFLAG_CANDELETE;
996 strlcpy(softc->disk->d_descr, cgd->ident_data.model,
997 MIN(sizeof(softc->disk->d_descr), sizeof(cgd->ident_data.model)));
998 softc->disk->d_hba_vendor = cpi.hba_vendor;
999 softc->disk->d_hba_device = cpi.hba_device;
1000 softc->disk->d_hba_subvendor = cpi.hba_subvendor;
1001 softc->disk->d_hba_subdevice = cpi.hba_subdevice;
1003 softc->disk->d_sectorsize = softc->params.secsize;
1004 softc->disk->d_mediasize = (off_t)softc->params.sectors *
1005 softc->params.secsize;
1006 if (ata_physical_sector_size(&cgd->ident_data) !=
1007 softc->params.secsize) {
1008 softc->disk->d_stripesize =
1009 ata_physical_sector_size(&cgd->ident_data);
1010 softc->disk->d_stripeoffset = (softc->disk->d_stripesize -
1011 ata_logical_sector_offset(&cgd->ident_data)) %
1012 softc->disk->d_stripesize;
1013 } else if (softc->quirks & ADA_Q_4K) {
1014 softc->disk->d_stripesize = 4096;
1015 softc->disk->d_stripeoffset = 0;
1017 softc->disk->d_fwsectors = softc->params.secs_per_track;
1018 softc->disk->d_fwheads = softc->params.heads;
1019 ata_disk_firmware_geom_adjust(softc->disk);
1021 if (ada_legacy_aliases) {
1022 #ifdef ATA_STATIC_ID
1023 legacy_id = xpt_path_legacy_ata_id(periph->path);
1025 legacy_id = softc->disk->d_unit;
1027 if (legacy_id >= 0) {
1028 snprintf(announce_buf, sizeof(announce_buf),
1029 "kern.devalias.%s%d",
1030 softc->disk->d_name, softc->disk->d_unit);
1031 snprintf(buf1, sizeof(buf1),
1033 setenv(announce_buf, buf1);
1037 disk_create(softc->disk, DISK_VERSION);
1038 mtx_lock(periph->sim->mtx);
1039 cam_periph_unhold(periph);
1041 dp = &softc->params;
1042 snprintf(announce_buf, sizeof(announce_buf),
1043 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1044 (uintmax_t)(((uintmax_t)dp->secsize *
1045 dp->sectors) / (1024*1024)),
1046 (uintmax_t)dp->sectors,
1047 dp->secsize, dp->heads,
1048 dp->secs_per_track, dp->cylinders);
1049 xpt_announce_periph(periph, announce_buf);
1051 printf("%s%d: Previously was known as ad%d\n",
1052 periph->periph_name, periph->unit_number, legacy_id);
1055 * Create our sysctl variables, now that we know
1056 * we have successfully attached.
1058 cam_periph_acquire(periph);
1059 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task);
1062 * Add async callbacks for bus reset and
1063 * bus device reset calls. I don't bother
1064 * checking if this fails as, in most cases,
1065 * the system will function just fine without
1066 * them and the only alternative would be to
1067 * not attach the device on failure.
1069 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
1070 adaasync, periph, periph->path);
1073 * Schedule a periodic event to occasionally send an
1074 * ordered tag to a device.
1076 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
1077 callout_reset(&softc->sendordered_c,
1078 (ADA_DEFAULT_TIMEOUT * hz) / ADA_ORDEREDTAG_INTERVAL,
1079 adasendorderedtag, softc);
1082 cgd->ident_data.support.command1 & ATA_SUPPORT_LOOKAHEAD) {
1083 softc->state = ADA_STATE_RAHEAD;
1084 cam_periph_acquire(periph);
1085 cam_freeze_devq_arg(periph->path,
1086 RELSIM_RELEASE_RUNLEVEL, CAM_RL_DEV + 1);
1087 xpt_schedule(periph, CAM_PRIORITY_DEV);
1088 } else if (ADA_WC >= 0 &&
1089 cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) {
1090 softc->state = ADA_STATE_WCACHE;
1091 cam_periph_acquire(periph);
1092 cam_freeze_devq_arg(periph->path,
1093 RELSIM_RELEASE_RUNLEVEL, CAM_RL_DEV + 1);
1094 xpt_schedule(periph, CAM_PRIORITY_DEV);
1096 softc->state = ADA_STATE_NORMAL;
1098 return(CAM_REQ_CMP);
1102 adastart(struct cam_periph *periph, union ccb *start_ccb)
1104 struct ada_softc *softc = (struct ada_softc *)periph->softc;
1105 struct ccb_ataio *ataio = &start_ccb->ataio;
1107 switch (softc->state) {
1108 case ADA_STATE_NORMAL:
1113 /* Execute immediate CCB if waiting. */
1114 if (periph->immediate_priority <= periph->pinfo.priority) {
1115 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1116 ("queuing for immediate ccb\n"));
1117 start_ccb->ccb_h.ccb_state = ADA_CCB_WAITING;
1118 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1120 periph->immediate_priority = CAM_PRIORITY_NONE;
1121 wakeup(&periph->ccb_list);
1122 /* Have more work to do, so ensure we stay scheduled */
1123 adaschedule(periph);
1126 /* Run TRIM if not running yet. */
1127 if (!softc->trim_running &&
1128 (bp = bioq_first(&softc->trim_queue)) != 0) {
1129 struct trim_request *req = &softc->trim_req;
1131 uint64_t lastlba = (uint64_t)-1;
1132 int bps = 0, c, lastcount = 0, off, ranges = 0;
1134 softc->trim_running = 1;
1135 bzero(req, sizeof(*req));
1138 uint64_t lba = bp1->bio_pblkno;
1139 int count = bp1->bio_bcount /
1140 softc->params.secsize;
1142 bioq_remove(&softc->trim_queue, bp1);
1144 /* Try to extend the previous range. */
1145 if (lba == lastlba) {
1146 c = min(count, 0xffff - lastcount);
1148 off = (ranges - 1) * 8;
1149 req->data[off + 6] = lastcount & 0xff;
1150 req->data[off + 7] =
1151 (lastcount >> 8) & 0xff;
1157 c = min(count, 0xffff);
1159 req->data[off + 0] = lba & 0xff;
1160 req->data[off + 1] = (lba >> 8) & 0xff;
1161 req->data[off + 2] = (lba >> 16) & 0xff;
1162 req->data[off + 3] = (lba >> 24) & 0xff;
1163 req->data[off + 4] = (lba >> 32) & 0xff;
1164 req->data[off + 5] = (lba >> 40) & 0xff;
1165 req->data[off + 6] = c & 0xff;
1166 req->data[off + 7] = (c >> 8) & 0xff;
1173 req->bps[bps++] = bp1;
1174 bp1 = bioq_first(&softc->trim_queue);
1175 if (bps >= TRIM_MAX_BIOS ||
1177 bp1->bio_bcount / softc->params.secsize >
1178 (softc->trim_max_ranges - ranges) * 0xffff)
1181 cam_fill_ataio(ataio,
1187 ((ranges + 63) / 64) * 512,
1188 ada_default_timeout * 1000);
1189 ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT,
1190 ATA_DSM_TRIM, 0, (ranges + 63) / 64);
1191 start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM;
1194 /* Run regular command. */
1195 bp = bioq_first(&softc->bio_queue);
1197 xpt_release_ccb(start_ccb);
1200 bioq_remove(&softc->bio_queue, bp);
1202 if ((bp->bio_flags & BIO_ORDERED) != 0
1203 || (softc->flags & ADA_FLAG_NEED_OTAG) != 0) {
1204 softc->flags &= ~ADA_FLAG_NEED_OTAG;
1205 softc->ordered_tag_count++;
1210 switch (bp->bio_cmd) {
1214 uint64_t lba = bp->bio_pblkno;
1215 uint16_t count = bp->bio_bcount / softc->params.secsize;
1216 #ifdef ADA_TEST_FAILURE
1220 * Support the failure ioctls. If the command is a
1221 * read, and there are pending forced read errors, or
1222 * if a write and pending write errors, then fail this
1223 * operation with EIO. This is useful for testing
1224 * purposes. Also, support having every Nth read fail.
1226 * This is a rather blunt tool.
1228 if (bp->bio_cmd == BIO_READ) {
1229 if (softc->force_read_error) {
1230 softc->force_read_error--;
1233 if (softc->periodic_read_error > 0) {
1234 if (++softc->periodic_read_count >=
1235 softc->periodic_read_error) {
1236 softc->periodic_read_count = 0;
1241 if (softc->force_write_error) {
1242 softc->force_write_error--;
1247 bp->bio_error = EIO;
1248 bp->bio_flags |= BIO_ERROR;
1250 xpt_release_ccb(start_ccb);
1251 adaschedule(periph);
1255 cam_fill_ataio(ataio,
1258 bp->bio_cmd == BIO_READ ?
1259 CAM_DIR_IN : CAM_DIR_OUT,
1263 ada_default_timeout*1000);
1265 if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) {
1266 if (bp->bio_cmd == BIO_READ) {
1267 ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED,
1270 ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED,
1273 } else if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
1274 (lba + count >= ATA_MAX_28BIT_LBA ||
1276 if (softc->flags & ADA_FLAG_CAN_DMA) {
1277 if (bp->bio_cmd == BIO_READ) {
1278 ata_48bit_cmd(ataio, ATA_READ_DMA48,
1281 ata_48bit_cmd(ataio, ATA_WRITE_DMA48,
1285 if (bp->bio_cmd == BIO_READ) {
1286 ata_48bit_cmd(ataio, ATA_READ_MUL48,
1289 ata_48bit_cmd(ataio, ATA_WRITE_MUL48,
1296 if (softc->flags & ADA_FLAG_CAN_DMA) {
1297 if (bp->bio_cmd == BIO_READ) {
1298 ata_28bit_cmd(ataio, ATA_READ_DMA,
1301 ata_28bit_cmd(ataio, ATA_WRITE_DMA,
1305 if (bp->bio_cmd == BIO_READ) {
1306 ata_28bit_cmd(ataio, ATA_READ_MUL,
1309 ata_28bit_cmd(ataio, ATA_WRITE_MUL,
1318 uint64_t lba = bp->bio_pblkno;
1319 uint16_t count = bp->bio_bcount / softc->params.secsize;
1321 cam_fill_ataio(ataio,
1328 ada_default_timeout*1000);
1332 ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count);
1336 cam_fill_ataio(ataio,
1343 ada_default_timeout*1000);
1345 if (softc->flags & ADA_FLAG_CAN_48BIT)
1346 ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1348 ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0);
1351 start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO;
1353 start_ccb->ccb_h.ccb_bp = bp;
1354 softc->outstanding_cmds++;
1355 xpt_action(start_ccb);
1357 /* May have more work to do, so ensure we stay scheduled */
1358 adaschedule(periph);
1361 case ADA_STATE_RAHEAD:
1362 case ADA_STATE_WCACHE:
1364 if (softc->flags & ADA_FLAG_PACK_INVALID) {
1365 softc->state = ADA_STATE_NORMAL;
1366 xpt_release_ccb(start_ccb);
1367 cam_release_devq(periph->path,
1368 RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_DEV + 1, FALSE);
1369 adaschedule(periph);
1370 cam_periph_release_locked(periph);
1374 cam_fill_ataio(ataio,
1381 ada_default_timeout*1000);
1383 if (softc->state == ADA_STATE_RAHEAD) {
1384 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_RA ?
1385 ATA_SF_ENAB_RCACHE : ATA_SF_DIS_RCACHE, 0, 0);
1386 start_ccb->ccb_h.ccb_state = ADA_CCB_RAHEAD;
1388 ata_28bit_cmd(ataio, ATA_SETFEATURES, ADA_WC ?
1389 ATA_SF_ENAB_WCACHE : ATA_SF_DIS_WCACHE, 0, 0);
1390 start_ccb->ccb_h.ccb_state = ADA_CCB_WCACHE;
1392 xpt_action(start_ccb);
1399 adadone(struct cam_periph *periph, union ccb *done_ccb)
1401 struct ada_softc *softc;
1402 struct ccb_ataio *ataio;
1403 struct ccb_getdev *cgd;
1405 softc = (struct ada_softc *)periph->softc;
1406 ataio = &done_ccb->ataio;
1407 switch (ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) {
1408 case ADA_CCB_BUFFER_IO:
1413 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1414 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1417 error = adaerror(done_ccb, 0, 0);
1418 if (error == ERESTART) {
1419 /* A retry was scheduled, so just return. */
1423 if (error == ENXIO &&
1424 (softc->flags & ADA_FLAG_PACK_INVALID) == 0) {
1426 * Catastrophic error. Mark our pack as
1430 * XXX See if this is really a media
1433 xpt_print(periph->path,
1434 "Invalidating pack\n");
1435 softc->flags |= ADA_FLAG_PACK_INVALID;
1437 bp->bio_error = error;
1438 bp->bio_resid = bp->bio_bcount;
1439 bp->bio_flags |= BIO_ERROR;
1441 bp->bio_resid = ataio->resid;
1443 if (bp->bio_resid != 0)
1444 bp->bio_flags |= BIO_ERROR;
1446 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1447 cam_release_devq(done_ccb->ccb_h.path,
1451 /*getcount_only*/0);
1453 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1454 panic("REQ_CMP with QFRZN");
1455 bp->bio_resid = ataio->resid;
1456 if (ataio->resid > 0)
1457 bp->bio_flags |= BIO_ERROR;
1459 softc->outstanding_cmds--;
1460 if (softc->outstanding_cmds == 0)
1461 softc->flags |= ADA_FLAG_WENT_IDLE;
1462 if ((ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) ==
1464 struct trim_request *req =
1465 (struct trim_request *)ataio->data_ptr;
1468 for (i = 1; i < TRIM_MAX_BIOS && req->bps[i]; i++) {
1469 struct bio *bp1 = req->bps[i];
1471 bp1->bio_resid = bp->bio_resid;
1472 bp1->bio_error = bp->bio_error;
1473 if (bp->bio_flags & BIO_ERROR)
1474 bp1->bio_flags |= BIO_ERROR;
1477 softc->trim_running = 0;
1479 adaschedule(periph);
1484 case ADA_CCB_RAHEAD:
1486 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1487 if (adaerror(done_ccb, 0, 0) == ERESTART) {
1489 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1490 cam_release_devq(done_ccb->ccb_h.path,
1494 /*getcount_only*/0);
1499 * Since our peripheral may be invalidated by an error
1500 * above or an external event, we must release our CCB
1501 * before releasing the reference on the peripheral.
1502 * The peripheral will only go away once the last reference
1503 * is removed, and we need it around for the CCB release
1506 cgd = (struct ccb_getdev *)done_ccb;
1507 xpt_setup_ccb(&cgd->ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1508 cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1509 xpt_action((union ccb *)cgd);
1511 cgd->ident_data.support.command1 & ATA_SUPPORT_WRITECACHE) {
1512 softc->state = ADA_STATE_WCACHE;
1513 xpt_release_ccb(done_ccb);
1514 xpt_schedule(periph, CAM_PRIORITY_DEV);
1517 softc->state = ADA_STATE_NORMAL;
1518 xpt_release_ccb(done_ccb);
1519 cam_release_devq(periph->path,
1520 RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_DEV + 1, FALSE);
1521 adaschedule(periph);
1522 cam_periph_release_locked(periph);
1525 case ADA_CCB_WCACHE:
1527 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1528 if (adaerror(done_ccb, 0, 0) == ERESTART) {
1530 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1531 cam_release_devq(done_ccb->ccb_h.path,
1535 /*getcount_only*/0);
1539 softc->state = ADA_STATE_NORMAL;
1541 * Since our peripheral may be invalidated by an error
1542 * above or an external event, we must release our CCB
1543 * before releasing the reference on the peripheral.
1544 * The peripheral will only go away once the last reference
1545 * is removed, and we need it around for the CCB release
1548 xpt_release_ccb(done_ccb);
1549 cam_release_devq(periph->path,
1550 RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_DEV + 1, FALSE);
1551 adaschedule(periph);
1552 cam_periph_release_locked(periph);
1555 case ADA_CCB_WAITING:
1557 /* Caller will release the CCB */
1558 wakeup(&done_ccb->ccb_h.cbfcnp);
1562 /* No-op. We're polling */
1567 xpt_release_ccb(done_ccb);
1571 adaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1574 return(cam_periph_error(ccb, cam_flags, sense_flags, NULL));
1578 adagetparams(struct cam_periph *periph, struct ccb_getdev *cgd)
1580 struct ada_softc *softc = (struct ada_softc *)periph->softc;
1581 struct disk_params *dp = &softc->params;
1582 u_int64_t lbasize48;
1585 dp->secsize = ata_logical_sector_size(&cgd->ident_data);
1586 if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) &&
1587 cgd->ident_data.current_heads && cgd->ident_data.current_sectors) {
1588 dp->heads = cgd->ident_data.current_heads;
1589 dp->secs_per_track = cgd->ident_data.current_sectors;
1590 dp->cylinders = cgd->ident_data.cylinders;
1591 dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 |
1592 ((u_int32_t)cgd->ident_data.current_size_2 << 16);
1594 dp->heads = cgd->ident_data.heads;
1595 dp->secs_per_track = cgd->ident_data.sectors;
1596 dp->cylinders = cgd->ident_data.cylinders;
1597 dp->sectors = cgd->ident_data.cylinders * dp->heads * dp->secs_per_track;
1599 lbasize = (u_int32_t)cgd->ident_data.lba_size_1 |
1600 ((u_int32_t)cgd->ident_data.lba_size_2 << 16);
1602 /* use the 28bit LBA size if valid or bigger than the CHS mapping */
1603 if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize)
1604 dp->sectors = lbasize;
1606 /* use the 48bit LBA size if valid */
1607 lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) |
1608 ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) |
1609 ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) |
1610 ((u_int64_t)cgd->ident_data.lba_size48_4 << 48);
1611 if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) &&
1612 lbasize48 > ATA_MAX_28BIT_LBA)
1613 dp->sectors = lbasize48;
1617 adasendorderedtag(void *arg)
1619 struct ada_softc *softc = arg;
1621 if (ada_send_ordered) {
1622 if ((softc->ordered_tag_count == 0)
1623 && ((softc->flags & ADA_FLAG_WENT_IDLE) == 0)) {
1624 softc->flags |= ADA_FLAG_NEED_OTAG;
1626 if (softc->outstanding_cmds > 0)
1627 softc->flags &= ~ADA_FLAG_WENT_IDLE;
1629 softc->ordered_tag_count = 0;
1631 /* Queue us up again */
1632 callout_reset(&softc->sendordered_c,
1633 (ADA_DEFAULT_TIMEOUT * hz) / ADA_ORDEREDTAG_INTERVAL,
1634 adasendorderedtag, softc);
1638 * Step through all ADA peripheral drivers, and if the device is still open,
1639 * sync the disk cache to physical media.
1644 struct cam_periph *periph;
1645 struct ada_softc *softc;
1647 TAILQ_FOREACH(periph, &adadriver.units, unit_links) {
1650 /* If we paniced with lock held - not recurse here. */
1651 if (cam_periph_owned(periph))
1653 cam_periph_lock(periph);
1654 softc = (struct ada_softc *)periph->softc;
1656 * We only sync the cache if the drive is still open, and
1657 * if the drive is capable of it..
1659 if (((softc->flags & ADA_FLAG_OPEN) == 0) ||
1660 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) {
1661 cam_periph_unlock(periph);
1665 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1667 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
1668 cam_fill_ataio(&ccb.ataio,
1675 ada_default_timeout*1000);
1677 if (softc->flags & ADA_FLAG_CAN_48BIT)
1678 ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1680 ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0);
1681 xpt_polled_action(&ccb);
1683 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1684 xpt_print(periph->path, "Synchronize cache failed\n");
1686 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1687 cam_release_devq(ccb.ccb_h.path,
1691 /*getcount_only*/0);
1692 cam_periph_unlock(periph);
1697 adaspindown(uint8_t cmd, int flags)
1699 struct cam_periph *periph;
1700 struct ada_softc *softc;
1702 TAILQ_FOREACH(periph, &adadriver.units, unit_links) {
1705 /* If we paniced with lock held - not recurse here. */
1706 if (cam_periph_owned(periph))
1708 cam_periph_lock(periph);
1709 softc = (struct ada_softc *)periph->softc;
1711 * We only spin-down the drive if it is capable of it..
1713 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
1714 cam_periph_unlock(periph);
1719 xpt_print(periph->path, "spin-down\n");
1721 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1723 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
1724 cam_fill_ataio(&ccb.ataio,
1727 CAM_DIR_NONE | flags,
1731 ada_default_timeout*1000);
1733 ata_28bit_cmd(&ccb.ataio, cmd, 0, 0, 0);
1734 xpt_polled_action(&ccb);
1736 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1737 xpt_print(periph->path, "Spin-down disk failed\n");
1739 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1740 cam_release_devq(ccb.ccb_h.path,
1744 /*getcount_only*/0);
1745 cam_periph_unlock(periph);
1750 adashutdown(void *arg, int howto)
1754 if (ada_spindown_shutdown != 0 &&
1755 (howto & (RB_HALT | RB_POWEROFF)) != 0)
1756 adaspindown(ATA_STANDBY_IMMEDIATE, 0);
1760 adasuspend(void *arg)
1764 if (ada_spindown_suspend != 0)
1765 adaspindown(ATA_SLEEP, CAM_DEV_QFREEZE);
1769 adaresume(void *arg)
1771 struct cam_periph *periph;
1772 struct ada_softc *softc;
1774 if (ada_spindown_suspend == 0)
1777 TAILQ_FOREACH(periph, &adadriver.units, unit_links) {
1778 cam_periph_lock(periph);
1779 softc = (struct ada_softc *)periph->softc;
1781 * We only spin-down the drive if it is capable of it..
1783 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
1784 cam_periph_unlock(periph);
1789 xpt_print(periph->path, "resume\n");
1792 * Drop freeze taken due to CAM_DEV_QFREEZE flag set on
1795 cam_release_devq(periph->path,
1799 /*getcount_only*/0);
1801 cam_periph_unlock(periph);
1805 #endif /* _KERNEL */