kern

view src/ata.c @ 98:921a264297a4

merged the filesystem stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 17 Apr 2014 17:03:30 +0300
parents 2f555c81ae67
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <inttypes.h>
6 #include <assert.h>
7 #include "ata.h"
8 #include "intr.h"
9 #include "asmops.h"
10 #include "sched.h"
11 #include "mutex.h"
13 /* registers */
14 #define REG_DATA 0 /* R/W */
15 #define REG_ERROR 1 /* R */
16 #define REG_FEATURES 1 /* W */
17 #define REG_COUNT 2 /* R/W */
18 #define REG_LBA0 3 /* R/W */
19 #define REG_LBA1 4 /* R/W */
20 #define REG_LBA2 5 /* R/W */
21 #define REG_DEVICE 6 /* R/W */
22 #define REG_CMD 7 /* W */
23 #define REG_STATUS 7 /* R */
25 #define REG_CTL 518
26 #define REG_ALTSTAT 518
28 /* status bit fields */
29 #define ST_ERR (1 << 0)
30 #define ST_DRQ (1 << 3)
31 #define ST_DRDY (1 << 6)
32 #define ST_BSY (1 << 7)
34 /* device select bit in control register */
35 #define DEV_SEL(x) (((x) & 1) << 4)
36 #define DEV_LBA (1 << 6)
38 /* ATA commands */
39 #define CMD_IDENTIFY 0xec
40 #define CMD_READ 0x20
41 #define CMD_READ48 0x24
42 #define CMD_WRITE 0x30
43 #define CMD_WRITE48 0x34
46 struct device {
47 int id; /* id of the device on its ATA interface (0 master, 1 slave) */
48 int iface; /* ATA interface for this device (0 or 1) */
49 int port_base; /* interface I/O port base */
51 uint32_t nsect_lba;
52 uint64_t nsect_lba48;
53 };
56 static int readwrite_pio(int devno, uint64_t sect, void *buf, void (*rwdata)(struct device*, void*));
57 static int identify(struct device *dev, int iface, int id);
58 static void select_dev(struct device *dev);
59 static int wait_busy(struct device *dev);
60 static int wait_drq(struct device *dev);
61 static void read_data(struct device *dev, void *buf);
62 static void write_data(struct device *dev, void *buf);
63 static inline uint8_t read_reg8(struct device *dev, int reg);
64 static inline uint16_t read_reg16(struct device *dev, int reg);
65 static inline void write_reg8(struct device *dev, int reg, uint8_t val);
66 static inline void write_reg16(struct device *dev, int reg, uint16_t val);
67 static void ata_intr(int inum);
68 static void *atastr(void *res, void *src, int n);
69 static char *size_str(uint64_t nsect, char *buf);
70 static void print_error(int devid, int wr, uint32_t high, uint32_t low, unsigned char err);
72 /* last drive selected on each bus */
73 static int drvsel[2] = {-1, -1};
75 /* 4 possible devices: 2 ATA interfaces with 2 devices each.
76 * this will never change unless we start querying the PCI config space
77 * for additional drives (in which case this whole init code must be
78 * rewritten anyway), but I like it spelt out like this.
79 */
80 #define MAX_IFACES 2
81 #define MAX_DEV (MAX_IFACES * 2)
82 static struct device devices[MAX_DEV];
83 static int ndev;
85 /* This serves as a sync point for I/O. While the mutex is held,
86 * some process is doing I/O and all the others must wait.
87 */
88 static mutex_t pending;
91 void init_ata(void)
92 {
93 int i;
95 interrupt(IRQ_TO_INTR(15), ata_intr);
97 ndev = 0;
98 for(i=0; i<MAX_DEV; i++) {
99 int iface = i / MAX_IFACES;
100 int id = i % MAX_IFACES;
102 if(identify(devices + ndev, iface, id) == 0) {
103 ndev++;
104 }
105 }
106 }
108 int ata_num_devices(void)
109 {
110 return ndev;
111 }
113 uint64_t ata_num_sectors(int devno)
114 {
115 struct device *dev = devices + devno;
117 if(dev->nsect_lba48) {
118 return dev->nsect_lba48;
119 }
120 return dev->nsect_lba;
121 }
123 int ata_read_pio(int devno, uint64_t sect, void *buf)
124 {
125 return readwrite_pio(devno, sect, buf, read_data);
126 }
128 int ata_write_pio(int devno, uint64_t sect, void *buf)
129 {
130 return readwrite_pio(devno, sect, buf, write_data);
131 }
133 static int readwrite_pio(int devno, uint64_t sect, void *buf, void (*rwdata)(struct device*, void*))
134 {
135 int use_irq, cmd, st, res = -1;
136 uint32_t sect_low, sect_high;
137 struct device *dev = devices + devno;
139 if(dev->id == -1) {
140 return -1;
141 }
142 use_irq = get_current_proc() != 0;
144 if(use_irq) {
145 /* wait for the interface to become available */
146 mutex_lock(&pending);
147 }
149 select_dev(dev);
151 /* LBA48 requires the high-order bits first */
152 if(sect >= dev->nsect_lba) {
153 sect_high = (uint32_t)(sect >> 24);
154 sect_low = (uint32_t)sect & 0xffffff;
156 if(sect >= dev->nsect_lba48) {
157 goto end;
158 }
159 cmd = CMD_READ48;
161 write_reg8(dev, REG_COUNT, 0);
162 write_reg8(dev, REG_LBA0, sect_high & 0xff);
163 write_reg8(dev, REG_LBA1, (sect_high >> 8) & 0xff);
164 write_reg8(dev, REG_LBA2, (sect_high >> 16) & 0xff);
165 } else {
166 cmd = CMD_READ;
167 sect_high = 0;
168 sect_low = (uint32_t)sect & 0xfffffff;
169 }
171 write_reg8(dev, REG_COUNT, 1);
172 write_reg8(dev, REG_LBA0, sect_low & 0xff);
173 write_reg8(dev, REG_LBA1, (sect_low >> 8) & 0xff);
174 write_reg8(dev, REG_LBA2, (sect_low >> 16) & 0xff);
175 write_reg8(dev, REG_DEVICE, ((sect_low >> 24) & 0xf) | DEV_LBA | DEV_SEL(dev->id));
176 /* execute */
177 write_reg8(dev, REG_CMD, cmd);
179 /* wait for the data to become available */
180 do {
181 if(use_irq) {
182 /* also sleep on the mutex if we're called from userspace */
183 wait(&pending);
184 }
185 } while(((st = read_reg8(dev, REG_ALTSTAT)) & (ST_DRQ | ST_ERR)) == 0);
187 if(st & ST_ERR) {
188 //print_error(int devid, int wr, uint32_t high, uint32_t low, unsigned char err);
189 unsigned char err;
191 err = read_reg8(dev, REG_ERROR);
192 print_error(devno, 0, sect_high, sect_low, err);
193 goto end;
194 }
196 /* read/write the data and we're done */
197 rwdata(dev, buf);
198 res = 0;
199 end:
200 if(use_irq) {
201 mutex_unlock(&pending);
202 }
203 return res;
204 }
206 static int identify(struct device *dev, int iface, int id)
207 {
208 /* base address of the two ATA interfaces */
209 static const int port_base[] = {0x1f0, 0x170};
210 unsigned char st;
211 uint16_t *info;
212 char textbuf[42]; /* at most we need 40 chars for ident strings */
214 dev->id = id;
215 dev->iface = iface;
216 dev->port_base = port_base[iface];
218 /* a status of 0xff means there's no drive on the interface */
219 if((st = read_reg8(dev, REG_ALTSTAT)) == 0xff) {
220 return -1;
221 }
223 select_dev(dev);
225 write_reg8(dev, REG_CMD, CMD_IDENTIFY);
227 if(!(st = read_reg8(dev, REG_ALTSTAT)) || (st & ST_ERR)) {
228 /* does not exist */
229 return -1;
230 }
231 if(wait_busy(dev) == -1) {
232 /* got ST_ERR, not ATA */
233 return -1;
234 }
236 info = malloc(512);
237 assert(info);
239 /* read the device information */
240 read_data(dev, info);
242 /* print model and serial */
243 printf("ata%d: %s", (dev->iface << 1) | dev->id, atastr(textbuf, info + 27, 40));
244 printf(" [s/n: %s]", atastr(textbuf, info + 10, 20));
246 dev->nsect_lba = *(uint32_t*)(info + 60);
247 dev->nsect_lba48 = *(uint64_t*)(info + 100) & 0xffffffffffffull;
249 if(!dev->nsect_lba) {
250 printf(" drive does not support LBA, ignoring!\n");
251 free(info);
252 return -1;
253 }
255 if(dev->nsect_lba48) {
256 size_str(dev->nsect_lba48, textbuf);
257 } else {
258 size_str(dev->nsect_lba, textbuf);
259 }
260 printf(" size: %s\n", textbuf);
262 free(info);
263 return 0;
264 }
266 static void select_dev(struct device *dev)
267 {
268 /* if this is the currently selected device, thy work is done */
269 if(drvsel[dev->iface] == dev->id)
270 return;
272 /* wait for BSY and DRQ to clear */
273 while(read_reg8(dev, REG_ALTSTAT) & (ST_BSY | ST_DRQ));
275 /* set the correct device bit to the device register */
276 write_reg8(dev, REG_DEVICE, DEV_SEL(dev->id));
278 /* wait a bit to allow the device time to respond */
279 iodelay(); iodelay(); iodelay(); iodelay();
280 }
282 static int wait_busy(struct device *dev)
283 {
284 unsigned char st;
286 do {
287 st = read_reg8(dev, REG_ALTSTAT);
288 } while((st & ST_BSY) && !(st & ST_ERR));
290 return st & ST_ERR ? -1 : 0;
291 }
293 static int wait_drq(struct device *dev)
294 {
295 unsigned char st;
297 do {
298 st = read_reg8(dev, REG_ALTSTAT);
299 } while(!(st & (ST_DRQ | ST_ERR)));
301 return st & ST_ERR ? -1 : 0;
302 }
304 static void read_data(struct device *dev, void *buf)
305 {
306 int i;
307 uint16_t *ptr = buf;
309 /* wait for the data request from the drive */
310 wait_drq(dev);
312 /* ready to transfer */
313 for(i=0; i<256; i++) {
314 *ptr++ = read_reg16(dev, REG_DATA);
315 }
316 }
318 static void write_data(struct device *dev, void *buf)
319 {
320 int i;
321 uint16_t *ptr = buf;
323 /* wait for the data request from the device */
324 wait_drq(dev);
326 /* ready to transfer */
327 for(i=0; i<256; i++) {
328 write_reg16(dev, REG_DATA, *ptr++);
329 }
330 }
332 static inline uint8_t read_reg8(struct device *dev, int reg)
333 {
334 uint8_t val;
335 inb(val, dev->port_base + reg);
336 return val;
337 }
339 static inline uint16_t read_reg16(struct device *dev, int reg)
340 {
341 uint16_t val;
342 inw(val, dev->port_base + reg);
343 return val;
344 }
346 static inline void write_reg8(struct device *dev, int reg, uint8_t val)
347 {
348 outb(val, dev->port_base + reg);
349 }
351 static inline void write_reg16(struct device *dev, int reg, uint16_t val)
352 {
353 outw(val, dev->port_base + reg);
354 }
356 static void ata_intr(int inum)
357 {
358 printf("ATA interrupt\n");
359 }
361 static void *atastr(void *res, void *src, int n)
362 {
363 int i;
364 uint16_t *sptr = (uint16_t*)src;
365 char *dptr = res;
367 for(i=0; i<n/2; i++) {
368 *dptr++ = (*sptr & 0xff00) >> 8;
369 *dptr++ = *sptr++ & 0xff;
370 }
372 while(isspace(*--dptr));
373 *++dptr = 0;
374 return res;
375 }
377 static char *size_str(uint64_t nsect, char *buf)
378 {
379 static const char *suffix[] = {"kb", "mb", "gb", "tb", "pb", 0};
380 int i;
381 unsigned int rem;
383 /* start with kilobytes */
384 nsect /= 2;
386 for(i=0; nsect >= 1024 && suffix[i + 1]; i++) {
387 rem = nsect % 1024;
388 nsect /= 1024;
389 }
390 sprintf(buf, "%u.%u%s", (unsigned int)nsect, 100 * rem / 1024, suffix[i]);
391 return buf;
392 }
394 #define ERR_NM (1 << 1)
395 #define ERR_ABRT (1 << 2)
396 #define ERR_MCR (1 << 3)
397 #define ERR_IDNF (1 << 4)
398 #define ERR_MC (1 << 5)
399 #define ERR_UNC (1 << 6)
401 static void print_error(int devid, int wr, uint32_t high, uint32_t low, unsigned char err)
402 {
403 printf("ata%d %s %serror ", devid, wr ? "write" : "read", err & ERR_UNC ? "uncorrectable " : "");
404 printf("at sector %x%x: ", high, low);
406 if(err & ERR_ABRT)
407 printf("abort ");
408 if(err & ERR_IDNF)
409 printf("invalid address ");
410 if(err & ERR_NM)
411 printf("no media ");
413 printf("(%x)\n", (unsigned int)err);
414 }