blob: e3536da05c88aaddf8ed803feb3b089e47053dbe [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};
Corey Minyard16639eb2014-10-10 21:54:03 -0500203#define to_bmc_device(x) container_of((x), struct bmc_device, pdev.dev)
Corey Minyard50c812b2006-03-26 01:37:21 -0800204
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700205/*
206 * Various statistics for IPMI, these index stats[] in the ipmi_smi
207 * structure.
208 */
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700209enum ipmi_stat_indexes {
210 /* Commands we got from the user that were invalid. */
211 IPMI_STAT_sent_invalid_commands = 0,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700212
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700213 /* Commands we sent to the MC. */
214 IPMI_STAT_sent_local_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700215
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700216 /* Responses from the MC that were delivered to a user. */
217 IPMI_STAT_handled_local_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700218
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700219 /* Responses from the MC that were not delivered to a user. */
220 IPMI_STAT_unhandled_local_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700221
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700222 /* Commands we sent out to the IPMB bus. */
223 IPMI_STAT_sent_ipmb_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700224
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700225 /* Commands sent on the IPMB that had errors on the SEND CMD */
226 IPMI_STAT_sent_ipmb_command_errs,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700227
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700228 /* Each retransmit increments this count. */
229 IPMI_STAT_retransmitted_ipmb_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700230
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700231 /*
232 * When a message times out (runs out of retransmits) this is
233 * incremented.
234 */
235 IPMI_STAT_timed_out_ipmb_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700236
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700237 /*
238 * This is like above, but for broadcasts. Broadcasts are
239 * *not* included in the above count (they are expected to
240 * time out).
241 */
242 IPMI_STAT_timed_out_ipmb_broadcasts,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700243
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700244 /* Responses I have sent to the IPMB bus. */
245 IPMI_STAT_sent_ipmb_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700246
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700247 /* The response was delivered to the user. */
248 IPMI_STAT_handled_ipmb_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700249
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700250 /* The response had invalid data in it. */
251 IPMI_STAT_invalid_ipmb_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700252
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700253 /* The response didn't have anyone waiting for it. */
254 IPMI_STAT_unhandled_ipmb_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700255
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700256 /* Commands we sent out to the IPMB bus. */
257 IPMI_STAT_sent_lan_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700258
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700259 /* Commands sent on the IPMB that had errors on the SEND CMD */
260 IPMI_STAT_sent_lan_command_errs,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700261
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700262 /* Each retransmit increments this count. */
263 IPMI_STAT_retransmitted_lan_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700264
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700265 /*
266 * When a message times out (runs out of retransmits) this is
267 * incremented.
268 */
269 IPMI_STAT_timed_out_lan_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700270
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700271 /* Responses I have sent to the IPMB bus. */
272 IPMI_STAT_sent_lan_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700273
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700274 /* The response was delivered to the user. */
275 IPMI_STAT_handled_lan_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700276
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700277 /* The response had invalid data in it. */
278 IPMI_STAT_invalid_lan_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700279
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700280 /* The response didn't have anyone waiting for it. */
281 IPMI_STAT_unhandled_lan_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700282
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700283 /* The command was delivered to the user. */
284 IPMI_STAT_handled_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700285
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700286 /* The command had invalid data in it. */
287 IPMI_STAT_invalid_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700288
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700289 /* The command didn't have anyone waiting for it. */
290 IPMI_STAT_unhandled_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700291
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700292 /* Invalid data in an event. */
293 IPMI_STAT_invalid_events,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700294
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700295 /* Events that were received with the proper format. */
296 IPMI_STAT_events,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700297
Corey Minyard25176ed2009-04-21 12:24:04 -0700298 /* Retransmissions on IPMB that failed. */
299 IPMI_STAT_dropped_rexmit_ipmb_commands,
300
301 /* Retransmissions on LAN that failed. */
302 IPMI_STAT_dropped_rexmit_lan_commands,
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700303
304 /* This *must* remain last, add new values above this. */
305 IPMI_NUM_STATS
306};
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700307
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309#define IPMI_IPMB_NUM_SEQ 64
Corey Minyardc14979b2005-09-06 15:18:38 -0700310#define IPMI_MAX_CHANNELS 16
Corey Minyardc70d7492008-04-29 01:01:09 -0700311struct ipmi_smi {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 /* What interface number are we? */
313 int intf_num;
314
Corey Minyard393d2cc2005-11-07 00:59:54 -0800315 struct kref refcount;
316
Corey Minyard7ea0ed22014-11-06 16:58:48 -0600317 /* Set when the interface is being unregistered. */
318 bool in_shutdown;
319
Corey Minyardbca03242006-12-06 20:40:57 -0800320 /* Used for a list of interfaces. */
321 struct list_head link;
322
Corey Minyardc70d7492008-04-29 01:01:09 -0700323 /*
324 * The list of upper layers that are using me. seq_lock
325 * protects this.
326 */
Corey Minyard393d2cc2005-11-07 00:59:54 -0800327 struct list_head users;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Corey Minyardb2c03942006-12-06 20:41:00 -0800329 /* Information to supply to users. */
330 unsigned char ipmi_version_major;
331 unsigned char ipmi_version_minor;
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 /* Used for wake ups at startup. */
334 wait_queue_head_t waitq;
335
Corey Minyard50c812b2006-03-26 01:37:21 -0800336 struct bmc_device *bmc;
337 char *my_dev_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Corey Minyardc70d7492008-04-29 01:01:09 -0700339 /*
340 * This is the lower-layer's sender routine. Note that you
Corey Minyardb2c03942006-12-06 20:41:00 -0800341 * must either be holding the ipmi_interfaces_mutex or be in
342 * an umpreemptible region to use this. You must fetch the
Corey Minyardc70d7492008-04-29 01:01:09 -0700343 * value into a local variable and make sure it is not NULL.
344 */
Corey Minyard81d02b72015-06-13 10:34:25 -0500345 const struct ipmi_smi_handlers *handlers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 void *send_info;
347
Corey Minyard3b625942005-06-23 22:01:42 -0700348#ifdef CONFIG_PROC_FS
Corey Minyardac019152007-10-18 03:07:11 -0700349 /* A list of proc entries for this interface. */
350 struct mutex proc_entry_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 struct ipmi_proc_entry *proc_entries;
Corey Minyard3b625942005-06-23 22:01:42 -0700352#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Corey Minyard50c812b2006-03-26 01:37:21 -0800354 /* Driver-model device for the system interface. */
355 struct device *si_dev;
356
Corey Minyardc70d7492008-04-29 01:01:09 -0700357 /*
358 * A table of sequence numbers for this interface. We use the
359 * sequence numbers for IPMB messages that go out of the
360 * interface to match them up with their responses. A routine
361 * is called periodically to time the items in this list.
362 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 spinlock_t seq_lock;
364 struct seq_table seq_table[IPMI_IPMB_NUM_SEQ];
365 int curr_seq;
366
Corey Minyardc70d7492008-04-29 01:01:09 -0700367 /*
Corey Minyard7adf5792012-03-28 14:42:49 -0700368 * Messages queued for delivery. If delivery fails (out of memory
369 * for instance), They will stay in here to be processed later in a
370 * periodic timer interrupt. The tasklet is for handling received
371 * messages directly from the handler.
Corey Minyardc70d7492008-04-29 01:01:09 -0700372 */
Corey Minyard65be7542014-11-06 20:48:02 -0600373 spinlock_t waiting_rcv_msgs_lock;
374 struct list_head waiting_rcv_msgs;
Corey Minyard7adf5792012-03-28 14:42:49 -0700375 atomic_t watchdog_pretimeouts_to_deliver;
376 struct tasklet_struct recv_tasklet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Corey Minyard7ea0ed22014-11-06 16:58:48 -0600378 spinlock_t xmit_msgs_lock;
379 struct list_head xmit_msgs;
380 struct ipmi_smi_msg *curr_msg;
381 struct list_head hp_xmit_msgs;
382
Corey Minyardc70d7492008-04-29 01:01:09 -0700383 /*
384 * The list of command receivers that are registered for commands
385 * on this interface.
386 */
Corey Minyardd6dfd132006-03-31 02:30:41 -0800387 struct mutex cmd_rcvrs_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 struct list_head cmd_rcvrs;
389
Corey Minyardc70d7492008-04-29 01:01:09 -0700390 /*
391 * Events that were queues because no one was there to receive
392 * them.
393 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 spinlock_t events_lock; /* For dealing with event stuff. */
395 struct list_head waiting_events;
396 unsigned int waiting_events_count; /* How many events in queue? */
Corey Minyard87ebd062008-04-29 01:01:04 -0700397 char delivering_events;
398 char event_msg_printed;
Corey Minyard89986492014-04-14 09:46:54 -0500399 atomic_t event_waiters;
400 unsigned int ticks_to_req_ev;
401 int last_needs_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Corey Minyardc70d7492008-04-29 01:01:09 -0700403 /*
404 * The event receiver for my BMC, only really used at panic
405 * shutdown as a place to store this.
406 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 unsigned char event_receiver;
408 unsigned char event_receiver_lun;
409 unsigned char local_sel_device;
410 unsigned char local_event_generator;
411
Corey Minyardb9675132006-12-06 20:41:02 -0800412 /* For handling of maintenance mode. */
413 int maintenance_mode;
Corey Minyard7aefac22014-04-14 09:46:56 -0500414 bool maintenance_mode_enable;
Corey Minyardb9675132006-12-06 20:41:02 -0800415 int auto_maintenance_timeout;
416 spinlock_t maintenance_mode_lock; /* Used in a timer... */
417
Corey Minyardc70d7492008-04-29 01:01:09 -0700418 /*
419 * A cheap hack, if this is non-null and a message to an
420 * interface comes in with a NULL user, call this routine with
421 * it. Note that the message will still be freed by the
422 * caller. This only works on the system interface.
423 */
Corey Minyard56a55ec2005-09-06 15:18:42 -0700424 void (*null_user_handler)(ipmi_smi_t intf, struct ipmi_recv_msg *msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Corey Minyardc70d7492008-04-29 01:01:09 -0700426 /*
427 * When we are scanning the channels for an SMI, this will
428 * tell which channel we are scanning.
429 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 int curr_channel;
431
432 /* Channel information */
433 struct ipmi_channel channels[IPMI_MAX_CHANNELS];
434
435 /* Proc FS stuff. */
436 struct proc_dir_entry *proc_dir;
437 char proc_dir_name[10];
438
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700439 atomic_t stats[IPMI_NUM_STATS];
Konstantin Baydarov5956dce2008-04-29 01:01:03 -0700440
441 /*
442 * run_to_completion duplicate of smb_info, smi_info
443 * and ipmi_serial_info structures. Used to decrease numbers of
444 * parameters passed by "low" level IPMI code.
445 */
446 int run_to_completion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447};
Corey Minyard50c812b2006-03-26 01:37:21 -0800448#define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Corey Minyard50c812b2006-03-26 01:37:21 -0800450/**
451 * The driver model view of the IPMI messaging driver.
452 */
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -0800453static struct platform_driver ipmidriver = {
454 .driver = {
455 .name = "ipmi",
456 .bus = &platform_bus_type
457 }
Corey Minyard50c812b2006-03-26 01:37:21 -0800458};
459static DEFINE_MUTEX(ipmidriver_mutex);
460
Denis Chengbed97592008-02-06 01:37:35 -0800461static LIST_HEAD(ipmi_interfaces);
Corey Minyardbca03242006-12-06 20:40:57 -0800462static DEFINE_MUTEX(ipmi_interfaces_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Corey Minyardc70d7492008-04-29 01:01:09 -0700464/*
465 * List of watchers that want to know when smi's are added and deleted.
466 */
Denis Chengbed97592008-02-06 01:37:35 -0800467static LIST_HEAD(smi_watchers);
Corey Minyardb2c03942006-12-06 20:41:00 -0800468static DEFINE_MUTEX(smi_watchers_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700470#define ipmi_inc_stat(intf, stat) \
471 atomic_inc(&(intf)->stats[IPMI_STAT_ ## stat])
472#define ipmi_get_stat(intf, stat) \
473 ((unsigned int) atomic_read(&(intf)->stats[IPMI_STAT_ ## stat]))
474
Corey Minyard7e503872014-10-09 07:20:32 -0500475static char *addr_src_to_str[] = { "invalid", "hotmod", "hardcoded", "SPMI",
476 "ACPI", "SMBIOS", "PCI",
477 "device-tree", "default" };
478
479const char *ipmi_addr_src_to_str(enum ipmi_addr_src src)
480{
481 if (src > SI_DEFAULT)
482 src = 0; /* Invalid */
483 return addr_src_to_str[src];
484}
485EXPORT_SYMBOL(ipmi_addr_src_to_str);
486
Corey Minyard25176ed2009-04-21 12:24:04 -0700487static int is_lan_addr(struct ipmi_addr *addr)
488{
489 return addr->addr_type == IPMI_LAN_ADDR_TYPE;
490}
491
492static int is_ipmb_addr(struct ipmi_addr *addr)
493{
494 return addr->addr_type == IPMI_IPMB_ADDR_TYPE;
495}
496
497static int is_ipmb_bcast_addr(struct ipmi_addr *addr)
498{
499 return addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE;
500}
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700501
Corey Minyard393d2cc2005-11-07 00:59:54 -0800502static void free_recv_msg_list(struct list_head *q)
503{
504 struct ipmi_recv_msg *msg, *msg2;
505
506 list_for_each_entry_safe(msg, msg2, q, link) {
507 list_del(&msg->link);
508 ipmi_free_recv_msg(msg);
509 }
510}
511
Corey Minyardf3ce6a02006-11-08 17:44:52 -0800512static void free_smi_msg_list(struct list_head *q)
513{
514 struct ipmi_smi_msg *msg, *msg2;
515
516 list_for_each_entry_safe(msg, msg2, q, link) {
517 list_del(&msg->link);
518 ipmi_free_smi_msg(msg);
519 }
520}
521
Corey Minyard393d2cc2005-11-07 00:59:54 -0800522static void clean_up_interface_data(ipmi_smi_t intf)
523{
524 int i;
525 struct cmd_rcvr *rcvr, *rcvr2;
Corey Minyard393d2cc2005-11-07 00:59:54 -0800526 struct list_head list;
527
Corey Minyard7adf5792012-03-28 14:42:49 -0700528 tasklet_kill(&intf->recv_tasklet);
529
Corey Minyard65be7542014-11-06 20:48:02 -0600530 free_smi_msg_list(&intf->waiting_rcv_msgs);
Corey Minyard393d2cc2005-11-07 00:59:54 -0800531 free_recv_msg_list(&intf->waiting_events);
532
Corey Minyard78ba2fa2007-02-10 01:45:45 -0800533 /*
534 * Wholesale remove all the entries from the list in the
535 * interface and wait for RCU to know that none are in use.
536 */
Corey Minyardd6dfd132006-03-31 02:30:41 -0800537 mutex_lock(&intf->cmd_rcvrs_mutex);
Corey Minyard78ba2fa2007-02-10 01:45:45 -0800538 INIT_LIST_HEAD(&list);
539 list_splice_init_rcu(&intf->cmd_rcvrs, &list, synchronize_rcu);
Corey Minyardd6dfd132006-03-31 02:30:41 -0800540 mutex_unlock(&intf->cmd_rcvrs_mutex);
Corey Minyard393d2cc2005-11-07 00:59:54 -0800541
542 list_for_each_entry_safe(rcvr, rcvr2, &list, link)
543 kfree(rcvr);
544
545 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
546 if ((intf->seq_table[i].inuse)
Corey Minyardc70d7492008-04-29 01:01:09 -0700547 && (intf->seq_table[i].recv_msg))
Corey Minyard393d2cc2005-11-07 00:59:54 -0800548 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
Corey Minyard393d2cc2005-11-07 00:59:54 -0800550}
551
552static void intf_free(struct kref *ref)
553{
554 ipmi_smi_t intf = container_of(ref, struct ipmi_smi, refcount);
555
556 clean_up_interface_data(intf);
557 kfree(intf);
558}
559
Corey Minyardbca03242006-12-06 20:40:57 -0800560struct watcher_entry {
Corey Minyardb2c03942006-12-06 20:41:00 -0800561 int intf_num;
562 ipmi_smi_t intf;
Corey Minyardbca03242006-12-06 20:40:57 -0800563 struct list_head link;
Corey Minyardbca03242006-12-06 20:40:57 -0800564};
565
Corey Minyard393d2cc2005-11-07 00:59:54 -0800566int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher)
567{
Corey Minyardbca03242006-12-06 20:40:57 -0800568 ipmi_smi_t intf;
Denis Chenge381d1c2008-02-06 01:37:39 -0800569 LIST_HEAD(to_deliver);
Corey Minyardbca03242006-12-06 20:40:57 -0800570 struct watcher_entry *e, *e2;
571
Corey Minyardb2c03942006-12-06 20:41:00 -0800572 mutex_lock(&smi_watchers_mutex);
573
Corey Minyardbca03242006-12-06 20:40:57 -0800574 mutex_lock(&ipmi_interfaces_mutex);
575
Corey Minyardb2c03942006-12-06 20:41:00 -0800576 /* Build a list of things to deliver. */
Corey Minyard78ba2fa2007-02-10 01:45:45 -0800577 list_for_each_entry(intf, &ipmi_interfaces, link) {
Corey Minyardbca03242006-12-06 20:40:57 -0800578 if (intf->intf_num == -1)
579 continue;
580 e = kmalloc(sizeof(*e), GFP_KERNEL);
581 if (!e)
582 goto out_err;
Corey Minyardb2c03942006-12-06 20:41:00 -0800583 kref_get(&intf->refcount);
584 e->intf = intf;
Corey Minyardbca03242006-12-06 20:40:57 -0800585 e->intf_num = intf->intf_num;
586 list_add_tail(&e->link, &to_deliver);
587 }
Corey Minyard393d2cc2005-11-07 00:59:54 -0800588
Corey Minyardb2c03942006-12-06 20:41:00 -0800589 /* We will succeed, so add it to the list. */
590 list_add(&watcher->link, &smi_watchers);
Corey Minyardbca03242006-12-06 20:40:57 -0800591
592 mutex_unlock(&ipmi_interfaces_mutex);
593
594 list_for_each_entry_safe(e, e2, &to_deliver, link) {
595 list_del(&e->link);
Corey Minyardb2c03942006-12-06 20:41:00 -0800596 watcher->new_smi(e->intf_num, e->intf->si_dev);
597 kref_put(&e->intf->refcount, intf_free);
Corey Minyardbca03242006-12-06 20:40:57 -0800598 kfree(e);
Corey Minyard393d2cc2005-11-07 00:59:54 -0800599 }
Corey Minyardbca03242006-12-06 20:40:57 -0800600
Corey Minyardb2c03942006-12-06 20:41:00 -0800601 mutex_unlock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -0800602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return 0;
Corey Minyardbca03242006-12-06 20:40:57 -0800604
605 out_err:
Corey Minyardb2c03942006-12-06 20:41:00 -0800606 mutex_unlock(&ipmi_interfaces_mutex);
607 mutex_unlock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -0800608 list_for_each_entry_safe(e, e2, &to_deliver, link) {
609 list_del(&e->link);
Corey Minyardb2c03942006-12-06 20:41:00 -0800610 kref_put(&e->intf->refcount, intf_free);
Corey Minyardbca03242006-12-06 20:40:57 -0800611 kfree(e);
612 }
613 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
Corey Minyardc70d7492008-04-29 01:01:09 -0700615EXPORT_SYMBOL(ipmi_smi_watcher_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher)
618{
Corey Minyardb2c03942006-12-06 20:41:00 -0800619 mutex_lock(&smi_watchers_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 list_del(&(watcher->link));
Corey Minyardb2c03942006-12-06 20:41:00 -0800621 mutex_unlock(&smi_watchers_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return 0;
623}
Corey Minyardc70d7492008-04-29 01:01:09 -0700624EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Corey Minyardb2c03942006-12-06 20:41:00 -0800626/*
627 * Must be called with smi_watchers_mutex held.
628 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629static void
Corey Minyard50c812b2006-03-26 01:37:21 -0800630call_smi_watchers(int i, struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
632 struct ipmi_smi_watcher *w;
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 list_for_each_entry(w, &smi_watchers, link) {
635 if (try_module_get(w->owner)) {
Corey Minyard50c812b2006-03-26 01:37:21 -0800636 w->new_smi(i, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 module_put(w->owner);
638 }
639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
642static int
643ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2)
644{
645 if (addr1->addr_type != addr2->addr_type)
646 return 0;
647
648 if (addr1->channel != addr2->channel)
649 return 0;
650
651 if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
652 struct ipmi_system_interface_addr *smi_addr1
653 = (struct ipmi_system_interface_addr *) addr1;
654 struct ipmi_system_interface_addr *smi_addr2
655 = (struct ipmi_system_interface_addr *) addr2;
656 return (smi_addr1->lun == smi_addr2->lun);
657 }
658
Corey Minyard25176ed2009-04-21 12:24:04 -0700659 if (is_ipmb_addr(addr1) || is_ipmb_bcast_addr(addr1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 struct ipmi_ipmb_addr *ipmb_addr1
661 = (struct ipmi_ipmb_addr *) addr1;
662 struct ipmi_ipmb_addr *ipmb_addr2
663 = (struct ipmi_ipmb_addr *) addr2;
664
665 return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr)
666 && (ipmb_addr1->lun == ipmb_addr2->lun));
667 }
668
Corey Minyard25176ed2009-04-21 12:24:04 -0700669 if (is_lan_addr(addr1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 struct ipmi_lan_addr *lan_addr1
671 = (struct ipmi_lan_addr *) addr1;
672 struct ipmi_lan_addr *lan_addr2
673 = (struct ipmi_lan_addr *) addr2;
674
675 return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID)
676 && (lan_addr1->local_SWID == lan_addr2->local_SWID)
677 && (lan_addr1->session_handle
678 == lan_addr2->session_handle)
679 && (lan_addr1->lun == lan_addr2->lun));
680 }
681
682 return 1;
683}
684
685int ipmi_validate_addr(struct ipmi_addr *addr, int len)
686{
Corey Minyardc70d7492008-04-29 01:01:09 -0700687 if (len < sizeof(struct ipmi_system_interface_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
691 if (addr->channel != IPMI_BMC_CHANNEL)
692 return -EINVAL;
693 return 0;
694 }
695
696 if ((addr->channel == IPMI_BMC_CHANNEL)
Jayachandran C12fc1d72006-02-03 03:04:51 -0800697 || (addr->channel >= IPMI_MAX_CHANNELS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 || (addr->channel < 0))
699 return -EINVAL;
700
Corey Minyard25176ed2009-04-21 12:24:04 -0700701 if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
Corey Minyardc70d7492008-04-29 01:01:09 -0700702 if (len < sizeof(struct ipmi_ipmb_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return 0;
705 }
706
Corey Minyard25176ed2009-04-21 12:24:04 -0700707 if (is_lan_addr(addr)) {
Corey Minyardc70d7492008-04-29 01:01:09 -0700708 if (len < sizeof(struct ipmi_lan_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return 0;
711 }
712
713 return -EINVAL;
714}
Corey Minyardc70d7492008-04-29 01:01:09 -0700715EXPORT_SYMBOL(ipmi_validate_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717unsigned int ipmi_addr_length(int addr_type)
718{
719 if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
720 return sizeof(struct ipmi_system_interface_addr);
721
722 if ((addr_type == IPMI_IPMB_ADDR_TYPE)
Corey Minyardc70d7492008-04-29 01:01:09 -0700723 || (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return sizeof(struct ipmi_ipmb_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 if (addr_type == IPMI_LAN_ADDR_TYPE)
727 return sizeof(struct ipmi_lan_addr);
728
729 return 0;
730}
Corey Minyardc70d7492008-04-29 01:01:09 -0700731EXPORT_SYMBOL(ipmi_addr_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733static void deliver_response(struct ipmi_recv_msg *msg)
734{
Corey Minyard8a3628d2006-03-31 02:30:40 -0800735 if (!msg->user) {
Corey Minyard56a55ec2005-09-06 15:18:42 -0700736 ipmi_smi_t intf = msg->user_msg_data;
Corey Minyard56a55ec2005-09-06 15:18:42 -0700737
738 /* Special handling for NULL users. */
739 if (intf->null_user_handler) {
740 intf->null_user_handler(intf, msg);
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700741 ipmi_inc_stat(intf, handled_local_responses);
Corey Minyard56a55ec2005-09-06 15:18:42 -0700742 } else {
743 /* No handler, so give up. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700744 ipmi_inc_stat(intf, unhandled_local_responses);
Corey Minyard56a55ec2005-09-06 15:18:42 -0700745 }
746 ipmi_free_recv_msg(msg);
Hidehiro Kawaic49c0972015-07-27 14:55:16 +0900747 } else if (!oops_in_progress) {
748 /*
749 * If we are running in the panic context, calling the
750 * receive handler doesn't much meaning and has a deadlock
751 * risk. At this moment, simply skip it in that case.
752 */
753
Corey Minyard393d2cc2005-11-07 00:59:54 -0800754 ipmi_user_t user = msg->user;
755 user->handler->ipmi_recv_hndl(msg, user->handler_data);
Corey Minyard56a55ec2005-09-06 15:18:42 -0700756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
Corey Minyardb2c03942006-12-06 20:41:00 -0800759static void
760deliver_err_response(struct ipmi_recv_msg *msg, int err)
761{
762 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
763 msg->msg_data[0] = err;
764 msg->msg.netfn |= 1; /* Convert to a response. */
765 msg->msg.data_len = 1;
766 msg->msg.data = msg->msg_data;
767 deliver_response(msg);
768}
769
Corey Minyardc70d7492008-04-29 01:01:09 -0700770/*
771 * Find the next sequence number not being used and add the given
772 * message with the given timeout to the sequence table. This must be
773 * called with the interface's seq_lock held.
774 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775static int intf_next_seq(ipmi_smi_t intf,
776 struct ipmi_recv_msg *recv_msg,
777 unsigned long timeout,
778 int retries,
779 int broadcast,
780 unsigned char *seq,
781 long *seqid)
782{
783 int rv = 0;
784 unsigned int i;
785
Corey Minyardc70d7492008-04-29 01:01:09 -0700786 for (i = intf->curr_seq; (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq;
787 i = (i+1)%IPMI_IPMB_NUM_SEQ) {
Corey Minyard8a3628d2006-03-31 02:30:40 -0800788 if (!intf->seq_table[i].inuse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 break;
790 }
791
Corey Minyard8a3628d2006-03-31 02:30:40 -0800792 if (!intf->seq_table[i].inuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 intf->seq_table[i].recv_msg = recv_msg;
794
Corey Minyardc70d7492008-04-29 01:01:09 -0700795 /*
796 * Start with the maximum timeout, when the send response
797 * comes in we will start the real timer.
798 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 intf->seq_table[i].timeout = MAX_MSG_TIMEOUT;
800 intf->seq_table[i].orig_timeout = timeout;
801 intf->seq_table[i].retries_left = retries;
802 intf->seq_table[i].broadcast = broadcast;
803 intf->seq_table[i].inuse = 1;
804 intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid);
805 *seq = i;
806 *seqid = intf->seq_table[i].seqid;
807 intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ;
Corey Minyard89986492014-04-14 09:46:54 -0500808 need_waiter(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 } else {
810 rv = -EAGAIN;
811 }
Corey Minyardc70d7492008-04-29 01:01:09 -0700812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return rv;
814}
815
Corey Minyardc70d7492008-04-29 01:01:09 -0700816/*
817 * Return the receive message for the given sequence number and
818 * release the sequence number so it can be reused. Some other data
819 * is passed in to be sure the message matches up correctly (to help
820 * guard against message coming in after their timeout and the
821 * sequence number being reused).
822 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823static int intf_find_seq(ipmi_smi_t intf,
824 unsigned char seq,
825 short channel,
826 unsigned char cmd,
827 unsigned char netfn,
828 struct ipmi_addr *addr,
829 struct ipmi_recv_msg **recv_msg)
830{
831 int rv = -ENODEV;
832 unsigned long flags;
833
834 if (seq >= IPMI_IPMB_NUM_SEQ)
835 return -EINVAL;
836
837 spin_lock_irqsave(&(intf->seq_lock), flags);
838 if (intf->seq_table[seq].inuse) {
839 struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg;
840
Corey Minyardc70d7492008-04-29 01:01:09 -0700841 if ((msg->addr.channel == channel) && (msg->msg.cmd == cmd)
842 && (msg->msg.netfn == netfn)
843 && (ipmi_addr_equal(addr, &(msg->addr)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 *recv_msg = msg;
845 intf->seq_table[seq].inuse = 0;
846 rv = 0;
847 }
848 }
849 spin_unlock_irqrestore(&(intf->seq_lock), flags);
850
851 return rv;
852}
853
854
855/* Start the timer for a specific sequence table entry. */
856static int intf_start_seq_timer(ipmi_smi_t intf,
857 long msgid)
858{
859 int rv = -ENODEV;
860 unsigned long flags;
861 unsigned char seq;
862 unsigned long seqid;
863
864
865 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
866
867 spin_lock_irqsave(&(intf->seq_lock), flags);
Corey Minyardc70d7492008-04-29 01:01:09 -0700868 /*
869 * We do this verification because the user can be deleted
870 * while a message is outstanding.
871 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if ((intf->seq_table[seq].inuse)
Corey Minyardc70d7492008-04-29 01:01:09 -0700873 && (intf->seq_table[seq].seqid == seqid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 struct seq_table *ent = &(intf->seq_table[seq]);
875 ent->timeout = ent->orig_timeout;
876 rv = 0;
877 }
878 spin_unlock_irqrestore(&(intf->seq_lock), flags);
879
880 return rv;
881}
882
883/* Got an error for the send message for a specific sequence number. */
884static int intf_err_seq(ipmi_smi_t intf,
885 long msgid,
886 unsigned int err)
887{
888 int rv = -ENODEV;
889 unsigned long flags;
890 unsigned char seq;
891 unsigned long seqid;
892 struct ipmi_recv_msg *msg = NULL;
893
894
895 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
896
897 spin_lock_irqsave(&(intf->seq_lock), flags);
Corey Minyardc70d7492008-04-29 01:01:09 -0700898 /*
899 * We do this verification because the user can be deleted
900 * while a message is outstanding.
901 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 if ((intf->seq_table[seq].inuse)
Corey Minyardc70d7492008-04-29 01:01:09 -0700903 && (intf->seq_table[seq].seqid == seqid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 struct seq_table *ent = &(intf->seq_table[seq]);
905
906 ent->inuse = 0;
907 msg = ent->recv_msg;
908 rv = 0;
909 }
910 spin_unlock_irqrestore(&(intf->seq_lock), flags);
911
Corey Minyardb2c03942006-12-06 20:41:00 -0800912 if (msg)
913 deliver_err_response(msg, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 return rv;
916}
917
918
919int ipmi_create_user(unsigned int if_num,
920 struct ipmi_user_hndl *handler,
921 void *handler_data,
922 ipmi_user_t *user)
923{
924 unsigned long flags;
925 ipmi_user_t new_user;
926 int rv = 0;
927 ipmi_smi_t intf;
928
Corey Minyardc70d7492008-04-29 01:01:09 -0700929 /*
930 * There is no module usecount here, because it's not
931 * required. Since this can only be used by and called from
932 * other modules, they will implicitly use this module, and
933 * thus this can't be removed unless the other modules are
934 * removed.
935 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 if (handler == NULL)
938 return -EINVAL;
939
Corey Minyardc70d7492008-04-29 01:01:09 -0700940 /*
941 * Make sure the driver is actually initialized, this handles
942 * problems with initialization order.
943 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 if (!initialized) {
945 rv = ipmi_init_msghandler();
946 if (rv)
947 return rv;
948
Corey Minyardc70d7492008-04-29 01:01:09 -0700949 /*
950 * The init code doesn't return an error if it was turned
951 * off, but it won't initialize. Check that.
952 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (!initialized)
954 return -ENODEV;
955 }
956
957 new_user = kmalloc(sizeof(*new_user), GFP_KERNEL);
Corey Minyard8a3628d2006-03-31 02:30:40 -0800958 if (!new_user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return -ENOMEM;
960
Corey Minyardb2c03942006-12-06 20:41:00 -0800961 mutex_lock(&ipmi_interfaces_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -0800962 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
963 if (intf->intf_num == if_num)
964 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
Corey Minyardb2c03942006-12-06 20:41:00 -0800966 /* Not found, return an error */
Corey Minyardbca03242006-12-06 20:40:57 -0800967 rv = -EINVAL;
968 goto out_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Corey Minyardbca03242006-12-06 20:40:57 -0800970 found:
Corey Minyard393d2cc2005-11-07 00:59:54 -0800971 /* Note that each existing user holds a refcount to the interface. */
972 kref_get(&intf->refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Corey Minyard393d2cc2005-11-07 00:59:54 -0800974 kref_init(&new_user->refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 new_user->handler = handler;
976 new_user->handler_data = handler_data;
977 new_user->intf = intf;
Corey Minyard89986492014-04-14 09:46:54 -0500978 new_user->gets_events = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 if (!try_module_get(intf->handlers->owner)) {
981 rv = -ENODEV;
Adrian Bunk5c98d292006-03-25 03:07:52 -0800982 goto out_kref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
984
985 if (intf->handlers->inc_usecount) {
986 rv = intf->handlers->inc_usecount(intf->send_info);
987 if (rv) {
988 module_put(intf->handlers->owner);
Adrian Bunk5c98d292006-03-25 03:07:52 -0800989 goto out_kref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991 }
992
Corey Minyardc70d7492008-04-29 01:01:09 -0700993 /*
994 * Hold the lock so intf->handlers is guaranteed to be good
995 * until now
996 */
Corey Minyardb2c03942006-12-06 20:41:00 -0800997 mutex_unlock(&ipmi_interfaces_mutex);
998
Corey Minyard7aefac22014-04-14 09:46:56 -0500999 new_user->valid = true;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001000 spin_lock_irqsave(&intf->seq_lock, flags);
1001 list_add_rcu(&new_user->link, &intf->users);
1002 spin_unlock_irqrestore(&intf->seq_lock, flags);
Corey Minyard89986492014-04-14 09:46:54 -05001003 if (handler->ipmi_watchdog_pretimeout) {
1004 /* User wants pretimeouts, so make sure to watch for them. */
1005 if (atomic_inc_return(&intf->event_waiters) == 1)
1006 need_waiter(intf);
1007 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08001008 *user = new_user;
1009 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Adrian Bunk5c98d292006-03-25 03:07:52 -08001011out_kref:
Corey Minyard393d2cc2005-11-07 00:59:54 -08001012 kref_put(&intf->refcount, intf_free);
Adrian Bunk5c98d292006-03-25 03:07:52 -08001013out_kfree:
Corey Minyardb2c03942006-12-06 20:41:00 -08001014 mutex_unlock(&ipmi_interfaces_mutex);
Adrian Bunk5c98d292006-03-25 03:07:52 -08001015 kfree(new_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return rv;
1017}
Corey Minyardc70d7492008-04-29 01:01:09 -07001018EXPORT_SYMBOL(ipmi_create_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Zhao Yakui16f42322010-12-08 10:10:16 +08001020int ipmi_get_smi_info(int if_num, struct ipmi_smi_info *data)
1021{
1022 int rv = 0;
1023 ipmi_smi_t intf;
Corey Minyard81d02b72015-06-13 10:34:25 -05001024 const struct ipmi_smi_handlers *handlers;
Zhao Yakui16f42322010-12-08 10:10:16 +08001025
1026 mutex_lock(&ipmi_interfaces_mutex);
1027 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
1028 if (intf->intf_num == if_num)
1029 goto found;
1030 }
1031 /* Not found, return an error */
1032 rv = -EINVAL;
1033 mutex_unlock(&ipmi_interfaces_mutex);
1034 return rv;
1035
1036found:
1037 handlers = intf->handlers;
1038 rv = -ENOSYS;
1039 if (handlers->get_smi_info)
1040 rv = handlers->get_smi_info(intf->send_info, data);
1041 mutex_unlock(&ipmi_interfaces_mutex);
1042
1043 return rv;
1044}
1045EXPORT_SYMBOL(ipmi_get_smi_info);
1046
Corey Minyard393d2cc2005-11-07 00:59:54 -08001047static void free_user(struct kref *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Corey Minyard393d2cc2005-11-07 00:59:54 -08001049 ipmi_user_t user = container_of(ref, struct ipmi_user, refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 kfree(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
1053int ipmi_destroy_user(ipmi_user_t user)
1054{
Corey Minyard393d2cc2005-11-07 00:59:54 -08001055 ipmi_smi_t intf = user->intf;
1056 int i;
1057 unsigned long flags;
1058 struct cmd_rcvr *rcvr;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001059 struct cmd_rcvr *rcvrs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Corey Minyard7aefac22014-04-14 09:46:56 -05001061 user->valid = false;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001062
Corey Minyard89986492014-04-14 09:46:54 -05001063 if (user->handler->ipmi_watchdog_pretimeout)
1064 atomic_dec(&intf->event_waiters);
1065
1066 if (user->gets_events)
1067 atomic_dec(&intf->event_waiters);
1068
Corey Minyard393d2cc2005-11-07 00:59:54 -08001069 /* Remove the user from the interface's sequence table. */
1070 spin_lock_irqsave(&intf->seq_lock, flags);
1071 list_del_rcu(&user->link);
1072
1073 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
1074 if (intf->seq_table[i].inuse
Corey Minyardc70d7492008-04-29 01:01:09 -07001075 && (intf->seq_table[i].recv_msg->user == user)) {
Corey Minyard393d2cc2005-11-07 00:59:54 -08001076 intf->seq_table[i].inuse = 0;
Corey Minyardb2c03942006-12-06 20:41:00 -08001077 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
Corey Minyard393d2cc2005-11-07 00:59:54 -08001078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08001080 spin_unlock_irqrestore(&intf->seq_lock, flags);
1081
1082 /*
1083 * Remove the user from the command receiver's table. First
1084 * we build a list of everything (not using the standard link,
1085 * since other things may be using it till we do
1086 * synchronize_rcu()) then free everything in that list.
1087 */
Corey Minyardd6dfd132006-03-31 02:30:41 -08001088 mutex_lock(&intf->cmd_rcvrs_mutex);
Paul E. McKenney066bb8d2006-01-06 00:19:53 -08001089 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
Corey Minyard393d2cc2005-11-07 00:59:54 -08001090 if (rcvr->user == user) {
1091 list_del_rcu(&rcvr->link);
1092 rcvr->next = rcvrs;
1093 rcvrs = rcvr;
1094 }
1095 }
Corey Minyardd6dfd132006-03-31 02:30:41 -08001096 mutex_unlock(&intf->cmd_rcvrs_mutex);
Corey Minyard393d2cc2005-11-07 00:59:54 -08001097 synchronize_rcu();
1098 while (rcvrs) {
1099 rcvr = rcvrs;
1100 rcvrs = rcvr->next;
1101 kfree(rcvr);
1102 }
1103
Corey Minyardb2c03942006-12-06 20:41:00 -08001104 mutex_lock(&ipmi_interfaces_mutex);
1105 if (intf->handlers) {
1106 module_put(intf->handlers->owner);
1107 if (intf->handlers->dec_usecount)
1108 intf->handlers->dec_usecount(intf->send_info);
1109 }
1110 mutex_unlock(&ipmi_interfaces_mutex);
Corey Minyard393d2cc2005-11-07 00:59:54 -08001111
1112 kref_put(&intf->refcount, intf_free);
1113
1114 kref_put(&user->refcount, free_user);
1115
Corey Minyard8a3628d2006-03-31 02:30:40 -08001116 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
Corey Minyardc70d7492008-04-29 01:01:09 -07001118EXPORT_SYMBOL(ipmi_destroy_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120void ipmi_get_version(ipmi_user_t user,
1121 unsigned char *major,
1122 unsigned char *minor)
1123{
Corey Minyardb2c03942006-12-06 20:41:00 -08001124 *major = user->intf->ipmi_version_major;
1125 *minor = user->intf->ipmi_version_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
Corey Minyardc70d7492008-04-29 01:01:09 -07001127EXPORT_SYMBOL(ipmi_get_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Corey Minyardc14979b2005-09-06 15:18:38 -07001129int ipmi_set_my_address(ipmi_user_t user,
1130 unsigned int channel,
1131 unsigned char address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
Corey Minyardc14979b2005-09-06 15:18:38 -07001133 if (channel >= IPMI_MAX_CHANNELS)
1134 return -EINVAL;
1135 user->intf->channels[channel].address = address;
1136 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
Corey Minyardc70d7492008-04-29 01:01:09 -07001138EXPORT_SYMBOL(ipmi_set_my_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Corey Minyardc14979b2005-09-06 15:18:38 -07001140int ipmi_get_my_address(ipmi_user_t user,
1141 unsigned int channel,
1142 unsigned char *address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Corey Minyardc14979b2005-09-06 15:18:38 -07001144 if (channel >= IPMI_MAX_CHANNELS)
1145 return -EINVAL;
1146 *address = user->intf->channels[channel].address;
1147 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
Corey Minyardc70d7492008-04-29 01:01:09 -07001149EXPORT_SYMBOL(ipmi_get_my_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Corey Minyardc14979b2005-09-06 15:18:38 -07001151int ipmi_set_my_LUN(ipmi_user_t user,
1152 unsigned int channel,
1153 unsigned char LUN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
Corey Minyardc14979b2005-09-06 15:18:38 -07001155 if (channel >= IPMI_MAX_CHANNELS)
1156 return -EINVAL;
1157 user->intf->channels[channel].lun = LUN & 0x3;
1158 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
Corey Minyardc70d7492008-04-29 01:01:09 -07001160EXPORT_SYMBOL(ipmi_set_my_LUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Corey Minyardc14979b2005-09-06 15:18:38 -07001162int ipmi_get_my_LUN(ipmi_user_t user,
1163 unsigned int channel,
1164 unsigned char *address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Corey Minyardc14979b2005-09-06 15:18:38 -07001166 if (channel >= IPMI_MAX_CHANNELS)
1167 return -EINVAL;
1168 *address = user->intf->channels[channel].lun;
1169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
Corey Minyardc70d7492008-04-29 01:01:09 -07001171EXPORT_SYMBOL(ipmi_get_my_LUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Corey Minyardb9675132006-12-06 20:41:02 -08001173int ipmi_get_maintenance_mode(ipmi_user_t user)
1174{
1175 int mode;
1176 unsigned long flags;
1177
1178 spin_lock_irqsave(&user->intf->maintenance_mode_lock, flags);
1179 mode = user->intf->maintenance_mode;
1180 spin_unlock_irqrestore(&user->intf->maintenance_mode_lock, flags);
1181
1182 return mode;
1183}
1184EXPORT_SYMBOL(ipmi_get_maintenance_mode);
1185
1186static void maintenance_mode_update(ipmi_smi_t intf)
1187{
1188 if (intf->handlers->set_maintenance_mode)
1189 intf->handlers->set_maintenance_mode(
1190 intf->send_info, intf->maintenance_mode_enable);
1191}
1192
1193int ipmi_set_maintenance_mode(ipmi_user_t user, int mode)
1194{
1195 int rv = 0;
1196 unsigned long flags;
1197 ipmi_smi_t intf = user->intf;
1198
1199 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1200 if (intf->maintenance_mode != mode) {
1201 switch (mode) {
1202 case IPMI_MAINTENANCE_MODE_AUTO:
Corey Minyardb9675132006-12-06 20:41:02 -08001203 intf->maintenance_mode_enable
1204 = (intf->auto_maintenance_timeout > 0);
1205 break;
1206
1207 case IPMI_MAINTENANCE_MODE_OFF:
Corey Minyard7aefac22014-04-14 09:46:56 -05001208 intf->maintenance_mode_enable = false;
Corey Minyardb9675132006-12-06 20:41:02 -08001209 break;
1210
1211 case IPMI_MAINTENANCE_MODE_ON:
Corey Minyard7aefac22014-04-14 09:46:56 -05001212 intf->maintenance_mode_enable = true;
Corey Minyardb9675132006-12-06 20:41:02 -08001213 break;
1214
1215 default:
1216 rv = -EINVAL;
1217 goto out_unlock;
1218 }
Corey Minyard7aefac22014-04-14 09:46:56 -05001219 intf->maintenance_mode = mode;
Corey Minyardb9675132006-12-06 20:41:02 -08001220
1221 maintenance_mode_update(intf);
1222 }
1223 out_unlock:
1224 spin_unlock_irqrestore(&intf->maintenance_mode_lock, flags);
1225
1226 return rv;
1227}
1228EXPORT_SYMBOL(ipmi_set_maintenance_mode);
1229
Corey Minyard89986492014-04-14 09:46:54 -05001230int ipmi_set_gets_events(ipmi_user_t user, bool val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Corey Minyard393d2cc2005-11-07 00:59:54 -08001232 unsigned long flags;
1233 ipmi_smi_t intf = user->intf;
1234 struct ipmi_recv_msg *msg, *msg2;
1235 struct list_head msgs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Corey Minyard393d2cc2005-11-07 00:59:54 -08001237 INIT_LIST_HEAD(&msgs);
1238
1239 spin_lock_irqsave(&intf->events_lock, flags);
Corey Minyard89986492014-04-14 09:46:54 -05001240 if (user->gets_events == val)
1241 goto out;
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 user->gets_events = val;
1244
Corey Minyard89986492014-04-14 09:46:54 -05001245 if (val) {
1246 if (atomic_inc_return(&intf->event_waiters) == 1)
1247 need_waiter(intf);
1248 } else {
1249 atomic_dec(&intf->event_waiters);
1250 }
1251
Corey Minyardb2c03942006-12-06 20:41:00 -08001252 if (intf->delivering_events)
1253 /*
1254 * Another thread is delivering events for this, so
1255 * let it handle any new events.
1256 */
1257 goto out;
1258
1259 /* Deliver any queued events. */
1260 while (user->gets_events && !list_empty(&intf->waiting_events)) {
Akinobu Mita179e0912006-06-26 00:24:41 -07001261 list_for_each_entry_safe(msg, msg2, &intf->waiting_events, link)
1262 list_move_tail(&msg->link, &msgs);
Corey Minyard4791c032006-04-10 22:54:31 -07001263 intf->waiting_events_count = 0;
Corey Minyard87ebd062008-04-29 01:01:04 -07001264 if (intf->event_msg_printed) {
1265 printk(KERN_WARNING PFX "Event queue no longer"
1266 " full\n");
1267 intf->event_msg_printed = 0;
1268 }
Corey Minyardb2c03942006-12-06 20:41:00 -08001269
1270 intf->delivering_events = 1;
1271 spin_unlock_irqrestore(&intf->events_lock, flags);
1272
1273 list_for_each_entry_safe(msg, msg2, &msgs, link) {
1274 msg->user = user;
1275 kref_get(&user->refcount);
1276 deliver_response(msg);
1277 }
1278
1279 spin_lock_irqsave(&intf->events_lock, flags);
1280 intf->delivering_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08001282
Corey Minyardb2c03942006-12-06 20:41:00 -08001283 out:
Corey Minyard393d2cc2005-11-07 00:59:54 -08001284 spin_unlock_irqrestore(&intf->events_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 return 0;
1287}
Corey Minyardc70d7492008-04-29 01:01:09 -07001288EXPORT_SYMBOL(ipmi_set_gets_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Corey Minyard393d2cc2005-11-07 00:59:54 -08001290static struct cmd_rcvr *find_cmd_rcvr(ipmi_smi_t intf,
1291 unsigned char netfn,
Corey Minyardc69c3122006-09-30 23:27:56 -07001292 unsigned char cmd,
1293 unsigned char chan)
Corey Minyard393d2cc2005-11-07 00:59:54 -08001294{
1295 struct cmd_rcvr *rcvr;
1296
1297 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
Corey Minyardc69c3122006-09-30 23:27:56 -07001298 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1299 && (rcvr->chans & (1 << chan)))
Corey Minyard393d2cc2005-11-07 00:59:54 -08001300 return rcvr;
1301 }
1302 return NULL;
1303}
1304
Corey Minyardc69c3122006-09-30 23:27:56 -07001305static int is_cmd_rcvr_exclusive(ipmi_smi_t intf,
1306 unsigned char netfn,
1307 unsigned char cmd,
1308 unsigned int chans)
1309{
1310 struct cmd_rcvr *rcvr;
1311
1312 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1313 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1314 && (rcvr->chans & chans))
1315 return 0;
1316 }
1317 return 1;
1318}
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320int ipmi_register_for_cmd(ipmi_user_t user,
1321 unsigned char netfn,
Corey Minyardc69c3122006-09-30 23:27:56 -07001322 unsigned char cmd,
1323 unsigned int chans)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Corey Minyard393d2cc2005-11-07 00:59:54 -08001325 ipmi_smi_t intf = user->intf;
1326 struct cmd_rcvr *rcvr;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001327 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329
1330 rcvr = kmalloc(sizeof(*rcvr), GFP_KERNEL);
Corey Minyard8a3628d2006-03-31 02:30:40 -08001331 if (!rcvr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return -ENOMEM;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001333 rcvr->cmd = cmd;
1334 rcvr->netfn = netfn;
Corey Minyardc69c3122006-09-30 23:27:56 -07001335 rcvr->chans = chans;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001336 rcvr->user = user;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Corey Minyardd6dfd132006-03-31 02:30:41 -08001338 mutex_lock(&intf->cmd_rcvrs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 /* Make sure the command/netfn is not already registered. */
Corey Minyardc69c3122006-09-30 23:27:56 -07001340 if (!is_cmd_rcvr_exclusive(intf, netfn, cmd, chans)) {
Corey Minyard393d2cc2005-11-07 00:59:54 -08001341 rv = -EBUSY;
1342 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344
Corey Minyard89986492014-04-14 09:46:54 -05001345 if (atomic_inc_return(&intf->event_waiters) == 1)
1346 need_waiter(intf);
1347
Corey Minyard393d2cc2005-11-07 00:59:54 -08001348 list_add_rcu(&rcvr->link, &intf->cmd_rcvrs);
Corey Minyard877197e2005-09-06 15:18:45 -07001349
Corey Minyard393d2cc2005-11-07 00:59:54 -08001350 out_unlock:
Corey Minyardd6dfd132006-03-31 02:30:41 -08001351 mutex_unlock(&intf->cmd_rcvrs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (rv)
1353 kfree(rcvr);
1354
1355 return rv;
1356}
Corey Minyardc70d7492008-04-29 01:01:09 -07001357EXPORT_SYMBOL(ipmi_register_for_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359int ipmi_unregister_for_cmd(ipmi_user_t user,
1360 unsigned char netfn,
Corey Minyardc69c3122006-09-30 23:27:56 -07001361 unsigned char cmd,
1362 unsigned int chans)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
Corey Minyard393d2cc2005-11-07 00:59:54 -08001364 ipmi_smi_t intf = user->intf;
1365 struct cmd_rcvr *rcvr;
Corey Minyardc69c3122006-09-30 23:27:56 -07001366 struct cmd_rcvr *rcvrs = NULL;
1367 int i, rv = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Corey Minyardd6dfd132006-03-31 02:30:41 -08001369 mutex_lock(&intf->cmd_rcvrs_mutex);
Corey Minyardc69c3122006-09-30 23:27:56 -07001370 for (i = 0; i < IPMI_NUM_CHANNELS; i++) {
1371 if (((1 << i) & chans) == 0)
1372 continue;
1373 rcvr = find_cmd_rcvr(intf, netfn, cmd, i);
1374 if (rcvr == NULL)
1375 continue;
1376 if (rcvr->user == user) {
1377 rv = 0;
1378 rcvr->chans &= ~chans;
1379 if (rcvr->chans == 0) {
1380 list_del_rcu(&rcvr->link);
1381 rcvr->next = rcvrs;
1382 rcvrs = rcvr;
1383 }
1384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
Corey Minyardc69c3122006-09-30 23:27:56 -07001386 mutex_unlock(&intf->cmd_rcvrs_mutex);
1387 synchronize_rcu();
1388 while (rcvrs) {
Corey Minyard89986492014-04-14 09:46:54 -05001389 atomic_dec(&intf->event_waiters);
Corey Minyardc69c3122006-09-30 23:27:56 -07001390 rcvr = rcvrs;
1391 rcvrs = rcvr->next;
1392 kfree(rcvr);
1393 }
1394 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
Corey Minyardc70d7492008-04-29 01:01:09 -07001396EXPORT_SYMBOL(ipmi_unregister_for_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398static unsigned char
1399ipmb_checksum(unsigned char *data, int size)
1400{
1401 unsigned char csum = 0;
Corey Minyardc70d7492008-04-29 01:01:09 -07001402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 for (; size > 0; size--, data++)
1404 csum += *data;
1405
1406 return -csum;
1407}
1408
1409static inline void format_ipmb_msg(struct ipmi_smi_msg *smi_msg,
1410 struct kernel_ipmi_msg *msg,
1411 struct ipmi_ipmb_addr *ipmb_addr,
1412 long msgid,
1413 unsigned char ipmb_seq,
1414 int broadcast,
1415 unsigned char source_address,
1416 unsigned char source_lun)
1417{
1418 int i = broadcast;
1419
1420 /* Format the IPMB header data. */
1421 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1422 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1423 smi_msg->data[2] = ipmb_addr->channel;
1424 if (broadcast)
1425 smi_msg->data[3] = 0;
1426 smi_msg->data[i+3] = ipmb_addr->slave_addr;
1427 smi_msg->data[i+4] = (msg->netfn << 2) | (ipmb_addr->lun & 0x3);
1428 smi_msg->data[i+5] = ipmb_checksum(&(smi_msg->data[i+3]), 2);
1429 smi_msg->data[i+6] = source_address;
1430 smi_msg->data[i+7] = (ipmb_seq << 2) | source_lun;
1431 smi_msg->data[i+8] = msg->cmd;
1432
1433 /* Now tack on the data to the message. */
1434 if (msg->data_len > 0)
1435 memcpy(&(smi_msg->data[i+9]), msg->data,
1436 msg->data_len);
1437 smi_msg->data_size = msg->data_len + 9;
1438
1439 /* Now calculate the checksum and tack it on. */
1440 smi_msg->data[i+smi_msg->data_size]
1441 = ipmb_checksum(&(smi_msg->data[i+6]),
1442 smi_msg->data_size-6);
1443
Corey Minyardc70d7492008-04-29 01:01:09 -07001444 /*
1445 * Add on the checksum size and the offset from the
1446 * broadcast.
1447 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 smi_msg->data_size += 1 + i;
1449
1450 smi_msg->msgid = msgid;
1451}
1452
1453static inline void format_lan_msg(struct ipmi_smi_msg *smi_msg,
1454 struct kernel_ipmi_msg *msg,
1455 struct ipmi_lan_addr *lan_addr,
1456 long msgid,
1457 unsigned char ipmb_seq,
1458 unsigned char source_lun)
1459{
1460 /* Format the IPMB header data. */
1461 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1462 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1463 smi_msg->data[2] = lan_addr->channel;
1464 smi_msg->data[3] = lan_addr->session_handle;
1465 smi_msg->data[4] = lan_addr->remote_SWID;
1466 smi_msg->data[5] = (msg->netfn << 2) | (lan_addr->lun & 0x3);
1467 smi_msg->data[6] = ipmb_checksum(&(smi_msg->data[4]), 2);
1468 smi_msg->data[7] = lan_addr->local_SWID;
1469 smi_msg->data[8] = (ipmb_seq << 2) | source_lun;
1470 smi_msg->data[9] = msg->cmd;
1471
1472 /* Now tack on the data to the message. */
1473 if (msg->data_len > 0)
1474 memcpy(&(smi_msg->data[10]), msg->data,
1475 msg->data_len);
1476 smi_msg->data_size = msg->data_len + 10;
1477
1478 /* Now calculate the checksum and tack it on. */
1479 smi_msg->data[smi_msg->data_size]
1480 = ipmb_checksum(&(smi_msg->data[7]),
1481 smi_msg->data_size-7);
1482
Corey Minyardc70d7492008-04-29 01:01:09 -07001483 /*
1484 * Add on the checksum size and the offset from the
1485 * broadcast.
1486 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 smi_msg->data_size += 1;
1488
1489 smi_msg->msgid = msgid;
1490}
1491
Arnd Bergmann191cc412015-01-28 16:00:11 +01001492static struct ipmi_smi_msg *smi_add_send_msg(ipmi_smi_t intf,
1493 struct ipmi_smi_msg *smi_msg,
1494 int priority)
Corey Minyard7f4a1c82014-11-06 20:52:24 -06001495{
Corey Minyard7ea0ed22014-11-06 16:58:48 -06001496 if (intf->curr_msg) {
1497 if (priority > 0)
1498 list_add_tail(&smi_msg->link, &intf->hp_xmit_msgs);
1499 else
1500 list_add_tail(&smi_msg->link, &intf->xmit_msgs);
1501 smi_msg = NULL;
1502 } else {
1503 intf->curr_msg = smi_msg;
1504 }
Arnd Bergmann191cc412015-01-28 16:00:11 +01001505
1506 return smi_msg;
1507}
1508
1509
Corey Minyard81d02b72015-06-13 10:34:25 -05001510static void smi_send(ipmi_smi_t intf, const struct ipmi_smi_handlers *handlers,
Arnd Bergmann191cc412015-01-28 16:00:11 +01001511 struct ipmi_smi_msg *smi_msg, int priority)
1512{
1513 int run_to_completion = intf->run_to_completion;
1514
1515 if (run_to_completion) {
1516 smi_msg = smi_add_send_msg(intf, smi_msg, priority);
1517 } else {
1518 unsigned long flags;
1519
1520 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
1521 smi_msg = smi_add_send_msg(intf, smi_msg, priority);
Corey Minyard7ea0ed22014-11-06 16:58:48 -06001522 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
Arnd Bergmann191cc412015-01-28 16:00:11 +01001523 }
Corey Minyard7ea0ed22014-11-06 16:58:48 -06001524
1525 if (smi_msg)
Corey Minyard99ab32f2014-11-07 07:57:31 -06001526 handlers->sender(intf->send_info, smi_msg);
Corey Minyard7f4a1c82014-11-06 20:52:24 -06001527}
1528
Corey Minyardc70d7492008-04-29 01:01:09 -07001529/*
1530 * Separate from ipmi_request so that the user does not have to be
1531 * supplied in certain circumstances (mainly at panic time). If
1532 * messages are supplied, they will be freed, even if an error
1533 * occurs.
1534 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08001535static int i_ipmi_request(ipmi_user_t user,
1536 ipmi_smi_t intf,
1537 struct ipmi_addr *addr,
1538 long msgid,
1539 struct kernel_ipmi_msg *msg,
1540 void *user_msg_data,
1541 void *supplied_smi,
1542 struct ipmi_recv_msg *supplied_recv,
1543 int priority,
1544 unsigned char source_address,
1545 unsigned char source_lun,
1546 int retries,
1547 unsigned int retry_time_ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Corey Minyardb2c03942006-12-06 20:41:00 -08001549 int rv = 0;
1550 struct ipmi_smi_msg *smi_msg;
1551 struct ipmi_recv_msg *recv_msg;
1552 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554
Corey Minyardc70d7492008-04-29 01:01:09 -07001555 if (supplied_recv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 recv_msg = supplied_recv;
Corey Minyardc70d7492008-04-29 01:01:09 -07001557 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 recv_msg = ipmi_alloc_recv_msg();
Corey Minyardc70d7492008-04-29 01:01:09 -07001559 if (recv_msg == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 }
1562 recv_msg->user_msg_data = user_msg_data;
1563
Corey Minyardc70d7492008-04-29 01:01:09 -07001564 if (supplied_smi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 smi_msg = (struct ipmi_smi_msg *) supplied_smi;
Corey Minyardc70d7492008-04-29 01:01:09 -07001566 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 smi_msg = ipmi_alloc_smi_msg();
1568 if (smi_msg == NULL) {
1569 ipmi_free_recv_msg(recv_msg);
1570 return -ENOMEM;
1571 }
1572 }
1573
Corey Minyardb2c03942006-12-06 20:41:00 -08001574 rcu_read_lock();
Corey Minyard7ea0ed22014-11-06 16:58:48 -06001575 if (intf->in_shutdown) {
Corey Minyardb2c03942006-12-06 20:41:00 -08001576 rv = -ENODEV;
1577 goto out_err;
1578 }
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 recv_msg->user = user;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001581 if (user)
1582 kref_get(&user->refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 recv_msg->msgid = msgid;
Corey Minyardc70d7492008-04-29 01:01:09 -07001584 /*
1585 * Store the message to send in the receive message so timeout
1586 * responses can get the proper response data.
1587 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 recv_msg->msg = *msg;
1589
1590 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
1591 struct ipmi_system_interface_addr *smi_addr;
1592
1593 if (msg->netfn & 1) {
1594 /* Responses are not allowed to the SMI. */
1595 rv = -EINVAL;
1596 goto out_err;
1597 }
1598
1599 smi_addr = (struct ipmi_system_interface_addr *) addr;
1600 if (smi_addr->lun > 3) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001601 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 rv = -EINVAL;
1603 goto out_err;
1604 }
1605
1606 memcpy(&recv_msg->addr, smi_addr, sizeof(*smi_addr));
1607
1608 if ((msg->netfn == IPMI_NETFN_APP_REQUEST)
1609 && ((msg->cmd == IPMI_SEND_MSG_CMD)
1610 || (msg->cmd == IPMI_GET_MSG_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07001611 || (msg->cmd == IPMI_READ_EVENT_MSG_BUFFER_CMD))) {
1612 /*
1613 * We don't let the user do these, since we manage
1614 * the sequence numbers.
1615 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001616 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 rv = -EINVAL;
1618 goto out_err;
1619 }
1620
Corey Minyardb9675132006-12-06 20:41:02 -08001621 if (((msg->netfn == IPMI_NETFN_APP_REQUEST)
1622 && ((msg->cmd == IPMI_COLD_RESET_CMD)
1623 || (msg->cmd == IPMI_WARM_RESET_CMD)))
Corey Minyardc70d7492008-04-29 01:01:09 -07001624 || (msg->netfn == IPMI_NETFN_FIRMWARE_REQUEST)) {
Corey Minyardb9675132006-12-06 20:41:02 -08001625 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1626 intf->auto_maintenance_timeout
1627 = IPMI_MAINTENANCE_MODE_TIMEOUT;
1628 if (!intf->maintenance_mode
Corey Minyardc70d7492008-04-29 01:01:09 -07001629 && !intf->maintenance_mode_enable) {
Corey Minyard7aefac22014-04-14 09:46:56 -05001630 intf->maintenance_mode_enable = true;
Corey Minyardb9675132006-12-06 20:41:02 -08001631 maintenance_mode_update(intf);
1632 }
1633 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
1634 flags);
1635 }
1636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 if ((msg->data_len + 2) > IPMI_MAX_MSG_LENGTH) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001638 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 rv = -EMSGSIZE;
1640 goto out_err;
1641 }
1642
1643 smi_msg->data[0] = (msg->netfn << 2) | (smi_addr->lun & 0x3);
1644 smi_msg->data[1] = msg->cmd;
1645 smi_msg->msgid = msgid;
1646 smi_msg->user_data = recv_msg;
1647 if (msg->data_len > 0)
1648 memcpy(&(smi_msg->data[2]), msg->data, msg->data_len);
1649 smi_msg->data_size = msg->data_len + 2;
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001650 ipmi_inc_stat(intf, sent_local_commands);
Corey Minyard25176ed2009-04-21 12:24:04 -07001651 } else if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 struct ipmi_ipmb_addr *ipmb_addr;
1653 unsigned char ipmb_seq;
1654 long seqid;
1655 int broadcast = 0;
1656
KAMBAROV, ZAUR9c101fd2005-06-28 20:45:08 -07001657 if (addr->channel >= IPMI_MAX_CHANNELS) {
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 (intf->channels[addr->channel].medium
Corey Minyardc70d7492008-04-29 01:01:09 -07001664 != IPMI_CHANNEL_MEDIUM_IPMB) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001665 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 rv = -EINVAL;
1667 goto out_err;
1668 }
1669
1670 if (retries < 0) {
1671 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE)
1672 retries = 0; /* Don't retry broadcasts. */
1673 else
1674 retries = 4;
1675 }
1676 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001677 /*
1678 * Broadcasts add a zero at the beginning of the
1679 * message, but otherwise is the same as an IPMB
1680 * address.
1681 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 addr->addr_type = IPMI_IPMB_ADDR_TYPE;
1683 broadcast = 1;
1684 }
1685
1686
1687 /* Default to 1 second retries. */
1688 if (retry_time_ms == 0)
1689 retry_time_ms = 1000;
1690
Corey Minyardc70d7492008-04-29 01:01:09 -07001691 /*
1692 * 9 for the header and 1 for the checksum, plus
1693 * possibly one for the broadcast.
1694 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 if ((msg->data_len + 10 + broadcast) > IPMI_MAX_MSG_LENGTH) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001696 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 rv = -EMSGSIZE;
1698 goto out_err;
1699 }
1700
1701 ipmb_addr = (struct ipmi_ipmb_addr *) addr;
1702 if (ipmb_addr->lun > 3) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001703 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 rv = -EINVAL;
1705 goto out_err;
1706 }
1707
1708 memcpy(&recv_msg->addr, ipmb_addr, sizeof(*ipmb_addr));
1709
1710 if (recv_msg->msg.netfn & 0x1) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001711 /*
1712 * It's a response, so use the user's sequence
1713 * from msgid.
1714 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001715 ipmi_inc_stat(intf, sent_ipmb_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 format_ipmb_msg(smi_msg, msg, ipmb_addr, msgid,
1717 msgid, broadcast,
1718 source_address, source_lun);
1719
Corey Minyardc70d7492008-04-29 01:01:09 -07001720 /*
1721 * Save the receive message so we can use it
1722 * to deliver the response.
1723 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 smi_msg->user_data = recv_msg;
1725 } else {
1726 /* It's a command, so get a sequence for it. */
1727
1728 spin_lock_irqsave(&(intf->seq_lock), flags);
1729
Corey Minyardc70d7492008-04-29 01:01:09 -07001730 /*
1731 * Create a sequence number with a 1 second
1732 * timeout and 4 retries.
1733 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 rv = intf_next_seq(intf,
1735 recv_msg,
1736 retry_time_ms,
1737 retries,
1738 broadcast,
1739 &ipmb_seq,
1740 &seqid);
1741 if (rv) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001742 /*
1743 * We have used up all the sequence numbers,
1744 * probably, so abort.
1745 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 spin_unlock_irqrestore(&(intf->seq_lock),
1747 flags);
1748 goto out_err;
1749 }
1750
Corey Minyard25176ed2009-04-21 12:24:04 -07001751 ipmi_inc_stat(intf, sent_ipmb_commands);
1752
Corey Minyardc70d7492008-04-29 01:01:09 -07001753 /*
1754 * Store the sequence number in the message,
1755 * so that when the send message response
1756 * comes back we can start the timer.
1757 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 format_ipmb_msg(smi_msg, msg, ipmb_addr,
1759 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1760 ipmb_seq, broadcast,
1761 source_address, source_lun);
1762
Corey Minyardc70d7492008-04-29 01:01:09 -07001763 /*
1764 * Copy the message into the recv message data, so we
1765 * can retransmit it later if necessary.
1766 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 memcpy(recv_msg->msg_data, smi_msg->data,
1768 smi_msg->data_size);
1769 recv_msg->msg.data = recv_msg->msg_data;
1770 recv_msg->msg.data_len = smi_msg->data_size;
1771
Corey Minyardc70d7492008-04-29 01:01:09 -07001772 /*
1773 * We don't unlock until here, because we need
1774 * to copy the completed message into the
1775 * recv_msg before we release the lock.
1776 * Otherwise, race conditions may bite us. I
1777 * know that's pretty paranoid, but I prefer
1778 * to be correct.
1779 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1781 }
Corey Minyard25176ed2009-04-21 12:24:04 -07001782 } else if (is_lan_addr(addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 struct ipmi_lan_addr *lan_addr;
1784 unsigned char ipmb_seq;
1785 long seqid;
1786
Jayachandran C12fc1d72006-02-03 03:04:51 -08001787 if (addr->channel >= IPMI_MAX_CHANNELS) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001788 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 rv = -EINVAL;
1790 goto out_err;
1791 }
1792
1793 if ((intf->channels[addr->channel].medium
Corey Minyardc70d7492008-04-29 01:01:09 -07001794 != IPMI_CHANNEL_MEDIUM_8023LAN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 && (intf->channels[addr->channel].medium
Corey Minyardc70d7492008-04-29 01:01:09 -07001796 != IPMI_CHANNEL_MEDIUM_ASYNC)) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001797 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 rv = -EINVAL;
1799 goto out_err;
1800 }
1801
1802 retries = 4;
1803
1804 /* Default to 1 second retries. */
1805 if (retry_time_ms == 0)
1806 retry_time_ms = 1000;
1807
1808 /* 11 for the header and 1 for the checksum. */
1809 if ((msg->data_len + 12) > IPMI_MAX_MSG_LENGTH) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001810 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 rv = -EMSGSIZE;
1812 goto out_err;
1813 }
1814
1815 lan_addr = (struct ipmi_lan_addr *) addr;
1816 if (lan_addr->lun > 3) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001817 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 rv = -EINVAL;
1819 goto out_err;
1820 }
1821
1822 memcpy(&recv_msg->addr, lan_addr, sizeof(*lan_addr));
1823
1824 if (recv_msg->msg.netfn & 0x1) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001825 /*
1826 * It's a response, so use the user's sequence
1827 * from msgid.
1828 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001829 ipmi_inc_stat(intf, sent_lan_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 format_lan_msg(smi_msg, msg, lan_addr, msgid,
1831 msgid, source_lun);
1832
Corey Minyardc70d7492008-04-29 01:01:09 -07001833 /*
1834 * Save the receive message so we can use it
1835 * to deliver the response.
1836 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 smi_msg->user_data = recv_msg;
1838 } else {
1839 /* It's a command, so get a sequence for it. */
1840
1841 spin_lock_irqsave(&(intf->seq_lock), flags);
1842
Corey Minyardc70d7492008-04-29 01:01:09 -07001843 /*
1844 * Create a sequence number with a 1 second
1845 * timeout and 4 retries.
1846 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 rv = intf_next_seq(intf,
1848 recv_msg,
1849 retry_time_ms,
1850 retries,
1851 0,
1852 &ipmb_seq,
1853 &seqid);
1854 if (rv) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001855 /*
1856 * We have used up all the sequence numbers,
1857 * probably, so abort.
1858 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 spin_unlock_irqrestore(&(intf->seq_lock),
1860 flags);
1861 goto out_err;
1862 }
1863
Corey Minyard25176ed2009-04-21 12:24:04 -07001864 ipmi_inc_stat(intf, sent_lan_commands);
1865
Corey Minyardc70d7492008-04-29 01:01:09 -07001866 /*
1867 * Store the sequence number in the message,
1868 * so that when the send message response
1869 * comes back we can start the timer.
1870 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 format_lan_msg(smi_msg, msg, lan_addr,
1872 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1873 ipmb_seq, source_lun);
1874
Corey Minyardc70d7492008-04-29 01:01:09 -07001875 /*
1876 * Copy the message into the recv message data, so we
1877 * can retransmit it later if necessary.
1878 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 memcpy(recv_msg->msg_data, smi_msg->data,
1880 smi_msg->data_size);
1881 recv_msg->msg.data = recv_msg->msg_data;
1882 recv_msg->msg.data_len = smi_msg->data_size;
1883
Corey Minyardc70d7492008-04-29 01:01:09 -07001884 /*
1885 * We don't unlock until here, because we need
1886 * to copy the completed message into the
1887 * recv_msg before we release the lock.
1888 * Otherwise, race conditions may bite us. I
1889 * know that's pretty paranoid, but I prefer
1890 * to be correct.
1891 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1893 }
1894 } else {
1895 /* Unknown address type. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001896 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 rv = -EINVAL;
1898 goto out_err;
1899 }
1900
1901#ifdef DEBUG_MSGING
1902 {
1903 int m;
Corey Minyarde8b33612005-09-06 15:18:45 -07001904 for (m = 0; m < smi_msg->data_size; m++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 printk(" %2.2x", smi_msg->data[m]);
1906 printk("\n");
1907 }
1908#endif
Corey Minyardb2c03942006-12-06 20:41:00 -08001909
Corey Minyard7ea0ed22014-11-06 16:58:48 -06001910 smi_send(intf, intf->handlers, smi_msg, priority);
Corey Minyardb2c03942006-12-06 20:41:00 -08001911 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
1913 return 0;
1914
1915 out_err:
Corey Minyardb2c03942006-12-06 20:41:00 -08001916 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 ipmi_free_smi_msg(smi_msg);
1918 ipmi_free_recv_msg(recv_msg);
1919 return rv;
1920}
1921
Corey Minyardc14979b2005-09-06 15:18:38 -07001922static int check_addr(ipmi_smi_t intf,
1923 struct ipmi_addr *addr,
1924 unsigned char *saddr,
1925 unsigned char *lun)
1926{
1927 if (addr->channel >= IPMI_MAX_CHANNELS)
1928 return -EINVAL;
1929 *lun = intf->channels[addr->channel].lun;
1930 *saddr = intf->channels[addr->channel].address;
1931 return 0;
1932}
1933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934int ipmi_request_settime(ipmi_user_t user,
1935 struct ipmi_addr *addr,
1936 long msgid,
1937 struct kernel_ipmi_msg *msg,
1938 void *user_msg_data,
1939 int priority,
1940 int retries,
1941 unsigned int retry_time_ms)
1942{
Corey Minyardf0ba9392013-09-05 06:36:34 -05001943 unsigned char saddr = 0, lun = 0;
Corey Minyardc14979b2005-09-06 15:18:38 -07001944 int rv;
1945
Corey Minyard8a3628d2006-03-31 02:30:40 -08001946 if (!user)
Corey Minyard56a55ec2005-09-06 15:18:42 -07001947 return -EINVAL;
Corey Minyardc14979b2005-09-06 15:18:38 -07001948 rv = check_addr(user->intf, addr, &saddr, &lun);
1949 if (rv)
1950 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 return i_ipmi_request(user,
1952 user->intf,
1953 addr,
1954 msgid,
1955 msg,
1956 user_msg_data,
1957 NULL, NULL,
1958 priority,
Corey Minyardc14979b2005-09-06 15:18:38 -07001959 saddr,
1960 lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 retries,
1962 retry_time_ms);
1963}
Corey Minyardc70d7492008-04-29 01:01:09 -07001964EXPORT_SYMBOL(ipmi_request_settime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966int ipmi_request_supply_msgs(ipmi_user_t user,
1967 struct ipmi_addr *addr,
1968 long msgid,
1969 struct kernel_ipmi_msg *msg,
1970 void *user_msg_data,
1971 void *supplied_smi,
1972 struct ipmi_recv_msg *supplied_recv,
1973 int priority)
1974{
Corey Minyard9ebca932012-10-16 15:53:39 -05001975 unsigned char saddr = 0, lun = 0;
Corey Minyardc14979b2005-09-06 15:18:38 -07001976 int rv;
1977
Corey Minyard8a3628d2006-03-31 02:30:40 -08001978 if (!user)
Corey Minyard56a55ec2005-09-06 15:18:42 -07001979 return -EINVAL;
Corey Minyardc14979b2005-09-06 15:18:38 -07001980 rv = check_addr(user->intf, addr, &saddr, &lun);
1981 if (rv)
1982 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 return i_ipmi_request(user,
1984 user->intf,
1985 addr,
1986 msgid,
1987 msg,
1988 user_msg_data,
1989 supplied_smi,
1990 supplied_recv,
1991 priority,
Corey Minyardc14979b2005-09-06 15:18:38 -07001992 saddr,
1993 lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 -1, 0);
1995}
Corey Minyardc70d7492008-04-29 01:01:09 -07001996EXPORT_SYMBOL(ipmi_request_supply_msgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Randy Dunlap1aa16ee2006-12-06 20:41:20 -08001998#ifdef CONFIG_PROC_FS
Alexey Dobriyan07412732011-05-26 16:25:55 -07001999static int smi_ipmb_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
Alexey Dobriyan07412732011-05-26 16:25:55 -07002001 ipmi_smi_t intf = m->private;
Corey Minyardc14979b2005-09-06 15:18:38 -07002002 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Alexey Dobriyan07412732011-05-26 16:25:55 -07002004 seq_printf(m, "%x", intf->channels[0].address);
2005 for (i = 1; i < IPMI_MAX_CHANNELS; i++)
2006 seq_printf(m, " %x", intf->channels[i].address);
Joe Perchesd6c5dc12015-02-17 11:10:56 -08002007 seq_putc(m, '\n');
2008
Joe Perches5e33cd02015-02-22 10:21:07 -08002009 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010}
2011
Alexey Dobriyan07412732011-05-26 16:25:55 -07002012static int smi_ipmb_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
Al Virod9dda782013-03-31 18:16:14 -04002014 return single_open(file, smi_ipmb_proc_show, PDE_DATA(inode));
Alexey Dobriyan07412732011-05-26 16:25:55 -07002015}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Alexey Dobriyan07412732011-05-26 16:25:55 -07002017static const struct file_operations smi_ipmb_proc_ops = {
2018 .open = smi_ipmb_proc_open,
2019 .read = seq_read,
2020 .llseek = seq_lseek,
2021 .release = single_release,
2022};
2023
2024static int smi_version_proc_show(struct seq_file *m, void *v)
2025{
2026 ipmi_smi_t intf = m->private;
2027
Joe Perchesd6c5dc12015-02-17 11:10:56 -08002028 seq_printf(m, "%u.%u\n",
2029 ipmi_version_major(&intf->bmc->id),
2030 ipmi_version_minor(&intf->bmc->id));
2031
Joe Perches5e33cd02015-02-22 10:21:07 -08002032 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033}
2034
Alexey Dobriyan07412732011-05-26 16:25:55 -07002035static int smi_version_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
Al Virod9dda782013-03-31 18:16:14 -04002037 return single_open(file, smi_version_proc_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
Alexey Dobriyan07412732011-05-26 16:25:55 -07002039
2040static const struct file_operations smi_version_proc_ops = {
2041 .open = smi_version_proc_open,
2042 .read = seq_read,
2043 .llseek = seq_lseek,
2044 .release = single_release,
2045};
2046
2047static int smi_stats_proc_show(struct seq_file *m, void *v)
2048{
2049 ipmi_smi_t intf = m->private;
2050
2051 seq_printf(m, "sent_invalid_commands: %u\n",
2052 ipmi_get_stat(intf, sent_invalid_commands));
2053 seq_printf(m, "sent_local_commands: %u\n",
2054 ipmi_get_stat(intf, sent_local_commands));
2055 seq_printf(m, "handled_local_responses: %u\n",
2056 ipmi_get_stat(intf, handled_local_responses));
2057 seq_printf(m, "unhandled_local_responses: %u\n",
2058 ipmi_get_stat(intf, unhandled_local_responses));
2059 seq_printf(m, "sent_ipmb_commands: %u\n",
2060 ipmi_get_stat(intf, sent_ipmb_commands));
2061 seq_printf(m, "sent_ipmb_command_errs: %u\n",
2062 ipmi_get_stat(intf, sent_ipmb_command_errs));
2063 seq_printf(m, "retransmitted_ipmb_commands: %u\n",
2064 ipmi_get_stat(intf, retransmitted_ipmb_commands));
2065 seq_printf(m, "timed_out_ipmb_commands: %u\n",
2066 ipmi_get_stat(intf, timed_out_ipmb_commands));
2067 seq_printf(m, "timed_out_ipmb_broadcasts: %u\n",
2068 ipmi_get_stat(intf, timed_out_ipmb_broadcasts));
2069 seq_printf(m, "sent_ipmb_responses: %u\n",
2070 ipmi_get_stat(intf, sent_ipmb_responses));
2071 seq_printf(m, "handled_ipmb_responses: %u\n",
2072 ipmi_get_stat(intf, handled_ipmb_responses));
2073 seq_printf(m, "invalid_ipmb_responses: %u\n",
2074 ipmi_get_stat(intf, invalid_ipmb_responses));
2075 seq_printf(m, "unhandled_ipmb_responses: %u\n",
2076 ipmi_get_stat(intf, unhandled_ipmb_responses));
2077 seq_printf(m, "sent_lan_commands: %u\n",
2078 ipmi_get_stat(intf, sent_lan_commands));
2079 seq_printf(m, "sent_lan_command_errs: %u\n",
2080 ipmi_get_stat(intf, sent_lan_command_errs));
2081 seq_printf(m, "retransmitted_lan_commands: %u\n",
2082 ipmi_get_stat(intf, retransmitted_lan_commands));
2083 seq_printf(m, "timed_out_lan_commands: %u\n",
2084 ipmi_get_stat(intf, timed_out_lan_commands));
2085 seq_printf(m, "sent_lan_responses: %u\n",
2086 ipmi_get_stat(intf, sent_lan_responses));
2087 seq_printf(m, "handled_lan_responses: %u\n",
2088 ipmi_get_stat(intf, handled_lan_responses));
2089 seq_printf(m, "invalid_lan_responses: %u\n",
2090 ipmi_get_stat(intf, invalid_lan_responses));
2091 seq_printf(m, "unhandled_lan_responses: %u\n",
2092 ipmi_get_stat(intf, unhandled_lan_responses));
2093 seq_printf(m, "handled_commands: %u\n",
2094 ipmi_get_stat(intf, handled_commands));
2095 seq_printf(m, "invalid_commands: %u\n",
2096 ipmi_get_stat(intf, invalid_commands));
2097 seq_printf(m, "unhandled_commands: %u\n",
2098 ipmi_get_stat(intf, unhandled_commands));
2099 seq_printf(m, "invalid_events: %u\n",
2100 ipmi_get_stat(intf, invalid_events));
2101 seq_printf(m, "events: %u\n",
2102 ipmi_get_stat(intf, events));
2103 seq_printf(m, "failed rexmit LAN msgs: %u\n",
2104 ipmi_get_stat(intf, dropped_rexmit_lan_commands));
2105 seq_printf(m, "failed rexmit IPMB msgs: %u\n",
2106 ipmi_get_stat(intf, dropped_rexmit_ipmb_commands));
2107 return 0;
2108}
2109
2110static int smi_stats_proc_open(struct inode *inode, struct file *file)
2111{
Al Virod9dda782013-03-31 18:16:14 -04002112 return single_open(file, smi_stats_proc_show, PDE_DATA(inode));
Alexey Dobriyan07412732011-05-26 16:25:55 -07002113}
2114
2115static const struct file_operations smi_stats_proc_ops = {
2116 .open = smi_stats_proc_open,
2117 .read = seq_read,
2118 .llseek = seq_lseek,
2119 .release = single_release,
2120};
Randy Dunlap1aa16ee2006-12-06 20:41:20 -08002121#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
2123int ipmi_smi_add_proc_entry(ipmi_smi_t smi, char *name,
Alexey Dobriyan07412732011-05-26 16:25:55 -07002124 const struct file_operations *proc_ops,
Alexey Dobriyan99b76232009-03-25 22:48:06 +03002125 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 int rv = 0;
Corey Minyard3b625942005-06-23 22:01:42 -07002128#ifdef CONFIG_PROC_FS
2129 struct proc_dir_entry *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 struct ipmi_proc_entry *entry;
2131
2132 /* Create a list element. */
2133 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2134 if (!entry)
2135 return -ENOMEM;
Alexandru Gheorghiu1b6b6982013-05-16 14:04:24 -05002136 entry->name = kstrdup(name, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (!entry->name) {
2138 kfree(entry);
2139 return -ENOMEM;
2140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Alexey Dobriyan07412732011-05-26 16:25:55 -07002142 file = proc_create_data(name, 0, smi->proc_dir, proc_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (!file) {
2144 kfree(entry->name);
2145 kfree(entry);
2146 rv = -ENOMEM;
2147 } else {
Corey Minyardac019152007-10-18 03:07:11 -07002148 mutex_lock(&smi->proc_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 /* Stick it on the list. */
2150 entry->next = smi->proc_entries;
2151 smi->proc_entries = entry;
Corey Minyardac019152007-10-18 03:07:11 -07002152 mutex_unlock(&smi->proc_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 }
Corey Minyard3b625942005-06-23 22:01:42 -07002154#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 return rv;
2157}
Corey Minyardc70d7492008-04-29 01:01:09 -07002158EXPORT_SYMBOL(ipmi_smi_add_proc_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160static int add_proc_entries(ipmi_smi_t smi, int num)
2161{
2162 int rv = 0;
2163
Corey Minyard3b625942005-06-23 22:01:42 -07002164#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 sprintf(smi->proc_dir_name, "%d", num);
2166 smi->proc_dir = proc_mkdir(smi->proc_dir_name, proc_ipmi_root);
2167 if (!smi->proc_dir)
2168 rv = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 if (rv == 0)
2171 rv = ipmi_smi_add_proc_entry(smi, "stats",
Alexey Dobriyan07412732011-05-26 16:25:55 -07002172 &smi_stats_proc_ops,
Alexey Dobriyan99b76232009-03-25 22:48:06 +03002173 smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175 if (rv == 0)
2176 rv = ipmi_smi_add_proc_entry(smi, "ipmb",
Alexey Dobriyan07412732011-05-26 16:25:55 -07002177 &smi_ipmb_proc_ops,
Alexey Dobriyan99b76232009-03-25 22:48:06 +03002178 smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
2180 if (rv == 0)
2181 rv = ipmi_smi_add_proc_entry(smi, "version",
Alexey Dobriyan07412732011-05-26 16:25:55 -07002182 &smi_version_proc_ops,
Alexey Dobriyan99b76232009-03-25 22:48:06 +03002183 smi);
Corey Minyard3b625942005-06-23 22:01:42 -07002184#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
2186 return rv;
2187}
2188
2189static void remove_proc_entries(ipmi_smi_t smi)
2190{
Corey Minyard3b625942005-06-23 22:01:42 -07002191#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 struct ipmi_proc_entry *entry;
2193
Corey Minyardac019152007-10-18 03:07:11 -07002194 mutex_lock(&smi->proc_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 while (smi->proc_entries) {
2196 entry = smi->proc_entries;
2197 smi->proc_entries = entry->next;
2198
2199 remove_proc_entry(entry->name, smi->proc_dir);
2200 kfree(entry->name);
2201 kfree(entry);
2202 }
Corey Minyardac019152007-10-18 03:07:11 -07002203 mutex_unlock(&smi->proc_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 remove_proc_entry(smi->proc_dir_name, proc_ipmi_root);
Corey Minyard3b625942005-06-23 22:01:42 -07002205#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206}
2207
Corey Minyard50c812b2006-03-26 01:37:21 -08002208static int __find_bmc_guid(struct device *dev, void *data)
2209{
2210 unsigned char *id = data;
Corey Minyard16639eb2014-10-10 21:54:03 -05002211 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002212 return memcmp(bmc->guid, id, 16) == 0;
2213}
2214
2215static struct bmc_device *ipmi_find_bmc_guid(struct device_driver *drv,
2216 unsigned char *guid)
2217{
2218 struct device *dev;
2219
2220 dev = driver_find_device(drv, NULL, guid, __find_bmc_guid);
2221 if (dev)
Corey Minyard16639eb2014-10-10 21:54:03 -05002222 return to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002223 else
2224 return NULL;
2225}
2226
2227struct prod_dev_id {
2228 unsigned int product_id;
2229 unsigned char device_id;
2230};
2231
2232static int __find_bmc_prod_dev_id(struct device *dev, void *data)
2233{
2234 struct prod_dev_id *id = data;
Corey Minyard16639eb2014-10-10 21:54:03 -05002235 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002236
2237 return (bmc->id.product_id == id->product_id
Corey Minyard50c812b2006-03-26 01:37:21 -08002238 && bmc->id.device_id == id->device_id);
2239}
2240
2241static struct bmc_device *ipmi_find_bmc_prod_dev_id(
2242 struct device_driver *drv,
Corey Minyardf0b55da2006-12-06 20:40:54 -08002243 unsigned int product_id, unsigned char device_id)
Corey Minyard50c812b2006-03-26 01:37:21 -08002244{
2245 struct prod_dev_id id = {
2246 .product_id = product_id,
2247 .device_id = device_id,
2248 };
2249 struct device *dev;
2250
2251 dev = driver_find_device(drv, NULL, &id, __find_bmc_prod_dev_id);
2252 if (dev)
Corey Minyard16639eb2014-10-10 21:54:03 -05002253 return to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002254 else
2255 return NULL;
2256}
2257
2258static ssize_t device_id_show(struct device *dev,
2259 struct device_attribute *attr,
2260 char *buf)
2261{
Corey Minyard16639eb2014-10-10 21:54:03 -05002262 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002263
2264 return snprintf(buf, 10, "%u\n", bmc->id.device_id);
2265}
Corey Minyard9c633312014-12-12 19:06:07 -06002266static DEVICE_ATTR(device_id, S_IRUGO, device_id_show, NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002267
Corey Minyard16639eb2014-10-10 21:54:03 -05002268static ssize_t provides_device_sdrs_show(struct device *dev,
2269 struct device_attribute *attr,
2270 char *buf)
Corey Minyard50c812b2006-03-26 01:37:21 -08002271{
Corey Minyard16639eb2014-10-10 21:54:03 -05002272 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002273
2274 return snprintf(buf, 10, "%u\n",
Corey Minyard7947d2c2006-11-10 12:27:50 -08002275 (bmc->id.device_revision & 0x80) >> 7);
Corey Minyard50c812b2006-03-26 01:37:21 -08002276}
Corey Minyard9c633312014-12-12 19:06:07 -06002277static DEVICE_ATTR(provides_device_sdrs, S_IRUGO, provides_device_sdrs_show,
2278 NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002279
2280static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
2281 char *buf)
2282{
Corey Minyard16639eb2014-10-10 21:54:03 -05002283 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002284
2285 return snprintf(buf, 20, "%u\n",
Corey Minyard7947d2c2006-11-10 12:27:50 -08002286 bmc->id.device_revision & 0x0F);
Corey Minyard50c812b2006-03-26 01:37:21 -08002287}
Corey Minyard9c633312014-12-12 19:06:07 -06002288static DEVICE_ATTR(revision, S_IRUGO, revision_show, NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002289
Corey Minyard16639eb2014-10-10 21:54:03 -05002290static ssize_t firmware_revision_show(struct device *dev,
2291 struct device_attribute *attr,
2292 char *buf)
Corey Minyard50c812b2006-03-26 01:37:21 -08002293{
Corey Minyard16639eb2014-10-10 21:54:03 -05002294 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002295
2296 return snprintf(buf, 20, "%u.%x\n", bmc->id.firmware_revision_1,
2297 bmc->id.firmware_revision_2);
2298}
Corey Minyard9c633312014-12-12 19:06:07 -06002299static DEVICE_ATTR(firmware_revision, S_IRUGO, firmware_revision_show, NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002300
2301static ssize_t ipmi_version_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, 20, "%u.%u\n",
2308 ipmi_version_major(&bmc->id),
2309 ipmi_version_minor(&bmc->id));
2310}
Corey Minyard9c633312014-12-12 19:06:07 -06002311static DEVICE_ATTR(ipmi_version, S_IRUGO, ipmi_version_show, NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002312
2313static ssize_t add_dev_support_show(struct device *dev,
2314 struct device_attribute *attr,
2315 char *buf)
2316{
Corey Minyard16639eb2014-10-10 21:54:03 -05002317 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002318
2319 return snprintf(buf, 10, "0x%02x\n",
2320 bmc->id.additional_device_support);
2321}
Corey Minyard9c633312014-12-12 19:06:07 -06002322static DEVICE_ATTR(additional_device_support, S_IRUGO, add_dev_support_show,
2323 NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002324
2325static ssize_t manufacturer_id_show(struct device *dev,
2326 struct device_attribute *attr,
2327 char *buf)
2328{
Corey Minyard16639eb2014-10-10 21:54:03 -05002329 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002330
2331 return snprintf(buf, 20, "0x%6.6x\n", bmc->id.manufacturer_id);
2332}
Corey Minyard9c633312014-12-12 19:06:07 -06002333static DEVICE_ATTR(manufacturer_id, S_IRUGO, manufacturer_id_show, NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002334
2335static ssize_t product_id_show(struct device *dev,
2336 struct device_attribute *attr,
2337 char *buf)
2338{
Corey Minyard16639eb2014-10-10 21:54:03 -05002339 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002340
2341 return snprintf(buf, 10, "0x%4.4x\n", bmc->id.product_id);
2342}
Corey Minyard9c633312014-12-12 19:06:07 -06002343static DEVICE_ATTR(product_id, S_IRUGO, product_id_show, NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002344
2345static ssize_t aux_firmware_rev_show(struct device *dev,
2346 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, 21, "0x%02x 0x%02x 0x%02x 0x%02x\n",
2352 bmc->id.aux_firmware_revision[3],
2353 bmc->id.aux_firmware_revision[2],
2354 bmc->id.aux_firmware_revision[1],
2355 bmc->id.aux_firmware_revision[0]);
2356}
Corey Minyard9c633312014-12-12 19:06:07 -06002357static DEVICE_ATTR(aux_firmware_revision, S_IRUGO, aux_firmware_rev_show, NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002358
2359static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
2360 char *buf)
2361{
Corey Minyard16639eb2014-10-10 21:54:03 -05002362 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002363
2364 return snprintf(buf, 100, "%Lx%Lx\n",
2365 (long long) bmc->guid[0],
2366 (long long) bmc->guid[8]);
2367}
Corey Minyard9c633312014-12-12 19:06:07 -06002368static DEVICE_ATTR(guid, S_IRUGO, guid_show, NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002369
Corey Minyard16639eb2014-10-10 21:54:03 -05002370static struct attribute *bmc_dev_attrs[] = {
2371 &dev_attr_device_id.attr,
2372 &dev_attr_provides_device_sdrs.attr,
2373 &dev_attr_revision.attr,
2374 &dev_attr_firmware_revision.attr,
2375 &dev_attr_ipmi_version.attr,
2376 &dev_attr_additional_device_support.attr,
2377 &dev_attr_manufacturer_id.attr,
2378 &dev_attr_product_id.attr,
Takashi Iwai2d06a0c2015-02-04 15:36:14 +01002379 &dev_attr_aux_firmware_revision.attr,
2380 &dev_attr_guid.attr,
Corey Minyard16639eb2014-10-10 21:54:03 -05002381 NULL
2382};
2383
Takashi Iwai2d06a0c2015-02-04 15:36:14 +01002384static umode_t bmc_dev_attr_is_visible(struct kobject *kobj,
2385 struct attribute *attr, int idx)
2386{
2387 struct device *dev = kobj_to_dev(kobj);
2388 struct bmc_device *bmc = to_bmc_device(dev);
2389 umode_t mode = attr->mode;
2390
2391 if (attr == &dev_attr_aux_firmware_revision.attr)
2392 return bmc->id.aux_firmware_revision_set ? mode : 0;
2393 if (attr == &dev_attr_guid.attr)
2394 return bmc->guid_set ? mode : 0;
2395 return mode;
2396}
2397
Corey Minyard16639eb2014-10-10 21:54:03 -05002398static struct attribute_group bmc_dev_attr_group = {
2399 .attrs = bmc_dev_attrs,
Takashi Iwai2d06a0c2015-02-04 15:36:14 +01002400 .is_visible = bmc_dev_attr_is_visible,
Corey Minyard16639eb2014-10-10 21:54:03 -05002401};
2402
2403static const struct attribute_group *bmc_dev_attr_groups[] = {
2404 &bmc_dev_attr_group,
2405 NULL
2406};
2407
2408static struct device_type bmc_device_type = {
2409 .groups = bmc_dev_attr_groups,
2410};
2411
2412static void
2413release_bmc_device(struct device *dev)
Corey Minyard50c812b2006-03-26 01:37:21 -08002414{
Corey Minyard16639eb2014-10-10 21:54:03 -05002415 kfree(to_bmc_device(dev));
Jeff Garzik5e593932006-10-11 01:22:21 -07002416}
2417
2418static void
2419cleanup_bmc_device(struct kref *ref)
2420{
Corey Minyard16639eb2014-10-10 21:54:03 -05002421 struct bmc_device *bmc = container_of(ref, struct bmc_device, usecount);
Jeff Garzik5e593932006-10-11 01:22:21 -07002422
Corey Minyard16639eb2014-10-10 21:54:03 -05002423 platform_device_unregister(&bmc->pdev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002424}
2425
2426static void ipmi_bmc_unregister(ipmi_smi_t intf)
2427{
2428 struct bmc_device *bmc = intf->bmc;
2429
Corey Minyard5a0e10e2014-10-10 22:11:05 -05002430 sysfs_remove_link(&intf->si_dev->kobj, "bmc");
Corey Minyard50c812b2006-03-26 01:37:21 -08002431 if (intf->my_dev_name) {
Corey Minyard16639eb2014-10-10 21:54:03 -05002432 sysfs_remove_link(&bmc->pdev.dev.kobj, intf->my_dev_name);
Corey Minyard50c812b2006-03-26 01:37:21 -08002433 kfree(intf->my_dev_name);
2434 intf->my_dev_name = NULL;
2435 }
2436
2437 mutex_lock(&ipmidriver_mutex);
Corey Minyard16639eb2014-10-10 21:54:03 -05002438 kref_put(&bmc->usecount, cleanup_bmc_device);
Corey Minyardf0b55da2006-12-06 20:40:54 -08002439 intf->bmc = NULL;
Corey Minyard50c812b2006-03-26 01:37:21 -08002440 mutex_unlock(&ipmidriver_mutex);
2441}
2442
Corey Minyard5a0e10e2014-10-10 22:11:05 -05002443static int ipmi_bmc_register(ipmi_smi_t intf, int ifnum)
Corey Minyard50c812b2006-03-26 01:37:21 -08002444{
2445 int rv;
2446 struct bmc_device *bmc = intf->bmc;
2447 struct bmc_device *old_bmc;
Corey Minyard50c812b2006-03-26 01:37:21 -08002448
2449 mutex_lock(&ipmidriver_mutex);
2450
2451 /*
2452 * Try to find if there is an bmc_device struct
2453 * representing the interfaced BMC already
2454 */
2455 if (bmc->guid_set)
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08002456 old_bmc = ipmi_find_bmc_guid(&ipmidriver.driver, bmc->guid);
Corey Minyard50c812b2006-03-26 01:37:21 -08002457 else
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08002458 old_bmc = ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
Corey Minyard50c812b2006-03-26 01:37:21 -08002459 bmc->id.product_id,
2460 bmc->id.device_id);
2461
2462 /*
2463 * If there is already an bmc_device, free the new one,
2464 * otherwise register the new BMC device
2465 */
2466 if (old_bmc) {
2467 kfree(bmc);
2468 intf->bmc = old_bmc;
2469 bmc = old_bmc;
2470
Corey Minyard16639eb2014-10-10 21:54:03 -05002471 kref_get(&bmc->usecount);
Corey Minyard50c812b2006-03-26 01:37:21 -08002472 mutex_unlock(&ipmidriver_mutex);
2473
2474 printk(KERN_INFO
2475 "ipmi: interfacing existing BMC (man_id: 0x%6.6x,"
2476 " prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2477 bmc->id.manufacturer_id,
2478 bmc->id.product_id,
2479 bmc->id.device_id);
2480 } else {
Corey Minyardf0b55da2006-12-06 20:40:54 -08002481 unsigned char orig_dev_id = bmc->id.device_id;
2482 int warn_printed = 0;
2483
Corey Minyard16639eb2014-10-10 21:54:03 -05002484 snprintf(bmc->name, sizeof(bmc->name),
Corey Minyardf0b55da2006-12-06 20:40:54 -08002485 "ipmi_bmc.%4.4x", bmc->id.product_id);
Corey Minyard16639eb2014-10-10 21:54:03 -05002486 bmc->pdev.name = bmc->name;
Corey Minyardf0b55da2006-12-06 20:40:54 -08002487
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08002488 while (ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
Corey Minyardf0b55da2006-12-06 20:40:54 -08002489 bmc->id.product_id,
Corey Minyard1d5636c2006-12-10 02:19:08 -08002490 bmc->id.device_id)) {
Corey Minyardf0b55da2006-12-06 20:40:54 -08002491 if (!warn_printed) {
2492 printk(KERN_WARNING PFX
2493 "This machine has two different BMCs"
2494 " with the same product id and device"
2495 " id. This is an error in the"
2496 " firmware, but incrementing the"
2497 " device id to work around the problem."
2498 " Prod ID = 0x%x, Dev ID = 0x%x\n",
2499 bmc->id.product_id, bmc->id.device_id);
2500 warn_printed = 1;
2501 }
2502 bmc->id.device_id++; /* Wraps at 255 */
2503 if (bmc->id.device_id == orig_dev_id) {
2504 printk(KERN_ERR PFX
2505 "Out of device ids!\n");
2506 break;
2507 }
2508 }
2509
Corey Minyard16639eb2014-10-10 21:54:03 -05002510 bmc->pdev.dev.driver = &ipmidriver.driver;
2511 bmc->pdev.id = bmc->id.device_id;
2512 bmc->pdev.dev.release = release_bmc_device;
2513 bmc->pdev.dev.type = &bmc_device_type;
Corey Minyard5a0e10e2014-10-10 22:11:05 -05002514 kref_init(&bmc->usecount);
Corey Minyard50c812b2006-03-26 01:37:21 -08002515
Corey Minyard16639eb2014-10-10 21:54:03 -05002516 rv = platform_device_register(&bmc->pdev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002517 mutex_unlock(&ipmidriver_mutex);
2518 if (rv) {
Corey Minyard16639eb2014-10-10 21:54:03 -05002519 put_device(&bmc->pdev.dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002520 printk(KERN_ERR
2521 "ipmi_msghandler:"
2522 " Unable to register bmc device: %d\n",
2523 rv);
Corey Minyardc70d7492008-04-29 01:01:09 -07002524 /*
2525 * Don't go to out_err, you can only do that if
2526 * the device is registered already.
2527 */
Corey Minyard50c812b2006-03-26 01:37:21 -08002528 return rv;
2529 }
2530
Myron Stowe279fbd02010-05-26 14:43:52 -07002531 dev_info(intf->si_dev, "Found new BMC (man_id: 0x%6.6x, "
2532 "prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2533 bmc->id.manufacturer_id,
2534 bmc->id.product_id,
2535 bmc->id.device_id);
Corey Minyard50c812b2006-03-26 01:37:21 -08002536 }
2537
2538 /*
2539 * create symlink from system interface device to bmc device
2540 * and back.
2541 */
Corey Minyard5a0e10e2014-10-10 22:11:05 -05002542 rv = sysfs_create_link(&intf->si_dev->kobj, &bmc->pdev.dev.kobj, "bmc");
Corey Minyard50c812b2006-03-26 01:37:21 -08002543 if (rv) {
2544 printk(KERN_ERR
2545 "ipmi_msghandler: Unable to create bmc symlink: %d\n",
2546 rv);
2547 goto out_err;
2548 }
2549
Corey Minyard16639eb2014-10-10 21:54:03 -05002550 intf->my_dev_name = kasprintf(GFP_KERNEL, "ipmi%d", ifnum);
Corey Minyard50c812b2006-03-26 01:37:21 -08002551 if (!intf->my_dev_name) {
2552 rv = -ENOMEM;
2553 printk(KERN_ERR
2554 "ipmi_msghandler: allocate link from BMC: %d\n",
2555 rv);
2556 goto out_err;
2557 }
Corey Minyard50c812b2006-03-26 01:37:21 -08002558
Corey Minyard16639eb2014-10-10 21:54:03 -05002559 rv = sysfs_create_link(&bmc->pdev.dev.kobj, &intf->si_dev->kobj,
Corey Minyard50c812b2006-03-26 01:37:21 -08002560 intf->my_dev_name);
2561 if (rv) {
2562 kfree(intf->my_dev_name);
2563 intf->my_dev_name = NULL;
2564 printk(KERN_ERR
2565 "ipmi_msghandler:"
2566 " Unable to create symlink to bmc: %d\n",
2567 rv);
2568 goto out_err;
2569 }
2570
2571 return 0;
2572
2573out_err:
2574 ipmi_bmc_unregister(intf);
2575 return rv;
2576}
2577
2578static int
2579send_guid_cmd(ipmi_smi_t intf, int chan)
2580{
2581 struct kernel_ipmi_msg msg;
2582 struct ipmi_system_interface_addr si;
2583
2584 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2585 si.channel = IPMI_BMC_CHANNEL;
2586 si.lun = 0;
2587
2588 msg.netfn = IPMI_NETFN_APP_REQUEST;
2589 msg.cmd = IPMI_GET_DEVICE_GUID_CMD;
2590 msg.data = NULL;
2591 msg.data_len = 0;
2592 return i_ipmi_request(NULL,
2593 intf,
2594 (struct ipmi_addr *) &si,
2595 0,
2596 &msg,
2597 intf,
2598 NULL,
2599 NULL,
2600 0,
2601 intf->channels[0].address,
2602 intf->channels[0].lun,
2603 -1, 0);
2604}
2605
2606static void
2607guid_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
2608{
2609 if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2610 || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
2611 || (msg->msg.cmd != IPMI_GET_DEVICE_GUID_CMD))
2612 /* Not for me */
2613 return;
2614
2615 if (msg->msg.data[0] != 0) {
2616 /* Error from getting the GUID, the BMC doesn't have one. */
2617 intf->bmc->guid_set = 0;
2618 goto out;
2619 }
2620
2621 if (msg->msg.data_len < 17) {
2622 intf->bmc->guid_set = 0;
2623 printk(KERN_WARNING PFX
2624 "guid_handler: The GUID response from the BMC was too"
2625 " short, it was %d but should have been 17. Assuming"
2626 " GUID is not available.\n",
2627 msg->msg.data_len);
2628 goto out;
2629 }
2630
2631 memcpy(intf->bmc->guid, msg->msg.data, 16);
2632 intf->bmc->guid_set = 1;
2633 out:
2634 wake_up(&intf->waitq);
2635}
2636
2637static void
2638get_guid(ipmi_smi_t intf)
2639{
2640 int rv;
2641
2642 intf->bmc->guid_set = 0x2;
2643 intf->null_user_handler = guid_handler;
2644 rv = send_guid_cmd(intf, 0);
2645 if (rv)
2646 /* Send failed, no GUID available. */
2647 intf->bmc->guid_set = 0;
2648 wait_event(intf->waitq, intf->bmc->guid_set != 2);
2649 intf->null_user_handler = NULL;
2650}
2651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652static int
2653send_channel_info_cmd(ipmi_smi_t intf, int chan)
2654{
2655 struct kernel_ipmi_msg msg;
2656 unsigned char data[1];
2657 struct ipmi_system_interface_addr si;
2658
2659 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2660 si.channel = IPMI_BMC_CHANNEL;
2661 si.lun = 0;
2662
2663 msg.netfn = IPMI_NETFN_APP_REQUEST;
2664 msg.cmd = IPMI_GET_CHANNEL_INFO_CMD;
2665 msg.data = data;
2666 msg.data_len = 1;
2667 data[0] = chan;
2668 return i_ipmi_request(NULL,
2669 intf,
2670 (struct ipmi_addr *) &si,
2671 0,
2672 &msg,
Corey Minyard56a55ec2005-09-06 15:18:42 -07002673 intf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 NULL,
2675 NULL,
2676 0,
Corey Minyardc14979b2005-09-06 15:18:38 -07002677 intf->channels[0].address,
2678 intf->channels[0].lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 -1, 0);
2680}
2681
2682static void
Corey Minyard56a55ec2005-09-06 15:18:42 -07002683channel_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684{
2685 int rv = 0;
2686 int chan;
2687
Corey Minyard56a55ec2005-09-06 15:18:42 -07002688 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2689 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
Corey Minyardc70d7492008-04-29 01:01:09 -07002690 && (msg->msg.cmd == IPMI_GET_CHANNEL_INFO_CMD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 /* It's the one we want */
Corey Minyard56a55ec2005-09-06 15:18:42 -07002692 if (msg->msg.data[0] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 /* Got an error from the channel, just go on. */
2694
Corey Minyard56a55ec2005-09-06 15:18:42 -07002695 if (msg->msg.data[0] == IPMI_INVALID_COMMAND_ERR) {
Corey Minyardc70d7492008-04-29 01:01:09 -07002696 /*
2697 * If the MC does not support this
2698 * command, that is legal. We just
2699 * assume it has one IPMB at channel
2700 * zero.
2701 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 intf->channels[0].medium
2703 = IPMI_CHANNEL_MEDIUM_IPMB;
2704 intf->channels[0].protocol
2705 = IPMI_CHANNEL_PROTOCOL_IPMB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
2707 intf->curr_channel = IPMI_MAX_CHANNELS;
2708 wake_up(&intf->waitq);
2709 goto out;
2710 }
2711 goto next_channel;
2712 }
Corey Minyard56a55ec2005-09-06 15:18:42 -07002713 if (msg->msg.data_len < 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 /* Message not big enough, just go on. */
2715 goto next_channel;
2716 }
2717 chan = intf->curr_channel;
Corey Minyard56a55ec2005-09-06 15:18:42 -07002718 intf->channels[chan].medium = msg->msg.data[2] & 0x7f;
2719 intf->channels[chan].protocol = msg->msg.data[3] & 0x1f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
Corey Minyardc70d7492008-04-29 01:01:09 -07002721 next_channel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 intf->curr_channel++;
2723 if (intf->curr_channel >= IPMI_MAX_CHANNELS)
2724 wake_up(&intf->waitq);
2725 else
2726 rv = send_channel_info_cmd(intf, intf->curr_channel);
2727
2728 if (rv) {
2729 /* Got an error somehow, just give up. */
Corey Minyard1f668422014-10-06 14:17:50 -05002730 printk(KERN_WARNING PFX
2731 "Error sending channel information for channel"
2732 " %d: %d\n", intf->curr_channel, rv);
2733
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 intf->curr_channel = IPMI_MAX_CHANNELS;
2735 wake_up(&intf->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 }
2737 }
2738 out:
2739 return;
2740}
2741
Corey Minyard895dcfd2012-03-28 14:42:49 -07002742static void ipmi_poll(ipmi_smi_t intf)
Corey Minyardfcfa4722007-10-18 03:07:09 -07002743{
Corey Minyardfcfa4722007-10-18 03:07:09 -07002744 if (intf->handlers->poll)
2745 intf->handlers->poll(intf->send_info);
Corey Minyard7adf5792012-03-28 14:42:49 -07002746 /* In case something came in */
2747 handle_new_recv_msgs(intf);
Corey Minyardfcfa4722007-10-18 03:07:09 -07002748}
Corey Minyard895dcfd2012-03-28 14:42:49 -07002749
2750void ipmi_poll_interface(ipmi_user_t user)
2751{
2752 ipmi_poll(user->intf);
Corey Minyardfcfa4722007-10-18 03:07:09 -07002753}
Corey Minyardc70d7492008-04-29 01:01:09 -07002754EXPORT_SYMBOL(ipmi_poll_interface);
Corey Minyardfcfa4722007-10-18 03:07:09 -07002755
Corey Minyard81d02b72015-06-13 10:34:25 -05002756int ipmi_register_smi(const struct ipmi_smi_handlers *handlers,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 void *send_info,
Corey Minyard50c812b2006-03-26 01:37:21 -08002758 struct ipmi_device_id *device_id,
2759 struct device *si_dev,
Corey Minyard453823b2006-03-31 02:30:39 -08002760 unsigned char slave_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761{
2762 int i, j;
2763 int rv;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002764 ipmi_smi_t intf;
Corey Minyardbca03242006-12-06 20:40:57 -08002765 ipmi_smi_t tintf;
Corey Minyardbca03242006-12-06 20:40:57 -08002766 struct list_head *link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
Corey Minyardc70d7492008-04-29 01:01:09 -07002768 /*
2769 * Make sure the driver is actually initialized, this handles
2770 * problems with initialization order.
2771 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 if (!initialized) {
2773 rv = ipmi_init_msghandler();
2774 if (rv)
2775 return rv;
Corey Minyardc70d7492008-04-29 01:01:09 -07002776 /*
2777 * The init code doesn't return an error if it was turned
2778 * off, but it won't initialize. Check that.
2779 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 if (!initialized)
2781 return -ENODEV;
2782 }
2783
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002784 intf = kzalloc(sizeof(*intf), GFP_KERNEL);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002785 if (!intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 return -ENOMEM;
Corey Minyardb2c03942006-12-06 20:41:00 -08002787
2788 intf->ipmi_version_major = ipmi_version_major(device_id);
2789 intf->ipmi_version_minor = ipmi_version_minor(device_id);
2790
Corey Minyard50c812b2006-03-26 01:37:21 -08002791 intf->bmc = kzalloc(sizeof(*intf->bmc), GFP_KERNEL);
2792 if (!intf->bmc) {
2793 kfree(intf);
2794 return -ENOMEM;
2795 }
Corey Minyardbca03242006-12-06 20:40:57 -08002796 intf->intf_num = -1; /* Mark it invalid for now. */
Corey Minyard393d2cc2005-11-07 00:59:54 -08002797 kref_init(&intf->refcount);
Corey Minyard50c812b2006-03-26 01:37:21 -08002798 intf->bmc->id = *device_id;
2799 intf->si_dev = si_dev;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002800 for (j = 0; j < IPMI_MAX_CHANNELS; j++) {
2801 intf->channels[j].address = IPMI_BMC_SLAVE_ADDR;
2802 intf->channels[j].lun = 2;
2803 }
2804 if (slave_addr != 0)
2805 intf->channels[0].address = slave_addr;
2806 INIT_LIST_HEAD(&intf->users);
2807 intf->handlers = handlers;
2808 intf->send_info = send_info;
2809 spin_lock_init(&intf->seq_lock);
2810 for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++) {
2811 intf->seq_table[j].inuse = 0;
2812 intf->seq_table[j].seqid = 0;
2813 }
2814 intf->curr_seq = 0;
2815#ifdef CONFIG_PROC_FS
Corey Minyardac019152007-10-18 03:07:11 -07002816 mutex_init(&intf->proc_entry_lock);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002817#endif
Corey Minyard65be7542014-11-06 20:48:02 -06002818 spin_lock_init(&intf->waiting_rcv_msgs_lock);
2819 INIT_LIST_HEAD(&intf->waiting_rcv_msgs);
Corey Minyard7adf5792012-03-28 14:42:49 -07002820 tasklet_init(&intf->recv_tasklet,
2821 smi_recv_tasklet,
2822 (unsigned long) intf);
2823 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 0);
Corey Minyard7ea0ed22014-11-06 16:58:48 -06002824 spin_lock_init(&intf->xmit_msgs_lock);
2825 INIT_LIST_HEAD(&intf->xmit_msgs);
2826 INIT_LIST_HEAD(&intf->hp_xmit_msgs);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002827 spin_lock_init(&intf->events_lock);
Corey Minyard89986492014-04-14 09:46:54 -05002828 atomic_set(&intf->event_waiters, 0);
2829 intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002830 INIT_LIST_HEAD(&intf->waiting_events);
2831 intf->waiting_events_count = 0;
Corey Minyardd6dfd132006-03-31 02:30:41 -08002832 mutex_init(&intf->cmd_rcvrs_mutex);
Corey Minyardb9675132006-12-06 20:41:02 -08002833 spin_lock_init(&intf->maintenance_mode_lock);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002834 INIT_LIST_HEAD(&intf->cmd_rcvrs);
2835 init_waitqueue_head(&intf->waitq);
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07002836 for (i = 0; i < IPMI_NUM_STATS; i++)
2837 atomic_set(&intf->stats[i], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Corey Minyard393d2cc2005-11-07 00:59:54 -08002839 intf->proc_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Corey Minyardb2c03942006-12-06 20:41:00 -08002841 mutex_lock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -08002842 mutex_lock(&ipmi_interfaces_mutex);
2843 /* Look for a hole in the numbers. */
2844 i = 0;
2845 link = &ipmi_interfaces;
2846 list_for_each_entry_rcu(tintf, &ipmi_interfaces, link) {
2847 if (tintf->intf_num != i) {
2848 link = &tintf->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 break;
2850 }
Corey Minyardbca03242006-12-06 20:40:57 -08002851 i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 }
Corey Minyardbca03242006-12-06 20:40:57 -08002853 /* Add the new interface in numeric order. */
2854 if (i == 0)
2855 list_add_rcu(&intf->link, &ipmi_interfaces);
2856 else
2857 list_add_tail_rcu(&intf->link, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
Corey Minyard453823b2006-03-31 02:30:39 -08002859 rv = handlers->start_processing(send_info, intf);
2860 if (rv)
2861 goto out;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002862
Corey Minyard50c812b2006-03-26 01:37:21 -08002863 get_guid(intf);
2864
Corey Minyardb2c03942006-12-06 20:41:00 -08002865 if ((intf->ipmi_version_major > 1)
Corey Minyardc70d7492008-04-29 01:01:09 -07002866 || ((intf->ipmi_version_major == 1)
2867 && (intf->ipmi_version_minor >= 5))) {
2868 /*
2869 * Start scanning the channels to see what is
2870 * available.
2871 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08002872 intf->null_user_handler = channel_handler;
2873 intf->curr_channel = 0;
2874 rv = send_channel_info_cmd(intf, 0);
Corey Minyard1f668422014-10-06 14:17:50 -05002875 if (rv) {
2876 printk(KERN_WARNING PFX
2877 "Error sending channel information for channel"
2878 " 0, %d\n", rv);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002879 goto out;
Corey Minyard1f668422014-10-06 14:17:50 -05002880 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08002881
2882 /* Wait for the channel info to be read. */
2883 wait_event(intf->waitq,
2884 intf->curr_channel >= IPMI_MAX_CHANNELS);
Corey Minyard50c812b2006-03-26 01:37:21 -08002885 intf->null_user_handler = NULL;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002886 } else {
2887 /* Assume a single IPMB channel at zero. */
2888 intf->channels[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
2889 intf->channels[0].protocol = IPMI_CHANNEL_PROTOCOL_IPMB;
Corey Minyard9a2845c2009-05-20 13:36:17 -05002890 intf->curr_channel = IPMI_MAX_CHANNELS;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
2893 if (rv == 0)
Corey Minyard393d2cc2005-11-07 00:59:54 -08002894 rv = add_proc_entries(intf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
Corey Minyard5a0e10e2014-10-10 22:11:05 -05002896 rv = ipmi_bmc_register(intf, i);
Corey Minyard50c812b2006-03-26 01:37:21 -08002897
Corey Minyard393d2cc2005-11-07 00:59:54 -08002898 out:
2899 if (rv) {
2900 if (intf->proc_dir)
2901 remove_proc_entries(intf);
Corey Minyardb2c03942006-12-06 20:41:00 -08002902 intf->handlers = NULL;
Corey Minyardbca03242006-12-06 20:40:57 -08002903 list_del_rcu(&intf->link);
2904 mutex_unlock(&ipmi_interfaces_mutex);
Corey Minyardb2c03942006-12-06 20:41:00 -08002905 mutex_unlock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -08002906 synchronize_rcu();
Corey Minyard393d2cc2005-11-07 00:59:54 -08002907 kref_put(&intf->refcount, intf_free);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002908 } else {
Corey Minyard78ba2fa2007-02-10 01:45:45 -08002909 /*
2910 * Keep memory order straight for RCU readers. Make
2911 * sure everything else is committed to memory before
2912 * setting intf_num to mark the interface valid.
2913 */
2914 smp_wmb();
Corey Minyardbca03242006-12-06 20:40:57 -08002915 intf->intf_num = i;
2916 mutex_unlock(&ipmi_interfaces_mutex);
Corey Minyard78ba2fa2007-02-10 01:45:45 -08002917 /* After this point the interface is legal to use. */
Corey Minyard50c812b2006-03-26 01:37:21 -08002918 call_smi_watchers(i, intf->si_dev);
Corey Minyardb2c03942006-12-06 20:41:00 -08002919 mutex_unlock(&smi_watchers_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 }
2921
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 return rv;
2923}
Corey Minyardc70d7492008-04-29 01:01:09 -07002924EXPORT_SYMBOL(ipmi_register_smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
Corey Minyard7ea0ed22014-11-06 16:58:48 -06002926static void deliver_smi_err_response(ipmi_smi_t intf,
2927 struct ipmi_smi_msg *msg,
2928 unsigned char err)
2929{
2930 msg->rsp[0] = msg->data[0] | 4;
2931 msg->rsp[1] = msg->data[1];
2932 msg->rsp[2] = err;
2933 msg->rsp_size = 3;
2934 /* It's an error, so it will never requeue, no need to check return. */
2935 handle_one_recv_msg(intf, msg);
2936}
2937
Corey Minyardb2c03942006-12-06 20:41:00 -08002938static void cleanup_smi_msgs(ipmi_smi_t intf)
2939{
2940 int i;
2941 struct seq_table *ent;
Corey Minyard7ea0ed22014-11-06 16:58:48 -06002942 struct ipmi_smi_msg *msg;
2943 struct list_head *entry;
2944 struct list_head tmplist;
2945
2946 /* Clear out our transmit queues and hold the messages. */
2947 INIT_LIST_HEAD(&tmplist);
2948 list_splice_tail(&intf->hp_xmit_msgs, &tmplist);
2949 list_splice_tail(&intf->xmit_msgs, &tmplist);
2950
2951 /* Current message first, to preserve order */
2952 while (intf->curr_msg && !list_empty(&intf->waiting_rcv_msgs)) {
2953 /* Wait for the message to clear out. */
2954 schedule_timeout(1);
2955 }
Corey Minyardb2c03942006-12-06 20:41:00 -08002956
2957 /* No need for locks, the interface is down. */
Corey Minyard7ea0ed22014-11-06 16:58:48 -06002958
2959 /*
2960 * Return errors for all pending messages in queue and in the
2961 * tables waiting for remote responses.
2962 */
2963 while (!list_empty(&tmplist)) {
2964 entry = tmplist.next;
2965 list_del(entry);
2966 msg = list_entry(entry, struct ipmi_smi_msg, link);
2967 deliver_smi_err_response(intf, msg, IPMI_ERR_UNSPECIFIED);
2968 }
2969
Corey Minyardb2c03942006-12-06 20:41:00 -08002970 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
2971 ent = &(intf->seq_table[i]);
2972 if (!ent->inuse)
2973 continue;
2974 deliver_err_response(ent->recv_msg, IPMI_ERR_UNSPECIFIED);
2975 }
2976}
2977
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978int ipmi_unregister_smi(ipmi_smi_t intf)
2979{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 struct ipmi_smi_watcher *w;
Corey Minyard7ea0ed22014-11-06 16:58:48 -06002981 int intf_num = intf->intf_num;
2982 ipmi_user_t user;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
Corey Minyard50c812b2006-03-26 01:37:21 -08002984 ipmi_bmc_unregister(intf);
2985
Corey Minyardb2c03942006-12-06 20:41:00 -08002986 mutex_lock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -08002987 mutex_lock(&ipmi_interfaces_mutex);
Corey Minyardb2c03942006-12-06 20:41:00 -08002988 intf->intf_num = -1;
Corey Minyard7ea0ed22014-11-06 16:58:48 -06002989 intf->in_shutdown = true;
Corey Minyardbca03242006-12-06 20:40:57 -08002990 list_del_rcu(&intf->link);
2991 mutex_unlock(&ipmi_interfaces_mutex);
2992 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
Corey Minyardb2c03942006-12-06 20:41:00 -08002994 cleanup_smi_msgs(intf);
2995
Corey Minyard7ea0ed22014-11-06 16:58:48 -06002996 /* Clean up the effects of users on the lower-level software. */
2997 mutex_lock(&ipmi_interfaces_mutex);
2998 rcu_read_lock();
2999 list_for_each_entry_rcu(user, &intf->users, link) {
3000 module_put(intf->handlers->owner);
3001 if (intf->handlers->dec_usecount)
3002 intf->handlers->dec_usecount(intf->send_info);
3003 }
3004 rcu_read_unlock();
3005 intf->handlers = NULL;
3006 mutex_unlock(&ipmi_interfaces_mutex);
3007
Corey Minyard393d2cc2005-11-07 00:59:54 -08003008 remove_proc_entries(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Corey Minyardc70d7492008-04-29 01:01:09 -07003010 /*
3011 * Call all the watcher interfaces to tell them that
3012 * an interface is gone.
3013 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08003014 list_for_each_entry(w, &smi_watchers, link)
Corey Minyardb2c03942006-12-06 20:41:00 -08003015 w->smi_gone(intf_num);
3016 mutex_unlock(&smi_watchers_mutex);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003017
Corey Minyard393d2cc2005-11-07 00:59:54 -08003018 kref_put(&intf->refcount, intf_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 return 0;
3020}
Corey Minyardc70d7492008-04-29 01:01:09 -07003021EXPORT_SYMBOL(ipmi_unregister_smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
3023static int handle_ipmb_get_msg_rsp(ipmi_smi_t intf,
3024 struct ipmi_smi_msg *msg)
3025{
3026 struct ipmi_ipmb_addr ipmb_addr;
3027 struct ipmi_recv_msg *recv_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
Corey Minyardc70d7492008-04-29 01:01:09 -07003029 /*
3030 * This is 11, not 10, because the response must contain a
3031 * completion code.
3032 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 if (msg->rsp_size < 11) {
3034 /* Message not big enough, just ignore it. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003035 ipmi_inc_stat(intf, invalid_ipmb_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 return 0;
3037 }
3038
3039 if (msg->rsp[2] != 0) {
3040 /* An error getting the response, just ignore it. */
3041 return 0;
3042 }
3043
3044 ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
3045 ipmb_addr.slave_addr = msg->rsp[6];
3046 ipmb_addr.channel = msg->rsp[3] & 0x0f;
3047 ipmb_addr.lun = msg->rsp[7] & 3;
3048
Corey Minyardc70d7492008-04-29 01:01:09 -07003049 /*
3050 * It's a response from a remote entity. Look up the sequence
3051 * number and handle the response.
3052 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 if (intf_find_seq(intf,
3054 msg->rsp[7] >> 2,
3055 msg->rsp[3] & 0x0f,
3056 msg->rsp[8],
3057 (msg->rsp[4] >> 2) & (~1),
3058 (struct ipmi_addr *) &(ipmb_addr),
Corey Minyardc70d7492008-04-29 01:01:09 -07003059 &recv_msg)) {
3060 /*
3061 * We were unable to find the sequence number,
3062 * so just nuke the message.
3063 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003064 ipmi_inc_stat(intf, unhandled_ipmb_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 return 0;
3066 }
3067
3068 memcpy(recv_msg->msg_data,
3069 &(msg->rsp[9]),
3070 msg->rsp_size - 9);
Corey Minyardc70d7492008-04-29 01:01:09 -07003071 /*
3072 * The other fields matched, so no need to set them, except
3073 * for netfn, which needs to be the response that was
3074 * returned, not the request value.
3075 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3077 recv_msg->msg.data = recv_msg->msg_data;
3078 recv_msg->msg.data_len = msg->rsp_size - 10;
3079 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003080 ipmi_inc_stat(intf, handled_ipmb_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 deliver_response(recv_msg);
3082
3083 return 0;
3084}
3085
3086static int handle_ipmb_get_msg_cmd(ipmi_smi_t intf,
3087 struct ipmi_smi_msg *msg)
3088{
Corey Minyard393d2cc2005-11-07 00:59:54 -08003089 struct cmd_rcvr *rcvr;
3090 int rv = 0;
3091 unsigned char netfn;
3092 unsigned char cmd;
Corey Minyardc69c3122006-09-30 23:27:56 -07003093 unsigned char chan;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003094 ipmi_user_t user = NULL;
3095 struct ipmi_ipmb_addr *ipmb_addr;
3096 struct ipmi_recv_msg *recv_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
3098 if (msg->rsp_size < 10) {
3099 /* Message not big enough, just ignore it. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003100 ipmi_inc_stat(intf, invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 return 0;
3102 }
3103
3104 if (msg->rsp[2] != 0) {
3105 /* An error getting the response, just ignore it. */
3106 return 0;
3107 }
3108
3109 netfn = msg->rsp[4] >> 2;
3110 cmd = msg->rsp[8];
Corey Minyardc69c3122006-09-30 23:27:56 -07003111 chan = msg->rsp[3] & 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
Corey Minyarde61fb5b2005-11-07 01:00:05 -08003113 rcu_read_lock();
Corey Minyardc69c3122006-09-30 23:27:56 -07003114 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003115 if (rcvr) {
3116 user = rcvr->user;
3117 kref_get(&user->refcount);
3118 } else
3119 user = NULL;
Corey Minyarde61fb5b2005-11-07 01:00:05 -08003120 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
3122 if (user == NULL) {
3123 /* We didn't find a user, deliver an error response. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003124 ipmi_inc_stat(intf, unhandled_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
3126 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
3127 msg->data[1] = IPMI_SEND_MSG_CMD;
3128 msg->data[2] = msg->rsp[3];
3129 msg->data[3] = msg->rsp[6];
Corey Minyardc70d7492008-04-29 01:01:09 -07003130 msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 msg->data[5] = ipmb_checksum(&(msg->data[3]), 2);
Corey Minyardc14979b2005-09-06 15:18:38 -07003132 msg->data[6] = intf->channels[msg->rsp[3] & 0xf].address;
Corey Minyardc70d7492008-04-29 01:01:09 -07003133 /* rqseq/lun */
3134 msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 msg->data[8] = msg->rsp[8]; /* cmd */
3136 msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
3137 msg->data[10] = ipmb_checksum(&(msg->data[6]), 4);
3138 msg->data_size = 11;
3139
3140#ifdef DEBUG_MSGING
3141 {
3142 int m;
3143 printk("Invalid command:");
Corey Minyarde8b33612005-09-06 15:18:45 -07003144 for (m = 0; m < msg->data_size; m++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 printk(" %2.2x", msg->data[m]);
3146 printk("\n");
3147 }
3148#endif
Corey Minyardb2c03942006-12-06 20:41:00 -08003149 rcu_read_lock();
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003150 if (!intf->in_shutdown) {
3151 smi_send(intf, intf->handlers, msg, 0);
Corey Minyardc70d7492008-04-29 01:01:09 -07003152 /*
3153 * We used the message, so return the value
3154 * that causes it to not be freed or
3155 * queued.
3156 */
Corey Minyardb2c03942006-12-06 20:41:00 -08003157 rv = -1;
3158 }
3159 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 } else {
3161 /* Deliver the message to the user. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003162 ipmi_inc_stat(intf, handled_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
3164 recv_msg = ipmi_alloc_recv_msg();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003165 if (!recv_msg) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003166 /*
3167 * We couldn't allocate memory for the
3168 * message, so requeue it for handling
3169 * later.
3170 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 rv = 1;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003172 kref_put(&user->refcount, free_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 } else {
3174 /* Extract the source address from the data. */
3175 ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
3176 ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
3177 ipmb_addr->slave_addr = msg->rsp[6];
3178 ipmb_addr->lun = msg->rsp[7] & 3;
3179 ipmb_addr->channel = msg->rsp[3] & 0xf;
3180
Corey Minyardc70d7492008-04-29 01:01:09 -07003181 /*
3182 * Extract the rest of the message information
3183 * from the IPMB header.
3184 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 recv_msg->user = user;
3186 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3187 recv_msg->msgid = msg->rsp[7] >> 2;
3188 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3189 recv_msg->msg.cmd = msg->rsp[8];
3190 recv_msg->msg.data = recv_msg->msg_data;
3191
Corey Minyardc70d7492008-04-29 01:01:09 -07003192 /*
3193 * We chop off 10, not 9 bytes because the checksum
3194 * at the end also needs to be removed.
3195 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 recv_msg->msg.data_len = msg->rsp_size - 10;
3197 memcpy(recv_msg->msg_data,
3198 &(msg->rsp[9]),
3199 msg->rsp_size - 10);
3200 deliver_response(recv_msg);
3201 }
3202 }
3203
3204 return rv;
3205}
3206
3207static int handle_lan_get_msg_rsp(ipmi_smi_t intf,
3208 struct ipmi_smi_msg *msg)
3209{
3210 struct ipmi_lan_addr lan_addr;
3211 struct ipmi_recv_msg *recv_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
3213
Corey Minyardc70d7492008-04-29 01:01:09 -07003214 /*
3215 * This is 13, not 12, because the response must contain a
3216 * completion code.
3217 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 if (msg->rsp_size < 13) {
3219 /* Message not big enough, just ignore it. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003220 ipmi_inc_stat(intf, invalid_lan_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 return 0;
3222 }
3223
3224 if (msg->rsp[2] != 0) {
3225 /* An error getting the response, just ignore it. */
3226 return 0;
3227 }
3228
3229 lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
3230 lan_addr.session_handle = msg->rsp[4];
3231 lan_addr.remote_SWID = msg->rsp[8];
3232 lan_addr.local_SWID = msg->rsp[5];
3233 lan_addr.channel = msg->rsp[3] & 0x0f;
3234 lan_addr.privilege = msg->rsp[3] >> 4;
3235 lan_addr.lun = msg->rsp[9] & 3;
3236
Corey Minyardc70d7492008-04-29 01:01:09 -07003237 /*
3238 * It's a response from a remote entity. Look up the sequence
3239 * number and handle the response.
3240 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 if (intf_find_seq(intf,
3242 msg->rsp[9] >> 2,
3243 msg->rsp[3] & 0x0f,
3244 msg->rsp[10],
3245 (msg->rsp[6] >> 2) & (~1),
3246 (struct ipmi_addr *) &(lan_addr),
Corey Minyardc70d7492008-04-29 01:01:09 -07003247 &recv_msg)) {
3248 /*
3249 * We were unable to find the sequence number,
3250 * so just nuke the message.
3251 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003252 ipmi_inc_stat(intf, unhandled_lan_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 return 0;
3254 }
3255
3256 memcpy(recv_msg->msg_data,
3257 &(msg->rsp[11]),
3258 msg->rsp_size - 11);
Corey Minyardc70d7492008-04-29 01:01:09 -07003259 /*
3260 * The other fields matched, so no need to set them, except
3261 * for netfn, which needs to be the response that was
3262 * returned, not the request value.
3263 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3265 recv_msg->msg.data = recv_msg->msg_data;
3266 recv_msg->msg.data_len = msg->rsp_size - 12;
3267 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003268 ipmi_inc_stat(intf, handled_lan_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 deliver_response(recv_msg);
3270
3271 return 0;
3272}
3273
3274static int handle_lan_get_msg_cmd(ipmi_smi_t intf,
3275 struct ipmi_smi_msg *msg)
3276{
Corey Minyard393d2cc2005-11-07 00:59:54 -08003277 struct cmd_rcvr *rcvr;
3278 int rv = 0;
3279 unsigned char netfn;
3280 unsigned char cmd;
Corey Minyardc69c3122006-09-30 23:27:56 -07003281 unsigned char chan;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003282 ipmi_user_t user = NULL;
3283 struct ipmi_lan_addr *lan_addr;
3284 struct ipmi_recv_msg *recv_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
3286 if (msg->rsp_size < 12) {
3287 /* Message not big enough, just ignore it. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003288 ipmi_inc_stat(intf, invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 return 0;
3290 }
3291
3292 if (msg->rsp[2] != 0) {
3293 /* An error getting the response, just ignore it. */
3294 return 0;
3295 }
3296
3297 netfn = msg->rsp[6] >> 2;
3298 cmd = msg->rsp[10];
Corey Minyardc69c3122006-09-30 23:27:56 -07003299 chan = msg->rsp[3] & 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300
Corey Minyarde61fb5b2005-11-07 01:00:05 -08003301 rcu_read_lock();
Corey Minyardc69c3122006-09-30 23:27:56 -07003302 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003303 if (rcvr) {
3304 user = rcvr->user;
3305 kref_get(&user->refcount);
3306 } else
3307 user = NULL;
Corey Minyarde61fb5b2005-11-07 01:00:05 -08003308 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
3310 if (user == NULL) {
Corey Minyard393d2cc2005-11-07 00:59:54 -08003311 /* We didn't find a user, just give up. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003312 ipmi_inc_stat(intf, unhandled_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313
Corey Minyardc70d7492008-04-29 01:01:09 -07003314 /*
3315 * Don't do anything with these messages, just allow
3316 * them to be freed.
3317 */
3318 rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 } else {
3320 /* Deliver the message to the user. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003321 ipmi_inc_stat(intf, handled_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
3323 recv_msg = ipmi_alloc_recv_msg();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003324 if (!recv_msg) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003325 /*
3326 * We couldn't allocate memory for the
3327 * message, so requeue it for handling later.
3328 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 rv = 1;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003330 kref_put(&user->refcount, free_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 } else {
3332 /* Extract the source address from the data. */
3333 lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
3334 lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
3335 lan_addr->session_handle = msg->rsp[4];
3336 lan_addr->remote_SWID = msg->rsp[8];
3337 lan_addr->local_SWID = msg->rsp[5];
3338 lan_addr->lun = msg->rsp[9] & 3;
3339 lan_addr->channel = msg->rsp[3] & 0xf;
3340 lan_addr->privilege = msg->rsp[3] >> 4;
3341
Corey Minyardc70d7492008-04-29 01:01:09 -07003342 /*
3343 * Extract the rest of the message information
3344 * from the IPMB header.
3345 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 recv_msg->user = user;
3347 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3348 recv_msg->msgid = msg->rsp[9] >> 2;
3349 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3350 recv_msg->msg.cmd = msg->rsp[10];
3351 recv_msg->msg.data = recv_msg->msg_data;
3352
Corey Minyardc70d7492008-04-29 01:01:09 -07003353 /*
3354 * We chop off 12, not 11 bytes because the checksum
3355 * at the end also needs to be removed.
3356 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 recv_msg->msg.data_len = msg->rsp_size - 12;
3358 memcpy(recv_msg->msg_data,
3359 &(msg->rsp[11]),
3360 msg->rsp_size - 12);
3361 deliver_response(recv_msg);
3362 }
3363 }
3364
3365 return rv;
3366}
3367
dann frazier4dec3022009-04-21 12:24:05 -07003368/*
3369 * This routine will handle "Get Message" command responses with
3370 * channels that use an OEM Medium. The message format belongs to
3371 * the OEM. See IPMI 2.0 specification, Chapter 6 and
3372 * Chapter 22, sections 22.6 and 22.24 for more details.
3373 */
3374static int handle_oem_get_msg_cmd(ipmi_smi_t intf,
3375 struct ipmi_smi_msg *msg)
3376{
3377 struct cmd_rcvr *rcvr;
3378 int rv = 0;
3379 unsigned char netfn;
3380 unsigned char cmd;
3381 unsigned char chan;
3382 ipmi_user_t user = NULL;
3383 struct ipmi_system_interface_addr *smi_addr;
3384 struct ipmi_recv_msg *recv_msg;
3385
3386 /*
3387 * We expect the OEM SW to perform error checking
3388 * so we just do some basic sanity checks
3389 */
3390 if (msg->rsp_size < 4) {
3391 /* Message not big enough, just ignore it. */
3392 ipmi_inc_stat(intf, invalid_commands);
3393 return 0;
3394 }
3395
3396 if (msg->rsp[2] != 0) {
3397 /* An error getting the response, just ignore it. */
3398 return 0;
3399 }
3400
3401 /*
3402 * This is an OEM Message so the OEM needs to know how
3403 * handle the message. We do no interpretation.
3404 */
3405 netfn = msg->rsp[0] >> 2;
3406 cmd = msg->rsp[1];
3407 chan = msg->rsp[3] & 0xf;
3408
3409 rcu_read_lock();
3410 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3411 if (rcvr) {
3412 user = rcvr->user;
3413 kref_get(&user->refcount);
3414 } else
3415 user = NULL;
3416 rcu_read_unlock();
3417
3418 if (user == NULL) {
3419 /* We didn't find a user, just give up. */
3420 ipmi_inc_stat(intf, unhandled_commands);
3421
3422 /*
3423 * Don't do anything with these messages, just allow
3424 * them to be freed.
3425 */
3426
3427 rv = 0;
3428 } else {
3429 /* Deliver the message to the user. */
3430 ipmi_inc_stat(intf, handled_commands);
3431
3432 recv_msg = ipmi_alloc_recv_msg();
3433 if (!recv_msg) {
3434 /*
3435 * We couldn't allocate memory for the
3436 * message, so requeue it for handling
3437 * later.
3438 */
3439 rv = 1;
3440 kref_put(&user->refcount, free_user);
3441 } else {
3442 /*
3443 * OEM Messages are expected to be delivered via
3444 * the system interface to SMS software. We might
3445 * need to visit this again depending on OEM
3446 * requirements
3447 */
3448 smi_addr = ((struct ipmi_system_interface_addr *)
3449 &(recv_msg->addr));
3450 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3451 smi_addr->channel = IPMI_BMC_CHANNEL;
3452 smi_addr->lun = msg->rsp[0] & 3;
3453
3454 recv_msg->user = user;
3455 recv_msg->user_msg_data = NULL;
3456 recv_msg->recv_type = IPMI_OEM_RECV_TYPE;
3457 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3458 recv_msg->msg.cmd = msg->rsp[1];
3459 recv_msg->msg.data = recv_msg->msg_data;
3460
3461 /*
3462 * The message starts at byte 4 which follows the
3463 * the Channel Byte in the "GET MESSAGE" command
3464 */
3465 recv_msg->msg.data_len = msg->rsp_size - 4;
3466 memcpy(recv_msg->msg_data,
3467 &(msg->rsp[4]),
3468 msg->rsp_size - 4);
3469 deliver_response(recv_msg);
3470 }
3471 }
3472
3473 return rv;
3474}
3475
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
3477 struct ipmi_smi_msg *msg)
3478{
3479 struct ipmi_system_interface_addr *smi_addr;
Corey Minyardc70d7492008-04-29 01:01:09 -07003480
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 recv_msg->msgid = 0;
3482 smi_addr = (struct ipmi_system_interface_addr *) &(recv_msg->addr);
3483 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3484 smi_addr->channel = IPMI_BMC_CHANNEL;
3485 smi_addr->lun = msg->rsp[0] & 3;
3486 recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
3487 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3488 recv_msg->msg.cmd = msg->rsp[1];
3489 memcpy(recv_msg->msg_data, &(msg->rsp[3]), msg->rsp_size - 3);
3490 recv_msg->msg.data = recv_msg->msg_data;
3491 recv_msg->msg.data_len = msg->rsp_size - 3;
3492}
3493
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494static int handle_read_event_rsp(ipmi_smi_t intf,
3495 struct ipmi_smi_msg *msg)
3496{
3497 struct ipmi_recv_msg *recv_msg, *recv_msg2;
3498 struct list_head msgs;
3499 ipmi_user_t user;
3500 int rv = 0;
3501 int deliver_count = 0;
3502 unsigned long flags;
3503
3504 if (msg->rsp_size < 19) {
3505 /* Message is too small to be an IPMB event. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003506 ipmi_inc_stat(intf, invalid_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 return 0;
3508 }
3509
3510 if (msg->rsp[2] != 0) {
3511 /* An error getting the event, just ignore it. */
3512 return 0;
3513 }
3514
3515 INIT_LIST_HEAD(&msgs);
3516
Corey Minyard393d2cc2005-11-07 00:59:54 -08003517 spin_lock_irqsave(&intf->events_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003519 ipmi_inc_stat(intf, events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520
Corey Minyardc70d7492008-04-29 01:01:09 -07003521 /*
3522 * Allocate and fill in one message for every user that is
3523 * getting events.
3524 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08003525 rcu_read_lock();
3526 list_for_each_entry_rcu(user, &intf->users, link) {
Corey Minyard8a3628d2006-03-31 02:30:40 -08003527 if (!user->gets_events)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 continue;
3529
3530 recv_msg = ipmi_alloc_recv_msg();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003531 if (!recv_msg) {
Corey Minyard393d2cc2005-11-07 00:59:54 -08003532 rcu_read_unlock();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003533 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs,
3534 link) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 list_del(&recv_msg->link);
3536 ipmi_free_recv_msg(recv_msg);
3537 }
Corey Minyardc70d7492008-04-29 01:01:09 -07003538 /*
3539 * We couldn't allocate memory for the
3540 * message, so requeue it for handling
3541 * later.
3542 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 rv = 1;
3544 goto out;
3545 }
3546
3547 deliver_count++;
3548
3549 copy_event_into_recv_msg(recv_msg, msg);
3550 recv_msg->user = user;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003551 kref_get(&user->refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 list_add_tail(&(recv_msg->link), &msgs);
3553 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08003554 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
3556 if (deliver_count) {
3557 /* Now deliver all the messages. */
3558 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
3559 list_del(&recv_msg->link);
3560 deliver_response(recv_msg);
3561 }
3562 } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003563 /*
3564 * No one to receive the message, put it in queue if there's
3565 * not already too many things in the queue.
3566 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 recv_msg = ipmi_alloc_recv_msg();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003568 if (!recv_msg) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003569 /*
3570 * We couldn't allocate memory for the
3571 * message, so requeue it for handling
3572 * later.
3573 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 rv = 1;
3575 goto out;
3576 }
3577
3578 copy_event_into_recv_msg(recv_msg, msg);
3579 list_add_tail(&(recv_msg->link), &(intf->waiting_events));
Corey Minyard4791c032006-04-10 22:54:31 -07003580 intf->waiting_events_count++;
Corey Minyard87ebd062008-04-29 01:01:04 -07003581 } else if (!intf->event_msg_printed) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003582 /*
3583 * There's too many things in the queue, discard this
3584 * message.
3585 */
Corey Minyard87ebd062008-04-29 01:01:04 -07003586 printk(KERN_WARNING PFX "Event queue full, discarding"
3587 " incoming events\n");
3588 intf->event_msg_printed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 }
3590
3591 out:
3592 spin_unlock_irqrestore(&(intf->events_lock), flags);
3593
3594 return rv;
3595}
3596
3597static int handle_bmc_rsp(ipmi_smi_t intf,
3598 struct ipmi_smi_msg *msg)
3599{
3600 struct ipmi_recv_msg *recv_msg;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003601 struct ipmi_user *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602
3603 recv_msg = (struct ipmi_recv_msg *) msg->user_data;
Corey Minyardc70d7492008-04-29 01:01:09 -07003604 if (recv_msg == NULL) {
3605 printk(KERN_WARNING
3606 "IPMI message received with no owner. This\n"
3607 "could be because of a malformed message, or\n"
3608 "because of a hardware error. Contact your\n"
3609 "hardware vender for assistance\n");
Corey Minyard56a55ec2005-09-06 15:18:42 -07003610 return 0;
3611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612
Corey Minyard393d2cc2005-11-07 00:59:54 -08003613 user = recv_msg->user;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 /* Make sure the user still exists. */
Corey Minyard393d2cc2005-11-07 00:59:54 -08003615 if (user && !user->valid) {
Corey Minyard56a55ec2005-09-06 15:18:42 -07003616 /* The user for the message went away, so give up. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003617 ipmi_inc_stat(intf, unhandled_local_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 ipmi_free_recv_msg(recv_msg);
3619 } else {
3620 struct ipmi_system_interface_addr *smi_addr;
3621
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003622 ipmi_inc_stat(intf, handled_local_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3624 recv_msg->msgid = msg->msgid;
3625 smi_addr = ((struct ipmi_system_interface_addr *)
3626 &(recv_msg->addr));
3627 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3628 smi_addr->channel = IPMI_BMC_CHANNEL;
3629 smi_addr->lun = msg->rsp[0] & 3;
3630 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3631 recv_msg->msg.cmd = msg->rsp[1];
3632 memcpy(recv_msg->msg_data,
3633 &(msg->rsp[2]),
3634 msg->rsp_size - 2);
3635 recv_msg->msg.data = recv_msg->msg_data;
3636 recv_msg->msg.data_len = msg->rsp_size - 2;
3637 deliver_response(recv_msg);
3638 }
3639
3640 return 0;
3641}
3642
Corey Minyardc70d7492008-04-29 01:01:09 -07003643/*
Corey Minyard7adf5792012-03-28 14:42:49 -07003644 * Handle a received message. Return 1 if the message should be requeued,
Corey Minyardc70d7492008-04-29 01:01:09 -07003645 * 0 if the message should be freed, or -1 if the message should not
3646 * be freed or requeued.
3647 */
Corey Minyard7adf5792012-03-28 14:42:49 -07003648static int handle_one_recv_msg(ipmi_smi_t intf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 struct ipmi_smi_msg *msg)
3650{
3651 int requeue;
3652 int chan;
3653
3654#ifdef DEBUG_MSGING
3655 int m;
3656 printk("Recv:");
Corey Minyarde8b33612005-09-06 15:18:45 -07003657 for (m = 0; m < msg->rsp_size; m++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 printk(" %2.2x", msg->rsp[m]);
3659 printk("\n");
3660#endif
3661 if (msg->rsp_size < 2) {
3662 /* Message is too small to be correct. */
3663 printk(KERN_WARNING PFX "BMC returned to small a message"
3664 " for netfn %x cmd %x, got %d bytes\n",
3665 (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size);
3666
3667 /* Generate an error response for the message. */
3668 msg->rsp[0] = msg->data[0] | (1 << 2);
3669 msg->rsp[1] = msg->data[1];
3670 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
3671 msg->rsp_size = 3;
Corey Minyardc70d7492008-04-29 01:01:09 -07003672 } else if (((msg->rsp[0] >> 2) != ((msg->data[0] >> 2) | 1))
3673 || (msg->rsp[1] != msg->data[1])) {
3674 /*
3675 * The NetFN and Command in the response is not even
3676 * marginally correct.
3677 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 printk(KERN_WARNING PFX "BMC returned incorrect response,"
3679 " expected netfn %x cmd %x, got netfn %x cmd %x\n",
3680 (msg->data[0] >> 2) | 1, msg->data[1],
3681 msg->rsp[0] >> 2, msg->rsp[1]);
3682
3683 /* Generate an error response for the message. */
3684 msg->rsp[0] = msg->data[0] | (1 << 2);
3685 msg->rsp[1] = msg->data[1];
3686 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
3687 msg->rsp_size = 3;
3688 }
3689
3690 if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
3691 && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07003692 && (msg->user_data != NULL)) {
3693 /*
3694 * It's a response to a response we sent. For this we
3695 * deliver a send message response to the user.
3696 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08003697 struct ipmi_recv_msg *recv_msg = msg->user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698
3699 requeue = 0;
3700 if (msg->rsp_size < 2)
3701 /* Message is too small to be correct. */
3702 goto out;
3703
3704 chan = msg->data[2] & 0x0f;
3705 if (chan >= IPMI_MAX_CHANNELS)
3706 /* Invalid channel number */
3707 goto out;
3708
Corey Minyard393d2cc2005-11-07 00:59:54 -08003709 if (!recv_msg)
3710 goto out;
3711
3712 /* Make sure the user still exists. */
3713 if (!recv_msg->user || !recv_msg->user->valid)
3714 goto out;
3715
3716 recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
3717 recv_msg->msg.data = recv_msg->msg_data;
3718 recv_msg->msg.data_len = 1;
3719 recv_msg->msg_data[0] = msg->rsp[2];
3720 deliver_response(recv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
Corey Minyardc70d7492008-04-29 01:01:09 -07003722 && (msg->rsp[1] == IPMI_GET_MSG_CMD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 /* It's from the receive queue. */
3724 chan = msg->rsp[3] & 0xf;
3725 if (chan >= IPMI_MAX_CHANNELS) {
3726 /* Invalid channel number */
3727 requeue = 0;
3728 goto out;
3729 }
3730
dann frazier4dec3022009-04-21 12:24:05 -07003731 /*
Corey Minyard9a2845c2009-05-20 13:36:17 -05003732 * We need to make sure the channels have been initialized.
3733 * The channel_handler routine will set the "curr_channel"
3734 * equal to or greater than IPMI_MAX_CHANNELS when all the
3735 * channels for this interface have been initialized.
3736 */
dann frazier4dec3022009-04-21 12:24:05 -07003737 if (intf->curr_channel < IPMI_MAX_CHANNELS) {
Corey Minyard9a2845c2009-05-20 13:36:17 -05003738 requeue = 0; /* Throw the message away */
dann frazier4dec3022009-04-21 12:24:05 -07003739 goto out;
3740 }
3741
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 switch (intf->channels[chan].medium) {
3743 case IPMI_CHANNEL_MEDIUM_IPMB:
3744 if (msg->rsp[4] & 0x04) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003745 /*
3746 * It's a response, so find the
3747 * requesting message and send it up.
3748 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 requeue = handle_ipmb_get_msg_rsp(intf, msg);
3750 } else {
Corey Minyardc70d7492008-04-29 01:01:09 -07003751 /*
3752 * It's a command to the SMS from some other
3753 * entity. Handle that.
3754 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 requeue = handle_ipmb_get_msg_cmd(intf, msg);
3756 }
3757 break;
3758
3759 case IPMI_CHANNEL_MEDIUM_8023LAN:
3760 case IPMI_CHANNEL_MEDIUM_ASYNC:
3761 if (msg->rsp[6] & 0x04) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003762 /*
3763 * It's a response, so find the
3764 * requesting message and send it up.
3765 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 requeue = handle_lan_get_msg_rsp(intf, msg);
3767 } else {
Corey Minyardc70d7492008-04-29 01:01:09 -07003768 /*
3769 * It's a command to the SMS from some other
3770 * entity. Handle that.
3771 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 requeue = handle_lan_get_msg_cmd(intf, msg);
3773 }
3774 break;
3775
3776 default:
dann frazier4dec3022009-04-21 12:24:05 -07003777 /* Check for OEM Channels. Clients had better
3778 register for these commands. */
3779 if ((intf->channels[chan].medium
3780 >= IPMI_CHANNEL_MEDIUM_OEM_MIN)
3781 && (intf->channels[chan].medium
3782 <= IPMI_CHANNEL_MEDIUM_OEM_MAX)) {
3783 requeue = handle_oem_get_msg_cmd(intf, msg);
3784 } else {
3785 /*
3786 * We don't handle the channel type, so just
3787 * free the message.
3788 */
3789 requeue = 0;
3790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 }
3792
3793 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
Corey Minyardc70d7492008-04-29 01:01:09 -07003794 && (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD)) {
Adam Buchbinderb3834be2012-09-19 21:48:02 -04003795 /* It's an asynchronous event. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 requeue = handle_read_event_rsp(intf, msg);
3797 } else {
3798 /* It's a response from the local BMC. */
3799 requeue = handle_bmc_rsp(intf, msg);
3800 }
3801
3802 out:
3803 return requeue;
3804}
3805
Corey Minyard7adf5792012-03-28 14:42:49 -07003806/*
3807 * If there are messages in the queue or pretimeouts, handle them.
3808 */
3809static void handle_new_recv_msgs(ipmi_smi_t intf)
3810{
3811 struct ipmi_smi_msg *smi_msg;
3812 unsigned long flags = 0;
3813 int rv;
3814 int run_to_completion = intf->run_to_completion;
3815
3816 /* See if any waiting messages need to be processed. */
3817 if (!run_to_completion)
Corey Minyard65be7542014-11-06 20:48:02 -06003818 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
3819 while (!list_empty(&intf->waiting_rcv_msgs)) {
3820 smi_msg = list_entry(intf->waiting_rcv_msgs.next,
Corey Minyard7adf5792012-03-28 14:42:49 -07003821 struct ipmi_smi_msg, link);
Corey Minyard7adf5792012-03-28 14:42:49 -07003822 if (!run_to_completion)
Corey Minyard65be7542014-11-06 20:48:02 -06003823 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
3824 flags);
Corey Minyard7adf5792012-03-28 14:42:49 -07003825 rv = handle_one_recv_msg(intf, smi_msg);
3826 if (!run_to_completion)
Corey Minyard65be7542014-11-06 20:48:02 -06003827 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003828 if (rv > 0) {
Corey Minyard7adf5792012-03-28 14:42:49 -07003829 /*
3830 * To preserve message order, quit if we
3831 * can't handle a message.
3832 */
Corey Minyard7adf5792012-03-28 14:42:49 -07003833 break;
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003834 } else {
3835 list_del(&smi_msg->link);
3836 if (rv == 0)
3837 /* Message handled */
3838 ipmi_free_smi_msg(smi_msg);
3839 /* If rv < 0, fatal error, del but don't free. */
Corey Minyard7adf5792012-03-28 14:42:49 -07003840 }
3841 }
3842 if (!run_to_completion)
Corey Minyard65be7542014-11-06 20:48:02 -06003843 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock, flags);
Corey Minyard7adf5792012-03-28 14:42:49 -07003844
3845 /*
3846 * If the pretimout count is non-zero, decrement one from it and
3847 * deliver pretimeouts to all the users.
3848 */
3849 if (atomic_add_unless(&intf->watchdog_pretimeouts_to_deliver, -1, 0)) {
3850 ipmi_user_t user;
3851
3852 rcu_read_lock();
3853 list_for_each_entry_rcu(user, &intf->users, link) {
3854 if (user->handler->ipmi_watchdog_pretimeout)
3855 user->handler->ipmi_watchdog_pretimeout(
3856 user->handler_data);
3857 }
3858 rcu_read_unlock();
3859 }
3860}
3861
3862static void smi_recv_tasklet(unsigned long val)
3863{
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003864 unsigned long flags = 0; /* keep us warning-free. */
3865 ipmi_smi_t intf = (ipmi_smi_t) val;
3866 int run_to_completion = intf->run_to_completion;
3867 struct ipmi_smi_msg *newmsg = NULL;
3868
3869 /*
3870 * Start the next message if available.
3871 *
3872 * Do this here, not in the actual receiver, because we may deadlock
3873 * because the lower layer is allowed to hold locks while calling
3874 * message delivery.
3875 */
3876 if (!run_to_completion)
3877 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
3878 if (intf->curr_msg == NULL && !intf->in_shutdown) {
3879 struct list_head *entry = NULL;
3880
3881 /* Pick the high priority queue first. */
3882 if (!list_empty(&intf->hp_xmit_msgs))
3883 entry = intf->hp_xmit_msgs.next;
3884 else if (!list_empty(&intf->xmit_msgs))
3885 entry = intf->xmit_msgs.next;
3886
3887 if (entry) {
3888 list_del(entry);
3889 newmsg = list_entry(entry, struct ipmi_smi_msg, link);
3890 intf->curr_msg = newmsg;
3891 }
3892 }
3893 if (!run_to_completion)
3894 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
3895 if (newmsg)
Corey Minyard99ab32f2014-11-07 07:57:31 -06003896 intf->handlers->sender(intf->send_info, newmsg);
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003897
3898 handle_new_recv_msgs(intf);
Corey Minyard7adf5792012-03-28 14:42:49 -07003899}
3900
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901/* Handle a new message from the lower layer. */
3902void ipmi_smi_msg_received(ipmi_smi_t intf,
3903 struct ipmi_smi_msg *msg)
3904{
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003905 unsigned long flags = 0; /* keep us warning-free. */
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003906 int run_to_completion = intf->run_to_completion;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 if ((msg->data_size >= 2)
3909 && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
3910 && (msg->data[1] == IPMI_SEND_MSG_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07003911 && (msg->user_data == NULL)) {
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003912
3913 if (intf->in_shutdown)
3914 goto free_msg;
3915
Corey Minyardc70d7492008-04-29 01:01:09 -07003916 /*
3917 * This is the local response to a command send, start
3918 * the timer for these. The user_data will not be
3919 * NULL if this is a response send, and we will let
3920 * response sends just go through.
3921 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922
Corey Minyardc70d7492008-04-29 01:01:09 -07003923 /*
3924 * Check for errors, if we get certain errors (ones
3925 * that mean basically we can try again later), we
3926 * ignore them and start the timer. Otherwise we
3927 * report the error immediately.
3928 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
3930 && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
Corey Minyard46d52b02006-11-08 17:44:55 -08003931 && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR)
3932 && (msg->rsp[2] != IPMI_BUS_ERR)
Corey Minyardc70d7492008-04-29 01:01:09 -07003933 && (msg->rsp[2] != IPMI_NAK_ON_WRITE_ERR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 int chan = msg->rsp[3] & 0xf;
3935
3936 /* Got an error sending the message, handle it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 if (chan >= IPMI_MAX_CHANNELS)
3938 ; /* This shouldn't happen */
3939 else if ((intf->channels[chan].medium
3940 == IPMI_CHANNEL_MEDIUM_8023LAN)
3941 || (intf->channels[chan].medium
3942 == IPMI_CHANNEL_MEDIUM_ASYNC))
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003943 ipmi_inc_stat(intf, sent_lan_command_errs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 else
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003945 ipmi_inc_stat(intf, sent_ipmb_command_errs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 intf_err_seq(intf, msg->msgid, msg->rsp[2]);
Corey Minyardc70d7492008-04-29 01:01:09 -07003947 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 /* The message was sent, start the timer. */
3949 intf_start_seq_timer(intf, msg->msgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003951free_msg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 ipmi_free_smi_msg(msg);
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003953 } else {
3954 /*
3955 * To preserve message order, we keep a queue and deliver from
3956 * a tasklet.
3957 */
3958 if (!run_to_completion)
3959 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
3960 list_add_tail(&msg->link, &intf->waiting_rcv_msgs);
3961 if (!run_to_completion)
3962 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
3963 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 }
3965
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003966 if (!run_to_completion)
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003967 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
Corey Minyardb2234ee2015-02-19 11:29:24 -06003968 /*
3969 * We can get an asynchronous event or receive message in addition
3970 * to commands we send.
3971 */
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003972 if (msg == intf->curr_msg)
3973 intf->curr_msg = NULL;
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003974 if (!run_to_completion)
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003975 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
Corey Minyardc70d7492008-04-29 01:01:09 -07003976
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003977 if (run_to_completion)
3978 smi_recv_tasklet((unsigned long) intf);
3979 else
3980 tasklet_schedule(&intf->recv_tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981}
Corey Minyardc70d7492008-04-29 01:01:09 -07003982EXPORT_SYMBOL(ipmi_smi_msg_received);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983
3984void ipmi_smi_watchdog_pretimeout(ipmi_smi_t intf)
3985{
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003986 if (intf->in_shutdown)
3987 return;
3988
Corey Minyard7adf5792012-03-28 14:42:49 -07003989 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 1);
3990 tasklet_schedule(&intf->recv_tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991}
Corey Minyardc70d7492008-04-29 01:01:09 -07003992EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
Corey Minyard882fe012005-05-01 08:59:12 -07003994static struct ipmi_smi_msg *
3995smi_from_recv_msg(ipmi_smi_t intf, struct ipmi_recv_msg *recv_msg,
3996 unsigned char seq, long seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997{
Corey Minyard882fe012005-05-01 08:59:12 -07003998 struct ipmi_smi_msg *smi_msg = ipmi_alloc_smi_msg();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 if (!smi_msg)
Corey Minyardc70d7492008-04-29 01:01:09 -07004000 /*
4001 * If we can't allocate the message, then just return, we
4002 * get 4 retries, so this should be ok.
4003 */
Corey Minyard882fe012005-05-01 08:59:12 -07004004 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
4006 memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
4007 smi_msg->data_size = recv_msg->msg.data_len;
4008 smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
Corey Minyardc70d7492008-04-29 01:01:09 -07004009
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010#ifdef DEBUG_MSGING
4011 {
4012 int m;
4013 printk("Resend: ");
Corey Minyarde8b33612005-09-06 15:18:45 -07004014 for (m = 0; m < smi_msg->data_size; m++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 printk(" %2.2x", smi_msg->data[m]);
4016 printk("\n");
4017 }
4018#endif
Corey Minyard882fe012005-05-01 08:59:12 -07004019 return smi_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020}
4021
Corey Minyard393d2cc2005-11-07 00:59:54 -08004022static void check_msg_timeout(ipmi_smi_t intf, struct seq_table *ent,
4023 struct list_head *timeouts, long timeout_period,
Corey Minyard89986492014-04-14 09:46:54 -05004024 int slot, unsigned long *flags,
4025 unsigned int *waiting_msgs)
Corey Minyard393d2cc2005-11-07 00:59:54 -08004026{
Corey Minyardb2c03942006-12-06 20:41:00 -08004027 struct ipmi_recv_msg *msg;
Corey Minyard81d02b72015-06-13 10:34:25 -05004028 const struct ipmi_smi_handlers *handlers;
Corey Minyardb2c03942006-12-06 20:41:00 -08004029
Corey Minyard7ea0ed22014-11-06 16:58:48 -06004030 if (intf->in_shutdown)
Corey Minyardb2c03942006-12-06 20:41:00 -08004031 return;
Corey Minyard393d2cc2005-11-07 00:59:54 -08004032
4033 if (!ent->inuse)
4034 return;
4035
4036 ent->timeout -= timeout_period;
Corey Minyard89986492014-04-14 09:46:54 -05004037 if (ent->timeout > 0) {
4038 (*waiting_msgs)++;
Corey Minyard393d2cc2005-11-07 00:59:54 -08004039 return;
Corey Minyard89986492014-04-14 09:46:54 -05004040 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08004041
4042 if (ent->retries_left == 0) {
4043 /* The message has used all its retries. */
4044 ent->inuse = 0;
4045 msg = ent->recv_msg;
4046 list_add_tail(&msg->link, timeouts);
Corey Minyard393d2cc2005-11-07 00:59:54 -08004047 if (ent->broadcast)
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07004048 ipmi_inc_stat(intf, timed_out_ipmb_broadcasts);
Corey Minyard25176ed2009-04-21 12:24:04 -07004049 else if (is_lan_addr(&ent->recv_msg->addr))
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07004050 ipmi_inc_stat(intf, timed_out_lan_commands);
Corey Minyard393d2cc2005-11-07 00:59:54 -08004051 else
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07004052 ipmi_inc_stat(intf, timed_out_ipmb_commands);
Corey Minyard393d2cc2005-11-07 00:59:54 -08004053 } else {
4054 struct ipmi_smi_msg *smi_msg;
4055 /* More retries, send again. */
4056
Corey Minyard89986492014-04-14 09:46:54 -05004057 (*waiting_msgs)++;
4058
Corey Minyardc70d7492008-04-29 01:01:09 -07004059 /*
4060 * Start with the max timer, set to normal timer after
4061 * the message is sent.
4062 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08004063 ent->timeout = MAX_MSG_TIMEOUT;
4064 ent->retries_left--;
Corey Minyard393d2cc2005-11-07 00:59:54 -08004065 smi_msg = smi_from_recv_msg(intf, ent->recv_msg, slot,
4066 ent->seqid);
Corey Minyard25176ed2009-04-21 12:24:04 -07004067 if (!smi_msg) {
4068 if (is_lan_addr(&ent->recv_msg->addr))
4069 ipmi_inc_stat(intf,
4070 dropped_rexmit_lan_commands);
4071 else
4072 ipmi_inc_stat(intf,
4073 dropped_rexmit_ipmb_commands);
Corey Minyard393d2cc2005-11-07 00:59:54 -08004074 return;
Corey Minyard25176ed2009-04-21 12:24:04 -07004075 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08004076
4077 spin_unlock_irqrestore(&intf->seq_lock, *flags);
Corey Minyardb2c03942006-12-06 20:41:00 -08004078
Corey Minyardc70d7492008-04-29 01:01:09 -07004079 /*
4080 * Send the new message. We send with a zero
4081 * priority. It timed out, I doubt time is that
4082 * critical now, and high priority messages are really
4083 * only for messages to the local MC, which don't get
4084 * resent.
4085 */
Corey Minyardb2c03942006-12-06 20:41:00 -08004086 handlers = intf->handlers;
Corey Minyard25176ed2009-04-21 12:24:04 -07004087 if (handlers) {
4088 if (is_lan_addr(&ent->recv_msg->addr))
4089 ipmi_inc_stat(intf,
4090 retransmitted_lan_commands);
4091 else
4092 ipmi_inc_stat(intf,
4093 retransmitted_ipmb_commands);
4094
Corey Minyard81d02b72015-06-13 10:34:25 -05004095 smi_send(intf, handlers, smi_msg, 0);
Corey Minyard25176ed2009-04-21 12:24:04 -07004096 } else
Corey Minyardb2c03942006-12-06 20:41:00 -08004097 ipmi_free_smi_msg(smi_msg);
4098
Corey Minyard393d2cc2005-11-07 00:59:54 -08004099 spin_lock_irqsave(&intf->seq_lock, *flags);
4100 }
4101}
4102
Corey Minyard89986492014-04-14 09:46:54 -05004103static unsigned int ipmi_timeout_handler(ipmi_smi_t intf, long timeout_period)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 struct list_head timeouts;
4106 struct ipmi_recv_msg *msg, *msg2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 unsigned long flags;
Corey Minyardbca03242006-12-06 20:40:57 -08004108 int i;
Corey Minyard89986492014-04-14 09:46:54 -05004109 unsigned int waiting_msgs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110
Corey Minyard89986492014-04-14 09:46:54 -05004111 /*
4112 * Go through the seq table and find any messages that
4113 * have timed out, putting them in the timeouts
4114 * list.
4115 */
4116 INIT_LIST_HEAD(&timeouts);
4117 spin_lock_irqsave(&intf->seq_lock, flags);
4118 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++)
4119 check_msg_timeout(intf, &(intf->seq_table[i]),
4120 &timeouts, timeout_period, i,
4121 &flags, &waiting_msgs);
4122 spin_unlock_irqrestore(&intf->seq_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123
Corey Minyard89986492014-04-14 09:46:54 -05004124 list_for_each_entry_safe(msg, msg2, &timeouts, link)
4125 deliver_err_response(msg, IPMI_TIMEOUT_COMPLETION_CODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126
Corey Minyard89986492014-04-14 09:46:54 -05004127 /*
4128 * Maintenance mode handling. Check the timeout
4129 * optimistically before we claim the lock. It may
4130 * mean a timeout gets missed occasionally, but that
4131 * only means the timeout gets extended by one period
4132 * in that case. No big deal, and it avoids the lock
4133 * most of the time.
4134 */
4135 if (intf->auto_maintenance_timeout > 0) {
4136 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
Corey Minyardb9675132006-12-06 20:41:02 -08004137 if (intf->auto_maintenance_timeout > 0) {
Corey Minyard89986492014-04-14 09:46:54 -05004138 intf->auto_maintenance_timeout
4139 -= timeout_period;
4140 if (!intf->maintenance_mode
4141 && (intf->auto_maintenance_timeout <= 0)) {
Corey Minyard7aefac22014-04-14 09:46:56 -05004142 intf->maintenance_mode_enable = false;
Corey Minyard89986492014-04-14 09:46:54 -05004143 maintenance_mode_update(intf);
Corey Minyardb9675132006-12-06 20:41:02 -08004144 }
Corey Minyardb9675132006-12-06 20:41:02 -08004145 }
Corey Minyard89986492014-04-14 09:46:54 -05004146 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
4147 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 }
Corey Minyard89986492014-04-14 09:46:54 -05004149
4150 tasklet_schedule(&intf->recv_tasklet);
4151
4152 return waiting_msgs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153}
4154
Corey Minyard89986492014-04-14 09:46:54 -05004155static void ipmi_request_event(ipmi_smi_t intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156{
Corey Minyard89986492014-04-14 09:46:54 -05004157 /* No event requests when in maintenance mode. */
4158 if (intf->maintenance_mode_enable)
4159 return;
Corey Minyardb9675132006-12-06 20:41:02 -08004160
Corey Minyard7ea0ed22014-11-06 16:58:48 -06004161 if (!intf->in_shutdown)
4162 intf->handlers->request_events(intf->send_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163}
4164
4165static struct timer_list ipmi_timer;
4166
Corey Minyard8f43f842005-06-23 22:01:40 -07004167static atomic_t stop_operation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168
4169static void ipmi_timeout(unsigned long data)
4170{
Corey Minyard89986492014-04-14 09:46:54 -05004171 ipmi_smi_t intf;
4172 int nt = 0;
4173
Corey Minyard8f43f842005-06-23 22:01:40 -07004174 if (atomic_read(&stop_operation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176
Corey Minyard89986492014-04-14 09:46:54 -05004177 rcu_read_lock();
4178 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4179 int lnt = 0;
4180
4181 if (atomic_read(&intf->event_waiters)) {
4182 intf->ticks_to_req_ev--;
4183 if (intf->ticks_to_req_ev == 0) {
4184 ipmi_request_event(intf);
4185 intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
4186 }
4187 lnt++;
4188 }
4189
4190 lnt += ipmi_timeout_handler(intf, IPMI_TIMEOUT_TIME);
4191
4192 lnt = !!lnt;
4193 if (lnt != intf->last_needs_timer &&
4194 intf->handlers->set_need_watch)
4195 intf->handlers->set_need_watch(intf->send_info, lnt);
4196 intf->last_needs_timer = lnt;
4197
4198 nt += lnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 }
Corey Minyard89986492014-04-14 09:46:54 -05004200 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201
Corey Minyard89986492014-04-14 09:46:54 -05004202 if (nt)
4203 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204}
4205
Corey Minyard89986492014-04-14 09:46:54 -05004206static void need_waiter(ipmi_smi_t intf)
4207{
4208 /* Racy, but worst case we start the timer twice. */
4209 if (!timer_pending(&ipmi_timer))
4210 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
4211}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212
4213static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
4214static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
4215
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216static void free_smi_msg(struct ipmi_smi_msg *msg)
4217{
4218 atomic_dec(&smi_msg_inuse_count);
4219 kfree(msg);
4220}
4221
4222struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
4223{
4224 struct ipmi_smi_msg *rv;
4225 rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
4226 if (rv) {
4227 rv->done = free_smi_msg;
4228 rv->user_data = NULL;
4229 atomic_inc(&smi_msg_inuse_count);
4230 }
4231 return rv;
4232}
Corey Minyardc70d7492008-04-29 01:01:09 -07004233EXPORT_SYMBOL(ipmi_alloc_smi_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234
4235static void free_recv_msg(struct ipmi_recv_msg *msg)
4236{
4237 atomic_dec(&recv_msg_inuse_count);
4238 kfree(msg);
4239}
4240
Adrian Bunk74006302008-04-29 01:01:14 -07004241static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242{
4243 struct ipmi_recv_msg *rv;
4244
4245 rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
4246 if (rv) {
Corey Minyarda9eec552006-08-31 21:27:45 -07004247 rv->user = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 rv->done = free_recv_msg;
4249 atomic_inc(&recv_msg_inuse_count);
4250 }
4251 return rv;
4252}
4253
Corey Minyard393d2cc2005-11-07 00:59:54 -08004254void ipmi_free_recv_msg(struct ipmi_recv_msg *msg)
4255{
4256 if (msg->user)
4257 kref_put(&msg->user->refcount, free_user);
4258 msg->done(msg);
4259}
Corey Minyardc70d7492008-04-29 01:01:09 -07004260EXPORT_SYMBOL(ipmi_free_recv_msg);
Corey Minyard393d2cc2005-11-07 00:59:54 -08004261
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262#ifdef CONFIG_IPMI_PANIC_EVENT
4263
Corey Minyard895dcfd2012-03-28 14:42:49 -07004264static atomic_t panic_done_count = ATOMIC_INIT(0);
4265
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
4267{
Corey Minyard895dcfd2012-03-28 14:42:49 -07004268 atomic_dec(&panic_done_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269}
4270
4271static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
4272{
Corey Minyard895dcfd2012-03-28 14:42:49 -07004273 atomic_dec(&panic_done_count);
4274}
4275
4276/*
4277 * Inside a panic, send a message and wait for a response.
4278 */
4279static void ipmi_panic_request_and_wait(ipmi_smi_t intf,
4280 struct ipmi_addr *addr,
4281 struct kernel_ipmi_msg *msg)
4282{
4283 struct ipmi_smi_msg smi_msg;
4284 struct ipmi_recv_msg recv_msg;
4285 int rv;
4286
4287 smi_msg.done = dummy_smi_done_handler;
4288 recv_msg.done = dummy_recv_done_handler;
4289 atomic_add(2, &panic_done_count);
4290 rv = i_ipmi_request(NULL,
4291 intf,
4292 addr,
4293 0,
4294 msg,
4295 intf,
4296 &smi_msg,
4297 &recv_msg,
4298 0,
4299 intf->channels[0].address,
4300 intf->channels[0].lun,
4301 0, 1); /* Don't retry, and don't wait. */
4302 if (rv)
4303 atomic_sub(2, &panic_done_count);
Hidehiro Kawai82802f92015-07-27 14:55:16 +09004304 else if (intf->handlers->flush_messages)
4305 intf->handlers->flush_messages(intf->send_info);
4306
Corey Minyard895dcfd2012-03-28 14:42:49 -07004307 while (atomic_read(&panic_done_count) != 0)
4308 ipmi_poll(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309}
4310
4311#ifdef CONFIG_IPMI_PANIC_STRING
Corey Minyard56a55ec2005-09-06 15:18:42 -07004312static void event_receiver_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313{
Corey Minyard56a55ec2005-09-06 15:18:42 -07004314 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4315 && (msg->msg.netfn == IPMI_NETFN_SENSOR_EVENT_RESPONSE)
4316 && (msg->msg.cmd == IPMI_GET_EVENT_RECEIVER_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07004317 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 /* A get event receiver command, save it. */
Corey Minyard56a55ec2005-09-06 15:18:42 -07004319 intf->event_receiver = msg->msg.data[1];
4320 intf->event_receiver_lun = msg->msg.data[2] & 0x3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 }
4322}
4323
Corey Minyard56a55ec2005-09-06 15:18:42 -07004324static void device_id_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325{
Corey Minyard56a55ec2005-09-06 15:18:42 -07004326 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4327 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
4328 && (msg->msg.cmd == IPMI_GET_DEVICE_ID_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07004329 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
4330 /*
4331 * A get device id command, save if we are an event
4332 * receiver or generator.
4333 */
Corey Minyard56a55ec2005-09-06 15:18:42 -07004334 intf->local_sel_device = (msg->msg.data[6] >> 2) & 1;
4335 intf->local_event_generator = (msg->msg.data[6] >> 5) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 }
4337}
4338#endif
4339
4340static void send_panic_events(char *str)
4341{
4342 struct kernel_ipmi_msg msg;
4343 ipmi_smi_t intf;
4344 unsigned char data[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 struct ipmi_system_interface_addr *si;
4346 struct ipmi_addr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
4348 si = (struct ipmi_system_interface_addr *) &addr;
4349 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4350 si->channel = IPMI_BMC_CHANNEL;
4351 si->lun = 0;
4352
4353 /* Fill in an event telling that we have failed. */
4354 msg.netfn = 0x04; /* Sensor or Event. */
4355 msg.cmd = 2; /* Platform event command. */
4356 msg.data = data;
4357 msg.data_len = 8;
Matt Domschcda315a2005-12-12 00:37:32 -08004358 data[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 data[1] = 0x03; /* This is for IPMI 1.0. */
4360 data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
4361 data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
4362 data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
4363
Corey Minyardc70d7492008-04-29 01:01:09 -07004364 /*
4365 * Put a few breadcrumbs in. Hopefully later we can add more things
4366 * to make the panic events more useful.
4367 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 if (str) {
4369 data[3] = str[0];
4370 data[6] = str[1];
4371 data[7] = str[2];
4372 }
4373
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 /* For every registered interface, send the event. */
Corey Minyardbca03242006-12-06 20:40:57 -08004375 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
Corey Minyardb2c03942006-12-06 20:41:00 -08004376 if (!intf->handlers)
4377 /* Interface is not ready. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 continue;
4379
4380 /* Send the event announcing the panic. */
Corey Minyard895dcfd2012-03-28 14:42:49 -07004381 ipmi_panic_request_and_wait(intf, &addr, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 }
4383
4384#ifdef CONFIG_IPMI_PANIC_STRING
Corey Minyardc70d7492008-04-29 01:01:09 -07004385 /*
4386 * On every interface, dump a bunch of OEM event holding the
4387 * string.
4388 */
4389 if (!str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 return;
4391
Corey Minyardbca03242006-12-06 20:40:57 -08004392 /* For every registered interface, send the event. */
4393 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 char *p = str;
4395 struct ipmi_ipmb_addr *ipmb;
4396 int j;
4397
Corey Minyardbca03242006-12-06 20:40:57 -08004398 if (intf->intf_num == -1)
4399 /* Interface was not ready yet. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 continue;
4401
Corey Minyard78ba2fa2007-02-10 01:45:45 -08004402 /*
4403 * intf_num is used as an marker to tell if the
4404 * interface is valid. Thus we need a read barrier to
4405 * make sure data fetched before checking intf_num
4406 * won't be used.
4407 */
4408 smp_rmb();
4409
Corey Minyardc70d7492008-04-29 01:01:09 -07004410 /*
4411 * First job here is to figure out where to send the
4412 * OEM events. There's no way in IPMI to send OEM
4413 * events using an event send command, so we have to
4414 * find the SEL to put them in and stick them in
4415 * there.
4416 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417
4418 /* Get capabilities from the get device id. */
4419 intf->local_sel_device = 0;
4420 intf->local_event_generator = 0;
4421 intf->event_receiver = 0;
4422
4423 /* Request the device info from the local MC. */
4424 msg.netfn = IPMI_NETFN_APP_REQUEST;
4425 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
4426 msg.data = NULL;
4427 msg.data_len = 0;
4428 intf->null_user_handler = device_id_fetcher;
Corey Minyard895dcfd2012-03-28 14:42:49 -07004429 ipmi_panic_request_and_wait(intf, &addr, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430
4431 if (intf->local_event_generator) {
4432 /* Request the event receiver from the local MC. */
4433 msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
4434 msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
4435 msg.data = NULL;
4436 msg.data_len = 0;
4437 intf->null_user_handler = event_receiver_fetcher;
Corey Minyard895dcfd2012-03-28 14:42:49 -07004438 ipmi_panic_request_and_wait(intf, &addr, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 }
4440 intf->null_user_handler = NULL;
4441
Corey Minyardc70d7492008-04-29 01:01:09 -07004442 /*
4443 * Validate the event receiver. The low bit must not
4444 * be 1 (it must be a valid IPMB address), it cannot
4445 * be zero, and it must not be my address.
4446 */
4447 if (((intf->event_receiver & 1) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 && (intf->event_receiver != 0)
Corey Minyardc70d7492008-04-29 01:01:09 -07004449 && (intf->event_receiver != intf->channels[0].address)) {
4450 /*
4451 * The event receiver is valid, send an IPMB
4452 * message.
4453 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 ipmb = (struct ipmi_ipmb_addr *) &addr;
4455 ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
4456 ipmb->channel = 0; /* FIXME - is this right? */
4457 ipmb->lun = intf->event_receiver_lun;
4458 ipmb->slave_addr = intf->event_receiver;
4459 } else if (intf->local_sel_device) {
Corey Minyardc70d7492008-04-29 01:01:09 -07004460 /*
4461 * The event receiver was not valid (or was
4462 * me), but I am an SEL device, just dump it
4463 * in my SEL.
4464 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 si = (struct ipmi_system_interface_addr *) &addr;
4466 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4467 si->channel = IPMI_BMC_CHANNEL;
4468 si->lun = 0;
4469 } else
4470 continue; /* No where to send the event. */
4471
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
4473 msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
4474 msg.data = data;
4475 msg.data_len = 16;
4476
4477 j = 0;
4478 while (*p) {
4479 int size = strlen(p);
4480
4481 if (size > 11)
4482 size = 11;
4483 data[0] = 0;
4484 data[1] = 0;
4485 data[2] = 0xf0; /* OEM event without timestamp. */
Corey Minyardc14979b2005-09-06 15:18:38 -07004486 data[3] = intf->channels[0].address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 data[4] = j++; /* sequence # */
Corey Minyardc70d7492008-04-29 01:01:09 -07004488 /*
4489 * Always give 11 bytes, so strncpy will fill
4490 * it with zeroes for me.
4491 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 strncpy(data+5, p, 11);
4493 p += size;
4494
Corey Minyard895dcfd2012-03-28 14:42:49 -07004495 ipmi_panic_request_and_wait(intf, &addr, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 }
Corey Minyardc70d7492008-04-29 01:01:09 -07004497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498#endif /* CONFIG_IPMI_PANIC_STRING */
4499}
4500#endif /* CONFIG_IPMI_PANIC_EVENT */
4501
Randy Dunlap0c8204b2006-12-10 02:19:06 -08004502static int has_panicked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
4504static int panic_event(struct notifier_block *this,
4505 unsigned long event,
Corey Minyardc70d7492008-04-29 01:01:09 -07004506 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 ipmi_smi_t intf;
4509
Lee Revellf18190b2006-06-26 18:30:00 +02004510 if (has_panicked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 return NOTIFY_DONE;
Lee Revellf18190b2006-06-26 18:30:00 +02004512 has_panicked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513
4514 /* For every registered interface, set it to run to completion. */
Corey Minyardbca03242006-12-06 20:40:57 -08004515 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
Corey Minyardb2c03942006-12-06 20:41:00 -08004516 if (!intf->handlers)
4517 /* Interface is not ready. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 continue;
4519
Hidehiro Kawai06e5e342015-07-27 14:55:16 +09004520 /*
4521 * If we were interrupted while locking xmit_msgs_lock or
4522 * waiting_rcv_msgs_lock, the corresponding list may be
4523 * corrupted. In this case, drop items on the list for
4524 * the safety.
4525 */
4526 if (!spin_trylock(&intf->xmit_msgs_lock)) {
4527 INIT_LIST_HEAD(&intf->xmit_msgs);
4528 INIT_LIST_HEAD(&intf->hp_xmit_msgs);
4529 } else
4530 spin_unlock(&intf->xmit_msgs_lock);
4531
4532 if (!spin_trylock(&intf->waiting_rcv_msgs_lock))
4533 INIT_LIST_HEAD(&intf->waiting_rcv_msgs);
4534 else
4535 spin_unlock(&intf->waiting_rcv_msgs_lock);
4536
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07004537 intf->run_to_completion = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 intf->handlers->set_run_to_completion(intf->send_info, 1);
4539 }
4540
4541#ifdef CONFIG_IPMI_PANIC_EVENT
4542 send_panic_events(ptr);
4543#endif
4544
4545 return NOTIFY_DONE;
4546}
4547
4548static struct notifier_block panic_block = {
4549 .notifier_call = panic_event,
4550 .next = NULL,
4551 .priority = 200 /* priority: INT_MAX >= x >= 0 */
4552};
4553
4554static int ipmi_init_msghandler(void)
4555{
Corey Minyard50c812b2006-03-26 01:37:21 -08004556 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557
4558 if (initialized)
4559 return 0;
4560
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08004561 rv = driver_register(&ipmidriver.driver);
Corey Minyard50c812b2006-03-26 01:37:21 -08004562 if (rv) {
4563 printk(KERN_ERR PFX "Could not register IPMI driver\n");
4564 return rv;
4565 }
4566
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 printk(KERN_INFO "ipmi message handler version "
Corey Minyard1fdd75b2005-09-06 15:18:42 -07004568 IPMI_DRIVER_VERSION "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569
Corey Minyard3b625942005-06-23 22:01:42 -07004570#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 proc_ipmi_root = proc_mkdir("ipmi", NULL);
4572 if (!proc_ipmi_root) {
4573 printk(KERN_ERR PFX "Unable to create IPMI proc dir");
trenn@suse.de80fad5b2014-10-14 16:40:23 +02004574 driver_unregister(&ipmidriver.driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 return -ENOMEM;
4576 }
4577
Corey Minyard3b625942005-06-23 22:01:42 -07004578#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579
Corey Minyard409035e2006-06-28 04:26:53 -07004580 setup_timer(&ipmi_timer, ipmi_timeout, 0);
4581 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582
Alan Sterne041c682006-03-27 01:16:30 -08004583 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584
4585 initialized = 1;
4586
4587 return 0;
4588}
4589
Corey Minyard60ee6d52010-10-27 15:34:18 -07004590static int __init ipmi_init_msghandler_mod(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591{
4592 ipmi_init_msghandler();
4593 return 0;
4594}
4595
Corey Minyard60ee6d52010-10-27 15:34:18 -07004596static void __exit cleanup_ipmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597{
4598 int count;
4599
4600 if (!initialized)
4601 return;
4602
Alan Sterne041c682006-03-27 01:16:30 -08004603 atomic_notifier_chain_unregister(&panic_notifier_list, &panic_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
Corey Minyardc70d7492008-04-29 01:01:09 -07004605 /*
4606 * This can't be called if any interfaces exist, so no worry
4607 * about shutting down the interfaces.
4608 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609
Corey Minyardc70d7492008-04-29 01:01:09 -07004610 /*
4611 * Tell the timer to stop, then wait for it to stop. This
4612 * avoids problems with race conditions removing the timer
4613 * here.
4614 */
Corey Minyard8f43f842005-06-23 22:01:40 -07004615 atomic_inc(&stop_operation);
4616 del_timer_sync(&ipmi_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617
Corey Minyard3b625942005-06-23 22:01:42 -07004618#ifdef CONFIG_PROC_FS
David Howellsa8ca16e2013-04-12 17:27:28 +01004619 proc_remove(proc_ipmi_root);
Corey Minyard3b625942005-06-23 22:01:42 -07004620#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08004622 driver_unregister(&ipmidriver.driver);
Corey Minyard50c812b2006-03-26 01:37:21 -08004623
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 initialized = 0;
4625
4626 /* Check for buffer leaks. */
4627 count = atomic_read(&smi_msg_inuse_count);
4628 if (count != 0)
4629 printk(KERN_WARNING PFX "SMI message count %d at exit\n",
4630 count);
4631 count = atomic_read(&recv_msg_inuse_count);
4632 if (count != 0)
4633 printk(KERN_WARNING PFX "recv message count %d at exit\n",
4634 count);
4635}
4636module_exit(cleanup_ipmi);
4637
4638module_init(ipmi_init_msghandler_mod);
4639MODULE_LICENSE("GPL");
Corey Minyard1fdd75b2005-09-06 15:18:42 -07004640MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
Corey Minyardc70d7492008-04-29 01:01:09 -07004641MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI"
4642 " interface.");
Corey Minyard1fdd75b2005-09-06 15:18:42 -07004643MODULE_VERSION(IPMI_DRIVER_VERSION);