scsibuslogic.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. /*
  2. * Buslogic BT-* SCSI Host Adapter in both 24-bit and 32-bit mode.
  3. * 24-bit mode works for Adaptec 154xx series too.
  4. *
  5. * To do:
  6. * tidy the PCI probe and do EISA;
  7. * allocate more Ccb's as needed, up to NMbox-1;
  8. * add nmbox and nccb to Ctlr struct for the above;
  9. * 64-bit LUN/explicit wide support necessary?
  10. *
  11. */
  12. #include "all.h"
  13. #include "io.h"
  14. #include "mem.h"
  15. enum { /* registers */
  16. Rcontrol = 0x00, /* WO: control register */
  17. Rstatus = 0x00, /* RO: status register */
  18. Rcpr = 0x01, /* WO: command/parameter register */
  19. Rdatain = 0x01, /* RO: data-in register */
  20. Rinterrupt = 0x02, /* RO: interrupt register */
  21. };
  22. enum { /* Rcontrol */
  23. Rsbus = 0x10, /* SCSI Bus Reset */
  24. Rint = 0x20, /* Interrupt Reset */
  25. Rsoft = 0x40, /* Soft Reset */
  26. Rhard = 0x80, /* Hard Reset */
  27. };
  28. enum { /* Rstatus */
  29. Cmdinv = 0x01, /* Command Invalid */
  30. Dirrdy = 0x04, /* Data In Register Ready */
  31. Cprbsy = 0x08, /* Command/Parameter-Out Register Busy */
  32. Hardy = 0x10, /* Host Adapter Ready */
  33. Inreq = 0x20, /* Initialisation Required */
  34. Dfail = 0x40, /* Diagnostic Failure */
  35. Dact = 0x80, /* Diagnostic Active */
  36. };
  37. enum { /* Rcpr */
  38. Cinitialise = 0x01, /* Initialise Mailbox */
  39. Cstart = 0x02, /* Start Mailbox Command */
  40. Cinquiry = 0x04, /* Adapter Anquiry */
  41. Ceombri = 0x05, /* Enable OMBR Interrupt */
  42. Cinquire = 0x0B, /* Inquire Configuration */
  43. Cextbios = 0x28, /* AHA-1542: Return extended BIOS information */
  44. Cmbienable = 0x29, /* AHA-1542: Mailbox interface enable */
  45. Ciem = 0x81, /* Initialise Extended Mailbox */
  46. Ciesi = 0x8D, /* Inquire Extended Setup Information */
  47. Cerrm = 0x8F, /* Enable strict round-robin mode */
  48. Cwide = 0x96, /* Wide CCB */
  49. };
  50. enum { /* Rinterrupt */
  51. Imbl = 0x01, /* Incoming Mailbox Loaded */
  52. Mbor = 0x02, /* Mailbox Out Ready */
  53. Cmdc = 0x04, /* Command Complete */
  54. Rsts = 0x08, /* SCSI Reset State */
  55. Intv = 0x80, /* Interrupt Valid */
  56. };
  57. typedef struct {
  58. uchar code; /* action/completion code */
  59. uchar ccb[3]; /* CCB pointer (MSB, ..., LSB) */
  60. } Mbox24;
  61. typedef struct {
  62. uchar ccb[4]; /* CCB pointer (LSB, ..., MSB) */
  63. uchar btstat; /* BT-7[45]7[SD] status */
  64. uchar sdstat; /* SCSI device status */
  65. uchar pad;
  66. uchar code; /* action/completion code */
  67. } Mbox32;
  68. typedef union {
  69. Mbox24;
  70. Mbox32;
  71. } Mbox;
  72. enum { /* mailbox commands */
  73. Mbfree = 0x00, /* Mailbox not in use */
  74. Mbostart = 0x01, /* Start a mailbox command */
  75. Mboabort = 0x02, /* Abort a mailbox command */
  76. Mbiok = 0x01, /* CCB completed without error */
  77. Mbiabort = 0x02, /* CCB aborted at request of host */
  78. Mbinx = 0x03, /* Aborted CCB not found */
  79. Mbierror = 0x04, /* CCB completed with error */
  80. };
  81. typedef struct Ccb24 Ccb24;
  82. typedef struct Ccb32 Ccb32;
  83. typedef union Ccb Ccb;
  84. typedef struct Ccb24 {
  85. uchar opcode; /* Operation code */
  86. uchar datadir; /* Data direction control */
  87. uchar cdblen; /* Length of CDB */
  88. uchar senselen; /* Length of sense area */
  89. uchar datalen[3]; /* Data length (MSB, ..., LSB) */
  90. uchar dataptr[3]; /* Data pointer (MSB, ..., LSB) */
  91. uchar linkptr[3]; /* Link pointer (MSB, ..., LSB) */
  92. uchar linkid; /* command linking identifier */
  93. uchar btstat; /* BT-* adapter status */
  94. uchar sdstat; /* SCSI device status */
  95. uchar reserved[2]; /* */
  96. uchar cs[12+0xFF]; /* Command descriptor block + Sense bytes */
  97. Rendez;
  98. int done; /* command completed */
  99. Ccb* ccb; /* link on free list */
  100. } Ccb24;
  101. typedef struct Ccb32 {
  102. uchar opcode; /* Operation code */
  103. uchar datadir; /* Data direction control */
  104. uchar cdblen; /* Length of CDB */
  105. uchar senselen; /* Length of sense area */
  106. uchar datalen[4]; /* Data length (LSB, ..., MSB) */
  107. uchar dataptr[4]; /* Data pointer (LSB, ..., MSB) */
  108. uchar reserved[2];
  109. uchar btstat; /* BT-* adapter status */
  110. uchar sdstat; /* SCSI device status */
  111. uchar targetid; /* Target ID */
  112. uchar luntag; /* LUN & tag */
  113. uchar cdb[12]; /* Command descriptor block */
  114. uchar ccbctl; /* CCB control */
  115. uchar linkid; /* command linking identifier */
  116. uchar linkptr[4]; /* Link pointer (LSB, ..., MSB) */
  117. uchar senseptr[4]; /* Sense pointer (LSB, ..., MSB) */
  118. uchar sense[0xFF]; /* Sense bytes */
  119. Rendez;
  120. int done; /* command completed */
  121. Ccb* ccb; /* link on free list */
  122. } Ccb32;
  123. typedef union Ccb {
  124. Ccb24;
  125. Ccb32;
  126. } Ccb;
  127. enum { /* opcode */
  128. OInitiator = 0x00, /* initiator CCB */
  129. Ordl = 0x03, /* initiator CCB with residual data length returned */
  130. };
  131. enum { /* datadir */
  132. CCBdatain = 0x08, /* inbound data transfer, length is checked */
  133. CCBdataout = 0x10, /* outbound data transfer, length is checked */
  134. };
  135. enum { /* btstat */
  136. Eok = 0x00, /* CCB completed normally with no errors */
  137. };
  138. enum { /* luntag */
  139. TagEnable = 0x20, /* Tag enable */
  140. SQTag = 0x00, /* Simple Queue Tag */
  141. HQTag = 0x40, /* Head of Queue Tag */
  142. OQTag = 0x80, /* Ordered Queue Tag */
  143. };
  144. enum { /* CCB control */
  145. NoDisc = 0x08, /* No disconnect */
  146. NoUnd = 0x10, /* No underrrun error report */
  147. NoData = 0x20, /* No data transfer */
  148. NoStat = 0x40, /* No CCB status if zero */
  149. NoIntr = 0x80, /* No Interrupts */
  150. };
  151. typedef struct Bbuf Bbuf;
  152. struct Bbuf {
  153. Bbuf* next;
  154. uchar* data; /* original data */
  155. uchar* buf; /* bounce buffer */
  156. };
  157. typedef struct {
  158. ulong port; /* I/O port */
  159. int id; /* adapter SCSI id */
  160. int ctlrno;
  161. int bus; /* 24 or 32 -bit */
  162. int wide;
  163. int tbdf;
  164. Lock ccblock;
  165. QLock ccbq;
  166. Rendez ccbr;
  167. Lock mboxlock;
  168. Mbox* mb; /* mailbox out + mailbox in */
  169. int mbox; /* current mailbox out index into mb */
  170. int mbix; /* current mailbox in index into mb */
  171. Lock cachelock;
  172. Ccb* ccb; /* list of free Ccb's */
  173. Ccb* cache[NTarget]; /* last completed Ccb */
  174. Lock bblock;
  175. Bbuf* bbuf; /* list of free 24-bit bounce buffers */
  176. QLock bbq;
  177. Rendez bbr;
  178. } Ctlr;
  179. /*
  180. * The number of mailboxes should be a multiple of 8 (4 for Mbox32)
  181. * to ensure the boundary between the out and in mailboxes doesn't
  182. * straddle a cache-line boundary.
  183. * The number of Ccb's should be less than the number of mailboxes to
  184. * ensure no queueing is necessary on mailbox allocation.
  185. * NBbuf should minimally be the actual number of targets plus some
  186. * slop for queuing. Since 24-bit controllers are never wide and it's
  187. * unlikely anyone would fully populate the controller, 8 is probably
  188. * enough.
  189. */
  190. enum {
  191. NMbox = 8*8, /* number of Mbox's */
  192. NCcb = NMbox-1, /* number of Ccb's */
  193. NBbuf = 8, /* number of 24-bit bounce buffers */
  194. };
  195. #define PADDR24(a, n) ((PADDR(a)+(n)) <= (1<<24))
  196. static Ctlr *ctlrxx[MaxScsi];
  197. static int ctrls;
  198. static void
  199. cmd_scsi(int, char**)
  200. {
  201. int i, j;
  202. Ctlr *ctlr;
  203. Mbox24 *mb24;
  204. Mbox32 *mb32;
  205. for(i=0; i<MaxScsi; i++) {
  206. if(!(ctrls & (1<<i)))
  207. continue;
  208. print("ctlr %d:\n", i);
  209. ctlr = ctlrxx[i];
  210. print(" mbox %2.2d\n", ctlr->mbox);
  211. print(" mbix %2.2d\n", ctlr->mbix);
  212. if(ctlr->bus == 24){
  213. for(j=0; j<NMbox+NMbox; j++) {
  214. mb24 = &ctlr->mb[j];
  215. print(" mbox %2.2d: code #%x\tpaddr #%ux\n",
  216. j, mb24->code,
  217. (mb24->ccb[0]<<16)|(mb24->ccb[1]<<8)|mb24->ccb[2]);
  218. prflush();
  219. }
  220. }
  221. else {
  222. for(j=0; j<NMbox+NMbox; j++) {
  223. mb32 = &ctlr->mb[j];
  224. print(" mbox %2.2d: code #%x\tpaddr #%ux\tbt#%ux\tsd#%ux\n",
  225. j, mb32->code,
  226. (mb32->ccb[3]<<24)
  227. |(mb32->ccb[2]<<16)
  228. |(mb32->ccb[1]<<8)
  229. |mb32->ccb[0],
  230. mb32->btstat, mb32->sdstat);
  231. prflush();
  232. }
  233. }
  234. }
  235. }
  236. static void
  237. ccbfree(Ctlr* ctlr, Ccb* ccb)
  238. {
  239. lock(&ctlr->ccblock);
  240. if(ctlr->bus == 24)
  241. ((Ccb24*)ccb)->ccb = ctlr->ccb;
  242. else
  243. ((Ccb32*)ccb)->ccb = ctlr->ccb;
  244. if(ctlr->ccb == nil)
  245. wakeup(&ctlr->ccbr);
  246. ctlr->ccb = ccb;
  247. unlock(&ctlr->ccblock);
  248. }
  249. static int
  250. ccbavailable(void* a)
  251. {
  252. return ((Ctlr*)a)->ccb != nil;
  253. }
  254. static Ccb*
  255. ccballoc(Ctlr* ctlr)
  256. {
  257. Ccb *ccb;
  258. for(;;){
  259. lock(&ctlr->ccblock);
  260. if(ccb = ctlr->ccb){
  261. if(ctlr->bus == 24)
  262. ctlr->ccb = ((Ccb24*)ccb)->ccb;
  263. else
  264. ctlr->ccb = ((Ccb32*)ccb)->ccb;
  265. unlock(&ctlr->ccblock);
  266. break;
  267. }
  268. unlock(&ctlr->ccblock);
  269. qlock(&ctlr->ccbq);
  270. sleep(&ctlr->ccbr, ccbavailable, ctlr);
  271. qunlock(&ctlr->ccbq);
  272. }
  273. return ccb;
  274. }
  275. static void
  276. bbfree(Ctlr* ctlr, Bbuf *bb)
  277. {
  278. lock(&ctlr->bblock);
  279. bb->next = ctlr->bbuf;
  280. if(ctlr->bbuf == nil)
  281. wakeup(&ctlr->bbr);
  282. ctlr->bbuf = bb;
  283. unlock(&ctlr->bblock);
  284. }
  285. static int
  286. bbavailable(void* a)
  287. {
  288. return ((Ctlr*)a)->bbuf != nil;
  289. }
  290. static Bbuf*
  291. bballoc(Ctlr* ctlr)
  292. {
  293. Bbuf *bb;
  294. for(;;){
  295. lock(&ctlr->bblock);
  296. if(bb = ctlr->bbuf){
  297. ctlr->bbuf = bb->next;
  298. unlock(&ctlr->bblock);
  299. break;
  300. }
  301. unlock(&ctlr->bblock);
  302. qlock(&ctlr->bbq);
  303. sleep(&ctlr->bbr, bbavailable, ctlr);
  304. qunlock(&ctlr->bbq);
  305. }
  306. return bb;
  307. }
  308. static int
  309. done24(void* arg)
  310. {
  311. return ((Ccb24*)arg)->done;
  312. }
  313. static int
  314. scsiio24(Target* t, int rw, uchar* cmd, int cbytes, void* data, int* dbytes)
  315. {
  316. Ctlr *ctlr;
  317. Ccb24 *ccb;
  318. Mbox24 *mb;
  319. Bbuf *bb;
  320. ulong p;
  321. int d, id, n, btstat, sdstat;
  322. uchar *sense;
  323. uchar lun;
  324. if((ctlr = ctlrxx[t->ctlrno]) == nil || ctlr->port == 0)
  325. return STharderr;
  326. id = t->targetno;
  327. if(ctlr->id == id)
  328. return STownid;
  329. lun = (cmd[1]>>5) & 0x07;
  330. /*
  331. * Ctlr->cache holds the last completed Ccb for this target if it
  332. * returned 'check condition'.
  333. * If this command is a request-sense and there is valid sense data
  334. * from the last completed Ccb, return it immediately.
  335. */
  336. lock(&ctlr->cachelock);
  337. if(ccb = ctlr->cache[id]){
  338. ctlr->cache[id] = nil;
  339. if(cmd[0] == 0x03 && ccb->sdstat == STcheck && lun == ((ccb->cs[1]>>5) & 0x07)){
  340. unlock(&ctlr->cachelock);
  341. if(dbytes){
  342. sense = &ccb->cs[ccb->cdblen];
  343. n = 8+sense[7];
  344. if(n > *dbytes)
  345. n = *dbytes;
  346. memmove(data, sense, n);
  347. *dbytes = n;
  348. }
  349. ccbfree(ctlr, (Ccb*)ccb);
  350. return STok;
  351. }
  352. }
  353. unlock(&ctlr->cachelock);
  354. if(ccb == nil)
  355. ccb = ccballoc(ctlr);
  356. /*
  357. * Check if the transfer is to memory above the 24-bit limit the
  358. * controller can address. If it is, try to allocate a temporary
  359. * buffer as a staging area.
  360. */
  361. if(dbytes)
  362. n = *dbytes;
  363. else
  364. n = 0;
  365. if(n && !PADDR24(data, n)){
  366. bb = bballoc(ctlr);
  367. bb->data = data;
  368. if(rw == SCSIwrite)
  369. memmove(bb->buf, data, n);
  370. data = bb->buf;
  371. }
  372. else
  373. bb = nil;
  374. /*
  375. * Fill in the ccb.
  376. */
  377. ccb->opcode = Ordl;
  378. ccb->datadir = (id<<5)|lun;
  379. if(n == 0)
  380. ccb->datadir |= CCBdataout|CCBdatain;
  381. else if(rw == SCSIread)
  382. ccb->datadir |= CCBdatain;
  383. else
  384. ccb->datadir |= CCBdataout;
  385. ccb->cdblen = cbytes;
  386. ccb->senselen = 0xFF;
  387. ccb->datalen[0] = n>>16;
  388. ccb->datalen[1] = n>>8;
  389. ccb->datalen[2] = n;
  390. p = PADDR(data);
  391. ccb->dataptr[0] = p>>16;
  392. ccb->dataptr[1] = p>>8;
  393. ccb->dataptr[2] = p;
  394. ccb->linkptr[0] = ccb->linkptr[1] = ccb->linkptr[2] = 0;
  395. ccb->linkid = 0;
  396. ccb->btstat = ccb->sdstat = 0;
  397. ccb->reserved[0] = ccb->reserved[1] = 0;
  398. memmove(ccb->cs, cmd, cbytes);
  399. /*
  400. * There's one more mbox than there there is
  401. * ccb so there is always one free.
  402. */
  403. lock(&ctlr->mboxlock);
  404. mb = ctlr->mb;
  405. mb += ctlr->mbox;
  406. p = PADDR(ccb);
  407. mb->ccb[0] = p>>16;
  408. mb->ccb[1] = p>>8;
  409. mb->ccb[2] = p;
  410. mb->code = Mbostart;
  411. ctlr->mbox++;
  412. if(ctlr->mbox >= NMbox)
  413. ctlr->mbox = 0;
  414. /*
  415. * This command does not require Hardy
  416. * and doesn't generate a Cmdc interrupt.
  417. */
  418. ccb->done = 0;
  419. outb(ctlr->port+Rcpr, Cstart);
  420. unlock(&ctlr->mboxlock);
  421. /*
  422. * Wait for the request to complete and return the status.
  423. * Since the buffer is not reference counted cannot return
  424. * until the DMA is done writing into the buffer so the caller
  425. * cannot free the buffer prematurely.
  426. */
  427. sleep(ccb, done24, ccb);
  428. /*
  429. * Save the status and patch up the number of
  430. * bytes actually transferred.
  431. * There's a firmware bug on some 956C controllers
  432. * which causes the return count from a successful
  433. * READ CAPACITY not be updated, so fix it here.
  434. */
  435. sdstat = ccb->sdstat;
  436. btstat = ccb->btstat;
  437. d = ccb->datalen[0]<<16;
  438. d |= ccb->datalen[1]<<8;
  439. d |= ccb->datalen[2];
  440. if(ccb->cs[0] == 0x25 && sdstat == STok)
  441. d = 0;
  442. n -= d;
  443. if(dbytes)
  444. *dbytes = n;
  445. /*
  446. * Tidy things up if a staging area was used for the data,
  447. */
  448. if(bb != nil){
  449. if(sdstat == STok && btstat == 0 && rw == SCSIread)
  450. memmove(bb->data, data, n);
  451. bbfree(ctlr, bb);
  452. }
  453. /*
  454. * If there was a check-condition, save the
  455. * ccb for a possible request-sense command.
  456. */
  457. if(sdstat == STcheck){
  458. lock(&ctlr->cachelock);
  459. if(ctlr->cache[id])
  460. ccbfree(ctlr, ctlr->cache[id]);
  461. ctlr->cache[id] = (Ccb*)ccb;
  462. unlock(&ctlr->cachelock);
  463. return STcheck;
  464. }
  465. ccbfree(ctlr, (Ccb*)ccb);
  466. if(btstat){
  467. if(btstat == 0x11)
  468. return STtimeout;
  469. return STharderr;
  470. }
  471. return sdstat;
  472. }
  473. static void
  474. interrupt24(Ureg*, void* arg)
  475. {
  476. Ctlr *ctlr;
  477. ulong port;
  478. uchar rinterrupt, rstatus;
  479. Mbox24 *mb, *mbox;
  480. Ccb24 *ccb;
  481. ctlr = arg;
  482. /*
  483. * Save and clear the interrupt(s). The only
  484. * interrupts expected are Cmdc, which is ignored,
  485. * and Imbl which means something completed.
  486. */
  487. port = ctlr->port;
  488. rinterrupt = inb(port+Rinterrupt);
  489. rstatus = inb(port+Rstatus);
  490. if((rinterrupt & ~(Cmdc|Imbl)) != Intv)
  491. print("scsi#%d: interrupt 0x%2.2ux\n", ctlr->ctlrno, rinterrupt);
  492. if((rinterrupt & Cmdc) && (rstatus & Cmdinv))
  493. print("scsi#%d: command invalid\n", ctlr->ctlrno);
  494. /*
  495. * Look for something in the mail.
  496. * If there is, save the status, free the mailbox
  497. * and wakeup whoever.
  498. */
  499. mb = ctlr->mb;
  500. for(mbox = &mb[ctlr->mbix]; mbox->code; mbox = &mb[ctlr->mbix]){
  501. ccb = (Ccb*)(KZERO
  502. |(mbox->ccb[0]<<16)
  503. |(mbox->ccb[1]<<8)
  504. |mbox->ccb[2]);
  505. mbox->code = 0;
  506. ccb->done = 1;
  507. wakeup(ccb);
  508. ctlr->mbix++;
  509. if(ctlr->mbix >= NMbox+NMbox)
  510. ctlr->mbix = NMbox;
  511. }
  512. outb(port+Rcontrol, Rint);
  513. }
  514. static int
  515. done32(void* arg)
  516. {
  517. return ((Ccb32*)arg)->done;
  518. }
  519. static int
  520. scsiio32(Target* t, int rw, uchar* cmd, int cbytes, void* data, int* dbytes)
  521. {
  522. Ctlr *ctlr;
  523. Ccb32 *ccb;
  524. Mbox32 *mb;
  525. ulong p;
  526. int d, id, n, btstat, sdstat;
  527. uchar lun;
  528. if((ctlr = ctlrxx[t->ctlrno]) == nil || ctlr->port == 0)
  529. return STharderr;
  530. id = t->targetno;
  531. if(ctlr->id == id)
  532. return STownid;
  533. lun = (cmd[1]>>5) & 0x07;
  534. /*
  535. * Ctlr->cache holds the last completed Ccb for this target if it
  536. * returned 'check condition'.
  537. * If this command is a request-sense and there is valid sense data
  538. * from the last completed Ccb, return it immediately.
  539. */
  540. lock(&ctlr->cachelock);
  541. if(ccb = ctlr->cache[id]){
  542. ctlr->cache[id] = nil;
  543. if(cmd[0] == 0x03 && ccb->sdstat == STcheck && lun == (ccb->luntag & 0x07)){
  544. unlock(&ctlr->cachelock);
  545. if(dbytes){
  546. n = 8+ccb->sense[7];
  547. if(n > *dbytes)
  548. n = *dbytes;
  549. memmove(data, ccb->sense, n);
  550. *dbytes = n;
  551. }
  552. ccbfree(ctlr, (Ccb*)ccb);
  553. return STok;
  554. }
  555. }
  556. unlock(&ctlr->cachelock);
  557. if(ccb == nil)
  558. ccb = ccballoc(ctlr);
  559. /*
  560. * Fill in the ccb.
  561. */
  562. ccb->opcode = Ordl;
  563. if(dbytes)
  564. n = *dbytes;
  565. else
  566. n = 0;
  567. if(n == 0)
  568. ccb->datadir = CCBdataout|CCBdatain;
  569. else if(rw == SCSIread)
  570. ccb->datadir = CCBdatain;
  571. else
  572. ccb->datadir = CCBdataout;
  573. ccb->cdblen = cbytes;
  574. ccb->datalen[0] = n;
  575. ccb->datalen[1] = n>>8;
  576. ccb->datalen[2] = n>>16;
  577. ccb->datalen[3] = n>>24;
  578. p = PADDR(data);
  579. ccb->dataptr[0] = p;
  580. ccb->dataptr[1] = p>>8;
  581. ccb->dataptr[2] = p>>16;
  582. ccb->dataptr[3] = p>>24;
  583. ccb->targetid = id;
  584. ccb->luntag = lun;
  585. if(t->ok && (t->inquiry[7] & 0x02)){
  586. if(ctlr->wide)
  587. ccb->datadir |= SQTag|TagEnable;
  588. else
  589. ccb->luntag |= SQTag|TagEnable;
  590. }
  591. memmove(ccb->cdb, cmd, cbytes);
  592. ccb->btstat = ccb->sdstat = 0;
  593. ccb->ccbctl = 0;
  594. /*
  595. * There's one more mbox than there there is
  596. * ccb so there is always one free.
  597. */
  598. lock(&ctlr->mboxlock);
  599. mb = ctlr->mb;
  600. mb += ctlr->mbox;
  601. p = PADDR(ccb);
  602. mb->ccb[0] = p;
  603. mb->ccb[1] = p>>8;
  604. mb->ccb[2] = p>>16;
  605. mb->ccb[3] = p>>24;
  606. mb->code = Mbostart;
  607. ctlr->mbox++;
  608. if(ctlr->mbox >= NMbox)
  609. ctlr->mbox = 0;
  610. /*
  611. * This command does not require Hardy
  612. * and doesn't generate a Cmdc interrupt.
  613. */
  614. ccb->done = 0;
  615. outb(ctlr->port+Rcpr, Cstart);
  616. unlock(&ctlr->mboxlock);
  617. /*
  618. * Wait for the request to complete and return the status.
  619. * Since the buffer is not reference counted cannot return
  620. * until the DMA is done writing into the buffer so the caller
  621. * cannot free the buffer prematurely.
  622. */
  623. sleep(ccb, done32, ccb);
  624. /*
  625. * Save the status and patch up the number of
  626. * bytes actually transferred.
  627. * There's a firmware bug on some 956C controllers
  628. * which causes the return count from a successful
  629. * READ CAPACITY not be updated, so fix it here.
  630. */
  631. sdstat = ccb->sdstat;
  632. btstat = ccb->btstat;
  633. d = ccb->datalen[0];
  634. d |= (ccb->datalen[1]<<8);
  635. d |= (ccb->datalen[2]<<16);
  636. d |= (ccb->datalen[3]<<24);
  637. if(ccb->cdb[0] == 0x25 && sdstat == STok)
  638. d = 0;
  639. n -= d;
  640. if(dbytes)
  641. *dbytes = n;
  642. /*
  643. * If there was a check-condition, save the
  644. * ccb for a possible request-sense command.
  645. */
  646. if(sdstat == STcheck){
  647. lock(&ctlr->cachelock);
  648. if(ctlr->cache[id])
  649. ccbfree(ctlr, ctlr->cache[id]);
  650. ctlr->cache[id] = (Ccb*)ccb;
  651. unlock(&ctlr->cachelock);
  652. return STcheck;
  653. }
  654. ccbfree(ctlr, (Ccb*)ccb);
  655. if(btstat){
  656. if(btstat == 0x11)
  657. return STtimeout;
  658. return STharderr;
  659. }
  660. return sdstat;
  661. }
  662. static void
  663. interrupt32(Ureg*, void* arg)
  664. {
  665. Ctlr *ctlr;
  666. ulong port;
  667. uchar rinterrupt, rstatus;
  668. Mbox32 *mb, *mbox;
  669. Ccb32 *ccb;
  670. ctlr = arg;
  671. /*
  672. * Save and clear the interrupt(s). The only
  673. * interrupts expected are Cmdc, which is ignored,
  674. * and Imbl which means something completed.
  675. */
  676. port = ctlr->port;
  677. rinterrupt = inb(port+Rinterrupt);
  678. rstatus = inb(port+Rstatus);
  679. if((rinterrupt & ~(Cmdc|Imbl)) != Intv)
  680. print("scsi#%d: interrupt 0x%2.2ux\n", ctlr->ctlrno, rinterrupt);
  681. if((rinterrupt & Cmdc) && (rstatus & Cmdinv))
  682. print("scsi#%d: command invalid\n", ctlr->ctlrno);
  683. /*
  684. * Look for something in the mail.
  685. * If there is, save the status, free the mailbox
  686. * and wakeup whoever.
  687. */
  688. mb = ctlr->mb;
  689. for(mbox = &mb[ctlr->mbix]; mbox->code; mbox = &mb[ctlr->mbix]){
  690. ccb = (Ccb*)(KZERO
  691. |(mbox->ccb[3]<<24)
  692. |(mbox->ccb[2]<<16)
  693. |(mbox->ccb[1]<<8)
  694. |mbox->ccb[0]);
  695. mbox->code = 0;
  696. ccb->done = 1;
  697. wakeup(ccb);
  698. ctlr->mbix++;
  699. if(ctlr->mbix >= NMbox+NMbox)
  700. ctlr->mbix = NMbox;
  701. }
  702. outb(port+Rcontrol, Rint);
  703. }
  704. static Lock cmdlock[MaxScsi];
  705. /*
  706. * Issue a command to a controller. The command and its length is
  707. * contained in cmd and cmdlen. If any data is to be
  708. * returned, datalen should be non-zero, and the returned data
  709. * will be placed in data.
  710. * If Cmdc is set, bail out, the invalid command will be handled
  711. * when the interrupt is processed.
  712. */
  713. static int
  714. issueio(int port, uchar* cmd, int cmdlen, uchar* data, int datalen)
  715. {
  716. int len;
  717. if(cmd[0] != Cstart && cmd[0] != Ceombri){
  718. while(!(inb(port+Rstatus) & Hardy))
  719. ;
  720. }
  721. outb(port+Rcpr, cmd[0]);
  722. len = 1;
  723. while(len < cmdlen){
  724. if(!(inb(port+Rstatus) & Cprbsy)){
  725. outb(port+Rcpr, cmd[len]);
  726. len++;
  727. }
  728. if(inb(port+Rinterrupt) & Cmdc)
  729. return 0;
  730. }
  731. len = 0;
  732. if(datalen){
  733. while(len < datalen){
  734. if(inb(port+Rstatus) & Dirrdy){
  735. data[len] = inb(port+Rdatain);
  736. len++;
  737. }
  738. if(inb(port+Rinterrupt) & Cmdc)
  739. break;
  740. }
  741. }
  742. return len;
  743. }
  744. /*
  745. * Issue a command to a controller, wait for it to complete then
  746. * reset the interrupt.
  747. * Should only be called at initialisation.
  748. */
  749. static int
  750. issue(int ctlrno, int port, uchar* cmd, int cmdlen, uchar* data, int datalen)
  751. {
  752. int len;
  753. uchar rinterrupt, rstatus;
  754. ilock(&cmdlock[ctlrno]);
  755. len = issueio(port, cmd, cmdlen, data, datalen);
  756. while(!((rinterrupt = inb(port+Rinterrupt)) & Cmdc))
  757. ;
  758. rstatus = inb(port+Rstatus);
  759. outb(port+Rcontrol, Rint);
  760. if((rinterrupt & Cmdc) && (rstatus & Cmdinv)){
  761. iunlock(&cmdlock[ctlrno]);
  762. return -1;
  763. }
  764. iunlock(&cmdlock[ctlrno]);
  765. return len;
  766. }
  767. Scsiio
  768. buslogic24(Ctlr* ctlr, ISAConf* isa)
  769. {
  770. ulong p;
  771. Ccb24 *ccb, *ccbp;
  772. Bbuf *bb;
  773. uchar cmd[6], *v;
  774. int i, len;
  775. len = (sizeof(Mbox24)*NMbox*2)+(sizeof(Ccb24)*NCcb)+(sizeof(Bbuf)*NBbuf);
  776. v = ialloc(len, 0);
  777. if(!PADDR24(ctlr, sizeof(Ctlr)) || !PADDR24(v, len)){
  778. print("scsi#%d: %s: 24-bit allocation failed\n",
  779. ctlr->ctlrno, isa->type);
  780. return 0;
  781. }
  782. ctlr->mb = (Mbox*)v;
  783. v += sizeof(Mbox24)*NMbox*2;
  784. ccb = (Ccb24*)v;
  785. for(ccbp = ccb; ccbp < &ccb[NCcb]; ccbp++){
  786. ccbp->ccb = ctlr->ccb;
  787. ctlr->ccb = (Ccb*)ccbp;
  788. }
  789. v += sizeof(Ccb24)*NCcb;
  790. for(i = 0; i < NBbuf; i++){
  791. bb = (Bbuf*)v;
  792. bb->buf = ialloc(RBUFSIZE, 32);
  793. if(bb->buf == nil || !PADDR24(bb->buf, RBUFSIZE)){
  794. print("scsi#%d: %s: 24-bit bb allocation failed (%d)\n",
  795. ctlr->ctlrno, isa->type, i);
  796. break;
  797. }
  798. bb->next = ctlr->bbuf;
  799. ctlr->bbuf = bb;
  800. v += sizeof(Bbuf);
  801. }
  802. /*
  803. * Initialise the software controller and
  804. * set the board scanning the mailboxes.
  805. */
  806. ctlr->mbix = NMbox;
  807. cmd[0] = Cinitialise;
  808. cmd[1] = NMbox;
  809. p = PADDR(ctlr->mb);
  810. cmd[2] = p>>16;
  811. cmd[3] = p>>8;
  812. cmd[4] = p;
  813. if(issue(ctlr->ctlrno, ctlr->port, cmd, 5, 0, 0) >= 0){
  814. ctlrxx[ctlr->ctlrno] = ctlr;
  815. setvec(Int0vec+isa->irq, interrupt24, ctlr);
  816. return scsiio24;
  817. }
  818. print("scsi#%d: %s: mbox24 init failed\n", ctlr->ctlrno, isa->type);
  819. return 0;
  820. }
  821. Scsiio
  822. buslogic32(Ctlr* ctlr, ISAConf* isa)
  823. {
  824. ulong p;
  825. Ccb32 *ccb, *ccbp;
  826. uchar cmd[6], *v;
  827. v = ialloc((sizeof(Mbox32)*NMbox*2)+(sizeof(Ccb32)*NCcb), 0);
  828. ctlr->mb = (Mbox*)v;
  829. v += sizeof(Mbox32)*NMbox*2;
  830. ccb = (Ccb32*)v;
  831. for(ccbp = ccb; ccbp < &ccb[NCcb]; ccbp++){
  832. /*
  833. * Fill in some stuff that doesn't change.
  834. */
  835. ccbp->senselen = sizeof(ccbp->sense);
  836. p = PADDR(ccbp->sense);
  837. ccbp->senseptr[0] = p;
  838. ccbp->senseptr[1] = p>>8;
  839. ccbp->senseptr[2] = p>>16;
  840. ccbp->senseptr[3] = p>>24;
  841. ccbp->ccb = ctlr->ccb;
  842. ctlr->ccb = (Ccb*)ccbp;
  843. }
  844. /*
  845. * Wide mode setup.
  846. */
  847. if(ctlr->wide){
  848. cmd[0] = Cwide;
  849. cmd[1] = 1;
  850. if(issue(ctlr->ctlrno, ctlr->port, cmd, 2, 0, 0) < 0)
  851. print("scsi#%d: %s: wide init failed\n",
  852. ctlr->ctlrno, isa->type);
  853. }
  854. /*
  855. * Initialise the software controller and
  856. * set the board scanning the mailboxes.
  857. */
  858. ctlr->mbix = NMbox;
  859. cmd[0] = Ciem;
  860. cmd[1] = NMbox;
  861. p = PADDR(ctlr->mb);
  862. cmd[2] = p;
  863. cmd[3] = p>>8;
  864. cmd[4] = p>>16;
  865. cmd[5] = p>>24;
  866. if(issue(ctlr->ctlrno, ctlr->port, cmd, 6, 0, 0) >= 0){
  867. ctlrxx[ctlr->ctlrno] = ctlr;
  868. /*
  869. intrenable(VectorPIC+isa->irq, interrupt32, ctlr, ctlr->tbdf);
  870. */
  871. setvec(Int0vec+isa->irq, interrupt32, ctlr);
  872. return scsiio32;
  873. }
  874. print("scsi#%d: %s: mbox32 init failed\n", ctlr->ctlrno, isa->type);
  875. return 0;
  876. }
  877. typedef struct Adapter {
  878. int port;
  879. Pcidev* pcidev;
  880. } Adapter;
  881. static Msgbuf* adapter;
  882. static void
  883. buslogicpci(void)
  884. {
  885. Msgbuf *mb;
  886. Adapter *ap;
  887. Pcidev *p;
  888. p = nil;
  889. while(p = pcimatch(p, 0x104B, 0)){
  890. mb = mballoc(sizeof(Adapter), 0, Mxxx);
  891. ap = (Adapter*)mb->data;
  892. ap->port = p->mem[0].bar & ~0x01;
  893. ap->pcidev = p;
  894. mb->next = adapter;
  895. adapter = mb;
  896. }
  897. }
  898. Scsiio
  899. buslogic(int ctlrno, ISAConf* isa)
  900. {
  901. Scsiio io;
  902. Ctlr *ctlr;
  903. Adapter *ap;
  904. Pcidev *pcidev;
  905. Msgbuf *mb, **mbb;
  906. uchar cmd[6], data[256];
  907. int bus, cmdlen, datalen, port, timeo, wide;
  908. static int scandone;
  909. if(scandone == 0){
  910. buslogicpci();
  911. scandone = 1;
  912. }
  913. /*
  914. * Any adapter matches if no isa->port is supplied,
  915. * otherwise the ports must match.
  916. */
  917. port = 0;
  918. pcidev = nil;
  919. mbb = &adapter;
  920. for(mb = *mbb; mb != nil; mb = mb->next){
  921. ap = (Adapter*)mb->data;
  922. if(isa->port == 0 || isa->port == ap->port){
  923. port = ap->port;
  924. pcidev = ap->pcidev;
  925. *mbb = mb->next;
  926. mbfree(mb);
  927. break;
  928. }
  929. mbb = &mb->next;
  930. }
  931. if(port == 0){
  932. if(isa->port == 0)
  933. return nil;
  934. port = isa->port;
  935. }
  936. isa->port = port;
  937. /*
  938. * Attempt to hard-reset the board and reset
  939. * the SCSI bus. If the board state doesn't settle to
  940. * idle with mailbox initialisation required, either
  941. * it isn't a compatible board or it's broken.
  942. * If the controller has SCAM set this can take a while.
  943. */
  944. outb(port+Rcontrol, Rhard|Rsbus);
  945. for(timeo = 0; timeo < 100; timeo++){
  946. if(inb(port+Rstatus) == (Inreq|Hardy))
  947. break;
  948. delay(100);
  949. }
  950. if(inb(port+Rstatus) != (Inreq|Hardy)){
  951. print("scsi#%d: %s: port 0x%ux failed to hard-reset 0x%ux\n",
  952. ctlrno, isa->type, port, inb(port+Rstatus));
  953. return 0;
  954. }
  955. /*
  956. * Try to determine if this is a Buslogic 32-bit controller
  957. * by attempting to obtain the extended inquiry information;
  958. * this command is not implemented on Adaptec 154xx
  959. * controllers. If successful, the first byte of the returned
  960. * data is the host adapter bus type, 'E' for 32-bit EISA,
  961. * PCI and VLB buses.
  962. */
  963. cmd[0] = Ciesi;
  964. cmd[1] = 14;
  965. cmdlen = 2;
  966. datalen = 256;
  967. bus = 24;
  968. wide = 0;
  969. datalen = issue(ctlrno, port, cmd, cmdlen, data, datalen);
  970. if(datalen >= 0){
  971. if(data[0] == 'E')
  972. bus = 32;
  973. wide = data[0x0D] & 0x01;
  974. }
  975. else{
  976. /*
  977. * Inconceivable though it may seem, a hard controller reset is
  978. * necessary here to clear out the command queue. Every board seems to
  979. * lock-up in a different way if you give an invalid command and then
  980. * try to clear out the command/parameter and/or data-in register.
  981. * Soft reset doesn't do the job either. Fortunately no serious
  982. * initialisation has been done yet so there's nothing to tidy up.
  983. */
  984. outb(port+Rcontrol, Rhard);
  985. for(timeo = 0; timeo < 100; timeo++){
  986. if(inb(port+Rstatus) == (Inreq|Hardy))
  987. break;
  988. delay(100);
  989. }
  990. if(inb(port+Rstatus) != (Inreq|Hardy)){
  991. print("scsi#%d: %s: port 0x%ux Ciesi 0x%ux\n",
  992. ctlrno, isa->type, port, inb(port+Rstatus));
  993. return 0;
  994. }
  995. }
  996. /*
  997. * If the BIOS is enabled on the 1542C/CF and BIOS options for support of drives
  998. * > 1Gb, dynamic scanning of the SCSI bus or more than 2 drives under DOS 5.0
  999. * are enabled, the BIOS disables accepting Cmbinit to protect against running
  1000. * with drivers which don't support those options. In order to unlock the
  1001. * interface it is necessary to read a lock-code using Cextbios and write it back
  1002. * using Cmbienable; the lock-code is non-zero.
  1003. */
  1004. cmd[0] = Cinquiry;
  1005. cmdlen = 1;
  1006. datalen = 4;
  1007. if(issue(ctlrno, port, cmd, cmdlen, data, datalen) < 0){
  1008. print("scsi#%d: %s: Cinquiry\n", ctlrno, isa->type);
  1009. return 0;
  1010. }
  1011. if(data[0] >= 0x43){
  1012. cmd[0] = Cextbios;
  1013. cmdlen = 1;
  1014. datalen = 2;
  1015. if(issue(ctlrno, port, cmd, cmdlen, data, datalen) < 0){
  1016. print("scsi#%d: %s: Cextbios\n", ctlrno, isa->type);
  1017. return 0;
  1018. }
  1019. /*
  1020. * Lock-code returned in data[1]. If it's non-zero write it back
  1021. * along with bit 0 of byte 0 cleared to enable mailbox initialisation.
  1022. */
  1023. if(data[1]){
  1024. cmd[0] = Cmbienable;
  1025. cmd[1] = 0;
  1026. cmd[2] = data[1];
  1027. cmdlen = 3;
  1028. if(issue(ctlrno, port, cmd, cmdlen, 0, 0) < 0){
  1029. print("scsi#%d: %s: Cmbienable\n", ctlrno, isa->type);
  1030. return 0;
  1031. }
  1032. }
  1033. }
  1034. /*
  1035. * Get the DMA, IRQ and adapter SCSI ID from the board.
  1036. * This is necessary as the DMA won't be set up if the it's
  1037. * not a PCI adapter and the BIOS is disabled.
  1038. */
  1039. cmd[0] = Cinquire;
  1040. cmdlen = 1;
  1041. datalen = 3;
  1042. if(issue(ctlrno, port, cmd, cmdlen, data, datalen) < 0){
  1043. print("scsi#%d: %s: can't inquire configuration\n", ctlrno, isa->type);
  1044. return 0;
  1045. }
  1046. if(pcidev && pcidev->intl)
  1047. isa->irq = pcidev->intl;
  1048. else{
  1049. switch(data[0]){ /* DMA Arbitration Priority */
  1050. case 0x80: /* Channel 7 */
  1051. outb(0xD6, 0xC3);
  1052. outb(0xD4, 0x03);
  1053. isa->dma = 7;
  1054. break;
  1055. case 0x40: /* Channel 6 */
  1056. outb(0xD6, 0xC2);
  1057. outb(0xD4, 0x02);
  1058. isa->dma = 6;
  1059. break;
  1060. case 0x20: /* Channel 5 */
  1061. outb(0xD6, 0xC1);
  1062. outb(0xD4, 0x01);
  1063. isa->dma = 5;
  1064. break;
  1065. case 0x01: /* Channel 0 */
  1066. outb(0x0B, 0xC0);
  1067. outb(0x0A, 0x00);
  1068. isa->dma = 0;
  1069. break;
  1070. default:
  1071. /*
  1072. * No DMA channel will show for 32-bit EISA/VLB
  1073. * cards which don't have ISA DMA compatibility set.
  1074. * Carry on regardless.
  1075. */
  1076. isa->dma = -1;
  1077. break;
  1078. }
  1079. switch(data[1]){ /* Interrupt Channel */
  1080. case 0x40:
  1081. isa->irq = 15;
  1082. break;
  1083. case 0x20:
  1084. isa->irq = 14;
  1085. break;
  1086. case 0x08:
  1087. isa->irq = 12;
  1088. break;
  1089. case 0x04:
  1090. isa->irq = 11;
  1091. break;
  1092. case 0x02:
  1093. isa->irq = 10;
  1094. break;
  1095. case 0x01:
  1096. isa->irq = 9;
  1097. break;
  1098. default:
  1099. print("scsi#%d: %s: invalid irq #%2.2ux\n",
  1100. ctlrno, isa->type, data[1]);
  1101. return 0;
  1102. }
  1103. }
  1104. /*
  1105. * Allocate and start to initialise the software controller.
  1106. */
  1107. ctlr = ialloc(sizeof(Ctlr), 0);
  1108. ctlr->port = isa->port;
  1109. ctlr->id = data[2] & 0x07;
  1110. ctlr->ctlrno = ctlrno;
  1111. ctlr->bus = bus;
  1112. ctlr->wide = wide;
  1113. if(pcidev)
  1114. ctlr->tbdf = pcidev->tbdf;
  1115. else
  1116. ctlr->tbdf = BUSUNKNOWN;
  1117. if(bus == 24)
  1118. io = buslogic24(ctlr, isa);
  1119. else
  1120. io = buslogic32(ctlr, isa);
  1121. if(io){
  1122. if(ctrls == 0)
  1123. cmd_install("scsi", "-- scsi stats", cmd_scsi);
  1124. ctrls |= 1<<ctlrno;
  1125. }
  1126. return io;
  1127. }