GNU Linux-libre 4.19.286-gnu1
[releases.git] / include / linux / ipmi_smi.h
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 typedef struct ipmi_smi *ipmi_smi_t;
32
33 /*
34  * Flags for set_check_watch() below.  Tells if the SMI should be
35  * waiting for watchdog timeouts, commands and/or messages.  There is
36  * also an internal flag for the message handler, SMIs should ignore
37  * it.
38  */
39 #define IPMI_WATCH_MASK_INTERNAL        (1 << 0)
40 #define IPMI_WATCH_MASK_CHECK_MESSAGES  (1 << 1)
41 #define IPMI_WATCH_MASK_CHECK_WATCHDOG  (1 << 2)
42 #define IPMI_WATCH_MASK_CHECK_COMMANDS  (1 << 3)
43
44 /*
45  * Messages to/from the lower layer.  The smi interface will take one
46  * of these to send. After the send has occurred and a response has
47  * been received, it will report this same data structure back up to
48  * the upper layer.  If an error occurs, it should fill in the
49  * response with an error code in the completion code location. When
50  * asynchronous data is received, one of these is allocated, the
51  * data_size is set to zero and the response holds the data from the
52  * get message or get event command that the interface initiated.
53  * Note that it is the interfaces responsibility to detect
54  * asynchronous data and messages and request them from the
55  * interface.
56  */
57 struct ipmi_smi_msg {
58         struct list_head link;
59
60         long    msgid;
61         void    *user_data;
62
63         int           data_size;
64         unsigned char data[IPMI_MAX_MSG_LENGTH];
65
66         int           rsp_size;
67         unsigned char rsp[IPMI_MAX_MSG_LENGTH];
68
69         /*
70          * There should be a response message coming back in the BMC
71          * message queue.
72          */
73         bool needs_response;
74
75         /*
76          * Will be called when the system is done with the message
77          * (presumably to free it).
78          */
79         void (*done)(struct ipmi_smi_msg *msg);
80 };
81
82 struct ipmi_smi_handlers {
83         struct module *owner;
84
85         /*
86          * The low-level interface cannot start sending messages to
87          * the upper layer until this function is called.  This may
88          * not be NULL, the lower layer must take the interface from
89          * this call.
90          */
91         int (*start_processing)(void            *send_info,
92                                 struct ipmi_smi *new_intf);
93
94         /*
95          * When called, the low-level interface should disable all
96          * processing, it should be complete shut down when it returns.
97          */
98         void (*shutdown)(void *send_info);
99
100         /*
101          * Get the detailed private info of the low level interface and store
102          * it into the structure of ipmi_smi_data. For example: the
103          * ACPI device handle will be returned for the pnp_acpi IPMI device.
104          */
105         int (*get_smi_info)(void *send_info, struct ipmi_smi_info *data);
106
107         /*
108          * Called to enqueue an SMI message to be sent.  This
109          * operation is not allowed to fail.  If an error occurs, it
110          * should report back the error in a received message.  It may
111          * do this in the current call context, since no write locks
112          * are held when this is run.  Message are delivered one at
113          * a time by the message handler, a new message will not be
114          * delivered until the previous message is returned.
115          */
116         void (*sender)(void                *send_info,
117                        struct ipmi_smi_msg *msg);
118
119         /*
120          * Called by the upper layer to request that we try to get
121          * events from the BMC we are attached to.
122          */
123         void (*request_events)(void *send_info);
124
125         /*
126          * Called by the upper layer when some user requires that the
127          * interface watch for received messages and watchdog
128          * pretimeouts (basically do a "Get Flags", or not.  Used by
129          * the SMI to know if it should watch for these.  This may be
130          * NULL if the SMI does not implement it.  watch_mask is from
131          * IPMI_WATCH_MASK_xxx above.  The interface should run slower
132          * timeouts for just watchdog checking or faster timeouts when
133          * waiting for the message queue.
134          */
135         void (*set_need_watch)(void *send_info, unsigned int watch_mask);
136
137         /*
138          * Called when flushing all pending messages.
139          */
140         void (*flush_messages)(void *send_info);
141
142         /*
143          * Called when the interface should go into "run to
144          * completion" mode.  If this call sets the value to true, the
145          * interface should make sure that all messages are flushed
146          * out and that none are pending, and any new requests are run
147          * to completion immediately.
148          */
149         void (*set_run_to_completion)(void *send_info, bool run_to_completion);
150
151         /*
152          * Called to poll for work to do.  This is so upper layers can
153          * poll for operations during things like crash dumps.
154          */
155         void (*poll)(void *send_info);
156
157         /*
158          * Enable/disable firmware maintenance mode.  Note that this
159          * is *not* the modes defined, this is simply an on/off
160          * setting.  The message handler does the mode handling.  Note
161          * that this is called from interrupt context, so it cannot
162          * block.
163          */
164         void (*set_maintenance_mode)(void *send_info, bool enable);
165 };
166
167 struct ipmi_device_id {
168         unsigned char device_id;
169         unsigned char device_revision;
170         unsigned char firmware_revision_1;
171         unsigned char firmware_revision_2;
172         unsigned char ipmi_version;
173         unsigned char additional_device_support;
174         unsigned int  manufacturer_id;
175         unsigned int  product_id;
176         unsigned char aux_firmware_revision[4];
177         unsigned int  aux_firmware_revision_set : 1;
178 };
179
180 #define ipmi_version_major(v) ((v)->ipmi_version & 0xf)
181 #define ipmi_version_minor(v) ((v)->ipmi_version >> 4)
182
183 /*
184  * Take a pointer to an IPMI response and extract device id information from
185  * it. @netfn is in the IPMI_NETFN_ format, so may need to be shifted from
186  * a SI response.
187  */
188 static inline int ipmi_demangle_device_id(uint8_t netfn, uint8_t cmd,
189                                           const unsigned char *data,
190                                           unsigned int data_len,
191                                           struct ipmi_device_id *id)
192 {
193         if (data_len < 7)
194                 return -EINVAL;
195         if (netfn != IPMI_NETFN_APP_RESPONSE || cmd != IPMI_GET_DEVICE_ID_CMD)
196                 /* Strange, didn't get the response we expected. */
197                 return -EINVAL;
198         if (data[0] != 0)
199                 /* That's odd, it shouldn't be able to fail. */
200                 return -EINVAL;
201
202         data++;
203         data_len--;
204
205         id->device_id = data[0];
206         id->device_revision = data[1];
207         id->firmware_revision_1 = data[2];
208         id->firmware_revision_2 = data[3];
209         id->ipmi_version = data[4];
210         id->additional_device_support = data[5];
211         if (data_len >= 11) {
212                 id->manufacturer_id = (data[6] | (data[7] << 8) |
213                                        (data[8] << 16));
214                 id->product_id = data[9] | (data[10] << 8);
215         } else {
216                 id->manufacturer_id = 0;
217                 id->product_id = 0;
218         }
219         if (data_len >= 15) {
220                 memcpy(id->aux_firmware_revision, data+11, 4);
221                 id->aux_firmware_revision_set = 1;
222         } else
223                 id->aux_firmware_revision_set = 0;
224
225         return 0;
226 }
227
228 /*
229  * Add a low-level interface to the IPMI driver.  Note that if the
230  * interface doesn't know its slave address, it should pass in zero.
231  * The low-level interface should not deliver any messages to the
232  * upper layer until the start_processing() function in the handlers
233  * is called, and the lower layer must get the interface from that
234  * call.
235  */
236 int ipmi_add_smi(struct module            *owner,
237                  const struct ipmi_smi_handlers *handlers,
238                  void                     *send_info,
239                  struct device            *dev,
240                  unsigned char            slave_addr);
241
242 #define ipmi_register_smi(handlers, send_info, dev, slave_addr) \
243         ipmi_add_smi(THIS_MODULE, handlers, send_info, dev, slave_addr)
244
245 /*
246  * Remove a low-level interface from the IPMI driver.  This will
247  * return an error if the interface is still in use by a user.
248  */
249 void ipmi_unregister_smi(struct ipmi_smi *intf);
250
251 /*
252  * The lower layer reports received messages through this interface.
253  * The data_size should be zero if this is an asynchronous message.  If
254  * the lower layer gets an error sending a message, it should format
255  * an error response in the message response.
256  */
257 void ipmi_smi_msg_received(struct ipmi_smi     *intf,
258                            struct ipmi_smi_msg *msg);
259
260 /* The lower layer received a watchdog pre-timeout on interface. */
261 void ipmi_smi_watchdog_pretimeout(struct ipmi_smi *intf);
262
263 struct ipmi_smi_msg *ipmi_alloc_smi_msg(void);
264 static inline void ipmi_free_smi_msg(struct ipmi_smi_msg *msg)
265 {
266         msg->done(msg);
267 }
268
269 #endif /* __LINUX_IPMI_SMI_H */