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