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