sdmv50xx.c 33 KB

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