sdscsi.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. #include "all.h"
  2. #include "io.h"
  3. #include "mem.h"
  4. #include "sd.h"
  5. #include "../ip/ip.h" /* for Ether */
  6. #include "etherif.h" /* for Ether */
  7. #include "compat.h"
  8. #undef error
  9. static int
  10. scsitest(SDreq* r)
  11. {
  12. r->write = 0;
  13. memset(r->cmd, 0, sizeof(r->cmd));
  14. r->cmd[1] = r->lun<<5;
  15. r->clen = 6;
  16. r->data = nil;
  17. r->dlen = 0;
  18. r->flags = 0;
  19. r->status = ~0;
  20. // cgascreenputs("A", 1);
  21. return r->unit->dev->ifc->rio(r);
  22. }
  23. int
  24. scsiverify(SDunit* unit)
  25. {
  26. static SDreq *r;
  27. int i, status;
  28. static uchar *inquiry;
  29. if((r = sdmalloc(r, sizeof(SDreq))) == nil)
  30. return 0;
  31. if((inquiry = sdmalloc(inquiry, sizeof(unit->inquiry))) == nil)
  32. return 0;
  33. r->unit = unit;
  34. r->lun = 0; /* ??? */
  35. memset(unit->inquiry, 0, sizeof(unit->inquiry));
  36. r->write = 0;
  37. r->cmd[0] = 0x12;
  38. r->cmd[1] = r->lun<<5;
  39. r->cmd[4] = sizeof(unit->inquiry)-1;
  40. r->clen = 6;
  41. r->data = inquiry;
  42. r->dlen = sizeof(unit->inquiry)-1;
  43. r->flags = 0;
  44. r->status = ~0;
  45. // cgascreenputs("B", 1);
  46. if(unit->dev->ifc->rio(r) != SDok){
  47. return 0;
  48. }
  49. memmove(unit->inquiry, inquiry, r->dlen);
  50. SET(status);
  51. for(i = 0; i < 3; i++){
  52. while((status = scsitest(r)) == SDbusy)
  53. ;
  54. if(status == SDok || status != SDcheck)
  55. break;
  56. if(!(r->flags & SDvalidsense))
  57. break;
  58. if((r->sense[2] & 0x0F) != 0x02)
  59. continue;
  60. /*
  61. * Unit is 'not ready'.
  62. * If it is in the process of becoming ready or needs
  63. * an initialising command, set status so it will be spun-up
  64. * below.
  65. * If there's no medium, that's OK too, but don't
  66. * try to spin it up.
  67. */
  68. if(r->sense[12] == 0x04){
  69. if(r->sense[13] == 0x02 || r->sense[13] == 0x01){
  70. status = SDok;
  71. break;
  72. }
  73. }
  74. if(r->sense[12] == 0x3A)
  75. break;
  76. }
  77. if(status == SDok){
  78. /*
  79. * Try to ensure a direct-access device is spinning.
  80. * Ignore the result.
  81. */
  82. if((unit->inquiry[0] & 0x1F) == 0){
  83. memset(r->cmd, 0, sizeof(r->cmd));
  84. r->write = 0;
  85. r->cmd[0] = 0x1B;
  86. r->cmd[1] = r->lun<<5;
  87. r->cmd[4] = 1;
  88. r->clen = 6;
  89. r->data = nil;
  90. r->dlen = 0;
  91. r->flags = 0;
  92. r->status = ~0;
  93. unit->dev->ifc->rio(r);
  94. }
  95. return 1;
  96. }
  97. return 0;
  98. }
  99. int
  100. return0(void*)
  101. {
  102. return 0;
  103. }
  104. static int
  105. scsirio(SDreq* r)
  106. {
  107. /*
  108. * Perform an I/O request, returning
  109. * -1 failure
  110. * 0 ok
  111. * 2 retry
  112. * The contents of r may be altered so the
  113. * caller should re-initialise if necesary.
  114. */
  115. r->status = ~0;
  116. // cgascreenputs("C", 1);
  117. switch(r->unit->dev->ifc->rio(r)){
  118. default:
  119. return -1;
  120. case SDcheck:
  121. if(!(r->flags & SDvalidsense))
  122. return -1;
  123. switch(r->sense[2] & 0x0F){
  124. case 0x00: /* no sense */
  125. case 0x01: /* recovered error */
  126. return 2;
  127. case 0x06: /* check condition */
  128. /*
  129. * 0x28 - not ready to ready transition,
  130. * medium may have changed.
  131. * 0x29 - power on or some type of reset.
  132. */
  133. if(r->sense[12] == 0x28 && r->sense[13] == 0)
  134. return 2;
  135. if(r->sense[12] == 0x29)
  136. return 2;
  137. return -1;
  138. case 0x02: /* not ready */
  139. /*
  140. * If no medium present, bail out.
  141. * If unit is becoming ready, rather than not
  142. * not ready, wait a little then poke it again. */
  143. if(r->sense[12] == 0x3A)
  144. return -1;
  145. if(r->sense[12] != 0x04 || r->sense[13] != 0x01)
  146. return -1;
  147. tsleep(nil, return0, 0, 500);
  148. scsitest(r);
  149. return 2;
  150. default:
  151. return -1;
  152. }
  153. return -1;
  154. case SDok:
  155. return 0;
  156. }
  157. return -1;
  158. }
  159. int
  160. scsionline(SDunit* unit)
  161. {
  162. int ok;
  163. static SDreq *r;
  164. static uchar *p;
  165. if((r = sdmalloc(r, sizeof(SDreq))) == nil)
  166. return 0;
  167. if((p = sdmalloc(p, 8)) == nil)
  168. return 0;
  169. ok = 0;
  170. r->unit = unit;
  171. r->lun = 0; /* ??? */
  172. for(;;){
  173. /*
  174. * Read-capacity is mandatory for DA, WORM, CD-ROM and
  175. * MO. It may return 'not ready' if type DA is not
  176. * spun up, type MO or type CD-ROM are not loaded or just
  177. * plain slow getting their act together after a reset.
  178. */
  179. r->write = 0;
  180. memset(r->cmd, 0, sizeof(r->cmd));
  181. r->cmd[0] = 0x25;
  182. r->cmd[1] = r->lun<<5;
  183. r->clen = 10;
  184. r->data = p;
  185. r->dlen = 8;
  186. r->flags = 0;
  187. r->status = ~0;
  188. // cgascreenputs("F", 1);
  189. switch(scsirio(r)){
  190. default:
  191. break;
  192. case 0:
  193. unit->sectors = (p[0]<<24)|(p[1]<<16)|(p[2]<<8)|p[3];
  194. /*
  195. * Read-capacity returns the LBA of the last sector,
  196. * therefore the number of sectors must be incremented.
  197. */
  198. unit->sectors++;
  199. unit->secsize = (p[4]<<24)|(p[5]<<16)|(p[6]<<8)|p[7];
  200. ok = 1;
  201. break;
  202. case 2:
  203. continue;
  204. }
  205. break;
  206. }
  207. return ok;
  208. }
  209. int
  210. scsiexec(SDunit* unit, int write, uchar* cmd, int clen, void* data, int* dlen)
  211. {
  212. static SDreq *r;
  213. int status;
  214. if((r = sdmalloc(r, sizeof(SDreq))) == nil)
  215. return SDmalloc;
  216. r->unit = unit;
  217. r->lun = cmd[1]>>5; /* ??? */
  218. r->write = write;
  219. memmove(r->cmd, cmd, clen);
  220. r->clen = clen;
  221. r->data = data;
  222. if(dlen)
  223. r->dlen = *dlen;
  224. r->flags = 0;
  225. r->status = ~0;
  226. /*
  227. * Call the device-specific I/O routine.
  228. * There should be no calls to 'error()' below this
  229. * which percolate back up.
  230. */
  231. // cgascreenputs("D", 1);
  232. switch(status = unit->dev->ifc->rio(r)){
  233. case SDok:
  234. if(dlen)
  235. *dlen = r->rlen;
  236. /*FALLTHROUGH*/
  237. case SDcheck:
  238. /*FALLTHROUGH*/
  239. default:
  240. /*
  241. * It's more complicated than this. There are conditions
  242. * which are 'ok' but for which the returned status code
  243. * is not 'SDok'.
  244. * Also, not all conditions require a reqsense, might
  245. * need to do a reqsense here and make it available to the
  246. * caller somehow.
  247. *
  248. * Mañana.
  249. */
  250. break;
  251. }
  252. return status;
  253. }
  254. long
  255. scsibio(SDunit* unit, int lun, int write, void* data, long nb, Off bno)
  256. {
  257. static SDreq *r;
  258. long rlen;
  259. if((r = sdmalloc(r, sizeof(SDreq))) == nil)
  260. return SDmalloc;
  261. r->unit = unit;
  262. r->lun = lun;
  263. again:
  264. r->write = write;
  265. if(write == 0)
  266. r->cmd[0] = 0x28;
  267. else
  268. r->cmd[0] = 0x2A;
  269. r->cmd[1] = (lun<<5);
  270. r->cmd[2] = bno>>24;
  271. r->cmd[3] = bno>>16;
  272. r->cmd[4] = bno>>8;
  273. r->cmd[5] = bno;
  274. r->cmd[6] = 0;
  275. r->cmd[7] = nb>>8;
  276. r->cmd[8] = nb;
  277. r->cmd[9] = 0;
  278. r->clen = 10;
  279. r->data = data;
  280. r->dlen = nb*unit->secsize;
  281. r->flags = 0;
  282. r->status = ~0;
  283. // cgascreenputs("E", 1);
  284. switch(scsirio(r)){
  285. default:
  286. rlen = -1;
  287. break;
  288. case 0:
  289. rlen = r->rlen;
  290. break;
  291. case 2:
  292. rlen = -1;
  293. if(!(r->flags & SDvalidsense))
  294. break;
  295. switch(r->sense[2] & 0x0F){
  296. default:
  297. break;
  298. case 0x06: /* check condition */
  299. /*
  300. * Check for a removeable media change.
  301. * If so, mark it and zap the geometry info
  302. * to force an online request.
  303. */
  304. if(r->sense[12] != 0x28 || r->sense[13] != 0)
  305. break;
  306. if(unit->inquiry[1] & 0x80){
  307. unit->sectors = 0;
  308. }
  309. break;
  310. case 0x02: /* not ready */
  311. /*
  312. * If unit is becoming ready,
  313. * rather than not not ready, try again.
  314. */
  315. if(r->sense[12] == 0x04 && r->sense[13] == 0x01)
  316. goto again;
  317. break;
  318. }
  319. break;
  320. }
  321. return rlen;
  322. }
  323. SDev*
  324. scsiid(SDev* sdev, SDifc* ifc)
  325. {
  326. static char idno[16] = "0123456789abcdef";
  327. static char *p = idno;
  328. while(sdev){
  329. if(sdev->ifc == ifc){
  330. sdev->idno = *p++;
  331. if(p >= &idno[sizeof(idno)])
  332. break;
  333. }
  334. sdev = sdev->next;
  335. }
  336. return nil;
  337. }