sdiahci.c 46 KB

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