1 // Copyright (C) 2013 Michael McMaster <michael@codesrc.com>
3 // This file is part of SCSI2SD.
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.
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.
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/>.
37 STATUS = __scsiphase_cd | __scsiphase_io,
38 COMMAND = __scsiphase_cd,
39 DATA_IN = __scsiphase_io,
41 MESSAGE_IN = __scsiphase_msg | __scsiphase_cd | __scsiphase_io,
42 MESSAGE_OUT = __scsiphase_msg | __scsiphase_cd
56 MSG_COMMAND_COMPLETE = 0,
58 MSG_LINKED_COMMAND_COMPLETE = 0x0A,
59 MSG_LINKED_COMMAND_COMPLETE_WITH_FLAG = 0x0B
67 // Messages are being used, yet SCSI 2 mode is disabled.
68 // This impacts interpretation of INQUIRY commands.
69 COMPAT_SCSI2_DISABLED,
74 // Maximum value for bytes-per-sector.
75 #define MAX_SECTOR_SIZE 8192
76 #define MIN_SECTOR_SIZE 64
78 // Shadow parameters, possibly not saved to flash yet.
79 // Set via Mode Select
82 uint16_t bytesPerSector;
89 const S2S_TargetCfg* cfg;
95 uint16_t unitAttention; // Set to the sense qualifier key to be returned.
97 // Only let the reserved initiator talk to us.
98 // A 3rd party may be sending the RESERVE/RELEASE commands
99 int reservedId; // 0 -> 7 if reserved. -1 if not reserved.
100 int reserverId; // 0 -> 7 if reserved. -1 if not reserved.
108 // TODO reduce this buffer size and add a proper cache
109 // Must be aligned for DMA
110 // 65536 bytes is the DMA limit
111 uint8_t data[MAX_SECTOR_SIZE * 8];
113 TargetState targets[S2S_MAX_TARGETS];
115 S2S_BoardCfg boardCfg;
118 // Set to true (1) if the ATN flag was set, and we need to
119 // enter the MESSAGE_OUT phase.
122 // Set to true (1) if the RST flag was set.
123 volatile int resetFlag;
125 // Set to sel register if the SEL flag was set.
126 volatile int selFlag;
128 // Set to true (1) if a parity error was observed.
133 int dataPtr; // Index into data, reset on [re]selection to savedDataPtr
134 int savedDataPtr; // Index into data, initially 0.
137 uint8_t cdb[12]; // command descriptor block
138 uint8_t cdbLen; // 6, 10, or 12 byte message.
139 int8_t lun; // Target lun, set by IDENTIFY message.
140 uint8_t discPriv; // Disconnect priviledge.
141 uint8_t compatMode; // SCSI_COMPAT_MODE
143 // Only let the reserved initiator talk to us.
144 // A 3rd party may be sending the RESERVE/RELEASE commands
145 int initiatorId; // 0 -> 7. Set during the selection phase.
147 // SCSI_STATUS value.
148 // Change to CHECK_CONDITION when setting a SENSE value
154 void (*postDataOutHook)(void);
160 uint8_t watchdogTick;
163 uint16_t lastSenseASC;
164 uint8_t minSyncPeriod; // Debug use only.
166 int needSyncNegotiationAck;
169 // Estimate of the SCSI host actual speed
170 uint32_t hostSpeedKBs;
171 int hostSpeedMeasured;
174 extern ScsiDevice scsiDev;
176 void process_Status(void);
177 int process_MessageIn(int releaseBusFree);
178 void enter_BusFree(void);
182 void scsiDisconnect(void);
183 int scsiReconnect(void);
186 // Utility macros, consistent with the Linux Kernel code.
187 #define likely(x) __builtin_expect(!!(x), 1)
188 #define unlikely(x) __builtin_expect(!!(x), 0)
189 //#define likely(x) (x)
190 //#define unlikely(x) (x)