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. */
141 * ahci allows non-sequential ports.
142 * to avoid this hassle, we let
143 * driveno ctlr*NCtlrdrv + unit
144 * portno nth available port
164 Drive rawdrive[NCtlrdrv];
165 Drive* drive[NCtlrdrv];
169 static Ctlr iactlr[NCtlr];
170 static SDev sdevs[NCtlr];
173 static Drive *iadrive[NDrive];
181 static char stab[] = {
183 [8] 't', 'c', 'p', 'e',
184 [16] 'N', 'I', 'W', 'B', 'D', 'C', 'H', 'S', 'T', 'F', 'X'
188 serrstr(ulong r, char *s, char *e)
193 for(i = 0; i < nelem(stab) && s < e; i++)
194 if(r & (1<<i) && stab[i]){
202 static char ntab[] = "0123456789abcdef";
205 preg(uchar *reg, int n)
207 char buf[25*3+1], *e;
211 for(i = 0; i < n; i++){
212 *e++ = ntab[reg[i] >> 4];
213 *e++ = ntab[reg[i] & 0xf];
222 dreg(char *s, Aport *p)
224 dprint("%stask=%lux; cmd=%lux; ci=%lux; is=%lux\n",
225 s, p->task, p->cmd, p->ci, p->isr);
233 tsleep(&up->sleep, return0, 0, ms);
248 return (s->p->ci & s->i) == 0;
252 aesleep(Aportm *m, Asleep *a, int ms)
256 tsleep(m, ahciclear, a, ms);
261 ahciwait(Aportc *c, int ms)
270 aesleep(c->m, &as, ms);
271 if((p->task & 1) == 0 && p->ci == 0)
273 dreg("ahciwait fail/timeout ", c->p);
278 mkalist(Aportm *m, uint flags, uchar *data, int len)
286 l->flags = flags | 0x5;
288 l->ctab = PCIWADDR(t);
289 l->ctabhi = Pciwaddrh(t);
293 p->dba = PCIWADDR(data);
294 p->dbahi = Pciwaddrh(data);
295 p->count = 1<<31 | len - 2 | 1;
304 if((pc->m->feat & Dnop) == 0)
306 c = pc->m->ctab->cfis;
308 mkalist(pc->m, Lwrite, 0, 0);
309 return ahciwait(pc, 3*1000);
313 setfeatures(Aportc *pc, uchar f, uint w)
317 c = pc->m->ctab->cfis;
318 featfis(pc->m, c, f);
319 mkalist(pc->m, Lwrite, 0, 0);
320 return ahciwait(pc, w);
324 * ata 7, required for sata, requires that all devices "support"
325 * udma mode 5, however sata:pata bridges allow older devices
326 * which may not. the innodisk satadom, for example allows
327 * only udma mode 2. on the assumption that actual udma is
328 * taking place on these bridges, we set the highest udma mode
329 * available, or pio if there is no udma mode available.
332 settxmode(Aportc *pc, uchar f)
336 c = pc->m->ctab->cfis;
337 if(txmodefis(pc->m, c, f) == -1)
339 mkalist(pc->m, Lwrite, 0, 0);
340 return ahciwait(pc, 3*1000);
353 ahciportreset(Aportc *c, uint mode)
361 for(i = 0; i < 500; i += 25){
362 if((*cmd & Acr) == 0)
366 p->sctl = 3*Aipm | 0*Aspd | Adet;
368 p->sctl = 3*Aipm | mode*Aspd;
373 ahciflushcache(Aportc *pc)
377 c = pc->m->ctab->cfis;
378 flushcachefis(pc->m, c);
379 mkalist(pc->m, Lwrite, 0, 0);
381 if(ahciwait(pc, 60000) == -1 || pc->p->task & (1|32)){
382 dprint("ahciflushcache fail %lux\n", pc->p->task);
383 // preg(pc->m->fis.r, 20);
390 ahciidentify0(Aportc *pc, void *id)
397 memset(id, 0, 0x200);
398 identifyfis(pc->m, c);
399 mkalist(pc->m, 0, id, 0x200);
400 return ahciwait(pc, 3*1000);
404 ahciidentify(Aportc *pc, ushort *id, uint *ss, char *d)
412 if(i > 5 || ahciidentify0(pc, id) != 0)
415 if(n & Pspinup && setfeatures(pc, 7, 20*1000) == -1)
416 print("%s: puis spinup fail\n", d);
419 print("%s: puis waiting\n", d);
423 if(s == -1 || (m->feat&Dlba) == 0){
424 if((m->feat&Dlba) == 0)
425 dprint("%s: no lba support\n", d);
438 for(i = 0; i < 500; i += 50){
445 if((a->task & (ASdrq|ASbsy)) == 0){
451 for(i = 0; i < 500; i += 50){
459 dprint("ahci: clo clear %lux\n", a->task);
467 ahcicomreset(Aportc *pc)
471 dreg("comreset ", pc->p);
472 if(ahciquiet(pc->p) == -1){
473 dprint("ahci: ahciquiet failed\n");
476 dreg("comreset ", pc->p);
478 c = pc->m->ctab->cfis;
480 mkalist(pc->m, Lclear | Lreset, 0, 0);
481 if(ahciwait(pc, 500) == -1){
482 dprint("ahci: comreset1 failed\n");
486 dreg("comreset ", pc->p);
489 mkalist(pc->m, Lwrite, 0, 0);
490 if(ahciwait(pc, 150) == -1){
491 dprint("ahci: comreset2 failed\n");
494 dreg("comreset ", pc->p);
499 ahciidle(Aport *port)
508 for(i = 0; i < 500; i += 25){
518 for(i = 0; i < 500; i += 25){
527 * §6.2.2.1 first part; comreset handled by reset disk.
528 * - remainder is handled by configdisk.
529 * - ahcirecover is a quick recovery from a failed command.
532 ahciswreset(Aportc *pc)
540 if(pc->p->task & (ASdrq|ASbsy))
546 ahcirecover(Aportc *pc)
550 if(settxmode(pc, pc->m->udma) == -1)
556 malign(int size, int align)
560 v = xspanalloc(size, align, 0);
568 f->base = malign(0x100, 0x100);
570 f->p = f->base + 0x20;
571 f->r = f->base + 0x40;
572 f->u = f->base + 0x60;
573 f->devicebits = (ulong*)(f->base + 0x58);
577 ahciwakeup(Aportc *c, uint mode)
582 if((s & Isleepy) == 0)
584 if((s & Smask) != Spresent){
585 print("ahci: slumbering drive missing %.3ux\n", s);
588 ahciportreset(c, mode);
589 // iprint("ahci: wake %.3ux -> %.3lux\n", s, c->p->sstatus);
593 ahciconfigdrive(Ahba *h, Aportc *c, int mode)
603 m->list = malign(sizeof *m->list, 1024);
604 m->ctab = malign(sizeof *m->ctab, 128);
607 p->list = PCIWADDR(m->list);
608 p->listhi = Pciwaddrh(m->list);
609 p->fis = PCIWADDR(m->fis.base);
610 p->fishi = Pciwaddrh(m->fis.base);
612 dprint("ahci: configdrive cmd=%lux sstatus=%lux\n", p->cmd, p->sstatus);
615 if((p->sstatus & Sbist) == 0 && (p->cmd & Apwr) != Apwr){
617 dprint("ahci: power up ... [%.3lux]\n", p->sstatus);
619 if((p->sstatus & Sphylink) == 0){
622 dprint("ahci: spin up ... [%.3lux]\n", p->sstatus);
623 for(int i = 0; i < 1400; i += 50){
624 if(p->sstatus & (Sphylink | Sbist))
631 if((p->sstatus & SSmask) == (Isleepy | Spresent))
634 /* disable power managment sequence from book. */
635 p->sctl = 3*Aipm | mode*Aspd | 0*Adet;
665 for(i = 0; i < 32; i++)
677 h = c->hba = (Ahba*)c->mmio;
683 // print("#S/sd%c: ahci %s port %#p: sss %d ncs %d coal %d "
684 // "mport %d led %d clo %d ems %d\n",
685 // c->sdev->idno, Tname(c), h,
686 // (u>>27) & 1, (u>>8) & 0x1f, (u>>7) & 1, u & 0x1f, (u>>25) & 1,
687 // (u>>24) & 1, (u>>6) & 1);
688 return countbits(h->pi);
692 ahcihbareset(Ahba *h)
697 for(wait = 0; wait < 1000; wait += 100){
711 if(d->unit && d->unit->name)
725 s = ahciidentify(&d->portc, id, &d->secsize, dnam(d));
730 osectors = d->sectors;
731 memmove(oserial, d->serial, sizeof d->serial);
735 idmove(d->serial, id+10, 20);
736 idmove(d->firmware, id+23, 8);
737 idmove(d->model, id+27, 40);
738 d->wwn = idwwn(d->portc.m, id);
741 memset(u->inquiry, 0, sizeof u->inquiry);
744 u->inquiry[4] = sizeof u->inquiry - 4;
745 memmove(u->inquiry+8, d->model, 40);
747 if(osectors != s || memcmp(oserial, d->serial, sizeof oserial)){
766 return c->pci->vid == 0x8086;
770 ignoreahdrs(Drive *d)
772 return d->portm.feat & Datapi && d->ctlr->type == Tsb600;
776 updatedrive(Drive *d)
778 ulong f, cause, serr, s0, pr, ewake;
787 if(d->ctlr->type == Tjmicron)
795 }else if(cause & Adps)
799 dprint("%s: fatal\n", dnam(d));
803 if(ignoreahdrs(d) && serr & ErrE)
805 dprint("%s: Adhrs cause %lux serr %lux task %lux\n",
806 dnam(d), cause, serr, p->task);
812 if(p->task & 1 && last != cause)
813 dprint("%s: err ca %lux serr %lux task %lux sstat %.3lux\n",
814 dnam(d), cause, serr, p->task, p->sstatus);
816 dprint("%s: upd %lux ta %lux\n", dnam(d), cause, p->task);
818 if(cause & (Aprcs|Aifs)){
820 switch(p->sstatus & Smask){
825 if((p->sstatus & Imask) == Islumber)
831 /* power mgnt crap for suprise removal */
832 p->ie |= Aprcs|Apcs; /* is this required? */
839 dprint("%s: %s → %s [Apcrs] %.3lux\n", dnam(d), diskstates[s0],
840 diskstates[d->state], p->sstatus);
841 if(s0 == Dready && d->state != Dready)
842 idprint("%s: pulled\n", dnam(d));
843 if(d->state != Dready)
845 if(d->state != Dready || p->ci)
859 pstatus(Drive *d, ulong s)
862 * bogus code because the first interrupt is currently dropped.
863 * likely my fault. serror is maybe cleared at the wrong time.
867 print("%s: pstatus: bad status %.3lux\n", dnam(d), s);
884 configdrive(Drive *d)
886 if(ahciconfigdrive(d->ctlr->hba, &d->portc, d->mode) == -1)
889 pstatus(d, d->port->sstatus & Smask);
897 uint state, det, stat;
902 stat = p->sstatus & Smask;
903 state = (p->cmd>>28) & 0xf;
904 dprint("%s: resetdisk: icc %ux det %.3ux sdet %.3ux\n", dnam(d), state, det, stat);
908 if(d->state != Dready || d->state != Dnew)
909 d->portm.flag |= Ferror;
910 clearci(p); /* satisfy sleep condition. */
915 if(stat != Sphylink){
917 d->state = Dportreset;
923 if(p->cmd&Ast && ahciswreset(&d->portc) == -1){
925 d->state = Dportreset; /* get a bigger stick. */
933 dprint("%s: resetdisk: %s → %s\n", dnam(d), diskstates[state], diskstates[d->state]);
948 setfissig(m, c->p->sig);
949 if(identify(d) == -1){
950 dprint("%s: identify failure\n", dnam(d));
953 if(settxmode(c, m->udma) == -1){
954 dprint("%s: can't set udma mode\n", dnam(d));
957 if(m->feat & Dpower && setfeatures(c, 0x85, 3*1000) == -1){
959 if(ahcirecover(c) == -1)
972 idprint("%s: %sLBA %,lld sectors\n", dnam(d), s, d->sectors);
973 idprint(" %s %s %s %s\n", d->model, d->firmware, d->serial,
974 d->drivechange? "[newdrive]": "");
978 idprint("%s: can't be initialized\n", dnam(d));
988 Mphywait = 2*1024/Nms - 1,
989 Midwait = 16*1024/Nms - 1,
990 Mcomrwait = 64*1024/Nms - 1,
996 if((d->portm.feat & Datapi) == 0 && d->active &&
997 d->totick != 0 && (long)(Ticks - d->totick) > 0){
998 dprint("%s: drive hung; resetting [%lux] ci %lux\n",
999 dnam(d), d->port->task, d->port->ci);
1004 static ushort olds[NCtlr*NCtlrdrv];
1007 doportreset(Drive *d)
1013 if(ahciportreset(&d->portc, d->mode) == -1)
1014 dprint("ahci: ahciportreset fails\n");
1018 dprint("ahci: portreset → %s [task %.4lux ss %.3lux]\n",
1019 diskstates[d->state], d->port->task, d->port->sstatus);
1023 /* drive must be locked */
1025 statechange(Drive *d)
1031 if(d->unit->sectors != 0){
1043 return (c->hba->cap & 0xf*Hiss)/Hiss;
1047 checkdrive(Drive *d, int i)
1052 s = d->port->sstatus;
1054 d->lastseen = Ticks;
1056 dprint("%s: status: %.3ux -> %.3ux: %s\n",
1057 dnam(d), olds[i], s, diskstates[d->state]);
1068 switch(s & (Iactive|Smask)){
1070 ahciwakeup(&d->portc, d->mode);
1074 dprint("%s: unknown status %.3ux\n", dnam(d), s);
1076 case Iactive: /* active, no device */
1077 if(++d->wait&Mphywait)
1081 d->mode = maxmode(d->ctlr);
1084 if(d->mode == DMautoneg){
1085 d->state = Dportreset;
1088 dprint("%s: reset; new mode %s\n", dnam(d),
1094 case Iactive | Sphylink:
1097 if((++d->wait&Midwait) == 0){
1098 dprint("%s: slow reset %.3ux task=%lux; %d\n",
1099 dnam(d), s, d->port->task, d->wait);
1102 s = (uchar)d->port->task;
1103 sig = d->port->sig >> 16;
1104 if(s == 0x7f || s&ASbsy ||
1105 (sig != 0xeb14 && (s & ASdrdy) == 0))
1114 if(d->wait++ & Mcomrwait)
1119 dprint("%s: reset [%s]: mode %d; status %.3ux\n",
1120 dnam(d), diskstates[d->state], d->mode, s);
1127 if(d->wait++ & 0xff && (s & Iactive) == 0)
1129 dprint("%s: portreset [%s]: mode %d; status %.3ux\n",
1130 dnam(d), diskstates[d->state], d->mode, s);
1131 d->portm.flag |= Ferror;
1134 if((s & Smask) == 0){
1135 d->state = Dmissing;
1153 tsleep(&up->sleep, return0, 0, Nms);
1154 for(i = 0; i < niadrive; i++)
1155 checkdrive(iadrive[i], i);
1160 iainterrupt(Ureg*, void *a)
1169 cause = c->hba->isr;
1170 for(i = 0; cause; i++){
1172 if((cause & m) == 0)
1175 d = c->rawdrive + i;
1177 if(d->port->isr && c->hba->pi & m)
1186 ahciencreset(Ctlr *c)
1190 if(c->enctype == Eesb)
1194 while(h->emctl & Emrst)
1200 * from the standard: (http://en.wikipedia.org/wiki/IBPI)
1201 * rebuild is preferred as locate+fail; alternate 1hz fail
1202 * we're going to assume no locate led.
1206 Ledsleep = 125, /* 8hz */
1209 L0 = Ledon*Aled | Ledon*Locled,
1210 L1 = Ledon*Aled | Ledoff*Locled,
1211 R0 = Ledon*Aled | Ledon*Locled | Ledon*Errled,
1212 R1 = Ledon*Aled | Ledoff*Errled,
1213 S0 = Ledon*Aled | Ledon*Locled /*| Ledon*Errled*/, /* botch */
1214 S1 = Ledon*Aled | Ledoff*Errled,
1215 P0 = Ledon*Aled | Ledon*Errled,
1216 P1 = Ledon*Aled | Ledoff*Errled,
1217 F0 = Ledon*Aled | Ledon*Errled,
1218 C0 = Ledon*Aled | Ledon*Locled,
1219 C1 = Ledon*Aled | Ledoff*Locled,
1223 //static ushort led3[Ibpilast*8] = {
1224 //[Ibpinone*8] 0, 0, 0, 0, 0, 0, 0, 0,
1225 //[Ibpinormal*8] N0, N0, N0, N0, N0, N0, N0, N0,
1226 //[Ibpirebuild*8] R0, R0, R0, R0, R1, R1, R1, R1,
1227 //[Ibpilocate*8] L0, L1, L0, L1, L0, L1, L0, L1,
1228 //[Ibpispare*8] S0, S1, S0, S1, S1, S1, S1, S1,
1229 //[Ibpipfa*8] P0, P1, P0, P1, P1, P1, P1, P1, /* first 1 sec */
1230 //[Ibpifail*8] F0, F0, F0, F0, F0, F0, F0, F0,
1231 //[Ibpicritarray*8] C0, C0, C0, C0, C1, C1, C1, C1,
1232 //[Ibpifailarray*8] C0, C1, C0, C1, C0, C1, C0, C1,
1235 static ushort led2[Ibpilast*8] = {
1236 [Ibpinone*8] 0, 0, 0, 0, 0, 0, 0, 0,
1237 [Ibpinormal*8] N0, N0, N0, N0, N0, N0, N0, N0,
1238 [Ibpirebuild*8] R0, R0, R0, R0, R1, R1, R1, R1,
1239 [Ibpilocate*8] L0, L0, L0, L0, L0, L0, L0, L0,
1240 [Ibpispare*8] S0, S0, S0, S0, S1, S1, S1, S1,
1241 [Ibpipfa*8] P0, P1, P0, P1, P1, P1, P1, P1, /* first 1 sec */
1242 [Ibpifail*8] F0, F0, F0, F0, F0, F0, F0, F0,
1243 [Ibpicritarray*8] C0, C0, C0, C0, C1, C1, C1, C1,
1244 [Ibpifailarray*8] C0, C1, C0, C1, C0, C1, C0, C1,
1248 ledstate(Ledport *p, uint seq)
1252 if(p->led == Ibpipfa && seq%32 >= 8)
1255 i = led2[8*p->led + seq%8];
1256 if(i != p->ledbits){
1258 ledprint("ledstate %,.011ub %ud\n", p->ledbits, seq);
1265 blink(Drive *d, ulong t)
1271 if(ledstate(d, t) == 0)
1275 /* ensure last message has been transmitted */
1276 while(h->emctl & Tmsg)
1280 panic("%s: bad led type %d", dnam(d), c->enctype);
1282 memset(&msg, 0, sizeof msg);
1285 msg.msize = sizeof msg - 4;
1286 msg.led[0] = d->ledbits;
1287 msg.led[1] = d->ledbits>>8;
1289 msg.hba = d->driveno;
1290 memmove(c->enctx, &msg, sizeof msg);
1298 Esbdrv0 = 4, /* start pos in bits */
1299 Esbiota = 3, /* shift in bits */
1308 uint i, e; /* except after c */
1311 for(i = 0; i < 3; i++)
1312 e |= ((s>>3*i & 7) != 0)<<i;
1317 blinkesb(Ctlr *c, ulong t)
1324 for(i = 0; i < c->ndrive; i++){
1326 s |= ledstate(d, t); /* no port mapping */
1330 memset(u, 0, sizeof u);
1331 for(i = 0; i < c->ndrive; i++){
1333 s = Esbdrv0 + Esbiota*i;
1334 v = esbbits(d->ledbits) * (1ull << s%32);
1336 u[s/32 + 1] |= v>>32;
1338 for(i = 0; i < c->encsz; i++)
1344 ahciledr(SDunit *u, Chan *ch, void *a, long n, vlong off)
1350 d = c->drive[u->subno];
1351 return ledr(d, ch, a, n, off);
1355 ahciledw(SDunit *u, Chan *ch, void *a, long n, vlong off)
1361 d = c->drive[u->subno];
1362 return ledw(d, ch, a, n, off);
1374 memset(map, 0, sizeof map);
1375 for(i = 0; i < niactlr; i++)
1376 if(iactlr[i].enctype != 0){
1377 ahciencreset(iactlr + i);
1382 pexit("no work", 1);
1383 for(i = 0; i < niadrive; i++){
1384 iadrive[i]->nled = 3; /* hardcoded */
1385 if(iadrive[i]->ctlr->enctype == Eesb)
1386 iadrive[i]->nled = 3;
1387 iadrive[i]->ledbits = -1;
1391 for(j = 0; j < niadrive; ){
1392 c = iadrive[j]->ctlr;
1395 else if(c->enctype == Eesb){
1404 esleep(Ledsleep - TK2MS(t1 - t0));
1415 d = c->drive[u->subno];
1421 sdaddfile(u, "led", 0644, eve, ahciledr, ahciledw);
1425 checkdrive(d, d->driveno); /* c->d0 + d->driveno */
1441 kproc("iasata", satakproc, 0);
1443 panic("iaenable: zero s->ctlr->ndrive");
1445 snprint(name, sizeof name, "%s (%s)", s->name, s->ifc->name);
1446 intrenable(c->pci->intl, iainterrupt, c, c->pci->tbdf, name);
1447 /* supposed to squelch leftover interrupts here. */
1450 if(++once == niactlr)
1451 kproc("ialed", ledkproc, 0);
1465 ahcidisable(c->hba);
1466 snprint(name, sizeof name, "%s (%s)", s->name, s->ifc->name);
1467 intrdisable(c->pci->intl, iainterrupt, c, c->pci->tbdf, name);
1481 d = c->drive[u->subno];
1483 if(d->portm.feat & Datapi){
1486 return scsionline(u);
1492 }else if(d->state == Dready)
1495 u->sectors = d->sectors;
1496 u->secsize = d->secsize;
1503 ahcibuild(Aportm *m, int rw, void *data, uint n, vlong lba)
1511 rwfis(m, c, rw, n, lba);
1515 mkalist(m, flags, data, 512*n);
1520 ahcibuildpkt(Aportm *m, SDreq *r, void *data, int n)
1530 atapirwfis(m, c, r->cmd, r->clen, n);
1531 flags = 1<<16 | Lpref | Latapi;
1532 if(r->write != 0 && data)
1534 mkalist(m, flags, data, n);
1539 ahcibuildfis(Aportm *m, SDreq *r, void *data, uint n)
1547 if((r->ataproto & Pprotom) != Ppkt){
1548 memmove(c, r->cmd, r->clen);
1550 if(r->write || n == 0)
1552 mkalist(m, flags, data, n);
1554 atapirwfis(m, c, r->cmd, r->clen, n);
1555 flags = 1<<16 | Lpref | Latapi;
1556 if(r->write && data)
1558 mkalist(m, flags, data, n);
1568 for(i = 0; i < 15000; i += 250){
1569 if(d->state == Dreset || d->state == Dportreset ||
1572 δ = Ticks - d->lastseen;
1573 if(d->state == Dnull || δ > 10*1000)
1576 s = d->port->sstatus;
1578 if((s & Imask) == 0 && δ > 1500)
1580 if(d->state == Dready && (s & Smask) == Sphylink)
1584 print("%s: not responding; offline\n", dnam(d));
1586 d->state = Doffline;
1597 while ((i = waitready(d)) == 1) {
1606 flushcache(Drive *d)
1611 if(lockready(d) == 0)
1612 i = ahciflushcache(&d->portc);
1618 io(Drive *d, uint proto, int to, int interrupt)
1620 uint task, flag, rv;
1624 switch(waitready(d)){
1641 d->totick = Ticks + MS2TK(to) | 1; /* fix fencepost */
1648 if(ahcicomreset(&d->portc) == -1){
1655 sleep(&d->portm, ahciclear, &as);
1660 flag = d->portm.flag;
1661 task = d->port->task;
1666 rv = task >> 8 + 4 & 0xf;
1669 }else if(task & (Efatal<<8) || task & (ASbsy|ASdrq) && d->state == Dready){
1671 ahcirecover(&d->portc);
1672 task = d->port->task;
1673 flag &= ~Fdone; /* either an error or do-over */
1676 print("%s: retry\n", dnam(d));
1679 if(flag & (Fahdrs | Ferror)){
1680 if((task & Eidnf) == 0)
1681 print("%s: i/o error %ux\n", dnam(d), task);
1688 iariopkt(SDreq *r, Drive *d)
1690 int n, count, try, max;
1694 aprint("%s: %.2ux %.2ux %c %d %p\n", dnam(d), cmd[0], cmd[2],
1695 "rw"[r->write], r->dlen, r->data);
1700 for(try = 0; try < 10; try++){
1705 ahcibuildpkt(&d->portm, r, r->data, n);
1706 r->status = io(d, Ppkt, 5000, 0);
1714 r->rlen = d->portm.list->len;
1717 print("%s: bad disk\n", dnam(d));
1718 return r->status = SDcheck;
1722 ahcibio(SDunit *u, int lun, int write, void *a, long count, uvlong lba)
1724 int n, rw, try, status, max;
1730 d = c->drive[u->subno];
1731 if(d->portm.feat & Datapi)
1732 return scsibio(u, lun, write, a, count, lba);
1735 if(d->portm.feat & Dllba){
1736 max = 8192; /* ahci maximum */
1737 if(c->type == Tsb600)
1738 max = 255; /* errata */
1740 rw = write? SDwrite: SDread;
1742 for(try = 0; try < 10;){
1747 ahcibuild(&d->portm, rw, data, n, lba);
1748 status = io(d, Pdma, 5000, 0);
1760 data += n * u->secsize;
1762 return data - (uchar*)a;
1764 print("%s: bad disk\n", dnam(d));
1771 int i, n, count, rw;
1780 d = c->drive[u->subno];
1781 if(d->portm.feat & Datapi)
1782 return iariopkt(r, d);
1785 if(cmd[0] == 0x35 || cmd[0] == 0x91){
1786 if(flushcache(d) == 0)
1787 return sdsetsense(r, SDok, 0, 0, 0);
1788 return sdsetsense(r, SDcheck, 3, 0xc, 2);
1791 if((i = sdfakescsi(r)) != SDnostatus){
1796 if((i = sdfakescsirw(r, &lba, &count, &rw)) != SDnostatus)
1798 n = ahcibio(u, r->lun, r->write, r->data, count, lba);
1805 static uchar bogusrfis[16] = {
1819 memmove(c, bogusrfis, sizeof bogusrfis);
1824 sdr(SDreq *r, Drive *d, int st)
1829 if((r->ataproto & Pprotom) == Ppkt){
1832 st = t >> 8 + 4 & 0xf;
1835 memmove(r->cmd, c, 16);
1843 fisreqchk(Sfis *f, SDreq *r)
1845 if((r->ataproto & Pprotom) == Ppkt)
1848 * handle oob requests;
1849 * restrict & sanitize commands
1853 if(r->cmd[0] == 0xf0){
1854 sigtofis(f, r->cmd);
1874 d = c->drive[u->subno];
1876 if((r->status = fisreqchk(&d->portm, r)) != SDnostatus)
1880 for(try = 0; try < 10; try++){
1882 ahcibuildfis(&d->portm, r, r->data, r->dlen);
1883 r->status = io(d, r->ataproto & Pprotom, -1, 1);
1887 return sdsetsense(r, SDcheck, 11, 0, 6);
1894 if((r->ataproto & Pprotom) == Ppkt)
1895 r->rlen = d->portm.list->len;
1896 return sdr(r, d, r->status);
1898 print("%s: bad disk\n", dnam(d));
1904 * configure drives 0-5 as ahci sata (c.f. errata)
1907 iaahcimode(Pcidev *p)
1911 u = pcicfgr16(p, 0x92);
1912 dprint("ahci: %T: iaahcimode %.2ux %.4ux\n", p->tbdf, pcicfgr8(p, 0x91), u);
1913 pcicfgw16(p, 0x92, u | 0xf); /* ports 0-15 */
1918 Ghc = 0x04/4, /* global host control */
1919 Pi = 0x0c/4, /* ports implemented */
1920 Cmddec = 1<<15, /* enable command block decode */
1923 Ahcien = 1<<31, /* ahci enable */
1927 iasetupahci(Ctlr *c)
1929 pcicfgw16(c->pci, 0x40, pcicfgr16(c->pci, 0x40) & ~Cmddec);
1930 pcicfgw16(c->pci, 0x42, pcicfgr16(c->pci, 0x42) & ~Cmddec);
1932 c->lmmio[Ghc] |= Ahcien;
1933 c->lmmio[Pi] = (1 << 6) - 1; /* 5 ports (supposedly ro pi reg) */
1935 /* enable ahci mode; from ich9 datasheet */
1936 pcicfgw16(c->pci, 0x90, 1<<6 | 1<<5);
1940 sbsetupahci(Pcidev *p)
1942 print("sbsetupahci: tweaking %.4ux ccru %.2ux ccrp %.2ux\n",
1943 p->did, p->ccru, p->ccrp);
1944 pcicfgw8(p, 0x40, pcicfgr8(p, 0x40) | 1);
1945 pcicfgw8(p, PciCCRu, 6);
1946 pcicfgw8(p, PciCCRp, 1);
1955 c->enctx = (ulong*)(c->mmio + 0xa0);
1962 ahciencinit(Ctlr *c)
1964 ulong type, sz, o, *bar;
1970 if((h->cap & Hems) == 0)
1972 type = h->emctl & Emtype;
1984 sz = h->emloc & 0xffff;
1986 if(sz == 0 || o == 0)
1989 dprint("size = %.4lux; loc = %.4lux*4\n", sz, o);
1992 if((h->emctl & Xonly) == 0){
1996 c->encrx = bar + o*2;
2012 if((p->did & 0xfffc) == 0x2680)
2014 if((p->did & 0xfffb) == 0x27c1)
2015 return Tich; /* 82801g[bh]m */
2016 if((p->did & 0xffff) == 0x2822)
2017 return Tich; /* 82801 SATA RAID */
2018 if((p->did & 0xffff) == 0x2821)
2019 return Tich; /* 82801h[roh] */
2020 if((p->did & 0xfffe) == 0x2824)
2021 return Tich; /* 82801h[b] */
2022 if((p->did & 0xfeff) == 0x2829)
2023 return Tich; /* ich8 */
2024 if((p->did & 0xfffe) == 0x2922)
2025 return Tich; /* ich9 */
2026 if((p->did & 0xffff) == 0x3a02)
2027 return Tich; /* 82801jd/do */
2028 if((p->did & 0xfefe) == 0x3a22)
2029 return Tich; /* ich10, pch */
2030 if((p->did & 0xfff7) == 0x3b28)
2031 return Tich; /* pchm */
2032 if((p->did & 0xfffe) == 0x3b22)
2033 return Tich; /* pch */
2036 if(p->ccru == 1 || p->ccrp != 1)
2037 if(p->did == 0x4380 || p->did == 0x4390)
2043 * unconfirmed report that the programming
2044 * interface is set incorrectly.
2046 if(p->did == 0x3349)
2050 /* Hudson SATA Controller [AHCI mode] */
2051 if(p->did == 0x7801)
2064 if(p->ccrb == Pcibcstore && p->ccru == 6 && p->ccrp == 1)
2072 int i, n, nunit, type;
2084 if(getconf("*ahcidebug") != nil){
2089 memset(olds, 0xff, sizeof olds);
2092 while((p = pcimatch(p, 0, 0)) != nil){
2093 if((type = didtype(p)) == -1)
2095 if(p->mem[Abar].bar == 0)
2097 if(niactlr == NCtlr){
2098 print("iapnp: %s: too many controllers\n", tname[type]);
2101 c = iactlr + niactlr;
2102 s = sdevs + niactlr;
2103 memset(c, 0, sizeof *c);
2104 memset(s, 0, sizeof *s);
2105 io = p->mem[Abar].bar & ~0xf;
2106 c->mmio = vmap(io, p->mem[Abar].size);
2108 print("%s: address %#p in use did %.4ux\n",
2109 Tname(c), io, p->did);
2112 c->lmmio = (ulong*)c->mmio;
2116 s->ifc = &sdiahciifc;
2121 if(intel(c) && p->did != 0x2681)
2123 // ahcihbareset((Ahba*)c->mmio);
2124 nunit = ahciconf(c);
2125 if(intel(c) && iaahcimode(p) == -1 || nunit < 1){
2126 vunmap(c->mmio, p->mem[Abar].size);
2129 c->ndrive = s->nunit = nunit;
2131 /* map the drives -- they don't all need to be enabled. */
2132 memset(c->rawdrive, 0, sizeof c->rawdrive);
2134 for(i = 0; i < NCtlrdrv; i++){
2135 d = c->rawdrive + i;
2141 if((c->hba->pi & 1<<i) == 0)
2143 snprint(d->name, sizeof d->name, "iahci%d.%d", niactlr, i);
2144 d->port = (Aport*)(c->mmio + 0x80*i + 0x100);
2145 d->portc.p = d->port;
2146 d->portc.m = &d->portm;
2148 c->drive[d->driveno] = d;
2149 iadrive[niadrive + d->driveno] = d;
2151 for(i = 0; i < n; i++)
2152 if(ahciidle(c->drive[i]->port) == -1){
2153 print("%s: port %d wedged; abort\n",
2157 for(i = 0; i < n; i++){
2158 c->drive[i]->mode = DMautoneg;
2159 configdrive(c->drive[i]);
2166 i = (c->hba->cap >> 21) & 1;
2167 print("#S/%s: %s: sata-%s with %d ports\n", s->name,
2168 Tname(c), "I\0II" + i*2, nunit);
2173 static Htab ctab[] = {
2195 capfmt(char *p, char *e, Htab *t, int n, ulong cap)
2200 for(i = 0; i < n; i++)
2202 p = seprint(p, e, "%s ", t[i].name);
2207 iarctl(SDunit *u, char *p, int l)
2209 char buf[32], *e, *op;
2214 if((c = u->dev->ctlr) == nil)
2216 d = c->drive[u->subno];
2221 if(d->state == Dready){
2222 p = seprint(p, e, "model\t%s\n", d->model);
2223 p = seprint(p, e, "serial\t%s\n", d->serial);
2224 p = seprint(p, e, "firm\t%s\n", d->firmware);
2226 p = seprint(p, e, "wwn\t%ullx\n", d->wwn);
2227 p = seprint(p, e, "flag\t");
2228 p = pflag(p, e, &d->portm);
2229 p = seprint(p, e, "udma\t%d\n", d->portm.udma);
2231 p = seprint(p, e, "no disk present [%s]\n", diskstates[d->state]);
2232 serrstr(o->serror, buf, buf + sizeof buf - 1);
2233 p = seprint(p, e, "reg\ttask %lux cmd %lux serr %lux %s ci %lux is %lux "
2234 "sig %lux sstatus %.3lux\n", o->task, o->cmd, o->serror, buf,
2235 o->ci, o->isr, o->sig, o->sstatus);
2236 p = seprint(p, e, "cmd\t");
2237 p = capfmt(p, e, ctab, nelem(ctab), o->cmd);
2238 p = seprint(p, e, "\n");
2239 p = seprint(p, e, "mode\t%s %s\n", modes[d->mode], modes[maxmode(c)]);
2240 p = seprint(p, e, "geometry %llud %lud\n", d->sectors, u->secsize);
2241 p = seprint(p, e, "alignment %d %d\n",
2242 d->secsize<<d->portm.physshift, d->portm.physalign);
2247 forcemode(Drive *d, char *mode)
2251 for(i = 0; i < nelem(modes); i++)
2252 if(strcmp(mode, modes[i]) == 0)
2254 if(i == nelem(modes))
2262 forcestate(Drive *d, char *state)
2266 for(i = 0; i < nelem(diskstates); i++)
2267 if(strcmp(state, diskstates[i]) == 0)
2269 if(i == nelem(diskstates))
2278 runsettxmode(Drive *d, char *s)
2288 if(lockready(d) == 0){
2290 if(settxmode(c, m->udma) == 0)
2299 iawctl(SDunit *u, Cmdbuf *cmd)
2306 d = c->drive[u->subno];
2309 if(strcmp(f[0], "mode") == 0)
2310 forcemode(d, f[1]? f[1]: "satai");
2311 else if(strcmp(f[0], "state") == 0)
2312 forcestate(d, f[1]? f[1]: "null");
2313 else if(strcmp(f[0], "txmode") == 0){
2314 if(runsettxmode(d, f[1]? f[1]: "0"))
2315 cmderror(cmd, "bad txmode / stuck port");
2317 cmderror(cmd, Ebadctl);
2322 portr(char *p, char *e, uint x)
2328 for(i = 0; i < 32; i++){
2329 if((x & (1<<i)) == 0){
2330 if(a != -1 && i - 1 != a)
2331 p = seprint(p, e, "-%d", i - 1);
2337 p = seprint(p, e, ", ");
2338 p = seprint(p, e, "%d", a = i);
2341 if(a != -1 && i - 1 != a)
2342 p = seprint(p, e, "-%d", i - 1);
2346 static Htab htab[] = {
2366 static Htab htab2[] = {
2372 static Htab emtab[] = {
2384 iartopctl(SDev *s, char *p, char *e)
2394 p = seprint(p, e, "sd%c ahci %s port %#p: ", s->idno, Tname(c), h);
2395 p = capfmt(p, e, htab, nelem(htab), cap);
2396 p = capfmt(p, e, htab2, nelem(htab2), h->cap2);
2397 p = capfmt(p, e, emtab, nelem(emtab), h->emctl);
2398 portr(pr, pr + sizeof pr, h->pi);
2399 return seprint(p, e,
2400 "iss %ld ncs %ld np %ld ghc %lux isr %lux pi %lux %s ver %lux\n",
2401 (cap>>20) & 0xf, (cap>>8) & 0x1f, 1 + (cap & 0x1f),
2402 h->ghc, h->isr, h->pi, pr, h->ver);
2406 iawtopctl(SDev *, Cmdbuf *cmd)
2414 if(strcmp(f[0], "debug") == 0)
2416 else if(strcmp(f[0], "idprint") == 0)
2418 else if(strcmp(f[0], "aprint") == 0)
2420 else if(strcmp(f[0], "ledprint") == 0)
2423 cmderror(cmd, Ebadctl);
2427 cmderror(cmd, Ebadarg);
2433 *v = strcmp(f[1], "on") == 0;
2441 SDifc sdiahciifc = {