ahci: enable io/mem/dma
[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              (1 << 10) | /* clear busy on ok */
129              (iswrite ? (1 << 6) : 0) |
130              (isatapi ? (1 << 5) : 0) |
131              (4 << 0)); /* fis length (dwords) */
132     SET_FLATPTR(list[0].flags, flags);
133     SET_FLATPTR(list[0].bytes,  bsize);
134     SET_FLATPTR(list[0].base,   ((u32)(cmd)));
135     SET_FLATPTR(list[0].baseu,  0);
136
137     dprintf(2, "AHCI/%d: send cmd ...\n", pnr);
138     SET_FLATPTR(fis->rfis[2], 0);
139     ahci_port_writel(ctrl, pnr, PORT_SCR_ACT, 1);
140     ahci_port_writel(ctrl, pnr, PORT_CMD_ISSUE, 1);
141     while (ahci_port_readl(ctrl, pnr, PORT_CMD_ISSUE)) {
142         yield();
143     }
144     while ((status = GET_FLATPTR(fis->rfis[2])) == 0) {
145         yield();
146     }
147
148     success = (0x00 == (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_DF |
149                                   ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR)) &&
150                ATA_CB_STAT_RDY == (status & (ATA_CB_STAT_RDY)));
151     dprintf(2, "AHCI/%d: ... finished, status 0x%x, %s\n", pnr,
152             status, success ? "OK" : "ERROR");
153     return success ? 0 : -1;
154 }
155
156 #define CDROM_CDB_SIZE 12
157
158 int ahci_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
159 {
160     if (! CONFIG_AHCI)
161         return 0;
162
163     struct ahci_port_s *port = container_of(
164         op->drive_g, struct ahci_port_s, drive);
165     struct ahci_cmd_s *cmd = GET_GLOBAL(port->cmd);
166     u8 *atapi = cdbcmd;
167     int i, rc;
168
169     sata_prep_atapi(&cmd->fis, blocksize);
170     for (i = 0; i < CDROM_CDB_SIZE; i++) {
171         SET_FLATPTR(cmd->atapi[i], atapi[i]);
172     }
173     rc = ahci_command(port, 0, 1, op->buf_fl,
174                       op->count * blocksize);
175     if (rc < 0)
176         return DISK_RET_EBADTRACK;
177     return DISK_RET_SUCCESS;
178 }
179
180 // read/write count blocks from a harddrive.
181 static int
182 ahci_disk_readwrite(struct disk_op_s *op, int iswrite)
183 {
184     struct ahci_port_s *port = container_of(
185         op->drive_g, struct ahci_port_s, drive);
186     struct ahci_cmd_s *cmd = GET_GLOBAL(port->cmd);
187     int rc;
188
189     sata_prep_readwrite(&cmd->fis, op, iswrite);
190     rc = ahci_command(port, iswrite, 0, op->buf_fl,
191                       op->count * DISK_SECTOR_SIZE);
192     dprintf(2, "ahci disk %s, lba %6x, count %3x, buf %p, rc %d\n",
193             iswrite ? "write" : "read", (u32)op->lba, op->count, op->buf_fl, rc);
194     if (rc < 0)
195         return DISK_RET_EBADTRACK;
196     return DISK_RET_SUCCESS;
197 }
198
199 // command demuxer
200 int process_ahci_op(struct disk_op_s *op)
201 {
202     struct ahci_port_s *port;
203     u32 atapi;
204
205     if (!CONFIG_AHCI)
206         return 0;
207
208     port = container_of(op->drive_g, struct ahci_port_s, drive);
209     atapi = GET_GLOBAL(port->atapi);
210
211     if (atapi) {
212         switch (op->command) {
213         case CMD_READ:
214             return cdb_read(op);
215         case CMD_WRITE:
216         case CMD_FORMAT:
217             return DISK_RET_EWRITEPROTECT;
218         case CMD_RESET:
219             /* FIXME: what should we do here? */
220         case CMD_VERIFY:
221         case CMD_SEEK:
222             return DISK_RET_SUCCESS;
223         default:
224             dprintf(1, "AHCI: unknown cdrom command %d\n", op->command);
225             op->count = 0;
226             return DISK_RET_EPARAM;
227         }
228     } else {
229         switch (op->command) {
230         case CMD_READ:
231             return ahci_disk_readwrite(op, 0);
232         case CMD_WRITE:
233             return ahci_disk_readwrite(op, 1);
234         case CMD_RESET:
235             /* FIXME: what should we do here? */
236         case CMD_FORMAT:
237         case CMD_VERIFY:
238         case CMD_SEEK:
239             return DISK_RET_SUCCESS;
240         default:
241             dprintf(1, "AHCI: unknown disk command %d\n", op->command);
242             op->count = 0;
243             return DISK_RET_EPARAM;
244         }
245     }
246 }
247
248 /****************************************************************
249  * everything below is pure 32bit code
250  ****************************************************************/
251
252 static void
253 ahci_port_reset(struct ahci_ctrl_s *ctrl, u32 pnr)
254 {
255     u32 val, count = 0;
256
257     /* disable FIS + CMD */
258     val = ahci_port_readl(ctrl, pnr, PORT_CMD);
259     while (val & (PORT_CMD_FIS_RX | PORT_CMD_START |
260                   PORT_CMD_FIS_ON | PORT_CMD_LIST_ON) &&
261            count < AHCI_MAX_RETRIES) {
262         val &= ~(PORT_CMD_FIS_RX | PORT_CMD_START);
263         ahci_port_writel(ctrl, pnr, PORT_CMD, val);
264         ndelay(500);
265         val = ahci_port_readl(ctrl, pnr, PORT_CMD);
266         count++;
267     }
268
269     /* clear status */
270     val = ahci_port_readl(ctrl, pnr, PORT_SCR_ERR);
271     if (val)
272         ahci_port_writel(ctrl, pnr, PORT_SCR_ERR, val);
273
274     /* disable + clear IRQs */
275     ahci_port_writel(ctrl, pnr, PORT_IRQ_MASK, val);
276     val = ahci_port_readl(ctrl, pnr, PORT_IRQ_STAT);
277     if (val)
278         ahci_port_writel(ctrl, pnr, PORT_IRQ_STAT, val);
279 }
280
281 static int
282 ahci_port_probe(struct ahci_ctrl_s *ctrl, u32 pnr)
283 {
284     u32 val, count = 0;
285
286     val = ahci_port_readl(ctrl, pnr, PORT_TFDATA);
287     while (val & ((1 << 7) /* BSY */ |
288                   (1 << 3) /* DRQ */)) {
289         ndelay(500);
290         val = ahci_port_readl(ctrl, pnr, PORT_TFDATA);
291         count++;
292         if (count >= AHCI_MAX_RETRIES)
293             return -1;
294     }
295
296     val = ahci_port_readl(ctrl, pnr, PORT_SCR_STAT);
297     if ((val & 0x07) != 0x03)
298         return -1;
299     return 0;
300 }
301
302 #define MAXMODEL 40
303
304 static struct ahci_port_s*
305 ahci_port_init(struct ahci_ctrl_s *ctrl, u32 pnr)
306 {
307     struct ahci_port_s *port = malloc_fseg(sizeof(*port));
308     char model[MAXMODEL+1];
309     u16 buffer[256];
310     u32 val;
311     int rc;
312
313     if (!port) {
314         warn_noalloc();
315         return NULL;
316     }
317     port->pnr = pnr;
318     port->ctrl = ctrl;
319     port->list = memalign_low(1024, 1024);
320     port->fis = memalign_low(256, 256);
321     port->cmd = memalign_low(256, 256);
322     if (port->list == NULL || port->fis == NULL || port->cmd == NULL) {
323         warn_noalloc();
324         return NULL;
325     }
326     memset(port->list, 0, 1024);
327     memset(port->fis, 0, 256);
328     memset(port->cmd, 0, 256);
329
330     ahci_port_writel(ctrl, pnr, PORT_LST_ADDR, (u32)port->list);
331     ahci_port_writel(ctrl, pnr, PORT_FIS_ADDR, (u32)port->fis);
332     val = ahci_port_readl(ctrl, pnr, PORT_CMD);
333     ahci_port_writel(ctrl, pnr, PORT_CMD, val | PORT_CMD_FIS_RX);
334
335     sata_prep_simple(&port->cmd->fis, ATA_CMD_IDENTIFY_PACKET_DEVICE);
336     rc = ahci_command(port, 0, 0, buffer, sizeof(buffer));
337     if (rc == 0) {
338         port->atapi = 1;
339     } else {
340         port->atapi = 0;
341         sata_prep_simple(&port->cmd->fis, ATA_CMD_IDENTIFY_DEVICE);
342         rc = ahci_command(port, 0, 0, buffer, sizeof(buffer));
343         if (rc < 0)
344             goto err;
345     }
346
347     port->drive.type = DTYPE_AHCI;
348     port->drive.removable = (buffer[0] & 0x80) ? 1 : 0;
349     port->drive.desc = malloc_tmp(MAXDESCSIZE);
350     if (!port->drive.desc) {
351         warn_noalloc();
352         return NULL;
353     }
354
355     if (!port->atapi) {
356         // found disk (ata)
357         port->drive.blksize = DISK_SECTOR_SIZE;
358         port->drive.pchs.cylinders = buffer[1];
359         port->drive.pchs.heads = buffer[3];
360         port->drive.pchs.spt = buffer[6];
361
362         u64 sectors;
363         if (buffer[83] & (1 << 10)) // word 83 - lba48 support
364             sectors = *(u64*)&buffer[100]; // word 100-103
365         else
366             sectors = *(u32*)&buffer[60]; // word 60 and word 61
367         port->drive.sectors = sectors;
368         u64 adjsize = sectors >> 11;
369         char adjprefix = 'M';
370         if (adjsize >= (1 << 16)) {
371             adjsize >>= 10;
372             adjprefix = 'G';
373         }
374         snprintf(port->drive.desc, MAXDESCSIZE
375                  , "AHCI/%d: %s ATA-%d Hard-Disk (%u %ciBytes)"
376                  , port->pnr
377                  , ata_extract_model(model, MAXMODEL, buffer)
378                  , ata_extract_version(buffer)
379                  , (u32)adjsize, adjprefix);
380
381         // Setup disk geometry translation.
382         setup_translation(&port->drive);
383         // Register with bcv system.
384         add_bcv_internal(&port->drive);
385     } else {
386         // found cdrom (atapi)
387         port->drive.blksize = CDROM_SECTOR_SIZE;
388         port->drive.sectors = (u64)-1;
389         u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
390         snprintf(port->drive.desc, MAXDESCSIZE, "AHCI/%d: %s ATAPI-%d %s"
391                  , port->pnr
392                  , ata_extract_model(model, MAXMODEL, buffer)
393                  , ata_extract_version(buffer)
394                  , (iscd ? "DVD/CD" : "Device"));
395
396         // fill cdidmap
397         if (iscd)
398             map_cd_drive(&port->drive);
399     }
400     dprintf(1, "%s\n", port->drive.desc);
401
402     return port;
403
404 err:
405     dprintf(1, "AHCI/%d: init failure, reset\n", port->pnr);
406     ahci_port_reset(ctrl, pnr);
407     return NULL;
408 }
409
410 // Detect any drives attached to a given controller.
411 static void
412 ahci_detect(void *data)
413 {
414     struct ahci_ctrl_s *ctrl = data;
415     struct ahci_port_s *port;
416     u32 pnr, max;
417     int rc;
418
419     max = ctrl->caps & 0x1f;
420     for (pnr = 0; pnr < max; pnr++) {
421         if (!(ctrl->ports & (1 << pnr)))
422             continue;
423         dprintf(2, "AHCI/%d: probing\n", pnr);
424         ahci_port_reset(ctrl, pnr);
425         rc = ahci_port_probe(ctrl, pnr);
426         dprintf(1, "AHCI/%d: link %s\n", pnr, rc == 0 ? "up" : "down");
427         if (rc != 0)
428             continue;
429         port = ahci_port_init(ctrl, pnr);
430     }
431 }
432
433 // Initialize an ata controller and detect its drives.
434 static void
435 ahci_init_controller(int bdf)
436 {
437     struct ahci_ctrl_s *ctrl = malloc_fseg(sizeof(*ctrl));
438     u32 val;
439
440     if (!ctrl) {
441         warn_noalloc();
442         return;
443     }
444     ctrl->pci_bdf = bdf;
445     ctrl->iobase = pci_config_readl(bdf, PCI_BASE_ADDRESS_5);
446     ctrl->irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
447     dprintf(1, "AHCI controller at %02x.%x, iobase %x, irq %d\n",
448             bdf >> 3, bdf & 7, ctrl->iobase, ctrl->irq);
449
450     pci_config_maskw(bdf, PCI_COMMAND, 0,
451                      PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
452
453     val = ahci_ctrl_readl(ctrl, HOST_CTL);
454     ahci_ctrl_writel(ctrl, HOST_CTL, val | HOST_CTL_AHCI_EN);
455
456     ctrl->caps = ahci_ctrl_readl(ctrl, HOST_CAP);
457     ctrl->ports = ahci_ctrl_readl(ctrl, HOST_PORTS_IMPL);
458     dprintf(2, "AHCI: cap 0x%x, ports_impl 0x%x\n",
459             ctrl->caps, ctrl->ports);
460
461     run_thread(ahci_detect, ctrl);
462 }
463
464 // Locate and init ahci controllers.
465 static void
466 ahci_init(void)
467 {
468     // Scan PCI bus for ATA adapters
469     int bdf, max;
470     foreachpci(bdf, max) {
471         if (pci_config_readw(bdf, PCI_CLASS_DEVICE) != PCI_CLASS_STORAGE_SATA)
472             continue;
473         if (pci_config_readb(bdf, PCI_CLASS_PROG) != 1 /* AHCI rev 1 */)
474             continue;
475         ahci_init_controller(bdf);
476     }
477 }
478
479 void
480 ahci_setup(void)
481 {
482     ASSERT32FLAT();
483     if (!CONFIG_AHCI)
484         return;
485
486     dprintf(3, "init ahci\n");
487     ahci_init();
488 }