ipmi_msghandler.c 117.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*
 * ipmi_msghandler.c
 *
 * Incoming and outgoing message routing for an IPMI interface.
 *
 * Author: MontaVista Software, Inc.
 *         Corey Minyard <minyard@mvista.com>
 *         source@mvista.com
 *
 * Copyright 2002 MontaVista Software Inc.
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *
 *
 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/module.h>
#include <linux/errno.h>
#include <linux/poll.h>
37
#include <linux/sched.h>
38
#include <linux/seq_file.h>
L
Linus Torvalds 已提交
39
#include <linux/spinlock.h>
40
#include <linux/mutex.h>
L
Linus Torvalds 已提交
41 42 43 44 45 46
#include <linux/slab.h>
#include <linux/ipmi.h>
#include <linux/ipmi_smi.h>
#include <linux/notifier.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
47
#include <linux/rcupdate.h>
48
#include <linux/interrupt.h>
L
Linus Torvalds 已提交
49 50

#define PFX "IPMI message handler: "
51

C
Corey Minyard 已提交
52
#define IPMI_DRIVER_VERSION "39.2"
L
Linus Torvalds 已提交
53 54 55

static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void);
static int ipmi_init_msghandler(void);
56 57
static void smi_recv_tasklet(unsigned long);
static void handle_new_recv_msgs(ipmi_smi_t intf);
58
static void need_waiter(ipmi_smi_t intf);
59 60
static int handle_one_recv_msg(ipmi_smi_t          intf,
			       struct ipmi_smi_msg *msg);
L
Linus Torvalds 已提交
61

R
Randy Dunlap 已提交
62
static int initialized;
L
Linus Torvalds 已提交
63

64
#ifdef CONFIG_PROC_FS
R
Randy Dunlap 已提交
65
static struct proc_dir_entry *proc_ipmi_root;
66
#endif /* CONFIG_PROC_FS */
L
Linus Torvalds 已提交
67

C
Corey Minyard 已提交
68 69 70
/* Remain in auto-maintenance mode for this amount of time (in ms). */
#define IPMI_MAINTENANCE_MODE_TIMEOUT 30000

L
Linus Torvalds 已提交
71 72
#define MAX_EVENTS_IN_QUEUE	25

73 74 75 76
/*
 * Don't let a message sit in a queue forever, always time it with at lest
 * the max message timer.  This is in milliseconds.
 */
L
Linus Torvalds 已提交
77 78
#define MAX_MSG_TIMEOUT		60000

79 80 81 82 83 84 85 86 87 88 89 90 91 92
/* Call every ~1000 ms. */
#define IPMI_TIMEOUT_TIME	1000

/* How many jiffies does it take to get to the timeout time. */
#define IPMI_TIMEOUT_JIFFIES	((IPMI_TIMEOUT_TIME * HZ) / 1000)

/*
 * Request events from the queue every second (this is the number of
 * IPMI_TIMEOUT_TIMES between event requests).  Hopefully, in the
 * future, IPMI will add a way to know immediately if an event is in
 * the queue and this silliness can go away.
 */
#define IPMI_REQUEST_EV_TIME	(1000 / (IPMI_TIMEOUT_TIME))

93 94 95
/*
 * The main "user" data structure.
 */
96
struct ipmi_user {
L
Linus Torvalds 已提交
97 98
	struct list_head link;

C
Corey Minyard 已提交
99 100
	/* Set to false when the user is destroyed. */
	bool valid;
101 102 103

	struct kref refcount;

L
Linus Torvalds 已提交
104 105 106 107 108 109 110 111
	/* The upper layer that handles receive messages. */
	struct ipmi_user_hndl *handler;
	void             *handler_data;

	/* The interface this user is bound to. */
	ipmi_smi_t intf;

	/* Does this interface receive IPMI events? */
112
	bool gets_events;
L
Linus Torvalds 已提交
113 114
};

115
struct cmd_rcvr {
L
Linus Torvalds 已提交
116 117 118 119 120
	struct list_head link;

	ipmi_user_t   user;
	unsigned char netfn;
	unsigned char cmd;
121
	unsigned int  chans;
122 123 124 125 126 127 128

	/*
	 * This is used to form a linked lised during mass deletion.
	 * Since this is in an RCU list, we cannot use the link above
	 * or change any data until the RCU period completes.  So we
	 * use this next variable during mass deletion so we can have
	 * a list and don't have to wait and restart the search on
129 130
	 * every individual deletion of a command.
	 */
131
	struct cmd_rcvr *next;
L
Linus Torvalds 已提交
132 133
};

134
struct seq_table {
L
Linus Torvalds 已提交
135 136 137 138 139 140 141
	unsigned int         inuse : 1;
	unsigned int         broadcast : 1;

	unsigned long        timeout;
	unsigned long        orig_timeout;
	unsigned int         retries_left;

142 143 144 145 146
	/*
	 * To verify on an incoming send message response that this is
	 * the message that the response is for, we keep a sequence id
	 * and increment it every time we send a message.
	 */
L
Linus Torvalds 已提交
147 148
	long                 seqid;

149 150 151 152 153
	/*
	 * This is held so we can properly respond to the message on a
	 * timeout, and it is used to hold the temporary data for
	 * retransmission, too.
	 */
L
Linus Torvalds 已提交
154 155 156
	struct ipmi_recv_msg *recv_msg;
};

157 158 159 160
/*
 * Store the information in a msgid (long) to allow us to find a
 * sequence table entry from the msgid.
 */
L
Linus Torvalds 已提交
161 162 163 164 165 166
#define STORE_SEQ_IN_MSGID(seq, seqid) (((seq&0xff)<<26) | (seqid&0x3ffffff))

#define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
	do {								\
		seq = ((msgid >> 26) & 0x3f);				\
		seqid = (msgid & 0x3fffff);				\
167
	} while (0)
L
Linus Torvalds 已提交
168 169 170

#define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3fffff)

171
struct ipmi_channel {
L
Linus Torvalds 已提交
172 173
	unsigned char medium;
	unsigned char protocol;
174

175 176 177 178
	/*
	 * My slave address.  This is initialized to IPMI_BMC_SLAVE_ADDR,
	 * but may be changed by the user.
	 */
179 180
	unsigned char address;

181 182 183 184
	/*
	 * My LUN.  This should generally stay the SMS LUN, but just in
	 * case...
	 */
185
	unsigned char lun;
L
Linus Torvalds 已提交
186 187
};

188
#ifdef CONFIG_PROC_FS
189
struct ipmi_proc_entry {
L
Linus Torvalds 已提交
190 191 192
	char                   *name;
	struct ipmi_proc_entry *next;
};
193
#endif
L
Linus Torvalds 已提交
194

195
struct bmc_device {
196
	struct platform_device pdev;
197 198 199
	struct ipmi_device_id  id;
	unsigned char          guid[16];
	int                    guid_set;
200 201
	char                   name[16];
	struct kref	       usecount;
202
};
203
#define to_bmc_device(x) container_of((x), struct bmc_device, pdev.dev)
204

205 206 207 208
/*
 * Various statistics for IPMI, these index stats[] in the ipmi_smi
 * structure.
 */
209 210 211
enum ipmi_stat_indexes {
	/* Commands we got from the user that were invalid. */
	IPMI_STAT_sent_invalid_commands = 0,
212

213 214
	/* Commands we sent to the MC. */
	IPMI_STAT_sent_local_commands,
215

216 217
	/* Responses from the MC that were delivered to a user. */
	IPMI_STAT_handled_local_responses,
218

219 220
	/* Responses from the MC that were not delivered to a user. */
	IPMI_STAT_unhandled_local_responses,
221

222 223
	/* Commands we sent out to the IPMB bus. */
	IPMI_STAT_sent_ipmb_commands,
224

225 226
	/* Commands sent on the IPMB that had errors on the SEND CMD */
	IPMI_STAT_sent_ipmb_command_errs,
227

228 229
	/* Each retransmit increments this count. */
	IPMI_STAT_retransmitted_ipmb_commands,
230

231 232 233 234 235
	/*
	 * When a message times out (runs out of retransmits) this is
	 * incremented.
	 */
	IPMI_STAT_timed_out_ipmb_commands,
236

237 238 239 240 241 242
	/*
	 * This is like above, but for broadcasts.  Broadcasts are
	 * *not* included in the above count (they are expected to
	 * time out).
	 */
	IPMI_STAT_timed_out_ipmb_broadcasts,
243

244 245
	/* Responses I have sent to the IPMB bus. */
	IPMI_STAT_sent_ipmb_responses,
246

247 248
	/* The response was delivered to the user. */
	IPMI_STAT_handled_ipmb_responses,
249

250 251
	/* The response had invalid data in it. */
	IPMI_STAT_invalid_ipmb_responses,
252

253 254
	/* The response didn't have anyone waiting for it. */
	IPMI_STAT_unhandled_ipmb_responses,
255

256 257
	/* Commands we sent out to the IPMB bus. */
	IPMI_STAT_sent_lan_commands,
258

259 260
	/* Commands sent on the IPMB that had errors on the SEND CMD */
	IPMI_STAT_sent_lan_command_errs,
261

262 263
	/* Each retransmit increments this count. */
	IPMI_STAT_retransmitted_lan_commands,
264

265 266 267 268 269 270 271 272
	/*
	 * When a message times out (runs out of retransmits) this is
	 * incremented.
	 */
	IPMI_STAT_timed_out_lan_commands,

	/* Responses I have sent to the IPMB bus. */
	IPMI_STAT_sent_lan_responses,
273

274 275
	/* The response was delivered to the user. */
	IPMI_STAT_handled_lan_responses,
276

277 278
	/* The response had invalid data in it. */
	IPMI_STAT_invalid_lan_responses,
279

280 281
	/* The response didn't have anyone waiting for it. */
	IPMI_STAT_unhandled_lan_responses,
282

283 284
	/* The command was delivered to the user. */
	IPMI_STAT_handled_commands,
285

286 287
	/* The command had invalid data in it. */
	IPMI_STAT_invalid_commands,
288

289 290
	/* The command didn't have anyone waiting for it. */
	IPMI_STAT_unhandled_commands,
291

292 293
	/* Invalid data in an event. */
	IPMI_STAT_invalid_events,
294

295 296
	/* Events that were received with the proper format. */
	IPMI_STAT_events,
297

298 299 300 301 302
	/* Retransmissions on IPMB that failed. */
	IPMI_STAT_dropped_rexmit_ipmb_commands,

	/* Retransmissions on LAN that failed. */
	IPMI_STAT_dropped_rexmit_lan_commands,
303

304 305 306
	/* This *must* remain last, add new values above this. */
	IPMI_NUM_STATS
};
307 308


L
Linus Torvalds 已提交
309
#define IPMI_IPMB_NUM_SEQ	64
310
#define IPMI_MAX_CHANNELS       16
311
struct ipmi_smi {
L
Linus Torvalds 已提交
312 313 314
	/* What interface number are we? */
	int intf_num;

315 316
	struct kref refcount;

317 318 319
	/* Set when the interface is being unregistered. */
	bool in_shutdown;

320 321 322
	/* Used for a list of interfaces. */
	struct list_head link;

323 324 325 326
	/*
	 * The list of upper layers that are using me.  seq_lock
	 * protects this.
	 */
327
	struct list_head users;
L
Linus Torvalds 已提交
328

329 330 331 332
	/* Information to supply to users. */
	unsigned char ipmi_version_major;
	unsigned char ipmi_version_minor;

L
Linus Torvalds 已提交
333 334 335
	/* Used for wake ups at startup. */
	wait_queue_head_t waitq;

336 337
	struct bmc_device *bmc;
	char *my_dev_name;
L
Linus Torvalds 已提交
338

339 340
	/*
	 * This is the lower-layer's sender routine.  Note that you
341 342
	 * must either be holding the ipmi_interfaces_mutex or be in
	 * an umpreemptible region to use this.  You must fetch the
343 344
	 * value into a local variable and make sure it is not NULL.
	 */
345
	const struct ipmi_smi_handlers *handlers;
L
Linus Torvalds 已提交
346 347
	void                     *send_info;

348
#ifdef CONFIG_PROC_FS
C
Corey Minyard 已提交
349 350
	/* A list of proc entries for this interface. */
	struct mutex           proc_entry_lock;
L
Linus Torvalds 已提交
351
	struct ipmi_proc_entry *proc_entries;
352
#endif
L
Linus Torvalds 已提交
353

354 355 356
	/* Driver-model device for the system interface. */
	struct device          *si_dev;

357 358 359 360 361 362
	/*
	 * A table of sequence numbers for this interface.  We use the
	 * sequence numbers for IPMB messages that go out of the
	 * interface to match them up with their responses.  A routine
	 * is called periodically to time the items in this list.
	 */
L
Linus Torvalds 已提交
363 364 365 366
	spinlock_t       seq_lock;
	struct seq_table seq_table[IPMI_IPMB_NUM_SEQ];
	int curr_seq;

367
	/*
368 369 370 371
	 * Messages queued for delivery.  If delivery fails (out of memory
	 * for instance), They will stay in here to be processed later in a
	 * periodic timer interrupt.  The tasklet is for handling received
	 * messages directly from the handler.
372
	 */
373 374
	spinlock_t       waiting_rcv_msgs_lock;
	struct list_head waiting_rcv_msgs;
375 376
	atomic_t	 watchdog_pretimeouts_to_deliver;
	struct tasklet_struct recv_tasklet;
L
Linus Torvalds 已提交
377

378 379 380 381 382
	spinlock_t             xmit_msgs_lock;
	struct list_head       xmit_msgs;
	struct ipmi_smi_msg    *curr_msg;
	struct list_head       hp_xmit_msgs;

383 384 385 386
	/*
	 * The list of command receivers that are registered for commands
	 * on this interface.
	 */
387
	struct mutex     cmd_rcvrs_mutex;
L
Linus Torvalds 已提交
388 389
	struct list_head cmd_rcvrs;

390 391 392 393
	/*
	 * Events that were queues because no one was there to receive
	 * them.
	 */
L
Linus Torvalds 已提交
394 395 396
	spinlock_t       events_lock; /* For dealing with event stuff. */
	struct list_head waiting_events;
	unsigned int     waiting_events_count; /* How many events in queue? */
397 398
	char             delivering_events;
	char             event_msg_printed;
399 400 401
	atomic_t         event_waiters;
	unsigned int     ticks_to_req_ev;
	int              last_needs_timer;
L
Linus Torvalds 已提交
402

403 404 405 406
	/*
	 * The event receiver for my BMC, only really used at panic
	 * shutdown as a place to store this.
	 */
L
Linus Torvalds 已提交
407 408 409 410 411
	unsigned char event_receiver;
	unsigned char event_receiver_lun;
	unsigned char local_sel_device;
	unsigned char local_event_generator;

C
Corey Minyard 已提交
412 413
	/* For handling of maintenance mode. */
	int maintenance_mode;
C
Corey Minyard 已提交
414
	bool maintenance_mode_enable;
C
Corey Minyard 已提交
415 416 417
	int auto_maintenance_timeout;
	spinlock_t maintenance_mode_lock; /* Used in a timer... */

418 419 420 421 422 423
	/*
	 * A cheap hack, if this is non-null and a message to an
	 * interface comes in with a NULL user, call this routine with
	 * it.  Note that the message will still be freed by the
	 * caller.  This only works on the system interface.
	 */
424
	void (*null_user_handler)(ipmi_smi_t intf, struct ipmi_recv_msg *msg);
L
Linus Torvalds 已提交
425

426 427 428 429
	/*
	 * When we are scanning the channels for an SMI, this will
	 * tell which channel we are scanning.
	 */
L
Linus Torvalds 已提交
430 431 432 433 434 435 436 437 438
	int curr_channel;

	/* Channel information */
	struct ipmi_channel channels[IPMI_MAX_CHANNELS];

	/* Proc FS stuff. */
	struct proc_dir_entry *proc_dir;
	char                  proc_dir_name[10];

439
	atomic_t stats[IPMI_NUM_STATS];
440 441 442 443 444 445 446

	/*
	 * run_to_completion duplicate of smb_info, smi_info
	 * and ipmi_serial_info structures. Used to decrease numbers of
	 * parameters passed by "low" level IPMI code.
	 */
	int run_to_completion;
L
Linus Torvalds 已提交
447
};
448
#define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev)
L
Linus Torvalds 已提交
449

450 451 452
/**
 * The driver model view of the IPMI messaging driver.
 */
453 454 455 456 457
static struct platform_driver ipmidriver = {
	.driver = {
		.name = "ipmi",
		.bus = &platform_bus_type
	}
458 459 460
};
static DEFINE_MUTEX(ipmidriver_mutex);

461
static LIST_HEAD(ipmi_interfaces);
462
static DEFINE_MUTEX(ipmi_interfaces_mutex);
L
Linus Torvalds 已提交
463

464 465 466
/*
 * List of watchers that want to know when smi's are added and deleted.
 */
467
static LIST_HEAD(smi_watchers);
468
static DEFINE_MUTEX(smi_watchers_mutex);
L
Linus Torvalds 已提交
469

470 471 472 473 474
#define ipmi_inc_stat(intf, stat) \
	atomic_inc(&(intf)->stats[IPMI_STAT_ ## stat])
#define ipmi_get_stat(intf, stat) \
	((unsigned int) atomic_read(&(intf)->stats[IPMI_STAT_ ## stat]))

475 476 477 478 479 480 481 482 483 484 485 486
static char *addr_src_to_str[] = { "invalid", "hotmod", "hardcoded", "SPMI",
				   "ACPI", "SMBIOS", "PCI",
				   "device-tree", "default" };

const char *ipmi_addr_src_to_str(enum ipmi_addr_src src)
{
	if (src > SI_DEFAULT)
		src = 0; /* Invalid */
	return addr_src_to_str[src];
}
EXPORT_SYMBOL(ipmi_addr_src_to_str);

487 488 489 490 491 492 493 494 495 496 497 498 499 500
static int is_lan_addr(struct ipmi_addr *addr)
{
	return addr->addr_type == IPMI_LAN_ADDR_TYPE;
}

static int is_ipmb_addr(struct ipmi_addr *addr)
{
	return addr->addr_type == IPMI_IPMB_ADDR_TYPE;
}

static int is_ipmb_bcast_addr(struct ipmi_addr *addr)
{
	return addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE;
}
501

502 503 504 505 506 507 508 509 510 511
static void free_recv_msg_list(struct list_head *q)
{
	struct ipmi_recv_msg *msg, *msg2;

	list_for_each_entry_safe(msg, msg2, q, link) {
		list_del(&msg->link);
		ipmi_free_recv_msg(msg);
	}
}

512 513 514 515 516 517 518 519 520 521
static void free_smi_msg_list(struct list_head *q)
{
	struct ipmi_smi_msg *msg, *msg2;

	list_for_each_entry_safe(msg, msg2, q, link) {
		list_del(&msg->link);
		ipmi_free_smi_msg(msg);
	}
}

522 523 524 525 526 527
static void clean_up_interface_data(ipmi_smi_t intf)
{
	int              i;
	struct cmd_rcvr  *rcvr, *rcvr2;
	struct list_head list;

528 529
	tasklet_kill(&intf->recv_tasklet);

530
	free_smi_msg_list(&intf->waiting_rcv_msgs);
531 532
	free_recv_msg_list(&intf->waiting_events);

533 534 535 536
	/*
	 * Wholesale remove all the entries from the list in the
	 * interface and wait for RCU to know that none are in use.
	 */
537
	mutex_lock(&intf->cmd_rcvrs_mutex);
538 539
	INIT_LIST_HEAD(&list);
	list_splice_init_rcu(&intf->cmd_rcvrs, &list, synchronize_rcu);
540
	mutex_unlock(&intf->cmd_rcvrs_mutex);
541 542 543 544 545 546

	list_for_each_entry_safe(rcvr, rcvr2, &list, link)
		kfree(rcvr);

	for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
		if ((intf->seq_table[i].inuse)
547
					&& (intf->seq_table[i].recv_msg))
548 549 550 551 552 553 554 555 556 557 558 559
			ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
	}
}

static void intf_free(struct kref *ref)
{
	ipmi_smi_t intf = container_of(ref, struct ipmi_smi, refcount);

	clean_up_interface_data(intf);
	kfree(intf);
}

560
struct watcher_entry {
561 562
	int              intf_num;
	ipmi_smi_t       intf;
563 564 565
	struct list_head link;
};

L
Linus Torvalds 已提交
566 567
int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher)
{
568
	ipmi_smi_t intf;
569
	LIST_HEAD(to_deliver);
570 571
	struct watcher_entry *e, *e2;

572 573
	mutex_lock(&smi_watchers_mutex);

574 575
	mutex_lock(&ipmi_interfaces_mutex);

576
	/* Build a list of things to deliver. */
577
	list_for_each_entry(intf, &ipmi_interfaces, link) {
578 579 580 581 582
		if (intf->intf_num == -1)
			continue;
		e = kmalloc(sizeof(*e), GFP_KERNEL);
		if (!e)
			goto out_err;
583 584
		kref_get(&intf->refcount);
		e->intf = intf;
585 586 587
		e->intf_num = intf->intf_num;
		list_add_tail(&e->link, &to_deliver);
	}
L
Linus Torvalds 已提交
588

589 590
	/* We will succeed, so add it to the list. */
	list_add(&watcher->link, &smi_watchers);
591 592 593 594 595

	mutex_unlock(&ipmi_interfaces_mutex);

	list_for_each_entry_safe(e, e2, &to_deliver, link) {
		list_del(&e->link);
596 597
		watcher->new_smi(e->intf_num, e->intf->si_dev);
		kref_put(&e->intf->refcount, intf_free);
598
		kfree(e);
L
Linus Torvalds 已提交
599
	}
600

601
	mutex_unlock(&smi_watchers_mutex);
602

L
Linus Torvalds 已提交
603
	return 0;
604 605

 out_err:
606 607
	mutex_unlock(&ipmi_interfaces_mutex);
	mutex_unlock(&smi_watchers_mutex);
608 609
	list_for_each_entry_safe(e, e2, &to_deliver, link) {
		list_del(&e->link);
610
		kref_put(&e->intf->refcount, intf_free);
611 612 613
		kfree(e);
	}
	return -ENOMEM;
L
Linus Torvalds 已提交
614
}
615
EXPORT_SYMBOL(ipmi_smi_watcher_register);
L
Linus Torvalds 已提交
616 617 618

int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher)
{
619
	mutex_lock(&smi_watchers_mutex);
L
Linus Torvalds 已提交
620
	list_del(&(watcher->link));
621
	mutex_unlock(&smi_watchers_mutex);
L
Linus Torvalds 已提交
622 623
	return 0;
}
624
EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
L
Linus Torvalds 已提交
625

626 627 628
/*
 * Must be called with smi_watchers_mutex held.
 */
L
Linus Torvalds 已提交
629
static void
630
call_smi_watchers(int i, struct device *dev)
L
Linus Torvalds 已提交
631 632 633 634 635
{
	struct ipmi_smi_watcher *w;

	list_for_each_entry(w, &smi_watchers, link) {
		if (try_module_get(w->owner)) {
636
			w->new_smi(i, dev);
L
Linus Torvalds 已提交
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
			module_put(w->owner);
		}
	}
}

static int
ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2)
{
	if (addr1->addr_type != addr2->addr_type)
		return 0;

	if (addr1->channel != addr2->channel)
		return 0;

	if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
		struct ipmi_system_interface_addr *smi_addr1
		    = (struct ipmi_system_interface_addr *) addr1;
		struct ipmi_system_interface_addr *smi_addr2
		    = (struct ipmi_system_interface_addr *) addr2;
		return (smi_addr1->lun == smi_addr2->lun);
	}

659
	if (is_ipmb_addr(addr1) || is_ipmb_bcast_addr(addr1)) {
L
Linus Torvalds 已提交
660 661 662 663 664 665 666 667 668
		struct ipmi_ipmb_addr *ipmb_addr1
		    = (struct ipmi_ipmb_addr *) addr1;
		struct ipmi_ipmb_addr *ipmb_addr2
		    = (struct ipmi_ipmb_addr *) addr2;

		return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr)
			&& (ipmb_addr1->lun == ipmb_addr2->lun));
	}

669
	if (is_lan_addr(addr1)) {
L
Linus Torvalds 已提交
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
		struct ipmi_lan_addr *lan_addr1
			= (struct ipmi_lan_addr *) addr1;
		struct ipmi_lan_addr *lan_addr2
		    = (struct ipmi_lan_addr *) addr2;

		return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID)
			&& (lan_addr1->local_SWID == lan_addr2->local_SWID)
			&& (lan_addr1->session_handle
			    == lan_addr2->session_handle)
			&& (lan_addr1->lun == lan_addr2->lun));
	}

	return 1;
}

int ipmi_validate_addr(struct ipmi_addr *addr, int len)
{
687
	if (len < sizeof(struct ipmi_system_interface_addr))
L
Linus Torvalds 已提交
688 689 690 691 692 693 694 695 696
		return -EINVAL;

	if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
		if (addr->channel != IPMI_BMC_CHANNEL)
			return -EINVAL;
		return 0;
	}

	if ((addr->channel == IPMI_BMC_CHANNEL)
697
	    || (addr->channel >= IPMI_MAX_CHANNELS)
L
Linus Torvalds 已提交
698 699 700
	    || (addr->channel < 0))
		return -EINVAL;

701
	if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
702
		if (len < sizeof(struct ipmi_ipmb_addr))
L
Linus Torvalds 已提交
703 704 705 706
			return -EINVAL;
		return 0;
	}

707
	if (is_lan_addr(addr)) {
708
		if (len < sizeof(struct ipmi_lan_addr))
L
Linus Torvalds 已提交
709 710 711 712 713 714
			return -EINVAL;
		return 0;
	}

	return -EINVAL;
}
715
EXPORT_SYMBOL(ipmi_validate_addr);
L
Linus Torvalds 已提交
716 717 718 719 720 721 722

unsigned int ipmi_addr_length(int addr_type)
{
	if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
		return sizeof(struct ipmi_system_interface_addr);

	if ((addr_type == IPMI_IPMB_ADDR_TYPE)
723
			|| (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
L
Linus Torvalds 已提交
724 725 726 727 728 729 730
		return sizeof(struct ipmi_ipmb_addr);

	if (addr_type == IPMI_LAN_ADDR_TYPE)
		return sizeof(struct ipmi_lan_addr);

	return 0;
}
731
EXPORT_SYMBOL(ipmi_addr_length);
L
Linus Torvalds 已提交
732 733 734

static void deliver_response(struct ipmi_recv_msg *msg)
{
735
	if (!msg->user) {
736 737 738 739 740
		ipmi_smi_t    intf = msg->user_msg_data;

		/* Special handling for NULL users. */
		if (intf->null_user_handler) {
			intf->null_user_handler(intf, msg);
741
			ipmi_inc_stat(intf, handled_local_responses);
742 743
		} else {
			/* No handler, so give up. */
744
			ipmi_inc_stat(intf, unhandled_local_responses);
745 746 747
		}
		ipmi_free_recv_msg(msg);
	} else {
748 749
		ipmi_user_t user = msg->user;
		user->handler->ipmi_recv_hndl(msg, user->handler_data);
750
	}
L
Linus Torvalds 已提交
751 752
}

753 754 755 756 757 758 759 760 761 762 763
static void
deliver_err_response(struct ipmi_recv_msg *msg, int err)
{
	msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
	msg->msg_data[0] = err;
	msg->msg.netfn |= 1; /* Convert to a response. */
	msg->msg.data_len = 1;
	msg->msg.data = msg->msg_data;
	deliver_response(msg);
}

764 765 766 767 768
/*
 * Find the next sequence number not being used and add the given
 * message with the given timeout to the sequence table.  This must be
 * called with the interface's seq_lock held.
 */
L
Linus Torvalds 已提交
769 770 771 772 773 774 775 776 777 778 779
static int intf_next_seq(ipmi_smi_t           intf,
			 struct ipmi_recv_msg *recv_msg,
			 unsigned long        timeout,
			 int                  retries,
			 int                  broadcast,
			 unsigned char        *seq,
			 long                 *seqid)
{
	int          rv = 0;
	unsigned int i;

780 781
	for (i = intf->curr_seq; (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq;
					i = (i+1)%IPMI_IPMB_NUM_SEQ) {
782
		if (!intf->seq_table[i].inuse)
L
Linus Torvalds 已提交
783 784 785
			break;
	}

786
	if (!intf->seq_table[i].inuse) {
L
Linus Torvalds 已提交
787 788
		intf->seq_table[i].recv_msg = recv_msg;

789 790 791 792
		/*
		 * Start with the maximum timeout, when the send response
		 * comes in we will start the real timer.
		 */
L
Linus Torvalds 已提交
793 794 795 796 797 798 799 800 801
		intf->seq_table[i].timeout = MAX_MSG_TIMEOUT;
		intf->seq_table[i].orig_timeout = timeout;
		intf->seq_table[i].retries_left = retries;
		intf->seq_table[i].broadcast = broadcast;
		intf->seq_table[i].inuse = 1;
		intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid);
		*seq = i;
		*seqid = intf->seq_table[i].seqid;
		intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ;
802
		need_waiter(intf);
L
Linus Torvalds 已提交
803 804 805
	} else {
		rv = -EAGAIN;
	}
806

L
Linus Torvalds 已提交
807 808 809
	return rv;
}

810 811 812 813 814 815 816
/*
 * Return the receive message for the given sequence number and
 * release the sequence number so it can be reused.  Some other data
 * is passed in to be sure the message matches up correctly (to help
 * guard against message coming in after their timeout and the
 * sequence number being reused).
 */
L
Linus Torvalds 已提交
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
static int intf_find_seq(ipmi_smi_t           intf,
			 unsigned char        seq,
			 short                channel,
			 unsigned char        cmd,
			 unsigned char        netfn,
			 struct ipmi_addr     *addr,
			 struct ipmi_recv_msg **recv_msg)
{
	int           rv = -ENODEV;
	unsigned long flags;

	if (seq >= IPMI_IPMB_NUM_SEQ)
		return -EINVAL;

	spin_lock_irqsave(&(intf->seq_lock), flags);
	if (intf->seq_table[seq].inuse) {
		struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg;

835 836 837
		if ((msg->addr.channel == channel) && (msg->msg.cmd == cmd)
				&& (msg->msg.netfn == netfn)
				&& (ipmi_addr_equal(addr, &(msg->addr)))) {
L
Linus Torvalds 已提交
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
			*recv_msg = msg;
			intf->seq_table[seq].inuse = 0;
			rv = 0;
		}
	}
	spin_unlock_irqrestore(&(intf->seq_lock), flags);

	return rv;
}


/* Start the timer for a specific sequence table entry. */
static int intf_start_seq_timer(ipmi_smi_t intf,
				long       msgid)
{
	int           rv = -ENODEV;
	unsigned long flags;
	unsigned char seq;
	unsigned long seqid;


	GET_SEQ_FROM_MSGID(msgid, seq, seqid);

	spin_lock_irqsave(&(intf->seq_lock), flags);
862 863 864 865
	/*
	 * We do this verification because the user can be deleted
	 * while a message is outstanding.
	 */
L
Linus Torvalds 已提交
866
	if ((intf->seq_table[seq].inuse)
867
				&& (intf->seq_table[seq].seqid == seqid)) {
L
Linus Torvalds 已提交
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
		struct seq_table *ent = &(intf->seq_table[seq]);
		ent->timeout = ent->orig_timeout;
		rv = 0;
	}
	spin_unlock_irqrestore(&(intf->seq_lock), flags);

	return rv;
}

/* Got an error for the send message for a specific sequence number. */
static int intf_err_seq(ipmi_smi_t   intf,
			long         msgid,
			unsigned int err)
{
	int                  rv = -ENODEV;
	unsigned long        flags;
	unsigned char        seq;
	unsigned long        seqid;
	struct ipmi_recv_msg *msg = NULL;


	GET_SEQ_FROM_MSGID(msgid, seq, seqid);

	spin_lock_irqsave(&(intf->seq_lock), flags);
892 893 894 895
	/*
	 * We do this verification because the user can be deleted
	 * while a message is outstanding.
	 */
L
Linus Torvalds 已提交
896
	if ((intf->seq_table[seq].inuse)
897
				&& (intf->seq_table[seq].seqid == seqid)) {
L
Linus Torvalds 已提交
898 899 900 901 902 903 904 905
		struct seq_table *ent = &(intf->seq_table[seq]);

		ent->inuse = 0;
		msg = ent->recv_msg;
		rv = 0;
	}
	spin_unlock_irqrestore(&(intf->seq_lock), flags);

906 907
	if (msg)
		deliver_err_response(msg, err);
L
Linus Torvalds 已提交
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922

	return rv;
}


int ipmi_create_user(unsigned int          if_num,
		     struct ipmi_user_hndl *handler,
		     void                  *handler_data,
		     ipmi_user_t           *user)
{
	unsigned long flags;
	ipmi_user_t   new_user;
	int           rv = 0;
	ipmi_smi_t    intf;

923 924 925 926 927 928 929
	/*
	 * There is no module usecount here, because it's not
	 * required.  Since this can only be used by and called from
	 * other modules, they will implicitly use this module, and
	 * thus this can't be removed unless the other modules are
	 * removed.
	 */
L
Linus Torvalds 已提交
930 931 932 933

	if (handler == NULL)
		return -EINVAL;

934 935 936 937
	/*
	 * Make sure the driver is actually initialized, this handles
	 * problems with initialization order.
	 */
L
Linus Torvalds 已提交
938 939 940 941 942
	if (!initialized) {
		rv = ipmi_init_msghandler();
		if (rv)
			return rv;

943 944 945 946
		/*
		 * The init code doesn't return an error if it was turned
		 * off, but it won't initialize.  Check that.
		 */
L
Linus Torvalds 已提交
947 948 949 950 951
		if (!initialized)
			return -ENODEV;
	}

	new_user = kmalloc(sizeof(*new_user), GFP_KERNEL);
952
	if (!new_user)
L
Linus Torvalds 已提交
953 954
		return -ENOMEM;

955
	mutex_lock(&ipmi_interfaces_mutex);
956 957 958
	list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
		if (intf->intf_num == if_num)
			goto found;
L
Linus Torvalds 已提交
959
	}
960
	/* Not found, return an error */
961 962
	rv = -EINVAL;
	goto out_kfree;
L
Linus Torvalds 已提交
963

964
 found:
965 966
	/* Note that each existing user holds a refcount to the interface. */
	kref_get(&intf->refcount);
L
Linus Torvalds 已提交
967

968
	kref_init(&new_user->refcount);
L
Linus Torvalds 已提交
969 970 971
	new_user->handler = handler;
	new_user->handler_data = handler_data;
	new_user->intf = intf;
972
	new_user->gets_events = false;
L
Linus Torvalds 已提交
973 974 975

	if (!try_module_get(intf->handlers->owner)) {
		rv = -ENODEV;
976
		goto out_kref;
L
Linus Torvalds 已提交
977 978 979 980 981 982
	}

	if (intf->handlers->inc_usecount) {
		rv = intf->handlers->inc_usecount(intf->send_info);
		if (rv) {
			module_put(intf->handlers->owner);
983
			goto out_kref;
L
Linus Torvalds 已提交
984 985 986
		}
	}

987 988 989 990
	/*
	 * Hold the lock so intf->handlers is guaranteed to be good
	 * until now
	 */
991 992
	mutex_unlock(&ipmi_interfaces_mutex);

C
Corey Minyard 已提交
993
	new_user->valid = true;
994 995 996
	spin_lock_irqsave(&intf->seq_lock, flags);
	list_add_rcu(&new_user->link, &intf->users);
	spin_unlock_irqrestore(&intf->seq_lock, flags);
997 998 999 1000 1001
	if (handler->ipmi_watchdog_pretimeout) {
		/* User wants pretimeouts, so make sure to watch for them. */
		if (atomic_inc_return(&intf->event_waiters) == 1)
			need_waiter(intf);
	}
1002 1003
	*user = new_user;
	return 0;
L
Linus Torvalds 已提交
1004

1005
out_kref:
1006
	kref_put(&intf->refcount, intf_free);
1007
out_kfree:
1008
	mutex_unlock(&ipmi_interfaces_mutex);
1009
	kfree(new_user);
L
Linus Torvalds 已提交
1010 1011
	return rv;
}
1012
EXPORT_SYMBOL(ipmi_create_user);
L
Linus Torvalds 已提交
1013

1014 1015 1016 1017
int ipmi_get_smi_info(int if_num, struct ipmi_smi_info *data)
{
	int           rv = 0;
	ipmi_smi_t    intf;
1018
	const struct ipmi_smi_handlers *handlers;
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040

	mutex_lock(&ipmi_interfaces_mutex);
	list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
		if (intf->intf_num == if_num)
			goto found;
	}
	/* Not found, return an error */
	rv = -EINVAL;
	mutex_unlock(&ipmi_interfaces_mutex);
	return rv;

found:
	handlers = intf->handlers;
	rv = -ENOSYS;
	if (handlers->get_smi_info)
		rv = handlers->get_smi_info(intf->send_info, data);
	mutex_unlock(&ipmi_interfaces_mutex);

	return rv;
}
EXPORT_SYMBOL(ipmi_get_smi_info);

1041 1042 1043 1044 1045 1046 1047
static void free_user(struct kref *ref)
{
	ipmi_user_t user = container_of(ref, struct ipmi_user, refcount);
	kfree(user);
}

int ipmi_destroy_user(ipmi_user_t user)
L
Linus Torvalds 已提交
1048
{
1049
	ipmi_smi_t       intf = user->intf;
L
Linus Torvalds 已提交
1050 1051
	int              i;
	unsigned long    flags;
1052 1053
	struct cmd_rcvr  *rcvr;
	struct cmd_rcvr  *rcvrs = NULL;
L
Linus Torvalds 已提交
1054

C
Corey Minyard 已提交
1055
	user->valid = false;
L
Linus Torvalds 已提交
1056

1057 1058 1059 1060 1061 1062
	if (user->handler->ipmi_watchdog_pretimeout)
		atomic_dec(&intf->event_waiters);

	if (user->gets_events)
		atomic_dec(&intf->event_waiters);

1063 1064 1065
	/* Remove the user from the interface's sequence table. */
	spin_lock_irqsave(&intf->seq_lock, flags);
	list_del_rcu(&user->link);
L
Linus Torvalds 已提交
1066

C
Corey Minyard 已提交
1067
	for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
1068
		if (intf->seq_table[i].inuse
1069
		    && (intf->seq_table[i].recv_msg->user == user)) {
1070
			intf->seq_table[i].inuse = 0;
1071
			ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
L
Linus Torvalds 已提交
1072 1073
		}
	}
1074 1075 1076 1077 1078 1079 1080 1081
	spin_unlock_irqrestore(&intf->seq_lock, flags);

	/*
	 * Remove the user from the command receiver's table.  First
	 * we build a list of everything (not using the standard link,
	 * since other things may be using it till we do
	 * synchronize_rcu()) then free everything in that list.
	 */
1082
	mutex_lock(&intf->cmd_rcvrs_mutex);
1083
	list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
L
Linus Torvalds 已提交
1084
		if (rcvr->user == user) {
1085 1086 1087
			list_del_rcu(&rcvr->link);
			rcvr->next = rcvrs;
			rcvrs = rcvr;
L
Linus Torvalds 已提交
1088 1089
		}
	}
1090
	mutex_unlock(&intf->cmd_rcvrs_mutex);
1091 1092 1093 1094 1095 1096
	synchronize_rcu();
	while (rcvrs) {
		rcvr = rcvrs;
		rcvrs = rcvr->next;
		kfree(rcvr);
	}
L
Linus Torvalds 已提交
1097

1098 1099 1100 1101 1102 1103 1104
	mutex_lock(&ipmi_interfaces_mutex);
	if (intf->handlers) {
		module_put(intf->handlers->owner);
		if (intf->handlers->dec_usecount)
			intf->handlers->dec_usecount(intf->send_info);
	}
	mutex_unlock(&ipmi_interfaces_mutex);
L
Linus Torvalds 已提交
1105

1106
	kref_put(&intf->refcount, intf_free);
L
Linus Torvalds 已提交
1107

1108
	kref_put(&user->refcount, free_user);
L
Linus Torvalds 已提交
1109

1110
	return 0;
L
Linus Torvalds 已提交
1111
}
1112
EXPORT_SYMBOL(ipmi_destroy_user);
L
Linus Torvalds 已提交
1113 1114 1115 1116 1117

void ipmi_get_version(ipmi_user_t   user,
		      unsigned char *major,
		      unsigned char *minor)
{
1118 1119
	*major = user->intf->ipmi_version_major;
	*minor = user->intf->ipmi_version_minor;
L
Linus Torvalds 已提交
1120
}
1121
EXPORT_SYMBOL(ipmi_get_version);
L
Linus Torvalds 已提交
1122

1123 1124 1125
int ipmi_set_my_address(ipmi_user_t   user,
			unsigned int  channel,
			unsigned char address)
L
Linus Torvalds 已提交
1126
{
1127 1128 1129 1130
	if (channel >= IPMI_MAX_CHANNELS)
		return -EINVAL;
	user->intf->channels[channel].address = address;
	return 0;
L
Linus Torvalds 已提交
1131
}
1132
EXPORT_SYMBOL(ipmi_set_my_address);
L
Linus Torvalds 已提交
1133

1134 1135 1136
int ipmi_get_my_address(ipmi_user_t   user,
			unsigned int  channel,
			unsigned char *address)
L
Linus Torvalds 已提交
1137
{
1138 1139 1140 1141
	if (channel >= IPMI_MAX_CHANNELS)
		return -EINVAL;
	*address = user->intf->channels[channel].address;
	return 0;
L
Linus Torvalds 已提交
1142
}
1143
EXPORT_SYMBOL(ipmi_get_my_address);
L
Linus Torvalds 已提交
1144

1145 1146 1147
int ipmi_set_my_LUN(ipmi_user_t   user,
		    unsigned int  channel,
		    unsigned char LUN)
L
Linus Torvalds 已提交
1148
{
1149 1150 1151 1152
	if (channel >= IPMI_MAX_CHANNELS)
		return -EINVAL;
	user->intf->channels[channel].lun = LUN & 0x3;
	return 0;
L
Linus Torvalds 已提交
1153
}
1154
EXPORT_SYMBOL(ipmi_set_my_LUN);
L
Linus Torvalds 已提交
1155

1156 1157 1158
int ipmi_get_my_LUN(ipmi_user_t   user,
		    unsigned int  channel,
		    unsigned char *address)
L
Linus Torvalds 已提交
1159
{
1160 1161 1162 1163
	if (channel >= IPMI_MAX_CHANNELS)
		return -EINVAL;
	*address = user->intf->channels[channel].lun;
	return 0;
L
Linus Torvalds 已提交
1164
}
1165
EXPORT_SYMBOL(ipmi_get_my_LUN);
L
Linus Torvalds 已提交
1166

C
Corey Minyard 已提交
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
int ipmi_get_maintenance_mode(ipmi_user_t user)
{
	int           mode;
	unsigned long flags;

	spin_lock_irqsave(&user->intf->maintenance_mode_lock, flags);
	mode = user->intf->maintenance_mode;
	spin_unlock_irqrestore(&user->intf->maintenance_mode_lock, flags);

	return mode;
}
EXPORT_SYMBOL(ipmi_get_maintenance_mode);

static void maintenance_mode_update(ipmi_smi_t intf)
{
	if (intf->handlers->set_maintenance_mode)
		intf->handlers->set_maintenance_mode(
			intf->send_info, intf->maintenance_mode_enable);
}

int ipmi_set_maintenance_mode(ipmi_user_t user, int mode)
{
	int           rv = 0;
	unsigned long flags;
	ipmi_smi_t    intf = user->intf;

	spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
	if (intf->maintenance_mode != mode) {
		switch (mode) {
		case IPMI_MAINTENANCE_MODE_AUTO:
			intf->maintenance_mode_enable
				= (intf->auto_maintenance_timeout > 0);
			break;

		case IPMI_MAINTENANCE_MODE_OFF:
C
Corey Minyard 已提交
1202
			intf->maintenance_mode_enable = false;
C
Corey Minyard 已提交
1203 1204 1205
			break;

		case IPMI_MAINTENANCE_MODE_ON:
C
Corey Minyard 已提交
1206
			intf->maintenance_mode_enable = true;
C
Corey Minyard 已提交
1207 1208 1209 1210 1211 1212
			break;

		default:
			rv = -EINVAL;
			goto out_unlock;
		}
C
Corey Minyard 已提交
1213
		intf->maintenance_mode = mode;
C
Corey Minyard 已提交
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223

		maintenance_mode_update(intf);
	}
 out_unlock:
	spin_unlock_irqrestore(&intf->maintenance_mode_lock, flags);

	return rv;
}
EXPORT_SYMBOL(ipmi_set_maintenance_mode);

1224
int ipmi_set_gets_events(ipmi_user_t user, bool val)
L
Linus Torvalds 已提交
1225
{
1226 1227 1228 1229
	unsigned long        flags;
	ipmi_smi_t           intf = user->intf;
	struct ipmi_recv_msg *msg, *msg2;
	struct list_head     msgs;
L
Linus Torvalds 已提交
1230

1231 1232 1233
	INIT_LIST_HEAD(&msgs);

	spin_lock_irqsave(&intf->events_lock, flags);
1234 1235 1236
	if (user->gets_events == val)
		goto out;

L
Linus Torvalds 已提交
1237 1238
	user->gets_events = val;

1239 1240 1241 1242 1243 1244 1245
	if (val) {
		if (atomic_inc_return(&intf->event_waiters) == 1)
			need_waiter(intf);
	} else {
		atomic_dec(&intf->event_waiters);
	}

1246 1247 1248 1249 1250 1251 1252 1253 1254
	if (intf->delivering_events)
		/*
		 * Another thread is delivering events for this, so
		 * let it handle any new events.
		 */
		goto out;

	/* Deliver any queued events. */
	while (user->gets_events && !list_empty(&intf->waiting_events)) {
A
Akinobu Mita 已提交
1255 1256
		list_for_each_entry_safe(msg, msg2, &intf->waiting_events, link)
			list_move_tail(&msg->link, &msgs);
1257
		intf->waiting_events_count = 0;
1258 1259 1260 1261 1262
		if (intf->event_msg_printed) {
			printk(KERN_WARNING PFX "Event queue no longer"
			       " full\n");
			intf->event_msg_printed = 0;
		}
1263

1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
		intf->delivering_events = 1;
		spin_unlock_irqrestore(&intf->events_lock, flags);

		list_for_each_entry_safe(msg, msg2, &msgs, link) {
			msg->user = user;
			kref_get(&user->refcount);
			deliver_response(msg);
		}

		spin_lock_irqsave(&intf->events_lock, flags);
		intf->delivering_events = 0;
1275 1276
	}

1277
 out:
1278
	spin_unlock_irqrestore(&intf->events_lock, flags);
L
Linus Torvalds 已提交
1279 1280 1281

	return 0;
}
1282
EXPORT_SYMBOL(ipmi_set_gets_events);
L
Linus Torvalds 已提交
1283

1284 1285
static struct cmd_rcvr *find_cmd_rcvr(ipmi_smi_t    intf,
				      unsigned char netfn,
1286 1287
				      unsigned char cmd,
				      unsigned char chan)
1288 1289 1290 1291
{
	struct cmd_rcvr *rcvr;

	list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1292 1293
		if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
					&& (rcvr->chans & (1 << chan)))
1294 1295 1296 1297 1298
			return rcvr;
	}
	return NULL;
}

1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
static int is_cmd_rcvr_exclusive(ipmi_smi_t    intf,
				 unsigned char netfn,
				 unsigned char cmd,
				 unsigned int  chans)
{
	struct cmd_rcvr *rcvr;

	list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
		if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
					&& (rcvr->chans & chans))
			return 0;
	}
	return 1;
}

L
Linus Torvalds 已提交
1314 1315
int ipmi_register_for_cmd(ipmi_user_t   user,
			  unsigned char netfn,
1316 1317
			  unsigned char cmd,
			  unsigned int  chans)
L
Linus Torvalds 已提交
1318
{
1319 1320 1321
	ipmi_smi_t      intf = user->intf;
	struct cmd_rcvr *rcvr;
	int             rv = 0;
L
Linus Torvalds 已提交
1322 1323 1324


	rcvr = kmalloc(sizeof(*rcvr), GFP_KERNEL);
1325
	if (!rcvr)
L
Linus Torvalds 已提交
1326
		return -ENOMEM;
1327 1328
	rcvr->cmd = cmd;
	rcvr->netfn = netfn;
1329
	rcvr->chans = chans;
1330
	rcvr->user = user;
L
Linus Torvalds 已提交
1331

1332
	mutex_lock(&intf->cmd_rcvrs_mutex);
L
Linus Torvalds 已提交
1333
	/* Make sure the command/netfn is not already registered. */
1334
	if (!is_cmd_rcvr_exclusive(intf, netfn, cmd, chans)) {
1335 1336
		rv = -EBUSY;
		goto out_unlock;
L
Linus Torvalds 已提交
1337
	}
1338

1339 1340 1341
	if (atomic_inc_return(&intf->event_waiters) == 1)
		need_waiter(intf);

1342
	list_add_rcu(&rcvr->link, &intf->cmd_rcvrs);
L
Linus Torvalds 已提交
1343

1344
 out_unlock:
1345
	mutex_unlock(&intf->cmd_rcvrs_mutex);
L
Linus Torvalds 已提交
1346 1347 1348 1349 1350
	if (rv)
		kfree(rcvr);

	return rv;
}
1351
EXPORT_SYMBOL(ipmi_register_for_cmd);
L
Linus Torvalds 已提交
1352 1353 1354

int ipmi_unregister_for_cmd(ipmi_user_t   user,
			    unsigned char netfn,
1355 1356
			    unsigned char cmd,
			    unsigned int  chans)
L
Linus Torvalds 已提交
1357
{
1358 1359
	ipmi_smi_t      intf = user->intf;
	struct cmd_rcvr *rcvr;
1360 1361
	struct cmd_rcvr *rcvrs = NULL;
	int i, rv = -ENOENT;
L
Linus Torvalds 已提交
1362

1363
	mutex_lock(&intf->cmd_rcvrs_mutex);
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
	for (i = 0; i < IPMI_NUM_CHANNELS; i++) {
		if (((1 << i) & chans) == 0)
			continue;
		rcvr = find_cmd_rcvr(intf, netfn, cmd, i);
		if (rcvr == NULL)
			continue;
		if (rcvr->user == user) {
			rv = 0;
			rcvr->chans &= ~chans;
			if (rcvr->chans == 0) {
				list_del_rcu(&rcvr->link);
				rcvr->next = rcvrs;
				rcvrs = rcvr;
			}
		}
	}
	mutex_unlock(&intf->cmd_rcvrs_mutex);
	synchronize_rcu();
	while (rcvrs) {
1383
		atomic_dec(&intf->event_waiters);
1384 1385
		rcvr = rcvrs;
		rcvrs = rcvr->next;
1386
		kfree(rcvr);
L
Linus Torvalds 已提交
1387
	}
1388
	return rv;
L
Linus Torvalds 已提交
1389
}
1390
EXPORT_SYMBOL(ipmi_unregister_for_cmd);
L
Linus Torvalds 已提交
1391 1392 1393 1394 1395

static unsigned char
ipmb_checksum(unsigned char *data, int size)
{
	unsigned char csum = 0;
1396

L
Linus Torvalds 已提交
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
	for (; size > 0; size--, data++)
		csum += *data;

	return -csum;
}

static inline void format_ipmb_msg(struct ipmi_smi_msg   *smi_msg,
				   struct kernel_ipmi_msg *msg,
				   struct ipmi_ipmb_addr *ipmb_addr,
				   long                  msgid,
				   unsigned char         ipmb_seq,
				   int                   broadcast,
				   unsigned char         source_address,
				   unsigned char         source_lun)
{
	int i = broadcast;

	/* Format the IPMB header data. */
	smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
	smi_msg->data[1] = IPMI_SEND_MSG_CMD;
	smi_msg->data[2] = ipmb_addr->channel;
	if (broadcast)
		smi_msg->data[3] = 0;
	smi_msg->data[i+3] = ipmb_addr->slave_addr;
	smi_msg->data[i+4] = (msg->netfn << 2) | (ipmb_addr->lun & 0x3);
	smi_msg->data[i+5] = ipmb_checksum(&(smi_msg->data[i+3]), 2);
	smi_msg->data[i+6] = source_address;
	smi_msg->data[i+7] = (ipmb_seq << 2) | source_lun;
	smi_msg->data[i+8] = msg->cmd;

	/* Now tack on the data to the message. */
	if (msg->data_len > 0)
		memcpy(&(smi_msg->data[i+9]), msg->data,
		       msg->data_len);
	smi_msg->data_size = msg->data_len + 9;

	/* Now calculate the checksum and tack it on. */
	smi_msg->data[i+smi_msg->data_size]
		= ipmb_checksum(&(smi_msg->data[i+6]),
				smi_msg->data_size-6);

1438 1439 1440 1441
	/*
	 * Add on the checksum size and the offset from the
	 * broadcast.
	 */
L
Linus Torvalds 已提交
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
	smi_msg->data_size += 1 + i;

	smi_msg->msgid = msgid;
}

static inline void format_lan_msg(struct ipmi_smi_msg   *smi_msg,
				  struct kernel_ipmi_msg *msg,
				  struct ipmi_lan_addr  *lan_addr,
				  long                  msgid,
				  unsigned char         ipmb_seq,
				  unsigned char         source_lun)
{
	/* Format the IPMB header data. */
	smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
	smi_msg->data[1] = IPMI_SEND_MSG_CMD;
	smi_msg->data[2] = lan_addr->channel;
	smi_msg->data[3] = lan_addr->session_handle;
	smi_msg->data[4] = lan_addr->remote_SWID;
	smi_msg->data[5] = (msg->netfn << 2) | (lan_addr->lun & 0x3);
	smi_msg->data[6] = ipmb_checksum(&(smi_msg->data[4]), 2);
	smi_msg->data[7] = lan_addr->local_SWID;
	smi_msg->data[8] = (ipmb_seq << 2) | source_lun;
	smi_msg->data[9] = msg->cmd;

	/* Now tack on the data to the message. */
	if (msg->data_len > 0)
		memcpy(&(smi_msg->data[10]), msg->data,
		       msg->data_len);
	smi_msg->data_size = msg->data_len + 10;

	/* Now calculate the checksum and tack it on. */
	smi_msg->data[smi_msg->data_size]
		= ipmb_checksum(&(smi_msg->data[7]),
				smi_msg->data_size-7);

1477 1478 1479 1480
	/*
	 * Add on the checksum size and the offset from the
	 * broadcast.
	 */
L
Linus Torvalds 已提交
1481 1482 1483 1484 1485
	smi_msg->data_size += 1;

	smi_msg->msgid = msgid;
}

A
Arnd Bergmann 已提交
1486 1487 1488
static struct ipmi_smi_msg *smi_add_send_msg(ipmi_smi_t intf,
					     struct ipmi_smi_msg *smi_msg,
					     int priority)
1489
{
1490 1491 1492 1493 1494 1495 1496 1497 1498
	if (intf->curr_msg) {
		if (priority > 0)
			list_add_tail(&smi_msg->link, &intf->hp_xmit_msgs);
		else
			list_add_tail(&smi_msg->link, &intf->xmit_msgs);
		smi_msg = NULL;
	} else {
		intf->curr_msg = smi_msg;
	}
A
Arnd Bergmann 已提交
1499 1500 1501 1502 1503

	return smi_msg;
}


1504
static void smi_send(ipmi_smi_t intf, const struct ipmi_smi_handlers *handlers,
A
Arnd Bergmann 已提交
1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
		     struct ipmi_smi_msg *smi_msg, int priority)
{
	int run_to_completion = intf->run_to_completion;

	if (run_to_completion) {
		smi_msg = smi_add_send_msg(intf, smi_msg, priority);
	} else {
		unsigned long flags;

		spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
		smi_msg = smi_add_send_msg(intf, smi_msg, priority);
1516
		spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
A
Arnd Bergmann 已提交
1517
	}
1518 1519

	if (smi_msg)
1520
		handlers->sender(intf->send_info, smi_msg);
1521 1522
}

1523 1524 1525 1526 1527 1528
/*
 * Separate from ipmi_request so that the user does not have to be
 * supplied in certain circumstances (mainly at panic time).  If
 * messages are supplied, they will be freed, even if an error
 * occurs.
 */
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
static int i_ipmi_request(ipmi_user_t          user,
			  ipmi_smi_t           intf,
			  struct ipmi_addr     *addr,
			  long                 msgid,
			  struct kernel_ipmi_msg *msg,
			  void                 *user_msg_data,
			  void                 *supplied_smi,
			  struct ipmi_recv_msg *supplied_recv,
			  int                  priority,
			  unsigned char        source_address,
			  unsigned char        source_lun,
			  int                  retries,
			  unsigned int         retry_time_ms)
L
Linus Torvalds 已提交
1542
{
1543 1544 1545 1546
	int                      rv = 0;
	struct ipmi_smi_msg      *smi_msg;
	struct ipmi_recv_msg     *recv_msg;
	unsigned long            flags;
L
Linus Torvalds 已提交
1547 1548


1549
	if (supplied_recv)
L
Linus Torvalds 已提交
1550
		recv_msg = supplied_recv;
1551
	else {
L
Linus Torvalds 已提交
1552
		recv_msg = ipmi_alloc_recv_msg();
1553
		if (recv_msg == NULL)
L
Linus Torvalds 已提交
1554 1555 1556 1557
			return -ENOMEM;
	}
	recv_msg->user_msg_data = user_msg_data;

1558
	if (supplied_smi)
L
Linus Torvalds 已提交
1559
		smi_msg = (struct ipmi_smi_msg *) supplied_smi;
1560
	else {
L
Linus Torvalds 已提交
1561 1562 1563 1564 1565 1566 1567
		smi_msg = ipmi_alloc_smi_msg();
		if (smi_msg == NULL) {
			ipmi_free_recv_msg(recv_msg);
			return -ENOMEM;
		}
	}

1568
	rcu_read_lock();
1569
	if (intf->in_shutdown) {
1570 1571 1572 1573
		rv = -ENODEV;
		goto out_err;
	}

L
Linus Torvalds 已提交
1574
	recv_msg->user = user;
1575 1576
	if (user)
		kref_get(&user->refcount);
L
Linus Torvalds 已提交
1577
	recv_msg->msgid = msgid;
1578 1579 1580 1581
	/*
	 * Store the message to send in the receive message so timeout
	 * responses can get the proper response data.
	 */
L
Linus Torvalds 已提交
1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
	recv_msg->msg = *msg;

	if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
		struct ipmi_system_interface_addr *smi_addr;

		if (msg->netfn & 1) {
			/* Responses are not allowed to the SMI. */
			rv = -EINVAL;
			goto out_err;
		}

		smi_addr = (struct ipmi_system_interface_addr *) addr;
		if (smi_addr->lun > 3) {
1595
			ipmi_inc_stat(intf, sent_invalid_commands);
L
Linus Torvalds 已提交
1596 1597 1598 1599 1600 1601 1602 1603 1604
			rv = -EINVAL;
			goto out_err;
		}

		memcpy(&recv_msg->addr, smi_addr, sizeof(*smi_addr));

		if ((msg->netfn == IPMI_NETFN_APP_REQUEST)
		    && ((msg->cmd == IPMI_SEND_MSG_CMD)
			|| (msg->cmd == IPMI_GET_MSG_CMD)
1605 1606 1607 1608 1609
			|| (msg->cmd == IPMI_READ_EVENT_MSG_BUFFER_CMD))) {
			/*
			 * We don't let the user do these, since we manage
			 * the sequence numbers.
			 */
1610
			ipmi_inc_stat(intf, sent_invalid_commands);
L
Linus Torvalds 已提交
1611 1612 1613 1614
			rv = -EINVAL;
			goto out_err;
		}

C
Corey Minyard 已提交
1615 1616 1617
		if (((msg->netfn == IPMI_NETFN_APP_REQUEST)
		      && ((msg->cmd == IPMI_COLD_RESET_CMD)
			  || (msg->cmd == IPMI_WARM_RESET_CMD)))
1618
		     || (msg->netfn == IPMI_NETFN_FIRMWARE_REQUEST)) {
C
Corey Minyard 已提交
1619 1620 1621 1622
			spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
			intf->auto_maintenance_timeout
				= IPMI_MAINTENANCE_MODE_TIMEOUT;
			if (!intf->maintenance_mode
1623
			    && !intf->maintenance_mode_enable) {
C
Corey Minyard 已提交
1624
				intf->maintenance_mode_enable = true;
C
Corey Minyard 已提交
1625 1626 1627 1628 1629 1630
				maintenance_mode_update(intf);
			}
			spin_unlock_irqrestore(&intf->maintenance_mode_lock,
					       flags);
		}

L
Linus Torvalds 已提交
1631
		if ((msg->data_len + 2) > IPMI_MAX_MSG_LENGTH) {
1632
			ipmi_inc_stat(intf, sent_invalid_commands);
L
Linus Torvalds 已提交
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
			rv = -EMSGSIZE;
			goto out_err;
		}

		smi_msg->data[0] = (msg->netfn << 2) | (smi_addr->lun & 0x3);
		smi_msg->data[1] = msg->cmd;
		smi_msg->msgid = msgid;
		smi_msg->user_data = recv_msg;
		if (msg->data_len > 0)
			memcpy(&(smi_msg->data[2]), msg->data, msg->data_len);
		smi_msg->data_size = msg->data_len + 2;
1644
		ipmi_inc_stat(intf, sent_local_commands);
1645
	} else if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
L
Linus Torvalds 已提交
1646 1647 1648 1649 1650
		struct ipmi_ipmb_addr *ipmb_addr;
		unsigned char         ipmb_seq;
		long                  seqid;
		int                   broadcast = 0;

1651
		if (addr->channel >= IPMI_MAX_CHANNELS) {
1652
			ipmi_inc_stat(intf, sent_invalid_commands);
L
Linus Torvalds 已提交
1653 1654 1655 1656 1657
			rv = -EINVAL;
			goto out_err;
		}

		if (intf->channels[addr->channel].medium
1658
					!= IPMI_CHANNEL_MEDIUM_IPMB) {
1659
			ipmi_inc_stat(intf, sent_invalid_commands);
L
Linus Torvalds 已提交
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
			rv = -EINVAL;
			goto out_err;
		}

		if (retries < 0) {
		    if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE)
			retries = 0; /* Don't retry broadcasts. */
		    else
			retries = 4;
		}
		if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) {
1671 1672 1673 1674 1675
		    /*
		     * Broadcasts add a zero at the beginning of the
		     * message, but otherwise is the same as an IPMB
		     * address.
		     */
L
Linus Torvalds 已提交
1676 1677 1678 1679 1680 1681 1682 1683 1684
		    addr->addr_type = IPMI_IPMB_ADDR_TYPE;
		    broadcast = 1;
		}


		/* Default to 1 second retries. */
		if (retry_time_ms == 0)
		    retry_time_ms = 1000;

1685 1686 1687 1688
		/*
		 * 9 for the header and 1 for the checksum, plus
		 * possibly one for the broadcast.
		 */
L
Linus Torvalds 已提交
1689
		if ((msg->data_len + 10 + broadcast) > IPMI_MAX_MSG_LENGTH) {
1690
			ipmi_inc_stat(intf, sent_invalid_commands);
L
Linus Torvalds 已提交
1691 1692 1693 1694 1695 1696
			rv = -EMSGSIZE;
			goto out_err;
		}

		ipmb_addr = (struct ipmi_ipmb_addr *) addr;
		if (ipmb_addr->lun > 3) {
1697
			ipmi_inc_stat(intf, sent_invalid_commands);
L
Linus Torvalds 已提交
1698 1699 1700 1701 1702 1703 1704
			rv = -EINVAL;
			goto out_err;
		}

		memcpy(&recv_msg->addr, ipmb_addr, sizeof(*ipmb_addr));

		if (recv_msg->msg.netfn & 0x1) {
1705 1706 1707 1708
			/*
			 * It's a response, so use the user's sequence
			 * from msgid.
			 */
1709
			ipmi_inc_stat(intf, sent_ipmb_responses);
L
Linus Torvalds 已提交
1710 1711 1712 1713
			format_ipmb_msg(smi_msg, msg, ipmb_addr, msgid,
					msgid, broadcast,
					source_address, source_lun);

1714 1715 1716 1717
			/*
			 * Save the receive message so we can use it
			 * to deliver the response.
			 */
L
Linus Torvalds 已提交
1718 1719 1720 1721 1722 1723
			smi_msg->user_data = recv_msg;
		} else {
			/* It's a command, so get a sequence for it. */

			spin_lock_irqsave(&(intf->seq_lock), flags);

1724 1725 1726 1727
			/*
			 * Create a sequence number with a 1 second
			 * timeout and 4 retries.
			 */
L
Linus Torvalds 已提交
1728 1729 1730 1731 1732 1733 1734 1735
			rv = intf_next_seq(intf,
					   recv_msg,
					   retry_time_ms,
					   retries,
					   broadcast,
					   &ipmb_seq,
					   &seqid);
			if (rv) {
1736 1737 1738 1739
				/*
				 * We have used up all the sequence numbers,
				 * probably, so abort.
				 */
L
Linus Torvalds 已提交
1740 1741 1742 1743 1744
				spin_unlock_irqrestore(&(intf->seq_lock),
						       flags);
				goto out_err;
			}

1745 1746
			ipmi_inc_stat(intf, sent_ipmb_commands);

1747 1748 1749 1750 1751
			/*
			 * Store the sequence number in the message,
			 * so that when the send message response
			 * comes back we can start the timer.
			 */
L
Linus Torvalds 已提交
1752 1753 1754 1755 1756
			format_ipmb_msg(smi_msg, msg, ipmb_addr,
					STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
					ipmb_seq, broadcast,
					source_address, source_lun);

1757 1758 1759 1760
			/*
			 * Copy the message into the recv message data, so we
			 * can retransmit it later if necessary.
			 */
L
Linus Torvalds 已提交
1761 1762 1763 1764 1765
			memcpy(recv_msg->msg_data, smi_msg->data,
			       smi_msg->data_size);
			recv_msg->msg.data = recv_msg->msg_data;
			recv_msg->msg.data_len = smi_msg->data_size;

1766 1767 1768 1769 1770 1771 1772 1773
			/*
			 * We don't unlock until here, because we need
			 * to copy the completed message into the
			 * recv_msg before we release the lock.
			 * Otherwise, race conditions may bite us.  I
			 * know that's pretty paranoid, but I prefer
			 * to be correct.
			 */
L
Linus Torvalds 已提交
1774 1775
			spin_unlock_irqrestore(&(intf->seq_lock), flags);
		}
1776
	} else if (is_lan_addr(addr)) {
L
Linus Torvalds 已提交
1777 1778 1779 1780
		struct ipmi_lan_addr  *lan_addr;
		unsigned char         ipmb_seq;
		long                  seqid;

1781
		if (addr->channel >= IPMI_MAX_CHANNELS) {
1782
			ipmi_inc_stat(intf, sent_invalid_commands);
L
Linus Torvalds 已提交
1783 1784 1785 1786 1787
			rv = -EINVAL;
			goto out_err;
		}

		if ((intf->channels[addr->channel].medium
1788
				!= IPMI_CHANNEL_MEDIUM_8023LAN)
L
Linus Torvalds 已提交
1789
		    && (intf->channels[addr->channel].medium
1790
				!= IPMI_CHANNEL_MEDIUM_ASYNC)) {
1791
			ipmi_inc_stat(intf, sent_invalid_commands);
L
Linus Torvalds 已提交
1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803
			rv = -EINVAL;
			goto out_err;
		}

		retries = 4;

		/* Default to 1 second retries. */
		if (retry_time_ms == 0)
		    retry_time_ms = 1000;

		/* 11 for the header and 1 for the checksum. */
		if ((msg->data_len + 12) > IPMI_MAX_MSG_LENGTH) {
1804
			ipmi_inc_stat(intf, sent_invalid_commands);
L
Linus Torvalds 已提交
1805 1806 1807 1808 1809 1810
			rv = -EMSGSIZE;
			goto out_err;
		}

		lan_addr = (struct ipmi_lan_addr *) addr;
		if (lan_addr->lun > 3) {
1811
			ipmi_inc_stat(intf, sent_invalid_commands);
L
Linus Torvalds 已提交
1812 1813 1814 1815 1816 1817 1818
			rv = -EINVAL;
			goto out_err;
		}

		memcpy(&recv_msg->addr, lan_addr, sizeof(*lan_addr));

		if (recv_msg->msg.netfn & 0x1) {
1819 1820 1821 1822
			/*
			 * It's a response, so use the user's sequence
			 * from msgid.
			 */
1823
			ipmi_inc_stat(intf, sent_lan_responses);
L
Linus Torvalds 已提交
1824 1825 1826
			format_lan_msg(smi_msg, msg, lan_addr, msgid,
				       msgid, source_lun);

1827 1828 1829 1830
			/*
			 * Save the receive message so we can use it
			 * to deliver the response.
			 */
L
Linus Torvalds 已提交
1831 1832 1833 1834 1835 1836
			smi_msg->user_data = recv_msg;
		} else {
			/* It's a command, so get a sequence for it. */

			spin_lock_irqsave(&(intf->seq_lock), flags);

1837 1838 1839 1840
			/*
			 * Create a sequence number with a 1 second
			 * timeout and 4 retries.
			 */
L
Linus Torvalds 已提交
1841 1842 1843 1844 1845 1846 1847 1848
			rv = intf_next_seq(intf,
					   recv_msg,
					   retry_time_ms,
					   retries,
					   0,
					   &ipmb_seq,
					   &seqid);
			if (rv) {
1849 1850 1851 1852
				/*
				 * We have used up all the sequence numbers,
				 * probably, so abort.
				 */
L
Linus Torvalds 已提交
1853 1854 1855 1856 1857
				spin_unlock_irqrestore(&(intf->seq_lock),
						       flags);
				goto out_err;
			}

1858 1859
			ipmi_inc_stat(intf, sent_lan_commands);

1860 1861 1862 1863 1864
			/*
			 * Store the sequence number in the message,
			 * so that when the send message response
			 * comes back we can start the timer.
			 */
L
Linus Torvalds 已提交
1865 1866 1867 1868
			format_lan_msg(smi_msg, msg, lan_addr,
				       STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
				       ipmb_seq, source_lun);

1869 1870 1871 1872
			/*
			 * Copy the message into the recv message data, so we
			 * can retransmit it later if necessary.
			 */
L
Linus Torvalds 已提交
1873 1874 1875 1876 1877
			memcpy(recv_msg->msg_data, smi_msg->data,
			       smi_msg->data_size);
			recv_msg->msg.data = recv_msg->msg_data;
			recv_msg->msg.data_len = smi_msg->data_size;

1878 1879 1880 1881 1882 1883 1884 1885
			/*
			 * We don't unlock until here, because we need
			 * to copy the completed message into the
			 * recv_msg before we release the lock.
			 * Otherwise, race conditions may bite us.  I
			 * know that's pretty paranoid, but I prefer
			 * to be correct.
			 */
L
Linus Torvalds 已提交
1886 1887 1888 1889
			spin_unlock_irqrestore(&(intf->seq_lock), flags);
		}
	} else {
	    /* Unknown address type. */
1890
		ipmi_inc_stat(intf, sent_invalid_commands);
L
Linus Torvalds 已提交
1891 1892 1893 1894 1895 1896 1897
		rv = -EINVAL;
		goto out_err;
	}

#ifdef DEBUG_MSGING
	{
		int m;
C
Corey Minyard 已提交
1898
		for (m = 0; m < smi_msg->data_size; m++)
L
Linus Torvalds 已提交
1899 1900 1901 1902
			printk(" %2.2x", smi_msg->data[m]);
		printk("\n");
	}
#endif
1903

1904
	smi_send(intf, intf->handlers, smi_msg, priority);
1905
	rcu_read_unlock();
L
Linus Torvalds 已提交
1906 1907 1908 1909

	return 0;

 out_err:
1910
	rcu_read_unlock();
L
Linus Torvalds 已提交
1911 1912 1913 1914 1915
	ipmi_free_smi_msg(smi_msg);
	ipmi_free_recv_msg(recv_msg);
	return rv;
}

1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927
static int check_addr(ipmi_smi_t       intf,
		      struct ipmi_addr *addr,
		      unsigned char    *saddr,
		      unsigned char    *lun)
{
	if (addr->channel >= IPMI_MAX_CHANNELS)
		return -EINVAL;
	*lun = intf->channels[addr->channel].lun;
	*saddr = intf->channels[addr->channel].address;
	return 0;
}

L
Linus Torvalds 已提交
1928 1929 1930 1931 1932 1933 1934 1935 1936
int ipmi_request_settime(ipmi_user_t      user,
			 struct ipmi_addr *addr,
			 long             msgid,
			 struct kernel_ipmi_msg  *msg,
			 void             *user_msg_data,
			 int              priority,
			 int              retries,
			 unsigned int     retry_time_ms)
{
1937
	unsigned char saddr = 0, lun = 0;
1938 1939
	int           rv;

1940
	if (!user)
1941
		return -EINVAL;
1942 1943 1944
	rv = check_addr(user->intf, addr, &saddr, &lun);
	if (rv)
		return rv;
L
Linus Torvalds 已提交
1945 1946 1947 1948 1949 1950 1951 1952
	return i_ipmi_request(user,
			      user->intf,
			      addr,
			      msgid,
			      msg,
			      user_msg_data,
			      NULL, NULL,
			      priority,
1953 1954
			      saddr,
			      lun,
L
Linus Torvalds 已提交
1955 1956 1957
			      retries,
			      retry_time_ms);
}
1958
EXPORT_SYMBOL(ipmi_request_settime);
L
Linus Torvalds 已提交
1959 1960 1961 1962 1963 1964 1965 1966 1967 1968

int ipmi_request_supply_msgs(ipmi_user_t          user,
			     struct ipmi_addr     *addr,
			     long                 msgid,
			     struct kernel_ipmi_msg *msg,
			     void                 *user_msg_data,
			     void                 *supplied_smi,
			     struct ipmi_recv_msg *supplied_recv,
			     int                  priority)
{
1969
	unsigned char saddr = 0, lun = 0;
1970 1971
	int           rv;

1972
	if (!user)
1973
		return -EINVAL;
1974 1975 1976
	rv = check_addr(user->intf, addr, &saddr, &lun);
	if (rv)
		return rv;
L
Linus Torvalds 已提交
1977 1978 1979 1980 1981 1982 1983 1984 1985
	return i_ipmi_request(user,
			      user->intf,
			      addr,
			      msgid,
			      msg,
			      user_msg_data,
			      supplied_smi,
			      supplied_recv,
			      priority,
1986 1987
			      saddr,
			      lun,
L
Linus Torvalds 已提交
1988 1989
			      -1, 0);
}
1990
EXPORT_SYMBOL(ipmi_request_supply_msgs);
L
Linus Torvalds 已提交
1991

1992
#ifdef CONFIG_PROC_FS
1993
static int smi_ipmb_proc_show(struct seq_file *m, void *v)
L
Linus Torvalds 已提交
1994
{
1995
	ipmi_smi_t intf = m->private;
1996
	int        i;
L
Linus Torvalds 已提交
1997

1998 1999 2000
	seq_printf(m, "%x", intf->channels[0].address);
	for (i = 1; i < IPMI_MAX_CHANNELS; i++)
		seq_printf(m, " %x", intf->channels[i].address);
2001 2002
	seq_putc(m, '\n');

2003
	return 0;
L
Linus Torvalds 已提交
2004 2005
}

2006
static int smi_ipmb_proc_open(struct inode *inode, struct file *file)
L
Linus Torvalds 已提交
2007
{
A
Al Viro 已提交
2008
	return single_open(file, smi_ipmb_proc_show, PDE_DATA(inode));
2009
}
L
Linus Torvalds 已提交
2010

2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021
static const struct file_operations smi_ipmb_proc_ops = {
	.open		= smi_ipmb_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

static int smi_version_proc_show(struct seq_file *m, void *v)
{
	ipmi_smi_t intf = m->private;

2022 2023 2024 2025
	seq_printf(m, "%u.%u\n",
		   ipmi_version_major(&intf->bmc->id),
		   ipmi_version_minor(&intf->bmc->id));

2026
	return 0;
L
Linus Torvalds 已提交
2027 2028
}

2029
static int smi_version_proc_open(struct inode *inode, struct file *file)
L
Linus Torvalds 已提交
2030
{
A
Al Viro 已提交
2031
	return single_open(file, smi_version_proc_show, PDE_DATA(inode));
2032 2033 2034 2035 2036 2037 2038 2039
}

static const struct file_operations smi_version_proc_ops = {
	.open		= smi_version_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};
L
Linus Torvalds 已提交
2040

2041 2042 2043 2044 2045
static int smi_stats_proc_show(struct seq_file *m, void *v)
{
	ipmi_smi_t intf = m->private;

	seq_printf(m, "sent_invalid_commands:       %u\n",
2046
		       ipmi_get_stat(intf, sent_invalid_commands));
2047
	seq_printf(m, "sent_local_commands:         %u\n",
2048
		       ipmi_get_stat(intf, sent_local_commands));
2049
	seq_printf(m, "handled_local_responses:     %u\n",
2050
		       ipmi_get_stat(intf, handled_local_responses));
2051
	seq_printf(m, "unhandled_local_responses:   %u\n",
2052
		       ipmi_get_stat(intf, unhandled_local_responses));
2053
	seq_printf(m, "sent_ipmb_commands:          %u\n",
2054
		       ipmi_get_stat(intf, sent_ipmb_commands));
2055
	seq_printf(m, "sent_ipmb_command_errs:      %u\n",
2056
		       ipmi_get_stat(intf, sent_ipmb_command_errs));
2057
	seq_printf(m, "retransmitted_ipmb_commands: %u\n",
2058
		       ipmi_get_stat(intf, retransmitted_ipmb_commands));
2059
	seq_printf(m, "timed_out_ipmb_commands:     %u\n",
2060
		       ipmi_get_stat(intf, timed_out_ipmb_commands));
2061
	seq_printf(m, "timed_out_ipmb_broadcasts:   %u\n",
2062
		       ipmi_get_stat(intf, timed_out_ipmb_broadcasts));
2063
	seq_printf(m, "sent_ipmb_responses:         %u\n",
2064
		       ipmi_get_stat(intf, sent_ipmb_responses));
2065
	seq_printf(m, "handled_ipmb_responses:      %u\n",
2066
		       ipmi_get_stat(intf, handled_ipmb_responses));
2067
	seq_printf(m, "invalid_ipmb_responses:      %u\n",
2068
		       ipmi_get_stat(intf, invalid_ipmb_responses));
2069
	seq_printf(m, "unhandled_ipmb_responses:    %u\n",
2070
		       ipmi_get_stat(intf, unhandled_ipmb_responses));
2071
	seq_printf(m, "sent_lan_commands:           %u\n",
2072
		       ipmi_get_stat(intf, sent_lan_commands));
2073
	seq_printf(m, "sent_lan_command_errs:       %u\n",
2074
		       ipmi_get_stat(intf, sent_lan_command_errs));
2075
	seq_printf(m, "retransmitted_lan_commands:  %u\n",
2076
		       ipmi_get_stat(intf, retransmitted_lan_commands));
2077
	seq_printf(m, "timed_out_lan_commands:      %u\n",
2078
		       ipmi_get_stat(intf, timed_out_lan_commands));
2079
	seq_printf(m, "sent_lan_responses:          %u\n",
2080
		       ipmi_get_stat(intf, sent_lan_responses));
2081
	seq_printf(m, "handled_lan_responses:       %u\n",
2082
		       ipmi_get_stat(intf, handled_lan_responses));
2083
	seq_printf(m, "invalid_lan_responses:       %u\n",
2084
		       ipmi_get_stat(intf, invalid_lan_responses));
2085
	seq_printf(m, "unhandled_lan_responses:     %u\n",
2086
		       ipmi_get_stat(intf, unhandled_lan_responses));
2087
	seq_printf(m, "handled_commands:            %u\n",
2088
		       ipmi_get_stat(intf, handled_commands));
2089
	seq_printf(m, "invalid_commands:            %u\n",
2090
		       ipmi_get_stat(intf, invalid_commands));
2091
	seq_printf(m, "unhandled_commands:          %u\n",
2092
		       ipmi_get_stat(intf, unhandled_commands));
2093
	seq_printf(m, "invalid_events:              %u\n",
2094
		       ipmi_get_stat(intf, invalid_events));
2095
	seq_printf(m, "events:                      %u\n",
2096
		       ipmi_get_stat(intf, events));
2097
	seq_printf(m, "failed rexmit LAN msgs:      %u\n",
2098
		       ipmi_get_stat(intf, dropped_rexmit_lan_commands));
2099
	seq_printf(m, "failed rexmit IPMB msgs:     %u\n",
2100
		       ipmi_get_stat(intf, dropped_rexmit_ipmb_commands));
2101 2102
	return 0;
}
L
Linus Torvalds 已提交
2103

2104 2105
static int smi_stats_proc_open(struct inode *inode, struct file *file)
{
A
Al Viro 已提交
2106
	return single_open(file, smi_stats_proc_show, PDE_DATA(inode));
L
Linus Torvalds 已提交
2107
}
2108 2109 2110 2111 2112 2113 2114

static const struct file_operations smi_stats_proc_ops = {
	.open		= smi_stats_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};
2115
#endif /* CONFIG_PROC_FS */
L
Linus Torvalds 已提交
2116 2117

int ipmi_smi_add_proc_entry(ipmi_smi_t smi, char *name,
2118
			    const struct file_operations *proc_ops,
2119
			    void *data)
L
Linus Torvalds 已提交
2120 2121
{
	int                    rv = 0;
2122 2123
#ifdef CONFIG_PROC_FS
	struct proc_dir_entry  *file;
L
Linus Torvalds 已提交
2124 2125 2126 2127 2128 2129
	struct ipmi_proc_entry *entry;

	/* Create a list element. */
	entry = kmalloc(sizeof(*entry), GFP_KERNEL);
	if (!entry)
		return -ENOMEM;
2130
	entry->name = kstrdup(name, GFP_KERNEL);
L
Linus Torvalds 已提交
2131 2132 2133 2134 2135
	if (!entry->name) {
		kfree(entry);
		return -ENOMEM;
	}

2136
	file = proc_create_data(name, 0, smi->proc_dir, proc_ops, data);
L
Linus Torvalds 已提交
2137 2138 2139 2140 2141
	if (!file) {
		kfree(entry->name);
		kfree(entry);
		rv = -ENOMEM;
	} else {
C
Corey Minyard 已提交
2142
		mutex_lock(&smi->proc_entry_lock);
L
Linus Torvalds 已提交
2143 2144 2145
		/* Stick it on the list. */
		entry->next = smi->proc_entries;
		smi->proc_entries = entry;
C
Corey Minyard 已提交
2146
		mutex_unlock(&smi->proc_entry_lock);
L
Linus Torvalds 已提交
2147
	}
2148
#endif /* CONFIG_PROC_FS */
L
Linus Torvalds 已提交
2149 2150 2151

	return rv;
}
2152
EXPORT_SYMBOL(ipmi_smi_add_proc_entry);
L
Linus Torvalds 已提交
2153 2154 2155 2156 2157

static int add_proc_entries(ipmi_smi_t smi, int num)
{
	int rv = 0;

2158
#ifdef CONFIG_PROC_FS
L
Linus Torvalds 已提交
2159 2160 2161 2162 2163 2164 2165
	sprintf(smi->proc_dir_name, "%d", num);
	smi->proc_dir = proc_mkdir(smi->proc_dir_name, proc_ipmi_root);
	if (!smi->proc_dir)
		rv = -ENOMEM;

	if (rv == 0)
		rv = ipmi_smi_add_proc_entry(smi, "stats",
2166
					     &smi_stats_proc_ops,
2167
					     smi);
L
Linus Torvalds 已提交
2168 2169 2170

	if (rv == 0)
		rv = ipmi_smi_add_proc_entry(smi, "ipmb",
2171
					     &smi_ipmb_proc_ops,
2172
					     smi);
L
Linus Torvalds 已提交
2173 2174 2175

	if (rv == 0)
		rv = ipmi_smi_add_proc_entry(smi, "version",
2176
					     &smi_version_proc_ops,
2177
					     smi);
2178
#endif /* CONFIG_PROC_FS */
L
Linus Torvalds 已提交
2179 2180 2181 2182 2183 2184

	return rv;
}

static void remove_proc_entries(ipmi_smi_t smi)
{
2185
#ifdef CONFIG_PROC_FS
L
Linus Torvalds 已提交
2186 2187
	struct ipmi_proc_entry *entry;

C
Corey Minyard 已提交
2188
	mutex_lock(&smi->proc_entry_lock);
L
Linus Torvalds 已提交
2189 2190 2191 2192 2193 2194 2195 2196
	while (smi->proc_entries) {
		entry = smi->proc_entries;
		smi->proc_entries = entry->next;

		remove_proc_entry(entry->name, smi->proc_dir);
		kfree(entry->name);
		kfree(entry);
	}
C
Corey Minyard 已提交
2197
	mutex_unlock(&smi->proc_entry_lock);
L
Linus Torvalds 已提交
2198
	remove_proc_entry(smi->proc_dir_name, proc_ipmi_root);
2199
#endif /* CONFIG_PROC_FS */
L
Linus Torvalds 已提交
2200 2201
}

2202 2203 2204
static int __find_bmc_guid(struct device *dev, void *data)
{
	unsigned char *id = data;
2205
	struct bmc_device *bmc = to_bmc_device(dev);
2206 2207 2208 2209 2210 2211 2212 2213 2214 2215
	return memcmp(bmc->guid, id, 16) == 0;
}

static struct bmc_device *ipmi_find_bmc_guid(struct device_driver *drv,
					     unsigned char *guid)
{
	struct device *dev;

	dev = driver_find_device(drv, NULL, guid, __find_bmc_guid);
	if (dev)
2216
		return to_bmc_device(dev);
2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228
	else
		return NULL;
}

struct prod_dev_id {
	unsigned int  product_id;
	unsigned char device_id;
};

static int __find_bmc_prod_dev_id(struct device *dev, void *data)
{
	struct prod_dev_id *id = data;
2229
	struct bmc_device *bmc = to_bmc_device(dev);
2230 2231 2232 2233 2234 2235 2236

	return (bmc->id.product_id == id->product_id
		&& bmc->id.device_id == id->device_id);
}

static struct bmc_device *ipmi_find_bmc_prod_dev_id(
	struct device_driver *drv,
2237
	unsigned int product_id, unsigned char device_id)
2238 2239 2240 2241 2242 2243 2244 2245 2246
{
	struct prod_dev_id id = {
		.product_id = product_id,
		.device_id = device_id,
	};
	struct device *dev;

	dev = driver_find_device(drv, NULL, &id, __find_bmc_prod_dev_id);
	if (dev)
2247
		return to_bmc_device(dev);
2248 2249 2250 2251 2252 2253 2254 2255
	else
		return NULL;
}

static ssize_t device_id_show(struct device *dev,
			      struct device_attribute *attr,
			      char *buf)
{
2256
	struct bmc_device *bmc = to_bmc_device(dev);
2257 2258 2259

	return snprintf(buf, 10, "%u\n", bmc->id.device_id);
}
2260
static DEVICE_ATTR(device_id, S_IRUGO, device_id_show, NULL);
2261

2262 2263 2264
static ssize_t provides_device_sdrs_show(struct device *dev,
					 struct device_attribute *attr,
					 char *buf)
2265
{
2266
	struct bmc_device *bmc = to_bmc_device(dev);
2267 2268

	return snprintf(buf, 10, "%u\n",
C
Corey Minyard 已提交
2269
			(bmc->id.device_revision & 0x80) >> 7);
2270
}
2271 2272
static DEVICE_ATTR(provides_device_sdrs, S_IRUGO, provides_device_sdrs_show,
		   NULL);
2273 2274 2275 2276

static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
			     char *buf)
{
2277
	struct bmc_device *bmc = to_bmc_device(dev);
2278 2279

	return snprintf(buf, 20, "%u\n",
C
Corey Minyard 已提交
2280
			bmc->id.device_revision & 0x0F);
2281
}
2282
static DEVICE_ATTR(revision, S_IRUGO, revision_show, NULL);
2283

2284 2285 2286
static ssize_t firmware_revision_show(struct device *dev,
				      struct device_attribute *attr,
				      char *buf)
2287
{
2288
	struct bmc_device *bmc = to_bmc_device(dev);
2289 2290 2291 2292

	return snprintf(buf, 20, "%u.%x\n", bmc->id.firmware_revision_1,
			bmc->id.firmware_revision_2);
}
2293
static DEVICE_ATTR(firmware_revision, S_IRUGO, firmware_revision_show, NULL);
2294 2295 2296 2297 2298

static ssize_t ipmi_version_show(struct device *dev,
				 struct device_attribute *attr,
				 char *buf)
{
2299
	struct bmc_device *bmc = to_bmc_device(dev);
2300 2301 2302 2303 2304

	return snprintf(buf, 20, "%u.%u\n",
			ipmi_version_major(&bmc->id),
			ipmi_version_minor(&bmc->id));
}
2305
static DEVICE_ATTR(ipmi_version, S_IRUGO, ipmi_version_show, NULL);
2306 2307 2308 2309 2310

static ssize_t add_dev_support_show(struct device *dev,
				    struct device_attribute *attr,
				    char *buf)
{
2311
	struct bmc_device *bmc = to_bmc_device(dev);
2312 2313 2314 2315

	return snprintf(buf, 10, "0x%02x\n",
			bmc->id.additional_device_support);
}
2316 2317
static DEVICE_ATTR(additional_device_support, S_IRUGO, add_dev_support_show,
		   NULL);
2318 2319 2320 2321 2322

static ssize_t manufacturer_id_show(struct device *dev,
				    struct device_attribute *attr,
				    char *buf)
{
2323
	struct bmc_device *bmc = to_bmc_device(dev);
2324 2325 2326

	return snprintf(buf, 20, "0x%6.6x\n", bmc->id.manufacturer_id);
}
2327
static DEVICE_ATTR(manufacturer_id, S_IRUGO, manufacturer_id_show, NULL);
2328 2329 2330 2331 2332

static ssize_t product_id_show(struct device *dev,
			       struct device_attribute *attr,
			       char *buf)
{
2333
	struct bmc_device *bmc = to_bmc_device(dev);
2334 2335 2336

	return snprintf(buf, 10, "0x%4.4x\n", bmc->id.product_id);
}
2337
static DEVICE_ATTR(product_id, S_IRUGO, product_id_show, NULL);
2338 2339 2340 2341 2342

static ssize_t aux_firmware_rev_show(struct device *dev,
				     struct device_attribute *attr,
				     char *buf)
{
2343
	struct bmc_device *bmc = to_bmc_device(dev);
2344 2345 2346 2347 2348 2349 2350

	return snprintf(buf, 21, "0x%02x 0x%02x 0x%02x 0x%02x\n",
			bmc->id.aux_firmware_revision[3],
			bmc->id.aux_firmware_revision[2],
			bmc->id.aux_firmware_revision[1],
			bmc->id.aux_firmware_revision[0]);
}
2351
static DEVICE_ATTR(aux_firmware_revision, S_IRUGO, aux_firmware_rev_show, NULL);
2352 2353 2354 2355

static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
			 char *buf)
{
2356
	struct bmc_device *bmc = to_bmc_device(dev);
2357 2358 2359 2360 2361

	return snprintf(buf, 100, "%Lx%Lx\n",
			(long long) bmc->guid[0],
			(long long) bmc->guid[8]);
}
2362
static DEVICE_ATTR(guid, S_IRUGO, guid_show, NULL);
2363 2364 2365 2366 2367 2368 2369 2370 2371 2372

static struct attribute *bmc_dev_attrs[] = {
	&dev_attr_device_id.attr,
	&dev_attr_provides_device_sdrs.attr,
	&dev_attr_revision.attr,
	&dev_attr_firmware_revision.attr,
	&dev_attr_ipmi_version.attr,
	&dev_attr_additional_device_support.attr,
	&dev_attr_manufacturer_id.attr,
	&dev_attr_product_id.attr,
2373 2374
	&dev_attr_aux_firmware_revision.attr,
	&dev_attr_guid.attr,
2375 2376
	NULL
};
2377

2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391
static umode_t bmc_dev_attr_is_visible(struct kobject *kobj,
				       struct attribute *attr, int idx)
{
	struct device *dev = kobj_to_dev(kobj);
	struct bmc_device *bmc = to_bmc_device(dev);
	umode_t mode = attr->mode;

	if (attr == &dev_attr_aux_firmware_revision.attr)
		return bmc->id.aux_firmware_revision_set ? mode : 0;
	if (attr == &dev_attr_guid.attr)
		return bmc->guid_set ? mode : 0;
	return mode;
}

2392 2393
static struct attribute_group bmc_dev_attr_group = {
	.attrs		= bmc_dev_attrs,
2394
	.is_visible	= bmc_dev_attr_is_visible,
2395
};
J
Jeff Garzik 已提交
2396

2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409
static const struct attribute_group *bmc_dev_attr_groups[] = {
	&bmc_dev_attr_group,
	NULL
};

static struct device_type bmc_device_type = {
	.groups		= bmc_dev_attr_groups,
};

static void
release_bmc_device(struct device *dev)
{
	kfree(to_bmc_device(dev));
J
Jeff Garzik 已提交
2410 2411 2412 2413 2414
}

static void
cleanup_bmc_device(struct kref *ref)
{
2415
	struct bmc_device *bmc = container_of(ref, struct bmc_device, usecount);
J
Jeff Garzik 已提交
2416

2417
	platform_device_unregister(&bmc->pdev);
2418 2419 2420 2421 2422 2423
}

static void ipmi_bmc_unregister(ipmi_smi_t intf)
{
	struct bmc_device *bmc = intf->bmc;

2424
	sysfs_remove_link(&intf->si_dev->kobj, "bmc");
2425
	if (intf->my_dev_name) {
2426
		sysfs_remove_link(&bmc->pdev.dev.kobj, intf->my_dev_name);
2427 2428 2429 2430 2431
		kfree(intf->my_dev_name);
		intf->my_dev_name = NULL;
	}

	mutex_lock(&ipmidriver_mutex);
2432
	kref_put(&bmc->usecount, cleanup_bmc_device);
2433
	intf->bmc = NULL;
2434 2435 2436
	mutex_unlock(&ipmidriver_mutex);
}

2437
static int ipmi_bmc_register(ipmi_smi_t intf, int ifnum)
2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449
{
	int               rv;
	struct bmc_device *bmc = intf->bmc;
	struct bmc_device *old_bmc;

	mutex_lock(&ipmidriver_mutex);

	/*
	 * Try to find if there is an bmc_device struct
	 * representing the interfaced BMC already
	 */
	if (bmc->guid_set)
2450
		old_bmc = ipmi_find_bmc_guid(&ipmidriver.driver, bmc->guid);
2451
	else
2452
		old_bmc = ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464
						    bmc->id.product_id,
						    bmc->id.device_id);

	/*
	 * If there is already an bmc_device, free the new one,
	 * otherwise register the new BMC device
	 */
	if (old_bmc) {
		kfree(bmc);
		intf->bmc = old_bmc;
		bmc = old_bmc;

2465
		kref_get(&bmc->usecount);
2466 2467 2468 2469 2470 2471 2472 2473 2474
		mutex_unlock(&ipmidriver_mutex);

		printk(KERN_INFO
		       "ipmi: interfacing existing BMC (man_id: 0x%6.6x,"
		       " prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
		       bmc->id.manufacturer_id,
		       bmc->id.product_id,
		       bmc->id.device_id);
	} else {
2475 2476 2477
		unsigned char orig_dev_id = bmc->id.device_id;
		int warn_printed = 0;

2478
		snprintf(bmc->name, sizeof(bmc->name),
2479
			 "ipmi_bmc.%4.4x", bmc->id.product_id);
2480
		bmc->pdev.name = bmc->name;
2481

2482
		while (ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
2483
						 bmc->id.product_id,
C
Corey Minyard 已提交
2484
						 bmc->id.device_id)) {
2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503
			if (!warn_printed) {
				printk(KERN_WARNING PFX
				       "This machine has two different BMCs"
				       " with the same product id and device"
				       " id.  This is an error in the"
				       " firmware, but incrementing the"
				       " device id to work around the problem."
				       " Prod ID = 0x%x, Dev ID = 0x%x\n",
				       bmc->id.product_id, bmc->id.device_id);
				warn_printed = 1;
			}
			bmc->id.device_id++; /* Wraps at 255 */
			if (bmc->id.device_id == orig_dev_id) {
				printk(KERN_ERR PFX
				       "Out of device ids!\n");
				break;
			}
		}

2504 2505 2506 2507
		bmc->pdev.dev.driver = &ipmidriver.driver;
		bmc->pdev.id = bmc->id.device_id;
		bmc->pdev.dev.release = release_bmc_device;
		bmc->pdev.dev.type = &bmc_device_type;
2508
		kref_init(&bmc->usecount);
2509

2510
		rv = platform_device_register(&bmc->pdev);
2511 2512
		mutex_unlock(&ipmidriver_mutex);
		if (rv) {
2513
			put_device(&bmc->pdev.dev);
2514 2515 2516 2517
			printk(KERN_ERR
			       "ipmi_msghandler:"
			       " Unable to register bmc device: %d\n",
			       rv);
2518 2519 2520 2521
			/*
			 * Don't go to out_err, you can only do that if
			 * the device is registered already.
			 */
2522 2523 2524
			return rv;
		}

2525 2526 2527 2528 2529
		dev_info(intf->si_dev, "Found new BMC (man_id: 0x%6.6x, "
			 "prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
			 bmc->id.manufacturer_id,
			 bmc->id.product_id,
			 bmc->id.device_id);
2530 2531 2532 2533 2534 2535
	}

	/*
	 * create symlink from system interface device to bmc device
	 * and back.
	 */
2536
	rv = sysfs_create_link(&intf->si_dev->kobj, &bmc->pdev.dev.kobj, "bmc");
2537 2538 2539 2540 2541 2542 2543
	if (rv) {
		printk(KERN_ERR
		       "ipmi_msghandler: Unable to create bmc symlink: %d\n",
		       rv);
		goto out_err;
	}

2544
	intf->my_dev_name = kasprintf(GFP_KERNEL, "ipmi%d", ifnum);
2545 2546 2547 2548 2549 2550 2551 2552
	if (!intf->my_dev_name) {
		rv = -ENOMEM;
		printk(KERN_ERR
		       "ipmi_msghandler: allocate link from BMC: %d\n",
		       rv);
		goto out_err;
	}

2553
	rv = sysfs_create_link(&bmc->pdev.dev.kobj, &intf->si_dev->kobj,
2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645
			       intf->my_dev_name);
	if (rv) {
		kfree(intf->my_dev_name);
		intf->my_dev_name = NULL;
		printk(KERN_ERR
		       "ipmi_msghandler:"
		       " Unable to create symlink to bmc: %d\n",
		       rv);
		goto out_err;
	}

	return 0;

out_err:
	ipmi_bmc_unregister(intf);
	return rv;
}

static int
send_guid_cmd(ipmi_smi_t intf, int chan)
{
	struct kernel_ipmi_msg            msg;
	struct ipmi_system_interface_addr si;

	si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
	si.channel = IPMI_BMC_CHANNEL;
	si.lun = 0;

	msg.netfn = IPMI_NETFN_APP_REQUEST;
	msg.cmd = IPMI_GET_DEVICE_GUID_CMD;
	msg.data = NULL;
	msg.data_len = 0;
	return i_ipmi_request(NULL,
			      intf,
			      (struct ipmi_addr *) &si,
			      0,
			      &msg,
			      intf,
			      NULL,
			      NULL,
			      0,
			      intf->channels[0].address,
			      intf->channels[0].lun,
			      -1, 0);
}

static void
guid_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
{
	if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
	    || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
	    || (msg->msg.cmd != IPMI_GET_DEVICE_GUID_CMD))
		/* Not for me */
		return;

	if (msg->msg.data[0] != 0) {
		/* Error from getting the GUID, the BMC doesn't have one. */
		intf->bmc->guid_set = 0;
		goto out;
	}

	if (msg->msg.data_len < 17) {
		intf->bmc->guid_set = 0;
		printk(KERN_WARNING PFX
		       "guid_handler: The GUID response from the BMC was too"
		       " short, it was %d but should have been 17.  Assuming"
		       " GUID is not available.\n",
		       msg->msg.data_len);
		goto out;
	}

	memcpy(intf->bmc->guid, msg->msg.data, 16);
	intf->bmc->guid_set = 1;
 out:
	wake_up(&intf->waitq);
}

static void
get_guid(ipmi_smi_t intf)
{
	int rv;

	intf->bmc->guid_set = 0x2;
	intf->null_user_handler = guid_handler;
	rv = send_guid_cmd(intf, 0);
	if (rv)
		/* Send failed, no GUID available. */
		intf->bmc->guid_set = 0;
	wait_event(intf->waitq, intf->bmc->guid_set != 2);
	intf->null_user_handler = NULL;
}

L
Linus Torvalds 已提交
2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666
static int
send_channel_info_cmd(ipmi_smi_t intf, int chan)
{
	struct kernel_ipmi_msg            msg;
	unsigned char                     data[1];
	struct ipmi_system_interface_addr si;

	si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
	si.channel = IPMI_BMC_CHANNEL;
	si.lun = 0;

	msg.netfn = IPMI_NETFN_APP_REQUEST;
	msg.cmd = IPMI_GET_CHANNEL_INFO_CMD;
	msg.data = data;
	msg.data_len = 1;
	data[0] = chan;
	return i_ipmi_request(NULL,
			      intf,
			      (struct ipmi_addr *) &si,
			      0,
			      &msg,
2667
			      intf,
L
Linus Torvalds 已提交
2668 2669 2670
			      NULL,
			      NULL,
			      0,
2671 2672
			      intf->channels[0].address,
			      intf->channels[0].lun,
L
Linus Torvalds 已提交
2673 2674 2675 2676
			      -1, 0);
}

static void
2677
channel_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
L
Linus Torvalds 已提交
2678 2679 2680 2681
{
	int rv = 0;
	int chan;

2682 2683
	if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
	    && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
2684
	    && (msg->msg.cmd == IPMI_GET_CHANNEL_INFO_CMD)) {
L
Linus Torvalds 已提交
2685
		/* It's the one we want */
2686
		if (msg->msg.data[0] != 0) {
L
Linus Torvalds 已提交
2687 2688
			/* Got an error from the channel, just go on. */

2689
			if (msg->msg.data[0] == IPMI_INVALID_COMMAND_ERR) {
2690 2691 2692 2693 2694 2695
				/*
				 * If the MC does not support this
				 * command, that is legal.  We just
				 * assume it has one IPMB at channel
				 * zero.
				 */
L
Linus Torvalds 已提交
2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706
				intf->channels[0].medium
					= IPMI_CHANNEL_MEDIUM_IPMB;
				intf->channels[0].protocol
					= IPMI_CHANNEL_PROTOCOL_IPMB;

				intf->curr_channel = IPMI_MAX_CHANNELS;
				wake_up(&intf->waitq);
				goto out;
			}
			goto next_channel;
		}
2707
		if (msg->msg.data_len < 4) {
L
Linus Torvalds 已提交
2708 2709 2710 2711
			/* Message not big enough, just go on. */
			goto next_channel;
		}
		chan = intf->curr_channel;
2712 2713
		intf->channels[chan].medium = msg->msg.data[2] & 0x7f;
		intf->channels[chan].protocol = msg->msg.data[3] & 0x1f;
L
Linus Torvalds 已提交
2714

2715
 next_channel:
L
Linus Torvalds 已提交
2716 2717 2718 2719 2720 2721 2722 2723
		intf->curr_channel++;
		if (intf->curr_channel >= IPMI_MAX_CHANNELS)
			wake_up(&intf->waitq);
		else
			rv = send_channel_info_cmd(intf, intf->curr_channel);

		if (rv) {
			/* Got an error somehow, just give up. */
2724 2725 2726 2727
			printk(KERN_WARNING PFX
			       "Error sending channel information for channel"
			       " %d: %d\n", intf->curr_channel, rv);

L
Linus Torvalds 已提交
2728 2729 2730 2731 2732 2733 2734 2735
			intf->curr_channel = IPMI_MAX_CHANNELS;
			wake_up(&intf->waitq);
		}
	}
 out:
	return;
}

2736
static void ipmi_poll(ipmi_smi_t intf)
C
Corey Minyard 已提交
2737 2738 2739
{
	if (intf->handlers->poll)
		intf->handlers->poll(intf->send_info);
2740 2741
	/* In case something came in */
	handle_new_recv_msgs(intf);
C
Corey Minyard 已提交
2742
}
2743 2744 2745 2746

void ipmi_poll_interface(ipmi_user_t user)
{
	ipmi_poll(user->intf);
C
Corey Minyard 已提交
2747
}
2748
EXPORT_SYMBOL(ipmi_poll_interface);
C
Corey Minyard 已提交
2749

2750
int ipmi_register_smi(const struct ipmi_smi_handlers *handlers,
L
Linus Torvalds 已提交
2751
		      void		       *send_info,
2752 2753
		      struct ipmi_device_id    *device_id,
		      struct device            *si_dev,
2754
		      unsigned char            slave_addr)
L
Linus Torvalds 已提交
2755 2756 2757
{
	int              i, j;
	int              rv;
2758
	ipmi_smi_t       intf;
2759 2760
	ipmi_smi_t       tintf;
	struct list_head *link;
L
Linus Torvalds 已提交
2761

2762 2763 2764 2765
	/*
	 * Make sure the driver is actually initialized, this handles
	 * problems with initialization order.
	 */
L
Linus Torvalds 已提交
2766 2767 2768 2769
	if (!initialized) {
		rv = ipmi_init_msghandler();
		if (rv)
			return rv;
2770 2771 2772 2773
		/*
		 * The init code doesn't return an error if it was turned
		 * off, but it won't initialize.  Check that.
		 */
L
Linus Torvalds 已提交
2774 2775 2776 2777
		if (!initialized)
			return -ENODEV;
	}

2778
	intf = kzalloc(sizeof(*intf), GFP_KERNEL);
2779
	if (!intf)
L
Linus Torvalds 已提交
2780
		return -ENOMEM;
2781 2782 2783 2784

	intf->ipmi_version_major = ipmi_version_major(device_id);
	intf->ipmi_version_minor = ipmi_version_minor(device_id);

2785 2786 2787 2788 2789
	intf->bmc = kzalloc(sizeof(*intf->bmc), GFP_KERNEL);
	if (!intf->bmc) {
		kfree(intf);
		return -ENOMEM;
	}
2790
	intf->intf_num = -1; /* Mark it invalid for now. */
2791
	kref_init(&intf->refcount);
2792 2793
	intf->bmc->id = *device_id;
	intf->si_dev = si_dev;
2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809
	for (j = 0; j < IPMI_MAX_CHANNELS; j++) {
		intf->channels[j].address = IPMI_BMC_SLAVE_ADDR;
		intf->channels[j].lun = 2;
	}
	if (slave_addr != 0)
		intf->channels[0].address = slave_addr;
	INIT_LIST_HEAD(&intf->users);
	intf->handlers = handlers;
	intf->send_info = send_info;
	spin_lock_init(&intf->seq_lock);
	for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++) {
		intf->seq_table[j].inuse = 0;
		intf->seq_table[j].seqid = 0;
	}
	intf->curr_seq = 0;
#ifdef CONFIG_PROC_FS
C
Corey Minyard 已提交
2810
	mutex_init(&intf->proc_entry_lock);
2811
#endif
2812 2813
	spin_lock_init(&intf->waiting_rcv_msgs_lock);
	INIT_LIST_HEAD(&intf->waiting_rcv_msgs);
2814 2815 2816 2817
	tasklet_init(&intf->recv_tasklet,
		     smi_recv_tasklet,
		     (unsigned long) intf);
	atomic_set(&intf->watchdog_pretimeouts_to_deliver, 0);
2818 2819 2820
	spin_lock_init(&intf->xmit_msgs_lock);
	INIT_LIST_HEAD(&intf->xmit_msgs);
	INIT_LIST_HEAD(&intf->hp_xmit_msgs);
2821
	spin_lock_init(&intf->events_lock);
2822 2823
	atomic_set(&intf->event_waiters, 0);
	intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
2824 2825
	INIT_LIST_HEAD(&intf->waiting_events);
	intf->waiting_events_count = 0;
2826
	mutex_init(&intf->cmd_rcvrs_mutex);
C
Corey Minyard 已提交
2827
	spin_lock_init(&intf->maintenance_mode_lock);
2828 2829
	INIT_LIST_HEAD(&intf->cmd_rcvrs);
	init_waitqueue_head(&intf->waitq);
2830 2831
	for (i = 0; i < IPMI_NUM_STATS; i++)
		atomic_set(&intf->stats[i], 0);
2832 2833

	intf->proc_dir = NULL;
L
Linus Torvalds 已提交
2834

2835
	mutex_lock(&smi_watchers_mutex);
2836 2837 2838 2839 2840 2841 2842
	mutex_lock(&ipmi_interfaces_mutex);
	/* Look for a hole in the numbers. */
	i = 0;
	link = &ipmi_interfaces;
	list_for_each_entry_rcu(tintf, &ipmi_interfaces, link) {
		if (tintf->intf_num != i) {
			link = &tintf->link;
L
Linus Torvalds 已提交
2843 2844
			break;
		}
2845
		i++;
L
Linus Torvalds 已提交
2846
	}
2847 2848 2849 2850 2851
	/* Add the new interface in numeric order. */
	if (i == 0)
		list_add_rcu(&intf->link, &ipmi_interfaces);
	else
		list_add_tail_rcu(&intf->link, link);
L
Linus Torvalds 已提交
2852

2853 2854 2855
	rv = handlers->start_processing(send_info, intf);
	if (rv)
		goto out;
L
Linus Torvalds 已提交
2856

2857 2858
	get_guid(intf);

2859
	if ((intf->ipmi_version_major > 1)
2860 2861 2862 2863 2864 2865
			|| ((intf->ipmi_version_major == 1)
			    && (intf->ipmi_version_minor >= 5))) {
		/*
		 * Start scanning the channels to see what is
		 * available.
		 */
2866 2867 2868
		intf->null_user_handler = channel_handler;
		intf->curr_channel = 0;
		rv = send_channel_info_cmd(intf, 0);
2869 2870 2871 2872
		if (rv) {
			printk(KERN_WARNING PFX
			       "Error sending channel information for channel"
			       " 0, %d\n", rv);
2873
			goto out;
2874
		}
L
Linus Torvalds 已提交
2875

2876 2877 2878
		/* Wait for the channel info to be read. */
		wait_event(intf->waitq,
			   intf->curr_channel >= IPMI_MAX_CHANNELS);
2879
		intf->null_user_handler = NULL;
2880 2881 2882 2883
	} else {
		/* Assume a single IPMB channel at zero. */
		intf->channels[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
		intf->channels[0].protocol = IPMI_CHANNEL_PROTOCOL_IPMB;
C
Corey Minyard 已提交
2884
		intf->curr_channel = IPMI_MAX_CHANNELS;
L
Linus Torvalds 已提交
2885 2886
	}

2887 2888
	if (rv == 0)
		rv = add_proc_entries(intf, i);
L
Linus Torvalds 已提交
2889

2890
	rv = ipmi_bmc_register(intf, i);
2891

2892
 out:
L
Linus Torvalds 已提交
2893
	if (rv) {
2894 2895
		if (intf->proc_dir)
			remove_proc_entries(intf);
2896
		intf->handlers = NULL;
2897 2898
		list_del_rcu(&intf->link);
		mutex_unlock(&ipmi_interfaces_mutex);
2899
		mutex_unlock(&smi_watchers_mutex);
2900
		synchronize_rcu();
2901 2902
		kref_put(&intf->refcount, intf_free);
	} else {
2903 2904 2905 2906 2907 2908
		/*
		 * Keep memory order straight for RCU readers.  Make
		 * sure everything else is committed to memory before
		 * setting intf_num to mark the interface valid.
		 */
		smp_wmb();
2909 2910
		intf->intf_num = i;
		mutex_unlock(&ipmi_interfaces_mutex);
2911
		/* After this point the interface is legal to use. */
2912
		call_smi_watchers(i, intf->si_dev);
2913
		mutex_unlock(&smi_watchers_mutex);
L
Linus Torvalds 已提交
2914 2915 2916 2917
	}

	return rv;
}
2918
EXPORT_SYMBOL(ipmi_register_smi);
L
Linus Torvalds 已提交
2919

2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931
static void deliver_smi_err_response(ipmi_smi_t intf,
				     struct ipmi_smi_msg *msg,
				     unsigned char err)
{
	msg->rsp[0] = msg->data[0] | 4;
	msg->rsp[1] = msg->data[1];
	msg->rsp[2] = err;
	msg->rsp_size = 3;
	/* It's an error, so it will never requeue, no need to check return. */
	handle_one_recv_msg(intf, msg);
}

2932 2933 2934 2935
static void cleanup_smi_msgs(ipmi_smi_t intf)
{
	int              i;
	struct seq_table *ent;
2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949
	struct ipmi_smi_msg *msg;
	struct list_head *entry;
	struct list_head tmplist;

	/* Clear out our transmit queues and hold the messages. */
	INIT_LIST_HEAD(&tmplist);
	list_splice_tail(&intf->hp_xmit_msgs, &tmplist);
	list_splice_tail(&intf->xmit_msgs, &tmplist);

	/* Current message first, to preserve order */
	while (intf->curr_msg && !list_empty(&intf->waiting_rcv_msgs)) {
		/* Wait for the message to clear out. */
		schedule_timeout(1);
	}
2950 2951

	/* No need for locks, the interface is down. */
2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963

	/*
	 * Return errors for all pending messages in queue and in the
	 * tables waiting for remote responses.
	 */
	while (!list_empty(&tmplist)) {
		entry = tmplist.next;
		list_del(entry);
		msg = list_entry(entry, struct ipmi_smi_msg, link);
		deliver_smi_err_response(intf, msg, IPMI_ERR_UNSPECIFIED);
	}

2964 2965 2966 2967 2968 2969 2970 2971
	for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
		ent = &(intf->seq_table[i]);
		if (!ent->inuse)
			continue;
		deliver_err_response(ent->recv_msg, IPMI_ERR_UNSPECIFIED);
	}
}

L
Linus Torvalds 已提交
2972 2973 2974
int ipmi_unregister_smi(ipmi_smi_t intf)
{
	struct ipmi_smi_watcher *w;
2975 2976
	int intf_num = intf->intf_num;
	ipmi_user_t user;
L
Linus Torvalds 已提交
2977

2978 2979
	ipmi_bmc_unregister(intf);

2980
	mutex_lock(&smi_watchers_mutex);
2981
	mutex_lock(&ipmi_interfaces_mutex);
2982
	intf->intf_num = -1;
2983
	intf->in_shutdown = true;
2984 2985 2986
	list_del_rcu(&intf->link);
	mutex_unlock(&ipmi_interfaces_mutex);
	synchronize_rcu();
L
Linus Torvalds 已提交
2987

2988 2989
	cleanup_smi_msgs(intf);

2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001
	/* Clean up the effects of users on the lower-level software. */
	mutex_lock(&ipmi_interfaces_mutex);
	rcu_read_lock();
	list_for_each_entry_rcu(user, &intf->users, link) {
		module_put(intf->handlers->owner);
		if (intf->handlers->dec_usecount)
			intf->handlers->dec_usecount(intf->send_info);
	}
	rcu_read_unlock();
	intf->handlers = NULL;
	mutex_unlock(&ipmi_interfaces_mutex);

3002
	remove_proc_entries(intf);
L
Linus Torvalds 已提交
3003

3004 3005 3006 3007
	/*
	 * Call all the watcher interfaces to tell them that
	 * an interface is gone.
	 */
3008
	list_for_each_entry(w, &smi_watchers, link)
3009 3010
		w->smi_gone(intf_num);
	mutex_unlock(&smi_watchers_mutex);
3011 3012

	kref_put(&intf->refcount, intf_free);
L
Linus Torvalds 已提交
3013 3014
	return 0;
}
3015
EXPORT_SYMBOL(ipmi_unregister_smi);
L
Linus Torvalds 已提交
3016 3017 3018 3019 3020 3021 3022

static int handle_ipmb_get_msg_rsp(ipmi_smi_t          intf,
				   struct ipmi_smi_msg *msg)
{
	struct ipmi_ipmb_addr ipmb_addr;
	struct ipmi_recv_msg  *recv_msg;

3023 3024 3025 3026
	/*
	 * This is 11, not 10, because the response must contain a
	 * completion code.
	 */
L
Linus Torvalds 已提交
3027 3028
	if (msg->rsp_size < 11) {
		/* Message not big enough, just ignore it. */
3029
		ipmi_inc_stat(intf, invalid_ipmb_responses);
L
Linus Torvalds 已提交
3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042
		return 0;
	}

	if (msg->rsp[2] != 0) {
		/* An error getting the response, just ignore it. */
		return 0;
	}

	ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
	ipmb_addr.slave_addr = msg->rsp[6];
	ipmb_addr.channel = msg->rsp[3] & 0x0f;
	ipmb_addr.lun = msg->rsp[7] & 3;

3043 3044 3045 3046
	/*
	 * It's a response from a remote entity.  Look up the sequence
	 * number and handle the response.
	 */
L
Linus Torvalds 已提交
3047 3048 3049 3050 3051 3052
	if (intf_find_seq(intf,
			  msg->rsp[7] >> 2,
			  msg->rsp[3] & 0x0f,
			  msg->rsp[8],
			  (msg->rsp[4] >> 2) & (~1),
			  (struct ipmi_addr *) &(ipmb_addr),
3053 3054 3055 3056 3057
			  &recv_msg)) {
		/*
		 * We were unable to find the sequence number,
		 * so just nuke the message.
		 */
3058
		ipmi_inc_stat(intf, unhandled_ipmb_responses);
L
Linus Torvalds 已提交
3059 3060 3061 3062 3063 3064
		return 0;
	}

	memcpy(recv_msg->msg_data,
	       &(msg->rsp[9]),
	       msg->rsp_size - 9);
3065 3066 3067 3068 3069
	/*
	 * The other fields matched, so no need to set them, except
	 * for netfn, which needs to be the response that was
	 * returned, not the request value.
	 */
L
Linus Torvalds 已提交
3070 3071 3072 3073
	recv_msg->msg.netfn = msg->rsp[4] >> 2;
	recv_msg->msg.data = recv_msg->msg_data;
	recv_msg->msg.data_len = msg->rsp_size - 10;
	recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3074
	ipmi_inc_stat(intf, handled_ipmb_responses);
L
Linus Torvalds 已提交
3075 3076 3077 3078 3079 3080 3081 3082
	deliver_response(recv_msg);

	return 0;
}

static int handle_ipmb_get_msg_cmd(ipmi_smi_t          intf,
				   struct ipmi_smi_msg *msg)
{
3083 3084 3085 3086
	struct cmd_rcvr          *rcvr;
	int                      rv = 0;
	unsigned char            netfn;
	unsigned char            cmd;
3087
	unsigned char            chan;
3088 3089 3090
	ipmi_user_t              user = NULL;
	struct ipmi_ipmb_addr    *ipmb_addr;
	struct ipmi_recv_msg     *recv_msg;
L
Linus Torvalds 已提交
3091 3092 3093

	if (msg->rsp_size < 10) {
		/* Message not big enough, just ignore it. */
3094
		ipmi_inc_stat(intf, invalid_commands);
L
Linus Torvalds 已提交
3095 3096 3097 3098 3099 3100 3101 3102 3103 3104
		return 0;
	}

	if (msg->rsp[2] != 0) {
		/* An error getting the response, just ignore it. */
		return 0;
	}

	netfn = msg->rsp[4] >> 2;
	cmd = msg->rsp[8];
3105
	chan = msg->rsp[3] & 0xf;
L
Linus Torvalds 已提交
3106

3107
	rcu_read_lock();
3108
	rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3109 3110 3111 3112 3113
	if (rcvr) {
		user = rcvr->user;
		kref_get(&user->refcount);
	} else
		user = NULL;
3114
	rcu_read_unlock();
L
Linus Torvalds 已提交
3115 3116 3117

	if (user == NULL) {
		/* We didn't find a user, deliver an error response. */
3118
		ipmi_inc_stat(intf, unhandled_commands);
L
Linus Torvalds 已提交
3119 3120 3121 3122 3123

		msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
		msg->data[1] = IPMI_SEND_MSG_CMD;
		msg->data[2] = msg->rsp[3];
		msg->data[3] = msg->rsp[6];
3124
		msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
L
Linus Torvalds 已提交
3125
		msg->data[5] = ipmb_checksum(&(msg->data[3]), 2);
3126
		msg->data[6] = intf->channels[msg->rsp[3] & 0xf].address;
3127 3128
		/* rqseq/lun */
		msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
L
Linus Torvalds 已提交
3129 3130 3131 3132 3133 3134 3135 3136 3137
		msg->data[8] = msg->rsp[8]; /* cmd */
		msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
		msg->data[10] = ipmb_checksum(&(msg->data[6]), 4);
		msg->data_size = 11;

#ifdef DEBUG_MSGING
	{
		int m;
		printk("Invalid command:");
C
Corey Minyard 已提交
3138
		for (m = 0; m < msg->data_size; m++)
L
Linus Torvalds 已提交
3139 3140 3141 3142
			printk(" %2.2x", msg->data[m]);
		printk("\n");
	}
#endif
3143
		rcu_read_lock();
3144 3145
		if (!intf->in_shutdown) {
			smi_send(intf, intf->handlers, msg, 0);
3146 3147 3148 3149 3150
			/*
			 * We used the message, so return the value
			 * that causes it to not be freed or
			 * queued.
			 */
3151 3152 3153
			rv = -1;
		}
		rcu_read_unlock();
L
Linus Torvalds 已提交
3154 3155
	} else {
		/* Deliver the message to the user. */
3156
		ipmi_inc_stat(intf, handled_commands);
L
Linus Torvalds 已提交
3157 3158

		recv_msg = ipmi_alloc_recv_msg();
3159
		if (!recv_msg) {
3160 3161 3162 3163 3164
			/*
			 * We couldn't allocate memory for the
			 * message, so requeue it for handling
			 * later.
			 */
L
Linus Torvalds 已提交
3165
			rv = 1;
3166
			kref_put(&user->refcount, free_user);
L
Linus Torvalds 已提交
3167 3168 3169 3170 3171 3172 3173 3174
		} else {
			/* Extract the source address from the data. */
			ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
			ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
			ipmb_addr->slave_addr = msg->rsp[6];
			ipmb_addr->lun = msg->rsp[7] & 3;
			ipmb_addr->channel = msg->rsp[3] & 0xf;

3175 3176 3177 3178
			/*
			 * Extract the rest of the message information
			 * from the IPMB header.
			 */
L
Linus Torvalds 已提交
3179 3180 3181 3182 3183 3184 3185
			recv_msg->user = user;
			recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
			recv_msg->msgid = msg->rsp[7] >> 2;
			recv_msg->msg.netfn = msg->rsp[4] >> 2;
			recv_msg->msg.cmd = msg->rsp[8];
			recv_msg->msg.data = recv_msg->msg_data;

3186 3187 3188 3189
			/*
			 * We chop off 10, not 9 bytes because the checksum
			 * at the end also needs to be removed.
			 */
L
Linus Torvalds 已提交
3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207
			recv_msg->msg.data_len = msg->rsp_size - 10;
			memcpy(recv_msg->msg_data,
			       &(msg->rsp[9]),
			       msg->rsp_size - 10);
			deliver_response(recv_msg);
		}
	}

	return rv;
}

static int handle_lan_get_msg_rsp(ipmi_smi_t          intf,
				  struct ipmi_smi_msg *msg)
{
	struct ipmi_lan_addr  lan_addr;
	struct ipmi_recv_msg  *recv_msg;


3208 3209 3210 3211
	/*
	 * This is 13, not 12, because the response must contain a
	 * completion code.
	 */
L
Linus Torvalds 已提交
3212 3213
	if (msg->rsp_size < 13) {
		/* Message not big enough, just ignore it. */
3214
		ipmi_inc_stat(intf, invalid_lan_responses);
L
Linus Torvalds 已提交
3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230
		return 0;
	}

	if (msg->rsp[2] != 0) {
		/* An error getting the response, just ignore it. */
		return 0;
	}

	lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
	lan_addr.session_handle = msg->rsp[4];
	lan_addr.remote_SWID = msg->rsp[8];
	lan_addr.local_SWID = msg->rsp[5];
	lan_addr.channel = msg->rsp[3] & 0x0f;
	lan_addr.privilege = msg->rsp[3] >> 4;
	lan_addr.lun = msg->rsp[9] & 3;

3231 3232 3233 3234
	/*
	 * It's a response from a remote entity.  Look up the sequence
	 * number and handle the response.
	 */
L
Linus Torvalds 已提交
3235 3236 3237 3238 3239 3240
	if (intf_find_seq(intf,
			  msg->rsp[9] >> 2,
			  msg->rsp[3] & 0x0f,
			  msg->rsp[10],
			  (msg->rsp[6] >> 2) & (~1),
			  (struct ipmi_addr *) &(lan_addr),
3241 3242 3243 3244 3245
			  &recv_msg)) {
		/*
		 * We were unable to find the sequence number,
		 * so just nuke the message.
		 */
3246
		ipmi_inc_stat(intf, unhandled_lan_responses);
L
Linus Torvalds 已提交
3247 3248 3249 3250 3251 3252
		return 0;
	}

	memcpy(recv_msg->msg_data,
	       &(msg->rsp[11]),
	       msg->rsp_size - 11);
3253 3254 3255 3256 3257
	/*
	 * The other fields matched, so no need to set them, except
	 * for netfn, which needs to be the response that was
	 * returned, not the request value.
	 */
L
Linus Torvalds 已提交
3258 3259 3260 3261
	recv_msg->msg.netfn = msg->rsp[6] >> 2;
	recv_msg->msg.data = recv_msg->msg_data;
	recv_msg->msg.data_len = msg->rsp_size - 12;
	recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3262
	ipmi_inc_stat(intf, handled_lan_responses);
L
Linus Torvalds 已提交
3263 3264 3265 3266 3267 3268 3269 3270
	deliver_response(recv_msg);

	return 0;
}

static int handle_lan_get_msg_cmd(ipmi_smi_t          intf,
				  struct ipmi_smi_msg *msg)
{
3271 3272 3273 3274
	struct cmd_rcvr          *rcvr;
	int                      rv = 0;
	unsigned char            netfn;
	unsigned char            cmd;
3275
	unsigned char            chan;
3276 3277 3278
	ipmi_user_t              user = NULL;
	struct ipmi_lan_addr     *lan_addr;
	struct ipmi_recv_msg     *recv_msg;
L
Linus Torvalds 已提交
3279 3280 3281

	if (msg->rsp_size < 12) {
		/* Message not big enough, just ignore it. */
3282
		ipmi_inc_stat(intf, invalid_commands);
L
Linus Torvalds 已提交
3283 3284 3285 3286 3287 3288 3289 3290 3291 3292
		return 0;
	}

	if (msg->rsp[2] != 0) {
		/* An error getting the response, just ignore it. */
		return 0;
	}

	netfn = msg->rsp[6] >> 2;
	cmd = msg->rsp[10];
3293
	chan = msg->rsp[3] & 0xf;
L
Linus Torvalds 已提交
3294

3295
	rcu_read_lock();
3296
	rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3297 3298 3299 3300 3301
	if (rcvr) {
		user = rcvr->user;
		kref_get(&user->refcount);
	} else
		user = NULL;
3302
	rcu_read_unlock();
L
Linus Torvalds 已提交
3303 3304

	if (user == NULL) {
3305
		/* We didn't find a user, just give up. */
3306
		ipmi_inc_stat(intf, unhandled_commands);
L
Linus Torvalds 已提交
3307

3308 3309 3310 3311 3312
		/*
		 * Don't do anything with these messages, just allow
		 * them to be freed.
		 */
		rv = 0;
L
Linus Torvalds 已提交
3313 3314
	} else {
		/* Deliver the message to the user. */
3315
		ipmi_inc_stat(intf, handled_commands);
L
Linus Torvalds 已提交
3316 3317

		recv_msg = ipmi_alloc_recv_msg();
3318
		if (!recv_msg) {
3319 3320 3321 3322
			/*
			 * We couldn't allocate memory for the
			 * message, so requeue it for handling later.
			 */
L
Linus Torvalds 已提交
3323
			rv = 1;
3324
			kref_put(&user->refcount, free_user);
L
Linus Torvalds 已提交
3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335
		} else {
			/* Extract the source address from the data. */
			lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
			lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
			lan_addr->session_handle = msg->rsp[4];
			lan_addr->remote_SWID = msg->rsp[8];
			lan_addr->local_SWID = msg->rsp[5];
			lan_addr->lun = msg->rsp[9] & 3;
			lan_addr->channel = msg->rsp[3] & 0xf;
			lan_addr->privilege = msg->rsp[3] >> 4;

3336 3337 3338 3339
			/*
			 * Extract the rest of the message information
			 * from the IPMB header.
			 */
L
Linus Torvalds 已提交
3340 3341 3342 3343 3344 3345 3346
			recv_msg->user = user;
			recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
			recv_msg->msgid = msg->rsp[9] >> 2;
			recv_msg->msg.netfn = msg->rsp[6] >> 2;
			recv_msg->msg.cmd = msg->rsp[10];
			recv_msg->msg.data = recv_msg->msg_data;

3347 3348 3349 3350
			/*
			 * We chop off 12, not 11 bytes because the checksum
			 * at the end also needs to be removed.
			 */
L
Linus Torvalds 已提交
3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361
			recv_msg->msg.data_len = msg->rsp_size - 12;
			memcpy(recv_msg->msg_data,
			       &(msg->rsp[11]),
			       msg->rsp_size - 12);
			deliver_response(recv_msg);
		}
	}

	return rv;
}

D
dann frazier 已提交
3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469
/*
 * This routine will handle "Get Message" command responses with
 * channels that use an OEM Medium. The message format belongs to
 * the OEM.  See IPMI 2.0 specification, Chapter 6 and
 * Chapter 22, sections 22.6 and 22.24 for more details.
 */
static int handle_oem_get_msg_cmd(ipmi_smi_t          intf,
				  struct ipmi_smi_msg *msg)
{
	struct cmd_rcvr       *rcvr;
	int                   rv = 0;
	unsigned char         netfn;
	unsigned char         cmd;
	unsigned char         chan;
	ipmi_user_t           user = NULL;
	struct ipmi_system_interface_addr *smi_addr;
	struct ipmi_recv_msg  *recv_msg;

	/*
	 * We expect the OEM SW to perform error checking
	 * so we just do some basic sanity checks
	 */
	if (msg->rsp_size < 4) {
		/* Message not big enough, just ignore it. */
		ipmi_inc_stat(intf, invalid_commands);
		return 0;
	}

	if (msg->rsp[2] != 0) {
		/* An error getting the response, just ignore it. */
		return 0;
	}

	/*
	 * This is an OEM Message so the OEM needs to know how
	 * handle the message. We do no interpretation.
	 */
	netfn = msg->rsp[0] >> 2;
	cmd = msg->rsp[1];
	chan = msg->rsp[3] & 0xf;

	rcu_read_lock();
	rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
	if (rcvr) {
		user = rcvr->user;
		kref_get(&user->refcount);
	} else
		user = NULL;
	rcu_read_unlock();

	if (user == NULL) {
		/* We didn't find a user, just give up. */
		ipmi_inc_stat(intf, unhandled_commands);

		/*
		 * Don't do anything with these messages, just allow
		 * them to be freed.
		 */

		rv = 0;
	} else {
		/* Deliver the message to the user. */
		ipmi_inc_stat(intf, handled_commands);

		recv_msg = ipmi_alloc_recv_msg();
		if (!recv_msg) {
			/*
			 * We couldn't allocate memory for the
			 * message, so requeue it for handling
			 * later.
			 */
			rv = 1;
			kref_put(&user->refcount, free_user);
		} else {
			/*
			 * OEM Messages are expected to be delivered via
			 * the system interface to SMS software.  We might
			 * need to visit this again depending on OEM
			 * requirements
			 */
			smi_addr = ((struct ipmi_system_interface_addr *)
				    &(recv_msg->addr));
			smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
			smi_addr->channel = IPMI_BMC_CHANNEL;
			smi_addr->lun = msg->rsp[0] & 3;

			recv_msg->user = user;
			recv_msg->user_msg_data = NULL;
			recv_msg->recv_type = IPMI_OEM_RECV_TYPE;
			recv_msg->msg.netfn = msg->rsp[0] >> 2;
			recv_msg->msg.cmd = msg->rsp[1];
			recv_msg->msg.data = recv_msg->msg_data;

			/*
			 * The message starts at byte 4 which follows the
			 * the Channel Byte in the "GET MESSAGE" command
			 */
			recv_msg->msg.data_len = msg->rsp_size - 4;
			memcpy(recv_msg->msg_data,
			       &(msg->rsp[4]),
			       msg->rsp_size - 4);
			deliver_response(recv_msg);
		}
	}

	return rv;
}

L
Linus Torvalds 已提交
3470 3471 3472 3473
static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
				     struct ipmi_smi_msg  *msg)
{
	struct ipmi_system_interface_addr *smi_addr;
3474

L
Linus Torvalds 已提交
3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499
	recv_msg->msgid = 0;
	smi_addr = (struct ipmi_system_interface_addr *) &(recv_msg->addr);
	smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
	smi_addr->channel = IPMI_BMC_CHANNEL;
	smi_addr->lun = msg->rsp[0] & 3;
	recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
	recv_msg->msg.netfn = msg->rsp[0] >> 2;
	recv_msg->msg.cmd = msg->rsp[1];
	memcpy(recv_msg->msg_data, &(msg->rsp[3]), msg->rsp_size - 3);
	recv_msg->msg.data = recv_msg->msg_data;
	recv_msg->msg.data_len = msg->rsp_size - 3;
}

static int handle_read_event_rsp(ipmi_smi_t          intf,
				 struct ipmi_smi_msg *msg)
{
	struct ipmi_recv_msg *recv_msg, *recv_msg2;
	struct list_head     msgs;
	ipmi_user_t          user;
	int                  rv = 0;
	int                  deliver_count = 0;
	unsigned long        flags;

	if (msg->rsp_size < 19) {
		/* Message is too small to be an IPMB event. */
3500
		ipmi_inc_stat(intf, invalid_events);
L
Linus Torvalds 已提交
3501 3502 3503 3504 3505 3506 3507 3508 3509 3510
		return 0;
	}

	if (msg->rsp[2] != 0) {
		/* An error getting the event, just ignore it. */
		return 0;
	}

	INIT_LIST_HEAD(&msgs);

3511
	spin_lock_irqsave(&intf->events_lock, flags);
L
Linus Torvalds 已提交
3512

3513
	ipmi_inc_stat(intf, events);
L
Linus Torvalds 已提交
3514

3515 3516 3517 3518
	/*
	 * Allocate and fill in one message for every user that is
	 * getting events.
	 */
3519 3520
	rcu_read_lock();
	list_for_each_entry_rcu(user, &intf->users, link) {
3521
		if (!user->gets_events)
L
Linus Torvalds 已提交
3522 3523 3524
			continue;

		recv_msg = ipmi_alloc_recv_msg();
3525
		if (!recv_msg) {
3526
			rcu_read_unlock();
3527 3528
			list_for_each_entry_safe(recv_msg, recv_msg2, &msgs,
						 link) {
L
Linus Torvalds 已提交
3529 3530 3531
				list_del(&recv_msg->link);
				ipmi_free_recv_msg(recv_msg);
			}
3532 3533 3534 3535 3536
			/*
			 * We couldn't allocate memory for the
			 * message, so requeue it for handling
			 * later.
			 */
L
Linus Torvalds 已提交
3537 3538 3539 3540 3541 3542 3543 3544
			rv = 1;
			goto out;
		}

		deliver_count++;

		copy_event_into_recv_msg(recv_msg, msg);
		recv_msg->user = user;
3545
		kref_get(&user->refcount);
L
Linus Torvalds 已提交
3546 3547
		list_add_tail(&(recv_msg->link), &msgs);
	}
3548
	rcu_read_unlock();
L
Linus Torvalds 已提交
3549 3550 3551 3552 3553 3554 3555 3556

	if (deliver_count) {
		/* Now deliver all the messages. */
		list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
			list_del(&recv_msg->link);
			deliver_response(recv_msg);
		}
	} else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
3557 3558 3559 3560
		/*
		 * No one to receive the message, put it in queue if there's
		 * not already too many things in the queue.
		 */
L
Linus Torvalds 已提交
3561
		recv_msg = ipmi_alloc_recv_msg();
3562
		if (!recv_msg) {
3563 3564 3565 3566 3567
			/*
			 * We couldn't allocate memory for the
			 * message, so requeue it for handling
			 * later.
			 */
L
Linus Torvalds 已提交
3568 3569 3570 3571 3572 3573
			rv = 1;
			goto out;
		}

		copy_event_into_recv_msg(recv_msg, msg);
		list_add_tail(&(recv_msg->link), &(intf->waiting_events));
3574
		intf->waiting_events_count++;
3575
	} else if (!intf->event_msg_printed) {
3576 3577 3578 3579
		/*
		 * There's too many things in the queue, discard this
		 * message.
		 */
3580 3581 3582
		printk(KERN_WARNING PFX "Event queue full, discarding"
		       " incoming events\n");
		intf->event_msg_printed = 1;
L
Linus Torvalds 已提交
3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594
	}

 out:
	spin_unlock_irqrestore(&(intf->events_lock), flags);

	return rv;
}

static int handle_bmc_rsp(ipmi_smi_t          intf,
			  struct ipmi_smi_msg *msg)
{
	struct ipmi_recv_msg *recv_msg;
3595
	struct ipmi_user     *user;
L
Linus Torvalds 已提交
3596 3597

	recv_msg = (struct ipmi_recv_msg *) msg->user_data;
3598 3599 3600 3601 3602 3603
	if (recv_msg == NULL) {
		printk(KERN_WARNING
		       "IPMI message received with no owner. This\n"
		       "could be because of a malformed message, or\n"
		       "because of a hardware error.  Contact your\n"
		       "hardware vender for assistance\n");
3604 3605
		return 0;
	}
L
Linus Torvalds 已提交
3606

3607
	user = recv_msg->user;
L
Linus Torvalds 已提交
3608
	/* Make sure the user still exists. */
3609
	if (user && !user->valid) {
3610
		/* The user for the message went away, so give up. */
3611
		ipmi_inc_stat(intf, unhandled_local_responses);
L
Linus Torvalds 已提交
3612 3613 3614 3615
		ipmi_free_recv_msg(recv_msg);
	} else {
		struct ipmi_system_interface_addr *smi_addr;

3616
		ipmi_inc_stat(intf, handled_local_responses);
L
Linus Torvalds 已提交
3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636
		recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
		recv_msg->msgid = msg->msgid;
		smi_addr = ((struct ipmi_system_interface_addr *)
			    &(recv_msg->addr));
		smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
		smi_addr->channel = IPMI_BMC_CHANNEL;
		smi_addr->lun = msg->rsp[0] & 3;
		recv_msg->msg.netfn = msg->rsp[0] >> 2;
		recv_msg->msg.cmd = msg->rsp[1];
		memcpy(recv_msg->msg_data,
		       &(msg->rsp[2]),
		       msg->rsp_size - 2);
		recv_msg->msg.data = recv_msg->msg_data;
		recv_msg->msg.data_len = msg->rsp_size - 2;
		deliver_response(recv_msg);
	}

	return 0;
}

3637
/*
3638
 * Handle a received message.  Return 1 if the message should be requeued,
3639 3640 3641
 * 0 if the message should be freed, or -1 if the message should not
 * be freed or requeued.
 */
3642
static int handle_one_recv_msg(ipmi_smi_t          intf,
L
Linus Torvalds 已提交
3643 3644 3645 3646 3647 3648 3649 3650
			       struct ipmi_smi_msg *msg)
{
	int requeue;
	int chan;

#ifdef DEBUG_MSGING
	int m;
	printk("Recv:");
C
Corey Minyard 已提交
3651
	for (m = 0; m < msg->rsp_size; m++)
L
Linus Torvalds 已提交
3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665
		printk(" %2.2x", msg->rsp[m]);
	printk("\n");
#endif
	if (msg->rsp_size < 2) {
		/* Message is too small to be correct. */
		printk(KERN_WARNING PFX "BMC returned to small a message"
		       " for netfn %x cmd %x, got %d bytes\n",
		       (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size);

		/* Generate an error response for the message. */
		msg->rsp[0] = msg->data[0] | (1 << 2);
		msg->rsp[1] = msg->data[1];
		msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
		msg->rsp_size = 3;
3666 3667 3668 3669 3670 3671
	} else if (((msg->rsp[0] >> 2) != ((msg->data[0] >> 2) | 1))
		   || (msg->rsp[1] != msg->data[1])) {
		/*
		 * The NetFN and Command in the response is not even
		 * marginally correct.
		 */
L
Linus Torvalds 已提交
3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685
		printk(KERN_WARNING PFX "BMC returned incorrect response,"
		       " expected netfn %x cmd %x, got netfn %x cmd %x\n",
		       (msg->data[0] >> 2) | 1, msg->data[1],
		       msg->rsp[0] >> 2, msg->rsp[1]);

		/* Generate an error response for the message. */
		msg->rsp[0] = msg->data[0] | (1 << 2);
		msg->rsp[1] = msg->data[1];
		msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
		msg->rsp_size = 3;
	}

	if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
	    && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
3686 3687 3688 3689 3690
	    && (msg->user_data != NULL)) {
		/*
		 * It's a response to a response we sent.  For this we
		 * deliver a send message response to the user.
		 */
3691
		struct ipmi_recv_msg     *recv_msg = msg->user_data;
L
Linus Torvalds 已提交
3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702

		requeue = 0;
		if (msg->rsp_size < 2)
			/* Message is too small to be correct. */
			goto out;

		chan = msg->data[2] & 0x0f;
		if (chan >= IPMI_MAX_CHANNELS)
			/* Invalid channel number */
			goto out;

3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714
		if (!recv_msg)
			goto out;

		/* Make sure the user still exists. */
		if (!recv_msg->user || !recv_msg->user->valid)
			goto out;

		recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
		recv_msg->msg.data = recv_msg->msg_data;
		recv_msg->msg.data_len = 1;
		recv_msg->msg_data[0] = msg->rsp[2];
		deliver_response(recv_msg);
L
Linus Torvalds 已提交
3715
	} else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
3716
		   && (msg->rsp[1] == IPMI_GET_MSG_CMD)) {
L
Linus Torvalds 已提交
3717 3718 3719 3720 3721 3722 3723 3724
		/* It's from the receive queue. */
		chan = msg->rsp[3] & 0xf;
		if (chan >= IPMI_MAX_CHANNELS) {
			/* Invalid channel number */
			requeue = 0;
			goto out;
		}

D
dann frazier 已提交
3725
		/*
C
Corey Minyard 已提交
3726 3727 3728 3729 3730
		 * We need to make sure the channels have been initialized.
		 * The channel_handler routine will set the "curr_channel"
		 * equal to or greater than IPMI_MAX_CHANNELS when all the
		 * channels for this interface have been initialized.
		 */
D
dann frazier 已提交
3731
		if (intf->curr_channel < IPMI_MAX_CHANNELS) {
C
Corey Minyard 已提交
3732
			requeue = 0; /* Throw the message away */
D
dann frazier 已提交
3733 3734 3735
			goto out;
		}

L
Linus Torvalds 已提交
3736 3737 3738
		switch (intf->channels[chan].medium) {
		case IPMI_CHANNEL_MEDIUM_IPMB:
			if (msg->rsp[4] & 0x04) {
3739 3740 3741 3742
				/*
				 * It's a response, so find the
				 * requesting message and send it up.
				 */
L
Linus Torvalds 已提交
3743 3744
				requeue = handle_ipmb_get_msg_rsp(intf, msg);
			} else {
3745 3746 3747 3748
				/*
				 * It's a command to the SMS from some other
				 * entity.  Handle that.
				 */
L
Linus Torvalds 已提交
3749 3750 3751 3752 3753 3754 3755
				requeue = handle_ipmb_get_msg_cmd(intf, msg);
			}
			break;

		case IPMI_CHANNEL_MEDIUM_8023LAN:
		case IPMI_CHANNEL_MEDIUM_ASYNC:
			if (msg->rsp[6] & 0x04) {
3756 3757 3758 3759
				/*
				 * It's a response, so find the
				 * requesting message and send it up.
				 */
L
Linus Torvalds 已提交
3760 3761
				requeue = handle_lan_get_msg_rsp(intf, msg);
			} else {
3762 3763 3764 3765
				/*
				 * It's a command to the SMS from some other
				 * entity.  Handle that.
				 */
L
Linus Torvalds 已提交
3766 3767 3768 3769 3770
				requeue = handle_lan_get_msg_cmd(intf, msg);
			}
			break;

		default:
D
dann frazier 已提交
3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784
			/* Check for OEM Channels.  Clients had better
			   register for these commands. */
			if ((intf->channels[chan].medium
			     >= IPMI_CHANNEL_MEDIUM_OEM_MIN)
			    && (intf->channels[chan].medium
				<= IPMI_CHANNEL_MEDIUM_OEM_MAX)) {
				requeue = handle_oem_get_msg_cmd(intf, msg);
			} else {
				/*
				 * We don't handle the channel type, so just
				 * free the message.
				 */
				requeue = 0;
			}
L
Linus Torvalds 已提交
3785 3786 3787
		}

	} else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
3788
		   && (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD)) {
3789
		/* It's an asynchronous event. */
L
Linus Torvalds 已提交
3790 3791 3792 3793 3794 3795 3796 3797 3798 3799
		requeue = handle_read_event_rsp(intf, msg);
	} else {
		/* It's a response from the local BMC. */
		requeue = handle_bmc_rsp(intf, msg);
	}

 out:
	return requeue;
}

3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811
/*
 * If there are messages in the queue or pretimeouts, handle them.
 */
static void handle_new_recv_msgs(ipmi_smi_t intf)
{
	struct ipmi_smi_msg  *smi_msg;
	unsigned long        flags = 0;
	int                  rv;
	int                  run_to_completion = intf->run_to_completion;

	/* See if any waiting messages need to be processed. */
	if (!run_to_completion)
3812 3813 3814
		spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
	while (!list_empty(&intf->waiting_rcv_msgs)) {
		smi_msg = list_entry(intf->waiting_rcv_msgs.next,
3815 3816
				     struct ipmi_smi_msg, link);
		if (!run_to_completion)
3817 3818
			spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
					       flags);
3819 3820
		rv = handle_one_recv_msg(intf, smi_msg);
		if (!run_to_completion)
3821
			spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
3822
		if (rv > 0) {
3823 3824 3825 3826 3827
			/*
			 * To preserve message order, quit if we
			 * can't handle a message.
			 */
			break;
3828 3829 3830 3831 3832 3833
		} else {
			list_del(&smi_msg->link);
			if (rv == 0)
				/* Message handled */
				ipmi_free_smi_msg(smi_msg);
			/* If rv < 0, fatal error, del but don't free. */
3834 3835 3836
		}
	}
	if (!run_to_completion)
3837
		spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock, flags);
3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857

	/*
	 * If the pretimout count is non-zero, decrement one from it and
	 * deliver pretimeouts to all the users.
	 */
	if (atomic_add_unless(&intf->watchdog_pretimeouts_to_deliver, -1, 0)) {
		ipmi_user_t user;

		rcu_read_lock();
		list_for_each_entry_rcu(user, &intf->users, link) {
			if (user->handler->ipmi_watchdog_pretimeout)
				user->handler->ipmi_watchdog_pretimeout(
					user->handler_data);
		}
		rcu_read_unlock();
	}
}

static void smi_recv_tasklet(unsigned long val)
{
3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889
	unsigned long flags = 0; /* keep us warning-free. */
	ipmi_smi_t intf = (ipmi_smi_t) val;
	int run_to_completion = intf->run_to_completion;
	struct ipmi_smi_msg *newmsg = NULL;

	/*
	 * Start the next message if available.
	 *
	 * Do this here, not in the actual receiver, because we may deadlock
	 * because the lower layer is allowed to hold locks while calling
	 * message delivery.
	 */
	if (!run_to_completion)
		spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
	if (intf->curr_msg == NULL && !intf->in_shutdown) {
		struct list_head *entry = NULL;

		/* Pick the high priority queue first. */
		if (!list_empty(&intf->hp_xmit_msgs))
			entry = intf->hp_xmit_msgs.next;
		else if (!list_empty(&intf->xmit_msgs))
			entry = intf->xmit_msgs.next;

		if (entry) {
			list_del(entry);
			newmsg = list_entry(entry, struct ipmi_smi_msg, link);
			intf->curr_msg = newmsg;
		}
	}
	if (!run_to_completion)
		spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
	if (newmsg)
3890
		intf->handlers->sender(intf->send_info, newmsg);
3891 3892

	handle_new_recv_msgs(intf);
3893 3894
}

L
Linus Torvalds 已提交
3895 3896 3897 3898
/* Handle a new message from the lower layer. */
void ipmi_smi_msg_received(ipmi_smi_t          intf,
			   struct ipmi_smi_msg *msg)
{
3899
	unsigned long flags = 0; /* keep us warning-free. */
3900
	int run_to_completion = intf->run_to_completion;
L
Linus Torvalds 已提交
3901 3902 3903 3904

	if ((msg->data_size >= 2)
	    && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
	    && (msg->data[1] == IPMI_SEND_MSG_CMD)
3905
	    && (msg->user_data == NULL)) {
3906 3907 3908 3909

		if (intf->in_shutdown)
			goto free_msg;

3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922
		/*
		 * This is the local response to a command send, start
		 * the timer for these.  The user_data will not be
		 * NULL if this is a response send, and we will let
		 * response sends just go through.
		 */

		/*
		 * Check for errors, if we get certain errors (ones
		 * that mean basically we can try again later), we
		 * ignore them and start the timer.  Otherwise we
		 * report the error immediately.
		 */
L
Linus Torvalds 已提交
3923 3924
		if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
		    && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
3925 3926
		    && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR)
		    && (msg->rsp[2] != IPMI_BUS_ERR)
3927
		    && (msg->rsp[2] != IPMI_NAK_ON_WRITE_ERR)) {
L
Linus Torvalds 已提交
3928 3929 3930 3931 3932 3933 3934 3935 3936
			int chan = msg->rsp[3] & 0xf;

			/* Got an error sending the message, handle it. */
			if (chan >= IPMI_MAX_CHANNELS)
				; /* This shouldn't happen */
			else if ((intf->channels[chan].medium
				  == IPMI_CHANNEL_MEDIUM_8023LAN)
				 || (intf->channels[chan].medium
				     == IPMI_CHANNEL_MEDIUM_ASYNC))
3937
				ipmi_inc_stat(intf, sent_lan_command_errs);
L
Linus Torvalds 已提交
3938
			else
3939
				ipmi_inc_stat(intf, sent_ipmb_command_errs);
L
Linus Torvalds 已提交
3940
			intf_err_seq(intf, msg->msgid, msg->rsp[2]);
3941
		} else
L
Linus Torvalds 已提交
3942 3943 3944
			/* The message was sent, start the timer. */
			intf_start_seq_timer(intf, msg->msgid);

3945
free_msg:
L
Linus Torvalds 已提交
3946
		ipmi_free_smi_msg(msg);
3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957
	} else {
		/*
		 * To preserve message order, we keep a queue and deliver from
		 * a tasklet.
		 */
		if (!run_to_completion)
			spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags);
		list_add_tail(&msg->link, &intf->waiting_rcv_msgs);
		if (!run_to_completion)
			spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock,
					       flags);
L
Linus Torvalds 已提交
3958 3959
	}

3960
	if (!run_to_completion)
3961
		spin_lock_irqsave(&intf->xmit_msgs_lock, flags);
3962 3963 3964 3965
	/*
	 * We can get an asynchronous event or receive message in addition
	 * to commands we send.
	 */
3966 3967
	if (msg == intf->curr_msg)
		intf->curr_msg = NULL;
3968
	if (!run_to_completion)
3969
		spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags);
3970

3971 3972 3973 3974
	if (run_to_completion)
		smi_recv_tasklet((unsigned long) intf);
	else
		tasklet_schedule(&intf->recv_tasklet);
L
Linus Torvalds 已提交
3975
}
3976
EXPORT_SYMBOL(ipmi_smi_msg_received);
L
Linus Torvalds 已提交
3977 3978 3979

void ipmi_smi_watchdog_pretimeout(ipmi_smi_t intf)
{
3980 3981 3982
	if (intf->in_shutdown)
		return;

3983 3984
	atomic_set(&intf->watchdog_pretimeouts_to_deliver, 1);
	tasklet_schedule(&intf->recv_tasklet);
L
Linus Torvalds 已提交
3985
}
3986
EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
L
Linus Torvalds 已提交
3987

C
Corey Minyard 已提交
3988 3989 3990
static struct ipmi_smi_msg *
smi_from_recv_msg(ipmi_smi_t intf, struct ipmi_recv_msg *recv_msg,
		  unsigned char seq, long seqid)
L
Linus Torvalds 已提交
3991
{
C
Corey Minyard 已提交
3992
	struct ipmi_smi_msg *smi_msg = ipmi_alloc_smi_msg();
L
Linus Torvalds 已提交
3993
	if (!smi_msg)
3994 3995 3996 3997
		/*
		 * If we can't allocate the message, then just return, we
		 * get 4 retries, so this should be ok.
		 */
C
Corey Minyard 已提交
3998
		return NULL;
L
Linus Torvalds 已提交
3999 4000 4001 4002

	memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
	smi_msg->data_size = recv_msg->msg.data_len;
	smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
4003

L
Linus Torvalds 已提交
4004 4005 4006 4007
#ifdef DEBUG_MSGING
	{
		int m;
		printk("Resend: ");
C
Corey Minyard 已提交
4008
		for (m = 0; m < smi_msg->data_size; m++)
L
Linus Torvalds 已提交
4009 4010 4011 4012
			printk(" %2.2x", smi_msg->data[m]);
		printk("\n");
	}
#endif
C
Corey Minyard 已提交
4013
	return smi_msg;
L
Linus Torvalds 已提交
4014 4015
}

4016 4017
static void check_msg_timeout(ipmi_smi_t intf, struct seq_table *ent,
			      struct list_head *timeouts, long timeout_period,
4018 4019
			      int slot, unsigned long *flags,
			      unsigned int *waiting_msgs)
4020
{
4021
	struct ipmi_recv_msg     *msg;
4022
	const struct ipmi_smi_handlers *handlers;
4023

4024
	if (intf->in_shutdown)
4025
		return;
4026 4027 4028 4029 4030

	if (!ent->inuse)
		return;

	ent->timeout -= timeout_period;
4031 4032
	if (ent->timeout > 0) {
		(*waiting_msgs)++;
4033
		return;
4034
	}
4035 4036 4037 4038 4039 4040 4041

	if (ent->retries_left == 0) {
		/* The message has used all its retries. */
		ent->inuse = 0;
		msg = ent->recv_msg;
		list_add_tail(&msg->link, timeouts);
		if (ent->broadcast)
4042
			ipmi_inc_stat(intf, timed_out_ipmb_broadcasts);
4043
		else if (is_lan_addr(&ent->recv_msg->addr))
4044
			ipmi_inc_stat(intf, timed_out_lan_commands);
4045
		else
4046
			ipmi_inc_stat(intf, timed_out_ipmb_commands);
4047 4048 4049 4050
	} else {
		struct ipmi_smi_msg *smi_msg;
		/* More retries, send again. */

4051 4052
		(*waiting_msgs)++;

4053 4054 4055 4056
		/*
		 * Start with the max timer, set to normal timer after
		 * the message is sent.
		 */
4057 4058 4059 4060
		ent->timeout = MAX_MSG_TIMEOUT;
		ent->retries_left--;
		smi_msg = smi_from_recv_msg(intf, ent->recv_msg, slot,
					    ent->seqid);
4061 4062 4063 4064 4065 4066 4067
		if (!smi_msg) {
			if (is_lan_addr(&ent->recv_msg->addr))
				ipmi_inc_stat(intf,
					      dropped_rexmit_lan_commands);
			else
				ipmi_inc_stat(intf,
					      dropped_rexmit_ipmb_commands);
4068
			return;
4069
		}
4070 4071

		spin_unlock_irqrestore(&intf->seq_lock, *flags);
4072

4073 4074 4075 4076 4077 4078 4079
		/*
		 * Send the new message.  We send with a zero
		 * priority.  It timed out, I doubt time is that
		 * critical now, and high priority messages are really
		 * only for messages to the local MC, which don't get
		 * resent.
		 */
4080
		handlers = intf->handlers;
4081 4082 4083 4084 4085 4086 4087 4088
		if (handlers) {
			if (is_lan_addr(&ent->recv_msg->addr))
				ipmi_inc_stat(intf,
					      retransmitted_lan_commands);
			else
				ipmi_inc_stat(intf,
					      retransmitted_ipmb_commands);

4089
			smi_send(intf, handlers, smi_msg, 0);
4090
		} else
4091 4092
			ipmi_free_smi_msg(smi_msg);

4093 4094 4095 4096
		spin_lock_irqsave(&intf->seq_lock, *flags);
	}
}

4097
static unsigned int ipmi_timeout_handler(ipmi_smi_t intf, long timeout_period)
L
Linus Torvalds 已提交
4098 4099 4100 4101
{
	struct list_head     timeouts;
	struct ipmi_recv_msg *msg, *msg2;
	unsigned long        flags;
4102
	int                  i;
4103
	unsigned int         waiting_msgs = 0;
L
Linus Torvalds 已提交
4104

4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116
	/*
	 * Go through the seq table and find any messages that
	 * have timed out, putting them in the timeouts
	 * list.
	 */
	INIT_LIST_HEAD(&timeouts);
	spin_lock_irqsave(&intf->seq_lock, flags);
	for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++)
		check_msg_timeout(intf, &(intf->seq_table[i]),
				  &timeouts, timeout_period, i,
				  &flags, &waiting_msgs);
	spin_unlock_irqrestore(&intf->seq_lock, flags);
4117

4118 4119
	list_for_each_entry_safe(msg, msg2, &timeouts, link)
		deliver_err_response(msg, IPMI_TIMEOUT_COMPLETION_CODE);
C
Corey Minyard 已提交
4120

4121 4122 4123 4124 4125 4126 4127 4128 4129 4130
	/*
	 * Maintenance mode handling.  Check the timeout
	 * optimistically before we claim the lock.  It may
	 * mean a timeout gets missed occasionally, but that
	 * only means the timeout gets extended by one period
	 * in that case.  No big deal, and it avoids the lock
	 * most of the time.
	 */
	if (intf->auto_maintenance_timeout > 0) {
		spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
C
Corey Minyard 已提交
4131
		if (intf->auto_maintenance_timeout > 0) {
4132 4133 4134 4135
			intf->auto_maintenance_timeout
				-= timeout_period;
			if (!intf->maintenance_mode
			    && (intf->auto_maintenance_timeout <= 0)) {
C
Corey Minyard 已提交
4136
				intf->maintenance_mode_enable = false;
4137
				maintenance_mode_update(intf);
C
Corey Minyard 已提交
4138 4139
			}
		}
4140 4141
		spin_unlock_irqrestore(&intf->maintenance_mode_lock,
				       flags);
L
Linus Torvalds 已提交
4142
	}
4143 4144 4145 4146

	tasklet_schedule(&intf->recv_tasklet);

	return waiting_msgs;
L
Linus Torvalds 已提交
4147 4148
}

4149
static void ipmi_request_event(ipmi_smi_t intf)
L
Linus Torvalds 已提交
4150
{
4151 4152 4153
	/* No event requests when in maintenance mode. */
	if (intf->maintenance_mode_enable)
		return;
C
Corey Minyard 已提交
4154

4155 4156
	if (!intf->in_shutdown)
		intf->handlers->request_events(intf->send_info);
L
Linus Torvalds 已提交
4157 4158 4159 4160
}

static struct timer_list ipmi_timer;

4161
static atomic_t stop_operation;
L
Linus Torvalds 已提交
4162 4163 4164

static void ipmi_timeout(unsigned long data)
{
4165 4166 4167
	ipmi_smi_t intf;
	int nt = 0;

4168
	if (atomic_read(&stop_operation))
L
Linus Torvalds 已提交
4169 4170
		return;

4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184
	rcu_read_lock();
	list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
		int lnt = 0;

		if (atomic_read(&intf->event_waiters)) {
			intf->ticks_to_req_ev--;
			if (intf->ticks_to_req_ev == 0) {
				ipmi_request_event(intf);
				intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
			}
			lnt++;
		}

		lnt += ipmi_timeout_handler(intf, IPMI_TIMEOUT_TIME);
L
Linus Torvalds 已提交
4185

4186 4187 4188 4189 4190
		lnt = !!lnt;
		if (lnt != intf->last_needs_timer &&
					intf->handlers->set_need_watch)
			intf->handlers->set_need_watch(intf->send_info, lnt);
		intf->last_needs_timer = lnt;
L
Linus Torvalds 已提交
4191

4192 4193 4194 4195 4196 4197
		nt += lnt;
	}
	rcu_read_unlock();

	if (nt)
		mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
L
Linus Torvalds 已提交
4198 4199
}

4200 4201 4202 4203 4204 4205
static void need_waiter(ipmi_smi_t intf)
{
	/* Racy, but worst case we start the timer twice. */
	if (!timer_pending(&ipmi_timer))
		mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
}
L
Linus Torvalds 已提交
4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226

static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);

static void free_smi_msg(struct ipmi_smi_msg *msg)
{
	atomic_dec(&smi_msg_inuse_count);
	kfree(msg);
}

struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
{
	struct ipmi_smi_msg *rv;
	rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
	if (rv) {
		rv->done = free_smi_msg;
		rv->user_data = NULL;
		atomic_inc(&smi_msg_inuse_count);
	}
	return rv;
}
4227
EXPORT_SYMBOL(ipmi_alloc_smi_msg);
L
Linus Torvalds 已提交
4228 4229 4230 4231 4232 4233 4234

static void free_recv_msg(struct ipmi_recv_msg *msg)
{
	atomic_dec(&recv_msg_inuse_count);
	kfree(msg);
}

A
Adrian Bunk 已提交
4235
static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
L
Linus Torvalds 已提交
4236 4237 4238 4239 4240
{
	struct ipmi_recv_msg *rv;

	rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
	if (rv) {
4241
		rv->user = NULL;
L
Linus Torvalds 已提交
4242 4243 4244 4245 4246 4247
		rv->done = free_recv_msg;
		atomic_inc(&recv_msg_inuse_count);
	}
	return rv;
}

4248 4249 4250 4251 4252 4253
void ipmi_free_recv_msg(struct ipmi_recv_msg *msg)
{
	if (msg->user)
		kref_put(&msg->user->refcount, free_user);
	msg->done(msg);
}
4254
EXPORT_SYMBOL(ipmi_free_recv_msg);
4255

L
Linus Torvalds 已提交
4256 4257
#ifdef CONFIG_IPMI_PANIC_EVENT

4258 4259
static atomic_t panic_done_count = ATOMIC_INIT(0);

L
Linus Torvalds 已提交
4260 4261
static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
{
4262
	atomic_dec(&panic_done_count);
L
Linus Torvalds 已提交
4263 4264 4265 4266
}

static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
{
4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299
	atomic_dec(&panic_done_count);
}

/*
 * Inside a panic, send a message and wait for a response.
 */
static void ipmi_panic_request_and_wait(ipmi_smi_t           intf,
					struct ipmi_addr     *addr,
					struct kernel_ipmi_msg *msg)
{
	struct ipmi_smi_msg  smi_msg;
	struct ipmi_recv_msg recv_msg;
	int rv;

	smi_msg.done = dummy_smi_done_handler;
	recv_msg.done = dummy_recv_done_handler;
	atomic_add(2, &panic_done_count);
	rv = i_ipmi_request(NULL,
			    intf,
			    addr,
			    0,
			    msg,
			    intf,
			    &smi_msg,
			    &recv_msg,
			    0,
			    intf->channels[0].address,
			    intf->channels[0].lun,
			    0, 1); /* Don't retry, and don't wait. */
	if (rv)
		atomic_sub(2, &panic_done_count);
	while (atomic_read(&panic_done_count) != 0)
		ipmi_poll(intf);
L
Linus Torvalds 已提交
4300 4301 4302
}

#ifdef CONFIG_IPMI_PANIC_STRING
4303
static void event_receiver_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
L
Linus Torvalds 已提交
4304
{
4305 4306 4307
	if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
	    && (msg->msg.netfn == IPMI_NETFN_SENSOR_EVENT_RESPONSE)
	    && (msg->msg.cmd == IPMI_GET_EVENT_RECEIVER_CMD)
4308
	    && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
L
Linus Torvalds 已提交
4309
		/* A get event receiver command, save it. */
4310 4311
		intf->event_receiver = msg->msg.data[1];
		intf->event_receiver_lun = msg->msg.data[2] & 0x3;
L
Linus Torvalds 已提交
4312 4313 4314
	}
}

4315
static void device_id_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
L
Linus Torvalds 已提交
4316
{
4317 4318 4319
	if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
	    && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
	    && (msg->msg.cmd == IPMI_GET_DEVICE_ID_CMD)
4320 4321 4322 4323 4324
	    && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
		/*
		 * A get device id command, save if we are an event
		 * receiver or generator.
		 */
4325 4326
		intf->local_sel_device = (msg->msg.data[6] >> 2) & 1;
		intf->local_event_generator = (msg->msg.data[6] >> 5) & 1;
L
Linus Torvalds 已提交
4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348
	}
}
#endif

static void send_panic_events(char *str)
{
	struct kernel_ipmi_msg            msg;
	ipmi_smi_t                        intf;
	unsigned char                     data[16];
	struct ipmi_system_interface_addr *si;
	struct ipmi_addr                  addr;

	si = (struct ipmi_system_interface_addr *) &addr;
	si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
	si->channel = IPMI_BMC_CHANNEL;
	si->lun = 0;

	/* Fill in an event telling that we have failed. */
	msg.netfn = 0x04; /* Sensor or Event. */
	msg.cmd = 2; /* Platform event command. */
	msg.data = data;
	msg.data_len = 8;
M
Matt Domsch 已提交
4349
	data[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */
L
Linus Torvalds 已提交
4350 4351 4352 4353 4354
	data[1] = 0x03; /* This is for IPMI 1.0. */
	data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
	data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
	data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */

4355 4356 4357 4358
	/*
	 * Put a few breadcrumbs in.  Hopefully later we can add more things
	 * to make the panic events more useful.
	 */
L
Linus Torvalds 已提交
4359 4360 4361 4362 4363 4364 4365
	if (str) {
		data[3] = str[0];
		data[6] = str[1];
		data[7] = str[2];
	}

	/* For every registered interface, send the event. */
4366
	list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4367 4368
		if (!intf->handlers)
			/* Interface is not ready. */
L
Linus Torvalds 已提交
4369 4370 4371
			continue;

		/* Send the event announcing the panic. */
4372
		ipmi_panic_request_and_wait(intf, &addr, &msg);
L
Linus Torvalds 已提交
4373 4374 4375
	}

#ifdef CONFIG_IPMI_PANIC_STRING
4376 4377 4378 4379 4380
	/*
	 * On every interface, dump a bunch of OEM event holding the
	 * string.
	 */
	if (!str)
L
Linus Torvalds 已提交
4381 4382
		return;

4383 4384
	/* For every registered interface, send the event. */
	list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
L
Linus Torvalds 已提交
4385 4386 4387 4388
		char                  *p = str;
		struct ipmi_ipmb_addr *ipmb;
		int                   j;

4389 4390
		if (intf->intf_num == -1)
			/* Interface was not ready yet. */
L
Linus Torvalds 已提交
4391 4392
			continue;

4393 4394 4395 4396 4397 4398 4399 4400
		/*
		 * intf_num is used as an marker to tell if the
		 * interface is valid.  Thus we need a read barrier to
		 * make sure data fetched before checking intf_num
		 * won't be used.
		 */
		smp_rmb();

4401 4402 4403 4404 4405 4406 4407
		/*
		 * First job here is to figure out where to send the
		 * OEM events.  There's no way in IPMI to send OEM
		 * events using an event send command, so we have to
		 * find the SEL to put them in and stick them in
		 * there.
		 */
L
Linus Torvalds 已提交
4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419

		/* Get capabilities from the get device id. */
		intf->local_sel_device = 0;
		intf->local_event_generator = 0;
		intf->event_receiver = 0;

		/* Request the device info from the local MC. */
		msg.netfn = IPMI_NETFN_APP_REQUEST;
		msg.cmd = IPMI_GET_DEVICE_ID_CMD;
		msg.data = NULL;
		msg.data_len = 0;
		intf->null_user_handler = device_id_fetcher;
4420
		ipmi_panic_request_and_wait(intf, &addr, &msg);
L
Linus Torvalds 已提交
4421 4422 4423 4424 4425 4426 4427 4428

		if (intf->local_event_generator) {
			/* Request the event receiver from the local MC. */
			msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
			msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
			msg.data = NULL;
			msg.data_len = 0;
			intf->null_user_handler = event_receiver_fetcher;
4429
			ipmi_panic_request_and_wait(intf, &addr, &msg);
L
Linus Torvalds 已提交
4430 4431 4432
		}
		intf->null_user_handler = NULL;

4433 4434 4435 4436 4437 4438
		/*
		 * Validate the event receiver.  The low bit must not
		 * be 1 (it must be a valid IPMB address), it cannot
		 * be zero, and it must not be my address.
		 */
		if (((intf->event_receiver & 1) == 0)
L
Linus Torvalds 已提交
4439
		    && (intf->event_receiver != 0)
4440 4441 4442 4443 4444
		    && (intf->event_receiver != intf->channels[0].address)) {
			/*
			 * The event receiver is valid, send an IPMB
			 * message.
			 */
L
Linus Torvalds 已提交
4445 4446 4447 4448 4449 4450
			ipmb = (struct ipmi_ipmb_addr *) &addr;
			ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
			ipmb->channel = 0; /* FIXME - is this right? */
			ipmb->lun = intf->event_receiver_lun;
			ipmb->slave_addr = intf->event_receiver;
		} else if (intf->local_sel_device) {
4451 4452 4453 4454 4455
			/*
			 * The event receiver was not valid (or was
			 * me), but I am an SEL device, just dump it
			 * in my SEL.
			 */
L
Linus Torvalds 已提交
4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476
			si = (struct ipmi_system_interface_addr *) &addr;
			si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
			si->channel = IPMI_BMC_CHANNEL;
			si->lun = 0;
		} else
			continue; /* No where to send the event. */

		msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
		msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
		msg.data = data;
		msg.data_len = 16;

		j = 0;
		while (*p) {
			int size = strlen(p);

			if (size > 11)
				size = 11;
			data[0] = 0;
			data[1] = 0;
			data[2] = 0xf0; /* OEM event without timestamp. */
4477
			data[3] = intf->channels[0].address;
L
Linus Torvalds 已提交
4478
			data[4] = j++; /* sequence # */
4479 4480 4481 4482
			/*
			 * Always give 11 bytes, so strncpy will fill
			 * it with zeroes for me.
			 */
L
Linus Torvalds 已提交
4483 4484 4485
			strncpy(data+5, p, 11);
			p += size;

4486
			ipmi_panic_request_and_wait(intf, &addr, &msg);
L
Linus Torvalds 已提交
4487
		}
4488
	}
L
Linus Torvalds 已提交
4489 4490 4491 4492
#endif /* CONFIG_IPMI_PANIC_STRING */
}
#endif /* CONFIG_IPMI_PANIC_EVENT */

R
Randy Dunlap 已提交
4493
static int has_panicked;
L
Linus Torvalds 已提交
4494 4495 4496

static int panic_event(struct notifier_block *this,
		       unsigned long         event,
4497
		       void                  *ptr)
L
Linus Torvalds 已提交
4498 4499 4500
{
	ipmi_smi_t intf;

L
Lee Revell 已提交
4501
	if (has_panicked)
L
Linus Torvalds 已提交
4502
		return NOTIFY_DONE;
L
Lee Revell 已提交
4503
	has_panicked = 1;
L
Linus Torvalds 已提交
4504 4505

	/* For every registered interface, set it to run to completion. */
4506
	list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4507 4508
		if (!intf->handlers)
			/* Interface is not ready. */
L
Linus Torvalds 已提交
4509 4510
			continue;

4511
		intf->run_to_completion = 1;
L
Linus Torvalds 已提交
4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529
		intf->handlers->set_run_to_completion(intf->send_info, 1);
	}

#ifdef CONFIG_IPMI_PANIC_EVENT
	send_panic_events(ptr);
#endif

	return NOTIFY_DONE;
}

static struct notifier_block panic_block = {
	.notifier_call	= panic_event,
	.next		= NULL,
	.priority	= 200	/* priority: INT_MAX >= x >= 0 */
};

static int ipmi_init_msghandler(void)
{
4530
	int rv;
L
Linus Torvalds 已提交
4531 4532 4533 4534

	if (initialized)
		return 0;

4535
	rv = driver_register(&ipmidriver.driver);
4536 4537 4538 4539 4540
	if (rv) {
		printk(KERN_ERR PFX "Could not register IPMI driver\n");
		return rv;
	}

L
Linus Torvalds 已提交
4541
	printk(KERN_INFO "ipmi message handler version "
4542
	       IPMI_DRIVER_VERSION "\n");
L
Linus Torvalds 已提交
4543

4544
#ifdef CONFIG_PROC_FS
L
Linus Torvalds 已提交
4545 4546 4547
	proc_ipmi_root = proc_mkdir("ipmi", NULL);
	if (!proc_ipmi_root) {
	    printk(KERN_ERR PFX "Unable to create IPMI proc dir");
4548
	    driver_unregister(&ipmidriver.driver);
L
Linus Torvalds 已提交
4549 4550 4551
	    return -ENOMEM;
	}

4552
#endif /* CONFIG_PROC_FS */
L
Linus Torvalds 已提交
4553

4554 4555
	setup_timer(&ipmi_timer, ipmi_timeout, 0);
	mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
L
Linus Torvalds 已提交
4556

4557
	atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
L
Linus Torvalds 已提交
4558 4559 4560 4561 4562 4563

	initialized = 1;

	return 0;
}

4564
static int __init ipmi_init_msghandler_mod(void)
L
Linus Torvalds 已提交
4565 4566 4567 4568 4569
{
	ipmi_init_msghandler();
	return 0;
}

4570
static void __exit cleanup_ipmi(void)
L
Linus Torvalds 已提交
4571 4572 4573 4574 4575 4576
{
	int count;

	if (!initialized)
		return;

4577
	atomic_notifier_chain_unregister(&panic_notifier_list, &panic_block);
L
Linus Torvalds 已提交
4578

4579 4580 4581 4582
	/*
	 * This can't be called if any interfaces exist, so no worry
	 * about shutting down the interfaces.
	 */
L
Linus Torvalds 已提交
4583

4584 4585 4586 4587 4588
	/*
	 * Tell the timer to stop, then wait for it to stop.  This
	 * avoids problems with race conditions removing the timer
	 * here.
	 */
4589 4590
	atomic_inc(&stop_operation);
	del_timer_sync(&ipmi_timer);
L
Linus Torvalds 已提交
4591

4592
#ifdef CONFIG_PROC_FS
4593
	proc_remove(proc_ipmi_root);
4594
#endif /* CONFIG_PROC_FS */
L
Linus Torvalds 已提交
4595

4596
	driver_unregister(&ipmidriver.driver);
4597

L
Linus Torvalds 已提交
4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613
	initialized = 0;

	/* Check for buffer leaks. */
	count = atomic_read(&smi_msg_inuse_count);
	if (count != 0)
		printk(KERN_WARNING PFX "SMI message count %d at exit\n",
		       count);
	count = atomic_read(&recv_msg_inuse_count);
	if (count != 0)
		printk(KERN_WARNING PFX "recv message count %d at exit\n",
		       count);
}
module_exit(cleanup_ipmi);

module_init(ipmi_init_msghandler_mod);
MODULE_LICENSE("GPL");
4614
MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
4615 4616
MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI"
		   " interface.");
4617
MODULE_VERSION(IPMI_DRIVER_VERSION);