Merge from vendor branch LESS:
[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.21 2007/05/19 00:52:00 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/diskslice.h>
43 #include <sys/devicestat.h>
44 #include <sys/proc.h>
45 #include <sys/buf2.h>
46 #include <sys/thread2.h>
47
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 dev_ops ast_ops = {
59         { "ast", 119, D_TAPE | D_TRACKCLOSE },
60         .d_open =       astopen,
61         .d_close =      astclose,
62         .d_read =       physread,
63         .d_write =      physwrite,
64         .d_ioctl =      astioctl,
65         .d_strategy =   aststrategy
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     cdev_t dev;
94
95     stp = kmalloc(sizeof(struct ast_softc), M_AST, M_WAITOK | 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     bioq_init(&stp->bio_queue);
105
106     if (ast_sense(stp)) {
107         kfree(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_ops_add(&ast_ops, dkunitmask(), dkmakeunit(stp->lun));
132     dev = make_dev(&ast_ops, dkmakeminor(stp->lun, 0, 0),
133                    UID_ROOT, GID_OPERATOR, 0640, "ast%d", stp->lun);
134     dev->si_drv1 = stp;
135     dev->si_iosize_max = 256 * DEV_BSIZE;
136     dev = make_dev(&ast_ops, 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->device->flags |= ATA_D_MEDIA_CHANGED;
141     ast_describe(stp);
142     atadev->driver = stp;
143     return 1;
144 }
145
146 void    
147 astdetach(struct ata_device *atadev)
148 {   
149     struct ast_softc *stp = atadev->driver;
150     struct buf *bp;
151     struct bio *bio;
152     
153     while ((bio = bioq_first(&stp->bio_queue))) {
154         bioq_remove(&stp->bio_queue, bio);
155         bp = bio->bio_buf;
156         bp->b_flags |= B_ERROR;
157         bp->b_error = ENXIO;
158         biodone(bio);
159     }
160     devstat_remove_entry(&stp->stats);
161     dev_ops_remove(&ast_ops, dkunitmask(), dkmakeunit(stp->lun));
162     ata_free_name(atadev);
163     ata_free_lun(&ast_lun_map, stp->lun);
164     kfree(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         kprintf("transfer limit %d blk%s, ",
206                stp->cap.ctl, (stp->cap.ctl > 1) ? "s" : "");
207         kprintf("%dKB buffer, ", (stp->cap.buffer_size * DEV_BSIZE) / 1024);
208         kprintf("%s\n", ata_mode2str(stp->device->mode));
209         ata_prtdev(stp->device, "Medium: ");
210         switch (stp->cap.medium_type) {
211             case 0x00:
212                 kprintf("none"); break;
213             case 0x17:
214                 kprintf("Travan 1 (400 Mbyte)"); break;
215             case 0xb6:
216                 kprintf("Travan 4 (4 Gbyte)"); break;
217             case 0xda:
218                 kprintf("OnStream ADR (15Gyte)"); break;
219             default:
220                 kprintf("unknown (0x%x)", stp->cap.medium_type);
221         }
222         if (stp->cap.readonly) kprintf(", readonly");
223         if (stp->cap.reverse) kprintf(", reverse");
224         if (stp->cap.eformat) kprintf(", eformat");
225         if (stp->cap.qfa) kprintf(", qfa");
226         if (stp->cap.lock) kprintf(", lock");
227         if (stp->cap.locked) kprintf(", locked");
228         if (stp->cap.prevent) kprintf(", prevent");
229         if (stp->cap.eject) kprintf(", eject");
230         if (stp->cap.disconnect) kprintf(", disconnect");
231         if (stp->cap.ecc) kprintf(", ecc");
232         if (stp->cap.compress) kprintf(", compress");
233         if (stp->cap.blk512) kprintf(", 512b");
234         if (stp->cap.blk1024) kprintf(", 1024b");
235         if (stp->cap.blk32k) kprintf(", 32kb");
236         kprintf("\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(struct dev_open_args *ap)
249 {
250     cdev_t dev = ap->a_head.a_dev;
251     struct ast_softc *stp = dev->si_drv1;
252
253     if (!stp)
254         return ENXIO;
255
256     if (count_dev(dev) > 1)
257         return EBUSY;
258
259     atapi_test_ready(stp->device);
260
261     if (stp->cap.lock)
262         ast_prevent_allow(stp, 1);
263
264     if (ast_sense(stp))
265         ata_prtdev(stp->device, "sense media type failed\n");
266
267     stp->device->flags &= ~ATA_D_MEDIA_CHANGED;
268     stp->flags &= ~(F_DATA_WRITTEN | F_FM_WRITTEN);
269     ast_total = 0;
270     return 0;
271 }
272
273 static int 
274 astclose(struct dev_close_args *ap)
275 {
276     cdev_t dev = ap->a_head.a_dev;
277     struct ast_softc *stp = dev->si_drv1;
278
279     /* flush buffers, some drives fail here, they should report ctl = 0 */
280     if (stp->cap.ctl && (stp->flags & F_DATA_WRITTEN))
281         ast_write_filemark(stp, 0);
282
283     /* write filemark if data written to tape */
284     if (!(stp->flags & F_ONSTREAM) &&
285         (stp->flags & (F_DATA_WRITTEN | F_FM_WRITTEN)) == F_DATA_WRITTEN)
286         ast_write_filemark(stp, WF_WRITE);
287
288     /* if minor is even rewind on close */
289     if (!(minor(dev) & 0x01))
290         ast_rewind(stp);
291
292     if (stp->cap.lock && count_dev(dev) == 1)
293         ast_prevent_allow(stp, 0);
294
295     stp->flags &= F_CTL_WARN;
296 #ifdef AST_DEBUG
297     ata_prtdev(stp->device, "%llu total bytes transferred\n", ast_total);
298 #endif
299     return 0;
300 }
301
302 static int 
303 astioctl(struct dev_ioctl_args *ap)
304 {
305     cdev_t dev = ap->a_head.a_dev;
306     struct ast_softc *stp = dev->si_drv1;
307     int error = 0;
308
309     switch (ap->a_cmd) {
310     case MTIOCGET:
311         {
312             struct mtget *g = (struct mtget *) ap->a_data;
313
314             bzero(g, sizeof(struct mtget));
315             g->mt_type = 7;
316             g->mt_density = 1;
317             g->mt_blksiz = stp->blksize;
318             g->mt_comp = stp->cap.compress;
319             g->mt_density0 = 0; g->mt_density1 = 0;
320             g->mt_density2 = 0; g->mt_density3 = 0;
321             g->mt_blksiz0 = 0; g->mt_blksiz1 = 0;
322             g->mt_blksiz2 = 0; g->mt_blksiz3 = 0;
323             g->mt_comp0 = 0; g->mt_comp1 = 0;
324             g->mt_comp2 = 0; g->mt_comp3 = 0;
325             break;       
326         }
327     case MTIOCTOP:
328         {       
329             int i;
330             struct mtop *mt = (struct mtop *)ap->a_data;
331
332             switch ((int16_t) (mt->mt_op)) {
333
334             case MTWEOF:
335                 for (i=0; i < mt->mt_count && !error; i++)
336                     error = ast_write_filemark(stp, WF_WRITE);
337                 break;
338
339             case MTFSF:
340                 if (mt->mt_count)
341                     error = ast_space(stp, SP_FM, mt->mt_count);
342                 break;
343
344             case MTBSF:
345                 if (mt->mt_count)
346                     error = ast_space(stp, SP_FM, -(mt->mt_count));
347                 break;
348
349             case MTREW:
350                 error = ast_rewind(stp);
351                 break;
352
353             case MTOFFL:
354                 error = ast_load_unload(stp, SS_EJECT);
355                 break;
356
357             case MTNOP:
358                 error = ast_write_filemark(stp, 0);
359                 break;
360
361             case MTERASE:
362                 error = ast_erase(stp);
363                 break;
364
365             case MTEOD:
366                 error = ast_space(stp, SP_EOD, 0);
367                 break;
368
369             case MTRETENS:
370                 error = ast_load_unload(stp, SS_RETENSION | SS_LOAD);
371                 break;
372
373             case MTFSR:         
374             case MTBSR:
375             case MTCACHE:
376             case MTNOCACHE:
377             case MTSETBSIZ:
378             case MTSETDNSTY:
379             case MTCOMP:
380             default:
381                 error = EINVAL;
382             }
383             break;
384         }
385     case MTIOCRDSPOS:
386         {
387             struct ast_readposition position;
388
389             if ((error = ast_read_position(stp, 0, &position)))
390                 break;
391             *(u_int32_t *)ap->a_data = position.tape;
392             break;
393         }
394     case MTIOCRDHPOS:
395         {
396             struct ast_readposition position;
397
398             if ((error = ast_read_position(stp, 1, &position)))
399                 break;
400             *(u_int32_t *)ap->a_data = position.tape;
401             break;
402         }
403     case MTIOCSLOCATE:
404         error = ast_locate(stp, 0, *(u_int32_t *)ap->a_data);
405         break;
406     case MTIOCHLOCATE:
407         error = ast_locate(stp, 1, *(u_int32_t *)ap->a_data);
408         break;
409     default:
410         error = ENOTTY;
411     }
412     return error;
413 }
414
415 static int 
416 aststrategy(struct dev_strategy_args *ap)
417 {
418     cdev_t dev = ap->a_head.a_dev;
419     struct bio *bio = ap->a_bio;
420     struct buf *bp = bio->bio_buf;
421     struct ast_softc *stp = dev->si_drv1;
422
423     if (stp->device->flags & ATA_D_DETACHING) {
424         bp->b_flags |= B_ERROR;
425         bp->b_error = ENXIO;
426         biodone(bio);
427         return(0);
428     }
429
430     /* if it's a null transfer, return immediatly. */
431     if (bp->b_bcount == 0) {
432         bp->b_resid = 0;
433         biodone(bio);
434         return(0);
435     }
436     if (bp->b_cmd != BUF_CMD_READ && (stp->flags & F_WRITEPROTECT)) {
437         bp->b_flags |= B_ERROR;
438         bp->b_error = EPERM;
439         biodone(bio);
440         return(0);
441     }
442         
443     /* check for != blocksize requests */
444     if (bp->b_bcount % stp->blksize) {
445         ata_prtdev(stp->device, "transfers must be multiple of %d\n",
446                    stp->blksize);
447         bp->b_flags |= B_ERROR;
448         bp->b_error = EIO;
449         biodone(bio);
450         return(0);
451     }
452
453     /* warn about transfers bigger than the device suggests */
454     if (bp->b_bcount > stp->blksize * stp->cap.ctl) {    
455         if ((stp->flags & F_CTL_WARN) == 0) {
456             ata_prtdev(stp->device, "WARNING: CTL exceeded %d > %d\n",
457                        bp->b_bcount, stp->blksize * stp->cap.ctl);
458             stp->flags |= F_CTL_WARN;
459         }
460     }
461
462     crit_enter();
463     bioq_insert_tail(&stp->bio_queue, bio);
464     crit_exit();
465     ata_start(stp->device->channel);
466     return(0);
467 }
468
469 void 
470 ast_start(struct ata_device *atadev)
471 {
472     struct ast_softc *stp = atadev->driver;
473     struct bio *bio = bioq_first(&stp->bio_queue);
474     struct buf *bp;
475     u_int32_t blkcount;
476     int8_t ccb[16];
477     
478     if (bio == NULL)
479         return;
480     bzero(ccb, sizeof(ccb));
481
482     bp = bio->bio_buf;
483     if (bp->b_cmd == BUF_CMD_READ)
484         ccb[0] = ATAPI_READ;
485     else
486         ccb[0] = ATAPI_WRITE;
487     
488     bioq_remove(&stp->bio_queue, bio);
489     blkcount = bp->b_bcount / stp->blksize;
490
491     ccb[1] = 1;
492     ccb[2] = blkcount>>16;
493     ccb[3] = blkcount>>8;
494     ccb[4] = blkcount;
495
496     devstat_start_transaction(&stp->stats);
497
498     atapi_queue_cmd(stp->device, ccb, bp->b_data, blkcount * stp->blksize, 
499                     ((bp->b_cmd == BUF_CMD_READ) ? ATPR_F_READ : 0),
500                     120, ast_done, bio);
501 }
502
503 static int 
504 ast_done(struct atapi_request *request)
505 {
506     struct bio *bio = request->driver;
507     struct buf *bp = bio->bio_buf;
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     } else {
514         if (bp->b_cmd != BUF_CMD_READ)
515             stp->flags |= F_DATA_WRITTEN;
516         bp->b_resid = bp->b_bcount - request->donecount;
517         ast_total += (bp->b_bcount - bp->b_resid);
518     }
519     devstat_end_transaction_buf(&stp->stats, bp);
520     biodone(bio);
521     return 0;
522 }
523
524 static int
525 ast_mode_sense(struct ast_softc *stp, int page, void *pagebuf, int pagesize)
526 {
527     int8_t ccb[16] = { ATAPI_MODE_SENSE, 0x08, page, pagesize>>8, pagesize,
528                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
529     int error;
530  
531     error = atapi_queue_cmd(stp->device, ccb, pagebuf, pagesize, ATPR_F_READ,
532                             10, NULL, NULL);
533 #ifdef AST_DEBUG
534     atapi_dump("ast: mode sense ", pagebuf, pagesize);
535 #endif
536     return error;
537 }
538
539 static int       
540 ast_mode_select(struct ast_softc *stp, void *pagebuf, int pagesize)
541 {
542     int8_t ccb[16] = { ATAPI_MODE_SELECT, 0x10, 0, pagesize>>8, pagesize,
543                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
544      
545 #ifdef AST_DEBUG
546     ata_prtdev(stp->device, "modeselect pagesize=%d\n", pagesize);
547     atapi_dump("mode select ", pagebuf, pagesize);
548 #endif
549     return atapi_queue_cmd(stp->device, ccb, pagebuf, pagesize, 0,
550                            10, NULL, NULL);
551 }
552
553 static int
554 ast_write_filemark(struct ast_softc *stp, u_int8_t function)
555 {
556     int8_t ccb[16] = { ATAPI_WEOF, 0x01, 0, 0, function, 0, 0, 0,
557                        0, 0, 0, 0, 0, 0, 0, 0 };
558     int error;
559
560     if (stp->flags & F_ONSTREAM)
561         ccb[4] = 0x00;          /* only flush buffers supported */
562     else {
563         if (function) {
564             if (stp->flags & F_FM_WRITTEN)
565                 stp->flags &= ~F_DATA_WRITTEN;
566             else
567                 stp->flags |= F_FM_WRITTEN;
568         }
569     }
570     error = atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
571     if (error)
572         return error;
573     return atapi_wait_dsc(stp->device, 10*60);
574 }
575
576 static int
577 ast_read_position(struct ast_softc *stp, int hard,
578                   struct ast_readposition *position)
579 {
580     int8_t ccb[16] = { ATAPI_READ_POSITION, (hard ? 0x01 : 0), 0, 0, 0, 0, 0, 0,
581                        0, 0, 0, 0, 0, 0, 0, 0 };
582     int error;
583
584     error = atapi_queue_cmd(stp->device, ccb, (caddr_t)position, 
585                             sizeof(struct ast_readposition), ATPR_F_READ, 10,
586                             NULL, NULL);
587     position->tape = ntohl(position->tape);
588     position->host = ntohl(position->host);
589     return error;
590 }
591
592 static int
593 ast_space(struct ast_softc *stp, u_int8_t function, int32_t count)
594 {
595     int8_t ccb[16] = { ATAPI_SPACE, function, count>>16, count>>8, count,
596                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
597
598     return atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 60*60, NULL, NULL);
599 }
600
601 static int
602 ast_locate(struct ast_softc *stp, int hard, u_int32_t pos)
603 {
604     int8_t ccb[16] = { ATAPI_LOCATE, 0x01 | (hard ? 0x4 : 0), 0,
605                        pos>>24, pos>>16, pos>>8, pos,
606                        0, 0, 0, 0, 0, 0, 0, 0, 0 };
607     int error;
608
609     error = atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
610     if (error)
611         return error;
612     return atapi_wait_dsc(stp->device, 60*60);
613 }
614
615 static int
616 ast_prevent_allow(struct ast_softc *stp, int lock)
617 {
618     int8_t ccb[16] = { ATAPI_PREVENT_ALLOW, 0, 0, 0, lock,
619                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
620
621     return atapi_queue_cmd(stp->device, ccb, NULL, 0, 0,30, NULL, NULL);
622 }
623
624 static int
625 ast_load_unload(struct ast_softc *stp, u_int8_t function)
626 {
627     int8_t ccb[16] = { ATAPI_START_STOP, 0x01, 0, 0, function, 0, 0, 0,
628                        0, 0, 0, 0, 0, 0, 0, 0 };
629     int error;
630
631     if ((function & SS_EJECT) && !stp->cap.eject)
632         return 0;
633     error = atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
634     if (error)
635         return error;
636     tsleep((caddr_t)&error, 0, "astlu", 1 * hz);
637     if (function == SS_EJECT)
638         return 0;
639     return atapi_wait_dsc(stp->device, 60*60);
640 }
641
642 static int
643 ast_rewind(struct ast_softc *stp)
644 {
645     int8_t ccb[16] = { ATAPI_REZERO, 0x01, 0, 0, 0, 0, 0, 0,
646                        0, 0, 0, 0, 0, 0, 0, 0 };
647     int error;
648
649     error = atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 10, NULL, NULL);
650     if (error)
651         return error;
652     return atapi_wait_dsc(stp->device, 60*60);
653 }
654
655 static int
656 ast_erase(struct ast_softc *stp)
657 {
658     int8_t ccb[16] = { ATAPI_ERASE, 3, 0, 0, 0, 0, 0, 0,
659                        0, 0, 0, 0, 0, 0, 0, 0 };
660     int error;
661
662     if ((error = ast_rewind(stp)))
663         return error;
664
665     return atapi_queue_cmd(stp->device, ccb, NULL, 0, 0, 60*60, NULL, NULL);
666 }