1 | /* $Id: ahci.c 69496 2017-10-28 14:55:58Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * AHCI host adapter driver to boot from SATA disks.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2017 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include <stdint.h>
|
---|
19 | #include <string.h>
|
---|
20 | #include "biosint.h"
|
---|
21 | #include "ebda.h"
|
---|
22 | #include "inlines.h"
|
---|
23 | #include "pciutil.h"
|
---|
24 | #include "vds.h"
|
---|
25 |
|
---|
26 | #if DEBUG_AHCI
|
---|
27 | # define DBG_AHCI(...) BX_INFO(__VA_ARGS__)
|
---|
28 | #else
|
---|
29 | # define DBG_AHCI(...)
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | /* Number of S/G table entries in EDDS. */
|
---|
33 | #define NUM_EDDS_SG 16
|
---|
34 |
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * AHCI PRDT structure.
|
---|
38 | */
|
---|
39 | typedef struct
|
---|
40 | {
|
---|
41 | uint32_t phys_addr;
|
---|
42 | uint32_t something;
|
---|
43 | uint32_t reserved;
|
---|
44 | uint32_t len;
|
---|
45 | } ahci_prdt;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * SATA D2H FIS (Device to Host Frame Information Structure).
|
---|
49 | */
|
---|
50 | typedef struct {
|
---|
51 | uint8_t fis_type; /* 34h */
|
---|
52 | uint8_t intr; /* Bit 6 indicates interrupt status. */
|
---|
53 | uint8_t status; /* Status register. */
|
---|
54 | uint8_t error; /* Error register. */
|
---|
55 | uint8_t sec_no; /* Sector number register. */
|
---|
56 | uint8_t cyl_lo; /* Cylinder low register. */
|
---|
57 | uint8_t cyl_hi; /* Cylinder high register. */
|
---|
58 | uint8_t dev_hd; /* Device/head register. */
|
---|
59 | uint8_t sec_no_exp; /* Expanded sector number register. */
|
---|
60 | uint8_t cyl_lo_exp; /* Expanded cylinder low register. */
|
---|
61 | uint8_t cyl_hi_exp; /* Expanded cylinder high register. */
|
---|
62 | uint8_t resvd0;
|
---|
63 | uint8_t sec_cn; /* Sector count register. */
|
---|
64 | uint8_t sec_cn_exp; /* Expanded sector count register. */
|
---|
65 | uint16_t resvd1;
|
---|
66 | uint32_t resvd2;
|
---|
67 | } fis_d2h;
|
---|
68 |
|
---|
69 | ct_assert(sizeof(fis_d2h) == 20);
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * AHCI controller data.
|
---|
73 | */
|
---|
74 | typedef struct
|
---|
75 | {
|
---|
76 | /** The AHCI command list as defined by chapter 4.2.2 of the Intel AHCI spec.
|
---|
77 | * Because the BIOS doesn't support NCQ only the first command header is defined
|
---|
78 | * to save memory. - Must be aligned on a 1K boundary.
|
---|
79 | */
|
---|
80 | uint32_t aCmdHdr[0x8];
|
---|
81 | /** Align the next structure on a 128 byte boundary. */
|
---|
82 | uint8_t abAlignment1[0x60];
|
---|
83 | /** The command table of one request as defined by chapter 4.2.3 of the Intel AHCI spec.
|
---|
84 | * Must be aligned on 128 byte boundary.
|
---|
85 | */
|
---|
86 | uint8_t abCmd[0x40];
|
---|
87 | /** The ATAPI command region.
|
---|
88 | * Located 40h bytes after the beginning of the CFIS (Command FIS).
|
---|
89 | */
|
---|
90 | uint8_t abAcmd[0x20];
|
---|
91 | /** Align the PRDT structure on a 128 byte boundary. */
|
---|
92 | uint8_t abAlignment2[0x20];
|
---|
93 | /** Physical Region Descriptor Table (PRDT) array. In other
|
---|
94 | * words, a scatter/gather descriptor list.
|
---|
95 | */
|
---|
96 | ahci_prdt aPrdt[16];
|
---|
97 | /** Memory for the received command FIS area as specified by chapter 4.2.1
|
---|
98 | * of the Intel AHCI spec. This area is normally 256 bytes big but to save memory
|
---|
99 | * only the first 96 bytes are used because it is assumed that the controller
|
---|
100 | * never writes to the UFIS or reserved area. - Must be aligned on a 256byte boundary.
|
---|
101 | */
|
---|
102 | uint8_t abFisRecv[0x60];
|
---|
103 | /** Base I/O port for the index/data register pair. */
|
---|
104 | uint16_t iobase;
|
---|
105 | /** Current port which uses the memory to communicate with the controller. */
|
---|
106 | uint8_t cur_port;
|
---|
107 | /** Current PRD index (for pre/post skip). */
|
---|
108 | uint8_t cur_prd;
|
---|
109 | /** Physical address of the sink buffer (for pre/post skip). */
|
---|
110 | uint32_t sink_buf_phys;
|
---|
111 | /** Saved high bits of EAX. */
|
---|
112 | uint16_t saved_eax_hi;
|
---|
113 | /** VDS EDDS DMA buffer descriptor structure. */
|
---|
114 | vds_edds edds;
|
---|
115 | vds_sg edds_more_sg[NUM_EDDS_SG - 1];
|
---|
116 | } ahci_t;
|
---|
117 |
|
---|
118 | /* The AHCI specific data must fit into 1KB (statically allocated). */
|
---|
119 | ct_assert(sizeof(ahci_t) <= 1024);
|
---|
120 |
|
---|
121 | /** PCI configuration fields. */
|
---|
122 | #define PCI_CONFIG_CAP 0x34
|
---|
123 |
|
---|
124 | #define PCI_CAP_ID_SATACR 0x12
|
---|
125 | #define VBOX_AHCI_NO_DEVICE 0xffff
|
---|
126 |
|
---|
127 | #define RT_BIT_32(bit) ((uint32_t)(1L << (bit)))
|
---|
128 |
|
---|
129 | /** Global register set. */
|
---|
130 | #define AHCI_HBA_SIZE 0x100
|
---|
131 |
|
---|
132 | /// @todo what are the casts good for?
|
---|
133 | #define AHCI_REG_CAP ((uint32_t)0x00)
|
---|
134 | #define AHCI_REG_GHC ((uint32_t)0x04)
|
---|
135 | # define AHCI_GHC_AE RT_BIT_32(31)
|
---|
136 | # define AHCI_GHC_IR RT_BIT_32(1)
|
---|
137 | # define AHCI_GHC_HR RT_BIT_32(0)
|
---|
138 | #define AHCI_REG_IS ((uint32_t)0x08)
|
---|
139 | #define AHCI_REG_PI ((uint32_t)0x0c)
|
---|
140 | #define AHCI_REG_VS ((uint32_t)0x10)
|
---|
141 |
|
---|
142 | /** Per port register set. */
|
---|
143 | #define AHCI_PORT_SIZE 0x80
|
---|
144 |
|
---|
145 | #define AHCI_REG_PORT_CLB 0x00
|
---|
146 | #define AHCI_REG_PORT_CLBU 0x04
|
---|
147 | #define AHCI_REG_PORT_FB 0x08
|
---|
148 | #define AHCI_REG_PORT_FBU 0x0c
|
---|
149 | #define AHCI_REG_PORT_IS 0x10
|
---|
150 | # define AHCI_REG_PORT_IS_DHRS RT_BIT_32(0)
|
---|
151 | # define AHCI_REG_PORT_IS_TFES RT_BIT_32(30)
|
---|
152 | #define AHCI_REG_PORT_IE 0x14
|
---|
153 | #define AHCI_REG_PORT_CMD 0x18
|
---|
154 | # define AHCI_REG_PORT_CMD_ST RT_BIT_32(0)
|
---|
155 | # define AHCI_REG_PORT_CMD_FRE RT_BIT_32(4)
|
---|
156 | # define AHCI_REG_PORT_CMD_FR RT_BIT_32(14)
|
---|
157 | # define AHCI_REG_PORT_CMD_CR RT_BIT_32(15)
|
---|
158 | #define AHCI_REG_PORT_TFD 0x20
|
---|
159 | #define AHCI_REG_PORT_SIG 0x24
|
---|
160 | #define AHCI_REG_PORT_SSTS 0x28
|
---|
161 | #define AHCI_REG_PORT_SCTL 0x2c
|
---|
162 | #define AHCI_REG_PORT_SERR 0x30
|
---|
163 | #define AHCI_REG_PORT_SACT 0x34
|
---|
164 | #define AHCI_REG_PORT_CI 0x38
|
---|
165 |
|
---|
166 | /** Returns the absolute register offset from a given port and port register. */
|
---|
167 | #define AHCI_PORT_REG(port, reg) (AHCI_HBA_SIZE + (port) * AHCI_PORT_SIZE + (reg))
|
---|
168 |
|
---|
169 | #define AHCI_REG_IDX 0
|
---|
170 | #define AHCI_REG_DATA 4
|
---|
171 |
|
---|
172 | /** Writes the given value to a AHCI register. */
|
---|
173 | #define AHCI_WRITE_REG(iobase, reg, val) \
|
---|
174 | outpd((iobase) + AHCI_REG_IDX, reg); \
|
---|
175 | outpd((iobase) + AHCI_REG_DATA, val)
|
---|
176 |
|
---|
177 | /** Reads from a AHCI register. */
|
---|
178 | #define AHCI_READ_REG(iobase, reg, val) \
|
---|
179 | outpd((iobase) + AHCI_REG_IDX, reg); \
|
---|
180 | (val) = inpd((iobase) + AHCI_REG_DATA)
|
---|
181 |
|
---|
182 | /** Writes to the given port register. */
|
---|
183 | #define VBOXAHCI_PORT_WRITE_REG(iobase, port, reg, val) \
|
---|
184 | AHCI_WRITE_REG((iobase), AHCI_PORT_REG((port), (reg)), val)
|
---|
185 |
|
---|
186 | /** Reads from the given port register. */
|
---|
187 | #define VBOXAHCI_PORT_READ_REG(iobase, port, reg, val) \
|
---|
188 | AHCI_READ_REG((iobase), AHCI_PORT_REG((port), (reg)), val)
|
---|
189 |
|
---|
190 | #define ATA_CMD_IDENTIFY_DEVICE 0xEC
|
---|
191 | #define ATA_CMD_IDENTIFY_PACKET 0xA1
|
---|
192 | #define ATA_CMD_PACKET 0xA0
|
---|
193 | #define AHCI_CMD_READ_DMA_EXT 0x25
|
---|
194 | #define AHCI_CMD_WRITE_DMA_EXT 0x35
|
---|
195 |
|
---|
196 |
|
---|
197 | /* Warning: Destroys high bits of EAX. */
|
---|
198 | uint32_t inpd(uint16_t port);
|
---|
199 | #pragma aux inpd = \
|
---|
200 | ".386" \
|
---|
201 | "in eax, dx" \
|
---|
202 | "mov dx, ax" \
|
---|
203 | "shr eax, 16" \
|
---|
204 | "xchg ax, dx" \
|
---|
205 | parm [dx] value [dx ax] modify nomemory;
|
---|
206 |
|
---|
207 | /* Warning: Destroys high bits of EAX. */
|
---|
208 | void outpd(uint16_t port, uint32_t val);
|
---|
209 | #pragma aux outpd = \
|
---|
210 | ".386" \
|
---|
211 | "xchg ax, cx" \
|
---|
212 | "shl eax, 16" \
|
---|
213 | "mov ax, cx" \
|
---|
214 | "out dx, eax" \
|
---|
215 | parm [dx] [cx ax] modify nomemory;
|
---|
216 |
|
---|
217 |
|
---|
218 | /* Machinery to save/restore high bits of EAX. 32-bit port I/O needs to use
|
---|
219 | * EAX, but saving/restoring EAX around each port access would be inefficient.
|
---|
220 | * Instead, each externally callable routine must save the high bits before
|
---|
221 | * modifying them and restore the high bits before exiting.
|
---|
222 | */
|
---|
223 |
|
---|
224 | /* Note: Reading high EAX bits destroys them - *must* be restored later. */
|
---|
225 | uint16_t eax_hi_rd(void);
|
---|
226 | #pragma aux eax_hi_rd = \
|
---|
227 | ".386" \
|
---|
228 | "shr eax, 16" \
|
---|
229 | value [ax] modify nomemory;
|
---|
230 |
|
---|
231 | void eax_hi_wr(uint16_t);
|
---|
232 | #pragma aux eax_hi_wr = \
|
---|
233 | ".386" \
|
---|
234 | "shl eax, 16" \
|
---|
235 | parm [ax] modify nomemory;
|
---|
236 |
|
---|
237 | void inline high_bits_save(ahci_t __far *ahci)
|
---|
238 | {
|
---|
239 | ahci->saved_eax_hi = eax_hi_rd();
|
---|
240 | }
|
---|
241 |
|
---|
242 | void inline high_bits_restore(ahci_t __far *ahci)
|
---|
243 | {
|
---|
244 | eax_hi_wr(ahci->saved_eax_hi);
|
---|
245 | }
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * Sets a given set of bits in a register.
|
---|
249 | */
|
---|
250 | static void inline ahci_ctrl_set_bits(uint16_t iobase, uint16_t reg, uint32_t mask)
|
---|
251 | {
|
---|
252 | outpd(iobase + AHCI_REG_IDX, reg);
|
---|
253 | outpd(iobase + AHCI_REG_DATA, inpd(iobase + AHCI_REG_DATA) | mask);
|
---|
254 | }
|
---|
255 |
|
---|
256 | /**
|
---|
257 | * Clears a given set of bits in a register.
|
---|
258 | */
|
---|
259 | static void inline ahci_ctrl_clear_bits(uint16_t iobase, uint16_t reg, uint32_t mask)
|
---|
260 | {
|
---|
261 | outpd(iobase + AHCI_REG_IDX, reg);
|
---|
262 | outpd(iobase + AHCI_REG_DATA, inpd(iobase + AHCI_REG_DATA) & ~mask);
|
---|
263 | }
|
---|
264 |
|
---|
265 | /**
|
---|
266 | * Returns whether at least one of the bits in the given mask is set
|
---|
267 | * for a register.
|
---|
268 | */
|
---|
269 | static uint8_t inline ahci_ctrl_is_bit_set(uint16_t iobase, uint16_t reg, uint32_t mask)
|
---|
270 | {
|
---|
271 | outpd(iobase + AHCI_REG_IDX, reg);
|
---|
272 | return (inpd(iobase + AHCI_REG_DATA) & mask) != 0;
|
---|
273 | }
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * Extracts a range of bits from a register and shifts them
|
---|
277 | * to the right.
|
---|
278 | */
|
---|
279 | static uint16_t ahci_ctrl_extract_bits(uint32_t val, uint32_t mask, uint8_t shift)
|
---|
280 | {
|
---|
281 | return (val & mask) >> shift;
|
---|
282 | }
|
---|
283 |
|
---|
284 | /**
|
---|
285 | * Converts a segment:offset pair into a 32bit physical address.
|
---|
286 | */
|
---|
287 | static uint32_t ahci_addr_to_phys(void __far *ptr)
|
---|
288 | {
|
---|
289 | return ((uint32_t)FP_SEG(ptr) << 4) + FP_OFF(ptr);
|
---|
290 | }
|
---|
291 |
|
---|
292 | /**
|
---|
293 | * Issues a command to the SATA controller and waits for completion.
|
---|
294 | */
|
---|
295 | static void ahci_port_cmd_sync(ahci_t __far *ahci, uint8_t val)
|
---|
296 | {
|
---|
297 | uint16_t io_base;
|
---|
298 | uint8_t port;
|
---|
299 |
|
---|
300 | port = ahci->cur_port;
|
---|
301 | io_base = ahci->iobase;
|
---|
302 |
|
---|
303 | if (port != 0xff)
|
---|
304 | {
|
---|
305 | /* Prepare the command header. */
|
---|
306 | ahci->aCmdHdr[0] = ((uint32_t)ahci->cur_prd << 16) | RT_BIT_32(7) | val;
|
---|
307 | ahci->aCmdHdr[1] = 0;
|
---|
308 | ahci->aCmdHdr[2] = ahci_addr_to_phys(&ahci->abCmd[0]);
|
---|
309 |
|
---|
310 | /* Enable Command and FIS receive engine. */
|
---|
311 | ahci_ctrl_set_bits(io_base, AHCI_PORT_REG(port, AHCI_REG_PORT_CMD),
|
---|
312 | AHCI_REG_PORT_CMD_FRE | AHCI_REG_PORT_CMD_ST);
|
---|
313 |
|
---|
314 | /* Queue command. */
|
---|
315 | VBOXAHCI_PORT_WRITE_REG(io_base, port, AHCI_REG_PORT_CI, 0x1);
|
---|
316 |
|
---|
317 | /* Wait for a D2H FIS. */
|
---|
318 | DBG_AHCI("AHCI: Waiting for D2H FIS\n");
|
---|
319 | while (ahci_ctrl_is_bit_set(io_base, AHCI_PORT_REG(port, AHCI_REG_PORT_IS),
|
---|
320 | AHCI_REG_PORT_IS_DHRS | AHCI_REG_PORT_IS_TFES) == 0)
|
---|
321 | {
|
---|
322 | // This is where we'd need some kind of a yield functionality...
|
---|
323 | }
|
---|
324 |
|
---|
325 | ahci_ctrl_set_bits(io_base, AHCI_PORT_REG(port, AHCI_REG_PORT_IS),
|
---|
326 | AHCI_REG_PORT_IS_DHRS); /* Acknowledge received D2H FIS. */
|
---|
327 |
|
---|
328 | /* Disable command engine. */
|
---|
329 | ahci_ctrl_clear_bits(io_base, AHCI_PORT_REG(port, AHCI_REG_PORT_CMD),
|
---|
330 | AHCI_REG_PORT_CMD_ST);
|
---|
331 | /* Caller must examine status. */
|
---|
332 | }
|
---|
333 | else
|
---|
334 | DBG_AHCI("AHCI: Invalid port given\n");
|
---|
335 | }
|
---|
336 |
|
---|
337 | /**
|
---|
338 | * Issue command to device.
|
---|
339 | */
|
---|
340 | static uint16_t ahci_cmd_data(bio_dsk_t __far *bios_dsk, uint8_t cmd)
|
---|
341 | {
|
---|
342 | ahci_t __far *ahci = bios_dsk->ahci_seg :> 0;
|
---|
343 | uint16_t n_sect = bios_dsk->drqp.nsect;
|
---|
344 | uint16_t sectsz = bios_dsk->drqp.sect_sz;
|
---|
345 | fis_d2h __far *d2h;
|
---|
346 |
|
---|
347 | _fmemset(&ahci->abCmd[0], 0, sizeof(ahci->abCmd));
|
---|
348 |
|
---|
349 | /* Prepare the FIS. */
|
---|
350 | ahci->abCmd[0] = 0x27; /* FIS type H2D. */
|
---|
351 | ahci->abCmd[1] = 1 << 7; /* Command update. */
|
---|
352 | ahci->abCmd[2] = cmd;
|
---|
353 | ahci->abCmd[3] = 0;
|
---|
354 |
|
---|
355 | ahci->abCmd[4] = bios_dsk->drqp.lba & 0xff;
|
---|
356 | ahci->abCmd[5] = (bios_dsk->drqp.lba >> 8) & 0xff;
|
---|
357 | ahci->abCmd[6] = (bios_dsk->drqp.lba >> 16) & 0xff;
|
---|
358 | ahci->abCmd[7] = RT_BIT_32(6); /* LBA access. */
|
---|
359 |
|
---|
360 | ahci->abCmd[8] = (bios_dsk->drqp.lba >> 24) & 0xff;
|
---|
361 | ahci->abCmd[9] = (bios_dsk->drqp.lba >> 32) & 0xff;
|
---|
362 | ahci->abCmd[10] = (bios_dsk->drqp.lba >> 40) & 0xff;
|
---|
363 | ahci->abCmd[11] = 0;
|
---|
364 |
|
---|
365 | ahci->abCmd[12] = (uint8_t)(n_sect & 0xff);
|
---|
366 | ahci->abCmd[13] = (uint8_t)((n_sect >> 8) & 0xff);
|
---|
367 |
|
---|
368 | /* Lock memory needed for DMA. */
|
---|
369 | ahci->edds.num_avail = NUM_EDDS_SG;
|
---|
370 | DBG_AHCI("AHCI: S/G list for %lu bytes (skip %u)\n",
|
---|
371 | (uint32_t)n_sect * sectsz, bios_dsk->drqp.skip_a);
|
---|
372 | vds_build_sg_list(&ahci->edds, bios_dsk->drqp.buffer, (uint32_t)n_sect * sectsz);
|
---|
373 |
|
---|
374 | /* Set up the PRDT. */
|
---|
375 | ahci->aPrdt[ahci->cur_prd].len = ahci->edds.u.sg[0].size - 1;
|
---|
376 | ahci->aPrdt[ahci->cur_prd].phys_addr = ahci->edds.u.sg[0].phys_addr;
|
---|
377 | ++ahci->cur_prd;
|
---|
378 |
|
---|
379 | if (bios_dsk->drqp.skip_a) {
|
---|
380 | ahci->aPrdt[ahci->cur_prd].len = bios_dsk->drqp.skip_a - 1;
|
---|
381 | ahci->aPrdt[ahci->cur_prd].phys_addr = ahci->sink_buf_phys;
|
---|
382 | ++ahci->cur_prd;
|
---|
383 | }
|
---|
384 |
|
---|
385 | #if DEBUG_AHCI
|
---|
386 | {
|
---|
387 | uint16_t prdt_idx;
|
---|
388 |
|
---|
389 | for (prdt_idx = 0; prdt_idx < ahci->cur_prd; ++prdt_idx) {
|
---|
390 | DBG_AHCI("S/G entry %u: %5lu bytes @ %08lX\n", prdt_idx,
|
---|
391 | ahci->aPrdt[prdt_idx].len + 1, ahci->aPrdt[prdt_idx].phys_addr);
|
---|
392 | }
|
---|
393 | }
|
---|
394 | #endif
|
---|
395 |
|
---|
396 | /* Build variable part of first command DWORD (reuses 'cmd'). */
|
---|
397 | if (cmd == AHCI_CMD_WRITE_DMA_EXT)
|
---|
398 | cmd = RT_BIT_32(6); /* Indicate a write to device. */
|
---|
399 | else if (cmd == ATA_CMD_PACKET) {
|
---|
400 | cmd |= RT_BIT_32(5); /* Indicate ATAPI command. */
|
---|
401 | ahci->abCmd[3] |= 1; /* DMA transfers. */
|
---|
402 | } else
|
---|
403 | cmd = 0;
|
---|
404 |
|
---|
405 | cmd |= 5; /* Five DWORDs. */
|
---|
406 |
|
---|
407 | ahci_port_cmd_sync(ahci, cmd);
|
---|
408 |
|
---|
409 | /* Examine operation status. */
|
---|
410 | d2h = (void __far *)&ahci->abFisRecv[0x40];
|
---|
411 | DBG_AHCI("AHCI: ERR=%02x, STAT=%02x, SCNT=%02x\n", d2h->error, d2h->status, d2h->sec_cn);
|
---|
412 |
|
---|
413 | /* Unlock the buffer again. */
|
---|
414 | vds_free_sg_list(&ahci->edds);
|
---|
415 | return d2h->error ? 4 : 0;
|
---|
416 | }
|
---|
417 |
|
---|
418 | /**
|
---|
419 | * Deinits the curent active port.
|
---|
420 | */
|
---|
421 | static void ahci_port_deinit_current(ahci_t __far *ahci)
|
---|
422 | {
|
---|
423 | uint16_t io_base;
|
---|
424 | uint8_t port;
|
---|
425 |
|
---|
426 | io_base = ahci->iobase;
|
---|
427 | port = ahci->cur_port;
|
---|
428 |
|
---|
429 | if (port != 0xff)
|
---|
430 | {
|
---|
431 | /* Put the port into an idle state. */
|
---|
432 | ahci_ctrl_clear_bits(io_base, AHCI_PORT_REG(port, AHCI_REG_PORT_CMD),
|
---|
433 | AHCI_REG_PORT_CMD_FRE | AHCI_REG_PORT_CMD_ST);
|
---|
434 |
|
---|
435 | while (ahci_ctrl_is_bit_set(io_base, AHCI_PORT_REG(port, AHCI_REG_PORT_CMD),
|
---|
436 | AHCI_REG_PORT_CMD_FRE | AHCI_REG_PORT_CMD_ST | AHCI_REG_PORT_CMD_FR | AHCI_REG_PORT_CMD_CR) == 1)
|
---|
437 | {
|
---|
438 | DBG_AHCI("AHCI: Waiting for the port to idle\n");
|
---|
439 | }
|
---|
440 |
|
---|
441 | /*
|
---|
442 | * Port idles, set up memory for commands and received FIS and program the
|
---|
443 | * address registers.
|
---|
444 | */
|
---|
445 | /// @todo merge memsets?
|
---|
446 | _fmemset(&ahci->aCmdHdr[0], 0, sizeof(ahci->aCmdHdr));
|
---|
447 | _fmemset(&ahci->abCmd[0], 0, sizeof(ahci->abCmd));
|
---|
448 | _fmemset(&ahci->abFisRecv[0], 0, sizeof(ahci->abFisRecv));
|
---|
449 |
|
---|
450 | VBOXAHCI_PORT_WRITE_REG(io_base, port, AHCI_REG_PORT_FB, 0);
|
---|
451 | VBOXAHCI_PORT_WRITE_REG(io_base, port, AHCI_REG_PORT_FBU, 0);
|
---|
452 |
|
---|
453 | VBOXAHCI_PORT_WRITE_REG(io_base, port, AHCI_REG_PORT_CLB, 0);
|
---|
454 | VBOXAHCI_PORT_WRITE_REG(io_base, port, AHCI_REG_PORT_CLBU, 0);
|
---|
455 |
|
---|
456 | /* Disable all interrupts. */
|
---|
457 | VBOXAHCI_PORT_WRITE_REG(io_base, port, AHCI_REG_PORT_IE, 0);
|
---|
458 |
|
---|
459 | ahci->cur_port = 0xff;
|
---|
460 | }
|
---|
461 | }
|
---|
462 |
|
---|
463 | /**
|
---|
464 | * Brings a port into a minimal state to make device detection possible
|
---|
465 | * or to queue requests.
|
---|
466 | */
|
---|
467 | static void ahci_port_init(ahci_t __far *ahci, uint8_t u8Port)
|
---|
468 | {
|
---|
469 | /* Deinit any other port first. */
|
---|
470 | ahci_port_deinit_current(ahci);
|
---|
471 |
|
---|
472 | /* Put the port into an idle state. */
|
---|
473 | ahci_ctrl_clear_bits(ahci->iobase, AHCI_PORT_REG(u8Port, AHCI_REG_PORT_CMD),
|
---|
474 | AHCI_REG_PORT_CMD_FRE | AHCI_REG_PORT_CMD_ST);
|
---|
475 |
|
---|
476 | while (ahci_ctrl_is_bit_set(ahci->iobase, AHCI_PORT_REG(u8Port, AHCI_REG_PORT_CMD),
|
---|
477 | AHCI_REG_PORT_CMD_FRE | AHCI_REG_PORT_CMD_ST | AHCI_REG_PORT_CMD_FR | AHCI_REG_PORT_CMD_CR) == 1)
|
---|
478 | {
|
---|
479 | DBG_AHCI("AHCI: Waiting for the port to idle\n");
|
---|
480 | }
|
---|
481 |
|
---|
482 | /*
|
---|
483 | * Port idles, set up memory for commands and received FIS and program the
|
---|
484 | * address registers.
|
---|
485 | */
|
---|
486 | /// @todo just one memset?
|
---|
487 | _fmemset(&ahci->aCmdHdr[0], 0, sizeof(ahci->aCmdHdr));
|
---|
488 | _fmemset(&ahci->abCmd[0], 0, sizeof(ahci->abCmd));
|
---|
489 | _fmemset(&ahci->abFisRecv[0], 0, sizeof(ahci->abFisRecv));
|
---|
490 |
|
---|
491 | DBG_AHCI("AHCI: FIS receive area %lx from %x:%x\n",
|
---|
492 | ahci_addr_to_phys(&ahci->abFisRecv), FP_SEG(ahci->abFisRecv), FP_OFF(ahci->abFisRecv));
|
---|
493 | VBOXAHCI_PORT_WRITE_REG(ahci->iobase, u8Port, AHCI_REG_PORT_FB, ahci_addr_to_phys(&ahci->abFisRecv));
|
---|
494 | VBOXAHCI_PORT_WRITE_REG(ahci->iobase, u8Port, AHCI_REG_PORT_FBU, 0);
|
---|
495 |
|
---|
496 | DBG_AHCI("AHCI: CMD list area %lx\n", ahci_addr_to_phys(&ahci->aCmdHdr));
|
---|
497 | VBOXAHCI_PORT_WRITE_REG(ahci->iobase, u8Port, AHCI_REG_PORT_CLB, ahci_addr_to_phys(&ahci->aCmdHdr));
|
---|
498 | VBOXAHCI_PORT_WRITE_REG(ahci->iobase, u8Port, AHCI_REG_PORT_CLBU, 0);
|
---|
499 |
|
---|
500 | /* Disable all interrupts. */
|
---|
501 | VBOXAHCI_PORT_WRITE_REG(ahci->iobase, u8Port, AHCI_REG_PORT_IE, 0);
|
---|
502 | VBOXAHCI_PORT_WRITE_REG(ahci->iobase, u8Port, AHCI_REG_PORT_IS, 0xffffffff);
|
---|
503 | /* Clear all errors. */
|
---|
504 | VBOXAHCI_PORT_WRITE_REG(ahci->iobase, u8Port, AHCI_REG_PORT_SERR, 0xffffffff);
|
---|
505 |
|
---|
506 | ahci->cur_port = u8Port;
|
---|
507 | ahci->cur_prd = 0;
|
---|
508 | }
|
---|
509 |
|
---|
510 | /**
|
---|
511 | * Read sectors from an attached AHCI device.
|
---|
512 | *
|
---|
513 | * @returns status code.
|
---|
514 | * @param bios_dsk Pointer to disk request packet (in the
|
---|
515 | * EBDA).
|
---|
516 | */
|
---|
517 | int ahci_read_sectors(bio_dsk_t __far *bios_dsk)
|
---|
518 | {
|
---|
519 | uint16_t device_id;
|
---|
520 | uint16_t rc;
|
---|
521 |
|
---|
522 | device_id = VBOX_GET_AHCI_DEVICE(bios_dsk->drqp.dev_id);
|
---|
523 | if (device_id > BX_MAX_AHCI_DEVICES)
|
---|
524 | BX_PANIC("%s: device_id out of range %d\n", __func__, device_id);
|
---|
525 |
|
---|
526 | DBG_AHCI("%s: %u sectors @ LBA 0x%llx, device %d, port %d\n", __func__,
|
---|
527 | bios_dsk->drqp.nsect, bios_dsk->drqp.lba,
|
---|
528 | device_id, bios_dsk->ahcidev[device_id].port);
|
---|
529 |
|
---|
530 | high_bits_save(bios_dsk->ahci_seg :> 0);
|
---|
531 | ahci_port_init(bios_dsk->ahci_seg :> 0, bios_dsk->ahcidev[device_id].port);
|
---|
532 | rc = ahci_cmd_data(bios_dsk, AHCI_CMD_READ_DMA_EXT);
|
---|
533 | DBG_AHCI("%s: transferred %lu bytes\n", __func__, ((ahci_t __far *)(bios_dsk->ahci_seg :> 0))->aCmdHdr[1]);
|
---|
534 | bios_dsk->drqp.trsfsectors = bios_dsk->drqp.nsect;
|
---|
535 | #ifdef DMA_WORKAROUND
|
---|
536 | rep_movsw(bios_dsk->drqp.buffer, bios_dsk->drqp.buffer, bios_dsk->drqp.nsect * 512 / 2);
|
---|
537 | #endif
|
---|
538 | high_bits_restore(bios_dsk->ahci_seg :> 0);
|
---|
539 | return rc;
|
---|
540 | }
|
---|
541 |
|
---|
542 | /**
|
---|
543 | * Write sectors to an attached AHCI device.
|
---|
544 | *
|
---|
545 | * @returns status code.
|
---|
546 | * @param bios_dsk Pointer to disk request packet (in the
|
---|
547 | * EBDA).
|
---|
548 | */
|
---|
549 | int ahci_write_sectors(bio_dsk_t __far *bios_dsk)
|
---|
550 | {
|
---|
551 | uint16_t device_id;
|
---|
552 | uint16_t rc;
|
---|
553 |
|
---|
554 | device_id = VBOX_GET_AHCI_DEVICE(bios_dsk->drqp.dev_id);
|
---|
555 | if (device_id > BX_MAX_AHCI_DEVICES)
|
---|
556 | BX_PANIC("%s: device_id out of range %d\n", __func__, device_id);
|
---|
557 |
|
---|
558 | DBG_AHCI("%s: %u sectors @ LBA 0x%llx, device %d, port %d\n", __func__,
|
---|
559 | bios_dsk->drqp.nsect, bios_dsk->drqp.lba, device_id,
|
---|
560 | bios_dsk->ahcidev[device_id].port);
|
---|
561 |
|
---|
562 | high_bits_save(bios_dsk->ahci_seg :> 0);
|
---|
563 | ahci_port_init(bios_dsk->ahci_seg :> 0, bios_dsk->ahcidev[device_id].port);
|
---|
564 | rc = ahci_cmd_data(bios_dsk, AHCI_CMD_WRITE_DMA_EXT);
|
---|
565 | DBG_AHCI("%s: transferred %lu bytes\n", __func__, ((ahci_t __far *)(bios_dsk->ahci_seg :> 0))->aCmdHdr[1]);
|
---|
566 | bios_dsk->drqp.trsfsectors = bios_dsk->drqp.nsect;
|
---|
567 | high_bits_restore(bios_dsk->ahci_seg :> 0);
|
---|
568 | return rc;
|
---|
569 | }
|
---|
570 |
|
---|
571 | /// @todo move
|
---|
572 | #define ATA_DATA_NO 0x00
|
---|
573 | #define ATA_DATA_IN 0x01
|
---|
574 | #define ATA_DATA_OUT 0x02
|
---|
575 |
|
---|
576 | uint16_t ahci_cmd_packet(uint16_t device_id, uint8_t cmdlen, char __far *cmdbuf,
|
---|
577 | uint16_t skip_b, uint32_t length, uint8_t inout, char __far *buffer)
|
---|
578 | {
|
---|
579 | bio_dsk_t __far *bios_dsk = read_word(0x0040, 0x000E) :> &EbdaData->bdisk;
|
---|
580 | ahci_t __far *ahci;
|
---|
581 |
|
---|
582 | /* Data out is currently not supported. */
|
---|
583 | if (inout == ATA_DATA_OUT) {
|
---|
584 | BX_INFO("%s: DATA_OUT not supported yet\n", __func__);
|
---|
585 | return 1;
|
---|
586 | }
|
---|
587 |
|
---|
588 | /* The skip length must be even. */
|
---|
589 | if (skip_b & 1) {
|
---|
590 | DBG_AHCI("%s: skip must be even (%04x)\n", __func__, skip_b);
|
---|
591 | return 1;
|
---|
592 | }
|
---|
593 |
|
---|
594 | /* Convert to AHCI specific device number. */
|
---|
595 | device_id = VBOX_GET_AHCI_DEVICE(device_id);
|
---|
596 |
|
---|
597 | DBG_AHCI("%s: reading %lu bytes, skip %u/%u, device %d, port %d\n", __func__,
|
---|
598 | length, bios_dsk->drqp.skip_b, bios_dsk->drqp.skip_a,
|
---|
599 | device_id, bios_dsk->ahcidev[device_id].port);
|
---|
600 | DBG_AHCI("%s: reading %u %u-byte sectors\n", __func__,
|
---|
601 | bios_dsk->drqp.nsect, bios_dsk->drqp.sect_sz);
|
---|
602 |
|
---|
603 | bios_dsk->drqp.lba = length << 8; /// @todo xfer length limit
|
---|
604 | bios_dsk->drqp.buffer = buffer;
|
---|
605 | bios_dsk->drqp.nsect = length / bios_dsk->drqp.sect_sz;
|
---|
606 | // bios_dsk->drqp.sect_sz = 2048;
|
---|
607 |
|
---|
608 | ahci = bios_dsk->ahci_seg :> 0;
|
---|
609 | high_bits_save(ahci);
|
---|
610 |
|
---|
611 | ahci_port_init(bios_dsk->ahci_seg :> 0, bios_dsk->ahcidev[device_id].port);
|
---|
612 |
|
---|
613 | /* Copy the ATAPI command where the HBA can fetch it. */
|
---|
614 | _fmemcpy(ahci->abAcmd, cmdbuf, cmdlen);
|
---|
615 |
|
---|
616 | /* Reset transferred counts. */
|
---|
617 | /// @todo clear in calling code?
|
---|
618 | bios_dsk->drqp.trsfsectors = 0;
|
---|
619 | bios_dsk->drqp.trsfbytes = 0;
|
---|
620 |
|
---|
621 | /* Set up a PRD entry to throw away the beginning of the transfer. */
|
---|
622 | if (bios_dsk->drqp.skip_b) {
|
---|
623 | ahci->aPrdt[0].len = bios_dsk->drqp.skip_b - 1;
|
---|
624 | ahci->aPrdt[0].phys_addr = ahci->sink_buf_phys;
|
---|
625 | ahci->cur_prd++;
|
---|
626 | }
|
---|
627 |
|
---|
628 | ahci_cmd_data(bios_dsk, ATA_CMD_PACKET);
|
---|
629 | DBG_AHCI("%s: transferred %lu bytes\n", __func__, ahci->aCmdHdr[1]);
|
---|
630 | bios_dsk->drqp.trsfbytes = ahci->aCmdHdr[1];
|
---|
631 | #ifdef DMA_WORKAROUND
|
---|
632 | rep_movsw(bios_dsk->drqp.buffer, bios_dsk->drqp.buffer, bios_dsk->drqp.trsfbytes / 2);
|
---|
633 | #endif
|
---|
634 | high_bits_restore(ahci);
|
---|
635 |
|
---|
636 | return ahci->aCmdHdr[1] == 0 ? 4 : 0;
|
---|
637 | }
|
---|
638 |
|
---|
639 | /* Wait for the specified number of BIOS timer ticks or data bytes. */
|
---|
640 | void wait_ticks_device_init( unsigned wait_ticks, unsigned wait_bytes )
|
---|
641 | {
|
---|
642 | }
|
---|
643 |
|
---|
644 | void ahci_port_detect_device(ahci_t __far *ahci, uint8_t u8Port)
|
---|
645 | {
|
---|
646 | uint32_t val;
|
---|
647 | bio_dsk_t __far *bios_dsk;
|
---|
648 | volatile uint32_t __far *ticks;
|
---|
649 | uint32_t end_tick;
|
---|
650 | int device_found = 0;
|
---|
651 |
|
---|
652 | ahci_port_init(ahci, u8Port);
|
---|
653 |
|
---|
654 | bios_dsk = read_word(0x0040, 0x000E) :> &EbdaData->bdisk;
|
---|
655 |
|
---|
656 | /* Reset connection. */
|
---|
657 | VBOXAHCI_PORT_WRITE_REG(ahci->iobase, u8Port, AHCI_REG_PORT_SCTL, 0x01);
|
---|
658 | /*
|
---|
659 | * According to the spec we should wait at least 1msec until the reset
|
---|
660 | * is cleared but this is a virtual controller so we don't have to.
|
---|
661 | */
|
---|
662 | VBOXAHCI_PORT_WRITE_REG(ahci->iobase, u8Port, AHCI_REG_PORT_SCTL, 0);
|
---|
663 |
|
---|
664 | /*
|
---|
665 | * We do however have to wait for the device to initialize (the port reset
|
---|
666 | * to complete). That can take up to 10ms according to the SATA spec (device
|
---|
667 | * must send COMINIT within 10ms of COMRESET). We should be generous with
|
---|
668 | * the wait because in the typical case there are no ports without a device
|
---|
669 | * attached.
|
---|
670 | */
|
---|
671 | ticks = MK_FP( 0x40, 0x6C );
|
---|
672 | end_tick = *ticks + 3; /* Wait up to five BIOS ticks, something in 150ms range. */
|
---|
673 |
|
---|
674 | while( *ticks < end_tick )
|
---|
675 | {
|
---|
676 | /* If PxSSTS.DET is 3, everything went fine. */
|
---|
677 | VBOXAHCI_PORT_READ_REG(ahci->iobase, u8Port, AHCI_REG_PORT_SSTS, val);
|
---|
678 | if (ahci_ctrl_extract_bits(val, 0xfL, 0) == 3) {
|
---|
679 | device_found = 1;
|
---|
680 | break;
|
---|
681 | }
|
---|
682 | }
|
---|
683 |
|
---|
684 | /* Timed out, no device detected. */
|
---|
685 | if (!device_found) {
|
---|
686 | DBG_AHCI("AHCI: Timed out, no device detected on port %d\n", u8Port);
|
---|
687 | return;
|
---|
688 | }
|
---|
689 |
|
---|
690 | if (ahci_ctrl_extract_bits(val, 0xfL, 0) == 0x3)
|
---|
691 | {
|
---|
692 | uint8_t abBuffer[0x0200];
|
---|
693 | uint8_t hdcount, devcount_ahci, hd_index;
|
---|
694 | uint8_t cdcount;
|
---|
695 | uint8_t removable;
|
---|
696 |
|
---|
697 | /* Clear all errors after the reset. */
|
---|
698 | VBOXAHCI_PORT_WRITE_REG(ahci->iobase, u8Port, AHCI_REG_PORT_SERR, 0xffffffff);
|
---|
699 |
|
---|
700 | devcount_ahci = bios_dsk->ahci_devcnt;
|
---|
701 |
|
---|
702 | DBG_AHCI("AHCI: Device detected on port %d\n", u8Port);
|
---|
703 |
|
---|
704 | /// @todo Merge common HD/CDROM detection code
|
---|
705 | if (devcount_ahci < BX_MAX_AHCI_DEVICES)
|
---|
706 | {
|
---|
707 | /* Device detected, enable FIS receive. */
|
---|
708 | ahci_ctrl_set_bits(ahci->iobase, AHCI_PORT_REG(u8Port, AHCI_REG_PORT_CMD),
|
---|
709 | AHCI_REG_PORT_CMD_FRE);
|
---|
710 |
|
---|
711 | /* Check signature to determine device type. */
|
---|
712 | VBOXAHCI_PORT_READ_REG(ahci->iobase, u8Port, AHCI_REG_PORT_SIG, val);
|
---|
713 | if (val == 0x101)
|
---|
714 | {
|
---|
715 | uint64_t sectors;
|
---|
716 | uint16_t cylinders, heads, spt;
|
---|
717 | chs_t lgeo;
|
---|
718 | uint8_t idxCmosChsBase;
|
---|
719 |
|
---|
720 | DBG_AHCI("AHCI: Detected hard disk\n");
|
---|
721 |
|
---|
722 | /* Identify device. */
|
---|
723 | bios_dsk->drqp.lba = 0;
|
---|
724 | bios_dsk->drqp.buffer = &abBuffer;
|
---|
725 | bios_dsk->drqp.nsect = 1;
|
---|
726 | bios_dsk->drqp.sect_sz = 512;
|
---|
727 | ahci_cmd_data(bios_dsk, ATA_CMD_IDENTIFY_DEVICE);
|
---|
728 |
|
---|
729 | /* Calculate index into the generic device table. */
|
---|
730 | hd_index = devcount_ahci + BX_MAX_ATA_DEVICES + BX_MAX_SCSI_DEVICES;
|
---|
731 |
|
---|
732 | removable = *(abBuffer+0) & 0x80 ? 1 : 0;
|
---|
733 | cylinders = *(uint16_t *)(abBuffer+(1*2)); // word 1
|
---|
734 | heads = *(uint16_t *)(abBuffer+(3*2)); // word 3
|
---|
735 | spt = *(uint16_t *)(abBuffer+(6*2)); // word 6
|
---|
736 | sectors = *(uint32_t *)(abBuffer+(60*2)); // word 60 and word 61
|
---|
737 |
|
---|
738 | if (sectors == 0x0FFFFFFF) /* For disks bigger than ~128GB */
|
---|
739 | sectors = *(uint64_t *)(abBuffer+(100*2)); // words 100 to 103
|
---|
740 |
|
---|
741 | DBG_AHCI("AHCI: 0x%llx sectors\n", sectors);
|
---|
742 |
|
---|
743 | bios_dsk->ahcidev[devcount_ahci].port = u8Port;
|
---|
744 | bios_dsk->devices[hd_index].type = DSK_TYPE_AHCI;
|
---|
745 | bios_dsk->devices[hd_index].device = DSK_DEVICE_HD;
|
---|
746 | bios_dsk->devices[hd_index].removable = removable;
|
---|
747 | bios_dsk->devices[hd_index].lock = 0;
|
---|
748 | bios_dsk->devices[hd_index].blksize = 512;
|
---|
749 | bios_dsk->devices[hd_index].translation = GEO_TRANSLATION_LBA;
|
---|
750 | bios_dsk->devices[hd_index].sectors = sectors;
|
---|
751 |
|
---|
752 | bios_dsk->devices[hd_index].pchs.heads = heads;
|
---|
753 | bios_dsk->devices[hd_index].pchs.cylinders = cylinders;
|
---|
754 | bios_dsk->devices[hd_index].pchs.spt = spt;
|
---|
755 |
|
---|
756 | /* Get logical CHS geometry. */
|
---|
757 | switch (devcount_ahci)
|
---|
758 | {
|
---|
759 | case 0:
|
---|
760 | idxCmosChsBase = 0x40;
|
---|
761 | break;
|
---|
762 | case 1:
|
---|
763 | idxCmosChsBase = 0x48;
|
---|
764 | break;
|
---|
765 | case 2:
|
---|
766 | idxCmosChsBase = 0x50;
|
---|
767 | break;
|
---|
768 | case 3:
|
---|
769 | idxCmosChsBase = 0x58;
|
---|
770 | break;
|
---|
771 | default:
|
---|
772 | idxCmosChsBase = 0;
|
---|
773 | }
|
---|
774 | if (idxCmosChsBase && inb_cmos(idxCmosChsBase+7))
|
---|
775 | {
|
---|
776 | lgeo.cylinders = inb_cmos(idxCmosChsBase + 0) + (inb_cmos(idxCmosChsBase + 1) << 8);
|
---|
777 | lgeo.heads = inb_cmos(idxCmosChsBase + 2);
|
---|
778 | lgeo.spt = inb_cmos(idxCmosChsBase + 7);
|
---|
779 | }
|
---|
780 | else
|
---|
781 | set_geom_lba(&lgeo, sectors); /* Default EDD-style translated LBA geometry. */
|
---|
782 |
|
---|
783 | BX_INFO("AHCI %d-P#%d: PCHS=%u/%u/%u LCHS=%u/%u/%u 0x%llx sectors\n", devcount_ahci,
|
---|
784 | u8Port, cylinders, heads, spt, lgeo.cylinders, lgeo.heads, lgeo.spt,
|
---|
785 | sectors);
|
---|
786 |
|
---|
787 | bios_dsk->devices[hd_index].lchs = lgeo;
|
---|
788 |
|
---|
789 | /* Store the ID of the disk in the BIOS hdidmap. */
|
---|
790 | hdcount = bios_dsk->hdcount;
|
---|
791 | bios_dsk->hdidmap[hdcount] = devcount_ahci + BX_MAX_ATA_DEVICES + BX_MAX_SCSI_DEVICES;
|
---|
792 | hdcount++;
|
---|
793 | bios_dsk->hdcount = hdcount;
|
---|
794 |
|
---|
795 | /* Update hdcount in the BDA. */
|
---|
796 | hdcount = read_byte(0x40, 0x75);
|
---|
797 | hdcount++;
|
---|
798 | write_byte(0x40, 0x75, hdcount);
|
---|
799 | }
|
---|
800 | else if (val == 0xeb140101)
|
---|
801 | {
|
---|
802 | DBG_AHCI("AHCI: Detected ATAPI device\n");
|
---|
803 |
|
---|
804 | /* Identify packet device. */
|
---|
805 | bios_dsk->drqp.lba = 0;
|
---|
806 | bios_dsk->drqp.buffer = &abBuffer;
|
---|
807 | bios_dsk->drqp.nsect = 1;
|
---|
808 | bios_dsk->drqp.sect_sz = 512;
|
---|
809 | ahci_cmd_data(bios_dsk, ATA_CMD_IDENTIFY_PACKET);
|
---|
810 |
|
---|
811 | /* Calculate index into the generic device table. */
|
---|
812 | hd_index = devcount_ahci + BX_MAX_ATA_DEVICES + BX_MAX_SCSI_DEVICES;
|
---|
813 |
|
---|
814 | removable = *(abBuffer+0) & 0x80 ? 1 : 0;
|
---|
815 |
|
---|
816 | bios_dsk->ahcidev[devcount_ahci].port = u8Port;
|
---|
817 | bios_dsk->devices[hd_index].type = DSK_TYPE_AHCI;
|
---|
818 | bios_dsk->devices[hd_index].device = DSK_DEVICE_CDROM;
|
---|
819 | bios_dsk->devices[hd_index].removable = removable;
|
---|
820 | bios_dsk->devices[hd_index].blksize = 2048;
|
---|
821 |
|
---|
822 | /* Store the ID of the device in the BIOS cdidmap. */
|
---|
823 | cdcount = bios_dsk->cdcount;
|
---|
824 | bios_dsk->cdidmap[cdcount] = devcount_ahci + BX_MAX_ATA_DEVICES + BX_MAX_SCSI_DEVICES;
|
---|
825 | cdcount++;
|
---|
826 | bios_dsk->cdcount = cdcount;
|
---|
827 | }
|
---|
828 | else
|
---|
829 | DBG_AHCI("AHCI: Ignoring unknown device\n");
|
---|
830 |
|
---|
831 | devcount_ahci++;
|
---|
832 | bios_dsk->ahci_devcnt = devcount_ahci;
|
---|
833 | }
|
---|
834 | else
|
---|
835 | DBG_AHCI("AHCI: Reached maximum device count, skipping\n");
|
---|
836 | }
|
---|
837 | }
|
---|
838 |
|
---|
839 | /**
|
---|
840 | * Allocates 1K of conventional memory.
|
---|
841 | */
|
---|
842 | static uint16_t ahci_mem_alloc(void)
|
---|
843 | {
|
---|
844 | uint16_t base_mem_kb;
|
---|
845 | uint16_t ahci_seg;
|
---|
846 |
|
---|
847 | base_mem_kb = read_word(0x00, 0x0413);
|
---|
848 |
|
---|
849 | DBG_AHCI("AHCI: %dK of base mem\n", base_mem_kb);
|
---|
850 |
|
---|
851 | if (base_mem_kb == 0)
|
---|
852 | return 0;
|
---|
853 |
|
---|
854 | base_mem_kb--; /* Allocate one block. */
|
---|
855 | ahci_seg = (((uint32_t)base_mem_kb * 1024) >> 4); /* Calculate start segment. */
|
---|
856 |
|
---|
857 | write_word(0x00, 0x0413, base_mem_kb);
|
---|
858 |
|
---|
859 | return ahci_seg;
|
---|
860 | }
|
---|
861 |
|
---|
862 | /**
|
---|
863 | * Initializes the AHCI HBA and detects attached devices.
|
---|
864 | */
|
---|
865 | static int ahci_hba_init(uint16_t io_base)
|
---|
866 | {
|
---|
867 | uint8_t i, cPorts;
|
---|
868 | uint32_t val;
|
---|
869 | uint16_t ebda_seg;
|
---|
870 | uint16_t ahci_seg;
|
---|
871 | bio_dsk_t __far *bios_dsk;
|
---|
872 | ahci_t __far *ahci;
|
---|
873 |
|
---|
874 |
|
---|
875 | ebda_seg = read_word(0x0040, 0x000E);
|
---|
876 | bios_dsk = ebda_seg :> &EbdaData->bdisk;
|
---|
877 |
|
---|
878 | AHCI_READ_REG(io_base, AHCI_REG_VS, val);
|
---|
879 | DBG_AHCI("AHCI: Controller version: 0x%x (major) 0x%x (minor)\n",
|
---|
880 | ahci_ctrl_extract_bits(val, 0xffff0000, 16),
|
---|
881 | ahci_ctrl_extract_bits(val, 0x0000ffff, 0));
|
---|
882 |
|
---|
883 | /* Allocate 1K of base memory. */
|
---|
884 | ahci_seg = ahci_mem_alloc();
|
---|
885 | if (ahci_seg == 0)
|
---|
886 | {
|
---|
887 | DBG_AHCI("AHCI: Could not allocate 1K of memory, can't boot from controller\n");
|
---|
888 | return 0;
|
---|
889 | }
|
---|
890 | DBG_AHCI("AHCI: ahci_seg=%04x, size=%04x, pointer at EBDA:%04x (EBDA size=%04x)\n",
|
---|
891 | ahci_seg, sizeof(ahci_t), (uint16_t)&EbdaData->bdisk.ahci_seg, sizeof(ebda_data_t));
|
---|
892 |
|
---|
893 | bios_dsk->ahci_seg = ahci_seg;
|
---|
894 | bios_dsk->ahci_devcnt = 0;
|
---|
895 |
|
---|
896 | ahci = ahci_seg :> 0;
|
---|
897 | ahci->cur_port = 0xff;
|
---|
898 | ahci->iobase = io_base;
|
---|
899 |
|
---|
900 | /* Physical address of memory used for throwing away ATAPI data when reading 512-byte
|
---|
901 | * blocks from 2048-byte CD sectors.
|
---|
902 | */
|
---|
903 | ahci->sink_buf_phys = 0xCC000; /// @todo find some better place!
|
---|
904 |
|
---|
905 | /* Reset the controller. */
|
---|
906 | ahci_ctrl_set_bits(io_base, AHCI_REG_GHC, AHCI_GHC_HR);
|
---|
907 | do
|
---|
908 | {
|
---|
909 | AHCI_READ_REG(io_base, AHCI_REG_GHC, val);
|
---|
910 | } while ((val & AHCI_GHC_HR) != 0);
|
---|
911 |
|
---|
912 | AHCI_READ_REG(io_base, AHCI_REG_CAP, val);
|
---|
913 | cPorts = ahci_ctrl_extract_bits(val, 0x1f, 0) + 1; /* Extract number of ports.*/
|
---|
914 |
|
---|
915 | DBG_AHCI("AHCI: HBA has %u ports\n", cPorts);
|
---|
916 |
|
---|
917 | /* Go through the ports. */
|
---|
918 | i = 0;
|
---|
919 | while (i < 32)
|
---|
920 | {
|
---|
921 | if (ahci_ctrl_is_bit_set(io_base, AHCI_REG_PI, RT_BIT_32(i)) != 0)
|
---|
922 | {
|
---|
923 | DBG_AHCI("AHCI: Port %u is present\n", i);
|
---|
924 | ahci_port_detect_device(ahci_seg :> 0, i);
|
---|
925 | cPorts--;
|
---|
926 | if (cPorts == 0)
|
---|
927 | break;
|
---|
928 | }
|
---|
929 | i++;
|
---|
930 | }
|
---|
931 |
|
---|
932 | return 0;
|
---|
933 | }
|
---|
934 |
|
---|
935 | /**
|
---|
936 | * Init the AHCI driver and detect attached disks.
|
---|
937 | */
|
---|
938 | void BIOSCALL ahci_init(void)
|
---|
939 | {
|
---|
940 | uint16_t busdevfn;
|
---|
941 |
|
---|
942 | busdevfn = pci_find_classcode(0x00010601);
|
---|
943 | if (busdevfn != VBOX_AHCI_NO_DEVICE)
|
---|
944 | {
|
---|
945 | uint8_t u8Bus, u8DevFn;
|
---|
946 | uint8_t u8PciCapOff;
|
---|
947 |
|
---|
948 | u8Bus = (busdevfn & 0xff00) >> 8;
|
---|
949 | u8DevFn = busdevfn & 0x00ff;
|
---|
950 |
|
---|
951 | DBG_AHCI("AHCI HBA at Bus %u DevFn 0x%x (raw 0x%x)\n", u8Bus, u8DevFn, busdevfn);
|
---|
952 |
|
---|
953 | /* Examine the capability list and search for the Serial ATA Capability Register. */
|
---|
954 | u8PciCapOff = pci_read_config_byte(u8Bus, u8DevFn, PCI_CONFIG_CAP);
|
---|
955 |
|
---|
956 | while (u8PciCapOff != 0)
|
---|
957 | {
|
---|
958 | uint8_t u8PciCapId = pci_read_config_byte(u8Bus, u8DevFn, u8PciCapOff);
|
---|
959 |
|
---|
960 | DBG_AHCI("Capability ID 0x%x at 0x%x\n", u8PciCapId, u8PciCapOff);
|
---|
961 |
|
---|
962 | if (u8PciCapId == PCI_CAP_ID_SATACR)
|
---|
963 | break;
|
---|
964 |
|
---|
965 | /* Go on to the next capability. */
|
---|
966 | u8PciCapOff = pci_read_config_byte(u8Bus, u8DevFn, u8PciCapOff + 1);
|
---|
967 | }
|
---|
968 |
|
---|
969 | if (u8PciCapOff != 0)
|
---|
970 | {
|
---|
971 | uint8_t u8Rev;
|
---|
972 |
|
---|
973 | DBG_AHCI("AHCI HBA with SATA Capability register at 0x%x\n", u8PciCapOff);
|
---|
974 |
|
---|
975 | /* Advance to the stuff behind the id and next capability pointer. */
|
---|
976 | u8PciCapOff += 2;
|
---|
977 |
|
---|
978 | u8Rev = pci_read_config_byte(u8Bus, u8DevFn, u8PciCapOff);
|
---|
979 | if (u8Rev == 0x10)
|
---|
980 | {
|
---|
981 | /* Read the SATACR1 register and get the bar and offset of the index/data pair register. */
|
---|
982 | uint8_t u8Bar = 0x00;
|
---|
983 | uint16_t u16Off = 0x00;
|
---|
984 | uint16_t u16BarOff = pci_read_config_word(u8Bus, u8DevFn, u8PciCapOff + 2);
|
---|
985 |
|
---|
986 | DBG_AHCI("SATACR1: 0x%x\n", u16BarOff);
|
---|
987 |
|
---|
988 | switch (u16BarOff & 0xf)
|
---|
989 | {
|
---|
990 | case 0x04:
|
---|
991 | u8Bar = 0x10;
|
---|
992 | break;
|
---|
993 | case 0x05:
|
---|
994 | u8Bar = 0x14;
|
---|
995 | break;
|
---|
996 | case 0x06:
|
---|
997 | u8Bar = 0x18;
|
---|
998 | break;
|
---|
999 | case 0x07:
|
---|
1000 | u8Bar = 0x1c;
|
---|
1001 | break;
|
---|
1002 | case 0x08:
|
---|
1003 | u8Bar = 0x20;
|
---|
1004 | break;
|
---|
1005 | case 0x09:
|
---|
1006 | u8Bar = 0x24;
|
---|
1007 | break;
|
---|
1008 | case 0x0f:
|
---|
1009 | default:
|
---|
1010 | /* Reserved or unsupported. */
|
---|
1011 | DBG_AHCI("BAR 0x%x unsupported\n", u16BarOff & 0xf);
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 | /* Get the offset inside the BAR from bits 4:15. */
|
---|
1015 | u16Off = (u16BarOff >> 4) * 4;
|
---|
1016 |
|
---|
1017 | if (u8Bar != 0x00)
|
---|
1018 | {
|
---|
1019 | uint32_t u32Bar = pci_read_config_dword(u8Bus, u8DevFn, u8Bar);
|
---|
1020 |
|
---|
1021 | DBG_AHCI("BAR at 0x%x : 0x%x\n", u8Bar, u32Bar);
|
---|
1022 |
|
---|
1023 | if ((u32Bar & 0x01) != 0)
|
---|
1024 | {
|
---|
1025 | int rc;
|
---|
1026 | uint16_t u16AhciIoBase = (u32Bar & 0xfff0) + u16Off;
|
---|
1027 |
|
---|
1028 | /* Enable PCI memory, I/O, bus mastering access in command register. */
|
---|
1029 | pci_write_config_word(u8Bus, u8DevFn, 4, 0x7);
|
---|
1030 |
|
---|
1031 | DBG_AHCI("I/O base: 0x%x\n", u16AhciIoBase);
|
---|
1032 | rc = ahci_hba_init(u16AhciIoBase);
|
---|
1033 | }
|
---|
1034 | else
|
---|
1035 | DBG_AHCI("BAR is MMIO\n");
|
---|
1036 | }
|
---|
1037 | }
|
---|
1038 | else
|
---|
1039 | DBG_AHCI("Invalid revision 0x%x\n", u8Rev);
|
---|
1040 | }
|
---|
1041 | else
|
---|
1042 | DBG_AHCI("AHCI HBA with no usable Index/Data register pair!\n");
|
---|
1043 | }
|
---|
1044 | else
|
---|
1045 | DBG_AHCI("No AHCI HBA!\n");
|
---|
1046 | }
|
---|