sd63xxesb.c 35 KB

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