blob: 080e424c028dcd527ef4ee647c6e910436147aed [file] [log] [blame]
Andrew Jeffery1e531af2018-08-07 13:32:57 +09301// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2018 IBM Corp.
3#include "config.h"
4
5#include <errno.h>
6#include <stdint.h>
7
8#include "mbox.h"
9#include "lpc.h"
10#include "transport_mbox.h" /* TODO: Remove dependency on transport_mbox.h */
11#include "windows.h"
12
Andrew Jefferyab666a52018-08-07 14:28:09 +093013int protocol_v1_reset(struct mbox_context *context)
14{
15 /* Host requested it -> No BMC Event */
16 windows_reset_all(context, NO_BMC_EVENT);
17 return lpc_reset(context);
18}
19
Andrew Jeffery1e531af2018-08-07 13:32:57 +093020int protocol_v1_get_info(struct mbox_context *context,
21 struct protocol_get_info *io)
22{
23 uint8_t old_version = context->version;
24 int rc;
25
26 /* Bootstrap protocol version. This may involve {up,down}grading */
27 rc = protocol_negotiate_version(context, io->req.api_version);
28 if (rc < 0)
29 return rc;
30
31 /* Do the {up,down}grade if necessary*/
32 if (rc != old_version) {
33 windows_reset_all(context, SET_BMC_EVENT);
34 return context->protocol->get_info(context, io);
35 }
36
37 /* Record the negotiated version for the response */
38 io->resp.api_version = rc;
39
40 /* Now do all required intialisation for v1 */
41 context->block_size_shift = BLOCK_SIZE_SHIFT_V1;
42 MSG_INFO("Block Size: 0x%.8x (shift: %u)\n",
43 1 << context->block_size_shift, context->block_size_shift);
44
45 /* Knowing blocksize we can allocate the window dirty_bytemap */
46 windows_alloc_dirty_bytemap(context);
47
48 io->resp.v1.read_window_size =
49 context->windows.default_size >> context->block_size_shift;
50 io->resp.v1.write_window_size =
51 context->windows.default_size >> context->block_size_shift;
52
53 return lpc_map_memory(context);
54}
55
Andrew Jeffery91a87452018-08-07 14:54:14 +093056int protocol_v1_get_flash_info(struct mbox_context *context,
57 struct protocol_get_flash_info *io)
58{
59 io->resp.v1.flash_size = context->flash_size;
60 io->resp.v1.erase_size = context->mtd_info.erasesize;
61
62 return 0;
63}
64
Andrew Jeffery1e531af2018-08-07 13:32:57 +093065/*
Andrew Jeffery22fa5002018-08-07 15:22:50 +093066 * get_lpc_addr_shifted() - Get lpc address of the current window
67 * @context: The mbox context pointer
68 *
69 * Return: The lpc address to access that offset shifted by block size
70 */
71static inline uint16_t get_lpc_addr_shifted(struct mbox_context *context)
72{
73 uint32_t lpc_addr, mem_offset;
74
75 /* Offset of the current window in the reserved memory region */
76 mem_offset = context->current->mem - context->mem;
77 /* Total LPC Address */
78 lpc_addr = context->lpc_base + mem_offset;
79
80 MSG_DBG("LPC address of current window: 0x%.8x\n", lpc_addr);
81
82 return lpc_addr >> context->block_size_shift;
83}
84
Andrew Jeffery4bcec8e2018-08-07 15:33:41 +093085int protocol_v1_create_window(struct mbox_context *context,
86 struct protocol_create_window *io)
Andrew Jeffery22fa5002018-08-07 15:22:50 +093087{
88 int rc;
89 uint32_t offset = io->req.offset << context->block_size_shift;
90
91 /* Close the current window if there is one */
92 if (context->current) {
93 /* There is an implicit flush if it was a write window */
94 if (context->current_is_write) {
95 rc = mbox_handle_flush_window(context, NULL, NULL);
96 if (rc < 0) {
97 MSG_ERR("Couldn't Flush Write Window\n");
98 return rc;
99 }
100 }
101 windows_close_current(context, NO_BMC_EVENT, FLAGS_NONE);
102 }
103
104 /* Offset the host has requested */
105 MSG_INFO("Host requested flash @ 0x%.8x\n", offset);
106 /* Check if we have an existing window */
107 context->current = windows_search(context, offset,
108 context->version == API_VERSION_1);
109
110 if (!context->current) { /* No existing window */
111 MSG_DBG("No existing window which maps that flash offset\n");
112 rc = windows_create_map(context, &context->current,
113 offset,
114 context->version == API_VERSION_1);
115 if (rc < 0) { /* Unable to map offset */
116 MSG_ERR("Couldn't create window mapping for offset 0x%.8x\n",
Andrew Jeffery4bcec8e2018-08-07 15:33:41 +0930117 offset);
Andrew Jeffery22fa5002018-08-07 15:22:50 +0930118 return rc;
119 }
120 }
121
Andrew Jeffery4bcec8e2018-08-07 15:33:41 +0930122 context->current_is_write = !io->req.ro;
123
Andrew Jeffery22fa5002018-08-07 15:22:50 +0930124 MSG_INFO("Window @ %p for size 0x%.8x maps flash offset 0x%.8x\n",
125 context->current->mem, context->current->size,
126 context->current->flash_offset);
127
128 io->resp.lpc_address = get_lpc_addr_shifted(context);
129
130 return 0;
131}
132
133/*
Andrew Jeffery1e531af2018-08-07 13:32:57 +0930134 * get_suggested_timeout() - get the suggested timeout value in seconds
135 * @context: The mbox context pointer
136 *
137 * Return: Suggested timeout in seconds
138 */
139static uint16_t get_suggested_timeout(struct mbox_context *context)
140{
141 struct window_context *window = windows_find_largest(context);
142 uint32_t max_size_mb = window ? (window->size >> 20) : 0;
143 uint16_t ret;
144
145 ret = align_up(max_size_mb * FLASH_ACCESS_MS_PER_MB, 1000) / 1000;
146
147 MSG_DBG("Suggested Timeout: %us, max window size: %uMB, for %dms/MB\n",
148 ret, max_size_mb, FLASH_ACCESS_MS_PER_MB);
149 return ret;
150}
151
152int protocol_v2_get_info(struct mbox_context *context,
153 struct protocol_get_info *io)
154{
155 uint8_t old_version = context->version;
156 int rc;
157
158 /* Bootstrap protocol version. This may involve {up,down}grading */
159 rc = protocol_negotiate_version(context, io->req.api_version);
160 if (rc < 0)
161 return rc;
162
163 /* Do the {up,down}grade if necessary*/
164 if (rc != old_version) {
165 windows_reset_all(context, SET_BMC_EVENT);
166 return context->protocol->get_info(context, io);
167 }
168
169 /* Record the negotiated version for the response */
170 io->resp.api_version = rc;
171
172 /* Now do all required intialisation for v2 */
173 context->block_size_shift = log_2(context->mtd_info.erasesize);
174 MSG_INFO("Block Size: 0x%.8x (shift: %u)\n",
175 1 << context->block_size_shift, context->block_size_shift);
176
177 /* Knowing blocksize we can allocate the window dirty_bytemap */
178 windows_alloc_dirty_bytemap(context);
179
180 io->resp.v2.block_size_shift = context->block_size_shift;
181 io->resp.v2.timeout = get_suggested_timeout(context);
182
183 return lpc_map_memory(context);
184}
185
Andrew Jeffery91a87452018-08-07 14:54:14 +0930186int protocol_v2_get_flash_info(struct mbox_context *context,
187 struct protocol_get_flash_info *io)
188{
189 io->resp.v2.flash_size =
190 context->flash_size >> context->block_size_shift;
191 io->resp.v2.erase_size =
192 context->mtd_info.erasesize >> context->block_size_shift;
193
194 return 0;
195}
196
Andrew Jeffery4bcec8e2018-08-07 15:33:41 +0930197int protocol_v2_create_window(struct mbox_context *context,
198 struct protocol_create_window *io)
Andrew Jeffery22fa5002018-08-07 15:22:50 +0930199{
200 int rc;
201
Andrew Jeffery4bcec8e2018-08-07 15:33:41 +0930202 rc = protocol_v1_create_window(context, io);
Andrew Jeffery22fa5002018-08-07 15:22:50 +0930203 if (rc < 0)
204 return rc;
205
206 io->resp.size = context->current->size >> context->block_size_shift;
207 io->resp.offset = context->current->flash_offset >>
208 context->block_size_shift;
209
210 return 0;
211}
212
Andrew Jeffery1e531af2018-08-07 13:32:57 +0930213static const struct protocol_ops protocol_ops_v1 = {
Andrew Jefferyab666a52018-08-07 14:28:09 +0930214 .reset = protocol_v1_reset,
Andrew Jeffery1e531af2018-08-07 13:32:57 +0930215 .get_info = protocol_v1_get_info,
Andrew Jeffery91a87452018-08-07 14:54:14 +0930216 .get_flash_info = protocol_v1_get_flash_info,
Andrew Jeffery4bcec8e2018-08-07 15:33:41 +0930217 .create_window = protocol_v1_create_window,
Andrew Jeffery1e531af2018-08-07 13:32:57 +0930218};
219
220static const struct protocol_ops protocol_ops_v2 = {
Andrew Jefferyab666a52018-08-07 14:28:09 +0930221 .reset = protocol_v1_reset,
Andrew Jeffery1e531af2018-08-07 13:32:57 +0930222 .get_info = protocol_v2_get_info,
Andrew Jeffery91a87452018-08-07 14:54:14 +0930223 .get_flash_info = protocol_v2_get_flash_info,
Andrew Jeffery4bcec8e2018-08-07 15:33:41 +0930224 .create_window = protocol_v2_create_window,
Andrew Jeffery1e531af2018-08-07 13:32:57 +0930225};
226
227static const struct protocol_ops *protocol_ops_map[] = {
228 [0] = NULL,
229 [1] = &protocol_ops_v1,
230 [2] = &protocol_ops_v2,
231};
232
233int protocol_negotiate_version(struct mbox_context *context,
234 uint8_t requested)
235{
236 /* Check we support the version requested */
237 if (requested < API_MIN_VERSION)
238 return -EINVAL;
239
240 context->version = (requested > API_MAX_VERSION) ?
241 API_MAX_VERSION : requested;
242
243 context->protocol = protocol_ops_map[context->version];
244
245 return context->version;
246}
247
248int protocol_init(struct mbox_context *context)
249{
250 context->version = API_MAX_VERSION;
251 context->protocol = protocol_ops_map[context->version];
252
253 return 0;
254}
255
256void protocol_free(struct mbox_context *context)
257{
258 return;
259}