Support VMS 5.5_2 mode page bits.
[SCSI2SD-V6.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 \r
31 // VMS 5.5-2 is very particular regarding the mode page values.\r
32 // The required values for a SCSI2/NoTCQ device are:\r
33 // AWRE=0 ARRE=0 TB=1 RC=0 EER=? PER=1 DTE=1 DCR=?\r
34 // See ftp://www.digiater.nl/openvms/decus/vms94b/net94b/scsi_params_dkdriver.txt\r
35 // X-Newsgroups: comp.os.vms\r
36 // Subject: Re: VMS 6.1 vs. Seagate Disk Drives\r
37 // Message-Id: <32g87h$8q@nntpd.lkg.dec.com>\r
38 // From: weber@evms.enet.dec.com (Ralph O. Weber -- OpenVMS AXP)\r
39 // Date: 12 Aug 1994 16:32:49 GMT\r
40 0x26,\r
41 \r
42 0x00, // Don't try recovery algorithm during reads\r
43 0x00, // Correction span 0\r
44 0x00, // Head offset count 0,\r
45 0x00, // Data strobe offset count 0,\r
46 0x00, // Reserved\r
47 0x00, // Don't try recovery algorithm during writes\r
48 0x00, // Reserved\r
49 0x00, 0x00 // Recovery time limit 0 (use default)*/\r
50 };\r
51 \r
52 static const uint8 DisconnectReconnectPage[] =\r
53 {\r
54 0x02, // Page code\r
55 0x0E, // Page length\r
56 0, // Buffer full ratio\r
57 0, // Buffer empty ratio\r
58 0x00, 10, // Bus inactivity limit, 100us increments. Allow 1ms.\r
59 0x00, 0x00, // Disconnect time limit\r
60 0x00, 0x00, // Connect time limit\r
61 0x00, 0x00, // Maximum burst size\r
62 0x00 ,// DTDC. Not used.\r
63 0x00, 0x00, 0x00 // Reserved\r
64 };\r
65 \r
66 static const uint8 FormatDevicePage[] =\r
67 {\r
68 0x03 | 0x80, // Page code | PS (persist) bit.\r
69 0x16, // Page length\r
70 0x00, 0x00, // Single zone\r
71 0x00, 0x00, // No alternate sectors\r
72 0x00, 0x00, // No alternate tracks\r
73 0x00, 0x00, // No alternate tracks per lun\r
74 0x00, SCSI_SECTORS_PER_TRACK, // Sectors per track\r
75 0xFF, 0xFF, // Data bytes per physical sector. Configurable.\r
76 0x00, 0x01, // Interleave\r
77 0x00, 0x00, // Track skew factor\r
78 0x00, 0x00, // Cylinder skew factor\r
79 0xC0, // SSEC(set) HSEC(set) RMB SURF\r
80 0x00, 0x00, 0x00 // Reserved\r
81 };\r
82 \r
83 static const uint8 RigidDiskDriveGeometry[] =\r
84 {\r
85 0x04, // Page code\r
86 0x16, // Page length\r
87 0xFF, 0xFF, 0xFF, // Number of cylinders\r
88 SCSI_HEADS_PER_CYLINDER, // Number of heads\r
89 0xFF, 0xFF, 0xFF, // Starting cylinder-write precompensation\r
90 0xFF, 0xFF, 0xFF, // Starting cylinder-reduced write current\r
91 0x00, 0x1, // Drive step rate (units of 100ns)\r
92 0x00, 0x00, 0x00, // Landing zone cylinder\r
93 0x00, // RPL\r
94 0x00, // Rotational offset\r
95 0x00, // Reserved\r
96 5400 >> 8, 5400 & 0xFF, // Medium rotation rate (RPM)\r
97 0x00, 0x00 // Reserved\r
98 };\r
99 \r
100 static const uint8 CachingPage[] =\r
101 {\r
102 0x08, // Page Code\r
103 0x0A, // Page length\r
104 0x01, // Read cache disable\r
105 0x00, // No useful rention policy.\r
106 0x00, 0x00, // Pre-fetch always disabled\r
107 0x00, 0x00, // Minimum pre-fetch\r
108 0x00, 0x00, // Maximum pre-fetch\r
109 0x00, 0x00, // Maximum pre-fetch ceiling\r
110 };\r
111 \r
112 static const uint8 ControlModePage[] =\r
113 {\r
114 0x0A, // Page code\r
115 0x06, // Page length\r
116 0x00, // No logging\r
117 0x01, // Disable tagged queuing\r
118 0x00, // No async event notifications\r
119 0x00, // Reserved\r
120 0x00, 0x00 // AEN holdoff period.\r
121 };\r
122 \r
123 // Allow Apple 68k Drive Setup to format this drive.\r
124 // Code\r
125 static const uint8 AppleVendorPage[] =\r
126 {\r
127 0x30, // Page code\r
128 28, // Page length\r
129 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\r
130 'A','P','P','L','E',' ','C','O','M','P','U','T','E','R',',',' ','I','N','C','.'\r
131 };\r
132 \r
133 static void pageIn(int pc, int dataIdx, const uint8* pageData, int pageLen)\r
134 {\r
135         memcpy(&scsiDev.data[dataIdx], pageData, pageLen);\r
136 \r
137         if (pc == 0x01) // Mask out (un)changable values\r
138         {\r
139                 memset(&scsiDev.data[dataIdx+2], 0, pageLen - 2);\r
140         }\r
141 }\r
142 \r
143 static void doModeSense(\r
144         int sixByteCmd, int dbd, int pc, int pageCode, int allocLength)\r
145 {\r
146         if (pc == 0x03) // Saved Values not supported.\r
147         {\r
148                 scsiDev.status = CHECK_CONDITION;\r
149                 scsiDev.target->sense.code = ILLEGAL_REQUEST;\r
150                 scsiDev.target->sense.asc = SAVING_PARAMETERS_NOT_SUPPORTED;\r
151                 scsiDev.phase = STATUS;\r
152         }\r
153         else\r
154         {\r
155                 int pageFound = 1;\r
156 \r
157                 ////////////// Mode Parameter Header\r
158                 ////////////////////////////////////\r
159 \r
160                 // Skip the Mode Data Length, we set that last.\r
161                 int idx = 1;\r
162                 if (!sixByteCmd) ++idx;\r
163 \r
164                 uint8_t mediumType = 0;\r
165                 uint8_t deviceSpecificParam = 0;\r
166                 uint8_t density = 0;\r
167                 switch (scsiDev.target->cfg->deviceType == CONFIG_OPTICAL)\r
168                 {\r
169                 case CONFIG_FIXED:\r
170                 case CONFIG_REMOVEABLE:\r
171                         mediumType = 0; // We should support various floppy types here!\r
172                         // Contains cache bits (0) and a Write-Protect bit.\r
173                         deviceSpecificParam =\r
174                                 (blockDev.state & DISK_WP) ? 0x80 : 0;\r
175                         density = 0; // reserved for direct access\r
176                         break;\r
177 \r
178                 case CONFIG_FLOPPY_14MB:\r
179                         mediumType = 0x1E; // 90mm/3.5"\r
180                         deviceSpecificParam =\r
181                                 (blockDev.state & DISK_WP) ? 0x80 : 0;\r
182                         density = 0; // reserved for direct access\r
183                         break;\r
184 \r
185                 case CONFIG_OPTICAL:\r
186                         mediumType = 0x02; // 120mm CDROM, data only.\r
187                         deviceSpecificParam = 0;\r
188                         density = 0x01; // User data only, 2048bytes per sector.\r
189                         break;\r
190 \r
191                 };\r
192 \r
193                 scsiDev.data[idx++] = mediumType;\r
194                 scsiDev.data[idx++] = deviceSpecificParam;\r
195 \r
196                 if (sixByteCmd)\r
197                 {\r
198                         if (dbd)\r
199                         {\r
200                                 scsiDev.data[idx++] = 0; // No block descriptor\r
201                         }\r
202                         else\r
203                         {\r
204                                 // One block descriptor of length 8 bytes.\r
205                                 scsiDev.data[idx++] = 8;\r
206                         }\r
207                 }\r
208                 else\r
209                 {\r
210                         scsiDev.data[idx++] = 0; // Reserved\r
211                         scsiDev.data[idx++] = 0; // Reserved\r
212                         if (dbd)\r
213                         {\r
214                                 scsiDev.data[idx++] = 0; // No block descriptor\r
215                                 scsiDev.data[idx++] = 0; // No block descriptor\r
216                         }\r
217                         else\r
218                         {\r
219                                 // One block descriptor of length 8 bytes.\r
220                                 scsiDev.data[idx++] = 0;\r
221                                 scsiDev.data[idx++] = 8;\r
222                         }\r
223                 }\r
224 \r
225                 ////////////// Block Descriptor\r
226                 ////////////////////////////////////\r
227                 if (!dbd)\r
228                 {\r
229                         scsiDev.data[idx++] = density;\r
230                         // Number of blocks\r
231                         // Zero == all remaining blocks shall have the medium\r
232                         // characteristics specified.\r
233                         scsiDev.data[idx++] = 0;\r
234                         scsiDev.data[idx++] = 0;\r
235                         scsiDev.data[idx++] = 0;\r
236 \r
237                         scsiDev.data[idx++] = 0; // reserved\r
238 \r
239                         // Block length\r
240                         uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;\r
241                         scsiDev.data[idx++] = bytesPerSector >> 16;\r
242                         scsiDev.data[idx++] = bytesPerSector >> 8;\r
243                         scsiDev.data[idx++] = bytesPerSector & 0xFF;\r
244                 }\r
245 \r
246                 switch (pageCode)\r
247                 {\r
248                 case 0x3F:\r
249                         // EVERYTHING\r
250 \r
251                 case 0x01:\r
252                         pageIn(pc, idx, ReadWriteErrorRecoveryPage, sizeof(ReadWriteErrorRecoveryPage));\r
253                         idx += sizeof(ReadWriteErrorRecoveryPage);\r
254                         if (pageCode != 0x3f) break;\r
255 \r
256                 case 0x02:\r
257                         pageIn(pc, idx, DisconnectReconnectPage, sizeof(DisconnectReconnectPage));\r
258                         idx += sizeof(DisconnectReconnectPage);\r
259                         if (pageCode != 0x3f) break;\r
260 \r
261                 case 0x03:\r
262                         pageIn(pc, idx, FormatDevicePage, sizeof(FormatDevicePage));\r
263                         if (pc != 0x01)\r
264                         {\r
265                                 // Fill out the configured bytes-per-sector\r
266                                 uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;\r
267                                 scsiDev.data[idx+12] = bytesPerSector >> 8;\r
268                                 scsiDev.data[idx+13] = bytesPerSector & 0xFF;\r
269                         }\r
270                         else\r
271                         {\r
272                                 // Set a mask for the changeable values.\r
273                                 scsiDev.data[idx+12] = 0xFF;\r
274                                 scsiDev.data[idx+13] = 0xFF;\r
275                         }\r
276 \r
277                         idx += sizeof(FormatDevicePage);\r
278                         if (pageCode != 0x3f) break;\r
279 \r
280                 case 0x04:\r
281                 {\r
282                         pageIn(pc, idx, RigidDiskDriveGeometry, sizeof(RigidDiskDriveGeometry));\r
283 \r
284                         if (pc != 0x01)\r
285                         {\r
286                                 // Need to fill out the number of cylinders.\r
287                                 uint32 cyl;\r
288                                 uint8 head;\r
289                                 uint32 sector;\r
290                                 LBA2CHS(\r
291                                         getScsiCapacity(\r
292                                                 scsiDev.target->cfg->sdSectorStart,\r
293                                                 scsiDev.target->liveCfg.bytesPerSector,\r
294                                                 scsiDev.target->cfg->scsiSectors),\r
295                                         &cyl,\r
296                                         &head,\r
297                                         &sector);\r
298 \r
299                                 scsiDev.data[idx+2] = cyl >> 16;\r
300                                 scsiDev.data[idx+3] = cyl >> 8;\r
301                                 scsiDev.data[idx+4] = cyl;\r
302 \r
303                                 memcpy(&scsiDev.data[idx+6], &scsiDev.data[idx+2], 3);\r
304                                 memcpy(&scsiDev.data[idx+9], &scsiDev.data[idx+2], 3);\r
305                         }\r
306 \r
307                         idx += sizeof(RigidDiskDriveGeometry);\r
308                         if (pageCode != 0x3f) break;\r
309                 }\r
310 \r
311                 case 0x08:\r
312                         pageIn(pc, idx, CachingPage, sizeof(CachingPage));\r
313                         idx += sizeof(CachingPage);\r
314                         if (pageCode != 0x3f) break;\r
315 \r
316                 case 0x0A:\r
317                         pageIn(pc, idx, ControlModePage, sizeof(ControlModePage));\r
318                         idx += sizeof(ControlModePage);\r
319                         if (pageCode != 0x3f) break;\r
320 \r
321                 case 0x30:\r
322                         pageIn(pc, idx, AppleVendorPage, sizeof(AppleVendorPage));\r
323                         idx += sizeof(AppleVendorPage);\r
324                         break;\r
325 \r
326                 default:\r
327                         // Unknown Page Code\r
328                         pageFound = 0;\r
329                         scsiDev.status = CHECK_CONDITION;\r
330                         scsiDev.target->sense.code = ILLEGAL_REQUEST;\r
331                         scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;\r
332                         scsiDev.phase = STATUS;\r
333                 }\r
334 \r
335 \r
336                 if (idx > allocLength)\r
337                 {\r
338                         // Chop the reply off early if shorter length is requested\r
339                         idx = allocLength;\r
340                 }\r
341 \r
342                 if (pageFound)\r
343                 {\r
344                         // Go back and fill out the mode data length\r
345                         if (sixByteCmd)\r
346                         {\r
347                                 // Cannot currently exceed limits. yay\r
348                                 scsiDev.data[0] = idx - 1;\r
349                         }\r
350                         else\r
351                         {\r
352                                 scsiDev.data[0] = ((idx - 2) >> 8);\r
353                                 scsiDev.data[1] = (idx - 2);\r
354                         }\r
355 \r
356                         scsiDev.dataLen = idx;\r
357                         scsiDev.phase = DATA_IN;\r
358                 }\r
359                 else\r
360                 {\r
361                         // Page not found\r
362                         scsiDev.status = CHECK_CONDITION;\r
363                         scsiDev.target->sense.code = ILLEGAL_REQUEST;\r
364                         scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;\r
365                         scsiDev.phase = STATUS;\r
366                 }\r
367         }\r
368 }\r
369 \r
370 // Callback after the DATA OUT phase is complete.\r
371 static void doModeSelect(void)\r
372 {\r
373         if (scsiDev.status == GOOD) // skip if we've already encountered an error\r
374         {\r
375                 // scsiDev.dataLen bytes are in scsiDev.data\r
376 \r
377                 int idx;\r
378                 int blockDescLen;\r
379                 if (scsiDev.cdb[0] == 0x55)\r
380                 {\r
381                         blockDescLen =\r
382                                 (((uint16_t)scsiDev.data[6]) << 8) |scsiDev.data[7];\r
383                         idx = 8;\r
384                 }\r
385                 else\r
386                 {\r
387                         blockDescLen = scsiDev.data[3];\r
388                         idx = 4;\r
389                 }\r
390 \r
391                 // The unwritten rule.  Blocksizes are normally set using the\r
392                 // block descriptor value, not by changing page 0x03.\r
393                 if (blockDescLen >= 8)\r
394                 {\r
395                         uint32_t bytesPerSector =\r
396                                 (((uint32_t)scsiDev.data[idx+5]) << 16) |\r
397                                 (((uint32_t)scsiDev.data[idx+6]) << 8) |\r
398                                 scsiDev.data[idx+7];\r
399                         if ((bytesPerSector < MIN_SECTOR_SIZE) ||\r
400                                 (bytesPerSector > MAX_SECTOR_SIZE))\r
401                         {\r
402                                 goto bad;\r
403                         }\r
404                         else\r
405                         {\r
406                                 scsiDev.target->liveCfg.bytesPerSector = bytesPerSector;\r
407                                 if (bytesPerSector != scsiDev.target->cfg->bytesPerSector)\r
408                                 {\r
409                                         configSave(scsiDev.target->targetId, bytesPerSector);\r
410                                 }\r
411                         }\r
412                 }\r
413                 idx += blockDescLen;\r
414 \r
415                 while (idx < scsiDev.dataLen)\r
416                 {\r
417                         int pageLen = scsiDev.data[idx + 1];\r
418                         if (idx + 2 + pageLen > scsiDev.dataLen) goto bad;\r
419 \r
420                         int pageCode = scsiDev.data[idx] & 0x3F;\r
421                         switch (pageCode)\r
422                         {\r
423                         case 0x03: // Format Device Page\r
424                         {\r
425                                 if (pageLen != 0x16) goto bad;\r
426 \r
427                                 // Fill out the configured bytes-per-sector\r
428                                 uint16_t bytesPerSector =\r
429                                         (((uint16_t)scsiDev.data[idx+12]) << 8) |\r
430                                         scsiDev.data[idx+13];\r
431 \r
432                                 // Sane values only, ok ?\r
433                                 if ((bytesPerSector < MIN_SECTOR_SIZE) ||\r
434                                         (bytesPerSector > MAX_SECTOR_SIZE))\r
435                                 {\r
436                                         goto bad;\r
437                                 }\r
438 \r
439                                 scsiDev.target->liveCfg.bytesPerSector = bytesPerSector;\r
440                                 if (scsiDev.cdb[1] & 1) // SP Save Pages flag\r
441                                 {\r
442                                         configSave(scsiDev.target->targetId, bytesPerSector);\r
443                                 }\r
444                         }\r
445                         break;\r
446                         //default:\r
447 \r
448                                 // Easiest to just ignore for now. We'll get here when changing\r
449                                 // the SCSI block size via the descriptor header.\r
450                         }\r
451                         idx += 2 + pageLen;\r
452                 }\r
453         }\r
454 \r
455         goto out;\r
456 bad:\r
457         scsiDev.status = CHECK_CONDITION;\r
458         scsiDev.target->sense.code = ILLEGAL_REQUEST;\r
459         scsiDev.target->sense.asc = INVALID_FIELD_IN_PARAMETER_LIST;\r
460 \r
461 out:\r
462         scsiDev.phase = STATUS;\r
463 }\r
464 \r
465 int scsiModeCommand()\r
466 {\r
467         int commandHandled = 1;\r
468 \r
469         uint8 command = scsiDev.cdb[0];\r
470 \r
471         // We don't currently support the setting of any parameters.\r
472         // (ie. no MODE SELECT(6) or MODE SELECT(10) commands)\r
473 \r
474         if (command == 0x1A)\r
475         {\r
476                 // MODE SENSE(6)\r
477                 int dbd = scsiDev.cdb[1] & 0x08; // Disable block descriptors\r
478                 int pc = scsiDev.cdb[2] >> 6; // Page Control\r
479                 int pageCode = scsiDev.cdb[2] & 0x3F;\r
480                 int allocLength = scsiDev.cdb[4];\r
481                 if (allocLength == 0) allocLength = 256;\r
482                 doModeSense(1, dbd, pc, pageCode, allocLength);\r
483         }\r
484         else if (command == 0x5A)\r
485         {\r
486                 // MODE SENSE(10)\r
487                 int dbd = scsiDev.cdb[1] & 0x08; // Disable block descriptors\r
488                 int pc = scsiDev.cdb[2] >> 6; // Page Control\r
489                 int pageCode = scsiDev.cdb[2] & 0x3F;\r
490                 int allocLength =\r
491                         (((uint16) scsiDev.cdb[7]) << 8) +\r
492                         scsiDev.cdb[8];\r
493                 doModeSense(0, dbd, pc, pageCode, allocLength);\r
494         }\r
495         else if (command == 0x15)\r
496         {\r
497                 // MODE SELECT(6)\r
498                 int len = scsiDev.cdb[4];\r
499                 if (len == 0)\r
500                 {\r
501                         // If len == 0, then transfer no data. From the SCSI 2 standard:\r
502                         //      A parameter list length of zero indicates that no data shall\r
503                         //      be transferred. This condition shall not be considered as an\r
504                         //              error.\r
505                         scsiDev.phase = STATUS;\r
506                 }\r
507                 else\r
508                 {\r
509                         scsiDev.dataLen = len;\r
510                         scsiDev.phase = DATA_OUT;\r
511                         scsiDev.postDataOutHook = doModeSelect;\r
512                 }\r
513         }\r
514         else if (command == 0x55)\r
515         {\r
516                 // MODE SELECT(10)\r
517                 int allocLength = (((uint16) scsiDev.cdb[7]) << 8) + scsiDev.cdb[8];\r
518                 if (allocLength == 0)\r
519                 {\r
520                         // If len == 0, then transfer no data. From the SCSI 2 standard:\r
521                         //      A parameter list length of zero indicates that no data shall\r
522                         //      be transferred. This condition shall not be considered as an\r
523                         //              error.\r
524                         scsiDev.phase = STATUS;\r
525                 }\r
526                 else\r
527                 {\r
528                         scsiDev.dataLen = allocLength;\r
529                         scsiDev.phase = DATA_OUT;\r
530                         scsiDev.postDataOutHook = doModeSelect;\r
531                 }\r
532         }\r
533         else\r
534         {\r
535                 commandHandled = 0;\r
536         }\r
537 \r
538         return commandHandled;\r
539 }\r
540 \r
541 \r