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