sdscsi.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. case SDok:
  154. return 0;
  155. }
  156. }
  157. int
  158. scsionline(SDunit* unit)
  159. {
  160. int ok;
  161. static SDreq *r;
  162. static uchar *p;
  163. if((r = sdmalloc(r, sizeof(SDreq))) == nil)
  164. return 0;
  165. if((p = sdmalloc(p, 8)) == nil)
  166. return 0;
  167. ok = 0;
  168. r->unit = unit;
  169. r->lun = 0; /* ??? */
  170. for(;;){
  171. /*
  172. * Read-capacity is mandatory for DA, WORM, CD-ROM and
  173. * MO. It may return 'not ready' if type DA is not
  174. * spun up, type MO or type CD-ROM are not loaded or just
  175. * plain slow getting their act together after a reset.
  176. */
  177. r->write = 0;
  178. memset(r->cmd, 0, sizeof(r->cmd));
  179. r->cmd[0] = 0x25;
  180. r->cmd[1] = r->lun<<5;
  181. r->clen = 10;
  182. r->data = p;
  183. r->dlen = 8;
  184. r->flags = 0;
  185. r->status = ~0;
  186. // cgascreenputs("F", 1);
  187. switch(scsirio(r)){
  188. default:
  189. break;
  190. case 0:
  191. unit->sectors = (p[0]<<24)|(p[1]<<16)|(p[2]<<8)|p[3];
  192. /*
  193. * Read-capacity returns the LBA of the last sector,
  194. * therefore the number of sectors must be incremented.
  195. */
  196. unit->sectors++;
  197. unit->secsize = (p[4]<<24)|(p[5]<<16)|(p[6]<<8)|p[7];
  198. ok = 1;
  199. break;
  200. case 2:
  201. continue;
  202. }
  203. break;
  204. }
  205. return ok;
  206. }
  207. int
  208. scsiexec(SDunit* unit, int write, uchar* cmd, int clen, void* data, int* dlen)
  209. {
  210. static SDreq *r;
  211. int status;
  212. if((r = sdmalloc(r, sizeof(SDreq))) == nil)
  213. return SDmalloc;
  214. r->unit = unit;
  215. r->lun = cmd[1]>>5; /* ??? */
  216. r->write = write;
  217. memmove(r->cmd, cmd, clen);
  218. r->clen = clen;
  219. r->data = data;
  220. if(dlen)
  221. r->dlen = *dlen;
  222. r->flags = 0;
  223. r->status = ~0;
  224. /*
  225. * Call the device-specific I/O routine.
  226. * There should be no calls to 'error()' below this
  227. * which percolate back up.
  228. */
  229. // cgascreenputs("D", 1);
  230. switch(status = unit->dev->ifc->rio(r)){
  231. case SDok:
  232. if(dlen)
  233. *dlen = r->rlen;
  234. /*FALLTHROUGH*/
  235. case SDcheck:
  236. /*FALLTHROUGH*/
  237. default:
  238. /*
  239. * It's more complicated than this. There are conditions
  240. * which are 'ok' but for which the returned status code
  241. * is not 'SDok'.
  242. * Also, not all conditions require a reqsense, might
  243. * need to do a reqsense here and make it available to the
  244. * caller somehow.
  245. *
  246. * Mañana.
  247. */
  248. break;
  249. }
  250. return status;
  251. }
  252. long
  253. scsibio(SDunit* unit, int lun, int write, void* data, long nb, Off bno)
  254. {
  255. static SDreq *r;
  256. long rlen;
  257. if((r = sdmalloc(r, sizeof(SDreq))) == nil)
  258. return SDmalloc;
  259. r->unit = unit;
  260. r->lun = lun;
  261. again:
  262. r->write = write;
  263. if(write == 0)
  264. r->cmd[0] = 0x28;
  265. else
  266. r->cmd[0] = 0x2A;
  267. r->cmd[1] = (lun<<5);
  268. r->cmd[2] = bno>>24;
  269. r->cmd[3] = bno>>16;
  270. r->cmd[4] = bno>>8;
  271. r->cmd[5] = bno;
  272. r->cmd[6] = 0;
  273. r->cmd[7] = nb>>8;
  274. r->cmd[8] = nb;
  275. r->cmd[9] = 0;
  276. r->clen = 10;
  277. r->data = data;
  278. r->dlen = nb*unit->secsize;
  279. r->flags = 0;
  280. r->status = ~0;
  281. // cgascreenputs("E", 1);
  282. switch(scsirio(r)){
  283. default:
  284. rlen = -1;
  285. break;
  286. case 0:
  287. rlen = r->rlen;
  288. break;
  289. case 2:
  290. rlen = -1;
  291. if(!(r->flags & SDvalidsense))
  292. break;
  293. switch(r->sense[2] & 0x0F){
  294. default:
  295. break;
  296. case 0x06: /* check condition */
  297. /*
  298. * Check for a removeable media change.
  299. * If so, mark it and zap the geometry info
  300. * to force an online request.
  301. */
  302. if(r->sense[12] != 0x28 || r->sense[13] != 0)
  303. break;
  304. if(unit->inquiry[1] & 0x80){
  305. unit->sectors = 0;
  306. }
  307. break;
  308. case 0x02: /* not ready */
  309. /*
  310. * If unit is becoming ready,
  311. * rather than not not ready, try again.
  312. */
  313. if(r->sense[12] == 0x04 && r->sense[13] == 0x01)
  314. goto again;
  315. break;
  316. }
  317. break;
  318. }
  319. return rlen;
  320. }
  321. SDev*
  322. scsiid(SDev* sdev, SDifc* ifc)
  323. {
  324. static char idno[16] = "0123456789abcdef";
  325. static char *p = idno;
  326. while(sdev){
  327. if(sdev->ifc == ifc){
  328. sdev->idno = *p++;
  329. if(p >= &idno[sizeof(idno)])
  330. break;
  331. }
  332. sdev = sdev->next;
  333. }
  334. return nil;
  335. }