Merge from vendor branch NCURSES:
[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.12 2005/06/03 21:56:23 swildner 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 <sys/thread2.h>
48 #include "ata-all.h"
49 #include "atapi-all.h"
50 #include "atapi-tape.h"
51
52 /* device structures */
53 static  d_open_t        astopen;
54 static  d_close_t       astclose;
55 static  d_ioctl_t       astioctl;
56 static  d_strategy_t    aststrategy;
57
58 static struct cdevsw ast_cdevsw = {
59         /* name */      "ast",
60         /* maj */       119,
61         /* flags */     D_TAPE | D_TRACKCLOSE,
62         /* port */      NULL,
63         /* clone */     NULL,
64
65         /* open */      astopen,
66         /* close */     astclose,
67         /* read */      physread,
68         /* write */     physwrite,
69         /* ioctl */     astioctl,
70         /* poll */      nopoll,
71         /* mmap */      nommap,
72         /* strategy */  aststrategy,
73         /* dump */      nodump,
74         /* psize */     nopsize
75 };
76
77 /* prototypes */
78 static int ast_sense(struct ast_softc *);
79 static void ast_describe(struct ast_softc *);
80 static int ast_done(struct atapi_request *);
81 static int ast_mode_sense(struct ast_softc *, int, void *, int); 
82 static int ast_mode_select(struct ast_softc *, void *, int);
83 static int ast_write_filemark(struct ast_softc *, u_int8_t);
84 static int ast_read_position(struct ast_softc *, int, struct ast_readposition *);
85 static int ast_space(struct ast_softc *, u_int8_t, int32_t);
86 static int ast_locate(struct ast_softc *, int, u_int32_t);
87 static int ast_prevent_allow(struct ast_softc *stp, int);
88 static int ast_load_unload(struct ast_softc *, u_int8_t);
89 static int ast_rewind(struct ast_softc *);
90 static int ast_erase(struct ast_softc *);
91
92 /* internal vars */
93 static u_int32_t ast_lun_map = 0;
94 static u_int64_t ast_total = 0;
95 static MALLOC_DEFINE(M_AST, "AST driver", "ATAPI tape driver buffers");
96
97 int 
98 astattach(struct ata_device *atadev)
99 {
100     struct ast_softc *stp;
101     struct ast_readposition position;
102     dev_t dev;
103
104     stp = malloc(sizeof(struct ast_softc), M_AST, M_WAITOK | M_ZERO);
105     if (!stp) {
106         ata_prtdev(atadev, "out of memory\n");
107         return 0;
108     }
109
110     stp->device = atadev;
111     stp->lun = ata_get_lun(&ast_lun_map);
112     ata_set_name(atadev, "ast", stp->lun);
113     bufq_init(&stp->queue);
114
115     if (ast_sense(stp)) {
116         free(stp, M_AST);
117         return 0;
118     }
119
120     if (!strcmp(atadev->param->model, "OnStream DI-30")) {
121         struct ast_transferpage transfer;
122         struct ast_identifypage identify;
123
124         stp->flags |= F_ONSTREAM;
125         bzero(&transfer, sizeof(struct ast_transferpage));
126         ast_mode_sense(stp, ATAPI_TAPE_TRANSFER_PAGE,
127                        &transfer, sizeof(transfer));
128         bzero(&identify, sizeof(struct ast_identifypage));
129         ast_mode_sense(stp, ATAPI_TAPE_IDENTIFY_PAGE,
130                        &identify, sizeof(identify));
131         strncpy(identify.ident, "FBSD", 4);
132         ast_mode_select(stp, &identify, sizeof(identify));
133         ast_read_position(stp, 0, &position);
134     }
135
136     devstat_add_entry(&stp->stats, "ast", stp->lun, DEV_BSIZE,
137                       DEVSTAT_NO_ORDERED_TAGS,
138                       DEVSTAT_TYPE_SEQUENTIAL | DEVSTAT_TYPE_IF_IDE,
139                       DEVSTAT_PRIORITY_TAPE);
140     cdevsw_add(&ast_cdevsw, dkunitmask(), dkmakeunit(stp->lun));
141     dev = make_dev(&ast_cdevsw, dkmakeminor(stp->lun, 0, 0),
142                    UID_ROOT, GID_OPERATOR, 0640, "ast%d", stp->lun);
143     dev->si_drv1 = stp;
144     dev->si_iosize_max = 256 * DEV_BSIZE;
145     dev = make_dev(&ast_cdevsw, dkmakeminor(stp->lun, 0, 1),
146                    UID_ROOT, GID_OPERATOR, 0640, "nast%d", stp->lun);
147     dev->si_drv1 = stp;
148     dev->si_iosize_max = 256 * DEV_BSIZE;
149     stp->device->flags |= ATA_D_MEDIA_CHANGED;
150     ast_describe(stp);
151     atadev->driver = stp;
152     return 1;
153 }
154
155 void    
156 astdetach(struct ata_device *atadev)
157 {   
158     struct ast_softc *stp = atadev->driver;
159     struct buf *bp;
160     
161     while ((bp = bufq_first(&stp->queue))) {
162         bufq_remove(&stp->queue, bp);
163         bp->b_flags |= B_ERROR;
164         bp->b_error = ENXIO;
165         biodone(bp);
166     }
167     devstat_remove_entry(&stp->stats);
168     cdevsw_remove(&ast_cdevsw, dkunitmask(), dkmakeunit(stp->lun));
169     ata_free_name(atadev);
170     ata_free_lun(&ast_lun_map, stp->lun);
171     free(stp, M_AST);
172     atadev->driver = NULL;
173 }
174
175 static int
176 ast_sense(struct ast_softc *stp)
177 {
178     int count, error = 0;
179
180     /* get drive capabilities, some drives needs this repeated */
181     for (count = 0 ; count < 5 ; count++) {
182         if (!(error = ast_mode_sense(stp, ATAPI_TAPE_CAP_PAGE,
183                                      &stp->cap, sizeof(stp->cap)))) {
184             if (stp->cap.blk32k)
185                 stp->blksize = 32768;
186             if (stp->cap.blk1024)
187                 stp->blksize = 1024;
188             if (stp->cap.blk512)
189                 stp->blksize = 512;
190             if (!stp->blksize)
191                 continue;
192             stp->cap.max_speed = ntohs(stp->cap.max_speed);
193             stp->cap.max_defects = ntohs(stp->cap.max_defects);
194             stp->cap.ctl = ntohs(stp->cap.ctl);
195             stp->cap.speed = ntohs(stp->cap.speed);
196             stp->cap.buffer_size = ntohs(stp->cap.buffer_size);
197             return 0;
198         }
199     }
200     return 1;
201 }
202
203 static void 
204 ast_describe(struct ast_softc *stp)
205 {
206     if (bootverbose) {
207         ata_prtdev(stp->device, "<%.40s/%.8s> tape drive at ata%d as %s\n",
208                    stp->device->param->model, stp->device->param->revision,
209                    device_get_unit(stp->device->channel->dev),
210                    (stp->device->unit == ATA_MASTER) ? "master" : "slave");
211         ata_prtdev(stp->device, "%dKB/s, ", stp->cap.max_speed);
212         printf("transfer limit %d blk%s, ",
213                stp->cap.ctl, (stp->cap.ctl > 1) ? "s" : "");
214         printf("%dKB buffer, ", (stp->cap.buffer_size * DEV_BSIZE) / 1024);
215         printf("%s\n", ata_mode2str(stp->device->mode));
216         ata_prtdev(stp->device, "Medium: ");
217         switch (stp->cap.medium_type) {
218             case 0x00:
219                 printf("none"); break;
220             case 0x17:
221                 printf("Travan 1 (400 Mbyte)"); break;
222             case 0xb6:
223                 printf("Travan 4 (4 Gbyte)"); break;
224             case 0xda:
225                 printf("OnStream ADR (15Gyte)"); break;
226             default:
227                 printf("unknown (0x%x)", stp->cap.medium_type);
228         }
229         if (stp->cap.readonly) printf(", readonly");
230         if (stp->cap.reverse) printf(", reverse");
231         if (stp->cap.eformat) printf(", eformat");
232         if (stp->cap.qfa) printf(", qfa");
233         if (stp->cap.lock) printf(", lock");
234         if (stp->cap.locked) printf(", locked");
235         if (stp->cap.prevent) printf(", prevent");
236         if (stp->cap.eject) printf(", eject");
237         if (stp->cap.disconnect) printf(", disconnect");
238         if (stp->cap.ecc) printf(", ecc");
239         if (stp->cap.compress) printf(", compress");
240         if (stp->cap.blk512) printf(", 512b");
241         if (stp->cap.blk1024) printf(", 1024b");
242         if (stp->cap.blk32k) printf(", 32kb");
243         printf("\n");
244     }
245     else {
246         ata_prtdev(stp->device, "TAPE <%.40s> at ata%d-%s %s\n",
247                    stp->device->param->model,
248                    device_get_unit(stp->device->channel->dev),
249                    (stp->device->unit == ATA_MASTER) ? "master" : "slave",
250                    ata_mode2str(stp->device->mode));
251     }
252 }
253
254 static int
255 astopen(dev_t dev, int flags, int fmt, struct thread *td)
256 {
257     struct ast_softc *stp = dev->si_drv1;
258
259     if (!stp)
260         return ENXIO;
261
262     if (count_dev(dev) > 1)
263         return EBUSY;
264
265     atapi_test_ready(stp->device);
266
267     if (stp->cap.lock)
268         ast_prevent_allow(stp, 1);
269
270     if (ast_sense(stp))
271         ata_prtdev(stp->device, "sense media type failed\n");
272
273     stp->device->flags &= ~ATA_D_MEDIA_CHANGED;
274     stp->flags &= ~(F_DATA_WRITTEN | F_FM_WRITTEN);
275     ast_total = 0;
276     return 0;
277 }
278
279 static int 
280 astclose(dev_t dev, int flags, int fmt, struct thread *td)
281 {
282     struct ast_softc *stp = dev->si_drv1;
283
284     /* flush buffers, some drives fail here, they should report ctl = 0 */
285     if (stp->cap.ctl && (stp->flags & F_DATA_WRITTEN))
286         ast_write_filemark(stp, 0);
287
288     /* write filemark if data written to tape */
289     if (!(stp->flags & F_ONSTREAM) &&
290         (stp->flags & (F_DATA_WRITTEN | F_FM_WRITTEN)) == F_DATA_WRITTEN)
291         ast_write_filemark(stp, WF_WRITE);
292
293     /* if minor is even rewind on close */
294     if (!(minor(dev) & 0x01))
295         ast_rewind(stp);
296
297     if (stp->cap.lock && count_dev(dev) == 1)
298         ast_prevent_allow(stp, 0);
299
300     stp->flags &= F_CTL_WARN;
301 #ifdef AST_DEBUG
302     ata_prtdev(stp->device, "%llu total bytes transferred\n", ast_total);
303 #endif
304     return 0;
305 }
306
307 static int 
308 astioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
309 {
310     struct ast_softc *stp = dev->si_drv1;
311     int error = 0;
312
313     switch (cmd) {
314     case MTIOCGET:
315         {
316             struct mtget *g = (struct mtget *) addr;
317
318             bzero(g, sizeof(struct mtget));
319             g->mt_type = 7;
320             g->mt_density = 1;
321             g->mt_blksiz = stp->blksize;
322             g->mt_comp = stp->cap.compress;
323             g->mt_density0 = 0; g->mt_density1 = 0;
324             g->mt_density2 = 0; g->mt_density3 = 0;
325             g->mt_blksiz0 = 0; g->mt_blksiz1 = 0;
326             g->mt_blksiz2 = 0; g->mt_blksiz3 = 0;
327             g->mt_comp0 = 0; g->mt_comp1 = 0;
328             g->mt_comp2 = 0; g->mt_comp3 = 0;
329             break;       
330         }
331     case MTIOCTOP:
332         {       
333             int i;
334             struct mtop *mt = (struct mtop *)addr;
335
336             switch ((int16_t) (mt->mt_op)) {
337
338             case MTWEOF:
339                 for (i=0; i < mt->mt_count && !error; i++)
340                     error = ast_write_filemark(stp, WF_WRITE);
341                 break;
342
343             case MTFSF:
344                 if (mt->mt_count)
345                     error = ast_space(stp, SP_FM, mt->mt_count);
346                 break;
347
348             case MTBSF:
349                 if (mt->mt_count)
350                     error = ast_space(stp, SP_FM, -(mt->mt_count));
351                 break;
352
353             case MTREW:
354                 error = ast_rewind(stp);
355                 break;
356
357             case MTOFFL:
358                 error = ast_load_unload(stp, SS_EJECT);
359                 break;
360
361             case MTNOP:
362                 error = ast_write_filemark(stp, 0);
363                 break;
364
365             case MTERASE:
366                 error = ast_erase(stp);
367                 break;
368
369             case MTEOD:
370                 error = ast_space(stp, SP_EOD, 0);
371                 break;
372
373             case MTRETENS:
374                 error = ast_load_unload(stp, SS_RETENSION | SS_LOAD);
375                 break;
376
377             case MTFSR:         
378             case MTBSR:
379             case MTCACHE:
380             case MTNOCACHE:
381             case MTSETBSIZ:
382             case MTSETDNSTY:
383             case MTCOMP:
384             default:
385                 error = EINVAL;
386             }
387             break;
388         }
389     case MTIOCRDSPOS:
390         {
391             struct ast_readposition position;
392
393             if ((error = ast_read_position(stp, 0, &position)))
394                 break;
395             *(u_int32_t *)addr = position.tape;
396             break;
397         }
398     case MTIOCRDHPOS:
399         {
400             struct ast_readposition position;
401
402             if ((error = ast_read_position(stp, 1, &position)))
403                 break;
404             *(u_int32_t *)addr = position.tape;
405             break;
406         }
407     case MTIOCSLOCATE:
408         error = ast_locate(stp, 0, *(u_int32_t *)addr);
409         break;
410     case MTIOCHLOCATE:
411         error = ast_locate(stp, 1, *(u_int32_t *)addr);
412         break;
413     default:
414         error = ENOTTY;
415     }
416     return error;
417 }
418
419 static void 
420 aststrategy(struct buf *bp)
421 {
422     struct ast_softc *stp = bp->b_dev->si_drv1;
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     crit_enter();
464     bufq_insert_tail(&stp->queue, bp);
465     crit_exit();
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 }