sdmylex.c 27 KB

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