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