blob: e93fc8d22fb2b839413dacd50fda823c060f7989 [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>
36#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/poll.h>
38#include <linux/spinlock.h>
Corey Minyardd6dfd132006-03-31 02:30:41 -080039#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/slab.h>
41#include <linux/ipmi.h>
42#include <linux/ipmi_smi.h>
43#include <linux/notifier.h>
44#include <linux/init.h>
45#include <linux/proc_fs.h>
Corey Minyard393d2cc2005-11-07 00:59:54 -080046#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#define PFX "IPMI message handler: "
Corey Minyard1fdd75b2005-09-06 15:18:42 -070049
Corey Minyardf7caa1b2008-04-29 01:01:04 -070050#define IPMI_DRIVER_VERSION "39.2"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void);
53static int ipmi_init_msghandler(void);
54
Randy Dunlap0c8204b2006-12-10 02:19:06 -080055static int initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Corey Minyard3b625942005-06-23 22:01:42 -070057#ifdef CONFIG_PROC_FS
Randy Dunlap0c8204b2006-12-10 02:19:06 -080058static struct proc_dir_entry *proc_ipmi_root;
Corey Minyard3b625942005-06-23 22:01:42 -070059#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Corey Minyardb9675132006-12-06 20:41:02 -080061/* Remain in auto-maintenance mode for this amount of time (in ms). */
62#define IPMI_MAINTENANCE_MODE_TIMEOUT 30000
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#define MAX_EVENTS_IN_QUEUE 25
65
Corey Minyardc70d7492008-04-29 01:01:09 -070066/*
67 * Don't let a message sit in a queue forever, always time it with at lest
68 * the max message timer. This is in milliseconds.
69 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define MAX_MSG_TIMEOUT 60000
71
Corey Minyard393d2cc2005-11-07 00:59:54 -080072/*
73 * The main "user" data structure.
74 */
Corey Minyardc70d7492008-04-29 01:01:09 -070075struct ipmi_user {
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 struct list_head link;
77
Corey Minyard393d2cc2005-11-07 00:59:54 -080078 /* Set to "0" when the user is destroyed. */
79 int valid;
80
81 struct kref refcount;
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 /* The upper layer that handles receive messages. */
84 struct ipmi_user_hndl *handler;
85 void *handler_data;
86
87 /* The interface this user is bound to. */
88 ipmi_smi_t intf;
89
90 /* Does this interface receive IPMI events? */
91 int gets_events;
92};
93
Corey Minyardc70d7492008-04-29 01:01:09 -070094struct cmd_rcvr {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 struct list_head link;
96
97 ipmi_user_t user;
98 unsigned char netfn;
99 unsigned char cmd;
Corey Minyardc69c3122006-09-30 23:27:56 -0700100 unsigned int chans;
Corey Minyard393d2cc2005-11-07 00:59:54 -0800101
102 /*
103 * This is used to form a linked lised during mass deletion.
104 * Since this is in an RCU list, we cannot use the link above
105 * or change any data until the RCU period completes. So we
106 * use this next variable during mass deletion so we can have
107 * a list and don't have to wait and restart the search on
Corey Minyardc70d7492008-04-29 01:01:09 -0700108 * every individual deletion of a command.
109 */
Corey Minyard393d2cc2005-11-07 00:59:54 -0800110 struct cmd_rcvr *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
Corey Minyardc70d7492008-04-29 01:01:09 -0700113struct seq_table {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 unsigned int inuse : 1;
115 unsigned int broadcast : 1;
116
117 unsigned long timeout;
118 unsigned long orig_timeout;
119 unsigned int retries_left;
120
Corey Minyardc70d7492008-04-29 01:01:09 -0700121 /*
122 * To verify on an incoming send message response that this is
123 * the message that the response is for, we keep a sequence id
124 * and increment it every time we send a message.
125 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 long seqid;
127
Corey Minyardc70d7492008-04-29 01:01:09 -0700128 /*
129 * This is held so we can properly respond to the message on a
130 * timeout, and it is used to hold the temporary data for
131 * retransmission, too.
132 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 struct ipmi_recv_msg *recv_msg;
134};
135
Corey Minyardc70d7492008-04-29 01:01:09 -0700136/*
137 * Store the information in a msgid (long) to allow us to find a
138 * sequence table entry from the msgid.
139 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140#define STORE_SEQ_IN_MSGID(seq, seqid) (((seq&0xff)<<26) | (seqid&0x3ffffff))
141
142#define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
143 do { \
144 seq = ((msgid >> 26) & 0x3f); \
145 seqid = (msgid & 0x3fffff); \
Corey Minyardc70d7492008-04-29 01:01:09 -0700146 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148#define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3fffff)
149
Corey Minyardc70d7492008-04-29 01:01:09 -0700150struct ipmi_channel {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 unsigned char medium;
152 unsigned char protocol;
Corey Minyardc14979b2005-09-06 15:18:38 -0700153
Corey Minyardc70d7492008-04-29 01:01:09 -0700154 /*
155 * My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR,
156 * but may be changed by the user.
157 */
Corey Minyardc14979b2005-09-06 15:18:38 -0700158 unsigned char address;
159
Corey Minyardc70d7492008-04-29 01:01:09 -0700160 /*
161 * My LUN. This should generally stay the SMS LUN, but just in
162 * case...
163 */
Corey Minyardc14979b2005-09-06 15:18:38 -0700164 unsigned char lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165};
166
Corey Minyard3b625942005-06-23 22:01:42 -0700167#ifdef CONFIG_PROC_FS
Corey Minyardc70d7492008-04-29 01:01:09 -0700168struct ipmi_proc_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 char *name;
170 struct ipmi_proc_entry *next;
171};
Corey Minyard3b625942005-06-23 22:01:42 -0700172#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Corey Minyardc70d7492008-04-29 01:01:09 -0700174struct bmc_device {
Corey Minyard50c812b2006-03-26 01:37:21 -0800175 struct platform_device *dev;
176 struct ipmi_device_id id;
177 unsigned char guid[16];
178 int guid_set;
179
180 struct kref refcount;
181
182 /* bmc device attributes */
183 struct device_attribute device_id_attr;
184 struct device_attribute provides_dev_sdrs_attr;
185 struct device_attribute revision_attr;
186 struct device_attribute firmware_rev_attr;
187 struct device_attribute version_attr;
188 struct device_attribute add_dev_support_attr;
189 struct device_attribute manufacturer_id_attr;
190 struct device_attribute product_id_attr;
191 struct device_attribute guid_attr;
192 struct device_attribute aux_firmware_rev_attr;
193};
194
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700195/*
196 * Various statistics for IPMI, these index stats[] in the ipmi_smi
197 * structure.
198 */
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700199enum ipmi_stat_indexes {
200 /* Commands we got from the user that were invalid. */
201 IPMI_STAT_sent_invalid_commands = 0,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700202
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700203 /* Commands we sent to the MC. */
204 IPMI_STAT_sent_local_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700205
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700206 /* Responses from the MC that were delivered to a user. */
207 IPMI_STAT_handled_local_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700208
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700209 /* Responses from the MC that were not delivered to a user. */
210 IPMI_STAT_unhandled_local_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700211
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700212 /* Commands we sent out to the IPMB bus. */
213 IPMI_STAT_sent_ipmb_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700214
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700215 /* Commands sent on the IPMB that had errors on the SEND CMD */
216 IPMI_STAT_sent_ipmb_command_errs,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700217
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700218 /* Each retransmit increments this count. */
219 IPMI_STAT_retransmitted_ipmb_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700220
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700221 /*
222 * When a message times out (runs out of retransmits) this is
223 * incremented.
224 */
225 IPMI_STAT_timed_out_ipmb_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700226
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700227 /*
228 * This is like above, but for broadcasts. Broadcasts are
229 * *not* included in the above count (they are expected to
230 * time out).
231 */
232 IPMI_STAT_timed_out_ipmb_broadcasts,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700233
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700234 /* Responses I have sent to the IPMB bus. */
235 IPMI_STAT_sent_ipmb_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700236
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700237 /* The response was delivered to the user. */
238 IPMI_STAT_handled_ipmb_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700239
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700240 /* The response had invalid data in it. */
241 IPMI_STAT_invalid_ipmb_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700242
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700243 /* The response didn't have anyone waiting for it. */
244 IPMI_STAT_unhandled_ipmb_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700245
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700246 /* Commands we sent out to the IPMB bus. */
247 IPMI_STAT_sent_lan_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700248
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700249 /* Commands sent on the IPMB that had errors on the SEND CMD */
250 IPMI_STAT_sent_lan_command_errs,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700251
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700252 /* Each retransmit increments this count. */
253 IPMI_STAT_retransmitted_lan_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700254
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700255 /*
256 * When a message times out (runs out of retransmits) this is
257 * incremented.
258 */
259 IPMI_STAT_timed_out_lan_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700260
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700261 /* Responses I have sent to the IPMB bus. */
262 IPMI_STAT_sent_lan_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700263
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700264 /* The response was delivered to the user. */
265 IPMI_STAT_handled_lan_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700266
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700267 /* The response had invalid data in it. */
268 IPMI_STAT_invalid_lan_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700269
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700270 /* The response didn't have anyone waiting for it. */
271 IPMI_STAT_unhandled_lan_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700272
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700273 /* The command was delivered to the user. */
274 IPMI_STAT_handled_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700275
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700276 /* The command had invalid data in it. */
277 IPMI_STAT_invalid_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700278
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700279 /* The command didn't have anyone waiting for it. */
280 IPMI_STAT_unhandled_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700281
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700282 /* Invalid data in an event. */
283 IPMI_STAT_invalid_events,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700284
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700285 /* Events that were received with the proper format. */
286 IPMI_STAT_events,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700287
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700288
289 /* This *must* remain last, add new values above this. */
290 IPMI_NUM_STATS
291};
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700292
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294#define IPMI_IPMB_NUM_SEQ 64
Corey Minyardc14979b2005-09-06 15:18:38 -0700295#define IPMI_MAX_CHANNELS 16
Corey Minyardc70d7492008-04-29 01:01:09 -0700296struct ipmi_smi {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 /* What interface number are we? */
298 int intf_num;
299
Corey Minyard393d2cc2005-11-07 00:59:54 -0800300 struct kref refcount;
301
Corey Minyardbca03242006-12-06 20:40:57 -0800302 /* Used for a list of interfaces. */
303 struct list_head link;
304
Corey Minyardc70d7492008-04-29 01:01:09 -0700305 /*
306 * The list of upper layers that are using me. seq_lock
307 * protects this.
308 */
Corey Minyard393d2cc2005-11-07 00:59:54 -0800309 struct list_head users;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Corey Minyardb2c03942006-12-06 20:41:00 -0800311 /* Information to supply to users. */
312 unsigned char ipmi_version_major;
313 unsigned char ipmi_version_minor;
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 /* Used for wake ups at startup. */
316 wait_queue_head_t waitq;
317
Corey Minyard50c812b2006-03-26 01:37:21 -0800318 struct bmc_device *bmc;
319 char *my_dev_name;
Corey Minyard759643b2006-12-06 20:40:59 -0800320 char *sysfs_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Corey Minyardc70d7492008-04-29 01:01:09 -0700322 /*
323 * This is the lower-layer's sender routine. Note that you
Corey Minyardb2c03942006-12-06 20:41:00 -0800324 * must either be holding the ipmi_interfaces_mutex or be in
325 * an umpreemptible region to use this. You must fetch the
Corey Minyardc70d7492008-04-29 01:01:09 -0700326 * value into a local variable and make sure it is not NULL.
327 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 struct ipmi_smi_handlers *handlers;
329 void *send_info;
330
Corey Minyard3b625942005-06-23 22:01:42 -0700331#ifdef CONFIG_PROC_FS
Corey Minyardac019152007-10-18 03:07:11 -0700332 /* A list of proc entries for this interface. */
333 struct mutex proc_entry_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 struct ipmi_proc_entry *proc_entries;
Corey Minyard3b625942005-06-23 22:01:42 -0700335#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Corey Minyard50c812b2006-03-26 01:37:21 -0800337 /* Driver-model device for the system interface. */
338 struct device *si_dev;
339
Corey Minyardc70d7492008-04-29 01:01:09 -0700340 /*
341 * A table of sequence numbers for this interface. We use the
342 * sequence numbers for IPMB messages that go out of the
343 * interface to match them up with their responses. A routine
344 * is called periodically to time the items in this list.
345 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 spinlock_t seq_lock;
347 struct seq_table seq_table[IPMI_IPMB_NUM_SEQ];
348 int curr_seq;
349
Corey Minyardc70d7492008-04-29 01:01:09 -0700350 /*
351 * Messages that were delayed for some reason (out of memory,
352 * for instance), will go in here to be processed later in a
353 * periodic timer interrupt.
354 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 spinlock_t waiting_msgs_lock;
356 struct list_head waiting_msgs;
357
Corey Minyardc70d7492008-04-29 01:01:09 -0700358 /*
359 * The list of command receivers that are registered for commands
360 * on this interface.
361 */
Corey Minyardd6dfd132006-03-31 02:30:41 -0800362 struct mutex cmd_rcvrs_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 struct list_head cmd_rcvrs;
364
Corey Minyardc70d7492008-04-29 01:01:09 -0700365 /*
366 * Events that were queues because no one was there to receive
367 * them.
368 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 spinlock_t events_lock; /* For dealing with event stuff. */
370 struct list_head waiting_events;
371 unsigned int waiting_events_count; /* How many events in queue? */
Corey Minyard87ebd062008-04-29 01:01:04 -0700372 char delivering_events;
373 char event_msg_printed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Corey Minyardc70d7492008-04-29 01:01:09 -0700375 /*
376 * The event receiver for my BMC, only really used at panic
377 * shutdown as a place to store this.
378 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 unsigned char event_receiver;
380 unsigned char event_receiver_lun;
381 unsigned char local_sel_device;
382 unsigned char local_event_generator;
383
Corey Minyardb9675132006-12-06 20:41:02 -0800384 /* For handling of maintenance mode. */
385 int maintenance_mode;
386 int maintenance_mode_enable;
387 int auto_maintenance_timeout;
388 spinlock_t maintenance_mode_lock; /* Used in a timer... */
389
Corey Minyardc70d7492008-04-29 01:01:09 -0700390 /*
391 * A cheap hack, if this is non-null and a message to an
392 * interface comes in with a NULL user, call this routine with
393 * it. Note that the message will still be freed by the
394 * caller. This only works on the system interface.
395 */
Corey Minyard56a55ec2005-09-06 15:18:42 -0700396 void (*null_user_handler)(ipmi_smi_t intf, struct ipmi_recv_msg *msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Corey Minyardc70d7492008-04-29 01:01:09 -0700398 /*
399 * When we are scanning the channels for an SMI, this will
400 * tell which channel we are scanning.
401 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 int curr_channel;
403
404 /* Channel information */
405 struct ipmi_channel channels[IPMI_MAX_CHANNELS];
406
407 /* Proc FS stuff. */
408 struct proc_dir_entry *proc_dir;
409 char proc_dir_name[10];
410
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700411 atomic_t stats[IPMI_NUM_STATS];
Konstantin Baydarov5956dce2008-04-29 01:01:03 -0700412
413 /*
414 * run_to_completion duplicate of smb_info, smi_info
415 * and ipmi_serial_info structures. Used to decrease numbers of
416 * parameters passed by "low" level IPMI code.
417 */
418 int run_to_completion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419};
Corey Minyard50c812b2006-03-26 01:37:21 -0800420#define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Corey Minyard50c812b2006-03-26 01:37:21 -0800422/**
423 * The driver model view of the IPMI messaging driver.
424 */
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -0800425static struct platform_driver ipmidriver = {
426 .driver = {
427 .name = "ipmi",
428 .bus = &platform_bus_type
429 }
Corey Minyard50c812b2006-03-26 01:37:21 -0800430};
431static DEFINE_MUTEX(ipmidriver_mutex);
432
Denis Chengbed97592008-02-06 01:37:35 -0800433static LIST_HEAD(ipmi_interfaces);
Corey Minyardbca03242006-12-06 20:40:57 -0800434static DEFINE_MUTEX(ipmi_interfaces_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Corey Minyardc70d7492008-04-29 01:01:09 -0700436/*
437 * List of watchers that want to know when smi's are added and deleted.
438 */
Denis Chengbed97592008-02-06 01:37:35 -0800439static LIST_HEAD(smi_watchers);
Corey Minyardb2c03942006-12-06 20:41:00 -0800440static DEFINE_MUTEX(smi_watchers_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700443#define ipmi_inc_stat(intf, stat) \
444 atomic_inc(&(intf)->stats[IPMI_STAT_ ## stat])
445#define ipmi_get_stat(intf, stat) \
446 ((unsigned int) atomic_read(&(intf)->stats[IPMI_STAT_ ## stat]))
447
448
Corey Minyard393d2cc2005-11-07 00:59:54 -0800449static void free_recv_msg_list(struct list_head *q)
450{
451 struct ipmi_recv_msg *msg, *msg2;
452
453 list_for_each_entry_safe(msg, msg2, q, link) {
454 list_del(&msg->link);
455 ipmi_free_recv_msg(msg);
456 }
457}
458
Corey Minyardf3ce6a02006-11-08 17:44:52 -0800459static void free_smi_msg_list(struct list_head *q)
460{
461 struct ipmi_smi_msg *msg, *msg2;
462
463 list_for_each_entry_safe(msg, msg2, q, link) {
464 list_del(&msg->link);
465 ipmi_free_smi_msg(msg);
466 }
467}
468
Corey Minyard393d2cc2005-11-07 00:59:54 -0800469static void clean_up_interface_data(ipmi_smi_t intf)
470{
471 int i;
472 struct cmd_rcvr *rcvr, *rcvr2;
Corey Minyard393d2cc2005-11-07 00:59:54 -0800473 struct list_head list;
474
Corey Minyardf3ce6a02006-11-08 17:44:52 -0800475 free_smi_msg_list(&intf->waiting_msgs);
Corey Minyard393d2cc2005-11-07 00:59:54 -0800476 free_recv_msg_list(&intf->waiting_events);
477
Corey Minyard78ba2fa2007-02-10 01:45:45 -0800478 /*
479 * Wholesale remove all the entries from the list in the
480 * interface and wait for RCU to know that none are in use.
481 */
Corey Minyardd6dfd132006-03-31 02:30:41 -0800482 mutex_lock(&intf->cmd_rcvrs_mutex);
Corey Minyard78ba2fa2007-02-10 01:45:45 -0800483 INIT_LIST_HEAD(&list);
484 list_splice_init_rcu(&intf->cmd_rcvrs, &list, synchronize_rcu);
Corey Minyardd6dfd132006-03-31 02:30:41 -0800485 mutex_unlock(&intf->cmd_rcvrs_mutex);
Corey Minyard393d2cc2005-11-07 00:59:54 -0800486
487 list_for_each_entry_safe(rcvr, rcvr2, &list, link)
488 kfree(rcvr);
489
490 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
491 if ((intf->seq_table[i].inuse)
Corey Minyardc70d7492008-04-29 01:01:09 -0700492 && (intf->seq_table[i].recv_msg))
Corey Minyard393d2cc2005-11-07 00:59:54 -0800493 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
Corey Minyard393d2cc2005-11-07 00:59:54 -0800495}
496
497static void intf_free(struct kref *ref)
498{
499 ipmi_smi_t intf = container_of(ref, struct ipmi_smi, refcount);
500
501 clean_up_interface_data(intf);
502 kfree(intf);
503}
504
Corey Minyardbca03242006-12-06 20:40:57 -0800505struct watcher_entry {
Corey Minyardb2c03942006-12-06 20:41:00 -0800506 int intf_num;
507 ipmi_smi_t intf;
Corey Minyardbca03242006-12-06 20:40:57 -0800508 struct list_head link;
Corey Minyardbca03242006-12-06 20:40:57 -0800509};
510
Corey Minyard393d2cc2005-11-07 00:59:54 -0800511int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher)
512{
Corey Minyardbca03242006-12-06 20:40:57 -0800513 ipmi_smi_t intf;
Denis Chenge381d1c2008-02-06 01:37:39 -0800514 LIST_HEAD(to_deliver);
Corey Minyardbca03242006-12-06 20:40:57 -0800515 struct watcher_entry *e, *e2;
516
Corey Minyardb2c03942006-12-06 20:41:00 -0800517 mutex_lock(&smi_watchers_mutex);
518
Corey Minyardbca03242006-12-06 20:40:57 -0800519 mutex_lock(&ipmi_interfaces_mutex);
520
Corey Minyardb2c03942006-12-06 20:41:00 -0800521 /* Build a list of things to deliver. */
Corey Minyard78ba2fa2007-02-10 01:45:45 -0800522 list_for_each_entry(intf, &ipmi_interfaces, link) {
Corey Minyardbca03242006-12-06 20:40:57 -0800523 if (intf->intf_num == -1)
524 continue;
525 e = kmalloc(sizeof(*e), GFP_KERNEL);
526 if (!e)
527 goto out_err;
Corey Minyardb2c03942006-12-06 20:41:00 -0800528 kref_get(&intf->refcount);
529 e->intf = intf;
Corey Minyardbca03242006-12-06 20:40:57 -0800530 e->intf_num = intf->intf_num;
531 list_add_tail(&e->link, &to_deliver);
532 }
Corey Minyard393d2cc2005-11-07 00:59:54 -0800533
Corey Minyardb2c03942006-12-06 20:41:00 -0800534 /* We will succeed, so add it to the list. */
535 list_add(&watcher->link, &smi_watchers);
Corey Minyardbca03242006-12-06 20:40:57 -0800536
537 mutex_unlock(&ipmi_interfaces_mutex);
538
539 list_for_each_entry_safe(e, e2, &to_deliver, link) {
540 list_del(&e->link);
Corey Minyardb2c03942006-12-06 20:41:00 -0800541 watcher->new_smi(e->intf_num, e->intf->si_dev);
542 kref_put(&e->intf->refcount, intf_free);
Corey Minyardbca03242006-12-06 20:40:57 -0800543 kfree(e);
Corey Minyard393d2cc2005-11-07 00:59:54 -0800544 }
Corey Minyardbca03242006-12-06 20:40:57 -0800545
Corey Minyardb2c03942006-12-06 20:41:00 -0800546 mutex_unlock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -0800547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return 0;
Corey Minyardbca03242006-12-06 20:40:57 -0800549
550 out_err:
Corey Minyardb2c03942006-12-06 20:41:00 -0800551 mutex_unlock(&ipmi_interfaces_mutex);
552 mutex_unlock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -0800553 list_for_each_entry_safe(e, e2, &to_deliver, link) {
554 list_del(&e->link);
Corey Minyardb2c03942006-12-06 20:41:00 -0800555 kref_put(&e->intf->refcount, intf_free);
Corey Minyardbca03242006-12-06 20:40:57 -0800556 kfree(e);
557 }
558 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
Corey Minyardc70d7492008-04-29 01:01:09 -0700560EXPORT_SYMBOL(ipmi_smi_watcher_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher)
563{
Corey Minyardb2c03942006-12-06 20:41:00 -0800564 mutex_lock(&smi_watchers_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 list_del(&(watcher->link));
Corey Minyardb2c03942006-12-06 20:41:00 -0800566 mutex_unlock(&smi_watchers_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return 0;
568}
Corey Minyardc70d7492008-04-29 01:01:09 -0700569EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Corey Minyardb2c03942006-12-06 20:41:00 -0800571/*
572 * Must be called with smi_watchers_mutex held.
573 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574static void
Corey Minyard50c812b2006-03-26 01:37:21 -0800575call_smi_watchers(int i, struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 struct ipmi_smi_watcher *w;
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 list_for_each_entry(w, &smi_watchers, link) {
580 if (try_module_get(w->owner)) {
Corey Minyard50c812b2006-03-26 01:37:21 -0800581 w->new_smi(i, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 module_put(w->owner);
583 }
584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
587static int
588ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2)
589{
590 if (addr1->addr_type != addr2->addr_type)
591 return 0;
592
593 if (addr1->channel != addr2->channel)
594 return 0;
595
596 if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
597 struct ipmi_system_interface_addr *smi_addr1
598 = (struct ipmi_system_interface_addr *) addr1;
599 struct ipmi_system_interface_addr *smi_addr2
600 = (struct ipmi_system_interface_addr *) addr2;
601 return (smi_addr1->lun == smi_addr2->lun);
602 }
603
604 if ((addr1->addr_type == IPMI_IPMB_ADDR_TYPE)
Corey Minyardc70d7492008-04-29 01:01:09 -0700605 || (addr1->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 struct ipmi_ipmb_addr *ipmb_addr1
607 = (struct ipmi_ipmb_addr *) addr1;
608 struct ipmi_ipmb_addr *ipmb_addr2
609 = (struct ipmi_ipmb_addr *) addr2;
610
611 return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr)
612 && (ipmb_addr1->lun == ipmb_addr2->lun));
613 }
614
615 if (addr1->addr_type == IPMI_LAN_ADDR_TYPE) {
616 struct ipmi_lan_addr *lan_addr1
617 = (struct ipmi_lan_addr *) addr1;
618 struct ipmi_lan_addr *lan_addr2
619 = (struct ipmi_lan_addr *) addr2;
620
621 return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID)
622 && (lan_addr1->local_SWID == lan_addr2->local_SWID)
623 && (lan_addr1->session_handle
624 == lan_addr2->session_handle)
625 && (lan_addr1->lun == lan_addr2->lun));
626 }
627
628 return 1;
629}
630
631int ipmi_validate_addr(struct ipmi_addr *addr, int len)
632{
Corey Minyardc70d7492008-04-29 01:01:09 -0700633 if (len < sizeof(struct ipmi_system_interface_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
637 if (addr->channel != IPMI_BMC_CHANNEL)
638 return -EINVAL;
639 return 0;
640 }
641
642 if ((addr->channel == IPMI_BMC_CHANNEL)
Jayachandran C12fc1d72006-02-03 03:04:51 -0800643 || (addr->channel >= IPMI_MAX_CHANNELS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 || (addr->channel < 0))
645 return -EINVAL;
646
647 if ((addr->addr_type == IPMI_IPMB_ADDR_TYPE)
Corey Minyardc70d7492008-04-29 01:01:09 -0700648 || (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE)) {
649 if (len < sizeof(struct ipmi_ipmb_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return 0;
652 }
653
654 if (addr->addr_type == IPMI_LAN_ADDR_TYPE) {
Corey Minyardc70d7492008-04-29 01:01:09 -0700655 if (len < sizeof(struct ipmi_lan_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return 0;
658 }
659
660 return -EINVAL;
661}
Corey Minyardc70d7492008-04-29 01:01:09 -0700662EXPORT_SYMBOL(ipmi_validate_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664unsigned int ipmi_addr_length(int addr_type)
665{
666 if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
667 return sizeof(struct ipmi_system_interface_addr);
668
669 if ((addr_type == IPMI_IPMB_ADDR_TYPE)
Corey Minyardc70d7492008-04-29 01:01:09 -0700670 || (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return sizeof(struct ipmi_ipmb_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 if (addr_type == IPMI_LAN_ADDR_TYPE)
674 return sizeof(struct ipmi_lan_addr);
675
676 return 0;
677}
Corey Minyardc70d7492008-04-29 01:01:09 -0700678EXPORT_SYMBOL(ipmi_addr_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680static void deliver_response(struct ipmi_recv_msg *msg)
681{
Corey Minyard8a3628d2006-03-31 02:30:40 -0800682 if (!msg->user) {
Corey Minyard56a55ec2005-09-06 15:18:42 -0700683 ipmi_smi_t intf = msg->user_msg_data;
Corey Minyard56a55ec2005-09-06 15:18:42 -0700684
685 /* Special handling for NULL users. */
686 if (intf->null_user_handler) {
687 intf->null_user_handler(intf, msg);
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700688 ipmi_inc_stat(intf, handled_local_responses);
Corey Minyard56a55ec2005-09-06 15:18:42 -0700689 } else {
690 /* No handler, so give up. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700691 ipmi_inc_stat(intf, unhandled_local_responses);
Corey Minyard56a55ec2005-09-06 15:18:42 -0700692 }
693 ipmi_free_recv_msg(msg);
694 } else {
Corey Minyard393d2cc2005-11-07 00:59:54 -0800695 ipmi_user_t user = msg->user;
696 user->handler->ipmi_recv_hndl(msg, user->handler_data);
Corey Minyard56a55ec2005-09-06 15:18:42 -0700697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
Corey Minyardb2c03942006-12-06 20:41:00 -0800700static void
701deliver_err_response(struct ipmi_recv_msg *msg, int err)
702{
703 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
704 msg->msg_data[0] = err;
705 msg->msg.netfn |= 1; /* Convert to a response. */
706 msg->msg.data_len = 1;
707 msg->msg.data = msg->msg_data;
708 deliver_response(msg);
709}
710
Corey Minyardc70d7492008-04-29 01:01:09 -0700711/*
712 * Find the next sequence number not being used and add the given
713 * message with the given timeout to the sequence table. This must be
714 * called with the interface's seq_lock held.
715 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716static int intf_next_seq(ipmi_smi_t intf,
717 struct ipmi_recv_msg *recv_msg,
718 unsigned long timeout,
719 int retries,
720 int broadcast,
721 unsigned char *seq,
722 long *seqid)
723{
724 int rv = 0;
725 unsigned int i;
726
Corey Minyardc70d7492008-04-29 01:01:09 -0700727 for (i = intf->curr_seq; (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq;
728 i = (i+1)%IPMI_IPMB_NUM_SEQ) {
Corey Minyard8a3628d2006-03-31 02:30:40 -0800729 if (!intf->seq_table[i].inuse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 break;
731 }
732
Corey Minyard8a3628d2006-03-31 02:30:40 -0800733 if (!intf->seq_table[i].inuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 intf->seq_table[i].recv_msg = recv_msg;
735
Corey Minyardc70d7492008-04-29 01:01:09 -0700736 /*
737 * Start with the maximum timeout, when the send response
738 * comes in we will start the real timer.
739 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 intf->seq_table[i].timeout = MAX_MSG_TIMEOUT;
741 intf->seq_table[i].orig_timeout = timeout;
742 intf->seq_table[i].retries_left = retries;
743 intf->seq_table[i].broadcast = broadcast;
744 intf->seq_table[i].inuse = 1;
745 intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid);
746 *seq = i;
747 *seqid = intf->seq_table[i].seqid;
748 intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ;
749 } else {
750 rv = -EAGAIN;
751 }
Corey Minyardc70d7492008-04-29 01:01:09 -0700752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return rv;
754}
755
Corey Minyardc70d7492008-04-29 01:01:09 -0700756/*
757 * Return the receive message for the given sequence number and
758 * release the sequence number so it can be reused. Some other data
759 * is passed in to be sure the message matches up correctly (to help
760 * guard against message coming in after their timeout and the
761 * sequence number being reused).
762 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763static int intf_find_seq(ipmi_smi_t intf,
764 unsigned char seq,
765 short channel,
766 unsigned char cmd,
767 unsigned char netfn,
768 struct ipmi_addr *addr,
769 struct ipmi_recv_msg **recv_msg)
770{
771 int rv = -ENODEV;
772 unsigned long flags;
773
774 if (seq >= IPMI_IPMB_NUM_SEQ)
775 return -EINVAL;
776
777 spin_lock_irqsave(&(intf->seq_lock), flags);
778 if (intf->seq_table[seq].inuse) {
779 struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg;
780
Corey Minyardc70d7492008-04-29 01:01:09 -0700781 if ((msg->addr.channel == channel) && (msg->msg.cmd == cmd)
782 && (msg->msg.netfn == netfn)
783 && (ipmi_addr_equal(addr, &(msg->addr)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 *recv_msg = msg;
785 intf->seq_table[seq].inuse = 0;
786 rv = 0;
787 }
788 }
789 spin_unlock_irqrestore(&(intf->seq_lock), flags);
790
791 return rv;
792}
793
794
795/* Start the timer for a specific sequence table entry. */
796static int intf_start_seq_timer(ipmi_smi_t intf,
797 long msgid)
798{
799 int rv = -ENODEV;
800 unsigned long flags;
801 unsigned char seq;
802 unsigned long seqid;
803
804
805 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
806
807 spin_lock_irqsave(&(intf->seq_lock), flags);
Corey Minyardc70d7492008-04-29 01:01:09 -0700808 /*
809 * We do this verification because the user can be deleted
810 * while a message is outstanding.
811 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if ((intf->seq_table[seq].inuse)
Corey Minyardc70d7492008-04-29 01:01:09 -0700813 && (intf->seq_table[seq].seqid == seqid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 struct seq_table *ent = &(intf->seq_table[seq]);
815 ent->timeout = ent->orig_timeout;
816 rv = 0;
817 }
818 spin_unlock_irqrestore(&(intf->seq_lock), flags);
819
820 return rv;
821}
822
823/* Got an error for the send message for a specific sequence number. */
824static int intf_err_seq(ipmi_smi_t intf,
825 long msgid,
826 unsigned int err)
827{
828 int rv = -ENODEV;
829 unsigned long flags;
830 unsigned char seq;
831 unsigned long seqid;
832 struct ipmi_recv_msg *msg = NULL;
833
834
835 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
836
837 spin_lock_irqsave(&(intf->seq_lock), flags);
Corey Minyardc70d7492008-04-29 01:01:09 -0700838 /*
839 * We do this verification because the user can be deleted
840 * while a message is outstanding.
841 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if ((intf->seq_table[seq].inuse)
Corey Minyardc70d7492008-04-29 01:01:09 -0700843 && (intf->seq_table[seq].seqid == seqid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 struct seq_table *ent = &(intf->seq_table[seq]);
845
846 ent->inuse = 0;
847 msg = ent->recv_msg;
848 rv = 0;
849 }
850 spin_unlock_irqrestore(&(intf->seq_lock), flags);
851
Corey Minyardb2c03942006-12-06 20:41:00 -0800852 if (msg)
853 deliver_err_response(msg, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 return rv;
856}
857
858
859int ipmi_create_user(unsigned int if_num,
860 struct ipmi_user_hndl *handler,
861 void *handler_data,
862 ipmi_user_t *user)
863{
864 unsigned long flags;
865 ipmi_user_t new_user;
866 int rv = 0;
867 ipmi_smi_t intf;
868
Corey Minyardc70d7492008-04-29 01:01:09 -0700869 /*
870 * There is no module usecount here, because it's not
871 * required. Since this can only be used by and called from
872 * other modules, they will implicitly use this module, and
873 * thus this can't be removed unless the other modules are
874 * removed.
875 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 if (handler == NULL)
878 return -EINVAL;
879
Corey Minyardc70d7492008-04-29 01:01:09 -0700880 /*
881 * Make sure the driver is actually initialized, this handles
882 * problems with initialization order.
883 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (!initialized) {
885 rv = ipmi_init_msghandler();
886 if (rv)
887 return rv;
888
Corey Minyardc70d7492008-04-29 01:01:09 -0700889 /*
890 * The init code doesn't return an error if it was turned
891 * off, but it won't initialize. Check that.
892 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (!initialized)
894 return -ENODEV;
895 }
896
897 new_user = kmalloc(sizeof(*new_user), GFP_KERNEL);
Corey Minyard8a3628d2006-03-31 02:30:40 -0800898 if (!new_user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return -ENOMEM;
900
Corey Minyardb2c03942006-12-06 20:41:00 -0800901 mutex_lock(&ipmi_interfaces_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -0800902 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
903 if (intf->intf_num == if_num)
904 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
Corey Minyardb2c03942006-12-06 20:41:00 -0800906 /* Not found, return an error */
Corey Minyardbca03242006-12-06 20:40:57 -0800907 rv = -EINVAL;
908 goto out_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Corey Minyardbca03242006-12-06 20:40:57 -0800910 found:
Corey Minyard393d2cc2005-11-07 00:59:54 -0800911 /* Note that each existing user holds a refcount to the interface. */
912 kref_get(&intf->refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Corey Minyard393d2cc2005-11-07 00:59:54 -0800914 kref_init(&new_user->refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 new_user->handler = handler;
916 new_user->handler_data = handler_data;
917 new_user->intf = intf;
918 new_user->gets_events = 0;
919
920 if (!try_module_get(intf->handlers->owner)) {
921 rv = -ENODEV;
Adrian Bunk5c98d292006-03-25 03:07:52 -0800922 goto out_kref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924
925 if (intf->handlers->inc_usecount) {
926 rv = intf->handlers->inc_usecount(intf->send_info);
927 if (rv) {
928 module_put(intf->handlers->owner);
Adrian Bunk5c98d292006-03-25 03:07:52 -0800929 goto out_kref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
931 }
932
Corey Minyardc70d7492008-04-29 01:01:09 -0700933 /*
934 * Hold the lock so intf->handlers is guaranteed to be good
935 * until now
936 */
Corey Minyardb2c03942006-12-06 20:41:00 -0800937 mutex_unlock(&ipmi_interfaces_mutex);
938
Corey Minyard393d2cc2005-11-07 00:59:54 -0800939 new_user->valid = 1;
940 spin_lock_irqsave(&intf->seq_lock, flags);
941 list_add_rcu(&new_user->link, &intf->users);
942 spin_unlock_irqrestore(&intf->seq_lock, flags);
943 *user = new_user;
944 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Adrian Bunk5c98d292006-03-25 03:07:52 -0800946out_kref:
Corey Minyard393d2cc2005-11-07 00:59:54 -0800947 kref_put(&intf->refcount, intf_free);
Adrian Bunk5c98d292006-03-25 03:07:52 -0800948out_kfree:
Corey Minyardb2c03942006-12-06 20:41:00 -0800949 mutex_unlock(&ipmi_interfaces_mutex);
Adrian Bunk5c98d292006-03-25 03:07:52 -0800950 kfree(new_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return rv;
952}
Corey Minyardc70d7492008-04-29 01:01:09 -0700953EXPORT_SYMBOL(ipmi_create_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Corey Minyard393d2cc2005-11-07 00:59:54 -0800955static void free_user(struct kref *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Corey Minyard393d2cc2005-11-07 00:59:54 -0800957 ipmi_user_t user = container_of(ref, struct ipmi_user, refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 kfree(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
961int ipmi_destroy_user(ipmi_user_t user)
962{
Corey Minyard393d2cc2005-11-07 00:59:54 -0800963 ipmi_smi_t intf = user->intf;
964 int i;
965 unsigned long flags;
966 struct cmd_rcvr *rcvr;
Corey Minyard393d2cc2005-11-07 00:59:54 -0800967 struct cmd_rcvr *rcvrs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Corey Minyard8a3628d2006-03-31 02:30:40 -0800969 user->valid = 0;
Corey Minyard393d2cc2005-11-07 00:59:54 -0800970
971 /* Remove the user from the interface's sequence table. */
972 spin_lock_irqsave(&intf->seq_lock, flags);
973 list_del_rcu(&user->link);
974
975 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
976 if (intf->seq_table[i].inuse
Corey Minyardc70d7492008-04-29 01:01:09 -0700977 && (intf->seq_table[i].recv_msg->user == user)) {
Corey Minyard393d2cc2005-11-07 00:59:54 -0800978 intf->seq_table[i].inuse = 0;
Corey Minyardb2c03942006-12-06 20:41:00 -0800979 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
Corey Minyard393d2cc2005-11-07 00:59:54 -0800980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
Corey Minyard393d2cc2005-11-07 00:59:54 -0800982 spin_unlock_irqrestore(&intf->seq_lock, flags);
983
984 /*
985 * Remove the user from the command receiver's table. First
986 * we build a list of everything (not using the standard link,
987 * since other things may be using it till we do
988 * synchronize_rcu()) then free everything in that list.
989 */
Corey Minyardd6dfd132006-03-31 02:30:41 -0800990 mutex_lock(&intf->cmd_rcvrs_mutex);
Paul E. McKenney066bb8d2006-01-06 00:19:53 -0800991 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
Corey Minyard393d2cc2005-11-07 00:59:54 -0800992 if (rcvr->user == user) {
993 list_del_rcu(&rcvr->link);
994 rcvr->next = rcvrs;
995 rcvrs = rcvr;
996 }
997 }
Corey Minyardd6dfd132006-03-31 02:30:41 -0800998 mutex_unlock(&intf->cmd_rcvrs_mutex);
Corey Minyard393d2cc2005-11-07 00:59:54 -0800999 synchronize_rcu();
1000 while (rcvrs) {
1001 rcvr = rcvrs;
1002 rcvrs = rcvr->next;
1003 kfree(rcvr);
1004 }
1005
Corey Minyardb2c03942006-12-06 20:41:00 -08001006 mutex_lock(&ipmi_interfaces_mutex);
1007 if (intf->handlers) {
1008 module_put(intf->handlers->owner);
1009 if (intf->handlers->dec_usecount)
1010 intf->handlers->dec_usecount(intf->send_info);
1011 }
1012 mutex_unlock(&ipmi_interfaces_mutex);
Corey Minyard393d2cc2005-11-07 00:59:54 -08001013
1014 kref_put(&intf->refcount, intf_free);
1015
1016 kref_put(&user->refcount, free_user);
1017
Corey Minyard8a3628d2006-03-31 02:30:40 -08001018 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
Corey Minyardc70d7492008-04-29 01:01:09 -07001020EXPORT_SYMBOL(ipmi_destroy_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022void ipmi_get_version(ipmi_user_t user,
1023 unsigned char *major,
1024 unsigned char *minor)
1025{
Corey Minyardb2c03942006-12-06 20:41:00 -08001026 *major = user->intf->ipmi_version_major;
1027 *minor = user->intf->ipmi_version_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
Corey Minyardc70d7492008-04-29 01:01:09 -07001029EXPORT_SYMBOL(ipmi_get_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Corey Minyardc14979b2005-09-06 15:18:38 -07001031int ipmi_set_my_address(ipmi_user_t user,
1032 unsigned int channel,
1033 unsigned char address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Corey Minyardc14979b2005-09-06 15:18:38 -07001035 if (channel >= IPMI_MAX_CHANNELS)
1036 return -EINVAL;
1037 user->intf->channels[channel].address = address;
1038 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
Corey Minyardc70d7492008-04-29 01:01:09 -07001040EXPORT_SYMBOL(ipmi_set_my_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Corey Minyardc14979b2005-09-06 15:18:38 -07001042int ipmi_get_my_address(ipmi_user_t user,
1043 unsigned int channel,
1044 unsigned char *address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Corey Minyardc14979b2005-09-06 15:18:38 -07001046 if (channel >= IPMI_MAX_CHANNELS)
1047 return -EINVAL;
1048 *address = user->intf->channels[channel].address;
1049 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
Corey Minyardc70d7492008-04-29 01:01:09 -07001051EXPORT_SYMBOL(ipmi_get_my_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Corey Minyardc14979b2005-09-06 15:18:38 -07001053int ipmi_set_my_LUN(ipmi_user_t user,
1054 unsigned int channel,
1055 unsigned char LUN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
Corey Minyardc14979b2005-09-06 15:18:38 -07001057 if (channel >= IPMI_MAX_CHANNELS)
1058 return -EINVAL;
1059 user->intf->channels[channel].lun = LUN & 0x3;
1060 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061}
Corey Minyardc70d7492008-04-29 01:01:09 -07001062EXPORT_SYMBOL(ipmi_set_my_LUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Corey Minyardc14979b2005-09-06 15:18:38 -07001064int ipmi_get_my_LUN(ipmi_user_t user,
1065 unsigned int channel,
1066 unsigned char *address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Corey Minyardc14979b2005-09-06 15:18:38 -07001068 if (channel >= IPMI_MAX_CHANNELS)
1069 return -EINVAL;
1070 *address = user->intf->channels[channel].lun;
1071 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
Corey Minyardc70d7492008-04-29 01:01:09 -07001073EXPORT_SYMBOL(ipmi_get_my_LUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Corey Minyardb9675132006-12-06 20:41:02 -08001075int ipmi_get_maintenance_mode(ipmi_user_t user)
1076{
1077 int mode;
1078 unsigned long flags;
1079
1080 spin_lock_irqsave(&user->intf->maintenance_mode_lock, flags);
1081 mode = user->intf->maintenance_mode;
1082 spin_unlock_irqrestore(&user->intf->maintenance_mode_lock, flags);
1083
1084 return mode;
1085}
1086EXPORT_SYMBOL(ipmi_get_maintenance_mode);
1087
1088static void maintenance_mode_update(ipmi_smi_t intf)
1089{
1090 if (intf->handlers->set_maintenance_mode)
1091 intf->handlers->set_maintenance_mode(
1092 intf->send_info, intf->maintenance_mode_enable);
1093}
1094
1095int ipmi_set_maintenance_mode(ipmi_user_t user, int mode)
1096{
1097 int rv = 0;
1098 unsigned long flags;
1099 ipmi_smi_t intf = user->intf;
1100
1101 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1102 if (intf->maintenance_mode != mode) {
1103 switch (mode) {
1104 case IPMI_MAINTENANCE_MODE_AUTO:
1105 intf->maintenance_mode = mode;
1106 intf->maintenance_mode_enable
1107 = (intf->auto_maintenance_timeout > 0);
1108 break;
1109
1110 case IPMI_MAINTENANCE_MODE_OFF:
1111 intf->maintenance_mode = mode;
1112 intf->maintenance_mode_enable = 0;
1113 break;
1114
1115 case IPMI_MAINTENANCE_MODE_ON:
1116 intf->maintenance_mode = mode;
1117 intf->maintenance_mode_enable = 1;
1118 break;
1119
1120 default:
1121 rv = -EINVAL;
1122 goto out_unlock;
1123 }
1124
1125 maintenance_mode_update(intf);
1126 }
1127 out_unlock:
1128 spin_unlock_irqrestore(&intf->maintenance_mode_lock, flags);
1129
1130 return rv;
1131}
1132EXPORT_SYMBOL(ipmi_set_maintenance_mode);
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134int ipmi_set_gets_events(ipmi_user_t user, int val)
1135{
Corey Minyard393d2cc2005-11-07 00:59:54 -08001136 unsigned long flags;
1137 ipmi_smi_t intf = user->intf;
1138 struct ipmi_recv_msg *msg, *msg2;
1139 struct list_head msgs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Corey Minyard393d2cc2005-11-07 00:59:54 -08001141 INIT_LIST_HEAD(&msgs);
1142
1143 spin_lock_irqsave(&intf->events_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 user->gets_events = val;
1145
Corey Minyardb2c03942006-12-06 20:41:00 -08001146 if (intf->delivering_events)
1147 /*
1148 * Another thread is delivering events for this, so
1149 * let it handle any new events.
1150 */
1151 goto out;
1152
1153 /* Deliver any queued events. */
1154 while (user->gets_events && !list_empty(&intf->waiting_events)) {
Akinobu Mita179e0912006-06-26 00:24:41 -07001155 list_for_each_entry_safe(msg, msg2, &intf->waiting_events, link)
1156 list_move_tail(&msg->link, &msgs);
Corey Minyard4791c032006-04-10 22:54:31 -07001157 intf->waiting_events_count = 0;
Corey Minyard87ebd062008-04-29 01:01:04 -07001158 if (intf->event_msg_printed) {
1159 printk(KERN_WARNING PFX "Event queue no longer"
1160 " full\n");
1161 intf->event_msg_printed = 0;
1162 }
Corey Minyardb2c03942006-12-06 20:41:00 -08001163
1164 intf->delivering_events = 1;
1165 spin_unlock_irqrestore(&intf->events_lock, flags);
1166
1167 list_for_each_entry_safe(msg, msg2, &msgs, link) {
1168 msg->user = user;
1169 kref_get(&user->refcount);
1170 deliver_response(msg);
1171 }
1172
1173 spin_lock_irqsave(&intf->events_lock, flags);
1174 intf->delivering_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08001176
Corey Minyardb2c03942006-12-06 20:41:00 -08001177 out:
Corey Minyard393d2cc2005-11-07 00:59:54 -08001178 spin_unlock_irqrestore(&intf->events_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 return 0;
1181}
Corey Minyardc70d7492008-04-29 01:01:09 -07001182EXPORT_SYMBOL(ipmi_set_gets_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Corey Minyard393d2cc2005-11-07 00:59:54 -08001184static struct cmd_rcvr *find_cmd_rcvr(ipmi_smi_t intf,
1185 unsigned char netfn,
Corey Minyardc69c3122006-09-30 23:27:56 -07001186 unsigned char cmd,
1187 unsigned char chan)
Corey Minyard393d2cc2005-11-07 00:59:54 -08001188{
1189 struct cmd_rcvr *rcvr;
1190
1191 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
Corey Minyardc69c3122006-09-30 23:27:56 -07001192 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1193 && (rcvr->chans & (1 << chan)))
Corey Minyard393d2cc2005-11-07 00:59:54 -08001194 return rcvr;
1195 }
1196 return NULL;
1197}
1198
Corey Minyardc69c3122006-09-30 23:27:56 -07001199static int is_cmd_rcvr_exclusive(ipmi_smi_t intf,
1200 unsigned char netfn,
1201 unsigned char cmd,
1202 unsigned int chans)
1203{
1204 struct cmd_rcvr *rcvr;
1205
1206 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1207 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1208 && (rcvr->chans & chans))
1209 return 0;
1210 }
1211 return 1;
1212}
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214int ipmi_register_for_cmd(ipmi_user_t user,
1215 unsigned char netfn,
Corey Minyardc69c3122006-09-30 23:27:56 -07001216 unsigned char cmd,
1217 unsigned int chans)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Corey Minyard393d2cc2005-11-07 00:59:54 -08001219 ipmi_smi_t intf = user->intf;
1220 struct cmd_rcvr *rcvr;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001221 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223
1224 rcvr = kmalloc(sizeof(*rcvr), GFP_KERNEL);
Corey Minyard8a3628d2006-03-31 02:30:40 -08001225 if (!rcvr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 return -ENOMEM;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001227 rcvr->cmd = cmd;
1228 rcvr->netfn = netfn;
Corey Minyardc69c3122006-09-30 23:27:56 -07001229 rcvr->chans = chans;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001230 rcvr->user = user;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Corey Minyardd6dfd132006-03-31 02:30:41 -08001232 mutex_lock(&intf->cmd_rcvrs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 /* Make sure the command/netfn is not already registered. */
Corey Minyardc69c3122006-09-30 23:27:56 -07001234 if (!is_cmd_rcvr_exclusive(intf, netfn, cmd, chans)) {
Corey Minyard393d2cc2005-11-07 00:59:54 -08001235 rv = -EBUSY;
1236 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
1238
Corey Minyard393d2cc2005-11-07 00:59:54 -08001239 list_add_rcu(&rcvr->link, &intf->cmd_rcvrs);
Corey Minyard877197e2005-09-06 15:18:45 -07001240
Corey Minyard393d2cc2005-11-07 00:59:54 -08001241 out_unlock:
Corey Minyardd6dfd132006-03-31 02:30:41 -08001242 mutex_unlock(&intf->cmd_rcvrs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (rv)
1244 kfree(rcvr);
1245
1246 return rv;
1247}
Corey Minyardc70d7492008-04-29 01:01:09 -07001248EXPORT_SYMBOL(ipmi_register_for_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250int ipmi_unregister_for_cmd(ipmi_user_t user,
1251 unsigned char netfn,
Corey Minyardc69c3122006-09-30 23:27:56 -07001252 unsigned char cmd,
1253 unsigned int chans)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Corey Minyard393d2cc2005-11-07 00:59:54 -08001255 ipmi_smi_t intf = user->intf;
1256 struct cmd_rcvr *rcvr;
Corey Minyardc69c3122006-09-30 23:27:56 -07001257 struct cmd_rcvr *rcvrs = NULL;
1258 int i, rv = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Corey Minyardd6dfd132006-03-31 02:30:41 -08001260 mutex_lock(&intf->cmd_rcvrs_mutex);
Corey Minyardc69c3122006-09-30 23:27:56 -07001261 for (i = 0; i < IPMI_NUM_CHANNELS; i++) {
1262 if (((1 << i) & chans) == 0)
1263 continue;
1264 rcvr = find_cmd_rcvr(intf, netfn, cmd, i);
1265 if (rcvr == NULL)
1266 continue;
1267 if (rcvr->user == user) {
1268 rv = 0;
1269 rcvr->chans &= ~chans;
1270 if (rcvr->chans == 0) {
1271 list_del_rcu(&rcvr->link);
1272 rcvr->next = rcvrs;
1273 rcvrs = rcvr;
1274 }
1275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
Corey Minyardc69c3122006-09-30 23:27:56 -07001277 mutex_unlock(&intf->cmd_rcvrs_mutex);
1278 synchronize_rcu();
1279 while (rcvrs) {
1280 rcvr = rcvrs;
1281 rcvrs = rcvr->next;
1282 kfree(rcvr);
1283 }
1284 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
Corey Minyardc70d7492008-04-29 01:01:09 -07001286EXPORT_SYMBOL(ipmi_unregister_for_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288static unsigned char
1289ipmb_checksum(unsigned char *data, int size)
1290{
1291 unsigned char csum = 0;
Corey Minyardc70d7492008-04-29 01:01:09 -07001292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 for (; size > 0; size--, data++)
1294 csum += *data;
1295
1296 return -csum;
1297}
1298
1299static inline void format_ipmb_msg(struct ipmi_smi_msg *smi_msg,
1300 struct kernel_ipmi_msg *msg,
1301 struct ipmi_ipmb_addr *ipmb_addr,
1302 long msgid,
1303 unsigned char ipmb_seq,
1304 int broadcast,
1305 unsigned char source_address,
1306 unsigned char source_lun)
1307{
1308 int i = broadcast;
1309
1310 /* Format the IPMB header data. */
1311 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1312 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1313 smi_msg->data[2] = ipmb_addr->channel;
1314 if (broadcast)
1315 smi_msg->data[3] = 0;
1316 smi_msg->data[i+3] = ipmb_addr->slave_addr;
1317 smi_msg->data[i+4] = (msg->netfn << 2) | (ipmb_addr->lun & 0x3);
1318 smi_msg->data[i+5] = ipmb_checksum(&(smi_msg->data[i+3]), 2);
1319 smi_msg->data[i+6] = source_address;
1320 smi_msg->data[i+7] = (ipmb_seq << 2) | source_lun;
1321 smi_msg->data[i+8] = msg->cmd;
1322
1323 /* Now tack on the data to the message. */
1324 if (msg->data_len > 0)
1325 memcpy(&(smi_msg->data[i+9]), msg->data,
1326 msg->data_len);
1327 smi_msg->data_size = msg->data_len + 9;
1328
1329 /* Now calculate the checksum and tack it on. */
1330 smi_msg->data[i+smi_msg->data_size]
1331 = ipmb_checksum(&(smi_msg->data[i+6]),
1332 smi_msg->data_size-6);
1333
Corey Minyardc70d7492008-04-29 01:01:09 -07001334 /*
1335 * Add on the checksum size and the offset from the
1336 * broadcast.
1337 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 smi_msg->data_size += 1 + i;
1339
1340 smi_msg->msgid = msgid;
1341}
1342
1343static inline void format_lan_msg(struct ipmi_smi_msg *smi_msg,
1344 struct kernel_ipmi_msg *msg,
1345 struct ipmi_lan_addr *lan_addr,
1346 long msgid,
1347 unsigned char ipmb_seq,
1348 unsigned char source_lun)
1349{
1350 /* Format the IPMB header data. */
1351 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1352 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1353 smi_msg->data[2] = lan_addr->channel;
1354 smi_msg->data[3] = lan_addr->session_handle;
1355 smi_msg->data[4] = lan_addr->remote_SWID;
1356 smi_msg->data[5] = (msg->netfn << 2) | (lan_addr->lun & 0x3);
1357 smi_msg->data[6] = ipmb_checksum(&(smi_msg->data[4]), 2);
1358 smi_msg->data[7] = lan_addr->local_SWID;
1359 smi_msg->data[8] = (ipmb_seq << 2) | source_lun;
1360 smi_msg->data[9] = msg->cmd;
1361
1362 /* Now tack on the data to the message. */
1363 if (msg->data_len > 0)
1364 memcpy(&(smi_msg->data[10]), msg->data,
1365 msg->data_len);
1366 smi_msg->data_size = msg->data_len + 10;
1367
1368 /* Now calculate the checksum and tack it on. */
1369 smi_msg->data[smi_msg->data_size]
1370 = ipmb_checksum(&(smi_msg->data[7]),
1371 smi_msg->data_size-7);
1372
Corey Minyardc70d7492008-04-29 01:01:09 -07001373 /*
1374 * Add on the checksum size and the offset from the
1375 * broadcast.
1376 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 smi_msg->data_size += 1;
1378
1379 smi_msg->msgid = msgid;
1380}
1381
Corey Minyardc70d7492008-04-29 01:01:09 -07001382/*
1383 * Separate from ipmi_request so that the user does not have to be
1384 * supplied in certain circumstances (mainly at panic time). If
1385 * messages are supplied, they will be freed, even if an error
1386 * occurs.
1387 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08001388static int i_ipmi_request(ipmi_user_t user,
1389 ipmi_smi_t intf,
1390 struct ipmi_addr *addr,
1391 long msgid,
1392 struct kernel_ipmi_msg *msg,
1393 void *user_msg_data,
1394 void *supplied_smi,
1395 struct ipmi_recv_msg *supplied_recv,
1396 int priority,
1397 unsigned char source_address,
1398 unsigned char source_lun,
1399 int retries,
1400 unsigned int retry_time_ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
Corey Minyardb2c03942006-12-06 20:41:00 -08001402 int rv = 0;
1403 struct ipmi_smi_msg *smi_msg;
1404 struct ipmi_recv_msg *recv_msg;
1405 unsigned long flags;
1406 struct ipmi_smi_handlers *handlers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408
Corey Minyardc70d7492008-04-29 01:01:09 -07001409 if (supplied_recv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 recv_msg = supplied_recv;
Corey Minyardc70d7492008-04-29 01:01:09 -07001411 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 recv_msg = ipmi_alloc_recv_msg();
Corey Minyardc70d7492008-04-29 01:01:09 -07001413 if (recv_msg == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
1416 recv_msg->user_msg_data = user_msg_data;
1417
Corey Minyardc70d7492008-04-29 01:01:09 -07001418 if (supplied_smi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 smi_msg = (struct ipmi_smi_msg *) supplied_smi;
Corey Minyardc70d7492008-04-29 01:01:09 -07001420 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 smi_msg = ipmi_alloc_smi_msg();
1422 if (smi_msg == NULL) {
1423 ipmi_free_recv_msg(recv_msg);
1424 return -ENOMEM;
1425 }
1426 }
1427
Corey Minyardb2c03942006-12-06 20:41:00 -08001428 rcu_read_lock();
1429 handlers = intf->handlers;
1430 if (!handlers) {
1431 rv = -ENODEV;
1432 goto out_err;
1433 }
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 recv_msg->user = user;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001436 if (user)
1437 kref_get(&user->refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 recv_msg->msgid = msgid;
Corey Minyardc70d7492008-04-29 01:01:09 -07001439 /*
1440 * Store the message to send in the receive message so timeout
1441 * responses can get the proper response data.
1442 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 recv_msg->msg = *msg;
1444
1445 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
1446 struct ipmi_system_interface_addr *smi_addr;
1447
1448 if (msg->netfn & 1) {
1449 /* Responses are not allowed to the SMI. */
1450 rv = -EINVAL;
1451 goto out_err;
1452 }
1453
1454 smi_addr = (struct ipmi_system_interface_addr *) addr;
1455 if (smi_addr->lun > 3) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001456 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 rv = -EINVAL;
1458 goto out_err;
1459 }
1460
1461 memcpy(&recv_msg->addr, smi_addr, sizeof(*smi_addr));
1462
1463 if ((msg->netfn == IPMI_NETFN_APP_REQUEST)
1464 && ((msg->cmd == IPMI_SEND_MSG_CMD)
1465 || (msg->cmd == IPMI_GET_MSG_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07001466 || (msg->cmd == IPMI_READ_EVENT_MSG_BUFFER_CMD))) {
1467 /*
1468 * We don't let the user do these, since we manage
1469 * the sequence numbers.
1470 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001471 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 rv = -EINVAL;
1473 goto out_err;
1474 }
1475
Corey Minyardb9675132006-12-06 20:41:02 -08001476 if (((msg->netfn == IPMI_NETFN_APP_REQUEST)
1477 && ((msg->cmd == IPMI_COLD_RESET_CMD)
1478 || (msg->cmd == IPMI_WARM_RESET_CMD)))
Corey Minyardc70d7492008-04-29 01:01:09 -07001479 || (msg->netfn == IPMI_NETFN_FIRMWARE_REQUEST)) {
Corey Minyardb9675132006-12-06 20:41:02 -08001480 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1481 intf->auto_maintenance_timeout
1482 = IPMI_MAINTENANCE_MODE_TIMEOUT;
1483 if (!intf->maintenance_mode
Corey Minyardc70d7492008-04-29 01:01:09 -07001484 && !intf->maintenance_mode_enable) {
Corey Minyardb9675132006-12-06 20:41:02 -08001485 intf->maintenance_mode_enable = 1;
1486 maintenance_mode_update(intf);
1487 }
1488 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
1489 flags);
1490 }
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 if ((msg->data_len + 2) > IPMI_MAX_MSG_LENGTH) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001493 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 rv = -EMSGSIZE;
1495 goto out_err;
1496 }
1497
1498 smi_msg->data[0] = (msg->netfn << 2) | (smi_addr->lun & 0x3);
1499 smi_msg->data[1] = msg->cmd;
1500 smi_msg->msgid = msgid;
1501 smi_msg->user_data = recv_msg;
1502 if (msg->data_len > 0)
1503 memcpy(&(smi_msg->data[2]), msg->data, msg->data_len);
1504 smi_msg->data_size = msg->data_len + 2;
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001505 ipmi_inc_stat(intf, sent_local_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 } else if ((addr->addr_type == IPMI_IPMB_ADDR_TYPE)
Corey Minyardc70d7492008-04-29 01:01:09 -07001507 || (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 struct ipmi_ipmb_addr *ipmb_addr;
1509 unsigned char ipmb_seq;
1510 long seqid;
1511 int broadcast = 0;
1512
KAMBAROV, ZAUR9c101fd2005-06-28 20:45:08 -07001513 if (addr->channel >= IPMI_MAX_CHANNELS) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001514 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 rv = -EINVAL;
1516 goto out_err;
1517 }
1518
1519 if (intf->channels[addr->channel].medium
Corey Minyardc70d7492008-04-29 01:01:09 -07001520 != IPMI_CHANNEL_MEDIUM_IPMB) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001521 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 rv = -EINVAL;
1523 goto out_err;
1524 }
1525
1526 if (retries < 0) {
1527 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE)
1528 retries = 0; /* Don't retry broadcasts. */
1529 else
1530 retries = 4;
1531 }
1532 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001533 /*
1534 * Broadcasts add a zero at the beginning of the
1535 * message, but otherwise is the same as an IPMB
1536 * address.
1537 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 addr->addr_type = IPMI_IPMB_ADDR_TYPE;
1539 broadcast = 1;
1540 }
1541
1542
1543 /* Default to 1 second retries. */
1544 if (retry_time_ms == 0)
1545 retry_time_ms = 1000;
1546
Corey Minyardc70d7492008-04-29 01:01:09 -07001547 /*
1548 * 9 for the header and 1 for the checksum, plus
1549 * possibly one for the broadcast.
1550 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if ((msg->data_len + 10 + broadcast) > IPMI_MAX_MSG_LENGTH) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001552 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 rv = -EMSGSIZE;
1554 goto out_err;
1555 }
1556
1557 ipmb_addr = (struct ipmi_ipmb_addr *) addr;
1558 if (ipmb_addr->lun > 3) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001559 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 rv = -EINVAL;
1561 goto out_err;
1562 }
1563
1564 memcpy(&recv_msg->addr, ipmb_addr, sizeof(*ipmb_addr));
1565
1566 if (recv_msg->msg.netfn & 0x1) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001567 /*
1568 * It's a response, so use the user's sequence
1569 * from msgid.
1570 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001571 ipmi_inc_stat(intf, sent_ipmb_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 format_ipmb_msg(smi_msg, msg, ipmb_addr, msgid,
1573 msgid, broadcast,
1574 source_address, source_lun);
1575
Corey Minyardc70d7492008-04-29 01:01:09 -07001576 /*
1577 * Save the receive message so we can use it
1578 * to deliver the response.
1579 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 smi_msg->user_data = recv_msg;
1581 } else {
1582 /* It's a command, so get a sequence for it. */
1583
1584 spin_lock_irqsave(&(intf->seq_lock), flags);
1585
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001586 ipmi_inc_stat(intf, sent_ipmb_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Corey Minyardc70d7492008-04-29 01:01:09 -07001588 /*
1589 * Create a sequence number with a 1 second
1590 * timeout and 4 retries.
1591 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 rv = intf_next_seq(intf,
1593 recv_msg,
1594 retry_time_ms,
1595 retries,
1596 broadcast,
1597 &ipmb_seq,
1598 &seqid);
1599 if (rv) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001600 /*
1601 * We have used up all the sequence numbers,
1602 * probably, so abort.
1603 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 spin_unlock_irqrestore(&(intf->seq_lock),
1605 flags);
1606 goto out_err;
1607 }
1608
Corey Minyardc70d7492008-04-29 01:01:09 -07001609 /*
1610 * Store the sequence number in the message,
1611 * so that when the send message response
1612 * comes back we can start the timer.
1613 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 format_ipmb_msg(smi_msg, msg, ipmb_addr,
1615 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1616 ipmb_seq, broadcast,
1617 source_address, source_lun);
1618
Corey Minyardc70d7492008-04-29 01:01:09 -07001619 /*
1620 * Copy the message into the recv message data, so we
1621 * can retransmit it later if necessary.
1622 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 memcpy(recv_msg->msg_data, smi_msg->data,
1624 smi_msg->data_size);
1625 recv_msg->msg.data = recv_msg->msg_data;
1626 recv_msg->msg.data_len = smi_msg->data_size;
1627
Corey Minyardc70d7492008-04-29 01:01:09 -07001628 /*
1629 * We don't unlock until here, because we need
1630 * to copy the completed message into the
1631 * recv_msg before we release the lock.
1632 * Otherwise, race conditions may bite us. I
1633 * know that's pretty paranoid, but I prefer
1634 * to be correct.
1635 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1637 }
1638 } else if (addr->addr_type == IPMI_LAN_ADDR_TYPE) {
1639 struct ipmi_lan_addr *lan_addr;
1640 unsigned char ipmb_seq;
1641 long seqid;
1642
Jayachandran C12fc1d72006-02-03 03:04:51 -08001643 if (addr->channel >= IPMI_MAX_CHANNELS) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001644 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 rv = -EINVAL;
1646 goto out_err;
1647 }
1648
1649 if ((intf->channels[addr->channel].medium
Corey Minyardc70d7492008-04-29 01:01:09 -07001650 != IPMI_CHANNEL_MEDIUM_8023LAN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 && (intf->channels[addr->channel].medium
Corey Minyardc70d7492008-04-29 01:01:09 -07001652 != IPMI_CHANNEL_MEDIUM_ASYNC)) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001653 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 rv = -EINVAL;
1655 goto out_err;
1656 }
1657
1658 retries = 4;
1659
1660 /* Default to 1 second retries. */
1661 if (retry_time_ms == 0)
1662 retry_time_ms = 1000;
1663
1664 /* 11 for the header and 1 for the checksum. */
1665 if ((msg->data_len + 12) > IPMI_MAX_MSG_LENGTH) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001666 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 rv = -EMSGSIZE;
1668 goto out_err;
1669 }
1670
1671 lan_addr = (struct ipmi_lan_addr *) addr;
1672 if (lan_addr->lun > 3) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001673 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 rv = -EINVAL;
1675 goto out_err;
1676 }
1677
1678 memcpy(&recv_msg->addr, lan_addr, sizeof(*lan_addr));
1679
1680 if (recv_msg->msg.netfn & 0x1) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001681 /*
1682 * It's a response, so use the user's sequence
1683 * from msgid.
1684 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001685 ipmi_inc_stat(intf, sent_lan_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 format_lan_msg(smi_msg, msg, lan_addr, msgid,
1687 msgid, source_lun);
1688
Corey Minyardc70d7492008-04-29 01:01:09 -07001689 /*
1690 * Save the receive message so we can use it
1691 * to deliver the response.
1692 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 smi_msg->user_data = recv_msg;
1694 } else {
1695 /* It's a command, so get a sequence for it. */
1696
1697 spin_lock_irqsave(&(intf->seq_lock), flags);
1698
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001699 ipmi_inc_stat(intf, sent_lan_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Corey Minyardc70d7492008-04-29 01:01:09 -07001701 /*
1702 * Create a sequence number with a 1 second
1703 * timeout and 4 retries.
1704 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 rv = intf_next_seq(intf,
1706 recv_msg,
1707 retry_time_ms,
1708 retries,
1709 0,
1710 &ipmb_seq,
1711 &seqid);
1712 if (rv) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001713 /*
1714 * We have used up all the sequence numbers,
1715 * probably, so abort.
1716 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 spin_unlock_irqrestore(&(intf->seq_lock),
1718 flags);
1719 goto out_err;
1720 }
1721
Corey Minyardc70d7492008-04-29 01:01:09 -07001722 /*
1723 * Store the sequence number in the message,
1724 * so that when the send message response
1725 * comes back we can start the timer.
1726 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 format_lan_msg(smi_msg, msg, lan_addr,
1728 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1729 ipmb_seq, source_lun);
1730
Corey Minyardc70d7492008-04-29 01:01:09 -07001731 /*
1732 * Copy the message into the recv message data, so we
1733 * can retransmit it later if necessary.
1734 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 memcpy(recv_msg->msg_data, smi_msg->data,
1736 smi_msg->data_size);
1737 recv_msg->msg.data = recv_msg->msg_data;
1738 recv_msg->msg.data_len = smi_msg->data_size;
1739
Corey Minyardc70d7492008-04-29 01:01:09 -07001740 /*
1741 * We don't unlock until here, because we need
1742 * to copy the completed message into the
1743 * recv_msg before we release the lock.
1744 * Otherwise, race conditions may bite us. I
1745 * know that's pretty paranoid, but I prefer
1746 * to be correct.
1747 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1749 }
1750 } else {
1751 /* Unknown address type. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001752 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 rv = -EINVAL;
1754 goto out_err;
1755 }
1756
1757#ifdef DEBUG_MSGING
1758 {
1759 int m;
Corey Minyarde8b33612005-09-06 15:18:45 -07001760 for (m = 0; m < smi_msg->data_size; m++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 printk(" %2.2x", smi_msg->data[m]);
1762 printk("\n");
1763 }
1764#endif
Corey Minyardb2c03942006-12-06 20:41:00 -08001765
1766 handlers->sender(intf->send_info, smi_msg, priority);
1767 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 return 0;
1770
1771 out_err:
Corey Minyardb2c03942006-12-06 20:41:00 -08001772 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 ipmi_free_smi_msg(smi_msg);
1774 ipmi_free_recv_msg(recv_msg);
1775 return rv;
1776}
1777
Corey Minyardc14979b2005-09-06 15:18:38 -07001778static int check_addr(ipmi_smi_t intf,
1779 struct ipmi_addr *addr,
1780 unsigned char *saddr,
1781 unsigned char *lun)
1782{
1783 if (addr->channel >= IPMI_MAX_CHANNELS)
1784 return -EINVAL;
1785 *lun = intf->channels[addr->channel].lun;
1786 *saddr = intf->channels[addr->channel].address;
1787 return 0;
1788}
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790int ipmi_request_settime(ipmi_user_t user,
1791 struct ipmi_addr *addr,
1792 long msgid,
1793 struct kernel_ipmi_msg *msg,
1794 void *user_msg_data,
1795 int priority,
1796 int retries,
1797 unsigned int retry_time_ms)
1798{
Corey Minyardc14979b2005-09-06 15:18:38 -07001799 unsigned char saddr, lun;
1800 int rv;
1801
Corey Minyard8a3628d2006-03-31 02:30:40 -08001802 if (!user)
Corey Minyard56a55ec2005-09-06 15:18:42 -07001803 return -EINVAL;
Corey Minyardc14979b2005-09-06 15:18:38 -07001804 rv = check_addr(user->intf, addr, &saddr, &lun);
1805 if (rv)
1806 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return i_ipmi_request(user,
1808 user->intf,
1809 addr,
1810 msgid,
1811 msg,
1812 user_msg_data,
1813 NULL, NULL,
1814 priority,
Corey Minyardc14979b2005-09-06 15:18:38 -07001815 saddr,
1816 lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 retries,
1818 retry_time_ms);
1819}
Corey Minyardc70d7492008-04-29 01:01:09 -07001820EXPORT_SYMBOL(ipmi_request_settime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
1822int ipmi_request_supply_msgs(ipmi_user_t user,
1823 struct ipmi_addr *addr,
1824 long msgid,
1825 struct kernel_ipmi_msg *msg,
1826 void *user_msg_data,
1827 void *supplied_smi,
1828 struct ipmi_recv_msg *supplied_recv,
1829 int priority)
1830{
Corey Minyardc14979b2005-09-06 15:18:38 -07001831 unsigned char saddr, lun;
1832 int rv;
1833
Corey Minyard8a3628d2006-03-31 02:30:40 -08001834 if (!user)
Corey Minyard56a55ec2005-09-06 15:18:42 -07001835 return -EINVAL;
Corey Minyardc14979b2005-09-06 15:18:38 -07001836 rv = check_addr(user->intf, addr, &saddr, &lun);
1837 if (rv)
1838 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 return i_ipmi_request(user,
1840 user->intf,
1841 addr,
1842 msgid,
1843 msg,
1844 user_msg_data,
1845 supplied_smi,
1846 supplied_recv,
1847 priority,
Corey Minyardc14979b2005-09-06 15:18:38 -07001848 saddr,
1849 lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 -1, 0);
1851}
Corey Minyardc70d7492008-04-29 01:01:09 -07001852EXPORT_SYMBOL(ipmi_request_supply_msgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Randy Dunlap1aa16ee2006-12-06 20:41:20 -08001854#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855static int ipmb_file_read_proc(char *page, char **start, off_t off,
1856 int count, int *eof, void *data)
1857{
1858 char *out = (char *) page;
1859 ipmi_smi_t intf = data;
Corey Minyardc14979b2005-09-06 15:18:38 -07001860 int i;
Corey Minyard8a3628d2006-03-31 02:30:40 -08001861 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Corey Minyarde8b33612005-09-06 15:18:45 -07001863 for (i = 0; i < IPMI_MAX_CHANNELS; i++)
Corey Minyardc14979b2005-09-06 15:18:38 -07001864 rv += sprintf(out+rv, "%x ", intf->channels[i].address);
1865 out[rv-1] = '\n'; /* Replace the final space with a newline */
1866 out[rv] = '\0';
1867 rv++;
1868 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869}
1870
1871static int version_file_read_proc(char *page, char **start, off_t off,
1872 int count, int *eof, void *data)
1873{
1874 char *out = (char *) page;
1875 ipmi_smi_t intf = data;
1876
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001877 return sprintf(out, "%u.%u\n",
Corey Minyard50c812b2006-03-26 01:37:21 -08001878 ipmi_version_major(&intf->bmc->id),
1879 ipmi_version_minor(&intf->bmc->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
1881
1882static int stat_file_read_proc(char *page, char **start, off_t off,
1883 int count, int *eof, void *data)
1884{
1885 char *out = (char *) page;
1886 ipmi_smi_t intf = data;
1887
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001888 out += sprintf(out, "sent_invalid_commands: %u\n",
1889 ipmi_get_stat(intf, sent_invalid_commands));
1890 out += sprintf(out, "sent_local_commands: %u\n",
1891 ipmi_get_stat(intf, sent_local_commands));
1892 out += sprintf(out, "handled_local_responses: %u\n",
1893 ipmi_get_stat(intf, handled_local_responses));
1894 out += sprintf(out, "unhandled_local_responses: %u\n",
1895 ipmi_get_stat(intf, unhandled_local_responses));
1896 out += sprintf(out, "sent_ipmb_commands: %u\n",
1897 ipmi_get_stat(intf, sent_ipmb_commands));
1898 out += sprintf(out, "sent_ipmb_command_errs: %u\n",
1899 ipmi_get_stat(intf, sent_ipmb_command_errs));
1900 out += sprintf(out, "retransmitted_ipmb_commands: %u\n",
1901 ipmi_get_stat(intf, retransmitted_ipmb_commands));
1902 out += sprintf(out, "timed_out_ipmb_commands: %u\n",
1903 ipmi_get_stat(intf, timed_out_ipmb_commands));
1904 out += sprintf(out, "timed_out_ipmb_broadcasts: %u\n",
1905 ipmi_get_stat(intf, timed_out_ipmb_broadcasts));
1906 out += sprintf(out, "sent_ipmb_responses: %u\n",
1907 ipmi_get_stat(intf, sent_ipmb_responses));
1908 out += sprintf(out, "handled_ipmb_responses: %u\n",
1909 ipmi_get_stat(intf, handled_ipmb_responses));
1910 out += sprintf(out, "invalid_ipmb_responses: %u\n",
1911 ipmi_get_stat(intf, invalid_ipmb_responses));
1912 out += sprintf(out, "unhandled_ipmb_responses: %u\n",
1913 ipmi_get_stat(intf, unhandled_ipmb_responses));
1914 out += sprintf(out, "sent_lan_commands: %u\n",
1915 ipmi_get_stat(intf, sent_lan_commands));
1916 out += sprintf(out, "sent_lan_command_errs: %u\n",
1917 ipmi_get_stat(intf, sent_lan_command_errs));
1918 out += sprintf(out, "retransmitted_lan_commands: %u\n",
1919 ipmi_get_stat(intf, retransmitted_lan_commands));
1920 out += sprintf(out, "timed_out_lan_commands: %u\n",
1921 ipmi_get_stat(intf, timed_out_lan_commands));
1922 out += sprintf(out, "sent_lan_responses: %u\n",
1923 ipmi_get_stat(intf, sent_lan_responses));
1924 out += sprintf(out, "handled_lan_responses: %u\n",
1925 ipmi_get_stat(intf, handled_lan_responses));
1926 out += sprintf(out, "invalid_lan_responses: %u\n",
1927 ipmi_get_stat(intf, invalid_lan_responses));
1928 out += sprintf(out, "unhandled_lan_responses: %u\n",
1929 ipmi_get_stat(intf, unhandled_lan_responses));
1930 out += sprintf(out, "handled_commands: %u\n",
1931 ipmi_get_stat(intf, handled_commands));
1932 out += sprintf(out, "invalid_commands: %u\n",
1933 ipmi_get_stat(intf, invalid_commands));
1934 out += sprintf(out, "unhandled_commands: %u\n",
1935 ipmi_get_stat(intf, unhandled_commands));
1936 out += sprintf(out, "invalid_events: %u\n",
1937 ipmi_get_stat(intf, invalid_events));
1938 out += sprintf(out, "events: %u\n",
1939 ipmi_get_stat(intf, events));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
1941 return (out - ((char *) page));
1942}
Randy Dunlap1aa16ee2006-12-06 20:41:20 -08001943#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
1945int ipmi_smi_add_proc_entry(ipmi_smi_t smi, char *name,
Alexey Dobriyanfa68be02008-04-29 01:01:13 -07001946 read_proc_t *read_proc,
Alexey Dobriyan99b76232009-03-25 22:48:06 +03001947 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 int rv = 0;
Corey Minyard3b625942005-06-23 22:01:42 -07001950#ifdef CONFIG_PROC_FS
1951 struct proc_dir_entry *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 struct ipmi_proc_entry *entry;
1953
1954 /* Create a list element. */
1955 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1956 if (!entry)
1957 return -ENOMEM;
1958 entry->name = kmalloc(strlen(name)+1, GFP_KERNEL);
1959 if (!entry->name) {
1960 kfree(entry);
1961 return -ENOMEM;
1962 }
1963 strcpy(entry->name, name);
1964
1965 file = create_proc_entry(name, 0, smi->proc_dir);
1966 if (!file) {
1967 kfree(entry->name);
1968 kfree(entry);
1969 rv = -ENOMEM;
1970 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 file->data = data;
1972 file->read_proc = read_proc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Corey Minyardac019152007-10-18 03:07:11 -07001974 mutex_lock(&smi->proc_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 /* Stick it on the list. */
1976 entry->next = smi->proc_entries;
1977 smi->proc_entries = entry;
Corey Minyardac019152007-10-18 03:07:11 -07001978 mutex_unlock(&smi->proc_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 }
Corey Minyard3b625942005-06-23 22:01:42 -07001980#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982 return rv;
1983}
Corey Minyardc70d7492008-04-29 01:01:09 -07001984EXPORT_SYMBOL(ipmi_smi_add_proc_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986static int add_proc_entries(ipmi_smi_t smi, int num)
1987{
1988 int rv = 0;
1989
Corey Minyard3b625942005-06-23 22:01:42 -07001990#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 sprintf(smi->proc_dir_name, "%d", num);
1992 smi->proc_dir = proc_mkdir(smi->proc_dir_name, proc_ipmi_root);
1993 if (!smi->proc_dir)
1994 rv = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 if (rv == 0)
1997 rv = ipmi_smi_add_proc_entry(smi, "stats",
Alexey Dobriyanfa68be02008-04-29 01:01:13 -07001998 stat_file_read_proc,
Alexey Dobriyan99b76232009-03-25 22:48:06 +03001999 smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001 if (rv == 0)
2002 rv = ipmi_smi_add_proc_entry(smi, "ipmb",
Alexey Dobriyanfa68be02008-04-29 01:01:13 -07002003 ipmb_file_read_proc,
Alexey Dobriyan99b76232009-03-25 22:48:06 +03002004 smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006 if (rv == 0)
2007 rv = ipmi_smi_add_proc_entry(smi, "version",
Alexey Dobriyanfa68be02008-04-29 01:01:13 -07002008 version_file_read_proc,
Alexey Dobriyan99b76232009-03-25 22:48:06 +03002009 smi);
Corey Minyard3b625942005-06-23 22:01:42 -07002010#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
2012 return rv;
2013}
2014
2015static void remove_proc_entries(ipmi_smi_t smi)
2016{
Corey Minyard3b625942005-06-23 22:01:42 -07002017#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 struct ipmi_proc_entry *entry;
2019
Corey Minyardac019152007-10-18 03:07:11 -07002020 mutex_lock(&smi->proc_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 while (smi->proc_entries) {
2022 entry = smi->proc_entries;
2023 smi->proc_entries = entry->next;
2024
2025 remove_proc_entry(entry->name, smi->proc_dir);
2026 kfree(entry->name);
2027 kfree(entry);
2028 }
Corey Minyardac019152007-10-18 03:07:11 -07002029 mutex_unlock(&smi->proc_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 remove_proc_entry(smi->proc_dir_name, proc_ipmi_root);
Corey Minyard3b625942005-06-23 22:01:42 -07002031#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
Corey Minyard50c812b2006-03-26 01:37:21 -08002034static int __find_bmc_guid(struct device *dev, void *data)
2035{
2036 unsigned char *id = data;
2037 struct bmc_device *bmc = dev_get_drvdata(dev);
2038 return memcmp(bmc->guid, id, 16) == 0;
2039}
2040
2041static struct bmc_device *ipmi_find_bmc_guid(struct device_driver *drv,
2042 unsigned char *guid)
2043{
2044 struct device *dev;
2045
2046 dev = driver_find_device(drv, NULL, guid, __find_bmc_guid);
2047 if (dev)
2048 return dev_get_drvdata(dev);
2049 else
2050 return NULL;
2051}
2052
2053struct prod_dev_id {
2054 unsigned int product_id;
2055 unsigned char device_id;
2056};
2057
2058static int __find_bmc_prod_dev_id(struct device *dev, void *data)
2059{
2060 struct prod_dev_id *id = data;
2061 struct bmc_device *bmc = dev_get_drvdata(dev);
2062
2063 return (bmc->id.product_id == id->product_id
Corey Minyard50c812b2006-03-26 01:37:21 -08002064 && bmc->id.device_id == id->device_id);
2065}
2066
2067static struct bmc_device *ipmi_find_bmc_prod_dev_id(
2068 struct device_driver *drv,
Corey Minyardf0b55da2006-12-06 20:40:54 -08002069 unsigned int product_id, unsigned char device_id)
Corey Minyard50c812b2006-03-26 01:37:21 -08002070{
2071 struct prod_dev_id id = {
2072 .product_id = product_id,
2073 .device_id = device_id,
2074 };
2075 struct device *dev;
2076
2077 dev = driver_find_device(drv, NULL, &id, __find_bmc_prod_dev_id);
2078 if (dev)
2079 return dev_get_drvdata(dev);
2080 else
2081 return NULL;
2082}
2083
2084static ssize_t device_id_show(struct device *dev,
2085 struct device_attribute *attr,
2086 char *buf)
2087{
2088 struct bmc_device *bmc = dev_get_drvdata(dev);
2089
2090 return snprintf(buf, 10, "%u\n", bmc->id.device_id);
2091}
2092
2093static ssize_t provides_dev_sdrs_show(struct device *dev,
2094 struct device_attribute *attr,
2095 char *buf)
2096{
2097 struct bmc_device *bmc = dev_get_drvdata(dev);
2098
2099 return snprintf(buf, 10, "%u\n",
Corey Minyard7947d2c2006-11-10 12:27:50 -08002100 (bmc->id.device_revision & 0x80) >> 7);
Corey Minyard50c812b2006-03-26 01:37:21 -08002101}
2102
2103static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
2104 char *buf)
2105{
2106 struct bmc_device *bmc = dev_get_drvdata(dev);
2107
2108 return snprintf(buf, 20, "%u\n",
Corey Minyard7947d2c2006-11-10 12:27:50 -08002109 bmc->id.device_revision & 0x0F);
Corey Minyard50c812b2006-03-26 01:37:21 -08002110}
2111
2112static ssize_t firmware_rev_show(struct device *dev,
2113 struct device_attribute *attr,
2114 char *buf)
2115{
2116 struct bmc_device *bmc = dev_get_drvdata(dev);
2117
2118 return snprintf(buf, 20, "%u.%x\n", bmc->id.firmware_revision_1,
2119 bmc->id.firmware_revision_2);
2120}
2121
2122static ssize_t ipmi_version_show(struct device *dev,
2123 struct device_attribute *attr,
2124 char *buf)
2125{
2126 struct bmc_device *bmc = dev_get_drvdata(dev);
2127
2128 return snprintf(buf, 20, "%u.%u\n",
2129 ipmi_version_major(&bmc->id),
2130 ipmi_version_minor(&bmc->id));
2131}
2132
2133static ssize_t add_dev_support_show(struct device *dev,
2134 struct device_attribute *attr,
2135 char *buf)
2136{
2137 struct bmc_device *bmc = dev_get_drvdata(dev);
2138
2139 return snprintf(buf, 10, "0x%02x\n",
2140 bmc->id.additional_device_support);
2141}
2142
2143static ssize_t manufacturer_id_show(struct device *dev,
2144 struct device_attribute *attr,
2145 char *buf)
2146{
2147 struct bmc_device *bmc = dev_get_drvdata(dev);
2148
2149 return snprintf(buf, 20, "0x%6.6x\n", bmc->id.manufacturer_id);
2150}
2151
2152static ssize_t product_id_show(struct device *dev,
2153 struct device_attribute *attr,
2154 char *buf)
2155{
2156 struct bmc_device *bmc = dev_get_drvdata(dev);
2157
2158 return snprintf(buf, 10, "0x%4.4x\n", bmc->id.product_id);
2159}
2160
2161static ssize_t aux_firmware_rev_show(struct device *dev,
2162 struct device_attribute *attr,
2163 char *buf)
2164{
2165 struct bmc_device *bmc = dev_get_drvdata(dev);
2166
2167 return snprintf(buf, 21, "0x%02x 0x%02x 0x%02x 0x%02x\n",
2168 bmc->id.aux_firmware_revision[3],
2169 bmc->id.aux_firmware_revision[2],
2170 bmc->id.aux_firmware_revision[1],
2171 bmc->id.aux_firmware_revision[0]);
2172}
2173
2174static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
2175 char *buf)
2176{
2177 struct bmc_device *bmc = dev_get_drvdata(dev);
2178
2179 return snprintf(buf, 100, "%Lx%Lx\n",
2180 (long long) bmc->guid[0],
2181 (long long) bmc->guid[8]);
2182}
2183
Jeff Garzik5e593932006-10-11 01:22:21 -07002184static void remove_files(struct bmc_device *bmc)
Corey Minyard50c812b2006-03-26 01:37:21 -08002185{
Corey Minyardf0b55da2006-12-06 20:40:54 -08002186 if (!bmc->dev)
2187 return;
2188
Corey Minyard50c812b2006-03-26 01:37:21 -08002189 device_remove_file(&bmc->dev->dev,
2190 &bmc->device_id_attr);
2191 device_remove_file(&bmc->dev->dev,
2192 &bmc->provides_dev_sdrs_attr);
2193 device_remove_file(&bmc->dev->dev,
2194 &bmc->revision_attr);
2195 device_remove_file(&bmc->dev->dev,
2196 &bmc->firmware_rev_attr);
2197 device_remove_file(&bmc->dev->dev,
2198 &bmc->version_attr);
2199 device_remove_file(&bmc->dev->dev,
2200 &bmc->add_dev_support_attr);
2201 device_remove_file(&bmc->dev->dev,
2202 &bmc->manufacturer_id_attr);
2203 device_remove_file(&bmc->dev->dev,
2204 &bmc->product_id_attr);
Jeff Garzik5e593932006-10-11 01:22:21 -07002205
Corey Minyard50c812b2006-03-26 01:37:21 -08002206 if (bmc->id.aux_firmware_revision_set)
2207 device_remove_file(&bmc->dev->dev,
2208 &bmc->aux_firmware_rev_attr);
2209 if (bmc->guid_set)
2210 device_remove_file(&bmc->dev->dev,
2211 &bmc->guid_attr);
Jeff Garzik5e593932006-10-11 01:22:21 -07002212}
2213
2214static void
2215cleanup_bmc_device(struct kref *ref)
2216{
2217 struct bmc_device *bmc;
2218
2219 bmc = container_of(ref, struct bmc_device, refcount);
2220
2221 remove_files(bmc);
Corey Minyard1d5636c2006-12-10 02:19:08 -08002222 platform_device_unregister(bmc->dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002223 kfree(bmc);
2224}
2225
2226static void ipmi_bmc_unregister(ipmi_smi_t intf)
2227{
2228 struct bmc_device *bmc = intf->bmc;
2229
Corey Minyard759643b2006-12-06 20:40:59 -08002230 if (intf->sysfs_name) {
2231 sysfs_remove_link(&intf->si_dev->kobj, intf->sysfs_name);
2232 kfree(intf->sysfs_name);
2233 intf->sysfs_name = NULL;
2234 }
Corey Minyard50c812b2006-03-26 01:37:21 -08002235 if (intf->my_dev_name) {
2236 sysfs_remove_link(&bmc->dev->dev.kobj, intf->my_dev_name);
2237 kfree(intf->my_dev_name);
2238 intf->my_dev_name = NULL;
2239 }
2240
2241 mutex_lock(&ipmidriver_mutex);
2242 kref_put(&bmc->refcount, cleanup_bmc_device);
Corey Minyardf0b55da2006-12-06 20:40:54 -08002243 intf->bmc = NULL;
Corey Minyard50c812b2006-03-26 01:37:21 -08002244 mutex_unlock(&ipmidriver_mutex);
2245}
2246
Jeff Garzik5e593932006-10-11 01:22:21 -07002247static int create_files(struct bmc_device *bmc)
2248{
2249 int err;
2250
Corey Minyardf0b55da2006-12-06 20:40:54 -08002251 bmc->device_id_attr.attr.name = "device_id";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002252 bmc->device_id_attr.attr.mode = S_IRUGO;
2253 bmc->device_id_attr.show = device_id_show;
2254
2255 bmc->provides_dev_sdrs_attr.attr.name = "provides_device_sdrs";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002256 bmc->provides_dev_sdrs_attr.attr.mode = S_IRUGO;
2257 bmc->provides_dev_sdrs_attr.show = provides_dev_sdrs_show;
2258
2259 bmc->revision_attr.attr.name = "revision";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002260 bmc->revision_attr.attr.mode = S_IRUGO;
2261 bmc->revision_attr.show = revision_show;
2262
2263 bmc->firmware_rev_attr.attr.name = "firmware_revision";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002264 bmc->firmware_rev_attr.attr.mode = S_IRUGO;
2265 bmc->firmware_rev_attr.show = firmware_rev_show;
2266
2267 bmc->version_attr.attr.name = "ipmi_version";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002268 bmc->version_attr.attr.mode = S_IRUGO;
2269 bmc->version_attr.show = ipmi_version_show;
2270
2271 bmc->add_dev_support_attr.attr.name = "additional_device_support";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002272 bmc->add_dev_support_attr.attr.mode = S_IRUGO;
2273 bmc->add_dev_support_attr.show = add_dev_support_show;
2274
2275 bmc->manufacturer_id_attr.attr.name = "manufacturer_id";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002276 bmc->manufacturer_id_attr.attr.mode = S_IRUGO;
2277 bmc->manufacturer_id_attr.show = manufacturer_id_show;
2278
2279 bmc->product_id_attr.attr.name = "product_id";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002280 bmc->product_id_attr.attr.mode = S_IRUGO;
2281 bmc->product_id_attr.show = product_id_show;
2282
2283 bmc->guid_attr.attr.name = "guid";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002284 bmc->guid_attr.attr.mode = S_IRUGO;
2285 bmc->guid_attr.show = guid_show;
2286
2287 bmc->aux_firmware_rev_attr.attr.name = "aux_firmware_revision";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002288 bmc->aux_firmware_rev_attr.attr.mode = S_IRUGO;
2289 bmc->aux_firmware_rev_attr.show = aux_firmware_rev_show;
2290
Jeff Garzik5e593932006-10-11 01:22:21 -07002291 err = device_create_file(&bmc->dev->dev,
2292 &bmc->device_id_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002293 if (err)
2294 goto out;
Jeff Garzik5e593932006-10-11 01:22:21 -07002295 err = device_create_file(&bmc->dev->dev,
2296 &bmc->provides_dev_sdrs_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002297 if (err)
2298 goto out_devid;
Jeff Garzik5e593932006-10-11 01:22:21 -07002299 err = device_create_file(&bmc->dev->dev,
2300 &bmc->revision_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002301 if (err)
2302 goto out_sdrs;
Jeff Garzik5e593932006-10-11 01:22:21 -07002303 err = device_create_file(&bmc->dev->dev,
2304 &bmc->firmware_rev_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002305 if (err)
2306 goto out_rev;
Jeff Garzik5e593932006-10-11 01:22:21 -07002307 err = device_create_file(&bmc->dev->dev,
2308 &bmc->version_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002309 if (err)
2310 goto out_firm;
Jeff Garzik5e593932006-10-11 01:22:21 -07002311 err = device_create_file(&bmc->dev->dev,
2312 &bmc->add_dev_support_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002313 if (err)
2314 goto out_version;
Jeff Garzik5e593932006-10-11 01:22:21 -07002315 err = device_create_file(&bmc->dev->dev,
2316 &bmc->manufacturer_id_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002317 if (err)
2318 goto out_add_dev;
Jeff Garzik5e593932006-10-11 01:22:21 -07002319 err = device_create_file(&bmc->dev->dev,
2320 &bmc->product_id_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002321 if (err)
2322 goto out_manu;
Jeff Garzik5e593932006-10-11 01:22:21 -07002323 if (bmc->id.aux_firmware_revision_set) {
2324 err = device_create_file(&bmc->dev->dev,
2325 &bmc->aux_firmware_rev_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002326 if (err)
2327 goto out_prod_id;
Jeff Garzik5e593932006-10-11 01:22:21 -07002328 }
2329 if (bmc->guid_set) {
2330 err = device_create_file(&bmc->dev->dev,
2331 &bmc->guid_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002332 if (err)
2333 goto out_aux_firm;
Jeff Garzik5e593932006-10-11 01:22:21 -07002334 }
2335
2336 return 0;
2337
2338out_aux_firm:
2339 if (bmc->id.aux_firmware_revision_set)
2340 device_remove_file(&bmc->dev->dev,
2341 &bmc->aux_firmware_rev_attr);
2342out_prod_id:
2343 device_remove_file(&bmc->dev->dev,
2344 &bmc->product_id_attr);
2345out_manu:
2346 device_remove_file(&bmc->dev->dev,
2347 &bmc->manufacturer_id_attr);
2348out_add_dev:
2349 device_remove_file(&bmc->dev->dev,
2350 &bmc->add_dev_support_attr);
2351out_version:
2352 device_remove_file(&bmc->dev->dev,
2353 &bmc->version_attr);
2354out_firm:
2355 device_remove_file(&bmc->dev->dev,
2356 &bmc->firmware_rev_attr);
2357out_rev:
2358 device_remove_file(&bmc->dev->dev,
2359 &bmc->revision_attr);
2360out_sdrs:
2361 device_remove_file(&bmc->dev->dev,
2362 &bmc->provides_dev_sdrs_attr);
2363out_devid:
2364 device_remove_file(&bmc->dev->dev,
2365 &bmc->device_id_attr);
2366out:
2367 return err;
2368}
2369
Corey Minyard759643b2006-12-06 20:40:59 -08002370static int ipmi_bmc_register(ipmi_smi_t intf, int ifnum,
2371 const char *sysfs_name)
Corey Minyard50c812b2006-03-26 01:37:21 -08002372{
2373 int rv;
2374 struct bmc_device *bmc = intf->bmc;
2375 struct bmc_device *old_bmc;
2376 int size;
2377 char dummy[1];
2378
2379 mutex_lock(&ipmidriver_mutex);
2380
2381 /*
2382 * Try to find if there is an bmc_device struct
2383 * representing the interfaced BMC already
2384 */
2385 if (bmc->guid_set)
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08002386 old_bmc = ipmi_find_bmc_guid(&ipmidriver.driver, bmc->guid);
Corey Minyard50c812b2006-03-26 01:37:21 -08002387 else
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08002388 old_bmc = ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
Corey Minyard50c812b2006-03-26 01:37:21 -08002389 bmc->id.product_id,
2390 bmc->id.device_id);
2391
2392 /*
2393 * If there is already an bmc_device, free the new one,
2394 * otherwise register the new BMC device
2395 */
2396 if (old_bmc) {
2397 kfree(bmc);
2398 intf->bmc = old_bmc;
2399 bmc = old_bmc;
2400
2401 kref_get(&bmc->refcount);
2402 mutex_unlock(&ipmidriver_mutex);
2403
2404 printk(KERN_INFO
2405 "ipmi: interfacing existing BMC (man_id: 0x%6.6x,"
2406 " prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2407 bmc->id.manufacturer_id,
2408 bmc->id.product_id,
2409 bmc->id.device_id);
2410 } else {
Corey Minyardf0b55da2006-12-06 20:40:54 -08002411 char name[14];
2412 unsigned char orig_dev_id = bmc->id.device_id;
2413 int warn_printed = 0;
2414
2415 snprintf(name, sizeof(name),
2416 "ipmi_bmc.%4.4x", bmc->id.product_id);
2417
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08002418 while (ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
Corey Minyardf0b55da2006-12-06 20:40:54 -08002419 bmc->id.product_id,
Corey Minyard1d5636c2006-12-10 02:19:08 -08002420 bmc->id.device_id)) {
Corey Minyardf0b55da2006-12-06 20:40:54 -08002421 if (!warn_printed) {
2422 printk(KERN_WARNING PFX
2423 "This machine has two different BMCs"
2424 " with the same product id and device"
2425 " id. This is an error in the"
2426 " firmware, but incrementing the"
2427 " device id to work around the problem."
2428 " Prod ID = 0x%x, Dev ID = 0x%x\n",
2429 bmc->id.product_id, bmc->id.device_id);
2430 warn_printed = 1;
2431 }
2432 bmc->id.device_id++; /* Wraps at 255 */
2433 if (bmc->id.device_id == orig_dev_id) {
2434 printk(KERN_ERR PFX
2435 "Out of device ids!\n");
2436 break;
2437 }
2438 }
2439
2440 bmc->dev = platform_device_alloc(name, bmc->id.device_id);
Corey Minyard8a3628d2006-03-31 02:30:40 -08002441 if (!bmc->dev) {
Corey Minyardf0b55da2006-12-06 20:40:54 -08002442 mutex_unlock(&ipmidriver_mutex);
Corey Minyard50c812b2006-03-26 01:37:21 -08002443 printk(KERN_ERR
2444 "ipmi_msghandler:"
2445 " Unable to allocate platform device\n");
2446 return -ENOMEM;
2447 }
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08002448 bmc->dev->dev.driver = &ipmidriver.driver;
Corey Minyard50c812b2006-03-26 01:37:21 -08002449 dev_set_drvdata(&bmc->dev->dev, bmc);
2450 kref_init(&bmc->refcount);
2451
Zhang, Yanminb48f5452006-11-16 01:19:08 -08002452 rv = platform_device_add(bmc->dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002453 mutex_unlock(&ipmidriver_mutex);
2454 if (rv) {
Corey Minyardf0b55da2006-12-06 20:40:54 -08002455 platform_device_put(bmc->dev);
2456 bmc->dev = NULL;
Corey Minyard50c812b2006-03-26 01:37:21 -08002457 printk(KERN_ERR
2458 "ipmi_msghandler:"
2459 " Unable to register bmc device: %d\n",
2460 rv);
Corey Minyardc70d7492008-04-29 01:01:09 -07002461 /*
2462 * Don't go to out_err, you can only do that if
2463 * the device is registered already.
2464 */
Corey Minyard50c812b2006-03-26 01:37:21 -08002465 return rv;
2466 }
2467
Jeff Garzik5e593932006-10-11 01:22:21 -07002468 rv = create_files(bmc);
2469 if (rv) {
2470 mutex_lock(&ipmidriver_mutex);
2471 platform_device_unregister(bmc->dev);
2472 mutex_unlock(&ipmidriver_mutex);
2473
2474 return rv;
2475 }
Corey Minyard50c812b2006-03-26 01:37:21 -08002476
2477 printk(KERN_INFO
2478 "ipmi: Found new BMC (man_id: 0x%6.6x, "
2479 " prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2480 bmc->id.manufacturer_id,
2481 bmc->id.product_id,
2482 bmc->id.device_id);
2483 }
2484
2485 /*
2486 * create symlink from system interface device to bmc device
2487 * and back.
2488 */
Corey Minyard759643b2006-12-06 20:40:59 -08002489 intf->sysfs_name = kstrdup(sysfs_name, GFP_KERNEL);
2490 if (!intf->sysfs_name) {
2491 rv = -ENOMEM;
2492 printk(KERN_ERR
2493 "ipmi_msghandler: allocate link to BMC: %d\n",
2494 rv);
2495 goto out_err;
2496 }
2497
Corey Minyard50c812b2006-03-26 01:37:21 -08002498 rv = sysfs_create_link(&intf->si_dev->kobj,
Corey Minyard759643b2006-12-06 20:40:59 -08002499 &bmc->dev->dev.kobj, intf->sysfs_name);
Corey Minyard50c812b2006-03-26 01:37:21 -08002500 if (rv) {
Corey Minyard759643b2006-12-06 20:40:59 -08002501 kfree(intf->sysfs_name);
2502 intf->sysfs_name = NULL;
Corey Minyard50c812b2006-03-26 01:37:21 -08002503 printk(KERN_ERR
2504 "ipmi_msghandler: Unable to create bmc symlink: %d\n",
2505 rv);
2506 goto out_err;
2507 }
2508
Corey Minyard759643b2006-12-06 20:40:59 -08002509 size = snprintf(dummy, 0, "ipmi%d", ifnum);
Corey Minyard50c812b2006-03-26 01:37:21 -08002510 intf->my_dev_name = kmalloc(size+1, GFP_KERNEL);
2511 if (!intf->my_dev_name) {
Corey Minyard759643b2006-12-06 20:40:59 -08002512 kfree(intf->sysfs_name);
2513 intf->sysfs_name = NULL;
Corey Minyard50c812b2006-03-26 01:37:21 -08002514 rv = -ENOMEM;
2515 printk(KERN_ERR
2516 "ipmi_msghandler: allocate link from BMC: %d\n",
2517 rv);
2518 goto out_err;
2519 }
Corey Minyard759643b2006-12-06 20:40:59 -08002520 snprintf(intf->my_dev_name, size+1, "ipmi%d", ifnum);
Corey Minyard50c812b2006-03-26 01:37:21 -08002521
2522 rv = sysfs_create_link(&bmc->dev->dev.kobj, &intf->si_dev->kobj,
2523 intf->my_dev_name);
2524 if (rv) {
Corey Minyard759643b2006-12-06 20:40:59 -08002525 kfree(intf->sysfs_name);
2526 intf->sysfs_name = NULL;
Corey Minyard50c812b2006-03-26 01:37:21 -08002527 kfree(intf->my_dev_name);
2528 intf->my_dev_name = NULL;
2529 printk(KERN_ERR
2530 "ipmi_msghandler:"
2531 " Unable to create symlink to bmc: %d\n",
2532 rv);
2533 goto out_err;
2534 }
2535
2536 return 0;
2537
2538out_err:
2539 ipmi_bmc_unregister(intf);
2540 return rv;
2541}
2542
2543static int
2544send_guid_cmd(ipmi_smi_t intf, int chan)
2545{
2546 struct kernel_ipmi_msg msg;
2547 struct ipmi_system_interface_addr si;
2548
2549 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2550 si.channel = IPMI_BMC_CHANNEL;
2551 si.lun = 0;
2552
2553 msg.netfn = IPMI_NETFN_APP_REQUEST;
2554 msg.cmd = IPMI_GET_DEVICE_GUID_CMD;
2555 msg.data = NULL;
2556 msg.data_len = 0;
2557 return i_ipmi_request(NULL,
2558 intf,
2559 (struct ipmi_addr *) &si,
2560 0,
2561 &msg,
2562 intf,
2563 NULL,
2564 NULL,
2565 0,
2566 intf->channels[0].address,
2567 intf->channels[0].lun,
2568 -1, 0);
2569}
2570
2571static void
2572guid_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
2573{
2574 if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2575 || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
2576 || (msg->msg.cmd != IPMI_GET_DEVICE_GUID_CMD))
2577 /* Not for me */
2578 return;
2579
2580 if (msg->msg.data[0] != 0) {
2581 /* Error from getting the GUID, the BMC doesn't have one. */
2582 intf->bmc->guid_set = 0;
2583 goto out;
2584 }
2585
2586 if (msg->msg.data_len < 17) {
2587 intf->bmc->guid_set = 0;
2588 printk(KERN_WARNING PFX
2589 "guid_handler: The GUID response from the BMC was too"
2590 " short, it was %d but should have been 17. Assuming"
2591 " GUID is not available.\n",
2592 msg->msg.data_len);
2593 goto out;
2594 }
2595
2596 memcpy(intf->bmc->guid, msg->msg.data, 16);
2597 intf->bmc->guid_set = 1;
2598 out:
2599 wake_up(&intf->waitq);
2600}
2601
2602static void
2603get_guid(ipmi_smi_t intf)
2604{
2605 int rv;
2606
2607 intf->bmc->guid_set = 0x2;
2608 intf->null_user_handler = guid_handler;
2609 rv = send_guid_cmd(intf, 0);
2610 if (rv)
2611 /* Send failed, no GUID available. */
2612 intf->bmc->guid_set = 0;
2613 wait_event(intf->waitq, intf->bmc->guid_set != 2);
2614 intf->null_user_handler = NULL;
2615}
2616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617static int
2618send_channel_info_cmd(ipmi_smi_t intf, int chan)
2619{
2620 struct kernel_ipmi_msg msg;
2621 unsigned char data[1];
2622 struct ipmi_system_interface_addr si;
2623
2624 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2625 si.channel = IPMI_BMC_CHANNEL;
2626 si.lun = 0;
2627
2628 msg.netfn = IPMI_NETFN_APP_REQUEST;
2629 msg.cmd = IPMI_GET_CHANNEL_INFO_CMD;
2630 msg.data = data;
2631 msg.data_len = 1;
2632 data[0] = chan;
2633 return i_ipmi_request(NULL,
2634 intf,
2635 (struct ipmi_addr *) &si,
2636 0,
2637 &msg,
Corey Minyard56a55ec2005-09-06 15:18:42 -07002638 intf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 NULL,
2640 NULL,
2641 0,
Corey Minyardc14979b2005-09-06 15:18:38 -07002642 intf->channels[0].address,
2643 intf->channels[0].lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 -1, 0);
2645}
2646
2647static void
Corey Minyard56a55ec2005-09-06 15:18:42 -07002648channel_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649{
2650 int rv = 0;
2651 int chan;
2652
Corey Minyard56a55ec2005-09-06 15:18:42 -07002653 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2654 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
Corey Minyardc70d7492008-04-29 01:01:09 -07002655 && (msg->msg.cmd == IPMI_GET_CHANNEL_INFO_CMD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 /* It's the one we want */
Corey Minyard56a55ec2005-09-06 15:18:42 -07002657 if (msg->msg.data[0] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 /* Got an error from the channel, just go on. */
2659
Corey Minyard56a55ec2005-09-06 15:18:42 -07002660 if (msg->msg.data[0] == IPMI_INVALID_COMMAND_ERR) {
Corey Minyardc70d7492008-04-29 01:01:09 -07002661 /*
2662 * If the MC does not support this
2663 * command, that is legal. We just
2664 * assume it has one IPMB at channel
2665 * zero.
2666 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 intf->channels[0].medium
2668 = IPMI_CHANNEL_MEDIUM_IPMB;
2669 intf->channels[0].protocol
2670 = IPMI_CHANNEL_PROTOCOL_IPMB;
2671 rv = -ENOSYS;
2672
2673 intf->curr_channel = IPMI_MAX_CHANNELS;
2674 wake_up(&intf->waitq);
2675 goto out;
2676 }
2677 goto next_channel;
2678 }
Corey Minyard56a55ec2005-09-06 15:18:42 -07002679 if (msg->msg.data_len < 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 /* Message not big enough, just go on. */
2681 goto next_channel;
2682 }
2683 chan = intf->curr_channel;
Corey Minyard56a55ec2005-09-06 15:18:42 -07002684 intf->channels[chan].medium = msg->msg.data[2] & 0x7f;
2685 intf->channels[chan].protocol = msg->msg.data[3] & 0x1f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
Corey Minyardc70d7492008-04-29 01:01:09 -07002687 next_channel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 intf->curr_channel++;
2689 if (intf->curr_channel >= IPMI_MAX_CHANNELS)
2690 wake_up(&intf->waitq);
2691 else
2692 rv = send_channel_info_cmd(intf, intf->curr_channel);
2693
2694 if (rv) {
2695 /* Got an error somehow, just give up. */
2696 intf->curr_channel = IPMI_MAX_CHANNELS;
2697 wake_up(&intf->waitq);
2698
2699 printk(KERN_WARNING PFX
2700 "Error sending channel information: %d\n",
2701 rv);
2702 }
2703 }
2704 out:
2705 return;
2706}
2707
Corey Minyardfcfa4722007-10-18 03:07:09 -07002708void ipmi_poll_interface(ipmi_user_t user)
2709{
2710 ipmi_smi_t intf = user->intf;
2711
2712 if (intf->handlers->poll)
2713 intf->handlers->poll(intf->send_info);
2714}
Corey Minyardc70d7492008-04-29 01:01:09 -07002715EXPORT_SYMBOL(ipmi_poll_interface);
Corey Minyardfcfa4722007-10-18 03:07:09 -07002716
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717int ipmi_register_smi(struct ipmi_smi_handlers *handlers,
2718 void *send_info,
Corey Minyard50c812b2006-03-26 01:37:21 -08002719 struct ipmi_device_id *device_id,
2720 struct device *si_dev,
Corey Minyard759643b2006-12-06 20:40:59 -08002721 const char *sysfs_name,
Corey Minyard453823b2006-03-31 02:30:39 -08002722 unsigned char slave_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723{
2724 int i, j;
2725 int rv;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002726 ipmi_smi_t intf;
Corey Minyardbca03242006-12-06 20:40:57 -08002727 ipmi_smi_t tintf;
Corey Minyardbca03242006-12-06 20:40:57 -08002728 struct list_head *link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
Corey Minyardc70d7492008-04-29 01:01:09 -07002730 /*
2731 * Make sure the driver is actually initialized, this handles
2732 * problems with initialization order.
2733 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 if (!initialized) {
2735 rv = ipmi_init_msghandler();
2736 if (rv)
2737 return rv;
Corey Minyardc70d7492008-04-29 01:01:09 -07002738 /*
2739 * The init code doesn't return an error if it was turned
2740 * off, but it won't initialize. Check that.
2741 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 if (!initialized)
2743 return -ENODEV;
2744 }
2745
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002746 intf = kzalloc(sizeof(*intf), GFP_KERNEL);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002747 if (!intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 return -ENOMEM;
Corey Minyardb2c03942006-12-06 20:41:00 -08002749
2750 intf->ipmi_version_major = ipmi_version_major(device_id);
2751 intf->ipmi_version_minor = ipmi_version_minor(device_id);
2752
Corey Minyard50c812b2006-03-26 01:37:21 -08002753 intf->bmc = kzalloc(sizeof(*intf->bmc), GFP_KERNEL);
2754 if (!intf->bmc) {
2755 kfree(intf);
2756 return -ENOMEM;
2757 }
Corey Minyardbca03242006-12-06 20:40:57 -08002758 intf->intf_num = -1; /* Mark it invalid for now. */
Corey Minyard393d2cc2005-11-07 00:59:54 -08002759 kref_init(&intf->refcount);
Corey Minyard50c812b2006-03-26 01:37:21 -08002760 intf->bmc->id = *device_id;
2761 intf->si_dev = si_dev;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002762 for (j = 0; j < IPMI_MAX_CHANNELS; j++) {
2763 intf->channels[j].address = IPMI_BMC_SLAVE_ADDR;
2764 intf->channels[j].lun = 2;
2765 }
2766 if (slave_addr != 0)
2767 intf->channels[0].address = slave_addr;
2768 INIT_LIST_HEAD(&intf->users);
2769 intf->handlers = handlers;
2770 intf->send_info = send_info;
2771 spin_lock_init(&intf->seq_lock);
2772 for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++) {
2773 intf->seq_table[j].inuse = 0;
2774 intf->seq_table[j].seqid = 0;
2775 }
2776 intf->curr_seq = 0;
2777#ifdef CONFIG_PROC_FS
Corey Minyardac019152007-10-18 03:07:11 -07002778 mutex_init(&intf->proc_entry_lock);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002779#endif
2780 spin_lock_init(&intf->waiting_msgs_lock);
2781 INIT_LIST_HEAD(&intf->waiting_msgs);
2782 spin_lock_init(&intf->events_lock);
2783 INIT_LIST_HEAD(&intf->waiting_events);
2784 intf->waiting_events_count = 0;
Corey Minyardd6dfd132006-03-31 02:30:41 -08002785 mutex_init(&intf->cmd_rcvrs_mutex);
Corey Minyardb9675132006-12-06 20:41:02 -08002786 spin_lock_init(&intf->maintenance_mode_lock);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002787 INIT_LIST_HEAD(&intf->cmd_rcvrs);
2788 init_waitqueue_head(&intf->waitq);
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07002789 for (i = 0; i < IPMI_NUM_STATS; i++)
2790 atomic_set(&intf->stats[i], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
Corey Minyard393d2cc2005-11-07 00:59:54 -08002792 intf->proc_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
Corey Minyardb2c03942006-12-06 20:41:00 -08002794 mutex_lock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -08002795 mutex_lock(&ipmi_interfaces_mutex);
2796 /* Look for a hole in the numbers. */
2797 i = 0;
2798 link = &ipmi_interfaces;
2799 list_for_each_entry_rcu(tintf, &ipmi_interfaces, link) {
2800 if (tintf->intf_num != i) {
2801 link = &tintf->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 break;
2803 }
Corey Minyardbca03242006-12-06 20:40:57 -08002804 i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 }
Corey Minyardbca03242006-12-06 20:40:57 -08002806 /* Add the new interface in numeric order. */
2807 if (i == 0)
2808 list_add_rcu(&intf->link, &ipmi_interfaces);
2809 else
2810 list_add_tail_rcu(&intf->link, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
Corey Minyard453823b2006-03-31 02:30:39 -08002812 rv = handlers->start_processing(send_info, intf);
2813 if (rv)
2814 goto out;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002815
Corey Minyard50c812b2006-03-26 01:37:21 -08002816 get_guid(intf);
2817
Corey Minyardb2c03942006-12-06 20:41:00 -08002818 if ((intf->ipmi_version_major > 1)
Corey Minyardc70d7492008-04-29 01:01:09 -07002819 || ((intf->ipmi_version_major == 1)
2820 && (intf->ipmi_version_minor >= 5))) {
2821 /*
2822 * Start scanning the channels to see what is
2823 * available.
2824 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08002825 intf->null_user_handler = channel_handler;
2826 intf->curr_channel = 0;
2827 rv = send_channel_info_cmd(intf, 0);
2828 if (rv)
2829 goto out;
2830
2831 /* Wait for the channel info to be read. */
2832 wait_event(intf->waitq,
2833 intf->curr_channel >= IPMI_MAX_CHANNELS);
Corey Minyard50c812b2006-03-26 01:37:21 -08002834 intf->null_user_handler = NULL;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002835 } else {
2836 /* Assume a single IPMB channel at zero. */
2837 intf->channels[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
2838 intf->channels[0].protocol = IPMI_CHANNEL_PROTOCOL_IPMB;
2839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
2841 if (rv == 0)
Corey Minyard393d2cc2005-11-07 00:59:54 -08002842 rv = add_proc_entries(intf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
Corey Minyard759643b2006-12-06 20:40:59 -08002844 rv = ipmi_bmc_register(intf, i, sysfs_name);
Corey Minyard50c812b2006-03-26 01:37:21 -08002845
Corey Minyard393d2cc2005-11-07 00:59:54 -08002846 out:
2847 if (rv) {
2848 if (intf->proc_dir)
2849 remove_proc_entries(intf);
Corey Minyardb2c03942006-12-06 20:41:00 -08002850 intf->handlers = NULL;
Corey Minyardbca03242006-12-06 20:40:57 -08002851 list_del_rcu(&intf->link);
2852 mutex_unlock(&ipmi_interfaces_mutex);
Corey Minyardb2c03942006-12-06 20:41:00 -08002853 mutex_unlock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -08002854 synchronize_rcu();
Corey Minyard393d2cc2005-11-07 00:59:54 -08002855 kref_put(&intf->refcount, intf_free);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002856 } else {
Corey Minyard78ba2fa2007-02-10 01:45:45 -08002857 /*
2858 * Keep memory order straight for RCU readers. Make
2859 * sure everything else is committed to memory before
2860 * setting intf_num to mark the interface valid.
2861 */
2862 smp_wmb();
Corey Minyardbca03242006-12-06 20:40:57 -08002863 intf->intf_num = i;
2864 mutex_unlock(&ipmi_interfaces_mutex);
Corey Minyard78ba2fa2007-02-10 01:45:45 -08002865 /* After this point the interface is legal to use. */
Corey Minyard50c812b2006-03-26 01:37:21 -08002866 call_smi_watchers(i, intf->si_dev);
Corey Minyardb2c03942006-12-06 20:41:00 -08002867 mutex_unlock(&smi_watchers_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 }
2869
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 return rv;
2871}
Corey Minyardc70d7492008-04-29 01:01:09 -07002872EXPORT_SYMBOL(ipmi_register_smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
Corey Minyardb2c03942006-12-06 20:41:00 -08002874static void cleanup_smi_msgs(ipmi_smi_t intf)
2875{
2876 int i;
2877 struct seq_table *ent;
2878
2879 /* No need for locks, the interface is down. */
2880 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
2881 ent = &(intf->seq_table[i]);
2882 if (!ent->inuse)
2883 continue;
2884 deliver_err_response(ent->recv_msg, IPMI_ERR_UNSPECIFIED);
2885 }
2886}
2887
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888int ipmi_unregister_smi(ipmi_smi_t intf)
2889{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 struct ipmi_smi_watcher *w;
Corey Minyardb2c03942006-12-06 20:41:00 -08002891 int intf_num = intf->intf_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
Corey Minyard50c812b2006-03-26 01:37:21 -08002893 ipmi_bmc_unregister(intf);
2894
Corey Minyardb2c03942006-12-06 20:41:00 -08002895 mutex_lock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -08002896 mutex_lock(&ipmi_interfaces_mutex);
Corey Minyardb2c03942006-12-06 20:41:00 -08002897 intf->intf_num = -1;
2898 intf->handlers = NULL;
Corey Minyardbca03242006-12-06 20:40:57 -08002899 list_del_rcu(&intf->link);
2900 mutex_unlock(&ipmi_interfaces_mutex);
2901 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
Corey Minyardb2c03942006-12-06 20:41:00 -08002903 cleanup_smi_msgs(intf);
2904
Corey Minyard393d2cc2005-11-07 00:59:54 -08002905 remove_proc_entries(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
Corey Minyardc70d7492008-04-29 01:01:09 -07002907 /*
2908 * Call all the watcher interfaces to tell them that
2909 * an interface is gone.
2910 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08002911 list_for_each_entry(w, &smi_watchers, link)
Corey Minyardb2c03942006-12-06 20:41:00 -08002912 w->smi_gone(intf_num);
2913 mutex_unlock(&smi_watchers_mutex);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002914
Corey Minyard393d2cc2005-11-07 00:59:54 -08002915 kref_put(&intf->refcount, intf_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 return 0;
2917}
Corey Minyardc70d7492008-04-29 01:01:09 -07002918EXPORT_SYMBOL(ipmi_unregister_smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
2920static int handle_ipmb_get_msg_rsp(ipmi_smi_t intf,
2921 struct ipmi_smi_msg *msg)
2922{
2923 struct ipmi_ipmb_addr ipmb_addr;
2924 struct ipmi_recv_msg *recv_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
Corey Minyardc70d7492008-04-29 01:01:09 -07002926 /*
2927 * This is 11, not 10, because the response must contain a
2928 * completion code.
2929 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 if (msg->rsp_size < 11) {
2931 /* Message not big enough, just ignore it. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07002932 ipmi_inc_stat(intf, invalid_ipmb_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 return 0;
2934 }
2935
2936 if (msg->rsp[2] != 0) {
2937 /* An error getting the response, just ignore it. */
2938 return 0;
2939 }
2940
2941 ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
2942 ipmb_addr.slave_addr = msg->rsp[6];
2943 ipmb_addr.channel = msg->rsp[3] & 0x0f;
2944 ipmb_addr.lun = msg->rsp[7] & 3;
2945
Corey Minyardc70d7492008-04-29 01:01:09 -07002946 /*
2947 * It's a response from a remote entity. Look up the sequence
2948 * number and handle the response.
2949 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 if (intf_find_seq(intf,
2951 msg->rsp[7] >> 2,
2952 msg->rsp[3] & 0x0f,
2953 msg->rsp[8],
2954 (msg->rsp[4] >> 2) & (~1),
2955 (struct ipmi_addr *) &(ipmb_addr),
Corey Minyardc70d7492008-04-29 01:01:09 -07002956 &recv_msg)) {
2957 /*
2958 * We were unable to find the sequence number,
2959 * so just nuke the message.
2960 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07002961 ipmi_inc_stat(intf, unhandled_ipmb_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 return 0;
2963 }
2964
2965 memcpy(recv_msg->msg_data,
2966 &(msg->rsp[9]),
2967 msg->rsp_size - 9);
Corey Minyardc70d7492008-04-29 01:01:09 -07002968 /*
2969 * The other fields matched, so no need to set them, except
2970 * for netfn, which needs to be the response that was
2971 * returned, not the request value.
2972 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 recv_msg->msg.netfn = msg->rsp[4] >> 2;
2974 recv_msg->msg.data = recv_msg->msg_data;
2975 recv_msg->msg.data_len = msg->rsp_size - 10;
2976 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07002977 ipmi_inc_stat(intf, handled_ipmb_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 deliver_response(recv_msg);
2979
2980 return 0;
2981}
2982
2983static int handle_ipmb_get_msg_cmd(ipmi_smi_t intf,
2984 struct ipmi_smi_msg *msg)
2985{
Corey Minyard393d2cc2005-11-07 00:59:54 -08002986 struct cmd_rcvr *rcvr;
2987 int rv = 0;
2988 unsigned char netfn;
2989 unsigned char cmd;
Corey Minyardc69c3122006-09-30 23:27:56 -07002990 unsigned char chan;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002991 ipmi_user_t user = NULL;
2992 struct ipmi_ipmb_addr *ipmb_addr;
2993 struct ipmi_recv_msg *recv_msg;
Corey Minyardb2c03942006-12-06 20:41:00 -08002994 struct ipmi_smi_handlers *handlers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
2996 if (msg->rsp_size < 10) {
2997 /* Message not big enough, just ignore it. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07002998 ipmi_inc_stat(intf, invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 return 0;
3000 }
3001
3002 if (msg->rsp[2] != 0) {
3003 /* An error getting the response, just ignore it. */
3004 return 0;
3005 }
3006
3007 netfn = msg->rsp[4] >> 2;
3008 cmd = msg->rsp[8];
Corey Minyardc69c3122006-09-30 23:27:56 -07003009 chan = msg->rsp[3] & 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Corey Minyarde61fb5b2005-11-07 01:00:05 -08003011 rcu_read_lock();
Corey Minyardc69c3122006-09-30 23:27:56 -07003012 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003013 if (rcvr) {
3014 user = rcvr->user;
3015 kref_get(&user->refcount);
3016 } else
3017 user = NULL;
Corey Minyarde61fb5b2005-11-07 01:00:05 -08003018 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
3020 if (user == NULL) {
3021 /* We didn't find a user, deliver an error response. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003022 ipmi_inc_stat(intf, unhandled_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
3024 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
3025 msg->data[1] = IPMI_SEND_MSG_CMD;
3026 msg->data[2] = msg->rsp[3];
3027 msg->data[3] = msg->rsp[6];
Corey Minyardc70d7492008-04-29 01:01:09 -07003028 msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 msg->data[5] = ipmb_checksum(&(msg->data[3]), 2);
Corey Minyardc14979b2005-09-06 15:18:38 -07003030 msg->data[6] = intf->channels[msg->rsp[3] & 0xf].address;
Corey Minyardc70d7492008-04-29 01:01:09 -07003031 /* rqseq/lun */
3032 msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 msg->data[8] = msg->rsp[8]; /* cmd */
3034 msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
3035 msg->data[10] = ipmb_checksum(&(msg->data[6]), 4);
3036 msg->data_size = 11;
3037
3038#ifdef DEBUG_MSGING
3039 {
3040 int m;
3041 printk("Invalid command:");
Corey Minyarde8b33612005-09-06 15:18:45 -07003042 for (m = 0; m < msg->data_size; m++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 printk(" %2.2x", msg->data[m]);
3044 printk("\n");
3045 }
3046#endif
Corey Minyardb2c03942006-12-06 20:41:00 -08003047 rcu_read_lock();
3048 handlers = intf->handlers;
3049 if (handlers) {
3050 handlers->sender(intf->send_info, msg, 0);
Corey Minyardc70d7492008-04-29 01:01:09 -07003051 /*
3052 * We used the message, so return the value
3053 * that causes it to not be freed or
3054 * queued.
3055 */
Corey Minyardb2c03942006-12-06 20:41:00 -08003056 rv = -1;
3057 }
3058 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 } else {
3060 /* Deliver the message to the user. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003061 ipmi_inc_stat(intf, handled_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
3063 recv_msg = ipmi_alloc_recv_msg();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003064 if (!recv_msg) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003065 /*
3066 * We couldn't allocate memory for the
3067 * message, so requeue it for handling
3068 * later.
3069 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 rv = 1;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003071 kref_put(&user->refcount, free_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 } else {
3073 /* Extract the source address from the data. */
3074 ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
3075 ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
3076 ipmb_addr->slave_addr = msg->rsp[6];
3077 ipmb_addr->lun = msg->rsp[7] & 3;
3078 ipmb_addr->channel = msg->rsp[3] & 0xf;
3079
Corey Minyardc70d7492008-04-29 01:01:09 -07003080 /*
3081 * Extract the rest of the message information
3082 * from the IPMB header.
3083 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 recv_msg->user = user;
3085 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3086 recv_msg->msgid = msg->rsp[7] >> 2;
3087 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3088 recv_msg->msg.cmd = msg->rsp[8];
3089 recv_msg->msg.data = recv_msg->msg_data;
3090
Corey Minyardc70d7492008-04-29 01:01:09 -07003091 /*
3092 * We chop off 10, not 9 bytes because the checksum
3093 * at the end also needs to be removed.
3094 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 recv_msg->msg.data_len = msg->rsp_size - 10;
3096 memcpy(recv_msg->msg_data,
3097 &(msg->rsp[9]),
3098 msg->rsp_size - 10);
3099 deliver_response(recv_msg);
3100 }
3101 }
3102
3103 return rv;
3104}
3105
3106static int handle_lan_get_msg_rsp(ipmi_smi_t intf,
3107 struct ipmi_smi_msg *msg)
3108{
3109 struct ipmi_lan_addr lan_addr;
3110 struct ipmi_recv_msg *recv_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
3112
Corey Minyardc70d7492008-04-29 01:01:09 -07003113 /*
3114 * This is 13, not 12, because the response must contain a
3115 * completion code.
3116 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 if (msg->rsp_size < 13) {
3118 /* Message not big enough, just ignore it. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003119 ipmi_inc_stat(intf, invalid_lan_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 return 0;
3121 }
3122
3123 if (msg->rsp[2] != 0) {
3124 /* An error getting the response, just ignore it. */
3125 return 0;
3126 }
3127
3128 lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
3129 lan_addr.session_handle = msg->rsp[4];
3130 lan_addr.remote_SWID = msg->rsp[8];
3131 lan_addr.local_SWID = msg->rsp[5];
3132 lan_addr.channel = msg->rsp[3] & 0x0f;
3133 lan_addr.privilege = msg->rsp[3] >> 4;
3134 lan_addr.lun = msg->rsp[9] & 3;
3135
Corey Minyardc70d7492008-04-29 01:01:09 -07003136 /*
3137 * It's a response from a remote entity. Look up the sequence
3138 * number and handle the response.
3139 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 if (intf_find_seq(intf,
3141 msg->rsp[9] >> 2,
3142 msg->rsp[3] & 0x0f,
3143 msg->rsp[10],
3144 (msg->rsp[6] >> 2) & (~1),
3145 (struct ipmi_addr *) &(lan_addr),
Corey Minyardc70d7492008-04-29 01:01:09 -07003146 &recv_msg)) {
3147 /*
3148 * We were unable to find the sequence number,
3149 * so just nuke the message.
3150 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003151 ipmi_inc_stat(intf, unhandled_lan_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 return 0;
3153 }
3154
3155 memcpy(recv_msg->msg_data,
3156 &(msg->rsp[11]),
3157 msg->rsp_size - 11);
Corey Minyardc70d7492008-04-29 01:01:09 -07003158 /*
3159 * The other fields matched, so no need to set them, except
3160 * for netfn, which needs to be the response that was
3161 * returned, not the request value.
3162 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3164 recv_msg->msg.data = recv_msg->msg_data;
3165 recv_msg->msg.data_len = msg->rsp_size - 12;
3166 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003167 ipmi_inc_stat(intf, handled_lan_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 deliver_response(recv_msg);
3169
3170 return 0;
3171}
3172
3173static int handle_lan_get_msg_cmd(ipmi_smi_t intf,
3174 struct ipmi_smi_msg *msg)
3175{
Corey Minyard393d2cc2005-11-07 00:59:54 -08003176 struct cmd_rcvr *rcvr;
3177 int rv = 0;
3178 unsigned char netfn;
3179 unsigned char cmd;
Corey Minyardc69c3122006-09-30 23:27:56 -07003180 unsigned char chan;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003181 ipmi_user_t user = NULL;
3182 struct ipmi_lan_addr *lan_addr;
3183 struct ipmi_recv_msg *recv_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
3185 if (msg->rsp_size < 12) {
3186 /* Message not big enough, just ignore it. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003187 ipmi_inc_stat(intf, invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 return 0;
3189 }
3190
3191 if (msg->rsp[2] != 0) {
3192 /* An error getting the response, just ignore it. */
3193 return 0;
3194 }
3195
3196 netfn = msg->rsp[6] >> 2;
3197 cmd = msg->rsp[10];
Corey Minyardc69c3122006-09-30 23:27:56 -07003198 chan = msg->rsp[3] & 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
Corey Minyarde61fb5b2005-11-07 01:00:05 -08003200 rcu_read_lock();
Corey Minyardc69c3122006-09-30 23:27:56 -07003201 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003202 if (rcvr) {
3203 user = rcvr->user;
3204 kref_get(&user->refcount);
3205 } else
3206 user = NULL;
Corey Minyarde61fb5b2005-11-07 01:00:05 -08003207 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
3209 if (user == NULL) {
Corey Minyard393d2cc2005-11-07 00:59:54 -08003210 /* We didn't find a user, just give up. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003211 ipmi_inc_stat(intf, unhandled_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
Corey Minyardc70d7492008-04-29 01:01:09 -07003213 /*
3214 * Don't do anything with these messages, just allow
3215 * them to be freed.
3216 */
3217 rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 } else {
3219 /* Deliver the message to the user. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003220 ipmi_inc_stat(intf, handled_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
3222 recv_msg = ipmi_alloc_recv_msg();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003223 if (!recv_msg) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003224 /*
3225 * We couldn't allocate memory for the
3226 * message, so requeue it for handling later.
3227 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 rv = 1;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003229 kref_put(&user->refcount, free_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 } else {
3231 /* Extract the source address from the data. */
3232 lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
3233 lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
3234 lan_addr->session_handle = msg->rsp[4];
3235 lan_addr->remote_SWID = msg->rsp[8];
3236 lan_addr->local_SWID = msg->rsp[5];
3237 lan_addr->lun = msg->rsp[9] & 3;
3238 lan_addr->channel = msg->rsp[3] & 0xf;
3239 lan_addr->privilege = msg->rsp[3] >> 4;
3240
Corey Minyardc70d7492008-04-29 01:01:09 -07003241 /*
3242 * Extract the rest of the message information
3243 * from the IPMB header.
3244 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 recv_msg->user = user;
3246 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3247 recv_msg->msgid = msg->rsp[9] >> 2;
3248 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3249 recv_msg->msg.cmd = msg->rsp[10];
3250 recv_msg->msg.data = recv_msg->msg_data;
3251
Corey Minyardc70d7492008-04-29 01:01:09 -07003252 /*
3253 * We chop off 12, not 11 bytes because the checksum
3254 * at the end also needs to be removed.
3255 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 recv_msg->msg.data_len = msg->rsp_size - 12;
3257 memcpy(recv_msg->msg_data,
3258 &(msg->rsp[11]),
3259 msg->rsp_size - 12);
3260 deliver_response(recv_msg);
3261 }
3262 }
3263
3264 return rv;
3265}
3266
3267static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
3268 struct ipmi_smi_msg *msg)
3269{
3270 struct ipmi_system_interface_addr *smi_addr;
Corey Minyardc70d7492008-04-29 01:01:09 -07003271
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 recv_msg->msgid = 0;
3273 smi_addr = (struct ipmi_system_interface_addr *) &(recv_msg->addr);
3274 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3275 smi_addr->channel = IPMI_BMC_CHANNEL;
3276 smi_addr->lun = msg->rsp[0] & 3;
3277 recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
3278 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3279 recv_msg->msg.cmd = msg->rsp[1];
3280 memcpy(recv_msg->msg_data, &(msg->rsp[3]), msg->rsp_size - 3);
3281 recv_msg->msg.data = recv_msg->msg_data;
3282 recv_msg->msg.data_len = msg->rsp_size - 3;
3283}
3284
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285static int handle_read_event_rsp(ipmi_smi_t intf,
3286 struct ipmi_smi_msg *msg)
3287{
3288 struct ipmi_recv_msg *recv_msg, *recv_msg2;
3289 struct list_head msgs;
3290 ipmi_user_t user;
3291 int rv = 0;
3292 int deliver_count = 0;
3293 unsigned long flags;
3294
3295 if (msg->rsp_size < 19) {
3296 /* Message is too small to be an IPMB event. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003297 ipmi_inc_stat(intf, invalid_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 return 0;
3299 }
3300
3301 if (msg->rsp[2] != 0) {
3302 /* An error getting the event, just ignore it. */
3303 return 0;
3304 }
3305
3306 INIT_LIST_HEAD(&msgs);
3307
Corey Minyard393d2cc2005-11-07 00:59:54 -08003308 spin_lock_irqsave(&intf->events_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003310 ipmi_inc_stat(intf, events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311
Corey Minyardc70d7492008-04-29 01:01:09 -07003312 /*
3313 * Allocate and fill in one message for every user that is
3314 * getting events.
3315 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08003316 rcu_read_lock();
3317 list_for_each_entry_rcu(user, &intf->users, link) {
Corey Minyard8a3628d2006-03-31 02:30:40 -08003318 if (!user->gets_events)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 continue;
3320
3321 recv_msg = ipmi_alloc_recv_msg();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003322 if (!recv_msg) {
Corey Minyard393d2cc2005-11-07 00:59:54 -08003323 rcu_read_unlock();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003324 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs,
3325 link) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 list_del(&recv_msg->link);
3327 ipmi_free_recv_msg(recv_msg);
3328 }
Corey Minyardc70d7492008-04-29 01:01:09 -07003329 /*
3330 * We couldn't allocate memory for the
3331 * message, so requeue it for handling
3332 * later.
3333 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 rv = 1;
3335 goto out;
3336 }
3337
3338 deliver_count++;
3339
3340 copy_event_into_recv_msg(recv_msg, msg);
3341 recv_msg->user = user;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003342 kref_get(&user->refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 list_add_tail(&(recv_msg->link), &msgs);
3344 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08003345 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
3347 if (deliver_count) {
3348 /* Now deliver all the messages. */
3349 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
3350 list_del(&recv_msg->link);
3351 deliver_response(recv_msg);
3352 }
3353 } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003354 /*
3355 * No one to receive the message, put it in queue if there's
3356 * not already too many things in the queue.
3357 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 recv_msg = ipmi_alloc_recv_msg();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003359 if (!recv_msg) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003360 /*
3361 * We couldn't allocate memory for the
3362 * message, so requeue it for handling
3363 * later.
3364 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 rv = 1;
3366 goto out;
3367 }
3368
3369 copy_event_into_recv_msg(recv_msg, msg);
3370 list_add_tail(&(recv_msg->link), &(intf->waiting_events));
Corey Minyard4791c032006-04-10 22:54:31 -07003371 intf->waiting_events_count++;
Corey Minyard87ebd062008-04-29 01:01:04 -07003372 } else if (!intf->event_msg_printed) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003373 /*
3374 * There's too many things in the queue, discard this
3375 * message.
3376 */
Corey Minyard87ebd062008-04-29 01:01:04 -07003377 printk(KERN_WARNING PFX "Event queue full, discarding"
3378 " incoming events\n");
3379 intf->event_msg_printed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 }
3381
3382 out:
3383 spin_unlock_irqrestore(&(intf->events_lock), flags);
3384
3385 return rv;
3386}
3387
3388static int handle_bmc_rsp(ipmi_smi_t intf,
3389 struct ipmi_smi_msg *msg)
3390{
3391 struct ipmi_recv_msg *recv_msg;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003392 struct ipmi_user *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
3394 recv_msg = (struct ipmi_recv_msg *) msg->user_data;
Corey Minyardc70d7492008-04-29 01:01:09 -07003395 if (recv_msg == NULL) {
3396 printk(KERN_WARNING
3397 "IPMI message received with no owner. This\n"
3398 "could be because of a malformed message, or\n"
3399 "because of a hardware error. Contact your\n"
3400 "hardware vender for assistance\n");
Corey Minyard56a55ec2005-09-06 15:18:42 -07003401 return 0;
3402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403
Corey Minyard393d2cc2005-11-07 00:59:54 -08003404 user = recv_msg->user;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 /* Make sure the user still exists. */
Corey Minyard393d2cc2005-11-07 00:59:54 -08003406 if (user && !user->valid) {
Corey Minyard56a55ec2005-09-06 15:18:42 -07003407 /* The user for the message went away, so give up. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003408 ipmi_inc_stat(intf, unhandled_local_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 ipmi_free_recv_msg(recv_msg);
3410 } else {
3411 struct ipmi_system_interface_addr *smi_addr;
3412
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003413 ipmi_inc_stat(intf, handled_local_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3415 recv_msg->msgid = msg->msgid;
3416 smi_addr = ((struct ipmi_system_interface_addr *)
3417 &(recv_msg->addr));
3418 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3419 smi_addr->channel = IPMI_BMC_CHANNEL;
3420 smi_addr->lun = msg->rsp[0] & 3;
3421 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3422 recv_msg->msg.cmd = msg->rsp[1];
3423 memcpy(recv_msg->msg_data,
3424 &(msg->rsp[2]),
3425 msg->rsp_size - 2);
3426 recv_msg->msg.data = recv_msg->msg_data;
3427 recv_msg->msg.data_len = msg->rsp_size - 2;
3428 deliver_response(recv_msg);
3429 }
3430
3431 return 0;
3432}
3433
Corey Minyardc70d7492008-04-29 01:01:09 -07003434/*
3435 * Handle a new message. Return 1 if the message should be requeued,
3436 * 0 if the message should be freed, or -1 if the message should not
3437 * be freed or requeued.
3438 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439static int handle_new_recv_msg(ipmi_smi_t intf,
3440 struct ipmi_smi_msg *msg)
3441{
3442 int requeue;
3443 int chan;
3444
3445#ifdef DEBUG_MSGING
3446 int m;
3447 printk("Recv:");
Corey Minyarde8b33612005-09-06 15:18:45 -07003448 for (m = 0; m < msg->rsp_size; m++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 printk(" %2.2x", msg->rsp[m]);
3450 printk("\n");
3451#endif
3452 if (msg->rsp_size < 2) {
3453 /* Message is too small to be correct. */
3454 printk(KERN_WARNING PFX "BMC returned to small a message"
3455 " for netfn %x cmd %x, got %d bytes\n",
3456 (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size);
3457
3458 /* Generate an error response for the message. */
3459 msg->rsp[0] = msg->data[0] | (1 << 2);
3460 msg->rsp[1] = msg->data[1];
3461 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
3462 msg->rsp_size = 3;
Corey Minyardc70d7492008-04-29 01:01:09 -07003463 } else if (((msg->rsp[0] >> 2) != ((msg->data[0] >> 2) | 1))
3464 || (msg->rsp[1] != msg->data[1])) {
3465 /*
3466 * The NetFN and Command in the response is not even
3467 * marginally correct.
3468 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 printk(KERN_WARNING PFX "BMC returned incorrect response,"
3470 " expected netfn %x cmd %x, got netfn %x cmd %x\n",
3471 (msg->data[0] >> 2) | 1, msg->data[1],
3472 msg->rsp[0] >> 2, msg->rsp[1]);
3473
3474 /* Generate an error response for the message. */
3475 msg->rsp[0] = msg->data[0] | (1 << 2);
3476 msg->rsp[1] = msg->data[1];
3477 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
3478 msg->rsp_size = 3;
3479 }
3480
3481 if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
3482 && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07003483 && (msg->user_data != NULL)) {
3484 /*
3485 * It's a response to a response we sent. For this we
3486 * deliver a send message response to the user.
3487 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08003488 struct ipmi_recv_msg *recv_msg = msg->user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
3490 requeue = 0;
3491 if (msg->rsp_size < 2)
3492 /* Message is too small to be correct. */
3493 goto out;
3494
3495 chan = msg->data[2] & 0x0f;
3496 if (chan >= IPMI_MAX_CHANNELS)
3497 /* Invalid channel number */
3498 goto out;
3499
Corey Minyard393d2cc2005-11-07 00:59:54 -08003500 if (!recv_msg)
3501 goto out;
3502
3503 /* Make sure the user still exists. */
3504 if (!recv_msg->user || !recv_msg->user->valid)
3505 goto out;
3506
3507 recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
3508 recv_msg->msg.data = recv_msg->msg_data;
3509 recv_msg->msg.data_len = 1;
3510 recv_msg->msg_data[0] = msg->rsp[2];
3511 deliver_response(recv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
Corey Minyardc70d7492008-04-29 01:01:09 -07003513 && (msg->rsp[1] == IPMI_GET_MSG_CMD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 /* It's from the receive queue. */
3515 chan = msg->rsp[3] & 0xf;
3516 if (chan >= IPMI_MAX_CHANNELS) {
3517 /* Invalid channel number */
3518 requeue = 0;
3519 goto out;
3520 }
3521
3522 switch (intf->channels[chan].medium) {
3523 case IPMI_CHANNEL_MEDIUM_IPMB:
3524 if (msg->rsp[4] & 0x04) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003525 /*
3526 * It's a response, so find the
3527 * requesting message and send it up.
3528 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 requeue = handle_ipmb_get_msg_rsp(intf, msg);
3530 } else {
Corey Minyardc70d7492008-04-29 01:01:09 -07003531 /*
3532 * It's a command to the SMS from some other
3533 * entity. Handle that.
3534 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 requeue = handle_ipmb_get_msg_cmd(intf, msg);
3536 }
3537 break;
3538
3539 case IPMI_CHANNEL_MEDIUM_8023LAN:
3540 case IPMI_CHANNEL_MEDIUM_ASYNC:
3541 if (msg->rsp[6] & 0x04) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003542 /*
3543 * It's a response, so find the
3544 * requesting message and send it up.
3545 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 requeue = handle_lan_get_msg_rsp(intf, msg);
3547 } else {
Corey Minyardc70d7492008-04-29 01:01:09 -07003548 /*
3549 * It's a command to the SMS from some other
3550 * entity. Handle that.
3551 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 requeue = handle_lan_get_msg_cmd(intf, msg);
3553 }
3554 break;
3555
3556 default:
Corey Minyardc70d7492008-04-29 01:01:09 -07003557 /*
3558 * We don't handle the channel type, so just
3559 * free the message.
3560 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 requeue = 0;
3562 }
3563
3564 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
Corey Minyardc70d7492008-04-29 01:01:09 -07003565 && (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 /* It's an asyncronous event. */
3567 requeue = handle_read_event_rsp(intf, msg);
3568 } else {
3569 /* It's a response from the local BMC. */
3570 requeue = handle_bmc_rsp(intf, msg);
3571 }
3572
3573 out:
3574 return requeue;
3575}
3576
3577/* Handle a new message from the lower layer. */
3578void ipmi_smi_msg_received(ipmi_smi_t intf,
3579 struct ipmi_smi_msg *msg)
3580{
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003581 unsigned long flags = 0; /* keep us warning-free. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 int rv;
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003583 int run_to_completion;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
3585
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 if ((msg->data_size >= 2)
3587 && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
3588 && (msg->data[1] == IPMI_SEND_MSG_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07003589 && (msg->user_data == NULL)) {
3590 /*
3591 * This is the local response to a command send, start
3592 * the timer for these. The user_data will not be
3593 * NULL if this is a response send, and we will let
3594 * response sends just go through.
3595 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596
Corey Minyardc70d7492008-04-29 01:01:09 -07003597 /*
3598 * Check for errors, if we get certain errors (ones
3599 * that mean basically we can try again later), we
3600 * ignore them and start the timer. Otherwise we
3601 * report the error immediately.
3602 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
3604 && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
Corey Minyard46d52b02006-11-08 17:44:55 -08003605 && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR)
3606 && (msg->rsp[2] != IPMI_BUS_ERR)
Corey Minyardc70d7492008-04-29 01:01:09 -07003607 && (msg->rsp[2] != IPMI_NAK_ON_WRITE_ERR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 int chan = msg->rsp[3] & 0xf;
3609
3610 /* Got an error sending the message, handle it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 if (chan >= IPMI_MAX_CHANNELS)
3612 ; /* This shouldn't happen */
3613 else if ((intf->channels[chan].medium
3614 == IPMI_CHANNEL_MEDIUM_8023LAN)
3615 || (intf->channels[chan].medium
3616 == IPMI_CHANNEL_MEDIUM_ASYNC))
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003617 ipmi_inc_stat(intf, sent_lan_command_errs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 else
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003619 ipmi_inc_stat(intf, sent_ipmb_command_errs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 intf_err_seq(intf, msg->msgid, msg->rsp[2]);
Corey Minyardc70d7492008-04-29 01:01:09 -07003621 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 /* The message was sent, start the timer. */
3623 intf_start_seq_timer(intf, msg->msgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624
3625 ipmi_free_smi_msg(msg);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003626 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 }
3628
Corey Minyardc70d7492008-04-29 01:01:09 -07003629 /*
3630 * To preserve message order, if the list is not empty, we
3631 * tack this message onto the end of the list.
3632 */
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003633 run_to_completion = intf->run_to_completion;
3634 if (!run_to_completion)
3635 spin_lock_irqsave(&intf->waiting_msgs_lock, flags);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003636 if (!list_empty(&intf->waiting_msgs)) {
3637 list_add_tail(&msg->link, &intf->waiting_msgs);
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003638 if (!run_to_completion)
3639 spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003640 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 }
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003642 if (!run_to_completion)
3643 spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
Corey Minyardc70d7492008-04-29 01:01:09 -07003644
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 rv = handle_new_recv_msg(intf, msg);
3646 if (rv > 0) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003647 /*
3648 * Could not handle the message now, just add it to a
3649 * list to handle later.
3650 */
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003651 run_to_completion = intf->run_to_completion;
3652 if (!run_to_completion)
3653 spin_lock_irqsave(&intf->waiting_msgs_lock, flags);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003654 list_add_tail(&msg->link, &intf->waiting_msgs);
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003655 if (!run_to_completion)
3656 spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 } else if (rv == 0) {
3658 ipmi_free_smi_msg(msg);
3659 }
3660
Corey Minyard393d2cc2005-11-07 00:59:54 -08003661 out:
3662 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663}
Corey Minyardc70d7492008-04-29 01:01:09 -07003664EXPORT_SYMBOL(ipmi_smi_msg_received);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
3666void ipmi_smi_watchdog_pretimeout(ipmi_smi_t intf)
3667{
3668 ipmi_user_t user;
3669
Corey Minyard393d2cc2005-11-07 00:59:54 -08003670 rcu_read_lock();
3671 list_for_each_entry_rcu(user, &intf->users, link) {
Corey Minyard8a3628d2006-03-31 02:30:40 -08003672 if (!user->handler->ipmi_watchdog_pretimeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 continue;
3674
3675 user->handler->ipmi_watchdog_pretimeout(user->handler_data);
3676 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08003677 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678}
Corey Minyardc70d7492008-04-29 01:01:09 -07003679EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
Corey Minyard882fe012005-05-01 08:59:12 -07003681static struct ipmi_smi_msg *
3682smi_from_recv_msg(ipmi_smi_t intf, struct ipmi_recv_msg *recv_msg,
3683 unsigned char seq, long seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684{
Corey Minyard882fe012005-05-01 08:59:12 -07003685 struct ipmi_smi_msg *smi_msg = ipmi_alloc_smi_msg();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 if (!smi_msg)
Corey Minyardc70d7492008-04-29 01:01:09 -07003687 /*
3688 * If we can't allocate the message, then just return, we
3689 * get 4 retries, so this should be ok.
3690 */
Corey Minyard882fe012005-05-01 08:59:12 -07003691 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692
3693 memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
3694 smi_msg->data_size = recv_msg->msg.data_len;
3695 smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
Corey Minyardc70d7492008-04-29 01:01:09 -07003696
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697#ifdef DEBUG_MSGING
3698 {
3699 int m;
3700 printk("Resend: ");
Corey Minyarde8b33612005-09-06 15:18:45 -07003701 for (m = 0; m < smi_msg->data_size; m++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 printk(" %2.2x", smi_msg->data[m]);
3703 printk("\n");
3704 }
3705#endif
Corey Minyard882fe012005-05-01 08:59:12 -07003706 return smi_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707}
3708
Corey Minyard393d2cc2005-11-07 00:59:54 -08003709static void check_msg_timeout(ipmi_smi_t intf, struct seq_table *ent,
3710 struct list_head *timeouts, long timeout_period,
3711 int slot, unsigned long *flags)
3712{
Corey Minyardb2c03942006-12-06 20:41:00 -08003713 struct ipmi_recv_msg *msg;
3714 struct ipmi_smi_handlers *handlers;
3715
3716 if (intf->intf_num == -1)
3717 return;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003718
3719 if (!ent->inuse)
3720 return;
3721
3722 ent->timeout -= timeout_period;
3723 if (ent->timeout > 0)
3724 return;
3725
3726 if (ent->retries_left == 0) {
3727 /* The message has used all its retries. */
3728 ent->inuse = 0;
3729 msg = ent->recv_msg;
3730 list_add_tail(&msg->link, timeouts);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003731 if (ent->broadcast)
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003732 ipmi_inc_stat(intf, timed_out_ipmb_broadcasts);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003733 else if (ent->recv_msg->addr.addr_type == IPMI_LAN_ADDR_TYPE)
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003734 ipmi_inc_stat(intf, timed_out_lan_commands);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003735 else
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003736 ipmi_inc_stat(intf, timed_out_ipmb_commands);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003737 } else {
3738 struct ipmi_smi_msg *smi_msg;
3739 /* More retries, send again. */
3740
Corey Minyardc70d7492008-04-29 01:01:09 -07003741 /*
3742 * Start with the max timer, set to normal timer after
3743 * the message is sent.
3744 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08003745 ent->timeout = MAX_MSG_TIMEOUT;
3746 ent->retries_left--;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003747 if (ent->recv_msg->addr.addr_type == IPMI_LAN_ADDR_TYPE)
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003748 ipmi_inc_stat(intf, retransmitted_lan_commands);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003749 else
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003750 ipmi_inc_stat(intf, retransmitted_ipmb_commands);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003751
3752 smi_msg = smi_from_recv_msg(intf, ent->recv_msg, slot,
3753 ent->seqid);
Corey Minyard8a3628d2006-03-31 02:30:40 -08003754 if (!smi_msg)
Corey Minyard393d2cc2005-11-07 00:59:54 -08003755 return;
3756
3757 spin_unlock_irqrestore(&intf->seq_lock, *flags);
Corey Minyardb2c03942006-12-06 20:41:00 -08003758
Corey Minyardc70d7492008-04-29 01:01:09 -07003759 /*
3760 * Send the new message. We send with a zero
3761 * priority. It timed out, I doubt time is that
3762 * critical now, and high priority messages are really
3763 * only for messages to the local MC, which don't get
3764 * resent.
3765 */
Corey Minyardb2c03942006-12-06 20:41:00 -08003766 handlers = intf->handlers;
3767 if (handlers)
3768 intf->handlers->sender(intf->send_info,
3769 smi_msg, 0);
3770 else
3771 ipmi_free_smi_msg(smi_msg);
3772
Corey Minyard393d2cc2005-11-07 00:59:54 -08003773 spin_lock_irqsave(&intf->seq_lock, *flags);
3774 }
3775}
3776
3777static void ipmi_timeout_handler(long timeout_period)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778{
3779 ipmi_smi_t intf;
3780 struct list_head timeouts;
3781 struct ipmi_recv_msg *msg, *msg2;
3782 struct ipmi_smi_msg *smi_msg, *smi_msg2;
3783 unsigned long flags;
Corey Minyardbca03242006-12-06 20:40:57 -08003784 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785
Corey Minyardbca03242006-12-06 20:40:57 -08003786 rcu_read_lock();
3787 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 /* See if any waiting messages need to be processed. */
Corey Minyard393d2cc2005-11-07 00:59:54 -08003789 spin_lock_irqsave(&intf->waiting_msgs_lock, flags);
Corey Minyard8a3628d2006-03-31 02:30:40 -08003790 list_for_each_entry_safe(smi_msg, smi_msg2,
3791 &intf->waiting_msgs, link) {
3792 if (!handle_new_recv_msg(intf, smi_msg)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 list_del(&smi_msg->link);
3794 ipmi_free_smi_msg(smi_msg);
3795 } else {
Corey Minyardc70d7492008-04-29 01:01:09 -07003796 /*
3797 * To preserve message order, quit if we
3798 * can't handle a message.
3799 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 break;
3801 }
3802 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08003803 spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804
Corey Minyardc70d7492008-04-29 01:01:09 -07003805 /*
3806 * Go through the seq table and find any messages that
3807 * have timed out, putting them in the timeouts
3808 * list.
3809 */
David Barksdale41c57a82007-01-30 14:36:25 -08003810 INIT_LIST_HEAD(&timeouts);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003811 spin_lock_irqsave(&intf->seq_lock, flags);
Corey Minyardbca03242006-12-06 20:40:57 -08003812 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++)
3813 check_msg_timeout(intf, &(intf->seq_table[i]),
3814 &timeouts, timeout_period, i,
Corey Minyard393d2cc2005-11-07 00:59:54 -08003815 &flags);
3816 spin_unlock_irqrestore(&intf->seq_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817
Corey Minyard393d2cc2005-11-07 00:59:54 -08003818 list_for_each_entry_safe(msg, msg2, &timeouts, link)
Corey Minyardb2c03942006-12-06 20:41:00 -08003819 deliver_err_response(msg, IPMI_TIMEOUT_COMPLETION_CODE);
Corey Minyardb9675132006-12-06 20:41:02 -08003820
3821 /*
3822 * Maintenance mode handling. Check the timeout
3823 * optimistically before we claim the lock. It may
3824 * mean a timeout gets missed occasionally, but that
3825 * only means the timeout gets extended by one period
3826 * in that case. No big deal, and it avoids the lock
3827 * most of the time.
3828 */
3829 if (intf->auto_maintenance_timeout > 0) {
3830 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
3831 if (intf->auto_maintenance_timeout > 0) {
3832 intf->auto_maintenance_timeout
3833 -= timeout_period;
3834 if (!intf->maintenance_mode
Corey Minyardc70d7492008-04-29 01:01:09 -07003835 && (intf->auto_maintenance_timeout <= 0)) {
Corey Minyardb9675132006-12-06 20:41:02 -08003836 intf->maintenance_mode_enable = 0;
3837 maintenance_mode_update(intf);
3838 }
3839 }
3840 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
3841 flags);
3842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 }
Corey Minyardbca03242006-12-06 20:40:57 -08003844 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845}
3846
3847static void ipmi_request_event(void)
3848{
Corey Minyardb2c03942006-12-06 20:41:00 -08003849 ipmi_smi_t intf;
3850 struct ipmi_smi_handlers *handlers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851
Corey Minyardbca03242006-12-06 20:40:57 -08003852 rcu_read_lock();
Corey Minyardc70d7492008-04-29 01:01:09 -07003853 /*
3854 * Called from the timer, no need to check if handlers is
3855 * valid.
3856 */
Corey Minyardb2c03942006-12-06 20:41:00 -08003857 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
Corey Minyardb9675132006-12-06 20:41:02 -08003858 /* No event requests when in maintenance mode. */
3859 if (intf->maintenance_mode_enable)
3860 continue;
3861
Corey Minyardb2c03942006-12-06 20:41:00 -08003862 handlers = intf->handlers;
3863 if (handlers)
3864 handlers->request_events(intf->send_info);
3865 }
Corey Minyardbca03242006-12-06 20:40:57 -08003866 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867}
3868
3869static struct timer_list ipmi_timer;
3870
3871/* Call every ~100 ms. */
3872#define IPMI_TIMEOUT_TIME 100
3873
3874/* How many jiffies does it take to get to the timeout time. */
3875#define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000)
3876
Corey Minyardc70d7492008-04-29 01:01:09 -07003877/*
3878 * Request events from the queue every second (this is the number of
3879 * IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the
3880 * future, IPMI will add a way to know immediately if an event is in
3881 * the queue and this silliness can go away.
3882 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883#define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME))
3884
Corey Minyard8f43f842005-06-23 22:01:40 -07003885static atomic_t stop_operation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886static unsigned int ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
3887
3888static void ipmi_timeout(unsigned long data)
3889{
Corey Minyard8f43f842005-06-23 22:01:40 -07003890 if (atomic_read(&stop_operation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892
3893 ticks_to_req_ev--;
3894 if (ticks_to_req_ev == 0) {
3895 ipmi_request_event();
3896 ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
3897 }
3898
3899 ipmi_timeout_handler(IPMI_TIMEOUT_TIME);
3900
Corey Minyard8f43f842005-06-23 22:01:40 -07003901 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902}
3903
3904
3905static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
3906static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
3907
3908/* FIXME - convert these to slabs. */
3909static void free_smi_msg(struct ipmi_smi_msg *msg)
3910{
3911 atomic_dec(&smi_msg_inuse_count);
3912 kfree(msg);
3913}
3914
3915struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
3916{
3917 struct ipmi_smi_msg *rv;
3918 rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
3919 if (rv) {
3920 rv->done = free_smi_msg;
3921 rv->user_data = NULL;
3922 atomic_inc(&smi_msg_inuse_count);
3923 }
3924 return rv;
3925}
Corey Minyardc70d7492008-04-29 01:01:09 -07003926EXPORT_SYMBOL(ipmi_alloc_smi_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927
3928static void free_recv_msg(struct ipmi_recv_msg *msg)
3929{
3930 atomic_dec(&recv_msg_inuse_count);
3931 kfree(msg);
3932}
3933
Adrian Bunk74006302008-04-29 01:01:14 -07003934static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935{
3936 struct ipmi_recv_msg *rv;
3937
3938 rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
3939 if (rv) {
Corey Minyarda9eec552006-08-31 21:27:45 -07003940 rv->user = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 rv->done = free_recv_msg;
3942 atomic_inc(&recv_msg_inuse_count);
3943 }
3944 return rv;
3945}
3946
Corey Minyard393d2cc2005-11-07 00:59:54 -08003947void ipmi_free_recv_msg(struct ipmi_recv_msg *msg)
3948{
3949 if (msg->user)
3950 kref_put(&msg->user->refcount, free_user);
3951 msg->done(msg);
3952}
Corey Minyardc70d7492008-04-29 01:01:09 -07003953EXPORT_SYMBOL(ipmi_free_recv_msg);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003954
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955#ifdef CONFIG_IPMI_PANIC_EVENT
3956
3957static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
3958{
3959}
3960
3961static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
3962{
3963}
3964
3965#ifdef CONFIG_IPMI_PANIC_STRING
Corey Minyard56a55ec2005-09-06 15:18:42 -07003966static void event_receiver_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967{
Corey Minyard56a55ec2005-09-06 15:18:42 -07003968 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
3969 && (msg->msg.netfn == IPMI_NETFN_SENSOR_EVENT_RESPONSE)
3970 && (msg->msg.cmd == IPMI_GET_EVENT_RECEIVER_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07003971 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 /* A get event receiver command, save it. */
Corey Minyard56a55ec2005-09-06 15:18:42 -07003973 intf->event_receiver = msg->msg.data[1];
3974 intf->event_receiver_lun = msg->msg.data[2] & 0x3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 }
3976}
3977
Corey Minyard56a55ec2005-09-06 15:18:42 -07003978static void device_id_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979{
Corey Minyard56a55ec2005-09-06 15:18:42 -07003980 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
3981 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
3982 && (msg->msg.cmd == IPMI_GET_DEVICE_ID_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07003983 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
3984 /*
3985 * A get device id command, save if we are an event
3986 * receiver or generator.
3987 */
Corey Minyard56a55ec2005-09-06 15:18:42 -07003988 intf->local_sel_device = (msg->msg.data[6] >> 2) & 1;
3989 intf->local_event_generator = (msg->msg.data[6] >> 5) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 }
3991}
3992#endif
3993
3994static void send_panic_events(char *str)
3995{
3996 struct kernel_ipmi_msg msg;
3997 ipmi_smi_t intf;
3998 unsigned char data[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 struct ipmi_system_interface_addr *si;
4000 struct ipmi_addr addr;
4001 struct ipmi_smi_msg smi_msg;
4002 struct ipmi_recv_msg recv_msg;
4003
4004 si = (struct ipmi_system_interface_addr *) &addr;
4005 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4006 si->channel = IPMI_BMC_CHANNEL;
4007 si->lun = 0;
4008
4009 /* Fill in an event telling that we have failed. */
4010 msg.netfn = 0x04; /* Sensor or Event. */
4011 msg.cmd = 2; /* Platform event command. */
4012 msg.data = data;
4013 msg.data_len = 8;
Matt Domschcda315a2005-12-12 00:37:32 -08004014 data[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 data[1] = 0x03; /* This is for IPMI 1.0. */
4016 data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
4017 data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
4018 data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
4019
Corey Minyardc70d7492008-04-29 01:01:09 -07004020 /*
4021 * Put a few breadcrumbs in. Hopefully later we can add more things
4022 * to make the panic events more useful.
4023 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 if (str) {
4025 data[3] = str[0];
4026 data[6] = str[1];
4027 data[7] = str[2];
4028 }
4029
4030 smi_msg.done = dummy_smi_done_handler;
4031 recv_msg.done = dummy_recv_done_handler;
4032
4033 /* For every registered interface, send the event. */
Corey Minyardbca03242006-12-06 20:40:57 -08004034 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
Corey Minyardb2c03942006-12-06 20:41:00 -08004035 if (!intf->handlers)
4036 /* Interface is not ready. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 continue;
4038
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07004039 intf->run_to_completion = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 /* Send the event announcing the panic. */
4041 intf->handlers->set_run_to_completion(intf->send_info, 1);
4042 i_ipmi_request(NULL,
4043 intf,
4044 &addr,
4045 0,
4046 &msg,
Corey Minyard56a55ec2005-09-06 15:18:42 -07004047 intf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 &smi_msg,
4049 &recv_msg,
4050 0,
Corey Minyardc14979b2005-09-06 15:18:38 -07004051 intf->channels[0].address,
4052 intf->channels[0].lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 0, 1); /* Don't retry, and don't wait. */
4054 }
4055
4056#ifdef CONFIG_IPMI_PANIC_STRING
Corey Minyardc70d7492008-04-29 01:01:09 -07004057 /*
4058 * On every interface, dump a bunch of OEM event holding the
4059 * string.
4060 */
4061 if (!str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 return;
4063
Corey Minyardbca03242006-12-06 20:40:57 -08004064 /* For every registered interface, send the event. */
4065 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 char *p = str;
4067 struct ipmi_ipmb_addr *ipmb;
4068 int j;
4069
Corey Minyardbca03242006-12-06 20:40:57 -08004070 if (intf->intf_num == -1)
4071 /* Interface was not ready yet. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 continue;
4073
Corey Minyard78ba2fa2007-02-10 01:45:45 -08004074 /*
4075 * intf_num is used as an marker to tell if the
4076 * interface is valid. Thus we need a read barrier to
4077 * make sure data fetched before checking intf_num
4078 * won't be used.
4079 */
4080 smp_rmb();
4081
Corey Minyardc70d7492008-04-29 01:01:09 -07004082 /*
4083 * First job here is to figure out where to send the
4084 * OEM events. There's no way in IPMI to send OEM
4085 * events using an event send command, so we have to
4086 * find the SEL to put them in and stick them in
4087 * there.
4088 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089
4090 /* Get capabilities from the get device id. */
4091 intf->local_sel_device = 0;
4092 intf->local_event_generator = 0;
4093 intf->event_receiver = 0;
4094
4095 /* Request the device info from the local MC. */
4096 msg.netfn = IPMI_NETFN_APP_REQUEST;
4097 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
4098 msg.data = NULL;
4099 msg.data_len = 0;
4100 intf->null_user_handler = device_id_fetcher;
4101 i_ipmi_request(NULL,
4102 intf,
4103 &addr,
4104 0,
4105 &msg,
Corey Minyard56a55ec2005-09-06 15:18:42 -07004106 intf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 &smi_msg,
4108 &recv_msg,
4109 0,
Corey Minyardc14979b2005-09-06 15:18:38 -07004110 intf->channels[0].address,
4111 intf->channels[0].lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 0, 1); /* Don't retry, and don't wait. */
4113
4114 if (intf->local_event_generator) {
4115 /* Request the event receiver from the local MC. */
4116 msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
4117 msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
4118 msg.data = NULL;
4119 msg.data_len = 0;
4120 intf->null_user_handler = event_receiver_fetcher;
4121 i_ipmi_request(NULL,
4122 intf,
4123 &addr,
4124 0,
4125 &msg,
Corey Minyard56a55ec2005-09-06 15:18:42 -07004126 intf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 &smi_msg,
4128 &recv_msg,
4129 0,
Corey Minyardc14979b2005-09-06 15:18:38 -07004130 intf->channels[0].address,
4131 intf->channels[0].lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 0, 1); /* no retry, and no wait. */
4133 }
4134 intf->null_user_handler = NULL;
4135
Corey Minyardc70d7492008-04-29 01:01:09 -07004136 /*
4137 * Validate the event receiver. The low bit must not
4138 * be 1 (it must be a valid IPMB address), it cannot
4139 * be zero, and it must not be my address.
4140 */
4141 if (((intf->event_receiver & 1) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 && (intf->event_receiver != 0)
Corey Minyardc70d7492008-04-29 01:01:09 -07004143 && (intf->event_receiver != intf->channels[0].address)) {
4144 /*
4145 * The event receiver is valid, send an IPMB
4146 * message.
4147 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 ipmb = (struct ipmi_ipmb_addr *) &addr;
4149 ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
4150 ipmb->channel = 0; /* FIXME - is this right? */
4151 ipmb->lun = intf->event_receiver_lun;
4152 ipmb->slave_addr = intf->event_receiver;
4153 } else if (intf->local_sel_device) {
Corey Minyardc70d7492008-04-29 01:01:09 -07004154 /*
4155 * The event receiver was not valid (or was
4156 * me), but I am an SEL device, just dump it
4157 * in my SEL.
4158 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 si = (struct ipmi_system_interface_addr *) &addr;
4160 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4161 si->channel = IPMI_BMC_CHANNEL;
4162 si->lun = 0;
4163 } else
4164 continue; /* No where to send the event. */
4165
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
4167 msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
4168 msg.data = data;
4169 msg.data_len = 16;
4170
4171 j = 0;
4172 while (*p) {
4173 int size = strlen(p);
4174
4175 if (size > 11)
4176 size = 11;
4177 data[0] = 0;
4178 data[1] = 0;
4179 data[2] = 0xf0; /* OEM event without timestamp. */
Corey Minyardc14979b2005-09-06 15:18:38 -07004180 data[3] = intf->channels[0].address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 data[4] = j++; /* sequence # */
Corey Minyardc70d7492008-04-29 01:01:09 -07004182 /*
4183 * Always give 11 bytes, so strncpy will fill
4184 * it with zeroes for me.
4185 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 strncpy(data+5, p, 11);
4187 p += size;
4188
4189 i_ipmi_request(NULL,
4190 intf,
4191 &addr,
4192 0,
4193 &msg,
Corey Minyard56a55ec2005-09-06 15:18:42 -07004194 intf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 &smi_msg,
4196 &recv_msg,
4197 0,
Corey Minyardc14979b2005-09-06 15:18:38 -07004198 intf->channels[0].address,
4199 intf->channels[0].lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 0, 1); /* no retry, and no wait. */
4201 }
Corey Minyardc70d7492008-04-29 01:01:09 -07004202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203#endif /* CONFIG_IPMI_PANIC_STRING */
4204}
4205#endif /* CONFIG_IPMI_PANIC_EVENT */
4206
Randy Dunlap0c8204b2006-12-10 02:19:06 -08004207static int has_panicked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208
4209static int panic_event(struct notifier_block *this,
4210 unsigned long event,
Corey Minyardc70d7492008-04-29 01:01:09 -07004211 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 ipmi_smi_t intf;
4214
Lee Revellf18190b2006-06-26 18:30:00 +02004215 if (has_panicked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 return NOTIFY_DONE;
Lee Revellf18190b2006-06-26 18:30:00 +02004217 has_panicked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
4219 /* For every registered interface, set it to run to completion. */
Corey Minyardbca03242006-12-06 20:40:57 -08004220 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
Corey Minyardb2c03942006-12-06 20:41:00 -08004221 if (!intf->handlers)
4222 /* Interface is not ready. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 continue;
4224
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07004225 intf->run_to_completion = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 intf->handlers->set_run_to_completion(intf->send_info, 1);
4227 }
4228
4229#ifdef CONFIG_IPMI_PANIC_EVENT
4230 send_panic_events(ptr);
4231#endif
4232
4233 return NOTIFY_DONE;
4234}
4235
4236static struct notifier_block panic_block = {
4237 .notifier_call = panic_event,
4238 .next = NULL,
4239 .priority = 200 /* priority: INT_MAX >= x >= 0 */
4240};
4241
4242static int ipmi_init_msghandler(void)
4243{
Corey Minyard50c812b2006-03-26 01:37:21 -08004244 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245
4246 if (initialized)
4247 return 0;
4248
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08004249 rv = driver_register(&ipmidriver.driver);
Corey Minyard50c812b2006-03-26 01:37:21 -08004250 if (rv) {
4251 printk(KERN_ERR PFX "Could not register IPMI driver\n");
4252 return rv;
4253 }
4254
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 printk(KERN_INFO "ipmi message handler version "
Corey Minyard1fdd75b2005-09-06 15:18:42 -07004256 IPMI_DRIVER_VERSION "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257
Corey Minyard3b625942005-06-23 22:01:42 -07004258#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 proc_ipmi_root = proc_mkdir("ipmi", NULL);
4260 if (!proc_ipmi_root) {
4261 printk(KERN_ERR PFX "Unable to create IPMI proc dir");
4262 return -ENOMEM;
4263 }
4264
Corey Minyard3b625942005-06-23 22:01:42 -07004265#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
Corey Minyard409035e2006-06-28 04:26:53 -07004267 setup_timer(&ipmi_timer, ipmi_timeout, 0);
4268 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269
Alan Sterne041c682006-03-27 01:16:30 -08004270 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
4272 initialized = 1;
4273
4274 return 0;
4275}
4276
4277static __init int ipmi_init_msghandler_mod(void)
4278{
4279 ipmi_init_msghandler();
4280 return 0;
4281}
4282
4283static __exit void cleanup_ipmi(void)
4284{
4285 int count;
4286
4287 if (!initialized)
4288 return;
4289
Alan Sterne041c682006-03-27 01:16:30 -08004290 atomic_notifier_chain_unregister(&panic_notifier_list, &panic_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291
Corey Minyardc70d7492008-04-29 01:01:09 -07004292 /*
4293 * This can't be called if any interfaces exist, so no worry
4294 * about shutting down the interfaces.
4295 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296
Corey Minyardc70d7492008-04-29 01:01:09 -07004297 /*
4298 * Tell the timer to stop, then wait for it to stop. This
4299 * avoids problems with race conditions removing the timer
4300 * here.
4301 */
Corey Minyard8f43f842005-06-23 22:01:40 -07004302 atomic_inc(&stop_operation);
4303 del_timer_sync(&ipmi_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304
Corey Minyard3b625942005-06-23 22:01:42 -07004305#ifdef CONFIG_PROC_FS
Alexey Dobriyan3542ae42007-10-16 23:26:50 -07004306 remove_proc_entry(proc_ipmi_root->name, NULL);
Corey Minyard3b625942005-06-23 22:01:42 -07004307#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08004309 driver_unregister(&ipmidriver.driver);
Corey Minyard50c812b2006-03-26 01:37:21 -08004310
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 initialized = 0;
4312
4313 /* Check for buffer leaks. */
4314 count = atomic_read(&smi_msg_inuse_count);
4315 if (count != 0)
4316 printk(KERN_WARNING PFX "SMI message count %d at exit\n",
4317 count);
4318 count = atomic_read(&recv_msg_inuse_count);
4319 if (count != 0)
4320 printk(KERN_WARNING PFX "recv message count %d at exit\n",
4321 count);
4322}
4323module_exit(cleanup_ipmi);
4324
4325module_init(ipmi_init_msghandler_mod);
4326MODULE_LICENSE("GPL");
Corey Minyard1fdd75b2005-09-06 15:18:42 -07004327MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
Corey Minyardc70d7492008-04-29 01:01:09 -07004328MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI"
4329 " interface.");
Corey Minyard1fdd75b2005-09-06 15:18:42 -07004330MODULE_VERSION(IPMI_DRIVER_VERSION);