blob: e2729a9773187327d1583402f9c65140d2951cc2 [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 Hoffmannce12eaf2013-09-09 16:33:06 +0200382static void ahci_port_release(struct ahci_port_s *port)
383{
384 ahci_port_reset(port->ctrl, port->pnr);
385 free(port->list);
386 free(port->fis);
387 free(port->cmd);
388 free(port);
389}
390
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200391static struct ahci_port_s* ahci_port_realloc(struct ahci_port_s *port)
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200392{
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200393 struct ahci_port_s *tmp;
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200394 u32 cmd;
395
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200396 tmp = malloc_fseg(sizeof(*port));
Gerd Hoffmannce12eaf2013-09-09 16:33:06 +0200397 if (!tmp) {
398 warn_noalloc();
399 ahci_port_release(port);
400 return NULL;
401 }
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200402 *tmp = *port;
403 free(port);
404 port = tmp;
405
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200406 ahci_port_reset(port->ctrl, port->pnr);
407
408 free(port->list);
409 free(port->fis);
410 free(port->cmd);
411 port->list = memalign_low(1024, 1024);
412 port->fis = memalign_low(256, 256);
413 port->cmd = memalign_low(256, 256);
414
415 ahci_port_writel(port->ctrl, port->pnr, PORT_LST_ADDR, (u32)port->list);
416 ahci_port_writel(port->ctrl, port->pnr, PORT_FIS_ADDR, (u32)port->fis);
417
418 cmd = ahci_port_readl(port->ctrl, port->pnr, PORT_CMD);
419 cmd |= (PORT_CMD_FIS_RX|PORT_CMD_START);
420 ahci_port_writel(port->ctrl, port->pnr, PORT_CMD, cmd);
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200421
422 return port;
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200423}
424
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200425#define MAXMODEL 40
426
427/* See ahci spec chapter 10.1 "Software Initialization of HBA" */
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500428static int ahci_port_setup(struct ahci_port_s *port)
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200429{
430 struct ahci_ctrl_s *ctrl = port->ctrl;
431 u32 pnr = port->pnr;
432 char model[MAXMODEL+1];
433 u16 buffer[256];
434 u32 cmd, stat, err, tf;
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200435 int rc;
436
437 /* enable FIS recv */
438 cmd = ahci_port_readl(ctrl, pnr, PORT_CMD);
439 cmd |= PORT_CMD_FIS_RX;
440 ahci_port_writel(ctrl, pnr, PORT_CMD, cmd);
441
442 /* spin up */
443 cmd |= PORT_CMD_SPIN_UP;
444 ahci_port_writel(ctrl, pnr, PORT_CMD, cmd);
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400445 u32 end = timer_calc(AHCI_LINK_TIMEOUT);
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200446 for (;;) {
447 stat = ahci_port_readl(ctrl, pnr, PORT_SCR_STAT);
448 if ((stat & 0x07) == 0x03) {
Kevin O'Connorbf079e12012-03-11 11:19:52 -0400449 dprintf(2, "AHCI/%d: link up\n", port->pnr);
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200450 break;
451 }
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400452 if (timer_check(end)) {
Kevin O'Connorbf079e12012-03-11 11:19:52 -0400453 dprintf(2, "AHCI/%d: link down\n", port->pnr);
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200454 return -1;
455 }
456 yield();
457 }
458
459 /* clear error status */
460 err = ahci_port_readl(ctrl, pnr, PORT_SCR_ERR);
461 if (err)
462 ahci_port_writel(ctrl, pnr, PORT_SCR_ERR, err);
463
464 /* wait for device becoming ready */
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400465 end = timer_calc(AHCI_REQUEST_TIMEOUT);
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200466 for (;;) {
467 tf = ahci_port_readl(ctrl, pnr, PORT_TFDATA);
468 if (!(tf & (ATA_CB_STAT_BSY |
469 ATA_CB_STAT_DRQ)))
470 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400471 if (timer_check(end)) {
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200472 warn_timeout();
473 dprintf(1, "AHCI/%d: device not ready (tf 0x%x)\n", port->pnr, tf);
474 return -1;
475 }
476 yield();
477 }
478
479 /* start device */
480 cmd |= PORT_CMD_START;
481 ahci_port_writel(ctrl, pnr, PORT_CMD, cmd);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100482
483 sata_prep_simple(&port->cmd->fis, ATA_CMD_IDENTIFY_PACKET_DEVICE);
484 rc = ahci_command(port, 0, 0, buffer, sizeof(buffer));
485 if (rc == 0) {
486 port->atapi = 1;
487 } else {
488 port->atapi = 0;
489 sata_prep_simple(&port->cmd->fis, ATA_CMD_IDENTIFY_DEVICE);
490 rc = ahci_command(port, 0, 0, buffer, sizeof(buffer));
491 if (rc < 0)
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200492 return -1;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100493 }
494
Gerd Hoffmann0e6f6362010-12-09 08:39:48 +0100495 port->drive.cntl_id = pnr;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100496 port->drive.removable = (buffer[0] & 0x80) ? 1 : 0;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100497
498 if (!port->atapi) {
499 // found disk (ata)
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400500 port->drive.type = DTYPE_AHCI;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100501 port->drive.blksize = DISK_SECTOR_SIZE;
502 port->drive.pchs.cylinders = buffer[1];
503 port->drive.pchs.heads = buffer[3];
504 port->drive.pchs.spt = buffer[6];
505
506 u64 sectors;
507 if (buffer[83] & (1 << 10)) // word 83 - lba48 support
508 sectors = *(u64*)&buffer[100]; // word 100-103
509 else
510 sectors = *(u32*)&buffer[60]; // word 60 and word 61
511 port->drive.sectors = sectors;
512 u64 adjsize = sectors >> 11;
513 char adjprefix = 'M';
514 if (adjsize >= (1 << 16)) {
515 adjsize >>= 10;
516 adjprefix = 'G';
517 }
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200518 port->desc = znprintf(MAXDESCSIZE
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500519 , "AHCI/%d: %s ATA-%d Hard-Disk (%u %ciBytes)"
520 , port->pnr
521 , ata_extract_model(model, MAXMODEL, buffer)
522 , ata_extract_version(buffer)
523 , (u32)adjsize, adjprefix);
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200524 port->prio = bootprio_find_ata_device(ctrl->pci_tmp, pnr, 0);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100525 } else {
526 // found cdrom (atapi)
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400527 port->drive.type = DTYPE_AHCI_ATAPI;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100528 port->drive.blksize = CDROM_SECTOR_SIZE;
529 port->drive.sectors = (u64)-1;
530 u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
Gerd Hoffmann263ea2f2011-08-04 19:36:29 +0200531 if (!iscd) {
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400532 dprintf(1, "AHCI/%d: atapi device isn't a cdrom\n", port->pnr);
Gerd Hoffmann263ea2f2011-08-04 19:36:29 +0200533 return -1;
534 }
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200535 port->desc = znprintf(MAXDESCSIZE
Gerd Hoffmann263ea2f2011-08-04 19:36:29 +0200536 , "DVD/CD [AHCI/%d: %s ATAPI-%d DVD/CD]"
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500537 , port->pnr
538 , ata_extract_model(model, MAXMODEL, buffer)
Gerd Hoffmann263ea2f2011-08-04 19:36:29 +0200539 , ata_extract_version(buffer));
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200540 port->prio = bootprio_find_ata_device(ctrl->pci_tmp, pnr, 0);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100541 }
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200542 return 0;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100543}
544
545// Detect any drives attached to a given controller.
546static void
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200547ahci_port_detect(void *data)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100548{
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200549 struct ahci_port_s *port = data;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100550 int rc;
551
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200552 dprintf(2, "AHCI/%d: probing\n", port->pnr);
553 ahci_port_reset(port->ctrl, port->pnr);
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500554 rc = ahci_port_setup(port);
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200555 if (rc < 0)
556 ahci_port_release(port);
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200557 else {
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200558 port = ahci_port_realloc(port);
Gerd Hoffmannce12eaf2013-09-09 16:33:06 +0200559 if (port == NULL)
560 return;
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200561 dprintf(1, "AHCI/%d: registering: \"%s\"\n", port->pnr, port->desc);
562 if (!port->atapi) {
563 // Register with bcv system.
564 boot_add_hd(&port->drive, port->desc, port->prio);
565 } else {
566 // fill cdidmap
567 boot_add_cd(&port->drive, port->desc, port->prio);
568 }
569 }
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100570}
571
572// Initialize an ata controller and detect its drives.
573static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500574ahci_controller_setup(struct pci_device *pci)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100575{
576 struct ahci_ctrl_s *ctrl = malloc_fseg(sizeof(*ctrl));
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200577 struct ahci_port_s *port;
Gerd Hoffmann9c869922011-07-14 16:24:05 +0200578 u16 bdf = pci->bdf;
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200579 u32 val, pnr, max;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100580
581 if (!ctrl) {
582 warn_noalloc();
583 return;
584 }
Scott Duplichan9c48aab2011-07-14 16:24:02 +0200585
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500586 if (create_bounce_buf() < 0) {
Scott Duplichan9c48aab2011-07-14 16:24:02 +0200587 warn_noalloc();
Gerd Hoffmannd7a7cf32011-08-04 19:36:27 +0200588 free(ctrl);
Scott Duplichan9c48aab2011-07-14 16:24:02 +0200589 return;
590 }
591
Gerd Hoffmann9c869922011-07-14 16:24:05 +0200592 ctrl->pci_tmp = pci;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100593 ctrl->pci_bdf = bdf;
594 ctrl->iobase = pci_config_readl(bdf, PCI_BASE_ADDRESS_5);
595 ctrl->irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
596 dprintf(1, "AHCI controller at %02x.%x, iobase %x, irq %d\n",
597 bdf >> 3, bdf & 7, ctrl->iobase, ctrl->irq);
598
Kevin O'Connor7eb02222010-12-12 14:01:47 -0500599 pci_config_maskw(bdf, PCI_COMMAND, 0,
600 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
601
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100602 val = ahci_ctrl_readl(ctrl, HOST_CTL);
603 ahci_ctrl_writel(ctrl, HOST_CTL, val | HOST_CTL_AHCI_EN);
604
605 ctrl->caps = ahci_ctrl_readl(ctrl, HOST_CAP);
606 ctrl->ports = ahci_ctrl_readl(ctrl, HOST_PORTS_IMPL);
607 dprintf(2, "AHCI: cap 0x%x, ports_impl 0x%x\n",
608 ctrl->caps, ctrl->ports);
609
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200610 max = ctrl->caps & 0x1f;
611 for (pnr = 0; pnr <= max; pnr++) {
612 if (!(ctrl->ports & (1 << pnr)))
613 continue;
614 port = ahci_port_alloc(ctrl, pnr);
615 if (port == NULL)
616 continue;
617 run_thread(ahci_port_detect, port);
618 }
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100619}
620
621// Locate and init ahci controllers.
622static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500623ahci_scan(void)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100624{
625 // Scan PCI bus for ATA adapters
Kevin O'Connor9cb49922011-06-20 22:22:42 -0400626 struct pci_device *pci;
627 foreachpci(pci) {
628 if (pci->class != PCI_CLASS_STORAGE_SATA)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100629 continue;
Kevin O'Connor9cb49922011-06-20 22:22:42 -0400630 if (pci->prog_if != 1 /* AHCI rev 1 */)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100631 continue;
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500632 ahci_controller_setup(pci);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100633 }
634}
635
636void
637ahci_setup(void)
638{
639 ASSERT32FLAT();
640 if (!CONFIG_AHCI)
641 return;
642
643 dprintf(3, "init ahci\n");
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500644 ahci_scan();
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100645}