0ae9469cd949e0516ed6c7c18e601992d99ac0d6
[dragonfly.git] / sys / dev / disk / ata / atapi-tape.c
1 /*-
2  * Copyright (c) 1998,1999,2000,2001,2002 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  * $FreeBSD: src/sys/dev/ata/atapi-tape.c,v 1.36.2.12 2002/07/31 11:19:26 sos Exp $
29  * $DragonFly: src/sys/dev/disk/ata/atapi-tape.c,v 1.4 2003/06/23 17:55:29 dillon Exp $
30  */
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/conf.h>
38 #include <sys/malloc.h>
39 #include <sys/buf.h>
40 #include <sys/bus.h>
41 #include <sys/mtio.h>
42 #include <sys/disklabel.h>
43 #include <sys/devicestat.h>
44 #include <sys/proc.h>
45 #include <sys/buf2.h>
46 #include <dev/ata/ata-all.h>
47 #include <dev/ata/atapi-all.h>
48 #include <dev/ata/atapi-tape.h>
49
50 /* device structures */
51 static  d_open_t        astopen;
52 static  d_close_t       astclose;
53 static  d_ioctl_t       astioctl;
54 static  d_strategy_t    aststrategy;
55 static struct cdevsw ast_cdevsw = {
56         /* open */      astopen,
57         /* close */     astclose,
58         /* read */      physread,
59         /* write */     physwrite,
60         /* ioctl */     astioctl,
61         /* poll */      nopoll,
62         /* mmap */      nommap,
63         /* strategy */  aststrategy,
64         /* name */      "ast",
65         /* maj */       119,
66         /* dump */      nodump,
67         /* psize */     nopsize,
68         /* flags */     D_TAPE | D_TRACKCLOSE,
69 };
70
71 /* prototypes */
72 static int ast_sense(struct ast_softc *);
73 static void ast_describe(struct ast_softc *);
74 static int ast_done(struct atapi_request *);
75 static int ast_mode_sense(struct ast_softc *, int, void *, int); 
76 static int ast_mode_select(struct ast_softc *, void *, int);
77 static int ast_write_filemark(struct ast_softc *, u_int8_t);
78 static int ast_read_position(struct ast_softc *, int, struct ast_readposition *);
79 static int ast_space(struct ast_softc *, u_int8_t, int32_t);
80 static int ast_locate(struct ast_softc *, int, u_int32_t);
81 static int ast_prevent_allow(struct ast_softc *stp, int);
82 static int ast_load_unload(struct ast_softc *, u_int8_t);
83 static int ast_rewind(struct ast_softc *);
84 static int ast_erase(struct ast_softc *);
85
86 /* internal vars */
87 static u_int32_t ast_lun_map = 0;
88 static u_int64_t ast_total = 0;
89 static MALLOC_DEFINE(M_AST, "AST driver", "ATAPI tape driver buffers");
90
91 int 
92 astattach(struct ata_device *atadev)
93 {
94     struct ast_softc *stp;
95     struct ast_readposition position;
96     dev_t dev;
97
98     stp = malloc(sizeof(struct ast_softc), M_AST, M_NOWAIT | M_ZERO);
99     if (!stp) {
100         ata_prtdev(atadev, "out of memory\n");
101         return 0;
102     }
103
104     stp->device = atadev;
105     stp->lun = ata_get_lun(&ast_lun_map);
106     ata_set_name(atadev, "ast", stp->lun);
107     bufq_init(&stp->queue);
108
109     if (ast_sense(stp)) {
110         free(stp, M_AST);
111         return 0;
112     }
113
114     if (!strcmp(atadev->param->model, "OnStream DI-30")) {
115         struct ast_transferpage transfer;
116         struct ast_identifypage identify;
117
118         stp->flags |= F_ONSTREAM;
119         bzero(&transfer, sizeof(struct ast_transferpage));
120         ast_mode_sense(stp, ATAPI_TAPE_TRANSFER_PAGE,
121                        &transfer, sizeof(transfer));
122         bzero(&identify, sizeof(struct ast_identifypage));
123         ast_mode_sense(stp, ATAPI_TAPE_IDENTIFY_PAGE,
124                        &identify, sizeof(identify));
125         strncpy(identify.ident, "FBSD", 4);
126         ast_mode_select(stp, &identify, sizeof(identify));
127         ast_read_position(stp, 0, &position);
128     }
129
130     devstat_add_entry(&stp->stats, "ast", stp->lun, DEV_BSIZE,
131                       DEVSTAT_NO_ORDERED_TAGS,
132                       DEVSTAT_TYPE_SEQUENTIAL | DEVSTAT_TYPE_IF_IDE,
133                       DEVSTAT_PRIORITY_TAPE);
134     dev = make_dev(&ast_cdevsw, dkmakeminor(stp->lun, 0, 0),
135                    UID_ROOT, GID_OPERATOR, 0640, "ast%d", stp->lun);
136     dev->si_drv1 = stp;
137     dev->si_iosize_max = 256 * DEV_BSIZE;
138     stp->dev1 = dev;
139     dev = make_dev(&ast_cdevsw, dkmakeminor(stp->lun, 0, 1),
140                    UID_ROOT, GID_OPERATOR, 0640, "nast%d", stp->lun);
141     dev->si_drv1 = stp;
142     dev->si_iosize_max = 256 * DEV_BSIZE;
143     stp->dev2 = dev;
144     stp->device->flags |= ATA_D_MEDIA_CHANGED;
145     ast_describe(stp);
146     atadev->driver = stp;
147     return 1;
148 }
149
150 void    
151 astdetach(struct ata_device *atadev)
152 {   
153     struct ast_softc *stp = atadev->driver;
154     struct buf *bp;
155     
156     while ((bp = bufq_first(&stp->queue))) {
157         bufq_remove(&stp->queue, bp);
158         bp->b_flags |= B_ERROR;
159         bp->b_error = ENXIO;
160         biodone(bp);
161     }
162     destroy_dev(stp->dev1);
163     destroy_dev(stp->dev2);
164     devstat_remove_entry(&stp->stats);
165     ata_free_name(atadev);
166     ata_free_lun(&ast_lun_map, stp->lun);
167     free(stp, M_AST);
168     atadev->driver = NULL;
169 }
170
171 static int
172 ast_sense(struct ast_softc *stp)
173 {
174     int count, error = 0;
175
176     /* get drive capabilities, some drives needs this repeated */
177     for (count = 0 ; count < 5 ; count++) {
178         if (!(error = ast_mode_sense(stp, ATAPI_TAPE_CAP_PAGE,
179                                      &stp->cap, sizeof(stp->cap)))) {
180             if (stp->cap.blk32k)
181                 stp->blksize = 32768;
182             if (stp->cap.blk1024)
183                 stp->blksize = 1024;
184             if (stp->cap.blk512)
185                 stp->blksize = 512;
186             if (!stp->blksize)
187                 continue;
188             stp->cap.max_speed = ntohs(stp->cap.max_speed);
189             stp->cap.max_defects = ntohs(stp->cap.max_defects);
190             stp->cap.ctl = ntohs(stp->cap.ctl);
191             stp->cap.speed = ntohs(stp->cap.speed);
192             stp->cap.buffer_size = ntohs(stp->cap.buffer_size);
193             return 0;
194         }
195     }
196     return 1;
197 }
198
199 static void 
200 ast_describe(struct ast_softc *stp)
201 {
202     if (bootverbose) {
203         ata_prtdev(stp->device, "<%.40s/%.8s> tape drive at ata%d as %s\n",
204                    stp->device->param->model, stp->device->param->revision,
205                    device_get_unit(stp->device->channel->dev),
206                    (stp->device->unit == ATA_MASTER) ? "master" : "slave");
207         ata_prtdev(stp->device, "%dKB/s, ", stp->cap.max_speed);
208         printf("transfer limit %d blk%s, ",
209                stp->cap.ctl, (stp->cap.ctl > 1) ? "s" : "");
210         printf("%dKB buffer, ", (stp->cap.buffer_size * DEV_BSIZE) / 1024);
211         printf("%s\n", ata_mode2str(stp->device->mode));
212         ata_prtdev(stp->device, "Medium: ");
213         switch (stp->cap.medium_type) {
214             case 0x00:
215                 printf("none"); break;
216             case 0x17:
217                 printf("Travan 1 (400 Mbyte)"); break;
218             case 0xb6:
219                 printf("Travan 4 (4 Gbyte)"); break;
220             case 0xda:
221                 printf("OnStream ADR (15Gyte)"); break;
222             default:
223                 printf("unknown (0x%x)", stp->cap.medium_type);
224         }
225         if (stp->cap.readonly) printf(", readonly");
226         if (stp->cap.reverse) printf(", reverse");
227         if (stp->cap.eformat) printf(", eformat");
228         if (stp->cap.qfa) printf(", qfa");
229         if (stp->cap.lock) printf(", lock");
230         if (stp->cap.locked) printf(", locked");
231         if (stp->cap.prevent) printf(", prevent");
232         if (stp->cap.eject) printf(", eject");
233         if (stp->cap.disconnect) printf(", disconnect");
234         if (stp->cap.ecc) printf(", ecc");
235         if (stp->cap.compress) printf(", compress");
236         if (stp->cap.blk512) printf(", 512b");
237         if (stp->cap.blk1024) printf(", 1024b");
238         if (stp->cap.blk32k) printf(", 32kb");
239         printf("\n");
240     }
241     else {
242         ata_prtdev(stp->device, "TAPE <%.40s> at ata%d-%s %s\n",
243                    stp->device->param->model,
244                    device_get_unit(stp->device->channel->dev),
245                    (stp->device->unit == ATA_MASTER) ? "master" : "slave",
246                    ata_mode2str(stp->device->mode));
247     }
248 }
249
250 static int
251 astopen(dev_t dev, int flags, int fmt, struct thread *td)
252 {
253     struct ast_softc *stp = dev->si_drv1;
254
255     if (!stp)
256         return ENXIO;
257
258     if (count_dev(dev) > 1)
259         return EBUSY;
260
261     atapi_test_ready(stp->device);
262
263     if (stp->cap.lock)
264         ast_prevent_allow(stp, 1);
265
266     if (ast_sense(stp))
267         ata_prtdev(stp->device, "sense media type failed\n");
268
269     stp->device->flags &= ~ATA_D_MEDIA_CHANGED;
270     stp->flags &= ~(F_DATA_WRITTEN | F_FM_WRITTEN);
271     ast_total = 0;
272     return 0;
273 }
274
275 static int 
276 astclose(dev_t dev, int flags, int fmt, struct thread *td)
277 {
278     struct ast_softc *stp = dev->si_drv1;
279
280     /* flush buffers, some drives fail here, they should report ctl = 0 */
281     if (stp->cap.ctl && (stp->flags & F_DATA_WRITTEN))
282         ast_write_filemark(stp, 0);
283
284     /* write filemark if data written to tape */
285     if (!(stp->flags & F_ONSTREAM) &&
286         (stp->flags & (F_DATA_WRITTEN | F_FM_WRITTEN)) == F_DATA_WRITTEN)
287         ast_write_filemark(stp, WF_WRITE);
288
289     /* if minor is even rewind on close */
290     if (!(minor(dev) & 0x01))
291         ast_rewind(stp);
292
293     if (stp->cap.lock && count_dev(dev) == 1)
294         ast_prevent_allow(stp, 0);
295
296     stp->flags &= F_CTL_WARN;
297 #ifdef AST_DEBUG
298     ata_prtdev(stp->device, "%llu total bytes transferred\n", ast_total);
299 #endif
300     return 0;
301 }
302
303 static int 
304 astioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
305 {
306     struct ast_softc *stp = dev->si_drv1;
307     int error = 0;
308
309     switch (cmd) {
310     case MTIOCGET:
311         {
312             struct mtget *g = (struct mtget *) addr;
313
314             bzero(g, sizeof(struct mtget));
315             g->mt_type = 7;
316             g->mt_density = 1;
317             g->mt_blksiz = stp->blksize;
318             g->mt_comp = stp->cap.compress;
319             g->mt_density0 = 0; g->mt_density1 = 0;
320             g->mt_density2 = 0; g->mt_density3 = 0;
321             g->mt_blksiz0 = 0; g->mt_blksiz1 = 0;
322             g->mt_blksiz2 = 0; g->mt_blksiz3 = 0;
323             g->mt_comp0 = 0; g->mt_comp1 = 0;
324             g->mt_comp2 = 0; g->mt_comp3 = 0;
325             break;       
326         }
327     case MTIOCTOP:
328         {       
329             int i;
330             struct mtop *mt = (struct mtop *)addr;
331
332             switch ((int16_t) (mt->mt_op)) {
333
334             case MTWEOF:
335                 for (i=0; i < mt->mt_count && !error; i++)
336                     error = ast_write_filemark(stp, WF_WRITE);
337                 break;
338
339             case MTFSF:
340                 if (mt->mt_count)
341                     error = ast_space(stp, SP_FM, mt->mt_count);
342                 break;
343
344             case MTBSF:
345                 if (mt->mt_count)
346                     error = ast_space(stp, SP_FM, -(mt->mt_count));
347                 break;
348
349             case MTREW:
350                 error = ast_rewind(stp);
351                 break;
352
353             case MTOFFL:
354                 error = ast_load_unload(stp, SS_EJECT);
355                 break;
356
357             case MTNOP:
358                 error = ast_write_filemark(stp, 0);
359                 break;
360
361             case MTERASE:
362                 error = ast_erase(stp);
363                 break;
364
365             case MTEOD:
366                 error = ast_space(stp, SP_EOD, 0);
367                 break;
368
369             case MTRETENS:
370                 error = ast_load_unload(stp, SS_RETENSION | SS_LOAD);
371                 break;
372
373             case MTFSR:         
374             case MTBSR:
375             case MTCACHE:
376             case MTNOCACHE:
377             case MTSETBSIZ:
378             case MTSETDNSTY:
379             case MTCOMP:
380             default:
381                 error = EINVAL;
382             }
383             break;
384         }
385     case MTIOCRDSPOS:
386         {
387             struct ast_readposition position;
388
389             if ((error = ast_read_position(stp, 0, &position)))
390                 break;
391             *(u_int32_t *)addr = position.tape;
392             break;
393         }
394     case MTIOCRDHPOS:
395         {
396             struct ast_readposition position;
397
398             if ((error = ast_read_position(stp, 1, &position)))
399                 break;
400             *(u_int32_t *)addr = position.tape;
401             break;
402         }
403     case MTIOCSLOCATE:
404         error = ast_locate(stp, 0, *(u_int32_t *)addr);
405         break;
406     case MTIOCHLOCATE:
407         error = ast_locate(stp, 1, *(u_int32_t *)addr);
408         break;
409     default:
410         error = ENOTTY;
411     }
412     return error;
413 }
414
415 static void 
416 aststrategy(struct buf *bp)
417 {
418     struct ast_softc *stp = bp->b_dev->si_drv1;
419     int s;
420
421     if (stp->device->flags & ATA_D_DETACHING) {
422         bp->b_flags |= B_ERROR;
423         bp->b_error = ENXIO;
424         biodone(bp);
425         return;
426     }
427
428     /* if it's a null transfer, return immediatly. */
429     if (bp->b_bcount == 0) {
430         bp->b_resid = 0;
431         biodone(bp);
432         return;
433     }
434     if (!(bp->b_flags & B_READ) && stp->flags & F_WRITEPROTECT) {
435         bp->b_flags |= B_ERROR;
436         bp->b_error = EPERM;
437         biodone(bp);
438         return;
439     }
440         
441     /* check for != blocksize requests */
442     if (bp->b_bcount % stp->blksize) {
443         ata_prtdev(stp->device, "transfers must be multiple of %d\n",
444                    stp->blksize);
445         bp->b_flags |= B_ERROR;
446         bp->b_error = EIO;
447         biodone(bp);
448         return;
449     }
450
451     /* warn about transfers bigger than the device suggests */
452     if (bp->b_bcount > stp->blksize * stp->cap.ctl) {    
453         if ((stp->flags & F_CTL_WARN) == 0) {
454             ata_prtdev(stp->device, "WARNING: CTL exceeded %ld>%d\n",
455                        bp->b_bcount, stp->blksize * stp->cap.ctl);
456             stp->flags |= F_CTL_WARN;
457         }
458     }
459
460     s = splbio();
461     bufq_insert_tail(&stp->queue, bp);
462     splx(s);
463     ata_start(stp->device->channel);
464 }
465
466 void 
467 ast_start(struct ata_device *atadev)
468 {
469     struct ast_softc *stp = atadev->driver;
470     struct buf *bp = bufq_first(&stp->queue);
471     u_int32_t blkcount;
472     int8_t ccb[16];
473     
474     if (!bp)
475         return;
476
477     bzero(ccb, sizeof(ccb));
478
479     if (bp->b_flags & B_READ)
480         ccb[0] = ATAPI_READ;
481     else
482         ccb[0] = ATAPI_WRITE;
483     
484     bufq_remove(&stp->queue, bp);
485     blkcount = bp->b_bcount / stp->blksize;
486
487     ccb[1] = 1;
488     ccb[2] = blkcount>>16;
489     ccb[3] = blkcount>>8;
490     ccb[4] = blkcount;
491
492     devstat_start_transaction(&stp->stats);
493
494     atapi_queue_cmd(stp->device, ccb, bp->b_data, blkcount * stp->blksize, 
495                     (bp->b_flags & B_READ) ? ATPR_F_READ : 0,
496                     120, ast_done, bp);
497 }
498
499 static int 
500 ast_done(struct atapi_request *request)
501 {
502     struct buf *bp = request->driver;
503     struct ast_softc *stp = request->device->driver;
504
505     if (request->error) {
506         bp->b_error = request->error;
507         bp->b_flags |= B_ERROR;
508     }
509     else {
510         if (!(bp->b_flags & B_READ))
511             stp->flags |= F_DATA_WRITTEN;
512         bp->b_resid = bp->b_bcount - request->donecount;
513         ast_total += (bp->b_bcount - bp->b_resid);
514     }
515     devstat_end_transaction_buf(&stp->stats, bp);
516     biodone(bp);
517     return 0;
518 }
519
520 static int
521 ast_mode_sense(struct ast_softc *stp, int page, void *pagebuf, int pagesize)
522 {
523     int8_t ccb[16] = { ATAPI_MODE_SENSE, 0x08, page, pagesize>>8, pagesize,
524                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
525     int error;
526  
527     error = atapi_queue_cmd(stp->device, ccb, pagebuf, pagesize, ATPR_F_READ,
528                             10, NULL, NULL);
529 #ifdef AST_DEBUG
530     atapi_dump("ast: mode sense ", pagebuf, pagesize);
531 #endif
532     return error;
533 }
534
535 static int       
536 ast_mode_select(struct ast_softc *stp, void *pagebuf, int pagesize)
537 {
538     int8_t ccb[16] = { ATAPI_MODE_SELECT, 0x10, 0, pagesize>>8, pagesize,
539                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
540      
541 #ifdef AST_DEBUG
542     ata_prtdev(stp->device, "modeselect pagesize=%d\n", pagesize);
543     atapi_dump("mode select ", pagebuf, pagesize);
544 #endif
545     return atapi_queue_cmd(stp->device, ccb, pagebuf, pagesize, 0,
546                            10, NULL, NULL);
547 }
548
549 static int
550 ast_write_filemark(struct ast_softc *stp, u_int8_t function)
551 {
552     int8_t ccb[16] = { ATAPI_WEOF, 0x01, 0, 0, function, 0, 0, 0,
553                        0, 0, 0, 0, 0, 0, 0, 0 };
554     int error;
555
556     if (stp->flags & F_ONSTREAM)
557         ccb[4] = 0x00;          /* only flush buffers supported */
558     else {
559         if (function) {
560             if (stp->flags & F_FM_WRITTEN)
561                 stp->flags &= ~F_DATA_WRITTEN;
562             else
563                 stp->flags |= F_FM_WRITTEN;
564         }
565     }
566     error = atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
567     if (error)
568         return error;
569     return atapi_wait_dsc(stp->device, 10*60);
570 }
571
572 static int
573 ast_read_position(struct ast_softc *stp, int hard,
574                   struct ast_readposition *position)
575 {
576     int8_t ccb[16] = { ATAPI_READ_POSITION, (hard ? 0x01 : 0), 0, 0, 0, 0, 0, 0,
577                        0, 0, 0, 0, 0, 0, 0, 0 };
578     int error;
579
580     error = atapi_queue_cmd(stp->device, ccb, (caddr_t)position, 
581                             sizeof(struct ast_readposition), ATPR_F_READ, 10,
582                             NULL, NULL);
583     position->tape = ntohl(position->tape);
584     position->host = ntohl(position->host);
585     return error;
586 }
587
588 static int
589 ast_space(struct ast_softc *stp, u_int8_t function, int32_t count)
590 {
591     int8_t ccb[16] = { ATAPI_SPACE, function, count>>16, count>>8, count,
592                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
593
594     return atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 60*60, NULL, NULL);
595 }
596
597 static int
598 ast_locate(struct ast_softc *stp, int hard, u_int32_t pos)
599 {
600     int8_t ccb[16] = { ATAPI_LOCATE, 0x01 | (hard ? 0x4 : 0), 0,
601                        pos>>24, pos>>16, pos>>8, pos,
602                        0, 0, 0, 0, 0, 0, 0, 0, 0 };
603     int error;
604
605     error = atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
606     if (error)
607         return error;
608     return atapi_wait_dsc(stp->device, 60*60);
609 }
610
611 static int
612 ast_prevent_allow(struct ast_softc *stp, int lock)
613 {
614     int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
615                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
616
617     return atapi_queue_cmd(stp->device, ccb, NULL, 0, 0,30, NULL, NULL);
618 }
619
620 static int
621 ast_load_unload(struct ast_softc *stp, u_int8_t function)
622 {
623     int8_t ccb[16] = { ATAPI_START_STOP, 0x01, 0, 0, function, 0, 0, 0,
624                        0, 0, 0, 0, 0, 0, 0, 0 };
625     int error;
626
627     if ((function & SS_EJECT) && !stp->cap.eject)
628         return 0;
629     error = atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
630     if (error)
631         return error;
632     tsleep((caddr_t)&error, PRIBIO, "astlu", 1 * hz);
633     if (function == SS_EJECT)
634         return 0;
635     return atapi_wait_dsc(stp->device, 60*60);
636 }
637
638 static int
639 ast_rewind(struct ast_softc *stp)
640 {
641     int8_t ccb[16] = { ATAPI_REZERO, 0x01, 0, 0, 0, 0, 0, 0,
642                        0, 0, 0, 0, 0, 0, 0, 0 };
643     int error;
644
645     error = atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
646     if (error)
647         return error;
648     return atapi_wait_dsc(stp->device, 60*60);
649 }
650
651 static int
652 ast_erase(struct ast_softc *stp)
653 {
654     int8_t ccb[16] = { ATAPI_ERASE, 3, 0, 0, 0, 0, 0, 0,
655                        0, 0, 0, 0, 0, 0, 0, 0 };
656     int error;
657
658     if ((error = ast_rewind(stp)))
659         return error;
660
661     return atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 60*60, NULL, NULL);
662 }