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) iprint(__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];
171 static Ctlr iactlr[NCtlr];
172 static SDev sdevs[NCtlr];
175 static Drive *iadrive[NDrive];
183 static char stab[] = {
185 [8] 't', 'c', 'p', 'e',
186 [16] 'N', 'I', 'W', 'B', 'D', 'C', 'H', 'S', 'T', 'F', 'X'
190 serrstr(ulong r, char *s, char *e)
195 for(i = 0; i < nelem(stab) && s < e; i++)
196 if(r & (1<<i) && stab[i]){
204 static char ntab[] = "0123456789abcdef";
207 preg(uchar *reg, int n)
209 char buf[25*3+1], *e;
213 for(i = 0; i < n; i++){
214 *e++ = ntab[reg[i] >> 4];
215 *e++ = ntab[reg[i] & 0xf];
224 dreg(char *s, Aport *p)
226 dprint("%stask=%lux; cmd=%lux; ci=%lux; is=%lux\n",
227 s, p->task, p->cmd, p->ci, p->isr);
235 tsleep(&up->sleep, return0, 0, ms);
250 return (s->p->ci & s->i) == 0;
254 aesleep(Aportm *m, Asleep *a, int ms)
258 tsleep(m, ahciclear, a, ms);
263 ahciwait(Aportc *c, int ms)
272 aesleep(c->m, &as, ms);
273 if((p->task & 1) == 0 && p->ci == 0)
275 dreg("ahciwait fail/timeout ", c->p);
280 mkalist(Aportm *m, uint flags, uchar *data, int len)
288 l->flags = flags | 0x5;
290 l->ctab = PCIWADDR(t);
291 l->ctabhi = Pciwaddrh(t);
295 p->dba = PCIWADDR(data);
296 p->dbahi = Pciwaddrh(data);
297 p->count = 1<<31 | len - 2 | 1;
306 if((pc->m->feat & Dnop) == 0)
308 c = pc->m->ctab->cfis;
310 mkalist(pc->m, Lwrite, 0, 0);
311 return ahciwait(pc, 3*1000);
315 setfeatures(Aportc *pc, uchar f, uint w)
319 c = pc->m->ctab->cfis;
320 featfis(pc->m, c, f);
321 mkalist(pc->m, Lwrite, 0, 0);
322 return ahciwait(pc, w);
326 * ata 7, required for sata, requires that all devices "support"
327 * udma mode 5, however sata:pata bridges allow older devices
328 * which may not. the innodisk satadom, for example allows
329 * only udma mode 2. on the assumption that actual udma is
330 * taking place on these bridges, we set the highest udma mode
331 * available, or pio if there is no udma mode available.
334 settxmode(Aportc *pc, uchar f)
338 c = pc->m->ctab->cfis;
339 if(txmodefis(pc->m, c, f) == -1)
341 mkalist(pc->m, Lwrite, 0, 0);
342 return ahciwait(pc, 3*1000);
348 if(up == nil || !islo())
355 ahciportreset(Aportc *c, uint mode)
363 for(i = 0; i < 500; i += 25){
364 if((*cmd & Acr) == 0)
368 if((*cmd & Apwr) != Apwr)
370 p->sctl = 3*Aipm | 0*Aspd | Adet;
372 p->sctl = 3*Aipm | mode*Aspd;
377 ahciflushcache(Aportc *pc)
381 c = pc->m->ctab->cfis;
382 flushcachefis(pc->m, c);
383 mkalist(pc->m, Lwrite, 0, 0);
385 if(ahciwait(pc, 60000) == -1 || pc->p->task & (1|32)){
386 dprint("ahciflushcache fail [task %lux]\n", pc->p->task);
387 // preg(pc->m->fis.r, 20);
394 ahciidentify0(Aportc *pc, void *id)
401 memset(id, 0, 0x200);
402 identifyfis(pc->m, c);
403 mkalist(pc->m, 0, id, 0x200);
404 return ahciwait(pc, 3*1000);
408 ahciidentify(Aportc *pc, ushort *id, uint *ss, char *d)
416 if(i > 5 || ahciidentify0(pc, id) != 0)
419 if(n & Pspinup && setfeatures(pc, 7, 20*1000) == -1)
420 print("%s: puis spinup fail\n", d);
423 print("%s: puis waiting\n", d);
427 if(s == -1 || (m->feat&Dlba) == 0){
428 if((m->feat&Dlba) == 0)
429 dprint("%s: no lba support\n", d);
442 for(i = 0; i < 500; i += 50){
449 if((a->task & (ASdrq|ASbsy)) == 0){
455 for(i = 0; i < 500; i += 50){
463 dprint("ahci: clo clear [task %lux]\n", a->task);
471 ahcicomreset(Aportc *pc)
475 dreg("comreset ", pc->p);
476 if(ahciquiet(pc->p) == -1){
477 dprint("ahci: ahciquiet failed\n");
480 dreg("comreset ", pc->p);
482 c = pc->m->ctab->cfis;
484 mkalist(pc->m, Lclear | Lreset, 0, 0);
485 if(ahciwait(pc, 500) == -1){
486 dprint("ahci: comreset1 failed\n");
490 dreg("comreset ", pc->p);
493 mkalist(pc->m, Lwrite, 0, 0);
494 if(ahciwait(pc, 150) == -1){
495 dprint("ahci: comreset2 failed\n");
498 dreg("comreset ", pc->p);
503 ahciidle(Aport *port)
512 for(i = 0; i < 500; i += 25){
522 for(i = 0; i < 500; i += 25){
531 * §6.2.2.1 first part; comreset handled by reset disk.
532 * - remainder is handled by configdisk.
533 * - ahcirecover is a quick recovery from a failed command.
536 ahciswreset(Aportc *pc)
544 if(pc->p->task & (ASdrq|ASbsy))
550 ahcirecover(Aportc *pc)
554 if(settxmode(pc, pc->m->udma) == -1)
560 malign(int size, int align)
564 v = xspanalloc(size, align, 0);
572 f->base = malign(0x100, 0x100);
574 f->p = f->base + 0x20;
575 f->r = f->base + 0x40;
576 f->u = f->base + 0x60;
577 f->devicebits = (ulong*)(f->base + 0x58);
581 ahciwakeup(Aportc *c, uint mode)
588 if((s & Isleepy) != 0)
590 if((s & Smask) != Spresent){
591 dprint("ahci: slumbering drive missing [ss %.3ux]\n", s);
594 ahciportreset(c, mode);
595 dprint("ahci: wake %.3ux -> %.3lux\n", s, c->p->sstatus);
599 ahciconfigdrive(Ahba *h, Aportc *c, int mode)
610 m->list = malign(sizeof *m->list, 1024);
611 m->ctab = malign(sizeof *m->ctab, 128);
614 if(ahciidle(p) == -1){
615 dprint("ahci: port not idle\n");
619 p->list = PCIWADDR(m->list);
620 p->listhi = Pciwaddrh(m->list);
621 p->fis = PCIWADDR(m->fis.base);
622 p->fishi = Pciwaddrh(m->fis.base);
626 if((p->cmd & Apwr) != Apwr)
629 if((h->cap & Hss) != 0){
630 dprint("ahci: spin up ... [%.3lux]\n", p->sstatus);
631 for(i = 0; i < 1400; i += 50){
632 if((p->sstatus & Sbist) != 0)
634 if((p->sstatus & Sphylink) == Sphylink)
640 if((p->sstatus & SSmask) == (Isleepy | Spresent))
646 /* we will get called again once phylink has been established */
647 if((p->sstatus & Sphylink) != Sphylink)
650 /* disable power managment sequence from book. */
651 p->sctl = 3*Aipm | mode*Aspd | 0*Adet;
654 p->cmd |= Afre | Ast;
679 for(i = 0; i < 32; i++)
691 h = c->hba = (Ahba*)c->mmio;
697 return countbits(h->pi);
705 if((h->cap2 & Boh) == 0)
708 for(wait = 0; wait < 2000; wait += 100){
709 if((h->bios & Bos) == 0)
713 iprint("ahci: bios handoff timed out\n");
718 ahcihbareset(Ahba *h)
723 for(wait = 0; wait < 1000; wait += 100){
737 if(d->unit && d->unit->name)
751 s = ahciidentify(&d->portc, id, &d->secsize, dnam(d));
754 osectors = d->sectors;
755 memmove(oserial, d->serial, sizeof d->serial);
759 idmove(d->serial, id+10, 20);
760 idmove(d->firmware, id+23, 8);
761 idmove(d->model, id+27, 40);
762 d->wwn = idwwn(d->portc.m, id);
765 memset(u->inquiry, 0, sizeof u->inquiry);
768 u->inquiry[4] = sizeof u->inquiry - 4;
769 memmove(u->inquiry+8, d->model, 40);
771 if(osectors != s || memcmp(oserial, d->serial, sizeof oserial)){
790 return c->pci->vid == 0x8086;
794 ignoreahdrs(Drive *d)
796 return d->portm.feat & Datapi && d->ctlr->type == Tsb600;
800 updatedrive(Drive *d)
802 ulong f, cause, serr, s0, pr, ewake;
811 if(d->ctlr->type == Tjmicron)
819 }else if(cause & Adps){
821 }else if(cause & Atfes){
828 dprint("%s: fatal\n", dnam(d));
832 if(ignoreahdrs(d) && serr & ErrE)
834 dprint("%s: Adhrs cause %lux serr %lux task %lux\n",
835 dnam(d), cause, serr, p->task);
841 if(p->task & 1 && last != cause)
842 dprint("%s: err ca %lux serr %lux task %lux sstat %.3lux\n",
843 dnam(d), cause, serr, p->task, p->sstatus);
845 dprint("%s: upd %lux ta %lux\n", dnam(d), cause, p->task);
847 if(cause & (Aprcs|Aifs)){
849 switch(p->sstatus & Smask){
854 if((p->sstatus & Imask) == Islumber)
860 /* power mgnt crap for suprise removal */
861 p->ie |= Aprcs|Apcs; /* is this required? */
868 dprint("%s: updatedrive: %s → %s [ss %.3lux]\n",
869 dnam(d), diskstates[s0], diskstates[d->state], p->sstatus);
870 if(s0 == Dready && d->state != Dready)
871 dprint("%s: pulled\n", dnam(d));
872 if(d->state != Dready)
874 if(d->state != Dready || p->ci)
888 dstatus(Drive *d, int s)
896 configdrive(Drive *d)
898 if(ahciconfigdrive(d->ctlr->hba, &d->portc, d->mode) == -1){
899 dstatus(d, Dportreset);
904 switch(d->port->sstatus & Smask){
911 if(d->state == Dready)
926 uint state, det, stat;
931 stat = p->sstatus & Smask;
932 state = (p->cmd>>28) & 0xf;
933 dprint("%s: resetdisk [icc %ux; det %.3ux; sdet %.3ux]\n", dnam(d), state, det, stat);
936 if(d->state != Dready && d->state != Dnew)
937 d->portm.flag |= Ferror;
939 d->state = Dportreset;
942 clearci(p); /* satisfy sleep condition. */
950 if(p->cmd&Ast && ahciswreset(&d->portc) == -1)
951 dstatus(d, Dportreset); /* get a bigger stick. */
968 setfissig(m, c->p->sig);
969 if(identify(d) == -1){
970 dprint("%s: identify failure\n", dnam(d));
973 if(settxmode(c, m->udma) == -1){
974 dprint("%s: can't set udma mode\n", dnam(d));
977 if(m->feat & Dpower && setfeatures(c, 0x85, 3*1000) == -1){
978 dprint("%s: can't disable apm\n", dnam(d));
980 if(ahcirecover(c) == -1)
989 idprint("%s: %sLBA %,lld sectors\n", dnam(d), s, d->sectors);
990 idprint(" %s %s %s %s\n", d->model, d->firmware, d->serial,
991 d->drivechange? "[newdrive]": "");
995 idprint("%s: can't be initialized\n", dnam(d));
1003 Mphywait = 2*1024/Nms - 1,
1004 Midwait = 16*1024/Nms - 1,
1005 Mcomrwait = 64*1024/Nms - 1,
1011 if((d->portm.feat & Datapi) == 0 && d->active &&
1012 d->totick != 0 && (long)(Ticks - d->totick) > 0){
1013 dprint("%s: drive hung [task %lux; ci %lux; serr %lux]\n",
1014 dnam(d), d->port->task, d->port->ci, d->port->serror);
1019 static ushort olds[NCtlr*NCtlrdrv];
1022 doportreset(Drive *d)
1025 ahciportreset(&d->portc, d->mode);
1028 dprint("ahci: portreset: %s [task %lux; ss %.3lux]\n",
1029 diskstates[d->state], d->port->task, d->port->sstatus);
1032 /* drive must be locked */
1034 statechange(Drive *d)
1040 if(d->unit->sectors != 0){
1052 return (c->hba->cap & 0xf*Hiss)/Hiss;
1055 static void iainterrupt(Ureg*, void *);
1058 checkdrive(Drive *d, int i)
1062 if(d->ctlr->enabled == 0)
1065 iainterrupt(0, d->ctlr); /* check for missed irq's */
1068 s = d->port->sstatus;
1070 d->lastseen = Ticks;
1072 dprint("%s: status: %.3ux -> %.3ux: %s\n",
1073 dnam(d), olds[i], s, diskstates[d->state]);
1084 switch(s & (Iactive|Smask)){
1086 ahciwakeup(&d->portc, d->mode);
1090 dprint("%s: unknown status %.3ux\n", dnam(d), s);
1092 case Iactive: /* active, no device */
1093 if(++d->wait&Mphywait)
1097 d->mode = maxmode(d->ctlr);
1100 if(d->mode == DMautoneg){
1101 d->state = Dportreset;
1104 dprint("%s: reset; new mode %s\n", dnam(d),
1110 case Iactive | Sphylink:
1113 if((++d->wait&Midwait) == 0){
1114 dprint("%s: slow reset [task %lux; ss %.3ux; wait %d]\n",
1115 dnam(d), d->port->task, s, d->wait);
1118 s = (uchar)d->port->task;
1119 sig = d->port->sig >> 16;
1120 if(s == 0x7f || s&ASbsy ||
1121 (sig != 0xeb14 && (s & ASdrdy) == 0))
1130 if(d->wait++ & Mcomrwait)
1135 dprint("%s: reset [%s]: mode %d; status %.3ux\n",
1136 dnam(d), diskstates[d->state], d->mode, s);
1143 if(d->wait++ & 0xff && (s & Iactive) == 0)
1145 dprint("%s: portreset [%s]: mode %d; status %.3ux\n",
1146 dnam(d), diskstates[d->state], d->mode, s);
1147 d->portm.flag |= Ferror;
1150 if((s & Smask) == Smissing){
1151 d->state = Dmissing;
1169 tsleep(&up->sleep, return0, 0, Nms);
1170 for(i = 0; i < niadrive; i++)
1171 checkdrive(iadrive[i], i);
1176 iainterrupt(Ureg *u, void *a)
1185 cause = c->hba->isr;
1186 for(i = 0; cause; i++){
1188 if((cause & m) == 0)
1191 d = c->rawdrive + i;
1193 if(d->port->isr && c->hba->pi & m)
1204 ahciencreset(Ctlr *c)
1208 if(c->enctype == Eesb)
1212 while(h->emctl & Emrst)
1218 * from the standard: (http://en.wikipedia.org/wiki/IBPI)
1219 * rebuild is preferred as locate+fail; alternate 1hz fail
1220 * we're going to assume no locate led.
1224 Ledsleep = 125, /* 8hz */
1227 L0 = Ledon*Aled | Ledon*Locled,
1228 L1 = Ledon*Aled | Ledoff*Locled,
1229 R0 = Ledon*Aled | Ledon*Locled | Ledon*Errled,
1230 R1 = Ledon*Aled | Ledoff*Errled,
1231 S0 = Ledon*Aled | Ledon*Locled /*| Ledon*Errled*/, /* botch */
1232 S1 = Ledon*Aled | Ledoff*Errled,
1233 P0 = Ledon*Aled | Ledon*Errled,
1234 P1 = Ledon*Aled | Ledoff*Errled,
1235 F0 = Ledon*Aled | Ledon*Errled,
1236 C0 = Ledon*Aled | Ledon*Locled,
1237 C1 = Ledon*Aled | Ledoff*Locled,
1241 //static ushort led3[Ibpilast*8] = {
1242 //[Ibpinone*8] 0, 0, 0, 0, 0, 0, 0, 0,
1243 //[Ibpinormal*8] N0, N0, N0, N0, N0, N0, N0, N0,
1244 //[Ibpirebuild*8] R0, R0, R0, R0, R1, R1, R1, R1,
1245 //[Ibpilocate*8] L0, L1, L0, L1, L0, L1, L0, L1,
1246 //[Ibpispare*8] S0, S1, S0, S1, S1, S1, S1, S1,
1247 //[Ibpipfa*8] P0, P1, P0, P1, P1, P1, P1, P1, /* first 1 sec */
1248 //[Ibpifail*8] F0, F0, F0, F0, F0, F0, F0, F0,
1249 //[Ibpicritarray*8] C0, C0, C0, C0, C1, C1, C1, C1,
1250 //[Ibpifailarray*8] C0, C1, C0, C1, C0, C1, C0, C1,
1253 static ushort led2[Ibpilast*8] = {
1254 [Ibpinone*8] 0, 0, 0, 0, 0, 0, 0, 0,
1255 [Ibpinormal*8] N0, N0, N0, N0, N0, N0, N0, N0,
1256 [Ibpirebuild*8] R0, R0, R0, R0, R1, R1, R1, R1,
1257 [Ibpilocate*8] L0, L0, L0, L0, L0, L0, L0, L0,
1258 [Ibpispare*8] S0, S0, S0, S0, S1, S1, S1, S1,
1259 [Ibpipfa*8] P0, P1, P0, P1, P1, P1, P1, P1, /* first 1 sec */
1260 [Ibpifail*8] F0, F0, F0, F0, F0, F0, F0, F0,
1261 [Ibpicritarray*8] C0, C0, C0, C0, C1, C1, C1, C1,
1262 [Ibpifailarray*8] C0, C1, C0, C1, C0, C1, C0, C1,
1266 ledstate(Ledport *p, uint seq)
1270 if(p->led == Ibpipfa && seq%32 >= 8)
1273 i = led2[8*p->led + seq%8];
1274 if(i != p->ledbits){
1276 ledprint("ledstate %,.011ub %ud\n", p->ledbits, seq);
1283 blink(Drive *d, ulong t)
1289 if(ledstate(d, t) == 0)
1293 /* ensure last message has been transmitted */
1294 while(h->emctl & Tmsg)
1298 panic("%s: bad led type %d", dnam(d), c->enctype);
1300 memset(&msg, 0, sizeof msg);
1303 msg.msize = sizeof msg - 4;
1304 msg.led[0] = d->ledbits;
1305 msg.led[1] = d->ledbits>>8;
1307 msg.hba = d->driveno;
1308 memmove(c->enctx, &msg, sizeof msg);
1316 Esbdrv0 = 4, /* start pos in bits */
1317 Esbiota = 3, /* shift in bits */
1326 uint i, e; /* except after c */
1329 for(i = 0; i < 3; i++)
1330 e |= ((s>>3*i & 7) != 0)<<i;
1335 blinkesb(Ctlr *c, ulong t)
1342 for(i = 0; i < c->ndrive; i++){
1344 s |= ledstate(d, t); /* no port mapping */
1348 memset(u, 0, sizeof u);
1349 for(i = 0; i < c->ndrive; i++){
1351 s = Esbdrv0 + Esbiota*i;
1352 v = esbbits(d->ledbits) * (1ull << s%32);
1354 u[s/32 + 1] |= v>>32;
1356 for(i = 0; i < c->encsz; i++)
1362 ahciledr(SDunit *u, Chan *ch, void *a, long n, vlong off)
1368 d = c->drive[u->subno];
1369 return ledr(d, ch, a, n, off);
1373 ahciledw(SDunit *u, Chan *ch, void *a, long n, vlong off)
1379 d = c->drive[u->subno];
1380 return ledw(d, ch, a, n, off);
1392 memset(map, 0, sizeof map);
1393 for(i = 0; i < niactlr; i++)
1394 if(iactlr[i].enctype != 0){
1395 ahciencreset(iactlr + i);
1400 pexit("no work", 1);
1401 for(i = 0; i < niadrive; i++){
1402 iadrive[i]->nled = 3; /* hardcoded */
1403 if(iadrive[i]->ctlr->enctype == Eesb)
1404 iadrive[i]->nled = 3;
1405 iadrive[i]->ledbits = -1;
1409 for(j = 0; j < niadrive; ){
1410 c = iadrive[j]->ctlr;
1413 else if(c->enctype == Eesb){
1422 esleep(Ledsleep - TK2MS(t1 - t0));
1431 for(i = 0; i < 15000; i += 250){
1432 if(d->state == Dreset || d->state == Dportreset || d->state == Dnew)
1434 δ = Ticks - d->lastseen;
1435 if(d->state == Dnull || δ > 10*1000)
1437 s = d->port->sstatus;
1438 if((s & Imask) == 0 && δ > 1500)
1440 if(d->state == Dready && (s & Smask) == Sphylink)
1444 print("%s: not responding; offline\n", dnam(d));
1445 dstatus(d, Doffline);
1456 d = c->drive[u->subno];
1462 sdaddfile(u, "led", 0644, eve, ahciledr, ahciledw);
1467 checkdrive(d, d->driveno); /* c->d0 + d->driveno */
1469 while(waitready(d) == 1)
1472 if(d->portm.feat & Datapi)
1486 d = c->drive[u->subno];
1488 while(waitready(d) == 1)
1492 if(d->portm.feat & Datapi){
1495 return scsionline(u);
1501 }else if(d->state == Dready)
1504 u->sectors = d->sectors;
1505 u->secsize = d->secsize;
1523 kproc("iasata", satakproc, 0);
1525 panic("iaenable: zero s->ctlr->ndrive");
1527 snprint(name, sizeof name, "%s (%s)", s->name, s->ifc->name);
1528 intrenable(c->pci->intl, iainterrupt, c, c->pci->tbdf, name);
1529 /* supposed to squelch leftover interrupts here. */
1532 if(++once == niactlr)
1533 kproc("ialed", ledkproc, 0);
1547 ahcidisable(c->hba);
1548 snprint(name, sizeof name, "%s (%s)", s->name, s->ifc->name);
1549 intrdisable(c->pci->intl, iainterrupt, c, c->pci->tbdf, name);
1556 ahcibuild(Aportm *m, int rw, void *data, uint n, vlong lba)
1564 rwfis(m, c, rw, n, lba);
1568 mkalist(m, flags, data, 512*n);
1573 ahcibuildpkt(Aportm *m, SDreq *r, void *data, int n)
1583 atapirwfis(m, c, r->cmd, r->clen, n);
1584 flags = 1<<16 | Lpref | Latapi;
1585 if(r->write != 0 && data)
1587 mkalist(m, flags, data, n);
1592 ahcibuildfis(Aportm *m, SDreq *r, void *data, uint n)
1600 if((r->ataproto & Pprotom) != Ppkt){
1601 memmove(c, r->cmd, r->clen);
1603 if(r->write || n == 0)
1605 mkalist(m, flags, data, n);
1607 atapirwfis(m, c, r->cmd, r->clen, n);
1608 flags = 1<<16 | Lpref | Latapi;
1609 if(r->write && data)
1611 mkalist(m, flags, data, n);
1622 while ((i = waitready(d)) == 1) {
1631 flushcache(Drive *d)
1636 if(lockready(d) == 0)
1637 i = ahciflushcache(&d->portc);
1643 io(Drive *d, uint proto, int to, int interrupt)
1645 uint task, flag, rv;
1649 switch(waitready(d)){
1666 d->totick = Ticks + MS2TK(to) | 1; /* fix fencepost */
1673 if(ahcicomreset(&d->portc) == -1)
1678 sleep(&d->portm, ahciclear, &as);
1683 flag = d->portm.flag;
1684 task = d->port->task;
1689 rv = task >> 8 + 4 & 0xf;
1692 }else if(task & (Efatal<<8) || task & (ASbsy|ASdrq) && d->state == Dready){
1694 ahcirecover(&d->portc);
1695 task = d->port->task;
1696 flag &= ~Fdone; /* either an error or do-over */
1699 print("%s: retry\n", dnam(d));
1702 if(flag & (Fahdrs | Ferror)){
1703 if((task & Eidnf) == 0)
1704 print("%s: i/o error %ux\n", dnam(d), task);
1711 iariopkt(SDreq *r, Drive *d)
1713 int n, count, try, max;
1717 aprint("%s: %.2ux %.2ux %c %d %p\n", dnam(d), cmd[0], cmd[2],
1718 "rw"[r->write], r->dlen, r->data);
1723 for(try = 0; try < 10; try++){
1728 ahcibuildpkt(&d->portm, r, r->data, n);
1729 r->status = io(d, Ppkt, 5000, 0);
1737 r->rlen = d->portm.list->len;
1740 print("%s: bad disk\n", dnam(d));
1741 return r->status = SDcheck;
1745 ahcibio(SDunit *u, int lun, int write, void *a, long count, uvlong lba)
1747 int n, rw, try, status, max;
1753 d = c->drive[u->subno];
1754 if(d->portm.feat & Datapi)
1755 return scsibio(u, lun, write, a, count, lba);
1758 if(d->portm.feat & Dllba){
1759 max = 8192; /* ahci maximum */
1760 if(c->type == Tsb600)
1761 max = 255; /* errata */
1763 rw = write? SDwrite: SDread;
1765 dprint("%s: bio: %llud %c %lud (max %d) %p\n",
1766 dnam(d), lba, "rw"[rw], count, max, data);
1767 for(try = 0; try < 10;){
1772 ahcibuild(&d->portm, rw, data, n, lba);
1773 status = io(d, Pdma, 5000, 0);
1785 data += n * u->secsize;
1787 return data - (uchar*)a;
1789 print("%s: bad disk\n", dnam(d));
1796 int i, n, count, rw;
1805 d = c->drive[u->subno];
1806 if(d->portm.feat & Datapi)
1807 return iariopkt(r, d);
1810 if(cmd[0] == 0x35 || cmd[0] == 0x91){
1811 if(flushcache(d) == 0)
1812 return sdsetsense(r, SDok, 0, 0, 0);
1813 return sdsetsense(r, SDcheck, 3, 0xc, 2);
1816 if((i = sdfakescsi(r)) != SDnostatus){
1821 if((i = sdfakescsirw(r, &lba, &count, &rw)) != SDnostatus)
1823 n = ahcibio(u, r->lun, r->write, r->data, count, lba);
1830 static uchar bogusrfis[16] = {
1843 memmove(c, bogusrfis, sizeof bogusrfis);
1848 sdr(SDreq *r, Drive *d, int st)
1853 if((r->ataproto & Pprotom) == Ppkt){
1856 st = t >> 8 + 4 & 0xf;
1859 memmove(r->cmd, c, 16);
1867 fisreqchk(Sfis *f, SDreq *r)
1869 if((r->ataproto & Pprotom) == Ppkt)
1872 * handle oob requests;
1873 * restrict & sanitize commands
1877 if(r->cmd[0] == 0xf0){
1878 sigtofis(f, r->cmd);
1898 d = c->drive[u->subno];
1900 if((r->status = fisreqchk(&d->portm, r)) != SDnostatus)
1904 for(try = 0; try < 10; try++){
1906 ahcibuildfis(&d->portm, r, r->data, r->dlen);
1907 r->status = io(d, r->ataproto & Pprotom, -1, 1);
1911 return sdsetsense(r, SDcheck, 11, 0, 6);
1918 if((r->ataproto & Pprotom) == Ppkt)
1919 r->rlen = d->portm.list->len;
1920 return sdr(r, d, r->status);
1922 print("%s: bad disk\n", dnam(d));
1923 return r->status = SDeio;
1927 * configure drives 0-5 as ahci sata (c.f. errata)
1930 iaahcimode(Pcidev *p)
1934 u = pcicfgr16(p, 0x92);
1935 dprint("ahci: %T: iaahcimode %.2ux %.4ux\n", p->tbdf, pcicfgr8(p, 0x91), u);
1936 pcicfgw16(p, 0x92, u | 0xf); /* ports 0-15 */
1941 Ghc = 0x04/4, /* global host control */
1942 Pi = 0x0c/4, /* ports implemented */
1943 Cmddec = 1<<15, /* enable command block decode */
1946 Ahcien = 1<<31, /* ahci enable */
1950 iasetupahci(Ctlr *c)
1952 pcicfgw16(c->pci, 0x40, pcicfgr16(c->pci, 0x40) & ~Cmddec);
1953 pcicfgw16(c->pci, 0x42, pcicfgr16(c->pci, 0x42) & ~Cmddec);
1955 c->lmmio[Ghc] |= Ahcien;
1956 c->lmmio[Pi] = (1 << 6) - 1; /* 5 ports (supposedly ro pi reg) */
1958 /* enable ahci mode; from ich9 datasheet */
1959 pcicfgw16(c->pci, 0x90, 1<<6 | 1<<5);
1963 sbsetupahci(Pcidev *p)
1965 print("sbsetupahci: tweaking %.4ux ccru %.2ux ccrp %.2ux\n",
1966 p->did, p->ccru, p->ccrp);
1967 pcicfgw8(p, 0x40, pcicfgr8(p, 0x40) | 1);
1968 pcicfgw8(p, PciCCRu, 6);
1969 pcicfgw8(p, PciCCRp, 1);
1978 c->enctx = (ulong*)(c->mmio + 0xa0);
1985 ahciencinit(Ctlr *c)
1987 ulong type, sz, o, *bar;
1993 if((h->cap & Hems) == 0)
1995 type = h->emctl & Emtype;
2007 sz = h->emloc & 0xffff;
2009 if(sz == 0 || o == 0)
2012 dprint("size = %.4lux; loc = %.4lux*4\n", sz, o);
2015 if((h->emctl & Xonly) == 0){
2019 c->encrx = bar + o*2;
2035 if((p->did & 0xfffc) == 0x2680)
2037 if((p->did & 0xfffb) == 0x27c1)
2038 return Tich; /* 82801g[bh]m */
2039 if((p->did & 0xffff) == 0x2822)
2040 return Tich; /* 82801 SATA RAID */
2041 if((p->did & 0xffff) == 0x2821)
2042 return Tich; /* 82801h[roh] */
2043 if((p->did & 0xfffe) == 0x2824)
2044 return Tich; /* 82801h[b] */
2045 if((p->did & 0xfeff) == 0x2829)
2046 return Tich; /* ich8 */
2047 if((p->did & 0xfffe) == 0x2922)
2048 return Tich; /* ich9 */
2049 if((p->did & 0xffff) == 0x3a02)
2050 return Tich; /* 82801jd/do */
2051 if((p->did & 0xfefe) == 0x3a22)
2052 return Tich; /* ich10, pch */
2053 if((p->did & 0xfff7) == 0x3b28)
2054 return Tich; /* pchm */
2055 if((p->did & 0xfffe) == 0x3b22)
2056 return Tich; /* pch */
2059 if(p->ccru == 1 || p->ccrp != 1)
2060 if(p->did == 0x4380 || p->did == 0x4390)
2066 * unconfirmed report that the programming
2067 * interface is set incorrectly.
2069 if(p->did == 0x3349)
2073 /* Hudson SATA Controller [AHCI mode] */
2074 if(p->did == 0x7801)
2087 if(p->ccrb == Pcibcstore && p->ccru == 6 && p->ccrp == 1)
2095 int i, n, nunit, type;
2107 if(getconf("*ahcidebug") != nil){
2112 memset(olds, 0xff, sizeof olds);
2115 while((p = pcimatch(p, 0, 0)) != nil){
2116 if((type = didtype(p)) == -1)
2118 if(p->mem[Abar].bar == 0)
2120 if(niactlr == NCtlr){
2121 print("iapnp: %s: too many controllers\n", tname[type]);
2124 c = iactlr + niactlr;
2125 s = sdevs + niactlr;
2126 memset(c, 0, sizeof *c);
2127 memset(s, 0, sizeof *s);
2128 io = p->mem[Abar].bar & ~0xf;
2129 c->mmio = vmap(io, p->mem[Abar].size);
2131 print("%s: address %#p in use did %.4ux\n",
2132 Tname(c), io, p->did);
2135 c->lmmio = (ulong*)c->mmio;
2139 s->ifc = &sdiahciifc;
2144 ahcihandoff((Ahba*)c->mmio);
2145 if(intel(c) && p->did != 0x2681)
2147 // ahcihbareset((Ahba*)c->mmio);
2148 nunit = ahciconf(c);
2149 if(intel(c) && iaahcimode(p) == -1 || nunit < 1){
2150 vunmap(c->mmio, p->mem[Abar].size);
2153 c->ndrive = s->nunit = nunit;
2155 /* map the drives -- they don't all need to be enabled. */
2156 memset(c->rawdrive, 0, sizeof c->rawdrive);
2158 for(i = 0; i < NCtlrdrv; i++){
2159 d = c->rawdrive + i;
2165 if((c->hba->pi & 1<<i) == 0)
2167 snprint(d->name, sizeof d->name, "iahci%d.%d", niactlr, i);
2168 d->port = (Aport*)(c->mmio + 0x80*i + 0x100);
2169 d->portc.p = d->port;
2170 d->portc.m = &d->portm;
2172 c->drive[d->driveno] = d;
2173 iadrive[niadrive + d->driveno] = d;
2175 for(i = 0; i < n; i++)
2176 if(ahciidle(c->drive[i]->port) == -1){
2177 print("%s: port %d wedged; abort\n",
2181 for(i = 0; i < n; i++){
2182 c->drive[i]->mode = DMautoneg;
2183 configdrive(c->drive[i]);
2190 i = (c->hba->cap >> 21) & 1;
2191 print("#S/%s: %s: sata-%s with %d ports\n", s->name,
2192 Tname(c), "I\0II" + i*2, nunit);
2197 static Htab ctab[] = {
2219 capfmt(char *p, char *e, Htab *t, int n, ulong cap)
2224 for(i = 0; i < n; i++)
2226 p = seprint(p, e, "%s ", t[i].name);
2231 iarctl(SDunit *u, char *p, int l)
2233 char buf[32], *e, *op;
2238 if((c = u->dev->ctlr) == nil)
2240 d = c->drive[u->subno];
2245 if(d->state == Dready){
2246 p = seprint(p, e, "model\t%s\n", d->model);
2247 p = seprint(p, e, "serial\t%s\n", d->serial);
2248 p = seprint(p, e, "firm\t%s\n", d->firmware);
2250 p = seprint(p, e, "wwn\t%ullx\n", d->wwn);
2251 p = seprint(p, e, "flag\t");
2252 p = pflag(p, e, &d->portm);
2253 p = seprint(p, e, "udma\t%d\n", d->portm.udma);
2255 p = seprint(p, e, "no disk present [%s]\n", diskstates[d->state]);
2256 serrstr(o->serror, buf, buf + sizeof buf - 1);
2257 p = seprint(p, e, "reg\ttask %lux cmd %lux serr %lux %s ci %lux is %lux "
2258 "sig %lux sstatus %.3lux\n", o->task, o->cmd, o->serror, buf,
2259 o->ci, o->isr, o->sig, o->sstatus);
2260 p = seprint(p, e, "cmd\t");
2261 p = capfmt(p, e, ctab, nelem(ctab), o->cmd);
2262 p = seprint(p, e, "\n");
2263 p = seprint(p, e, "mode\t%s %s\n", modes[d->mode], modes[maxmode(c)]);
2264 p = seprint(p, e, "geometry %llud %lud\n", d->sectors, u->secsize);
2265 p = seprint(p, e, "alignment %d %d\n",
2266 d->secsize<<d->portm.physshift, d->portm.physalign);
2267 p = seprint(p, e, "missirq\t%ud\n", c->missirq);
2272 forcemode(Drive *d, char *mode)
2276 for(i = 0; i < nelem(modes); i++)
2277 if(strcmp(mode, modes[i]) == 0)
2279 if(i == nelem(modes))
2287 forcestate(Drive *d, char *state)
2291 for(i = 0; i < nelem(diskstates); i++)
2292 if(strcmp(state, diskstates[i]) == 0)
2294 if(i == nelem(diskstates))
2300 runsettxmode(Drive *d, char *s)
2310 if(lockready(d) == 0){
2312 if(settxmode(c, m->udma) == 0)
2321 iawctl(SDunit *u, Cmdbuf *cmd)
2328 d = c->drive[u->subno];
2331 if(strcmp(f[0], "mode") == 0)
2332 forcemode(d, f[1]? f[1]: "satai");
2333 else if(strcmp(f[0], "state") == 0)
2334 forcestate(d, f[1]? f[1]: "null");
2335 else if(strcmp(f[0], "txmode") == 0){
2336 if(runsettxmode(d, f[1]? f[1]: "0"))
2337 cmderror(cmd, "bad txmode / stuck port");
2339 cmderror(cmd, Ebadctl);
2344 portr(char *p, char *e, uint x)
2350 for(i = 0; i < 32; i++){
2351 if((x & (1<<i)) == 0){
2352 if(a != -1 && i - 1 != a)
2353 p = seprint(p, e, "-%d", i - 1);
2359 p = seprint(p, e, ", ");
2360 p = seprint(p, e, "%d", a = i);
2363 if(a != -1 && i - 1 != a)
2364 p = seprint(p, e, "-%d", i - 1);
2368 static Htab htab[] = {
2388 static Htab htab2[] = {
2394 static Htab emtab[] = {
2406 iartopctl(SDev *s, char *p, char *e)
2416 p = seprint(p, e, "sd%c ahci %s port %#p: ", s->idno, Tname(c), h);
2417 p = capfmt(p, e, htab, nelem(htab), cap);
2418 p = capfmt(p, e, htab2, nelem(htab2), h->cap2);
2419 p = capfmt(p, e, emtab, nelem(emtab), h->emctl);
2420 portr(pr, pr + sizeof pr, h->pi);
2421 return seprint(p, e,
2422 "iss %ld ncs %ld np %ld ghc %lux isr %lux pi %lux %s ver %lux\n",
2423 (cap>>20) & 0xf, (cap>>8) & 0x1f, 1 + (cap & 0x1f),
2424 h->ghc, h->isr, h->pi, pr, h->ver);
2428 iawtopctl(SDev *, Cmdbuf *cmd)
2436 if(strcmp(f[0], "debug") == 0)
2438 else if(strcmp(f[0], "idprint") == 0)
2440 else if(strcmp(f[0], "aprint") == 0)
2442 else if(strcmp(f[0], "ledprint") == 0)
2445 cmderror(cmd, Ebadctl);
2449 cmderror(cmd, Ebadarg);
2455 *v = strcmp(f[1], "on") == 0;
2463 SDifc sdiahciifc = {