16207eccece0a33f7b0570180503710b5867d440
[SCSI2SD.git] / software / SCSI2SD / src / mode.c
1 //      Copyright (C) 2013 Michael McMaster <michael@codesrc.com>\r
2 //  Copyright (C) 2014 Doug Brown <doug@downtowndougbrown.com>\r
3 //\r
4 //      This file is part of SCSI2SD.\r
5 //\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
10 //\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
15 //\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 \r
19 #include "device.h"\r
20 #include "scsi.h"\r
21 #include "mode.h"\r
22 #include "disk.h"\r
23 \r
24 #include <string.h>\r
25 \r
26 static const uint8 ReadWriteErrorRecoveryPage[] =\r
27 {\r
28 0x01, // Page code\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
35 0x00, // Reserved\r
36 0x00, // Don't try recovery algorithm during writes\r
37 0x00, // Reserved\r
38 0x00, 0x00 // Recovery time limit 0 (use default)*/\r
39 };\r
40 \r
41 static const uint8 DisconnectReconnectPage[] =\r
42 {\r
43 0x02, // Page code\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
53 };\r
54 \r
55 static const uint8 FormatDevicePage[] =\r
56 {\r
57 0x03 | 0x80, // Page code | PS (persist) bit.\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 0xFF, 0xFF, // Data bytes per physical sector. Configurable.\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
70 };\r
71 \r
72 static const uint8 RigidDiskDriveGeometry[] =\r
73 {\r
74 0x04, // Page code\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
82 0x00, // RPL\r
83 0x00, // Rotational offset\r
84 0x00, // Reserved\r
85 5400 >> 8, 5400 & 0xFF, // Medium rotation rate (RPM)\r
86 0x00, 0x00 // Reserved\r
87 };\r
88 \r
89 static const uint8 CachingPage[] =\r
90 {\r
91 0x08, // Page Code\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
99 };\r
100 \r
101 static const uint8 ControlModePage[] =\r
102 {\r
103 0x0A, // Page code\r
104 0x06, // Page length\r
105 0x00, // No logging\r
106 0x01, // Disable tagged queuing\r
107 0x00, // No async event notifications\r
108 0x00, // Reserved\r
109 0x00, 0x00 // AEN holdoff period.\r
110 };\r
111 \r
112 // Allow Apple 68k Drive Setup to format this drive.\r
113 // Code\r
114 static const uint8 AppleVendorPage[] =\r
115 {\r
116 0x30, // Page code\r
117 28, // Page length\r
118 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\r
119 'A','P','P','L','E',' ','C','O','M','P','U','T','E','R',',',' ','I','N','C','.'\r
120 };\r
121 \r
122 static void pageIn(int pc, int dataIdx, const uint8* pageData, int pageLen)\r
123 {\r
124         memcpy(&scsiDev.data[dataIdx], pageData, pageLen);\r
125 \r
126         if (pc == 0x01) // Mask out (un)changable values\r
127         {\r
128                 memset(&scsiDev.data[dataIdx+2], 0, pageLen - 2);\r
129         }\r
130 }\r
131 \r
132 static void doModeSense(\r
133         int sixByteCmd, int dbd, int pc, int pageCode, int allocLength)\r
134 {\r
135         if (pc == 0x03) // Saved Values not supported.\r
136         {\r
137                 scsiDev.status = CHECK_CONDITION;\r
138                 scsiDev.target->sense.code = ILLEGAL_REQUEST;\r
139                 scsiDev.target->sense.asc = SAVING_PARAMETERS_NOT_SUPPORTED;\r
140                 scsiDev.phase = STATUS;\r
141         }\r
142         else\r
143         {\r
144                 int pageFound = 1;\r
145 \r
146                 ////////////// Mode Parameter Header\r
147                 ////////////////////////////////////\r
148 \r
149                 // Skip the Mode Data Length, we set that last.\r
150                 int idx = 1;\r
151                 if (!sixByteCmd) ++idx;\r
152 \r
153                 scsiDev.data[idx++] = 0; // Medium type. 0 = default\r
154 \r
155                 // Device-specific parameter. Contains cache bits (0) and\r
156                 // a Write-Protect bit.\r
157                 scsiDev.data[idx++] = (blockDev.state & DISK_WP) ? 0x80 : 0;\r
158 \r
159                 if (sixByteCmd)\r
160                 {\r
161                         if (dbd)\r
162                         {\r
163                                 scsiDev.data[idx++] = 0; // No block descriptor\r
164                         }\r
165                         else\r
166                         {\r
167                                 // One block descriptor of length 8 bytes.\r
168                                 scsiDev.data[idx++] = 8;\r
169                         }\r
170                 }\r
171                 else\r
172                 {\r
173                         scsiDev.data[idx++] = 0; // Reserved\r
174                         scsiDev.data[idx++] = 0; // Reserved\r
175                         if (dbd)\r
176                         {\r
177                                 scsiDev.data[idx++] = 0; // No block descriptor\r
178                                 scsiDev.data[idx++] = 0; // No block descriptor\r
179                         }\r
180                         else\r
181                         {\r
182                                 // One block descriptor of length 8 bytes.\r
183                                 scsiDev.data[idx++] = 0;\r
184                                 scsiDev.data[idx++] = 8;\r
185                         }\r
186                 }\r
187 \r
188                 ////////////// Block Descriptor\r
189                 ////////////////////////////////////\r
190                 if (!dbd)\r
191                 {\r
192                         scsiDev.data[idx++] = 0; // Density code. Reserved for direct-access\r
193                         // Number of blocks\r
194                         // Zero == all remaining blocks shall have the medium\r
195                         // characteristics specified.\r
196                         scsiDev.data[idx++] = 0;\r
197                         scsiDev.data[idx++] = 0;\r
198                         scsiDev.data[idx++] = 0;\r
199 \r
200                         scsiDev.data[idx++] = 0; // reserved\r
201 \r
202                         // Block length\r
203                         uint32_t bytesPerSector = scsiDev.target->cfg->bytesPerSector;\r
204                         scsiDev.data[idx++] = bytesPerSector >> 16;\r
205                         scsiDev.data[idx++] = bytesPerSector >> 8;\r
206                         scsiDev.data[idx++] = bytesPerSector & 0xFF;\r
207                 }\r
208 \r
209                 switch (pageCode)\r
210                 {\r
211                 case 0x3F:\r
212                         // EVERYTHING\r
213 \r
214                 case 0x01:\r
215                         pageIn(pc, idx, ReadWriteErrorRecoveryPage, sizeof(ReadWriteErrorRecoveryPage));\r
216                         idx += sizeof(ReadWriteErrorRecoveryPage);\r
217                         if (pageCode != 0x3f) break;\r
218 \r
219                 case 0x02:\r
220                         pageIn(pc, idx, DisconnectReconnectPage, sizeof(DisconnectReconnectPage));\r
221                         idx += sizeof(DisconnectReconnectPage);\r
222                         if (pageCode != 0x3f) break;\r
223 \r
224                 case 0x03:\r
225                         pageIn(pc, idx, FormatDevicePage, sizeof(FormatDevicePage));\r
226                         if (pc != 0x01)\r
227                         {\r
228                                 // Fill out the configured bytes-per-sector\r
229                                 uint32_t bytesPerSector = scsiDev.target->cfg->bytesPerSector;\r
230                                 scsiDev.data[idx+12] = bytesPerSector >> 8;\r
231                                 scsiDev.data[idx+13] = bytesPerSector & 0xFF;\r
232                         }\r
233                         else\r
234                         {\r
235                                 // Set a mask for the changeable values.\r
236                                 scsiDev.data[idx+12] = 0xFF;\r
237                                 scsiDev.data[idx+13] = 0xFF;\r
238                         }\r
239 \r
240                         idx += sizeof(FormatDevicePage);\r
241                         if (pageCode != 0x3f) break;\r
242 \r
243                 case 0x04:\r
244                 {\r
245                         pageIn(pc, idx, RigidDiskDriveGeometry, sizeof(RigidDiskDriveGeometry));\r
246 \r
247                         if (pc != 0x01)\r
248                         {\r
249                                 // Need to fill out the number of cylinders.\r
250                                 uint32 cyl;\r
251                                 uint8 head;\r
252                                 uint32 sector;\r
253                                 LBA2CHS(getScsiCapacity(scsiDev.target->cfg), &cyl, &head, &sector);\r
254 \r
255                                 scsiDev.data[idx+2] = cyl >> 16;\r
256                                 scsiDev.data[idx+3] = cyl >> 8;\r
257                                 scsiDev.data[idx+4] = cyl;\r
258 \r
259                                 memcpy(&scsiDev.data[idx+6], &scsiDev.data[idx+2], 3);\r
260                                 memcpy(&scsiDev.data[idx+9], &scsiDev.data[idx+2], 3);\r
261                         }\r
262 \r
263                         idx += sizeof(RigidDiskDriveGeometry);\r
264                         if (pageCode != 0x3f) break;\r
265                 }\r
266 \r
267                 case 0x08:\r
268                         pageIn(pc, idx, CachingPage, sizeof(CachingPage));\r
269                         idx += sizeof(CachingPage);\r
270                         if (pageCode != 0x3f) break;\r
271 \r
272                 case 0x0A:\r
273                         pageIn(pc, idx, ControlModePage, sizeof(ControlModePage));\r
274                         idx += sizeof(ControlModePage);\r
275                         break;\r
276 \r
277                 case 0x30:\r
278                         pageIn(pc, idx, AppleVendorPage, sizeof(AppleVendorPage));\r
279                         idx += sizeof(AppleVendorPage);\r
280                         break;\r
281 \r
282                 default:\r
283                         // Unknown Page Code\r
284                         pageFound = 0;\r
285                         scsiDev.status = CHECK_CONDITION;\r
286                         scsiDev.target->sense.code = ILLEGAL_REQUEST;\r
287                         scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;\r
288                         scsiDev.phase = STATUS;\r
289                 }\r
290 \r
291 \r
292                 if (idx > allocLength)\r
293                 {\r
294                         // Chop the reply off early if shorter length is requested\r
295                         idx = allocLength;\r
296                 }\r
297 \r
298                 if (pageFound)\r
299                 {\r
300                         // Go back and fill out the mode data length\r
301                         if (sixByteCmd)\r
302                         {\r
303                                 // Cannot currently exceed limits. yay\r
304                                 scsiDev.data[0] = idx - 1;\r
305                         }\r
306                         else\r
307                         {\r
308                                 scsiDev.data[0] = ((idx - 2) >> 8);\r
309                                 scsiDev.data[1] = (idx - 2);\r
310                         }\r
311 \r
312                         scsiDev.dataLen = idx;\r
313                         scsiDev.phase = DATA_IN;\r
314                 }\r
315                 else\r
316                 {\r
317                         // Page not found\r
318                         scsiDev.status = CHECK_CONDITION;\r
319                         scsiDev.target->sense.code = ILLEGAL_REQUEST;\r
320                         scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;\r
321                         scsiDev.phase = STATUS;\r
322                 }\r
323         }\r
324 }\r
325 \r
326 // Callback after the DATA OUT phase is complete.\r
327 static void doModeSelect(void)\r
328 {\r
329         if (scsiDev.status == GOOD) // skip if we've already encountered an error\r
330         {\r
331                 // scsiDev.dataLen bytes are in scsiDev.data\r
332 \r
333                 int idx;\r
334                 int blockDescLen;\r
335                 if (scsiDev.cdb[0] == 0x55)\r
336                 {\r
337                         blockDescLen =\r
338                                 (((uint16_t)scsiDev.data[6]) << 8) |scsiDev.data[7];\r
339                         idx = 8;\r
340                 }\r
341                 else\r
342                 {\r
343                         blockDescLen = scsiDev.data[3];\r
344                         idx = 4;\r
345                 }\r
346                 \r
347                 // The unwritten rule.  Blocksizes are normally set using the\r
348                 // block descriptor value, not by changing page 0x03.\r
349                 if (blockDescLen >= 8)\r
350                 {\r
351                         uint32_t bytesPerSector =\r
352                                 (((uint32_t)scsiDev.data[idx+5]) << 16) |\r
353                                 (((uint32_t)scsiDev.data[idx+6]) << 8) |\r
354                                 scsiDev.data[idx+7];\r
355                         if ((bytesPerSector < MIN_SECTOR_SIZE) ||\r
356                                 (bytesPerSector > MAX_SECTOR_SIZE))\r
357                         {\r
358                                 goto bad;\r
359                         }\r
360                         else if (bytesPerSector != scsiDev.target->cfg->bytesPerSector)\r
361                         {\r
362                                 // TODO REIMPLEMENT CONFIG SAVEconfig->bytesPerSector = bytesPerSector;\r
363                                 configSave();\r
364                         }\r
365                 }\r
366                 idx += blockDescLen;\r
367 \r
368                 while (idx < scsiDev.dataLen)\r
369                 {\r
370                         int pageLen = scsiDev.data[idx + 1];\r
371                         if (idx + 2 + pageLen > scsiDev.dataLen) goto bad;\r
372 \r
373                         int pageCode = scsiDev.data[idx] & 0x3F;\r
374                         switch (pageCode)\r
375                         {\r
376                         case 0x03: // Format Device Page\r
377                         {\r
378                                 if (pageLen != 0x16) goto bad;\r
379 \r
380                                 // Fill out the configured bytes-per-sector\r
381                                 uint16_t bytesPerSector =\r
382                                         (((uint16_t)scsiDev.data[idx+12]) << 8) |\r
383                                         scsiDev.data[idx+13];\r
384 \r
385                                 // Sane values only, ok ?\r
386                                 if ((bytesPerSector < MIN_SECTOR_SIZE) ||\r
387                                         (bytesPerSector > MAX_SECTOR_SIZE))\r
388                                 {\r
389                                         goto bad;\r
390                                 }\r
391 \r
392                                 // TODO CONFIGFAVE REIMPLEMENT config->bytesPerSector = bytesPerSector;\r
393                                 if (scsiDev.cdb[1] & 1) // SP Save Pages flag\r
394                                 {\r
395                                         configSave();\r
396                                 }\r
397                         }\r
398                         break;\r
399                         //default:\r
400                         \r
401                                 // Easiest to just ignore for now. We'll get here when changing\r
402                                 // the SCSI block size via the descriptor header.\r
403                         }\r
404                         idx += 2 + pageLen;\r
405                 }\r
406         }\r
407 \r
408         goto out;\r
409 bad:\r
410         scsiDev.status = CHECK_CONDITION;\r
411         scsiDev.target->sense.code = ILLEGAL_REQUEST;\r
412         scsiDev.target->sense.asc = INVALID_FIELD_IN_PARAMETER_LIST;\r
413 \r
414 out:\r
415         scsiDev.phase = STATUS;\r
416 }\r
417 \r
418 int scsiModeCommand()\r
419 {\r
420         int commandHandled = 1;\r
421 \r
422         uint8 command = scsiDev.cdb[0];\r
423 \r
424         // We don't currently support the setting of any parameters.\r
425         // (ie. no MODE SELECT(6) or MODE SELECT(10) commands)\r
426 \r
427         if (command == 0x1A)\r
428         {\r
429                 // MODE SENSE(6)\r
430                 int dbd = scsiDev.cdb[1] & 0x08; // Disable block descriptors\r
431                 int pc = scsiDev.cdb[2] >> 6; // Page Control\r
432                 int pageCode = scsiDev.cdb[2] & 0x3F;\r
433                 int allocLength = scsiDev.cdb[4];\r
434                 if (allocLength == 0) allocLength = 256;\r
435                 doModeSense(1, dbd, pc, pageCode, allocLength);\r
436         }\r
437         else if (command == 0x5A)\r
438         {\r
439                 // MODE SENSE(10)\r
440                 int dbd = scsiDev.cdb[1] & 0x08; // Disable block descriptors\r
441                 int pc = scsiDev.cdb[2] >> 6; // Page Control\r
442                 int pageCode = scsiDev.cdb[2] & 0x3F;\r
443                 int allocLength =\r
444                         (((uint16) scsiDev.cdb[7]) << 8) +\r
445                         scsiDev.cdb[8];\r
446                 doModeSense(0, dbd, pc, pageCode, allocLength);\r
447         }\r
448         else if (command == 0x15)\r
449         {\r
450                 // MODE SELECT(6)\r
451                 int len = scsiDev.cdb[4];\r
452                 if (len == 0)\r
453                 {\r
454                         // If len == 0, then transfer no data. From the SCSI 2 standard:\r
455                         //      A parameter list length of zero indicates that no data shall\r
456                         //      be transferred. This condition shall not be considered as an\r
457                         //              error.\r
458                         scsiDev.phase = STATUS;\r
459                 }\r
460                 else\r
461                 {\r
462                         scsiDev.dataLen = len;\r
463                         scsiDev.phase = DATA_OUT;\r
464                         scsiDev.postDataOutHook = doModeSelect;\r
465                 }\r
466         }\r
467         else if (command == 0x55)\r
468         {\r
469                 // MODE SELECT(10)\r
470                 int allocLength = (((uint16) scsiDev.cdb[7]) << 8) + scsiDev.cdb[8];\r
471                 if (allocLength == 0)\r
472                 {\r
473                         // If len == 0, then transfer no data. From the SCSI 2 standard:\r
474                         //      A parameter list length of zero indicates that no data shall\r
475                         //      be transferred. This condition shall not be considered as an\r
476                         //              error.\r
477                         scsiDev.phase = STATUS;\r
478                 }\r
479                 else\r
480                 {\r
481                         scsiDev.dataLen = allocLength;\r
482                         scsiDev.phase = DATA_OUT;\r
483                         scsiDev.postDataOutHook = doModeSelect;\r
484                 }\r
485         }\r
486         else\r
487         {\r
488                 commandHandled = 0;\r
489         }\r
490 \r
491         return commandHandled;\r
492 }\r
493 \r
494 \r