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