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.
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/malloc.h>
41 #include <sys/disklabel.h>
42 #include <sys/devicestat.h>
43 #include <machine/bus.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 bioq_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 = bioq_first(&stp->queue))) {
155 bioq_remove(&stp->queue, bp);
156 biofinish(bp, NULL, ENXIO);
158 destroy_dev(stp->dev1);
159 destroy_dev(stp->dev2);
160 devstat_remove_entry(&stp->stats);
161 ata_free_name(atadev);
162 ata_free_lun(&ast_lun_map, stp->lun);
164 atadev->driver = NULL;
168 ast_sense(struct ast_softc *stp)
170 int count, error = 0;
172 /* get drive capabilities, some drives needs this repeated */
173 for (count = 0 ; count < 5 ; count++) {
174 if (!(error = ast_mode_sense(stp, ATAPI_TAPE_CAP_PAGE,
175 &stp->cap, sizeof(stp->cap)))) {
177 stp->blksize = 32768;
178 if (stp->cap.blk1024)
184 stp->cap.max_speed = ntohs(stp->cap.max_speed);
185 stp->cap.max_defects = ntohs(stp->cap.max_defects);
186 stp->cap.ctl = ntohs(stp->cap.ctl);
187 stp->cap.speed = ntohs(stp->cap.speed);
188 stp->cap.buffer_size = ntohs(stp->cap.buffer_size);
196 ast_describe(struct ast_softc *stp)
199 ata_prtdev(stp->device, "<%.40s/%.8s> tape drive at ata%d as %s\n",
200 stp->device->param->model, stp->device->param->revision,
201 device_get_unit(stp->device->channel->dev),
202 (stp->device->unit == ATA_MASTER) ? "master" : "slave");
203 ata_prtdev(stp->device, "%dKB/s, ", stp->cap.max_speed);
204 printf("transfer limit %d blk%s, ",
205 stp->cap.ctl, (stp->cap.ctl > 1) ? "s" : "");
206 printf("%dKB buffer, ", (stp->cap.buffer_size * DEV_BSIZE) / 1024);
207 printf("%s\n", ata_mode2str(stp->device->mode));
208 ata_prtdev(stp->device, "Medium: ");
209 switch (stp->cap.medium_type) {
211 printf("none"); break;
213 printf("Travan 1 (400 Mbyte)"); break;
215 printf("Travan 4 (4 Gbyte)"); break;
217 printf("OnStream ADR (15Gyte)"); break;
219 printf("unknown (0x%x)", stp->cap.medium_type);
221 if (stp->cap.readonly) printf(", readonly");
222 if (stp->cap.reverse) printf(", reverse");
223 if (stp->cap.eformat) printf(", eformat");
224 if (stp->cap.qfa) printf(", qfa");
225 if (stp->cap.lock) printf(", lock");
226 if (stp->cap.locked) printf(", locked");
227 if (stp->cap.prevent) printf(", prevent");
228 if (stp->cap.eject) printf(", eject");
229 if (stp->cap.disconnect) printf(", disconnect");
230 if (stp->cap.ecc) printf(", ecc");
231 if (stp->cap.compress) printf(", compress");
232 if (stp->cap.blk512) printf(", 512b");
233 if (stp->cap.blk1024) printf(", 1024b");
234 if (stp->cap.blk32k) printf(", 32kb");
238 ata_prtdev(stp->device, "TAPE <%.40s> at ata%d-%s %s\n",
239 stp->device->param->model,
240 device_get_unit(stp->device->channel->dev),
241 (stp->device->unit == ATA_MASTER) ? "master" : "slave",
242 ata_mode2str(stp->device->mode));
247 astopen(dev_t dev, int flags, int fmt, struct thread *td)
249 struct ast_softc *stp = dev->si_drv1;
254 if (count_dev(dev) > 1)
257 atapi_test_ready(stp->device);
260 ast_prevent_allow(stp, 1);
263 ata_prtdev(stp->device, "sense media type failed\n");
265 stp->device->flags &= ~ATA_D_MEDIA_CHANGED;
266 stp->flags &= ~(F_DATA_WRITTEN | F_FM_WRITTEN);
272 astclose(dev_t dev, int flags, int fmt, struct thread *td)
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(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
302 struct ast_softc *stp = dev->si_drv1;
308 struct mtget *g = (struct mtget *) addr;
310 bzero(g, sizeof(struct mtget));
313 g->mt_blksiz = stp->blksize;
314 g->mt_comp = stp->cap.compress;
315 g->mt_density0 = 0; g->mt_density1 = 0;
316 g->mt_density2 = 0; g->mt_density3 = 0;
317 g->mt_blksiz0 = 0; g->mt_blksiz1 = 0;
318 g->mt_blksiz2 = 0; g->mt_blksiz3 = 0;
319 g->mt_comp0 = 0; g->mt_comp1 = 0;
320 g->mt_comp2 = 0; g->mt_comp3 = 0;
326 struct mtop *mt = (struct mtop *)addr;
328 switch ((int16_t) (mt->mt_op)) {
331 for (i=0; i < mt->mt_count && !error; i++)
332 error = ast_write_filemark(stp, WF_WRITE);
337 error = ast_space(stp, SP_FM, mt->mt_count);
342 error = ast_space(stp, SP_FM, -(mt->mt_count));
346 error = ast_rewind(stp);
350 error = ast_load_unload(stp, SS_EJECT);
354 error = ast_write_filemark(stp, 0);
358 error = ast_erase(stp);
362 error = ast_space(stp, SP_EOD, 0);
366 error = ast_load_unload(stp, SS_RETENSION | SS_LOAD);
383 struct ast_readposition position;
385 if ((error = ast_read_position(stp, 0, &position)))
387 *(u_int32_t *)addr = position.tape;
392 struct ast_readposition position;
394 if ((error = ast_read_position(stp, 1, &position)))
396 *(u_int32_t *)addr = position.tape;
400 error = ast_locate(stp, 0, *(u_int32_t *)addr);
403 error = ast_locate(stp, 1, *(u_int32_t *)addr);
412 aststrategy(struct bio *bp)
414 struct ast_softc *stp = bp->bio_dev->si_drv1;
417 if (stp->device->flags & ATA_D_DETACHING) {
418 biofinish(bp, NULL, ENXIO);
422 /* if it's a null transfer, return immediatly. */
423 if (bp->bio_bcount == 0) {
428 if (!(bp->bio_cmd == BIO_READ) && stp->flags & F_WRITEPROTECT) {
429 biofinish(bp, NULL, EPERM);
433 /* check for != blocksize requests */
434 if (bp->bio_bcount % stp->blksize) {
435 ata_prtdev(stp->device, "transfers must be multiple of %d\n",
437 biofinish(bp, NULL, EIO);
441 /* warn about transfers bigger than the device suggests */
442 if (bp->bio_bcount > stp->blksize * stp->cap.ctl) {
443 if ((stp->flags & F_CTL_WARN) == 0) {
444 ata_prtdev(stp->device, "WARNING: CTL exceeded %ld>%d\n",
445 bp->bio_bcount, stp->blksize * stp->cap.ctl);
446 stp->flags |= F_CTL_WARN;
451 bioq_insert_tail(&stp->queue, bp);
453 ata_start(stp->device->channel);
457 ast_start(struct ata_device *atadev)
459 struct ast_softc *stp = atadev->driver;
460 struct bio *bp = bioq_first(&stp->queue);
467 bzero(ccb, sizeof(ccb));
469 if (bp->bio_cmd == BIO_READ)
472 ccb[0] = ATAPI_WRITE;
474 bioq_remove(&stp->queue, bp);
475 blkcount = bp->bio_bcount / stp->blksize;
478 ccb[2] = blkcount>>16;
479 ccb[3] = blkcount>>8;
482 devstat_start_transaction(&stp->stats);
484 atapi_queue_cmd(stp->device, ccb, bp->bio_data, blkcount * stp->blksize,
485 (bp->bio_cmd == BIO_READ) ? ATPR_F_READ : 0,
490 ast_done(struct atapi_request *request)
492 struct bio *bp = request->driver;
493 struct ast_softc *stp = request->device->driver;
495 if (request->error) {
496 bp->bio_error = request->error;
497 bp->bio_flags |= BIO_ERROR;
500 if (!(bp->bio_cmd == BIO_READ))
501 stp->flags |= F_DATA_WRITTEN;
502 bp->bio_resid = bp->bio_bcount - request->donecount;
503 ast_total += (bp->bio_bcount - bp->bio_resid);
505 biofinish(bp, &stp->stats, 0);
510 ast_mode_sense(struct ast_softc *stp, 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 = atapi_queue_cmd(stp->device, ccb, pagebuf, pagesize, ATPR_F_READ,
519 atapi_dump("ast: mode sense ", pagebuf, pagesize);
525 ast_mode_select(struct ast_softc *stp, void *pagebuf, int pagesize)
527 int8_t ccb[16] = { ATAPI_MODE_SELECT, 0x10, 0, pagesize>>8, pagesize,
528 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
531 ata_prtdev(stp->device, "modeselect pagesize=%d\n", pagesize);
532 atapi_dump("mode select ", pagebuf, pagesize);
534 return atapi_queue_cmd(stp->device, ccb, pagebuf, pagesize, 0,
539 ast_write_filemark(struct ast_softc *stp, u_int8_t function)
541 int8_t ccb[16] = { ATAPI_WEOF, 0x01, 0, 0, function, 0, 0, 0,
542 0, 0, 0, 0, 0, 0, 0, 0 };
545 if (stp->flags & F_ONSTREAM)
546 ccb[4] = 0x00; /* only flush buffers supported */
549 if (stp->flags & F_FM_WRITTEN)
550 stp->flags &= ~F_DATA_WRITTEN;
552 stp->flags |= F_FM_WRITTEN;
555 error = atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
558 return atapi_wait_dsc(stp->device, 10*60);
562 ast_read_position(struct ast_softc *stp, int hard,
563 struct ast_readposition *position)
565 int8_t ccb[16] = { ATAPI_READ_POSITION, (hard ? 0x01 : 0), 0, 0, 0, 0, 0, 0,
566 0, 0, 0, 0, 0, 0, 0, 0 };
569 error = atapi_queue_cmd(stp->device, ccb, (caddr_t)position,
570 sizeof(struct ast_readposition), ATPR_F_READ, 10,
572 position->tape = ntohl(position->tape);
573 position->host = ntohl(position->host);
578 ast_space(struct ast_softc *stp, u_int8_t function, int32_t count)
580 int8_t ccb[16] = { ATAPI_SPACE, function, count>>16, count>>8, count,
581 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
583 return atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 60*60, NULL, NULL);
587 ast_locate(struct ast_softc *stp, int hard, u_int32_t pos)
589 int8_t ccb[16] = { ATAPI_LOCATE, 0x01 | (hard ? 0x4 : 0), 0,
590 pos>>24, pos>>16, pos>>8, pos,
591 0, 0, 0, 0, 0, 0, 0, 0, 0 };
594 error = atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
597 return atapi_wait_dsc(stp->device, 60*60);
601 ast_prevent_allow(struct ast_softc *stp, int lock)
603 int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
604 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
606 return atapi_queue_cmd(stp->device, ccb, NULL, 0, 0,30, NULL, NULL);
610 ast_load_unload(struct ast_softc *stp, u_int8_t function)
612 int8_t ccb[16] = { ATAPI_START_STOP, 0x01, 0, 0, function, 0, 0, 0,
613 0, 0, 0, 0, 0, 0, 0, 0 };
616 if ((function & SS_EJECT) && !stp->cap.eject)
618 error = atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
621 tsleep((caddr_t)&error, PRIBIO, "astlu", 1 * hz);
622 if (function == SS_EJECT)
624 return atapi_wait_dsc(stp->device, 60*60);
628 ast_rewind(struct ast_softc *stp)
630 int8_t ccb[16] = { ATAPI_REZERO, 0x01, 0, 0, 0, 0, 0, 0,
631 0, 0, 0, 0, 0, 0, 0, 0 };
634 error = atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
637 return atapi_wait_dsc(stp->device, 60*60);
641 ast_erase(struct ast_softc *stp)
643 int8_t ccb[16] = { ATAPI_ERASE, 3, 0, 0, 0, 0, 0, 0,
644 0, 0, 0, 0, 0, 0, 0, 0 };
647 if ((error = ast_rewind(stp)))
650 return atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 60*60, NULL, NULL);