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