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