blob: fd4677081c0eb0125494ef3c620a70cd5786fe42 [file] [log] [blame]
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +11001/*
2 * Mailbox Daemon Window Helpers
3 *
4 * Copyright 2016 IBM
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20#define _GNU_SOURCE
21#include <assert.h>
22#include <errno.h>
23#include <fcntl.h>
24#include <getopt.h>
25#include <limits.h>
26#include <poll.h>
27#include <stdbool.h>
28#include <stdint.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <syslog.h>
33#include <signal.h>
34#include <sys/ioctl.h>
35#include <sys/mman.h>
36#include <sys/stat.h>
37#include <sys/timerfd.h>
38#include <sys/types.h>
39#include <time.h>
40#include <unistd.h>
41#include <inttypes.h>
42#include <mtd/mtd-abi.h>
43
44#include "mbox.h"
45#include "common.h"
46#include "mboxd_msg.h"
47#include "mboxd_windows.h"
48#include "mboxd_flash.h"
49
50/* Initialisation Functions */
51
52/*
53 * init_window_state() - Initialise a new window to a known state
54 * @window: The window to initialise
55 * @size: The size of the window
56 */
57void init_window_state(struct window_context *window, uint32_t size)
58{
59 window->mem = NULL;
60 window->flash_offset = FLASH_OFFSET_UNINIT;
61 window->size = size;
62 window->dirty_bmap = NULL;
63 window->age = 0;
64}
65
66/*
67 * init_window_mem() - Divide the reserved memory region among the windows
68 * @context: The mbox context pointer
69 *
70 * Return: 0 on success otherwise negative error code
71 */
72int init_window_mem(struct mbox_context *context)
73{
74 void *mem_location = context->mem;
75 int i;
76
77 /*
78 * Carve up the reserved memory region and allocate it to each of the
79 * windows. The windows are placed one after the other in ascending
80 * order, so the first window will be first in memory and so on. We
81 * shouldn't have allocated more windows than we have memory, but if we
82 * did we will error out here
83 */
84 for (i = 0; i < context->windows.num; i++) {
85 context->windows.window[i].mem = mem_location;
86 mem_location += context->windows.window[i].size;
87 if (mem_location > (context->mem + context->mem_size)) {
88 /* Tried to allocate window past the end of memory */
89 MSG_ERR("Total size of windows exceeds reserved mem\n");
90 MSG_ERR("Try smaller or fewer windows\n");
91 MSG_ERR("Mem size: 0x%.8x\n", context->mem_size);
92 return -1;
93 }
94 }
95
96 return 0;
97}
98
99/* Write from Window Functions */
100
101/*
102 * write_from_window_v1() - Handle writing when erase and block size differ
103 * @context: The mbox context pointer
104 * @offset_bytes: The offset in the current window to write from (bytes)
105 * @count_bytes: Number of bytes to write
106 *
107 * Handle a write_from_window for dirty memory when block_size is less than the
108 * flash erase size
109 * This requires us to be a bit careful because we might have to erase more
110 * than we want to write which could result in data loss if we don't have the
111 * entire portion of flash to be erased already saved in memory (for us to
112 * write back after the erase)
113 *
114 * Return: 0 on success otherwise negative error code
115 */
116int write_from_window_v1(struct mbox_context *context,
117 uint32_t offset_bytes, uint32_t count_bytes)
118{
119 int rc;
120 uint32_t flash_offset;
121 struct window_context low_mem = { 0 }, high_mem = { 0 };
122
123 /* Find where in phys flash this is based on the window.flash_offset */
124 flash_offset = context->current->flash_offset + offset_bytes;
125
126 /*
127 * low_mem.flash_offset = erase boundary below where we're writing
128 * low_mem.size = size from low_mem.flash_offset to where we're writing
129 *
130 * high_mem.flash_offset = end of where we're writing
131 * high_mem.size = size from end of where we're writing to next erase
132 * boundary
133 */
134 low_mem.flash_offset = align_down(flash_offset,
135 context->mtd_info.erasesize);
136 low_mem.size = flash_offset - low_mem.flash_offset;
137 high_mem.flash_offset = flash_offset + count_bytes;
138 high_mem.size = align_up(high_mem.flash_offset,
139 context->mtd_info.erasesize) -
140 high_mem.flash_offset;
141
142 /*
143 * Check if we already have a copy of the required flash areas in
144 * memory as part of the existing window
145 */
146 if (low_mem.flash_offset < context->current->flash_offset) {
147 /* Before the start of our current window */
148 low_mem.mem = malloc(low_mem.size);
149 if (!low_mem.mem) {
150 MSG_ERR("Unable to allocate memory\n");
151 return -MBOX_R_SYSTEM_ERROR;
152 }
153 rc = copy_flash(context, low_mem.flash_offset,
154 low_mem.mem, low_mem.size);
155 if (rc < 0) {
156 goto out;
157 }
158 }
159 if ((high_mem.flash_offset + high_mem.size) >
160 (context->current->flash_offset + context->current->size)) {
161 /* After the end of our current window */
162 high_mem.mem = malloc(high_mem.size);
163 if (!high_mem.mem) {
164 MSG_ERR("Unable to allocate memory\n");
165 rc = -MBOX_R_SYSTEM_ERROR;
166 goto out;
167 }
168 rc = copy_flash(context, high_mem.flash_offset,
169 high_mem.mem, high_mem.size);
170 if (rc < 0) {
171 goto out;
172 }
173 }
174
175 /*
176 * We need to erase the flash from low_mem.flash_offset->
177 * high_mem.flash_offset + high_mem.size
178 */
179 rc = erase_flash(context, low_mem.flash_offset,
180 (high_mem.flash_offset - low_mem.flash_offset) +
181 high_mem.size);
182 if (rc < 0) {
183 MSG_ERR("Couldn't erase flash\n");
184 goto out;
185 }
186
187 /* Write back over the erased area */
188 if (low_mem.mem) {
189 /* Exceed window at the start */
190 rc = write_flash(context, low_mem.flash_offset, low_mem.mem,
191 low_mem.size);
192 if (rc < 0) {
193 goto out;
194 }
195 }
196 rc = write_flash(context, flash_offset,
197 context->current->mem + offset_bytes, count_bytes);
198 if (rc < 0) {
199 goto out;
200 }
201 /*
202 * We still need to write the last little bit that we erased - it's
203 * either in the current window or the high_mem window.
204 */
205 if (high_mem.mem) {
206 /* Exceed window at the end */
207 rc = write_flash(context, high_mem.flash_offset, high_mem.mem,
208 high_mem.size);
209 if (rc < 0) {
210 goto out;
211 }
212 } else {
213 /* Write from the current window - it's atleast that big */
214 rc = write_flash(context, high_mem.flash_offset,
215 context->current->mem + offset_bytes +
216 count_bytes, high_mem.size);
217 if (rc < 0) {
218 goto out;
219 }
220 }
221
222out:
223 free(low_mem.mem);
224 free(high_mem.mem);
225 return rc;
226}
227
228/*
229 * write_from_window() - Write back to the flash from the current window
230 * @context: The mbox context pointer
231 * @offset_bytes: The offset in the current window to write from (blocks)
232 * @count_bytes: Number of blocks to write
233 * @type: Whether this is an erase & write or just an erase
234 *
235 * Return: 0 on success otherwise negative error code
236 */
237int write_from_window(struct mbox_context *context, uint32_t offset,
238 uint32_t count, uint8_t type)
239{
240 int rc;
241 uint32_t flash_offset, count_bytes = count << context->block_size_shift;
242 uint32_t offset_bytes = offset << context->block_size_shift;
243
244 switch (type) {
245 case WINDOW_ERASED: /* >= V2 ONLY -> block_size == erasesize */
246 flash_offset = context->current->flash_offset + offset_bytes;
247 rc = erase_flash(context, flash_offset, count_bytes);
248 if (rc < 0) {
249 MSG_ERR("Couldn't erase flash\n");
250 return rc;
251 }
252 break;
253 case WINDOW_DIRTY:
254 /*
255 * For protocol V1, block_size may be smaller than erase size
256 * so we have a special function to make sure that we do this
257 * correctly without losing data.
258 */
259 if (log_2(context->mtd_info.erasesize) !=
260 context->block_size_shift) {
261 return write_from_window_v1(context, offset_bytes,
262 count_bytes);
263 }
264 flash_offset = context->current->flash_offset + offset_bytes;
265
266 /* Erase the flash */
267 rc = erase_flash(context, flash_offset, count_bytes);
268 if (rc < 0) {
269 return rc;
270 }
271
272 /* Write to the erased flash */
273 rc = write_flash(context, flash_offset,
274 context->current->mem + offset_bytes,
275 count_bytes);
276 if (rc < 0) {
277 return rc;
278 }
279
280 break;
281 default:
282 /* We shouldn't be able to get here */
283 MSG_ERR("Write from window with invalid type: %d\n", type);
284 return -MBOX_R_SYSTEM_ERROR;
285 }
286
287 return 0;
288}
289
290/* Window Management Functions */
291
292/*
293 * alloc_window_dirty_bytemap() - (re)allocate all the window dirty bytemaps
294 * @context: The mbox context pointer
295 */
296void alloc_window_dirty_bytemap(struct mbox_context *context)
297{
298 struct window_context *cur;
299 int i;
300
301 for (i = 0; i < context->windows.num; i++) {
302 cur = &context->windows.window[i];
303 /* There may already be one allocated */
304 free(cur->dirty_bmap);
305 /* Allocate the new one */
306 cur->dirty_bmap = calloc((cur->size >>
307 context->block_size_shift),
308 sizeof(*cur->dirty_bmap));
309 }
310}
311
312/*
313 * free_window_dirty_bytemap() - free all window dirty bytemaps
314 * @context: The mbox context pointer
315 */
316void free_window_dirty_bytemap(struct mbox_context *context)
317{
318 int i;
319
320 for (i = 0; i < context->windows.num; i++) {
321 free(context->windows.window[i].dirty_bmap);
322 }
323}
324
325/*
326 * set_window_bytemap() - Set the window bytemap
327 * @context: The mbox context pointer
328 * @cur: The window to set the bytemap of
329 * @offset: Where in the window to set the bytemap (blocks)
330 * @size: The number of blocks to set
331 * @val: The value to set the bytemap to
332 *
333 * Return: 0 on success otherwise negative error code
334 */
335int set_window_bytemap(struct mbox_context *context, struct window_context *cur,
336 uint32_t offset, uint32_t size, uint8_t val)
337{
338 if (offset + size > (cur->size >> context->block_size_shift)) {
339 MSG_ERR("Tried to set window bytemap past end of window\n");
340 MSG_ERR("Requested offset: 0x%x size: 0x%x window size: 0x%x\n",
341 offset << context->block_size_shift,
342 size << context->block_size_shift,
343 cur->size << context->block_size_shift);
344 return -MBOX_R_PARAM_ERROR;
345 }
346
347 memset(cur->dirty_bmap + offset, val, size);
348 return 0;
349}
350
351/*
352 * close_current_window() - Close the current (active) window
353 * @context: The mbox context pointer
354 * @set_bmc_event: Whether to set the bmc event bit
355 * @flags: Flags as defined for a close command in the protocol
356 *
357 * This closes the current window. If the host has requested the current window
358 * be closed then we don't need to set the bmc event bit
359 * (set_bmc_event == false), otherwise if the current window has been closed
360 * without the host requesting it the bmc event bit must be set to indicate this
361 * to the host (set_bmc_event == true).
362 */
363void close_current_window(struct mbox_context *context, bool set_bmc_event,
364 uint8_t flags)
365{
366 if (set_bmc_event) {
367 set_bmc_events(context, BMC_EVENT_WINDOW_RESET, SET_BMC_EVENT);
368 }
369
370 if (flags & FLAGS_SHORT_LIFETIME) {
371 context->current->age = 0;
372 }
373
374 context->current->size = context->windows.default_size;
375 context->current = NULL;
376 context->current_is_write = false;
377}
378
379/*
380 * reset_window() - Reset a window context to a well defined default state
381 * @context: The mbox context pointer
382 * @window: The window to reset
383 */
384void reset_window(struct mbox_context *context, struct window_context *window)
385{
386 window->flash_offset = FLASH_OFFSET_UNINIT;
387 window->size = context->windows.default_size;
388 if (window->dirty_bmap) { /* Might not have been allocated */
389 set_window_bytemap(context, window, 0,
390 window->size >> context->block_size_shift,
391 WINDOW_CLEAN);
392 }
393 window->age = 0;
394}
395
396/*
397 * reset_all_windows() - Reset all windows to a well defined default state
398 * @context: The mbox context pointer
399 * @set_bmc_event: If any state change should be indicated to the host
400 */
401void reset_all_windows(struct mbox_context *context, bool set_bmc_event)
402{
403 int i;
404
405 /* We might have an open window which needs closing */
406 if (context->current) {
407 close_current_window(context, set_bmc_event, FLAGS_NONE);
408 }
409 for (i = 0; i < context->windows.num; i++) {
410 reset_window(context, &context->windows.window[i]);
411 }
412
413 context->windows.max_age = 0;
414}
415
416/*
417 * find_oldest_window() - Find the oldest (Least Recently Used) window
418 * @context: The mbox context pointer
419 *
420 * Return: Pointer to the least recently used window
421 */
422struct window_context *find_oldest_window(struct mbox_context *context)
423{
424 struct window_context *oldest = NULL, *cur;
425 uint32_t min_age = context->windows.max_age + 1;
426 int i;
427
428 for (i = 0; i < context->windows.num; i++) {
429 cur = &context->windows.window[i];
430
431 if (cur->age < min_age) {
432 min_age = cur->age;
433 oldest = cur;
434 }
435 }
436
437 return oldest;
438}
439
440/*
441 * search_windows() - Search the window cache for a window containing offset
442 * @context: The mbox context pointer
443 * @offset: Absolute flash offset to search for (bytes)
444 * @exact: If the window must exactly map the requested offset
445 *
446 * This will search the cache of windows for one containing the requested
447 * offset. For V1 of the protocol windows must exactly map the offset since we
448 * can't tell the host how much of its request we actually mapped and it will
449 * thus assume it can access window->size from the offset we give it.
450 *
451 * Return: Pointer to a window containing the requested offset otherwise
452 * NULL
453 */
454struct window_context *search_windows(struct mbox_context *context,
455 uint32_t offset, bool exact)
456{
457 struct window_context *cur;
458 int i;
459
460 for (i = 0; i < context->windows.num; i++) {
461 cur = &context->windows.window[i];
462 if (cur->flash_offset == FLASH_OFFSET_UNINIT) {
463 /* Uninitialised Window */
464 if (offset == FLASH_OFFSET_UNINIT) {
465 return cur;
466 }
467 continue;
468 }
469 if ((offset >= cur->flash_offset) &&
470 (offset < (cur->flash_offset + cur->size))) {
471 if (exact && (cur->flash_offset != offset)) {
472 continue;
473 }
474 /* This window contains the requested offset */
475 cur->age = ++(context->windows.max_age);
476 return cur;
477 }
478 }
479
480 return NULL;
481}
482
483/*
484 * create_map_window() - Create a window mapping which maps the requested offset
485 * @context: The mbox context pointer
486 * @this_window: A pointer to update to the "new" window
487 * @offset: Absolute flash offset to create a mapping for (bytes)
488 * @exact: If the window must exactly map the requested offset
489 *
490 * This is used to create a window mapping for the requested offset when there
491 * is no existing window in the cache which satisfies the offset. This involves
492 * choosing an existing window from the window cache to evict so we can use it
493 * to store the flash contents from the requested offset, we then point the
494 * caller to that window since it now maps their request.
495 *
496 * Return: 0 on success otherwise negative error code
497 */
498int create_map_window(struct mbox_context *context,
499 struct window_context **this_window, uint32_t offset,
500 bool exact)
501{
502 struct window_context *cur = NULL;
503 int rc;
504
505
506 /* Search for an uninitialised window, use this before evicting */
507 cur = search_windows(context, FLASH_OFFSET_UNINIT, true);
508
509 /* No uninitialised window found, we need to choose one to "evict" */
510 if (!cur) {
511 cur = find_oldest_window(context);
512 }
513
514 if (!exact) {
515 /*
516 * It would be nice to align the offsets which we map to window
517 * size, this will help prevent overlap which would be an
518 * inefficient use of our reserved memory area (we would like
519 * to "cache" as much of the acutal flash as possible in
520 * memory). If we're protocol V1 however we must ensure the
521 * offset requested is exactly mapped.
522 */
523 offset &= ~(cur->size - 1);
524 }
525
526 if ((offset + cur->size) > context->flash_size) {
527 /*
528 * There is V1 skiboot implementations out there which don't
529 * mask offset with window size, meaning when we have
530 * window size == flash size we will never allow the host to
531 * open a window except at 0x0, which isn't always where the
532 * host requests it. Thus we have to ignore this check and just
533 * hope the host doesn't access past the end of the window
534 * (which it shouldn't) for V1 implementations to get around
535 * this.
536 */
537 if (context->version == API_VERSION_1) {
538 cur->size = align_down(context->flash_size - offset,
539 1 << context->block_size_shift);
540 } else {
541 /* Trying to read past the end of flash */
542 MSG_ERR("Tried to open read window past flash limit\n");
543 return -MBOX_R_PARAM_ERROR;
544 }
545 }
546
547 /* Copy from flash into the window buffer */
548 rc = copy_flash(context, offset, cur->mem, cur->size);
549 if (rc < 0) {
550 /* We don't know how much we've copied -> better reset window */
551 reset_window(context, cur);
552 return rc;
553 }
554
555 /*
556 * Since for V1 windows aren't constrained to start at multiples of
557 * window size it's possible that something already maps this offset.
558 * Reset any windows which map this offset to avoid coherency problems.
559 * We just have to check for anything which maps the start or the end
560 * of the window since all windows are the same size so another window
561 * cannot map just the middle of this window.
562 */
563 if (context->version == API_VERSION_1) {
564 uint32_t i;
565
566 for (i = offset; i < (offset + cur->size); i += (cur->size - 1)) {
567 struct window_context *tmp = NULL;
568 do {
569 tmp = search_windows(context, i, false);
570 if (tmp) {
571 reset_window(context, tmp);
572 }
573 } while (tmp);
574 }
575 }
576
577 /* Clear the bytemap of the window just loaded -> we know it's clean */
578 set_window_bytemap(context, cur, 0,
579 cur->size >> context->block_size_shift,
580 WINDOW_CLEAN);
581
582 /* Update so we know what's in the window */
583 cur->flash_offset = offset;
584 cur->age = ++(context->windows.max_age);
585 *this_window = cur;
586
587 return 0;
588}