Small compatibility improvements, and added scsi2sd-monitor test program
[SCSI2SD-V6.git] / software / SCSI2SD / src / scsi.h
CommitLineData
75de1226
MM
1// Copyright (C) 2013 Michael McMaster <michael@codesrc.com>
2//
3// This file is part of SCSI2SD.
4//
5// SCSI2SD is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// SCSI2SD is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with SCSI2SD. If not, see <http://www.gnu.org/licenses/>.
17#ifndef SCSI_H
18#define SCSI_H
19
75de1226
MM
20#include "geometry.h"
21#include "sense.h"
22
23typedef enum
24{
25 // internal bits
26 __scsiphase_msg = 1,
27 __scsiphase_cd = 2,
28 __scsiphase_io = 4,
29
30 BUS_FREE = -1,
31 BUS_BUSY = -2,
32 ARBITRATION = -3,
33 SELECTION = -4,
34 RESELECTION = -5,
35 STATUS = __scsiphase_cd | __scsiphase_io,
36 COMMAND = __scsiphase_cd,
37 DATA_IN = __scsiphase_io,
38 DATA_OUT = 0,
39 MESSAGE_IN = __scsiphase_msg | __scsiphase_cd | __scsiphase_io,
40 MESSAGE_OUT = __scsiphase_msg | __scsiphase_cd
41} SCSI_PHASE;
42
43typedef enum
44{
45 GOOD = 0,
46 CHECK_CONDITION = 2,
47 BUSY = 0x8,
abe0a5f5 48 INTERMEDIATE = 0x10,
75de1226
MM
49 CONFLICT = 0x18
50} SCSI_STATUS;
51
52typedef enum
53{
54 MSG_COMMAND_COMPLETE = 0,
abe0a5f5
MM
55 MSG_REJECT = 0x7,
56 MSG_LINKED_COMMAND_COMPLETE = 0x0A,
57 MSG_LINKED_COMMAND_COMPLETE_WITH_FLAG = 0x0B
75de1226
MM
58} SCSI_MESSAGE;
59
9ad7cc15
MM
60typedef enum
61{
62 COMPAT_UNKNOWN,
63 COMPAT_SCSI1,
64 COMPAT_SCSI2
65} SCSI_COMPAT_MODE;
66
0bb5ed83 67// Maximum value for bytes-per-sector.
db9c3160 68#define MAX_SECTOR_SIZE 8192
0bb5ed83
MM
69#define MIN_SECTOR_SIZE 64
70
638c94ce
MM
71// Shadow parameters, possibly not saved to flash yet.
72// Set via Mode Select
73typedef struct
74{
75 uint16_t bytesPerSector;
76} LiveCfg;
77
75de1226
MM
78typedef struct
79{
b19a0a4c 80 uint8_t targetId;
638c94ce 81
b19a0a4c
MM
82 const TargetConfig* cfg;
83
638c94ce
MM
84 LiveCfg liveCfg;
85
b19a0a4c
MM
86 ScsiSense sense;
87
88 uint16 unitAttention; // Set to the sense qualifier key to be returned.
89
90 // Only let the reserved initiator talk to us.
91 // A 3rd party may be sending the RESERVE/RELEASE commands
92 int reservedId; // 0 -> 7 if reserved. -1 if not reserved.
93 int reserverId; // 0 -> 7 if reserved. -1 if not reserved.
94} TargetState;
95
96typedef struct
97{
98 TargetState targets[MAX_SCSI_TARGETS];
99 TargetState* target;
75de1226
MM
100
101 // Set to true (1) if the ATN flag was set, and we need to
102 // enter the MESSAGE_OUT phase.
cc6921e4 103 volatile int atnFlag;
75de1226
MM
104
105 // Set to true (1) if the RST flag was set.
cc6921e4 106 volatile int resetFlag;
75de1226
MM
107
108 // Set to true (1) if a parity error was observed.
109 int parityError;
110
111 int phase;
112
70257ca8 113 uint8 data[MAX_SECTOR_SIZE * 2];
75de1226
MM
114 int dataPtr; // Index into data, reset on [re]selection to savedDataPtr
115 int savedDataPtr; // Index into data, initially 0.
116 int dataLen;
117
118 uint8 cdb[12]; // command descriptor block
abe0a5f5 119 uint8 cdbLen; // 6, 10, or 12 byte message.
f45769ae 120 int8 lun; // Target lun, set by IDENTIFY message.
a8cd4216 121 uint8 discPriv; // Disconnect priviledge.
9ad7cc15 122 uint8_t compatMode; // SCSI_COMPAT_MODE
75de1226
MM
123
124 // Only let the reserved initiator talk to us.
125 // A 3rd party may be sending the RESERVE/RELEASE commands
126 int initiatorId; // 0 -> 7. Set during the selection phase.
75de1226
MM
127
128 // SCSI_STATUS value.
a8cd4216 129 // Change to CHECK_CONDITION when setting a SENSE value
75de1226
MM
130 uint8 status;
131
75de1226
MM
132 uint8 msgIn;
133 uint8 msgOut;
6c8cbf54 134
0bb5ed83
MM
135 void (*postDataOutHook)(void);
136
030fc25f 137 uint8 cmdCount;
6c8cbf54
MM
138 uint8 selCount;
139 uint8 rstCount;
140 uint8 msgCount;
141 uint8 watchdogTick;
142 uint8 lastStatus;
143 uint8 lastSense;
70257ca8 144 uint16_t lastSenseASC;
75de1226
MM
145} ScsiDevice;
146
147extern ScsiDevice scsiDev;
148
70257ca8
MM
149void process_Status(void);
150void process_MessageIn(void);
151
c8389e5f 152void scsiInit(void);
75de1226 153void scsiPoll(void);
a8cd4216
MM
154void scsiDisconnect(void);
155int scsiReconnect(void);
75de1226 156
95b51978
MM
157
158// Utility macros, consistent with the Linux Kernel code.
159#define likely(x) __builtin_expect(!!(x), 1)
160#define unlikely(x) __builtin_expect(!!(x), 0)
161//#define likely(x) (x)
162//#define unlikely(x) (x)
75de1226 163#endif