Cleanup
[SCSI2SD.git] / software / include / scsi2sd.h
1 //      Copyright (C) 2014 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 scsi2sd_h
18 #define scsi2sd_h
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 /* Common type definitions shared between the firmware and config tools
25
26         The configuration data is stored in flash.
27
28         The flash is organised as 2 arrays of 256 rows, with each row
29         having 256 bytes. Total of 128kb.
30
31         Linear flash memory map:
32         -----------------------------------------
33         Array 1 |Row 255 | Bootloader metadata
34                         ---------------------------------
35                         |Row 254 |
36                         |Row 252 | Blank
37                         ---------------------------------
38                         |Row 251 |
39                         | ...    |
40                         |Row 236 | Config target 3
41                         | ...    |
42                         |Row 220 | Config target 2
43                         | ...    |
44                         |Row 204 | Config target 1
45                         | ...    |
46                         |Row 188 | Config target 0
47                         ---------------------------------
48                         |Row 187 | Board Config
49                         | ...    | Empty
50                         |Row 172 | Empty
51                         ---------------------------------
52                         |Row 171 |
53                         | ...    |
54                         |Row 156 | Config target 6
55                         | ...    |
56                         |Row 140 | Config target 5
57                         | ...    |
58                         |Row 124 | Config target 4
59                         ---------------------------------
60                         |Row 123 |
61                         | ...    |
62                         |Row 0   |
63         --------|        |
64         Array 0 |Row 255 | Blank
65                         ---------------------------------
66                         |Row 121 |
67                         | ...    |
68                         |Row 37  | Application
69                         ---------------------------------
70                         |Row 36  |
71                         | ...    |
72                         |Row 0   | Bootloader
73
74 */
75
76 #include "stdint.h"
77
78 //#define MAX_SCSI_TARGETS 7
79 #define MAX_SCSI_TARGETS 4
80 #define SCSI_CONFIG_ARRAY 1
81 #define SCSI_CONFIG_ROWS 16
82
83 // 256 bytes data, 32 bytes ECC
84 #define SCSI_CONFIG_ROW_SIZE 256
85 #define SCSI_CONFIG_ROW_ECC 288
86 #define SCSI_CONFIG_BOARD_ROW 187
87 #define SCSI_CONFIG_0_ROW 188
88 #define SCSI_CONFIG_1_ROW 204
89 #define SCSI_CONFIG_2_ROW 220
90 #define SCSI_CONFIG_3_ROW 236
91 #define SCSI_CONFIG_4_ROW 124
92 #define SCSI_CONFIG_5_ROW 140
93 #define SCSI_CONFIG_6_ROW 156
94
95 typedef enum
96 {
97         CONFIG_TARGET_ID_BITS = 0x07,
98         CONFIG_TARGET_ENABLED = 0x80
99 } CONFIG_TARGET_FLAGS;
100
101 typedef enum
102 {
103         CONFIG_ENABLE_UNIT_ATTENTION = 1,
104         CONFIG_ENABLE_PARITY = 2,
105         CONFIG_ENABLE_SCSI2 = 4,
106         CONFIG_DISABLE_GLITCH = 8,
107         CONFIG_ENABLE_CACHE = 16,
108         CONFIG_ENABLE_DISCONNECT = 32,
109         CONFIG_ENABLE_SEL_LATCH = 64,
110         CONFIG_MAP_LUNS_TO_IDS = 128
111 } CONFIG_FLAGS;
112
113 typedef enum
114 {
115         S2S_CFG_ENABLE_TERMINATOR = 1
116 } CONFIG_FLAGS6;
117
118 typedef enum
119 {
120         CONFIG_FIXED,
121         CONFIG_REMOVEABLE,
122         CONFIG_OPTICAL,
123         CONFIG_FLOPPY_14MB,
124         CONFIG_MO,
125         CONFIG_SEQUENTIAL
126
127 } CONFIG_TYPE;
128
129 typedef enum
130 {
131         CONFIG_QUIRKS_NONE = 0,
132         CONFIG_QUIRKS_APPLE = 1,
133         CONFIG_QUIRKS_OMTI = 2,
134         CONFIG_QUIRKS_XEBEC = 4
135 } CONFIG_QUIRKS;
136
137 typedef enum
138 {
139         CONFIG_SPEED_NoLimit,
140         CONFIG_SPEED_ASYNC_15
141 } CONFIG_SPEED;
142
143 typedef struct __attribute__((packed))
144 {
145         uint8_t deviceType;
146         uint8_t pageCode;
147         uint8_t reserved;
148         uint8_t pageLength;
149         uint8_t data[0]; // pageLength bytes.
150 } VPD;
151
152 typedef struct __attribute__((packed))
153 {
154         // bits 7 -> 3 = CONFIG_TARGET_FLAGS
155         // bits 2 -> 0 = target SCSI ID.
156         uint8_t scsiId;
157
158         uint8_t deviceType; // CONFIG_TYPE
159         uint8_t flagsDEPRECATED; // CONFIG_FLAGS, removed in v4.5
160         uint8_t deviceTypeModifier; // Used in INQUIRY response.
161
162         uint32_t sdSectorStart;
163         uint32_t scsiSectors;
164
165         uint16_t bytesPerSector;
166
167         // Max allowed by legacy IBM-PC bios is 6 bits (63)
168         uint16_t sectorsPerTrack;
169
170         // MS-Dos up to 7.10 will crash on >= 256 heads.
171         uint16_t headsPerCylinder;
172
173
174         char vendor[8];
175         char prodId[16];
176         char revision[4];
177         char serial[16];
178
179         uint16_t quirks; // CONFIG_QUIRKS
180
181         uint8_t reserved[960]; // Pad out to 1024 bytes for main section.
182
183         uint8_t modePages[1024];
184         uint8_t vpd[1024];
185         uint8_t unused[1024]; // Total size is 4k.
186 } TargetConfig;
187
188 typedef struct __attribute__((packed))
189 {
190         char magic[4]; // 'BCFG'
191         uint8_t flags; // CONFIG_FLAGS
192         uint8_t startupDelay; // Seconds.
193         uint8_t selectionDelay; // milliseconds. 255 = auto
194         uint8_t flags6; // CONFIG_FLAGS6
195
196         uint8_t scsiSpeed;
197
198
199         uint8_t reserved[247]; // Pad out to 256 bytes
200 } BoardConfig;
201
202 typedef enum
203 {
204         CONFIG_NONE, // Invalid
205
206         // Command content:
207         // uint8_t CONFIG_PING
208         // Response:
209         // CONFIG_STATUS
210         CONFIG_PING,
211
212         // Command content:
213         // uint8_t CONFIG_WRITEFLASH
214         // uint8_t[256] flashData
215         // uint8_t flashArray
216         // uint8_t flashRow
217         // Response:
218         // CONFIG_STATUS
219         CONFIG_WRITEFLASH,
220
221         // Command content:
222         // uint8_t CONFIG_READFLASH
223         // uint8_t flashArray
224         // uint8_t flashRow
225         // Response:
226         // 256 bytes of flash
227         CONFIG_READFLASH,
228
229         // Command content:
230         // uint8_t CONFIG_REBOOT
231         // Response: None.
232         CONFIG_REBOOT,
233
234         // Command content:
235         // uint8_t CONFIG_INFO
236         // Response:
237         // uint8_t[16] CSD
238         // uint8_t[16] CID
239         CONFIG_SDINFO,
240
241         // Command content:
242         // uint8_t CONFIG_SCSITEST
243         // Response:
244         // CONFIG_STATUS
245         // uint8_t result code (0 = passed)
246         CONFIG_SCSITEST
247 } CONFIG_COMMAND;
248
249 typedef enum
250 {
251         CONFIG_STATUS_GOOD,
252         CONFIG_STATUS_ERR
253 } CONFIG_STATUS;
254
255
256
257
258 #ifdef __cplusplus
259 } // extern "C"
260
261         #include <type_traits>
262         static_assert(
263                 std::is_pod<TargetConfig>::value, "Misuse of TargetConfig struct"
264                 );
265         static_assert(
266                 sizeof(TargetConfig) == 4096,
267                 "TargetConfig struct size mismatch"
268                 );
269
270 #endif
271
272 #endif