1 /* Copyright 2013-2016 IBM Corp.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * 	http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __IPMI_H
18 #define __IPMI_H
19 
20 #include <stdint.h>
21 #include <ccan/list/list.h>
22 #include <stdbool.h>
23 #include <types.h>
24 
25 #define MAX_IPMI_SENSORS 255
26 
27 /*
28  * IPMI codes as defined by the standard.
29  */
30 #define IPMI_GET_DEVICE_ID_CMD		0x01
31 #define IPMI_COLD_RESET_CMD		0x02
32 #define IPMI_WARM_RESET_CMD		0x03
33 #define IPMI_CLEAR_MSG_FLAGS_CMD	0x30
34 #define IPMI_GET_DEVICE_GUID_CMD	0x08
35 #define IPMI_GET_MSG_FLAGS_CMD		0x31
36 #define IPMI_SEND_MSG_CMD		0x34
37 #define IPMI_GET_MSG_CMD		0x33
38 #define IPMI_SET_BMC_GLOBAL_ENABLES_CMD	0x2e
39 #define IPMI_GET_BMC_GLOBAL_ENABLES_CMD	0x2f
40 #define IPMI_READ_EVENT_MSG_BUFFER_CMD	0x35
41 #define IPMI_GET_CHANNEL_INFO_CMD	0x42
42 
43 /*
44  * 28. Chassis Commands
45  */
46 #define IPMI_CHASSIS_GET_CAP_CMD		0x00
47 #define IPMI_CHASSIS_GET_STATUS_CMD		0x01
48 #define IPMI_CHASSIS_CONTROL_CMD		0x02
49 #define IPMI_CHASSIS_RESET_CMD			0x03
50 #define IPMI_CHASSIS_IDENTIFY_CMD		0x04
51 #define IPMI_CHASSIS_SET_PANEL_BUTTON_EN_CMD	0x0a
52 #define IPMI_CHASSIS_SET_CAP_CMD		0x05
53 #define IPMI_CHASSIS_SET_PWR_RESTORE_CMD	0x06
54 #define IPMI_CHASSIS_SET_PWR_CYCLE_CMD		0x0b
55 #define IPMI_CHASSIS_GET_SYS_RESTART_CAUSE_CMD	0x07
56 #define IPMI_CHASSIS_SET_SYS_BOOT_OPT_CMD	0x08
57 #define IPMI_CHASSIS_GET_SYS_BOOT_OPT_CMD	0x09
58 #define IPMI_CHASSIS_GET_POH_COUNTER_CMD	0x0f
59 
60 
61 /* 28.3. Chassis Control Command */
62 #define   IPMI_CHASSIS_PWR_DOWN 		0x00
63 #define   IPMI_CHASSIS_PWR_UP			0x01
64 #define   IPMI_CHASSIS_PWR_CYCLE		0x02
65 #define   IPMI_CHASSIS_HARD_RESET		0x03
66 #define   IPMI_CHASSIS_PULSE_DIAG		0x04
67 #define   IPMI_CHASSIS_SOFT_SHUTDOWN		0x05
68 
69 /* 20.7. ACPI Power State Command */
70 #define   IPMI_PWR_SYS_S0_WORKING		0x00
71 #define   IPMI_PWR_SYS_S1			0x01
72 #define   IPMI_PWR_SYS_S2			0x02
73 #define   IPMI_PWR_SYS_S3_SUSPEND_TO_RAM	0x03
74 #define   IPMI_PWR_SYS_S4_SUSPEND_TO_DISK	0x04
75 #define   IPMI_PWR_SYS_S5_SOFT_OFF		0x05
76 #define   IPMI_PWR_SYS_SUSPEND			0x06
77 #define   IPMI_PWR_SYS_LEGACY_ON		0x20
78 #define   IPMI_PWR_SYS_LEGACY_OFF		0x21
79 #define   IPMI_PWR_SYS_UNKNOWN			0x2a
80 #define   IPMI_PWR_NOCHANGE                     0x7f
81 
82 /* 22.{3,4} Clear / Get message flags */
83 #define	IPMI_MESSAGE_FLAGS_RX_MESSAGE_QUEUE	(1<<0)
84 #define IPMI_MESSAGE_FLAGS_EVENT_BUFFER		(1<<1)
85 #define IPMI_MESSAGE_FLAGS_WATCHDOG_PRE_TIMEOUT	(1<<3)
86 #define IPMI_MESSAGE_FLAGS_OEM0			(1<<5)
87 #define IPMI_MESSAGE_FLAGS_OEM1			(1<<6)
88 #define IPMI_MESSAGE_FLAGS_OEM2			(1<<7)
89 
90 /* Firmware Progress Sensor states */
91 #define IPMI_FW_PCI_INIT		0x07
92 #define IPMI_FW_OS_BOOT			0x13
93 #define IPMI_FW_MOTHERBOARD_INIT	0x14
94 
95 #define IPMI_CODE(netfn, cmd)		((netfn) << 8 | (cmd))
96 #define IPMI_CMD(code)			((code) & 0xff)
97 #define IPMI_NETFN(code)		((code) >> 8 & 0xff)
98 
99 #define IPMI_NETFN_RETURN_CODE(netfn)	((netfn) | 0x4)
100 
101 #define IPMI_NETFN_CHASSIS		0x00
102 #define IPMI_NETFN_SE			0x04
103 #define IPMI_NETFN_STORAGE		0x0a
104 #define IPMI_NETFN_APP			0x06
105 
106 #define IPMI_WRITE_FRU			IPMI_CODE(IPMI_NETFN_STORAGE, 0x12)
107 #define IPMI_GET_SEL_INFO		IPMI_CODE(IPMI_NETFN_STORAGE, 0x40)
108 #define IPMI_RESERVE_SEL		IPMI_CODE(IPMI_NETFN_STORAGE, 0x42)
109 #define IPMI_ADD_SEL_EVENT		IPMI_CODE(IPMI_NETFN_STORAGE, 0x44)
110 #define IPMI_GET_SEL_TIME		IPMI_CODE(IPMI_NETFN_STORAGE, 0x48)
111 #define IPMI_SET_SEL_TIME		IPMI_CODE(IPMI_NETFN_STORAGE, 0x49)
112 #define IPMI_CHASSIS_CONTROL		IPMI_CODE(IPMI_NETFN_CHASSIS, 0x02)
113 #define IPMI_CHASSIS_GET_BOOT_OPT	IPMI_CODE(IPMI_NETFN_CHASSIS, 0x09)
114 #define IPMI_BMC_GET_DEVICE_ID		IPMI_CODE(IPMI_NETFN_APP, 0x01)
115 #define IPMI_SET_POWER_STATE		IPMI_CODE(IPMI_NETFN_APP, 0x06)
116 #define IPMI_GET_POWER_STATE		IPMI_CODE(IPMI_NETFN_APP, 0x07)
117 #define IPMI_RESET_WDT			IPMI_CODE(IPMI_NETFN_APP, 0x22)
118 #define IPMI_SET_WDT			IPMI_CODE(IPMI_NETFN_APP, 0x24)
119 #define IPMI_SET_ENABLES		IPMI_CODE(IPMI_NETFN_APP, 0x2E)
120 #define IPMI_GET_ENABLES		IPMI_CODE(IPMI_NETFN_APP, 0x2F)
121 #define IPMI_CLEAR_MESSAGE_FLAGS	IPMI_CODE(IPMI_NETFN_APP, 0x30)
122 #define IPMI_GET_MESSAGE_FLAGS		IPMI_CODE(IPMI_NETFN_APP, 0x31)
123 #define IPMI_GET_MESSAGE		IPMI_CODE(IPMI_NETFN_APP, 0x33)
124 #define IPMI_READ_EVENT			IPMI_CODE(IPMI_NETFN_APP, 0x35)
125 #define IPMI_GET_BT_CAPS		IPMI_CODE(IPMI_NETFN_APP, 0x36)
126 #define IPMI_SET_SENSOR_READING		IPMI_CODE(IPMI_NETFN_SE, 0x30)
127 
128 /*
129  * IPMI response codes.
130  */
131 #define IPMI_CC_NO_ERROR		0x00
132 #define IPMI_NODE_BUSY_ERR		0xc0
133 #define IPMI_INVALID_COMMAND_ERR	0xc1
134 #define IPMI_TIMEOUT_ERR		0xc3
135 #define IPMI_ERR_MSG_TRUNCATED		0xc6
136 #define IPMI_REQ_LEN_INVALID_ERR	0xc7
137 #define IPMI_REQ_LEN_EXCEEDED_ERR	0xc8
138 #define IPMI_NOT_IN_MY_STATE_ERR	0xd5	/* IPMI 2.0 */
139 #define IPMI_LOST_ARBITRATION_ERR	0x81
140 #define IPMI_BUS_ERR			0x82
141 #define IPMI_NAK_ON_WRITE_ERR		0x83
142 #define IPMI_ERR_UNSPECIFIED		0xff
143 
144 #define IPMI_DEFAULT_INTERFACE		0
145 
146 #define IPMI_MAX_REQ_SIZE		60
147 #define IPMI_MAX_RESP_SIZE		60
148 
149 /*
150  * As far as I can tell the size of PEL record is unbounded (due to
151  * the possible presence of the user defined section). We chose this
152  * size because it's what hostboot also uses and most of the OPAL logs
153  * are few hundred bytes.
154  */
155 #define IPMI_MAX_PEL_SIZE		0x800
156 
157 struct ipmi_backend;
158 struct ipmi_msg {
159 	/* Can be used by command implementations to track requests */
160 	struct list_node link;
161 
162 	struct ipmi_backend *backend;
163 	uint8_t netfn;
164 	uint8_t cmd;
165 	uint8_t cc;
166 
167 	/* Called when a response is received to the ipmi message */
168 	void (*complete)(struct ipmi_msg *);
169 
170 	/* Called if non-NULL when the ipmi layer detects an error */
171 	void (*error)(struct ipmi_msg *);
172 	void *user_data;
173 
174 	uint8_t req_size;
175 	uint8_t resp_size;
176 	uint8_t *data;
177 };
178 
179 struct ipmi_backend {
180 	__be64 opal_event_ipmi_recv;
181 	struct ipmi_msg *(*alloc_msg)(size_t, size_t);
182 	void (*free_msg)(struct ipmi_msg *);
183 	int (*queue_msg)(struct ipmi_msg *);
184 	int (*queue_msg_head)(struct ipmi_msg *);
185 	int (*dequeue_msg)(struct ipmi_msg *);
186 	void (*disable_retry)(struct ipmi_msg *);
187 	/*
188 	 * When processing a synchronous IPMI message, pollers may not run, and
189 	 * neither may timers (as the synchronous IPMI message may be being
190 	 * done with locks held, which a timer may then try to also take).
191 	 *
192 	 * So, ensure we have a way to drive any state machines that an IPMI
193 	 * backend may neeed to crank to ensure forward progress.
194 	 */
195 	void (*poll)(void);
196 };
197 
198 extern struct ipmi_backend *ipmi_backend;
199 
200 /* Initialise the IPMI interface */
201 void ipmi_init(void);
202 
203 bool ipmi_present(void);
204 
205 void ipmi_free_msg(struct ipmi_msg *msg);
206 
207 struct ipmi_msg *ipmi_mkmsg_simple(uint32_t code, void *req_data, size_t req_size);
208 struct ipmi_msg *ipmi_mkmsg(int interface, uint32_t code,
209 			    void (*complete)(struct ipmi_msg *),
210 			    void *user_data, void *req_data, size_t req_size,
211 			    size_t resp_size);
212 
213 /* Initialise a previously allocated message with the required
214 fields. The caller must ensure the message is large enough to hold the
215 request and response data. */
216 void ipmi_init_msg(struct ipmi_msg *msg, int interface,
217 		   uint32_t code, void (*complete)(struct ipmi_msg *),
218 		   void *user_data, size_t req_size, size_t resp_size);
219 
220 /* called by backend code to indicate a SMS_ATN event */
221 void ipmi_sms_attention(void);
222 
223 /* Add an ipmi message to the queue */
224 int ipmi_queue_msg(struct ipmi_msg *msg);
225 
226 /* Add an ipmi message to the start of the queue */
227 int ipmi_queue_msg_head(struct ipmi_msg *msg);
228 
229 /* Synchronously send an ipmi message. This won't return until the
230  * messages callback has been called. */
231 void ipmi_queue_msg_sync(struct ipmi_msg *msg);
232 
233 /* Removes the message from the list, queued previously */
234 int ipmi_dequeue_msg(struct ipmi_msg *msg);
235 
236 /* Process a completed message */
237 void ipmi_cmd_done(uint8_t cmd, uint8_t netfn, uint8_t cc, struct ipmi_msg *msg);
238 
239 /* 28.3 Chassis Control Command. Changes the power state of the P8. */
240 int ipmi_chassis_control(uint8_t request);
241 
242 /* 20.7 ACPI Power State Command (without the ACPI part). Informative only,
243  * use chassis control to perform power off and reboot. */
244 int ipmi_set_power_state(uint8_t system, uint8_t device);
245 
246 /* 35.17 Set Sensor Reading Command */
247 int ipmi_set_sensor(uint8_t sensor, uint8_t *reading, size_t len);
248 int ipmi_set_fw_progress_sensor(uint8_t state);
249 
250 /* Register a backend with the ipmi core. Currently we only support one. */
251 void ipmi_register_backend(struct ipmi_backend *backend);
252 
253 /* Allocate IPMI SEL panic message */
254 void ipmi_sel_init(void);
255 
256 /* Register SEL handler with IPMI core */
257 int ipmi_sel_register(uint8_t oem_cmd,
258 		      void (*handler)(uint8_t data, void *context),
259 		      void *context);
260 
261 /* Register rtc ipmi commands with as opal callbacks. */
262 void ipmi_rtc_init(void);
263 
264 /* Register ipmi host interface access callbacks */
265 void ipmi_opal_init(void);
266 
267 /* Populate fru data */
268 void ipmi_fru_init(uint8_t fru_dev_id);
269 
270 /* Commit an error log to the bmc using the OEM add eSEL commands */
271 struct errorlog;
272 int ipmi_elog_commit(struct errorlog *elog_buf);
273 
274 /* Callback to parse an OEM SEL message */
275 void ipmi_parse_sel(struct ipmi_msg *msg);
276 
277 /* Starts the watchdog timer */
278 void ipmi_wdt_init(void);
279 
280 /* Stop the wdt */
281 void ipmi_wdt_stop(void);
282 
283 /* Reset the watchdog timer. Does not return until the timer has been
284  * reset and does not schedule future resets. */
285 void ipmi_wdt_final_reset(void);
286 
287 /* Discover id of settable ipmi sensors */
288 void ipmi_sensor_init(void);
289 
290 /* Get sensor number for given sensor type */
291 uint8_t ipmi_get_sensor_number(uint8_t sensor_type);
292 
293 /* Set the boot count once the OS is up and running */
294 int ipmi_set_boot_count(void);
295 
296 /* Terminate immediate */
297 void __attribute__((noreturn)) ipmi_terminate(const char *msg);
298 
299 /* Get BMC firmware info */
300 extern int ipmi_get_bmc_info_request(void);
301 
302 /* Add BMC firmware info to device tree */
303 extern void ipmi_dt_add_bmc_info(void);
304 
305 /* Get BMC Boot Options info (specifically OEM param 0x62) */
306 int ipmi_get_chassis_boot_opt_request(void);
307 
308 /* Get OEM Boot Option 0x62 for SBE validation flag */
309 int ipmi_chassis_check_sbe_validation(void);
310 
311 #endif
312