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