1Copyright 2017 IBM
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7  http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14
15## Intro
16
17This document describes a protocol for host to BMC communication via the
18mailbox registers present on the Aspeed 2400 and 2500 chips.
19This protocol is specifically designed to allow a host to request and manage
20access to the flash with the specifics of how the host is required to control
21this described below.
22
23## Version
24
25Both version 1 and version 2 of the protocol are described below with version 2
26specificities represented with V2 in brackets - (V2).
27
28## Problem Overview
29
30"mbox" is the name we use to represent a protocol we have established between
31the host and the BMC via the Aspeed mailbox registers. This protocol is used
32for the host to control the flash.
33
34Prior to the mbox protocol, the host uses a backdoor into the BMC address space
35(the iLPC-to-AHB bridge) to directly manipulate the BMCs own flash controller.
36
37This is not sustainable for a number of reasons. The main ones are:
38
391. Every piece of the host software stack that needs flash access (HostBoot,
40   OCC, OPAL, ...) has to have a complete driver for the flash controller,
41   update it on each BMC generation, have all the quirks for all the flash
42   chips supported etc... We have 3 copies on the host already in addition to
43   the one in the BMC itself.
44
452. There are serious issues of access conflicts to that controller between the
46   host and the BMC.
47
483. It's very hard to support "BMC reboots" when doing that
49
504. It's slow
51
525. Last but probably most important, having that backdoor open is a security
53   risk. It means the host can access any address on the BMC internal bus and
54   implant malware in the BMC itself. So if the host is a "bare metal" shared
55   system in some kind of data center, not only the host flash needs to be
56   reflashed when switching from one customer to another, but the entire BMC
57   flash too as nothing can be trusted. So we want to disable it.
58
59To address all these, we have implemented a new mechanism that we call mbox.
60
61When using this mechanism, the BMC is solely responsible for directly accessing
62the flash controller. All flash erase and write operations are performed by the
63BMC and the BMC only. (We can allow direct reads from flash under some
64circumstances but we tend to prefer going via memory).
65
66The host uses the mailbox registers to send "commands" to the BMC, which
67responds via the same mechanism. Those commands allow the host to control a
68"window" (which is the LPC -> AHB FW space mapping) that is either a read
69window or a write window onto the flash.
70
71When set for writing, the BMC makes the window point to a chunk of RAM instead.
72When the host "commits" a change (via MBOX), then the BMC can perform the
73actual flashing from the data in the RAM window.
74
75The idea is to have the LPC FW space be routed to an active "window".  That
76window can be a read or a write window. The commands allow to control which
77window and which offset into the flash it maps.
78
79* A read window can be a direct window to the flash controller space (ie.
80  0x3000\_0000) or it can be a window to a RAM image of a flash. It doesn't have
81  to be the full size of the flash per protocol (commands can be use to "slide"
82  it to various parts of the flash) but if its set to map the actual flash
83  controller space at 0x3000\_0000, it's probably simpler to make it the full
84  flash. The host makes no assumption, it's your choice what to provide. The
85  simplest implementation is to just route to the flash read/only.
86
87* A write window has to be a chunk of BMC memory. The minimum size is not
88  defined in the spec, but it should be at least one block (4k for now but it
89  should support larger block sizes in the future). When the BMC receive the
90  command to map the write window at a given offset of the flash, the BMC should
91  copy that portion of the flash into a reserved memory buffer, and modify the
92  LPC mapping to point to that buffer.
93
94The host can then write to that window directly (updating the BMC memory) and
95send a command to "commit" those updates to flash.
96
97Finally there is a `RESET_STATE`. It's the state in which the bootloader in the
98SEEPROM of the POWER9 chip will find what it needs to load HostBoot. The
99details are still being ironed out: either mapping the full flash read only or
100reset to a "window" that is either at the bottom or top of the flash. The
101current implementation resets to point to the full flash.
102
103## Where is the code?
104
105The mbox userspace is available [on GitHub](https://github.com/openbmc/mboxbridge)
106This is Apache licensed but we are keen to see any enhancements you may have.
107
108The kernel driver is still in the process of being upstreamed but can be found
109in the OpenBMC Linux kernel staging tree:
110
111https://github.com/openbmc/linux/commit/85770a7d1caa6a1fa1a291c33dfe46e05755a2ef
112
113## Building
114
115The autotools of this requires the autoconf-archive package for your
116system
117
118## The Hardware
119
120The Aspeed mailbox consists of 16 (8 bit) data registers see Layout for their
121use. Mailbox interrupt enabling, masking and triggering is done using a pair
122of control registers, one accessible by the host the other by the BMC.
123Interrupts can also be raised per write to each data register, for BMC and
124host. Write tiggered interrupts are configured using two 8 bit registers where
125each bit represents a data register and if an interrupt should fire on write.
126Two 8 bit registers are present to act as a mask for write triggered
127interrupts.
128
129### Layout
130
131```
132Byte 0: COMMAND
133Byte 1: Sequence
134Byte 2-12: Arguments
135Byte 13: Response code
136Byte 14: Host controlled status reg
137Byte 15: BMC controlled status reg
138```
139
140## Low Level Protocol Flow
141
142What we essentially have is a set of registers which either the host or BMC can
143write to in order to communicate to the other which will respond in some way.
144There are 3 basic types of communication.
145
1461. Commands sent from the Host to the BMC
1472. Responses sent from the BMC to the Host in response to commands
1483. Asyncronous events raised by the BMC
149
150### General Use
151
152Messages usually originate from the host to the BMC. There are special
153cases for a back channel for the BMC to pass new information to the
154host which will be discussed later.
155
156To initiate a request the host must set a command code (see Commands) into
157mailbox data register 0, and generate a sequence number (see Sequence Numbers)
158to write to mailbox register data 1. After these two values, any
159command-specific data should be written (see Layout). The host must then
160generate an interrupt to the BMC by using bit 0 of its control register and
161wait for an interrupt on the response register.  Generating an interrupt
162automatically sets bit 7 of the corresponding control register. This bit can be
163used to poll for messages.
164
165On receiving an interrupt (or polling on bit 7 of its Control
166Register) the BMC should read the message from the general registers
167of the mailbox and perform the necessary action before responding. On
168responding the BMC must ensure that the sequence number is the same as
169the one in the request from the host. The BMC must also ensure that
170mailbox data register 13 is a valid response code (see Responses). The
171BMC should then use its control register to generate an interrupt for
172the host to notify it of a response.
173
174### Asynchronous BMC to Host Events
175
176BMC to host communication is also possible for notification of events
177from the BMC. This requires that the host have interrupts enabled on
178mailbox data register 15 (or otherwise poll on bit 7 of mailbox status
179register 1). On receiving such a notification the host should read
180mailbox data register 15 to determine the event code which was set by the
181BMC (see BMC Event notifications in Commands for detail). Events which are
182defined as being able to be acknowledged by the host must be with a
183BMC_EVENT_ACK command.
184
185## High Level Protocol Flow
186
187When a host wants to communicate with the BMC via the mbox protocol the first
188thing it should do it call MBOX_GET_INFO in order to establish the protocol
189version which each understands. Before this the only other commands which are
190allowed are RESET_STATE and BMC_EVENT_ACK.
191
192After this the host can open and close windows with the CREATE_READ_WINDOW,
193CREATE_WRITE_WINDOW and CLOSE_WINDOW commands. Creating a window is how the
194host requests access to a section of flash. It is worth noting that the host
195can only ever have one window that it is accessing at a time - hence forth
196referred to as the active window.
197
198When the active window is a write window the host can perform MARK_WRITE_DIRTY,
199MARK_WRITE_ERASED and WRITE_FLUSH commands to identify changed blocks and
200control when the changed blocks are written to flash.
201
202Independently, and at any point not during an existing mbox command
203transaction, the BMC may raise raise asynchronous events with the host to
204communicate a change in state.
205
206### Version Negotiation
207
208Given that a majority of command and response arguments are specified as a
209multiple of block size it is necessary for the host and BMC to agree on a
210protocol version as this determines the block size. In V1 it is hard coded at
2114K and in V2 the BMC chooses and specifies this to the host as a response
212argument to `MBOX_GET_INFO`. Thus the host must always call `MBOX_GET_INFO`
213before any other command which specifies an argument in block size.
214
215When invoking `MBOX_GET_INFO` the host must provide the BMC its highest
216supported version of the protocol. The BMC must respond with a protocol version
217less than or equal to that requested by the host, or in the event that there is
218no such value, an error code. In the event that an error is returned the host
219must not continue to communicate with the BMC. Otherwise, the protocol version
220returned by the BMC is the agreed protocol version for all further
221communication. The host may at a future point request a change in protocol
222version by issuing a subsequent `MBOX_GET_INFO` command.
223
224### Window Management
225
226In order to access flash contents the host must request a window be opened at
227the flash offset it would like to access. The host may give a hint as to how
228much data it would like to access or otherwise set this argument to zero. The
229BMC must respond with the lpc bus address to access this window and the
230window size. The host must not access past the end of the active window.
231
232There is only ever one active window which is the window created by the most
233recent CREATE_READ_WINDOW or CREATE_WRITE_WINDOW call which succeeded. Even
234though there are two types of windows there can still only be one active window
235irrespective of type. A host must not write to a read window. A host may read
236from a write window and the BMC must guarantee that the window reflects what
237the host has written there.
238
239A window can be closed by calling CLOSE_WINDOW in which case there is no active
240window and the host must not access the LPC window after it has been closed.
241If the host closes an active write window then the BMC must perform an
242implicit flush. If the host tries to open a new window with an already active
243window then the active window is closed (and implicitly flushed if it was a
244write window). If the new window is successfully opened then it is the new
245active window, if the command fails then there is no active window and the
246previous active window must no longer be accessed.
247
248The host must not access an lpc address other than that which is contained by
249the active window. The host must not use write management functions (see below)
250if the active window is a read window or if there is no active window.
251
252### Write Management
253
254The BMC has no method for intercepting writes that occur over the LPC bus. Thus
255the host must explicitly notify the BMC of where and when a write has
256occured. The host must use the MARK_WRITE_DIRTY command to tell the BMC where
257within the write window it has modified. The host may also use the
258MARK_WRITE_ERASED command to erase large parts of the active window without the
259need to write 0xFF. The BMC must ensure that if the host
260reads from an area it has erased that the read values are 0xFF. Any part of the
261active window marked dirty/erased is only marked for the lifetime of the current
262active write window and does not persist if the active window is closed either
263implicitly or explicitly by the host or the BMC. The BMC may at any time
264or must on a call to WRITE_FLUSH flush the changes which it has been notified
265of back to the flash, at which point the dirty or erased marking is cleared
266for the active window. The host must not assume that any changes have been
267written to flash unless an explicit flush call was successful, a close of an
268active write window was successful or a create window command with an active
269write window was successful - otherwise consistency between the flash and memory
270contents cannot be guaranteed.
271
272The host is not required to perform an erase before a write command and the
273BMC must ensure that a write performs as expected - that is if an erase is
274required before a write then the BMC must perform this itself.
275
276### BMC Events
277
278The BMC can raise events with the host asynchronously to communicate to the
279host a change in state which it should take notice of. The host must (if
280possible for the given event) acknowledge it to inform the BMC it has been
281received.
282
283If the BMC raises a BMC Reboot event then the host must renegotiate the
284protocol version so that both the BMC and the host agree on the block size.
285A BMC Reboot event implies a BMC Windows Reset event.
286If the BMC raises a BMC Windows Reset event then the host must
287assume that there is no longer an active window - that is if there was an
288active window it has been closed by the BMC and if it was a write window
289then the host must not assume that it was flushed unless a previous explicit
290flush call was successful.
291
292The BMC may at some points require access to the flash and the BMC daemon must
293set the BMC Flash Control Lost event when the BMC is accessing the flash behind
294the BMC daemons back. When this event is set the host must assume that the
295contents of the active window could be inconsistent with the contents of flash.
296
297## Protocol Definition
298
299### Commands
300
301```
302RESET_STATE          0x01
303GET_MBOX_INFO        0x02
304GET_FLASH_INFO       0x03
305CREATE_READ_WINDOW   0x04
306CLOSE_WINDOW         0x05
307CREATE_WRITE_WINDOW  0x06
308MARK_WRITE_DIRTY     0x07
309WRITE_FLUSH          0x08
310BMC_EVENT_ACK        0x09
311MARK_WRITE_ERASED    0x0a	(V2)
312```
313
314### Responses
315
316```
317SUCCESS		1
318PARAM_ERROR	2
319WRITE_ERROR	3
320SYSTEM_ERROR	4
321TIMEOUT		5
322BUSY		6	(V2)
323WINDOW_ERROR	7	(V2)
324SEQ_ERROR	8	(V2)
325```
326
327### Sequence Numbers
328
329Sequence numbers are included in messages for correlation of commands and
330responses. V1 and V2 of the protocol permit either zero or one commands to be
331in progress (yet to receive a response).
332
333For generality, the host must generate a sequence number that is unique with
334respect to the previous command (one that has received a response) and any
335in-progress commands. Sequence numbers meeting this requirement are considered
336valid. The BMC's response to a command must contain the same sequence number
337issued by the host as found in the relevant command.
338
339Sequence numbers may be reused in accordance with the constraints outlined
340above, however it is not an error if the BMC receives a `GET_MBOX_INFO` with an
341invalid sequence number. For all other cases, the BMC must respond with
342`SEQ_ERROR` if the constraints are violated. If the host receives a `SEQ_ERROR`
343response it must consider any in-progress commands to have failed. The host may
344retry the affected command(s) after generating a suitable sequence number.
345
346#### Description:
347
348SUCCESS		- Command completed successfully
349
350PARAM_ERROR	- Error with parameters supplied or command invalid
351
352WRITE_ERROR	- Error writing to the backing file system
353
354SYSTEM_ERROR	- Error in BMC performing system action
355
356TIMEOUT		- Timeout in performing action
357
358BUSY		- Daemon in suspended state (currently unable to access flash)
359		- Retry again later
360
361WINDOW_ERROR	- Command not valid for active window or no active window
362		- Try opening an appropriate window and retrying the command
363
364### Information
365- All multibyte messages are LSB first (little endian)
366- All responses must have a valid return code in byte 13
367
368
369### Commands in detail
370
371Block size refers to an agreed value which is used as a unit for the
372arguments of various commands and responses. Having a block size multiplier
373allows us to specify larger values with fewer command and response fields.
374
375In V1 block size is hard coded to 4K.
376In V2 it is variable and must be queried with the GET_MBOX_INFO command.
377Note that for simplicity block size must always be a power-of-2.
378Block size must also be greater than or equal to 4K. This is due to the
379fact that we have a 28-bit LPC address space and commands which return an
380LPC address do so in 16 bits, thus we need at least a 12-bit unit to ensure
381that we can specify the entire address space. This additionally allows us
382to specify flash addresses of at least 256MB.
383
384Sizes and addresses are specified in either bytes - (bytes)
385					 or blocks - (blocks)
386Sizes and addresses specified in blocks must be converted to bytes by
387multiplying by the block size.
388```
389Command:
390	RESET_STATE
391	Implemented in Versions:
392		V1, V2
393	Arguments:
394		-
395	Response:
396		-
397	Notes:
398		This command is designed to inform the BMC that it should put
399		host LPC mapping back in a state where the SBE will be able to
400		use it. Currently this means pointing back to BMC flash
401		pre mailbox protocol. Final behavour is still TBD.
402
403Command:
404	GET_MBOX_INFO
405	Implemented in Versions:
406		V1, V2
407	Arguments:
408		V1:
409		Args 0: API version
410
411		V2:
412		Args 0: API version
413
414	Response:
415		V1:
416		Args 0: API version
417		Args 1-2: default read window size (blocks)
418		Args 3-4: default write window size (blocks)
419
420		V2:
421		Args 0: API version
422		Args 1-2: reserved
423		Args 3-4: reserved
424		Args 5: Block size as power of two (encoded as a shift)
425
426Command:
427	GET_FLASH_INFO
428	Implemented in Versions:
429		V1, V2
430	Arguments:
431		-
432	Response:
433		V1:
434		Args 0-3: Flash size (bytes)
435		Args 4-7: Erase granule (bytes)
436
437		V2:
438		Args 0-1: Flash size (blocks)
439		Args 2-3: Erase granule (blocks)
440
441Command:
442	CREATE_{READ/WRITE}_WINDOW
443	Implemented in Versions:
444		V1, V2
445	Arguments:
446		V1:
447		Args 0-1: Requested flash offset (blocks)
448
449		V2:
450		Args 0-1: Requested flash offset (blocks)
451		Args 2-3: Requested flash size to access (blocks)
452
453	Response:
454		V1:
455		Args 0-1: LPC bus address of window (blocks)
456
457		V2:
458		Args 0-1: LPC bus address of window (blocks)
459		Args 2-3: Window size (blocks)
460		Args 4-5: Flash offset mapped by window (blocks)
461	Notes:
462		The flash offset which the host requests access to is always
463		taken from the start of flash - that is it is an absolute
464		offset into flash.
465
466		LPC bus address is always given from the start of the LPC
467		address space - that is it is an absolute address.
468
469		The requested access size is only a hint. The response
470		indicates the actual size of the window. The BMC may
471		want to use the requested size to pre-load the remainder
472		of the request. The host must not access past the end of the
473		active window.
474
475		The flash offset mapped by the window is an absolute flash
476		offset and must be less than or equal to the flash offset
477		requested by the host. It is the responsibility of the host
478		to use this information to access any offset which is required.
479
480		The requested window size may be zero. In this case the
481		BMC is free to create any sized window but it must contain
482		atleast the first block of data requested by the host. A large
483		window is of course preferred and should correspond to
484		the default size returned in the GET_MBOX_INFO command.
485
486		If this command returns successfully then the created window
487		is the active window. If it fails then there is no active
488		window.
489
490Command:
491	CLOSE_WINDOW
492	Implemented in Versions:
493		V1, V2
494	Arguments:
495		V1:
496		-
497
498		V2:
499		Args 0: Flags
500	Response:
501		-
502	Notes:
503		Closes the active window. Any further access to the LPC bus
504		address specified to address the previously active window will
505		have undefined effects. If the active window is a
506		write window then the BMC must perform an implicit flush.
507
508		The Flags argument allows the host to provide some
509		hints to the BMC. Defined Values:
510			0x01 - Short Lifetime:
511				The window is unlikely to be accessed
512				anytime again in the near future. The effect of
513				this will depend on BMC implementation. In
514				the event that the BMC performs some caching
515				the BMC daemon could mark data contained in a
516				window closed with this flag as first to be
517				evicted from the cache.
518
519Command:
520	MARK_WRITE_DIRTY
521	Implemented in Versions:
522		V1, V2
523	Arguments:
524		V1:
525		Args 0-1: Flash offset to mark from base of flash (blocks)
526		Args 2-5: Number to mark dirty at offset (bytes)
527
528		V2:
529		Args 0-1: Window offset to mark (blocks)
530		Args 2-3: Number to mark dirty at offset (blocks)
531
532	Response:
533		-
534	Notes:
535		The BMC has no method for intercepting writes that
536		occur over the LPC bus. The host must explicitly notify
537		the daemon of where and when a write has occured so it
538		can be flushed to backing storage.
539
540		Offsets are given as an absolute (either into flash (V1) or the
541		active window (V2)) and a zero offset refers to the first
542		block. If the offset + number exceeds the size of the active
543		window then the command must not succeed.
544
545Command
546	WRITE_FLUSH
547	Implemented in Versions:
548		V1, V2
549	Arguments:
550		V1:
551		Args 0-1: Flash offset to mark from base of flash (blocks)
552		Args 2-5: Number to mark dirty at offset (bytes)
553
554		V2:
555		-
556
557	Response:
558		-
559	Notes:
560		Flushes any dirty/erased blocks in the active window to
561		the backing storage.
562
563		In V1 this can also be used to mark parts of the flash
564		dirty and flush in a single command. In V2 the explicit
565		mark dirty command must be used before a call to flush
566		since there are no longer any arguments. If the offset + number
567		exceeds the size of the active window then the command must not
568		succeed.
569
570
571Command:
572	BMC_EVENT_ACK
573	Implemented in Versions:
574		V1, V2
575	Arguments:
576		Args 0:	Bits in the BMC status byte (mailbox data
577			register 15) to ack
578	Response:
579		*clears the bits in mailbox data register 15*
580	Notes:
581		The host should use this command to acknowledge BMC events
582		supplied in mailbox register 15.
583
584Command:
585	MARK_WRITE_ERASED
586	Implemented in Versions:
587		V2
588	Arguments:
589		V2:
590		Args 0-1: Window offset to erase (blocks)
591		Args 2-3: Number to erase at offset (blocks)
592	Response:
593		-
594	Notes:
595		This command allows the host to erase a large area
596		without the need to individually write 0xFF
597		repetitively.
598
599		Offset is the offset within the active window to start erasing
600		from (zero refers to the first block of the active window) and
601		number is the number of blocks of the active window to erase
602		starting at offset. If the offset + number exceeds the size of
603		the active window then the command must not succeed.
604```
605
606### BMC Events in Detail:
607
608If the BMC needs to tell the host something then it simply
609writes to Byte 15. The host should have interrupts enabled
610on that register, or otherwise be polling it.
611
612#### Bit Definitions:
613
614Events which should be ACKed:
615```
6160x01: BMC Reboot
6170x02: BMC Windows Reset (V2)
618```
619
620Events which cannot be ACKed (BMC will clear when no longer
621applicable):
622```
6230x40: BMC Flash Control Lost (V2)
6240x80: BMC MBOX Daemon Ready (V2)
625```
626
627#### Event Description:
628
629Events which must be ACKed:
630The host should acknowledge these events with BMC_EVENT_ACK to
631let the BMC know that they have been received and understood.
632```
6330x01 - BMC Reboot:
634	Used to inform the host that a BMC reboot has occured.
635	The host must perform protocol verison negotiation again and
636	must assume it has no active window. The host must not assume
637	that any commands which didn't respond as such succeeded.
6380x02 - BMC Windows Reset: (V2)
639	The host must assume that its active window has been closed and
640	that it no longer has an active window. The host is not
641	required to perform protocol version negotiation again. The
642	host must not assume that any commands which didn't respond as such
643	succeeded.
644```
645
646Events which cannot be ACKed:
647These events cannot be acknowledged by the host and a call to
648BMC_EVENT_ACK with these bits set will have no effect. The BMC
649will clear these bits when they are no longer applicable.
650```
6510x40 - BMC Flash Control Lost: (V2)
652	The BMC daemon has been suspended and thus no longer
653	controls access to the flash (most likely because some
654	other process on the BMC required direct access to the
655	flash and has suspended the BMC daemon to preclude
656	concurrent access).
657	The BMC daemon must clear this bit itself when it regains
658	control of the flash (the host isn't able to clear it
659	through an acknowledge command).
660	The host must not assume that the contents of the active window
661	correctly reflect the contents of flash while this bit is set.
6620x80 - BMC MBOX Daemon Ready: (V2)
663	Used to inform the host that the BMC daemon is ready to
664	accept command requests. The host isn't able to clear
665	this bit through an acknowledge command, the BMC daemon must
666	clear it before it terminates (assuming it didn't
667	terminate unexpectedly).
668	The host should not expect a response while this bit is
669	not set.
670	Note that this bit being set is not a guarantee that the BMC daemon
671	will respond as it or the BMC may have crashed without clearing
672	it.
673```
674