1*acdbdd14SAndrew JefferyCopyright 2017,2018 IBM 204e8ffdaSSuraj Jitindar Singh 304e8ffdaSSuraj Jitindar SinghLicensed under the Apache License, Version 2.0 (the "License"); 404e8ffdaSSuraj Jitindar Singhyou may not use this file except in compliance with the License. 504e8ffdaSSuraj Jitindar SinghYou may obtain a copy of the License at 604e8ffdaSSuraj Jitindar Singh 704e8ffdaSSuraj Jitindar Singh http://www.apache.org/licenses/LICENSE-2.0 804e8ffdaSSuraj Jitindar Singh 904e8ffdaSSuraj Jitindar SinghUnless required by applicable law or agreed to in writing, software 1004e8ffdaSSuraj Jitindar Singhdistributed under the License is distributed on an "AS IS" BASIS, 1104e8ffdaSSuraj Jitindar SinghWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1204e8ffdaSSuraj Jitindar SinghSee the License for the specific language governing permissions and 1304e8ffdaSSuraj Jitindar Singhlimitations under the License. 1404e8ffdaSSuraj Jitindar Singh 1504e8ffdaSSuraj Jitindar Singh## Intro 1604e8ffdaSSuraj Jitindar Singh 1704e8ffdaSSuraj Jitindar SinghThis document describes the reference mailbox daemon contained in this 1804e8ffdaSSuraj Jitindar Singhrepository. 1904e8ffdaSSuraj Jitindar Singh 2004e8ffdaSSuraj Jitindar Singh## Files 2104e8ffdaSSuraj Jitindar Singh 2204e8ffdaSSuraj Jitindar SinghThe main mailbox daemon is implemented in mboxd.c. This file uses helper 2304e8ffdaSSuraj Jitindar Singhfunctions from the various mboxd_*.c files. 2404e8ffdaSSuraj Jitindar Singh 2504e8ffdaSSuraj Jitindar Singh``` 2697578a6bSGunnar Millsmboxd_dbus.c - Contains the handlers for the D-Bus commands which the daemon can 2704e8ffdaSSuraj Jitindar Singh receive. 2804e8ffdaSSuraj Jitindar Singhmboxd_flash.c - Contains the functions for performing flash access including 2904e8ffdaSSuraj Jitindar Singh read, write and erase. 3097578a6bSGunnar Millsmboxd_lpc.c - Contains the functions for controlling the LPC bus mapping 3104e8ffdaSSuraj Jitindar Singh including pointing the bus so it maps flash and memory. 3204e8ffdaSSuraj Jitindar Singhmboxd_msg.c - Contains the handlers for the mbox commands which the daemon 3304e8ffdaSSuraj Jitindar Singh can receive. 3404e8ffdaSSuraj Jitindar Singhmboxd_windows.c - Contains the functions for managing the window cache. 3504e8ffdaSSuraj Jitindar Singh``` 3604e8ffdaSSuraj Jitindar Singh 3704e8ffdaSSuraj Jitindar Singh## Daemon State 3804e8ffdaSSuraj Jitindar Singh 3904e8ffdaSSuraj Jitindar SinghThe daemon is a state machine with 5 valid states: 4004e8ffdaSSuraj Jitindar Singh 4104e8ffdaSSuraj Jitindar Singh``` 4204e8ffdaSSuraj Jitindar SinghUNINITIALISED - The daemon is still in the initialisation phase and 4304e8ffdaSSuraj Jitindar Singh will not respond 4404e8ffdaSSuraj Jitindar SinghACTIVE_MAPS_FLASH - The daemon is polling for incoming commands, is not 4597578a6bSGunnar Mills currently suspended and the LPC bus maps the flash 4604e8ffdaSSuraj Jitindar Singh device. 4704e8ffdaSSuraj Jitindar SinghSUSPEND_MAPS_FLASH - The daemon is polling for incoming commands, is 4897578a6bSGunnar Mills currently suspended and the LPC bus maps the flash 4904e8ffdaSSuraj Jitindar Singh device. 5004e8ffdaSSuraj Jitindar SinghACTIVE_MAPS_MEM - The daemon is polling for incoming commands, is not 5197578a6bSGunnar Mills currently suspended and the LPC bus maps the reserved 5204e8ffdaSSuraj Jitindar Singh memory region. 5304e8ffdaSSuraj Jitindar SinghSUSPEND_MAPS_MEM - The daemon is polling for incoming commands, is 5497578a6bSGunnar Mills currently suspended and the LPC bus maps the reserved 5504e8ffdaSSuraj Jitindar Singh memory region. 5604e8ffdaSSuraj Jitindar Singh``` 5704e8ffdaSSuraj Jitindar Singh 5804e8ffdaSSuraj Jitindar SinghAs described in the protocol, the daemon can be suspended to allow the BMC to 5904e8ffdaSSuraj Jitindar Singhaccess flash without the daemon interfering. The daemon will still respond to 6004e8ffdaSSuraj Jitindar Singhcommands while suspended but won't allow the host to, or itself, access the 6104e8ffdaSSuraj Jitindar Singhflash. 6204e8ffdaSSuraj Jitindar Singh 6304e8ffdaSSuraj Jitindar Singh### State Transitions 6404e8ffdaSSuraj Jitindar Singh 6504e8ffdaSSuraj Jitindar Singh``` 6604e8ffdaSSuraj Jitindar SinghUNINITIALISED -> ACTIVE_MAPS_FLASH - Uninitiated: Occurs when the daemon is 6704e8ffdaSSuraj Jitindar Singh finished initialising. 6804e8ffdaSSuraj Jitindar SinghACTIVE_MAPS_FLASH -> SUSPEND_MAPS_FLASH - Suspend command received over 6997578a6bSGunnar Mills D-Bus 7004e8ffdaSSuraj Jitindar SinghSUSPEND_MAPS_FLASH -> ACTIVE_MAPS_FLASH - Resume command received over 7197578a6bSGunnar Mills D-Bus 7297578a6bSGunnar MillsACTIVE_MAPS_MEM -> SUSPEND_MAPS_MEM - Suspend command received over D-Bus 7397578a6bSGunnar MillsSUSPEND_MAPS_MEM -> ACTIVE_MAPS_MEM - Resume command received over D-Bus 7404e8ffdaSSuraj Jitindar SinghACTIVE_MAPS_FLASH -> ACTIVE_MAPS_MEM - GET_MBOX_INFO command received 7504e8ffdaSSuraj Jitindar SinghSUSPEND_MAPS_FLASH -> SUSPEND_MAPS_MEM - GET_MBOX_INFO command received 7697578a6bSGunnar MillsACTIVE_MAPS_MEM -> ACTIVE_MAPS_FLASH - Reset D-Bus or mbox command received 7704e8ffdaSSuraj Jitindar SinghSUSPEND_MAPS_MEM -> SUSPEND_MAPS_FLASH - Transition not allowed, we 7804e8ffdaSSuraj Jitindar Singh don't let the host modify flash 7904e8ffdaSSuraj Jitindar Singh while the daemon is suspended. 8004e8ffdaSSuraj Jitindar Singh``` 8104e8ffdaSSuraj Jitindar Singh 8204e8ffdaSSuraj Jitindar Singh## Window Cache 8304e8ffdaSSuraj Jitindar Singh 8404e8ffdaSSuraj Jitindar SinghWhile the protocol describes that there is only one active window at a time, 8504e8ffdaSSuraj Jitindar Singhthe daemon keeps a cache of previously accessed windows to avoid the need to 8604e8ffdaSSuraj Jitindar Singhreload them from flash should the host access them again in the future. 8704e8ffdaSSuraj Jitindar Singh 8804e8ffdaSSuraj Jitindar SinghThe reserved memory region is divided among a number of windows which make up 8904e8ffdaSSuraj Jitindar Singhthe window cache. When the host requests a flash offset the cache is searched 9004e8ffdaSSuraj Jitindar Singhfor one which contains that offset. If one is found we simply point the host to 9197578a6bSGunnar Millsit at the correct LPC offset for that windows location and the requested flash 9204e8ffdaSSuraj Jitindar Singhoffset. 9304e8ffdaSSuraj Jitindar Singh 9404e8ffdaSSuraj Jitindar SinghIf there is no window in the cache which contains the requested flash offset 9504e8ffdaSSuraj Jitindar Singhthen we have to find one to load with the requested flash contents. If there 9697578a6bSGunnar Millsare any windows which are empty then we choose those, otherwise, we choose one to 9704e8ffdaSSuraj Jitindar Singhevict using a least recently used (LRU) scheme. The flash is then copied into 9897578a6bSGunnar Millsthis window and the host pointed at its location on the LPC bus. 9904e8ffdaSSuraj Jitindar Singh 10004e8ffdaSSuraj Jitindar SinghWhen ever the flash is changed we have to invalidate all windows in the window 10104e8ffdaSSuraj Jitindar Singhcache as their contents may no longer accurately reflect those of the flash. 10204e8ffdaSSuraj Jitindar Singh 10304e8ffdaSSuraj Jitindar Singh## Daemon Operation 10404e8ffdaSSuraj Jitindar Singh 10504e8ffdaSSuraj Jitindar Singh### Invocation 10604e8ffdaSSuraj Jitindar Singh 10704e8ffdaSSuraj Jitindar SinghThe daemon is invoked on the command line with a few parameters: 10804e8ffdaSSuraj Jitindar Singh 10904e8ffdaSSuraj Jitindar Singh``` 11004e8ffdaSSuraj Jitindar Singh(*) - required 11104e8ffdaSSuraj Jitindar Singh(#) - optional but recommended 11204e8ffdaSSuraj Jitindar Singh(~) - optional 11304e8ffdaSSuraj Jitindar Singh 11404e8ffdaSSuraj Jitindar Singh--flash * - The size of the PNOR image on the flash device 11504e8ffdaSSuraj Jitindar Singh--window-size # - The size to make each window in the cache 11604e8ffdaSSuraj Jitindar Singh--window-num # - The number of windows to have in the cache 11704e8ffdaSSuraj Jitindar Singh--verbose ~ - Increase the verbosity with which the daemon runs 11897578a6bSGunnar Mills--sys-log ~ - Use the system log rather than outputting to the console 11904e8ffdaSSuraj Jitindar Singh``` 12004e8ffdaSSuraj Jitindar Singh 12104e8ffdaSSuraj Jitindar SinghIf any of the required parameters are missing or invalid an error will be 12204e8ffdaSSuraj Jitindar Singhprinted and the daemon will terminate. 12304e8ffdaSSuraj Jitindar SinghIf window-size and window-num aren't specified then the default is to have a 12404e8ffdaSSuraj Jitindar Singhsingle window which spans the entire reserved memory region. 12504e8ffdaSSuraj Jitindar Singh 12604e8ffdaSSuraj Jitindar Singh### Initialisation 12704e8ffdaSSuraj Jitindar Singh 12804e8ffdaSSuraj Jitindar SinghAfter the command line has been parsed the daemon will initalise its various 12904e8ffdaSSuraj Jitindar Singhinterfaces. If any of these initialisations fail it will print an error and the 13004e8ffdaSSuraj Jitindar Singhdaemon will terminate. 13104e8ffdaSSuraj Jitindar Singh 13297578a6bSGunnar MillsAfter initilisation, the daemon points the LPC mapping to the actual flash 13304e8ffdaSSuraj Jitindar Singhdevice, sets the BMC_READY BMC event and starts polling. 13404e8ffdaSSuraj Jitindar Singh 13504e8ffdaSSuraj Jitindar Singh### Polling 13604e8ffdaSSuraj Jitindar Singh 13704e8ffdaSSuraj Jitindar SinghThe daemon sits in a poll loop waiting for an event to happen on one or more of 13897578a6bSGunnar Millsthe mbox, D-Bus or signal file descriptors. 13904e8ffdaSSuraj Jitindar Singh 14004e8ffdaSSuraj Jitindar Singh#### Handling MBOX Commands 14104e8ffdaSSuraj Jitindar Singh 14204e8ffdaSSuraj Jitindar SinghWhen an event occurs on the mbox file descriptor the mbox request is read from 14304e8ffdaSSuraj Jitindar Singhthe mbox registers and processed. 14404e8ffdaSSuraj Jitindar Singh 14504e8ffdaSSuraj Jitindar SinghThe command is validated and then the appropriate handler called. The response 14604e8ffdaSSuraj Jitindar Singhis then written back to the mbox registers to indicate the outcome of the 14797578a6bSGunnar Millscommand and an interrupt is sent to the host. 14804e8ffdaSSuraj Jitindar Singh 14997578a6bSGunnar Mills#### Handling D-Bus Commands 15004e8ffdaSSuraj Jitindar Singh 15197578a6bSGunnar MillsWhen an event occurs on the D-Bus file descriptor the D-Bus request is read from 15297578a6bSGunnar Millsthe D-Bus interface and processed. 15304e8ffdaSSuraj Jitindar Singh 15404e8ffdaSSuraj Jitindar SinghThe command is validated and then the appropriate handler called. The response 15597578a6bSGunnar Millsis then written back to the D-Bus interface to indicate the outcome of the 15604e8ffdaSSuraj Jitindar Singhcommand. 15704e8ffdaSSuraj Jitindar Singh 15804e8ffdaSSuraj Jitindar Singh#### Handling Signals 15904e8ffdaSSuraj Jitindar Singh 16004e8ffdaSSuraj Jitindar SinghThe daemon blocks SIGINTs, SIGTERMs, and SIGHUPs and instead polls for them on 16104e8ffdaSSuraj Jitindar Singha signal file descriptor. 16204e8ffdaSSuraj Jitindar Singh 16304e8ffdaSSuraj Jitindar SinghWhen an event occurs on this file descriptor the signal received is determined 16404e8ffdaSSuraj Jitindar Singhand processed as follows: 16504e8ffdaSSuraj Jitindar Singh 16604e8ffdaSSuraj Jitindar Singh``` 16704e8ffdaSSuraj Jitindar SinghSIGINT - Terminate the daemon 16804e8ffdaSSuraj Jitindar SinghSIGTERM - Terminate the daemon 16997578a6bSGunnar MillsSIGHUP - Clear the window cache and point the LPC bus mapping back to 17004e8ffdaSSuraj Jitindar Singh flash 17104e8ffdaSSuraj Jitindar Singh``` 17204e8ffdaSSuraj Jitindar Singh 17304e8ffdaSSuraj Jitindar Singh### Termination 17404e8ffdaSSuraj Jitindar Singh 17504e8ffdaSSuraj Jitindar SinghThe daemon can be terminated for multiple reasons; invalid command line, unable 17697578a6bSGunnar Millsto initialise, received SIGINT or SIGTERM, or because it received the kill D-Bus 17704e8ffdaSSuraj Jitindar Singhcommand. 17804e8ffdaSSuraj Jitindar Singh 17997578a6bSGunnar MillsOn termination, the daemon clears the window cache and notifies the host that the 18097578a6bSGunnar Millsactive window has been closed, points the LPC bus mapping back to flash, clears 18104e8ffdaSSuraj Jitindar Singhthe BMC_READY BMC event bit and frees all of its allocated resources. 182