Version:  2.0.40 2.2.26 2.4.37 2.6.39 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us