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