1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * ipmi_si.c
4 *
5 * The interface to the IPMI driver for the system interfaces (KCS, SMIC,
6 * BT).
7 *
8 * Author: MontaVista Software, Inc.
9 * Corey Minyard <minyard@mvista.com>
10 * source@mvista.com
11 *
12 * Copyright 2002 MontaVista Software Inc.
13 * Copyright 2006 IBM Corp., Christian Krafft <krafft@de.ibm.com>
14 */
15
16 /*
17 * This file holds the "policy" for the interface to the SMI state
18 * machine. It does the configuration, handles timers and interrupts,
19 * and drives the real SMI state machine.
20 */
21
22 #define pr_fmt(fmt) "ipmi_si: " fmt
23
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/sched.h>
27 #include <linux/seq_file.h>
28 #include <linux/timer.h>
29 #include <linux/errno.h>
30 #include <linux/spinlock.h>
31 #include <linux/slab.h>
32 #include <linux/delay.h>
33 #include <linux/list.h>
34 #include <linux/notifier.h>
35 #include <linux/mutex.h>
36 #include <linux/kthread.h>
37 #include <asm/irq.h>
38 #include <linux/interrupt.h>
39 #include <linux/rcupdate.h>
40 #include <linux/ipmi.h>
41 #include <linux/ipmi_smi.h>
42 #include "ipmi_si.h"
43 #include "ipmi_si_sm.h"
44 #include <linux/string.h>
45 #include <linux/ctype.h>
46
47 /* Measure times between events in the driver. */
48 #undef DEBUG_TIMING
49
50 /* Call every 10 ms. */
51 #define SI_TIMEOUT_TIME_USEC 10000
52 #define SI_USEC_PER_JIFFY (1000000/HZ)
53 #define SI_TIMEOUT_JIFFIES (SI_TIMEOUT_TIME_USEC/SI_USEC_PER_JIFFY)
54 #define SI_SHORT_TIMEOUT_USEC 250 /* .25ms when the SM request a
55 short timeout */
56 #define SI_TIMEOUT_HOSED (HZ) /* 1 second when in hosed state. */
57
58 enum si_intf_state {
59 SI_NORMAL,
60 SI_GETTING_FLAGS,
61 SI_GETTING_EVENTS,
62 SI_CLEARING_FLAGS,
63 SI_GETTING_MESSAGES,
64 SI_CHECKING_ENABLES,
65 SI_SETTING_ENABLES,
66 SI_HOSED
67 /* FIXME - add watchdog stuff. */
68 };
69
70 /* Some BT-specific defines we need here. */
71 #define IPMI_BT_INTMASK_REG 2
72 #define IPMI_BT_INTMASK_CLEAR_IRQ_BIT 2
73 #define IPMI_BT_INTMASK_ENABLE_IRQ_BIT 1
74
75 /* 'invalid' to allow a firmware-specified interface to be disabled */
76 const char *const si_to_str[] = { "invalid", "kcs", "smic", "bt", NULL };
77
78 const struct ipmi_match_info ipmi_kcs_si_info = { .type = SI_KCS };
79 const struct ipmi_match_info ipmi_smic_si_info = { .type = SI_SMIC };
80 const struct ipmi_match_info ipmi_bt_si_info = { .type = SI_BT };
81
82 static bool initialized;
83
84 /*
85 * Indexes into stats[] in smi_info below.
86 */
87 enum si_stat_indexes {
88 /*
89 * Number of times the driver requested a timer while an operation
90 * was in progress.
91 */
92 SI_STAT_short_timeouts = 0,
93
94 /*
95 * Number of times the driver requested a timer while nothing was in
96 * progress.
97 */
98 SI_STAT_long_timeouts,
99
100 /* Number of times the interface was idle while being polled. */
101 SI_STAT_idles,
102
103 /* Number of interrupts the driver handled. */
104 SI_STAT_interrupts,
105
106 /* Number of time the driver got an ATTN from the hardware. */
107 SI_STAT_attentions,
108
109 /* Number of times the driver requested flags from the hardware. */
110 SI_STAT_flag_fetches,
111
112 /* Number of times the hardware didn't follow the state machine. */
113 SI_STAT_hosed_count,
114
115 /* Number of completed messages. */
116 SI_STAT_complete_transactions,
117
118 /* Number of IPMI events received from the hardware. */
119 SI_STAT_events,
120
121 /* Number of watchdog pretimeouts. */
122 SI_STAT_watchdog_pretimeouts,
123
124 /* Number of asynchronous messages received. */
125 SI_STAT_incoming_messages,
126
127
128 /* This *must* remain last, add new values above this. */
129 SI_NUM_STATS
130 };
131
132 struct smi_info {
133 int si_num;
134 struct ipmi_smi *intf;
135 struct si_sm_data *si_sm;
136 const struct si_sm_handlers *handlers;
137 spinlock_t si_lock;
138 struct ipmi_smi_msg *waiting_msg;
139 struct ipmi_smi_msg *curr_msg;
140 enum si_intf_state si_state;
141
142 /*
143 * Used to handle the various types of I/O that can occur with
144 * IPMI
145 */
146 struct si_sm_io io;
147
148 /*
149 * Per-OEM handler, called from handle_flags(). Returns 1
150 * when handle_flags() needs to be re-run or 0 indicating it
151 * set si_state itself.
152 */
153 int (*oem_data_avail_handler)(struct smi_info *smi_info);
154
155 /*
156 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
157 * is set to hold the flags until we are done handling everything
158 * from the flags.
159 */
160 #define RECEIVE_MSG_AVAIL 0x01
161 #define EVENT_MSG_BUFFER_FULL 0x02
162 #define WDT_PRE_TIMEOUT_INT 0x08
163 #define OEM0_DATA_AVAIL 0x20
164 #define OEM1_DATA_AVAIL 0x40
165 #define OEM2_DATA_AVAIL 0x80
166 #define OEM_DATA_AVAIL (OEM0_DATA_AVAIL | \
167 OEM1_DATA_AVAIL | \
168 OEM2_DATA_AVAIL)
169 unsigned char msg_flags;
170
171 /* Does the BMC have an event buffer? */
172 bool has_event_buffer;
173
174 /*
175 * If set to true, this will request events the next time the
176 * state machine is idle.
177 */
178 atomic_t req_events;
179
180 /*
181 * If true, run the state machine to completion on every send
182 * call. Generally used after a panic to make sure stuff goes
183 * out.
184 */
185 bool run_to_completion;
186
187 /* The timer for this si. */
188 struct timer_list si_timer;
189
190 /* This flag is set, if the timer can be set */
191 bool timer_can_start;
192
193 /* This flag is set, if the timer is running (timer_pending() isn't enough) */
194 bool timer_running;
195
196 /* The time (in jiffies) the last timeout occurred at. */
197 unsigned long last_timeout_jiffies;
198
199 /* Are we waiting for the events, pretimeouts, received msgs? */
200 atomic_t need_watch;
201
202 /*
203 * The driver will disable interrupts when it gets into a
204 * situation where it cannot handle messages due to lack of
205 * memory. Once that situation clears up, it will re-enable
206 * interrupts.
207 */
208 bool interrupt_disabled;
209
210 /*
211 * Does the BMC support events?
212 */
213 bool supports_event_msg_buff;
214
215 /*
216 * Can we disable interrupts the global enables receive irq
217 * bit? There are currently two forms of brokenness, some
218 * systems cannot disable the bit (which is technically within
219 * the spec but a bad idea) and some systems have the bit
220 * forced to zero even though interrupts work (which is
221 * clearly outside the spec). The next bool tells which form
222 * of brokenness is present.
223 */
224 bool cannot_disable_irq;
225
226 /*
227 * Some systems are broken and cannot set the irq enable
228 * bit, even if they support interrupts.
229 */
230 bool irq_enable_broken;
231
232 /* Is the driver in maintenance mode? */
233 bool in_maintenance_mode;
234
235 /*
236 * Did we get an attention that we did not handle?
237 */
238 bool got_attn;
239
240 /* From the get device id response... */
241 struct ipmi_device_id device_id;
242
243 /* Have we added the device group to the device? */
244 bool dev_group_added;
245
246 /* Counters and things for the proc filesystem. */
247 atomic_t stats[SI_NUM_STATS];
248
249 struct task_struct *thread;
250
251 struct list_head link;
252 };
253
254 #define smi_inc_stat(smi, stat) \
255 atomic_inc(&(smi)->stats[SI_STAT_ ## stat])
256 #define smi_get_stat(smi, stat) \
257 ((unsigned int) atomic_read(&(smi)->stats[SI_STAT_ ## stat]))
258
259 #define IPMI_MAX_INTFS 4
260 static int force_kipmid[IPMI_MAX_INTFS];
261 static int num_force_kipmid;
262
263 static unsigned int kipmid_max_busy_us[IPMI_MAX_INTFS];
264 static int num_max_busy_us;
265
266 static bool unload_when_empty = true;
267
268 static int try_smi_init(struct smi_info *smi);
269 static void cleanup_one_si(struct smi_info *smi_info);
270 static void cleanup_ipmi_si(void);
271
272 #ifdef DEBUG_TIMING
debug_timestamp(struct smi_info * smi_info,char * msg)273 void debug_timestamp(struct smi_info *smi_info, char *msg)
274 {
275 struct timespec64 t;
276
277 ktime_get_ts64(&t);
278 dev_dbg(smi_info->io.dev, "**%s: %ptSp\n", msg, &t);
279 }
280 #else
281 #define debug_timestamp(smi_info, x)
282 #endif
283
284 static ATOMIC_NOTIFIER_HEAD(xaction_notifier_list);
register_xaction_notifier(struct notifier_block * nb)285 static int register_xaction_notifier(struct notifier_block *nb)
286 {
287 return atomic_notifier_chain_register(&xaction_notifier_list, nb);
288 }
289
deliver_recv_msg(struct smi_info * smi_info,struct ipmi_smi_msg * msg)290 static void deliver_recv_msg(struct smi_info *smi_info,
291 struct ipmi_smi_msg *msg)
292 {
293 /* Deliver the message to the upper layer. */
294 ipmi_smi_msg_received(smi_info->intf, msg);
295 }
296
return_hosed_msg(struct smi_info * smi_info,int cCode)297 static void return_hosed_msg(struct smi_info *smi_info, int cCode)
298 {
299 struct ipmi_smi_msg *msg = smi_info->curr_msg;
300
301 if (cCode < 0 || cCode > IPMI_ERR_UNSPECIFIED)
302 cCode = IPMI_ERR_UNSPECIFIED;
303 /* else use it as is */
304
305 /* Make it a response */
306 msg->rsp[0] = msg->data[0] | 4;
307 msg->rsp[1] = msg->data[1];
308 msg->rsp[2] = cCode;
309 msg->rsp_size = 3;
310
311 smi_info->curr_msg = NULL;
312 deliver_recv_msg(smi_info, msg);
313 }
314
start_next_msg(struct smi_info * smi_info)315 static enum si_sm_result start_next_msg(struct smi_info *smi_info)
316 {
317 int rv;
318
319 if (!smi_info->waiting_msg) {
320 smi_info->curr_msg = NULL;
321 rv = SI_SM_IDLE;
322 } else {
323 int err;
324
325 smi_info->curr_msg = smi_info->waiting_msg;
326 smi_info->waiting_msg = NULL;
327 debug_timestamp(smi_info, "Start2");
328 err = atomic_notifier_call_chain(&xaction_notifier_list,
329 0, smi_info);
330 if (err & NOTIFY_STOP_MASK) {
331 rv = SI_SM_CALL_WITHOUT_DELAY;
332 goto out;
333 }
334 err = smi_info->handlers->start_transaction(
335 smi_info->si_sm,
336 smi_info->curr_msg->data,
337 smi_info->curr_msg->data_size);
338 if (err)
339 return_hosed_msg(smi_info, err);
340
341 rv = SI_SM_CALL_WITHOUT_DELAY;
342 }
343 out:
344 return rv;
345 }
346
smi_mod_timer(struct smi_info * smi_info,unsigned long new_val)347 static void smi_mod_timer(struct smi_info *smi_info, unsigned long new_val)
348 {
349 if (!smi_info->timer_can_start)
350 return;
351 smi_info->last_timeout_jiffies = jiffies;
352 mod_timer(&smi_info->si_timer, new_val);
353 smi_info->timer_running = true;
354 }
355
356 /*
357 * Start a new message and (re)start the timer and thread.
358 */
start_new_msg(struct smi_info * smi_info,unsigned char * msg,unsigned int size)359 static void start_new_msg(struct smi_info *smi_info, unsigned char *msg,
360 unsigned int size)
361 {
362 smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
363
364 if (smi_info->thread)
365 wake_up_process(smi_info->thread);
366
367 smi_info->handlers->start_transaction(smi_info->si_sm, msg, size);
368 }
369
start_check_enables(struct smi_info * smi_info)370 static void start_check_enables(struct smi_info *smi_info)
371 {
372 unsigned char msg[2];
373
374 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
375 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
376
377 start_new_msg(smi_info, msg, 2);
378 smi_info->si_state = SI_CHECKING_ENABLES;
379 }
380
start_clear_flags(struct smi_info * smi_info)381 static void start_clear_flags(struct smi_info *smi_info)
382 {
383 unsigned char msg[3];
384
385 /* Make sure the watchdog pre-timeout flag is not set at startup. */
386 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
387 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
388 msg[2] = WDT_PRE_TIMEOUT_INT;
389
390 start_new_msg(smi_info, msg, 3);
391 smi_info->si_state = SI_CLEARING_FLAGS;
392 }
393
start_get_flags(struct smi_info * smi_info)394 static void start_get_flags(struct smi_info *smi_info)
395 {
396 unsigned char msg[2];
397
398 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
399 msg[1] = IPMI_GET_MSG_FLAGS_CMD;
400
401 start_new_msg(smi_info, msg, 2);
402 smi_info->si_state = SI_GETTING_FLAGS;
403 }
404
start_getting_msg_queue(struct smi_info * smi_info)405 static void start_getting_msg_queue(struct smi_info *smi_info)
406 {
407 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
408 smi_info->curr_msg->data[1] = IPMI_GET_MSG_CMD;
409 smi_info->curr_msg->data_size = 2;
410
411 start_new_msg(smi_info, smi_info->curr_msg->data,
412 smi_info->curr_msg->data_size);
413 smi_info->si_state = SI_GETTING_MESSAGES;
414 }
415
start_getting_events(struct smi_info * smi_info)416 static void start_getting_events(struct smi_info *smi_info)
417 {
418 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
419 smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
420 smi_info->curr_msg->data_size = 2;
421
422 start_new_msg(smi_info, smi_info->curr_msg->data,
423 smi_info->curr_msg->data_size);
424 smi_info->si_state = SI_GETTING_EVENTS;
425 }
426
427 /*
428 * When we have a situtaion where we run out of memory and cannot
429 * allocate messages, we just leave them in the BMC and run the system
430 * polled until we can allocate some memory. Once we have some
431 * memory, we will re-enable the interrupt.
432 *
433 * Note that we cannot just use disable_irq(), since the interrupt may
434 * be shared.
435 */
disable_si_irq(struct smi_info * smi_info)436 static inline bool disable_si_irq(struct smi_info *smi_info)
437 {
438 if ((smi_info->io.irq) && (!smi_info->interrupt_disabled)) {
439 smi_info->interrupt_disabled = true;
440 start_check_enables(smi_info);
441 return true;
442 }
443 return false;
444 }
445
enable_si_irq(struct smi_info * smi_info)446 static inline bool enable_si_irq(struct smi_info *smi_info)
447 {
448 if ((smi_info->io.irq) && (smi_info->interrupt_disabled)) {
449 smi_info->interrupt_disabled = false;
450 start_check_enables(smi_info);
451 return true;
452 }
453 return false;
454 }
455
456 /*
457 * Allocate a message. If unable to allocate, start the interrupt
458 * disable process and return NULL. If able to allocate but
459 * interrupts are disabled, free the message and return NULL after
460 * starting the interrupt enable process.
461 */
alloc_msg_handle_irq(struct smi_info * smi_info)462 static struct ipmi_smi_msg *alloc_msg_handle_irq(struct smi_info *smi_info)
463 {
464 struct ipmi_smi_msg *msg;
465
466 msg = ipmi_alloc_smi_msg();
467 if (!msg) {
468 if (!disable_si_irq(smi_info))
469 smi_info->si_state = SI_NORMAL;
470 } else if (enable_si_irq(smi_info)) {
471 ipmi_free_smi_msg(msg);
472 msg = NULL;
473 }
474 return msg;
475 }
476
handle_flags(struct smi_info * smi_info)477 static void handle_flags(struct smi_info *smi_info)
478 {
479 retry:
480 if (smi_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
481 /* Watchdog pre-timeout */
482 smi_inc_stat(smi_info, watchdog_pretimeouts);
483
484 start_clear_flags(smi_info);
485 smi_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
486 ipmi_smi_watchdog_pretimeout(smi_info->intf);
487 } else if (smi_info->msg_flags & RECEIVE_MSG_AVAIL) {
488 /* Messages available. */
489 smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
490 if (!smi_info->curr_msg)
491 return;
492
493 start_getting_msg_queue(smi_info);
494 } else if (smi_info->msg_flags & EVENT_MSG_BUFFER_FULL) {
495 /* Events available. */
496 smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
497 if (!smi_info->curr_msg)
498 return;
499
500 start_getting_events(smi_info);
501 } else if (smi_info->msg_flags & OEM_DATA_AVAIL &&
502 smi_info->oem_data_avail_handler) {
503 if (smi_info->oem_data_avail_handler(smi_info))
504 goto retry;
505 } else
506 smi_info->si_state = SI_NORMAL;
507 }
508
509 /*
510 * Global enables we care about.
511 */
512 #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
513 IPMI_BMC_EVT_MSG_INTR)
514
current_global_enables(struct smi_info * smi_info,u8 base,bool * irq_on)515 static u8 current_global_enables(struct smi_info *smi_info, u8 base,
516 bool *irq_on)
517 {
518 u8 enables = 0;
519
520 if (smi_info->supports_event_msg_buff)
521 enables |= IPMI_BMC_EVT_MSG_BUFF;
522
523 if (((smi_info->io.irq && !smi_info->interrupt_disabled) ||
524 smi_info->cannot_disable_irq) &&
525 !smi_info->irq_enable_broken)
526 enables |= IPMI_BMC_RCV_MSG_INTR;
527
528 if (smi_info->supports_event_msg_buff &&
529 smi_info->io.irq && !smi_info->interrupt_disabled &&
530 !smi_info->irq_enable_broken)
531 enables |= IPMI_BMC_EVT_MSG_INTR;
532
533 *irq_on = enables & (IPMI_BMC_EVT_MSG_INTR | IPMI_BMC_RCV_MSG_INTR);
534
535 return enables;
536 }
537
check_bt_irq(struct smi_info * smi_info,bool irq_on)538 static void check_bt_irq(struct smi_info *smi_info, bool irq_on)
539 {
540 u8 irqstate = smi_info->io.inputb(&smi_info->io, IPMI_BT_INTMASK_REG);
541
542 irqstate &= IPMI_BT_INTMASK_ENABLE_IRQ_BIT;
543
544 if ((bool)irqstate == irq_on)
545 return;
546
547 if (irq_on)
548 smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
549 IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
550 else
551 smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG, 0);
552 }
553
handle_transaction_done(struct smi_info * smi_info)554 static void handle_transaction_done(struct smi_info *smi_info)
555 {
556 struct ipmi_smi_msg *msg;
557
558 debug_timestamp(smi_info, "Done");
559 switch (smi_info->si_state) {
560 case SI_NORMAL:
561 if (!smi_info->curr_msg)
562 break;
563
564 smi_info->curr_msg->rsp_size
565 = smi_info->handlers->get_result(
566 smi_info->si_sm,
567 smi_info->curr_msg->rsp,
568 IPMI_MAX_MSG_LENGTH);
569
570 /*
571 * Do this here becase deliver_recv_msg() releases the
572 * lock, and a new message can be put in during the
573 * time the lock is released.
574 */
575 msg = smi_info->curr_msg;
576 smi_info->curr_msg = NULL;
577 deliver_recv_msg(smi_info, msg);
578 break;
579
580 case SI_GETTING_FLAGS:
581 {
582 unsigned char msg[4];
583 unsigned int len;
584
585 /* We got the flags from the SMI, now handle them. */
586 len = smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
587 if (msg[2] != 0) {
588 /* Error fetching flags, just give up for now. */
589 smi_info->si_state = SI_NORMAL;
590 } else if (len < 4) {
591 /*
592 * Hmm, no flags. That's technically illegal, but
593 * don't use uninitialized data.
594 */
595 smi_info->si_state = SI_NORMAL;
596 } else {
597 smi_info->msg_flags = msg[3];
598 handle_flags(smi_info);
599 }
600 break;
601 }
602
603 case SI_CLEARING_FLAGS:
604 {
605 unsigned char msg[3];
606
607 /* We cleared the flags. */
608 smi_info->handlers->get_result(smi_info->si_sm, msg, 3);
609 if (msg[2] != 0) {
610 /* Error clearing flags */
611 dev_warn_ratelimited(smi_info->io.dev,
612 "Error clearing flags: %2.2x\n", msg[2]);
613 }
614 smi_info->si_state = SI_NORMAL;
615 break;
616 }
617
618 case SI_GETTING_EVENTS:
619 {
620 smi_info->curr_msg->rsp_size
621 = smi_info->handlers->get_result(
622 smi_info->si_sm,
623 smi_info->curr_msg->rsp,
624 IPMI_MAX_MSG_LENGTH);
625
626 /*
627 * Do this here becase deliver_recv_msg() releases the
628 * lock, and a new message can be put in during the
629 * time the lock is released.
630 */
631 msg = smi_info->curr_msg;
632 smi_info->curr_msg = NULL;
633 if (msg->rsp[2] != 0) {
634 /* Error getting event, probably done. */
635 msg->done(msg);
636
637 /* Take off the event flag. */
638 smi_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
639 handle_flags(smi_info);
640 } else {
641 smi_inc_stat(smi_info, events);
642
643 /*
644 * Do this before we deliver the message
645 * because delivering the message releases the
646 * lock and something else can mess with the
647 * state.
648 */
649 handle_flags(smi_info);
650
651 deliver_recv_msg(smi_info, msg);
652 }
653 break;
654 }
655
656 case SI_GETTING_MESSAGES:
657 {
658 smi_info->curr_msg->rsp_size
659 = smi_info->handlers->get_result(
660 smi_info->si_sm,
661 smi_info->curr_msg->rsp,
662 IPMI_MAX_MSG_LENGTH);
663
664 /*
665 * Do this here becase deliver_recv_msg() releases the
666 * lock, and a new message can be put in during the
667 * time the lock is released.
668 */
669 msg = smi_info->curr_msg;
670 smi_info->curr_msg = NULL;
671 if (msg->rsp[2] != 0) {
672 /* Error getting event, probably done. */
673 msg->done(msg);
674
675 /* Take off the msg flag. */
676 smi_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
677 handle_flags(smi_info);
678 } else {
679 smi_inc_stat(smi_info, incoming_messages);
680
681 /*
682 * Do this before we deliver the message
683 * because delivering the message releases the
684 * lock and something else can mess with the
685 * state.
686 */
687 handle_flags(smi_info);
688
689 deliver_recv_msg(smi_info, msg);
690 }
691 break;
692 }
693
694 case SI_CHECKING_ENABLES:
695 {
696 unsigned char msg[4];
697 u8 enables;
698 bool irq_on;
699
700 /* We got the flags from the SMI, now handle them. */
701 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
702 if (msg[2] != 0) {
703 dev_warn_ratelimited(smi_info->io.dev,
704 "Couldn't get irq info: %x,\n"
705 "Maybe ok, but ipmi might run very slowly.\n",
706 msg[2]);
707 smi_info->si_state = SI_NORMAL;
708 break;
709 }
710 enables = current_global_enables(smi_info, 0, &irq_on);
711 if (smi_info->io.si_info->type == SI_BT)
712 /* BT has its own interrupt enable bit. */
713 check_bt_irq(smi_info, irq_on);
714 if (enables != (msg[3] & GLOBAL_ENABLES_MASK)) {
715 /* Enables are not correct, fix them. */
716 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
717 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
718 msg[2] = enables | (msg[3] & ~GLOBAL_ENABLES_MASK);
719 smi_info->handlers->start_transaction(
720 smi_info->si_sm, msg, 3);
721 smi_info->si_state = SI_SETTING_ENABLES;
722 } else if (smi_info->supports_event_msg_buff) {
723 smi_info->curr_msg = ipmi_alloc_smi_msg();
724 if (!smi_info->curr_msg) {
725 smi_info->si_state = SI_NORMAL;
726 break;
727 }
728 start_getting_events(smi_info);
729 } else {
730 smi_info->si_state = SI_NORMAL;
731 }
732 break;
733 }
734
735 case SI_SETTING_ENABLES:
736 {
737 unsigned char msg[4];
738
739 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
740 if (msg[2] != 0)
741 dev_warn_ratelimited(smi_info->io.dev,
742 "Could not set the global enables: 0x%x.\n",
743 msg[2]);
744
745 if (smi_info->supports_event_msg_buff) {
746 smi_info->curr_msg = ipmi_alloc_smi_msg();
747 if (!smi_info->curr_msg) {
748 smi_info->si_state = SI_NORMAL;
749 break;
750 }
751 start_getting_events(smi_info);
752 } else {
753 smi_info->si_state = SI_NORMAL;
754 }
755 break;
756 }
757 case SI_HOSED: /* Shouldn't happen. */
758 break;
759 }
760 }
761
762 /*
763 * Called on timeouts and events. Timeouts should pass the elapsed
764 * time, interrupts should pass in zero. Must be called with
765 * si_lock held and interrupts disabled.
766 */
smi_event_handler(struct smi_info * smi_info,int time)767 static enum si_sm_result smi_event_handler(struct smi_info *smi_info,
768 int time)
769 {
770 enum si_sm_result si_sm_result;
771
772 restart:
773 if (smi_info->si_state == SI_HOSED)
774 /* Just in case, hosed state is only left from the timeout. */
775 return SI_SM_HOSED;
776
777 /*
778 * There used to be a loop here that waited a little while
779 * (around 25us) before giving up. That turned out to be
780 * pointless, the minimum delays I was seeing were in the 300us
781 * range, which is far too long to wait in an interrupt. So
782 * we just run until the state machine tells us something
783 * happened or it needs a delay.
784 */
785 si_sm_result = smi_info->handlers->event(smi_info->si_sm, time);
786 time = 0;
787 while (si_sm_result == SI_SM_CALL_WITHOUT_DELAY)
788 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
789
790 if (si_sm_result == SI_SM_TRANSACTION_COMPLETE) {
791 smi_inc_stat(smi_info, complete_transactions);
792
793 handle_transaction_done(smi_info);
794 goto restart;
795 } else if (si_sm_result == SI_SM_HOSED) {
796 smi_inc_stat(smi_info, hosed_count);
797
798 /*
799 * Do the before return_hosed_msg, because that
800 * releases the lock. We just disable operations for
801 * a while and retry in hosed state.
802 */
803 smi_info->si_state = SI_HOSED;
804 if (smi_info->curr_msg != NULL) {
805 /*
806 * If we were handling a user message, format
807 * a response to send to the upper layer to
808 * tell it about the error.
809 */
810 return_hosed_msg(smi_info, IPMI_BUS_ERR);
811 }
812 if (smi_info->waiting_msg != NULL) {
813 /* Also handle if there was a message waiting. */
814 smi_info->curr_msg = smi_info->waiting_msg;
815 smi_info->waiting_msg = NULL;
816 return_hosed_msg(smi_info, IPMI_BUS_ERR);
817 }
818 smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_HOSED);
819 goto out;
820 }
821
822 /*
823 * We prefer handling attn over new messages. But don't do
824 * this if there is not yet an upper layer to handle anything.
825 */
826 if (si_sm_result == SI_SM_ATTN || smi_info->got_attn) {
827 if (smi_info->si_state != SI_NORMAL) {
828 /*
829 * We got an ATTN, but we are doing something else.
830 * Handle the ATTN later.
831 */
832 smi_info->got_attn = true;
833 } else {
834 smi_info->got_attn = false;
835 smi_inc_stat(smi_info, attentions);
836
837 /*
838 * Got a attn, send down a get message flags to see
839 * what's causing it. It would be better to handle
840 * this in the upper layer, but due to the way
841 * interrupts work with the SMI, that's not really
842 * possible.
843 */
844 start_get_flags(smi_info);
845 goto restart;
846 }
847 }
848
849 /* If we are currently idle, try to start the next message. */
850 if (si_sm_result == SI_SM_IDLE) {
851 smi_inc_stat(smi_info, idles);
852
853 si_sm_result = start_next_msg(smi_info);
854 if (si_sm_result != SI_SM_IDLE)
855 goto restart;
856 }
857
858 if ((si_sm_result == SI_SM_IDLE)
859 && (atomic_read(&smi_info->req_events))) {
860 /*
861 * We are idle and the upper layer requested that I fetch
862 * events, so do so.
863 */
864 atomic_set(&smi_info->req_events, 0);
865
866 /*
867 * Take this opportunity to check the interrupt and
868 * message enable state for the BMC. The BMC can be
869 * asynchronously reset, and may thus get interrupts
870 * disable and messages disabled.
871 */
872 if (smi_info->supports_event_msg_buff || smi_info->io.irq) {
873 start_check_enables(smi_info);
874 } else {
875 smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
876 if (!smi_info->curr_msg)
877 goto out;
878
879 start_getting_events(smi_info);
880 }
881 goto restart;
882 }
883
884 if (si_sm_result == SI_SM_IDLE && smi_info->timer_running) {
885 /* Ok it if fails, the timer will just go off. */
886 if (timer_delete(&smi_info->si_timer))
887 smi_info->timer_running = false;
888 }
889
890 out:
891 return si_sm_result;
892 }
893
check_start_timer_thread(struct smi_info * smi_info)894 static void check_start_timer_thread(struct smi_info *smi_info)
895 {
896 if (smi_info->si_state == SI_NORMAL && smi_info->curr_msg == NULL) {
897 smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
898
899 if (smi_info->thread)
900 wake_up_process(smi_info->thread);
901
902 start_next_msg(smi_info);
903 smi_event_handler(smi_info, 0);
904 }
905 }
906
flush_messages(void * send_info)907 static void flush_messages(void *send_info)
908 {
909 struct smi_info *smi_info = send_info;
910 enum si_sm_result result;
911
912 /*
913 * Currently, this function is called only in run-to-completion
914 * mode. This means we are single-threaded, no need for locks.
915 */
916 result = smi_event_handler(smi_info, 0);
917 while (result != SI_SM_IDLE && result != SI_SM_HOSED) {
918 udelay(SI_SHORT_TIMEOUT_USEC);
919 result = smi_event_handler(smi_info, SI_SHORT_TIMEOUT_USEC);
920 }
921 }
922
sender(void * send_info,struct ipmi_smi_msg * msg)923 static int sender(void *send_info, struct ipmi_smi_msg *msg)
924 {
925 struct smi_info *smi_info = send_info;
926 unsigned long flags;
927 int rv = IPMI_CC_NO_ERROR;
928
929 debug_timestamp(smi_info, "Enqueue");
930
931 /*
932 * Check here for run to completion mode. A check under lock is
933 * later.
934 */
935 if (smi_info->si_state == SI_HOSED)
936 return IPMI_BUS_ERR;
937
938 if (smi_info->run_to_completion) {
939 /*
940 * If we are running to completion, start it. Upper
941 * layer will call flush_messages to clear it out.
942 */
943 smi_info->waiting_msg = msg;
944 return IPMI_CC_NO_ERROR;
945 }
946
947 spin_lock_irqsave(&smi_info->si_lock, flags);
948 if (smi_info->si_state == SI_HOSED) {
949 rv = IPMI_BUS_ERR;
950 } else {
951 BUG_ON(smi_info->waiting_msg);
952 smi_info->waiting_msg = msg;
953 check_start_timer_thread(smi_info);
954 }
955 spin_unlock_irqrestore(&smi_info->si_lock, flags);
956 return rv;
957 }
958
set_run_to_completion(void * send_info,bool i_run_to_completion)959 static void set_run_to_completion(void *send_info, bool i_run_to_completion)
960 {
961 struct smi_info *smi_info = send_info;
962
963 smi_info->run_to_completion = i_run_to_completion;
964 if (i_run_to_completion)
965 flush_messages(smi_info);
966 }
967
968 /*
969 * Use -1 as a special constant to tell that we are spinning in kipmid
970 * looking for something and not delaying between checks
971 */
972 #define IPMI_TIME_NOT_BUSY ns_to_ktime(-1ull)
ipmi_thread_busy_wait(enum si_sm_result smi_result,const struct smi_info * smi_info,ktime_t * busy_until)973 static inline bool ipmi_thread_busy_wait(enum si_sm_result smi_result,
974 const struct smi_info *smi_info,
975 ktime_t *busy_until)
976 {
977 unsigned int max_busy_us = 0;
978
979 if (smi_info->si_num < num_max_busy_us)
980 max_busy_us = kipmid_max_busy_us[smi_info->si_num];
981 if (max_busy_us == 0 || smi_result != SI_SM_CALL_WITH_DELAY)
982 *busy_until = IPMI_TIME_NOT_BUSY;
983 else if (*busy_until == IPMI_TIME_NOT_BUSY) {
984 *busy_until = ktime_get() + max_busy_us * NSEC_PER_USEC;
985 } else {
986 if (unlikely(ktime_get() > *busy_until)) {
987 *busy_until = IPMI_TIME_NOT_BUSY;
988 return false;
989 }
990 }
991 return true;
992 }
993
994
995 /*
996 * A busy-waiting loop for speeding up IPMI operation.
997 *
998 * Lousy hardware makes this hard. This is only enabled for systems
999 * that are not BT and do not have interrupts. It starts spinning
1000 * when an operation is complete or until max_busy tells it to stop
1001 * (if that is enabled). See the paragraph on kimid_max_busy_us in
1002 * Documentation/driver-api/ipmi.rst for details.
1003 */
ipmi_thread(void * data)1004 static int ipmi_thread(void *data)
1005 {
1006 struct smi_info *smi_info = data;
1007 unsigned long flags;
1008 enum si_sm_result smi_result;
1009 ktime_t busy_until = IPMI_TIME_NOT_BUSY;
1010
1011 set_user_nice(current, MAX_NICE);
1012 while (!kthread_should_stop()) {
1013 int busy_wait;
1014
1015 spin_lock_irqsave(&(smi_info->si_lock), flags);
1016 smi_result = smi_event_handler(smi_info, 0);
1017
1018 /*
1019 * If the driver is doing something, there is a possible
1020 * race with the timer. If the timer handler see idle,
1021 * and the thread here sees something else, the timer
1022 * handler won't restart the timer even though it is
1023 * required. So start it here if necessary.
1024 */
1025 if (smi_result != SI_SM_IDLE && !smi_info->timer_running)
1026 smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
1027
1028 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1029 busy_wait = ipmi_thread_busy_wait(smi_result, smi_info,
1030 &busy_until);
1031 if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
1032 ; /* do nothing */
1033 } else if (smi_result == SI_SM_CALL_WITH_DELAY && busy_wait) {
1034 /*
1035 * In maintenance mode we run as fast as
1036 * possible to allow firmware updates to
1037 * complete as fast as possible, but normally
1038 * don't bang on the scheduler.
1039 */
1040 if (smi_info->in_maintenance_mode)
1041 schedule();
1042 else
1043 usleep_range(100, 200);
1044 } else if (smi_result == SI_SM_IDLE) {
1045 if (atomic_read(&smi_info->need_watch)) {
1046 schedule_timeout_interruptible(100);
1047 } else {
1048 /* Wait to be woken up when we are needed. */
1049 __set_current_state(TASK_INTERRUPTIBLE);
1050 schedule();
1051 }
1052 } else {
1053 schedule_timeout_interruptible(1);
1054 }
1055 }
1056 return 0;
1057 }
1058
1059
poll(void * send_info)1060 static void poll(void *send_info)
1061 {
1062 struct smi_info *smi_info = send_info;
1063 unsigned long flags = 0;
1064 bool run_to_completion = smi_info->run_to_completion;
1065
1066 /*
1067 * Make sure there is some delay in the poll loop so we can
1068 * drive time forward and timeout things.
1069 */
1070 udelay(10);
1071 if (!run_to_completion)
1072 spin_lock_irqsave(&smi_info->si_lock, flags);
1073 smi_event_handler(smi_info, 10);
1074 if (!run_to_completion)
1075 spin_unlock_irqrestore(&smi_info->si_lock, flags);
1076 }
1077
request_events(void * send_info)1078 static void request_events(void *send_info)
1079 {
1080 struct smi_info *smi_info = send_info;
1081
1082 if (!smi_info->has_event_buffer)
1083 return;
1084
1085 atomic_set(&smi_info->req_events, 1);
1086 }
1087
set_need_watch(void * send_info,unsigned int watch_mask)1088 static void set_need_watch(void *send_info, unsigned int watch_mask)
1089 {
1090 struct smi_info *smi_info = send_info;
1091 unsigned long flags;
1092 int enable;
1093
1094 enable = !!watch_mask;
1095
1096 atomic_set(&smi_info->need_watch, enable);
1097 spin_lock_irqsave(&smi_info->si_lock, flags);
1098 check_start_timer_thread(smi_info);
1099 spin_unlock_irqrestore(&smi_info->si_lock, flags);
1100 }
1101
smi_timeout(struct timer_list * t)1102 static void smi_timeout(struct timer_list *t)
1103 {
1104 struct smi_info *smi_info = timer_container_of(smi_info, t,
1105 si_timer);
1106 enum si_sm_result smi_result;
1107 unsigned long flags;
1108 unsigned long jiffies_now;
1109 long time_diff;
1110 long timeout;
1111
1112 spin_lock_irqsave(&(smi_info->si_lock), flags);
1113 debug_timestamp(smi_info, "Timer");
1114
1115 if (smi_info->si_state == SI_HOSED)
1116 /* Try something to see if the BMC is now operational. */
1117 start_get_flags(smi_info);
1118
1119 jiffies_now = jiffies;
1120 time_diff = (((long)jiffies_now - (long)smi_info->last_timeout_jiffies)
1121 * SI_USEC_PER_JIFFY);
1122 smi_result = smi_event_handler(smi_info, time_diff);
1123
1124 if (smi_info->si_state == SI_HOSED) {
1125 timeout = jiffies + SI_TIMEOUT_HOSED;
1126 } else if ((smi_info->io.irq) && (!smi_info->interrupt_disabled)) {
1127 /* Running with interrupts, only do long timeouts. */
1128 timeout = jiffies + SI_TIMEOUT_JIFFIES;
1129 smi_inc_stat(smi_info, long_timeouts);
1130 } else if (smi_result == SI_SM_CALL_WITH_DELAY) {
1131 /*
1132 * If the state machine asks for a short delay, then shorten
1133 * the timer timeout.
1134 */
1135 smi_inc_stat(smi_info, short_timeouts);
1136 timeout = jiffies + 1;
1137 } else {
1138 smi_inc_stat(smi_info, long_timeouts);
1139 timeout = jiffies + SI_TIMEOUT_JIFFIES;
1140 }
1141
1142 if (smi_result != SI_SM_IDLE)
1143 smi_mod_timer(smi_info, timeout);
1144 else
1145 smi_info->timer_running = false;
1146 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1147 }
1148
ipmi_si_irq_handler(int irq,void * data)1149 irqreturn_t ipmi_si_irq_handler(int irq, void *data)
1150 {
1151 struct smi_info *smi_info = data;
1152 unsigned long flags;
1153
1154 if (smi_info->io.si_info->type == SI_BT)
1155 /* We need to clear the IRQ flag for the BT interface. */
1156 smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
1157 IPMI_BT_INTMASK_CLEAR_IRQ_BIT
1158 | IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
1159
1160 spin_lock_irqsave(&(smi_info->si_lock), flags);
1161
1162 smi_inc_stat(smi_info, interrupts);
1163
1164 debug_timestamp(smi_info, "Interrupt");
1165
1166 smi_event_handler(smi_info, 0);
1167 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1168 return IRQ_HANDLED;
1169 }
1170
smi_start_processing(void * send_info,struct ipmi_smi * intf)1171 static int smi_start_processing(void *send_info,
1172 struct ipmi_smi *intf)
1173 {
1174 struct smi_info *new_smi = send_info;
1175 int enable = 0;
1176
1177 new_smi->intf = intf;
1178
1179 /* Set up the timer that drives the interface. */
1180 timer_setup(&new_smi->si_timer, smi_timeout, 0);
1181 new_smi->timer_can_start = true;
1182 smi_mod_timer(new_smi, jiffies + SI_TIMEOUT_JIFFIES);
1183
1184 /* Try to claim any interrupts. */
1185 if (new_smi->io.irq_setup) {
1186 new_smi->io.irq_handler_data = new_smi;
1187 new_smi->io.irq_setup(&new_smi->io);
1188 }
1189
1190 /*
1191 * Check if the user forcefully enabled the daemon.
1192 */
1193 if (new_smi->si_num < num_force_kipmid)
1194 enable = force_kipmid[new_smi->si_num];
1195 /*
1196 * The BT interface is efficient enough to not need a thread,
1197 * and there is no need for a thread if we have interrupts.
1198 */
1199 else if (new_smi->io.si_info->type != SI_BT && !new_smi->io.irq)
1200 enable = 1;
1201
1202 if (enable) {
1203 new_smi->thread = kthread_run(ipmi_thread, new_smi,
1204 "kipmi%d", new_smi->si_num);
1205 if (IS_ERR(new_smi->thread)) {
1206 dev_notice(new_smi->io.dev,
1207 "Could not start kernel thread due to error %ld, only using timers to drive the interface\n",
1208 PTR_ERR(new_smi->thread));
1209 new_smi->thread = NULL;
1210 }
1211 }
1212
1213 return 0;
1214 }
1215
get_smi_info(void * send_info,struct ipmi_smi_info * data)1216 static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1217 {
1218 struct smi_info *smi = send_info;
1219
1220 data->addr_src = smi->io.addr_source;
1221 data->dev = smi->io.dev;
1222 data->addr_info = smi->io.addr_info;
1223 get_device(smi->io.dev);
1224
1225 return 0;
1226 }
1227
set_maintenance_mode(void * send_info,bool enable)1228 static void set_maintenance_mode(void *send_info, bool enable)
1229 {
1230 struct smi_info *smi_info = send_info;
1231
1232 if (!enable)
1233 atomic_set(&smi_info->req_events, 0);
1234 smi_info->in_maintenance_mode = enable;
1235 }
1236
1237 static void shutdown_smi(void *send_info);
1238 static const struct ipmi_smi_handlers handlers = {
1239 .owner = THIS_MODULE,
1240 .start_processing = smi_start_processing,
1241 .shutdown = shutdown_smi,
1242 .get_smi_info = get_smi_info,
1243 .sender = sender,
1244 .request_events = request_events,
1245 .set_need_watch = set_need_watch,
1246 .set_maintenance_mode = set_maintenance_mode,
1247 .set_run_to_completion = set_run_to_completion,
1248 .flush_messages = flush_messages,
1249 .poll = poll,
1250 };
1251
1252 static LIST_HEAD(smi_infos);
1253 static DEFINE_MUTEX(smi_infos_lock);
1254 static int smi_num; /* Used to sequence the SMIs */
1255
1256 static const char * const addr_space_to_str[] = { "i/o", "mem" };
1257
1258 module_param_array(force_kipmid, int, &num_force_kipmid, 0);
1259 MODULE_PARM_DESC(force_kipmid,
1260 "Force the kipmi daemon to be enabled (1) or disabled(0). Normally the IPMI driver auto-detects this, but the value may be overridden by this parm.");
1261 module_param(unload_when_empty, bool, 0);
1262 MODULE_PARM_DESC(unload_when_empty,
1263 "Unload the module if no interfaces are specified or found, default is 1. Setting to 0 is useful for hot add of devices using hotmod.");
1264 module_param_array(kipmid_max_busy_us, uint, &num_max_busy_us, 0644);
1265 MODULE_PARM_DESC(kipmid_max_busy_us,
1266 "Max time (in microseconds) to busy-wait for IPMI data before sleeping. 0 (default) means to wait forever. Set to 100-500 if kipmid is using up a lot of CPU time.");
1267
ipmi_irq_finish_setup(struct si_sm_io * io)1268 void ipmi_irq_finish_setup(struct si_sm_io *io)
1269 {
1270 if (io->si_info->type == SI_BT)
1271 /* Enable the interrupt in the BT interface. */
1272 io->outputb(io, IPMI_BT_INTMASK_REG,
1273 IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
1274 }
1275
ipmi_irq_start_cleanup(struct si_sm_io * io)1276 void ipmi_irq_start_cleanup(struct si_sm_io *io)
1277 {
1278 if (io->si_info->type == SI_BT)
1279 /* Disable the interrupt in the BT interface. */
1280 io->outputb(io, IPMI_BT_INTMASK_REG, 0);
1281 }
1282
std_irq_cleanup(struct si_sm_io * io)1283 static void std_irq_cleanup(struct si_sm_io *io)
1284 {
1285 ipmi_irq_start_cleanup(io);
1286 free_irq(io->irq, io->irq_handler_data);
1287 }
1288
ipmi_std_irq_setup(struct si_sm_io * io)1289 int ipmi_std_irq_setup(struct si_sm_io *io)
1290 {
1291 int rv;
1292
1293 if (!io->irq)
1294 return 0;
1295
1296 rv = request_irq(io->irq,
1297 ipmi_si_irq_handler,
1298 IRQF_SHARED,
1299 SI_DEVICE_NAME,
1300 io->irq_handler_data);
1301 if (rv) {
1302 dev_warn(io->dev, "%s unable to claim interrupt %d, running polled\n",
1303 SI_DEVICE_NAME, io->irq);
1304 io->irq = 0;
1305 } else {
1306 io->irq_cleanup = std_irq_cleanup;
1307 ipmi_irq_finish_setup(io);
1308 dev_info(io->dev, "Using irq %d\n", io->irq);
1309 }
1310
1311 return rv;
1312 }
1313
wait_for_msg_done(struct smi_info * smi_info)1314 static int wait_for_msg_done(struct smi_info *smi_info)
1315 {
1316 enum si_sm_result smi_result;
1317
1318 smi_result = smi_info->handlers->event(smi_info->si_sm, 0);
1319 for (;;) {
1320 if (smi_result == SI_SM_CALL_WITH_DELAY ||
1321 smi_result == SI_SM_CALL_WITH_TICK_DELAY) {
1322 schedule_timeout_uninterruptible(1);
1323 smi_result = smi_info->handlers->event(
1324 smi_info->si_sm, jiffies_to_usecs(1));
1325 } else if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
1326 smi_result = smi_info->handlers->event(
1327 smi_info->si_sm, 0);
1328 } else
1329 break;
1330 }
1331 if (smi_result == SI_SM_HOSED)
1332 /*
1333 * We couldn't get the state machine to run, so whatever's at
1334 * the port is probably not an IPMI SMI interface.
1335 */
1336 return -ENODEV;
1337
1338 return 0;
1339 }
1340
try_get_dev_id(struct smi_info * smi_info)1341 static int try_get_dev_id(struct smi_info *smi_info)
1342 {
1343 unsigned char msg[2];
1344 unsigned char *resp;
1345 unsigned long resp_len;
1346 int rv = 0;
1347 unsigned int retry_count = 0;
1348
1349 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1350 if (!resp)
1351 return -ENOMEM;
1352
1353 /*
1354 * Do a Get Device ID command, since it comes back with some
1355 * useful info.
1356 */
1357 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1358 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1359
1360 retry:
1361 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
1362
1363 rv = wait_for_msg_done(smi_info);
1364 if (rv)
1365 goto out;
1366
1367 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
1368 resp, IPMI_MAX_MSG_LENGTH);
1369
1370 /* Check and record info from the get device id, in case we need it. */
1371 rv = ipmi_demangle_device_id(resp[0] >> 2, resp[1],
1372 resp + 2, resp_len - 2, &smi_info->device_id);
1373 if (rv) {
1374 /* record completion code */
1375 unsigned char cc = *(resp + 2);
1376
1377 if (cc != IPMI_CC_NO_ERROR &&
1378 ++retry_count <= GET_DEVICE_ID_MAX_RETRY) {
1379 dev_warn_ratelimited(smi_info->io.dev,
1380 "BMC returned 0x%2.2x, retry get bmc device id\n",
1381 cc);
1382 goto retry;
1383 }
1384 }
1385
1386 out:
1387 kfree(resp);
1388 return rv;
1389 }
1390
get_global_enables(struct smi_info * smi_info,u8 * enables)1391 static int get_global_enables(struct smi_info *smi_info, u8 *enables)
1392 {
1393 unsigned char msg[3];
1394 unsigned char *resp;
1395 unsigned long resp_len;
1396 int rv;
1397
1398 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1399 if (!resp)
1400 return -ENOMEM;
1401
1402 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1403 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
1404 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
1405
1406 rv = wait_for_msg_done(smi_info);
1407 if (rv) {
1408 dev_warn(smi_info->io.dev,
1409 "Error getting response from get global enables command: %d\n",
1410 rv);
1411 goto out;
1412 }
1413
1414 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
1415 resp, IPMI_MAX_MSG_LENGTH);
1416
1417 if (resp_len < 4 ||
1418 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
1419 resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD ||
1420 resp[2] != 0) {
1421 dev_warn(smi_info->io.dev,
1422 "Invalid return from get global enables command: %ld %x %x %x\n",
1423 resp_len, resp[0], resp[1], resp[2]);
1424 rv = -EINVAL;
1425 goto out;
1426 } else {
1427 *enables = resp[3];
1428 }
1429
1430 out:
1431 kfree(resp);
1432 return rv;
1433 }
1434
1435 /*
1436 * Returns 1 if it gets an error from the command.
1437 */
set_global_enables(struct smi_info * smi_info,u8 enables)1438 static int set_global_enables(struct smi_info *smi_info, u8 enables)
1439 {
1440 unsigned char msg[3];
1441 unsigned char *resp;
1442 unsigned long resp_len;
1443 int rv;
1444
1445 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1446 if (!resp)
1447 return -ENOMEM;
1448
1449 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1450 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1451 msg[2] = enables;
1452 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
1453
1454 rv = wait_for_msg_done(smi_info);
1455 if (rv) {
1456 dev_warn(smi_info->io.dev,
1457 "Error getting response from set global enables command: %d\n",
1458 rv);
1459 goto out;
1460 }
1461
1462 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
1463 resp, IPMI_MAX_MSG_LENGTH);
1464
1465 if (resp_len < 3 ||
1466 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
1467 resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
1468 dev_warn(smi_info->io.dev,
1469 "Invalid return from set global enables command: %ld %x %x\n",
1470 resp_len, resp[0], resp[1]);
1471 rv = -EINVAL;
1472 goto out;
1473 }
1474
1475 if (resp[2] != 0)
1476 rv = 1;
1477
1478 out:
1479 kfree(resp);
1480 return rv;
1481 }
1482
1483 /*
1484 * Some BMCs do not support clearing the receive irq bit in the global
1485 * enables (even if they don't support interrupts on the BMC). Check
1486 * for this and handle it properly.
1487 */
check_clr_rcv_irq(struct smi_info * smi_info)1488 static void check_clr_rcv_irq(struct smi_info *smi_info)
1489 {
1490 u8 enables = 0;
1491 int rv;
1492
1493 rv = get_global_enables(smi_info, &enables);
1494 if (!rv) {
1495 if ((enables & IPMI_BMC_RCV_MSG_INTR) == 0)
1496 /* Already clear, should work ok. */
1497 return;
1498
1499 enables &= ~IPMI_BMC_RCV_MSG_INTR;
1500 rv = set_global_enables(smi_info, enables);
1501 }
1502
1503 if (rv < 0) {
1504 dev_err(smi_info->io.dev,
1505 "Cannot check clearing the rcv irq: %d\n", rv);
1506 return;
1507 }
1508
1509 if (rv) {
1510 /*
1511 * An error when setting the event buffer bit means
1512 * clearing the bit is not supported.
1513 */
1514 dev_warn(smi_info->io.dev,
1515 "The BMC does not support clearing the recv irq bit, compensating, but the BMC needs to be fixed.\n");
1516 smi_info->cannot_disable_irq = true;
1517 }
1518 }
1519
1520 /*
1521 * Some BMCs do not support setting the interrupt bits in the global
1522 * enables even if they support interrupts. Clearly bad, but we can
1523 * compensate.
1524 */
check_set_rcv_irq(struct smi_info * smi_info)1525 static void check_set_rcv_irq(struct smi_info *smi_info)
1526 {
1527 u8 enables = 0;
1528 int rv;
1529
1530 if (!smi_info->io.irq)
1531 return;
1532
1533 rv = get_global_enables(smi_info, &enables);
1534 if (!rv) {
1535 enables |= IPMI_BMC_RCV_MSG_INTR;
1536 rv = set_global_enables(smi_info, enables);
1537 }
1538
1539 if (rv < 0) {
1540 dev_err(smi_info->io.dev,
1541 "Cannot check setting the rcv irq: %d\n", rv);
1542 return;
1543 }
1544
1545 if (rv) {
1546 /*
1547 * An error when setting the event buffer bit means
1548 * setting the bit is not supported.
1549 */
1550 dev_warn(smi_info->io.dev,
1551 "The BMC does not support setting the recv irq bit, compensating, but the BMC needs to be fixed.\n");
1552 smi_info->cannot_disable_irq = true;
1553 smi_info->irq_enable_broken = true;
1554 }
1555 }
1556
try_enable_event_buffer(struct smi_info * smi_info)1557 static int try_enable_event_buffer(struct smi_info *smi_info)
1558 {
1559 unsigned char msg[3];
1560 unsigned char *resp;
1561 unsigned long resp_len;
1562 int rv = 0;
1563
1564 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1565 if (!resp)
1566 return -ENOMEM;
1567
1568 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1569 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
1570 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
1571
1572 rv = wait_for_msg_done(smi_info);
1573 if (rv) {
1574 pr_warn("Error getting response from get global enables command, the event buffer is not enabled\n");
1575 goto out;
1576 }
1577
1578 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
1579 resp, IPMI_MAX_MSG_LENGTH);
1580
1581 if (resp_len < 4 ||
1582 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
1583 resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD ||
1584 resp[2] != 0) {
1585 pr_warn("Invalid return from get global enables command, cannot enable the event buffer\n");
1586 rv = -EINVAL;
1587 goto out;
1588 }
1589
1590 if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
1591 /* buffer is already enabled, nothing to do. */
1592 smi_info->supports_event_msg_buff = true;
1593 goto out;
1594 }
1595
1596 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1597 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1598 msg[2] = resp[3] | IPMI_BMC_EVT_MSG_BUFF;
1599 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
1600
1601 rv = wait_for_msg_done(smi_info);
1602 if (rv) {
1603 pr_warn("Error getting response from set global, enables command, the event buffer is not enabled\n");
1604 goto out;
1605 }
1606
1607 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
1608 resp, IPMI_MAX_MSG_LENGTH);
1609
1610 if (resp_len < 3 ||
1611 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
1612 resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
1613 pr_warn("Invalid return from get global, enables command, not enable the event buffer\n");
1614 rv = -EINVAL;
1615 goto out;
1616 }
1617
1618 if (resp[2] != 0)
1619 /*
1620 * An error when setting the event buffer bit means
1621 * that the event buffer is not supported.
1622 */
1623 rv = -ENOENT;
1624 else
1625 smi_info->supports_event_msg_buff = true;
1626
1627 out:
1628 kfree(resp);
1629 return rv;
1630 }
1631
1632 #define IPMI_SI_ATTR(name) \
1633 static ssize_t name##_show(struct device *dev, \
1634 struct device_attribute *attr, \
1635 char *buf) \
1636 { \
1637 struct smi_info *smi_info = dev_get_drvdata(dev); \
1638 \
1639 return sysfs_emit(buf, "%u\n", smi_get_stat(smi_info, name)); \
1640 } \
1641 static DEVICE_ATTR_RO(name)
1642
type_show(struct device * dev,struct device_attribute * attr,char * buf)1643 static ssize_t type_show(struct device *dev,
1644 struct device_attribute *attr,
1645 char *buf)
1646 {
1647 struct smi_info *smi_info = dev_get_drvdata(dev);
1648
1649 return sysfs_emit(buf, "%s\n", si_to_str[smi_info->io.si_info->type]);
1650 }
1651 static DEVICE_ATTR_RO(type);
1652
interrupts_enabled_show(struct device * dev,struct device_attribute * attr,char * buf)1653 static ssize_t interrupts_enabled_show(struct device *dev,
1654 struct device_attribute *attr,
1655 char *buf)
1656 {
1657 struct smi_info *smi_info = dev_get_drvdata(dev);
1658 int enabled = smi_info->io.irq && !smi_info->interrupt_disabled;
1659
1660 return sysfs_emit(buf, "%d\n", enabled);
1661 }
1662 static DEVICE_ATTR_RO(interrupts_enabled);
1663
1664 IPMI_SI_ATTR(short_timeouts);
1665 IPMI_SI_ATTR(long_timeouts);
1666 IPMI_SI_ATTR(idles);
1667 IPMI_SI_ATTR(interrupts);
1668 IPMI_SI_ATTR(attentions);
1669 IPMI_SI_ATTR(flag_fetches);
1670 IPMI_SI_ATTR(hosed_count);
1671 IPMI_SI_ATTR(complete_transactions);
1672 IPMI_SI_ATTR(events);
1673 IPMI_SI_ATTR(watchdog_pretimeouts);
1674 IPMI_SI_ATTR(incoming_messages);
1675
params_show(struct device * dev,struct device_attribute * attr,char * buf)1676 static ssize_t params_show(struct device *dev,
1677 struct device_attribute *attr,
1678 char *buf)
1679 {
1680 struct smi_info *smi_info = dev_get_drvdata(dev);
1681
1682 return sysfs_emit(buf,
1683 "%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n",
1684 si_to_str[smi_info->io.si_info->type],
1685 addr_space_to_str[smi_info->io.addr_space],
1686 smi_info->io.addr_data,
1687 smi_info->io.regspacing,
1688 smi_info->io.regsize,
1689 smi_info->io.regshift,
1690 smi_info->io.irq,
1691 smi_info->io.slave_addr);
1692 }
1693 static DEVICE_ATTR_RO(params);
1694
1695 static struct attribute *ipmi_si_dev_attrs[] = {
1696 &dev_attr_type.attr,
1697 &dev_attr_interrupts_enabled.attr,
1698 &dev_attr_short_timeouts.attr,
1699 &dev_attr_long_timeouts.attr,
1700 &dev_attr_idles.attr,
1701 &dev_attr_interrupts.attr,
1702 &dev_attr_attentions.attr,
1703 &dev_attr_flag_fetches.attr,
1704 &dev_attr_hosed_count.attr,
1705 &dev_attr_complete_transactions.attr,
1706 &dev_attr_events.attr,
1707 &dev_attr_watchdog_pretimeouts.attr,
1708 &dev_attr_incoming_messages.attr,
1709 &dev_attr_params.attr,
1710 NULL
1711 };
1712
1713 static const struct attribute_group ipmi_si_dev_attr_group = {
1714 .attrs = ipmi_si_dev_attrs,
1715 };
1716
1717 /*
1718 * oem_data_avail_to_receive_msg_avail
1719 * @info - smi_info structure with msg_flags set
1720 *
1721 * Converts flags from OEM_DATA_AVAIL to RECEIVE_MSG_AVAIL
1722 * Returns 1 indicating need to re-run handle_flags().
1723 */
oem_data_avail_to_receive_msg_avail(struct smi_info * smi_info)1724 static int oem_data_avail_to_receive_msg_avail(struct smi_info *smi_info)
1725 {
1726 smi_info->msg_flags = ((smi_info->msg_flags & ~OEM_DATA_AVAIL) |
1727 RECEIVE_MSG_AVAIL);
1728 return 1;
1729 }
1730
1731 /*
1732 * setup_dell_poweredge_oem_data_handler
1733 * @info - smi_info.device_id must be populated
1734 *
1735 * Systems that match, but have firmware version < 1.40 may assert
1736 * OEM0_DATA_AVAIL on their own, without being told via Set Flags that
1737 * it's safe to do so. Such systems will de-assert OEM1_DATA_AVAIL
1738 * upon receipt of IPMI_GET_MSG_CMD, so we should treat these flags
1739 * as RECEIVE_MSG_AVAIL instead.
1740 *
1741 * As Dell has no plans to release IPMI 1.5 firmware that *ever*
1742 * assert the OEM[012] bits, and if it did, the driver would have to
1743 * change to handle that properly, we don't actually check for the
1744 * firmware version.
1745 * Device ID = 0x20 BMC on PowerEdge 8G servers
1746 * Device Revision = 0x80
1747 * Firmware Revision1 = 0x01 BMC version 1.40
1748 * Firmware Revision2 = 0x40 BCD encoded
1749 * IPMI Version = 0x51 IPMI 1.5
1750 * Manufacturer ID = A2 02 00 Dell IANA
1751 *
1752 * Additionally, PowerEdge systems with IPMI < 1.5 may also assert
1753 * OEM0_DATA_AVAIL and needs to be treated as RECEIVE_MSG_AVAIL.
1754 *
1755 */
1756 #define DELL_POWEREDGE_8G_BMC_DEVICE_ID 0x20
1757 #define DELL_POWEREDGE_8G_BMC_DEVICE_REV 0x80
1758 #define DELL_POWEREDGE_8G_BMC_IPMI_VERSION 0x51
1759 #define DELL_IANA_MFR_ID 0x0002a2
setup_dell_poweredge_oem_data_handler(struct smi_info * smi_info)1760 static void setup_dell_poweredge_oem_data_handler(struct smi_info *smi_info)
1761 {
1762 struct ipmi_device_id *id = &smi_info->device_id;
1763 if (id->manufacturer_id == DELL_IANA_MFR_ID) {
1764 if (id->device_id == DELL_POWEREDGE_8G_BMC_DEVICE_ID &&
1765 id->device_revision == DELL_POWEREDGE_8G_BMC_DEVICE_REV &&
1766 id->ipmi_version == DELL_POWEREDGE_8G_BMC_IPMI_VERSION) {
1767 smi_info->oem_data_avail_handler =
1768 oem_data_avail_to_receive_msg_avail;
1769 } else if (ipmi_version_major(id) < 1 ||
1770 (ipmi_version_major(id) == 1 &&
1771 ipmi_version_minor(id) < 5)) {
1772 smi_info->oem_data_avail_handler =
1773 oem_data_avail_to_receive_msg_avail;
1774 }
1775 }
1776 }
1777
1778 #define CANNOT_RETURN_REQUESTED_LENGTH 0xCA
return_hosed_msg_badsize(struct smi_info * smi_info)1779 static void return_hosed_msg_badsize(struct smi_info *smi_info)
1780 {
1781 struct ipmi_smi_msg *msg = smi_info->curr_msg;
1782
1783 /* Make it a response */
1784 msg->rsp[0] = msg->data[0] | 4;
1785 msg->rsp[1] = msg->data[1];
1786 msg->rsp[2] = CANNOT_RETURN_REQUESTED_LENGTH;
1787 msg->rsp_size = 3;
1788 smi_info->curr_msg = NULL;
1789 deliver_recv_msg(smi_info, msg);
1790 }
1791
1792 /*
1793 * dell_poweredge_bt_xaction_handler
1794 * @info - smi_info.device_id must be populated
1795 *
1796 * Dell PowerEdge servers with the BT interface (x6xx and 1750) will
1797 * not respond to a Get SDR command if the length of the data
1798 * requested is exactly 0x3A, which leads to command timeouts and no
1799 * data returned. This intercepts such commands, and causes userspace
1800 * callers to try again with a different-sized buffer, which succeeds.
1801 */
1802
1803 #define STORAGE_NETFN 0x0A
1804 #define STORAGE_CMD_GET_SDR 0x23
dell_poweredge_bt_xaction_handler(struct notifier_block * self,unsigned long unused,void * in)1805 static int dell_poweredge_bt_xaction_handler(struct notifier_block *self,
1806 unsigned long unused,
1807 void *in)
1808 {
1809 struct smi_info *smi_info = in;
1810 unsigned char *data = smi_info->curr_msg->data;
1811 unsigned int size = smi_info->curr_msg->data_size;
1812 if (size >= 8 &&
1813 (data[0]>>2) == STORAGE_NETFN &&
1814 data[1] == STORAGE_CMD_GET_SDR &&
1815 data[7] == 0x3A) {
1816 return_hosed_msg_badsize(smi_info);
1817 return NOTIFY_STOP;
1818 }
1819 return NOTIFY_DONE;
1820 }
1821
1822 static struct notifier_block dell_poweredge_bt_xaction_notifier = {
1823 .notifier_call = dell_poweredge_bt_xaction_handler,
1824 };
1825
1826 /*
1827 * setup_dell_poweredge_bt_xaction_handler
1828 * @info - smi_info.device_id must be filled in already
1829 *
1830 * Fills in smi_info.device_id.start_transaction_pre_hook
1831 * when we know what function to use there.
1832 */
1833 static void
setup_dell_poweredge_bt_xaction_handler(struct smi_info * smi_info)1834 setup_dell_poweredge_bt_xaction_handler(struct smi_info *smi_info)
1835 {
1836 struct ipmi_device_id *id = &smi_info->device_id;
1837 if (id->manufacturer_id == DELL_IANA_MFR_ID &&
1838 smi_info->io.si_info->type == SI_BT)
1839 register_xaction_notifier(&dell_poweredge_bt_xaction_notifier);
1840 }
1841
1842 /*
1843 * setup_oem_data_handler
1844 * @info - smi_info.device_id must be filled in already
1845 *
1846 * Fills in smi_info.device_id.oem_data_available_handler
1847 * when we know what function to use there.
1848 */
1849
setup_oem_data_handler(struct smi_info * smi_info)1850 static void setup_oem_data_handler(struct smi_info *smi_info)
1851 {
1852 setup_dell_poweredge_oem_data_handler(smi_info);
1853 }
1854
setup_xaction_handlers(struct smi_info * smi_info)1855 static void setup_xaction_handlers(struct smi_info *smi_info)
1856 {
1857 setup_dell_poweredge_bt_xaction_handler(smi_info);
1858 }
1859
check_for_broken_irqs(struct smi_info * smi_info)1860 static void check_for_broken_irqs(struct smi_info *smi_info)
1861 {
1862 check_clr_rcv_irq(smi_info);
1863 check_set_rcv_irq(smi_info);
1864 }
1865
stop_timer_and_thread(struct smi_info * smi_info)1866 static inline void stop_timer_and_thread(struct smi_info *smi_info)
1867 {
1868 if (smi_info->thread != NULL) {
1869 kthread_stop(smi_info->thread);
1870 smi_info->thread = NULL;
1871 }
1872
1873 smi_info->timer_can_start = false;
1874 timer_delete_sync(&smi_info->si_timer);
1875 }
1876
find_dup_si(struct smi_info * info)1877 static struct smi_info *find_dup_si(struct smi_info *info)
1878 {
1879 struct smi_info *e;
1880
1881 list_for_each_entry(e, &smi_infos, link) {
1882 if (e->io.addr_space != info->io.addr_space)
1883 continue;
1884 if (e->io.addr_data == info->io.addr_data) {
1885 /*
1886 * This is a cheap hack, ACPI doesn't have a defined
1887 * slave address but SMBIOS does. Pick it up from
1888 * any source that has it available.
1889 */
1890 if (info->io.slave_addr && !e->io.slave_addr)
1891 e->io.slave_addr = info->io.slave_addr;
1892 return e;
1893 }
1894 }
1895
1896 return NULL;
1897 }
1898
ipmi_si_add_smi(struct si_sm_io * io)1899 int ipmi_si_add_smi(struct si_sm_io *io)
1900 {
1901 int rv = 0;
1902 struct smi_info *new_smi, *dup;
1903
1904 /*
1905 * If the user gave us a hard-coded device at the same
1906 * address, they presumably want us to use it and not what is
1907 * in the firmware.
1908 */
1909 if (io->addr_source != SI_HARDCODED && io->addr_source != SI_HOTMOD &&
1910 ipmi_si_hardcode_match(io->addr_space, io->addr_data)) {
1911 dev_info(io->dev,
1912 "Hard-coded device at this address already exists");
1913 return -ENODEV;
1914 }
1915
1916 if (!io->io_setup) {
1917 if (IS_ENABLED(CONFIG_HAS_IOPORT) &&
1918 io->addr_space == IPMI_IO_ADDR_SPACE) {
1919 io->io_setup = ipmi_si_port_setup;
1920 } else if (io->addr_space == IPMI_MEM_ADDR_SPACE) {
1921 io->io_setup = ipmi_si_mem_setup;
1922 } else {
1923 return -EINVAL;
1924 }
1925 }
1926
1927 new_smi = kzalloc_obj(*new_smi);
1928 if (!new_smi)
1929 return -ENOMEM;
1930 spin_lock_init(&new_smi->si_lock);
1931
1932 new_smi->io = *io;
1933
1934 mutex_lock(&smi_infos_lock);
1935 dup = find_dup_si(new_smi);
1936 if (dup) {
1937 if (new_smi->io.addr_source == SI_ACPI &&
1938 dup->io.addr_source == SI_SMBIOS) {
1939 /* We prefer ACPI over SMBIOS. */
1940 dev_info(dup->io.dev,
1941 "Removing SMBIOS-specified %s state machine in favor of ACPI\n",
1942 si_to_str[new_smi->io.si_info->type]);
1943 cleanup_one_si(dup);
1944 } else {
1945 dev_info(new_smi->io.dev,
1946 "%s-specified %s state machine: duplicate\n",
1947 ipmi_addr_src_to_str(new_smi->io.addr_source),
1948 si_to_str[new_smi->io.si_info->type]);
1949 rv = -EBUSY;
1950 kfree(new_smi);
1951 goto out_err;
1952 }
1953 }
1954
1955 pr_info("Adding %s-specified %s state machine\n",
1956 ipmi_addr_src_to_str(new_smi->io.addr_source),
1957 si_to_str[new_smi->io.si_info->type]);
1958
1959 list_add_tail(&new_smi->link, &smi_infos);
1960
1961 if (initialized)
1962 rv = try_smi_init(new_smi);
1963 out_err:
1964 mutex_unlock(&smi_infos_lock);
1965 return rv;
1966 }
1967
1968 /*
1969 * Try to start up an interface. Must be called with smi_infos_lock
1970 * held, primarily to keep smi_num consistent, we only one to do these
1971 * one at a time.
1972 */
try_smi_init(struct smi_info * new_smi)1973 static int try_smi_init(struct smi_info *new_smi)
1974 {
1975 int rv = 0;
1976 int i;
1977
1978 pr_info("Trying %s-specified %s state machine at %s address 0x%lx, slave address 0x%x, irq %d\n",
1979 ipmi_addr_src_to_str(new_smi->io.addr_source),
1980 si_to_str[new_smi->io.si_info->type],
1981 addr_space_to_str[new_smi->io.addr_space],
1982 new_smi->io.addr_data,
1983 new_smi->io.slave_addr, new_smi->io.irq);
1984
1985 switch (new_smi->io.si_info->type) {
1986 case SI_KCS:
1987 new_smi->handlers = &kcs_smi_handlers;
1988 break;
1989
1990 case SI_SMIC:
1991 new_smi->handlers = &smic_smi_handlers;
1992 break;
1993
1994 case SI_BT:
1995 new_smi->handlers = &bt_smi_handlers;
1996 break;
1997
1998 default:
1999 /* No support for anything else yet. */
2000 rv = -EIO;
2001 goto out_err;
2002 }
2003
2004 new_smi->si_num = smi_num;
2005
2006 /* Do this early so it's available for logs. */
2007 if (!new_smi->io.dev) {
2008 pr_err("IPMI interface added with no device\n");
2009 rv = -EIO;
2010 goto out_err;
2011 }
2012
2013 /* Allocate the state machine's data and initialize it. */
2014 new_smi->si_sm = kmalloc(new_smi->handlers->size(), GFP_KERNEL);
2015 if (!new_smi->si_sm) {
2016 rv = -ENOMEM;
2017 goto out_err;
2018 }
2019 new_smi->io.io_size = new_smi->handlers->init_data(new_smi->si_sm,
2020 &new_smi->io);
2021
2022 /* Now that we know the I/O size, we can set up the I/O. */
2023 rv = new_smi->io.io_setup(&new_smi->io);
2024 if (rv) {
2025 dev_err(new_smi->io.dev, "Could not set up I/O space\n");
2026 goto out_err;
2027 }
2028
2029 /* Do low-level detection first. */
2030 if (new_smi->handlers->detect(new_smi->si_sm)) {
2031 if (new_smi->io.addr_source)
2032 dev_err(new_smi->io.dev,
2033 "Interface detection failed\n");
2034 rv = -ENODEV;
2035 goto out_err;
2036 }
2037
2038 /*
2039 * Attempt a get device id command. If it fails, we probably
2040 * don't have a BMC here.
2041 */
2042 rv = try_get_dev_id(new_smi);
2043 if (rv) {
2044 if (new_smi->io.addr_source)
2045 dev_err(new_smi->io.dev,
2046 "There appears to be no BMC at this location\n");
2047 goto out_err;
2048 }
2049
2050 setup_oem_data_handler(new_smi);
2051 setup_xaction_handlers(new_smi);
2052 check_for_broken_irqs(new_smi);
2053
2054 new_smi->waiting_msg = NULL;
2055 new_smi->curr_msg = NULL;
2056 atomic_set(&new_smi->req_events, 0);
2057 new_smi->run_to_completion = false;
2058 for (i = 0; i < SI_NUM_STATS; i++)
2059 atomic_set(&new_smi->stats[i], 0);
2060
2061 new_smi->interrupt_disabled = true;
2062 atomic_set(&new_smi->need_watch, 0);
2063
2064 rv = try_enable_event_buffer(new_smi);
2065 if (rv == 0)
2066 new_smi->has_event_buffer = true;
2067
2068 /*
2069 * Start clearing the flags before we enable interrupts or the
2070 * timer to avoid racing with the timer.
2071 */
2072 start_clear_flags(new_smi);
2073
2074 /*
2075 * IRQ is defined to be set when non-zero. req_events will
2076 * cause a global flags check that will enable interrupts.
2077 */
2078 if (new_smi->io.irq) {
2079 new_smi->interrupt_disabled = false;
2080 atomic_set(&new_smi->req_events, 1);
2081 }
2082
2083 dev_set_drvdata(new_smi->io.dev, new_smi);
2084 rv = device_add_group(new_smi->io.dev, &ipmi_si_dev_attr_group);
2085 if (rv) {
2086 dev_err(new_smi->io.dev,
2087 "Unable to add device attributes: error %d\n",
2088 rv);
2089 goto out_err;
2090 }
2091 new_smi->dev_group_added = true;
2092
2093 rv = ipmi_register_smi(&handlers,
2094 new_smi,
2095 new_smi->io.dev,
2096 new_smi->io.slave_addr);
2097 if (rv) {
2098 dev_err(new_smi->io.dev,
2099 "Unable to register device: error %d\n",
2100 rv);
2101 goto out_err;
2102 }
2103
2104 /* Don't increment till we know we have succeeded. */
2105 smi_num++;
2106
2107 dev_info(new_smi->io.dev, "IPMI %s interface initialized\n",
2108 si_to_str[new_smi->io.si_info->type]);
2109
2110 WARN_ON(new_smi->io.dev->init_name != NULL);
2111
2112 out_err:
2113 if (rv && new_smi->io.io_cleanup) {
2114 new_smi->io.io_cleanup(&new_smi->io);
2115 new_smi->io.io_cleanup = NULL;
2116 }
2117
2118 if (rv && new_smi->si_sm) {
2119 kfree(new_smi->si_sm);
2120 new_smi->si_sm = NULL;
2121 }
2122
2123 return rv;
2124 }
2125
2126 /*
2127 * Devices in the same address space at the same address are the same.
2128 */
ipmi_smi_info_same(struct smi_info * e1,struct smi_info * e2)2129 static bool __init ipmi_smi_info_same(struct smi_info *e1, struct smi_info *e2)
2130 {
2131 return (e1->io.addr_space == e2->io.addr_space &&
2132 e1->io.addr_data == e2->io.addr_data);
2133 }
2134
init_ipmi_si(void)2135 static int __init init_ipmi_si(void)
2136 {
2137 struct smi_info *e, *e2;
2138
2139 if (initialized)
2140 return 0;
2141
2142 ipmi_hardcode_init();
2143
2144 pr_info("IPMI System Interface driver\n");
2145
2146 ipmi_si_platform_init();
2147
2148 ipmi_si_pci_init();
2149
2150 ipmi_si_ls2k_init();
2151
2152 ipmi_si_parisc_init();
2153
2154 mutex_lock(&smi_infos_lock);
2155
2156 /*
2157 * Scan through all the devices. We prefer devices with
2158 * interrupts, so go through those first in case there are any
2159 * duplicates that don't have the interrupt set.
2160 */
2161 list_for_each_entry(e, &smi_infos, link) {
2162 bool dup = false;
2163
2164 /* Register ones with interrupts first. */
2165 if (!e->io.irq)
2166 continue;
2167
2168 /*
2169 * Go through the ones we have already seen to see if this
2170 * is a dup.
2171 */
2172 list_for_each_entry(e2, &smi_infos, link) {
2173 if (e2 == e)
2174 break;
2175 if (e2->io.irq && ipmi_smi_info_same(e, e2)) {
2176 dup = true;
2177 break;
2178 }
2179 }
2180 if (!dup)
2181 try_smi_init(e);
2182 }
2183
2184 /*
2185 * Now try devices without interrupts.
2186 */
2187 list_for_each_entry(e, &smi_infos, link) {
2188 bool dup = false;
2189
2190 if (e->io.irq)
2191 continue;
2192
2193 /*
2194 * Go through the ones we have already seen to see if
2195 * this is a dup. We have already looked at the ones
2196 * with interrupts.
2197 */
2198 list_for_each_entry(e2, &smi_infos, link) {
2199 if (!e2->io.irq)
2200 continue;
2201 if (ipmi_smi_info_same(e, e2)) {
2202 dup = true;
2203 break;
2204 }
2205 }
2206 list_for_each_entry(e2, &smi_infos, link) {
2207 if (e2 == e)
2208 break;
2209 if (ipmi_smi_info_same(e, e2)) {
2210 dup = true;
2211 break;
2212 }
2213 }
2214 if (!dup)
2215 try_smi_init(e);
2216 }
2217
2218 initialized = true;
2219 mutex_unlock(&smi_infos_lock);
2220
2221 mutex_lock(&smi_infos_lock);
2222 if (unload_when_empty && list_empty(&smi_infos)) {
2223 mutex_unlock(&smi_infos_lock);
2224 cleanup_ipmi_si();
2225 pr_warn("Unable to find any System Interface(s)\n");
2226 return -ENODEV;
2227 } else {
2228 mutex_unlock(&smi_infos_lock);
2229 return 0;
2230 }
2231 }
2232 module_init(init_ipmi_si);
2233
wait_msg_processed(struct smi_info * smi_info)2234 static void wait_msg_processed(struct smi_info *smi_info)
2235 {
2236 unsigned long jiffies_now;
2237 long time_diff;
2238
2239 while (smi_info->si_state != SI_HOSED &&
2240 (smi_info->curr_msg || (smi_info->si_state != SI_NORMAL))) {
2241 jiffies_now = jiffies;
2242 time_diff = (((long)jiffies_now - (long)smi_info->last_timeout_jiffies)
2243 * SI_USEC_PER_JIFFY);
2244 smi_event_handler(smi_info, time_diff);
2245 schedule_timeout_uninterruptible(1);
2246 }
2247 }
2248
shutdown_smi(void * send_info)2249 static void shutdown_smi(void *send_info)
2250 {
2251 struct smi_info *smi_info = send_info;
2252
2253 if (smi_info->dev_group_added) {
2254 device_remove_group(smi_info->io.dev, &ipmi_si_dev_attr_group);
2255 smi_info->dev_group_added = false;
2256 }
2257 if (smi_info->io.dev)
2258 dev_set_drvdata(smi_info->io.dev, NULL);
2259
2260 /*
2261 * Make sure that interrupts, the timer and the thread are
2262 * stopped and will not run again.
2263 */
2264 smi_info->interrupt_disabled = true;
2265 if (smi_info->io.irq_cleanup) {
2266 smi_info->io.irq_cleanup(&smi_info->io);
2267 smi_info->io.irq_cleanup = NULL;
2268 }
2269 stop_timer_and_thread(smi_info);
2270
2271 /*
2272 * Wait until we know that we are out of any interrupt
2273 * handlers might have been running before we freed the
2274 * interrupt.
2275 */
2276 synchronize_rcu();
2277
2278 /*
2279 * Timeouts are stopped, now make sure the interrupts are off
2280 * in the BMC. Note that timers and CPU interrupts are off,
2281 * so no need for locks.
2282 */
2283 wait_msg_processed(smi_info);
2284
2285 if (smi_info->handlers)
2286 disable_si_irq(smi_info);
2287
2288 wait_msg_processed(smi_info);
2289
2290 if (smi_info->handlers)
2291 smi_info->handlers->cleanup(smi_info->si_sm);
2292
2293 if (smi_info->io.io_cleanup) {
2294 smi_info->io.io_cleanup(&smi_info->io);
2295 smi_info->io.io_cleanup = NULL;
2296 }
2297
2298 kfree(smi_info->si_sm);
2299 smi_info->si_sm = NULL;
2300
2301 smi_info->intf = NULL;
2302 }
2303
2304 /*
2305 * Must be called with smi_infos_lock held, to serialize the
2306 * smi_info->intf check.
2307 */
cleanup_one_si(struct smi_info * smi_info)2308 static void cleanup_one_si(struct smi_info *smi_info)
2309 {
2310 if (!smi_info)
2311 return;
2312
2313 list_del(&smi_info->link);
2314 ipmi_unregister_smi(smi_info->intf);
2315 kfree(smi_info);
2316 }
2317
ipmi_si_remove_by_dev(struct device * dev)2318 void ipmi_si_remove_by_dev(struct device *dev)
2319 {
2320 struct smi_info *e;
2321
2322 mutex_lock(&smi_infos_lock);
2323 list_for_each_entry(e, &smi_infos, link) {
2324 if (e->io.dev == dev) {
2325 cleanup_one_si(e);
2326 break;
2327 }
2328 }
2329 mutex_unlock(&smi_infos_lock);
2330 }
2331
ipmi_si_remove_by_data(int addr_space,enum si_type si_type,unsigned long addr)2332 struct device *ipmi_si_remove_by_data(int addr_space, enum si_type si_type,
2333 unsigned long addr)
2334 {
2335 /* remove */
2336 struct smi_info *e, *tmp_e;
2337 struct device *dev = NULL;
2338
2339 mutex_lock(&smi_infos_lock);
2340 list_for_each_entry_safe(e, tmp_e, &smi_infos, link) {
2341 if (e->io.addr_space != addr_space)
2342 continue;
2343 if (e->io.si_info->type != si_type)
2344 continue;
2345 if (e->io.addr_data == addr) {
2346 dev = get_device(e->io.dev);
2347 cleanup_one_si(e);
2348 }
2349 }
2350 mutex_unlock(&smi_infos_lock);
2351
2352 return dev;
2353 }
2354
cleanup_ipmi_si(void)2355 static void cleanup_ipmi_si(void)
2356 {
2357 struct smi_info *e, *tmp_e;
2358
2359 if (!initialized)
2360 return;
2361
2362 ipmi_si_pci_shutdown();
2363
2364 ipmi_si_ls2k_shutdown();
2365
2366 ipmi_si_parisc_shutdown();
2367
2368 ipmi_si_platform_shutdown();
2369
2370 mutex_lock(&smi_infos_lock);
2371 list_for_each_entry_safe(e, tmp_e, &smi_infos, link)
2372 cleanup_one_si(e);
2373 mutex_unlock(&smi_infos_lock);
2374
2375 ipmi_si_hardcode_exit();
2376 ipmi_si_hotmod_exit();
2377 }
2378 module_exit(cleanup_ipmi_si);
2379
2380 MODULE_ALIAS("platform:dmi-ipmi-si");
2381 MODULE_LICENSE("GPL");
2382 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
2383 MODULE_DESCRIPTION("Interface to the IPMI driver for the KCS, SMIC, and BT system interfaces.");
2384