summaryrefslogtreecommitdiffstats
path: root/sys/scsi/st.c
blob: 438e0bb3b088159596af7503ab3717356f7af72b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
/*
 * Written by Julian Elischer (julian@tfs.com)(now julian@DIALix.oz.au)
 * for TRW Financial Systems for use under the MACH(2.5) operating system.
 *
 * TRW Financial Systems, in accordance with their agreement with Carnegie
 * Mellon University, makes this software available to CMU to distribute
 * or use in any manner that they see fit as long as this message is kept with
 * the software. For this reason TFS also grants any other persons or
 * organisations permission to use or modify this software.
 *
 * TFS supplies this software to be publicly redistributed
 * on the understanding that TFS is not responsible for the correct
 * functioning of this software in any circumstances.
 *
 * $Id: st.c,v 1.72 1996/07/23 21:52:31 phk Exp $
 */

/*
 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
 * major changes by Julian Elischer (julian@jules.dialix.oz.au) May 1993
 */

/*
 * To do:
 * work out some better way of guessing what a good timeout is going
 * to be depending on whether we expect to retension or not.
 *
 */

#include "opt_bounce.h"
#include "opt_scsi.h"

#include <sys/types.h>

#include <sys/param.h>
#include <sys/systm.h>

#include <sys/fcntl.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/malloc.h>
#include <sys/buf.h>
#include <sys/proc.h>
#include <sys/mtio.h>
#include <sys/conf.h>
#include <sys/kernel.h>
#ifdef DEVFS
#include <sys/devfsext.h>
#endif /*DEVFS*/

#include <scsi/scsi_all.h>
#include <scsi/scsi_tape.h>
#include <scsi/scsiconf.h>



/* Defines for device specific stuff */
#define		PAGE_0_SENSE_DATA_SIZE	12
#define DEF_FIXED_BSIZE  512
#define	ST_RETRIES	4	/* only on non IO commands */

#define STUNIT(DEV)      ((minor(DEV)&0xF0) >> 4)    /* 4 bit unit.  */
#define STSETUNIT(DEV, U) makedev(major(DEV), ((U) << 4))

#define MODE(z)		(  (minor(z)       & 0x03) )
#define DSTY(z)         ( ((minor(z) >> 2) & 0x03) )
#define CTLMODE	3

#define IS_CTLMODE(DEV) (MODE(DEV) == CTLMODE)

static errval	st_space __P((u_int32_t unit, int32_t number, u_int32_t what, u_int32_t flags));
static errval	st_rewind __P((u_int32_t unit, boolean immed, u_int32_t flags));
static errval	st_erase __P((u_int32_t unit, boolean immed, u_int32_t flags));
static errval st_mode_sense __P((u_int32_t unit, u_int32_t flags, \
	struct tape_pages *page, u_int32_t pagelen, u_int32_t pagecode));
static errval	st_decide_mode __P((u_int32_t unit, boolean first_read));
static errval	st_read __P((u_int32_t unit, char *buf, u_int32_t size,
	u_int32_t flags));
static errval	st_rd_blk_lim __P((u_int32_t unit, u_int32_t flags));
static errval	st_touch_tape __P((u_int32_t unit));
static errval	st_write_filemarks __P((u_int32_t unit, int32_t number, u_int32_t flags));
static errval	st_load __P((u_int32_t unit, u_int32_t type, u_int32_t flags));
static errval	st_mode_select __P((u_int32_t unit, u_int32_t flags, \
	struct tape_pages *page, u_int32_t pagelen));
static errval	st_comp __P((u_int32_t unit, u_int32_t mode));
static int32_t	st_chkeod __P((u_int32_t unit, boolean position, int32_t *nmarks,
	u_int32_t flags));
static void	ststart(u_int32_t	unit, u_int32_t flags);
static void	st_unmount __P((int unit, boolean eject));
static errval	st_mount_tape __P((dev_t dev, u_int32_t flags));
static void	st_loadquirks __P((struct scsi_link *sc_link));
static errval	st_interpret_sense __P((struct scsi_xfer *xs));

#define ESUCCESS 0
#define NOEJECT 0
#define EJECT 1

struct scsi_data {
/*--------------------present operating parameters, flags etc.----------------*/
	u_int32_t flags;		/* see below                          */
	u_int32_t blksiz;		/* blksiz we are using                */
	u_int32_t density;	/* present density                    */
	u_int32_t comp;		/* present compression mode           */
	u_int32_t quirks;		/* quirks for the open mode           */
	u_int32_t last_dsty;	/* last density openned               */
/*--------------------parameters reported by the device ----------------------*/
	u_int32_t blkmin;		/* min blk size                       */
	u_int32_t blkmax;		/* max blk size                       */
/*--------------------parameters reported by the device for this media--------*/
	u_int32_t numblks;	/* nominal blocks capacity            */
	u_int32_t media_blksiz;	/* 0 if not ST_FIXEDBLOCKS            */
	u_int32_t media_density;	/* this is what it said when asked    */
/*--------------------quirks for the whole drive------------------------------*/
	u_int32_t drive_quirks;	/* quirks of this drive               */
/*--------------------How we should set up when openning each minor device----*/
	st_modes modes; /* plus more for each mode            */
	u_int8_t modeflags[4];	/* flags for the modes                */
#define DENSITY_SET_BY_USER	0x01
#define DENSITY_SET_BY_QUIRK	0x02
#define BLKSIZE_SET_BY_USER	0x04
#define BLKSIZE_SET_BY_QUIRK	0x08
#define COMPRES_SET_BY_USER	0x10
#define COMPRES_SET_BY_QUIRK	0x20
/*--------------------storage for sense data returned by the drive------------*/
	unsigned char saved_page0[PAGE_0_SENSE_DATA_SIZE];	/*
					 * additional sense data needed
					 * for mode sense/select.
					 */
	struct buf_queue_head buf_queue;
	struct scsi_xfer scsi_xfer;	/* scsi xfer struct for this drive */
	u_int32_t xfer_block_wait;	/* is a process waiting? */
#ifdef	DEVFS
	struct {
		void	*rst;
		void	*nrst;
		void	*erst;
	/* end of aliases */
		void	*rst_[4];
		void	*nrst_[4];
		void	*erst_[4];
		void	*ctl_[4];
		void	*ctl;
	} devfs_token;
#endif
};

static int stunit(dev_t dev) { return STUNIT(dev); }
static dev_t stsetunit(dev_t dev, int unit) { return STSETUNIT(dev, unit); }

static errval st_open(dev_t dev, int flags, int fmt, struct proc *p,
		struct scsi_link *sc_link);
static errval st_ioctl(dev_t dev, int cmd, caddr_t addr, int flag,
		struct proc *p, struct scsi_link *sc_link);
static errval st_close(dev_t dev, int flag, int fmt, struct proc *p,
        struct scsi_link *sc_link);
static void st_strategy(struct buf *bp, struct scsi_link *sc_link);

static d_open_t		stopen;
static d_close_t	stclose;
static d_ioctl_t	stioctl;
static d_strategy_t	ststrategy;

#define CDEV_MAJOR 14
#define BDEV_MAJOR 5
static struct cdevsw st_cdevsw;
static struct bdevsw st_bdevsw = 
	{ stopen,	stclose,	ststrategy,	stioctl,	/*5*/
	  nodump,	nopsize,	0,	"st",	&st_cdevsw,	-1 };

SCSI_DEVICE_ENTRIES(st)

static struct scsi_device st_switch =
{
	st_interpret_sense,	/* check errors with us first */
	ststart,	 /* we have a queue, and this is how we service it */
	NULL,
	NULL,			/* use the default 'done' routine */
	"st",
	0,
	{0, 0},
	0,				/* Link flags */
	stattach,
	"Sequential-Access",
	stopen,
	sizeof(struct scsi_data),
	T_SEQUENTIAL,
	stunit,
	stsetunit,
	st_open,
	st_ioctl,
	st_close,
	st_strategy,
};

#define ST_INITIALIZED	0x01
#define	ST_INFO_VALID	0x02
#define ST_OPEN		0x04
#define	ST_BLOCK_SET	0x08	/* block size, mode set by ioctl      */
#define	ST_WRITTEN	0x10	/* data have been written, EOD needed */
#define	ST_FIXEDBLOCKS	0x20
#define	ST_AT_FILEMARK	0x40
#define	ST_EIO_PENDING	0x80	/* we couldn't report it then (had data) */
#define	ST_NEW_MOUNT	0x100	/* still need to decide mode              */
#define	ST_READONLY	0x200	/* st_mode_sense says write protected */
#define	ST_FM_WRITTEN	0x400	/*
				 * EOF file mark written  -- used with
				 * ~ST_WRITTEN to indicate that multiple file
				 * marks have been written
				 */
#define	ST_BLANK_READ	0x800	/* BLANK CHECK encountered already */
#define	ST_2FM_AT_EOD	0x1000	/* write 2 file marks at EOD */
#define	ST_MOUNTED	0x2000	/* Device is presently mounted */
#define	ST_SENSE_READ	0x4000	/* mode sense read from drive */

#define	ST_PER_ACTION	(ST_AT_FILEMARK | ST_EIO_PENDING | ST_BLANK_READ)
#define	ST_PER_MOUNT	(ST_INFO_VALID | ST_BLOCK_SET | ST_WRITTEN | \
			ST_FIXEDBLOCKS | ST_READONLY | \
			ST_FM_WRITTEN | ST_2FM_AT_EOD | ST_PER_ACTION)

/*
 * The routine called by the low level scsi routine when it discovers
 * a device suitable for this driver
 */

static	errval
stattach(struct scsi_link *sc_link)
{
	u_int32_t unit;
#ifdef DEVFS
	int ii;
#endif /*DEVFS*/

	struct scsi_data *st = sc_link->sd;

	unit = sc_link->dev_unit;

	TAILQ_INIT(&st->buf_queue);
	/*
	 * Check if the drive is a known criminal and take
	 * Any steps needed to bring it into line
	 */
	st_loadquirks(sc_link);
	/*
	 * Use the subdriver to request information regarding
	 * the drive. We cannot use interrupts yet, so the
	 * request must specify this.
	 */
	if (st_mode_sense(unit, SCSI_NOSLEEP | SCSI_NOMASK | SCSI_SILENT,
		NULL, 0, 0)) {
		printf("drive offline");
	} else {
		printf("density code 0x%lx, ", st->media_density);
		if (!scsi_test_unit_ready(sc_link, SCSI_NOSLEEP | SCSI_NOMASK | SCSI_SILENT)) {
			if (st->media_blksiz) {
				printf("%ld-byte", st->media_blksiz);
			} else {
				printf("variable");
			}
			printf(" blocks, write-%s",
			    (st->flags & ST_READONLY) ? "protected" : "enabled");
		} else {
			printf(" drive empty");
		}
	}
	/*
	 * Set up the buf queue for this device
	 */
	st->flags |= ST_INITIALIZED;
#ifdef	DEVFS
	for(ii=0; ii<4; ii++) {
		st->devfs_token.rst_[ii] = 
			devfs_add_devswf(&st_cdevsw, 
					 (unit << 4 ) + (ii * 4), DV_CHR, 
					 UID_ROOT, GID_OPERATOR, 0660, 
					 "rst%ld.%d", unit, ii);
		st->devfs_token.nrst_[ii] = 
			devfs_add_devswf(&st_cdevsw, 
					 (unit << 4 ) + ((ii * 4) + 1), DV_CHR, 
					 UID_ROOT, GID_OPERATOR, 0660, 
					 "nrst%ld.%d", unit, ii);
		st->devfs_token.erst_[ii] = 
			devfs_add_devswf(&st_cdevsw, 
					 (unit << 4 ) + ((ii * 4) + 2), DV_CHR,
					 UID_ROOT, GID_OPERATOR, 0660, 
					 "erst%ld.%d", unit, ii);
		st->devfs_token.ctl_[ii] = 
			devfs_add_devswf(&st_cdevsw, 
					 (unit << 4 ) + ((ii * 4) + 3), DV_CHR,	
					 UID_ROOT, GID_OPERATOR, 0600, 
					 "st%ldctl.%d", unit, ii);
	}

	st->devfs_token.ctl = 
		devfs_add_devswf(&st_cdevsw, (unit << 4 ) | SCSI_CONTROL_MASK,
				 DV_CHR, UID_ROOT, GID_WHEEL, 0600,
				 "rst%d.ctl", unit);
	/** add links **/
	st->devfs_token.rst = 
		devfs_link(st->devfs_token.rst_[0], "rst%ld", unit);
	st->devfs_token.nrst = 
		devfs_link(st->devfs_token.nrst_[0], "nrst%ld", unit);
	st->devfs_token.erst = 
		devfs_link(st->devfs_token.erst_[0], "erst%ld", unit);
#endif
	return 0;
}


/*
 * initialise the subdevices to the default (QUIRK) state.
 * this will remove any setting made by the system operator or previous
 * operations.
 */
static	void
st_loadquirks(sc_link)
	struct scsi_link *sc_link;
{
	struct scsi_data *st = sc_link->sd;
	int     i;
	struct	st_mode *mode;
	struct	st_mode *mode2;

	mode = (struct st_mode*) sc_link->devmodes;
	if (!mode)
		return;

	st->quirks = st->drive_quirks = sc_link->quirks;

	mode2 = st->modes;

	for (i = 0; i < 4; i++) {
		bzero(mode2, sizeof(*mode2));
		st->modeflags[i] &= ~(BLKSIZE_SET_BY_QUIRK
		    | DENSITY_SET_BY_QUIRK
		    | BLKSIZE_SET_BY_USER
		    | DENSITY_SET_BY_USER);
		if (mode->blksiz && ((mode->quirks | st->drive_quirks)
			& (ST_Q_FORCE_FIXED_MODE))) {
			mode2->blksiz = mode->blksiz;
			st->modeflags[i] |= BLKSIZE_SET_BY_QUIRK;
		} else {
			if ((mode->quirks | st->drive_quirks)
			    & ST_Q_FORCE_VAR_MODE) {
				mode2->blksiz = 0;
				st->modeflags[i] |= BLKSIZE_SET_BY_QUIRK;
			}
		}
		if (mode->density) {
			mode2->density = mode->density;
			st->modeflags[i] |= DENSITY_SET_BY_QUIRK;
		}
		mode++;
		mode2++;
	}
}

/*
 * open the device.
 */
static	errval
st_open(dev_t dev, int flags, int fmt, struct proc *p,
struct scsi_link *sc_link)
{
	u_int32_t unit, mode, dsty;
	errval  errno = 0;
	struct scsi_data *st;

	unit = STUNIT(dev);
	mode = MODE(dev);
	dsty = DSTY(dev);

	st = sc_link->sd;
	/*
	 * Make sure the device has been initialised
	 */
	if ((st == NULL) || (!(st->flags & ST_INITIALIZED)))
		return (ENXIO);

	/*
	 * Only allow one at a time
	 */
	if (st->flags & ST_OPEN) {
		return (EBUSY);
	}
	/*
	 * Throw out a dummy instruction to catch 'Unit attention
	 * errors (the error handling will invalidate all our
	 * device info if we get one, but otherwise, ignore it)
	 */
	scsi_test_unit_ready(sc_link, SCSI_SILENT);

	sc_link->flags |= SDEV_OPEN;	/* unit attn are now errors */
	/*
	 * If the mode is 3 (e.g. minor = 3,7,11,15)
	 * then the device has been openned to set defaults
	 * This mode does NOT ALLOW I/O, only ioctls.
	 * XXX: Where do we lock out I/O?
	 */
	if (IS_CTLMODE(dev))
		return 0;

	/*
	 * Check that the device is ready to use (media loaded?)
	 * This time take notice of the return result
	 */
	if ( (errno = scsi_test_unit_ready(sc_link, 0)) ) {
		uprintf("st%d: not ready\n", unit);
		st_unmount(unit, NOEJECT);
		return (errno);
	}
	/*
	 * if it's a different mode, or if the media has been
	 * invalidated, unmount the tape from the previous
	 * session but continue with open processing
	 */
	if ((st->last_dsty != dsty)
	    || (!(sc_link->flags & SDEV_MEDIA_LOADED))) {
		st_unmount(unit, NOEJECT);
	}
	/*
	 * If we are not mounted, then we should start a new
	 * mount session.
	 */
	if (!(st->flags & ST_MOUNTED)) {
		st_mount_tape(dev, flags);
		st->last_dsty = dsty;
	}
	/*
	 * Make sure that a tape opened in write-only mode will have
	 * file marks written on it when closed, even if not written to.
	 * This is for SUN compatibility
	 */
	if ((flags & O_ACCMODE) == FWRITE)
		st->flags |= ST_WRITTEN;

	scsi_prevent(sc_link, PR_PREVENT, 0);	/* who cares if it fails? */

	SC_DEBUG(sc_link, SDEV_DB2, ("Open complete\n"));

	st->flags |= ST_OPEN;
	return 0;
}

/*
 * close the device.. only called if we are the LAST
 * occurence of an open device
 */
static	errval
st_close(dev_t dev, int flag, int fmt, struct proc *p,
        struct scsi_link *sc_link)
{
	u_int32_t unit, mode;
	struct scsi_data *st;

	unit = STUNIT(dev);
	mode = MODE(dev);
	st = sc_link->sd;

	if ((st->flags & (ST_WRITTEN | ST_FM_WRITTEN)) == ST_WRITTEN)
		st_write_filemarks(unit, 1, 0);

	/*
	 * Since the device has seen its last close, allow media removal. 
	 * Do this before we attempt an eject.
	 */
 	scsi_prevent(sc_link, PR_ALLOW, SCSI_SILENT);

	switch (mode & 0x3) {
	case 0:
	case 3:		/* for now */
		st_unmount(unit, NOEJECT);
		break;
	case 1:		/*leave mounted unless media seems to have been removed */
		if (!(sc_link->flags & SDEV_MEDIA_LOADED)) {
			st_unmount(unit, NOEJECT);
		}
		break;
	case 2:
		st_unmount(unit, EJECT);
		break;
	}

	sc_link->flags &= ~SDEV_OPEN;
	st->flags &= ~ST_OPEN;

	return (0);
}

/*
 * Start a new mount session.
 * Copy in all the default parameters from the selected device mode.
 * and try guess any that seem to be defaulted.
 */
static	errval
st_mount_tape(dev, flags)
	dev_t   dev;
	u_int32_t flags;
{
	u_int32_t unit, mode, dsty;
	struct scsi_data *st;
	struct scsi_link *sc_link;
	errval  errno = 0;

	unit = STUNIT(dev);
	mode = MODE(dev);
	dsty = DSTY(dev);
	sc_link = SCSI_LINK(&st_switch, unit);
	st = sc_link->sd;

	if (st->flags & ST_MOUNTED)
		return 0;

	SC_DEBUG(sc_link, SDEV_DB1, ("mounting\n "));
	st->flags |= ST_NEW_MOUNT;
	st->quirks = st->drive_quirks | st->modes[dsty].quirks;
	/*
	 * If the media is new, then make sure we give it a chance to
	 * to do a 'load' instruction. ( We assume it is new)
	 */
	if ( (errno = st_load(unit, LD_LOAD, 0)) ) {
		return (errno);
	}
	/*
	 * Throw another dummy instruction to catch
	 * 'Unit attention' errors. Some drives appear to give
	 * these after doing a Load instruction.
	 * (noteably some DAT drives)
	 */
	scsi_test_unit_ready(sc_link, SCSI_SILENT);

	/*
	 * Some devices can't tell you much until they have been
	 * asked to look at the media. This quirk does this.
	 */
	if (st->quirks & ST_Q_SNS_HLP) {
		if ( (errno = st_touch_tape(unit)) )
			return errno;
	}
	/*
	 * Load the physical device parameters
	 * loads: blkmin, blkmax
	 */
	if ( (errno = st_rd_blk_lim(unit, 0)) ) {
		return errno;
	}
	/*
	 * Load the media dependent parameters
	 * includes: media_blksiz,media_density,numblks
	 * As we have a tape in, it should be reflected here.
	 * If not you may need the "quirk" above.
	 */
	if ( (errno = st_mode_sense(unit, 0, NULL, 0, 0)) ) {
		return errno;
	}
	/*
	 * If we have gained a permanent density from somewhere,
	 * then use it in preference to the one supplied by
	 * default by the driver.
	 */
	if (st->modeflags[dsty] & (DENSITY_SET_BY_QUIRK | DENSITY_SET_BY_USER)) {
		st->density = st->modes[dsty].density;
	} else {
		st->density = st->media_density;
	}
	/*
	 * If we have gained a permanent blocksize
	 * then use it in preference to the one supplied by
	 * default by the driver.
	 */
	st->flags &= ~ST_FIXEDBLOCKS;
	if (st->modeflags[dsty] & (BLKSIZE_SET_BY_QUIRK | BLKSIZE_SET_BY_USER)) {
		st->blksiz = st->modes[dsty].blksiz;
		if (st->blksiz) {
			st->flags |= ST_FIXEDBLOCKS;
		}
	} else {
		if ( (errno = st_decide_mode(unit, FALSE)) ) {
			return errno;
		}
	}
	if ( (errno = st_mode_select(unit, 0, NULL, 0)) ) {
		printf("st%ld: Cannot set selected mode", unit);
		return errno;
	}
	st->flags &= ~ST_NEW_MOUNT;
	st->flags |= ST_MOUNTED;
	sc_link->flags |= SDEV_MEDIA_LOADED;

	return 0;
}

/*
 * End the present mount session.
 * Rewind, and optionally eject the tape.
 * Reset various flags to indicate that all new
 * operations require another mount operation
 */
void
st_unmount(int unit, boolean eject)
{
	struct scsi_link *sc_link = SCSI_LINK(&st_switch, unit);
	struct scsi_data *st = sc_link->sd;
	int32_t nmarks;

	if (!(st->flags & ST_MOUNTED))
		return;
	SC_DEBUG(sc_link, SDEV_DB1, ("unmounting\n"));
	st_chkeod(unit, FALSE, &nmarks, SCSI_SILENT);
	st_rewind(unit, FALSE, SCSI_SILENT);
	if (eject) {
		st_load(unit, LD_UNLOAD, SCSI_SILENT);
	}
	st->flags &= ~(ST_MOUNTED | ST_NEW_MOUNT);
	sc_link->flags &= ~SDEV_MEDIA_LOADED;
}

/*
 * Given all we know about the device, media, mode, 'quirks' and
 * initial operation, make a decision as to how we should be set
 * to run (regarding blocking and EOD marks)
 */
static	errval
st_decide_mode(unit, first_read)
	u_int32_t	unit;
	boolean	first_read;
{
	struct scsi_link *sc_link = SCSI_LINK(&st_switch, unit);
	struct scsi_data *st = sc_link->sd;

	SC_DEBUG(sc_link, SDEV_DB2, ("starting block mode decision\n"));

	/*
	 * If the user hasn't already specified fixed or variable-length
	 * blocks and the block size (zero if variable-length), we'll
	 * have to try to figure them out ourselves.
	 *
	 * Our first shot at a method is, "The quirks made me do it!"
	 */
	switch ((int)(st->quirks & (ST_Q_FORCE_FIXED_MODE | ST_Q_FORCE_VAR_MODE))) {
	case (ST_Q_FORCE_FIXED_MODE | ST_Q_FORCE_VAR_MODE):
		printf("st%ld: bad quirks\n", unit);
		return (EINVAL);
	case ST_Q_FORCE_FIXED_MODE:	/*specified fixed, but not what size */
		st->flags |= ST_FIXEDBLOCKS;
		if (st->blkmin && (st->blkmin == st->blkmax))
			st->blksiz = st->blkmin;
		else if (st->media_blksiz > 0)
			st->blksiz = st->media_blksiz;
		else
			st->blksiz = DEF_FIXED_BSIZE;
		SC_DEBUG(sc_link, SDEV_DB3, ("Quirks force fixed mode(%ld)\n",
			st->blksiz));
		goto done;
	case ST_Q_FORCE_VAR_MODE:
		st->flags &= ~ST_FIXEDBLOCKS;
		st->blksiz = 0;
		SC_DEBUG(sc_link, SDEV_DB3, ("Quirks force variable mode\n"));
		goto done;
	}
		/*
		 * If the drive can only handle fixed-length blocks and only at
		 * one size, perhaps we should just do that.
		 */
		if (st->blkmin && (st->blkmin == st->blkmax)) {
		st->flags |= ST_FIXEDBLOCKS;
		st->blksiz = st->blkmin;
		SC_DEBUG(sc_link, SDEV_DB3,
		    ("blkmin == blkmax of %ld\n", st->blkmin));
		goto done;
	}
	/*
	 * If the tape density mandates (or even suggests) use of fixed
	 * or variable-length blocks, comply.
	 */
	switch ((int)st->density) {
	case HALFINCH_800:
	case HALFINCH_1600:
	case HALFINCH_6250:
	case DDS:
	case QIC_525:
	case QIC_1320:
	case QIC_3080:
		st->flags &= ~ST_FIXEDBLOCKS;
		st->blksiz = 0;
		SC_DEBUG(sc_link, SDEV_DB3, ("density specified variable\n"));
		goto done;
	case QIC_11:
	case QIC_24:
	case QIC_120:
	case QIC_150:
		st->flags |= ST_FIXEDBLOCKS;
		if (st->media_blksiz > 0) {
			st->blksiz = st->media_blksiz;
		} else {
			st->blksiz = DEF_FIXED_BSIZE;
		}
		SC_DEBUG(sc_link, SDEV_DB3, ("density specified fixed\n"));
		goto done;
	}
	/*
	 * If we're about to read the tape, perhaps we should choose
	 * fixed or variable-length blocks and block size according to
	 * what the drive found on the tape.
	 */
	if (first_read
	    && (!(st->quirks & ST_Q_BLKSIZ)
		|| (st->media_blksiz == 0)
		|| (st->media_blksiz == DEF_FIXED_BSIZE)
		|| (st->media_blksiz == 1024))) {
		if (st->media_blksiz == 0) {
			st->flags &= ~ST_FIXEDBLOCKS;
		} else {
			st->flags |= ST_FIXEDBLOCKS;
		}
		st->blksiz = st->media_blksiz;
		SC_DEBUG(sc_link, SDEV_DB3,
		    ("Used media_blksiz of %ld\n", st->media_blksiz));
		goto done;
	}
	/*
	 * We're getting no hints from any direction.  Choose variable-
	 * length blocks arbitrarily.
	 */
	st->flags &= ~ST_FIXEDBLOCKS;
	st->blksiz = 0;
	SC_DEBUG(sc_link, SDEV_DB3, ("Give up and default to variable mode\n"));
done:

	/*
	 * Decide whether or not to write two file marks to signify end-
	 * of-data.  Make the decision as a function of density.  If
	 * the decision is not to use a second file mark, the SCSI BLANK
	 * CHECK condition code will be recognized as end-of-data when
	 * first read.
	 * (I think this should be a by-product of fixed/variable..julian)
	 */
	switch ((int)st->density) {
/*      case    8 mm:   What is the SCSI density code for 8 mm, anyway? */
	case QIC_11:
	case QIC_24:
	case QIC_120:
	case QIC_150:
	case QIC_525:
	case QIC_1320:
	case QIC_3080:
		st->flags &= ~ST_2FM_AT_EOD;
		break;
	default:
		st->flags |= ST_2FM_AT_EOD;
	}
	return 0;
}

/*
 * Actually translate the requested transfer into
 * one the physical driver can understand
 * The transfer is described by a buf and will include
 * only one physical transfer.
 */
static	void
st_strategy(struct buf *bp, struct scsi_link *sc_link)
{
	u_int32_t unit;
	u_int32_t opri;
	struct scsi_data *st;
	int	len;

	unit = STUNIT((bp->b_dev));
	st = sc_link->sd;
	/*
	 * If it's a null transfer, return immediatly
	 */
	if ((len = bp->b_bcount) == 0) {
		goto done;
	}
	/*
	 * Check the adapter can do it
	 */
	scsi_minphys(bp,&st_switch);
	/*
	 * Odd sized request on fixed drives are verboten
	 */
	if (st->flags & ST_FIXEDBLOCKS) {
		if (bp->b_bcount % st->blksiz) {
			printf("st%ld: bad request, must be multiple of %ld\n",
			    unit, st->blksiz);
			bp->b_error = EIO;
			goto bad;
		}
	}
	/*
	 * as are out-of-range requests on variable drives.
	 * (or if we got chopped by minphys)
	 */
	else {
		if ((bp->b_bcount < st->blkmin || bp->b_bcount > st->blkmax)) {
			printf("st%ld: bad request, must be between %ld and %ld\n",
		    		unit, st->blkmin, st->blkmax);
			bp->b_error = EIO;
			goto bad;
		}
		if (len != bp->b_bcount) {
			printf("st%ld: bad request, must be less than %ld bytes\n",
				unit, bp->b_bcount + 1);
			bp->b_error = EIO;
			goto bad;
		}
	}
	opri = splbio();

	/*
	 * Use a bounce buffer if necessary
	 */
#ifdef BOUNCE_BUFFERS
	if (sc_link->flags & SDEV_BOUNCE)
		vm_bounce_alloc(bp);
#endif

	/*
	 * Place it in the queue of activities for this tape
	 * at the end (a bit silly because we only have on user..
	 * (but it could fork() ))
	 */
	TAILQ_INSERT_TAIL(&st->buf_queue, bp, b_act);

	/*
	 * Tell the device to get going on the transfer if it's
	 * not doing anything, otherwise just wait for completion
	 * (All a bit silly if we're only allowing 1 open but..)
	 */
	ststart(unit, 0);

	splx(opri);
	return;
bad:
	bp->b_flags |= B_ERROR;
done:
	/*
	 * Correctly set the buf to indicate a completed xfer
	 */
	biodone(bp);
	return;
}

/*
 * ststart looks to see if there is a buf waiting for the device
 * and that the device is not already busy. If both are true,
 * It dequeues the buf and creates a scsi command to perform the
 * transfer required. The transfer request will call scsi_done
 * on completion, which will in turn call this routine again
 * so that the next queued transfer is performed.
 * The bufs are queued by the strategy routine (ststrategy)
 *
 * This routine is also called after other non-queued requests
 * have been made of the scsi driver, to ensure that the queue
 * continues to be drained.
 * ststart() is called at splbio
 */
static	void
ststart(unit, flags)
	u_int32_t	unit;
	u_int32_t flags;
{
	struct scsi_link *sc_link = SCSI_LINK(&st_switch, unit);
	struct scsi_data *st = sc_link->sd;
	register struct buf *bp = 0;
	struct scsi_rw_tape cmd;

	SC_DEBUG(sc_link, SDEV_DB2, ("ststart "));
	/*
	 * See if there is a buf to do and we are not already
	 * doing one
	 */
	while (sc_link->opennings != 0) {

		/* if a special awaits, let it proceed first */
		if (sc_link->flags & SDEV_WAITING) {
			sc_link->flags &= ~SDEV_WAITING;
			wakeup((caddr_t)sc_link);
			return;
		}

		bp = st->buf_queue.tqh_first;
		if (bp == NULL) {	/* yes, an assign */
			return;
		}
		TAILQ_REMOVE( &st->buf_queue, bp, b_act);

		/*
		 * if the device has been unmounted by the user
		 * then throw away all requests until done
		 */
		if ((!(st->flags & ST_MOUNTED))
		    || (!(sc_link->flags & SDEV_MEDIA_LOADED))) {
			/* make sure that one implies the other.. */
			sc_link->flags &= ~SDEV_MEDIA_LOADED;
			goto badnews;
		}
		/*
		 * only FIXEDBLOCK devices have pending operations
		 */
		if (st->flags & ST_FIXEDBLOCKS) {
			/*
			 * If we are at a filemark but have not reported it yet
			 * then we should report it now
			 */
			if (st->flags & ST_AT_FILEMARK) {
				if ((bp->b_flags & B_READ) == B_WRITE) {
					/*
					 * Handling of ST_AT_FILEMARK in
					 * st_space will fill in the right file
					 * mark count.
					 * Back up over filemark
					 */
					if (st_space(unit, 0, SP_FILEMARKS, 0) !=
					    ESUCCESS)
						goto badnews;
				} else {
					bp->b_resid = bp->b_bcount;
					bp->b_error = 0;
					bp->b_flags &= ~B_ERROR;
					st->flags &= ~ST_AT_FILEMARK;
					biodone(bp);
					continue;	/* seek more work */
				}
			}
			/*
			 * If we are at EIO (e.g. EOM) but have not reported it
			 * yet then we should report it now
			 */
			if (st->flags & ST_EIO_PENDING) {
				bp->b_resid = bp->b_bcount;
				bp->b_error = EIO;
				bp->b_flags |= B_ERROR;
				st->flags &= ~ST_EIO_PENDING;
				biodone(bp);
				continue;	/* seek more work */
			}
		}
		/*
		 *  Fill out the scsi command
		 */
		bzero(&cmd, sizeof(cmd));
		if ((bp->b_flags & B_READ) == B_WRITE) {
			cmd.op_code = WRITE_COMMAND_TAPE;
			st->flags &= ~ST_FM_WRITTEN;
			st->flags |= ST_WRITTEN;
			flags |= SCSI_DATA_OUT;
		} else {
			cmd.op_code = READ_COMMAND_TAPE;
			flags |= SCSI_DATA_IN;
		}
		/*
		 * Handle "fixed-block-mode" tape drives by using the
		 * block count instead of the length.
		 */
		if (st->flags & ST_FIXEDBLOCKS) {
			cmd.byte2 |= SRWT_FIXED;
			scsi_uto3b(bp->b_bcount / st->blksiz, cmd.len);
		} else {
			scsi_uto3b(bp->b_bcount, cmd.len);
		}
		/*
		 * go ask the adapter to do all this for us
		 */
		if (scsi_scsi_cmd(sc_link,
			(struct scsi_generic *) &cmd,
			sizeof(cmd),
			(u_char *) bp->b_un.b_addr,
			bp->b_bcount,
			0,	/* can't retry a read on a tape really */
			100000,
			bp,
			flags) == SUCCESSFULLY_QUEUED) {
		} else {
badnews:
			printf("st%ld: oops not queued\n", unit);
			bp->b_flags |= B_ERROR;
			bp->b_error = EIO;
			biodone(bp);
		}
	} /* go back and see if we can cram more work in.. */
}

/*
 * Perform special action on behalf of the user;
 * knows about the internals of this device
 */
static	errval
st_ioctl(dev_t dev, int cmd, caddr_t arg, int	flag,
struct proc *p, struct scsi_link *sc_link)
{
	errval  errcode = 0;
	u_int32_t unit;
	u_int32_t number, flags, dsty;
	struct scsi_data *st;
	u_int32_t hold_blksiz;
	u_int32_t hold_density;
	int32_t   nmarks;
	struct mtop *mt = (struct mtop *) arg;

	/*
	 * Find the device that the user is talking about
	 */
	flags = 0;		/* give error messages, act on errors etc. */
	unit = STUNIT(dev);
	dsty = DSTY(dev);
	st = sc_link->sd;
	hold_blksiz = st->blksiz;
	hold_density = st->density;

	switch (cmd) {

	case MTIOCGET:
		{
			struct mtget *g = (struct mtget *) arg;

			SC_DEBUG(sc_link, SDEV_DB1, ("[ioctl: get status]\n"));
			bzero(g, sizeof(struct mtget));
			g->mt_type = 0x7;	/* Ultrix compat *//*? */
			g->mt_density = st->density;
			g->mt_blksiz = st->blksiz;
			g->mt_comp = st->comp;
			g->mt_density0 = st->modes[0].density;
			g->mt_density1 = st->modes[1].density;
			g->mt_density2 = st->modes[2].density;
			g->mt_density3 = st->modes[3].density;
			g->mt_blksiz0 = st->modes[0].blksiz;
			g->mt_blksiz1 = st->modes[1].blksiz;
			g->mt_blksiz2 = st->modes[2].blksiz;
			g->mt_blksiz3 = st->modes[3].blksiz;
			g->mt_comp0 = 0;
			g->mt_comp1 = 0;
			g->mt_comp2 = 0;
			g->mt_comp3 = 0;
			break;
		}
	case MTIOCTOP:
		{

			SC_DEBUG(sc_link, SDEV_DB1, ("[ioctl: op=0x%x count=0x%lx]\n",
				mt->mt_op, mt->mt_count));

			/* compat: in U*x it is a short */
			number = mt->mt_count;
			switch ((short) (mt->mt_op)) {
			case MTWEOF:	/* write an end-of-file record */
				errcode = st_write_filemarks(unit, number, flags);
				break;
			case MTBSF:	/* backward space file */
				number = -number;
			case MTFSF:	/* forward space file */
				errcode = st_chkeod(unit, FALSE, &nmarks, flags);
				if (errcode == ESUCCESS)
					errcode = st_space(unit, number - nmarks,
					    SP_FILEMARKS, flags);
				break;
			case MTBSR:	/* backward space record */
				number = -number;
			case MTFSR:	/* forward space record */
				errcode = st_chkeod(unit, TRUE, &nmarks, flags);
				if (errcode == ESUCCESS)
					errcode = st_space(unit, number, SP_BLKS, flags);
				break;
			case MTEOD:	/* space to end of recorded medium */
				errcode = st_chkeod(unit, FALSE, &nmarks, flags);
				if (errcode == ESUCCESS)
					errcode = st_space(unit, 0, SP_EOM,
							   flags);
				break;
			case MTREW:	/* rewind */
				errcode = st_rewind(unit, FALSE, flags);
				break;
			case MTERASE:	/* erase */
				errcode = st_erase(unit, FALSE, flags);
				break;
			case MTRETENS:	/* re-tension tape */
				errcode = st_load(unit, LD_LOAD|LD_RETEN,
						  flags);
				break;
			case MTOFFL:	/* rewind and put the drive offline */
				/*
				 * Be sure to allow media removal before
				 * attempting the eject.
				 */
				scsi_prevent(sc_link, PR_ALLOW, SCSI_SILENT);
				st_unmount(unit, EJECT);
				break;
			case MTNOP:	/* no operation, sets status only */
			case MTCACHE:	/* enable controller cache */
			case MTNOCACHE:	/* disable controller cache */
				break;
			case MTSETBSIZ:	/* Set block size for device */
#ifdef	NOTYET
				if (!(st->flags & ST_NEW_MOUNT)) {
					uprintf("re-mount tape before changing blocksize");
					errcode = EINVAL;
					break;
				}
#endif
				if (number == 0) {
					st->flags &= ~ST_FIXEDBLOCKS;
				} else {
					if ((st->blkmin || st->blkmax)	/* they exist */
					    &&((number < st->blkmin
						    || number > st->blkmax))) {
						errcode = EINVAL;
						break;
					}
					st->flags |= ST_FIXEDBLOCKS;
				}
				st->blksiz = number;
				st->flags |= ST_BLOCK_SET;	/*XXX */
				goto try_new_value;

			case MTSETDNSTY:	/* Set density for device and mode */
				if (number > (u_int8_t)0xff) {
					/* Guard against overflows */
					errcode = EINVAL;
				} else {
					st->density = number;
					goto try_new_value;
				}
				break;
			case MTCOMP:	/* enable default compression */
				errcode = st_comp(unit,number);
				break;
			default:
				errcode = EINVAL;
			}
			break;
		}
	case MTIOCIEOT:
	case MTIOCEEOT:
		break;
	default:
		if(IS_CTLMODE(dev))
			errcode = scsi_do_ioctl(dev, cmd, arg, flag, p, sc_link);
		else
			errcode = ENOTTY;
		break;
	}
	return errcode;
/*-----------------------------*/
try_new_value:
	/*
	 * Check that the mode being asked for is aggreeable to the
	 * drive. If not, put it back the way it was.
	 */
	if ( (errcode = st_mode_select(unit, 0, NULL, 0)) ) {	/* put back as it was */
		printf("st%ld: Cannot set selected mode", unit);
		st->density = hold_density;
		st->blksiz = hold_blksiz;
		if (st->blksiz) {
			st->flags |= ST_FIXEDBLOCKS;
		} else {
			st->flags &= ~ST_FIXEDBLOCKS;
		}
		return (errcode);
	}
	/*
	 * As the drive liked it, if we are setting a new default,
	 * set it into the structures as such.
	 *
	 * The means for deciding this are not finalised yet
	 */
	if (IS_CTLMODE(dev)) {
		/* special mode */
		/* XXX */
		switch ((short) (mt->mt_op)) {
		case MTSETBSIZ:
			st->modes[dsty].blksiz = st->blksiz;
			st->modeflags[dsty] |= BLKSIZE_SET_BY_USER;
			break;
		case MTSETDNSTY:
			st->modes[dsty].density = st->density;
			st->modeflags[dsty] |= DENSITY_SET_BY_USER;
			break;
		}
	}
	return 0;
}

/*
 * Do a synchronous read.
 */
static errval
st_read(unit, buf, size, flags)
	u_int32_t unit, size, flags;
	char   *buf;
{
	struct scsi_link *sc_link = SCSI_LINK(&st_switch, unit);
	struct scsi_data *st = sc_link->sd;
	struct scsi_rw_tape scsi_cmd;

	/*
	 * If it's a null transfer, return immediatly
	 */
	if (size == 0) {
		return (ESUCCESS);
	}
	bzero(&scsi_cmd, sizeof(scsi_cmd));
	scsi_cmd.op_code = READ_COMMAND_TAPE;
	if (st->flags & ST_FIXEDBLOCKS) {
		scsi_cmd.byte2 |= SRWT_FIXED;
		scsi_uto3b(size / (st->blksiz ? st->blksiz : DEF_FIXED_BSIZE),
		    scsi_cmd.len);
	} else {
		scsi_uto3b(size, scsi_cmd.len);
	}
	return (scsi_scsi_cmd(sc_link,
		(struct scsi_generic *) &scsi_cmd,
		sizeof(scsi_cmd),
		(u_char *) buf,
		size,
		0,		/* not on io commands */
		100000,
		NULL,
		flags | SCSI_DATA_IN));
}
#ifdef	__STDC__
#define b2tol(a)	(((unsigned)(a##_1) << 8) + (unsigned)a##_0 )
#else
#define b2tol(a)	(((unsigned)(a/**/_1) << 8) + (unsigned)a/**/_0 )
#endif

/*
 * Ask the drive what it's min and max blk sizes are.
 */
static	errval
st_rd_blk_lim(unit, flags)
	u_int32_t unit, flags;
{
	struct scsi_link *sc_link = SCSI_LINK(&st_switch, unit);
	struct scsi_data *st = sc_link->sd;
	struct scsi_blk_limits scsi_cmd;
	struct scsi_blk_limits_data scsi_blkl;
	errval  errno;

	/*
	 * First check if we have it all loaded
	 */
	if ((sc_link->flags & SDEV_MEDIA_LOADED))
		return 0;

	/*
	 * do a 'Read Block Limits'
	 */
	bzero(&scsi_cmd, sizeof(scsi_cmd));
	scsi_cmd.op_code = READ_BLK_LIMITS;

	/*
	 * do the command, update the global values
	 */
	if ( (errno = scsi_scsi_cmd(sc_link,
		(struct scsi_generic *) &scsi_cmd,
		sizeof(scsi_cmd),
		(u_char *) & scsi_blkl,
		sizeof(scsi_blkl),
		ST_RETRIES,
		5000,
		NULL,
		flags | SCSI_DATA_IN)) ) {
		return errno;
	}
	st->blkmin = b2tol(scsi_blkl.min_length);
	st->blkmax = scsi_3btou(&scsi_blkl.max_length_2);

	SC_DEBUG(sc_link, SDEV_DB3,
	    ("(%ld <= blksiz <= %ld)\n", st->blkmin, st->blkmax));
	return 0;
}

/*
 * Get the scsi driver to send a full inquiry to the
 * device and use the results to fill out the global
 * parameter structure.
 *
 * called from:
 * attach
 * open
 * ioctl (to reset original blksize)
 */
static errval
st_mode_sense(unit, flags, page, pagelen, pagecode)
	u_int32_t unit, flags;
	struct tape_pages *page;
	u_int32_t pagelen,pagecode;
{
	u_int32_t dat_len;
	errval  errno;
	struct scsi_mode_sense scsi_cmd;
	struct {
		struct scsi_mode_header header;
		struct blk_desc blk_desc;
		struct tape_pages page;
	} dat;

	/* Tandberg tape drives returns page 00
	 * with the sense data, whether or not
	 * you want it( ie the don't like you
	 * saying you want anything less!!!!!
	 * They also expect page 00
	 * back when you issue a mode select
	 */
	struct scsi_link *sc_link = SCSI_LINK(&st_switch, unit);
	struct scsi_data *st = sc_link->sd;

	st->flags &= ~ST_SENSE_READ;
	/*
	 * Check if we need to use a default page..
	 */
	if ((st->quirks & ST_Q_NEEDS_PAGE_0) && (!page)) {
		pagelen = PAGE_0_SENSE_DATA_SIZE;
		page = (struct tape_pages *) st->saved_page0;
		pagecode = 0;
	}
	/*
	 * Now work out the total dat size etc.
	 */
	dat_len = sizeof(struct scsi_mode_header)
		+ sizeof(struct blk_desc)
		+ (page ? pagelen : 0);
	/*
	 * Set up a mode sense
	 */
	bzero(&scsi_cmd, sizeof(scsi_cmd));
	scsi_cmd.op_code = MODE_SENSE;
	scsi_cmd.page = (u_char) pagecode;
	scsi_cmd.length = dat_len;

	/*
	 * do the command,
	 * use the results to set blksiz, numblks and density
	 * or if we need it as a template for the mode select
	 * store it away.
	 */
	if ( (errno = scsi_scsi_cmd(sc_link,
		(struct scsi_generic *) &scsi_cmd,
		sizeof(scsi_cmd),
		(u_char *) &dat,
		dat_len,
		ST_RETRIES,
		5000,
		NULL,
		flags | SCSI_DATA_IN)) ) {
		return errno;
	}
	st->numblks = scsi_3btou(dat.blk_desc.nblocks);
	st->media_blksiz = scsi_3btou(dat.blk_desc.blklen);
	st->media_density = dat.blk_desc.density;
	if (dat.header.dev_spec & SMH_DSP_WRITE_PROT) {
		st->flags |= ST_READONLY;
	}
	SC_DEBUG(sc_link, SDEV_DB3,
	    ("density code 0x%lx, %ld-byte blocks, write-%s, ",
		st->media_density, st->media_blksiz,
		st->flags & ST_READONLY ? "protected" : "enabled"));
	SC_DEBUG(sc_link, SDEV_DB3, ("%sbuffered\n",
		((dat.header.dev_spec & SMH_DSP_BUFF_MODE) ? "" : "un")));
	if (page) {
		bcopy(&dat.page, page, pagelen);
	}
	st->flags |= ST_SENSE_READ;
	return 0;
}

/*
 * Send a filled out parameter structure to the drive to
 * set it into the desire modes etc.
 */
static	errval
st_mode_select(unit, flags, page, pagelen)
	u_int32_t unit, flags;
	struct tape_pages *page;
	u_int32_t pagelen;
{
	u_int32_t dat_len;
	struct scsi_mode_select scsi_cmd;
	struct {
		struct scsi_mode_header header;
		struct blk_desc blk_desc;
		struct tape_pages page;
	} dat;
	struct scsi_link *sc_link = SCSI_LINK(&st_switch, unit);
	struct scsi_data *st = sc_link->sd;

	/*
	 * Check if we need to use a default page..
	 * Gee, hope we saved one before now........
	 */
	if ((st->quirks & ST_Q_NEEDS_PAGE_0) && (!page)) {
		pagelen = PAGE_0_SENSE_DATA_SIZE;
		page = (struct tape_pages *) st->saved_page0;
	}
	/*
	 * Now work out the total dat size etc.
	 */
	dat_len = sizeof(struct scsi_mode_header)
		+ sizeof(struct blk_desc)
		+ (page ? pagelen : 0);
	/*
	 * Set up for a mode select
	 */
	bzero(&dat, dat_len);
	bzero(&scsi_cmd, sizeof(scsi_cmd));
	scsi_cmd.op_code = MODE_SELECT;
	scsi_cmd.length = dat_len;
	dat.header.blk_desc_len = sizeof(struct blk_desc);
	dat.header.dev_spec |= SMH_DSP_BUFF_MODE_ON;
	dat.blk_desc.density = st->density;
	if (st->flags & ST_FIXEDBLOCKS) {
		scsi_uto3b(st->blksiz, dat.blk_desc.blklen);
	}
	if (page) {
		bcopy(page, &dat.page, pagelen);
		/* the Tandberg tapes need the block size to */
		/* be set on each mode sense/select. */
	}
	/*
	 * do the command
	 */
	return (scsi_scsi_cmd(sc_link,
		(struct scsi_generic *) &scsi_cmd,
		sizeof(scsi_cmd),
		(u_char *) &dat,
		dat_len,
		ST_RETRIES,
		5000,
		NULL,
		flags | SCSI_DATA_OUT));
}

static int noisy_st = 0;
/***************************************************************\
* Set the compression mode of the drive to on (1) or off (0)	*
	still doesn't work! grrr!
\***************************************************************/
static	errval
st_comp(unit,mode)
u_int32_t unit,mode;
{
	struct tape_pages       page;
	int	pagesize;
	int retval;
	struct scsi_link *sc_link = SCSI_LINK(&st_switch, unit);
	struct scsi_data *st = sc_link->sd;

	bzero(&page, sizeof(page));
	pagesize = sizeof(page.pages.configuration) + PAGE_HEADERLEN;

	if ( (retval = st_mode_sense(unit, 0,
			&page, pagesize, ST_PAGE_CONFIGURATION)) )
	{
		printf("sense returned an error of %d\n",retval);
		return retval;
	}
	if ( noisy_st)
		printf("drive reports value of %d, setting %ld\n",
			page.pages.configuration.data_compress_alg,mode);

	page.pg_code &= ST_P_CODE;
	page.pg_length = sizeof(page.pages.configuration);

	switch(mode)
	{
	case 0:
		page.pages.configuration.data_compress_alg = 0;
		break;
	case 1:
		page.pages.configuration.data_compress_alg = 1;
		break;
	default:
		printf("st%ld: bad value for compression mode\n",unit);
		return EINVAL;
	}
	if ( (retval = st_mode_select(unit, 0, &page, pagesize)) )
	{
		printf("select returned an error of %d\n",retval);
		return retval;
	}
	st->comp = mode;
	return 0;
}
/*
 * skip N blocks/filemarks/seq filemarks/eom
 */
static	errval
st_space(unit, number, what, flags)
	u_int32_t unit, what, flags;
	int32_t   number;
{
	errval  error;
	struct scsi_space scsi_cmd;
	struct scsi_link *sc_link = SCSI_LINK(&st_switch, unit);
	struct scsi_data *st = sc_link->sd;

	switch ((int)what) {
	case SP_BLKS:
		if (st->flags & ST_PER_ACTION) {
			if (number > 0) {
				st->flags &= ~ST_PER_ACTION;
				return (EIO);
			} else if (number < 0) {
				if (st->flags & ST_AT_FILEMARK) {
					/*
					 * Handling of ST_AT_FILEMARK
					 * in st_space will fill in the
					 * right file mark count.
					 */
					error = st_space(unit, 0, SP_FILEMARKS,
					    flags);
					if (error)
						return (error);
				}
				if (st->flags & ST_BLANK_READ) {
					st->flags &= ~ST_BLANK_READ;
					return (EIO);
				}
				st->flags &= ~ST_EIO_PENDING;
			}
		}
		break;
	case SP_FILEMARKS:
		if (st->flags & ST_EIO_PENDING) {
			if (number > 0) {	/* pretend we just discover the error */
				st->flags &= ~ST_EIO_PENDING;
				return (EIO);
			} else if (number < 0) {	/* back away from the error */
				st->flags &= ~ST_EIO_PENDING;
			}
		}
		if (st->flags & ST_AT_FILEMARK) {
			st->flags &= ~ST_AT_FILEMARK;
			number--;
		}
		if ((st->flags & ST_BLANK_READ) && (number < 0)) {	/* back away from unwritten tape */
			st->flags &= ~ST_BLANK_READ;
			number++;	/* dubious */
		}
		break;
	case	SP_EOM:
		if (st->flags & ST_EIO_PENDING)
		{
			/* we are already at EOM */
			st->flags &= ~ST_EIO_PENDING;
			return(ESUCCESS);
		}
		number = 1;	/* we have only one end-of-medium */
		break;
	}
	if (number == 0) {
		return (ESUCCESS);
	}
	bzero(&scsi_cmd, sizeof(scsi_cmd));
	scsi_cmd.op_code = SPACE;
	scsi_cmd.byte2 = what & SS_CODE;
	scsi_uto3b(number, scsi_cmd.number);
	return (scsi_scsi_cmd(sc_link,
		(struct scsi_generic *) &scsi_cmd,
		sizeof(scsi_cmd),
		0,
		0,
		0,		/* no retries please , just fail */
		3600000,	/* wait up to an hour for completion */
		NULL,
		flags));
}

/*
 * write N filemarks
 */
static	errval
st_write_filemarks(unit, number, flags)
	u_int32_t unit, flags;
	int32_t   number;
{
	struct scsi_write_filemarks scsi_cmd;
	struct scsi_link *sc_link = SCSI_LINK(&st_switch, unit);
	struct scsi_data *st = sc_link->sd;

	/*
	 * It's hard to write a negative number of file marks.
	 * Don't try.
	 */
	if (number < 0) {
		return EINVAL;
	}
	switch ((int)number) {
	case 0:		/* really a command to sync the drive's buffers */
		break;
	case 1:
		if (st->flags & ST_FM_WRITTEN) {	/* already have one down */
			st->flags &= ~ST_WRITTEN;
		} else {
			st->flags |= ST_FM_WRITTEN;
		}
		st->flags &= ~ST_PER_ACTION;
		break;
	default:
		st->flags &= ~(ST_PER_ACTION | ST_WRITTEN);
	}
	bzero(&scsi_cmd, sizeof(scsi_cmd));
	scsi_cmd.op_code = WRITE_FILEMARKS;
	scsi_uto3b(number, scsi_cmd.number);
	return scsi_scsi_cmd(sc_link,
		(struct scsi_generic *) &scsi_cmd,
		sizeof(scsi_cmd),
		0,
		0,
		0,		/* no retries, just fail */
		100000,		/* 10 secs.. (may need to repos head ) */
		NULL,
		flags);
}

/*
 * Make sure the right number of file marks is on tape if the
 * tape has been written.  If the position argument is true,
 * leave the tape positioned where it was originally.
 *
 * nmarks returns the number of marks to skip (or, if position
 * true, which were skipped) to get back original position.
 */
static	int32_t
st_chkeod(unit, position, nmarks, flags)
	u_int32_t unit;
	boolean position;
	int32_t *nmarks;
	u_int32_t flags;
{
	errval  error;
	struct scsi_data *st = SCSI_DATA(&st_switch, unit);

	switch ((int)(st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD))) {
	default:
		*nmarks = 0;
		return (ESUCCESS);
	case ST_WRITTEN:
	case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD:
		*nmarks = 1;
		break;
	case ST_WRITTEN | ST_2FM_AT_EOD:
		*nmarks = 2;
	}
	error = st_write_filemarks(unit, *nmarks, flags);
	if (position && (error == ESUCCESS))
		error = st_space(unit, -*nmarks, SP_FILEMARKS, flags);
	return (error);
}

/*
 * load/unload (with retension if true)
 */
static	errval
st_load(unit, type, flags)
	u_int32_t unit, type, flags;
{
	struct scsi_load scsi_cmd;
	struct scsi_link *sc_link = SCSI_LINK(&st_switch, unit);
	struct scsi_data *st = sc_link->sd;

	bzero(&scsi_cmd, sizeof(scsi_cmd));
	if (type != LD_LOAD) {
		errval  error;
		int32_t nmarks;

		error = st_chkeod(unit, FALSE, &nmarks, flags);
		if (error != ESUCCESS)
			return (error);
		sc_link->flags &= ~SDEV_MEDIA_LOADED;
	}
	if (st->quirks & ST_Q_IGNORE_LOADS)
		return (0);
	scsi_cmd.op_code = LOAD_UNLOAD;
	scsi_cmd.how |= type;
	return (scsi_scsi_cmd(sc_link,
		(struct scsi_generic *) &scsi_cmd,
		sizeof(scsi_cmd),
		0,
		0,
		ST_RETRIES,
		900000,		/* 15 min */
		NULL,
		flags));
}

/*
 *  Rewind the device
 */
static	errval
st_rewind(unit, immed, flags)
	u_int32_t unit, flags;
	boolean immed;
{
	struct scsi_rewind scsi_cmd;
	struct scsi_link *sc_link = SCSI_LINK(&st_switch, unit);
	struct scsi_data *st = sc_link->sd;
	errval  error;
	int32_t nmarks;

	error = st_chkeod(unit, FALSE, &nmarks, flags);
	if (error != ESUCCESS)
		return (error);
	st->flags &= ~ST_PER_ACTION;
	bzero(&scsi_cmd, sizeof(scsi_cmd));
	scsi_cmd.op_code = REWIND;
	scsi_cmd.byte2 = immed ? SR_IMMED : 0;
	return (scsi_scsi_cmd(sc_link,
		(struct scsi_generic *) &scsi_cmd,
		sizeof(scsi_cmd),
		0,
		0,
		ST_RETRIES,
		immed ? 5000 : 300000,	/* 5 sec or 5 min */
		NULL,
		flags));
}

/*
**  Erase the device
*/
static	errval
st_erase(unit, immed, flags)
	u_int32_t unit, flags;
	boolean immed;
{
	struct scsi_erase scsi_cmd;
	struct scsi_link *sc_link = SCSI_LINK(&st_switch, unit);
	struct scsi_data *st = sc_link->sd;
	errval  error;
	int32_t nmarks;

	error = st_chkeod(unit, FALSE, &nmarks, flags);
	if (error != ESUCCESS)
		return (error);
	/*
	**      Archive Viper 2525 technical manual 5.7 (ERASE 19h):
	**	tape has to be positioned to BOT first before erase command
	**	is issued or command is rejected. So we rewind the tape first
	**	and exit with an error, if the tape can't be rewinded.
	*/
	error = st_rewind(unit, FALSE, SCSI_SILENT);
	if (error != ESUCCESS)
		return (error);
	st->flags &= ~ST_PER_ACTION;
	bzero(&scsi_cmd, sizeof(scsi_cmd));
	scsi_cmd.op_code = ERASE;
	scsi_cmd.byte2 = SE_LONG;		/* LONG_ERASE */
	scsi_cmd.byte2 += immed ? SE_IMMED : 0; /* immed bit is here the 2nd! */
	return (scsi_scsi_cmd(sc_link,
		(struct scsi_generic *) &scsi_cmd,
		sizeof(scsi_cmd),
		0,
		0,
		ST_RETRIES,
		immed ? 5000 : (200 * 60 * 1000),	/* 5 sec or 200 min */
		NULL,
		flags));
}

/*
 * Look at the returned sense and act on the error and detirmine
 * The unix error number to pass back... (0 = report no error)
 *                            (SCSIRET_CONTINUE = continue processing)
 */
static	errval
st_interpret_sense(xs)
	struct scsi_xfer *xs;
{
	struct scsi_link *sc_link = xs->sc_link;
	struct scsi_sense_data *sense = &(xs->sense);
	boolean silent = xs->flags & SCSI_SILENT;
	u_int32_t unit = sc_link->dev_unit;
	struct scsi_data *st = SCSI_DATA(&st_switch, unit);
	u_int32_t key;
	int32_t   info;

	/*
	 * Get the sense fields and work out what code
	 */
	if (sense->error_code & SSD_ERRCODE_VALID) {
		info = ntohl(*((int32_t *) sense->ext.extended.info));
	} else {
		if (st->flags & ST_FIXEDBLOCKS) {
			info = xs->datalen / st->blksiz;
		} else {
			info = xs->datalen;
		}
	}

	key = sense->ext.extended.flags & SSD_KEY;

	if (((sense->error_code & SSD_ERRCODE) != 0x70)
	  || (key == 0x7)) /* Media Write Protected */ {
		return SCSIRET_CONTINUE;/* let the generic code handle it */
	}
	if(sense->ext.extended.flags & (SSD_EOM|SSD_FILEMARK|SSD_ILI)) {
		if (st->flags & ST_FIXEDBLOCKS) {
			xs->resid = info * st->blksiz;
			xs->flags |= SCSI_RESID_VALID;
			if (sense->ext.extended.flags & SSD_EOM) {
				st->flags |= ST_EIO_PENDING;
			}
			if (sense->ext.extended.flags & SSD_FILEMARK) {
				st->flags |= ST_AT_FILEMARK;
			}
			if (sense->ext.extended.flags & SSD_ILI) {
				st->flags |= ST_EIO_PENDING;
				if (sense->error_code & SSD_ERRCODE_VALID &&
			    	!silent)
					printf("st%ld: block wrong size"
				    	", %ld blocks residual\n", unit
				    	,info);
				/*XXX*/ /* is this how it works ? */
				/* check def of ILI for fixed blk tapes */

				/*
			 	 * This quirk code helps the drive read
			 	 * the first tape block, regardless of
			 	 * format.  That is required for these
			 	 * drives to return proper MODE SENSE
			 	 * information.
			 	 */
				if ((st->quirks & ST_Q_SNS_HLP) &&
				    !(st->flags & ST_SENSE_READ)) {
					st->blksiz -= 512;
				}
			}
			/*
			 * If no data was tranfered, do it immediatly
			 */
			if (xs->resid >= xs->datalen) {
				xs->flags &= ~SCSI_RESID_VALID;
				if (st->flags & ST_AT_FILEMARK) {
					xs->flags |= SCSI_EOF;
					st->flags &= ~ST_AT_FILEMARK;
					return 0;
				}
				if (st->flags & ST_EIO_PENDING) {
					st->flags &= ~ST_EIO_PENDING;
					return EIO;
				}
			}
			return 0;
		} else {		/* must be variable mode */
			xs->resid = xs->datalen;	/* to be sure */
			if (sense->ext.extended.flags & SSD_EOM) {
				return (EIO);
			}
			if (sense->ext.extended.flags & SSD_FILEMARK) {
				xs->flags |= SCSI_EOF;
			}
			if (sense->ext.extended.flags & SSD_ILI) {
				if (info < 0) {
					/*
					 * the record was bigger than the read
					 */
					if (!silent)
						printf("st%ld: %ld-byte record "
					    	"too big\n", unit,
					    	xs->datalen - info);
					return (EIO);
				}
				xs->resid = info;
				xs->flags |= SCSI_RESID_VALID;
			}
		}
		return 0;
	}

	if (key == 0x8) {
		xs->flags |= SCSI_EOF; /* some drives need this */
		/*
		 * This quirk code helps the drive read the
		 * first tape block, regardless of format.  That
		 * is required for these drives to return proper
		 * MODE SENSE information.
		 */
		if ((st->quirks & ST_Q_SNS_HLP) &&
		    !(st->flags & ST_SENSE_READ)) {
			/* still starting */
			st->blksiz -= 512;
		} else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) {
			st->flags |= ST_BLANK_READ;
			xs->flags |= SCSI_EOF;
			return (ESUCCESS);
		}
	}
	return SCSIRET_CONTINUE;  /* Use the the generic handler */
}

/*
 * The quirk here is that the drive returns some value to st_mode_sense
 * incorrectly until the tape has actually passed by the head.
 *
 * The method is to set the drive to large fixed-block state (user-specified
 * density and 1024-byte blocks), then read and rewind to get it to sense the
 * tape.  If that doesn't work, try 512-byte fixed blocks.  If that doesn't
 * work, as a last resort, try variable- length blocks.  The result will be
 * the ability to do an accurate st_mode_sense.
 *
 * We know we can do a rewind because we just did a load, which implies rewind.
 * Rewind seems preferable to space backward if we have a virgin tape.
 *
 * The rest of the code for this quirk is in ILI processing and BLANK CHECK
 * error processing, both part of st_interpret_sense.
 */
static	errval
st_touch_tape(unit)
	u_int32_t	unit;
{
	struct scsi_data *st = SCSI_DATA(&st_switch, unit);
	char   *buf;
	u_int32_t readsiz;
	errval  errno;

	buf = malloc(1024, M_TEMP, M_NOWAIT);
	if (!buf)
		return (ENOMEM);

	if (( errno = st_mode_sense(unit, 0, NULL, 0, 0)) ) {
		goto bad;
	}
	st->blksiz = (st->quirks & ST_Q_NO_1024)? 512: 1024;
	do {
		switch ((int)st->blksiz) {
		case 512:
		case 1024:
			readsiz = st->blksiz;
			st->flags |= ST_FIXEDBLOCKS;
			break;
		default:
			readsiz = 1;
			st->flags &= ~ST_FIXEDBLOCKS;
		} if ( (errno = st_mode_select(unit, 0, NULL, 0)) ) {
			goto bad;
		}
		st_read(unit, buf, readsiz, SCSI_SILENT);
		if ( (errno = st_rewind(unit, FALSE, 0)) ) {
bad:			free(buf, M_TEMP);
			return (errno);
		}
	} while (readsiz != 1 && readsiz > st->blksiz);
	free(buf, M_TEMP);
	return 0;
}

static st_devsw_installed = 0;

static void 
st_drvinit(void *unused)
{

	if( ! st_devsw_installed ) {
		bdevsw_add_generic(BDEV_MAJOR, CDEV_MAJOR, &st_bdevsw);
		st_devsw_installed = 1;
    	}
}

SYSINIT(stdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,st_drvinit,NULL)


OpenPOWER on IntegriCloud