-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathatapi.c
1249 lines (1043 loc) · 36.1 KB
/
atapi.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
// SPDX-License-Identifier: GPL-2.0-only
/* This file is part of lide.device
* Copyright (C) 2023 Matthew Harlum <[email protected]>
*/
#include <devices/scsidisk.h>
#include <devices/timer.h>
#include <devices/trackdisk.h>
#include <inline/timer.h>
#include <proto/exec.h>
#include <proto/alib.h>
#include <proto/expansion.h>
#include <exec/errors.h>
#include <exec/execbase.h>
#include <stdbool.h>
#include "debug.h"
#include "device.h"
#include "ata.h"
#include "atapi.h"
#include "scsi.h"
#include "string.h"
#include "wait.h"
#include "blockcopy.h"
/**
* atapi_status_reg_delay
*
* We need a short delay before actually checking the status register to let the drive update the status
* Reading a CIA register should ensure a consistent delay regardless of CPU speed
* More info: https://wiki.osdev.org/ATA_PIO_Mode#400ns_delays
*
* @param unit Pointer to an IDEUnit struct
*/
static void __attribute__((always_inline)) atapi_status_reg_delay(struct IDEUnit *unit) {
asm volatile (
"tst.b 0xBFEB01"
);
}
/**
* atapi_wait_drq
*
* Poll DRQ in the status register until set or timeout
* @param unit Pointer to an IDEUnit struct
* @param tries Tries, sets the timeout
*/
static bool atapi_wait_drq(struct IDEUnit *unit, ULONG tries) {
Trace("atapi_wait_drq enter\n");
struct timerequest *tr = unit->itask->tr;
atapi_status_reg_delay(unit);
for (int i=0; i < tries; i++) {
if ((*unit->drive.status_command & ata_flag_drq) != 0) return true;
wait_us(tr,ATAPI_DRQ_WAIT_LOOP_US);
}
Trace("atapi_wait_drq timeout\n");
return false;
}
/**
* atapi_wait_drq_not_bsy
*
* Poll the status register until DRQ set and BSY clear or timeout
* @param unit Pointer to an IDEUnit struct
* @param tries Tries, sets the timeout
*/
static bool atapi_wait_drq_not_bsy(struct IDEUnit *unit, ULONG tries) {
struct timerequest *tr = unit->itask->tr;
atapi_status_reg_delay(unit);
for (int i=0; i < tries; i++) {
if ((*unit->drive.status_command & (ata_flag_busy | ata_flag_drq)) == ata_flag_drq) return true;
wait_us(tr,ATAPI_DRQ_WAIT_LOOP_US);
}
return false;
}
/**
* atapi_wait_not_bsy
*
* Poll BSY in the status register until clear or timeout
* @param unit Pointer to an IDEUnit struct
* @param tries Tries, sets the timeout
*/
static bool atapi_wait_not_bsy(struct IDEUnit *unit, ULONG tries) {
Trace("atapi_wait_not_bsy enter\n");
struct timerequest *tr = unit->itask->tr;
for (int i=0; i < tries; i++) {
if ((*unit->drive.status_command & ata_flag_busy) == 0) return true;
wait_us(tr,ATAPI_BSY_WAIT_LOOP_US);
}
Trace("atapi_wait_not_bsy timeout\n");
return false;
}
/**
* atapi_wait_not_drqbsy
*
* Poll DRQ & BSY in the status register until clear or timeout
* @param unit Pointer to an IDEUnit struct
* @param tries Tries, sets the timeout
*/
static bool atapi_wait_not_drqbsy(struct IDEUnit *unit, ULONG tries) {
Trace("atapi_wait_not_drqbsy enter\n");
struct timerequest *tr = unit->itask->tr;
atapi_status_reg_delay(unit);
for (int i=0; i < tries; i++) {
if ((*(volatile BYTE *)unit->drive.status_command & (ata_flag_busy | ata_flag_drq)) == 0) return true;
wait_us(tr,ATAPI_BSY_WAIT_LOOP_US);
}
Trace("atapi_wait_not_drqbsy timeout\n");
return false;
}
/**
* atapi_check_ir
*
* Mask the Interrupt Reason register and check against a value
*
* @param unit Pointer to an IDEUnit struct
* @param mask Mask
* @param value Expected value after masking
* @param tries Maximum attempts to find the expected value
* @returns True if value matches
*/
static bool atapi_check_ir(struct IDEUnit *unit, UBYTE mask, UBYTE value, UWORD tries) {
for (int i=0; i<tries; i++) {
atapi_status_reg_delay(unit);
if ((*unit->drive.sectorCount & mask) == value) return true;
}
return false;
}
/**
* atapi_dev_reset
*
* Resets the device by sending a DEVICE RESET command to it
* @param unit Pointer to an IDEUnit struct
*/
void atapi_dev_reset(struct IDEUnit *unit) {
Info("ATAPI: Resetting device\n");
atapi_wait_not_bsy(unit,10);
*unit->drive.status_command = ATA_CMD_DEVICE_RESET;
atapi_wait_not_bsy(unit,ATAPI_BSY_WAIT_COUNT);
}
/**
* atapi_check_signature
*
* Resets the device then checks the signature in the LBA High and Mid registers to see if an ATAPI device is present
* @param unit Pointer to an IDEUnit struct
*/
bool atapi_check_signature(struct IDEUnit *unit) {
atapi_dev_reset(unit);
wait_us(unit->itask->tr,10000);
for (int i=0; i<20; i++) {
if ((*unit->drive.lbaHigh == 0xEB) && (*unit->drive.lbaMid == 0x14)) return true;
}
return false;
}
/**
* atapi_identify
*
* Send an "IDENTIFY PACKET DEVICE" command and read the results into a buffer
*
* @param unit Pointer to an IDEUnit struct
* @param buffer Pointer to the destination buffer
* @returns True on success, false on failure
*/
bool atapi_identify(struct IDEUnit *unit, UWORD *buffer) {
UBYTE drvSel = (unit->primary) ? 0xE0 : 0xF0; // Select drive
// Only update the devHead register if absolutely necessary to save time
ata_select(unit,drvSel,false);
//if (!atapi_wait_rdy(unit,ATAPI_RDY_WAIT_COUNT))
// return HFERR_SelTimeout;
*unit->drive.sectorCount = 0;
*unit->drive.lbaLow = 0;
*unit->drive.lbaMid = 0;
*unit->drive.lbaHigh = 0;
*unit->drive.error_features = 0;
*unit->drive.status_command = ATAPI_CMD_IDENTIFY;
if (!atapi_wait_drq(unit,ATAPI_DRQ_WAIT_COUNT)) {
if (*unit->drive.status_command & (ata_flag_error | ata_flag_df)) {
Info("ATAPI: IDENTIFY Status: Error\n");
Info("ATAPI: last_error: %08lx\n",&unit->last_error[0]);
// Save the error details
unit->last_error[0] = *unit->drive.error_features;
unit->last_error[1] = *unit->drive.lbaHigh;
unit->last_error[2] = *unit->drive.lbaMid;
unit->last_error[3] = *unit->drive.lbaLow;
unit->last_error[4] = *unit->drive.status_command;
}
return false;
}
if (buffer) {
UWORD read_data;
for (int i=0; i<256; i++) {
read_data = *unit->drive.data;
buffer[i] = ((read_data >> 8) | (read_data << 8));
}
}
return true;
}
/**
* atapi_translate
*
* Translate TD commands to ATAPI and issue them to the device
*
* @param io_Data Pointer to the data buffer
* @param lba LBA to transfer
* @param count Number of LBAs to transfer
* @param io_Actual Pointer to the io_Actual field of the ioreq
* @param unit Pointer to the IDE Unit
* @param direction Transfer direction
* @returns error
*/
BYTE atapi_translate(APTR io_Data, ULONG lba, ULONG count, ULONG *io_Actual, struct IDEUnit *unit, enum xfer_dir direction)
{
Trace("atapi_translate enter\n");
struct SCSICmd *cmd = MakeSCSICmd(SZ_CDB_10);
if (cmd == NULL) return TDERR_NoMem;
struct SCSI_CDB_10 *cdb = (struct SCSI_CDB_10 *)cmd->scsi_Command;
UBYTE errorCode = 0;
UBYTE senseKey = 0;
UBYTE asc = 0;
UBYTE asq = 0;
if (count == 0) {
return IOERR_BADLENGTH;
}
Trace("%ld lba %ld count\n %ld bs\n",lba,count,unit->blockShift);
BYTE err = 0;
BYTE ret = 0;
for (int tries = 4; tries > 0; tries--) {
cmd->scsi_CmdLength = sizeof(struct SCSI_CDB_10);
cmd->scsi_CmdActual = 0;
cmd->scsi_Flags = (direction == READ) ? SCSIF_READ : SCSIF_WRITE;
cmd->scsi_Data = io_Data;
cmd->scsi_Length = count * unit->blockSize;
cmd->scsi_Actual = 0;
cmd->scsi_SenseData = NULL;
cmd->scsi_SenseLength = 0;
cdb->operation = (direction == READ) ? SCSI_CMD_READ_10 : SCSI_CMD_WRITE_10;
cdb->control = 0;
cdb->flags = 0;
cdb->group = 0;
cdb->lba = lba;
cdb->length = (UWORD)count;
if ((err = atapi_packet(cmd,unit)) == 0) {
goto done;
} else {
if (cmd->scsi_Status == 2) {
// Unit reported CHECK STATUS
// Request the sense data
if ((ret = atapi_request_sense(unit,&errorCode,&senseKey,&asc,&asq)) != 0) {
// Got an error even trying to get the sense data :/
goto done;
}
switch (senseKey) {
case 0x01: // Recovered error
ret = 0;
goto done;
case 0x02: // Unit not ready
if (asc == 0x4) { // Becoming ready
ret = TDERR_DiskChanged;
wait(unit->itask->tr,1); // Wait
continue; // and try again
} else {
ret = TDERR_DiskChanged; // No media
atapi_update_presence(unit,false);
goto done;
}
case 0x06: // Media changed or unit completed reset
ret = err;
continue; // Try the command again
case 0x07: // Disk is write protected
ret = TDERR_WriteProt;
goto done;
default: // Anything else
ret = TDERR_NotSpecified;
continue; // Try again
}
} else {
// Command time outs / bad phase etc end up here
atapi_dev_reset(unit); // Reset the unit before trying again
continue;
}
}
}
done:
Trace("atapi_packet returns %ld\n",ret);
*io_Actual = cmd->scsi_Actual;
DeleteSCSICmd(cmd);
return ret;
}
#pragma GCC optimize ("-O3")
/**
* atapi_packet
*
* Send a SCSICmd to an ATAPI device
*
* @param cmd Pointer to a SCSICmd struct
* @param unit Pointer to the IDEUnit
* @returns error, sense key returned in SenseData
*/
BYTE atapi_packet(struct SCSICmd *cmd, struct IDEUnit *unit) {
Trace("atapi_packet\n");
LONG byte_count = 0;
LONG remaining;
UWORD data;
BYTE ret = 0;
UBYTE senseKey;
volatile UBYTE *status = unit->drive.status_command;
if (cmd->scsi_Length > 0 && cmd->scsi_Data == NULL) {
ret = IOERR_BADADDRESS;
goto end;
}
if (cmd->scsi_CmdLength > 12 || cmd->scsi_CmdLength < 6) {
ret = IOERR_BADADDRESS;
goto end;
}
cmd->scsi_Actual = 0;
UBYTE drvSelHead = ((unit->primary) ? 0xE0 : 0xF0);
// Only update the devHead register if absolutely necessary to save time
ata_select(unit,drvSelHead,true);
if (!atapi_wait_not_drqbsy(unit,ATAPI_BSY_WAIT_COUNT)) {
ret = IOERR_UNITBUSY;
goto end;
}
if (cmd->scsi_Length > 65534) {
byte_count = 65534;
} else {
byte_count = cmd->scsi_Length;
}
*unit->drive.lbaMid = byte_count & 0xFF;
*unit->drive.lbaHigh = byte_count >> 8 & 0xFF;
*unit->drive.error_features = 0;
*unit->drive.devHead = drvSelHead;
*unit->drive.status_command = ATAPI_CMD_PACKET;
if (!atapi_wait_drq_not_bsy(unit,ATAPI_DRQ_WAIT_COUNT)) {
Trace("ATAPI: Packet bsy timeout\n");
ret = IOERR_UNITBUSY;
goto end;
}
if (!atapi_check_ir(unit,IR_STATUS,IR_COMMAND,10)) {
Trace("ATAPI: Failed command phase\n");
ret = HFERR_Phase;
goto end;
}
for (int i=0; i < (cmd->scsi_CmdLength/2); i++)
{
data = *((UWORD *)cmd->scsi_Command + i);
*unit->drive.data = data;
Trace("ATAPI: CMD Word: %ld\n",data);
}
// ATAPI requires 12 command bytes transferred, pad out our 6 and 10 byte CDBs
if (cmd->scsi_CmdLength < 12)
{
for (int i = cmd->scsi_CmdLength; i < 12; i+=2) {
*unit->drive.data = 0x00;
Trace("ATAPI: CMD Word: 0\n");
}
}
if (*status & ata_flag_error) goto ata_error;
cmd->scsi_CmdActual = cmd->scsi_CmdLength;
ULONG index = 0;
while (1) {
atapi_status_reg_delay(unit);
if (!atapi_wait_not_bsy(unit,ATAPI_BSY_WAIT_COUNT)) {
ret = IOERR_UNITBUSY;
goto end;
}
if (cmd->scsi_Length == 0) break;
if ((atapi_check_ir(unit,0x03,IR_STATUS,1))) break;
if (!(atapi_wait_drq(unit,10))) break;
byte_count = *unit->drive.lbaHigh << 8 | *unit->drive.lbaMid;
byte_count += (byte_count & 0x01); // Ensure that the byte count is always an even number
while (byte_count > 0) {
remaining = cmd->scsi_Length - cmd->scsi_Actual;
if ((byte_count >= 512 && remaining >= 512)) {
// 512 or more bytes to transfer, use the fast ATA transfer routines
if (cmd->scsi_Flags & SCSIF_READ) {
unit->read_fast((void *)unit->drive.data, cmd->scsi_Data + index);
} else {
unit->write_fast(cmd->scsi_Data + index, (void *)unit->drive.data);
}
index += 256;
cmd->scsi_Actual += 512;
byte_count -= 512;
} else if (remaining > 0) {
// Less than 512 bytes means we can't use the fast ATA transfer routines, copy word-by-word
if (cmd->scsi_Flags & SCSIF_READ) {
cmd->scsi_Data[index] = *unit->drive.data;
} else {
*unit->drive.data = cmd->scsi_Data[index];
}
index++;
cmd->scsi_Actual+=2;
byte_count -= 2;
} else {
// If we got here the drive wanted to transfer more data than the buffer could take
//
// Make the drive happy by reading/writing some more...
if (cmd->scsi_Flags & SCSIF_READ) {
*unit->drive.data;
} else {
*unit->drive.data = 0;
}
byte_count -= 2;
}
}
}
if (unit->SysBase->SoftVer > 36) {
CacheClearE(cmd->scsi_Data,cmd->scsi_Length,CACRF_ClearI);
}
atapi_wait_not_bsy(unit,ATAPI_BSY_WAIT_COUNT);
if (!atapi_check_ir(unit,atapi_flag_cd,IR_COMMAND,10)) {
// Drive is still in the data phase but should be either reporting completion or ready for a command
Warn("ATAPI: Completion not reported at end of command\n");
ret = HFERR_Phase;
goto ata_error;
}
end:
if (*status & ata_flag_error) {
ata_error:
unit->last_error[0] = *unit->drive.error_features;
unit->last_error[1] = *unit->drive.status_command;
unit->last_error[2] = *unit->drive.sectorCount;
senseKey = *unit->drive.error_features >> 4;
Warn("ATAPI ERROR!\n");
Warn("Sense Key: %02lx\n",senseKey);
Warn("Error: %02lx\n",*unit->drive.error_features);
Warn("Status: %02lx\n",*status);
Warn("Interrupt reason: %02lx\n",*unit->drive.sectorCount);
cmd->scsi_Status = 2;
if (ret == 0) ret = HFERR_BadStatus;
}
if (ret == 0) {
atapi_do_defer_tur(unit,cmd->scsi_Command[0]);
}
Trace("exit atapi_packet\n");
return ret;
}
#pragma GCC reset_options
/**
* atapi_test_unit_ready
*
* Send a TEST UNIT READY to the unit and update the media change count & presence
*
* @param unit Pointer to an IDEUnit struct
* @returns nonzero if there was an error
*/
BYTE atapi_test_unit_ready(struct IDEUnit *unit) {
struct SCSICmd *cmd = MakeSCSICmd(SZ_CDB_10);
if (cmd == NULL) return TDERR_NoMem;
struct SCSI_CDB_10 *cdb = (struct SCSI_CDB_10 *)cmd->scsi_Command;
UBYTE senseError, senseKey, asc, asq = 0;
UBYTE ret = 0;
for (int tries = 4; tries > 0; tries--) {
cdb->operation = SCSI_CMD_TEST_UNIT_READY;
cmd->scsi_Command = (UBYTE *)cdb;
cmd->scsi_CmdLength = sizeof(struct SCSI_CDB_10);
cmd->scsi_Length = 0;
cmd->scsi_Data = NULL;
cmd->scsi_Flags = SCSIF_READ;
ret = atapi_packet(cmd,unit);
if (ret == 0) {
break;
} else {
if ((ret = atapi_request_sense(unit,&senseError,&senseKey,&asc,&asq)) == 0) {
Trace("SenseKey: %lx ASC: %lx ASQ: %lx\n",senseKey,asc,asq);
switch (senseKey) {
case 0x02: // Not ready
if (asc == 4) { // Becoming ready
// The medium is becoming ready, wait a few seconds before checking again
ret = TDERR_DiskChanged;
if (tries > 0) wait(unit->itask->tr,3);
} else { // Anything else - No medium/bad medium etc
ret = TDERR_DiskChanged;
goto done;
}
break;
case 0x06: // Unit attention
if (asc == 0x28) { // Medium became ready
ret = 0;
}
break;
case 0x03: // Medium error
ret = TDERR_DiskChanged;
break;
default:
// Anything else, could be a timeout/bad phase etc
// Reset the unit and try again
//atapi_dev_reset(unit);
ret = TDERR_NotSpecified;
break;
}
} else {
atapi_dev_reset(unit);
break;
}
}
}
done:
atapi_update_presence(unit,(ret == 0)); // Update the media presence
DeleteSCSICmd(cmd);
return ret;
}
/**
* atapi_request_sense
*
* Request extended sense data from the ATAPI device
*
* @param unit Pointer to an IDEUnit struct
* @param senseKey Pointer for the senseKey result
* @param asc Pointer for the asc result
* @param asq Pointer for the asq result
* @return non-zero on error
*/
BYTE atapi_request_sense(struct IDEUnit *unit, UBYTE *errorCode, UBYTE *senseKey, UBYTE *asc, UBYTE *asq) {
struct SCSICmd *cmd = MakeSCSICmd(SZ_CDB_10);
if (cmd == NULL) return TDERR_NoMem;
UBYTE *cdb = (UBYTE *)cmd->scsi_Command;
UBYTE *buf;
if ((buf = AllocMem(18,MEMF_CLEAR|MEMF_ANY)) == NULL) {
DeleteSCSICmd(cmd);
return TDERR_NoMem;
}
UBYTE ret;
cdb[0] = SCSI_CMD_REQUEST_SENSE;
cdb[4] = 18;
cmd->scsi_Command = (UBYTE *)cdb;
cmd->scsi_CmdLength = sizeof(struct SCSI_CDB_10);
cmd->scsi_Length = 18;
cmd->scsi_Data = (UWORD *)buf;
cmd->scsi_Flags = SCSIF_READ;
ret = atapi_packet(cmd,unit);
Trace("ATAPI RS: Status %lx\n",ret);
*errorCode = buf[0];
*senseKey = buf[2] & 0x0F;
*asc = buf[12];
*asq = buf[13];
DeleteSCSICmd(cmd);
if (buf) FreeMem(buf,18);
return ret;
}
/**
* atapi_get_capacity
*
* Send a READ CAPACITY (10) to the ATAPI device then update the unit geometry with the returned values
*
* @param unit Pointer to an IDEUnit struct
* @return non-zero on error
*/
BYTE atapi_get_capacity(struct IDEUnit *unit) {
struct SCSICmd *cmd = MakeSCSICmd(SZ_CDB_10);
if (cmd == NULL) return TDERR_NoMem;
struct SCSI_CDB_10 *cdb = (struct SCSI_CDB_10 *)cmd->scsi_Command;
BYTE ret;
struct {
ULONG logicalSectors;
ULONG blockSize;
} capacity;
cdb->operation = SCSI_CMD_READ_CAPACITY_10;
cmd->scsi_CmdLength = sizeof(struct SCSI_CDB_10);
cmd->scsi_CmdActual = 0;
cmd->scsi_Command = (UBYTE *)cdb;
cmd->scsi_Flags = SCSIF_READ;
cmd->scsi_Data = (UWORD *)&capacity;
cmd->scsi_Length = 8;
cmd->scsi_SenseData = NULL;
unit->cylinders = 0;
unit->heads = 0;
unit->sectorsPerTrack = 0;
unit->logicalSectors = 0;
unit->blockShift = 0;
if ((ret = atapi_packet(cmd,unit)) == 0) {
unit->logicalSectors = capacity.logicalSectors + 1;
unit->blockSize = capacity.blockSize;
while ((unit->blockSize >> unit->blockShift) > 1) {
unit->blockShift++;
}
}
Trace("New geometry: %ld %ld\n",unit->logicalSectors, unit->blockSize);
DeleteSCSICmd(cmd);
return ret;
}
/**
* atapi_mode_sense
*
* Send a MODE SENSE (10) request to the ATAPI device
*
* @param unit Pointer to an IDEUnit struct
* @param page_code Page code to request
* @param buffer Pointer to a buffer for the mode sense data response
* @param length Size of the buffer
* @param actual Pointer to the actual byte count returned
* @return Non-zero on error
*/
BYTE atapi_mode_sense(struct IDEUnit *unit, BYTE page_code, BYTE subpage_code, UWORD *buffer, UWORD length, UWORD *actual, BOOL dbd) {
struct SCSICmd *cmd = MakeSCSICmd(SZ_CDB_10);
if (cmd == NULL) return TDERR_NoMem;
UBYTE *cdb = cmd->scsi_Command;
BYTE ret;
cdb[0] = SCSI_CMD_MODE_SENSE_10;
cdb[1] = (dbd ? 1 : 0) << 3;
cdb[2] = page_code;
cdb[3] = subpage_code;
cdb[7] = length >> 8;
cdb[8] = length & 0xFF;
cmd->scsi_CmdLength = sizeof(struct SCSI_CDB_10);
cmd->scsi_CmdActual = 0;
cmd->scsi_Command = (UBYTE *)cdb;
cmd->scsi_Flags = SCSIF_READ;
cmd->scsi_Data = buffer;
cmd->scsi_Length = length;
cmd->scsi_SenseData = NULL;
ret = atapi_packet(cmd,unit);
if (actual) *actual = cmd->scsi_Actual;
DeleteSCSICmd(cmd);
return ret;
}
/**
* atapi_scsi_mode_sense_6
*
* ATAPI devices do not support MODE SENSE (6) so translate to a MODE SENSE (10)
*
* @param cmd Pointer to a SCSICmd struct containing a MODE SENSE (6) request
* @param unit Pointer to an IDEUnit struct
* @returns non-zero on error, mode-sense data in cmd->scsi_Data
*/
BYTE atapi_scsi_mode_sense_6(struct SCSICmd *cmd, struct IDEUnit *unit) {
if (cmd->scsi_Data == NULL || cmd->scsi_Length == 0) return IOERR_BADADDRESS;
BYTE ret;
UBYTE *buf = NULL;
UBYTE *dest = (UBYTE *)cmd->scsi_Data;
ULONG len = cmd->scsi_Command[4] + 4; // Original allocation length
struct SCSICmd *cmd_sense = NULL;
if ((buf = AllocMem(len,MEMF_ANY|MEMF_CLEAR)) == NULL) {
return TDERR_NoMem;
}
cmd_sense = MakeSCSICmd(SZ_CDB_10);
if (cmd_sense == NULL) {
return TDERR_NoMem;
}
cmd_sense->scsi_Command[0] = SCSI_CMD_MODE_SENSE_10;
cmd_sense->scsi_Command[1] = cmd->scsi_Command[1]; // DBD flag
cmd_sense->scsi_Command[2] = cmd->scsi_Command[2]; // Page Code
cmd_sense->scsi_Command[3] = cmd->scsi_Command[3]; // Subpage Code
cmd_sense->scsi_Command[7] = (len >> 8) & 0xFF; // Allocation Length
cmd_sense->scsi_Command[8] = len; // Allocation Length
cmd_sense->scsi_Data = (UWORD *)buf;
cmd_sense->scsi_Length = len;
cmd_sense->scsi_Flags = SCSIF_READ;
ret = atapi_packet(cmd_sense,unit);
if (ret == 0 && cmd_sense->scsi_Status == 0) {
// Translate the mode parameter header
dest[0] = (buf[1] - 3); // Length
dest[1] = buf[2]; // Medium type
dest[2] = buf[3]; // WP/DPOFUA Flags
dest[3] = buf[7]; // Block descriptor length
// Copy the mode sense data
for (int i = 0; i < (cmd_sense->scsi_Actual - 8); i++) {
dest[i+4] = buf[i+8];
}
cmd->scsi_Actual = cmd_sense->scsi_Actual - 4;
cmd->scsi_Status = 0;
} else {
cmd->scsi_Status = 2;
}
cmd->scsi_CmdActual = cmd->scsi_CmdLength;
cmd->scsi_SenseActual = cmd_sense->scsi_SenseActual;
FreeMem(buf,len);
DeleteSCSICmd(cmd_sense);
return ret;
}
/**
* atapi_scsi_mode_select_6
*
* ATAPI devices do not support MODE SELECT (6) so translate to a MODE SELECT (10)
*
* @param cmd Pointer to a SCSICmd struct containing a MODE SENSE (6) request
* @param unit Pointer to an IDEUnit struct
* @returns non-zero on error, mode-sense data in cmd->scsi_Data
*/
BYTE atapi_scsi_mode_select_6(struct SCSICmd *cmd, struct IDEUnit *unit) {
BYTE ret;
UBYTE *buf = NULL;
UBYTE *src = NULL;
UBYTE *dst = NULL;
ULONG len = 0;
struct SCSICmd *cmd_select = NULL;
if (cmd->scsi_Data == NULL || cmd->scsi_Length == 0) return IOERR_BADADDRESS;
ULONG bufSize = cmd->scsi_Command[4] + 4;
if ((buf = AllocMem(bufSize,MEMF_ANY|MEMF_CLEAR)) == NULL) {
return TDERR_NoMem;
}
src = (UBYTE *)cmd->scsi_Data;
dst = buf;
cmd_select = MakeSCSICmd(SZ_CDB_10);
if (cmd_select == NULL) {
return TDERR_NoMem;
}
cmd_select->scsi_Command[0] = SCSI_CMD_MODE_SELECT_10;
cmd_select->scsi_Command[1] = cmd->scsi_Command[1]; // PF / SP
cmd_select->scsi_Command[7] = (bufSize >> 8) & 0xFF; // Parameter list length
cmd_select->scsi_Command[8] = bufSize; // Parameter list length
cmd_select->scsi_Data = (UWORD *)buf;
cmd_select->scsi_Length = bufSize;
cmd_select->scsi_Flags = cmd->scsi_Flags;
cmd_select->scsi_SenseData = cmd->scsi_SenseData;
cmd_select->scsi_SenseLength = cmd->scsi_SenseLength;
// Copy the Mode Parameters
dst += 4;
len = bufSize - 4;
CopyMem(src,dst,len);
ret = atapi_packet(cmd_select, unit);
if (ret == 0 && cmd_select->scsi_Status == 0) {
cmd->scsi_Status = 0;
} else {
cmd->scsi_Status = 2;
}
cmd->scsi_SenseActual = cmd_select->scsi_SenseActual;
cmd->scsi_CmdActual = cmd->scsi_CmdLength;
cmd->scsi_Actual = cmd_select->scsi_Actual;
DeleteSCSICmd(cmd_select);
FreeMem(buf,bufSize);
return ret;
}
/**
* atapi_scsi_read_write_6
*
* ATAPI devices do not support READ (6) or WRITE (6)
* Translate these calls to READ (10) / WRITE (10);
*
* @param cmd Pointer to a SCSICmd struct
* @param unit Pointer to an IDEUnit struct
* @returns non-zero on error
*/
BYTE atapi_scsi_read_write_6 (struct SCSICmd *cmd, struct IDEUnit *unit) {
BYTE ret;
struct SCSI_CDB_10 *cdb = AllocMem(sizeof(struct SCSI_CDB_10),MEMF_ANY|MEMF_CLEAR);
if (!cdb) return TDERR_NoMem;
struct SCSI_CDB_6 *oldcdb = (struct SCSI_CDB_6 *)cmd->scsi_Command;
cdb->operation = oldcdb->operation | 0x20;
cdb->length = oldcdb->length;
cdb->lba = oldcdb->lba_high << 16 |
oldcdb->lba_mid << 8 |
oldcdb->lba_low;
if (cdb->length == 0) cdb->length = 256; // for SCSI READ/WRITE 6 a transfer length of 0 specifies that 256 blocks will be transferred
cmd->scsi_Command = (BYTE *)cdb;
if (!((ULONG)cmd->scsi_Data & 0x01)) { // Buffer is word-aligned?
ret = atapi_packet(cmd,unit);
} else {
ret = atapi_packet_unaligned(cmd,unit);
}
FreeMem(cdb,sizeof(struct SCSI_CDB_10));
cmd->scsi_Command = (BYTE *)oldcdb;
return ret;
}
/**
* atapi_packet_unaligned
*
* In the unlikely event that someone has allocated an unaligned data buffer, align the data first by making a copy
*
* @param cmd Pointer to a SCSICmd struct
* @param unit Pointer to an IDEUnit struct
* @returns non-zero on exit
*/
BYTE atapi_packet_unaligned(struct SCSICmd *cmd, struct IDEUnit *unit) {
BYTE error = 0;
// Some bozo with an unaligned data buffer... (lookin' at you HDToolbox!)
// Allocate an aligned buffer and CopyMem to / from this one
UWORD *orig_buffer = cmd->scsi_Data;
if ((cmd->scsi_Data = AllocMem(cmd->scsi_Length,MEMF_CLEAR|MEMF_ANY)) == NULL) {
cmd->scsi_Data = orig_buffer;
error = TDERR_NoMem;
return error;
}
if (cmd->scsi_Flags & SCSIF_READ) {
error = atapi_packet(cmd,unit);
CopyMem(cmd->scsi_Data,orig_buffer,cmd->scsi_Length);
} else {
CopyMem(orig_buffer,cmd->scsi_Data,cmd->scsi_Length);
error = atapi_packet(cmd,unit);
}
FreeMem(cmd->scsi_Data,cmd->scsi_Length);
cmd->scsi_Data = orig_buffer;
return error;
}
/**
* atapi_start_stop_unit
*
* send START STOP command to ATAPI drive e.g to eject the disc
*
* @param unit Pointer to an IDEUnit struct
* @param start Start bit of START STOP
* @param loej loej bit of START STOP
* @returns non-zero on error
*/
BYTE atapi_start_stop_unit(struct IDEUnit *unit, bool start, bool loej) {
struct SCSICmd *cmd = NULL;
UBYTE operation = 0;
UBYTE ret;
if (loej) operation |= (1<<1);
if (start) operation |= (1<<0);
if ((cmd = MakeSCSICmd(SZ_CDB_10)) == NULL) return TDERR_NoMem;
cmd->scsi_Command[0] = SCSI_CMD_START_STOP_UNIT;
cmd->scsi_Command[1] = (1<<0); // Immediate bit set
cmd->scsi_Command[4] = operation;
ret = atapi_packet(cmd,unit);
DeleteSCSICmd(cmd);
return ret;
}
/**
* atapi_check_wp
*
* Check write-protect status of the disk
*
* @param unit Pointer to an IDEUnit struct
* @returns non-zero on error
*/
BYTE atapi_check_wp(struct IDEUnit *unit) {
UBYTE ret = 0;
UBYTE *buf = NULL;
if ((buf = AllocMem(512,MEMF_ANY|MEMF_CLEAR)) == NULL) return TDERR_NoMem;
UWORD actual = 0;
if ((ret = atapi_mode_sense(unit,0x3F,0,(UWORD *)buf,512,&actual,false)) == 0) {
if (buf[3] & 1<<7)
ret = TDERR_WriteProt;
}
if (buf) FreeMem(buf,512);
return ret;
}
/**
* atapi_update_presence
*
* If the medium has changed state update the unit info, geometry etc
* @param unit Pointer to an IDEUnit struct
* @param present Medium present
* @returns bool true if changed
*/
bool atapi_update_presence(struct IDEUnit *unit, bool present) {
bool ret = false;
if (present && unit->mediumPresent == false) {
unit->changeCount++;
unit->mediumPresent = true;
atapi_get_capacity(unit);
ret = true;
} else if (!present && unit->mediumPresent == true) {