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$");
30 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
36 #include <sys/sysctl.h>
37 #include <sys/taskqueue.h>
39 #include <sys/mutex.h>
41 #include <sys/devicestat.h>
42 #include <sys/eventhandler.h>
43 #include <sys/malloc.h>
45 #include <geom/geom_disk.h>
54 #include <cam/cam_ccb.h>
55 #include <cam/cam_periph.h>
56 #include <cam/cam_xpt_periph.h>
57 #include <cam/cam_sim.h>
59 #include <cam/ata/ata_all.h>
61 #include <machine/md_var.h> /* geometry translation */
65 #define ATA_MAX_28BIT_LBA 268435455UL
72 ADA_FLAG_PACK_INVALID = 0x001,
73 ADA_FLAG_CAN_48BIT = 0x002,
74 ADA_FLAG_CAN_FLUSHCACHE = 0x004,
75 ADA_FLAG_CAN_NCQ = 0x008,
76 ADA_FLAG_CAN_DMA = 0x010,
77 ADA_FLAG_NEED_OTAG = 0x020,
78 ADA_FLAG_WENT_IDLE = 0x040,
79 ADA_FLAG_CAN_TRIM = 0x080,
80 ADA_FLAG_OPEN = 0x100,
81 ADA_FLAG_SCTX_INIT = 0x200,
82 ADA_FLAG_CAN_CFA = 0x400
90 ADA_CCB_BUFFER_IO = 0x03,
91 ADA_CCB_WAITING = 0x04,
94 ADA_CCB_TYPE_MASK = 0x0F,
97 /* Offsets into our private area for storing information */
98 #define ccb_state ppriv_field0
99 #define ccb_bp ppriv_ptr1
103 u_int8_t secs_per_track;
105 u_int32_t secsize; /* Number of bytes/logical sector */
106 u_int64_t sectors; /* Total number sectors */
109 #define TRIM_MAX_BLOCKS 4
110 #define TRIM_MAX_RANGES TRIM_MAX_BLOCKS * 64
111 struct trim_request {
112 uint8_t data[TRIM_MAX_RANGES * 8];
113 struct bio *bps[TRIM_MAX_RANGES];
117 struct bio_queue_head bio_queue;
118 struct bio_queue_head trim_queue;
122 int ordered_tag_count;
123 int outstanding_cmds;
126 struct disk_params params;
128 struct task sysctl_task;
129 struct sysctl_ctx_list sysctl_ctx;
130 struct sysctl_oid *sysctl_tree;
131 struct callout sendordered_c;
132 struct trim_request trim_req;
135 struct ada_quirk_entry {
136 struct scsi_inquiry_pattern inq_pat;
140 static struct ada_quirk_entry ada_quirk_table[] =
145 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
146 /*vendor*/"*", /*product*/"*", /*revision*/"*"
152 static disk_strategy_t adastrategy;
153 static dumper_t adadump;
154 static periph_init_t adainit;
155 static void adaasync(void *callback_arg, u_int32_t code,
156 struct cam_path *path, void *arg);
157 static void adasysctlinit(void *context, int pending);
158 static periph_ctor_t adaregister;
159 static periph_dtor_t adacleanup;
160 static periph_start_t adastart;
161 static periph_oninv_t adaoninvalidate;
162 static void adadone(struct cam_periph *periph,
163 union ccb *done_ccb);
164 static int adaerror(union ccb *ccb, u_int32_t cam_flags,
165 u_int32_t sense_flags);
166 static void adagetparams(struct cam_periph *periph,
167 struct ccb_getdev *cgd);
168 static timeout_t adasendorderedtag;
169 static void adashutdown(void *arg, int howto);
171 #ifndef ADA_DEFAULT_TIMEOUT
172 #define ADA_DEFAULT_TIMEOUT 30 /* Timeout in seconds */
175 #ifndef ADA_DEFAULT_RETRY
176 #define ADA_DEFAULT_RETRY 4
179 #ifndef ADA_DEFAULT_SEND_ORDERED
180 #define ADA_DEFAULT_SEND_ORDERED 1
184 * Most platforms map firmware geometry to actual, but some don't. If
185 * not overridden, default to nothing.
187 #ifndef ata_disk_firmware_geom_adjust
188 #define ata_disk_firmware_geom_adjust(disk)
191 static int ada_retry_count = ADA_DEFAULT_RETRY;
192 static int ada_default_timeout = ADA_DEFAULT_TIMEOUT;
193 static int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED;
195 SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD, 0,
196 "CAM Direct Access Disk driver");
197 SYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RW,
198 &ada_retry_count, 0, "Normal I/O retry count");
199 TUNABLE_INT("kern.cam.ada.retry_count", &ada_retry_count);
200 SYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RW,
201 &ada_default_timeout, 0, "Normal I/O timeout (in seconds)");
202 TUNABLE_INT("kern.cam.ada.default_timeout", &ada_default_timeout);
203 SYSCTL_INT(_kern_cam_ada, OID_AUTO, ada_send_ordered, CTLFLAG_RW,
204 &ada_send_ordered, 0, "Send Ordered Tags");
205 TUNABLE_INT("kern.cam.ada.ada_send_ordered", &ada_send_ordered);
208 * ADA_ORDEREDTAG_INTERVAL determines how often, relative
209 * to the default timeout, we check to see whether an ordered
210 * tagged transaction is appropriate to prevent simple tag
211 * starvation. Since we'd like to ensure that there is at least
212 * 1/2 of the timeout length left for a starved transaction to
213 * complete after we've sent an ordered tag, we must poll at least
214 * four times in every timeout period. This takes care of the worst
215 * case where a starved transaction starts during an interval that
216 * meets the requirement "don't send an ordered tag" test so it takes
217 * us two intervals to determine that a tag must be sent.
219 #ifndef ADA_ORDEREDTAG_INTERVAL
220 #define ADA_ORDEREDTAG_INTERVAL 4
223 static struct periph_driver adadriver =
226 TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0
229 PERIPHDRIVER_DECLARE(ada, adadriver);
231 MALLOC_DEFINE(M_ATADA, "ata_da", "ata_da buffers");
234 adaopen(struct disk *dp)
236 struct cam_periph *periph;
237 struct ada_softc *softc;
241 periph = (struct cam_periph *)dp->d_drv1;
242 if (periph == NULL) {
246 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
250 cam_periph_lock(periph);
251 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
252 cam_periph_unlock(periph);
253 cam_periph_release(periph);
257 unit = periph->unit_number;
258 softc = (struct ada_softc *)periph->softc;
259 softc->flags |= ADA_FLAG_OPEN;
261 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
262 ("adaopen: disk=%s%d (unit %d)\n", dp->d_name, dp->d_unit,
265 if ((softc->flags & ADA_FLAG_PACK_INVALID) != 0) {
266 /* Invalidate our pack information. */
267 softc->flags &= ~ADA_FLAG_PACK_INVALID;
270 cam_periph_unhold(periph);
271 cam_periph_unlock(periph);
276 adaclose(struct disk *dp)
278 struct cam_periph *periph;
279 struct ada_softc *softc;
283 periph = (struct cam_periph *)dp->d_drv1;
287 cam_periph_lock(periph);
288 if ((error = cam_periph_hold(periph, PRIBIO)) != 0) {
289 cam_periph_unlock(periph);
290 cam_periph_release(periph);
294 softc = (struct ada_softc *)periph->softc;
295 /* We only sync the cache if the drive is capable of it. */
296 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) {
298 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
299 cam_fill_ataio(&ccb->ataio,
306 ada_default_timeout*1000);
308 if (softc->flags & ADA_FLAG_CAN_48BIT)
309 ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
311 ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
312 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
313 /*sense_flags*/0, softc->disk->d_devstat);
315 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
316 xpt_print(periph->path, "Synchronize cache failed\n");
318 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
319 cam_release_devq(ccb->ccb_h.path,
324 xpt_release_ccb(ccb);
327 softc->flags &= ~ADA_FLAG_OPEN;
328 cam_periph_unhold(periph);
329 cam_periph_unlock(periph);
330 cam_periph_release(periph);
335 adaschedule(struct cam_periph *periph)
337 struct ada_softc *softc = (struct ada_softc *)periph->softc;
339 if (bioq_first(&softc->bio_queue) ||
340 (!softc->trim_running && bioq_first(&softc->trim_queue))) {
341 /* Have more work to do, so ensure we stay scheduled */
342 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
347 * Actually translate the requested transfer into one the physical driver
348 * can understand. The transfer is described by a buf and will include
349 * only one physical transfer.
352 adastrategy(struct bio *bp)
354 struct cam_periph *periph;
355 struct ada_softc *softc;
357 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
358 if (periph == NULL) {
359 biofinish(bp, NULL, ENXIO);
362 softc = (struct ada_softc *)periph->softc;
364 cam_periph_lock(periph);
367 * If the device has been made invalid, error out
369 if ((softc->flags & ADA_FLAG_PACK_INVALID)) {
370 cam_periph_unlock(periph);
371 biofinish(bp, NULL, ENXIO);
376 * Place it in the queue of disk activities for this disk
378 if (bp->bio_cmd == BIO_DELETE &&
379 (softc->flags & ADA_FLAG_CAN_TRIM))
380 bioq_disksort(&softc->trim_queue, bp);
382 bioq_disksort(&softc->bio_queue, bp);
385 * Schedule ourselves for performing the work.
388 cam_periph_unlock(periph);
394 adadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
396 struct cam_periph *periph;
397 struct ada_softc *softc;
408 softc = (struct ada_softc *)periph->softc;
409 cam_periph_lock(periph);
410 secsize = softc->params.secsize;
411 lba = offset / secsize;
412 count = length / secsize;
414 if ((softc->flags & ADA_FLAG_PACK_INVALID) != 0) {
415 cam_periph_unlock(periph);
420 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
421 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
422 cam_fill_ataio(&ccb.ataio,
427 (u_int8_t *) virtual,
429 ada_default_timeout*1000);
430 if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
431 (lba + count >= ATA_MAX_28BIT_LBA ||
433 ata_48bit_cmd(&ccb.ataio, ATA_WRITE_DMA48,
436 ata_28bit_cmd(&ccb.ataio, ATA_WRITE_DMA,
439 xpt_polled_action(&ccb);
441 if ((ccb.ataio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
442 printf("Aborting dump due to I/O error.\n");
443 cam_periph_unlock(periph);
446 cam_periph_unlock(periph);
450 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) {
451 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
453 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
454 cam_fill_ataio(&ccb.ataio,
461 ada_default_timeout*1000);
463 if (softc->flags & ADA_FLAG_CAN_48BIT)
464 ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0);
466 ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0);
467 xpt_polled_action(&ccb);
469 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
470 xpt_print(periph->path, "Synchronize cache failed\n");
472 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
473 cam_release_devq(ccb.ccb_h.path,
479 cam_periph_unlock(periph);
489 * Install a global async callback. This callback will
490 * receive async callbacks like "new device found".
492 status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL);
494 if (status != CAM_REQ_CMP) {
495 printf("ada: Failed to attach master async callback "
496 "due to status 0x%x!\n", status);
497 } else if (ada_send_ordered) {
499 /* Register our shutdown event handler */
500 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown,
501 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
502 printf("adainit: shutdown event registration failed!\n");
507 adaoninvalidate(struct cam_periph *periph)
509 struct ada_softc *softc;
511 softc = (struct ada_softc *)periph->softc;
514 * De-register any async callbacks.
516 xpt_register_async(0, adaasync, periph, periph->path);
518 softc->flags |= ADA_FLAG_PACK_INVALID;
521 * Return all queued I/O with ENXIO.
522 * XXX Handle any transactions queued to the card
523 * with XPT_ABORT_CCB.
525 bioq_flush(&softc->bio_queue, NULL, ENXIO);
526 bioq_flush(&softc->trim_queue, NULL, ENXIO);
528 disk_gone(softc->disk);
529 xpt_print(periph->path, "lost device\n");
533 adacleanup(struct cam_periph *periph)
535 struct ada_softc *softc;
537 softc = (struct ada_softc *)periph->softc;
539 xpt_print(periph->path, "removing device entry\n");
540 cam_periph_unlock(periph);
543 * If we can't free the sysctl tree, oh well...
545 if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0
546 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
547 xpt_print(periph->path, "can't remove sysctl context\n");
550 disk_destroy(softc->disk);
551 callout_drain(&softc->sendordered_c);
552 free(softc, M_DEVBUF);
553 cam_periph_lock(periph);
557 adaasync(void *callback_arg, u_int32_t code,
558 struct cam_path *path, void *arg)
560 struct cam_periph *periph;
562 periph = (struct cam_periph *)callback_arg;
564 case AC_FOUND_DEVICE:
566 struct ccb_getdev *cgd;
569 cgd = (struct ccb_getdev *)arg;
573 if (cgd->protocol != PROTO_ATA)
577 * Allocate a peripheral instance for
578 * this device and start the probe
581 status = cam_periph_alloc(adaregister, adaoninvalidate,
582 adacleanup, adastart,
583 "ada", CAM_PERIPH_BIO,
584 cgd->ccb_h.path, adaasync,
585 AC_FOUND_DEVICE, cgd);
587 if (status != CAM_REQ_CMP
588 && status != CAM_REQ_INPROG)
589 printf("adaasync: Unable to attach to new device "
590 "due to status 0x%x\n", status);
594 cam_periph_async(periph, code, path, arg);
600 adasysctlinit(void *context, int pending)
602 struct cam_periph *periph;
603 struct ada_softc *softc;
604 char tmpstr[80], tmpstr2[80];
606 periph = (struct cam_periph *)context;
607 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
610 softc = (struct ada_softc *)periph->softc;
611 snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d", periph->unit_number);
612 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
614 sysctl_ctx_init(&softc->sysctl_ctx);
615 softc->flags |= ADA_FLAG_SCTX_INIT;
616 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
617 SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2,
618 CTLFLAG_RD, 0, tmpstr);
619 if (softc->sysctl_tree == NULL) {
620 printf("adasysctlinit: unable to allocate sysctl tree\n");
621 cam_periph_release(periph);
625 cam_periph_release(periph);
629 adaregister(struct cam_periph *periph, void *arg)
631 struct ada_softc *softc;
632 struct ccb_pathinq cpi;
633 struct ccb_getdev *cgd;
634 char announce_buf[80];
635 struct disk_params *dp;
639 cgd = (struct ccb_getdev *)arg;
640 if (periph == NULL) {
641 printf("adaregister: periph was NULL!!\n");
642 return(CAM_REQ_CMP_ERR);
646 printf("adaregister: no getdev CCB, can't register device\n");
647 return(CAM_REQ_CMP_ERR);
650 softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF,
654 printf("adaregister: Unable to probe new device. "
655 "Unable to allocate softc\n");
656 return(CAM_REQ_CMP_ERR);
659 bioq_init(&softc->bio_queue);
660 bioq_init(&softc->trim_queue);
662 if (cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA)
663 softc->flags |= ADA_FLAG_CAN_DMA;
664 if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48)
665 softc->flags |= ADA_FLAG_CAN_48BIT;
666 if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE)
667 softc->flags |= ADA_FLAG_CAN_FLUSHCACHE;
668 if (cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ &&
669 cgd->inq_flags & SID_CmdQue)
670 softc->flags |= ADA_FLAG_CAN_NCQ;
671 if (cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) {
672 softc->flags |= ADA_FLAG_CAN_TRIM;
673 softc->trim_max_ranges = TRIM_MAX_RANGES;
674 if (cgd->ident_data.max_dsm_blocks != 0) {
675 softc->trim_max_ranges =
676 min(cgd->ident_data.max_dsm_blocks * 64,
677 softc->trim_max_ranges);
680 if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA)
681 softc->flags |= ADA_FLAG_CAN_CFA;
682 softc->state = ADA_STATE_NORMAL;
684 periph->softc = softc;
687 * See if this device has any quirks.
689 match = cam_quirkmatch((caddr_t)&cgd->ident_data,
690 (caddr_t)ada_quirk_table,
691 sizeof(ada_quirk_table)/sizeof(*ada_quirk_table),
692 sizeof(*ada_quirk_table), ata_identify_match);
694 softc->quirks = ((struct ada_quirk_entry *)match)->quirks;
696 softc->quirks = ADA_Q_NONE;
698 bzero(&cpi, sizeof(cpi));
699 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE);
700 cpi.ccb_h.func_code = XPT_PATH_INQ;
701 xpt_action((union ccb *)&cpi);
703 TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph);
706 * Register this media as a disk
708 mtx_unlock(periph->sim->mtx);
709 adagetparams(periph, cgd);
710 softc->disk = disk_alloc();
711 softc->disk->d_open = adaopen;
712 softc->disk->d_close = adaclose;
713 softc->disk->d_strategy = adastrategy;
714 softc->disk->d_dump = adadump;
715 softc->disk->d_name = "ada";
716 softc->disk->d_drv1 = periph;
717 maxio = cpi.maxio; /* Honor max I/O size of SIM */
719 maxio = DFLTPHYS; /* traditional default */
720 else if (maxio > MAXPHYS)
721 maxio = MAXPHYS; /* for safety */
722 if (softc->flags & ADA_FLAG_CAN_48BIT)
723 maxio = min(maxio, 65536 * softc->params.secsize);
724 else /* 28bit ATA command limit */
725 maxio = min(maxio, 256 * softc->params.secsize);
726 softc->disk->d_maxsize = maxio;
727 softc->disk->d_unit = periph->unit_number;
728 softc->disk->d_flags = 0;
729 if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE)
730 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
731 if ((softc->flags & ADA_FLAG_CAN_TRIM) ||
732 ((softc->flags & ADA_FLAG_CAN_CFA) &&
733 !(softc->flags & ADA_FLAG_CAN_48BIT)))
734 softc->disk->d_flags |= DISKFLAG_CANDELETE;
735 strlcpy(softc->disk->d_ident, cgd->serial_num,
736 MIN(sizeof(softc->disk->d_ident), cgd->serial_num_len + 1));
738 softc->disk->d_sectorsize = softc->params.secsize;
739 softc->disk->d_mediasize = (off_t)softc->params.sectors *
740 softc->params.secsize;
741 if (ata_physical_sector_size(&cgd->ident_data) !=
742 softc->params.secsize) {
743 softc->disk->d_stripesize =
744 ata_physical_sector_size(&cgd->ident_data);
745 softc->disk->d_stripeoffset = (softc->disk->d_stripesize -
746 ata_logical_sector_offset(&cgd->ident_data)) %
747 softc->disk->d_stripesize;
749 softc->disk->d_fwsectors = softc->params.secs_per_track;
750 softc->disk->d_fwheads = softc->params.heads;
751 ata_disk_firmware_geom_adjust(softc->disk);
753 disk_create(softc->disk, DISK_VERSION);
754 mtx_lock(periph->sim->mtx);
757 snprintf(announce_buf, sizeof(announce_buf),
758 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
759 (uintmax_t)(((uintmax_t)dp->secsize *
760 dp->sectors) / (1024*1024)),
761 (uintmax_t)dp->sectors,
762 dp->secsize, dp->heads,
763 dp->secs_per_track, dp->cylinders);
764 xpt_announce_periph(periph, announce_buf);
766 * Add async callbacks for bus reset and
767 * bus device reset calls. I don't bother
768 * checking if this fails as, in most cases,
769 * the system will function just fine without
770 * them and the only alternative would be to
771 * not attach the device on failure.
773 xpt_register_async(AC_LOST_DEVICE,
774 adaasync, periph, periph->path);
777 * Schedule a periodic event to occasionally send an
778 * ordered tag to a device.
780 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
781 callout_reset(&softc->sendordered_c,
782 (ADA_DEFAULT_TIMEOUT * hz) / ADA_ORDEREDTAG_INTERVAL,
783 adasendorderedtag, softc);
789 adastart(struct cam_periph *periph, union ccb *start_ccb)
791 struct ada_softc *softc = (struct ada_softc *)periph->softc;
792 struct ccb_ataio *ataio = &start_ccb->ataio;
794 switch (softc->state) {
795 case ADA_STATE_NORMAL:
800 /* Execute immediate CCB if waiting. */
801 if (periph->immediate_priority <= periph->pinfo.priority) {
802 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
803 ("queuing for immediate ccb\n"));
804 start_ccb->ccb_h.ccb_state = ADA_CCB_WAITING;
805 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
807 periph->immediate_priority = CAM_PRIORITY_NONE;
808 wakeup(&periph->ccb_list);
809 /* Have more work to do, so ensure we stay scheduled */
813 /* Run TRIM if not running yet. */
814 if (!softc->trim_running &&
815 (bp = bioq_first(&softc->trim_queue)) != 0) {
816 struct trim_request *req = &softc->trim_req;
818 int bps = 0, ranges = 0;
820 softc->trim_running = 1;
821 bzero(req, sizeof(*req));
824 uint64_t lba = bp1->bio_pblkno;
825 int count = bp1->bio_bcount /
826 softc->params.secsize;
828 bioq_remove(&softc->trim_queue, bp1);
830 int c = min(count, 0xffff);
831 int off = ranges * 8;
833 req->data[off + 0] = lba & 0xff;
834 req->data[off + 1] = (lba >> 8) & 0xff;
835 req->data[off + 2] = (lba >> 16) & 0xff;
836 req->data[off + 3] = (lba >> 24) & 0xff;
837 req->data[off + 4] = (lba >> 32) & 0xff;
838 req->data[off + 5] = (lba >> 40) & 0xff;
839 req->data[off + 6] = c & 0xff;
840 req->data[off + 7] = (c >> 8) & 0xff;
845 req->bps[bps++] = bp1;
846 bp1 = bioq_first(&softc->trim_queue);
848 bp1->bio_bcount / softc->params.secsize >
849 (softc->trim_max_ranges - ranges) * 0xffff)
852 cam_fill_ataio(ataio,
858 ((ranges + 63) / 64) * 512,
859 ada_default_timeout * 1000);
860 ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT,
861 ATA_DSM_TRIM, 0, (ranges + 63) / 64);
862 start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM;
865 /* Run regular command. */
866 bp = bioq_first(&softc->bio_queue);
868 xpt_release_ccb(start_ccb);
871 bioq_remove(&softc->bio_queue, bp);
873 if ((softc->flags & ADA_FLAG_NEED_OTAG) != 0) {
874 softc->flags &= ~ADA_FLAG_NEED_OTAG;
875 softc->ordered_tag_count++;
880 switch (bp->bio_cmd) {
884 uint64_t lba = bp->bio_pblkno;
885 uint16_t count = bp->bio_bcount / softc->params.secsize;
887 cam_fill_ataio(ataio,
890 bp->bio_cmd == BIO_READ ?
891 CAM_DIR_IN : CAM_DIR_OUT,
895 ada_default_timeout*1000);
897 if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) {
898 if (bp->bio_cmd == BIO_READ) {
899 ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED,
902 ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED,
905 } else if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
906 (lba + count >= ATA_MAX_28BIT_LBA ||
908 if (softc->flags & ADA_FLAG_CAN_DMA) {
909 if (bp->bio_cmd == BIO_READ) {
910 ata_48bit_cmd(ataio, ATA_READ_DMA48,
913 ata_48bit_cmd(ataio, ATA_WRITE_DMA48,
917 if (bp->bio_cmd == BIO_READ) {
918 ata_48bit_cmd(ataio, ATA_READ_MUL48,
921 ata_48bit_cmd(ataio, ATA_WRITE_MUL48,
928 if (softc->flags & ADA_FLAG_CAN_DMA) {
929 if (bp->bio_cmd == BIO_READ) {
930 ata_28bit_cmd(ataio, ATA_READ_DMA,
933 ata_28bit_cmd(ataio, ATA_WRITE_DMA,
937 if (bp->bio_cmd == BIO_READ) {
938 ata_28bit_cmd(ataio, ATA_READ_MUL,
941 ata_28bit_cmd(ataio, ATA_WRITE_MUL,
950 uint64_t lba = bp->bio_pblkno;
951 uint16_t count = bp->bio_bcount / softc->params.secsize;
953 cam_fill_ataio(ataio,
960 ada_default_timeout*1000);
964 ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count);
968 cam_fill_ataio(ataio,
975 ada_default_timeout*1000);
977 if (softc->flags & ADA_FLAG_CAN_48BIT)
978 ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0);
980 ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0);
983 start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO;
985 start_ccb->ccb_h.ccb_bp = bp;
986 softc->outstanding_cmds++;
987 xpt_action(start_ccb);
989 /* May have more work to do, so ensure we stay scheduled */
997 adadone(struct cam_periph *periph, union ccb *done_ccb)
999 struct ada_softc *softc;
1000 struct ccb_ataio *ataio;
1002 softc = (struct ada_softc *)periph->softc;
1003 ataio = &done_ccb->ataio;
1004 switch (ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) {
1005 case ADA_CCB_BUFFER_IO:
1010 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1011 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1014 error = adaerror(done_ccb, 0, 0);
1015 if (error == ERESTART) {
1016 /* A retry was scheduled, so just return. */
1020 if (error == ENXIO) {
1022 * Catastrophic error. Mark our pack as
1026 * XXX See if this is really a media
1029 xpt_print(periph->path,
1030 "Invalidating pack\n");
1031 softc->flags |= ADA_FLAG_PACK_INVALID;
1033 bp->bio_error = error;
1034 bp->bio_resid = bp->bio_bcount;
1035 bp->bio_flags |= BIO_ERROR;
1037 bp->bio_resid = ataio->resid;
1039 if (bp->bio_resid != 0)
1040 bp->bio_flags |= BIO_ERROR;
1042 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1043 cam_release_devq(done_ccb->ccb_h.path,
1047 /*getcount_only*/0);
1049 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1050 panic("REQ_CMP with QFRZN");
1051 bp->bio_resid = ataio->resid;
1052 if (ataio->resid > 0)
1053 bp->bio_flags |= BIO_ERROR;
1055 softc->outstanding_cmds--;
1056 if (softc->outstanding_cmds == 0)
1057 softc->flags |= ADA_FLAG_WENT_IDLE;
1058 if ((ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) ==
1060 struct trim_request *req =
1061 (struct trim_request *)ataio->data_ptr;
1064 for (i = 1; i < softc->trim_max_ranges &&
1066 struct bio *bp1 = req->bps[i];
1068 bp1->bio_resid = bp->bio_resid;
1069 bp1->bio_error = bp->bio_error;
1070 if (bp->bio_flags & BIO_ERROR)
1071 bp1->bio_flags |= BIO_ERROR;
1074 softc->trim_running = 0;
1076 adaschedule(periph);
1081 case ADA_CCB_WAITING:
1083 /* Caller will release the CCB */
1084 wakeup(&done_ccb->ccb_h.cbfcnp);
1088 /* No-op. We're polling */
1093 xpt_release_ccb(done_ccb);
1097 adaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1099 struct ada_softc *softc;
1100 struct cam_periph *periph;
1102 periph = xpt_path_periph(ccb->ccb_h.path);
1103 softc = (struct ada_softc *)periph->softc;
1105 return(cam_periph_error(ccb, cam_flags, sense_flags, NULL));
1109 adagetparams(struct cam_periph *periph, struct ccb_getdev *cgd)
1111 struct ada_softc *softc = (struct ada_softc *)periph->softc;
1112 struct disk_params *dp = &softc->params;
1113 u_int64_t lbasize48;
1116 dp->secsize = ata_logical_sector_size(&cgd->ident_data);
1117 if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) &&
1118 cgd->ident_data.current_heads && cgd->ident_data.current_sectors) {
1119 dp->heads = cgd->ident_data.current_heads;
1120 dp->secs_per_track = cgd->ident_data.current_sectors;
1121 dp->cylinders = cgd->ident_data.cylinders;
1122 dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 |
1123 ((u_int32_t)cgd->ident_data.current_size_2 << 16);
1125 dp->heads = cgd->ident_data.heads;
1126 dp->secs_per_track = cgd->ident_data.sectors;
1127 dp->cylinders = cgd->ident_data.cylinders;
1128 dp->sectors = cgd->ident_data.cylinders * dp->heads * dp->secs_per_track;
1130 lbasize = (u_int32_t)cgd->ident_data.lba_size_1 |
1131 ((u_int32_t)cgd->ident_data.lba_size_2 << 16);
1133 /* use the 28bit LBA size if valid or bigger than the CHS mapping */
1134 if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize)
1135 dp->sectors = lbasize;
1137 /* use the 48bit LBA size if valid */
1138 lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) |
1139 ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) |
1140 ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) |
1141 ((u_int64_t)cgd->ident_data.lba_size48_4 << 48);
1142 if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) &&
1143 lbasize48 > ATA_MAX_28BIT_LBA)
1144 dp->sectors = lbasize48;
1148 adasendorderedtag(void *arg)
1150 struct ada_softc *softc = arg;
1152 if (ada_send_ordered) {
1153 if ((softc->ordered_tag_count == 0)
1154 && ((softc->flags & ADA_FLAG_WENT_IDLE) == 0)) {
1155 softc->flags |= ADA_FLAG_NEED_OTAG;
1157 if (softc->outstanding_cmds > 0)
1158 softc->flags &= ~ADA_FLAG_WENT_IDLE;
1160 softc->ordered_tag_count = 0;
1162 /* Queue us up again */
1163 callout_reset(&softc->sendordered_c,
1164 (ADA_DEFAULT_TIMEOUT * hz) / ADA_ORDEREDTAG_INTERVAL,
1165 adasendorderedtag, softc);
1169 * Step through all ADA peripheral drivers, and if the device is still open,
1170 * sync the disk cache to physical media.
1173 adashutdown(void * arg, int howto)
1175 struct cam_periph *periph;
1176 struct ada_softc *softc;
1178 TAILQ_FOREACH(periph, &adadriver.units, unit_links) {
1181 /* If we paniced with lock held - not recurse here. */
1182 if (cam_periph_owned(periph))
1184 cam_periph_lock(periph);
1185 softc = (struct ada_softc *)periph->softc;
1187 * We only sync the cache if the drive is still open, and
1188 * if the drive is capable of it..
1190 if (((softc->flags & ADA_FLAG_OPEN) == 0) ||
1191 (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) {
1192 cam_periph_unlock(periph);
1196 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1198 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
1199 cam_fill_ataio(&ccb.ataio,
1206 ada_default_timeout*1000);
1208 if (softc->flags & ADA_FLAG_CAN_48BIT)
1209 ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1211 ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0);
1212 xpt_polled_action(&ccb);
1214 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1215 xpt_print(periph->path, "Synchronize cache failed\n");
1217 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1218 cam_release_devq(ccb.ccb_h.path,
1222 /*getcount_only*/0);
1223 cam_periph_unlock(periph);
1227 #endif /* _KERNEL */