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