diagnostic.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // Copyright (C) 2013 Michael McMaster <michael@codesrc.com>
  2. //
  3. // This file is part of SCSI2SD.
  4. //
  5. // SCSI2SD is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // SCSI2SD is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with SCSI2SD. If not, see <http://www.gnu.org/licenses/>.
  17. #include "scsi.h"
  18. #include "diagnostic.h"
  19. #include <string.h>
  20. static const uint8_t SupportedDiagnosticPages[] =
  21. {
  22. 0x00, // Page Code
  23. 0x00, // Reserved
  24. 0x02, // Page length
  25. 0x00, // Support "Supported diagnostic page"
  26. 0x40 // Support "Translate address page"
  27. };
  28. void scsiSendDiagnostic()
  29. {
  30. // SEND DIAGNOSTIC
  31. // Pretend to do self-test. Actual data is returned via the
  32. // RECEIVE DIAGNOSTIC RESULTS command.
  33. int selfTest = scsiDev.cdb[1] & 0x04;
  34. uint32_t paramLength =
  35. (((uint32_t) scsiDev.cdb[3]) << 8) +
  36. scsiDev.cdb[4];
  37. if (!selfTest)
  38. {
  39. // Initiator sends us page data.
  40. scsiDev.dataLen = paramLength;
  41. scsiDev.phase = DATA_OUT;
  42. if (scsiDev.dataLen > sizeof (scsiDev.data))
  43. {
  44. // Nowhere to store this data!
  45. // Shouldn't happen - our buffer should be many magnitudes larger
  46. // than the required size for diagnostic parameters.
  47. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  48. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  49. scsiDev.status = CHECK_CONDITION;
  50. scsiDev.phase = STATUS;
  51. }
  52. }
  53. else
  54. {
  55. // Default command result will be a status of GOOD anyway.
  56. }
  57. }
  58. void scsiReceiveDiagnostic()
  59. {
  60. // RECEIVE DIAGNOSTIC RESULTS
  61. // We assume scsiDev.data contains the contents of a previous
  62. // SEND DIAGNOSTICS command. We only care about the page-code part
  63. // of the parameter list.
  64. uint8_t pageCode = scsiDev.data[0];
  65. int allocLength =
  66. (((uint16_t) scsiDev.cdb[3]) << 8) +
  67. scsiDev.cdb[4];
  68. if (pageCode == 0x00)
  69. {
  70. memcpy(
  71. scsiDev.data,
  72. SupportedDiagnosticPages,
  73. sizeof(SupportedDiagnosticPages));
  74. scsiDev.dataLen = sizeof(SupportedDiagnosticPages);
  75. scsiDev.phase = DATA_IN;
  76. }
  77. else if (pageCode == 0x40)
  78. {
  79. // Translate between logical block address, physical sector address, or
  80. // physical bytes.
  81. uint8_t suppliedFmt = scsiDev.data[4] & 0x7;
  82. uint8_t translateFmt = scsiDev.data[5] & 0x7;
  83. // Convert each supplied address back to a simple
  84. // 64bit linear address, then convert back again.
  85. uint64_t fromByteAddr =
  86. scsiByteAddress(
  87. scsiDev.target->liveCfg.bytesPerSector,
  88. scsiDev.target->cfg->headsPerCylinder,
  89. scsiDev.target->cfg->sectorsPerTrack,
  90. suppliedFmt,
  91. &scsiDev.data[6]);
  92. scsiSaveByteAddress(
  93. scsiDev.target->liveCfg.bytesPerSector,
  94. scsiDev.target->cfg->headsPerCylinder,
  95. scsiDev.target->cfg->sectorsPerTrack,
  96. translateFmt,
  97. fromByteAddr,
  98. &scsiDev.data[6]);
  99. // Fill out the rest of the response.
  100. // (Clear out any optional bits).
  101. scsiDev.data[4] = suppliedFmt;
  102. scsiDev.data[5] = translateFmt;
  103. scsiDev.dataLen = 14;
  104. scsiDev.phase = DATA_IN;
  105. }
  106. else
  107. {
  108. // error.
  109. scsiDev.status = CHECK_CONDITION;
  110. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  111. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  112. scsiDev.phase = STATUS;
  113. }
  114. if (scsiDev.phase == DATA_IN && scsiDev.dataLen > allocLength)
  115. {
  116. // simply truncate the response.
  117. scsiDev.dataLen = allocLength;
  118. }
  119. {
  120. // Set the first byte to indicate LUN presence.
  121. if (scsiDev.lun) // We only support lun 0
  122. {
  123. scsiDev.data[0] = 0x7F;
  124. }
  125. }
  126. }
  127. void scsiReadBuffer()
  128. {
  129. // READ BUFFER
  130. // Used for testing the speed of the SCSI interface.
  131. uint8_t mode = scsiDev.data[1] & 7;
  132. int allocLength =
  133. (((uint32_t) scsiDev.cdb[6]) << 16) +
  134. (((uint32_t) scsiDev.cdb[7]) << 8) +
  135. scsiDev.cdb[8];
  136. if (mode == 0)
  137. {
  138. uint32_t maxSize = sizeof(scsiDev.data) - 4;
  139. // 4 byte header
  140. scsiDev.data[0] = 0;
  141. scsiDev.data[1] = (maxSize >> 16) & 0xff;
  142. scsiDev.data[2] = (maxSize >> 8) & 0xff;
  143. scsiDev.data[3] = maxSize & 0xff;
  144. scsiDev.dataLen =
  145. (allocLength > sizeof(scsiDev.data)) ? sizeof(scsiDev.data) : allocLength;
  146. scsiDev.phase = DATA_IN;
  147. }
  148. else if (mode == 0x2 && (scsiDev.cdb[2] == 0))
  149. {
  150. // TODO support BUFFER OFFSET fields in CDB
  151. scsiDev.dataLen =
  152. (allocLength > sizeof(scsiDev.data)) ? sizeof(scsiDev.data) : allocLength;
  153. scsiDev.phase = DATA_IN;
  154. }
  155. else if (mode == 0x3)
  156. {
  157. uint32_t maxSize = sizeof(scsiDev.data) - 4;
  158. // 4 byte header
  159. scsiDev.data[0] = 0;
  160. scsiDev.data[1] = (maxSize >> 16) & 0xff;
  161. scsiDev.data[2] = (maxSize >> 8) & 0xff;
  162. scsiDev.data[3] = maxSize & 0xff;
  163. scsiDev.dataLen =
  164. (allocLength > 4) ? 4: allocLength;
  165. scsiDev.phase = DATA_IN;
  166. }
  167. else
  168. {
  169. // error.
  170. scsiDev.status = CHECK_CONDITION;
  171. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  172. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  173. scsiDev.phase = STATUS;
  174. }
  175. }
  176. // Callback after the DATA OUT phase is complete.
  177. static void doWriteBuffer(void)
  178. {
  179. if (scsiDev.status == GOOD) // skip if we've already encountered an error
  180. {
  181. // scsiDev.dataLen bytes are in scsiDev.data
  182. // Don't shift it down 4 bytes ... this space is taken by
  183. // the read buffer header anyway
  184. scsiDev.phase = STATUS;
  185. }
  186. }
  187. void scsiWriteBuffer()
  188. {
  189. // WRITE BUFFER
  190. // Used for testing the speed of the SCSI interface.
  191. uint8_t mode = scsiDev.data[1] & 7;
  192. int allocLength =
  193. (((uint32_t) scsiDev.cdb[6]) << 16) +
  194. (((uint32_t) scsiDev.cdb[7]) << 8) +
  195. scsiDev.cdb[8];
  196. if ((mode == 0 || mode == 2) && allocLength <= sizeof(scsiDev.data))
  197. {
  198. scsiDev.dataLen = allocLength;
  199. scsiDev.phase = DATA_OUT;
  200. scsiDev.postDataOutHook = doWriteBuffer;
  201. }
  202. else
  203. {
  204. // error.
  205. scsiDev.status = CHECK_CONDITION;
  206. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  207. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  208. scsiDev.phase = STATUS;
  209. }
  210. }
  211. // XEBEC specific command. See
  212. // http://www.bitsavers.org/pdf/westernDigital/WD100x/79-000004_WD1002-SHD_OEM_Manual_Aug1984.pdf
  213. // Section 4.3.14
  214. void scsiWriteSectorBuffer()
  215. {
  216. scsiDev.dataLen = scsiDev.target->liveCfg.bytesPerSector;
  217. scsiDev.phase = DATA_OUT;
  218. scsiDev.postDataOutHook = doWriteBuffer;
  219. }