blob: 11305c22c3a23faae253481e4c5074f5582526fb [file] [log] [blame]
Edward O'Callaghanefc58412014-01-23 08:30:42 +11001/*
2 * This file is part of the libpayload project.
3 *
4 * Copyright (C) 2012 secunet Security Networks AG
5 * Copyright (C) 2013 Edward O'Callaghan <eocallaghan@alterapraxis.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <stdlib.h>
32#include <stdint.h>
33#include <string.h>
34#include <libpayload.h>
35#include <pci.h>
36#include <storage/ata.h>
37#include <storage/ahci.h>
38
39
40#include "ahci_private.h"
41
42
43ssize_t ahci_packet_read_cmd(atapi_dev_t *const _dev,
44 const u8 *const cmd, const size_t cmdlen,
45 u8 *const buf, const size_t buflen)
46{
47 ahci_dev_t *const dev = (ahci_dev_t *)_dev;
48
49 if ((cmdlen != 12) && (cmdlen != 16)) {
50 printf("ahci: Only 12- and 16-byte packet commands allowed.\n");
51 return -1;
52 }
53
54 const size_t len = ahci_cmdslot_prepare(dev, buf, buflen, 0);
55 u16 byte_limit = MIN(len, 63 * 1024); /* like Linux */
56 if (byte_limit & 1) ++byte_limit; /* even limit */
57
58 dev->cmdlist[0].cmd |= CMD_ATAPI;
59 dev->cmdtable->fis[0] = FIS_HOST_TO_DEVICE;
60 dev->cmdtable->fis[1] = FIS_H2D_CMD;
61 dev->cmdtable->fis[2] = ATA_PACKET;
62 dev->cmdtable->fis[5] = byte_limit & 0xff;
63 dev->cmdtable->fis[6] = byte_limit >> 8;
64 memcpy((void *)dev->cmdtable->atapi_cmd, cmd, cmdlen);
65
66 return ahci_cmdslot_exec(dev);
67}