blob: 9f699951b75aa40a15ed3aec78a97b1daf7cab41 [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. */
Corey Minyard210af2a2017-01-05 10:52:10 -0600105 const struct ipmi_user_hndl *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 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 */
Corey Minyarda24b5dd2016-11-14 10:11:30 -0600161#define STORE_SEQ_IN_MSGID(seq, seqid) \
162 ((((seq) & 0x3f) << 26) | ((seqid) & 0x3ffffff))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164#define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
165 do { \
Corey Minyarda24b5dd2016-11-14 10:11:30 -0600166 seq = (((msgid) >> 26) & 0x3f); \
167 seqid = ((msgid) & 0x3ffffff); \
Corey Minyardc70d7492008-04-29 01:01:09 -0700168 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Corey Minyarda24b5dd2016-11-14 10:11:30 -0600170#define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3ffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Corey Minyardc70d7492008-04-29 01:01:09 -0700172struct ipmi_channel {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 unsigned char medium;
174 unsigned char protocol;
Corey Minyardc14979b2005-09-06 15:18:38 -0700175
Corey Minyardc70d7492008-04-29 01:01:09 -0700176 /*
177 * My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR,
178 * but may be changed by the user.
179 */
Corey Minyardc14979b2005-09-06 15:18:38 -0700180 unsigned char address;
181
Corey Minyardc70d7492008-04-29 01:01:09 -0700182 /*
183 * My LUN. This should generally stay the SMS LUN, but just in
184 * case...
185 */
Corey Minyardc14979b2005-09-06 15:18:38 -0700186 unsigned char lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187};
188
Corey Minyard3b625942005-06-23 22:01:42 -0700189#ifdef CONFIG_PROC_FS
Corey Minyardc70d7492008-04-29 01:01:09 -0700190struct ipmi_proc_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 char *name;
192 struct ipmi_proc_entry *next;
193};
Corey Minyard3b625942005-06-23 22:01:42 -0700194#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Corey Minyardc70d7492008-04-29 01:01:09 -0700196struct bmc_device {
Corey Minyard16639eb2014-10-10 21:54:03 -0500197 struct platform_device pdev;
Corey Minyard50c812b2006-03-26 01:37:21 -0800198 struct ipmi_device_id id;
199 unsigned char guid[16];
200 int guid_set;
Corey Minyard16639eb2014-10-10 21:54:03 -0500201 char name[16];
202 struct kref usecount;
Corey Minyard50c812b2006-03-26 01:37:21 -0800203};
Corey Minyard16639eb2014-10-10 21:54:03 -0500204#define to_bmc_device(x) container_of((x), struct bmc_device, pdev.dev)
Corey Minyard50c812b2006-03-26 01:37:21 -0800205
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700206/*
207 * Various statistics for IPMI, these index stats[] in the ipmi_smi
208 * structure.
209 */
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700210enum ipmi_stat_indexes {
211 /* Commands we got from the user that were invalid. */
212 IPMI_STAT_sent_invalid_commands = 0,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700213
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700214 /* Commands we sent to the MC. */
215 IPMI_STAT_sent_local_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700216
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700217 /* Responses from the MC that were delivered to a user. */
218 IPMI_STAT_handled_local_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700219
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700220 /* Responses from the MC that were not delivered to a user. */
221 IPMI_STAT_unhandled_local_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700222
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700223 /* Commands we sent out to the IPMB bus. */
224 IPMI_STAT_sent_ipmb_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700225
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700226 /* Commands sent on the IPMB that had errors on the SEND CMD */
227 IPMI_STAT_sent_ipmb_command_errs,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700228
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700229 /* Each retransmit increments this count. */
230 IPMI_STAT_retransmitted_ipmb_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700231
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700232 /*
233 * When a message times out (runs out of retransmits) this is
234 * incremented.
235 */
236 IPMI_STAT_timed_out_ipmb_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700237
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700238 /*
239 * This is like above, but for broadcasts. Broadcasts are
240 * *not* included in the above count (they are expected to
241 * time out).
242 */
243 IPMI_STAT_timed_out_ipmb_broadcasts,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700244
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700245 /* Responses I have sent to the IPMB bus. */
246 IPMI_STAT_sent_ipmb_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700247
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700248 /* The response was delivered to the user. */
249 IPMI_STAT_handled_ipmb_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700250
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700251 /* The response had invalid data in it. */
252 IPMI_STAT_invalid_ipmb_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700253
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700254 /* The response didn't have anyone waiting for it. */
255 IPMI_STAT_unhandled_ipmb_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700256
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700257 /* Commands we sent out to the IPMB bus. */
258 IPMI_STAT_sent_lan_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700259
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700260 /* Commands sent on the IPMB that had errors on the SEND CMD */
261 IPMI_STAT_sent_lan_command_errs,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700262
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700263 /* Each retransmit increments this count. */
264 IPMI_STAT_retransmitted_lan_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700265
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700266 /*
267 * When a message times out (runs out of retransmits) this is
268 * incremented.
269 */
270 IPMI_STAT_timed_out_lan_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700271
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700272 /* Responses I have sent to the IPMB bus. */
273 IPMI_STAT_sent_lan_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700274
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700275 /* The response was delivered to the user. */
276 IPMI_STAT_handled_lan_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700277
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700278 /* The response had invalid data in it. */
279 IPMI_STAT_invalid_lan_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700280
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700281 /* The response didn't have anyone waiting for it. */
282 IPMI_STAT_unhandled_lan_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700283
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700284 /* The command was delivered to the user. */
285 IPMI_STAT_handled_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700286
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700287 /* The command had invalid data in it. */
288 IPMI_STAT_invalid_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700289
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700290 /* The command didn't have anyone waiting for it. */
291 IPMI_STAT_unhandled_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700292
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700293 /* Invalid data in an event. */
294 IPMI_STAT_invalid_events,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700295
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700296 /* Events that were received with the proper format. */
297 IPMI_STAT_events,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700298
Corey Minyard25176ed2009-04-21 12:24:04 -0700299 /* Retransmissions on IPMB that failed. */
300 IPMI_STAT_dropped_rexmit_ipmb_commands,
301
302 /* Retransmissions on LAN that failed. */
303 IPMI_STAT_dropped_rexmit_lan_commands,
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700304
305 /* This *must* remain last, add new values above this. */
306 IPMI_NUM_STATS
307};
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700308
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310#define IPMI_IPMB_NUM_SEQ 64
Corey Minyardc14979b2005-09-06 15:18:38 -0700311#define IPMI_MAX_CHANNELS 16
Corey Minyardc70d7492008-04-29 01:01:09 -0700312struct ipmi_smi {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /* What interface number are we? */
314 int intf_num;
315
Corey Minyard393d2cc2005-11-07 00:59:54 -0800316 struct kref refcount;
317
Corey Minyard7ea0ed22014-11-06 16:58:48 -0600318 /* Set when the interface is being unregistered. */
319 bool in_shutdown;
320
Corey Minyardbca03242006-12-06 20:40:57 -0800321 /* Used for a list of interfaces. */
322 struct list_head link;
323
Corey Minyardc70d7492008-04-29 01:01:09 -0700324 /*
325 * The list of upper layers that are using me. seq_lock
326 * protects this.
327 */
Corey Minyard393d2cc2005-11-07 00:59:54 -0800328 struct list_head users;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Corey Minyardb2c03942006-12-06 20:41:00 -0800330 /* Information to supply to users. */
331 unsigned char ipmi_version_major;
332 unsigned char ipmi_version_minor;
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 /* Used for wake ups at startup. */
335 wait_queue_head_t waitq;
336
Corey Minyard50c812b2006-03-26 01:37:21 -0800337 struct bmc_device *bmc;
338 char *my_dev_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Corey Minyardc70d7492008-04-29 01:01:09 -0700340 /*
341 * This is the lower-layer's sender routine. Note that you
Corey Minyardb2c03942006-12-06 20:41:00 -0800342 * must either be holding the ipmi_interfaces_mutex or be in
343 * an umpreemptible region to use this. You must fetch the
Corey Minyardc70d7492008-04-29 01:01:09 -0700344 * value into a local variable and make sure it is not NULL.
345 */
Corey Minyard81d02b72015-06-13 10:34:25 -0500346 const struct ipmi_smi_handlers *handlers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 void *send_info;
348
Corey Minyard3b625942005-06-23 22:01:42 -0700349#ifdef CONFIG_PROC_FS
Corey Minyardac019152007-10-18 03:07:11 -0700350 /* A list of proc entries for this interface. */
351 struct mutex proc_entry_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 struct ipmi_proc_entry *proc_entries;
Corey Minyard3b625942005-06-23 22:01:42 -0700353#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Corey Minyard50c812b2006-03-26 01:37:21 -0800355 /* Driver-model device for the system interface. */
356 struct device *si_dev;
357
Corey Minyardc70d7492008-04-29 01:01:09 -0700358 /*
359 * A table of sequence numbers for this interface. We use the
360 * sequence numbers for IPMB messages that go out of the
361 * interface to match them up with their responses. A routine
362 * is called periodically to time the items in this list.
363 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 spinlock_t seq_lock;
365 struct seq_table seq_table[IPMI_IPMB_NUM_SEQ];
366 int curr_seq;
367
Corey Minyardc70d7492008-04-29 01:01:09 -0700368 /*
Corey Minyard7adf5792012-03-28 14:42:49 -0700369 * Messages queued for delivery. If delivery fails (out of memory
370 * for instance), They will stay in here to be processed later in a
371 * periodic timer interrupt. The tasklet is for handling received
372 * messages directly from the handler.
Corey Minyardc70d7492008-04-29 01:01:09 -0700373 */
Corey Minyard65be7542014-11-06 20:48:02 -0600374 spinlock_t waiting_rcv_msgs_lock;
375 struct list_head waiting_rcv_msgs;
Corey Minyard7adf5792012-03-28 14:42:49 -0700376 atomic_t watchdog_pretimeouts_to_deliver;
377 struct tasklet_struct recv_tasklet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Corey Minyard7ea0ed22014-11-06 16:58:48 -0600379 spinlock_t xmit_msgs_lock;
380 struct list_head xmit_msgs;
381 struct ipmi_smi_msg *curr_msg;
382 struct list_head hp_xmit_msgs;
383
Corey Minyardc70d7492008-04-29 01:01:09 -0700384 /*
385 * The list of command receivers that are registered for commands
386 * on this interface.
387 */
Corey Minyardd6dfd132006-03-31 02:30:41 -0800388 struct mutex cmd_rcvrs_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 struct list_head cmd_rcvrs;
390
Corey Minyardc70d7492008-04-29 01:01:09 -0700391 /*
392 * Events that were queues because no one was there to receive
393 * them.
394 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 spinlock_t events_lock; /* For dealing with event stuff. */
396 struct list_head waiting_events;
397 unsigned int waiting_events_count; /* How many events in queue? */
Corey Minyard87ebd062008-04-29 01:01:04 -0700398 char delivering_events;
399 char event_msg_printed;
Corey Minyard89986492014-04-14 09:46:54 -0500400 atomic_t event_waiters;
401 unsigned int ticks_to_req_ev;
402 int last_needs_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Corey Minyardc70d7492008-04-29 01:01:09 -0700404 /*
405 * The event receiver for my BMC, only really used at panic
406 * shutdown as a place to store this.
407 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 unsigned char event_receiver;
409 unsigned char event_receiver_lun;
410 unsigned char local_sel_device;
411 unsigned char local_event_generator;
412
Corey Minyardb9675132006-12-06 20:41:02 -0800413 /* For handling of maintenance mode. */
414 int maintenance_mode;
Corey Minyard7aefac22014-04-14 09:46:56 -0500415 bool maintenance_mode_enable;
Corey Minyardb9675132006-12-06 20:41:02 -0800416 int auto_maintenance_timeout;
417 spinlock_t maintenance_mode_lock; /* Used in a timer... */
418
Corey Minyardc70d7492008-04-29 01:01:09 -0700419 /*
420 * A cheap hack, if this is non-null and a message to an
421 * interface comes in with a NULL user, call this routine with
422 * it. Note that the message will still be freed by the
423 * caller. This only works on the system interface.
424 */
Corey Minyard56a55ec2005-09-06 15:18:42 -0700425 void (*null_user_handler)(ipmi_smi_t intf, struct ipmi_recv_msg *msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Corey Minyardc70d7492008-04-29 01:01:09 -0700427 /*
428 * When we are scanning the channels for an SMI, this will
429 * tell which channel we are scanning.
430 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 int curr_channel;
432
433 /* Channel information */
434 struct ipmi_channel channels[IPMI_MAX_CHANNELS];
435
436 /* Proc FS stuff. */
437 struct proc_dir_entry *proc_dir;
438 char proc_dir_name[10];
439
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700440 atomic_t stats[IPMI_NUM_STATS];
Konstantin Baydarov5956dce2008-04-29 01:01:03 -0700441
442 /*
443 * run_to_completion duplicate of smb_info, smi_info
444 * and ipmi_serial_info structures. Used to decrease numbers of
445 * parameters passed by "low" level IPMI code.
446 */
447 int run_to_completion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448};
Corey Minyard50c812b2006-03-26 01:37:21 -0800449#define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Corey Minyard50c812b2006-03-26 01:37:21 -0800451/**
452 * The driver model view of the IPMI messaging driver.
453 */
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -0800454static struct platform_driver ipmidriver = {
455 .driver = {
456 .name = "ipmi",
457 .bus = &platform_bus_type
458 }
Corey Minyard50c812b2006-03-26 01:37:21 -0800459};
460static DEFINE_MUTEX(ipmidriver_mutex);
461
Denis Chengbed97592008-02-06 01:37:35 -0800462static LIST_HEAD(ipmi_interfaces);
Corey Minyardbca03242006-12-06 20:40:57 -0800463static DEFINE_MUTEX(ipmi_interfaces_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Corey Minyardc70d7492008-04-29 01:01:09 -0700465/*
466 * List of watchers that want to know when smi's are added and deleted.
467 */
Denis Chengbed97592008-02-06 01:37:35 -0800468static LIST_HEAD(smi_watchers);
Corey Minyardb2c03942006-12-06 20:41:00 -0800469static DEFINE_MUTEX(smi_watchers_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700471#define ipmi_inc_stat(intf, stat) \
472 atomic_inc(&(intf)->stats[IPMI_STAT_ ## stat])
473#define ipmi_get_stat(intf, stat) \
474 ((unsigned int) atomic_read(&(intf)->stats[IPMI_STAT_ ## stat]))
475
LABBE Corentin99ee6732015-11-13 13:31:51 +0100476static const char * const addr_src_to_str[] = {
477 "invalid", "hotmod", "hardcoded", "SPMI", "ACPI", "SMBIOS", "PCI",
Tony Camusob07b58a2016-06-22 14:22:28 -0400478 "device-tree"
LABBE Corentin99ee6732015-11-13 13:31:51 +0100479};
Corey Minyard7e503872014-10-09 07:20:32 -0500480
481const char *ipmi_addr_src_to_str(enum ipmi_addr_src src)
482{
Tony Camusob07b58a2016-06-22 14:22:28 -0400483 if (src >= SI_LAST)
Corey Minyard7e503872014-10-09 07:20:32 -0500484 src = 0; /* Invalid */
485 return addr_src_to_str[src];
486}
487EXPORT_SYMBOL(ipmi_addr_src_to_str);
488
Corey Minyard25176ed2009-04-21 12:24:04 -0700489static int is_lan_addr(struct ipmi_addr *addr)
490{
491 return addr->addr_type == IPMI_LAN_ADDR_TYPE;
492}
493
494static int is_ipmb_addr(struct ipmi_addr *addr)
495{
496 return addr->addr_type == IPMI_IPMB_ADDR_TYPE;
497}
498
499static int is_ipmb_bcast_addr(struct ipmi_addr *addr)
500{
501 return addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE;
502}
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700503
Corey Minyard393d2cc2005-11-07 00:59:54 -0800504static void free_recv_msg_list(struct list_head *q)
505{
506 struct ipmi_recv_msg *msg, *msg2;
507
508 list_for_each_entry_safe(msg, msg2, q, link) {
509 list_del(&msg->link);
510 ipmi_free_recv_msg(msg);
511 }
512}
513
Corey Minyardf3ce6a02006-11-08 17:44:52 -0800514static void free_smi_msg_list(struct list_head *q)
515{
516 struct ipmi_smi_msg *msg, *msg2;
517
518 list_for_each_entry_safe(msg, msg2, q, link) {
519 list_del(&msg->link);
520 ipmi_free_smi_msg(msg);
521 }
522}
523
Corey Minyard393d2cc2005-11-07 00:59:54 -0800524static void clean_up_interface_data(ipmi_smi_t intf)
525{
526 int i;
527 struct cmd_rcvr *rcvr, *rcvr2;
Corey Minyard393d2cc2005-11-07 00:59:54 -0800528 struct list_head list;
529
Corey Minyard7adf5792012-03-28 14:42:49 -0700530 tasklet_kill(&intf->recv_tasklet);
531
Corey Minyard65be7542014-11-06 20:48:02 -0600532 free_smi_msg_list(&intf->waiting_rcv_msgs);
Corey Minyard393d2cc2005-11-07 00:59:54 -0800533 free_recv_msg_list(&intf->waiting_events);
534
Corey Minyard78ba2fa2007-02-10 01:45:45 -0800535 /*
536 * Wholesale remove all the entries from the list in the
537 * interface and wait for RCU to know that none are in use.
538 */
Corey Minyardd6dfd132006-03-31 02:30:41 -0800539 mutex_lock(&intf->cmd_rcvrs_mutex);
Corey Minyard78ba2fa2007-02-10 01:45:45 -0800540 INIT_LIST_HEAD(&list);
541 list_splice_init_rcu(&intf->cmd_rcvrs, &list, synchronize_rcu);
Corey Minyardd6dfd132006-03-31 02:30:41 -0800542 mutex_unlock(&intf->cmd_rcvrs_mutex);
Corey Minyard393d2cc2005-11-07 00:59:54 -0800543
544 list_for_each_entry_safe(rcvr, rcvr2, &list, link)
545 kfree(rcvr);
546
547 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
548 if ((intf->seq_table[i].inuse)
Corey Minyardc70d7492008-04-29 01:01:09 -0700549 && (intf->seq_table[i].recv_msg))
Corey Minyard393d2cc2005-11-07 00:59:54 -0800550 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
Corey Minyard393d2cc2005-11-07 00:59:54 -0800552}
553
554static void intf_free(struct kref *ref)
555{
556 ipmi_smi_t intf = container_of(ref, struct ipmi_smi, refcount);
557
558 clean_up_interface_data(intf);
559 kfree(intf);
560}
561
Corey Minyardbca03242006-12-06 20:40:57 -0800562struct watcher_entry {
Corey Minyardb2c03942006-12-06 20:41:00 -0800563 int intf_num;
564 ipmi_smi_t intf;
Corey Minyardbca03242006-12-06 20:40:57 -0800565 struct list_head link;
Corey Minyardbca03242006-12-06 20:40:57 -0800566};
567
Corey Minyard393d2cc2005-11-07 00:59:54 -0800568int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher)
569{
Corey Minyardbca03242006-12-06 20:40:57 -0800570 ipmi_smi_t intf;
Denis Chenge381d1c42008-02-06 01:37:39 -0800571 LIST_HEAD(to_deliver);
Corey Minyardbca03242006-12-06 20:40:57 -0800572 struct watcher_entry *e, *e2;
573
Corey Minyardb2c03942006-12-06 20:41:00 -0800574 mutex_lock(&smi_watchers_mutex);
575
Corey Minyardbca03242006-12-06 20:40:57 -0800576 mutex_lock(&ipmi_interfaces_mutex);
577
Corey Minyardb2c03942006-12-06 20:41:00 -0800578 /* Build a list of things to deliver. */
Corey Minyard78ba2fa2007-02-10 01:45:45 -0800579 list_for_each_entry(intf, &ipmi_interfaces, link) {
Corey Minyardbca03242006-12-06 20:40:57 -0800580 if (intf->intf_num == -1)
581 continue;
582 e = kmalloc(sizeof(*e), GFP_KERNEL);
583 if (!e)
584 goto out_err;
Corey Minyardb2c03942006-12-06 20:41:00 -0800585 kref_get(&intf->refcount);
586 e->intf = intf;
Corey Minyardbca03242006-12-06 20:40:57 -0800587 e->intf_num = intf->intf_num;
588 list_add_tail(&e->link, &to_deliver);
589 }
Corey Minyard393d2cc2005-11-07 00:59:54 -0800590
Corey Minyardb2c03942006-12-06 20:41:00 -0800591 /* We will succeed, so add it to the list. */
592 list_add(&watcher->link, &smi_watchers);
Corey Minyardbca03242006-12-06 20:40:57 -0800593
594 mutex_unlock(&ipmi_interfaces_mutex);
595
596 list_for_each_entry_safe(e, e2, &to_deliver, link) {
597 list_del(&e->link);
Corey Minyardb2c03942006-12-06 20:41:00 -0800598 watcher->new_smi(e->intf_num, e->intf->si_dev);
599 kref_put(&e->intf->refcount, intf_free);
Corey Minyardbca03242006-12-06 20:40:57 -0800600 kfree(e);
Corey Minyard393d2cc2005-11-07 00:59:54 -0800601 }
Corey Minyardbca03242006-12-06 20:40:57 -0800602
Corey Minyardb2c03942006-12-06 20:41:00 -0800603 mutex_unlock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -0800604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return 0;
Corey Minyardbca03242006-12-06 20:40:57 -0800606
607 out_err:
Corey Minyardb2c03942006-12-06 20:41:00 -0800608 mutex_unlock(&ipmi_interfaces_mutex);
609 mutex_unlock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -0800610 list_for_each_entry_safe(e, e2, &to_deliver, link) {
611 list_del(&e->link);
Corey Minyardb2c03942006-12-06 20:41:00 -0800612 kref_put(&e->intf->refcount, intf_free);
Corey Minyardbca03242006-12-06 20:40:57 -0800613 kfree(e);
614 }
615 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
Corey Minyardc70d7492008-04-29 01:01:09 -0700617EXPORT_SYMBOL(ipmi_smi_watcher_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher)
620{
Corey Minyardb2c03942006-12-06 20:41:00 -0800621 mutex_lock(&smi_watchers_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 list_del(&(watcher->link));
Corey Minyardb2c03942006-12-06 20:41:00 -0800623 mutex_unlock(&smi_watchers_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return 0;
625}
Corey Minyardc70d7492008-04-29 01:01:09 -0700626EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Corey Minyardb2c03942006-12-06 20:41:00 -0800628/*
629 * Must be called with smi_watchers_mutex held.
630 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631static void
Corey Minyard50c812b2006-03-26 01:37:21 -0800632call_smi_watchers(int i, struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 struct ipmi_smi_watcher *w;
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 list_for_each_entry(w, &smi_watchers, link) {
637 if (try_module_get(w->owner)) {
Corey Minyard50c812b2006-03-26 01:37:21 -0800638 w->new_smi(i, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 module_put(w->owner);
640 }
641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
644static int
645ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2)
646{
647 if (addr1->addr_type != addr2->addr_type)
648 return 0;
649
650 if (addr1->channel != addr2->channel)
651 return 0;
652
653 if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
654 struct ipmi_system_interface_addr *smi_addr1
655 = (struct ipmi_system_interface_addr *) addr1;
656 struct ipmi_system_interface_addr *smi_addr2
657 = (struct ipmi_system_interface_addr *) addr2;
658 return (smi_addr1->lun == smi_addr2->lun);
659 }
660
Corey Minyard25176ed2009-04-21 12:24:04 -0700661 if (is_ipmb_addr(addr1) || is_ipmb_bcast_addr(addr1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 struct ipmi_ipmb_addr *ipmb_addr1
663 = (struct ipmi_ipmb_addr *) addr1;
664 struct ipmi_ipmb_addr *ipmb_addr2
665 = (struct ipmi_ipmb_addr *) addr2;
666
667 return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr)
668 && (ipmb_addr1->lun == ipmb_addr2->lun));
669 }
670
Corey Minyard25176ed2009-04-21 12:24:04 -0700671 if (is_lan_addr(addr1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 struct ipmi_lan_addr *lan_addr1
673 = (struct ipmi_lan_addr *) addr1;
674 struct ipmi_lan_addr *lan_addr2
675 = (struct ipmi_lan_addr *) addr2;
676
677 return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID)
678 && (lan_addr1->local_SWID == lan_addr2->local_SWID)
679 && (lan_addr1->session_handle
680 == lan_addr2->session_handle)
681 && (lan_addr1->lun == lan_addr2->lun));
682 }
683
684 return 1;
685}
686
687int ipmi_validate_addr(struct ipmi_addr *addr, int len)
688{
Corey Minyardc70d7492008-04-29 01:01:09 -0700689 if (len < sizeof(struct ipmi_system_interface_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
693 if (addr->channel != IPMI_BMC_CHANNEL)
694 return -EINVAL;
695 return 0;
696 }
697
698 if ((addr->channel == IPMI_BMC_CHANNEL)
Jayachandran C12fc1d72006-02-03 03:04:51 -0800699 || (addr->channel >= IPMI_MAX_CHANNELS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 || (addr->channel < 0))
701 return -EINVAL;
702
Corey Minyard25176ed2009-04-21 12:24:04 -0700703 if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
Corey Minyardc70d7492008-04-29 01:01:09 -0700704 if (len < sizeof(struct ipmi_ipmb_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return 0;
707 }
708
Corey Minyard25176ed2009-04-21 12:24:04 -0700709 if (is_lan_addr(addr)) {
Corey Minyardc70d7492008-04-29 01:01:09 -0700710 if (len < sizeof(struct ipmi_lan_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return 0;
713 }
714
715 return -EINVAL;
716}
Corey Minyardc70d7492008-04-29 01:01:09 -0700717EXPORT_SYMBOL(ipmi_validate_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719unsigned int ipmi_addr_length(int addr_type)
720{
721 if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
722 return sizeof(struct ipmi_system_interface_addr);
723
724 if ((addr_type == IPMI_IPMB_ADDR_TYPE)
Corey Minyardc70d7492008-04-29 01:01:09 -0700725 || (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return sizeof(struct ipmi_ipmb_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 if (addr_type == IPMI_LAN_ADDR_TYPE)
729 return sizeof(struct ipmi_lan_addr);
730
731 return 0;
732}
Corey Minyardc70d7492008-04-29 01:01:09 -0700733EXPORT_SYMBOL(ipmi_addr_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735static void deliver_response(struct ipmi_recv_msg *msg)
736{
Corey Minyard8a3628d2006-03-31 02:30:40 -0800737 if (!msg->user) {
Corey Minyard56a55ec2005-09-06 15:18:42 -0700738 ipmi_smi_t intf = msg->user_msg_data;
Corey Minyard56a55ec2005-09-06 15:18:42 -0700739
740 /* Special handling for NULL users. */
741 if (intf->null_user_handler) {
742 intf->null_user_handler(intf, msg);
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700743 ipmi_inc_stat(intf, handled_local_responses);
Corey Minyard56a55ec2005-09-06 15:18:42 -0700744 } else {
745 /* No handler, so give up. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700746 ipmi_inc_stat(intf, unhandled_local_responses);
Corey Minyard56a55ec2005-09-06 15:18:42 -0700747 }
748 ipmi_free_recv_msg(msg);
Hidehiro Kawaic49c0972015-07-27 14:55:16 +0900749 } else if (!oops_in_progress) {
750 /*
751 * If we are running in the panic context, calling the
752 * receive handler doesn't much meaning and has a deadlock
753 * risk. At this moment, simply skip it in that case.
754 */
755
Corey Minyard393d2cc2005-11-07 00:59:54 -0800756 ipmi_user_t user = msg->user;
757 user->handler->ipmi_recv_hndl(msg, user->handler_data);
Corey Minyard56a55ec2005-09-06 15:18:42 -0700758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
Corey Minyardb2c03942006-12-06 20:41:00 -0800761static void
762deliver_err_response(struct ipmi_recv_msg *msg, int err)
763{
764 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
765 msg->msg_data[0] = err;
766 msg->msg.netfn |= 1; /* Convert to a response. */
767 msg->msg.data_len = 1;
768 msg->msg.data = msg->msg_data;
769 deliver_response(msg);
770}
771
Corey Minyardc70d7492008-04-29 01:01:09 -0700772/*
773 * Find the next sequence number not being used and add the given
774 * message with the given timeout to the sequence table. This must be
775 * called with the interface's seq_lock held.
776 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777static int intf_next_seq(ipmi_smi_t intf,
778 struct ipmi_recv_msg *recv_msg,
779 unsigned long timeout,
780 int retries,
781 int broadcast,
782 unsigned char *seq,
783 long *seqid)
784{
785 int rv = 0;
786 unsigned int i;
787
Corey Minyardc70d7492008-04-29 01:01:09 -0700788 for (i = intf->curr_seq; (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq;
789 i = (i+1)%IPMI_IPMB_NUM_SEQ) {
Corey Minyard8a3628d2006-03-31 02:30:40 -0800790 if (!intf->seq_table[i].inuse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 break;
792 }
793
Corey Minyard8a3628d2006-03-31 02:30:40 -0800794 if (!intf->seq_table[i].inuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 intf->seq_table[i].recv_msg = recv_msg;
796
Corey Minyardc70d7492008-04-29 01:01:09 -0700797 /*
798 * Start with the maximum timeout, when the send response
799 * comes in we will start the real timer.
800 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 intf->seq_table[i].timeout = MAX_MSG_TIMEOUT;
802 intf->seq_table[i].orig_timeout = timeout;
803 intf->seq_table[i].retries_left = retries;
804 intf->seq_table[i].broadcast = broadcast;
805 intf->seq_table[i].inuse = 1;
806 intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid);
807 *seq = i;
808 *seqid = intf->seq_table[i].seqid;
809 intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ;
Corey Minyard89986492014-04-14 09:46:54 -0500810 need_waiter(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 } else {
812 rv = -EAGAIN;
813 }
Corey Minyardc70d7492008-04-29 01:01:09 -0700814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return rv;
816}
817
Corey Minyardc70d7492008-04-29 01:01:09 -0700818/*
819 * Return the receive message for the given sequence number and
820 * release the sequence number so it can be reused. Some other data
821 * is passed in to be sure the message matches up correctly (to help
822 * guard against message coming in after their timeout and the
823 * sequence number being reused).
824 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825static int intf_find_seq(ipmi_smi_t intf,
826 unsigned char seq,
827 short channel,
828 unsigned char cmd,
829 unsigned char netfn,
830 struct ipmi_addr *addr,
831 struct ipmi_recv_msg **recv_msg)
832{
833 int rv = -ENODEV;
834 unsigned long flags;
835
836 if (seq >= IPMI_IPMB_NUM_SEQ)
837 return -EINVAL;
838
839 spin_lock_irqsave(&(intf->seq_lock), flags);
840 if (intf->seq_table[seq].inuse) {
841 struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg;
842
Corey Minyardc70d7492008-04-29 01:01:09 -0700843 if ((msg->addr.channel == channel) && (msg->msg.cmd == cmd)
844 && (msg->msg.netfn == netfn)
845 && (ipmi_addr_equal(addr, &(msg->addr)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 *recv_msg = msg;
847 intf->seq_table[seq].inuse = 0;
848 rv = 0;
849 }
850 }
851 spin_unlock_irqrestore(&(intf->seq_lock), flags);
852
853 return rv;
854}
855
856
857/* Start the timer for a specific sequence table entry. */
858static int intf_start_seq_timer(ipmi_smi_t intf,
859 long msgid)
860{
861 int rv = -ENODEV;
862 unsigned long flags;
863 unsigned char seq;
864 unsigned long seqid;
865
866
867 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
868
869 spin_lock_irqsave(&(intf->seq_lock), flags);
Corey Minyardc70d7492008-04-29 01:01:09 -0700870 /*
871 * We do this verification because the user can be deleted
872 * while a message is outstanding.
873 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if ((intf->seq_table[seq].inuse)
Corey Minyardc70d7492008-04-29 01:01:09 -0700875 && (intf->seq_table[seq].seqid == seqid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 struct seq_table *ent = &(intf->seq_table[seq]);
877 ent->timeout = ent->orig_timeout;
878 rv = 0;
879 }
880 spin_unlock_irqrestore(&(intf->seq_lock), flags);
881
882 return rv;
883}
884
885/* Got an error for the send message for a specific sequence number. */
886static int intf_err_seq(ipmi_smi_t intf,
887 long msgid,
888 unsigned int err)
889{
890 int rv = -ENODEV;
891 unsigned long flags;
892 unsigned char seq;
893 unsigned long seqid;
894 struct ipmi_recv_msg *msg = NULL;
895
896
897 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
898
899 spin_lock_irqsave(&(intf->seq_lock), flags);
Corey Minyardc70d7492008-04-29 01:01:09 -0700900 /*
901 * We do this verification because the user can be deleted
902 * while a message is outstanding.
903 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if ((intf->seq_table[seq].inuse)
Corey Minyardc70d7492008-04-29 01:01:09 -0700905 && (intf->seq_table[seq].seqid == seqid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 struct seq_table *ent = &(intf->seq_table[seq]);
907
908 ent->inuse = 0;
909 msg = ent->recv_msg;
910 rv = 0;
911 }
912 spin_unlock_irqrestore(&(intf->seq_lock), flags);
913
Corey Minyardb2c03942006-12-06 20:41:00 -0800914 if (msg)
915 deliver_err_response(msg, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 return rv;
918}
919
920
921int ipmi_create_user(unsigned int if_num,
Corey Minyard210af2a2017-01-05 10:52:10 -0600922 const struct ipmi_user_hndl *handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 void *handler_data,
924 ipmi_user_t *user)
925{
926 unsigned long flags;
927 ipmi_user_t new_user;
928 int rv = 0;
929 ipmi_smi_t intf;
930
Corey Minyardc70d7492008-04-29 01:01:09 -0700931 /*
932 * There is no module usecount here, because it's not
933 * required. Since this can only be used by and called from
934 * other modules, they will implicitly use this module, and
935 * thus this can't be removed unless the other modules are
936 * removed.
937 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 if (handler == NULL)
940 return -EINVAL;
941
Corey Minyardc70d7492008-04-29 01:01:09 -0700942 /*
943 * Make sure the driver is actually initialized, this handles
944 * problems with initialization order.
945 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (!initialized) {
947 rv = ipmi_init_msghandler();
948 if (rv)
949 return rv;
950
Corey Minyardc70d7492008-04-29 01:01:09 -0700951 /*
952 * The init code doesn't return an error if it was turned
953 * off, but it won't initialize. Check that.
954 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (!initialized)
956 return -ENODEV;
957 }
958
959 new_user = kmalloc(sizeof(*new_user), GFP_KERNEL);
Corey Minyard8a3628d2006-03-31 02:30:40 -0800960 if (!new_user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return -ENOMEM;
962
Corey Minyardb2c03942006-12-06 20:41:00 -0800963 mutex_lock(&ipmi_interfaces_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -0800964 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
965 if (intf->intf_num == if_num)
966 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
Corey Minyardb2c03942006-12-06 20:41:00 -0800968 /* Not found, return an error */
Corey Minyardbca03242006-12-06 20:40:57 -0800969 rv = -EINVAL;
970 goto out_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Corey Minyardbca03242006-12-06 20:40:57 -0800972 found:
Corey Minyard393d2cc2005-11-07 00:59:54 -0800973 /* Note that each existing user holds a refcount to the interface. */
974 kref_get(&intf->refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Corey Minyard393d2cc2005-11-07 00:59:54 -0800976 kref_init(&new_user->refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 new_user->handler = handler;
978 new_user->handler_data = handler_data;
979 new_user->intf = intf;
Corey Minyard89986492014-04-14 09:46:54 -0500980 new_user->gets_events = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 if (!try_module_get(intf->handlers->owner)) {
983 rv = -ENODEV;
Adrian Bunk5c98d292006-03-25 03:07:52 -0800984 goto out_kref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986
987 if (intf->handlers->inc_usecount) {
988 rv = intf->handlers->inc_usecount(intf->send_info);
989 if (rv) {
990 module_put(intf->handlers->owner);
Adrian Bunk5c98d292006-03-25 03:07:52 -0800991 goto out_kref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
993 }
994
Corey Minyardc70d7492008-04-29 01:01:09 -0700995 /*
996 * Hold the lock so intf->handlers is guaranteed to be good
997 * until now
998 */
Corey Minyardb2c03942006-12-06 20:41:00 -0800999 mutex_unlock(&ipmi_interfaces_mutex);
1000
Corey Minyard7aefac22014-04-14 09:46:56 -05001001 new_user->valid = true;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001002 spin_lock_irqsave(&intf->seq_lock, flags);
1003 list_add_rcu(&new_user->link, &intf->users);
1004 spin_unlock_irqrestore(&intf->seq_lock, flags);
Corey Minyard89986492014-04-14 09:46:54 -05001005 if (handler->ipmi_watchdog_pretimeout) {
1006 /* User wants pretimeouts, so make sure to watch for them. */
1007 if (atomic_inc_return(&intf->event_waiters) == 1)
1008 need_waiter(intf);
1009 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08001010 *user = new_user;
1011 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Adrian Bunk5c98d292006-03-25 03:07:52 -08001013out_kref:
Corey Minyard393d2cc2005-11-07 00:59:54 -08001014 kref_put(&intf->refcount, intf_free);
Adrian Bunk5c98d292006-03-25 03:07:52 -08001015out_kfree:
Corey Minyardb2c03942006-12-06 20:41:00 -08001016 mutex_unlock(&ipmi_interfaces_mutex);
Adrian Bunk5c98d292006-03-25 03:07:52 -08001017 kfree(new_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return rv;
1019}
Corey Minyardc70d7492008-04-29 01:01:09 -07001020EXPORT_SYMBOL(ipmi_create_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Zhao Yakui16f42322010-12-08 10:10:16 +08001022int ipmi_get_smi_info(int if_num, struct ipmi_smi_info *data)
1023{
1024 int rv = 0;
1025 ipmi_smi_t intf;
Corey Minyard81d02b72015-06-13 10:34:25 -05001026 const struct ipmi_smi_handlers *handlers;
Zhao Yakui16f42322010-12-08 10:10:16 +08001027
1028 mutex_lock(&ipmi_interfaces_mutex);
1029 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
1030 if (intf->intf_num == if_num)
1031 goto found;
1032 }
1033 /* Not found, return an error */
1034 rv = -EINVAL;
1035 mutex_unlock(&ipmi_interfaces_mutex);
1036 return rv;
1037
1038found:
1039 handlers = intf->handlers;
1040 rv = -ENOSYS;
1041 if (handlers->get_smi_info)
1042 rv = handlers->get_smi_info(intf->send_info, data);
1043 mutex_unlock(&ipmi_interfaces_mutex);
1044
1045 return rv;
1046}
1047EXPORT_SYMBOL(ipmi_get_smi_info);
1048
Corey Minyard393d2cc2005-11-07 00:59:54 -08001049static void free_user(struct kref *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
Corey Minyard393d2cc2005-11-07 00:59:54 -08001051 ipmi_user_t user = container_of(ref, struct ipmi_user, refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 kfree(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053}
1054
1055int ipmi_destroy_user(ipmi_user_t user)
1056{
Corey Minyard393d2cc2005-11-07 00:59:54 -08001057 ipmi_smi_t intf = user->intf;
1058 int i;
1059 unsigned long flags;
1060 struct cmd_rcvr *rcvr;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001061 struct cmd_rcvr *rcvrs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Corey Minyard7aefac22014-04-14 09:46:56 -05001063 user->valid = false;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001064
Corey Minyard89986492014-04-14 09:46:54 -05001065 if (user->handler->ipmi_watchdog_pretimeout)
1066 atomic_dec(&intf->event_waiters);
1067
1068 if (user->gets_events)
1069 atomic_dec(&intf->event_waiters);
1070
Corey Minyard393d2cc2005-11-07 00:59:54 -08001071 /* Remove the user from the interface's sequence table. */
1072 spin_lock_irqsave(&intf->seq_lock, flags);
1073 list_del_rcu(&user->link);
1074
1075 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
1076 if (intf->seq_table[i].inuse
Corey Minyardc70d7492008-04-29 01:01:09 -07001077 && (intf->seq_table[i].recv_msg->user == user)) {
Corey Minyard393d2cc2005-11-07 00:59:54 -08001078 intf->seq_table[i].inuse = 0;
Corey Minyardb2c03942006-12-06 20:41:00 -08001079 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
Corey Minyard393d2cc2005-11-07 00:59:54 -08001080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08001082 spin_unlock_irqrestore(&intf->seq_lock, flags);
1083
1084 /*
1085 * Remove the user from the command receiver's table. First
1086 * we build a list of everything (not using the standard link,
1087 * since other things may be using it till we do
1088 * synchronize_rcu()) then free everything in that list.
1089 */
Corey Minyardd6dfd132006-03-31 02:30:41 -08001090 mutex_lock(&intf->cmd_rcvrs_mutex);
Paul E. McKenney066bb8d2006-01-06 00:19:53 -08001091 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
Corey Minyard393d2cc2005-11-07 00:59:54 -08001092 if (rcvr->user == user) {
1093 list_del_rcu(&rcvr->link);
1094 rcvr->next = rcvrs;
1095 rcvrs = rcvr;
1096 }
1097 }
Corey Minyardd6dfd132006-03-31 02:30:41 -08001098 mutex_unlock(&intf->cmd_rcvrs_mutex);
Corey Minyard393d2cc2005-11-07 00:59:54 -08001099 synchronize_rcu();
1100 while (rcvrs) {
1101 rcvr = rcvrs;
1102 rcvrs = rcvr->next;
1103 kfree(rcvr);
1104 }
1105
Corey Minyardb2c03942006-12-06 20:41:00 -08001106 mutex_lock(&ipmi_interfaces_mutex);
1107 if (intf->handlers) {
1108 module_put(intf->handlers->owner);
1109 if (intf->handlers->dec_usecount)
1110 intf->handlers->dec_usecount(intf->send_info);
1111 }
1112 mutex_unlock(&ipmi_interfaces_mutex);
Corey Minyard393d2cc2005-11-07 00:59:54 -08001113
1114 kref_put(&intf->refcount, intf_free);
1115
1116 kref_put(&user->refcount, free_user);
1117
Corey Minyard8a3628d2006-03-31 02:30:40 -08001118 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
Corey Minyardc70d7492008-04-29 01:01:09 -07001120EXPORT_SYMBOL(ipmi_destroy_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122void ipmi_get_version(ipmi_user_t user,
1123 unsigned char *major,
1124 unsigned char *minor)
1125{
Corey Minyardb2c03942006-12-06 20:41:00 -08001126 *major = user->intf->ipmi_version_major;
1127 *minor = user->intf->ipmi_version_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
Corey Minyardc70d7492008-04-29 01:01:09 -07001129EXPORT_SYMBOL(ipmi_get_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Corey Minyardc14979b2005-09-06 15:18:38 -07001131int ipmi_set_my_address(ipmi_user_t user,
1132 unsigned int channel,
1133 unsigned char address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Corey Minyardc14979b2005-09-06 15:18:38 -07001135 if (channel >= IPMI_MAX_CHANNELS)
1136 return -EINVAL;
1137 user->intf->channels[channel].address = address;
1138 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
Corey Minyardc70d7492008-04-29 01:01:09 -07001140EXPORT_SYMBOL(ipmi_set_my_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Corey Minyardc14979b2005-09-06 15:18:38 -07001142int ipmi_get_my_address(ipmi_user_t user,
1143 unsigned int channel,
1144 unsigned char *address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Corey Minyardc14979b2005-09-06 15:18:38 -07001146 if (channel >= IPMI_MAX_CHANNELS)
1147 return -EINVAL;
1148 *address = user->intf->channels[channel].address;
1149 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
Corey Minyardc70d7492008-04-29 01:01:09 -07001151EXPORT_SYMBOL(ipmi_get_my_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Corey Minyardc14979b2005-09-06 15:18:38 -07001153int ipmi_set_my_LUN(ipmi_user_t user,
1154 unsigned int channel,
1155 unsigned char LUN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
Corey Minyardc14979b2005-09-06 15:18:38 -07001157 if (channel >= IPMI_MAX_CHANNELS)
1158 return -EINVAL;
1159 user->intf->channels[channel].lun = LUN & 0x3;
1160 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
Corey Minyardc70d7492008-04-29 01:01:09 -07001162EXPORT_SYMBOL(ipmi_set_my_LUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Corey Minyardc14979b2005-09-06 15:18:38 -07001164int ipmi_get_my_LUN(ipmi_user_t user,
1165 unsigned int channel,
1166 unsigned char *address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Corey Minyardc14979b2005-09-06 15:18:38 -07001168 if (channel >= IPMI_MAX_CHANNELS)
1169 return -EINVAL;
1170 *address = user->intf->channels[channel].lun;
1171 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
Corey Minyardc70d7492008-04-29 01:01:09 -07001173EXPORT_SYMBOL(ipmi_get_my_LUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Corey Minyardb9675132006-12-06 20:41:02 -08001175int ipmi_get_maintenance_mode(ipmi_user_t user)
1176{
1177 int mode;
1178 unsigned long flags;
1179
1180 spin_lock_irqsave(&user->intf->maintenance_mode_lock, flags);
1181 mode = user->intf->maintenance_mode;
1182 spin_unlock_irqrestore(&user->intf->maintenance_mode_lock, flags);
1183
1184 return mode;
1185}
1186EXPORT_SYMBOL(ipmi_get_maintenance_mode);
1187
1188static void maintenance_mode_update(ipmi_smi_t intf)
1189{
1190 if (intf->handlers->set_maintenance_mode)
1191 intf->handlers->set_maintenance_mode(
1192 intf->send_info, intf->maintenance_mode_enable);
1193}
1194
1195int ipmi_set_maintenance_mode(ipmi_user_t user, int mode)
1196{
1197 int rv = 0;
1198 unsigned long flags;
1199 ipmi_smi_t intf = user->intf;
1200
1201 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1202 if (intf->maintenance_mode != mode) {
1203 switch (mode) {
1204 case IPMI_MAINTENANCE_MODE_AUTO:
Corey Minyardb9675132006-12-06 20:41:02 -08001205 intf->maintenance_mode_enable
1206 = (intf->auto_maintenance_timeout > 0);
1207 break;
1208
1209 case IPMI_MAINTENANCE_MODE_OFF:
Corey Minyard7aefac22014-04-14 09:46:56 -05001210 intf->maintenance_mode_enable = false;
Corey Minyardb9675132006-12-06 20:41:02 -08001211 break;
1212
1213 case IPMI_MAINTENANCE_MODE_ON:
Corey Minyard7aefac22014-04-14 09:46:56 -05001214 intf->maintenance_mode_enable = true;
Corey Minyardb9675132006-12-06 20:41:02 -08001215 break;
1216
1217 default:
1218 rv = -EINVAL;
1219 goto out_unlock;
1220 }
Corey Minyard7aefac22014-04-14 09:46:56 -05001221 intf->maintenance_mode = mode;
Corey Minyardb9675132006-12-06 20:41:02 -08001222
1223 maintenance_mode_update(intf);
1224 }
1225 out_unlock:
1226 spin_unlock_irqrestore(&intf->maintenance_mode_lock, flags);
1227
1228 return rv;
1229}
1230EXPORT_SYMBOL(ipmi_set_maintenance_mode);
1231
Corey Minyard89986492014-04-14 09:46:54 -05001232int ipmi_set_gets_events(ipmi_user_t user, bool val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Corey Minyard393d2cc2005-11-07 00:59:54 -08001234 unsigned long flags;
1235 ipmi_smi_t intf = user->intf;
1236 struct ipmi_recv_msg *msg, *msg2;
1237 struct list_head msgs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Corey Minyard393d2cc2005-11-07 00:59:54 -08001239 INIT_LIST_HEAD(&msgs);
1240
1241 spin_lock_irqsave(&intf->events_lock, flags);
Corey Minyard89986492014-04-14 09:46:54 -05001242 if (user->gets_events == val)
1243 goto out;
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 user->gets_events = val;
1246
Corey Minyard89986492014-04-14 09:46:54 -05001247 if (val) {
1248 if (atomic_inc_return(&intf->event_waiters) == 1)
1249 need_waiter(intf);
1250 } else {
1251 atomic_dec(&intf->event_waiters);
1252 }
1253
Corey Minyardb2c03942006-12-06 20:41:00 -08001254 if (intf->delivering_events)
1255 /*
1256 * Another thread is delivering events for this, so
1257 * let it handle any new events.
1258 */
1259 goto out;
1260
1261 /* Deliver any queued events. */
1262 while (user->gets_events && !list_empty(&intf->waiting_events)) {
Akinobu Mita179e0912006-06-26 00:24:41 -07001263 list_for_each_entry_safe(msg, msg2, &intf->waiting_events, link)
1264 list_move_tail(&msg->link, &msgs);
Corey Minyard4791c032006-04-10 22:54:31 -07001265 intf->waiting_events_count = 0;
Corey Minyard87ebd062008-04-29 01:01:04 -07001266 if (intf->event_msg_printed) {
1267 printk(KERN_WARNING PFX "Event queue no longer"
1268 " full\n");
1269 intf->event_msg_printed = 0;
1270 }
Corey Minyardb2c03942006-12-06 20:41:00 -08001271
1272 intf->delivering_events = 1;
1273 spin_unlock_irqrestore(&intf->events_lock, flags);
1274
1275 list_for_each_entry_safe(msg, msg2, &msgs, link) {
1276 msg->user = user;
1277 kref_get(&user->refcount);
1278 deliver_response(msg);
1279 }
1280
1281 spin_lock_irqsave(&intf->events_lock, flags);
1282 intf->delivering_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08001284
Corey Minyardb2c03942006-12-06 20:41:00 -08001285 out:
Corey Minyard393d2cc2005-11-07 00:59:54 -08001286 spin_unlock_irqrestore(&intf->events_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 return 0;
1289}
Corey Minyardc70d7492008-04-29 01:01:09 -07001290EXPORT_SYMBOL(ipmi_set_gets_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Corey Minyard393d2cc2005-11-07 00:59:54 -08001292static struct cmd_rcvr *find_cmd_rcvr(ipmi_smi_t intf,
1293 unsigned char netfn,
Corey Minyardc69c3122006-09-30 23:27:56 -07001294 unsigned char cmd,
1295 unsigned char chan)
Corey Minyard393d2cc2005-11-07 00:59:54 -08001296{
1297 struct cmd_rcvr *rcvr;
1298
1299 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
Corey Minyardc69c3122006-09-30 23:27:56 -07001300 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1301 && (rcvr->chans & (1 << chan)))
Corey Minyard393d2cc2005-11-07 00:59:54 -08001302 return rcvr;
1303 }
1304 return NULL;
1305}
1306
Corey Minyardc69c3122006-09-30 23:27:56 -07001307static int is_cmd_rcvr_exclusive(ipmi_smi_t intf,
1308 unsigned char netfn,
1309 unsigned char cmd,
1310 unsigned int chans)
1311{
1312 struct cmd_rcvr *rcvr;
1313
1314 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1315 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1316 && (rcvr->chans & chans))
1317 return 0;
1318 }
1319 return 1;
1320}
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322int ipmi_register_for_cmd(ipmi_user_t user,
1323 unsigned char netfn,
Corey Minyardc69c3122006-09-30 23:27:56 -07001324 unsigned char cmd,
1325 unsigned int chans)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Corey Minyard393d2cc2005-11-07 00:59:54 -08001327 ipmi_smi_t intf = user->intf;
1328 struct cmd_rcvr *rcvr;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001329 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331
1332 rcvr = kmalloc(sizeof(*rcvr), GFP_KERNEL);
Corey Minyard8a3628d2006-03-31 02:30:40 -08001333 if (!rcvr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return -ENOMEM;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001335 rcvr->cmd = cmd;
1336 rcvr->netfn = netfn;
Corey Minyardc69c3122006-09-30 23:27:56 -07001337 rcvr->chans = chans;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001338 rcvr->user = user;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Corey Minyardd6dfd132006-03-31 02:30:41 -08001340 mutex_lock(&intf->cmd_rcvrs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 /* Make sure the command/netfn is not already registered. */
Corey Minyardc69c3122006-09-30 23:27:56 -07001342 if (!is_cmd_rcvr_exclusive(intf, netfn, cmd, chans)) {
Corey Minyard393d2cc2005-11-07 00:59:54 -08001343 rv = -EBUSY;
1344 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346
Corey Minyard89986492014-04-14 09:46:54 -05001347 if (atomic_inc_return(&intf->event_waiters) == 1)
1348 need_waiter(intf);
1349
Corey Minyard393d2cc2005-11-07 00:59:54 -08001350 list_add_rcu(&rcvr->link, &intf->cmd_rcvrs);
Corey Minyard877197e2005-09-06 15:18:45 -07001351
Corey Minyard393d2cc2005-11-07 00:59:54 -08001352 out_unlock:
Corey Minyardd6dfd132006-03-31 02:30:41 -08001353 mutex_unlock(&intf->cmd_rcvrs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (rv)
1355 kfree(rcvr);
1356
1357 return rv;
1358}
Corey Minyardc70d7492008-04-29 01:01:09 -07001359EXPORT_SYMBOL(ipmi_register_for_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361int ipmi_unregister_for_cmd(ipmi_user_t user,
1362 unsigned char netfn,
Corey Minyardc69c3122006-09-30 23:27:56 -07001363 unsigned char cmd,
1364 unsigned int chans)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Corey Minyard393d2cc2005-11-07 00:59:54 -08001366 ipmi_smi_t intf = user->intf;
1367 struct cmd_rcvr *rcvr;
Corey Minyardc69c3122006-09-30 23:27:56 -07001368 struct cmd_rcvr *rcvrs = NULL;
1369 int i, rv = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Corey Minyardd6dfd132006-03-31 02:30:41 -08001371 mutex_lock(&intf->cmd_rcvrs_mutex);
Corey Minyardc69c3122006-09-30 23:27:56 -07001372 for (i = 0; i < IPMI_NUM_CHANNELS; i++) {
1373 if (((1 << i) & chans) == 0)
1374 continue;
1375 rcvr = find_cmd_rcvr(intf, netfn, cmd, i);
1376 if (rcvr == NULL)
1377 continue;
1378 if (rcvr->user == user) {
1379 rv = 0;
1380 rcvr->chans &= ~chans;
1381 if (rcvr->chans == 0) {
1382 list_del_rcu(&rcvr->link);
1383 rcvr->next = rcvrs;
1384 rcvrs = rcvr;
1385 }
1386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
Corey Minyardc69c3122006-09-30 23:27:56 -07001388 mutex_unlock(&intf->cmd_rcvrs_mutex);
1389 synchronize_rcu();
1390 while (rcvrs) {
Corey Minyard89986492014-04-14 09:46:54 -05001391 atomic_dec(&intf->event_waiters);
Corey Minyardc69c3122006-09-30 23:27:56 -07001392 rcvr = rcvrs;
1393 rcvrs = rcvr->next;
1394 kfree(rcvr);
1395 }
1396 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397}
Corey Minyardc70d7492008-04-29 01:01:09 -07001398EXPORT_SYMBOL(ipmi_unregister_for_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400static unsigned char
1401ipmb_checksum(unsigned char *data, int size)
1402{
1403 unsigned char csum = 0;
Corey Minyardc70d7492008-04-29 01:01:09 -07001404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 for (; size > 0; size--, data++)
1406 csum += *data;
1407
1408 return -csum;
1409}
1410
1411static inline void format_ipmb_msg(struct ipmi_smi_msg *smi_msg,
1412 struct kernel_ipmi_msg *msg,
1413 struct ipmi_ipmb_addr *ipmb_addr,
1414 long msgid,
1415 unsigned char ipmb_seq,
1416 int broadcast,
1417 unsigned char source_address,
1418 unsigned char source_lun)
1419{
1420 int i = broadcast;
1421
1422 /* Format the IPMB header data. */
1423 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1424 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1425 smi_msg->data[2] = ipmb_addr->channel;
1426 if (broadcast)
1427 smi_msg->data[3] = 0;
1428 smi_msg->data[i+3] = ipmb_addr->slave_addr;
1429 smi_msg->data[i+4] = (msg->netfn << 2) | (ipmb_addr->lun & 0x3);
1430 smi_msg->data[i+5] = ipmb_checksum(&(smi_msg->data[i+3]), 2);
1431 smi_msg->data[i+6] = source_address;
1432 smi_msg->data[i+7] = (ipmb_seq << 2) | source_lun;
1433 smi_msg->data[i+8] = msg->cmd;
1434
1435 /* Now tack on the data to the message. */
1436 if (msg->data_len > 0)
1437 memcpy(&(smi_msg->data[i+9]), msg->data,
1438 msg->data_len);
1439 smi_msg->data_size = msg->data_len + 9;
1440
1441 /* Now calculate the checksum and tack it on. */
1442 smi_msg->data[i+smi_msg->data_size]
1443 = ipmb_checksum(&(smi_msg->data[i+6]),
1444 smi_msg->data_size-6);
1445
Corey Minyardc70d7492008-04-29 01:01:09 -07001446 /*
1447 * Add on the checksum size and the offset from the
1448 * broadcast.
1449 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 smi_msg->data_size += 1 + i;
1451
1452 smi_msg->msgid = msgid;
1453}
1454
1455static inline void format_lan_msg(struct ipmi_smi_msg *smi_msg,
1456 struct kernel_ipmi_msg *msg,
1457 struct ipmi_lan_addr *lan_addr,
1458 long msgid,
1459 unsigned char ipmb_seq,
1460 unsigned char source_lun)
1461{
1462 /* Format the IPMB header data. */
1463 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1464 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1465 smi_msg->data[2] = lan_addr->channel;
1466 smi_msg->data[3] = lan_addr->session_handle;
1467 smi_msg->data[4] = lan_addr->remote_SWID;
1468 smi_msg->data[5] = (msg->netfn << 2) | (lan_addr->lun & 0x3);
1469 smi_msg->data[6] = ipmb_checksum(&(smi_msg->data[4]), 2);
1470 smi_msg->data[7] = lan_addr->local_SWID;
1471 smi_msg->data[8] = (ipmb_seq << 2) | source_lun;
1472 smi_msg->data[9] = msg->cmd;
1473
1474 /* Now tack on the data to the message. */
1475 if (msg->data_len > 0)
1476 memcpy(&(smi_msg->data[10]), msg->data,
1477 msg->data_len);
1478 smi_msg->data_size = msg->data_len + 10;
1479
1480 /* Now calculate the checksum and tack it on. */
1481 smi_msg->data[smi_msg->data_size]
1482 = ipmb_checksum(&(smi_msg->data[7]),
1483 smi_msg->data_size-7);
1484
Corey Minyardc70d7492008-04-29 01:01:09 -07001485 /*
1486 * Add on the checksum size and the offset from the
1487 * broadcast.
1488 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 smi_msg->data_size += 1;
1490
1491 smi_msg->msgid = msgid;
1492}
1493
Arnd Bergmann191cc412015-01-28 16:00:11 +01001494static struct ipmi_smi_msg *smi_add_send_msg(ipmi_smi_t intf,
1495 struct ipmi_smi_msg *smi_msg,
1496 int priority)
Corey Minyard7f4a1c82014-11-06 20:52:24 -06001497{
Corey Minyard7ea0ed22014-11-06 16:58:48 -06001498 if (intf->curr_msg) {
1499 if (priority > 0)
1500 list_add_tail(&smi_msg->link, &intf->hp_xmit_msgs);
1501 else
1502 list_add_tail(&smi_msg->link, &intf->xmit_msgs);
1503 smi_msg = NULL;
1504 } else {
1505 intf->curr_msg = smi_msg;
1506 }
Arnd Bergmann191cc412015-01-28 16:00:11 +01001507
1508 return smi_msg;
1509}
1510
1511
Corey Minyard81d02b72015-06-13 10:34:25 -05001512static void smi_send(ipmi_smi_t intf, const struct ipmi_smi_handlers *handlers,
Arnd Bergmann191cc412015-01-28 16:00:11 +01001513 struct ipmi_smi_msg *smi_msg, int priority)
1514{
1515 int run_to_completion = intf->run_to_completion;
1516
1517 if (run_to_completion) {
1518 smi_msg = smi_add_send_msg(intf, smi_msg, priority);
1519 } else {
1520 unsigned long flags;
1521
1522 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
1523 smi_msg = smi_add_send_msg(intf, smi_msg, priority);
Corey Minyard7ea0ed22014-11-06 16:58:48 -06001524 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
Arnd Bergmann191cc412015-01-28 16:00:11 +01001525 }
Corey Minyard7ea0ed22014-11-06 16:58:48 -06001526
1527 if (smi_msg)
Corey Minyard99ab32f2014-11-07 07:57:31 -06001528 handlers->sender(intf->send_info, smi_msg);
Corey Minyard7f4a1c82014-11-06 20:52:24 -06001529}
1530
Corey Minyardc70d7492008-04-29 01:01:09 -07001531/*
1532 * Separate from ipmi_request so that the user does not have to be
1533 * supplied in certain circumstances (mainly at panic time). If
1534 * messages are supplied, they will be freed, even if an error
1535 * occurs.
1536 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08001537static int i_ipmi_request(ipmi_user_t user,
1538 ipmi_smi_t intf,
1539 struct ipmi_addr *addr,
1540 long msgid,
1541 struct kernel_ipmi_msg *msg,
1542 void *user_msg_data,
1543 void *supplied_smi,
1544 struct ipmi_recv_msg *supplied_recv,
1545 int priority,
1546 unsigned char source_address,
1547 unsigned char source_lun,
1548 int retries,
1549 unsigned int retry_time_ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
Corey Minyardb2c03942006-12-06 20:41:00 -08001551 int rv = 0;
1552 struct ipmi_smi_msg *smi_msg;
1553 struct ipmi_recv_msg *recv_msg;
1554 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
1556
Corey Minyardc70d7492008-04-29 01:01:09 -07001557 if (supplied_recv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 recv_msg = supplied_recv;
Corey Minyardc70d7492008-04-29 01:01:09 -07001559 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 recv_msg = ipmi_alloc_recv_msg();
Corey Minyardc70d7492008-04-29 01:01:09 -07001561 if (recv_msg == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
1564 recv_msg->user_msg_data = user_msg_data;
1565
Corey Minyardc70d7492008-04-29 01:01:09 -07001566 if (supplied_smi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 smi_msg = (struct ipmi_smi_msg *) supplied_smi;
Corey Minyardc70d7492008-04-29 01:01:09 -07001568 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 smi_msg = ipmi_alloc_smi_msg();
1570 if (smi_msg == NULL) {
1571 ipmi_free_recv_msg(recv_msg);
1572 return -ENOMEM;
1573 }
1574 }
1575
Corey Minyardb2c03942006-12-06 20:41:00 -08001576 rcu_read_lock();
Corey Minyard7ea0ed22014-11-06 16:58:48 -06001577 if (intf->in_shutdown) {
Corey Minyardb2c03942006-12-06 20:41:00 -08001578 rv = -ENODEV;
1579 goto out_err;
1580 }
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 recv_msg->user = user;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001583 if (user)
1584 kref_get(&user->refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 recv_msg->msgid = msgid;
Corey Minyardc70d7492008-04-29 01:01:09 -07001586 /*
1587 * Store the message to send in the receive message so timeout
1588 * responses can get the proper response data.
1589 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 recv_msg->msg = *msg;
1591
1592 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
1593 struct ipmi_system_interface_addr *smi_addr;
1594
1595 if (msg->netfn & 1) {
1596 /* Responses are not allowed to the SMI. */
1597 rv = -EINVAL;
1598 goto out_err;
1599 }
1600
1601 smi_addr = (struct ipmi_system_interface_addr *) addr;
1602 if (smi_addr->lun > 3) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001603 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 rv = -EINVAL;
1605 goto out_err;
1606 }
1607
1608 memcpy(&recv_msg->addr, smi_addr, sizeof(*smi_addr));
1609
1610 if ((msg->netfn == IPMI_NETFN_APP_REQUEST)
1611 && ((msg->cmd == IPMI_SEND_MSG_CMD)
1612 || (msg->cmd == IPMI_GET_MSG_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07001613 || (msg->cmd == IPMI_READ_EVENT_MSG_BUFFER_CMD))) {
1614 /*
1615 * We don't let the user do these, since we manage
1616 * the sequence numbers.
1617 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001618 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 rv = -EINVAL;
1620 goto out_err;
1621 }
1622
Corey Minyardb9675132006-12-06 20:41:02 -08001623 if (((msg->netfn == IPMI_NETFN_APP_REQUEST)
1624 && ((msg->cmd == IPMI_COLD_RESET_CMD)
1625 || (msg->cmd == IPMI_WARM_RESET_CMD)))
Corey Minyardc70d7492008-04-29 01:01:09 -07001626 || (msg->netfn == IPMI_NETFN_FIRMWARE_REQUEST)) {
Corey Minyardb9675132006-12-06 20:41:02 -08001627 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1628 intf->auto_maintenance_timeout
1629 = IPMI_MAINTENANCE_MODE_TIMEOUT;
1630 if (!intf->maintenance_mode
Corey Minyardc70d7492008-04-29 01:01:09 -07001631 && !intf->maintenance_mode_enable) {
Corey Minyard7aefac22014-04-14 09:46:56 -05001632 intf->maintenance_mode_enable = true;
Corey Minyardb9675132006-12-06 20:41:02 -08001633 maintenance_mode_update(intf);
1634 }
1635 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
1636 flags);
1637 }
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if ((msg->data_len + 2) > IPMI_MAX_MSG_LENGTH) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001640 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 rv = -EMSGSIZE;
1642 goto out_err;
1643 }
1644
1645 smi_msg->data[0] = (msg->netfn << 2) | (smi_addr->lun & 0x3);
1646 smi_msg->data[1] = msg->cmd;
1647 smi_msg->msgid = msgid;
1648 smi_msg->user_data = recv_msg;
1649 if (msg->data_len > 0)
1650 memcpy(&(smi_msg->data[2]), msg->data, msg->data_len);
1651 smi_msg->data_size = msg->data_len + 2;
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001652 ipmi_inc_stat(intf, sent_local_commands);
Corey Minyard25176ed2009-04-21 12:24:04 -07001653 } else if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 struct ipmi_ipmb_addr *ipmb_addr;
1655 unsigned char ipmb_seq;
1656 long seqid;
1657 int broadcast = 0;
1658
KAMBAROV, ZAUR9c101fd2005-06-28 20:45:08 -07001659 if (addr->channel >= IPMI_MAX_CHANNELS) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001660 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 rv = -EINVAL;
1662 goto out_err;
1663 }
1664
1665 if (intf->channels[addr->channel].medium
Corey Minyardc70d7492008-04-29 01:01:09 -07001666 != IPMI_CHANNEL_MEDIUM_IPMB) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001667 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 rv = -EINVAL;
1669 goto out_err;
1670 }
1671
1672 if (retries < 0) {
1673 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE)
1674 retries = 0; /* Don't retry broadcasts. */
1675 else
1676 retries = 4;
1677 }
1678 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001679 /*
1680 * Broadcasts add a zero at the beginning of the
1681 * message, but otherwise is the same as an IPMB
1682 * address.
1683 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 addr->addr_type = IPMI_IPMB_ADDR_TYPE;
1685 broadcast = 1;
1686 }
1687
1688
1689 /* Default to 1 second retries. */
1690 if (retry_time_ms == 0)
1691 retry_time_ms = 1000;
1692
Corey Minyardc70d7492008-04-29 01:01:09 -07001693 /*
1694 * 9 for the header and 1 for the checksum, plus
1695 * possibly one for the broadcast.
1696 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 if ((msg->data_len + 10 + broadcast) > IPMI_MAX_MSG_LENGTH) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001698 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 rv = -EMSGSIZE;
1700 goto out_err;
1701 }
1702
1703 ipmb_addr = (struct ipmi_ipmb_addr *) addr;
1704 if (ipmb_addr->lun > 3) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001705 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 rv = -EINVAL;
1707 goto out_err;
1708 }
1709
1710 memcpy(&recv_msg->addr, ipmb_addr, sizeof(*ipmb_addr));
1711
1712 if (recv_msg->msg.netfn & 0x1) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001713 /*
1714 * It's a response, so use the user's sequence
1715 * from msgid.
1716 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001717 ipmi_inc_stat(intf, sent_ipmb_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 format_ipmb_msg(smi_msg, msg, ipmb_addr, msgid,
1719 msgid, broadcast,
1720 source_address, source_lun);
1721
Corey Minyardc70d7492008-04-29 01:01:09 -07001722 /*
1723 * Save the receive message so we can use it
1724 * to deliver the response.
1725 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 smi_msg->user_data = recv_msg;
1727 } else {
1728 /* It's a command, so get a sequence for it. */
1729
1730 spin_lock_irqsave(&(intf->seq_lock), flags);
1731
Corey Minyardc70d7492008-04-29 01:01:09 -07001732 /*
1733 * Create a sequence number with a 1 second
1734 * timeout and 4 retries.
1735 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 rv = intf_next_seq(intf,
1737 recv_msg,
1738 retry_time_ms,
1739 retries,
1740 broadcast,
1741 &ipmb_seq,
1742 &seqid);
1743 if (rv) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001744 /*
1745 * We have used up all the sequence numbers,
1746 * probably, so abort.
1747 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 spin_unlock_irqrestore(&(intf->seq_lock),
1749 flags);
1750 goto out_err;
1751 }
1752
Corey Minyard25176ed2009-04-21 12:24:04 -07001753 ipmi_inc_stat(intf, sent_ipmb_commands);
1754
Corey Minyardc70d7492008-04-29 01:01:09 -07001755 /*
1756 * Store the sequence number in the message,
1757 * so that when the send message response
1758 * comes back we can start the timer.
1759 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 format_ipmb_msg(smi_msg, msg, ipmb_addr,
1761 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1762 ipmb_seq, broadcast,
1763 source_address, source_lun);
1764
Corey Minyardc70d7492008-04-29 01:01:09 -07001765 /*
1766 * Copy the message into the recv message data, so we
1767 * can retransmit it later if necessary.
1768 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 memcpy(recv_msg->msg_data, smi_msg->data,
1770 smi_msg->data_size);
1771 recv_msg->msg.data = recv_msg->msg_data;
1772 recv_msg->msg.data_len = smi_msg->data_size;
1773
Corey Minyardc70d7492008-04-29 01:01:09 -07001774 /*
1775 * We don't unlock until here, because we need
1776 * to copy the completed message into the
1777 * recv_msg before we release the lock.
1778 * Otherwise, race conditions may bite us. I
1779 * know that's pretty paranoid, but I prefer
1780 * to be correct.
1781 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1783 }
Corey Minyard25176ed2009-04-21 12:24:04 -07001784 } else if (is_lan_addr(addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 struct ipmi_lan_addr *lan_addr;
1786 unsigned char ipmb_seq;
1787 long seqid;
1788
Jayachandran C12fc1d72006-02-03 03:04:51 -08001789 if (addr->channel >= IPMI_MAX_CHANNELS) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001790 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 rv = -EINVAL;
1792 goto out_err;
1793 }
1794
1795 if ((intf->channels[addr->channel].medium
Corey Minyardc70d7492008-04-29 01:01:09 -07001796 != IPMI_CHANNEL_MEDIUM_8023LAN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 && (intf->channels[addr->channel].medium
Corey Minyardc70d7492008-04-29 01:01:09 -07001798 != IPMI_CHANNEL_MEDIUM_ASYNC)) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001799 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 rv = -EINVAL;
1801 goto out_err;
1802 }
1803
1804 retries = 4;
1805
1806 /* Default to 1 second retries. */
1807 if (retry_time_ms == 0)
1808 retry_time_ms = 1000;
1809
1810 /* 11 for the header and 1 for the checksum. */
1811 if ((msg->data_len + 12) > IPMI_MAX_MSG_LENGTH) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001812 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 rv = -EMSGSIZE;
1814 goto out_err;
1815 }
1816
1817 lan_addr = (struct ipmi_lan_addr *) addr;
1818 if (lan_addr->lun > 3) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001819 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 rv = -EINVAL;
1821 goto out_err;
1822 }
1823
1824 memcpy(&recv_msg->addr, lan_addr, sizeof(*lan_addr));
1825
1826 if (recv_msg->msg.netfn & 0x1) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001827 /*
1828 * It's a response, so use the user's sequence
1829 * from msgid.
1830 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001831 ipmi_inc_stat(intf, sent_lan_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 format_lan_msg(smi_msg, msg, lan_addr, msgid,
1833 msgid, source_lun);
1834
Corey Minyardc70d7492008-04-29 01:01:09 -07001835 /*
1836 * Save the receive message so we can use it
1837 * to deliver the response.
1838 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 smi_msg->user_data = recv_msg;
1840 } else {
1841 /* It's a command, so get a sequence for it. */
1842
1843 spin_lock_irqsave(&(intf->seq_lock), flags);
1844
Corey Minyardc70d7492008-04-29 01:01:09 -07001845 /*
1846 * Create a sequence number with a 1 second
1847 * timeout and 4 retries.
1848 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 rv = intf_next_seq(intf,
1850 recv_msg,
1851 retry_time_ms,
1852 retries,
1853 0,
1854 &ipmb_seq,
1855 &seqid);
1856 if (rv) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001857 /*
1858 * We have used up all the sequence numbers,
1859 * probably, so abort.
1860 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 spin_unlock_irqrestore(&(intf->seq_lock),
1862 flags);
1863 goto out_err;
1864 }
1865
Corey Minyard25176ed2009-04-21 12:24:04 -07001866 ipmi_inc_stat(intf, sent_lan_commands);
1867
Corey Minyardc70d7492008-04-29 01:01:09 -07001868 /*
1869 * Store the sequence number in the message,
1870 * so that when the send message response
1871 * comes back we can start the timer.
1872 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 format_lan_msg(smi_msg, msg, lan_addr,
1874 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1875 ipmb_seq, source_lun);
1876
Corey Minyardc70d7492008-04-29 01:01:09 -07001877 /*
1878 * Copy the message into the recv message data, so we
1879 * can retransmit it later if necessary.
1880 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 memcpy(recv_msg->msg_data, smi_msg->data,
1882 smi_msg->data_size);
1883 recv_msg->msg.data = recv_msg->msg_data;
1884 recv_msg->msg.data_len = smi_msg->data_size;
1885
Corey Minyardc70d7492008-04-29 01:01:09 -07001886 /*
1887 * We don't unlock until here, because we need
1888 * to copy the completed message into the
1889 * recv_msg before we release the lock.
1890 * Otherwise, race conditions may bite us. I
1891 * know that's pretty paranoid, but I prefer
1892 * to be correct.
1893 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1895 }
1896 } else {
1897 /* Unknown address type. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001898 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 rv = -EINVAL;
1900 goto out_err;
1901 }
1902
1903#ifdef DEBUG_MSGING
1904 {
1905 int m;
Corey Minyarde8b33612005-09-06 15:18:45 -07001906 for (m = 0; m < smi_msg->data_size; m++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 printk(" %2.2x", smi_msg->data[m]);
1908 printk("\n");
1909 }
1910#endif
Corey Minyardb2c03942006-12-06 20:41:00 -08001911
Corey Minyard7ea0ed22014-11-06 16:58:48 -06001912 smi_send(intf, intf->handlers, smi_msg, priority);
Corey Minyardb2c03942006-12-06 20:41:00 -08001913 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915 return 0;
1916
1917 out_err:
Corey Minyardb2c03942006-12-06 20:41:00 -08001918 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 ipmi_free_smi_msg(smi_msg);
1920 ipmi_free_recv_msg(recv_msg);
1921 return rv;
1922}
1923
Corey Minyardc14979b2005-09-06 15:18:38 -07001924static int check_addr(ipmi_smi_t intf,
1925 struct ipmi_addr *addr,
1926 unsigned char *saddr,
1927 unsigned char *lun)
1928{
1929 if (addr->channel >= IPMI_MAX_CHANNELS)
1930 return -EINVAL;
1931 *lun = intf->channels[addr->channel].lun;
1932 *saddr = intf->channels[addr->channel].address;
1933 return 0;
1934}
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936int ipmi_request_settime(ipmi_user_t user,
1937 struct ipmi_addr *addr,
1938 long msgid,
1939 struct kernel_ipmi_msg *msg,
1940 void *user_msg_data,
1941 int priority,
1942 int retries,
1943 unsigned int retry_time_ms)
1944{
Corey Minyardf0ba9392013-09-05 06:36:34 -05001945 unsigned char saddr = 0, lun = 0;
Corey Minyardc14979b2005-09-06 15:18:38 -07001946 int rv;
1947
Corey Minyard8a3628d2006-03-31 02:30:40 -08001948 if (!user)
Corey Minyard56a55ec2005-09-06 15:18:42 -07001949 return -EINVAL;
Corey Minyardc14979b2005-09-06 15:18:38 -07001950 rv = check_addr(user->intf, addr, &saddr, &lun);
1951 if (rv)
1952 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 return i_ipmi_request(user,
1954 user->intf,
1955 addr,
1956 msgid,
1957 msg,
1958 user_msg_data,
1959 NULL, NULL,
1960 priority,
Corey Minyardc14979b2005-09-06 15:18:38 -07001961 saddr,
1962 lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 retries,
1964 retry_time_ms);
1965}
Corey Minyardc70d7492008-04-29 01:01:09 -07001966EXPORT_SYMBOL(ipmi_request_settime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968int ipmi_request_supply_msgs(ipmi_user_t user,
1969 struct ipmi_addr *addr,
1970 long msgid,
1971 struct kernel_ipmi_msg *msg,
1972 void *user_msg_data,
1973 void *supplied_smi,
1974 struct ipmi_recv_msg *supplied_recv,
1975 int priority)
1976{
Corey Minyard9ebca932012-10-16 15:53:39 -05001977 unsigned char saddr = 0, lun = 0;
Corey Minyardc14979b2005-09-06 15:18:38 -07001978 int rv;
1979
Corey Minyard8a3628d2006-03-31 02:30:40 -08001980 if (!user)
Corey Minyard56a55ec2005-09-06 15:18:42 -07001981 return -EINVAL;
Corey Minyardc14979b2005-09-06 15:18:38 -07001982 rv = check_addr(user->intf, addr, &saddr, &lun);
1983 if (rv)
1984 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 return i_ipmi_request(user,
1986 user->intf,
1987 addr,
1988 msgid,
1989 msg,
1990 user_msg_data,
1991 supplied_smi,
1992 supplied_recv,
1993 priority,
Corey Minyardc14979b2005-09-06 15:18:38 -07001994 saddr,
1995 lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 -1, 0);
1997}
Corey Minyardc70d7492008-04-29 01:01:09 -07001998EXPORT_SYMBOL(ipmi_request_supply_msgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Randy Dunlap1aa16ee2006-12-06 20:41:20 -08002000#ifdef CONFIG_PROC_FS
Alexey Dobriyan07412732011-05-26 16:25:55 -07002001static int smi_ipmb_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
Alexey Dobriyan07412732011-05-26 16:25:55 -07002003 ipmi_smi_t intf = m->private;
Corey Minyardc14979b2005-09-06 15:18:38 -07002004 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Alexey Dobriyan07412732011-05-26 16:25:55 -07002006 seq_printf(m, "%x", intf->channels[0].address);
2007 for (i = 1; i < IPMI_MAX_CHANNELS; i++)
2008 seq_printf(m, " %x", intf->channels[i].address);
Joe Perchesd6c5dc12015-02-17 11:10:56 -08002009 seq_putc(m, '\n');
2010
Joe Perches5e33cd02015-02-22 10:21:07 -08002011 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
2013
Alexey Dobriyan07412732011-05-26 16:25:55 -07002014static int smi_ipmb_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
Al Virod9dda782013-03-31 18:16:14 -04002016 return single_open(file, smi_ipmb_proc_show, PDE_DATA(inode));
Alexey Dobriyan07412732011-05-26 16:25:55 -07002017}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Alexey Dobriyan07412732011-05-26 16:25:55 -07002019static const struct file_operations smi_ipmb_proc_ops = {
2020 .open = smi_ipmb_proc_open,
2021 .read = seq_read,
2022 .llseek = seq_lseek,
2023 .release = single_release,
2024};
2025
2026static int smi_version_proc_show(struct seq_file *m, void *v)
2027{
2028 ipmi_smi_t intf = m->private;
2029
Joe Perchesd6c5dc12015-02-17 11:10:56 -08002030 seq_printf(m, "%u.%u\n",
2031 ipmi_version_major(&intf->bmc->id),
2032 ipmi_version_minor(&intf->bmc->id));
2033
Joe Perches5e33cd02015-02-22 10:21:07 -08002034 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035}
2036
Alexey Dobriyan07412732011-05-26 16:25:55 -07002037static int smi_version_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
Al Virod9dda782013-03-31 18:16:14 -04002039 return single_open(file, smi_version_proc_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040}
Alexey Dobriyan07412732011-05-26 16:25:55 -07002041
2042static const struct file_operations smi_version_proc_ops = {
2043 .open = smi_version_proc_open,
2044 .read = seq_read,
2045 .llseek = seq_lseek,
2046 .release = single_release,
2047};
2048
2049static int smi_stats_proc_show(struct seq_file *m, void *v)
2050{
2051 ipmi_smi_t intf = m->private;
2052
2053 seq_printf(m, "sent_invalid_commands: %u\n",
2054 ipmi_get_stat(intf, sent_invalid_commands));
2055 seq_printf(m, "sent_local_commands: %u\n",
2056 ipmi_get_stat(intf, sent_local_commands));
2057 seq_printf(m, "handled_local_responses: %u\n",
2058 ipmi_get_stat(intf, handled_local_responses));
2059 seq_printf(m, "unhandled_local_responses: %u\n",
2060 ipmi_get_stat(intf, unhandled_local_responses));
2061 seq_printf(m, "sent_ipmb_commands: %u\n",
2062 ipmi_get_stat(intf, sent_ipmb_commands));
2063 seq_printf(m, "sent_ipmb_command_errs: %u\n",
2064 ipmi_get_stat(intf, sent_ipmb_command_errs));
2065 seq_printf(m, "retransmitted_ipmb_commands: %u\n",
2066 ipmi_get_stat(intf, retransmitted_ipmb_commands));
2067 seq_printf(m, "timed_out_ipmb_commands: %u\n",
2068 ipmi_get_stat(intf, timed_out_ipmb_commands));
2069 seq_printf(m, "timed_out_ipmb_broadcasts: %u\n",
2070 ipmi_get_stat(intf, timed_out_ipmb_broadcasts));
2071 seq_printf(m, "sent_ipmb_responses: %u\n",
2072 ipmi_get_stat(intf, sent_ipmb_responses));
2073 seq_printf(m, "handled_ipmb_responses: %u\n",
2074 ipmi_get_stat(intf, handled_ipmb_responses));
2075 seq_printf(m, "invalid_ipmb_responses: %u\n",
2076 ipmi_get_stat(intf, invalid_ipmb_responses));
2077 seq_printf(m, "unhandled_ipmb_responses: %u\n",
2078 ipmi_get_stat(intf, unhandled_ipmb_responses));
2079 seq_printf(m, "sent_lan_commands: %u\n",
2080 ipmi_get_stat(intf, sent_lan_commands));
2081 seq_printf(m, "sent_lan_command_errs: %u\n",
2082 ipmi_get_stat(intf, sent_lan_command_errs));
2083 seq_printf(m, "retransmitted_lan_commands: %u\n",
2084 ipmi_get_stat(intf, retransmitted_lan_commands));
2085 seq_printf(m, "timed_out_lan_commands: %u\n",
2086 ipmi_get_stat(intf, timed_out_lan_commands));
2087 seq_printf(m, "sent_lan_responses: %u\n",
2088 ipmi_get_stat(intf, sent_lan_responses));
2089 seq_printf(m, "handled_lan_responses: %u\n",
2090 ipmi_get_stat(intf, handled_lan_responses));
2091 seq_printf(m, "invalid_lan_responses: %u\n",
2092 ipmi_get_stat(intf, invalid_lan_responses));
2093 seq_printf(m, "unhandled_lan_responses: %u\n",
2094 ipmi_get_stat(intf, unhandled_lan_responses));
2095 seq_printf(m, "handled_commands: %u\n",
2096 ipmi_get_stat(intf, handled_commands));
2097 seq_printf(m, "invalid_commands: %u\n",
2098 ipmi_get_stat(intf, invalid_commands));
2099 seq_printf(m, "unhandled_commands: %u\n",
2100 ipmi_get_stat(intf, unhandled_commands));
2101 seq_printf(m, "invalid_events: %u\n",
2102 ipmi_get_stat(intf, invalid_events));
2103 seq_printf(m, "events: %u\n",
2104 ipmi_get_stat(intf, events));
2105 seq_printf(m, "failed rexmit LAN msgs: %u\n",
2106 ipmi_get_stat(intf, dropped_rexmit_lan_commands));
2107 seq_printf(m, "failed rexmit IPMB msgs: %u\n",
2108 ipmi_get_stat(intf, dropped_rexmit_ipmb_commands));
2109 return 0;
2110}
2111
2112static int smi_stats_proc_open(struct inode *inode, struct file *file)
2113{
Al Virod9dda782013-03-31 18:16:14 -04002114 return single_open(file, smi_stats_proc_show, PDE_DATA(inode));
Alexey Dobriyan07412732011-05-26 16:25:55 -07002115}
2116
2117static const struct file_operations smi_stats_proc_ops = {
2118 .open = smi_stats_proc_open,
2119 .read = seq_read,
2120 .llseek = seq_lseek,
2121 .release = single_release,
2122};
Randy Dunlap1aa16ee2006-12-06 20:41:20 -08002123#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125int ipmi_smi_add_proc_entry(ipmi_smi_t smi, char *name,
Alexey Dobriyan07412732011-05-26 16:25:55 -07002126 const struct file_operations *proc_ops,
Alexey Dobriyan99b76232009-03-25 22:48:06 +03002127 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 int rv = 0;
Corey Minyard3b625942005-06-23 22:01:42 -07002130#ifdef CONFIG_PROC_FS
2131 struct proc_dir_entry *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 struct ipmi_proc_entry *entry;
2133
2134 /* Create a list element. */
2135 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2136 if (!entry)
2137 return -ENOMEM;
Alexandru Gheorghiu1b6b6982013-05-16 14:04:24 -05002138 entry->name = kstrdup(name, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 if (!entry->name) {
2140 kfree(entry);
2141 return -ENOMEM;
2142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Alexey Dobriyan07412732011-05-26 16:25:55 -07002144 file = proc_create_data(name, 0, smi->proc_dir, proc_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 if (!file) {
2146 kfree(entry->name);
2147 kfree(entry);
2148 rv = -ENOMEM;
2149 } else {
Corey Minyardac019152007-10-18 03:07:11 -07002150 mutex_lock(&smi->proc_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 /* Stick it on the list. */
2152 entry->next = smi->proc_entries;
2153 smi->proc_entries = entry;
Corey Minyardac019152007-10-18 03:07:11 -07002154 mutex_unlock(&smi->proc_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 }
Corey Minyard3b625942005-06-23 22:01:42 -07002156#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
2158 return rv;
2159}
Corey Minyardc70d7492008-04-29 01:01:09 -07002160EXPORT_SYMBOL(ipmi_smi_add_proc_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162static int add_proc_entries(ipmi_smi_t smi, int num)
2163{
2164 int rv = 0;
2165
Corey Minyard3b625942005-06-23 22:01:42 -07002166#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 sprintf(smi->proc_dir_name, "%d", num);
2168 smi->proc_dir = proc_mkdir(smi->proc_dir_name, proc_ipmi_root);
2169 if (!smi->proc_dir)
2170 rv = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172 if (rv == 0)
2173 rv = ipmi_smi_add_proc_entry(smi, "stats",
Alexey Dobriyan07412732011-05-26 16:25:55 -07002174 &smi_stats_proc_ops,
Alexey Dobriyan99b76232009-03-25 22:48:06 +03002175 smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
2177 if (rv == 0)
2178 rv = ipmi_smi_add_proc_entry(smi, "ipmb",
Alexey Dobriyan07412732011-05-26 16:25:55 -07002179 &smi_ipmb_proc_ops,
Alexey Dobriyan99b76232009-03-25 22:48:06 +03002180 smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 if (rv == 0)
2183 rv = ipmi_smi_add_proc_entry(smi, "version",
Alexey Dobriyan07412732011-05-26 16:25:55 -07002184 &smi_version_proc_ops,
Alexey Dobriyan99b76232009-03-25 22:48:06 +03002185 smi);
Corey Minyard3b625942005-06-23 22:01:42 -07002186#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188 return rv;
2189}
2190
2191static void remove_proc_entries(ipmi_smi_t smi)
2192{
Corey Minyard3b625942005-06-23 22:01:42 -07002193#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 struct ipmi_proc_entry *entry;
2195
Corey Minyardac019152007-10-18 03:07:11 -07002196 mutex_lock(&smi->proc_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 while (smi->proc_entries) {
2198 entry = smi->proc_entries;
2199 smi->proc_entries = entry->next;
2200
2201 remove_proc_entry(entry->name, smi->proc_dir);
2202 kfree(entry->name);
2203 kfree(entry);
2204 }
Corey Minyardac019152007-10-18 03:07:11 -07002205 mutex_unlock(&smi->proc_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 remove_proc_entry(smi->proc_dir_name, proc_ipmi_root);
Corey Minyard3b625942005-06-23 22:01:42 -07002207#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208}
2209
Corey Minyard50c812b2006-03-26 01:37:21 -08002210static int __find_bmc_guid(struct device *dev, void *data)
2211{
2212 unsigned char *id = data;
Corey Minyard16639eb2014-10-10 21:54:03 -05002213 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002214 return memcmp(bmc->guid, id, 16) == 0;
2215}
2216
2217static struct bmc_device *ipmi_find_bmc_guid(struct device_driver *drv,
2218 unsigned char *guid)
2219{
2220 struct device *dev;
2221
2222 dev = driver_find_device(drv, NULL, guid, __find_bmc_guid);
2223 if (dev)
Corey Minyard16639eb2014-10-10 21:54:03 -05002224 return to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002225 else
2226 return NULL;
2227}
2228
2229struct prod_dev_id {
2230 unsigned int product_id;
2231 unsigned char device_id;
2232};
2233
2234static int __find_bmc_prod_dev_id(struct device *dev, void *data)
2235{
2236 struct prod_dev_id *id = data;
Corey Minyard16639eb2014-10-10 21:54:03 -05002237 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002238
2239 return (bmc->id.product_id == id->product_id
Corey Minyard50c812b2006-03-26 01:37:21 -08002240 && bmc->id.device_id == id->device_id);
2241}
2242
2243static struct bmc_device *ipmi_find_bmc_prod_dev_id(
2244 struct device_driver *drv,
Corey Minyardf0b55da2006-12-06 20:40:54 -08002245 unsigned int product_id, unsigned char device_id)
Corey Minyard50c812b2006-03-26 01:37:21 -08002246{
2247 struct prod_dev_id id = {
2248 .product_id = product_id,
2249 .device_id = device_id,
2250 };
2251 struct device *dev;
2252
2253 dev = driver_find_device(drv, NULL, &id, __find_bmc_prod_dev_id);
2254 if (dev)
Corey Minyard16639eb2014-10-10 21:54:03 -05002255 return to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002256 else
2257 return NULL;
2258}
2259
2260static ssize_t device_id_show(struct device *dev,
2261 struct device_attribute *attr,
2262 char *buf)
2263{
Corey Minyard16639eb2014-10-10 21:54:03 -05002264 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002265
2266 return snprintf(buf, 10, "%u\n", bmc->id.device_id);
2267}
Corey Minyard9c633312014-12-12 19:06:07 -06002268static DEVICE_ATTR(device_id, S_IRUGO, device_id_show, NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002269
Corey Minyard16639eb2014-10-10 21:54:03 -05002270static ssize_t provides_device_sdrs_show(struct device *dev,
2271 struct device_attribute *attr,
2272 char *buf)
Corey Minyard50c812b2006-03-26 01:37:21 -08002273{
Corey Minyard16639eb2014-10-10 21:54:03 -05002274 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002275
2276 return snprintf(buf, 10, "%u\n",
Corey Minyard7947d2c2006-11-10 12:27:50 -08002277 (bmc->id.device_revision & 0x80) >> 7);
Corey Minyard50c812b2006-03-26 01:37:21 -08002278}
Corey Minyard9c633312014-12-12 19:06:07 -06002279static DEVICE_ATTR(provides_device_sdrs, S_IRUGO, provides_device_sdrs_show,
2280 NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002281
2282static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
2283 char *buf)
2284{
Corey Minyard16639eb2014-10-10 21:54:03 -05002285 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002286
2287 return snprintf(buf, 20, "%u\n",
Corey Minyard7947d2c2006-11-10 12:27:50 -08002288 bmc->id.device_revision & 0x0F);
Corey Minyard50c812b2006-03-26 01:37:21 -08002289}
Corey Minyard9c633312014-12-12 19:06:07 -06002290static DEVICE_ATTR(revision, S_IRUGO, revision_show, NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002291
Corey Minyard16639eb2014-10-10 21:54:03 -05002292static ssize_t firmware_revision_show(struct device *dev,
2293 struct device_attribute *attr,
2294 char *buf)
Corey Minyard50c812b2006-03-26 01:37:21 -08002295{
Corey Minyard16639eb2014-10-10 21:54:03 -05002296 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002297
2298 return snprintf(buf, 20, "%u.%x\n", bmc->id.firmware_revision_1,
2299 bmc->id.firmware_revision_2);
2300}
Corey Minyard9c633312014-12-12 19:06:07 -06002301static DEVICE_ATTR(firmware_revision, S_IRUGO, firmware_revision_show, NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002302
2303static ssize_t ipmi_version_show(struct device *dev,
2304 struct device_attribute *attr,
2305 char *buf)
2306{
Corey Minyard16639eb2014-10-10 21:54:03 -05002307 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002308
2309 return snprintf(buf, 20, "%u.%u\n",
2310 ipmi_version_major(&bmc->id),
2311 ipmi_version_minor(&bmc->id));
2312}
Corey Minyard9c633312014-12-12 19:06:07 -06002313static DEVICE_ATTR(ipmi_version, S_IRUGO, ipmi_version_show, NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002314
2315static ssize_t add_dev_support_show(struct device *dev,
2316 struct device_attribute *attr,
2317 char *buf)
2318{
Corey Minyard16639eb2014-10-10 21:54:03 -05002319 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002320
2321 return snprintf(buf, 10, "0x%02x\n",
2322 bmc->id.additional_device_support);
2323}
Corey Minyard9c633312014-12-12 19:06:07 -06002324static DEVICE_ATTR(additional_device_support, S_IRUGO, add_dev_support_show,
2325 NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002326
2327static ssize_t manufacturer_id_show(struct device *dev,
2328 struct device_attribute *attr,
2329 char *buf)
2330{
Corey Minyard16639eb2014-10-10 21:54:03 -05002331 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002332
2333 return snprintf(buf, 20, "0x%6.6x\n", bmc->id.manufacturer_id);
2334}
Corey Minyard9c633312014-12-12 19:06:07 -06002335static DEVICE_ATTR(manufacturer_id, S_IRUGO, manufacturer_id_show, NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002336
2337static ssize_t product_id_show(struct device *dev,
2338 struct device_attribute *attr,
2339 char *buf)
2340{
Corey Minyard16639eb2014-10-10 21:54:03 -05002341 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002342
2343 return snprintf(buf, 10, "0x%4.4x\n", bmc->id.product_id);
2344}
Corey Minyard9c633312014-12-12 19:06:07 -06002345static DEVICE_ATTR(product_id, S_IRUGO, product_id_show, NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002346
2347static ssize_t aux_firmware_rev_show(struct device *dev,
2348 struct device_attribute *attr,
2349 char *buf)
2350{
Corey Minyard16639eb2014-10-10 21:54:03 -05002351 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002352
2353 return snprintf(buf, 21, "0x%02x 0x%02x 0x%02x 0x%02x\n",
2354 bmc->id.aux_firmware_revision[3],
2355 bmc->id.aux_firmware_revision[2],
2356 bmc->id.aux_firmware_revision[1],
2357 bmc->id.aux_firmware_revision[0]);
2358}
Corey Minyard9c633312014-12-12 19:06:07 -06002359static DEVICE_ATTR(aux_firmware_revision, S_IRUGO, aux_firmware_rev_show, NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002360
2361static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
2362 char *buf)
2363{
Corey Minyard16639eb2014-10-10 21:54:03 -05002364 struct bmc_device *bmc = to_bmc_device(dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002365
2366 return snprintf(buf, 100, "%Lx%Lx\n",
2367 (long long) bmc->guid[0],
2368 (long long) bmc->guid[8]);
2369}
Corey Minyard9c633312014-12-12 19:06:07 -06002370static DEVICE_ATTR(guid, S_IRUGO, guid_show, NULL);
Corey Minyard50c812b2006-03-26 01:37:21 -08002371
Corey Minyard16639eb2014-10-10 21:54:03 -05002372static struct attribute *bmc_dev_attrs[] = {
2373 &dev_attr_device_id.attr,
2374 &dev_attr_provides_device_sdrs.attr,
2375 &dev_attr_revision.attr,
2376 &dev_attr_firmware_revision.attr,
2377 &dev_attr_ipmi_version.attr,
2378 &dev_attr_additional_device_support.attr,
2379 &dev_attr_manufacturer_id.attr,
2380 &dev_attr_product_id.attr,
Takashi Iwai2d06a0c2015-02-04 15:36:14 +01002381 &dev_attr_aux_firmware_revision.attr,
2382 &dev_attr_guid.attr,
Corey Minyard16639eb2014-10-10 21:54:03 -05002383 NULL
2384};
2385
Takashi Iwai2d06a0c2015-02-04 15:36:14 +01002386static umode_t bmc_dev_attr_is_visible(struct kobject *kobj,
2387 struct attribute *attr, int idx)
2388{
2389 struct device *dev = kobj_to_dev(kobj);
2390 struct bmc_device *bmc = to_bmc_device(dev);
2391 umode_t mode = attr->mode;
2392
2393 if (attr == &dev_attr_aux_firmware_revision.attr)
2394 return bmc->id.aux_firmware_revision_set ? mode : 0;
2395 if (attr == &dev_attr_guid.attr)
2396 return bmc->guid_set ? mode : 0;
2397 return mode;
2398}
2399
Corey Minyard16639eb2014-10-10 21:54:03 -05002400static struct attribute_group bmc_dev_attr_group = {
2401 .attrs = bmc_dev_attrs,
Takashi Iwai2d06a0c2015-02-04 15:36:14 +01002402 .is_visible = bmc_dev_attr_is_visible,
Corey Minyard16639eb2014-10-10 21:54:03 -05002403};
2404
2405static const struct attribute_group *bmc_dev_attr_groups[] = {
2406 &bmc_dev_attr_group,
2407 NULL
2408};
2409
2410static struct device_type bmc_device_type = {
2411 .groups = bmc_dev_attr_groups,
2412};
2413
2414static void
2415release_bmc_device(struct device *dev)
Corey Minyard50c812b2006-03-26 01:37:21 -08002416{
Corey Minyard16639eb2014-10-10 21:54:03 -05002417 kfree(to_bmc_device(dev));
Jeff Garzik5e593932006-10-11 01:22:21 -07002418}
2419
2420static void
2421cleanup_bmc_device(struct kref *ref)
2422{
Corey Minyard16639eb2014-10-10 21:54:03 -05002423 struct bmc_device *bmc = container_of(ref, struct bmc_device, usecount);
Jeff Garzik5e593932006-10-11 01:22:21 -07002424
Corey Minyard16639eb2014-10-10 21:54:03 -05002425 platform_device_unregister(&bmc->pdev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002426}
2427
2428static void ipmi_bmc_unregister(ipmi_smi_t intf)
2429{
2430 struct bmc_device *bmc = intf->bmc;
2431
Corey Minyard5a0e10e2014-10-10 22:11:05 -05002432 sysfs_remove_link(&intf->si_dev->kobj, "bmc");
Corey Minyard50c812b2006-03-26 01:37:21 -08002433 if (intf->my_dev_name) {
Corey Minyard16639eb2014-10-10 21:54:03 -05002434 sysfs_remove_link(&bmc->pdev.dev.kobj, intf->my_dev_name);
Corey Minyard50c812b2006-03-26 01:37:21 -08002435 kfree(intf->my_dev_name);
2436 intf->my_dev_name = NULL;
2437 }
2438
2439 mutex_lock(&ipmidriver_mutex);
Corey Minyard16639eb2014-10-10 21:54:03 -05002440 kref_put(&bmc->usecount, cleanup_bmc_device);
Corey Minyardf0b55da2006-12-06 20:40:54 -08002441 intf->bmc = NULL;
Corey Minyard50c812b2006-03-26 01:37:21 -08002442 mutex_unlock(&ipmidriver_mutex);
2443}
2444
Corey Minyard5a0e10e2014-10-10 22:11:05 -05002445static int ipmi_bmc_register(ipmi_smi_t intf, int ifnum)
Corey Minyard50c812b2006-03-26 01:37:21 -08002446{
2447 int rv;
2448 struct bmc_device *bmc = intf->bmc;
2449 struct bmc_device *old_bmc;
Corey Minyard50c812b2006-03-26 01:37:21 -08002450
2451 mutex_lock(&ipmidriver_mutex);
2452
2453 /*
2454 * Try to find if there is an bmc_device struct
2455 * representing the interfaced BMC already
2456 */
2457 if (bmc->guid_set)
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08002458 old_bmc = ipmi_find_bmc_guid(&ipmidriver.driver, bmc->guid);
Corey Minyard50c812b2006-03-26 01:37:21 -08002459 else
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08002460 old_bmc = ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
Corey Minyard50c812b2006-03-26 01:37:21 -08002461 bmc->id.product_id,
2462 bmc->id.device_id);
2463
2464 /*
2465 * If there is already an bmc_device, free the new one,
2466 * otherwise register the new BMC device
2467 */
2468 if (old_bmc) {
2469 kfree(bmc);
2470 intf->bmc = old_bmc;
2471 bmc = old_bmc;
2472
Corey Minyard16639eb2014-10-10 21:54:03 -05002473 kref_get(&bmc->usecount);
Corey Minyard50c812b2006-03-26 01:37:21 -08002474 mutex_unlock(&ipmidriver_mutex);
2475
2476 printk(KERN_INFO
2477 "ipmi: interfacing existing BMC (man_id: 0x%6.6x,"
2478 " prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2479 bmc->id.manufacturer_id,
2480 bmc->id.product_id,
2481 bmc->id.device_id);
2482 } else {
Corey Minyardf0b55da2006-12-06 20:40:54 -08002483 unsigned char orig_dev_id = bmc->id.device_id;
2484 int warn_printed = 0;
2485
Corey Minyard16639eb2014-10-10 21:54:03 -05002486 snprintf(bmc->name, sizeof(bmc->name),
Corey Minyardf0b55da2006-12-06 20:40:54 -08002487 "ipmi_bmc.%4.4x", bmc->id.product_id);
Corey Minyard16639eb2014-10-10 21:54:03 -05002488 bmc->pdev.name = bmc->name;
Corey Minyardf0b55da2006-12-06 20:40:54 -08002489
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08002490 while (ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
Corey Minyardf0b55da2006-12-06 20:40:54 -08002491 bmc->id.product_id,
Corey Minyard1d5636c2006-12-10 02:19:08 -08002492 bmc->id.device_id)) {
Corey Minyardf0b55da2006-12-06 20:40:54 -08002493 if (!warn_printed) {
2494 printk(KERN_WARNING PFX
2495 "This machine has two different BMCs"
2496 " with the same product id and device"
2497 " id. This is an error in the"
2498 " firmware, but incrementing the"
2499 " device id to work around the problem."
2500 " Prod ID = 0x%x, Dev ID = 0x%x\n",
2501 bmc->id.product_id, bmc->id.device_id);
2502 warn_printed = 1;
2503 }
2504 bmc->id.device_id++; /* Wraps at 255 */
2505 if (bmc->id.device_id == orig_dev_id) {
2506 printk(KERN_ERR PFX
2507 "Out of device ids!\n");
2508 break;
2509 }
2510 }
2511
Corey Minyard16639eb2014-10-10 21:54:03 -05002512 bmc->pdev.dev.driver = &ipmidriver.driver;
2513 bmc->pdev.id = bmc->id.device_id;
2514 bmc->pdev.dev.release = release_bmc_device;
2515 bmc->pdev.dev.type = &bmc_device_type;
Corey Minyard5a0e10e2014-10-10 22:11:05 -05002516 kref_init(&bmc->usecount);
Corey Minyard50c812b2006-03-26 01:37:21 -08002517
Corey Minyard16639eb2014-10-10 21:54:03 -05002518 rv = platform_device_register(&bmc->pdev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002519 mutex_unlock(&ipmidriver_mutex);
2520 if (rv) {
Corey Minyard16639eb2014-10-10 21:54:03 -05002521 put_device(&bmc->pdev.dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002522 printk(KERN_ERR
2523 "ipmi_msghandler:"
2524 " Unable to register bmc device: %d\n",
2525 rv);
Corey Minyardc70d7492008-04-29 01:01:09 -07002526 /*
2527 * Don't go to out_err, you can only do that if
2528 * the device is registered already.
2529 */
Corey Minyard50c812b2006-03-26 01:37:21 -08002530 return rv;
2531 }
2532
Myron Stowe279fbd02010-05-26 14:43:52 -07002533 dev_info(intf->si_dev, "Found new BMC (man_id: 0x%6.6x, "
2534 "prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2535 bmc->id.manufacturer_id,
2536 bmc->id.product_id,
2537 bmc->id.device_id);
Corey Minyard50c812b2006-03-26 01:37:21 -08002538 }
2539
2540 /*
2541 * create symlink from system interface device to bmc device
2542 * and back.
2543 */
Corey Minyard5a0e10e2014-10-10 22:11:05 -05002544 rv = sysfs_create_link(&intf->si_dev->kobj, &bmc->pdev.dev.kobj, "bmc");
Corey Minyard50c812b2006-03-26 01:37:21 -08002545 if (rv) {
2546 printk(KERN_ERR
2547 "ipmi_msghandler: Unable to create bmc symlink: %d\n",
2548 rv);
2549 goto out_err;
2550 }
2551
Corey Minyard16639eb2014-10-10 21:54:03 -05002552 intf->my_dev_name = kasprintf(GFP_KERNEL, "ipmi%d", ifnum);
Corey Minyard50c812b2006-03-26 01:37:21 -08002553 if (!intf->my_dev_name) {
2554 rv = -ENOMEM;
2555 printk(KERN_ERR
2556 "ipmi_msghandler: allocate link from BMC: %d\n",
2557 rv);
2558 goto out_err;
2559 }
Corey Minyard50c812b2006-03-26 01:37:21 -08002560
Corey Minyard16639eb2014-10-10 21:54:03 -05002561 rv = sysfs_create_link(&bmc->pdev.dev.kobj, &intf->si_dev->kobj,
Corey Minyard50c812b2006-03-26 01:37:21 -08002562 intf->my_dev_name);
2563 if (rv) {
2564 kfree(intf->my_dev_name);
2565 intf->my_dev_name = NULL;
2566 printk(KERN_ERR
2567 "ipmi_msghandler:"
2568 " Unable to create symlink to bmc: %d\n",
2569 rv);
2570 goto out_err;
2571 }
2572
2573 return 0;
2574
2575out_err:
2576 ipmi_bmc_unregister(intf);
2577 return rv;
2578}
2579
2580static int
2581send_guid_cmd(ipmi_smi_t intf, int chan)
2582{
2583 struct kernel_ipmi_msg msg;
2584 struct ipmi_system_interface_addr si;
2585
2586 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2587 si.channel = IPMI_BMC_CHANNEL;
2588 si.lun = 0;
2589
2590 msg.netfn = IPMI_NETFN_APP_REQUEST;
2591 msg.cmd = IPMI_GET_DEVICE_GUID_CMD;
2592 msg.data = NULL;
2593 msg.data_len = 0;
2594 return i_ipmi_request(NULL,
2595 intf,
2596 (struct ipmi_addr *) &si,
2597 0,
2598 &msg,
2599 intf,
2600 NULL,
2601 NULL,
2602 0,
2603 intf->channels[0].address,
2604 intf->channels[0].lun,
2605 -1, 0);
2606}
2607
2608static void
2609guid_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
2610{
2611 if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2612 || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
2613 || (msg->msg.cmd != IPMI_GET_DEVICE_GUID_CMD))
2614 /* Not for me */
2615 return;
2616
2617 if (msg->msg.data[0] != 0) {
2618 /* Error from getting the GUID, the BMC doesn't have one. */
2619 intf->bmc->guid_set = 0;
2620 goto out;
2621 }
2622
2623 if (msg->msg.data_len < 17) {
2624 intf->bmc->guid_set = 0;
2625 printk(KERN_WARNING PFX
2626 "guid_handler: The GUID response from the BMC was too"
2627 " short, it was %d but should have been 17. Assuming"
2628 " GUID is not available.\n",
2629 msg->msg.data_len);
2630 goto out;
2631 }
2632
2633 memcpy(intf->bmc->guid, msg->msg.data, 16);
2634 intf->bmc->guid_set = 1;
2635 out:
2636 wake_up(&intf->waitq);
2637}
2638
2639static void
2640get_guid(ipmi_smi_t intf)
2641{
2642 int rv;
2643
2644 intf->bmc->guid_set = 0x2;
2645 intf->null_user_handler = guid_handler;
2646 rv = send_guid_cmd(intf, 0);
2647 if (rv)
2648 /* Send failed, no GUID available. */
2649 intf->bmc->guid_set = 0;
2650 wait_event(intf->waitq, intf->bmc->guid_set != 2);
2651 intf->null_user_handler = NULL;
2652}
2653
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654static int
2655send_channel_info_cmd(ipmi_smi_t intf, int chan)
2656{
2657 struct kernel_ipmi_msg msg;
2658 unsigned char data[1];
2659 struct ipmi_system_interface_addr si;
2660
2661 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2662 si.channel = IPMI_BMC_CHANNEL;
2663 si.lun = 0;
2664
2665 msg.netfn = IPMI_NETFN_APP_REQUEST;
2666 msg.cmd = IPMI_GET_CHANNEL_INFO_CMD;
2667 msg.data = data;
2668 msg.data_len = 1;
2669 data[0] = chan;
2670 return i_ipmi_request(NULL,
2671 intf,
2672 (struct ipmi_addr *) &si,
2673 0,
2674 &msg,
Corey Minyard56a55ec2005-09-06 15:18:42 -07002675 intf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 NULL,
2677 NULL,
2678 0,
Corey Minyardc14979b2005-09-06 15:18:38 -07002679 intf->channels[0].address,
2680 intf->channels[0].lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 -1, 0);
2682}
2683
2684static void
Corey Minyard56a55ec2005-09-06 15:18:42 -07002685channel_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686{
2687 int rv = 0;
2688 int chan;
2689
Corey Minyard56a55ec2005-09-06 15:18:42 -07002690 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2691 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
Corey Minyardc70d7492008-04-29 01:01:09 -07002692 && (msg->msg.cmd == IPMI_GET_CHANNEL_INFO_CMD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 /* It's the one we want */
Corey Minyard56a55ec2005-09-06 15:18:42 -07002694 if (msg->msg.data[0] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 /* Got an error from the channel, just go on. */
2696
Corey Minyard56a55ec2005-09-06 15:18:42 -07002697 if (msg->msg.data[0] == IPMI_INVALID_COMMAND_ERR) {
Corey Minyardc70d7492008-04-29 01:01:09 -07002698 /*
2699 * If the MC does not support this
2700 * command, that is legal. We just
2701 * assume it has one IPMB at channel
2702 * zero.
2703 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 intf->channels[0].medium
2705 = IPMI_CHANNEL_MEDIUM_IPMB;
2706 intf->channels[0].protocol
2707 = IPMI_CHANNEL_PROTOCOL_IPMB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
2709 intf->curr_channel = IPMI_MAX_CHANNELS;
2710 wake_up(&intf->waitq);
2711 goto out;
2712 }
2713 goto next_channel;
2714 }
Corey Minyard56a55ec2005-09-06 15:18:42 -07002715 if (msg->msg.data_len < 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 /* Message not big enough, just go on. */
2717 goto next_channel;
2718 }
2719 chan = intf->curr_channel;
Corey Minyard56a55ec2005-09-06 15:18:42 -07002720 intf->channels[chan].medium = msg->msg.data[2] & 0x7f;
2721 intf->channels[chan].protocol = msg->msg.data[3] & 0x1f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
Corey Minyardc70d7492008-04-29 01:01:09 -07002723 next_channel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 intf->curr_channel++;
2725 if (intf->curr_channel >= IPMI_MAX_CHANNELS)
2726 wake_up(&intf->waitq);
2727 else
2728 rv = send_channel_info_cmd(intf, intf->curr_channel);
2729
2730 if (rv) {
2731 /* Got an error somehow, just give up. */
Corey Minyard1f668422014-10-06 14:17:50 -05002732 printk(KERN_WARNING PFX
2733 "Error sending channel information for channel"
2734 " %d: %d\n", intf->curr_channel, rv);
2735
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 intf->curr_channel = IPMI_MAX_CHANNELS;
2737 wake_up(&intf->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 }
2739 }
2740 out:
2741 return;
2742}
2743
Corey Minyard895dcfd2012-03-28 14:42:49 -07002744static void ipmi_poll(ipmi_smi_t intf)
Corey Minyardfcfa4722007-10-18 03:07:09 -07002745{
Corey Minyardfcfa4722007-10-18 03:07:09 -07002746 if (intf->handlers->poll)
2747 intf->handlers->poll(intf->send_info);
Corey Minyard7adf5792012-03-28 14:42:49 -07002748 /* In case something came in */
2749 handle_new_recv_msgs(intf);
Corey Minyardfcfa4722007-10-18 03:07:09 -07002750}
Corey Minyard895dcfd2012-03-28 14:42:49 -07002751
2752void ipmi_poll_interface(ipmi_user_t user)
2753{
2754 ipmi_poll(user->intf);
Corey Minyardfcfa4722007-10-18 03:07:09 -07002755}
Corey Minyardc70d7492008-04-29 01:01:09 -07002756EXPORT_SYMBOL(ipmi_poll_interface);
Corey Minyardfcfa4722007-10-18 03:07:09 -07002757
Corey Minyard81d02b72015-06-13 10:34:25 -05002758int ipmi_register_smi(const struct ipmi_smi_handlers *handlers,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 void *send_info,
Corey Minyard50c812b2006-03-26 01:37:21 -08002760 struct ipmi_device_id *device_id,
2761 struct device *si_dev,
Corey Minyard453823b2006-03-31 02:30:39 -08002762 unsigned char slave_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763{
2764 int i, j;
2765 int rv;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002766 ipmi_smi_t intf;
Corey Minyardbca03242006-12-06 20:40:57 -08002767 ipmi_smi_t tintf;
Corey Minyardbca03242006-12-06 20:40:57 -08002768 struct list_head *link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
Corey Minyardc70d7492008-04-29 01:01:09 -07002770 /*
2771 * Make sure the driver is actually initialized, this handles
2772 * problems with initialization order.
2773 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 if (!initialized) {
2775 rv = ipmi_init_msghandler();
2776 if (rv)
2777 return rv;
Corey Minyardc70d7492008-04-29 01:01:09 -07002778 /*
2779 * The init code doesn't return an error if it was turned
2780 * off, but it won't initialize. Check that.
2781 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 if (!initialized)
2783 return -ENODEV;
2784 }
2785
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002786 intf = kzalloc(sizeof(*intf), GFP_KERNEL);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002787 if (!intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 return -ENOMEM;
Corey Minyardb2c03942006-12-06 20:41:00 -08002789
2790 intf->ipmi_version_major = ipmi_version_major(device_id);
2791 intf->ipmi_version_minor = ipmi_version_minor(device_id);
2792
Corey Minyard50c812b2006-03-26 01:37:21 -08002793 intf->bmc = kzalloc(sizeof(*intf->bmc), GFP_KERNEL);
2794 if (!intf->bmc) {
2795 kfree(intf);
2796 return -ENOMEM;
2797 }
Corey Minyardbca03242006-12-06 20:40:57 -08002798 intf->intf_num = -1; /* Mark it invalid for now. */
Corey Minyard393d2cc2005-11-07 00:59:54 -08002799 kref_init(&intf->refcount);
Corey Minyard50c812b2006-03-26 01:37:21 -08002800 intf->bmc->id = *device_id;
2801 intf->si_dev = si_dev;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002802 for (j = 0; j < IPMI_MAX_CHANNELS; j++) {
2803 intf->channels[j].address = IPMI_BMC_SLAVE_ADDR;
2804 intf->channels[j].lun = 2;
2805 }
2806 if (slave_addr != 0)
2807 intf->channels[0].address = slave_addr;
2808 INIT_LIST_HEAD(&intf->users);
2809 intf->handlers = handlers;
2810 intf->send_info = send_info;
2811 spin_lock_init(&intf->seq_lock);
2812 for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++) {
2813 intf->seq_table[j].inuse = 0;
2814 intf->seq_table[j].seqid = 0;
2815 }
2816 intf->curr_seq = 0;
2817#ifdef CONFIG_PROC_FS
Corey Minyardac019152007-10-18 03:07:11 -07002818 mutex_init(&intf->proc_entry_lock);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002819#endif
Corey Minyard65be7542014-11-06 20:48:02 -06002820 spin_lock_init(&intf->waiting_rcv_msgs_lock);
2821 INIT_LIST_HEAD(&intf->waiting_rcv_msgs);
Corey Minyard7adf5792012-03-28 14:42:49 -07002822 tasklet_init(&intf->recv_tasklet,
2823 smi_recv_tasklet,
2824 (unsigned long) intf);
2825 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 0);
Corey Minyard7ea0ed22014-11-06 16:58:48 -06002826 spin_lock_init(&intf->xmit_msgs_lock);
2827 INIT_LIST_HEAD(&intf->xmit_msgs);
2828 INIT_LIST_HEAD(&intf->hp_xmit_msgs);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002829 spin_lock_init(&intf->events_lock);
Corey Minyard89986492014-04-14 09:46:54 -05002830 atomic_set(&intf->event_waiters, 0);
2831 intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002832 INIT_LIST_HEAD(&intf->waiting_events);
2833 intf->waiting_events_count = 0;
Corey Minyardd6dfd132006-03-31 02:30:41 -08002834 mutex_init(&intf->cmd_rcvrs_mutex);
Corey Minyardb9675132006-12-06 20:41:02 -08002835 spin_lock_init(&intf->maintenance_mode_lock);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002836 INIT_LIST_HEAD(&intf->cmd_rcvrs);
2837 init_waitqueue_head(&intf->waitq);
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07002838 for (i = 0; i < IPMI_NUM_STATS; i++)
2839 atomic_set(&intf->stats[i], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Corey Minyard393d2cc2005-11-07 00:59:54 -08002841 intf->proc_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
Corey Minyardb2c03942006-12-06 20:41:00 -08002843 mutex_lock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -08002844 mutex_lock(&ipmi_interfaces_mutex);
2845 /* Look for a hole in the numbers. */
2846 i = 0;
2847 link = &ipmi_interfaces;
2848 list_for_each_entry_rcu(tintf, &ipmi_interfaces, link) {
2849 if (tintf->intf_num != i) {
2850 link = &tintf->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 break;
2852 }
Corey Minyardbca03242006-12-06 20:40:57 -08002853 i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 }
Corey Minyardbca03242006-12-06 20:40:57 -08002855 /* Add the new interface in numeric order. */
2856 if (i == 0)
2857 list_add_rcu(&intf->link, &ipmi_interfaces);
2858 else
2859 list_add_tail_rcu(&intf->link, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860
Corey Minyard453823b2006-03-31 02:30:39 -08002861 rv = handlers->start_processing(send_info, intf);
2862 if (rv)
2863 goto out;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002864
Corey Minyard50c812b2006-03-26 01:37:21 -08002865 get_guid(intf);
2866
Corey Minyardb2c03942006-12-06 20:41:00 -08002867 if ((intf->ipmi_version_major > 1)
Corey Minyardc70d7492008-04-29 01:01:09 -07002868 || ((intf->ipmi_version_major == 1)
2869 && (intf->ipmi_version_minor >= 5))) {
2870 /*
2871 * Start scanning the channels to see what is
2872 * available.
2873 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08002874 intf->null_user_handler = channel_handler;
2875 intf->curr_channel = 0;
2876 rv = send_channel_info_cmd(intf, 0);
Corey Minyard1f668422014-10-06 14:17:50 -05002877 if (rv) {
2878 printk(KERN_WARNING PFX
2879 "Error sending channel information for channel"
2880 " 0, %d\n", rv);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002881 goto out;
Corey Minyard1f668422014-10-06 14:17:50 -05002882 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08002883
2884 /* Wait for the channel info to be read. */
2885 wait_event(intf->waitq,
2886 intf->curr_channel >= IPMI_MAX_CHANNELS);
Corey Minyard50c812b2006-03-26 01:37:21 -08002887 intf->null_user_handler = NULL;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002888 } else {
2889 /* Assume a single IPMB channel at zero. */
2890 intf->channels[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
2891 intf->channels[0].protocol = IPMI_CHANNEL_PROTOCOL_IPMB;
Corey Minyard9a2845c2009-05-20 13:36:17 -05002892 intf->curr_channel = IPMI_MAX_CHANNELS;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
Xie XiuQibd85f4b32016-09-27 15:07:12 +08002895 rv = ipmi_bmc_register(intf, i);
2896
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 if (rv == 0)
Corey Minyard393d2cc2005-11-07 00:59:54 -08002898 rv = add_proc_entries(intf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
Corey Minyard393d2cc2005-11-07 00:59:54 -08002900 out:
2901 if (rv) {
2902 if (intf->proc_dir)
2903 remove_proc_entries(intf);
Corey Minyardb2c03942006-12-06 20:41:00 -08002904 intf->handlers = NULL;
Corey Minyardbca03242006-12-06 20:40:57 -08002905 list_del_rcu(&intf->link);
2906 mutex_unlock(&ipmi_interfaces_mutex);
Corey Minyardb2c03942006-12-06 20:41:00 -08002907 mutex_unlock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -08002908 synchronize_rcu();
Corey Minyard393d2cc2005-11-07 00:59:54 -08002909 kref_put(&intf->refcount, intf_free);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002910 } else {
Corey Minyard78ba2fa2007-02-10 01:45:45 -08002911 /*
2912 * Keep memory order straight for RCU readers. Make
2913 * sure everything else is committed to memory before
2914 * setting intf_num to mark the interface valid.
2915 */
2916 smp_wmb();
Corey Minyardbca03242006-12-06 20:40:57 -08002917 intf->intf_num = i;
2918 mutex_unlock(&ipmi_interfaces_mutex);
Corey Minyard78ba2fa2007-02-10 01:45:45 -08002919 /* After this point the interface is legal to use. */
Corey Minyard50c812b2006-03-26 01:37:21 -08002920 call_smi_watchers(i, intf->si_dev);
Corey Minyardb2c03942006-12-06 20:41:00 -08002921 mutex_unlock(&smi_watchers_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 }
2923
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 return rv;
2925}
Corey Minyardc70d7492008-04-29 01:01:09 -07002926EXPORT_SYMBOL(ipmi_register_smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Corey Minyard7ea0ed22014-11-06 16:58:48 -06002928static void deliver_smi_err_response(ipmi_smi_t intf,
2929 struct ipmi_smi_msg *msg,
2930 unsigned char err)
2931{
2932 msg->rsp[0] = msg->data[0] | 4;
2933 msg->rsp[1] = msg->data[1];
2934 msg->rsp[2] = err;
2935 msg->rsp_size = 3;
2936 /* It's an error, so it will never requeue, no need to check return. */
2937 handle_one_recv_msg(intf, msg);
2938}
2939
Corey Minyardb2c03942006-12-06 20:41:00 -08002940static void cleanup_smi_msgs(ipmi_smi_t intf)
2941{
2942 int i;
2943 struct seq_table *ent;
Corey Minyard7ea0ed22014-11-06 16:58:48 -06002944 struct ipmi_smi_msg *msg;
2945 struct list_head *entry;
2946 struct list_head tmplist;
2947
2948 /* Clear out our transmit queues and hold the messages. */
2949 INIT_LIST_HEAD(&tmplist);
2950 list_splice_tail(&intf->hp_xmit_msgs, &tmplist);
2951 list_splice_tail(&intf->xmit_msgs, &tmplist);
2952
2953 /* Current message first, to preserve order */
2954 while (intf->curr_msg && !list_empty(&intf->waiting_rcv_msgs)) {
2955 /* Wait for the message to clear out. */
2956 schedule_timeout(1);
2957 }
Corey Minyardb2c03942006-12-06 20:41:00 -08002958
2959 /* No need for locks, the interface is down. */
Corey Minyard7ea0ed22014-11-06 16:58:48 -06002960
2961 /*
2962 * Return errors for all pending messages in queue and in the
2963 * tables waiting for remote responses.
2964 */
2965 while (!list_empty(&tmplist)) {
2966 entry = tmplist.next;
2967 list_del(entry);
2968 msg = list_entry(entry, struct ipmi_smi_msg, link);
2969 deliver_smi_err_response(intf, msg, IPMI_ERR_UNSPECIFIED);
2970 }
2971
Corey Minyardb2c03942006-12-06 20:41:00 -08002972 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
2973 ent = &(intf->seq_table[i]);
2974 if (!ent->inuse)
2975 continue;
2976 deliver_err_response(ent->recv_msg, IPMI_ERR_UNSPECIFIED);
2977 }
2978}
2979
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980int ipmi_unregister_smi(ipmi_smi_t intf)
2981{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 struct ipmi_smi_watcher *w;
Corey Minyard7ea0ed22014-11-06 16:58:48 -06002983 int intf_num = intf->intf_num;
2984 ipmi_user_t user;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
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);
Xie XiuQibd85f4b32016-09-27 15:07:12 +08003009 ipmi_bmc_unregister(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Corey Minyardc70d7492008-04-29 01:01:09 -07003011 /*
3012 * Call all the watcher interfaces to tell them that
3013 * an interface is gone.
3014 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08003015 list_for_each_entry(w, &smi_watchers, link)
Corey Minyardb2c03942006-12-06 20:41:00 -08003016 w->smi_gone(intf_num);
3017 mutex_unlock(&smi_watchers_mutex);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003018
Corey Minyard393d2cc2005-11-07 00:59:54 -08003019 kref_put(&intf->refcount, intf_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 return 0;
3021}
Corey Minyardc70d7492008-04-29 01:01:09 -07003022EXPORT_SYMBOL(ipmi_unregister_smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
3024static int handle_ipmb_get_msg_rsp(ipmi_smi_t intf,
3025 struct ipmi_smi_msg *msg)
3026{
3027 struct ipmi_ipmb_addr ipmb_addr;
3028 struct ipmi_recv_msg *recv_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
Corey Minyardc70d7492008-04-29 01:01:09 -07003030 /*
3031 * This is 11, not 10, because the response must contain a
3032 * completion code.
3033 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 if (msg->rsp_size < 11) {
3035 /* Message not big enough, just ignore it. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003036 ipmi_inc_stat(intf, invalid_ipmb_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 return 0;
3038 }
3039
3040 if (msg->rsp[2] != 0) {
3041 /* An error getting the response, just ignore it. */
3042 return 0;
3043 }
3044
3045 ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
3046 ipmb_addr.slave_addr = msg->rsp[6];
3047 ipmb_addr.channel = msg->rsp[3] & 0x0f;
3048 ipmb_addr.lun = msg->rsp[7] & 3;
3049
Corey Minyardc70d7492008-04-29 01:01:09 -07003050 /*
3051 * It's a response from a remote entity. Look up the sequence
3052 * number and handle the response.
3053 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 if (intf_find_seq(intf,
3055 msg->rsp[7] >> 2,
3056 msg->rsp[3] & 0x0f,
3057 msg->rsp[8],
3058 (msg->rsp[4] >> 2) & (~1),
3059 (struct ipmi_addr *) &(ipmb_addr),
Corey Minyardc70d7492008-04-29 01:01:09 -07003060 &recv_msg)) {
3061 /*
3062 * We were unable to find the sequence number,
3063 * so just nuke the message.
3064 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003065 ipmi_inc_stat(intf, unhandled_ipmb_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 return 0;
3067 }
3068
3069 memcpy(recv_msg->msg_data,
3070 &(msg->rsp[9]),
3071 msg->rsp_size - 9);
Corey Minyardc70d7492008-04-29 01:01:09 -07003072 /*
3073 * The other fields matched, so no need to set them, except
3074 * for netfn, which needs to be the response that was
3075 * returned, not the request value.
3076 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3078 recv_msg->msg.data = recv_msg->msg_data;
3079 recv_msg->msg.data_len = msg->rsp_size - 10;
3080 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003081 ipmi_inc_stat(intf, handled_ipmb_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 deliver_response(recv_msg);
3083
3084 return 0;
3085}
3086
3087static int handle_ipmb_get_msg_cmd(ipmi_smi_t intf,
3088 struct ipmi_smi_msg *msg)
3089{
Corey Minyard393d2cc2005-11-07 00:59:54 -08003090 struct cmd_rcvr *rcvr;
3091 int rv = 0;
3092 unsigned char netfn;
3093 unsigned char cmd;
Corey Minyardc69c3122006-09-30 23:27:56 -07003094 unsigned char chan;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003095 ipmi_user_t user = NULL;
3096 struct ipmi_ipmb_addr *ipmb_addr;
3097 struct ipmi_recv_msg *recv_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
3099 if (msg->rsp_size < 10) {
3100 /* Message not big enough, just ignore it. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003101 ipmi_inc_stat(intf, invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 return 0;
3103 }
3104
3105 if (msg->rsp[2] != 0) {
3106 /* An error getting the response, just ignore it. */
3107 return 0;
3108 }
3109
3110 netfn = msg->rsp[4] >> 2;
3111 cmd = msg->rsp[8];
Corey Minyardc69c3122006-09-30 23:27:56 -07003112 chan = msg->rsp[3] & 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
Corey Minyarde61fb5b2005-11-07 01:00:05 -08003114 rcu_read_lock();
Corey Minyardc69c3122006-09-30 23:27:56 -07003115 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003116 if (rcvr) {
3117 user = rcvr->user;
3118 kref_get(&user->refcount);
3119 } else
3120 user = NULL;
Corey Minyarde61fb5b2005-11-07 01:00:05 -08003121 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
3123 if (user == NULL) {
3124 /* We didn't find a user, deliver an error response. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003125 ipmi_inc_stat(intf, unhandled_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
3127 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
3128 msg->data[1] = IPMI_SEND_MSG_CMD;
3129 msg->data[2] = msg->rsp[3];
3130 msg->data[3] = msg->rsp[6];
Corey Minyardc70d7492008-04-29 01:01:09 -07003131 msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 msg->data[5] = ipmb_checksum(&(msg->data[3]), 2);
Corey Minyardc14979b2005-09-06 15:18:38 -07003133 msg->data[6] = intf->channels[msg->rsp[3] & 0xf].address;
Corey Minyardc70d7492008-04-29 01:01:09 -07003134 /* rqseq/lun */
3135 msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 msg->data[8] = msg->rsp[8]; /* cmd */
3137 msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
3138 msg->data[10] = ipmb_checksum(&(msg->data[6]), 4);
3139 msg->data_size = 11;
3140
3141#ifdef DEBUG_MSGING
3142 {
3143 int m;
3144 printk("Invalid command:");
Corey Minyarde8b33612005-09-06 15:18:45 -07003145 for (m = 0; m < msg->data_size; m++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 printk(" %2.2x", msg->data[m]);
3147 printk("\n");
3148 }
3149#endif
Corey Minyardb2c03942006-12-06 20:41:00 -08003150 rcu_read_lock();
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003151 if (!intf->in_shutdown) {
3152 smi_send(intf, intf->handlers, msg, 0);
Corey Minyardc70d7492008-04-29 01:01:09 -07003153 /*
3154 * We used the message, so return the value
3155 * that causes it to not be freed or
3156 * queued.
3157 */
Corey Minyardb2c03942006-12-06 20:41:00 -08003158 rv = -1;
3159 }
3160 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 } else {
3162 /* Deliver the message to the user. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003163 ipmi_inc_stat(intf, handled_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
3165 recv_msg = ipmi_alloc_recv_msg();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003166 if (!recv_msg) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003167 /*
3168 * We couldn't allocate memory for the
3169 * message, so requeue it for handling
3170 * later.
3171 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 rv = 1;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003173 kref_put(&user->refcount, free_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 } else {
3175 /* Extract the source address from the data. */
3176 ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
3177 ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
3178 ipmb_addr->slave_addr = msg->rsp[6];
3179 ipmb_addr->lun = msg->rsp[7] & 3;
3180 ipmb_addr->channel = msg->rsp[3] & 0xf;
3181
Corey Minyardc70d7492008-04-29 01:01:09 -07003182 /*
3183 * Extract the rest of the message information
3184 * from the IPMB header.
3185 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 recv_msg->user = user;
3187 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3188 recv_msg->msgid = msg->rsp[7] >> 2;
3189 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3190 recv_msg->msg.cmd = msg->rsp[8];
3191 recv_msg->msg.data = recv_msg->msg_data;
3192
Corey Minyardc70d7492008-04-29 01:01:09 -07003193 /*
3194 * We chop off 10, not 9 bytes because the checksum
3195 * at the end also needs to be removed.
3196 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 recv_msg->msg.data_len = msg->rsp_size - 10;
3198 memcpy(recv_msg->msg_data,
3199 &(msg->rsp[9]),
3200 msg->rsp_size - 10);
3201 deliver_response(recv_msg);
3202 }
3203 }
3204
3205 return rv;
3206}
3207
3208static int handle_lan_get_msg_rsp(ipmi_smi_t intf,
3209 struct ipmi_smi_msg *msg)
3210{
3211 struct ipmi_lan_addr lan_addr;
3212 struct ipmi_recv_msg *recv_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
3214
Corey Minyardc70d7492008-04-29 01:01:09 -07003215 /*
3216 * This is 13, not 12, because the response must contain a
3217 * completion code.
3218 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 if (msg->rsp_size < 13) {
3220 /* Message not big enough, just ignore it. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003221 ipmi_inc_stat(intf, invalid_lan_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 return 0;
3223 }
3224
3225 if (msg->rsp[2] != 0) {
3226 /* An error getting the response, just ignore it. */
3227 return 0;
3228 }
3229
3230 lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
3231 lan_addr.session_handle = msg->rsp[4];
3232 lan_addr.remote_SWID = msg->rsp[8];
3233 lan_addr.local_SWID = msg->rsp[5];
3234 lan_addr.channel = msg->rsp[3] & 0x0f;
3235 lan_addr.privilege = msg->rsp[3] >> 4;
3236 lan_addr.lun = msg->rsp[9] & 3;
3237
Corey Minyardc70d7492008-04-29 01:01:09 -07003238 /*
3239 * It's a response from a remote entity. Look up the sequence
3240 * number and handle the response.
3241 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 if (intf_find_seq(intf,
3243 msg->rsp[9] >> 2,
3244 msg->rsp[3] & 0x0f,
3245 msg->rsp[10],
3246 (msg->rsp[6] >> 2) & (~1),
3247 (struct ipmi_addr *) &(lan_addr),
Corey Minyardc70d7492008-04-29 01:01:09 -07003248 &recv_msg)) {
3249 /*
3250 * We were unable to find the sequence number,
3251 * so just nuke the message.
3252 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003253 ipmi_inc_stat(intf, unhandled_lan_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 return 0;
3255 }
3256
3257 memcpy(recv_msg->msg_data,
3258 &(msg->rsp[11]),
3259 msg->rsp_size - 11);
Corey Minyardc70d7492008-04-29 01:01:09 -07003260 /*
3261 * The other fields matched, so no need to set them, except
3262 * for netfn, which needs to be the response that was
3263 * returned, not the request value.
3264 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3266 recv_msg->msg.data = recv_msg->msg_data;
3267 recv_msg->msg.data_len = msg->rsp_size - 12;
3268 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003269 ipmi_inc_stat(intf, handled_lan_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 deliver_response(recv_msg);
3271
3272 return 0;
3273}
3274
3275static int handle_lan_get_msg_cmd(ipmi_smi_t intf,
3276 struct ipmi_smi_msg *msg)
3277{
Corey Minyard393d2cc2005-11-07 00:59:54 -08003278 struct cmd_rcvr *rcvr;
3279 int rv = 0;
3280 unsigned char netfn;
3281 unsigned char cmd;
Corey Minyardc69c3122006-09-30 23:27:56 -07003282 unsigned char chan;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003283 ipmi_user_t user = NULL;
3284 struct ipmi_lan_addr *lan_addr;
3285 struct ipmi_recv_msg *recv_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
3287 if (msg->rsp_size < 12) {
3288 /* Message not big enough, just ignore it. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003289 ipmi_inc_stat(intf, invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 return 0;
3291 }
3292
3293 if (msg->rsp[2] != 0) {
3294 /* An error getting the response, just ignore it. */
3295 return 0;
3296 }
3297
3298 netfn = msg->rsp[6] >> 2;
3299 cmd = msg->rsp[10];
Corey Minyardc69c3122006-09-30 23:27:56 -07003300 chan = msg->rsp[3] & 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301
Corey Minyarde61fb5b2005-11-07 01:00:05 -08003302 rcu_read_lock();
Corey Minyardc69c3122006-09-30 23:27:56 -07003303 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003304 if (rcvr) {
3305 user = rcvr->user;
3306 kref_get(&user->refcount);
3307 } else
3308 user = NULL;
Corey Minyarde61fb5b2005-11-07 01:00:05 -08003309 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
3311 if (user == NULL) {
Corey Minyard393d2cc2005-11-07 00:59:54 -08003312 /* We didn't find a user, just give up. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003313 ipmi_inc_stat(intf, unhandled_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
Corey Minyardc70d7492008-04-29 01:01:09 -07003315 /*
3316 * Don't do anything with these messages, just allow
3317 * them to be freed.
3318 */
3319 rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 } else {
3321 /* Deliver the message to the user. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003322 ipmi_inc_stat(intf, handled_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323
3324 recv_msg = ipmi_alloc_recv_msg();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003325 if (!recv_msg) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003326 /*
3327 * We couldn't allocate memory for the
3328 * message, so requeue it for handling later.
3329 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 rv = 1;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003331 kref_put(&user->refcount, free_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 } else {
3333 /* Extract the source address from the data. */
3334 lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
3335 lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
3336 lan_addr->session_handle = msg->rsp[4];
3337 lan_addr->remote_SWID = msg->rsp[8];
3338 lan_addr->local_SWID = msg->rsp[5];
3339 lan_addr->lun = msg->rsp[9] & 3;
3340 lan_addr->channel = msg->rsp[3] & 0xf;
3341 lan_addr->privilege = msg->rsp[3] >> 4;
3342
Corey Minyardc70d7492008-04-29 01:01:09 -07003343 /*
3344 * Extract the rest of the message information
3345 * from the IPMB header.
3346 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 recv_msg->user = user;
3348 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3349 recv_msg->msgid = msg->rsp[9] >> 2;
3350 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3351 recv_msg->msg.cmd = msg->rsp[10];
3352 recv_msg->msg.data = recv_msg->msg_data;
3353
Corey Minyardc70d7492008-04-29 01:01:09 -07003354 /*
3355 * We chop off 12, not 11 bytes because the checksum
3356 * at the end also needs to be removed.
3357 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 recv_msg->msg.data_len = msg->rsp_size - 12;
3359 memcpy(recv_msg->msg_data,
3360 &(msg->rsp[11]),
3361 msg->rsp_size - 12);
3362 deliver_response(recv_msg);
3363 }
3364 }
3365
3366 return rv;
3367}
3368
dann frazier4dec3022009-04-21 12:24:05 -07003369/*
3370 * This routine will handle "Get Message" command responses with
3371 * channels that use an OEM Medium. The message format belongs to
3372 * the OEM. See IPMI 2.0 specification, Chapter 6 and
3373 * Chapter 22, sections 22.6 and 22.24 for more details.
3374 */
3375static int handle_oem_get_msg_cmd(ipmi_smi_t intf,
3376 struct ipmi_smi_msg *msg)
3377{
3378 struct cmd_rcvr *rcvr;
3379 int rv = 0;
3380 unsigned char netfn;
3381 unsigned char cmd;
3382 unsigned char chan;
3383 ipmi_user_t user = NULL;
3384 struct ipmi_system_interface_addr *smi_addr;
3385 struct ipmi_recv_msg *recv_msg;
3386
3387 /*
3388 * We expect the OEM SW to perform error checking
3389 * so we just do some basic sanity checks
3390 */
3391 if (msg->rsp_size < 4) {
3392 /* Message not big enough, just ignore it. */
3393 ipmi_inc_stat(intf, invalid_commands);
3394 return 0;
3395 }
3396
3397 if (msg->rsp[2] != 0) {
3398 /* An error getting the response, just ignore it. */
3399 return 0;
3400 }
3401
3402 /*
3403 * This is an OEM Message so the OEM needs to know how
3404 * handle the message. We do no interpretation.
3405 */
3406 netfn = msg->rsp[0] >> 2;
3407 cmd = msg->rsp[1];
3408 chan = msg->rsp[3] & 0xf;
3409
3410 rcu_read_lock();
3411 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3412 if (rcvr) {
3413 user = rcvr->user;
3414 kref_get(&user->refcount);
3415 } else
3416 user = NULL;
3417 rcu_read_unlock();
3418
3419 if (user == NULL) {
3420 /* We didn't find a user, just give up. */
3421 ipmi_inc_stat(intf, unhandled_commands);
3422
3423 /*
3424 * Don't do anything with these messages, just allow
3425 * them to be freed.
3426 */
3427
3428 rv = 0;
3429 } else {
3430 /* Deliver the message to the user. */
3431 ipmi_inc_stat(intf, handled_commands);
3432
3433 recv_msg = ipmi_alloc_recv_msg();
3434 if (!recv_msg) {
3435 /*
3436 * We couldn't allocate memory for the
3437 * message, so requeue it for handling
3438 * later.
3439 */
3440 rv = 1;
3441 kref_put(&user->refcount, free_user);
3442 } else {
3443 /*
3444 * OEM Messages are expected to be delivered via
3445 * the system interface to SMS software. We might
3446 * need to visit this again depending on OEM
3447 * requirements
3448 */
3449 smi_addr = ((struct ipmi_system_interface_addr *)
3450 &(recv_msg->addr));
3451 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3452 smi_addr->channel = IPMI_BMC_CHANNEL;
3453 smi_addr->lun = msg->rsp[0] & 3;
3454
3455 recv_msg->user = user;
3456 recv_msg->user_msg_data = NULL;
3457 recv_msg->recv_type = IPMI_OEM_RECV_TYPE;
3458 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3459 recv_msg->msg.cmd = msg->rsp[1];
3460 recv_msg->msg.data = recv_msg->msg_data;
3461
3462 /*
3463 * The message starts at byte 4 which follows the
3464 * the Channel Byte in the "GET MESSAGE" command
3465 */
3466 recv_msg->msg.data_len = msg->rsp_size - 4;
3467 memcpy(recv_msg->msg_data,
3468 &(msg->rsp[4]),
3469 msg->rsp_size - 4);
3470 deliver_response(recv_msg);
3471 }
3472 }
3473
3474 return rv;
3475}
3476
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
3478 struct ipmi_smi_msg *msg)
3479{
3480 struct ipmi_system_interface_addr *smi_addr;
Corey Minyardc70d7492008-04-29 01:01:09 -07003481
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 recv_msg->msgid = 0;
3483 smi_addr = (struct ipmi_system_interface_addr *) &(recv_msg->addr);
3484 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3485 smi_addr->channel = IPMI_BMC_CHANNEL;
3486 smi_addr->lun = msg->rsp[0] & 3;
3487 recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
3488 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3489 recv_msg->msg.cmd = msg->rsp[1];
3490 memcpy(recv_msg->msg_data, &(msg->rsp[3]), msg->rsp_size - 3);
3491 recv_msg->msg.data = recv_msg->msg_data;
3492 recv_msg->msg.data_len = msg->rsp_size - 3;
3493}
3494
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495static int handle_read_event_rsp(ipmi_smi_t intf,
3496 struct ipmi_smi_msg *msg)
3497{
3498 struct ipmi_recv_msg *recv_msg, *recv_msg2;
3499 struct list_head msgs;
3500 ipmi_user_t user;
3501 int rv = 0;
3502 int deliver_count = 0;
3503 unsigned long flags;
3504
3505 if (msg->rsp_size < 19) {
3506 /* Message is too small to be an IPMB event. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003507 ipmi_inc_stat(intf, invalid_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 return 0;
3509 }
3510
3511 if (msg->rsp[2] != 0) {
3512 /* An error getting the event, just ignore it. */
3513 return 0;
3514 }
3515
3516 INIT_LIST_HEAD(&msgs);
3517
Corey Minyard393d2cc2005-11-07 00:59:54 -08003518 spin_lock_irqsave(&intf->events_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003520 ipmi_inc_stat(intf, events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521
Corey Minyardc70d7492008-04-29 01:01:09 -07003522 /*
3523 * Allocate and fill in one message for every user that is
3524 * getting events.
3525 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08003526 rcu_read_lock();
3527 list_for_each_entry_rcu(user, &intf->users, link) {
Corey Minyard8a3628d2006-03-31 02:30:40 -08003528 if (!user->gets_events)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 continue;
3530
3531 recv_msg = ipmi_alloc_recv_msg();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003532 if (!recv_msg) {
Corey Minyard393d2cc2005-11-07 00:59:54 -08003533 rcu_read_unlock();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003534 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs,
3535 link) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 list_del(&recv_msg->link);
3537 ipmi_free_recv_msg(recv_msg);
3538 }
Corey Minyardc70d7492008-04-29 01:01:09 -07003539 /*
3540 * We couldn't allocate memory for the
3541 * message, so requeue it for handling
3542 * later.
3543 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 rv = 1;
3545 goto out;
3546 }
3547
3548 deliver_count++;
3549
3550 copy_event_into_recv_msg(recv_msg, msg);
3551 recv_msg->user = user;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003552 kref_get(&user->refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 list_add_tail(&(recv_msg->link), &msgs);
3554 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08003555 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
3557 if (deliver_count) {
3558 /* Now deliver all the messages. */
3559 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
3560 list_del(&recv_msg->link);
3561 deliver_response(recv_msg);
3562 }
3563 } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003564 /*
3565 * No one to receive the message, put it in queue if there's
3566 * not already too many things in the queue.
3567 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 recv_msg = ipmi_alloc_recv_msg();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003569 if (!recv_msg) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003570 /*
3571 * We couldn't allocate memory for the
3572 * message, so requeue it for handling
3573 * later.
3574 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 rv = 1;
3576 goto out;
3577 }
3578
3579 copy_event_into_recv_msg(recv_msg, msg);
3580 list_add_tail(&(recv_msg->link), &(intf->waiting_events));
Corey Minyard4791c032006-04-10 22:54:31 -07003581 intf->waiting_events_count++;
Corey Minyard87ebd062008-04-29 01:01:04 -07003582 } else if (!intf->event_msg_printed) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003583 /*
3584 * There's too many things in the queue, discard this
3585 * message.
3586 */
Corey Minyard87ebd062008-04-29 01:01:04 -07003587 printk(KERN_WARNING PFX "Event queue full, discarding"
3588 " incoming events\n");
3589 intf->event_msg_printed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 }
3591
3592 out:
3593 spin_unlock_irqrestore(&(intf->events_lock), flags);
3594
3595 return rv;
3596}
3597
3598static int handle_bmc_rsp(ipmi_smi_t intf,
3599 struct ipmi_smi_msg *msg)
3600{
3601 struct ipmi_recv_msg *recv_msg;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003602 struct ipmi_user *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603
3604 recv_msg = (struct ipmi_recv_msg *) msg->user_data;
Corey Minyardc70d7492008-04-29 01:01:09 -07003605 if (recv_msg == NULL) {
3606 printk(KERN_WARNING
3607 "IPMI message received with no owner. This\n"
3608 "could be because of a malformed message, or\n"
3609 "because of a hardware error. Contact your\n"
3610 "hardware vender for assistance\n");
Corey Minyard56a55ec2005-09-06 15:18:42 -07003611 return 0;
3612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
Corey Minyard393d2cc2005-11-07 00:59:54 -08003614 user = recv_msg->user;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 /* Make sure the user still exists. */
Corey Minyard393d2cc2005-11-07 00:59:54 -08003616 if (user && !user->valid) {
Corey Minyard56a55ec2005-09-06 15:18:42 -07003617 /* The user for the message went away, so give up. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003618 ipmi_inc_stat(intf, unhandled_local_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 ipmi_free_recv_msg(recv_msg);
3620 } else {
3621 struct ipmi_system_interface_addr *smi_addr;
3622
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003623 ipmi_inc_stat(intf, handled_local_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3625 recv_msg->msgid = msg->msgid;
3626 smi_addr = ((struct ipmi_system_interface_addr *)
3627 &(recv_msg->addr));
3628 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3629 smi_addr->channel = IPMI_BMC_CHANNEL;
3630 smi_addr->lun = msg->rsp[0] & 3;
3631 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3632 recv_msg->msg.cmd = msg->rsp[1];
3633 memcpy(recv_msg->msg_data,
3634 &(msg->rsp[2]),
3635 msg->rsp_size - 2);
3636 recv_msg->msg.data = recv_msg->msg_data;
3637 recv_msg->msg.data_len = msg->rsp_size - 2;
3638 deliver_response(recv_msg);
3639 }
3640
3641 return 0;
3642}
3643
Corey Minyardc70d7492008-04-29 01:01:09 -07003644/*
Corey Minyard7adf5792012-03-28 14:42:49 -07003645 * Handle a received message. Return 1 if the message should be requeued,
Corey Minyardc70d7492008-04-29 01:01:09 -07003646 * 0 if the message should be freed, or -1 if the message should not
3647 * be freed or requeued.
3648 */
Corey Minyard7adf5792012-03-28 14:42:49 -07003649static int handle_one_recv_msg(ipmi_smi_t intf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 struct ipmi_smi_msg *msg)
3651{
3652 int requeue;
3653 int chan;
3654
3655#ifdef DEBUG_MSGING
3656 int m;
3657 printk("Recv:");
Corey Minyarde8b33612005-09-06 15:18:45 -07003658 for (m = 0; m < msg->rsp_size; m++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 printk(" %2.2x", msg->rsp[m]);
3660 printk("\n");
3661#endif
3662 if (msg->rsp_size < 2) {
3663 /* Message is too small to be correct. */
3664 printk(KERN_WARNING PFX "BMC returned to small a message"
3665 " for netfn %x cmd %x, got %d bytes\n",
3666 (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size);
3667
3668 /* Generate an error response for the message. */
3669 msg->rsp[0] = msg->data[0] | (1 << 2);
3670 msg->rsp[1] = msg->data[1];
3671 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
3672 msg->rsp_size = 3;
Corey Minyardc70d7492008-04-29 01:01:09 -07003673 } else if (((msg->rsp[0] >> 2) != ((msg->data[0] >> 2) | 1))
3674 || (msg->rsp[1] != msg->data[1])) {
3675 /*
3676 * The NetFN and Command in the response is not even
3677 * marginally correct.
3678 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 printk(KERN_WARNING PFX "BMC returned incorrect response,"
3680 " expected netfn %x cmd %x, got netfn %x cmd %x\n",
3681 (msg->data[0] >> 2) | 1, msg->data[1],
3682 msg->rsp[0] >> 2, msg->rsp[1]);
3683
3684 /* Generate an error response for the message. */
3685 msg->rsp[0] = msg->data[0] | (1 << 2);
3686 msg->rsp[1] = msg->data[1];
3687 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
3688 msg->rsp_size = 3;
3689 }
3690
3691 if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
3692 && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07003693 && (msg->user_data != NULL)) {
3694 /*
3695 * It's a response to a response we sent. For this we
3696 * deliver a send message response to the user.
3697 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08003698 struct ipmi_recv_msg *recv_msg = msg->user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699
3700 requeue = 0;
3701 if (msg->rsp_size < 2)
3702 /* Message is too small to be correct. */
3703 goto out;
3704
3705 chan = msg->data[2] & 0x0f;
3706 if (chan >= IPMI_MAX_CHANNELS)
3707 /* Invalid channel number */
3708 goto out;
3709
Corey Minyard393d2cc2005-11-07 00:59:54 -08003710 if (!recv_msg)
3711 goto out;
3712
3713 /* Make sure the user still exists. */
3714 if (!recv_msg->user || !recv_msg->user->valid)
3715 goto out;
3716
3717 recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
3718 recv_msg->msg.data = recv_msg->msg_data;
3719 recv_msg->msg.data_len = 1;
3720 recv_msg->msg_data[0] = msg->rsp[2];
3721 deliver_response(recv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
Corey Minyardc70d7492008-04-29 01:01:09 -07003723 && (msg->rsp[1] == IPMI_GET_MSG_CMD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 /* It's from the receive queue. */
3725 chan = msg->rsp[3] & 0xf;
3726 if (chan >= IPMI_MAX_CHANNELS) {
3727 /* Invalid channel number */
3728 requeue = 0;
3729 goto out;
3730 }
3731
dann frazier4dec3022009-04-21 12:24:05 -07003732 /*
Corey Minyard9a2845c2009-05-20 13:36:17 -05003733 * We need to make sure the channels have been initialized.
3734 * The channel_handler routine will set the "curr_channel"
3735 * equal to or greater than IPMI_MAX_CHANNELS when all the
3736 * channels for this interface have been initialized.
3737 */
dann frazier4dec3022009-04-21 12:24:05 -07003738 if (intf->curr_channel < IPMI_MAX_CHANNELS) {
Corey Minyard9a2845c2009-05-20 13:36:17 -05003739 requeue = 0; /* Throw the message away */
dann frazier4dec3022009-04-21 12:24:05 -07003740 goto out;
3741 }
3742
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 switch (intf->channels[chan].medium) {
3744 case IPMI_CHANNEL_MEDIUM_IPMB:
3745 if (msg->rsp[4] & 0x04) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003746 /*
3747 * It's a response, so find the
3748 * requesting message and send it up.
3749 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 requeue = handle_ipmb_get_msg_rsp(intf, msg);
3751 } else {
Corey Minyardc70d7492008-04-29 01:01:09 -07003752 /*
3753 * It's a command to the SMS from some other
3754 * entity. Handle that.
3755 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 requeue = handle_ipmb_get_msg_cmd(intf, msg);
3757 }
3758 break;
3759
3760 case IPMI_CHANNEL_MEDIUM_8023LAN:
3761 case IPMI_CHANNEL_MEDIUM_ASYNC:
3762 if (msg->rsp[6] & 0x04) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003763 /*
3764 * It's a response, so find the
3765 * requesting message and send it up.
3766 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 requeue = handle_lan_get_msg_rsp(intf, msg);
3768 } else {
Corey Minyardc70d7492008-04-29 01:01:09 -07003769 /*
3770 * It's a command to the SMS from some other
3771 * entity. Handle that.
3772 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 requeue = handle_lan_get_msg_cmd(intf, msg);
3774 }
3775 break;
3776
3777 default:
dann frazier4dec3022009-04-21 12:24:05 -07003778 /* Check for OEM Channels. Clients had better
3779 register for these commands. */
3780 if ((intf->channels[chan].medium
3781 >= IPMI_CHANNEL_MEDIUM_OEM_MIN)
3782 && (intf->channels[chan].medium
3783 <= IPMI_CHANNEL_MEDIUM_OEM_MAX)) {
3784 requeue = handle_oem_get_msg_cmd(intf, msg);
3785 } else {
3786 /*
3787 * We don't handle the channel type, so just
3788 * free the message.
3789 */
3790 requeue = 0;
3791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 }
3793
3794 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
Corey Minyardc70d7492008-04-29 01:01:09 -07003795 && (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD)) {
Adam Buchbinderb3834be2012-09-19 21:48:02 -04003796 /* It's an asynchronous event. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 requeue = handle_read_event_rsp(intf, msg);
3798 } else {
3799 /* It's a response from the local BMC. */
3800 requeue = handle_bmc_rsp(intf, msg);
3801 }
3802
3803 out:
3804 return requeue;
3805}
3806
Corey Minyard7adf5792012-03-28 14:42:49 -07003807/*
3808 * If there are messages in the queue or pretimeouts, handle them.
3809 */
3810static void handle_new_recv_msgs(ipmi_smi_t intf)
3811{
3812 struct ipmi_smi_msg *smi_msg;
3813 unsigned long flags = 0;
3814 int rv;
3815 int run_to_completion = intf->run_to_completion;
3816
3817 /* See if any waiting messages need to be processed. */
3818 if (!run_to_completion)
Corey Minyard65be7542014-11-06 20:48:02 -06003819 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
3820 while (!list_empty(&intf->waiting_rcv_msgs)) {
3821 smi_msg = list_entry(intf->waiting_rcv_msgs.next,
Corey Minyard7adf5792012-03-28 14:42:49 -07003822 struct ipmi_smi_msg, link);
Junichi Nomuraae4ea9a2016-06-10 04:31:52 +00003823 list_del(&smi_msg->link);
Corey Minyard7adf5792012-03-28 14:42:49 -07003824 if (!run_to_completion)
Corey Minyard65be7542014-11-06 20:48:02 -06003825 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
3826 flags);
Corey Minyard7adf5792012-03-28 14:42:49 -07003827 rv = handle_one_recv_msg(intf, smi_msg);
3828 if (!run_to_completion)
Corey Minyard65be7542014-11-06 20:48:02 -06003829 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003830 if (rv > 0) {
Corey Minyard7adf5792012-03-28 14:42:49 -07003831 /*
3832 * To preserve message order, quit if we
Junichi Nomuraae4ea9a2016-06-10 04:31:52 +00003833 * can't handle a message. Add the message
3834 * back at the head, this is safe because this
3835 * tasklet is the only thing that pulls the
3836 * messages.
Corey Minyard7adf5792012-03-28 14:42:49 -07003837 */
Junichi Nomuraae4ea9a2016-06-10 04:31:52 +00003838 list_add(&smi_msg->link, &intf->waiting_rcv_msgs);
Corey Minyard7adf5792012-03-28 14:42:49 -07003839 break;
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003840 } else {
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003841 if (rv == 0)
3842 /* Message handled */
3843 ipmi_free_smi_msg(smi_msg);
3844 /* If rv < 0, fatal error, del but don't free. */
Corey Minyard7adf5792012-03-28 14:42:49 -07003845 }
3846 }
3847 if (!run_to_completion)
Corey Minyard65be7542014-11-06 20:48:02 -06003848 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock, flags);
Corey Minyard7adf5792012-03-28 14:42:49 -07003849
3850 /*
3851 * If the pretimout count is non-zero, decrement one from it and
3852 * deliver pretimeouts to all the users.
3853 */
3854 if (atomic_add_unless(&intf->watchdog_pretimeouts_to_deliver, -1, 0)) {
3855 ipmi_user_t user;
3856
3857 rcu_read_lock();
3858 list_for_each_entry_rcu(user, &intf->users, link) {
3859 if (user->handler->ipmi_watchdog_pretimeout)
3860 user->handler->ipmi_watchdog_pretimeout(
3861 user->handler_data);
3862 }
3863 rcu_read_unlock();
3864 }
3865}
3866
3867static void smi_recv_tasklet(unsigned long val)
3868{
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003869 unsigned long flags = 0; /* keep us warning-free. */
3870 ipmi_smi_t intf = (ipmi_smi_t) val;
3871 int run_to_completion = intf->run_to_completion;
3872 struct ipmi_smi_msg *newmsg = NULL;
3873
3874 /*
3875 * Start the next message if available.
3876 *
3877 * Do this here, not in the actual receiver, because we may deadlock
3878 * because the lower layer is allowed to hold locks while calling
3879 * message delivery.
3880 */
3881 if (!run_to_completion)
3882 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
3883 if (intf->curr_msg == NULL && !intf->in_shutdown) {
3884 struct list_head *entry = NULL;
3885
3886 /* Pick the high priority queue first. */
3887 if (!list_empty(&intf->hp_xmit_msgs))
3888 entry = intf->hp_xmit_msgs.next;
3889 else if (!list_empty(&intf->xmit_msgs))
3890 entry = intf->xmit_msgs.next;
3891
3892 if (entry) {
3893 list_del(entry);
3894 newmsg = list_entry(entry, struct ipmi_smi_msg, link);
3895 intf->curr_msg = newmsg;
3896 }
3897 }
3898 if (!run_to_completion)
3899 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
3900 if (newmsg)
Corey Minyard99ab32f2014-11-07 07:57:31 -06003901 intf->handlers->sender(intf->send_info, newmsg);
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003902
3903 handle_new_recv_msgs(intf);
Corey Minyard7adf5792012-03-28 14:42:49 -07003904}
3905
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906/* Handle a new message from the lower layer. */
3907void ipmi_smi_msg_received(ipmi_smi_t intf,
3908 struct ipmi_smi_msg *msg)
3909{
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003910 unsigned long flags = 0; /* keep us warning-free. */
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003911 int run_to_completion = intf->run_to_completion;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 if ((msg->data_size >= 2)
3914 && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
3915 && (msg->data[1] == IPMI_SEND_MSG_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07003916 && (msg->user_data == NULL)) {
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003917
3918 if (intf->in_shutdown)
3919 goto free_msg;
3920
Corey Minyardc70d7492008-04-29 01:01:09 -07003921 /*
3922 * This is the local response to a command send, start
3923 * the timer for these. The user_data will not be
3924 * NULL if this is a response send, and we will let
3925 * response sends just go through.
3926 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927
Corey Minyardc70d7492008-04-29 01:01:09 -07003928 /*
3929 * Check for errors, if we get certain errors (ones
3930 * that mean basically we can try again later), we
3931 * ignore them and start the timer. Otherwise we
3932 * report the error immediately.
3933 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
3935 && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
Corey Minyard46d52b02006-11-08 17:44:55 -08003936 && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR)
3937 && (msg->rsp[2] != IPMI_BUS_ERR)
Corey Minyardc70d7492008-04-29 01:01:09 -07003938 && (msg->rsp[2] != IPMI_NAK_ON_WRITE_ERR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 int chan = msg->rsp[3] & 0xf;
3940
3941 /* Got an error sending the message, handle it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 if (chan >= IPMI_MAX_CHANNELS)
3943 ; /* This shouldn't happen */
3944 else if ((intf->channels[chan].medium
3945 == IPMI_CHANNEL_MEDIUM_8023LAN)
3946 || (intf->channels[chan].medium
3947 == IPMI_CHANNEL_MEDIUM_ASYNC))
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003948 ipmi_inc_stat(intf, sent_lan_command_errs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 else
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003950 ipmi_inc_stat(intf, sent_ipmb_command_errs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 intf_err_seq(intf, msg->msgid, msg->rsp[2]);
Corey Minyardc70d7492008-04-29 01:01:09 -07003952 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 /* The message was sent, start the timer. */
3954 intf_start_seq_timer(intf, msg->msgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003956free_msg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 ipmi_free_smi_msg(msg);
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003958 } else {
3959 /*
3960 * To preserve message order, we keep a queue and deliver from
3961 * a tasklet.
3962 */
3963 if (!run_to_completion)
3964 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
3965 list_add_tail(&msg->link, &intf->waiting_rcv_msgs);
3966 if (!run_to_completion)
3967 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
3968 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 }
3970
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003971 if (!run_to_completion)
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003972 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
Corey Minyardb2234ee2015-02-19 11:29:24 -06003973 /*
3974 * We can get an asynchronous event or receive message in addition
3975 * to commands we send.
3976 */
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003977 if (msg == intf->curr_msg)
3978 intf->curr_msg = NULL;
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003979 if (!run_to_completion)
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003980 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
Corey Minyardc70d7492008-04-29 01:01:09 -07003981
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003982 if (run_to_completion)
3983 smi_recv_tasklet((unsigned long) intf);
3984 else
3985 tasklet_schedule(&intf->recv_tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986}
Corey Minyardc70d7492008-04-29 01:01:09 -07003987EXPORT_SYMBOL(ipmi_smi_msg_received);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988
3989void ipmi_smi_watchdog_pretimeout(ipmi_smi_t intf)
3990{
Corey Minyard7ea0ed22014-11-06 16:58:48 -06003991 if (intf->in_shutdown)
3992 return;
3993
Corey Minyard7adf5792012-03-28 14:42:49 -07003994 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 1);
3995 tasklet_schedule(&intf->recv_tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996}
Corey Minyardc70d7492008-04-29 01:01:09 -07003997EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998
Corey Minyard882fe012005-05-01 08:59:12 -07003999static struct ipmi_smi_msg *
4000smi_from_recv_msg(ipmi_smi_t intf, struct ipmi_recv_msg *recv_msg,
4001 unsigned char seq, long seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002{
Corey Minyard882fe012005-05-01 08:59:12 -07004003 struct ipmi_smi_msg *smi_msg = ipmi_alloc_smi_msg();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 if (!smi_msg)
Corey Minyardc70d7492008-04-29 01:01:09 -07004005 /*
4006 * If we can't allocate the message, then just return, we
4007 * get 4 retries, so this should be ok.
4008 */
Corey Minyard882fe012005-05-01 08:59:12 -07004009 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010
4011 memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
4012 smi_msg->data_size = recv_msg->msg.data_len;
4013 smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
Corey Minyardc70d7492008-04-29 01:01:09 -07004014
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015#ifdef DEBUG_MSGING
4016 {
4017 int m;
4018 printk("Resend: ");
Corey Minyarde8b33612005-09-06 15:18:45 -07004019 for (m = 0; m < smi_msg->data_size; m++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 printk(" %2.2x", smi_msg->data[m]);
4021 printk("\n");
4022 }
4023#endif
Corey Minyard882fe012005-05-01 08:59:12 -07004024 return smi_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025}
4026
Corey Minyard393d2cc2005-11-07 00:59:54 -08004027static void check_msg_timeout(ipmi_smi_t intf, struct seq_table *ent,
4028 struct list_head *timeouts, long timeout_period,
Corey Minyard89986492014-04-14 09:46:54 -05004029 int slot, unsigned long *flags,
4030 unsigned int *waiting_msgs)
Corey Minyard393d2cc2005-11-07 00:59:54 -08004031{
Corey Minyardb2c03942006-12-06 20:41:00 -08004032 struct ipmi_recv_msg *msg;
Corey Minyard81d02b72015-06-13 10:34:25 -05004033 const struct ipmi_smi_handlers *handlers;
Corey Minyardb2c03942006-12-06 20:41:00 -08004034
Corey Minyard7ea0ed22014-11-06 16:58:48 -06004035 if (intf->in_shutdown)
Corey Minyardb2c03942006-12-06 20:41:00 -08004036 return;
Corey Minyard393d2cc2005-11-07 00:59:54 -08004037
4038 if (!ent->inuse)
4039 return;
4040
4041 ent->timeout -= timeout_period;
Corey Minyard89986492014-04-14 09:46:54 -05004042 if (ent->timeout > 0) {
4043 (*waiting_msgs)++;
Corey Minyard393d2cc2005-11-07 00:59:54 -08004044 return;
Corey Minyard89986492014-04-14 09:46:54 -05004045 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08004046
4047 if (ent->retries_left == 0) {
4048 /* The message has used all its retries. */
4049 ent->inuse = 0;
4050 msg = ent->recv_msg;
4051 list_add_tail(&msg->link, timeouts);
Corey Minyard393d2cc2005-11-07 00:59:54 -08004052 if (ent->broadcast)
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07004053 ipmi_inc_stat(intf, timed_out_ipmb_broadcasts);
Corey Minyard25176ed2009-04-21 12:24:04 -07004054 else if (is_lan_addr(&ent->recv_msg->addr))
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07004055 ipmi_inc_stat(intf, timed_out_lan_commands);
Corey Minyard393d2cc2005-11-07 00:59:54 -08004056 else
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07004057 ipmi_inc_stat(intf, timed_out_ipmb_commands);
Corey Minyard393d2cc2005-11-07 00:59:54 -08004058 } else {
4059 struct ipmi_smi_msg *smi_msg;
4060 /* More retries, send again. */
4061
Corey Minyard89986492014-04-14 09:46:54 -05004062 (*waiting_msgs)++;
4063
Corey Minyardc70d7492008-04-29 01:01:09 -07004064 /*
4065 * Start with the max timer, set to normal timer after
4066 * the message is sent.
4067 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08004068 ent->timeout = MAX_MSG_TIMEOUT;
4069 ent->retries_left--;
Corey Minyard393d2cc2005-11-07 00:59:54 -08004070 smi_msg = smi_from_recv_msg(intf, ent->recv_msg, slot,
4071 ent->seqid);
Corey Minyard25176ed2009-04-21 12:24:04 -07004072 if (!smi_msg) {
4073 if (is_lan_addr(&ent->recv_msg->addr))
4074 ipmi_inc_stat(intf,
4075 dropped_rexmit_lan_commands);
4076 else
4077 ipmi_inc_stat(intf,
4078 dropped_rexmit_ipmb_commands);
Corey Minyard393d2cc2005-11-07 00:59:54 -08004079 return;
Corey Minyard25176ed2009-04-21 12:24:04 -07004080 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08004081
4082 spin_unlock_irqrestore(&intf->seq_lock, *flags);
Corey Minyardb2c03942006-12-06 20:41:00 -08004083
Corey Minyardc70d7492008-04-29 01:01:09 -07004084 /*
4085 * Send the new message. We send with a zero
4086 * priority. It timed out, I doubt time is that
4087 * critical now, and high priority messages are really
4088 * only for messages to the local MC, which don't get
4089 * resent.
4090 */
Corey Minyardb2c03942006-12-06 20:41:00 -08004091 handlers = intf->handlers;
Corey Minyard25176ed2009-04-21 12:24:04 -07004092 if (handlers) {
4093 if (is_lan_addr(&ent->recv_msg->addr))
4094 ipmi_inc_stat(intf,
4095 retransmitted_lan_commands);
4096 else
4097 ipmi_inc_stat(intf,
4098 retransmitted_ipmb_commands);
4099
Corey Minyard81d02b72015-06-13 10:34:25 -05004100 smi_send(intf, handlers, smi_msg, 0);
Corey Minyard25176ed2009-04-21 12:24:04 -07004101 } else
Corey Minyardb2c03942006-12-06 20:41:00 -08004102 ipmi_free_smi_msg(smi_msg);
4103
Corey Minyard393d2cc2005-11-07 00:59:54 -08004104 spin_lock_irqsave(&intf->seq_lock, *flags);
4105 }
4106}
4107
Corey Minyard89986492014-04-14 09:46:54 -05004108static unsigned int ipmi_timeout_handler(ipmi_smi_t intf, long timeout_period)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 struct list_head timeouts;
4111 struct ipmi_recv_msg *msg, *msg2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 unsigned long flags;
Corey Minyardbca03242006-12-06 20:40:57 -08004113 int i;
Corey Minyard89986492014-04-14 09:46:54 -05004114 unsigned int waiting_msgs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115
Corey Minyard89986492014-04-14 09:46:54 -05004116 /*
4117 * Go through the seq table and find any messages that
4118 * have timed out, putting them in the timeouts
4119 * list.
4120 */
4121 INIT_LIST_HEAD(&timeouts);
4122 spin_lock_irqsave(&intf->seq_lock, flags);
4123 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++)
4124 check_msg_timeout(intf, &(intf->seq_table[i]),
4125 &timeouts, timeout_period, i,
4126 &flags, &waiting_msgs);
4127 spin_unlock_irqrestore(&intf->seq_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128
Corey Minyard89986492014-04-14 09:46:54 -05004129 list_for_each_entry_safe(msg, msg2, &timeouts, link)
4130 deliver_err_response(msg, IPMI_TIMEOUT_COMPLETION_CODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131
Corey Minyard89986492014-04-14 09:46:54 -05004132 /*
4133 * Maintenance mode handling. Check the timeout
4134 * optimistically before we claim the lock. It may
4135 * mean a timeout gets missed occasionally, but that
4136 * only means the timeout gets extended by one period
4137 * in that case. No big deal, and it avoids the lock
4138 * most of the time.
4139 */
4140 if (intf->auto_maintenance_timeout > 0) {
4141 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
Corey Minyardb9675132006-12-06 20:41:02 -08004142 if (intf->auto_maintenance_timeout > 0) {
Corey Minyard89986492014-04-14 09:46:54 -05004143 intf->auto_maintenance_timeout
4144 -= timeout_period;
4145 if (!intf->maintenance_mode
4146 && (intf->auto_maintenance_timeout <= 0)) {
Corey Minyard7aefac22014-04-14 09:46:56 -05004147 intf->maintenance_mode_enable = false;
Corey Minyard89986492014-04-14 09:46:54 -05004148 maintenance_mode_update(intf);
Corey Minyardb9675132006-12-06 20:41:02 -08004149 }
Corey Minyardb9675132006-12-06 20:41:02 -08004150 }
Corey Minyard89986492014-04-14 09:46:54 -05004151 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
4152 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 }
Corey Minyard89986492014-04-14 09:46:54 -05004154
4155 tasklet_schedule(&intf->recv_tasklet);
4156
4157 return waiting_msgs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158}
4159
Corey Minyard89986492014-04-14 09:46:54 -05004160static void ipmi_request_event(ipmi_smi_t intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161{
Corey Minyard89986492014-04-14 09:46:54 -05004162 /* No event requests when in maintenance mode. */
4163 if (intf->maintenance_mode_enable)
4164 return;
Corey Minyardb9675132006-12-06 20:41:02 -08004165
Corey Minyard7ea0ed22014-11-06 16:58:48 -06004166 if (!intf->in_shutdown)
4167 intf->handlers->request_events(intf->send_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168}
4169
4170static struct timer_list ipmi_timer;
4171
Corey Minyard8f43f842005-06-23 22:01:40 -07004172static atomic_t stop_operation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173
4174static void ipmi_timeout(unsigned long data)
4175{
Corey Minyard89986492014-04-14 09:46:54 -05004176 ipmi_smi_t intf;
4177 int nt = 0;
4178
Corey Minyard8f43f842005-06-23 22:01:40 -07004179 if (atomic_read(&stop_operation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181
Corey Minyard89986492014-04-14 09:46:54 -05004182 rcu_read_lock();
4183 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4184 int lnt = 0;
4185
4186 if (atomic_read(&intf->event_waiters)) {
4187 intf->ticks_to_req_ev--;
4188 if (intf->ticks_to_req_ev == 0) {
4189 ipmi_request_event(intf);
4190 intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
4191 }
4192 lnt++;
4193 }
4194
4195 lnt += ipmi_timeout_handler(intf, IPMI_TIMEOUT_TIME);
4196
4197 lnt = !!lnt;
4198 if (lnt != intf->last_needs_timer &&
4199 intf->handlers->set_need_watch)
4200 intf->handlers->set_need_watch(intf->send_info, lnt);
4201 intf->last_needs_timer = lnt;
4202
4203 nt += lnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 }
Corey Minyard89986492014-04-14 09:46:54 -05004205 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206
Corey Minyard89986492014-04-14 09:46:54 -05004207 if (nt)
4208 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209}
4210
Corey Minyard89986492014-04-14 09:46:54 -05004211static void need_waiter(ipmi_smi_t intf)
4212{
4213 /* Racy, but worst case we start the timer twice. */
4214 if (!timer_pending(&ipmi_timer))
4215 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
4216}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217
4218static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
4219static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
4220
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221static void free_smi_msg(struct ipmi_smi_msg *msg)
4222{
4223 atomic_dec(&smi_msg_inuse_count);
4224 kfree(msg);
4225}
4226
4227struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
4228{
4229 struct ipmi_smi_msg *rv;
4230 rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
4231 if (rv) {
4232 rv->done = free_smi_msg;
4233 rv->user_data = NULL;
4234 atomic_inc(&smi_msg_inuse_count);
4235 }
4236 return rv;
4237}
Corey Minyardc70d7492008-04-29 01:01:09 -07004238EXPORT_SYMBOL(ipmi_alloc_smi_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239
4240static void free_recv_msg(struct ipmi_recv_msg *msg)
4241{
4242 atomic_dec(&recv_msg_inuse_count);
4243 kfree(msg);
4244}
4245
Adrian Bunk74006302008-04-29 01:01:14 -07004246static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247{
4248 struct ipmi_recv_msg *rv;
4249
4250 rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
4251 if (rv) {
Corey Minyarda9eec552006-08-31 21:27:45 -07004252 rv->user = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 rv->done = free_recv_msg;
4254 atomic_inc(&recv_msg_inuse_count);
4255 }
4256 return rv;
4257}
4258
Corey Minyard393d2cc2005-11-07 00:59:54 -08004259void ipmi_free_recv_msg(struct ipmi_recv_msg *msg)
4260{
4261 if (msg->user)
4262 kref_put(&msg->user->refcount, free_user);
4263 msg->done(msg);
4264}
Corey Minyardc70d7492008-04-29 01:01:09 -07004265EXPORT_SYMBOL(ipmi_free_recv_msg);
Corey Minyard393d2cc2005-11-07 00:59:54 -08004266
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267#ifdef CONFIG_IPMI_PANIC_EVENT
4268
Corey Minyard895dcfd2012-03-28 14:42:49 -07004269static atomic_t panic_done_count = ATOMIC_INIT(0);
4270
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
4272{
Corey Minyard895dcfd2012-03-28 14:42:49 -07004273 atomic_dec(&panic_done_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274}
4275
4276static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
4277{
Corey Minyard895dcfd2012-03-28 14:42:49 -07004278 atomic_dec(&panic_done_count);
4279}
4280
4281/*
4282 * Inside a panic, send a message and wait for a response.
4283 */
4284static void ipmi_panic_request_and_wait(ipmi_smi_t intf,
4285 struct ipmi_addr *addr,
4286 struct kernel_ipmi_msg *msg)
4287{
4288 struct ipmi_smi_msg smi_msg;
4289 struct ipmi_recv_msg recv_msg;
4290 int rv;
4291
4292 smi_msg.done = dummy_smi_done_handler;
4293 recv_msg.done = dummy_recv_done_handler;
4294 atomic_add(2, &panic_done_count);
4295 rv = i_ipmi_request(NULL,
4296 intf,
4297 addr,
4298 0,
4299 msg,
4300 intf,
4301 &smi_msg,
4302 &recv_msg,
4303 0,
4304 intf->channels[0].address,
4305 intf->channels[0].lun,
4306 0, 1); /* Don't retry, and don't wait. */
4307 if (rv)
4308 atomic_sub(2, &panic_done_count);
Hidehiro Kawai82802f92015-07-27 14:55:16 +09004309 else if (intf->handlers->flush_messages)
4310 intf->handlers->flush_messages(intf->send_info);
4311
Corey Minyard895dcfd2012-03-28 14:42:49 -07004312 while (atomic_read(&panic_done_count) != 0)
4313 ipmi_poll(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314}
4315
4316#ifdef CONFIG_IPMI_PANIC_STRING
Corey Minyard56a55ec2005-09-06 15:18:42 -07004317static void event_receiver_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318{
Corey Minyard56a55ec2005-09-06 15:18:42 -07004319 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4320 && (msg->msg.netfn == IPMI_NETFN_SENSOR_EVENT_RESPONSE)
4321 && (msg->msg.cmd == IPMI_GET_EVENT_RECEIVER_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07004322 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 /* A get event receiver command, save it. */
Corey Minyard56a55ec2005-09-06 15:18:42 -07004324 intf->event_receiver = msg->msg.data[1];
4325 intf->event_receiver_lun = msg->msg.data[2] & 0x3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 }
4327}
4328
Corey Minyard56a55ec2005-09-06 15:18:42 -07004329static void device_id_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330{
Corey Minyard56a55ec2005-09-06 15:18:42 -07004331 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4332 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
4333 && (msg->msg.cmd == IPMI_GET_DEVICE_ID_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07004334 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
4335 /*
4336 * A get device id command, save if we are an event
4337 * receiver or generator.
4338 */
Corey Minyard56a55ec2005-09-06 15:18:42 -07004339 intf->local_sel_device = (msg->msg.data[6] >> 2) & 1;
4340 intf->local_event_generator = (msg->msg.data[6] >> 5) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 }
4342}
4343#endif
4344
4345static void send_panic_events(char *str)
4346{
4347 struct kernel_ipmi_msg msg;
4348 ipmi_smi_t intf;
4349 unsigned char data[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 struct ipmi_system_interface_addr *si;
4351 struct ipmi_addr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352
4353 si = (struct ipmi_system_interface_addr *) &addr;
4354 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4355 si->channel = IPMI_BMC_CHANNEL;
4356 si->lun = 0;
4357
4358 /* Fill in an event telling that we have failed. */
4359 msg.netfn = 0x04; /* Sensor or Event. */
4360 msg.cmd = 2; /* Platform event command. */
4361 msg.data = data;
4362 msg.data_len = 8;
Matt Domschcda315a2005-12-12 00:37:32 -08004363 data[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 data[1] = 0x03; /* This is for IPMI 1.0. */
4365 data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
4366 data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
4367 data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
4368
Corey Minyardc70d7492008-04-29 01:01:09 -07004369 /*
4370 * Put a few breadcrumbs in. Hopefully later we can add more things
4371 * to make the panic events more useful.
4372 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 if (str) {
4374 data[3] = str[0];
4375 data[6] = str[1];
4376 data[7] = str[2];
4377 }
4378
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 /* For every registered interface, send the event. */
Corey Minyardbca03242006-12-06 20:40:57 -08004380 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
Corey Minyardb2c03942006-12-06 20:41:00 -08004381 if (!intf->handlers)
4382 /* Interface is not ready. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 continue;
4384
4385 /* Send the event announcing the panic. */
Corey Minyard895dcfd2012-03-28 14:42:49 -07004386 ipmi_panic_request_and_wait(intf, &addr, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 }
4388
4389#ifdef CONFIG_IPMI_PANIC_STRING
Corey Minyardc70d7492008-04-29 01:01:09 -07004390 /*
4391 * On every interface, dump a bunch of OEM event holding the
4392 * string.
4393 */
4394 if (!str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 return;
4396
Corey Minyardbca03242006-12-06 20:40:57 -08004397 /* For every registered interface, send the event. */
4398 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 char *p = str;
4400 struct ipmi_ipmb_addr *ipmb;
4401 int j;
4402
Corey Minyardbca03242006-12-06 20:40:57 -08004403 if (intf->intf_num == -1)
4404 /* Interface was not ready yet. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 continue;
4406
Corey Minyard78ba2fa2007-02-10 01:45:45 -08004407 /*
4408 * intf_num is used as an marker to tell if the
4409 * interface is valid. Thus we need a read barrier to
4410 * make sure data fetched before checking intf_num
4411 * won't be used.
4412 */
4413 smp_rmb();
4414
Corey Minyardc70d7492008-04-29 01:01:09 -07004415 /*
4416 * First job here is to figure out where to send the
4417 * OEM events. There's no way in IPMI to send OEM
4418 * events using an event send command, so we have to
4419 * find the SEL to put them in and stick them in
4420 * there.
4421 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422
4423 /* Get capabilities from the get device id. */
4424 intf->local_sel_device = 0;
4425 intf->local_event_generator = 0;
4426 intf->event_receiver = 0;
4427
4428 /* Request the device info from the local MC. */
4429 msg.netfn = IPMI_NETFN_APP_REQUEST;
4430 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
4431 msg.data = NULL;
4432 msg.data_len = 0;
4433 intf->null_user_handler = device_id_fetcher;
Corey Minyard895dcfd2012-03-28 14:42:49 -07004434 ipmi_panic_request_and_wait(intf, &addr, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435
4436 if (intf->local_event_generator) {
4437 /* Request the event receiver from the local MC. */
4438 msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
4439 msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
4440 msg.data = NULL;
4441 msg.data_len = 0;
4442 intf->null_user_handler = event_receiver_fetcher;
Corey Minyard895dcfd2012-03-28 14:42:49 -07004443 ipmi_panic_request_and_wait(intf, &addr, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 }
4445 intf->null_user_handler = NULL;
4446
Corey Minyardc70d7492008-04-29 01:01:09 -07004447 /*
4448 * Validate the event receiver. The low bit must not
4449 * be 1 (it must be a valid IPMB address), it cannot
4450 * be zero, and it must not be my address.
4451 */
4452 if (((intf->event_receiver & 1) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 && (intf->event_receiver != 0)
Corey Minyardc70d7492008-04-29 01:01:09 -07004454 && (intf->event_receiver != intf->channels[0].address)) {
4455 /*
4456 * The event receiver is valid, send an IPMB
4457 * message.
4458 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 ipmb = (struct ipmi_ipmb_addr *) &addr;
4460 ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
4461 ipmb->channel = 0; /* FIXME - is this right? */
4462 ipmb->lun = intf->event_receiver_lun;
4463 ipmb->slave_addr = intf->event_receiver;
4464 } else if (intf->local_sel_device) {
Corey Minyardc70d7492008-04-29 01:01:09 -07004465 /*
4466 * The event receiver was not valid (or was
4467 * me), but I am an SEL device, just dump it
4468 * in my SEL.
4469 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 si = (struct ipmi_system_interface_addr *) &addr;
4471 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4472 si->channel = IPMI_BMC_CHANNEL;
4473 si->lun = 0;
4474 } else
4475 continue; /* No where to send the event. */
4476
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
4478 msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
4479 msg.data = data;
4480 msg.data_len = 16;
4481
4482 j = 0;
4483 while (*p) {
4484 int size = strlen(p);
4485
4486 if (size > 11)
4487 size = 11;
4488 data[0] = 0;
4489 data[1] = 0;
4490 data[2] = 0xf0; /* OEM event without timestamp. */
Corey Minyardc14979b2005-09-06 15:18:38 -07004491 data[3] = intf->channels[0].address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 data[4] = j++; /* sequence # */
Corey Minyardc70d7492008-04-29 01:01:09 -07004493 /*
4494 * Always give 11 bytes, so strncpy will fill
4495 * it with zeroes for me.
4496 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 strncpy(data+5, p, 11);
4498 p += size;
4499
Corey Minyard895dcfd2012-03-28 14:42:49 -07004500 ipmi_panic_request_and_wait(intf, &addr, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 }
Corey Minyardc70d7492008-04-29 01:01:09 -07004502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503#endif /* CONFIG_IPMI_PANIC_STRING */
4504}
4505#endif /* CONFIG_IPMI_PANIC_EVENT */
4506
Randy Dunlap0c8204b2006-12-10 02:19:06 -08004507static int has_panicked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508
4509static int panic_event(struct notifier_block *this,
4510 unsigned long event,
Corey Minyardc70d7492008-04-29 01:01:09 -07004511 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 ipmi_smi_t intf;
4514
Lee Revellf18190b2006-06-26 18:30:00 +02004515 if (has_panicked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 return NOTIFY_DONE;
Lee Revellf18190b2006-06-26 18:30:00 +02004517 has_panicked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518
4519 /* For every registered interface, set it to run to completion. */
Corey Minyardbca03242006-12-06 20:40:57 -08004520 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
Corey Minyardb2c03942006-12-06 20:41:00 -08004521 if (!intf->handlers)
4522 /* Interface is not ready. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 continue;
4524
Hidehiro Kawai06e5e342015-07-27 14:55:16 +09004525 /*
4526 * If we were interrupted while locking xmit_msgs_lock or
4527 * waiting_rcv_msgs_lock, the corresponding list may be
4528 * corrupted. In this case, drop items on the list for
4529 * the safety.
4530 */
4531 if (!spin_trylock(&intf->xmit_msgs_lock)) {
4532 INIT_LIST_HEAD(&intf->xmit_msgs);
4533 INIT_LIST_HEAD(&intf->hp_xmit_msgs);
4534 } else
4535 spin_unlock(&intf->xmit_msgs_lock);
4536
4537 if (!spin_trylock(&intf->waiting_rcv_msgs_lock))
4538 INIT_LIST_HEAD(&intf->waiting_rcv_msgs);
4539 else
4540 spin_unlock(&intf->waiting_rcv_msgs_lock);
4541
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07004542 intf->run_to_completion = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 intf->handlers->set_run_to_completion(intf->send_info, 1);
4544 }
4545
4546#ifdef CONFIG_IPMI_PANIC_EVENT
4547 send_panic_events(ptr);
4548#endif
4549
4550 return NOTIFY_DONE;
4551}
4552
4553static struct notifier_block panic_block = {
4554 .notifier_call = panic_event,
4555 .next = NULL,
4556 .priority = 200 /* priority: INT_MAX >= x >= 0 */
4557};
4558
4559static int ipmi_init_msghandler(void)
4560{
Corey Minyard50c812b2006-03-26 01:37:21 -08004561 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562
4563 if (initialized)
4564 return 0;
4565
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08004566 rv = driver_register(&ipmidriver.driver);
Corey Minyard50c812b2006-03-26 01:37:21 -08004567 if (rv) {
4568 printk(KERN_ERR PFX "Could not register IPMI driver\n");
4569 return rv;
4570 }
4571
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 printk(KERN_INFO "ipmi message handler version "
Corey Minyard1fdd75b2005-09-06 15:18:42 -07004573 IPMI_DRIVER_VERSION "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574
Corey Minyard3b625942005-06-23 22:01:42 -07004575#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 proc_ipmi_root = proc_mkdir("ipmi", NULL);
4577 if (!proc_ipmi_root) {
4578 printk(KERN_ERR PFX "Unable to create IPMI proc dir");
trenn@suse.de80fad5b2014-10-14 16:40:23 +02004579 driver_unregister(&ipmidriver.driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 return -ENOMEM;
4581 }
4582
Corey Minyard3b625942005-06-23 22:01:42 -07004583#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584
Corey Minyard409035e2006-06-28 04:26:53 -07004585 setup_timer(&ipmi_timer, ipmi_timeout, 0);
4586 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587
Alan Sterne041c682006-03-27 01:16:30 -08004588 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589
4590 initialized = 1;
4591
4592 return 0;
4593}
4594
Corey Minyard60ee6d52010-10-27 15:34:18 -07004595static int __init ipmi_init_msghandler_mod(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596{
4597 ipmi_init_msghandler();
4598 return 0;
4599}
4600
Corey Minyard60ee6d52010-10-27 15:34:18 -07004601static void __exit cleanup_ipmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602{
4603 int count;
4604
4605 if (!initialized)
4606 return;
4607
Alan Sterne041c682006-03-27 01:16:30 -08004608 atomic_notifier_chain_unregister(&panic_notifier_list, &panic_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609
Corey Minyardc70d7492008-04-29 01:01:09 -07004610 /*
4611 * This can't be called if any interfaces exist, so no worry
4612 * about shutting down the interfaces.
4613 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614
Corey Minyardc70d7492008-04-29 01:01:09 -07004615 /*
4616 * Tell the timer to stop, then wait for it to stop. This
4617 * avoids problems with race conditions removing the timer
4618 * here.
4619 */
Corey Minyard8f43f842005-06-23 22:01:40 -07004620 atomic_inc(&stop_operation);
4621 del_timer_sync(&ipmi_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622
Corey Minyard3b625942005-06-23 22:01:42 -07004623#ifdef CONFIG_PROC_FS
David Howellsa8ca16e2013-04-12 17:27:28 +01004624 proc_remove(proc_ipmi_root);
Corey Minyard3b625942005-06-23 22:01:42 -07004625#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08004627 driver_unregister(&ipmidriver.driver);
Corey Minyard50c812b2006-03-26 01:37:21 -08004628
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 initialized = 0;
4630
4631 /* Check for buffer leaks. */
4632 count = atomic_read(&smi_msg_inuse_count);
4633 if (count != 0)
4634 printk(KERN_WARNING PFX "SMI message count %d at exit\n",
4635 count);
4636 count = atomic_read(&recv_msg_inuse_count);
4637 if (count != 0)
4638 printk(KERN_WARNING PFX "recv message count %d at exit\n",
4639 count);
4640}
4641module_exit(cleanup_ipmi);
4642
4643module_init(ipmi_init_msghandler_mod);
4644MODULE_LICENSE("GPL");
Corey Minyard1fdd75b2005-09-06 15:18:42 -07004645MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
Corey Minyardc70d7492008-04-29 01:01:09 -07004646MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI"
4647 " interface.");
Corey Minyard1fdd75b2005-09-06 15:18:42 -07004648MODULE_VERSION(IPMI_DRIVER_VERSION);
Martin Wilck070cbd12016-11-25 10:55:36 +01004649MODULE_SOFTDEP("post: ipmi_devintf");