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