sdiahci.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * ahci serial ata driver
  11. * copyright © 2007-8 coraid, inc.
  12. */
  13. #include "u.h"
  14. #include "../port/lib.h"
  15. #include "mem.h"
  16. #include "dat.h"
  17. #include "fns.h"
  18. #include "io.h"
  19. #include "../port/error.h"
  20. #include "../port/sd.h"
  21. #include "ahci.h"
  22. #include "atomic.h"
  23. enum {
  24. Vatiamd = 0x1002,
  25. Vintel = 0x8086,
  26. Vmarvell= 0x1b4b,
  27. };
  28. #define dprint(...) do if(debug) iprint(__VA_ARGS__); while(0)
  29. #define idprint(...) do if(prid) iprint(__VA_ARGS__); while(0)
  30. #define aprint(...) do if(datapi) iprint(__VA_ARGS__); while(0)
  31. #define Tname(c) tname[(c)->type]
  32. #define Intel(x) ((x)->pci->vid == Vintel)
  33. enum {
  34. NCtlr = 16,
  35. NCtlrdrv= 32,
  36. NDrive = NCtlr*NCtlrdrv,
  37. Read = 0,
  38. Write,
  39. Nms = 256, /* ms. between drive checks */
  40. Mphywait= 2*1024/Nms - 1,
  41. Midwait = 16*1024/Nms - 1,
  42. Mcomrwait= 64*1024/Nms - 1,
  43. Obs = 0xa0, /* obsolete device bits */
  44. /*
  45. * if we get more than this many interrupts per tick for a drive,
  46. * either the hardware is broken or we've got a bug in this driver.
  47. */
  48. Maxintrspertick = 2000, /* was 1000 */
  49. };
  50. /* pci space configuration */
  51. enum {
  52. Pmap = 0x90,
  53. Ppcs = 0x91,
  54. Prev = 0xa8,
  55. };
  56. enum {
  57. Tesb,
  58. Tich,
  59. Tsb600,
  60. Tunk,
  61. };
  62. static char *tname[] = {
  63. "63xxesb",
  64. "ich",
  65. "sb600",
  66. "unknown",
  67. };
  68. enum {
  69. Dnull,
  70. Dmissing,
  71. Dnew,
  72. Dready,
  73. Derror,
  74. Dreset,
  75. Doffline,
  76. Dportreset,
  77. Dlast,
  78. };
  79. static char *diskstates[Dlast] = {
  80. "null",
  81. "missing",
  82. "new",
  83. "ready",
  84. "error",
  85. "reset",
  86. "offline",
  87. "portreset",
  88. };
  89. enum {
  90. DMautoneg,
  91. DMsatai,
  92. DMsataii,
  93. DMsata3,
  94. };
  95. static char *modename[] = { /* used in control messages */
  96. "auto",
  97. "satai",
  98. "sataii",
  99. "sata3",
  100. };
  101. static char *descmode[] = { /* only printed */
  102. "auto",
  103. "sata 1",
  104. "sata 2",
  105. "sata 3",
  106. };
  107. static char *flagname[] = {
  108. "llba",
  109. "smart",
  110. "power",
  111. "nop",
  112. "atapi",
  113. "atapi16",
  114. };
  115. typedef struct Asleep Asleep;
  116. typedef struct Ctlr Ctlr;
  117. typedef struct Drive Drive;
  118. struct Drive {
  119. Lock Lock;
  120. Ctlr *ctlr;
  121. SDunit *unit;
  122. char name[10];
  123. Aport *port;
  124. Aportm portm;
  125. Aportc portc; /* redundant ptr to port and portm */
  126. unsigned char mediachange;
  127. unsigned char state;
  128. unsigned char smartrs;
  129. uint64_t sectors;
  130. uint32_t secsize;
  131. uint32_t intick; /* start tick of current transfer */
  132. uint32_t lastseen;
  133. int wait;
  134. unsigned char mode; /* DMautoneg, satai or sataii */
  135. unsigned char active;
  136. char serial[20+1];
  137. char firmware[8+1];
  138. char model[40+1];
  139. int infosz;
  140. uint16_t *info;
  141. uint16_t tinyinfo[2]; /* used iff malloc fails */
  142. int driveno; /* ctlr*NCtlrdrv + unit */
  143. /* controller port # != driveno when not all ports are enabled */
  144. int portno;
  145. uint32_t lastintr0;
  146. uint32_t intrs;
  147. };
  148. struct Ctlr {
  149. Lock Lock;
  150. int type;
  151. int enabled;
  152. SDev *sdev;
  153. Pcidev *pci;
  154. void* vector;
  155. /* virtual register addresses */
  156. unsigned char *mmio;
  157. uint32_t *lmmio;
  158. Ahba *hba;
  159. /* phyical register address */
  160. unsigned char *physio;
  161. Drive *rawdrive;
  162. Drive *drive[NCtlrdrv];
  163. int ndrive;
  164. int mport; /* highest drive # (0-origin) on ich9 at least */
  165. uint32_t lastintr0;
  166. uint32_t intrs; /* not attributable to any drive */
  167. };
  168. struct Asleep {
  169. Aport *p;
  170. int i;
  171. int slept;
  172. };
  173. extern SDifc sdiahciifc;
  174. static Ctlr iactlr[NCtlr];
  175. static SDev sdevs[NCtlr];
  176. static int niactlr;
  177. static Drive *iadrive[NDrive];
  178. static int niadrive;
  179. /* these are fiddled in iawtopctl() */
  180. static int debug;
  181. static int prid = 1;
  182. static int datapi;
  183. // TODO: does this get initialized correctly?
  184. static char stab[] = {
  185. [0] = 'i', 'm',
  186. [8] = 't', 'c', 'p', 'e',
  187. [16] = 'N', 'I', 'W', 'B', 'D', 'C', 'H', 'S', 'T', 'F', 'X'
  188. };
  189. static void
  190. serrstr(uint32_t r, char *s, char *e)
  191. {
  192. int i;
  193. e -= 3;
  194. for(i = 0; i < nelem(stab) && s < e; i++)
  195. if(r & (1<<i) && stab[i]){
  196. *s++ = stab[i];
  197. if(SerrBad & (1<<i))
  198. *s++ = '*';
  199. }
  200. *s = 0;
  201. }
  202. static char ntab[] = "0123456789abcdef";
  203. static void
  204. preg(unsigned char *reg, int n)
  205. {
  206. int i;
  207. char buf[25*3+1], *e;
  208. e = buf;
  209. for(i = 0; i < n; i++){
  210. *e++ = ntab[reg[i]>>4];
  211. *e++ = ntab[reg[i]&0xf];
  212. *e++ = ' ';
  213. }
  214. *e++ = '\n';
  215. *e = 0;
  216. dprint(buf);
  217. }
  218. static void
  219. dreg(char *s, Aport *p)
  220. {
  221. dprint("ahci: %stask=%#lx; cmd=%#lx; ci=%#lx; is=%#lx\n",
  222. s, p->task, p->cmd, p->ci, p->isr);
  223. }
  224. static void
  225. esleep(int ms)
  226. {
  227. Proc *up = externup();
  228. if(waserror())
  229. return;
  230. tsleep(&up->sleep, return0, 0, ms);
  231. poperror();
  232. }
  233. static int
  234. ahciclear(void *v)
  235. {
  236. Asleep *s = v;
  237. if (!s->slept) {
  238. s->slept = 1;
  239. return 0;
  240. }
  241. return (s->p->ci & s->i) == 0;
  242. }
  243. static void
  244. aesleep(Aportm *pm, Asleep *a, int ms)
  245. {
  246. Proc *up = externup();
  247. if(waserror())
  248. return;
  249. tsleep(&pm->Rendez, ahciclear, a, ms);
  250. poperror();
  251. }
  252. static int
  253. ahciwait(Aportc *c, int ms)
  254. {
  255. Asleep as;
  256. Aport *p;
  257. p = c->p;
  258. p->ci = 1;
  259. as.p = p;
  260. as.i = 1;
  261. aesleep(c->pm, &as, ms);
  262. if((p->task&1) == 0 && p->ci == 0)
  263. return 0;
  264. dreg("ahciwait timeout ", c->p);
  265. return -1;
  266. }
  267. /* fill in cfis boilerplate */
  268. static unsigned char *
  269. cfissetup(Aportc *pc)
  270. {
  271. unsigned char *cfis;
  272. cfis = pc->pm->ctab->cfis;
  273. memset(cfis, 0, 0x20);
  274. cfis[0] = 0x27;
  275. cfis[1] = 0x80;
  276. cfis[7] = Obs;
  277. return cfis;
  278. }
  279. /* initialise pc's list */
  280. static void
  281. listsetup(Aportc *pc, int flags)
  282. {
  283. Alist *list;
  284. list = pc->pm->list;
  285. list->flags = flags | 5;
  286. list->len = 0;
  287. list->ctab = PCIWADDR(pc->pm->ctab);
  288. list->ctabhi = PCIWADDR(pc->pm->ctab)>>32;
  289. }
  290. static int
  291. nop(Aportc *pc)
  292. {
  293. unsigned char *c;
  294. if((pc->pm->feat & Dnop) == 0)
  295. return -1;
  296. c = cfissetup(pc);
  297. c[2] = 0;
  298. listsetup(pc, Lwrite);
  299. return ahciwait(pc, 3*1000);
  300. }
  301. static int
  302. setfeatures(Aportc *pc, unsigned char f)
  303. {
  304. unsigned char *c;
  305. c = cfissetup(pc);
  306. c[2] = 0xef;
  307. c[3] = f;
  308. listsetup(pc, Lwrite);
  309. return ahciwait(pc, 3*1000);
  310. }
  311. static int
  312. setudmamode(Aportc *pc, unsigned char f)
  313. {
  314. unsigned char *c;
  315. /* hack */
  316. if((pc->p->sig >> 16) == 0xeb14)
  317. return 0;
  318. c = cfissetup(pc);
  319. c[2] = 0xef;
  320. c[3] = 3; /* set transfer mode */
  321. c[12] = 0x40 | f; /* sector count */
  322. listsetup(pc, Lwrite);
  323. return ahciwait(pc, 3*1000);
  324. }
  325. static void
  326. asleep(int ms)
  327. {
  328. Proc *up = externup();
  329. if(up == nil)
  330. delay(ms);
  331. else
  332. esleep(ms);
  333. }
  334. static int
  335. ahciportreset(Aportc *c)
  336. {
  337. uint32_t *cmd, i;
  338. Aport *p;
  339. p = c->p;
  340. cmd = &p->cmd;
  341. *cmd &= ~(Afre|Ast);
  342. for(i = 0; i < 500; i += 25){
  343. if((*cmd&Acr) == 0)
  344. break;
  345. asleep(25);
  346. }
  347. p->sctl = 1|(p->sctl&~7);
  348. delay(1);
  349. p->sctl &= ~7;
  350. return 0;
  351. }
  352. static int
  353. smart(Aportc *pc, int n)
  354. {
  355. unsigned char *c;
  356. if((pc->pm->feat&Dsmart) == 0)
  357. return -1;
  358. c = cfissetup(pc);
  359. c[2] = 0xb0;
  360. c[3] = 0xd8 + n; /* able smart */
  361. c[5] = 0x4f;
  362. c[6] = 0xc2;
  363. listsetup(pc, Lwrite);
  364. if(ahciwait(pc, 1000) == -1 || pc->p->task & (1|32)){
  365. dprint("ahci: smart fail %#lx\n", pc->p->task);
  366. // preg(pc->m->fis.r, 20);
  367. return -1;
  368. }
  369. if(n)
  370. return 0;
  371. return 1;
  372. }
  373. static int
  374. smartrs(Aportc *pc)
  375. {
  376. unsigned char *c;
  377. c = cfissetup(pc);
  378. c[2] = 0xb0;
  379. c[3] = 0xda; /* return smart status */
  380. c[5] = 0x4f;
  381. c[6] = 0xc2;
  382. listsetup(pc, Lwrite);
  383. c = pc->pm->fis.r;
  384. if(ahciwait(pc, 1000) == -1 || pc->p->task & (1|32)){
  385. dprint("ahci: smart fail %#lx\n", pc->p->task);
  386. preg(c, 20);
  387. return -1;
  388. }
  389. if(c[5] == 0x4f && c[6] == 0xc2)
  390. return 1;
  391. return 0;
  392. }
  393. static int
  394. ahciflushcache(Aportc *pc)
  395. {
  396. unsigned char *c;
  397. c = cfissetup(pc);
  398. c[2] = pc->pm->feat & Dllba? 0xea: 0xe7;
  399. listsetup(pc, Lwrite);
  400. if(ahciwait(pc, 60000) == -1 || pc->p->task & (1|32)){
  401. dprint("ahciflushcache: fail %#lx\n", pc->p->task);
  402. // preg(pc->m->fis.r, 20);
  403. return -1;
  404. }
  405. return 0;
  406. }
  407. static uint16_t
  408. gbit16(void *a)
  409. {
  410. unsigned char *i;
  411. i = a;
  412. return i[1]<<8 | i[0];
  413. }
  414. static uint32_t
  415. gbit32(void *a)
  416. {
  417. uint32_t j;
  418. unsigned char *i;
  419. i = a;
  420. j = i[3] << 24;
  421. j |= i[2] << 16;
  422. j |= i[1] << 8;
  423. j |= i[0];
  424. return j;
  425. }
  426. static uint64_t
  427. gbit64(void *a)
  428. {
  429. unsigned char *i;
  430. i = a;
  431. return (uint64_t)gbit32(i+4) << 32 | gbit32(a);
  432. }
  433. static int
  434. ahciidentify0(Aportc *pc, void *id, int atapi)
  435. {
  436. unsigned char *c;
  437. Aprdt *p;
  438. static unsigned char tab[] = { 0xec, 0xa1, };
  439. c = cfissetup(pc);
  440. c[2] = tab[atapi];
  441. listsetup(pc, 1<<16);
  442. memset(id, 0, 0x100); /* magic */
  443. p = &pc->pm->ctab->prdt;
  444. p->dba = PCIWADDR(id);
  445. p->dbahi = PCIWADDR(id)>>32;
  446. p->count = 1<<31 | (0x200-2) | 1;
  447. return ahciwait(pc, 3*1000);
  448. }
  449. static int64_t
  450. ahciidentify(Aportc *pc, uint16_t *id)
  451. {
  452. int i, sig;
  453. int64_t s;
  454. Aportm *pm;
  455. pm = pc->pm;
  456. pm->feat = 0;
  457. pm->smart = 0;
  458. i = 0;
  459. sig = pc->p->sig >> 16;
  460. if(sig == 0xeb14){
  461. pm->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. pm->feat |= Dllba;
  469. s = gbit64(id+100);
  470. }else
  471. s = gbit32(id+60);
  472. if(pm->feat&Datapi){
  473. i = gbit16(id+0);
  474. if(i&1)
  475. pm->feat |= Datapi16;
  476. }
  477. i = gbit16(id+83);
  478. if((i>>14) == 1) {
  479. if(i & (1<<3))
  480. pm->feat |= Dpower;
  481. i = gbit16(id+82);
  482. if(i & 1)
  483. pm->feat |= Dsmart;
  484. if(i & (1<<14))
  485. pm->feat |= Dnop;
  486. }
  487. return s;
  488. }
  489. #if 0
  490. static int
  491. ahciquiet(Aport *a)
  492. {
  493. uint32_t *p, i;
  494. p = &a->cmd;
  495. *p &= ~Ast;
  496. for(i = 0; i < 500; i += 50){
  497. if((*p & Acr) == 0)
  498. goto stop;
  499. asleep(50);
  500. }
  501. return -1;
  502. stop:
  503. if((a->task & (ASdrq|ASbsy)) == 0){
  504. *p |= Ast;
  505. return 0;
  506. }
  507. *p |= Aclo;
  508. for(i = 0; i < 500; i += 50){
  509. if((*p & Aclo) == 0)
  510. goto stop1;
  511. asleep(50);
  512. }
  513. return -1;
  514. stop1:
  515. /* extra check */
  516. dprint("ahci: clo clear %#lx\n", a->task);
  517. if(a->task & ASbsy)
  518. return -1;
  519. *p |= Ast;
  520. return 0;
  521. }
  522. #endif
  523. #if 0
  524. static int
  525. ahcicomreset(Aportc *pc)
  526. {
  527. unsigned char *c;
  528. dprint("ahcicomreset\n");
  529. dreg("ahci: comreset ", pc->p);
  530. if(ahciquiet(pc->p) == -1){
  531. dprint("ahciquiet failed\n");
  532. return -1;
  533. }
  534. dreg("comreset ", pc->p);
  535. c = cfissetup(pc);
  536. c[1] = 0;
  537. c[15] = 1<<2; /* srst */
  538. listsetup(pc, Lclear | Lreset);
  539. if(ahciwait(pc, 500) == -1){
  540. dprint("ahcicomreset: first command failed\n");
  541. return -1;
  542. }
  543. microdelay(250);
  544. dreg("comreset ", pc->p);
  545. c = cfissetup(pc);
  546. c[1] = 0;
  547. listsetup(pc, Lwrite);
  548. if(ahciwait(pc, 150) == -1){
  549. dprint("ahcicomreset: second command failed\n");
  550. return -1;
  551. }
  552. dreg("comreset ", pc->p);
  553. return 0;
  554. }
  555. #endif
  556. static int
  557. ahciidle(Aport *port)
  558. {
  559. uint32_t *p, i, r;
  560. p = &port->cmd;
  561. if((*p & Arun) == 0)
  562. return 0;
  563. *p &= ~Ast;
  564. r = 0;
  565. for(i = 0; i < 500; i += 25){
  566. if((*p & Acr) == 0)
  567. goto stop;
  568. asleep(25);
  569. }
  570. r = -1;
  571. stop:
  572. if((*p & Afre) == 0)
  573. return r;
  574. *p &= ~Afre;
  575. for(i = 0; i < 500; i += 25){
  576. if((*p & Afre) == 0)
  577. return 0;
  578. asleep(25);
  579. }
  580. return -1;
  581. }
  582. /*
  583. * § 6.2.2.1 first part; comreset handled by reset disk.
  584. * - remainder is handled by configdisk.
  585. * - ahcirecover is a quick recovery from a failed command.
  586. */
  587. static int
  588. ahciswreset(Aportc *pc)
  589. {
  590. int i;
  591. i = ahciidle(pc->p);
  592. pc->p->cmd |= Afre;
  593. if(i == -1)
  594. return -1;
  595. if(pc->p->task & (ASdrq|ASbsy))
  596. return -1;
  597. return 0;
  598. }
  599. static int
  600. ahcirecover(Aportc *pc)
  601. {
  602. ahciswreset(pc);
  603. pc->p->cmd |= Ast;
  604. if(setudmamode(pc, 5) == -1)
  605. return -1;
  606. return 0;
  607. }
  608. static void*
  609. malign(int size, int align)
  610. {
  611. return mallocalign(size, align, 0, 0);
  612. }
  613. static void
  614. setupfis(Afis *f)
  615. {
  616. f->base = malign(0x100, 0x100); /* magic */
  617. f->d = f->base + 0;
  618. f->p = f->base + 0x20;
  619. f->r = f->base + 0x40;
  620. f->u = f->base + 0x60;
  621. f->devicebits = (uint32_t*)(f->base + 0x58);
  622. }
  623. static void
  624. ahciwakeup(Aport *p)
  625. {
  626. uint16_t s;
  627. s = p->sstatus;
  628. if((s & Intpm) != Intslumber && (s & Intpm) != Intpartpwr)
  629. return;
  630. if((s & Devdet) != Devpresent){ /* not (device, no phy) */
  631. iprint("ahci: slumbering drive unwakable %#x\n", s);
  632. return;
  633. }
  634. p->sctl = 3*Aipm | 0*Aspd | Adet;
  635. delay(1);
  636. p->sctl &= ~7;
  637. // iprint("ahci: wake %#x -> %#x\n", s, p->sstatus);
  638. }
  639. static int
  640. ahciconfigdrive(Drive *d)
  641. {
  642. char *name;
  643. Ahba *h;
  644. Aport *p;
  645. Aportm *pm;
  646. h = d->ctlr->hba;
  647. p = d->portc.p;
  648. pm = d->portc.pm;
  649. if(pm->list == 0){
  650. setupfis(&pm->fis);
  651. pm->list = malign(sizeof *pm->list, 1024);
  652. pm->ctab = malign(sizeof *pm->ctab, 128);
  653. }
  654. if (d->unit)
  655. name = d->unit->SDperm.name;
  656. else
  657. name = nil;
  658. if(p->sstatus & (Devphycomm|Devpresent) && h->cap & Hsss){
  659. /* device connected & staggered spin-up */
  660. dprint("ahci: configdrive: %s: spinning up ... [%#lx]\n",
  661. name, p->sstatus);
  662. p->cmd |= Apod|Asud;
  663. asleep(1400);
  664. }
  665. p->serror = SerrAll;
  666. p->list = PCIWADDR(pm->list);
  667. p->listhi = PCIWADDR(pm->list)>>32;
  668. p->fis = PCIWADDR(pm->fis.base);
  669. p->fishi = PCIWADDR(pm->fis.base)>>32;
  670. p->cmd |= Afre|Ast;
  671. /* drive coming up in slumbering? */
  672. if((p->sstatus & Devdet) == Devpresent &&
  673. ((p->sstatus & Intpm) == Intslumber ||
  674. (p->sstatus & Intpm) == Intpartpwr))
  675. ahciwakeup(p);
  676. /* "disable power managment" sequence from book. */
  677. p->sctl = (3*Aipm) | (d->mode*Aspd) | (0*Adet);
  678. p->cmd &= ~Aalpe;
  679. p->ie = IEM;
  680. return 0;
  681. }
  682. static void
  683. ahcienable(Ahba *h)
  684. {
  685. h->ghc |= Hie;
  686. }
  687. static void
  688. ahcidisable(Ahba *h)
  689. {
  690. h->ghc &= ~Hie;
  691. }
  692. static int
  693. countbits(uint32_t u)
  694. {
  695. int n;
  696. n = 0;
  697. for (; u != 0; u >>= 1)
  698. if(u & 1)
  699. n++;
  700. return n;
  701. }
  702. static int
  703. ahciconf(Ctlr *ctlr)
  704. {
  705. Ahba *h;
  706. uint32_t u;
  707. h = ctlr->hba = (Ahba*)ctlr->mmio;
  708. u = h->cap;
  709. if((u&Hsam) == 0)
  710. h->ghc |= Hae;
  711. dprint("#S/sd%c: type %s port %#p: sss %ld ncs %ld coal %ld "
  712. "%ld ports, led %ld clo %ld ems %ld\n",
  713. ctlr->sdev->idno, tname[ctlr->type], h,
  714. (u>>27) & 1, (u>>8) & 0x1f, (u>>7) & 1,
  715. (u & 0x1f) + 1, (u>>25) & 1, (u>>24) & 1, (u>>6) & 1);
  716. return countbits(h->pi);
  717. }
  718. #if 0
  719. static int
  720. ahcihbareset(Ahba *h)
  721. {
  722. int wait;
  723. h->ghc |= 1;
  724. for(wait = 0; wait < 1000; wait += 100){
  725. if(h->ghc == 0)
  726. return 0;
  727. delay(100);
  728. }
  729. return -1;
  730. }
  731. #endif
  732. static void
  733. idmove(char *p, uint16_t *a, int n)
  734. {
  735. int i;
  736. char *op, *e;
  737. op = p;
  738. for(i = 0; i < n/2; i++){
  739. *p++ = a[i] >> 8;
  740. *p++ = a[i];
  741. }
  742. *p = 0;
  743. while(p > op && *--p == ' ')
  744. *p = 0;
  745. e = p;
  746. for (p = op; *p == ' '; p++)
  747. ;
  748. memmove(op, p, n - (e - p));
  749. }
  750. static int
  751. identify(Drive *d)
  752. {
  753. uint16_t *id;
  754. int64_t osectors, s;
  755. unsigned char oserial[21];
  756. SDunit *u;
  757. if(d->info == nil) {
  758. d->infosz = 512 * sizeof(uint16_t);
  759. d->info = malloc(d->infosz);
  760. }
  761. if(d->info == nil) {
  762. d->info = d->tinyinfo;
  763. d->infosz = sizeof d->tinyinfo;
  764. }
  765. id = d->info;
  766. s = ahciidentify(&d->portc, id);
  767. if(s == -1){
  768. d->state = Derror;
  769. return -1;
  770. }
  771. osectors = d->sectors;
  772. memmove(oserial, d->serial, sizeof d->serial);
  773. u = d->unit;
  774. d->sectors = s;
  775. d->secsize = u->secsize;
  776. if(d->secsize == 0)
  777. d->secsize = 512; /* default */
  778. d->smartrs = 0;
  779. idmove(d->serial, id+10, 20);
  780. idmove(d->firmware, id+23, 8);
  781. idmove(d->model, id+27, 40);
  782. memset(u->inquiry, 0, sizeof u->inquiry);
  783. u->inquiry[2] = 2;
  784. u->inquiry[3] = 2;
  785. u->inquiry[4] = sizeof u->inquiry - 4;
  786. memmove(u->inquiry+8, d->model, 40);
  787. if(osectors != s || memcmp(oserial, d->serial, sizeof oserial) != 0){
  788. d->mediachange = 1;
  789. u->sectors = 0;
  790. }
  791. return 0;
  792. }
  793. static void
  794. clearci(Aport *p)
  795. {
  796. if(p->cmd & Ast) {
  797. p->cmd &= ~Ast;
  798. p->cmd |= Ast;
  799. }
  800. }
  801. static void
  802. updatedrive(Drive *d)
  803. {
  804. uint32_t cause, serr, s0, pr, ewake;
  805. char *name;
  806. Aport *p;
  807. static uint32_t last;
  808. pr = 1;
  809. ewake = 0;
  810. p = d->port;
  811. cause = p->isr;
  812. serr = p->serror;
  813. p->isr = cause;
  814. name = "??";
  815. if(d->unit && d->unit->SDperm.name)
  816. name = d->unit->SDperm.name;
  817. if(p->ci == 0){
  818. atomic_set(&d->portm.flag, atomic_read(&d->portm.flag) | Fdone);
  819. wakeup(&d->portm.Rendez);
  820. pr = 0;
  821. }else if(cause & Adps)
  822. pr = 0;
  823. if(cause & Ifatal){
  824. ewake = 1;
  825. dprint("ahci: updatedrive: %s: fatal\n", name);
  826. }
  827. if(cause & Adhrs){
  828. if(p->task & (1<<5|1)){
  829. dprint("ahci: %s: Adhrs cause %#lx serr %#lx task %#lx\n",
  830. name, cause, serr, p->task);
  831. atomic_set(&d->portm.flag, atomic_read(&d->portm.flag) | Ferror);
  832. ewake = 1;
  833. }
  834. pr = 0;
  835. }
  836. if((p->task & 1) && last != cause)
  837. dprint("%s: err ca %#lx serr %#lx task %#lx sstat %#lx\n",
  838. name, cause, serr, p->task, p->sstatus);
  839. if(pr)
  840. dprint("%s: upd %#lx ta %#lx\n", name, cause, p->task);
  841. if(cause & (Aprcs|Aifs)){
  842. s0 = d->state;
  843. switch(p->sstatus & Devdet){
  844. case 0: /* no device */
  845. d->state = Dmissing;
  846. break;
  847. case Devpresent: /* device but no phy comm. */
  848. if((p->sstatus & Intpm) == Intslumber ||
  849. (p->sstatus & Intpm) == Intpartpwr)
  850. d->state = Dnew; /* slumbering */
  851. else
  852. d->state = Derror;
  853. break;
  854. case Devpresent|Devphycomm:
  855. /* power mgnt crap for surprise removal */
  856. p->ie |= Aprcs|Apcs; /* is this required? */
  857. d->state = Dreset;
  858. break;
  859. case Devphyoffline:
  860. d->state = Doffline;
  861. break;
  862. }
  863. dprint("%s: %s → %s [Apcrs] %#lx\n", name,
  864. diskstates[s0], diskstates[d->state], p->sstatus);
  865. /* print pulled message here. */
  866. if(s0 == Dready && d->state != Dready)
  867. idprint("%s: pulled\n", name); /* wtf? */
  868. if(d->state != Dready)
  869. atomic_set(&d->portm.flag, atomic_read(&d->portm.flag) | Ferror);
  870. ewake = 1;
  871. }
  872. p->serror = serr;
  873. if(ewake){
  874. clearci(p);
  875. wakeup(&d->portm.Rendez);
  876. }
  877. last = cause;
  878. }
  879. static void
  880. pstatus(Drive *d, uint32_t s)
  881. {
  882. /*
  883. * s is masked with Devdet.
  884. *
  885. * bogus code because the first interrupt is currently dropped.
  886. * likely my fault. serror may be cleared at the wrong time.
  887. */
  888. switch(s){
  889. case 0: /* no device */
  890. d->state = Dmissing;
  891. break;
  892. case Devpresent: /* device but no phy. comm. */
  893. break;
  894. case Devphycomm: /* should this be missing? need testcase. */
  895. dprint("ahci: pstatus 2\n");
  896. /* fallthrough */
  897. case Devpresent|Devphycomm:
  898. d->wait = 0;
  899. d->state = Dnew;
  900. break;
  901. case Devphyoffline:
  902. d->state = Doffline;
  903. break;
  904. case Devphyoffline|Devphycomm: /* does this make sense? */
  905. d->state = Dnew;
  906. break;
  907. }
  908. }
  909. static int
  910. configdrive(Drive *d)
  911. {
  912. if(ahciconfigdrive(d) == -1)
  913. return -1;
  914. ilock(&d->Lock);
  915. pstatus(d, d->port->sstatus & Devdet);
  916. iunlock(&d->Lock);
  917. return 0;
  918. }
  919. static void
  920. setstate(Drive *d, int state)
  921. {
  922. ilock(&d->Lock);
  923. d->state = state;
  924. iunlock(&d->Lock);
  925. }
  926. static void
  927. resetdisk(Drive *d)
  928. {
  929. uint state, det, stat;
  930. Aport *p;
  931. p = d->port;
  932. det = p->sctl & 7;
  933. stat = p->sstatus & Devdet;
  934. state = (p->cmd>>28) & 0xf;
  935. dprint("ahci: resetdisk: icc %#x det %d sdet %d\n", state, det, stat);
  936. ilock(&d->Lock);
  937. state = d->state;
  938. if(d->state != Dready || d->state != Dnew)
  939. atomic_set(&d->portm.flag, atomic_read(&d->portm.flag) | Ferror);
  940. clearci(p); /* satisfy sleep condition. */
  941. wakeup(&d->portm.Rendez);
  942. if(stat != (Devpresent|Devphycomm)){
  943. /* device absent or phy not communicating */
  944. d->state = Dportreset;
  945. iunlock(&d->Lock);
  946. return;
  947. }
  948. d->state = Derror;
  949. iunlock(&d->Lock);
  950. qlock(&d->portm.ql);
  951. if(p->cmd&Ast && ahciswreset(&d->portc) == -1)
  952. setstate(d, Dportreset); /* get a bigger stick. */
  953. else {
  954. setstate(d, Dmissing);
  955. configdrive(d);
  956. }
  957. dprint("ahci: %s: resetdisk: %s → %s\n", (d->unit? d->unit->SDperm.name: nil),
  958. diskstates[state], diskstates[d->state]);
  959. qunlock(&d->portm.ql);
  960. }
  961. static int
  962. newdrive(Drive *d)
  963. {
  964. char *name;
  965. Aportc *c;
  966. Aportm *pm;
  967. c = &d->portc;
  968. pm = &d->portm;
  969. name = d->unit->SDperm.name;
  970. if(name == 0)
  971. name = "??";
  972. if(d->port->task == 0x80)
  973. return -1;
  974. qlock(&c->pm->ql);
  975. if(setudmamode(c, 5) == -1){
  976. dprint("%s: can't set udma mode\n", name);
  977. goto lose;
  978. }
  979. if(identify(d) == -1){
  980. dprint("%s: identify failure\n", name);
  981. goto lose;
  982. }
  983. if(pm->feat & Dpower && setfeatures(c, 0x85) == -1){
  984. pm->feat &= ~Dpower;
  985. if(ahcirecover(c) == -1)
  986. goto lose;
  987. }
  988. setstate(d, Dready);
  989. qunlock(&c->pm->ql);
  990. idprint("%s: %sLBA %,llu sectors: %s %s %s %s\n", d->unit->SDperm.name,
  991. (pm->feat & Dllba? "L": ""), d->sectors, d->model, d->firmware,
  992. d->serial, d->mediachange? "[mediachange]": "");
  993. return 0;
  994. lose:
  995. idprint("%s: can't be initialized\n", d->unit->SDperm.name);
  996. setstate(d, Dnull);
  997. qunlock(&c->pm->ql);
  998. return -1;
  999. }
  1000. static void
  1001. westerndigitalhung(Drive *d)
  1002. {
  1003. if((d->portm.feat&Datapi) == 0 && d->active &&
  1004. TK2MS(sys->ticks - d->intick) > 5000){
  1005. dprint("%s: drive hung; resetting [%#lx] ci %#lx\n",
  1006. d->unit->SDperm.name, d->port->task, d->port->ci);
  1007. d->state = Dreset;
  1008. }
  1009. }
  1010. static uint16_t olds[NCtlr*NCtlrdrv];
  1011. static int
  1012. doportreset(Drive *d)
  1013. {
  1014. int i;
  1015. i = -1;
  1016. qlock(&d->portm.ql);
  1017. if(ahciportreset(&d->portc) == -1)
  1018. dprint("ahci: doportreset: fails\n");
  1019. else
  1020. i = 0;
  1021. qunlock(&d->portm.ql);
  1022. dprint("ahci: doportreset: portreset → %s [task %#lx]\n",
  1023. diskstates[d->state], d->port->task);
  1024. return i;
  1025. }
  1026. /* drive must be locked */
  1027. static void
  1028. statechange(Drive *d)
  1029. {
  1030. switch(d->state){
  1031. case Dnull:
  1032. case Doffline:
  1033. if(d->unit->sectors != 0){
  1034. d->sectors = 0;
  1035. d->mediachange = 1;
  1036. }
  1037. /* fallthrough */
  1038. case Dready:
  1039. d->wait = 0;
  1040. break;
  1041. }
  1042. }
  1043. static void
  1044. checkdrive(Drive *d, int i)
  1045. {
  1046. uint16_t s;
  1047. char *name;
  1048. if(d == nil) {
  1049. print("checkdrive: nil d\n");
  1050. return;
  1051. }
  1052. ilock(&d->Lock);
  1053. if(d->unit == nil || d->port == nil) {
  1054. if(0)
  1055. print("checkdrive: nil d->%s\n",
  1056. d->unit == nil? "unit": "port");
  1057. iunlock(&d->Lock);
  1058. return;
  1059. }
  1060. name = d->unit->SDperm.name;
  1061. s = d->port->sstatus;
  1062. if(s)
  1063. d->lastseen = sys->ticks;
  1064. if(s != olds[i]){
  1065. dprint("%s: status: %06#x -> %06#x: %s\n",
  1066. name, olds[i], s, diskstates[d->state]);
  1067. olds[i] = s;
  1068. d->wait = 0;
  1069. }
  1070. westerndigitalhung(d);
  1071. switch(d->state){
  1072. case Dnull:
  1073. case Dready:
  1074. break;
  1075. case Dmissing:
  1076. case Dnew:
  1077. switch(s & (Intactive | Devdet)){
  1078. case Devpresent: /* no device (pm), device but no phy. comm. */
  1079. ahciwakeup(d->port);
  1080. /* fall through */
  1081. case 0: /* no device */
  1082. break;
  1083. default:
  1084. dprint("%s: unknown status %06#x\n", name, s);
  1085. /* fall through */
  1086. case Intactive: /* active, no device */
  1087. if(++d->wait&Mphywait)
  1088. break;
  1089. reset:
  1090. if(++d->mode > DMsataii)
  1091. d->mode = 0;
  1092. if(d->mode == DMsatai){ /* we tried everything */
  1093. d->state = Dportreset;
  1094. goto portreset;
  1095. }
  1096. dprint("%s: reset; new mode %s\n", name,
  1097. modename[d->mode]);
  1098. iunlock(&d->Lock);
  1099. resetdisk(d);
  1100. ilock(&d->Lock);
  1101. break;
  1102. case Intactive|Devphycomm|Devpresent:
  1103. if((++d->wait&Midwait) == 0){
  1104. dprint("%s: slow reset %06#x task=%#lx; %d\n",
  1105. name, s, d->port->task, d->wait);
  1106. goto reset;
  1107. }
  1108. s = (unsigned char)d->port->task;
  1109. if(s == 0x7f || ((d->port->sig >> 16) != 0xeb14 &&
  1110. (s & ~0x17) != (1<<6)))
  1111. break;
  1112. iunlock(&d->Lock);
  1113. newdrive(d);
  1114. ilock(&d->Lock);
  1115. break;
  1116. }
  1117. break;
  1118. case Doffline:
  1119. if(d->wait++ & Mcomrwait)
  1120. break;
  1121. /* fallthrough */
  1122. case Derror:
  1123. case Dreset:
  1124. dprint("%s: reset [%s]: mode %d; status %06#x\n",
  1125. name, diskstates[d->state], d->mode, s);
  1126. iunlock(&d->Lock);
  1127. resetdisk(d);
  1128. ilock(&d->Lock);
  1129. break;
  1130. case Dportreset:
  1131. portreset:
  1132. if(d->wait++ & 0xff && (s & Intactive) == 0)
  1133. break;
  1134. /* device is active */
  1135. dprint("%s: portreset [%s]: mode %d; status %06#x\n",
  1136. name, diskstates[d->state], d->mode, s);
  1137. atomic_set(&d->portm.flag, atomic_read(&d->portm.flag) | Ferror);
  1138. clearci(d->port);
  1139. wakeup(&d->portm.Rendez);
  1140. if((s & Devdet) == 0){ /* no device */
  1141. d->state = Dmissing;
  1142. break;
  1143. }
  1144. iunlock(&d->Lock);
  1145. doportreset(d);
  1146. ilock(&d->Lock);
  1147. break;
  1148. }
  1149. statechange(d);
  1150. iunlock(&d->Lock);
  1151. }
  1152. static void
  1153. satakproc(void *v)
  1154. {
  1155. Proc *up = externup();
  1156. int i;
  1157. for(;;){
  1158. tsleep(&up->sleep, return0, 0, Nms);
  1159. for(i = 0; i < niadrive; i++)
  1160. if(iadrive[i] != nil)
  1161. checkdrive(iadrive[i], i);
  1162. }
  1163. }
  1164. static void
  1165. isctlrjabbering(Ctlr *c, uint32_t cause)
  1166. {
  1167. uint32_t now;
  1168. now = TK2MS(sys->ticks);
  1169. if (now > c->lastintr0) {
  1170. c->intrs = 0;
  1171. c->lastintr0 = now;
  1172. }
  1173. if (++c->intrs > Maxintrspertick) {
  1174. iprint("sdiahci: %lu intrs per tick for no serviced "
  1175. "drive; cause %#lx mport %d\n",
  1176. c->intrs, cause, c->mport);
  1177. c->intrs = 0;
  1178. }
  1179. }
  1180. static void
  1181. isdrivejabbering(Drive *d)
  1182. {
  1183. uint32_t now;
  1184. now = TK2MS(sys->ticks);
  1185. if (now > d->lastintr0) {
  1186. d->intrs = 0;
  1187. d->lastintr0 = now;
  1188. }
  1189. if (++d->intrs > Maxintrspertick) {
  1190. iprint("sdiahci: %lu interrupts per tick for %s\n",
  1191. d->intrs, d->unit->SDperm.name);
  1192. d->intrs = 0;
  1193. }
  1194. }
  1195. static void
  1196. iainterrupt(Ureg *u, void *a)
  1197. {
  1198. int i;
  1199. uint32_t cause, mask;
  1200. Ctlr *c;
  1201. Drive *d;
  1202. c = a;
  1203. ilock(&c->Lock);
  1204. cause = c->hba->isr;
  1205. if (cause == 0) {
  1206. isctlrjabbering(c, cause);
  1207. // iprint("sdiahci: interrupt for no drive\n");
  1208. iunlock(&c->Lock);
  1209. return;
  1210. }
  1211. for(i = 0; cause && i <= c->mport; i++){
  1212. mask = 1 << i;
  1213. if((cause & mask) == 0)
  1214. continue;
  1215. d = c->rawdrive + i;
  1216. ilock(&d->Lock);
  1217. isdrivejabbering(d);
  1218. if(d->port->isr && c->hba->pi & mask)
  1219. updatedrive(d);
  1220. c->hba->isr = mask;
  1221. iunlock(&d->Lock);
  1222. cause &= ~mask;
  1223. }
  1224. if (cause) {
  1225. isctlrjabbering(c, cause);
  1226. iprint("sdiachi: intr cause unserviced: %#lx\n", cause);
  1227. }
  1228. iunlock(&c->Lock);
  1229. }
  1230. /* checkdrive, called from satakproc, will prod the drive while we wait */
  1231. static void
  1232. awaitspinup(Drive *d)
  1233. {
  1234. int ms;
  1235. uint16_t s;
  1236. char *name;
  1237. ilock(&d->Lock);
  1238. if(d->unit == nil || d->port == nil) {
  1239. panic("awaitspinup: nil d->unit or d->port");
  1240. iunlock(&d->Lock);
  1241. return;
  1242. }
  1243. name = (d->unit? d->unit->SDperm.name: nil);
  1244. s = d->port->sstatus;
  1245. if(!(s & Devpresent)) { /* never going to be ready */
  1246. dprint("awaitspinup: %s absent, not waiting\n", name);
  1247. iunlock(&d->Lock);
  1248. return;
  1249. }
  1250. for (ms = 20000; ms > 0; ms -= 50)
  1251. switch(d->state){
  1252. case Dnull:
  1253. /* absent; done */
  1254. iunlock(&d->Lock);
  1255. dprint("awaitspinup: %s in null state\n", name);
  1256. return;
  1257. case Dready:
  1258. case Dnew:
  1259. if(d->sectors || d->mediachange) {
  1260. /* ready to use; done */
  1261. iunlock(&d->Lock);
  1262. dprint("awaitspinup: %s ready!\n", name);
  1263. return;
  1264. }
  1265. /* fall through */
  1266. default:
  1267. case Dmissing: /* normal waiting states */
  1268. case Dreset:
  1269. case Doffline: /* transitional states */
  1270. case Derror:
  1271. case Dportreset:
  1272. iunlock(&d->Lock);
  1273. asleep(50);
  1274. ilock(&d->Lock);
  1275. break;
  1276. }
  1277. print("awaitspinup: %s didn't spin up after 20 seconds\n", name);
  1278. iunlock(&d->Lock);
  1279. }
  1280. static int
  1281. iaverify(SDunit *u)
  1282. {
  1283. Ctlr *c;
  1284. Drive *d;
  1285. c = u->dev->ctlr;
  1286. d = c->drive[u->subno];
  1287. ilock(&c->Lock);
  1288. ilock(&d->Lock);
  1289. d->unit = u;
  1290. iunlock(&d->Lock);
  1291. iunlock(&c->Lock);
  1292. checkdrive(d, d->driveno); /* c->d0 + d->driveno */
  1293. /*
  1294. * hang around until disks are spun up and thus available as
  1295. * nvram, dos file systems, etc. you wouldn't expect it, but
  1296. * the intel 330 ssd takes a while to `spin up'.
  1297. */
  1298. awaitspinup(d);
  1299. return 1;
  1300. }
  1301. static int
  1302. iaenable(SDev *s)
  1303. {
  1304. char name[32];
  1305. Ctlr *c;
  1306. static int once;
  1307. c = s->ctlr;
  1308. ilock(&c->Lock);
  1309. if(!c->enabled) {
  1310. if(once == 0) {
  1311. once = 1;
  1312. kproc("ahci", satakproc, 0);
  1313. }
  1314. if(c->ndrive == 0)
  1315. panic("iaenable: zero s->ctlr->ndrive");
  1316. pcisetbme(c->pci);
  1317. snprint(name, sizeof name, "%s (%s)", s->name, s->ifc->name);
  1318. c->vector = intrenable(c->pci->intl, iainterrupt, c, c->pci->tbdf, name);
  1319. /* supposed to squelch leftover interrupts here. */
  1320. ahcienable(c->hba);
  1321. c->enabled = 1;
  1322. }
  1323. iunlock(&c->Lock);
  1324. return 1;
  1325. }
  1326. static int
  1327. iadisable(SDev *s)
  1328. {
  1329. char name[32];
  1330. Ctlr *c;
  1331. c = s->ctlr;
  1332. ilock(&c->Lock);
  1333. ahcidisable(c->hba);
  1334. snprint(name, sizeof name, "%s (%s)", s->name, s->ifc->name);
  1335. intrdisable(c->vector);
  1336. c->enabled = 0;
  1337. iunlock(&c->Lock);
  1338. return 1;
  1339. }
  1340. static int
  1341. iaonline(SDunit *unit)
  1342. {
  1343. int r;
  1344. Ctlr *c;
  1345. Drive *d;
  1346. c = unit->dev->ctlr;
  1347. d = c->drive[unit->subno];
  1348. r = 0;
  1349. if(d->portm.feat & Datapi && d->mediachange){
  1350. r = scsionline(unit);
  1351. if(r > 0)
  1352. d->mediachange = 0;
  1353. return r;
  1354. }
  1355. ilock(&d->Lock);
  1356. if(d->mediachange){
  1357. r = 2;
  1358. d->mediachange = 0;
  1359. /* devsd resets this after online is called; why? */
  1360. unit->sectors = d->sectors;
  1361. unit->secsize = 512; /* default size */
  1362. } else if(d->state == Dready)
  1363. r = 1;
  1364. iunlock(&d->Lock);
  1365. return r;
  1366. }
  1367. /* returns locked list! */
  1368. static Alist*
  1369. ahcibuild(Drive *d, unsigned char *cmd, void *data, int n, int64_t lba)
  1370. {
  1371. unsigned char *c, acmd, dir, llba;
  1372. Alist *l;
  1373. Actab *t;
  1374. Aportm *pm;
  1375. Aprdt *p;
  1376. static unsigned char tab[2][2] = { {0xc8, 0x25}, {0xca, 0x35}, };
  1377. pm = &d->portm;
  1378. dir = *cmd != 0x28;
  1379. llba = pm->feat&Dllba? 1: 0;
  1380. acmd = tab[dir][llba];
  1381. qlock(&pm->ql);
  1382. l = pm->list;
  1383. t = pm->ctab;
  1384. c = t->cfis;
  1385. c[0] = 0x27;
  1386. c[1] = 0x80;
  1387. c[2] = acmd;
  1388. c[3] = 0;
  1389. c[4] = lba; /* sector lba low 7:0 */
  1390. c[5] = lba >> 8; /* cylinder low lba mid 15:8 */
  1391. c[6] = lba >> 16; /* cylinder hi lba hi 23:16 */
  1392. c[7] = Obs | 0x40; /* 0x40 == lba */
  1393. if(llba == 0)
  1394. c[7] |= (lba>>24) & 7;
  1395. c[8] = lba >> 24; /* sector (exp) lba 31:24 */
  1396. c[9] = lba >> 32; /* cylinder low (exp) lba 39:32 */
  1397. c[10] = lba >> 48; /* cylinder hi (exp) lba 48:40 */
  1398. c[11] = 0; /* features (exp); */
  1399. c[12] = n; /* sector count */
  1400. c[13] = n >> 8; /* sector count (exp) */
  1401. c[14] = 0; /* r */
  1402. c[15] = 0; /* control */
  1403. *(uint32_t*)(c + 16) = 0;
  1404. l->flags = 1<<16 | Lpref | 0x5; /* Lpref ?? */
  1405. if(dir == Write)
  1406. l->flags |= Lwrite;
  1407. l->len = 0;
  1408. l->ctab = PCIWADDR(t);
  1409. l->ctabhi = PCIWADDR(t)>>32;
  1410. p = &t->prdt;
  1411. p->dba = PCIWADDR(data);
  1412. p->dbahi = PCIWADDR(data)>>32;
  1413. if(d->unit == nil)
  1414. panic("ahcibuild: nil d->unit");
  1415. p->count = 1<<31 | (d->unit->secsize*n - 2) | 1;
  1416. return l;
  1417. }
  1418. static Alist*
  1419. ahcibuildpkt(Aportm *pm, SDreq *r, void *data, int n)
  1420. {
  1421. int fill, len;
  1422. unsigned char *c;
  1423. Alist *l;
  1424. Actab *t;
  1425. Aprdt *p;
  1426. qlock(&pm->ql);
  1427. l = pm->list;
  1428. t = pm->ctab;
  1429. c = t->cfis;
  1430. fill = pm->feat&Datapi16? 16: 12;
  1431. if((len = r->clen) > fill)
  1432. len = fill;
  1433. memmove(t->atapi, r->cmd, len);
  1434. memset(t->atapi+len, 0, fill-len);
  1435. c[0] = 0x27;
  1436. c[1] = 0x80;
  1437. c[2] = 0xa0;
  1438. if(n != 0)
  1439. c[3] = 1; /* dma */
  1440. else
  1441. c[3] = 0; /* features (exp); */
  1442. c[4] = 0; /* sector lba low 7:0 */
  1443. c[5] = n; /* cylinder low lba mid 15:8 */
  1444. c[6] = n >> 8; /* cylinder hi lba hi 23:16 */
  1445. c[7] = Obs;
  1446. *(uint32_t*)(c + 8) = 0;
  1447. *(uint32_t*)(c + 12) = 0;
  1448. *(uint32_t*)(c + 16) = 0;
  1449. l->flags = 1<<16 | Lpref | Latapi | 0x5;
  1450. if(r->write != 0 && data)
  1451. l->flags |= Lwrite;
  1452. l->len = 0;
  1453. l->ctab = PCIWADDR(t);
  1454. l->ctabhi = PCIWADDR(t)>>32;
  1455. if(data == 0)
  1456. return l;
  1457. p = &t->prdt;
  1458. p->dba = PCIWADDR(data);
  1459. p->dbahi = PCIWADDR(data)>>32;
  1460. p->count = 1<<31 | (n - 2) | 1;
  1461. return l;
  1462. }
  1463. static int
  1464. waitready(Drive *d)
  1465. {
  1466. uint32_t s, i, delta;
  1467. for(i = 0; i < 15000; i += 250){
  1468. if(d->state == Dreset || d->state == Dportreset ||
  1469. d->state == Dnew)
  1470. return 1;
  1471. delta = sys->ticks - d->lastseen;
  1472. if(d->state == Dnull || delta > 10*1000)
  1473. return -1;
  1474. ilock(&d->Lock);
  1475. s = d->port->sstatus;
  1476. iunlock(&d->Lock);
  1477. if((s & Intpm) == 0 && delta > 1500)
  1478. return -1; /* no detect */
  1479. if(d->state == Dready &&
  1480. (s & Devdet) == (Devphycomm|Devpresent))
  1481. return 0; /* ready, present & phy. comm. */
  1482. esleep(250);
  1483. }
  1484. print("%s: not responding; offline\n", d->unit->SDperm.name);
  1485. setstate(d, Doffline);
  1486. return -1;
  1487. }
  1488. static int
  1489. lockready(Drive *d)
  1490. {
  1491. int i;
  1492. qlock(&d->portm.ql);
  1493. while ((i = waitready(d)) == 1) { /* could wait forever? */
  1494. qunlock(&d->portm.ql);
  1495. esleep(1);
  1496. qlock(&d->portm.ql);
  1497. }
  1498. return i;
  1499. }
  1500. static int
  1501. flushcache(Drive *d)
  1502. {
  1503. int i;
  1504. i = -1;
  1505. if(lockready(d) == 0)
  1506. i = ahciflushcache(&d->portc);
  1507. qunlock(&d->portm.ql);
  1508. return i;
  1509. }
  1510. static int
  1511. iariopkt(SDreq *r, Drive *d)
  1512. {
  1513. Proc *up = externup();
  1514. int n, count, try, max, flag, task, wormwrite;
  1515. char *name;
  1516. unsigned char *cmd, *data;
  1517. Aport *p;
  1518. Asleep as;
  1519. cmd = r->cmd;
  1520. name = d->unit->SDperm.name;
  1521. p = d->port;
  1522. aprint("ahci: iariopkt: %04#x %04#x %c %d %p\n",
  1523. cmd[0], cmd[2], "rw"[r->write], r->dlen, r->data);
  1524. if(cmd[0] == 0x5a && (cmd[2] & 0x3f) == 0x3f)
  1525. return sdmodesense(r, cmd, d->info, d->infosz);
  1526. r->rlen = 0;
  1527. count = r->dlen;
  1528. max = 65536;
  1529. try = 0;
  1530. retry:
  1531. data = r->data;
  1532. n = count;
  1533. if(n > max)
  1534. n = max;
  1535. ahcibuildpkt(&d->portm, r, data, n);
  1536. switch(waitready(d)){
  1537. case -1:
  1538. qunlock(&d->portm.ql);
  1539. return SDeio;
  1540. case 1:
  1541. qunlock(&d->portm.ql);
  1542. esleep(1);
  1543. goto retry;
  1544. }
  1545. /* d->portm qlock held here */
  1546. ilock(&d->Lock);
  1547. atomic_set(&d->portm.flag, 0);
  1548. iunlock(&d->Lock);
  1549. p->ci = 1;
  1550. as.p = p;
  1551. as.i = 1;
  1552. d->intick = sys->ticks;
  1553. d->active++;
  1554. while(waserror())
  1555. ;
  1556. /* don't sleep here forever */
  1557. tsleep(&d->portm.Rendez, ahciclear, &as, 3*1000);
  1558. poperror();
  1559. if(!ahciclear(&as)) {
  1560. qunlock(&d->portm.ql);
  1561. print("%s: ahciclear not true after 3 seconds\n", name);
  1562. r->status = SDcheck;
  1563. return SDcheck;
  1564. }
  1565. d->active--;
  1566. ilock(&d->Lock);
  1567. flag = atomic_read(&d->portm.flag);
  1568. task = d->port->task;
  1569. iunlock(&d->Lock);
  1570. if((task & (Efatal<<8)) || ((task & (ASbsy|ASdrq)) && d->state == Dready)){
  1571. d->port->ci = 0;
  1572. ahcirecover(&d->portc);
  1573. task = d->port->task;
  1574. flag &= ~Fdone; /* either an error or do-over */
  1575. }
  1576. qunlock(&d->portm.ql);
  1577. if(flag == 0){
  1578. if(++try == 10){
  1579. print("%s: bad disk\n", name);
  1580. r->status = SDcheck;
  1581. return SDcheck;
  1582. }
  1583. /*
  1584. * write retries cannot succeed on write-once media,
  1585. * so just accept any failure.
  1586. */
  1587. wormwrite = 0;
  1588. switch(d->unit->inquiry[0] & SDinq0periphtype){
  1589. case SDperworm:
  1590. case SDpercd:
  1591. switch(cmd[0]){
  1592. case 0x0a: /* write (6?) */
  1593. case 0x2a: /* write (10) */
  1594. case 0x8a: /* int32_t write (16) */
  1595. case 0x2e: /* write and verify (10) */
  1596. wormwrite = 1;
  1597. break;
  1598. }
  1599. break;
  1600. }
  1601. if (!wormwrite) {
  1602. print("%s: retry\n", name);
  1603. goto retry;
  1604. }
  1605. }
  1606. if(flag & Ferror){
  1607. if((task&Eidnf) == 0)
  1608. print("%s: i/o error task=%#x\n", name, task);
  1609. r->status = SDcheck;
  1610. return SDcheck;
  1611. }
  1612. data += n;
  1613. r->rlen = data - (unsigned char*)r->data;
  1614. r->status = SDok;
  1615. return SDok;
  1616. }
  1617. static int
  1618. iario(SDreq *r)
  1619. {
  1620. Proc *up = externup();
  1621. int i, n, count, try, max, flag, task;
  1622. int64_t lba;
  1623. char *name;
  1624. unsigned char *cmd, *data;
  1625. Aport *p;
  1626. Asleep as;
  1627. Ctlr *c;
  1628. Drive *d;
  1629. SDunit *unit;
  1630. unit = r->unit;
  1631. c = unit->dev->ctlr;
  1632. d = c->drive[unit->subno];
  1633. if(d->portm.feat & Datapi)
  1634. return iariopkt(r, d);
  1635. cmd = r->cmd;
  1636. name = d->unit->SDperm.name;
  1637. p = d->port;
  1638. if(r->cmd[0] == 0x35 || r->cmd[0] == 0x91){
  1639. if(flushcache(d) == 0)
  1640. return sdsetsense(r, SDok, 0, 0, 0);
  1641. return sdsetsense(r, SDcheck, 3, 0xc, 2);
  1642. }
  1643. if((i = sdfakescsi(r, d->info, d->infosz)) != SDnostatus){
  1644. r->status = i;
  1645. return i;
  1646. }
  1647. if(*cmd != 0x28 && *cmd != 0x2a){
  1648. print("%s: bad cmd %.2#x\n", name, cmd[0]);
  1649. r->status = SDcheck;
  1650. return SDcheck;
  1651. }
  1652. lba = cmd[2]<<24 | cmd[3]<<16 | cmd[4]<<8 | cmd[5];
  1653. count = cmd[7]<<8 | cmd[8];
  1654. if(r->data == nil)
  1655. return SDok;
  1656. if(r->dlen < count * unit->secsize)
  1657. count = r->dlen / unit->secsize;
  1658. max = 128;
  1659. try = 0;
  1660. retry:
  1661. data = r->data;
  1662. while(count > 0){
  1663. n = count;
  1664. if(n > max)
  1665. n = max;
  1666. ahcibuild(d, cmd, data, n, lba);
  1667. switch(waitready(d)){
  1668. case -1:
  1669. qunlock(&d->portm.ql);
  1670. return SDeio;
  1671. case 1:
  1672. qunlock(&d->portm.ql);
  1673. esleep(1);
  1674. goto retry;
  1675. }
  1676. /* d->portm qlock held here */
  1677. ilock(&d->Lock);
  1678. atomic_set(&d->portm.flag, 0);
  1679. iunlock(&d->Lock);
  1680. p->ci = 1;
  1681. as.p = p;
  1682. as.i = 1;
  1683. as.slept = 0;
  1684. d->intick = sys->ticks;
  1685. d->active++;
  1686. while(waserror())
  1687. ;
  1688. /* don't sleep here forever */
  1689. tsleep(&d->portm.Rendez, ahciclear, &as, 3*1000);
  1690. poperror();
  1691. if(!ahciclear(&as)) {
  1692. qunlock(&d->portm.ql);
  1693. print("%s: ahciclear not true after 3 seconds\n", name);
  1694. r->status = SDcheck;
  1695. return SDcheck;
  1696. }
  1697. d->active--;
  1698. ilock(&d->Lock);
  1699. flag = atomic_read(&d->portm.flag);
  1700. task = d->port->task;
  1701. iunlock(&d->Lock);
  1702. if((task & (Efatal<<8)) ||
  1703. ((task & (ASbsy|ASdrq)) && d->state == Dready)){
  1704. d->port->ci = 0;
  1705. ahcirecover(&d->portc);
  1706. task = d->port->task;
  1707. }
  1708. qunlock(&d->portm.ql);
  1709. if(flag == 0){
  1710. if(++try == 10){
  1711. print("%s: bad disk\n", name);
  1712. r->status = SDeio;
  1713. return SDeio;
  1714. }
  1715. print("%s: retry blk %lld\n", name, lba);
  1716. goto retry;
  1717. }
  1718. if(flag & Ferror){
  1719. print("%s: i/o error task=%#x @%,lld\n",
  1720. name, task, lba);
  1721. r->status = SDeio;
  1722. return SDeio;
  1723. }
  1724. count -= n;
  1725. lba += n;
  1726. data += n * unit->secsize;
  1727. }
  1728. r->rlen = data - (unsigned char*)r->data;
  1729. r->status = SDok;
  1730. return SDok;
  1731. }
  1732. /*
  1733. * configure drives 0-5 as ahci sata (c.f. errata).
  1734. * what about 6 & 7, as claimed by marvell 0x9123?
  1735. */
  1736. static int
  1737. iaahcimode(Pcidev *p)
  1738. {
  1739. dprint("iaahcimode: %#x %#x %#x\n", pcicfgr8(p, 0x91), pcicfgr8(p, 92),
  1740. pcicfgr8(p, 93));
  1741. pcicfgw16(p, 0x92, pcicfgr16(p, 0x92) | 0x3f); /* ports 0-5 */
  1742. return 0;
  1743. }
  1744. static void
  1745. iasetupahci(Ctlr *c)
  1746. {
  1747. /* disable cmd block decoding. */
  1748. pcicfgw16(c->pci, 0x40, pcicfgr16(c->pci, 0x40) & ~(1<<15));
  1749. pcicfgw16(c->pci, 0x42, pcicfgr16(c->pci, 0x42) & ~(1<<15));
  1750. c->lmmio[0x4/4] |= 1 << 31; /* enable ahci mode (ghc register) */
  1751. c->lmmio[0xc/4] = (1 << 6) - 1; /* 5 ports. (supposedly ro pi reg.) */
  1752. /* enable ahci mode and 6 ports; from ich9 datasheet */
  1753. pcicfgw16(c->pci, 0x90, 1<<6 | 1<<5);
  1754. }
  1755. static int
  1756. didtype(Pcidev *p)
  1757. {
  1758. switch(p->vid){
  1759. case Vintel:
  1760. if((p->did & 0xfffc) == 0x2680)
  1761. return Tesb;
  1762. /*
  1763. * 0x27c4 is the intel 82801 in compatibility (not sata) mode.
  1764. */
  1765. if (p->did == 0x1e02 || /* c210 */
  1766. p->did == 0x24d1 || /* 82801eb/er */
  1767. (p->did & 0xfffb) == 0x27c1 || /* 82801g[bh]m ich7 */
  1768. p->did == 0x2821 || /* 82801h[roh] */
  1769. (p->did & 0xfffe) == 0x2824 || /* 82801h[b] */
  1770. (p->did & 0xfeff) == 0x2829 || /* ich8/9m */
  1771. (p->did & 0xfffe) == 0x2922 || /* ich9 */
  1772. p->did == 0x3a02 || /* 82801jd/do */
  1773. (p->did & 0xfefe) == 0x3a22 || /* ich10, pch */
  1774. (p->did & 0xfff8) == 0x3b28) /* pchm */
  1775. return Tich;
  1776. break;
  1777. case Vatiamd:
  1778. if(p->did == 0x4380 || p->did == 0x4390 || p->did == 0x4391){
  1779. print("detected sb600 vid %#x did %#x\n", p->vid, p->did);
  1780. return Tsb600;
  1781. }
  1782. break;
  1783. case Vmarvell:
  1784. if (p->did == 0x9123)
  1785. print("ahci: marvell sata 3 controller has delusions "
  1786. "of something on unit 7\n");
  1787. break;
  1788. }
  1789. if(p->ccrb == Pcibcstore && p->ccru == Pciscsata && p->ccrp == 1){
  1790. print("ahci: Tunk: vid %#4.4x did %#4.4x\n", p->vid, p->did);
  1791. return Tunk;
  1792. }
  1793. return -1;
  1794. }
  1795. static int
  1796. newctlr(Ctlr *ctlr, SDev *sdev, int nunit)
  1797. {
  1798. int i, n;
  1799. Drive *drive;
  1800. ctlr->ndrive = sdev->nunit = nunit;
  1801. ctlr->mport = ctlr->hba->cap & ((1<<5)-1);
  1802. i = (ctlr->hba->cap >> 20) & ((1<<4)-1); /* iss */
  1803. print("#S/sd%c: %s: %#p %s, %d ports, irq %d\n", sdev->idno,
  1804. Tname(ctlr), ctlr->physio, descmode[i], nunit, ctlr->pci->intl);
  1805. /* map the drives -- they don't all need to be enabled. */
  1806. n = 0;
  1807. ctlr->rawdrive = malloc(NCtlrdrv * sizeof(Drive));
  1808. if(ctlr->rawdrive == nil) {
  1809. print("ahci: out of memory\n");
  1810. return -1;
  1811. }
  1812. for(i = 0; i < NCtlrdrv; i++) {
  1813. drive = ctlr->rawdrive + i;
  1814. drive->portno = i;
  1815. drive->driveno = -1;
  1816. drive->sectors = 0;
  1817. drive->serial[0] = ' ';
  1818. drive->ctlr = ctlr;
  1819. if((ctlr->hba->pi & (1<<i)) == 0)
  1820. continue;
  1821. drive->port = (Aport*)(ctlr->mmio + 0x80*i + 0x100);
  1822. drive->portc.p = drive->port;
  1823. drive->portc.pm = &drive->portm;
  1824. drive->driveno = n++;
  1825. ctlr->drive[drive->driveno] = drive;
  1826. iadrive[niadrive + drive->driveno] = drive;
  1827. }
  1828. for(i = 0; i < n; i++)
  1829. if(ahciidle(ctlr->drive[i]->port) == -1){
  1830. dprint("ahci: %s: port %d wedged; abort\n",
  1831. Tname(ctlr), i);
  1832. return -1;
  1833. }
  1834. for(i = 0; i < n; i++){
  1835. ctlr->drive[i]->mode = DMsatai;
  1836. configdrive(ctlr->drive[i]);
  1837. }
  1838. return n;
  1839. }
  1840. static SDev*
  1841. iapnp(void)
  1842. {
  1843. int n, nunit, type;
  1844. uintptr_t io;
  1845. Ctlr *c;
  1846. Pcidev *p;
  1847. SDev *head, *tail, *s;
  1848. static int done;
  1849. if(done++)
  1850. return nil;
  1851. memset(olds, 0xff, sizeof olds);
  1852. p = nil;
  1853. head = tail = nil;
  1854. while((p = pcimatch(p, 0, 0)) != nil){
  1855. type = didtype(p);
  1856. if (type == -1 || p->mem[Abar].bar == 0)
  1857. continue;
  1858. if(niactlr == NCtlr){
  1859. print("ahci: iapnp: %s: too many controllers\n",
  1860. tname[type]);
  1861. break;
  1862. }
  1863. c = iactlr + niactlr;
  1864. s = sdevs + niactlr;
  1865. memset(c, 0, sizeof *c);
  1866. memset(s, 0, sizeof *s);
  1867. io = p->mem[Abar].bar & ~0xf;
  1868. c->physio = (unsigned char *)io;
  1869. c->mmio = vmap(io, p->mem[Abar].size);
  1870. if(c->mmio == 0){
  1871. print("ahci: %s: address %#lX in use did=%#x\n",
  1872. Tname(c), io, p->did);
  1873. continue;
  1874. }
  1875. c->lmmio = (uint32_t*)c->mmio;
  1876. c->pci = p;
  1877. c->type = type;
  1878. s->ifc = &sdiahciifc;
  1879. s->idno = 'E' + niactlr;
  1880. s->ctlr = c;
  1881. c->sdev = s;
  1882. if(Intel(c) && p->did != 0x2681)
  1883. iasetupahci(c);
  1884. nunit = ahciconf(c);
  1885. // ahcihbareset((Ahba*)c->mmio);
  1886. if(Intel(c) && iaahcimode(p) == -1)
  1887. break;
  1888. if(nunit < 1){
  1889. vunmap(c->mmio, p->mem[Abar].size);
  1890. continue;
  1891. }
  1892. n = newctlr(c, s, nunit);
  1893. if(n < 0)
  1894. continue;
  1895. niadrive += n;
  1896. niactlr++;
  1897. if(head)
  1898. tail->next = s;
  1899. else
  1900. head = s;
  1901. tail = s;
  1902. }
  1903. return head;
  1904. }
  1905. static char* smarttab[] = {
  1906. "unset",
  1907. "error",
  1908. "threshold exceeded",
  1909. "normal"
  1910. };
  1911. static char *
  1912. pflag(char *s, char *e, unsigned char f)
  1913. {
  1914. unsigned char i;
  1915. for(i = 0; i < 8; i++)
  1916. if(f & (1 << i))
  1917. s = seprint(s, e, "%s ", flagname[i]);
  1918. return seprint(s, e, "\n");
  1919. }
  1920. static int
  1921. iarctl(SDunit *u, char *p, int l)
  1922. {
  1923. char buf[32];
  1924. char *e, *op;
  1925. Aport *o;
  1926. Ctlr *c;
  1927. Drive *d;
  1928. c = u->dev->ctlr;
  1929. if(c == nil) {
  1930. print("iarctl: nil u->dev->ctlr\n");
  1931. return 0;
  1932. }
  1933. d = c->drive[u->subno];
  1934. o = d->port;
  1935. e = p+l;
  1936. op = p;
  1937. if(d->state == Dready){
  1938. p = seprint(p, e, "model\t%s\n", d->model);
  1939. p = seprint(p, e, "serial\t%s\n", d->serial);
  1940. p = seprint(p, e, "firm\t%s\n", d->firmware);
  1941. if(d->smartrs == 0xff)
  1942. p = seprint(p, e, "smart\tenable error\n");
  1943. else if(d->smartrs == 0)
  1944. p = seprint(p, e, "smart\tdisabled\n");
  1945. else
  1946. p = seprint(p, e, "smart\t%s\n",
  1947. smarttab[d->portm.smart]);
  1948. p = seprint(p, e, "flag\t");
  1949. p = pflag(p, e, d->portm.feat);
  1950. }else
  1951. p = seprint(p, e, "no disk present [%s]\n", diskstates[d->state]);
  1952. serrstr(o->serror, buf, buf + sizeof buf - 1);
  1953. p = seprint(p, e, "reg\ttask %#lx cmd %#lx serr %#lx %s ci %#lx "
  1954. "is %#lx; sig %#lx sstatus %06#lx\n",
  1955. o->task, o->cmd, o->serror, buf,
  1956. o->ci, o->isr, o->sig, o->sstatus);
  1957. if(d->unit == nil)
  1958. panic("iarctl: nil d->unit");
  1959. p = seprint(p, e, "geometry %llu %lu\n", d->sectors, d->unit->secsize);
  1960. return p - op;
  1961. }
  1962. static void
  1963. runflushcache(Drive *d)
  1964. {
  1965. int32_t t0;
  1966. t0 = sys->ticks;
  1967. if(flushcache(d) != 0)
  1968. error(Eio);
  1969. dprint("ahci: flush in %ld ms\n", sys->ticks - t0);
  1970. }
  1971. static void
  1972. forcemode(Drive *d, char *mode)
  1973. {
  1974. int i;
  1975. for(i = 0; i < nelem(modename); i++)
  1976. if(strcmp(mode, modename[i]) == 0)
  1977. break;
  1978. if(i == nelem(modename))
  1979. i = 0;
  1980. ilock(&d->Lock);
  1981. d->mode = i;
  1982. iunlock(&d->Lock);
  1983. }
  1984. static void
  1985. runsmartable(Drive *d, int i)
  1986. {
  1987. Proc *up = externup();
  1988. if(waserror()){
  1989. qunlock(&d->portm.ql);
  1990. d->smartrs = 0;
  1991. nexterror();
  1992. }
  1993. if(lockready(d) == -1)
  1994. error(Eio);
  1995. d->smartrs = smart(&d->portc, i);
  1996. d->portm.smart = 0;
  1997. qunlock(&d->portm.ql);
  1998. poperror();
  1999. }
  2000. static void
  2001. forcestate(Drive *d, char *state)
  2002. {
  2003. int i;
  2004. for(i = 0; i < nelem(diskstates); i++)
  2005. if(strcmp(state, diskstates[i]) == 0)
  2006. break;
  2007. if(i == nelem(diskstates))
  2008. error(Ebadctl);
  2009. setstate(d, i);
  2010. }
  2011. /*
  2012. * force this driver to notice a change of medium if the hardware doesn't
  2013. * report it.
  2014. */
  2015. static void
  2016. changemedia(SDunit *u)
  2017. {
  2018. Ctlr *c;
  2019. Drive *d;
  2020. c = u->dev->ctlr;
  2021. d = c->drive[u->subno];
  2022. ilock(&d->Lock);
  2023. d->mediachange = 1;
  2024. u->sectors = 0;
  2025. iunlock(&d->Lock);
  2026. }
  2027. static int
  2028. iawctl(SDunit *u, Cmdbuf *cmd)
  2029. {
  2030. Proc *up = externup();
  2031. char **f;
  2032. Ctlr *c;
  2033. Drive *d;
  2034. uint i;
  2035. c = u->dev->ctlr;
  2036. d = c->drive[u->subno];
  2037. f = cmd->f;
  2038. if(strcmp(f[0], "change") == 0)
  2039. changemedia(u);
  2040. else if(strcmp(f[0], "flushcache") == 0)
  2041. runflushcache(d);
  2042. else if(strcmp(f[0], "identify") == 0){
  2043. i = strtoul(f[1]? f[1]: "0", 0, 0);
  2044. if(i > 0xff)
  2045. i = 0;
  2046. dprint("ahci: %04d %#x\n", i, d->info[i]);
  2047. }else if(strcmp(f[0], "mode") == 0)
  2048. forcemode(d, f[1]? f[1]: "satai");
  2049. else if(strcmp(f[0], "nop") == 0){
  2050. if((d->portm.feat & Dnop) == 0){
  2051. cmderror(cmd, "no drive support");
  2052. return -1;
  2053. }
  2054. if(waserror()){
  2055. qunlock(&d->portm.ql);
  2056. nexterror();
  2057. }
  2058. if(lockready(d) == -1)
  2059. error(Eio);
  2060. nop(&d->portc);
  2061. qunlock(&d->portm.ql);
  2062. poperror();
  2063. }else if(strcmp(f[0], "reset") == 0)
  2064. forcestate(d, "reset");
  2065. else if(strcmp(f[0], "smart") == 0){
  2066. if(d->smartrs == 0){
  2067. cmderror(cmd, "smart not enabled");
  2068. return -1;
  2069. }
  2070. if(waserror()){
  2071. qunlock(&d->portm.ql);
  2072. d->smartrs = 0;
  2073. nexterror();
  2074. }
  2075. if(lockready(d) == -1)
  2076. error(Eio);
  2077. d->portm.smart = 2 + smartrs(&d->portc);
  2078. qunlock(&d->portm.ql);
  2079. poperror();
  2080. }else if(strcmp(f[0], "smartdisable") == 0)
  2081. runsmartable(d, 1);
  2082. else if(strcmp(f[0], "smartenable") == 0)
  2083. runsmartable(d, 0);
  2084. else if(strcmp(f[0], "state") == 0)
  2085. forcestate(d, f[1]? f[1]: "null");
  2086. else{
  2087. cmderror(cmd, Ebadctl);
  2088. return -1;
  2089. }
  2090. return 0;
  2091. }
  2092. static char *
  2093. portr(char *p, char *e, uint x)
  2094. {
  2095. int i, a;
  2096. p[0] = 0;
  2097. a = -1;
  2098. for(i = 0; i < 32; i++){
  2099. if((x & (1<<i)) == 0){
  2100. if(a != -1 && i - 1 != a)
  2101. p = seprint(p, e, "-%d", i - 1);
  2102. a = -1;
  2103. continue;
  2104. }
  2105. if(a == -1){
  2106. if(i > 0)
  2107. p = seprint(p, e, ", ");
  2108. p = seprint(p, e, "%d", a = i);
  2109. }
  2110. }
  2111. if(a != -1 && i - 1 != a)
  2112. p = seprint(p, e, "-%d", i - 1);
  2113. return p;
  2114. }
  2115. /* must emit exactly one line per controller (sd(3)) */
  2116. static char*
  2117. iartopctl(SDev *sdev, char *p, char *e)
  2118. {
  2119. uint32_t cap;
  2120. char pr[25];
  2121. Ahba *hba;
  2122. Ctlr *ctlr;
  2123. #define has(x, str) if(cap & (x)) p = seprint(p, e, "%s ", (str))
  2124. ctlr = sdev->ctlr;
  2125. hba = ctlr->hba;
  2126. p = seprint(p, e, "sd%c ahci port %#p: ", sdev->idno, ctlr->physio);
  2127. cap = hba->cap;
  2128. has(Hs64a, "64a");
  2129. has(Hsalp, "alp");
  2130. has(Hsam, "am");
  2131. has(Hsclo, "clo");
  2132. has(Hcccs, "coal");
  2133. has(Hems, "ems");
  2134. has(Hsal, "led");
  2135. has(Hsmps, "mps");
  2136. has(Hsncq, "ncq");
  2137. has(Hssntf, "ntf");
  2138. has(Hspm, "pm");
  2139. has(Hpsc, "pslum");
  2140. has(Hssc, "slum");
  2141. has(Hsss, "ss");
  2142. has(Hsxs, "sxs");
  2143. portr(pr, pr + sizeof pr, hba->pi);
  2144. return seprint(p, e,
  2145. "iss %ld ncs %ld np %ld; ghc %#lx isr %#lx pi %#lx %s ver %#lx\n",
  2146. (cap>>20) & 0xf, (cap>>8) & 0x1f, 1 + (cap & 0x1f),
  2147. hba->ghc, hba->isr, hba->pi, pr, hba->ver);
  2148. #undef has
  2149. }
  2150. static int
  2151. iawtopctl(SDev *sdev, Cmdbuf *cmd)
  2152. {
  2153. int *v;
  2154. char **f;
  2155. f = cmd->f;
  2156. v = 0;
  2157. if (f[0] == nil)
  2158. return 0;
  2159. if(strcmp(f[0], "debug") == 0)
  2160. v = &debug;
  2161. else if(strcmp(f[0], "idprint") == 0)
  2162. v = &prid;
  2163. else if(strcmp(f[0], "aprint") == 0)
  2164. v = &datapi;
  2165. else
  2166. cmderror(cmd, Ebadctl);
  2167. switch(cmd->nf){
  2168. default:
  2169. cmderror(cmd, Ebadarg);
  2170. case 1:
  2171. *v ^= 1;
  2172. break;
  2173. case 2:
  2174. if(f[1])
  2175. *v = strcmp(f[1], "on") == 0;
  2176. else
  2177. *v ^= 1;
  2178. break;
  2179. }
  2180. return 0;
  2181. }
  2182. SDifc sdiahciifc = {
  2183. "iahci",
  2184. iapnp,
  2185. nil, /* legacy */
  2186. iaenable,
  2187. iadisable,
  2188. iaverify,
  2189. iaonline,
  2190. iario,
  2191. iarctl,
  2192. iawctl,
  2193. scsibio,
  2194. nil, /* probe */
  2195. nil, /* clear */
  2196. iartopctl,
  2197. iawtopctl,
  2198. };