1 /*
2  * ipmi.h
3  *
4  * MontaVista IPMI interface
5  *
6  * Author: MontaVista Software, Inc.
7  *         Corey Minyard <minyard@mvista.com>
8  *         source@mvista.com
9  *
10  * Copyright 2002 MontaVista Software Inc.
11  *
12  *  This program is free software; you can redistribute it and/or modify it
13  *  under the terms of the GNU General Public License as published by the
14  *  Free Software Foundation; either version 2 of the License, or (at your
15  *  option) any later version.
16  *
17  *
18  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
19  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
26  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
27  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  *  You should have received a copy of the GNU General Public License along
30  *  with this program; if not, write to the Free Software Foundation, Inc.,
31  *  675 Mass Ave, Cambridge, MA 02139, USA.
32  */
33 
34 #ifndef __LINUX_IPMI_H
35 #define __LINUX_IPMI_H
36 
37 #include <linux/ipmi_msgdefs.h>
38 
39 /*
40  * This file describes an interface to an IPMI driver.  You have to
41  * have a fairly good understanding of IPMI to use this, so go read
42  * the specs first before actually trying to do anything.
43  *
44  * With that said, this driver provides a multi-user interface to the
45  * IPMI driver, and it allows multiple IPMI physical interfaces below
46  * the driver.  The physical interfaces bind as a lower layer on the
47  * driver.  They appear as interfaces to the application using this
48  * interface.
49  *
50  * Multi-user means that multiple applications may use the driver,
51  * send commands, receive responses, etc.  The driver keeps track of
52  * commands the user sends and tracks the responses.  The responses
53  * will go back to the application that send the command.  If the
54  * response doesn't come back in time, the driver will return a
55  * timeout error response to the application.  Asynchronous events
56  * from the BMC event queue will go to all users bound to the driver.
57  * The incoming event queue in the BMC will automatically be flushed
58  * if it becomes full and it is queried once a second to see if
59  * anything is in it.  Incoming commands to the driver will get
60  * delivered as commands.
61  *
62  * This driver provides two main interfaces: one for in-kernel
63  * applications and another for userland applications.  The
64  * capabilities are basically the same for both interface, although
65  * the interfaces are somewhat different.  The stuff in the
66  * #ifdef KERNEL below is the in-kernel interface.  The userland
67  * interface is defined later in the file.  */
68 
69 
70 
71 /*
72  * This is an overlay for all the address types, so it's easy to
73  * determine the actual address type.  This is kind of like addresses
74  * work for sockets.
75  */
76 #define IPMI_MAX_ADDR_SIZE 32
77 struct ipmi_addr
78 {
79 	 /* Try to take these from the "Channel Medium Type" table
80 	    in section 6.5 of the IPMI 1.5 manual. */
81 	int   addr_type;
82 	short channel;
83 	char  data[IPMI_MAX_ADDR_SIZE];
84 };
85 
86 /*
87  * When the address is not used, the type will be set to this value.
88  * The channel is the BMC's channel number for the channel (usually
89  * 0), or IPMC_BMC_CHANNEL if communicating directly with the BMC.
90  */
91 #define IPMI_SYSTEM_INTERFACE_ADDR_TYPE	0x0c
92 struct ipmi_system_interface_addr
93 {
94 	int           addr_type;
95 	short         channel;
96 	unsigned char lun;
97 };
98 
99 /* An IPMB Address. */
100 #define IPMI_IPMB_ADDR_TYPE		0x01
101 /* Used for broadcast get device id as described in section 17.9 of the
102    IPMI 1.5 manual. */
103 #define IPMI_IPMB_BROADCAST_ADDR_TYPE	0x41
104 struct ipmi_ipmb_addr
105 {
106 	int           addr_type;
107 	short         channel;
108 	unsigned char slave_addr;
109 	unsigned char lun;
110 };
111 
112 
113 /*
114  * Channel for talking directly with the BMC.  When using this
115  * channel, This is for the system interface address type only.  FIXME
116  * - is this right, or should we use -1?
117  */
118 #define IPMI_BMC_CHANNEL  0xf
119 #define IPMI_NUM_CHANNELS 0x10
120 
121 
122 /*
123  * A raw IPMI message without any addressing.  This covers both
124  * commands and responses.  The completion code is always the first
125  * byte of data in the response (as the spec shows the messages laid
126  * out).
127  */
128 struct ipmi_msg
129 {
130 	unsigned char  netfn;
131 	unsigned char  cmd;
132 	unsigned short data_len;
133 	unsigned char  *data;
134 };
135 
136 /*
137  * Various defines that are useful for IPMI applications.
138  */
139 #define IPMI_INVALID_CMD_COMPLETION_CODE	0xC1
140 #define IPMI_TIMEOUT_COMPLETION_CODE		0xC3
141 #define IPMI_UNKNOWN_ERR_COMPLETION_CODE	0xff
142 
143 
144 /*
145  * Receive types for messages coming from the receive interface.  This
146  * is used for the receive in-kernel interface and in the receive
147  * IOCTL.
148  */
149 #define IPMI_RESPONSE_RECV_TYPE		1 /* A response to a command */
150 #define IPMI_ASYNC_EVENT_RECV_TYPE	2 /* Something from the event queue */
151 #define IPMI_CMD_RECV_TYPE		3 /* A command from somewhere else */
152 /* Note that async events and received commands do not have a completion
153    code as the first byte of the incoming data, unlike a response. */
154 
155 
156 
157 #ifdef __KERNEL__
158 
159 /*
160  * The in-kernel interface.
161  */
162 #include <linux/list.h>
163 
164 /* Opaque type for a IPMI message user.  One of these is needed to
165    send and receive messages. */
166 typedef struct ipmi_user *ipmi_user_t;
167 
168 /*
169  * Stuff coming from the recieve interface comes as one of these.
170  * They are allocated, the receiver must free them with
171  * ipmi_free_recv_msg() when done with the message.  The link is not
172  * used after the message is delivered, so the upper layer may use the
173  * link to build a linked list, if it likes.
174  */
175 struct ipmi_recv_msg
176 {
177 	struct list_head link;
178 
179 	/* The type of message as defined in the "Receive Types"
180            defines above. */
181 	int              recv_type;
182 
183 	ipmi_user_t      user;
184 	struct ipmi_addr addr;
185 	long             msgid;
186 	struct ipmi_msg  msg;
187 
188 	/* Call this when done with the message.  It will presumably free
189 	   the message and do any other necessary cleanup. */
190 	void (*done)(struct ipmi_recv_msg *msg);
191 
192 	/* Place-holder for the data, don't make any assumptions about
193 	   the size or existance of this, since it may change. */
194 	unsigned char   msg_data[IPMI_MAX_MSG_LENGTH];
195 };
196 
197 /* Allocate and free the receive message. */
ipmi_free_recv_msg(struct ipmi_recv_msg * msg)198 static inline void ipmi_free_recv_msg(struct ipmi_recv_msg *msg)
199 {
200 	msg->done(msg);
201 }
202 struct ipmi_recv_msg *ipmi_alloc_recv_msg(void);
203 
204 struct ipmi_user_hndl
205 {
206         /* Routine type to call when a message needs to be routed to
207 	   the upper layer.  This will be called with some locks held,
208 	   the only IPMI routines that can be called are ipmi_request
209 	   and the alloc/free operations. */
210 	void (*ipmi_recv_hndl)(struct ipmi_recv_msg *msg,
211 			       void                 *handler_data);
212 
213 	/* Called when the interface detects a watchdog pre-timeout.  If
214 	   this is NULL, it will be ignored for the user. */
215 	void (*ipmi_watchdog_pretimeout)(void *handler_data);
216 };
217 
218 /* Create a new user of the IPMI layer on the given interface number. */
219 int ipmi_create_user(unsigned int          if_num,
220 		     struct ipmi_user_hndl *handler,
221 		     void                  *handler_data,
222 		     ipmi_user_t           *user);
223 
224 /* Destroy the given user of the IPMI layer. */
225 int ipmi_destroy_user(ipmi_user_t user);
226 
227 /* Get the IPMI version of the BMC we are talking to. */
228 void ipmi_get_version(ipmi_user_t   user,
229 		      unsigned char *major,
230 		      unsigned char *minor);
231 
232 /* Set and get the slave address and LUN that we will use for our
233    source messages.  Note that this affects the interface, not just
234    this user, so it will affect all users of this interface.  This is
235    so some initialization code can come in and do the OEM-specific
236    things it takes to determine your address (if not the BMC) and set
237    it for everyone else. */
238 void ipmi_set_my_address(ipmi_user_t   user,
239 			 unsigned char address);
240 unsigned char ipmi_get_my_address(ipmi_user_t user);
241 void ipmi_set_my_LUN(ipmi_user_t   user,
242 		     unsigned char LUN);
243 unsigned char ipmi_get_my_LUN(ipmi_user_t user);
244 
245 /*
246  * Send a command request from the given user.  The address is the
247  * proper address for the channel type.  If this is a command, then
248  * the message response comes back, the receive handler for this user
249  * will be called with the given msgid value in the recv msg.  If this
250  * is a response to a command, then the msgid will be used as the
251  * sequence number for the response (truncated if necessary), so when
252  * sending a response you should use the sequence number you received
253  * in the msgid field of the received command.  If the priority is >
254  * 0, the message will go into a high-priority queue and be sent
255  * first.  Otherwise, it goes into a normal-priority queue.
256  */
257 int ipmi_request(ipmi_user_t      user,
258 		 struct ipmi_addr *addr,
259 		 long             msgid,
260 		 struct ipmi_msg  *msg,
261 		 int              priority);
262 
263 /*
264  * Like ipmi_request, but lets you specify the slave return address.
265  */
266 int ipmi_request_with_source(ipmi_user_t      user,
267 			     struct ipmi_addr *addr,
268 			     long             msgid,
269 			     struct ipmi_msg  *msg,
270 			     int              priority,
271 			     unsigned char    source_address,
272 			     unsigned char    source_lun);
273 
274 /*
275  * Like ipmi_request, but with messages supplied.  This will not
276  * allocate any memory, and the messages may be statically allocated
277  * (just make sure to do the "done" handling on them).  Note that this
278  * is primarily for the watchdog timer, since it should be able to
279  * send messages even if no memory is available.  This is subject to
280  * change as the system changes, so don't use it unless you REALLY
281  * have to.
282  */
283 int ipmi_request_supply_msgs(ipmi_user_t          user,
284 			     struct ipmi_addr     *addr,
285 			     long                 msgid,
286 			     struct ipmi_msg      *msg,
287 			     void                 *supplied_smi,
288 			     struct ipmi_recv_msg *supplied_recv,
289 			     int                  priority);
290 
291 /*
292  * When commands come in to the SMS, the user can register to receive
293  * them.  Only one user can be listening on a specific netfn/cmd pair
294  * at a time, you will get an EBUSY error if the command is already
295  * registered.  If a command is received that does not have a user
296  * registered, the driver will automatically return the proper
297  * error.
298  */
299 int ipmi_register_for_cmd(ipmi_user_t   user,
300 			  unsigned char netfn,
301 			  unsigned char cmd);
302 int ipmi_unregister_for_cmd(ipmi_user_t   user,
303 			    unsigned char netfn,
304 			    unsigned char cmd);
305 
306 /*
307  * When the user is created, it will not receive IPMI events by
308  * default.  The user must set this to TRUE to get incoming events.
309  * The first user that sets this to TRUE will receive all events that
310  * have been queued while no one was waiting for events.
311  */
312 int ipmi_set_gets_events(ipmi_user_t user, int val);
313 
314 /*
315  * Register the given user to handle all received IPMI commands.  This
316  * will fail if anyone is registered as a command receiver or if
317  * another is already registered to receive all commands.  NOTE THAT
318  * THIS IS FOR EMULATION USERS ONLY, DO NOT USER THIS FOR NORMAL
319  * STUFF.
320  */
321 int ipmi_register_all_cmd_rcvr(ipmi_user_t user);
322 int ipmi_unregister_all_cmd_rcvr(ipmi_user_t user);
323 
324 
325 /*
326  * Called when a new SMI is registered.  This will also be called on
327  * every existing interface when a new watcher is registered with
328  * ipmi_smi_watcher_register().
329  */
330 struct ipmi_smi_watcher
331 {
332 	struct list_head link;
333 
334 	/* These two are called with read locks held for the interface
335 	   the watcher list.  So you can add and remove users from the
336 	   IPMI interface, send messages, etc., but you cannot add
337 	   or remove SMI watchers or SMI interfaces. */
338 	void (*new_smi)(int if_num);
339 	void (*smi_gone)(int if_num);
340 };
341 
342 int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher);
343 int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher);
344 
345 /* The following are various helper functions for dealing with IPMI
346    addresses. */
347 
348 /* Return the maximum length of an IPMI address given it's type. */
349 unsigned int ipmi_addr_length(int addr_type);
350 
351 /* Validate that the given IPMI address is valid. */
352 int ipmi_validate_addr(struct ipmi_addr *addr, int len);
353 
354 /* Return 1 if the given addresses are equal, 0 if not. */
355 int ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2);
356 
357 #endif /* __KERNEL__ */
358 
359 
360 /*
361  * The userland interface
362  */
363 
364 /*
365  * The userland interface for the IPMI driver is a standard character
366  * device, with each instance of an interface registered as a minor
367  * number under the major character device.
368  *
369  * The read and write calls do not work, to get messages in and out
370  * requires ioctl calls because of the complexity of the data.  select
371  * and poll do work, so you can wait for input using the file
372  * descriptor, you just can use read to get it.
373  *
374  * In general, you send a command down to the interface and receive
375  * responses back.  You can use the msgid value to correlate commands
376  * and responses, the driver will take care of figuring out which
377  * incoming messages are for which command and find the proper msgid
378  * value to report.  You will only receive reponses for commands you
379  * send.  Asynchronous events, however, go to all open users, so you
380  * must be ready to handle these (or ignore them if you don't care).
381  *
382  * The address type depends upon the channel type.  When talking
383  * directly to the BMC (IPMC_BMC_CHANNEL), the address is ignored
384  * (IPMI_UNUSED_ADDR_TYPE).  When talking to an IPMB channel, you must
385  * supply a valid IPMB address with the addr_type set properly.
386  *
387  * When talking to normal channels, the driver takes care of the
388  * details of formatting and sending messages on that channel.  You do
389  * not, for instance, have to format a send command, you just send
390  * whatever command you want to the channel, the driver will create
391  * the send command, automatically issue receive command and get even
392  * commands, and pass those up to the proper user.
393  */
394 
395 
396 /* The magic IOCTL value for this interface. */
397 #define IPMI_IOC_MAGIC 'i'
398 
399 
400 /* Messages sent to the interface are this format. */
401 struct ipmi_req
402 {
403 	unsigned char *addr; /* Address to send the message to. */
404 	unsigned int  addr_len;
405 
406 	long    msgid; /* The sequence number for the message.  This
407 			  exact value will be reported back in the
408 			  response to this request if it is a command.
409 			  If it is a response, this will be used as
410 			  the sequence value for the response.  */
411 
412 	struct ipmi_msg msg;
413 };
414 /*
415  * Send a message to the interfaces.  error values are:
416  *   - EFAULT - an address supplied was invalid.
417  *   - EINVAL - The address supplied was not valid, or the command
418  *              was not allowed.
419  *   - EMSGSIZE - The message to was too large.
420  *   - ENOMEM - Buffers could not be allocated for the command.
421  */
422 #define IPMICTL_SEND_COMMAND		_IOR(IPMI_IOC_MAGIC, 13,	\
423 					     struct ipmi_req)
424 
425 /* Messages received from the interface are this format. */
426 struct ipmi_recv
427 {
428 	int     recv_type; /* Is this a command, response or an
429 			      asyncronous event. */
430 
431 	unsigned char *addr;    /* Address the message was from is put
432 				   here.  The caller must supply the
433 				   memory. */
434 	unsigned int  addr_len; /* The size of the address buffer.
435 				   The caller supplies the full buffer
436 				   length, this value is updated to
437 				   the actual message length when the
438 				   message is received. */
439 
440 	long    msgid; /* The sequence number specified in the request
441 			  if this is a response.  If this is a command,
442 			  this will be the sequence number from the
443 			  command. */
444 
445 	struct ipmi_msg msg; /* The data field must point to a buffer.
446 				The data_size field must be set to the
447 				size of the message buffer.  The
448 				caller supplies the full buffer
449 				length, this value is updated to the
450 				actual message length when the message
451 				is received. */
452 };
453 
454 /*
455  * Receive a message.  error values:
456  *  - EAGAIN - no messages in the queue.
457  *  - EFAULT - an address supplied was invalid.
458  *  - EINVAL - The address supplied was not valid.
459  *  - EMSGSIZE - The message to was too large to fit into the message buffer,
460  *               the message will be left in the buffer. */
461 #define IPMICTL_RECEIVE_MSG		_IOWR(IPMI_IOC_MAGIC, 12,	\
462 					      struct ipmi_recv)
463 
464 /*
465  * Like RECEIVE_MSG, but if the message won't fit in the buffer, it
466  * will truncate the contents instead of leaving the data in the
467  * buffer.
468  */
469 #define IPMICTL_RECEIVE_MSG_TRUNC	_IOWR(IPMI_IOC_MAGIC, 11,	\
470 					      struct ipmi_recv)
471 
472 /* Register to get commands from other entities on this interface. */
473 struct ipmi_cmdspec
474 {
475 	unsigned char netfn;
476 	unsigned char cmd;
477 };
478 
479 /*
480  * Register to receive a specific command.  error values:
481  *   - EFAULT - an address supplied was invalid.
482  *   - EBUSY - The netfn/cmd supplied was already in use.
483  *   - ENOMEM - could not allocate memory for the entry.
484  */
485 #define IPMICTL_REGISTER_FOR_CMD	_IOR(IPMI_IOC_MAGIC, 14,	\
486 					     struct ipmi_cmdspec)
487 /*
488  * Unregister a regsitered command.  error values:
489  *  - EFAULT - an address supplied was invalid.
490  *  - ENOENT - The netfn/cmd was not found registered for this user.
491  */
492 #define IPMICTL_UNREGISTER_FOR_CMD	_IOR(IPMI_IOC_MAGIC, 15,	\
493 					     struct ipmi_cmdspec)
494 
495 /*
496  * Set whether this interface receives events.  Note that the first
497  * user registered for events will get all pending events for the
498  * interface.  error values:
499  *  - EFAULT - an address supplied was invalid.
500  */
501 #define IPMICTL_SET_GETS_EVENTS_CMD	_IOR(IPMI_IOC_MAGIC, 16, int)
502 
503 /*
504  * Set and get the slave address and LUN that we will use for our
505  * source messages.  Note that this affects the interface, not just
506  * this user, so it will affect all users of this interface.  This is
507  * so some initialization code can come in and do the OEM-specific
508  * things it takes to determine your address (if not the BMC) and set
509  * it for everyone else.  You should probably leave the LUN alone.
510  */
511 #define IPMICTL_SET_MY_ADDRESS_CMD	_IOR(IPMI_IOC_MAGIC, 17, unsigned int)
512 #define IPMICTL_GET_MY_ADDRESS_CMD	_IOR(IPMI_IOC_MAGIC, 18, unsigned int)
513 #define IPMICTL_SET_MY_LUN_CMD		_IOR(IPMI_IOC_MAGIC, 19, unsigned int)
514 #define IPMICTL_GET_MY_LUN_CMD		_IOR(IPMI_IOC_MAGIC, 20, unsigned int)
515 
516 #endif /* __LINUX_IPMI_H */
517