sdscsi.c 7.1 KB

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