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