1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
// ata.c: ATA simulator for vblade
#include "config.h"
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include "dat.h"
#include "fns.h"
enum {
// err bits
UNC = 1<<6,
MC = 1<<5,
IDNF = 1<<4,
MCR = 1<<3,
ABRT = 1<<2,
NM = 1<<1,
// status bits
BSY = 1<<7,
DRDY = 1<<6,
DF = 1<<5,
DRQ = 1<<3,
ERR = 1<<0,
};
static ushort ident[256];
static void
setfld(ushort *a, int idx, int len, char *str) // set field in ident
{
uchar *p;
p = (uchar *)(a+idx);
while (len > 0) {
if (*str == 0)
p[1] = ' ';
else
p[1] = *str++;
if (*str == 0)
p[0] = ' ';
else
p[0] = *str++;
p += 2;
len -= 2;
}
}
static void
setlba28(ushort *ident, vlong lba)
{
uchar *cp;
cp = (uchar *) &ident[60];
*cp++ = lba;
*cp++ = lba >>= 8;
*cp++ = lba >>= 8;
*cp++ = (lba >>= 8) & 0xf;
}
static void
setlba48(ushort *ident, vlong lba)
{
uchar *cp;
cp = (uchar *) &ident[100];
*cp++ = lba;
*cp++ = lba >>= 8;
*cp++ = lba >>= 8;
*cp++ = lba >>= 8;
*cp++ = lba >>= 8;
*cp++ = lba >>= 8;
}
static void
setushort(ushort *a, int i, ushort n)
{
uchar *p;
p = (uchar *)(a+i);
*p++ = n & 0xff;
*p++ = n >> 8;
}
void
atainit(void)
{
char buf[64];
setushort(ident, 47, 0x8000);
setushort(ident, 49, 0x0200);
setushort(ident, 50, 0x4000);
setushort(ident, 83, 0x5400);
setushort(ident, 84, 0x4000);
setushort(ident, 86, 0x1400);
setushort(ident, 87, 0x4000);
setushort(ident, 93, 0x400b);
setfld(ident, 27, 40, "Coraid EtherDrive vblade");
sprintf(buf, "V%d", VBLADE_VERSION);
setfld(ident, 23, 8, buf);
setfld(ident, 10, 20, serial);
}
/* The ATA spec is weird in that you specify the device size as number
* of sectors and then address the sectors with an offset. That means
* with LBA 28 you shouldn't see an LBA of all ones. Still, we don't
* check for that.
*/
int
atacmd(Ataregs *p, uchar *dp, int ndp, int payload) // do the ata cmd
{
vlong lba;
ushort *ip;
int n;
enum { MAXLBA28SIZE = 0x0fffffff };
extern int maxscnt;
p->status = 0;
switch (p->cmd) {
default:
p->status = DRDY | ERR;
p->err = ABRT;
return 0;
case 0xe7: // flush cache
return 0;
case 0xec: // identify device
if (p->sectors != 1 || ndp < 512)
return -1;
memmove(dp, ident, 512);
ip = (ushort *)dp;
if (size & ~MAXLBA28SIZE)
setlba28(ip, MAXLBA28SIZE);
else
setlba28(ip, size);
setlba48(ip, size);
p->err = 0;
p->status = DRDY;
p->sectors = 0;
return 0;
case 0xe5: // check power mode
p->err = 0;
p->sectors = 0xff; // the device is active or idle
p->status = DRDY;
return 0;
case 0x20: // read sectors
case 0x30: // write sectors
lba = p->lba & MAXLBA28SIZE;
break;
case 0x24: // read sectors ext
case 0x34: // write sectors ext
lba = p->lba & 0x0000ffffffffffffLL; // full 48
break;
}
// we ought not be here unless we are a read/write
if (p->sectors > maxscnt || p->sectors*512 > ndp)
return -1;
if (lba + p->sectors > size) {
p->err = IDNF;
p->status = DRDY | ERR;
p->lba = lba;
return 0;
}
if (p->cmd == 0x20 || p->cmd == 0x24)
n = getsec(bfd, dp, lba+offset, p->sectors);
else {
// packet should be big enough to contain the data
if (payload < 512 * p->sectors)
return -1;
n = putsec(bfd, dp, lba+offset, p->sectors);
}
n /= 512;
if (n != p->sectors) {
p->err = ABRT;
p->status = ERR;
} else
p->err = 0;
p->status |= DRDY;
p->lba += n;
p->sectors -= n;
return 0;
}
|