123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238 |
- /*
- * intel/amd ahci sata controller
- * copyright © 2007 coraid, inc.
- */
- #include "u.h"
- #include "../port/lib.h"
- #include "mem.h"
- #include "dat.h"
- #include "fns.h"
- #include "io.h"
- #include "../port/error.h"
- #include "../port/sd.h"
- #include "ahci.h"
- #define dprint(...) if(debug) iprint(__VA_ARGS__); else USED(debug)
- #define idprint(...) if(prid) print(__VA_ARGS__); else USED(prid)
- #define aprint(...) if(datapi) print(__VA_ARGS__); else USED(datapi)
- #define Tname(c) tname[(c)->type]
- enum {
- NCtlr = 4,
- NCtlrdrv= 32,
- NDrive = NCtlr*NCtlrdrv,
- Read = 0,
- Write,
- };
- /* pci space configuration */
- enum {
- Pmap = 0x90,
- Ppcs = 0x91,
- Prev = 0xa8,
- };
- enum {
- Tesb,
- Tich,
- Tsb600,
- };
- #define Intel(x) ((x) == Tesb || (x) == Tich)
- static char *tname[] = {
- "63xxesb",
- "ich",
- "sb600",
- };
- enum {
- Dnull,
- Dmissing,
- Dnew,
- Dready,
- Derror,
- Dreset,
- Doffline,
- Dportreset,
- Dlast,
- };
- static char *diskstates[Dlast] = {
- "null",
- "missing",
- "new",
- "ready",
- "error",
- "reset",
- "offline",
- "portreset",
- };
- extern SDifc sdiahciifc;
- typedef struct Ctlr Ctlr;
- enum {
- DMautoneg,
- DMsatai,
- DMsataii,
- };
- static char *modename[] = {
- "auto",
- "satai",
- "sataii",
- };
- static char *flagname[] = {
- "llba",
- "smart",
- "power",
- "nop",
- "atapi",
- "atapi16",
- };
- typedef struct {
- Lock;
- Ctlr *ctlr;
- SDunit *unit;
- char name[10];
- Aport *port;
- Aportm portm;
- Aportc portc; /* redundant ptr to port and portm. */
- uchar mediachange;
- uchar state;
- uchar smartrs;
- uvlong sectors;
- ulong intick;
- ulong lastseen;
- int wait;
- uchar mode; /* DMautoneg, satai or sataii. */
- uchar active;
- char serial[20+1];
- char firmware[8+1];
- char model[40+1];
- ushort info[0x200];
- int driveno; /* ctlr*NCtlrdrv + unit */
- /* controller port # != driveno when not all ports are enabled */
- int portno;
- } Drive;
- struct Ctlr {
- Lock;
- int type;
- int enabled;
- SDev *sdev;
- Pcidev *pci;
- uchar *mmio;
- ulong *lmmio;
- Ahba *hba;
- Drive rawdrive[NCtlrdrv];
- Drive* drive[NCtlrdrv];
- int ndrive;
- };
- static Ctlr iactlr[NCtlr];
- static SDev sdevs[NCtlr];
- static int niactlr;
- static Drive *iadrive[NDrive];
- static int niadrive;
- static int debug;
- static int prid = 1;
- static int datapi;
- static char stab[] = {
- [0] 'i', 'm',
- [8] 't', 'c', 'p', 'e',
- [16] 'N', 'I', 'W', 'B', 'D', 'C', 'H', 'S', 'T', 'F', 'X'
- };
- static void
- serrstr(ulong r, char *s, char *e)
- {
- int i;
- e -= 3;
- for(i = 0; i < nelem(stab) && s < e; i++)
- if(r & (1<<i) && stab[i]){
- *s++ = stab[i];
- if(SerrBad & (1<<i))
- *s++ = '*';
- }
- *s = 0;
- }
- static char ntab[] = "0123456789abcdef";
- static void
- preg(uchar *reg, int n)
- {
- int i;
- char buf[25*3+1], *e;
- e = buf;
- for(i = 0; i < n; i++){
- *e++ = ntab[reg[i]>>4];
- *e++ = ntab[reg[i]&0xf];
- *e++ = ' ';
- }
- *e++ = '\n';
- *e = 0;
- dprint(buf);
- }
- static void
- dreg(char *s, Aport *p)
- {
- dprint("%stask=%lux; cmd=%lux; ci=%lux; is=%lux\n", s, p->task, p->cmd,
- p->ci, p->isr);
- }
- static void
- esleep(int ms)
- {
- if(waserror())
- return;
- tsleep(&up->sleep, return0, 0, ms);
- poperror();
- }
- typedef struct {
- Aport *p;
- int i;
- }Asleep;
- static int
- ahciclear(void *v)
- {
- Asleep *s;
- s = v;
- return (s->p->ci & s->i) == 0;
- }
- static void
- aesleep(Aportm *m, Asleep *a, int ms)
- {
- if(waserror())
- return;
- tsleep(m, ahciclear, a, ms);
- poperror();
- }
- static int
- ahciwait(Aportc *c, int ms)
- {
- Asleep as;
- Aport *p;
- p = c->p;
- p->ci = 1;
- as.p = p;
- as.i = 1;
- aesleep(c->m, &as, ms);
- if((p->task&1) == 0 && p->ci == 0)
- return 0;
- dreg("ahciwait timeout ", c->p);
- return -1;
- }
- static int
- nop(Aportc *pc)
- {
- uchar *c;
- Actab *t;
- Alist *l;
- if((pc->m->feat & Dnop) == 0)
- return -1;
- t = pc->m->ctab;
- c = t->cfis;
- memset(c, 0, 0x20);
- c[0] = 0x27;
- c[1] = 0x80;
- c[2] = 0x00;
- c[7] = 0xa0; /* obsolete device bits */
- l = pc->m->list;
- l->flags = Lwrite | 0x5;
- l->len = 0;
- l->ctab = PCIWADDR(t);
- l->ctabhi = 0;
- return ahciwait(pc, 3*1000);
- }
- static int
- setfeatures(Aportc *pc, uchar f)
- {
- uchar *c;
- Actab *t;
- Alist *l;
- t = pc->m->ctab;
- c = t->cfis;
- memset(c, 0, 0x20);
- c[0] = 0x27;
- c[1] = 0x80;
- c[2] = 0xef;
- c[3] = f;
- c[7] = 0xa0; /* obsolete device bits */
- l = pc->m->list;
- l->flags = Lwrite | 0x5;
- l->len = 0;
- l->ctab = PCIWADDR(t);
- l->ctabhi = 0;
- return ahciwait(pc, 3*1000);
- }
- static int
- setudmamode(Aportc *pc, uchar f)
- {
- uchar *c;
- Actab *t;
- Alist *l;
- /* hack */
- if((pc->p->sig >> 16) == 0xeb14)
- return 0;
- t = pc->m->ctab;
- c = t->cfis;
- memset(c, 0, 0x20);
- c[0] = 0x27;
- c[1] = 0x80;
- c[2] = 0xef;
- c[3] = 3; /* set transfer mode */
- c[7] = 0xa0; /* obsolete device bits */
- c[12] = 0x40 | f; /* sector count */
- l = pc->m->list;
- l->flags = Lwrite | 0x5;
- l->len = 0;
- l->ctab = PCIWADDR(t);
- l->ctabhi = 0;
- return ahciwait(pc, 3*1000);
- }
- static void
- asleep(int ms)
- {
- if(up == nil)
- delay(ms);
- else
- esleep(ms);
- }
- static int
- ahciportreset(Aportc *c)
- {
- ulong *cmd, i;
- Aport *p;
- p = c->p;
- cmd = &p->cmd;
- *cmd &= ~(Afre|Ast);
- for(i = 0; i < 500; i += 25){
- if((*cmd&Acr) == 0)
- break;
- asleep(25);
- }
- p->sctl = 1|(p->sctl&~7);
- delay(1);
- p->sctl &= ~7;
- return 0;
- }
- static int
- smart(Aportc *pc, int n)
- {
- uchar *c;
- Actab *t;
- Alist *l;
- if((pc->m->feat&Dsmart) == 0)
- return -1;
- t = pc->m->ctab;
- c = t->cfis;
- memset(c, 0, 0x20);
- c[0] = 0x27;
- c[1] = 0x80;
- c[2] = 0xb0;
- c[3] = 0xd8 + n; /* able smart */
- c[5] = 0x4f;
- c[6] = 0xc2;
- c[7] = 0xa0;
- l = pc->m->list;
- l->flags = Lwrite | 0x5;
- l->len = 0;
- l->ctab = PCIWADDR(t);
- l->ctabhi = 0;
- if(ahciwait(pc, 1000) == -1 || pc->p->task & (1|32)){
- dprint("smart fail %lux\n", pc->p->task);
- // preg(pc->m->fis.r, 20);
- return -1;
- }
- if(n)
- return 0;
- return 1;
- }
- static int
- smartrs(Aportc *pc)
- {
- uchar *c;
- Actab *t;
- Alist *l;
- t = pc->m->ctab;
- c = t->cfis;
- memset(c, 0, 0x20);
- c[0] = 0x27;
- c[1] = 0x80;
- c[2] = 0xb0;
- c[3] = 0xda; /* return smart status */
- c[5] = 0x4f;
- c[6] = 0xc2;
- c[7] = 0xa0;
- l = pc->m->list;
- l->flags = Lwrite | 0x5;
- l->len = 0;
- l->ctab = PCIWADDR(t);
- l->ctabhi = 0;
- c = pc->m->fis.r;
- if(ahciwait(pc, 1000) == -1 || pc->p->task & (1|32)){
- dprint("smart fail %lux\n", pc->p->task);
- preg(c, 20);
- return -1;
- }
- if(c[5] == 0x4f && c[6] == 0xc2)
- return 1;
- return 0;
- }
- static int
- ahciflushcache(Aportc *pc)
- {
- uchar *c, llba;
- Actab *t;
- Alist *l;
- static uchar tab[2] = {0xe7, 0xea};
- llba = pc->m->feat&Dllba? 1: 0;
- t = pc->m->ctab;
- c = t->cfis;
- memset(c, 0, 0x20);
- c[0] = 0x27;
- c[1] = 0x80;
- c[2] = tab[llba];
- c[7] = 0xa0;
- l = pc->m->list;
- l->flags = Lwrite | 0x5;
- l->len = 0;
- l->ctab = PCIWADDR(t);
- l->ctabhi = 0;
- if(ahciwait(pc, 60000) == -1 || pc->p->task & (1|32)){
- dprint("ahciflushcache fail %lux\n", pc->p->task);
- // preg( pc->m->fis.r, 20);
- return -1;
- }
- return 0;
- }
- static ushort
- gbit16(void *a)
- {
- ushort j;
- uchar *i;
- i = a;
- j = i[1] << 8;
- j |= i[0];
- return j;
- }
- static ulong
- gbit32(void *a)
- {
- ulong j;
- uchar *i;
- i = a;
- j = i[3] << 24;
- j |= i[2] << 16;
- j |= i[1] << 8;
- j |= i[0];
- return j;
- }
- static uvlong
- gbit64(void *a)
- {
- uchar *i;
- i = a;
- return (uvlong)gbit32(i+4) << 32 | gbit32(a);
- }
- static int
- ahciidentify0(Aportc *pc, void *id, int atapi)
- {
- uchar *c;
- Actab *t;
- Alist *l;
- Aprdt *p;
- static uchar tab[] = { 0xec, 0xa1, };
- t = pc->m->ctab;
- c = t->cfis;
- memset(c, 0, 0x20);
- c[0] = 0x27;
- c[1] = 0x80;
- c[2] = tab[atapi];
- c[7] = 0xa0; /* obsolete device bits */
- l = pc->m->list;
- l->flags = 1<<16 | 0x5;
- l->len = 0;
- l->ctab = PCIWADDR(t);
- l->ctabhi = 0;
- memset(id, 0, 0x100);
- p = &t->prdt;
- p->dba = PCIWADDR(id);
- p->dbahi = 0;
- p->count = 1<<31 | (0x200-2) | 1;
- return ahciwait(pc, 3*1000);
- }
- static vlong
- ahciidentify(Aportc *pc, ushort *id)
- {
- int i, sig;
- vlong s;
- Aportm *m;
- m = pc->m;
- m->feat = 0;
- m->smart = 0;
- i = 0;
- sig = pc->p->sig >> 16;
- if(sig == 0xeb14){
- m->feat |= Datapi;
- i = 1;
- }
- if(ahciidentify0(pc, id, i) == -1)
- return -1;
- i = gbit16(id+83) | gbit16(id+86);
- if(i & (1<<10)){
- m->feat |= Dllba;
- s = gbit64(id+100);
- }else
- s = gbit32(id+60);
- if(m->feat&Datapi){
- i = gbit16(id+0);
- if(i&1)
- m->feat |= Datapi16;
- }
- i = gbit16(id+83);
- if((i>>14) == 1) {
- if(i & (1<<3))
- m->feat |= Dpower;
- i = gbit16(id+82);
- if(i & 1)
- m->feat |= Dsmart;
- if(i & (1<<14))
- m->feat |= Dnop;
- }
- return s;
- }
- static int
- ahciquiet(Aport *a)
- {
- ulong *p, i;
- p = &a->cmd;
- *p &= ~Ast;
- for(i = 0; i < 500; i += 50){
- if((*p & Acr) == 0)
- goto stop;
- asleep(50);
- }
- return -1;
- stop:
- if((a->task & (ASdrq|ASbsy)) == 0){
- *p |= Ast;
- return 0;
- }
- *p |= Aclo;
- for(i = 0; i < 500; i += 50){
- if((*p & Aclo) == 0)
- goto stop1;
- asleep(50);
- }
- return -1;
- stop1:
- /* extra check */
- dprint("clo clear %lx\n", a->task);
- if(a->task & ASbsy)
- return -1;
- *p |= Ast;
- return 0;
- }
- static int
- ahcicomreset(Aportc *pc)
- {
- uchar *c;
- Actab *t;
- Alist *l;
- dprint("ahcicomreset\n");
- dreg("comreset ", pc->p);
- if(ahciquiet(pc->p) == -1){
- dprint("ahciquiet fails\n");
- return -1;
- }
- dreg("comreset ", pc->p);
- t = pc->m->ctab;
- c = t->cfis;
- memset(c, 0, 0x20);
- c[0] = 0x27;
- c[1] = 0x00;
- c[7] = 0xa0; /* obsolete device bits */
- c[15] = 1<<2; /* srst */
- l = pc->m->list;
- l->flags = Lclear | Lreset | 0x5;
- l->len = 0;
- l->ctab = PCIWADDR(t);
- l->ctabhi = 0;
- if(ahciwait(pc, 500) == -1){
- dprint("first command in comreset fails\n");
- return -1;
- }
- microdelay(250);
- dreg("comreset ", pc->p);
- memset(c, 0, 0x20);
- c[0] = 0x27;
- c[1] = 0x00;
- c[7] = 0xa0; /* obsolete device bits */
- l = pc->m->list;
- l->flags = Lwrite | 0x5;
- l->len = 0;
- l->ctab = PCIWADDR(t);
- l->ctabhi = 0;
- if(ahciwait(pc, 150) == -1){
- dprint("second command in comreset fails\n");
- return -1;
- }
- dreg("comreset ", pc->p);
- return 0;
- }
- static int
- ahciidle(Aport *port)
- {
- ulong *p, i, r;
- p = &port->cmd;
- if((*p & Arun) == 0)
- return 0;
- *p &= ~Ast;
- r = 0;
- for(i = 0; i < 500; i += 25){
- if((*p & Acr) == 0)
- goto stop;
- asleep(25);
- }
- r = -1;
- stop:
- if((*p & Afre) == 0)
- return r;
- *p &= ~Afre;
- for(i = 0; i < 500; i += 25){
- if((*p & Afre) == 0)
- return 0;
- asleep(25);
- }
- return -1;
- }
- /*
- * § 6.2.2.1 first part; comreset handled by reset disk.
- * - remainder is handled by configdisk.
- * - ahcirecover is a quick recovery from a failed command.
- */
- static int
- ahciswreset(Aportc *pc)
- {
- int i;
- i = ahciidle(pc->p);
- pc->p->cmd |= Afre;
- if(i == -1)
- return -1;
- if(pc->p->task & (ASdrq|ASbsy))
- return -1;
- return 0;
- }
- static int
- ahcirecover(Aportc *pc)
- {
- ahciswreset(pc);
- pc->p->cmd |= Ast;
- if(setudmamode(pc, 5) == -1)
- return -1;
- return 0;
- }
- static void*
- malign(int size, int align)
- {
- void *v;
- v = xspanalloc(size, align, 0);
- memset(v, 0, size);
- return v;
- }
- static void
- setupfis(Afis *f)
- {
- f->base = malign(0x100, 0x100);
- f->d = f->base + 0;
- f->p = f->base + 0x20;
- f->r = f->base + 0x40;
- f->u = f->base + 0x60;
- f->devicebits = (ulong*)(f->base + 0x58);
- }
- static void
- ahciwakeup(Aport *p)
- {
- ushort s;
- s = p->sstatus;
- if((s & 0xF00) != 0x600)
- return;
- if((s & 7) != 1){ /* not (device, no phy) */
- iprint("ahci: slumbering drive unwakeable %ux\n", s);
- return;
- }
- p->sctl = 3*Aipm | 0*Aspd | Adet;
- delay(1);
- p->sctl &= ~7;
- // iprint("ahci: wake %ux -> %ux\n", s, p->sstatus);
- }
- static int
- ahciconfigdrive(Ahba *h, Aportc *c, int mode)
- {
- Aportm *m;
- Aport *p;
- p = c->p;
- m = c->m;
- if(m->list == 0){
- setupfis(&m->fis);
- m->list = malign(sizeof *m->list, 1024);
- m->ctab = malign(sizeof *m->ctab, 128);
- }
- if(p->sstatus & 3 && h->cap & Hsss){
- /* device connected & staggered spin-up */
- dprint("configdrive: spinning up ... [%lux]\n", p->sstatus);
- p->cmd |= Apod|Asud;
- asleep(1400);
- }
- p->serror = SerrAll;
- p->list = PCIWADDR(m->list);
- p->listhi = 0;
- p->fis = PCIWADDR(m->fis.base);
- p->fishi = 0;
- p->cmd |= Afre|Ast;
- if((p->sstatus & 0xF0F) == 0x601) /* drive coming up in slumbering? */
- ahciwakeup(p);
- /* disable power managment sequence from book. */
- p->sctl = (3*Aipm) | (mode*Aspd) | 0*Adet;
- p->cmd &= ~Aalpe;
- p->ie = IEM;
- return 0;
- }
- static int
- ahcienable(Ahba *h)
- {
- h->ghc |= Hie;
- return 0;
- }
- static int
- ahcidisable(Ahba *h)
- {
- h->ghc &= ~Hie;
- return 0;
- }
- static int
- countbits(ulong u)
- {
- int i, n;
- n = 0;
- for(i = 0; i < 32; i++)
- if(u & (1<<i))
- n++;
- return n;
- }
- static int
- ahciconf(Ctlr *ctlr)
- {
- Ahba *h;
- ulong u;
- h = ctlr->hba = (Ahba*)ctlr->mmio;
- u = h->cap;
- if((u&Hsam) == 0)
- h->ghc |= Hae;
- print("#S/sd%c: ahci: port %#p: hba sss %ld; ncs %ld; coal %ld; "
- "mports %ld; led %ld; clo %ld; ems %ld\n",
- ctlr->sdev->idno, h,
- (u>>27) & 1, (u>>8) & 0x1f, (u>>7) & 1, u & 0x1f, (u>>25) & 1,
- (u>>24) & 1, (u>>6) & 1);
- return countbits(h->pi);
- }
- static int
- ahcihbareset(Ahba *h)
- {
- int wait;
- h->ghc |= 1;
- for(wait = 0; wait < 1000; wait += 100){
- if(h->ghc == 0)
- return 0;
- delay(100);
- }
- return -1;
- }
- static void
- idmove(char *p, ushort *a, int n)
- {
- int i;
- char *op, *e;
- op = p;
- for(i = 0; i < n/2; i++){
- *p++ = a[i] >> 8;
- *p++ = a[i];
- }
- *p = 0;
- while(p > op && *--p == ' ')
- *p = 0;
- e = p;
- p = op;
- while(*p == ' ')
- p++;
- memmove(op, p, n - (e - p));
- }
- static int
- identify(Drive *d)
- {
- ushort *id;
- vlong osectors, s;
- uchar oserial[21];
- SDunit *u;
- id = d->info;
- s = ahciidentify(&d->portc, id);
- if(s == -1){
- d->state = Derror;
- return -1;
- }
- osectors = d->sectors;
- memmove(oserial, d->serial, sizeof d->serial);
- d->sectors = s;
- d->smartrs = 0;
- idmove(d->serial, id+10, 20);
- idmove(d->firmware, id+23, 8);
- idmove(d->model, id+27, 40);
- u = d->unit;
- memset(u->inquiry, 0, sizeof u->inquiry);
- u->inquiry[2] = 2;
- u->inquiry[3] = 2;
- u->inquiry[4] = sizeof u->inquiry - 4;
- memmove(u->inquiry+8, d->model, 40);
- if((osectors == 0 || osectors != s) &&
- memcmp(oserial, d->serial, sizeof oserial) != 0){
- d->mediachange = 1;
- u->sectors = 0;
- }
- return 0;
- }
- static void
- clearci(Aport *p)
- {
- if(p->cmd & Ast) {
- p->cmd &= ~Ast;
- p->cmd |= Ast;
- }
- }
- static void
- updatedrive(Drive *d)
- {
- ulong cause, serr, s0, pr, ewake;
- char *name;
- Aport *p;
- static ulong last;
- pr = 1;
- ewake = 0;
- p = d->port;
- cause = p->isr;
- serr = p->serror;
- p->isr = cause;
- name = "??";
- if(d->unit && d->unit->name)
- name = d->unit->name;
- if(p->ci == 0){
- d->portm.flag |= Fdone;
- wakeup(&d->portm);
- pr = 0;
- }else if(cause & Adps)
- pr = 0;
- if(cause & Ifatal){
- ewake = 1;
- dprint("Fatal\n");
- }
- if(cause & Adhrs){
- if(p->task & 33){
- dprint("Adhrs cause = %lux; serr = %lux; task=%lux\n",
- cause, serr, p->task);
- d->portm.flag |= Ferror;
- ewake = 1;
- }
- pr = 0;
- }
- if(p->task & 1 && last != cause)
- dprint("err ca %lux serr %lux task %lux sstat %lux\n",
- cause, serr, p->task, p->sstatus);
- if(pr)
- dprint("%s: upd %lux ta %lux\n", name, cause, p->task);
- if(cause & (Aprcs|Aifs)){
- s0 = d->state;
- switch(p->sstatus & 7){
- case 0: /* no device */
- d->state = Dmissing;
- break;
- case 1: /* device but no phy comm. */
- if((p->sstatus & 0xF00) == 0x600)
- d->state = Dnew; /* slumbering */
- else
- d->state = Derror;
- break;
- case 3: /* device & phy comm. estab. */
- /* power mgnt crap for suprise removal */
- p->ie |= Aprcs|Apcs; /* is this required? */
- d->state = Dreset;
- break;
- case 4: /* phy off-line */
- d->state = Doffline;
- break;
- }
- dprint("%s: %s → %s [Apcrs] %lux\n", name, diskstates[s0],
- diskstates[d->state], p->sstatus);
- /* print pulled message here. */
- if(s0 == Dready && d->state != Dready)
- idprint("%s: pulled\n", name);
- if(d->state != Dready)
- d->portm.flag |= Ferror;
- ewake = 1;
- }
- p->serror = serr;
- if(ewake){
- clearci(p);
- wakeup(&d->portm);
- }
- last = cause;
- }
- static void
- pstatus(Drive *d, ulong s)
- {
- /*
- * bogus code because the first interrupt is currently dropped.
- * likely my fault. serror is maybe cleared at the wrong time.
- */
- switch(s){
- case 0: /* no device */
- d->state = Dmissing;
- break;
- case 1: /* device but no phy. comm. */
- break;
- case 2: /* should this be missing? need testcase. */
- dprint("pstatus 2\n");
- /* fallthrough */
- case 3: /* device & phy. comm. */
- d->wait = 0;
- d->state = Dnew;
- break;
- case 4: /* offline */
- d->state = Doffline;
- break;
- case 6: /* ? not sure this makes sense. TODO */
- d->state = Dnew;
- break;
- }
- }
- static int
- configdrive(Drive *d)
- {
- if(ahciconfigdrive(d->ctlr->hba, &d->portc, d->mode) == -1)
- return -1;
- ilock(d);
- pstatus(d, d->port->sstatus & 7);
- iunlock(d);
- return 0;
- }
- static void
- resetdisk(Drive *d)
- {
- uint state, det, stat;
- Aport *p;
- p = d->port;
- det = p->sctl & 7;
- stat = p->sstatus & 7;
- state = (p->cmd>>28) & 0xf;
- dprint("resetdisk: icc %ux det %d sdet %d\n", state, det, stat);
- if(stat != 3){ /* device absent or phy not communicating? */
- ilock(d);
- d->state = Dportreset;
- iunlock(d);
- return;
- }
- ilock(d);
- state = d->state;
- if(d->state != Dready || d->state != Dnew)
- d->portm.flag |= Ferror;
- clearci(p); /* satisfy sleep condition. */
- wakeup(&d->portm);
- d->state = Derror;
- iunlock(d);
- qlock(&d->portm);
- if(p->cmd&Ast && ahciswreset(&d->portc) == -1){
- ilock(d);
- d->state = Dportreset; /* get a bigger stick. */
- iunlock(d);
- } else {
- ilock(d);
- d->state = Dmissing;
- iunlock(d);
- configdrive(d);
- }
- dprint("resetdisk: %s → %s\n", diskstates[state], diskstates[d->state]);
- qunlock(&d->portm);
- }
- static int
- newdrive(Drive *d)
- {
- char *name, *s;
- Aportc *c;
- Aportm *m;
- c = &d->portc;
- m = &d->portm;
- name = d->unit->name;
- if(name == 0)
- name = "??";
- if(d->port->task == 0x80)
- return -1;
- qlock(c->m);
- if(setudmamode(c, 5) == -1){
- dprint("%s: can't set udma mode\n", name);
- goto lose;
- }
- if(identify(d) == -1){
- dprint("%s: identify failure\n", name);
- goto lose;
- }
- if(m->feat & Dpower && setfeatures(c, 0x85) == -1){
- m->feat &= ~Dpower;
- if(ahcirecover(c) == -1)
- goto lose;
- }
- ilock(d);
- d->state = Dready;
- iunlock(d);
- qunlock(c->m);
- s = "";
- if(m->feat & Dllba)
- s = "L";
- idprint("%s: %sLBA %,lld sectors\n", d->unit->name, s, d->sectors);
- idprint(" %s %s %s %s\n", d->model, d->firmware, d->serial,
- d->mediachange? "[mediachange]": "");
- return 0;
- lose:
- idprint("%s: can't be initialized\n", d->unit->name);
- ilock(d);
- d->state = Dnull;
- iunlock(d);
- qunlock(c->m);
- return -1;
- }
- enum {
- Nms = 256,
- Mphywait = 2*1024/Nms - 1,
- Midwait = 16*1024/Nms - 1,
- Mcomrwait = 64*1024/Nms - 1,
- };
- static void
- westerndigitalhung(Drive *d)
- {
- if((d->portm.feat&Datapi) == 0 && d->active &&
- TK2MS(MACHP(0)->ticks - d->intick) > 5000){
- dprint("%s: drive hung; resetting [%lux] ci=%lx\n",
- d->unit->name, d->port->task, d->port->ci);
- d->state = Dreset;
- }
- }
- static ushort olds[NCtlr*NCtlrdrv];
- static int
- doportreset(Drive *d)
- {
- int i;
- i = -1;
- qlock(&d->portm);
- if(ahciportreset(&d->portc) == -1)
- dprint("ahciportreset fails\n");
- else
- i = 0;
- qunlock(&d->portm);
- dprint("portreset → %s [task %lux]\n",
- diskstates[d->state], d->port->task);
- return i;
- }
- static void
- checkdrive(Drive *d, int i)
- {
- ushort s;
- char *name;
- ilock(d);
- name = d->unit->name;
- s = d->port->sstatus;
- if(s)
- d->lastseen = MACHP(0)->ticks;
- if(s != olds[i]){
- dprint("%s: status: %04ux -> %04ux: %s\n",
- name, olds[i], s, diskstates[d->state]);
- olds[i] = s;
- d->wait = 0;
- }
- westerndigitalhung(d);
- switch(d->state){
- case Dnull:
- case Dready:
- break;
- case Dmissing:
- case Dnew:
- switch(s & 0x107){
- case 1: /* no device (pm), device but no phy. comm. */
- ahciwakeup(d->port);
- /* fall through */
- case 0: /* no device */
- break;
- default:
- dprint("%s: unknown status %04ux\n", name, s);
- /* fall through */
- case 0x100: /* active, no device */
- if(++d->wait&Mphywait)
- break;
- reset:
- if(++d->mode > DMsataii)
- d->mode = 0;
- if(d->mode == DMsatai){ /* we tried everything */
- d->state = Dportreset;
- goto portreset;
- }
- dprint("%s: reset; new mode %s\n", name,
- modename[d->mode]);
- iunlock(d);
- resetdisk(d);
- ilock(d);
- break;
- case 0x103: /* active, device, phy. comm. */
- if((++d->wait&Midwait) == 0){
- dprint("%s: slow reset %04ux task=%lux; %d\n",
- name, s, d->port->task, d->wait);
- goto reset;
- }
- s = (uchar)d->port->task;
- if(s == 0x7f || ((d->port->sig >> 16) != 0xeb14 &&
- (s & ~0x17) != (1<<6)))
- break;
- iunlock(d);
- newdrive(d);
- ilock(d);
- break;
- }
- break;
- case Doffline:
- if(d->wait++ & Mcomrwait)
- break;
- /* fallthrough */
- case Derror:
- case Dreset:
- dprint("%s: reset [%s]: mode %d; status %04ux\n",
- name, diskstates[d->state], d->mode, s);
- iunlock(d);
- resetdisk(d);
- ilock(d);
- break;
- case Dportreset:
- portreset:
- if(d->wait++ & 0xff && (s & 0x100) == 0)
- break;
- /* device is active */
- dprint("%s: portreset [%s]: mode %d; status %04ux\n",
- name, diskstates[d->state], d->mode, s);
- d->portm.flag |= Ferror;
- clearci(d->port);
- wakeup(&d->portm);
- if((s & 7) == 0){ /* no device */
- d->state = Dmissing;
- break;
- }
- iunlock(d);
- doportreset(d);
- ilock(d);
- break;
- }
- iunlock(d);
- }
- static void
- satakproc(void*)
- {
- int i;
- memset(olds, 0xff, sizeof olds);
- for(;;){
- tsleep(&up->sleep, return0, 0, Nms);
- for(i = 0; i < niadrive; i++)
- checkdrive(iadrive[i], i);
- }
- }
- static void
- iainterrupt(Ureg*, void *a)
- {
- int i;
- ulong cause, m;
- Ctlr *c;
- Drive *d;
- c = a;
- ilock(c);
- cause = c->hba->isr;
- for(i = 0; i < c->ndrive; i++){
- m = 1 << i;
- if((cause & m) == 0)
- continue;
- d = c->rawdrive + i;
- ilock(d);
- if(d->port->isr && c->hba->pi & m)
- updatedrive(d);
- c->hba->isr = m;
- iunlock(d);
- }
- iunlock(c);
- }
- static int
- iaverify(SDunit *u)
- {
- Ctlr *c;
- Drive *d;
- c = u->dev->ctlr;
- d = c->drive[u->subno];
- ilock(c);
- ilock(d);
- d->unit = u;
- iunlock(d);
- iunlock(c);
- checkdrive(d, d->driveno);
- return 1;
- }
- static int
- iaenable(SDev *s)
- {
- char name[32];
- Ctlr *c;
- static int once;
- c = s->ctlr;
- ilock(c);
- if(!c->enabled) {
- if(once == 0) {
- once = 1;
- kproc("iasata", satakproc, 0);
- }
- if(c->ndrive == 0)
- panic("iaenable: zero s->ctlr->ndrive");
- pcisetbme(c->pci);
- snprint(name, sizeof name, "%s (%s)", s->name, s->ifc->name);
- intrenable(c->pci->intl, iainterrupt, c, c->pci->tbdf, name);
- /* supposed to squelch leftover interrupts here. */
- ahcienable(c->hba);
- c->enabled = 1;
- }
- iunlock(c);
- return 1;
- }
- static int
- iadisable(SDev *s)
- {
- char name[32];
- Ctlr *c;
- c = s->ctlr;
- ilock(c);
- ahcidisable(c->hba);
- snprint(name, sizeof name, "%s (%s)", s->name, s->ifc->name);
- intrdisable(c->pci->intl, iainterrupt, c, c->pci->tbdf, name);
- c->enabled = 0;
- iunlock(c);
- return 1;
- }
- static int
- iaonline(SDunit *unit)
- {
- int r;
- Ctlr *c;
- Drive *d;
- c = unit->dev->ctlr;
- d = c->drive[unit->subno];
- r = 0;
- if(d->portm.feat & Datapi && d->mediachange){
- r = scsionline(unit);
- if(r > 0)
- d->mediachange = 0;
- return r;
- }
- ilock(d);
- if(d->mediachange){
- r = 2;
- d->mediachange = 0;
- /* devsd resets this after online is called; why? */
- unit->sectors = d->sectors;
- unit->secsize = 512;
- } else if(d->state == Dready)
- r = 1;
- iunlock(d);
- return r;
- }
- /* returns locked list! */
- static Alist*
- ahcibuild(Aportm *m, uchar *cmd, void *data, int n, vlong lba)
- {
- uchar *c, acmd, dir, llba;
- Alist *l;
- Actab *t;
- Aprdt *p;
- static uchar tab[2][2] = { 0xc8, 0x25, 0xca, 0x35, };
- dir = *cmd != 0x28;
- llba = m->feat&Dllba? 1: 0;
- acmd = tab[dir][llba];
- qlock(m);
- l = m->list;
- t = m->ctab;
- c = t->cfis;
- c[0] = 0x27;
- c[1] = 0x80;
- c[2] = acmd;
- c[3] = 0;
- c[4] = lba; /* sector lba low 7:0 */
- c[5] = lba >> 8; /* cylinder low lba mid 15:8 */
- c[6] = lba >> 16; /* cylinder hi lba hi 23:16 */
- c[7] = 0xa0 | 0x40; /* obsolete device bits + lba */
- if(llba == 0)
- c[7] |= (lba>>24) & 7;
- c[8] = lba >> 24; /* sector (exp) lba 31:24 */
- c[9] = lba >> 32; /* cylinder low (exp) lba 39:32 */
- c[10] = lba >> 48; /* cylinder hi (exp) lba 48:40 */
- c[11] = 0; /* features (exp); */
- c[12] = n; /* sector count */
- c[13] = n >> 8; /* sector count (exp) */
- c[14] = 0; /* r */
- c[15] = 0; /* control */
- *(ulong*)(c + 16) = 0;
- l->flags = 1<<16 | Lpref | 0x5; /* Lpref ?? */
- if(dir == Write)
- l->flags |= Lwrite;
- l->len = 0;
- l->ctab = PCIWADDR(t);
- l->ctabhi = 0;
- p = &t->prdt;
- p->dba = PCIWADDR(data);
- p->dbahi = 0;
- p->count = 1<<31 | (512*n - 2) | 1;
- return l;
- }
- static Alist*
- ahcibuildpkt(Aportm *m, SDreq *r, void *data, int n)
- {
- int fill, len;
- uchar *c;
- Alist *l;
- Actab *t;
- Aprdt *p;
- qlock(m);
- l = m->list;
- t = m->ctab;
- c = t->cfis;
- fill = m->feat&Datapi16? 16: 12;
- if((len = r->clen) > fill)
- len = fill;
- memmove(t->atapi, r->cmd, len);
- memset(t->atapi+len, 0, fill-len);
- c[0] = 0x27;
- c[1] = 0x80;
- c[2] = 0xa0;
- if(n != 0)
- c[3] = 1; /* dma */
- else
- c[3] = 0; /* features (exp); */
- c[4] = 0; /* sector lba low 7:0 */
- c[5] = n; /* cylinder low lba mid 15:8 */
- c[6] = n >> 8; /* cylinder hi lba hi 23:16 */
- c[7] = 0xa0; /* obsolete device bits */
- *(ulong*)(c + 8) = 0;
- *(ulong*)(c + 12) = 0;
- *(ulong*)(c + 16) = 0;
- l->flags = 1<<16 | Lpref | Latapi | 0x5;
- if(r->write != 0 && data)
- l->flags |= Lwrite;
- l->len = 0;
- l->ctab = PCIWADDR(t);
- l->ctabhi = 0;
- if(data == 0)
- return l;
- p = &t->prdt;
- p->dba = PCIWADDR(data);
- p->dbahi = 0;
- p->count = 1<<31 | (n - 2) | 1;
- return l;
- }
- static int
- waitready(Drive *d)
- {
- ulong s, i, δ;
- for(i = 0; i < 15000; i += 250){
- if(d->state == Dreset || d->state == Dportreset ||
- d->state == Dnew)
- return 1;
- δ = MACHP(0)->ticks - d->lastseen;
- if(d->state == Dnull || δ > 10*1000)
- return -1;
- ilock(d);
- s = d->port->sstatus;
- iunlock(d);
- if((s & 0x700) == 0 && δ > 1500)
- return -1; /* no detect */
- if(d->state == Dready && (s & 7) == 3)
- return 0; /* ready, present & phy. comm. */
- esleep(250);
- }
- print("%s: not responding; offline\n", d->unit->name);
- ilock(d);
- d->state = Doffline;
- iunlock(d);
- return -1;
- }
- static int
- lockready(Drive *d)
- {
- int i;
- qlock(&d->portm);
- while ((i = waitready(d)) == 1) {
- qunlock(&d->portm);
- esleep(1);
- qlock(&d->portm);
- }
- return i;
- }
- static int
- flushcache(Drive *d)
- {
- int i;
- i = -1;
- if(lockready(d) == 0)
- i = ahciflushcache(&d->portc);
- qunlock(&d->portm);
- return i;
- }
- static int
- iariopkt(SDreq *r, Drive *d)
- {
- int n, count, try, max, flag, task;
- char *name;
- uchar *cmd, *data;
- Aport *p;
- Asleep as;
- cmd = r->cmd;
- name = d->unit->name;
- p = d->port;
- aprint("%02ux %02ux %c %d %p\n", cmd[0], cmd[2], "rw"[r->write],
- r->dlen, r->data);
- if(cmd[0] == 0x5a && (cmd[2] & 0x3f) == 0x3f)
- return sdmodesense(r, cmd, d->info, sizeof d->info);
- r->rlen = 0;
- count = r->dlen;
- max = 65536;
- try = 0;
- retry:
- data = r->data;
- n = count;
- if(n > max)
- n = max;
- d->active++;
- ahcibuildpkt(&d->portm, r, data, n);
- switch(waitready(d)){
- case -1:
- qunlock(&d->portm);
- return SDeio;
- case 1:
- qunlock(&d->portm);
- esleep(1);
- goto retry;
- }
- ilock(d);
- d->portm.flag = 0;
- iunlock(d);
- p->ci = 1;
- as.p = p;
- as.i = 1;
- d->intick = MACHP(0)->ticks;
- while(waserror())
- ;
- sleep(&d->portm, ahciclear, &as);
- poperror();
- ilock(d);
- flag = d->portm.flag;
- task = d->port->task;
- iunlock(d);
- if(task & (Efatal<<8) || task & (ASbsy|ASdrq) && d->state == Dready){
- d->port->ci = 0; /* clearci? */
- ahcirecover(&d->portc);
- task = d->port->task;
- flag &= ~Fdone; /* either an error or do-over */
- }
- d->active--;
- qunlock(&d->portm);
- if(flag == 0){
- if(++try == 10){
- print("%s: bad disk\n", name);
- r->status = SDcheck;
- return SDcheck;
- }
- print("%s: retry\n", name);
- goto retry;
- }
- if(flag & Ferror){
- if((task&Eidnf) == 0)
- print("%s: i/o error %ux\n", name, task);
- r->status = SDcheck;
- return SDcheck;
- }
- data += n;
- r->rlen = data - (uchar*)r->data;
- r->status = SDok;
- return SDok;
- }
- static int
- iario(SDreq *r)
- {
- int i, n, count, try, max, flag, task;
- vlong lba;
- char *name;
- uchar *cmd, *data;
- Aport *p;
- Asleep as;
- Ctlr *c;
- Drive *d;
- SDunit *unit;
- unit = r->unit;
- c = unit->dev->ctlr;
- d = c->drive[unit->subno];
- if(d->portm.feat & Datapi)
- return iariopkt(r, d);
- cmd = r->cmd;
- name = d->unit->name;
- p = d->port;
- if(r->cmd[0] == 0x35 || r->cmd[0] == 0x91){
- if(flushcache(d) == 0)
- return sdsetsense(r, SDok, 0, 0, 0);
- return sdsetsense(r, SDcheck, 3, 0xc, 2);
- }
- if((i = sdfakescsi(r, d->info, sizeof d->info)) != SDnostatus){
- r->status = i;
- return i;
- }
- if(*cmd != 0x28 && *cmd != 0x2a){
- print("%s: bad cmd 0x%.2ux\n", name, cmd[0]);
- r->status = SDcheck;
- return SDcheck;
- }
- lba = cmd[2]<<24 | cmd[3]<<16 | cmd[4]<<8 | cmd[5];
- count = cmd[7]<<8 | cmd[8];
- if(r->data == nil)
- return SDok;
- if(r->dlen < count * unit->secsize)
- count = r->dlen / unit->secsize;
- max = 128;
- try = 0;
- retry:
- data = r->data;
- while(count > 0){
- n = count;
- if(n > max)
- n = max;
- d->active++;
- ahcibuild(&d->portm, cmd, data, n, lba);
- switch(waitready(d)){
- case -1:
- qunlock(&d->portm);
- return SDeio;
- case 1:
- qunlock(&d->portm);
- esleep(1);
- goto retry;
- }
- ilock(d);
- d->portm.flag = 0;
- iunlock(d);
- p->ci = 1;
- as.p = p;
- as.i = 1;
- d->intick = MACHP(0)->ticks;
- while(waserror())
- ;
- sleep(&d->portm, ahciclear, &as);
- poperror();
- ilock(d);
- flag = d->portm.flag;
- task = d->port->task;
- iunlock(d);
- if(task & (Efatal<<8) ||
- task & (ASbsy|ASdrq) && d->state == Dready){
- d->port->ci = 0; /* @? */
- ahcirecover(&d->portc);
- task = d->port->task;
- }
- d->active--;
- qunlock(&d->portm);
- if(flag == 0){
- if(++try == 10){
- print("%s: bad disk\n", name);
- r->status = SDeio;
- return SDeio;
- }
- iprint("%s: retry %lld\n", name, lba);
- goto retry;
- }
- if(flag & Ferror){
- iprint("%s: i/o error %ux @%,lld\n", name, task, lba);
- r->status = SDeio;
- return SDeio;
- }
- count -= n;
- lba += n;
- data += n * unit->secsize;
- }
- r->rlen = data - (uchar*)r->data;
- r->status = SDok;
- return SDok;
- }
- /*
- * configure drives 0-5 as ahci sata (c.f. errata)
- */
- static int
- iaahcimode(Pcidev *p)
- {
- dprint("iaahcimode %ux %ux %ux\n", pcicfgr8(p, 0x91), pcicfgr8(p, 92),
- pcicfgr8(p, 93));
- pcicfgw16(p, 0x92, pcicfgr32(p, 0x92) | 0xf); /* ports 0-3 */
- // pcicfgw8(p, 0x93, pcicfgr32(p, 9x93) | 3); /* ports 4-5 */
- return 0;
- }
- static void
- iasetupahci(Ctlr *c)
- {
- /* disable cmd block decoding. */
- pcicfgw16(c->pci, 0x40, pcicfgr16(c->pci, 0x40) & ~(1<<15));
- pcicfgw16(c->pci, 0x42, pcicfgr16(c->pci, 0x42) & ~(1<<15));
- c->lmmio[0x4/4] |= 1 << 31; /* enable ahci mode (ghc register) */
- c->lmmio[0xc/4] = (1 << 6) - 1; /* 5 ports. (supposedly ro pi reg.) */
- /* enable ahci mode; from ich9 datasheet */
- pcicfgw8(c->pci, 0x90, 1<<6 | 1<<5);
- }
- static SDev*
- iapnp(void)
- {
- int i, n, nunit, type;
- ulong io;
- Ctlr *c;
- Drive *d;
- Pcidev *p;
- SDev *head, *tail, *s;
- static int done;
- if(done++)
- return nil;
- p = nil;
- head = tail = nil;
- loop:
- while((p = pcimatch(p, 0, 0)) != nil){
- /* 0x27c4 is the intel 82801 in compatibility (not sata) mode */
- if(p->vid == 0x8086 && (p->did & 0xfffc) == 0x2680)
- type = Tesb;
- else if(p->vid == 0x8086 &&
- (p->did == 0x27c5 || p->did == 0x27c0))
- type = Tich; /* 82801g[bh]m?; compat mode fails */
- else if(p->vid == 0x8086 && (p->did & 0xfeff) == 0x2829)
- type = Tich; /* ich8 */
- else if(p->vid == 0x8086 && (p->did & 0xfffe) == 0x2922)
- type = Tich; /* ich8 */
- else if(p->vid == 0x1002 && p->did == 0x4380)
- type = Tsb600;
- else
- continue;
- if (p->mem[Abar].bar == 0)
- continue;
- if(niactlr == NCtlr){
- print("%spnp: too many controllers\n", tname[type]);
- break;
- }
- c = iactlr + niactlr;
- s = sdevs + niactlr;
- memset(c, 0, sizeof *c);
- memset(s, 0, sizeof *s);
- io = p->mem[Abar].bar & ~0xf;
- c->mmio = vmap(io, p->mem[Abar].size);
- if(c->mmio == 0){
- print("%s: address 0x%luX in use did=%x\n",
- Tname(c), io, p->did);
- continue;
- }
- c->lmmio = (ulong*)c->mmio;
- c->pci = p;
- c->type = type;
- s->ifc = &sdiahciifc;
- s->idno = 'E' + niactlr;
- s->ctlr = c;
- c->sdev = s;
- if(Intel(c->type) && p->did != 0x2681)
- iasetupahci(c);
- nunit = ahciconf(c);
- // ahcihbareset((Ahba*)c->mmio);
- if(Intel(c->type) && iaahcimode(p) == -1)
- break;
- if(nunit < 1){
- vunmap(c->mmio, p->mem[Abar].size);
- continue;
- }
- c->ndrive = s->nunit = nunit;
- i = (c->hba->cap >> 21) & 1;
- print("#S/sd%c: %s: sata-%s with %d ports\n", s->idno,
- Tname(c), "I\0II" + i*2, nunit);
- /* map the drives -- they don't all need to be enabled. */
- memset(c->rawdrive, 0, sizeof c->rawdrive);
- n = 0;
- for(i = 0; i < NCtlrdrv; i++) {
- d = c->rawdrive + i;
- d->portno = i;
- d->driveno = -1;
- d->sectors = 0;
- d->ctlr = c;
- if((c->hba->pi & (1<<i)) == 0)
- continue;
- d->port = (Aport*)(c->mmio + 0x80*i + 0x100);
- d->portc.p = d->port;
- d->portc.m = &d->portm;
- d->driveno = n++;
- c->drive[d->driveno] = d;
- iadrive[niadrive + d->driveno] = d;
- }
- for(i = 0; i < n; i++)
- if(ahciidle(c->drive[i]->port) == -1){
- dprint("%s: port %d wedged; abort\n",
- Tname(c), i);
- goto loop;
- }
- for(i = 0; i < n; i++){
- c->drive[i]->mode = DMsatai;
- configdrive(c->drive[i]);
- }
- niadrive += nunit;
- niactlr++;
- if(head)
- tail->next = s;
- else
- head = s;
- tail = s;
- }
- return head;
- }
- static char* smarttab[] = {
- "unset",
- "error",
- "threshold exceeded",
- "normal"
- };
- static char *
- pflag(char *s, char *e, uchar f)
- {
- uchar i;
- for(i = 0; i < 8; i++)
- if(f & (1 << i))
- s = seprint(s, e, "%s ", flagname[i]);
- return seprint(s, e, "\n");
- }
- static int
- iarctl(SDunit *u, char *p, int l)
- {
- char buf[32];
- char *e, *op;
- Aport *o;
- Ctlr *c;
- Drive *d;
- if((c = u->dev->ctlr) == nil)
- return 0;
- d = c->drive[u->subno];
- o = d->port;
- e = p+l;
- op = p;
- if(d->state == Dready){
- p = seprint(p, e, "model\t%s\n", d->model);
- p = seprint(p, e, "serial\t%s\n", d->serial);
- p = seprint(p, e, "firm\t%s\n", d->firmware);
- if(d->smartrs == 0xff)
- p = seprint(p, e, "smart\tenable error\n");
- else if(d->smartrs == 0)
- p = seprint(p, e, "smart\tdisabled\n");
- else
- p = seprint(p, e, "smart\t%s\n",
- smarttab[d->portm.smart]);
- p = seprint(p, e, "flag\t");
- p = pflag(p, e, d->portm.feat);
- }else
- p = seprint(p, e, "no disk present [%s]\n", diskstates[d->state]);
- serrstr(o->serror, buf, buf + sizeof buf - 1);
- p = seprint(p, e, "reg\ttask %lux cmd %lux serr %lux %s ci %lux is %lux; "
- "sig %lux sstatus %04lux\n", o->task, o->cmd, o->serror, buf,
- o->ci, o->isr, o->sig, o->sstatus);
- p = seprint(p, e, "geometry %llud 512\n", d->sectors);
- return p - op;
- }
- static void
- runflushcache(Drive *d)
- {
- long t0;
- t0 = MACHP(0)->ticks;
- if(flushcache(d) != 0)
- error(Eio);
- dprint("flush in %ldms\n", MACHP(0)->ticks - t0);
- }
- static void
- forcemode(Drive *d, char *mode)
- {
- int i;
- for(i = 0; i < nelem(modename); i++)
- if(strcmp(mode, modename[i]) == 0)
- break;
- if(i == nelem(modename))
- i = 0;
- ilock(d);
- d->mode = i;
- iunlock(d);
- }
- static void
- runsmartable(Drive *d, int i)
- {
- if(waserror()){
- qunlock(&d->portm);
- d->smartrs = 0;
- nexterror();
- }
- if(lockready(d) == -1)
- error(Eio);
- d->smartrs = smart(&d->portc, i);
- d->portm.smart = 0;
- qunlock(&d->portm);
- poperror();
- }
- static void
- forcestate(Drive *d, char *state)
- {
- int i;
- for(i = 0; i < nelem(diskstates); i++)
- if(strcmp(state, diskstates[i]) == 0)
- break;
- if(i == nelem(diskstates))
- i = 0;
- ilock(d);
- d->state = i;
- if(i == Dnull){
- d->mediachange = 1;
- if(d->unit)
- d->unit->sectors = 0; /* force disk to disappear. */
- }
- iunlock(d);
- }
- static int
- iawctl(SDunit *u, Cmdbuf *cmd)
- {
- char **f;
- Ctlr *c;
- Drive *d;
- uint i;
- c = u->dev->ctlr;
- d = c->drive[u->subno];
- f = cmd->f;
- if(strcmp(f[0], "flushcache") == 0)
- runflushcache(d);
- else if(strcmp(f[0], "identify") == 0){
- i = strtoul(f[1]? f[1]: "0", 0, 0);
- if(i > 0xff)
- i = 0;
- dprint("%04d %ux\n", i, d->info[i]);
- }else if(strcmp(f[0], "mode") == 0)
- forcemode(d, f[1]? f[1]: "satai");
- else if(strcmp(f[0], "nop") == 0){
- if((d->portm.feat & Dnop) == 0){
- cmderror(cmd, "no drive support");
- return -1;
- }
- if(waserror()){
- qunlock(&d->portm);
- nexterror();
- }
- if(lockready(d) == -1)
- error(Eio);
- nop(&d->portc);
- qunlock(&d->portm);
- poperror();
- }else if(strcmp(f[0], "reset") == 0)
- forcestate(d, "reset");
- else if(strcmp(f[0], "smart") == 0){
- if(d->smartrs == 0){
- cmderror(cmd, "smart not enabled");
- return -1;
- }
- if(waserror()){
- qunlock(&d->portm);
- d->smartrs = 0;
- nexterror();
- }
- if(lockready(d) == -1)
- error(Eio);
- d->portm.smart = 2 + smartrs(&d->portc);
- qunlock(&d->portm);
- poperror();
- }else if(strcmp(f[0], "smartdisable") == 0)
- runsmartable(d, 1);
- else if(strcmp(f[0], "smartenable") == 0)
- runsmartable(d, 0);
- else if(strcmp(f[0], "state") == 0)
- forcestate(d, f[1]? f[1]: "null");
- else{
- cmderror(cmd, Ebadctl);
- return -1;
- }
- return 0;
- }
- static char *
- portr(char *p, char *e, uint x)
- {
- int i, a;
- p[0] = 0;
- a = -1;
- for(i = 0; i < 32; i++){
- if((x & (1<<i)) == 0){
- if(a != -1 && i - 1 != a)
- p = seprint(p, e, "-%d", i - 1);
- a = -1;
- continue;
- }
- if(a == -1){
- if(i > 0)
- p = seprint(p, e, ", ");
- p = seprint(p, e, "%d", a = i);
- }
- }
- if(a != -1 && i - 1 != a)
- p = seprint(p, e, "-%d", i - 1);
- return p;
- }
- /* must emit exactly one line per controller (sd(3)) */
- static char*
- iartopctl(SDev *sdev, char *p, char *e)
- {
- ulong cap;
- char pr[25];
- Ahba *hba;
- Ctlr *ctlr;
- #define has(x, str) if(cap & (x)) p = seprint(p, e, "%s ", (str))
- ctlr = sdev->ctlr;
- hba = ctlr->hba;
- p = seprint(p, e, "sd%c ahci port %#p: ", sdev->idno, hba);
- cap = hba->cap;
- has(Hs64a, "64a");
- has(Hsalp, "alp");
- has(Hsam, "am");
- has(Hsclo, "clo");
- has(Hcccs, "coal");
- has(Hems, "ems");
- has(Hsal, "led");
- has(Hsmps, "mps");
- has(Hsncq, "ncq");
- has(Hssntf, "ntf");
- has(Hspm, "pm");
- has(Hpsc, "pslum");
- has(Hssc, "slum");
- has(Hsss, "ss");
- has(Hsxs, "sxs");
- portr(pr, pr + sizeof pr, hba->pi);
- return seprint(p, e,
- "iss %ld ncs %ld np %ld; ghc %lux isr %lux pi %lux %s ver %lux\n",
- (cap>>20) & 0xf, (cap>>8) & 0x1f, 1 + (cap & 0x1f),
- hba->ghc, hba->isr, hba->pi, pr, hba->ver);
- #undef has
- }
- static int
- iawtopctl(SDev *, Cmdbuf *cmd)
- {
- int *v;
- char **f;
- f = cmd->f;
- v = 0;
- if(strcmp(f[0], "debug") == 0)
- v = &debug;
- else if(strcmp(f[0], "idprint") == 0)
- v = &prid;
- else if(strcmp(f[0], "aprint") == 0)
- v = &datapi;
- else
- cmderror(cmd, Ebadctl);
- switch(cmd->nf){
- default:
- cmderror(cmd, Ebadarg);
- case 1:
- *v ^= 1;
- break;
- case 2:
- *v = (strcmp(f[1], "on") == 0);
- break;
- }
- return 0;
- }
- SDifc sdiahciifc = {
- "iahci",
- iapnp,
- nil, /* legacy */
- iaenable,
- iadisable,
- iaverify,
- iaonline,
- iario,
- iarctl,
- iawctl,
- scsibio,
- nil, /* probe */
- nil, /* clear */
- iartopctl,
- iawtopctl,
- };
|