blob: 3c2727ae6ddca2d55757df65da2444e9eaa1c163 [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
Andrew Jeffery26558db2018-08-10 00:22:38 +093027#include "mboxd.h"
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110028#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 Jefferyf62601b2018-11-01 13:44:25 +103093static int transport_mbox_flush_events(struct mbox_context *context, uint8_t events)
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 */
Andrew Jefferyf62601b2018-11-01 13:44:25 +1030106 rc = write(context->fds[MBOX_FD].fd, &events, 1);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100107 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
Andrew Jeffery4414fb82018-08-20 12:13:09 +0930124static int transport_mbox_set_events(struct mbox_context *context,
Andrew Jefferyf62601b2018-11-01 13:44:25 +1030125 uint8_t events, uint8_t mask)
Andrew Jeffery4414fb82018-08-20 12:13:09 +0930126{
Andrew Jefferyf62601b2018-11-01 13:44:25 +1030127 return transport_mbox_flush_events(context, context->bmc_events & mask);
Andrew Jeffery4414fb82018-08-20 12:13:09 +0930128}
129
130static int transport_mbox_clear_events(struct mbox_context *context,
Andrew Jefferyf62601b2018-11-01 13:44:25 +1030131 uint8_t events, uint8_t mask)
Andrew Jeffery4414fb82018-08-20 12:13:09 +0930132{
Andrew Jefferyf62601b2018-11-01 13:44:25 +1030133 return transport_mbox_flush_events(context, context->bmc_events & mask);
Andrew Jeffery4414fb82018-08-20 12:13:09 +0930134}
135
Andrew Jeffery23a48212018-08-10 14:41:48 +0930136static const struct transport_ops transport_mbox_ops = {
Andrew Jeffery4414fb82018-08-20 12:13:09 +0930137 .set_events = transport_mbox_set_events,
138 .clear_events = transport_mbox_clear_events,
Andrew Jeffery23a48212018-08-10 14:41:48 +0930139};
140
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100141/* Command Handlers */
142
143/*
144 * Command: RESET_STATE
Deepak Kodihalli017e45c2017-07-12 01:06:30 -0500145 * Reset the LPC mapping to point back at the flash, or memory in case we're
146 * using a virtual pnor.
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100147 */
Andrew Jefferyf21c81c2018-08-09 13:57:46 +0930148static int mbox_handle_reset(struct mbox_context *context,
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100149 union mbox_regs *req, struct mbox_msg *resp)
150{
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930151 return context->protocol->reset(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100152}
153
154/*
155 * Command: GET_MBOX_INFO
156 * Get the API version, default window size and block size
157 * We also set the LPC mapping to point to the reserved memory region here so
158 * this command must be called before any window manipulation
159 *
160 * V1:
161 * ARGS[0]: API Version
162 *
163 * RESP[0]: API Version
164 * RESP[1:2]: Default read window size (number of blocks)
165 * RESP[3:4]: Default write window size (number of blocks)
166 * RESP[5]: Block size (as shift)
167 *
168 * V2:
169 * ARGS[0]: API Version
170 *
171 * RESP[0]: API Version
172 * RESP[1:2]: Default read window size (number of blocks)
173 * RESP[3:4]: Default write window size (number of blocks)
174 * RESP[5]: Block size (as shift)
175 */
Andrew Jefferyf21c81c2018-08-09 13:57:46 +0930176static int mbox_handle_mbox_info(struct mbox_context *context,
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100177 union mbox_regs *req, struct mbox_msg *resp)
178{
179 uint8_t mbox_api_version = req->msg.args[0];
Andrew Jeffery1e531af2018-08-07 13:32:57 +0930180 struct protocol_get_info io = {
181 .req = { .api_version = mbox_api_version }
182 };
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100183 int rc;
184
Andrew Jeffery1e531af2018-08-07 13:32:57 +0930185 rc = context->protocol->get_info(context, &io);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100186 if (rc < 0) {
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930187 return rc;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100188 }
189
Andrew Jeffery23a48212018-08-10 14:41:48 +0930190 /*
191 * Switch transport to mbox, however we need to delay flushing the
192 * event state until after the command is processed.
193 */
194 context->transport = &transport_mbox_ops;
195
Andrew Jeffery1e531af2018-08-07 13:32:57 +0930196 resp->args[0] = io.resp.api_version;
197 if (io.resp.api_version == API_VERSION_1) {
198 put_u16(&resp->args[1], io.resp.v1.read_window_size);
199 put_u16(&resp->args[3], io.resp.v1.write_window_size);
200 } else if (io.resp.api_version >= API_VERSION_2) {
201 resp->args[5] = io.resp.v2.block_size_shift;
202 put_u16(&resp->args[6], io.resp.v2.timeout);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100203 }
204
205 return 0;
206}
207
208/*
209 * Command: GET_FLASH_INFO
210 * Get the flash size and erase granularity
211 *
212 * V1:
213 * RESP[0:3]: Flash Size (bytes)
214 * RESP[4:7]: Erase Size (bytes)
215 * V2:
216 * RESP[0:1]: Flash Size (number of blocks)
217 * RESP[2:3]: Erase Size (number of blocks)
218 */
Andrew Jefferyf21c81c2018-08-09 13:57:46 +0930219static int mbox_handle_flash_info(struct mbox_context *context,
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100220 union mbox_regs *req, struct mbox_msg *resp)
221{
Andrew Jeffery91a87452018-08-07 14:54:14 +0930222 struct protocol_get_flash_info io;
223 int rc;
224
225 rc = context->protocol->get_flash_info(context, &io);
226 if (rc < 0) {
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930227 return rc;
Andrew Jeffery91a87452018-08-07 14:54:14 +0930228 }
229
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100230 switch (context->version) {
231 case API_VERSION_1:
232 /* Both Sizes in Bytes */
Andrew Jeffery91a87452018-08-07 14:54:14 +0930233 put_u32(&resp->args[0], io.resp.v1.flash_size);
234 put_u32(&resp->args[4], io.resp.v1.erase_size);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100235 break;
236 case API_VERSION_2:
237 /* Both Sizes in Block Size */
Andrew Jeffery91a87452018-08-07 14:54:14 +0930238 put_u16(&resp->args[0], io.resp.v2.flash_size);
239 put_u16(&resp->args[2], io.resp.v2.erase_size);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100240 break;
241 default:
242 MSG_ERR("API Version Not Valid - Invalid System State\n");
243 return -MBOX_R_SYSTEM_ERROR;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100244 }
245
246 return 0;
247}
248
249/*
250 * get_lpc_addr_shifted() - Get lpc address of the current window
251 * @context: The mbox context pointer
252 *
253 * Return: The lpc address to access that offset shifted by block size
254 */
255static inline uint16_t get_lpc_addr_shifted(struct mbox_context *context)
256{
257 uint32_t lpc_addr, mem_offset;
258
259 /* Offset of the current window in the reserved memory region */
260 mem_offset = context->current->mem - context->mem;
261 /* Total LPC Address */
262 lpc_addr = context->lpc_base + mem_offset;
263
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000264 MSG_DBG("LPC address of current window: 0x%.8x\n", lpc_addr);
265
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100266 return lpc_addr >> context->block_size_shift;
267}
268
Andrew Jefferyf21c81c2018-08-09 13:57:46 +0930269static int mbox_handle_create_window(struct mbox_context *context, bool ro,
Andrew Jeffery4bcec8e2018-08-07 15:33:41 +0930270 union mbox_regs *req, struct mbox_msg *resp)
271{
272 struct protocol_create_window io;
273 int rc;
274
275 io.req.offset = get_u16(&req->msg.args[0]);
276 io.req.ro = ro;
277
278 rc = context->protocol->create_window(context, &io);
279 if (rc < 0) {
280 return rc;
281 }
282
283 put_u16(&resp->args[0], io.resp.lpc_address);
284 if (context->version >= API_VERSION_2) {
285 put_u16(&resp->args[2], io.resp.size);
286 put_u16(&resp->args[4], io.resp.offset);
287 }
288
289 return 0;
290}
291
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100292/*
293 * Command: CREATE_READ_WINDOW
294 * Opens a read window
295 * First checks if any current window with the requested data, if so we just
296 * point the host to that. Otherwise we read the request data in from flash and
297 * point the host there.
298 *
299 * V1:
300 * ARGS[0:1]: Window Location as Offset into Flash (number of blocks)
301 *
302 * RESP[0:1]: LPC bus address for host to access this window (number of blocks)
303 *
304 * V2:
305 * ARGS[0:1]: Window Location as Offset into Flash (number of blocks)
306 * ARGS[2:3]: Requested window size (number of blocks)
307 *
308 * RESP[0:1]: LPC bus address for host to access this window (number of blocks)
309 * RESP[2:3]: Actual window size that the host can access (number of blocks)
310 */
Andrew Jefferyf21c81c2018-08-09 13:57:46 +0930311static int mbox_handle_read_window(struct mbox_context *context,
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100312 union mbox_regs *req, struct mbox_msg *resp)
313{
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930314 return mbox_handle_create_window(context, true, req, resp);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100315}
316
317/*
318 * Command: CREATE_WRITE_WINDOW
319 * Opens a write window
320 * First checks if any current window with the requested data, if so we just
321 * point the host to that. Otherwise we read the request data in from flash and
322 * point the host there.
323 *
324 * V1:
325 * ARGS[0:1]: Window Location as Offset into Flash (number of blocks)
326 *
327 * RESP[0:1]: LPC bus address for host to access this window (number of blocks)
328 *
329 * V2:
330 * ARGS[0:1]: Window Location as Offset into Flash (number of blocks)
331 * ARGS[2:3]: Requested window size (number of blocks)
332 *
333 * RESP[0:1]: LPC bus address for host to access this window (number of blocks)
334 * RESP[2:3]: Actual window size that was mapped/host can access (n.o. blocks)
335 */
Andrew Jefferyf21c81c2018-08-09 13:57:46 +0930336static int mbox_handle_write_window(struct mbox_context *context,
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100337 union mbox_regs *req, struct mbox_msg *resp)
338{
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930339 return mbox_handle_create_window(context, false, req, resp);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100340}
341
342/*
343 * Commands: MARK_WRITE_DIRTY
344 * Marks a portion of the current (write) window dirty, informing the daemon
345 * that is has been written to and thus must be at some point written to the
346 * backing store
347 * These changes aren't written back to the backing store unless flush is then
348 * called or the window closed
349 *
350 * V1:
351 * ARGS[0:1]: Where within flash to start (number of blocks)
352 * ARGS[2:5]: Number to mark dirty (number of bytes)
353 *
354 * V2:
355 * ARGS[0:1]: Where within window to start (number of blocks)
356 * ARGS[2:3]: Number to mark dirty (number of blocks)
357 */
Andrew Jefferyf21c81c2018-08-09 13:57:46 +0930358static int mbox_handle_dirty_window(struct mbox_context *context,
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100359 union mbox_regs *req, struct mbox_msg *resp)
360{
Andrew Jefferya336e432018-08-07 16:00:40 +0930361 struct protocol_mark_dirty io;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100362
Andrew Jefferya336e432018-08-07 16:00:40 +0930363 if (context->version == API_VERSION_1) {
364 io.req.v1.offset = get_u16(&req->msg.args[0]);
365 io.req.v1.size = get_u32(&req->msg.args[2]);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100366 } else {
Andrew Jefferya336e432018-08-07 16:00:40 +0930367 io.req.v2.offset = get_u16(&req->msg.args[0]);
368 io.req.v2.size = get_u16(&req->msg.args[2]);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100369 }
370
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930371 return context->protocol->mark_dirty(context, &io);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100372}
373
374/*
375 * Commands: MARK_WRITE_ERASE
376 * Erases a portion of the current window
377 * These changes aren't written back to the backing store unless flush is then
378 * called or the window closed
379 *
380 * V1:
381 * Unimplemented
382 *
383 * V2:
384 * ARGS[0:1]: Where within window to start (number of blocks)
385 * ARGS[2:3]: Number to erase (number of blocks)
386 */
Andrew Jefferyf21c81c2018-08-09 13:57:46 +0930387static int mbox_handle_erase_window(struct mbox_context *context,
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100388 union mbox_regs *req, struct mbox_msg *resp)
389{
Andrew Jeffery62a3daa2018-08-07 22:30:32 +0930390 struct protocol_erase io;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100391
Andrew Jeffery62a3daa2018-08-07 22:30:32 +0930392 io.req.offset = get_u16(&req->msg.args[0]);
393 io.req.size = get_u16(&req->msg.args[2]);
394
395 if (!context->protocol->erase) {
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100396 MSG_ERR("Protocol Version invalid for Erase Command\n");
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930397 return -ENOTSUP;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100398 }
399
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930400 return context->protocol->erase(context, &io);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100401}
402
403/*
404 * Command: WRITE_FLUSH
405 * Flushes any dirty or erased blocks in the current window back to the backing
406 * store
407 * NOTE: For V1 this behaves much the same as the dirty command in that it
408 * takes an offset and number of blocks to dirty, then also performs a flush as
409 * part of the same command. For V2 this will only flush blocks already marked
410 * dirty/erased with the appropriate commands and doesn't take any arguments
411 * directly.
412 *
413 * V1:
414 * ARGS[0:1]: Where within window to start (number of blocks)
415 * ARGS[2:5]: Number to mark dirty (number of bytes)
416 *
417 * V2:
418 * NONE
419 */
Andrew Jefferyf21c81c2018-08-09 13:57:46 +0930420static int mbox_handle_flush_window(struct mbox_context *context,
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100421 union mbox_regs *req, struct mbox_msg *resp)
422{
Andrew Jeffery9b920cf2018-08-07 22:49:19 +0930423 struct protocol_flush io = { 0 };
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100424
Andrew Jeffery9b920cf2018-08-07 22:49:19 +0930425 if (context->version == API_VERSION_1) {
426 io.req.offset = get_u16(&req->msg.args[0]);
427 io.req.size = get_u32(&req->msg.args[2]);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100428 }
429
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930430 return context->protocol->flush(context, &io);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100431}
432
433/*
434 * Command: CLOSE_WINDOW
435 * Close the current window
436 * NOTE: There is an implicit flush
437 *
438 * V1:
439 * NONE
440 *
441 * V2:
442 * ARGS[0]: FLAGS
443 */
Andrew Jefferyf21c81c2018-08-09 13:57:46 +0930444static int mbox_handle_close_window(struct mbox_context *context,
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100445 union mbox_regs *req, struct mbox_msg *resp)
446{
Andrew Jeffery093eda52018-08-07 23:10:43 +0930447 struct protocol_close io = { 0 };
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100448
Andrew Jeffery093eda52018-08-07 23:10:43 +0930449 if (context->version >= API_VERSION_2) {
450 io.req.flags = req->msg.args[0];
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100451 }
452
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930453 return context->protocol->close(context, &io);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100454}
455
456/*
457 * Command: BMC_EVENT_ACK
458 * Sent by the host to acknowledge BMC events supplied in mailbox register 15
459 *
460 * ARGS[0]: Bitmap of bits to ack (by clearing)
461 */
Andrew Jefferyf21c81c2018-08-09 13:57:46 +0930462static int mbox_handle_ack(struct mbox_context *context, union mbox_regs *req,
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100463 struct mbox_msg *resp)
464{
Andrew Jefferyc5c83042018-08-07 23:22:05 +0930465 struct protocol_ack io;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100466
Andrew Jefferyc5c83042018-08-07 23:22:05 +0930467 io.req.flags = req->msg.args[0];
468
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930469 return context->protocol->ack(context, &io);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100470}
471
472/*
Andrew Jeffery55dede62017-04-24 16:13:06 +0930473 * check_req_valid() - Check if the given request is a valid mbox request
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100474 * @context: The mbox context pointer
Andrew Jeffery55dede62017-04-24 16:13:06 +0930475 * @cmd: The request registers
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100476 *
Andrew Jeffery55dede62017-04-24 16:13:06 +0930477 * Return: 0 if request is valid otherwise negative error code
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100478 */
Andrew Jeffery55dede62017-04-24 16:13:06 +0930479static int check_req_valid(struct mbox_context *context, union mbox_regs *req)
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100480{
Andrew Jeffery55dede62017-04-24 16:13:06 +0930481 uint8_t cmd = req->msg.command;
482 uint8_t seq = req->msg.seq;
483
484 if (cmd > NUM_MBOX_CMDS) {
Andrew Jeffery121dc0d2017-04-24 16:15:06 +0930485 MSG_ERR("Unknown mbox command: %d\n", cmd);
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930486 return -ENOTSUP;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100487 }
Andrew Jeffery121dc0d2017-04-24 16:15:06 +0930488
Andrew Jeffery55dede62017-04-24 16:13:06 +0930489 if (seq == context->prev_seq && cmd != MBOX_C_GET_MBOX_INFO) {
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000490 MSG_ERR("Invalid sequence number: %d, previous: %d\n", seq,
491 context->prev_seq);
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930492 return -EBADMSG;
Andrew Jeffery55dede62017-04-24 16:13:06 +0930493 }
494
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100495 if (context->state & STATE_SUSPENDED) {
496 if (cmd != MBOX_C_GET_MBOX_INFO && cmd != MBOX_C_ACK) {
497 MSG_ERR("Cannot use that cmd while suspended: %d\n",
498 cmd);
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930499 return -EBUSY;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100500 }
501 }
Andrew Jeffery121dc0d2017-04-24 16:15:06 +0930502
Andrew Jeffery23a48212018-08-10 14:41:48 +0930503 if (context->transport != &transport_mbox_ops) {
504 if (cmd != MBOX_C_RESET_STATE && cmd != MBOX_C_GET_MBOX_INFO) {
505 MSG_ERR("Cannot switch transport with command %d\n",
506 cmd);
507 return -EPROTO;
508 }
509 }
510
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100511 if (!(context->state & MAPS_MEM)) {
512 if (cmd != MBOX_C_RESET_STATE && cmd != MBOX_C_GET_MBOX_INFO
513 && cmd != MBOX_C_ACK) {
Andrew Jeffery121dc0d2017-04-24 16:15:06 +0930514 MSG_ERR("Must call GET_MBOX_INFO before %d\n", cmd);
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930515 return -EPROTO;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100516 }
517 }
518
519 return 0;
520}
521
Andrew Jeffery5335f092018-08-09 14:56:08 +0930522typedef int (*mboxd_mbox_handler)(struct mbox_context *, union mbox_regs *,
523 struct mbox_msg *);
524
525static const mboxd_mbox_handler transport_mbox_handlers[] = {
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100526 mbox_handle_reset,
527 mbox_handle_mbox_info,
528 mbox_handle_flash_info,
529 mbox_handle_read_window,
530 mbox_handle_close_window,
531 mbox_handle_write_window,
532 mbox_handle_dirty_window,
533 mbox_handle_flush_window,
534 mbox_handle_ack,
535 mbox_handle_erase_window
536};
537
538/*
539 * handle_mbox_req() - Handle an incoming mbox command request
540 * @context: The mbox context pointer
541 * @req: The mbox request message
542 *
543 * Return: 0 if handled successfully otherwise negative error code
544 */
545static int handle_mbox_req(struct mbox_context *context, union mbox_regs *req)
546{
Andrew Jeffery23a48212018-08-10 14:41:48 +0930547 const struct transport_ops *old_transport = context->transport;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100548 struct mbox_msg resp = {
549 .command = req->msg.command,
550 .seq = req->msg.seq,
551 .args = { 0 },
552 .response = MBOX_R_SUCCESS
553 };
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000554 int rc = 0, len, i;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100555
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000556 MSG_INFO("Received MBOX command: %u\n", req->msg.command);
Andrew Jeffery23a48212018-08-10 14:41:48 +0930557
Andrew Jeffery55dede62017-04-24 16:13:06 +0930558 rc = check_req_valid(context, req);
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930559 if (!rc) {
Andrew Jeffery5335f092018-08-09 14:56:08 +0930560 mboxd_mbox_handler handler;
561
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100562 /* Commands start at 1 so we have to subtract 1 from the cmd */
Andrew Jeffery5335f092018-08-09 14:56:08 +0930563 handler = transport_mbox_handlers[req->msg.command - 1];
564 rc = handler(context, req, &resp);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100565 if (rc < 0) {
566 MSG_ERR("Error handling mbox cmd: %d\n",
567 req->msg.command);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100568 }
569 }
570
Andrew Jeffery2f1477c2018-08-09 23:24:38 +0930571 rc = mbox_xlate_errno(context, rc);
572 resp.response = rc;
Andrew Jeffery55dede62017-04-24 16:13:06 +0930573 context->prev_seq = req->msg.seq;
574
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000575 MSG_DBG("Writing MBOX response:\n");
576 MSG_DBG("MBOX cmd: %u\n", resp.command);
577 MSG_DBG("MBOX seq: %u\n", resp.seq);
578 for (i = 0; i < MBOX_ARGS_BYTES; i++) {
579 MSG_DBG("MBOX arg[%d]: 0x%.2x\n", i, resp.args[i]);
580 }
581 MSG_INFO("Writing MBOX response: %u\n", resp.response);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100582 len = write(context->fds[MBOX_FD].fd, &resp, sizeof(resp));
583 if (len < sizeof(resp)) {
584 MSG_ERR("Didn't write the full response\n");
585 rc = -errno;
586 }
587
Andrew Jeffery23a48212018-08-10 14:41:48 +0930588 if (context->transport != old_transport &&
589 context->transport == &transport_mbox_ops) {
Andrew Jeffery0453aa42018-08-21 08:25:46 +0930590 /* A bit messy, but we need the correct event mask */
591 protocol_events_set(context, context->bmc_events);
Andrew Jeffery23a48212018-08-10 14:41:48 +0930592 }
593
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100594 return rc;
595}
596
597/*
598 * get_message() - Read an mbox request message from the mbox registers
599 * @context: The mbox context pointer
600 * @msg: Where to put the received message
601 *
602 * Return: 0 if read successfully otherwise negative error code
603 */
604static int get_message(struct mbox_context *context, union mbox_regs *msg)
605{
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000606 int rc, i;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100607
608 rc = read(context->fds[MBOX_FD].fd, msg, sizeof(msg->raw));
609 if (rc < 0) {
610 MSG_ERR("Couldn't read: %s\n", strerror(errno));
611 return -errno;
612 } else if (rc < sizeof(msg->raw)) {
613 MSG_ERR("Short read: %d expecting %zu\n", rc, sizeof(msg->raw));
614 return -1;
615 }
616
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000617 MSG_DBG("Received MBOX request:\n");
618 MSG_DBG("MBOX cmd: %u\n", msg->msg.command);
619 MSG_DBG("MBOX seq: %u\n", msg->msg.seq);
620 for (i = 0; i < MBOX_ARGS_BYTES; i++) {
621 MSG_DBG("MBOX arg[%d]: 0x%.2x\n", i, msg->msg.args[i]);
622 }
623
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100624 return 0;
625}
626
627/*
Andrew Jefferyd86141b2018-08-09 14:58:53 +0930628 * transport_mbox_dispatch() - handle an mbox interrupt
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100629 * @context: The mbox context pointer
630 *
631 * Return: 0 if handled successfully otherwise negative error code
632 */
Andrew Jefferyd86141b2018-08-09 14:58:53 +0930633int transport_mbox_dispatch(struct mbox_context *context)
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100634{
635 int rc = 0;
636 union mbox_regs req = { 0 };
637
638 assert(context);
639
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100640 rc = get_message(context, &req);
641 if (rc) {
642 return rc;
643 }
644
645 return handle_mbox_req(context, &req);
646}
647
Andrew Jefferyb2466ee2018-08-09 15:03:27 +0930648int __transport_mbox_init(struct mbox_context *context, const char *path)
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100649{
650 int fd;
651
Andrew Jeffery5335f092018-08-09 14:56:08 +0930652 context->transport = &transport_mbox_ops;
Andrew Jefferyefb09de2018-03-26 14:36:43 +1030653
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100654 /* Open MBOX Device */
Andrew Jeffery913740f2017-04-10 23:51:07 +0930655 fd = open(path, O_RDWR | O_NONBLOCK);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100656 if (fd < 0) {
657 MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n",
Andrew Jeffery913740f2017-04-10 23:51:07 +0930658 path, strerror(errno));
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100659 return -errno;
660 }
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000661 MSG_DBG("Opened mbox dev: %s\n", path);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100662
663 context->fds[MBOX_FD].fd = fd;
664
665 return 0;
666}
667
Andrew Jefferyb2466ee2018-08-09 15:03:27 +0930668int transport_mbox_init(struct mbox_context *context)
Andrew Jeffery913740f2017-04-10 23:51:07 +0930669{
Andrew Jefferyb2466ee2018-08-09 15:03:27 +0930670 return __transport_mbox_init(context, MBOX_HOST_PATH);
Andrew Jeffery913740f2017-04-10 23:51:07 +0930671}
672
Andrew Jeffery55260ce2018-08-09 15:05:59 +0930673void transport_mbox_free(struct mbox_context *context)
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100674{
675 close(context->fds[MBOX_FD].fd);
676}