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