1 // Copyright (C) 2013 Michael McMaster <michael@codesrc.com>
\r
2 // Copyright (C) 2014 Doug Brown <doug@downtowndougbrown.com>
\r
4 // This file is part of SCSI2SD.
\r
6 // SCSI2SD is free software: you can redistribute it and/or modify
\r
7 // it under the terms of the GNU General Public License as published by
\r
8 // the Free Software Foundation, either version 3 of the License, or
\r
9 // (at your option) any later version.
\r
11 // SCSI2SD is distributed in the hope that it will be useful,
\r
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 // GNU General Public License for more details.
\r
16 // You should have received a copy of the GNU General Public License
\r
17 // along with SCSI2SD. If not, see <http://www.gnu.org/licenses/>.
\r
26 static const uint8 ReadWriteErrorRecoveryPage[] =
\r
29 0x0A, // Page length
\r
30 0x00, // No error recovery options for now
\r
31 0x00, // Don't try recovery algorithm during reads
\r
32 0x00, // Correction span 0
\r
33 0x00, // Head offset count 0,
\r
34 0x00, // Data strobe offset count 0,
\r
36 0x00, // Don't try recovery algorithm during writes
\r
38 0x00, 0x00 // Recovery time limit 0 (use default)*/
\r
41 static const uint8 DisconnectReconnectPage[] =
\r
44 0x0E, // Page length
\r
45 0, // Buffer full ratio
\r
46 0, // Buffer empty ratio
\r
47 0x00, 10, // Bus inactivity limit, 100us increments. Allow 1ms.
\r
48 0x00, 0x00, // Disconnect time limit
\r
49 0x00, 0x00, // Connect time limit
\r
50 0x00, 0x00, // Maximum burst size
\r
51 0x00 ,// DTDC. Not used.
\r
52 0x00, 0x00, 0x00 // Reserved
\r
55 static const uint8 FormatDevicePage[] =
\r
58 0x16, // Page length
\r
59 0x00, 0x00, // Single zone
\r
60 0x00, 0x00, // No alternate sectors
\r
61 0x00, 0x00, // No alternate tracks
\r
62 0x00, 0x00, // No alternate tracks per lun
\r
63 0x00, SCSI_SECTORS_PER_TRACK, // Sectors per track
\r
64 SCSI_SECTOR_SIZE >> 8, SCSI_SECTOR_SIZE & 0xFF, // Data bytes per physical sector
\r
65 0x00, 0x01, // Interleave
\r
66 0x00, 0x00, // Track skew factor
\r
67 0x00, 0x00, // Cylinder skew factor
\r
68 0xC0, // SSEC(set) HSEC(set) RMB SURF
\r
69 0x00, 0x00, 0x00 // Reserved
\r
72 static const uint8 RigidDiskDriveGeometry[] =
\r
75 0x16, // Page length
\r
76 0xFF, 0xFF, 0xFF, // Number of cylinders
\r
77 SCSI_HEADS_PER_CYLINDER, // Number of heads
\r
78 0xFF, 0xFF, 0xFF, // Starting cylinder-write precompensation
\r
79 0xFF, 0xFF, 0xFF, // Starting cylinder-reduced write current
\r
80 0x00, 0x1, // Drive step rate (units of 100ns)
\r
81 0x00, 0x00, 0x00, // Landing zone cylinder
\r
83 0x00, // Rotational offset
\r
85 5400 >> 8, 5400 & 0xFF, // Medium rotation rate (RPM)
\r
86 0x00, 0x00 // Reserved
\r
89 static const uint8 CachingPage[] =
\r
92 0x0A, // Page length
\r
93 0x01, // Read cache disable
\r
94 0x00, // No useful rention policy.
\r
95 0x00, 0x00, // Pre-fetch always disabled
\r
96 0x00, 0x00, // Minimum pre-fetch
\r
97 0x00, 0x00, // Maximum pre-fetch
\r
98 0x00, 0x00, // Maximum pre-fetch ceiling
\r
101 static const uint8 ControlModePage[] =
\r
104 0x06, // Page length
\r
105 0x00, // No logging
\r
106 0x01, // Disable tagged queuing
\r
107 0x00, // No async event notifications
\r
109 0x00, 0x00 // AEN holdoff period.
\r
112 // Allow Apple 68k Drive Setup to format this drive.
\r
114 // TODO make this string configurable.
\r
115 static const uint8 AppleVendorPage[] =
\r
119 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
\r
120 'A','P','P','L','E',' ','C','O','M','P','U','T','E','R',',',' ','I','N','C','.'
\r
123 static void pageIn(int pc, int dataIdx, const uint8* pageData, int pageLen)
\r
125 memcpy(&scsiDev.data[dataIdx], pageData, pageLen);
\r
127 if (pc == 0x01) // Mask out (un)changable values
\r
129 memset(&scsiDev.data[dataIdx+2], 0, pageLen - 2);
\r
133 static void doModeSense(
\r
134 int sixByteCmd, int dbd, int pc, int pageCode, int allocLength)
\r
136 if (pc == 0x03) // Saved Values not supported.
\r
138 scsiDev.status = CHECK_CONDITION;
\r
139 scsiDev.sense.code = ILLEGAL_REQUEST;
\r
140 scsiDev.sense.asc = SAVING_PARAMETERS_NOT_SUPPORTED;
\r
141 scsiDev.phase = STATUS;
\r
147 ////////////// Mode Parameter Header
\r
148 ////////////////////////////////////
\r
150 // Skip the Mode Data Length, we set that last.
\r
152 if (!sixByteCmd) ++idx;
\r
154 scsiDev.data[idx++] = 0; // Medium type. 0 = default
\r
156 // Device-specific parameter. Contains cache bits (0) and
\r
157 // a Write-Protect bit.
\r
158 scsiDev.data[idx++] = (blockDev.state & DISK_WP) ? 0x80 : 0;
\r
164 scsiDev.data[idx++] = 0; // No block descriptor
\r
168 // One block descriptor of length 8 bytes.
\r
169 scsiDev.data[idx++] = 8;
\r
174 scsiDev.data[idx++] = 0; // Reserved
\r
175 scsiDev.data[idx++] = 0; // Reserved
\r
178 scsiDev.data[idx++] = 0; // No block descriptor
\r
179 scsiDev.data[idx++] = 0; // No block descriptor
\r
183 // One block descriptor of length 8 bytes.
\r
184 scsiDev.data[idx++] = 0;
\r
185 scsiDev.data[idx++] = 8;
\r
189 ////////////// Block Descriptor
\r
190 ////////////////////////////////////
\r
193 scsiDev.data[idx++] = 0; // Density code. Reserved for direct-access
\r
194 // Number of blocks
\r
195 // Zero == all remaining blocks shall have the medium
\r
196 // characteristics specified.
\r
197 scsiDev.data[idx++] = 0;
\r
198 scsiDev.data[idx++] = 0;
\r
199 scsiDev.data[idx++] = 0;
\r
201 scsiDev.data[idx++] = 0; // reserved
\r
204 scsiDev.data[idx++] = SCSI_BLOCK_SIZE >> 16;
\r
205 scsiDev.data[idx++] = SCSI_BLOCK_SIZE >> 8;
\r
206 scsiDev.data[idx++] = SCSI_BLOCK_SIZE & 0xFF;
\r
215 pageIn(pc, idx, ReadWriteErrorRecoveryPage, sizeof(ReadWriteErrorRecoveryPage));
\r
216 idx += sizeof(ReadWriteErrorRecoveryPage);
\r
217 if (pageCode != 0x3f) break;
\r
220 pageIn(pc, idx, DisconnectReconnectPage, sizeof(DisconnectReconnectPage));
\r
221 idx += sizeof(DisconnectReconnectPage);
\r
222 if (pageCode != 0x3f) break;
\r
225 pageIn(pc, idx, FormatDevicePage, sizeof(FormatDevicePage));
\r
226 idx += sizeof(FormatDevicePage);
\r
227 if (pageCode != 0x3f) break;
\r
231 pageIn(pc, idx, RigidDiskDriveGeometry, sizeof(RigidDiskDriveGeometry));
\r
235 // Need to fill out the number of cylinders.
\r
239 LBA2CHS(blockDev.capacity, &cyl, &head, §or);
\r
241 scsiDev.data[idx+2] = cyl >> 16;
\r
242 scsiDev.data[idx+3] = cyl >> 8;
\r
243 scsiDev.data[idx+4] = cyl;
\r
245 memcpy(&scsiDev.data[idx+6], &scsiDev.data[idx+2], 3);
\r
246 memcpy(&scsiDev.data[idx+9], &scsiDev.data[idx+2], 3);
\r
249 idx += sizeof(RigidDiskDriveGeometry);
\r
250 if (pageCode != 0x3f) break;
\r
254 pageIn(pc, idx, CachingPage, sizeof(CachingPage));
\r
255 idx += sizeof(CachingPage);
\r
256 if (pageCode != 0x3f) break;
\r
259 pageIn(pc, idx, ControlModePage, sizeof(ControlModePage));
\r
260 idx += sizeof(ControlModePage);
\r
264 pageIn(pc, idx, AppleVendorPage, sizeof(AppleVendorPage));
\r
265 idx += sizeof(AppleVendorPage);
\r
269 // Unknown Page Code
\r
271 scsiDev.status = CHECK_CONDITION;
\r
272 scsiDev.sense.code = ILLEGAL_REQUEST;
\r
273 scsiDev.sense.asc = INVALID_FIELD_IN_CDB;
\r
274 scsiDev.phase = STATUS;
\r
278 if (idx > allocLength)
\r
280 // Chop the reply off early if shorter length is requested
\r
286 // Go back and fill out the mode data length
\r
289 // Cannot currently exceed limits. yay
\r
290 scsiDev.data[0] = idx - 1;
\r
294 scsiDev.data[0] = ((idx - 2) >> 8);
\r
295 scsiDev.data[1] = (idx - 2);
\r
298 scsiDev.dataLen = idx;
\r
299 scsiDev.phase = DATA_IN;
\r
304 scsiDev.status = CHECK_CONDITION;
\r
305 scsiDev.sense.code = ILLEGAL_REQUEST;
\r
306 scsiDev.sense.asc = INVALID_FIELD_IN_CDB;
\r
307 scsiDev.phase = STATUS;
\r
312 int scsiModeCommand()
\r
314 int commandHandled = 1;
\r
316 uint8 command = scsiDev.cdb[0];
\r
318 // We don't currently support the setting of any parameters.
\r
319 // (ie. no MODE SELECT(6) or MODE SELECT(10) commands)
\r
321 if (command == 0x1A)
\r
324 int dbd = scsiDev.cdb[1] & 0x08; // Disable block descriptors
\r
325 int pc = scsiDev.cdb[2] >> 6; // Page Control
\r
326 int pageCode = scsiDev.cdb[2] & 0x3F;
\r
327 int allocLength = scsiDev.cdb[4];
\r
328 if (allocLength == 0) allocLength = 256;
\r
329 doModeSense(1, dbd, pc, pageCode, allocLength);
\r
331 else if (command == 0x5A)
\r
334 int dbd = scsiDev.cdb[1] & 0x08; // Disable block descriptors
\r
335 int pc = scsiDev.cdb[2] >> 6; // Page Control
\r
336 int pageCode = scsiDev.cdb[2] & 0x3F;
\r
338 (((uint16) scsiDev.cdb[7]) << 8) +
\r
340 doModeSense(0, dbd, pc, pageCode, allocLength);
\r
342 else if (command == 0x15)
\r
345 int len = scsiDev.cdb[4];
\r
346 if (len == 0) len = 256;
\r
347 scsiDev.dataLen = len;
\r
348 scsiDev.phase = DATA_OUT;
\r
350 else if (command == 0x55)
\r
353 int allocLength = (((uint16) scsiDev.cdb[7]) << 8) + scsiDev.cdb[8];
\r
354 scsiDev.dataLen = allocLength;
\r
355 scsiDev.phase = DATA_OUT;
\r
359 commandHandled = 0;
\r
362 return commandHandled;
\r