sdiahci.c 44 KB

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