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