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.2 2003/06/17 04:28:22 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>
44 #include <dev/ata/ata-all.h>
45 #include <dev/ata/atapi-all.h>
46 #include <dev/ata/atapi-tape.h>
48 /* device structures */
49 static d_open_t astopen;
50 static d_close_t astclose;
51 static d_ioctl_t astioctl;
52 static d_strategy_t aststrategy;
53 static struct cdevsw ast_cdevsw = {
57 /* write */ physwrite,
61 /* strategy */ aststrategy,
66 /* flags */ D_TAPE | D_TRACKCLOSE,
70 static int ast_sense(struct ast_softc *);
71 static void ast_describe(struct ast_softc *);
72 static int ast_done(struct atapi_request *);
73 static int ast_mode_sense(struct ast_softc *, int, void *, int);
74 static int ast_mode_select(struct ast_softc *, void *, int);
75 static int ast_write_filemark(struct ast_softc *, u_int8_t);
76 static int ast_read_position(struct ast_softc *, int, struct ast_readposition *);
77 static int ast_space(struct ast_softc *, u_int8_t, int32_t);
78 static int ast_locate(struct ast_softc *, int, u_int32_t);
79 static int ast_prevent_allow(struct ast_softc *stp, int);
80 static int ast_load_unload(struct ast_softc *, u_int8_t);
81 static int ast_rewind(struct ast_softc *);
82 static int ast_erase(struct ast_softc *);
85 static u_int32_t ast_lun_map = 0;
86 static u_int64_t ast_total = 0;
87 static MALLOC_DEFINE(M_AST, "AST driver", "ATAPI tape driver buffers");
90 astattach(struct ata_device *atadev)
92 struct ast_softc *stp;
93 struct ast_readposition position;
96 stp = malloc(sizeof(struct ast_softc), M_AST, M_NOWAIT | M_ZERO);
98 ata_prtdev(atadev, "out of memory\n");
102 stp->device = atadev;
103 stp->lun = ata_get_lun(&ast_lun_map);
104 ata_set_name(atadev, "ast", stp->lun);
105 bufq_init(&stp->queue);
107 if (ast_sense(stp)) {
112 if (!strcmp(atadev->param->model, "OnStream DI-30")) {
113 struct ast_transferpage transfer;
114 struct ast_identifypage identify;
116 stp->flags |= F_ONSTREAM;
117 bzero(&transfer, sizeof(struct ast_transferpage));
118 ast_mode_sense(stp, ATAPI_TAPE_TRANSFER_PAGE,
119 &transfer, sizeof(transfer));
120 bzero(&identify, sizeof(struct ast_identifypage));
121 ast_mode_sense(stp, ATAPI_TAPE_IDENTIFY_PAGE,
122 &identify, sizeof(identify));
123 strncpy(identify.ident, "FBSD", 4);
124 ast_mode_select(stp, &identify, sizeof(identify));
125 ast_read_position(stp, 0, &position);
128 devstat_add_entry(&stp->stats, "ast", stp->lun, DEV_BSIZE,
129 DEVSTAT_NO_ORDERED_TAGS,
130 DEVSTAT_TYPE_SEQUENTIAL | DEVSTAT_TYPE_IF_IDE,
131 DEVSTAT_PRIORITY_TAPE);
132 dev = make_dev(&ast_cdevsw, dkmakeminor(stp->lun, 0, 0),
133 UID_ROOT, GID_OPERATOR, 0640, "ast%d", stp->lun);
135 dev->si_iosize_max = 256 * DEV_BSIZE;
137 dev = make_dev(&ast_cdevsw, dkmakeminor(stp->lun, 0, 1),
138 UID_ROOT, GID_OPERATOR, 0640, "nast%d", stp->lun);
140 dev->si_iosize_max = 256 * DEV_BSIZE;
142 stp->device->flags |= ATA_D_MEDIA_CHANGED;
144 atadev->driver = stp;
149 astdetach(struct ata_device *atadev)
151 struct ast_softc *stp = atadev->driver;
154 while ((bp = bufq_first(&stp->queue))) {
155 bufq_remove(&stp->queue, bp);
156 bp->b_flags |= B_ERROR;
160 destroy_dev(stp->dev1);
161 destroy_dev(stp->dev2);
162 devstat_remove_entry(&stp->stats);
163 ata_free_name(atadev);
164 ata_free_lun(&ast_lun_map, stp->lun);
166 atadev->driver = NULL;
170 ast_sense(struct ast_softc *stp)
172 int count, error = 0;
174 /* get drive capabilities, some drives needs this repeated */
175 for (count = 0 ; count < 5 ; count++) {
176 if (!(error = ast_mode_sense(stp, ATAPI_TAPE_CAP_PAGE,
177 &stp->cap, sizeof(stp->cap)))) {
179 stp->blksize = 32768;
180 if (stp->cap.blk1024)
186 stp->cap.max_speed = ntohs(stp->cap.max_speed);
187 stp->cap.max_defects = ntohs(stp->cap.max_defects);
188 stp->cap.ctl = ntohs(stp->cap.ctl);
189 stp->cap.speed = ntohs(stp->cap.speed);
190 stp->cap.buffer_size = ntohs(stp->cap.buffer_size);
198 ast_describe(struct ast_softc *stp)
201 ata_prtdev(stp->device, "<%.40s/%.8s> tape drive at ata%d as %s\n",
202 stp->device->param->model, stp->device->param->revision,
203 device_get_unit(stp->device->channel->dev),
204 (stp->device->unit == ATA_MASTER) ? "master" : "slave");
205 ata_prtdev(stp->device, "%dKB/s, ", stp->cap.max_speed);
206 printf("transfer limit %d blk%s, ",
207 stp->cap.ctl, (stp->cap.ctl > 1) ? "s" : "");
208 printf("%dKB buffer, ", (stp->cap.buffer_size * DEV_BSIZE) / 1024);
209 printf("%s\n", ata_mode2str(stp->device->mode));
210 ata_prtdev(stp->device, "Medium: ");
211 switch (stp->cap.medium_type) {
213 printf("none"); break;
215 printf("Travan 1 (400 Mbyte)"); break;
217 printf("Travan 4 (4 Gbyte)"); break;
219 printf("OnStream ADR (15Gyte)"); break;
221 printf("unknown (0x%x)", stp->cap.medium_type);
223 if (stp->cap.readonly) printf(", readonly");
224 if (stp->cap.reverse) printf(", reverse");
225 if (stp->cap.eformat) printf(", eformat");
226 if (stp->cap.qfa) printf(", qfa");
227 if (stp->cap.lock) printf(", lock");
228 if (stp->cap.locked) printf(", locked");
229 if (stp->cap.prevent) printf(", prevent");
230 if (stp->cap.eject) printf(", eject");
231 if (stp->cap.disconnect) printf(", disconnect");
232 if (stp->cap.ecc) printf(", ecc");
233 if (stp->cap.compress) printf(", compress");
234 if (stp->cap.blk512) printf(", 512b");
235 if (stp->cap.blk1024) printf(", 1024b");
236 if (stp->cap.blk32k) printf(", 32kb");
240 ata_prtdev(stp->device, "TAPE <%.40s> at ata%d-%s %s\n",
241 stp->device->param->model,
242 device_get_unit(stp->device->channel->dev),
243 (stp->device->unit == ATA_MASTER) ? "master" : "slave",
244 ata_mode2str(stp->device->mode));
249 astopen(dev_t dev, int flags, int fmt, struct proc *p)
251 struct ast_softc *stp = dev->si_drv1;
256 if (count_dev(dev) > 1)
259 atapi_test_ready(stp->device);
262 ast_prevent_allow(stp, 1);
265 ata_prtdev(stp->device, "sense media type failed\n");
267 stp->device->flags &= ~ATA_D_MEDIA_CHANGED;
268 stp->flags &= ~(F_DATA_WRITTEN | F_FM_WRITTEN);
274 astclose(dev_t dev, int flags, int fmt, struct proc *p)
276 struct ast_softc *stp = dev->si_drv1;
278 /* flush buffers, some drives fail here, they should report ctl = 0 */
279 if (stp->cap.ctl && (stp->flags & F_DATA_WRITTEN))
280 ast_write_filemark(stp, 0);
282 /* write filemark if data written to tape */
283 if (!(stp->flags & F_ONSTREAM) &&
284 (stp->flags & (F_DATA_WRITTEN | F_FM_WRITTEN)) == F_DATA_WRITTEN)
285 ast_write_filemark(stp, WF_WRITE);
287 /* if minor is even rewind on close */
288 if (!(minor(dev) & 0x01))
291 if (stp->cap.lock && count_dev(dev) == 1)
292 ast_prevent_allow(stp, 0);
294 stp->flags &= F_CTL_WARN;
296 ata_prtdev(stp->device, "%llu total bytes transferred\n", ast_total);
302 astioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
304 struct ast_softc *stp = dev->si_drv1;
310 struct mtget *g = (struct mtget *) addr;
312 bzero(g, sizeof(struct mtget));
315 g->mt_blksiz = stp->blksize;
316 g->mt_comp = stp->cap.compress;
317 g->mt_density0 = 0; g->mt_density1 = 0;
318 g->mt_density2 = 0; g->mt_density3 = 0;
319 g->mt_blksiz0 = 0; g->mt_blksiz1 = 0;
320 g->mt_blksiz2 = 0; g->mt_blksiz3 = 0;
321 g->mt_comp0 = 0; g->mt_comp1 = 0;
322 g->mt_comp2 = 0; g->mt_comp3 = 0;
328 struct mtop *mt = (struct mtop *)addr;
330 switch ((int16_t) (mt->mt_op)) {
333 for (i=0; i < mt->mt_count && !error; i++)
334 error = ast_write_filemark(stp, WF_WRITE);
339 error = ast_space(stp, SP_FM, mt->mt_count);
344 error = ast_space(stp, SP_FM, -(mt->mt_count));
348 error = ast_rewind(stp);
352 error = ast_load_unload(stp, SS_EJECT);
356 error = ast_write_filemark(stp, 0);
360 error = ast_erase(stp);
364 error = ast_space(stp, SP_EOD, 0);
368 error = ast_load_unload(stp, SS_RETENSION | SS_LOAD);
385 struct ast_readposition position;
387 if ((error = ast_read_position(stp, 0, &position)))
389 *(u_int32_t *)addr = position.tape;
394 struct ast_readposition position;
396 if ((error = ast_read_position(stp, 1, &position)))
398 *(u_int32_t *)addr = position.tape;
402 error = ast_locate(stp, 0, *(u_int32_t *)addr);
405 error = ast_locate(stp, 1, *(u_int32_t *)addr);
414 aststrategy(struct buf *bp)
416 struct ast_softc *stp = bp->b_dev->si_drv1;
419 if (stp->device->flags & ATA_D_DETACHING) {
420 bp->b_flags |= B_ERROR;
426 /* if it's a null transfer, return immediatly. */
427 if (bp->b_bcount == 0) {
432 if (!(bp->b_flags & B_READ) && stp->flags & F_WRITEPROTECT) {
433 bp->b_flags |= B_ERROR;
439 /* check for != blocksize requests */
440 if (bp->b_bcount % stp->blksize) {
441 ata_prtdev(stp->device, "transfers must be multiple of %d\n",
443 bp->b_flags |= B_ERROR;
449 /* warn about transfers bigger than the device suggests */
450 if (bp->b_bcount > stp->blksize * stp->cap.ctl) {
451 if ((stp->flags & F_CTL_WARN) == 0) {
452 ata_prtdev(stp->device, "WARNING: CTL exceeded %ld>%d\n",
453 bp->b_bcount, stp->blksize * stp->cap.ctl);
454 stp->flags |= F_CTL_WARN;
459 bufq_insert_tail(&stp->queue, bp);
461 ata_start(stp->device->channel);
465 ast_start(struct ata_device *atadev)
467 struct ast_softc *stp = atadev->driver;
468 struct buf *bp = bufq_first(&stp->queue);
475 bzero(ccb, sizeof(ccb));
477 if (bp->b_flags & B_READ)
480 ccb[0] = ATAPI_WRITE;
482 bufq_remove(&stp->queue, bp);
483 blkcount = bp->b_bcount / stp->blksize;
486 ccb[2] = blkcount>>16;
487 ccb[3] = blkcount>>8;
490 devstat_start_transaction(&stp->stats);
492 atapi_queue_cmd(stp->device, ccb, bp->b_data, blkcount * stp->blksize,
493 (bp->b_flags & B_READ) ? ATPR_F_READ : 0,
498 ast_done(struct atapi_request *request)
500 struct buf *bp = request->driver;
501 struct ast_softc *stp = request->device->driver;
503 if (request->error) {
504 bp->b_error = request->error;
505 bp->b_flags |= B_ERROR;
508 if (!(bp->b_flags & B_READ))
509 stp->flags |= F_DATA_WRITTEN;
510 bp->b_resid = bp->b_bcount - request->donecount;
511 ast_total += (bp->b_bcount - bp->b_resid);
513 devstat_end_transaction_buf(&stp->stats, bp);
519 ast_mode_sense(struct ast_softc *stp, int page, void *pagebuf, int pagesize)
521 int8_t ccb[16] = { ATAPI_MODE_SENSE, 0x08, page, pagesize>>8, pagesize,
522 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
525 error = atapi_queue_cmd(stp->device, ccb, pagebuf, pagesize, ATPR_F_READ,
528 atapi_dump("ast: mode sense ", pagebuf, pagesize);
534 ast_mode_select(struct ast_softc *stp, void *pagebuf, int pagesize)
536 int8_t ccb[16] = { ATAPI_MODE_SELECT, 0x10, 0, pagesize>>8, pagesize,
537 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
540 ata_prtdev(stp->device, "modeselect pagesize=%d\n", pagesize);
541 atapi_dump("mode select ", pagebuf, pagesize);
543 return atapi_queue_cmd(stp->device, ccb, pagebuf, pagesize, 0,
548 ast_write_filemark(struct ast_softc *stp, u_int8_t function)
550 int8_t ccb[16] = { ATAPI_WEOF, 0x01, 0, 0, function, 0, 0, 0,
551 0, 0, 0, 0, 0, 0, 0, 0 };
554 if (stp->flags & F_ONSTREAM)
555 ccb[4] = 0x00; /* only flush buffers supported */
558 if (stp->flags & F_FM_WRITTEN)
559 stp->flags &= ~F_DATA_WRITTEN;
561 stp->flags |= F_FM_WRITTEN;
564 error = atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
567 return atapi_wait_dsc(stp->device, 10*60);
571 ast_read_position(struct ast_softc *stp, int hard,
572 struct ast_readposition *position)
574 int8_t ccb[16] = { ATAPI_READ_POSITION, (hard ? 0x01 : 0), 0, 0, 0, 0, 0, 0,
575 0, 0, 0, 0, 0, 0, 0, 0 };
578 error = atapi_queue_cmd(stp->device, ccb, (caddr_t)position,
579 sizeof(struct ast_readposition), ATPR_F_READ, 10,
581 position->tape = ntohl(position->tape);
582 position->host = ntohl(position->host);
587 ast_space(struct ast_softc *stp, 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 atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 60*60, NULL, NULL);
596 ast_locate(struct ast_softc *stp, 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 = atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
606 return atapi_wait_dsc(stp->device, 60*60);
610 ast_prevent_allow(struct ast_softc *stp, 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 atapi_queue_cmd(stp->device, ccb, NULL, 0, 0,30, NULL, NULL);
619 ast_load_unload(struct ast_softc *stp, u_int8_t function)
621 int8_t ccb[16] = { ATAPI_START_STOP, 0x01, 0, 0, function, 0, 0, 0,
622 0, 0, 0, 0, 0, 0, 0, 0 };
625 if ((function & SS_EJECT) && !stp->cap.eject)
627 error = atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
630 tsleep((caddr_t)&error, PRIBIO, "astlu", 1 * hz);
631 if (function == SS_EJECT)
633 return atapi_wait_dsc(stp->device, 60*60);
637 ast_rewind(struct ast_softc *stp)
639 int8_t ccb[16] = { ATAPI_REZERO, 0x01, 0, 0, 0, 0, 0, 0,
640 0, 0, 0, 0, 0, 0, 0, 0 };
643 error = atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
646 return atapi_wait_dsc(stp->device, 60*60);
650 ast_erase(struct ast_softc *stp)
652 int8_t ccb[16] = { ATAPI_ERASE, 3, 0, 0, 0, 0, 0, 0,
653 0, 0, 0, 0, 0, 0, 0, 0 };
656 if ((error = ast_rewind(stp)))
659 return atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 60*60, NULL, NULL);