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