705ae77522fff9a5914e137c0a648d247a31cbf5
[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.4 2008/08/30 02:56:11 dillon 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     stp = kmalloc(sizeof(struct ast_softc), M_AST, M_WAITOK | M_ZERO);
110     device_set_ivars(dev, stp);
111     ATA_SETMODE(device_get_parent(dev), dev);
112
113     if (ast_sense(dev)) {
114         device_set_ivars(dev, NULL);
115         kfree(stp, M_AST);
116         return ENXIO;
117     }
118     if (!strcmp(atadev->param.model, "OnStream DI-30")) {
119         struct ast_transferpage transfer;
120         struct ast_identifypage identify;
121
122         stp->flags |= F_ONSTREAM;
123         bzero(&transfer, sizeof(struct ast_transferpage));
124         ast_mode_sense(dev, ATAPI_TAPE_TRANSFER_PAGE,
125                        &transfer, sizeof(transfer));
126         bzero(&identify, sizeof(struct ast_identifypage));
127         ast_mode_sense(dev, ATAPI_TAPE_IDENTIFY_PAGE,
128                        &identify, sizeof(identify));
129         strncpy(identify.ident, "FBSD", 4);
130         ast_mode_select(dev, &identify, sizeof(identify));
131         ast_read_position(dev, 0, &position);
132     }
133
134     devstat_add_entry(&stp->stats, "ast", device_get_unit(dev), DEV_BSIZE,
135                       DEVSTAT_NO_ORDERED_TAGS,
136                       DEVSTAT_TYPE_SEQUENTIAL | DEVSTAT_TYPE_IF_IDE,
137                       DEVSTAT_PRIORITY_TAPE);
138     cdev = make_dev(&ast_ops, 2 * device_get_unit(dev), UID_ROOT, GID_OPERATOR,
139                     0640, "ast%d", device_get_unit(dev));
140     reference_dev(cdev);
141     cdev->si_drv1 = dev;
142     if (ch->dma)
143         cdev->si_iosize_max = ch->dma->max_iosize;
144     else
145         cdev->si_iosize_max = DFLTPHYS;
146     stp->cdev1 = cdev;
147     cdev = make_dev(&ast_ops, 2 * device_get_unit(dev) + 1, UID_ROOT,
148                     GID_OPERATOR, 0640, "nast%d", device_get_unit(dev));
149     reference_dev(cdev);
150     cdev->si_drv1 = dev;
151     if (ch->dma)
152         cdev->si_iosize_max = ch->dma->max_iosize;
153     else
154         cdev->si_iosize_max = DFLTPHYS;
155     stp->cdev2 = cdev;
156
157     /* announce we are here and ready */
158     ast_describe(dev);
159     return 0;
160 }
161
162 static int      
163 ast_detach(device_t dev)
164 {   
165     struct ast_softc *stp = device_get_ivars(dev);
166     
167     /* detroy devices from the system so we dont get any further requests */
168     destroy_dev(stp->cdev1);
169     destroy_dev(stp->cdev2);
170
171     /* fail requests on the queue and any thats "in flight" for this device */
172     ata_fail_requests(dev);
173
174     /* dont leave anything behind */
175     dev_ops_remove_minor(&ast_ops, /*dkunitmask(), */dkmakeunit(device_get_unit(dev)));
176     devstat_remove_entry(&stp->stats);
177     device_set_ivars(dev, NULL);
178     kfree(stp, M_AST);
179     return 0;
180 }
181
182 static void
183 ast_shutdown(device_t dev)
184 {
185     struct ata_device *atadev = device_get_softc(dev);
186
187     if (atadev->param.support.command2 & ATA_SUPPORT_FLUSHCACHE)
188         ata_controlcmd(dev, ATA_FLUSHCACHE, 0, 0, 0);
189 }
190
191 static int
192 ast_reinit(device_t dev)
193 {
194     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
195     struct ata_device *atadev = device_get_softc(dev);
196     struct ast_softc *stp = device_get_ivars(dev);
197
198     if (((atadev->unit == ATA_MASTER) && !(ch->devices & ATA_ATAPI_MASTER)) ||
199         ((atadev->unit == ATA_SLAVE) && !(ch->devices & ATA_ATAPI_SLAVE))) {
200         device_set_ivars(dev, NULL);
201         kfree(stp, M_AST);
202         return 1;
203     }
204     ATA_SETMODE(device_get_parent(dev), dev);
205     return 0;
206 }
207
208 static int
209 ast_open(struct dev_open_args *ap)
210 {
211     device_t dev = ap->a_head.a_dev->si_drv1;
212     struct ata_device *atadev = device_get_softc(dev);
213     struct ast_softc *stp = device_get_ivars(dev);
214
215     if (!stp)
216         return ENXIO;
217     if (!device_is_attached(dev))
218         return EBUSY;
219
220     ast_test_ready(dev);
221     if (stp->cap.lock)
222         ast_prevent_allow(dev, 1);
223     if (ast_sense(dev))
224         device_printf(dev, "sense media type failed\n");
225
226     atadev->flags &= ~ATA_D_MEDIA_CHANGED;
227     stp->flags &= ~(F_DATA_WRITTEN | F_FM_WRITTEN);
228     ast_total = 0;
229     return 0;
230 }
231
232 static int 
233 ast_close(struct dev_close_args *ap)
234 {
235     device_t dev = ap->a_head.a_dev->si_drv1;
236     struct ast_softc *stp = device_get_ivars(dev);
237     cdev_t cdev = ap->a_head.a_dev;
238
239     /* flush buffers, some drives fail here, they should report ctl = 0 */
240     if (stp->cap.ctl && (stp->flags & F_DATA_WRITTEN))
241         ast_write_filemark(dev, 0);
242
243     /* write filemark if data written to tape */
244     if (!(stp->flags & F_ONSTREAM) &&
245         (stp->flags & (F_DATA_WRITTEN | F_FM_WRITTEN)) == F_DATA_WRITTEN)
246         ast_write_filemark(dev, ATAPI_WF_WRITE);
247
248     /* if minor is even rewind on close */
249     if (!(minor(cdev) & 0x01))
250         ast_rewind(dev);
251
252     if (stp->cap.lock && count_dev(cdev) == 1)
253         ast_prevent_allow(dev, 0);
254
255     stp->flags &= ~F_CTL_WARN;
256 #ifdef AST_DEBUG
257     device_printf(dev, "%llu total bytes transferred\n",
258                   (unsigned long long)ast_total);
259 #endif
260     return 0;
261 }
262
263 static int 
264 ast_ioctl(struct dev_ioctl_args *ap)
265 {
266     device_t dev = ap->a_head.a_dev->si_drv1;
267     struct ast_softc *stp = device_get_ivars(dev);
268     int error = 0;
269
270     switch (ap->a_cmd) {
271     case MTIOCGET:
272         {
273             struct mtget *g = (struct mtget *)ap->a_data;
274
275             bzero(g, sizeof(struct mtget));
276             g->mt_type = 7;
277             g->mt_density = 1;
278             g->mt_blksiz = stp->blksize;
279             g->mt_comp = stp->cap.compress;
280             g->mt_density0 = 0; g->mt_density1 = 0;
281             g->mt_density2 = 0; g->mt_density3 = 0;
282             g->mt_blksiz0 = 0; g->mt_blksiz1 = 0;
283             g->mt_blksiz2 = 0; g->mt_blksiz3 = 0;
284             g->mt_comp0 = 0; g->mt_comp1 = 0;
285             g->mt_comp2 = 0; g->mt_comp3 = 0;
286         }
287         break;   
288
289     case MTIOCTOP:
290         {       
291             int i;
292             struct mtop *mt = (struct mtop *)ap->a_data;
293
294             switch ((int16_t) (mt->mt_op)) {
295
296             case MTWEOF:
297                 for (i=0; i < mt->mt_count && !error; i++)
298                     error = ast_write_filemark(dev, ATAPI_WF_WRITE);
299                 break;
300
301             case MTFSF:
302                 if (mt->mt_count)
303                     error = ast_space(dev, ATAPI_SP_FM, mt->mt_count);
304                 break;
305
306             case MTBSF:
307                 if (mt->mt_count)
308                     error = ast_space(dev, ATAPI_SP_FM, -(mt->mt_count));
309                 break;
310
311             case MTREW:
312                 error = ast_rewind(dev);
313                 break;
314
315             case MTOFFL:
316                 error = ast_load_unload(dev, ATAPI_SS_EJECT);
317                 break;
318
319             case MTNOP:
320                 error = ast_write_filemark(dev, 0);
321                 break;
322
323             case MTERASE:
324                 error = ast_erase(dev);
325                 break;
326
327             case MTEOD:
328                 error = ast_space(dev, ATAPI_SP_EOD, 0);
329                 break;
330
331             case MTRETENS:
332                 error = ast_load_unload(dev, ATAPI_SS_RETENSION|ATAPI_SS_LOAD);
333                 break;
334
335             case MTFSR:         
336             case MTBSR:
337             case MTCACHE:
338             case MTNOCACHE:
339             case MTSETBSIZ:
340             case MTSETDNSTY:
341             case MTCOMP:
342             default:
343                 error = EINVAL;
344             }
345         }
346         break;
347
348     case MTIOCRDSPOS:
349         {
350             struct ast_readposition position;
351
352             if ((error = ast_read_position(dev, 0, &position)))
353                 break;
354             *(u_int32_t *)ap->a_data = position.tape;
355         }
356         break;
357
358     case MTIOCRDHPOS:
359         {
360             struct ast_readposition position;
361
362             if ((error = ast_read_position(dev, 1, &position)))
363                 break;
364             *(u_int32_t *)ap->a_data = position.tape;
365         }
366         break;
367
368     case MTIOCSLOCATE:
369         error = ast_locate(dev, 0, *(u_int32_t *)ap->a_data);
370         break;
371
372     case MTIOCHLOCATE:
373         error = ast_locate(dev, 1, *(u_int32_t *)ap->a_data);
374         break;
375
376     default:
377         error = ata_device_ioctl(dev, ap->a_cmd, ap->a_data);
378     }
379     return error;
380 }
381
382 static int
383 ast_strategy(struct dev_strategy_args *ap)
384 {
385     device_t dev = ap->a_head.a_dev->si_drv1;
386     struct bio *bp = ap->a_bio;
387     struct buf *bbp = bp->bio_buf;
388     struct ata_device *atadev = device_get_softc(dev);
389     struct ast_softc *stp = device_get_ivars(dev);
390     struct ata_request *request;
391     u_int32_t blkcount;
392     int8_t ccb[16];
393
394     /* if it's a null transfer, return immediatly. */
395     if (bbp->b_bcount == 0) {
396         bbp->b_resid = 0;
397         biodone(bp);
398         return 0;
399     }
400     if (!(bbp->b_cmd == BUF_CMD_READ) && (stp->flags & F_WRITEPROTECT)) {
401         bbp->b_flags |= B_ERROR;
402         bbp->b_error = EPERM;
403         biodone(bp);
404         return 0;
405     }
406     if (bbp->b_cmd != BUF_CMD_READ && bbp->b_cmd != BUF_CMD_WRITE) {
407         bbp->b_flags |= B_ERROR;
408         bbp->b_error = EIO;
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
463     switch (bbp->b_cmd) {
464     case BUF_CMD_READ:
465         request->flags |= (ATA_R_ATAPI | ATA_R_READ);
466         break;
467     case BUF_CMD_WRITE:
468         request->flags |= (ATA_R_ATAPI | ATA_R_WRITE);
469         break;
470     default:
471         panic("bbp->b_cmd");
472     }
473     devstat_start_transaction(&stp->stats);
474     ata_queue_request(request);
475     return 0;
476 }
477
478 static void 
479 ast_done(struct ata_request *request)
480 {
481     struct ast_softc *stp = device_get_ivars(request->dev);
482     struct bio *bp = request->bio;
483     struct buf *bbp = bp->bio_buf;
484
485     /* finish up transfer */
486     if ((bbp->b_error = request->result))
487         bbp->b_flags |= B_ERROR;
488     if (bbp->b_cmd == BUF_CMD_WRITE)
489         stp->flags |= F_DATA_WRITTEN;
490     bbp->b_resid = bbp->b_bcount - request->donecount;
491     ast_total += (bbp->b_bcount - bbp->b_resid);
492     devstat_end_transaction_buf(&stp->stats, bbp);
493     biodone(bp);
494     ata_free_request(request);
495 }
496
497 static int
498 ast_sense(device_t dev)
499 {
500     struct ast_softc *stp = device_get_ivars(dev);
501     int count;
502
503     /* get drive capabilities, some bugridden drives needs this repeated */
504     for (count = 0 ; count < 5 ; count++) {
505         if (!ast_mode_sense(dev, ATAPI_TAPE_CAP_PAGE,
506                             &stp->cap, sizeof(stp->cap)) &&
507             stp->cap.page_code == ATAPI_TAPE_CAP_PAGE) {
508             if (stp->cap.blk32k)
509                 stp->blksize = 32768;
510             if (stp->cap.blk1024)
511                 stp->blksize = 1024;
512             if (stp->cap.blk512)
513                 stp->blksize = 512;
514             if (!stp->blksize)
515                 continue;
516             stp->cap.max_speed = ntohs(stp->cap.max_speed);
517             stp->cap.max_defects = ntohs(stp->cap.max_defects);
518             stp->cap.ctl = ntohs(stp->cap.ctl);
519             stp->cap.speed = ntohs(stp->cap.speed);
520             stp->cap.buffer_size = ntohs(stp->cap.buffer_size);
521             return 0;
522         }
523     }
524     return 1;
525 }
526
527 static int
528 ast_mode_sense(device_t dev, int page, void *pagebuf, int pagesize)
529 {
530     int8_t ccb[16] = { ATAPI_MODE_SENSE, 0x08, page, pagesize>>8, pagesize,
531                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
532     int error;
533  
534     error = ata_atapicmd(dev, ccb, pagebuf, pagesize, ATA_R_READ, 10);
535     return error;
536 }
537
538 static int       
539 ast_mode_select(device_t dev, void *pagebuf, int pagesize)
540 {
541     int8_t ccb[16] = { ATAPI_MODE_SELECT, 0x10, 0, pagesize>>8, pagesize,
542                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
543      
544     return ata_atapicmd(dev, ccb, pagebuf, pagesize, 0, 10);
545 }
546
547 static int
548 ast_write_filemark(device_t dev, u_int8_t function)
549 {
550     struct ast_softc *stp = device_get_ivars(dev);
551     int8_t ccb[16] = { ATAPI_WEOF, 0x01, 0, 0, function, 0, 0, 0,
552                        0, 0, 0, 0, 0, 0, 0, 0 };
553     int error;
554
555     if (stp->flags & F_ONSTREAM)
556         ccb[4] = 0x00;          /* only flush buffers supported */
557     else {
558         if (function) {
559             if (stp->flags & F_FM_WRITTEN)
560                 stp->flags &= ~F_DATA_WRITTEN;
561             else
562                 stp->flags |= F_FM_WRITTEN;
563         }
564     }
565     error = ata_atapicmd(dev, ccb, NULL, 0, 0, 10);
566     if (error)
567         return error;
568     return ast_wait_dsc(dev, 10*60);
569 }
570
571 static int
572 ast_read_position(device_t dev, int hard, struct ast_readposition *position)
573 {
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 };
576     int error;
577
578     error = ata_atapicmd(dev, ccb, (caddr_t)position, 
579                          sizeof(struct ast_readposition), ATA_R_READ, 10);
580     position->tape = ntohl(position->tape);
581     position->host = ntohl(position->host);
582     return error;
583 }
584
585 static int
586 ast_space(device_t dev, u_int8_t function, int32_t count)
587 {
588     int8_t ccb[16] = { ATAPI_SPACE, function, count>>16, count>>8, count,
589                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
590
591     return ata_atapicmd(dev, ccb, NULL, 0, 0, 60*60);
592 }
593
594 static int
595 ast_locate(device_t dev, int hard, u_int32_t pos)
596 {
597     int8_t ccb[16] = { ATAPI_LOCATE, 0x01 | (hard ? 0x4 : 0), 0,
598                        pos>>24, pos>>16, pos>>8, pos,
599                        0, 0, 0, 0, 0, 0, 0, 0, 0 };
600     int error;
601
602     error = ata_atapicmd(dev, ccb, NULL, 0, 0, 10);
603     if (error)
604         return error;
605     return ast_wait_dsc(dev, 60*60);
606 }
607
608 static int
609 ast_prevent_allow(device_t dev, int lock)
610 {
611     int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
612                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
613
614     return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
615 }
616
617 static int
618 ast_load_unload(device_t dev, u_int8_t function)
619 {
620     struct ast_softc *stp = device_get_ivars(dev);
621     int8_t ccb[16] = { ATAPI_START_STOP, 0x01, 0, 0, function, 0, 0, 0,
622                        0, 0, 0, 0, 0, 0, 0, 0 };
623     int error;
624
625     if ((function & ATAPI_SS_EJECT) && !stp->cap.eject)
626         return 0;
627     error = ata_atapicmd(dev, ccb, NULL, 0, 0, 10);
628     if (error)
629         return error;
630     tsleep((caddr_t)&error, 0, "astlu", 1 * hz);
631     if (function == ATAPI_SS_EJECT)
632         return 0;
633     return ast_wait_dsc(dev, 60*60);
634 }
635
636 static int
637 ast_rewind(device_t dev)
638 {
639     int8_t ccb[16] = { ATAPI_REZERO, 0x01, 0, 0, 0, 0, 0, 0,
640                        0, 0, 0, 0, 0, 0, 0, 0 };
641     int error;
642
643     error = ata_atapicmd(dev, ccb, NULL, 0, 0, 10);
644     if (error)
645         return error;
646     return ast_wait_dsc(dev, 60*60);
647 }
648
649 static int
650 ast_erase(device_t dev)
651 {
652     int8_t ccb[16] = { ATAPI_ERASE, 3, 0, 0, 0, 0, 0, 0,
653                        0, 0, 0, 0, 0, 0, 0, 0 };
654     int error;
655
656     if ((error = ast_rewind(dev)))
657         return error;
658
659     return ata_atapicmd(dev, ccb, NULL, 0, 0, 60*60);
660 }
661
662 static int
663 ast_test_ready(device_t dev)
664 {
665     int8_t ccb[16] = { ATAPI_TEST_UNIT_READY, 0, 0, 0, 0,
666                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
667
668     return ata_atapicmd(dev, ccb, NULL, 0, 0, 30);
669 }
670
671 static int
672 ast_wait_dsc(device_t dev, int timeout)
673 {
674     int error = 0;
675     int8_t ccb[16] = { ATAPI_POLL_DSC, 0, 0, 0, 0,
676                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
677
678     timeout *= hz;
679     while (timeout > 0) {
680         error = ata_atapicmd(dev, ccb, NULL, 0, 0, 0);
681         if (error != EBUSY)
682             break;
683         tsleep(&error, 0, "atpwt", hz / 2);
684         timeout -= (hz / 2);
685     }
686     return error;
687 }
688
689 static void 
690 ast_describe(device_t dev)
691 {
692     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
693     struct ata_device *atadev = device_get_softc(dev);
694     struct ast_softc *stp = device_get_ivars(dev);
695
696     if (bootverbose) {
697         device_printf(dev, "<%.40s/%.8s> tape drive at ata%d as %s\n",
698                       atadev->param.model, atadev->param.revision,
699                       device_get_unit(ch->dev),
700                       (atadev->unit == ATA_MASTER) ? "master" : "slave");
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),
739                       (atadev->unit == ATA_MASTER) ? "master" : "slave",
740                       ata_mode2str(atadev->mode));
741     }
742 }
743
744 static device_method_t ast_methods[] = {
745     /* device interface */
746     DEVMETHOD(device_probe,     ast_probe),
747     DEVMETHOD(device_attach,    ast_attach),
748     DEVMETHOD(device_detach,    ast_detach),
749     DEVMETHOD(device_shutdown,  ast_shutdown),
750                            
751     /* ATA methods */
752     DEVMETHOD(ata_reinit,       ast_reinit),
753
754     { 0, 0 }
755 };
756             
757 static driver_t ast_driver = {
758     "ast",
759     ast_methods,
760     0,
761 };
762
763 static devclass_t ast_devclass;
764
765 DRIVER_MODULE(ast, ata, ast_driver, ast_devclass, NULL, NULL);
766 MODULE_VERSION(ast, 1);
767 MODULE_DEPEND(ast, ata, 1, 1, 1);