blob: bd21bf648dc3460487efd4ba81bdfbb9beb9d436 [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
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040011#include "malloc.h" // free
12#include "output.h" // dprintf
Kevin O'Connor9cb49922011-06-20 22:22:42 -040013#include "pci.h" // foreachpci
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010014#include "pci_ids.h" // PCI_CLASS_STORAGE_OTHER
15#include "pci_regs.h" // PCI_INTERRUPT_LINE
Kevin O'Connor3df600b2013-09-14 19:28:55 -040016#include "stacks.h" // yield
Kevin O'Connor135f3f62013-09-14 23:57:26 -040017#include "std/disk.h" // DISK_RET_SUCCESS
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040018#include "string.h" // memset
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040019#include "util.h" // timer_calc
Kevin O'Connor4ade5232013-09-18 21:41:48 -040020#include "x86.h" // inb
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010021
Gerd Hoffmanne1041192011-07-14 16:24:04 +020022#define AHCI_REQUEST_TIMEOUT 32000 // 32 seconds max for IDE ops
23#define AHCI_RESET_TIMEOUT 500 // 500 miliseconds
24#define AHCI_LINK_TIMEOUT 10 // 10 miliseconds
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010025
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010026// prepare sata command fis
27static void sata_prep_simple(struct sata_cmd_fis *fis, u8 command)
28{
29 memset_fl(fis, 0, sizeof(*fis));
Gerd Hoffmann26ae90b2013-11-26 14:01:20 +010030 fis->command = command;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010031}
32
33static 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)) {
Gerd Hoffmann26ae90b2013-11-26 14:01:20 +010042 fis->sector_count2 = op->count >> 8;
43 fis->lba_low2 = lba >> 24;
44 fis->lba_mid2 = lba >> 32;
45 fis->lba_high2 = lba >> 40;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010046 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 }
Gerd Hoffmann26ae90b2013-11-26 14:01:20 +010053 fis->feature = 1; /* dma */
54 fis->command = command;
55 fis->sector_count = op->count;
56 fis->lba_low = lba;
57 fis->lba_mid = lba >> 8;
58 fis->lba_high = lba >> 16;
59 fis->device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010060}
61
62static void sata_prep_atapi(struct sata_cmd_fis *fis, u16 blocksize)
63{
64 memset_fl(fis, 0, sizeof(*fis));
Gerd Hoffmann26ae90b2013-11-26 14:01:20 +010065 fis->command = ATA_CMD_PACKET;
66 fis->feature = 1; /* dma */
67 fis->lba_mid = blocksize;
68 fis->lba_high = blocksize >> 8;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010069}
70
71// ahci register access helpers
72static u32 ahci_ctrl_readl(struct ahci_ctrl_s *ctrl, u32 reg)
73{
Kevin O'Connor6e3436b2016-02-02 22:11:30 -050074 return readl(ctrl->iobase + reg);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010075}
76
77static void ahci_ctrl_writel(struct ahci_ctrl_s *ctrl, u32 reg, u32 val)
78{
Kevin O'Connor6e3436b2016-02-02 22:11:30 -050079 writel(ctrl->iobase + reg, val);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010080}
81
82static 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
90static 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
96static 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
Kevin O'Connor1902c942013-10-26 11:48:06 -0400103static int ahci_command(struct ahci_port_s *port_gf, int iswrite, int isatapi,
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100104 void *buffer, u32 bsize)
105{
Gerd Hoffmann6f850492011-07-14 16:24:01 +0200106 u32 val, status, success, flags, intbits, error;
Gerd Hoffmann26ae90b2013-11-26 14:01:20 +0100107 struct ahci_ctrl_s *ctrl = port_gf->ctrl;
108 struct ahci_cmd_s *cmd = port_gf->cmd;
109 struct ahci_fis_s *fis = port_gf->fis;
110 struct ahci_list_s *list = port_gf->list;
111 u32 pnr = port_gf->pnr;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100112
Gerd Hoffmann26ae90b2013-11-26 14:01:20 +0100113 cmd->fis.reg = 0x27;
Gerd Hoffmann4a0f3662013-11-26 14:02:54 +0100114 cmd->fis.pmp_type = 1 << 7; /* cmd fis */
115 cmd->prdt[0].base = (u32)buffer;
Gerd Hoffmann26ae90b2013-11-26 14:01:20 +0100116 cmd->prdt[0].baseu = 0;
117 cmd->prdt[0].flags = bsize-1;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100118
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100119 flags = ((1 << 16) | /* one prd entry */
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100120 (iswrite ? (1 << 6) : 0) |
121 (isatapi ? (1 << 5) : 0) |
Gerd Hoffmanna8c6a4e2011-07-14 16:23:59 +0200122 (5 << 0)); /* fis length (dwords) */
Gerd Hoffmann26ae90b2013-11-26 14:01:20 +0100123 list[0].flags = flags;
124 list[0].bytes = 0;
Gerd Hoffmann4a0f3662013-11-26 14:02:54 +0100125 list[0].base = (u32)(cmd);
Gerd Hoffmann26ae90b2013-11-26 14:01:20 +0100126 list[0].baseu = 0;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100127
Kevin O'Connorbf079e12012-03-11 11:19:52 -0400128 dprintf(8, "AHCI/%d: send cmd ...\n", pnr);
Gerd Hoffmann07532972011-07-14 16:24:00 +0200129 intbits = ahci_port_readl(ctrl, pnr, PORT_IRQ_STAT);
130 if (intbits)
131 ahci_port_writel(ctrl, pnr, PORT_IRQ_STAT, intbits);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100132 ahci_port_writel(ctrl, pnr, PORT_SCR_ACT, 1);
133 ahci_port_writel(ctrl, pnr, PORT_CMD_ISSUE, 1);
Gerd Hoffmann07532972011-07-14 16:24:00 +0200134
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400135 u32 end = timer_calc(AHCI_REQUEST_TIMEOUT);
Gerd Hoffmann07532972011-07-14 16:24:00 +0200136 do {
137 for (;;) {
138 intbits = ahci_port_readl(ctrl, pnr, PORT_IRQ_STAT);
139 if (intbits) {
140 ahci_port_writel(ctrl, pnr, PORT_IRQ_STAT, intbits);
141 if (intbits & 0x02) {
Kevin O'Connor890c0852012-05-24 23:55:00 -0400142 status = GET_LOWFLAT(fis->psfis[2]);
143 error = GET_LOWFLAT(fis->psfis[3]);
Gerd Hoffmann07532972011-07-14 16:24:00 +0200144 break;
145 }
146 if (intbits & 0x01) {
Kevin O'Connor890c0852012-05-24 23:55:00 -0400147 status = GET_LOWFLAT(fis->rfis[2]);
148 error = GET_LOWFLAT(fis->rfis[3]);
Gerd Hoffmann07532972011-07-14 16:24:00 +0200149 break;
150 }
151 }
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400152 if (timer_check(end)) {
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200153 warn_timeout();
154 return -1;
155 }
Gerd Hoffmann07532972011-07-14 16:24:00 +0200156 yield();
157 }
Kevin O'Connorbf079e12012-03-11 11:19:52 -0400158 dprintf(8, "AHCI/%d: ... intbits 0x%x, status 0x%x ...\n",
Gerd Hoffmann07532972011-07-14 16:24:00 +0200159 pnr, intbits, status);
160 } while (status & ATA_CB_STAT_BSY);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100161
162 success = (0x00 == (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_DF |
Gerd Hoffmanncbda7952011-07-14 16:24:03 +0200163 ATA_CB_STAT_ERR)) &&
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100164 ATA_CB_STAT_RDY == (status & (ATA_CB_STAT_RDY)));
Gerd Hoffmann6f850492011-07-14 16:24:01 +0200165 if (success) {
Kevin O'Connorbf079e12012-03-11 11:19:52 -0400166 dprintf(8, "AHCI/%d: ... finished, status 0x%x, OK\n", pnr,
Gerd Hoffmann6f850492011-07-14 16:24:01 +0200167 status);
168 } else {
169 dprintf(2, "AHCI/%d: ... finished, status 0x%x, ERROR 0x%x\n", pnr,
170 status, error);
171
172 // non-queued error recovery (AHCI 1.3 section 6.2.2.1)
173 // Clears PxCMD.ST to 0 to reset the PxCI register
174 val = ahci_port_readl(ctrl, pnr, PORT_CMD);
175 ahci_port_writel(ctrl, pnr, PORT_CMD, val & ~PORT_CMD_START);
176
177 // waits for PxCMD.CR to clear to 0
178 while (1) {
179 val = ahci_port_readl(ctrl, pnr, PORT_CMD);
180 if ((val & PORT_CMD_LIST_ON) == 0)
181 break;
182 yield();
183 }
184
185 // Clears any error bits in PxSERR to enable capturing new errors
186 val = ahci_port_readl(ctrl, pnr, PORT_SCR_ERR);
187 ahci_port_writel(ctrl, pnr, PORT_SCR_ERR, val);
188
189 // Clears status bits in PxIS as appropriate
190 val = ahci_port_readl(ctrl, pnr, PORT_IRQ_STAT);
191 ahci_port_writel(ctrl, pnr, PORT_IRQ_STAT, val);
192
193 // If PxTFD.STS.BSY or PxTFD.STS.DRQ is set to 1, issue
194 // a COMRESET to the device to put it in an idle state
195 val = ahci_port_readl(ctrl, pnr, PORT_TFDATA);
196 if (val & (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ)) {
197 dprintf(2, "AHCI/%d: issue comreset\n", pnr);
198 val = ahci_port_readl(ctrl, pnr, PORT_SCR_CTL);
199 // set Device Detection Initialization (DET) to 1 for 1 ms for comreset
200 ahci_port_writel(ctrl, pnr, PORT_SCR_CTL, val | 1);
201 mdelay (1);
202 ahci_port_writel(ctrl, pnr, PORT_SCR_CTL, val);
203 }
204
205 // Sets PxCMD.ST to 1 to enable issuing new commands
206 val = ahci_port_readl(ctrl, pnr, PORT_CMD);
207 ahci_port_writel(ctrl, pnr, PORT_CMD, val | PORT_CMD_START);
208 }
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100209 return success ? 0 : -1;
210}
211
212#define CDROM_CDB_SIZE 12
213
Kevin O'Connor0e5c7702015-07-07 11:24:27 -0400214int ahci_atapi_process_op(struct disk_op_s *op)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100215{
Kevin O'Connor80c2b6e2010-12-05 12:52:02 -0500216 if (! CONFIG_AHCI)
217 return 0;
218
Kevin O'Connor1902c942013-10-26 11:48:06 -0400219 struct ahci_port_s *port_gf = container_of(
220 op->drive_gf, struct ahci_port_s, drive);
Gerd Hoffmann26ae90b2013-11-26 14:01:20 +0100221 struct ahci_cmd_s *cmd = port_gf->cmd;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100222
Kevin O'Connor0e5c7702015-07-07 11:24:27 -0400223 if (op->command == CMD_WRITE || op->command == CMD_FORMAT)
224 return DISK_RET_EWRITEPROTECT;
225 int blocksize = scsi_fill_cmd(op, cmd->atapi, CDROM_CDB_SIZE);
226 if (blocksize < 0)
227 return default_process_op(op);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100228 sata_prep_atapi(&cmd->fis, blocksize);
Kevin O'Connor0e5c7702015-07-07 11:24:27 -0400229 int rc = ahci_command(port_gf, 0, 1, op->buf_fl, op->count * blocksize);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100230 if (rc < 0)
231 return DISK_RET_EBADTRACK;
232 return DISK_RET_SUCCESS;
233}
234
Scott Duplichan9c48aab2011-07-14 16:24:02 +0200235// read/write count blocks from a harddrive, op->buf_fl must be word aligned
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100236static int
Scott Duplichan9c48aab2011-07-14 16:24:02 +0200237ahci_disk_readwrite_aligned(struct disk_op_s *op, int iswrite)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100238{
Kevin O'Connor1902c942013-10-26 11:48:06 -0400239 struct ahci_port_s *port_gf = container_of(
240 op->drive_gf, struct ahci_port_s, drive);
Gerd Hoffmann26ae90b2013-11-26 14:01:20 +0100241 struct ahci_cmd_s *cmd = port_gf->cmd;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100242 int rc;
243
244 sata_prep_readwrite(&cmd->fis, op, iswrite);
Kevin O'Connor1902c942013-10-26 11:48:06 -0400245 rc = ahci_command(port_gf, iswrite, 0, op->buf_fl,
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100246 op->count * DISK_SECTOR_SIZE);
Kevin O'Connorbf079e12012-03-11 11:19:52 -0400247 dprintf(8, "ahci disk %s, lba %6x, count %3x, buf %p, rc %d\n",
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100248 iswrite ? "write" : "read", (u32)op->lba, op->count, op->buf_fl, rc);
249 if (rc < 0)
250 return DISK_RET_EBADTRACK;
251 return DISK_RET_SUCCESS;
252}
253
Scott Duplichan9c48aab2011-07-14 16:24:02 +0200254// read/write count blocks from a harddrive.
255static int
256ahci_disk_readwrite(struct disk_op_s *op, int iswrite)
257{
258 // if caller's buffer is word aligned, use it directly
259 if (((u32) op->buf_fl & 1) == 0)
260 return ahci_disk_readwrite_aligned(op, iswrite);
261
262 // Use a word aligned buffer for AHCI I/O
263 int rc;
264 struct disk_op_s localop = *op;
Gerd Hoffmann26ae90b2013-11-26 14:01:20 +0100265 u8 *alignedbuf_fl = bounce_buf_fl;
Scott Duplichan9c48aab2011-07-14 16:24:02 +0200266 u8 *position = op->buf_fl;
267
268 localop.buf_fl = alignedbuf_fl;
269 localop.count = 1;
270
271 if (iswrite) {
272 u16 block;
273 for (block = 0; block < op->count; block++) {
274 memcpy_fl (alignedbuf_fl, position, DISK_SECTOR_SIZE);
275 rc = ahci_disk_readwrite_aligned (&localop, 1);
276 if (rc)
277 return rc;
278 position += DISK_SECTOR_SIZE;
279 localop.lba++;
280 }
281 } else { // read
282 u16 block;
283 for (block = 0; block < op->count; block++) {
284 rc = ahci_disk_readwrite_aligned (&localop, 0);
285 if (rc)
286 return rc;
287 memcpy_fl (position, alignedbuf_fl, DISK_SECTOR_SIZE);
288 position += DISK_SECTOR_SIZE;
289 localop.lba++;
290 }
291 }
292 return DISK_RET_SUCCESS;
293}
294
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100295// command demuxer
Kevin O'Connorc7fa7892015-07-07 08:35:51 -0400296int
Kevin O'Connor17856452015-07-07 14:56:20 -0400297ahci_process_op(struct disk_op_s *op)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100298{
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100299 if (!CONFIG_AHCI)
300 return 0;
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400301 switch (op->command) {
302 case CMD_READ:
303 return ahci_disk_readwrite(op, 0);
304 case CMD_WRITE:
305 return ahci_disk_readwrite(op, 1);
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400306 default:
Kevin O'Connor85c72c62015-07-07 09:01:52 -0400307 return default_process_op(op);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100308 }
309}
310
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100311static void
312ahci_port_reset(struct ahci_ctrl_s *ctrl, u32 pnr)
313{
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200314 u32 val;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100315
316 /* disable FIS + CMD */
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400317 u32 end = timer_calc(AHCI_RESET_TIMEOUT);
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200318 for (;;) {
319 val = ahci_port_readl(ctrl, pnr, PORT_CMD);
320 if (!(val & (PORT_CMD_FIS_RX | PORT_CMD_START |
321 PORT_CMD_FIS_ON | PORT_CMD_LIST_ON)))
322 break;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100323 val &= ~(PORT_CMD_FIS_RX | PORT_CMD_START);
324 ahci_port_writel(ctrl, pnr, PORT_CMD, val);
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400325 if (timer_check(end)) {
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200326 warn_timeout();
327 break;
328 }
329 yield();
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100330 }
331
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100332 /* disable + clear IRQs */
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200333 ahci_port_writel(ctrl, pnr, PORT_IRQ_MASK, 0);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100334 val = ahci_port_readl(ctrl, pnr, PORT_IRQ_STAT);
335 if (val)
336 ahci_port_writel(ctrl, pnr, PORT_IRQ_STAT, val);
337}
338
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100339static struct ahci_port_s*
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200340ahci_port_alloc(struct ahci_ctrl_s *ctrl, u32 pnr)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100341{
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200342 struct ahci_port_s *port = malloc_tmp(sizeof(*port));
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100343
344 if (!port) {
345 warn_noalloc();
346 return NULL;
347 }
348 port->pnr = pnr;
349 port->ctrl = ctrl;
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200350 port->list = memalign_tmp(1024, 1024);
351 port->fis = memalign_tmp(256, 256);
352 port->cmd = memalign_tmp(256, 256);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100353 if (port->list == NULL || port->fis == NULL || port->cmd == NULL) {
354 warn_noalloc();
355 return NULL;
356 }
357 memset(port->list, 0, 1024);
358 memset(port->fis, 0, 256);
359 memset(port->cmd, 0, 256);
360
361 ahci_port_writel(ctrl, pnr, PORT_LST_ADDR, (u32)port->list);
362 ahci_port_writel(ctrl, pnr, PORT_FIS_ADDR, (u32)port->fis);
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200363 return port;
364}
365
Gerd Hoffmannce12eaf2013-09-09 16:33:06 +0200366static void ahci_port_release(struct ahci_port_s *port)
367{
368 ahci_port_reset(port->ctrl, port->pnr);
369 free(port->list);
370 free(port->fis);
371 free(port->cmd);
372 free(port);
373}
374
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200375static struct ahci_port_s* ahci_port_realloc(struct ahci_port_s *port)
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200376{
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200377 struct ahci_port_s *tmp;
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200378 u32 cmd;
379
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200380 tmp = malloc_fseg(sizeof(*port));
Gerd Hoffmannce12eaf2013-09-09 16:33:06 +0200381 if (!tmp) {
382 warn_noalloc();
383 ahci_port_release(port);
384 return NULL;
385 }
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200386 *tmp = *port;
387 free(port);
388 port = tmp;
389
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200390 ahci_port_reset(port->ctrl, port->pnr);
391
392 free(port->list);
393 free(port->fis);
394 free(port->cmd);
Gerd Hoffmann3ecdc492013-11-26 14:10:25 +0100395 port->list = memalign_high(1024, 1024);
396 port->fis = memalign_high(256, 256);
397 port->cmd = memalign_high(256, 256);
Kevin O'Connor3abdc7c2015-06-30 11:10:41 -0400398 if (!port->list || !port->fis || !port->cmd) {
399 warn_noalloc();
400 free(port->list);
401 free(port->fis);
402 free(port->cmd);
403 free(port);
404 return NULL;
405 }
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200406
407 ahci_port_writel(port->ctrl, port->pnr, PORT_LST_ADDR, (u32)port->list);
408 ahci_port_writel(port->ctrl, port->pnr, PORT_FIS_ADDR, (u32)port->fis);
409
410 cmd = ahci_port_readl(port->ctrl, port->pnr, PORT_CMD);
411 cmd |= (PORT_CMD_FIS_RX|PORT_CMD_START);
412 ahci_port_writel(port->ctrl, port->pnr, PORT_CMD, cmd);
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200413
414 return port;
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200415}
416
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200417#define MAXMODEL 40
418
419/* See ahci spec chapter 10.1 "Software Initialization of HBA" */
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500420static int ahci_port_setup(struct ahci_port_s *port)
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200421{
422 struct ahci_ctrl_s *ctrl = port->ctrl;
423 u32 pnr = port->pnr;
424 char model[MAXMODEL+1];
425 u16 buffer[256];
426 u32 cmd, stat, err, tf;
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200427 int rc;
428
429 /* enable FIS recv */
430 cmd = ahci_port_readl(ctrl, pnr, PORT_CMD);
431 cmd |= PORT_CMD_FIS_RX;
432 ahci_port_writel(ctrl, pnr, PORT_CMD, cmd);
433
434 /* spin up */
435 cmd |= PORT_CMD_SPIN_UP;
436 ahci_port_writel(ctrl, pnr, PORT_CMD, cmd);
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400437 u32 end = timer_calc(AHCI_LINK_TIMEOUT);
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200438 for (;;) {
439 stat = ahci_port_readl(ctrl, pnr, PORT_SCR_STAT);
440 if ((stat & 0x07) == 0x03) {
Kevin O'Connorbf079e12012-03-11 11:19:52 -0400441 dprintf(2, "AHCI/%d: link up\n", port->pnr);
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200442 break;
443 }
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400444 if (timer_check(end)) {
Kevin O'Connorbf079e12012-03-11 11:19:52 -0400445 dprintf(2, "AHCI/%d: link down\n", port->pnr);
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200446 return -1;
447 }
448 yield();
449 }
450
451 /* clear error status */
452 err = ahci_port_readl(ctrl, pnr, PORT_SCR_ERR);
453 if (err)
454 ahci_port_writel(ctrl, pnr, PORT_SCR_ERR, err);
455
456 /* wait for device becoming ready */
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400457 end = timer_calc(AHCI_REQUEST_TIMEOUT);
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200458 for (;;) {
459 tf = ahci_port_readl(ctrl, pnr, PORT_TFDATA);
460 if (!(tf & (ATA_CB_STAT_BSY |
461 ATA_CB_STAT_DRQ)))
462 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400463 if (timer_check(end)) {
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200464 warn_timeout();
465 dprintf(1, "AHCI/%d: device not ready (tf 0x%x)\n", port->pnr, tf);
466 return -1;
467 }
468 yield();
469 }
470
471 /* start device */
472 cmd |= PORT_CMD_START;
473 ahci_port_writel(ctrl, pnr, PORT_CMD, cmd);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100474
475 sata_prep_simple(&port->cmd->fis, ATA_CMD_IDENTIFY_PACKET_DEVICE);
476 rc = ahci_command(port, 0, 0, buffer, sizeof(buffer));
477 if (rc == 0) {
478 port->atapi = 1;
479 } else {
480 port->atapi = 0;
481 sata_prep_simple(&port->cmd->fis, ATA_CMD_IDENTIFY_DEVICE);
482 rc = ahci_command(port, 0, 0, buffer, sizeof(buffer));
483 if (rc < 0)
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200484 return -1;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100485 }
486
Gerd Hoffmann0e6f6362010-12-09 08:39:48 +0100487 port->drive.cntl_id = pnr;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100488 port->drive.removable = (buffer[0] & 0x80) ? 1 : 0;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100489
490 if (!port->atapi) {
491 // found disk (ata)
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400492 port->drive.type = DTYPE_AHCI;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100493 port->drive.blksize = DISK_SECTOR_SIZE;
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400494 port->drive.pchs.cylinder = buffer[1];
495 port->drive.pchs.head = buffer[3];
496 port->drive.pchs.sector = buffer[6];
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100497
498 u64 sectors;
499 if (buffer[83] & (1 << 10)) // word 83 - lba48 support
500 sectors = *(u64*)&buffer[100]; // word 100-103
501 else
502 sectors = *(u32*)&buffer[60]; // word 60 and word 61
503 port->drive.sectors = sectors;
504 u64 adjsize = sectors >> 11;
505 char adjprefix = 'M';
506 if (adjsize >= (1 << 16)) {
507 adjsize >>= 10;
508 adjprefix = 'G';
509 }
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200510 port->desc = znprintf(MAXDESCSIZE
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500511 , "AHCI/%d: %s ATA-%d Hard-Disk (%u %ciBytes)"
512 , port->pnr
513 , ata_extract_model(model, MAXMODEL, buffer)
514 , ata_extract_version(buffer)
515 , (u32)adjsize, adjprefix);
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200516 port->prio = bootprio_find_ata_device(ctrl->pci_tmp, pnr, 0);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100517 } else {
518 // found cdrom (atapi)
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400519 port->drive.type = DTYPE_AHCI_ATAPI;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100520 port->drive.blksize = CDROM_SECTOR_SIZE;
521 port->drive.sectors = (u64)-1;
522 u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
Gerd Hoffmann263ea2f2011-08-04 19:36:29 +0200523 if (!iscd) {
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400524 dprintf(1, "AHCI/%d: atapi device isn't a cdrom\n", port->pnr);
Gerd Hoffmann263ea2f2011-08-04 19:36:29 +0200525 return -1;
526 }
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200527 port->desc = znprintf(MAXDESCSIZE
Gerd Hoffmann263ea2f2011-08-04 19:36:29 +0200528 , "DVD/CD [AHCI/%d: %s ATAPI-%d DVD/CD]"
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500529 , port->pnr
530 , ata_extract_model(model, MAXMODEL, buffer)
Gerd Hoffmann263ea2f2011-08-04 19:36:29 +0200531 , ata_extract_version(buffer));
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200532 port->prio = bootprio_find_ata_device(ctrl->pci_tmp, pnr, 0);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100533 }
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200534 return 0;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100535}
536
537// Detect any drives attached to a given controller.
538static void
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200539ahci_port_detect(void *data)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100540{
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200541 struct ahci_port_s *port = data;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100542 int rc;
543
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200544 dprintf(2, "AHCI/%d: probing\n", port->pnr);
545 ahci_port_reset(port->ctrl, port->pnr);
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500546 rc = ahci_port_setup(port);
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200547 if (rc < 0)
548 ahci_port_release(port);
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200549 else {
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200550 port = ahci_port_realloc(port);
Gerd Hoffmannce12eaf2013-09-09 16:33:06 +0200551 if (port == NULL)
552 return;
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200553 dprintf(1, "AHCI/%d: registering: \"%s\"\n", port->pnr, port->desc);
554 if (!port->atapi) {
555 // Register with bcv system.
556 boot_add_hd(&port->drive, port->desc, port->prio);
557 } else {
558 // fill cdidmap
559 boot_add_cd(&port->drive, port->desc, port->prio);
560 }
561 }
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100562}
563
564// Initialize an ata controller and detect its drives.
565static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500566ahci_controller_setup(struct pci_device *pci)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100567{
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200568 struct ahci_port_s *port;
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200569 u32 val, pnr, max;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100570
Kevin O'Connor6e3436b2016-02-02 22:11:30 -0500571 if (create_bounce_buf() < 0)
572 return;
573
574 void *iobase = pci_enable_membar(pci, PCI_BASE_ADDRESS_5);
575 if (!iobase)
576 return;
577
578 struct ahci_ctrl_s *ctrl = malloc_fseg(sizeof(*ctrl));
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100579 if (!ctrl) {
580 warn_noalloc();
581 return;
582 }
Scott Duplichan9c48aab2011-07-14 16:24:02 +0200583
Gerd Hoffmann9c869922011-07-14 16:24:05 +0200584 ctrl->pci_tmp = pci;
Kevin O'Connor6e3436b2016-02-02 22:11:30 -0500585 ctrl->iobase = iobase;
586 ctrl->irq = pci_config_readb(pci->bdf, PCI_INTERRUPT_LINE);
Kevin O'Connor7b673002016-02-03 03:03:15 -0500587 dprintf(1, "AHCI controller at %pP, iobase %p, irq %d\n"
588 , pci, ctrl->iobase, ctrl->irq);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100589
Kevin O'Connor6e3436b2016-02-02 22:11:30 -0500590 pci_enable_busmaster(pci);
Kevin O'Connor7eb02222010-12-12 14:01:47 -0500591
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100592 val = ahci_ctrl_readl(ctrl, HOST_CTL);
593 ahci_ctrl_writel(ctrl, HOST_CTL, val | HOST_CTL_AHCI_EN);
594
595 ctrl->caps = ahci_ctrl_readl(ctrl, HOST_CAP);
596 ctrl->ports = ahci_ctrl_readl(ctrl, HOST_PORTS_IMPL);
597 dprintf(2, "AHCI: cap 0x%x, ports_impl 0x%x\n",
598 ctrl->caps, ctrl->ports);
599
Vladimir Serbinenko40dfc0e2015-05-18 15:00:31 +0200600 max = 0x1f;
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200601 for (pnr = 0; pnr <= max; pnr++) {
602 if (!(ctrl->ports & (1 << pnr)))
603 continue;
604 port = ahci_port_alloc(ctrl, pnr);
605 if (port == NULL)
606 continue;
607 run_thread(ahci_port_detect, port);
608 }
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100609}
610
611// Locate and init ahci controllers.
612static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500613ahci_scan(void)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100614{
615 // Scan PCI bus for ATA adapters
Kevin O'Connor9cb49922011-06-20 22:22:42 -0400616 struct pci_device *pci;
617 foreachpci(pci) {
618 if (pci->class != PCI_CLASS_STORAGE_SATA)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100619 continue;
Kevin O'Connor9cb49922011-06-20 22:22:42 -0400620 if (pci->prog_if != 1 /* AHCI rev 1 */)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100621 continue;
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500622 ahci_controller_setup(pci);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100623 }
624}
625
626void
627ahci_setup(void)
628{
629 ASSERT32FLAT();
630 if (!CONFIG_AHCI)
631 return;
632
633 dprintf(3, "init ahci\n");
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500634 ahci_scan();
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100635}