xref: /dragonfly/sys/dev/disk/nata/atapi-tape.c (revision caaec4e3)
1 /*-
2  * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/ata/atapi-tape.c,v 1.101 2006/01/05 21:27:19 sos Exp $
27  */
28 
29 #include "opt_ata.h"
30 
31 #include <sys/param.h>
32 #include <sys/bio.h>
33 #include <sys/buf.h>
34 #include <sys/bus.h>
35 #include <sys/conf.h>
36 #include <sys/device.h>
37 #include <sys/devicestat.h>
38 #include <sys/disk.h>
39 #include <sys/kernel.h>
40 #include <sys/libkern.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/mtio.h>
44 #include <sys/nata.h>
45 #include <sys/systm.h>
46 
47 #include "ata-all.h"
48 #include "atapi-tape.h"
49 #include "ata_if.h"
50 
51 /* local implementation, to trigger a warning */
52 static inline void
53 biofinish(struct bio *bp, struct bio *x __unused, int error)
54 {
55 	struct buf *bbp = bp->bio_buf;
56 
57 	bbp->b_flags |= B_ERROR;
58 	bbp->b_error = error;
59 	biodone(bp);
60 }
61 
62 /* device structure */
63 static  d_open_t        ast_open;
64 static  d_close_t       ast_close;
65 static  d_ioctl_t       ast_ioctl;
66 static  d_strategy_t    ast_strategy;
67 static struct dev_ops ast_ops = {
68 	{ "ast", 119, D_TAPE | D_TRACKCLOSE },
69 	.d_open =       ast_open,
70 	.d_close =      ast_close,
71 	.d_read =       physread,
72 	.d_write =      physwrite,
73 	.d_ioctl =      ast_ioctl,
74 	.d_strategy =   ast_strategy
75 };
76 
77 /* prototypes */
78 static int ast_sense(device_t);
79 static void ast_describe(device_t);
80 static void ast_done(struct ata_request *);
81 static int ast_mode_sense(device_t, int, void *, int);
82 static int ast_mode_select(device_t, void *, int);
83 static int ast_write_filemark(device_t, u_int8_t);
84 static int ast_read_position(device_t, int, struct ast_readposition *);
85 static int ast_space(device_t, u_int8_t, int32_t);
86 static int ast_locate(device_t, int, u_int32_t);
87 static int ast_prevent_allow(device_t, int);
88 static int ast_load_unload(device_t, u_int8_t);
89 static int ast_rewind(device_t);
90 static int ast_erase(device_t);
91 static int ast_test_ready(device_t);
92 static int ast_wait_dsc(device_t, int);
93 
94 /* internal vars */
95 static u_int64_t ast_total = 0;
96 static MALLOC_DEFINE(M_AST, "ast_driver", "ATAPI tape driver buffers");
97 
98 static int
99 ast_probe(device_t dev)
100 {
101     struct ata_device *atadev = device_get_softc(dev);
102 
103     if ((atadev->param.config & ATA_PROTO_ATAPI) &&
104 	(atadev->param.config & ATA_ATAPI_TYPE_MASK) == ATA_ATAPI_TYPE_TAPE)
105 	return 0;
106     else
107 	return ENXIO;
108 }
109 
110 static int
111 ast_attach(device_t dev)
112 {
113     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
114     struct ata_device *atadev = device_get_softc(dev);
115     struct ast_softc *stp;
116     struct ast_readposition position;
117     cdev_t cdev;
118 
119     stp = kmalloc(sizeof(struct ast_softc), M_AST, M_WAITOK | M_ZERO);
120     device_set_ivars(dev, stp);
121     ATA_SETMODE(device_get_parent(dev), dev);
122 
123     if (ast_sense(dev)) {
124 	device_set_ivars(dev, NULL);
125 	kfree(stp, M_AST);
126 	return ENXIO;
127     }
128     if (!strcmp(atadev->param.model, "OnStream DI-30")) {
129 	struct ast_transferpage transfer;
130 	struct ast_identifypage identify;
131 
132 	stp->flags |= F_ONSTREAM;
133 	bzero(&transfer, sizeof(struct ast_transferpage));
134 	ast_mode_sense(dev, ATAPI_TAPE_TRANSFER_PAGE,
135 		       &transfer, sizeof(transfer));
136 	bzero(&identify, sizeof(struct ast_identifypage));
137 	ast_mode_sense(dev, ATAPI_TAPE_IDENTIFY_PAGE,
138 		       &identify, sizeof(identify));
139 	strncpy(identify.ident, "FBSD", 4);
140 	ast_mode_select(dev, &identify, sizeof(identify));
141 	ast_read_position(dev, 0, &position);
142     }
143 
144     devstat_add_entry(&stp->stats, "ast", device_get_unit(dev), DEV_BSIZE,
145 		      DEVSTAT_NO_ORDERED_TAGS,
146 		      DEVSTAT_TYPE_SEQUENTIAL | DEVSTAT_TYPE_IF_IDE,
147 		      DEVSTAT_PRIORITY_TAPE);
148     cdev = make_dev(&ast_ops, 2 * device_get_unit(dev), UID_ROOT, GID_OPERATOR,
149 		    0640, "ast%d", device_get_unit(dev));
150     reference_dev(cdev);
151     cdev->si_drv1 = dev;
152     if (ch->dma)
153 	cdev->si_iosize_max = ch->dma->max_iosize;
154     else
155 	cdev->si_iosize_max = min(MAXPHYS,64*1024);
156     stp->cdev1 = cdev;
157     cdev = make_dev(&ast_ops, 2 * device_get_unit(dev) + 1, UID_ROOT,
158 		    GID_OPERATOR, 0640, "nast%d", device_get_unit(dev));
159     reference_dev(cdev);
160     cdev->si_drv1 = dev;
161     if (ch->dma)
162 	cdev->si_iosize_max = ch->dma->max_iosize;
163     else
164 	cdev->si_iosize_max = min(MAXPHYS,64*1024);
165     stp->cdev2 = cdev;
166 
167     /* announce we are here and ready */
168     ast_describe(dev);
169     return 0;
170 }
171 
172 static int
173 ast_detach(device_t dev)
174 {
175     struct ast_softc *stp = device_get_ivars(dev);
176 
177     /* detroy devices from the system so we dont get any further requests */
178     destroy_dev(stp->cdev1);
179     destroy_dev(stp->cdev2);
180 
181     /* fail requests on the queue and any thats "in flight" for this device */
182     ata_fail_requests(dev);
183 
184     /* dont leave anything behind */
185     dev_ops_remove_minor(&ast_ops, /*dkunitmask(), */dkmakeunit(device_get_unit(dev)));
186     devstat_remove_entry(&stp->stats);
187     device_set_ivars(dev, NULL);
188     kfree(stp, M_AST);
189     return 0;
190 }
191 
192 static void
193 ast_shutdown(device_t dev)
194 {
195     struct ata_device *atadev = device_get_softc(dev);
196 
197     if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
198 	ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
199 }
200 
201 static int
202 ast_reinit(device_t dev)
203 {
204     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
205     struct ata_device *atadev = device_get_softc(dev);
206     struct ast_softc *stp = device_get_ivars(dev);
207 
208     if (((atadev->unit == ATA_MASTER) && !(ch->devices & ATA_ATAPI_MASTER)) ||
209 	((atadev->unit == ATA_SLAVE) && !(ch->devices & ATA_ATAPI_SLAVE))) {
210 	device_set_ivars(dev, NULL);
211 	kfree(stp, M_AST);
212 	return 1;
213     }
214     ATA_SETMODE(device_get_parent(dev), dev);
215     return 0;
216 }
217 
218 static int
219 ast_open(struct dev_open_args *ap)
220 {
221     device_t dev = ap->a_head.a_dev->si_drv1;
222     struct ata_device *atadev = device_get_softc(dev);
223     struct ast_softc *stp = device_get_ivars(dev);
224 
225     if (!stp)
226 	return ENXIO;
227     if (!device_is_attached(dev))
228 	return EBUSY;
229 
230     ast_test_ready(dev);
231     if (stp->cap.lock)
232 	ast_prevent_allow(dev, 1);
233     if (ast_sense(dev))
234 	device_printf(dev, "sense media type failed\n");
235 
236     atadev->flags &= ~ATA_D_MEDIA_CHANGED;
237     stp->flags &= ~(F_DATA_WRITTEN | F_FM_WRITTEN);
238     ast_total = 0;
239     return 0;
240 }
241 
242 static int
243 ast_close(struct dev_close_args *ap)
244 {
245     device_t dev = ap->a_head.a_dev->si_drv1;
246     struct ast_softc *stp = device_get_ivars(dev);
247     cdev_t cdev = ap->a_head.a_dev;
248 
249     /* flush buffers, some drives fail here, they should report ctl = 0 */
250     if (stp->cap.ctl && (stp->flags & F_DATA_WRITTEN))
251 	ast_write_filemark(dev, 0);
252 
253     /* write filemark if data written to tape */
254     if (!(stp->flags & F_ONSTREAM) &&
255 	(stp->flags & (F_DATA_WRITTEN | F_FM_WRITTEN)) == F_DATA_WRITTEN)
256 	ast_write_filemark(dev, ATAPI_WF_WRITE);
257 
258     /* if minor is even rewind on close */
259     if (!(minor(cdev) & 0x01))
260 	ast_rewind(dev);
261 
262     if (stp->cap.lock && count_dev(cdev) == 1)
263 	ast_prevent_allow(dev, 0);
264 
265     stp->flags &= ~F_CTL_WARN;
266 #ifdef AST_DEBUG
267     device_printf(dev, "%ju total bytes transferred\n", (uintmax_t)ast_total);
268 #endif
269     return 0;
270 }
271 
272 static int
273 ast_ioctl(struct dev_ioctl_args *ap)
274 {
275     device_t dev = ap->a_head.a_dev->si_drv1;
276     struct ast_softc *stp = device_get_ivars(dev);
277     int error = 0;
278 
279     switch (ap->a_cmd) {
280     case MTIOCGET:
281 	{
282 	    struct mtget *g = (struct mtget *)ap->a_data;
283 
284 	    bzero(g, sizeof(struct mtget));
285 	    g->mt_type = 7;
286 	    g->mt_density = 1;
287 	    g->mt_blksiz = stp->blksize;
288 	    g->mt_comp = stp->cap.compress;
289 	    g->mt_density0 = 0; g->mt_density1 = 0;
290 	    g->mt_density2 = 0; g->mt_density3 = 0;
291 	    g->mt_blksiz0 = 0; g->mt_blksiz1 = 0;
292 	    g->mt_blksiz2 = 0; g->mt_blksiz3 = 0;
293 	    g->mt_comp0 = 0; g->mt_comp1 = 0;
294 	    g->mt_comp2 = 0; g->mt_comp3 = 0;
295 	}
296 	break;
297 
298     case MTIOCTOP:
299 	{
300 	    int i;
301 	    struct mtop *mt = (struct mtop *)ap->a_data;
302 
303 	    switch ((int16_t) (mt->mt_op)) {
304 
305 	    case MTWEOF:
306 		for (i=0; i < mt->mt_count && !error; i++)
307 		    error = ast_write_filemark(dev, ATAPI_WF_WRITE);
308 		break;
309 
310 	    case MTFSF:
311 		if (mt->mt_count)
312 		    error = ast_space(dev, ATAPI_SP_FM, mt->mt_count);
313 		break;
314 
315 	    case MTBSF:
316 		if (mt->mt_count)
317 		    error = ast_space(dev, ATAPI_SP_FM, -(mt->mt_count));
318 		break;
319 
320 	    case MTREW:
321 		error = ast_rewind(dev);
322 		break;
323 
324 	    case MTOFFL:
325 		error = ast_load_unload(dev, ATAPI_SS_EJECT);
326 		break;
327 
328 	    case MTNOP:
329 		error = ast_write_filemark(dev, 0);
330 		break;
331 
332 	    case MTERASE:
333 		error = ast_erase(dev);
334 		break;
335 
336 	    case MTEOD:
337 		error = ast_space(dev, ATAPI_SP_EOD, 0);
338 		break;
339 
340 	    case MTRETENS:
341 		error = ast_load_unload(dev, ATAPI_SS_RETENSION|ATAPI_SS_LOAD);
342 		break;
343 
344 	    case MTFSR:
345 	    case MTBSR:
346 	    case MTCACHE:
347 	    case MTNOCACHE:
348 	    case MTSETBSIZ:
349 	    case MTSETDNSTY:
350 	    case MTCOMP:
351 	    default:
352 		error = EINVAL;
353 	    }
354 	}
355 	break;
356 
357     case MTIOCRDSPOS:
358 	{
359 	    struct ast_readposition position;
360 
361 	    if ((error = ast_read_position(dev, 0, &position)))
362 		break;
363 	    *(u_int32_t *)ap->a_data = position.tape;
364 	}
365 	break;
366 
367     case MTIOCRDHPOS:
368 	{
369 	    struct ast_readposition position;
370 
371 	    if ((error = ast_read_position(dev, 1, &position)))
372 		break;
373 	    *(u_int32_t *)ap->a_data = position.tape;
374 	}
375 	break;
376 
377     case MTIOCSLOCATE:
378 	error = ast_locate(dev, 0, *(u_int32_t *)ap->a_data);
379 	break;
380 
381     case MTIOCHLOCATE:
382 	error = ast_locate(dev, 1, *(u_int32_t *)ap->a_data);
383 	break;
384 
385     default:
386 	error = ata_device_ioctl(dev, ap->a_cmd, ap->a_data);
387     }
388     return error;
389 }
390 
391 static int
392 ast_strategy(struct dev_strategy_args *ap)
393 {
394     device_t dev = ap->a_head.a_dev->si_drv1;
395     struct bio *bp = ap->a_bio;
396     struct buf *bbp = bp->bio_buf;
397     struct ata_device *atadev = device_get_softc(dev);
398     struct ast_softc *stp = device_get_ivars(dev);
399     struct ata_request *request;
400     u_int32_t blkcount;
401     int8_t ccb[16];
402 
403     /* if it's a null transfer, return immediatly. */
404     if (bbp->b_bcount == 0) {
405 	bbp->b_resid = 0;
406 	biodone(bp);
407 	return 0;
408     }
409     if (!(bbp->b_cmd == BUF_CMD_READ) && (stp->flags & F_WRITEPROTECT)) {
410 	biofinish(bp, NULL, EPERM);
411 	return 0;
412     }
413     if (bbp->b_cmd != BUF_CMD_READ && bbp->b_cmd != BUF_CMD_WRITE) {
414 	biofinish(bp, NULL, EIO);
415 	return 0;
416     }
417 
418     /* check for != blocksize requests */
419     if (bbp->b_bcount % stp->blksize) {
420 	device_printf(dev, "transfers must be multiple of %d\n", stp->blksize);
421 	biofinish(bp, NULL, EIO);
422 	return 0;
423     }
424 
425     /* warn about transfers bigger than the device suggests */
426     if (bbp->b_bcount > stp->blksize * stp->cap.ctl) {
427 	if ((stp->flags & F_CTL_WARN) == 0) {
428 	    device_printf(dev, "WARNING: CTL exceeded %d>%d\n",
429 			  bbp->b_bcount, stp->blksize * stp->cap.ctl);
430 	    stp->flags |= F_CTL_WARN;
431 	}
432     }
433 
434     bzero(ccb, sizeof(ccb));
435 
436     if (bbp->b_cmd == BUF_CMD_READ)
437 	ccb[0] = ATAPI_READ;
438     else
439 	ccb[0] = ATAPI_WRITE;
440 
441     blkcount = bbp->b_bcount / stp->blksize;
442 
443     ccb[1] = 1;
444     ccb[2] = blkcount >> 16;
445     ccb[3] = blkcount >> 8;
446     ccb[4] = blkcount;
447 
448     if (!(request = ata_alloc_request())) {
449 	biofinish(bp, NULL, ENOMEM);
450 	return 0;
451     }
452     request->dev = dev;
453     request->bio = bp;
454     bcopy(ccb, request->u.atapi.ccb,
455 	  (atadev->param.config & ATA_PROTO_MASK) ==
456 	  ATA_PROTO_ATAPI_12 ? 16 : 12);
457     request->data = bbp->b_data;
458     request->bytecount = blkcount * stp->blksize;
459     request->transfersize = min(request->bytecount, 65534);
460     request->timeout = (ccb[0] == ATAPI_WRITE_BIG) ? 180 : 120;
461     request->retries = 2;
462     request->callback = ast_done;
463 
464     switch (bbp->b_cmd) {
465     case BUF_CMD_READ:
466 	request->flags |= (ATA_R_ATAPI | ATA_R_READ);
467 	break;
468     case BUF_CMD_WRITE:
469 	request->flags |= (ATA_R_ATAPI | ATA_R_WRITE);
470 	break;
471     default:
472 	panic("bbp->b_cmd");
473     }
474     devstat_start_transaction(&stp->stats);
475     ata_queue_request(request);
476     return 0;
477 }
478 
479 static void
480 ast_done(struct ata_request *request)
481 {
482     struct ast_softc *stp = device_get_ivars(request->dev);
483     struct bio *bp = request->bio;
484     struct buf *bbp = bp->bio_buf;
485 
486     /* finish up transfer */
487     if ((bbp->b_error = request->result))
488 	bbp->b_flags |= B_ERROR;
489     if (bbp->b_cmd == BUF_CMD_WRITE)
490 	stp->flags |= F_DATA_WRITTEN;
491     bbp->b_resid = bbp->b_bcount - request->donecount;
492     ast_total += (bbp->b_bcount - bbp->b_resid);
493     devstat_end_transaction_buf(&stp->stats, bbp);
494     biodone(bp);
495     ata_free_request(request);
496 }
497 
498 static int
499 ast_sense(device_t dev)
500 {
501     struct ast_softc *stp = device_get_ivars(dev);
502     int count;
503 
504     /* get drive capabilities, some bugridden drives needs this repeated */
505     for (count = 0 ; count < 5 ; count++) {
506 	if (!ast_mode_sense(dev, ATAPI_TAPE_CAP_PAGE,
507 			    &stp->cap, sizeof(stp->cap)) &&
508 	    stp->cap.page_code == ATAPI_TAPE_CAP_PAGE) {
509 	    if (stp->cap.blk32k)
510 		stp->blksize = 32768;
511 	    if (stp->cap.blk1024)
512 		stp->blksize = 1024;
513 	    if (stp->cap.blk512)
514 		stp->blksize = 512;
515 	    if (!stp->blksize)
516 		continue;
517 	    stp->cap.max_speed = ntohs(stp->cap.max_speed);
518 	    stp->cap.max_defects = ntohs(stp->cap.max_defects);
519 	    stp->cap.ctl = ntohs(stp->cap.ctl);
520 	    stp->cap.speed = ntohs(stp->cap.speed);
521 	    stp->cap.buffer_size = ntohs(stp->cap.buffer_size);
522 	    return 0;
523 	}
524     }
525     return 1;
526 }
527 
528 static int
529 ast_mode_sense(device_t dev, int page, void *pagebuf, int pagesize)
530 {
531     int8_t ccb[16] = { ATAPI_MODE_SENSE, 0x08, page, pagesize>>8, pagesize,
532 		       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
533     int error;
534 
535     error = ata_atapicmd(dev, ccb, pagebuf, pagesize, ATA_R_READ, 10);
536     return error;
537 }
538 
539 static int
540 ast_mode_select(device_t dev, void *pagebuf, int pagesize)
541 {
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 };
544 
545     return ata_atapicmd(dev, ccb, pagebuf, pagesize, 0, 10);
546 }
547 
548 static int
549 ast_write_filemark(device_t dev, u_int8_t function)
550 {
551     struct ast_softc *stp = device_get_ivars(dev);
552     int8_t ccb[16] = { ATAPI_WEOF, 0x01, 0, 0, function, 0, 0, 0,
553 		       0, 0, 0, 0, 0, 0, 0, 0 };
554     int error;
555 
556     if (stp->flags & F_ONSTREAM)
557 	ccb[4] = 0x00;          /* only flush buffers supported */
558     else {
559 	if (function) {
560 	    if (stp->flags & F_FM_WRITTEN)
561 		stp->flags &= ~F_DATA_WRITTEN;
562 	    else
563 		stp->flags |= F_FM_WRITTEN;
564 	}
565     }
566     error = ata_atapicmd(dev, ccb, NULL, 0, 0, 10);
567     if (error)
568 	return error;
569     return ast_wait_dsc(dev, 10*60);
570 }
571 
572 static int
573 ast_read_position(device_t dev, int hard, struct ast_readposition *position)
574 {
575     int8_t ccb[16] = { ATAPI_READ_POSITION, (hard ? 0x01 : 0), 0, 0, 0, 0, 0, 0,
576 		       0, 0, 0, 0, 0, 0, 0, 0 };
577     int error;
578 
579     error = ata_atapicmd(dev, ccb, (caddr_t)position,
580 			 sizeof(struct ast_readposition), ATA_R_READ, 10);
581     position->tape = ntohl(position->tape);
582     position->host = ntohl(position->host);
583     return error;
584 }
585 
586 static int
587 ast_space(device_t dev, u_int8_t function, int32_t count)
588 {
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 };
591 
592     return ata_atapicmd(dev, ccb, NULL, 0, 0, 60*60);
593 }
594 
595 static int
596 ast_locate(device_t dev, int hard, u_int32_t pos)
597 {
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 };
601     int error;
602 
603     error = ata_atapicmd(dev, ccb, NULL, 0, 0, 10);
604     if (error)
605 	return error;
606     return ast_wait_dsc(dev, 60*60);
607 }
608 
609 static int
610 ast_prevent_allow(device_t dev, int lock)
611 {
612     int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
613 		       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
614 
615     return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
616 }
617 
618 static int
619 ast_load_unload(device_t dev, u_int8_t function)
620 {
621     struct ast_softc *stp = device_get_ivars(dev);
622     int8_t ccb[16] = { ATAPI_START_STOP, 0x01, 0, 0, function, 0, 0, 0,
623 		       0, 0, 0, 0, 0, 0, 0, 0 };
624     int error;
625 
626     if ((function & ATAPI_SS_EJECT) && !stp->cap.eject)
627 	return 0;
628     error = ata_atapicmd(dev, ccb, NULL, 0, 0, 10);
629     if (error)
630 	return error;
631     tsleep((caddr_t)&error, 0, "astlu", 1 * hz);
632     if (function == ATAPI_SS_EJECT)
633 	return 0;
634     return ast_wait_dsc(dev, 60*60);
635 }
636 
637 static int
638 ast_rewind(device_t dev)
639 {
640     int8_t ccb[16] = { ATAPI_REZERO, 0x01, 0, 0, 0, 0, 0, 0,
641 		       0, 0, 0, 0, 0, 0, 0, 0 };
642     int error;
643 
644     error = ata_atapicmd(dev, ccb, NULL, 0, 0, 10);
645     if (error)
646 	return error;
647     return ast_wait_dsc(dev, 60*60);
648 }
649 
650 static int
651 ast_erase(device_t dev)
652 {
653     int8_t ccb[16] = { ATAPI_ERASE, 3, 0, 0, 0, 0, 0, 0,
654 		       0, 0, 0, 0, 0, 0, 0, 0 };
655     int error;
656 
657     if ((error = ast_rewind(dev)))
658 	return error;
659 
660     return ata_atapicmd(dev, ccb, NULL, 0, 0, 60*60);
661 }
662 
663 static int
664 ast_test_ready(device_t dev)
665 {
666     int8_t ccb[16] = { ATAPI_TEST_UNIT_READY, 0, 0, 0, 0,
667 		       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
668 
669     return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
670 }
671 
672 static int
673 ast_wait_dsc(device_t dev, int timeout)
674 {
675     int error = 0;
676     int8_t ccb[16] = { ATAPI_POLL_DSC, 0, 0, 0, 0,
677 		       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
678 
679     timeout *= hz;
680     while (timeout > 0) {
681 	error = ata_atapicmd(dev, ccb, NULL, 0, 0, 0);
682 	if (error != EBUSY)
683 	    break;
684 	tsleep(&error, 0, "atpwt", hz / 2);
685 	timeout -= (hz / 2);
686     }
687     return error;
688 }
689 
690 static void
691 ast_describe(device_t dev)
692 {
693     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
694     struct ata_device *atadev = device_get_softc(dev);
695     struct ast_softc *stp = device_get_ivars(dev);
696 
697     if (bootverbose) {
698 	device_printf(dev, "<%.40s/%.8s> tape drive at ata%d as %s\n",
699 		      atadev->param.model, atadev->param.revision,
700 		      device_get_unit(ch->dev), ata_unit2str(atadev));
701 	device_printf(dev, "%dKB/s, ", stp->cap.max_speed);
702 	kprintf("transfer limit %d blk%s, ",
703 	       stp->cap.ctl, (stp->cap.ctl > 1) ? "s" : "");
704 	kprintf("%dKB buffer, ", (stp->cap.buffer_size * DEV_BSIZE) / 1024);
705 	kprintf("%s\n", ata_mode2str(atadev->mode));
706 	device_printf(dev, "Medium: ");
707 	switch (stp->cap.medium_type) {
708 	    case 0x00:
709 		kprintf("none"); break;
710 	    case 0x17:
711 		kprintf("Travan 1 (400 Mbyte)"); break;
712 	    case 0xb6:
713 		kprintf("Travan 4 (4 Gbyte)"); break;
714 	    case 0xda:
715 		kprintf("OnStream ADR (15Gyte)"); break;
716 	    default:
717 		kprintf("unknown (0x%x)", stp->cap.medium_type);
718 	}
719 	if (stp->cap.readonly) kprintf(", readonly");
720 	if (stp->cap.reverse) kprintf(", reverse");
721 	if (stp->cap.eformat) kprintf(", eformat");
722 	if (stp->cap.qfa) kprintf(", qfa");
723 	if (stp->cap.lock) kprintf(", lock");
724 	if (stp->cap.locked) kprintf(", locked");
725 	if (stp->cap.prevent) kprintf(", prevent");
726 	if (stp->cap.eject) kprintf(", eject");
727 	if (stp->cap.disconnect) kprintf(", disconnect");
728 	if (stp->cap.ecc) kprintf(", ecc");
729 	if (stp->cap.compress) kprintf(", compress");
730 	if (stp->cap.blk512) kprintf(", 512b");
731 	if (stp->cap.blk1024) kprintf(", 1024b");
732 	if (stp->cap.blk32k) kprintf(", 32kb");
733 	kprintf("\n");
734     }
735     else {
736 	device_printf(dev, "TAPE <%.40s/%.8s> at ata%d-%s %s\n",
737 		      atadev->param.model, atadev->param.revision,
738 		      device_get_unit(ch->dev), ata_unit2str(atadev),
739 		      ata_mode2str(atadev->mode));
740     }
741 }
742 
743 static device_method_t ast_methods[] = {
744     /* device interface */
745     DEVMETHOD(device_probe,     ast_probe),
746     DEVMETHOD(device_attach,    ast_attach),
747     DEVMETHOD(device_detach,    ast_detach),
748     DEVMETHOD(device_shutdown,  ast_shutdown),
749 
750     /* ATA methods */
751     DEVMETHOD(ata_reinit,       ast_reinit),
752 
753     DEVMETHOD_END
754 };
755 
756 static driver_t ast_driver = {
757     "ast",
758     ast_methods,
759     0,
760 };
761 
762 static devclass_t ast_devclass;
763 
764 DRIVER_MODULE(ast, ata, ast_driver, ast_devclass, NULL, NULL);
765 MODULE_VERSION(ast, 1);
766 MODULE_DEPEND(ast, ata, 1, 1, 1);
767