6c3127ae4c6421add41d784a0a9e4d07468f97b7
[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->command,      command);
54     SET_FLATPTR(fis->sector_count, op->count);
55     SET_FLATPTR(fis->lba_low,      lba);
56     SET_FLATPTR(fis->lba_mid,      lba >> 8);
57     SET_FLATPTR(fis->lba_high,     lba >> 16);
58     SET_FLATPTR(fis->device,       ((lba >> 24) & 0xf) | ATA_CB_DH_LBA);
59 }
60
61 static void sata_prep_atapi(struct sata_cmd_fis *fis, u16 blocksize)
62 {
63     memset_fl(fis, 0, sizeof(*fis));
64     SET_FLATPTR(fis->command,  ATA_CMD_PACKET);
65     SET_FLATPTR(fis->lba_mid,  blocksize);
66     SET_FLATPTR(fis->lba_high, blocksize >> 8);
67 }
68
69 // ahci register access helpers
70 static u32 ahci_ctrl_readl(struct ahci_ctrl_s *ctrl, u32 reg)
71 {
72     u32 addr = GET_GLOBALFLAT(ctrl->iobase) + reg;
73     return pci_readl(addr);
74 }
75
76 static void ahci_ctrl_writel(struct ahci_ctrl_s *ctrl, u32 reg, u32 val)
77 {
78     u32 addr = GET_GLOBALFLAT(ctrl->iobase) + reg;
79     pci_writel(addr, val);
80 }
81
82 static u32 ahci_port_to_ctrl(u32 pnr, u32 port_reg)
83 {
84     u32 ctrl_reg = 0x100;
85     ctrl_reg += pnr * 0x80;
86     ctrl_reg += port_reg;
87     return ctrl_reg;
88 }
89
90 static u32 ahci_port_readl(struct ahci_ctrl_s *ctrl, u32 pnr, u32 reg)
91 {
92     u32 ctrl_reg = ahci_port_to_ctrl(pnr, reg);
93     return ahci_ctrl_readl(ctrl, ctrl_reg);
94 }
95
96 static void ahci_port_writel(struct ahci_ctrl_s *ctrl, u32 pnr, u32 reg, u32 val)
97 {
98     u32 ctrl_reg = ahci_port_to_ctrl(pnr, reg);
99     ahci_ctrl_writel(ctrl, ctrl_reg, val);
100 }
101
102 // submit ahci command + wait for result
103 static int ahci_command(struct ahci_port_s *port, int iswrite, int isatapi,
104                         void *buffer, u32 bsize)
105 {
106     u32 val, status, success, flags;
107     struct ahci_ctrl_s *ctrl = GET_GLOBAL(port->ctrl);
108     struct ahci_cmd_s  *cmd  = GET_GLOBAL(port->cmd);
109     struct ahci_fis_s  *fis  = GET_GLOBAL(port->fis);
110     struct ahci_list_s *list = GET_GLOBAL(port->list);
111     u32 pnr                  = GET_GLOBAL(port->pnr);
112
113     SET_FLATPTR(cmd->fis.reg,       0x27);
114     SET_FLATPTR(cmd->fis.pmp_type,  (1 << 7)); /* cmd fis */
115     SET_FLATPTR(cmd->prdt[0].base,  ((u32)buffer));
116     SET_FLATPTR(cmd->prdt[0].baseu, 0);
117     SET_FLATPTR(cmd->prdt[0].flags, bsize-1);
118
119     val = ahci_port_readl(ctrl, pnr, PORT_CMD);
120     ahci_port_writel(ctrl, pnr, PORT_CMD, val | PORT_CMD_START);
121
122     if (ahci_port_readl(ctrl, pnr, PORT_CMD_ISSUE))
123         return -1;
124
125     flags = ((1 << 16) | /* one prd entry */
126              (1 << 10) | /* clear busy on ok */
127              (iswrite ? (1 << 6) : 0) |
128              (isatapi ? (1 << 5) : 0) |
129              (4 << 0)); /* fis length (dwords) */
130     SET_FLATPTR(list[0].flags, flags);
131     SET_FLATPTR(list[0].bytes,  bsize);
132     SET_FLATPTR(list[0].base,   ((u32)(cmd)));
133     SET_FLATPTR(list[0].baseu,  0);
134
135     dprintf(2, "AHCI/%d: send cmd ...\n", pnr);
136     SET_FLATPTR(fis->rfis[2], 0);
137     ahci_port_writel(ctrl, pnr, PORT_SCR_ACT, 1);
138     ahci_port_writel(ctrl, pnr, PORT_CMD_ISSUE, 1);
139     while (ahci_port_readl(ctrl, pnr, PORT_CMD_ISSUE)) {
140         yield();
141     }
142     while ((status = GET_FLATPTR(fis->rfis[2])) == 0) {
143         yield();
144     }
145
146     success = (0x00 == (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_DF |
147                                   ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR)) &&
148                ATA_CB_STAT_RDY == (status & (ATA_CB_STAT_RDY)));
149     dprintf(2, "AHCI/%d: ... finished, status 0x%x, %s\n", pnr,
150             status, success ? "OK" : "ERROR");
151     return success ? 0 : -1;
152 }
153
154 #define CDROM_CDB_SIZE 12
155
156 int ahci_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
157 {
158     if (! CONFIG_AHCI)
159         return 0;
160
161     struct ahci_port_s *port = container_of(
162         op->drive_g, struct ahci_port_s, drive);
163     struct ahci_cmd_s *cmd = GET_GLOBAL(port->cmd);
164     u8 *atapi = cdbcmd;
165     int i, rc;
166
167     sata_prep_atapi(&cmd->fis, blocksize);
168     for (i = 0; i < CDROM_CDB_SIZE; i++) {
169         SET_FLATPTR(cmd->atapi[i], atapi[i]);
170     }
171     rc = ahci_command(port, 0, 1, op->buf_fl,
172                       op->count * blocksize);
173     if (rc < 0)
174         return DISK_RET_EBADTRACK;
175     return DISK_RET_SUCCESS;
176 }
177
178 // read/write count blocks from a harddrive.
179 static int
180 ahci_disk_readwrite(struct disk_op_s *op, int iswrite)
181 {
182     struct ahci_port_s *port = container_of(
183         op->drive_g, struct ahci_port_s, drive);
184     struct ahci_cmd_s *cmd = GET_GLOBAL(port->cmd);
185     int rc;
186
187     sata_prep_readwrite(&cmd->fis, op, iswrite);
188     rc = ahci_command(port, iswrite, 0, op->buf_fl,
189                       op->count * DISK_SECTOR_SIZE);
190     dprintf(2, "ahci disk %s, lba %6x, count %3x, buf %p, rc %d\n",
191             iswrite ? "write" : "read", (u32)op->lba, op->count, op->buf_fl, rc);
192     if (rc < 0)
193         return DISK_RET_EBADTRACK;
194     return DISK_RET_SUCCESS;
195 }
196
197 // command demuxer
198 int process_ahci_op(struct disk_op_s *op)
199 {
200     struct ahci_port_s *port;
201     u32 atapi;
202
203     if (!CONFIG_AHCI)
204         return 0;
205
206     port = container_of(op->drive_g, struct ahci_port_s, drive);
207     atapi = GET_GLOBAL(port->atapi);
208
209     if (atapi) {
210         switch (op->command) {
211         case CMD_READ:
212             return cdb_read(op);
213         case CMD_WRITE:
214         case CMD_FORMAT:
215             return DISK_RET_EWRITEPROTECT;
216         case CMD_RESET:
217             /* FIXME: what should we do here? */
218         case CMD_VERIFY:
219         case CMD_SEEK:
220             return DISK_RET_SUCCESS;
221         default:
222             dprintf(1, "AHCI: unknown cdrom command %d\n", op->command);
223             op->count = 0;
224             return DISK_RET_EPARAM;
225         }
226     } else {
227         switch (op->command) {
228         case CMD_READ:
229             return ahci_disk_readwrite(op, 0);
230         case CMD_WRITE:
231             return ahci_disk_readwrite(op, 1);
232         case CMD_RESET:
233             /* FIXME: what should we do here? */
234         case CMD_FORMAT:
235         case CMD_VERIFY:
236         case CMD_SEEK:
237             return DISK_RET_SUCCESS;
238         default:
239             dprintf(1, "AHCI: unknown disk command %d\n", op->command);
240             op->count = 0;
241             return DISK_RET_EPARAM;
242         }
243     }
244 }
245
246 /****************************************************************
247  * everything below is pure 32bit code
248  ****************************************************************/
249
250 static void
251 ahci_port_reset(struct ahci_ctrl_s *ctrl, u32 pnr)
252 {
253     u32 val, count = 0;
254
255     /* disable FIS + CMD */
256     val = ahci_port_readl(ctrl, pnr, PORT_CMD);
257     while (val & (PORT_CMD_FIS_RX | PORT_CMD_START |
258                   PORT_CMD_FIS_ON | PORT_CMD_LIST_ON) &&
259            count < AHCI_MAX_RETRIES) {
260         val &= ~(PORT_CMD_FIS_RX | PORT_CMD_START);
261         ahci_port_writel(ctrl, pnr, PORT_CMD, val);
262         ndelay(500);
263         val = ahci_port_readl(ctrl, pnr, PORT_CMD);
264         count++;
265     }
266
267     /* clear status */
268     val = ahci_port_readl(ctrl, pnr, PORT_SCR_ERR);
269     if (val)
270         ahci_port_writel(ctrl, pnr, PORT_SCR_ERR, val);
271
272     /* disable + clear IRQs */
273     ahci_port_writel(ctrl, pnr, PORT_IRQ_MASK, val);
274     val = ahci_port_readl(ctrl, pnr, PORT_IRQ_STAT);
275     if (val)
276         ahci_port_writel(ctrl, pnr, PORT_IRQ_STAT, val);
277 }
278
279 static int
280 ahci_port_probe(struct ahci_ctrl_s *ctrl, u32 pnr)
281 {
282     u32 val, count = 0;
283
284     val = ahci_port_readl(ctrl, pnr, PORT_TFDATA);
285     while (val & ((1 << 7) /* BSY */ |
286                   (1 << 3) /* DRQ */)) {
287         ndelay(500);
288         val = ahci_port_readl(ctrl, pnr, PORT_TFDATA);
289         count++;
290         if (count >= AHCI_MAX_RETRIES)
291             return -1;
292     }
293
294     val = ahci_port_readl(ctrl, pnr, PORT_SCR_STAT);
295     if ((val & 0x07) != 0x03)
296         return -1;
297     return 0;
298 }
299
300 #define MAXMODEL 40
301
302 static struct ahci_port_s*
303 ahci_port_init(struct ahci_ctrl_s *ctrl, u32 pnr)
304 {
305     struct ahci_port_s *port = malloc_fseg(sizeof(*port));
306     char model[MAXMODEL+1];
307     u16 buffer[256];
308     u32 val;
309     int rc;
310
311     if (!port) {
312         warn_noalloc();
313         return NULL;
314     }
315     port->pnr = pnr;
316     port->ctrl = ctrl;
317     port->list = memalign_low(1024, 1024);
318     port->fis = memalign_low(256, 256);
319     port->cmd = memalign_low(256, 256);
320     if (port->list == NULL || port->fis == NULL || port->cmd == NULL) {
321         warn_noalloc();
322         return NULL;
323     }
324     memset(port->list, 0, 1024);
325     memset(port->fis, 0, 256);
326     memset(port->cmd, 0, 256);
327
328     ahci_port_writel(ctrl, pnr, PORT_LST_ADDR, (u32)port->list);
329     ahci_port_writel(ctrl, pnr, PORT_FIS_ADDR, (u32)port->fis);
330     val = ahci_port_readl(ctrl, pnr, PORT_CMD);
331     ahci_port_writel(ctrl, pnr, PORT_CMD, val | PORT_CMD_FIS_RX);
332
333     sata_prep_simple(&port->cmd->fis, ATA_CMD_IDENTIFY_PACKET_DEVICE);
334     rc = ahci_command(port, 0, 0, buffer, sizeof(buffer));
335     if (rc == 0) {
336         port->atapi = 1;
337     } else {
338         port->atapi = 0;
339         sata_prep_simple(&port->cmd->fis, ATA_CMD_IDENTIFY_DEVICE);
340         rc = ahci_command(port, 0, 0, buffer, sizeof(buffer));
341         if (rc < 0)
342             goto err;
343     }
344
345     port->drive.type = DTYPE_AHCI;
346     port->drive.removable = (buffer[0] & 0x80) ? 1 : 0;
347     port->drive.desc = malloc_tmp(MAXDESCSIZE);
348     if (!port->drive.desc) {
349         warn_noalloc();
350         return NULL;
351     }
352
353     if (!port->atapi) {
354         // found disk (ata)
355         port->drive.blksize = DISK_SECTOR_SIZE;
356         port->drive.pchs.cylinders = buffer[1];
357         port->drive.pchs.heads = buffer[3];
358         port->drive.pchs.spt = buffer[6];
359
360         u64 sectors;
361         if (buffer[83] & (1 << 10)) // word 83 - lba48 support
362             sectors = *(u64*)&buffer[100]; // word 100-103
363         else
364             sectors = *(u32*)&buffer[60]; // word 60 and word 61
365         port->drive.sectors = sectors;
366         u64 adjsize = sectors >> 11;
367         char adjprefix = 'M';
368         if (adjsize >= (1 << 16)) {
369             adjsize >>= 10;
370             adjprefix = 'G';
371         }
372         snprintf(port->drive.desc, MAXDESCSIZE
373                  , "AHCI/%d: %s ATA-%d Hard-Disk (%u %ciBytes)"
374                  , port->pnr
375                  , ata_extract_model(model, MAXMODEL, buffer)
376                  , ata_extract_version(buffer)
377                  , (u32)adjsize, adjprefix);
378
379         // Setup disk geometry translation.
380         setup_translation(&port->drive);
381         // Register with bcv system.
382         add_bcv_internal(&port->drive);
383     } else {
384         // found cdrom (atapi)
385         port->drive.blksize = CDROM_SECTOR_SIZE;
386         port->drive.sectors = (u64)-1;
387         u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
388         snprintf(port->drive.desc, MAXDESCSIZE, "AHCI/%d: %s ATAPI-%d %s"
389                  , port->pnr
390                  , ata_extract_model(model, MAXMODEL, buffer)
391                  , ata_extract_version(buffer)
392                  , (iscd ? "DVD/CD" : "Device"));
393
394         // fill cdidmap
395         if (iscd)
396             map_cd_drive(&port->drive);
397     }
398     dprintf(1, "%s\n", port->drive.desc);
399
400     return port;
401
402 err:
403     dprintf(1, "AHCI/%d: init failure, reset\n", port->pnr);
404     ahci_port_reset(ctrl, pnr);
405     return NULL;
406 }
407
408 // Detect any drives attached to a given controller.
409 static void
410 ahci_detect(void *data)
411 {
412     struct ahci_ctrl_s *ctrl = data;
413     struct ahci_port_s *port;
414     u32 pnr, max;
415     int rc;
416
417     max = ctrl->caps & 0x1f;
418     for (pnr = 0; pnr < max; pnr++) {
419         if (!(ctrl->ports & (1 << pnr)))
420             continue;
421         dprintf(2, "AHCI/%d: probing\n", pnr);
422         ahci_port_reset(ctrl, pnr);
423         rc = ahci_port_probe(ctrl, pnr);
424         dprintf(1, "AHCI/%d: link %s\n", pnr, rc == 0 ? "up" : "down");
425         if (rc != 0)
426             continue;
427         port = ahci_port_init(ctrl, pnr);
428     }
429 }
430
431 // Initialize an ata controller and detect its drives.
432 static void
433 ahci_init_controller(int bdf)
434 {
435     struct ahci_ctrl_s *ctrl = malloc_fseg(sizeof(*ctrl));
436     u32 val;
437
438     if (!ctrl) {
439         warn_noalloc();
440         return;
441     }
442     ctrl->pci_bdf = bdf;
443     ctrl->iobase = pci_config_readl(bdf, PCI_BASE_ADDRESS_5);
444     ctrl->irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
445     dprintf(1, "AHCI controller at %02x.%x, iobase %x, irq %d\n",
446             bdf >> 3, bdf & 7, ctrl->iobase, ctrl->irq);
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     int bdf, max;
465     foreachpci(bdf, max) {
466         if (pci_config_readw(bdf, PCI_CLASS_DEVICE) != PCI_CLASS_STORAGE_SATA)
467             continue;
468         if (pci_config_readb(bdf, PCI_CLASS_PROG) != 1 /* AHCI rev 1 */)
469             continue;
470         ahci_init_controller(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 }