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