blob: dd412a473897f7ad4412b73c8f7466b5016a5a51 [file] [log] [blame]
Andrew Jeffery4fe996c2018-02-27 12:16:48 +10301// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2018 IBM Corp.
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +11003
4#define _GNU_SOURCE
5#include <assert.h>
6#include <errno.h>
7#include <fcntl.h>
8#include <getopt.h>
9#include <limits.h>
10#include <poll.h>
11#include <stdbool.h>
12#include <stdint.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <syslog.h>
17#include <signal.h>
18#include <sys/ioctl.h>
19#include <sys/mman.h>
20#include <sys/stat.h>
21#include <sys/timerfd.h>
22#include <sys/types.h>
23#include <time.h>
24#include <unistd.h>
25#include <inttypes.h>
26
27#include "mbox.h"
28#include "common.h"
Andrew Jeffery457a6e52018-08-08 11:21:08 +093029#include "transport_mbox.h"
Andrew Jefferyf593b1b2018-08-08 11:01:04 +093030#include "windows.h"
Andrew Jefferycd186112018-08-08 10:47:55 +093031#include "lpc.h"
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110032
Andrew Jeffery1e531af2018-08-07 13:32:57 +093033struct errno_map {
34 int rc;
35 int mbox_errno;
36};
37
38static const struct errno_map errno_map_v1[] = {
39 { 0, MBOX_R_SUCCESS },
40 { EACCES, MBOX_R_PARAM_ERROR },
Andrew Jeffery2f1477c2018-08-09 23:24:38 +093041 { EBADMSG, MBOX_R_PARAM_ERROR },
Andrew Jeffery1e531af2018-08-07 13:32:57 +093042 { EBUSY, MBOX_R_SYSTEM_ERROR },
43 { EINVAL, MBOX_R_PARAM_ERROR },
Andrew Jeffery2f1477c2018-08-09 23:24:38 +093044 { ENOTSUP, MBOX_R_PARAM_ERROR },
Andrew Jeffery1e531af2018-08-07 13:32:57 +093045 { EPERM, MBOX_R_PARAM_ERROR },
Andrew Jeffery2f1477c2018-08-09 23:24:38 +093046 { EPROTO, MBOX_R_PARAM_ERROR },
Andrew Jeffery1e531af2018-08-07 13:32:57 +093047 { ETIMEDOUT, MBOX_R_TIMEOUT },
48 { -1, MBOX_R_SYSTEM_ERROR },
49};
50
51static const struct errno_map errno_map_v2[] = {
52 { 0, MBOX_R_SUCCESS },
Andrew Jefferyc7d19472018-08-08 11:43:08 +093053 { EACCES, MBOX_R_WINDOW_ERROR },
Andrew Jeffery2f1477c2018-08-09 23:24:38 +093054 { EBADMSG, MBOX_R_SEQ_ERROR },
Andrew Jeffery1e531af2018-08-07 13:32:57 +093055 { EBUSY, MBOX_R_BUSY },
56 { EINVAL, MBOX_R_PARAM_ERROR },
Andrew Jeffery2f1477c2018-08-09 23:24:38 +093057 { ENOTSUP, MBOX_R_PARAM_ERROR },
Andrew Jeffery1e531af2018-08-07 13:32:57 +093058 { EPERM, MBOX_R_WINDOW_ERROR },
Andrew Jeffery2f1477c2018-08-09 23:24:38 +093059 { EPROTO, MBOX_R_PARAM_ERROR },
Andrew Jeffery1e531af2018-08-07 13:32:57 +093060 { ETIMEDOUT, MBOX_R_TIMEOUT },
61 { -1, MBOX_R_SYSTEM_ERROR },
62};
63
64static const struct errno_map *errno_maps[] = {
65 [0] = NULL,
66 [1] = errno_map_v1,
67 [2] = errno_map_v2,
68};
69
70static inline int mbox_xlate_errno(struct mbox_context *context,
71 int rc)
72{
73 const struct errno_map *entry;
74
75 rc = -rc;
Andrew Jeffery2f1477c2018-08-09 23:24:38 +093076 MSG_DBG("Translating errno %d: %s\n", rc, strerror(rc));
Andrew Jeffery1e531af2018-08-07 13:32:57 +093077 for(entry = errno_maps[context->version]; entry->rc != -1; entry++) {
78 if (rc == entry->rc) {
Andrew Jeffery2f1477c2018-08-09 23:24:38 +093079 return entry->mbox_errno;
Andrew Jeffery1e531af2018-08-07 13:32:57 +093080 }
81 }
82
Andrew Jeffery2f1477c2018-08-09 23:24:38 +093083 return entry->mbox_errno;
Andrew Jeffery1e531af2018-08-07 13:32:57 +093084}
85
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110086/*
Andrew Jeffery5335f092018-08-09 14:56:08 +093087 * transport_mbox_flush_events() - Write to the BMC controlled status register
88 * (reg 15)
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110089 * @context: The mbox context pointer
90 *
91 * Return: 0 on success otherwise negative error code
92 */
Andrew Jeffery5335f092018-08-09 14:56:08 +093093static int transport_mbox_flush_events(struct mbox_context *context)
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110094{
95 int rc;
96
97 /* Seek mbox registers */
98 rc = lseek(context->fds[MBOX_FD].fd, MBOX_BMC_EVENT, SEEK_SET);
99 if (rc != MBOX_BMC_EVENT) {
100 MSG_ERR("Couldn't lseek mbox to byte %d: %s\n", MBOX_BMC_EVENT,
101 strerror(errno));
Andrew Jeffery5335f092018-08-09 14:56:08 +0930102 return -errno;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100103 }
104
105 /* Write to mbox status register */
106 rc = write(context->fds[MBOX_FD].fd, &context->bmc_events, 1);
107 if (rc != 1) {
108 MSG_ERR("Couldn't write to BMC status reg: %s\n",
109 strerror(errno));
Andrew Jeffery5335f092018-08-09 14:56:08 +0930110 return -errno;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100111 }
112
113 /* Reset to start */
114 rc = lseek(context->fds[MBOX_FD].fd, 0, SEEK_SET);
115 if (rc) {
116 MSG_ERR("Couldn't reset MBOX offset to zero: %s\n",
117 strerror(errno));
Andrew Jeffery5335f092018-08-09 14:56:08 +0930118 return -errno;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100119 }
120
121 return 0;
122}
123
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100124/* Command Handlers */
125
126/*
127 * Command: RESET_STATE
Deepak Kodihalli017e45c2017-07-12 01:06:30 -0500128 * Reset the LPC mapping to point back at the flash, or memory in case we're
129 * using a virtual pnor.
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100130 */
Andrew Jefferyf21c81c2018-08-09 13:57:46 +0930131static int mbox_handle_reset(struct mbox_context *context,
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100132 union mbox_regs *req, struct mbox_msg *resp)
133{
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930134 return context->protocol->reset(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100135}
136
137/*
138 * Command: GET_MBOX_INFO
139 * Get the API version, default window size and block size
140 * We also set the LPC mapping to point to the reserved memory region here so
141 * this command must be called before any window manipulation
142 *
143 * V1:
144 * ARGS[0]: API Version
145 *
146 * RESP[0]: API Version
147 * RESP[1:2]: Default read window size (number of blocks)
148 * RESP[3:4]: Default write window size (number of blocks)
149 * RESP[5]: Block size (as shift)
150 *
151 * V2:
152 * ARGS[0]: API Version
153 *
154 * RESP[0]: API Version
155 * RESP[1:2]: Default read window size (number of blocks)
156 * RESP[3:4]: Default write window size (number of blocks)
157 * RESP[5]: Block size (as shift)
158 */
Andrew Jefferyf21c81c2018-08-09 13:57:46 +0930159static int mbox_handle_mbox_info(struct mbox_context *context,
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100160 union mbox_regs *req, struct mbox_msg *resp)
161{
162 uint8_t mbox_api_version = req->msg.args[0];
Andrew Jeffery1e531af2018-08-07 13:32:57 +0930163 struct protocol_get_info io = {
164 .req = { .api_version = mbox_api_version }
165 };
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100166 int rc;
167
Andrew Jeffery1e531af2018-08-07 13:32:57 +0930168 rc = context->protocol->get_info(context, &io);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100169 if (rc < 0) {
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930170 return rc;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100171 }
172
Andrew Jeffery1e531af2018-08-07 13:32:57 +0930173 resp->args[0] = io.resp.api_version;
174 if (io.resp.api_version == API_VERSION_1) {
175 put_u16(&resp->args[1], io.resp.v1.read_window_size);
176 put_u16(&resp->args[3], io.resp.v1.write_window_size);
177 } else if (io.resp.api_version >= API_VERSION_2) {
178 resp->args[5] = io.resp.v2.block_size_shift;
179 put_u16(&resp->args[6], io.resp.v2.timeout);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100180 }
181
182 return 0;
183}
184
185/*
186 * Command: GET_FLASH_INFO
187 * Get the flash size and erase granularity
188 *
189 * V1:
190 * RESP[0:3]: Flash Size (bytes)
191 * RESP[4:7]: Erase Size (bytes)
192 * V2:
193 * RESP[0:1]: Flash Size (number of blocks)
194 * RESP[2:3]: Erase Size (number of blocks)
195 */
Andrew Jefferyf21c81c2018-08-09 13:57:46 +0930196static int mbox_handle_flash_info(struct mbox_context *context,
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100197 union mbox_regs *req, struct mbox_msg *resp)
198{
Andrew Jeffery91a87452018-08-07 14:54:14 +0930199 struct protocol_get_flash_info io;
200 int rc;
201
202 rc = context->protocol->get_flash_info(context, &io);
203 if (rc < 0) {
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930204 return rc;
Andrew Jeffery91a87452018-08-07 14:54:14 +0930205 }
206
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100207 switch (context->version) {
208 case API_VERSION_1:
209 /* Both Sizes in Bytes */
Andrew Jeffery91a87452018-08-07 14:54:14 +0930210 put_u32(&resp->args[0], io.resp.v1.flash_size);
211 put_u32(&resp->args[4], io.resp.v1.erase_size);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100212 break;
213 case API_VERSION_2:
214 /* Both Sizes in Block Size */
Andrew Jeffery91a87452018-08-07 14:54:14 +0930215 put_u16(&resp->args[0], io.resp.v2.flash_size);
216 put_u16(&resp->args[2], io.resp.v2.erase_size);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100217 break;
218 default:
219 MSG_ERR("API Version Not Valid - Invalid System State\n");
220 return -MBOX_R_SYSTEM_ERROR;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100221 }
222
223 return 0;
224}
225
226/*
227 * get_lpc_addr_shifted() - Get lpc address of the current window
228 * @context: The mbox context pointer
229 *
230 * Return: The lpc address to access that offset shifted by block size
231 */
232static inline uint16_t get_lpc_addr_shifted(struct mbox_context *context)
233{
234 uint32_t lpc_addr, mem_offset;
235
236 /* Offset of the current window in the reserved memory region */
237 mem_offset = context->current->mem - context->mem;
238 /* Total LPC Address */
239 lpc_addr = context->lpc_base + mem_offset;
240
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000241 MSG_DBG("LPC address of current window: 0x%.8x\n", lpc_addr);
242
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100243 return lpc_addr >> context->block_size_shift;
244}
245
Andrew Jefferyf21c81c2018-08-09 13:57:46 +0930246static int mbox_handle_create_window(struct mbox_context *context, bool ro,
Andrew Jeffery4bcec8e2018-08-07 15:33:41 +0930247 union mbox_regs *req, struct mbox_msg *resp)
248{
249 struct protocol_create_window io;
250 int rc;
251
252 io.req.offset = get_u16(&req->msg.args[0]);
253 io.req.ro = ro;
254
255 rc = context->protocol->create_window(context, &io);
256 if (rc < 0) {
257 return rc;
258 }
259
260 put_u16(&resp->args[0], io.resp.lpc_address);
261 if (context->version >= API_VERSION_2) {
262 put_u16(&resp->args[2], io.resp.size);
263 put_u16(&resp->args[4], io.resp.offset);
264 }
265
266 return 0;
267}
268
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100269/*
270 * Command: CREATE_READ_WINDOW
271 * Opens a read window
272 * First checks if any current window with the requested data, if so we just
273 * point the host to that. Otherwise we read the request data in from flash and
274 * point the host there.
275 *
276 * V1:
277 * ARGS[0:1]: Window Location as Offset into Flash (number of blocks)
278 *
279 * RESP[0:1]: LPC bus address for host to access this window (number of blocks)
280 *
281 * V2:
282 * ARGS[0:1]: Window Location as Offset into Flash (number of blocks)
283 * ARGS[2:3]: Requested window size (number of blocks)
284 *
285 * RESP[0:1]: LPC bus address for host to access this window (number of blocks)
286 * RESP[2:3]: Actual window size that the host can access (number of blocks)
287 */
Andrew Jefferyf21c81c2018-08-09 13:57:46 +0930288static int mbox_handle_read_window(struct mbox_context *context,
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100289 union mbox_regs *req, struct mbox_msg *resp)
290{
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930291 return mbox_handle_create_window(context, true, req, resp);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100292}
293
294/*
295 * Command: CREATE_WRITE_WINDOW
296 * Opens a write window
297 * First checks if any current window with the requested data, if so we just
298 * point the host to that. Otherwise we read the request data in from flash and
299 * point the host there.
300 *
301 * V1:
302 * ARGS[0:1]: Window Location as Offset into Flash (number of blocks)
303 *
304 * RESP[0:1]: LPC bus address for host to access this window (number of blocks)
305 *
306 * V2:
307 * ARGS[0:1]: Window Location as Offset into Flash (number of blocks)
308 * ARGS[2:3]: Requested window size (number of blocks)
309 *
310 * RESP[0:1]: LPC bus address for host to access this window (number of blocks)
311 * RESP[2:3]: Actual window size that was mapped/host can access (n.o. blocks)
312 */
Andrew Jefferyf21c81c2018-08-09 13:57:46 +0930313static int mbox_handle_write_window(struct mbox_context *context,
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100314 union mbox_regs *req, struct mbox_msg *resp)
315{
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930316 return mbox_handle_create_window(context, false, req, resp);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100317}
318
319/*
320 * Commands: MARK_WRITE_DIRTY
321 * Marks a portion of the current (write) window dirty, informing the daemon
322 * that is has been written to and thus must be at some point written to the
323 * backing store
324 * These changes aren't written back to the backing store unless flush is then
325 * called or the window closed
326 *
327 * V1:
328 * ARGS[0:1]: Where within flash to start (number of blocks)
329 * ARGS[2:5]: Number to mark dirty (number of bytes)
330 *
331 * V2:
332 * ARGS[0:1]: Where within window to start (number of blocks)
333 * ARGS[2:3]: Number to mark dirty (number of blocks)
334 */
Andrew Jefferyf21c81c2018-08-09 13:57:46 +0930335static int mbox_handle_dirty_window(struct mbox_context *context,
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100336 union mbox_regs *req, struct mbox_msg *resp)
337{
Andrew Jefferya336e432018-08-07 16:00:40 +0930338 struct protocol_mark_dirty io;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100339
Andrew Jefferya336e432018-08-07 16:00:40 +0930340 if (context->version == API_VERSION_1) {
341 io.req.v1.offset = get_u16(&req->msg.args[0]);
342 io.req.v1.size = get_u32(&req->msg.args[2]);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100343 } else {
Andrew Jefferya336e432018-08-07 16:00:40 +0930344 io.req.v2.offset = get_u16(&req->msg.args[0]);
345 io.req.v2.size = get_u16(&req->msg.args[2]);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100346 }
347
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930348 return context->protocol->mark_dirty(context, &io);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100349}
350
351/*
352 * Commands: MARK_WRITE_ERASE
353 * Erases a portion of the current window
354 * These changes aren't written back to the backing store unless flush is then
355 * called or the window closed
356 *
357 * V1:
358 * Unimplemented
359 *
360 * V2:
361 * ARGS[0:1]: Where within window to start (number of blocks)
362 * ARGS[2:3]: Number to erase (number of blocks)
363 */
Andrew Jefferyf21c81c2018-08-09 13:57:46 +0930364static int mbox_handle_erase_window(struct mbox_context *context,
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100365 union mbox_regs *req, struct mbox_msg *resp)
366{
Andrew Jeffery62a3daa2018-08-07 22:30:32 +0930367 struct protocol_erase io;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100368
Andrew Jeffery62a3daa2018-08-07 22:30:32 +0930369 io.req.offset = get_u16(&req->msg.args[0]);
370 io.req.size = get_u16(&req->msg.args[2]);
371
372 if (!context->protocol->erase) {
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100373 MSG_ERR("Protocol Version invalid for Erase Command\n");
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930374 return -ENOTSUP;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100375 }
376
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930377 return context->protocol->erase(context, &io);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100378}
379
380/*
381 * Command: WRITE_FLUSH
382 * Flushes any dirty or erased blocks in the current window back to the backing
383 * store
384 * NOTE: For V1 this behaves much the same as the dirty command in that it
385 * takes an offset and number of blocks to dirty, then also performs a flush as
386 * part of the same command. For V2 this will only flush blocks already marked
387 * dirty/erased with the appropriate commands and doesn't take any arguments
388 * directly.
389 *
390 * V1:
391 * ARGS[0:1]: Where within window to start (number of blocks)
392 * ARGS[2:5]: Number to mark dirty (number of bytes)
393 *
394 * V2:
395 * NONE
396 */
Andrew Jefferyf21c81c2018-08-09 13:57:46 +0930397static int mbox_handle_flush_window(struct mbox_context *context,
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100398 union mbox_regs *req, struct mbox_msg *resp)
399{
Andrew Jeffery9b920cf2018-08-07 22:49:19 +0930400 struct protocol_flush io = { 0 };
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100401
Andrew Jeffery9b920cf2018-08-07 22:49:19 +0930402 if (context->version == API_VERSION_1) {
403 io.req.offset = get_u16(&req->msg.args[0]);
404 io.req.size = get_u32(&req->msg.args[2]);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100405 }
406
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930407 return context->protocol->flush(context, &io);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100408}
409
410/*
411 * Command: CLOSE_WINDOW
412 * Close the current window
413 * NOTE: There is an implicit flush
414 *
415 * V1:
416 * NONE
417 *
418 * V2:
419 * ARGS[0]: FLAGS
420 */
Andrew Jefferyf21c81c2018-08-09 13:57:46 +0930421static int mbox_handle_close_window(struct mbox_context *context,
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100422 union mbox_regs *req, struct mbox_msg *resp)
423{
Andrew Jeffery093eda52018-08-07 23:10:43 +0930424 struct protocol_close io = { 0 };
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100425
Andrew Jeffery093eda52018-08-07 23:10:43 +0930426 if (context->version >= API_VERSION_2) {
427 io.req.flags = req->msg.args[0];
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100428 }
429
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930430 return context->protocol->close(context, &io);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100431}
432
433/*
434 * Command: BMC_EVENT_ACK
435 * Sent by the host to acknowledge BMC events supplied in mailbox register 15
436 *
437 * ARGS[0]: Bitmap of bits to ack (by clearing)
438 */
Andrew Jefferyf21c81c2018-08-09 13:57:46 +0930439static int mbox_handle_ack(struct mbox_context *context, union mbox_regs *req,
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100440 struct mbox_msg *resp)
441{
Andrew Jefferyc5c83042018-08-07 23:22:05 +0930442 struct protocol_ack io;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100443
Andrew Jefferyc5c83042018-08-07 23:22:05 +0930444 io.req.flags = req->msg.args[0];
445
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930446 return context->protocol->ack(context, &io);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100447}
448
449/*
Andrew Jeffery55dede62017-04-24 16:13:06 +0930450 * check_req_valid() - Check if the given request is a valid mbox request
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100451 * @context: The mbox context pointer
Andrew Jeffery55dede62017-04-24 16:13:06 +0930452 * @cmd: The request registers
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100453 *
Andrew Jeffery55dede62017-04-24 16:13:06 +0930454 * Return: 0 if request is valid otherwise negative error code
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100455 */
Andrew Jeffery55dede62017-04-24 16:13:06 +0930456static int check_req_valid(struct mbox_context *context, union mbox_regs *req)
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100457{
Andrew Jeffery55dede62017-04-24 16:13:06 +0930458 uint8_t cmd = req->msg.command;
459 uint8_t seq = req->msg.seq;
460
461 if (cmd > NUM_MBOX_CMDS) {
Andrew Jeffery121dc0d2017-04-24 16:15:06 +0930462 MSG_ERR("Unknown mbox command: %d\n", cmd);
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930463 return -ENOTSUP;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100464 }
Andrew Jeffery121dc0d2017-04-24 16:15:06 +0930465
Andrew Jeffery55dede62017-04-24 16:13:06 +0930466 if (seq == context->prev_seq && cmd != MBOX_C_GET_MBOX_INFO) {
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000467 MSG_ERR("Invalid sequence number: %d, previous: %d\n", seq,
468 context->prev_seq);
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930469 return -EBADMSG;
Andrew Jeffery55dede62017-04-24 16:13:06 +0930470 }
471
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100472 if (context->state & STATE_SUSPENDED) {
473 if (cmd != MBOX_C_GET_MBOX_INFO && cmd != MBOX_C_ACK) {
474 MSG_ERR("Cannot use that cmd while suspended: %d\n",
475 cmd);
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930476 return -EBUSY;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100477 }
478 }
Andrew Jeffery121dc0d2017-04-24 16:15:06 +0930479
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100480 if (!(context->state & MAPS_MEM)) {
481 if (cmd != MBOX_C_RESET_STATE && cmd != MBOX_C_GET_MBOX_INFO
482 && cmd != MBOX_C_ACK) {
Andrew Jeffery121dc0d2017-04-24 16:15:06 +0930483 MSG_ERR("Must call GET_MBOX_INFO before %d\n", cmd);
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930484 return -EPROTO;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100485 }
486 }
487
488 return 0;
489}
490
Andrew Jeffery5335f092018-08-09 14:56:08 +0930491typedef int (*mboxd_mbox_handler)(struct mbox_context *, union mbox_regs *,
492 struct mbox_msg *);
493
494static const mboxd_mbox_handler transport_mbox_handlers[] = {
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100495 mbox_handle_reset,
496 mbox_handle_mbox_info,
497 mbox_handle_flash_info,
498 mbox_handle_read_window,
499 mbox_handle_close_window,
500 mbox_handle_write_window,
501 mbox_handle_dirty_window,
502 mbox_handle_flush_window,
503 mbox_handle_ack,
504 mbox_handle_erase_window
505};
506
507/*
508 * handle_mbox_req() - Handle an incoming mbox command request
509 * @context: The mbox context pointer
510 * @req: The mbox request message
511 *
512 * Return: 0 if handled successfully otherwise negative error code
513 */
514static int handle_mbox_req(struct mbox_context *context, union mbox_regs *req)
515{
516 struct mbox_msg resp = {
517 .command = req->msg.command,
518 .seq = req->msg.seq,
519 .args = { 0 },
520 .response = MBOX_R_SUCCESS
521 };
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000522 int rc = 0, len, i;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100523
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000524 MSG_INFO("Received MBOX command: %u\n", req->msg.command);
Andrew Jeffery55dede62017-04-24 16:13:06 +0930525 rc = check_req_valid(context, req);
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930526 if (!rc) {
Andrew Jeffery5335f092018-08-09 14:56:08 +0930527 mboxd_mbox_handler handler;
528
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100529 /* Commands start at 1 so we have to subtract 1 from the cmd */
Andrew Jeffery5335f092018-08-09 14:56:08 +0930530 handler = transport_mbox_handlers[req->msg.command - 1];
531 rc = handler(context, req, &resp);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100532 if (rc < 0) {
533 MSG_ERR("Error handling mbox cmd: %d\n",
534 req->msg.command);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100535 }
536 }
537
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930538 rc = mbox_xlate_errno(context, rc);
539 resp.response = rc;
Andrew Jeffery55dede62017-04-24 16:13:06 +0930540 context->prev_seq = req->msg.seq;
541
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000542 MSG_DBG("Writing MBOX response:\n");
543 MSG_DBG("MBOX cmd: %u\n", resp.command);
544 MSG_DBG("MBOX seq: %u\n", resp.seq);
545 for (i = 0; i < MBOX_ARGS_BYTES; i++) {
546 MSG_DBG("MBOX arg[%d]: 0x%.2x\n", i, resp.args[i]);
547 }
548 MSG_INFO("Writing MBOX response: %u\n", resp.response);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100549 len = write(context->fds[MBOX_FD].fd, &resp, sizeof(resp));
550 if (len < sizeof(resp)) {
551 MSG_ERR("Didn't write the full response\n");
552 rc = -errno;
553 }
554
555 return rc;
556}
557
558/*
559 * get_message() - Read an mbox request message from the mbox registers
560 * @context: The mbox context pointer
561 * @msg: Where to put the received message
562 *
563 * Return: 0 if read successfully otherwise negative error code
564 */
565static int get_message(struct mbox_context *context, union mbox_regs *msg)
566{
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000567 int rc, i;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100568
569 rc = read(context->fds[MBOX_FD].fd, msg, sizeof(msg->raw));
570 if (rc < 0) {
571 MSG_ERR("Couldn't read: %s\n", strerror(errno));
572 return -errno;
573 } else if (rc < sizeof(msg->raw)) {
574 MSG_ERR("Short read: %d expecting %zu\n", rc, sizeof(msg->raw));
575 return -1;
576 }
577
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000578 MSG_DBG("Received MBOX request:\n");
579 MSG_DBG("MBOX cmd: %u\n", msg->msg.command);
580 MSG_DBG("MBOX seq: %u\n", msg->msg.seq);
581 for (i = 0; i < MBOX_ARGS_BYTES; i++) {
582 MSG_DBG("MBOX arg[%d]: 0x%.2x\n", i, msg->msg.args[i]);
583 }
584
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100585 return 0;
586}
587
588/*
Andrew Jefferyd86141b2018-08-09 14:58:53 +0930589 * transport_mbox_dispatch() - handle an mbox interrupt
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100590 * @context: The mbox context pointer
591 *
592 * Return: 0 if handled successfully otherwise negative error code
593 */
Andrew Jefferyd86141b2018-08-09 14:58:53 +0930594int transport_mbox_dispatch(struct mbox_context *context)
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100595{
596 int rc = 0;
597 union mbox_regs req = { 0 };
598
599 assert(context);
600
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100601 rc = get_message(context, &req);
602 if (rc) {
603 return rc;
604 }
605
606 return handle_mbox_req(context, &req);
607}
608
Andrew Jeffery5335f092018-08-09 14:56:08 +0930609static const struct transport_ops transport_mbox_ops = {
610 .flush_events = transport_mbox_flush_events,
611};
612
Andrew Jefferyb2466ee2018-08-09 15:03:27 +0930613int __transport_mbox_init(struct mbox_context *context, const char *path)
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100614{
615 int fd;
616
Andrew Jeffery5335f092018-08-09 14:56:08 +0930617 context->transport = &transport_mbox_ops;
Andrew Jefferyefb09de2018-03-26 14:36:43 +1030618
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100619 /* Open MBOX Device */
Andrew Jeffery913740f2017-04-10 23:51:07 +0930620 fd = open(path, O_RDWR | O_NONBLOCK);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100621 if (fd < 0) {
622 MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n",
Andrew Jeffery913740f2017-04-10 23:51:07 +0930623 path, strerror(errno));
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100624 return -errno;
625 }
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000626 MSG_DBG("Opened mbox dev: %s\n", path);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100627
628 context->fds[MBOX_FD].fd = fd;
629
630 return 0;
631}
632
Andrew Jefferyb2466ee2018-08-09 15:03:27 +0930633int transport_mbox_init(struct mbox_context *context)
Andrew Jeffery913740f2017-04-10 23:51:07 +0930634{
Andrew Jefferyb2466ee2018-08-09 15:03:27 +0930635 return __transport_mbox_init(context, MBOX_HOST_PATH);
Andrew Jeffery913740f2017-04-10 23:51:07 +0930636}
637
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100638void free_mbox_dev(struct mbox_context *context)
639{
640 close(context->fds[MBOX_FD].fd);
641}