xref: /linux-6.15/include/linux/ipmi.h (revision 91e2dd0a)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * ipmi.h
4  *
5  * MontaVista IPMI interface
6  *
7  * Author: MontaVista Software, Inc.
8  *         Corey Minyard <[email protected]>
9  *         [email protected]
10  *
11  * Copyright 2002 MontaVista Software Inc.
12  *
13  */
14 #ifndef __LINUX_IPMI_H
15 #define __LINUX_IPMI_H
16 
17 #include <uapi/linux/ipmi.h>
18 
19 #include <linux/list.h>
20 #include <linux/proc_fs.h>
21 #include <linux/acpi.h> /* For acpi_handle */
22 
23 struct module;
24 struct device;
25 
26 /* Opaque type for a IPMI message user.  One of these is needed to
27    send and receive messages. */
28 typedef struct ipmi_user *ipmi_user_t;
29 
30 /*
31  * Stuff coming from the receive interface comes as one of these.
32  * They are allocated, the receiver must free them with
33  * ipmi_free_recv_msg() when done with the message.  The link is not
34  * used after the message is delivered, so the upper layer may use the
35  * link to build a linked list, if it likes.
36  */
37 struct ipmi_recv_msg {
38 	struct list_head link;
39 
40 	/* The type of message as defined in the "Receive Types"
41 	   defines above. */
42 	int              recv_type;
43 
44 	ipmi_user_t      user;
45 	struct ipmi_addr addr;
46 	long             msgid;
47 	struct kernel_ipmi_msg  msg;
48 
49 	/* The user_msg_data is the data supplied when a message was
50 	   sent, if this is a response to a sent message.  If this is
51 	   not a response to a sent message, then user_msg_data will
52 	   be NULL.  If the user above is NULL, then this will be the
53 	   intf. */
54 	void             *user_msg_data;
55 
56 	/* Call this when done with the message.  It will presumably free
57 	   the message and do any other necessary cleanup. */
58 	void (*done)(struct ipmi_recv_msg *msg);
59 
60 	/* Place-holder for the data, don't make any assumptions about
61 	   the size or existence of this, since it may change. */
62 	unsigned char   msg_data[IPMI_MAX_MSG_LENGTH];
63 };
64 
65 /* Allocate and free the receive message. */
66 void ipmi_free_recv_msg(struct ipmi_recv_msg *msg);
67 
68 struct ipmi_user_hndl {
69 	/* Routine type to call when a message needs to be routed to
70 	   the upper layer.  This will be called with some locks held,
71 	   the only IPMI routines that can be called are ipmi_request
72 	   and the alloc/free operations.  The handler_data is the
73 	   variable supplied when the receive handler was registered. */
74 	void (*ipmi_recv_hndl)(struct ipmi_recv_msg *msg,
75 			       void                 *user_msg_data);
76 
77 	/* Called when the interface detects a watchdog pre-timeout.  If
78 	   this is NULL, it will be ignored for the user. */
79 	void (*ipmi_watchdog_pretimeout)(void *handler_data);
80 
81 	/*
82 	 * If not NULL, called at panic time after the interface has
83 	 * been set up to handle run to completion.
84 	 */
85 	void (*ipmi_panic_handler)(void *handler_data);
86 };
87 
88 /* Create a new user of the IPMI layer on the given interface number. */
89 int ipmi_create_user(unsigned int          if_num,
90 		     const struct ipmi_user_hndl *handler,
91 		     void                  *handler_data,
92 		     ipmi_user_t           *user);
93 
94 /* Destroy the given user of the IPMI layer.  Note that after this
95    function returns, the system is guaranteed to not call any
96    callbacks for the user.  Thus as long as you destroy all the users
97    before you unload a module, you will be safe.  And if you destroy
98    the users before you destroy the callback structures, it should be
99    safe, too. */
100 int ipmi_destroy_user(ipmi_user_t user);
101 
102 /* Get the IPMI version of the BMC we are talking to. */
103 int ipmi_get_version(ipmi_user_t   user,
104 		     unsigned char *major,
105 		     unsigned char *minor);
106 
107 /* Set and get the slave address and LUN that we will use for our
108    source messages.  Note that this affects the interface, not just
109    this user, so it will affect all users of this interface.  This is
110    so some initialization code can come in and do the OEM-specific
111    things it takes to determine your address (if not the BMC) and set
112    it for everyone else.  Note that each channel can have its own address. */
113 int ipmi_set_my_address(ipmi_user_t   user,
114 			unsigned int  channel,
115 			unsigned char address);
116 int ipmi_get_my_address(ipmi_user_t   user,
117 			unsigned int  channel,
118 			unsigned char *address);
119 int ipmi_set_my_LUN(ipmi_user_t   user,
120 		    unsigned int  channel,
121 		    unsigned char LUN);
122 int ipmi_get_my_LUN(ipmi_user_t   user,
123 		    unsigned int  channel,
124 		    unsigned char *LUN);
125 
126 /*
127  * Like ipmi_request, but lets you specify the number of retries and
128  * the retry time.  The retries is the number of times the message
129  * will be resent if no reply is received.  If set to -1, the default
130  * value will be used.  The retry time is the time in milliseconds
131  * between retries.  If set to zero, the default value will be
132  * used.
133  *
134  * Don't use this unless you *really* have to.  It's primarily for the
135  * IPMI over LAN converter; since the LAN stuff does its own retries,
136  * it makes no sense to do it here.  However, this can be used if you
137  * have unusual requirements.
138  */
139 int ipmi_request_settime(ipmi_user_t      user,
140 			 struct ipmi_addr *addr,
141 			 long             msgid,
142 			 struct kernel_ipmi_msg  *msg,
143 			 void             *user_msg_data,
144 			 int              priority,
145 			 int              max_retries,
146 			 unsigned int     retry_time_ms);
147 
148 /*
149  * Like ipmi_request, but with messages supplied.  This will not
150  * allocate any memory, and the messages may be statically allocated
151  * (just make sure to do the "done" handling on them).  Note that this
152  * is primarily for the watchdog timer, since it should be able to
153  * send messages even if no memory is available.  This is subject to
154  * change as the system changes, so don't use it unless you REALLY
155  * have to.
156  */
157 int ipmi_request_supply_msgs(ipmi_user_t          user,
158 			     struct ipmi_addr     *addr,
159 			     long                 msgid,
160 			     struct kernel_ipmi_msg *msg,
161 			     void                 *user_msg_data,
162 			     void                 *supplied_smi,
163 			     struct ipmi_recv_msg *supplied_recv,
164 			     int                  priority);
165 
166 /*
167  * Poll the IPMI interface for the user.  This causes the IPMI code to
168  * do an immediate check for information from the driver and handle
169  * anything that is immediately pending.  This will not block in any
170  * way.  This is useful if you need to spin waiting for something to
171  * happen in the IPMI driver.
172  */
173 void ipmi_poll_interface(ipmi_user_t user);
174 
175 /*
176  * When commands come in to the SMS, the user can register to receive
177  * them.  Only one user can be listening on a specific netfn/cmd/chan tuple
178  * at a time, you will get an EBUSY error if the command is already
179  * registered.  If a command is received that does not have a user
180  * registered, the driver will automatically return the proper
181  * error.  Channels are specified as a bitfield, use IPMI_CHAN_ALL to
182  * mean all channels.
183  */
184 int ipmi_register_for_cmd(ipmi_user_t   user,
185 			  unsigned char netfn,
186 			  unsigned char cmd,
187 			  unsigned int  chans);
188 int ipmi_unregister_for_cmd(ipmi_user_t   user,
189 			    unsigned char netfn,
190 			    unsigned char cmd,
191 			    unsigned int  chans);
192 
193 /*
194  * Go into a mode where the driver will not autonomously attempt to do
195  * things with the interface.  It will still respond to attentions and
196  * interrupts, and it will expect that commands will complete.  It
197  * will not automatcially check for flags, events, or things of that
198  * nature.
199  *
200  * This is primarily used for firmware upgrades.  The idea is that
201  * when you go into firmware upgrade mode, you do this operation
202  * and the driver will not attempt to do anything but what you tell
203  * it or what the BMC asks for.
204  *
205  * Note that if you send a command that resets the BMC, the driver
206  * will still expect a response from that command.  So the BMC should
207  * reset itself *after* the response is sent.  Resetting before the
208  * response is just silly.
209  *
210  * If in auto maintenance mode, the driver will automatically go into
211  * maintenance mode for 30 seconds if it sees a cold reset, a warm
212  * reset, or a firmware NetFN.  This means that code that uses only
213  * firmware NetFN commands to do upgrades will work automatically
214  * without change, assuming it sends a message every 30 seconds or
215  * less.
216  *
217  * See the IPMI_MAINTENANCE_MODE_xxx defines for what the mode means.
218  */
219 int ipmi_get_maintenance_mode(ipmi_user_t user);
220 int ipmi_set_maintenance_mode(ipmi_user_t user, int mode);
221 
222 /*
223  * When the user is created, it will not receive IPMI events by
224  * default.  The user must set this to TRUE to get incoming events.
225  * The first user that sets this to TRUE will receive all events that
226  * have been queued while no one was waiting for events.
227  */
228 int ipmi_set_gets_events(ipmi_user_t user, bool val);
229 
230 /*
231  * Called when a new SMI is registered.  This will also be called on
232  * every existing interface when a new watcher is registered with
233  * ipmi_smi_watcher_register().
234  */
235 struct ipmi_smi_watcher {
236 	struct list_head link;
237 
238 	/* You must set the owner to the current module, if you are in
239 	   a module (generally just set it to "THIS_MODULE"). */
240 	struct module *owner;
241 
242 	/* These two are called with read locks held for the interface
243 	   the watcher list.  So you can add and remove users from the
244 	   IPMI interface, send messages, etc., but you cannot add
245 	   or remove SMI watchers or SMI interfaces. */
246 	void (*new_smi)(int if_num, struct device *dev);
247 	void (*smi_gone)(int if_num);
248 };
249 
250 int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher);
251 int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher);
252 
253 /* The following are various helper functions for dealing with IPMI
254    addresses. */
255 
256 /* Return the maximum length of an IPMI address given it's type. */
257 unsigned int ipmi_addr_length(int addr_type);
258 
259 /* Validate that the given IPMI address is valid. */
260 int ipmi_validate_addr(struct ipmi_addr *addr, int len);
261 
262 /*
263  * How did the IPMI driver find out about the device?
264  */
265 enum ipmi_addr_src {
266 	SI_INVALID = 0, SI_HOTMOD, SI_HARDCODED, SI_SPMI, SI_ACPI, SI_SMBIOS,
267 	SI_PCI,	SI_DEVICETREE, SI_PLATFORM, SI_LAST
268 };
269 const char *ipmi_addr_src_to_str(enum ipmi_addr_src src);
270 
271 union ipmi_smi_info_union {
272 #ifdef CONFIG_ACPI
273 	/*
274 	 * the acpi_info element is defined for the SI_ACPI
275 	 * address type
276 	 */
277 	struct {
278 		acpi_handle acpi_handle;
279 	} acpi_info;
280 #endif
281 };
282 
283 struct ipmi_smi_info {
284 	enum ipmi_addr_src addr_src;
285 
286 	/*
287 	 * Base device for the interface.  Don't forget to put this when
288 	 * you are done.
289 	 */
290 	struct device *dev;
291 
292 	/*
293 	 * The addr_info provides more detailed info for some IPMI
294 	 * devices, depending on the addr_src.  Currently only SI_ACPI
295 	 * info is provided.
296 	 */
297 	union ipmi_smi_info_union addr_info;
298 };
299 
300 /* This is to get the private info of ipmi_smi_t */
301 extern int ipmi_get_smi_info(int if_num, struct ipmi_smi_info *data);
302 
303 #endif /* __LINUX_IPMI_H */
304