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