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