xref: /linux/include/linux/ipmi_smi.h (revision 2ace52718376fdb56aca863da2eebe70d7e2ddb1)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * ipmi_smi.h
4  *
5  * MontaVista IPMI system management interface
6  *
7  * Author: MontaVista Software, Inc.
8  *         Corey Minyard <minyard@mvista.com>
9  *         source@mvista.com
10  *
11  * Copyright 2002 MontaVista Software Inc.
12  *
13  */
14 
15 #ifndef __LINUX_IPMI_SMI_H
16 #define __LINUX_IPMI_SMI_H
17 
18 #include <linux/ipmi_msgdefs.h>
19 #include <linux/proc_fs.h>
20 #include <linux/platform_device.h>
21 #include <linux/ipmi.h>
22 
23 struct device;
24 
25 /*
26  * This files describes the interface for IPMI system management interface
27  * drivers to bind into the IPMI message handler.
28  */
29 
30 /* Structure for the low-level drivers. */
31 struct ipmi_smi;
32 
33 /*
34  * Flags for set_check_watch() below.  Tells if the SMI should be
35  * waiting for watchdog timeouts, commands and/or messages.
36  */
37 #define IPMI_WATCH_MASK_CHECK_MESSAGES	(1 << 0)
38 #define IPMI_WATCH_MASK_CHECK_WATCHDOG	(1 << 1)
39 #define IPMI_WATCH_MASK_CHECK_COMMANDS	(1 << 2)
40 
41 /*
42  * SMI messages
43  *
44  * When communicating with an SMI, messages come in two formats:
45  *
46  * * Normal (to a BMC over a BMC interface)
47  *
48  * * IPMB (over a IPMB to another MC)
49  *
50  * When normal, commands are sent using the format defined by a
51  * standard message over KCS (NetFn must be even):
52  *
53  *   +-----------+-----+------+
54  *   | NetFn/LUN | Cmd | Data |
55  *   +-----------+-----+------+
56  *
57  * And responses, similarly, with an completion code added (NetFn must
58  * be odd):
59  *
60  *   +-----------+-----+------+------+
61  *   | NetFn/LUN | Cmd | CC   | Data |
62  *   +-----------+-----+------+------+
63  *
64  * With normal messages, only commands are sent and only responses are
65  * received.
66  *
67  * In IPMB mode, we are acting as an IPMB device. Commands will be in
68  * the following format (NetFn must be even):
69  *
70  *   +-------------+------+-------------+-----+------+
71  *   | NetFn/rsLUN | Addr | rqSeq/rqLUN | Cmd | Data |
72  *   +-------------+------+-------------+-----+------+
73  *
74  * Responses will using the following format:
75  *
76  *   +-------------+------+-------------+-----+------+------+
77  *   | NetFn/rqLUN | Addr | rqSeq/rsLUN | Cmd | CC   | Data |
78  *   +-------------+------+-------------+-----+------+------+
79  *
80  * This is similar to the format defined in the IPMB manual section
81  * 2.11.1 with the checksums and the first address removed.  Also, the
82  * address is always the remote address.
83  *
84  * IPMB messages can be commands and responses in both directions.
85  * Received commands are handled as received commands from the message
86  * queue.
87  */
88 
89 enum ipmi_smi_msg_type {
90 	IPMI_SMI_MSG_TYPE_NORMAL = 0,
91 	IPMI_SMI_MSG_TYPE_IPMB_DIRECT
92 };
93 
94 /*
95  * Messages to/from the lower layer.  The smi interface will take one
96  * of these to send. After the send has occurred and a response has
97  * been received, it will report this same data structure back up to
98  * the upper layer.  If an error occurs, it should fill in the
99  * response with an error code in the completion code location. When
100  * asynchronous data is received, one of these is allocated, the
101  * data_size is set to zero and the response holds the data from the
102  * get message or get event command that the interface initiated.
103  * Note that it is the interfaces responsibility to detect
104  * asynchronous data and messages and request them from the
105  * interface.
106  */
107 struct ipmi_smi_msg {
108 	struct list_head link;
109 
110 	enum ipmi_smi_msg_type type;
111 
112 	long msgid;
113 	/* Response to this message, will be NULL if not from a user request. */
114 	struct ipmi_recv_msg *recv_msg;
115 
116 	int           data_size;
117 	unsigned char data[IPMI_MAX_MSG_LENGTH];
118 
119 	int           rsp_size;
120 	unsigned char rsp[IPMI_MAX_MSG_LENGTH];
121 
122 	/*
123 	 * Will be called when the system is done with the message
124 	 * (presumably to free it).
125 	 */
126 	void (*done)(struct ipmi_smi_msg *msg);
127 };
128 
129 #define INIT_IPMI_SMI_MSG(done_handler) \
130 {						\
131 	.done = done_handler,			\
132 	.type = IPMI_SMI_MSG_TYPE_NORMAL	\
133 }
134 
135 struct ipmi_smi_handlers {
136 	struct module *owner;
137 
138 	/* Capabilities of the SMI. */
139 #define IPMI_SMI_CAN_HANDLE_IPMB_DIRECT		(1 << 0)
140 	unsigned int flags;
141 
142 	/*
143 	 * The low-level interface cannot start sending messages to
144 	 * the upper layer until this function is called.  This may
145 	 * not be NULL, the lower layer must take the interface from
146 	 * this call.
147 	 */
148 	int (*start_processing)(void            *send_info,
149 				struct ipmi_smi *new_intf);
150 
151 	/*
152 	 * When called, the low-level interface should disable all
153 	 * processing, it should be complete shut down when it returns.
154 	 */
155 	void (*shutdown)(void *send_info);
156 
157 	/*
158 	 * Get the detailed private info of the low level interface and store
159 	 * it into the structure of ipmi_smi_data. For example: the
160 	 * ACPI device handle will be returned for the pnp_acpi IPMI device.
161 	 */
162 	int (*get_smi_info)(void *send_info, struct ipmi_smi_info *data);
163 
164 	/*
165 	 * Called to enqueue an SMI message to be sent.  This
166 	 * operation is not allowed to fail.  If an error occurs, it
167 	 * should report back the error in a received message.  It may
168 	 * do this in the current call context, since no write locks
169 	 * are held when this is run.  Message are delivered one at
170 	 * a time by the message handler, a new message will not be
171 	 * delivered until the previous message is returned.
172 	 *
173 	 * This can return an error if the SMI is not in a state where it
174 	 * can send a message.
175 	 */
176 	int (*sender)(void *send_info, struct ipmi_smi_msg *msg);
177 
178 	/*
179 	 * Called by the upper layer to request that we try to get
180 	 * events from the BMC we are attached to.
181 	 */
182 	void (*request_events)(void *send_info);
183 
184 	/*
185 	 * Called by the upper layer when some user requires that the
186 	 * interface watch for received messages and watchdog
187 	 * pretimeouts (basically do a "Get Flags", or not.  Used by
188 	 * the SMI to know if it should watch for these.  This may be
189 	 * NULL if the SMI does not implement it.  watch_mask is from
190 	 * IPMI_WATCH_MASK_xxx above.  The interface should run slower
191 	 * timeouts for just watchdog checking or faster timeouts when
192 	 * waiting for the message queue.
193 	 */
194 	void (*set_need_watch)(void *send_info, unsigned int watch_mask);
195 
196 	/*
197 	 * Called when flushing all pending messages.
198 	 */
199 	void (*flush_messages)(void *send_info);
200 
201 	/*
202 	 * Called when the interface should go into "run to
203 	 * completion" mode.  If this call sets the value to true, the
204 	 * interface should make sure that all messages are flushed
205 	 * out and that none are pending, and any new requests are run
206 	 * to completion immediately.
207 	 */
208 	void (*set_run_to_completion)(void *send_info, bool run_to_completion);
209 
210 	/*
211 	 * Called to poll for work to do.  This is so upper layers can
212 	 * poll for operations during things like crash dumps.
213 	 */
214 	void (*poll)(void *send_info);
215 
216 	/*
217 	 * Enable/disable firmware maintenance mode.  Note that this
218 	 * is *not* the modes defined, this is simply an on/off
219 	 * setting.  The message handler does the mode handling.  Note
220 	 * that this is called from interrupt context, so it cannot
221 	 * block.
222 	 */
223 	void (*set_maintenance_mode)(void *send_info, bool enable);
224 };
225 
226 struct ipmi_device_id {
227 	unsigned char device_id;
228 	unsigned char device_revision;
229 	unsigned char firmware_revision_1;
230 	unsigned char firmware_revision_2;
231 	unsigned char ipmi_version;
232 	unsigned char additional_device_support;
233 	unsigned int  manufacturer_id;
234 	unsigned int  product_id;
235 	unsigned char aux_firmware_revision[4];
236 	unsigned int  aux_firmware_revision_set : 1;
237 };
238 
239 #define ipmi_version_major(v) ((v)->ipmi_version & 0xf)
240 #define ipmi_version_minor(v) ((v)->ipmi_version >> 4)
241 
242 /*
243  * Take a pointer to an IPMI response and extract device id information from
244  * it. @netfn is in the IPMI_NETFN_ format, so may need to be shifted from
245  * a SI response.
246  */
ipmi_demangle_device_id(uint8_t netfn,uint8_t cmd,const unsigned char * data,unsigned int data_len,struct ipmi_device_id * id)247 static inline int ipmi_demangle_device_id(uint8_t netfn, uint8_t cmd,
248 					  const unsigned char *data,
249 					  unsigned int data_len,
250 					  struct ipmi_device_id *id)
251 {
252 	if (data_len < 7)
253 		return -EINVAL;
254 	if (netfn != IPMI_NETFN_APP_RESPONSE || cmd != IPMI_GET_DEVICE_ID_CMD)
255 		/* Strange, didn't get the response we expected. */
256 		return -EINVAL;
257 	if (data[0] != 0)
258 		/* That's odd, it shouldn't be able to fail. */
259 		return -EINVAL;
260 
261 	data++;
262 	data_len--;
263 
264 	id->device_id = data[0];
265 	id->device_revision = data[1];
266 	id->firmware_revision_1 = data[2];
267 	id->firmware_revision_2 = data[3];
268 	id->ipmi_version = data[4];
269 	id->additional_device_support = data[5];
270 	if (data_len >= 11) {
271 		id->manufacturer_id = (data[6] | (data[7] << 8) |
272 				       (data[8] << 16));
273 		id->product_id = data[9] | (data[10] << 8);
274 	} else {
275 		id->manufacturer_id = 0;
276 		id->product_id = 0;
277 	}
278 	if (data_len >= 15) {
279 		memcpy(id->aux_firmware_revision, data+11, 4);
280 		id->aux_firmware_revision_set = 1;
281 	} else
282 		id->aux_firmware_revision_set = 0;
283 
284 	return 0;
285 }
286 
287 /*
288  * Add a low-level interface to the IPMI driver.  Note that if the
289  * interface doesn't know its slave address, it should pass in zero.
290  * The low-level interface should not deliver any messages to the
291  * upper layer until the start_processing() function in the handlers
292  * is called, and the lower layer must get the interface from that
293  * call.
294  */
295 int ipmi_add_smi(struct module            *owner,
296 		 const struct ipmi_smi_handlers *handlers,
297 		 void                     *send_info,
298 		 struct device            *dev,
299 		 unsigned char            slave_addr);
300 
301 #define ipmi_register_smi(handlers, send_info, dev, slave_addr) \
302 	ipmi_add_smi(THIS_MODULE, handlers, send_info, dev, slave_addr)
303 
304 /*
305  * Remove a low-level interface from the IPMI driver.  This will
306  * return an error if the interface is still in use by a user.
307  */
308 void ipmi_unregister_smi(struct ipmi_smi *intf);
309 
310 /*
311  * The lower layer reports received messages through this interface.
312  * The data_size should be zero if this is an asynchronous message.  If
313  * the lower layer gets an error sending a message, it should format
314  * an error response in the message response.
315  */
316 void ipmi_smi_msg_received(struct ipmi_smi     *intf,
317 			   struct ipmi_smi_msg *msg);
318 
319 /* The lower layer received a watchdog pre-timeout on interface. */
320 void ipmi_smi_watchdog_pretimeout(struct ipmi_smi *intf);
321 
322 struct ipmi_smi_msg *ipmi_alloc_smi_msg(void);
ipmi_free_smi_msg(struct ipmi_smi_msg * msg)323 static inline void ipmi_free_smi_msg(struct ipmi_smi_msg *msg)
324 {
325 	msg->done(msg);
326 }
327 
328 #endif /* __LINUX_IPMI_SMI_H */
329