blob: 5fa83f751378fb56ecc7da11a16d2c8386fb6862 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ipmi_msghandler.c
3 *
4 * Incoming and outgoing message routing for an 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/module.h>
35#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/poll.h>
Alexey Dobriyana99bbaf2009-10-04 16:11:37 +040037#include <linux/sched.h>
Alexey Dobriyan07412732011-05-26 16:25:55 -070038#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/spinlock.h>
Corey Minyardd6dfd132006-03-31 02:30:41 -080040#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/slab.h>
42#include <linux/ipmi.h>
43#include <linux/ipmi_smi.h>
44#include <linux/notifier.h>
45#include <linux/init.h>
46#include <linux/proc_fs.h>
Corey Minyard393d2cc2005-11-07 00:59:54 -080047#include <linux/rcupdate.h>
Corey Minyard7adf5792012-03-28 14:42:49 -070048#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#define PFX "IPMI message handler: "
Corey Minyard1fdd75b2005-09-06 15:18:42 -070051
Corey Minyardf7caa1b2008-04-29 01:01:04 -070052#define IPMI_DRIVER_VERSION "39.2"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void);
55static int ipmi_init_msghandler(void);
Corey Minyard7adf5792012-03-28 14:42:49 -070056static void smi_recv_tasklet(unsigned long);
57static void handle_new_recv_msgs(ipmi_smi_t intf);
Corey Minyard89986492014-04-14 09:46:54 -050058static void need_waiter(ipmi_smi_t intf);
Corey Minyard7ea0ed22014-11-06 16:58:48 -060059static int handle_one_recv_msg(ipmi_smi_t intf,
60 struct ipmi_smi_msg *msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Randy Dunlap0c8204b2006-12-10 02:19:06 -080062static int initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Corey Minyard3b625942005-06-23 22:01:42 -070064#ifdef CONFIG_PROC_FS
Randy Dunlap0c8204b2006-12-10 02:19:06 -080065static struct proc_dir_entry *proc_ipmi_root;
Corey Minyard3b625942005-06-23 22:01:42 -070066#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Corey Minyardb9675132006-12-06 20:41:02 -080068/* Remain in auto-maintenance mode for this amount of time (in ms). */
69#define IPMI_MAINTENANCE_MODE_TIMEOUT 30000
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define MAX_EVENTS_IN_QUEUE 25
72
Corey Minyardc70d7492008-04-29 01:01:09 -070073/*
74 * Don't let a message sit in a queue forever, always time it with at lest
75 * the max message timer. This is in milliseconds.
76 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define MAX_MSG_TIMEOUT 60000
78
Corey Minyard89986492014-04-14 09:46:54 -050079/* Call every ~1000 ms. */
80#define IPMI_TIMEOUT_TIME 1000
81
82/* How many jiffies does it take to get to the timeout time. */
83#define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000)
84
85/*
86 * Request events from the queue every second (this is the number of
87 * IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the
88 * future, IPMI will add a way to know immediately if an event is in
89 * the queue and this silliness can go away.
90 */
91#define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME))
92
Corey Minyard393d2cc2005-11-07 00:59:54 -080093/*
94 * The main "user" data structure.
95 */
Corey Minyardc70d7492008-04-29 01:01:09 -070096struct ipmi_user {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 struct list_head link;
98
Corey Minyard7aefac22014-04-14 09:46:56 -050099 /* Set to false when the user is destroyed. */
100 bool valid;
Corey Minyard393d2cc2005-11-07 00:59:54 -0800101
102 struct kref refcount;
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 /* The upper layer that handles receive messages. */
105 struct ipmi_user_hndl *handler;
106 void *handler_data;
107
108 /* The interface this user is bound to. */
109 ipmi_smi_t intf;
110
111 /* Does this interface receive IPMI events? */
Corey Minyard89986492014-04-14 09:46:54 -0500112 bool gets_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
Corey Minyardc70d7492008-04-29 01:01:09 -0700115struct cmd_rcvr {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 struct list_head link;
117
118 ipmi_user_t user;
119 unsigned char netfn;
120 unsigned char cmd;
Corey Minyardc69c3122006-09-30 23:27:56 -0700121 unsigned int chans;
Corey Minyard393d2cc2005-11-07 00:59:54 -0800122
123 /*
124 * This is used to form a linked lised during mass deletion.
125 * Since this is in an RCU list, we cannot use the link above
126 * or change any data until the RCU period completes. So we
127 * use this next variable during mass deletion so we can have
128 * a list and don't have to wait and restart the search on
Corey Minyardc70d7492008-04-29 01:01:09 -0700129 * every individual deletion of a command.
130 */
Corey Minyard393d2cc2005-11-07 00:59:54 -0800131 struct cmd_rcvr *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132};
133
Corey Minyardc70d7492008-04-29 01:01:09 -0700134struct seq_table {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 unsigned int inuse : 1;
136 unsigned int broadcast : 1;
137
138 unsigned long timeout;
139 unsigned long orig_timeout;
140 unsigned int retries_left;
141
Corey Minyardc70d7492008-04-29 01:01:09 -0700142 /*
143 * To verify on an incoming send message response that this is
144 * the message that the response is for, we keep a sequence id
145 * and increment it every time we send a message.
146 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 long seqid;
148
Corey Minyardc70d7492008-04-29 01:01:09 -0700149 /*
150 * This is held so we can properly respond to the message on a
151 * timeout, and it is used to hold the temporary data for
152 * retransmission, too.
153 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 struct ipmi_recv_msg *recv_msg;
155};
156
Corey Minyardc70d7492008-04-29 01:01:09 -0700157/*
158 * Store the information in a msgid (long) to allow us to find a
159 * sequence table entry from the msgid.
160 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#define STORE_SEQ_IN_MSGID(seq, seqid) (((seq&0xff)<<26) | (seqid&0x3ffffff))
162
163#define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
164 do { \
165 seq = ((msgid >> 26) & 0x3f); \
166 seqid = (msgid & 0x3fffff); \
Corey Minyardc70d7492008-04-29 01:01:09 -0700167 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169#define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3fffff)
170
Corey Minyardc70d7492008-04-29 01:01:09 -0700171struct ipmi_channel {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 unsigned char medium;
173 unsigned char protocol;
Corey Minyardc14979b2005-09-06 15:18:38 -0700174
Corey Minyardc70d7492008-04-29 01:01:09 -0700175 /*
176 * My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR,
177 * but may be changed by the user.
178 */
Corey Minyardc14979b2005-09-06 15:18:38 -0700179 unsigned char address;
180
Corey Minyardc70d7492008-04-29 01:01:09 -0700181 /*
182 * My LUN. This should generally stay the SMS LUN, but just in
183 * case...
184 */
Corey Minyardc14979b2005-09-06 15:18:38 -0700185 unsigned char lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186};
187
Corey Minyard3b625942005-06-23 22:01:42 -0700188#ifdef CONFIG_PROC_FS
Corey Minyardc70d7492008-04-29 01:01:09 -0700189struct ipmi_proc_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 char *name;
191 struct ipmi_proc_entry *next;
192};
Corey Minyard3b625942005-06-23 22:01:42 -0700193#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Corey Minyardc70d7492008-04-29 01:01:09 -0700195struct bmc_device {
Corey Minyard16639eb2014-10-10 21:54:03 -0500196 struct platform_device pdev;
Corey Minyard50c812b2006-03-26 01:37:21 -0800197 struct ipmi_device_id id;
198 unsigned char guid[16];
199 int guid_set;
Corey Minyard16639eb2014-10-10 21:54:03 -0500200 char name[16];
201 struct kref usecount;
Corey Minyard50c812b2006-03-26 01:37:21 -0800202
203 /* bmc device attributes */
204 struct device_attribute device_id_attr;
205 struct device_attribute provides_dev_sdrs_attr;
206 struct device_attribute revision_attr;
207 struct device_attribute firmware_rev_attr;
208 struct device_attribute version_attr;
209 struct device_attribute add_dev_support_attr;
210 struct device_attribute manufacturer_id_attr;
211 struct device_attribute product_id_attr;
212 struct device_attribute guid_attr;
213 struct device_attribute aux_firmware_rev_attr;
214};
Corey Minyard16639eb2014-10-10 21:54:03 -0500215#define to_bmc_device(x) container_of((x), struct bmc_device, pdev.dev)
Corey Minyard50c812b2006-03-26 01:37:21 -0800216
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700217/*
218 * Various statistics for IPMI, these index stats[] in the ipmi_smi
219 * structure.
220 */
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700221enum ipmi_stat_indexes {
222 /* Commands we got from the user that were invalid. */
223 IPMI_STAT_sent_invalid_commands = 0,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700224
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700225 /* Commands we sent to the MC. */
226 IPMI_STAT_sent_local_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700227
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700228 /* Responses from the MC that were delivered to a user. */
229 IPMI_STAT_handled_local_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700230
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700231 /* Responses from the MC that were not delivered to a user. */
232 IPMI_STAT_unhandled_local_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700233
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700234 /* Commands we sent out to the IPMB bus. */
235 IPMI_STAT_sent_ipmb_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700236
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700237 /* Commands sent on the IPMB that had errors on the SEND CMD */
238 IPMI_STAT_sent_ipmb_command_errs,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700239
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700240 /* Each retransmit increments this count. */
241 IPMI_STAT_retransmitted_ipmb_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700242
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700243 /*
244 * When a message times out (runs out of retransmits) this is
245 * incremented.
246 */
247 IPMI_STAT_timed_out_ipmb_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700248
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700249 /*
250 * This is like above, but for broadcasts. Broadcasts are
251 * *not* included in the above count (they are expected to
252 * time out).
253 */
254 IPMI_STAT_timed_out_ipmb_broadcasts,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700255
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700256 /* Responses I have sent to the IPMB bus. */
257 IPMI_STAT_sent_ipmb_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700258
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700259 /* The response was delivered to the user. */
260 IPMI_STAT_handled_ipmb_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700261
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700262 /* The response had invalid data in it. */
263 IPMI_STAT_invalid_ipmb_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700264
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700265 /* The response didn't have anyone waiting for it. */
266 IPMI_STAT_unhandled_ipmb_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700267
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700268 /* Commands we sent out to the IPMB bus. */
269 IPMI_STAT_sent_lan_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700270
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700271 /* Commands sent on the IPMB that had errors on the SEND CMD */
272 IPMI_STAT_sent_lan_command_errs,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700273
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700274 /* Each retransmit increments this count. */
275 IPMI_STAT_retransmitted_lan_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700276
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700277 /*
278 * When a message times out (runs out of retransmits) this is
279 * incremented.
280 */
281 IPMI_STAT_timed_out_lan_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700282
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700283 /* Responses I have sent to the IPMB bus. */
284 IPMI_STAT_sent_lan_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700285
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700286 /* The response was delivered to the user. */
287 IPMI_STAT_handled_lan_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700288
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700289 /* The response had invalid data in it. */
290 IPMI_STAT_invalid_lan_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700291
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700292 /* The response didn't have anyone waiting for it. */
293 IPMI_STAT_unhandled_lan_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700294
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700295 /* The command was delivered to the user. */
296 IPMI_STAT_handled_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700297
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700298 /* The command had invalid data in it. */
299 IPMI_STAT_invalid_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700300
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700301 /* The command didn't have anyone waiting for it. */
302 IPMI_STAT_unhandled_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700303
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700304 /* Invalid data in an event. */
305 IPMI_STAT_invalid_events,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700306
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700307 /* Events that were received with the proper format. */
308 IPMI_STAT_events,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700309
Corey Minyard25176ed2009-04-21 12:24:04 -0700310 /* Retransmissions on IPMB that failed. */
311 IPMI_STAT_dropped_rexmit_ipmb_commands,
312
313 /* Retransmissions on LAN that failed. */
314 IPMI_STAT_dropped_rexmit_lan_commands,
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700315
316 /* This *must* remain last, add new values above this. */
317 IPMI_NUM_STATS
318};
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700319
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321#define IPMI_IPMB_NUM_SEQ 64
Corey Minyardc14979b2005-09-06 15:18:38 -0700322#define IPMI_MAX_CHANNELS 16
Corey Minyardc70d7492008-04-29 01:01:09 -0700323struct ipmi_smi {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 /* What interface number are we? */
325 int intf_num;
326
Corey Minyard393d2cc2005-11-07 00:59:54 -0800327 struct kref refcount;
328
Corey Minyard7ea0ed22014-11-06 16:58:48 -0600329 /* Set when the interface is being unregistered. */
330 bool in_shutdown;
331
Corey Minyardbca03242006-12-06 20:40:57 -0800332 /* Used for a list of interfaces. */
333 struct list_head link;
334
Corey Minyardc70d7492008-04-29 01:01:09 -0700335 /*
336 * The list of upper layers that are using me. seq_lock
337 * protects this.
338 */
Corey Minyard393d2cc2005-11-07 00:59:54 -0800339 struct list_head users;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Corey Minyardb2c03942006-12-06 20:41:00 -0800341 /* Information to supply to users. */
342 unsigned char ipmi_version_major;
343 unsigned char ipmi_version_minor;
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 /* Used for wake ups at startup. */
346 wait_queue_head_t waitq;
347
Corey Minyard50c812b2006-03-26 01:37:21 -0800348 struct bmc_device *bmc;
349 char *my_dev_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Corey Minyardc70d7492008-04-29 01:01:09 -0700351 /*
352 * This is the lower-layer's sender routine. Note that you
Corey Minyardb2c03942006-12-06 20:41:00 -0800353 * must either be holding the ipmi_interfaces_mutex or be in
354 * an umpreemptible region to use this. You must fetch the
Corey Minyardc70d7492008-04-29 01:01:09 -0700355 * value into a local variable and make sure it is not NULL.
356 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 struct ipmi_smi_handlers *handlers;
358 void *send_info;
359
Corey Minyard3b625942005-06-23 22:01:42 -0700360#ifdef CONFIG_PROC_FS
Corey Minyardac019152007-10-18 03:07:11 -0700361 /* A list of proc entries for this interface. */
362 struct mutex proc_entry_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 struct ipmi_proc_entry *proc_entries;
Corey Minyard3b625942005-06-23 22:01:42 -0700364#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Corey Minyard50c812b2006-03-26 01:37:21 -0800366 /* Driver-model device for the system interface. */
367 struct device *si_dev;
368
Corey Minyardc70d7492008-04-29 01:01:09 -0700369 /*
370 * A table of sequence numbers for this interface. We use the
371 * sequence numbers for IPMB messages that go out of the
372 * interface to match them up with their responses. A routine
373 * is called periodically to time the items in this list.
374 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 spinlock_t seq_lock;
376 struct seq_table seq_table[IPMI_IPMB_NUM_SEQ];
377 int curr_seq;
378
Corey Minyardc70d7492008-04-29 01:01:09 -0700379 /*
Corey Minyard7adf5792012-03-28 14:42:49 -0700380 * Messages queued for delivery. If delivery fails (out of memory
381 * for instance), They will stay in here to be processed later in a
382 * periodic timer interrupt. The tasklet is for handling received
383 * messages directly from the handler.
Corey Minyardc70d7492008-04-29 01:01:09 -0700384 */
Corey Minyard65be7542014-11-06 20:48:02 -0600385 spinlock_t waiting_rcv_msgs_lock;
386 struct list_head waiting_rcv_msgs;
Corey Minyard7adf5792012-03-28 14:42:49 -0700387 atomic_t watchdog_pretimeouts_to_deliver;
388 struct tasklet_struct recv_tasklet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Corey Minyard7ea0ed22014-11-06 16:58:48 -0600390 spinlock_t xmit_msgs_lock;
391 struct list_head xmit_msgs;
392 struct ipmi_smi_msg *curr_msg;
393 struct list_head hp_xmit_msgs;
394
Corey Minyardc70d7492008-04-29 01:01:09 -0700395 /*
396 * The list of command receivers that are registered for commands
397 * on this interface.
398 */
Corey Minyardd6dfd132006-03-31 02:30:41 -0800399 struct mutex cmd_rcvrs_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 struct list_head cmd_rcvrs;
401
Corey Minyardc70d7492008-04-29 01:01:09 -0700402 /*
403 * Events that were queues because no one was there to receive
404 * them.
405 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 spinlock_t events_lock; /* For dealing with event stuff. */
407 struct list_head waiting_events;
408 unsigned int waiting_events_count; /* How many events in queue? */
Corey Minyard87ebd062008-04-29 01:01:04 -0700409 char delivering_events;
410 char event_msg_printed;
Corey Minyard89986492014-04-14 09:46:54 -0500411 atomic_t event_waiters;
412 unsigned int ticks_to_req_ev;
413 int last_needs_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Corey Minyardc70d7492008-04-29 01:01:09 -0700415 /*
416 * The event receiver for my BMC, only really used at panic
417 * shutdown as a place to store this.
418 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 unsigned char event_receiver;
420 unsigned char event_receiver_lun;
421 unsigned char local_sel_device;
422 unsigned char local_event_generator;
423
Corey Minyardb9675132006-12-06 20:41:02 -0800424 /* For handling of maintenance mode. */
425 int maintenance_mode;
Corey Minyard7aefac22014-04-14 09:46:56 -0500426 bool maintenance_mode_enable;
Corey Minyardb9675132006-12-06 20:41:02 -0800427 int auto_maintenance_timeout;
428 spinlock_t maintenance_mode_lock; /* Used in a timer... */
429
Corey Minyardc70d7492008-04-29 01:01:09 -0700430 /*
431 * A cheap hack, if this is non-null and a message to an
432 * interface comes in with a NULL user, call this routine with
433 * it. Note that the message will still be freed by the
434 * caller. This only works on the system interface.
435 */
Corey Minyard56a55ec2005-09-06 15:18:42 -0700436 void (*null_user_handler)(ipmi_smi_t intf, struct ipmi_recv_msg *msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Corey Minyardc70d7492008-04-29 01:01:09 -0700438 /*
439 * When we are scanning the channels for an SMI, this will
440 * tell which channel we are scanning.
441 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 int curr_channel;
443
444 /* Channel information */
445 struct ipmi_channel channels[IPMI_MAX_CHANNELS];
446
447 /* Proc FS stuff. */
448 struct proc_dir_entry *proc_dir;
449 char proc_dir_name[10];
450
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700451 atomic_t stats[IPMI_NUM_STATS];
Konstantin Baydarov5956dce2008-04-29 01:01:03 -0700452
453 /*
454 * run_to_completion duplicate of smb_info, smi_info
455 * and ipmi_serial_info structures. Used to decrease numbers of
456 * parameters passed by "low" level IPMI code.
457 */
458 int run_to_completion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459};
Corey Minyard50c812b2006-03-26 01:37:21 -0800460#define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Corey Minyard50c812b2006-03-26 01:37:21 -0800462/**
463 * The driver model view of the IPMI messaging driver.
464 */
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -0800465static struct platform_driver ipmidriver = {
466 .driver = {
467 .name = "ipmi",
468 .bus = &platform_bus_type
469 }
Corey Minyard50c812b2006-03-26 01:37:21 -0800470};
471static DEFINE_MUTEX(ipmidriver_mutex);
472
Denis Chengbed97592008-02-06 01:37:35 -0800473static LIST_HEAD(ipmi_interfaces);
Corey Minyardbca03242006-12-06 20:40:57 -0800474static DEFINE_MUTEX(ipmi_interfaces_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Corey Minyardc70d7492008-04-29 01:01:09 -0700476/*
477 * List of watchers that want to know when smi's are added and deleted.
478 */
Denis Chengbed97592008-02-06 01:37:35 -0800479static LIST_HEAD(smi_watchers);
Corey Minyardb2c03942006-12-06 20:41:00 -0800480static DEFINE_MUTEX(smi_watchers_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700482#define ipmi_inc_stat(intf, stat) \
483 atomic_inc(&(intf)->stats[IPMI_STAT_ ## stat])
484#define ipmi_get_stat(intf, stat) \
485 ((unsigned int) atomic_read(&(intf)->stats[IPMI_STAT_ ## stat]))
486
Corey Minyard7e503872014-10-09 07:20:32 -0500487static char *addr_src_to_str[] = { "invalid", "hotmod", "hardcoded", "SPMI",
488 "ACPI", "SMBIOS", "PCI",
489 "device-tree", "default" };
490
491const char *ipmi_addr_src_to_str(enum ipmi_addr_src src)
492{
493 if (src > SI_DEFAULT)
494 src = 0; /* Invalid */
495 return addr_src_to_str[src];
496}
497EXPORT_SYMBOL(ipmi_addr_src_to_str);
498
Corey Minyard25176ed2009-04-21 12:24:04 -0700499static int is_lan_addr(struct ipmi_addr *addr)
500{
501 return addr->addr_type == IPMI_LAN_ADDR_TYPE;
502}
503
504static int is_ipmb_addr(struct ipmi_addr *addr)
505{
506 return addr->addr_type == IPMI_IPMB_ADDR_TYPE;
507}
508
509static int is_ipmb_bcast_addr(struct ipmi_addr *addr)
510{
511 return addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE;
512}
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700513
Corey Minyard393d2cc2005-11-07 00:59:54 -0800514static void free_recv_msg_list(struct list_head *q)
515{
516 struct ipmi_recv_msg *msg, *msg2;
517
518 list_for_each_entry_safe(msg, msg2, q, link) {
519 list_del(&msg->link);
520 ipmi_free_recv_msg(msg);
521 }
522}
523
Corey Minyardf3ce6a02006-11-08 17:44:52 -0800524static void free_smi_msg_list(struct list_head *q)
525{
526 struct ipmi_smi_msg *msg, *msg2;
527
528 list_for_each_entry_safe(msg, msg2, q, link) {
529 list_del(&msg->link);
530 ipmi_free_smi_msg(msg);
531 }
532}
533
Corey Minyard393d2cc2005-11-07 00:59:54 -0800534static void clean_up_interface_data(ipmi_smi_t intf)
535{
536 int i;
537 struct cmd_rcvr *rcvr, *rcvr2;
Corey Minyard393d2cc2005-11-07 00:59:54 -0800538 struct list_head list;
539
Corey Minyard7adf5792012-03-28 14:42:49 -0700540 tasklet_kill(&intf->recv_tasklet);
541
Corey Minyard65be7542014-11-06 20:48:02 -0600542 free_smi_msg_list(&intf->waiting_rcv_msgs);
Corey Minyard393d2cc2005-11-07 00:59:54 -0800543 free_recv_msg_list(&intf->waiting_events);
544
Corey Minyard78ba2fa2007-02-10 01:45:45 -0800545 /*
546 * Wholesale remove all the entries from the list in the
547 * interface and wait for RCU to know that none are in use.
548 */
Corey Minyardd6dfd132006-03-31 02:30:41 -0800549 mutex_lock(&intf->cmd_rcvrs_mutex);
Corey Minyard78ba2fa2007-02-10 01:45:45 -0800550 INIT_LIST_HEAD(&list);
551 list_splice_init_rcu(&intf->cmd_rcvrs, &list, synchronize_rcu);
Corey Minyardd6dfd132006-03-31 02:30:41 -0800552 mutex_unlock(&intf->cmd_rcvrs_mutex);
Corey Minyard393d2cc2005-11-07 00:59:54 -0800553
554 list_for_each_entry_safe(rcvr, rcvr2, &list, link)
555 kfree(rcvr);
556
557 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
558 if ((intf->seq_table[i].inuse)
Corey Minyardc70d7492008-04-29 01:01:09 -0700559 && (intf->seq_table[i].recv_msg))
Corey Minyard393d2cc2005-11-07 00:59:54 -0800560 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
Corey Minyard393d2cc2005-11-07 00:59:54 -0800562}
563
564static void intf_free(struct kref *ref)
565{
566 ipmi_smi_t intf = container_of(ref, struct ipmi_smi, refcount);
567
568 clean_up_interface_data(intf);
569 kfree(intf);
570}
571
Corey Minyardbca03242006-12-06 20:40:57 -0800572struct watcher_entry {
Corey Minyardb2c03942006-12-06 20:41:00 -0800573 int intf_num;
574 ipmi_smi_t intf;
Corey Minyardbca03242006-12-06 20:40:57 -0800575 struct list_head link;
Corey Minyardbca03242006-12-06 20:40:57 -0800576};
577
Corey Minyard393d2cc2005-11-07 00:59:54 -0800578int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher)
579{
Corey Minyardbca03242006-12-06 20:40:57 -0800580 ipmi_smi_t intf;
Denis Chenge381d1c2008-02-06 01:37:39 -0800581 LIST_HEAD(to_deliver);
Corey Minyardbca03242006-12-06 20:40:57 -0800582 struct watcher_entry *e, *e2;
583
Corey Minyardb2c03942006-12-06 20:41:00 -0800584 mutex_lock(&smi_watchers_mutex);
585
Corey Minyardbca03242006-12-06 20:40:57 -0800586 mutex_lock(&ipmi_interfaces_mutex);
587
Corey Minyardb2c03942006-12-06 20:41:00 -0800588 /* Build a list of things to deliver. */
Corey Minyard78ba2fa2007-02-10 01:45:45 -0800589 list_for_each_entry(intf, &ipmi_interfaces, link) {
Corey Minyardbca03242006-12-06 20:40:57 -0800590 if (intf->intf_num == -1)
591 continue;
592 e = kmalloc(sizeof(*e), GFP_KERNEL);
593 if (!e)
594 goto out_err;
Corey Minyardb2c03942006-12-06 20:41:00 -0800595 kref_get(&intf->refcount);
596 e->intf = intf;
Corey Minyardbca03242006-12-06 20:40:57 -0800597 e->intf_num = intf->intf_num;
598 list_add_tail(&e->link, &to_deliver);
599 }
Corey Minyard393d2cc2005-11-07 00:59:54 -0800600
Corey Minyardb2c03942006-12-06 20:41:00 -0800601 /* We will succeed, so add it to the list. */
602 list_add(&watcher->link, &smi_watchers);
Corey Minyardbca03242006-12-06 20:40:57 -0800603
604 mutex_unlock(&ipmi_interfaces_mutex);
605
606 list_for_each_entry_safe(e, e2, &to_deliver, link) {
607 list_del(&e->link);
Corey Minyardb2c03942006-12-06 20:41:00 -0800608 watcher->new_smi(e->intf_num, e->intf->si_dev);
609 kref_put(&e->intf->refcount, intf_free);
Corey Minyardbca03242006-12-06 20:40:57 -0800610 kfree(e);
Corey Minyard393d2cc2005-11-07 00:59:54 -0800611 }
Corey Minyardbca03242006-12-06 20:40:57 -0800612
Corey Minyardb2c03942006-12-06 20:41:00 -0800613 mutex_unlock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -0800614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return 0;
Corey Minyardbca03242006-12-06 20:40:57 -0800616
617 out_err:
Corey Minyardb2c03942006-12-06 20:41:00 -0800618 mutex_unlock(&ipmi_interfaces_mutex);
619 mutex_unlock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -0800620 list_for_each_entry_safe(e, e2, &to_deliver, link) {
621 list_del(&e->link);
Corey Minyardb2c03942006-12-06 20:41:00 -0800622 kref_put(&e->intf->refcount, intf_free);
Corey Minyardbca03242006-12-06 20:40:57 -0800623 kfree(e);
624 }
625 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
Corey Minyardc70d7492008-04-29 01:01:09 -0700627EXPORT_SYMBOL(ipmi_smi_watcher_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher)
630{
Corey Minyardb2c03942006-12-06 20:41:00 -0800631 mutex_lock(&smi_watchers_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 list_del(&(watcher->link));
Corey Minyardb2c03942006-12-06 20:41:00 -0800633 mutex_unlock(&smi_watchers_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return 0;
635}
Corey Minyardc70d7492008-04-29 01:01:09 -0700636EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Corey Minyardb2c03942006-12-06 20:41:00 -0800638/*
639 * Must be called with smi_watchers_mutex held.
640 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641static void
Corey Minyard50c812b2006-03-26 01:37:21 -0800642call_smi_watchers(int i, struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 struct ipmi_smi_watcher *w;
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 list_for_each_entry(w, &smi_watchers, link) {
647 if (try_module_get(w->owner)) {
Corey Minyard50c812b2006-03-26 01:37:21 -0800648 w->new_smi(i, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 module_put(w->owner);
650 }
651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
654static int
655ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2)
656{
657 if (addr1->addr_type != addr2->addr_type)
658 return 0;
659
660 if (addr1->channel != addr2->channel)
661 return 0;
662
663 if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
664 struct ipmi_system_interface_addr *smi_addr1
665 = (struct ipmi_system_interface_addr *) addr1;
666 struct ipmi_system_interface_addr *smi_addr2
667 = (struct ipmi_system_interface_addr *) addr2;
668 return (smi_addr1->lun == smi_addr2->lun);
669 }
670
Corey Minyard25176ed2009-04-21 12:24:04 -0700671 if (is_ipmb_addr(addr1) || is_ipmb_bcast_addr(addr1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 struct ipmi_ipmb_addr *ipmb_addr1
673 = (struct ipmi_ipmb_addr *) addr1;
674 struct ipmi_ipmb_addr *ipmb_addr2
675 = (struct ipmi_ipmb_addr *) addr2;
676
677 return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr)
678 && (ipmb_addr1->lun == ipmb_addr2->lun));
679 }
680
Corey Minyard25176ed2009-04-21 12:24:04 -0700681 if (is_lan_addr(addr1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 struct ipmi_lan_addr *lan_addr1
683 = (struct ipmi_lan_addr *) addr1;
684 struct ipmi_lan_addr *lan_addr2
685 = (struct ipmi_lan_addr *) addr2;
686
687 return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID)
688 && (lan_addr1->local_SWID == lan_addr2->local_SWID)
689 && (lan_addr1->session_handle
690 == lan_addr2->session_handle)
691 && (lan_addr1->lun == lan_addr2->lun));
692 }
693
694 return 1;
695}
696
697int ipmi_validate_addr(struct ipmi_addr *addr, int len)
698{
Corey Minyardc70d7492008-04-29 01:01:09 -0700699 if (len < sizeof(struct ipmi_system_interface_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
703 if (addr->channel != IPMI_BMC_CHANNEL)
704 return -EINVAL;
705 return 0;
706 }
707
708 if ((addr->channel == IPMI_BMC_CHANNEL)
Jayachandran C12fc1d72006-02-03 03:04:51 -0800709 || (addr->channel >= IPMI_MAX_CHANNELS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 || (addr->channel < 0))
711 return -EINVAL;
712
Corey Minyard25176ed2009-04-21 12:24:04 -0700713 if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
Corey Minyardc70d7492008-04-29 01:01:09 -0700714 if (len < sizeof(struct ipmi_ipmb_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return 0;
717 }
718
Corey Minyard25176ed2009-04-21 12:24:04 -0700719 if (is_lan_addr(addr)) {
Corey Minyardc70d7492008-04-29 01:01:09 -0700720 if (len < sizeof(struct ipmi_lan_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return 0;
723 }
724
725 return -EINVAL;
726}
Corey Minyardc70d7492008-04-29 01:01:09 -0700727EXPORT_SYMBOL(ipmi_validate_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729unsigned int ipmi_addr_length(int addr_type)
730{
731 if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
732 return sizeof(struct ipmi_system_interface_addr);
733
734 if ((addr_type == IPMI_IPMB_ADDR_TYPE)
Corey Minyardc70d7492008-04-29 01:01:09 -0700735 || (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return sizeof(struct ipmi_ipmb_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 if (addr_type == IPMI_LAN_ADDR_TYPE)
739 return sizeof(struct ipmi_lan_addr);
740
741 return 0;
742}
Corey Minyardc70d7492008-04-29 01:01:09 -0700743EXPORT_SYMBOL(ipmi_addr_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745static void deliver_response(struct ipmi_recv_msg *msg)
746{
Corey Minyard8a3628d2006-03-31 02:30:40 -0800747 if (!msg->user) {
Corey Minyard56a55ec2005-09-06 15:18:42 -0700748 ipmi_smi_t intf = msg->user_msg_data;
Corey Minyard56a55ec2005-09-06 15:18:42 -0700749
750 /* Special handling for NULL users. */
751 if (intf->null_user_handler) {
752 intf->null_user_handler(intf, msg);
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700753 ipmi_inc_stat(intf, handled_local_responses);
Corey Minyard56a55ec2005-09-06 15:18:42 -0700754 } else {
755 /* No handler, so give up. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700756 ipmi_inc_stat(intf, unhandled_local_responses);
Corey Minyard56a55ec2005-09-06 15:18:42 -0700757 }
758 ipmi_free_recv_msg(msg);
759 } else {
Corey Minyard393d2cc2005-11-07 00:59:54 -0800760 ipmi_user_t user = msg->user;
761 user->handler->ipmi_recv_hndl(msg, user->handler_data);
Corey Minyard56a55ec2005-09-06 15:18:42 -0700762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
Corey Minyardb2c03942006-12-06 20:41:00 -0800765static void
766deliver_err_response(struct ipmi_recv_msg *msg, int err)
767{
768 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
769 msg->msg_data[0] = err;
770 msg->msg.netfn |= 1; /* Convert to a response. */
771 msg->msg.data_len = 1;
772 msg->msg.data = msg->msg_data;
773 deliver_response(msg);
774}
775
Corey Minyardc70d7492008-04-29 01:01:09 -0700776/*
777 * Find the next sequence number not being used and add the given
778 * message with the given timeout to the sequence table. This must be
779 * called with the interface's seq_lock held.
780 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781static int intf_next_seq(ipmi_smi_t intf,
782 struct ipmi_recv_msg *recv_msg,
783 unsigned long timeout,
784 int retries,
785 int broadcast,
786 unsigned char *seq,
787 long *seqid)
788{
789 int rv = 0;
790 unsigned int i;
791
Corey Minyardc70d7492008-04-29 01:01:09 -0700792 for (i = intf->curr_seq; (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq;
793 i = (i+1)%IPMI_IPMB_NUM_SEQ) {
Corey Minyard8a3628d2006-03-31 02:30:40 -0800794 if (!intf->seq_table[i].inuse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 break;
796 }
797
Corey Minyard8a3628d2006-03-31 02:30:40 -0800798 if (!intf->seq_table[i].inuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 intf->seq_table[i].recv_msg = recv_msg;
800
Corey Minyardc70d7492008-04-29 01:01:09 -0700801 /*
802 * Start with the maximum timeout, when the send response
803 * comes in we will start the real timer.
804 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 intf->seq_table[i].timeout = MAX_MSG_TIMEOUT;
806 intf->seq_table[i].orig_timeout = timeout;
807 intf->seq_table[i].retries_left = retries;
808 intf->seq_table[i].broadcast = broadcast;
809 intf->seq_table[i].inuse = 1;
810 intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid);
811 *seq = i;
812 *seqid = intf->seq_table[i].seqid;
813 intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ;
Corey Minyard89986492014-04-14 09:46:54 -0500814 need_waiter(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 } else {
816 rv = -EAGAIN;
817 }
Corey Minyardc70d7492008-04-29 01:01:09 -0700818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return rv;
820}
821
Corey Minyardc70d7492008-04-29 01:01:09 -0700822/*
823 * Return the receive message for the given sequence number and
824 * release the sequence number so it can be reused. Some other data
825 * is passed in to be sure the message matches up correctly (to help
826 * guard against message coming in after their timeout and the
827 * sequence number being reused).
828 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829static int intf_find_seq(ipmi_smi_t intf,
830 unsigned char seq,
831 short channel,
832 unsigned char cmd,
833 unsigned char netfn,
834 struct ipmi_addr *addr,
835 struct ipmi_recv_msg **recv_msg)
836{
837 int rv = -ENODEV;
838 unsigned long flags;
839
840 if (seq >= IPMI_IPMB_NUM_SEQ)
841 return -EINVAL;
842
843 spin_lock_irqsave(&(intf->seq_lock), flags);
844 if (intf->seq_table[seq].inuse) {
845 struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg;
846
Corey Minyardc70d7492008-04-29 01:01:09 -0700847 if ((msg->addr.channel == channel) && (msg->msg.cmd == cmd)
848 && (msg->msg.netfn == netfn)
849 && (ipmi_addr_equal(addr, &(msg->addr)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 *recv_msg = msg;
851 intf->seq_table[seq].inuse = 0;
852 rv = 0;
853 }
854 }
855 spin_unlock_irqrestore(&(intf->seq_lock), flags);
856
857 return rv;
858}
859
860
861/* Start the timer for a specific sequence table entry. */
862static int intf_start_seq_timer(ipmi_smi_t intf,
863 long msgid)
864{
865 int rv = -ENODEV;
866 unsigned long flags;
867 unsigned char seq;
868 unsigned long seqid;
869
870
871 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
872
873 spin_lock_irqsave(&(intf->seq_lock), flags);
Corey Minyardc70d7492008-04-29 01:01:09 -0700874 /*
875 * We do this verification because the user can be deleted
876 * while a message is outstanding.
877 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if ((intf->seq_table[seq].inuse)
Corey Minyardc70d7492008-04-29 01:01:09 -0700879 && (intf->seq_table[seq].seqid == seqid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 struct seq_table *ent = &(intf->seq_table[seq]);
881 ent->timeout = ent->orig_timeout;
882 rv = 0;
883 }
884 spin_unlock_irqrestore(&(intf->seq_lock), flags);
885
886 return rv;
887}
888
889/* Got an error for the send message for a specific sequence number. */
890static int intf_err_seq(ipmi_smi_t intf,
891 long msgid,
892 unsigned int err)
893{
894 int rv = -ENODEV;
895 unsigned long flags;
896 unsigned char seq;
897 unsigned long seqid;
898 struct ipmi_recv_msg *msg = NULL;
899
900
901 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
902
903 spin_lock_irqsave(&(intf->seq_lock), flags);
Corey Minyardc70d7492008-04-29 01:01:09 -0700904 /*
905 * We do this verification because the user can be deleted
906 * while a message is outstanding.
907 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if ((intf->seq_table[seq].inuse)
Corey Minyardc70d7492008-04-29 01:01:09 -0700909 && (intf->seq_table[seq].seqid == seqid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 struct seq_table *ent = &(intf->seq_table[seq]);
911
912 ent->inuse = 0;
913 msg = ent->recv_msg;
914 rv = 0;
915 }
916 spin_unlock_irqrestore(&(intf->seq_lock), flags);
917
Corey Minyardb2c03942006-12-06 20:41:00 -0800918 if (msg)
919 deliver_err_response(msg, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 return rv;
922}
923
924
925int ipmi_create_user(unsigned int if_num,
926 struct ipmi_user_hndl *handler,
927 void *handler_data,
928 ipmi_user_t *user)
929{
930 unsigned long flags;
931 ipmi_user_t new_user;
932 int rv = 0;
933 ipmi_smi_t intf;
934
Corey Minyardc70d7492008-04-29 01:01:09 -0700935 /*
936 * There is no module usecount here, because it's not
937 * required. Since this can only be used by and called from
938 * other modules, they will implicitly use this module, and
939 * thus this can't be removed unless the other modules are
940 * removed.
941 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 if (handler == NULL)
944 return -EINVAL;
945
Corey Minyardc70d7492008-04-29 01:01:09 -0700946 /*
947 * Make sure the driver is actually initialized, this handles
948 * problems with initialization order.
949 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (!initialized) {
951 rv = ipmi_init_msghandler();
952 if (rv)
953 return rv;
954
Corey Minyardc70d7492008-04-29 01:01:09 -0700955 /*
956 * The init code doesn't return an error if it was turned
957 * off, but it won't initialize. Check that.
958 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (!initialized)
960 return -ENODEV;
961 }
962
963 new_user = kmalloc(sizeof(*new_user), GFP_KERNEL);
Corey Minyard8a3628d2006-03-31 02:30:40 -0800964 if (!new_user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return -ENOMEM;
966
Corey Minyardb2c03942006-12-06 20:41:00 -0800967 mutex_lock(&ipmi_interfaces_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -0800968 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
969 if (intf->intf_num == if_num)
970 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
Corey Minyardb2c03942006-12-06 20:41:00 -0800972 /* Not found, return an error */
Corey Minyardbca03242006-12-06 20:40:57 -0800973 rv = -EINVAL;
974 goto out_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Corey Minyardbca03242006-12-06 20:40:57 -0800976 found:
Corey Minyard393d2cc2005-11-07 00:59:54 -0800977 /* Note that each existing user holds a refcount to the interface. */
978 kref_get(&intf->refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Corey Minyard393d2cc2005-11-07 00:59:54 -0800980 kref_init(&new_user->refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 new_user->handler = handler;
982 new_user->handler_data = handler_data;
983 new_user->intf = intf;
Corey Minyard89986492014-04-14 09:46:54 -0500984 new_user->gets_events = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 if (!try_module_get(intf->handlers->owner)) {
987 rv = -ENODEV;
Adrian Bunk5c98d292006-03-25 03:07:52 -0800988 goto out_kref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
990
991 if (intf->handlers->inc_usecount) {
992 rv = intf->handlers->inc_usecount(intf->send_info);
993 if (rv) {
994 module_put(intf->handlers->owner);
Adrian Bunk5c98d292006-03-25 03:07:52 -0800995 goto out_kref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997 }
998
Corey Minyardc70d7492008-04-29 01:01:09 -0700999 /*
1000 * Hold the lock so intf->handlers is guaranteed to be good
1001 * until now
1002 */
Corey Minyardb2c03942006-12-06 20:41:00 -08001003 mutex_unlock(&ipmi_interfaces_mutex);
1004
Corey Minyard7aefac22014-04-14 09:46:56 -05001005 new_user->valid = true;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001006 spin_lock_irqsave(&intf->seq_lock, flags);
1007 list_add_rcu(&new_user->link, &intf->users);
1008 spin_unlock_irqrestore(&intf->seq_lock, flags);
Corey Minyard89986492014-04-14 09:46:54 -05001009 if (handler->ipmi_watchdog_pretimeout) {
1010 /* User wants pretimeouts, so make sure to watch for them. */
1011 if (atomic_inc_return(&intf->event_waiters) == 1)
1012 need_waiter(intf);
1013 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08001014 *user = new_user;
1015 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Adrian Bunk5c98d292006-03-25 03:07:52 -08001017out_kref:
Corey Minyard393d2cc2005-11-07 00:59:54 -08001018 kref_put(&intf->refcount, intf_free);
Adrian Bunk5c98d292006-03-25 03:07:52 -08001019out_kfree:
Corey Minyardb2c03942006-12-06 20:41:00 -08001020 mutex_unlock(&ipmi_interfaces_mutex);
Adrian Bunk5c98d292006-03-25 03:07:52 -08001021 kfree(new_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return rv;
1023}
Corey Minyardc70d7492008-04-29 01:01:09 -07001024EXPORT_SYMBOL(ipmi_create_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Zhao Yakui16f42322010-12-08 10:10:16 +08001026int ipmi_get_smi_info(int if_num, struct ipmi_smi_info *data)
1027{
1028 int rv = 0;
1029 ipmi_smi_t intf;
1030 struct ipmi_smi_handlers *handlers;
1031
1032 mutex_lock(&ipmi_interfaces_mutex);
1033 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
1034 if (intf->intf_num == if_num)
1035 goto found;
1036 }
1037 /* Not found, return an error */
1038 rv = -EINVAL;
1039 mutex_unlock(&ipmi_interfaces_mutex);
1040 return rv;
1041
1042found:
1043 handlers = intf->handlers;
1044 rv = -ENOSYS;
1045 if (handlers->get_smi_info)
1046 rv = handlers->get_smi_info(intf->send_info, data);
1047 mutex_unlock(&ipmi_interfaces_mutex);
1048
1049 return rv;
1050}
1051EXPORT_SYMBOL(ipmi_get_smi_info);
1052
Corey Minyard393d2cc2005-11-07 00:59:54 -08001053static void free_user(struct kref *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Corey Minyard393d2cc2005-11-07 00:59:54 -08001055 ipmi_user_t user = container_of(ref, struct ipmi_user, refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 kfree(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058
1059int ipmi_destroy_user(ipmi_user_t user)
1060{
Corey Minyard393d2cc2005-11-07 00:59:54 -08001061 ipmi_smi_t intf = user->intf;
1062 int i;
1063 unsigned long flags;
1064 struct cmd_rcvr *rcvr;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001065 struct cmd_rcvr *rcvrs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Corey Minyard7aefac22014-04-14 09:46:56 -05001067 user->valid = false;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001068
Corey Minyard89986492014-04-14 09:46:54 -05001069 if (user->handler->ipmi_watchdog_pretimeout)
1070 atomic_dec(&intf->event_waiters);
1071
1072 if (user->gets_events)
1073 atomic_dec(&intf->event_waiters);
1074
Corey Minyard393d2cc2005-11-07 00:59:54 -08001075 /* Remove the user from the interface's sequence table. */
1076 spin_lock_irqsave(&intf->seq_lock, flags);
1077 list_del_rcu(&user->link);
1078
1079 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
1080 if (intf->seq_table[i].inuse
Corey Minyardc70d7492008-04-29 01:01:09 -07001081 && (intf->seq_table[i].recv_msg->user == user)) {
Corey Minyard393d2cc2005-11-07 00:59:54 -08001082 intf->seq_table[i].inuse = 0;
Corey Minyardb2c03942006-12-06 20:41:00 -08001083 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
Corey Minyard393d2cc2005-11-07 00:59:54 -08001084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08001086 spin_unlock_irqrestore(&intf->seq_lock, flags);
1087
1088 /*
1089 * Remove the user from the command receiver's table. First
1090 * we build a list of everything (not using the standard link,
1091 * since other things may be using it till we do
1092 * synchronize_rcu()) then free everything in that list.
1093 */
Corey Minyardd6dfd132006-03-31 02:30:41 -08001094 mutex_lock(&intf->cmd_rcvrs_mutex);
Paul E. McKenney066bb8d2006-01-06 00:19:53 -08001095 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
Corey Minyard393d2cc2005-11-07 00:59:54 -08001096 if (rcvr->user == user) {
1097 list_del_rcu(&rcvr->link);
1098 rcvr->next = rcvrs;
1099 rcvrs = rcvr;
1100 }
1101 }
Corey Minyardd6dfd132006-03-31 02:30:41 -08001102 mutex_unlock(&intf->cmd_rcvrs_mutex);
Corey Minyard393d2cc2005-11-07 00:59:54 -08001103 synchronize_rcu();
1104 while (rcvrs) {
1105 rcvr = rcvrs;
1106 rcvrs = rcvr->next;
1107 kfree(rcvr);
1108 }
1109
Corey Minyardb2c03942006-12-06 20:41:00 -08001110 mutex_lock(&ipmi_interfaces_mutex);
1111 if (intf->handlers) {
1112 module_put(intf->handlers->owner);
1113 if (intf->handlers->dec_usecount)
1114 intf->handlers->dec_usecount(intf->send_info);
1115 }
1116 mutex_unlock(&ipmi_interfaces_mutex);
Corey Minyard393d2cc2005-11-07 00:59:54 -08001117
1118 kref_put(&intf->refcount, intf_free);
1119
1120 kref_put(&user->refcount, free_user);
1121
Corey Minyard8a3628d2006-03-31 02:30:40 -08001122 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123}
Corey Minyardc70d7492008-04-29 01:01:09 -07001124EXPORT_SYMBOL(ipmi_destroy_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126void ipmi_get_version(ipmi_user_t user,
1127 unsigned char *major,
1128 unsigned char *minor)
1129{
Corey Minyardb2c03942006-12-06 20:41:00 -08001130 *major = user->intf->ipmi_version_major;
1131 *minor = user->intf->ipmi_version_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
Corey Minyardc70d7492008-04-29 01:01:09 -07001133EXPORT_SYMBOL(ipmi_get_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Corey Minyardc14979b2005-09-06 15:18:38 -07001135int ipmi_set_my_address(ipmi_user_t user,
1136 unsigned int channel,
1137 unsigned char address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Corey Minyardc14979b2005-09-06 15:18:38 -07001139 if (channel >= IPMI_MAX_CHANNELS)
1140 return -EINVAL;
1141 user->intf->channels[channel].address = address;
1142 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
Corey Minyardc70d7492008-04-29 01:01:09 -07001144EXPORT_SYMBOL(ipmi_set_my_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Corey Minyardc14979b2005-09-06 15:18:38 -07001146int ipmi_get_my_address(ipmi_user_t user,
1147 unsigned int channel,
1148 unsigned char *address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Corey Minyardc14979b2005-09-06 15:18:38 -07001150 if (channel >= IPMI_MAX_CHANNELS)
1151 return -EINVAL;
1152 *address = user->intf->channels[channel].address;
1153 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
Corey Minyardc70d7492008-04-29 01:01:09 -07001155EXPORT_SYMBOL(ipmi_get_my_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Corey Minyardc14979b2005-09-06 15:18:38 -07001157int ipmi_set_my_LUN(ipmi_user_t user,
1158 unsigned int channel,
1159 unsigned char LUN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
Corey Minyardc14979b2005-09-06 15:18:38 -07001161 if (channel >= IPMI_MAX_CHANNELS)
1162 return -EINVAL;
1163 user->intf->channels[channel].lun = LUN & 0x3;
1164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
Corey Minyardc70d7492008-04-29 01:01:09 -07001166EXPORT_SYMBOL(ipmi_set_my_LUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Corey Minyardc14979b2005-09-06 15:18:38 -07001168int ipmi_get_my_LUN(ipmi_user_t user,
1169 unsigned int channel,
1170 unsigned char *address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
Corey Minyardc14979b2005-09-06 15:18:38 -07001172 if (channel >= IPMI_MAX_CHANNELS)
1173 return -EINVAL;
1174 *address = user->intf->channels[channel].lun;
1175 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
Corey Minyardc70d7492008-04-29 01:01:09 -07001177EXPORT_SYMBOL(ipmi_get_my_LUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Corey Minyardb9675132006-12-06 20:41:02 -08001179int ipmi_get_maintenance_mode(ipmi_user_t user)
1180{
1181 int mode;
1182 unsigned long flags;
1183
1184 spin_lock_irqsave(&user->intf->maintenance_mode_lock, flags);
1185 mode = user->intf->maintenance_mode;
1186 spin_unlock_irqrestore(&user->intf->maintenance_mode_lock, flags);
1187
1188 return mode;
1189}
1190EXPORT_SYMBOL(ipmi_get_maintenance_mode);
1191
1192static void maintenance_mode_update(ipmi_smi_t intf)
1193{
1194 if (intf->handlers->set_maintenance_mode)
1195 intf->handlers->set_maintenance_mode(
1196 intf->send_info, intf->maintenance_mode_enable);
1197}
1198
1199int ipmi_set_maintenance_mode(ipmi_user_t user, int mode)
1200{
1201 int rv = 0;
1202 unsigned long flags;
1203 ipmi_smi_t intf = user->intf;
1204
1205 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1206 if (intf->maintenance_mode != mode) {
1207 switch (mode) {
1208 case IPMI_MAINTENANCE_MODE_AUTO:
Corey Minyardb9675132006-12-06 20:41:02 -08001209 intf->maintenance_mode_enable
1210 = (intf->auto_maintenance_timeout > 0);
1211 break;
1212
1213 case IPMI_MAINTENANCE_MODE_OFF:
Corey Minyard7aefac22014-04-14 09:46:56 -05001214 intf->maintenance_mode_enable = false;
Corey Minyardb9675132006-12-06 20:41:02 -08001215 break;
1216
1217 case IPMI_MAINTENANCE_MODE_ON:
Corey Minyard7aefac22014-04-14 09:46:56 -05001218 intf->maintenance_mode_enable = true;
Corey Minyardb9675132006-12-06 20:41:02 -08001219 break;
1220
1221 default:
1222 rv = -EINVAL;
1223 goto out_unlock;
1224 }
Corey Minyard7aefac22014-04-14 09:46:56 -05001225 intf->maintenance_mode = mode;
Corey Minyardb9675132006-12-06 20:41:02 -08001226
1227 maintenance_mode_update(intf);
1228 }
1229 out_unlock:
1230 spin_unlock_irqrestore(&intf->maintenance_mode_lock, flags);
1231
1232 return rv;
1233}
1234EXPORT_SYMBOL(ipmi_set_maintenance_mode);
1235
Corey Minyard89986492014-04-14 09:46:54 -05001236int ipmi_set_gets_events(ipmi_user_t user, bool val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Corey Minyard393d2cc2005-11-07 00:59:54 -08001238 unsigned long flags;
1239 ipmi_smi_t intf = user->intf;
1240 struct ipmi_recv_msg *msg, *msg2;
1241 struct list_head msgs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Corey Minyard393d2cc2005-11-07 00:59:54 -08001243 INIT_LIST_HEAD(&msgs);
1244
1245 spin_lock_irqsave(&intf->events_lock, flags);
Corey Minyard89986492014-04-14 09:46:54 -05001246 if (user->gets_events == val)
1247 goto out;
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 user->gets_events = val;
1250
Corey Minyard89986492014-04-14 09:46:54 -05001251 if (val) {
1252 if (atomic_inc_return(&intf->event_waiters) == 1)
1253 need_waiter(intf);
1254 } else {
1255 atomic_dec(&intf->event_waiters);
1256 }
1257
Corey Minyardb2c03942006-12-06 20:41:00 -08001258 if (intf->delivering_events)
1259 /*
1260 * Another thread is delivering events for this, so
1261 * let it handle any new events.
1262 */
1263 goto out;
1264
1265 /* Deliver any queued events. */
1266 while (user->gets_events && !list_empty(&intf->waiting_events)) {
Akinobu Mita179e0912006-06-26 00:24:41 -07001267 list_for_each_entry_safe(msg, msg2, &intf->waiting_events, link)
1268 list_move_tail(&msg->link, &msgs);
Corey Minyard4791c032006-04-10 22:54:31 -07001269 intf->waiting_events_count = 0;
Corey Minyard87ebd062008-04-29 01:01:04 -07001270 if (intf->event_msg_printed) {
1271 printk(KERN_WARNING PFX "Event queue no longer"
1272 " full\n");
1273 intf->event_msg_printed = 0;
1274 }
Corey Minyardb2c03942006-12-06 20:41:00 -08001275
1276 intf->delivering_events = 1;
1277 spin_unlock_irqrestore(&intf->events_lock, flags);
1278
1279 list_for_each_entry_safe(msg, msg2, &msgs, link) {
1280 msg->user = user;
1281 kref_get(&user->refcount);
1282 deliver_response(msg);
1283 }
1284
1285 spin_lock_irqsave(&intf->events_lock, flags);
1286 intf->delivering_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08001288
Corey Minyardb2c03942006-12-06 20:41:00 -08001289 out:
Corey Minyard393d2cc2005-11-07 00:59:54 -08001290 spin_unlock_irqrestore(&intf->events_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 return 0;
1293}
Corey Minyardc70d7492008-04-29 01:01:09 -07001294EXPORT_SYMBOL(ipmi_set_gets_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Corey Minyard393d2cc2005-11-07 00:59:54 -08001296static struct cmd_rcvr *find_cmd_rcvr(ipmi_smi_t intf,
1297 unsigned char netfn,
Corey Minyardc69c3122006-09-30 23:27:56 -07001298 unsigned char cmd,
1299 unsigned char chan)
Corey Minyard393d2cc2005-11-07 00:59:54 -08001300{
1301 struct cmd_rcvr *rcvr;
1302
1303 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
Corey Minyardc69c3122006-09-30 23:27:56 -07001304 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1305 && (rcvr->chans & (1 << chan)))
Corey Minyard393d2cc2005-11-07 00:59:54 -08001306 return rcvr;
1307 }
1308 return NULL;
1309}
1310
Corey Minyardc69c3122006-09-30 23:27:56 -07001311static int is_cmd_rcvr_exclusive(ipmi_smi_t intf,
1312 unsigned char netfn,
1313 unsigned char cmd,
1314 unsigned int chans)
1315{
1316 struct cmd_rcvr *rcvr;
1317
1318 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1319 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1320 && (rcvr->chans & chans))
1321 return 0;
1322 }
1323 return 1;
1324}
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326int ipmi_register_for_cmd(ipmi_user_t user,
1327 unsigned char netfn,
Corey Minyardc69c3122006-09-30 23:27:56 -07001328 unsigned char cmd,
1329 unsigned int chans)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
Corey Minyard393d2cc2005-11-07 00:59:54 -08001331 ipmi_smi_t intf = user->intf;
1332 struct cmd_rcvr *rcvr;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001333 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335
1336 rcvr = kmalloc(sizeof(*rcvr), GFP_KERNEL);
Corey Minyard8a3628d2006-03-31 02:30:40 -08001337 if (!rcvr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return -ENOMEM;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001339 rcvr->cmd = cmd;
1340 rcvr->netfn = netfn;
Corey Minyardc69c3122006-09-30 23:27:56 -07001341 rcvr->chans = chans;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001342 rcvr->user = user;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Corey Minyardd6dfd132006-03-31 02:30:41 -08001344 mutex_lock(&intf->cmd_rcvrs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 /* Make sure the command/netfn is not already registered. */
Corey Minyardc69c3122006-09-30 23:27:56 -07001346 if (!is_cmd_rcvr_exclusive(intf, netfn, cmd, chans)) {
Corey Minyard393d2cc2005-11-07 00:59:54 -08001347 rv = -EBUSY;
1348 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350
Corey Minyard89986492014-04-14 09:46:54 -05001351 if (atomic_inc_return(&intf->event_waiters) == 1)
1352 need_waiter(intf);
1353
Corey Minyard393d2cc2005-11-07 00:59:54 -08001354 list_add_rcu(&rcvr->link, &intf->cmd_rcvrs);
Corey Minyard877197e2005-09-06 15:18:45 -07001355
Corey Minyard393d2cc2005-11-07 00:59:54 -08001356 out_unlock:
Corey Minyardd6dfd132006-03-31 02:30:41 -08001357 mutex_unlock(&intf->cmd_rcvrs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if (rv)
1359 kfree(rcvr);
1360
1361 return rv;
1362}
Corey Minyardc70d7492008-04-29 01:01:09 -07001363EXPORT_SYMBOL(ipmi_register_for_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365int ipmi_unregister_for_cmd(ipmi_user_t user,
1366 unsigned char netfn,
Corey Minyardc69c3122006-09-30 23:27:56 -07001367 unsigned char cmd,
1368 unsigned int chans)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Corey Minyard393d2cc2005-11-07 00:59:54 -08001370 ipmi_smi_t intf = user->intf;
1371 struct cmd_rcvr *rcvr;
Corey Minyardc69c3122006-09-30 23:27:56 -07001372 struct cmd_rcvr *rcvrs = NULL;
1373 int i, rv = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Corey Minyardd6dfd132006-03-31 02:30:41 -08001375 mutex_lock(&intf->cmd_rcvrs_mutex);
Corey Minyardc69c3122006-09-30 23:27:56 -07001376 for (i = 0; i < IPMI_NUM_CHANNELS; i++) {
1377 if (((1 << i) & chans) == 0)
1378 continue;
1379 rcvr = find_cmd_rcvr(intf, netfn, cmd, i);
1380 if (rcvr == NULL)
1381 continue;
1382 if (rcvr->user == user) {
1383 rv = 0;
1384 rcvr->chans &= ~chans;
1385 if (rcvr->chans == 0) {
1386 list_del_rcu(&rcvr->link);
1387 rcvr->next = rcvrs;
1388 rcvrs = rcvr;
1389 }
1390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
Corey Minyardc69c3122006-09-30 23:27:56 -07001392 mutex_unlock(&intf->cmd_rcvrs_mutex);
1393 synchronize_rcu();
1394 while (rcvrs) {
Corey Minyard89986492014-04-14 09:46:54 -05001395 atomic_dec(&intf->event_waiters);
Corey Minyardc69c3122006-09-30 23:27:56 -07001396 rcvr = rcvrs;
1397 rcvrs = rcvr->next;
1398 kfree(rcvr);
1399 }
1400 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
Corey Minyardc70d7492008-04-29 01:01:09 -07001402EXPORT_SYMBOL(ipmi_unregister_for_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404static unsigned char
1405ipmb_checksum(unsigned char *data, int size)
1406{
1407 unsigned char csum = 0;
Corey Minyardc70d7492008-04-29 01:01:09 -07001408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 for (; size > 0; size--, data++)
1410 csum += *data;
1411
1412 return -csum;
1413}
1414
1415static inline void format_ipmb_msg(struct ipmi_smi_msg *smi_msg,
1416 struct kernel_ipmi_msg *msg,
1417 struct ipmi_ipmb_addr *ipmb_addr,
1418 long msgid,
1419 unsigned char ipmb_seq,
1420 int broadcast,
1421 unsigned char source_address,
1422 unsigned char source_lun)
1423{
1424 int i = broadcast;
1425
1426 /* Format the IPMB header data. */
1427 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1428 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1429 smi_msg->data[2] = ipmb_addr->channel;
1430 if (broadcast)
1431 smi_msg->data[3] = 0;
1432 smi_msg->data[i+3] = ipmb_addr->slave_addr;
1433 smi_msg->data[i+4] = (msg->netfn << 2) | (ipmb_addr->lun & 0x3);
1434 smi_msg->data[i+5] = ipmb_checksum(&(smi_msg->data[i+3]), 2);
1435 smi_msg->data[i+6] = source_address;
1436 smi_msg->data[i+7] = (ipmb_seq << 2) | source_lun;
1437 smi_msg->data[i+8] = msg->cmd;
1438
1439 /* Now tack on the data to the message. */
1440 if (msg->data_len > 0)
1441 memcpy(&(smi_msg->data[i+9]), msg->data,
1442 msg->data_len);
1443 smi_msg->data_size = msg->data_len + 9;
1444
1445 /* Now calculate the checksum and tack it on. */
1446 smi_msg->data[i+smi_msg->data_size]
1447 = ipmb_checksum(&(smi_msg->data[i+6]),
1448 smi_msg->data_size-6);
1449
Corey Minyardc70d7492008-04-29 01:01:09 -07001450 /*
1451 * Add on the checksum size and the offset from the
1452 * broadcast.
1453 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 smi_msg->data_size += 1 + i;
1455
1456 smi_msg->msgid = msgid;
1457}
1458
1459static inline void format_lan_msg(struct ipmi_smi_msg *smi_msg,
1460 struct kernel_ipmi_msg *msg,
1461 struct ipmi_lan_addr *lan_addr,
1462 long msgid,
1463 unsigned char ipmb_seq,
1464 unsigned char source_lun)
1465{
1466 /* Format the IPMB header data. */
1467 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1468 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1469 smi_msg->data[2] = lan_addr->channel;
1470 smi_msg->data[3] = lan_addr->session_handle;
1471 smi_msg->data[4] = lan_addr->remote_SWID;
1472 smi_msg->data[5] = (msg->netfn << 2) | (lan_addr->lun & 0x3);
1473 smi_msg->data[6] = ipmb_checksum(&(smi_msg->data[4]), 2);
1474 smi_msg->data[7] = lan_addr->local_SWID;
1475 smi_msg->data[8] = (ipmb_seq << 2) | source_lun;
1476 smi_msg->data[9] = msg->cmd;
1477
1478 /* Now tack on the data to the message. */
1479 if (msg->data_len > 0)
1480 memcpy(&(smi_msg->data[10]), msg->data,
1481 msg->data_len);
1482 smi_msg->data_size = msg->data_len + 10;
1483
1484 /* Now calculate the checksum and tack it on. */
1485 smi_msg->data[smi_msg->data_size]
1486 = ipmb_checksum(&(smi_msg->data[7]),
1487 smi_msg->data_size-7);
1488
Corey Minyardc70d7492008-04-29 01:01:09 -07001489 /*
1490 * Add on the checksum size and the offset from the
1491 * broadcast.
1492 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 smi_msg->data_size += 1;
1494
1495 smi_msg->msgid = msgid;
1496}
1497
Corey Minyard7f4a1c82014-11-06 20:52:24 -06001498static void smi_send(ipmi_smi_t intf, struct ipmi_smi_handlers *handlers,
1499 struct ipmi_smi_msg *smi_msg, int priority)
1500{
Corey Minyard7ea0ed22014-11-06 16:58:48 -06001501 int run_to_completion = intf->run_to_completion;
1502 unsigned long flags;
1503
1504 if (!run_to_completion)
1505 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
1506 if (intf->curr_msg) {
1507 if (priority > 0)
1508 list_add_tail(&smi_msg->link, &intf->hp_xmit_msgs);
1509 else
1510 list_add_tail(&smi_msg->link, &intf->xmit_msgs);
1511 smi_msg = NULL;
1512 } else {
1513 intf->curr_msg = smi_msg;
1514 }
1515 if (!run_to_completion)
1516 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
1517
1518 if (smi_msg)
Corey Minyard99ab32f2014-11-07 07:57:31 -06001519 handlers->sender(intf->send_info, smi_msg);
Corey Minyard7f4a1c82014-11-06 20:52:24 -06001520}
1521
Corey Minyardc70d7492008-04-29 01:01:09 -07001522/*
1523 * Separate from ipmi_request so that the user does not have to be
1524 * supplied in certain circumstances (mainly at panic time). If
1525 * messages are supplied, they will be freed, even if an error
1526 * occurs.
1527 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08001528static int i_ipmi_request(ipmi_user_t user,
1529 ipmi_smi_t intf,
1530 struct ipmi_addr *addr,
1531 long msgid,
1532 struct kernel_ipmi_msg *msg,
1533 void *user_msg_data,
1534 void *supplied_smi,
1535 struct ipmi_recv_msg *supplied_recv,
1536 int priority,
1537 unsigned char source_address,
1538 unsigned char source_lun,
1539 int retries,
1540 unsigned int retry_time_ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
Corey Minyardb2c03942006-12-06 20:41:00 -08001542 int rv = 0;
1543 struct ipmi_smi_msg *smi_msg;
1544 struct ipmi_recv_msg *recv_msg;
1545 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547
Corey Minyardc70d7492008-04-29 01:01:09 -07001548 if (supplied_recv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 recv_msg = supplied_recv;
Corey Minyardc70d7492008-04-29 01:01:09 -07001550 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 recv_msg = ipmi_alloc_recv_msg();
Corey Minyardc70d7492008-04-29 01:01:09 -07001552 if (recv_msg == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
1555 recv_msg->user_msg_data = user_msg_data;
1556
Corey Minyardc70d7492008-04-29 01:01:09 -07001557 if (supplied_smi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 smi_msg = (struct ipmi_smi_msg *) supplied_smi;
Corey Minyardc70d7492008-04-29 01:01:09 -07001559 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 smi_msg = ipmi_alloc_smi_msg();
1561 if (smi_msg == NULL) {
1562 ipmi_free_recv_msg(recv_msg);
1563 return -ENOMEM;
1564 }
1565 }
1566
Corey Minyardb2c03942006-12-06 20:41:00 -08001567 rcu_read_lock();
Corey Minyard7ea0ed22014-11-06 16:58:48 -06001568 if (intf->in_shutdown) {
Corey Minyardb2c03942006-12-06 20:41:00 -08001569 rv = -ENODEV;
1570 goto out_err;
1571 }
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 recv_msg->user = user;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001574 if (user)
1575 kref_get(&user->refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 recv_msg->msgid = msgid;
Corey Minyardc70d7492008-04-29 01:01:09 -07001577 /*
1578 * Store the message to send in the receive message so timeout
1579 * responses can get the proper response data.
1580 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 recv_msg->msg = *msg;
1582
1583 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
1584 struct ipmi_system_interface_addr *smi_addr;
1585
1586 if (msg->netfn & 1) {
1587 /* Responses are not allowed to the SMI. */
1588 rv = -EINVAL;
1589 goto out_err;
1590 }
1591
1592 smi_addr = (struct ipmi_system_interface_addr *) addr;
1593 if (smi_addr->lun > 3) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001594 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 rv = -EINVAL;
1596 goto out_err;
1597 }
1598
1599 memcpy(&recv_msg->addr, smi_addr, sizeof(*smi_addr));
1600
1601 if ((msg->netfn == IPMI_NETFN_APP_REQUEST)
1602 && ((msg->cmd == IPMI_SEND_MSG_CMD)
1603 || (msg->cmd == IPMI_GET_MSG_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07001604 || (msg->cmd == IPMI_READ_EVENT_MSG_BUFFER_CMD))) {
1605 /*
1606 * We don't let the user do these, since we manage
1607 * the sequence numbers.
1608 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001609 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 rv = -EINVAL;
1611 goto out_err;
1612 }
1613
Corey Minyardb9675132006-12-06 20:41:02 -08001614 if (((msg->netfn == IPMI_NETFN_APP_REQUEST)
1615 && ((msg->cmd == IPMI_COLD_RESET_CMD)
1616 || (msg->cmd == IPMI_WARM_RESET_CMD)))
Corey Minyardc70d7492008-04-29 01:01:09 -07001617 || (msg->netfn == IPMI_NETFN_FIRMWARE_REQUEST)) {
Corey Minyardb9675132006-12-06 20:41:02 -08001618 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1619 intf->auto_maintenance_timeout
1620 = IPMI_MAINTENANCE_MODE_TIMEOUT;
1621 if (!intf->maintenance_mode
Corey Minyardc70d7492008-04-29 01:01:09 -07001622 && !intf->maintenance_mode_enable) {
Corey Minyard7aefac22014-04-14 09:46:56 -05001623 intf->maintenance_mode_enable = true;
Corey Minyardb9675132006-12-06 20:41:02 -08001624 maintenance_mode_update(intf);
1625 }
1626 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
1627 flags);
1628 }
1629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 if ((msg->data_len + 2) > IPMI_MAX_MSG_LENGTH) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001631 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 rv = -EMSGSIZE;
1633 goto out_err;
1634 }
1635
1636 smi_msg->data[0] = (msg->netfn << 2) | (smi_addr->lun & 0x3);
1637 smi_msg->data[1] = msg->cmd;
1638 smi_msg->msgid = msgid;
1639 smi_msg->user_data = recv_msg;
1640 if (msg->data_len > 0)
1641 memcpy(&(smi_msg->data[2]), msg->data, msg->data_len);
1642 smi_msg->data_size = msg->data_len + 2;
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001643 ipmi_inc_stat(intf, sent_local_commands);
Corey Minyard25176ed2009-04-21 12:24:04 -07001644 } else if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 struct ipmi_ipmb_addr *ipmb_addr;
1646 unsigned char ipmb_seq;
1647 long seqid;
1648 int broadcast = 0;
1649
KAMBAROV, ZAUR9c101fd2005-06-28 20:45:08 -07001650 if (addr->channel >= IPMI_MAX_CHANNELS) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001651 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 rv = -EINVAL;
1653 goto out_err;
1654 }
1655
1656 if (intf->channels[addr->channel].medium
Corey Minyardc70d7492008-04-29 01:01:09 -07001657 != IPMI_CHANNEL_MEDIUM_IPMB) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001658 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 rv = -EINVAL;
1660 goto out_err;
1661 }
1662
1663 if (retries < 0) {
1664 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE)
1665 retries = 0; /* Don't retry broadcasts. */
1666 else
1667 retries = 4;
1668 }
1669 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001670 /*
1671 * Broadcasts add a zero at the beginning of the
1672 * message, but otherwise is the same as an IPMB
1673 * address.
1674 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 addr->addr_type = IPMI_IPMB_ADDR_TYPE;
1676 broadcast = 1;
1677 }
1678
1679
1680 /* Default to 1 second retries. */
1681 if (retry_time_ms == 0)
1682 retry_time_ms = 1000;
1683
Corey Minyardc70d7492008-04-29 01:01:09 -07001684 /*
1685 * 9 for the header and 1 for the checksum, plus
1686 * possibly one for the broadcast.
1687 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 if ((msg->data_len + 10 + broadcast) > IPMI_MAX_MSG_LENGTH) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001689 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 rv = -EMSGSIZE;
1691 goto out_err;
1692 }
1693
1694 ipmb_addr = (struct ipmi_ipmb_addr *) addr;
1695 if (ipmb_addr->lun > 3) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001696 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 rv = -EINVAL;
1698 goto out_err;
1699 }
1700
1701 memcpy(&recv_msg->addr, ipmb_addr, sizeof(*ipmb_addr));
1702
1703 if (recv_msg->msg.netfn & 0x1) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001704 /*
1705 * It's a response, so use the user's sequence
1706 * from msgid.
1707 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001708 ipmi_inc_stat(intf, sent_ipmb_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 format_ipmb_msg(smi_msg, msg, ipmb_addr, msgid,
1710 msgid, broadcast,
1711 source_address, source_lun);
1712
Corey Minyardc70d7492008-04-29 01:01:09 -07001713 /*
1714 * Save the receive message so we can use it
1715 * to deliver the response.
1716 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 smi_msg->user_data = recv_msg;
1718 } else {
1719 /* It's a command, so get a sequence for it. */
1720
1721 spin_lock_irqsave(&(intf->seq_lock), flags);
1722
Corey Minyardc70d7492008-04-29 01:01:09 -07001723 /*
1724 * Create a sequence number with a 1 second
1725 * timeout and 4 retries.
1726 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 rv = intf_next_seq(intf,
1728 recv_msg,
1729 retry_time_ms,
1730 retries,
1731 broadcast,
1732 &ipmb_seq,
1733 &seqid);
1734 if (rv) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001735 /*
1736 * We have used up all the sequence numbers,
1737 * probably, so abort.
1738 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 spin_unlock_irqrestore(&(intf->seq_lock),
1740 flags);
1741 goto out_err;
1742 }
1743
Corey Minyard25176ed2009-04-21 12:24:04 -07001744 ipmi_inc_stat(intf, sent_ipmb_commands);
1745
Corey Minyardc70d7492008-04-29 01:01:09 -07001746 /*
1747 * Store the sequence number in the message,
1748 * so that when the send message response
1749 * comes back we can start the timer.
1750 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 format_ipmb_msg(smi_msg, msg, ipmb_addr,
1752 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1753 ipmb_seq, broadcast,
1754 source_address, source_lun);
1755
Corey Minyardc70d7492008-04-29 01:01:09 -07001756 /*
1757 * Copy the message into the recv message data, so we
1758 * can retransmit it later if necessary.
1759 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 memcpy(recv_msg->msg_data, smi_msg->data,
1761 smi_msg->data_size);
1762 recv_msg->msg.data = recv_msg->msg_data;
1763 recv_msg->msg.data_len = smi_msg->data_size;
1764
Corey Minyardc70d7492008-04-29 01:01:09 -07001765 /*
1766 * We don't unlock until here, because we need
1767 * to copy the completed message into the
1768 * recv_msg before we release the lock.
1769 * Otherwise, race conditions may bite us. I
1770 * know that's pretty paranoid, but I prefer
1771 * to be correct.
1772 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1774 }
Corey Minyard25176ed2009-04-21 12:24:04 -07001775 } else if (is_lan_addr(addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 struct ipmi_lan_addr *lan_addr;
1777 unsigned char ipmb_seq;
1778 long seqid;
1779
Jayachandran C12fc1d72006-02-03 03:04:51 -08001780 if (addr->channel >= IPMI_MAX_CHANNELS) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001781 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 rv = -EINVAL;
1783 goto out_err;
1784 }
1785
1786 if ((intf->channels[addr->channel].medium
Corey Minyardc70d7492008-04-29 01:01:09 -07001787 != IPMI_CHANNEL_MEDIUM_8023LAN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 && (intf->channels[addr->channel].medium
Corey Minyardc70d7492008-04-29 01:01:09 -07001789 != IPMI_CHANNEL_MEDIUM_ASYNC)) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001790 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 rv = -EINVAL;
1792 goto out_err;
1793 }
1794
1795 retries = 4;
1796
1797 /* Default to 1 second retries. */
1798 if (retry_time_ms == 0)
1799 retry_time_ms = 1000;
1800
1801 /* 11 for the header and 1 for the checksum. */
1802 if ((msg->data_len + 12) > IPMI_MAX_MSG_LENGTH) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001803 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 rv = -EMSGSIZE;
1805 goto out_err;
1806 }
1807
1808 lan_addr = (struct ipmi_lan_addr *) addr;
1809 if (lan_addr->lun > 3) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001810 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 rv = -EINVAL;
1812 goto out_err;
1813 }
1814
1815 memcpy(&recv_msg->addr, lan_addr, sizeof(*lan_addr));
1816
1817 if (recv_msg->msg.netfn & 0x1) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001818 /*
1819 * It's a response, so use the user's sequence
1820 * from msgid.
1821 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001822 ipmi_inc_stat(intf, sent_lan_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 format_lan_msg(smi_msg, msg, lan_addr, msgid,
1824 msgid, source_lun);
1825
Corey Minyardc70d7492008-04-29 01:01:09 -07001826 /*
1827 * Save the receive message so we can use it
1828 * to deliver the response.
1829 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 smi_msg->user_data = recv_msg;
1831 } else {
1832 /* It's a command, so get a sequence for it. */
1833
1834 spin_lock_irqsave(&(intf->seq_lock), flags);
1835
Corey Minyardc70d7492008-04-29 01:01:09 -07001836 /*
1837 * Create a sequence number with a 1 second
1838 * timeout and 4 retries.
1839 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 rv = intf_next_seq(intf,
1841 recv_msg,
1842 retry_time_ms,
1843 retries,
1844 0,
1845 &ipmb_seq,
1846 &seqid);
1847 if (rv) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001848 /*
1849 * We have used up all the sequence numbers,
1850 * probably, so abort.
1851 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 spin_unlock_irqrestore(&(intf->seq_lock),
1853 flags);
1854 goto out_err;
1855 }
1856
Corey Minyard25176ed2009-04-21 12:24:04 -07001857 ipmi_inc_stat(intf, sent_lan_commands);
1858
Corey Minyardc70d7492008-04-29 01:01:09 -07001859 /*
1860 * Store the sequence number in the message,
1861 * so that when the send message response
1862 * comes back we can start the timer.
1863 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 format_lan_msg(smi_msg, msg, lan_addr,
1865 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1866 ipmb_seq, source_lun);
1867
Corey Minyardc70d7492008-04-29 01:01:09 -07001868 /*
1869 * Copy the message into the recv message data, so we
1870 * can retransmit it later if necessary.
1871 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 memcpy(recv_msg->msg_data, smi_msg->data,
1873 smi_msg->data_size);
1874 recv_msg->msg.data = recv_msg->msg_data;
1875 recv_msg->msg.data_len = smi_msg->data_size;
1876
Corey Minyardc70d7492008-04-29 01:01:09 -07001877 /*
1878 * We don't unlock until here, because we need
1879 * to copy the completed message into the
1880 * recv_msg before we release the lock.
1881 * Otherwise, race conditions may bite us. I
1882 * know that's pretty paranoid, but I prefer
1883 * to be correct.
1884 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1886 }
1887 } else {
1888 /* Unknown address type. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001889 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 rv = -EINVAL;
1891 goto out_err;
1892 }
1893
1894#ifdef DEBUG_MSGING
1895 {
1896 int m;
Corey Minyarde8b33612005-09-06 15:18:45 -07001897 for (m = 0; m < smi_msg->data_size; m++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 printk(" %2.2x", smi_msg->data[m]);
1899 printk("\n");
1900 }
1901#endif
Corey Minyardb2c03942006-12-06 20:41:00 -08001902
Corey Minyard7ea0ed22014-11-06 16:58:48 -06001903 smi_send(intf, intf->handlers, smi_msg, priority);
Corey Minyardb2c03942006-12-06 20:41:00 -08001904 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 return 0;
1907
1908 out_err:
Corey Minyardb2c03942006-12-06 20:41:00 -08001909 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 ipmi_free_smi_msg(smi_msg);
1911 ipmi_free_recv_msg(recv_msg);
1912 return rv;
1913}
1914
Corey Minyardc14979b2005-09-06 15:18:38 -07001915static int check_addr(ipmi_smi_t intf,
1916 struct ipmi_addr *addr,
1917 unsigned char *saddr,
1918 unsigned char *lun)
1919{
1920 if (addr->channel >= IPMI_MAX_CHANNELS)
1921 return -EINVAL;
1922 *lun = intf->channels[addr->channel].lun;
1923 *saddr = intf->channels[addr->channel].address;
1924 return 0;
1925}
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927int ipmi_request_settime(ipmi_user_t user,
1928 struct ipmi_addr *addr,
1929 long msgid,
1930 struct kernel_ipmi_msg *msg,
1931 void *user_msg_data,
1932 int priority,
1933 int retries,
1934 unsigned int retry_time_ms)
1935{
Corey Minyardf0ba9392013-09-05 06:36:34 -05001936 unsigned char saddr = 0, lun = 0;
Corey Minyardc14979b2005-09-06 15:18:38 -07001937 int rv;
1938
Corey Minyard8a3628d2006-03-31 02:30:40 -08001939 if (!user)
Corey Minyard56a55ec2005-09-06 15:18:42 -07001940 return -EINVAL;
Corey Minyardc14979b2005-09-06 15:18:38 -07001941 rv = check_addr(user->intf, addr, &saddr, &lun);
1942 if (rv)
1943 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 return i_ipmi_request(user,
1945 user->intf,
1946 addr,
1947 msgid,
1948 msg,
1949 user_msg_data,
1950 NULL, NULL,
1951 priority,
Corey Minyardc14979b2005-09-06 15:18:38 -07001952 saddr,
1953 lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 retries,
1955 retry_time_ms);
1956}
Corey Minyardc70d7492008-04-29 01:01:09 -07001957EXPORT_SYMBOL(ipmi_request_settime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959int ipmi_request_supply_msgs(ipmi_user_t user,
1960 struct ipmi_addr *addr,
1961 long msgid,
1962 struct kernel_ipmi_msg *msg,
1963 void *user_msg_data,
1964 void *supplied_smi,
1965 struct ipmi_recv_msg *supplied_recv,
1966 int priority)
1967{
Corey Minyard9ebca932012-10-16 15:53:39 -05001968 unsigned char saddr = 0, lun = 0;
Corey Minyardc14979b2005-09-06 15:18:38 -07001969 int rv;
1970
Corey Minyard8a3628d2006-03-31 02:30:40 -08001971 if (!user)
Corey Minyard56a55ec2005-09-06 15:18:42 -07001972 return -EINVAL;
Corey Minyardc14979b2005-09-06 15:18:38 -07001973 rv = check_addr(user->intf, addr, &saddr, &lun);
1974 if (rv)
1975 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 return i_ipmi_request(user,
1977 user->intf,
1978 addr,
1979 msgid,
1980 msg,
1981 user_msg_data,
1982 supplied_smi,
1983 supplied_recv,
1984 priority,
Corey Minyardc14979b2005-09-06 15:18:38 -07001985 saddr,
1986 lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 -1, 0);
1988}
Corey Minyardc70d7492008-04-29 01:01:09 -07001989EXPORT_SYMBOL(ipmi_request_supply_msgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Randy Dunlap1aa16ee2006-12-06 20:41:20 -08001991#ifdef CONFIG_PROC_FS
Alexey Dobriyan07412732011-05-26 16:25:55 -07001992static int smi_ipmb_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
Alexey Dobriyan07412732011-05-26 16:25:55 -07001994 ipmi_smi_t intf = m->private;
Corey Minyardc14979b2005-09-06 15:18:38 -07001995 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Alexey Dobriyan07412732011-05-26 16:25:55 -07001997 seq_printf(m, "%x", intf->channels[0].address);
1998 for (i = 1; i < IPMI_MAX_CHANNELS; i++)
1999 seq_printf(m, " %x", intf->channels[i].address);
2000 return seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001}
2002
Alexey Dobriyan07412732011-05-26 16:25:55 -07002003static int smi_ipmb_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
Al Virod9dda782013-03-31 18:16:14 -04002005 return single_open(file, smi_ipmb_proc_show, PDE_DATA(inode));
Alexey Dobriyan07412732011-05-26 16:25:55 -07002006}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Alexey Dobriyan07412732011-05-26 16:25:55 -07002008static const struct file_operations smi_ipmb_proc_ops = {
2009 .open = smi_ipmb_proc_open,
2010 .read = seq_read,
2011 .llseek = seq_lseek,
2012 .release = single_release,
2013};
2014
2015static int smi_version_proc_show(struct seq_file *m, void *v)
2016{
2017 ipmi_smi_t intf = m->private;
2018
2019 return seq_printf(m, "%u.%u\n",
Corey Minyard50c812b2006-03-26 01:37:21 -08002020 ipmi_version_major(&intf->bmc->id),
2021 ipmi_version_minor(&intf->bmc->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022}
2023
Alexey Dobriyan07412732011-05-26 16:25:55 -07002024static int smi_version_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
Al Virod9dda782013-03-31 18:16:14 -04002026 return single_open(file, smi_version_proc_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027}
Alexey Dobriyan07412732011-05-26 16:25:55 -07002028
2029static const struct file_operations smi_version_proc_ops = {
2030 .open = smi_version_proc_open,
2031 .read = seq_read,
2032 .llseek = seq_lseek,
2033 .release = single_release,
2034};
2035
2036static int smi_stats_proc_show(struct seq_file *m, void *v)
2037{
2038 ipmi_smi_t intf = m->private;
2039
2040 seq_printf(m, "sent_invalid_commands: %u\n",
2041 ipmi_get_stat(intf, sent_invalid_commands));
2042 seq_printf(m, "sent_local_commands: %u\n",
2043 ipmi_get_stat(intf, sent_local_commands));
2044 seq_printf(m, "handled_local_responses: %u\n",
2045 ipmi_get_stat(intf, handled_local_responses));
2046 seq_printf(m, "unhandled_local_responses: %u\n",
2047 ipmi_get_stat(intf, unhandled_local_responses));
2048 seq_printf(m, "sent_ipmb_commands: %u\n",
2049 ipmi_get_stat(intf, sent_ipmb_commands));
2050 seq_printf(m, "sent_ipmb_command_errs: %u\n",
2051 ipmi_get_stat(intf, sent_ipmb_command_errs));
2052 seq_printf(m, "retransmitted_ipmb_commands: %u\n",
2053 ipmi_get_stat(intf, retransmitted_ipmb_commands));
2054 seq_printf(m, "timed_out_ipmb_commands: %u\n",
2055 ipmi_get_stat(intf, timed_out_ipmb_commands));
2056 seq_printf(m, "timed_out_ipmb_broadcasts: %u\n",
2057 ipmi_get_stat(intf, timed_out_ipmb_broadcasts));
2058 seq_printf(m, "sent_ipmb_responses: %u\n",
2059 ipmi_get_stat(intf, sent_ipmb_responses));
2060 seq_printf(m, "handled_ipmb_responses: %u\n",
2061 ipmi_get_stat(intf, handled_ipmb_responses));
2062 seq_printf(m, "invalid_ipmb_responses: %u\n",
2063 ipmi_get_stat(intf, invalid_ipmb_responses));
2064 seq_printf(m, "unhandled_ipmb_responses: %u\n",
2065 ipmi_get_stat(intf, unhandled_ipmb_responses));
2066 seq_printf(m, "sent_lan_commands: %u\n",
2067 ipmi_get_stat(intf, sent_lan_commands));
2068 seq_printf(m, "sent_lan_command_errs: %u\n",
2069 ipmi_get_stat(intf, sent_lan_command_errs));
2070 seq_printf(m, "retransmitted_lan_commands: %u\n",
2071 ipmi_get_stat(intf, retransmitted_lan_commands));
2072 seq_printf(m, "timed_out_lan_commands: %u\n",
2073 ipmi_get_stat(intf, timed_out_lan_commands));
2074 seq_printf(m, "sent_lan_responses: %u\n",
2075 ipmi_get_stat(intf, sent_lan_responses));
2076 seq_printf(m, "handled_lan_responses: %u\n",
2077 ipmi_get_stat(intf, handled_lan_responses));
2078 seq_printf(m, "invalid_lan_responses: %u\n",
2079 ipmi_get_stat(intf, invalid_lan_responses));
2080 seq_printf(m, "unhandled_lan_responses: %u\n",
2081 ipmi_get_stat(intf, unhandled_lan_responses));
2082 seq_printf(m, "handled_commands: %u\n",
2083 ipmi_get_stat(intf, handled_commands));
2084 seq_printf(m, "invalid_commands: %u\n",
2085 ipmi_get_stat(intf, invalid_commands));
2086 seq_printf(m, "unhandled_commands: %u\n",
2087 ipmi_get_stat(intf, unhandled_commands));
2088 seq_printf(m, "invalid_events: %u\n",
2089 ipmi_get_stat(intf, invalid_events));
2090 seq_printf(m, "events: %u\n",
2091 ipmi_get_stat(intf, events));
2092 seq_printf(m, "failed rexmit LAN msgs: %u\n",
2093 ipmi_get_stat(intf, dropped_rexmit_lan_commands));
2094 seq_printf(m, "failed rexmit IPMB msgs: %u\n",
2095 ipmi_get_stat(intf, dropped_rexmit_ipmb_commands));
2096 return 0;
2097}
2098
2099static int smi_stats_proc_open(struct inode *inode, struct file *file)
2100{
Al Virod9dda782013-03-31 18:16:14 -04002101 return single_open(file, smi_stats_proc_show, PDE_DATA(inode));
Alexey Dobriyan07412732011-05-26 16:25:55 -07002102}
2103
2104static const struct file_operations smi_stats_proc_ops = {
2105 .open = smi_stats_proc_open,
2106 .read = seq_read,
2107 .llseek = seq_lseek,
2108 .release = single_release,
2109};
Randy Dunlap1aa16ee2006-12-06 20:41:20 -08002110#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112int ipmi_smi_add_proc_entry(ipmi_smi_t smi, char *name,
Alexey Dobriyan07412732011-05-26 16:25:55 -07002113 const struct file_operations *proc_ops,
Alexey Dobriyan99b76232009-03-25 22:48:06 +03002114 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 int rv = 0;
Corey Minyard3b625942005-06-23 22:01:42 -07002117#ifdef CONFIG_PROC_FS
2118 struct proc_dir_entry *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 struct ipmi_proc_entry *entry;
2120
2121 /* Create a list element. */
2122 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2123 if (!entry)
2124 return -ENOMEM;
Alexandru Gheorghiu1b6b6982013-05-16 14:04:24 -05002125 entry->name = kstrdup(name, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 if (!entry->name) {
2127 kfree(entry);
2128 return -ENOMEM;
2129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Alexey Dobriyan07412732011-05-26 16:25:55 -07002131 file = proc_create_data(name, 0, smi->proc_dir, proc_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 if (!file) {
2133 kfree(entry->name);
2134 kfree(entry);
2135 rv = -ENOMEM;
2136 } else {
Corey Minyardac019152007-10-18 03:07:11 -07002137 mutex_lock(&smi->proc_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 /* Stick it on the list. */
2139 entry->next = smi->proc_entries;
2140 smi->proc_entries = entry;
Corey Minyardac019152007-10-18 03:07:11 -07002141 mutex_unlock(&smi->proc_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
Corey Minyard3b625942005-06-23 22:01:42 -07002143#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145 return rv;
2146}
Corey Minyardc70d7492008-04-29 01:01:09 -07002147EXPORT_SYMBOL(ipmi_smi_add_proc_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149static int add_proc_entries(ipmi_smi_t smi, int num)
2150{
2151 int rv = 0;
2152
Corey Minyard3b625942005-06-23 22:01:42 -07002153#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 sprintf(smi->proc_dir_name, "%d", num);
2155 smi->proc_dir = proc_mkdir(smi->proc_dir_name, proc_ipmi_root);
2156 if (!smi->proc_dir)
2157 rv = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
2159 if (rv == 0)
2160 rv = ipmi_smi_add_proc_entry(smi, "stats",
Alexey Dobriyan07412732011-05-26 16:25:55 -07002161 &smi_stats_proc_ops,
Alexey Dobriyan99b76232009-03-25 22:48:06 +03002162 smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
2164 if (rv == 0)
2165 rv = ipmi_smi_add_proc_entry(smi, "ipmb",
Alexey Dobriyan07412732011-05-26 16:25:55 -07002166 &smi_ipmb_proc_ops,
Alexey Dobriyan99b76232009-03-25 22:48:06 +03002167 smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
2169 if (rv == 0)
2170 rv = ipmi_smi_add_proc_entry(smi, "version",
Alexey Dobriyan07412732011-05-26 16:25:55 -07002171 &smi_version_proc_ops,
Alexey Dobriyan99b76232009-03-25 22:48:06 +03002172 smi);
Corey Minyard3b625942005-06-23 22:01:42 -07002173#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175 return rv;
2176}
2177
2178static void remove_proc_entries(ipmi_smi_t smi)
2179{
Corey Minyard3b625942005-06-23 22:01:42 -07002180#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 struct ipmi_proc_entry *entry;
2182
Corey Minyardac019152007-10-18 03:07:11 -07002183 mutex_lock(&smi->proc_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 while (smi->proc_entries) {
2185 entry = smi->proc_entries;
2186 smi->proc_entries = entry->next;
2187
2188 remove_proc_entry(entry->name, smi->proc_dir);
2189 kfree(entry->name);
2190 kfree(entry);
2191 }
Corey Minyardac019152007-10-18 03:07:11 -07002192 mutex_unlock(&smi->proc_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 remove_proc_entry(smi->proc_dir_name, proc_ipmi_root);
Corey Minyard3b625942005-06-23 22:01:42 -07002194#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195}
2196
Corey Minyard50c812b2006-03-26 01:37:21 -08002197static int __find_bmc_guid(struct device *dev, void *data)
2198{
2199 unsigned char *id = data;
Corey Minyard16639eb2014-10-10 21:54:03 -05002200 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002201 return memcmp(bmc->guid, id, 16) == 0;
2202}
2203
2204static struct bmc_device *ipmi_find_bmc_guid(struct device_driver *drv,
2205 unsigned char *guid)
2206{
2207 struct device *dev;
2208
2209 dev = driver_find_device(drv, NULL, guid, __find_bmc_guid);
2210 if (dev)
Corey Minyard16639eb2014-10-10 21:54:03 -05002211 return to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002212 else
2213 return NULL;
2214}
2215
2216struct prod_dev_id {
2217 unsigned int product_id;
2218 unsigned char device_id;
2219};
2220
2221static int __find_bmc_prod_dev_id(struct device *dev, void *data)
2222{
2223 struct prod_dev_id *id = data;
Corey Minyard16639eb2014-10-10 21:54:03 -05002224 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002225
2226 return (bmc->id.product_id == id->product_id
Corey Minyard50c812b2006-03-26 01:37:21 -08002227 && bmc->id.device_id == id->device_id);
2228}
2229
2230static struct bmc_device *ipmi_find_bmc_prod_dev_id(
2231 struct device_driver *drv,
Corey Minyardf0b55da2006-12-06 20:40:54 -08002232 unsigned int product_id, unsigned char device_id)
Corey Minyard50c812b2006-03-26 01:37:21 -08002233{
2234 struct prod_dev_id id = {
2235 .product_id = product_id,
2236 .device_id = device_id,
2237 };
2238 struct device *dev;
2239
2240 dev = driver_find_device(drv, NULL, &id, __find_bmc_prod_dev_id);
2241 if (dev)
Corey Minyard16639eb2014-10-10 21:54:03 -05002242 return to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002243 else
2244 return NULL;
2245}
2246
2247static ssize_t device_id_show(struct device *dev,
2248 struct device_attribute *attr,
2249 char *buf)
2250{
Corey Minyard16639eb2014-10-10 21:54:03 -05002251 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002252
2253 return snprintf(buf, 10, "%u\n", bmc->id.device_id);
2254}
Corey Minyard16639eb2014-10-10 21:54:03 -05002255DEVICE_ATTR(device_id, S_IRUGO, device_id_show, NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002256
Corey Minyard16639eb2014-10-10 21:54:03 -05002257static ssize_t provides_device_sdrs_show(struct device *dev,
2258 struct device_attribute *attr,
2259 char *buf)
Corey Minyard50c812b2006-03-26 01:37:21 -08002260{
Corey Minyard16639eb2014-10-10 21:54:03 -05002261 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002262
2263 return snprintf(buf, 10, "%u\n",
Corey Minyard7947d2c2006-11-10 12:27:50 -08002264 (bmc->id.device_revision & 0x80) >> 7);
Corey Minyard50c812b2006-03-26 01:37:21 -08002265}
Corey Minyard16639eb2014-10-10 21:54:03 -05002266DEVICE_ATTR(provides_device_sdrs, S_IRUGO, provides_device_sdrs_show, NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002267
2268static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
2269 char *buf)
2270{
Corey Minyard16639eb2014-10-10 21:54:03 -05002271 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002272
2273 return snprintf(buf, 20, "%u\n",
Corey Minyard7947d2c2006-11-10 12:27:50 -08002274 bmc->id.device_revision & 0x0F);
Corey Minyard50c812b2006-03-26 01:37:21 -08002275}
Corey Minyard16639eb2014-10-10 21:54:03 -05002276DEVICE_ATTR(revision, S_IRUGO, revision_show, NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002277
Corey Minyard16639eb2014-10-10 21:54:03 -05002278static ssize_t firmware_revision_show(struct device *dev,
2279 struct device_attribute *attr,
2280 char *buf)
Corey Minyard50c812b2006-03-26 01:37:21 -08002281{
Corey Minyard16639eb2014-10-10 21:54:03 -05002282 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002283
2284 return snprintf(buf, 20, "%u.%x\n", bmc->id.firmware_revision_1,
2285 bmc->id.firmware_revision_2);
2286}
Corey Minyard16639eb2014-10-10 21:54:03 -05002287DEVICE_ATTR(firmware_revision, S_IRUGO, firmware_revision_show, NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002288
2289static ssize_t ipmi_version_show(struct device *dev,
2290 struct device_attribute *attr,
2291 char *buf)
2292{
Corey Minyard16639eb2014-10-10 21:54:03 -05002293 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002294
2295 return snprintf(buf, 20, "%u.%u\n",
2296 ipmi_version_major(&bmc->id),
2297 ipmi_version_minor(&bmc->id));
2298}
Corey Minyard16639eb2014-10-10 21:54:03 -05002299DEVICE_ATTR(ipmi_version, S_IRUGO, ipmi_version_show, NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002300
2301static ssize_t add_dev_support_show(struct device *dev,
2302 struct device_attribute *attr,
2303 char *buf)
2304{
Corey Minyard16639eb2014-10-10 21:54:03 -05002305 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002306
2307 return snprintf(buf, 10, "0x%02x\n",
2308 bmc->id.additional_device_support);
2309}
Corey Minyard16639eb2014-10-10 21:54:03 -05002310DEVICE_ATTR(additional_device_support, S_IRUGO, add_dev_support_show, NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002311
2312static ssize_t manufacturer_id_show(struct device *dev,
2313 struct device_attribute *attr,
2314 char *buf)
2315{
Corey Minyard16639eb2014-10-10 21:54:03 -05002316 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002317
2318 return snprintf(buf, 20, "0x%6.6x\n", bmc->id.manufacturer_id);
2319}
Corey Minyard16639eb2014-10-10 21:54:03 -05002320DEVICE_ATTR(manufacturer_id, S_IRUGO, manufacturer_id_show, NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002321
2322static ssize_t product_id_show(struct device *dev,
2323 struct device_attribute *attr,
2324 char *buf)
2325{
Corey Minyard16639eb2014-10-10 21:54:03 -05002326 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002327
2328 return snprintf(buf, 10, "0x%4.4x\n", bmc->id.product_id);
2329}
Corey Minyard16639eb2014-10-10 21:54:03 -05002330DEVICE_ATTR(product_id, S_IRUGO, product_id_show, NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002331
2332static ssize_t aux_firmware_rev_show(struct device *dev,
2333 struct device_attribute *attr,
2334 char *buf)
2335{
Corey Minyard16639eb2014-10-10 21:54:03 -05002336 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002337
2338 return snprintf(buf, 21, "0x%02x 0x%02x 0x%02x 0x%02x\n",
2339 bmc->id.aux_firmware_revision[3],
2340 bmc->id.aux_firmware_revision[2],
2341 bmc->id.aux_firmware_revision[1],
2342 bmc->id.aux_firmware_revision[0]);
2343}
Corey Minyard16639eb2014-10-10 21:54:03 -05002344DEVICE_ATTR(aux_firmware_revision, S_IRUGO, aux_firmware_rev_show, NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002345
2346static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
2347 char *buf)
2348{
Corey Minyard16639eb2014-10-10 21:54:03 -05002349 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002350
2351 return snprintf(buf, 100, "%Lx%Lx\n",
2352 (long long) bmc->guid[0],
2353 (long long) bmc->guid[8]);
2354}
Corey Minyard16639eb2014-10-10 21:54:03 -05002355DEVICE_ATTR(guid, S_IRUGO, guid_show, NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002356
Corey Minyard16639eb2014-10-10 21:54:03 -05002357static struct attribute *bmc_dev_attrs[] = {
2358 &dev_attr_device_id.attr,
2359 &dev_attr_provides_device_sdrs.attr,
2360 &dev_attr_revision.attr,
2361 &dev_attr_firmware_revision.attr,
2362 &dev_attr_ipmi_version.attr,
2363 &dev_attr_additional_device_support.attr,
2364 &dev_attr_manufacturer_id.attr,
2365 &dev_attr_product_id.attr,
2366 NULL
2367};
2368
2369static struct attribute_group bmc_dev_attr_group = {
2370 .attrs = bmc_dev_attrs,
2371};
2372
2373static const struct attribute_group *bmc_dev_attr_groups[] = {
2374 &bmc_dev_attr_group,
2375 NULL
2376};
2377
2378static struct device_type bmc_device_type = {
2379 .groups = bmc_dev_attr_groups,
2380};
2381
2382static void
2383release_bmc_device(struct device *dev)
Corey Minyard50c812b2006-03-26 01:37:21 -08002384{
Corey Minyard16639eb2014-10-10 21:54:03 -05002385 kfree(to_bmc_device(dev));
Jeff Garzik5e593932006-10-11 01:22:21 -07002386}
2387
2388static void
2389cleanup_bmc_device(struct kref *ref)
2390{
Corey Minyard16639eb2014-10-10 21:54:03 -05002391 struct bmc_device *bmc = container_of(ref, struct bmc_device, usecount);
Jeff Garzik5e593932006-10-11 01:22:21 -07002392
Corey Minyard16639eb2014-10-10 21:54:03 -05002393 if (bmc->id.aux_firmware_revision_set)
2394 device_remove_file(&bmc->pdev.dev,
2395 &bmc->aux_firmware_rev_attr);
2396 if (bmc->guid_set)
2397 device_remove_file(&bmc->pdev.dev,
2398 &bmc->guid_attr);
Jeff Garzik5e593932006-10-11 01:22:21 -07002399
Corey Minyard16639eb2014-10-10 21:54:03 -05002400 platform_device_unregister(&bmc->pdev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002401}
2402
2403static void ipmi_bmc_unregister(ipmi_smi_t intf)
2404{
2405 struct bmc_device *bmc = intf->bmc;
2406
Corey Minyard5a0e10e2014-10-10 22:11:05 -05002407 sysfs_remove_link(&intf->si_dev->kobj, "bmc");
Corey Minyard50c812b2006-03-26 01:37:21 -08002408 if (intf->my_dev_name) {
Corey Minyard16639eb2014-10-10 21:54:03 -05002409 sysfs_remove_link(&bmc->pdev.dev.kobj, intf->my_dev_name);
Corey Minyard50c812b2006-03-26 01:37:21 -08002410 kfree(intf->my_dev_name);
2411 intf->my_dev_name = NULL;
2412 }
2413
2414 mutex_lock(&ipmidriver_mutex);
Corey Minyard16639eb2014-10-10 21:54:03 -05002415 kref_put(&bmc->usecount, cleanup_bmc_device);
Corey Minyardf0b55da2006-12-06 20:40:54 -08002416 intf->bmc = NULL;
Corey Minyard50c812b2006-03-26 01:37:21 -08002417 mutex_unlock(&ipmidriver_mutex);
2418}
2419
Corey Minyard16639eb2014-10-10 21:54:03 -05002420static int create_bmc_files(struct bmc_device *bmc)
Jeff Garzik5e593932006-10-11 01:22:21 -07002421{
2422 int err;
2423
Jeff Garzik5e593932006-10-11 01:22:21 -07002424 if (bmc->id.aux_firmware_revision_set) {
Corey Minyard3d9e5df2014-11-06 20:11:40 -06002425 bmc->aux_firmware_rev_attr.attr.name = "aux_firmware_revision";
Corey Minyard16639eb2014-10-10 21:54:03 -05002426 err = device_create_file(&bmc->pdev.dev,
Jeff Garzik5e593932006-10-11 01:22:21 -07002427 &bmc->aux_firmware_rev_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002428 if (err)
Corey Minyard16639eb2014-10-10 21:54:03 -05002429 goto out;
Jeff Garzik5e593932006-10-11 01:22:21 -07002430 }
2431 if (bmc->guid_set) {
Corey Minyard3d9e5df2014-11-06 20:11:40 -06002432 bmc->guid_attr.attr.name = "guid";
Corey Minyard16639eb2014-10-10 21:54:03 -05002433 err = device_create_file(&bmc->pdev.dev,
Jeff Garzik5e593932006-10-11 01:22:21 -07002434 &bmc->guid_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002435 if (err)
2436 goto out_aux_firm;
Jeff Garzik5e593932006-10-11 01:22:21 -07002437 }
2438
2439 return 0;
2440
2441out_aux_firm:
2442 if (bmc->id.aux_firmware_revision_set)
Corey Minyard16639eb2014-10-10 21:54:03 -05002443 device_remove_file(&bmc->pdev.dev,
Jeff Garzik5e593932006-10-11 01:22:21 -07002444 &bmc->aux_firmware_rev_attr);
Jeff Garzik5e593932006-10-11 01:22:21 -07002445out:
2446 return err;
2447}
2448
Corey Minyard5a0e10e2014-10-10 22:11:05 -05002449static int ipmi_bmc_register(ipmi_smi_t intf, int ifnum)
Corey Minyard50c812b2006-03-26 01:37:21 -08002450{
2451 int rv;
2452 struct bmc_device *bmc = intf->bmc;
2453 struct bmc_device *old_bmc;
Corey Minyard50c812b2006-03-26 01:37:21 -08002454
2455 mutex_lock(&ipmidriver_mutex);
2456
2457 /*
2458 * Try to find if there is an bmc_device struct
2459 * representing the interfaced BMC already
2460 */
2461 if (bmc->guid_set)
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08002462 old_bmc = ipmi_find_bmc_guid(&ipmidriver.driver, bmc->guid);
Corey Minyard50c812b2006-03-26 01:37:21 -08002463 else
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08002464 old_bmc = ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
Corey Minyard50c812b2006-03-26 01:37:21 -08002465 bmc->id.product_id,
2466 bmc->id.device_id);
2467
2468 /*
2469 * If there is already an bmc_device, free the new one,
2470 * otherwise register the new BMC device
2471 */
2472 if (old_bmc) {
2473 kfree(bmc);
2474 intf->bmc = old_bmc;
2475 bmc = old_bmc;
2476
Corey Minyard16639eb2014-10-10 21:54:03 -05002477 kref_get(&bmc->usecount);
Corey Minyard50c812b2006-03-26 01:37:21 -08002478 mutex_unlock(&ipmidriver_mutex);
2479
2480 printk(KERN_INFO
2481 "ipmi: interfacing existing BMC (man_id: 0x%6.6x,"
2482 " prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2483 bmc->id.manufacturer_id,
2484 bmc->id.product_id,
2485 bmc->id.device_id);
2486 } else {
Corey Minyardf0b55da2006-12-06 20:40:54 -08002487 unsigned char orig_dev_id = bmc->id.device_id;
2488 int warn_printed = 0;
2489
Corey Minyard16639eb2014-10-10 21:54:03 -05002490 snprintf(bmc->name, sizeof(bmc->name),
Corey Minyardf0b55da2006-12-06 20:40:54 -08002491 "ipmi_bmc.%4.4x", bmc->id.product_id);
Corey Minyard16639eb2014-10-10 21:54:03 -05002492 bmc->pdev.name = bmc->name;
Corey Minyardf0b55da2006-12-06 20:40:54 -08002493
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08002494 while (ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
Corey Minyardf0b55da2006-12-06 20:40:54 -08002495 bmc->id.product_id,
Corey Minyard1d5636c2006-12-10 02:19:08 -08002496 bmc->id.device_id)) {
Corey Minyardf0b55da2006-12-06 20:40:54 -08002497 if (!warn_printed) {
2498 printk(KERN_WARNING PFX
2499 "This machine has two different BMCs"
2500 " with the same product id and device"
2501 " id. This is an error in the"
2502 " firmware, but incrementing the"
2503 " device id to work around the problem."
2504 " Prod ID = 0x%x, Dev ID = 0x%x\n",
2505 bmc->id.product_id, bmc->id.device_id);
2506 warn_printed = 1;
2507 }
2508 bmc->id.device_id++; /* Wraps at 255 */
2509 if (bmc->id.device_id == orig_dev_id) {
2510 printk(KERN_ERR PFX
2511 "Out of device ids!\n");
2512 break;
2513 }
2514 }
2515
Corey Minyard16639eb2014-10-10 21:54:03 -05002516 bmc->pdev.dev.driver = &ipmidriver.driver;
2517 bmc->pdev.id = bmc->id.device_id;
2518 bmc->pdev.dev.release = release_bmc_device;
2519 bmc->pdev.dev.type = &bmc_device_type;
Corey Minyard5a0e10e2014-10-10 22:11:05 -05002520 kref_init(&bmc->usecount);
Corey Minyard50c812b2006-03-26 01:37:21 -08002521
Corey Minyard16639eb2014-10-10 21:54:03 -05002522 rv = platform_device_register(&bmc->pdev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002523 mutex_unlock(&ipmidriver_mutex);
2524 if (rv) {
Corey Minyard16639eb2014-10-10 21:54:03 -05002525 put_device(&bmc->pdev.dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002526 printk(KERN_ERR
2527 "ipmi_msghandler:"
2528 " Unable to register bmc device: %d\n",
2529 rv);
Corey Minyardc70d7492008-04-29 01:01:09 -07002530 /*
2531 * Don't go to out_err, you can only do that if
2532 * the device is registered already.
2533 */
Corey Minyard50c812b2006-03-26 01:37:21 -08002534 return rv;
2535 }
2536
Corey Minyard16639eb2014-10-10 21:54:03 -05002537 rv = create_bmc_files(bmc);
Jeff Garzik5e593932006-10-11 01:22:21 -07002538 if (rv) {
2539 mutex_lock(&ipmidriver_mutex);
Corey Minyard16639eb2014-10-10 21:54:03 -05002540 platform_device_unregister(&bmc->pdev);
Jeff Garzik5e593932006-10-11 01:22:21 -07002541 mutex_unlock(&ipmidriver_mutex);
2542
2543 return rv;
2544 }
Corey Minyard50c812b2006-03-26 01:37:21 -08002545
Myron Stowe279fbd02010-05-26 14:43:52 -07002546 dev_info(intf->si_dev, "Found new BMC (man_id: 0x%6.6x, "
2547 "prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2548 bmc->id.manufacturer_id,
2549 bmc->id.product_id,
2550 bmc->id.device_id);
Corey Minyard50c812b2006-03-26 01:37:21 -08002551 }
2552
2553 /*
2554 * create symlink from system interface device to bmc device
2555 * and back.
2556 */
Corey Minyard5a0e10e2014-10-10 22:11:05 -05002557 rv = sysfs_create_link(&intf->si_dev->kobj, &bmc->pdev.dev.kobj, "bmc");
Corey Minyard50c812b2006-03-26 01:37:21 -08002558 if (rv) {
2559 printk(KERN_ERR
2560 "ipmi_msghandler: Unable to create bmc symlink: %d\n",
2561 rv);
2562 goto out_err;
2563 }
2564
Corey Minyard16639eb2014-10-10 21:54:03 -05002565 intf->my_dev_name = kasprintf(GFP_KERNEL, "ipmi%d", ifnum);
Corey Minyard50c812b2006-03-26 01:37:21 -08002566 if (!intf->my_dev_name) {
2567 rv = -ENOMEM;
2568 printk(KERN_ERR
2569 "ipmi_msghandler: allocate link from BMC: %d\n",
2570 rv);
2571 goto out_err;
2572 }
Corey Minyard50c812b2006-03-26 01:37:21 -08002573
Corey Minyard16639eb2014-10-10 21:54:03 -05002574 rv = sysfs_create_link(&bmc->pdev.dev.kobj, &intf->si_dev->kobj,
Corey Minyard50c812b2006-03-26 01:37:21 -08002575 intf->my_dev_name);
2576 if (rv) {
2577 kfree(intf->my_dev_name);
2578 intf->my_dev_name = NULL;
2579 printk(KERN_ERR
2580 "ipmi_msghandler:"
2581 " Unable to create symlink to bmc: %d\n",
2582 rv);
2583 goto out_err;
2584 }
2585
2586 return 0;
2587
2588out_err:
2589 ipmi_bmc_unregister(intf);
2590 return rv;
2591}
2592
2593static int
2594send_guid_cmd(ipmi_smi_t intf, int chan)
2595{
2596 struct kernel_ipmi_msg msg;
2597 struct ipmi_system_interface_addr si;
2598
2599 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2600 si.channel = IPMI_BMC_CHANNEL;
2601 si.lun = 0;
2602
2603 msg.netfn = IPMI_NETFN_APP_REQUEST;
2604 msg.cmd = IPMI_GET_DEVICE_GUID_CMD;
2605 msg.data = NULL;
2606 msg.data_len = 0;
2607 return i_ipmi_request(NULL,
2608 intf,
2609 (struct ipmi_addr *) &si,
2610 0,
2611 &msg,
2612 intf,
2613 NULL,
2614 NULL,
2615 0,
2616 intf->channels[0].address,
2617 intf->channels[0].lun,
2618 -1, 0);
2619}
2620
2621static void
2622guid_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
2623{
2624 if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2625 || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
2626 || (msg->msg.cmd != IPMI_GET_DEVICE_GUID_CMD))
2627 /* Not for me */
2628 return;
2629
2630 if (msg->msg.data[0] != 0) {
2631 /* Error from getting the GUID, the BMC doesn't have one. */
2632 intf->bmc->guid_set = 0;
2633 goto out;
2634 }
2635
2636 if (msg->msg.data_len < 17) {
2637 intf->bmc->guid_set = 0;
2638 printk(KERN_WARNING PFX
2639 "guid_handler: The GUID response from the BMC was too"
2640 " short, it was %d but should have been 17. Assuming"
2641 " GUID is not available.\n",
2642 msg->msg.data_len);
2643 goto out;
2644 }
2645
2646 memcpy(intf->bmc->guid, msg->msg.data, 16);
2647 intf->bmc->guid_set = 1;
2648 out:
2649 wake_up(&intf->waitq);
2650}
2651
2652static void
2653get_guid(ipmi_smi_t intf)
2654{
2655 int rv;
2656
2657 intf->bmc->guid_set = 0x2;
2658 intf->null_user_handler = guid_handler;
2659 rv = send_guid_cmd(intf, 0);
2660 if (rv)
2661 /* Send failed, no GUID available. */
2662 intf->bmc->guid_set = 0;
2663 wait_event(intf->waitq, intf->bmc->guid_set != 2);
2664 intf->null_user_handler = NULL;
2665}
2666
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667static int
2668send_channel_info_cmd(ipmi_smi_t intf, int chan)
2669{
2670 struct kernel_ipmi_msg msg;
2671 unsigned char data[1];
2672 struct ipmi_system_interface_addr si;
2673
2674 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2675 si.channel = IPMI_BMC_CHANNEL;
2676 si.lun = 0;
2677
2678 msg.netfn = IPMI_NETFN_APP_REQUEST;
2679 msg.cmd = IPMI_GET_CHANNEL_INFO_CMD;
2680 msg.data = data;
2681 msg.data_len = 1;
2682 data[0] = chan;
2683 return i_ipmi_request(NULL,
2684 intf,
2685 (struct ipmi_addr *) &si,
2686 0,
2687 &msg,
Corey Minyard56a55ec2005-09-06 15:18:42 -07002688 intf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 NULL,
2690 NULL,
2691 0,
Corey Minyardc14979b2005-09-06 15:18:38 -07002692 intf->channels[0].address,
2693 intf->channels[0].lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 -1, 0);
2695}
2696
2697static void
Corey Minyard56a55ec2005-09-06 15:18:42 -07002698channel_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
2700 int rv = 0;
2701 int chan;
2702
Corey Minyard56a55ec2005-09-06 15:18:42 -07002703 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2704 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
Corey Minyardc70d7492008-04-29 01:01:09 -07002705 && (msg->msg.cmd == IPMI_GET_CHANNEL_INFO_CMD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 /* It's the one we want */
Corey Minyard56a55ec2005-09-06 15:18:42 -07002707 if (msg->msg.data[0] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 /* Got an error from the channel, just go on. */
2709
Corey Minyard56a55ec2005-09-06 15:18:42 -07002710 if (msg->msg.data[0] == IPMI_INVALID_COMMAND_ERR) {
Corey Minyardc70d7492008-04-29 01:01:09 -07002711 /*
2712 * If the MC does not support this
2713 * command, that is legal. We just
2714 * assume it has one IPMB at channel
2715 * zero.
2716 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 intf->channels[0].medium
2718 = IPMI_CHANNEL_MEDIUM_IPMB;
2719 intf->channels[0].protocol
2720 = IPMI_CHANNEL_PROTOCOL_IPMB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
2722 intf->curr_channel = IPMI_MAX_CHANNELS;
2723 wake_up(&intf->waitq);
2724 goto out;
2725 }
2726 goto next_channel;
2727 }
Corey Minyard56a55ec2005-09-06 15:18:42 -07002728 if (msg->msg.data_len < 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 /* Message not big enough, just go on. */
2730 goto next_channel;
2731 }
2732 chan = intf->curr_channel;
Corey Minyard56a55ec2005-09-06 15:18:42 -07002733 intf->channels[chan].medium = msg->msg.data[2] & 0x7f;
2734 intf->channels[chan].protocol = msg->msg.data[3] & 0x1f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Corey Minyardc70d7492008-04-29 01:01:09 -07002736 next_channel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 intf->curr_channel++;
2738 if (intf->curr_channel >= IPMI_MAX_CHANNELS)
2739 wake_up(&intf->waitq);
2740 else
2741 rv = send_channel_info_cmd(intf, intf->curr_channel);
2742
2743 if (rv) {
2744 /* Got an error somehow, just give up. */
Corey Minyard1f668422014-10-06 14:17:50 -05002745 printk(KERN_WARNING PFX
2746 "Error sending channel information for channel"
2747 " %d: %d\n", intf->curr_channel, rv);
2748
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 intf->curr_channel = IPMI_MAX_CHANNELS;
2750 wake_up(&intf->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 }
2752 }
2753 out:
2754 return;
2755}
2756
Corey Minyard895dcfd2012-03-28 14:42:49 -07002757static void ipmi_poll(ipmi_smi_t intf)
Corey Minyardfcfa4722007-10-18 03:07:09 -07002758{
Corey Minyardfcfa4722007-10-18 03:07:09 -07002759 if (intf->handlers->poll)
2760 intf->handlers->poll(intf->send_info);
Corey Minyard7adf5792012-03-28 14:42:49 -07002761 /* In case something came in */
2762 handle_new_recv_msgs(intf);
Corey Minyardfcfa4722007-10-18 03:07:09 -07002763}
Corey Minyard895dcfd2012-03-28 14:42:49 -07002764
2765void ipmi_poll_interface(ipmi_user_t user)
2766{
2767 ipmi_poll(user->intf);
Corey Minyardfcfa4722007-10-18 03:07:09 -07002768}
Corey Minyardc70d7492008-04-29 01:01:09 -07002769EXPORT_SYMBOL(ipmi_poll_interface);
Corey Minyardfcfa4722007-10-18 03:07:09 -07002770
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771int ipmi_register_smi(struct ipmi_smi_handlers *handlers,
2772 void *send_info,
Corey Minyard50c812b2006-03-26 01:37:21 -08002773 struct ipmi_device_id *device_id,
2774 struct device *si_dev,
Corey Minyard453823b2006-03-31 02:30:39 -08002775 unsigned char slave_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776{
2777 int i, j;
2778 int rv;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002779 ipmi_smi_t intf;
Corey Minyardbca03242006-12-06 20:40:57 -08002780 ipmi_smi_t tintf;
Corey Minyardbca03242006-12-06 20:40:57 -08002781 struct list_head *link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
Corey Minyardc70d7492008-04-29 01:01:09 -07002783 /*
2784 * Make sure the driver is actually initialized, this handles
2785 * problems with initialization order.
2786 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 if (!initialized) {
2788 rv = ipmi_init_msghandler();
2789 if (rv)
2790 return rv;
Corey Minyardc70d7492008-04-29 01:01:09 -07002791 /*
2792 * The init code doesn't return an error if it was turned
2793 * off, but it won't initialize. Check that.
2794 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 if (!initialized)
2796 return -ENODEV;
2797 }
2798
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002799 intf = kzalloc(sizeof(*intf), GFP_KERNEL);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002800 if (!intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 return -ENOMEM;
Corey Minyardb2c03942006-12-06 20:41:00 -08002802
2803 intf->ipmi_version_major = ipmi_version_major(device_id);
2804 intf->ipmi_version_minor = ipmi_version_minor(device_id);
2805
Corey Minyard50c812b2006-03-26 01:37:21 -08002806 intf->bmc = kzalloc(sizeof(*intf->bmc), GFP_KERNEL);
2807 if (!intf->bmc) {
2808 kfree(intf);
2809 return -ENOMEM;
2810 }
Corey Minyardbca03242006-12-06 20:40:57 -08002811 intf->intf_num = -1; /* Mark it invalid for now. */
Corey Minyard393d2cc2005-11-07 00:59:54 -08002812 kref_init(&intf->refcount);
Corey Minyard50c812b2006-03-26 01:37:21 -08002813 intf->bmc->id = *device_id;
2814 intf->si_dev = si_dev;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002815 for (j = 0; j < IPMI_MAX_CHANNELS; j++) {
2816 intf->channels[j].address = IPMI_BMC_SLAVE_ADDR;
2817 intf->channels[j].lun = 2;
2818 }
2819 if (slave_addr != 0)
2820 intf->channels[0].address = slave_addr;
2821 INIT_LIST_HEAD(&intf->users);
2822 intf->handlers = handlers;
2823 intf->send_info = send_info;
2824 spin_lock_init(&intf->seq_lock);
2825 for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++) {
2826 intf->seq_table[j].inuse = 0;
2827 intf->seq_table[j].seqid = 0;
2828 }
2829 intf->curr_seq = 0;
2830#ifdef CONFIG_PROC_FS
Corey Minyardac019152007-10-18 03:07:11 -07002831 mutex_init(&intf->proc_entry_lock);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002832#endif
Corey Minyard65be7542014-11-06 20:48:02 -06002833 spin_lock_init(&intf->waiting_rcv_msgs_lock);
2834 INIT_LIST_HEAD(&intf->waiting_rcv_msgs);
Corey Minyard7adf5792012-03-28 14:42:49 -07002835 tasklet_init(&intf->recv_tasklet,
2836 smi_recv_tasklet,
2837 (unsigned long) intf);
2838 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 0);
Corey Minyard7ea0ed22014-11-06 16:58:48 -06002839 spin_lock_init(&intf->xmit_msgs_lock);
2840 INIT_LIST_HEAD(&intf->xmit_msgs);
2841 INIT_LIST_HEAD(&intf->hp_xmit_msgs);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002842 spin_lock_init(&intf->events_lock);
Corey Minyard89986492014-04-14 09:46:54 -05002843 atomic_set(&intf->event_waiters, 0);
2844 intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002845 INIT_LIST_HEAD(&intf->waiting_events);
2846 intf->waiting_events_count = 0;
Corey Minyardd6dfd132006-03-31 02:30:41 -08002847 mutex_init(&intf->cmd_rcvrs_mutex);
Corey Minyardb9675132006-12-06 20:41:02 -08002848 spin_lock_init(&intf->maintenance_mode_lock);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002849 INIT_LIST_HEAD(&intf->cmd_rcvrs);
2850 init_waitqueue_head(&intf->waitq);
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07002851 for (i = 0; i < IPMI_NUM_STATS; i++)
2852 atomic_set(&intf->stats[i], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
Corey Minyard393d2cc2005-11-07 00:59:54 -08002854 intf->proc_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
Corey Minyardb2c03942006-12-06 20:41:00 -08002856 mutex_lock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -08002857 mutex_lock(&ipmi_interfaces_mutex);
2858 /* Look for a hole in the numbers. */
2859 i = 0;
2860 link = &ipmi_interfaces;
2861 list_for_each_entry_rcu(tintf, &ipmi_interfaces, link) {
2862 if (tintf->intf_num != i) {
2863 link = &tintf->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 break;
2865 }
Corey Minyardbca03242006-12-06 20:40:57 -08002866 i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 }
Corey Minyardbca03242006-12-06 20:40:57 -08002868 /* Add the new interface in numeric order. */
2869 if (i == 0)
2870 list_add_rcu(&intf->link, &ipmi_interfaces);
2871 else
2872 list_add_tail_rcu(&intf->link, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
Corey Minyard453823b2006-03-31 02:30:39 -08002874 rv = handlers->start_processing(send_info, intf);
2875 if (rv)
2876 goto out;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002877
Corey Minyard50c812b2006-03-26 01:37:21 -08002878 get_guid(intf);
2879
Corey Minyardb2c03942006-12-06 20:41:00 -08002880 if ((intf->ipmi_version_major > 1)
Corey Minyardc70d7492008-04-29 01:01:09 -07002881 || ((intf->ipmi_version_major == 1)
2882 && (intf->ipmi_version_minor >= 5))) {
2883 /*
2884 * Start scanning the channels to see what is
2885 * available.
2886 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08002887 intf->null_user_handler = channel_handler;
2888 intf->curr_channel = 0;
2889 rv = send_channel_info_cmd(intf, 0);
Corey Minyard1f668422014-10-06 14:17:50 -05002890 if (rv) {
2891 printk(KERN_WARNING PFX
2892 "Error sending channel information for channel"
2893 " 0, %d\n", rv);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002894 goto out;
Corey Minyard1f668422014-10-06 14:17:50 -05002895 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08002896
2897 /* Wait for the channel info to be read. */
2898 wait_event(intf->waitq,
2899 intf->curr_channel >= IPMI_MAX_CHANNELS);
Corey Minyard50c812b2006-03-26 01:37:21 -08002900 intf->null_user_handler = NULL;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002901 } else {
2902 /* Assume a single IPMB channel at zero. */
2903 intf->channels[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
2904 intf->channels[0].protocol = IPMI_CHANNEL_PROTOCOL_IPMB;
Corey Minyard9a2845c2009-05-20 13:36:17 -05002905 intf->curr_channel = IPMI_MAX_CHANNELS;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
2908 if (rv == 0)
Corey Minyard393d2cc2005-11-07 00:59:54 -08002909 rv = add_proc_entries(intf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
Corey Minyard5a0e10e2014-10-10 22:11:05 -05002911 rv = ipmi_bmc_register(intf, i);
Corey Minyard50c812b2006-03-26 01:37:21 -08002912
Corey Minyard393d2cc2005-11-07 00:59:54 -08002913 out:
2914 if (rv) {
2915 if (intf->proc_dir)
2916 remove_proc_entries(intf);
Corey Minyardb2c03942006-12-06 20:41:00 -08002917 intf->handlers = NULL;
Corey Minyardbca03242006-12-06 20:40:57 -08002918 list_del_rcu(&intf->link);
2919 mutex_unlock(&ipmi_interfaces_mutex);
Corey Minyardb2c03942006-12-06 20:41:00 -08002920 mutex_unlock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -08002921 synchronize_rcu();
Corey Minyard393d2cc2005-11-07 00:59:54 -08002922 kref_put(&intf->refcount, intf_free);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002923 } else {
Corey Minyard78ba2fa2007-02-10 01:45:45 -08002924 /*
2925 * Keep memory order straight for RCU readers. Make
2926 * sure everything else is committed to memory before
2927 * setting intf_num to mark the interface valid.
2928 */
2929 smp_wmb();
Corey Minyardbca03242006-12-06 20:40:57 -08002930 intf->intf_num = i;
2931 mutex_unlock(&ipmi_interfaces_mutex);
Corey Minyard78ba2fa2007-02-10 01:45:45 -08002932 /* After this point the interface is legal to use. */
Corey Minyard50c812b2006-03-26 01:37:21 -08002933 call_smi_watchers(i, intf->si_dev);
Corey Minyardb2c03942006-12-06 20:41:00 -08002934 mutex_unlock(&smi_watchers_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 }
2936
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 return rv;
2938}
Corey Minyardc70d7492008-04-29 01:01:09 -07002939EXPORT_SYMBOL(ipmi_register_smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
Corey Minyard7ea0ed22014-11-06 16:58:48 -06002941static void deliver_smi_err_response(ipmi_smi_t intf,
2942 struct ipmi_smi_msg *msg,
2943 unsigned char err)
2944{
2945 msg->rsp[0] = msg->data[0] | 4;
2946 msg->rsp[1] = msg->data[1];
2947 msg->rsp[2] = err;
2948 msg->rsp_size = 3;
2949 /* It's an error, so it will never requeue, no need to check return. */
2950 handle_one_recv_msg(intf, msg);
2951}
2952
Corey Minyardb2c03942006-12-06 20:41:00 -08002953static void cleanup_smi_msgs(ipmi_smi_t intf)
2954{
2955 int i;
2956 struct seq_table *ent;
Corey Minyard7ea0ed22014-11-06 16:58:48 -06002957 struct ipmi_smi_msg *msg;
2958 struct list_head *entry;
2959 struct list_head tmplist;
2960
2961 /* Clear out our transmit queues and hold the messages. */
2962 INIT_LIST_HEAD(&tmplist);
2963 list_splice_tail(&intf->hp_xmit_msgs, &tmplist);
2964 list_splice_tail(&intf->xmit_msgs, &tmplist);
2965
2966 /* Current message first, to preserve order */
2967 while (intf->curr_msg && !list_empty(&intf->waiting_rcv_msgs)) {
2968 /* Wait for the message to clear out. */
2969 schedule_timeout(1);
2970 }
Corey Minyardb2c03942006-12-06 20:41:00 -08002971
2972 /* No need for locks, the interface is down. */
Corey Minyard7ea0ed22014-11-06 16:58:48 -06002973
2974 /*
2975 * Return errors for all pending messages in queue and in the
2976 * tables waiting for remote responses.
2977 */
2978 while (!list_empty(&tmplist)) {
2979 entry = tmplist.next;
2980 list_del(entry);
2981 msg = list_entry(entry, struct ipmi_smi_msg, link);
2982 deliver_smi_err_response(intf, msg, IPMI_ERR_UNSPECIFIED);
2983 }
2984
Corey Minyardb2c03942006-12-06 20:41:00 -08002985 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
2986 ent = &(intf->seq_table[i]);
2987 if (!ent->inuse)
2988 continue;
2989 deliver_err_response(ent->recv_msg, IPMI_ERR_UNSPECIFIED);
2990 }
2991}
2992
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993int ipmi_unregister_smi(ipmi_smi_t intf)
2994{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 struct ipmi_smi_watcher *w;
Corey Minyard7ea0ed22014-11-06 16:58:48 -06002996 int intf_num = intf->intf_num;
2997 ipmi_user_t user;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Corey Minyard50c812b2006-03-26 01:37:21 -08002999 ipmi_bmc_unregister(intf);
3000
Corey Minyardb2c03942006-12-06 20:41:00 -08003001 mutex_lock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -08003002 mutex_lock(&ipmi_interfaces_mutex);
Corey Minyardb2c03942006-12-06 20:41:00 -08003003 intf->intf_num = -1;
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003004 intf->in_shutdown = true;
Corey Minyardbca03242006-12-06 20:40:57 -08003005 list_del_rcu(&intf->link);
3006 mutex_unlock(&ipmi_interfaces_mutex);
3007 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
Corey Minyardb2c03942006-12-06 20:41:00 -08003009 cleanup_smi_msgs(intf);
3010
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003011 /* Clean up the effects of users on the lower-level software. */
3012 mutex_lock(&ipmi_interfaces_mutex);
3013 rcu_read_lock();
3014 list_for_each_entry_rcu(user, &intf->users, link) {
3015 module_put(intf->handlers->owner);
3016 if (intf->handlers->dec_usecount)
3017 intf->handlers->dec_usecount(intf->send_info);
3018 }
3019 rcu_read_unlock();
3020 intf->handlers = NULL;
3021 mutex_unlock(&ipmi_interfaces_mutex);
3022
Corey Minyard393d2cc2005-11-07 00:59:54 -08003023 remove_proc_entries(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Corey Minyardc70d7492008-04-29 01:01:09 -07003025 /*
3026 * Call all the watcher interfaces to tell them that
3027 * an interface is gone.
3028 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08003029 list_for_each_entry(w, &smi_watchers, link)
Corey Minyardb2c03942006-12-06 20:41:00 -08003030 w->smi_gone(intf_num);
3031 mutex_unlock(&smi_watchers_mutex);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003032
Corey Minyard393d2cc2005-11-07 00:59:54 -08003033 kref_put(&intf->refcount, intf_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 return 0;
3035}
Corey Minyardc70d7492008-04-29 01:01:09 -07003036EXPORT_SYMBOL(ipmi_unregister_smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
3038static int handle_ipmb_get_msg_rsp(ipmi_smi_t intf,
3039 struct ipmi_smi_msg *msg)
3040{
3041 struct ipmi_ipmb_addr ipmb_addr;
3042 struct ipmi_recv_msg *recv_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Corey Minyardc70d7492008-04-29 01:01:09 -07003044 /*
3045 * This is 11, not 10, because the response must contain a
3046 * completion code.
3047 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 if (msg->rsp_size < 11) {
3049 /* Message not big enough, just ignore it. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003050 ipmi_inc_stat(intf, invalid_ipmb_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 return 0;
3052 }
3053
3054 if (msg->rsp[2] != 0) {
3055 /* An error getting the response, just ignore it. */
3056 return 0;
3057 }
3058
3059 ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
3060 ipmb_addr.slave_addr = msg->rsp[6];
3061 ipmb_addr.channel = msg->rsp[3] & 0x0f;
3062 ipmb_addr.lun = msg->rsp[7] & 3;
3063
Corey Minyardc70d7492008-04-29 01:01:09 -07003064 /*
3065 * It's a response from a remote entity. Look up the sequence
3066 * number and handle the response.
3067 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 if (intf_find_seq(intf,
3069 msg->rsp[7] >> 2,
3070 msg->rsp[3] & 0x0f,
3071 msg->rsp[8],
3072 (msg->rsp[4] >> 2) & (~1),
3073 (struct ipmi_addr *) &(ipmb_addr),
Corey Minyardc70d7492008-04-29 01:01:09 -07003074 &recv_msg)) {
3075 /*
3076 * We were unable to find the sequence number,
3077 * so just nuke the message.
3078 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003079 ipmi_inc_stat(intf, unhandled_ipmb_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 return 0;
3081 }
3082
3083 memcpy(recv_msg->msg_data,
3084 &(msg->rsp[9]),
3085 msg->rsp_size - 9);
Corey Minyardc70d7492008-04-29 01:01:09 -07003086 /*
3087 * The other fields matched, so no need to set them, except
3088 * for netfn, which needs to be the response that was
3089 * returned, not the request value.
3090 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3092 recv_msg->msg.data = recv_msg->msg_data;
3093 recv_msg->msg.data_len = msg->rsp_size - 10;
3094 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003095 ipmi_inc_stat(intf, handled_ipmb_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 deliver_response(recv_msg);
3097
3098 return 0;
3099}
3100
3101static int handle_ipmb_get_msg_cmd(ipmi_smi_t intf,
3102 struct ipmi_smi_msg *msg)
3103{
Corey Minyard393d2cc2005-11-07 00:59:54 -08003104 struct cmd_rcvr *rcvr;
3105 int rv = 0;
3106 unsigned char netfn;
3107 unsigned char cmd;
Corey Minyardc69c3122006-09-30 23:27:56 -07003108 unsigned char chan;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003109 ipmi_user_t user = NULL;
3110 struct ipmi_ipmb_addr *ipmb_addr;
3111 struct ipmi_recv_msg *recv_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
3113 if (msg->rsp_size < 10) {
3114 /* Message not big enough, just ignore it. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003115 ipmi_inc_stat(intf, invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 return 0;
3117 }
3118
3119 if (msg->rsp[2] != 0) {
3120 /* An error getting the response, just ignore it. */
3121 return 0;
3122 }
3123
3124 netfn = msg->rsp[4] >> 2;
3125 cmd = msg->rsp[8];
Corey Minyardc69c3122006-09-30 23:27:56 -07003126 chan = msg->rsp[3] & 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
Corey Minyarde61fb5b2005-11-07 01:00:05 -08003128 rcu_read_lock();
Corey Minyardc69c3122006-09-30 23:27:56 -07003129 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003130 if (rcvr) {
3131 user = rcvr->user;
3132 kref_get(&user->refcount);
3133 } else
3134 user = NULL;
Corey Minyarde61fb5b2005-11-07 01:00:05 -08003135 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
3137 if (user == NULL) {
3138 /* We didn't find a user, deliver an error response. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003139 ipmi_inc_stat(intf, unhandled_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
3141 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
3142 msg->data[1] = IPMI_SEND_MSG_CMD;
3143 msg->data[2] = msg->rsp[3];
3144 msg->data[3] = msg->rsp[6];
Corey Minyardc70d7492008-04-29 01:01:09 -07003145 msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 msg->data[5] = ipmb_checksum(&(msg->data[3]), 2);
Corey Minyardc14979b2005-09-06 15:18:38 -07003147 msg->data[6] = intf->channels[msg->rsp[3] & 0xf].address;
Corey Minyardc70d7492008-04-29 01:01:09 -07003148 /* rqseq/lun */
3149 msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 msg->data[8] = msg->rsp[8]; /* cmd */
3151 msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
3152 msg->data[10] = ipmb_checksum(&(msg->data[6]), 4);
3153 msg->data_size = 11;
3154
3155#ifdef DEBUG_MSGING
3156 {
3157 int m;
3158 printk("Invalid command:");
Corey Minyarde8b33612005-09-06 15:18:45 -07003159 for (m = 0; m < msg->data_size; m++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 printk(" %2.2x", msg->data[m]);
3161 printk("\n");
3162 }
3163#endif
Corey Minyardb2c03942006-12-06 20:41:00 -08003164 rcu_read_lock();
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003165 if (!intf->in_shutdown) {
3166 smi_send(intf, intf->handlers, msg, 0);
Corey Minyardc70d7492008-04-29 01:01:09 -07003167 /*
3168 * We used the message, so return the value
3169 * that causes it to not be freed or
3170 * queued.
3171 */
Corey Minyardb2c03942006-12-06 20:41:00 -08003172 rv = -1;
3173 }
3174 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 } else {
3176 /* Deliver the message to the user. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003177 ipmi_inc_stat(intf, handled_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
3179 recv_msg = ipmi_alloc_recv_msg();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003180 if (!recv_msg) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003181 /*
3182 * We couldn't allocate memory for the
3183 * message, so requeue it for handling
3184 * later.
3185 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 rv = 1;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003187 kref_put(&user->refcount, free_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 } else {
3189 /* Extract the source address from the data. */
3190 ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
3191 ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
3192 ipmb_addr->slave_addr = msg->rsp[6];
3193 ipmb_addr->lun = msg->rsp[7] & 3;
3194 ipmb_addr->channel = msg->rsp[3] & 0xf;
3195
Corey Minyardc70d7492008-04-29 01:01:09 -07003196 /*
3197 * Extract the rest of the message information
3198 * from the IPMB header.
3199 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 recv_msg->user = user;
3201 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3202 recv_msg->msgid = msg->rsp[7] >> 2;
3203 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3204 recv_msg->msg.cmd = msg->rsp[8];
3205 recv_msg->msg.data = recv_msg->msg_data;
3206
Corey Minyardc70d7492008-04-29 01:01:09 -07003207 /*
3208 * We chop off 10, not 9 bytes because the checksum
3209 * at the end also needs to be removed.
3210 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 recv_msg->msg.data_len = msg->rsp_size - 10;
3212 memcpy(recv_msg->msg_data,
3213 &(msg->rsp[9]),
3214 msg->rsp_size - 10);
3215 deliver_response(recv_msg);
3216 }
3217 }
3218
3219 return rv;
3220}
3221
3222static int handle_lan_get_msg_rsp(ipmi_smi_t intf,
3223 struct ipmi_smi_msg *msg)
3224{
3225 struct ipmi_lan_addr lan_addr;
3226 struct ipmi_recv_msg *recv_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
3228
Corey Minyardc70d7492008-04-29 01:01:09 -07003229 /*
3230 * This is 13, not 12, because the response must contain a
3231 * completion code.
3232 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 if (msg->rsp_size < 13) {
3234 /* Message not big enough, just ignore it. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003235 ipmi_inc_stat(intf, invalid_lan_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 return 0;
3237 }
3238
3239 if (msg->rsp[2] != 0) {
3240 /* An error getting the response, just ignore it. */
3241 return 0;
3242 }
3243
3244 lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
3245 lan_addr.session_handle = msg->rsp[4];
3246 lan_addr.remote_SWID = msg->rsp[8];
3247 lan_addr.local_SWID = msg->rsp[5];
3248 lan_addr.channel = msg->rsp[3] & 0x0f;
3249 lan_addr.privilege = msg->rsp[3] >> 4;
3250 lan_addr.lun = msg->rsp[9] & 3;
3251
Corey Minyardc70d7492008-04-29 01:01:09 -07003252 /*
3253 * It's a response from a remote entity. Look up the sequence
3254 * number and handle the response.
3255 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 if (intf_find_seq(intf,
3257 msg->rsp[9] >> 2,
3258 msg->rsp[3] & 0x0f,
3259 msg->rsp[10],
3260 (msg->rsp[6] >> 2) & (~1),
3261 (struct ipmi_addr *) &(lan_addr),
Corey Minyardc70d7492008-04-29 01:01:09 -07003262 &recv_msg)) {
3263 /*
3264 * We were unable to find the sequence number,
3265 * so just nuke the message.
3266 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003267 ipmi_inc_stat(intf, unhandled_lan_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 return 0;
3269 }
3270
3271 memcpy(recv_msg->msg_data,
3272 &(msg->rsp[11]),
3273 msg->rsp_size - 11);
Corey Minyardc70d7492008-04-29 01:01:09 -07003274 /*
3275 * The other fields matched, so no need to set them, except
3276 * for netfn, which needs to be the response that was
3277 * returned, not the request value.
3278 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3280 recv_msg->msg.data = recv_msg->msg_data;
3281 recv_msg->msg.data_len = msg->rsp_size - 12;
3282 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003283 ipmi_inc_stat(intf, handled_lan_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 deliver_response(recv_msg);
3285
3286 return 0;
3287}
3288
3289static int handle_lan_get_msg_cmd(ipmi_smi_t intf,
3290 struct ipmi_smi_msg *msg)
3291{
Corey Minyard393d2cc2005-11-07 00:59:54 -08003292 struct cmd_rcvr *rcvr;
3293 int rv = 0;
3294 unsigned char netfn;
3295 unsigned char cmd;
Corey Minyardc69c3122006-09-30 23:27:56 -07003296 unsigned char chan;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003297 ipmi_user_t user = NULL;
3298 struct ipmi_lan_addr *lan_addr;
3299 struct ipmi_recv_msg *recv_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300
3301 if (msg->rsp_size < 12) {
3302 /* Message not big enough, just ignore it. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003303 ipmi_inc_stat(intf, invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 return 0;
3305 }
3306
3307 if (msg->rsp[2] != 0) {
3308 /* An error getting the response, just ignore it. */
3309 return 0;
3310 }
3311
3312 netfn = msg->rsp[6] >> 2;
3313 cmd = msg->rsp[10];
Corey Minyardc69c3122006-09-30 23:27:56 -07003314 chan = msg->rsp[3] & 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
Corey Minyarde61fb5b2005-11-07 01:00:05 -08003316 rcu_read_lock();
Corey Minyardc69c3122006-09-30 23:27:56 -07003317 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003318 if (rcvr) {
3319 user = rcvr->user;
3320 kref_get(&user->refcount);
3321 } else
3322 user = NULL;
Corey Minyarde61fb5b2005-11-07 01:00:05 -08003323 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324
3325 if (user == NULL) {
Corey Minyard393d2cc2005-11-07 00:59:54 -08003326 /* We didn't find a user, just give up. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003327 ipmi_inc_stat(intf, unhandled_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
Corey Minyardc70d7492008-04-29 01:01:09 -07003329 /*
3330 * Don't do anything with these messages, just allow
3331 * them to be freed.
3332 */
3333 rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 } else {
3335 /* Deliver the message to the user. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003336 ipmi_inc_stat(intf, handled_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
3338 recv_msg = ipmi_alloc_recv_msg();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003339 if (!recv_msg) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003340 /*
3341 * We couldn't allocate memory for the
3342 * message, so requeue it for handling later.
3343 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 rv = 1;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003345 kref_put(&user->refcount, free_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 } else {
3347 /* Extract the source address from the data. */
3348 lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
3349 lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
3350 lan_addr->session_handle = msg->rsp[4];
3351 lan_addr->remote_SWID = msg->rsp[8];
3352 lan_addr->local_SWID = msg->rsp[5];
3353 lan_addr->lun = msg->rsp[9] & 3;
3354 lan_addr->channel = msg->rsp[3] & 0xf;
3355 lan_addr->privilege = msg->rsp[3] >> 4;
3356
Corey Minyardc70d7492008-04-29 01:01:09 -07003357 /*
3358 * Extract the rest of the message information
3359 * from the IPMB header.
3360 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 recv_msg->user = user;
3362 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3363 recv_msg->msgid = msg->rsp[9] >> 2;
3364 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3365 recv_msg->msg.cmd = msg->rsp[10];
3366 recv_msg->msg.data = recv_msg->msg_data;
3367
Corey Minyardc70d7492008-04-29 01:01:09 -07003368 /*
3369 * We chop off 12, not 11 bytes because the checksum
3370 * at the end also needs to be removed.
3371 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 recv_msg->msg.data_len = msg->rsp_size - 12;
3373 memcpy(recv_msg->msg_data,
3374 &(msg->rsp[11]),
3375 msg->rsp_size - 12);
3376 deliver_response(recv_msg);
3377 }
3378 }
3379
3380 return rv;
3381}
3382
dann frazier4dec3022009-04-21 12:24:05 -07003383/*
3384 * This routine will handle "Get Message" command responses with
3385 * channels that use an OEM Medium. The message format belongs to
3386 * the OEM. See IPMI 2.0 specification, Chapter 6 and
3387 * Chapter 22, sections 22.6 and 22.24 for more details.
3388 */
3389static int handle_oem_get_msg_cmd(ipmi_smi_t intf,
3390 struct ipmi_smi_msg *msg)
3391{
3392 struct cmd_rcvr *rcvr;
3393 int rv = 0;
3394 unsigned char netfn;
3395 unsigned char cmd;
3396 unsigned char chan;
3397 ipmi_user_t user = NULL;
3398 struct ipmi_system_interface_addr *smi_addr;
3399 struct ipmi_recv_msg *recv_msg;
3400
3401 /*
3402 * We expect the OEM SW to perform error checking
3403 * so we just do some basic sanity checks
3404 */
3405 if (msg->rsp_size < 4) {
3406 /* Message not big enough, just ignore it. */
3407 ipmi_inc_stat(intf, invalid_commands);
3408 return 0;
3409 }
3410
3411 if (msg->rsp[2] != 0) {
3412 /* An error getting the response, just ignore it. */
3413 return 0;
3414 }
3415
3416 /*
3417 * This is an OEM Message so the OEM needs to know how
3418 * handle the message. We do no interpretation.
3419 */
3420 netfn = msg->rsp[0] >> 2;
3421 cmd = msg->rsp[1];
3422 chan = msg->rsp[3] & 0xf;
3423
3424 rcu_read_lock();
3425 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3426 if (rcvr) {
3427 user = rcvr->user;
3428 kref_get(&user->refcount);
3429 } else
3430 user = NULL;
3431 rcu_read_unlock();
3432
3433 if (user == NULL) {
3434 /* We didn't find a user, just give up. */
3435 ipmi_inc_stat(intf, unhandled_commands);
3436
3437 /*
3438 * Don't do anything with these messages, just allow
3439 * them to be freed.
3440 */
3441
3442 rv = 0;
3443 } else {
3444 /* Deliver the message to the user. */
3445 ipmi_inc_stat(intf, handled_commands);
3446
3447 recv_msg = ipmi_alloc_recv_msg();
3448 if (!recv_msg) {
3449 /*
3450 * We couldn't allocate memory for the
3451 * message, so requeue it for handling
3452 * later.
3453 */
3454 rv = 1;
3455 kref_put(&user->refcount, free_user);
3456 } else {
3457 /*
3458 * OEM Messages are expected to be delivered via
3459 * the system interface to SMS software. We might
3460 * need to visit this again depending on OEM
3461 * requirements
3462 */
3463 smi_addr = ((struct ipmi_system_interface_addr *)
3464 &(recv_msg->addr));
3465 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3466 smi_addr->channel = IPMI_BMC_CHANNEL;
3467 smi_addr->lun = msg->rsp[0] & 3;
3468
3469 recv_msg->user = user;
3470 recv_msg->user_msg_data = NULL;
3471 recv_msg->recv_type = IPMI_OEM_RECV_TYPE;
3472 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3473 recv_msg->msg.cmd = msg->rsp[1];
3474 recv_msg->msg.data = recv_msg->msg_data;
3475
3476 /*
3477 * The message starts at byte 4 which follows the
3478 * the Channel Byte in the "GET MESSAGE" command
3479 */
3480 recv_msg->msg.data_len = msg->rsp_size - 4;
3481 memcpy(recv_msg->msg_data,
3482 &(msg->rsp[4]),
3483 msg->rsp_size - 4);
3484 deliver_response(recv_msg);
3485 }
3486 }
3487
3488 return rv;
3489}
3490
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
3492 struct ipmi_smi_msg *msg)
3493{
3494 struct ipmi_system_interface_addr *smi_addr;
Corey Minyardc70d7492008-04-29 01:01:09 -07003495
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 recv_msg->msgid = 0;
3497 smi_addr = (struct ipmi_system_interface_addr *) &(recv_msg->addr);
3498 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3499 smi_addr->channel = IPMI_BMC_CHANNEL;
3500 smi_addr->lun = msg->rsp[0] & 3;
3501 recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
3502 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3503 recv_msg->msg.cmd = msg->rsp[1];
3504 memcpy(recv_msg->msg_data, &(msg->rsp[3]), msg->rsp_size - 3);
3505 recv_msg->msg.data = recv_msg->msg_data;
3506 recv_msg->msg.data_len = msg->rsp_size - 3;
3507}
3508
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509static int handle_read_event_rsp(ipmi_smi_t intf,
3510 struct ipmi_smi_msg *msg)
3511{
3512 struct ipmi_recv_msg *recv_msg, *recv_msg2;
3513 struct list_head msgs;
3514 ipmi_user_t user;
3515 int rv = 0;
3516 int deliver_count = 0;
3517 unsigned long flags;
3518
3519 if (msg->rsp_size < 19) {
3520 /* Message is too small to be an IPMB event. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003521 ipmi_inc_stat(intf, invalid_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 return 0;
3523 }
3524
3525 if (msg->rsp[2] != 0) {
3526 /* An error getting the event, just ignore it. */
3527 return 0;
3528 }
3529
3530 INIT_LIST_HEAD(&msgs);
3531
Corey Minyard393d2cc2005-11-07 00:59:54 -08003532 spin_lock_irqsave(&intf->events_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003534 ipmi_inc_stat(intf, events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
Corey Minyardc70d7492008-04-29 01:01:09 -07003536 /*
3537 * Allocate and fill in one message for every user that is
3538 * getting events.
3539 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08003540 rcu_read_lock();
3541 list_for_each_entry_rcu(user, &intf->users, link) {
Corey Minyard8a3628d2006-03-31 02:30:40 -08003542 if (!user->gets_events)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 continue;
3544
3545 recv_msg = ipmi_alloc_recv_msg();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003546 if (!recv_msg) {
Corey Minyard393d2cc2005-11-07 00:59:54 -08003547 rcu_read_unlock();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003548 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs,
3549 link) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 list_del(&recv_msg->link);
3551 ipmi_free_recv_msg(recv_msg);
3552 }
Corey Minyardc70d7492008-04-29 01:01:09 -07003553 /*
3554 * We couldn't allocate memory for the
3555 * message, so requeue it for handling
3556 * later.
3557 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 rv = 1;
3559 goto out;
3560 }
3561
3562 deliver_count++;
3563
3564 copy_event_into_recv_msg(recv_msg, msg);
3565 recv_msg->user = user;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003566 kref_get(&user->refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 list_add_tail(&(recv_msg->link), &msgs);
3568 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08003569 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570
3571 if (deliver_count) {
3572 /* Now deliver all the messages. */
3573 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
3574 list_del(&recv_msg->link);
3575 deliver_response(recv_msg);
3576 }
3577 } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003578 /*
3579 * No one to receive the message, put it in queue if there's
3580 * not already too many things in the queue.
3581 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 recv_msg = ipmi_alloc_recv_msg();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003583 if (!recv_msg) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003584 /*
3585 * We couldn't allocate memory for the
3586 * message, so requeue it for handling
3587 * later.
3588 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 rv = 1;
3590 goto out;
3591 }
3592
3593 copy_event_into_recv_msg(recv_msg, msg);
3594 list_add_tail(&(recv_msg->link), &(intf->waiting_events));
Corey Minyard4791c032006-04-10 22:54:31 -07003595 intf->waiting_events_count++;
Corey Minyard87ebd062008-04-29 01:01:04 -07003596 } else if (!intf->event_msg_printed) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003597 /*
3598 * There's too many things in the queue, discard this
3599 * message.
3600 */
Corey Minyard87ebd062008-04-29 01:01:04 -07003601 printk(KERN_WARNING PFX "Event queue full, discarding"
3602 " incoming events\n");
3603 intf->event_msg_printed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 }
3605
3606 out:
3607 spin_unlock_irqrestore(&(intf->events_lock), flags);
3608
3609 return rv;
3610}
3611
3612static int handle_bmc_rsp(ipmi_smi_t intf,
3613 struct ipmi_smi_msg *msg)
3614{
3615 struct ipmi_recv_msg *recv_msg;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003616 struct ipmi_user *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
3618 recv_msg = (struct ipmi_recv_msg *) msg->user_data;
Corey Minyardc70d7492008-04-29 01:01:09 -07003619 if (recv_msg == NULL) {
3620 printk(KERN_WARNING
3621 "IPMI message received with no owner. This\n"
3622 "could be because of a malformed message, or\n"
3623 "because of a hardware error. Contact your\n"
3624 "hardware vender for assistance\n");
Corey Minyard56a55ec2005-09-06 15:18:42 -07003625 return 0;
3626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
Corey Minyard393d2cc2005-11-07 00:59:54 -08003628 user = recv_msg->user;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 /* Make sure the user still exists. */
Corey Minyard393d2cc2005-11-07 00:59:54 -08003630 if (user && !user->valid) {
Corey Minyard56a55ec2005-09-06 15:18:42 -07003631 /* The user for the message went away, so give up. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003632 ipmi_inc_stat(intf, unhandled_local_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 ipmi_free_recv_msg(recv_msg);
3634 } else {
3635 struct ipmi_system_interface_addr *smi_addr;
3636
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003637 ipmi_inc_stat(intf, handled_local_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3639 recv_msg->msgid = msg->msgid;
3640 smi_addr = ((struct ipmi_system_interface_addr *)
3641 &(recv_msg->addr));
3642 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3643 smi_addr->channel = IPMI_BMC_CHANNEL;
3644 smi_addr->lun = msg->rsp[0] & 3;
3645 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3646 recv_msg->msg.cmd = msg->rsp[1];
3647 memcpy(recv_msg->msg_data,
3648 &(msg->rsp[2]),
3649 msg->rsp_size - 2);
3650 recv_msg->msg.data = recv_msg->msg_data;
3651 recv_msg->msg.data_len = msg->rsp_size - 2;
3652 deliver_response(recv_msg);
3653 }
3654
3655 return 0;
3656}
3657
Corey Minyardc70d7492008-04-29 01:01:09 -07003658/*
Corey Minyard7adf5792012-03-28 14:42:49 -07003659 * Handle a received message. Return 1 if the message should be requeued,
Corey Minyardc70d7492008-04-29 01:01:09 -07003660 * 0 if the message should be freed, or -1 if the message should not
3661 * be freed or requeued.
3662 */
Corey Minyard7adf5792012-03-28 14:42:49 -07003663static int handle_one_recv_msg(ipmi_smi_t intf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 struct ipmi_smi_msg *msg)
3665{
3666 int requeue;
3667 int chan;
3668
3669#ifdef DEBUG_MSGING
3670 int m;
3671 printk("Recv:");
Corey Minyarde8b33612005-09-06 15:18:45 -07003672 for (m = 0; m < msg->rsp_size; m++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 printk(" %2.2x", msg->rsp[m]);
3674 printk("\n");
3675#endif
3676 if (msg->rsp_size < 2) {
3677 /* Message is too small to be correct. */
3678 printk(KERN_WARNING PFX "BMC returned to small a message"
3679 " for netfn %x cmd %x, got %d bytes\n",
3680 (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size);
3681
3682 /* Generate an error response for the message. */
3683 msg->rsp[0] = msg->data[0] | (1 << 2);
3684 msg->rsp[1] = msg->data[1];
3685 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
3686 msg->rsp_size = 3;
Corey Minyardc70d7492008-04-29 01:01:09 -07003687 } else if (((msg->rsp[0] >> 2) != ((msg->data[0] >> 2) | 1))
3688 || (msg->rsp[1] != msg->data[1])) {
3689 /*
3690 * The NetFN and Command in the response is not even
3691 * marginally correct.
3692 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 printk(KERN_WARNING PFX "BMC returned incorrect response,"
3694 " expected netfn %x cmd %x, got netfn %x cmd %x\n",
3695 (msg->data[0] >> 2) | 1, msg->data[1],
3696 msg->rsp[0] >> 2, msg->rsp[1]);
3697
3698 /* Generate an error response for the message. */
3699 msg->rsp[0] = msg->data[0] | (1 << 2);
3700 msg->rsp[1] = msg->data[1];
3701 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
3702 msg->rsp_size = 3;
3703 }
3704
3705 if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
3706 && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07003707 && (msg->user_data != NULL)) {
3708 /*
3709 * It's a response to a response we sent. For this we
3710 * deliver a send message response to the user.
3711 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08003712 struct ipmi_recv_msg *recv_msg = msg->user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713
3714 requeue = 0;
3715 if (msg->rsp_size < 2)
3716 /* Message is too small to be correct. */
3717 goto out;
3718
3719 chan = msg->data[2] & 0x0f;
3720 if (chan >= IPMI_MAX_CHANNELS)
3721 /* Invalid channel number */
3722 goto out;
3723
Corey Minyard393d2cc2005-11-07 00:59:54 -08003724 if (!recv_msg)
3725 goto out;
3726
3727 /* Make sure the user still exists. */
3728 if (!recv_msg->user || !recv_msg->user->valid)
3729 goto out;
3730
3731 recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
3732 recv_msg->msg.data = recv_msg->msg_data;
3733 recv_msg->msg.data_len = 1;
3734 recv_msg->msg_data[0] = msg->rsp[2];
3735 deliver_response(recv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
Corey Minyardc70d7492008-04-29 01:01:09 -07003737 && (msg->rsp[1] == IPMI_GET_MSG_CMD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 /* It's from the receive queue. */
3739 chan = msg->rsp[3] & 0xf;
3740 if (chan >= IPMI_MAX_CHANNELS) {
3741 /* Invalid channel number */
3742 requeue = 0;
3743 goto out;
3744 }
3745
dann frazier4dec3022009-04-21 12:24:05 -07003746 /*
Corey Minyard9a2845c2009-05-20 13:36:17 -05003747 * We need to make sure the channels have been initialized.
3748 * The channel_handler routine will set the "curr_channel"
3749 * equal to or greater than IPMI_MAX_CHANNELS when all the
3750 * channels for this interface have been initialized.
3751 */
dann frazier4dec3022009-04-21 12:24:05 -07003752 if (intf->curr_channel < IPMI_MAX_CHANNELS) {
Corey Minyard9a2845c2009-05-20 13:36:17 -05003753 requeue = 0; /* Throw the message away */
dann frazier4dec3022009-04-21 12:24:05 -07003754 goto out;
3755 }
3756
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 switch (intf->channels[chan].medium) {
3758 case IPMI_CHANNEL_MEDIUM_IPMB:
3759 if (msg->rsp[4] & 0x04) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003760 /*
3761 * It's a response, so find the
3762 * requesting message and send it up.
3763 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 requeue = handle_ipmb_get_msg_rsp(intf, msg);
3765 } else {
Corey Minyardc70d7492008-04-29 01:01:09 -07003766 /*
3767 * It's a command to the SMS from some other
3768 * entity. Handle that.
3769 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 requeue = handle_ipmb_get_msg_cmd(intf, msg);
3771 }
3772 break;
3773
3774 case IPMI_CHANNEL_MEDIUM_8023LAN:
3775 case IPMI_CHANNEL_MEDIUM_ASYNC:
3776 if (msg->rsp[6] & 0x04) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003777 /*
3778 * It's a response, so find the
3779 * requesting message and send it up.
3780 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 requeue = handle_lan_get_msg_rsp(intf, msg);
3782 } else {
Corey Minyardc70d7492008-04-29 01:01:09 -07003783 /*
3784 * It's a command to the SMS from some other
3785 * entity. Handle that.
3786 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 requeue = handle_lan_get_msg_cmd(intf, msg);
3788 }
3789 break;
3790
3791 default:
dann frazier4dec3022009-04-21 12:24:05 -07003792 /* Check for OEM Channels. Clients had better
3793 register for these commands. */
3794 if ((intf->channels[chan].medium
3795 >= IPMI_CHANNEL_MEDIUM_OEM_MIN)
3796 && (intf->channels[chan].medium
3797 <= IPMI_CHANNEL_MEDIUM_OEM_MAX)) {
3798 requeue = handle_oem_get_msg_cmd(intf, msg);
3799 } else {
3800 /*
3801 * We don't handle the channel type, so just
3802 * free the message.
3803 */
3804 requeue = 0;
3805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 }
3807
3808 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
Corey Minyardc70d7492008-04-29 01:01:09 -07003809 && (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD)) {
Adam Buchbinderb3834be2012-09-19 21:48:02 -04003810 /* It's an asynchronous event. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 requeue = handle_read_event_rsp(intf, msg);
3812 } else {
3813 /* It's a response from the local BMC. */
3814 requeue = handle_bmc_rsp(intf, msg);
3815 }
3816
3817 out:
3818 return requeue;
3819}
3820
Corey Minyard7adf5792012-03-28 14:42:49 -07003821/*
3822 * If there are messages in the queue or pretimeouts, handle them.
3823 */
3824static void handle_new_recv_msgs(ipmi_smi_t intf)
3825{
3826 struct ipmi_smi_msg *smi_msg;
3827 unsigned long flags = 0;
3828 int rv;
3829 int run_to_completion = intf->run_to_completion;
3830
3831 /* See if any waiting messages need to be processed. */
3832 if (!run_to_completion)
Corey Minyard65be7542014-11-06 20:48:02 -06003833 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
3834 while (!list_empty(&intf->waiting_rcv_msgs)) {
3835 smi_msg = list_entry(intf->waiting_rcv_msgs.next,
Corey Minyard7adf5792012-03-28 14:42:49 -07003836 struct ipmi_smi_msg, link);
Corey Minyard7adf5792012-03-28 14:42:49 -07003837 if (!run_to_completion)
Corey Minyard65be7542014-11-06 20:48:02 -06003838 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
3839 flags);
Corey Minyard7adf5792012-03-28 14:42:49 -07003840 rv = handle_one_recv_msg(intf, smi_msg);
3841 if (!run_to_completion)
Corey Minyard65be7542014-11-06 20:48:02 -06003842 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003843 if (rv > 0) {
Corey Minyard7adf5792012-03-28 14:42:49 -07003844 /*
3845 * To preserve message order, quit if we
3846 * can't handle a message.
3847 */
Corey Minyard7adf5792012-03-28 14:42:49 -07003848 break;
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003849 } else {
3850 list_del(&smi_msg->link);
3851 if (rv == 0)
3852 /* Message handled */
3853 ipmi_free_smi_msg(smi_msg);
3854 /* If rv < 0, fatal error, del but don't free. */
Corey Minyard7adf5792012-03-28 14:42:49 -07003855 }
3856 }
3857 if (!run_to_completion)
Corey Minyard65be7542014-11-06 20:48:02 -06003858 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock, flags);
Corey Minyard7adf5792012-03-28 14:42:49 -07003859
3860 /*
3861 * If the pretimout count is non-zero, decrement one from it and
3862 * deliver pretimeouts to all the users.
3863 */
3864 if (atomic_add_unless(&intf->watchdog_pretimeouts_to_deliver, -1, 0)) {
3865 ipmi_user_t user;
3866
3867 rcu_read_lock();
3868 list_for_each_entry_rcu(user, &intf->users, link) {
3869 if (user->handler->ipmi_watchdog_pretimeout)
3870 user->handler->ipmi_watchdog_pretimeout(
3871 user->handler_data);
3872 }
3873 rcu_read_unlock();
3874 }
3875}
3876
3877static void smi_recv_tasklet(unsigned long val)
3878{
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003879 unsigned long flags = 0; /* keep us warning-free. */
3880 ipmi_smi_t intf = (ipmi_smi_t) val;
3881 int run_to_completion = intf->run_to_completion;
3882 struct ipmi_smi_msg *newmsg = NULL;
3883
3884 /*
3885 * Start the next message if available.
3886 *
3887 * Do this here, not in the actual receiver, because we may deadlock
3888 * because the lower layer is allowed to hold locks while calling
3889 * message delivery.
3890 */
3891 if (!run_to_completion)
3892 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
3893 if (intf->curr_msg == NULL && !intf->in_shutdown) {
3894 struct list_head *entry = NULL;
3895
3896 /* Pick the high priority queue first. */
3897 if (!list_empty(&intf->hp_xmit_msgs))
3898 entry = intf->hp_xmit_msgs.next;
3899 else if (!list_empty(&intf->xmit_msgs))
3900 entry = intf->xmit_msgs.next;
3901
3902 if (entry) {
3903 list_del(entry);
3904 newmsg = list_entry(entry, struct ipmi_smi_msg, link);
3905 intf->curr_msg = newmsg;
3906 }
3907 }
3908 if (!run_to_completion)
3909 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
3910 if (newmsg)
Corey Minyard99ab32f2014-11-07 07:57:31 -06003911 intf->handlers->sender(intf->send_info, newmsg);
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003912
3913 handle_new_recv_msgs(intf);
Corey Minyard7adf5792012-03-28 14:42:49 -07003914}
3915
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916/* Handle a new message from the lower layer. */
3917void ipmi_smi_msg_received(ipmi_smi_t intf,
3918 struct ipmi_smi_msg *msg)
3919{
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003920 unsigned long flags = 0; /* keep us warning-free. */
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003921 int run_to_completion = intf->run_to_completion;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 if ((msg->data_size >= 2)
3924 && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
3925 && (msg->data[1] == IPMI_SEND_MSG_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07003926 && (msg->user_data == NULL)) {
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003927
3928 if (intf->in_shutdown)
3929 goto free_msg;
3930
Corey Minyardc70d7492008-04-29 01:01:09 -07003931 /*
3932 * This is the local response to a command send, start
3933 * the timer for these. The user_data will not be
3934 * NULL if this is a response send, and we will let
3935 * response sends just go through.
3936 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937
Corey Minyardc70d7492008-04-29 01:01:09 -07003938 /*
3939 * Check for errors, if we get certain errors (ones
3940 * that mean basically we can try again later), we
3941 * ignore them and start the timer. Otherwise we
3942 * report the error immediately.
3943 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
3945 && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
Corey Minyard46d52b02006-11-08 17:44:55 -08003946 && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR)
3947 && (msg->rsp[2] != IPMI_BUS_ERR)
Corey Minyardc70d7492008-04-29 01:01:09 -07003948 && (msg->rsp[2] != IPMI_NAK_ON_WRITE_ERR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 int chan = msg->rsp[3] & 0xf;
3950
3951 /* Got an error sending the message, handle it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 if (chan >= IPMI_MAX_CHANNELS)
3953 ; /* This shouldn't happen */
3954 else if ((intf->channels[chan].medium
3955 == IPMI_CHANNEL_MEDIUM_8023LAN)
3956 || (intf->channels[chan].medium
3957 == IPMI_CHANNEL_MEDIUM_ASYNC))
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003958 ipmi_inc_stat(intf, sent_lan_command_errs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 else
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003960 ipmi_inc_stat(intf, sent_ipmb_command_errs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 intf_err_seq(intf, msg->msgid, msg->rsp[2]);
Corey Minyardc70d7492008-04-29 01:01:09 -07003962 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 /* The message was sent, start the timer. */
3964 intf_start_seq_timer(intf, msg->msgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003966free_msg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 ipmi_free_smi_msg(msg);
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003968 } else {
3969 /*
3970 * To preserve message order, we keep a queue and deliver from
3971 * a tasklet.
3972 */
3973 if (!run_to_completion)
3974 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
3975 list_add_tail(&msg->link, &intf->waiting_rcv_msgs);
3976 if (!run_to_completion)
3977 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
3978 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 }
3980
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003981 if (!run_to_completion)
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003982 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
3983 if (msg == intf->curr_msg)
3984 intf->curr_msg = NULL;
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003985 if (!run_to_completion)
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003986 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
Corey Minyardc70d7492008-04-29 01:01:09 -07003987
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003988 if (run_to_completion)
3989 smi_recv_tasklet((unsigned long) intf);
3990 else
3991 tasklet_schedule(&intf->recv_tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992}
Corey Minyardc70d7492008-04-29 01:01:09 -07003993EXPORT_SYMBOL(ipmi_smi_msg_received);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
3995void ipmi_smi_watchdog_pretimeout(ipmi_smi_t intf)
3996{
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003997 if (intf->in_shutdown)
3998 return;
3999
Corey Minyard7adf5792012-03-28 14:42:49 -07004000 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 1);
4001 tasklet_schedule(&intf->recv_tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002}
Corey Minyardc70d7492008-04-29 01:01:09 -07004003EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004
Corey Minyard882fe012005-05-01 08:59:12 -07004005static struct ipmi_smi_msg *
4006smi_from_recv_msg(ipmi_smi_t intf, struct ipmi_recv_msg *recv_msg,
4007 unsigned char seq, long seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008{
Corey Minyard882fe012005-05-01 08:59:12 -07004009 struct ipmi_smi_msg *smi_msg = ipmi_alloc_smi_msg();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 if (!smi_msg)
Corey Minyardc70d7492008-04-29 01:01:09 -07004011 /*
4012 * If we can't allocate the message, then just return, we
4013 * get 4 retries, so this should be ok.
4014 */
Corey Minyard882fe012005-05-01 08:59:12 -07004015 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016
4017 memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
4018 smi_msg->data_size = recv_msg->msg.data_len;
4019 smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
Corey Minyardc70d7492008-04-29 01:01:09 -07004020
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021#ifdef DEBUG_MSGING
4022 {
4023 int m;
4024 printk("Resend: ");
Corey Minyarde8b33612005-09-06 15:18:45 -07004025 for (m = 0; m < smi_msg->data_size; m++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 printk(" %2.2x", smi_msg->data[m]);
4027 printk("\n");
4028 }
4029#endif
Corey Minyard882fe012005-05-01 08:59:12 -07004030 return smi_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031}
4032
Corey Minyard393d2cc2005-11-07 00:59:54 -08004033static void check_msg_timeout(ipmi_smi_t intf, struct seq_table *ent,
4034 struct list_head *timeouts, long timeout_period,
Corey Minyard89986492014-04-14 09:46:54 -05004035 int slot, unsigned long *flags,
4036 unsigned int *waiting_msgs)
Corey Minyard393d2cc2005-11-07 00:59:54 -08004037{
Corey Minyardb2c03942006-12-06 20:41:00 -08004038 struct ipmi_recv_msg *msg;
4039 struct ipmi_smi_handlers *handlers;
4040
Corey Minyard7ea0ed22014-11-06 16:58:48 -06004041 if (intf->in_shutdown)
Corey Minyardb2c03942006-12-06 20:41:00 -08004042 return;
Corey Minyard393d2cc2005-11-07 00:59:54 -08004043
4044 if (!ent->inuse)
4045 return;
4046
4047 ent->timeout -= timeout_period;
Corey Minyard89986492014-04-14 09:46:54 -05004048 if (ent->timeout > 0) {
4049 (*waiting_msgs)++;
Corey Minyard393d2cc2005-11-07 00:59:54 -08004050 return;
Corey Minyard89986492014-04-14 09:46:54 -05004051 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08004052
4053 if (ent->retries_left == 0) {
4054 /* The message has used all its retries. */
4055 ent->inuse = 0;
4056 msg = ent->recv_msg;
4057 list_add_tail(&msg->link, timeouts);
Corey Minyard393d2cc2005-11-07 00:59:54 -08004058 if (ent->broadcast)
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07004059 ipmi_inc_stat(intf, timed_out_ipmb_broadcasts);
Corey Minyard25176ed2009-04-21 12:24:04 -07004060 else if (is_lan_addr(&ent->recv_msg->addr))
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07004061 ipmi_inc_stat(intf, timed_out_lan_commands);
Corey Minyard393d2cc2005-11-07 00:59:54 -08004062 else
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07004063 ipmi_inc_stat(intf, timed_out_ipmb_commands);
Corey Minyard393d2cc2005-11-07 00:59:54 -08004064 } else {
4065 struct ipmi_smi_msg *smi_msg;
4066 /* More retries, send again. */
4067
Corey Minyard89986492014-04-14 09:46:54 -05004068 (*waiting_msgs)++;
4069
Corey Minyardc70d7492008-04-29 01:01:09 -07004070 /*
4071 * Start with the max timer, set to normal timer after
4072 * the message is sent.
4073 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08004074 ent->timeout = MAX_MSG_TIMEOUT;
4075 ent->retries_left--;
Corey Minyard393d2cc2005-11-07 00:59:54 -08004076 smi_msg = smi_from_recv_msg(intf, ent->recv_msg, slot,
4077 ent->seqid);
Corey Minyard25176ed2009-04-21 12:24:04 -07004078 if (!smi_msg) {
4079 if (is_lan_addr(&ent->recv_msg->addr))
4080 ipmi_inc_stat(intf,
4081 dropped_rexmit_lan_commands);
4082 else
4083 ipmi_inc_stat(intf,
4084 dropped_rexmit_ipmb_commands);
Corey Minyard393d2cc2005-11-07 00:59:54 -08004085 return;
Corey Minyard25176ed2009-04-21 12:24:04 -07004086 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08004087
4088 spin_unlock_irqrestore(&intf->seq_lock, *flags);
Corey Minyardb2c03942006-12-06 20:41:00 -08004089
Corey Minyardc70d7492008-04-29 01:01:09 -07004090 /*
4091 * Send the new message. We send with a zero
4092 * priority. It timed out, I doubt time is that
4093 * critical now, and high priority messages are really
4094 * only for messages to the local MC, which don't get
4095 * resent.
4096 */
Corey Minyardb2c03942006-12-06 20:41:00 -08004097 handlers = intf->handlers;
Corey Minyard25176ed2009-04-21 12:24:04 -07004098 if (handlers) {
4099 if (is_lan_addr(&ent->recv_msg->addr))
4100 ipmi_inc_stat(intf,
4101 retransmitted_lan_commands);
4102 else
4103 ipmi_inc_stat(intf,
4104 retransmitted_ipmb_commands);
4105
Corey Minyard7f4a1c82014-11-06 20:52:24 -06004106 smi_send(intf, intf->handlers, smi_msg, 0);
Corey Minyard25176ed2009-04-21 12:24:04 -07004107 } else
Corey Minyardb2c03942006-12-06 20:41:00 -08004108 ipmi_free_smi_msg(smi_msg);
4109
Corey Minyard393d2cc2005-11-07 00:59:54 -08004110 spin_lock_irqsave(&intf->seq_lock, *flags);
4111 }
4112}
4113
Corey Minyard89986492014-04-14 09:46:54 -05004114static unsigned int ipmi_timeout_handler(ipmi_smi_t intf, long timeout_period)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 struct list_head timeouts;
4117 struct ipmi_recv_msg *msg, *msg2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 unsigned long flags;
Corey Minyardbca03242006-12-06 20:40:57 -08004119 int i;
Corey Minyard89986492014-04-14 09:46:54 -05004120 unsigned int waiting_msgs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
Corey Minyard89986492014-04-14 09:46:54 -05004122 /*
4123 * Go through the seq table and find any messages that
4124 * have timed out, putting them in the timeouts
4125 * list.
4126 */
4127 INIT_LIST_HEAD(&timeouts);
4128 spin_lock_irqsave(&intf->seq_lock, flags);
4129 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++)
4130 check_msg_timeout(intf, &(intf->seq_table[i]),
4131 &timeouts, timeout_period, i,
4132 &flags, &waiting_msgs);
4133 spin_unlock_irqrestore(&intf->seq_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134
Corey Minyard89986492014-04-14 09:46:54 -05004135 list_for_each_entry_safe(msg, msg2, &timeouts, link)
4136 deliver_err_response(msg, IPMI_TIMEOUT_COMPLETION_CODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137
Corey Minyard89986492014-04-14 09:46:54 -05004138 /*
4139 * Maintenance mode handling. Check the timeout
4140 * optimistically before we claim the lock. It may
4141 * mean a timeout gets missed occasionally, but that
4142 * only means the timeout gets extended by one period
4143 * in that case. No big deal, and it avoids the lock
4144 * most of the time.
4145 */
4146 if (intf->auto_maintenance_timeout > 0) {
4147 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
Corey Minyardb9675132006-12-06 20:41:02 -08004148 if (intf->auto_maintenance_timeout > 0) {
Corey Minyard89986492014-04-14 09:46:54 -05004149 intf->auto_maintenance_timeout
4150 -= timeout_period;
4151 if (!intf->maintenance_mode
4152 && (intf->auto_maintenance_timeout <= 0)) {
Corey Minyard7aefac22014-04-14 09:46:56 -05004153 intf->maintenance_mode_enable = false;
Corey Minyard89986492014-04-14 09:46:54 -05004154 maintenance_mode_update(intf);
Corey Minyardb9675132006-12-06 20:41:02 -08004155 }
Corey Minyardb9675132006-12-06 20:41:02 -08004156 }
Corey Minyard89986492014-04-14 09:46:54 -05004157 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
4158 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 }
Corey Minyard89986492014-04-14 09:46:54 -05004160
4161 tasklet_schedule(&intf->recv_tasklet);
4162
4163 return waiting_msgs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164}
4165
Corey Minyard89986492014-04-14 09:46:54 -05004166static void ipmi_request_event(ipmi_smi_t intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167{
Corey Minyard89986492014-04-14 09:46:54 -05004168 /* No event requests when in maintenance mode. */
4169 if (intf->maintenance_mode_enable)
4170 return;
Corey Minyardb9675132006-12-06 20:41:02 -08004171
Corey Minyard7ea0ed22014-11-06 16:58:48 -06004172 if (!intf->in_shutdown)
4173 intf->handlers->request_events(intf->send_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174}
4175
4176static struct timer_list ipmi_timer;
4177
Corey Minyard8f43f842005-06-23 22:01:40 -07004178static atomic_t stop_operation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179
4180static void ipmi_timeout(unsigned long data)
4181{
Corey Minyard89986492014-04-14 09:46:54 -05004182 ipmi_smi_t intf;
4183 int nt = 0;
4184
Corey Minyard8f43f842005-06-23 22:01:40 -07004185 if (atomic_read(&stop_operation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187
Corey Minyard89986492014-04-14 09:46:54 -05004188 rcu_read_lock();
4189 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4190 int lnt = 0;
4191
4192 if (atomic_read(&intf->event_waiters)) {
4193 intf->ticks_to_req_ev--;
4194 if (intf->ticks_to_req_ev == 0) {
4195 ipmi_request_event(intf);
4196 intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
4197 }
4198 lnt++;
4199 }
4200
4201 lnt += ipmi_timeout_handler(intf, IPMI_TIMEOUT_TIME);
4202
4203 lnt = !!lnt;
4204 if (lnt != intf->last_needs_timer &&
4205 intf->handlers->set_need_watch)
4206 intf->handlers->set_need_watch(intf->send_info, lnt);
4207 intf->last_needs_timer = lnt;
4208
4209 nt += lnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 }
Corey Minyard89986492014-04-14 09:46:54 -05004211 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212
Corey Minyard89986492014-04-14 09:46:54 -05004213 if (nt)
4214 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215}
4216
Corey Minyard89986492014-04-14 09:46:54 -05004217static void need_waiter(ipmi_smi_t intf)
4218{
4219 /* Racy, but worst case we start the timer twice. */
4220 if (!timer_pending(&ipmi_timer))
4221 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
4222}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223
4224static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
4225static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
4226
4227/* FIXME - convert these to slabs. */
4228static void free_smi_msg(struct ipmi_smi_msg *msg)
4229{
4230 atomic_dec(&smi_msg_inuse_count);
4231 kfree(msg);
4232}
4233
4234struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
4235{
4236 struct ipmi_smi_msg *rv;
4237 rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
4238 if (rv) {
4239 rv->done = free_smi_msg;
4240 rv->user_data = NULL;
4241 atomic_inc(&smi_msg_inuse_count);
4242 }
4243 return rv;
4244}
Corey Minyardc70d7492008-04-29 01:01:09 -07004245EXPORT_SYMBOL(ipmi_alloc_smi_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246
4247static void free_recv_msg(struct ipmi_recv_msg *msg)
4248{
4249 atomic_dec(&recv_msg_inuse_count);
4250 kfree(msg);
4251}
4252
Adrian Bunk74006302008-04-29 01:01:14 -07004253static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254{
4255 struct ipmi_recv_msg *rv;
4256
4257 rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
4258 if (rv) {
Corey Minyarda9eec552006-08-31 21:27:45 -07004259 rv->user = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 rv->done = free_recv_msg;
4261 atomic_inc(&recv_msg_inuse_count);
4262 }
4263 return rv;
4264}
4265
Corey Minyard393d2cc2005-11-07 00:59:54 -08004266void ipmi_free_recv_msg(struct ipmi_recv_msg *msg)
4267{
4268 if (msg->user)
4269 kref_put(&msg->user->refcount, free_user);
4270 msg->done(msg);
4271}
Corey Minyardc70d7492008-04-29 01:01:09 -07004272EXPORT_SYMBOL(ipmi_free_recv_msg);
Corey Minyard393d2cc2005-11-07 00:59:54 -08004273
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274#ifdef CONFIG_IPMI_PANIC_EVENT
4275
Corey Minyard895dcfd2012-03-28 14:42:49 -07004276static atomic_t panic_done_count = ATOMIC_INIT(0);
4277
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
4279{
Corey Minyard895dcfd2012-03-28 14:42:49 -07004280 atomic_dec(&panic_done_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281}
4282
4283static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
4284{
Corey Minyard895dcfd2012-03-28 14:42:49 -07004285 atomic_dec(&panic_done_count);
4286}
4287
4288/*
4289 * Inside a panic, send a message and wait for a response.
4290 */
4291static void ipmi_panic_request_and_wait(ipmi_smi_t intf,
4292 struct ipmi_addr *addr,
4293 struct kernel_ipmi_msg *msg)
4294{
4295 struct ipmi_smi_msg smi_msg;
4296 struct ipmi_recv_msg recv_msg;
4297 int rv;
4298
4299 smi_msg.done = dummy_smi_done_handler;
4300 recv_msg.done = dummy_recv_done_handler;
4301 atomic_add(2, &panic_done_count);
4302 rv = i_ipmi_request(NULL,
4303 intf,
4304 addr,
4305 0,
4306 msg,
4307 intf,
4308 &smi_msg,
4309 &recv_msg,
4310 0,
4311 intf->channels[0].address,
4312 intf->channels[0].lun,
4313 0, 1); /* Don't retry, and don't wait. */
4314 if (rv)
4315 atomic_sub(2, &panic_done_count);
4316 while (atomic_read(&panic_done_count) != 0)
4317 ipmi_poll(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318}
4319
4320#ifdef CONFIG_IPMI_PANIC_STRING
Corey Minyard56a55ec2005-09-06 15:18:42 -07004321static void event_receiver_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322{
Corey Minyard56a55ec2005-09-06 15:18:42 -07004323 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4324 && (msg->msg.netfn == IPMI_NETFN_SENSOR_EVENT_RESPONSE)
4325 && (msg->msg.cmd == IPMI_GET_EVENT_RECEIVER_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07004326 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 /* A get event receiver command, save it. */
Corey Minyard56a55ec2005-09-06 15:18:42 -07004328 intf->event_receiver = msg->msg.data[1];
4329 intf->event_receiver_lun = msg->msg.data[2] & 0x3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 }
4331}
4332
Corey Minyard56a55ec2005-09-06 15:18:42 -07004333static void device_id_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334{
Corey Minyard56a55ec2005-09-06 15:18:42 -07004335 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4336 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
4337 && (msg->msg.cmd == IPMI_GET_DEVICE_ID_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07004338 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
4339 /*
4340 * A get device id command, save if we are an event
4341 * receiver or generator.
4342 */
Corey Minyard56a55ec2005-09-06 15:18:42 -07004343 intf->local_sel_device = (msg->msg.data[6] >> 2) & 1;
4344 intf->local_event_generator = (msg->msg.data[6] >> 5) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 }
4346}
4347#endif
4348
4349static void send_panic_events(char *str)
4350{
4351 struct kernel_ipmi_msg msg;
4352 ipmi_smi_t intf;
4353 unsigned char data[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 struct ipmi_system_interface_addr *si;
4355 struct ipmi_addr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356
4357 si = (struct ipmi_system_interface_addr *) &addr;
4358 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4359 si->channel = IPMI_BMC_CHANNEL;
4360 si->lun = 0;
4361
4362 /* Fill in an event telling that we have failed. */
4363 msg.netfn = 0x04; /* Sensor or Event. */
4364 msg.cmd = 2; /* Platform event command. */
4365 msg.data = data;
4366 msg.data_len = 8;
Matt Domschcda315a2005-12-12 00:37:32 -08004367 data[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 data[1] = 0x03; /* This is for IPMI 1.0. */
4369 data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
4370 data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
4371 data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
4372
Corey Minyardc70d7492008-04-29 01:01:09 -07004373 /*
4374 * Put a few breadcrumbs in. Hopefully later we can add more things
4375 * to make the panic events more useful.
4376 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 if (str) {
4378 data[3] = str[0];
4379 data[6] = str[1];
4380 data[7] = str[2];
4381 }
4382
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 /* For every registered interface, send the event. */
Corey Minyardbca03242006-12-06 20:40:57 -08004384 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
Corey Minyardb2c03942006-12-06 20:41:00 -08004385 if (!intf->handlers)
4386 /* Interface is not ready. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 continue;
4388
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07004389 intf->run_to_completion = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 /* Send the event announcing the panic. */
4391 intf->handlers->set_run_to_completion(intf->send_info, 1);
Corey Minyard895dcfd2012-03-28 14:42:49 -07004392 ipmi_panic_request_and_wait(intf, &addr, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 }
4394
4395#ifdef CONFIG_IPMI_PANIC_STRING
Corey Minyardc70d7492008-04-29 01:01:09 -07004396 /*
4397 * On every interface, dump a bunch of OEM event holding the
4398 * string.
4399 */
4400 if (!str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 return;
4402
Corey Minyardbca03242006-12-06 20:40:57 -08004403 /* For every registered interface, send the event. */
4404 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 char *p = str;
4406 struct ipmi_ipmb_addr *ipmb;
4407 int j;
4408
Corey Minyardbca03242006-12-06 20:40:57 -08004409 if (intf->intf_num == -1)
4410 /* Interface was not ready yet. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 continue;
4412
Corey Minyard78ba2fa2007-02-10 01:45:45 -08004413 /*
4414 * intf_num is used as an marker to tell if the
4415 * interface is valid. Thus we need a read barrier to
4416 * make sure data fetched before checking intf_num
4417 * won't be used.
4418 */
4419 smp_rmb();
4420
Corey Minyardc70d7492008-04-29 01:01:09 -07004421 /*
4422 * First job here is to figure out where to send the
4423 * OEM events. There's no way in IPMI to send OEM
4424 * events using an event send command, so we have to
4425 * find the SEL to put them in and stick them in
4426 * there.
4427 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428
4429 /* Get capabilities from the get device id. */
4430 intf->local_sel_device = 0;
4431 intf->local_event_generator = 0;
4432 intf->event_receiver = 0;
4433
4434 /* Request the device info from the local MC. */
4435 msg.netfn = IPMI_NETFN_APP_REQUEST;
4436 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
4437 msg.data = NULL;
4438 msg.data_len = 0;
4439 intf->null_user_handler = device_id_fetcher;
Corey Minyard895dcfd2012-03-28 14:42:49 -07004440 ipmi_panic_request_and_wait(intf, &addr, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441
4442 if (intf->local_event_generator) {
4443 /* Request the event receiver from the local MC. */
4444 msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
4445 msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
4446 msg.data = NULL;
4447 msg.data_len = 0;
4448 intf->null_user_handler = event_receiver_fetcher;
Corey Minyard895dcfd2012-03-28 14:42:49 -07004449 ipmi_panic_request_and_wait(intf, &addr, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 }
4451 intf->null_user_handler = NULL;
4452
Corey Minyardc70d7492008-04-29 01:01:09 -07004453 /*
4454 * Validate the event receiver. The low bit must not
4455 * be 1 (it must be a valid IPMB address), it cannot
4456 * be zero, and it must not be my address.
4457 */
4458 if (((intf->event_receiver & 1) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 && (intf->event_receiver != 0)
Corey Minyardc70d7492008-04-29 01:01:09 -07004460 && (intf->event_receiver != intf->channels[0].address)) {
4461 /*
4462 * The event receiver is valid, send an IPMB
4463 * message.
4464 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 ipmb = (struct ipmi_ipmb_addr *) &addr;
4466 ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
4467 ipmb->channel = 0; /* FIXME - is this right? */
4468 ipmb->lun = intf->event_receiver_lun;
4469 ipmb->slave_addr = intf->event_receiver;
4470 } else if (intf->local_sel_device) {
Corey Minyardc70d7492008-04-29 01:01:09 -07004471 /*
4472 * The event receiver was not valid (or was
4473 * me), but I am an SEL device, just dump it
4474 * in my SEL.
4475 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 si = (struct ipmi_system_interface_addr *) &addr;
4477 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4478 si->channel = IPMI_BMC_CHANNEL;
4479 si->lun = 0;
4480 } else
4481 continue; /* No where to send the event. */
4482
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
4484 msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
4485 msg.data = data;
4486 msg.data_len = 16;
4487
4488 j = 0;
4489 while (*p) {
4490 int size = strlen(p);
4491
4492 if (size > 11)
4493 size = 11;
4494 data[0] = 0;
4495 data[1] = 0;
4496 data[2] = 0xf0; /* OEM event without timestamp. */
Corey Minyardc14979b2005-09-06 15:18:38 -07004497 data[3] = intf->channels[0].address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 data[4] = j++; /* sequence # */
Corey Minyardc70d7492008-04-29 01:01:09 -07004499 /*
4500 * Always give 11 bytes, so strncpy will fill
4501 * it with zeroes for me.
4502 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 strncpy(data+5, p, 11);
4504 p += size;
4505
Corey Minyard895dcfd2012-03-28 14:42:49 -07004506 ipmi_panic_request_and_wait(intf, &addr, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 }
Corey Minyardc70d7492008-04-29 01:01:09 -07004508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509#endif /* CONFIG_IPMI_PANIC_STRING */
4510}
4511#endif /* CONFIG_IPMI_PANIC_EVENT */
4512
Randy Dunlap0c8204b2006-12-10 02:19:06 -08004513static int has_panicked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514
4515static int panic_event(struct notifier_block *this,
4516 unsigned long event,
Corey Minyardc70d7492008-04-29 01:01:09 -07004517 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 ipmi_smi_t intf;
4520
Lee Revellf18190b2006-06-26 18:30:00 +02004521 if (has_panicked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 return NOTIFY_DONE;
Lee Revellf18190b2006-06-26 18:30:00 +02004523 has_panicked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524
4525 /* For every registered interface, set it to run to completion. */
Corey Minyardbca03242006-12-06 20:40:57 -08004526 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
Corey Minyardb2c03942006-12-06 20:41:00 -08004527 if (!intf->handlers)
4528 /* Interface is not ready. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 continue;
4530
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07004531 intf->run_to_completion = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 intf->handlers->set_run_to_completion(intf->send_info, 1);
4533 }
4534
4535#ifdef CONFIG_IPMI_PANIC_EVENT
4536 send_panic_events(ptr);
4537#endif
4538
4539 return NOTIFY_DONE;
4540}
4541
4542static struct notifier_block panic_block = {
4543 .notifier_call = panic_event,
4544 .next = NULL,
4545 .priority = 200 /* priority: INT_MAX >= x >= 0 */
4546};
4547
4548static int ipmi_init_msghandler(void)
4549{
Corey Minyard50c812b2006-03-26 01:37:21 -08004550 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551
4552 if (initialized)
4553 return 0;
4554
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08004555 rv = driver_register(&ipmidriver.driver);
Corey Minyard50c812b2006-03-26 01:37:21 -08004556 if (rv) {
4557 printk(KERN_ERR PFX "Could not register IPMI driver\n");
4558 return rv;
4559 }
4560
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 printk(KERN_INFO "ipmi message handler version "
Corey Minyard1fdd75b2005-09-06 15:18:42 -07004562 IPMI_DRIVER_VERSION "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563
Corey Minyard3b625942005-06-23 22:01:42 -07004564#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 proc_ipmi_root = proc_mkdir("ipmi", NULL);
4566 if (!proc_ipmi_root) {
4567 printk(KERN_ERR PFX "Unable to create IPMI proc dir");
trenn@suse.de80fad5b2014-10-14 16:40:23 +02004568 driver_unregister(&ipmidriver.driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 return -ENOMEM;
4570 }
4571
Corey Minyard3b625942005-06-23 22:01:42 -07004572#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573
Corey Minyard409035e2006-06-28 04:26:53 -07004574 setup_timer(&ipmi_timer, ipmi_timeout, 0);
4575 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576
Alan Sterne041c682006-03-27 01:16:30 -08004577 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
4579 initialized = 1;
4580
4581 return 0;
4582}
4583
Corey Minyard60ee6d52010-10-27 15:34:18 -07004584static int __init ipmi_init_msghandler_mod(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585{
4586 ipmi_init_msghandler();
4587 return 0;
4588}
4589
Corey Minyard60ee6d52010-10-27 15:34:18 -07004590static void __exit cleanup_ipmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591{
4592 int count;
4593
4594 if (!initialized)
4595 return;
4596
Alan Sterne041c682006-03-27 01:16:30 -08004597 atomic_notifier_chain_unregister(&panic_notifier_list, &panic_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598
Corey Minyardc70d7492008-04-29 01:01:09 -07004599 /*
4600 * This can't be called if any interfaces exist, so no worry
4601 * about shutting down the interfaces.
4602 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603
Corey Minyardc70d7492008-04-29 01:01:09 -07004604 /*
4605 * Tell the timer to stop, then wait for it to stop. This
4606 * avoids problems with race conditions removing the timer
4607 * here.
4608 */
Corey Minyard8f43f842005-06-23 22:01:40 -07004609 atomic_inc(&stop_operation);
4610 del_timer_sync(&ipmi_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
Corey Minyard3b625942005-06-23 22:01:42 -07004612#ifdef CONFIG_PROC_FS
David Howellsa8ca16e2013-04-12 17:27:28 +01004613 proc_remove(proc_ipmi_root);
Corey Minyard3b625942005-06-23 22:01:42 -07004614#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08004616 driver_unregister(&ipmidriver.driver);
Corey Minyard50c812b2006-03-26 01:37:21 -08004617
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 initialized = 0;
4619
4620 /* Check for buffer leaks. */
4621 count = atomic_read(&smi_msg_inuse_count);
4622 if (count != 0)
4623 printk(KERN_WARNING PFX "SMI message count %d at exit\n",
4624 count);
4625 count = atomic_read(&recv_msg_inuse_count);
4626 if (count != 0)
4627 printk(KERN_WARNING PFX "recv message count %d at exit\n",
4628 count);
4629}
4630module_exit(cleanup_ipmi);
4631
4632module_init(ipmi_init_msghandler_mod);
4633MODULE_LICENSE("GPL");
Corey Minyard1fdd75b2005-09-06 15:18:42 -07004634MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
Corey Minyardc70d7492008-04-29 01:01:09 -07004635MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI"
4636 " interface.");
Corey Minyard1fdd75b2005-09-06 15:18:42 -07004637MODULE_VERSION(IPMI_DRIVER_VERSION);