5538cb4657681ace73bf6b727110f32a5d2fe935
[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->liveCfg.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->liveCfg.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(\r
254                                         getScsiCapacity(\r
255                                                 scsiDev.target->cfg->sdSectorStart,\r
256                                                 scsiDev.target->liveCfg.bytesPerSector,\r
257                                                 scsiDev.target->cfg->scsiSectors),\r
258                                         &cyl,\r
259                                         &head,\r
260                                         &sector);\r
261 \r
262                                 scsiDev.data[idx+2] = cyl >> 16;\r
263                                 scsiDev.data[idx+3] = cyl >> 8;\r
264                                 scsiDev.data[idx+4] = cyl;\r
265 \r
266                                 memcpy(&scsiDev.data[idx+6], &scsiDev.data[idx+2], 3);\r
267                                 memcpy(&scsiDev.data[idx+9], &scsiDev.data[idx+2], 3);\r
268                         }\r
269 \r
270                         idx += sizeof(RigidDiskDriveGeometry);\r
271                         if (pageCode != 0x3f) break;\r
272                 }\r
273 \r
274                 case 0x08:\r
275                         pageIn(pc, idx, CachingPage, sizeof(CachingPage));\r
276                         idx += sizeof(CachingPage);\r
277                         if (pageCode != 0x3f) break;\r
278 \r
279                 case 0x0A:\r
280                         pageIn(pc, idx, ControlModePage, sizeof(ControlModePage));\r
281                         idx += sizeof(ControlModePage);\r
282                         break;\r
283 \r
284                 case 0x30:\r
285                         pageIn(pc, idx, AppleVendorPage, sizeof(AppleVendorPage));\r
286                         idx += sizeof(AppleVendorPage);\r
287                         break;\r
288 \r
289                 default:\r
290                         // Unknown Page Code\r
291                         pageFound = 0;\r
292                         scsiDev.status = CHECK_CONDITION;\r
293                         scsiDev.target->sense.code = ILLEGAL_REQUEST;\r
294                         scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;\r
295                         scsiDev.phase = STATUS;\r
296                 }\r
297 \r
298 \r
299                 if (idx > allocLength)\r
300                 {\r
301                         // Chop the reply off early if shorter length is requested\r
302                         idx = allocLength;\r
303                 }\r
304 \r
305                 if (pageFound)\r
306                 {\r
307                         // Go back and fill out the mode data length\r
308                         if (sixByteCmd)\r
309                         {\r
310                                 // Cannot currently exceed limits. yay\r
311                                 scsiDev.data[0] = idx - 1;\r
312                         }\r
313                         else\r
314                         {\r
315                                 scsiDev.data[0] = ((idx - 2) >> 8);\r
316                                 scsiDev.data[1] = (idx - 2);\r
317                         }\r
318 \r
319                         scsiDev.dataLen = idx;\r
320                         scsiDev.phase = DATA_IN;\r
321                 }\r
322                 else\r
323                 {\r
324                         // Page not found\r
325                         scsiDev.status = CHECK_CONDITION;\r
326                         scsiDev.target->sense.code = ILLEGAL_REQUEST;\r
327                         scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;\r
328                         scsiDev.phase = STATUS;\r
329                 }\r
330         }\r
331 }\r
332 \r
333 // Callback after the DATA OUT phase is complete.\r
334 static void doModeSelect(void)\r
335 {\r
336         if (scsiDev.status == GOOD) // skip if we've already encountered an error\r
337         {\r
338                 // scsiDev.dataLen bytes are in scsiDev.data\r
339 \r
340                 int idx;\r
341                 int blockDescLen;\r
342                 if (scsiDev.cdb[0] == 0x55)\r
343                 {\r
344                         blockDescLen =\r
345                                 (((uint16_t)scsiDev.data[6]) << 8) |scsiDev.data[7];\r
346                         idx = 8;\r
347                 }\r
348                 else\r
349                 {\r
350                         blockDescLen = scsiDev.data[3];\r
351                         idx = 4;\r
352                 }\r
353 \r
354                 // The unwritten rule.  Blocksizes are normally set using the\r
355                 // block descriptor value, not by changing page 0x03.\r
356                 if (blockDescLen >= 8)\r
357                 {\r
358                         uint32_t bytesPerSector =\r
359                                 (((uint32_t)scsiDev.data[idx+5]) << 16) |\r
360                                 (((uint32_t)scsiDev.data[idx+6]) << 8) |\r
361                                 scsiDev.data[idx+7];\r
362                         if ((bytesPerSector < MIN_SECTOR_SIZE) ||\r
363                                 (bytesPerSector > MAX_SECTOR_SIZE))\r
364                         {\r
365                                 goto bad;\r
366                         }\r
367                         else\r
368                         {\r
369                                 scsiDev.target->liveCfg.bytesPerSector = bytesPerSector;\r
370                                 if (bytesPerSector != scsiDev.target->cfg->bytesPerSector)\r
371                                 {\r
372                                         configSave(scsiDev.target->targetId, bytesPerSector);\r
373                                 }\r
374                         }\r
375                 }\r
376                 idx += blockDescLen;\r
377 \r
378                 while (idx < scsiDev.dataLen)\r
379                 {\r
380                         int pageLen = scsiDev.data[idx + 1];\r
381                         if (idx + 2 + pageLen > scsiDev.dataLen) goto bad;\r
382 \r
383                         int pageCode = scsiDev.data[idx] & 0x3F;\r
384                         switch (pageCode)\r
385                         {\r
386                         case 0x03: // Format Device Page\r
387                         {\r
388                                 if (pageLen != 0x16) goto bad;\r
389 \r
390                                 // Fill out the configured bytes-per-sector\r
391                                 uint16_t bytesPerSector =\r
392                                         (((uint16_t)scsiDev.data[idx+12]) << 8) |\r
393                                         scsiDev.data[idx+13];\r
394 \r
395                                 // Sane values only, ok ?\r
396                                 if ((bytesPerSector < MIN_SECTOR_SIZE) ||\r
397                                         (bytesPerSector > MAX_SECTOR_SIZE))\r
398                                 {\r
399                                         goto bad;\r
400                                 }\r
401 \r
402                                 scsiDev.target->liveCfg.bytesPerSector = bytesPerSector;\r
403                                 if (scsiDev.cdb[1] & 1) // SP Save Pages flag\r
404                                 {\r
405                                         configSave(scsiDev.target->targetId, bytesPerSector);\r
406                                 }\r
407                         }\r
408                         break;\r
409                         //default:\r
410 \r
411                                 // Easiest to just ignore for now. We'll get here when changing\r
412                                 // the SCSI block size via the descriptor header.\r
413                         }\r
414                         idx += 2 + pageLen;\r
415                 }\r
416         }\r
417 \r
418         goto out;\r
419 bad:\r
420         scsiDev.status = CHECK_CONDITION;\r
421         scsiDev.target->sense.code = ILLEGAL_REQUEST;\r
422         scsiDev.target->sense.asc = INVALID_FIELD_IN_PARAMETER_LIST;\r
423 \r
424 out:\r
425         scsiDev.phase = STATUS;\r
426 }\r
427 \r
428 int scsiModeCommand()\r
429 {\r
430         int commandHandled = 1;\r
431 \r
432         uint8 command = scsiDev.cdb[0];\r
433 \r
434         // We don't currently support the setting of any parameters.\r
435         // (ie. no MODE SELECT(6) or MODE SELECT(10) commands)\r
436 \r
437         if (command == 0x1A)\r
438         {\r
439                 // MODE SENSE(6)\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 = scsiDev.cdb[4];\r
444                 if (allocLength == 0) allocLength = 256;\r
445                 doModeSense(1, dbd, pc, pageCode, allocLength);\r
446         }\r
447         else if (command == 0x5A)\r
448         {\r
449                 // MODE SENSE(10)\r
450                 int dbd = scsiDev.cdb[1] & 0x08; // Disable block descriptors\r
451                 int pc = scsiDev.cdb[2] >> 6; // Page Control\r
452                 int pageCode = scsiDev.cdb[2] & 0x3F;\r
453                 int allocLength =\r
454                         (((uint16) scsiDev.cdb[7]) << 8) +\r
455                         scsiDev.cdb[8];\r
456                 doModeSense(0, dbd, pc, pageCode, allocLength);\r
457         }\r
458         else if (command == 0x15)\r
459         {\r
460                 // MODE SELECT(6)\r
461                 int len = scsiDev.cdb[4];\r
462                 if (len == 0)\r
463                 {\r
464                         // If len == 0, then transfer no data. From the SCSI 2 standard:\r
465                         //      A parameter list length of zero indicates that no data shall\r
466                         //      be transferred. This condition shall not be considered as an\r
467                         //              error.\r
468                         scsiDev.phase = STATUS;\r
469                 }\r
470                 else\r
471                 {\r
472                         scsiDev.dataLen = len;\r
473                         scsiDev.phase = DATA_OUT;\r
474                         scsiDev.postDataOutHook = doModeSelect;\r
475                 }\r
476         }\r
477         else if (command == 0x55)\r
478         {\r
479                 // MODE SELECT(10)\r
480                 int allocLength = (((uint16) scsiDev.cdb[7]) << 8) + scsiDev.cdb[8];\r
481                 if (allocLength == 0)\r
482                 {\r
483                         // If len == 0, then transfer no data. From the SCSI 2 standard:\r
484                         //      A parameter list length of zero indicates that no data shall\r
485                         //      be transferred. This condition shall not be considered as an\r
486                         //              error.\r
487                         scsiDev.phase = STATUS;\r
488                 }\r
489                 else\r
490                 {\r
491                         scsiDev.dataLen = allocLength;\r
492                         scsiDev.phase = DATA_OUT;\r
493                         scsiDev.postDataOutHook = doModeSelect;\r
494                 }\r
495         }\r
496         else\r
497         {\r
498                 commandHandled = 0;\r
499         }\r
500 \r
501         return commandHandled;\r
502 }\r
503 \r
504 \r