sdmv50xx.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. /*
  2. * Marvell 88SX5040, 5041, 5080, 5081 driver
  3. * This is a heavily-modified version of a driver written by Coraid, Inc.
  4. * The original copyright notice appears at the end of this file.
  5. */
  6. #include "u.h"
  7. #include "../port/lib.h"
  8. #include "mem.h"
  9. #include "dat.h"
  10. #include "fns.h"
  11. #include "io.h"
  12. #include "../port/error.h"
  13. #include "../port/sd.h"
  14. #define DPRINT if(0)iprint
  15. enum {
  16. SrbRing = 32,
  17. /* Addresses of ATA register */
  18. ARcmd = 027,
  19. ARdev = 026,
  20. ARerr = 021,
  21. ARfea = 021,
  22. ARlba2 = 025,
  23. ARlba1 = 024,
  24. ARlba0 = 023,
  25. ARseccnt = 022,
  26. ARstat = 027,
  27. ATAerr = (1<<0),
  28. ATAdrq = (1<<3),
  29. ATAdf = (1<<5),
  30. ATAdrdy = (1<<6),
  31. ATAbusy = (1<<7),
  32. ATAabort = (1<<2),
  33. ATAeIEN = (1<<1),
  34. ATAsrst = (1<<2),
  35. ATAhob = (1<<7),
  36. SFdone = (1<<0),
  37. SFerror = (1<<1),
  38. SRBident = 0,
  39. SRBread,
  40. SRBwrite,
  41. SRBsmart,
  42. SRBnodata = 0,
  43. SRBdatain,
  44. SRBdataout,
  45. RQread = 1, /* data coming IN from device */
  46. PRDeot = (1<<15),
  47. /* EDMA interrupt error cause register */
  48. ePrtDataErr = (1<<0),
  49. ePrtPRDErr = (1<<1),
  50. eDevErr = (1<<2),
  51. eDevDis = (1<<3),
  52. eDevCon = (1<<4),
  53. eOverrun = (1<<5),
  54. eUnderrun = (1<<6),
  55. eSelfDis = (1<<8),
  56. ePrtCRQBErr = (1<<9),
  57. ePrtCRPBErr = (1<<10),
  58. ePrtIntErr = (1<<11),
  59. eIORdyErr = (1<<12),
  60. /* EDMA Command Register */
  61. eEnEDMA = (1<<0),
  62. eDsEDMA = (1<<1),
  63. eAtaRst = (1<<2),
  64. /* Interrupt mask for errors we care about */
  65. IEM = (eDevDis | eDevCon | eSelfDis),
  66. Dnull = 0,
  67. Dnew,
  68. Dident,
  69. Dready,
  70. Derror,
  71. Dmissing,
  72. Dunconfig,
  73. Dext = (1<<0), /* use ext commands */
  74. Dpio = (1<<1), /* doing pio */
  75. Dwanted = (1<<2), /* someone wants an srb entry */
  76. Dedma = (1<<3), /* device in edma mode */
  77. Dpiowant = (1<<4), /* some wants to use the pio mode */
  78. };
  79. static char* diskstates[] =
  80. {
  81. "null",
  82. "new",
  83. "ident",
  84. "ready",
  85. "error",
  86. "missing",
  87. "unconfigured",
  88. };
  89. extern SDifc sdmv50xxifc;
  90. typedef struct Arb Arb;
  91. typedef struct Bridge Bridge;
  92. typedef struct Chip Chip;
  93. typedef struct Ctlr Ctlr;
  94. typedef struct Drive Drive;
  95. typedef struct Edma Edma;
  96. typedef struct Prd Prd;
  97. typedef struct Rx Rx;
  98. typedef struct Srb Srb;
  99. typedef struct Tx Tx;
  100. struct Chip /* pointers to per-Chip mmio */
  101. {
  102. Arb *arb;
  103. Edma *edma; /* array of 4 */
  104. };
  105. struct Drive /* a single disk */
  106. {
  107. Lock;
  108. Ctlr *ctlr;
  109. SDunit *unit;
  110. int subno;
  111. char name[10];
  112. Bridge *bridge;
  113. Edma *edma;
  114. Chip *chip;
  115. int chipx;
  116. int state;
  117. int flag;
  118. uvlong sectors;
  119. char serial[20+1];
  120. char firmware[8+1];
  121. char model[40+1];
  122. ushort info[256];
  123. Srb *srb[SrbRing-1];
  124. int nsrb;
  125. Prd *prd;
  126. Tx *tx;
  127. Rx *rx;
  128. Srb *srbhead;
  129. Srb *srbtail;
  130. };
  131. struct Ctlr /* a single PCI card */
  132. {
  133. Lock;
  134. int irq;
  135. int tbdf;
  136. SDev *sdev;
  137. Pcidev *pcidev;
  138. uchar *mmio;
  139. Chip chip[2];
  140. int nchip;
  141. Drive drive[8];
  142. int ndrive;
  143. };
  144. struct Srb /* request buffer */
  145. {
  146. Lock;
  147. Rendez;
  148. Srb *next;
  149. Drive *drive;
  150. uvlong blockno;
  151. int count;
  152. int req;
  153. int flag;
  154. uchar *data;
  155. uchar cmd;
  156. uchar lba[6];
  157. uchar sectors;
  158. int sta;
  159. int err;
  160. };
  161. /*
  162. * Memory-mapped I/O registers in many forms.
  163. */
  164. struct Bridge /* memory-mapped per-Drive registers */
  165. {
  166. ulong status;
  167. ulong serror;
  168. ulong sctrl;
  169. ulong phyctrl;
  170. char fill1[0x2c];
  171. ulong ctrl;
  172. char fill2[0x34];
  173. ulong phymode;
  174. char fill3[0x88]; /* pad to 0x100 in length */
  175. };
  176. struct Arb /* memory-mapped per-Chip registers */
  177. {
  178. ulong fill0;
  179. ulong rqop; /* request queue out-pointer */
  180. ulong rqip; /* response queue in pointer */
  181. ulong ict; /* inerrupt caolescing threshold */
  182. ulong itt; /* interrupt timer threshold */
  183. ulong ic; /* interrupt cause */
  184. ulong btc; /* bridges test control */
  185. ulong bts; /* bridges test status */
  186. ulong bpc; /* bridges pin configuration */
  187. char fill1[0xdc];
  188. Bridge bridge[4];
  189. };
  190. struct Edma /* memory-mapped per-Drive DMA-related registers */
  191. {
  192. ulong config; /* configuration register */
  193. ulong timer;
  194. ulong iec; /* interrupt error cause */
  195. ulong iem; /* interrupt error mask */
  196. ulong txbasehi; /* request queue base address high */
  197. ulong txi; /* request queue in pointer */
  198. ulong txo; /* request queue out pointer */
  199. ulong rxbasehi; /* response queue base address high */
  200. ulong rxi; /* response queue in pointer */
  201. ulong rxo; /* response queue out pointer */
  202. ulong ctl; /* command register */
  203. ulong testctl; /* test control */
  204. ulong status;
  205. ulong iordyto; /* IORDY timeout */
  206. char fill[0xc8];
  207. ushort pio; /* data register */
  208. char pad0[2];
  209. uchar err; /* features and error */
  210. char pad1[3];
  211. uchar seccnt; /* sector count */
  212. char pad2[3];
  213. uchar lba0;
  214. char pad3[3];
  215. uchar lba1;
  216. char pad4[3];
  217. uchar lba2;
  218. char pad5[3];
  219. uchar lba3;
  220. char pad6[3];
  221. uchar cmdstat; /* cmd/status */
  222. char pad7[3];
  223. uchar altstat; /* alternate status */
  224. char fill2[0x1edc]; /* pad to 0x2000 bytes */
  225. };
  226. /*
  227. * Memory structures shared with card.
  228. */
  229. struct Prd /* physical region descriptor */
  230. {
  231. ulong pa; /* byte address of physical memory */
  232. ushort count; /* byte count (bit0 must be 0) */
  233. ushort flag;
  234. ulong zero; /* high long of 64 bit address */
  235. ulong reserved;
  236. };
  237. struct Tx /* command request block */
  238. {
  239. ulong prdpa; /* physical region descriptor table structures */
  240. ulong zero; /* must be zero (high long of prd address) */
  241. ushort flag; /* control flags */
  242. ushort regs[11];
  243. };
  244. struct Rx /* command response block */
  245. {
  246. ushort cid; /* cID of response */
  247. uchar cEdmaSts; /* EDMA status */
  248. uchar cDevSts; /* status from disk */
  249. ulong ts; /* time stamp */
  250. };
  251. /*
  252. * Little-endian parsing for drive data.
  253. */
  254. static ushort
  255. lhgets(void *p)
  256. {
  257. uchar *a = p;
  258. return ((ushort) a[1] << 8) | a[0];
  259. }
  260. static ulong
  261. lhgetl(void *p)
  262. {
  263. uchar *a = p;
  264. return ((ulong) lhgets(a+2) << 16) | lhgets(a);
  265. }
  266. static uvlong
  267. lhgetv(void *p)
  268. {
  269. uchar *a = p;
  270. return ((uvlong) lhgetl(a+4) << 32) | lhgetl(a);
  271. }
  272. static void
  273. idmove(char *p, ushort *a, int n)
  274. {
  275. char *op;
  276. int i;
  277. op = p;
  278. for(i=0; i<n/2; i++){
  279. *p++ = a[i]>>8;
  280. *p++ = a[i];
  281. }
  282. while(p>op && *--p == ' ')
  283. *p = 0;
  284. }
  285. /*
  286. * Request buffers.
  287. */
  288. struct
  289. {
  290. Lock;
  291. Srb *freechain;
  292. int nalloc;
  293. } srblist;
  294. static Srb*
  295. allocsrb(void)
  296. {
  297. Srb *p;
  298. ilock(&srblist);
  299. if((p = srblist.freechain) == nil){
  300. srblist.nalloc++;
  301. iunlock(&srblist);
  302. p = smalloc(sizeof *p);
  303. }else{
  304. srblist.freechain = p->next;
  305. iunlock(&srblist);
  306. }
  307. return p;
  308. }
  309. static void
  310. freesrb(Srb *p)
  311. {
  312. ilock(&srblist);
  313. p->next = srblist.freechain;
  314. srblist.freechain = p;
  315. iunlock(&srblist);
  316. }
  317. /*
  318. * Wait for a byte to be a particular value.
  319. */
  320. static int
  321. satawait(uchar *p, uchar mask, uchar v, int ms)
  322. {
  323. int i;
  324. // DPRINT("satawait %p %#x %#x %d...", p, mask, v, ms);
  325. // DPRINT("!%#x...", *p);
  326. for(i=0; i<ms && (*p & mask) != v; i++){
  327. if(i%1000 == 0)
  328. DPRINT("!%#x", *p);
  329. microdelay(1000);
  330. }
  331. return (*p & mask) == v;
  332. }
  333. /*
  334. * Drive initialization
  335. */
  336. static int
  337. configdrive(Ctlr *ctlr, Drive *d, SDunit *unit)
  338. {
  339. int i;
  340. ulong *r;
  341. DPRINT("%s: configdrive\n", unit->name);
  342. d->unit = unit;
  343. d->ctlr = ctlr;
  344. d->chipx = unit->subno%4;
  345. d->chip = &ctlr->chip[unit->subno/4];
  346. d->bridge = &d->chip->arb->bridge[d->chipx];
  347. d->edma = &d->chip->edma[d->chipx];
  348. if(d->tx == nil){
  349. d->tx = mallocalign(32*sizeof(Tx), 1024, 0, 0);
  350. d->rx = mallocalign(32*sizeof(Rx), 256, 0, 0);
  351. d->prd = mallocalign(32*sizeof(Prd), 32, 0, 0);
  352. if(d->tx == nil || d->rx == nil || d->prd == nil){
  353. iprint("%s: out of memory allocating ring buffers\n",
  354. unit->name);
  355. free(d->tx);
  356. d->tx = nil;
  357. free(d->rx);
  358. d->rx = nil;
  359. free(d->prd);
  360. d->prd = nil;
  361. d->state = Dunconfig;
  362. return 0;
  363. }
  364. for(i=0; i<32; i++)
  365. d->tx[i].prdpa = PADDR(&d->prd[i]);
  366. coherence();
  367. }
  368. /* leave disk interrupts turned off until we use it ... */
  369. d->edma->iem = 0;
  370. /* ... but enable them on the controller */
  371. r = (ulong*)(d->ctlr->mmio + 0x1D64);
  372. if(d->unit->subno < 4)
  373. *r |= 3 << (d->chipx*2);
  374. else
  375. *r |= 3 << (d->chipx*2+9);
  376. return 1;
  377. }
  378. static int
  379. enabledrive(Drive *d)
  380. {
  381. Edma *edma;
  382. DPRINT("%s: enabledrive\n", d->unit->name);
  383. if((d->bridge->status & 0xF) != 0x3){ /* Det */
  384. DPRINT("%s: not present\n", d->unit->name);
  385. d->state = Dmissing;
  386. return 0;
  387. }
  388. edma = d->edma;
  389. if(satawait(&edma->cmdstat, ATAbusy, 0, 10*1000) == 0){
  390. print("%s: busy timeout\n", d->unit->name);
  391. d->state = Dmissing;
  392. return 0;
  393. }
  394. edma->iec = 0;
  395. d->chip->arb->ic &= ~(0x101 << d->chipx);
  396. edma->config = 0x11F;
  397. edma->txi = PADDR(d->tx);
  398. edma->txo = (ulong)d->tx & 0x3E0;
  399. edma->rxi = (ulong)d->rx & 0xF8;
  400. edma->rxo = PADDR(d->rx);
  401. edma->ctl |= 1; /* enable dma */
  402. DPRINT("%s: enable interrupts\n", d->unit->name);
  403. if(d->bridge->status = 0x113)
  404. d->state = Dnew;
  405. d->edma->iem = IEM;
  406. return 1;
  407. }
  408. static void
  409. disabledrive(Drive *d)
  410. {
  411. int i;
  412. ulong *r;
  413. DPRINT("%s: disabledrive\n", d->unit->name);
  414. if(d->tx == nil) /* never enabled */
  415. return;
  416. d->edma->ctl = 0;
  417. d->edma->iem = 0;
  418. r = (ulong*)(d->ctlr->mmio + 0x1D64);
  419. i = d->chipx;
  420. if(d->chipx < 4)
  421. *r &= ~(3 << (i*2));
  422. else
  423. *r |= ~(3 << (i*2+9));
  424. }
  425. static int
  426. setudmamode(Drive *d, uchar mode)
  427. {
  428. Edma *edma;
  429. DPRINT("%s: setudmamode %d\n", d->unit->name, mode);
  430. edma = d->edma;
  431. if(satawait(&edma->cmdstat, ATAerr|ATAdrq|ATAdf|ATAdrdy|ATAbusy, ATAdrdy, 15*1000) == 0){
  432. iprint("%s: cmdstat 0x%.2ux ready timeout\n",
  433. d->unit->name, edma->cmdstat);
  434. return 0;
  435. }
  436. edma->altstat = ATAeIEN;
  437. edma->err = 3;
  438. edma->seccnt = 0x40 | mode;
  439. edma->cmdstat = 0xEF;
  440. microdelay(1);
  441. if(satawait(&edma->cmdstat, ATAbusy, 0, 15*1000) == 0){
  442. iprint("%s: cmdstat 0x%.2ux busy timeout\n",
  443. d->unit->name, edma->cmdstat);
  444. return 0;
  445. }
  446. return 1;
  447. }
  448. static void
  449. identifydrive(Drive *d)
  450. {
  451. int i;
  452. ushort *id;
  453. Edma *edma;
  454. SDunit *unit;
  455. DPRINT("%s: identifydrive\n", d->unit->name);
  456. if(setudmamode(d, 5) == 0) /* do all SATA support 5? */
  457. goto Error;
  458. id = d->info;
  459. memset(d->info, 0, sizeof d->info);
  460. edma = d->edma;
  461. if(satawait(&edma->cmdstat, 0xE9, 0x40, 15*1000) == 0)
  462. goto Error;
  463. edma->altstat = ATAeIEN; /* no interrupts */
  464. edma->cmdstat = 0xEC;
  465. microdelay(1);
  466. if(satawait(&edma->cmdstat, ATAbusy, 0, 15*1000) == 0)
  467. goto Error;
  468. for(i=0; i<256; i++)
  469. id[i] = edma->pio;
  470. if(edma->cmdstat & (ATAerr|ATAdf))
  471. goto Error;
  472. i = lhgets(id+83) | lhgets(id+86);
  473. if(i & (1<<10)){
  474. d->flag |= Dext;
  475. d->sectors = lhgetv(id+100);
  476. }else{
  477. d->flag &= ~Dext;
  478. d->sectors = lhgetl(id+60);
  479. }
  480. idmove(d->serial, id+10, 20);
  481. idmove(d->firmware, id+23, 8);
  482. idmove(d->model, id+27, 40);
  483. unit = d->unit;
  484. memset(unit->inquiry, 0, sizeof unit->inquiry);
  485. unit->inquiry[2] = 2;
  486. unit->inquiry[3] = 2;
  487. unit->inquiry[4] = sizeof(unit->inquiry)-4;
  488. idmove((char*)unit->inquiry+8, id+27, 40);
  489. if(enabledrive(d))
  490. d->state = Dready;
  491. else
  492. d->state = Derror;
  493. return;
  494. Error:
  495. DPRINT("error...");
  496. d->state = Derror;
  497. }
  498. static void abortallsrb(Drive*);
  499. static void
  500. updatedrive(Drive *d, ulong cause)
  501. {
  502. int x;
  503. Edma *edma;
  504. if(cause == 0)
  505. return;
  506. DPRINT("%s: updatedrive %#lux\n", d->unit->name, cause);
  507. edma = d->edma;
  508. if(cause & eDevDis){
  509. d->state = Dmissing;
  510. edma->ctl |= eAtaRst;
  511. microdelay(25);
  512. edma->ctl &= ~eAtaRst;
  513. microdelay(25);
  514. }
  515. if(cause & eDevCon){
  516. d->bridge->sctrl = (d->bridge->sctrl & ~0xF) | 1;
  517. d->state = Dnew;
  518. }
  519. if(cause & eSelfDis)
  520. d->state = Derror;
  521. edma->iec = 0;
  522. d->sectors = 0;
  523. d->unit->sectors = 0;
  524. abortallsrb(d);
  525. x = edma->cmdstat;
  526. USED(x);
  527. }
  528. /*
  529. * Requests
  530. */
  531. static Srb*
  532. srbrw(int req, Drive *d, uchar *data, uint sectors, uvlong lba)
  533. {
  534. int i;
  535. Srb *srb;
  536. static uchar cmd[2][2] = { 0xC8, 0x25, 0xCA, 0x35 };
  537. switch(req){
  538. case SRBread:
  539. case SRBwrite:
  540. break;
  541. default:
  542. return nil;
  543. }
  544. srb = allocsrb();
  545. srb->req = req;
  546. srb->drive = d;
  547. srb->blockno = lba;
  548. srb->sectors = sectors;
  549. srb->count = sectors*512;
  550. srb->flag = 0;
  551. srb->data = data;
  552. for(i=0; i<6; i++)
  553. srb->lba[i] = lba >> (8*i);
  554. srb->cmd = cmd[srb->req!=SRBread][(d->flag&Dext)!=0];
  555. return srb;
  556. }
  557. static uintptr
  558. advance(uintptr pa, int shift)
  559. {
  560. int n, mask;
  561. mask = 0x1F<<shift;
  562. n = (pa & mask) + (1<<shift);
  563. return (pa & ~mask) | (n & mask);
  564. }
  565. #define CMD(r, v) (((r)<<8) | ((v)&0xFF))
  566. static void
  567. atarequest(ushort *cmd, Srb *srb, int ext)
  568. {
  569. *cmd++ = CMD(ARseccnt, 0);
  570. *cmd++ = CMD(ARseccnt, srb->sectors);
  571. *cmd++ = CMD(ARfea, 0);
  572. if(ext){
  573. *cmd++ = CMD(ARlba0, srb->lba[3]);
  574. *cmd++ = CMD(ARlba0, srb->lba[0]);
  575. *cmd++ = CMD(ARlba1, srb->lba[4]);
  576. *cmd++ = CMD(ARlba1, srb->lba[1]);
  577. *cmd++ = CMD(ARlba2, srb->lba[5]);
  578. *cmd++ = CMD(ARlba2, srb->lba[2]);
  579. *cmd++ = CMD(ARdev, 0xE0);
  580. }else{
  581. *cmd++ = CMD(ARlba0, srb->lba[0]);
  582. *cmd++ = CMD(ARlba1, srb->lba[1]);
  583. *cmd++ = CMD(ARlba2, srb->lba[2]);
  584. *cmd++ = CMD(ARdev, srb->lba[3] | 0xE0);
  585. }
  586. *cmd++ = CMD(ARcmd, srb->cmd) | (1<<15);
  587. USED(cmd);
  588. }
  589. static void
  590. startsrb(Drive *d, Srb *srb)
  591. {
  592. int i;
  593. Edma *edma;
  594. Prd *prd;
  595. Tx *tx;
  596. if(d->nsrb >= nelem(d->srb)){
  597. srb->next = nil;
  598. if(d->srbhead)
  599. d->srbtail->next = srb;
  600. else
  601. d->srbhead = srb;
  602. d->srbtail = srb;
  603. return;
  604. }
  605. d->nsrb++;
  606. for(i=0; i<nelem(d->srb); i++)
  607. if(d->srb[i] == nil)
  608. break;
  609. if(i == nelem(d->srb))
  610. panic("sdmv50xx: no free srbs");
  611. d->srb[i] = srb;
  612. edma = d->edma;
  613. tx = (Tx*)KADDR(edma->txi);
  614. tx->flag = (i<<1) | (srb->req == SRBread);
  615. prd = KADDR(tx->prdpa);
  616. prd->pa = PADDR(srb->data);
  617. prd->count = srb->count;
  618. prd->flag = PRDeot;
  619. atarequest(tx->regs, srb, d->flag&Dext);
  620. coherence();
  621. edma->txi = advance(edma->txi, 5);
  622. }
  623. static void
  624. completesrb(Drive *d)
  625. {
  626. Edma *edma;
  627. Rx *rx;
  628. Srb *srb;
  629. edma = d->edma;
  630. if((edma->ctl & eEnEDMA) == 0)
  631. return;
  632. while((edma->rxo & (0x1F<<3)) != (edma->rxi & (0x1F<<3))){
  633. rx = (Rx*)KADDR(edma->rxo);
  634. if(srb = d->srb[rx->cid]){
  635. d->srb[rx->cid] = nil;
  636. d->nsrb--;
  637. if(rx->cDevSts & (ATAerr|ATAdf))
  638. srb->flag |= SFerror;
  639. srb->flag |= SFdone;
  640. srb->sta = rx->cDevSts;
  641. wakeup(srb);
  642. }else
  643. iprint("srb missing\n");
  644. edma->rxo = advance(edma->rxo, 3);
  645. if(srb = d->srbhead){
  646. d->srbhead = srb->next;
  647. startsrb(d, srb);
  648. }
  649. }
  650. }
  651. static void
  652. abortallsrb(Drive *d)
  653. {
  654. int i;
  655. Srb *srb;
  656. for(i=0; i<nelem(d->srb); i++){
  657. if(srb = d->srb[i]){
  658. d->srb[i] = nil;
  659. d->nsrb--;
  660. srb->flag |= SFerror|SFdone;
  661. wakeup(srb);
  662. }
  663. }
  664. while(srb = d->srbhead){
  665. d->srbhead = srb->next;
  666. srb->flag |= SFerror|SFdone;
  667. wakeup(srb);
  668. }
  669. }
  670. static int
  671. srbdone(void *v)
  672. {
  673. Srb *srb;
  674. srb = v;
  675. return srb->flag & SFdone;
  676. }
  677. /*
  678. * Interrupts
  679. */
  680. static void
  681. mv50interrupt(Ureg*, void *a)
  682. {
  683. int i;
  684. ulong cause;
  685. Ctlr *ctlr;
  686. Drive *drive;
  687. ctlr = a;
  688. ilock(ctlr);
  689. cause = *(ulong*)(ctlr->mmio + 0x1D60);
  690. DPRINT("sd%c: mv50interrupt: 0x%lux\n", ctlr->sdev->idno, cause);
  691. for(i=0; i<ctlr->ndrive; i++){
  692. if(cause & (3<<(i*2+i/4))){
  693. drive = &ctlr->drive[i];
  694. ilock(drive);
  695. updatedrive(drive, drive->edma->iec);
  696. while(ctlr->chip[i/4].arb->ic & (0x0101 << (i%4))){
  697. ctlr->chip[i/4].arb->ic = ~(0x101 << (i%4));
  698. completesrb(drive);
  699. }
  700. iunlock(drive);
  701. }
  702. }
  703. iunlock(ctlr);
  704. }
  705. /*
  706. * Device discovery
  707. */
  708. static SDev*
  709. mv50pnp(void)
  710. {
  711. int i, nunit;
  712. uchar *base;
  713. ulong io;
  714. void *mem;
  715. Ctlr *ctlr;
  716. Pcidev *p;
  717. SDev *head, *tail, *sdev;
  718. DPRINT("mv50pnp\n");
  719. p = nil;
  720. head = nil;
  721. tail = nil;
  722. while((p = pcimatch(p, 0x11AB, 0)) != nil){
  723. switch(p->did){
  724. case 0x5041:
  725. nunit = 4;
  726. break;
  727. case 0x5081:
  728. nunit = 8;
  729. break;
  730. default:
  731. continue;
  732. }
  733. if((sdev = malloc(sizeof(SDev))) == nil)
  734. continue;
  735. if((ctlr = malloc(sizeof(Ctlr))) == nil){
  736. free(sdev);
  737. continue;
  738. }
  739. io = p->mem[0].bar & ~0x0F;
  740. mem = vmap(io, p->mem[0].size);
  741. if(mem == 0){
  742. print("sdmv50xx: address 0x%luX in use\n", io);
  743. free(sdev);
  744. free(ctlr);
  745. continue;
  746. }
  747. sdev->ifc = &sdmv50xxifc;
  748. sdev->ctlr = ctlr;
  749. sdev->nunit = nunit;
  750. sdev->idno = 'E';
  751. ctlr->sdev = sdev;
  752. ctlr->irq = p->intl;
  753. ctlr->tbdf = p->tbdf;
  754. ctlr->pcidev = p;
  755. ctlr->mmio = mem;
  756. ctlr->nchip = (nunit+3)/4;
  757. ctlr->ndrive = nunit;
  758. for(i=0; i<ctlr->nchip; i++){
  759. base = ctlr->mmio+0x20000+0x10000*i;
  760. ctlr->chip[i].arb = (Arb*)base;
  761. ctlr->chip[i].edma = (Edma*)(base + 0x2000);
  762. }
  763. if(head)
  764. tail->next = sdev;
  765. else
  766. head = sdev;
  767. tail = sdev;
  768. }
  769. return head;
  770. }
  771. /*
  772. * Enable the controller. Each disk has its own interrupt mask,
  773. * and those get enabled as the disks are brought online.
  774. */
  775. static int
  776. mv50enable(SDev *sdev)
  777. {
  778. char name[32];
  779. Ctlr *ctlr;
  780. DPRINT("sd%c: enable\n", sdev->idno);
  781. ctlr = sdev->ctlr;
  782. snprint(name, sizeof name, "%s (%s)", sdev->name, sdev->ifc->name);
  783. intrenable(ctlr->irq, mv50interrupt, ctlr, ctlr->tbdf, name);
  784. return 1;
  785. }
  786. /*
  787. * Disable the controller.
  788. */
  789. static int
  790. mv50disable(SDev *sdev)
  791. {
  792. char name[32];
  793. int i;
  794. Ctlr *ctlr;
  795. Drive *drive;
  796. DPRINT("sd%c: disable\n", sdev->idno);
  797. ctlr = sdev->ctlr;
  798. ilock(ctlr);
  799. for(i=0; i<ctlr->sdev->nunit; i++){
  800. drive = &ctlr->drive[i];
  801. ilock(drive);
  802. disabledrive(drive);
  803. iunlock(drive);
  804. }
  805. iunlock(ctlr);
  806. snprint(name, sizeof name, "%s (%s)", sdev->name, sdev->ifc->name);
  807. intrdisable(ctlr->irq, mv50interrupt, ctlr, ctlr->tbdf, name);
  808. return 0;
  809. }
  810. /*
  811. * Clean up all disk structures. Already disabled.
  812. * Could keep count of number of allocated controllers
  813. * and free the srblist when it drops to zero.
  814. */
  815. static void
  816. mv50clear(SDev *sdev)
  817. {
  818. int i;
  819. Ctlr *ctlr;
  820. Drive *d;
  821. DPRINT("sd%c: clear\n", sdev->idno);
  822. ctlr = sdev->ctlr;
  823. for(i=0; i<ctlr->ndrive; i++){
  824. d = &ctlr->drive[i];
  825. free(d->tx);
  826. free(d->rx);
  827. free(d->prd);
  828. }
  829. free(ctlr);
  830. }
  831. /*
  832. * Check that there is a disk or at least a hot swap bay in the drive.
  833. */
  834. static int
  835. mv50verify(SDunit *unit)
  836. {
  837. Ctlr *ctlr;
  838. Drive *drive;
  839. DPRINT("%s: verify\n", unit->name);
  840. /*
  841. * First access of unit.
  842. */
  843. ctlr = unit->dev->ctlr;
  844. drive = &ctlr->drive[unit->subno];
  845. ilock(ctlr);
  846. ilock(drive);
  847. if(!configdrive(ctlr, drive, unit) || !enabledrive(drive)){
  848. iunlock(drive);
  849. iunlock(ctlr);
  850. return 0;
  851. }
  852. /*
  853. * Need to reset the drive before the first call to
  854. * identifydrive, or else the satawait in setudma will
  855. * freeze the machine when accessing edma->cmdstat.
  856. * I do not understand this. -rsc
  857. */
  858. updatedrive(drive, eDevDis);
  859. iunlock(drive);
  860. iunlock(ctlr);
  861. return 1;
  862. }
  863. /*
  864. * Check whether the disk is online.
  865. */
  866. static int
  867. mv50online(SDunit *unit)
  868. {
  869. Ctlr *ctlr;
  870. Drive *drive;
  871. ctlr = unit->dev->ctlr;
  872. drive = &ctlr->drive[unit->subno];
  873. ilock(drive);
  874. if(drive->state == Dready){
  875. unit->sectors = drive->sectors;
  876. unit->secsize = 512;
  877. iunlock(drive);
  878. return 1;
  879. }
  880. DPRINT("%s: online %s\n", unit->name, diskstates[drive->state]);
  881. if(drive->state == Dnew){
  882. identifydrive(drive);
  883. if(drive->state == Dready){
  884. unit->sectors = drive->sectors;
  885. unit->secsize = 512;
  886. iunlock(drive);
  887. return 2; /* media changed */
  888. }
  889. }
  890. iunlock(drive);
  891. return 0;
  892. }
  893. /*
  894. * Register dumps
  895. */
  896. typedef struct Regs Regs;
  897. struct Regs
  898. {
  899. ulong offset;
  900. char *name;
  901. };
  902. static Regs regsctlr[] =
  903. {
  904. 0x0C28, "pci serr# mask",
  905. 0x1D40, "pci err addr low",
  906. 0x1D44, "pci err addr hi",
  907. 0x1D48, "pci err attr",
  908. 0x1D50, "pci err cmd",
  909. 0x1D58, "pci intr cause",
  910. 0x1D5C, "pci mask cause",
  911. 0x1D60, "device micr",
  912. 0x1D64, "device mimr",
  913. };
  914. static Regs regsarb[] =
  915. {
  916. 0x0004, "arb rqop",
  917. 0x0008, "arb rqip",
  918. 0x000C, "arb ict",
  919. 0x0010, "arb itt",
  920. 0x0014, "arb ic",
  921. 0x0018, "arb btc",
  922. 0x001C, "arb bts",
  923. 0x0020, "arb bpc",
  924. };
  925. static Regs regsbridge[] =
  926. {
  927. 0x0000, "bridge status",
  928. 0x0004, "bridge serror",
  929. 0x0008, "bridge sctrl",
  930. 0x000C, "bridge phyctrl",
  931. 0x003C, "bridge ctrl",
  932. 0x0074, "bridge phymode",
  933. };
  934. static Regs regsedma[] =
  935. {
  936. 0x0000, "edma config",
  937. 0x0004, "edma timer",
  938. 0x0008, "edma iec",
  939. 0x000C, "edma iem",
  940. 0x0010, "edma txbasehi",
  941. 0x0014, "edma txi",
  942. 0x0018, "edma txo",
  943. 0x001C, "edma rxbasehi",
  944. 0x0020, "edma rxi",
  945. 0x0024, "edma rxo",
  946. 0x0028, "edma c",
  947. 0x002C, "edma tc",
  948. 0x0030, "edma status",
  949. 0x0034, "edma iordyto",
  950. /* 0x0100, "edma pio",
  951. 0x0104, "edma err",
  952. 0x0108, "edma sectors",
  953. 0x010C, "edma lba0",
  954. 0x0110, "edma lba1",
  955. 0x0114, "edma lba2",
  956. 0x0118, "edma lba3",
  957. 0x011C, "edma cmdstat",
  958. 0x0120, "edma altstat",
  959. */
  960. };
  961. static char*
  962. rdregs(char *p, char *e, void *base, Regs *r, int n, char *prefix)
  963. {
  964. int i;
  965. for(i=0; i<n; i++)
  966. p = seprint(p, e, "%s%s%-19s %.8ux\n",
  967. prefix ? prefix : "", prefix ? ": " : "",
  968. r[i].name, *(u32int*)((uchar*)base+r[i].offset));
  969. return p;
  970. }
  971. static char*
  972. rdinfo(char *p, char *e, ushort *info)
  973. {
  974. int i;
  975. p = seprint(p, e, "info");
  976. for(i=0; i<256; i++){
  977. p = seprint(p, e, "%s%.4ux%s",
  978. i%8==0 ? "\t" : "",
  979. info[i],
  980. i%8==7 ? "\n" : "");
  981. }
  982. return p;
  983. }
  984. static int
  985. mv50rctl(SDunit *unit, char *p, int l)
  986. {
  987. char *e, *op;
  988. Ctlr *ctlr;
  989. Drive *drive;
  990. if((ctlr = unit->dev->ctlr) == nil)
  991. return 0;
  992. drive = &ctlr->drive[unit->subno];
  993. e = p+l;
  994. op = p;
  995. if(drive->state == Dready){
  996. p = seprint(p, e, "model %s\n", drive->model);
  997. p = seprint(p, e, "serial %s\n", drive->serial);
  998. p = seprint(p, e, "firmware %s\n", drive->firmware);
  999. }else
  1000. p = seprint(p, e, "no disk present\n");
  1001. p = seprint(p, e, "geometry %llud 512\n", drive->sectors);
  1002. p = rdinfo(p, e, drive->info);
  1003. p = rdregs(p, e, drive->chip->arb, regsarb, nelem(regsarb), nil);
  1004. p = rdregs(p, e, drive->bridge, regsbridge, nelem(regsbridge), nil);
  1005. p = rdregs(p, e, drive->edma, regsedma, nelem(regsedma), nil);
  1006. return p-op;
  1007. }
  1008. static int
  1009. mv50wctl(SDunit *unit, Cmdbuf *cb)
  1010. {
  1011. Ctlr *ctlr;
  1012. Drive *drive;
  1013. USED(unit);
  1014. if(strcmp(cb->f[0], "reset") == 0){
  1015. ctlr = unit->dev->ctlr;
  1016. drive = &ctlr->drive[unit->subno];
  1017. ilock(drive);
  1018. updatedrive(drive, eDevDis);
  1019. iunlock(drive);
  1020. return 0;
  1021. }
  1022. cmderror(cb, Ebadctl);
  1023. return -1;
  1024. }
  1025. static char*
  1026. mv50rtopctl(SDev *sdev, char *p, char *e)
  1027. {
  1028. char name[10];
  1029. Ctlr *ctlr;
  1030. ctlr = sdev->ctlr;
  1031. if(ctlr == nil)
  1032. return p;
  1033. snprint(name, sizeof name, "sd%c", sdev->idno);
  1034. p = rdregs(p, e, ctlr->mmio, regsctlr, nelem(regsctlr), name);
  1035. /* info for first disk */
  1036. p = rdregs(p, e, ctlr->chip[0].arb, regsarb, nelem(regsarb), name);
  1037. p = rdregs(p, e, &ctlr->chip[0].arb->bridge[0], regsbridge, nelem(regsbridge), name);
  1038. p = rdregs(p, e, &ctlr->chip[0].edma[0], regsedma, nelem(regsedma), name);
  1039. return p;
  1040. }
  1041. static int
  1042. mv50rio(SDreq *r)
  1043. {
  1044. int count, max, n, status;
  1045. uchar *cmd, *data;
  1046. uvlong lba;
  1047. Ctlr *ctlr;
  1048. Drive *drive;
  1049. SDunit *unit;
  1050. Srb *srb;
  1051. unit = r->unit;
  1052. ctlr = unit->dev->ctlr;
  1053. drive = &ctlr->drive[unit->subno];
  1054. cmd = r->cmd;
  1055. if((status = sdfakescsi(r, drive->info, sizeof drive->info)) != SDnostatus){
  1056. /* XXX check for SDcheck here */
  1057. r->status = status;
  1058. return status;
  1059. }
  1060. switch(cmd[0]){
  1061. case 0x28: /* read */
  1062. case 0x2A: /* write */
  1063. break;
  1064. default:
  1065. print("sdmv50xx: bad cmd 0x%.2ux\n", cmd[0]);
  1066. r->status = SDcheck;
  1067. return SDcheck;
  1068. }
  1069. lba = (cmd[2]<<24)|(cmd[3]<<16)|(cmd[4]<<8)|cmd[5];
  1070. count = (cmd[7]<<8)|cmd[8];
  1071. if(r->data == nil)
  1072. return SDok;
  1073. if(r->dlen < count*unit->secsize)
  1074. count = r->dlen/unit->secsize;
  1075. /*
  1076. * Could arrange here to have an Srb always outstanding:
  1077. *
  1078. * lsrb = nil;
  1079. * while(count > 0 || lsrb != nil){
  1080. * srb = nil;
  1081. * if(count > 0){
  1082. * srb = issue next srb;
  1083. * }
  1084. * if(lsrb){
  1085. * sleep on lsrb and handle it
  1086. * }
  1087. * }
  1088. *
  1089. * On the disks I tried, this didn't help. If anything,
  1090. * it's a little slower. -rsc
  1091. */
  1092. data = r->data;
  1093. while(count > 0){
  1094. /*
  1095. * Max is 128 sectors (64kB) because prd->count is 16 bits.
  1096. */
  1097. max = 128;
  1098. n = count;
  1099. if(n > max)
  1100. n = max;
  1101. srb = srbrw(cmd[0]==0x28 ? SRBread : SRBwrite, drive, data, n, lba);
  1102. ilock(drive);
  1103. startsrb(drive, srb);
  1104. iunlock(drive);
  1105. /*
  1106. * Cannot let user interrupt the DMA.
  1107. */
  1108. while(waserror())
  1109. ;
  1110. tsleep(srb, srbdone, srb, 60*1000);
  1111. poperror();
  1112. if(!(srb->flag & SFdone)){
  1113. ilock(drive);
  1114. if(!(srb->flag & SFdone)){
  1115. /*
  1116. * DMA didn't finish but we have to let go of
  1117. * the data buffer. Reset the drive to (try to) keep it
  1118. * from using the buffer after we're gone.
  1119. */
  1120. iprint("%s: i/o timeout\n", unit->name);
  1121. updatedrive(drive, eDevDis);
  1122. enabledrive(drive);
  1123. freesrb(srb);
  1124. iunlock(drive);
  1125. error("i/o timeout");
  1126. }
  1127. iunlock(drive);
  1128. }
  1129. if(srb->flag & SFerror){
  1130. freesrb(srb);
  1131. error("i/o error");
  1132. }
  1133. freesrb(srb);
  1134. count -= n;
  1135. lba += n;
  1136. data += n*unit->secsize;
  1137. }
  1138. r->rlen = data - (uchar*)r->data;
  1139. return SDok;
  1140. }
  1141. SDifc sdmv50xxifc = {
  1142. "mv50xx", /* name */
  1143. mv50pnp, /* pnp */
  1144. nil, /* legacy */
  1145. mv50enable, /* enable */
  1146. mv50disable, /* disable */
  1147. mv50verify, /* verify */
  1148. mv50online, /* online */
  1149. mv50rio, /* rio */
  1150. mv50rctl, /* rctl */
  1151. mv50wctl, /* wctl */
  1152. scsibio, /* bio */
  1153. nil, /* probe */
  1154. mv50clear, /* clear */
  1155. mv50rtopctl, /* rtopctl */
  1156. };
  1157. /*
  1158. * The original driver on which this one is based came with the
  1159. * following notice:
  1160. *
  1161. * Copyright 2005
  1162. * Coraid, Inc.
  1163. *
  1164. * This software is provided `as-is,' without any express or implied
  1165. * warranty. In no event will the author be held liable for any damages
  1166. * arising from the use of this software.
  1167. *
  1168. * Permission is granted to anyone to use this software for any purpose,
  1169. * including commercial applications, and to alter it and redistribute it
  1170. * freely, subject to the following restrictions:
  1171. *
  1172. * 1. The origin of this software must not be misrepresented; you must
  1173. * not claim that you wrote the original software. If you use this
  1174. * software in a product, an acknowledgment in the product documentation
  1175. * would be appreciated but is not required.
  1176. *
  1177. * 2. Altered source versions must be plainly marked as such, and must
  1178. * not be misrepresented as being the original software.
  1179. *
  1180. * 3. This notice may not be removed or altered from any source
  1181. * distribution.
  1182. */