blob: 150623f66ab221921dab717b29a1c3bca71184fa [file] [log] [blame]
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +01001// 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
Kevin O'Connor2d2fa312013-09-14 21:55:26 -04007#include "ahci.h" // CDB_CMD_READ_10
8#include "ata.h" // ATA_CB_STAT
Kevin O'Connor4bc49972012-05-13 22:58:08 -04009#include "biosvar.h" // GET_GLOBAL
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040010#include "blockcmd.h" // CDB_CMD_READ_10
11#include "boot.h" // add_bcv_hd
12#include "disk.h" // struct drive_s
13#include "ioport.h" // inb
14#include "malloc.h" // free
15#include "output.h" // dprintf
Kevin O'Connor9cb49922011-06-20 22:22:42 -040016#include "pci.h" // foreachpci
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010017#include "pci_ids.h" // PCI_CLASS_STORAGE_OTHER
18#include "pci_regs.h" // PCI_INTERRUPT_LINE
Kevin O'Connor3df600b2013-09-14 19:28:55 -040019#include "stacks.h" // yield
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040020#include "string.h" // memset
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040021#include "util.h" // timer_calc
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010022
Gerd Hoffmanne1041192011-07-14 16:24:04 +020023#define AHCI_REQUEST_TIMEOUT 32000 // 32 seconds max for IDE ops
24#define AHCI_RESET_TIMEOUT 500 // 500 miliseconds
25#define AHCI_LINK_TIMEOUT 10 // 10 miliseconds
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010026
27/****************************************************************
28 * these bits must run in both 16bit and 32bit modes
29 ****************************************************************/
30
31// prepare sata command fis
32static void sata_prep_simple(struct sata_cmd_fis *fis, u8 command)
33{
34 memset_fl(fis, 0, sizeof(*fis));
Kevin O'Connor890c0852012-05-24 23:55:00 -040035 SET_LOWFLAT(fis->command, command);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010036}
37
38static void sata_prep_readwrite(struct sata_cmd_fis *fis,
39 struct disk_op_s *op, int iswrite)
40{
41 u64 lba = op->lba;
42 u8 command;
43
44 memset_fl(fis, 0, sizeof(*fis));
45
46 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
Kevin O'Connor890c0852012-05-24 23:55:00 -040047 SET_LOWFLAT(fis->sector_count2, op->count >> 8);
48 SET_LOWFLAT(fis->lba_low2, lba >> 24);
49 SET_LOWFLAT(fis->lba_mid2, lba >> 32);
50 SET_LOWFLAT(fis->lba_high2, lba >> 40);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010051 lba &= 0xffffff;
52 command = (iswrite ? ATA_CMD_WRITE_DMA_EXT
53 : ATA_CMD_READ_DMA_EXT);
54 } else {
55 command = (iswrite ? ATA_CMD_WRITE_DMA
56 : ATA_CMD_READ_DMA);
57 }
Kevin O'Connor890c0852012-05-24 23:55:00 -040058 SET_LOWFLAT(fis->feature, 1); /* dma */
59 SET_LOWFLAT(fis->command, command);
60 SET_LOWFLAT(fis->sector_count, op->count);
61 SET_LOWFLAT(fis->lba_low, lba);
62 SET_LOWFLAT(fis->lba_mid, lba >> 8);
63 SET_LOWFLAT(fis->lba_high, lba >> 16);
64 SET_LOWFLAT(fis->device, ((lba >> 24) & 0xf) | ATA_CB_DH_LBA);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010065}
66
67static void sata_prep_atapi(struct sata_cmd_fis *fis, u16 blocksize)
68{
69 memset_fl(fis, 0, sizeof(*fis));
Kevin O'Connor890c0852012-05-24 23:55:00 -040070 SET_LOWFLAT(fis->command, ATA_CMD_PACKET);
71 SET_LOWFLAT(fis->feature, 1); /* dma */
72 SET_LOWFLAT(fis->lba_mid, blocksize);
73 SET_LOWFLAT(fis->lba_high, blocksize >> 8);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010074}
75
76// ahci register access helpers
77static u32 ahci_ctrl_readl(struct ahci_ctrl_s *ctrl, u32 reg)
78{
79 u32 addr = GET_GLOBALFLAT(ctrl->iobase) + reg;
80 return pci_readl(addr);
81}
82
83static void ahci_ctrl_writel(struct ahci_ctrl_s *ctrl, u32 reg, u32 val)
84{
85 u32 addr = GET_GLOBALFLAT(ctrl->iobase) + reg;
86 pci_writel(addr, val);
87}
88
89static u32 ahci_port_to_ctrl(u32 pnr, u32 port_reg)
90{
91 u32 ctrl_reg = 0x100;
92 ctrl_reg += pnr * 0x80;
93 ctrl_reg += port_reg;
94 return ctrl_reg;
95}
96
97static u32 ahci_port_readl(struct ahci_ctrl_s *ctrl, u32 pnr, u32 reg)
98{
99 u32 ctrl_reg = ahci_port_to_ctrl(pnr, reg);
100 return ahci_ctrl_readl(ctrl, ctrl_reg);
101}
102
103static void ahci_port_writel(struct ahci_ctrl_s *ctrl, u32 pnr, u32 reg, u32 val)
104{
105 u32 ctrl_reg = ahci_port_to_ctrl(pnr, reg);
106 ahci_ctrl_writel(ctrl, ctrl_reg, val);
107}
108
109// submit ahci command + wait for result
110static int ahci_command(struct ahci_port_s *port, int iswrite, int isatapi,
111 void *buffer, u32 bsize)
112{
Gerd Hoffmann6f850492011-07-14 16:24:01 +0200113 u32 val, status, success, flags, intbits, error;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100114 struct ahci_ctrl_s *ctrl = GET_GLOBAL(port->ctrl);
115 struct ahci_cmd_s *cmd = GET_GLOBAL(port->cmd);
116 struct ahci_fis_s *fis = GET_GLOBAL(port->fis);
117 struct ahci_list_s *list = GET_GLOBAL(port->list);
118 u32 pnr = GET_GLOBAL(port->pnr);
119
Kevin O'Connor890c0852012-05-24 23:55:00 -0400120 SET_LOWFLAT(cmd->fis.reg, 0x27);
121 SET_LOWFLAT(cmd->fis.pmp_type, (1 << 7)); /* cmd fis */
122 SET_LOWFLAT(cmd->prdt[0].base, ((u32)buffer));
123 SET_LOWFLAT(cmd->prdt[0].baseu, 0);
124 SET_LOWFLAT(cmd->prdt[0].flags, bsize-1);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100125
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100126 flags = ((1 << 16) | /* one prd entry */
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100127 (iswrite ? (1 << 6) : 0) |
128 (isatapi ? (1 << 5) : 0) |
Gerd Hoffmanna8c6a4e2011-07-14 16:23:59 +0200129 (5 << 0)); /* fis length (dwords) */
Kevin O'Connor890c0852012-05-24 23:55:00 -0400130 SET_LOWFLAT(list[0].flags, flags);
131 SET_LOWFLAT(list[0].bytes, 0);
132 SET_LOWFLAT(list[0].base, ((u32)(cmd)));
133 SET_LOWFLAT(list[0].baseu, 0);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100134
Kevin O'Connorbf079e12012-03-11 11:19:52 -0400135 dprintf(8, "AHCI/%d: send cmd ...\n", pnr);
Gerd Hoffmann07532972011-07-14 16:24:00 +0200136 intbits = ahci_port_readl(ctrl, pnr, PORT_IRQ_STAT);
137 if (intbits)
138 ahci_port_writel(ctrl, pnr, PORT_IRQ_STAT, intbits);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100139 ahci_port_writel(ctrl, pnr, PORT_SCR_ACT, 1);
140 ahci_port_writel(ctrl, pnr, PORT_CMD_ISSUE, 1);
Gerd Hoffmann07532972011-07-14 16:24:00 +0200141
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400142 u32 end = timer_calc(AHCI_REQUEST_TIMEOUT);
Gerd Hoffmann07532972011-07-14 16:24:00 +0200143 do {
144 for (;;) {
145 intbits = ahci_port_readl(ctrl, pnr, PORT_IRQ_STAT);
146 if (intbits) {
147 ahci_port_writel(ctrl, pnr, PORT_IRQ_STAT, intbits);
148 if (intbits & 0x02) {
Kevin O'Connor890c0852012-05-24 23:55:00 -0400149 status = GET_LOWFLAT(fis->psfis[2]);
150 error = GET_LOWFLAT(fis->psfis[3]);
Gerd Hoffmann07532972011-07-14 16:24:00 +0200151 break;
152 }
153 if (intbits & 0x01) {
Kevin O'Connor890c0852012-05-24 23:55:00 -0400154 status = GET_LOWFLAT(fis->rfis[2]);
155 error = GET_LOWFLAT(fis->rfis[3]);
Gerd Hoffmann07532972011-07-14 16:24:00 +0200156 break;
157 }
158 }
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400159 if (timer_check(end)) {
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200160 warn_timeout();
161 return -1;
162 }
Gerd Hoffmann07532972011-07-14 16:24:00 +0200163 yield();
164 }
Kevin O'Connorbf079e12012-03-11 11:19:52 -0400165 dprintf(8, "AHCI/%d: ... intbits 0x%x, status 0x%x ...\n",
Gerd Hoffmann07532972011-07-14 16:24:00 +0200166 pnr, intbits, status);
167 } while (status & ATA_CB_STAT_BSY);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100168
169 success = (0x00 == (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_DF |
Gerd Hoffmanncbda7952011-07-14 16:24:03 +0200170 ATA_CB_STAT_ERR)) &&
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100171 ATA_CB_STAT_RDY == (status & (ATA_CB_STAT_RDY)));
Gerd Hoffmann6f850492011-07-14 16:24:01 +0200172 if (success) {
Kevin O'Connorbf079e12012-03-11 11:19:52 -0400173 dprintf(8, "AHCI/%d: ... finished, status 0x%x, OK\n", pnr,
Gerd Hoffmann6f850492011-07-14 16:24:01 +0200174 status);
175 } else {
176 dprintf(2, "AHCI/%d: ... finished, status 0x%x, ERROR 0x%x\n", pnr,
177 status, error);
178
179 // non-queued error recovery (AHCI 1.3 section 6.2.2.1)
180 // Clears PxCMD.ST to 0 to reset the PxCI register
181 val = ahci_port_readl(ctrl, pnr, PORT_CMD);
182 ahci_port_writel(ctrl, pnr, PORT_CMD, val & ~PORT_CMD_START);
183
184 // waits for PxCMD.CR to clear to 0
185 while (1) {
186 val = ahci_port_readl(ctrl, pnr, PORT_CMD);
187 if ((val & PORT_CMD_LIST_ON) == 0)
188 break;
189 yield();
190 }
191
192 // Clears any error bits in PxSERR to enable capturing new errors
193 val = ahci_port_readl(ctrl, pnr, PORT_SCR_ERR);
194 ahci_port_writel(ctrl, pnr, PORT_SCR_ERR, val);
195
196 // Clears status bits in PxIS as appropriate
197 val = ahci_port_readl(ctrl, pnr, PORT_IRQ_STAT);
198 ahci_port_writel(ctrl, pnr, PORT_IRQ_STAT, val);
199
200 // If PxTFD.STS.BSY or PxTFD.STS.DRQ is set to 1, issue
201 // a COMRESET to the device to put it in an idle state
202 val = ahci_port_readl(ctrl, pnr, PORT_TFDATA);
203 if (val & (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ)) {
204 dprintf(2, "AHCI/%d: issue comreset\n", pnr);
205 val = ahci_port_readl(ctrl, pnr, PORT_SCR_CTL);
206 // set Device Detection Initialization (DET) to 1 for 1 ms for comreset
207 ahci_port_writel(ctrl, pnr, PORT_SCR_CTL, val | 1);
208 mdelay (1);
209 ahci_port_writel(ctrl, pnr, PORT_SCR_CTL, val);
210 }
211
212 // Sets PxCMD.ST to 1 to enable issuing new commands
213 val = ahci_port_readl(ctrl, pnr, PORT_CMD);
214 ahci_port_writel(ctrl, pnr, PORT_CMD, val | PORT_CMD_START);
215 }
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100216 return success ? 0 : -1;
217}
218
219#define CDROM_CDB_SIZE 12
220
221int ahci_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
222{
Kevin O'Connor80c2b6e2010-12-05 12:52:02 -0500223 if (! CONFIG_AHCI)
224 return 0;
225
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100226 struct ahci_port_s *port = container_of(
227 op->drive_g, struct ahci_port_s, drive);
228 struct ahci_cmd_s *cmd = GET_GLOBAL(port->cmd);
229 u8 *atapi = cdbcmd;
230 int i, rc;
231
232 sata_prep_atapi(&cmd->fis, blocksize);
233 for (i = 0; i < CDROM_CDB_SIZE; i++) {
Kevin O'Connor890c0852012-05-24 23:55:00 -0400234 SET_LOWFLAT(cmd->atapi[i], atapi[i]);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100235 }
236 rc = ahci_command(port, 0, 1, op->buf_fl,
237 op->count * blocksize);
238 if (rc < 0)
239 return DISK_RET_EBADTRACK;
240 return DISK_RET_SUCCESS;
241}
242
Scott Duplichan9c48aab2011-07-14 16:24:02 +0200243// read/write count blocks from a harddrive, op->buf_fl must be word aligned
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100244static int
Scott Duplichan9c48aab2011-07-14 16:24:02 +0200245ahci_disk_readwrite_aligned(struct disk_op_s *op, int iswrite)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100246{
247 struct ahci_port_s *port = container_of(
248 op->drive_g, struct ahci_port_s, drive);
249 struct ahci_cmd_s *cmd = GET_GLOBAL(port->cmd);
250 int rc;
251
252 sata_prep_readwrite(&cmd->fis, op, iswrite);
253 rc = ahci_command(port, iswrite, 0, op->buf_fl,
254 op->count * DISK_SECTOR_SIZE);
Kevin O'Connorbf079e12012-03-11 11:19:52 -0400255 dprintf(8, "ahci disk %s, lba %6x, count %3x, buf %p, rc %d\n",
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100256 iswrite ? "write" : "read", (u32)op->lba, op->count, op->buf_fl, rc);
257 if (rc < 0)
258 return DISK_RET_EBADTRACK;
259 return DISK_RET_SUCCESS;
260}
261
Scott Duplichan9c48aab2011-07-14 16:24:02 +0200262// read/write count blocks from a harddrive.
263static int
264ahci_disk_readwrite(struct disk_op_s *op, int iswrite)
265{
266 // if caller's buffer is word aligned, use it directly
267 if (((u32) op->buf_fl & 1) == 0)
268 return ahci_disk_readwrite_aligned(op, iswrite);
269
270 // Use a word aligned buffer for AHCI I/O
271 int rc;
272 struct disk_op_s localop = *op;
Gerd Hoffmannd7a7cf32011-08-04 19:36:27 +0200273 u8 *alignedbuf_fl = GET_GLOBAL(bounce_buf_fl);
Scott Duplichan9c48aab2011-07-14 16:24:02 +0200274 u8 *position = op->buf_fl;
275
276 localop.buf_fl = alignedbuf_fl;
277 localop.count = 1;
278
279 if (iswrite) {
280 u16 block;
281 for (block = 0; block < op->count; block++) {
282 memcpy_fl (alignedbuf_fl, position, DISK_SECTOR_SIZE);
283 rc = ahci_disk_readwrite_aligned (&localop, 1);
284 if (rc)
285 return rc;
286 position += DISK_SECTOR_SIZE;
287 localop.lba++;
288 }
289 } else { // read
290 u16 block;
291 for (block = 0; block < op->count; block++) {
292 rc = ahci_disk_readwrite_aligned (&localop, 0);
293 if (rc)
294 return rc;
295 memcpy_fl (position, alignedbuf_fl, DISK_SECTOR_SIZE);
296 position += DISK_SECTOR_SIZE;
297 localop.lba++;
298 }
299 }
300 return DISK_RET_SUCCESS;
301}
302
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100303// command demuxer
304int process_ahci_op(struct disk_op_s *op)
305{
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100306 if (!CONFIG_AHCI)
307 return 0;
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400308 switch (op->command) {
309 case CMD_READ:
310 return ahci_disk_readwrite(op, 0);
311 case CMD_WRITE:
312 return ahci_disk_readwrite(op, 1);
313 case CMD_FORMAT:
314 case CMD_RESET:
315 case CMD_ISREADY:
316 case CMD_VERIFY:
317 case CMD_SEEK:
318 return DISK_RET_SUCCESS;
319 default:
320 dprintf(1, "AHCI: unknown disk command %d\n", op->command);
321 op->count = 0;
322 return DISK_RET_EPARAM;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100323 }
324}
325
326/****************************************************************
327 * everything below is pure 32bit code
328 ****************************************************************/
329
330static void
331ahci_port_reset(struct ahci_ctrl_s *ctrl, u32 pnr)
332{
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200333 u32 val;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100334
335 /* disable FIS + CMD */
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400336 u32 end = timer_calc(AHCI_RESET_TIMEOUT);
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200337 for (;;) {
338 val = ahci_port_readl(ctrl, pnr, PORT_CMD);
339 if (!(val & (PORT_CMD_FIS_RX | PORT_CMD_START |
340 PORT_CMD_FIS_ON | PORT_CMD_LIST_ON)))
341 break;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100342 val &= ~(PORT_CMD_FIS_RX | PORT_CMD_START);
343 ahci_port_writel(ctrl, pnr, PORT_CMD, val);
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400344 if (timer_check(end)) {
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200345 warn_timeout();
346 break;
347 }
348 yield();
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100349 }
350
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100351 /* disable + clear IRQs */
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200352 ahci_port_writel(ctrl, pnr, PORT_IRQ_MASK, 0);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100353 val = ahci_port_readl(ctrl, pnr, PORT_IRQ_STAT);
354 if (val)
355 ahci_port_writel(ctrl, pnr, PORT_IRQ_STAT, val);
356}
357
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100358static struct ahci_port_s*
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200359ahci_port_alloc(struct ahci_ctrl_s *ctrl, u32 pnr)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100360{
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200361 struct ahci_port_s *port = malloc_tmp(sizeof(*port));
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100362
363 if (!port) {
364 warn_noalloc();
365 return NULL;
366 }
367 port->pnr = pnr;
368 port->ctrl = ctrl;
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200369 port->list = memalign_tmp(1024, 1024);
370 port->fis = memalign_tmp(256, 256);
371 port->cmd = memalign_tmp(256, 256);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100372 if (port->list == NULL || port->fis == NULL || port->cmd == NULL) {
373 warn_noalloc();
374 return NULL;
375 }
376 memset(port->list, 0, 1024);
377 memset(port->fis, 0, 256);
378 memset(port->cmd, 0, 256);
379
380 ahci_port_writel(ctrl, pnr, PORT_LST_ADDR, (u32)port->list);
381 ahci_port_writel(ctrl, pnr, PORT_FIS_ADDR, (u32)port->fis);
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200382 return port;
383}
384
Gerd Hoffmannce12eaf2013-09-09 16:33:06 +0200385static void ahci_port_release(struct ahci_port_s *port)
386{
387 ahci_port_reset(port->ctrl, port->pnr);
388 free(port->list);
389 free(port->fis);
390 free(port->cmd);
391 free(port);
392}
393
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200394static struct ahci_port_s* ahci_port_realloc(struct ahci_port_s *port)
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200395{
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200396 struct ahci_port_s *tmp;
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200397 u32 cmd;
398
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200399 tmp = malloc_fseg(sizeof(*port));
Gerd Hoffmannce12eaf2013-09-09 16:33:06 +0200400 if (!tmp) {
401 warn_noalloc();
402 ahci_port_release(port);
403 return NULL;
404 }
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200405 *tmp = *port;
406 free(port);
407 port = tmp;
408
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200409 ahci_port_reset(port->ctrl, port->pnr);
410
411 free(port->list);
412 free(port->fis);
413 free(port->cmd);
414 port->list = memalign_low(1024, 1024);
415 port->fis = memalign_low(256, 256);
416 port->cmd = memalign_low(256, 256);
417
418 ahci_port_writel(port->ctrl, port->pnr, PORT_LST_ADDR, (u32)port->list);
419 ahci_port_writel(port->ctrl, port->pnr, PORT_FIS_ADDR, (u32)port->fis);
420
421 cmd = ahci_port_readl(port->ctrl, port->pnr, PORT_CMD);
422 cmd |= (PORT_CMD_FIS_RX|PORT_CMD_START);
423 ahci_port_writel(port->ctrl, port->pnr, PORT_CMD, cmd);
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200424
425 return port;
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200426}
427
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200428#define MAXMODEL 40
429
430/* See ahci spec chapter 10.1 "Software Initialization of HBA" */
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500431static int ahci_port_setup(struct ahci_port_s *port)
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200432{
433 struct ahci_ctrl_s *ctrl = port->ctrl;
434 u32 pnr = port->pnr;
435 char model[MAXMODEL+1];
436 u16 buffer[256];
437 u32 cmd, stat, err, tf;
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200438 int rc;
439
440 /* enable FIS recv */
441 cmd = ahci_port_readl(ctrl, pnr, PORT_CMD);
442 cmd |= PORT_CMD_FIS_RX;
443 ahci_port_writel(ctrl, pnr, PORT_CMD, cmd);
444
445 /* spin up */
446 cmd |= PORT_CMD_SPIN_UP;
447 ahci_port_writel(ctrl, pnr, PORT_CMD, cmd);
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400448 u32 end = timer_calc(AHCI_LINK_TIMEOUT);
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200449 for (;;) {
450 stat = ahci_port_readl(ctrl, pnr, PORT_SCR_STAT);
451 if ((stat & 0x07) == 0x03) {
Kevin O'Connorbf079e12012-03-11 11:19:52 -0400452 dprintf(2, "AHCI/%d: link up\n", port->pnr);
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200453 break;
454 }
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400455 if (timer_check(end)) {
Kevin O'Connorbf079e12012-03-11 11:19:52 -0400456 dprintf(2, "AHCI/%d: link down\n", port->pnr);
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200457 return -1;
458 }
459 yield();
460 }
461
462 /* clear error status */
463 err = ahci_port_readl(ctrl, pnr, PORT_SCR_ERR);
464 if (err)
465 ahci_port_writel(ctrl, pnr, PORT_SCR_ERR, err);
466
467 /* wait for device becoming ready */
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400468 end = timer_calc(AHCI_REQUEST_TIMEOUT);
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200469 for (;;) {
470 tf = ahci_port_readl(ctrl, pnr, PORT_TFDATA);
471 if (!(tf & (ATA_CB_STAT_BSY |
472 ATA_CB_STAT_DRQ)))
473 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400474 if (timer_check(end)) {
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200475 warn_timeout();
476 dprintf(1, "AHCI/%d: device not ready (tf 0x%x)\n", port->pnr, tf);
477 return -1;
478 }
479 yield();
480 }
481
482 /* start device */
483 cmd |= PORT_CMD_START;
484 ahci_port_writel(ctrl, pnr, PORT_CMD, cmd);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100485
486 sata_prep_simple(&port->cmd->fis, ATA_CMD_IDENTIFY_PACKET_DEVICE);
487 rc = ahci_command(port, 0, 0, buffer, sizeof(buffer));
488 if (rc == 0) {
489 port->atapi = 1;
490 } else {
491 port->atapi = 0;
492 sata_prep_simple(&port->cmd->fis, ATA_CMD_IDENTIFY_DEVICE);
493 rc = ahci_command(port, 0, 0, buffer, sizeof(buffer));
494 if (rc < 0)
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200495 return -1;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100496 }
497
Gerd Hoffmann0e6f6362010-12-09 08:39:48 +0100498 port->drive.cntl_id = pnr;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100499 port->drive.removable = (buffer[0] & 0x80) ? 1 : 0;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100500
501 if (!port->atapi) {
502 // found disk (ata)
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400503 port->drive.type = DTYPE_AHCI;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100504 port->drive.blksize = DISK_SECTOR_SIZE;
505 port->drive.pchs.cylinders = buffer[1];
506 port->drive.pchs.heads = buffer[3];
507 port->drive.pchs.spt = buffer[6];
508
509 u64 sectors;
510 if (buffer[83] & (1 << 10)) // word 83 - lba48 support
511 sectors = *(u64*)&buffer[100]; // word 100-103
512 else
513 sectors = *(u32*)&buffer[60]; // word 60 and word 61
514 port->drive.sectors = sectors;
515 u64 adjsize = sectors >> 11;
516 char adjprefix = 'M';
517 if (adjsize >= (1 << 16)) {
518 adjsize >>= 10;
519 adjprefix = 'G';
520 }
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200521 port->desc = znprintf(MAXDESCSIZE
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500522 , "AHCI/%d: %s ATA-%d Hard-Disk (%u %ciBytes)"
523 , port->pnr
524 , ata_extract_model(model, MAXMODEL, buffer)
525 , ata_extract_version(buffer)
526 , (u32)adjsize, adjprefix);
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200527 port->prio = bootprio_find_ata_device(ctrl->pci_tmp, pnr, 0);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100528 } else {
529 // found cdrom (atapi)
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400530 port->drive.type = DTYPE_AHCI_ATAPI;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100531 port->drive.blksize = CDROM_SECTOR_SIZE;
532 port->drive.sectors = (u64)-1;
533 u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
Gerd Hoffmann263ea2f2011-08-04 19:36:29 +0200534 if (!iscd) {
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400535 dprintf(1, "AHCI/%d: atapi device isn't a cdrom\n", port->pnr);
Gerd Hoffmann263ea2f2011-08-04 19:36:29 +0200536 return -1;
537 }
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200538 port->desc = znprintf(MAXDESCSIZE
Gerd Hoffmann263ea2f2011-08-04 19:36:29 +0200539 , "DVD/CD [AHCI/%d: %s ATAPI-%d DVD/CD]"
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500540 , port->pnr
541 , ata_extract_model(model, MAXMODEL, buffer)
Gerd Hoffmann263ea2f2011-08-04 19:36:29 +0200542 , ata_extract_version(buffer));
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200543 port->prio = bootprio_find_ata_device(ctrl->pci_tmp, pnr, 0);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100544 }
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200545 return 0;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100546}
547
548// Detect any drives attached to a given controller.
549static void
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200550ahci_port_detect(void *data)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100551{
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200552 struct ahci_port_s *port = data;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100553 int rc;
554
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200555 dprintf(2, "AHCI/%d: probing\n", port->pnr);
556 ahci_port_reset(port->ctrl, port->pnr);
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500557 rc = ahci_port_setup(port);
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200558 if (rc < 0)
559 ahci_port_release(port);
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200560 else {
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200561 port = ahci_port_realloc(port);
Gerd Hoffmannce12eaf2013-09-09 16:33:06 +0200562 if (port == NULL)
563 return;
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200564 dprintf(1, "AHCI/%d: registering: \"%s\"\n", port->pnr, port->desc);
565 if (!port->atapi) {
566 // Register with bcv system.
567 boot_add_hd(&port->drive, port->desc, port->prio);
568 } else {
569 // fill cdidmap
570 boot_add_cd(&port->drive, port->desc, port->prio);
571 }
572 }
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100573}
574
575// Initialize an ata controller and detect its drives.
576static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500577ahci_controller_setup(struct pci_device *pci)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100578{
579 struct ahci_ctrl_s *ctrl = malloc_fseg(sizeof(*ctrl));
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200580 struct ahci_port_s *port;
Gerd Hoffmann9c869922011-07-14 16:24:05 +0200581 u16 bdf = pci->bdf;
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200582 u32 val, pnr, max;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100583
584 if (!ctrl) {
585 warn_noalloc();
586 return;
587 }
Scott Duplichan9c48aab2011-07-14 16:24:02 +0200588
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500589 if (create_bounce_buf() < 0) {
Scott Duplichan9c48aab2011-07-14 16:24:02 +0200590 warn_noalloc();
Gerd Hoffmannd7a7cf32011-08-04 19:36:27 +0200591 free(ctrl);
Scott Duplichan9c48aab2011-07-14 16:24:02 +0200592 return;
593 }
594
Gerd Hoffmann9c869922011-07-14 16:24:05 +0200595 ctrl->pci_tmp = pci;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100596 ctrl->pci_bdf = bdf;
597 ctrl->iobase = pci_config_readl(bdf, PCI_BASE_ADDRESS_5);
598 ctrl->irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
599 dprintf(1, "AHCI controller at %02x.%x, iobase %x, irq %d\n",
600 bdf >> 3, bdf & 7, ctrl->iobase, ctrl->irq);
601
Kevin O'Connor7eb02222010-12-12 14:01:47 -0500602 pci_config_maskw(bdf, PCI_COMMAND, 0,
603 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
604
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100605 val = ahci_ctrl_readl(ctrl, HOST_CTL);
606 ahci_ctrl_writel(ctrl, HOST_CTL, val | HOST_CTL_AHCI_EN);
607
608 ctrl->caps = ahci_ctrl_readl(ctrl, HOST_CAP);
609 ctrl->ports = ahci_ctrl_readl(ctrl, HOST_PORTS_IMPL);
610 dprintf(2, "AHCI: cap 0x%x, ports_impl 0x%x\n",
611 ctrl->caps, ctrl->ports);
612
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200613 max = ctrl->caps & 0x1f;
614 for (pnr = 0; pnr <= max; pnr++) {
615 if (!(ctrl->ports & (1 << pnr)))
616 continue;
617 port = ahci_port_alloc(ctrl, pnr);
618 if (port == NULL)
619 continue;
620 run_thread(ahci_port_detect, port);
621 }
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100622}
623
624// Locate and init ahci controllers.
625static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500626ahci_scan(void)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100627{
628 // Scan PCI bus for ATA adapters
Kevin O'Connor9cb49922011-06-20 22:22:42 -0400629 struct pci_device *pci;
630 foreachpci(pci) {
631 if (pci->class != PCI_CLASS_STORAGE_SATA)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100632 continue;
Kevin O'Connor9cb49922011-06-20 22:22:42 -0400633 if (pci->prog_if != 1 /* AHCI rev 1 */)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100634 continue;
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500635 ahci_controller_setup(pci);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100636 }
637}
638
639void
640ahci_setup(void)
641{
642 ASSERT32FLAT();
643 if (!CONFIG_AHCI)
644 return;
645
646 dprintf(3, "init ahci\n");
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500647 ahci_scan();
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100648}