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
18 #pragma GCC push_options
\r
19 #pragma GCC optimize("-flto")
\r
28 static const uint8 ReadWriteErrorRecoveryPage[] =
\r
31 0x0A, // Page length
\r
33 // VMS 5.5-2 is very particular regarding the mode page values.
\r
34 // The required values for a SCSI2/NoTCQ device are:
\r
35 // AWRE=0 ARRE=0 TB=1 RC=0 EER=? PER=1 DTE=1 DCR=?
\r
36 // See ftp://www.digiater.nl/openvms/decus/vms94b/net94b/scsi_params_dkdriver.txt
\r
37 // X-Newsgroups: comp.os.vms
\r
38 // Subject: Re: VMS 6.1 vs. Seagate Disk Drives
\r
39 // Message-Id: <32g87h$8q@nntpd.lkg.dec.com>
\r
40 // From: weber@evms.enet.dec.com (Ralph O. Weber -- OpenVMS AXP)
\r
41 // Date: 12 Aug 1994 16:32:49 GMT
\r
44 0x00, // Don't try recovery algorithm during reads
\r
45 0x00, // Correction span 0
\r
46 0x00, // Head offset count 0,
\r
47 0x00, // Data strobe offset count 0,
\r
49 0x00, // Don't try recovery algorithm during writes
\r
51 0x00, 0x00 // Recovery time limit 0 (use default)*/
\r
54 static const uint8 ReadWriteErrorRecoveryPage_SCSI1[] =
\r
57 0x06, // Page length
\r
59 0x00, // Don't try recovery algorithm during reads
\r
60 0x00, // Correction span 0
\r
61 0x00, // Head offset count 0,
\r
62 0x00, // Data strobe offset count 0,
\r
66 static const uint8 DisconnectReconnectPage[] =
\r
69 0x0E, // Page length
\r
70 0, // Buffer full ratio
\r
71 0, // Buffer empty ratio
\r
72 0x00, 10, // Bus inactivity limit, 100us increments. Allow 1ms.
\r
73 0x00, 0x00, // Disconnect time limit
\r
74 0x00, 0x00, // Connect time limit
\r
75 0x00, 0x00, // Maximum burst size
\r
76 0x00 ,// DTDC. Not used.
\r
77 0x00, 0x00, 0x00 // Reserved
\r
80 static const uint8 DisconnectReconnectPage_SCSI1[] =
\r
83 0x0A, // Page length
\r
84 0, // Buffer full ratio
\r
85 0, // Buffer empty ratio
\r
86 0x00, 10, // Bus inactivity limit, 100us increments. Allow 1ms.
\r
87 0x00, 0x00, // Disconnect time limit
\r
88 0x00, 0x00, // Connect time limit
\r
89 0x00, 0x00 // Maximum burst size
\r
92 static const uint8 FormatDevicePage[] =
\r
94 0x03 | 0x80, // Page code | PS (persist) bit.
\r
95 0x16, // Page length
\r
96 0x00, 0x00, // Single zone
\r
97 0x00, 0x00, // No alternate sectors
\r
98 0x00, 0x00, // No alternate tracks
\r
99 0x00, 0x00, // No alternate tracks per lun
\r
100 0x00, 0x00, // Sectors per track, configurable
\r
101 0xFF, 0xFF, // Data bytes per physical sector. Configurable.
\r
102 0x00, 0x01, // Interleave
\r
103 0x00, 0x00, // Track skew factor
\r
104 0x00, 0x00, // Cylinder skew factor
\r
105 0xC0, // SSEC(set) HSEC(set) RMB SURF
\r
106 0x00, 0x00, 0x00 // Reserved
\r
109 static const uint8 RigidDiskDriveGeometry[] =
\r
112 0x16, // Page length
\r
113 0xFF, 0xFF, 0xFF, // Number of cylinders
\r
114 0x00, // Number of heads (replaced by configured value)
\r
115 0xFF, 0xFF, 0xFF, // Starting cylinder-write precompensation
\r
116 0xFF, 0xFF, 0xFF, // Starting cylinder-reduced write current
\r
117 0x00, 0x1, // Drive step rate (units of 100ns)
\r
118 0x00, 0x00, 0x00, // Landing zone cylinder
\r
120 0x00, // Rotational offset
\r
122 5400 >> 8, 5400 & 0xFF, // Medium rotation rate (RPM)
\r
123 0x00, 0x00 // Reserved
\r
126 static const uint8 RigidDiskDriveGeometry_SCSI1[] =
\r
129 0x12, // Page length
\r
130 0xFF, 0xFF, 0xFF, // Number of cylinders
\r
131 0x00, // Number of heads (replaced by configured value)
\r
132 0xFF, 0xFF, 0xFF, // Starting cylinder-write precompensation
\r
133 0xFF, 0xFF, 0xFF, // Starting cylinder-reduced write current
\r
134 0x00, 0x1, // Drive step rate (units of 100ns)
\r
135 0x00, 0x00, 0x00, // Landing zone cylinder
\r
137 0x00, // Rotational offset
\r
141 static const uint8 CachingPage[] =
\r
144 0x0A, // Page length
\r
145 0x01, // Read cache disable
\r
146 0x00, // No useful rention policy.
\r
147 0x00, 0x00, // Pre-fetch always disabled
\r
148 0x00, 0x00, // Minimum pre-fetch
\r
149 0x00, 0x00, // Maximum pre-fetch
\r
150 0x00, 0x00, // Maximum pre-fetch ceiling
\r
153 static const uint8 ControlModePage[] =
\r
156 0x06, // Page length
\r
157 0x00, // No logging
\r
158 0x01, // Disable tagged queuing
\r
159 0x00, // No async event notifications
\r
161 0x00, 0x00 // AEN holdoff period.
\r
164 static const uint8_t SequentialDeviceConfigPage[] =
\r
167 0x0E, // Page length
\r
168 0x00, // CAP, CAF, Active Format
\r
169 0x00, // Active partition
\r
170 0x00, // Write buffer full ratio
\r
171 0x00, // Read buffer empty ratio
\r
172 0x00,0x01, // Write delay time, in 100ms units
\r
173 0x00, // Default gap size
\r
174 0x10, // auto-generation of default eod (end of data)
\r
175 0x00,0x00,0x00, // buffer-size at early warning
\r
176 0x00, // No data compression
\r
180 // Allow Apple 68k Drive Setup to format this drive.
\r
182 static const uint8 AppleVendorPage[] =
\r
186 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
\r
187 'A','P','P','L','E',' ','C','O','M','P','U','T','E','R',',',' ','I','N','C','.'
\r
190 static void pageIn(int pc, int dataIdx, const uint8* pageData, int pageLen)
\r
192 memcpy(&scsiDev.data[dataIdx], pageData, pageLen);
\r
194 if (pc == 0x01) // Mask out (un)changable values
\r
196 memset(&scsiDev.data[dataIdx+2], 0, pageLen - 2);
\r
200 static void doModeSense(
\r
201 int sixByteCmd, int dbd, int pc, int pageCode, int allocLength)
\r
203 ////////////// Mode Parameter Header
\r
204 ////////////////////////////////////
\r
206 // Skip the Mode Data Length, we set that last.
\r
208 if (!sixByteCmd) ++idx;
\r
210 uint8_t mediumType = 0;
\r
211 uint8_t deviceSpecificParam = 0;
\r
212 uint8_t density = 0;
\r
213 switch (scsiDev.target->cfg->deviceType)
\r
216 case CONFIG_REMOVEABLE:
\r
217 mediumType = 0; // We should support various floppy types here!
\r
218 // Contains cache bits (0) and a Write-Protect bit.
\r
219 deviceSpecificParam =
\r
220 (blockDev.state & DISK_WP) ? 0x80 : 0;
\r
221 density = 0; // reserved for direct access
\r
224 case CONFIG_FLOPPY_14MB:
\r
225 mediumType = 0x1E; // 90mm/3.5"
\r
226 deviceSpecificParam =
\r
227 (blockDev.state & DISK_WP) ? 0x80 : 0;
\r
228 density = 0; // reserved for direct access
\r
231 case CONFIG_OPTICAL:
\r
232 mediumType = 0x02; // 120mm CDROM, data only.
\r
233 deviceSpecificParam = 0;
\r
234 density = 0x01; // User data only, 2048bytes per sector.
\r
237 case CONFIG_SEQUENTIAL:
\r
238 mediumType = 0; // reserved
\r
239 deviceSpecificParam =
\r
240 (blockDev.state & DISK_WP) ? 0x80 : 0;
\r
241 density = 0x13; // DAT Data Storage, X3B5/88-185A
\r
245 mediumType = 0x03; // Optical reversible or erasable medium
\r
246 deviceSpecificParam =
\r
247 (blockDev.state & DISK_WP) ? 0x80 : 0;
\r
248 density = 0x00; // Default
\r
253 scsiDev.data[idx++] = mediumType;
\r
254 scsiDev.data[idx++] = deviceSpecificParam;
\r
260 scsiDev.data[idx++] = 0; // No block descriptor
\r
264 // One block descriptor of length 8 bytes.
\r
265 scsiDev.data[idx++] = 8;
\r
270 scsiDev.data[idx++] = 0; // Reserved
\r
271 scsiDev.data[idx++] = 0; // Reserved
\r
274 scsiDev.data[idx++] = 0; // No block descriptor
\r
275 scsiDev.data[idx++] = 0; // No block descriptor
\r
279 // One block descriptor of length 8 bytes.
\r
280 scsiDev.data[idx++] = 0;
\r
281 scsiDev.data[idx++] = 8;
\r
285 ////////////// Block Descriptor
\r
286 ////////////////////////////////////
\r
289 scsiDev.data[idx++] = density;
\r
290 // Number of blocks
\r
291 // Zero == all remaining blocks shall have the medium
\r
292 // characteristics specified.
\r
293 scsiDev.data[idx++] = 0;
\r
294 scsiDev.data[idx++] = 0;
\r
295 scsiDev.data[idx++] = 0;
\r
297 scsiDev.data[idx++] = 0; // reserved
\r
300 uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
\r
301 scsiDev.data[idx++] = bytesPerSector >> 16;
\r
302 scsiDev.data[idx++] = bytesPerSector >> 8;
\r
303 scsiDev.data[idx++] = bytesPerSector & 0xFF;
\r
308 if (pageCode == 0x01 || pageCode == 0x3F)
\r
311 if ((scsiDev.compatMode >= COMPAT_SCSI2))
\r
313 pageIn(pc, idx, ReadWriteErrorRecoveryPage, sizeof(ReadWriteErrorRecoveryPage));
\r
314 idx += sizeof(ReadWriteErrorRecoveryPage);
\r
318 pageIn(pc, idx, ReadWriteErrorRecoveryPage_SCSI1, sizeof(ReadWriteErrorRecoveryPage_SCSI1));
\r
319 idx += sizeof(ReadWriteErrorRecoveryPage_SCSI1);
\r
323 if (pageCode == 0x02 || pageCode == 0x3F)
\r
326 if ((scsiDev.compatMode >= COMPAT_SCSI2))
\r
328 pageIn(pc, idx, DisconnectReconnectPage, sizeof(DisconnectReconnectPage));
\r
329 idx += sizeof(DisconnectReconnectPage);
\r
333 pageIn(pc, idx, DisconnectReconnectPage_SCSI1, sizeof(DisconnectReconnectPage_SCSI1));
\r
334 idx += sizeof(DisconnectReconnectPage_SCSI1);
\r
338 if (pageCode == 0x03 || pageCode == 0x3F)
\r
341 pageIn(pc, idx, FormatDevicePage, sizeof(FormatDevicePage));
\r
344 uint16_t sectorsPerTrack = scsiDev.target->cfg->sectorsPerTrack;
\r
345 scsiDev.data[idx+10] = sectorsPerTrack >> 8;
\r
346 scsiDev.data[idx+11] = sectorsPerTrack & 0xFF;
\r
348 // Fill out the configured bytes-per-sector
\r
349 uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
\r
350 scsiDev.data[idx+12] = bytesPerSector >> 8;
\r
351 scsiDev.data[idx+13] = bytesPerSector & 0xFF;
\r
355 // Set a mask for the changeable values.
\r
356 scsiDev.data[idx+12] = 0xFF;
\r
357 scsiDev.data[idx+13] = 0xFF;
\r
360 idx += sizeof(FormatDevicePage);
\r
363 if (pageCode == 0x04 || pageCode == 0x3F)
\r
366 if ((scsiDev.compatMode >= COMPAT_SCSI2))
\r
368 pageIn(pc, idx, RigidDiskDriveGeometry, sizeof(RigidDiskDriveGeometry));
\r
372 pageIn(pc, idx, RigidDiskDriveGeometry_SCSI1, sizeof(RigidDiskDriveGeometry_SCSI1));
\r
377 // Need to fill out the number of cylinders.
\r
383 scsiDev.target->cfg->sdSectorStart,
\r
384 scsiDev.target->liveCfg.bytesPerSector,
\r
385 scsiDev.target->cfg->scsiSectors),
\r
389 scsiDev.target->cfg->headsPerCylinder,
\r
390 scsiDev.target->cfg->sectorsPerTrack);
\r
392 scsiDev.data[idx+2] = cyl >> 16;
\r
393 scsiDev.data[idx+3] = cyl >> 8;
\r
394 scsiDev.data[idx+4] = cyl;
\r
396 memcpy(&scsiDev.data[idx+6], &scsiDev.data[idx+2], 3);
\r
397 memcpy(&scsiDev.data[idx+9], &scsiDev.data[idx+2], 3);
\r
399 scsiDev.data[idx+5] = scsiDev.target->cfg->headsPerCylinder;
\r
402 if ((scsiDev.compatMode >= COMPAT_SCSI2))
\r
404 idx += sizeof(RigidDiskDriveGeometry);
\r
408 idx += sizeof(RigidDiskDriveGeometry_SCSI1);
\r
412 // DON'T output the following pages for SCSI1 hosts. They get upset when
\r
413 // we have more data to send than the allocation length provided.
\r
414 // (ie. Try not to output any more pages below this comment)
\r
417 if ((scsiDev.compatMode >= COMPAT_SCSI2) &&
\r
418 (pageCode == 0x08 || pageCode == 0x3F))
\r
421 pageIn(pc, idx, CachingPage, sizeof(CachingPage));
\r
422 idx += sizeof(CachingPage);
\r
425 if ((scsiDev.compatMode >= COMPAT_SCSI2)
\r
426 && (pageCode == 0x0A || pageCode == 0x3F))
\r
429 pageIn(pc, idx, ControlModePage, sizeof(ControlModePage));
\r
430 idx += sizeof(ControlModePage);
\r
433 if ((scsiDev.target->cfg->deviceType == CONFIG_SEQUENTIAL) &&
\r
434 (pageCode == 0x10 || pageCode == 0x3F))
\r
440 SequentialDeviceConfigPage,
\r
441 sizeof(SequentialDeviceConfigPage));
\r
442 idx += sizeof(SequentialDeviceConfigPage);
\r
446 (scsiDev.target->cfg->quirks == CONFIG_QUIRKS_APPLE) ||
\r
447 (idx + sizeof(AppleVendorPage) <= allocLength)
\r
449 (pageCode == 0x30 || pageCode == 0x3F))
\r
452 pageIn(pc, idx, AppleVendorPage, sizeof(AppleVendorPage));
\r
453 idx += sizeof(AppleVendorPage);
\r
458 // Unknown Page Code
\r
460 scsiDev.status = CHECK_CONDITION;
\r
461 scsiDev.target->sense.code = ILLEGAL_REQUEST;
\r
462 scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
\r
463 scsiDev.phase = STATUS;
\r
467 // Go back and fill out the mode data length
\r
470 // Cannot currently exceed limits. yay
\r
471 scsiDev.data[0] = idx - 1;
\r
475 scsiDev.data[0] = ((idx - 2) >> 8);
\r
476 scsiDev.data[1] = (idx - 2);
\r
479 scsiDev.dataLen = idx > allocLength ? allocLength : idx;
\r
480 scsiDev.phase = DATA_IN;
\r
485 // Callback after the DATA OUT phase is complete.
\r
486 static void doModeSelect(void)
\r
488 if (scsiDev.status == GOOD) // skip if we've already encountered an error
\r
490 // scsiDev.dataLen bytes are in scsiDev.data
\r
494 if (scsiDev.cdb[0] == 0x55)
\r
497 (((uint16_t)scsiDev.data[6]) << 8) |scsiDev.data[7];
\r
502 blockDescLen = scsiDev.data[3];
\r
506 // The unwritten rule. Blocksizes are normally set using the
\r
507 // block descriptor value, not by changing page 0x03.
\r
508 if (blockDescLen >= 8)
\r
510 uint32_t bytesPerSector =
\r
511 (((uint32_t)scsiDev.data[idx+5]) << 16) |
\r
512 (((uint32_t)scsiDev.data[idx+6]) << 8) |
\r
513 scsiDev.data[idx+7];
\r
514 if ((bytesPerSector < MIN_SECTOR_SIZE) ||
\r
515 (bytesPerSector > MAX_SECTOR_SIZE))
\r
521 scsiDev.target->liveCfg.bytesPerSector = bytesPerSector;
\r
522 if (bytesPerSector != scsiDev.target->cfg->bytesPerSector)
\r
524 configSave(scsiDev.target->targetId, bytesPerSector);
\r
528 idx += blockDescLen;
\r
530 while (idx < scsiDev.dataLen)
\r
532 int pageLen = scsiDev.data[idx + 1];
\r
533 if (idx + 2 + pageLen > scsiDev.dataLen) goto bad;
\r
535 int pageCode = scsiDev.data[idx] & 0x3F;
\r
538 case 0x03: // Format Device Page
\r
540 if (pageLen != 0x16) goto bad;
\r
542 // Fill out the configured bytes-per-sector
\r
543 uint16_t bytesPerSector =
\r
544 (((uint16_t)scsiDev.data[idx+12]) << 8) |
\r
545 scsiDev.data[idx+13];
\r
547 // Sane values only, ok ?
\r
548 if ((bytesPerSector < MIN_SECTOR_SIZE) ||
\r
549 (bytesPerSector > MAX_SECTOR_SIZE))
\r
554 scsiDev.target->liveCfg.bytesPerSector = bytesPerSector;
\r
555 if (scsiDev.cdb[1] & 1) // SP Save Pages flag
\r
557 configSave(scsiDev.target->targetId, bytesPerSector);
\r
563 // Easiest to just ignore for now. We'll get here when changing
\r
564 // the SCSI block size via the descriptor header.
\r
566 idx += 2 + pageLen;
\r
572 scsiDev.status = CHECK_CONDITION;
\r
573 scsiDev.target->sense.code = ILLEGAL_REQUEST;
\r
574 scsiDev.target->sense.asc = INVALID_FIELD_IN_PARAMETER_LIST;
\r
577 scsiDev.phase = STATUS;
\r
580 int scsiModeCommand()
\r
582 int commandHandled = 1;
\r
584 uint8 command = scsiDev.cdb[0];
\r
586 // We don't currently support the setting of any parameters.
\r
587 // (ie. no MODE SELECT(6) or MODE SELECT(10) commands)
\r
589 if (command == 0x1A)
\r
592 int dbd = scsiDev.cdb[1] & 0x08; // Disable block descriptors
\r
593 int pc = scsiDev.cdb[2] >> 6; // Page Control
\r
594 int pageCode = scsiDev.cdb[2] & 0x3F;
\r
595 int allocLength = scsiDev.cdb[4];
\r
597 // SCSI1 standard: (CCS X3T9.2/86-52)
\r
598 // "An Allocation Length of zero indicates that no MODE SENSE data shall
\r
599 // be transferred. This condition shall not be considered as an error."
\r
600 doModeSense(1, dbd, pc, pageCode, allocLength);
\r
602 else if (command == 0x5A)
\r
605 int dbd = scsiDev.cdb[1] & 0x08; // Disable block descriptors
\r
606 int pc = scsiDev.cdb[2] >> 6; // Page Control
\r
607 int pageCode = scsiDev.cdb[2] & 0x3F;
\r
609 (((uint16) scsiDev.cdb[7]) << 8) +
\r
611 doModeSense(0, dbd, pc, pageCode, allocLength);
\r
613 else if (command == 0x15)
\r
616 int len = scsiDev.cdb[4];
\r
619 // If len == 0, then transfer no data. From the SCSI 2 standard:
\r
620 // A parameter list length of zero indicates that no data shall
\r
621 // be transferred. This condition shall not be considered as an
\r
623 scsiDev.phase = STATUS;
\r
627 scsiDev.dataLen = len;
\r
628 scsiDev.phase = DATA_OUT;
\r
629 scsiDev.postDataOutHook = doModeSelect;
\r
632 else if (command == 0x55)
\r
635 int allocLength = (((uint16) scsiDev.cdb[7]) << 8) + scsiDev.cdb[8];
\r
636 if (allocLength == 0)
\r
638 // If len == 0, then transfer no data. From the SCSI 2 standard:
\r
639 // A parameter list length of zero indicates that no data shall
\r
640 // be transferred. This condition shall not be considered as an
\r
642 scsiDev.phase = STATUS;
\r
646 scsiDev.dataLen = allocLength;
\r
647 scsiDev.phase = DATA_OUT;
\r
648 scsiDev.postDataOutHook = doModeSelect;
\r
653 commandHandled = 0;
\r
656 return commandHandled;
\r
659 #pragma GCC pop_options
\r