2 * Copyright (c) 1998 - 2004 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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
42 #include <sys/devicestat.h>
44 #include <sys/taskqueue.h>
46 #include <machine/bus.h>
47 #include <dev/ata/ata-all.h>
48 #include <dev/ata/atapi-tape.h>
50 /* device structures */
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,
65 .d_flags = D_TAPE | D_TRACKCLOSE,
69 static void ast_detach(struct ata_device *);
70 static void ast_start(struct ata_device *);
71 static int ast_sense(struct ast_softc *);
72 static void ast_describe(struct ast_softc *);
73 static void ast_done(struct ata_request *);
74 static int ast_mode_sense(struct ast_softc *, int, void *, int);
75 static int ast_mode_select(struct ast_softc *, void *, int);
76 static int ast_write_filemark(struct ast_softc *, u_int8_t);
77 static int ast_read_position(struct ast_softc *, int, struct ast_readposition *);
78 static int ast_space(struct ast_softc *, u_int8_t, int32_t);
79 static int ast_locate(struct ast_softc *, int, u_int32_t);
80 static int ast_prevent_allow(struct ast_softc *stp, int);
81 static int ast_load_unload(struct ast_softc *, u_int8_t);
82 static int ast_rewind(struct ast_softc *);
83 static int ast_erase(struct ast_softc *);
84 static int ast_test_ready(struct ata_device *);
85 static int ast_wait_dsc(struct ata_device *, int);
88 static u_int32_t ast_lun_map = 0;
89 static u_int64_t ast_total = 0;
90 static MALLOC_DEFINE(M_AST, "AST driver", "ATAPI tape driver buffers");
93 ast_attach(struct ata_device *atadev)
95 struct ast_softc *stp;
96 struct ast_readposition position;
99 stp = malloc(sizeof(struct ast_softc), M_AST, M_NOWAIT | M_ZERO);
101 ata_prtdev(atadev, "out of memory\n");
105 stp->device = atadev;
106 stp->lun = ata_get_lun(&ast_lun_map);
107 ata_set_name(atadev, "ast", stp->lun);
108 bioq_init(&stp->queue);
109 mtx_init(&stp->queue_mtx, "ATAPI TAPE bioqueue lock", NULL, MTX_DEF);
111 if (ast_sense(stp)) {
116 if (!strcmp(atadev->param->model, "OnStream DI-30")) {
117 struct ast_transferpage transfer;
118 struct ast_identifypage identify;
120 stp->flags |= F_ONSTREAM;
121 bzero(&transfer, sizeof(struct ast_transferpage));
122 ast_mode_sense(stp, ATAPI_TAPE_TRANSFER_PAGE,
123 &transfer, sizeof(transfer));
124 bzero(&identify, sizeof(struct ast_identifypage));
125 ast_mode_sense(stp, ATAPI_TAPE_IDENTIFY_PAGE,
126 &identify, sizeof(identify));
127 strncpy(identify.ident, "FBSD", 4);
128 ast_mode_select(stp, &identify, sizeof(identify));
129 ast_read_position(stp, 0, &position);
132 stp->stats = devstat_new_entry("ast", stp->lun, DEV_BSIZE,
133 DEVSTAT_NO_ORDERED_TAGS,
134 DEVSTAT_TYPE_SEQUENTIAL | DEVSTAT_TYPE_IF_IDE,
135 DEVSTAT_PRIORITY_TAPE);
136 dev = make_dev(&ast_cdevsw, 2 * stp->lun,
137 UID_ROOT, GID_OPERATOR, 0640, "ast%d", stp->lun);
139 if (atadev->channel->dma)
140 dev->si_iosize_max = atadev->channel->dma->max_iosize;
142 dev->si_iosize_max = DFLTPHYS;
144 dev = make_dev(&ast_cdevsw, 2 * stp->lun + 1,
145 UID_ROOT, GID_OPERATOR, 0640, "nast%d", stp->lun);
148 if (atadev->channel->dma)
149 dev->si_iosize_max = atadev->channel->dma->max_iosize;
151 dev->si_iosize_max = DFLTPHYS;
154 /* setup the function ptrs */
155 atadev->detach = ast_detach;
156 atadev->start = ast_start;
158 atadev->flags |= ATA_D_MEDIA_CHANGED;
160 /* announce we are here */
165 ast_detach(struct ata_device *atadev)
167 struct ast_softc *stp = atadev->softc;
169 mtx_lock(&stp->queue_mtx);
170 bioq_flush(&stp->queue, NULL, ENXIO);
171 mtx_unlock(&stp->queue_mtx);
172 mtx_destroy(&stp->queue_mtx);
173 destroy_dev(stp->dev1);
174 destroy_dev(stp->dev2);
175 devstat_remove_entry(stp->stats);
176 ata_prtdev(atadev, "WARNING - removed from configuration\n");
177 ata_free_name(atadev);
178 ata_free_lun(&ast_lun_map, stp->lun);
179 atadev->attach = NULL;
180 atadev->detach = NULL;
181 atadev->start = NULL;
182 atadev->softc = NULL;
188 ast_sense(struct ast_softc *stp)
192 /* get drive capabilities, some bugridden drives needs this repeated */
193 for (count = 0 ; count < 5 ; count++) {
194 if (!ast_mode_sense(stp, ATAPI_TAPE_CAP_PAGE,
195 &stp->cap, sizeof(stp->cap)) &&
196 stp->cap.page_code == ATAPI_TAPE_CAP_PAGE) {
198 stp->blksize = 32768;
199 if (stp->cap.blk1024)
205 stp->cap.max_speed = ntohs(stp->cap.max_speed);
206 stp->cap.max_defects = ntohs(stp->cap.max_defects);
207 stp->cap.ctl = ntohs(stp->cap.ctl);
208 stp->cap.speed = ntohs(stp->cap.speed);
209 stp->cap.buffer_size = ntohs(stp->cap.buffer_size);
217 ast_describe(struct ast_softc *stp)
220 ata_prtdev(stp->device, "<%.40s/%.8s> tape drive at ata%d as %s\n",
221 stp->device->param->model, stp->device->param->revision,
222 device_get_unit(stp->device->channel->dev),
223 (stp->device->unit == ATA_MASTER) ? "master" : "slave");
224 ata_prtdev(stp->device, "%dKB/s, ", stp->cap.max_speed);
225 printf("transfer limit %d blk%s, ",
226 stp->cap.ctl, (stp->cap.ctl > 1) ? "s" : "");
227 printf("%dKB buffer, ", (stp->cap.buffer_size * DEV_BSIZE) / 1024);
228 printf("%s\n", ata_mode2str(stp->device->mode));
229 ata_prtdev(stp->device, "Medium: ");
230 switch (stp->cap.medium_type) {
232 printf("none"); break;
234 printf("Travan 1 (400 Mbyte)"); break;
236 printf("Travan 4 (4 Gbyte)"); break;
238 printf("OnStream ADR (15Gyte)"); break;
240 printf("unknown (0x%x)", stp->cap.medium_type);
242 if (stp->cap.readonly) printf(", readonly");
243 if (stp->cap.reverse) printf(", reverse");
244 if (stp->cap.eformat) printf(", eformat");
245 if (stp->cap.qfa) printf(", qfa");
246 if (stp->cap.lock) printf(", lock");
247 if (stp->cap.locked) printf(", locked");
248 if (stp->cap.prevent) printf(", prevent");
249 if (stp->cap.eject) printf(", eject");
250 if (stp->cap.disconnect) printf(", disconnect");
251 if (stp->cap.ecc) printf(", ecc");
252 if (stp->cap.compress) printf(", compress");
253 if (stp->cap.blk512) printf(", 512b");
254 if (stp->cap.blk1024) printf(", 1024b");
255 if (stp->cap.blk32k) printf(", 32kb");
259 ata_prtdev(stp->device, "TAPE <%.40s/%.8s> at ata%d-%s %s\n",
260 stp->device->param->model, stp->device->param->revision,
261 device_get_unit(stp->device->channel->dev),
262 (stp->device->unit == ATA_MASTER) ? "master" : "slave",
263 ata_mode2str(stp->device->mode));
268 ast_open(struct cdev *dev, int flags, int fmt, struct thread *td)
270 struct ast_softc *stp = dev->si_drv1;
272 if (!stp || stp->device->flags & ATA_D_DETACHING)
275 if (count_dev(dev) > 1)
278 ast_test_ready(stp->device);
281 ast_prevent_allow(stp, 1);
284 ata_prtdev(stp->device, "sense media type failed\n");
286 stp->device->flags &= ~ATA_D_MEDIA_CHANGED;
287 stp->flags &= ~(F_DATA_WRITTEN | F_FM_WRITTEN);
293 ast_close(struct cdev *dev, int flags, int fmt, struct thread *td)
295 struct ast_softc *stp = dev->si_drv1;
297 /* flush buffers, some drives fail here, they should report ctl = 0 */
298 if (stp->cap.ctl && (stp->flags & F_DATA_WRITTEN))
299 ast_write_filemark(stp, 0);
301 /* write filemark if data written to tape */
302 if (!(stp->flags & F_ONSTREAM) &&
303 (stp->flags & (F_DATA_WRITTEN | F_FM_WRITTEN)) == F_DATA_WRITTEN)
304 ast_write_filemark(stp, ATAPI_WF_WRITE);
306 /* if minor is even rewind on close */
307 if (!(minor(dev) & 0x01))
310 if (stp->cap.lock && count_dev(dev) == 1)
311 ast_prevent_allow(stp, 0);
313 stp->flags &= ~F_CTL_WARN;
315 ata_prtdev(stp->device, "%ju total bytes transferred\n",
316 (uintmax_t)ast_total);
322 ast_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
324 struct ast_softc *stp = dev->si_drv1;
330 struct mtget *g = (struct mtget *) addr;
332 bzero(g, sizeof(struct mtget));
335 g->mt_blksiz = stp->blksize;
336 g->mt_comp = stp->cap.compress;
337 g->mt_density0 = 0; g->mt_density1 = 0;
338 g->mt_density2 = 0; g->mt_density3 = 0;
339 g->mt_blksiz0 = 0; g->mt_blksiz1 = 0;
340 g->mt_blksiz2 = 0; g->mt_blksiz3 = 0;
341 g->mt_comp0 = 0; g->mt_comp1 = 0;
342 g->mt_comp2 = 0; g->mt_comp3 = 0;
349 struct mtop *mt = (struct mtop *)addr;
351 switch ((int16_t) (mt->mt_op)) {
354 for (i=0; i < mt->mt_count && !error; i++)
355 error = ast_write_filemark(stp, ATAPI_WF_WRITE);
360 error = ast_space(stp, ATAPI_SP_FM, mt->mt_count);
365 error = ast_space(stp, ATAPI_SP_FM, -(mt->mt_count));
369 error = ast_rewind(stp);
373 error = ast_load_unload(stp, ATAPI_SS_EJECT);
377 error = ast_write_filemark(stp, 0);
381 error = ast_erase(stp);
385 error = ast_space(stp, ATAPI_SP_EOD, 0);
389 error = ast_load_unload(stp, ATAPI_SS_RETENSION|ATAPI_SS_LOAD);
407 struct ast_readposition position;
409 if ((error = ast_read_position(stp, 0, &position)))
411 *(u_int32_t *)addr = position.tape;
417 struct ast_readposition position;
419 if ((error = ast_read_position(stp, 1, &position)))
421 *(u_int32_t *)addr = position.tape;
426 error = ast_locate(stp, 0, *(u_int32_t *)addr);
430 error = ast_locate(stp, 1, *(u_int32_t *)addr);
440 ast_strategy(struct bio *bp)
442 struct ast_softc *stp = bp->bio_dev->si_drv1;
444 if (stp->device->flags & ATA_D_DETACHING) {
445 biofinish(bp, NULL, ENXIO);
449 /* if it's a null transfer, return immediatly. */
450 if (bp->bio_bcount == 0) {
455 if (!(bp->bio_cmd == BIO_READ) && stp->flags & F_WRITEPROTECT) {
456 biofinish(bp, NULL, EPERM);
460 /* check for != blocksize requests */
461 if (bp->bio_bcount % stp->blksize) {
462 ata_prtdev(stp->device, "transfers must be multiple of %d\n",
464 biofinish(bp, NULL, EIO);
468 /* warn about transfers bigger than the device suggests */
469 if (bp->bio_bcount > stp->blksize * stp->cap.ctl) {
470 if ((stp->flags & F_CTL_WARN) == 0) {
471 ata_prtdev(stp->device, "WARNING: CTL exceeded %ld>%d\n",
472 bp->bio_bcount, stp->blksize * stp->cap.ctl);
473 stp->flags |= F_CTL_WARN;
477 mtx_lock(&stp->queue_mtx);
478 bioq_insert_tail(&stp->queue, bp);
479 mtx_unlock(&stp->queue_mtx);
480 ata_start(stp->device->channel);
484 ast_start(struct ata_device *atadev)
486 struct ast_softc *stp = atadev->softc;
488 struct ata_request *request;
492 mtx_lock(&stp->queue_mtx);
493 bp = bioq_first(&stp->queue);
495 mtx_unlock(&stp->queue_mtx);
498 bioq_remove(&stp->queue, bp);
499 mtx_unlock(&stp->queue_mtx);
501 bzero(ccb, sizeof(ccb));
503 if (bp->bio_cmd == BIO_READ)
506 ccb[0] = ATAPI_WRITE;
508 blkcount = bp->bio_bcount / stp->blksize;
511 ccb[2] = blkcount>>16;
512 ccb[3] = blkcount>>8;
515 if (!(request = ata_alloc_request())) {
516 biofinish(bp, NULL, ENOMEM);
519 request->device = atadev;
520 request->driver = bp;
521 bcopy(ccb, request->u.atapi.ccb,
522 (request->device->param->config & ATA_PROTO_MASK) ==
523 ATA_PROTO_ATAPI_12 ? 16 : 12);
524 request->data = bp->bio_data;
525 request->bytecount = blkcount * stp->blksize;
526 request->transfersize = min(request->bytecount, 65534);
527 request->timeout = (ccb[0] == ATAPI_WRITE_BIG) ? 180 : 120;
528 request->retries = 2;
529 request->callback = ast_done;
530 switch (bp->bio_cmd) {
532 request->flags |= (ATA_R_ATAPI | ATA_R_READ);
535 request->flags |= (ATA_R_ATAPI | ATA_R_WRITE);
538 ata_prtdev(atadev, "unknown BIO operation\n");
539 ata_free_request(request);
540 biofinish(bp, NULL, EIO);
543 devstat_start_transaction_bio(stp->stats, bp);
544 ata_queue_request(request);
548 ast_done(struct ata_request *request)
550 struct bio *bp = request->driver;
551 struct ast_softc *stp = request->device->softc;
553 /* finish up transfer */
554 if ((bp->bio_error = request->result))
555 bp->bio_flags |= BIO_ERROR;
556 if (bp->bio_cmd == BIO_WRITE)
557 stp->flags |= F_DATA_WRITTEN;
558 bp->bio_resid = bp->bio_bcount - request->donecount;
559 ast_total += (bp->bio_bcount - bp->bio_resid);
560 biofinish(bp, stp->stats, 0);
561 ata_free_request(request);
565 ast_mode_sense(struct ast_softc *stp, int page, void *pagebuf, int pagesize)
567 int8_t ccb[16] = { ATAPI_MODE_SENSE, 0x08, page, pagesize>>8, pagesize,
568 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
571 error = ata_atapicmd(stp->device, ccb, pagebuf, pagesize, ATA_R_READ, 10);
573 atapi_dump("ast: mode sense ", pagebuf, pagesize);
579 ast_mode_select(struct ast_softc *stp, void *pagebuf, int pagesize)
581 int8_t ccb[16] = { ATAPI_MODE_SELECT, 0x10, 0, pagesize>>8, pagesize,
582 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
585 ata_prtdev(stp->device, "modeselect pagesize=%d\n", pagesize);
586 atapi_dump("mode select ", pagebuf, pagesize);
588 return ata_atapicmd(stp->device, ccb, pagebuf, pagesize, 0, 10);
592 ast_write_filemark(struct ast_softc *stp, u_int8_t function)
594 int8_t ccb[16] = { ATAPI_WEOF, 0x01, 0, 0, function, 0, 0, 0,
595 0, 0, 0, 0, 0, 0, 0, 0 };
598 if (stp->flags & F_ONSTREAM)
599 ccb[4] = 0x00; /* only flush buffers supported */
602 if (stp->flags & F_FM_WRITTEN)
603 stp->flags &= ~F_DATA_WRITTEN;
605 stp->flags |= F_FM_WRITTEN;
608 error = ata_atapicmd(stp->device, ccb, NULL, 0, 0, 10);
611 return ast_wait_dsc(stp->device, 10*60);
615 ast_read_position(struct ast_softc *stp, int hard,
616 struct ast_readposition *position)
618 int8_t ccb[16] = { ATAPI_READ_POSITION, (hard ? 0x01 : 0), 0, 0, 0, 0, 0, 0,
619 0, 0, 0, 0, 0, 0, 0, 0 };
622 error = ata_atapicmd(stp->device, ccb, (caddr_t)position,
623 sizeof(struct ast_readposition), ATA_R_READ, 10);
624 position->tape = ntohl(position->tape);
625 position->host = ntohl(position->host);
630 ast_space(struct ast_softc *stp, u_int8_t function, int32_t count)
632 int8_t ccb[16] = { ATAPI_SPACE, function, count>>16, count>>8, count,
633 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
635 return ata_atapicmd(stp->device, ccb, NULL, 0, 0, 60*60);
639 ast_locate(struct ast_softc *stp, int hard, u_int32_t pos)
641 int8_t ccb[16] = { ATAPI_LOCATE, 0x01 | (hard ? 0x4 : 0), 0,
642 pos>>24, pos>>16, pos>>8, pos,
643 0, 0, 0, 0, 0, 0, 0, 0, 0 };
646 error = ata_atapicmd(stp->device, ccb, NULL, 0, 0, 10);
649 return ast_wait_dsc(stp->device, 60*60);
653 ast_prevent_allow(struct ast_softc *stp, int lock)
655 int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
656 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
658 return ata_atapicmd(stp->device, ccb, NULL, 0, 0, 30);
662 ast_load_unload(struct ast_softc *stp, u_int8_t function)
664 int8_t ccb[16] = { ATAPI_START_STOP, 0x01, 0, 0, function, 0, 0, 0,
665 0, 0, 0, 0, 0, 0, 0, 0 };
668 if ((function & ATAPI_SS_EJECT) && !stp->cap.eject)
670 error = ata_atapicmd(stp->device, ccb, NULL, 0, 0, 10);
673 tsleep((caddr_t)&error, PRIBIO, "astlu", 1 * hz);
674 if (function == ATAPI_SS_EJECT)
676 return ast_wait_dsc(stp->device, 60*60);
680 ast_rewind(struct ast_softc *stp)
682 int8_t ccb[16] = { ATAPI_REZERO, 0x01, 0, 0, 0, 0, 0, 0,
683 0, 0, 0, 0, 0, 0, 0, 0 };
686 error = ata_atapicmd(stp->device, ccb, NULL, 0, 0, 10);
689 return ast_wait_dsc(stp->device, 60*60);
693 ast_erase(struct ast_softc *stp)
695 int8_t ccb[16] = { ATAPI_ERASE, 3, 0, 0, 0, 0, 0, 0,
696 0, 0, 0, 0, 0, 0, 0, 0 };
699 if ((error = ast_rewind(stp)))
702 return ata_atapicmd(stp->device, ccb, NULL, 0, 0, 60*60);
706 ast_test_ready(struct ata_device *atadev)
708 int8_t ccb[16] = { ATAPI_TEST_UNIT_READY, 0, 0, 0, 0,
709 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
711 return ata_atapicmd(atadev, ccb, NULL, 0, 0, 30);
715 ast_wait_dsc(struct ata_device *atadev, int timeout)
718 int8_t ccb[16] = { ATAPI_POLL_DSC, 0, 0, 0, 0,
719 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
722 while (timeout > 0) {
723 error = ata_atapicmd(atadev, ccb, NULL, 0, 0, 0);
726 tsleep(&error, PRIBIO, "atpwt", hz / 2);