VirtualBox

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

Last change on this file since 46757 was 44529, checked in by vboxsync, 12 years ago

header (C) fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 32.9 KB
Line 
1/* $Id: ahci.c 44529 2013-02-04 15:54:15Z 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) == 0x3)
630 {
631 uint8_t abBuffer[0x0200];
632 uint8_t hdcount, devcount_ahci, hd_index;
633 uint8_t cdcount;
634 uint8_t removable;
635
636 devcount_ahci = bios_dsk->ahci_devcnt;
637
638 DBG_AHCI("AHCI: Device detected on port %d\n", u8Port);
639
640 //@todo: Merge common HD/CDROM detection code
641 if (devcount_ahci < BX_MAX_AHCI_DEVICES)
642 {
643 /* Device detected, enable FIS receive. */
644 ahci_ctrl_set_bits(ahci->iobase, AHCI_PORT_REG(u8Port, AHCI_REG_PORT_CMD),
645 AHCI_REG_PORT_CMD_FRE);
646
647 /* Check signature to determine device type. */
648 VBOXAHCI_PORT_READ_REG(ahci->iobase, u8Port, AHCI_REG_PORT_SIG, val);
649 if (val == 0x101)
650 {
651 uint32_t sectors;
652 uint16_t cylinders, heads, spt;
653 chs_t lgeo;
654 uint8_t idxCmosChsBase;
655
656 DBG_AHCI("AHCI: Detected hard disk\n");
657
658 /* Identify device. */
659 bios_dsk->drqp.lba = 0;
660 bios_dsk->drqp.buffer = &abBuffer;
661 bios_dsk->drqp.nsect = 1;
662 bios_dsk->drqp.sect_sz = 512;
663 ahci_cmd_data(bios_dsk, ATA_CMD_IDENTIFY_DEVICE);
664
665 /* Calculate index into the generic device table. */
666 hd_index = devcount_ahci + BX_MAX_ATA_DEVICES + BX_MAX_SCSI_DEVICES;
667
668 removable = *(abBuffer+0) & 0x80 ? 1 : 0;
669 cylinders = *(uint16_t *)(abBuffer+(1*2)); // word 1
670 heads = *(uint16_t *)(abBuffer+(3*2)); // word 3
671 spt = *(uint16_t *)(abBuffer+(6*2)); // word 6
672 sectors = *(uint32_t *)(abBuffer+(60*2)); // word 60 and word 61
673
674 /** @todo update sectors to be a 64 bit number (also lba...). */
675 if (sectors == 0x0FFFFFFF) /* For disks bigger than ~128GB */
676 sectors = *(uint32_t *)(abBuffer+(100*2)); // words 100 to 103 (someday)
677
678 DBG_AHCI("AHCI: %ld sectors\n", sectors);
679
680 bios_dsk->ahcidev[devcount_ahci].port = u8Port;
681 bios_dsk->devices[hd_index].type = DSK_TYPE_AHCI;
682 bios_dsk->devices[hd_index].device = DSK_DEVICE_HD;
683 bios_dsk->devices[hd_index].removable = removable;
684 bios_dsk->devices[hd_index].lock = 0;
685 bios_dsk->devices[hd_index].blksize = 512;
686 bios_dsk->devices[hd_index].translation = GEO_TRANSLATION_LBA;
687 bios_dsk->devices[hd_index].sectors = sectors;
688
689 bios_dsk->devices[hd_index].pchs.heads = heads;
690 bios_dsk->devices[hd_index].pchs.cylinders = cylinders;
691 bios_dsk->devices[hd_index].pchs.spt = spt;
692
693 /* Get logical CHS geometry. */
694 switch (devcount_ahci)
695 {
696 case 0:
697 idxCmosChsBase = 0x40;
698 break;
699 case 1:
700 idxCmosChsBase = 0x48;
701 break;
702 case 2:
703 idxCmosChsBase = 0x50;
704 break;
705 case 3:
706 idxCmosChsBase = 0x58;
707 break;
708 default:
709 idxCmosChsBase = 0;
710 }
711 if (idxCmosChsBase && inb_cmos(idxCmosChsBase+7))
712 {
713 lgeo.cylinders = inb_cmos(idxCmosChsBase + 0) + (inb_cmos(idxCmosChsBase + 1) << 8);
714 lgeo.heads = inb_cmos(idxCmosChsBase + 2);
715 lgeo.spt = inb_cmos(idxCmosChsBase + 7);
716 }
717 else
718 set_geom_lba(&lgeo, sectors); /* Default EDD-style translated LBA geometry. */
719
720 BX_INFO("AHCI %d-P#%d: PCHS=%u/%d/%d LCHS=%u/%u/%u %ld sectors\n", devcount_ahci,
721 u8Port, cylinders, heads, spt, lgeo.cylinders, lgeo.heads, lgeo.spt, sectors);
722
723 bios_dsk->devices[hd_index].lchs = lgeo;
724
725 /* Store the ID of the disk in the BIOS hdidmap. */
726 hdcount = bios_dsk->hdcount;
727 bios_dsk->hdidmap[hdcount] = devcount_ahci + BX_MAX_ATA_DEVICES + BX_MAX_SCSI_DEVICES;
728 hdcount++;
729 bios_dsk->hdcount = hdcount;
730
731 /* Update hdcount in the BDA. */
732 hdcount = read_byte(0x40, 0x75);
733 hdcount++;
734 write_byte(0x40, 0x75, hdcount);
735 }
736 else if (val == 0xeb140101)
737 {
738 DBG_AHCI("AHCI: Detected ATAPI device\n");
739
740 /* Identify packet device. */
741 bios_dsk->drqp.lba = 0;
742 bios_dsk->drqp.buffer = &abBuffer;
743 bios_dsk->drqp.nsect = 1;
744 bios_dsk->drqp.sect_sz = 512;
745 ahci_cmd_data(bios_dsk, ATA_CMD_IDENTIFY_PACKET);
746
747 /* Calculate index into the generic device table. */
748 hd_index = devcount_ahci + BX_MAX_ATA_DEVICES + BX_MAX_SCSI_DEVICES;
749
750 removable = *(abBuffer+0) & 0x80 ? 1 : 0;
751
752 bios_dsk->ahcidev[devcount_ahci].port = u8Port;
753 bios_dsk->devices[hd_index].type = DSK_TYPE_AHCI;
754 bios_dsk->devices[hd_index].device = DSK_DEVICE_CDROM;
755 bios_dsk->devices[hd_index].removable = removable;
756 bios_dsk->devices[hd_index].blksize = 2048;
757
758 /* Store the ID of the device in the BIOS cdidmap. */
759 cdcount = bios_dsk->cdcount;
760 bios_dsk->cdidmap[cdcount] = devcount_ahci + BX_MAX_ATA_DEVICES + BX_MAX_SCSI_DEVICES;
761 cdcount++;
762 bios_dsk->cdcount = cdcount;
763 }
764 else
765 DBG_AHCI("AHCI: Ignoring unknown device\n");
766
767 devcount_ahci++;
768 bios_dsk->ahci_devcnt = devcount_ahci;
769 }
770 else
771 DBG_AHCI("AHCI: Reached maximum device count, skipping\n");
772 }
773}
774
775/**
776 * Allocates 1K of conventional memory.
777 */
778static uint16_t ahci_mem_alloc(void)
779{
780 uint16_t base_mem_kb;
781 uint16_t ahci_seg;
782
783 base_mem_kb = read_word(0x00, 0x0413);
784
785 DBG_AHCI("AHCI: %dK of base mem\n", base_mem_kb);
786
787 if (base_mem_kb == 0)
788 return 0;
789
790 base_mem_kb--; /* Allocate one block. */
791 ahci_seg = (((uint32_t)base_mem_kb * 1024) >> 4); /* Calculate start segment. */
792
793 write_word(0x00, 0x0413, base_mem_kb);
794
795 return ahci_seg;
796}
797
798/**
799 * Initializes the AHCI HBA and detects attached devices.
800 */
801static int ahci_hba_init(uint16_t io_base)
802{
803 uint8_t i, cPorts;
804 uint32_t val;
805 uint16_t ebda_seg;
806 uint16_t ahci_seg;
807 bio_dsk_t __far *bios_dsk;
808 ahci_t __far *ahci;
809
810
811 ebda_seg = read_word(0x0040, 0x000E);
812 bios_dsk = ebda_seg :> &EbdaData->bdisk;
813
814 AHCI_READ_REG(io_base, AHCI_REG_VS, val);
815 DBG_AHCI("AHCI: Controller version: 0x%x (major) 0x%x (minor)\n",
816 ahci_ctrl_extract_bits(val, 0xffff0000, 16),
817 ahci_ctrl_extract_bits(val, 0x0000ffff, 0));
818
819 /* Allocate 1K of base memory. */
820 ahci_seg = ahci_mem_alloc();
821 if (ahci_seg == 0)
822 {
823 DBG_AHCI("AHCI: Could not allocate 1K of memory, can't boot from controller\n");
824 return 0;
825 }
826 DBG_AHCI("AHCI: ahci_seg=%04x, size=%04x, pointer at EBDA:%04x (EBDA size=%04x)\n",
827 ahci_seg, sizeof(ahci_t), (uint16_t)&EbdaData->bdisk.ahci_seg, sizeof(ebda_data_t));
828
829 bios_dsk->ahci_seg = ahci_seg;
830 bios_dsk->ahci_devcnt = 0;
831
832 ahci = ahci_seg :> 0;
833 ahci->cur_port = 0xff;
834 ahci->iobase = io_base;
835
836 /* Physical address of memory used for throwing away ATAPI data when reading 512-byte
837 * blocks from 2048-byte CD sectors.
838 */
839 ahci->sink_buf_phys = 0xCC000; //@todo: find some better place!
840
841 /* Reset the controller. */
842 ahci_ctrl_set_bits(io_base, AHCI_REG_GHC, AHCI_GHC_HR);
843 do
844 {
845 AHCI_READ_REG(io_base, AHCI_REG_GHC, val);
846 } while (val & AHCI_GHC_HR != 0);
847
848 AHCI_READ_REG(io_base, AHCI_REG_CAP, val);
849 cPorts = ahci_ctrl_extract_bits(val, 0x1f, 0) + 1; /* Extract number of ports.*/
850
851 DBG_AHCI("AHCI: HBA has %u ports\n", cPorts);
852
853 /* Go through the ports. */
854 i = 0;
855 while (i < 32)
856 {
857 if (ahci_ctrl_is_bit_set(io_base, AHCI_REG_PI, RT_BIT_32(i)) != 0)
858 {
859 DBG_AHCI("AHCI: Port %u is present\n", i);
860 ahci_port_detect_device(ahci_seg :> 0, i);
861 cPorts--;
862 if (cPorts == 0)
863 break;
864 }
865 i++;
866 }
867
868 return 0;
869}
870
871/**
872 * Init the AHCI driver and detect attached disks.
873 */
874void BIOSCALL ahci_init(void)
875{
876 uint16_t busdevfn;
877
878 busdevfn = pci_find_classcode(0x00010601);
879 if (busdevfn != VBOX_AHCI_NO_DEVICE)
880 {
881 uint8_t u8Bus, u8DevFn;
882 uint8_t u8PciCapOff;
883
884 u8Bus = (busdevfn & 0xff00) >> 8;
885 u8DevFn = busdevfn & 0x00ff;
886
887 DBG_AHCI("AHCI HBA at Bus %u DevFn 0x%x (raw 0x%x)\n", u8Bus, u8DevFn, busdevfn);
888
889 /* Examine the capability list and search for the Serial ATA Capability Register. */
890 u8PciCapOff = pci_read_config_byte(u8Bus, u8DevFn, PCI_CONFIG_CAP);
891
892 while (u8PciCapOff != 0)
893 {
894 uint8_t u8PciCapId = pci_read_config_byte(u8Bus, u8DevFn, u8PciCapOff);
895
896 DBG_AHCI("Capability ID 0x%x at 0x%x\n", u8PciCapId, u8PciCapOff);
897
898 if (u8PciCapId == PCI_CAP_ID_SATACR)
899 break;
900
901 /* Go on to the next capability. */
902 u8PciCapOff = pci_read_config_byte(u8Bus, u8DevFn, u8PciCapOff + 1);
903 }
904
905 if (u8PciCapOff != 0)
906 {
907 uint8_t u8Rev;
908
909 DBG_AHCI("AHCI HBA with SATA Capability register at 0x%x\n", u8PciCapOff);
910
911 /* Advance to the stuff behind the id and next capability pointer. */
912 u8PciCapOff += 2;
913
914 u8Rev = pci_read_config_byte(u8Bus, u8DevFn, u8PciCapOff);
915 if (u8Rev == 0x10)
916 {
917 /* Read the SATACR1 register and get the bar and offset of the index/data pair register. */
918 uint8_t u8Bar = 0x00;
919 uint16_t u16Off = 0x00;
920 uint16_t u16BarOff = pci_read_config_word(u8Bus, u8DevFn, u8PciCapOff + 2);
921
922 DBG_AHCI("SATACR1: 0x%x\n", u16BarOff);
923
924 switch (u16BarOff & 0xf)
925 {
926 case 0x04:
927 u8Bar = 0x10;
928 break;
929 case 0x05:
930 u8Bar = 0x14;
931 break;
932 case 0x06:
933 u8Bar = 0x18;
934 break;
935 case 0x07:
936 u8Bar = 0x1c;
937 break;
938 case 0x08:
939 u8Bar = 0x20;
940 break;
941 case 0x09:
942 u8Bar = 0x24;
943 break;
944 case 0x0f:
945 default:
946 /* Reserved or unsupported. */
947 DBG_AHCI("BAR 0x%x unsupported\n", u16BarOff & 0xf);
948 }
949
950 /* Get the offset inside the BAR from bits 4:15. */
951 u16Off = (u16BarOff >> 4) * 4;
952
953 if (u8Bar != 0x00)
954 {
955 uint32_t u32Bar = pci_read_config_dword(u8Bus, u8DevFn, u8Bar);
956
957 DBG_AHCI("BAR at 0x%x : 0x%x\n", u8Bar, u32Bar);
958
959 if ((u32Bar & 0x01) != 0)
960 {
961 int rc;
962 uint16_t u16AhciIoBase = (u32Bar & 0xfff0) + u16Off;
963
964 DBG_AHCI("I/O base: 0x%x\n", u16AhciIoBase);
965 rc = ahci_hba_init(u16AhciIoBase);
966 }
967 else
968 DBG_AHCI("BAR is MMIO\n");
969 }
970 }
971 else
972 DBG_AHCI("Invalid revision 0x%x\n", u8Rev);
973 }
974 else
975 DBG_AHCI("AHCI HBA with no usable Index/Data register pair!\n");
976 }
977 else
978 DBG_AHCI("No AHCI HBA!\n");
979}
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