sdscsi.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 needs an initialising command, set status
  66. * so it will be spun-up below.
  67. * If there's no medium, that's OK too, but don't
  68. * try to spin it up.
  69. */
  70. if(r->sense[12] == 0x04 && r->sense[13] == 0x02){
  71. status = SDok;
  72. break;
  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. * Don't wait for completion, 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)|0x01;
  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. }
  96. free(r);
  97. if(status == SDok || status == SDcheck)
  98. return 1;
  99. return 0;
  100. }
  101. static int
  102. scsirio(SDreq* r)
  103. {
  104. /*
  105. * Perform an I/O request, returning
  106. * -1 failure
  107. * 0 ok
  108. * 1 no medium present
  109. * 2 retry
  110. * The contents of r may be altered so the
  111. * caller should re-initialise if necesary.
  112. */
  113. r->status = ~0;
  114. switch(r->unit->dev->ifc->rio(r)){
  115. default:
  116. return -1;
  117. case SDcheck:
  118. if(!(r->flags & SDvalidsense))
  119. return -1;
  120. switch(r->sense[2] & 0x0F){
  121. case 0x00: /* no sense */
  122. case 0x01: /* recovered error */
  123. return 2;
  124. case 0x06: /* check condition */
  125. /*
  126. * 0x28 - not ready to ready transition,
  127. * medium may have changed.
  128. * 0x29 - power on or some type of reset.
  129. */
  130. if(r->sense[12] == 0x28 && r->sense[13] == 0)
  131. return 2;
  132. if(r->sense[12] == 0x29)
  133. return 2;
  134. return -1;
  135. case 0x02: /* not ready */
  136. /*
  137. * If no medium present, bail out.
  138. * If unit is becoming ready, rather than not
  139. * not ready, wait a little then poke it again. */
  140. if(r->sense[12] == 0x3A)
  141. return 1;
  142. if(r->sense[12] != 0x04 || r->sense[13] != 0x01)
  143. return -1;
  144. while(waserror())
  145. ;
  146. tsleep(&r->unit->rendez, return0, 0, 500);
  147. poperror();
  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. SDreq *r;
  163. uchar *p;
  164. int ok, retries;
  165. if((r = malloc(sizeof(SDreq))) == nil)
  166. return 0;
  167. if((p = sdmalloc(8)) == nil){
  168. free(r);
  169. return 0;
  170. }
  171. ok = 0;
  172. r->unit = unit;
  173. r->lun = 0; /* ??? */
  174. for(retries = 0; retries < 10; retries++){
  175. /*
  176. * Read-capacity is mandatory for DA, WORM, CD-ROM and
  177. * MO. It may return 'not ready' if type DA is not
  178. * spun up, type MO or type CD-ROM are not loaded or just
  179. * plain slow getting their act together after a reset.
  180. */
  181. r->write = 0;
  182. memset(r->cmd, 0, sizeof(r->cmd));
  183. r->cmd[0] = 0x25;
  184. r->cmd[1] = r->lun<<5;
  185. r->clen = 10;
  186. r->data = p;
  187. r->dlen = 8;
  188. r->flags = 0;
  189. r->status = ~0;
  190. switch(scsirio(r)){
  191. default:
  192. break;
  193. case 0:
  194. unit->sectors = (p[0]<<24)|(p[1]<<16)|(p[2]<<8)|p[3];
  195. if(unit->sectors == 0)
  196. continue;
  197. /*
  198. * Read-capacity returns the LBA of the last sector,
  199. * therefore the number of sectors must be incremented.
  200. */
  201. unit->sectors++;
  202. unit->secsize = (p[4]<<24)|(p[5]<<16)|(p[6]<<8)|p[7];
  203. /*
  204. * Some ATAPI CD readers lie about the block size.
  205. * Since we don't read audio via this interface
  206. * it's okay to always fudge this.
  207. */
  208. if(unit->secsize == 2352)
  209. unit->secsize = 2048;
  210. ok = 1;
  211. break;
  212. case 1:
  213. ok = 1;
  214. break;
  215. case 2:
  216. continue;
  217. }
  218. break;
  219. }
  220. free(p);
  221. free(r);
  222. if(ok)
  223. return ok+retries;
  224. else
  225. return 0;
  226. }
  227. int
  228. scsiexec(SDunit* unit, int write, uchar* cmd, int clen, void* data, int* dlen)
  229. {
  230. SDreq *r;
  231. int status;
  232. if((r = malloc(sizeof(SDreq))) == nil)
  233. return SDmalloc;
  234. r->unit = unit;
  235. r->lun = cmd[1]>>5; /* ??? */
  236. r->write = write;
  237. memmove(r->cmd, cmd, clen);
  238. r->clen = clen;
  239. r->data = data;
  240. if(dlen)
  241. r->dlen = *dlen;
  242. r->flags = 0;
  243. r->status = ~0;
  244. /*
  245. * Call the device-specific I/O routine.
  246. * There should be no calls to 'error()' below this
  247. * which percolate back up.
  248. */
  249. switch(status = unit->dev->ifc->rio(r)){
  250. case SDok:
  251. if(dlen)
  252. *dlen = r->rlen;
  253. /*FALLTHROUGH*/
  254. case SDcheck:
  255. /*FALLTHROUGH*/
  256. default:
  257. /*
  258. * It's more complicated than this. There are conditions
  259. * which are 'ok' but for which the returned status code
  260. * is not 'SDok'.
  261. * Also, not all conditions require a reqsense, might
  262. * need to do a reqsense here and make it available to the
  263. * caller somehow.
  264. *
  265. * Mañana.
  266. */
  267. break;
  268. }
  269. sdfree(r);
  270. return status;
  271. }
  272. long
  273. scsibio(SDunit* unit, int lun, int write, void* data, long nb, long bno)
  274. {
  275. SDreq *r;
  276. long rlen;
  277. if((r = malloc(sizeof(SDreq))) == nil)
  278. error(Enomem);
  279. r->unit = unit;
  280. r->lun = lun;
  281. again:
  282. r->write = write;
  283. if(write == 0)
  284. r->cmd[0] = 0x28;
  285. else
  286. r->cmd[0] = 0x2A;
  287. r->cmd[1] = (lun<<5);
  288. r->cmd[2] = bno>>24;
  289. r->cmd[3] = bno>>16;
  290. r->cmd[4] = bno>>8;
  291. r->cmd[5] = bno;
  292. r->cmd[6] = 0;
  293. r->cmd[7] = nb>>8;
  294. r->cmd[8] = nb;
  295. r->cmd[9] = 0;
  296. r->clen = 10;
  297. r->data = data;
  298. r->dlen = nb*unit->secsize;
  299. r->flags = 0;
  300. r->status = ~0;
  301. switch(scsirio(r)){
  302. default:
  303. rlen = -1;
  304. break;
  305. case 0:
  306. rlen = r->rlen;
  307. break;
  308. case 2:
  309. rlen = -1;
  310. if(!(r->flags & SDvalidsense))
  311. break;
  312. switch(r->sense[2] & 0x0F){
  313. default:
  314. break;
  315. case 0x06: /* check condition */
  316. /*
  317. * Check for a removeable media change.
  318. * If so, mark it by zapping the geometry info
  319. * to force an online request.
  320. */
  321. if(r->sense[12] != 0x28 || r->sense[13] != 0)
  322. break;
  323. if(unit->inquiry[1] & 0x80)
  324. unit->sectors = 0;
  325. break;
  326. case 0x02: /* not ready */
  327. /*
  328. * If unit is becoming ready,
  329. * rather than not not ready, try again.
  330. */
  331. if(r->sense[12] == 0x04 && r->sense[13] == 0x01)
  332. goto again;
  333. break;
  334. }
  335. break;
  336. }
  337. free(r);
  338. return rlen;
  339. }
  340. SDev*
  341. scsiid(SDev* sdev, SDifc* ifc)
  342. {
  343. char name[32];
  344. static char idno[16] = "0123456789abcdef";
  345. static char *p = idno;
  346. while(sdev){
  347. if(sdev->ifc == ifc){
  348. sdev->idno = *p++;
  349. snprint(name, sizeof(name), "sd%c", sdev->idno);
  350. kstrdup(&sdev->name, name);
  351. if(p >= &idno[sizeof(idno)])
  352. break;
  353. }
  354. sdev = sdev->next;
  355. }
  356. return nil;
  357. }