st.c revision 1.19 1 /*
2 * Written by Julian Elischer (julian (at) tfs.com)
3 * for TRW Financial Systems for use under the MACH(2.5) operating system.
4 * Hacked by Theo de Raadt <deraadt (at) fsa.ca>
5 *
6 * TRW Financial Systems, in accordance with their agreement with Carnegie
7 * Mellon University, makes this software available to CMU to distribute
8 * or use in any manner that they see fit as long as this message is kept with
9 * the software. For this reason TFS also grants any other persons or
10 * organisations permission to use or modify this software.
11 *
12 * TFS supplies this software to be publicly redistributed
13 * on the understanding that TFS is not responsible for the correct
14 * functioning of this software in any circumstances.
15 *
16 * $Id: st.c,v 1.19 1994/02/06 08:01:45 mycroft Exp $
17 */
18
19 /*
20 * To do:
21 * work out some better way of guessing what a good timeout is going
22 * to be depending on whether we expect to retension or not.
23 *
24 */
25
26 #include "st.h"
27
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/errno.h>
32 #include <sys/malloc.h>
33 #include <sys/ioctl.h>
34 #include <sys/buf.h>
35 #include <sys/proc.h>
36 #include <sys/user.h>
37 #include <sys/mtio.h>
38 #include <sys/dkbad.h>
39 #include <sys/disklabel.h>
40
41 #include <scsi/scsi_all.h>
42 #include <scsi/scsi_tape.h>
43 #include <scsi/scsiconf.h>
44 #include <scsi/stdefs.h>
45
46 long int ststrats, stqueues;
47
48 #define ST_RETRIES 4
49
50 #define UNITSHIFT 4
51 #define MODE(z) ((minor(z) & 0x03))
52 #define DSTY(z) (((minor(z) >> 2) & 0x03))
53 #define UNIT(z) ((minor(z) >> UNITSHIFT))
54
55 #undef NST
56 #define NST ( makedev(1,0) >> UNITSHIFT)
57
58 #define DSTY_QIC120 3
59 #define DSTY_QIC150 2
60 #define DSTY_QIC525 1
61
62 #define QIC120 0x0f
63 #define QIC150 0x10
64 #define QIC525 0x11
65
66 #define ESUCCESS 0
67
68 int st_debug = 0;
69
70 struct st_data *st_data[NST];
71 static int next_st_unit = 0;
72
73 /*
74 * The routine called by the low level scsi routine when it discovers
75 * A device suitable for this driver
76 */
77 int
78 stattach(int masunit, struct scsi_switch *sw, int physid, int *unit)
79 {
80 struct st_data *st;
81 unsigned char *tbl;
82 int targ, lun, i;
83
84 targ = physid >> 3;
85 lun = physid & 7;
86
87 /*printf("stattach: st%d at %s%d target %d lun %d\n",
88 *unit, sw->name, masunit, targ, lun);*/
89
90 if(*unit==-1) {
91 for(i=0; i<NST && *unit==-1; i++)
92 if(st_data[i]==NULL)
93 *unit = i;
94 }
95 if(*unit >= NST || *unit==-1)
96 return 0;
97 if(st_data[*unit])
98 return 0;
99
100 st = st_data[*unit] = (struct st_data *)malloc(sizeof *st,
101 M_TEMP, M_NOWAIT);
102 bzero(st, sizeof *st);
103
104 st->sc_sw = sw;
105 st->ctlr = masunit;
106 st->targ = targ;
107 st->lu = lun;
108
109 /*
110 * Use the subdriver to request information regarding
111 * the drive. We cannot use interrupts yet, so the
112 * request must specify this.
113 */
114 if( st_mode_sense(*unit, SCSI_NOSLEEP | SCSI_NOMASK | SCSI_SILENT))
115 printf("st%d at %s%d targ %d lun %d: %d blocks of %d bytes\n",
116 *unit, sw->name, masunit, targ, lun,
117 st->numblks, st->blksiz);
118 else
119 printf("st%d at %s%d targ %d lun %d: offline\n",
120 *unit, sw->name, masunit, targ, lun);
121
122 /*
123 * Set up the bufs for this device
124 */
125 st->buf_queue.b_active = 0;
126 st->buf_queue.b_actf = 0;
127 st->buf_queue.b_actb = &st->buf_queue.b_actf;
128 st->initialized = 1;
129 return 1;
130 }
131
132 /*
133 * open the device.
134 */
135 int
136 stopen(dev_t dev)
137 {
138 int errcode = 0;
139 int unit, mode, dsty;
140 int dsty_code;
141 struct st_data *st;
142 unit = UNIT(dev);
143 mode = MODE(dev);
144 dsty = DSTY(dev);
145 st = st_data[unit];
146
147 /*
148 * Check the unit is legal
149 */
150 if( unit >= NST )
151 return ENXIO;
152 if(!st)
153 return ENXIO;
154
155 /*
156 * Only allow one at a time
157 */
158 if(st->flags & ST_OPEN) {
159 errcode = EBUSY;
160 goto bad;
161 }
162 /*
163 * Set up the mode flags according to the minor number
164 * ensure all open flags are in a known state
165 */
166 st->flags &= ~ST_PER_OPEN;
167 switch(mode) {
168 case 2:
169 case 0:
170 st->flags &= ~ST_NOREWIND;
171 break;
172 case 3:
173 case 1:
174 st->flags |= ST_NOREWIND;
175 break;
176 default:
177 printf("st%d: bad mode (minor number) %d\n", unit, mode);
178 return EINVAL;
179 }
180 /*
181 * Check density code: 0 is drive default
182 */
183 switch(dsty) {
184 case 0:
185 dsty_code = 0;
186 break;
187 case DSTY_QIC120:
188 dsty_code = QIC120;
189 break;
190 case DSTY_QIC150:
191 dsty_code = QIC150;
192 break;
193 case DSTY_QIC525:
194 dsty_code = QIC525;
195 break;
196 default:
197 printf("st%d: bad density (minor number) %d\n", unit, dsty);
198 return EINVAL;
199 }
200
201 if(scsi_debug & (PRINTROUTINES | TRACEOPENS))
202 printf("st%d: open dev=0x%x (unit %d (of %d))\n", unit, dev, NST);
203
204 /*
205 * Make sure the device has been initialised
206 */
207 if (!st->initialized) {
208 /*printf("st%d: uninitialized\n", unit);*/
209 return ENXIO;
210 }
211
212 /*
213 * Check that it is still responding and ok.
214 */
215 if (!(st_req_sense(unit, 0))) {
216 errcode = EIO;
217 if(scsi_debug & TRACEOPENS)
218 printf("st%d: not responding\n", unit);
219 goto bad;
220 }
221 if(scsi_debug & TRACEOPENS)
222 printf("st%d: is responding\n", unit);
223
224 if(!(st_test_ready(unit, 0))) {
225 printf("st%d: not ready\n", unit);
226 return EIO;
227 }
228
229 if(!st->info_valid) /* is media new? */
230 if(!st_load(unit, LD_LOAD, 0))
231 return EIO;
232
233 if(!st_rd_blk_lim(unit, 0))
234 return EIO;
235
236 if(!st_mode_sense(unit, 0))
237 return EIO;
238
239 if(!st_mode_select(unit, 0, dsty_code))
240 return EIO;
241
242 st->info_valid = TRUE;
243
244 st_prevent(unit, PR_PREVENT, 0); /* who cares if it fails? */
245
246 /*
247 * Load the physical device parameters
248 */
249 if(scsi_debug & TRACEOPENS)
250 printf("st%d: params ", unit);
251 st->flags |= ST_OPEN;
252
253 bad:
254 return errcode;
255 }
256
257 /*
258 * close the device.. only called if we are the LAST
259 * occurence of an open device
260 */
261 int
262 stclose(dev_t dev)
263 {
264 unsigned char unit, mode;
265 struct st_data *st;
266
267 unit = UNIT(dev);
268 mode = MODE(dev);
269 st = st_data[unit];
270
271 if(scsi_debug & TRACEOPENS)
272 printf("st%d: close\n", unit);
273 if(st->flags & ST_WRITTEN)
274 st_write_filemarks(unit, 1, 0);
275
276 st->flags &= ~ST_WRITTEN;
277 switch(mode) {
278 case 0:
279 st_rewind(unit, FALSE, SCSI_SILENT);
280 st_prevent(unit, PR_ALLOW, SCSI_SILENT);
281 break;
282 case 1:
283 st_prevent(unit, PR_ALLOW, SCSI_SILENT);
284 break;
285 case 2:
286 st_rewind(unit, FALSE, SCSI_SILENT);
287 st_prevent(unit, PR_ALLOW, SCSI_SILENT);
288 st_load(unit, LD_UNLOAD, SCSI_SILENT);
289 break;
290 case 3:
291 st_prevent(unit, PR_ALLOW, SCSI_SILENT);
292 st_load(unit, LD_UNLOAD, SCSI_SILENT);
293 break;
294 default:
295 printf("st%d: bad mode (minor number) %d (how's it open?)\n",
296 unit, mode);
297 return EINVAL;
298 }
299 st->flags &= ~ST_PER_OPEN;
300 return 0;
301 }
302
303 /*
304 * trim the size of the transfer if needed,
305 * called by physio
306 * basically the smaller of our min and the scsi driver's*
307 * minphys
308 */
309 void
310 stminphys(struct buf *bp)
311 {
312 (*(st_data[UNIT(bp->b_dev)]->sc_sw->scsi_minphys))(bp);
313 }
314
315 /*
316 * Actually translate the requested transfer into
317 * one the physical driver can understand
318 * The transfer is described by a buf and will include
319 * only one physical transfer.
320 */
321 void
322 ststrategy(struct buf *bp)
323 {
324 struct st_data *st;
325 struct buf *dp;
326 unsigned char unit;
327 unsigned int opri;
328
329 if (bp->b_bcount == 0)
330 goto done;
331
332 ststrats++;
333 unit = UNIT((bp->b_dev));
334 st = st_data[unit];
335 if(scsi_debug & PRINTROUTINES)
336 printf("\nststrategy ");
337 if(scsi_debug & SHOWREQUESTS)
338 printf("st%d: %d bytes @ blk%d\n", unit, bp->b_bcount, bp->b_blkno);
339
340 /*
341 * Odd sized request on fixed drives are verboten
342 */
343 if((st->flags & ST_FIXEDBLOCKS) && bp->b_bcount % st->blkmin) {
344 printf("st%d: bad request, must be multiple of %d\n",
345 unit, st->blkmin);
346 bp->b_error = EIO;
347 goto bad;
348 }
349
350 stminphys(bp);
351 opri = splbio();
352 dp = &st->buf_queue;
353
354 /*
355 * Place it at the end of the queue of activities for this tape.
356 */
357 bp->b_actf = NULL;
358 bp->b_actb = dp->b_actb;
359 *dp->b_actb = bp;
360 dp->b_actb = &bp->b_actf;
361
362 /*
363 * Tell the device to get going on the transfer if it's
364 * not doing anything, otherwise just wait for completion.
365 */
366 ststart(unit);
367
368 splx(opri);
369 return;
370 bad:
371 bp->b_flags |= B_ERROR;
372 done:
373 /*
374 * Correctly set the buf to indicate a completed xfer
375 */
376 iodone(bp);
377 return;
378 }
379
380
381 /*
382 * ststart looks to see if there is a buf waiting for the device
383 * and that the device is not already busy. If both are true,
384 * It deques the buf and creates a scsi command to perform the
385 * transfer in the buf. The transfer request will call st_done
386 * on completion, which will in turn call this routine again
387 * so that the next queued transfer is performed.
388 * The bufs are queued by the strategy routine (ststrategy)
389 *
390 * This routine is also called after other non-queued requests
391 * have been made of the scsi driver, to ensure that the queue
392 * continues to be drained.
393 */
394 /* ststart() is called at splbio */
395 int
396 ststart(int unit)
397 {
398 struct st_data *st = st_data[unit];
399 register struct buf *bp = 0, *dp;
400 struct scsi_rw_tape cmd;
401 struct scsi_xfer *xs;
402 int drivecount, blkno, nblk;
403
404 if(scsi_debug & PRINTROUTINES)
405 printf("st%d: start\n", unit);
406
407 /*
408 * See if there is a buf to do and we are not already
409 * doing one
410 */
411 xs = &st->scsi_xfer;
412 if(xs->flags & INUSE)
413 return; /* unit already underway */
414
415 trynext:
416 if(st->blockwait) {
417 wakeup((caddr_t)&st->blockwait);
418 return;
419 }
420
421 bp = st->buf_queue.b_actf;
422 if (!bp)
423 return;
424 if (dp = bp->b_actf)
425 dp->b_actb = bp->b_actb;
426 else
427 st->buf_queue.b_actb = bp->b_actb;
428 *bp->b_actb = dp;
429 xs->flags = INUSE; /* Now ours */
430
431 /*
432 * We have a buf, now we should move the data into
433 * a scsi_xfer definition and try start it
434 */
435
436 /*
437 * If we are at a filemark but have not reported it yet
438 * then we should report it now
439 */
440 if(st->flags & ST_AT_FILEMARK) {
441 bp->b_error = 0;
442 bp->b_flags |= B_ERROR; /* EOF*/
443 st->flags &= ~ST_AT_FILEMARK;
444 biodone(bp);
445 xs->flags = 0; /* won't need it now */
446 goto trynext;
447 }
448
449 /*
450 * If we are at EOM but have not reported it yet
451 * then we should report it now
452 */
453 if(st->flags & ST_AT_EOM) {
454 bp->b_error = EIO;
455 bp->b_flags |= B_ERROR;
456 st->flags &= ~ST_AT_EOM;
457 biodone(bp);
458 xs->flags = 0; /* won't need it now */
459 goto trynext;
460 }
461
462 /*
463 * Fill out the scsi command
464 */
465 bzero(&cmd, sizeof(cmd));
466 if((bp->b_flags & B_READ) == B_WRITE) {
467 st->flags |= ST_WRITTEN;
468 xs->flags |= SCSI_DATA_OUT;
469 }
470 else
471 xs->flags |= SCSI_DATA_IN;
472 cmd.op_code = (bp->b_flags & B_READ) ? READ_COMMAND_TAPE : WRITE_COMMAND_TAPE;
473
474 /*
475 * Handle "fixed-block-mode" tape drives by using the *
476 * block count instead of the length.
477 */
478 if(st->flags & ST_FIXEDBLOCKS) {
479 cmd.fixed = 1;
480 lto3b(bp->b_bcount/st->blkmin, cmd.len);
481 }
482 else
483 lto3b(bp->b_bcount, cmd.len);
484
485 /*
486 * Fill out the scsi_xfer structure
487 * Note: we cannot sleep as we may be an interrupt
488 */
489 xs->flags |= SCSI_NOSLEEP;
490 xs->adapter = st->ctlr;
491 xs->targ = st->targ;
492 xs->lu = st->lu;
493 xs->retries = 1; /* can't retry on tape*/
494 xs->timeout = 200000; /* allow 200 secs for retension */
495 xs->cmd = (struct scsi_generic *)&cmd;
496 xs->cmdlen = sizeof(cmd);
497 xs->data = (u_char *)bp->b_un.b_addr;
498 xs->datalen = bp->b_bcount;
499 xs->resid = bp->b_bcount;
500 xs->when_done = st_done;
501 xs->done_arg = unit;
502 xs->done_arg2 = (int)xs;
503 xs->error = XS_NOERROR;
504 xs->bp = bp;
505
506 #if defined(OSF) || defined(FIX_ME)
507 if (bp->b_flags & B_PHYS) {
508 xs->data = (u_char*)map_pva_kva(bp->b_proc, bp->b_un.b_addr,
509 bp->b_bcount, st_window[unit],
510 (bp->b_flags&B_READ)?B_WRITE:B_READ);
511 } else
512 xs->data = (u_char*)bp->b_un.b_addr;
513 #endif /* defined(OSF) */
514
515 if ( (*(st->sc_sw->scsi_cmd))(xs) != SUCCESSFULLY_QUEUED) {
516 printf("st%d: oops not queued", unit);
517 xs->error = XS_DRIVER_STUFFUP;
518 st_done(unit, xs);
519 }
520 stqueues++;
521 }
522
523 /*
524 * This routine is called by the scsi interrupt when
525 * the transfer is complete.
526 */
527 int
528 st_done(int unit, struct scsi_xfer *xs)
529 {
530 struct st_data *st = st_data[unit];
531 struct buf *bp;
532 int retval;
533
534 if(scsi_debug & PRINTROUTINES)
535 printf("st%d: done\n", unit);
536 if (! (xs->flags & INUSE))
537 panic("scsi_xfer not in use!");
538
539 if(bp = xs->bp) {
540 switch(xs->error) {
541 case XS_NOERROR:
542 bp->b_flags &= ~B_ERROR;
543 bp->b_error = 0;
544 bp->b_resid = 0;
545 break;
546 case XS_SENSE:
547 retval = st_interpret_sense(unit, xs);
548 if(retval) {
549 /*
550 * We have a real error, the bit should
551 * be set to indicate this. The return
552 * value will contain the unix error code*
553 * that the error interpretation routine
554 * thought was suitable, so pass this
555 * value back in the buf structure.
556 * Furthermore we return information
557 * saying that no data was transferred
558 */
559 bp->b_flags |= B_ERROR;
560 bp->b_error = retval;
561 bp->b_resid = bp->b_bcount;
562 st->flags &= ~(ST_AT_FILEMARK|ST_AT_EOM);
563 } else if(xs->resid && ( xs->resid != xs->datalen )) {
564 /*
565 * Here we have the tricky part..
566 * We successfully read less data than
567 * we requested. (but not 0)
568 *------for variable blocksize tapes:----*
569 * UNDER 386BSD:
570 * We should legitimatly have the error
571 * bit set, with the error value set to
572 * zero.. This is to indicate to the
573 * physio code that while we didn't get
574 * as much information as was requested,
575 * we did reach the end of the record
576 * and so physio should not call us
577 * again for more data... we have it all
578 * SO SET THE ERROR BIT!
579 *
580 * UNDER MACH (CMU) and NetBSD:
581 * To indicate the same as above, we
582 * need only have a non 0 resid that is
583 * less than the b_bcount, but the
584 * ERROR BIT MUST BE CLEAR! (sigh)
585 *
586 * UNDER OSF1:
587 * To indicate the same as above, we
588 * need to have a non 0 resid that is
589 * less than the b_bcount, but the
590 * ERROR BIT MUST BE SET! (gasp)(sigh)
591 *
592 *-------for fixed blocksize device------*
593 * We could have read some successful
594 * records before hitting
595 * the EOF or EOT. These must be passed
596 * to the user, before we report the
597 * EOx. Only if there is no data for the
598 * user do we report it now. (via an EIO
599 * for EOM and resid == count for EOF).
600 * We will report the EOx NEXT time..
601 */
602 bp->b_flags &= ~B_ERROR;
603 bp->b_error = 0;
604 bp->b_resid = xs->resid;
605 if((st->flags & ST_FIXEDBLOCKS)) {
606 bp->b_resid *= st->blkmin;
607 if( (st->flags & ST_AT_EOM)
608 && (bp->b_resid == bp->b_bcount)) {
609 bp->b_error = EIO;
610 st->flags &= ~ST_AT_EOM;
611 }
612 }
613 xs->error = XS_NOERROR;
614 break;
615 } else {
616 /*
617 * We have come out of the error handler
618 * with no error code.. we have also
619 * not had an ili (would have gone to
620 * the previous clause). Now we need to
621 * distiguish between succesful read of
622 * no data (EOF or EOM) and successfull
623 * read of all requested data.
624 * At least all o/s agree that:
625 * 0 bytes read with no error is EOF
626 * 0 bytes read with an EIO is EOM
627 */
628 bp->b_resid = bp->b_bcount;
629 if(st->flags & ST_AT_FILEMARK) {
630 st->flags &= ~ST_AT_FILEMARK;
631 bp->b_flags &= ~B_ERROR;
632 bp->b_error = 0;
633 break;
634 }
635 if(st->flags & ST_AT_EOM) {
636 bp->b_flags |= B_ERROR;
637 bp->b_error = EIO;
638 st->flags &= ~ST_AT_EOM;
639 break;
640 }
641 printf("st%d: error ignored\n", unit);
642 }
643 break;
644 case XS_TIMEOUT:
645 printf("st%d: timeout\n", unit);
646 break;
647 case XS_BUSY: /* should retry -- how? */
648 /*
649 * SHOULD put buf back at head of queue
650 * and decrement retry count in (*xs)
651 * HOWEVER, this should work as a kludge
652 */
653 if(xs->retries--) {
654 xs->flags &= ~ITSDONE;
655 xs->error = XS_NOERROR;
656 if ( (*(st->sc_sw->scsi_cmd))(xs)
657 == SUCCESSFULLY_QUEUED) {
658 /* don't wake the job, ok? */
659 return;
660 }
661 printf("st%d: device busy\n");
662 xs->flags |= ITSDONE;
663 }
664 case XS_DRIVER_STUFFUP:
665 bp->b_flags |= B_ERROR;
666 bp->b_error = EIO;
667 break;
668 default:
669 printf("st%d: unknown error category %d from scsi driver\n",
670 unit, xs->error);
671 }
672 biodone(bp);
673 xs->flags = 0; /* no longer in use */
674 ststart(unit); /* If there's another waiting.. do it */
675 } else
676 wakeup((caddr_t)xs);
677 }
678
679 /*
680 * Perform special action on behalf of the user
681 * Knows about the internals of this device
682 */
683 int
684 stioctl(dev_t dev, int cmd, caddr_t arg, int mode)
685 {
686 struct st_data *st;
687 struct mtop *mt;
688 struct mtget *g;
689 unsigned int opri;
690 unsigned char unit;
691 register i, j;
692 int errcode=0, number, flags, ret;
693
694 /*
695 * Find the device that the user is talking about
696 */
697 flags = 0; /* give error messages, act on errors etc. */
698 unit = UNIT(dev);
699 st = st_data[unit];
700
701 if(unit >= NST)
702 return ENXIO;
703 if(!st)
704 return ENXIO;
705
706 switch(cmd) {
707 default:
708 return EINVAL;
709 case MTIOCGET:
710 g = (struct mtget *)arg;
711 bzero(g, sizeof *g);
712 g->mt_type = 0x7; /* Ultrix compat */
713 ret=TRUE;
714 break;
715 case MTIOCTOP:
716 mt = (struct mtop *)arg;
717
718 if (st_debug)
719 printf("[sctape_sstatus: %x %x]\n", mt->mt_op, mt->mt_count);
720
721 /* compat: in U*x it is a short */
722 number = mt->mt_count;
723 switch ((short)(mt->mt_op)) {
724 case MTWEOF: /* write an end-of-file record */
725 ret = st_write_filemarks(unit, number, flags);
726 st->flags &= ~ST_WRITTEN;
727 break;
728 case MTFSF: /* forward space file */
729 ret = st_space(unit, number, SP_FILEMARKS, flags);
730 break;
731 case MTBSF: /* backward space file */
732 ret = st_space(unit, -number, SP_FILEMARKS, flags);
733 break;
734 case MTFSR: /* forward space record */
735 ret = st_space(unit, number, SP_BLKS, flags);
736 break;
737 case MTBSR: /* backward space record */
738 ret = st_space(unit, -number, SP_BLKS, flags);
739 break;
740 case MTREW: /* rewind */
741 ret = st_rewind(unit, FALSE, flags);
742 break;
743 case MTOFFL: /* rewind and put the drive offline */
744 if((ret = st_rewind(unit, FALSE, flags))) {
745 st_prevent(unit, PR_ALLOW, 0);
746 ret = st_load(unit, LD_UNLOAD, flags);
747 } else
748 printf("st%d: rewind failed; unit still loaded\n");
749 break;
750 case MTNOP: /* no operation, sets status only */
751 case MTCACHE: /* enable controller cache */
752 case MTNOCACHE: /* disable controller cache */
753 ret = TRUE;
754 break;
755 default:
756 return EINVAL;
757 }
758 break;
759 case MTIOCIEOT:
760 case MTIOCEEOT:
761 ret=TRUE;
762 break;
763 }
764 return ret ? ESUCCESS : EIO;
765 }
766
767
768 /*
769 * Check with the device that it is ok, (via scsi driver)*
770 */
771 int
772 st_req_sense(int unit, int flags)
773 {
774 struct scsi_sense_data sense;
775 struct scsi_sense scsi_cmd;
776
777 bzero(&scsi_cmd, sizeof(scsi_cmd));
778 scsi_cmd.op_code = REQUEST_SENSE;
779 scsi_cmd.length = sizeof(sense);
780
781 if (st_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
782 sizeof(scsi_cmd), (u_char *)&sense, sizeof(sense),
783 100000, flags | SCSI_DATA_IN) != 0)
784 return FALSE;
785 else
786 return TRUE;
787 }
788
789 /*
790 * Get scsi driver to send a "are you ready" command
791 */
792 int
793 st_test_ready(int unit, int flags)
794 {
795 struct scsi_test_unit_ready scsi_cmd;
796
797 bzero(&scsi_cmd, sizeof(scsi_cmd));
798 scsi_cmd.op_code = TEST_UNIT_READY;
799
800 if (st_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
801 sizeof(scsi_cmd), (u_char *)0, 0, 100000, flags) != 0)
802 return FALSE;
803 else
804 return TRUE;
805 }
806
807
808 #ifdef __STDC__
809 #define b2tol(a) (((unsigned)(a##_1) << 8) + (unsigned)a##_0 )
810 #else
811 #define b2tol(a) (((unsigned)(a/**/_1) << 8) + (unsigned)a/**/_0 )
812 #endif
813
814 /*
815 * Ask the drive what it's min and max blk sizes are.
816 */
817 int
818 st_rd_blk_lim(int unit, int flags)
819 {
820 struct st_data *st = st_data[unit];
821 struct scsi_blk_limits scsi_cmd;
822 struct scsi_blk_limits_data scsi_blkl;
823
824 st = st_data[unit];
825
826 /*
827 * First check if we have it all loaded
828 */
829 if (st->info_valid)
830 goto done;
831
832 /*
833 * do a 'Read Block Limits'
834 */
835 bzero(&scsi_cmd, sizeof(scsi_cmd));
836 scsi_cmd.op_code = READ_BLK_LIMITS;
837
838 /*
839 * do the command, update the global values
840 */
841 if (st_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
842 sizeof(scsi_cmd), (u_char *)&scsi_blkl, sizeof(scsi_blkl),
843 5000, flags | SCSI_DATA_IN) != 0) {
844 if(!(flags & SCSI_SILENT))
845 printf("st%d: read block limits failed\n", unit);
846 st->info_valid = FALSE;
847 return FALSE;
848 }
849 if (st_debug)
850 printf("st%d: block size min %d max %d\n", unit,
851 b2tol(scsi_blkl.min_length),
852 _3btol(&scsi_blkl.max_length_2));
853
854 st->blkmin = b2tol(scsi_blkl.min_length);
855 st->blkmax = _3btol(&scsi_blkl.max_length_2);
856
857 done:
858 if(st->blkmin && (st->blkmin == st->blkmax))
859 st->flags |= ST_FIXEDBLOCKS;
860 return TRUE;
861 }
862
863 /*
864 * Get the scsi driver to send a full inquiry to the
865 * device and use the results to fill out the global
866 * parameter structure.
867 */
868 int
869 st_mode_sense(int unit, int flags)
870 {
871 struct st_data *st = st_data[unit];
872 struct scsi_mode_sense scsi_cmd;
873 struct {
874 struct scsi_mode_header_tape header;
875 struct blk_desc blk_desc;
876 } scsi_s;
877
878 /*
879 * First check if we have it all loaded
880 */
881 if(st->info_valid)
882 return TRUE;
883 /*
884 * First do a mode sense
885 */
886 bzero(&scsi_cmd, sizeof(scsi_cmd));
887 scsi_cmd.op_code = MODE_SENSE;
888 scsi_cmd.length = sizeof(scsi_s);
889
890 /*
891 * do the command, but we don't need the results
892 * just print them for our interest's sake
893 */
894 if (st_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
895 sizeof(scsi_cmd), (u_char *)&scsi_s, sizeof(scsi_s),
896 5000, flags | SCSI_DATA_IN) != 0) {
897 if(!(flags & SCSI_SILENT))
898 printf("st%d: mode sense failed\n", unit);
899 st->info_valid = FALSE;
900 return FALSE;
901 }
902 if (st_debug)
903 printf("st%d: %d blocks of %d bytes, write %s, %sbuffered\n",
904 unit,
905 _3btol((u_char *)&scsi_s.blk_desc.nblocks),
906 _3btol((u_char *)&scsi_s.blk_desc.blklen),
907 scsi_s.header.write_protected ? "protected" : "enabled",
908 scsi_s.header.buf_mode ? "" : "un");
909
910 st->numblks = _3btol((u_char *)&scsi_s.blk_desc.nblocks);
911 st->blksiz = _3btol((u_char *)&scsi_s.blk_desc.blklen);
912 return TRUE;
913 }
914
915 /*
916 * Get the scsi driver to send a full inquiry to the
917 * device and use the results to fill out the global
918 * parameter structure.
919 */
920 int
921 st_mode_select(int unit, int flags, int dsty_code)
922 {
923 struct st_data *st = st_data[unit];
924 struct scsi_mode_select scsi_cmd;
925 struct {
926 struct scsi_mode_header_tape header;
927 struct blk_desc blk_desc;
928 } dat;
929
930 /*
931 * Set up for a mode select
932 */
933 bzero(&dat, sizeof(dat));
934 bzero(&scsi_cmd, sizeof(scsi_cmd));
935 scsi_cmd.op_code = MODE_SELECT;
936 scsi_cmd.length = sizeof(dat);
937 dat.header.blk_desc_len = sizeof(struct blk_desc);
938 dat.header.buf_mode = 1;
939 dat.blk_desc.density = dsty_code;
940 if(st->flags & ST_FIXEDBLOCKS)
941 lto3b(st->blkmin, dat.blk_desc.blklen);
942
943 /* lto3b( st->numblks, dat.blk_desc.nblocks); use defaults!!!!
944 lto3b( st->blksiz, dat.blk_desc.blklen);
945 */
946 /*
947 * do the command
948 */
949 if (st_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
950 sizeof(scsi_cmd), (u_char *)&dat, sizeof(dat),
951 5000, flags | SCSI_DATA_OUT) != 0) {
952 if(!(flags & SCSI_SILENT))
953 printf("st%d: mode select failed\n", unit);
954 #if 0
955 st->info_valid = FALSE;
956 return FALSE;
957 #endif
958 }
959 return TRUE;
960 }
961
962 /*
963 * skip N blocks/filemarks/seq filemarks/eom
964 */
965 int
966 st_space(int unit, int number, int what, int flags)
967 {
968 struct st_data *st = st_data[unit];
969 struct scsi_space scsi_cmd;
970
971 /* if we are at a filemark now, we soon won't be*/
972 st->flags &= ~(ST_AT_FILEMARK | ST_AT_EOM);
973 bzero(&scsi_cmd, sizeof(scsi_cmd));
974 scsi_cmd.op_code = SPACE;
975 scsi_cmd.code = what;
976 lto3b(number, scsi_cmd.number);
977 if (st_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
978 sizeof(scsi_cmd), (u_char *)0, 0, 600000, flags) != 0) {
979 if(!(flags & SCSI_SILENT))
980 printf("st%d: %s space failed\n", unit,
981 (number > 0) ? "forward" : "backward");
982 st->info_valid = FALSE;
983 return FALSE;
984 }
985 return TRUE;
986 }
987
988 /*
989 * write N filemarks
990 */
991 int
992 st_write_filemarks(int unit, int number, int flags)
993 {
994 struct st_data *st = st_data[unit];
995 struct scsi_write_filemarks scsi_cmd;
996
997 st->flags &= ~(ST_AT_FILEMARK);
998 bzero(&scsi_cmd, sizeof(scsi_cmd));
999 scsi_cmd.op_code = WRITE_FILEMARKS;
1000 lto3b(number, scsi_cmd.number);
1001 if (st_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
1002 sizeof(scsi_cmd), (u_char *)0, 0, 100000, flags) != 0) {
1003 if(!(flags & SCSI_SILENT))
1004 printf("st%d: write file marks failed\n", unit);
1005 st->info_valid = FALSE;
1006 return FALSE;
1007 }
1008 return TRUE;
1009 }
1010
1011 /*
1012 * load /unload (with retension if true)
1013 */
1014 int
1015 st_load(int unit, int type, int flags)
1016 {
1017 struct st_data *st = st_data[unit];
1018 struct scsi_load scsi_cmd;
1019
1020 st->flags &= ~(ST_AT_FILEMARK | ST_AT_EOM);
1021 bzero(&scsi_cmd, sizeof(scsi_cmd));
1022 scsi_cmd.op_code = LOAD_UNLOAD;
1023 scsi_cmd.load=type;
1024 if (type == LD_LOAD)
1025 {
1026 /*scsi_cmd.reten=TRUE;*/
1027 scsi_cmd.reten=FALSE;
1028 }
1029 else
1030 {
1031 scsi_cmd.reten=FALSE;
1032 }
1033 if (st_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
1034 sizeof(scsi_cmd), (u_char *)0, 0, 30000, flags) != 0) {
1035 if(!(flags & SCSI_SILENT))
1036 printf("st%d: %s failed\n", unit,
1037 type == LD_LOAD ? "load" : "unload");
1038 st->info_valid = FALSE;
1039 return FALSE;
1040 }
1041 return TRUE;
1042 }
1043
1044 /*
1045 * Prevent or allow the user to remove the tape
1046 */
1047 int
1048 st_prevent(int unit, int type, int flags)
1049 {
1050 struct st_data *st = st_data[unit];
1051 struct scsi_prevent scsi_cmd;
1052
1053 bzero(&scsi_cmd, sizeof(scsi_cmd));
1054 scsi_cmd.op_code = PREVENT_ALLOW;
1055 scsi_cmd.prevent=type;
1056 if (st_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
1057 sizeof(scsi_cmd), (u_char *)0, 0, 5000, flags) != 0) {
1058 if(!(flags & SCSI_SILENT))
1059 printf("st%d: %s failed\n", unit,
1060 type == PR_PREVENT ? "prevent" : "allow");
1061 st->info_valid = FALSE;
1062 return FALSE;
1063 }
1064 return TRUE;
1065 }
1066
1067 /*
1068 * Rewind the device
1069 */
1070 int
1071 st_rewind(int unit, int immed, int flags)
1072 {
1073 struct st_data *st = st_data[unit];
1074 struct scsi_rewind scsi_cmd;
1075
1076 st->flags &= ~(ST_AT_FILEMARK | ST_AT_EOM);
1077 bzero(&scsi_cmd, sizeof(scsi_cmd));
1078 scsi_cmd.op_code = REWIND;
1079 scsi_cmd.immed=immed;
1080 if (st_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
1081 sizeof(scsi_cmd), (u_char *)0, 0, immed?5000:300000, flags) != 0) {
1082 if(!(flags & SCSI_SILENT))
1083 printf("st%d: rewind failed\n", unit);
1084 st->info_valid = FALSE;
1085 return FALSE;
1086 }
1087 return TRUE;
1088 }
1089
1090 /*
1091 * ask the scsi driver to perform a command for us.
1092 * Call it through the switch table, and tell it which
1093 * sub-unit we want, and what target and lu we wish to
1094 * talk to. Also tell it where to find the command
1095 * how long int is.
1096 * Also tell it where to read/write the data, and how
1097 * long the data is supposed to be
1098 */
1099 int
1100 st_scsi_cmd(int unit, struct scsi_generic *scsi_cmd, int cmdlen,
1101 u_char *data_addr, int datalen, int timeout, int flags)
1102 {
1103 struct st_data *st = st_data[unit];
1104 struct scsi_xfer *xs;
1105 int retval, s;
1106
1107 if(scsi_debug & PRINTROUTINES)
1108 printf("\nst_scsi_cmd%d ", unit);
1109 if(!st->sc_sw) {
1110 printf("st%d: not set up\n", unit);
1111 return EINVAL;
1112 }
1113
1114 xs = &st->scsi_xfer;
1115 if(!(flags & SCSI_NOMASK))
1116 s = splbio();
1117 st->blockwait++; /* there is someone waiting */
1118 while (xs->flags & INUSE)
1119 tsleep((caddr_t)&st->blockwait, PRIBIO+1, "st_cmd1", 0);
1120 st->blockwait--;
1121 xs->flags = INUSE;
1122 if(!(flags & SCSI_NOMASK))
1123 splx(s);
1124
1125 /*
1126 * Fill out the scsi_xfer structure
1127 */
1128 xs->flags |= flags;
1129 xs->adapter = st->ctlr;
1130 xs->targ = st->targ;
1131 xs->lu = st->lu;
1132 xs->retries = ST_RETRIES;
1133 xs->timeout = timeout;
1134 xs->cmd = scsi_cmd;
1135 xs->cmdlen = cmdlen;
1136 xs->data = data_addr;
1137 xs->datalen = datalen;
1138 xs->resid = datalen;
1139 xs->when_done = (flags & SCSI_NOMASK) ? (int (*)())0 : st_done;
1140 xs->done_arg = unit;
1141 xs->done_arg2 = (int)xs;
1142 retry:
1143 xs->error = XS_NOERROR;
1144 xs->bp = 0;
1145 retval = (*(st->sc_sw->scsi_cmd))(xs);
1146 switch(retval) {
1147 case SUCCESSFULLY_QUEUED:
1148 s = splbio();
1149 while(!(xs->flags & ITSDONE))
1150 tsleep((caddr_t)xs,PRIBIO+1, "st_cmd2", 0);
1151 splx(s);
1152 case HAD_ERROR:
1153 case COMPLETE:
1154 switch(xs->error) {
1155 case XS_NOERROR:
1156 retval = ESUCCESS;
1157 break;
1158 case XS_SENSE:
1159 retval = st_interpret_sense(unit, xs);
1160 /* only useful for reads */
1161 if (retval)
1162 st->flags &= ~(ST_AT_FILEMARK | ST_AT_EOM);
1163 else {
1164 xs->error = XS_NOERROR;
1165 retval = ESUCCESS;
1166 }
1167 break;
1168 case XS_DRIVER_STUFFUP:
1169 retval = EIO;
1170 break;
1171 case XS_TIMEOUT:
1172 case XS_BUSY:
1173 if(xs->retries-- ) {
1174 xs->flags &= ~ITSDONE;
1175 goto retry;
1176 }
1177 retval = EIO;
1178 break;
1179 default:
1180 retval = EIO;
1181 printf("st%d: unknown error category %d from scsi driver\n",
1182 unit, xs->error);
1183 break;
1184 }
1185 break;
1186 case TRY_AGAIN_LATER:
1187 if(xs->retries--) {
1188 xs->flags &= ~ITSDONE;
1189 goto retry;
1190 }
1191 retval = EIO;
1192 break;
1193 default:
1194 retval = EIO;
1195 }
1196 xs->flags = 0; /* it's free! */
1197 ststart(unit);
1198
1199 return retval;
1200 }
1201
1202 /*
1203 * Look at the returned sense and act on the error and detirmine
1204 * The unix error number to pass back... (0 = report no error)
1205 */
1206
1207 int
1208 st_interpret_sense(int unit, struct scsi_xfer *xs)
1209 {
1210 struct st_data *st = st_data[unit];
1211 struct scsi_sense_data *sense;
1212 int silent = xs->flags & SCSI_SILENT, key;
1213
1214 /*
1215 * If errors are ok, report a success
1216 */
1217 if(xs->flags & SCSI_ERR_OK)
1218 return ESUCCESS;
1219
1220 /*
1221 * Get the sense fields and work out what CLASS
1222 */
1223 sense = &(xs->sense);
1224 if(st_debug) {
1225 int count = 0;
1226 printf("code%x class%x valid%x\n", sense->error_code,
1227 sense->error_class, sense->valid);
1228 printf("seg%x key%x ili%x eom%x fmark%x\n",
1229 sense->ext.extended.segment, sense->ext.extended.sense_key,
1230 sense->ext.extended.ili, sense->ext.extended.eom,
1231 sense->ext.extended.filemark);
1232 printf("info: %x %x %x %x followed by %d extra bytes\n",
1233 sense->ext.extended.info[0], sense->ext.extended.info[1],
1234 sense->ext.extended.info[2], sense->ext.extended.info[3],
1235 sense->ext.extended.extra_len);
1236 printf("extra: ");
1237 while(count < sense->ext.extended.extra_len)
1238 printf("%x ", sense->ext.extended.extra_bytes[count++]);
1239 printf("\n");
1240 }
1241
1242 switch(sense->error_class) {
1243 case 0:
1244 case 1:
1245 case 2:
1246 case 3:
1247 case 4:
1248 case 5:
1249 case 6:
1250 if(!silent) {
1251 printf("st%d: error class %d code %d\n", unit,
1252 sense->error_class, sense->error_code);
1253 if(sense->valid)
1254 printf("block no. %d (decimal)\n",
1255 (sense->ext.unextended.blockhi <<16),
1256 + (sense->ext.unextended.blockmed <<8),
1257 + (sense->ext.unextended.blocklow ));
1258 }
1259 return EIO;
1260 case 7:
1261 /*
1262 * If it's class 7, use the extended stuff and interpret
1263 * the key
1264 */
1265 if(sense->ext.extended.eom)
1266 st->flags |= ST_AT_EOM;
1267 if(sense->ext.extended.filemark)
1268 st->flags |= ST_AT_FILEMARK;
1269
1270 if(sense->ext.extended.ili) {
1271 if(sense->valid) {
1272 /*
1273 * In all ili cases, note that
1274 * the resid is non-0 AND not
1275 * unchanged.
1276 */
1277 xs->resid = ntohl(*((long *)sense->ext.extended.info));
1278 if(xs->bp) {
1279 if(xs->resid < 0) {
1280 /* never on block devices */
1281 /*
1282 * it's only really bad
1283 * if we have lost data
1284 * (the record was
1285 * bigger than the read)
1286 */
1287 return EIO;
1288 }
1289 }
1290 } else
1291 printf("st%d: bad length error?", unit);
1292 }
1293
1294 key = sense->ext.extended.sense_key;
1295 switch(key) {
1296 case 0x0:
1297 return ESUCCESS;
1298 case 0x1:
1299 if(!silent) {
1300 printf("st%d: soft error (corrected)", unit);
1301 if(sense->valid) {
1302 printf(" block %d\n",
1303 (sense->ext.extended.info[0] <<24)|
1304 (sense->ext.extended.info[1] <<16)|
1305 (sense->ext.extended.info[2] <<8)|
1306 (sense->ext.extended.info[3] ));
1307 } else
1308 printf("\n");
1309 }
1310 return ESUCCESS;
1311 case 0x2:
1312 if(!silent)
1313 printf("st%d: not ready\n", unit);
1314 return ENODEV;
1315 case 0x3:
1316 if(!silent) {
1317 printf("st%d: medium error", unit);
1318 if(sense->valid) {
1319 printf(" block %d\n",
1320 (sense->ext.extended.info[0] <<24)|
1321 (sense->ext.extended.info[1] <<16)|
1322 (sense->ext.extended.info[2] <<8)|
1323 (sense->ext.extended.info[3] ));
1324 } else
1325 printf("\n");
1326 }
1327 return EIO;
1328 case 0x4:
1329 if(!silent)
1330 printf("st%d: component failure\n",
1331 unit);
1332 return EIO;
1333 case 0x5:
1334 if(!silent)
1335 printf("st%d: illegal request\n", unit);
1336 return EINVAL;
1337 case 0x6:
1338 if(!silent)
1339 printf("st%d: media change\n", unit);
1340 st->flags &= ~(ST_AT_FILEMARK|ST_AT_EOM);
1341 st->info_valid = FALSE;
1342 if (st->flags & ST_OPEN) /* TEMP!!!! */
1343 return EIO;
1344 return ESUCCESS;
1345 case 0x7:
1346 if(!silent) {
1347 printf("st%d: attempted protection violation",
1348 unit);
1349 if(sense->valid) {
1350 printf(" block %d\n",
1351 (sense->ext.extended.info[0] <<24)|
1352 (sense->ext.extended.info[1] <<16)|
1353 (sense->ext.extended.info[2] <<8)|
1354 (sense->ext.extended.info[3] ));
1355 } else
1356 printf("\n");
1357 }
1358 return EACCES;
1359 case 0x8:
1360 if(!silent) {
1361 printf("st%d: block wrong state (worm)", unit);
1362 if(sense->valid) {
1363 printf(" block %d\n",
1364 (sense->ext.extended.info[0] <<24)|
1365 (sense->ext.extended.info[1] <<16)|
1366 (sense->ext.extended.info[2] <<8)|
1367 (sense->ext.extended.info[3] ));
1368 } else
1369 printf("\n");
1370 }
1371 return EIO;
1372 case 0x9:
1373 if(!silent)
1374 printf("st%d: vendor unique\n", unit);
1375 return EIO;
1376 case 0xa:
1377 if(!silent)
1378 printf("st%d: copy aborted\n", unit);
1379 return EIO;
1380 case 0xb:
1381 if(!silent)
1382 printf("st%d: command aborted\n", unit);
1383 return EIO;
1384 case 0xc:
1385 if(!silent) {
1386 printf("st%d: search returned", unit);
1387 if(sense->valid) {
1388 printf(" block %d\n",
1389 (sense->ext.extended.info[0] <<24)|
1390 (sense->ext.extended.info[1] <<16)|
1391 (sense->ext.extended.info[2] <<8)|
1392 (sense->ext.extended.info[3] ));
1393 } else
1394 printf("\n");
1395 }
1396 return ESUCCESS;
1397 case 0xd:
1398 if(!silent)
1399 printf("st%d: volume overflow\n", unit);
1400 return ENOSPC;
1401 case 0xe:
1402 if(!silent) {
1403 printf("st%d: verify miscompare\n", unit);
1404 if(sense->valid) {
1405 printf("block no. %d (decimal)\n",
1406 (sense->ext.extended.info[0] <<24)|
1407 (sense->ext.extended.info[1] <<16)|
1408 (sense->ext.extended.info[2] <<8)|
1409 (sense->ext.extended.info[3] ));
1410 } else
1411 printf("\n");
1412 }
1413 return EIO;
1414 case 0xf:
1415 if(!silent)
1416 printf("st%d: unknown error key\n", unit);
1417 return EIO;
1418 }
1419 break;
1420 }
1421 return 0;
1422 }
1423