2 * intel/amd ahci sata controller
3 * copyright © 2007-8 coraid, inc.
7 #include "../port/lib.h"
12 #include "../port/error.h"
13 #include "../port/sd.h"
16 #define dprint(...) if(debug) iprint(__VA_ARGS__); else USED(debug)
17 #define idprint(...) if(prid) iprint(__VA_ARGS__); else USED(prid)
18 #define aprint(...) if(datapi) iprint(__VA_ARGS__); else USED(datapi)
19 #define Tname(c) tname[(c)->type]
24 NDrive = NCtlr*NCtlrdrv,
30 /* pci space configuration */
44 #define Intel(x) ((x)->pci->vid == 0x8086)
46 static char *tname[] = {
65 static char *diskstates[Dlast] = {
76 extern SDifc sdiahciifc;
77 typedef struct Ctlr Ctlr;
85 static char *modename[] = {
91 static char *flagname[] = {
108 Aportc portc; /* redundant ptr to port and portm */
116 ulong intick; /* start tick of current transfer */
119 uchar mode; /* DMautoneg, satai or sataii */
128 int driveno; /* ctlr*NCtlrdrv + unit */
129 /* controller port # != driveno when not all ports are enabled */
145 Drive rawdrive[NCtlrdrv];
146 Drive* drive[NCtlrdrv];
151 static Ctlr iactlr[NCtlr];
152 static SDev sdevs[NCtlr];
155 static Drive *iadrive[NDrive];
158 /* these are fiddled in iawtopctl() */
163 static char stab[] = {
165 [8] 't', 'c', 'p', 'e',
166 [16] 'N', 'I', 'W', 'B', 'D', 'C', 'H', 'S', 'T', 'F', 'X'
170 serrstr(ulong r, char *s, char *e)
175 for(i = 0; i < nelem(stab) && s < e; i++)
176 if(r & (1<<i) && stab[i]){
184 static char ntab[] = "0123456789abcdef";
187 preg(uchar *reg, int n)
190 char buf[25*3+1], *e;
193 for(i = 0; i < n; i++){
194 *e++ = ntab[reg[i]>>4];
195 *e++ = ntab[reg[i]&0xf];
204 dreg(char *s, Aport *p)
206 dprint("ahci: %stask=%lux; cmd=%lux; ci=%lux; is=%lux\n",
207 s, p->task, p->cmd, p->ci, p->isr);
215 tsleep(&up->sleep, return0, 0, ms);
230 return (s->p->ci & s->i) == 0;
234 aesleep(Aportm *m, Asleep *a, int ms)
238 tsleep(m, ahciclear, a, ms);
243 ahciwait(Aportc *c, int ms)
252 aesleep(c->m, &as, ms);
253 if((p->task&1) == 0 && p->ci == 0)
255 dreg("ahciwait timeout ", c->p);
266 if((pc->m->feat & Dnop) == 0)
276 c[7] = 0xa0; /* obsolete device bits */
279 l->flags = Lwrite | 0x5;
281 l->ctab = PCIWADDR(t);
284 return ahciwait(pc, 3*1000);
288 setfeatures(Aportc *pc, uchar f)
302 c[7] = 0xa0; /* obsolete device bits */
305 l->flags = Lwrite | 0x5;
307 l->ctab = PCIWADDR(t);
310 return ahciwait(pc, 3*1000);
314 setudmamode(Aportc *pc, uchar f)
321 if((pc->p->sig >> 16) == 0xeb14)
331 c[3] = 3; /* set transfer mode */
332 c[7] = 0xa0; /* obsolete device bits */
333 c[12] = 0x40 | f; /* sector count */
336 l->flags = Lwrite | 0x5;
338 l->ctab = PCIWADDR(t);
341 return ahciwait(pc, 3*1000);
354 ahciportreset(Aportc *c)
362 for(i = 0; i < 500; i += 25){
367 p->sctl = 1|(p->sctl&~7);
374 smart(Aportc *pc, int n)
380 if((pc->m->feat&Dsmart) == 0)
390 c[3] = 0xd8 + n; /* able smart */
396 l->flags = Lwrite | 0x5;
398 l->ctab = PCIWADDR(t);
401 if(ahciwait(pc, 1000) == -1 || pc->p->task & (1|32)){
402 dprint("ahci: smart fail %lux\n", pc->p->task);
403 // preg(pc->m->fis.r, 20);
425 c[3] = 0xda; /* return smart status */
431 l->flags = Lwrite | 0x5;
433 l->ctab = PCIWADDR(t);
437 if(ahciwait(pc, 1000) == -1 || pc->p->task & (1|32)){
438 dprint("ahci: smart fail %lux\n", pc->p->task);
442 if(c[5] == 0x4f && c[6] == 0xc2)
448 ahciflushcache(Aportc *pc)
453 static uchar tab[2] = {0xe7, 0xea};
455 llba = pc->m->feat&Dllba? 1: 0;
466 l->flags = Lwrite | 0x5;
468 l->ctab = PCIWADDR(t);
471 if(ahciwait(pc, 60000) == -1 || pc->p->task & (1|32)){
472 dprint("ahciflushcache: fail %lux\n", pc->p->task);
473 // preg( pc->m->fis.r, 20);
485 return i[1]<<8 | i[0];
508 return (uvlong)gbit32(i+4) << 32 | gbit32(a);
512 ahciidentify0(Aportc *pc, void *id, int atapi)
518 static uchar tab[] = { 0xec, 0xa1, };
527 c[7] = 0xa0; /* obsolete device bits */
530 l->flags = 1<<16 | 0x5;
532 l->ctab = PCIWADDR(t);
535 memset(id, 0, 0x100);
537 p->dba = PCIWADDR(id);
539 p->count = 1<<31 | (0x200-2) | 1;
541 return ahciwait(pc, 3*1000);
545 ahciidentify(Aportc *pc, ushort *id)
555 sig = pc->p->sig >> 16;
560 if(ahciidentify0(pc, id, i) == -1)
563 i = gbit16(id+83) | gbit16(id+86);
596 for(i = 0; i < 500; i += 50){
603 if((a->task & (ASdrq|ASbsy)) == 0){
609 for(i = 0; i < 500; i += 50){
617 dprint("ahci: clo clear %lx\n", a->task);
625 ahcicomreset(Aportc *pc)
631 dprint("ahcicomreset\n");
632 dreg("ahci: comreset ", pc->p);
633 if(ahciquiet(pc->p) == -1){
634 dprint("ahciquiet failed\n");
637 dreg("comreset ", pc->p);
645 c[7] = 0xa0; /* obsolete device bits */
646 c[15] = 1<<2; /* srst */
649 l->flags = Lclear | Lreset | 0x5;
651 l->ctab = PCIWADDR(t);
654 if(ahciwait(pc, 500) == -1){
655 dprint("ahcicomreset: first command failed\n");
659 dreg("comreset ", pc->p);
664 c[7] = 0xa0; /* obsolete device bits */
667 l->flags = Lwrite | 0x5;
669 l->ctab = PCIWADDR(t);
672 if(ahciwait(pc, 150) == -1){
673 dprint("ahcicomreset: second command failed\n");
676 dreg("comreset ", pc->p);
681 ahciidle(Aport *port)
690 for(i = 0; i < 500; i += 25){
700 for(i = 0; i < 500; i += 25){
709 * § 6.2.2.1 first part; comreset handled by reset disk.
710 * - remainder is handled by configdisk.
711 * - ahcirecover is a quick recovery from a failed command.
714 ahciswreset(Aportc *pc)
722 if(pc->p->task & (ASdrq|ASbsy))
728 ahcirecover(Aportc *pc)
732 if(setudmamode(pc, 5) == -1)
738 malign(int size, int align)
742 v = xspanalloc(size, align, 0);
750 f->base = malign(0x100, 0x100);
752 f->p = f->base + 0x20;
753 f->r = f->base + 0x40;
754 f->u = f->base + 0x60;
755 f->devicebits = (ulong*)(f->base + 0x58);
764 if((s & 0xF00) != 0x600)
766 if((s & 7) != 1){ /* not (device, no phy) */
767 iprint("ahci: slumbering drive unwakable %ux\n", s);
770 p->sctl = 3*Aipm | 0*Aspd | Adet;
773 // iprint("ahci: wake %ux -> %ux\n", s, p->sstatus);
777 ahciconfigdrive(Ahba *h, Aportc *c, int mode)
787 m->list = malign(sizeof *m->list, 1024);
788 m->ctab = malign(sizeof *m->ctab, 128);
791 if(p->sstatus & 3 && h->cap & Hsss){
792 /* device connected & staggered spin-up */
793 dprint("ahci: configdrive: spinning up ... [%lux]\n",
801 p->list = PCIWADDR(m->list);
803 p->fis = PCIWADDR(m->fis.base);
807 if((p->sstatus & 0xF0F) == 0x601) /* drive coming up in slumbering? */
810 /* disable power managment sequence from book. */
811 p->sctl = (3*Aipm) | (mode*Aspd) | (0*Adet);
839 for(i = 0; i < 32; i++)
851 h = ctlr->hba = (Ahba*)ctlr->mmio;
857 print("#S/sd%c: ahci %s port %#p: sss %ld ncs %ld coal %ld "
858 "mports %ld led %ld clo %ld ems %ld\n",
859 ctlr->sdev->idno, tname[ctlr->type], h,
860 (u>>27) & 1, (u>>8) & 0x1f, (u>>7) & 1, u & 0x1f, (u>>25) & 1,
861 (u>>24) & 1, (u>>6) & 1);
862 return countbits(h->pi);
866 ahcihbareset(Ahba *h)
871 for(wait = 0; wait < 1000; wait += 100){
880 idmove(char *p, ushort *a, int n)
886 for(i = 0; i < n/2; i++){
891 while(p > op && *--p == ' ')
894 for (p = op; *p == ' '; p++)
896 memmove(op, p, n - (e - p));
908 s = ahciidentify(&d->portc, id);
913 osectors = d->sectors;
914 memmove(oserial, d->serial, sizeof d->serial);
918 d->secsize = u->secsize;
920 d->secsize = 512; /* default */
923 idmove(d->serial, id+10, 20);
924 idmove(d->firmware, id+23, 8);
925 idmove(d->model, id+27, 40);
927 memset(u->inquiry, 0, sizeof u->inquiry);
930 u->inquiry[4] = sizeof u->inquiry - 4;
931 memmove(u->inquiry+8, d->model, 40);
933 if(osectors != s || memcmp(oserial, d->serial, sizeof oserial) != 0){
950 updatedrive(Drive *d)
952 ulong cause, serr, s0, pr, ewake;
964 if(d->unit && d->unit->name)
965 name = d->unit->name;
968 d->portm.flag |= Fdone;
971 }else if(cause & Adps)
975 dprint("ahci: updatedrive: fatal\n");
978 if(p->task & (1<<5|1)){
979 dprint("ahci: Adhrs cause %lux serr %lux task %lux\n",
980 cause, serr, p->task);
981 d->portm.flag |= Ferror;
986 if(p->task & 1 && last != cause)
987 dprint("%s: err ca %lux serr %lux task %lux sstat %lux\n",
988 name, cause, serr, p->task, p->sstatus);
990 dprint("%s: upd %lux ta %lux\n", name, cause, p->task);
992 if(cause & (Aprcs|Aifs)){
994 switch(p->sstatus & 7){
995 case 0: /* no device */
998 case 1: /* device but no phy comm. */
999 if((p->sstatus & 0xF00) == 0x600)
1000 d->state = Dnew; /* slumbering */
1004 case 3: /* device & phy comm. estab. */
1005 /* power mgnt crap for surprise removal */
1006 p->ie |= Aprcs|Apcs; /* is this required? */
1009 case 4: /* phy off-line */
1010 d->state = Doffline;
1013 dprint("%s: %s → %s [Apcrs] %lux\n", name,
1014 diskstates[s0], diskstates[d->state], p->sstatus);
1015 /* print pulled message here. */
1016 if(s0 == Dready && d->state != Dready)
1017 idprint("%s: pulled\n", name);
1018 if(d->state != Dready)
1019 d->portm.flag |= Ferror;
1031 pstatus(Drive *d, ulong s)
1034 * bogus code because the first interrupt is currently dropped.
1035 * likely my fault. serror may be cleared at the wrong time.
1038 case 0: /* no device */
1039 d->state = Dmissing;
1041 case 1: /* device but no phy. comm. */
1043 case 2: /* should this be missing? need testcase. */
1044 dprint("ahci: pstatus 2\n");
1046 case 3: /* device & phy. comm. */
1050 case 4: /* offline */
1051 d->state = Doffline;
1053 case 6: /* ? not sure this makes sense. TODO */
1060 configdrive(Drive *d)
1062 if(ahciconfigdrive(d->ctlr->hba, &d->portc, d->mode) == -1)
1065 pstatus(d, d->port->sstatus & 7);
1073 uint state, det, stat;
1078 stat = p->sstatus & 7;
1079 state = (p->cmd>>28) & 0xf;
1080 dprint("ahci: resetdisk: icc %ux det %d sdet %d\n", state, det, stat);
1084 if(d->state != Dready || d->state != Dnew)
1085 d->portm.flag |= Ferror;
1086 clearci(p); /* satisfy sleep condition. */
1088 if(stat != 3){ /* device absent or phy not communicating? */
1089 d->state = Dportreset;
1097 if(p->cmd&Ast && ahciswreset(&d->portc) == -1){
1099 d->state = Dportreset; /* get a bigger stick. */
1103 d->state = Dmissing;
1108 dprint("ahci: resetdisk: %s → %s\n",
1109 diskstates[state], diskstates[d->state]);
1123 name = d->unit->name;
1127 if(d->port->task == 0x80)
1130 if(setudmamode(c, 5) == -1){
1131 dprint("%s: can't set udma mode\n", name);
1134 if(identify(d) == -1){
1135 dprint("%s: identify failure\n", name);
1138 if(m->feat & Dpower && setfeatures(c, 0x85) == -1){
1140 if(ahcirecover(c) == -1)
1150 idprint("%s: %sLBA %,llud sectors: %s %s %s %s\n", d->unit->name,
1151 (m->feat & Dllba? "L": ""), d->sectors, d->model, d->firmware,
1152 d->serial, d->mediachange? "[mediachange]": "");
1156 idprint("%s: can't be initialized\n", d->unit->name);
1166 Mphywait = 2*1024/Nms - 1,
1167 Midwait = 16*1024/Nms - 1,
1168 Mcomrwait = 64*1024/Nms - 1,
1172 westerndigitalhung(Drive *d)
1174 if((d->portm.feat&Datapi) == 0 && d->active &&
1175 TK2MS(MACHP(0)->ticks - d->intick) > 5000){
1176 dprint("%s: drive hung; resetting [%lux] ci %lx\n",
1177 d->unit->name, d->port->task, d->port->ci);
1182 static ushort olds[NCtlr*NCtlrdrv];
1185 doportreset(Drive *d)
1191 if(ahciportreset(&d->portc) == -1)
1192 dprint("ahci: doportreset: fails\n");
1196 dprint("ahci: doportreset: portreset → %s [task %lux]\n",
1197 diskstates[d->state], d->port->task);
1201 /* drive must be locked */
1203 statechange(Drive *d)
1208 if(d->unit->sectors != 0){
1220 checkdrive(Drive *d, int i)
1226 print("checkdrive: nil d\n");
1230 if(d->unit == nil || d->port == nil) {
1232 print("checkdrive: nil d->%s\n",
1233 d->unit == nil? "unit": "port");
1237 name = d->unit->name;
1238 s = d->port->sstatus;
1240 d->lastseen = MACHP(0)->ticks;
1242 dprint("%s: status: %04ux -> %04ux: %s\n",
1243 name, olds[i], s, diskstates[d->state]);
1247 westerndigitalhung(d);
1255 case 1: /* no device (pm), device but no phy. comm. */
1256 ahciwakeup(d->port);
1258 case 0: /* no device */
1261 dprint("%s: unknown status %04ux\n", name, s);
1263 case 0x100: /* active, no device */
1264 if(++d->wait&Mphywait)
1267 if(++d->mode > DMsataii)
1269 if(d->mode == DMsatai){ /* we tried everything */
1270 d->state = Dportreset;
1273 dprint("%s: reset; new mode %s\n", name,
1279 case 0x103: /* active, device, phy. comm. */
1280 if((++d->wait&Midwait) == 0){
1281 dprint("%s: slow reset %04ux task=%lux; %d\n",
1282 name, s, d->port->task, d->wait);
1285 s = (uchar)d->port->task;
1286 if(s == 0x7f || ((d->port->sig >> 16) != 0xeb14 &&
1287 (s & ~0x17) != (1<<6)))
1296 if(d->wait++ & Mcomrwait)
1301 dprint("%s: reset [%s]: mode %d; status %04ux\n",
1302 name, diskstates[d->state], d->mode, s);
1309 if(d->wait++ & 0xff && (s & 0x100) == 0)
1311 /* device is active */
1312 dprint("%s: portreset [%s]: mode %d; status %04ux\n",
1313 name, diskstates[d->state], d->mode, s);
1314 d->portm.flag |= Ferror;
1317 if((s & 7) == 0){ /* no device */
1318 d->state = Dmissing;
1336 tsleep(&up->sleep, return0, 0, Nms);
1337 for(i = 0; i < niadrive; i++)
1338 checkdrive(iadrive[i], i);
1343 iainterrupt(Ureg*, void *a)
1352 cause = c->hba->isr;
1353 for(i = 0; i < c->mport; i++){
1355 if((cause & m) == 0)
1357 d = c->rawdrive + i;
1359 if(d->port->isr && c->hba->pi & m)
1374 d = c->drive[u->subno];
1380 checkdrive(d, d->driveno); /* c->d0 + d->driveno */
1396 kproc("iasata", satakproc, 0);
1399 panic("iaenable: zero s->ctlr->ndrive");
1401 snprint(name, sizeof name, "%s (%s)", s->name, s->ifc->name);
1402 intrenable(c->pci->intl, iainterrupt, c, c->pci->tbdf, name);
1403 /* supposed to squelch leftover interrupts here. */
1419 ahcidisable(c->hba);
1420 snprint(name, sizeof name, "%s (%s)", s->name, s->ifc->name);
1421 intrdisable(c->pci->intl, iainterrupt, c, c->pci->tbdf, name);
1428 iaonline(SDunit *unit)
1434 c = unit->dev->ctlr;
1435 d = c->drive[unit->subno];
1438 if(d->portm.feat & Datapi && d->mediachange){
1439 r = scsionline(unit);
1449 /* devsd resets this after online is called; why? */
1450 unit->sectors = d->sectors;
1451 unit->secsize = 512; /* default size */
1452 } else if(d->state == Dready)
1458 /* returns locked list! */
1460 ahcibuild(Drive *d, uchar *cmd, void *data, int n, vlong lba)
1462 uchar *c, acmd, dir, llba;
1467 static uchar tab[2][2] = { 0xc8, 0x25, 0xca, 0x35, };
1471 llba = m->feat&Dllba? 1: 0;
1472 acmd = tab[dir][llba];
1483 c[4] = lba; /* sector lba low 7:0 */
1484 c[5] = lba >> 8; /* cylinder low lba mid 15:8 */
1485 c[6] = lba >> 16; /* cylinder hi lba hi 23:16 */
1486 c[7] = 0xa0 | 0x40; /* obsolete device bits + lba */
1488 c[7] |= (lba>>24) & 7;
1490 c[8] = lba >> 24; /* sector (exp) lba 31:24 */
1491 c[9] = lba >> 32; /* cylinder low (exp) lba 39:32 */
1492 c[10] = lba >> 48; /* cylinder hi (exp) lba 48:40 */
1493 c[11] = 0; /* features (exp); */
1495 c[12] = n; /* sector count */
1496 c[13] = n >> 8; /* sector count (exp) */
1498 c[15] = 0; /* control */
1500 *(ulong*)(c + 16) = 0;
1502 l->flags = 1<<16 | Lpref | 0x5; /* Lpref ?? */
1506 l->ctab = PCIWADDR(t);
1510 p->dba = PCIWADDR(data);
1513 panic("ahcibuild: nil d->unit");
1514 p->count = 1<<31 | (d->unit->secsize*n - 2) | 1;
1520 ahcibuildpkt(Aportm *m, SDreq *r, void *data, int n)
1533 fill = m->feat&Datapi16? 16: 12;
1534 if((len = r->clen) > fill)
1536 memmove(t->atapi, r->cmd, len);
1537 memset(t->atapi+len, 0, fill-len);
1545 c[3] = 0; /* features (exp); */
1547 c[4] = 0; /* sector lba low 7:0 */
1548 c[5] = n; /* cylinder low lba mid 15:8 */
1549 c[6] = n >> 8; /* cylinder hi lba hi 23:16 */
1550 c[7] = 0xa0; /* obsolete device bits */
1552 *(ulong*)(c + 8) = 0;
1553 *(ulong*)(c + 12) = 0;
1554 *(ulong*)(c + 16) = 0;
1556 l->flags = 1<<16 | Lpref | Latapi | 0x5;
1557 if(r->write != 0 && data)
1560 l->ctab = PCIWADDR(t);
1567 p->dba = PCIWADDR(data);
1569 p->count = 1<<31 | (n - 2) | 1;
1579 for(i = 0; i < 15000; i += 250){
1580 if(d->state == Dreset || d->state == Dportreset ||
1583 δ = MACHP(0)->ticks - d->lastseen;
1584 if(d->state == Dnull || δ > 10*1000)
1587 s = d->port->sstatus;
1589 if((s & 0x700) == 0 && δ > 1500)
1590 return -1; /* no detect */
1591 if(d->state == Dready && (s & 7) == 3)
1592 return 0; /* ready, present & phy. comm. */
1595 print("%s: not responding; offline\n", d->unit->name);
1597 d->state = Doffline;
1608 while ((i = waitready(d)) == 1) {
1617 flushcache(Drive *d)
1622 if(lockready(d) == 0)
1623 i = ahciflushcache(&d->portc);
1629 iariopkt(SDreq *r, Drive *d)
1631 int n, count, try, max, flag, task;
1638 name = d->unit->name;
1641 aprint("ahci: iariopkt: %02ux %02ux %c %d %p\n",
1642 cmd[0], cmd[2], "rw"[r->write], r->dlen, r->data);
1643 if(cmd[0] == 0x5a && (cmd[2] & 0x3f) == 0x3f)
1644 return sdmodesense(r, cmd, d->info, sizeof d->info);
1655 ahcibuildpkt(&d->portm, r, data, n);
1656 switch(waitready(d)){
1673 d->intick = MACHP(0)->ticks;
1678 sleep(&d->portm, ahciclear, &as);
1683 flag = d->portm.flag;
1684 task = d->port->task;
1687 if(task & (Efatal<<8) || task & (ASbsy|ASdrq) && d->state == Dready){
1689 ahcirecover(&d->portc);
1690 task = d->port->task;
1691 flag &= ~Fdone; /* either an error or do-over */
1696 print("%s: bad disk\n", name);
1697 r->status = SDcheck;
1700 print("%s: retry\n", name);
1704 if((task&Eidnf) == 0)
1705 print("%s: i/o error %ux\n", name, task);
1706 r->status = SDcheck;
1712 r->rlen = data - (uchar*)r->data;
1720 int i, n, count, try, max, flag, task;
1731 c = unit->dev->ctlr;
1732 d = c->drive[unit->subno];
1733 if(d->portm.feat & Datapi)
1734 return iariopkt(r, d);
1736 name = d->unit->name;
1739 if(r->cmd[0] == 0x35 || r->cmd[0] == 0x91){
1740 if(flushcache(d) == 0)
1741 return sdsetsense(r, SDok, 0, 0, 0);
1742 return sdsetsense(r, SDcheck, 3, 0xc, 2);
1745 if((i = sdfakescsi(r, d->info, sizeof d->info)) != SDnostatus){
1750 if(*cmd != 0x28 && *cmd != 0x2a){
1751 print("%s: bad cmd 0x%.2ux\n", name, cmd[0]);
1752 r->status = SDcheck;
1756 lba = cmd[2]<<24 | cmd[3]<<16 | cmd[4]<<8 | cmd[5];
1757 count = cmd[7]<<8 | cmd[8];
1760 if(r->dlen < count * unit->secsize)
1761 count = r->dlen / unit->secsize;
1771 ahcibuild(d, cmd, data, n, lba);
1772 switch(waitready(d)){
1788 d->intick = MACHP(0)->ticks;
1793 sleep(&d->portm, ahciclear, &as);
1798 flag = d->portm.flag;
1799 task = d->port->task;
1802 if(task & (Efatal<<8) ||
1803 task & (ASbsy|ASdrq) && d->state == Dready){
1805 ahcirecover(&d->portc);
1806 task = d->port->task;
1811 print("%s: bad disk\n", name);
1815 iprint("%s: retry %lld\n", name, lba);
1819 iprint("%s: i/o error %ux @%,lld\n", name, task, lba);
1826 data += n * unit->secsize;
1828 r->rlen = data - (uchar*)r->data;
1834 * configure drives 0-5 as ahci sata (c.f. errata)
1837 iaahcimode(Pcidev *p)
1839 dprint("iaahcimode: %ux %ux %ux\n", pcicfgr8(p, 0x91), pcicfgr8(p, 92),
1841 pcicfgw16(p, 0x92, pcicfgr32(p, 0x92) | 0xf); /* ports 0-3 */
1842 // pcicfgw8(p, 0x93, pcicfgr32(p, 9x93) | 3); /* ports 4-5 */
1847 iasetupahci(Ctlr *c)
1849 /* disable cmd block decoding. */
1850 pcicfgw16(c->pci, 0x40, pcicfgr16(c->pci, 0x40) & ~(1<<15));
1851 pcicfgw16(c->pci, 0x42, pcicfgr16(c->pci, 0x42) & ~(1<<15));
1853 c->lmmio[0x4/4] |= 1 << 31; /* enable ahci mode (ghc register) */
1854 c->lmmio[0xc/4] = (1 << 6) - 1; /* 5 ports. (supposedly ro pi reg.) */
1856 /* enable ahci mode; from ich9 datasheet */
1857 pcicfgw16(c->pci, 0x90, 1<<6 | 1<<5);
1865 if((p->did & 0xfffc) == 0x2680)
1868 * 0x27c4 is the intel 82801 in compatibility (not sata) mode.
1870 if ((p->did & 0xfffb) == 0x27c1 || /* 82801g[bh]m ich7 */
1871 p->did == 0x2821 || /* 82801h[roh] */
1872 (p->did & 0xfffe) == 0x2824 || /* 82801h[b] */
1873 (p->did & 0xfeff) == 0x2829 || /* ich8/9m */
1874 (p->did & 0xfffe) == 0x2922 || /* ich9 */
1875 p->did == 0x3a02 || /* 82801jd/do */
1876 (p->did & 0xfefe) == 0x3a22 || /* ich10, pch */
1877 (p->did & 0xfff7) == 0x3b28) /* pchm */
1881 if(p->did == 0x4380)
1885 if(p->ccrb == Pcibcstore && p->ccru == 6 && p->ccrp == 1)
1893 int i, n, nunit, type;
1898 SDev *head, *tail, *s;
1904 memset(olds, 0xff, sizeof olds);
1908 while((p = pcimatch(p, 0, 0)) != nil){
1910 if (type == -1 || p->mem[Abar].bar == 0)
1912 if(niactlr == NCtlr){
1913 print("ahci: iapnp: %s: too many controllers\n",
1917 c = iactlr + niactlr;
1918 s = sdevs + niactlr;
1919 memset(c, 0, sizeof *c);
1920 memset(s, 0, sizeof *s);
1921 io = p->mem[Abar].bar & ~0xf;
1922 c->mmio = vmap(io, p->mem[Abar].size);
1924 print("ahci: %s: address 0x%luX in use did=%x\n",
1925 Tname(c), io, p->did);
1928 c->lmmio = (ulong*)c->mmio;
1932 s->ifc = &sdiahciifc;
1933 s->idno = 'E' + niactlr;
1937 if(Intel(c) && p->did != 0x2681)
1939 nunit = ahciconf(c);
1940 // ahcihbareset((Ahba*)c->mmio);
1941 if(Intel(c) && iaahcimode(p) == -1)
1944 vunmap(c->mmio, p->mem[Abar].size);
1947 c->ndrive = s->nunit = nunit;
1948 c->mport = c->hba->cap & ((1<<5)-1);
1950 i = (c->hba->cap >> 21) & 1;
1951 print("#S/sd%c: %s: sata-%s with %d ports\n", s->idno,
1952 Tname(c), "I\0II" + i*2, nunit);
1954 /* map the drives -- they don't all need to be enabled. */
1955 memset(c->rawdrive, 0, sizeof c->rawdrive);
1957 for(i = 0; i < NCtlrdrv; i++) {
1958 d = c->rawdrive + i;
1964 if((c->hba->pi & (1<<i)) == 0)
1966 d->port = (Aport*)(c->mmio + 0x80*i + 0x100);
1967 d->portc.p = d->port;
1968 d->portc.m = &d->portm;
1970 c->drive[d->driveno] = d;
1971 iadrive[niadrive + d->driveno] = d;
1973 for(i = 0; i < n; i++)
1974 if(ahciidle(c->drive[i]->port) == -1){
1975 dprint("ahci: %s: port %d wedged; abort\n",
1979 for(i = 0; i < n; i++){
1980 c->drive[i]->mode = DMsatai;
1981 configdrive(c->drive[i]);
1995 static char* smarttab[] = {
1998 "threshold exceeded",
2003 pflag(char *s, char *e, uchar f)
2007 for(i = 0; i < 8; i++)
2009 s = seprint(s, e, "%s ", flagname[i]);
2010 return seprint(s, e, "\n");
2014 iarctl(SDunit *u, char *p, int l)
2024 print("iarctl: nil u->dev->ctlr\n");
2027 d = c->drive[u->subno];
2032 if(d->state == Dready){
2033 p = seprint(p, e, "model\t%s\n", d->model);
2034 p = seprint(p, e, "serial\t%s\n", d->serial);
2035 p = seprint(p, e, "firm\t%s\n", d->firmware);
2036 if(d->smartrs == 0xff)
2037 p = seprint(p, e, "smart\tenable error\n");
2038 else if(d->smartrs == 0)
2039 p = seprint(p, e, "smart\tdisabled\n");
2041 p = seprint(p, e, "smart\t%s\n",
2042 smarttab[d->portm.smart]);
2043 p = seprint(p, e, "flag\t");
2044 p = pflag(p, e, d->portm.feat);
2046 p = seprint(p, e, "no disk present [%s]\n", diskstates[d->state]);
2047 serrstr(o->serror, buf, buf + sizeof buf - 1);
2048 p = seprint(p, e, "reg\ttask %lux cmd %lux serr %lux %s ci %lux is %lux; "
2049 "sig %lux sstatus %04lux\n", o->task, o->cmd, o->serror, buf,
2050 o->ci, o->isr, o->sig, o->sstatus);
2052 panic("iarctl: nil d->unit");
2053 p = seprint(p, e, "geometry %llud %lud\n", d->sectors, d->unit->secsize);
2058 runflushcache(Drive *d)
2062 t0 = MACHP(0)->ticks;
2063 if(flushcache(d) != 0)
2065 dprint("ahci: flush in %ld ms\n", MACHP(0)->ticks - t0);
2069 forcemode(Drive *d, char *mode)
2073 for(i = 0; i < nelem(modename); i++)
2074 if(strcmp(mode, modename[i]) == 0)
2076 if(i == nelem(modename))
2084 runsmartable(Drive *d, int i)
2091 if(lockready(d) == -1)
2093 d->smartrs = smart(&d->portc, i);
2100 forcestate(Drive *d, char *state)
2104 for(i = 0; i < nelem(diskstates); i++)
2105 if(strcmp(state, diskstates[i]) == 0)
2107 if(i == nelem(diskstates))
2116 iawctl(SDunit *u, Cmdbuf *cmd)
2124 d = c->drive[u->subno];
2127 if(strcmp(f[0], "flushcache") == 0)
2129 else if(strcmp(f[0], "identify") == 0){
2130 i = strtoul(f[1]? f[1]: "0", 0, 0);
2133 dprint("ahci: %04d %ux\n", i, d->info[i]);
2134 }else if(strcmp(f[0], "mode") == 0)
2135 forcemode(d, f[1]? f[1]: "satai");
2136 else if(strcmp(f[0], "nop") == 0){
2137 if((d->portm.feat & Dnop) == 0){
2138 cmderror(cmd, "no drive support");
2145 if(lockready(d) == -1)
2150 }else if(strcmp(f[0], "reset") == 0)
2151 forcestate(d, "reset");
2152 else if(strcmp(f[0], "smart") == 0){
2153 if(d->smartrs == 0){
2154 cmderror(cmd, "smart not enabled");
2162 if(lockready(d) == -1)
2164 d->portm.smart = 2 + smartrs(&d->portc);
2167 }else if(strcmp(f[0], "smartdisable") == 0)
2169 else if(strcmp(f[0], "smartenable") == 0)
2171 else if(strcmp(f[0], "state") == 0)
2172 forcestate(d, f[1]? f[1]: "null");
2174 cmderror(cmd, Ebadctl);
2181 portr(char *p, char *e, uint x)
2187 for(i = 0; i < 32; i++){
2188 if((x & (1<<i)) == 0){
2189 if(a != -1 && i - 1 != a)
2190 p = seprint(p, e, "-%d", i - 1);
2196 p = seprint(p, e, ", ");
2197 p = seprint(p, e, "%d", a = i);
2200 if(a != -1 && i - 1 != a)
2201 p = seprint(p, e, "-%d", i - 1);
2205 /* must emit exactly one line per controller (sd(3)) */
2207 iartopctl(SDev *sdev, char *p, char *e)
2214 #define has(x, str) if(cap & (x)) p = seprint(p, e, "%s ", (str))
2218 p = seprint(p, e, "sd%c ahci port %#p: ", sdev->idno, hba);
2235 portr(pr, pr + sizeof pr, hba->pi);
2236 return seprint(p, e,
2237 "iss %ld ncs %ld np %ld; ghc %lux isr %lux pi %lux %s ver %lux\n",
2238 (cap>>20) & 0xf, (cap>>8) & 0x1f, 1 + (cap & 0x1f),
2239 hba->ghc, hba->isr, hba->pi, pr, hba->ver);
2244 iawtopctl(SDev *, Cmdbuf *cmd)
2254 if(strcmp(f[0], "debug") == 0)
2256 else if(strcmp(f[0], "idprint") == 0)
2258 else if(strcmp(f[0], "aprint") == 0)
2261 cmderror(cmd, Ebadctl);
2265 cmderror(cmd, Ebadarg);
2271 *v = strcmp(f[1], "on") == 0;
2279 SDifc sdiahciifc = {