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.7 2003/08/07 21:16:51 dillon Exp $
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
42 #include <sys/disklabel.h>
43 #include <sys/devicestat.h>
47 #include "atapi-all.h"
48 #include "atapi-tape.h"
50 /* device structures */
51 static d_open_t astopen;
52 static d_close_t astclose;
53 static d_ioctl_t astioctl;
54 static d_strategy_t aststrategy;
56 static struct cdevsw ast_cdevsw = {
59 /* flags */ D_TAPE | D_TRACKCLOSE,
66 /* write */ physwrite,
70 /* strategy */ aststrategy,
76 static int ast_sense(struct ast_softc *);
77 static void ast_describe(struct ast_softc *);
78 static int ast_done(struct atapi_request *);
79 static int ast_mode_sense(struct ast_softc *, int, void *, int);
80 static int ast_mode_select(struct ast_softc *, void *, int);
81 static int ast_write_filemark(struct ast_softc *, u_int8_t);
82 static int ast_read_position(struct ast_softc *, int, struct ast_readposition *);
83 static int ast_space(struct ast_softc *, u_int8_t, int32_t);
84 static int ast_locate(struct ast_softc *, int, u_int32_t);
85 static int ast_prevent_allow(struct ast_softc *stp, int);
86 static int ast_load_unload(struct ast_softc *, u_int8_t);
87 static int ast_rewind(struct ast_softc *);
88 static int ast_erase(struct ast_softc *);
91 static u_int32_t ast_lun_map = 0;
92 static u_int64_t ast_total = 0;
93 static MALLOC_DEFINE(M_AST, "AST driver", "ATAPI tape driver buffers");
96 astattach(struct ata_device *atadev)
98 struct ast_softc *stp;
99 struct ast_readposition position;
102 stp = malloc(sizeof(struct ast_softc), M_AST, M_NOWAIT | M_ZERO);
104 ata_prtdev(atadev, "out of memory\n");
108 stp->device = atadev;
109 stp->lun = ata_get_lun(&ast_lun_map);
110 ata_set_name(atadev, "ast", stp->lun);
111 bufq_init(&stp->queue);
113 if (ast_sense(stp)) {
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(stp, ATAPI_TAPE_TRANSFER_PAGE,
125 &transfer, sizeof(transfer));
126 bzero(&identify, sizeof(struct ast_identifypage));
127 ast_mode_sense(stp, ATAPI_TAPE_IDENTIFY_PAGE,
128 &identify, sizeof(identify));
129 strncpy(identify.ident, "FBSD", 4);
130 ast_mode_select(stp, &identify, sizeof(identify));
131 ast_read_position(stp, 0, &position);
134 devstat_add_entry(&stp->stats, "ast", stp->lun, DEV_BSIZE,
135 DEVSTAT_NO_ORDERED_TAGS,
136 DEVSTAT_TYPE_SEQUENTIAL | DEVSTAT_TYPE_IF_IDE,
137 DEVSTAT_PRIORITY_TAPE);
138 dev = make_dev(&ast_cdevsw, dkmakeminor(stp->lun, 0, 0),
139 UID_ROOT, GID_OPERATOR, 0640, "ast%d", stp->lun);
141 dev->si_iosize_max = 256 * DEV_BSIZE;
143 dev = make_dev(&ast_cdevsw, dkmakeminor(stp->lun, 0, 1),
144 UID_ROOT, GID_OPERATOR, 0640, "nast%d", stp->lun);
146 dev->si_iosize_max = 256 * DEV_BSIZE;
148 stp->device->flags |= ATA_D_MEDIA_CHANGED;
150 atadev->driver = stp;
155 astdetach(struct ata_device *atadev)
157 struct ast_softc *stp = atadev->driver;
160 while ((bp = bufq_first(&stp->queue))) {
161 bufq_remove(&stp->queue, bp);
162 bp->b_flags |= B_ERROR;
166 destroy_dev(stp->dev1);
167 destroy_dev(stp->dev2);
168 devstat_remove_entry(&stp->stats);
169 ata_free_name(atadev);
170 ata_free_lun(&ast_lun_map, stp->lun);
172 atadev->driver = NULL;
176 ast_sense(struct ast_softc *stp)
178 int count, error = 0;
180 /* get drive capabilities, some drives needs this repeated */
181 for (count = 0 ; count < 5 ; count++) {
182 if (!(error = ast_mode_sense(stp, ATAPI_TAPE_CAP_PAGE,
183 &stp->cap, sizeof(stp->cap)))) {
185 stp->blksize = 32768;
186 if (stp->cap.blk1024)
192 stp->cap.max_speed = ntohs(stp->cap.max_speed);
193 stp->cap.max_defects = ntohs(stp->cap.max_defects);
194 stp->cap.ctl = ntohs(stp->cap.ctl);
195 stp->cap.speed = ntohs(stp->cap.speed);
196 stp->cap.buffer_size = ntohs(stp->cap.buffer_size);
204 ast_describe(struct ast_softc *stp)
207 ata_prtdev(stp->device, "<%.40s/%.8s> tape drive at ata%d as %s\n",
208 stp->device->param->model, stp->device->param->revision,
209 device_get_unit(stp->device->channel->dev),
210 (stp->device->unit == ATA_MASTER) ? "master" : "slave");
211 ata_prtdev(stp->device, "%dKB/s, ", stp->cap.max_speed);
212 printf("transfer limit %d blk%s, ",
213 stp->cap.ctl, (stp->cap.ctl > 1) ? "s" : "");
214 printf("%dKB buffer, ", (stp->cap.buffer_size * DEV_BSIZE) / 1024);
215 printf("%s\n", ata_mode2str(stp->device->mode));
216 ata_prtdev(stp->device, "Medium: ");
217 switch (stp->cap.medium_type) {
219 printf("none"); break;
221 printf("Travan 1 (400 Mbyte)"); break;
223 printf("Travan 4 (4 Gbyte)"); break;
225 printf("OnStream ADR (15Gyte)"); break;
227 printf("unknown (0x%x)", stp->cap.medium_type);
229 if (stp->cap.readonly) printf(", readonly");
230 if (stp->cap.reverse) printf(", reverse");
231 if (stp->cap.eformat) printf(", eformat");
232 if (stp->cap.qfa) printf(", qfa");
233 if (stp->cap.lock) printf(", lock");
234 if (stp->cap.locked) printf(", locked");
235 if (stp->cap.prevent) printf(", prevent");
236 if (stp->cap.eject) printf(", eject");
237 if (stp->cap.disconnect) printf(", disconnect");
238 if (stp->cap.ecc) printf(", ecc");
239 if (stp->cap.compress) printf(", compress");
240 if (stp->cap.blk512) printf(", 512b");
241 if (stp->cap.blk1024) printf(", 1024b");
242 if (stp->cap.blk32k) printf(", 32kb");
246 ata_prtdev(stp->device, "TAPE <%.40s> at ata%d-%s %s\n",
247 stp->device->param->model,
248 device_get_unit(stp->device->channel->dev),
249 (stp->device->unit == ATA_MASTER) ? "master" : "slave",
250 ata_mode2str(stp->device->mode));
255 astopen(dev_t dev, int flags, int fmt, struct thread *td)
257 struct ast_softc *stp = dev->si_drv1;
262 if (count_dev(dev) > 1)
265 atapi_test_ready(stp->device);
268 ast_prevent_allow(stp, 1);
271 ata_prtdev(stp->device, "sense media type failed\n");
273 stp->device->flags &= ~ATA_D_MEDIA_CHANGED;
274 stp->flags &= ~(F_DATA_WRITTEN | F_FM_WRITTEN);
280 astclose(dev_t dev, int flags, int fmt, struct thread *td)
282 struct ast_softc *stp = dev->si_drv1;
284 /* flush buffers, some drives fail here, they should report ctl = 0 */
285 if (stp->cap.ctl && (stp->flags & F_DATA_WRITTEN))
286 ast_write_filemark(stp, 0);
288 /* write filemark if data written to tape */
289 if (!(stp->flags & F_ONSTREAM) &&
290 (stp->flags & (F_DATA_WRITTEN | F_FM_WRITTEN)) == F_DATA_WRITTEN)
291 ast_write_filemark(stp, WF_WRITE);
293 /* if minor is even rewind on close */
294 if (!(minor(dev) & 0x01))
297 if (stp->cap.lock && count_dev(dev) == 1)
298 ast_prevent_allow(stp, 0);
300 stp->flags &= F_CTL_WARN;
302 ata_prtdev(stp->device, "%llu total bytes transferred\n", ast_total);
308 astioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
310 struct ast_softc *stp = dev->si_drv1;
316 struct mtget *g = (struct mtget *) addr;
318 bzero(g, sizeof(struct mtget));
321 g->mt_blksiz = stp->blksize;
322 g->mt_comp = stp->cap.compress;
323 g->mt_density0 = 0; g->mt_density1 = 0;
324 g->mt_density2 = 0; g->mt_density3 = 0;
325 g->mt_blksiz0 = 0; g->mt_blksiz1 = 0;
326 g->mt_blksiz2 = 0; g->mt_blksiz3 = 0;
327 g->mt_comp0 = 0; g->mt_comp1 = 0;
328 g->mt_comp2 = 0; g->mt_comp3 = 0;
334 struct mtop *mt = (struct mtop *)addr;
336 switch ((int16_t) (mt->mt_op)) {
339 for (i=0; i < mt->mt_count && !error; i++)
340 error = ast_write_filemark(stp, WF_WRITE);
345 error = ast_space(stp, SP_FM, mt->mt_count);
350 error = ast_space(stp, SP_FM, -(mt->mt_count));
354 error = ast_rewind(stp);
358 error = ast_load_unload(stp, SS_EJECT);
362 error = ast_write_filemark(stp, 0);
366 error = ast_erase(stp);
370 error = ast_space(stp, SP_EOD, 0);
374 error = ast_load_unload(stp, SS_RETENSION | SS_LOAD);
391 struct ast_readposition position;
393 if ((error = ast_read_position(stp, 0, &position)))
395 *(u_int32_t *)addr = position.tape;
400 struct ast_readposition position;
402 if ((error = ast_read_position(stp, 1, &position)))
404 *(u_int32_t *)addr = position.tape;
408 error = ast_locate(stp, 0, *(u_int32_t *)addr);
411 error = ast_locate(stp, 1, *(u_int32_t *)addr);
420 aststrategy(struct buf *bp)
422 struct ast_softc *stp = bp->b_dev->si_drv1;
425 if (stp->device->flags & ATA_D_DETACHING) {
426 bp->b_flags |= B_ERROR;
432 /* if it's a null transfer, return immediatly. */
433 if (bp->b_bcount == 0) {
438 if (!(bp->b_flags & B_READ) && stp->flags & F_WRITEPROTECT) {
439 bp->b_flags |= B_ERROR;
445 /* check for != blocksize requests */
446 if (bp->b_bcount % stp->blksize) {
447 ata_prtdev(stp->device, "transfers must be multiple of %d\n",
449 bp->b_flags |= B_ERROR;
455 /* warn about transfers bigger than the device suggests */
456 if (bp->b_bcount > stp->blksize * stp->cap.ctl) {
457 if ((stp->flags & F_CTL_WARN) == 0) {
458 ata_prtdev(stp->device, "WARNING: CTL exceeded %ld>%d\n",
459 bp->b_bcount, stp->blksize * stp->cap.ctl);
460 stp->flags |= F_CTL_WARN;
465 bufq_insert_tail(&stp->queue, bp);
467 ata_start(stp->device->channel);
471 ast_start(struct ata_device *atadev)
473 struct ast_softc *stp = atadev->driver;
474 struct buf *bp = bufq_first(&stp->queue);
481 bzero(ccb, sizeof(ccb));
483 if (bp->b_flags & B_READ)
486 ccb[0] = ATAPI_WRITE;
488 bufq_remove(&stp->queue, bp);
489 blkcount = bp->b_bcount / stp->blksize;
492 ccb[2] = blkcount>>16;
493 ccb[3] = blkcount>>8;
496 devstat_start_transaction(&stp->stats);
498 atapi_queue_cmd(stp->device, ccb, bp->b_data, blkcount * stp->blksize,
499 (bp->b_flags & B_READ) ? ATPR_F_READ : 0,
504 ast_done(struct atapi_request *request)
506 struct buf *bp = request->driver;
507 struct ast_softc *stp = request->device->driver;
509 if (request->error) {
510 bp->b_error = request->error;
511 bp->b_flags |= B_ERROR;
514 if (!(bp->b_flags & B_READ))
515 stp->flags |= F_DATA_WRITTEN;
516 bp->b_resid = bp->b_bcount - request->donecount;
517 ast_total += (bp->b_bcount - bp->b_resid);
519 devstat_end_transaction_buf(&stp->stats, bp);
525 ast_mode_sense(struct ast_softc *stp, int page, void *pagebuf, int pagesize)
527 int8_t ccb[16] = { ATAPI_MODE_SENSE, 0x08, page, pagesize>>8, pagesize,
528 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
531 error = atapi_queue_cmd(stp->device, ccb, pagebuf, pagesize, ATPR_F_READ,
534 atapi_dump("ast: mode sense ", pagebuf, pagesize);
540 ast_mode_select(struct ast_softc *stp, 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 };
546 ata_prtdev(stp->device, "modeselect pagesize=%d\n", pagesize);
547 atapi_dump("mode select ", pagebuf, pagesize);
549 return atapi_queue_cmd(stp->device, ccb, pagebuf, pagesize, 0,
554 ast_write_filemark(struct ast_softc *stp, u_int8_t function)
556 int8_t ccb[16] = { ATAPI_WEOF, 0x01, 0, 0, function, 0, 0, 0,
557 0, 0, 0, 0, 0, 0, 0, 0 };
560 if (stp->flags & F_ONSTREAM)
561 ccb[4] = 0x00; /* only flush buffers supported */
564 if (stp->flags & F_FM_WRITTEN)
565 stp->flags &= ~F_DATA_WRITTEN;
567 stp->flags |= F_FM_WRITTEN;
570 error = atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
573 return atapi_wait_dsc(stp->device, 10*60);
577 ast_read_position(struct ast_softc *stp, int hard,
578 struct ast_readposition *position)
580 int8_t ccb[16] = { ATAPI_READ_POSITION, (hard ? 0x01 : 0), 0, 0, 0, 0, 0, 0,
581 0, 0, 0, 0, 0, 0, 0, 0 };
584 error = atapi_queue_cmd(stp->device, ccb, (caddr_t)position,
585 sizeof(struct ast_readposition), ATPR_F_READ, 10,
587 position->tape = ntohl(position->tape);
588 position->host = ntohl(position->host);
593 ast_space(struct ast_softc *stp, u_int8_t function, int32_t count)
595 int8_t ccb[16] = { ATAPI_SPACE, function, count>>16, count>>8, count,
596 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
598 return atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 60*60, NULL, NULL);
602 ast_locate(struct ast_softc *stp, int hard, u_int32_t pos)
604 int8_t ccb[16] = { ATAPI_LOCATE, 0x01 | (hard ? 0x4 : 0), 0,
605 pos>>24, pos>>16, pos>>8, pos,
606 0, 0, 0, 0, 0, 0, 0, 0, 0 };
609 error = atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
612 return atapi_wait_dsc(stp->device, 60*60);
616 ast_prevent_allow(struct ast_softc *stp, int lock)
618 int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
619 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
621 return atapi_queue_cmd(stp->device, ccb, NULL, 0, 0,30, NULL, NULL);
625 ast_load_unload(struct ast_softc *stp, u_int8_t function)
627 int8_t ccb[16] = { ATAPI_START_STOP, 0x01, 0, 0, function, 0, 0, 0,
628 0, 0, 0, 0, 0, 0, 0, 0 };
631 if ((function & SS_EJECT) && !stp->cap.eject)
633 error = atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
636 tsleep((caddr_t)&error, 0, "astlu", 1 * hz);
637 if (function == SS_EJECT)
639 return atapi_wait_dsc(stp->device, 60*60);
643 ast_rewind(struct ast_softc *stp)
645 int8_t ccb[16] = { ATAPI_REZERO, 0x01, 0, 0, 0, 0, 0, 0,
646 0, 0, 0, 0, 0, 0, 0, 0 };
649 error = atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
652 return atapi_wait_dsc(stp->device, 60*60);
656 ast_erase(struct ast_softc *stp)
658 int8_t ccb[16] = { ATAPI_ERASE, 3, 0, 0, 0, 0, 0, 0,
659 0, 0, 0, 0, 0, 0, 0, 0 };
662 if ((error = ast_rewind(stp)))
665 return atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 60*60, NULL, NULL);