2 * Copyright (c) 1998 - 2007 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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/malloc.h>
41 #include <sys/devicestat.h>
43 #include <sys/taskqueue.h>
45 #include <machine/bus.h>
46 #include <dev/ata/ata-all.h>
47 #include <dev/ata/atapi-tape.h>
50 /* device structure */
51 static d_open_t ast_open;
52 static d_close_t ast_close;
53 static d_ioctl_t ast_ioctl;
54 static d_strategy_t ast_strategy;
55 static struct cdevsw ast_cdevsw = {
56 .d_version = D_VERSION,
62 .d_strategy = ast_strategy,
64 .d_flags = D_TAPE | D_TRACKCLOSE,
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 if (!(stp = malloc(sizeof(struct ast_softc), M_AST, M_NOWAIT | M_ZERO))) {
110 device_printf(dev, "out of memory\n");
113 device_set_ivars(dev, stp);
114 ATA_SETMODE(device_get_parent(dev), dev);
116 if (ast_sense(dev)) {
117 device_set_ivars(dev, NULL);
121 if (!strcmp(atadev->param.model, "OnStream DI-30")) {
122 struct ast_transferpage transfer;
123 struct ast_identifypage identify;
125 stp->flags |= F_ONSTREAM;
126 bzero(&transfer, sizeof(struct ast_transferpage));
127 ast_mode_sense(dev, ATAPI_TAPE_TRANSFER_PAGE,
128 &transfer, sizeof(transfer));
129 bzero(&identify, sizeof(struct ast_identifypage));
130 ast_mode_sense(dev, ATAPI_TAPE_IDENTIFY_PAGE,
131 &identify, sizeof(identify));
132 strncpy(identify.ident, "FBSD", 4);
133 ast_mode_select(dev, &identify, sizeof(identify));
134 ast_read_position(dev, 0, &position);
137 stp->stats = devstat_new_entry("ast", device_get_unit(dev), DEV_BSIZE,
138 DEVSTAT_NO_ORDERED_TAGS,
139 DEVSTAT_TYPE_SEQUENTIAL | DEVSTAT_TYPE_IF_IDE,
140 DEVSTAT_PRIORITY_TAPE);
141 device = make_dev(&ast_cdevsw, 2 * device_get_unit(dev),
142 UID_ROOT, GID_OPERATOR, 0640, "ast%d",
143 device_get_unit(dev));
144 device->si_drv1 = dev;
146 device->si_iosize_max = ch->dma->max_iosize;
148 device->si_iosize_max = DFLTPHYS;
150 device = make_dev(&ast_cdevsw, 2 * device_get_unit(dev) + 1,
151 UID_ROOT, GID_OPERATOR, 0640, "nast%d",
152 device_get_unit(dev));
153 device->si_drv1 = dev;
155 device->si_iosize_max = ch->dma->max_iosize;
157 device->si_iosize_max = DFLTPHYS;
160 /* announce we are here and ready */
166 ast_detach(device_t dev)
168 struct ast_softc *stp = device_get_ivars(dev);
170 /* detroy devices from the system so we dont get any further requests */
171 destroy_dev(stp->dev1);
172 destroy_dev(stp->dev2);
174 /* fail requests on the queue and any thats "in flight" for this device */
175 ata_fail_requests(dev);
177 /* dont leave anything behind */
178 devstat_remove_entry(stp->stats);
179 device_set_ivars(dev, NULL);
185 ast_shutdown(device_t dev)
187 struct ata_device *atadev = device_get_softc(dev);
189 if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
190 ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
194 ast_reinit(device_t dev)
196 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
197 struct ata_device *atadev = device_get_softc(dev);
199 if (((atadev->unit == ATA_MASTER) && !(ch->devices & ATA_ATAPI_MASTER)) ||
200 ((atadev->unit == ATA_SLAVE) && !(ch->devices & ATA_ATAPI_SLAVE))) {
203 ATA_SETMODE(device_get_parent(dev), dev);
208 ast_open(struct cdev *cdev, int flags, int fmt, struct thread *td)
210 device_t dev = cdev->si_drv1;
211 struct ata_device *atadev = device_get_softc(dev);
212 struct ast_softc *stp = device_get_ivars(dev);
216 if (!device_is_attached(dev))
221 ast_prevent_allow(dev, 1);
223 device_printf(dev, "sense media type failed\n");
225 atadev->flags &= ~ATA_D_MEDIA_CHANGED;
226 stp->flags &= ~(F_DATA_WRITTEN | F_FM_WRITTEN);
232 ast_close(struct cdev *cdev, int flags, int fmt, struct thread *td)
234 device_t dev = cdev->si_drv1;
235 struct ast_softc *stp = device_get_ivars(dev);
237 /* flush buffers, some drives fail here, they should report ctl = 0 */
238 if (stp->cap.ctl && (stp->flags & F_DATA_WRITTEN))
239 ast_write_filemark(dev, 0);
241 /* write filemark if data written to tape */
242 if (!(stp->flags & F_ONSTREAM) &&
243 (stp->flags & (F_DATA_WRITTEN | F_FM_WRITTEN)) == F_DATA_WRITTEN)
244 ast_write_filemark(dev, ATAPI_WF_WRITE);
246 /* if minor is even rewind on close */
247 if (!(minor(cdev) & 0x01))
250 if (stp->cap.lock && count_dev(cdev) == 1)
251 ast_prevent_allow(dev, 0);
253 stp->flags &= ~F_CTL_WARN;
255 device_printf(dev, "%ju total bytes transferred\n", (uintmax_t)ast_total);
261 ast_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int flag, struct thread *td)
263 device_t dev = cdev->si_drv1;
264 struct ast_softc *stp = device_get_ivars(dev);
270 struct mtget *g = (struct mtget *) data;
272 bzero(g, sizeof(struct mtget));
275 g->mt_blksiz = stp->blksize;
276 g->mt_comp = stp->cap.compress;
277 g->mt_density0 = 0; g->mt_density1 = 0;
278 g->mt_density2 = 0; g->mt_density3 = 0;
279 g->mt_blksiz0 = 0; g->mt_blksiz1 = 0;
280 g->mt_blksiz2 = 0; g->mt_blksiz3 = 0;
281 g->mt_comp0 = 0; g->mt_comp1 = 0;
282 g->mt_comp2 = 0; g->mt_comp3 = 0;
289 struct mtop *mt = (struct mtop *)data;
291 switch ((int16_t) (mt->mt_op)) {
294 for (i=0; i < mt->mt_count && !error; i++)
295 error = ast_write_filemark(dev, ATAPI_WF_WRITE);
300 error = ast_space(dev, ATAPI_SP_FM, mt->mt_count);
305 error = ast_space(dev, ATAPI_SP_FM, -(mt->mt_count));
309 error = ast_rewind(dev);
313 error = ast_load_unload(dev, ATAPI_SS_EJECT);
317 error = ast_write_filemark(dev, 0);
321 error = ast_erase(dev);
325 error = ast_space(dev, ATAPI_SP_EOD, 0);
329 error = ast_load_unload(dev, ATAPI_SS_RETENSION|ATAPI_SS_LOAD);
347 struct ast_readposition position;
349 if ((error = ast_read_position(dev, 0, &position)))
351 *(u_int32_t *)data = position.tape;
357 struct ast_readposition position;
359 if ((error = ast_read_position(dev, 1, &position)))
361 *(u_int32_t *)data = position.tape;
366 error = ast_locate(dev, 0, *(u_int32_t *)data);
370 error = ast_locate(dev, 1, *(u_int32_t *)data);
374 error = ata_device_ioctl(dev, cmd, data);
380 ast_strategy(struct bio *bp)
382 device_t dev = bp->bio_dev->si_drv1;
383 struct ata_device *atadev = device_get_softc(dev);
384 struct ast_softc *stp = device_get_ivars(dev);
385 struct ata_request *request;
389 /* if it's a null transfer, return immediatly. */
390 if (bp->bio_bcount == 0) {
395 if (!(bp->bio_cmd == BIO_READ) && stp->flags & F_WRITEPROTECT) {
396 biofinish(bp, NULL, EPERM);
400 /* check for != blocksize requests */
401 if (bp->bio_bcount % stp->blksize) {
402 device_printf(dev, "transfers must be multiple of %d\n", stp->blksize);
403 biofinish(bp, NULL, EIO);
407 /* warn about transfers bigger than the device suggests */
408 if (bp->bio_bcount > stp->blksize * stp->cap.ctl) {
409 if ((stp->flags & F_CTL_WARN) == 0) {
410 device_printf(dev, "WARNING: CTL exceeded %ld>%d\n",
411 bp->bio_bcount, stp->blksize * stp->cap.ctl);
412 stp->flags |= F_CTL_WARN;
416 bzero(ccb, sizeof(ccb));
418 if (bp->bio_cmd == BIO_READ)
421 ccb[0] = ATAPI_WRITE;
423 blkcount = bp->bio_bcount / stp->blksize;
426 ccb[2] = blkcount >> 16;
427 ccb[3] = blkcount >> 8;
430 if (!(request = ata_alloc_request())) {
431 biofinish(bp, NULL, ENOMEM);
435 request->driver = bp;
436 bcopy(ccb, request->u.atapi.ccb,
437 (atadev->param.config & ATA_PROTO_MASK) ==
438 ATA_PROTO_ATAPI_12 ? 16 : 12);
439 request->data = bp->bio_data;
440 request->bytecount = blkcount * stp->blksize;
441 request->transfersize = min(request->bytecount, 65534);
442 request->timeout = (ccb[0] == ATAPI_WRITE_BIG) ? 180 : 120;
443 request->retries = 2;
444 request->callback = ast_done;
445 switch (bp->bio_cmd) {
447 request->flags |= (ATA_R_ATAPI | ATA_R_READ);
450 request->flags |= (ATA_R_ATAPI | ATA_R_WRITE);
453 device_printf(dev, "unknown BIO operation\n");
454 ata_free_request(request);
455 biofinish(bp, NULL, EIO);
458 devstat_start_transaction_bio(stp->stats, bp);
459 ata_queue_request(request);
463 ast_done(struct ata_request *request)
465 struct ast_softc *stp = device_get_ivars(request->dev);
466 struct bio *bp = request->driver;
468 /* finish up transfer */
469 if ((bp->bio_error = request->result))
470 bp->bio_flags |= BIO_ERROR;
471 if (bp->bio_cmd == BIO_WRITE)
472 stp->flags |= F_DATA_WRITTEN;
473 bp->bio_resid = bp->bio_bcount - request->donecount;
474 ast_total += (bp->bio_bcount - bp->bio_resid);
475 biofinish(bp, stp->stats, 0);
476 ata_free_request(request);
480 ast_sense(device_t dev)
482 struct ast_softc *stp = device_get_ivars(dev);
485 /* get drive capabilities, some bugridden drives needs this repeated */
486 for (count = 0 ; count < 5 ; count++) {
487 if (!ast_mode_sense(dev, ATAPI_TAPE_CAP_PAGE,
488 &stp->cap, sizeof(stp->cap)) &&
489 stp->cap.page_code == ATAPI_TAPE_CAP_PAGE) {
491 stp->blksize = 32768;
492 if (stp->cap.blk1024)
498 stp->cap.max_speed = ntohs(stp->cap.max_speed);
499 stp->cap.max_defects = ntohs(stp->cap.max_defects);
500 stp->cap.ctl = ntohs(stp->cap.ctl);
501 stp->cap.speed = ntohs(stp->cap.speed);
502 stp->cap.buffer_size = ntohs(stp->cap.buffer_size);
510 ast_mode_sense(device_t dev, int page, void *pagebuf, int pagesize)
512 int8_t ccb[16] = { ATAPI_MODE_SENSE, 0x08, page, pagesize>>8, pagesize,
513 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
516 error = ata_atapicmd(dev, ccb, pagebuf, pagesize, ATA_R_READ, 10);
521 ast_mode_select(device_t dev, void *pagebuf, int pagesize)
523 int8_t ccb[16] = { ATAPI_MODE_SELECT, 0x10, 0, pagesize>>8, pagesize,
524 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
526 return ata_atapicmd(dev, ccb, pagebuf, pagesize, 0, 10);
530 ast_write_filemark(device_t dev, u_int8_t function)
532 struct ast_softc *stp = device_get_ivars(dev);
533 int8_t ccb[16] = { ATAPI_WEOF, 0x01, 0, 0, function, 0, 0, 0,
534 0, 0, 0, 0, 0, 0, 0, 0 };
537 if (stp->flags & F_ONSTREAM)
538 ccb[4] = 0x00; /* only flush buffers supported */
541 if (stp->flags & F_FM_WRITTEN)
542 stp->flags &= ~F_DATA_WRITTEN;
544 stp->flags |= F_FM_WRITTEN;
547 error = ata_atapicmd(dev, ccb, NULL, 0, 0, 10);
550 return ast_wait_dsc(dev, 10*60);
554 ast_read_position(device_t dev, int hard, struct ast_readposition *position)
556 int8_t ccb[16] = { ATAPI_READ_POSITION, (hard ? 0x01 : 0), 0, 0, 0, 0, 0, 0,
557 0, 0, 0, 0, 0, 0, 0, 0 };
560 error = ata_atapicmd(dev, ccb, (caddr_t)position,
561 sizeof(struct ast_readposition), ATA_R_READ, 10);
562 position->tape = ntohl(position->tape);
563 position->host = ntohl(position->host);
568 ast_space(device_t dev, u_int8_t function, int32_t count)
570 int8_t ccb[16] = { ATAPI_SPACE, function, count>>16, count>>8, count,
571 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
573 return ata_atapicmd(dev, ccb, NULL, 0, 0, 60*60);
577 ast_locate(device_t dev, int hard, u_int32_t pos)
579 int8_t ccb[16] = { ATAPI_LOCATE, 0x01 | (hard ? 0x4 : 0), 0,
580 pos>>24, pos>>16, pos>>8, pos,
581 0, 0, 0, 0, 0, 0, 0, 0, 0 };
584 error = ata_atapicmd(dev, ccb, NULL, 0, 0, 10);
587 return ast_wait_dsc(dev, 60*60);
591 ast_prevent_allow(device_t dev, int lock)
593 int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
594 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
596 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
600 ast_load_unload(device_t dev, u_int8_t function)
602 struct ast_softc *stp = device_get_ivars(dev);
603 int8_t ccb[16] = { ATAPI_START_STOP, 0x01, 0, 0, function, 0, 0, 0,
604 0, 0, 0, 0, 0, 0, 0, 0 };
607 if ((function & ATAPI_SS_EJECT) && !stp->cap.eject)
609 error = ata_atapicmd(dev, ccb, NULL, 0, 0, 10);
612 pause("astlu", 1 * hz);
613 if (function == ATAPI_SS_EJECT)
615 return ast_wait_dsc(dev, 60*60);
619 ast_rewind(device_t dev)
621 int8_t ccb[16] = { ATAPI_REZERO, 0x01, 0, 0, 0, 0, 0, 0,
622 0, 0, 0, 0, 0, 0, 0, 0 };
625 error = ata_atapicmd(dev, ccb, NULL, 0, 0, 10);
628 return ast_wait_dsc(dev, 60*60);
632 ast_erase(device_t dev)
634 int8_t ccb[16] = { ATAPI_ERASE, 3, 0, 0, 0, 0, 0, 0,
635 0, 0, 0, 0, 0, 0, 0, 0 };
638 if ((error = ast_rewind(dev)))
641 return ata_atapicmd(dev, ccb, NULL, 0, 0, 60*60);
645 ast_test_ready(device_t dev)
647 int8_t ccb[16] = { ATAPI_TEST_UNIT_READY, 0, 0, 0, 0,
648 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
650 return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
654 ast_wait_dsc(device_t dev, int timeout)
657 int8_t ccb[16] = { ATAPI_POLL_DSC, 0, 0, 0, 0,
658 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
661 while (timeout > 0) {
662 error = ata_atapicmd(dev, ccb, NULL, 0, 0, 0);
665 pause("atpwt", hz / 2);
672 ast_describe(device_t dev)
674 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
675 struct ata_device *atadev = device_get_softc(dev);
676 struct ast_softc *stp = device_get_ivars(dev);
679 device_printf(dev, "<%.40s/%.8s> tape drive at ata%d as %s\n",
680 atadev->param.model, atadev->param.revision,
681 device_get_unit(ch->dev),
682 (atadev->unit == ATA_MASTER) ? "master" : "slave");
683 device_printf(dev, "%dKB/s, ", stp->cap.max_speed);
684 printf("transfer limit %d blk%s, ",
685 stp->cap.ctl, (stp->cap.ctl > 1) ? "s" : "");
686 printf("%dKB buffer, ", (stp->cap.buffer_size * DEV_BSIZE) / 1024);
687 printf("%s\n", ata_mode2str(atadev->mode));
688 device_printf(dev, "Medium: ");
689 switch (stp->cap.medium_type) {
691 printf("none"); break;
693 printf("Travan 1 (400 Mbyte)"); break;
695 printf("Travan 4 (4 Gbyte)"); break;
697 printf("OnStream ADR (15Gyte)"); break;
699 printf("unknown (0x%x)", stp->cap.medium_type);
701 if (stp->cap.readonly) printf(", readonly");
702 if (stp->cap.reverse) printf(", reverse");
703 if (stp->cap.eformat) printf(", eformat");
704 if (stp->cap.qfa) printf(", qfa");
705 if (stp->cap.lock) printf(", lock");
706 if (stp->cap.locked) printf(", locked");
707 if (stp->cap.prevent) printf(", prevent");
708 if (stp->cap.eject) printf(", eject");
709 if (stp->cap.disconnect) printf(", disconnect");
710 if (stp->cap.ecc) printf(", ecc");
711 if (stp->cap.compress) printf(", compress");
712 if (stp->cap.blk512) printf(", 512b");
713 if (stp->cap.blk1024) printf(", 1024b");
714 if (stp->cap.blk32k) printf(", 32kb");
718 device_printf(dev, "TAPE <%.40s/%.8s> at ata%d-%s %s\n",
719 atadev->param.model, atadev->param.revision,
720 device_get_unit(ch->dev),
721 (atadev->unit == ATA_MASTER) ? "master" : "slave",
722 ata_mode2str(atadev->mode));
726 static device_method_t ast_methods[] = {
727 /* device interface */
728 DEVMETHOD(device_probe, ast_probe),
729 DEVMETHOD(device_attach, ast_attach),
730 DEVMETHOD(device_detach, ast_detach),
731 DEVMETHOD(device_shutdown, ast_shutdown),
734 DEVMETHOD(ata_reinit, ast_reinit),
739 static driver_t ast_driver = {
745 static devclass_t ast_devclass;
747 DRIVER_MODULE(ast, ata, ast_driver, ast_devclass, NULL, NULL);
748 MODULE_VERSION(ast, 1);
749 MODULE_DEPEND(ast, ata, 1, 1, 1);