sdiahci.c 43 KB

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