sdiahci.c 40 KB

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