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