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