2 * Copyright (c) 1998 - 2006 Søren Schmidt <sos@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.
26 * $FreeBSD: src/sys/dev/ata/atapi-tape.c,v 1.101 2006/01/05 21:27:19 sos Exp $
27 * $DragonFly: src/sys/dev/disk/nata/atapi-tape.c,v 1.4 2008/08/30 02:56:11 dillon Exp $
32 #include <sys/param.h>
37 #include <sys/device.h>
38 #include <sys/devicestat.h>
40 #include <sys/kernel.h>
41 #include <sys/libkern.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
46 #include <sys/systm.h>
49 #include "atapi-tape.h"
52 /* device structure */
53 static d_open_t ast_open;
54 static d_close_t ast_close;
55 static d_ioctl_t ast_ioctl;
56 static d_strategy_t ast_strategy;
57 static struct dev_ops ast_ops = {
58 { "ast", 119, D_TAPE | D_TRACKCLOSE },
64 .d_strategy = ast_strategy
68 static int ast_sense(device_t);
69 static void ast_describe(device_t);
70 static void ast_done(struct ata_request *);
71 static int ast_mode_sense(device_t, int, void *, int);
72 static int ast_mode_select(device_t, void *, int);
73 static int ast_write_filemark(device_t, u_int8_t);
74 static int ast_read_position(device_t, int, struct ast_readposition *);
75 static int ast_space(device_t, u_int8_t, int32_t);
76 static int ast_locate(device_t, int, u_int32_t);
77 static int ast_prevent_allow(device_t, int);
78 static int ast_load_unload(device_t, u_int8_t);
79 static int ast_rewind(device_t);
80 static int ast_erase(device_t);
81 static int ast_test_ready(device_t);
82 static int ast_wait_dsc(device_t, int);
85 static u_int64_t ast_total = 0;
86 static MALLOC_DEFINE(M_AST, "ast_driver", "ATAPI tape driver buffers");
89 ast_probe(device_t dev)
91 struct ata_device *atadev = device_get_softc(dev);
93 if ((atadev->param.config & ATA_PROTO_ATAPI) &&
94 (atadev->param.config & ATA_ATAPI_TYPE_MASK) == ATA_ATAPI_TYPE_TAPE)
101 ast_attach(device_t dev)
103 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
104 struct ata_device *atadev = device_get_softc(dev);
105 struct ast_softc *stp;
106 struct ast_readposition position;
109 stp = kmalloc(sizeof(struct ast_softc), M_AST, M_WAITOK | M_ZERO);
110 device_set_ivars(dev, stp);
111 ATA_SETMODE(device_get_parent(dev), dev);
113 if (ast_sense(dev)) {
114 device_set_ivars(dev, NULL);
118 if (!strcmp(atadev->param.model, "OnStream DI-30")) {
119 struct ast_transferpage transfer;
120 struct ast_identifypage identify;
122 stp->flags |= F_ONSTREAM;
123 bzero(&transfer, sizeof(struct ast_transferpage));
124 ast_mode_sense(dev, ATAPI_TAPE_TRANSFER_PAGE,
125 &transfer, sizeof(transfer));
126 bzero(&identify, sizeof(struct ast_identifypage));
127 ast_mode_sense(dev, ATAPI_TAPE_IDENTIFY_PAGE,
128 &identify, sizeof(identify));
129 strncpy(identify.ident, "FBSD", 4);
130 ast_mode_select(dev, &identify, sizeof(identify));
131 ast_read_position(dev, 0, &position);
134 devstat_add_entry(&stp->stats, "ast", device_get_unit(dev), DEV_BSIZE,
135 DEVSTAT_NO_ORDERED_TAGS,
136 DEVSTAT_TYPE_SEQUENTIAL | DEVSTAT_TYPE_IF_IDE,
137 DEVSTAT_PRIORITY_TAPE);
138 cdev = make_dev(&ast_ops, 2 * device_get_unit(dev), UID_ROOT, GID_OPERATOR,
139 0640, "ast%d", device_get_unit(dev));
143 cdev->si_iosize_max = ch->dma->max_iosize;
145 cdev->si_iosize_max = DFLTPHYS;
147 cdev = make_dev(&ast_ops, 2 * device_get_unit(dev) + 1, UID_ROOT,
148 GID_OPERATOR, 0640, "nast%d", device_get_unit(dev));
152 cdev->si_iosize_max = ch->dma->max_iosize;
154 cdev->si_iosize_max = DFLTPHYS;
157 /* announce we are here and ready */
163 ast_detach(device_t dev)
165 struct ast_softc *stp = device_get_ivars(dev);
167 /* detroy devices from the system so we dont get any further requests */
168 destroy_dev(stp->cdev1);
169 destroy_dev(stp->cdev2);
171 /* fail requests on the queue and any thats "in flight" for this device */
172 ata_fail_requests(dev);
174 /* dont leave anything behind */
175 kprintf("devfs: Please check that only the right ata tape device was removed!!!\n");
176 dev_ops_remove_minor(&ast_ops, /*dkunitmask(), */dkmakeunit(device_get_unit(dev)));
177 devstat_remove_entry(&stp->stats);
178 device_set_ivars(dev, NULL);
184 ast_shutdown(device_t dev)
186 struct ata_device *atadev = device_get_softc(dev);
188 if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
189 ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
193 ast_reinit(device_t dev)
195 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
196 struct ata_device *atadev = device_get_softc(dev);
197 struct ast_softc *stp = device_get_ivars(dev);
199 if (((atadev->unit == ATA_MASTER) && !(ch->devices & ATA_ATAPI_MASTER)) ||
200 ((atadev->unit == ATA_SLAVE) && !(ch->devices & ATA_ATAPI_SLAVE))) {
201 device_set_ivars(dev, NULL);
205 ATA_SETMODE(device_get_parent(dev), dev);
210 ast_open(struct dev_open_args *ap)
212 device_t dev = ap->a_head.a_dev->si_drv1;
213 struct ata_device *atadev = device_get_softc(dev);
214 struct ast_softc *stp = device_get_ivars(dev);
218 if (!device_is_attached(dev))
223 ast_prevent_allow(dev, 1);
225 device_printf(dev, "sense media type failed\n");
227 atadev->flags &= ~ATA_D_MEDIA_CHANGED;
228 stp->flags &= ~(F_DATA_WRITTEN | F_FM_WRITTEN);
234 ast_close(struct dev_close_args *ap)
236 device_t dev = ap->a_head.a_dev->si_drv1;
237 struct ast_softc *stp = device_get_ivars(dev);
238 cdev_t cdev = ap->a_head.a_dev;
240 /* flush buffers, some drives fail here, they should report ctl = 0 */
241 if (stp->cap.ctl && (stp->flags & F_DATA_WRITTEN))
242 ast_write_filemark(dev, 0);
244 /* write filemark if data written to tape */
245 if (!(stp->flags & F_ONSTREAM) &&
246 (stp->flags & (F_DATA_WRITTEN | F_FM_WRITTEN)) == F_DATA_WRITTEN)
247 ast_write_filemark(dev, ATAPI_WF_WRITE);
249 /* if minor is even rewind on close */
250 if (!(minor(cdev) & 0x01))
253 if (stp->cap.lock && count_dev(cdev) == 1)
254 ast_prevent_allow(dev, 0);
256 stp->flags &= ~F_CTL_WARN;
258 device_printf(dev, "%llu total bytes transferred\n",
259 (unsigned long long)ast_total);
265 ast_ioctl(struct dev_ioctl_args *ap)
267 device_t dev = ap->a_head.a_dev->si_drv1;
268 struct ast_softc *stp = device_get_ivars(dev);
274 struct mtget *g = (struct mtget *)ap->a_data;
276 bzero(g, sizeof(struct mtget));
279 g->mt_blksiz = stp->blksize;
280 g->mt_comp = stp->cap.compress;
281 g->mt_density0 = 0; g->mt_density1 = 0;
282 g->mt_density2 = 0; g->mt_density3 = 0;
283 g->mt_blksiz0 = 0; g->mt_blksiz1 = 0;
284 g->mt_blksiz2 = 0; g->mt_blksiz3 = 0;
285 g->mt_comp0 = 0; g->mt_comp1 = 0;
286 g->mt_comp2 = 0; g->mt_comp3 = 0;
293 struct mtop *mt = (struct mtop *)ap->a_data;
295 switch ((int16_t) (mt->mt_op)) {
298 for (i=0; i < mt->mt_count && !error; i++)
299 error = ast_write_filemark(dev, ATAPI_WF_WRITE);
304 error = ast_space(dev, ATAPI_SP_FM, mt->mt_count);
309 error = ast_space(dev, ATAPI_SP_FM, -(mt->mt_count));
313 error = ast_rewind(dev);
317 error = ast_load_unload(dev, ATAPI_SS_EJECT);
321 error = ast_write_filemark(dev, 0);
325 error = ast_erase(dev);
329 error = ast_space(dev, ATAPI_SP_EOD, 0);
333 error = ast_load_unload(dev, ATAPI_SS_RETENSION|ATAPI_SS_LOAD);
351 struct ast_readposition position;
353 if ((error = ast_read_position(dev, 0, &position)))
355 *(u_int32_t *)ap->a_data = position.tape;
361 struct ast_readposition position;
363 if ((error = ast_read_position(dev, 1, &position)))
365 *(u_int32_t *)ap->a_data = position.tape;
370 error = ast_locate(dev, 0, *(u_int32_t *)ap->a_data);
374 error = ast_locate(dev, 1, *(u_int32_t *)ap->a_data);
378 error = ata_device_ioctl(dev, ap->a_cmd, ap->a_data);
384 ast_strategy(struct dev_strategy_args *ap)
386 device_t dev = ap->a_head.a_dev->si_drv1;
387 struct bio *bp = ap->a_bio;
388 struct buf *bbp = bp->bio_buf;
389 struct ata_device *atadev = device_get_softc(dev);
390 struct ast_softc *stp = device_get_ivars(dev);
391 struct ata_request *request;
395 /* if it's a null transfer, return immediatly. */
396 if (bbp->b_bcount == 0) {
401 if (!(bbp->b_cmd == BUF_CMD_READ) && (stp->flags & F_WRITEPROTECT)) {
402 bbp->b_flags |= B_ERROR;
403 bbp->b_error = EPERM;
407 if (bbp->b_cmd != BUF_CMD_READ && bbp->b_cmd != BUF_CMD_WRITE) {
408 bbp->b_flags |= B_ERROR;
414 /* check for != blocksize requests */
415 if (bbp->b_bcount % stp->blksize) {
416 device_printf(dev, "transfers must be multiple of %d\n", stp->blksize);
417 bbp->b_flags |= B_ERROR;
423 /* warn about transfers bigger than the device suggests */
424 if (bbp->b_bcount > stp->blksize * stp->cap.ctl) {
425 if ((stp->flags & F_CTL_WARN) == 0) {
426 device_printf(dev, "WARNING: CTL exceeded %d>%d\n",
427 bbp->b_bcount, stp->blksize * stp->cap.ctl);
428 stp->flags |= F_CTL_WARN;
432 bzero(ccb, sizeof(ccb));
434 if (bbp->b_cmd == BUF_CMD_READ)
437 ccb[0] = ATAPI_WRITE;
439 blkcount = bbp->b_bcount / stp->blksize;
442 ccb[2] = blkcount >> 16;
443 ccb[3] = blkcount >> 8;
446 if (!(request = ata_alloc_request())) {
447 bbp->b_flags |= B_ERROR;
448 bbp->b_error = ENOMEM;
454 bcopy(ccb, request->u.atapi.ccb,
455 (atadev->param.config & ATA_PROTO_MASK) ==
456 ATA_PROTO_ATAPI_12 ? 16 : 12);
457 request->data = bbp->b_data;
458 request->bytecount = blkcount * stp->blksize;
459 request->transfersize = min(request->bytecount, 65534);
460 request->timeout = (ccb[0] == ATAPI_WRITE_BIG) ? 180 : 120;
461 request->retries = 2;
462 request->callback = ast_done;
464 switch (bbp->b_cmd) {
466 request->flags |= (ATA_R_ATAPI | ATA_R_READ);
469 request->flags |= (ATA_R_ATAPI | ATA_R_WRITE);
474 devstat_start_transaction(&stp->stats);
475 ata_queue_request(request);
480 ast_done(struct ata_request *request)
482 struct ast_softc *stp = device_get_ivars(request->dev);
483 struct bio *bp = request->bio;
484 struct buf *bbp = bp->bio_buf;
486 /* finish up transfer */
487 if ((bbp->b_error = request->result))
488 bbp->b_flags |= B_ERROR;
489 if (bbp->b_cmd == BUF_CMD_WRITE)
490 stp->flags |= F_DATA_WRITTEN;
491 bbp->b_resid = bbp->b_bcount - request->donecount;
492 ast_total += (bbp->b_bcount - bbp->b_resid);
493 devstat_end_transaction_buf(&stp->stats, bbp);
495 ata_free_request(request);
499 ast_sense(device_t dev)
501 struct ast_softc *stp = device_get_ivars(dev);
504 /* get drive capabilities, some bugridden drives needs this repeated */
505 for (count = 0 ; count < 5 ; count++) {
506 if (!ast_mode_sense(dev, ATAPI_TAPE_CAP_PAGE,
507 &stp->cap, sizeof(stp->cap)) &&
508 stp->cap.page_code == ATAPI_TAPE_CAP_PAGE) {
510 stp->blksize = 32768;
511 if (stp->cap.blk1024)
517 stp->cap.max_speed = ntohs(stp->cap.max_speed);
518 stp->cap.max_defects = ntohs(stp->cap.max_defects);
519 stp->cap.ctl = ntohs(stp->cap.ctl);
520 stp->cap.speed = ntohs(stp->cap.speed);
521 stp->cap.buffer_size = ntohs(stp->cap.buffer_size);
529 ast_mode_sense(device_t dev, int page, void *pagebuf, int pagesize)
531 int8_t ccb[16] = { ATAPI_MODE_SENSE, 0x08, page, pagesize>>8, pagesize,
532 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
535 error = ata_atapicmd(dev, ccb, pagebuf, pagesize, ATA_R_READ, 10);
540 ast_mode_select(device_t dev, void *pagebuf, int pagesize)
542 int8_t ccb[16] = { ATAPI_MODE_SELECT, 0x10, 0, pagesize>>8, pagesize,
543 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
545 return ata_atapicmd(dev, ccb, pagebuf, pagesize, 0, 10);
549 ast_write_filemark(device_t dev, u_int8_t function)
551 struct ast_softc *stp = device_get_ivars(dev);
552 int8_t ccb[16] = { ATAPI_WEOF, 0x01, 0, 0, function, 0, 0, 0,
553 0, 0, 0, 0, 0, 0, 0, 0 };
556 if (stp->flags & F_ONSTREAM)
557 ccb[4] = 0x00; /* only flush buffers supported */
560 if (stp->flags & F_FM_WRITTEN)
561 stp->flags &= ~F_DATA_WRITTEN;
563 stp->flags |= F_FM_WRITTEN;
566 error = ata_atapicmd(dev, ccb, NULL, 0, 0, 10);
569 return ast_wait_dsc(dev, 10*60);
573 ast_read_position(device_t dev, int hard, struct ast_readposition *position)
575 int8_t ccb[16] = { ATAPI_READ_POSITION, (hard ? 0x01 : 0), 0, 0, 0, 0, 0, 0,
576 0, 0, 0, 0, 0, 0, 0, 0 };
579 error = ata_atapicmd(dev, ccb, (caddr_t)position,
580 sizeof(struct ast_readposition), ATA_R_READ, 10);
581 position->tape = ntohl(position->tape);
582 position->host = ntohl(position->host);
587 ast_space(device_t dev, u_int8_t function, int32_t count)
589 int8_t ccb[16] = { ATAPI_SPACE, function, count>>16, count>>8, count,
590 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
592 return ata_atapicmd(dev, ccb, NULL, 0, 0, 60*60);
596 ast_locate(device_t dev, int hard, u_int32_t pos)
598 int8_t ccb[16] = { ATAPI_LOCATE, 0x01 | (hard ? 0x4 : 0), 0,
599 pos>>24, pos>>16, pos>>8, pos,
600 0, 0, 0, 0, 0, 0, 0, 0, 0 };
603 error = ata_atapicmd(dev, ccb, NULL, 0, 0, 10);
606 return ast_wait_dsc(dev, 60*60);
610 ast_prevent_allow(device_t dev, int lock)
612 int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
613 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
615 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
619 ast_load_unload(device_t dev, u_int8_t function)
621 struct ast_softc *stp = device_get_ivars(dev);
622 int8_t ccb[16] = { ATAPI_START_STOP, 0x01, 0, 0, function, 0, 0, 0,
623 0, 0, 0, 0, 0, 0, 0, 0 };
626 if ((function & ATAPI_SS_EJECT) && !stp->cap.eject)
628 error = ata_atapicmd(dev, ccb, NULL, 0, 0, 10);
631 tsleep((caddr_t)&error, 0, "astlu", 1 * hz);
632 if (function == ATAPI_SS_EJECT)
634 return ast_wait_dsc(dev, 60*60);
638 ast_rewind(device_t dev)
640 int8_t ccb[16] = { ATAPI_REZERO, 0x01, 0, 0, 0, 0, 0, 0,
641 0, 0, 0, 0, 0, 0, 0, 0 };
644 error = ata_atapicmd(dev, ccb, NULL, 0, 0, 10);
647 return ast_wait_dsc(dev, 60*60);
651 ast_erase(device_t dev)
653 int8_t ccb[16] = { ATAPI_ERASE, 3, 0, 0, 0, 0, 0, 0,
654 0, 0, 0, 0, 0, 0, 0, 0 };
657 if ((error = ast_rewind(dev)))
660 return ata_atapicmd(dev, ccb, NULL, 0, 0, 60*60);
664 ast_test_ready(device_t dev)
666 int8_t ccb[16] = { ATAPI_TEST_UNIT_READY, 0, 0, 0, 0,
667 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
669 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
673 ast_wait_dsc(device_t dev, int timeout)
676 int8_t ccb[16] = { ATAPI_POLL_DSC, 0, 0, 0, 0,
677 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
680 while (timeout > 0) {
681 error = ata_atapicmd(dev, ccb, NULL, 0, 0, 0);
684 tsleep(&error, 0, "atpwt", hz / 2);
691 ast_describe(device_t dev)
693 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
694 struct ata_device *atadev = device_get_softc(dev);
695 struct ast_softc *stp = device_get_ivars(dev);
698 device_printf(dev, "<%.40s/%.8s> tape drive at ata%d as %s\n",
699 atadev->param.model, atadev->param.revision,
700 device_get_unit(ch->dev),
701 (atadev->unit == ATA_MASTER) ? "master" : "slave");
702 device_printf(dev, "%dKB/s, ", stp->cap.max_speed);
703 kprintf("transfer limit %d blk%s, ",
704 stp->cap.ctl, (stp->cap.ctl > 1) ? "s" : "");
705 kprintf("%dKB buffer, ", (stp->cap.buffer_size * DEV_BSIZE) / 1024);
706 kprintf("%s\n", ata_mode2str(atadev->mode));
707 device_printf(dev, "Medium: ");
708 switch (stp->cap.medium_type) {
710 kprintf("none"); break;
712 kprintf("Travan 1 (400 Mbyte)"); break;
714 kprintf("Travan 4 (4 Gbyte)"); break;
716 kprintf("OnStream ADR (15Gyte)"); break;
718 kprintf("unknown (0x%x)", stp->cap.medium_type);
720 if (stp->cap.readonly) kprintf(", readonly");
721 if (stp->cap.reverse) kprintf(", reverse");
722 if (stp->cap.eformat) kprintf(", eformat");
723 if (stp->cap.qfa) kprintf(", qfa");
724 if (stp->cap.lock) kprintf(", lock");
725 if (stp->cap.locked) kprintf(", locked");
726 if (stp->cap.prevent) kprintf(", prevent");
727 if (stp->cap.eject) kprintf(", eject");
728 if (stp->cap.disconnect) kprintf(", disconnect");
729 if (stp->cap.ecc) kprintf(", ecc");
730 if (stp->cap.compress) kprintf(", compress");
731 if (stp->cap.blk512) kprintf(", 512b");
732 if (stp->cap.blk1024) kprintf(", 1024b");
733 if (stp->cap.blk32k) kprintf(", 32kb");
737 device_printf(dev, "TAPE <%.40s/%.8s> at ata%d-%s %s\n",
738 atadev->param.model, atadev->param.revision,
739 device_get_unit(ch->dev),
740 (atadev->unit == ATA_MASTER) ? "master" : "slave",
741 ata_mode2str(atadev->mode));
745 static device_method_t ast_methods[] = {
746 /* device interface */
747 DEVMETHOD(device_probe, ast_probe),
748 DEVMETHOD(device_attach, ast_attach),
749 DEVMETHOD(device_detach, ast_detach),
750 DEVMETHOD(device_shutdown, ast_shutdown),
753 DEVMETHOD(ata_reinit, ast_reinit),
758 static driver_t ast_driver = {
764 static devclass_t ast_devclass;
766 DRIVER_MODULE(ast, ata, ast_driver, ast_devclass, NULL, NULL);
767 MODULE_VERSION(ast, 1);
768 MODULE_DEPEND(ast, ata, 1, 1, 1);