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