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