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