2 * Copyright (c) 1998,1999,2000,2001,2002 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.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * $FreeBSD: src/sys/dev/ata/atapi-tape.c,v 1.36.2.12 2002/07/31 11:19:26 sos Exp $
29 * $DragonFly: src/sys/dev/disk/ata/atapi-tape.c,v 1.22 2008/01/06 16:55:49 swildner Exp $
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
42 #include <sys/diskslice.h>
43 #include <sys/devicestat.h>
46 #include <sys/thread2.h>
49 #include "atapi-all.h"
50 #include "atapi-tape.h"
52 /* device structures */
53 static d_open_t astopen;
54 static d_close_t astclose;
55 static d_ioctl_t astioctl;
56 static d_strategy_t aststrategy;
58 static struct dev_ops ast_ops = {
59 { "ast", 119, D_TAPE | D_TRACKCLOSE },
65 .d_strategy = aststrategy
69 static int ast_sense(struct ast_softc *);
70 static void ast_describe(struct ast_softc *);
71 static int ast_done(struct atapi_request *);
72 static int ast_mode_sense(struct ast_softc *, int, void *, int);
73 static int ast_mode_select(struct ast_softc *, void *, int);
74 static int ast_write_filemark(struct ast_softc *, u_int8_t);
75 static int ast_read_position(struct ast_softc *, int, struct ast_readposition *);
76 static int ast_space(struct ast_softc *, u_int8_t, int32_t);
77 static int ast_locate(struct ast_softc *, int, u_int32_t);
78 static int ast_prevent_allow(struct ast_softc *stp, int);
79 static int ast_load_unload(struct ast_softc *, u_int8_t);
80 static int ast_rewind(struct ast_softc *);
81 static int ast_erase(struct ast_softc *);
84 static u_int32_t ast_lun_map = 0;
85 static u_int64_t ast_total = 0;
86 static MALLOC_DEFINE(M_AST, "AST driver", "ATAPI tape driver buffers");
89 astattach(struct ata_device *atadev)
91 struct ast_softc *stp;
92 struct ast_readposition position;
95 stp = kmalloc(sizeof(struct ast_softc), M_AST, M_WAITOK | M_ZERO);
98 stp->lun = ata_get_lun(&ast_lun_map);
99 ata_set_name(atadev, "ast", stp->lun);
100 bioq_init(&stp->bio_queue);
102 if (ast_sense(stp)) {
107 if (!strcmp(atadev->param->model, "OnStream DI-30")) {
108 struct ast_transferpage transfer;
109 struct ast_identifypage identify;
111 stp->flags |= F_ONSTREAM;
112 bzero(&transfer, sizeof(struct ast_transferpage));
113 ast_mode_sense(stp, ATAPI_TAPE_TRANSFER_PAGE,
114 &transfer, sizeof(transfer));
115 bzero(&identify, sizeof(struct ast_identifypage));
116 ast_mode_sense(stp, ATAPI_TAPE_IDENTIFY_PAGE,
117 &identify, sizeof(identify));
118 strncpy(identify.ident, "FBSD", 4);
119 ast_mode_select(stp, &identify, sizeof(identify));
120 ast_read_position(stp, 0, &position);
123 devstat_add_entry(&stp->stats, "ast", stp->lun, DEV_BSIZE,
124 DEVSTAT_NO_ORDERED_TAGS,
125 DEVSTAT_TYPE_SEQUENTIAL | DEVSTAT_TYPE_IF_IDE,
126 DEVSTAT_PRIORITY_TAPE);
127 dev_ops_add(&ast_ops, dkunitmask(), dkmakeunit(stp->lun));
128 dev = make_dev(&ast_ops, dkmakeminor(stp->lun, 0, 0),
129 UID_ROOT, GID_OPERATOR, 0640, "ast%d", stp->lun);
131 dev->si_iosize_max = 256 * DEV_BSIZE;
132 dev = make_dev(&ast_ops, dkmakeminor(stp->lun, 0, 1),
133 UID_ROOT, GID_OPERATOR, 0640, "nast%d", stp->lun);
135 dev->si_iosize_max = 256 * DEV_BSIZE;
136 stp->device->flags |= ATA_D_MEDIA_CHANGED;
138 atadev->driver = stp;
143 astdetach(struct ata_device *atadev)
145 struct ast_softc *stp = atadev->driver;
149 while ((bio = bioq_first(&stp->bio_queue))) {
150 bioq_remove(&stp->bio_queue, bio);
152 bp->b_flags |= B_ERROR;
156 devstat_remove_entry(&stp->stats);
157 kprintf("devfs: Please check that only the right atapi-tape device was removed!!\n");
158 dev_ops_remove_minor(&ast_ops,/* dkunitmask(), */dkmakeunit(stp->lun));
159 ata_free_name(atadev);
160 ata_free_lun(&ast_lun_map, stp->lun);
162 atadev->driver = NULL;
166 ast_sense(struct ast_softc *stp)
168 int count, error = 0;
170 /* get drive capabilities, some drives needs this repeated */
171 for (count = 0 ; count < 5 ; count++) {
172 if (!(error = ast_mode_sense(stp, ATAPI_TAPE_CAP_PAGE,
173 &stp->cap, sizeof(stp->cap)))) {
175 stp->blksize = 32768;
176 if (stp->cap.blk1024)
182 stp->cap.max_speed = ntohs(stp->cap.max_speed);
183 stp->cap.max_defects = ntohs(stp->cap.max_defects);
184 stp->cap.ctl = ntohs(stp->cap.ctl);
185 stp->cap.speed = ntohs(stp->cap.speed);
186 stp->cap.buffer_size = ntohs(stp->cap.buffer_size);
194 ast_describe(struct ast_softc *stp)
197 ata_prtdev(stp->device, "<%.40s/%.8s> tape drive at ata%d as %s\n",
198 stp->device->param->model, stp->device->param->revision,
199 device_get_unit(stp->device->channel->dev),
200 (stp->device->unit == ATA_MASTER) ? "master" : "slave");
201 ata_prtdev(stp->device, "%dKB/s, ", stp->cap.max_speed);
202 kprintf("transfer limit %d blk%s, ",
203 stp->cap.ctl, (stp->cap.ctl > 1) ? "s" : "");
204 kprintf("%dKB buffer, ", (stp->cap.buffer_size * DEV_BSIZE) / 1024);
205 kprintf("%s\n", ata_mode2str(stp->device->mode));
206 ata_prtdev(stp->device, "Medium: ");
207 switch (stp->cap.medium_type) {
209 kprintf("none"); break;
211 kprintf("Travan 1 (400 Mbyte)"); break;
213 kprintf("Travan 4 (4 Gbyte)"); break;
215 kprintf("OnStream ADR (15Gyte)"); break;
217 kprintf("unknown (0x%x)", stp->cap.medium_type);
219 if (stp->cap.readonly) kprintf(", readonly");
220 if (stp->cap.reverse) kprintf(", reverse");
221 if (stp->cap.eformat) kprintf(", eformat");
222 if (stp->cap.qfa) kprintf(", qfa");
223 if (stp->cap.lock) kprintf(", lock");
224 if (stp->cap.locked) kprintf(", locked");
225 if (stp->cap.prevent) kprintf(", prevent");
226 if (stp->cap.eject) kprintf(", eject");
227 if (stp->cap.disconnect) kprintf(", disconnect");
228 if (stp->cap.ecc) kprintf(", ecc");
229 if (stp->cap.compress) kprintf(", compress");
230 if (stp->cap.blk512) kprintf(", 512b");
231 if (stp->cap.blk1024) kprintf(", 1024b");
232 if (stp->cap.blk32k) kprintf(", 32kb");
236 ata_prtdev(stp->device, "TAPE <%.40s> at ata%d-%s %s\n",
237 stp->device->param->model,
238 device_get_unit(stp->device->channel->dev),
239 (stp->device->unit == ATA_MASTER) ? "master" : "slave",
240 ata_mode2str(stp->device->mode));
245 astopen(struct dev_open_args *ap)
247 cdev_t dev = ap->a_head.a_dev;
248 struct ast_softc *stp = dev->si_drv1;
253 if (count_dev(dev) > 1)
256 atapi_test_ready(stp->device);
259 ast_prevent_allow(stp, 1);
262 ata_prtdev(stp->device, "sense media type failed\n");
264 stp->device->flags &= ~ATA_D_MEDIA_CHANGED;
265 stp->flags &= ~(F_DATA_WRITTEN | F_FM_WRITTEN);
271 astclose(struct dev_close_args *ap)
273 cdev_t dev = ap->a_head.a_dev;
274 struct ast_softc *stp = dev->si_drv1;
276 /* flush buffers, some drives fail here, they should report ctl = 0 */
277 if (stp->cap.ctl && (stp->flags & F_DATA_WRITTEN))
278 ast_write_filemark(stp, 0);
280 /* write filemark if data written to tape */
281 if (!(stp->flags & F_ONSTREAM) &&
282 (stp->flags & (F_DATA_WRITTEN | F_FM_WRITTEN)) == F_DATA_WRITTEN)
283 ast_write_filemark(stp, WF_WRITE);
285 /* if minor is even rewind on close */
286 if (!(minor(dev) & 0x01))
289 if (stp->cap.lock && count_dev(dev) == 1)
290 ast_prevent_allow(stp, 0);
292 stp->flags &= F_CTL_WARN;
294 ata_prtdev(stp->device, "%llu total bytes transferred\n", ast_total);
300 astioctl(struct dev_ioctl_args *ap)
302 cdev_t dev = ap->a_head.a_dev;
303 struct ast_softc *stp = dev->si_drv1;
309 struct mtget *g = (struct mtget *) ap->a_data;
311 bzero(g, sizeof(struct mtget));
314 g->mt_blksiz = stp->blksize;
315 g->mt_comp = stp->cap.compress;
316 g->mt_density0 = 0; g->mt_density1 = 0;
317 g->mt_density2 = 0; g->mt_density3 = 0;
318 g->mt_blksiz0 = 0; g->mt_blksiz1 = 0;
319 g->mt_blksiz2 = 0; g->mt_blksiz3 = 0;
320 g->mt_comp0 = 0; g->mt_comp1 = 0;
321 g->mt_comp2 = 0; g->mt_comp3 = 0;
327 struct mtop *mt = (struct mtop *)ap->a_data;
329 switch ((int16_t) (mt->mt_op)) {
332 for (i=0; i < mt->mt_count && !error; i++)
333 error = ast_write_filemark(stp, WF_WRITE);
338 error = ast_space(stp, SP_FM, mt->mt_count);
343 error = ast_space(stp, SP_FM, -(mt->mt_count));
347 error = ast_rewind(stp);
351 error = ast_load_unload(stp, SS_EJECT);
355 error = ast_write_filemark(stp, 0);
359 error = ast_erase(stp);
363 error = ast_space(stp, SP_EOD, 0);
367 error = ast_load_unload(stp, SS_RETENSION | SS_LOAD);
384 struct ast_readposition position;
386 if ((error = ast_read_position(stp, 0, &position)))
388 *(u_int32_t *)ap->a_data = position.tape;
393 struct ast_readposition position;
395 if ((error = ast_read_position(stp, 1, &position)))
397 *(u_int32_t *)ap->a_data = position.tape;
401 error = ast_locate(stp, 0, *(u_int32_t *)ap->a_data);
404 error = ast_locate(stp, 1, *(u_int32_t *)ap->a_data);
413 aststrategy(struct dev_strategy_args *ap)
415 cdev_t dev = ap->a_head.a_dev;
416 struct bio *bio = ap->a_bio;
417 struct buf *bp = bio->bio_buf;
418 struct ast_softc *stp = dev->si_drv1;
420 if (stp->device->flags & ATA_D_DETACHING) {
421 bp->b_flags |= B_ERROR;
427 /* if it's a null transfer, return immediatly. */
428 if (bp->b_bcount == 0) {
433 if (bp->b_cmd != BUF_CMD_READ && (stp->flags & F_WRITEPROTECT)) {
434 bp->b_flags |= B_ERROR;
440 /* check for != blocksize requests */
441 if (bp->b_bcount % stp->blksize) {
442 ata_prtdev(stp->device, "transfers must be multiple of %d\n",
444 bp->b_flags |= B_ERROR;
450 /* warn about transfers bigger than the device suggests */
451 if (bp->b_bcount > stp->blksize * stp->cap.ctl) {
452 if ((stp->flags & F_CTL_WARN) == 0) {
453 ata_prtdev(stp->device, "WARNING: CTL exceeded %d > %d\n",
454 bp->b_bcount, stp->blksize * stp->cap.ctl);
455 stp->flags |= F_CTL_WARN;
460 bioq_insert_tail(&stp->bio_queue, bio);
462 ata_start(stp->device->channel);
467 ast_start(struct ata_device *atadev)
469 struct ast_softc *stp = atadev->driver;
470 struct bio *bio = bioq_first(&stp->bio_queue);
477 bzero(ccb, sizeof(ccb));
480 if (bp->b_cmd == BUF_CMD_READ)
483 ccb[0] = ATAPI_WRITE;
485 bioq_remove(&stp->bio_queue, bio);
486 blkcount = bp->b_bcount / stp->blksize;
489 ccb[2] = blkcount>>16;
490 ccb[3] = blkcount>>8;
493 devstat_start_transaction(&stp->stats);
495 atapi_queue_cmd(stp->device, ccb, bp->b_data, blkcount * stp->blksize,
496 ((bp->b_cmd == BUF_CMD_READ) ? ATPR_F_READ : 0),
501 ast_done(struct atapi_request *request)
503 struct bio *bio = request->driver;
504 struct buf *bp = bio->bio_buf;
505 struct ast_softc *stp = request->device->driver;
507 if (request->error) {
508 bp->b_error = request->error;
509 bp->b_flags |= B_ERROR;
511 if (bp->b_cmd != BUF_CMD_READ)
512 stp->flags |= F_DATA_WRITTEN;
513 bp->b_resid = bp->b_bcount - request->donecount;
514 ast_total += (bp->b_bcount - bp->b_resid);
516 devstat_end_transaction_buf(&stp->stats, bp);
522 ast_mode_sense(struct ast_softc *stp, int page, void *pagebuf, int pagesize)
524 int8_t ccb[16] = { ATAPI_MODE_SENSE, 0x08, page, pagesize>>8, pagesize,
525 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
528 error = atapi_queue_cmd(stp->device, ccb, pagebuf, pagesize, ATPR_F_READ,
531 atapi_dump("ast: mode sense ", pagebuf, pagesize);
537 ast_mode_select(struct ast_softc *stp, void *pagebuf, int pagesize)
539 int8_t ccb[16] = { ATAPI_MODE_SELECT, 0x10, 0, pagesize>>8, pagesize,
540 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
543 ata_prtdev(stp->device, "modeselect pagesize=%d\n", pagesize);
544 atapi_dump("mode select ", pagebuf, pagesize);
546 return atapi_queue_cmd(stp->device, ccb, pagebuf, pagesize, 0,
551 ast_write_filemark(struct ast_softc *stp, u_int8_t function)
553 int8_t ccb[16] = { ATAPI_WEOF, 0x01, 0, 0, function, 0, 0, 0,
554 0, 0, 0, 0, 0, 0, 0, 0 };
557 if (stp->flags & F_ONSTREAM)
558 ccb[4] = 0x00; /* only flush buffers supported */
561 if (stp->flags & F_FM_WRITTEN)
562 stp->flags &= ~F_DATA_WRITTEN;
564 stp->flags |= F_FM_WRITTEN;
567 error = atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
570 return atapi_wait_dsc(stp->device, 10*60);
574 ast_read_position(struct ast_softc *stp, int hard,
575 struct ast_readposition *position)
577 int8_t ccb[16] = { ATAPI_READ_POSITION, (hard ? 0x01 : 0), 0, 0, 0, 0, 0, 0,
578 0, 0, 0, 0, 0, 0, 0, 0 };
581 error = atapi_queue_cmd(stp->device, ccb, (caddr_t)position,
582 sizeof(struct ast_readposition), ATPR_F_READ, 10,
584 position->tape = ntohl(position->tape);
585 position->host = ntohl(position->host);
590 ast_space(struct ast_softc *stp, u_int8_t function, int32_t count)
592 int8_t ccb[16] = { ATAPI_SPACE, function, count>>16, count>>8, count,
593 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
595 return atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 60*60, NULL, NULL);
599 ast_locate(struct ast_softc *stp, int hard, u_int32_t pos)
601 int8_t ccb[16] = { ATAPI_LOCATE, 0x01 | (hard ? 0x4 : 0), 0,
602 pos>>24, pos>>16, pos>>8, pos,
603 0, 0, 0, 0, 0, 0, 0, 0, 0 };
606 error = atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
609 return atapi_wait_dsc(stp->device, 60*60);
613 ast_prevent_allow(struct ast_softc *stp, int lock)
615 int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
616 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
618 return atapi_queue_cmd(stp->device, ccb, NULL, 0, 0,30, NULL, NULL);
622 ast_load_unload(struct ast_softc *stp, u_int8_t function)
624 int8_t ccb[16] = { ATAPI_START_STOP, 0x01, 0, 0, function, 0, 0, 0,
625 0, 0, 0, 0, 0, 0, 0, 0 };
628 if ((function & SS_EJECT) && !stp->cap.eject)
630 error = atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
633 tsleep((caddr_t)&error, 0, "astlu", 1 * hz);
634 if (function == SS_EJECT)
636 return atapi_wait_dsc(stp->device, 60*60);
640 ast_rewind(struct ast_softc *stp)
642 int8_t ccb[16] = { ATAPI_REZERO, 0x01, 0, 0, 0, 0, 0, 0,
643 0, 0, 0, 0, 0, 0, 0, 0 };
646 error = atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
649 return atapi_wait_dsc(stp->device, 60*60);
653 ast_erase(struct ast_softc *stp)
655 int8_t ccb[16] = { ATAPI_ERASE, 3, 0, 0, 0, 0, 0, 0,
656 0, 0, 0, 0, 0, 0, 0, 0 };
659 if ((error = ast_rewind(stp)))
662 return atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 60*60, NULL, NULL);