sdscsi.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. unit->secsize = (p[4]<<24)|(p[5]<<16)|(p[6]<<8)|p[7];
  199. /*
  200. * Some ATAPI CD readers lie about the block size.
  201. * Since we don't read audio via this interface
  202. * it's okay to always fudge this.
  203. */
  204. if(unit->secsize == 2352)
  205. unit->secsize = 2048;
  206. /*
  207. * Devices with removable media may return 0 sectors
  208. * when they have empty media (e.g. sata dvd writers);
  209. * if so, keep the count zero.
  210. *
  211. * Read-capacity returns the LBA of the last sector,
  212. * therefore the number of sectors must be incremented.
  213. */
  214. if(unit->sectors != 0)
  215. unit->sectors++;
  216. ok = 1;
  217. break;
  218. case 1:
  219. ok = 1;
  220. break;
  221. case 2:
  222. continue;
  223. }
  224. break;
  225. }
  226. free(p);
  227. free(r);
  228. if(ok)
  229. return ok+retries;
  230. else
  231. return 0;
  232. }
  233. int
  234. scsiexec(SDunit* unit, int write, uchar* cmd, int clen, void* data, int* dlen)
  235. {
  236. SDreq *r;
  237. int status;
  238. if((r = malloc(sizeof(SDreq))) == nil)
  239. return SDmalloc;
  240. r->unit = unit;
  241. r->lun = cmd[1]>>5; /* ??? */
  242. r->write = write;
  243. memmove(r->cmd, cmd, clen);
  244. r->clen = clen;
  245. r->data = data;
  246. if(dlen)
  247. r->dlen = *dlen;
  248. r->flags = 0;
  249. r->status = ~0;
  250. /*
  251. * Call the device-specific I/O routine.
  252. * There should be no calls to 'error()' below this
  253. * which percolate back up.
  254. */
  255. switch(status = unit->dev->ifc->rio(r)){
  256. case SDok:
  257. if(dlen)
  258. *dlen = r->rlen;
  259. /*FALLTHROUGH*/
  260. case SDcheck:
  261. /*FALLTHROUGH*/
  262. default:
  263. /*
  264. * It's more complicated than this. There are conditions
  265. * which are 'ok' but for which the returned status code
  266. * is not 'SDok'.
  267. * Also, not all conditions require a reqsense, might
  268. * need to do a reqsense here and make it available to the
  269. * caller somehow.
  270. *
  271. * Mañana.
  272. */
  273. break;
  274. }
  275. sdfree(r);
  276. return status;
  277. }
  278. static void
  279. scsifmt10(SDreq *r, int write, int lun, ulong nb, uvlong bno)
  280. {
  281. uchar *c;
  282. c = r->cmd;
  283. if(write == 0)
  284. c[0] = 0x28;
  285. else
  286. c[0] = 0x2A;
  287. c[1] = lun<<5;
  288. c[2] = bno>>24;
  289. c[3] = bno>>16;
  290. c[4] = bno>>8;
  291. c[5] = bno;
  292. c[6] = 0;
  293. c[7] = nb>>8;
  294. c[8] = nb;
  295. c[9] = 0;
  296. r->clen = 10;
  297. }
  298. static void
  299. scsifmt16(SDreq *r, int write, int lun, ulong nb, uvlong bno)
  300. {
  301. uchar *c;
  302. c = r->cmd;
  303. if(write == 0)
  304. c[0] = 0x88;
  305. else
  306. c[0] = 0x8A;
  307. c[1] = lun<<5; /* so wrong */
  308. c[2] = bno>>56;
  309. c[3] = bno>>48;
  310. c[4] = bno>>40;
  311. c[5] = bno>>32;
  312. c[6] = bno>>24;
  313. c[7] = bno>>16;
  314. c[8] = bno>>8;
  315. c[9] = bno;
  316. c[10] = nb>>24;
  317. c[11] = nb>>16;
  318. c[12] = nb>>8;
  319. c[13] = nb;
  320. c[14] = 0;
  321. c[15] = 0;
  322. r->clen = 16;
  323. }
  324. long
  325. scsibio(SDunit* unit, int lun, int write, void* data, long nb, uvlong bno)
  326. {
  327. SDreq *r;
  328. long rlen;
  329. if((r = malloc(sizeof(SDreq))) == nil)
  330. error(Enomem);
  331. r->unit = unit;
  332. r->lun = lun;
  333. again:
  334. r->write = write;
  335. if(bno >= (1ULL<<32))
  336. scsifmt16(r, write, lun, nb, bno);
  337. else
  338. scsifmt10(r, write, lun, nb, bno);
  339. r->data = data;
  340. r->dlen = nb*unit->secsize;
  341. r->flags = 0;
  342. r->status = ~0;
  343. switch(scsirio(r)){
  344. default:
  345. rlen = -1;
  346. break;
  347. case 0:
  348. rlen = r->rlen;
  349. break;
  350. case 2:
  351. rlen = -1;
  352. if(!(r->flags & SDvalidsense))
  353. break;
  354. switch(r->sense[2] & 0x0F){
  355. default:
  356. break;
  357. case 0x01: /* recovered error */
  358. print("%s: recovered error at sector %llud\n",
  359. unit->name, bno);
  360. rlen = r->rlen;
  361. break;
  362. case 0x06: /* check condition */
  363. /*
  364. * Check for a removeable media change.
  365. * If so, mark it by zapping the geometry info
  366. * to force an online request.
  367. */
  368. if(r->sense[12] != 0x28 || r->sense[13] != 0)
  369. break;
  370. if(unit->inquiry[1] & 0x80)
  371. unit->sectors = 0;
  372. break;
  373. case 0x02: /* not ready */
  374. /*
  375. * If unit is becoming ready,
  376. * rather than not not ready, try again.
  377. */
  378. if(r->sense[12] == 0x04 && r->sense[13] == 0x01)
  379. goto again;
  380. break;
  381. }
  382. break;
  383. }
  384. free(r);
  385. return rlen;
  386. }