2 * intel/amd ahci sata controller
3 * copyright © 2007-10 coraid, inc.
7 #include "../port/lib.h"
12 #include "../port/error.h"
13 #include "../port/sd.h"
16 #include "../port/led.h"
18 #pragma varargck type "T" int
19 #define dprint(...) if(debug) print(__VA_ARGS__); else USED(debug)
20 #define idprint(...) if(prid) print(__VA_ARGS__); else USED(prid)
21 #define aprint(...) if(datapi) print(__VA_ARGS__); else USED(datapi)
22 #define ledprint(...) if(dled) print(__VA_ARGS__); else USED(dled)
23 #define Pciwaddrh(a) 0
24 #define Tname(c) tname[(c)->type]
25 #define Ticks MACHP(0)->ticks
26 #define MS2TK(t) (((ulong)(t)*HZ)/1000)
31 NDrive = NCtlr*NCtlrdrv,
38 Eesb = 1<<0, /* must have (Eesb & Emtype) == 0 */
41 /* pci space configuration */
56 static char *tname[] = {
76 static char *diskstates[Dlast] = {
87 extern SDifc sdiahciifc;
88 typedef struct Ctlr Ctlr;
98 static char *modes[DMlast] = {
105 typedef struct Htab Htab;
119 Aportc portc; /* redundant ptr to port and portm. */
142 * ahci allows non-sequential ports.
143 * to avoid this hassle, we let
144 * driveno ctlr*NCtlrdrv + unit
145 * portno nth available port
165 Drive rawdrive[NCtlrdrv];
166 Drive* drive[NCtlrdrv];
172 static Ctlr iactlr[NCtlr];
173 static SDev sdevs[NCtlr];
176 static Drive *iadrive[NDrive];
184 static char stab[] = {
186 [8] 't', 'c', 'p', 'e',
187 [16] 'N', 'I', 'W', 'B', 'D', 'C', 'H', 'S', 'T', 'F', 'X'
191 serrstr(ulong r, char *s, char *e)
196 for(i = 0; i < nelem(stab) && s < e; i++)
197 if(r & (1<<i) && stab[i]){
205 static char ntab[] = "0123456789abcdef";
208 preg(uchar *reg, int n)
210 char buf[25*3+1], *e;
214 for(i = 0; i < n; i++){
215 *e++ = ntab[reg[i] >> 4];
216 *e++ = ntab[reg[i] & 0xf];
225 dreg(char *s, Aport *p)
227 dprint("%stask=%lux; cmd=%lux; ci=%lux; is=%lux\n",
228 s, p->task, p->cmd, p->ci, p->isr);
236 tsleep(&up->sleep, return0, 0, ms);
251 return (s->p->ci & s->i) == 0;
255 aesleep(Aportm *m, Asleep *a, int ms)
259 tsleep(m, ahciclear, a, ms);
264 ahciwait(Aportc *c, int ms)
273 aesleep(c->m, &as, ms);
274 if((p->task & 1) == 0 && p->ci == 0)
276 dreg("ahciwait fail/timeout ", c->p);
281 mkalist(Aportm *m, uint flags, uchar *data, int len)
290 p->dba = PCIWADDR(data);
291 p->dbahi = Pciwaddrh(data);
292 p->count = 1<<31 | len - 2 | 1;
296 l->flags = flags | 0x5;
298 l->ctab = PCIWADDR(t);
299 l->ctabhi = Pciwaddrh(t);
308 if((pc->m->feat & Dnop) == 0)
310 c = pc->m->ctab->cfis;
312 mkalist(pc->m, Lwrite, 0, 0);
313 return ahciwait(pc, 3*1000);
317 setfeatures(Aportc *pc, uchar f, uint w)
321 c = pc->m->ctab->cfis;
322 featfis(pc->m, c, f);
323 mkalist(pc->m, Lwrite, 0, 0);
324 return ahciwait(pc, w);
328 * ata 7, required for sata, requires that all devices "support"
329 * udma mode 5, however sata:pata bridges allow older devices
330 * which may not. the innodisk satadom, for example allows
331 * only udma mode 2. on the assumption that actual udma is
332 * taking place on these bridges, we set the highest udma mode
333 * available, or pio if there is no udma mode available.
336 settxmode(Aportc *pc, uchar f)
340 c = pc->m->ctab->cfis;
341 if(txmodefis(pc->m, c, f) == -1)
343 mkalist(pc->m, Lwrite, 0, 0);
344 return ahciwait(pc, 3*1000);
350 if(up == nil || !islo())
357 ahciportreset(Aportc *c, uint mode)
365 for(i = 0; i < 500; i += 25){
366 if((*cmd & Acr) == 0)
370 if((*cmd & Apwr) != Apwr)
372 p->sctl = 3*Aipm | 0*Aspd | Adet;
374 p->sctl = 3*Aipm | mode*Aspd;
379 ahciflushcache(Aportc *pc)
383 c = pc->m->ctab->cfis;
384 flushcachefis(pc->m, c);
385 mkalist(pc->m, Lwrite, 0, 0);
387 if(ahciwait(pc, 60000) == -1 || pc->p->task & (1|32)){
388 dprint("ahciflushcache fail [task %lux]\n", pc->p->task);
389 // preg(pc->m->fis.r, 20);
396 ahciidentify0(Aportc *pc, void *id)
403 memset(id, 0, 0x200);
404 identifyfis(pc->m, c);
405 mkalist(pc->m, 0, id, 0x200);
406 return ahciwait(pc, 3*1000);
410 ahciidentify(Aportc *pc, ushort *id, uint *ss, char *d)
418 if(i > 5 || ahciidentify0(pc, id) != 0)
421 if(n & Pspinup && setfeatures(pc, 7, 20*1000) == -1)
422 print("%s: puis spinup fail\n", d);
425 print("%s: puis waiting\n", d);
429 if(s == -1 || (m->feat&Dlba) == 0){
430 if((m->feat&Dlba) == 0)
431 dprint("%s: no lba support\n", d);
444 for(i = 0; i < 500; i += 50){
451 if((a->task & (ASdrq|ASbsy)) == 0){
457 for(i = 0; i < 500; i += 50){
465 dprint("ahci: clo clear [task %lux]\n", a->task);
473 ahcicomreset(Aportc *pc)
477 dreg("comreset ", pc->p);
478 if(ahciquiet(pc->p) == -1){
479 dprint("ahci: ahciquiet failed\n");
482 dreg("comreset ", pc->p);
484 c = pc->m->ctab->cfis;
486 mkalist(pc->m, Lclear | Lreset, 0, 0);
487 if(ahciwait(pc, 500) == -1){
488 dprint("ahci: comreset1 failed\n");
492 dreg("comreset ", pc->p);
495 mkalist(pc->m, Lwrite, 0, 0);
496 if(ahciwait(pc, 150) == -1){
497 dprint("ahci: comreset2 failed\n");
500 dreg("comreset ", pc->p);
505 ahciidle(Aport *port)
514 for(i = 0; i < 500; i += 25){
524 for(i = 0; i < 500; i += 25){
533 * §6.2.2.1 first part; comreset handled by reset disk.
534 * - remainder is handled by configdisk.
535 * - ahcirecover is a quick recovery from a failed command.
538 ahciswreset(Aportc *pc)
546 if(pc->p->task & (ASdrq|ASbsy))
552 ahcirecover(Aportc *pc)
556 if(settxmode(pc, pc->m->udma) == -1)
562 malign(int size, int align)
566 v = xspanalloc(size, align, 0);
574 f->base = malign(0x100, 0x100);
576 f->p = f->base + 0x20;
577 f->r = f->base + 0x40;
578 f->u = f->base + 0x60;
579 f->devicebits = (ulong*)(f->base + 0x58);
583 ahciwakeup(Aportc *c, uint mode)
590 if((s & Isleepy) == 0)
592 if((s & Smask) != Spresent){
593 dprint("ahci: slumbering drive missing [ss %.3ux]\n", s);
596 ahciportreset(c, mode);
597 dprint("ahci: wake %.3ux -> %.3lux\n", s, c->p->sstatus);
601 ahciconfigdrive(Ahba *h, Aportc *c, int mode)
612 m->list = malign(sizeof *m->list, 1024);
613 m->ctab = malign(sizeof *m->ctab, 128);
616 if(ahciidle(p) == -1){
617 dprint("ahci: port not idle\n");
621 p->list = PCIWADDR(m->list);
622 p->listhi = Pciwaddrh(m->list);
623 p->fis = PCIWADDR(m->fis.base);
624 p->fishi = Pciwaddrh(m->fis.base);
628 if((p->cmd & Apwr) != Apwr)
631 if((h->cap & Hss) != 0){
632 dprint("ahci: spin up ... [%.3lux]\n", p->sstatus);
633 for(i = 0; i < 1400; i += 50){
634 if((p->sstatus & Sbist) != 0)
636 if((p->sstatus & Smask) == Sphylink)
642 if((p->sstatus & SSmask) == (Isleepy | Spresent))
648 /* we will get called again once phylink has been established */
649 if((p->sstatus & Smask) != Sphylink)
652 /* disable power managment sequence from book. */
653 p->sctl = 3*Aipm | mode*Aspd | 0*Adet;
656 p->cmd |= Afre | Ast;
681 for(i = 0; i < 32; i++)
693 h = c->hba = (Ahba*)c->mmio;
699 return countbits(h->pi);
707 if((h->cap2 & Boh) == 0)
710 for(wait = 0; wait < 2000; wait += 100){
711 if((h->bios & Bos) == 0)
715 iprint("ahci: bios handoff timed out\n");
720 ahcihbareset(Ahba *h)
725 for(wait = 0; wait < 1000; wait += 100){
739 if(d->unit && d->unit->name)
753 s = ahciidentify(&d->portc, id, &d->secsize, dnam(d));
756 osectors = d->sectors;
757 memmove(oserial, d->serial, sizeof d->serial);
761 idmove(d->serial, id+10, 20);
762 idmove(d->firmware, id+23, 8);
763 idmove(d->model, id+27, 40);
764 d->wwn = idwwn(d->portc.m, id);
767 memset(u->inquiry, 0, sizeof u->inquiry);
770 u->inquiry[4] = sizeof u->inquiry - 4;
771 memmove(u->inquiry+8, d->model, 40);
773 if(osectors != s || memcmp(oserial, d->serial, sizeof oserial)){
793 return c->pci->vid == 0x8086;
797 ignoreahdrs(Drive *d)
799 return d->portm.feat & Datapi && d->ctlr->type == Tsb600;
803 updatedrive(Drive *d)
805 ulong f, cause, serr, s0, pr, ewake;
814 if(d->ctlr->type == Tjmicron)
822 }else if(cause & Adps){
824 }else if(cause & Atfes){
831 dprint("%s: fatal\n", dnam(d));
835 if(ignoreahdrs(d) && serr & ErrE)
837 dprint("%s: Adhrs cause %lux serr %lux task %lux\n",
838 dnam(d), cause, serr, p->task);
844 if(p->task & 1 && last != cause)
845 dprint("%s: err ca %lux serr %lux task %lux sstat %.3lux\n",
846 dnam(d), cause, serr, p->task, p->sstatus);
848 dprint("%s: upd %lux ta %lux\n", dnam(d), cause, p->task);
850 if(cause & (Aprcs|Aifs)){
852 switch(p->sstatus & Smask){
857 if((p->sstatus & Imask) == Islumber)
863 /* power mgnt crap for suprise removal */
864 p->ie |= Aprcs|Apcs; /* is this required? */
871 dprint("%s: updatedrive: %s → %s [ss %.3lux]\n",
872 dnam(d), diskstates[s0], diskstates[d->state], p->sstatus);
873 if(s0 == Dready && d->state != Dready)
874 dprint("%s: pulled\n", dnam(d));
875 if(d->state != Dready)
877 if(d->state != Dready || p->ci)
891 dstatus(Drive *d, int s)
893 dprint("%s: dstatus: %s → %s from pc=%p\n", dnam(d),
894 diskstates[d->state], diskstates[s], getcallerpc(&d));
902 configdrive(Drive *d)
904 if(ahciconfigdrive(d->ctlr->hba, &d->portc, d->mode) == -1){
905 dstatus(d, Dportreset);
910 switch(d->port->sstatus & Smask){
916 if(d->state == Dnull)
917 d->state = Dportreset;
920 if(d->state == Dready)
931 dprint("%s: configdrive: %s\n", dnam(d), diskstates[d->state]);
937 uint state, det, stat;
942 stat = p->sstatus & Smask;
943 state = (p->cmd>>28) & 0xf;
944 dprint("%s: resetdisk [icc %ux; det %.3ux; sdet %.3ux]\n", dnam(d), state, det, stat);
947 if(d->state != Dready && d->state != Dnew)
948 d->portm.flag |= Ferror;
950 d->state = Dportreset;
953 clearci(p); /* satisfy sleep condition. */
961 if(p->cmd&Ast && ahciswreset(&d->portc) == -1)
962 dstatus(d, Dportreset); /* get a bigger stick. */
979 setfissig(m, c->p->sig);
980 if(identify(d) == -1){
981 dprint("%s: identify failure\n", dnam(d));
984 if(settxmode(c, m->udma) == -1){
985 dprint("%s: can't set udma mode\n", dnam(d));
988 if(m->feat & Dpower && setfeatures(c, 0x85, 3*1000) == -1){
989 dprint("%s: can't disable apm\n", dnam(d));
991 if(ahcirecover(c) == -1)
1000 idprint("%s: %sLBA %,lld sectors\n", dnam(d), s, d->sectors);
1001 idprint(" %s %s %s %s\n", d->model, d->firmware, d->serial,
1002 d->drivechange? "[newdrive]": "");
1006 idprint("%s: can't be initialized\n", dnam(d));
1014 Mphywait = 2*1024/Nms - 1,
1015 Midwait = 16*1024/Nms - 1,
1016 Mcomrwait = 64*1024/Nms - 1,
1022 if(d->active && d->totick != 0 && (long)(Ticks - d->totick) > 0){
1023 dprint("%s: drive hung [task %lux; ci %lux; serr %lux]%s\n",
1024 dnam(d), d->port->task, d->port->ci, d->port->serror,
1025 d->nodma == 0 ? "; disabling dma" : "");
1031 static ushort olds[NCtlr*NCtlrdrv];
1034 doportreset(Drive *d)
1037 ahciportreset(&d->portc, d->mode);
1040 dprint("ahci: portreset: %s [task %lux; ss %.3lux]\n",
1041 diskstates[d->state], d->port->task, d->port->sstatus);
1044 /* drive must be locked */
1046 statechange(Drive *d)
1052 if(d->unit->sectors != 0){
1064 return (c->hba->cap & 0xf*Hiss)/Hiss;
1067 static void iainterrupt(Ureg*, void *);
1070 checkdrive(Drive *d, int i)
1074 if(d->ctlr->enabled == 0)
1077 iainterrupt(0, d->ctlr); /* check for missed irq's */
1080 s = d->port->sstatus;
1082 d->lastseen = Ticks;
1084 dprint("%s: status: %.3ux -> %.3ux: %s\n",
1085 dnam(d), olds[i], s, diskstates[d->state]);
1096 switch(s & (Iactive|Smask)){
1098 ahciwakeup(&d->portc, d->mode);
1102 dprint("%s: unknown status %.3ux\n", dnam(d), s);
1104 case Iactive: /* active, no device */
1105 if(++d->wait&Mphywait)
1109 d->mode = maxmode(d->ctlr);
1112 if(d->mode == DMautoneg){
1113 d->state = Dportreset;
1116 dprint("%s: reset; new mode %s\n", dnam(d),
1122 case Iactive | Sphylink:
1125 if((++d->wait&Midwait) == 0){
1126 dprint("%s: slow reset [task %lux; ss %.3ux; wait %d]\n",
1127 dnam(d), d->port->task, s, d->wait);
1130 s = (uchar)d->port->task;
1131 sig = d->port->sig >> 16;
1132 if(s == 0x7f || s&ASbsy ||
1133 (sig != 0xeb14 && (s & ASdrdy) == 0))
1142 if(d->wait++ & Mcomrwait)
1147 dprint("%s: reset [%s]: mode %d; status %.3ux\n",
1148 dnam(d), diskstates[d->state], d->mode, s);
1155 if(d->wait++ & 0xff && (s & Iactive) == 0)
1157 dprint("%s: portreset [%s]: mode %d; status %.3ux\n",
1158 dnam(d), diskstates[d->state], d->mode, s);
1159 d->portm.flag |= Ferror;
1162 if((s & Smask) == Smissing){
1163 d->state = Dmissing;
1183 tsleep(&up->sleep, return0, 0, Nms);
1184 for(i = 0; i < niadrive; i++)
1185 checkdrive(iadrive[i], i);
1190 iainterrupt(Ureg *u, void *a)
1199 cause = c->hba->isr;
1200 for(i = 0; cause; i++){
1202 if((cause & m) == 0)
1205 d = c->rawdrive + i;
1207 if(d->port != nil && d->port->isr && c->hba->pi & m)
1218 ahciencreset(Ctlr *c)
1222 if(c->enctype == Eesb)
1226 while(h->emctl & Emrst)
1232 * from the standard: (http://en.wikipedia.org/wiki/IBPI)
1233 * rebuild is preferred as locate+fail; alternate 1hz fail
1234 * we're going to assume no locate led.
1238 Ledsleep = 125, /* 8hz */
1241 L0 = Ledon*Aled | Ledon*Locled,
1242 L1 = Ledon*Aled | Ledoff*Locled,
1243 R0 = Ledon*Aled | Ledon*Locled | Ledon*Errled,
1244 R1 = Ledon*Aled | Ledoff*Errled,
1245 S0 = Ledon*Aled | Ledon*Locled /*| Ledon*Errled*/, /* botch */
1246 S1 = Ledon*Aled | Ledoff*Errled,
1247 P0 = Ledon*Aled | Ledon*Errled,
1248 P1 = Ledon*Aled | Ledoff*Errled,
1249 F0 = Ledon*Aled | Ledon*Errled,
1250 C0 = Ledon*Aled | Ledon*Locled,
1251 C1 = Ledon*Aled | Ledoff*Locled,
1255 //static ushort led3[Ibpilast*8] = {
1256 //[Ibpinone*8] 0, 0, 0, 0, 0, 0, 0, 0,
1257 //[Ibpinormal*8] N0, N0, N0, N0, N0, N0, N0, N0,
1258 //[Ibpirebuild*8] R0, R0, R0, R0, R1, R1, R1, R1,
1259 //[Ibpilocate*8] L0, L1, L0, L1, L0, L1, L0, L1,
1260 //[Ibpispare*8] S0, S1, S0, S1, S1, S1, S1, S1,
1261 //[Ibpipfa*8] P0, P1, P0, P1, P1, P1, P1, P1, /* first 1 sec */
1262 //[Ibpifail*8] F0, F0, F0, F0, F0, F0, F0, F0,
1263 //[Ibpicritarray*8] C0, C0, C0, C0, C1, C1, C1, C1,
1264 //[Ibpifailarray*8] C0, C1, C0, C1, C0, C1, C0, C1,
1267 static ushort led2[Ibpilast*8] = {
1268 [Ibpinone*8] 0, 0, 0, 0, 0, 0, 0, 0,
1269 [Ibpinormal*8] N0, N0, N0, N0, N0, N0, N0, N0,
1270 [Ibpirebuild*8] R0, R0, R0, R0, R1, R1, R1, R1,
1271 [Ibpilocate*8] L0, L0, L0, L0, L0, L0, L0, L0,
1272 [Ibpispare*8] S0, S0, S0, S0, S1, S1, S1, S1,
1273 [Ibpipfa*8] P0, P1, P0, P1, P1, P1, P1, P1, /* first 1 sec */
1274 [Ibpifail*8] F0, F0, F0, F0, F0, F0, F0, F0,
1275 [Ibpicritarray*8] C0, C0, C0, C0, C1, C1, C1, C1,
1276 [Ibpifailarray*8] C0, C1, C0, C1, C0, C1, C0, C1,
1280 ledstate(Ledport *p, uint seq)
1284 if(p->led == Ibpipfa && seq%32 >= 8)
1287 i = led2[8*p->led + seq%8];
1288 if(i != p->ledbits){
1290 ledprint("ledstate %,.011ub %ud\n", p->ledbits, seq);
1297 blink(Drive *d, ulong t)
1303 if(ledstate(d, t) == 0)
1307 /* ensure last message has been transmitted */
1308 while(h->emctl & Tmsg)
1312 panic("%s: bad led type %d", dnam(d), c->enctype);
1314 memset(&msg, 0, sizeof msg);
1317 msg.msize = sizeof msg - 4;
1318 msg.led[0] = d->ledbits;
1319 msg.led[1] = d->ledbits>>8;
1321 msg.hba = d->driveno;
1322 memmove(c->enctx, &msg, sizeof msg);
1330 Esbdrv0 = 4, /* start pos in bits */
1331 Esbiota = 3, /* shift in bits */
1340 uint i, e; /* except after c */
1343 for(i = 0; i < 3; i++)
1344 e |= ((s>>3*i & 7) != 0)<<i;
1349 blinkesb(Ctlr *c, ulong t)
1356 for(i = 0; i < c->ndrive; i++){
1358 s |= ledstate(d, t); /* no port mapping */
1362 memset(u, 0, sizeof u);
1363 for(i = 0; i < c->ndrive; i++){
1365 s = Esbdrv0 + Esbiota*i;
1366 v = esbbits(d->ledbits) * (1ull << s%32);
1368 u[s/32 + 1] |= v>>32;
1370 for(i = 0; i < c->encsz; i++)
1376 ahciledr(SDunit *u, Chan *ch, void *a, long n, vlong off)
1382 d = c->drive[u->subno];
1383 return ledr(d, ch, a, n, off);
1387 ahciledw(SDunit *u, Chan *ch, void *a, long n, vlong off)
1393 d = c->drive[u->subno];
1394 return ledw(d, ch, a, n, off);
1406 memset(map, 0, sizeof map);
1407 for(i = 0; i < niactlr; i++)
1408 if(iactlr[i].enctype != 0){
1409 ahciencreset(iactlr + i);
1414 pexit("no work", 1);
1415 for(i = 0; i < niadrive; i++){
1416 iadrive[i]->nled = 3; /* hardcoded */
1417 if(iadrive[i]->ctlr->enctype == Eesb)
1418 iadrive[i]->nled = 3;
1419 iadrive[i]->ledbits = -1;
1423 for(j = 0; j < niadrive; ){
1424 c = iadrive[j]->ctlr;
1427 else if(c->enctype == Eesb){
1436 esleep(Ledsleep - TK2MS(t1 - t0));
1445 for(i = 0;; i += 250){
1446 if(d->state == Dreset || d->state == Dportreset || d->state == Dnew)
1449 s = d->port->sstatus;
1450 if(d->state == Dready && (s & Smask) == Sphylink){
1454 δ = Ticks - d->lastseen;
1455 if(d->state == Dnull || δ > 10*1000)
1457 if((s & Imask) == 0 && δ > 1500)
1460 d->state = Doffline;
1462 print("%s: not responding; offline\n", dnam(d));
1479 d = c->drive[u->subno];
1485 sdaddfile(u, "led", 0644, eve, ahciledr, ahciledw);
1489 checkdrive(d, d->driveno); /* c->d0 + d->driveno */
1501 d = c->drive[u->subno];
1503 while(d->state != Dmissing && waitready(d) == 1)
1506 dprint("%s: iaonline: %s\n", dnam(d), diskstates[d->state]);
1509 if(d->portm.feat & Datapi){
1515 return scsionline(u);
1521 }else if(d->state == Dready)
1524 u->sectors = d->sectors;
1525 u->secsize = d->secsize;
1543 kproc("iasata", satakproc, 0);
1545 panic("iaenable: zero s->ctlr->ndrive");
1547 snprint(name, sizeof name, "%s (%s)", s->name, s->ifc->name);
1548 intrenable(c->pci->intl, iainterrupt, c, c->pci->tbdf, name);
1549 /* supposed to squelch leftover interrupts here. */
1552 if(++once == niactlr)
1553 kproc("ialed", ledkproc, 0);
1567 ahcidisable(c->hba);
1568 snprint(name, sizeof name, "%s (%s)", s->name, s->ifc->name);
1569 intrdisable(c->pci->intl, iainterrupt, c, c->pci->tbdf, name);
1576 ahcibuild(Drive *d, int rw, void *data, int nsect, vlong lba)
1584 rwfis(m, c, rw, nsect, lba);
1588 return mkalist(m, flags, data, nsect * d->secsize);
1592 ahcibuildpkt(Drive *d, SDreq *r, void *data, int n)
1603 atapirwfis(m, c, r->cmd, r->clen, 0x2000);
1604 if((n & 15) != 0 || d->nodma)
1605 c[Ffeat] &= ~1; /* use pio */
1606 else if(c[Ffeat] & 1 && d->info[62] & (1<<15)) /* dma direction */
1607 c[Ffeat] = (c[Ffeat] & ~(1<<2)) | ((r->write == 0) << 2);
1608 flags = Lpref | Latapi;
1609 if(r->write != 0 && data)
1611 return mkalist(m, flags, data, n);
1615 ahcibuildfis(Drive *d, SDreq *r, void *data, uint n)
1621 if((r->ataproto & Pprotom) == Ppkt)
1622 return ahcibuildpkt(d, r, data, n);
1626 memmove(c, r->cmd, r->clen);
1628 if(r->write || n == 0)
1630 return mkalist(m, flags, data, n);
1639 while ((i = waitready(d)) == 1) {
1648 flushcache(Drive *d)
1653 if(lockready(d) == 0)
1654 i = ahciflushcache(&d->portc);
1660 io(Drive *d, uint proto, int to, int interrupt)
1662 uint task, flag, stat, rv;
1666 switch(waitready(d)){
1683 d->totick = Ticks + MS2TK(to) | 1; /* fix fencepost */
1690 if(ahcicomreset(&d->portc) == -1)
1695 sleep(&d->portm, ahciclear, &as);
1701 flag = d->portm.flag;
1702 task = d->port->task;
1706 if(proto & Ppkt && stat == Dready){
1707 rv = task >> 8 + 4 & 0xf;
1710 }else if(task & (Efatal<<8) || task & (ASbsy|ASdrq) && stat == Dready){
1712 ahcirecover(&d->portc);
1713 task = d->port->task;
1714 flag &= ~Fdone; /* either an error or do-over */
1717 print("%s: retry\n", dnam(d));
1720 if(flag & (Fahdrs | Ferror)){
1721 if((task & Eidnf) == 0)
1722 print("%s: i/o error %ux\n", dnam(d), task);
1729 iariopkt(SDreq *r, Drive *d)
1736 aprint("%s: %.2ux %.2ux %c %d %p\n", dnam(d), cmd[0], cmd[2],
1737 "rw"[r->write], r->dlen, r->data);
1742 * prevent iaonline() to hang forever by timing out
1743 * inquiry and capacity commands after 5 seconds.
1747 case 0x9e: if(cmd[1] != 0x10) break;
1754 for(try = 0; try < 10; try++){
1756 l = ahcibuildpkt(d, r, r->data, r->dlen);
1757 r->status = io(d, Ppkt, to, 0);
1770 print("%s: bad disk\n", dnam(d));
1771 return r->status = SDcheck;
1775 ahcibio(SDunit *u, int lun, int write, void *a, long count, uvlong lba)
1777 int n, rw, try, status, max;
1783 d = c->drive[u->subno];
1784 if(d->portm.feat & Datapi)
1785 return scsibio(u, lun, write, a, count, lba);
1788 if(d->portm.feat & Dllba){
1789 max = 8192; /* ahci maximum */
1790 if(c->type == Tsb600)
1791 max = 255; /* errata */
1793 rw = write? SDwrite: SDread;
1795 dprint("%s: bio: %llud %c %lud %p\n",
1796 dnam(d), lba, "rw"[rw], count, data);
1798 for(try = 0; try < 10;){
1803 ahcibuild(d, rw, data, n, lba);
1804 status = io(d, Pdma, 5000, 0);
1816 data += n * u->secsize;
1818 return data - (uchar*)a;
1820 print("%s: bad disk\n", dnam(d));
1827 int i, n, count, rw;
1836 d = c->drive[u->subno];
1837 if(d->portm.feat & Datapi)
1838 return iariopkt(r, d);
1841 if(cmd[0] == 0x35 || cmd[0] == 0x91){
1842 if(flushcache(d) == 0)
1843 return sdsetsense(r, SDok, 0, 0, 0);
1844 return sdsetsense(r, SDcheck, 3, 0xc, 2);
1847 if((i = sdfakescsi(r)) != SDnostatus){
1852 if((i = sdfakescsirw(r, &lba, &count, &rw)) != SDnostatus)
1854 n = ahcibio(u, r->lun, r->write, r->data, count, lba);
1861 static uchar bogusrfis[16] = {
1874 memmove(c, bogusrfis, sizeof bogusrfis);
1879 sdr(SDreq *r, Drive *d, int st)
1884 if((r->ataproto & Pprotom) == Ppkt){
1887 st = t >> 8 + 4 & 0xf;
1890 memmove(r->cmd, c, 16);
1898 fisreqchk(Sfis *f, SDreq *r)
1900 if((r->ataproto & Pprotom) == Ppkt)
1903 * handle oob requests;
1904 * restrict & sanitize commands
1908 if(r->cmd[0] == 0xf0){
1909 sigtofis(f, r->cmd);
1930 d = c->drive[u->subno];
1932 if((r->status = fisreqchk(&d->portm, r)) != SDnostatus)
1936 for(try = 0; try < 10; try++){
1938 l = ahcibuildfis(d, r, r->data, r->dlen);
1939 r->status = io(d, r->ataproto & Pprotom, -1, 1);
1943 return sdsetsense(r, SDcheck, 11, 0, 6);
1951 r->rlen = (r->ataproto & Pprotom) == Ppkt ? l->len : r->dlen;
1952 try = sdr(r, d, r->status);
1956 print("%s: bad disk\n", dnam(d));
1957 return r->status = SDeio;
1961 * configure drives 0-5 as ahci sata (c.f. errata)
1964 iaahcimode(Pcidev *p)
1968 u = pcicfgr16(p, 0x92);
1969 dprint("ahci: %T: iaahcimode %.2ux %.4ux\n", p->tbdf, pcicfgr8(p, 0x91), u);
1970 pcicfgw16(p, 0x92, u | 0xf); /* ports 0-15 */
1975 Ghc = 0x04/4, /* global host control */
1976 Pi = 0x0c/4, /* ports implemented */
1977 Cmddec = 1<<15, /* enable command block decode */
1980 Ahcien = 1<<31, /* ahci enable */
1984 iasetupahci(Ctlr *c)
1986 pcicfgw16(c->pci, 0x40, pcicfgr16(c->pci, 0x40) & ~Cmddec);
1987 pcicfgw16(c->pci, 0x42, pcicfgr16(c->pci, 0x42) & ~Cmddec);
1989 c->lmmio[Ghc] |= Ahcien;
1990 c->lmmio[Pi] = (1 << 6) - 1; /* 5 ports (supposedly ro pi reg) */
1992 /* enable ahci mode; from ich9 datasheet */
1993 pcicfgw16(c->pci, 0x90, 1<<6 | 1<<5);
1997 sbsetupahci(Pcidev *p)
1999 print("sbsetupahci: tweaking %.4ux ccru %.2ux ccrp %.2ux\n",
2000 p->did, p->ccru, p->ccrp);
2001 pcicfgw8(p, 0x40, pcicfgr8(p, 0x40) | 1);
2002 pcicfgw8(p, PciCCRu, 6);
2003 pcicfgw8(p, PciCCRp, 1);
2012 c->enctx = (ulong*)(c->mmio + 0xa0);
2019 ahciencinit(Ctlr *c)
2021 ulong type, sz, o, *bar;
2027 if((h->cap & Hems) == 0)
2029 type = h->emctl & Emtype;
2041 sz = h->emloc & 0xffff;
2043 if(sz == 0 || o == 0)
2046 dprint("size = %.4lux; loc = %.4lux*4\n", sz, o);
2049 if((h->emctl & Xonly) == 0){
2053 c->encrx = bar + o*2;
2069 if((p->did & 0xffff) == 0x1e02)
2070 return Tich; /* c210 */
2071 if((p->did & 0xffff) == 0x24d1)
2072 return Tich; /* 82801eb/er */
2073 if((p->did & 0xffff) == 0x2653)
2074 return Tich; /* 82801fbm */
2075 if((p->did & 0xfffc) == 0x2680)
2077 if((p->did & 0xfffb) == 0x27c1)
2078 return Tich; /* 82801g[bh]m */
2079 if((p->did & 0xffff) == 0x2822)
2080 return Tich; /* 82801 SATA RAID */
2081 if((p->did & 0xffff) == 0x2821)
2082 return Tich; /* 82801h[roh] */
2083 if((p->did & 0xfffe) == 0x2824)
2084 return Tich; /* 82801h[b] */
2085 if((p->did & 0xfeff) == 0x2829)
2086 return Tich; /* ich8 */
2087 if((p->did & 0xfffe) == 0x2922)
2088 return Tich; /* ich9 */
2089 if((p->did & 0xffff) == 0x3a02)
2090 return Tich; /* 82801jd/do */
2091 if((p->did & 0xfefe) == 0x3a22)
2092 return Tich; /* ich10, pch */
2093 if((p->did & 0xfff7) == 0x3b28)
2094 return Tich; /* pchm */
2095 if((p->did & 0xfffe) == 0x3b22)
2096 return Tich; /* pch */
2099 if(p->ccru == 1 || p->ccrp != 1)
2100 if(p->did == 0x4380 || p->did == 0x4390)
2106 * unconfirmed report that the programming
2107 * interface is set incorrectly.
2109 if(p->did == 0x3349)
2113 /* Hudson SATA Controller [AHCI mode] */
2114 if(p->did == 0x7801)
2127 if(p->ccrb == Pcibcstore && p->ccru == 6 && p->ccrp == 1)
2135 int i, n, nunit, type;
2147 if(getconf("*noahci") != nil)
2150 if(getconf("*ahcidebug") != nil){
2155 memset(olds, 0xff, sizeof olds);
2157 while((p = pcimatch(p, 0, 0)) != nil){
2158 if((type = didtype(p)) == -1)
2160 if(p->mem[Abar].bar == 0)
2162 if(niactlr == NCtlr){
2163 print("iapnp: %s: too many controllers\n", tname[type]);
2166 c = iactlr + niactlr;
2167 s = sdevs + niactlr;
2168 memset(c, 0, sizeof *c);
2169 memset(s, 0, sizeof *s);
2170 io = p->mem[Abar].bar & ~0xf;
2171 c->mmio = vmap(io, p->mem[Abar].size);
2173 print("%s: address %#p in use did %.4ux\n",
2174 Tname(c), io, p->did);
2177 c->lmmio = (ulong*)c->mmio;
2181 s->ifc = &sdiahciifc;
2186 ahcihandoff((Ahba*)c->mmio);
2187 if(intel(c) && p->did != 0x2681)
2189 // ahcihbareset((Ahba*)c->mmio);
2190 nunit = ahciconf(c);
2191 if(intel(c) && iaahcimode(p) == -1 || nunit < 1){
2192 vunmap(c->mmio, p->mem[Abar].size);
2195 c->ndrive = s->nunit = nunit;
2197 /* map the drives -- they don't all need to be enabled. */
2198 memset(c->rawdrive, 0, sizeof c->rawdrive);
2200 for(i = 0; i < NCtlrdrv; i++){
2201 d = c->rawdrive + i;
2207 if((c->hba->pi & 1<<i) == 0)
2209 snprint(d->name, sizeof d->name, "iahci%d.%d", niactlr, i);
2210 d->port = (Aport*)(c->mmio + 0x80*i + 0x100);
2211 d->portc.p = d->port;
2212 d->portc.m = &d->portm;
2214 c->drive[d->driveno] = d;
2215 iadrive[niadrive + d->driveno] = d;
2217 for(i = 0; i < n; i++){
2218 c->drive[i]->mode = DMautoneg;
2219 configdrive(c->drive[i]);
2226 i = (c->hba->cap >> 21) & 1;
2227 print("#S/%s: %s: sata-%s with %d ports\n", s->name,
2228 Tname(c), "I\0II" + i*2, nunit);
2233 static Htab ctab[] = {
2255 capfmt(char *p, char *e, Htab *t, int n, ulong cap)
2260 for(i = 0; i < n; i++)
2262 p = seprint(p, e, "%s ", t[i].name);
2267 iarctl(SDunit *u, char *p, int l)
2269 char buf[32], *e, *op;
2274 if((c = u->dev->ctlr) == nil)
2276 d = c->drive[u->subno];
2281 if(d->state == Dready){
2282 p = seprint(p, e, "model\t%s\n", d->model);
2283 p = seprint(p, e, "serial\t%s\n", d->serial);
2284 p = seprint(p, e, "firm\t%s\n", d->firmware);
2286 p = seprint(p, e, "wwn\t%ullx\n", d->wwn);
2287 p = seprint(p, e, "flag\t");
2288 p = pflag(p, e, &d->portm);
2289 p = seprint(p, e, "udma\t%d\n", d->portm.udma);
2291 p = seprint(p, e, "no disk present [%s]\n", diskstates[d->state]);
2292 serrstr(o->serror, buf, buf + sizeof buf - 1);
2293 p = seprint(p, e, "reg\ttask %lux cmd %lux serr %lux %s ci %lux is %lux "
2294 "sig %lux sstatus %.3lux\n", o->task, o->cmd, o->serror, buf,
2295 o->ci, o->isr, o->sig, o->sstatus);
2296 p = seprint(p, e, "cmd\t");
2297 p = capfmt(p, e, ctab, nelem(ctab), o->cmd);
2298 p = seprint(p, e, "\n");
2299 p = seprint(p, e, "mode\t%s %s\n", modes[d->mode], modes[maxmode(c)]);
2300 p = seprint(p, e, "geometry %llud %d\n", d->sectors, d->secsize);
2301 p = seprint(p, e, "alignment %d %d\n",
2302 d->secsize<<d->portm.physshift, d->portm.physalign);
2303 p = seprint(p, e, "missirq\t%ud\n", c->missirq);
2308 forcemode(Drive *d, char *mode)
2312 for(i = 0; i < nelem(modes); i++)
2313 if(strcmp(mode, modes[i]) == 0)
2315 if(i == nelem(modes))
2323 forcestate(Drive *d, char *state)
2327 for(i = 0; i < nelem(diskstates); i++)
2328 if(strcmp(state, diskstates[i]) == 0)
2330 if(i == nelem(diskstates))
2336 runsettxmode(Drive *d, char *s)
2346 if(lockready(d) == 0){
2348 if(settxmode(c, m->udma) == 0)
2357 iawctl(SDunit *u, Cmdbuf *cmd)
2364 d = c->drive[u->subno];
2367 if(strcmp(f[0], "mode") == 0)
2368 forcemode(d, f[1]? f[1]: "satai");
2369 else if(strcmp(f[0], "state") == 0)
2370 forcestate(d, f[1]? f[1]: "null");
2371 else if(strcmp(f[0], "txmode") == 0){
2372 if(runsettxmode(d, f[1]? f[1]: "0"))
2373 cmderror(cmd, "bad txmode / stuck port");
2375 cmderror(cmd, Ebadctl);
2380 portr(char *p, char *e, uint x)
2386 for(i = 0; i < 32; i++){
2387 if((x & (1<<i)) == 0){
2388 if(a != -1 && i - 1 != a)
2389 p = seprint(p, e, "-%d", i - 1);
2395 p = seprint(p, e, ", ");
2396 p = seprint(p, e, "%d", a = i);
2399 if(a != -1 && i - 1 != a)
2400 p = seprint(p, e, "-%d", i - 1);
2404 static Htab htab[] = {
2424 static Htab htab2[] = {
2430 static Htab emtab[] = {
2442 iartopctl(SDev *s, char *p, char *e)
2452 p = seprint(p, e, "sd%c ahci %s port %#p: ", s->idno, Tname(c), h);
2453 p = capfmt(p, e, htab, nelem(htab), cap);
2454 p = capfmt(p, e, htab2, nelem(htab2), h->cap2);
2455 p = capfmt(p, e, emtab, nelem(emtab), h->emctl);
2456 portr(pr, pr + sizeof pr, h->pi);
2457 return seprint(p, e,
2458 "iss %ld ncs %ld np %ld ghc %lux isr %lux pi %lux %s ver %lux\n",
2459 (cap>>20) & 0xf, (cap>>8) & 0x1f, 1 + (cap & 0x1f),
2460 h->ghc, h->isr, h->pi, pr, h->ver);
2464 iawtopctl(SDev *, Cmdbuf *cmd)
2472 if(strcmp(f[0], "debug") == 0)
2474 else if(strcmp(f[0], "idprint") == 0)
2476 else if(strcmp(f[0], "aprint") == 0)
2478 else if(strcmp(f[0], "ledprint") == 0)
2481 cmderror(cmd, Ebadctl);
2485 cmderror(cmd, Ebadarg);
2491 *v = strcmp(f[1], "on") == 0;
2499 SDifc sdiahciifc = {