blob: 046d5362999e8979ce73a5161aed3c0a15fc5163 [file] [log] [blame]
Andrew Jefferyc7d19472018-08-08 11:43:08 +09301// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2018 IBM Corp.
3#include "config.h"
4
5#include <errno.h>
6
7extern "C" {
Andrew Jeffery26558db2018-08-10 00:22:38 +09308#include "mboxd.h"
Andrew Jefferyc7d19472018-08-08 11:43:08 +09309#include "protocol.h"
10#include "vpnor/protocol.h"
11}
12
13/* We need to hijack create_window for vpnor */
14
15static const struct protocol_ops protocol_ops_v1 = {
16 .reset = protocol_v1_reset,
17 .get_info = protocol_v1_get_info,
18 .get_flash_info = protocol_v1_get_flash_info,
19 .create_window = protocol_v1_vpnor_create_window,
20 .mark_dirty = protocol_v1_mark_dirty,
21 .erase = NULL,
22 .flush = protocol_v1_flush,
23 .close = protocol_v1_close,
24 .ack = protocol_v1_ack,
25};
26
27static const struct protocol_ops protocol_ops_v2 = {
28 .reset = protocol_v1_reset,
29 .get_info = protocol_v2_get_info,
30 .get_flash_info = protocol_v2_get_flash_info,
31 .create_window = protocol_v2_vpnor_create_window,
32 .mark_dirty = protocol_v2_mark_dirty,
33 .erase = protocol_v2_erase,
34 .flush = protocol_v2_flush,
35 .close = protocol_v2_close,
36 .ack = protocol_v1_ack,
37};
38
39static const struct protocol_ops *protocol_ops_map[] = {
40 [0] = NULL,
41 [1] = &protocol_ops_v1,
42 [2] = &protocol_ops_v2,
43};
44
45int protocol_negotiate_version(struct mbox_context *context, uint8_t requested)
46{
47 /* Check we support the version requested */
48 if (requested < API_MIN_VERSION)
49 return -EINVAL;
50
51 uint8_t version =
52 (requested > API_MAX_VERSION) ? API_MAX_VERSION : requested;
53 context->version = static_cast<enum api_version>(version);
54
55 context->protocol = protocol_ops_map[context->version];
56
57 return context->version;
58}