ahci/sata: Fix FIS setup.
[seabios.git] / src / ahci.c
1 // Low level AHCI disk access
2 //
3 // Copyright (C) 2010 Gerd Hoffmann <kraxel@redhat.com>
4 //
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
6
7 #include "types.h" // u8
8 #include "ioport.h" // inb
9 #include "util.h" // dprintf
10 #include "biosvar.h" // GET_EBDA
11 #include "pci.h" // foreachpci
12 #include "pci_ids.h" // PCI_CLASS_STORAGE_OTHER
13 #include "pci_regs.h" // PCI_INTERRUPT_LINE
14 #include "boot.h" // add_bcv_hd
15 #include "disk.h" // struct ata_s
16 #include "ata.h" // ATA_CB_STAT
17 #include "ahci.h" // CDB_CMD_READ_10
18 #include "blockcmd.h" // CDB_CMD_READ_10
19
20 #define AHCI_MAX_RETRIES 5
21
22 /****************************************************************
23  * these bits must run in both 16bit and 32bit modes
24  ****************************************************************/
25
26 // prepare sata command fis
27 static void sata_prep_simple(struct sata_cmd_fis *fis, u8 command)
28 {
29     memset_fl(fis, 0, sizeof(*fis));
30     SET_FLATPTR(fis->command, command);
31 }
32
33 static void sata_prep_readwrite(struct sata_cmd_fis *fis,
34                                 struct disk_op_s *op, int iswrite)
35 {
36     u64 lba = op->lba;
37     u8 command;
38
39     memset_fl(fis, 0, sizeof(*fis));
40
41     if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
42         SET_FLATPTR(fis->sector_count2, op->count >> 8);
43         SET_FLATPTR(fis->lba_low2,      lba >> 24);
44         SET_FLATPTR(fis->lba_mid2,      lba >> 32);
45         SET_FLATPTR(fis->lba_high2,     lba >> 40);
46         lba &= 0xffffff;
47         command = (iswrite ? ATA_CMD_WRITE_DMA_EXT
48                    : ATA_CMD_READ_DMA_EXT);
49     } else {
50         command = (iswrite ? ATA_CMD_WRITE_DMA
51                    : ATA_CMD_READ_DMA);
52     }
53     SET_FLATPTR(fis->feature,      1); /* dma */
54     SET_FLATPTR(fis->command,      command);
55     SET_FLATPTR(fis->sector_count, op->count);
56     SET_FLATPTR(fis->lba_low,      lba);
57     SET_FLATPTR(fis->lba_mid,      lba >> 8);
58     SET_FLATPTR(fis->lba_high,     lba >> 16);
59     SET_FLATPTR(fis->device,       ((lba >> 24) & 0xf) | ATA_CB_DH_LBA);
60 }
61
62 static void sata_prep_atapi(struct sata_cmd_fis *fis, u16 blocksize)
63 {
64     memset_fl(fis, 0, sizeof(*fis));
65     SET_FLATPTR(fis->command,  ATA_CMD_PACKET);
66     SET_FLATPTR(fis->feature,  1); /* dma */
67     SET_FLATPTR(fis->lba_mid,  blocksize);
68     SET_FLATPTR(fis->lba_high, blocksize >> 8);
69 }
70
71 // ahci register access helpers
72 static u32 ahci_ctrl_readl(struct ahci_ctrl_s *ctrl, u32 reg)
73 {
74     u32 addr = GET_GLOBALFLAT(ctrl->iobase) + reg;
75     return pci_readl(addr);
76 }
77
78 static void ahci_ctrl_writel(struct ahci_ctrl_s *ctrl, u32 reg, u32 val)
79 {
80     u32 addr = GET_GLOBALFLAT(ctrl->iobase) + reg;
81     pci_writel(addr, val);
82 }
83
84 static u32 ahci_port_to_ctrl(u32 pnr, u32 port_reg)
85 {
86     u32 ctrl_reg = 0x100;
87     ctrl_reg += pnr * 0x80;
88     ctrl_reg += port_reg;
89     return ctrl_reg;
90 }
91
92 static u32 ahci_port_readl(struct ahci_ctrl_s *ctrl, u32 pnr, u32 reg)
93 {
94     u32 ctrl_reg = ahci_port_to_ctrl(pnr, reg);
95     return ahci_ctrl_readl(ctrl, ctrl_reg);
96 }
97
98 static void ahci_port_writel(struct ahci_ctrl_s *ctrl, u32 pnr, u32 reg, u32 val)
99 {
100     u32 ctrl_reg = ahci_port_to_ctrl(pnr, reg);
101     ahci_ctrl_writel(ctrl, ctrl_reg, val);
102 }
103
104 // submit ahci command + wait for result
105 static int ahci_command(struct ahci_port_s *port, int iswrite, int isatapi,
106                         void *buffer, u32 bsize)
107 {
108     u32 val, status, success, flags;
109     struct ahci_ctrl_s *ctrl = GET_GLOBAL(port->ctrl);
110     struct ahci_cmd_s  *cmd  = GET_GLOBAL(port->cmd);
111     struct ahci_fis_s  *fis  = GET_GLOBAL(port->fis);
112     struct ahci_list_s *list = GET_GLOBAL(port->list);
113     u32 pnr                  = GET_GLOBAL(port->pnr);
114
115     SET_FLATPTR(cmd->fis.reg,       0x27);
116     SET_FLATPTR(cmd->fis.pmp_type,  (1 << 7)); /* cmd fis */
117     SET_FLATPTR(cmd->prdt[0].base,  ((u32)buffer));
118     SET_FLATPTR(cmd->prdt[0].baseu, 0);
119     SET_FLATPTR(cmd->prdt[0].flags, bsize-1);
120
121     val = ahci_port_readl(ctrl, pnr, PORT_CMD);
122     ahci_port_writel(ctrl, pnr, PORT_CMD, val | PORT_CMD_START);
123
124     if (ahci_port_readl(ctrl, pnr, PORT_CMD_ISSUE))
125         return -1;
126
127     flags = ((1 << 16) | /* one prd entry */
128              (iswrite ? (1 << 6) : 0) |
129              (isatapi ? (1 << 5) : 0) |
130              (5 << 0)); /* fis length (dwords) */
131     SET_FLATPTR(list[0].flags,  flags);
132     SET_FLATPTR(list[0].bytes,  0);
133     SET_FLATPTR(list[0].base,   ((u32)(cmd)));
134     SET_FLATPTR(list[0].baseu,  0);
135
136     dprintf(2, "AHCI/%d: send cmd ...\n", pnr);
137     SET_FLATPTR(fis->rfis[2], 0);
138     ahci_port_writel(ctrl, pnr, PORT_SCR_ACT, 1);
139     ahci_port_writel(ctrl, pnr, PORT_CMD_ISSUE, 1);
140     while (ahci_port_readl(ctrl, pnr, PORT_CMD_ISSUE)) {
141         yield();
142     }
143     while ((status = GET_FLATPTR(fis->rfis[2])) == 0) {
144         yield();
145     }
146
147     success = (0x00 == (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_DF |
148                                   ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR)) &&
149                ATA_CB_STAT_RDY == (status & (ATA_CB_STAT_RDY)));
150     dprintf(2, "AHCI/%d: ... finished, status 0x%x, %s\n", pnr,
151             status, success ? "OK" : "ERROR");
152     return success ? 0 : -1;
153 }
154
155 #define CDROM_CDB_SIZE 12
156
157 int ahci_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
158 {
159     if (! CONFIG_AHCI)
160         return 0;
161
162     struct ahci_port_s *port = container_of(
163         op->drive_g, struct ahci_port_s, drive);
164     struct ahci_cmd_s *cmd = GET_GLOBAL(port->cmd);
165     u8 *atapi = cdbcmd;
166     int i, rc;
167
168     sata_prep_atapi(&cmd->fis, blocksize);
169     for (i = 0; i < CDROM_CDB_SIZE; i++) {
170         SET_FLATPTR(cmd->atapi[i], atapi[i]);
171     }
172     rc = ahci_command(port, 0, 1, op->buf_fl,
173                       op->count * blocksize);
174     if (rc < 0)
175         return DISK_RET_EBADTRACK;
176     return DISK_RET_SUCCESS;
177 }
178
179 // read/write count blocks from a harddrive.
180 static int
181 ahci_disk_readwrite(struct disk_op_s *op, int iswrite)
182 {
183     struct ahci_port_s *port = container_of(
184         op->drive_g, struct ahci_port_s, drive);
185     struct ahci_cmd_s *cmd = GET_GLOBAL(port->cmd);
186     int rc;
187
188     sata_prep_readwrite(&cmd->fis, op, iswrite);
189     rc = ahci_command(port, iswrite, 0, op->buf_fl,
190                       op->count * DISK_SECTOR_SIZE);
191     dprintf(2, "ahci disk %s, lba %6x, count %3x, buf %p, rc %d\n",
192             iswrite ? "write" : "read", (u32)op->lba, op->count, op->buf_fl, rc);
193     if (rc < 0)
194         return DISK_RET_EBADTRACK;
195     return DISK_RET_SUCCESS;
196 }
197
198 // command demuxer
199 int process_ahci_op(struct disk_op_s *op)
200 {
201     struct ahci_port_s *port;
202     u32 atapi;
203
204     if (!CONFIG_AHCI)
205         return 0;
206
207     port = container_of(op->drive_g, struct ahci_port_s, drive);
208     atapi = GET_GLOBAL(port->atapi);
209
210     if (atapi) {
211         switch (op->command) {
212         case CMD_READ:
213             return cdb_read(op);
214         case CMD_WRITE:
215         case CMD_FORMAT:
216             return DISK_RET_EWRITEPROTECT;
217         case CMD_RESET:
218             /* FIXME: what should we do here? */
219         case CMD_VERIFY:
220         case CMD_SEEK:
221             return DISK_RET_SUCCESS;
222         default:
223             dprintf(1, "AHCI: unknown cdrom command %d\n", op->command);
224             op->count = 0;
225             return DISK_RET_EPARAM;
226         }
227     } else {
228         switch (op->command) {
229         case CMD_READ:
230             return ahci_disk_readwrite(op, 0);
231         case CMD_WRITE:
232             return ahci_disk_readwrite(op, 1);
233         case CMD_RESET:
234             /* FIXME: what should we do here? */
235         case CMD_FORMAT:
236         case CMD_VERIFY:
237         case CMD_SEEK:
238             return DISK_RET_SUCCESS;
239         default:
240             dprintf(1, "AHCI: unknown disk command %d\n", op->command);
241             op->count = 0;
242             return DISK_RET_EPARAM;
243         }
244     }
245 }
246
247 /****************************************************************
248  * everything below is pure 32bit code
249  ****************************************************************/
250
251 static void
252 ahci_port_reset(struct ahci_ctrl_s *ctrl, u32 pnr)
253 {
254     u32 val, count = 0;
255
256     /* disable FIS + CMD */
257     val = ahci_port_readl(ctrl, pnr, PORT_CMD);
258     while (val & (PORT_CMD_FIS_RX | PORT_CMD_START |
259                   PORT_CMD_FIS_ON | PORT_CMD_LIST_ON) &&
260            count < AHCI_MAX_RETRIES) {
261         val &= ~(PORT_CMD_FIS_RX | PORT_CMD_START);
262         ahci_port_writel(ctrl, pnr, PORT_CMD, val);
263         ndelay(500);
264         val = ahci_port_readl(ctrl, pnr, PORT_CMD);
265         count++;
266     }
267
268     /* clear status */
269     val = ahci_port_readl(ctrl, pnr, PORT_SCR_ERR);
270     if (val)
271         ahci_port_writel(ctrl, pnr, PORT_SCR_ERR, val);
272
273     /* disable + clear IRQs */
274     ahci_port_writel(ctrl, pnr, PORT_IRQ_MASK, val);
275     val = ahci_port_readl(ctrl, pnr, PORT_IRQ_STAT);
276     if (val)
277         ahci_port_writel(ctrl, pnr, PORT_IRQ_STAT, val);
278 }
279
280 static int
281 ahci_port_probe(struct ahci_ctrl_s *ctrl, u32 pnr)
282 {
283     u32 val, count = 0;
284
285     val = ahci_port_readl(ctrl, pnr, PORT_TFDATA);
286     while (val & ((1 << 7) /* BSY */ |
287                   (1 << 3) /* DRQ */)) {
288         ndelay(500);
289         val = ahci_port_readl(ctrl, pnr, PORT_TFDATA);
290         count++;
291         if (count >= AHCI_MAX_RETRIES)
292             return -1;
293     }
294
295     val = ahci_port_readl(ctrl, pnr, PORT_SCR_STAT);
296     if ((val & 0x07) != 0x03)
297         return -1;
298     return 0;
299 }
300
301 #define MAXMODEL 40
302
303 static struct ahci_port_s*
304 ahci_port_init(struct ahci_ctrl_s *ctrl, u32 pnr)
305 {
306     struct ahci_port_s *port = malloc_fseg(sizeof(*port));
307     char model[MAXMODEL+1];
308     u16 buffer[256];
309     u32 val;
310     int rc;
311
312     if (!port) {
313         warn_noalloc();
314         return NULL;
315     }
316     port->pnr = pnr;
317     port->ctrl = ctrl;
318     port->list = memalign_low(1024, 1024);
319     port->fis = memalign_low(256, 256);
320     port->cmd = memalign_low(256, 256);
321     if (port->list == NULL || port->fis == NULL || port->cmd == NULL) {
322         warn_noalloc();
323         return NULL;
324     }
325     memset(port->list, 0, 1024);
326     memset(port->fis, 0, 256);
327     memset(port->cmd, 0, 256);
328
329     ahci_port_writel(ctrl, pnr, PORT_LST_ADDR, (u32)port->list);
330     ahci_port_writel(ctrl, pnr, PORT_FIS_ADDR, (u32)port->fis);
331     val = ahci_port_readl(ctrl, pnr, PORT_CMD);
332     ahci_port_writel(ctrl, pnr, PORT_CMD, val | PORT_CMD_FIS_RX);
333
334     sata_prep_simple(&port->cmd->fis, ATA_CMD_IDENTIFY_PACKET_DEVICE);
335     rc = ahci_command(port, 0, 0, buffer, sizeof(buffer));
336     if (rc == 0) {
337         port->atapi = 1;
338     } else {
339         port->atapi = 0;
340         sata_prep_simple(&port->cmd->fis, ATA_CMD_IDENTIFY_DEVICE);
341         rc = ahci_command(port, 0, 0, buffer, sizeof(buffer));
342         if (rc < 0)
343             goto err;
344     }
345
346     port->drive.type = DTYPE_AHCI;
347     port->drive.cntl_id = pnr;
348     port->drive.removable = (buffer[0] & 0x80) ? 1 : 0;
349
350     if (!port->atapi) {
351         // found disk (ata)
352         port->drive.blksize = DISK_SECTOR_SIZE;
353         port->drive.pchs.cylinders = buffer[1];
354         port->drive.pchs.heads = buffer[3];
355         port->drive.pchs.spt = buffer[6];
356
357         u64 sectors;
358         if (buffer[83] & (1 << 10)) // word 83 - lba48 support
359             sectors = *(u64*)&buffer[100]; // word 100-103
360         else
361             sectors = *(u32*)&buffer[60]; // word 60 and word 61
362         port->drive.sectors = sectors;
363         u64 adjsize = sectors >> 11;
364         char adjprefix = 'M';
365         if (adjsize >= (1 << 16)) {
366             adjsize >>= 10;
367             adjprefix = 'G';
368         }
369         char *desc = znprintf(MAXDESCSIZE
370                               , "AHCI/%d: %s ATA-%d Hard-Disk (%u %ciBytes)"
371                               , port->pnr
372                               , ata_extract_model(model, MAXMODEL, buffer)
373                               , ata_extract_version(buffer)
374                               , (u32)adjsize, adjprefix);
375         dprintf(1, "%s\n", desc);
376
377         // Register with bcv system.
378         boot_add_hd(&port->drive, desc, -1);
379     } else {
380         // found cdrom (atapi)
381         port->drive.blksize = CDROM_SECTOR_SIZE;
382         port->drive.sectors = (u64)-1;
383         u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
384         char *desc = znprintf(MAXDESCSIZE
385                               , "DVD/CD [AHCI/%d: %s ATAPI-%d %s]"
386                               , port->pnr
387                               , ata_extract_model(model, MAXMODEL, buffer)
388                               , ata_extract_version(buffer)
389                               , (iscd ? "DVD/CD" : "Device"));
390         dprintf(1, "%s\n", desc);
391
392         // fill cdidmap
393         if (iscd)
394             boot_add_cd(&port->drive, desc, -1);
395     }
396
397     return port;
398
399 err:
400     dprintf(1, "AHCI/%d: init failure, reset\n", port->pnr);
401     ahci_port_reset(ctrl, pnr);
402     return NULL;
403 }
404
405 // Detect any drives attached to a given controller.
406 static void
407 ahci_detect(void *data)
408 {
409     struct ahci_ctrl_s *ctrl = data;
410     struct ahci_port_s *port;
411     u32 pnr, max;
412     int rc;
413
414     max = ctrl->caps & 0x1f;
415     for (pnr = 0; pnr <= max; pnr++) {
416         if (!(ctrl->ports & (1 << pnr)))
417             continue;
418         dprintf(2, "AHCI/%d: probing\n", pnr);
419         ahci_port_reset(ctrl, pnr);
420         rc = ahci_port_probe(ctrl, pnr);
421         dprintf(1, "AHCI/%d: link %s\n", pnr, rc == 0 ? "up" : "down");
422         if (rc != 0)
423             continue;
424         port = ahci_port_init(ctrl, pnr);
425     }
426 }
427
428 // Initialize an ata controller and detect its drives.
429 static void
430 ahci_init_controller(int bdf)
431 {
432     struct ahci_ctrl_s *ctrl = malloc_fseg(sizeof(*ctrl));
433     u32 val;
434
435     if (!ctrl) {
436         warn_noalloc();
437         return;
438     }
439     ctrl->pci_bdf = bdf;
440     ctrl->iobase = pci_config_readl(bdf, PCI_BASE_ADDRESS_5);
441     ctrl->irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
442     dprintf(1, "AHCI controller at %02x.%x, iobase %x, irq %d\n",
443             bdf >> 3, bdf & 7, ctrl->iobase, ctrl->irq);
444
445     pci_config_maskw(bdf, PCI_COMMAND, 0,
446                      PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
447
448     val = ahci_ctrl_readl(ctrl, HOST_CTL);
449     ahci_ctrl_writel(ctrl, HOST_CTL, val | HOST_CTL_AHCI_EN);
450
451     ctrl->caps = ahci_ctrl_readl(ctrl, HOST_CAP);
452     ctrl->ports = ahci_ctrl_readl(ctrl, HOST_PORTS_IMPL);
453     dprintf(2, "AHCI: cap 0x%x, ports_impl 0x%x\n",
454             ctrl->caps, ctrl->ports);
455
456     run_thread(ahci_detect, ctrl);
457 }
458
459 // Locate and init ahci controllers.
460 static void
461 ahci_init(void)
462 {
463     // Scan PCI bus for ATA adapters
464     struct pci_device *pci;
465     foreachpci(pci) {
466         if (pci->class != PCI_CLASS_STORAGE_SATA)
467             continue;
468         if (pci->prog_if != 1 /* AHCI rev 1 */)
469             continue;
470         ahci_init_controller(pci->bdf);
471     }
472 }
473
474 void
475 ahci_setup(void)
476 {
477     ASSERT32FLAT();
478     if (!CONFIG_AHCI)
479         return;
480
481     dprintf(3, "init ahci\n");
482     ahci_init();
483 }