]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/char/ipmi/ipmi_msghandler.c
[PATCH] ipmi: style cleanups
[mirror_ubuntu-bionic-kernel.git] / drivers / char / ipmi / ipmi_msghandler.c
CommitLineData
1da177e4
LT
1/*
2 * ipmi_msghandler.c
3 *
4 * Incoming and outgoing message routing for an IPMI interface.
5 *
6 * Author: MontaVista Software, Inc.
7 * Corey Minyard <minyard@mvista.com>
8 * source@mvista.com
9 *
10 * Copyright 2002 MontaVista Software Inc.
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 *
17 *
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
26 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
27 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * You should have received a copy of the GNU General Public License along
30 * with this program; if not, write to the Free Software Foundation, Inc.,
31 * 675 Mass Ave, Cambridge, MA 02139, USA.
32 */
33
34#include <linux/config.h>
35#include <linux/module.h>
36#include <linux/errno.h>
37#include <asm/system.h>
38#include <linux/sched.h>
39#include <linux/poll.h>
40#include <linux/spinlock.h>
41#include <linux/rwsem.h>
42#include <linux/slab.h>
43#include <linux/ipmi.h>
44#include <linux/ipmi_smi.h>
45#include <linux/notifier.h>
46#include <linux/init.h>
47#include <linux/proc_fs.h>
48
49#define PFX "IPMI message handler: "
1fdd75bd
CM
50
51#define IPMI_DRIVER_VERSION "36.0"
1da177e4
LT
52
53static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void);
54static int ipmi_init_msghandler(void);
55
56static int initialized = 0;
57
3b625943
CM
58#ifdef CONFIG_PROC_FS
59struct proc_dir_entry *proc_ipmi_root = NULL;
60#endif /* CONFIG_PROC_FS */
1da177e4
LT
61
62#define MAX_EVENTS_IN_QUEUE 25
63
64/* Don't let a message sit in a queue forever, always time it with at lest
65 the max message timer. This is in milliseconds. */
66#define MAX_MSG_TIMEOUT 60000
67
68struct ipmi_user
69{
70 struct list_head link;
71
72 /* The upper layer that handles receive messages. */
73 struct ipmi_user_hndl *handler;
74 void *handler_data;
75
76 /* The interface this user is bound to. */
77 ipmi_smi_t intf;
78
79 /* Does this interface receive IPMI events? */
80 int gets_events;
81};
82
83struct cmd_rcvr
84{
85 struct list_head link;
86
87 ipmi_user_t user;
88 unsigned char netfn;
89 unsigned char cmd;
90};
91
92struct seq_table
93{
94 unsigned int inuse : 1;
95 unsigned int broadcast : 1;
96
97 unsigned long timeout;
98 unsigned long orig_timeout;
99 unsigned int retries_left;
100
101 /* To verify on an incoming send message response that this is
102 the message that the response is for, we keep a sequence id
103 and increment it every time we send a message. */
104 long seqid;
105
106 /* This is held so we can properly respond to the message on a
107 timeout, and it is used to hold the temporary data for
108 retransmission, too. */
109 struct ipmi_recv_msg *recv_msg;
110};
111
112/* Store the information in a msgid (long) to allow us to find a
113 sequence table entry from the msgid. */
114#define STORE_SEQ_IN_MSGID(seq, seqid) (((seq&0xff)<<26) | (seqid&0x3ffffff))
115
116#define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
117 do { \
118 seq = ((msgid >> 26) & 0x3f); \
119 seqid = (msgid & 0x3fffff); \
e8b33617 120 } while (0)
1da177e4
LT
121
122#define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3fffff)
123
124struct ipmi_channel
125{
126 unsigned char medium;
127 unsigned char protocol;
c14979b9
CM
128
129 /* My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR,
130 but may be changed by the user. */
131 unsigned char address;
132
133 /* My LUN. This should generally stay the SMS LUN, but just in
134 case... */
135 unsigned char lun;
1da177e4
LT
136};
137
3b625943 138#ifdef CONFIG_PROC_FS
1da177e4
LT
139struct ipmi_proc_entry
140{
141 char *name;
142 struct ipmi_proc_entry *next;
143};
3b625943 144#endif
1da177e4
LT
145
146#define IPMI_IPMB_NUM_SEQ 64
c14979b9 147#define IPMI_MAX_CHANNELS 16
1da177e4
LT
148struct ipmi_smi
149{
150 /* What interface number are we? */
151 int intf_num;
152
153 /* The list of upper layers that are using me. We read-lock
154 this when delivering messages to the upper layer to keep
155 the user from going away while we are processing the
156 message. This means that you cannot add or delete a user
157 from the receive callback. */
158 rwlock_t users_lock;
159 struct list_head users;
160
161 /* Used for wake ups at startup. */
162 wait_queue_head_t waitq;
163
164 /* The IPMI version of the BMC on the other end. */
165 unsigned char version_major;
166 unsigned char version_minor;
167
168 /* This is the lower-layer's sender routine. */
169 struct ipmi_smi_handlers *handlers;
170 void *send_info;
171
3b625943 172#ifdef CONFIG_PROC_FS
1da177e4
LT
173 /* A list of proc entries for this interface. This does not
174 need a lock, only one thread creates it and only one thread
175 destroys it. */
3b625943 176 spinlock_t proc_entry_lock;
1da177e4 177 struct ipmi_proc_entry *proc_entries;
3b625943 178#endif
1da177e4
LT
179
180 /* A table of sequence numbers for this interface. We use the
181 sequence numbers for IPMB messages that go out of the
182 interface to match them up with their responses. A routine
183 is called periodically to time the items in this list. */
184 spinlock_t seq_lock;
185 struct seq_table seq_table[IPMI_IPMB_NUM_SEQ];
186 int curr_seq;
187
188 /* Messages that were delayed for some reason (out of memory,
189 for instance), will go in here to be processed later in a
190 periodic timer interrupt. */
191 spinlock_t waiting_msgs_lock;
192 struct list_head waiting_msgs;
193
194 /* The list of command receivers that are registered for commands
195 on this interface. */
196 rwlock_t cmd_rcvr_lock;
197 struct list_head cmd_rcvrs;
198
199 /* Events that were queues because no one was there to receive
200 them. */
201 spinlock_t events_lock; /* For dealing with event stuff. */
202 struct list_head waiting_events;
203 unsigned int waiting_events_count; /* How many events in queue? */
204
205 /* This will be non-null if someone registers to receive all
206 IPMI commands (this is for interface emulation). There
207 may not be any things in the cmd_rcvrs list above when
208 this is registered. */
209 ipmi_user_t all_cmd_rcvr;
210
1da177e4
LT
211 /* The event receiver for my BMC, only really used at panic
212 shutdown as a place to store this. */
213 unsigned char event_receiver;
214 unsigned char event_receiver_lun;
215 unsigned char local_sel_device;
216 unsigned char local_event_generator;
217
218 /* A cheap hack, if this is non-null and a message to an
219 interface comes in with a NULL user, call this routine with
220 it. Note that the message will still be freed by the
221 caller. This only works on the system interface. */
56a55ec6 222 void (*null_user_handler)(ipmi_smi_t intf, struct ipmi_recv_msg *msg);
1da177e4
LT
223
224 /* When we are scanning the channels for an SMI, this will
225 tell which channel we are scanning. */
226 int curr_channel;
227
228 /* Channel information */
229 struct ipmi_channel channels[IPMI_MAX_CHANNELS];
230
231 /* Proc FS stuff. */
232 struct proc_dir_entry *proc_dir;
233 char proc_dir_name[10];
234
235 spinlock_t counter_lock; /* For making counters atomic. */
236
237 /* Commands we got that were invalid. */
238 unsigned int sent_invalid_commands;
239
240 /* Commands we sent to the MC. */
241 unsigned int sent_local_commands;
242 /* Responses from the MC that were delivered to a user. */
243 unsigned int handled_local_responses;
244 /* Responses from the MC that were not delivered to a user. */
245 unsigned int unhandled_local_responses;
246
247 /* Commands we sent out to the IPMB bus. */
248 unsigned int sent_ipmb_commands;
249 /* Commands sent on the IPMB that had errors on the SEND CMD */
250 unsigned int sent_ipmb_command_errs;
251 /* Each retransmit increments this count. */
252 unsigned int retransmitted_ipmb_commands;
253 /* When a message times out (runs out of retransmits) this is
254 incremented. */
255 unsigned int timed_out_ipmb_commands;
256
257 /* This is like above, but for broadcasts. Broadcasts are
258 *not* included in the above count (they are expected to
259 time out). */
260 unsigned int timed_out_ipmb_broadcasts;
261
262 /* Responses I have sent to the IPMB bus. */
263 unsigned int sent_ipmb_responses;
264
265 /* The response was delivered to the user. */
266 unsigned int handled_ipmb_responses;
267 /* The response had invalid data in it. */
268 unsigned int invalid_ipmb_responses;
269 /* The response didn't have anyone waiting for it. */
270 unsigned int unhandled_ipmb_responses;
271
272 /* Commands we sent out to the IPMB bus. */
273 unsigned int sent_lan_commands;
274 /* Commands sent on the IPMB that had errors on the SEND CMD */
275 unsigned int sent_lan_command_errs;
276 /* Each retransmit increments this count. */
277 unsigned int retransmitted_lan_commands;
278 /* When a message times out (runs out of retransmits) this is
279 incremented. */
280 unsigned int timed_out_lan_commands;
281
282 /* Responses I have sent to the IPMB bus. */
283 unsigned int sent_lan_responses;
284
285 /* The response was delivered to the user. */
286 unsigned int handled_lan_responses;
287 /* The response had invalid data in it. */
288 unsigned int invalid_lan_responses;
289 /* The response didn't have anyone waiting for it. */
290 unsigned int unhandled_lan_responses;
291
292 /* The command was delivered to the user. */
293 unsigned int handled_commands;
294 /* The command had invalid data in it. */
295 unsigned int invalid_commands;
296 /* The command didn't have anyone waiting for it. */
297 unsigned int unhandled_commands;
298
299 /* Invalid data in an event. */
300 unsigned int invalid_events;
301 /* Events that were received with the proper format. */
302 unsigned int events;
303};
304
305#define MAX_IPMI_INTERFACES 4
306static ipmi_smi_t ipmi_interfaces[MAX_IPMI_INTERFACES];
307
308/* Used to keep interfaces from going away while operations are
309 operating on interfaces. Grab read if you are not modifying the
310 interfaces, write if you are. */
311static DECLARE_RWSEM(interfaces_sem);
312
313/* Directly protects the ipmi_interfaces data structure. This is
314 claimed in the timer interrupt. */
315static DEFINE_SPINLOCK(interfaces_lock);
316
317/* List of watchers that want to know when smi's are added and
318 deleted. */
319static struct list_head smi_watchers = LIST_HEAD_INIT(smi_watchers);
320static DECLARE_RWSEM(smi_watchers_sem);
321
322int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher)
323{
324 int i;
325
326 down_read(&interfaces_sem);
327 down_write(&smi_watchers_sem);
328 list_add(&(watcher->link), &smi_watchers);
e8b33617 329 for (i = 0; i < MAX_IPMI_INTERFACES; i++) {
1da177e4
LT
330 if (ipmi_interfaces[i] != NULL) {
331 watcher->new_smi(i);
332 }
333 }
334 up_write(&smi_watchers_sem);
335 up_read(&interfaces_sem);
336 return 0;
337}
338
339int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher)
340{
341 down_write(&smi_watchers_sem);
342 list_del(&(watcher->link));
343 up_write(&smi_watchers_sem);
344 return 0;
345}
346
347static void
348call_smi_watchers(int i)
349{
350 struct ipmi_smi_watcher *w;
351
352 down_read(&smi_watchers_sem);
353 list_for_each_entry(w, &smi_watchers, link) {
354 if (try_module_get(w->owner)) {
355 w->new_smi(i);
356 module_put(w->owner);
357 }
358 }
359 up_read(&smi_watchers_sem);
360}
361
362static int
363ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2)
364{
365 if (addr1->addr_type != addr2->addr_type)
366 return 0;
367
368 if (addr1->channel != addr2->channel)
369 return 0;
370
371 if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
372 struct ipmi_system_interface_addr *smi_addr1
373 = (struct ipmi_system_interface_addr *) addr1;
374 struct ipmi_system_interface_addr *smi_addr2
375 = (struct ipmi_system_interface_addr *) addr2;
376 return (smi_addr1->lun == smi_addr2->lun);
377 }
378
379 if ((addr1->addr_type == IPMI_IPMB_ADDR_TYPE)
380 || (addr1->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
381 {
382 struct ipmi_ipmb_addr *ipmb_addr1
383 = (struct ipmi_ipmb_addr *) addr1;
384 struct ipmi_ipmb_addr *ipmb_addr2
385 = (struct ipmi_ipmb_addr *) addr2;
386
387 return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr)
388 && (ipmb_addr1->lun == ipmb_addr2->lun));
389 }
390
391 if (addr1->addr_type == IPMI_LAN_ADDR_TYPE) {
392 struct ipmi_lan_addr *lan_addr1
393 = (struct ipmi_lan_addr *) addr1;
394 struct ipmi_lan_addr *lan_addr2
395 = (struct ipmi_lan_addr *) addr2;
396
397 return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID)
398 && (lan_addr1->local_SWID == lan_addr2->local_SWID)
399 && (lan_addr1->session_handle
400 == lan_addr2->session_handle)
401 && (lan_addr1->lun == lan_addr2->lun));
402 }
403
404 return 1;
405}
406
407int ipmi_validate_addr(struct ipmi_addr *addr, int len)
408{
409 if (len < sizeof(struct ipmi_system_interface_addr)) {
410 return -EINVAL;
411 }
412
413 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
414 if (addr->channel != IPMI_BMC_CHANNEL)
415 return -EINVAL;
416 return 0;
417 }
418
419 if ((addr->channel == IPMI_BMC_CHANNEL)
420 || (addr->channel >= IPMI_NUM_CHANNELS)
421 || (addr->channel < 0))
422 return -EINVAL;
423
424 if ((addr->addr_type == IPMI_IPMB_ADDR_TYPE)
425 || (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
426 {
427 if (len < sizeof(struct ipmi_ipmb_addr)) {
428 return -EINVAL;
429 }
430 return 0;
431 }
432
433 if (addr->addr_type == IPMI_LAN_ADDR_TYPE) {
434 if (len < sizeof(struct ipmi_lan_addr)) {
435 return -EINVAL;
436 }
437 return 0;
438 }
439
440 return -EINVAL;
441}
442
443unsigned int ipmi_addr_length(int addr_type)
444{
445 if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
446 return sizeof(struct ipmi_system_interface_addr);
447
448 if ((addr_type == IPMI_IPMB_ADDR_TYPE)
449 || (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
450 {
451 return sizeof(struct ipmi_ipmb_addr);
452 }
453
454 if (addr_type == IPMI_LAN_ADDR_TYPE)
455 return sizeof(struct ipmi_lan_addr);
456
457 return 0;
458}
459
460static void deliver_response(struct ipmi_recv_msg *msg)
461{
56a55ec6
CM
462 if (! msg->user) {
463 ipmi_smi_t intf = msg->user_msg_data;
464 unsigned long flags;
465
466 /* Special handling for NULL users. */
467 if (intf->null_user_handler) {
468 intf->null_user_handler(intf, msg);
469 spin_lock_irqsave(&intf->counter_lock, flags);
470 intf->handled_local_responses++;
471 spin_unlock_irqrestore(&intf->counter_lock, flags);
472 } else {
473 /* No handler, so give up. */
474 spin_lock_irqsave(&intf->counter_lock, flags);
475 intf->unhandled_local_responses++;
476 spin_unlock_irqrestore(&intf->counter_lock, flags);
477 }
478 ipmi_free_recv_msg(msg);
479 } else {
480 msg->user->handler->ipmi_recv_hndl(msg,
481 msg->user->handler_data);
482 }
1da177e4
LT
483}
484
485/* Find the next sequence number not being used and add the given
486 message with the given timeout to the sequence table. This must be
487 called with the interface's seq_lock held. */
488static int intf_next_seq(ipmi_smi_t intf,
489 struct ipmi_recv_msg *recv_msg,
490 unsigned long timeout,
491 int retries,
492 int broadcast,
493 unsigned char *seq,
494 long *seqid)
495{
496 int rv = 0;
497 unsigned int i;
498
e8b33617 499 for (i = intf->curr_seq;
1da177e4 500 (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq;
e8b33617 501 i = (i+1)%IPMI_IPMB_NUM_SEQ)
1da177e4
LT
502 {
503 if (! intf->seq_table[i].inuse)
504 break;
505 }
506
507 if (! intf->seq_table[i].inuse) {
508 intf->seq_table[i].recv_msg = recv_msg;
509
510 /* Start with the maximum timeout, when the send response
511 comes in we will start the real timer. */
512 intf->seq_table[i].timeout = MAX_MSG_TIMEOUT;
513 intf->seq_table[i].orig_timeout = timeout;
514 intf->seq_table[i].retries_left = retries;
515 intf->seq_table[i].broadcast = broadcast;
516 intf->seq_table[i].inuse = 1;
517 intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid);
518 *seq = i;
519 *seqid = intf->seq_table[i].seqid;
520 intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ;
521 } else {
522 rv = -EAGAIN;
523 }
524
525 return rv;
526}
527
528/* Return the receive message for the given sequence number and
529 release the sequence number so it can be reused. Some other data
530 is passed in to be sure the message matches up correctly (to help
531 guard against message coming in after their timeout and the
532 sequence number being reused). */
533static int intf_find_seq(ipmi_smi_t intf,
534 unsigned char seq,
535 short channel,
536 unsigned char cmd,
537 unsigned char netfn,
538 struct ipmi_addr *addr,
539 struct ipmi_recv_msg **recv_msg)
540{
541 int rv = -ENODEV;
542 unsigned long flags;
543
544 if (seq >= IPMI_IPMB_NUM_SEQ)
545 return -EINVAL;
546
547 spin_lock_irqsave(&(intf->seq_lock), flags);
548 if (intf->seq_table[seq].inuse) {
549 struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg;
550
551 if ((msg->addr.channel == channel)
552 && (msg->msg.cmd == cmd)
553 && (msg->msg.netfn == netfn)
554 && (ipmi_addr_equal(addr, &(msg->addr))))
555 {
556 *recv_msg = msg;
557 intf->seq_table[seq].inuse = 0;
558 rv = 0;
559 }
560 }
561 spin_unlock_irqrestore(&(intf->seq_lock), flags);
562
563 return rv;
564}
565
566
567/* Start the timer for a specific sequence table entry. */
568static int intf_start_seq_timer(ipmi_smi_t intf,
569 long msgid)
570{
571 int rv = -ENODEV;
572 unsigned long flags;
573 unsigned char seq;
574 unsigned long seqid;
575
576
577 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
578
579 spin_lock_irqsave(&(intf->seq_lock), flags);
580 /* We do this verification because the user can be deleted
581 while a message is outstanding. */
582 if ((intf->seq_table[seq].inuse)
583 && (intf->seq_table[seq].seqid == seqid))
584 {
585 struct seq_table *ent = &(intf->seq_table[seq]);
586 ent->timeout = ent->orig_timeout;
587 rv = 0;
588 }
589 spin_unlock_irqrestore(&(intf->seq_lock), flags);
590
591 return rv;
592}
593
594/* Got an error for the send message for a specific sequence number. */
595static int intf_err_seq(ipmi_smi_t intf,
596 long msgid,
597 unsigned int err)
598{
599 int rv = -ENODEV;
600 unsigned long flags;
601 unsigned char seq;
602 unsigned long seqid;
603 struct ipmi_recv_msg *msg = NULL;
604
605
606 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
607
608 spin_lock_irqsave(&(intf->seq_lock), flags);
609 /* We do this verification because the user can be deleted
610 while a message is outstanding. */
611 if ((intf->seq_table[seq].inuse)
612 && (intf->seq_table[seq].seqid == seqid))
613 {
614 struct seq_table *ent = &(intf->seq_table[seq]);
615
616 ent->inuse = 0;
617 msg = ent->recv_msg;
618 rv = 0;
619 }
620 spin_unlock_irqrestore(&(intf->seq_lock), flags);
621
622 if (msg) {
623 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
624 msg->msg_data[0] = err;
625 msg->msg.netfn |= 1; /* Convert to a response. */
626 msg->msg.data_len = 1;
627 msg->msg.data = msg->msg_data;
628 deliver_response(msg);
629 }
630
631 return rv;
632}
633
634
635int ipmi_create_user(unsigned int if_num,
636 struct ipmi_user_hndl *handler,
637 void *handler_data,
638 ipmi_user_t *user)
639{
640 unsigned long flags;
641 ipmi_user_t new_user;
642 int rv = 0;
643 ipmi_smi_t intf;
644
645 /* There is no module usecount here, because it's not
646 required. Since this can only be used by and called from
647 other modules, they will implicitly use this module, and
648 thus this can't be removed unless the other modules are
649 removed. */
650
651 if (handler == NULL)
652 return -EINVAL;
653
654 /* Make sure the driver is actually initialized, this handles
655 problems with initialization order. */
656 if (!initialized) {
657 rv = ipmi_init_msghandler();
658 if (rv)
659 return rv;
660
661 /* The init code doesn't return an error if it was turned
662 off, but it won't initialize. Check that. */
663 if (!initialized)
664 return -ENODEV;
665 }
666
667 new_user = kmalloc(sizeof(*new_user), GFP_KERNEL);
668 if (! new_user)
669 return -ENOMEM;
670
671 down_read(&interfaces_sem);
3a845099 672 if ((if_num >= MAX_IPMI_INTERFACES) || ipmi_interfaces[if_num] == NULL)
1da177e4
LT
673 {
674 rv = -EINVAL;
675 goto out_unlock;
676 }
677
678 intf = ipmi_interfaces[if_num];
679
680 new_user->handler = handler;
681 new_user->handler_data = handler_data;
682 new_user->intf = intf;
683 new_user->gets_events = 0;
684
685 if (!try_module_get(intf->handlers->owner)) {
686 rv = -ENODEV;
687 goto out_unlock;
688 }
689
690 if (intf->handlers->inc_usecount) {
691 rv = intf->handlers->inc_usecount(intf->send_info);
692 if (rv) {
693 module_put(intf->handlers->owner);
694 goto out_unlock;
695 }
696 }
697
698 write_lock_irqsave(&intf->users_lock, flags);
699 list_add_tail(&new_user->link, &intf->users);
700 write_unlock_irqrestore(&intf->users_lock, flags);
701
702 out_unlock:
703 if (rv) {
704 kfree(new_user);
705 } else {
706 *user = new_user;
707 }
708
709 up_read(&interfaces_sem);
710 return rv;
711}
712
713static int ipmi_destroy_user_nolock(ipmi_user_t user)
714{
715 int rv = -ENODEV;
716 ipmi_user_t t_user;
717 struct cmd_rcvr *rcvr, *rcvr2;
718 int i;
719 unsigned long flags;
720
721 /* Find the user and delete them from the list. */
722 list_for_each_entry(t_user, &(user->intf->users), link) {
723 if (t_user == user) {
724 list_del(&t_user->link);
725 rv = 0;
726 break;
727 }
728 }
729
730 if (rv) {
731 goto out_unlock;
732 }
733
734 /* Remove the user from the interfaces sequence table. */
735 spin_lock_irqsave(&(user->intf->seq_lock), flags);
e8b33617 736 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
1da177e4
LT
737 if (user->intf->seq_table[i].inuse
738 && (user->intf->seq_table[i].recv_msg->user == user))
739 {
740 user->intf->seq_table[i].inuse = 0;
741 }
742 }
743 spin_unlock_irqrestore(&(user->intf->seq_lock), flags);
744
745 /* Remove the user from the command receiver's table. */
746 write_lock_irqsave(&(user->intf->cmd_rcvr_lock), flags);
747 list_for_each_entry_safe(rcvr, rcvr2, &(user->intf->cmd_rcvrs), link) {
748 if (rcvr->user == user) {
749 list_del(&rcvr->link);
750 kfree(rcvr);
751 }
752 }
753 write_unlock_irqrestore(&(user->intf->cmd_rcvr_lock), flags);
754
755 kfree(user);
756
757 out_unlock:
758
759 return rv;
760}
761
762int ipmi_destroy_user(ipmi_user_t user)
763{
764 int rv;
765 ipmi_smi_t intf = user->intf;
766 unsigned long flags;
767
768 down_read(&interfaces_sem);
769 write_lock_irqsave(&intf->users_lock, flags);
770 rv = ipmi_destroy_user_nolock(user);
771 if (!rv) {
772 module_put(intf->handlers->owner);
773 if (intf->handlers->dec_usecount)
774 intf->handlers->dec_usecount(intf->send_info);
775 }
776
777 write_unlock_irqrestore(&intf->users_lock, flags);
778 up_read(&interfaces_sem);
779 return rv;
780}
781
782void ipmi_get_version(ipmi_user_t user,
783 unsigned char *major,
784 unsigned char *minor)
785{
786 *major = user->intf->version_major;
787 *minor = user->intf->version_minor;
788}
789
c14979b9
CM
790int ipmi_set_my_address(ipmi_user_t user,
791 unsigned int channel,
792 unsigned char address)
1da177e4 793{
c14979b9
CM
794 if (channel >= IPMI_MAX_CHANNELS)
795 return -EINVAL;
796 user->intf->channels[channel].address = address;
797 return 0;
1da177e4
LT
798}
799
c14979b9
CM
800int ipmi_get_my_address(ipmi_user_t user,
801 unsigned int channel,
802 unsigned char *address)
1da177e4 803{
c14979b9
CM
804 if (channel >= IPMI_MAX_CHANNELS)
805 return -EINVAL;
806 *address = user->intf->channels[channel].address;
807 return 0;
1da177e4
LT
808}
809
c14979b9
CM
810int ipmi_set_my_LUN(ipmi_user_t user,
811 unsigned int channel,
812 unsigned char LUN)
1da177e4 813{
c14979b9
CM
814 if (channel >= IPMI_MAX_CHANNELS)
815 return -EINVAL;
816 user->intf->channels[channel].lun = LUN & 0x3;
817 return 0;
1da177e4
LT
818}
819
c14979b9
CM
820int ipmi_get_my_LUN(ipmi_user_t user,
821 unsigned int channel,
822 unsigned char *address)
1da177e4 823{
c14979b9
CM
824 if (channel >= IPMI_MAX_CHANNELS)
825 return -EINVAL;
826 *address = user->intf->channels[channel].lun;
827 return 0;
1da177e4
LT
828}
829
830int ipmi_set_gets_events(ipmi_user_t user, int val)
831{
832 unsigned long flags;
833 struct ipmi_recv_msg *msg, *msg2;
834
835 read_lock(&(user->intf->users_lock));
836 spin_lock_irqsave(&(user->intf->events_lock), flags);
837 user->gets_events = val;
838
839 if (val) {
840 /* Deliver any queued events. */
841 list_for_each_entry_safe(msg, msg2, &(user->intf->waiting_events), link) {
842 list_del(&msg->link);
843 msg->user = user;
844 deliver_response(msg);
845 }
846 }
847
848 spin_unlock_irqrestore(&(user->intf->events_lock), flags);
849 read_unlock(&(user->intf->users_lock));
850
851 return 0;
852}
853
854int ipmi_register_for_cmd(ipmi_user_t user,
855 unsigned char netfn,
856 unsigned char cmd)
857{
858 struct cmd_rcvr *cmp;
859 unsigned long flags;
860 struct cmd_rcvr *rcvr;
861 int rv = 0;
862
863
864 rcvr = kmalloc(sizeof(*rcvr), GFP_KERNEL);
865 if (! rcvr)
866 return -ENOMEM;
867
868 read_lock(&(user->intf->users_lock));
869 write_lock_irqsave(&(user->intf->cmd_rcvr_lock), flags);
870 if (user->intf->all_cmd_rcvr != NULL) {
871 rv = -EBUSY;
872 goto out_unlock;
873 }
874
875 /* Make sure the command/netfn is not already registered. */
876 list_for_each_entry(cmp, &(user->intf->cmd_rcvrs), link) {
877 if ((cmp->netfn == netfn) && (cmp->cmd == cmd)) {
878 rv = -EBUSY;
879 break;
880 }
881 }
882
883 if (! rv) {
884 rcvr->cmd = cmd;
885 rcvr->netfn = netfn;
886 rcvr->user = user;
887 list_add_tail(&(rcvr->link), &(user->intf->cmd_rcvrs));
888 }
889 out_unlock:
890 write_unlock_irqrestore(&(user->intf->cmd_rcvr_lock), flags);
891 read_unlock(&(user->intf->users_lock));
892
893 if (rv)
894 kfree(rcvr);
895
896 return rv;
897}
898
899int ipmi_unregister_for_cmd(ipmi_user_t user,
900 unsigned char netfn,
901 unsigned char cmd)
902{
903 unsigned long flags;
904 struct cmd_rcvr *rcvr;
905 int rv = -ENOENT;
906
907 read_lock(&(user->intf->users_lock));
908 write_lock_irqsave(&(user->intf->cmd_rcvr_lock), flags);
909 /* Make sure the command/netfn is not already registered. */
910 list_for_each_entry(rcvr, &(user->intf->cmd_rcvrs), link) {
911 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)) {
912 rv = 0;
913 list_del(&rcvr->link);
914 kfree(rcvr);
915 break;
916 }
917 }
918 write_unlock_irqrestore(&(user->intf->cmd_rcvr_lock), flags);
919 read_unlock(&(user->intf->users_lock));
920
921 return rv;
922}
923
924void ipmi_user_set_run_to_completion(ipmi_user_t user, int val)
925{
926 user->intf->handlers->set_run_to_completion(user->intf->send_info,
927 val);
928}
929
930static unsigned char
931ipmb_checksum(unsigned char *data, int size)
932{
933 unsigned char csum = 0;
934
935 for (; size > 0; size--, data++)
936 csum += *data;
937
938 return -csum;
939}
940
941static inline void format_ipmb_msg(struct ipmi_smi_msg *smi_msg,
942 struct kernel_ipmi_msg *msg,
943 struct ipmi_ipmb_addr *ipmb_addr,
944 long msgid,
945 unsigned char ipmb_seq,
946 int broadcast,
947 unsigned char source_address,
948 unsigned char source_lun)
949{
950 int i = broadcast;
951
952 /* Format the IPMB header data. */
953 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
954 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
955 smi_msg->data[2] = ipmb_addr->channel;
956 if (broadcast)
957 smi_msg->data[3] = 0;
958 smi_msg->data[i+3] = ipmb_addr->slave_addr;
959 smi_msg->data[i+4] = (msg->netfn << 2) | (ipmb_addr->lun & 0x3);
960 smi_msg->data[i+5] = ipmb_checksum(&(smi_msg->data[i+3]), 2);
961 smi_msg->data[i+6] = source_address;
962 smi_msg->data[i+7] = (ipmb_seq << 2) | source_lun;
963 smi_msg->data[i+8] = msg->cmd;
964
965 /* Now tack on the data to the message. */
966 if (msg->data_len > 0)
967 memcpy(&(smi_msg->data[i+9]), msg->data,
968 msg->data_len);
969 smi_msg->data_size = msg->data_len + 9;
970
971 /* Now calculate the checksum and tack it on. */
972 smi_msg->data[i+smi_msg->data_size]
973 = ipmb_checksum(&(smi_msg->data[i+6]),
974 smi_msg->data_size-6);
975
976 /* Add on the checksum size and the offset from the
977 broadcast. */
978 smi_msg->data_size += 1 + i;
979
980 smi_msg->msgid = msgid;
981}
982
983static inline void format_lan_msg(struct ipmi_smi_msg *smi_msg,
984 struct kernel_ipmi_msg *msg,
985 struct ipmi_lan_addr *lan_addr,
986 long msgid,
987 unsigned char ipmb_seq,
988 unsigned char source_lun)
989{
990 /* Format the IPMB header data. */
991 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
992 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
993 smi_msg->data[2] = lan_addr->channel;
994 smi_msg->data[3] = lan_addr->session_handle;
995 smi_msg->data[4] = lan_addr->remote_SWID;
996 smi_msg->data[5] = (msg->netfn << 2) | (lan_addr->lun & 0x3);
997 smi_msg->data[6] = ipmb_checksum(&(smi_msg->data[4]), 2);
998 smi_msg->data[7] = lan_addr->local_SWID;
999 smi_msg->data[8] = (ipmb_seq << 2) | source_lun;
1000 smi_msg->data[9] = msg->cmd;
1001
1002 /* Now tack on the data to the message. */
1003 if (msg->data_len > 0)
1004 memcpy(&(smi_msg->data[10]), msg->data,
1005 msg->data_len);
1006 smi_msg->data_size = msg->data_len + 10;
1007
1008 /* Now calculate the checksum and tack it on. */
1009 smi_msg->data[smi_msg->data_size]
1010 = ipmb_checksum(&(smi_msg->data[7]),
1011 smi_msg->data_size-7);
1012
1013 /* Add on the checksum size and the offset from the
1014 broadcast. */
1015 smi_msg->data_size += 1;
1016
1017 smi_msg->msgid = msgid;
1018}
1019
1020/* Separate from ipmi_request so that the user does not have to be
1021 supplied in certain circumstances (mainly at panic time). If
1022 messages are supplied, they will be freed, even if an error
1023 occurs. */
1024static inline int i_ipmi_request(ipmi_user_t user,
1025 ipmi_smi_t intf,
1026 struct ipmi_addr *addr,
1027 long msgid,
1028 struct kernel_ipmi_msg *msg,
1029 void *user_msg_data,
1030 void *supplied_smi,
1031 struct ipmi_recv_msg *supplied_recv,
1032 int priority,
1033 unsigned char source_address,
1034 unsigned char source_lun,
1035 int retries,
1036 unsigned int retry_time_ms)
1037{
1038 int rv = 0;
1039 struct ipmi_smi_msg *smi_msg;
1040 struct ipmi_recv_msg *recv_msg;
1041 unsigned long flags;
1042
1043
1044 if (supplied_recv) {
1045 recv_msg = supplied_recv;
1046 } else {
1047 recv_msg = ipmi_alloc_recv_msg();
1048 if (recv_msg == NULL) {
1049 return -ENOMEM;
1050 }
1051 }
1052 recv_msg->user_msg_data = user_msg_data;
1053
1054 if (supplied_smi) {
1055 smi_msg = (struct ipmi_smi_msg *) supplied_smi;
1056 } else {
1057 smi_msg = ipmi_alloc_smi_msg();
1058 if (smi_msg == NULL) {
1059 ipmi_free_recv_msg(recv_msg);
1060 return -ENOMEM;
1061 }
1062 }
1063
1064 recv_msg->user = user;
1065 recv_msg->msgid = msgid;
1066 /* Store the message to send in the receive message so timeout
1067 responses can get the proper response data. */
1068 recv_msg->msg = *msg;
1069
1070 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
1071 struct ipmi_system_interface_addr *smi_addr;
1072
1073 if (msg->netfn & 1) {
1074 /* Responses are not allowed to the SMI. */
1075 rv = -EINVAL;
1076 goto out_err;
1077 }
1078
1079 smi_addr = (struct ipmi_system_interface_addr *) addr;
1080 if (smi_addr->lun > 3) {
1081 spin_lock_irqsave(&intf->counter_lock, flags);
1082 intf->sent_invalid_commands++;
1083 spin_unlock_irqrestore(&intf->counter_lock, flags);
1084 rv = -EINVAL;
1085 goto out_err;
1086 }
1087
1088 memcpy(&recv_msg->addr, smi_addr, sizeof(*smi_addr));
1089
1090 if ((msg->netfn == IPMI_NETFN_APP_REQUEST)
1091 && ((msg->cmd == IPMI_SEND_MSG_CMD)
1092 || (msg->cmd == IPMI_GET_MSG_CMD)
1093 || (msg->cmd == IPMI_READ_EVENT_MSG_BUFFER_CMD)))
1094 {
1095 /* We don't let the user do these, since we manage
1096 the sequence numbers. */
1097 spin_lock_irqsave(&intf->counter_lock, flags);
1098 intf->sent_invalid_commands++;
1099 spin_unlock_irqrestore(&intf->counter_lock, flags);
1100 rv = -EINVAL;
1101 goto out_err;
1102 }
1103
1104 if ((msg->data_len + 2) > IPMI_MAX_MSG_LENGTH) {
1105 spin_lock_irqsave(&intf->counter_lock, flags);
1106 intf->sent_invalid_commands++;
1107 spin_unlock_irqrestore(&intf->counter_lock, flags);
1108 rv = -EMSGSIZE;
1109 goto out_err;
1110 }
1111
1112 smi_msg->data[0] = (msg->netfn << 2) | (smi_addr->lun & 0x3);
1113 smi_msg->data[1] = msg->cmd;
1114 smi_msg->msgid = msgid;
1115 smi_msg->user_data = recv_msg;
1116 if (msg->data_len > 0)
1117 memcpy(&(smi_msg->data[2]), msg->data, msg->data_len);
1118 smi_msg->data_size = msg->data_len + 2;
1119 spin_lock_irqsave(&intf->counter_lock, flags);
1120 intf->sent_local_commands++;
1121 spin_unlock_irqrestore(&intf->counter_lock, flags);
1122 } else if ((addr->addr_type == IPMI_IPMB_ADDR_TYPE)
1123 || (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
1124 {
1125 struct ipmi_ipmb_addr *ipmb_addr;
1126 unsigned char ipmb_seq;
1127 long seqid;
1128 int broadcast = 0;
1129
9c101fd4
KZ
1130 if (addr->channel >= IPMI_MAX_CHANNELS) {
1131 spin_lock_irqsave(&intf->counter_lock, flags);
1da177e4
LT
1132 intf->sent_invalid_commands++;
1133 spin_unlock_irqrestore(&intf->counter_lock, flags);
1134 rv = -EINVAL;
1135 goto out_err;
1136 }
1137
1138 if (intf->channels[addr->channel].medium
1139 != IPMI_CHANNEL_MEDIUM_IPMB)
1140 {
1141 spin_lock_irqsave(&intf->counter_lock, flags);
1142 intf->sent_invalid_commands++;
1143 spin_unlock_irqrestore(&intf->counter_lock, flags);
1144 rv = -EINVAL;
1145 goto out_err;
1146 }
1147
1148 if (retries < 0) {
1149 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE)
1150 retries = 0; /* Don't retry broadcasts. */
1151 else
1152 retries = 4;
1153 }
1154 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) {
1155 /* Broadcasts add a zero at the beginning of the
1156 message, but otherwise is the same as an IPMB
1157 address. */
1158 addr->addr_type = IPMI_IPMB_ADDR_TYPE;
1159 broadcast = 1;
1160 }
1161
1162
1163 /* Default to 1 second retries. */
1164 if (retry_time_ms == 0)
1165 retry_time_ms = 1000;
1166
1167 /* 9 for the header and 1 for the checksum, plus
1168 possibly one for the broadcast. */
1169 if ((msg->data_len + 10 + broadcast) > IPMI_MAX_MSG_LENGTH) {
1170 spin_lock_irqsave(&intf->counter_lock, flags);
1171 intf->sent_invalid_commands++;
1172 spin_unlock_irqrestore(&intf->counter_lock, flags);
1173 rv = -EMSGSIZE;
1174 goto out_err;
1175 }
1176
1177 ipmb_addr = (struct ipmi_ipmb_addr *) addr;
1178 if (ipmb_addr->lun > 3) {
1179 spin_lock_irqsave(&intf->counter_lock, flags);
1180 intf->sent_invalid_commands++;
1181 spin_unlock_irqrestore(&intf->counter_lock, flags);
1182 rv = -EINVAL;
1183 goto out_err;
1184 }
1185
1186 memcpy(&recv_msg->addr, ipmb_addr, sizeof(*ipmb_addr));
1187
1188 if (recv_msg->msg.netfn & 0x1) {
1189 /* It's a response, so use the user's sequence
1190 from msgid. */
1191 spin_lock_irqsave(&intf->counter_lock, flags);
1192 intf->sent_ipmb_responses++;
1193 spin_unlock_irqrestore(&intf->counter_lock, flags);
1194 format_ipmb_msg(smi_msg, msg, ipmb_addr, msgid,
1195 msgid, broadcast,
1196 source_address, source_lun);
1197
1198 /* Save the receive message so we can use it
1199 to deliver the response. */
1200 smi_msg->user_data = recv_msg;
1201 } else {
1202 /* It's a command, so get a sequence for it. */
1203
1204 spin_lock_irqsave(&(intf->seq_lock), flags);
1205
1206 spin_lock(&intf->counter_lock);
1207 intf->sent_ipmb_commands++;
1208 spin_unlock(&intf->counter_lock);
1209
1210 /* Create a sequence number with a 1 second
1211 timeout and 4 retries. */
1212 rv = intf_next_seq(intf,
1213 recv_msg,
1214 retry_time_ms,
1215 retries,
1216 broadcast,
1217 &ipmb_seq,
1218 &seqid);
1219 if (rv) {
1220 /* We have used up all the sequence numbers,
1221 probably, so abort. */
1222 spin_unlock_irqrestore(&(intf->seq_lock),
1223 flags);
1224 goto out_err;
1225 }
1226
1227 /* Store the sequence number in the message,
1228 so that when the send message response
1229 comes back we can start the timer. */
1230 format_ipmb_msg(smi_msg, msg, ipmb_addr,
1231 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1232 ipmb_seq, broadcast,
1233 source_address, source_lun);
1234
1235 /* Copy the message into the recv message data, so we
1236 can retransmit it later if necessary. */
1237 memcpy(recv_msg->msg_data, smi_msg->data,
1238 smi_msg->data_size);
1239 recv_msg->msg.data = recv_msg->msg_data;
1240 recv_msg->msg.data_len = smi_msg->data_size;
1241
1242 /* We don't unlock until here, because we need
1243 to copy the completed message into the
1244 recv_msg before we release the lock.
1245 Otherwise, race conditions may bite us. I
1246 know that's pretty paranoid, but I prefer
1247 to be correct. */
1248 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1249 }
1250 } else if (addr->addr_type == IPMI_LAN_ADDR_TYPE) {
1251 struct ipmi_lan_addr *lan_addr;
1252 unsigned char ipmb_seq;
1253 long seqid;
1254
c14979b9 1255 if (addr->channel >= IPMI_NUM_CHANNELS) {
1da177e4
LT
1256 spin_lock_irqsave(&intf->counter_lock, flags);
1257 intf->sent_invalid_commands++;
1258 spin_unlock_irqrestore(&intf->counter_lock, flags);
1259 rv = -EINVAL;
1260 goto out_err;
1261 }
1262
1263 if ((intf->channels[addr->channel].medium
1264 != IPMI_CHANNEL_MEDIUM_8023LAN)
1265 && (intf->channels[addr->channel].medium
1266 != IPMI_CHANNEL_MEDIUM_ASYNC))
1267 {
1268 spin_lock_irqsave(&intf->counter_lock, flags);
1269 intf->sent_invalid_commands++;
1270 spin_unlock_irqrestore(&intf->counter_lock, flags);
1271 rv = -EINVAL;
1272 goto out_err;
1273 }
1274
1275 retries = 4;
1276
1277 /* Default to 1 second retries. */
1278 if (retry_time_ms == 0)
1279 retry_time_ms = 1000;
1280
1281 /* 11 for the header and 1 for the checksum. */
1282 if ((msg->data_len + 12) > IPMI_MAX_MSG_LENGTH) {
1283 spin_lock_irqsave(&intf->counter_lock, flags);
1284 intf->sent_invalid_commands++;
1285 spin_unlock_irqrestore(&intf->counter_lock, flags);
1286 rv = -EMSGSIZE;
1287 goto out_err;
1288 }
1289
1290 lan_addr = (struct ipmi_lan_addr *) addr;
1291 if (lan_addr->lun > 3) {
1292 spin_lock_irqsave(&intf->counter_lock, flags);
1293 intf->sent_invalid_commands++;
1294 spin_unlock_irqrestore(&intf->counter_lock, flags);
1295 rv = -EINVAL;
1296 goto out_err;
1297 }
1298
1299 memcpy(&recv_msg->addr, lan_addr, sizeof(*lan_addr));
1300
1301 if (recv_msg->msg.netfn & 0x1) {
1302 /* It's a response, so use the user's sequence
1303 from msgid. */
1304 spin_lock_irqsave(&intf->counter_lock, flags);
1305 intf->sent_lan_responses++;
1306 spin_unlock_irqrestore(&intf->counter_lock, flags);
1307 format_lan_msg(smi_msg, msg, lan_addr, msgid,
1308 msgid, source_lun);
1309
1310 /* Save the receive message so we can use it
1311 to deliver the response. */
1312 smi_msg->user_data = recv_msg;
1313 } else {
1314 /* It's a command, so get a sequence for it. */
1315
1316 spin_lock_irqsave(&(intf->seq_lock), flags);
1317
1318 spin_lock(&intf->counter_lock);
1319 intf->sent_lan_commands++;
1320 spin_unlock(&intf->counter_lock);
1321
1322 /* Create a sequence number with a 1 second
1323 timeout and 4 retries. */
1324 rv = intf_next_seq(intf,
1325 recv_msg,
1326 retry_time_ms,
1327 retries,
1328 0,
1329 &ipmb_seq,
1330 &seqid);
1331 if (rv) {
1332 /* We have used up all the sequence numbers,
1333 probably, so abort. */
1334 spin_unlock_irqrestore(&(intf->seq_lock),
1335 flags);
1336 goto out_err;
1337 }
1338
1339 /* Store the sequence number in the message,
1340 so that when the send message response
1341 comes back we can start the timer. */
1342 format_lan_msg(smi_msg, msg, lan_addr,
1343 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1344 ipmb_seq, source_lun);
1345
1346 /* Copy the message into the recv message data, so we
1347 can retransmit it later if necessary. */
1348 memcpy(recv_msg->msg_data, smi_msg->data,
1349 smi_msg->data_size);
1350 recv_msg->msg.data = recv_msg->msg_data;
1351 recv_msg->msg.data_len = smi_msg->data_size;
1352
1353 /* We don't unlock until here, because we need
1354 to copy the completed message into the
1355 recv_msg before we release the lock.
1356 Otherwise, race conditions may bite us. I
1357 know that's pretty paranoid, but I prefer
1358 to be correct. */
1359 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1360 }
1361 } else {
1362 /* Unknown address type. */
1363 spin_lock_irqsave(&intf->counter_lock, flags);
1364 intf->sent_invalid_commands++;
1365 spin_unlock_irqrestore(&intf->counter_lock, flags);
1366 rv = -EINVAL;
1367 goto out_err;
1368 }
1369
1370#ifdef DEBUG_MSGING
1371 {
1372 int m;
e8b33617 1373 for (m = 0; m < smi_msg->data_size; m++)
1da177e4
LT
1374 printk(" %2.2x", smi_msg->data[m]);
1375 printk("\n");
1376 }
1377#endif
1378 intf->handlers->sender(intf->send_info, smi_msg, priority);
1379
1380 return 0;
1381
1382 out_err:
1383 ipmi_free_smi_msg(smi_msg);
1384 ipmi_free_recv_msg(recv_msg);
1385 return rv;
1386}
1387
c14979b9
CM
1388static int check_addr(ipmi_smi_t intf,
1389 struct ipmi_addr *addr,
1390 unsigned char *saddr,
1391 unsigned char *lun)
1392{
1393 if (addr->channel >= IPMI_MAX_CHANNELS)
1394 return -EINVAL;
1395 *lun = intf->channels[addr->channel].lun;
1396 *saddr = intf->channels[addr->channel].address;
1397 return 0;
1398}
1399
1da177e4
LT
1400int ipmi_request_settime(ipmi_user_t user,
1401 struct ipmi_addr *addr,
1402 long msgid,
1403 struct kernel_ipmi_msg *msg,
1404 void *user_msg_data,
1405 int priority,
1406 int retries,
1407 unsigned int retry_time_ms)
1408{
c14979b9
CM
1409 unsigned char saddr, lun;
1410 int rv;
1411
56a55ec6
CM
1412 if (! user)
1413 return -EINVAL;
c14979b9
CM
1414 rv = check_addr(user->intf, addr, &saddr, &lun);
1415 if (rv)
1416 return rv;
1da177e4
LT
1417 return i_ipmi_request(user,
1418 user->intf,
1419 addr,
1420 msgid,
1421 msg,
1422 user_msg_data,
1423 NULL, NULL,
1424 priority,
c14979b9
CM
1425 saddr,
1426 lun,
1da177e4
LT
1427 retries,
1428 retry_time_ms);
1429}
1430
1431int ipmi_request_supply_msgs(ipmi_user_t user,
1432 struct ipmi_addr *addr,
1433 long msgid,
1434 struct kernel_ipmi_msg *msg,
1435 void *user_msg_data,
1436 void *supplied_smi,
1437 struct ipmi_recv_msg *supplied_recv,
1438 int priority)
1439{
c14979b9
CM
1440 unsigned char saddr, lun;
1441 int rv;
1442
56a55ec6
CM
1443 if (! user)
1444 return -EINVAL;
c14979b9
CM
1445 rv = check_addr(user->intf, addr, &saddr, &lun);
1446 if (rv)
1447 return rv;
1da177e4
LT
1448 return i_ipmi_request(user,
1449 user->intf,
1450 addr,
1451 msgid,
1452 msg,
1453 user_msg_data,
1454 supplied_smi,
1455 supplied_recv,
1456 priority,
c14979b9
CM
1457 saddr,
1458 lun,
1da177e4
LT
1459 -1, 0);
1460}
1461
1462static int ipmb_file_read_proc(char *page, char **start, off_t off,
1463 int count, int *eof, void *data)
1464{
1465 char *out = (char *) page;
1466 ipmi_smi_t intf = data;
c14979b9
CM
1467 int i;
1468 int rv= 0;
1da177e4 1469
e8b33617 1470 for (i = 0; i < IPMI_MAX_CHANNELS; i++)
c14979b9
CM
1471 rv += sprintf(out+rv, "%x ", intf->channels[i].address);
1472 out[rv-1] = '\n'; /* Replace the final space with a newline */
1473 out[rv] = '\0';
1474 rv++;
1475 return rv;
1da177e4
LT
1476}
1477
1478static int version_file_read_proc(char *page, char **start, off_t off,
1479 int count, int *eof, void *data)
1480{
1481 char *out = (char *) page;
1482 ipmi_smi_t intf = data;
1483
1484 return sprintf(out, "%d.%d\n",
1485 intf->version_major, intf->version_minor);
1486}
1487
1488static int stat_file_read_proc(char *page, char **start, off_t off,
1489 int count, int *eof, void *data)
1490{
1491 char *out = (char *) page;
1492 ipmi_smi_t intf = data;
1493
1494 out += sprintf(out, "sent_invalid_commands: %d\n",
1495 intf->sent_invalid_commands);
1496 out += sprintf(out, "sent_local_commands: %d\n",
1497 intf->sent_local_commands);
1498 out += sprintf(out, "handled_local_responses: %d\n",
1499 intf->handled_local_responses);
1500 out += sprintf(out, "unhandled_local_responses: %d\n",
1501 intf->unhandled_local_responses);
1502 out += sprintf(out, "sent_ipmb_commands: %d\n",
1503 intf->sent_ipmb_commands);
1504 out += sprintf(out, "sent_ipmb_command_errs: %d\n",
1505 intf->sent_ipmb_command_errs);
1506 out += sprintf(out, "retransmitted_ipmb_commands: %d\n",
1507 intf->retransmitted_ipmb_commands);
1508 out += sprintf(out, "timed_out_ipmb_commands: %d\n",
1509 intf->timed_out_ipmb_commands);
1510 out += sprintf(out, "timed_out_ipmb_broadcasts: %d\n",
1511 intf->timed_out_ipmb_broadcasts);
1512 out += sprintf(out, "sent_ipmb_responses: %d\n",
1513 intf->sent_ipmb_responses);
1514 out += sprintf(out, "handled_ipmb_responses: %d\n",
1515 intf->handled_ipmb_responses);
1516 out += sprintf(out, "invalid_ipmb_responses: %d\n",
1517 intf->invalid_ipmb_responses);
1518 out += sprintf(out, "unhandled_ipmb_responses: %d\n",
1519 intf->unhandled_ipmb_responses);
1520 out += sprintf(out, "sent_lan_commands: %d\n",
1521 intf->sent_lan_commands);
1522 out += sprintf(out, "sent_lan_command_errs: %d\n",
1523 intf->sent_lan_command_errs);
1524 out += sprintf(out, "retransmitted_lan_commands: %d\n",
1525 intf->retransmitted_lan_commands);
1526 out += sprintf(out, "timed_out_lan_commands: %d\n",
1527 intf->timed_out_lan_commands);
1528 out += sprintf(out, "sent_lan_responses: %d\n",
1529 intf->sent_lan_responses);
1530 out += sprintf(out, "handled_lan_responses: %d\n",
1531 intf->handled_lan_responses);
1532 out += sprintf(out, "invalid_lan_responses: %d\n",
1533 intf->invalid_lan_responses);
1534 out += sprintf(out, "unhandled_lan_responses: %d\n",
1535 intf->unhandled_lan_responses);
1536 out += sprintf(out, "handled_commands: %d\n",
1537 intf->handled_commands);
1538 out += sprintf(out, "invalid_commands: %d\n",
1539 intf->invalid_commands);
1540 out += sprintf(out, "unhandled_commands: %d\n",
1541 intf->unhandled_commands);
1542 out += sprintf(out, "invalid_events: %d\n",
1543 intf->invalid_events);
1544 out += sprintf(out, "events: %d\n",
1545 intf->events);
1546
1547 return (out - ((char *) page));
1548}
1549
1550int ipmi_smi_add_proc_entry(ipmi_smi_t smi, char *name,
1551 read_proc_t *read_proc, write_proc_t *write_proc,
1552 void *data, struct module *owner)
1553{
1da177e4 1554 int rv = 0;
3b625943
CM
1555#ifdef CONFIG_PROC_FS
1556 struct proc_dir_entry *file;
1da177e4
LT
1557 struct ipmi_proc_entry *entry;
1558
1559 /* Create a list element. */
1560 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1561 if (!entry)
1562 return -ENOMEM;
1563 entry->name = kmalloc(strlen(name)+1, GFP_KERNEL);
1564 if (!entry->name) {
1565 kfree(entry);
1566 return -ENOMEM;
1567 }
1568 strcpy(entry->name, name);
1569
1570 file = create_proc_entry(name, 0, smi->proc_dir);
1571 if (!file) {
1572 kfree(entry->name);
1573 kfree(entry);
1574 rv = -ENOMEM;
1575 } else {
1576 file->nlink = 1;
1577 file->data = data;
1578 file->read_proc = read_proc;
1579 file->write_proc = write_proc;
1580 file->owner = owner;
1581
3b625943 1582 spin_lock(&smi->proc_entry_lock);
1da177e4
LT
1583 /* Stick it on the list. */
1584 entry->next = smi->proc_entries;
1585 smi->proc_entries = entry;
3b625943 1586 spin_unlock(&smi->proc_entry_lock);
1da177e4 1587 }
3b625943 1588#endif /* CONFIG_PROC_FS */
1da177e4
LT
1589
1590 return rv;
1591}
1592
1593static int add_proc_entries(ipmi_smi_t smi, int num)
1594{
1595 int rv = 0;
1596
3b625943 1597#ifdef CONFIG_PROC_FS
1da177e4
LT
1598 sprintf(smi->proc_dir_name, "%d", num);
1599 smi->proc_dir = proc_mkdir(smi->proc_dir_name, proc_ipmi_root);
1600 if (!smi->proc_dir)
1601 rv = -ENOMEM;
1602 else {
1603 smi->proc_dir->owner = THIS_MODULE;
1604 }
1605
1606 if (rv == 0)
1607 rv = ipmi_smi_add_proc_entry(smi, "stats",
1608 stat_file_read_proc, NULL,
1609 smi, THIS_MODULE);
1610
1611 if (rv == 0)
1612 rv = ipmi_smi_add_proc_entry(smi, "ipmb",
1613 ipmb_file_read_proc, NULL,
1614 smi, THIS_MODULE);
1615
1616 if (rv == 0)
1617 rv = ipmi_smi_add_proc_entry(smi, "version",
1618 version_file_read_proc, NULL,
1619 smi, THIS_MODULE);
3b625943 1620#endif /* CONFIG_PROC_FS */
1da177e4
LT
1621
1622 return rv;
1623}
1624
1625static void remove_proc_entries(ipmi_smi_t smi)
1626{
3b625943 1627#ifdef CONFIG_PROC_FS
1da177e4
LT
1628 struct ipmi_proc_entry *entry;
1629
3b625943 1630 spin_lock(&smi->proc_entry_lock);
1da177e4
LT
1631 while (smi->proc_entries) {
1632 entry = smi->proc_entries;
1633 smi->proc_entries = entry->next;
1634
1635 remove_proc_entry(entry->name, smi->proc_dir);
1636 kfree(entry->name);
1637 kfree(entry);
1638 }
3b625943 1639 spin_unlock(&smi->proc_entry_lock);
1da177e4 1640 remove_proc_entry(smi->proc_dir_name, proc_ipmi_root);
3b625943 1641#endif /* CONFIG_PROC_FS */
1da177e4
LT
1642}
1643
1644static int
1645send_channel_info_cmd(ipmi_smi_t intf, int chan)
1646{
1647 struct kernel_ipmi_msg msg;
1648 unsigned char data[1];
1649 struct ipmi_system_interface_addr si;
1650
1651 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
1652 si.channel = IPMI_BMC_CHANNEL;
1653 si.lun = 0;
1654
1655 msg.netfn = IPMI_NETFN_APP_REQUEST;
1656 msg.cmd = IPMI_GET_CHANNEL_INFO_CMD;
1657 msg.data = data;
1658 msg.data_len = 1;
1659 data[0] = chan;
1660 return i_ipmi_request(NULL,
1661 intf,
1662 (struct ipmi_addr *) &si,
1663 0,
1664 &msg,
56a55ec6 1665 intf,
1da177e4
LT
1666 NULL,
1667 NULL,
1668 0,
c14979b9
CM
1669 intf->channels[0].address,
1670 intf->channels[0].lun,
1da177e4
LT
1671 -1, 0);
1672}
1673
1674static void
56a55ec6 1675channel_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
1da177e4
LT
1676{
1677 int rv = 0;
1678 int chan;
1679
56a55ec6
CM
1680 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
1681 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
1682 && (msg->msg.cmd == IPMI_GET_CHANNEL_INFO_CMD))
1da177e4
LT
1683 {
1684 /* It's the one we want */
56a55ec6 1685 if (msg->msg.data[0] != 0) {
1da177e4
LT
1686 /* Got an error from the channel, just go on. */
1687
56a55ec6 1688 if (msg->msg.data[0] == IPMI_INVALID_COMMAND_ERR) {
1da177e4
LT
1689 /* If the MC does not support this
1690 command, that is legal. We just
1691 assume it has one IPMB at channel
1692 zero. */
1693 intf->channels[0].medium
1694 = IPMI_CHANNEL_MEDIUM_IPMB;
1695 intf->channels[0].protocol
1696 = IPMI_CHANNEL_PROTOCOL_IPMB;
1697 rv = -ENOSYS;
1698
1699 intf->curr_channel = IPMI_MAX_CHANNELS;
1700 wake_up(&intf->waitq);
1701 goto out;
1702 }
1703 goto next_channel;
1704 }
56a55ec6 1705 if (msg->msg.data_len < 4) {
1da177e4
LT
1706 /* Message not big enough, just go on. */
1707 goto next_channel;
1708 }
1709 chan = intf->curr_channel;
56a55ec6
CM
1710 intf->channels[chan].medium = msg->msg.data[2] & 0x7f;
1711 intf->channels[chan].protocol = msg->msg.data[3] & 0x1f;
1da177e4
LT
1712
1713 next_channel:
1714 intf->curr_channel++;
1715 if (intf->curr_channel >= IPMI_MAX_CHANNELS)
1716 wake_up(&intf->waitq);
1717 else
1718 rv = send_channel_info_cmd(intf, intf->curr_channel);
1719
1720 if (rv) {
1721 /* Got an error somehow, just give up. */
1722 intf->curr_channel = IPMI_MAX_CHANNELS;
1723 wake_up(&intf->waitq);
1724
1725 printk(KERN_WARNING PFX
1726 "Error sending channel information: %d\n",
1727 rv);
1728 }
1729 }
1730 out:
1731 return;
1732}
1733
1734int ipmi_register_smi(struct ipmi_smi_handlers *handlers,
1735 void *send_info,
1736 unsigned char version_major,
1737 unsigned char version_minor,
1738 unsigned char slave_addr,
1739 ipmi_smi_t *intf)
1740{
1741 int i, j;
1742 int rv;
1743 ipmi_smi_t new_intf;
1744 unsigned long flags;
1745
1746
1747 /* Make sure the driver is actually initialized, this handles
1748 problems with initialization order. */
1749 if (!initialized) {
1750 rv = ipmi_init_msghandler();
1751 if (rv)
1752 return rv;
1753 /* The init code doesn't return an error if it was turned
1754 off, but it won't initialize. Check that. */
1755 if (!initialized)
1756 return -ENODEV;
1757 }
1758
1759 new_intf = kmalloc(sizeof(*new_intf), GFP_KERNEL);
1760 if (!new_intf)
1761 return -ENOMEM;
1762 memset(new_intf, 0, sizeof(*new_intf));
1763
1764 new_intf->proc_dir = NULL;
1765
1766 rv = -ENOMEM;
1767
1768 down_write(&interfaces_sem);
e8b33617 1769 for (i = 0; i < MAX_IPMI_INTERFACES; i++) {
1da177e4
LT
1770 if (ipmi_interfaces[i] == NULL) {
1771 new_intf->intf_num = i;
1772 new_intf->version_major = version_major;
1773 new_intf->version_minor = version_minor;
e8b33617 1774 for (j = 0; j < IPMI_MAX_CHANNELS; j++) {
c14979b9
CM
1775 new_intf->channels[j].address
1776 = IPMI_BMC_SLAVE_ADDR;
1777 new_intf->channels[j].lun = 2;
1778 }
1779 if (slave_addr != 0)
1780 new_intf->channels[0].address = slave_addr;
1da177e4
LT
1781 rwlock_init(&(new_intf->users_lock));
1782 INIT_LIST_HEAD(&(new_intf->users));
1783 new_intf->handlers = handlers;
1784 new_intf->send_info = send_info;
1785 spin_lock_init(&(new_intf->seq_lock));
e8b33617 1786 for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++) {
1da177e4
LT
1787 new_intf->seq_table[j].inuse = 0;
1788 new_intf->seq_table[j].seqid = 0;
1789 }
1790 new_intf->curr_seq = 0;
3b625943
CM
1791#ifdef CONFIG_PROC_FS
1792 spin_lock_init(&(new_intf->proc_entry_lock));
1793#endif
1da177e4
LT
1794 spin_lock_init(&(new_intf->waiting_msgs_lock));
1795 INIT_LIST_HEAD(&(new_intf->waiting_msgs));
1796 spin_lock_init(&(new_intf->events_lock));
1797 INIT_LIST_HEAD(&(new_intf->waiting_events));
1798 new_intf->waiting_events_count = 0;
1799 rwlock_init(&(new_intf->cmd_rcvr_lock));
1800 init_waitqueue_head(&new_intf->waitq);
1801 INIT_LIST_HEAD(&(new_intf->cmd_rcvrs));
1802 new_intf->all_cmd_rcvr = NULL;
1803
1804 spin_lock_init(&(new_intf->counter_lock));
1805
1806 spin_lock_irqsave(&interfaces_lock, flags);
1807 ipmi_interfaces[i] = new_intf;
1808 spin_unlock_irqrestore(&interfaces_lock, flags);
1809
1810 rv = 0;
1811 *intf = new_intf;
1812 break;
1813 }
1814 }
1815
1816 downgrade_write(&interfaces_sem);
1817
1818 if (rv == 0)
1819 rv = add_proc_entries(*intf, i);
1820
1821 if (rv == 0) {
1822 if ((version_major > 1)
1823 || ((version_major == 1) && (version_minor >= 5)))
1824 {
1825 /* Start scanning the channels to see what is
1826 available. */
1827 (*intf)->null_user_handler = channel_handler;
1828 (*intf)->curr_channel = 0;
1829 rv = send_channel_info_cmd(*intf, 0);
1830 if (rv)
1831 goto out;
1832
1833 /* Wait for the channel info to be read. */
1834 up_read(&interfaces_sem);
1835 wait_event((*intf)->waitq,
1836 ((*intf)->curr_channel>=IPMI_MAX_CHANNELS));
1837 down_read(&interfaces_sem);
1838
1839 if (ipmi_interfaces[i] != new_intf)
1840 /* Well, it went away. Just return. */
1841 goto out;
1842 } else {
1843 /* Assume a single IPMB channel at zero. */
1844 (*intf)->channels[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
1845 (*intf)->channels[0].protocol
1846 = IPMI_CHANNEL_PROTOCOL_IPMB;
1847 }
1848
1849 /* Call all the watcher interfaces to tell
1850 them that a new interface is available. */
1851 call_smi_watchers(i);
1852 }
1853
1854 out:
1855 up_read(&interfaces_sem);
1856
1857 if (rv) {
1858 if (new_intf->proc_dir)
1859 remove_proc_entries(new_intf);
1860 kfree(new_intf);
1861 }
1862
1863 return rv;
1864}
1865
1866static void free_recv_msg_list(struct list_head *q)
1867{
1868 struct ipmi_recv_msg *msg, *msg2;
1869
1870 list_for_each_entry_safe(msg, msg2, q, link) {
1871 list_del(&msg->link);
1872 ipmi_free_recv_msg(msg);
1873 }
1874}
1875
1876static void free_cmd_rcvr_list(struct list_head *q)
1877{
1878 struct cmd_rcvr *rcvr, *rcvr2;
1879
1880 list_for_each_entry_safe(rcvr, rcvr2, q, link) {
1881 list_del(&rcvr->link);
1882 kfree(rcvr);
1883 }
1884}
1885
1886static void clean_up_interface_data(ipmi_smi_t intf)
1887{
1888 int i;
1889
1890 free_recv_msg_list(&(intf->waiting_msgs));
1891 free_recv_msg_list(&(intf->waiting_events));
1892 free_cmd_rcvr_list(&(intf->cmd_rcvrs));
1893
e8b33617 1894 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
1da177e4
LT
1895 if ((intf->seq_table[i].inuse)
1896 && (intf->seq_table[i].recv_msg))
1897 {
1898 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
1899 }
1900 }
1901}
1902
1903int ipmi_unregister_smi(ipmi_smi_t intf)
1904{
1905 int rv = -ENODEV;
1906 int i;
1907 struct ipmi_smi_watcher *w;
1908 unsigned long flags;
1909
1910 down_write(&interfaces_sem);
1911 if (list_empty(&(intf->users)))
1912 {
e8b33617 1913 for (i = 0; i < MAX_IPMI_INTERFACES; i++) {
1da177e4
LT
1914 if (ipmi_interfaces[i] == intf) {
1915 remove_proc_entries(intf);
1916 spin_lock_irqsave(&interfaces_lock, flags);
1917 ipmi_interfaces[i] = NULL;
1918 clean_up_interface_data(intf);
1919 spin_unlock_irqrestore(&interfaces_lock,flags);
1920 kfree(intf);
1921 rv = 0;
1922 goto out_call_watcher;
1923 }
1924 }
1925 } else {
1926 rv = -EBUSY;
1927 }
1928 up_write(&interfaces_sem);
1929
1930 return rv;
1931
1932 out_call_watcher:
1933 downgrade_write(&interfaces_sem);
1934
1935 /* Call all the watcher interfaces to tell them that
1936 an interface is gone. */
1937 down_read(&smi_watchers_sem);
1938 list_for_each_entry(w, &smi_watchers, link) {
1939 w->smi_gone(i);
1940 }
1941 up_read(&smi_watchers_sem);
1942 up_read(&interfaces_sem);
1943 return 0;
1944}
1945
1946static int handle_ipmb_get_msg_rsp(ipmi_smi_t intf,
1947 struct ipmi_smi_msg *msg)
1948{
1949 struct ipmi_ipmb_addr ipmb_addr;
1950 struct ipmi_recv_msg *recv_msg;
1951 unsigned long flags;
1952
1953
1954 /* This is 11, not 10, because the response must contain a
1955 * completion code. */
1956 if (msg->rsp_size < 11) {
1957 /* Message not big enough, just ignore it. */
1958 spin_lock_irqsave(&intf->counter_lock, flags);
1959 intf->invalid_ipmb_responses++;
1960 spin_unlock_irqrestore(&intf->counter_lock, flags);
1961 return 0;
1962 }
1963
1964 if (msg->rsp[2] != 0) {
1965 /* An error getting the response, just ignore it. */
1966 return 0;
1967 }
1968
1969 ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
1970 ipmb_addr.slave_addr = msg->rsp[6];
1971 ipmb_addr.channel = msg->rsp[3] & 0x0f;
1972 ipmb_addr.lun = msg->rsp[7] & 3;
1973
1974 /* It's a response from a remote entity. Look up the sequence
1975 number and handle the response. */
1976 if (intf_find_seq(intf,
1977 msg->rsp[7] >> 2,
1978 msg->rsp[3] & 0x0f,
1979 msg->rsp[8],
1980 (msg->rsp[4] >> 2) & (~1),
1981 (struct ipmi_addr *) &(ipmb_addr),
1982 &recv_msg))
1983 {
1984 /* We were unable to find the sequence number,
1985 so just nuke the message. */
1986 spin_lock_irqsave(&intf->counter_lock, flags);
1987 intf->unhandled_ipmb_responses++;
1988 spin_unlock_irqrestore(&intf->counter_lock, flags);
1989 return 0;
1990 }
1991
1992 memcpy(recv_msg->msg_data,
1993 &(msg->rsp[9]),
1994 msg->rsp_size - 9);
1995 /* THe other fields matched, so no need to set them, except
1996 for netfn, which needs to be the response that was
1997 returned, not the request value. */
1998 recv_msg->msg.netfn = msg->rsp[4] >> 2;
1999 recv_msg->msg.data = recv_msg->msg_data;
2000 recv_msg->msg.data_len = msg->rsp_size - 10;
2001 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
2002 spin_lock_irqsave(&intf->counter_lock, flags);
2003 intf->handled_ipmb_responses++;
2004 spin_unlock_irqrestore(&intf->counter_lock, flags);
2005 deliver_response(recv_msg);
2006
2007 return 0;
2008}
2009
2010static int handle_ipmb_get_msg_cmd(ipmi_smi_t intf,
2011 struct ipmi_smi_msg *msg)
2012{
2013 struct cmd_rcvr *rcvr;
2014 int rv = 0;
2015 unsigned char netfn;
2016 unsigned char cmd;
2017 ipmi_user_t user = NULL;
2018 struct ipmi_ipmb_addr *ipmb_addr;
2019 struct ipmi_recv_msg *recv_msg;
2020 unsigned long flags;
2021
2022 if (msg->rsp_size < 10) {
2023 /* Message not big enough, just ignore it. */
2024 spin_lock_irqsave(&intf->counter_lock, flags);
2025 intf->invalid_commands++;
2026 spin_unlock_irqrestore(&intf->counter_lock, flags);
2027 return 0;
2028 }
2029
2030 if (msg->rsp[2] != 0) {
2031 /* An error getting the response, just ignore it. */
2032 return 0;
2033 }
2034
2035 netfn = msg->rsp[4] >> 2;
2036 cmd = msg->rsp[8];
2037
2038 read_lock(&(intf->cmd_rcvr_lock));
2039
2040 if (intf->all_cmd_rcvr) {
2041 user = intf->all_cmd_rcvr;
2042 } else {
2043 /* Find the command/netfn. */
2044 list_for_each_entry(rcvr, &(intf->cmd_rcvrs), link) {
2045 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)) {
2046 user = rcvr->user;
2047 break;
2048 }
2049 }
2050 }
2051 read_unlock(&(intf->cmd_rcvr_lock));
2052
2053 if (user == NULL) {
2054 /* We didn't find a user, deliver an error response. */
2055 spin_lock_irqsave(&intf->counter_lock, flags);
2056 intf->unhandled_commands++;
2057 spin_unlock_irqrestore(&intf->counter_lock, flags);
2058
2059 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
2060 msg->data[1] = IPMI_SEND_MSG_CMD;
2061 msg->data[2] = msg->rsp[3];
2062 msg->data[3] = msg->rsp[6];
2063 msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
2064 msg->data[5] = ipmb_checksum(&(msg->data[3]), 2);
c14979b9 2065 msg->data[6] = intf->channels[msg->rsp[3] & 0xf].address;
1da177e4
LT
2066 /* rqseq/lun */
2067 msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
2068 msg->data[8] = msg->rsp[8]; /* cmd */
2069 msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
2070 msg->data[10] = ipmb_checksum(&(msg->data[6]), 4);
2071 msg->data_size = 11;
2072
2073#ifdef DEBUG_MSGING
2074 {
2075 int m;
2076 printk("Invalid command:");
e8b33617 2077 for (m = 0; m < msg->data_size; m++)
1da177e4
LT
2078 printk(" %2.2x", msg->data[m]);
2079 printk("\n");
2080 }
2081#endif
2082 intf->handlers->sender(intf->send_info, msg, 0);
2083
2084 rv = -1; /* We used the message, so return the value that
2085 causes it to not be freed or queued. */
2086 } else {
2087 /* Deliver the message to the user. */
2088 spin_lock_irqsave(&intf->counter_lock, flags);
2089 intf->handled_commands++;
2090 spin_unlock_irqrestore(&intf->counter_lock, flags);
2091
2092 recv_msg = ipmi_alloc_recv_msg();
2093 if (! recv_msg) {
2094 /* We couldn't allocate memory for the
2095 message, so requeue it for handling
2096 later. */
2097 rv = 1;
2098 } else {
2099 /* Extract the source address from the data. */
2100 ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
2101 ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
2102 ipmb_addr->slave_addr = msg->rsp[6];
2103 ipmb_addr->lun = msg->rsp[7] & 3;
2104 ipmb_addr->channel = msg->rsp[3] & 0xf;
2105
2106 /* Extract the rest of the message information
2107 from the IPMB header.*/
2108 recv_msg->user = user;
2109 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
2110 recv_msg->msgid = msg->rsp[7] >> 2;
2111 recv_msg->msg.netfn = msg->rsp[4] >> 2;
2112 recv_msg->msg.cmd = msg->rsp[8];
2113 recv_msg->msg.data = recv_msg->msg_data;
2114
2115 /* We chop off 10, not 9 bytes because the checksum
2116 at the end also needs to be removed. */
2117 recv_msg->msg.data_len = msg->rsp_size - 10;
2118 memcpy(recv_msg->msg_data,
2119 &(msg->rsp[9]),
2120 msg->rsp_size - 10);
2121 deliver_response(recv_msg);
2122 }
2123 }
2124
2125 return rv;
2126}
2127
2128static int handle_lan_get_msg_rsp(ipmi_smi_t intf,
2129 struct ipmi_smi_msg *msg)
2130{
2131 struct ipmi_lan_addr lan_addr;
2132 struct ipmi_recv_msg *recv_msg;
2133 unsigned long flags;
2134
2135
2136 /* This is 13, not 12, because the response must contain a
2137 * completion code. */
2138 if (msg->rsp_size < 13) {
2139 /* Message not big enough, just ignore it. */
2140 spin_lock_irqsave(&intf->counter_lock, flags);
2141 intf->invalid_lan_responses++;
2142 spin_unlock_irqrestore(&intf->counter_lock, flags);
2143 return 0;
2144 }
2145
2146 if (msg->rsp[2] != 0) {
2147 /* An error getting the response, just ignore it. */
2148 return 0;
2149 }
2150
2151 lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
2152 lan_addr.session_handle = msg->rsp[4];
2153 lan_addr.remote_SWID = msg->rsp[8];
2154 lan_addr.local_SWID = msg->rsp[5];
2155 lan_addr.channel = msg->rsp[3] & 0x0f;
2156 lan_addr.privilege = msg->rsp[3] >> 4;
2157 lan_addr.lun = msg->rsp[9] & 3;
2158
2159 /* It's a response from a remote entity. Look up the sequence
2160 number and handle the response. */
2161 if (intf_find_seq(intf,
2162 msg->rsp[9] >> 2,
2163 msg->rsp[3] & 0x0f,
2164 msg->rsp[10],
2165 (msg->rsp[6] >> 2) & (~1),
2166 (struct ipmi_addr *) &(lan_addr),
2167 &recv_msg))
2168 {
2169 /* We were unable to find the sequence number,
2170 so just nuke the message. */
2171 spin_lock_irqsave(&intf->counter_lock, flags);
2172 intf->unhandled_lan_responses++;
2173 spin_unlock_irqrestore(&intf->counter_lock, flags);
2174 return 0;
2175 }
2176
2177 memcpy(recv_msg->msg_data,
2178 &(msg->rsp[11]),
2179 msg->rsp_size - 11);
2180 /* The other fields matched, so no need to set them, except
2181 for netfn, which needs to be the response that was
2182 returned, not the request value. */
2183 recv_msg->msg.netfn = msg->rsp[6] >> 2;
2184 recv_msg->msg.data = recv_msg->msg_data;
2185 recv_msg->msg.data_len = msg->rsp_size - 12;
2186 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
2187 spin_lock_irqsave(&intf->counter_lock, flags);
2188 intf->handled_lan_responses++;
2189 spin_unlock_irqrestore(&intf->counter_lock, flags);
2190 deliver_response(recv_msg);
2191
2192 return 0;
2193}
2194
2195static int handle_lan_get_msg_cmd(ipmi_smi_t intf,
2196 struct ipmi_smi_msg *msg)
2197{
2198 struct cmd_rcvr *rcvr;
2199 int rv = 0;
2200 unsigned char netfn;
2201 unsigned char cmd;
2202 ipmi_user_t user = NULL;
2203 struct ipmi_lan_addr *lan_addr;
2204 struct ipmi_recv_msg *recv_msg;
2205 unsigned long flags;
2206
2207 if (msg->rsp_size < 12) {
2208 /* Message not big enough, just ignore it. */
2209 spin_lock_irqsave(&intf->counter_lock, flags);
2210 intf->invalid_commands++;
2211 spin_unlock_irqrestore(&intf->counter_lock, flags);
2212 return 0;
2213 }
2214
2215 if (msg->rsp[2] != 0) {
2216 /* An error getting the response, just ignore it. */
2217 return 0;
2218 }
2219
2220 netfn = msg->rsp[6] >> 2;
2221 cmd = msg->rsp[10];
2222
2223 read_lock(&(intf->cmd_rcvr_lock));
2224
2225 if (intf->all_cmd_rcvr) {
2226 user = intf->all_cmd_rcvr;
2227 } else {
2228 /* Find the command/netfn. */
2229 list_for_each_entry(rcvr, &(intf->cmd_rcvrs), link) {
2230 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)) {
2231 user = rcvr->user;
2232 break;
2233 }
2234 }
2235 }
2236 read_unlock(&(intf->cmd_rcvr_lock));
2237
2238 if (user == NULL) {
2239 /* We didn't find a user, deliver an error response. */
2240 spin_lock_irqsave(&intf->counter_lock, flags);
2241 intf->unhandled_commands++;
2242 spin_unlock_irqrestore(&intf->counter_lock, flags);
2243
2244 rv = 0; /* Don't do anything with these messages, just
2245 allow them to be freed. */
2246 } else {
2247 /* Deliver the message to the user. */
2248 spin_lock_irqsave(&intf->counter_lock, flags);
2249 intf->handled_commands++;
2250 spin_unlock_irqrestore(&intf->counter_lock, flags);
2251
2252 recv_msg = ipmi_alloc_recv_msg();
2253 if (! recv_msg) {
2254 /* We couldn't allocate memory for the
2255 message, so requeue it for handling
2256 later. */
2257 rv = 1;
2258 } else {
2259 /* Extract the source address from the data. */
2260 lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
2261 lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
2262 lan_addr->session_handle = msg->rsp[4];
2263 lan_addr->remote_SWID = msg->rsp[8];
2264 lan_addr->local_SWID = msg->rsp[5];
2265 lan_addr->lun = msg->rsp[9] & 3;
2266 lan_addr->channel = msg->rsp[3] & 0xf;
2267 lan_addr->privilege = msg->rsp[3] >> 4;
2268
2269 /* Extract the rest of the message information
2270 from the IPMB header.*/
2271 recv_msg->user = user;
2272 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
2273 recv_msg->msgid = msg->rsp[9] >> 2;
2274 recv_msg->msg.netfn = msg->rsp[6] >> 2;
2275 recv_msg->msg.cmd = msg->rsp[10];
2276 recv_msg->msg.data = recv_msg->msg_data;
2277
2278 /* We chop off 12, not 11 bytes because the checksum
2279 at the end also needs to be removed. */
2280 recv_msg->msg.data_len = msg->rsp_size - 12;
2281 memcpy(recv_msg->msg_data,
2282 &(msg->rsp[11]),
2283 msg->rsp_size - 12);
2284 deliver_response(recv_msg);
2285 }
2286 }
2287
2288 return rv;
2289}
2290
2291static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
2292 struct ipmi_smi_msg *msg)
2293{
2294 struct ipmi_system_interface_addr *smi_addr;
2295
2296 recv_msg->msgid = 0;
2297 smi_addr = (struct ipmi_system_interface_addr *) &(recv_msg->addr);
2298 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2299 smi_addr->channel = IPMI_BMC_CHANNEL;
2300 smi_addr->lun = msg->rsp[0] & 3;
2301 recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
2302 recv_msg->msg.netfn = msg->rsp[0] >> 2;
2303 recv_msg->msg.cmd = msg->rsp[1];
2304 memcpy(recv_msg->msg_data, &(msg->rsp[3]), msg->rsp_size - 3);
2305 recv_msg->msg.data = recv_msg->msg_data;
2306 recv_msg->msg.data_len = msg->rsp_size - 3;
2307}
2308
2309/* This will be called with the intf->users_lock read-locked, so no need
2310 to do that here. */
2311static int handle_read_event_rsp(ipmi_smi_t intf,
2312 struct ipmi_smi_msg *msg)
2313{
2314 struct ipmi_recv_msg *recv_msg, *recv_msg2;
2315 struct list_head msgs;
2316 ipmi_user_t user;
2317 int rv = 0;
2318 int deliver_count = 0;
2319 unsigned long flags;
2320
2321 if (msg->rsp_size < 19) {
2322 /* Message is too small to be an IPMB event. */
2323 spin_lock_irqsave(&intf->counter_lock, flags);
2324 intf->invalid_events++;
2325 spin_unlock_irqrestore(&intf->counter_lock, flags);
2326 return 0;
2327 }
2328
2329 if (msg->rsp[2] != 0) {
2330 /* An error getting the event, just ignore it. */
2331 return 0;
2332 }
2333
2334 INIT_LIST_HEAD(&msgs);
2335
2336 spin_lock_irqsave(&(intf->events_lock), flags);
2337
2338 spin_lock(&intf->counter_lock);
2339 intf->events++;
2340 spin_unlock(&intf->counter_lock);
2341
2342 /* Allocate and fill in one message for every user that is getting
2343 events. */
2344 list_for_each_entry(user, &(intf->users), link) {
2345 if (! user->gets_events)
2346 continue;
2347
2348 recv_msg = ipmi_alloc_recv_msg();
2349 if (! recv_msg) {
2350 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
2351 list_del(&recv_msg->link);
2352 ipmi_free_recv_msg(recv_msg);
2353 }
2354 /* We couldn't allocate memory for the
2355 message, so requeue it for handling
2356 later. */
2357 rv = 1;
2358 goto out;
2359 }
2360
2361 deliver_count++;
2362
2363 copy_event_into_recv_msg(recv_msg, msg);
2364 recv_msg->user = user;
2365 list_add_tail(&(recv_msg->link), &msgs);
2366 }
2367
2368 if (deliver_count) {
2369 /* Now deliver all the messages. */
2370 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
2371 list_del(&recv_msg->link);
2372 deliver_response(recv_msg);
2373 }
2374 } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
2375 /* No one to receive the message, put it in queue if there's
2376 not already too many things in the queue. */
2377 recv_msg = ipmi_alloc_recv_msg();
2378 if (! recv_msg) {
2379 /* We couldn't allocate memory for the
2380 message, so requeue it for handling
2381 later. */
2382 rv = 1;
2383 goto out;
2384 }
2385
2386 copy_event_into_recv_msg(recv_msg, msg);
2387 list_add_tail(&(recv_msg->link), &(intf->waiting_events));
2388 } else {
2389 /* There's too many things in the queue, discard this
2390 message. */
2391 printk(KERN_WARNING PFX "Event queue full, discarding an"
2392 " incoming event\n");
2393 }
2394
2395 out:
2396 spin_unlock_irqrestore(&(intf->events_lock), flags);
2397
2398 return rv;
2399}
2400
2401static int handle_bmc_rsp(ipmi_smi_t intf,
2402 struct ipmi_smi_msg *msg)
2403{
2404 struct ipmi_recv_msg *recv_msg;
2405 int found = 0;
2406 struct ipmi_user *user;
2407 unsigned long flags;
2408
2409 recv_msg = (struct ipmi_recv_msg *) msg->user_data;
56a55ec6
CM
2410 if (recv_msg == NULL)
2411 {
2412 printk(KERN_WARNING"IPMI message received with no owner. This\n"
2413 "could be because of a malformed message, or\n"
2414 "because of a hardware error. Contact your\n"
2415 "hardware vender for assistance\n");
2416 return 0;
2417 }
1da177e4
LT
2418
2419 /* Make sure the user still exists. */
2420 list_for_each_entry(user, &(intf->users), link) {
2421 if (user == recv_msg->user) {
2422 /* Found it, so we can deliver it */
2423 found = 1;
2424 break;
2425 }
2426 }
2427
56a55ec6
CM
2428 if ((! found) && recv_msg->user) {
2429 /* The user for the message went away, so give up. */
2430 spin_lock_irqsave(&intf->counter_lock, flags);
2431 intf->unhandled_local_responses++;
2432 spin_unlock_irqrestore(&intf->counter_lock, flags);
1da177e4
LT
2433 ipmi_free_recv_msg(recv_msg);
2434 } else {
2435 struct ipmi_system_interface_addr *smi_addr;
2436
2437 spin_lock_irqsave(&intf->counter_lock, flags);
2438 intf->handled_local_responses++;
2439 spin_unlock_irqrestore(&intf->counter_lock, flags);
2440 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
2441 recv_msg->msgid = msg->msgid;
2442 smi_addr = ((struct ipmi_system_interface_addr *)
2443 &(recv_msg->addr));
2444 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2445 smi_addr->channel = IPMI_BMC_CHANNEL;
2446 smi_addr->lun = msg->rsp[0] & 3;
2447 recv_msg->msg.netfn = msg->rsp[0] >> 2;
2448 recv_msg->msg.cmd = msg->rsp[1];
2449 memcpy(recv_msg->msg_data,
2450 &(msg->rsp[2]),
2451 msg->rsp_size - 2);
2452 recv_msg->msg.data = recv_msg->msg_data;
2453 recv_msg->msg.data_len = msg->rsp_size - 2;
2454 deliver_response(recv_msg);
2455 }
2456
2457 return 0;
2458}
2459
2460/* Handle a new message. Return 1 if the message should be requeued,
2461 0 if the message should be freed, or -1 if the message should not
2462 be freed or requeued. */
2463static int handle_new_recv_msg(ipmi_smi_t intf,
2464 struct ipmi_smi_msg *msg)
2465{
2466 int requeue;
2467 int chan;
2468
2469#ifdef DEBUG_MSGING
2470 int m;
2471 printk("Recv:");
e8b33617 2472 for (m = 0; m < msg->rsp_size; m++)
1da177e4
LT
2473 printk(" %2.2x", msg->rsp[m]);
2474 printk("\n");
2475#endif
2476 if (msg->rsp_size < 2) {
2477 /* Message is too small to be correct. */
2478 printk(KERN_WARNING PFX "BMC returned to small a message"
2479 " for netfn %x cmd %x, got %d bytes\n",
2480 (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size);
2481
2482 /* Generate an error response for the message. */
2483 msg->rsp[0] = msg->data[0] | (1 << 2);
2484 msg->rsp[1] = msg->data[1];
2485 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
2486 msg->rsp_size = 3;
2487 } else if (((msg->rsp[0] >> 2) != ((msg->data[0] >> 2) | 1))/* Netfn */
2488 || (msg->rsp[1] != msg->data[1])) /* Command */
2489 {
2490 /* The response is not even marginally correct. */
2491 printk(KERN_WARNING PFX "BMC returned incorrect response,"
2492 " expected netfn %x cmd %x, got netfn %x cmd %x\n",
2493 (msg->data[0] >> 2) | 1, msg->data[1],
2494 msg->rsp[0] >> 2, msg->rsp[1]);
2495
2496 /* Generate an error response for the message. */
2497 msg->rsp[0] = msg->data[0] | (1 << 2);
2498 msg->rsp[1] = msg->data[1];
2499 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
2500 msg->rsp_size = 3;
2501 }
2502
2503 if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
2504 && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
2505 && (msg->user_data != NULL))
2506 {
2507 /* It's a response to a response we sent. For this we
2508 deliver a send message response to the user. */
2509 struct ipmi_recv_msg *recv_msg = msg->user_data;
2510
2511 requeue = 0;
2512 if (msg->rsp_size < 2)
2513 /* Message is too small to be correct. */
2514 goto out;
2515
2516 chan = msg->data[2] & 0x0f;
2517 if (chan >= IPMI_MAX_CHANNELS)
2518 /* Invalid channel number */
2519 goto out;
2520
2521 if (recv_msg) {
2522 recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
2523 recv_msg->msg.data = recv_msg->msg_data;
2524 recv_msg->msg.data_len = 1;
2525 recv_msg->msg_data[0] = msg->rsp[2];
2526 deliver_response(recv_msg);
2527 }
2528 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
2529 && (msg->rsp[1] == IPMI_GET_MSG_CMD))
2530 {
2531 /* It's from the receive queue. */
2532 chan = msg->rsp[3] & 0xf;
2533 if (chan >= IPMI_MAX_CHANNELS) {
2534 /* Invalid channel number */
2535 requeue = 0;
2536 goto out;
2537 }
2538
2539 switch (intf->channels[chan].medium) {
2540 case IPMI_CHANNEL_MEDIUM_IPMB:
2541 if (msg->rsp[4] & 0x04) {
2542 /* It's a response, so find the
2543 requesting message and send it up. */
2544 requeue = handle_ipmb_get_msg_rsp(intf, msg);
2545 } else {
2546 /* It's a command to the SMS from some other
2547 entity. Handle that. */
2548 requeue = handle_ipmb_get_msg_cmd(intf, msg);
2549 }
2550 break;
2551
2552 case IPMI_CHANNEL_MEDIUM_8023LAN:
2553 case IPMI_CHANNEL_MEDIUM_ASYNC:
2554 if (msg->rsp[6] & 0x04) {
2555 /* It's a response, so find the
2556 requesting message and send it up. */
2557 requeue = handle_lan_get_msg_rsp(intf, msg);
2558 } else {
2559 /* It's a command to the SMS from some other
2560 entity. Handle that. */
2561 requeue = handle_lan_get_msg_cmd(intf, msg);
2562 }
2563 break;
2564
2565 default:
2566 /* We don't handle the channel type, so just
2567 * free the message. */
2568 requeue = 0;
2569 }
2570
2571 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
2572 && (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD))
2573 {
2574 /* It's an asyncronous event. */
2575 requeue = handle_read_event_rsp(intf, msg);
2576 } else {
2577 /* It's a response from the local BMC. */
2578 requeue = handle_bmc_rsp(intf, msg);
2579 }
2580
2581 out:
2582 return requeue;
2583}
2584
2585/* Handle a new message from the lower layer. */
2586void ipmi_smi_msg_received(ipmi_smi_t intf,
2587 struct ipmi_smi_msg *msg)
2588{
2589 unsigned long flags;
2590 int rv;
2591
2592
2593 /* Lock the user lock so the user can't go away while we are
2594 working on it. */
2595 read_lock(&(intf->users_lock));
2596
2597 if ((msg->data_size >= 2)
2598 && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
2599 && (msg->data[1] == IPMI_SEND_MSG_CMD)
2600 && (msg->user_data == NULL)) {
2601 /* This is the local response to a command send, start
2602 the timer for these. The user_data will not be
2603 NULL if this is a response send, and we will let
2604 response sends just go through. */
2605
2606 /* Check for errors, if we get certain errors (ones
2607 that mean basically we can try again later), we
2608 ignore them and start the timer. Otherwise we
2609 report the error immediately. */
2610 if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
2611 && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
2612 && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR))
2613 {
2614 int chan = msg->rsp[3] & 0xf;
2615
2616 /* Got an error sending the message, handle it. */
2617 spin_lock_irqsave(&intf->counter_lock, flags);
2618 if (chan >= IPMI_MAX_CHANNELS)
2619 ; /* This shouldn't happen */
2620 else if ((intf->channels[chan].medium
2621 == IPMI_CHANNEL_MEDIUM_8023LAN)
2622 || (intf->channels[chan].medium
2623 == IPMI_CHANNEL_MEDIUM_ASYNC))
2624 intf->sent_lan_command_errs++;
2625 else
2626 intf->sent_ipmb_command_errs++;
2627 spin_unlock_irqrestore(&intf->counter_lock, flags);
2628 intf_err_seq(intf, msg->msgid, msg->rsp[2]);
2629 } else {
2630 /* The message was sent, start the timer. */
2631 intf_start_seq_timer(intf, msg->msgid);
2632 }
2633
2634 ipmi_free_smi_msg(msg);
2635 goto out_unlock;
2636 }
2637
2638 /* To preserve message order, if the list is not empty, we
2639 tack this message onto the end of the list. */
2640 spin_lock_irqsave(&(intf->waiting_msgs_lock), flags);
2641 if (!list_empty(&(intf->waiting_msgs))) {
2642 list_add_tail(&(msg->link), &(intf->waiting_msgs));
2643 spin_unlock(&(intf->waiting_msgs_lock));
2644 goto out_unlock;
2645 }
2646 spin_unlock_irqrestore(&(intf->waiting_msgs_lock), flags);
2647
2648 rv = handle_new_recv_msg(intf, msg);
2649 if (rv > 0) {
2650 /* Could not handle the message now, just add it to a
2651 list to handle later. */
2652 spin_lock(&(intf->waiting_msgs_lock));
2653 list_add_tail(&(msg->link), &(intf->waiting_msgs));
2654 spin_unlock(&(intf->waiting_msgs_lock));
2655 } else if (rv == 0) {
2656 ipmi_free_smi_msg(msg);
2657 }
2658
2659 out_unlock:
2660 read_unlock(&(intf->users_lock));
2661}
2662
2663void ipmi_smi_watchdog_pretimeout(ipmi_smi_t intf)
2664{
2665 ipmi_user_t user;
2666
2667 read_lock(&(intf->users_lock));
2668 list_for_each_entry(user, &(intf->users), link) {
2669 if (! user->handler->ipmi_watchdog_pretimeout)
2670 continue;
2671
2672 user->handler->ipmi_watchdog_pretimeout(user->handler_data);
2673 }
2674 read_unlock(&(intf->users_lock));
2675}
2676
2677static void
2678handle_msg_timeout(struct ipmi_recv_msg *msg)
2679{
2680 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
2681 msg->msg_data[0] = IPMI_TIMEOUT_COMPLETION_CODE;
2682 msg->msg.netfn |= 1; /* Convert to a response. */
2683 msg->msg.data_len = 1;
2684 msg->msg.data = msg->msg_data;
2685 deliver_response(msg);
2686}
2687
882fe011
CM
2688static struct ipmi_smi_msg *
2689smi_from_recv_msg(ipmi_smi_t intf, struct ipmi_recv_msg *recv_msg,
2690 unsigned char seq, long seqid)
1da177e4 2691{
882fe011 2692 struct ipmi_smi_msg *smi_msg = ipmi_alloc_smi_msg();
1da177e4
LT
2693 if (!smi_msg)
2694 /* If we can't allocate the message, then just return, we
2695 get 4 retries, so this should be ok. */
882fe011 2696 return NULL;
1da177e4
LT
2697
2698 memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
2699 smi_msg->data_size = recv_msg->msg.data_len;
2700 smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
2701
1da177e4
LT
2702#ifdef DEBUG_MSGING
2703 {
2704 int m;
2705 printk("Resend: ");
e8b33617 2706 for (m = 0; m < smi_msg->data_size; m++)
1da177e4
LT
2707 printk(" %2.2x", smi_msg->data[m]);
2708 printk("\n");
2709 }
2710#endif
882fe011 2711 return smi_msg;
1da177e4
LT
2712}
2713
2714static void
2715ipmi_timeout_handler(long timeout_period)
2716{
2717 ipmi_smi_t intf;
2718 struct list_head timeouts;
2719 struct ipmi_recv_msg *msg, *msg2;
2720 struct ipmi_smi_msg *smi_msg, *smi_msg2;
2721 unsigned long flags;
2722 int i, j;
2723
2724 INIT_LIST_HEAD(&timeouts);
2725
2726 spin_lock(&interfaces_lock);
e8b33617 2727 for (i = 0; i < MAX_IPMI_INTERFACES; i++) {
1da177e4
LT
2728 intf = ipmi_interfaces[i];
2729 if (intf == NULL)
2730 continue;
2731
2732 read_lock(&(intf->users_lock));
2733
2734 /* See if any waiting messages need to be processed. */
2735 spin_lock_irqsave(&(intf->waiting_msgs_lock), flags);
2736 list_for_each_entry_safe(smi_msg, smi_msg2, &(intf->waiting_msgs), link) {
2737 if (! handle_new_recv_msg(intf, smi_msg)) {
2738 list_del(&smi_msg->link);
2739 ipmi_free_smi_msg(smi_msg);
2740 } else {
2741 /* To preserve message order, quit if we
2742 can't handle a message. */
2743 break;
2744 }
2745 }
2746 spin_unlock_irqrestore(&(intf->waiting_msgs_lock), flags);
2747
2748 /* Go through the seq table and find any messages that
2749 have timed out, putting them in the timeouts
2750 list. */
2751 spin_lock_irqsave(&(intf->seq_lock), flags);
e8b33617 2752 for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++) {
1da177e4
LT
2753 struct seq_table *ent = &(intf->seq_table[j]);
2754 if (!ent->inuse)
2755 continue;
2756
2757 ent->timeout -= timeout_period;
2758 if (ent->timeout > 0)
2759 continue;
2760
2761 if (ent->retries_left == 0) {
2762 /* The message has used all its retries. */
2763 ent->inuse = 0;
2764 msg = ent->recv_msg;
2765 list_add_tail(&(msg->link), &timeouts);
2766 spin_lock(&intf->counter_lock);
2767 if (ent->broadcast)
2768 intf->timed_out_ipmb_broadcasts++;
2769 else if (ent->recv_msg->addr.addr_type
2770 == IPMI_LAN_ADDR_TYPE)
2771 intf->timed_out_lan_commands++;
2772 else
2773 intf->timed_out_ipmb_commands++;
2774 spin_unlock(&intf->counter_lock);
2775 } else {
882fe011 2776 struct ipmi_smi_msg *smi_msg;
1da177e4
LT
2777 /* More retries, send again. */
2778
2779 /* Start with the max timer, set to normal
2780 timer after the message is sent. */
2781 ent->timeout = MAX_MSG_TIMEOUT;
2782 ent->retries_left--;
1da177e4
LT
2783 spin_lock(&intf->counter_lock);
2784 if (ent->recv_msg->addr.addr_type
2785 == IPMI_LAN_ADDR_TYPE)
2786 intf->retransmitted_lan_commands++;
2787 else
2788 intf->retransmitted_ipmb_commands++;
2789 spin_unlock(&intf->counter_lock);
882fe011
CM
2790 smi_msg = smi_from_recv_msg(intf,
2791 ent->recv_msg, j, ent->seqid);
e8b33617 2792 if (! smi_msg)
882fe011
CM
2793 continue;
2794
2795 spin_unlock_irqrestore(&(intf->seq_lock),flags);
2796 /* Send the new message. We send with a zero
2797 * priority. It timed out, I doubt time is
2798 * that critical now, and high priority
2799 * messages are really only for messages to the
2800 * local MC, which don't get resent. */
2801 intf->handlers->sender(intf->send_info,
2802 smi_msg, 0);
2803 spin_lock_irqsave(&(intf->seq_lock), flags);
1da177e4
LT
2804 }
2805 }
2806 spin_unlock_irqrestore(&(intf->seq_lock), flags);
2807
2808 list_for_each_entry_safe(msg, msg2, &timeouts, link) {
2809 handle_msg_timeout(msg);
2810 }
2811
2812 read_unlock(&(intf->users_lock));
2813 }
2814 spin_unlock(&interfaces_lock);
2815}
2816
2817static void ipmi_request_event(void)
2818{
2819 ipmi_smi_t intf;
2820 int i;
2821
2822 spin_lock(&interfaces_lock);
e8b33617 2823 for (i = 0; i < MAX_IPMI_INTERFACES; i++) {
1da177e4
LT
2824 intf = ipmi_interfaces[i];
2825 if (intf == NULL)
2826 continue;
2827
2828 intf->handlers->request_events(intf->send_info);
2829 }
2830 spin_unlock(&interfaces_lock);
2831}
2832
2833static struct timer_list ipmi_timer;
2834
2835/* Call every ~100 ms. */
2836#define IPMI_TIMEOUT_TIME 100
2837
2838/* How many jiffies does it take to get to the timeout time. */
2839#define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000)
2840
2841/* Request events from the queue every second (this is the number of
2842 IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the
2843 future, IPMI will add a way to know immediately if an event is in
2844 the queue and this silliness can go away. */
2845#define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME))
2846
8f43f84f 2847static atomic_t stop_operation;
1da177e4
LT
2848static unsigned int ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
2849
2850static void ipmi_timeout(unsigned long data)
2851{
8f43f84f 2852 if (atomic_read(&stop_operation))
1da177e4 2853 return;
1da177e4
LT
2854
2855 ticks_to_req_ev--;
2856 if (ticks_to_req_ev == 0) {
2857 ipmi_request_event();
2858 ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
2859 }
2860
2861 ipmi_timeout_handler(IPMI_TIMEOUT_TIME);
2862
8f43f84f 2863 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
1da177e4
LT
2864}
2865
2866
2867static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
2868static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
2869
2870/* FIXME - convert these to slabs. */
2871static void free_smi_msg(struct ipmi_smi_msg *msg)
2872{
2873 atomic_dec(&smi_msg_inuse_count);
2874 kfree(msg);
2875}
2876
2877struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
2878{
2879 struct ipmi_smi_msg *rv;
2880 rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
2881 if (rv) {
2882 rv->done = free_smi_msg;
2883 rv->user_data = NULL;
2884 atomic_inc(&smi_msg_inuse_count);
2885 }
2886 return rv;
2887}
2888
2889static void free_recv_msg(struct ipmi_recv_msg *msg)
2890{
2891 atomic_dec(&recv_msg_inuse_count);
2892 kfree(msg);
2893}
2894
2895struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
2896{
2897 struct ipmi_recv_msg *rv;
2898
2899 rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
2900 if (rv) {
2901 rv->done = free_recv_msg;
2902 atomic_inc(&recv_msg_inuse_count);
2903 }
2904 return rv;
2905}
2906
2907#ifdef CONFIG_IPMI_PANIC_EVENT
2908
2909static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
2910{
2911}
2912
2913static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
2914{
2915}
2916
2917#ifdef CONFIG_IPMI_PANIC_STRING
56a55ec6 2918static void event_receiver_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
1da177e4 2919{
56a55ec6
CM
2920 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2921 && (msg->msg.netfn == IPMI_NETFN_SENSOR_EVENT_RESPONSE)
2922 && (msg->msg.cmd == IPMI_GET_EVENT_RECEIVER_CMD)
2923 && (msg->msg.data[0] == IPMI_CC_NO_ERROR))
1da177e4
LT
2924 {
2925 /* A get event receiver command, save it. */
56a55ec6
CM
2926 intf->event_receiver = msg->msg.data[1];
2927 intf->event_receiver_lun = msg->msg.data[2] & 0x3;
1da177e4
LT
2928 }
2929}
2930
56a55ec6 2931static void device_id_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
1da177e4 2932{
56a55ec6
CM
2933 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2934 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
2935 && (msg->msg.cmd == IPMI_GET_DEVICE_ID_CMD)
2936 && (msg->msg.data[0] == IPMI_CC_NO_ERROR))
1da177e4
LT
2937 {
2938 /* A get device id command, save if we are an event
2939 receiver or generator. */
56a55ec6
CM
2940 intf->local_sel_device = (msg->msg.data[6] >> 2) & 1;
2941 intf->local_event_generator = (msg->msg.data[6] >> 5) & 1;
1da177e4
LT
2942 }
2943}
2944#endif
2945
2946static void send_panic_events(char *str)
2947{
2948 struct kernel_ipmi_msg msg;
2949 ipmi_smi_t intf;
2950 unsigned char data[16];
2951 int i;
2952 struct ipmi_system_interface_addr *si;
2953 struct ipmi_addr addr;
2954 struct ipmi_smi_msg smi_msg;
2955 struct ipmi_recv_msg recv_msg;
2956
2957 si = (struct ipmi_system_interface_addr *) &addr;
2958 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2959 si->channel = IPMI_BMC_CHANNEL;
2960 si->lun = 0;
2961
2962 /* Fill in an event telling that we have failed. */
2963 msg.netfn = 0x04; /* Sensor or Event. */
2964 msg.cmd = 2; /* Platform event command. */
2965 msg.data = data;
2966 msg.data_len = 8;
2967 data[0] = 0x21; /* Kernel generator ID, IPMI table 5-4 */
2968 data[1] = 0x03; /* This is for IPMI 1.0. */
2969 data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
2970 data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
2971 data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
2972
2973 /* Put a few breadcrumbs in. Hopefully later we can add more things
2974 to make the panic events more useful. */
2975 if (str) {
2976 data[3] = str[0];
2977 data[6] = str[1];
2978 data[7] = str[2];
2979 }
2980
2981 smi_msg.done = dummy_smi_done_handler;
2982 recv_msg.done = dummy_recv_done_handler;
2983
2984 /* For every registered interface, send the event. */
e8b33617 2985 for (i = 0; i < MAX_IPMI_INTERFACES; i++) {
1da177e4
LT
2986 intf = ipmi_interfaces[i];
2987 if (intf == NULL)
2988 continue;
2989
2990 /* Send the event announcing the panic. */
2991 intf->handlers->set_run_to_completion(intf->send_info, 1);
2992 i_ipmi_request(NULL,
2993 intf,
2994 &addr,
2995 0,
2996 &msg,
56a55ec6 2997 intf,
1da177e4
LT
2998 &smi_msg,
2999 &recv_msg,
3000 0,
c14979b9
CM
3001 intf->channels[0].address,
3002 intf->channels[0].lun,
1da177e4
LT
3003 0, 1); /* Don't retry, and don't wait. */
3004 }
3005
3006#ifdef CONFIG_IPMI_PANIC_STRING
3007 /* On every interface, dump a bunch of OEM event holding the
3008 string. */
3009 if (!str)
3010 return;
3011
e8b33617 3012 for (i = 0; i < MAX_IPMI_INTERFACES; i++) {
1da177e4
LT
3013 char *p = str;
3014 struct ipmi_ipmb_addr *ipmb;
3015 int j;
3016
3017 intf = ipmi_interfaces[i];
3018 if (intf == NULL)
3019 continue;
3020
3021 /* First job here is to figure out where to send the
3022 OEM events. There's no way in IPMI to send OEM
3023 events using an event send command, so we have to
3024 find the SEL to put them in and stick them in
3025 there. */
3026
3027 /* Get capabilities from the get device id. */
3028 intf->local_sel_device = 0;
3029 intf->local_event_generator = 0;
3030 intf->event_receiver = 0;
3031
3032 /* Request the device info from the local MC. */
3033 msg.netfn = IPMI_NETFN_APP_REQUEST;
3034 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
3035 msg.data = NULL;
3036 msg.data_len = 0;
3037 intf->null_user_handler = device_id_fetcher;
3038 i_ipmi_request(NULL,
3039 intf,
3040 &addr,
3041 0,
3042 &msg,
56a55ec6 3043 intf,
1da177e4
LT
3044 &smi_msg,
3045 &recv_msg,
3046 0,
c14979b9
CM
3047 intf->channels[0].address,
3048 intf->channels[0].lun,
1da177e4
LT
3049 0, 1); /* Don't retry, and don't wait. */
3050
3051 if (intf->local_event_generator) {
3052 /* Request the event receiver from the local MC. */
3053 msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
3054 msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
3055 msg.data = NULL;
3056 msg.data_len = 0;
3057 intf->null_user_handler = event_receiver_fetcher;
3058 i_ipmi_request(NULL,
3059 intf,
3060 &addr,
3061 0,
3062 &msg,
56a55ec6 3063 intf,
1da177e4
LT
3064 &smi_msg,
3065 &recv_msg,
3066 0,
c14979b9
CM
3067 intf->channels[0].address,
3068 intf->channels[0].lun,
1da177e4
LT
3069 0, 1); /* no retry, and no wait. */
3070 }
3071 intf->null_user_handler = NULL;
3072
3073 /* Validate the event receiver. The low bit must not
3074 be 1 (it must be a valid IPMB address), it cannot
3075 be zero, and it must not be my address. */
3076 if (((intf->event_receiver & 1) == 0)
3077 && (intf->event_receiver != 0)
c14979b9 3078 && (intf->event_receiver != intf->channels[0].address))
1da177e4
LT
3079 {
3080 /* The event receiver is valid, send an IPMB
3081 message. */
3082 ipmb = (struct ipmi_ipmb_addr *) &addr;
3083 ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
3084 ipmb->channel = 0; /* FIXME - is this right? */
3085 ipmb->lun = intf->event_receiver_lun;
3086 ipmb->slave_addr = intf->event_receiver;
3087 } else if (intf->local_sel_device) {
3088 /* The event receiver was not valid (or was
3089 me), but I am an SEL device, just dump it
3090 in my SEL. */
3091 si = (struct ipmi_system_interface_addr *) &addr;
3092 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3093 si->channel = IPMI_BMC_CHANNEL;
3094 si->lun = 0;
3095 } else
3096 continue; /* No where to send the event. */
3097
3098
3099 msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
3100 msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
3101 msg.data = data;
3102 msg.data_len = 16;
3103
3104 j = 0;
3105 while (*p) {
3106 int size = strlen(p);
3107
3108 if (size > 11)
3109 size = 11;
3110 data[0] = 0;
3111 data[1] = 0;
3112 data[2] = 0xf0; /* OEM event without timestamp. */
c14979b9 3113 data[3] = intf->channels[0].address;
1da177e4
LT
3114 data[4] = j++; /* sequence # */
3115 /* Always give 11 bytes, so strncpy will fill
3116 it with zeroes for me. */
3117 strncpy(data+5, p, 11);
3118 p += size;
3119
3120 i_ipmi_request(NULL,
3121 intf,
3122 &addr,
3123 0,
3124 &msg,
56a55ec6 3125 intf,
1da177e4
LT
3126 &smi_msg,
3127 &recv_msg,
3128 0,
c14979b9
CM
3129 intf->channels[0].address,
3130 intf->channels[0].lun,
1da177e4
LT
3131 0, 1); /* no retry, and no wait. */
3132 }
3133 }
3134#endif /* CONFIG_IPMI_PANIC_STRING */
3135}
3136#endif /* CONFIG_IPMI_PANIC_EVENT */
3137
3138static int has_paniced = 0;
3139
3140static int panic_event(struct notifier_block *this,
3141 unsigned long event,
3142 void *ptr)
3143{
3144 int i;
3145 ipmi_smi_t intf;
3146
3147 if (has_paniced)
3148 return NOTIFY_DONE;
3149 has_paniced = 1;
3150
3151 /* For every registered interface, set it to run to completion. */
e8b33617 3152 for (i = 0; i < MAX_IPMI_INTERFACES; i++) {
1da177e4
LT
3153 intf = ipmi_interfaces[i];
3154 if (intf == NULL)
3155 continue;
3156
3157 intf->handlers->set_run_to_completion(intf->send_info, 1);
3158 }
3159
3160#ifdef CONFIG_IPMI_PANIC_EVENT
3161 send_panic_events(ptr);
3162#endif
3163
3164 return NOTIFY_DONE;
3165}
3166
3167static struct notifier_block panic_block = {
3168 .notifier_call = panic_event,
3169 .next = NULL,
3170 .priority = 200 /* priority: INT_MAX >= x >= 0 */
3171};
3172
3173static int ipmi_init_msghandler(void)
3174{
3175 int i;
3176
3177 if (initialized)
3178 return 0;
3179
3180 printk(KERN_INFO "ipmi message handler version "
1fdd75bd 3181 IPMI_DRIVER_VERSION "\n");
1da177e4 3182
e8b33617 3183 for (i = 0; i < MAX_IPMI_INTERFACES; i++) {
1da177e4
LT
3184 ipmi_interfaces[i] = NULL;
3185 }
3186
3b625943 3187#ifdef CONFIG_PROC_FS
1da177e4
LT
3188 proc_ipmi_root = proc_mkdir("ipmi", NULL);
3189 if (!proc_ipmi_root) {
3190 printk(KERN_ERR PFX "Unable to create IPMI proc dir");
3191 return -ENOMEM;
3192 }
3193
3194 proc_ipmi_root->owner = THIS_MODULE;
3b625943 3195#endif /* CONFIG_PROC_FS */
1da177e4
LT
3196
3197 init_timer(&ipmi_timer);
3198 ipmi_timer.data = 0;
3199 ipmi_timer.function = ipmi_timeout;
3200 ipmi_timer.expires = jiffies + IPMI_TIMEOUT_JIFFIES;
3201 add_timer(&ipmi_timer);
3202
3203 notifier_chain_register(&panic_notifier_list, &panic_block);
3204
3205 initialized = 1;
3206
3207 return 0;
3208}
3209
3210static __init int ipmi_init_msghandler_mod(void)
3211{
3212 ipmi_init_msghandler();
3213 return 0;
3214}
3215
3216static __exit void cleanup_ipmi(void)
3217{
3218 int count;
3219
3220 if (!initialized)
3221 return;
3222
3223 notifier_chain_unregister(&panic_notifier_list, &panic_block);
3224
3225 /* This can't be called if any interfaces exist, so no worry about
3226 shutting down the interfaces. */
3227
3228 /* Tell the timer to stop, then wait for it to stop. This avoids
3229 problems with race conditions removing the timer here. */
8f43f84f
CM
3230 atomic_inc(&stop_operation);
3231 del_timer_sync(&ipmi_timer);
1da177e4 3232
3b625943 3233#ifdef CONFIG_PROC_FS
1da177e4 3234 remove_proc_entry(proc_ipmi_root->name, &proc_root);
3b625943 3235#endif /* CONFIG_PROC_FS */
1da177e4
LT
3236
3237 initialized = 0;
3238
3239 /* Check for buffer leaks. */
3240 count = atomic_read(&smi_msg_inuse_count);
3241 if (count != 0)
3242 printk(KERN_WARNING PFX "SMI message count %d at exit\n",
3243 count);
3244 count = atomic_read(&recv_msg_inuse_count);
3245 if (count != 0)
3246 printk(KERN_WARNING PFX "recv message count %d at exit\n",
3247 count);
3248}
3249module_exit(cleanup_ipmi);
3250
3251module_init(ipmi_init_msghandler_mod);
3252MODULE_LICENSE("GPL");
1fdd75bd
CM
3253MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
3254MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI interface.");
3255MODULE_VERSION(IPMI_DRIVER_VERSION);
1da177e4
LT
3256
3257EXPORT_SYMBOL(ipmi_create_user);
3258EXPORT_SYMBOL(ipmi_destroy_user);
3259EXPORT_SYMBOL(ipmi_get_version);
3260EXPORT_SYMBOL(ipmi_request_settime);
3261EXPORT_SYMBOL(ipmi_request_supply_msgs);
3262EXPORT_SYMBOL(ipmi_register_smi);
3263EXPORT_SYMBOL(ipmi_unregister_smi);
3264EXPORT_SYMBOL(ipmi_register_for_cmd);
3265EXPORT_SYMBOL(ipmi_unregister_for_cmd);
3266EXPORT_SYMBOL(ipmi_smi_msg_received);
3267EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
3268EXPORT_SYMBOL(ipmi_alloc_smi_msg);
3269EXPORT_SYMBOL(ipmi_addr_length);
3270EXPORT_SYMBOL(ipmi_validate_addr);
3271EXPORT_SYMBOL(ipmi_set_gets_events);
3272EXPORT_SYMBOL(ipmi_smi_watcher_register);
3273EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
3274EXPORT_SYMBOL(ipmi_set_my_address);
3275EXPORT_SYMBOL(ipmi_get_my_address);
3276EXPORT_SYMBOL(ipmi_set_my_LUN);
3277EXPORT_SYMBOL(ipmi_get_my_LUN);
3278EXPORT_SYMBOL(ipmi_smi_add_proc_entry);
3b625943 3279EXPORT_SYMBOL(proc_ipmi_root);
1da177e4 3280EXPORT_SYMBOL(ipmi_user_set_run_to_completion);