VirtualBox

source: vbox/trunk/src/VBox/Devices/PC/BIOS/ahci.c@ 53426

Last change on this file since 53426 was 52812, checked in by vboxsync, 10 years ago

BIOS: Fixed format specifiers.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette