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