sd63xxesb.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124
  1. /*
  2. * intel 63[12]?esb ahci sata controller
  3. * copyright © 2007 coraid, inc.
  4. */
  5. #include "u.h"
  6. #include "../port/lib.h"
  7. #include "mem.h"
  8. #include "dat.h"
  9. #include "fns.h"
  10. #include "io.h"
  11. #include "../port/error.h"
  12. #include "../port/sd.h"
  13. #include "ahci.h"
  14. #define dprint(...) if(debug == 1) iprint(__VA_ARGS__); else USED(debug)
  15. #define idprint(...) if(prid == 1) print(__VA_ARGS__); else USED(prid)
  16. #define aprint(...) if(datapi == 1) iprint(__VA_ARGS__); else USED(datapi)
  17. enum{
  18. NCtlr = 4,
  19. NCtlrdrv= 32,
  20. NDrive = NCtlr*NCtlrdrv,
  21. Read = 0,
  22. Write,
  23. };
  24. /* pci space configuration */
  25. enum{
  26. Pmap = 0x90,
  27. Ppcs = 0x91,
  28. Prev = 0xa8,
  29. };
  30. enum{
  31. Dnull,
  32. Dmissing,
  33. Dnew,
  34. Dready,
  35. Derror,
  36. Dreset,
  37. Doffline,
  38. Dportreset,
  39. Dlast,
  40. };
  41. static char *diskstates[Dlast] = {
  42. "null",
  43. "missing",
  44. "new",
  45. "ready",
  46. "error",
  47. "reset",
  48. "offline",
  49. "portreset",
  50. };
  51. extern SDifc sd63xxesbifc;
  52. typedef struct Ctlr Ctlr;
  53. enum{
  54. DMautoneg,
  55. DMsatai,
  56. DMsataii,
  57. };
  58. static char *modename[] = {
  59. "auto",
  60. "satai",
  61. "sataii",
  62. };
  63. static char *flagname[] = {
  64. "llba",
  65. "smart",
  66. "power",
  67. "nop",
  68. "atapi",
  69. "atapi16",
  70. };
  71. typedef struct{
  72. Lock;
  73. Ctlr *ctlr;
  74. SDunit *unit;
  75. char name[10];
  76. Aport *port;
  77. Aportm portm;
  78. Aportc portc; /* redundant ptr to port and portm. */
  79. uchar mediachange;
  80. uchar state;
  81. uchar smartrs;
  82. uvlong sectors;
  83. ulong intick;
  84. int wait;
  85. uchar mode; /* DMautoneg, satai or sataii. */
  86. uchar active;
  87. char serial[20+1];
  88. char firmware[8+1];
  89. char model[40+1];
  90. ushort info[0x200];
  91. int driveno; /* ctlr*NCtlrdrv + unit */
  92. /* controller port # != driveno when not all ports are enabled */
  93. int portno;
  94. }Drive;
  95. struct Ctlr{
  96. Lock;
  97. int irq;
  98. int tbdf;
  99. int enabled;
  100. SDev *sdev;
  101. Pcidev *pci;
  102. uchar *mmio;
  103. ulong *lmmio;
  104. Ahba *hba;
  105. Drive rawdrive[NCtlrdrv];
  106. Drive* drive[NCtlrdrv];
  107. int ndrive;
  108. };
  109. static Ctlr iactlr[NCtlr];
  110. static SDev sdevs[NCtlr];
  111. static int niactlr;
  112. static Drive *iadrive[NDrive];
  113. static int niadrive;
  114. static int debug;
  115. static int prid = 1;
  116. static int datapi = 0;
  117. static char stab[] = {
  118. [0] 'i', 'm',
  119. [8] 't', 'c', 'p', 'e',
  120. [16] 'N', 'I', 'W', 'B', 'D', 'C', 'H', 'S', 'T', 'F', 'X'
  121. };
  122. static void
  123. serrstr(ulong r, char *s, char *e)
  124. {
  125. int i;
  126. e -= 3;
  127. for(i = 0; i < nelem(stab) && s < e; i++)
  128. if(r & (1<<i) && stab[i]){
  129. *s++ = stab[i];
  130. if(SerrBad & (1<<i))
  131. *s++ = '*';
  132. }
  133. *s = 0;
  134. }
  135. static char ntab[] = "0123456789abcdef";
  136. static void
  137. preg(uchar *reg, int n)
  138. {
  139. int i;
  140. char buf[25*3+1], *e;
  141. e = buf;
  142. for(i = 0; i < n; i++){
  143. *e++ = ntab[reg[i]>>4];
  144. *e++ = ntab[reg[i]&0xf];
  145. *e++ = ' ';
  146. }
  147. *e++ = '\n';
  148. *e = 0;
  149. dprint(buf);
  150. }
  151. static void
  152. dreg(char *s, Aport *p)
  153. {
  154. dprint("%stask=%ux; cmd=%ux; ci=%ux; is=%ux\n", s, p->task, p->cmd,
  155. p->ci, p->isr);
  156. }
  157. static void
  158. esleep(int ms)
  159. {
  160. if(waserror())
  161. return;
  162. tsleep(&up->sleep, return0, 0, ms);
  163. poperror();
  164. }
  165. typedef struct{
  166. Aport *p;
  167. int i;
  168. }Asleep;
  169. static int
  170. ahciclear(void *v)
  171. {
  172. Asleep *s;
  173. s = v;
  174. return (s->p->ci&s->i) == 0;
  175. }
  176. static void
  177. aesleep(Aportm *m, Asleep *a, int ms)
  178. {
  179. if(waserror())
  180. return;
  181. tsleep(m, ahciclear, a, ms);
  182. poperror();
  183. }
  184. static int
  185. ahciwait(Aportc *c, int ms)
  186. {
  187. Asleep as;
  188. Aport *p;
  189. p = c->p;
  190. p->ci = 1;
  191. as.p = p;
  192. as.i = 1;
  193. aesleep(c->m, &as, ms);
  194. if((p->task&1) == 0 && p->ci == 0)
  195. return 0;
  196. dreg("ahciwait timeout ", c->p);
  197. return -1;
  198. }
  199. static int
  200. nop(Aportc *pc)
  201. {
  202. uchar *c;
  203. Actab *t;
  204. Alist *l;
  205. if((pc->m->feat&Dnop) == 0)
  206. return -1;
  207. t = pc->m->ctab;
  208. c = t->cfis;
  209. memset(c, 0, 0x20);
  210. c[0] = 0x27;
  211. c[1] = 0x80;
  212. c[2] = 0x00;
  213. c[7] = 0xa0; /* obsolete device bits */
  214. l = pc->m->list;
  215. l->flags = Lwrite | 0x5;
  216. l->len = 0;
  217. l->ctab = PCIWADDR(t);
  218. l->ctabhi = 0;
  219. return ahciwait(pc, 3*1000);
  220. }
  221. static int
  222. setfeatures(Aportc *pc, uchar f)
  223. {
  224. uchar *c;
  225. Actab *t;
  226. Alist *l;
  227. t = pc->m->ctab;
  228. c = t->cfis;
  229. memset(c, 0, 0x20);
  230. c[0] = 0x27;
  231. c[1] = 0x80;
  232. c[2] = 0xef;
  233. c[3] = f;
  234. c[7] = 0xa0; /* obsolete device bits */
  235. l = pc->m->list;
  236. l->flags = Lwrite | 0x5;
  237. l->len = 0;
  238. l->ctab = PCIWADDR(t);
  239. l->ctabhi = 0;
  240. return ahciwait(pc, 3*1000);
  241. }
  242. static int
  243. setudmamode(Aportc *pc, uchar f)
  244. {
  245. uchar *c;
  246. Actab *t;
  247. Alist *l;
  248. t = pc->m->ctab;
  249. c = t->cfis;
  250. memset(c, 0, 0x20);
  251. c[0] = 0x27;
  252. c[1] = 0x80;
  253. c[2] = 0xef;
  254. c[3] = 3; /* set transfer mode */
  255. c[7] = 0xa0; /* obsolete device bits */
  256. c[12] = 0x40 | f; /* sector count */
  257. l = pc->m->list;
  258. l->flags = Lwrite | 0x5;
  259. l->len = 0;
  260. l->ctab = PCIWADDR(t);
  261. l->ctabhi = 0;
  262. return ahciwait(pc, 3*1000);
  263. }
  264. static void
  265. asleep(int ms)
  266. {
  267. if(up == nil)
  268. delay(ms);
  269. else
  270. esleep(ms);
  271. }
  272. static int
  273. ahciportreset(Aportc *c)
  274. {
  275. u32int *cmd, i;
  276. Aport *p;
  277. p = c->p;
  278. cmd = &p->cmd;
  279. *cmd &= ~(Afre|Ast);
  280. for(i = 0; i < 500; i += 25){
  281. if((*cmd&Acr) == 0)
  282. break;
  283. asleep(25);
  284. }
  285. p->sctl = 1|(p->sctl&~7);
  286. delay(1);
  287. p->sctl &= ~7;
  288. return 0;
  289. }
  290. static int
  291. smart(Aportc *pc, int n)
  292. {
  293. uchar *c;
  294. Actab *t;
  295. Alist *l;
  296. if((pc->m->feat&Dsmart) == 0)
  297. return -1;
  298. t = pc->m->ctab;
  299. c = t->cfis;
  300. memset(c, 0, 0x20);
  301. c[0] = 0x27;
  302. c[1] = 0x80;
  303. c[2] = 0xb0;
  304. c[3] = 0xd8 + n; /* able smart */
  305. c[5] = 0x4f;
  306. c[6] = 0xc2;
  307. c[7] = 0xa0;
  308. l = pc->m->list;
  309. l->flags = Lwrite | 0x5;
  310. l->len = 0;
  311. l->ctab = PCIWADDR(t);
  312. l->ctabhi = 0;
  313. if(ahciwait(pc, 1000) == -1 || pc->p->task & (1|32)){
  314. dprint("smart fail %ux\n", pc->p->task);
  315. // preg(pc->m->fis.r, 20);
  316. return -1;
  317. }
  318. if(n)
  319. return 0;
  320. return 1;
  321. }
  322. static int
  323. smartrs(Aportc *pc)
  324. {
  325. uchar *c;
  326. Actab *t;
  327. Alist *l;
  328. t = pc->m->ctab;
  329. c = t->cfis;
  330. memset(c, 0, 0x20);
  331. c[0] = 0x27;
  332. c[1] = 0x80;
  333. c[2] = 0xb0;
  334. c[3] = 0xda; /* return smart status */
  335. c[5] = 0x4f;
  336. c[6] = 0xc2;
  337. c[7] = 0xa0;
  338. l = pc->m->list;
  339. l->flags = Lwrite | 0x5;
  340. l->len = 0;
  341. l->ctab = PCIWADDR(t);
  342. l->ctabhi = 0;
  343. c = pc->m->fis.r;
  344. if(ahciwait(pc, 1000) == -1 || pc->p->task & (1|32)){
  345. dprint("smart fail %ux\n", pc->p->task);
  346. preg(c, 20);
  347. return -1;
  348. }
  349. if(c[5] == 0x4f && c[6] == 0xc2)
  350. return 1;
  351. return 0;
  352. }
  353. static int
  354. flushcache(Aportc *pc)
  355. {
  356. uchar *c, llba;
  357. Actab *t;
  358. Alist *l;
  359. static uchar tab[2] = {0xe7, 0xea};
  360. llba = pc->m->feat&Dllba? 1: 0;
  361. t = pc->m->ctab;
  362. c = t->cfis;
  363. memset(c, 0, 0x20);
  364. c[0] = 0x27;
  365. c[1] = 0x80;
  366. c[2] = tab[llba];
  367. c[7] = 0xa0;
  368. l = pc->m->list;
  369. l->flags = Lwrite | 0x5;
  370. l->len = 0;
  371. l->ctab = PCIWADDR(t);
  372. l->ctabhi = 0;
  373. if(ahciwait(pc, 60000) == -1 || pc->p->task & (1|32)){
  374. dprint("flushcache fail %ux\n", pc->p->task);
  375. // preg( pc->m->fis.r, 20);
  376. return -1;
  377. }
  378. return 0;
  379. }
  380. static ushort
  381. gbit16(void *a)
  382. {
  383. ushort j;
  384. uchar *i;
  385. i = a;
  386. j = i[1] << 8;
  387. j |= i[0];
  388. return j;
  389. }
  390. static u32int
  391. gbit32(void *a)
  392. {
  393. u32int j;
  394. uchar *i;
  395. i = a;
  396. j = i[3] << 24;
  397. j |= i[2] << 16;
  398. j |= i[1] << 8;
  399. j |= i[0];
  400. return j;
  401. }
  402. static uvlong
  403. gbit64(void *a)
  404. {
  405. uchar *i;
  406. i = a;
  407. return (uvlong)gbit32(i+4) << 32 | gbit32(a);
  408. }
  409. static int
  410. ahciidentify0(Aportc *pc, void *id, int atapi)
  411. {
  412. uchar *c;
  413. Actab *t;
  414. Alist *l;
  415. Aprdt *p;
  416. static uchar tab[] = { 0xec, 0xa1, };
  417. t = pc->m->ctab;
  418. c = t->cfis;
  419. memset(c, 0, 0x20);
  420. c[0] = 0x27;
  421. c[1] = 0x80;
  422. c[2] = tab[atapi];
  423. c[7] = 0xa0; /* obsolete device bits */
  424. l = pc->m->list;
  425. l->flags = 1<<16 | 0x5;
  426. l->len = 0;
  427. l->ctab = PCIWADDR(t);
  428. l->ctabhi = 0;
  429. memset(id, 0, 0x100);
  430. p = &t->prdt;
  431. p->dba = PCIWADDR(id);
  432. p->dbahi = 0;
  433. p->count = 1<<31 | (0x200-2) | 1;
  434. return ahciwait(pc, 3*1000);
  435. }
  436. static vlong
  437. ahciidentify(Aportc *pc, ushort *id)
  438. {
  439. int i, sig;
  440. vlong s;
  441. Aportm *m;
  442. m = pc->m;
  443. m->feat = 0;
  444. m->smart = 0;
  445. i = 0;
  446. sig = pc->p->sig >> 16;
  447. if(sig == 0xeb14){
  448. m->feat |= Datapi;
  449. i = 1;
  450. }
  451. if(ahciidentify0(pc, id, i) == -1)
  452. return -1;
  453. i = gbit16(id+83) | gbit16(id+86);
  454. if(i & (1<<10)){
  455. m->feat |= Dllba;
  456. s = gbit64(id+100);
  457. }else
  458. s = gbit32(id+60);
  459. if(m->feat&Datapi){
  460. i = gbit16(id+0);
  461. if(i&1)
  462. m->feat |= Datapi16;
  463. }
  464. i = gbit16(id+83);
  465. if((i>>14) == 1) {
  466. if(i & (1<<3))
  467. m->feat |= Dpower;
  468. i = gbit16(id+82);
  469. if(i & 1)
  470. m->feat |= Dsmart;
  471. if(i & (1<<14))
  472. m->feat |= Dnop;
  473. }
  474. return s;
  475. }
  476. static int
  477. ahciquiet(Aport *a)
  478. {
  479. u32int *p, i;
  480. p = &a->cmd;
  481. *p &= ~Ast;
  482. for(i = 0; i < 500; i += 50){
  483. if((*p & Acr) == 0)
  484. goto stop;
  485. asleep(50);
  486. }
  487. return -1;
  488. stop:
  489. if((a->task & (ASdrq|ASbsy)) == 0){
  490. *p |= Ast;
  491. return 0;
  492. }
  493. *p |= Aclo;
  494. for(i = 0; i < 500; i += 50){
  495. if((*p & Aclo) == 0)
  496. goto stop1;
  497. asleep(50);
  498. }
  499. return -1;
  500. stop1:
  501. /* extra check */
  502. dprint("clo clear %x\n", a->task);
  503. if(a->task & ASbsy)
  504. return -1;
  505. *p |= Ast;
  506. return 0;
  507. }
  508. static int
  509. ahcicomreset(Aportc *pc)
  510. {
  511. uchar *c;
  512. Actab *t;
  513. Alist *l;
  514. dprint("ahcicomreset\n");
  515. dreg("comreset ", pc->p);
  516. if(ahciquiet(pc->p) == -1){
  517. dprint("ahciquiet fails\n");
  518. return -1;
  519. }
  520. dreg("comreset ", pc->p);
  521. t = pc->m->ctab;
  522. c = t->cfis;
  523. memset(c, 0, 0x20);
  524. c[0] = 0x27;
  525. c[1] = 0x00;
  526. c[7] = 0xa0; /* obsolete device bits */
  527. c[15] = 1<<2; /* srst */
  528. l = pc->m->list;
  529. l->flags = Lclear | Lreset | 0x5;
  530. l->len = 0;
  531. l->ctab = PCIWADDR(t);
  532. l->ctabhi = 0;
  533. if(ahciwait(pc, 500) == -1){
  534. dprint("first command in comreset fails\n");
  535. return -1;
  536. }
  537. microdelay(250);
  538. dreg("comreset ", pc->p);
  539. memset(c, 0, 0x20);
  540. c[0] = 0x27;
  541. c[1] = 0x00;
  542. c[7] = 0xa0; /* obsolete device bits */
  543. l = pc->m->list;
  544. l->flags = Lwrite | 0x5;
  545. l->len = 0;
  546. l->ctab = PCIWADDR(t);
  547. l->ctabhi = 0;
  548. if(ahciwait(pc, 150) == -1){
  549. dprint("second command in comreset fails\n");
  550. return -1;
  551. }
  552. dreg("comreset ", pc->p);
  553. return 0;
  554. }
  555. static int
  556. ahciidle(Aport *port)
  557. {
  558. u32int *p, i, r;
  559. p = &port->cmd;
  560. if((*p & Arun) == 0)
  561. return 0;
  562. *p &= ~Ast;
  563. r = 0;
  564. for(i = 0; i < 500; i += 25){
  565. if((*p & Acr) == 0)
  566. goto stop;
  567. asleep(25);
  568. }
  569. r = -1;
  570. stop:
  571. if((*p & Afre) == 0)
  572. return r;
  573. *p &= ~Afre;
  574. for(i = 0; i < 500; i += 25){
  575. if((*p & Afre) == 0)
  576. return 0;
  577. asleep(25);
  578. }
  579. return -1;
  580. }
  581. /*
  582. * § 6.2.2.1 first part; comreset handled by reset disk.
  583. * - remainder is handled by configdisk.
  584. * - ahcirecover is a quick recovery from a failed command.
  585. */
  586. int
  587. ahciswreset(Aportc *pc)
  588. {
  589. int i;
  590. i = ahciidle(pc->p);
  591. pc->p->cmd |= Afre;
  592. if(i == -1)
  593. return -1;
  594. if(pc->p->task & (ASdrq|ASbsy))
  595. return -1;
  596. return 0;
  597. }
  598. int
  599. ahcirecover(Aportc *pc)
  600. {
  601. ahciswreset(pc);
  602. pc->p->cmd |= Ast;
  603. if(setudmamode(pc, 5) == -1)
  604. return -1;
  605. return 0;
  606. }
  607. static void*
  608. malign(int size, int align)
  609. {
  610. void *v;
  611. v = xspanalloc(size, align, 0);
  612. memset(v, 0, size);
  613. return v;
  614. }
  615. static void
  616. setupfis(Afis *f)
  617. {
  618. f->base = malign(0x100, 0x100);
  619. f->d = f->base + 0;
  620. f->p = f->base + 0x20;
  621. f->r = f->base + 0x40;
  622. f->u = f->base + 0x60;
  623. f->devicebits = (u32int*)(f->base + 0x58);
  624. }
  625. static int
  626. ahciconfigdrive(Ahba *h, Aportc *c, int mode)
  627. {
  628. Aportm *m;
  629. Aport *p;
  630. p = c->p;
  631. m = c->m;
  632. if(m->list == 0){
  633. setupfis(&m->fis);
  634. m->list = malign(sizeof *m->list, 1024);
  635. m->ctab = malign(sizeof *m->ctab, 128);
  636. }
  637. if(p->sstatus & 3 && h->cap & Hsss){
  638. dprint("configdrive: spinning up ... [%ux]\n", p->sstatus);
  639. p->cmd |= Apod|Asud;
  640. asleep(1400);
  641. }
  642. p->serror = SerrAll;
  643. p->list = PCIWADDR(m->list);
  644. p->listhi = 0;
  645. p->fis = PCIWADDR(m->fis.base);
  646. p->fishi = 0;
  647. p->cmd |= Afre|Ast;
  648. /* disable power managment sequence from book. */
  649. p->sctl = (3*Aipm) | (mode*Aspd) | 0*Adet;
  650. p->cmd &= ~Aalpe;
  651. p->ie = IEM;
  652. return 0;
  653. }
  654. static int
  655. ahcienable(Ahba *h)
  656. {
  657. h->ghc |= Hie;
  658. return 0;
  659. }
  660. static int
  661. ahcidisable(Ahba *h)
  662. {
  663. h->ghc &= ~Hie;
  664. return 0;
  665. }
  666. static int
  667. countbits(ulong u)
  668. {
  669. int i, n;
  670. n = 0;
  671. for(i = 0; i < 32; i++)
  672. if(u & (1<<i))
  673. n++;
  674. return n;
  675. }
  676. static int
  677. ahciconf(Ctlr *c)
  678. {
  679. Ahba *h;
  680. u32int u;
  681. h = c->hba = (Ahba*)c->mmio;
  682. u = h->cap;
  683. if((u&Hsam) == 0)
  684. h->ghc |= Hae;
  685. print("ahci hba sss %d; ncs %d; coal %d; mports %d; led %d; clo %d; ems %d;\n",
  686. (u>>27) & 1, (u>>8) & 0x1f, (u>>7) & 1, u & 0x1f, (u>>25) & 1,
  687. (u>>24) & 1, (u>>6) & 1);
  688. return countbits(h->pi);
  689. }
  690. static int
  691. ahcihbareset(Ahba *h)
  692. {
  693. int wait;
  694. h->ghc |= 1;
  695. for(wait = 0; wait < 1000; wait += 100){
  696. if(h->ghc == 0)
  697. return 0;
  698. delay(100);
  699. }
  700. return -1;
  701. }
  702. static void
  703. idmove(char *p, ushort *a, int n)
  704. {
  705. int i;
  706. char *op, *e;
  707. op = p;
  708. for(i = 0; i < n/2; i++){
  709. *p++ = a[i] >> 8;
  710. *p++ = a[i];
  711. }
  712. *p = 0;
  713. while(p > op && *--p == ' ')
  714. *p = 0;
  715. e = p;
  716. p = op;
  717. while(*p == ' ')
  718. p++;
  719. memmove(op, p, n - (e - p));
  720. }
  721. static int
  722. identify(Drive *d)
  723. {
  724. u16int *id;
  725. vlong osectors, s;
  726. uchar oserial[21];
  727. SDunit *u;
  728. id = d->info;
  729. s = ahciidentify(&d->portc, id);
  730. if(s == -1){
  731. d->state = Derror;
  732. return -1;
  733. }
  734. osectors = d->sectors;
  735. memmove(oserial, d->serial, sizeof d->serial);
  736. d->sectors = s;
  737. d->smartrs = 0;
  738. idmove(d->serial, id+10, 20);
  739. idmove(d->firmware, id+23, 8);
  740. idmove(d->model, id+27, 40);
  741. u = d->unit;
  742. memset(u->inquiry, 0, sizeof u->inquiry);
  743. u->inquiry[2] = 2;
  744. u->inquiry[3] = 2;
  745. u->inquiry[4] = sizeof u->inquiry - 4;
  746. memmove(u->inquiry+8, d->model, 40);
  747. if((osectors == 0 || osectors != s) &&
  748. memcmp(oserial, d->serial, sizeof oserial) != 0){
  749. d->mediachange = 1;
  750. u->sectors = 0;
  751. }
  752. return 0;
  753. }
  754. static void
  755. clearci(Aport *p)
  756. {
  757. if(p->cmd & Ast) {
  758. p->cmd &= ~Ast;
  759. p->cmd |= Ast;
  760. }
  761. }
  762. static void
  763. updatedrive(Drive *d)
  764. {
  765. u32int cause, serr, s0, pr, ewake;
  766. char *name;
  767. Aport *p;
  768. static u32int last;
  769. pr = 1;
  770. ewake = 0;
  771. p = d->port;
  772. cause = p->isr;
  773. serr = p->serror;
  774. p->isr = cause;
  775. name = "??";
  776. if(d->unit && d->unit->name)
  777. name = d->unit->name;
  778. if(p->ci == 0){
  779. d->portm.flag |= Fdone;
  780. wakeup(&d->portm);
  781. pr = 0;
  782. }else if(cause & Adps)
  783. pr = 0;
  784. if(cause & Ifatal){
  785. ewake = 1;
  786. dprint("Fatal\n");
  787. }
  788. if(cause & Adhrs){
  789. if(p->task & 33){
  790. dprint("Adhrs cause = %ux; serr = %ux; task=%ux\n",
  791. cause, serr, p->task);
  792. d->portm.flag |= Ferror;
  793. ewake = 1;
  794. }
  795. pr = 0;
  796. }
  797. // if(p->task & 1 && last != cause)
  798. // dprint("err ca %ux serr %ux task %ux sstat %ux\n",
  799. // cause, serr, p->task, p->sstatus);
  800. if(pr)
  801. dprint("%s: upd %ux ta %ux\n", name, cause, p->task);
  802. if(cause & (Aprcs|Aifs)){
  803. s0 = d->state;
  804. switch(p->sstatus & 7){
  805. case 0:
  806. d->state = Dmissing;
  807. break;
  808. case 1:
  809. d->state = Derror;
  810. break;
  811. case 3:
  812. /* power mgnt crap for suprise removal */
  813. p->ie |= Aprcs|Apcs; /* is this required? */
  814. d->state = Dreset;
  815. break;
  816. case 4:
  817. d->state = Doffline;
  818. break;
  819. }
  820. dprint("%s: %s → %s [Apcrs] %ux\n", name, diskstates[s0],
  821. diskstates[d->state], p->sstatus);
  822. /* print pulled message here. */
  823. if(s0 == Dready && d->state != Dready)
  824. idprint("%s: pulled\n", name);
  825. if(d->state != Dready)
  826. d->portm.flag |= Ferror;
  827. ewake = 1;
  828. }
  829. p->serror = serr;
  830. if(ewake){
  831. clearci(p);
  832. wakeup(&d->portm);
  833. }
  834. last = cause;
  835. }
  836. static void
  837. pstatus(Drive *d, ulong s)
  838. {
  839. /*
  840. * bogus code because the first interrupt is currently dropped.
  841. * likely my fault. serror is maybe cleared at the wrong time.
  842. */
  843. switch(s){
  844. case 0:
  845. d->state = Dmissing;
  846. break;
  847. case 2: /* should this be missing? need testcase. */
  848. dprint("pstatus 2\n");
  849. /* fallthrough */
  850. case 3:
  851. d->wait = 0;
  852. d->state = Dnew;
  853. break;
  854. case 4:
  855. d->state = Doffline;
  856. break;
  857. }
  858. }
  859. static int
  860. configdrive(Drive *d)
  861. {
  862. if(ahciconfigdrive(d->ctlr->hba, &d->portc, d->mode) == -1)
  863. return -1;
  864. ilock(d);
  865. pstatus(d, d->port->sstatus & 7);
  866. iunlock(d);
  867. return 0;
  868. }
  869. static void
  870. resetdisk(Drive *d)
  871. {
  872. uint state, det, stat;
  873. Aport *p;
  874. p = d->port;
  875. det = p->sctl & 7;
  876. stat = p->sstatus & 7;
  877. state = (p->cmd>>28) & 0xf;
  878. dprint("resetdisk: icc %ux det %d sdet %d\n", state, det, stat);
  879. if(stat != 3){
  880. ilock(d);
  881. d->state = Dportreset;
  882. iunlock(d);
  883. return;
  884. }
  885. ilock(d);
  886. state = d->state;
  887. if(d->state != Dready || d->state != Dnew)
  888. d->portm.flag |= Ferror;
  889. clearci(p); /* satisfy sleep condition. */
  890. wakeup(&d->portm);
  891. iunlock(d);
  892. qlock(&d->portm);
  893. if(p->cmd&Ast && ahciswreset(&d->portc) == -1){
  894. ilock(d);
  895. d->state = Dportreset; /* get a bigger stick. */
  896. iunlock(d);
  897. } else {
  898. ilock(d);
  899. d->state = Dmissing;
  900. iunlock(d);
  901. configdrive(d);
  902. }
  903. dprint("resetdisk: %s → %s\n", diskstates[state], diskstates[d->state]);
  904. qunlock(&d->portm);
  905. }
  906. static int
  907. newdrive(Drive *d)
  908. {
  909. char *name, *s;
  910. Aportc *c;
  911. Aportm *m;
  912. c = &d->portc;
  913. m = &d->portm;
  914. name = d->unit->name;
  915. if(name == 0)
  916. name = "??";
  917. if(d->port->task == 0x80)
  918. return -1;
  919. qlock(c->m);
  920. if(setudmamode(c, 5) == -1){
  921. dprint("%s: can't set udma mode\n", name);
  922. goto lose;
  923. }
  924. if(identify(d) == -1){
  925. dprint("%s: identify failure\n", name);
  926. goto lose;
  927. }
  928. if(m->feat & Dpower && setfeatures(c, 0x85) == -1){
  929. m->feat &= ~Dpower;
  930. if(ahcirecover(c) == -1)
  931. goto lose;
  932. }
  933. ilock(d);
  934. d->state = Dready;
  935. iunlock(d);
  936. qunlock(c->m);
  937. s = "";
  938. if(m->feat & Dllba)
  939. s = "L";
  940. idprint("%s: %sLBA %,lld sectors\n", d->unit->name, s, d->sectors);
  941. idprint(" %s %s %s %s\n", d->model, d->firmware, d->serial,
  942. d->mediachange?"[mediachange]":"");
  943. return 0;
  944. lose:
  945. // qunlock(&d->portm); /* shurely shome mishtake */
  946. qunlock(c->m);
  947. return -1;
  948. }
  949. enum{
  950. Nms = 256,
  951. Mphywait = 2*1024/Nms - 1,
  952. Midwait = 16*1024/Nms - 1,
  953. Mcomrwait = 64*1024/Nms - 1,
  954. };
  955. static void
  956. westerndigitalhung(Drive *d)
  957. {
  958. if((d->portm.feat&Datapi) == 0 && d->active &&
  959. TK2MS(MACHP(0)->ticks-d->intick) > 5000){
  960. dprint("%s: drive hung; resetting [%ux] ci=%x\n",
  961. d->unit->name, d->port->task, d->port->ci);
  962. d->state = Dreset;
  963. }
  964. }
  965. static ushort olds[NCtlr*NCtlrdrv];
  966. static int
  967. doportreset(Drive *d)
  968. {
  969. int i;
  970. i = -1;
  971. qlock(&d->portm);
  972. if(ahciportreset(&d->portc) == -1)
  973. dprint("ahciportreset fails\n");
  974. else
  975. i = 0;
  976. qunlock(&d->portm);
  977. dprint("portreset → %s [task %ux]\n",
  978. diskstates[d->state], d->port->task);
  979. return i;
  980. }
  981. static void
  982. checkdrive(Drive *d, int i)
  983. {
  984. ushort s;
  985. char *name;
  986. ilock(d);
  987. name = d->unit->name;
  988. s = d->port->sstatus;
  989. if(s != olds[i]){
  990. dprint("%s: status: %04ux -> %04ux: %s\n",
  991. name, olds[i], s, diskstates[d->state]);
  992. olds[i] = s;
  993. d->wait = 0;
  994. }
  995. westerndigitalhung(d);
  996. switch(d->state){
  997. case Dnull:
  998. case Dready:
  999. break;
  1000. case Dmissing:
  1001. case Dnew:
  1002. switch(s & 0x107){
  1003. case 0:
  1004. case 1:
  1005. break;
  1006. default:
  1007. dprint("%s: unknown status %04ux\n", name, s);
  1008. case 0x100:
  1009. if(++d->wait&Mphywait)
  1010. break;
  1011. reset:
  1012. if(++d->mode > DMsataii)
  1013. d->mode = 0;
  1014. if(d->mode == DMsatai){ /* we tried everything */
  1015. d->state = Dportreset;
  1016. goto portreset;
  1017. }
  1018. dprint("%s: reset; new mode %s\n", name,
  1019. modename[d->mode]);
  1020. iunlock(d);
  1021. resetdisk(d);
  1022. ilock(d);
  1023. break;
  1024. case 0x103:
  1025. if((++d->wait&Midwait) == 0){
  1026. dprint("%s: slow reset %04ux task=%ux; %d\n",
  1027. name, s, d->port->task, d->wait);
  1028. goto reset;
  1029. }
  1030. s = d->port->task&0xff;
  1031. if(s == 0x7f || ((d->port->sig >> 16) != 0xeb14 &&
  1032. (s & ~0x17) != (1<<6)))
  1033. break;
  1034. iunlock(d);
  1035. newdrive(d);
  1036. ilock(d);
  1037. break;
  1038. }
  1039. break;
  1040. case Doffline:
  1041. if(d->wait++ & Mcomrwait)
  1042. break;
  1043. /* fallthrough */
  1044. case Derror:
  1045. case Dreset:
  1046. dprint("%s: reset [%s]: mode %d; status %04ux\n",
  1047. name, diskstates[d->state], d->mode, s);
  1048. iunlock(d);
  1049. resetdisk(d);
  1050. ilock(d);
  1051. break;
  1052. case Dportreset:
  1053. portreset:
  1054. if(d->wait++ & 0xff && (s & 0x100) == 0)
  1055. break;
  1056. dprint("%s: portreset [%s]: mode %d; status %04ux\n",
  1057. name, diskstates[d->state], d->mode, s);
  1058. d->portm.flag |= Ferror;
  1059. clearci(d->port);
  1060. wakeup(&d->portm);
  1061. if((s & 7) == 0){
  1062. d->state = Dmissing;
  1063. break;
  1064. }
  1065. iunlock(d);
  1066. doportreset(d);
  1067. ilock(d);
  1068. break;
  1069. }
  1070. iunlock(d);
  1071. }
  1072. static void
  1073. satakproc(void*)
  1074. {
  1075. int i;
  1076. memset(olds, 0xff, sizeof olds);
  1077. for(;;){
  1078. tsleep(&up->sleep, return0, 0, Nms);
  1079. for(i = 0; i < niadrive; i++)
  1080. checkdrive(iadrive[i], i);
  1081. }
  1082. }
  1083. static void
  1084. iainterrupt(Ureg*, void *a)
  1085. {
  1086. int i;
  1087. ulong cause, m;
  1088. Ctlr *c;
  1089. Drive *d;
  1090. c = a;
  1091. ilock(c);
  1092. cause = c->hba->isr;
  1093. for(i = 0; i < c->ndrive; i++){
  1094. m = 1 << i;
  1095. if((cause & m) == 0)
  1096. continue;
  1097. d = c->rawdrive + i;
  1098. ilock(d);
  1099. if(d->port->isr && c->hba->pi & m)
  1100. updatedrive(d);
  1101. c->hba->isr = m;
  1102. iunlock(d);
  1103. }
  1104. iunlock(c);
  1105. }
  1106. static int
  1107. iaverify(SDunit *u)
  1108. {
  1109. Ctlr *c;
  1110. Drive *d;
  1111. c = u->dev->ctlr;
  1112. d = c->drive[u->subno];
  1113. ilock(c);
  1114. ilock(d);
  1115. d->unit = u;
  1116. iunlock(d);
  1117. iunlock(c);
  1118. return 1;
  1119. }
  1120. static int
  1121. iaenable(SDev *s)
  1122. {
  1123. char name[32];
  1124. static int once;
  1125. Ctlr *c;
  1126. c = s->ctlr;
  1127. ilock(c);
  1128. if(!c->enabled) {
  1129. if(once++ == 0)
  1130. kproc("iasata", satakproc, 0);
  1131. pcisetbme(c->pci);
  1132. snprint(name, sizeof name, "%s (%s)", s->name, s->ifc->name);
  1133. intrenable(c->irq, iainterrupt, c, c->tbdf, name);
  1134. /* supposed to squelch leftover interrupts here. */
  1135. ahcienable(c->hba);
  1136. }
  1137. c->enabled = 1;
  1138. iunlock(c);
  1139. return 1;
  1140. }
  1141. static int
  1142. iadisable(SDev *s)
  1143. {
  1144. char name[32];
  1145. Ctlr *c;
  1146. c = s->ctlr;
  1147. ilock(c);
  1148. ahcidisable(c->hba);
  1149. snprint(name, sizeof name, "%s (%s)", s->name, s->ifc->name);
  1150. intrdisable(c->irq, iainterrupt, c, c->tbdf, name);
  1151. c->enabled = 0;
  1152. iunlock(c);
  1153. return 1;
  1154. }
  1155. static int
  1156. iaonline(SDunit *unit)
  1157. {
  1158. int r;
  1159. Ctlr *c;
  1160. Drive *d;
  1161. c = unit->dev->ctlr;
  1162. d = c->drive[unit->subno];
  1163. r = 0;
  1164. if(d->portm.feat & Datapi && d->mediachange){
  1165. r = scsionline(unit);
  1166. if(r > 0)
  1167. d->mediachange = 0;
  1168. return r;
  1169. }
  1170. ilock(d);
  1171. if(d->mediachange){
  1172. r = 2;
  1173. d->mediachange = 0;
  1174. /* devsd resets this after online is called; why? */
  1175. unit->sectors = d->sectors;
  1176. unit->secsize = 512;
  1177. } else if(d->state == Dready)
  1178. r = 1;
  1179. iunlock(d);
  1180. return r;
  1181. }
  1182. /* returns locked list! */
  1183. static Alist*
  1184. ahcibuild(Aportm *m, uchar *cmd, void *data, int n, vlong lba)
  1185. {
  1186. uchar *c, acmd, dir, llba;
  1187. Alist *l;
  1188. Actab *t;
  1189. Aprdt *p;
  1190. static uchar tab[2][2] = { 0xc8, 0x25, 0xca, 0x35, };
  1191. dir = *cmd != 0x28;
  1192. llba = m->feat&Dllba? 1: 0;
  1193. acmd = tab[dir][llba];
  1194. qlock(m);
  1195. l = m->list;
  1196. t = m->ctab;
  1197. c = t->cfis;
  1198. c[0] = 0x27;
  1199. c[1] = 0x80;
  1200. c[2] = acmd;
  1201. c[3] = 0;
  1202. c[4] = lba; /* sector lba low 7:0 */
  1203. c[5] = lba >> 8; /* cylinder low lba mid 15:8 */
  1204. c[6] = lba >> 16; /* cylinder hi lba hi 23:16 */
  1205. c[7] = 0xa0 | 0x40; /* obsolete device bits + lba */
  1206. if(llba == 0)
  1207. c[7] |= (lba>>24) & 7;
  1208. c[8] = lba >> 24; /* sector (exp) lba 31:24 */
  1209. c[9] = lba >> 32; /* cylinder low (exp) lba 39:32 */
  1210. c[10] = lba >> 48; /* cylinder hi (exp) lba 48:40 */
  1211. c[11] = 0; /* features (exp); */
  1212. c[12] = n; /* sector count */
  1213. c[13] = n >> 8; /* sector count (exp) */
  1214. c[14] = 0; /* r */
  1215. c[15] = 0; /* control */
  1216. *(ulong*)(c + 16) = 0;
  1217. l->flags = 1<<16 | Lpref | 0x5; /* Lpref ?? */
  1218. if(dir == Write)
  1219. l->flags |= Lwrite;
  1220. l->len = 0;
  1221. l->ctab = PCIWADDR(t);
  1222. l->ctabhi = 0;
  1223. p = &t->prdt;
  1224. p->dba = PCIWADDR(data);
  1225. p->dbahi = 0;
  1226. p->count = 1<<31 | (512*n - 2) | 1;
  1227. return l;
  1228. }
  1229. static Alist*
  1230. ahcibuildpkt(Aportm *m, SDreq *r, void *data, int n)
  1231. {
  1232. int fill, len;
  1233. uchar *c;
  1234. Alist *l;
  1235. Actab *t;
  1236. Aprdt *p;
  1237. qlock(m);
  1238. l = m->list;
  1239. t = m->ctab;
  1240. c = t->cfis;
  1241. fill = m->feat&Datapi16? 16: 12;
  1242. if((len = r->clen) > fill)
  1243. len = fill;
  1244. memmove(t->atapi, r->cmd, len);
  1245. memset(t->atapi+len, 0, fill-len);
  1246. c[0] = 0x27;
  1247. c[1] = 0x80;
  1248. c[2] = 0xa0;
  1249. if(n != 0)
  1250. c[3] = 1; /* dma */
  1251. else
  1252. c[3] = 0; /* features (exp); */
  1253. c[4] = 0; /* sector lba low 7:0 */
  1254. c[5] = n; /* cylinder low lba mid 15:8 */
  1255. c[6] = n >> 8; /* cylinder hi lba hi 23:16 */
  1256. c[7] = 0xa0; /* obsolete device bits */
  1257. *(ulong*)(c + 8) = 0;
  1258. *(ulong*)(c + 12) = 0;
  1259. *(ulong*)(c + 16) = 0;
  1260. l->flags = 1<<16 | Lpref | Latapi | 0x5;
  1261. if(r->write != 0 && data)
  1262. l->flags |= Lwrite;
  1263. l->len = 0;
  1264. l->ctab = PCIWADDR(t);
  1265. l->ctabhi = 0;
  1266. if(data == 0)
  1267. return l;
  1268. p = &t->prdt;
  1269. p->dba = PCIWADDR(data);
  1270. p->dbahi = 0;
  1271. p->count = 1<<31 | (n - 2) | 1;
  1272. return l;
  1273. }
  1274. static int
  1275. waitready(Drive *d)
  1276. {
  1277. u32int s, t, i;
  1278. for(i = 0; i < 120; i++){
  1279. ilock(d);
  1280. s = d->port->sstatus;
  1281. t = d->port->task;
  1282. iunlock(d);
  1283. if((s & 0x100) == 0)
  1284. return -1;
  1285. if(d->state == Dready && (s & 7) == 3)
  1286. return 0;
  1287. if((i+1) % 30 == 0)
  1288. print("%s: waitready: [%s] task=%ux sstat=%ux\n",
  1289. d->unit->name, diskstates[d->state], t, s);
  1290. esleep(1000);
  1291. }
  1292. print("%s: not responding; offline\n", d->unit->name);
  1293. ilock(d);
  1294. d->state = Doffline;
  1295. iunlock(d);
  1296. return -1;
  1297. }
  1298. static int
  1299. iariopkt(SDreq *r, Drive *d)
  1300. {
  1301. int n, count, try, max, flag, task;
  1302. char *name;
  1303. uchar *cmd, *data;
  1304. Aport *p;
  1305. Asleep as;
  1306. cmd = r->cmd;
  1307. name = d->unit->name;
  1308. p = d->port;
  1309. aprint("%02ux %02ux %c %d %p\n", cmd[0], cmd[2], "rw"[r->write],
  1310. r->dlen, r->data);
  1311. if(cmd[0] == 0x5a && (cmd[2] & 0x3f) == 0x3f)
  1312. return sdmodesense(r, cmd, d->info, sizeof d->info);
  1313. r->rlen = 0;
  1314. count = r->dlen;
  1315. max = 65536;
  1316. try = 0;
  1317. retry:
  1318. if(waitready(d) == -1)
  1319. return SDeio;
  1320. data = r->data;
  1321. n = count;
  1322. if(n > max)
  1323. n = max;
  1324. d->active++;
  1325. ahcibuildpkt(&d->portm, r, data, n);
  1326. ilock(d);
  1327. d->portm.flag = 0;
  1328. iunlock(d);
  1329. p->ci = 1;
  1330. as.p = p;
  1331. as.i = 1;
  1332. d->intick = MACHP(0)->ticks;
  1333. while(waserror())
  1334. ;
  1335. sleep(&d->portm, ahciclear, &as);
  1336. poperror();
  1337. ilock(d);
  1338. flag = d->portm.flag;
  1339. task = d->port->task;
  1340. iunlock(d);
  1341. if(task & (Efatal<<8) || task & (ASbsy|ASdrq) && d->state == Dready){
  1342. d->port->ci = 0; /* @? */
  1343. ahcirecover(&d->portc);
  1344. task = d->port->task;
  1345. }
  1346. d->active--;
  1347. qunlock(&d->portm);
  1348. if(flag == 0){
  1349. if(++try == 10){
  1350. print("%s: bad disk\n", name);
  1351. r->status = SDcheck;
  1352. return SDcheck;
  1353. }
  1354. iprint("%s: retry\n", name);
  1355. esleep(1000);
  1356. goto retry;
  1357. }
  1358. if(flag & Ferror){
  1359. iprint("%s: i/o error %ux\n", name, task);
  1360. r->status = SDcheck;
  1361. return SDcheck;
  1362. }
  1363. data += n;
  1364. r->rlen = data - (uchar*)r->data;
  1365. r->status = SDok;
  1366. return SDok;
  1367. }
  1368. static int
  1369. iario(SDreq *r)
  1370. {
  1371. int i, n, count, try, max, flag, task;
  1372. vlong lba;
  1373. char *name;
  1374. uchar *cmd, *data;
  1375. Aport *p;
  1376. Asleep as;
  1377. Ctlr *c;
  1378. Drive *d;
  1379. SDunit *unit;
  1380. unit = r->unit;
  1381. c = unit->dev->ctlr;
  1382. d = c->drive[unit->subno];
  1383. if(d->portm.feat & Datapi)
  1384. return iariopkt(r, d);
  1385. cmd = r->cmd;
  1386. name = d->unit->name;
  1387. p = d->port;
  1388. if(r->cmd[0] == 0x35 || r->cmd[0] == 0x91){
  1389. qlock(&d->portm);
  1390. i = flushcache(&d->portc);
  1391. qunlock(&d->portm);
  1392. if(i == 0)
  1393. return sdsetsense(r, SDok, 0, 0, 0);
  1394. return sdsetsense(r, SDcheck, 3, 0xc, 2);
  1395. }
  1396. if((i = sdfakescsi(r, d->info, sizeof d->info)) != SDnostatus){
  1397. r->status = i;
  1398. return i;
  1399. }
  1400. if(*cmd != 0x28 && *cmd != 0x2a){
  1401. print("%s: bad cmd 0x%.2ux\n", name, cmd[0]);
  1402. r->status = SDcheck;
  1403. return SDcheck;
  1404. }
  1405. lba = cmd[2]<<24 | cmd[3]<<16 | cmd[4]<<8 | cmd[5];
  1406. count = cmd[7]<<8 | cmd[8];
  1407. if(r->data == nil)
  1408. return SDok;
  1409. if(r->dlen < count * unit->secsize)
  1410. count = r->dlen / unit->secsize;
  1411. max = 128;
  1412. try = 0;
  1413. retry:
  1414. if(waitready(d) == -1)
  1415. return SDeio;
  1416. data = r->data;
  1417. while(count > 0){
  1418. n = count;
  1419. if(n > max)
  1420. n = max;
  1421. d->active++;
  1422. ahcibuild(&d->portm, cmd, data, n, lba);
  1423. ilock(d);
  1424. d->portm.flag = 0;
  1425. iunlock(d);
  1426. p->ci = 1;
  1427. as.p = p;
  1428. as.i = 1;
  1429. d->intick = MACHP(0)->ticks;
  1430. while(waserror())
  1431. ;
  1432. sleep(&d->portm, ahciclear, &as);
  1433. poperror();
  1434. ilock(d);
  1435. flag = d->portm.flag;
  1436. task = d->port->task;
  1437. iunlock(d);
  1438. if(task & (Efatal<<8) ||
  1439. task & (ASbsy|ASdrq) && d->state == Dready){
  1440. d->port->ci = 0; /* @? */
  1441. ahcirecover(&d->portc);
  1442. task = d->port->task;
  1443. }
  1444. d->active--;
  1445. qunlock(&d->portm);
  1446. if(flag == 0){
  1447. if(++try == 10){
  1448. print("%s: bad disk\n", name);
  1449. r->status = SDeio;
  1450. return SDeio;
  1451. }
  1452. iprint("%s: retry %lld\n", name, lba);
  1453. esleep(1000);
  1454. goto retry;
  1455. }
  1456. if(flag & Ferror){
  1457. iprint("%s: i/o error %ux @%,lld\n", name, task, lba);
  1458. r->status = SDeio;
  1459. return SDeio;
  1460. }
  1461. count -= n;
  1462. lba += n;
  1463. data += n * unit->secsize;
  1464. }
  1465. r->rlen = data - (uchar*)r->data;
  1466. r->status = SDok;
  1467. return SDok;
  1468. }
  1469. /*
  1470. * configure drives 0-5 as ahci sata (c.f. errata)
  1471. */
  1472. static int
  1473. iaahcimode(Pcidev *p)
  1474. {
  1475. dprint("iaahcimode %ux %ux %ux\n", pcicfgr8(p, 0x91), pcicfgr8(p, 92),
  1476. pcicfgr8(p, 93));
  1477. pcicfgw16(p, 0x92, pcicfgr32(p, 0x92) | 0xf); /* ports 0-3 */
  1478. // pcicfgw8(p, 0x93, pcicfgr32(p, 9x93) | 3); /* ports 4-5 */
  1479. return 0;
  1480. }
  1481. static void
  1482. iasetupahci(Ctlr *c)
  1483. {
  1484. /* disable cmd block decoding. */
  1485. pcicfgw16(c->pci, 0x40, pcicfgr16(c->pci, 0x40) & ~(1<<15));
  1486. pcicfgw16(c->pci, 0x42, pcicfgr16(c->pci, 0x42) & ~(1<<15));
  1487. c->lmmio[0x4/4] |= 1<<31; /* enable ahci mode (ghc register) */
  1488. c->lmmio[0xc/4] = (1<<6) - 1; /* 5 ports. (supposedly ro pi reg.) */
  1489. /* enable ahci mode. */
  1490. // pcicfgw8(c->pci, 0x90, 0x40);
  1491. // pcicfgw16(c->pci, 0x90, 1<<6 | 1<<5); /* pedantically proper for ich9 */
  1492. pcicfgw8(c->pci, 0x90, 1<<6 | 1<<5); /* pedantically proper for ich9 */
  1493. }
  1494. static SDev*
  1495. iapnp(void)
  1496. {
  1497. int i, n, nunit;
  1498. ulong io;
  1499. Ctlr *c;
  1500. Drive *d;
  1501. Pcidev *p;
  1502. SDev *head, *tail, *s;
  1503. static int done;
  1504. if(done++)
  1505. return nil;
  1506. p = nil;
  1507. head = tail = nil;
  1508. loop:
  1509. while((p = pcimatch(p, 0x8086, 0)) != nil){
  1510. if((p->did & 0xfffc) != 0x2680 && (p->did & 0xfffe) != 0x27c4)
  1511. continue; /* !esb && !82801g[bh]m */
  1512. if(niactlr == NCtlr){
  1513. print("iapnp: too many controllers\n");
  1514. break;
  1515. }
  1516. c = iactlr + niactlr;
  1517. s = sdevs + niactlr;
  1518. memset(c, 0, sizeof *c);
  1519. memset(s, 0, sizeof *s);
  1520. io = p->mem[Abar].bar & ~0xf;
  1521. c->mmio = vmap(io, p->mem[0].size);
  1522. if(c->mmio == 0){
  1523. print("iapnp: address 0x%luX in use did=%x\n",
  1524. io, p->did);
  1525. continue;
  1526. }
  1527. c->lmmio = (ulong*)c->mmio;
  1528. c->pci = p;
  1529. if(p->did != 0x2681)
  1530. iasetupahci(c);
  1531. nunit = ahciconf(c);
  1532. // ahcihbareset((Ahba*)c->mmio);
  1533. if(iaahcimode(p) == -1)
  1534. break;
  1535. if(nunit < 1){
  1536. vunmap(c->mmio, p->mem[0].size);
  1537. continue;
  1538. }
  1539. i = (c->hba->cap>>21) & 1;
  1540. print("intel 63[12]xesb: sata-%s ports with %d ports\n",
  1541. "I\0II" + i*2, nunit);
  1542. s->ifc = &sd63xxesbifc;
  1543. s->ctlr = c;
  1544. s->nunit = nunit;
  1545. s->idno = 'E';
  1546. c->sdev = s;
  1547. c->irq = p->intl;
  1548. c->tbdf = p->tbdf;
  1549. c->ndrive = nunit;
  1550. /* map the drives -- they don't all need to be enabled. */
  1551. memset(c->rawdrive, 0, sizeof c->rawdrive);
  1552. n = 0;
  1553. for(i = 0; i < NCtlrdrv; i++) {
  1554. d = c->rawdrive + i;
  1555. d->portno = i;
  1556. d->driveno = -1;
  1557. d->sectors = 0;
  1558. d->ctlr = c;
  1559. if((c->hba->pi & (1<<i)) == 0)
  1560. continue;
  1561. d->port = (Aport*)(c->mmio + 0x80*i + 0x100);
  1562. d->portc.p = d->port;
  1563. d->portc.m = &d->portm;
  1564. d->driveno = n++;
  1565. c->drive[i] = d;
  1566. iadrive[d->driveno] = d;
  1567. }
  1568. for(i = 0; i < n; i++)
  1569. if(ahciidle(c->drive[i]->port) == -1){
  1570. dprint("intel 63[12]xesb: port %d wedged; abort\n", i);
  1571. goto loop;
  1572. }
  1573. for(i = 0; i < n; i++){
  1574. c->drive[i]->mode = DMsatai;
  1575. configdrive(c->drive[i]);
  1576. }
  1577. niadrive += nunit;
  1578. if(head)
  1579. tail->next = s;
  1580. else
  1581. head = s;
  1582. tail = s;
  1583. }
  1584. return head;
  1585. }
  1586. static char* smarttab[] = {
  1587. "unset",
  1588. "error",
  1589. "threshold exceeded",
  1590. "normal"
  1591. };
  1592. static char *
  1593. pflag(char *s, char *e, uchar f)
  1594. {
  1595. uchar i, m;
  1596. for(i = 0; i < 8; i++){
  1597. m = 1 << i;
  1598. if(f&m)
  1599. s = seprint(s, e, "%s ", flagname[i]);
  1600. }
  1601. return seprint(s, e, "\n");
  1602. }
  1603. static int
  1604. iarctl(SDunit *u, char *p, int l)
  1605. {
  1606. char buf[32];
  1607. char *e, *op;
  1608. Aport *o;
  1609. Ctlr *c;
  1610. Drive *d;
  1611. if((c = u->dev->ctlr) == nil)
  1612. return 0;
  1613. d = c->drive[u->subno];
  1614. o = d->port;
  1615. e = p+l;
  1616. op = p;
  1617. if(d->state == Dready){
  1618. p = seprint(p, e, "model\t%s\n", d->model);
  1619. p = seprint(p, e, "serial\t%s\n", d->serial);
  1620. p = seprint(p, e, "firm\t%s\n", d->firmware);
  1621. if(d->smartrs == 0xff)
  1622. p = seprint(p, e, "smart\tenable error\n");
  1623. else if(d->smartrs == 0)
  1624. p = seprint(p, e, "smart\tdisabled\n");
  1625. else
  1626. p = seprint(p, e, "smart\t%s\n",
  1627. smarttab[d->portm.smart]);
  1628. p = seprint(p, e, "flag\t");
  1629. p = pflag(p, e, d->portm.feat);
  1630. }else
  1631. p = seprint(p, e, "no disk present [%s]\n", diskstates[d->state]);
  1632. serrstr(o->serror, buf, buf + sizeof buf - 1);
  1633. p = seprint(p, e, "reg\ttask %ux cmd %ux serr %ux %s ci %ux is %ux; "
  1634. "sig %ux sstatus %04x\n", o->task, o->cmd, o->serror, buf,
  1635. o->ci, o->isr, o->sig, o->sstatus);
  1636. p = seprint(p, e, "geometry %llud 512\n", d->sectors);
  1637. return p - op;
  1638. }
  1639. static void
  1640. runflushcache(Drive *d)
  1641. {
  1642. long t0;
  1643. t0 = MACHP(0)->ticks;
  1644. qlock(&d->portm);
  1645. flushcache(&d->portc);
  1646. qunlock(&d->portm);
  1647. dprint("flush in %ldms\n", TK2MS(MACHP(0)->ticks-t0));
  1648. }
  1649. static void
  1650. forcemode(Drive *d, char *mode)
  1651. {
  1652. int i;
  1653. for(i = 0; i < nelem(modename); i++)
  1654. if(strcmp(mode, modename[i]) == 0)
  1655. break;
  1656. if(i == nelem(modename))
  1657. i = 0;
  1658. ilock(d);
  1659. d->mode = i;
  1660. iunlock(d);
  1661. }
  1662. static void
  1663. runsmartable(Drive *d, int i)
  1664. {
  1665. if(waserror()){
  1666. qunlock(&d->portm);
  1667. d->smartrs = 0;
  1668. nexterror();
  1669. }
  1670. qlock(&d->portm);
  1671. d->smartrs = smart(&d->portc, i);
  1672. d->portm.smart = 0;
  1673. qunlock(&d->portm);
  1674. poperror();
  1675. }
  1676. static void
  1677. forcestate(Drive *d, char *state)
  1678. {
  1679. int i;
  1680. for(i = 0; i < nelem(diskstates); i++)
  1681. if(strcmp(state, diskstates[i]) == 0)
  1682. break;
  1683. if(i == nelem(diskstates))
  1684. i = 0;
  1685. ilock(d);
  1686. d->state = i;
  1687. if(i == Dnull){
  1688. d->mediachange = 1;
  1689. d->unit->sectors = 0; /* force disk to disappear. */
  1690. }
  1691. iunlock(d);
  1692. }
  1693. static int
  1694. iawctl(SDunit *u, Cmdbuf *cmd)
  1695. {
  1696. char **f;
  1697. Ctlr *c;
  1698. Drive *d;
  1699. c = u->dev->ctlr;
  1700. d = c->drive[u->subno];
  1701. f = cmd->f;
  1702. if(strcmp(f[0], "flushcache") == 0)
  1703. runflushcache(d);
  1704. else if(strcmp(f[0], "identify") == 0){
  1705. uint i;
  1706. i = strtoul(f[1]? f[1]: "0", 0, 0);
  1707. if(i > 0xff)
  1708. i = 0;
  1709. dprint("%04d %ux\n", i, d->info[i]);
  1710. }else if(strcmp(f[0], "mode") == 0)
  1711. forcemode(d, f[1]? f[1]: "satai");
  1712. else if(strcmp(f[0], "nop") == 0){
  1713. if((d->portm.feat & Dnop) == 0){
  1714. cmderror(cmd, "nop command not supported");
  1715. return -1;
  1716. }
  1717. if(waserror()){
  1718. qunlock(&d->portm);
  1719. nexterror();
  1720. }
  1721. qlock(&d->portm);
  1722. nop(&d->portc);
  1723. qunlock(&d->portm);
  1724. poperror();
  1725. }else if(strcmp(f[0], "reset") == 0)
  1726. forcestate(d, "reset");
  1727. else if(strcmp(f[0], "smart") == 0){
  1728. if(d->smartrs == 0){
  1729. cmderror(cmd, "smart not enabled");
  1730. return -1;
  1731. }
  1732. if(waserror()){
  1733. qunlock(&d->portm);
  1734. d->smartrs = 0;
  1735. nexterror();
  1736. }
  1737. qlock(&d->portm);
  1738. d->portm.smart = 2 + smartrs(&d->portc);
  1739. qunlock(&d->portm);
  1740. poperror();
  1741. }else if(strcmp(f[0], "smartdisable") == 0)
  1742. runsmartable(d, 1);
  1743. else if(strcmp(f[0], "smartenable") == 0)
  1744. runsmartable(d, 0);
  1745. else if(strcmp(f[0], "state") == 0)
  1746. forcestate(d, f[1]? f[1]: "null");
  1747. else{
  1748. cmderror(cmd, Ebadctl);
  1749. return -1;
  1750. }
  1751. return 0;
  1752. }
  1753. static char *
  1754. portr(char *p, char *e, uint x)
  1755. {
  1756. int i, a;
  1757. p[0] = 0;
  1758. a = -1;
  1759. for(i = 0; i < 32; i++){
  1760. if((x & (1<<i)) == 0){
  1761. if(a != -1 && i - 1 != a)
  1762. p = seprint(p, e, "-%d", i - 1);
  1763. a = -1;
  1764. continue;
  1765. }
  1766. if(a == -1){
  1767. if(i > 0)
  1768. p = seprint(p, e, ", ");
  1769. p = seprint(p, e, "%d", a = i);
  1770. }
  1771. }
  1772. if(a != -1 && i - 1 != a)
  1773. p = seprint(p, e, "-%d", i - 1);
  1774. return p;
  1775. }
  1776. #define has(x, s) if(u & (x)) p = seprint(p, e, (s))
  1777. static char*
  1778. iartopctl(SDev *s, char *p, char *e)
  1779. {
  1780. u32int u;
  1781. char name[10], pr[25];
  1782. Ahba *h;
  1783. Ctlr *c;
  1784. c = s->ctlr;
  1785. snprint(name, sizeof name, "sd%c", s->idno);
  1786. p = seprint(p, e, "%s ahci ", name);
  1787. u = c->hba->cap;
  1788. has(Hs64a, "64a ");
  1789. has(Hsncq, "ncq ");
  1790. has(Hssntf, "ntf ");
  1791. has(Hsmps, "mps ");
  1792. has(Hsss, "ss ");
  1793. has(Hsalp, "alp ");
  1794. has(Hsal, "led ");
  1795. has(Hsclo, "clo ");
  1796. has(Hsam, "am ");
  1797. has(Hspm, "pm ");
  1798. has(Hssc, "slum ");
  1799. has(Hpsc, "pslum ");
  1800. has(Hcccs, "coal ");
  1801. has(Hems, "ems ");
  1802. has(Hsxs, "sxs ");
  1803. p = seprint(p, e, "\n");
  1804. p = seprint(p, e, "%s iss %d ncs %d np %d\n", name, (u>>20) & 0xf,
  1805. (u>>8) & 0x1f, 1 + (u & 0x1f));
  1806. h = c->hba;
  1807. portr(pr, pr + sizeof pr, h->pi);
  1808. p = seprint(p, e, "%s ghc %ux isr %ux pi %ux %s ver %ux\n",
  1809. name, h->ghc, h->isr, h->pi, pr, h->ver);
  1810. return p;
  1811. }
  1812. static int
  1813. iawtopctl(SDev *, Cmdbuf *cmd)
  1814. {
  1815. int *v;
  1816. char **f;
  1817. f = cmd->f;
  1818. v = 0;
  1819. if(strcmp(f[0], "debug") == 0)
  1820. v = &debug;
  1821. else if(strcmp(f[0], "idprint") == 0)
  1822. v = &prid;
  1823. else if(strcmp(f[0], "aprint") == 0)
  1824. v = &datapi;
  1825. else
  1826. cmderror(cmd, Ebadctl);
  1827. switch(cmd->nf){
  1828. default:
  1829. cmderror(cmd, Ebadarg);
  1830. case 1:
  1831. *v ^= 1;
  1832. break;
  1833. case 2:
  1834. if(strcmp(f[1], "on") == 0)
  1835. *v = 1;
  1836. else
  1837. *v = 0;
  1838. break;
  1839. }
  1840. return 0;
  1841. }
  1842. SDifc sd63xxesbifc = {
  1843. "iahci",
  1844. iapnp,
  1845. nil, /* legacy */
  1846. iaenable,
  1847. iadisable,
  1848. iaverify,
  1849. iaonline,
  1850. iario,
  1851. iarctl,
  1852. iawctl,
  1853. scsibio,
  1854. nil, /* probe */
  1855. nil, /* clear */
  1856. iartopctl,
  1857. iawtopctl,
  1858. };