Merge tag 'rtc-5.18' of git://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux
[linux-block.git] / include / linux / ipmi_smi.h
CommitLineData
243ac210 1/* SPDX-License-Identifier: GPL-2.0+ */
1da177e4
LT
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 *
1da177e4
LT
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>
50c812b2 20#include <linux/platform_device.h>
16f4232c 21#include <linux/ipmi.h>
1da177e4 22
313162d0
PG
23struct device;
24
6dc1181f
CM
25/*
26 * This files describes the interface for IPMI system management interface
27 * drivers to bind into the IPMI message handler.
28 */
1da177e4
LT
29
30/* Structure for the low-level drivers. */
4372ea94 31struct ipmi_smi;
1da177e4 32
c65ea996
CM
33/*
34 * Flags for set_check_watch() below. Tells if the SMI should be
e1891cff 35 * waiting for watchdog timeouts, commands and/or messages.
c65ea996 36 */
e1891cff
CM
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)
c65ea996 40
059747c2
CM
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
89enum ipmi_smi_msg_type {
90 IPMI_SMI_MSG_TYPE_NORMAL = 0,
91 IPMI_SMI_MSG_TYPE_IPMB_DIRECT
92};
93
1da177e4
LT
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 */
c70d7499 107struct ipmi_smi_msg {
1da177e4
LT
108 struct list_head link;
109
059747c2
CM
110 enum ipmi_smi_msg_type type;
111
1da177e4
LT
112 long msgid;
113 void *user_data;
114
115 int data_size;
116 unsigned char data[IPMI_MAX_MSG_LENGTH];
117
118 int rsp_size;
119 unsigned char rsp[IPMI_MAX_MSG_LENGTH];
120
c65ea996
CM
121 /*
122 * Will be called when the system is done with the message
123 * (presumably to free it).
124 */
1da177e4
LT
125 void (*done)(struct ipmi_smi_msg *msg);
126};
127
c70d7499 128struct ipmi_smi_handlers {
1da177e4
LT
129 struct module *owner;
130
059747c2
CM
131 /* Capabilities of the SMI. */
132#define IPMI_SMI_CAN_HANDLE_IPMB_DIRECT (1 << 0)
133 unsigned int flags;
134
6dc1181f
CM
135 /*
136 * The low-level interface cannot start sending messages to
137 * the upper layer until this function is called. This may
138 * not be NULL, the lower layer must take the interface from
139 * this call.
140 */
5ce1a7dc
CM
141 int (*start_processing)(void *send_info,
142 struct ipmi_smi *new_intf);
453823ba 143
b7780dab
CM
144 /*
145 * When called, the low-level interface should disable all
146 * processing, it should be complete shut down when it returns.
147 */
148 void (*shutdown)(void *send_info);
149
16f4232c
ZY
150 /*
151 * Get the detailed private info of the low level interface and store
152 * it into the structure of ipmi_smi_data. For example: the
153 * ACPI device handle will be returned for the pnp_acpi IPMI device.
154 */
155 int (*get_smi_info)(void *send_info, struct ipmi_smi_info *data);
156
6dc1181f
CM
157 /*
158 * Called to enqueue an SMI message to be sent. This
159 * operation is not allowed to fail. If an error occurs, it
160 * should report back the error in a received message. It may
161 * do this in the current call context, since no write locks
162 * are held when this is run. Message are delivered one at
163 * a time by the message handler, a new message will not be
164 * delivered until the previous message is returned.
165 */
1da177e4 166 void (*sender)(void *send_info,
99ab32f3 167 struct ipmi_smi_msg *msg);
1da177e4 168
6dc1181f
CM
169 /*
170 * Called by the upper layer to request that we try to get
171 * events from the BMC we are attached to.
172 */
1da177e4
LT
173 void (*request_events)(void *send_info);
174
6dc1181f
CM
175 /*
176 * Called by the upper layer when some user requires that the
c65ea996
CM
177 * interface watch for received messages and watchdog
178 * pretimeouts (basically do a "Get Flags", or not. Used by
179 * the SMI to know if it should watch for these. This may be
180 * NULL if the SMI does not implement it. watch_mask is from
181 * IPMI_WATCH_MASK_xxx above. The interface should run slower
182 * timeouts for just watchdog checking or faster timeouts when
183 * waiting for the message queue.
6dc1181f 184 */
c65ea996 185 void (*set_need_watch)(void *send_info, unsigned int watch_mask);
89986496 186
82802f96
HK
187 /*
188 * Called when flushing all pending messages.
189 */
190 void (*flush_messages)(void *send_info);
191
6dc1181f
CM
192 /*
193 * Called when the interface should go into "run to
194 * completion" mode. If this call sets the value to true, the
195 * interface should make sure that all messages are flushed
196 * out and that none are pending, and any new requests are run
197 * to completion immediately.
198 */
7aefac26 199 void (*set_run_to_completion)(void *send_info, bool run_to_completion);
1da177e4 200
6dc1181f
CM
201 /*
202 * Called to poll for work to do. This is so upper layers can
203 * poll for operations during things like crash dumps.
204 */
1da177e4
LT
205 void (*poll)(void *send_info);
206
6dc1181f
CM
207 /*
208 * Enable/disable firmware maintenance mode. Note that this
209 * is *not* the modes defined, this is simply an on/off
210 * setting. The message handler does the mode handling. Note
211 * that this is called from interrupt context, so it cannot
212 * block.
213 */
7aefac26 214 void (*set_maintenance_mode)(void *send_info, bool enable);
1da177e4
LT
215};
216
50c812b2
CM
217struct ipmi_device_id {
218 unsigned char device_id;
219 unsigned char device_revision;
220 unsigned char firmware_revision_1;
221 unsigned char firmware_revision_2;
222 unsigned char ipmi_version;
223 unsigned char additional_device_support;
224 unsigned int manufacturer_id;
225 unsigned int product_id;
226 unsigned char aux_firmware_revision[4];
227 unsigned int aux_firmware_revision_set : 1;
228};
229
230#define ipmi_version_major(v) ((v)->ipmi_version & 0xf)
231#define ipmi_version_minor(v) ((v)->ipmi_version >> 4)
232
6dc1181f
CM
233/*
234 * Take a pointer to an IPMI response and extract device id information from
c468f911
JK
235 * it. @netfn is in the IPMI_NETFN_ format, so may need to be shifted from
236 * a SI response.
237 */
238static inline int ipmi_demangle_device_id(uint8_t netfn, uint8_t cmd,
239 const unsigned char *data,
d8c98618
CM
240 unsigned int data_len,
241 struct ipmi_device_id *id)
50c812b2 242{
c468f911 243 if (data_len < 7)
d8c98618 244 return -EINVAL;
c468f911 245 if (netfn != IPMI_NETFN_APP_RESPONSE || cmd != IPMI_GET_DEVICE_ID_CMD)
d8c98618
CM
246 /* Strange, didn't get the response we expected. */
247 return -EINVAL;
c468f911 248 if (data[0] != 0)
d8c98618
CM
249 /* That's odd, it shouldn't be able to fail. */
250 return -EINVAL;
251
c468f911
JK
252 data++;
253 data_len--;
254
50c812b2
CM
255 id->device_id = data[0];
256 id->device_revision = data[1];
257 id->firmware_revision_1 = data[2];
258 id->firmware_revision_2 = data[3];
259 id->ipmi_version = data[4];
260 id->additional_device_support = data[5];
64e862a5 261 if (data_len >= 11) {
d8c98618
CM
262 id->manufacturer_id = (data[6] | (data[7] << 8) |
263 (data[8] << 16));
264 id->product_id = data[9] | (data[10] << 8);
265 } else {
266 id->manufacturer_id = 0;
267 id->product_id = 0;
268 }
50c812b2
CM
269 if (data_len >= 15) {
270 memcpy(id->aux_firmware_revision, data+11, 4);
271 id->aux_firmware_revision_set = 1;
272 } else
273 id->aux_firmware_revision_set = 0;
d8c98618
CM
274
275 return 0;
50c812b2
CM
276}
277
6dc1181f
CM
278/*
279 * Add a low-level interface to the IPMI driver. Note that if the
280 * interface doesn't know its slave address, it should pass in zero.
281 * The low-level interface should not deliver any messages to the
282 * upper layer until the start_processing() function in the handlers
283 * is called, and the lower layer must get the interface from that
284 * call.
285 */
cbb79863
CM
286int ipmi_add_smi(struct module *owner,
287 const struct ipmi_smi_handlers *handlers,
288 void *send_info,
289 struct device *dev,
290 unsigned char slave_addr);
291
292#define ipmi_register_smi(handlers, send_info, dev, slave_addr) \
293 ipmi_add_smi(THIS_MODULE, handlers, send_info, dev, slave_addr)
1da177e4
LT
294
295/*
296 * Remove a low-level interface from the IPMI driver. This will
297 * return an error if the interface is still in use by a user.
298 */
5ce1a7dc 299void ipmi_unregister_smi(struct ipmi_smi *intf);
1da177e4
LT
300
301/*
302 * The lower layer reports received messages through this interface.
b3834be5 303 * The data_size should be zero if this is an asynchronous message. If
1da177e4
LT
304 * the lower layer gets an error sending a message, it should format
305 * an error response in the message response.
306 */
5ce1a7dc 307void ipmi_smi_msg_received(struct ipmi_smi *intf,
1da177e4
LT
308 struct ipmi_smi_msg *msg);
309
310/* The lower layer received a watchdog pre-timeout on interface. */
5ce1a7dc 311void ipmi_smi_watchdog_pretimeout(struct ipmi_smi *intf);
1da177e4
LT
312
313struct ipmi_smi_msg *ipmi_alloc_smi_msg(void);
314static inline void ipmi_free_smi_msg(struct ipmi_smi_msg *msg)
315{
316 msg->done(msg);
317}
318
1da177e4 319#endif /* __LINUX_IPMI_SMI_H */