-
Notifications
You must be signed in to change notification settings - Fork 20
/
sim_scsi.c
1817 lines (1497 loc) · 63.7 KB
/
sim_scsi.c
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
/* sim_scsi.c: SCSI bus simulation
Copyright (c) 2019, Matt Burke
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of the author shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from the author.
*/
#include "sim_scsi.h"
#include "sim_disk.h"
#include "sim_tape.h"
/* SCSI commands */
#define CMD_TESTRDY 0x00 /* test unit ready */
#define CMD_INQUIRY 0x12 /* inquiry */
#define CMD_REQSENSE 0x03 /* request sense */
#define CMD_RDBLKLIM 0x05 /* read block limits */
#define CMD_MODESEL6 0x15 /* mode select (6 bytes) */
#define CMD_MODESEL10 0x55 /* mode select (10 bytes) */
#define CMD_MODESENSE6 0x1A /* mode sense (6 bytes) */
#define CMD_MODESENSE10 0x5A /* mode sense (10 bytes) */
#define CMD_STARTSTOP 0x1B /* start/stop unit */
#define CMD_LOADUNLOAD 0x1B /* load/unload unit */
#define CMD_PREVALLOW 0x1E /* prevent/allow medium removal */
#define CMD_RDCAP 0x25 /* read capacity */
#define CMD_READ6 0x08 /* read (6 bytes) */
#define CMD_READ10 0x28 /* read (10 bytes) */
#define CMD_RDLONG 0x3E /* read long */
#define CMD_WRITE6 0x0A /* write (6 bytes) */
#define CMD_WRITE10 0x2A /* write (10 bytes) */
#define CMD_ERASE 0x19 /* erase */
#define CMD_RESERVE 0x16 /* reserve unit */
#define CMD_RELEASE 0x17 /* release unit */
#define CMD_REWIND 0x01 /* rewind */
#define CMD_SNDDIAG 0x1D /* send diagnostic */
#define CMD_SPACE 0x11 /* space */
#define CMD_WRFMARK 0x10 /* write filemarks */
#define CMD_READ6_TAPE_FIXED 0x01 /* Fixed record size read */
#define CMD_READ6_TAPE_SILI 0x02 /* Suppress Incorrect Length Indicator */
/* SCSI status codes */
#define STS_OK 0 /* good */
#define STS_CHK 2 /* check condition */
/* SCSI sense keys */
#define KEY_OK 0 /* no sense */
#define KEY_NOTRDY 2 /* not ready */
#define KEY_ILLREQ 5 /* illegal request */
#define KEY_PROT 7 /* data protect */
#define KEY_BLANK 8 /* blank check */
#define KEY_M_ILI 0x20 /* incorrect length indicator */
/* Additional sense codes */
#define ASC_OK 0 /* no additional sense information */
#define ASC_INVCOM 0x20 /* invalid command operation code */
#define ASC_INVCDB 0x24 /* invalid field in cdb */
#define ASC_NOMEDIA 0x3A /* media not present */
#define PUTL(b,x,v) b[x] = (v >> 24) & 0xFF; \
b[x+1] = (v >> 16) & 0xFF; \
b[x+2] = (v >> 8) & 0xFF; \
b[x+3] = v & 0xFF
#define PUTW(b,x,v) b[x] = (v >> 8) & 0xFF; \
b[x+1] = v & 0xFF
#define GETL(b,x) ((b[x] << 24) | \
(b[x+1] << 16) | \
(b[x+2] << 8) | \
b[x+3])
#define GETW(b,x) ((b[x] << 8)|b[x+1])
static void _scsi_vdebug (uint32 dbits, SCSI_BUS *bus, const char* fmt, va_list arglist)
{
UNIT *uptr = bus->dev[bus->target];
size_t tfmt_size = strlen (fmt) + strlen (sim_uname (uptr)) + 3;
char *tfmt = (char *)malloc (tfmt_size);
snprintf (tfmt, tfmt_size, "%s: %s", sim_uname (uptr), fmt);
_sim_vdebug (dbits, bus->dptr, uptr, tfmt, arglist);
free (tfmt);
}
static void scsi_debug_cmd (SCSI_BUS *bus, const char* fmt, ...)
{
va_list arglist;
va_start (arglist, fmt);
_scsi_vdebug (SCSI_DBG_CMD, bus, fmt, arglist);
va_end (arglist);
}
static const char *scsi_phases[] = {
"DATO", /* data out */
"DATI", /* data in */
"CMD", /* command */
"STS", /* status */
"", /* invalid*/
"", /* invalid*/
"MSGO", /* message out */
"MSGI" /* message in */
};
/* Arbitrate for control of the bus */
t_bool scsi_arbitrate (SCSI_BUS *bus, uint32 initiator)
{
if (bus->initiator < 0) { /* bus free? */
sim_debug (SCSI_DBG_BUS, bus->dptr,
"Initiator %d won arbitration\n", initiator);
bus->initiator = initiator; /* won arbitration */
return TRUE;
}
sim_debug (SCSI_DBG_BUS, bus->dptr,
"Initiator %d lost arbitration\n", initiator);
return FALSE; /* lost arbitration */
}
/* Release control of the bus */
void scsi_release (SCSI_BUS *bus)
{
if (bus->initiator < 0) /* already free? */
return;
sim_debug (SCSI_DBG_BUS, bus->dptr,
"Initiator %d released bus\n", bus->initiator);
bus->phase = SCSI_DATO; /* bus free state */
bus->initiator = -1;
bus->target = -1;
bus->buf_t = bus->buf_b = 0;
}
/* Assert the attention signal */
void scsi_set_atn (SCSI_BUS *bus)
{
sim_debug (SCSI_DBG_BUS, bus->dptr,
"Attention signal asserted\n");
bus->atn = TRUE; /* assert ATN */
if (bus->target != -1) /* target selected? */
bus->phase = SCSI_MSGO; /* go to msg out phase */
}
/* Clear the attention signal */
void scsi_release_atn (SCSI_BUS *bus)
{
sim_debug (SCSI_DBG_BUS, bus->dptr,
"Attention signal cleared\n");
bus->atn = FALSE; /* release ATN */
}
/* Assert the request signal */
void scsi_set_req (SCSI_BUS *bus)
{
if (bus->req == FALSE) {
sim_debug (SCSI_DBG_BUS, bus->dptr,
"Request signal asserted\n");
bus->req = TRUE; /* assert REQ */
}
}
/* Clear the attention signal */
void scsi_release_req (SCSI_BUS *bus)
{
if (bus->req == TRUE) {
sim_debug (SCSI_DBG_BUS, bus->dptr,
"Request signal cleared\n");
bus->req = FALSE; /* release REQ */
}
}
void scsi_set_phase (SCSI_BUS *bus, uint32 phase)
{
if (bus->phase != phase) {
sim_debug (SCSI_DBG_BUS, bus->dptr,
"Phase changed to %s\n", scsi_phases[phase]);
bus->phase = phase;
}
}
/* Attempt to select a target device */
t_bool scsi_select (SCSI_BUS *bus, uint32 target)
{
UNIT *uptr = bus->dev[target];
if (bus->initiator < 0) {
sim_debug (SCSI_DBG_BUS, bus->dptr,
"SCSI: Attempted to select a target without arbitration\n");
return FALSE;
}
if (bus->target >= 0) {
sim_debug (SCSI_DBG_BUS, bus->dptr,
"SCSI: Attempted to select a target when a target is already selected\n");
return FALSE;
}
if ((uptr->flags & UNIT_DIS) == 0) { /* unit enabled? */
sim_debug (SCSI_DBG_BUS, bus->dptr,
"Select target %d%s\n", target, (bus->atn ? " with attention" : ""));
if (bus->atn)
scsi_set_phase (bus, SCSI_MSGO); /* message out */
else
scsi_set_phase (bus, SCSI_CMD); /* command */
bus->target = target;
scsi_set_req (bus); /* request data */
return TRUE;
}
sim_debug (SCSI_DBG_BUS, bus->dptr,
"Select timeout for target %d\n", target);
scsi_release (bus);
return FALSE;
}
/* Process a SCSI message */
uint32 scsi_message (SCSI_BUS *bus, uint8 *data, uint32 len)
{
uint32 used;
if (data[0] & 0x80) { /* identify */
bus->lun = (data[0] & 0xF);
sim_debug (SCSI_DBG_MSG, bus->dptr,
"Identify, LUN = %d\n", bus->lun);
scsi_set_req (bus); /* request data */
used = 1; /* message length */
}
else if (data[0] == 0x1) { /* extended message */
if (len < 2)
return 0; /* need more */
if (len < (data[1] + 2u))
return 0; /* need more */
sim_debug (SCSI_DBG_MSG, bus->dptr,
"Extended message\n");
scsi_set_req (bus); /* request data */
used = data[1] + 2; /* extended message length */
}
else if (data[0] == 0x6) { /* abort */
sim_debug (SCSI_DBG_MSG, bus->dptr,
"Abort\n");
scsi_release (bus); /* disconnect */
used = 1;
}
else if (data[0] == 0xc) {
sim_debug (SCSI_DBG_MSG, bus->dptr,
"Bus device reset\n");
scsi_release (bus); /* disconnect */
used = 1;
}
else {
sim_printf ("SCSI: Unknown Message %02X\n", data[0]);
used = len; /* discard all bytes */
}
scsi_set_phase (bus, SCSI_CMD); /* command phase next */
return used;
}
/* Send status to the initiator immediately */
void scsi_status (SCSI_BUS *bus, uint32 sts, uint32 key, uint32 asc)
{
bus->sense_key = key;
bus->sense_code = asc;
bus->buf[0] = sts; /* status code */
bus->buf_b = 1;
scsi_set_phase (bus, SCSI_STS); /* status phase next */
scsi_set_req (bus); /* request to send data */
}
/* Send status to the initiator at the end of transaction */
void scsi_status_deferred (SCSI_BUS *bus, uint32 sts, uint32 key, uint32 asc)
{
bus->status = sts;
bus->sense_key = key;
bus->sense_code = asc;
}
/* Decode the command group to get the command length */
uint32 scsi_decode_group (uint8 data)
{
uint32 group = (data >> 5) & 0x7;
switch (group) {
case 0: /* 6 byte commands */
return 6;
case 1: /* 10 byte commands */
case 2:
return 10;
case 3: /* 12 byte commands */
return 12;
default: /* vendor specific or reserved */
return 0;
}
}
/* Translate sim_tape status to SCSI status */
void scsi_tape_status (SCSI_BUS *bus, t_stat st)
{
switch (st) {
case MTSE_OK:
scsi_status_deferred (bus, STS_OK, KEY_OK, ASC_OK);
break;
case MTSE_TMK:
scsi_status_deferred (bus, STS_CHK, (KEY_OK | 0x80), ASC_OK);
bus->sense_qual = 1; /* filemark detected */
break;
case MTSE_RECE: /* record in error */
case MTSE_INVRL: /* invalid rec lnt */
case MTSE_IOERR: /* IO error */
scsi_status_deferred (bus, STS_CHK, KEY_OK, ASC_OK);
break;
case MTSE_FMT:
case MTSE_UNATT:
case MTSE_EOM: /* end of medium */
scsi_status_deferred (bus, STS_CHK, (KEY_BLANK | 0x40), ASC_OK);
break;
case MTSE_BOT: /* reverse into BOT */
scsi_status_deferred (bus, STS_CHK, (KEY_OK | 0x40), ASC_OK);
break;
case MTSE_WRP: /* write protect */
scsi_status_deferred (bus, STS_CHK, KEY_PROT, ASC_OK);
break;
}
return;
}
/* Limit the transfer count to the allocation specified
by the SCSI command */
void scsi_check_alloc (SCSI_BUS *bus, uint32 alloc)
{
if (bus->buf_b > alloc) /* check allocation */
bus->buf_b = alloc;
}
/* Command - Test Unit Ready */
void scsi_test_ready (SCSI_BUS *bus, uint8 *data, uint32 len)
{
UNIT *uptr = bus->dev[bus->target];
scsi_debug_cmd (bus, "Test Unit Ready\n");
if (uptr->flags & UNIT_ATT) /* attached? */
scsi_status (bus, STS_OK, KEY_OK, ASC_OK); /* unit is ready */
else
scsi_status (bus, STS_CHK, KEY_NOTRDY, ASC_NOMEDIA); /* no media present */
}
/* Command - Inquiry */
void scsi_inquiry (SCSI_BUS *bus, uint8 *data, uint32 len)
{
UNIT *uptr = bus->dev[bus->target];
SCSI_DEV *dev = (SCSI_DEV *)uptr->up7;
scsi_debug_cmd (bus, "Inquiry\n");
if ((bus->lun != 0) || (uptr->flags & UNIT_DIS)) {
memset (&bus->buf[0], 0, 36); /* no such device or lun */
bus->buf[0] = 0x7f;
bus->buf_b += 36;
// scsi_status (bus, STS_CHK, KEY_ILLREQ, ASC_INVCOM);
}
else {
bus->buf[bus->buf_b++] = (dev->pqual << 5) | dev->devtype; /* device class */
#if 0
if (data[0] & 0x01) { /* vital product data */
switch (data[2]) { /* page code */
case 0x00: /* list of supported pages */
bus->buf[bus->buf_b++] = 0x00; /* page code */
bus->buf[bus->buf_b++] = 0x00; /* reserved */
bus->buf[bus->buf_b++] = 0x02; /* page length */
bus->buf[bus->buf_b++] = 0x00; /* page 0 is supported. */
bus->buf[bus->buf_b++] = 0x80; /* page 0x80 is supported. */
break;
case 0x80: /* unit serial # page */
bus->buf[bus->buf_b++] = 0x80; /* page code */
bus->buf[bus->buf_b++] = 0x00; /* reserved */
bus->buf[bus->buf_b++] = 4; /* serial number length */
sprintf (&bus->buf[l], "%-4s", "1234");
l += 4;
break;
default:
sim_printf ("SCSI: Vital product data page %02x not implemented\n", data[2]);
}
}
#endif
if (dev->removeable)
bus->buf[bus->buf_b++] = 0x80; /* removable */
else
bus->buf[bus->buf_b++] = 0; /* fixed */
bus->buf[bus->buf_b++] = dev->scsiver; /* versions */
bus->buf[bus->buf_b++] = dev->scsiver; /* response data format */
bus->buf[bus->buf_b++] = 31; /* additional length */
bus->buf[bus->buf_b++] = 0; /* reserved */
bus->buf[bus->buf_b++] = 0; /* reserved */
bus->buf[bus->buf_b++] = 0;
sprintf ((char *)&bus->buf[bus->buf_b], "%-8s", dev->manufacturer);
bus->buf_b += 8;
sprintf ((char *)&bus->buf[bus->buf_b], "%-16s", dev->product);
bus->buf_b += 16;
sprintf ((char *)&bus->buf[bus->buf_b], "%-4s", dev->rev);
bus->buf_b += 4;
}
scsi_check_alloc (bus, data[4]); /* check allocation */
scsi_set_phase (bus, SCSI_DATI); /* data in phase next */
scsi_set_req (bus); /* request to send data */
}
/* Command - Request Sense */
void scsi_req_sense (SCSI_BUS *bus, uint8 *data, uint32 len)
{
scsi_debug_cmd (bus, "Request Sense\n");
bus->buf[bus->buf_b++] = (0x70 | 0x80); /* current error, valid */
bus->buf[bus->buf_b++] = 0; /* segment # */
bus->buf[bus->buf_b++] = bus->sense_key; /* sense key */
bus->buf[bus->buf_b++] = (bus->sense_info >> 24) & 0xFF; /* information */
bus->buf[bus->buf_b++] = (bus->sense_info >> 16) & 0xFF;
bus->buf[bus->buf_b++] = (bus->sense_info >> 8) & 0xFF;
bus->buf[bus->buf_b++] = bus->sense_info & 0xFF;
bus->buf[bus->buf_b++] = 10; /* additional length */
bus->buf[bus->buf_b++] = 0; /* cmd specific info */
bus->buf[bus->buf_b++] = 0;
bus->buf[bus->buf_b++] = 0;
bus->buf[bus->buf_b++] = 0;
bus->buf[bus->buf_b++] = bus->sense_code; /* ASC */
bus->buf[bus->buf_b++] = bus->sense_qual; /* ASCQ */
bus->buf[bus->buf_b++] = 0; /* FRU code */
bus->buf[bus->buf_b++] = 0; /* sense key specific */
bus->buf[bus->buf_b++] = 0;
bus->buf[bus->buf_b++] = 0;
bus->sense_key = 0; /* no sense */
bus->sense_code = 0; /* no additional sense information */
bus->sense_qual = 0;
bus->sense_info = 0;
scsi_check_alloc (bus, data[4]); /* check allocation */
scsi_set_phase (bus, SCSI_DATI); /* data in phase next */
scsi_set_req (bus); /* request to send data */
}
/* Command - Mode Select (6 byte command) */
void scsi_mode_sel6 (SCSI_BUS *bus, uint8 *data, uint32 len)
{
UNIT *uptr = bus->dev[bus->target];
SCSI_DEV *dev = (SCSI_DEV *)uptr->up7;
uint32 blk_size;
if (bus->phase == SCSI_CMD) {
scsi_debug_cmd (bus, "Mode Select(6) - CMD\n");
memcpy (&bus->cmd[0], &data[0], 6);
bus->buf_b = bus->cmd[4];
scsi_set_phase (bus, SCSI_DATO); /* data out phase next */
scsi_set_req (bus); /* request data */
}
else if (bus->phase == SCSI_DATO) {
scsi_debug_cmd (bus, "Mode Select(6) - DATO\n");
if (dev->devtype == SCSI_TAPE && uptr->flags & SCSI_QIC) {
blk_size = ((uint32)bus->buf[9]) << 16 |
((uint32)bus->buf[10]) << 8 |
(uint32)bus->buf[11];
/* QIC tape ONLY supports requesting a fixed block size of
* 0x200 bytes. Any other block size will cause an illegal
* request. */
if (blk_size == SCSI_QIC_BLKSZ) {
scsi_status(bus, STS_OK, KEY_OK, ASC_OK);
}
else {
scsi_status(bus, STS_CHK, KEY_ILLREQ|KEY_M_ILI, ASC_INVCDB);
}
}
else {
/* Not implemented for disk and non-QIC tape */
scsi_status(bus, STS_OK, KEY_OK, ASC_OK);
}
}
}
/* Command - Mode Select (10 byte command) */
void scsi_mode_sel10 (SCSI_BUS *bus, uint8 *data, uint32 len)
{
if (bus->phase == SCSI_CMD) {
scsi_debug_cmd (bus, "Mode Select(10) - CMD\n");
memcpy (&bus->cmd[0], &data[0], 10);
bus->buf_b = GETW (data, 7);
scsi_set_phase (bus, SCSI_DATO); /* data out phase next */
scsi_set_req (bus); /* request data */
}
else if (bus->phase == SCSI_DATO) {
scsi_debug_cmd (bus, "Mode Select(6) - DATO\n");
/* Not currently implemented so just return
good status for now */
scsi_status (bus, STS_OK, KEY_OK, ASC_OK);
}
}
/* Mode Sense common fields */
void scsi_mode_sense (SCSI_BUS *bus, uint8 *data, uint32 len)
{
UNIT *uptr = bus->dev[bus->target];
SCSI_DEV *dev = (SCSI_DEV *)uptr->up7;
uint32 pc, pctl;
pc = data[2] & 0x3F; /* page code */
pctl = (data[2] >> 6) & 0x3F; /* page control */
bus->buf[bus->buf_b++] = 0x00; /* density code */
bus->buf[bus->buf_b++] = ((uptr->capac - 1) >> 16) & 0xFF; /* # blocks (23:16) */
bus->buf[bus->buf_b++] = ((uptr->capac - 1) >> 8) & 0xFF; /* # blocks (15:8) */
bus->buf[bus->buf_b++] = (uptr->capac - 1) & 0xFF; /* # blocks (7:0) */
bus->buf[bus->buf_b++] = 0x00; /* reserved */
bus->buf[bus->buf_b++] = (dev->block_size >> 16) & 0xFF;
bus->buf[bus->buf_b++] = (dev->block_size >> 8) & 0xFF;
bus->buf[bus->buf_b++] = (dev->block_size >> 0) & 0xFF;
if ((pc == 0x1) || (pc == 0x3F)) {
bus->buf[bus->buf_b++] = 0x1; /* R/W error recovery page */
bus->buf[bus->buf_b++] = 0xA; /* page length */
bus->buf[bus->buf_b++] = 0x26; /* TB, PER, DTE */
bus->buf[bus->buf_b++] = 0x8; /* read retry count */
bus->buf[bus->buf_b++] = 0x78; /* correction span */
bus->buf[bus->buf_b++] = 0;
bus->buf[bus->buf_b++] = 0;
bus->buf[bus->buf_b++] = 0;
bus->buf[bus->buf_b++] = 0x8; /* write retry count */
bus->buf[bus->buf_b++] = 0;
bus->buf[bus->buf_b++] = 0;
bus->buf[bus->buf_b++] = 0;
}
if ((pc == 0x2) || (pc == 0x3F)) {
bus->buf[bus->buf_b++] = 0x2; /* disconnect-reconnect page */
bus->buf[bus->buf_b++] = 0xE; /* page length */
bus->buf[bus->buf_b++] = 0x10; /* buffer full ratio */
bus->buf[bus->buf_b++] = 0x10; /* buffer empty ratio */
bus->buf[bus->buf_b++] = 0;
bus->buf[bus->buf_b++] = 0;
bus->buf[bus->buf_b++] = 0;
bus->buf[bus->buf_b++] = 0;
bus->buf[bus->buf_b++] = 0;
bus->buf[bus->buf_b++] = 0;
bus->buf[bus->buf_b++] = 0;
bus->buf[bus->buf_b++] = 0;
bus->buf[bus->buf_b++] = 0;
bus->buf[bus->buf_b++] = 0;
bus->buf[bus->buf_b++] = 0;
bus->buf[bus->buf_b++] = 0;
}
if ((pc == 0x3) || (pc == 0x3F)) {
bus->buf[bus->buf_b++] = 0x3; /* format device page */
bus->buf[bus->buf_b++] = 0x16; /* page length */
bus->buf[bus->buf_b++] = 0; /* tracks per zone (15:8) */
bus->buf[bus->buf_b++] = 1; /* tracks per zone (7:0) */
bus->buf[bus->buf_b++] = 0; /* alt sectors per zone (15:8) */
bus->buf[bus->buf_b++] = 1; /* alt sectors per zone (7:0) */
bus->buf[bus->buf_b++] = 0; /* alt tracks per zone (15:8) */
bus->buf[bus->buf_b++] = 0; /* alt tracks per zone (7:0) */
bus->buf[bus->buf_b++] = 0; /* alt tracks per unit (15:8) */
bus->buf[bus->buf_b++] = 0; /* alt tracks per unit (7:0) */
bus->buf[bus->buf_b++] = 0; /* sectors per track (15:8) */
bus->buf[bus->buf_b++] = 0x21; /* sectors per track (7:0) */
bus->buf[bus->buf_b++] = 0x2; /* bytes per sector (15:8) */
bus->buf[bus->buf_b++] = 0; /* bytes per sector (7:0) */
bus->buf[bus->buf_b++] = 0; /* interleave (15:8) */
bus->buf[bus->buf_b++] = 0; /* interleave (7:0) */
bus->buf[bus->buf_b++] = 0; /* track skew factor (15:8) */
bus->buf[bus->buf_b++] = 0; /* track skew factor (7:0) */
bus->buf[bus->buf_b++] = 0; /* cyl skew factor (15:8) */
bus->buf[bus->buf_b++] = 0; /* cyl skew factor (7:0) */
bus->buf[bus->buf_b++] = 0x40; /* flags */
bus->buf[bus->buf_b++] = 0; /* reserved */
bus->buf[bus->buf_b++] = 0; /* reserved */
bus->buf[bus->buf_b++] = 0; /* reserved */
}
if ((pc == 0x4) || (pc == 0x3F)) {
bus->buf[bus->buf_b++] = 0x4; /* rigid disk geometry page */
bus->buf[bus->buf_b++] = 0x16; /* page length */
bus->buf[bus->buf_b++] = 0; /* # cyls (23:16) */
bus->buf[bus->buf_b++] = 0x4; /* # cyls (15:8) */
bus->buf[bus->buf_b++] = 0; /* # cyls (7:0) */
bus->buf[bus->buf_b++] = 0x2; /* # heads */
bus->buf[bus->buf_b++] = 0; /* start cyl for write precomp (23:16) */
bus->buf[bus->buf_b++] = 0x4; /* start cyl for write precomp (15:8) */
bus->buf[bus->buf_b++] = 0; /* start cyl for write precomp (7:0) */
bus->buf[bus->buf_b++] = 0; /* start cyl for reduced write current (23:16) */
bus->buf[bus->buf_b++] = 0x4; /* start cyl for reduced write current (15:8) */
bus->buf[bus->buf_b++] = 0; /* start cyl for reduced write current (7:0) */
bus->buf[bus->buf_b++] = 0; /* drive step rate (15:8) */
bus->buf[bus->buf_b++] = 0x1; /* drive step rate (7:0) */
bus->buf[bus->buf_b++] = 0; /* landing zone cyl (23:16) */
bus->buf[bus->buf_b++] = 0x4; /* landing zone cyl (15:8) */
bus->buf[bus->buf_b++] = 0; /* landing zone cyl (7:0) */
bus->buf[bus->buf_b++] = 0; /* reserved, RPL */
bus->buf[bus->buf_b++] = 0; /* rotational offset */
bus->buf[bus->buf_b++] = 0; /* reserved */
bus->buf[bus->buf_b++] = 0x1C; /* medium rotation rate (15:8) */
bus->buf[bus->buf_b++] = 0x20; /* medium rotation rate (7:0) */
bus->buf[bus->buf_b++] = 0; /* reserved */
bus->buf[bus->buf_b++] = 0; /* reserved */
}
if ((pc == 0xA) || (pc == 0x3F)) {
bus->buf[bus->buf_b++] = 0xA; /* control mode page */
bus->buf[bus->buf_b++] = 0x6; /* page length */
bus->buf[bus->buf_b++] = 0;
bus->buf[bus->buf_b++] = 0;
bus->buf[bus->buf_b++] = 0;
bus->buf[bus->buf_b++] = 0;
bus->buf[bus->buf_b++] = 0;
bus->buf[bus->buf_b++] = 0;
}
}
/* Command - Mode Sense (6 byte command) */
void scsi_mode_sense6 (SCSI_BUS *bus, uint8 *data, uint32 len)
{
UNIT *uptr = bus->dev[bus->target];
SCSI_DEV *dev = (SCSI_DEV *)uptr->up7;
uint32 pc, pctl;
scsi_debug_cmd (bus, "Mode Sense(6)\n");
pc = data[2] & 0x3F; /* page code */
pctl = (data[2] >> 6) & 0x3F; /* page control */
if (pc == 0x8) {
scsi_status (bus, STS_CHK, KEY_ILLREQ, ASC_INVCDB);
return;
}
memset (&bus->buf[0], 0, data[4]); /* allocation len */
bus->buf[bus->buf_b++] = 0x0; /* mode data length */
bus->buf[bus->buf_b++] = 0x0; /* medium type */
if (dev->devtype == SCSI_CDROM)
bus->buf[bus->buf_b++] = 0x80; /* dev specific param */
else
bus->buf[bus->buf_b++] = 0x0; /* dev specific param */
bus->buf[bus->buf_b++] = 0x8; /* block descriptor len */
scsi_mode_sense (bus, data, len); /* get common data */
bus->buf[0] = bus->buf_b - 1; /* mode data length */
scsi_check_alloc (bus, data[4]); /* check allocation */
scsi_set_phase (bus, SCSI_DATI);
scsi_set_req (bus); /* request to send data */
}
/* Command - Mode Sense (10 byte command) */
void scsi_mode_sense10 (SCSI_BUS *bus, uint8 *data, uint32 len)
{
uint32 pc, pctl;
scsi_debug_cmd (bus, "Mode Sense(10)\n");
pc = data[2] & 0x3F; /* page code */
pctl = (data[2] >> 6) & 0x3F; /* page control */
if (pc == 0x8) {
scsi_status (bus, STS_CHK, KEY_ILLREQ, ASC_INVCDB);
return;
}
memset (&bus->buf[0], 0, GETW (data, 7)); /* allocation len */
bus->buf[bus->buf_b++] = 0x0; /* mode data length (15:8) */
bus->buf[bus->buf_b++] = 0x0; /* mode data length (7:0) */
bus->buf[bus->buf_b++] = 0x0; /* medium type */
bus->buf[bus->buf_b++] = 0x0; /* dev specific param */
bus->buf[bus->buf_b++] = 0x0; /* reserved */
bus->buf[bus->buf_b++] = 0x0; /* reserved */
bus->buf[bus->buf_b++] = 0x0; /* block descriptor len (15:8) */
bus->buf[bus->buf_b++] = 0x8; /* block descriptor len (7:0) */
scsi_mode_sense (bus, data, len); /* get common data */
PUTW (bus->buf, 0, (bus->buf_b - 1)); /* mode data length */
scsi_check_alloc (bus, GETW (data, 7)); /* check allocation */
scsi_set_phase (bus, SCSI_DATI);
scsi_set_req (bus); /* request to send data */
}
/* Command - Start/Stop Unit */
void scsi_start_stop (SCSI_BUS *bus, uint8 *data, uint32 len)
{
scsi_debug_cmd (bus, "Start/Stop Unit\n");
scsi_status (bus, STS_OK, KEY_OK, ASC_OK);
}
/* Command - Prevent/Allow Medium Removal */
void scsi_prev_allow (SCSI_BUS *bus, uint8 *data, uint32 len)
{
scsi_debug_cmd (bus, "Prevent/Allow Medium Removal\n");
scsi_status (bus, STS_OK, KEY_OK, ASC_OK);
}
/* Command - Read Capacity */
void scsi_read_capacity (SCSI_BUS *bus, uint8 *data, uint32 len)
{
UNIT *uptr = bus->dev[bus->target];
SCSI_DEV *dev = (SCSI_DEV *)uptr->up7;
scsi_debug_cmd (bus, "Read Capacity, pmi = %d\n", (data[8] & 0x1));
if ((uptr->flags & UNIT_ATT) == 0) { /* not attached? */
scsi_status (bus, STS_CHK, KEY_NOTRDY, ASC_NOMEDIA);
return;
}
PUTL (bus->buf, 0, (uptr->capac - 1)); /* # blocks */
PUTL (bus->buf, 4, dev->block_size); /* block size */
bus->buf_b = 8;
scsi_set_phase (bus, SCSI_DATI); /* data in phase next */
scsi_set_req (bus); /* request to send data */
}
/* Command - Read (6 byte command), disk version */
void scsi_read6_disk (SCSI_BUS *bus, uint8 *data, uint32 len)
{
UNIT *uptr = bus->dev[bus->target];
SCSI_DEV *dev = (SCSI_DEV *)uptr->up7;
t_lba lba;
t_seccnt sects, sectsread;
t_stat r;
lba = GETW (data, 2) | ((data[1] & 0x1F) << 16);
sects = data[4];
if (sects == 0)
sects = 256;
scsi_debug_cmd (bus, "Read(6) lba %d blks %d\n", lba, sects);
if (uptr->flags & UNIT_ATT)
r = sim_disk_rdsect (uptr, lba, &bus->buf[0], §sread, sects);
else {
memset (&bus->buf[0], 0, (sects * dev->block_size));
sectsread = sects;
}
bus->buf_b = (sectsread * dev->block_size);
scsi_set_phase (bus, SCSI_DATI); /* data in phase next */
scsi_set_req (bus); /* request to send data */
}
/* Command - Read (6 byte command), tape version */
void scsi_read6_tape (SCSI_BUS *bus, uint8 *data, uint32 len)
{
UNIT *uptr = bus->dev[bus->target];
SCSI_DEV *dev = (SCSI_DEV *)uptr->up7;
t_seccnt sects, sectsread, new_buf_b;
t_stat r;
uint32 i;
if ((data[1] & 0x3) == 0x3) { /* SILI and FIXED? */
scsi_status (bus, STS_CHK, KEY_ILLREQ, ASC_INVCDB);
return;
}
sects = GETW (data, 3) | (data[2] << 16);
new_buf_b = 0;
sectsread = 0;
if (sects == 0) { /* no data to read */
scsi_status (bus, STS_OK, KEY_OK, ASC_OK);
return;
}
scsi_debug_cmd (bus, "Read(6) blks %d fixed %d\n", sects, (data[1] & 0x1));
if (uptr->flags & UNIT_ATT) {
if (uptr->flags & SCSI_QIC) {
if (data[1] & 0x1) {
/* If this is a QIC tape drive and bit 0 is set, this is a
request to read multiple fixed-length blocks. */
scsi_debug_cmd(bus, "QIC in fixed block mode\n");
for (i = 0; i < sects; i++) {
r = sim_tape_rdrecf(uptr, &bus->buf[new_buf_b], §sread, SCSI_QIC_BLKSZ);
scsi_debug_cmd(bus, "Read tape blk %d, read %d, r = %d\n",
sects, sectsread, r);
if (r == MTSE_OK) {
new_buf_b += SCSI_QIC_BLKSZ;
} else {
scsi_tape_status(bus, r);
scsi_status(bus, bus->status, bus->sense_key, bus->sense_code);
return;
}
}
} else {
/* QIC drives respond with an illegal request when the
request does not specify fixed-block mode */
scsi_debug_cmd(bus, "QIC not in fixed block mode, invalid command\n");
scsi_status(bus, STS_CHK, KEY_ILLREQ|KEY_M_ILI, ASC_INVCDB);
return;
}
}
else {
/* Otherwise, this is a normal streaming tape read */
if (data[1] & 0x1) {
r = sim_tape_rdrecf (uptr, &bus->buf[0], §sread, (sects * dev->block_size));
scsi_debug_cmd (bus, "Read tape blk %d, read %d, r = %d\n", sects, sectsread, r);
}
else {
r = sim_tape_rdrecf (uptr, &bus->buf[0], §sread, sects);
scsi_debug_cmd (bus, "Read tape max %d, read %d, r = %d\n", sects, sectsread, r);
if (r == MTSE_INVRL) { /* overlength condition */
scsi_debug_cmd (bus, "Overlength\n");
if ((data[1] & 0x2) && (dev->block_size == 0)) { /* SILI set */
scsi_debug_cmd (bus, "SILI set\n");
}
else {
scsi_debug_cmd (bus, "SILI not set - check condition\n");
scsi_status (bus, STS_CHK, (KEY_OK | KEY_M_ILI), ASC_OK);
return;
}
}
else if ((r == MTSE_OK) && (sectsread < sects)) { /* underlength condition */
scsi_debug_cmd (bus, "Underlength\n");
if (data[1] & 0x2) { /* SILI set */
scsi_debug_cmd (bus, "SILI set\n");
}
else {
scsi_debug_cmd (bus, "SILI not set - check condition\n");
scsi_status_deferred (bus, STS_CHK, (KEY_OK | KEY_M_ILI), ASC_OK);
bus->sense_info = (sects - sectsread);
}
}
}
new_buf_b = sectsread;
}
if (r != MTSE_OK) {
scsi_debug_cmd (bus, "Read error, r = %d\n", r);
}
scsi_tape_status (bus, r);
}
else {
memset (&bus->buf[0], 0, (sects * dev->block_size));
sectsread = (sects * dev->block_size);
}
if (sectsread > 0) {
bus->buf_b = new_buf_b;
scsi_set_phase (bus, SCSI_DATI); /* data in phase next */
}
else {
bus->buf[bus->buf_b++] = bus->status; /* status code */
scsi_set_phase (bus, SCSI_STS); /* status phase next */
}
scsi_set_req (bus); /* request to send data */
}
/* Command - Read (10 byte command), disk version */
void scsi_read10_disk (SCSI_BUS *bus, uint8 *data, uint32 len)
{
UNIT *uptr = bus->dev[bus->target];
SCSI_DEV *dev = (SCSI_DEV *)uptr->up7;
t_lba lba;
t_seccnt sects, sectsread;
t_stat r;
lba = GETL (data, 2);
sects = GETW (data, 7);
scsi_debug_cmd (bus, "Read(10) lba %d blks %d\n", lba, sects);
if (sects == 0) { /* no data to read */
scsi_status (bus, STS_OK, KEY_OK, ASC_OK);
return;
}
if (uptr->flags & UNIT_ATT)
r = sim_disk_rdsect (uptr, lba, &bus->buf[0], §sread, sects);
else {
memset (&bus->buf[0], 0, (sects * dev->block_size));
sectsread = sects;
}
bus->buf_b = (sectsread * dev->block_size);
scsi_set_phase (bus, SCSI_DATI); /* data in phase next */
scsi_set_req (bus); /* request to send data */
}
/* Command - Read Long */
/* This command is needed by VMS for host-based volume shadowing */
/* See DKDRIVER */
void scsi_read_long (SCSI_BUS *bus, uint8 *data, uint32 len)
{
UNIT *uptr = bus->dev[bus->target];
t_lba lba;
t_seccnt sects, sectsread;
t_stat r;
lba = GETL (data, 2);
sects = GETW (data, 7);
scsi_debug_cmd (bus, "Read Long lba %d bytes %d\n", lba, sects);
if (uptr->flags & UNIT_ATT)
r = sim_disk_rdsect (uptr, lba, &bus->buf[0], §sread, ((sects >> 9) + 1));
else {
memset (&bus->buf[0], 0, sects);
}
bus->buf_b = sects;
scsi_set_phase (bus, SCSI_DATI); /* data in phase next */
scsi_set_req (bus); /* request to send data */
}
/* Command - Write (6 byte command), disk version */
void scsi_write6_disk (SCSI_BUS *bus, uint8 *data, uint32 len)
{
UNIT *uptr = bus->dev[bus->target];
SCSI_DEV *dev = (SCSI_DEV *)uptr->up7;
t_lba lba;
t_seccnt sects, sectswritten;
t_stat r;
if (bus->phase == SCSI_CMD) {
scsi_debug_cmd (bus, "Write(6) - CMD\n");
memcpy (&bus->cmd[0], &data[0], 6);
sects = bus->cmd[4];
if (sects == 0) sects = 256;
bus->buf_b = (sects * dev->block_size);
scsi_set_phase (bus, SCSI_DATO); /* data out phase next */
scsi_set_req (bus); /* request data */
}
else if (bus->phase == SCSI_DATO) {
sects = bus->cmd[4];
if (sects == 0) sects = 256;
lba = GETW (bus->cmd, 2) | ((bus->cmd[1] & 0x1F) << 16);
scsi_debug_cmd (bus, "Write(6) - DATO, lba %d bytes %d\n", lba, sects);
if (uptr->flags & UNIT_ATT)
r = sim_disk_wrsect (uptr, lba, &bus->buf[0], §swritten, sects);
memset (&bus->cmd[0], 0, 10);
scsi_status (bus, STS_OK, KEY_OK, ASC_OK);
}
}
/* Command - Write (6 byte command), tape version */