1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
/* Copyright 2016 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include <endian.h>
#include "libsbefifo.h"
#include "sbefifo_private.h"
static int sbefifo_read(struct sbefifo_context *sctx, void *buf, size_t *buflen)
{
ssize_t n;
assert(*buflen > 0);
n = read(sctx->fd, buf, *buflen);
if (n < 0)
return errno;
*buflen = n;
return 0;
}
static int sbefifo_write(struct sbefifo_context *sctx, void *buf, size_t buflen)
{
ssize_t n;
assert(buflen > 0);
n = write(sctx->fd, buf, buflen);
if (n < 0)
return errno;
if (n != buflen)
return EIO;
return 0;
}
static int sbefifo_transport(struct sbefifo_context *sctx, uint8_t *msg, uint32_t msg_len, uint8_t *out, uint32_t *out_len)
{
int rc;
size_t buflen;
buflen = msg_len;
rc = sbefifo_write(sctx, msg, buflen);
if (rc) {
LOG("write: cmd=%08x, rc=%d\n", be32toh(*(uint32_t *)(msg+4)), rc);
return rc;
}
buflen = *out_len;
rc = sbefifo_read(sctx, out, &buflen);
if (rc) {
LOG("read: cmd=%08x, buflen=%zu, rc=%d\n", be32toh(*(uint32_t *)(msg+4)), buflen, rc);
return rc;
}
*out_len = buflen;
return 0;
}
int sbefifo_parse_output(struct sbefifo_context *sctx, uint32_t cmd,
uint8_t *buf, uint32_t buflen,
uint8_t **out, uint32_t *out_len)
{
uint32_t offset_word, header_word, status_word;
uint32_t offset;
sbefifo_ffdc_clear(sctx);
/*
* At least 3 words are expected in response
* - header word
* - status word
* - header offset word
*/
if (buflen < 3 * sizeof(uint32_t)) {
LOG("reply: cmd=%08x, len=%u\n", cmd, buflen);
return EPROTO;
}
/* Last word is header offset (in words) */
offset_word = be32toh(*(uint32_t *) &buf[buflen-4]);
offset = buflen - (offset_word * 4);
*out_len = offset;
header_word = be32toh(*(uint32_t *) &buf[offset]);
offset += 4;
status_word = be32toh(*(uint32_t *) &buf[offset]);
offset += 4;
if (header_word != (0xc0de0000 | cmd)) {
LOG("reply: cmd=%08x, len=%u, header=%08x\n", cmd, buflen, header_word);
return EPROTO;
}
LOG("reply: cmd=%08x, len=%u, status=%08x\n", cmd, buflen, status_word);
if (status_word) {
sbefifo_ffdc_set(sctx, status_word, buf + offset, buflen - offset-4);
return ESBEFIFO;
}
if (*out_len > 0) {
*out = malloc(*out_len);
if (! *out)
return ENOMEM;
memcpy(*out, buf, *out_len);
} else {
*out = NULL;
}
return 0;
}
int sbefifo_operation(struct sbefifo_context *sctx,
uint8_t *msg, uint32_t msg_len,
uint8_t **out, uint32_t *out_len)
{
uint8_t *buf;
uint32_t buflen;
uint32_t cmd;
int rc;
assert(msg);
assert(msg_len > 0);
if (!sctx->transport && sctx->fd == -1)
return ENOTCONN;
/*
* Allocate extra memory for FFDC (SBEFIFO_MAX_FFDC_SIZE = 0x2000)
* Use *out_len as a hint to expected reply length
*/
buflen = (*out_len + 0x2000 + 3) & ~(uint32_t)3;
buf = malloc(buflen);
if (!buf)
return ENOMEM;
cmd = be32toh(*(uint32_t *)(msg + 4));
LOG("request: cmd=%08x, len=%u\n", cmd, msg_len);
if (sctx->transport)
rc = sctx->transport(msg, msg_len, buf, &buflen, sctx->priv);
else
rc = sbefifo_transport(sctx, msg, msg_len, buf, &buflen);
if (rc) {
free(buf);
if (rc == ETIMEDOUT) {
uint32_t status;
status = SBEFIFO_PRI_UNKNOWN_ERROR | SBEFIFO_SEC_HW_TIMEOUT;
sbefifo_ffdc_set(sctx, status, NULL, 0);
}
return rc;
}
rc = sbefifo_parse_output(sctx, cmd, buf, buflen, out, out_len);
free(buf);
return rc;
}
|