sdmv50xx.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755
  1. /*
  2. * Marvell 88SX[56]0[48][01] fileserver Serial ATA (SATA) driver
  3. *
  4. * See MV-S101357-00 Rev B Marvell PCI/PCI-X to 8-Port/4-Port
  5. * SATA Host Controller, ATA-5 ANSI NCITS 340-2000.
  6. *
  7. * This is a heavily-modified version (by Coraid) of a heavily-modified
  8. * version (from The Labs) of a driver written by Coraid, Inc.
  9. * The original copyright notice appears at the end of this file.
  10. */
  11. #include "u.h"
  12. #include "../port/lib.h"
  13. #include "mem.h"
  14. #include "dat.h"
  15. #include "fns.h"
  16. #include "io.h"
  17. #include "../port/error.h"
  18. #include "../port/sd.h"
  19. #define dprint if(!0){}else iprint
  20. #define idprint if(!0){}else iprint
  21. #define ioprint if(!0){}else iprint
  22. enum{
  23. NCtlr = 4,
  24. NCtlrdrv = 8,
  25. NDrive = NCtlr*NCtlrdrv,
  26. Read = 0,
  27. Write,
  28. };
  29. enum {
  30. SrbRing = 32,
  31. /* Addresses of ATA register */
  32. ARcmd = 027,
  33. ARdev = 026,
  34. ARerr = 021,
  35. ARfea = 021,
  36. ARlba2 = 025,
  37. ARlba1 = 024,
  38. ARlba0 = 023,
  39. ARseccnt = 022,
  40. ARstat = 027,
  41. ATAerr = (1<<0),
  42. ATAdrq = (1<<3),
  43. ATAdf = (1<<5),
  44. ATAdrdy = (1<<6),
  45. ATAbusy = (1<<7),
  46. ATAabort = (1<<2),
  47. ATAobs = (1<<1 | 1<<2 | 1<<4),
  48. ATAeIEN = (1<<1),
  49. ATAsrst = (1<<2),
  50. ATAhob = (1<<7),
  51. ATAbad = (ATAbusy|ATAdf|ATAdrq|ATAerr),
  52. SFdone = (1<<0),
  53. SFerror = (1<<1),
  54. SRBident = 0,
  55. SRBread,
  56. SRBwrite,
  57. SRBsmart,
  58. SRBnodata = 0,
  59. SRBdatain,
  60. SRBdataout,
  61. RQread = 1, /* data coming IN from device */
  62. PRDeot = (1<<15),
  63. /* EDMA interrupt error cause register */
  64. ePrtDataErr = (1<<0),
  65. ePrtPRDErr = (1<<1),
  66. eDevErr = (1<<2),
  67. eDevDis = (1<<3),
  68. eDevCon = (1<<4),
  69. eOverrun = (1<<5),
  70. eUnderrun = (1<<6),
  71. eSelfDis = (1<<8),
  72. ePrtCRQBErr = (1<<9),
  73. ePrtCRPBErr = (1<<10),
  74. ePrtIntErr = (1<<11),
  75. eIORdyErr = (1<<12),
  76. /* flags for sata 2 version */
  77. eSelfDis2 = (1<<7),
  78. SerrInt = (1<<5),
  79. /* EDMA Command Register */
  80. eEnEDMA = (1<<0),
  81. eDsEDMA = (1<<1),
  82. eAtaRst = (1<<2),
  83. /* Interrupt mask for errors we care about */
  84. IEM = (eDevDis | eDevCon | eSelfDis),
  85. IEM2 = (eDevDis | eDevCon | eSelfDis2),
  86. /* drive states */
  87. Dnull = 0,
  88. Dnew,
  89. Dready,
  90. Derror,
  91. Dmissing,
  92. Dreset,
  93. Dlast,
  94. /* drive flags */
  95. Dext = (1<<0), /* use ext commands */
  96. Dpio = (1<<1), /* doing pio */
  97. Dwanted = (1<<2), /* someone wants an srb entry */
  98. Dedma = (1<<3), /* device in edma mode */
  99. Dpiowant = (1<<4), /* some wants to use the pio mode */
  100. /* phyerrata magic crap */
  101. Mpreamp = 0x7e0,
  102. Dpreamp = 0x720,
  103. REV60X1B2 = 0x7,
  104. REV60X1C0 = 0x9,
  105. };
  106. static char* diskstates[Dlast] = {
  107. "null",
  108. "new",
  109. "ready",
  110. "error",
  111. "missing",
  112. "reset",
  113. };
  114. extern SDifc sdmv50xxifc;
  115. typedef struct Arb Arb;
  116. typedef struct Bridge Bridge;
  117. typedef struct Chip Chip;
  118. typedef struct Ctlr Ctlr;
  119. typedef struct Drive Drive;
  120. typedef struct Edma Edma;
  121. typedef struct Prd Prd;
  122. typedef struct Rx Rx;
  123. typedef struct Srb Srb;
  124. typedef struct Tx Tx;
  125. /*
  126. * there are 4 drives per chip. thus an 8-port
  127. * card has two chips.
  128. */
  129. struct Chip
  130. {
  131. Arb *arb;
  132. Edma *edma;
  133. };
  134. enum{
  135. DMautoneg,
  136. DMsatai,
  137. DMsataii,
  138. };
  139. struct Drive
  140. {
  141. Lock;
  142. Ctlr *ctlr;
  143. SDunit *unit;
  144. char name[10];
  145. ulong magic;
  146. Bridge *bridge;
  147. Edma *edma;
  148. Chip *chip;
  149. int chipx;
  150. int mediachange;
  151. int state;
  152. int flag;
  153. uvlong sectors;
  154. ulong pm2; /* phymode 2 init state */
  155. ulong intick; /* check for hung western digital drives. */
  156. int wait;
  157. int mode; /* DMautoneg, satai or sataii. */
  158. char serial[20+1];
  159. char firmware[8+1];
  160. char model[40+1];
  161. ushort info[256];
  162. Srb *srb[SrbRing-1];
  163. int nsrb;
  164. Prd *prd;
  165. Tx *tx;
  166. Rx *rx;
  167. Srb *srbhead;
  168. Srb *srbtail;
  169. int driveno; /* ctlr*NCtlrdrv + unit */
  170. };
  171. struct Ctlr
  172. {
  173. Lock;
  174. int irq;
  175. int tbdf;
  176. int rid;
  177. ulong magic;
  178. int enabled;
  179. int type;
  180. SDev *sdev;
  181. Pcidev *pcidev;
  182. uchar *mmio;
  183. ulong *lmmio;
  184. Chip chip[2];
  185. int nchip;
  186. Drive drive[NCtlrdrv];
  187. int ndrive;
  188. };
  189. struct Srb /* request buffer */
  190. {
  191. Lock;
  192. Rendez;
  193. Srb *next;
  194. Drive *drive;
  195. uvlong blockno;
  196. int count;
  197. int req;
  198. int flag;
  199. uchar *data;
  200. uchar cmd;
  201. uchar lba[6];
  202. uchar sectors;
  203. int sta;
  204. int err;
  205. };
  206. /*
  207. * Memory-mapped I/O registers in many forms.
  208. */
  209. struct Bridge /* memory-mapped per-Drive registers */
  210. {
  211. ulong status;
  212. ulong serror;
  213. ulong sctrl;
  214. ulong phyctrl;
  215. ulong phymode3;
  216. ulong phymode4;
  217. uchar fill0[0x14];
  218. ulong phymode1;
  219. ulong phymode2;
  220. char fill1[8];
  221. ulong ctrl;
  222. char fill2[0x34];
  223. ulong phymode;
  224. char fill3[0x88];
  225. }; /* length must be 0x100 */
  226. struct Arb /* memory-mapped per-Chip registers */
  227. {
  228. ulong config; /* satahc configuration register (sata2 only) */
  229. ulong rqop; /* request queue out-pointer */
  230. ulong rqip; /* response queue in pointer */
  231. ulong ict; /* inerrupt caolescing threshold */
  232. ulong itt; /* interrupt timer threshold */
  233. ulong ic; /* interrupt cause */
  234. ulong btc; /* bridges test control */
  235. ulong bts; /* bridges test status */
  236. ulong bpc; /* bridges pin configuration */
  237. char fill1[0xdc];
  238. Bridge bridge[4];
  239. };
  240. struct Edma /* memory-mapped per-Drive DMA-related registers */
  241. {
  242. ulong config; /* configuration register */
  243. ulong timer;
  244. ulong iec; /* interrupt error cause */
  245. ulong iem; /* interrupt error mask */
  246. ulong txbasehi; /* request queue base address high */
  247. ulong txi; /* request queue in pointer */
  248. ulong txo; /* request queue out pointer */
  249. ulong rxbasehi; /* response queue base address high */
  250. ulong rxi; /* response queue in pointer */
  251. ulong rxo; /* response queue out pointer */
  252. ulong ctl; /* command register */
  253. ulong testctl; /* test control */
  254. ulong status;
  255. ulong iordyto; /* IORDY timeout */
  256. char fill[0x18];
  257. ulong sataconfig; /* sata 2 */
  258. char fill[0xac];
  259. ushort pio; /* data register */
  260. char pad0[2];
  261. uchar err; /* features and error */
  262. char pad1[3];
  263. uchar seccnt; /* sector count */
  264. char pad2[3];
  265. uchar lba0;
  266. char pad3[3];
  267. uchar lba1;
  268. char pad4[3];
  269. uchar lba2;
  270. char pad5[3];
  271. uchar lba3;
  272. char pad6[3];
  273. uchar cmdstat; /* cmd/status */
  274. char pad7[3];
  275. uchar altstat; /* alternate status */
  276. uchar fill2[0x1df];
  277. Bridge port;
  278. char fill3[0x1c00]; /* pad to 0x2000 bytes */
  279. };
  280. /*
  281. * Memory structures shared with card.
  282. */
  283. struct Prd /* physical region descriptor */
  284. {
  285. ulong pa; /* byte address of physical memory */
  286. ushort count; /* byte count (bit0 must be 0) */
  287. ushort flag;
  288. ulong zero; /* high long of 64 bit address */
  289. ulong reserved;
  290. };
  291. struct Tx /* command request block */
  292. {
  293. ulong prdpa; /* physical region descriptor table structures */
  294. ulong zero; /* must be zero (high long of prd address) */
  295. ushort flag; /* control flags */
  296. ushort regs[11];
  297. };
  298. struct Rx /* command response block */
  299. {
  300. ushort cid; /* cID of response */
  301. uchar cEdmaSts; /* EDMA status */
  302. uchar cDevSts; /* status from disk */
  303. ulong ts; /* time stamp */
  304. };
  305. static Drive *mvsatadrive[NDrive];
  306. static int nmvsatadrive;
  307. /*
  308. * Little-endian parsing for drive data.
  309. */
  310. static ushort
  311. lhgets(void *p)
  312. {
  313. uchar *a = p;
  314. return ((ushort) a[1] << 8) | a[0];
  315. }
  316. static ulong
  317. lhgetl(void *p)
  318. {
  319. uchar *a = p;
  320. return ((ulong) lhgets(a+2) << 16) | lhgets(a);
  321. }
  322. static uvlong
  323. lhgetv(void *p)
  324. {
  325. uchar *a = p;
  326. return ((uvlong) lhgetl(a+4) << 32) | lhgetl(a);
  327. }
  328. static void
  329. idmove(char *p, ushort *a, int n)
  330. {
  331. char *op;
  332. int i;
  333. op = p;
  334. for(i=0; i<n/2; i++){
  335. *p++ = a[i]>>8;
  336. *p++ = a[i];
  337. }
  338. while(p>op && *--p == ' ')
  339. *p = 0;
  340. }
  341. /*
  342. * Request buffers.
  343. */
  344. struct
  345. {
  346. Lock;
  347. Srb *freechain;
  348. int nalloc;
  349. } srblist;
  350. static Srb*
  351. allocsrb(void)
  352. {
  353. Srb *p;
  354. ilock(&srblist);
  355. if((p = srblist.freechain) == nil){
  356. srblist.nalloc++;
  357. iunlock(&srblist);
  358. p = smalloc(sizeof *p);
  359. }else{
  360. srblist.freechain = p->next;
  361. iunlock(&srblist);
  362. }
  363. return p;
  364. }
  365. static void
  366. freesrb(Srb *p)
  367. {
  368. ilock(&srblist);
  369. p->next = srblist.freechain;
  370. srblist.freechain = p;
  371. iunlock(&srblist);
  372. }
  373. /*
  374. * Wait for a byte to be a particular value.
  375. */
  376. static int
  377. satawait(uchar *p, uchar mask, uchar v, int ms)
  378. {
  379. int i;
  380. for(i=0; i<ms && (*p & mask) != v; i++)
  381. microdelay(1000);
  382. return (*p & mask) == v;
  383. }
  384. /*
  385. * Drive initialization
  386. */
  387. /* unmask in the pci registers err done */
  388. static void
  389. unmask(ulong *mmio, int port, int coal)
  390. {
  391. port &= 7;
  392. if(coal)
  393. coal = 1;
  394. if (port < 4)
  395. mmio[0x1d64/4] |= (3 << (((port&3)*2)) | (coal<<8));
  396. else
  397. mmio[0x1d64/4] |= (3 << (((port&3)*2+9)) | (coal<<17));
  398. }
  399. static void
  400. mask(ulong *mmio, int port, int coal)
  401. {
  402. port &= 7;
  403. if(coal)
  404. coal = 1;
  405. if (port < 4)
  406. mmio[0x1d64/4] &= ~(3 << (((port&3)*2)) | (coal<<8));
  407. else
  408. mmio[0x1d64/4] &= ~(3 << (((port&3)*2+9)) | (coal<<17));
  409. }
  410. /* I give up, marvell. You win. */
  411. static void
  412. phyerrata(Drive *d)
  413. {
  414. ulong n, m;
  415. enum { BadAutoCal = 0xf << 26, };
  416. if (d->ctlr->type == 1)
  417. return;
  418. microdelay(200);
  419. n = d->bridge->phymode2;
  420. while ((n & BadAutoCal) == BadAutoCal) {
  421. dprint("%s: badautocal\n", d->unit->name);
  422. n &= ~(1<<16);
  423. n |= (1<<31);
  424. d->bridge->phymode2 = n;
  425. microdelay(200);
  426. d->bridge->phymode2 &= ~((1<<16) | (1<<31));
  427. microdelay(200);
  428. n = d->bridge->phymode2;
  429. }
  430. n &= ~(1<<31);
  431. d->bridge->phymode2 = n;
  432. microdelay(200);
  433. /* abra cadabra! (random magic) */
  434. m = d->bridge->phymode3;
  435. m &= ~0x7f800000;
  436. m |= 0x2a800000;
  437. d->bridge->phymode3 = m;
  438. /* fix phy mode 4 */
  439. m = d->bridge->phymode3;
  440. n = d->bridge->phymode4;
  441. n &= ~(1<<1);
  442. n |= 1;
  443. switch(d->ctlr->rid){
  444. case REV60X1B2:
  445. default:
  446. d->bridge->phymode4 = n;
  447. d->bridge->phymode3 = m;
  448. break;
  449. case REV60X1C0:
  450. d->bridge->phymode4 = n;
  451. break;
  452. }
  453. /* revert values of pre-emphasis and signal amps to the saved ones */
  454. n = d->bridge->phymode2;
  455. n &= ~Mpreamp;
  456. n |= d->pm2;
  457. n &= ~(1<<16);
  458. d->bridge->phymode2 = n;
  459. }
  460. static void
  461. edmacleanout(Drive *d)
  462. {
  463. int i;
  464. Srb *srb;
  465. for(i=0; i<nelem(d->srb); i++){
  466. if(srb = d->srb[i]){
  467. d->srb[i] = nil;
  468. d->nsrb--;
  469. srb->flag |= SFerror|SFdone;
  470. wakeup(srb);
  471. }
  472. }
  473. while(srb = d->srbhead){
  474. d->srbhead = srb->next;
  475. srb->flag |= SFerror|SFdone;
  476. wakeup(srb);
  477. }
  478. }
  479. static void
  480. resetdisk(Drive *d)
  481. {
  482. ulong n;
  483. d->sectors = 0;
  484. d->unit->sectors = 0;
  485. if (d->ctlr->type == 2) {
  486. /*
  487. * without bit 8 we can boot without disks, but
  488. * inserted disks will never appear. :-X
  489. */
  490. n = d->edma->sataconfig;
  491. n &= 0xff;
  492. n |= 0x9b1100;
  493. d->edma->sataconfig = n;
  494. n = d->edma->sataconfig; /* flush */
  495. USED(n);
  496. }
  497. d->edma->ctl = eDsEDMA;
  498. microdelay(1);
  499. d->edma->ctl = eAtaRst;
  500. microdelay(25);
  501. d->edma->ctl = 0;
  502. if (satawait((uchar *)&d->edma->ctl, eEnEDMA, 0, 3*1000) == 0)
  503. print("%s: eEnEDMA never cleared on reset\n", d->unit->name);
  504. edmacleanout(d);
  505. phyerrata(d);
  506. d->bridge->sctrl = 0x301 | (d->mode << 4);
  507. d->state = Dmissing;
  508. }
  509. static void
  510. edmainit(Drive *d)
  511. {
  512. int i;
  513. if(d->tx != nil)
  514. return;
  515. d->tx = xspanalloc(32*sizeof(Tx), 1024, 0);
  516. d->rx = xspanalloc(32*sizeof(Rx), 256, 0);
  517. d->prd = xspanalloc(32*sizeof(Prd), 32, 0);
  518. for(i = 0; i < 32; i++)
  519. d->tx[i].prdpa = PADDR(&d->prd[i]);
  520. coherence();
  521. }
  522. static int
  523. configdrive(Ctlr *ctlr, Drive *d, SDunit *unit)
  524. {
  525. dprint("%s: configdrive\n", unit->name);
  526. if(d->driveno < 0)
  527. panic("mv50xx: configdrive: unset driveno\n");
  528. d->unit = unit;
  529. edmainit(d);
  530. d->mode = DMsatai;
  531. if(d->ctlr->type == 1){
  532. d->edma->iem = IEM;
  533. d->bridge = &d->chip->arb->bridge[d->chipx];
  534. }else{
  535. d->edma->iem = IEM2;
  536. d->bridge = &d->chip->edma[d->chipx].port;
  537. d->edma->iem = ~(1<<6);
  538. d->pm2 = Dpreamp;
  539. if(d->ctlr->lmmio[0x180d8/4] & 1)
  540. d->pm2 = d->bridge->phymode2 & Mpreamp;
  541. }
  542. resetdisk(d);
  543. unmask(ctlr->lmmio, d->driveno, 0);
  544. delay(100);
  545. if(d->bridge->status){
  546. dprint("%s: configdrive: found drive %lx\n", unit->name, d->bridge->status);
  547. return 0;
  548. }
  549. return -1;
  550. }
  551. static int
  552. enabledrive(Drive *d)
  553. {
  554. Edma *edma;
  555. dprint("%s: enabledrive..", d->unit->name);
  556. if((d->bridge->status & 0xf) != 3){
  557. dprint("%s: not present\n", d->unit->name);
  558. d->state = Dmissing;
  559. return -1;
  560. }
  561. edma = d->edma;
  562. if(satawait(&edma->cmdstat, ATAbusy, 0, 5*1000) == 0){
  563. dprint("%s: busy timeout\n", d->unit->name);
  564. d->state = Dmissing;
  565. return -1;
  566. }
  567. edma->iec = 0;
  568. d->chip->arb->ic &= ~(0x101 << d->chipx);
  569. edma->config = 0x51f;
  570. if (d->ctlr->type == 2)
  571. edma->config |= 7<<11;
  572. edma->txi = PADDR(d->tx);
  573. edma->txo = (ulong)d->tx & 0x3e0;
  574. edma->rxi = (ulong)d->rx & 0xf8;
  575. edma->rxo = PADDR(d->rx);
  576. edma->ctl |= 1; /* enable dma */
  577. if(d->bridge->status = 0x113){
  578. dprint("%s: new\n", d->unit->name);
  579. d->state = Dnew;
  580. }else
  581. print("%s: status not forced (should be okay)\n", d->unit->name);
  582. return 0;
  583. }
  584. static void
  585. disabledrive(Drive *d)
  586. {
  587. int i;
  588. ulong *r;
  589. dprint("%s: disabledrive\n", d->unit->name);
  590. if(d->tx == nil) /* never enabled */
  591. return;
  592. d->edma->ctl = 0;
  593. d->edma->iem = 0;
  594. r = (ulong*)(d->ctlr->mmio + 0x1d64);
  595. i = d->chipx;
  596. if(d->chipx < 4)
  597. *r &= ~(3 << (i*2));
  598. else
  599. *r |= ~(3 << (i*2+9));
  600. }
  601. static int
  602. setudmamode(Drive *d, uchar mode)
  603. {
  604. Edma *edma;
  605. dprint("%s: setudmamode %d\n", d->unit->name, mode);
  606. edma = d->edma;
  607. if (edma == nil) {
  608. iprint("setudamode(m%d): zero d->edma\m", d->driveno);
  609. return 0;
  610. }
  611. if(satawait(&edma->cmdstat, ~ATAobs, ATAdrdy, 9*1000) == 0){
  612. iprint("%s: cmdstat 0x%.2ux ready timeout\n", d->unit->name, edma->cmdstat);
  613. return 0;
  614. }
  615. edma->altstat = ATAeIEN;
  616. edma->err = 3;
  617. edma->seccnt = 0x40 | mode;
  618. edma->cmdstat = 0xef;
  619. microdelay(1);
  620. if(satawait(&edma->cmdstat, ATAbusy, 0, 5*1000) == 0){
  621. iprint("%s: cmdstat 0x%.2ux busy timeout\n", d->unit->name, edma->cmdstat);
  622. return 0;
  623. }
  624. return 1;
  625. }
  626. static int
  627. identifydrive(Drive *d)
  628. {
  629. int i;
  630. ushort *id;
  631. Edma *edma;
  632. SDunit *unit;
  633. dprint("%s: identifydrive\n", d->unit->name);
  634. if(setudmamode(d, 5) == 0) /* do all SATA support 5? */
  635. goto Error;
  636. id = d->info;
  637. memset(d->info, 0, sizeof d->info);
  638. edma = d->edma;
  639. if(satawait(&edma->cmdstat, ~ATAobs, ATAdrdy, 5*1000) == 0)
  640. goto Error;
  641. edma->altstat = ATAeIEN; /* no interrupts */
  642. edma->cmdstat = 0xec;
  643. microdelay(1);
  644. if(satawait(&edma->cmdstat, ATAbusy, 0, 5*1000) == 0)
  645. goto Error;
  646. for(i = 0; i < 256; i++)
  647. id[i] = edma->pio;
  648. if(edma->cmdstat & ATAbad)
  649. goto Error;
  650. i = lhgets(id+83) | lhgets(id+86);
  651. if(i & (1<<10)){
  652. d->flag |= Dext;
  653. d->sectors = lhgetv(id+100);
  654. }else{
  655. d->flag &= ~Dext;
  656. d->sectors = lhgetl(id+60);
  657. }
  658. idmove(d->serial, id+10, 20);
  659. idmove(d->firmware, id+23, 8);
  660. idmove(d->model, id+27, 40);
  661. unit = d->unit;
  662. memset(unit->inquiry, 0, sizeof unit->inquiry);
  663. unit->inquiry[2] = 2;
  664. unit->inquiry[3] = 2;
  665. unit->inquiry[4] = sizeof(unit->inquiry)-4;
  666. idmove((char*)unit->inquiry+8, id+27, 40);
  667. if(enabledrive(d) == 0) {
  668. d->state = Dready;
  669. d->mediachange = 1;
  670. idprint("%s: LLBA %lld sectors\n", d->unit->name, d->sectors);
  671. } else
  672. d->state = Derror;
  673. if(d->state == Dready)
  674. return 0;
  675. return -1;
  676. Error:
  677. dprint("error...");
  678. d->state = Derror;
  679. return -1;
  680. }
  681. /* p. 163:
  682. M recovered error
  683. P protocol error
  684. N PhyRdy change
  685. W CommWake
  686. B 8-to-10 encoding error
  687. D disparity error
  688. C crc error
  689. H handshake error
  690. S link sequence error
  691. T transport state transition error
  692. F unrecognized fis type
  693. X device changed
  694. */
  695. static char stab[] = {
  696. [1] 'M',
  697. [10] 'P',
  698. [16] 'N',
  699. [18] 'W', 'B', 'D', 'C', 'H', 'S', 'T', 'F', 'X'
  700. };
  701. static ulong sbad = (7<<20)|(3<<23);
  702. static void
  703. serrdecode(ulong r, char *s, char *e)
  704. {
  705. int i;
  706. e -= 3;
  707. for(i = 0; i < nelem(stab) && s < e; i++){
  708. if((r&(1<<i)) && stab[i]){
  709. *s++ = stab[i];
  710. if(sbad&(1<<i))
  711. *s++ = '*';
  712. }
  713. }
  714. *s = 0;
  715. }
  716. char *iectab[] = {
  717. "ePrtDataErr",
  718. "ePrtPRDErr",
  719. "eDevErr",
  720. "eDevDis",
  721. "eDevCon",
  722. "SerrInt",
  723. "eUnderrun",
  724. "eSelfDis2",
  725. "eSelfDis",
  726. "ePrtCRQBErr",
  727. "ePrtCRPBErr",
  728. "ePrtIntErr",
  729. "eIORdyErr",
  730. };
  731. static char*
  732. iecdecode(ulong cause)
  733. {
  734. int i;
  735. for(i = 0; i < nelem(iectab); i++)
  736. if(cause&(1<<i))
  737. return iectab[i];
  738. return "";
  739. }
  740. enum{
  741. Cerror = ePrtDataErr|ePrtPRDErr|eDevErr|eSelfDis2|ePrtCRPBErr|ePrtIntErr,
  742. };
  743. static void
  744. updatedrive(Drive *d)
  745. {
  746. int x;
  747. ulong cause;
  748. Edma *edma;
  749. char buf[32+4+1];
  750. edma = d->edma;
  751. if((edma->ctl&eEnEDMA) == 0){
  752. /* FEr SATA#4 40xx */
  753. x = d->edma->cmdstat;
  754. USED(x);
  755. }
  756. cause = edma->iec;
  757. if(cause == 0)
  758. return;
  759. dprint("%s: cause %08ulx [%s]\n", d->unit->name, cause, iecdecode(cause));
  760. if(cause & eDevCon)
  761. d->state = Dnew;
  762. if(cause&eDevDis && d->state == Dready)
  763. iprint("%s: pulled: st=%08ulx\n", d->unit->name, cause);
  764. switch(d->ctlr->type){
  765. case 1:
  766. if(cause&eSelfDis)
  767. d->state = Derror;
  768. break;
  769. case 2:
  770. if(cause&Cerror)
  771. d->state = Derror;
  772. if(cause&SerrInt){
  773. serrdecode(d->bridge->serror, buf, buf+sizeof buf);
  774. dprint("%s: serror %08ulx [%s]\n", d->unit->name, (ulong)d->bridge->serror, buf);
  775. d->bridge->serror = d->bridge->serror;
  776. }
  777. }
  778. edma->iec = ~cause;
  779. }
  780. /*
  781. * Requests
  782. */
  783. static Srb*
  784. srbrw(int req, Drive *d, uchar *data, uint sectors, uvlong lba)
  785. {
  786. int i;
  787. Srb *srb;
  788. static uchar cmd[2][2] = { 0xC8, 0x25, 0xCA, 0x35 };
  789. srb = allocsrb();
  790. srb->req = req;
  791. srb->drive = d;
  792. srb->blockno = lba;
  793. srb->sectors = sectors;
  794. srb->count = sectors*512;
  795. srb->flag = 0;
  796. srb->data = data;
  797. for(i=0; i<6; i++)
  798. srb->lba[i] = lba >> (8*i);
  799. srb->cmd = cmd[srb->req!=SRBread][(d->flag&Dext)!=0];
  800. return srb;
  801. }
  802. static uintptr
  803. advance(uintptr pa, int shift)
  804. {
  805. int n, mask;
  806. mask = 0x1F<<shift;
  807. n = (pa & mask) + (1<<shift);
  808. return (pa & ~mask) | (n & mask);
  809. }
  810. #define CMD(r, v) (((r)<<8) | ((v)&0xFF))
  811. static void
  812. mvsatarequest(ushort *cmd, Srb *srb, int ext)
  813. {
  814. *cmd++ = CMD(ARseccnt, 0);
  815. *cmd++ = CMD(ARseccnt, srb->sectors);
  816. *cmd++ = CMD(ARfea, 0);
  817. if(ext){
  818. *cmd++ = CMD(ARlba0, srb->lba[3]);
  819. *cmd++ = CMD(ARlba0, srb->lba[0]);
  820. *cmd++ = CMD(ARlba1, srb->lba[4]);
  821. *cmd++ = CMD(ARlba1, srb->lba[1]);
  822. *cmd++ = CMD(ARlba2, srb->lba[5]);
  823. *cmd++ = CMD(ARlba2, srb->lba[2]);
  824. *cmd++ = CMD(ARdev, 0xe0);
  825. }else{
  826. *cmd++ = CMD(ARlba0, srb->lba[0]);
  827. *cmd++ = CMD(ARlba1, srb->lba[1]);
  828. *cmd++ = CMD(ARlba2, srb->lba[2]);
  829. *cmd++ = CMD(ARdev, srb->lba[3] | 0xe0);
  830. }
  831. *cmd = CMD(ARcmd, srb->cmd) | (1<<15);
  832. }
  833. static void
  834. startsrb(Drive *d, Srb *srb)
  835. {
  836. int i;
  837. Edma *edma;
  838. Prd *prd;
  839. Tx *tx;
  840. if(d->nsrb >= nelem(d->srb)){
  841. srb->next = nil;
  842. if(d->srbhead)
  843. d->srbtail->next = srb;
  844. else
  845. d->srbhead = srb;
  846. d->srbtail = srb;
  847. return;
  848. }
  849. d->nsrb++;
  850. for(i=0; i<nelem(d->srb); i++)
  851. if(d->srb[i] == nil)
  852. break;
  853. if(i == nelem(d->srb))
  854. panic("sdmv50xx: no free srbs");
  855. d->intick = MACHP(0)->ticks;
  856. d->srb[i] = srb;
  857. edma = d->edma;
  858. tx = (Tx*)KADDR(edma->txi);
  859. tx->flag = (i<<1) | (srb->req == SRBread);
  860. prd = KADDR(tx->prdpa);
  861. prd->pa = PADDR(srb->data);
  862. prd->count = srb->count;
  863. prd->flag = PRDeot;
  864. mvsatarequest(tx->regs, srb, d->flag&Dext);
  865. coherence();
  866. edma->txi = advance(edma->txi, 5);
  867. d->intick = MACHP(0)->ticks;
  868. }
  869. enum{
  870. Rpidx = 0x1f<<3,
  871. };
  872. static void
  873. completesrb(Drive *d)
  874. {
  875. Edma *edma;
  876. Rx *rx;
  877. Srb *srb;
  878. edma = d->edma;
  879. if((edma->ctl & eEnEDMA) == 0)
  880. return;
  881. while((edma->rxo&Rpidx) != (edma->rxi&Rpidx)){
  882. rx = (Rx*)KADDR(edma->rxo);
  883. if(srb = d->srb[rx->cid]){
  884. d->srb[rx->cid] = nil;
  885. d->nsrb--;
  886. if(rx->cDevSts & ATAbad)
  887. srb->flag |= SFerror;
  888. if (rx->cEdmaSts)
  889. iprint("cEdmaSts: %02ux\n", rx->cEdmaSts);
  890. srb->sta = rx->cDevSts;
  891. srb->flag |= SFdone;
  892. wakeup(srb);
  893. }else
  894. iprint("srb missing\n");
  895. edma->rxo = advance(edma->rxo, 3);
  896. if(srb = d->srbhead){
  897. d->srbhead = srb->next;
  898. startsrb(d, srb);
  899. }
  900. }
  901. }
  902. static int
  903. srbdone(void *v)
  904. {
  905. Srb *srb;
  906. srb = v;
  907. return srb->flag & SFdone;
  908. }
  909. /*
  910. * Interrupts
  911. */
  912. static void
  913. mv50interrupt(Ureg*, void *a)
  914. {
  915. int i;
  916. ulong cause;
  917. Ctlr *ctlr;
  918. Drive *drive;
  919. ctlr = a;
  920. ilock(ctlr);
  921. cause = ctlr->lmmio[0x1d60/4];
  922. // dprint("sd%c: mv50interrupt: 0x%lux\n", ctlr->sdev->idno, cause);
  923. for(i=0; i<ctlr->ndrive; i++)
  924. if(cause & (3<<(i*2+i/4))){
  925. drive = &ctlr->drive[i];
  926. if(drive->edma == 0)
  927. continue; /* not ready yet. */
  928. ilock(drive);
  929. updatedrive(drive);
  930. while(ctlr->chip[i/4].arb->ic & (0x0101 << (i%4))){
  931. ctlr->chip[i/4].arb->ic = ~(0x101 << (i%4));
  932. completesrb(drive);
  933. }
  934. iunlock(drive);
  935. }
  936. iunlock(ctlr);
  937. }
  938. enum{
  939. Nms = 256,
  940. Midwait = 16*1024/Nms-1,
  941. Mphywait = 512/Nms-1,
  942. };
  943. static void
  944. westerndigitalhung(Drive *d)
  945. {
  946. Edma *e;
  947. e = d->edma;
  948. if(d->srb
  949. && TK2MS(MACHP(0)->ticks-d->intick) > 5*1000
  950. && (e->rxo&Rpidx) == (e->rxi&Rpidx)){
  951. dprint("westerndigital drive hung; resetting\n");
  952. d->state = Dreset;
  953. }
  954. }
  955. static void
  956. checkdrive(Drive *d, int i)
  957. {
  958. static ulong s, olds[NCtlr*NCtlrdrv];
  959. char *name;
  960. ilock(d);
  961. name = d->unit->name;
  962. s = d->bridge->status;
  963. if(s != olds[i]){
  964. dprint("%s: status: %08lx -> %08lx: %s\n", name, olds[i], s, diskstates[d->state]);
  965. olds[i] = s;
  966. }
  967. /* westerndigitalhung(d); */
  968. switch(d->state){
  969. case Dnew:
  970. case Dmissing:
  971. switch(s){
  972. case 0x000:
  973. break;
  974. default:
  975. dprint("%s: unknown state %8lx\n", name, s);
  976. case 0x100:
  977. if(++d->wait&Mphywait)
  978. break;
  979. reset: d->mode ^= 1;
  980. dprint("%s: reset; new mode %d\n", name, d->mode);
  981. resetdisk(d);
  982. break;
  983. case 0x123:
  984. case 0x113:
  985. s = d->edma->cmdstat;
  986. if(s == 0x7f || (s&~ATAobs) != ATAdrdy){
  987. if((++d->wait&Midwait) == 0)
  988. goto reset;
  989. }else if(identifydrive(d) == -1)
  990. goto reset;
  991. }
  992. break;
  993. case Dready:
  994. if(s != 0)
  995. break;
  996. iprint("%s: pulled: st=%08ulx\n", name, s); /* never happens */
  997. case Dreset:
  998. case Derror:
  999. dprint("%s reset: mode %d\n", name, d->mode);
  1000. resetdisk(d);
  1001. break;
  1002. }
  1003. iunlock(d);
  1004. }
  1005. static void
  1006. satakproc(void*)
  1007. {
  1008. int i;
  1009. while(waserror())
  1010. ;
  1011. for(;;){
  1012. tsleep(&up->sleep, return0, 0, Nms);
  1013. for(i = 0; i < nmvsatadrive; i++)
  1014. checkdrive(mvsatadrive[i], i);
  1015. }
  1016. }
  1017. /*
  1018. * Device discovery
  1019. */
  1020. static SDev*
  1021. mv50pnp(void)
  1022. {
  1023. int i, nunit;
  1024. uchar *base;
  1025. ulong io, n, *mem;
  1026. Ctlr *ctlr;
  1027. Pcidev *p;
  1028. SDev *head, *tail, *sdev;
  1029. Drive *d;
  1030. static int ctlrno, done;
  1031. dprint("mv50pnp\n");
  1032. if(done++)
  1033. return nil;
  1034. p = nil;
  1035. head = nil;
  1036. tail = nil;
  1037. while((p = pcimatch(p, 0x11ab, 0)) != nil){
  1038. switch(p->did){
  1039. case 0x5040:
  1040. case 0x5041:
  1041. case 0x5080:
  1042. case 0x5081:
  1043. case 0x6041:
  1044. case 0x6081:
  1045. break;
  1046. default:
  1047. print("mv50pnp: unknown did %ux ignored\n", (ushort)p->did);
  1048. continue;
  1049. }
  1050. if (ctlrno >= NCtlr) {
  1051. print("mv50pnp: too many controllers\n");
  1052. break;
  1053. }
  1054. nunit = (p->did&0xf0) >> 4;
  1055. print("Marvell 88SX%ux: %d SATA-%s ports with%s flash\n",
  1056. (ushort)p->did, nunit,
  1057. ((p->did&0xf000)==0x6000? "II": "I"),
  1058. (p->did&1? "": "out"));
  1059. if((sdev = malloc(sizeof(SDev))) == nil)
  1060. continue;
  1061. if((ctlr = malloc(sizeof(Ctlr))) == nil){
  1062. free(sdev);
  1063. continue;
  1064. }
  1065. memset(sdev, 0, sizeof *sdev);
  1066. memset(ctlr, 0, sizeof *ctlr);
  1067. io = p->mem[0].bar & ~0x0F;
  1068. mem = (ulong*)vmap(io, p->mem[0].size);
  1069. if(mem == 0){
  1070. print("sdmv50xx: address 0x%luX in use\n", io);
  1071. free(sdev);
  1072. free(ctlr);
  1073. continue;
  1074. }
  1075. ctlr->rid = p->rid;
  1076. /* avert thine eyes! (what does this do?) */
  1077. mem[0x104f0/4] = 0;
  1078. ctlr->type = (p->did >> 12) & 3;
  1079. if(ctlr->type == 1){
  1080. n = mem[0xc00/4];
  1081. n &= ~(3<<4);
  1082. mem[0xc00/4] = n;
  1083. }
  1084. sdev->ifc = &sdmv50xxifc;
  1085. sdev->ctlr = ctlr;
  1086. sdev->nunit = nunit;
  1087. sdev->idno = 'E';
  1088. ctlr->sdev = sdev;
  1089. ctlr->irq = p->intl;
  1090. ctlr->tbdf = p->tbdf;
  1091. ctlr->pcidev = p;
  1092. ctlr->lmmio = mem;
  1093. ctlr->mmio = (uchar*)mem;
  1094. ctlr->nchip = (nunit+3)/4;
  1095. ctlr->ndrive = nunit;
  1096. ctlr->enabled = 0;
  1097. for(i = 0; i < ctlr->nchip; i++){
  1098. base = ctlr->mmio+0x20000+0x10000*i;
  1099. ctlr->chip[i].arb = (Arb*)base;
  1100. ctlr->chip[i].edma = (Edma*)(base + 0x2000);
  1101. }
  1102. for (i = 0; i < nunit; i++) {
  1103. d = &ctlr->drive[i];
  1104. d->sectors = 0;
  1105. d->ctlr = ctlr;
  1106. d->driveno = ctlrno*NCtlrdrv + i;
  1107. d->chipx = i%4;
  1108. d->chip = &ctlr->chip[i/4];
  1109. d->edma = &d->chip->edma[d->chipx];
  1110. mvsatadrive[d->driveno] = d;
  1111. }
  1112. nmvsatadrive += nunit;
  1113. ctlrno++;
  1114. if(head)
  1115. tail->next = sdev;
  1116. else
  1117. head = sdev;
  1118. tail = sdev;
  1119. }
  1120. return head;
  1121. }
  1122. /*
  1123. * Enable the controller. Each disk has its own interrupt mask,
  1124. * and those get enabled as the disks are brought online.
  1125. */
  1126. static int
  1127. mv50enable(SDev *sdev)
  1128. {
  1129. char name[32];
  1130. Ctlr *ctlr;
  1131. dprint("sd%c: enable\n", sdev->idno);
  1132. ctlr = sdev->ctlr;
  1133. if (ctlr->enabled)
  1134. return 1;
  1135. snprint(name, sizeof name, "%s (%s)", sdev->name, sdev->ifc->name);
  1136. intrenable(ctlr->irq, mv50interrupt, ctlr, ctlr->tbdf, name);
  1137. ctlr->enabled = 1;
  1138. return 1;
  1139. }
  1140. /*
  1141. * Disable the controller.
  1142. */
  1143. static int
  1144. mv50disable(SDev *sdev)
  1145. {
  1146. char name[32];
  1147. int i;
  1148. Ctlr *ctlr;
  1149. Drive *drive;
  1150. dprint("sd%c: disable\n", sdev->idno);
  1151. ctlr = sdev->ctlr;
  1152. ilock(ctlr);
  1153. for(i=0; i<ctlr->sdev->nunit; i++){
  1154. drive = &ctlr->drive[i];
  1155. ilock(drive);
  1156. disabledrive(drive);
  1157. iunlock(drive);
  1158. }
  1159. iunlock(ctlr);
  1160. snprint(name, sizeof name, "%s (%s)", sdev->name, sdev->ifc->name);
  1161. intrdisable(ctlr->irq, mv50interrupt, ctlr, ctlr->tbdf, name);
  1162. return 0;
  1163. }
  1164. /*
  1165. * Clean up all disk structures. Already disabled.
  1166. * Could keep count of number of allocated controllers
  1167. * and free the srblist when it drops to zero.
  1168. */
  1169. static void
  1170. mv50clear(SDev *sdev)
  1171. {
  1172. int i;
  1173. Ctlr *ctlr;
  1174. Drive *d;
  1175. dprint("sd%c: clear\n", sdev->idno);
  1176. ctlr = sdev->ctlr;
  1177. for(i=0; i<ctlr->ndrive; i++){
  1178. d = &ctlr->drive[i];
  1179. free(d->tx);
  1180. free(d->rx);
  1181. free(d->prd);
  1182. }
  1183. free(ctlr);
  1184. }
  1185. /*
  1186. * Check that there is a disk or at least a hot swap bay in the drive.
  1187. */
  1188. static int
  1189. mv50verify(SDunit *unit)
  1190. {
  1191. Ctlr *ctlr;
  1192. Drive *drive;
  1193. int i;
  1194. dprint("%s: verify\n", unit->name);
  1195. ctlr = unit->dev->ctlr;
  1196. drive = &ctlr->drive[unit->subno];
  1197. ilock(ctlr);
  1198. ilock(drive);
  1199. i = configdrive(ctlr, drive, unit);
  1200. iunlock(drive);
  1201. iunlock(ctlr);
  1202. /*
  1203. * If ctlr->type == 1, then the drives spin up whenever
  1204. * the controller feels like it; if ctlr->type != 1, then
  1205. * they spin up as a result of configdrive.
  1206. *
  1207. * If there is a drive in the slot, give it 1.5s to spin up
  1208. * before returning. There is a noticeable drag on the
  1209. * power supply when spinning up fifteen drives
  1210. * all at once (like in the Coraid enclosures).
  1211. */
  1212. if(ctlr->type != 1 && i == 0){
  1213. if(!waserror()){
  1214. tsleep(&up->sleep, return0, 0, 1500);
  1215. poperror();
  1216. }
  1217. }
  1218. return 1;
  1219. }
  1220. /*
  1221. * Check whether the disk is online.
  1222. */
  1223. static int
  1224. mv50online(SDunit *unit)
  1225. {
  1226. Ctlr *ctlr;
  1227. Drive *d;
  1228. int r, s0;
  1229. static int once;
  1230. if(once++ == 0)
  1231. kproc("mvsata", satakproc, 0);
  1232. ctlr = unit->dev->ctlr;
  1233. d = &ctlr->drive[unit->subno];
  1234. r = 0;
  1235. ilock(d);
  1236. s0 = d->state;
  1237. USED(s0);
  1238. if(d->state == Dnew)
  1239. identifydrive(d);
  1240. if(d->mediachange){
  1241. idprint("%s: online: %s -> %s\n", unit->name, diskstates[s0], diskstates[d->state]);
  1242. r = 2;
  1243. unit->sectors = d->sectors;
  1244. unit->secsize = 512;
  1245. d->mediachange = 0;
  1246. } else if(d->state == Dready)
  1247. r = 1;
  1248. iunlock(d);
  1249. return r;
  1250. }
  1251. /*
  1252. * Register dumps
  1253. */
  1254. typedef struct Regs Regs;
  1255. struct Regs
  1256. {
  1257. ulong offset;
  1258. char *name;
  1259. };
  1260. static Regs regsctlr[] =
  1261. {
  1262. 0x0C28, "pci serr# mask",
  1263. 0x1D40, "pci err addr low",
  1264. 0x1D44, "pci err addr hi",
  1265. 0x1D48, "pci err attr",
  1266. 0x1D50, "pci err cmd",
  1267. 0x1D58, "pci intr cause",
  1268. 0x1D5C, "pci mask cause",
  1269. 0x1D60, "device micr",
  1270. 0x1D64, "device mimr",
  1271. };
  1272. static Regs regsarb[] =
  1273. {
  1274. 0x0004, "arb rqop",
  1275. 0x0008, "arb rqip",
  1276. 0x000C, "arb ict",
  1277. 0x0010, "arb itt",
  1278. 0x0014, "arb ic",
  1279. 0x0018, "arb btc",
  1280. 0x001C, "arb bts",
  1281. 0x0020, "arb bpc",
  1282. };
  1283. static Regs regsbridge[] =
  1284. {
  1285. 0x0000, "bridge status",
  1286. 0x0004, "bridge serror",
  1287. 0x0008, "bridge sctrl",
  1288. 0x000C, "bridge phyctrl",
  1289. 0x003C, "bridge ctrl",
  1290. 0x0074, "bridge phymode",
  1291. };
  1292. static Regs regsedma[] =
  1293. {
  1294. 0x0000, "edma config",
  1295. 0x0004, "edma timer",
  1296. 0x0008, "edma iec",
  1297. 0x000C, "edma iem",
  1298. 0x0010, "edma txbasehi",
  1299. 0x0014, "edma txi",
  1300. 0x0018, "edma txo",
  1301. 0x001C, "edma rxbasehi",
  1302. 0x0020, "edma rxi",
  1303. 0x0024, "edma rxo",
  1304. 0x0028, "edma c",
  1305. 0x002C, "edma tc",
  1306. 0x0030, "edma status",
  1307. 0x0034, "edma iordyto",
  1308. /* 0x0100, "edma pio",
  1309. 0x0104, "edma err",
  1310. 0x0108, "edma sectors",
  1311. 0x010C, "edma lba0",
  1312. 0x0110, "edma lba1",
  1313. 0x0114, "edma lba2",
  1314. 0x0118, "edma lba3",
  1315. 0x011C, "edma cmdstat",
  1316. 0x0120, "edma altstat",
  1317. */
  1318. };
  1319. static char*
  1320. rdregs(char *p, char *e, void *base, Regs *r, int n, char *prefix)
  1321. {
  1322. int i;
  1323. for(i=0; i<n; i++)
  1324. p = seprint(p, e, "%s%s%-19s %.8ux\n",
  1325. prefix ? prefix : "", prefix ? ": " : "",
  1326. r[i].name, *(ulong *)((uchar*)base+r[i].offset));
  1327. return p;
  1328. }
  1329. static char*
  1330. rdinfo(char *p, char *e, ushort *info)
  1331. {
  1332. int i;
  1333. p = seprint(p, e, "info");
  1334. for(i=0; i<256; i++){
  1335. p = seprint(p, e, "%s%.4ux%s",
  1336. i%8==0 ? "\t" : "",
  1337. info[i],
  1338. i%8==7 ? "\n" : "");
  1339. }
  1340. return p;
  1341. }
  1342. static int
  1343. mv50rctl(SDunit *unit, char *p, int l)
  1344. {
  1345. char *e, *op;
  1346. Ctlr *ctlr;
  1347. Drive *drive;
  1348. if((ctlr = unit->dev->ctlr) == nil)
  1349. return 0;
  1350. drive = &ctlr->drive[unit->subno];
  1351. e = p+l;
  1352. op = p;
  1353. if(drive->state == Dready){
  1354. p = seprint(p, e, "model %s\n", drive->model);
  1355. p = seprint(p, e, "serial %s\n", drive->serial);
  1356. p = seprint(p, e, "firmware %s\n", drive->firmware);
  1357. }else
  1358. p = seprint(p, e, "no disk present\n");
  1359. p = seprint(p, e, "geometry %llud 512\n", drive->sectors);
  1360. p = rdinfo(p, e, drive->info);
  1361. p = rdregs(p, e, drive->chip->arb, regsarb, nelem(regsarb), nil);
  1362. p = rdregs(p, e, drive->bridge, regsbridge, nelem(regsbridge), nil);
  1363. p = rdregs(p, e, drive->edma, regsedma, nelem(regsedma), nil);
  1364. return p-op;
  1365. }
  1366. static int
  1367. mv50wctl(SDunit *unit, Cmdbuf *cb)
  1368. {
  1369. Ctlr *ctlr;
  1370. Drive *drive;
  1371. USED(unit);
  1372. if(strcmp(cb->f[0], "reset") == 0){
  1373. ctlr = unit->dev->ctlr;
  1374. drive = &ctlr->drive[unit->subno];
  1375. ilock(drive);
  1376. drive->state = Dreset;
  1377. iunlock(drive);
  1378. return 0;
  1379. }
  1380. cmderror(cb, Ebadctl);
  1381. return -1;
  1382. }
  1383. static char*
  1384. mv50rtopctl(SDev *sdev, char *p, char *e)
  1385. {
  1386. char name[10];
  1387. Ctlr *ctlr;
  1388. ctlr = sdev->ctlr;
  1389. if(ctlr == nil)
  1390. return p;
  1391. snprint(name, sizeof name, "sd%c", sdev->idno);
  1392. p = rdregs(p, e, ctlr->mmio, regsctlr, nelem(regsctlr), name);
  1393. /* info for first disk */
  1394. p = rdregs(p, e, ctlr->chip[0].arb, regsarb, nelem(regsarb), name);
  1395. p = rdregs(p, e, &ctlr->chip[0].arb->bridge[0], regsbridge, nelem(regsbridge), name);
  1396. p = rdregs(p, e, &ctlr->chip[0].edma[0], regsedma, nelem(regsedma), name);
  1397. return p;
  1398. }
  1399. static int
  1400. waitready(Drive *d)
  1401. {
  1402. ulong s, i;
  1403. for(i = 0; i < 120; i++){
  1404. ilock(d);
  1405. s = d->bridge->status;
  1406. iunlock(d);
  1407. if(s == 0)
  1408. return SDeio;
  1409. if (d->state == Dready)
  1410. return SDok;
  1411. if ((i+1)%60 == 0){
  1412. ilock(d);
  1413. resetdisk(d);
  1414. iunlock(d);
  1415. }
  1416. if(!waserror()){
  1417. tsleep(&up->sleep, return0, 0, 1000);
  1418. poperror();
  1419. }
  1420. }
  1421. print("%s: not responding after 2 minutes\n", d->unit->name);
  1422. return SDeio;
  1423. }
  1424. static int
  1425. mv50rio(SDreq *r)
  1426. {
  1427. int count, max, n, status, try, flag;
  1428. uchar *cmd, *data;
  1429. uvlong lba;
  1430. Ctlr *ctlr;
  1431. Drive *drive;
  1432. SDunit *unit;
  1433. Srb *srb;
  1434. unit = r->unit;
  1435. ctlr = unit->dev->ctlr;
  1436. drive = &ctlr->drive[unit->subno];
  1437. cmd = r->cmd;
  1438. if((status = sdfakescsi(r, drive->info, sizeof drive->info)) != SDnostatus){
  1439. /* XXX check for SDcheck here */
  1440. r->status = status;
  1441. return status;
  1442. }
  1443. switch(cmd[0]){
  1444. case 0x28: /* read */
  1445. case 0x2A: /* write */
  1446. break;
  1447. default:
  1448. iprint("%s: bad cmd 0x%.2ux\n", drive->unit->name, cmd[0]);
  1449. r->status = SDcheck;
  1450. return SDcheck;
  1451. }
  1452. lba = (cmd[2]<<24)|(cmd[3]<<16)|(cmd[4]<<8)|cmd[5];
  1453. count = (cmd[7]<<8)|cmd[8];
  1454. if(r->data == nil)
  1455. return SDok;
  1456. if(r->dlen < count*unit->secsize)
  1457. count = r->dlen/unit->secsize;
  1458. try = 0;
  1459. retry:
  1460. if(waitready(drive) != SDok)
  1461. return SDeio;
  1462. /*
  1463. * Could arrange here to have an Srb always outstanding:
  1464. *
  1465. * lsrb = nil;
  1466. * while(count > 0 || lsrb != nil){
  1467. * srb = nil;
  1468. * if(count > 0){
  1469. * srb = issue next srb;
  1470. * }
  1471. * if(lsrb){
  1472. * sleep on lsrb and handle it
  1473. * }
  1474. * }
  1475. *
  1476. * On the disks I tried, this didn't help. If anything,
  1477. * it's a little slower. -rsc
  1478. */
  1479. data = r->data;
  1480. while(count > 0){
  1481. /*
  1482. * Max is 128 sectors (64kB) because prd->count is 16 bits.
  1483. */
  1484. max = 128;
  1485. n = count;
  1486. if(n > max)
  1487. n = max;
  1488. if((drive->edma->ctl&eEnEDMA) == 0)
  1489. goto tryagain;
  1490. srb = srbrw(cmd[0]==0x28 ? SRBread : SRBwrite, drive, data, n, lba);
  1491. ilock(drive);
  1492. startsrb(drive, srb);
  1493. iunlock(drive);
  1494. /* Don't let user interrupt DMA. */
  1495. while(waserror())
  1496. ;
  1497. sleep(srb, srbdone, srb);
  1498. poperror();
  1499. flag = srb->flag;
  1500. freesrb(srb);
  1501. if(flag == 0){
  1502. tryagain:
  1503. if(++try == 10){
  1504. print("%s: bad disk\n", drive->unit->name);
  1505. return SDeio;
  1506. }
  1507. dprint("%s: retry\n", drive->unit->name);
  1508. if(!waserror()){
  1509. tsleep(&up->sleep, return0, 0, 1000);
  1510. poperror();
  1511. }
  1512. goto retry;
  1513. }
  1514. if(flag & SFerror){
  1515. print("%s: i/o error\n", drive->unit->name);
  1516. return SDeio;
  1517. }
  1518. count -= n;
  1519. lba += n;
  1520. data += n*unit->secsize;
  1521. }
  1522. r->rlen = data - (uchar*)r->data;
  1523. return SDok;
  1524. }
  1525. SDifc sdmv50xxifc = {
  1526. "mv50xx", /* name */
  1527. mv50pnp, /* pnp */
  1528. nil, /* legacy */
  1529. mv50enable, /* enable */
  1530. mv50disable, /* disable */
  1531. mv50verify, /* verify */
  1532. mv50online, /* online */
  1533. mv50rio, /* rio */
  1534. mv50rctl, /* rctl */
  1535. mv50wctl, /* wctl */
  1536. scsibio, /* bio */
  1537. nil, /* probe */
  1538. mv50clear, /* clear */
  1539. mv50rtopctl, /* rtopctl */
  1540. };
  1541. /*
  1542. * The original driver on which this one is based came with the
  1543. * following notice:
  1544. *
  1545. * Copyright 2005
  1546. * Coraid, Inc.
  1547. *
  1548. * This software is provided `as-is,' without any express or implied
  1549. * warranty. In no event will the author be held liable for any damages
  1550. * arising from the use of this software.
  1551. *
  1552. * Permission is granted to anyone to use this software for any purpose,
  1553. * including commercial applications, and to alter it and redistribute it
  1554. * freely, subject to the following restrictions:
  1555. *
  1556. * 1. The origin of this software must not be misrepresented; you must
  1557. * not claim that you wrote the original software. If you use this
  1558. * software in a product, an acknowledgment in the product documentation
  1559. * would be appreciated but is not required.
  1560. *
  1561. * 2. Altered source versions must be plainly marked as such, and must
  1562. * not be misrepresented as being the original software.
  1563. *
  1564. * 3. This notice may not be removed or altered from any source
  1565. * distribution.
  1566. */