blob: 0e99d1444a32b1b5684bac9d9a7dbaaf9d83ef20 [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
7#include "types.h" // u8
8#include "ioport.h" // inb
9#include "util.h" // dprintf
Kevin O'Connor4bc49972012-05-13 22:58:08 -040010#include "biosvar.h" // GET_GLOBAL
Kevin O'Connor9cb49922011-06-20 22:22:42 -040011#include "pci.h" // foreachpci
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010012#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
Gerd Hoffmanne1041192011-07-14 16:24:04 +020020#define AHCI_REQUEST_TIMEOUT 32000 // 32 seconds max for IDE ops
21#define AHCI_RESET_TIMEOUT 500 // 500 miliseconds
22#define AHCI_LINK_TIMEOUT 10 // 10 miliseconds
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010023
24/****************************************************************
25 * these bits must run in both 16bit and 32bit modes
26 ****************************************************************/
27
28// prepare sata command fis
29static void sata_prep_simple(struct sata_cmd_fis *fis, u8 command)
30{
31 memset_fl(fis, 0, sizeof(*fis));
Kevin O'Connor890c0852012-05-24 23:55:00 -040032 SET_LOWFLAT(fis->command, command);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010033}
34
35static void sata_prep_readwrite(struct sata_cmd_fis *fis,
36 struct disk_op_s *op, int iswrite)
37{
38 u64 lba = op->lba;
39 u8 command;
40
41 memset_fl(fis, 0, sizeof(*fis));
42
43 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
Kevin O'Connor890c0852012-05-24 23:55:00 -040044 SET_LOWFLAT(fis->sector_count2, op->count >> 8);
45 SET_LOWFLAT(fis->lba_low2, lba >> 24);
46 SET_LOWFLAT(fis->lba_mid2, lba >> 32);
47 SET_LOWFLAT(fis->lba_high2, lba >> 40);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010048 lba &= 0xffffff;
49 command = (iswrite ? ATA_CMD_WRITE_DMA_EXT
50 : ATA_CMD_READ_DMA_EXT);
51 } else {
52 command = (iswrite ? ATA_CMD_WRITE_DMA
53 : ATA_CMD_READ_DMA);
54 }
Kevin O'Connor890c0852012-05-24 23:55:00 -040055 SET_LOWFLAT(fis->feature, 1); /* dma */
56 SET_LOWFLAT(fis->command, command);
57 SET_LOWFLAT(fis->sector_count, op->count);
58 SET_LOWFLAT(fis->lba_low, lba);
59 SET_LOWFLAT(fis->lba_mid, lba >> 8);
60 SET_LOWFLAT(fis->lba_high, lba >> 16);
61 SET_LOWFLAT(fis->device, ((lba >> 24) & 0xf) | ATA_CB_DH_LBA);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010062}
63
64static void sata_prep_atapi(struct sata_cmd_fis *fis, u16 blocksize)
65{
66 memset_fl(fis, 0, sizeof(*fis));
Kevin O'Connor890c0852012-05-24 23:55:00 -040067 SET_LOWFLAT(fis->command, ATA_CMD_PACKET);
68 SET_LOWFLAT(fis->feature, 1); /* dma */
69 SET_LOWFLAT(fis->lba_mid, blocksize);
70 SET_LOWFLAT(fis->lba_high, blocksize >> 8);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010071}
72
73// ahci register access helpers
74static u32 ahci_ctrl_readl(struct ahci_ctrl_s *ctrl, u32 reg)
75{
76 u32 addr = GET_GLOBALFLAT(ctrl->iobase) + reg;
77 return pci_readl(addr);
78}
79
80static void ahci_ctrl_writel(struct ahci_ctrl_s *ctrl, u32 reg, u32 val)
81{
82 u32 addr = GET_GLOBALFLAT(ctrl->iobase) + reg;
83 pci_writel(addr, val);
84}
85
86static u32 ahci_port_to_ctrl(u32 pnr, u32 port_reg)
87{
88 u32 ctrl_reg = 0x100;
89 ctrl_reg += pnr * 0x80;
90 ctrl_reg += port_reg;
91 return ctrl_reg;
92}
93
94static u32 ahci_port_readl(struct ahci_ctrl_s *ctrl, u32 pnr, u32 reg)
95{
96 u32 ctrl_reg = ahci_port_to_ctrl(pnr, reg);
97 return ahci_ctrl_readl(ctrl, ctrl_reg);
98}
99
100static void ahci_port_writel(struct ahci_ctrl_s *ctrl, u32 pnr, u32 reg, u32 val)
101{
102 u32 ctrl_reg = ahci_port_to_ctrl(pnr, reg);
103 ahci_ctrl_writel(ctrl, ctrl_reg, val);
104}
105
106// submit ahci command + wait for result
107static int ahci_command(struct ahci_port_s *port, int iswrite, int isatapi,
108 void *buffer, u32 bsize)
109{
Gerd Hoffmann6f850492011-07-14 16:24:01 +0200110 u32 val, status, success, flags, intbits, error;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100111 struct ahci_ctrl_s *ctrl = GET_GLOBAL(port->ctrl);
112 struct ahci_cmd_s *cmd = GET_GLOBAL(port->cmd);
113 struct ahci_fis_s *fis = GET_GLOBAL(port->fis);
114 struct ahci_list_s *list = GET_GLOBAL(port->list);
115 u32 pnr = GET_GLOBAL(port->pnr);
116
Kevin O'Connor890c0852012-05-24 23:55:00 -0400117 SET_LOWFLAT(cmd->fis.reg, 0x27);
118 SET_LOWFLAT(cmd->fis.pmp_type, (1 << 7)); /* cmd fis */
119 SET_LOWFLAT(cmd->prdt[0].base, ((u32)buffer));
120 SET_LOWFLAT(cmd->prdt[0].baseu, 0);
121 SET_LOWFLAT(cmd->prdt[0].flags, bsize-1);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100122
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100123 flags = ((1 << 16) | /* one prd entry */
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100124 (iswrite ? (1 << 6) : 0) |
125 (isatapi ? (1 << 5) : 0) |
Gerd Hoffmanna8c6a4e2011-07-14 16:23:59 +0200126 (5 << 0)); /* fis length (dwords) */
Kevin O'Connor890c0852012-05-24 23:55:00 -0400127 SET_LOWFLAT(list[0].flags, flags);
128 SET_LOWFLAT(list[0].bytes, 0);
129 SET_LOWFLAT(list[0].base, ((u32)(cmd)));
130 SET_LOWFLAT(list[0].baseu, 0);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100131
Kevin O'Connorbf079e12012-03-11 11:19:52 -0400132 dprintf(8, "AHCI/%d: send cmd ...\n", pnr);
Gerd Hoffmann07532972011-07-14 16:24:00 +0200133 intbits = ahci_port_readl(ctrl, pnr, PORT_IRQ_STAT);
134 if (intbits)
135 ahci_port_writel(ctrl, pnr, PORT_IRQ_STAT, intbits);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100136 ahci_port_writel(ctrl, pnr, PORT_SCR_ACT, 1);
137 ahci_port_writel(ctrl, pnr, PORT_CMD_ISSUE, 1);
Gerd Hoffmann07532972011-07-14 16:24:00 +0200138
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400139 u32 end = timer_calc(AHCI_REQUEST_TIMEOUT);
Gerd Hoffmann07532972011-07-14 16:24:00 +0200140 do {
141 for (;;) {
142 intbits = ahci_port_readl(ctrl, pnr, PORT_IRQ_STAT);
143 if (intbits) {
144 ahci_port_writel(ctrl, pnr, PORT_IRQ_STAT, intbits);
145 if (intbits & 0x02) {
Kevin O'Connor890c0852012-05-24 23:55:00 -0400146 status = GET_LOWFLAT(fis->psfis[2]);
147 error = GET_LOWFLAT(fis->psfis[3]);
Gerd Hoffmann07532972011-07-14 16:24:00 +0200148 break;
149 }
150 if (intbits & 0x01) {
Kevin O'Connor890c0852012-05-24 23:55:00 -0400151 status = GET_LOWFLAT(fis->rfis[2]);
152 error = GET_LOWFLAT(fis->rfis[3]);
Gerd Hoffmann07532972011-07-14 16:24:00 +0200153 break;
154 }
155 }
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400156 if (timer_check(end)) {
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200157 warn_timeout();
158 return -1;
159 }
Gerd Hoffmann07532972011-07-14 16:24:00 +0200160 yield();
161 }
Kevin O'Connorbf079e12012-03-11 11:19:52 -0400162 dprintf(8, "AHCI/%d: ... intbits 0x%x, status 0x%x ...\n",
Gerd Hoffmann07532972011-07-14 16:24:00 +0200163 pnr, intbits, status);
164 } while (status & ATA_CB_STAT_BSY);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100165
166 success = (0x00 == (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_DF |
Gerd Hoffmanncbda7952011-07-14 16:24:03 +0200167 ATA_CB_STAT_ERR)) &&
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100168 ATA_CB_STAT_RDY == (status & (ATA_CB_STAT_RDY)));
Gerd Hoffmann6f850492011-07-14 16:24:01 +0200169 if (success) {
Kevin O'Connorbf079e12012-03-11 11:19:52 -0400170 dprintf(8, "AHCI/%d: ... finished, status 0x%x, OK\n", pnr,
Gerd Hoffmann6f850492011-07-14 16:24:01 +0200171 status);
172 } else {
173 dprintf(2, "AHCI/%d: ... finished, status 0x%x, ERROR 0x%x\n", pnr,
174 status, error);
175
176 // non-queued error recovery (AHCI 1.3 section 6.2.2.1)
177 // Clears PxCMD.ST to 0 to reset the PxCI register
178 val = ahci_port_readl(ctrl, pnr, PORT_CMD);
179 ahci_port_writel(ctrl, pnr, PORT_CMD, val & ~PORT_CMD_START);
180
181 // waits for PxCMD.CR to clear to 0
182 while (1) {
183 val = ahci_port_readl(ctrl, pnr, PORT_CMD);
184 if ((val & PORT_CMD_LIST_ON) == 0)
185 break;
186 yield();
187 }
188
189 // Clears any error bits in PxSERR to enable capturing new errors
190 val = ahci_port_readl(ctrl, pnr, PORT_SCR_ERR);
191 ahci_port_writel(ctrl, pnr, PORT_SCR_ERR, val);
192
193 // Clears status bits in PxIS as appropriate
194 val = ahci_port_readl(ctrl, pnr, PORT_IRQ_STAT);
195 ahci_port_writel(ctrl, pnr, PORT_IRQ_STAT, val);
196
197 // If PxTFD.STS.BSY or PxTFD.STS.DRQ is set to 1, issue
198 // a COMRESET to the device to put it in an idle state
199 val = ahci_port_readl(ctrl, pnr, PORT_TFDATA);
200 if (val & (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ)) {
201 dprintf(2, "AHCI/%d: issue comreset\n", pnr);
202 val = ahci_port_readl(ctrl, pnr, PORT_SCR_CTL);
203 // set Device Detection Initialization (DET) to 1 for 1 ms for comreset
204 ahci_port_writel(ctrl, pnr, PORT_SCR_CTL, val | 1);
205 mdelay (1);
206 ahci_port_writel(ctrl, pnr, PORT_SCR_CTL, val);
207 }
208
209 // Sets PxCMD.ST to 1 to enable issuing new commands
210 val = ahci_port_readl(ctrl, pnr, PORT_CMD);
211 ahci_port_writel(ctrl, pnr, PORT_CMD, val | PORT_CMD_START);
212 }
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100213 return success ? 0 : -1;
214}
215
216#define CDROM_CDB_SIZE 12
217
218int ahci_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
219{
Kevin O'Connor80c2b6e2010-12-05 12:52:02 -0500220 if (! CONFIG_AHCI)
221 return 0;
222
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100223 struct ahci_port_s *port = container_of(
224 op->drive_g, struct ahci_port_s, drive);
225 struct ahci_cmd_s *cmd = GET_GLOBAL(port->cmd);
226 u8 *atapi = cdbcmd;
227 int i, rc;
228
229 sata_prep_atapi(&cmd->fis, blocksize);
230 for (i = 0; i < CDROM_CDB_SIZE; i++) {
Kevin O'Connor890c0852012-05-24 23:55:00 -0400231 SET_LOWFLAT(cmd->atapi[i], atapi[i]);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100232 }
233 rc = ahci_command(port, 0, 1, op->buf_fl,
234 op->count * blocksize);
235 if (rc < 0)
236 return DISK_RET_EBADTRACK;
237 return DISK_RET_SUCCESS;
238}
239
Scott Duplichan9c48aab2011-07-14 16:24:02 +0200240// read/write count blocks from a harddrive, op->buf_fl must be word aligned
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100241static int
Scott Duplichan9c48aab2011-07-14 16:24:02 +0200242ahci_disk_readwrite_aligned(struct disk_op_s *op, int iswrite)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100243{
244 struct ahci_port_s *port = container_of(
245 op->drive_g, struct ahci_port_s, drive);
246 struct ahci_cmd_s *cmd = GET_GLOBAL(port->cmd);
247 int rc;
248
249 sata_prep_readwrite(&cmd->fis, op, iswrite);
250 rc = ahci_command(port, iswrite, 0, op->buf_fl,
251 op->count * DISK_SECTOR_SIZE);
Kevin O'Connorbf079e12012-03-11 11:19:52 -0400252 dprintf(8, "ahci disk %s, lba %6x, count %3x, buf %p, rc %d\n",
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100253 iswrite ? "write" : "read", (u32)op->lba, op->count, op->buf_fl, rc);
254 if (rc < 0)
255 return DISK_RET_EBADTRACK;
256 return DISK_RET_SUCCESS;
257}
258
Scott Duplichan9c48aab2011-07-14 16:24:02 +0200259// read/write count blocks from a harddrive.
260static int
261ahci_disk_readwrite(struct disk_op_s *op, int iswrite)
262{
263 // if caller's buffer is word aligned, use it directly
264 if (((u32) op->buf_fl & 1) == 0)
265 return ahci_disk_readwrite_aligned(op, iswrite);
266
267 // Use a word aligned buffer for AHCI I/O
268 int rc;
269 struct disk_op_s localop = *op;
Gerd Hoffmannd7a7cf32011-08-04 19:36:27 +0200270 u8 *alignedbuf_fl = GET_GLOBAL(bounce_buf_fl);
Scott Duplichan9c48aab2011-07-14 16:24:02 +0200271 u8 *position = op->buf_fl;
272
273 localop.buf_fl = alignedbuf_fl;
274 localop.count = 1;
275
276 if (iswrite) {
277 u16 block;
278 for (block = 0; block < op->count; block++) {
279 memcpy_fl (alignedbuf_fl, position, DISK_SECTOR_SIZE);
280 rc = ahci_disk_readwrite_aligned (&localop, 1);
281 if (rc)
282 return rc;
283 position += DISK_SECTOR_SIZE;
284 localop.lba++;
285 }
286 } else { // read
287 u16 block;
288 for (block = 0; block < op->count; block++) {
289 rc = ahci_disk_readwrite_aligned (&localop, 0);
290 if (rc)
291 return rc;
292 memcpy_fl (position, alignedbuf_fl, DISK_SECTOR_SIZE);
293 position += DISK_SECTOR_SIZE;
294 localop.lba++;
295 }
296 }
297 return DISK_RET_SUCCESS;
298}
299
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100300// command demuxer
301int process_ahci_op(struct disk_op_s *op)
302{
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100303 if (!CONFIG_AHCI)
304 return 0;
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400305 switch (op->command) {
306 case CMD_READ:
307 return ahci_disk_readwrite(op, 0);
308 case CMD_WRITE:
309 return ahci_disk_readwrite(op, 1);
310 case CMD_FORMAT:
311 case CMD_RESET:
312 case CMD_ISREADY:
313 case CMD_VERIFY:
314 case CMD_SEEK:
315 return DISK_RET_SUCCESS;
316 default:
317 dprintf(1, "AHCI: unknown disk command %d\n", op->command);
318 op->count = 0;
319 return DISK_RET_EPARAM;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100320 }
321}
322
323/****************************************************************
324 * everything below is pure 32bit code
325 ****************************************************************/
326
327static void
328ahci_port_reset(struct ahci_ctrl_s *ctrl, u32 pnr)
329{
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200330 u32 val;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100331
332 /* disable FIS + CMD */
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400333 u32 end = timer_calc(AHCI_RESET_TIMEOUT);
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200334 for (;;) {
335 val = ahci_port_readl(ctrl, pnr, PORT_CMD);
336 if (!(val & (PORT_CMD_FIS_RX | PORT_CMD_START |
337 PORT_CMD_FIS_ON | PORT_CMD_LIST_ON)))
338 break;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100339 val &= ~(PORT_CMD_FIS_RX | PORT_CMD_START);
340 ahci_port_writel(ctrl, pnr, PORT_CMD, val);
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400341 if (timer_check(end)) {
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200342 warn_timeout();
343 break;
344 }
345 yield();
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100346 }
347
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100348 /* disable + clear IRQs */
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200349 ahci_port_writel(ctrl, pnr, PORT_IRQ_MASK, 0);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100350 val = ahci_port_readl(ctrl, pnr, PORT_IRQ_STAT);
351 if (val)
352 ahci_port_writel(ctrl, pnr, PORT_IRQ_STAT, val);
353}
354
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100355static struct ahci_port_s*
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200356ahci_port_alloc(struct ahci_ctrl_s *ctrl, u32 pnr)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100357{
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200358 struct ahci_port_s *port = malloc_tmp(sizeof(*port));
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100359
360 if (!port) {
361 warn_noalloc();
362 return NULL;
363 }
364 port->pnr = pnr;
365 port->ctrl = ctrl;
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200366 port->list = memalign_tmp(1024, 1024);
367 port->fis = memalign_tmp(256, 256);
368 port->cmd = memalign_tmp(256, 256);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100369 if (port->list == NULL || port->fis == NULL || port->cmd == NULL) {
370 warn_noalloc();
371 return NULL;
372 }
373 memset(port->list, 0, 1024);
374 memset(port->fis, 0, 256);
375 memset(port->cmd, 0, 256);
376
377 ahci_port_writel(ctrl, pnr, PORT_LST_ADDR, (u32)port->list);
378 ahci_port_writel(ctrl, pnr, PORT_FIS_ADDR, (u32)port->fis);
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200379 return port;
380}
381
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200382static struct ahci_port_s* ahci_port_realloc(struct ahci_port_s *port)
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200383{
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200384 struct ahci_port_s *tmp;
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200385 u32 cmd;
386
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200387 tmp = malloc_fseg(sizeof(*port));
388 *tmp = *port;
389 free(port);
390 port = tmp;
391
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200392 ahci_port_reset(port->ctrl, port->pnr);
393
394 free(port->list);
395 free(port->fis);
396 free(port->cmd);
397 port->list = memalign_low(1024, 1024);
398 port->fis = memalign_low(256, 256);
399 port->cmd = memalign_low(256, 256);
400
401 ahci_port_writel(port->ctrl, port->pnr, PORT_LST_ADDR, (u32)port->list);
402 ahci_port_writel(port->ctrl, port->pnr, PORT_FIS_ADDR, (u32)port->fis);
403
404 cmd = ahci_port_readl(port->ctrl, port->pnr, PORT_CMD);
405 cmd |= (PORT_CMD_FIS_RX|PORT_CMD_START);
406 ahci_port_writel(port->ctrl, port->pnr, PORT_CMD, cmd);
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200407
408 return port;
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200409}
410
411static void ahci_port_release(struct ahci_port_s *port)
412{
413 ahci_port_reset(port->ctrl, port->pnr);
414 free(port->list);
415 free(port->fis);
416 free(port->cmd);
417 free(port);
418}
419
420#define MAXMODEL 40
421
422/* See ahci spec chapter 10.1 "Software Initialization of HBA" */
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500423static int ahci_port_setup(struct ahci_port_s *port)
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200424{
425 struct ahci_ctrl_s *ctrl = port->ctrl;
426 u32 pnr = port->pnr;
427 char model[MAXMODEL+1];
428 u16 buffer[256];
429 u32 cmd, stat, err, tf;
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200430 int rc;
431
432 /* enable FIS recv */
433 cmd = ahci_port_readl(ctrl, pnr, PORT_CMD);
434 cmd |= PORT_CMD_FIS_RX;
435 ahci_port_writel(ctrl, pnr, PORT_CMD, cmd);
436
437 /* spin up */
438 cmd |= PORT_CMD_SPIN_UP;
439 ahci_port_writel(ctrl, pnr, PORT_CMD, cmd);
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400440 u32 end = timer_calc(AHCI_LINK_TIMEOUT);
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200441 for (;;) {
442 stat = ahci_port_readl(ctrl, pnr, PORT_SCR_STAT);
443 if ((stat & 0x07) == 0x03) {
Kevin O'Connorbf079e12012-03-11 11:19:52 -0400444 dprintf(2, "AHCI/%d: link up\n", port->pnr);
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200445 break;
446 }
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400447 if (timer_check(end)) {
Kevin O'Connorbf079e12012-03-11 11:19:52 -0400448 dprintf(2, "AHCI/%d: link down\n", port->pnr);
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200449 return -1;
450 }
451 yield();
452 }
453
454 /* clear error status */
455 err = ahci_port_readl(ctrl, pnr, PORT_SCR_ERR);
456 if (err)
457 ahci_port_writel(ctrl, pnr, PORT_SCR_ERR, err);
458
459 /* wait for device becoming ready */
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400460 end = timer_calc(AHCI_REQUEST_TIMEOUT);
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200461 for (;;) {
462 tf = ahci_port_readl(ctrl, pnr, PORT_TFDATA);
463 if (!(tf & (ATA_CB_STAT_BSY |
464 ATA_CB_STAT_DRQ)))
465 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400466 if (timer_check(end)) {
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200467 warn_timeout();
468 dprintf(1, "AHCI/%d: device not ready (tf 0x%x)\n", port->pnr, tf);
469 return -1;
470 }
471 yield();
472 }
473
474 /* start device */
475 cmd |= PORT_CMD_START;
476 ahci_port_writel(ctrl, pnr, PORT_CMD, cmd);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100477
478 sata_prep_simple(&port->cmd->fis, ATA_CMD_IDENTIFY_PACKET_DEVICE);
479 rc = ahci_command(port, 0, 0, buffer, sizeof(buffer));
480 if (rc == 0) {
481 port->atapi = 1;
482 } else {
483 port->atapi = 0;
484 sata_prep_simple(&port->cmd->fis, ATA_CMD_IDENTIFY_DEVICE);
485 rc = ahci_command(port, 0, 0, buffer, sizeof(buffer));
486 if (rc < 0)
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200487 return -1;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100488 }
489
Gerd Hoffmann0e6f6362010-12-09 08:39:48 +0100490 port->drive.cntl_id = pnr;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100491 port->drive.removable = (buffer[0] & 0x80) ? 1 : 0;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100492
493 if (!port->atapi) {
494 // found disk (ata)
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400495 port->drive.type = DTYPE_AHCI;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100496 port->drive.blksize = DISK_SECTOR_SIZE;
497 port->drive.pchs.cylinders = buffer[1];
498 port->drive.pchs.heads = buffer[3];
499 port->drive.pchs.spt = buffer[6];
500
501 u64 sectors;
502 if (buffer[83] & (1 << 10)) // word 83 - lba48 support
503 sectors = *(u64*)&buffer[100]; // word 100-103
504 else
505 sectors = *(u32*)&buffer[60]; // word 60 and word 61
506 port->drive.sectors = sectors;
507 u64 adjsize = sectors >> 11;
508 char adjprefix = 'M';
509 if (adjsize >= (1 << 16)) {
510 adjsize >>= 10;
511 adjprefix = 'G';
512 }
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200513 port->desc = znprintf(MAXDESCSIZE
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500514 , "AHCI/%d: %s ATA-%d Hard-Disk (%u %ciBytes)"
515 , port->pnr
516 , ata_extract_model(model, MAXMODEL, buffer)
517 , ata_extract_version(buffer)
518 , (u32)adjsize, adjprefix);
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200519 port->prio = bootprio_find_ata_device(ctrl->pci_tmp, pnr, 0);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100520 } else {
521 // found cdrom (atapi)
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400522 port->drive.type = DTYPE_AHCI_ATAPI;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100523 port->drive.blksize = CDROM_SECTOR_SIZE;
524 port->drive.sectors = (u64)-1;
525 u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
Gerd Hoffmann263ea2f2011-08-04 19:36:29 +0200526 if (!iscd) {
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400527 dprintf(1, "AHCI/%d: atapi device isn't a cdrom\n", port->pnr);
Gerd Hoffmann263ea2f2011-08-04 19:36:29 +0200528 return -1;
529 }
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200530 port->desc = znprintf(MAXDESCSIZE
Gerd Hoffmann263ea2f2011-08-04 19:36:29 +0200531 , "DVD/CD [AHCI/%d: %s ATAPI-%d DVD/CD]"
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500532 , port->pnr
533 , ata_extract_model(model, MAXMODEL, buffer)
Gerd Hoffmann263ea2f2011-08-04 19:36:29 +0200534 , ata_extract_version(buffer));
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200535 port->prio = bootprio_find_ata_device(ctrl->pci_tmp, pnr, 0);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100536 }
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200537 return 0;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100538}
539
540// Detect any drives attached to a given controller.
541static void
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200542ahci_port_detect(void *data)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100543{
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200544 struct ahci_port_s *port = data;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100545 int rc;
546
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200547 dprintf(2, "AHCI/%d: probing\n", port->pnr);
548 ahci_port_reset(port->ctrl, port->pnr);
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500549 rc = ahci_port_setup(port);
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200550 if (rc < 0)
551 ahci_port_release(port);
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200552 else {
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200553 port = ahci_port_realloc(port);
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200554 dprintf(1, "AHCI/%d: registering: \"%s\"\n", port->pnr, port->desc);
555 if (!port->atapi) {
556 // Register with bcv system.
557 boot_add_hd(&port->drive, port->desc, port->prio);
558 } else {
559 // fill cdidmap
560 boot_add_cd(&port->drive, port->desc, port->prio);
561 }
562 }
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100563}
564
565// Initialize an ata controller and detect its drives.
566static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500567ahci_controller_setup(struct pci_device *pci)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100568{
569 struct ahci_ctrl_s *ctrl = malloc_fseg(sizeof(*ctrl));
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200570 struct ahci_port_s *port;
Gerd Hoffmann9c869922011-07-14 16:24:05 +0200571 u16 bdf = pci->bdf;
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200572 u32 val, pnr, max;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100573
574 if (!ctrl) {
575 warn_noalloc();
576 return;
577 }
Scott Duplichan9c48aab2011-07-14 16:24:02 +0200578
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500579 if (create_bounce_buf() < 0) {
Scott Duplichan9c48aab2011-07-14 16:24:02 +0200580 warn_noalloc();
Gerd Hoffmannd7a7cf32011-08-04 19:36:27 +0200581 free(ctrl);
Scott Duplichan9c48aab2011-07-14 16:24:02 +0200582 return;
583 }
584
Gerd Hoffmann9c869922011-07-14 16:24:05 +0200585 ctrl->pci_tmp = pci;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100586 ctrl->pci_bdf = bdf;
587 ctrl->iobase = pci_config_readl(bdf, PCI_BASE_ADDRESS_5);
588 ctrl->irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
589 dprintf(1, "AHCI controller at %02x.%x, iobase %x, irq %d\n",
590 bdf >> 3, bdf & 7, ctrl->iobase, ctrl->irq);
591
Kevin O'Connor7eb02222010-12-12 14:01:47 -0500592 pci_config_maskw(bdf, PCI_COMMAND, 0,
593 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
594
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100595 val = ahci_ctrl_readl(ctrl, HOST_CTL);
596 ahci_ctrl_writel(ctrl, HOST_CTL, val | HOST_CTL_AHCI_EN);
597
598 ctrl->caps = ahci_ctrl_readl(ctrl, HOST_CAP);
599 ctrl->ports = ahci_ctrl_readl(ctrl, HOST_PORTS_IMPL);
600 dprintf(2, "AHCI: cap 0x%x, ports_impl 0x%x\n",
601 ctrl->caps, ctrl->ports);
602
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200603 max = ctrl->caps & 0x1f;
604 for (pnr = 0; pnr <= max; pnr++) {
605 if (!(ctrl->ports & (1 << pnr)))
606 continue;
607 port = ahci_port_alloc(ctrl, pnr);
608 if (port == NULL)
609 continue;
610 run_thread(ahci_port_detect, port);
611 }
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100612}
613
614// Locate and init ahci controllers.
615static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500616ahci_scan(void)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100617{
618 // Scan PCI bus for ATA adapters
Kevin O'Connor9cb49922011-06-20 22:22:42 -0400619 struct pci_device *pci;
620 foreachpci(pci) {
621 if (pci->class != PCI_CLASS_STORAGE_SATA)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100622 continue;
Kevin O'Connor9cb49922011-06-20 22:22:42 -0400623 if (pci->prog_if != 1 /* AHCI rev 1 */)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100624 continue;
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500625 ahci_controller_setup(pci);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100626 }
627}
628
629void
630ahci_setup(void)
631{
632 ASSERT32FLAT();
633 if (!CONFIG_AHCI)
634 return;
635
636 dprintf(3, "init ahci\n");
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500637 ahci_scan();
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100638}