]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/char/ipmi/ipmi_msghandler.c
ipmi: Pick up slave address from SMBIOS on an ACPI device
[mirror_ubuntu-bionic-kernel.git] / drivers / char / ipmi / ipmi_msghandler.c
CommitLineData
1da177e4
LT
1/*
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
1da177e4
LT
34#include <linux/module.h>
35#include <linux/errno.h>
1da177e4 36#include <linux/poll.h>
a99bbaf5 37#include <linux/sched.h>
07412736 38#include <linux/seq_file.h>
1da177e4 39#include <linux/spinlock.h>
d6dfd131 40#include <linux/mutex.h>
1da177e4
LT
41#include <linux/slab.h>
42#include <linux/ipmi.h>
43#include <linux/ipmi_smi.h>
44#include <linux/notifier.h>
45#include <linux/init.h>
46#include <linux/proc_fs.h>
393d2cc3 47#include <linux/rcupdate.h>
7adf579c 48#include <linux/interrupt.h>
1da177e4
LT
49
50#define PFX "IPMI message handler: "
1fdd75bd 51
f7caa1b5 52#define IPMI_DRIVER_VERSION "39.2"
1da177e4
LT
53
54static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void);
55static int ipmi_init_msghandler(void);
7adf579c
CM
56static void smi_recv_tasklet(unsigned long);
57static void handle_new_recv_msgs(ipmi_smi_t intf);
89986496 58static void need_waiter(ipmi_smi_t intf);
7ea0ed2b
CM
59static int handle_one_recv_msg(ipmi_smi_t intf,
60 struct ipmi_smi_msg *msg);
1da177e4 61
0c8204b3 62static int initialized;
1da177e4 63
3b625943 64#ifdef CONFIG_PROC_FS
0c8204b3 65static struct proc_dir_entry *proc_ipmi_root;
3b625943 66#endif /* CONFIG_PROC_FS */
1da177e4 67
b9675136
CM
68/* Remain in auto-maintenance mode for this amount of time (in ms). */
69#define IPMI_MAINTENANCE_MODE_TIMEOUT 30000
70
1da177e4
LT
71#define MAX_EVENTS_IN_QUEUE 25
72
c70d7499
CM
73/*
74 * Don't let a message sit in a queue forever, always time it with at lest
75 * the max message timer. This is in milliseconds.
76 */
1da177e4
LT
77#define MAX_MSG_TIMEOUT 60000
78
89986496
CM
79/* Call every ~1000 ms. */
80#define IPMI_TIMEOUT_TIME 1000
81
82/* How many jiffies does it take to get to the timeout time. */
83#define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000)
84
85/*
86 * Request events from the queue every second (this is the number of
87 * IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the
88 * future, IPMI will add a way to know immediately if an event is in
89 * the queue and this silliness can go away.
90 */
91#define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME))
92
393d2cc3
CM
93/*
94 * The main "user" data structure.
95 */
c70d7499 96struct ipmi_user {
1da177e4
LT
97 struct list_head link;
98
7aefac26
CM
99 /* Set to false when the user is destroyed. */
100 bool valid;
393d2cc3
CM
101
102 struct kref refcount;
103
1da177e4
LT
104 /* The upper layer that handles receive messages. */
105 struct ipmi_user_hndl *handler;
106 void *handler_data;
107
108 /* The interface this user is bound to. */
109 ipmi_smi_t intf;
110
111 /* Does this interface receive IPMI events? */
89986496 112 bool gets_events;
1da177e4
LT
113};
114
c70d7499 115struct cmd_rcvr {
1da177e4
LT
116 struct list_head link;
117
118 ipmi_user_t user;
119 unsigned char netfn;
120 unsigned char cmd;
c69c3127 121 unsigned int chans;
393d2cc3
CM
122
123 /*
124 * This is used to form a linked lised during mass deletion.
125 * Since this is in an RCU list, we cannot use the link above
126 * or change any data until the RCU period completes. So we
127 * use this next variable during mass deletion so we can have
128 * a list and don't have to wait and restart the search on
c70d7499
CM
129 * every individual deletion of a command.
130 */
393d2cc3 131 struct cmd_rcvr *next;
1da177e4
LT
132};
133
c70d7499 134struct seq_table {
1da177e4
LT
135 unsigned int inuse : 1;
136 unsigned int broadcast : 1;
137
138 unsigned long timeout;
139 unsigned long orig_timeout;
140 unsigned int retries_left;
141
c70d7499
CM
142 /*
143 * To verify on an incoming send message response that this is
144 * the message that the response is for, we keep a sequence id
145 * and increment it every time we send a message.
146 */
1da177e4
LT
147 long seqid;
148
c70d7499
CM
149 /*
150 * This is held so we can properly respond to the message on a
151 * timeout, and it is used to hold the temporary data for
152 * retransmission, too.
153 */
1da177e4
LT
154 struct ipmi_recv_msg *recv_msg;
155};
156
c70d7499
CM
157/*
158 * Store the information in a msgid (long) to allow us to find a
159 * sequence table entry from the msgid.
160 */
1da177e4
LT
161#define STORE_SEQ_IN_MSGID(seq, seqid) (((seq&0xff)<<26) | (seqid&0x3ffffff))
162
163#define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
164 do { \
165 seq = ((msgid >> 26) & 0x3f); \
166 seqid = (msgid & 0x3fffff); \
c70d7499 167 } while (0)
1da177e4
LT
168
169#define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3fffff)
170
c70d7499 171struct ipmi_channel {
1da177e4
LT
172 unsigned char medium;
173 unsigned char protocol;
c14979b9 174
c70d7499
CM
175 /*
176 * My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR,
177 * but may be changed by the user.
178 */
c14979b9
CM
179 unsigned char address;
180
c70d7499
CM
181 /*
182 * My LUN. This should generally stay the SMS LUN, but just in
183 * case...
184 */
c14979b9 185 unsigned char lun;
1da177e4
LT
186};
187
3b625943 188#ifdef CONFIG_PROC_FS
c70d7499 189struct ipmi_proc_entry {
1da177e4
LT
190 char *name;
191 struct ipmi_proc_entry *next;
192};
3b625943 193#endif
1da177e4 194
c70d7499 195struct bmc_device {
16639eb0 196 struct platform_device pdev;
50c812b2
CM
197 struct ipmi_device_id id;
198 unsigned char guid[16];
199 int guid_set;
16639eb0
CM
200 char name[16];
201 struct kref usecount;
50c812b2 202};
16639eb0 203#define to_bmc_device(x) container_of((x), struct bmc_device, pdev.dev)
50c812b2 204
b2655f26
KB
205/*
206 * Various statistics for IPMI, these index stats[] in the ipmi_smi
207 * structure.
208 */
73f2bdb9
CM
209enum ipmi_stat_indexes {
210 /* Commands we got from the user that were invalid. */
211 IPMI_STAT_sent_invalid_commands = 0,
b2655f26 212
73f2bdb9
CM
213 /* Commands we sent to the MC. */
214 IPMI_STAT_sent_local_commands,
b2655f26 215
73f2bdb9
CM
216 /* Responses from the MC that were delivered to a user. */
217 IPMI_STAT_handled_local_responses,
b2655f26 218
73f2bdb9
CM
219 /* Responses from the MC that were not delivered to a user. */
220 IPMI_STAT_unhandled_local_responses,
b2655f26 221
73f2bdb9
CM
222 /* Commands we sent out to the IPMB bus. */
223 IPMI_STAT_sent_ipmb_commands,
b2655f26 224
73f2bdb9
CM
225 /* Commands sent on the IPMB that had errors on the SEND CMD */
226 IPMI_STAT_sent_ipmb_command_errs,
b2655f26 227
73f2bdb9
CM
228 /* Each retransmit increments this count. */
229 IPMI_STAT_retransmitted_ipmb_commands,
b2655f26 230
73f2bdb9
CM
231 /*
232 * When a message times out (runs out of retransmits) this is
233 * incremented.
234 */
235 IPMI_STAT_timed_out_ipmb_commands,
b2655f26 236
73f2bdb9
CM
237 /*
238 * This is like above, but for broadcasts. Broadcasts are
239 * *not* included in the above count (they are expected to
240 * time out).
241 */
242 IPMI_STAT_timed_out_ipmb_broadcasts,
b2655f26 243
73f2bdb9
CM
244 /* Responses I have sent to the IPMB bus. */
245 IPMI_STAT_sent_ipmb_responses,
b2655f26 246
73f2bdb9
CM
247 /* The response was delivered to the user. */
248 IPMI_STAT_handled_ipmb_responses,
b2655f26 249
73f2bdb9
CM
250 /* The response had invalid data in it. */
251 IPMI_STAT_invalid_ipmb_responses,
b2655f26 252
73f2bdb9
CM
253 /* The response didn't have anyone waiting for it. */
254 IPMI_STAT_unhandled_ipmb_responses,
b2655f26 255
73f2bdb9
CM
256 /* Commands we sent out to the IPMB bus. */
257 IPMI_STAT_sent_lan_commands,
b2655f26 258
73f2bdb9
CM
259 /* Commands sent on the IPMB that had errors on the SEND CMD */
260 IPMI_STAT_sent_lan_command_errs,
b2655f26 261
73f2bdb9
CM
262 /* Each retransmit increments this count. */
263 IPMI_STAT_retransmitted_lan_commands,
b2655f26 264
73f2bdb9
CM
265 /*
266 * When a message times out (runs out of retransmits) this is
267 * incremented.
268 */
269 IPMI_STAT_timed_out_lan_commands,
270
271 /* Responses I have sent to the IPMB bus. */
272 IPMI_STAT_sent_lan_responses,
b2655f26 273
73f2bdb9
CM
274 /* The response was delivered to the user. */
275 IPMI_STAT_handled_lan_responses,
b2655f26 276
73f2bdb9
CM
277 /* The response had invalid data in it. */
278 IPMI_STAT_invalid_lan_responses,
b2655f26 279
73f2bdb9
CM
280 /* The response didn't have anyone waiting for it. */
281 IPMI_STAT_unhandled_lan_responses,
b2655f26 282
73f2bdb9
CM
283 /* The command was delivered to the user. */
284 IPMI_STAT_handled_commands,
b2655f26 285
73f2bdb9
CM
286 /* The command had invalid data in it. */
287 IPMI_STAT_invalid_commands,
b2655f26 288
73f2bdb9
CM
289 /* The command didn't have anyone waiting for it. */
290 IPMI_STAT_unhandled_commands,
b2655f26 291
73f2bdb9
CM
292 /* Invalid data in an event. */
293 IPMI_STAT_invalid_events,
b2655f26 294
73f2bdb9
CM
295 /* Events that were received with the proper format. */
296 IPMI_STAT_events,
b2655f26 297
25176ed6
CM
298 /* Retransmissions on IPMB that failed. */
299 IPMI_STAT_dropped_rexmit_ipmb_commands,
300
301 /* Retransmissions on LAN that failed. */
302 IPMI_STAT_dropped_rexmit_lan_commands,
b2655f26 303
73f2bdb9
CM
304 /* This *must* remain last, add new values above this. */
305 IPMI_NUM_STATS
306};
b2655f26
KB
307
308
1da177e4 309#define IPMI_IPMB_NUM_SEQ 64
c14979b9 310#define IPMI_MAX_CHANNELS 16
c70d7499 311struct ipmi_smi {
1da177e4
LT
312 /* What interface number are we? */
313 int intf_num;
314
393d2cc3
CM
315 struct kref refcount;
316
7ea0ed2b
CM
317 /* Set when the interface is being unregistered. */
318 bool in_shutdown;
319
bca0324d
CM
320 /* Used for a list of interfaces. */
321 struct list_head link;
322
c70d7499
CM
323 /*
324 * The list of upper layers that are using me. seq_lock
325 * protects this.
326 */
393d2cc3 327 struct list_head users;
1da177e4 328
b2c03941
CM
329 /* Information to supply to users. */
330 unsigned char ipmi_version_major;
331 unsigned char ipmi_version_minor;
332
1da177e4
LT
333 /* Used for wake ups at startup. */
334 wait_queue_head_t waitq;
335
50c812b2
CM
336 struct bmc_device *bmc;
337 char *my_dev_name;
1da177e4 338
c70d7499
CM
339 /*
340 * This is the lower-layer's sender routine. Note that you
b2c03941
CM
341 * must either be holding the ipmi_interfaces_mutex or be in
342 * an umpreemptible region to use this. You must fetch the
c70d7499
CM
343 * value into a local variable and make sure it is not NULL.
344 */
81d02b7f 345 const struct ipmi_smi_handlers *handlers;
1da177e4
LT
346 void *send_info;
347
3b625943 348#ifdef CONFIG_PROC_FS
ac019151
CM
349 /* A list of proc entries for this interface. */
350 struct mutex proc_entry_lock;
1da177e4 351 struct ipmi_proc_entry *proc_entries;
3b625943 352#endif
1da177e4 353
50c812b2
CM
354 /* Driver-model device for the system interface. */
355 struct device *si_dev;
356
c70d7499
CM
357 /*
358 * A table of sequence numbers for this interface. We use the
359 * sequence numbers for IPMB messages that go out of the
360 * interface to match them up with their responses. A routine
361 * is called periodically to time the items in this list.
362 */
1da177e4
LT
363 spinlock_t seq_lock;
364 struct seq_table seq_table[IPMI_IPMB_NUM_SEQ];
365 int curr_seq;
366
c70d7499 367 /*
7adf579c
CM
368 * Messages queued for delivery. If delivery fails (out of memory
369 * for instance), They will stay in here to be processed later in a
370 * periodic timer interrupt. The tasklet is for handling received
371 * messages directly from the handler.
c70d7499 372 */
65be7544
CM
373 spinlock_t waiting_rcv_msgs_lock;
374 struct list_head waiting_rcv_msgs;
7adf579c
CM
375 atomic_t watchdog_pretimeouts_to_deliver;
376 struct tasklet_struct recv_tasklet;
1da177e4 377
7ea0ed2b
CM
378 spinlock_t xmit_msgs_lock;
379 struct list_head xmit_msgs;
380 struct ipmi_smi_msg *curr_msg;
381 struct list_head hp_xmit_msgs;
382
c70d7499
CM
383 /*
384 * The list of command receivers that are registered for commands
385 * on this interface.
386 */
d6dfd131 387 struct mutex cmd_rcvrs_mutex;
1da177e4
LT
388 struct list_head cmd_rcvrs;
389
c70d7499
CM
390 /*
391 * Events that were queues because no one was there to receive
392 * them.
393 */
1da177e4
LT
394 spinlock_t events_lock; /* For dealing with event stuff. */
395 struct list_head waiting_events;
396 unsigned int waiting_events_count; /* How many events in queue? */
87ebd06f
CM
397 char delivering_events;
398 char event_msg_printed;
89986496
CM
399 atomic_t event_waiters;
400 unsigned int ticks_to_req_ev;
401 int last_needs_timer;
1da177e4 402
c70d7499
CM
403 /*
404 * The event receiver for my BMC, only really used at panic
405 * shutdown as a place to store this.
406 */
1da177e4
LT
407 unsigned char event_receiver;
408 unsigned char event_receiver_lun;
409 unsigned char local_sel_device;
410 unsigned char local_event_generator;
411
b9675136
CM
412 /* For handling of maintenance mode. */
413 int maintenance_mode;
7aefac26 414 bool maintenance_mode_enable;
b9675136
CM
415 int auto_maintenance_timeout;
416 spinlock_t maintenance_mode_lock; /* Used in a timer... */
417
c70d7499
CM
418 /*
419 * A cheap hack, if this is non-null and a message to an
420 * interface comes in with a NULL user, call this routine with
421 * it. Note that the message will still be freed by the
422 * caller. This only works on the system interface.
423 */
56a55ec6 424 void (*null_user_handler)(ipmi_smi_t intf, struct ipmi_recv_msg *msg);
1da177e4 425
c70d7499
CM
426 /*
427 * When we are scanning the channels for an SMI, this will
428 * tell which channel we are scanning.
429 */
1da177e4
LT
430 int curr_channel;
431
432 /* Channel information */
433 struct ipmi_channel channels[IPMI_MAX_CHANNELS];
434
435 /* Proc FS stuff. */
436 struct proc_dir_entry *proc_dir;
437 char proc_dir_name[10];
438
b2655f26 439 atomic_t stats[IPMI_NUM_STATS];
5956dce1
KB
440
441 /*
442 * run_to_completion duplicate of smb_info, smi_info
443 * and ipmi_serial_info structures. Used to decrease numbers of
444 * parameters passed by "low" level IPMI code.
445 */
446 int run_to_completion;
1da177e4 447};
50c812b2 448#define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev)
1da177e4 449
50c812b2
CM
450/**
451 * The driver model view of the IPMI messaging driver.
452 */
fe2d5ffc
DW
453static struct platform_driver ipmidriver = {
454 .driver = {
455 .name = "ipmi",
456 .bus = &platform_bus_type
457 }
50c812b2
CM
458};
459static DEFINE_MUTEX(ipmidriver_mutex);
460
bed9759b 461static LIST_HEAD(ipmi_interfaces);
bca0324d 462static DEFINE_MUTEX(ipmi_interfaces_mutex);
1da177e4 463
c70d7499
CM
464/*
465 * List of watchers that want to know when smi's are added and deleted.
466 */
bed9759b 467static LIST_HEAD(smi_watchers);
b2c03941 468static DEFINE_MUTEX(smi_watchers_mutex);
1da177e4 469
b2655f26
KB
470#define ipmi_inc_stat(intf, stat) \
471 atomic_inc(&(intf)->stats[IPMI_STAT_ ## stat])
472#define ipmi_get_stat(intf, stat) \
473 ((unsigned int) atomic_read(&(intf)->stats[IPMI_STAT_ ## stat]))
474
99ee6735
LC
475static const char * const addr_src_to_str[] = {
476 "invalid", "hotmod", "hardcoded", "SPMI", "ACPI", "SMBIOS", "PCI",
b07b58a3 477 "device-tree"
99ee6735 478};
7e50387b
CM
479
480const char *ipmi_addr_src_to_str(enum ipmi_addr_src src)
481{
b07b58a3 482 if (src >= SI_LAST)
7e50387b
CM
483 src = 0; /* Invalid */
484 return addr_src_to_str[src];
485}
486EXPORT_SYMBOL(ipmi_addr_src_to_str);
487
25176ed6
CM
488static int is_lan_addr(struct ipmi_addr *addr)
489{
490 return addr->addr_type == IPMI_LAN_ADDR_TYPE;
491}
492
493static int is_ipmb_addr(struct ipmi_addr *addr)
494{
495 return addr->addr_type == IPMI_IPMB_ADDR_TYPE;
496}
497
498static int is_ipmb_bcast_addr(struct ipmi_addr *addr)
499{
500 return addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE;
501}
b2655f26 502
393d2cc3
CM
503static void free_recv_msg_list(struct list_head *q)
504{
505 struct ipmi_recv_msg *msg, *msg2;
506
507 list_for_each_entry_safe(msg, msg2, q, link) {
508 list_del(&msg->link);
509 ipmi_free_recv_msg(msg);
510 }
511}
512
f3ce6a0e
CM
513static void free_smi_msg_list(struct list_head *q)
514{
515 struct ipmi_smi_msg *msg, *msg2;
516
517 list_for_each_entry_safe(msg, msg2, q, link) {
518 list_del(&msg->link);
519 ipmi_free_smi_msg(msg);
520 }
521}
522
393d2cc3
CM
523static void clean_up_interface_data(ipmi_smi_t intf)
524{
525 int i;
526 struct cmd_rcvr *rcvr, *rcvr2;
393d2cc3
CM
527 struct list_head list;
528
7adf579c
CM
529 tasklet_kill(&intf->recv_tasklet);
530
65be7544 531 free_smi_msg_list(&intf->waiting_rcv_msgs);
393d2cc3
CM
532 free_recv_msg_list(&intf->waiting_events);
533
78ba2faf
CM
534 /*
535 * Wholesale remove all the entries from the list in the
536 * interface and wait for RCU to know that none are in use.
537 */
d6dfd131 538 mutex_lock(&intf->cmd_rcvrs_mutex);
78ba2faf
CM
539 INIT_LIST_HEAD(&list);
540 list_splice_init_rcu(&intf->cmd_rcvrs, &list, synchronize_rcu);
d6dfd131 541 mutex_unlock(&intf->cmd_rcvrs_mutex);
393d2cc3
CM
542
543 list_for_each_entry_safe(rcvr, rcvr2, &list, link)
544 kfree(rcvr);
545
546 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
547 if ((intf->seq_table[i].inuse)
c70d7499 548 && (intf->seq_table[i].recv_msg))
393d2cc3 549 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
393d2cc3
CM
550 }
551}
552
553static void intf_free(struct kref *ref)
554{
555 ipmi_smi_t intf = container_of(ref, struct ipmi_smi, refcount);
556
557 clean_up_interface_data(intf);
558 kfree(intf);
559}
560
bca0324d 561struct watcher_entry {
b2c03941
CM
562 int intf_num;
563 ipmi_smi_t intf;
bca0324d 564 struct list_head link;
bca0324d
CM
565};
566
1da177e4
LT
567int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher)
568{
bca0324d 569 ipmi_smi_t intf;
e381d1c4 570 LIST_HEAD(to_deliver);
bca0324d
CM
571 struct watcher_entry *e, *e2;
572
b2c03941
CM
573 mutex_lock(&smi_watchers_mutex);
574
bca0324d
CM
575 mutex_lock(&ipmi_interfaces_mutex);
576
b2c03941 577 /* Build a list of things to deliver. */
78ba2faf 578 list_for_each_entry(intf, &ipmi_interfaces, link) {
bca0324d
CM
579 if (intf->intf_num == -1)
580 continue;
581 e = kmalloc(sizeof(*e), GFP_KERNEL);
582 if (!e)
583 goto out_err;
b2c03941
CM
584 kref_get(&intf->refcount);
585 e->intf = intf;
bca0324d
CM
586 e->intf_num = intf->intf_num;
587 list_add_tail(&e->link, &to_deliver);
588 }
1da177e4 589
b2c03941
CM
590 /* We will succeed, so add it to the list. */
591 list_add(&watcher->link, &smi_watchers);
bca0324d
CM
592
593 mutex_unlock(&ipmi_interfaces_mutex);
594
595 list_for_each_entry_safe(e, e2, &to_deliver, link) {
596 list_del(&e->link);
b2c03941
CM
597 watcher->new_smi(e->intf_num, e->intf->si_dev);
598 kref_put(&e->intf->refcount, intf_free);
bca0324d 599 kfree(e);
1da177e4 600 }
bca0324d 601
b2c03941 602 mutex_unlock(&smi_watchers_mutex);
bca0324d 603
1da177e4 604 return 0;
bca0324d
CM
605
606 out_err:
b2c03941
CM
607 mutex_unlock(&ipmi_interfaces_mutex);
608 mutex_unlock(&smi_watchers_mutex);
bca0324d
CM
609 list_for_each_entry_safe(e, e2, &to_deliver, link) {
610 list_del(&e->link);
b2c03941 611 kref_put(&e->intf->refcount, intf_free);
bca0324d
CM
612 kfree(e);
613 }
614 return -ENOMEM;
1da177e4 615}
c70d7499 616EXPORT_SYMBOL(ipmi_smi_watcher_register);
1da177e4
LT
617
618int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher)
619{
b2c03941 620 mutex_lock(&smi_watchers_mutex);
1da177e4 621 list_del(&(watcher->link));
b2c03941 622 mutex_unlock(&smi_watchers_mutex);
1da177e4
LT
623 return 0;
624}
c70d7499 625EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
1da177e4 626
b2c03941
CM
627/*
628 * Must be called with smi_watchers_mutex held.
629 */
1da177e4 630static void
50c812b2 631call_smi_watchers(int i, struct device *dev)
1da177e4
LT
632{
633 struct ipmi_smi_watcher *w;
634
1da177e4
LT
635 list_for_each_entry(w, &smi_watchers, link) {
636 if (try_module_get(w->owner)) {
50c812b2 637 w->new_smi(i, dev);
1da177e4
LT
638 module_put(w->owner);
639 }
640 }
1da177e4
LT
641}
642
643static int
644ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2)
645{
646 if (addr1->addr_type != addr2->addr_type)
647 return 0;
648
649 if (addr1->channel != addr2->channel)
650 return 0;
651
652 if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
653 struct ipmi_system_interface_addr *smi_addr1
654 = (struct ipmi_system_interface_addr *) addr1;
655 struct ipmi_system_interface_addr *smi_addr2
656 = (struct ipmi_system_interface_addr *) addr2;
657 return (smi_addr1->lun == smi_addr2->lun);
658 }
659
25176ed6 660 if (is_ipmb_addr(addr1) || is_ipmb_bcast_addr(addr1)) {
1da177e4
LT
661 struct ipmi_ipmb_addr *ipmb_addr1
662 = (struct ipmi_ipmb_addr *) addr1;
663 struct ipmi_ipmb_addr *ipmb_addr2
664 = (struct ipmi_ipmb_addr *) addr2;
665
666 return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr)
667 && (ipmb_addr1->lun == ipmb_addr2->lun));
668 }
669
25176ed6 670 if (is_lan_addr(addr1)) {
1da177e4
LT
671 struct ipmi_lan_addr *lan_addr1
672 = (struct ipmi_lan_addr *) addr1;
673 struct ipmi_lan_addr *lan_addr2
674 = (struct ipmi_lan_addr *) addr2;
675
676 return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID)
677 && (lan_addr1->local_SWID == lan_addr2->local_SWID)
678 && (lan_addr1->session_handle
679 == lan_addr2->session_handle)
680 && (lan_addr1->lun == lan_addr2->lun));
681 }
682
683 return 1;
684}
685
686int ipmi_validate_addr(struct ipmi_addr *addr, int len)
687{
c70d7499 688 if (len < sizeof(struct ipmi_system_interface_addr))
1da177e4 689 return -EINVAL;
1da177e4
LT
690
691 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
692 if (addr->channel != IPMI_BMC_CHANNEL)
693 return -EINVAL;
694 return 0;
695 }
696
697 if ((addr->channel == IPMI_BMC_CHANNEL)
12fc1d7b 698 || (addr->channel >= IPMI_MAX_CHANNELS)
1da177e4
LT
699 || (addr->channel < 0))
700 return -EINVAL;
701
25176ed6 702 if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
c70d7499 703 if (len < sizeof(struct ipmi_ipmb_addr))
1da177e4 704 return -EINVAL;
1da177e4
LT
705 return 0;
706 }
707
25176ed6 708 if (is_lan_addr(addr)) {
c70d7499 709 if (len < sizeof(struct ipmi_lan_addr))
1da177e4 710 return -EINVAL;
1da177e4
LT
711 return 0;
712 }
713
714 return -EINVAL;
715}
c70d7499 716EXPORT_SYMBOL(ipmi_validate_addr);
1da177e4
LT
717
718unsigned int ipmi_addr_length(int addr_type)
719{
720 if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
721 return sizeof(struct ipmi_system_interface_addr);
722
723 if ((addr_type == IPMI_IPMB_ADDR_TYPE)
c70d7499 724 || (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
1da177e4 725 return sizeof(struct ipmi_ipmb_addr);
1da177e4
LT
726
727 if (addr_type == IPMI_LAN_ADDR_TYPE)
728 return sizeof(struct ipmi_lan_addr);
729
730 return 0;
731}
c70d7499 732EXPORT_SYMBOL(ipmi_addr_length);
1da177e4
LT
733
734static void deliver_response(struct ipmi_recv_msg *msg)
735{
8a3628d5 736 if (!msg->user) {
56a55ec6 737 ipmi_smi_t intf = msg->user_msg_data;
56a55ec6
CM
738
739 /* Special handling for NULL users. */
740 if (intf->null_user_handler) {
741 intf->null_user_handler(intf, msg);
b2655f26 742 ipmi_inc_stat(intf, handled_local_responses);
56a55ec6
CM
743 } else {
744 /* No handler, so give up. */
b2655f26 745 ipmi_inc_stat(intf, unhandled_local_responses);
56a55ec6
CM
746 }
747 ipmi_free_recv_msg(msg);
c49c0976
HK
748 } else if (!oops_in_progress) {
749 /*
750 * If we are running in the panic context, calling the
751 * receive handler doesn't much meaning and has a deadlock
752 * risk. At this moment, simply skip it in that case.
753 */
754
393d2cc3
CM
755 ipmi_user_t user = msg->user;
756 user->handler->ipmi_recv_hndl(msg, user->handler_data);
56a55ec6 757 }
1da177e4
LT
758}
759
b2c03941
CM
760static void
761deliver_err_response(struct ipmi_recv_msg *msg, int err)
762{
763 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
764 msg->msg_data[0] = err;
765 msg->msg.netfn |= 1; /* Convert to a response. */
766 msg->msg.data_len = 1;
767 msg->msg.data = msg->msg_data;
768 deliver_response(msg);
769}
770
c70d7499
CM
771/*
772 * Find the next sequence number not being used and add the given
773 * message with the given timeout to the sequence table. This must be
774 * called with the interface's seq_lock held.
775 */
1da177e4
LT
776static int intf_next_seq(ipmi_smi_t intf,
777 struct ipmi_recv_msg *recv_msg,
778 unsigned long timeout,
779 int retries,
780 int broadcast,
781 unsigned char *seq,
782 long *seqid)
783{
784 int rv = 0;
785 unsigned int i;
786
c70d7499
CM
787 for (i = intf->curr_seq; (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq;
788 i = (i+1)%IPMI_IPMB_NUM_SEQ) {
8a3628d5 789 if (!intf->seq_table[i].inuse)
1da177e4
LT
790 break;
791 }
792
8a3628d5 793 if (!intf->seq_table[i].inuse) {
1da177e4
LT
794 intf->seq_table[i].recv_msg = recv_msg;
795
c70d7499
CM
796 /*
797 * Start with the maximum timeout, when the send response
798 * comes in we will start the real timer.
799 */
1da177e4
LT
800 intf->seq_table[i].timeout = MAX_MSG_TIMEOUT;
801 intf->seq_table[i].orig_timeout = timeout;
802 intf->seq_table[i].retries_left = retries;
803 intf->seq_table[i].broadcast = broadcast;
804 intf->seq_table[i].inuse = 1;
805 intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid);
806 *seq = i;
807 *seqid = intf->seq_table[i].seqid;
808 intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ;
89986496 809 need_waiter(intf);
1da177e4
LT
810 } else {
811 rv = -EAGAIN;
812 }
c70d7499 813
1da177e4
LT
814 return rv;
815}
816
c70d7499
CM
817/*
818 * Return the receive message for the given sequence number and
819 * release the sequence number so it can be reused. Some other data
820 * is passed in to be sure the message matches up correctly (to help
821 * guard against message coming in after their timeout and the
822 * sequence number being reused).
823 */
1da177e4
LT
824static int intf_find_seq(ipmi_smi_t intf,
825 unsigned char seq,
826 short channel,
827 unsigned char cmd,
828 unsigned char netfn,
829 struct ipmi_addr *addr,
830 struct ipmi_recv_msg **recv_msg)
831{
832 int rv = -ENODEV;
833 unsigned long flags;
834
835 if (seq >= IPMI_IPMB_NUM_SEQ)
836 return -EINVAL;
837
838 spin_lock_irqsave(&(intf->seq_lock), flags);
839 if (intf->seq_table[seq].inuse) {
840 struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg;
841
c70d7499
CM
842 if ((msg->addr.channel == channel) && (msg->msg.cmd == cmd)
843 && (msg->msg.netfn == netfn)
844 && (ipmi_addr_equal(addr, &(msg->addr)))) {
1da177e4
LT
845 *recv_msg = msg;
846 intf->seq_table[seq].inuse = 0;
847 rv = 0;
848 }
849 }
850 spin_unlock_irqrestore(&(intf->seq_lock), flags);
851
852 return rv;
853}
854
855
856/* Start the timer for a specific sequence table entry. */
857static int intf_start_seq_timer(ipmi_smi_t intf,
858 long msgid)
859{
860 int rv = -ENODEV;
861 unsigned long flags;
862 unsigned char seq;
863 unsigned long seqid;
864
865
866 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
867
868 spin_lock_irqsave(&(intf->seq_lock), flags);
c70d7499
CM
869 /*
870 * We do this verification because the user can be deleted
871 * while a message is outstanding.
872 */
1da177e4 873 if ((intf->seq_table[seq].inuse)
c70d7499 874 && (intf->seq_table[seq].seqid == seqid)) {
1da177e4
LT
875 struct seq_table *ent = &(intf->seq_table[seq]);
876 ent->timeout = ent->orig_timeout;
877 rv = 0;
878 }
879 spin_unlock_irqrestore(&(intf->seq_lock), flags);
880
881 return rv;
882}
883
884/* Got an error for the send message for a specific sequence number. */
885static int intf_err_seq(ipmi_smi_t intf,
886 long msgid,
887 unsigned int err)
888{
889 int rv = -ENODEV;
890 unsigned long flags;
891 unsigned char seq;
892 unsigned long seqid;
893 struct ipmi_recv_msg *msg = NULL;
894
895
896 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
897
898 spin_lock_irqsave(&(intf->seq_lock), flags);
c70d7499
CM
899 /*
900 * We do this verification because the user can be deleted
901 * while a message is outstanding.
902 */
1da177e4 903 if ((intf->seq_table[seq].inuse)
c70d7499 904 && (intf->seq_table[seq].seqid == seqid)) {
1da177e4
LT
905 struct seq_table *ent = &(intf->seq_table[seq]);
906
907 ent->inuse = 0;
908 msg = ent->recv_msg;
909 rv = 0;
910 }
911 spin_unlock_irqrestore(&(intf->seq_lock), flags);
912
b2c03941
CM
913 if (msg)
914 deliver_err_response(msg, err);
1da177e4
LT
915
916 return rv;
917}
918
919
920int ipmi_create_user(unsigned int if_num,
921 struct ipmi_user_hndl *handler,
922 void *handler_data,
923 ipmi_user_t *user)
924{
925 unsigned long flags;
926 ipmi_user_t new_user;
927 int rv = 0;
928 ipmi_smi_t intf;
929
c70d7499
CM
930 /*
931 * There is no module usecount here, because it's not
932 * required. Since this can only be used by and called from
933 * other modules, they will implicitly use this module, and
934 * thus this can't be removed unless the other modules are
935 * removed.
936 */
1da177e4
LT
937
938 if (handler == NULL)
939 return -EINVAL;
940
c70d7499
CM
941 /*
942 * Make sure the driver is actually initialized, this handles
943 * problems with initialization order.
944 */
1da177e4
LT
945 if (!initialized) {
946 rv = ipmi_init_msghandler();
947 if (rv)
948 return rv;
949
c70d7499
CM
950 /*
951 * The init code doesn't return an error if it was turned
952 * off, but it won't initialize. Check that.
953 */
1da177e4
LT
954 if (!initialized)
955 return -ENODEV;
956 }
957
958 new_user = kmalloc(sizeof(*new_user), GFP_KERNEL);
8a3628d5 959 if (!new_user)
1da177e4
LT
960 return -ENOMEM;
961
b2c03941 962 mutex_lock(&ipmi_interfaces_mutex);
bca0324d
CM
963 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
964 if (intf->intf_num == if_num)
965 goto found;
1da177e4 966 }
b2c03941 967 /* Not found, return an error */
bca0324d
CM
968 rv = -EINVAL;
969 goto out_kfree;
1da177e4 970
bca0324d 971 found:
393d2cc3
CM
972 /* Note that each existing user holds a refcount to the interface. */
973 kref_get(&intf->refcount);
1da177e4 974
393d2cc3 975 kref_init(&new_user->refcount);
1da177e4
LT
976 new_user->handler = handler;
977 new_user->handler_data = handler_data;
978 new_user->intf = intf;
89986496 979 new_user->gets_events = false;
1da177e4
LT
980
981 if (!try_module_get(intf->handlers->owner)) {
982 rv = -ENODEV;
5c98d29a 983 goto out_kref;
1da177e4
LT
984 }
985
986 if (intf->handlers->inc_usecount) {
987 rv = intf->handlers->inc_usecount(intf->send_info);
988 if (rv) {
989 module_put(intf->handlers->owner);
5c98d29a 990 goto out_kref;
1da177e4
LT
991 }
992 }
993
c70d7499
CM
994 /*
995 * Hold the lock so intf->handlers is guaranteed to be good
996 * until now
997 */
b2c03941
CM
998 mutex_unlock(&ipmi_interfaces_mutex);
999
7aefac26 1000 new_user->valid = true;
393d2cc3
CM
1001 spin_lock_irqsave(&intf->seq_lock, flags);
1002 list_add_rcu(&new_user->link, &intf->users);
1003 spin_unlock_irqrestore(&intf->seq_lock, flags);
89986496
CM
1004 if (handler->ipmi_watchdog_pretimeout) {
1005 /* User wants pretimeouts, so make sure to watch for them. */
1006 if (atomic_inc_return(&intf->event_waiters) == 1)
1007 need_waiter(intf);
1008 }
393d2cc3
CM
1009 *user = new_user;
1010 return 0;
1da177e4 1011
5c98d29a 1012out_kref:
393d2cc3 1013 kref_put(&intf->refcount, intf_free);
5c98d29a 1014out_kfree:
b2c03941 1015 mutex_unlock(&ipmi_interfaces_mutex);
5c98d29a 1016 kfree(new_user);
1da177e4
LT
1017 return rv;
1018}
c70d7499 1019EXPORT_SYMBOL(ipmi_create_user);
1da177e4 1020
16f4232c
ZY
1021int ipmi_get_smi_info(int if_num, struct ipmi_smi_info *data)
1022{
1023 int rv = 0;
1024 ipmi_smi_t intf;
81d02b7f 1025 const struct ipmi_smi_handlers *handlers;
16f4232c
ZY
1026
1027 mutex_lock(&ipmi_interfaces_mutex);
1028 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
1029 if (intf->intf_num == if_num)
1030 goto found;
1031 }
1032 /* Not found, return an error */
1033 rv = -EINVAL;
1034 mutex_unlock(&ipmi_interfaces_mutex);
1035 return rv;
1036
1037found:
1038 handlers = intf->handlers;
1039 rv = -ENOSYS;
1040 if (handlers->get_smi_info)
1041 rv = handlers->get_smi_info(intf->send_info, data);
1042 mutex_unlock(&ipmi_interfaces_mutex);
1043
1044 return rv;
1045}
1046EXPORT_SYMBOL(ipmi_get_smi_info);
1047
393d2cc3
CM
1048static void free_user(struct kref *ref)
1049{
1050 ipmi_user_t user = container_of(ref, struct ipmi_user, refcount);
1051 kfree(user);
1052}
1053
1054int ipmi_destroy_user(ipmi_user_t user)
1da177e4 1055{
393d2cc3 1056 ipmi_smi_t intf = user->intf;
1da177e4
LT
1057 int i;
1058 unsigned long flags;
393d2cc3 1059 struct cmd_rcvr *rcvr;
393d2cc3 1060 struct cmd_rcvr *rcvrs = NULL;
1da177e4 1061
7aefac26 1062 user->valid = false;
1da177e4 1063
89986496
CM
1064 if (user->handler->ipmi_watchdog_pretimeout)
1065 atomic_dec(&intf->event_waiters);
1066
1067 if (user->gets_events)
1068 atomic_dec(&intf->event_waiters);
1069
393d2cc3
CM
1070 /* Remove the user from the interface's sequence table. */
1071 spin_lock_irqsave(&intf->seq_lock, flags);
1072 list_del_rcu(&user->link);
1da177e4 1073
e8b33617 1074 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
393d2cc3 1075 if (intf->seq_table[i].inuse
c70d7499 1076 && (intf->seq_table[i].recv_msg->user == user)) {
393d2cc3 1077 intf->seq_table[i].inuse = 0;
b2c03941 1078 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
1da177e4
LT
1079 }
1080 }
393d2cc3
CM
1081 spin_unlock_irqrestore(&intf->seq_lock, flags);
1082
1083 /*
1084 * Remove the user from the command receiver's table. First
1085 * we build a list of everything (not using the standard link,
1086 * since other things may be using it till we do
1087 * synchronize_rcu()) then free everything in that list.
1088 */
d6dfd131 1089 mutex_lock(&intf->cmd_rcvrs_mutex);
066bb8d0 1090 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1da177e4 1091 if (rcvr->user == user) {
393d2cc3
CM
1092 list_del_rcu(&rcvr->link);
1093 rcvr->next = rcvrs;
1094 rcvrs = rcvr;
1da177e4
LT
1095 }
1096 }
d6dfd131 1097 mutex_unlock(&intf->cmd_rcvrs_mutex);
393d2cc3
CM
1098 synchronize_rcu();
1099 while (rcvrs) {
1100 rcvr = rcvrs;
1101 rcvrs = rcvr->next;
1102 kfree(rcvr);
1103 }
1da177e4 1104
b2c03941
CM
1105 mutex_lock(&ipmi_interfaces_mutex);
1106 if (intf->handlers) {
1107 module_put(intf->handlers->owner);
1108 if (intf->handlers->dec_usecount)
1109 intf->handlers->dec_usecount(intf->send_info);
1110 }
1111 mutex_unlock(&ipmi_interfaces_mutex);
1da177e4 1112
393d2cc3 1113 kref_put(&intf->refcount, intf_free);
1da177e4 1114
393d2cc3 1115 kref_put(&user->refcount, free_user);
1da177e4 1116
8a3628d5 1117 return 0;
1da177e4 1118}
c70d7499 1119EXPORT_SYMBOL(ipmi_destroy_user);
1da177e4
LT
1120
1121void ipmi_get_version(ipmi_user_t user,
1122 unsigned char *major,
1123 unsigned char *minor)
1124{
b2c03941
CM
1125 *major = user->intf->ipmi_version_major;
1126 *minor = user->intf->ipmi_version_minor;
1da177e4 1127}
c70d7499 1128EXPORT_SYMBOL(ipmi_get_version);
1da177e4 1129
c14979b9
CM
1130int ipmi_set_my_address(ipmi_user_t user,
1131 unsigned int channel,
1132 unsigned char address)
1da177e4 1133{
c14979b9
CM
1134 if (channel >= IPMI_MAX_CHANNELS)
1135 return -EINVAL;
1136 user->intf->channels[channel].address = address;
1137 return 0;
1da177e4 1138}
c70d7499 1139EXPORT_SYMBOL(ipmi_set_my_address);
1da177e4 1140
c14979b9
CM
1141int ipmi_get_my_address(ipmi_user_t user,
1142 unsigned int channel,
1143 unsigned char *address)
1da177e4 1144{
c14979b9
CM
1145 if (channel >= IPMI_MAX_CHANNELS)
1146 return -EINVAL;
1147 *address = user->intf->channels[channel].address;
1148 return 0;
1da177e4 1149}
c70d7499 1150EXPORT_SYMBOL(ipmi_get_my_address);
1da177e4 1151
c14979b9
CM
1152int ipmi_set_my_LUN(ipmi_user_t user,
1153 unsigned int channel,
1154 unsigned char LUN)
1da177e4 1155{
c14979b9
CM
1156 if (channel >= IPMI_MAX_CHANNELS)
1157 return -EINVAL;
1158 user->intf->channels[channel].lun = LUN & 0x3;
1159 return 0;
1da177e4 1160}
c70d7499 1161EXPORT_SYMBOL(ipmi_set_my_LUN);
1da177e4 1162
c14979b9
CM
1163int ipmi_get_my_LUN(ipmi_user_t user,
1164 unsigned int channel,
1165 unsigned char *address)
1da177e4 1166{
c14979b9
CM
1167 if (channel >= IPMI_MAX_CHANNELS)
1168 return -EINVAL;
1169 *address = user->intf->channels[channel].lun;
1170 return 0;
1da177e4 1171}
c70d7499 1172EXPORT_SYMBOL(ipmi_get_my_LUN);
1da177e4 1173
b9675136
CM
1174int ipmi_get_maintenance_mode(ipmi_user_t user)
1175{
1176 int mode;
1177 unsigned long flags;
1178
1179 spin_lock_irqsave(&user->intf->maintenance_mode_lock, flags);
1180 mode = user->intf->maintenance_mode;
1181 spin_unlock_irqrestore(&user->intf->maintenance_mode_lock, flags);
1182
1183 return mode;
1184}
1185EXPORT_SYMBOL(ipmi_get_maintenance_mode);
1186
1187static void maintenance_mode_update(ipmi_smi_t intf)
1188{
1189 if (intf->handlers->set_maintenance_mode)
1190 intf->handlers->set_maintenance_mode(
1191 intf->send_info, intf->maintenance_mode_enable);
1192}
1193
1194int ipmi_set_maintenance_mode(ipmi_user_t user, int mode)
1195{
1196 int rv = 0;
1197 unsigned long flags;
1198 ipmi_smi_t intf = user->intf;
1199
1200 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1201 if (intf->maintenance_mode != mode) {
1202 switch (mode) {
1203 case IPMI_MAINTENANCE_MODE_AUTO:
b9675136
CM
1204 intf->maintenance_mode_enable
1205 = (intf->auto_maintenance_timeout > 0);
1206 break;
1207
1208 case IPMI_MAINTENANCE_MODE_OFF:
7aefac26 1209 intf->maintenance_mode_enable = false;
b9675136
CM
1210 break;
1211
1212 case IPMI_MAINTENANCE_MODE_ON:
7aefac26 1213 intf->maintenance_mode_enable = true;
b9675136
CM
1214 break;
1215
1216 default:
1217 rv = -EINVAL;
1218 goto out_unlock;
1219 }
7aefac26 1220 intf->maintenance_mode = mode;
b9675136
CM
1221
1222 maintenance_mode_update(intf);
1223 }
1224 out_unlock:
1225 spin_unlock_irqrestore(&intf->maintenance_mode_lock, flags);
1226
1227 return rv;
1228}
1229EXPORT_SYMBOL(ipmi_set_maintenance_mode);
1230
89986496 1231int ipmi_set_gets_events(ipmi_user_t user, bool val)
1da177e4 1232{
393d2cc3
CM
1233 unsigned long flags;
1234 ipmi_smi_t intf = user->intf;
1235 struct ipmi_recv_msg *msg, *msg2;
1236 struct list_head msgs;
1da177e4 1237
393d2cc3
CM
1238 INIT_LIST_HEAD(&msgs);
1239
1240 spin_lock_irqsave(&intf->events_lock, flags);
89986496
CM
1241 if (user->gets_events == val)
1242 goto out;
1243
1da177e4
LT
1244 user->gets_events = val;
1245
89986496
CM
1246 if (val) {
1247 if (atomic_inc_return(&intf->event_waiters) == 1)
1248 need_waiter(intf);
1249 } else {
1250 atomic_dec(&intf->event_waiters);
1251 }
1252
b2c03941
CM
1253 if (intf->delivering_events)
1254 /*
1255 * Another thread is delivering events for this, so
1256 * let it handle any new events.
1257 */
1258 goto out;
1259
1260 /* Deliver any queued events. */
1261 while (user->gets_events && !list_empty(&intf->waiting_events)) {
179e0917
AM
1262 list_for_each_entry_safe(msg, msg2, &intf->waiting_events, link)
1263 list_move_tail(&msg->link, &msgs);
4791c03d 1264 intf->waiting_events_count = 0;
87ebd06f
CM
1265 if (intf->event_msg_printed) {
1266 printk(KERN_WARNING PFX "Event queue no longer"
1267 " full\n");
1268 intf->event_msg_printed = 0;
1269 }
393d2cc3 1270
b2c03941
CM
1271 intf->delivering_events = 1;
1272 spin_unlock_irqrestore(&intf->events_lock, flags);
1273
1274 list_for_each_entry_safe(msg, msg2, &msgs, link) {
1275 msg->user = user;
1276 kref_get(&user->refcount);
1277 deliver_response(msg);
1278 }
1279
1280 spin_lock_irqsave(&intf->events_lock, flags);
1281 intf->delivering_events = 0;
393d2cc3
CM
1282 }
1283
b2c03941 1284 out:
393d2cc3 1285 spin_unlock_irqrestore(&intf->events_lock, flags);
1da177e4
LT
1286
1287 return 0;
1288}
c70d7499 1289EXPORT_SYMBOL(ipmi_set_gets_events);
1da177e4 1290
393d2cc3
CM
1291static struct cmd_rcvr *find_cmd_rcvr(ipmi_smi_t intf,
1292 unsigned char netfn,
c69c3127
CM
1293 unsigned char cmd,
1294 unsigned char chan)
393d2cc3
CM
1295{
1296 struct cmd_rcvr *rcvr;
1297
1298 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
c69c3127
CM
1299 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1300 && (rcvr->chans & (1 << chan)))
393d2cc3
CM
1301 return rcvr;
1302 }
1303 return NULL;
1304}
1305
c69c3127
CM
1306static int is_cmd_rcvr_exclusive(ipmi_smi_t intf,
1307 unsigned char netfn,
1308 unsigned char cmd,
1309 unsigned int chans)
1310{
1311 struct cmd_rcvr *rcvr;
1312
1313 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1314 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1315 && (rcvr->chans & chans))
1316 return 0;
1317 }
1318 return 1;
1319}
1320
1da177e4
LT
1321int ipmi_register_for_cmd(ipmi_user_t user,
1322 unsigned char netfn,
c69c3127
CM
1323 unsigned char cmd,
1324 unsigned int chans)
1da177e4 1325{
393d2cc3
CM
1326 ipmi_smi_t intf = user->intf;
1327 struct cmd_rcvr *rcvr;
393d2cc3 1328 int rv = 0;
1da177e4
LT
1329
1330
1331 rcvr = kmalloc(sizeof(*rcvr), GFP_KERNEL);
8a3628d5 1332 if (!rcvr)
1da177e4 1333 return -ENOMEM;
393d2cc3
CM
1334 rcvr->cmd = cmd;
1335 rcvr->netfn = netfn;
c69c3127 1336 rcvr->chans = chans;
393d2cc3 1337 rcvr->user = user;
1da177e4 1338
d6dfd131 1339 mutex_lock(&intf->cmd_rcvrs_mutex);
1da177e4 1340 /* Make sure the command/netfn is not already registered. */
c69c3127 1341 if (!is_cmd_rcvr_exclusive(intf, netfn, cmd, chans)) {
393d2cc3
CM
1342 rv = -EBUSY;
1343 goto out_unlock;
1da177e4 1344 }
877197ef 1345
89986496
CM
1346 if (atomic_inc_return(&intf->event_waiters) == 1)
1347 need_waiter(intf);
1348
393d2cc3 1349 list_add_rcu(&rcvr->link, &intf->cmd_rcvrs);
1da177e4 1350
393d2cc3 1351 out_unlock:
d6dfd131 1352 mutex_unlock(&intf->cmd_rcvrs_mutex);
1da177e4
LT
1353 if (rv)
1354 kfree(rcvr);
1355
1356 return rv;
1357}
c70d7499 1358EXPORT_SYMBOL(ipmi_register_for_cmd);
1da177e4
LT
1359
1360int ipmi_unregister_for_cmd(ipmi_user_t user,
1361 unsigned char netfn,
c69c3127
CM
1362 unsigned char cmd,
1363 unsigned int chans)
1da177e4 1364{
393d2cc3
CM
1365 ipmi_smi_t intf = user->intf;
1366 struct cmd_rcvr *rcvr;
c69c3127
CM
1367 struct cmd_rcvr *rcvrs = NULL;
1368 int i, rv = -ENOENT;
1da177e4 1369
d6dfd131 1370 mutex_lock(&intf->cmd_rcvrs_mutex);
c69c3127
CM
1371 for (i = 0; i < IPMI_NUM_CHANNELS; i++) {
1372 if (((1 << i) & chans) == 0)
1373 continue;
1374 rcvr = find_cmd_rcvr(intf, netfn, cmd, i);
1375 if (rcvr == NULL)
1376 continue;
1377 if (rcvr->user == user) {
1378 rv = 0;
1379 rcvr->chans &= ~chans;
1380 if (rcvr->chans == 0) {
1381 list_del_rcu(&rcvr->link);
1382 rcvr->next = rcvrs;
1383 rcvrs = rcvr;
1384 }
1385 }
1386 }
1387 mutex_unlock(&intf->cmd_rcvrs_mutex);
1388 synchronize_rcu();
1389 while (rcvrs) {
89986496 1390 atomic_dec(&intf->event_waiters);
c69c3127
CM
1391 rcvr = rcvrs;
1392 rcvrs = rcvr->next;
393d2cc3 1393 kfree(rcvr);
1da177e4 1394 }
c69c3127 1395 return rv;
1da177e4 1396}
c70d7499 1397EXPORT_SYMBOL(ipmi_unregister_for_cmd);
1da177e4 1398
1da177e4
LT
1399static unsigned char
1400ipmb_checksum(unsigned char *data, int size)
1401{
1402 unsigned char csum = 0;
c70d7499 1403
1da177e4
LT
1404 for (; size > 0; size--, data++)
1405 csum += *data;
1406
1407 return -csum;
1408}
1409
1410static inline void format_ipmb_msg(struct ipmi_smi_msg *smi_msg,
1411 struct kernel_ipmi_msg *msg,
1412 struct ipmi_ipmb_addr *ipmb_addr,
1413 long msgid,
1414 unsigned char ipmb_seq,
1415 int broadcast,
1416 unsigned char source_address,
1417 unsigned char source_lun)
1418{
1419 int i = broadcast;
1420
1421 /* Format the IPMB header data. */
1422 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1423 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1424 smi_msg->data[2] = ipmb_addr->channel;
1425 if (broadcast)
1426 smi_msg->data[3] = 0;
1427 smi_msg->data[i+3] = ipmb_addr->slave_addr;
1428 smi_msg->data[i+4] = (msg->netfn << 2) | (ipmb_addr->lun & 0x3);
1429 smi_msg->data[i+5] = ipmb_checksum(&(smi_msg->data[i+3]), 2);
1430 smi_msg->data[i+6] = source_address;
1431 smi_msg->data[i+7] = (ipmb_seq << 2) | source_lun;
1432 smi_msg->data[i+8] = msg->cmd;
1433
1434 /* Now tack on the data to the message. */
1435 if (msg->data_len > 0)
1436 memcpy(&(smi_msg->data[i+9]), msg->data,
1437 msg->data_len);
1438 smi_msg->data_size = msg->data_len + 9;
1439
1440 /* Now calculate the checksum and tack it on. */
1441 smi_msg->data[i+smi_msg->data_size]
1442 = ipmb_checksum(&(smi_msg->data[i+6]),
1443 smi_msg->data_size-6);
1444
c70d7499
CM
1445 /*
1446 * Add on the checksum size and the offset from the
1447 * broadcast.
1448 */
1da177e4
LT
1449 smi_msg->data_size += 1 + i;
1450
1451 smi_msg->msgid = msgid;
1452}
1453
1454static inline void format_lan_msg(struct ipmi_smi_msg *smi_msg,
1455 struct kernel_ipmi_msg *msg,
1456 struct ipmi_lan_addr *lan_addr,
1457 long msgid,
1458 unsigned char ipmb_seq,
1459 unsigned char source_lun)
1460{
1461 /* Format the IPMB header data. */
1462 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1463 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1464 smi_msg->data[2] = lan_addr->channel;
1465 smi_msg->data[3] = lan_addr->session_handle;
1466 smi_msg->data[4] = lan_addr->remote_SWID;
1467 smi_msg->data[5] = (msg->netfn << 2) | (lan_addr->lun & 0x3);
1468 smi_msg->data[6] = ipmb_checksum(&(smi_msg->data[4]), 2);
1469 smi_msg->data[7] = lan_addr->local_SWID;
1470 smi_msg->data[8] = (ipmb_seq << 2) | source_lun;
1471 smi_msg->data[9] = msg->cmd;
1472
1473 /* Now tack on the data to the message. */
1474 if (msg->data_len > 0)
1475 memcpy(&(smi_msg->data[10]), msg->data,
1476 msg->data_len);
1477 smi_msg->data_size = msg->data_len + 10;
1478
1479 /* Now calculate the checksum and tack it on. */
1480 smi_msg->data[smi_msg->data_size]
1481 = ipmb_checksum(&(smi_msg->data[7]),
1482 smi_msg->data_size-7);
1483
c70d7499
CM
1484 /*
1485 * Add on the checksum size and the offset from the
1486 * broadcast.
1487 */
1da177e4
LT
1488 smi_msg->data_size += 1;
1489
1490 smi_msg->msgid = msgid;
1491}
1492
191cc414
AB
1493static struct ipmi_smi_msg *smi_add_send_msg(ipmi_smi_t intf,
1494 struct ipmi_smi_msg *smi_msg,
1495 int priority)
7f4a1c84 1496{
7ea0ed2b
CM
1497 if (intf->curr_msg) {
1498 if (priority > 0)
1499 list_add_tail(&smi_msg->link, &intf->hp_xmit_msgs);
1500 else
1501 list_add_tail(&smi_msg->link, &intf->xmit_msgs);
1502 smi_msg = NULL;
1503 } else {
1504 intf->curr_msg = smi_msg;
1505 }
191cc414
AB
1506
1507 return smi_msg;
1508}
1509
1510
81d02b7f 1511static void smi_send(ipmi_smi_t intf, const struct ipmi_smi_handlers *handlers,
191cc414
AB
1512 struct ipmi_smi_msg *smi_msg, int priority)
1513{
1514 int run_to_completion = intf->run_to_completion;
1515
1516 if (run_to_completion) {
1517 smi_msg = smi_add_send_msg(intf, smi_msg, priority);
1518 } else {
1519 unsigned long flags;
1520
1521 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
1522 smi_msg = smi_add_send_msg(intf, smi_msg, priority);
7ea0ed2b 1523 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
191cc414 1524 }
7ea0ed2b
CM
1525
1526 if (smi_msg)
99ab32f3 1527 handlers->sender(intf->send_info, smi_msg);
7f4a1c84
CM
1528}
1529
c70d7499
CM
1530/*
1531 * Separate from ipmi_request so that the user does not have to be
1532 * supplied in certain circumstances (mainly at panic time). If
1533 * messages are supplied, they will be freed, even if an error
1534 * occurs.
1535 */
393d2cc3
CM
1536static int i_ipmi_request(ipmi_user_t user,
1537 ipmi_smi_t intf,
1538 struct ipmi_addr *addr,
1539 long msgid,
1540 struct kernel_ipmi_msg *msg,
1541 void *user_msg_data,
1542 void *supplied_smi,
1543 struct ipmi_recv_msg *supplied_recv,
1544 int priority,
1545 unsigned char source_address,
1546 unsigned char source_lun,
1547 int retries,
1548 unsigned int retry_time_ms)
1da177e4 1549{
b2c03941
CM
1550 int rv = 0;
1551 struct ipmi_smi_msg *smi_msg;
1552 struct ipmi_recv_msg *recv_msg;
1553 unsigned long flags;
1da177e4
LT
1554
1555
c70d7499 1556 if (supplied_recv)
1da177e4 1557 recv_msg = supplied_recv;
c70d7499 1558 else {
1da177e4 1559 recv_msg = ipmi_alloc_recv_msg();
c70d7499 1560 if (recv_msg == NULL)
1da177e4 1561 return -ENOMEM;
1da177e4
LT
1562 }
1563 recv_msg->user_msg_data = user_msg_data;
1564
c70d7499 1565 if (supplied_smi)
1da177e4 1566 smi_msg = (struct ipmi_smi_msg *) supplied_smi;
c70d7499 1567 else {
1da177e4
LT
1568 smi_msg = ipmi_alloc_smi_msg();
1569 if (smi_msg == NULL) {
1570 ipmi_free_recv_msg(recv_msg);
1571 return -ENOMEM;
1572 }
1573 }
1574
b2c03941 1575 rcu_read_lock();
7ea0ed2b 1576 if (intf->in_shutdown) {
b2c03941
CM
1577 rv = -ENODEV;
1578 goto out_err;
1579 }
1580
1da177e4 1581 recv_msg->user = user;
393d2cc3
CM
1582 if (user)
1583 kref_get(&user->refcount);
1da177e4 1584 recv_msg->msgid = msgid;
c70d7499
CM
1585 /*
1586 * Store the message to send in the receive message so timeout
1587 * responses can get the proper response data.
1588 */
1da177e4
LT
1589 recv_msg->msg = *msg;
1590
1591 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
1592 struct ipmi_system_interface_addr *smi_addr;
1593
1594 if (msg->netfn & 1) {
1595 /* Responses are not allowed to the SMI. */
1596 rv = -EINVAL;
1597 goto out_err;
1598 }
1599
1600 smi_addr = (struct ipmi_system_interface_addr *) addr;
1601 if (smi_addr->lun > 3) {
b2655f26 1602 ipmi_inc_stat(intf, sent_invalid_commands);
1da177e4
LT
1603 rv = -EINVAL;
1604 goto out_err;
1605 }
1606
1607 memcpy(&recv_msg->addr, smi_addr, sizeof(*smi_addr));
1608
1609 if ((msg->netfn == IPMI_NETFN_APP_REQUEST)
1610 && ((msg->cmd == IPMI_SEND_MSG_CMD)
1611 || (msg->cmd == IPMI_GET_MSG_CMD)
c70d7499
CM
1612 || (msg->cmd == IPMI_READ_EVENT_MSG_BUFFER_CMD))) {
1613 /*
1614 * We don't let the user do these, since we manage
1615 * the sequence numbers.
1616 */
b2655f26 1617 ipmi_inc_stat(intf, sent_invalid_commands);
1da177e4
LT
1618 rv = -EINVAL;
1619 goto out_err;
1620 }
1621
b9675136
CM
1622 if (((msg->netfn == IPMI_NETFN_APP_REQUEST)
1623 && ((msg->cmd == IPMI_COLD_RESET_CMD)
1624 || (msg->cmd == IPMI_WARM_RESET_CMD)))
c70d7499 1625 || (msg->netfn == IPMI_NETFN_FIRMWARE_REQUEST)) {
b9675136
CM
1626 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1627 intf->auto_maintenance_timeout
1628 = IPMI_MAINTENANCE_MODE_TIMEOUT;
1629 if (!intf->maintenance_mode
c70d7499 1630 && !intf->maintenance_mode_enable) {
7aefac26 1631 intf->maintenance_mode_enable = true;
b9675136
CM
1632 maintenance_mode_update(intf);
1633 }
1634 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
1635 flags);
1636 }
1637
1da177e4 1638 if ((msg->data_len + 2) > IPMI_MAX_MSG_LENGTH) {
b2655f26 1639 ipmi_inc_stat(intf, sent_invalid_commands);
1da177e4
LT
1640 rv = -EMSGSIZE;
1641 goto out_err;
1642 }
1643
1644 smi_msg->data[0] = (msg->netfn << 2) | (smi_addr->lun & 0x3);
1645 smi_msg->data[1] = msg->cmd;
1646 smi_msg->msgid = msgid;
1647 smi_msg->user_data = recv_msg;
1648 if (msg->data_len > 0)
1649 memcpy(&(smi_msg->data[2]), msg->data, msg->data_len);
1650 smi_msg->data_size = msg->data_len + 2;
b2655f26 1651 ipmi_inc_stat(intf, sent_local_commands);
25176ed6 1652 } else if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
1da177e4
LT
1653 struct ipmi_ipmb_addr *ipmb_addr;
1654 unsigned char ipmb_seq;
1655 long seqid;
1656 int broadcast = 0;
1657
9c101fd4 1658 if (addr->channel >= IPMI_MAX_CHANNELS) {
b2655f26 1659 ipmi_inc_stat(intf, sent_invalid_commands);
1da177e4
LT
1660 rv = -EINVAL;
1661 goto out_err;
1662 }
1663
1664 if (intf->channels[addr->channel].medium
c70d7499 1665 != IPMI_CHANNEL_MEDIUM_IPMB) {
b2655f26 1666 ipmi_inc_stat(intf, sent_invalid_commands);
1da177e4
LT
1667 rv = -EINVAL;
1668 goto out_err;
1669 }
1670
1671 if (retries < 0) {
1672 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE)
1673 retries = 0; /* Don't retry broadcasts. */
1674 else
1675 retries = 4;
1676 }
1677 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) {
c70d7499
CM
1678 /*
1679 * Broadcasts add a zero at the beginning of the
1680 * message, but otherwise is the same as an IPMB
1681 * address.
1682 */
1da177e4
LT
1683 addr->addr_type = IPMI_IPMB_ADDR_TYPE;
1684 broadcast = 1;
1685 }
1686
1687
1688 /* Default to 1 second retries. */
1689 if (retry_time_ms == 0)
1690 retry_time_ms = 1000;
1691
c70d7499
CM
1692 /*
1693 * 9 for the header and 1 for the checksum, plus
1694 * possibly one for the broadcast.
1695 */
1da177e4 1696 if ((msg->data_len + 10 + broadcast) > IPMI_MAX_MSG_LENGTH) {
b2655f26 1697 ipmi_inc_stat(intf, sent_invalid_commands);
1da177e4
LT
1698 rv = -EMSGSIZE;
1699 goto out_err;
1700 }
1701
1702 ipmb_addr = (struct ipmi_ipmb_addr *) addr;
1703 if (ipmb_addr->lun > 3) {
b2655f26 1704 ipmi_inc_stat(intf, sent_invalid_commands);
1da177e4
LT
1705 rv = -EINVAL;
1706 goto out_err;
1707 }
1708
1709 memcpy(&recv_msg->addr, ipmb_addr, sizeof(*ipmb_addr));
1710
1711 if (recv_msg->msg.netfn & 0x1) {
c70d7499
CM
1712 /*
1713 * It's a response, so use the user's sequence
1714 * from msgid.
1715 */
b2655f26 1716 ipmi_inc_stat(intf, sent_ipmb_responses);
1da177e4
LT
1717 format_ipmb_msg(smi_msg, msg, ipmb_addr, msgid,
1718 msgid, broadcast,
1719 source_address, source_lun);
1720
c70d7499
CM
1721 /*
1722 * Save the receive message so we can use it
1723 * to deliver the response.
1724 */
1da177e4
LT
1725 smi_msg->user_data = recv_msg;
1726 } else {
1727 /* It's a command, so get a sequence for it. */
1728
1729 spin_lock_irqsave(&(intf->seq_lock), flags);
1730
c70d7499
CM
1731 /*
1732 * Create a sequence number with a 1 second
1733 * timeout and 4 retries.
1734 */
1da177e4
LT
1735 rv = intf_next_seq(intf,
1736 recv_msg,
1737 retry_time_ms,
1738 retries,
1739 broadcast,
1740 &ipmb_seq,
1741 &seqid);
1742 if (rv) {
c70d7499
CM
1743 /*
1744 * We have used up all the sequence numbers,
1745 * probably, so abort.
1746 */
1da177e4
LT
1747 spin_unlock_irqrestore(&(intf->seq_lock),
1748 flags);
1749 goto out_err;
1750 }
1751
25176ed6
CM
1752 ipmi_inc_stat(intf, sent_ipmb_commands);
1753
c70d7499
CM
1754 /*
1755 * Store the sequence number in the message,
1756 * so that when the send message response
1757 * comes back we can start the timer.
1758 */
1da177e4
LT
1759 format_ipmb_msg(smi_msg, msg, ipmb_addr,
1760 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1761 ipmb_seq, broadcast,
1762 source_address, source_lun);
1763
c70d7499
CM
1764 /*
1765 * Copy the message into the recv message data, so we
1766 * can retransmit it later if necessary.
1767 */
1da177e4
LT
1768 memcpy(recv_msg->msg_data, smi_msg->data,
1769 smi_msg->data_size);
1770 recv_msg->msg.data = recv_msg->msg_data;
1771 recv_msg->msg.data_len = smi_msg->data_size;
1772
c70d7499
CM
1773 /*
1774 * We don't unlock until here, because we need
1775 * to copy the completed message into the
1776 * recv_msg before we release the lock.
1777 * Otherwise, race conditions may bite us. I
1778 * know that's pretty paranoid, but I prefer
1779 * to be correct.
1780 */
1da177e4
LT
1781 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1782 }
25176ed6 1783 } else if (is_lan_addr(addr)) {
1da177e4
LT
1784 struct ipmi_lan_addr *lan_addr;
1785 unsigned char ipmb_seq;
1786 long seqid;
1787
12fc1d7b 1788 if (addr->channel >= IPMI_MAX_CHANNELS) {
b2655f26 1789 ipmi_inc_stat(intf, sent_invalid_commands);
1da177e4
LT
1790 rv = -EINVAL;
1791 goto out_err;
1792 }
1793
1794 if ((intf->channels[addr->channel].medium
c70d7499 1795 != IPMI_CHANNEL_MEDIUM_8023LAN)
1da177e4 1796 && (intf->channels[addr->channel].medium
c70d7499 1797 != IPMI_CHANNEL_MEDIUM_ASYNC)) {
b2655f26 1798 ipmi_inc_stat(intf, sent_invalid_commands);
1da177e4
LT
1799 rv = -EINVAL;
1800 goto out_err;
1801 }
1802
1803 retries = 4;
1804
1805 /* Default to 1 second retries. */
1806 if (retry_time_ms == 0)
1807 retry_time_ms = 1000;
1808
1809 /* 11 for the header and 1 for the checksum. */
1810 if ((msg->data_len + 12) > IPMI_MAX_MSG_LENGTH) {
b2655f26 1811 ipmi_inc_stat(intf, sent_invalid_commands);
1da177e4
LT
1812 rv = -EMSGSIZE;
1813 goto out_err;
1814 }
1815
1816 lan_addr = (struct ipmi_lan_addr *) addr;
1817 if (lan_addr->lun > 3) {
b2655f26 1818 ipmi_inc_stat(intf, sent_invalid_commands);
1da177e4
LT
1819 rv = -EINVAL;
1820 goto out_err;
1821 }
1822
1823 memcpy(&recv_msg->addr, lan_addr, sizeof(*lan_addr));
1824
1825 if (recv_msg->msg.netfn & 0x1) {
c70d7499
CM
1826 /*
1827 * It's a response, so use the user's sequence
1828 * from msgid.
1829 */
b2655f26 1830 ipmi_inc_stat(intf, sent_lan_responses);
1da177e4
LT
1831 format_lan_msg(smi_msg, msg, lan_addr, msgid,
1832 msgid, source_lun);
1833
c70d7499
CM
1834 /*
1835 * Save the receive message so we can use it
1836 * to deliver the response.
1837 */
1da177e4
LT
1838 smi_msg->user_data = recv_msg;
1839 } else {
1840 /* It's a command, so get a sequence for it. */
1841
1842 spin_lock_irqsave(&(intf->seq_lock), flags);
1843
c70d7499
CM
1844 /*
1845 * Create a sequence number with a 1 second
1846 * timeout and 4 retries.
1847 */
1da177e4
LT
1848 rv = intf_next_seq(intf,
1849 recv_msg,
1850 retry_time_ms,
1851 retries,
1852 0,
1853 &ipmb_seq,
1854 &seqid);
1855 if (rv) {
c70d7499
CM
1856 /*
1857 * We have used up all the sequence numbers,
1858 * probably, so abort.
1859 */
1da177e4
LT
1860 spin_unlock_irqrestore(&(intf->seq_lock),
1861 flags);
1862 goto out_err;
1863 }
1864
25176ed6
CM
1865 ipmi_inc_stat(intf, sent_lan_commands);
1866
c70d7499
CM
1867 /*
1868 * Store the sequence number in the message,
1869 * so that when the send message response
1870 * comes back we can start the timer.
1871 */
1da177e4
LT
1872 format_lan_msg(smi_msg, msg, lan_addr,
1873 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1874 ipmb_seq, source_lun);
1875
c70d7499
CM
1876 /*
1877 * Copy the message into the recv message data, so we
1878 * can retransmit it later if necessary.
1879 */
1da177e4
LT
1880 memcpy(recv_msg->msg_data, smi_msg->data,
1881 smi_msg->data_size);
1882 recv_msg->msg.data = recv_msg->msg_data;
1883 recv_msg->msg.data_len = smi_msg->data_size;
1884
c70d7499
CM
1885 /*
1886 * We don't unlock until here, because we need
1887 * to copy the completed message into the
1888 * recv_msg before we release the lock.
1889 * Otherwise, race conditions may bite us. I
1890 * know that's pretty paranoid, but I prefer
1891 * to be correct.
1892 */
1da177e4
LT
1893 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1894 }
1895 } else {
1896 /* Unknown address type. */
b2655f26 1897 ipmi_inc_stat(intf, sent_invalid_commands);
1da177e4
LT
1898 rv = -EINVAL;
1899 goto out_err;
1900 }
1901
1902#ifdef DEBUG_MSGING
1903 {
1904 int m;
e8b33617 1905 for (m = 0; m < smi_msg->data_size; m++)
1da177e4
LT
1906 printk(" %2.2x", smi_msg->data[m]);
1907 printk("\n");
1908 }
1909#endif
b2c03941 1910
7ea0ed2b 1911 smi_send(intf, intf->handlers, smi_msg, priority);
b2c03941 1912 rcu_read_unlock();
1da177e4
LT
1913
1914 return 0;
1915
1916 out_err:
b2c03941 1917 rcu_read_unlock();
1da177e4
LT
1918 ipmi_free_smi_msg(smi_msg);
1919 ipmi_free_recv_msg(recv_msg);
1920 return rv;
1921}
1922
c14979b9
CM
1923static int check_addr(ipmi_smi_t intf,
1924 struct ipmi_addr *addr,
1925 unsigned char *saddr,
1926 unsigned char *lun)
1927{
1928 if (addr->channel >= IPMI_MAX_CHANNELS)
1929 return -EINVAL;
1930 *lun = intf->channels[addr->channel].lun;
1931 *saddr = intf->channels[addr->channel].address;
1932 return 0;
1933}
1934
1da177e4
LT
1935int ipmi_request_settime(ipmi_user_t user,
1936 struct ipmi_addr *addr,
1937 long msgid,
1938 struct kernel_ipmi_msg *msg,
1939 void *user_msg_data,
1940 int priority,
1941 int retries,
1942 unsigned int retry_time_ms)
1943{
f0ba9390 1944 unsigned char saddr = 0, lun = 0;
c14979b9
CM
1945 int rv;
1946
8a3628d5 1947 if (!user)
56a55ec6 1948 return -EINVAL;
c14979b9
CM
1949 rv = check_addr(user->intf, addr, &saddr, &lun);
1950 if (rv)
1951 return rv;
1da177e4
LT
1952 return i_ipmi_request(user,
1953 user->intf,
1954 addr,
1955 msgid,
1956 msg,
1957 user_msg_data,
1958 NULL, NULL,
1959 priority,
c14979b9
CM
1960 saddr,
1961 lun,
1da177e4
LT
1962 retries,
1963 retry_time_ms);
1964}
c70d7499 1965EXPORT_SYMBOL(ipmi_request_settime);
1da177e4
LT
1966
1967int ipmi_request_supply_msgs(ipmi_user_t user,
1968 struct ipmi_addr *addr,
1969 long msgid,
1970 struct kernel_ipmi_msg *msg,
1971 void *user_msg_data,
1972 void *supplied_smi,
1973 struct ipmi_recv_msg *supplied_recv,
1974 int priority)
1975{
9ebca93b 1976 unsigned char saddr = 0, lun = 0;
c14979b9
CM
1977 int rv;
1978
8a3628d5 1979 if (!user)
56a55ec6 1980 return -EINVAL;
c14979b9
CM
1981 rv = check_addr(user->intf, addr, &saddr, &lun);
1982 if (rv)
1983 return rv;
1da177e4
LT
1984 return i_ipmi_request(user,
1985 user->intf,
1986 addr,
1987 msgid,
1988 msg,
1989 user_msg_data,
1990 supplied_smi,
1991 supplied_recv,
1992 priority,
c14979b9
CM
1993 saddr,
1994 lun,
1da177e4
LT
1995 -1, 0);
1996}
c70d7499 1997EXPORT_SYMBOL(ipmi_request_supply_msgs);
1da177e4 1998
1aa16eea 1999#ifdef CONFIG_PROC_FS
07412736 2000static int smi_ipmb_proc_show(struct seq_file *m, void *v)
1da177e4 2001{
07412736 2002 ipmi_smi_t intf = m->private;
c14979b9 2003 int i;
1da177e4 2004
07412736
AD
2005 seq_printf(m, "%x", intf->channels[0].address);
2006 for (i = 1; i < IPMI_MAX_CHANNELS; i++)
2007 seq_printf(m, " %x", intf->channels[i].address);
d6c5dc18
JP
2008 seq_putc(m, '\n');
2009
5e33cd0c 2010 return 0;
1da177e4
LT
2011}
2012
07412736 2013static int smi_ipmb_proc_open(struct inode *inode, struct file *file)
1da177e4 2014{
d9dda78b 2015 return single_open(file, smi_ipmb_proc_show, PDE_DATA(inode));
07412736 2016}
1da177e4 2017
07412736
AD
2018static const struct file_operations smi_ipmb_proc_ops = {
2019 .open = smi_ipmb_proc_open,
2020 .read = seq_read,
2021 .llseek = seq_lseek,
2022 .release = single_release,
2023};
2024
2025static int smi_version_proc_show(struct seq_file *m, void *v)
2026{
2027 ipmi_smi_t intf = m->private;
2028
d6c5dc18
JP
2029 seq_printf(m, "%u.%u\n",
2030 ipmi_version_major(&intf->bmc->id),
2031 ipmi_version_minor(&intf->bmc->id));
2032
5e33cd0c 2033 return 0;
1da177e4
LT
2034}
2035
07412736 2036static int smi_version_proc_open(struct inode *inode, struct file *file)
1da177e4 2037{
d9dda78b 2038 return single_open(file, smi_version_proc_show, PDE_DATA(inode));
07412736
AD
2039}
2040
2041static const struct file_operations smi_version_proc_ops = {
2042 .open = smi_version_proc_open,
2043 .read = seq_read,
2044 .llseek = seq_lseek,
2045 .release = single_release,
2046};
1da177e4 2047
07412736
AD
2048static int smi_stats_proc_show(struct seq_file *m, void *v)
2049{
2050 ipmi_smi_t intf = m->private;
2051
2052 seq_printf(m, "sent_invalid_commands: %u\n",
b2655f26 2053 ipmi_get_stat(intf, sent_invalid_commands));
07412736 2054 seq_printf(m, "sent_local_commands: %u\n",
b2655f26 2055 ipmi_get_stat(intf, sent_local_commands));
07412736 2056 seq_printf(m, "handled_local_responses: %u\n",
b2655f26 2057 ipmi_get_stat(intf, handled_local_responses));
07412736 2058 seq_printf(m, "unhandled_local_responses: %u\n",
b2655f26 2059 ipmi_get_stat(intf, unhandled_local_responses));
07412736 2060 seq_printf(m, "sent_ipmb_commands: %u\n",
b2655f26 2061 ipmi_get_stat(intf, sent_ipmb_commands));
07412736 2062 seq_printf(m, "sent_ipmb_command_errs: %u\n",
b2655f26 2063 ipmi_get_stat(intf, sent_ipmb_command_errs));
07412736 2064 seq_printf(m, "retransmitted_ipmb_commands: %u\n",
b2655f26 2065 ipmi_get_stat(intf, retransmitted_ipmb_commands));
07412736 2066 seq_printf(m, "timed_out_ipmb_commands: %u\n",
b2655f26 2067 ipmi_get_stat(intf, timed_out_ipmb_commands));
07412736 2068 seq_printf(m, "timed_out_ipmb_broadcasts: %u\n",
b2655f26 2069 ipmi_get_stat(intf, timed_out_ipmb_broadcasts));
07412736 2070 seq_printf(m, "sent_ipmb_responses: %u\n",
b2655f26 2071 ipmi_get_stat(intf, sent_ipmb_responses));
07412736 2072 seq_printf(m, "handled_ipmb_responses: %u\n",
b2655f26 2073 ipmi_get_stat(intf, handled_ipmb_responses));
07412736 2074 seq_printf(m, "invalid_ipmb_responses: %u\n",
b2655f26 2075 ipmi_get_stat(intf, invalid_ipmb_responses));
07412736 2076 seq_printf(m, "unhandled_ipmb_responses: %u\n",
b2655f26 2077 ipmi_get_stat(intf, unhandled_ipmb_responses));
07412736 2078 seq_printf(m, "sent_lan_commands: %u\n",
b2655f26 2079 ipmi_get_stat(intf, sent_lan_commands));
07412736 2080 seq_printf(m, "sent_lan_command_errs: %u\n",
b2655f26 2081 ipmi_get_stat(intf, sent_lan_command_errs));
07412736 2082 seq_printf(m, "retransmitted_lan_commands: %u\n",
b2655f26 2083 ipmi_get_stat(intf, retransmitted_lan_commands));
07412736 2084 seq_printf(m, "timed_out_lan_commands: %u\n",
b2655f26 2085 ipmi_get_stat(intf, timed_out_lan_commands));
07412736 2086 seq_printf(m, "sent_lan_responses: %u\n",
b2655f26 2087 ipmi_get_stat(intf, sent_lan_responses));
07412736 2088 seq_printf(m, "handled_lan_responses: %u\n",
b2655f26 2089 ipmi_get_stat(intf, handled_lan_responses));
07412736 2090 seq_printf(m, "invalid_lan_responses: %u\n",
b2655f26 2091 ipmi_get_stat(intf, invalid_lan_responses));
07412736 2092 seq_printf(m, "unhandled_lan_responses: %u\n",
b2655f26 2093 ipmi_get_stat(intf, unhandled_lan_responses));
07412736 2094 seq_printf(m, "handled_commands: %u\n",
b2655f26 2095 ipmi_get_stat(intf, handled_commands));
07412736 2096 seq_printf(m, "invalid_commands: %u\n",
b2655f26 2097 ipmi_get_stat(intf, invalid_commands));
07412736 2098 seq_printf(m, "unhandled_commands: %u\n",
b2655f26 2099 ipmi_get_stat(intf, unhandled_commands));
07412736 2100 seq_printf(m, "invalid_events: %u\n",
b2655f26 2101 ipmi_get_stat(intf, invalid_events));
07412736 2102 seq_printf(m, "events: %u\n",
b2655f26 2103 ipmi_get_stat(intf, events));
07412736 2104 seq_printf(m, "failed rexmit LAN msgs: %u\n",
25176ed6 2105 ipmi_get_stat(intf, dropped_rexmit_lan_commands));
07412736 2106 seq_printf(m, "failed rexmit IPMB msgs: %u\n",
25176ed6 2107 ipmi_get_stat(intf, dropped_rexmit_ipmb_commands));
07412736
AD
2108 return 0;
2109}
1da177e4 2110
07412736
AD
2111static int smi_stats_proc_open(struct inode *inode, struct file *file)
2112{
d9dda78b 2113 return single_open(file, smi_stats_proc_show, PDE_DATA(inode));
1da177e4 2114}
07412736
AD
2115
2116static const struct file_operations smi_stats_proc_ops = {
2117 .open = smi_stats_proc_open,
2118 .read = seq_read,
2119 .llseek = seq_lseek,
2120 .release = single_release,
2121};
1aa16eea 2122#endif /* CONFIG_PROC_FS */
1da177e4
LT
2123
2124int ipmi_smi_add_proc_entry(ipmi_smi_t smi, char *name,
07412736 2125 const struct file_operations *proc_ops,
99b76233 2126 void *data)
1da177e4 2127{
1da177e4 2128 int rv = 0;
3b625943
CM
2129#ifdef CONFIG_PROC_FS
2130 struct proc_dir_entry *file;
1da177e4
LT
2131 struct ipmi_proc_entry *entry;
2132
2133 /* Create a list element. */
2134 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2135 if (!entry)
2136 return -ENOMEM;
1b6b698f 2137 entry->name = kstrdup(name, GFP_KERNEL);
1da177e4
LT
2138 if (!entry->name) {
2139 kfree(entry);
2140 return -ENOMEM;
2141 }
1da177e4 2142
07412736 2143 file = proc_create_data(name, 0, smi->proc_dir, proc_ops, data);
1da177e4
LT
2144 if (!file) {
2145 kfree(entry->name);
2146 kfree(entry);
2147 rv = -ENOMEM;
2148 } else {
ac019151 2149 mutex_lock(&smi->proc_entry_lock);
1da177e4
LT
2150 /* Stick it on the list. */
2151 entry->next = smi->proc_entries;
2152 smi->proc_entries = entry;
ac019151 2153 mutex_unlock(&smi->proc_entry_lock);
1da177e4 2154 }
3b625943 2155#endif /* CONFIG_PROC_FS */
1da177e4
LT
2156
2157 return rv;
2158}
c70d7499 2159EXPORT_SYMBOL(ipmi_smi_add_proc_entry);
1da177e4
LT
2160
2161static int add_proc_entries(ipmi_smi_t smi, int num)
2162{
2163 int rv = 0;
2164
3b625943 2165#ifdef CONFIG_PROC_FS
1da177e4
LT
2166 sprintf(smi->proc_dir_name, "%d", num);
2167 smi->proc_dir = proc_mkdir(smi->proc_dir_name, proc_ipmi_root);
2168 if (!smi->proc_dir)
2169 rv = -ENOMEM;
1da177e4
LT
2170
2171 if (rv == 0)
2172 rv = ipmi_smi_add_proc_entry(smi, "stats",
07412736 2173 &smi_stats_proc_ops,
99b76233 2174 smi);
1da177e4
LT
2175
2176 if (rv == 0)
2177 rv = ipmi_smi_add_proc_entry(smi, "ipmb",
07412736 2178 &smi_ipmb_proc_ops,
99b76233 2179 smi);
1da177e4
LT
2180
2181 if (rv == 0)
2182 rv = ipmi_smi_add_proc_entry(smi, "version",
07412736 2183 &smi_version_proc_ops,
99b76233 2184 smi);
3b625943 2185#endif /* CONFIG_PROC_FS */
1da177e4
LT
2186
2187 return rv;
2188}
2189
2190static void remove_proc_entries(ipmi_smi_t smi)
2191{
3b625943 2192#ifdef CONFIG_PROC_FS
1da177e4
LT
2193 struct ipmi_proc_entry *entry;
2194
ac019151 2195 mutex_lock(&smi->proc_entry_lock);
1da177e4
LT
2196 while (smi->proc_entries) {
2197 entry = smi->proc_entries;
2198 smi->proc_entries = entry->next;
2199
2200 remove_proc_entry(entry->name, smi->proc_dir);
2201 kfree(entry->name);
2202 kfree(entry);
2203 }
ac019151 2204 mutex_unlock(&smi->proc_entry_lock);
1da177e4 2205 remove_proc_entry(smi->proc_dir_name, proc_ipmi_root);
3b625943 2206#endif /* CONFIG_PROC_FS */
1da177e4
LT
2207}
2208
50c812b2
CM
2209static int __find_bmc_guid(struct device *dev, void *data)
2210{
2211 unsigned char *id = data;
16639eb0 2212 struct bmc_device *bmc = to_bmc_device(dev);
50c812b2
CM
2213 return memcmp(bmc->guid, id, 16) == 0;
2214}
2215
2216static struct bmc_device *ipmi_find_bmc_guid(struct device_driver *drv,
2217 unsigned char *guid)
2218{
2219 struct device *dev;
2220
2221 dev = driver_find_device(drv, NULL, guid, __find_bmc_guid);
2222 if (dev)
16639eb0 2223 return to_bmc_device(dev);
50c812b2
CM
2224 else
2225 return NULL;
2226}
2227
2228struct prod_dev_id {
2229 unsigned int product_id;
2230 unsigned char device_id;
2231};
2232
2233static int __find_bmc_prod_dev_id(struct device *dev, void *data)
2234{
2235 struct prod_dev_id *id = data;
16639eb0 2236 struct bmc_device *bmc = to_bmc_device(dev);
50c812b2
CM
2237
2238 return (bmc->id.product_id == id->product_id
50c812b2
CM
2239 && bmc->id.device_id == id->device_id);
2240}
2241
2242static struct bmc_device *ipmi_find_bmc_prod_dev_id(
2243 struct device_driver *drv,
f0b55da0 2244 unsigned int product_id, unsigned char device_id)
50c812b2
CM
2245{
2246 struct prod_dev_id id = {
2247 .product_id = product_id,
2248 .device_id = device_id,
2249 };
2250 struct device *dev;
2251
2252 dev = driver_find_device(drv, NULL, &id, __find_bmc_prod_dev_id);
2253 if (dev)
16639eb0 2254 return to_bmc_device(dev);
50c812b2
CM
2255 else
2256 return NULL;
2257}
2258
2259static ssize_t device_id_show(struct device *dev,
2260 struct device_attribute *attr,
2261 char *buf)
2262{
16639eb0 2263 struct bmc_device *bmc = to_bmc_device(dev);
50c812b2
CM
2264
2265 return snprintf(buf, 10, "%u\n", bmc->id.device_id);
2266}
9c633317 2267static DEVICE_ATTR(device_id, S_IRUGO, device_id_show, NULL);
50c812b2 2268
16639eb0
CM
2269static ssize_t provides_device_sdrs_show(struct device *dev,
2270 struct device_attribute *attr,
2271 char *buf)
50c812b2 2272{
16639eb0 2273 struct bmc_device *bmc = to_bmc_device(dev);
50c812b2
CM
2274
2275 return snprintf(buf, 10, "%u\n",
7947d2cc 2276 (bmc->id.device_revision & 0x80) >> 7);
50c812b2 2277}
9c633317
CM
2278static DEVICE_ATTR(provides_device_sdrs, S_IRUGO, provides_device_sdrs_show,
2279 NULL);
50c812b2
CM
2280
2281static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
2282 char *buf)
2283{
16639eb0 2284 struct bmc_device *bmc = to_bmc_device(dev);
50c812b2
CM
2285
2286 return snprintf(buf, 20, "%u\n",
7947d2cc 2287 bmc->id.device_revision & 0x0F);
50c812b2 2288}
9c633317 2289static DEVICE_ATTR(revision, S_IRUGO, revision_show, NULL);
50c812b2 2290
16639eb0
CM
2291static ssize_t firmware_revision_show(struct device *dev,
2292 struct device_attribute *attr,
2293 char *buf)
50c812b2 2294{
16639eb0 2295 struct bmc_device *bmc = to_bmc_device(dev);
50c812b2
CM
2296
2297 return snprintf(buf, 20, "%u.%x\n", bmc->id.firmware_revision_1,
2298 bmc->id.firmware_revision_2);
2299}
9c633317 2300static DEVICE_ATTR(firmware_revision, S_IRUGO, firmware_revision_show, NULL);
50c812b2
CM
2301
2302static ssize_t ipmi_version_show(struct device *dev,
2303 struct device_attribute *attr,
2304 char *buf)
2305{
16639eb0 2306 struct bmc_device *bmc = to_bmc_device(dev);
50c812b2
CM
2307
2308 return snprintf(buf, 20, "%u.%u\n",
2309 ipmi_version_major(&bmc->id),
2310 ipmi_version_minor(&bmc->id));
2311}
9c633317 2312static DEVICE_ATTR(ipmi_version, S_IRUGO, ipmi_version_show, NULL);
50c812b2
CM
2313
2314static ssize_t add_dev_support_show(struct device *dev,
2315 struct device_attribute *attr,
2316 char *buf)
2317{
16639eb0 2318 struct bmc_device *bmc = to_bmc_device(dev);
50c812b2
CM
2319
2320 return snprintf(buf, 10, "0x%02x\n",
2321 bmc->id.additional_device_support);
2322}
9c633317
CM
2323static DEVICE_ATTR(additional_device_support, S_IRUGO, add_dev_support_show,
2324 NULL);
50c812b2
CM
2325
2326static ssize_t manufacturer_id_show(struct device *dev,
2327 struct device_attribute *attr,
2328 char *buf)
2329{
16639eb0 2330 struct bmc_device *bmc = to_bmc_device(dev);
50c812b2
CM
2331
2332 return snprintf(buf, 20, "0x%6.6x\n", bmc->id.manufacturer_id);
2333}
9c633317 2334static DEVICE_ATTR(manufacturer_id, S_IRUGO, manufacturer_id_show, NULL);
50c812b2
CM
2335
2336static ssize_t product_id_show(struct device *dev,
2337 struct device_attribute *attr,
2338 char *buf)
2339{
16639eb0 2340 struct bmc_device *bmc = to_bmc_device(dev);
50c812b2
CM
2341
2342 return snprintf(buf, 10, "0x%4.4x\n", bmc->id.product_id);
2343}
9c633317 2344static DEVICE_ATTR(product_id, S_IRUGO, product_id_show, NULL);
50c812b2
CM
2345
2346static ssize_t aux_firmware_rev_show(struct device *dev,
2347 struct device_attribute *attr,
2348 char *buf)
2349{
16639eb0 2350 struct bmc_device *bmc = to_bmc_device(dev);
50c812b2
CM
2351
2352 return snprintf(buf, 21, "0x%02x 0x%02x 0x%02x 0x%02x\n",
2353 bmc->id.aux_firmware_revision[3],
2354 bmc->id.aux_firmware_revision[2],
2355 bmc->id.aux_firmware_revision[1],
2356 bmc->id.aux_firmware_revision[0]);
2357}
9c633317 2358static DEVICE_ATTR(aux_firmware_revision, S_IRUGO, aux_firmware_rev_show, NULL);
50c812b2
CM
2359
2360static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
2361 char *buf)
2362{
16639eb0 2363 struct bmc_device *bmc = to_bmc_device(dev);
50c812b2
CM
2364
2365 return snprintf(buf, 100, "%Lx%Lx\n",
2366 (long long) bmc->guid[0],
2367 (long long) bmc->guid[8]);
2368}
9c633317 2369static DEVICE_ATTR(guid, S_IRUGO, guid_show, NULL);
16639eb0
CM
2370
2371static struct attribute *bmc_dev_attrs[] = {
2372 &dev_attr_device_id.attr,
2373 &dev_attr_provides_device_sdrs.attr,
2374 &dev_attr_revision.attr,
2375 &dev_attr_firmware_revision.attr,
2376 &dev_attr_ipmi_version.attr,
2377 &dev_attr_additional_device_support.attr,
2378 &dev_attr_manufacturer_id.attr,
2379 &dev_attr_product_id.attr,
2d06a0c9
TI
2380 &dev_attr_aux_firmware_revision.attr,
2381 &dev_attr_guid.attr,
16639eb0
CM
2382 NULL
2383};
50c812b2 2384
2d06a0c9
TI
2385static umode_t bmc_dev_attr_is_visible(struct kobject *kobj,
2386 struct attribute *attr, int idx)
2387{
2388 struct device *dev = kobj_to_dev(kobj);
2389 struct bmc_device *bmc = to_bmc_device(dev);
2390 umode_t mode = attr->mode;
2391
2392 if (attr == &dev_attr_aux_firmware_revision.attr)
2393 return bmc->id.aux_firmware_revision_set ? mode : 0;
2394 if (attr == &dev_attr_guid.attr)
2395 return bmc->guid_set ? mode : 0;
2396 return mode;
2397}
2398
16639eb0
CM
2399static struct attribute_group bmc_dev_attr_group = {
2400 .attrs = bmc_dev_attrs,
2d06a0c9 2401 .is_visible = bmc_dev_attr_is_visible,
16639eb0 2402};
5e59393e 2403
16639eb0
CM
2404static const struct attribute_group *bmc_dev_attr_groups[] = {
2405 &bmc_dev_attr_group,
2406 NULL
2407};
2408
2409static struct device_type bmc_device_type = {
2410 .groups = bmc_dev_attr_groups,
2411};
2412
2413static void
2414release_bmc_device(struct device *dev)
2415{
2416 kfree(to_bmc_device(dev));
5e59393e
JG
2417}
2418
2419static void
2420cleanup_bmc_device(struct kref *ref)
2421{
16639eb0 2422 struct bmc_device *bmc = container_of(ref, struct bmc_device, usecount);
5e59393e 2423
16639eb0 2424 platform_device_unregister(&bmc->pdev);
50c812b2
CM
2425}
2426
2427static void ipmi_bmc_unregister(ipmi_smi_t intf)
2428{
2429 struct bmc_device *bmc = intf->bmc;
2430
5a0e10ec 2431 sysfs_remove_link(&intf->si_dev->kobj, "bmc");
50c812b2 2432 if (intf->my_dev_name) {
16639eb0 2433 sysfs_remove_link(&bmc->pdev.dev.kobj, intf->my_dev_name);
50c812b2
CM
2434 kfree(intf->my_dev_name);
2435 intf->my_dev_name = NULL;
2436 }
2437
2438 mutex_lock(&ipmidriver_mutex);
16639eb0 2439 kref_put(&bmc->usecount, cleanup_bmc_device);
f0b55da0 2440 intf->bmc = NULL;
50c812b2
CM
2441 mutex_unlock(&ipmidriver_mutex);
2442}
2443
5a0e10ec 2444static int ipmi_bmc_register(ipmi_smi_t intf, int ifnum)
50c812b2
CM
2445{
2446 int rv;
2447 struct bmc_device *bmc = intf->bmc;
2448 struct bmc_device *old_bmc;
50c812b2
CM
2449
2450 mutex_lock(&ipmidriver_mutex);
2451
2452 /*
2453 * Try to find if there is an bmc_device struct
2454 * representing the interfaced BMC already
2455 */
2456 if (bmc->guid_set)
fe2d5ffc 2457 old_bmc = ipmi_find_bmc_guid(&ipmidriver.driver, bmc->guid);
50c812b2 2458 else
fe2d5ffc 2459 old_bmc = ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
50c812b2
CM
2460 bmc->id.product_id,
2461 bmc->id.device_id);
2462
2463 /*
2464 * If there is already an bmc_device, free the new one,
2465 * otherwise register the new BMC device
2466 */
2467 if (old_bmc) {
2468 kfree(bmc);
2469 intf->bmc = old_bmc;
2470 bmc = old_bmc;
2471
16639eb0 2472 kref_get(&bmc->usecount);
50c812b2
CM
2473 mutex_unlock(&ipmidriver_mutex);
2474
2475 printk(KERN_INFO
2476 "ipmi: interfacing existing BMC (man_id: 0x%6.6x,"
2477 " prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2478 bmc->id.manufacturer_id,
2479 bmc->id.product_id,
2480 bmc->id.device_id);
2481 } else {
f0b55da0
CM
2482 unsigned char orig_dev_id = bmc->id.device_id;
2483 int warn_printed = 0;
2484
16639eb0 2485 snprintf(bmc->name, sizeof(bmc->name),
f0b55da0 2486 "ipmi_bmc.%4.4x", bmc->id.product_id);
16639eb0 2487 bmc->pdev.name = bmc->name;
f0b55da0 2488
fe2d5ffc 2489 while (ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
f0b55da0 2490 bmc->id.product_id,
1d5636cc 2491 bmc->id.device_id)) {
f0b55da0
CM
2492 if (!warn_printed) {
2493 printk(KERN_WARNING PFX
2494 "This machine has two different BMCs"
2495 " with the same product id and device"
2496 " id. This is an error in the"
2497 " firmware, but incrementing the"
2498 " device id to work around the problem."
2499 " Prod ID = 0x%x, Dev ID = 0x%x\n",
2500 bmc->id.product_id, bmc->id.device_id);
2501 warn_printed = 1;
2502 }
2503 bmc->id.device_id++; /* Wraps at 255 */
2504 if (bmc->id.device_id == orig_dev_id) {
2505 printk(KERN_ERR PFX
2506 "Out of device ids!\n");
2507 break;
2508 }
2509 }
2510
16639eb0
CM
2511 bmc->pdev.dev.driver = &ipmidriver.driver;
2512 bmc->pdev.id = bmc->id.device_id;
2513 bmc->pdev.dev.release = release_bmc_device;
2514 bmc->pdev.dev.type = &bmc_device_type;
5a0e10ec 2515 kref_init(&bmc->usecount);
50c812b2 2516
16639eb0 2517 rv = platform_device_register(&bmc->pdev);
50c812b2
CM
2518 mutex_unlock(&ipmidriver_mutex);
2519 if (rv) {
16639eb0 2520 put_device(&bmc->pdev.dev);
50c812b2
CM
2521 printk(KERN_ERR
2522 "ipmi_msghandler:"
2523 " Unable to register bmc device: %d\n",
2524 rv);
c70d7499
CM
2525 /*
2526 * Don't go to out_err, you can only do that if
2527 * the device is registered already.
2528 */
50c812b2
CM
2529 return rv;
2530 }
2531
279fbd0c
MS
2532 dev_info(intf->si_dev, "Found new BMC (man_id: 0x%6.6x, "
2533 "prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2534 bmc->id.manufacturer_id,
2535 bmc->id.product_id,
2536 bmc->id.device_id);
50c812b2
CM
2537 }
2538
2539 /*
2540 * create symlink from system interface device to bmc device
2541 * and back.
2542 */
5a0e10ec 2543 rv = sysfs_create_link(&intf->si_dev->kobj, &bmc->pdev.dev.kobj, "bmc");
50c812b2
CM
2544 if (rv) {
2545 printk(KERN_ERR
2546 "ipmi_msghandler: Unable to create bmc symlink: %d\n",
2547 rv);
2548 goto out_err;
2549 }
2550
16639eb0 2551 intf->my_dev_name = kasprintf(GFP_KERNEL, "ipmi%d", ifnum);
50c812b2
CM
2552 if (!intf->my_dev_name) {
2553 rv = -ENOMEM;
2554 printk(KERN_ERR
2555 "ipmi_msghandler: allocate link from BMC: %d\n",
2556 rv);
2557 goto out_err;
2558 }
50c812b2 2559
16639eb0 2560 rv = sysfs_create_link(&bmc->pdev.dev.kobj, &intf->si_dev->kobj,
50c812b2
CM
2561 intf->my_dev_name);
2562 if (rv) {
2563 kfree(intf->my_dev_name);
2564 intf->my_dev_name = NULL;
2565 printk(KERN_ERR
2566 "ipmi_msghandler:"
2567 " Unable to create symlink to bmc: %d\n",
2568 rv);
2569 goto out_err;
2570 }
2571
2572 return 0;
2573
2574out_err:
2575 ipmi_bmc_unregister(intf);
2576 return rv;
2577}
2578
2579static int
2580send_guid_cmd(ipmi_smi_t intf, int chan)
2581{
2582 struct kernel_ipmi_msg msg;
2583 struct ipmi_system_interface_addr si;
2584
2585 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2586 si.channel = IPMI_BMC_CHANNEL;
2587 si.lun = 0;
2588
2589 msg.netfn = IPMI_NETFN_APP_REQUEST;
2590 msg.cmd = IPMI_GET_DEVICE_GUID_CMD;
2591 msg.data = NULL;
2592 msg.data_len = 0;
2593 return i_ipmi_request(NULL,
2594 intf,
2595 (struct ipmi_addr *) &si,
2596 0,
2597 &msg,
2598 intf,
2599 NULL,
2600 NULL,
2601 0,
2602 intf->channels[0].address,
2603 intf->channels[0].lun,
2604 -1, 0);
2605}
2606
2607static void
2608guid_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
2609{
2610 if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2611 || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
2612 || (msg->msg.cmd != IPMI_GET_DEVICE_GUID_CMD))
2613 /* Not for me */
2614 return;
2615
2616 if (msg->msg.data[0] != 0) {
2617 /* Error from getting the GUID, the BMC doesn't have one. */
2618 intf->bmc->guid_set = 0;
2619 goto out;
2620 }
2621
2622 if (msg->msg.data_len < 17) {
2623 intf->bmc->guid_set = 0;
2624 printk(KERN_WARNING PFX
2625 "guid_handler: The GUID response from the BMC was too"
2626 " short, it was %d but should have been 17. Assuming"
2627 " GUID is not available.\n",
2628 msg->msg.data_len);
2629 goto out;
2630 }
2631
2632 memcpy(intf->bmc->guid, msg->msg.data, 16);
2633 intf->bmc->guid_set = 1;
2634 out:
2635 wake_up(&intf->waitq);
2636}
2637
2638static void
2639get_guid(ipmi_smi_t intf)
2640{
2641 int rv;
2642
2643 intf->bmc->guid_set = 0x2;
2644 intf->null_user_handler = guid_handler;
2645 rv = send_guid_cmd(intf, 0);
2646 if (rv)
2647 /* Send failed, no GUID available. */
2648 intf->bmc->guid_set = 0;
2649 wait_event(intf->waitq, intf->bmc->guid_set != 2);
2650 intf->null_user_handler = NULL;
2651}
2652
1da177e4
LT
2653static int
2654send_channel_info_cmd(ipmi_smi_t intf, int chan)
2655{
2656 struct kernel_ipmi_msg msg;
2657 unsigned char data[1];
2658 struct ipmi_system_interface_addr si;
2659
2660 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2661 si.channel = IPMI_BMC_CHANNEL;
2662 si.lun = 0;
2663
2664 msg.netfn = IPMI_NETFN_APP_REQUEST;
2665 msg.cmd = IPMI_GET_CHANNEL_INFO_CMD;
2666 msg.data = data;
2667 msg.data_len = 1;
2668 data[0] = chan;
2669 return i_ipmi_request(NULL,
2670 intf,
2671 (struct ipmi_addr *) &si,
2672 0,
2673 &msg,
56a55ec6 2674 intf,
1da177e4
LT
2675 NULL,
2676 NULL,
2677 0,
c14979b9
CM
2678 intf->channels[0].address,
2679 intf->channels[0].lun,
1da177e4
LT
2680 -1, 0);
2681}
2682
2683static void
56a55ec6 2684channel_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
1da177e4
LT
2685{
2686 int rv = 0;
2687 int chan;
2688
56a55ec6
CM
2689 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2690 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
c70d7499 2691 && (msg->msg.cmd == IPMI_GET_CHANNEL_INFO_CMD)) {
1da177e4 2692 /* It's the one we want */
56a55ec6 2693 if (msg->msg.data[0] != 0) {
1da177e4
LT
2694 /* Got an error from the channel, just go on. */
2695
56a55ec6 2696 if (msg->msg.data[0] == IPMI_INVALID_COMMAND_ERR) {
c70d7499
CM
2697 /*
2698 * If the MC does not support this
2699 * command, that is legal. We just
2700 * assume it has one IPMB at channel
2701 * zero.
2702 */
1da177e4
LT
2703 intf->channels[0].medium
2704 = IPMI_CHANNEL_MEDIUM_IPMB;
2705 intf->channels[0].protocol
2706 = IPMI_CHANNEL_PROTOCOL_IPMB;
1da177e4
LT
2707
2708 intf->curr_channel = IPMI_MAX_CHANNELS;
2709 wake_up(&intf->waitq);
2710 goto out;
2711 }
2712 goto next_channel;
2713 }
56a55ec6 2714 if (msg->msg.data_len < 4) {
1da177e4
LT
2715 /* Message not big enough, just go on. */
2716 goto next_channel;
2717 }
2718 chan = intf->curr_channel;
56a55ec6
CM
2719 intf->channels[chan].medium = msg->msg.data[2] & 0x7f;
2720 intf->channels[chan].protocol = msg->msg.data[3] & 0x1f;
1da177e4 2721
c70d7499 2722 next_channel:
1da177e4
LT
2723 intf->curr_channel++;
2724 if (intf->curr_channel >= IPMI_MAX_CHANNELS)
2725 wake_up(&intf->waitq);
2726 else
2727 rv = send_channel_info_cmd(intf, intf->curr_channel);
2728
2729 if (rv) {
2730 /* Got an error somehow, just give up. */
1f668423
CM
2731 printk(KERN_WARNING PFX
2732 "Error sending channel information for channel"
2733 " %d: %d\n", intf->curr_channel, rv);
2734
1da177e4
LT
2735 intf->curr_channel = IPMI_MAX_CHANNELS;
2736 wake_up(&intf->waitq);
1da177e4
LT
2737 }
2738 }
2739 out:
2740 return;
2741}
2742
895dcfd1 2743static void ipmi_poll(ipmi_smi_t intf)
fcfa4724 2744{
fcfa4724
CM
2745 if (intf->handlers->poll)
2746 intf->handlers->poll(intf->send_info);
7adf579c
CM
2747 /* In case something came in */
2748 handle_new_recv_msgs(intf);
fcfa4724 2749}
895dcfd1
CM
2750
2751void ipmi_poll_interface(ipmi_user_t user)
2752{
2753 ipmi_poll(user->intf);
fcfa4724 2754}
c70d7499 2755EXPORT_SYMBOL(ipmi_poll_interface);
fcfa4724 2756
81d02b7f 2757int ipmi_register_smi(const struct ipmi_smi_handlers *handlers,
1da177e4 2758 void *send_info,
50c812b2
CM
2759 struct ipmi_device_id *device_id,
2760 struct device *si_dev,
453823ba 2761 unsigned char slave_addr)
1da177e4
LT
2762{
2763 int i, j;
2764 int rv;
393d2cc3 2765 ipmi_smi_t intf;
bca0324d 2766 ipmi_smi_t tintf;
bca0324d 2767 struct list_head *link;
1da177e4 2768
c70d7499
CM
2769 /*
2770 * Make sure the driver is actually initialized, this handles
2771 * problems with initialization order.
2772 */
1da177e4
LT
2773 if (!initialized) {
2774 rv = ipmi_init_msghandler();
2775 if (rv)
2776 return rv;
c70d7499
CM
2777 /*
2778 * The init code doesn't return an error if it was turned
2779 * off, but it won't initialize. Check that.
2780 */
1da177e4
LT
2781 if (!initialized)
2782 return -ENODEV;
2783 }
2784
dd00cc48 2785 intf = kzalloc(sizeof(*intf), GFP_KERNEL);
393d2cc3 2786 if (!intf)
1da177e4 2787 return -ENOMEM;
b2c03941
CM
2788
2789 intf->ipmi_version_major = ipmi_version_major(device_id);
2790 intf->ipmi_version_minor = ipmi_version_minor(device_id);
2791
50c812b2
CM
2792 intf->bmc = kzalloc(sizeof(*intf->bmc), GFP_KERNEL);
2793 if (!intf->bmc) {
2794 kfree(intf);
2795 return -ENOMEM;
2796 }
bca0324d 2797 intf->intf_num = -1; /* Mark it invalid for now. */
393d2cc3 2798 kref_init(&intf->refcount);
50c812b2
CM
2799 intf->bmc->id = *device_id;
2800 intf->si_dev = si_dev;
393d2cc3
CM
2801 for (j = 0; j < IPMI_MAX_CHANNELS; j++) {
2802 intf->channels[j].address = IPMI_BMC_SLAVE_ADDR;
2803 intf->channels[j].lun = 2;
2804 }
2805 if (slave_addr != 0)
2806 intf->channels[0].address = slave_addr;
2807 INIT_LIST_HEAD(&intf->users);
2808 intf->handlers = handlers;
2809 intf->send_info = send_info;
2810 spin_lock_init(&intf->seq_lock);
2811 for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++) {
2812 intf->seq_table[j].inuse = 0;
2813 intf->seq_table[j].seqid = 0;
2814 }
2815 intf->curr_seq = 0;
2816#ifdef CONFIG_PROC_FS
ac019151 2817 mutex_init(&intf->proc_entry_lock);
393d2cc3 2818#endif
65be7544
CM
2819 spin_lock_init(&intf->waiting_rcv_msgs_lock);
2820 INIT_LIST_HEAD(&intf->waiting_rcv_msgs);
7adf579c
CM
2821 tasklet_init(&intf->recv_tasklet,
2822 smi_recv_tasklet,
2823 (unsigned long) intf);
2824 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 0);
7ea0ed2b
CM
2825 spin_lock_init(&intf->xmit_msgs_lock);
2826 INIT_LIST_HEAD(&intf->xmit_msgs);
2827 INIT_LIST_HEAD(&intf->hp_xmit_msgs);
393d2cc3 2828 spin_lock_init(&intf->events_lock);
89986496
CM
2829 atomic_set(&intf->event_waiters, 0);
2830 intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
393d2cc3
CM
2831 INIT_LIST_HEAD(&intf->waiting_events);
2832 intf->waiting_events_count = 0;
d6dfd131 2833 mutex_init(&intf->cmd_rcvrs_mutex);
b9675136 2834 spin_lock_init(&intf->maintenance_mode_lock);
393d2cc3
CM
2835 INIT_LIST_HEAD(&intf->cmd_rcvrs);
2836 init_waitqueue_head(&intf->waitq);
b2655f26
KB
2837 for (i = 0; i < IPMI_NUM_STATS; i++)
2838 atomic_set(&intf->stats[i], 0);
393d2cc3 2839
393d2cc3 2840 intf->proc_dir = NULL;
1da177e4 2841
b2c03941 2842 mutex_lock(&smi_watchers_mutex);
bca0324d
CM
2843 mutex_lock(&ipmi_interfaces_mutex);
2844 /* Look for a hole in the numbers. */
2845 i = 0;
2846 link = &ipmi_interfaces;
2847 list_for_each_entry_rcu(tintf, &ipmi_interfaces, link) {
2848 if (tintf->intf_num != i) {
2849 link = &tintf->link;
1da177e4
LT
2850 break;
2851 }
bca0324d 2852 i++;
1da177e4 2853 }
bca0324d
CM
2854 /* Add the new interface in numeric order. */
2855 if (i == 0)
2856 list_add_rcu(&intf->link, &ipmi_interfaces);
2857 else
2858 list_add_tail_rcu(&intf->link, link);
1da177e4 2859
453823ba
CM
2860 rv = handlers->start_processing(send_info, intf);
2861 if (rv)
2862 goto out;
1da177e4 2863
50c812b2
CM
2864 get_guid(intf);
2865
b2c03941 2866 if ((intf->ipmi_version_major > 1)
c70d7499
CM
2867 || ((intf->ipmi_version_major == 1)
2868 && (intf->ipmi_version_minor >= 5))) {
2869 /*
2870 * Start scanning the channels to see what is
2871 * available.
2872 */
393d2cc3
CM
2873 intf->null_user_handler = channel_handler;
2874 intf->curr_channel = 0;
2875 rv = send_channel_info_cmd(intf, 0);
1f668423
CM
2876 if (rv) {
2877 printk(KERN_WARNING PFX
2878 "Error sending channel information for channel"
2879 " 0, %d\n", rv);
393d2cc3 2880 goto out;
1f668423 2881 }
1da177e4 2882
393d2cc3
CM
2883 /* Wait for the channel info to be read. */
2884 wait_event(intf->waitq,
2885 intf->curr_channel >= IPMI_MAX_CHANNELS);
50c812b2 2886 intf->null_user_handler = NULL;
393d2cc3
CM
2887 } else {
2888 /* Assume a single IPMB channel at zero. */
2889 intf->channels[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
2890 intf->channels[0].protocol = IPMI_CHANNEL_PROTOCOL_IPMB;
9a2845c4 2891 intf->curr_channel = IPMI_MAX_CHANNELS;
1da177e4
LT
2892 }
2893
bd85f4b3
XX
2894 rv = ipmi_bmc_register(intf, i);
2895
393d2cc3
CM
2896 if (rv == 0)
2897 rv = add_proc_entries(intf, i);
1da177e4 2898
393d2cc3 2899 out:
1da177e4 2900 if (rv) {
393d2cc3
CM
2901 if (intf->proc_dir)
2902 remove_proc_entries(intf);
b2c03941 2903 intf->handlers = NULL;
bca0324d
CM
2904 list_del_rcu(&intf->link);
2905 mutex_unlock(&ipmi_interfaces_mutex);
b2c03941 2906 mutex_unlock(&smi_watchers_mutex);
bca0324d 2907 synchronize_rcu();
393d2cc3 2908 kref_put(&intf->refcount, intf_free);
393d2cc3 2909 } else {
78ba2faf
CM
2910 /*
2911 * Keep memory order straight for RCU readers. Make
2912 * sure everything else is committed to memory before
2913 * setting intf_num to mark the interface valid.
2914 */
2915 smp_wmb();
bca0324d
CM
2916 intf->intf_num = i;
2917 mutex_unlock(&ipmi_interfaces_mutex);
78ba2faf 2918 /* After this point the interface is legal to use. */
50c812b2 2919 call_smi_watchers(i, intf->si_dev);
b2c03941 2920 mutex_unlock(&smi_watchers_mutex);
1da177e4
LT
2921 }
2922
2923 return rv;
2924}
c70d7499 2925EXPORT_SYMBOL(ipmi_register_smi);
1da177e4 2926
7ea0ed2b
CM
2927static void deliver_smi_err_response(ipmi_smi_t intf,
2928 struct ipmi_smi_msg *msg,
2929 unsigned char err)
2930{
2931 msg->rsp[0] = msg->data[0] | 4;
2932 msg->rsp[1] = msg->data[1];
2933 msg->rsp[2] = err;
2934 msg->rsp_size = 3;
2935 /* It's an error, so it will never requeue, no need to check return. */
2936 handle_one_recv_msg(intf, msg);
2937}
2938
b2c03941
CM
2939static void cleanup_smi_msgs(ipmi_smi_t intf)
2940{
2941 int i;
2942 struct seq_table *ent;
7ea0ed2b
CM
2943 struct ipmi_smi_msg *msg;
2944 struct list_head *entry;
2945 struct list_head tmplist;
2946
2947 /* Clear out our transmit queues and hold the messages. */
2948 INIT_LIST_HEAD(&tmplist);
2949 list_splice_tail(&intf->hp_xmit_msgs, &tmplist);
2950 list_splice_tail(&intf->xmit_msgs, &tmplist);
2951
2952 /* Current message first, to preserve order */
2953 while (intf->curr_msg && !list_empty(&intf->waiting_rcv_msgs)) {
2954 /* Wait for the message to clear out. */
2955 schedule_timeout(1);
2956 }
b2c03941
CM
2957
2958 /* No need for locks, the interface is down. */
7ea0ed2b
CM
2959
2960 /*
2961 * Return errors for all pending messages in queue and in the
2962 * tables waiting for remote responses.
2963 */
2964 while (!list_empty(&tmplist)) {
2965 entry = tmplist.next;
2966 list_del(entry);
2967 msg = list_entry(entry, struct ipmi_smi_msg, link);
2968 deliver_smi_err_response(intf, msg, IPMI_ERR_UNSPECIFIED);
2969 }
2970
b2c03941
CM
2971 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
2972 ent = &(intf->seq_table[i]);
2973 if (!ent->inuse)
2974 continue;
2975 deliver_err_response(ent->recv_msg, IPMI_ERR_UNSPECIFIED);
2976 }
2977}
2978
1da177e4
LT
2979int ipmi_unregister_smi(ipmi_smi_t intf)
2980{
1da177e4 2981 struct ipmi_smi_watcher *w;
7ea0ed2b
CM
2982 int intf_num = intf->intf_num;
2983 ipmi_user_t user;
1da177e4 2984
b2c03941 2985 mutex_lock(&smi_watchers_mutex);
bca0324d 2986 mutex_lock(&ipmi_interfaces_mutex);
b2c03941 2987 intf->intf_num = -1;
7ea0ed2b 2988 intf->in_shutdown = true;
bca0324d
CM
2989 list_del_rcu(&intf->link);
2990 mutex_unlock(&ipmi_interfaces_mutex);
2991 synchronize_rcu();
1da177e4 2992
b2c03941
CM
2993 cleanup_smi_msgs(intf);
2994
7ea0ed2b
CM
2995 /* Clean up the effects of users on the lower-level software. */
2996 mutex_lock(&ipmi_interfaces_mutex);
2997 rcu_read_lock();
2998 list_for_each_entry_rcu(user, &intf->users, link) {
2999 module_put(intf->handlers->owner);
3000 if (intf->handlers->dec_usecount)
3001 intf->handlers->dec_usecount(intf->send_info);
3002 }
3003 rcu_read_unlock();
3004 intf->handlers = NULL;
3005 mutex_unlock(&ipmi_interfaces_mutex);
3006
393d2cc3 3007 remove_proc_entries(intf);
bd85f4b3 3008 ipmi_bmc_unregister(intf);
1da177e4 3009
c70d7499
CM
3010 /*
3011 * Call all the watcher interfaces to tell them that
3012 * an interface is gone.
3013 */
393d2cc3 3014 list_for_each_entry(w, &smi_watchers, link)
b2c03941
CM
3015 w->smi_gone(intf_num);
3016 mutex_unlock(&smi_watchers_mutex);
393d2cc3 3017
393d2cc3 3018 kref_put(&intf->refcount, intf_free);
1da177e4
LT
3019 return 0;
3020}
c70d7499 3021EXPORT_SYMBOL(ipmi_unregister_smi);
1da177e4
LT
3022
3023static int handle_ipmb_get_msg_rsp(ipmi_smi_t intf,
3024 struct ipmi_smi_msg *msg)
3025{
3026 struct ipmi_ipmb_addr ipmb_addr;
3027 struct ipmi_recv_msg *recv_msg;
1da177e4 3028
c70d7499
CM
3029 /*
3030 * This is 11, not 10, because the response must contain a
3031 * completion code.
3032 */
1da177e4
LT
3033 if (msg->rsp_size < 11) {
3034 /* Message not big enough, just ignore it. */
b2655f26 3035 ipmi_inc_stat(intf, invalid_ipmb_responses);
1da177e4
LT
3036 return 0;
3037 }
3038
3039 if (msg->rsp[2] != 0) {
3040 /* An error getting the response, just ignore it. */
3041 return 0;
3042 }
3043
3044 ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
3045 ipmb_addr.slave_addr = msg->rsp[6];
3046 ipmb_addr.channel = msg->rsp[3] & 0x0f;
3047 ipmb_addr.lun = msg->rsp[7] & 3;
3048
c70d7499
CM
3049 /*
3050 * It's a response from a remote entity. Look up the sequence
3051 * number and handle the response.
3052 */
1da177e4
LT
3053 if (intf_find_seq(intf,
3054 msg->rsp[7] >> 2,
3055 msg->rsp[3] & 0x0f,
3056 msg->rsp[8],
3057 (msg->rsp[4] >> 2) & (~1),
3058 (struct ipmi_addr *) &(ipmb_addr),
c70d7499
CM
3059 &recv_msg)) {
3060 /*
3061 * We were unable to find the sequence number,
3062 * so just nuke the message.
3063 */
b2655f26 3064 ipmi_inc_stat(intf, unhandled_ipmb_responses);
1da177e4
LT
3065 return 0;
3066 }
3067
3068 memcpy(recv_msg->msg_data,
3069 &(msg->rsp[9]),
3070 msg->rsp_size - 9);
c70d7499
CM
3071 /*
3072 * The other fields matched, so no need to set them, except
3073 * for netfn, which needs to be the response that was
3074 * returned, not the request value.
3075 */
1da177e4
LT
3076 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3077 recv_msg->msg.data = recv_msg->msg_data;
3078 recv_msg->msg.data_len = msg->rsp_size - 10;
3079 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
b2655f26 3080 ipmi_inc_stat(intf, handled_ipmb_responses);
1da177e4
LT
3081 deliver_response(recv_msg);
3082
3083 return 0;
3084}
3085
3086static int handle_ipmb_get_msg_cmd(ipmi_smi_t intf,
3087 struct ipmi_smi_msg *msg)
3088{
393d2cc3
CM
3089 struct cmd_rcvr *rcvr;
3090 int rv = 0;
3091 unsigned char netfn;
3092 unsigned char cmd;
c69c3127 3093 unsigned char chan;
393d2cc3
CM
3094 ipmi_user_t user = NULL;
3095 struct ipmi_ipmb_addr *ipmb_addr;
3096 struct ipmi_recv_msg *recv_msg;
1da177e4
LT
3097
3098 if (msg->rsp_size < 10) {
3099 /* Message not big enough, just ignore it. */
b2655f26 3100 ipmi_inc_stat(intf, invalid_commands);
1da177e4
LT
3101 return 0;
3102 }
3103
3104 if (msg->rsp[2] != 0) {
3105 /* An error getting the response, just ignore it. */
3106 return 0;
3107 }
3108
3109 netfn = msg->rsp[4] >> 2;
3110 cmd = msg->rsp[8];
c69c3127 3111 chan = msg->rsp[3] & 0xf;
1da177e4 3112
e61fb5b6 3113 rcu_read_lock();
c69c3127 3114 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
393d2cc3
CM
3115 if (rcvr) {
3116 user = rcvr->user;
3117 kref_get(&user->refcount);
3118 } else
3119 user = NULL;
e61fb5b6 3120 rcu_read_unlock();
1da177e4
LT
3121
3122 if (user == NULL) {
3123 /* We didn't find a user, deliver an error response. */
b2655f26 3124 ipmi_inc_stat(intf, unhandled_commands);
1da177e4
LT
3125
3126 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
3127 msg->data[1] = IPMI_SEND_MSG_CMD;
3128 msg->data[2] = msg->rsp[3];
3129 msg->data[3] = msg->rsp[6];
c70d7499 3130 msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
1da177e4 3131 msg->data[5] = ipmb_checksum(&(msg->data[3]), 2);
c14979b9 3132 msg->data[6] = intf->channels[msg->rsp[3] & 0xf].address;
c70d7499
CM
3133 /* rqseq/lun */
3134 msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
1da177e4
LT
3135 msg->data[8] = msg->rsp[8]; /* cmd */
3136 msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
3137 msg->data[10] = ipmb_checksum(&(msg->data[6]), 4);
3138 msg->data_size = 11;
3139
3140#ifdef DEBUG_MSGING
3141 {
3142 int m;
3143 printk("Invalid command:");
e8b33617 3144 for (m = 0; m < msg->data_size; m++)
1da177e4
LT
3145 printk(" %2.2x", msg->data[m]);
3146 printk("\n");
3147 }
3148#endif
b2c03941 3149 rcu_read_lock();
7ea0ed2b
CM
3150 if (!intf->in_shutdown) {
3151 smi_send(intf, intf->handlers, msg, 0);
c70d7499
CM
3152 /*
3153 * We used the message, so return the value
3154 * that causes it to not be freed or
3155 * queued.
3156 */
b2c03941
CM
3157 rv = -1;
3158 }
3159 rcu_read_unlock();
1da177e4
LT
3160 } else {
3161 /* Deliver the message to the user. */
b2655f26 3162 ipmi_inc_stat(intf, handled_commands);
1da177e4
LT
3163
3164 recv_msg = ipmi_alloc_recv_msg();
8a3628d5 3165 if (!recv_msg) {
c70d7499
CM
3166 /*
3167 * We couldn't allocate memory for the
3168 * message, so requeue it for handling
3169 * later.
3170 */
1da177e4 3171 rv = 1;
393d2cc3 3172 kref_put(&user->refcount, free_user);
1da177e4
LT
3173 } else {
3174 /* Extract the source address from the data. */
3175 ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
3176 ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
3177 ipmb_addr->slave_addr = msg->rsp[6];
3178 ipmb_addr->lun = msg->rsp[7] & 3;
3179 ipmb_addr->channel = msg->rsp[3] & 0xf;
3180
c70d7499
CM
3181 /*
3182 * Extract the rest of the message information
3183 * from the IPMB header.
3184 */
1da177e4
LT
3185 recv_msg->user = user;
3186 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3187 recv_msg->msgid = msg->rsp[7] >> 2;
3188 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3189 recv_msg->msg.cmd = msg->rsp[8];
3190 recv_msg->msg.data = recv_msg->msg_data;
3191
c70d7499
CM
3192 /*
3193 * We chop off 10, not 9 bytes because the checksum
3194 * at the end also needs to be removed.
3195 */
1da177e4
LT
3196 recv_msg->msg.data_len = msg->rsp_size - 10;
3197 memcpy(recv_msg->msg_data,
3198 &(msg->rsp[9]),
3199 msg->rsp_size - 10);
3200 deliver_response(recv_msg);
3201 }
3202 }
3203
3204 return rv;
3205}
3206
3207static int handle_lan_get_msg_rsp(ipmi_smi_t intf,
3208 struct ipmi_smi_msg *msg)
3209{
3210 struct ipmi_lan_addr lan_addr;
3211 struct ipmi_recv_msg *recv_msg;
1da177e4
LT
3212
3213
c70d7499
CM
3214 /*
3215 * This is 13, not 12, because the response must contain a
3216 * completion code.
3217 */
1da177e4
LT
3218 if (msg->rsp_size < 13) {
3219 /* Message not big enough, just ignore it. */
b2655f26 3220 ipmi_inc_stat(intf, invalid_lan_responses);
1da177e4
LT
3221 return 0;
3222 }
3223
3224 if (msg->rsp[2] != 0) {
3225 /* An error getting the response, just ignore it. */
3226 return 0;
3227 }
3228
3229 lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
3230 lan_addr.session_handle = msg->rsp[4];
3231 lan_addr.remote_SWID = msg->rsp[8];
3232 lan_addr.local_SWID = msg->rsp[5];
3233 lan_addr.channel = msg->rsp[3] & 0x0f;
3234 lan_addr.privilege = msg->rsp[3] >> 4;
3235 lan_addr.lun = msg->rsp[9] & 3;
3236
c70d7499
CM
3237 /*
3238 * It's a response from a remote entity. Look up the sequence
3239 * number and handle the response.
3240 */
1da177e4
LT
3241 if (intf_find_seq(intf,
3242 msg->rsp[9] >> 2,
3243 msg->rsp[3] & 0x0f,
3244 msg->rsp[10],
3245 (msg->rsp[6] >> 2) & (~1),
3246 (struct ipmi_addr *) &(lan_addr),
c70d7499
CM
3247 &recv_msg)) {
3248 /*
3249 * We were unable to find the sequence number,
3250 * so just nuke the message.
3251 */
b2655f26 3252 ipmi_inc_stat(intf, unhandled_lan_responses);
1da177e4
LT
3253 return 0;
3254 }
3255
3256 memcpy(recv_msg->msg_data,
3257 &(msg->rsp[11]),
3258 msg->rsp_size - 11);
c70d7499
CM
3259 /*
3260 * The other fields matched, so no need to set them, except
3261 * for netfn, which needs to be the response that was
3262 * returned, not the request value.
3263 */
1da177e4
LT
3264 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3265 recv_msg->msg.data = recv_msg->msg_data;
3266 recv_msg->msg.data_len = msg->rsp_size - 12;
3267 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
b2655f26 3268 ipmi_inc_stat(intf, handled_lan_responses);
1da177e4
LT
3269 deliver_response(recv_msg);
3270
3271 return 0;
3272}
3273
3274static int handle_lan_get_msg_cmd(ipmi_smi_t intf,
3275 struct ipmi_smi_msg *msg)
3276{
393d2cc3
CM
3277 struct cmd_rcvr *rcvr;
3278 int rv = 0;
3279 unsigned char netfn;
3280 unsigned char cmd;
c69c3127 3281 unsigned char chan;
393d2cc3
CM
3282 ipmi_user_t user = NULL;
3283 struct ipmi_lan_addr *lan_addr;
3284 struct ipmi_recv_msg *recv_msg;
1da177e4
LT
3285
3286 if (msg->rsp_size < 12) {
3287 /* Message not big enough, just ignore it. */
b2655f26 3288 ipmi_inc_stat(intf, invalid_commands);
1da177e4
LT
3289 return 0;
3290 }
3291
3292 if (msg->rsp[2] != 0) {
3293 /* An error getting the response, just ignore it. */
3294 return 0;
3295 }
3296
3297 netfn = msg->rsp[6] >> 2;
3298 cmd = msg->rsp[10];
c69c3127 3299 chan = msg->rsp[3] & 0xf;
1da177e4 3300
e61fb5b6 3301 rcu_read_lock();
c69c3127 3302 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
393d2cc3
CM
3303 if (rcvr) {
3304 user = rcvr->user;
3305 kref_get(&user->refcount);
3306 } else
3307 user = NULL;
e61fb5b6 3308 rcu_read_unlock();
1da177e4
LT
3309
3310 if (user == NULL) {
393d2cc3 3311 /* We didn't find a user, just give up. */
b2655f26 3312 ipmi_inc_stat(intf, unhandled_commands);
1da177e4 3313
c70d7499
CM
3314 /*
3315 * Don't do anything with these messages, just allow
3316 * them to be freed.
3317 */
3318 rv = 0;
1da177e4
LT
3319 } else {
3320 /* Deliver the message to the user. */
b2655f26 3321 ipmi_inc_stat(intf, handled_commands);
1da177e4
LT
3322
3323 recv_msg = ipmi_alloc_recv_msg();
8a3628d5 3324 if (!recv_msg) {
c70d7499
CM
3325 /*
3326 * We couldn't allocate memory for the
3327 * message, so requeue it for handling later.
3328 */
1da177e4 3329 rv = 1;
393d2cc3 3330 kref_put(&user->refcount, free_user);
1da177e4
LT
3331 } else {
3332 /* Extract the source address from the data. */
3333 lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
3334 lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
3335 lan_addr->session_handle = msg->rsp[4];
3336 lan_addr->remote_SWID = msg->rsp[8];
3337 lan_addr->local_SWID = msg->rsp[5];
3338 lan_addr->lun = msg->rsp[9] & 3;
3339 lan_addr->channel = msg->rsp[3] & 0xf;
3340 lan_addr->privilege = msg->rsp[3] >> 4;
3341
c70d7499
CM
3342 /*
3343 * Extract the rest of the message information
3344 * from the IPMB header.
3345 */
1da177e4
LT
3346 recv_msg->user = user;
3347 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3348 recv_msg->msgid = msg->rsp[9] >> 2;
3349 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3350 recv_msg->msg.cmd = msg->rsp[10];
3351 recv_msg->msg.data = recv_msg->msg_data;
3352
c70d7499
CM
3353 /*
3354 * We chop off 12, not 11 bytes because the checksum
3355 * at the end also needs to be removed.
3356 */
1da177e4
LT
3357 recv_msg->msg.data_len = msg->rsp_size - 12;
3358 memcpy(recv_msg->msg_data,
3359 &(msg->rsp[11]),
3360 msg->rsp_size - 12);
3361 deliver_response(recv_msg);
3362 }
3363 }
3364
3365 return rv;
3366}
3367
4dec302f 3368/*
3369 * This routine will handle "Get Message" command responses with
3370 * channels that use an OEM Medium. The message format belongs to
3371 * the OEM. See IPMI 2.0 specification, Chapter 6 and
3372 * Chapter 22, sections 22.6 and 22.24 for more details.
3373 */
3374static int handle_oem_get_msg_cmd(ipmi_smi_t intf,
3375 struct ipmi_smi_msg *msg)
3376{
3377 struct cmd_rcvr *rcvr;
3378 int rv = 0;
3379 unsigned char netfn;
3380 unsigned char cmd;
3381 unsigned char chan;
3382 ipmi_user_t user = NULL;
3383 struct ipmi_system_interface_addr *smi_addr;
3384 struct ipmi_recv_msg *recv_msg;
3385
3386 /*
3387 * We expect the OEM SW to perform error checking
3388 * so we just do some basic sanity checks
3389 */
3390 if (msg->rsp_size < 4) {
3391 /* Message not big enough, just ignore it. */
3392 ipmi_inc_stat(intf, invalid_commands);
3393 return 0;
3394 }
3395
3396 if (msg->rsp[2] != 0) {
3397 /* An error getting the response, just ignore it. */
3398 return 0;
3399 }
3400
3401 /*
3402 * This is an OEM Message so the OEM needs to know how
3403 * handle the message. We do no interpretation.
3404 */
3405 netfn = msg->rsp[0] >> 2;
3406 cmd = msg->rsp[1];
3407 chan = msg->rsp[3] & 0xf;
3408
3409 rcu_read_lock();
3410 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3411 if (rcvr) {
3412 user = rcvr->user;
3413 kref_get(&user->refcount);
3414 } else
3415 user = NULL;
3416 rcu_read_unlock();
3417
3418 if (user == NULL) {
3419 /* We didn't find a user, just give up. */
3420 ipmi_inc_stat(intf, unhandled_commands);
3421
3422 /*
3423 * Don't do anything with these messages, just allow
3424 * them to be freed.
3425 */
3426
3427 rv = 0;
3428 } else {
3429 /* Deliver the message to the user. */
3430 ipmi_inc_stat(intf, handled_commands);
3431
3432 recv_msg = ipmi_alloc_recv_msg();
3433 if (!recv_msg) {
3434 /*
3435 * We couldn't allocate memory for the
3436 * message, so requeue it for handling
3437 * later.
3438 */
3439 rv = 1;
3440 kref_put(&user->refcount, free_user);
3441 } else {
3442 /*
3443 * OEM Messages are expected to be delivered via
3444 * the system interface to SMS software. We might
3445 * need to visit this again depending on OEM
3446 * requirements
3447 */
3448 smi_addr = ((struct ipmi_system_interface_addr *)
3449 &(recv_msg->addr));
3450 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3451 smi_addr->channel = IPMI_BMC_CHANNEL;
3452 smi_addr->lun = msg->rsp[0] & 3;
3453
3454 recv_msg->user = user;
3455 recv_msg->user_msg_data = NULL;
3456 recv_msg->recv_type = IPMI_OEM_RECV_TYPE;
3457 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3458 recv_msg->msg.cmd = msg->rsp[1];
3459 recv_msg->msg.data = recv_msg->msg_data;
3460
3461 /*
3462 * The message starts at byte 4 which follows the
3463 * the Channel Byte in the "GET MESSAGE" command
3464 */
3465 recv_msg->msg.data_len = msg->rsp_size - 4;
3466 memcpy(recv_msg->msg_data,
3467 &(msg->rsp[4]),
3468 msg->rsp_size - 4);
3469 deliver_response(recv_msg);
3470 }
3471 }
3472
3473 return rv;
3474}
3475
1da177e4
LT
3476static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
3477 struct ipmi_smi_msg *msg)
3478{
3479 struct ipmi_system_interface_addr *smi_addr;
c70d7499 3480
1da177e4
LT
3481 recv_msg->msgid = 0;
3482 smi_addr = (struct ipmi_system_interface_addr *) &(recv_msg->addr);
3483 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3484 smi_addr->channel = IPMI_BMC_CHANNEL;
3485 smi_addr->lun = msg->rsp[0] & 3;
3486 recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
3487 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3488 recv_msg->msg.cmd = msg->rsp[1];
3489 memcpy(recv_msg->msg_data, &(msg->rsp[3]), msg->rsp_size - 3);
3490 recv_msg->msg.data = recv_msg->msg_data;
3491 recv_msg->msg.data_len = msg->rsp_size - 3;
3492}
3493
1da177e4
LT
3494static int handle_read_event_rsp(ipmi_smi_t intf,
3495 struct ipmi_smi_msg *msg)
3496{
3497 struct ipmi_recv_msg *recv_msg, *recv_msg2;
3498 struct list_head msgs;
3499 ipmi_user_t user;
3500 int rv = 0;
3501 int deliver_count = 0;
3502 unsigned long flags;
3503
3504 if (msg->rsp_size < 19) {
3505 /* Message is too small to be an IPMB event. */
b2655f26 3506 ipmi_inc_stat(intf, invalid_events);
1da177e4
LT
3507 return 0;
3508 }
3509
3510 if (msg->rsp[2] != 0) {
3511 /* An error getting the event, just ignore it. */
3512 return 0;
3513 }
3514
3515 INIT_LIST_HEAD(&msgs);
3516
393d2cc3 3517 spin_lock_irqsave(&intf->events_lock, flags);
1da177e4 3518
b2655f26 3519 ipmi_inc_stat(intf, events);
1da177e4 3520
c70d7499
CM
3521 /*
3522 * Allocate and fill in one message for every user that is
3523 * getting events.
3524 */
393d2cc3
CM
3525 rcu_read_lock();
3526 list_for_each_entry_rcu(user, &intf->users, link) {
8a3628d5 3527 if (!user->gets_events)
1da177e4
LT
3528 continue;
3529
3530 recv_msg = ipmi_alloc_recv_msg();
8a3628d5 3531 if (!recv_msg) {
393d2cc3 3532 rcu_read_unlock();
8a3628d5
CM
3533 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs,
3534 link) {
1da177e4
LT
3535 list_del(&recv_msg->link);
3536 ipmi_free_recv_msg(recv_msg);
3537 }
c70d7499
CM
3538 /*
3539 * We couldn't allocate memory for the
3540 * message, so requeue it for handling
3541 * later.
3542 */
1da177e4
LT
3543 rv = 1;
3544 goto out;
3545 }
3546
3547 deliver_count++;
3548
3549 copy_event_into_recv_msg(recv_msg, msg);
3550 recv_msg->user = user;
393d2cc3 3551 kref_get(&user->refcount);
1da177e4
LT
3552 list_add_tail(&(recv_msg->link), &msgs);
3553 }
393d2cc3 3554 rcu_read_unlock();
1da177e4
LT
3555
3556 if (deliver_count) {
3557 /* Now deliver all the messages. */
3558 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
3559 list_del(&recv_msg->link);
3560 deliver_response(recv_msg);
3561 }
3562 } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
c70d7499
CM
3563 /*
3564 * No one to receive the message, put it in queue if there's
3565 * not already too many things in the queue.
3566 */
1da177e4 3567 recv_msg = ipmi_alloc_recv_msg();
8a3628d5 3568 if (!recv_msg) {
c70d7499
CM
3569 /*
3570 * We couldn't allocate memory for the
3571 * message, so requeue it for handling
3572 * later.
3573 */
1da177e4
LT
3574 rv = 1;
3575 goto out;
3576 }
3577
3578 copy_event_into_recv_msg(recv_msg, msg);
3579 list_add_tail(&(recv_msg->link), &(intf->waiting_events));
4791c03d 3580 intf->waiting_events_count++;
87ebd06f 3581 } else if (!intf->event_msg_printed) {
c70d7499
CM
3582 /*
3583 * There's too many things in the queue, discard this
3584 * message.
3585 */
87ebd06f
CM
3586 printk(KERN_WARNING PFX "Event queue full, discarding"
3587 " incoming events\n");
3588 intf->event_msg_printed = 1;
1da177e4
LT
3589 }
3590
3591 out:
3592 spin_unlock_irqrestore(&(intf->events_lock), flags);
3593
3594 return rv;
3595}
3596
3597static int handle_bmc_rsp(ipmi_smi_t intf,
3598 struct ipmi_smi_msg *msg)
3599{
3600 struct ipmi_recv_msg *recv_msg;
393d2cc3 3601 struct ipmi_user *user;
1da177e4
LT
3602
3603 recv_msg = (struct ipmi_recv_msg *) msg->user_data;
c70d7499
CM
3604 if (recv_msg == NULL) {
3605 printk(KERN_WARNING
3606 "IPMI message received with no owner. This\n"
3607 "could be because of a malformed message, or\n"
3608 "because of a hardware error. Contact your\n"
3609 "hardware vender for assistance\n");
56a55ec6
CM
3610 return 0;
3611 }
1da177e4 3612
393d2cc3 3613 user = recv_msg->user;
1da177e4 3614 /* Make sure the user still exists. */
393d2cc3 3615 if (user && !user->valid) {
56a55ec6 3616 /* The user for the message went away, so give up. */
b2655f26 3617 ipmi_inc_stat(intf, unhandled_local_responses);
1da177e4
LT
3618 ipmi_free_recv_msg(recv_msg);
3619 } else {
3620 struct ipmi_system_interface_addr *smi_addr;
3621
b2655f26 3622 ipmi_inc_stat(intf, handled_local_responses);
1da177e4
LT
3623 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3624 recv_msg->msgid = msg->msgid;
3625 smi_addr = ((struct ipmi_system_interface_addr *)
3626 &(recv_msg->addr));
3627 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3628 smi_addr->channel = IPMI_BMC_CHANNEL;
3629 smi_addr->lun = msg->rsp[0] & 3;
3630 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3631 recv_msg->msg.cmd = msg->rsp[1];
3632 memcpy(recv_msg->msg_data,
3633 &(msg->rsp[2]),
3634 msg->rsp_size - 2);
3635 recv_msg->msg.data = recv_msg->msg_data;
3636 recv_msg->msg.data_len = msg->rsp_size - 2;
3637 deliver_response(recv_msg);
3638 }
3639
3640 return 0;
3641}
3642
c70d7499 3643/*
7adf579c 3644 * Handle a received message. Return 1 if the message should be requeued,
c70d7499
CM
3645 * 0 if the message should be freed, or -1 if the message should not
3646 * be freed or requeued.
3647 */
7adf579c 3648static int handle_one_recv_msg(ipmi_smi_t intf,
1da177e4
LT
3649 struct ipmi_smi_msg *msg)
3650{
3651 int requeue;
3652 int chan;
3653
3654#ifdef DEBUG_MSGING
3655 int m;
3656 printk("Recv:");
e8b33617 3657 for (m = 0; m < msg->rsp_size; m++)
1da177e4
LT
3658 printk(" %2.2x", msg->rsp[m]);
3659 printk("\n");
3660#endif
3661 if (msg->rsp_size < 2) {
3662 /* Message is too small to be correct. */
3663 printk(KERN_WARNING PFX "BMC returned to small a message"
3664 " for netfn %x cmd %x, got %d bytes\n",
3665 (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size);
3666
3667 /* Generate an error response for the message. */
3668 msg->rsp[0] = msg->data[0] | (1 << 2);
3669 msg->rsp[1] = msg->data[1];
3670 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
3671 msg->rsp_size = 3;
c70d7499
CM
3672 } else if (((msg->rsp[0] >> 2) != ((msg->data[0] >> 2) | 1))
3673 || (msg->rsp[1] != msg->data[1])) {
3674 /*
3675 * The NetFN and Command in the response is not even
3676 * marginally correct.
3677 */
1da177e4
LT
3678 printk(KERN_WARNING PFX "BMC returned incorrect response,"
3679 " expected netfn %x cmd %x, got netfn %x cmd %x\n",
3680 (msg->data[0] >> 2) | 1, msg->data[1],
3681 msg->rsp[0] >> 2, msg->rsp[1]);
3682
3683 /* Generate an error response for the message. */
3684 msg->rsp[0] = msg->data[0] | (1 << 2);
3685 msg->rsp[1] = msg->data[1];
3686 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
3687 msg->rsp_size = 3;
3688 }
3689
3690 if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
3691 && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
c70d7499
CM
3692 && (msg->user_data != NULL)) {
3693 /*
3694 * It's a response to a response we sent. For this we
3695 * deliver a send message response to the user.
3696 */
393d2cc3 3697 struct ipmi_recv_msg *recv_msg = msg->user_data;
1da177e4
LT
3698
3699 requeue = 0;
3700 if (msg->rsp_size < 2)
3701 /* Message is too small to be correct. */
3702 goto out;
3703
3704 chan = msg->data[2] & 0x0f;
3705 if (chan >= IPMI_MAX_CHANNELS)
3706 /* Invalid channel number */
3707 goto out;
3708
393d2cc3
CM
3709 if (!recv_msg)
3710 goto out;
3711
3712 /* Make sure the user still exists. */
3713 if (!recv_msg->user || !recv_msg->user->valid)
3714 goto out;
3715
3716 recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
3717 recv_msg->msg.data = recv_msg->msg_data;
3718 recv_msg->msg.data_len = 1;
3719 recv_msg->msg_data[0] = msg->rsp[2];
3720 deliver_response(recv_msg);
1da177e4 3721 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
c70d7499 3722 && (msg->rsp[1] == IPMI_GET_MSG_CMD)) {
1da177e4
LT
3723 /* It's from the receive queue. */
3724 chan = msg->rsp[3] & 0xf;
3725 if (chan >= IPMI_MAX_CHANNELS) {
3726 /* Invalid channel number */
3727 requeue = 0;
3728 goto out;
3729 }
3730
4dec302f 3731 /*
9a2845c4
CM
3732 * We need to make sure the channels have been initialized.
3733 * The channel_handler routine will set the "curr_channel"
3734 * equal to or greater than IPMI_MAX_CHANNELS when all the
3735 * channels for this interface have been initialized.
3736 */
4dec302f 3737 if (intf->curr_channel < IPMI_MAX_CHANNELS) {
9a2845c4 3738 requeue = 0; /* Throw the message away */
4dec302f 3739 goto out;
3740 }
3741
1da177e4
LT
3742 switch (intf->channels[chan].medium) {
3743 case IPMI_CHANNEL_MEDIUM_IPMB:
3744 if (msg->rsp[4] & 0x04) {
c70d7499
CM
3745 /*
3746 * It's a response, so find the
3747 * requesting message and send it up.
3748 */
1da177e4
LT
3749 requeue = handle_ipmb_get_msg_rsp(intf, msg);
3750 } else {
c70d7499
CM
3751 /*
3752 * It's a command to the SMS from some other
3753 * entity. Handle that.
3754 */
1da177e4
LT
3755 requeue = handle_ipmb_get_msg_cmd(intf, msg);
3756 }
3757 break;
3758
3759 case IPMI_CHANNEL_MEDIUM_8023LAN:
3760 case IPMI_CHANNEL_MEDIUM_ASYNC:
3761 if (msg->rsp[6] & 0x04) {
c70d7499
CM
3762 /*
3763 * It's a response, so find the
3764 * requesting message and send it up.
3765 */
1da177e4
LT
3766 requeue = handle_lan_get_msg_rsp(intf, msg);
3767 } else {
c70d7499
CM
3768 /*
3769 * It's a command to the SMS from some other
3770 * entity. Handle that.
3771 */
1da177e4
LT
3772 requeue = handle_lan_get_msg_cmd(intf, msg);
3773 }
3774 break;
3775
3776 default:
4dec302f 3777 /* Check for OEM Channels. Clients had better
3778 register for these commands. */
3779 if ((intf->channels[chan].medium
3780 >= IPMI_CHANNEL_MEDIUM_OEM_MIN)
3781 && (intf->channels[chan].medium
3782 <= IPMI_CHANNEL_MEDIUM_OEM_MAX)) {
3783 requeue = handle_oem_get_msg_cmd(intf, msg);
3784 } else {
3785 /*
3786 * We don't handle the channel type, so just
3787 * free the message.
3788 */
3789 requeue = 0;
3790 }
1da177e4
LT
3791 }
3792
3793 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
c70d7499 3794 && (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD)) {
b3834be5 3795 /* It's an asynchronous event. */
1da177e4
LT
3796 requeue = handle_read_event_rsp(intf, msg);
3797 } else {
3798 /* It's a response from the local BMC. */
3799 requeue = handle_bmc_rsp(intf, msg);
3800 }
3801
3802 out:
3803 return requeue;
3804}
3805
7adf579c
CM
3806/*
3807 * If there are messages in the queue or pretimeouts, handle them.
3808 */
3809static void handle_new_recv_msgs(ipmi_smi_t intf)
3810{
3811 struct ipmi_smi_msg *smi_msg;
3812 unsigned long flags = 0;
3813 int rv;
3814 int run_to_completion = intf->run_to_completion;
3815
3816 /* See if any waiting messages need to be processed. */
3817 if (!run_to_completion)
65be7544
CM
3818 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
3819 while (!list_empty(&intf->waiting_rcv_msgs)) {
3820 smi_msg = list_entry(intf->waiting_rcv_msgs.next,
7adf579c 3821 struct ipmi_smi_msg, link);
ae4ea9a2 3822 list_del(&smi_msg->link);
7adf579c 3823 if (!run_to_completion)
65be7544
CM
3824 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
3825 flags);
7adf579c
CM
3826 rv = handle_one_recv_msg(intf, smi_msg);
3827 if (!run_to_completion)
65be7544 3828 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
7ea0ed2b 3829 if (rv > 0) {
7adf579c
CM
3830 /*
3831 * To preserve message order, quit if we
ae4ea9a2
JN
3832 * can't handle a message. Add the message
3833 * back at the head, this is safe because this
3834 * tasklet is the only thing that pulls the
3835 * messages.
7adf579c 3836 */
ae4ea9a2 3837 list_add(&smi_msg->link, &intf->waiting_rcv_msgs);
7adf579c 3838 break;
7ea0ed2b 3839 } else {
7ea0ed2b
CM
3840 if (rv == 0)
3841 /* Message handled */
3842 ipmi_free_smi_msg(smi_msg);
3843 /* If rv < 0, fatal error, del but don't free. */
7adf579c
CM
3844 }
3845 }
3846 if (!run_to_completion)
65be7544 3847 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock, flags);
7adf579c
CM
3848
3849 /*
3850 * If the pretimout count is non-zero, decrement one from it and
3851 * deliver pretimeouts to all the users.
3852 */
3853 if (atomic_add_unless(&intf->watchdog_pretimeouts_to_deliver, -1, 0)) {
3854 ipmi_user_t user;
3855
3856 rcu_read_lock();
3857 list_for_each_entry_rcu(user, &intf->users, link) {
3858 if (user->handler->ipmi_watchdog_pretimeout)
3859 user->handler->ipmi_watchdog_pretimeout(
3860 user->handler_data);
3861 }
3862 rcu_read_unlock();
3863 }
3864}
3865
3866static void smi_recv_tasklet(unsigned long val)
3867{
7ea0ed2b
CM
3868 unsigned long flags = 0; /* keep us warning-free. */
3869 ipmi_smi_t intf = (ipmi_smi_t) val;
3870 int run_to_completion = intf->run_to_completion;
3871 struct ipmi_smi_msg *newmsg = NULL;
3872
3873 /*
3874 * Start the next message if available.
3875 *
3876 * Do this here, not in the actual receiver, because we may deadlock
3877 * because the lower layer is allowed to hold locks while calling
3878 * message delivery.
3879 */
3880 if (!run_to_completion)
3881 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
3882 if (intf->curr_msg == NULL && !intf->in_shutdown) {
3883 struct list_head *entry = NULL;
3884
3885 /* Pick the high priority queue first. */
3886 if (!list_empty(&intf->hp_xmit_msgs))
3887 entry = intf->hp_xmit_msgs.next;
3888 else if (!list_empty(&intf->xmit_msgs))
3889 entry = intf->xmit_msgs.next;
3890
3891 if (entry) {
3892 list_del(entry);
3893 newmsg = list_entry(entry, struct ipmi_smi_msg, link);
3894 intf->curr_msg = newmsg;
3895 }
3896 }
3897 if (!run_to_completion)
3898 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
3899 if (newmsg)
99ab32f3 3900 intf->handlers->sender(intf->send_info, newmsg);
7ea0ed2b
CM
3901
3902 handle_new_recv_msgs(intf);
7adf579c
CM
3903}
3904
1da177e4
LT
3905/* Handle a new message from the lower layer. */
3906void ipmi_smi_msg_received(ipmi_smi_t intf,
3907 struct ipmi_smi_msg *msg)
3908{
5956dce1 3909 unsigned long flags = 0; /* keep us warning-free. */
7ea0ed2b 3910 int run_to_completion = intf->run_to_completion;
1da177e4 3911
1da177e4
LT
3912 if ((msg->data_size >= 2)
3913 && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
3914 && (msg->data[1] == IPMI_SEND_MSG_CMD)
c70d7499 3915 && (msg->user_data == NULL)) {
7ea0ed2b
CM
3916
3917 if (intf->in_shutdown)
3918 goto free_msg;
3919
c70d7499
CM
3920 /*
3921 * This is the local response to a command send, start
3922 * the timer for these. The user_data will not be
3923 * NULL if this is a response send, and we will let
3924 * response sends just go through.
3925 */
3926
3927 /*
3928 * Check for errors, if we get certain errors (ones
3929 * that mean basically we can try again later), we
3930 * ignore them and start the timer. Otherwise we
3931 * report the error immediately.
3932 */
1da177e4
LT
3933 if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
3934 && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
46d52b09
CM
3935 && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR)
3936 && (msg->rsp[2] != IPMI_BUS_ERR)
c70d7499 3937 && (msg->rsp[2] != IPMI_NAK_ON_WRITE_ERR)) {
1da177e4
LT
3938 int chan = msg->rsp[3] & 0xf;
3939
3940 /* Got an error sending the message, handle it. */
1da177e4
LT
3941 if (chan >= IPMI_MAX_CHANNELS)
3942 ; /* This shouldn't happen */
3943 else if ((intf->channels[chan].medium
3944 == IPMI_CHANNEL_MEDIUM_8023LAN)
3945 || (intf->channels[chan].medium
3946 == IPMI_CHANNEL_MEDIUM_ASYNC))
b2655f26 3947 ipmi_inc_stat(intf, sent_lan_command_errs);
1da177e4 3948 else
b2655f26 3949 ipmi_inc_stat(intf, sent_ipmb_command_errs);
1da177e4 3950 intf_err_seq(intf, msg->msgid, msg->rsp[2]);
c70d7499 3951 } else
1da177e4
LT
3952 /* The message was sent, start the timer. */
3953 intf_start_seq_timer(intf, msg->msgid);
1da177e4 3954
7ea0ed2b 3955free_msg:
1da177e4 3956 ipmi_free_smi_msg(msg);
7ea0ed2b
CM
3957 } else {
3958 /*
3959 * To preserve message order, we keep a queue and deliver from
3960 * a tasklet.
3961 */
3962 if (!run_to_completion)
3963 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
3964 list_add_tail(&msg->link, &intf->waiting_rcv_msgs);
3965 if (!run_to_completion)
3966 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
3967 flags);
1da177e4
LT
3968 }
3969
5956dce1 3970 if (!run_to_completion)
7ea0ed2b 3971 spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
b2234ee9
CM
3972 /*
3973 * We can get an asynchronous event or receive message in addition
3974 * to commands we send.
3975 */
7ea0ed2b
CM
3976 if (msg == intf->curr_msg)
3977 intf->curr_msg = NULL;
5956dce1 3978 if (!run_to_completion)
7ea0ed2b 3979 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
c70d7499 3980
7ea0ed2b
CM
3981 if (run_to_completion)
3982 smi_recv_tasklet((unsigned long) intf);
3983 else
3984 tasklet_schedule(&intf->recv_tasklet);
1da177e4 3985}
c70d7499 3986EXPORT_SYMBOL(ipmi_smi_msg_received);
1da177e4
LT
3987
3988void ipmi_smi_watchdog_pretimeout(ipmi_smi_t intf)
3989{
7ea0ed2b
CM
3990 if (intf->in_shutdown)
3991 return;
3992
7adf579c
CM
3993 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 1);
3994 tasklet_schedule(&intf->recv_tasklet);
1da177e4 3995}
c70d7499 3996EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
1da177e4 3997
882fe011
CM
3998static struct ipmi_smi_msg *
3999smi_from_recv_msg(ipmi_smi_t intf, struct ipmi_recv_msg *recv_msg,
4000 unsigned char seq, long seqid)
1da177e4 4001{
882fe011 4002 struct ipmi_smi_msg *smi_msg = ipmi_alloc_smi_msg();
1da177e4 4003 if (!smi_msg)
c70d7499
CM
4004 /*
4005 * If we can't allocate the message, then just return, we
4006 * get 4 retries, so this should be ok.
4007 */
882fe011 4008 return NULL;
1da177e4
LT
4009
4010 memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
4011 smi_msg->data_size = recv_msg->msg.data_len;
4012 smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
c70d7499 4013
1da177e4
LT
4014#ifdef DEBUG_MSGING
4015 {
4016 int m;
4017 printk("Resend: ");
e8b33617 4018 for (m = 0; m < smi_msg->data_size; m++)
1da177e4
LT
4019 printk(" %2.2x", smi_msg->data[m]);
4020 printk("\n");
4021 }
4022#endif
882fe011 4023 return smi_msg;
1da177e4
LT
4024}
4025
393d2cc3
CM
4026static void check_msg_timeout(ipmi_smi_t intf, struct seq_table *ent,
4027 struct list_head *timeouts, long timeout_period,
89986496
CM
4028 int slot, unsigned long *flags,
4029 unsigned int *waiting_msgs)
393d2cc3 4030{
b2c03941 4031 struct ipmi_recv_msg *msg;
81d02b7f 4032 const struct ipmi_smi_handlers *handlers;
b2c03941 4033
7ea0ed2b 4034 if (intf->in_shutdown)
b2c03941 4035 return;
393d2cc3
CM
4036
4037 if (!ent->inuse)
4038 return;
4039
4040 ent->timeout -= timeout_period;
89986496
CM
4041 if (ent->timeout > 0) {
4042 (*waiting_msgs)++;
393d2cc3 4043 return;
89986496 4044 }
393d2cc3
CM
4045
4046 if (ent->retries_left == 0) {
4047 /* The message has used all its retries. */
4048 ent->inuse = 0;
4049 msg = ent->recv_msg;
4050 list_add_tail(&msg->link, timeouts);
393d2cc3 4051 if (ent->broadcast)
b2655f26 4052 ipmi_inc_stat(intf, timed_out_ipmb_broadcasts);
25176ed6 4053 else if (is_lan_addr(&ent->recv_msg->addr))
b2655f26 4054 ipmi_inc_stat(intf, timed_out_lan_commands);
393d2cc3 4055 else
b2655f26 4056 ipmi_inc_stat(intf, timed_out_ipmb_commands);
393d2cc3
CM
4057 } else {
4058 struct ipmi_smi_msg *smi_msg;
4059 /* More retries, send again. */
4060
89986496
CM
4061 (*waiting_msgs)++;
4062
c70d7499
CM
4063 /*
4064 * Start with the max timer, set to normal timer after
4065 * the message is sent.
4066 */
393d2cc3
CM
4067 ent->timeout = MAX_MSG_TIMEOUT;
4068 ent->retries_left--;
393d2cc3
CM
4069 smi_msg = smi_from_recv_msg(intf, ent->recv_msg, slot,
4070 ent->seqid);
25176ed6
CM
4071 if (!smi_msg) {
4072 if (is_lan_addr(&ent->recv_msg->addr))
4073 ipmi_inc_stat(intf,
4074 dropped_rexmit_lan_commands);
4075 else
4076 ipmi_inc_stat(intf,
4077 dropped_rexmit_ipmb_commands);
393d2cc3 4078 return;
25176ed6 4079 }
393d2cc3
CM
4080
4081 spin_unlock_irqrestore(&intf->seq_lock, *flags);
b2c03941 4082
c70d7499
CM
4083 /*
4084 * Send the new message. We send with a zero
4085 * priority. It timed out, I doubt time is that
4086 * critical now, and high priority messages are really
4087 * only for messages to the local MC, which don't get
4088 * resent.
4089 */
b2c03941 4090 handlers = intf->handlers;
25176ed6
CM
4091 if (handlers) {
4092 if (is_lan_addr(&ent->recv_msg->addr))
4093 ipmi_inc_stat(intf,
4094 retransmitted_lan_commands);
4095 else
4096 ipmi_inc_stat(intf,
4097 retransmitted_ipmb_commands);
4098
81d02b7f 4099 smi_send(intf, handlers, smi_msg, 0);
25176ed6 4100 } else
b2c03941
CM
4101 ipmi_free_smi_msg(smi_msg);
4102
393d2cc3
CM
4103 spin_lock_irqsave(&intf->seq_lock, *flags);
4104 }
4105}
4106
89986496 4107static unsigned int ipmi_timeout_handler(ipmi_smi_t intf, long timeout_period)
1da177e4 4108{
1da177e4
LT
4109 struct list_head timeouts;
4110 struct ipmi_recv_msg *msg, *msg2;
1da177e4 4111 unsigned long flags;
bca0324d 4112 int i;
89986496 4113 unsigned int waiting_msgs = 0;
1da177e4 4114
89986496
CM
4115 /*
4116 * Go through the seq table and find any messages that
4117 * have timed out, putting them in the timeouts
4118 * list.
4119 */
4120 INIT_LIST_HEAD(&timeouts);
4121 spin_lock_irqsave(&intf->seq_lock, flags);
4122 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++)
4123 check_msg_timeout(intf, &(intf->seq_table[i]),
4124 &timeouts, timeout_period, i,
4125 &flags, &waiting_msgs);
4126 spin_unlock_irqrestore(&intf->seq_lock, flags);
393d2cc3 4127
89986496
CM
4128 list_for_each_entry_safe(msg, msg2, &timeouts, link)
4129 deliver_err_response(msg, IPMI_TIMEOUT_COMPLETION_CODE);
b9675136 4130
89986496
CM
4131 /*
4132 * Maintenance mode handling. Check the timeout
4133 * optimistically before we claim the lock. It may
4134 * mean a timeout gets missed occasionally, but that
4135 * only means the timeout gets extended by one period
4136 * in that case. No big deal, and it avoids the lock
4137 * most of the time.
4138 */
4139 if (intf->auto_maintenance_timeout > 0) {
4140 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
b9675136 4141 if (intf->auto_maintenance_timeout > 0) {
89986496
CM
4142 intf->auto_maintenance_timeout
4143 -= timeout_period;
4144 if (!intf->maintenance_mode
4145 && (intf->auto_maintenance_timeout <= 0)) {
7aefac26 4146 intf->maintenance_mode_enable = false;
89986496 4147 maintenance_mode_update(intf);
b9675136 4148 }
b9675136 4149 }
89986496
CM
4150 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
4151 flags);
1da177e4 4152 }
89986496
CM
4153
4154 tasklet_schedule(&intf->recv_tasklet);
4155
4156 return waiting_msgs;
1da177e4
LT
4157}
4158
89986496 4159static void ipmi_request_event(ipmi_smi_t intf)
1da177e4 4160{
89986496
CM
4161 /* No event requests when in maintenance mode. */
4162 if (intf->maintenance_mode_enable)
4163 return;
b9675136 4164
7ea0ed2b
CM
4165 if (!intf->in_shutdown)
4166 intf->handlers->request_events(intf->send_info);
1da177e4
LT
4167}
4168
4169static struct timer_list ipmi_timer;
4170
8f43f84f 4171static atomic_t stop_operation;
1da177e4
LT
4172
4173static void ipmi_timeout(unsigned long data)
4174{
89986496
CM
4175 ipmi_smi_t intf;
4176 int nt = 0;
4177
8f43f84f 4178 if (atomic_read(&stop_operation))
1da177e4 4179 return;
1da177e4 4180
89986496
CM
4181 rcu_read_lock();
4182 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4183 int lnt = 0;
4184
4185 if (atomic_read(&intf->event_waiters)) {
4186 intf->ticks_to_req_ev--;
4187 if (intf->ticks_to_req_ev == 0) {
4188 ipmi_request_event(intf);
4189 intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
4190 }
4191 lnt++;
4192 }
4193
4194 lnt += ipmi_timeout_handler(intf, IPMI_TIMEOUT_TIME);
1da177e4 4195
89986496
CM
4196 lnt = !!lnt;
4197 if (lnt != intf->last_needs_timer &&
4198 intf->handlers->set_need_watch)
4199 intf->handlers->set_need_watch(intf->send_info, lnt);
4200 intf->last_needs_timer = lnt;
1da177e4 4201
89986496
CM
4202 nt += lnt;
4203 }
4204 rcu_read_unlock();
4205
4206 if (nt)
4207 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
1da177e4
LT
4208}
4209
89986496
CM
4210static void need_waiter(ipmi_smi_t intf)
4211{
4212 /* Racy, but worst case we start the timer twice. */
4213 if (!timer_pending(&ipmi_timer))
4214 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
4215}
1da177e4
LT
4216
4217static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
4218static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
4219
1da177e4
LT
4220static void free_smi_msg(struct ipmi_smi_msg *msg)
4221{
4222 atomic_dec(&smi_msg_inuse_count);
4223 kfree(msg);
4224}
4225
4226struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
4227{
4228 struct ipmi_smi_msg *rv;
4229 rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
4230 if (rv) {
4231 rv->done = free_smi_msg;
4232 rv->user_data = NULL;
4233 atomic_inc(&smi_msg_inuse_count);
4234 }
4235 return rv;
4236}
c70d7499 4237EXPORT_SYMBOL(ipmi_alloc_smi_msg);
1da177e4
LT
4238
4239static void free_recv_msg(struct ipmi_recv_msg *msg)
4240{
4241 atomic_dec(&recv_msg_inuse_count);
4242 kfree(msg);
4243}
4244
74006309 4245static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
1da177e4
LT
4246{
4247 struct ipmi_recv_msg *rv;
4248
4249 rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
4250 if (rv) {
a9eec556 4251 rv->user = NULL;
1da177e4
LT
4252 rv->done = free_recv_msg;
4253 atomic_inc(&recv_msg_inuse_count);
4254 }
4255 return rv;
4256}
4257
393d2cc3
CM
4258void ipmi_free_recv_msg(struct ipmi_recv_msg *msg)
4259{
4260 if (msg->user)
4261 kref_put(&msg->user->refcount, free_user);
4262 msg->done(msg);
4263}
c70d7499 4264EXPORT_SYMBOL(ipmi_free_recv_msg);
393d2cc3 4265
1da177e4
LT
4266#ifdef CONFIG_IPMI_PANIC_EVENT
4267
895dcfd1
CM
4268static atomic_t panic_done_count = ATOMIC_INIT(0);
4269
1da177e4
LT
4270static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
4271{
895dcfd1 4272 atomic_dec(&panic_done_count);
1da177e4
LT
4273}
4274
4275static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
4276{
895dcfd1
CM
4277 atomic_dec(&panic_done_count);
4278}
4279
4280/*
4281 * Inside a panic, send a message and wait for a response.
4282 */
4283static void ipmi_panic_request_and_wait(ipmi_smi_t intf,
4284 struct ipmi_addr *addr,
4285 struct kernel_ipmi_msg *msg)
4286{
4287 struct ipmi_smi_msg smi_msg;
4288 struct ipmi_recv_msg recv_msg;
4289 int rv;
4290
4291 smi_msg.done = dummy_smi_done_handler;
4292 recv_msg.done = dummy_recv_done_handler;
4293 atomic_add(2, &panic_done_count);
4294 rv = i_ipmi_request(NULL,
4295 intf,
4296 addr,
4297 0,
4298 msg,
4299 intf,
4300 &smi_msg,
4301 &recv_msg,
4302 0,
4303 intf->channels[0].address,
4304 intf->channels[0].lun,
4305 0, 1); /* Don't retry, and don't wait. */
4306 if (rv)
4307 atomic_sub(2, &panic_done_count);
82802f96
HK
4308 else if (intf->handlers->flush_messages)
4309 intf->handlers->flush_messages(intf->send_info);
4310
895dcfd1
CM
4311 while (atomic_read(&panic_done_count) != 0)
4312 ipmi_poll(intf);
1da177e4
LT
4313}
4314
4315#ifdef CONFIG_IPMI_PANIC_STRING
56a55ec6 4316static void event_receiver_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
1da177e4 4317{
56a55ec6
CM
4318 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4319 && (msg->msg.netfn == IPMI_NETFN_SENSOR_EVENT_RESPONSE)
4320 && (msg->msg.cmd == IPMI_GET_EVENT_RECEIVER_CMD)
c70d7499 4321 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
1da177e4 4322 /* A get event receiver command, save it. */
56a55ec6
CM
4323 intf->event_receiver = msg->msg.data[1];
4324 intf->event_receiver_lun = msg->msg.data[2] & 0x3;
1da177e4
LT
4325 }
4326}
4327
56a55ec6 4328static void device_id_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
1da177e4 4329{
56a55ec6
CM
4330 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4331 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
4332 && (msg->msg.cmd == IPMI_GET_DEVICE_ID_CMD)
c70d7499
CM
4333 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
4334 /*
4335 * A get device id command, save if we are an event
4336 * receiver or generator.
4337 */
56a55ec6
CM
4338 intf->local_sel_device = (msg->msg.data[6] >> 2) & 1;
4339 intf->local_event_generator = (msg->msg.data[6] >> 5) & 1;
1da177e4
LT
4340 }
4341}
4342#endif
4343
4344static void send_panic_events(char *str)
4345{
4346 struct kernel_ipmi_msg msg;
4347 ipmi_smi_t intf;
4348 unsigned char data[16];
1da177e4
LT
4349 struct ipmi_system_interface_addr *si;
4350 struct ipmi_addr addr;
1da177e4
LT
4351
4352 si = (struct ipmi_system_interface_addr *) &addr;
4353 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4354 si->channel = IPMI_BMC_CHANNEL;
4355 si->lun = 0;
4356
4357 /* Fill in an event telling that we have failed. */
4358 msg.netfn = 0x04; /* Sensor or Event. */
4359 msg.cmd = 2; /* Platform event command. */
4360 msg.data = data;
4361 msg.data_len = 8;
cda315ab 4362 data[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */
1da177e4
LT
4363 data[1] = 0x03; /* This is for IPMI 1.0. */
4364 data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
4365 data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
4366 data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
4367
c70d7499
CM
4368 /*
4369 * Put a few breadcrumbs in. Hopefully later we can add more things
4370 * to make the panic events more useful.
4371 */
1da177e4
LT
4372 if (str) {
4373 data[3] = str[0];
4374 data[6] = str[1];
4375 data[7] = str[2];
4376 }
4377
1da177e4 4378 /* For every registered interface, send the event. */
bca0324d 4379 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
b2c03941
CM
4380 if (!intf->handlers)
4381 /* Interface is not ready. */
1da177e4
LT
4382 continue;
4383
4384 /* Send the event announcing the panic. */
895dcfd1 4385 ipmi_panic_request_and_wait(intf, &addr, &msg);
1da177e4
LT
4386 }
4387
4388#ifdef CONFIG_IPMI_PANIC_STRING
c70d7499
CM
4389 /*
4390 * On every interface, dump a bunch of OEM event holding the
4391 * string.
4392 */
4393 if (!str)
1da177e4
LT
4394 return;
4395
bca0324d
CM
4396 /* For every registered interface, send the event. */
4397 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
1da177e4
LT
4398 char *p = str;
4399 struct ipmi_ipmb_addr *ipmb;
4400 int j;
4401
bca0324d
CM
4402 if (intf->intf_num == -1)
4403 /* Interface was not ready yet. */
1da177e4
LT
4404 continue;
4405
78ba2faf
CM
4406 /*
4407 * intf_num is used as an marker to tell if the
4408 * interface is valid. Thus we need a read barrier to
4409 * make sure data fetched before checking intf_num
4410 * won't be used.
4411 */
4412 smp_rmb();
4413
c70d7499
CM
4414 /*
4415 * First job here is to figure out where to send the
4416 * OEM events. There's no way in IPMI to send OEM
4417 * events using an event send command, so we have to
4418 * find the SEL to put them in and stick them in
4419 * there.
4420 */
1da177e4
LT
4421
4422 /* Get capabilities from the get device id. */
4423 intf->local_sel_device = 0;
4424 intf->local_event_generator = 0;
4425 intf->event_receiver = 0;
4426
4427 /* Request the device info from the local MC. */
4428 msg.netfn = IPMI_NETFN_APP_REQUEST;
4429 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
4430 msg.data = NULL;
4431 msg.data_len = 0;
4432 intf->null_user_handler = device_id_fetcher;
895dcfd1 4433 ipmi_panic_request_and_wait(intf, &addr, &msg);
1da177e4
LT
4434
4435 if (intf->local_event_generator) {
4436 /* Request the event receiver from the local MC. */
4437 msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
4438 msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
4439 msg.data = NULL;
4440 msg.data_len = 0;
4441 intf->null_user_handler = event_receiver_fetcher;
895dcfd1 4442 ipmi_panic_request_and_wait(intf, &addr, &msg);
1da177e4
LT
4443 }
4444 intf->null_user_handler = NULL;
4445
c70d7499
CM
4446 /*
4447 * Validate the event receiver. The low bit must not
4448 * be 1 (it must be a valid IPMB address), it cannot
4449 * be zero, and it must not be my address.
4450 */
4451 if (((intf->event_receiver & 1) == 0)
1da177e4 4452 && (intf->event_receiver != 0)
c70d7499
CM
4453 && (intf->event_receiver != intf->channels[0].address)) {
4454 /*
4455 * The event receiver is valid, send an IPMB
4456 * message.
4457 */
1da177e4
LT
4458 ipmb = (struct ipmi_ipmb_addr *) &addr;
4459 ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
4460 ipmb->channel = 0; /* FIXME - is this right? */
4461 ipmb->lun = intf->event_receiver_lun;
4462 ipmb->slave_addr = intf->event_receiver;
4463 } else if (intf->local_sel_device) {
c70d7499
CM
4464 /*
4465 * The event receiver was not valid (or was
4466 * me), but I am an SEL device, just dump it
4467 * in my SEL.
4468 */
1da177e4
LT
4469 si = (struct ipmi_system_interface_addr *) &addr;
4470 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4471 si->channel = IPMI_BMC_CHANNEL;
4472 si->lun = 0;
4473 } else
4474 continue; /* No where to send the event. */
4475
1da177e4
LT
4476 msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
4477 msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
4478 msg.data = data;
4479 msg.data_len = 16;
4480
4481 j = 0;
4482 while (*p) {
4483 int size = strlen(p);
4484
4485 if (size > 11)
4486 size = 11;
4487 data[0] = 0;
4488 data[1] = 0;
4489 data[2] = 0xf0; /* OEM event without timestamp. */
c14979b9 4490 data[3] = intf->channels[0].address;
1da177e4 4491 data[4] = j++; /* sequence # */
c70d7499
CM
4492 /*
4493 * Always give 11 bytes, so strncpy will fill
4494 * it with zeroes for me.
4495 */
1da177e4
LT
4496 strncpy(data+5, p, 11);
4497 p += size;
4498
895dcfd1 4499 ipmi_panic_request_and_wait(intf, &addr, &msg);
1da177e4 4500 }
c70d7499 4501 }
1da177e4
LT
4502#endif /* CONFIG_IPMI_PANIC_STRING */
4503}
4504#endif /* CONFIG_IPMI_PANIC_EVENT */
4505
0c8204b3 4506static int has_panicked;
1da177e4
LT
4507
4508static int panic_event(struct notifier_block *this,
4509 unsigned long event,
c70d7499 4510 void *ptr)
1da177e4 4511{
1da177e4
LT
4512 ipmi_smi_t intf;
4513
f18190bd 4514 if (has_panicked)
1da177e4 4515 return NOTIFY_DONE;
f18190bd 4516 has_panicked = 1;
1da177e4
LT
4517
4518 /* For every registered interface, set it to run to completion. */
bca0324d 4519 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
b2c03941
CM
4520 if (!intf->handlers)
4521 /* Interface is not ready. */
1da177e4
LT
4522 continue;
4523
06e5e345
HK
4524 /*
4525 * If we were interrupted while locking xmit_msgs_lock or
4526 * waiting_rcv_msgs_lock, the corresponding list may be
4527 * corrupted. In this case, drop items on the list for
4528 * the safety.
4529 */
4530 if (!spin_trylock(&intf->xmit_msgs_lock)) {
4531 INIT_LIST_HEAD(&intf->xmit_msgs);
4532 INIT_LIST_HEAD(&intf->hp_xmit_msgs);
4533 } else
4534 spin_unlock(&intf->xmit_msgs_lock);
4535
4536 if (!spin_trylock(&intf->waiting_rcv_msgs_lock))
4537 INIT_LIST_HEAD(&intf->waiting_rcv_msgs);
4538 else
4539 spin_unlock(&intf->waiting_rcv_msgs_lock);
4540
5956dce1 4541 intf->run_to_completion = 1;
1da177e4
LT
4542 intf->handlers->set_run_to_completion(intf->send_info, 1);
4543 }
4544
4545#ifdef CONFIG_IPMI_PANIC_EVENT
4546 send_panic_events(ptr);
4547#endif
4548
4549 return NOTIFY_DONE;
4550}
4551
4552static struct notifier_block panic_block = {
4553 .notifier_call = panic_event,
4554 .next = NULL,
4555 .priority = 200 /* priority: INT_MAX >= x >= 0 */
4556};
4557
4558static int ipmi_init_msghandler(void)
4559{
50c812b2 4560 int rv;
1da177e4
LT
4561
4562 if (initialized)
4563 return 0;
4564
fe2d5ffc 4565 rv = driver_register(&ipmidriver.driver);
50c812b2
CM
4566 if (rv) {
4567 printk(KERN_ERR PFX "Could not register IPMI driver\n");
4568 return rv;
4569 }
4570
1da177e4 4571 printk(KERN_INFO "ipmi message handler version "
1fdd75bd 4572 IPMI_DRIVER_VERSION "\n");
1da177e4 4573
3b625943 4574#ifdef CONFIG_PROC_FS
1da177e4
LT
4575 proc_ipmi_root = proc_mkdir("ipmi", NULL);
4576 if (!proc_ipmi_root) {
4577 printk(KERN_ERR PFX "Unable to create IPMI proc dir");
80fad5b9 4578 driver_unregister(&ipmidriver.driver);
1da177e4
LT
4579 return -ENOMEM;
4580 }
4581
3b625943 4582#endif /* CONFIG_PROC_FS */
1da177e4 4583
409035e0
CM
4584 setup_timer(&ipmi_timer, ipmi_timeout, 0);
4585 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
1da177e4 4586
e041c683 4587 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
1da177e4
LT
4588
4589 initialized = 1;
4590
4591 return 0;
4592}
4593
60ee6d5f 4594static int __init ipmi_init_msghandler_mod(void)
1da177e4
LT
4595{
4596 ipmi_init_msghandler();
4597 return 0;
4598}
4599
60ee6d5f 4600static void __exit cleanup_ipmi(void)
1da177e4
LT
4601{
4602 int count;
4603
4604 if (!initialized)
4605 return;
4606
e041c683 4607 atomic_notifier_chain_unregister(&panic_notifier_list, &panic_block);
1da177e4 4608
c70d7499
CM
4609 /*
4610 * This can't be called if any interfaces exist, so no worry
4611 * about shutting down the interfaces.
4612 */
1da177e4 4613
c70d7499
CM
4614 /*
4615 * Tell the timer to stop, then wait for it to stop. This
4616 * avoids problems with race conditions removing the timer
4617 * here.
4618 */
8f43f84f
CM
4619 atomic_inc(&stop_operation);
4620 del_timer_sync(&ipmi_timer);
1da177e4 4621
3b625943 4622#ifdef CONFIG_PROC_FS
a8ca16ea 4623 proc_remove(proc_ipmi_root);
3b625943 4624#endif /* CONFIG_PROC_FS */
1da177e4 4625
fe2d5ffc 4626 driver_unregister(&ipmidriver.driver);
50c812b2 4627
1da177e4
LT
4628 initialized = 0;
4629
4630 /* Check for buffer leaks. */
4631 count = atomic_read(&smi_msg_inuse_count);
4632 if (count != 0)
4633 printk(KERN_WARNING PFX "SMI message count %d at exit\n",
4634 count);
4635 count = atomic_read(&recv_msg_inuse_count);
4636 if (count != 0)
4637 printk(KERN_WARNING PFX "recv message count %d at exit\n",
4638 count);
4639}
4640module_exit(cleanup_ipmi);
4641
4642module_init(ipmi_init_msghandler_mod);
4643MODULE_LICENSE("GPL");
1fdd75bd 4644MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
c70d7499
CM
4645MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI"
4646 " interface.");
1fdd75bd 4647MODULE_VERSION(IPMI_DRIVER_VERSION);