-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathscsi.cpp
5750 lines (5084 loc) · 134 KB
/
scsi.cpp
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
/*
* UAE - The Un*x Amiga Emulator
*
* SCSI and SASI emulation (not uaescsi.device)
*
* Copyright 2007-2022 Toni Wilen
*
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include "options.h"
#include "filesys.h"
#include "blkdev.h"
#include "zfile.h"
#include "debug.h"
#include "memory.h"
#include "scsi.h"
#include "autoconf.h"
#include "rommgr.h"
#include "newcpu.h"
#include "custom.h"
#include "gayle.h"
#include "cia.h"
#include "devices.h"
#include "flashrom.h"
#include "gui.h"
#define SCSI_EMU_DEBUG 0
#define RAW_SCSI_DEBUG 0
#define NCR5380_DEBUG 0
#define NCR5380_DEBUG_IRQ 0
#define NCR53400_DEBUG 0
#define NCR5380_SUPRA 1
#define NONCR_GOLEM 2
#define NCR5380_STARDRIVE 3
#define NONCR_KOMMOS 4
#define NONCR_VECTOR 5
#define NONCR_APOLLO 6
#define NCR5380_PROTAR 7
#define NCR5380_ADD500 8
#define NCR5380_KRONOS 9
#define NCR5380_ADSCSI 10
#define NCR5380_ROCHARD 11
#define NCR5380_CLTD 12
#define NCR5380_PTNEXUS 13
#define NCR5380_DATAFLYER 14
#define NONCR_TECMAR 15
#define NCR5380_XEBEC 16
#define NONCR_MICROFORGE 17
#define NONCR_PARADOX 18
#define OMTI_HDA506 19
#define OMTI_ALF1 20
#define OMTI_PROMIGOS 21
#define OMTI_SYSTEM2000 22
#define OMTI_ADAPTER 23
#define NCR5380_X86_RT1000 24
#define NCR5380_PHOENIXBOARD 25
#define NCR5380_TRUMPCARDPRO 26
#define NCR5380_IVSVECTOR 27 // nearly identical to trumpcard pro
#define NCR5380_SCRAM 28
#define NCR5380_OSSI 29
#define NCR5380_DATAFLYERPLUS 30
#define NONCR_HARDFRAME 31
#define NCR5380_MALIBU 32
#define NCR5380_ADDHARD 33
#define NONCR_INMATE 34
#define NCR5380_EMPLANT 35
#define OMTI_HD3000 36
#define OMTI_WEDGE 37
#define NCR5380_EVESHAMREF 38
#define OMTI_PROFEX 39
#define NCR5380_FASTTRAK 40
#define NCR5380_12GAUGE 41
#define NCR5380_OVERDRIVE 42
#define NCR5380_TRUMPCARD 43
#define OMTI_ALF2 44
#define NCR5380_SYNTHESIS 45 // clone of ICD AdSCSI
#define NCR5380_FIREBALL 46
#define OMTI_HD20 47
#define NCR_LAST 48
extern int log_scsiemu;
static const uae_s16 outcmd[] = { 0x04, 0x0a, 0x0c, 0x11, 0x2a, 0xaa, 0x15, 0x55, 0x0f, -1 };
static const uae_s16 incmd[] = { 0x01, 0x03, 0x08, 0x0e, 0x12, 0x1a, 0x5a, 0x25, 0x28, 0x34, 0x37, 0x42, 0x43, 0xa8, 0x51, 0x52, 0xb9, 0xbd, 0xd8, 0xd9, 0xbe, -1 };
static const uae_s16 nonecmd[] = { 0x00, 0x05, 0x06, 0x07, 0x09, 0x0b, 0x10, 0x16, 0x17, 0x19, 0x1b, 0x1d, 0x1e, 0x2b, 0x35, 0x45, 0x47, 0x48, 0x49, 0x4b, 0x4e, 0xa5, 0xa9, 0xba, 0xbc, 0xe0, 0xe3, 0xe4, -1 };
static const uae_s16 safescsi[] = { 0x00, 0x01, 0x03, 0x08, 0x0e, 0x0f, 0x12, 0x1a, 0x1b, 0x25, 0x28, 0x35, 0x5a, -1 };
static const uae_s16 scsicmdsizes[] = { 6, 10, 10, 12, 16, 12, 10, 6 };
static void scsi_illegal_command(struct scsi_data *sd)
{
uae_u8 *s = sd->sense;
memset (s, 0, sizeof (sd->sense));
sd->status = SCSI_STATUS_CHECK_CONDITION;
s[0] = 0x70;
s[2] = 5; /* ILLEGAL REQUEST */
s[12] = 0x24; /* ILLEGAL FIELD IN CDB */
sd->sense_len = 0x12;
}
static void scsi_grow_buffer(struct scsi_data *sd, int newsize)
{
if (sd->buffer_size >= newsize)
return;
uae_u8 *oldbuf = sd->buffer;
int oldsize = sd->buffer_size;
sd->buffer_size = newsize + SCSI_DEFAULT_DATA_BUFFER_SIZE;
write_log(_T("SCSI buffer %d -> %d\n"), oldsize, sd->buffer_size);
sd->buffer = xmalloc(uae_u8, sd->buffer_size);
memcpy(sd->buffer, oldbuf, oldsize);
xfree(oldbuf);
}
static int scsi_data_dir(struct scsi_data *sd)
{
int i;
uae_u8 cmd;
cmd = sd->cmd[0];
for (i = 0; outcmd[i] >= 0; i++) {
if (cmd == outcmd[i]) {
return 1;
}
}
for (i = 0; incmd[i] >= 0; i++) {
if (cmd == incmd[i]) {
return -1;
}
}
for (i = 0; nonecmd[i] >= 0; i++) {
if (cmd == nonecmd[i]) {
return 0;
}
}
write_log (_T("SCSI command %02X, no direction specified!\n"), sd->cmd[0]);
return 0;
}
bool scsi_emulate_analyze (struct scsi_data *sd)
{
int cmd_len, data_len, data_len2, tmp_len;
data_len = sd->data_len;
data_len2 = 0;
cmd_len = scsicmdsizes[sd->cmd[0] >> 5];
if (sd->hdhfd && sd->hdhfd->ansi_version < 2 && cmd_len > 10)
goto nocmd;
sd->cmd_len = cmd_len;
switch (sd->cmd[0])
{
case 0x04: // FORMAT UNIT
if (sd->device_type == UAEDEV_CD)
goto nocmd;
// FmtData set?
if (sd->cmd[1] & 0x10) {
int cl = (sd->cmd[1] & 8) != 0;
int dlf = sd->cmd[1] & 7;
data_len2 = 4;
} else {
sd->direction = 0;
sd->data_len = 0;
return true;
}
break;
case 0x06: // FORMAT TRACK
case 0x07: // FORMAT BAD TRACK
if (sd->device_type == UAEDEV_CD)
goto nocmd;
sd->direction = 0;
sd->data_len = 0;
return true;
case 0x0c: // INITIALIZE DRIVE CHARACTERICS (SASI)
if (sd->hfd && sd->hfd->ci.unit_feature_level < HD_LEVEL_SASI)
goto nocmd;
data_len = 8;
break;
case 0x08: // READ(6)
data_len2 = (sd->cmd[4] == 0 ? 256 : sd->cmd[4]) * sd->blocksize;
scsi_grow_buffer(sd, data_len2);
break;
case 0x11: // ASSIGN ALTERNATE TRACK (SASI)
if (sd->hfd && sd->hfd->ci.unit_feature_level < HD_LEVEL_SASI)
goto nocmd;
data_len = 4;
break;
case 0x28: // READ(10)
data_len2 = ((sd->cmd[7] << 8) | (sd->cmd[8] << 0)) * sd->blocksize;
scsi_grow_buffer(sd, data_len2);
break;
case 0xa8: // READ(12)
data_len2 = ((sd->cmd[6] << 24) | (sd->cmd[7] << 16) | (sd->cmd[8] << 8) | (sd->cmd[9] << 0)) * sd->blocksize;
scsi_grow_buffer(sd, data_len2);
break;
case 0x0f: // WRITE SECTOR BUFFER
data_len = sd->blocksize;
scsi_grow_buffer(sd, data_len);
break;
case 0x0a: // WRITE(6)
if (sd->device_type == UAEDEV_CD)
goto nocmd;
data_len = (sd->cmd[4] == 0 ? 256 : sd->cmd[4]) * sd->blocksize;
scsi_grow_buffer(sd, data_len);
break;
case 0x2a: // WRITE(10)
if (sd->device_type == UAEDEV_CD)
goto nocmd;
data_len = ((sd->cmd[7] << 8) | (sd->cmd[8] << 0)) * sd->blocksize;
scsi_grow_buffer(sd, data_len);
break;
case 0xaa: // WRITE(12)
if (sd->device_type == UAEDEV_CD)
goto nocmd;
data_len = ((sd->cmd[6] << 24) | (sd->cmd[7] << 16) | (sd->cmd[8] << 8) | (sd->cmd[9] << 0)) * sd->blocksize;
scsi_grow_buffer(sd, data_len);
break;
case 0xbe: // READ CD
case 0xb9: // READ CD MSF
case 0xd8: // READ CD-DA
case 0xd9: // READ CD-DA MSF
if (sd->device_type != UAEDEV_CD)
goto nocmd;
tmp_len = (sd->cmd[6] << 16) | (sd->cmd[7] << 8) | sd->cmd[8];
// max block transfer size, it is usually smaller.
tmp_len *= 2352 + 96;
scsi_grow_buffer(sd, tmp_len);
break;
case 0x2f: // VERIFY
if (sd->cmd[1] & 2) {
sd->data_len = ((sd->cmd[7] << 8) | (sd->cmd[8] << 0)) * sd->blocksize;
scsi_grow_buffer(sd, sd->data_len);
sd->direction = 1;
} else {
sd->data_len = 0;
sd->direction = 0;
}
return true;
case 0x15: // MODE SELECT (6)
case 0x55: // MODE SELECT (10)
if (cmd_len == 6) {
data_len = sd->cmd[4];
} else {
data_len = (sd->cmd[7] << 8) | sd->cmd[8];
}
scsi_grow_buffer(sd, data_len);
break;
}
if (data_len < 0) {
if (cmd_len == 6) {
sd->data_len = sd->cmd[4];
} else {
sd->data_len = (sd->cmd[7] << 8) | sd->cmd[8];
}
} else {
sd->data_len = data_len;
}
sd->direction = scsi_data_dir(sd);
if (sd->direction > 0 && sd->data_len == 0) {
sd->direction = 0;
}
return true;
nocmd:
sd->status = SCSI_STATUS_CHECK_CONDITION;
sd->direction = 0;
scsi_illegal_command(sd);
return false;
}
void scsi_illegal_lun(struct scsi_data *sd)
{
uae_u8 *s = sd->sense;
memset (s, 0, sizeof (sd->sense));
sd->status = SCSI_STATUS_CHECK_CONDITION;
s[0] = 0x70;
s[2] = SCSI_SK_ILLEGAL_REQ;
s[12] = SCSI_INVALID_LUN;
sd->sense_len = 0x12;
}
void scsi_clear_sense(struct scsi_data *sd)
{
memset (sd->sense, 0, sizeof (sd->sense));
memset (sd->reply, 0, sizeof (sd->reply));
sd->sense[0] = 0x70;
}
static void showsense(struct scsi_data *sd)
{
if (log_scsiemu) {
for (int i = 0; i < sd->sense_len; i++) {
if (i > 0)
write_log (_T("."));
write_log (_T("%02X"), sd->buffer[i]);
}
write_log (_T("\n"));
}
}
static void copysense(struct scsi_data *sd)
{
bool sasi = sd->hfd && (sd->hfd->ci.unit_feature_level >= HD_LEVEL_SASI && sd->hfd->ci.unit_feature_level <= HD_LEVEL_SASI_ENHANCED);
int len = sd->cmd[4];
if (len == 0 || sasi)
len = 4;
memset(sd->buffer, 0, len);
int tlen = sd->sense_len > len ? len : sd->sense_len;
memcpy(sd->buffer, sd->sense, tlen);
if (!sasi && sd->sense_len == 0) {
// at least 0x12 bytes if SCSI and no sense
tlen = len > 0x12 ? 0x12 : len;
sd->buffer[0] = 0x70;
sd->sense_len = tlen;
}
if (log_scsiemu)
write_log(_T("REQUEST SENSE %d (%d -> %d)\n"), sd->cmd[4], sd->sense_len, tlen);
showsense (sd);
sd->data_len = tlen;
scsi_clear_sense(sd);
}
static void copyreply(struct scsi_data *sd)
{
if (sd->status == 0 && sd->reply_len > 0) {
memset(sd->buffer, 0, 256);
memcpy(sd->buffer, sd->reply, sd->reply_len);
sd->data_len = sd->reply_len;
}
}
static void scsi_set_unit_attention(struct scsi_data *sd, uae_u8 v1, uae_u8 v2)
{
sd->unit_attention = (v1 << 8) | v2;
}
static void scsi_emulate_reset_device(struct scsi_data *sd)
{
if (!sd)
return;
if (sd->device_type == UAEDEV_HDF && sd->nativescsiunit < 0) {
scsi_clear_sense(sd);
// SCSI bus reset occurred
scsi_set_unit_attention(sd, 0x29, 0x02);
}
}
static bool handle_ca(struct scsi_data *sd)
{
bool cc = sd->sense_len > 2 && sd->sense[2] >= 2;
bool ua = sd->unit_attention != 0;
uae_u8 cmd = sd->cmd[0];
// INQUIRY
if (cmd == 0x12) {
if (ua && cc && sd->sense[2] == 6) {
// INQUIRY clears UA only if previous
// command was aborted due to UA
sd->unit_attention = 0;
}
memset(sd->reply, 0, sizeof(sd->reply));
return true;
}
// REQUEST SENSE
if (cmd == 0x03) {
if (ua) {
uae_u8 *s = sd->sense;
scsi_clear_sense(sd);
s[0] = 0x70;
s[2] = 6; /* UNIT ATTENTION */
s[12] = (sd->unit_attention >> 8) & 0xff;
s[13] = (sd->unit_attention >> 0) & 0xff;
sd->sense_len = 0x12;
}
sd->unit_attention = 0;
return true;
}
scsi_clear_sense(sd);
if (ua) {
uae_u8 *s = sd->sense;
s[0] = 0x70;
s[2] = 6; /* UNIT ATTENTION */
s[12] = (sd->unit_attention >> 8) & 0xff;
s[13] = (sd->unit_attention >> 0) & 0xff;
sd->sense_len = 0x12;
sd->unit_attention = 0;
sd->status = 2;
return false;
}
return true;
}
bool scsi_cmd_is_safe(uae_u8 cmd)
{
for (int i = 0; safescsi[i] >= 0; i++) {
if (safescsi[i] == cmd) {
return true;
}
}
return false;
}
void scsi_emulate_cmd(struct scsi_data *sd)
{
sd->status = 0;
if ((sd->message[0] & 0xc0) == 0x80 && (sd->message[0] & 0x1f)) {
uae_u8 lun = sd->message[0] & 0x1f;
if (lun > 7)
lun = 7;
sd->cmd[1] &= ~(7 << 5);
sd->cmd[1] |= lun << 5;
}
#if SCSI_EMU_DEBUG
write_log (_T("CMD=%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x (%d,%d)\n"),
sd->cmd[0], sd->cmd[1], sd->cmd[2], sd->cmd[3], sd->cmd[4], sd->cmd[5], sd->cmd[6], sd->cmd[7], sd->cmd[8], sd->cmd[9],
sd->device_type, sd->nativescsiunit);
#endif
if (sd->device_type == UAEDEV_CD && sd->cd_emu_unit >= 0) {
uae_u32 ua = 0;
ua = scsi_cd_emulate(sd->cd_emu_unit, NULL, 0, 0, 0, 0, 0, 0, 0, sd->atapi);
if (ua)
sd->unit_attention = ua;
if (handle_ca(sd)) {
if (sd->cmd[0] == 0x03) { /* REQUEST SENSE */
scsi_cd_emulate(sd->cd_emu_unit, sd->cmd, 0, 0, 0, 0, 0, 0, 0, sd->atapi); /* ack request sense */
copysense(sd);
} else {
sd->status = scsi_cd_emulate(sd->cd_emu_unit, sd->cmd, sd->cmd_len, sd->buffer, &sd->data_len, sd->reply, &sd->reply_len, sd->sense, &sd->sense_len, sd->atapi);
copyreply(sd);
}
}
gui_flicker_led(LED_CD, sd->uae_unitnum, 1);
} else if (sd->device_type == UAEDEV_HDF && sd->nativescsiunit < 0) {
uae_u32 ua = 0;
ua = scsi_hd_emulate(sd->hfd, sd->hdhfd, NULL, 0, 0, 0, 0, 0, 0, 0);
if (ua)
sd->unit_attention = ua;
if (handle_ca(sd)) {
if (sd->cmd[0] == 0x03) { /* REQUEST SENSE */
scsi_hd_emulate(sd->hfd, sd->hdhfd, sd->cmd, 0, 0, 0, 0, 0, sd->sense, &sd->sense_len);
copysense(sd);
} else {
sd->status = scsi_hd_emulate(sd->hfd, sd->hdhfd,
sd->cmd, sd->cmd_len, sd->buffer, &sd->data_len, sd->reply, &sd->reply_len, sd->sense, &sd->sense_len);
copyreply(sd);
}
}
} else if (sd->device_type == UAEDEV_TAPE && sd->nativescsiunit < 0) {
uae_u32 ua = 0;
ua = scsi_tape_emulate(sd->tape, NULL, 0, 0, 0, 0, 0, 0, 0);
if (ua)
sd->unit_attention = ua;
if (handle_ca(sd)) {
if (sd->cmd[0] == 0x03) { /* REQUEST SENSE */
scsi_tape_emulate(sd->tape, sd->cmd, 0, 0, 0, sd->reply, &sd->reply_len, sd->sense, &sd->sense_len); /* get request sense extra bits */
copysense(sd);
} else {
sd->status = scsi_tape_emulate(sd->tape,
sd->cmd, sd->cmd_len, sd->buffer, &sd->data_len, sd->reply, &sd->reply_len, sd->sense, &sd->sense_len);
copyreply(sd);
}
}
} else if (sd->device_type == UAEDEV_DIR) {
uae_u32 ua = 0;
ua = scsi_hd_emulate(sd->hfd, sd->hdhfd, NULL, 0, 0, 0, 0, 0, 0, 0);
if (ua)
sd->unit_attention = ua;
if (handle_ca(sd)) {
if (scsi_cmd_is_safe(sd->cmd[0])) {
if (sd->cmd[0] == 0x03) { /* REQUEST SENSE */
scsi_hd_emulate(sd->hfd, sd->hdhfd, sd->cmd, 0, 0, 0, 0, 0, sd->sense, &sd->sense_len);
copysense(sd);
} else {
sd->status = scsi_hd_emulate(sd->hfd, sd->hdhfd,
sd->cmd, sd->cmd_len, sd->buffer, &sd->data_len, sd->reply, &sd->reply_len, sd->sense, &sd->sense_len);
copyreply(sd);
}
} else {
sd->sense[0] = 0x70;
sd->sense[2] = 5; /* Illegal Request */
sd->sense[12] = 0x20; /* Invalid/unsupported command code */
sd->sense_len = 18;
sd->status = 2;
copyreply(sd);
}
}
} else if (sd->nativescsiunit >= 0) {
struct amigascsi as;
memset(sd->sense, 0, 256);
memset(&as, 0, sizeof as);
memcpy (&as.cmd, sd->cmd, sd->cmd_len);
as.flags = 2 | 1;
if (sd->direction > 0)
as.flags &= ~1;
as.sense_len = 32;
as.cmd_len = sd->cmd_len;
as.data = sd->buffer;
as.len = sd->direction < 0 ? DEVICE_SCSI_BUFSIZE : sd->data_len;
sys_command_scsi_direct_native(sd->nativescsiunit, -1, &as);
sd->status = as.status;
sd->data_len = as.len;
if (sd->status) {
sd->direction = 0;
sd->data_len = 0;
memcpy(sd->sense, as.sensedata, as.sense_len);
}
}
sd->offset = 0;
}
static void allocscsibuf(struct scsi_data *sd)
{
sd->buffer_size = SCSI_DEFAULT_DATA_BUFFER_SIZE;
sd->buffer = xcalloc(uae_u8, sd->buffer_size);
}
struct scsi_data *scsi_alloc_generic(struct hardfiledata *hfd, int type, int uae_unitnum)
{
struct scsi_data *sd = xcalloc(struct scsi_data, 1);
sd->hfd = hfd;
sd->id = -1;
sd->nativescsiunit = -1;
sd->cd_emu_unit = -1;
sd->blocksize = hfd->ci.blocksize;
sd->device_type = type;
sd->uae_unitnum = uae_unitnum;
allocscsibuf(sd);
return sd;
}
struct scsi_data *scsi_alloc_hd(int id, struct hd_hardfiledata *hfd, int uae_unitnum)
{
struct scsi_data *sd = xcalloc (struct scsi_data, 1);
sd->hdhfd = hfd;
sd->hfd = &hfd->hfd;
sd->id = id;
sd->nativescsiunit = -1;
sd->cd_emu_unit = -1;
sd->blocksize = hfd->hfd.virtual_rdb ? 512 : hfd->hfd.ci.blocksize;
sd->device_type = UAEDEV_HDF;
sd->uae_unitnum = uae_unitnum;
allocscsibuf(sd);
return sd;
}
struct scsi_data *scsi_alloc_cd(int id, int unitnum, bool atapi, int uae_unitnum)
{
struct scsi_data *sd;
if (!sys_command_open (unitnum)) {
write_log (_T("SCSI: CD EMU scsi unit %d failed to open\n"), unitnum);
return NULL;
}
sd = xcalloc (struct scsi_data, 1);
sd->id = id;
sd->cd_emu_unit = unitnum;
sd->nativescsiunit = -1;
sd->atapi = atapi;
sd->blocksize = 2048;
sd->device_type = UAEDEV_CD;
sd->uae_unitnum = uae_unitnum;
allocscsibuf(sd);
return sd;
}
struct scsi_data *scsi_alloc_tape(int id, const TCHAR *tape_directory, bool readonly, int uae_unitnum)
{
struct scsi_data_tape *tape;
tape = tape_alloc (id, tape_directory, readonly);
if (!tape)
return NULL;
struct scsi_data *sd = xcalloc (struct scsi_data, 1);
sd->id = id;
sd->nativescsiunit = -1;
sd->cd_emu_unit = -1;
sd->blocksize = tape->blocksize;
sd->tape = tape;
sd->device_type = UAEDEV_TAPE;
sd->uae_unitnum = uae_unitnum;
allocscsibuf(sd);
return sd;
}
struct scsi_data *scsi_alloc_native(int id, int nativeunit)
{
struct scsi_data *sd;
if (!sys_command_open (nativeunit)) {
write_log (_T("SCSI: native scsi unit %d failed to open\n"), nativeunit);
return NULL;
}
sd = xcalloc (struct scsi_data, 1);
sd->id = id;
sd->nativescsiunit = nativeunit;
sd->cd_emu_unit = -1;
sd->blocksize = 2048;
sd->device_type = 0;
allocscsibuf(sd);
return sd;
}
void scsi_reset(void)
{
//device_func_init (DEVICE_TYPE_SCSI);
}
void scsi_free(struct scsi_data *sd)
{
if (!sd)
return;
if (sd->nativescsiunit >= 0) {
sys_command_close (sd->nativescsiunit);
sd->nativescsiunit = -1;
}
if (sd->cd_emu_unit >= 0) {
sys_command_close (sd->cd_emu_unit);
sd->cd_emu_unit = -1;
}
tape_free (sd->tape);
xfree(sd->buffer);
xfree(sd);
}
void scsi_start_transfer(struct scsi_data *sd)
{
sd->offset = 0;
}
int scsi_send_data(struct scsi_data *sd, uae_u8 b)
{
if (sd->offset < 0) {
write_log(_T("SCSI data offset is negative!\n"));
return 0;
}
if (sd->direction == 1) {
if (sd->offset >= sd->buffer_size) {
write_log (_T("SCSI data buffer overflow!\n"));
return 0;
}
sd->buffer[sd->offset++] = b;
} else if (sd->direction == 2) {
if (sd->offset >= 16) {
write_log (_T("SCSI command buffer overflow!\n"));
return 0;
}
sd->cmd[sd->offset++] = b;
if (sd->offset == sd->cmd_len)
return 1;
} else {
write_log (_T("scsi_send_data() without direction! (%02X)\n"), sd->cmd[0]);
return 0;
}
if (sd->offset == sd->data_len)
return 1;
return 0;
}
int scsi_receive_data(struct scsi_data *sd, uae_u8 *b, bool next)
{
if (!sd->data_len)
return -1;
*b = sd->buffer[sd->offset];
if (next) {
sd->offset++;
if (sd->offset == sd->data_len)
return 1; // requested length got
}
return 0;
}
void free_scsi (struct scsi_data *sd)
{
if (!sd)
return;
hdf_hd_close(sd->hdhfd);
scsi_free (sd);
}
int add_scsi_hd (struct scsi_data **sd, int ch, struct hd_hardfiledata *hfd, struct uaedev_config_info *ci)
{
free_scsi (*sd);
*sd = NULL;
if (!hfd) {
hfd = xcalloc (struct hd_hardfiledata, 1);
memcpy (&hfd->hfd.ci, ci, sizeof (struct uaedev_config_info));
}
if (!hdf_hd_open (hfd))
return 0;
hfd->ansi_version = ci->unit_feature_level + 1;
*sd = scsi_alloc_hd (ch, hfd, ci->uae_unitnum);
return *sd ? 1 : 0;
}
int add_scsi_cd (struct scsi_data **sd, int ch, int unitnum)
{
device_func_init (0);
free_scsi (*sd);
*sd = scsi_alloc_cd (ch, unitnum, false, unitnum);
return *sd ? 1 : 0;
}
int add_scsi_tape (struct scsi_data **sd, int ch, const TCHAR *tape_directory, bool readonly)
{
free_scsi (*sd);
*sd = scsi_alloc_tape (ch, tape_directory, readonly, ch);
return *sd ? 1 : 0;
}
int add_scsi_device(struct scsi_data **sd, int ch, struct uaedev_config_info *ci, struct romconfig *rc)
{
if (ci->type == UAEDEV_CD)
return add_scsi_cd(sd, ch, ci->device_emu_unit);
else if (ci->type == UAEDEV_TAPE)
return add_scsi_tape(sd, ch, ci->rootdir, ci->readonly);
else if (ci->type == UAEDEV_HDF)
return add_scsi_hd(sd, ch, NULL, ci);
return 0;
}
void scsi_freenative(struct scsi_data **sd, int max)
{
for (int i = 0; i < max; i++) {
free_scsi (sd[i]);
sd[i] = NULL;
}
}
void scsi_addnative(struct scsi_data **sd)
{
int i, j;
int devices[MAX_TOTAL_SCSI_DEVICES];
int types[MAX_TOTAL_SCSI_DEVICES];
struct device_info dis[MAX_TOTAL_SCSI_DEVICES];
scsi_freenative (sd, MAX_TOTAL_SCSI_DEVICES);
i = 0;
while (i < MAX_TOTAL_SCSI_DEVICES) {
types[i] = -1;
devices[i] = -1;
if (sys_command_open (i)) {
if (sys_command_info (i, &dis[i], 0)) {
devices[i] = i;
types[i] = 100 - i;
if (dis[i].type == INQ_ROMD)
types[i] = 1000 - i;
}
sys_command_close (i);
}
i++;
}
i = 0;
while (devices[i] >= 0) {
j = i + 1;
while (devices[j] >= 0) {
if (types[i] > types[j]) {
int tmp = types[i];
types[i] = types[j];
types[j] = tmp;
}
j++;
}
i++;
}
i = 0; j = 0;
while (devices[i] >= 0 && j < 7) {
if (sd[j] == NULL) {
sd[j] = scsi_alloc_native(j, devices[i]);
write_log (_T("SCSI: %d:'%s'\n"), j, dis[i].label);
i++;
}
j++;
}
}
// raw scsi
#define SCSI_IO_BUSY 0x80
#define SCSI_IO_ATN 0x40
#define SCSI_IO_SEL 0x20
#define SCSI_IO_REQ 0x10
#define SCSI_IO_DIRECTION 0x01
#define SCSI_IO_COMMAND 0x02
#define SCSI_IO_MESSAGE 0x04
#define SCSI_SIGNAL_PHASE_FREE -1
#define SCSI_SIGNAL_PHASE_ARBIT -2
#define SCSI_SIGNAL_PHASE_SELECT_1 -3
#define SCSI_SIGNAL_PHASE_SELECT_2 -4
#define SCSI_SIGNAL_PHASE_DATA_OUT 0
#define SCSI_SIGNAL_PHASE_DATA_IN 1
#define SCSI_SIGNAL_PHASE_COMMAND 2
#define SCSI_SIGNAL_PHASE_STATUS 3
#define SCSI_SIGNAL_PHASE_MESSAGE_OUT 6
#define SCSI_SIGNAL_PHASE_MESSAGE_IN 7
struct raw_scsi
{
int io;
int bus_phase;
bool atn;
bool ack;
bool wait_ack;
uae_u8 data_write;
uae_u8 status;
bool databusoutput;
int initiator_id, target_id;
struct scsi_data *device[MAX_TOTAL_SCSI_DEVICES];
struct scsi_data *target;
int msglun;
};
struct soft_scsi
{
uae_u8 regs[32];
uae_u16 regs_400[2];
uae_u8 scratch_400[64];
int c400_count;
bool c400;
bool dp8490v;
uae_u8 aic_reg;
struct raw_scsi rscsi;
bool irq;
bool intena;
bool level6;
bool enabled;
bool delayed_irq;
bool configured;
uae_u8 acmemory[128];
uaecptr baseaddress;
uaecptr baseaddress2;
uae_u8 *rom;
int rom_size;
int board_mask;
int board_mask2;
int board_size;
addrbank *bank;
int type;
int subtype;
int dma_direction;
bool dma_active;
bool dma_started;
bool dma_controller;
bool dma_drq;
bool dma_autodack;
uae_u32 dma_mask;
struct romconfig *rc;
struct soft_scsi **self_ptr;
int dmac_direction;
uaecptr dmac_address;
int dmac_length;
int dmac_active;
int chip_state;
// add500
uae_u16 databuffer[2];
bool databuffer_empty;
// kronos/xebec
uae_u8 *databufferptr;
int databuffer_size;
int db_read_index;
int db_write_index;
void *eeprom;
// sasi
bool active_select;
bool wait_select;
// 12 Gauge needs this (Driver has buggy BSY test)
bool busy_delayed_hack;
int busy_delayed_hack_cnt;
};
#define MAX_SOFT_SCSI_UNITS 10
static struct soft_scsi *soft_scsi_devices[MAX_SOFT_SCSI_UNITS];
static struct soft_scsi *soft_scsi_units[NCR_LAST * MAX_DUPLICATE_EXPANSION_BOARDS];
bool parallel_port_scsi;
static struct soft_scsi *parallel_port_scsi_data;
static struct soft_scsi *x86_hd_data;
static void soft_scsi_free_unit(struct soft_scsi *s)
{
if (!s)
return;
struct raw_scsi *rs = &s->rscsi;
for (int j = 0; j < 8; j++) {
free_scsi (rs->device[j]);
rs->device[j] = NULL;
}
eeprom93xx_free(s->eeprom);
xfree(s->databufferptr);
xfree(s->rom);
if (s->self_ptr)
*s->self_ptr = NULL;
xfree(s);
}
static void freescsi(struct soft_scsi **ncr)
{
if (!ncr)
return;
for (int i = 0; i < MAX_SOFT_SCSI_UNITS; i++) {
if (soft_scsi_devices[i] == *ncr) {
soft_scsi_devices[i] = NULL;
}
}
if (*ncr) {
soft_scsi_free_unit(*ncr);
}
*ncr = NULL;
}
static struct soft_scsi *allocscsi(struct soft_scsi **ncr, struct romconfig *rc, int ch)
{
struct soft_scsi *scsi;
if (ch < 0) {
freescsi(ncr);
*ncr = NULL;
}
if ((*ncr) == NULL) {
scsi = xcalloc(struct soft_scsi, 1);
for (int i = 0; i < MAX_SOFT_SCSI_UNITS; i++) {
if (soft_scsi_devices[i] == NULL) {
soft_scsi_devices[i] = scsi;
rc->unitdata = scsi;
scsi->rc = rc;
scsi->self_ptr = ncr;
if (ncr)
*ncr = scsi;
return scsi;
}
}
}
return *ncr;
}
static struct soft_scsi *getscsiboard(uaecptr addr)
{
for (int i = 0; soft_scsi_devices[i]; i++) {
struct soft_scsi *s = soft_scsi_devices[i];
if (s->baseaddress2 && (addr & ~s->board_mask2) == s->baseaddress2)
return s;
if (!s->baseaddress && !s->configured)
return s;
if ((addr & ~s->board_mask) == s->baseaddress)
return s;
if (s->baseaddress == AUTOCONFIG_Z2 && addr < 65536)
return s;
}
return NULL;
}
static void raw_scsi_reset(struct raw_scsi *rs)
{
rs->target = NULL;
rs->io = 0;
rs->bus_phase = SCSI_SIGNAL_PHASE_FREE;
}
extern addrbank soft_bank_generic;
static void ew(struct soft_scsi *scsi, int addr, uae_u32 value)
{
addr &= 0xffff;
if (addr == 00 || addr == 02 || addr == 0x40 || addr == 0x42) {
scsi->acmemory[addr] = (value & 0xf0);
scsi->acmemory[addr + 2] = (value & 0x0f) << 4;
} else {
scsi->acmemory[addr] = ~(value & 0xf0);
scsi->acmemory[addr + 2] = ~((value & 0x0f) << 4);
}
}
static struct soft_scsi *generic_soft_scsi_add(int ch, struct uaedev_config_info *ci, struct romconfig *rc, int type, int boardsize, int romsize, int romtype)
{
struct soft_scsi *ss = allocscsi(&soft_scsi_units[type * MAX_DUPLICATE_EXPANSION_BOARDS + ci->controller_type_unit], rc, ch);
ss->type = type;
ss->configured = 0;
ss->bank = &soft_bank_generic;
ss->subtype = rc->subtype;
ss->intena = false;
ss->dma_mask = 0xffffffff;
if (boardsize > 0) {
ss->board_size = boardsize;