blob: 45dc6613991664ab956fe5aa822b2255e8836d4a [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'Connor4d8510c2016-02-03 01:28:20 -050013#include "pci.h" // pci_config_readb
14#include "pcidevice.h" // foreachpci
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010015#include "pci_ids.h" // PCI_CLASS_STORAGE_OTHER
16#include "pci_regs.h" // PCI_INTERRUPT_LINE
Kevin O'Connor3df600b2013-09-14 19:28:55 -040017#include "stacks.h" // yield
Kevin O'Connor135f3f62013-09-14 23:57:26 -040018#include "std/disk.h" // DISK_RET_SUCCESS
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040019#include "string.h" // memset
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040020#include "util.h" // timer_calc
Kevin O'Connor4ade5232013-09-18 21:41:48 -040021#include "x86.h" // inb
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
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010027// prepare sata command fis
28static void sata_prep_simple(struct sata_cmd_fis *fis, u8 command)
29{
30 memset_fl(fis, 0, sizeof(*fis));
Gerd Hoffmann26ae90b2013-11-26 14:01:20 +010031 fis->command = command;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010032}
33
34static void sata_prep_readwrite(struct sata_cmd_fis *fis,
35 struct disk_op_s *op, int iswrite)
36{
37 u64 lba = op->lba;
38 u8 command;
39
40 memset_fl(fis, 0, sizeof(*fis));
41
42 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
Gerd Hoffmann26ae90b2013-11-26 14:01:20 +010043 fis->sector_count2 = op->count >> 8;
44 fis->lba_low2 = lba >> 24;
45 fis->lba_mid2 = lba >> 32;
46 fis->lba_high2 = lba >> 40;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010047 lba &= 0xffffff;
48 command = (iswrite ? ATA_CMD_WRITE_DMA_EXT
49 : ATA_CMD_READ_DMA_EXT);
50 } else {
51 command = (iswrite ? ATA_CMD_WRITE_DMA
52 : ATA_CMD_READ_DMA);
53 }
Gerd Hoffmann26ae90b2013-11-26 14:01:20 +010054 fis->feature = 1; /* dma */
55 fis->command = command;
56 fis->sector_count = op->count;
57 fis->lba_low = lba;
58 fis->lba_mid = lba >> 8;
59 fis->lba_high = lba >> 16;
60 fis->device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010061}
62
63static void sata_prep_atapi(struct sata_cmd_fis *fis, u16 blocksize)
64{
65 memset_fl(fis, 0, sizeof(*fis));
Gerd Hoffmann26ae90b2013-11-26 14:01:20 +010066 fis->command = ATA_CMD_PACKET;
67 fis->feature = 1; /* dma */
68 fis->lba_mid = blocksize;
69 fis->lba_high = blocksize >> 8;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010070}
71
72// ahci register access helpers
73static u32 ahci_ctrl_readl(struct ahci_ctrl_s *ctrl, u32 reg)
74{
Kevin O'Connor6e3436b2016-02-02 22:11:30 -050075 return readl(ctrl->iobase + reg);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010076}
77
78static void ahci_ctrl_writel(struct ahci_ctrl_s *ctrl, u32 reg, u32 val)
79{
Kevin O'Connor6e3436b2016-02-02 22:11:30 -050080 writel(ctrl->iobase + reg, val);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010081}
82
83static u32 ahci_port_to_ctrl(u32 pnr, u32 port_reg)
84{
85 u32 ctrl_reg = 0x100;
86 ctrl_reg += pnr * 0x80;
87 ctrl_reg += port_reg;
88 return ctrl_reg;
89}
90
91static u32 ahci_port_readl(struct ahci_ctrl_s *ctrl, u32 pnr, u32 reg)
92{
93 u32 ctrl_reg = ahci_port_to_ctrl(pnr, reg);
94 return ahci_ctrl_readl(ctrl, ctrl_reg);
95}
96
97static void ahci_port_writel(struct ahci_ctrl_s *ctrl, u32 pnr, u32 reg, u32 val)
98{
99 u32 ctrl_reg = ahci_port_to_ctrl(pnr, reg);
100 ahci_ctrl_writel(ctrl, ctrl_reg, val);
101}
102
103// submit ahci command + wait for result
Kevin O'Connor1902c942013-10-26 11:48:06 -0400104static int ahci_command(struct ahci_port_s *port_gf, int iswrite, int isatapi,
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100105 void *buffer, u32 bsize)
106{
Gerd Hoffmann6f850492011-07-14 16:24:01 +0200107 u32 val, status, success, flags, intbits, error;
Gerd Hoffmann26ae90b2013-11-26 14:01:20 +0100108 struct ahci_ctrl_s *ctrl = port_gf->ctrl;
109 struct ahci_cmd_s *cmd = port_gf->cmd;
110 struct ahci_fis_s *fis = port_gf->fis;
111 struct ahci_list_s *list = port_gf->list;
112 u32 pnr = port_gf->pnr;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100113
Gerd Hoffmann26ae90b2013-11-26 14:01:20 +0100114 cmd->fis.reg = 0x27;
Gerd Hoffmann4a0f3662013-11-26 14:02:54 +0100115 cmd->fis.pmp_type = 1 << 7; /* cmd fis */
116 cmd->prdt[0].base = (u32)buffer;
Gerd Hoffmann26ae90b2013-11-26 14:01:20 +0100117 cmd->prdt[0].baseu = 0;
118 cmd->prdt[0].flags = bsize-1;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100119
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100120 flags = ((1 << 16) | /* one prd entry */
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100121 (iswrite ? (1 << 6) : 0) |
122 (isatapi ? (1 << 5) : 0) |
Gerd Hoffmanna8c6a4e2011-07-14 16:23:59 +0200123 (5 << 0)); /* fis length (dwords) */
Gerd Hoffmann26ae90b2013-11-26 14:01:20 +0100124 list[0].flags = flags;
125 list[0].bytes = 0;
Gerd Hoffmann4a0f3662013-11-26 14:02:54 +0100126 list[0].base = (u32)(cmd);
Gerd Hoffmann26ae90b2013-11-26 14:01:20 +0100127 list[0].baseu = 0;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100128
Kevin O'Connorbf079e12012-03-11 11:19:52 -0400129 dprintf(8, "AHCI/%d: send cmd ...\n", pnr);
Gerd Hoffmann07532972011-07-14 16:24:00 +0200130 intbits = ahci_port_readl(ctrl, pnr, PORT_IRQ_STAT);
131 if (intbits)
132 ahci_port_writel(ctrl, pnr, PORT_IRQ_STAT, intbits);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100133 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(
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400220 op->drive_fl, 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(
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400240 op->drive_fl, 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 }
Gerd Hoffmann3bdd2b72019-11-13 10:13:02 +0100348 memset(port, 0, sizeof(*port));
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100349 port->pnr = pnr;
350 port->ctrl = ctrl;
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200351 port->list = memalign_tmp(1024, 1024);
352 port->fis = memalign_tmp(256, 256);
353 port->cmd = memalign_tmp(256, 256);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100354 if (port->list == NULL || port->fis == NULL || port->cmd == NULL) {
355 warn_noalloc();
356 return NULL;
357 }
358 memset(port->list, 0, 1024);
359 memset(port->fis, 0, 256);
360 memset(port->cmd, 0, 256);
361
362 ahci_port_writel(ctrl, pnr, PORT_LST_ADDR, (u32)port->list);
363 ahci_port_writel(ctrl, pnr, PORT_FIS_ADDR, (u32)port->fis);
Ladi Prosek106543d2017-01-13 10:48:29 +0100364 if (ctrl->caps & HOST_CAP_64) {
365 ahci_port_writel(ctrl, pnr, PORT_LST_ADDR_HI, 0);
366 ahci_port_writel(ctrl, pnr, PORT_FIS_ADDR_HI, 0);
367 }
368
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200369 return port;
370}
371
Gerd Hoffmannce12eaf2013-09-09 16:33:06 +0200372static void ahci_port_release(struct ahci_port_s *port)
373{
374 ahci_port_reset(port->ctrl, port->pnr);
375 free(port->list);
376 free(port->fis);
377 free(port->cmd);
378 free(port);
379}
380
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200381static struct ahci_port_s* ahci_port_realloc(struct ahci_port_s *port)
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200382{
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200383 struct ahci_port_s *tmp;
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200384 u32 cmd;
385
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200386 tmp = malloc_fseg(sizeof(*port));
Gerd Hoffmannce12eaf2013-09-09 16:33:06 +0200387 if (!tmp) {
388 warn_noalloc();
389 ahci_port_release(port);
390 return NULL;
391 }
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200392 *tmp = *port;
393 free(port);
394 port = tmp;
395
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200396 ahci_port_reset(port->ctrl, port->pnr);
397
398 free(port->list);
399 free(port->fis);
400 free(port->cmd);
Gerd Hoffmann3ecdc492013-11-26 14:10:25 +0100401 port->list = memalign_high(1024, 1024);
402 port->fis = memalign_high(256, 256);
403 port->cmd = memalign_high(256, 256);
Kevin O'Connor3abdc7c2015-06-30 11:10:41 -0400404 if (!port->list || !port->fis || !port->cmd) {
405 warn_noalloc();
406 free(port->list);
407 free(port->fis);
408 free(port->cmd);
409 free(port);
410 return NULL;
411 }
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200412
413 ahci_port_writel(port->ctrl, port->pnr, PORT_LST_ADDR, (u32)port->list);
414 ahci_port_writel(port->ctrl, port->pnr, PORT_FIS_ADDR, (u32)port->fis);
415
416 cmd = ahci_port_readl(port->ctrl, port->pnr, PORT_CMD);
417 cmd |= (PORT_CMD_FIS_RX|PORT_CMD_START);
418 ahci_port_writel(port->ctrl, port->pnr, PORT_CMD, cmd);
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200419
420 return port;
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200421}
422
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200423#define MAXMODEL 40
424
425/* See ahci spec chapter 10.1 "Software Initialization of HBA" */
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500426static int ahci_port_setup(struct ahci_port_s *port)
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200427{
428 struct ahci_ctrl_s *ctrl = port->ctrl;
429 u32 pnr = port->pnr;
430 char model[MAXMODEL+1];
431 u16 buffer[256];
432 u32 cmd, stat, err, tf;
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200433 int rc;
434
435 /* enable FIS recv */
436 cmd = ahci_port_readl(ctrl, pnr, PORT_CMD);
437 cmd |= PORT_CMD_FIS_RX;
438 ahci_port_writel(ctrl, pnr, PORT_CMD, cmd);
439
440 /* spin up */
441 cmd |= PORT_CMD_SPIN_UP;
442 ahci_port_writel(ctrl, pnr, PORT_CMD, cmd);
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400443 u32 end = timer_calc(AHCI_LINK_TIMEOUT);
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200444 for (;;) {
445 stat = ahci_port_readl(ctrl, pnr, PORT_SCR_STAT);
446 if ((stat & 0x07) == 0x03) {
Kevin O'Connorbf079e12012-03-11 11:19:52 -0400447 dprintf(2, "AHCI/%d: link up\n", port->pnr);
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200448 break;
449 }
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400450 if (timer_check(end)) {
Kevin O'Connorbf079e12012-03-11 11:19:52 -0400451 dprintf(2, "AHCI/%d: link down\n", port->pnr);
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200452 return -1;
453 }
454 yield();
455 }
456
457 /* clear error status */
458 err = ahci_port_readl(ctrl, pnr, PORT_SCR_ERR);
459 if (err)
460 ahci_port_writel(ctrl, pnr, PORT_SCR_ERR, err);
461
462 /* wait for device becoming ready */
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400463 end = timer_calc(AHCI_REQUEST_TIMEOUT);
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200464 for (;;) {
465 tf = ahci_port_readl(ctrl, pnr, PORT_TFDATA);
466 if (!(tf & (ATA_CB_STAT_BSY |
467 ATA_CB_STAT_DRQ)))
468 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400469 if (timer_check(end)) {
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200470 warn_timeout();
471 dprintf(1, "AHCI/%d: device not ready (tf 0x%x)\n", port->pnr, tf);
472 return -1;
473 }
474 yield();
475 }
476
477 /* start device */
478 cmd |= PORT_CMD_START;
479 ahci_port_writel(ctrl, pnr, PORT_CMD, cmd);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100480
481 sata_prep_simple(&port->cmd->fis, ATA_CMD_IDENTIFY_PACKET_DEVICE);
482 rc = ahci_command(port, 0, 0, buffer, sizeof(buffer));
483 if (rc == 0) {
484 port->atapi = 1;
485 } else {
486 port->atapi = 0;
487 sata_prep_simple(&port->cmd->fis, ATA_CMD_IDENTIFY_DEVICE);
488 rc = ahci_command(port, 0, 0, buffer, sizeof(buffer));
489 if (rc < 0)
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200490 return -1;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100491 }
492
Gerd Hoffmann0e6f6362010-12-09 08:39:48 +0100493 port->drive.cntl_id = pnr;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100494 port->drive.removable = (buffer[0] & 0x80) ? 1 : 0;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100495
496 if (!port->atapi) {
497 // found disk (ata)
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400498 port->drive.type = DTYPE_AHCI;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100499 port->drive.blksize = DISK_SECTOR_SIZE;
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400500 port->drive.pchs.cylinder = buffer[1];
501 port->drive.pchs.head = buffer[3];
502 port->drive.pchs.sector = buffer[6];
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100503
504 u64 sectors;
505 if (buffer[83] & (1 << 10)) // word 83 - lba48 support
506 sectors = *(u64*)&buffer[100]; // word 100-103
507 else
508 sectors = *(u32*)&buffer[60]; // word 60 and word 61
509 port->drive.sectors = sectors;
510 u64 adjsize = sectors >> 11;
511 char adjprefix = 'M';
512 if (adjsize >= (1 << 16)) {
513 adjsize >>= 10;
514 adjprefix = 'G';
515 }
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200516 port->desc = znprintf(MAXDESCSIZE
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500517 , "AHCI/%d: %s ATA-%d Hard-Disk (%u %ciBytes)"
518 , port->pnr
519 , ata_extract_model(model, MAXMODEL, buffer)
520 , ata_extract_version(buffer)
521 , (u32)adjsize, adjprefix);
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200522 port->prio = bootprio_find_ata_device(ctrl->pci_tmp, pnr, 0);
Gerd Hoffmann3f478b92016-02-20 15:20:15 +0100523
524 s8 multi_dma = -1;
525 s8 pio_mode = -1;
526 s8 udma_mode = -1;
527 // If bit 2 in word 53 is set, udma information is valid in word 88.
528 if (buffer[53] & 0x04) {
529 udma_mode = 6;
530 while ((udma_mode >= 0) &&
531 !((buffer[88] & 0x7f) & ( 1 << udma_mode ))) {
532 udma_mode--;
533 }
534 }
535 // If bit 1 in word 53 is set, multiword-dma and advanced pio modes
536 // are available in words 63 and 64.
537 if (buffer[53] & 0x02) {
538 pio_mode = 4;
539 multi_dma = 3;
540 while ((multi_dma >= 0) &&
541 !((buffer[63] & 0x7) & ( 1 << multi_dma ))) {
542 multi_dma--;
543 }
544 while ((pio_mode >= 3) &&
545 !((buffer[64] & 0x3) & ( 1 << ( pio_mode - 3 ) ))) {
546 pio_mode--;
547 }
548 }
549 dprintf(2, "AHCI/%d: supported modes: udma %d, multi-dma %d, pio %d\n",
550 port->pnr, udma_mode, multi_dma, pio_mode);
551
552 sata_prep_simple(&port->cmd->fis, ATA_CMD_SET_FEATURES);
553 port->cmd->fis.feature = ATA_SET_FEATRUE_TRANSFER_MODE;
554 // Select used mode. UDMA first, then Multi-DMA followed by
555 // advanced PIO modes 3 or 4. If non, set default PIO.
556 if (udma_mode >= 0) {
557 dprintf(1, "AHCI/%d: Set transfer mode to UDMA-%d\n",
558 port->pnr, udma_mode);
559 port->cmd->fis.sector_count = ATA_TRANSFER_MODE_ULTRA_DMA
560 | udma_mode;
561 } else if (multi_dma >= 0) {
562 dprintf(1, "AHCI/%d: Set transfer mode to Multi-DMA-%d\n",
563 port->pnr, multi_dma);
564 port->cmd->fis.sector_count = ATA_TRANSFER_MODE_MULTIWORD_DMA
565 | multi_dma;
566 } else if (pio_mode >= 3) {
567 dprintf(1, "AHCI/%d: Set transfer mode to PIO-%d\n",
568 port->pnr, pio_mode);
569 port->cmd->fis.sector_count = ATA_TRANSFER_MODE_PIO_FLOW_CTRL
570 | pio_mode;
571 } else {
572 dprintf(1, "AHCI/%d: Set transfer mode to default PIO\n",
573 port->pnr);
574 port->cmd->fis.sector_count = ATA_TRANSFER_MODE_DEFAULT_PIO;
575 }
576 rc = ahci_command(port, 1, 0, 0, 0);
577 if (rc < 0) {
578 dprintf(1, "AHCI/%d: Set transfer mode failed.\n", port->pnr);
579 }
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100580 } else {
581 // found cdrom (atapi)
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400582 port->drive.type = DTYPE_AHCI_ATAPI;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100583 port->drive.blksize = CDROM_SECTOR_SIZE;
584 port->drive.sectors = (u64)-1;
585 u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
Gerd Hoffmann263ea2f2011-08-04 19:36:29 +0200586 if (!iscd) {
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400587 dprintf(1, "AHCI/%d: atapi device isn't a cdrom\n", port->pnr);
Gerd Hoffmann263ea2f2011-08-04 19:36:29 +0200588 return -1;
589 }
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200590 port->desc = znprintf(MAXDESCSIZE
Gerd Hoffmann263ea2f2011-08-04 19:36:29 +0200591 , "DVD/CD [AHCI/%d: %s ATAPI-%d DVD/CD]"
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500592 , port->pnr
593 , ata_extract_model(model, MAXMODEL, buffer)
Gerd Hoffmann263ea2f2011-08-04 19:36:29 +0200594 , ata_extract_version(buffer));
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200595 port->prio = bootprio_find_ata_device(ctrl->pci_tmp, pnr, 0);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100596 }
Gerd Hoffmanne1041192011-07-14 16:24:04 +0200597 return 0;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100598}
599
600// Detect any drives attached to a given controller.
601static void
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200602ahci_port_detect(void *data)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100603{
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200604 struct ahci_port_s *port = data;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100605 int rc;
606
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200607 dprintf(2, "AHCI/%d: probing\n", port->pnr);
608 ahci_port_reset(port->ctrl, port->pnr);
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500609 rc = ahci_port_setup(port);
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200610 if (rc < 0)
611 ahci_port_release(port);
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200612 else {
Gerd Hoffmannef8adc02011-08-04 19:36:31 +0200613 port = ahci_port_realloc(port);
Gerd Hoffmannce12eaf2013-09-09 16:33:06 +0200614 if (port == NULL)
615 return;
Gerd Hoffmann2dcbf7f2011-08-04 19:36:30 +0200616 dprintf(1, "AHCI/%d: registering: \"%s\"\n", port->pnr, port->desc);
617 if (!port->atapi) {
618 // Register with bcv system.
619 boot_add_hd(&port->drive, port->desc, port->prio);
620 } else {
621 // fill cdidmap
622 boot_add_cd(&port->drive, port->desc, port->prio);
623 }
624 }
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100625}
626
627// Initialize an ata controller and detect its drives.
628static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500629ahci_controller_setup(struct pci_device *pci)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100630{
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200631 struct ahci_port_s *port;
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200632 u32 val, pnr, max;
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100633
Kevin O'Connor6e3436b2016-02-02 22:11:30 -0500634 if (create_bounce_buf() < 0)
635 return;
636
637 void *iobase = pci_enable_membar(pci, PCI_BASE_ADDRESS_5);
638 if (!iobase)
639 return;
640
641 struct ahci_ctrl_s *ctrl = malloc_fseg(sizeof(*ctrl));
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100642 if (!ctrl) {
643 warn_noalloc();
644 return;
645 }
Scott Duplichan9c48aab2011-07-14 16:24:02 +0200646
Gerd Hoffmann9c869922011-07-14 16:24:05 +0200647 ctrl->pci_tmp = pci;
Kevin O'Connor6e3436b2016-02-02 22:11:30 -0500648 ctrl->iobase = iobase;
649 ctrl->irq = pci_config_readb(pci->bdf, PCI_INTERRUPT_LINE);
Kevin O'Connor7b673002016-02-03 03:03:15 -0500650 dprintf(1, "AHCI controller at %pP, iobase %p, irq %d\n"
651 , pci, ctrl->iobase, ctrl->irq);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100652
Kevin O'Connor6e3436b2016-02-02 22:11:30 -0500653 pci_enable_busmaster(pci);
Kevin O'Connor7eb02222010-12-12 14:01:47 -0500654
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100655 val = ahci_ctrl_readl(ctrl, HOST_CTL);
656 ahci_ctrl_writel(ctrl, HOST_CTL, val | HOST_CTL_AHCI_EN);
657
658 ctrl->caps = ahci_ctrl_readl(ctrl, HOST_CAP);
659 ctrl->ports = ahci_ctrl_readl(ctrl, HOST_PORTS_IMPL);
660 dprintf(2, "AHCI: cap 0x%x, ports_impl 0x%x\n",
661 ctrl->caps, ctrl->ports);
662
Vladimir Serbinenko40dfc0e2015-05-18 15:00:31 +0200663 max = 0x1f;
Gerd Hoffmann9713f242011-08-04 19:36:28 +0200664 for (pnr = 0; pnr <= max; pnr++) {
665 if (!(ctrl->ports & (1 << pnr)))
666 continue;
667 port = ahci_port_alloc(ctrl, pnr);
668 if (port == NULL)
669 continue;
670 run_thread(ahci_port_detect, port);
671 }
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100672}
673
674// Locate and init ahci controllers.
675static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500676ahci_scan(void)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100677{
678 // Scan PCI bus for ATA adapters
Kevin O'Connor9cb49922011-06-20 22:22:42 -0400679 struct pci_device *pci;
680 foreachpci(pci) {
681 if (pci->class != PCI_CLASS_STORAGE_SATA)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100682 continue;
Kevin O'Connor9cb49922011-06-20 22:22:42 -0400683 if (pci->prog_if != 1 /* AHCI rev 1 */)
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100684 continue;
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500685 ahci_controller_setup(pci);
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100686 }
687}
688
689void
690ahci_setup(void)
691{
692 ASSERT32FLAT();
693 if (!CONFIG_AHCI)
694 return;
695
696 dprintf(3, "init ahci\n");
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500697 ahci_scan();
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100698}