scsi.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Now thread-safe.
  3. *
  4. * The codeqlock guarantees that once codes != nil, that pointer will never
  5. * change nor become invalid.
  6. *
  7. * The QLock in the Scsi structure moderates access to the raw device.
  8. * We should probably export some of the already-locked routines, but
  9. * there hasn't been a need.
  10. */
  11. #include <u.h>
  12. #include <libc.h>
  13. #include <disk.h>
  14. int scsiverbose;
  15. #define codefile "/sys/lib/scsicodes"
  16. static char *codes;
  17. static QLock codeqlock;
  18. static void
  19. getcodes(void)
  20. {
  21. Dir *d;
  22. int n, fd;
  23. if(codes != nil)
  24. return;
  25. qlock(&codeqlock);
  26. if(codes != nil) {
  27. qunlock(&codeqlock);
  28. return;
  29. }
  30. if((d = dirstat(codefile)) == nil || (fd = open(codefile, OREAD)) < 0) {
  31. qunlock(&codeqlock);
  32. return;
  33. }
  34. codes = malloc(1+d->length+1);
  35. if(codes == nil) {
  36. close(fd);
  37. qunlock(&codeqlock);
  38. free(d);
  39. return;
  40. }
  41. codes[0] = '\n'; /* for searches */
  42. n = readn(fd, codes+1, d->length);
  43. close(fd);
  44. free(d);
  45. if(n < 0) {
  46. free(codes);
  47. codes = nil;
  48. qunlock(&codeqlock);
  49. return;
  50. }
  51. codes[n] = '\0';
  52. qunlock(&codeqlock);
  53. }
  54. char*
  55. scsierror(int asc, int ascq)
  56. {
  57. char *p, *q;
  58. static char search[32];
  59. static char buf[128];
  60. getcodes();
  61. if(codes) {
  62. sprint(search, "\n%.2ux%.2ux ", asc, ascq);
  63. if(p = strstr(codes, search)) {
  64. p += 6;
  65. if((q = strchr(p, '\n')) == nil)
  66. q = p+strlen(p);
  67. snprint(buf, sizeof buf, "%.*s", (int)(q-p), p);
  68. return buf;
  69. }
  70. sprint(search, "\n%.2ux00", asc);
  71. if(p = strstr(codes, search)) {
  72. p += 6;
  73. if((q = strchr(p, '\n')) == nil)
  74. q = p+strlen(p);
  75. snprint(buf, sizeof buf, "(ascq #%.2ux) %.*s", ascq, (int)(q-p), p);
  76. return buf;
  77. }
  78. }
  79. sprint(buf, "scsi #%.2ux %.2ux", asc, ascq);
  80. return buf;
  81. }
  82. static int
  83. _scsicmd(Scsi *s, uchar *cmd, int ccount, void *data, int dcount, int io, int dolock)
  84. {
  85. uchar resp[16];
  86. int n;
  87. long status;
  88. if(dolock)
  89. qlock(s);
  90. if(write(s->rawfd, cmd, ccount) != ccount) {
  91. werrstr("cmd write: %r");
  92. if(dolock)
  93. qunlock(s);
  94. return -1;
  95. }
  96. switch(io){
  97. case Sread:
  98. n = read(s->rawfd, data, dcount);
  99. if(n < 0 && scsiverbose)
  100. fprint(2, "dat read: %r: cmd 0x%2.2uX\n", cmd[0]);
  101. break;
  102. case Swrite:
  103. n = write(s->rawfd, data, dcount);
  104. if(n != dcount && scsiverbose)
  105. fprint(2, "dat write: %r: cmd 0x%2.2uX\n", cmd[0]);
  106. break;
  107. default:
  108. case Snone:
  109. n = write(s->rawfd, resp, 0);
  110. if(n != 0 && scsiverbose)
  111. fprint(2, "none write: %r: cmd 0x%2.2uX\n", cmd[0]);
  112. break;
  113. }
  114. memset(resp, 0, sizeof(resp));
  115. if(read(s->rawfd, resp, sizeof(resp)) < 0) {
  116. werrstr("resp read: %r\n");
  117. if(dolock)
  118. qunlock(s);
  119. return -1;
  120. }
  121. if(dolock)
  122. qunlock(s);
  123. resp[sizeof(resp)-1] = '\0';
  124. status = atoi((char*)resp);
  125. if(status == 0)
  126. return n;
  127. werrstr("cmd %2.2uX: status %luX dcount %d n %d", cmd[0], status, dcount, n);
  128. return -1;
  129. }
  130. int
  131. scsicmd(Scsi *s, uchar *cmd, int ccount, void *data, int dcount, int io)
  132. {
  133. return _scsicmd(s, cmd, ccount, data, dcount, io, 1);
  134. }
  135. static int
  136. _scsiready(Scsi *s, int dolock)
  137. {
  138. uchar cmd[6], resp[16];
  139. int status, i;
  140. if(dolock)
  141. qlock(s);
  142. for(i=0; i<3; i++) {
  143. memset(cmd, 0, sizeof(cmd));
  144. cmd[0] = 0x00; /* unit ready */
  145. if(write(s->rawfd, cmd, sizeof(cmd)) != sizeof(cmd)) {
  146. if(scsiverbose)
  147. fprint(2, "ur cmd write: %r\n");
  148. goto bad;
  149. }
  150. write(s->rawfd, resp, 0);
  151. if(read(s->rawfd, resp, sizeof(resp)) < 0) {
  152. if(scsiverbose)
  153. fprint(2, "ur resp read: %r\n");
  154. goto bad;
  155. }
  156. resp[sizeof(resp)-1] = '\0';
  157. status = atoi((char*)resp);
  158. if(status == 0 || status == 0x02) {
  159. if(dolock)
  160. qunlock(s);
  161. return 0;
  162. }
  163. if(scsiverbose)
  164. fprint(2, "target: bad status: %x\n", status);
  165. bad:;
  166. }
  167. if(dolock)
  168. qunlock(s);
  169. return -1;
  170. }
  171. int
  172. scsiready(Scsi *s)
  173. {
  174. return _scsiready(s, 1);
  175. }
  176. int
  177. scsi(Scsi *s, uchar *cmd, int ccount, void *v, int dcount, int io)
  178. {
  179. uchar req[6], sense[255], *data;
  180. int tries, code, key, n;
  181. char *p;
  182. data = v;
  183. SET(key, code);
  184. qlock(s);
  185. for(tries=0; tries<2; tries++) {
  186. n = _scsicmd(s, cmd, ccount, data, dcount, io, 0);
  187. if(n >= 0) {
  188. qunlock(s);
  189. return n;
  190. }
  191. /*
  192. * request sense
  193. */
  194. memset(req, 0, sizeof(req));
  195. req[0] = 0x03;
  196. req[4] = sizeof(sense);
  197. memset(sense, 0xFF, sizeof(sense));
  198. if((n=_scsicmd(s, req, sizeof(req), sense, sizeof(sense), Sread, 0)) < 14)
  199. if(scsiverbose)
  200. fprint(2, "reqsense scsicmd %d: %r\n", n);
  201. if(_scsiready(s, 0) < 0)
  202. if(scsiverbose)
  203. fprint(2, "unit not ready\n");
  204. key = sense[2];
  205. code = sense[12];
  206. if(code == 0x17 || code == 0x18) { /* recovered errors */
  207. qunlock(s);
  208. return dcount;
  209. }
  210. if(code == 0x28 && cmd[0] == 0x43) { /* get info and media changed */
  211. s->nchange++;
  212. s->changetime = time(0);
  213. continue;
  214. }
  215. }
  216. /* drive not ready, or medium not present */
  217. if(cmd[0] == 0x43 && key == 2 && (code == 0x3a || code == 0x04)) {
  218. s->changetime = 0;
  219. qunlock(s);
  220. return -1;
  221. }
  222. qunlock(s);
  223. if(cmd[0] == 0x43 && key == 5 && code == 0x24) /* blank media */
  224. return -1;
  225. p = scsierror(code, sense[13]);
  226. werrstr("cmd #%.2ux: %s", cmd[0], p);
  227. if(scsiverbose)
  228. fprint(2, "scsi cmd #%.2ux: %.2ux %.2ux %.2ux: %s\n", cmd[0], key, code, sense[13], p);
  229. // if(key == 0)
  230. // return dcount;
  231. return -1;
  232. }
  233. Scsi*
  234. openscsi(char *dev)
  235. {
  236. Scsi *s;
  237. int rawfd, ctlfd, l, n;
  238. char *name, *p, buf[512];
  239. l = strlen(dev)+1+3+1;
  240. name = malloc(l);
  241. if(name == nil)
  242. return nil;
  243. snprint(name, l, "%s/raw", dev);
  244. if((rawfd = open(name, ORDWR)) < 0) {
  245. free(name);
  246. return nil;
  247. }
  248. snprint(name, l, "%s/ctl", dev);
  249. if((ctlfd = open(name, ORDWR)) < 0) {
  250. free(name);
  251. Error:
  252. close(rawfd);
  253. return nil;
  254. }
  255. free(name);
  256. n = readn(ctlfd, buf, sizeof buf);
  257. close(ctlfd);
  258. if(n <= 0)
  259. goto Error;
  260. if(strncmp(buf, "inquiry ", 8) != 0 || (p = strchr(buf, '\n')) == nil)
  261. goto Error;
  262. *p = '\0';
  263. if((p = strdup(buf+8)) == nil)
  264. goto Error;
  265. s = malloc(sizeof(*s));
  266. if(s == nil) {
  267. Error1:
  268. free(p);
  269. goto Error;
  270. }
  271. memset(s, 0, sizeof(*s));
  272. s->rawfd = rawfd;
  273. s->inquire = p;
  274. s->changetime = time(0);
  275. if(scsiready(s) < 0)
  276. goto Error1;
  277. return s;
  278. }