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