scsi.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * interface to scsi devices via scsi(2) to sd(3),
  11. * which does not implement LUNs.
  12. */
  13. #include "all.h"
  14. #include "io.h"
  15. enum {
  16. Ninquiry = 255,
  17. Nsense = 255,
  18. CMDtest = 0x00,
  19. CMDreqsense = 0x03,
  20. CMDread6 = 0x08,
  21. CMDwrite6 = 0x0A,
  22. CMDinquiry = 0x12,
  23. CMDstart = 0x1B,
  24. CMDread10 = 0x28,
  25. CMDwrite10 = 0x2A,
  26. };
  27. typedef struct {
  28. Target target[NTarget];
  29. } Ctlr;
  30. static Ctlr scsictlr[MaxScsi];
  31. extern int scsiverbose;
  32. void
  33. scsiinit(void)
  34. {
  35. Ctlr *ctlr;
  36. int ctlrno, targetno;
  37. Target *tp;
  38. scsiverbose = 1;
  39. for(ctlrno = 0; ctlrno < MaxScsi; ctlrno++){
  40. ctlr = &scsictlr[ctlrno];
  41. memset(ctlr, 0, sizeof(Ctlr));
  42. for(targetno = 0; targetno < NTarget; targetno++){
  43. tp = &ctlr->target[targetno];
  44. qlock(tp);
  45. qunlock(tp);
  46. sprint(tp->id, "scsictlr#%d.%d", ctlrno, targetno);
  47. tp->ctlrno = ctlrno;
  48. tp->targetno = targetno;
  49. tp->inquiry = malloc(Ninquiry);
  50. tp->sense = malloc(Nsense);
  51. }
  52. }
  53. }
  54. static uint8_t lastcmd[16];
  55. static int lastcmdsz;
  56. static int
  57. sense2stcode(uint8_t *sense)
  58. {
  59. switch(sense[2] & 0x0F){
  60. case 6: /* unit attention */
  61. /*
  62. * 0x28 - not ready to ready transition,
  63. * medium may have changed.
  64. * 0x29 - power on, RESET or BUS DEVICE RESET occurred.
  65. */
  66. if(sense[12] != 0x28 && sense[12] != 0x29)
  67. return STcheck;
  68. /*FALLTHROUGH*/
  69. case 0: /* no sense */
  70. case 1: /* recovered error */
  71. return STok;
  72. case 8: /* blank data */
  73. return STblank;
  74. case 2: /* not ready */
  75. if(sense[12] == 0x3A) /* medium not present */
  76. return STcheck;
  77. /*FALLTHROUGH*/
  78. default:
  79. /*
  80. * If unit is becoming ready, rather than not ready,
  81. * then wait a little then poke it again; should this
  82. * be here or in the caller?
  83. */
  84. if((sense[12] == 0x04 && sense[13] == 0x01)) {
  85. // delay(500);
  86. // scsitest(tp, lun);
  87. fprint(2, "sense2stcode: unit becoming ready\n");
  88. return STcheck; /* not exactly right */
  89. }
  90. return STcheck;
  91. }
  92. }
  93. /*
  94. * issue the SCSI command via scsi(2). lun must already be in cmd[1].
  95. */
  96. static int
  97. doscsi(Target* tp, int rw, uint8_t* cmd, int cbytes, void* data,
  98. int* dbytes)
  99. {
  100. int lun, db = 0;
  101. uint8_t reqcmd[6], reqdata[Nsense], dummy[1];
  102. Scsi *sc;
  103. sc = tp->sc;
  104. if (sc == nil)
  105. panic("doscsi: nil tp->sc");
  106. lun = cmd[1] >> 5; /* save lun in case we need it for reqsense */
  107. /* cope with zero arguments */
  108. if (dbytes != nil)
  109. db = *dbytes;
  110. if (data == nil)
  111. data = dummy;
  112. if (scsi(sc, cmd, cbytes, data, db, rw) >= 0)
  113. return STok;
  114. /* cmd failed, get whatever sense data we can */
  115. memset(reqcmd, 0, sizeof reqcmd);
  116. reqcmd[0] = CMDreqsense;
  117. reqcmd[1] = lun<<5;
  118. reqcmd[4] = Nsense;
  119. memset(reqdata, 0, sizeof reqdata);
  120. if (scsicmd(sc, reqcmd, sizeof reqcmd, reqdata, sizeof reqdata,
  121. Sread) < 0)
  122. return STharderr;
  123. /* translate sense data to ST* codes */
  124. return sense2stcode(reqdata);
  125. }
  126. static int
  127. scsiexec(Target* tp, int rw, uint8_t* cmd, int cbytes, void* data,
  128. int* dbytes)
  129. {
  130. int s;
  131. /*
  132. * issue the SCSI command. lun must already be in cmd[1].
  133. */
  134. s = doscsi(tp, rw, cmd, cbytes, data, dbytes);
  135. switch(s){
  136. case STcheck:
  137. memmove(lastcmd, cmd, cbytes);
  138. lastcmdsz = cbytes;
  139. /*FALLTHROUGH*/
  140. default:
  141. /*
  142. * It's more complicated than this. There are conditions which
  143. * are 'ok' but for which the returned status code is not 'STok'.
  144. * Also, not all conditions require a reqsense, there may be a
  145. * need to do a reqsense here when necessary and making it
  146. * available to the caller somehow.
  147. *
  148. * Later.
  149. */
  150. break;
  151. }
  152. return s;
  153. }
  154. static int
  155. scsitest(Target* tp, char lun)
  156. {
  157. uint8_t cmd[6];
  158. memset(cmd, 0, sizeof cmd);
  159. cmd[0] = CMDtest;
  160. cmd[1] = lun<<5;
  161. return scsiexec(tp, SCSIread, cmd, sizeof cmd, 0, 0);
  162. }
  163. static int
  164. scsistart(Target* tp, char lun, int start)
  165. {
  166. uint8_t cmd[6];
  167. memset(cmd, 0, sizeof cmd);
  168. cmd[0] = CMDstart;
  169. cmd[1] = lun<<5;
  170. if(start)
  171. cmd[4] = 1;
  172. return scsiexec(tp, SCSIread, cmd, sizeof cmd, 0, 0);
  173. }
  174. static int
  175. scsiinquiry(Target* tp, char lun, int* nbytes)
  176. {
  177. uint8_t cmd[6];
  178. memset(cmd, 0, sizeof cmd);
  179. cmd[0] = CMDinquiry;
  180. cmd[1] = lun<<5;
  181. *nbytes = Ninquiry;
  182. cmd[4] = *nbytes;
  183. return scsiexec(tp, SCSIread, cmd, sizeof cmd, tp->inquiry, nbytes);
  184. }
  185. static char *key[] =
  186. {
  187. "no sense",
  188. "recovered error",
  189. "not ready",
  190. "medium error",
  191. "hardware error",
  192. "illegal request",
  193. "unit attention",
  194. "data protect",
  195. "blank check",
  196. "vendor specific",
  197. "copy aborted",
  198. "aborted command",
  199. "equal",
  200. "volume overflow",
  201. "miscompare",
  202. "reserved"
  203. };
  204. static int
  205. scsireqsense(Target* tp, char lun, int* nbytes, int quiet)
  206. {
  207. char *s;
  208. int n, status, try;
  209. uint8_t cmd[6], *sense;
  210. sense = tp->sense;
  211. for(try = 0; try < 20; try++) {
  212. memset(cmd, 0, sizeof cmd);
  213. cmd[0] = CMDreqsense;
  214. cmd[1] = lun<<5;
  215. cmd[4] = Ninquiry;
  216. memset(sense, 0, Ninquiry);
  217. *nbytes = Ninquiry;
  218. status = scsiexec(tp, SCSIread, cmd, sizeof cmd, sense, nbytes);
  219. if(status != STok)
  220. return status;
  221. *nbytes = sense[0x07]+8;
  222. switch(sense[2] & 0x0F){
  223. case 6: /* unit attention */
  224. /*
  225. * 0x28 - not ready to ready transition,
  226. * medium may have changed.
  227. * 0x29 - power on, RESET or BUS DEVICE RESET occurred.
  228. */
  229. if(sense[12] != 0x28 && sense[12] != 0x29)
  230. goto buggery;
  231. /*FALLTHROUGH*/
  232. case 0: /* no sense */
  233. case 1: /* recovered error */
  234. return STok;
  235. case 8: /* blank data */
  236. return STblank;
  237. case 2: /* not ready */
  238. if(sense[12] == 0x3A) /* medium not present */
  239. goto buggery;
  240. /*FALLTHROUGH*/
  241. default:
  242. /*
  243. * If unit is becoming ready, rather than not ready,
  244. * then wait a little then poke it again; should this
  245. * be here or in the caller?
  246. */
  247. if((sense[12] == 0x04 && sense[13] == 0x01)){
  248. delay(500);
  249. scsitest(tp, lun);
  250. break;
  251. }
  252. goto buggery;
  253. }
  254. }
  255. buggery:
  256. if(quiet == 0){
  257. s = key[sense[2]&0x0F];
  258. print("%s: reqsense: '%s' code #%2.2x #%2.2x\n",
  259. tp->id, s, sense[12], sense[13]);
  260. print("%s: byte 2: #%2.2x, bytes 15-17: #%2.2x #%2.2x #%2.2x\n",
  261. tp->id, sense[2], sense[15], sense[16], sense[17]);
  262. print("lastcmd (%d): ", lastcmdsz);
  263. for(n = 0; n < lastcmdsz; n++)
  264. print(" #%2.2x", lastcmd[n]);
  265. print("\n");
  266. }
  267. return STcheck;
  268. }
  269. static Target*
  270. scsitarget(Device* d)
  271. {
  272. int ctlrno, targetno;
  273. ctlrno = d->wren.ctrl;
  274. if(ctlrno < 0 || ctlrno >= MaxScsi /* || scsictlr[ctlrno].io == nil */)
  275. return 0;
  276. targetno = d->wren.targ;
  277. if(targetno < 0 || targetno >= NTarget)
  278. return 0;
  279. return &scsictlr[ctlrno].target[targetno];
  280. }
  281. static void
  282. scsiprobe(Device* d)
  283. {
  284. Target *tp;
  285. int nbytes, s;
  286. uint8_t *sense;
  287. int acount;
  288. if((tp = scsitarget(d)) == 0)
  289. panic("scsiprobe: device = %Z", d);
  290. acount = 0;
  291. again:
  292. s = scsitest(tp, d->wren.lun);
  293. if(s < STok){
  294. print("%s: test, status %d\n", tp->id, s);
  295. return;
  296. }
  297. /*
  298. * Determine if the drive exists and is not ready or
  299. * is simply not responding.
  300. * If the status is OK but the drive came back with a 'power on' or
  301. * 'reset' status, try the test again to make sure the drive is really
  302. * ready.
  303. * If the drive is not ready and requires intervention, try to spin it
  304. * up.
  305. */
  306. s = scsireqsense(tp, d->wren.lun, &nbytes, acount);
  307. sense = tp->sense;
  308. switch(s){
  309. case STok:
  310. if ((sense[2] & 0x0F) == 0x06 &&
  311. (sense[12] == 0x28 || sense[12] == 0x29))
  312. if(acount == 0){
  313. acount = 1;
  314. goto again;
  315. }
  316. break;
  317. case STcheck:
  318. if((sense[2] & 0x0F) == 0x02){
  319. if(sense[12] == 0x3A)
  320. break;
  321. if(sense[12] == 0x04 && sense[13] == 0x02){
  322. print("%s: starting...\n", tp->id);
  323. if(scsistart(tp, d->wren.lun, 1) == STok)
  324. break;
  325. s = scsireqsense(tp, d->wren.lun, &nbytes, 0);
  326. }
  327. }
  328. /*FALLTHROUGH*/
  329. default:
  330. print("%s: unavailable, status %d\n", tp->id, s);
  331. return;
  332. }
  333. /*
  334. * Inquire to find out what the device is.
  335. * Hardware drivers may need some of the info.
  336. */
  337. s = scsiinquiry(tp, d->wren.lun, &nbytes);
  338. if(s != STok) {
  339. print("%s: inquiry failed, status %d\n", tp->id, s);
  340. return;
  341. }
  342. print("%s: %s\n", tp->id, (char*)tp->inquiry+8);
  343. tp->ok = 1;
  344. }
  345. int
  346. scsiio(Device* d, int rw, uint8_t* cmd, int cbytes, void* data, int dbytes)
  347. {
  348. Target *tp;
  349. int e, nbytes, s;
  350. if((tp = scsitarget(d)) == 0)
  351. panic("scsiio: device = %Z", d);
  352. qlock(tp);
  353. if(tp->ok == 0)
  354. scsiprobe(d);
  355. qunlock(tp);
  356. s = STinit;
  357. for(e = 0; e < 10; e++){
  358. for(;;){
  359. nbytes = dbytes;
  360. s = scsiexec(tp, rw, cmd, cbytes, data, &nbytes);
  361. if(s == STok)
  362. break;
  363. s = scsireqsense(tp, d->wren.lun, &nbytes, 0);
  364. if(s == STblank && rw == SCSIread) {
  365. memset(data, 0, dbytes);
  366. return STok;
  367. }
  368. if(s != STok)
  369. break;
  370. }
  371. if(s == STok)
  372. break;
  373. }
  374. if(e)
  375. print("%s: retry %d cmd #%x\n", tp->id, e, cmd[0]);
  376. return s;
  377. }
  378. void
  379. newscsi(Device *d, Scsi *sc)
  380. {
  381. Target *tp;
  382. if((tp = scsitarget(d)) == nil)
  383. panic("newscsi: device = %Z", d);
  384. tp->sc = sc; /* connect Target to Scsi */
  385. }