1 /*
2 * ipmi_si.c
3 *
4 * The interface to the IPMI driver for the system interfaces (KCS, SMIC,
5 * BT).
6 *
7 * Author: MontaVista Software, Inc.
8 * Corey Minyard <minyard@mvista.com>
9 * source@mvista.com
10 *
11 * Copyright 2002 MontaVista Software Inc.
12 * Copyright 2006 IBM Corp., Christian Krafft <krafft@de.ibm.com>
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version.
18 *
19 *
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
28 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
29 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * You should have received a copy of the GNU General Public License along
32 * with this program; if not, write to the Free Software Foundation, Inc.,
33 * 675 Mass Ave, Cambridge, MA 02139, USA.
34 */
35
36 /*
37 * This file holds the "policy" for the interface to the SMI state
38 * machine. It does the configuration, handles timers and interrupts,
39 * and drives the real SMI state machine.
40 */
41
42 #include <linux/module.h>
43 #include <linux/moduleparam.h>
44 #include <linux/sched.h>
45 #include <linux/seq_file.h>
46 #include <linux/timer.h>
47 #include <linux/errno.h>
48 #include <linux/spinlock.h>
49 #include <linux/slab.h>
50 #include <linux/delay.h>
51 #include <linux/list.h>
52 #include <linux/pci.h>
53 #include <linux/ioport.h>
54 #include <linux/notifier.h>
55 #include <linux/mutex.h>
56 #include <linux/kthread.h>
57 #include <asm/irq.h>
58 #include <linux/interrupt.h>
59 #include <linux/rcupdate.h>
60 #include <linux/ipmi.h>
61 #include <linux/ipmi_smi.h>
62 #include <asm/io.h>
63 #include "ipmi_si_sm.h"
64 #include <linux/init.h>
65 #include <linux/dmi.h>
66 #include <linux/string.h>
67 #include <linux/ctype.h>
68 #include <linux/pnp.h>
69 #include <linux/of_device.h>
70 #include <linux/of_platform.h>
71 #include <linux/of_address.h>
72 #include <linux/of_irq.h>
73
74 #define PFX "ipmi_si: "
75
76 /* Measure times between events in the driver. */
77 #undef DEBUG_TIMING
78
79 /* Call every 10 ms. */
80 #define SI_TIMEOUT_TIME_USEC 10000
81 #define SI_USEC_PER_JIFFY (1000000/HZ)
82 #define SI_TIMEOUT_JIFFIES (SI_TIMEOUT_TIME_USEC/SI_USEC_PER_JIFFY)
83 #define SI_SHORT_TIMEOUT_USEC 250 /* .25ms when the SM request a
84 short timeout */
85
86 enum si_intf_state {
87 SI_NORMAL,
88 SI_GETTING_FLAGS,
89 SI_GETTING_EVENTS,
90 SI_CLEARING_FLAGS,
91 SI_CLEARING_FLAGS_THEN_SET_IRQ,
92 SI_GETTING_MESSAGES,
93 SI_ENABLE_INTERRUPTS1,
94 SI_ENABLE_INTERRUPTS2,
95 SI_DISABLE_INTERRUPTS1,
96 SI_DISABLE_INTERRUPTS2
97 /* FIXME - add watchdog stuff. */
98 };
99
100 /* Some BT-specific defines we need here. */
101 #define IPMI_BT_INTMASK_REG 2
102 #define IPMI_BT_INTMASK_CLEAR_IRQ_BIT 2
103 #define IPMI_BT_INTMASK_ENABLE_IRQ_BIT 1
104
105 enum si_type {
106 SI_KCS, SI_SMIC, SI_BT
107 };
108 static char *si_to_str[] = { "kcs", "smic", "bt" };
109
110 static char *ipmi_addr_src_to_str[] = { NULL, "hotmod", "hardcoded", "SPMI",
111 "ACPI", "SMBIOS", "PCI",
112 "device-tree", "default" };
113
114 #define DEVICE_NAME "ipmi_si"
115
116 static struct platform_driver ipmi_driver;
117
118 /*
119 * Indexes into stats[] in smi_info below.
120 */
121 enum si_stat_indexes {
122 /*
123 * Number of times the driver requested a timer while an operation
124 * was in progress.
125 */
126 SI_STAT_short_timeouts = 0,
127
128 /*
129 * Number of times the driver requested a timer while nothing was in
130 * progress.
131 */
132 SI_STAT_long_timeouts,
133
134 /* Number of times the interface was idle while being polled. */
135 SI_STAT_idles,
136
137 /* Number of interrupts the driver handled. */
138 SI_STAT_interrupts,
139
140 /* Number of time the driver got an ATTN from the hardware. */
141 SI_STAT_attentions,
142
143 /* Number of times the driver requested flags from the hardware. */
144 SI_STAT_flag_fetches,
145
146 /* Number of times the hardware didn't follow the state machine. */
147 SI_STAT_hosed_count,
148
149 /* Number of completed messages. */
150 SI_STAT_complete_transactions,
151
152 /* Number of IPMI events received from the hardware. */
153 SI_STAT_events,
154
155 /* Number of watchdog pretimeouts. */
156 SI_STAT_watchdog_pretimeouts,
157
158 /* Number of asyncronous messages received. */
159 SI_STAT_incoming_messages,
160
161
162 /* This *must* remain last, add new values above this. */
163 SI_NUM_STATS
164 };
165
166 struct smi_info {
167 int intf_num;
168 ipmi_smi_t intf;
169 struct si_sm_data *si_sm;
170 struct si_sm_handlers *handlers;
171 enum si_type si_type;
172 spinlock_t si_lock;
173 struct list_head xmit_msgs;
174 struct list_head hp_xmit_msgs;
175 struct ipmi_smi_msg *curr_msg;
176 enum si_intf_state si_state;
177
178 /*
179 * Used to handle the various types of I/O that can occur with
180 * IPMI
181 */
182 struct si_sm_io io;
183 int (*io_setup)(struct smi_info *info);
184 void (*io_cleanup)(struct smi_info *info);
185 int (*irq_setup)(struct smi_info *info);
186 void (*irq_cleanup)(struct smi_info *info);
187 unsigned int io_size;
188 enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */
189 void (*addr_source_cleanup)(struct smi_info *info);
190 void *addr_source_data;
191
192 /*
193 * Per-OEM handler, called from handle_flags(). Returns 1
194 * when handle_flags() needs to be re-run or 0 indicating it
195 * set si_state itself.
196 */
197 int (*oem_data_avail_handler)(struct smi_info *smi_info);
198
199 /*
200 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
201 * is set to hold the flags until we are done handling everything
202 * from the flags.
203 */
204 #define RECEIVE_MSG_AVAIL 0x01
205 #define EVENT_MSG_BUFFER_FULL 0x02
206 #define WDT_PRE_TIMEOUT_INT 0x08
207 #define OEM0_DATA_AVAIL 0x20
208 #define OEM1_DATA_AVAIL 0x40
209 #define OEM2_DATA_AVAIL 0x80
210 #define OEM_DATA_AVAIL (OEM0_DATA_AVAIL | \
211 OEM1_DATA_AVAIL | \
212 OEM2_DATA_AVAIL)
213 unsigned char msg_flags;
214
215 /* Does the BMC have an event buffer? */
216 char has_event_buffer;
217
218 /*
219 * If set to true, this will request events the next time the
220 * state machine is idle.
221 */
222 atomic_t req_events;
223
224 /*
225 * If true, run the state machine to completion on every send
226 * call. Generally used after a panic to make sure stuff goes
227 * out.
228 */
229 int run_to_completion;
230
231 /* The I/O port of an SI interface. */
232 int port;
233
234 /*
235 * The space between start addresses of the two ports. For
236 * instance, if the first port is 0xca2 and the spacing is 4, then
237 * the second port is 0xca6.
238 */
239 unsigned int spacing;
240
241 /* zero if no irq; */
242 int irq;
243
244 /* The timer for this si. */
245 struct timer_list si_timer;
246
247 /* This flag is set, if the timer is running (timer_pending() isn't enough) */
248 bool timer_running;
249
250 /* The time (in jiffies) the last timeout occurred at. */
251 unsigned long last_timeout_jiffies;
252
253 /* Used to gracefully stop the timer without race conditions. */
254 atomic_t stop_operation;
255
256 /*
257 * The driver will disable interrupts when it gets into a
258 * situation where it cannot handle messages due to lack of
259 * memory. Once that situation clears up, it will re-enable
260 * interrupts.
261 */
262 int interrupt_disabled;
263
264 /* From the get device id response... */
265 struct ipmi_device_id device_id;
266
267 /* Driver model stuff. */
268 struct device *dev;
269 struct platform_device *pdev;
270
271 /*
272 * True if we allocated the device, false if it came from
273 * someplace else (like PCI).
274 */
275 int dev_registered;
276
277 /* Slave address, could be reported from DMI. */
278 unsigned char slave_addr;
279
280 /* Counters and things for the proc filesystem. */
281 atomic_t stats[SI_NUM_STATS];
282
283 struct task_struct *thread;
284
285 struct list_head link;
286 union ipmi_smi_info_union addr_info;
287 };
288
289 #define smi_inc_stat(smi, stat) \
290 atomic_inc(&(smi)->stats[SI_STAT_ ## stat])
291 #define smi_get_stat(smi, stat) \
292 ((unsigned int) atomic_read(&(smi)->stats[SI_STAT_ ## stat]))
293
294 #define SI_MAX_PARMS 4
295
296 static int force_kipmid[SI_MAX_PARMS];
297 static int num_force_kipmid;
298 #ifdef CONFIG_PCI
299 static int pci_registered;
300 #endif
301 #ifdef CONFIG_ACPI
302 static int pnp_registered;
303 #endif
304
305 static unsigned int kipmid_max_busy_us[SI_MAX_PARMS];
306 static int num_max_busy_us;
307
308 static int unload_when_empty = 1;
309
310 static int add_smi(struct smi_info *smi);
311 static int try_smi_init(struct smi_info *smi);
312 static void cleanup_one_si(struct smi_info *to_clean);
313 static void cleanup_ipmi_si(void);
314
315 static ATOMIC_NOTIFIER_HEAD(xaction_notifier_list);
register_xaction_notifier(struct notifier_block * nb)316 static int register_xaction_notifier(struct notifier_block *nb)
317 {
318 return atomic_notifier_chain_register(&xaction_notifier_list, nb);
319 }
320
deliver_recv_msg(struct smi_info * smi_info,struct ipmi_smi_msg * msg)321 static void deliver_recv_msg(struct smi_info *smi_info,
322 struct ipmi_smi_msg *msg)
323 {
324 /* Deliver the message to the upper layer. */
325 ipmi_smi_msg_received(smi_info->intf, msg);
326 }
327
return_hosed_msg(struct smi_info * smi_info,int cCode)328 static void return_hosed_msg(struct smi_info *smi_info, int cCode)
329 {
330 struct ipmi_smi_msg *msg = smi_info->curr_msg;
331
332 if (cCode < 0 || cCode > IPMI_ERR_UNSPECIFIED)
333 cCode = IPMI_ERR_UNSPECIFIED;
334 /* else use it as is */
335
336 /* Make it a response */
337 msg->rsp[0] = msg->data[0] | 4;
338 msg->rsp[1] = msg->data[1];
339 msg->rsp[2] = cCode;
340 msg->rsp_size = 3;
341
342 smi_info->curr_msg = NULL;
343 deliver_recv_msg(smi_info, msg);
344 }
345
start_next_msg(struct smi_info * smi_info)346 static enum si_sm_result start_next_msg(struct smi_info *smi_info)
347 {
348 int rv;
349 struct list_head *entry = NULL;
350 #ifdef DEBUG_TIMING
351 struct timeval t;
352 #endif
353
354 /* Pick the high priority queue first. */
355 if (!list_empty(&(smi_info->hp_xmit_msgs))) {
356 entry = smi_info->hp_xmit_msgs.next;
357 } else if (!list_empty(&(smi_info->xmit_msgs))) {
358 entry = smi_info->xmit_msgs.next;
359 }
360
361 if (!entry) {
362 smi_info->curr_msg = NULL;
363 rv = SI_SM_IDLE;
364 } else {
365 int err;
366
367 list_del(entry);
368 smi_info->curr_msg = list_entry(entry,
369 struct ipmi_smi_msg,
370 link);
371 #ifdef DEBUG_TIMING
372 do_gettimeofday(&t);
373 printk(KERN_DEBUG "**Start2: %d.%9.9d\n", t.tv_sec, t.tv_usec);
374 #endif
375 err = atomic_notifier_call_chain(&xaction_notifier_list,
376 0, smi_info);
377 if (err & NOTIFY_STOP_MASK) {
378 rv = SI_SM_CALL_WITHOUT_DELAY;
379 goto out;
380 }
381 err = smi_info->handlers->start_transaction(
382 smi_info->si_sm,
383 smi_info->curr_msg->data,
384 smi_info->curr_msg->data_size);
385 if (err)
386 return_hosed_msg(smi_info, err);
387
388 rv = SI_SM_CALL_WITHOUT_DELAY;
389 }
390 out:
391 return rv;
392 }
393
start_enable_irq(struct smi_info * smi_info)394 static void start_enable_irq(struct smi_info *smi_info)
395 {
396 unsigned char msg[2];
397
398 /*
399 * If we are enabling interrupts, we have to tell the
400 * BMC to use them.
401 */
402 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
403 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
404
405 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
406 smi_info->si_state = SI_ENABLE_INTERRUPTS1;
407 }
408
start_disable_irq(struct smi_info * smi_info)409 static void start_disable_irq(struct smi_info *smi_info)
410 {
411 unsigned char msg[2];
412
413 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
414 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
415
416 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
417 smi_info->si_state = SI_DISABLE_INTERRUPTS1;
418 }
419
start_clear_flags(struct smi_info * smi_info)420 static void start_clear_flags(struct smi_info *smi_info)
421 {
422 unsigned char msg[3];
423
424 /* Make sure the watchdog pre-timeout flag is not set at startup. */
425 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
426 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
427 msg[2] = WDT_PRE_TIMEOUT_INT;
428
429 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
430 smi_info->si_state = SI_CLEARING_FLAGS;
431 }
432
smi_mod_timer(struct smi_info * smi_info,unsigned long new_val)433 static void smi_mod_timer(struct smi_info *smi_info, unsigned long new_val)
434 {
435 smi_info->last_timeout_jiffies = jiffies;
436 mod_timer(&smi_info->si_timer, new_val);
437 smi_info->timer_running = true;
438 }
439
440 /*
441 * When we have a situtaion where we run out of memory and cannot
442 * allocate messages, we just leave them in the BMC and run the system
443 * polled until we can allocate some memory. Once we have some
444 * memory, we will re-enable the interrupt.
445 */
disable_si_irq(struct smi_info * smi_info)446 static inline void disable_si_irq(struct smi_info *smi_info)
447 {
448 if ((smi_info->irq) && (!smi_info->interrupt_disabled)) {
449 start_disable_irq(smi_info);
450 smi_info->interrupt_disabled = 1;
451 if (!atomic_read(&smi_info->stop_operation))
452 smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
453 }
454 }
455
enable_si_irq(struct smi_info * smi_info)456 static inline void enable_si_irq(struct smi_info *smi_info)
457 {
458 if ((smi_info->irq) && (smi_info->interrupt_disabled)) {
459 start_enable_irq(smi_info);
460 smi_info->interrupt_disabled = 0;
461 }
462 }
463
handle_flags(struct smi_info * smi_info)464 static void handle_flags(struct smi_info *smi_info)
465 {
466 retry:
467 if (smi_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
468 /* Watchdog pre-timeout */
469 smi_inc_stat(smi_info, watchdog_pretimeouts);
470
471 start_clear_flags(smi_info);
472 smi_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
473 ipmi_smi_watchdog_pretimeout(smi_info->intf);
474 } else if (smi_info->msg_flags & RECEIVE_MSG_AVAIL) {
475 /* Messages available. */
476 smi_info->curr_msg = ipmi_alloc_smi_msg();
477 if (!smi_info->curr_msg) {
478 disable_si_irq(smi_info);
479 smi_info->si_state = SI_NORMAL;
480 return;
481 }
482 enable_si_irq(smi_info);
483
484 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
485 smi_info->curr_msg->data[1] = IPMI_GET_MSG_CMD;
486 smi_info->curr_msg->data_size = 2;
487
488 smi_info->handlers->start_transaction(
489 smi_info->si_sm,
490 smi_info->curr_msg->data,
491 smi_info->curr_msg->data_size);
492 smi_info->si_state = SI_GETTING_MESSAGES;
493 } else if (smi_info->msg_flags & EVENT_MSG_BUFFER_FULL) {
494 /* Events available. */
495 smi_info->curr_msg = ipmi_alloc_smi_msg();
496 if (!smi_info->curr_msg) {
497 disable_si_irq(smi_info);
498 smi_info->si_state = SI_NORMAL;
499 return;
500 }
501 enable_si_irq(smi_info);
502
503 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
504 smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
505 smi_info->curr_msg->data_size = 2;
506
507 smi_info->handlers->start_transaction(
508 smi_info->si_sm,
509 smi_info->curr_msg->data,
510 smi_info->curr_msg->data_size);
511 smi_info->si_state = SI_GETTING_EVENTS;
512 } else if (smi_info->msg_flags & OEM_DATA_AVAIL &&
513 smi_info->oem_data_avail_handler) {
514 if (smi_info->oem_data_avail_handler(smi_info))
515 goto retry;
516 } else
517 smi_info->si_state = SI_NORMAL;
518 }
519
handle_transaction_done(struct smi_info * smi_info)520 static void handle_transaction_done(struct smi_info *smi_info)
521 {
522 struct ipmi_smi_msg *msg;
523 #ifdef DEBUG_TIMING
524 struct timeval t;
525
526 do_gettimeofday(&t);
527 printk(KERN_DEBUG "**Done: %d.%9.9d\n", t.tv_sec, t.tv_usec);
528 #endif
529 switch (smi_info->si_state) {
530 case SI_NORMAL:
531 if (!smi_info->curr_msg)
532 break;
533
534 smi_info->curr_msg->rsp_size
535 = smi_info->handlers->get_result(
536 smi_info->si_sm,
537 smi_info->curr_msg->rsp,
538 IPMI_MAX_MSG_LENGTH);
539
540 /*
541 * Do this here becase deliver_recv_msg() releases the
542 * lock, and a new message can be put in during the
543 * time the lock is released.
544 */
545 msg = smi_info->curr_msg;
546 smi_info->curr_msg = NULL;
547 deliver_recv_msg(smi_info, msg);
548 break;
549
550 case SI_GETTING_FLAGS:
551 {
552 unsigned char msg[4];
553 unsigned int len;
554
555 /* We got the flags from the SMI, now handle them. */
556 len = smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
557 if (msg[2] != 0) {
558 /* Error fetching flags, just give up for now. */
559 smi_info->si_state = SI_NORMAL;
560 } else if (len < 4) {
561 /*
562 * Hmm, no flags. That's technically illegal, but
563 * don't use uninitialized data.
564 */
565 smi_info->si_state = SI_NORMAL;
566 } else {
567 smi_info->msg_flags = msg[3];
568 handle_flags(smi_info);
569 }
570 break;
571 }
572
573 case SI_CLEARING_FLAGS:
574 case SI_CLEARING_FLAGS_THEN_SET_IRQ:
575 {
576 unsigned char msg[3];
577
578 /* We cleared the flags. */
579 smi_info->handlers->get_result(smi_info->si_sm, msg, 3);
580 if (msg[2] != 0) {
581 /* Error clearing flags */
582 dev_warn(smi_info->dev,
583 "Error clearing flags: %2.2x\n", msg[2]);
584 }
585 if (smi_info->si_state == SI_CLEARING_FLAGS_THEN_SET_IRQ)
586 start_enable_irq(smi_info);
587 else
588 smi_info->si_state = SI_NORMAL;
589 break;
590 }
591
592 case SI_GETTING_EVENTS:
593 {
594 smi_info->curr_msg->rsp_size
595 = smi_info->handlers->get_result(
596 smi_info->si_sm,
597 smi_info->curr_msg->rsp,
598 IPMI_MAX_MSG_LENGTH);
599
600 /*
601 * Do this here becase deliver_recv_msg() releases the
602 * lock, and a new message can be put in during the
603 * time the lock is released.
604 */
605 msg = smi_info->curr_msg;
606 smi_info->curr_msg = NULL;
607 if (msg->rsp[2] != 0) {
608 /* Error getting event, probably done. */
609 msg->done(msg);
610
611 /* Take off the event flag. */
612 smi_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
613 handle_flags(smi_info);
614 } else {
615 smi_inc_stat(smi_info, events);
616
617 /*
618 * Do this before we deliver the message
619 * because delivering the message releases the
620 * lock and something else can mess with the
621 * state.
622 */
623 handle_flags(smi_info);
624
625 deliver_recv_msg(smi_info, msg);
626 }
627 break;
628 }
629
630 case SI_GETTING_MESSAGES:
631 {
632 smi_info->curr_msg->rsp_size
633 = smi_info->handlers->get_result(
634 smi_info->si_sm,
635 smi_info->curr_msg->rsp,
636 IPMI_MAX_MSG_LENGTH);
637
638 /*
639 * Do this here becase deliver_recv_msg() releases the
640 * lock, and a new message can be put in during the
641 * time the lock is released.
642 */
643 msg = smi_info->curr_msg;
644 smi_info->curr_msg = NULL;
645 if (msg->rsp[2] != 0) {
646 /* Error getting event, probably done. */
647 msg->done(msg);
648
649 /* Take off the msg flag. */
650 smi_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
651 handle_flags(smi_info);
652 } else {
653 smi_inc_stat(smi_info, incoming_messages);
654
655 /*
656 * Do this before we deliver the message
657 * because delivering the message releases the
658 * lock and something else can mess with the
659 * state.
660 */
661 handle_flags(smi_info);
662
663 deliver_recv_msg(smi_info, msg);
664 }
665 break;
666 }
667
668 case SI_ENABLE_INTERRUPTS1:
669 {
670 unsigned char msg[4];
671
672 /* We got the flags from the SMI, now handle them. */
673 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
674 if (msg[2] != 0) {
675 dev_warn(smi_info->dev, "Could not enable interrupts"
676 ", failed get, using polled mode.\n");
677 smi_info->si_state = SI_NORMAL;
678 } else {
679 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
680 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
681 msg[2] = (msg[3] |
682 IPMI_BMC_RCV_MSG_INTR |
683 IPMI_BMC_EVT_MSG_INTR);
684 smi_info->handlers->start_transaction(
685 smi_info->si_sm, msg, 3);
686 smi_info->si_state = SI_ENABLE_INTERRUPTS2;
687 }
688 break;
689 }
690
691 case SI_ENABLE_INTERRUPTS2:
692 {
693 unsigned char msg[4];
694
695 /* We got the flags from the SMI, now handle them. */
696 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
697 if (msg[2] != 0)
698 dev_warn(smi_info->dev, "Could not enable interrupts"
699 ", failed set, using polled mode.\n");
700 else
701 smi_info->interrupt_disabled = 0;
702 smi_info->si_state = SI_NORMAL;
703 break;
704 }
705
706 case SI_DISABLE_INTERRUPTS1:
707 {
708 unsigned char msg[4];
709
710 /* We got the flags from the SMI, now handle them. */
711 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
712 if (msg[2] != 0) {
713 dev_warn(smi_info->dev, "Could not disable interrupts"
714 ", failed get.\n");
715 smi_info->si_state = SI_NORMAL;
716 } else {
717 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
718 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
719 msg[2] = (msg[3] &
720 ~(IPMI_BMC_RCV_MSG_INTR |
721 IPMI_BMC_EVT_MSG_INTR));
722 smi_info->handlers->start_transaction(
723 smi_info->si_sm, msg, 3);
724 smi_info->si_state = SI_DISABLE_INTERRUPTS2;
725 }
726 break;
727 }
728
729 case SI_DISABLE_INTERRUPTS2:
730 {
731 unsigned char msg[4];
732
733 /* We got the flags from the SMI, now handle them. */
734 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
735 if (msg[2] != 0) {
736 dev_warn(smi_info->dev, "Could not disable interrupts"
737 ", failed set.\n");
738 }
739 smi_info->si_state = SI_NORMAL;
740 break;
741 }
742 }
743 }
744
745 /*
746 * Called on timeouts and events. Timeouts should pass the elapsed
747 * time, interrupts should pass in zero. Must be called with
748 * si_lock held and interrupts disabled.
749 */
smi_event_handler(struct smi_info * smi_info,int time)750 static enum si_sm_result smi_event_handler(struct smi_info *smi_info,
751 int time)
752 {
753 enum si_sm_result si_sm_result;
754
755 restart:
756 /*
757 * There used to be a loop here that waited a little while
758 * (around 25us) before giving up. That turned out to be
759 * pointless, the minimum delays I was seeing were in the 300us
760 * range, which is far too long to wait in an interrupt. So
761 * we just run until the state machine tells us something
762 * happened or it needs a delay.
763 */
764 si_sm_result = smi_info->handlers->event(smi_info->si_sm, time);
765 time = 0;
766 while (si_sm_result == SI_SM_CALL_WITHOUT_DELAY)
767 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
768
769 if (si_sm_result == SI_SM_TRANSACTION_COMPLETE) {
770 smi_inc_stat(smi_info, complete_transactions);
771
772 handle_transaction_done(smi_info);
773 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
774 } else if (si_sm_result == SI_SM_HOSED) {
775 smi_inc_stat(smi_info, hosed_count);
776
777 /*
778 * Do the before return_hosed_msg, because that
779 * releases the lock.
780 */
781 smi_info->si_state = SI_NORMAL;
782 if (smi_info->curr_msg != NULL) {
783 /*
784 * If we were handling a user message, format
785 * a response to send to the upper layer to
786 * tell it about the error.
787 */
788 return_hosed_msg(smi_info, IPMI_ERR_UNSPECIFIED);
789 }
790 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
791 }
792
793 /*
794 * We prefer handling attn over new messages. But don't do
795 * this if there is not yet an upper layer to handle anything.
796 */
797 if (likely(smi_info->intf) && si_sm_result == SI_SM_ATTN) {
798 unsigned char msg[2];
799
800 smi_inc_stat(smi_info, attentions);
801
802 /*
803 * Got a attn, send down a get message flags to see
804 * what's causing it. It would be better to handle
805 * this in the upper layer, but due to the way
806 * interrupts work with the SMI, that's not really
807 * possible.
808 */
809 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
810 msg[1] = IPMI_GET_MSG_FLAGS_CMD;
811
812 smi_info->handlers->start_transaction(
813 smi_info->si_sm, msg, 2);
814 smi_info->si_state = SI_GETTING_FLAGS;
815 goto restart;
816 }
817
818 /* If we are currently idle, try to start the next message. */
819 if (si_sm_result == SI_SM_IDLE) {
820 smi_inc_stat(smi_info, idles);
821
822 si_sm_result = start_next_msg(smi_info);
823 if (si_sm_result != SI_SM_IDLE)
824 goto restart;
825 }
826
827 if ((si_sm_result == SI_SM_IDLE)
828 && (atomic_read(&smi_info->req_events))) {
829 /*
830 * We are idle and the upper layer requested that I fetch
831 * events, so do so.
832 */
833 atomic_set(&smi_info->req_events, 0);
834
835 smi_info->curr_msg = ipmi_alloc_smi_msg();
836 if (!smi_info->curr_msg)
837 goto out;
838
839 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
840 smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
841 smi_info->curr_msg->data_size = 2;
842
843 smi_info->handlers->start_transaction(
844 smi_info->si_sm,
845 smi_info->curr_msg->data,
846 smi_info->curr_msg->data_size);
847 smi_info->si_state = SI_GETTING_EVENTS;
848 goto restart;
849 }
850 out:
851 return si_sm_result;
852 }
853
sender(void * send_info,struct ipmi_smi_msg * msg,int priority)854 static void sender(void *send_info,
855 struct ipmi_smi_msg *msg,
856 int priority)
857 {
858 struct smi_info *smi_info = send_info;
859 enum si_sm_result result;
860 unsigned long flags;
861 #ifdef DEBUG_TIMING
862 struct timeval t;
863 #endif
864
865 if (atomic_read(&smi_info->stop_operation)) {
866 msg->rsp[0] = msg->data[0] | 4;
867 msg->rsp[1] = msg->data[1];
868 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
869 msg->rsp_size = 3;
870 deliver_recv_msg(smi_info, msg);
871 return;
872 }
873
874 #ifdef DEBUG_TIMING
875 do_gettimeofday(&t);
876 printk("**Enqueue: %d.%9.9d\n", t.tv_sec, t.tv_usec);
877 #endif
878
879 if (smi_info->run_to_completion) {
880 /*
881 * If we are running to completion, then throw it in
882 * the list and run transactions until everything is
883 * clear. Priority doesn't matter here.
884 */
885
886 /*
887 * Run to completion means we are single-threaded, no
888 * need for locks.
889 */
890 list_add_tail(&(msg->link), &(smi_info->xmit_msgs));
891
892 result = smi_event_handler(smi_info, 0);
893 while (result != SI_SM_IDLE) {
894 udelay(SI_SHORT_TIMEOUT_USEC);
895 result = smi_event_handler(smi_info,
896 SI_SHORT_TIMEOUT_USEC);
897 }
898 return;
899 }
900
901 spin_lock_irqsave(&smi_info->si_lock, flags);
902 if (priority > 0)
903 list_add_tail(&msg->link, &smi_info->hp_xmit_msgs);
904 else
905 list_add_tail(&msg->link, &smi_info->xmit_msgs);
906
907 if (smi_info->si_state == SI_NORMAL && smi_info->curr_msg == NULL) {
908 smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
909
910 if (smi_info->thread)
911 wake_up_process(smi_info->thread);
912
913 start_next_msg(smi_info);
914 smi_event_handler(smi_info, 0);
915 }
916 spin_unlock_irqrestore(&smi_info->si_lock, flags);
917 }
918
set_run_to_completion(void * send_info,int i_run_to_completion)919 static void set_run_to_completion(void *send_info, int i_run_to_completion)
920 {
921 struct smi_info *smi_info = send_info;
922 enum si_sm_result result;
923
924 smi_info->run_to_completion = i_run_to_completion;
925 if (i_run_to_completion) {
926 result = smi_event_handler(smi_info, 0);
927 while (result != SI_SM_IDLE) {
928 udelay(SI_SHORT_TIMEOUT_USEC);
929 result = smi_event_handler(smi_info,
930 SI_SHORT_TIMEOUT_USEC);
931 }
932 }
933 }
934
935 /*
936 * Use -1 in the nsec value of the busy waiting timespec to tell that
937 * we are spinning in kipmid looking for something and not delaying
938 * between checks
939 */
ipmi_si_set_not_busy(struct timespec * ts)940 static inline void ipmi_si_set_not_busy(struct timespec *ts)
941 {
942 ts->tv_nsec = -1;
943 }
ipmi_si_is_busy(struct timespec * ts)944 static inline int ipmi_si_is_busy(struct timespec *ts)
945 {
946 return ts->tv_nsec != -1;
947 }
948
ipmi_thread_busy_wait(enum si_sm_result smi_result,const struct smi_info * smi_info,struct timespec * busy_until)949 static int ipmi_thread_busy_wait(enum si_sm_result smi_result,
950 const struct smi_info *smi_info,
951 struct timespec *busy_until)
952 {
953 unsigned int max_busy_us = 0;
954
955 if (smi_info->intf_num < num_max_busy_us)
956 max_busy_us = kipmid_max_busy_us[smi_info->intf_num];
957 if (max_busy_us == 0 || smi_result != SI_SM_CALL_WITH_DELAY)
958 ipmi_si_set_not_busy(busy_until);
959 else if (!ipmi_si_is_busy(busy_until)) {
960 getnstimeofday(busy_until);
961 timespec_add_ns(busy_until, max_busy_us*NSEC_PER_USEC);
962 } else {
963 struct timespec now;
964 getnstimeofday(&now);
965 if (unlikely(timespec_compare(&now, busy_until) > 0)) {
966 ipmi_si_set_not_busy(busy_until);
967 return 0;
968 }
969 }
970 return 1;
971 }
972
973
974 /*
975 * A busy-waiting loop for speeding up IPMI operation.
976 *
977 * Lousy hardware makes this hard. This is only enabled for systems
978 * that are not BT and do not have interrupts. It starts spinning
979 * when an operation is complete or until max_busy tells it to stop
980 * (if that is enabled). See the paragraph on kimid_max_busy_us in
981 * Documentation/IPMI.txt for details.
982 */
ipmi_thread(void * data)983 static int ipmi_thread(void *data)
984 {
985 struct smi_info *smi_info = data;
986 unsigned long flags;
987 enum si_sm_result smi_result;
988 struct timespec busy_until;
989
990 ipmi_si_set_not_busy(&busy_until);
991 set_user_nice(current, 19);
992 while (!kthread_should_stop()) {
993 int busy_wait;
994
995 spin_lock_irqsave(&(smi_info->si_lock), flags);
996 smi_result = smi_event_handler(smi_info, 0);
997
998 /*
999 * If the driver is doing something, there is a possible
1000 * race with the timer. If the timer handler see idle,
1001 * and the thread here sees something else, the timer
1002 * handler won't restart the timer even though it is
1003 * required. So start it here if necessary.
1004 */
1005 if (smi_result != SI_SM_IDLE && !smi_info->timer_running)
1006 smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
1007
1008 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1009 busy_wait = ipmi_thread_busy_wait(smi_result, smi_info,
1010 &busy_until);
1011 if (smi_result == SI_SM_CALL_WITHOUT_DELAY)
1012 ; /* do nothing */
1013 else if (smi_result == SI_SM_CALL_WITH_DELAY && busy_wait)
1014 schedule();
1015 else if (smi_result == SI_SM_IDLE)
1016 schedule_timeout_interruptible(100);
1017 else
1018 schedule_timeout_interruptible(1);
1019 }
1020 return 0;
1021 }
1022
1023
poll(void * send_info)1024 static void poll(void *send_info)
1025 {
1026 struct smi_info *smi_info = send_info;
1027 unsigned long flags = 0;
1028 int run_to_completion = smi_info->run_to_completion;
1029
1030 /*
1031 * Make sure there is some delay in the poll loop so we can
1032 * drive time forward and timeout things.
1033 */
1034 udelay(10);
1035 if (!run_to_completion)
1036 spin_lock_irqsave(&smi_info->si_lock, flags);
1037 smi_event_handler(smi_info, 10);
1038 if (!run_to_completion)
1039 spin_unlock_irqrestore(&smi_info->si_lock, flags);
1040 }
1041
request_events(void * send_info)1042 static void request_events(void *send_info)
1043 {
1044 struct smi_info *smi_info = send_info;
1045
1046 if (atomic_read(&smi_info->stop_operation) ||
1047 !smi_info->has_event_buffer)
1048 return;
1049
1050 atomic_set(&smi_info->req_events, 1);
1051 }
1052
1053 static int initialized;
1054
smi_timeout(unsigned long data)1055 static void smi_timeout(unsigned long data)
1056 {
1057 struct smi_info *smi_info = (struct smi_info *) data;
1058 enum si_sm_result smi_result;
1059 unsigned long flags;
1060 unsigned long jiffies_now;
1061 long time_diff;
1062 long timeout;
1063 #ifdef DEBUG_TIMING
1064 struct timeval t;
1065 #endif
1066
1067 spin_lock_irqsave(&(smi_info->si_lock), flags);
1068 #ifdef DEBUG_TIMING
1069 do_gettimeofday(&t);
1070 printk(KERN_DEBUG "**Timer: %d.%9.9d\n", t.tv_sec, t.tv_usec);
1071 #endif
1072 jiffies_now = jiffies;
1073 time_diff = (((long)jiffies_now - (long)smi_info->last_timeout_jiffies)
1074 * SI_USEC_PER_JIFFY);
1075 smi_result = smi_event_handler(smi_info, time_diff);
1076
1077 if ((smi_info->irq) && (!smi_info->interrupt_disabled)) {
1078 /* Running with interrupts, only do long timeouts. */
1079 timeout = jiffies + SI_TIMEOUT_JIFFIES;
1080 smi_inc_stat(smi_info, long_timeouts);
1081 goto do_mod_timer;
1082 }
1083
1084 /*
1085 * If the state machine asks for a short delay, then shorten
1086 * the timer timeout.
1087 */
1088 if (smi_result == SI_SM_CALL_WITH_DELAY) {
1089 smi_inc_stat(smi_info, short_timeouts);
1090 timeout = jiffies + 1;
1091 } else {
1092 smi_inc_stat(smi_info, long_timeouts);
1093 timeout = jiffies + SI_TIMEOUT_JIFFIES;
1094 }
1095
1096 do_mod_timer:
1097 if (smi_result != SI_SM_IDLE)
1098 smi_mod_timer(smi_info, timeout);
1099 else
1100 smi_info->timer_running = false;
1101 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1102 }
1103
si_irq_handler(int irq,void * data)1104 static irqreturn_t si_irq_handler(int irq, void *data)
1105 {
1106 struct smi_info *smi_info = data;
1107 unsigned long flags;
1108 #ifdef DEBUG_TIMING
1109 struct timeval t;
1110 #endif
1111
1112 spin_lock_irqsave(&(smi_info->si_lock), flags);
1113
1114 smi_inc_stat(smi_info, interrupts);
1115
1116 #ifdef DEBUG_TIMING
1117 do_gettimeofday(&t);
1118 printk(KERN_DEBUG "**Interrupt: %d.%9.9d\n", t.tv_sec, t.tv_usec);
1119 #endif
1120 smi_event_handler(smi_info, 0);
1121 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1122 return IRQ_HANDLED;
1123 }
1124
si_bt_irq_handler(int irq,void * data)1125 static irqreturn_t si_bt_irq_handler(int irq, void *data)
1126 {
1127 struct smi_info *smi_info = data;
1128 /* We need to clear the IRQ flag for the BT interface. */
1129 smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
1130 IPMI_BT_INTMASK_CLEAR_IRQ_BIT
1131 | IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
1132 return si_irq_handler(irq, data);
1133 }
1134
smi_start_processing(void * send_info,ipmi_smi_t intf)1135 static int smi_start_processing(void *send_info,
1136 ipmi_smi_t intf)
1137 {
1138 struct smi_info *new_smi = send_info;
1139 int enable = 0;
1140
1141 new_smi->intf = intf;
1142
1143 /* Try to claim any interrupts. */
1144 if (new_smi->irq_setup)
1145 new_smi->irq_setup(new_smi);
1146
1147 /* Set up the timer that drives the interface. */
1148 setup_timer(&new_smi->si_timer, smi_timeout, (long)new_smi);
1149 smi_mod_timer(new_smi, jiffies + SI_TIMEOUT_JIFFIES);
1150
1151 /*
1152 * Check if the user forcefully enabled the daemon.
1153 */
1154 if (new_smi->intf_num < num_force_kipmid)
1155 enable = force_kipmid[new_smi->intf_num];
1156 /*
1157 * The BT interface is efficient enough to not need a thread,
1158 * and there is no need for a thread if we have interrupts.
1159 */
1160 else if ((new_smi->si_type != SI_BT) && (!new_smi->irq))
1161 enable = 1;
1162
1163 if (enable) {
1164 new_smi->thread = kthread_run(ipmi_thread, new_smi,
1165 "kipmi%d", new_smi->intf_num);
1166 if (IS_ERR(new_smi->thread)) {
1167 dev_notice(new_smi->dev, "Could not start"
1168 " kernel thread due to error %ld, only using"
1169 " timers to drive the interface\n",
1170 PTR_ERR(new_smi->thread));
1171 new_smi->thread = NULL;
1172 }
1173 }
1174
1175 return 0;
1176 }
1177
get_smi_info(void * send_info,struct ipmi_smi_info * data)1178 static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1179 {
1180 struct smi_info *smi = send_info;
1181
1182 data->addr_src = smi->addr_source;
1183 data->dev = smi->dev;
1184 data->addr_info = smi->addr_info;
1185 get_device(smi->dev);
1186
1187 return 0;
1188 }
1189
set_maintenance_mode(void * send_info,int enable)1190 static void set_maintenance_mode(void *send_info, int enable)
1191 {
1192 struct smi_info *smi_info = send_info;
1193
1194 if (!enable)
1195 atomic_set(&smi_info->req_events, 0);
1196 }
1197
1198 static struct ipmi_smi_handlers handlers = {
1199 .owner = THIS_MODULE,
1200 .start_processing = smi_start_processing,
1201 .get_smi_info = get_smi_info,
1202 .sender = sender,
1203 .request_events = request_events,
1204 .set_maintenance_mode = set_maintenance_mode,
1205 .set_run_to_completion = set_run_to_completion,
1206 .poll = poll,
1207 };
1208
1209 /*
1210 * There can be 4 IO ports passed in (with or without IRQs), 4 addresses,
1211 * a default IO port, and 1 ACPI/SPMI address. That sets SI_MAX_DRIVERS.
1212 */
1213
1214 static LIST_HEAD(smi_infos);
1215 static DEFINE_MUTEX(smi_infos_lock);
1216 static int smi_num; /* Used to sequence the SMIs */
1217
1218 #define DEFAULT_REGSPACING 1
1219 #define DEFAULT_REGSIZE 1
1220
1221 static bool si_trydefaults = 1;
1222 static char *si_type[SI_MAX_PARMS];
1223 #define MAX_SI_TYPE_STR 30
1224 static char si_type_str[MAX_SI_TYPE_STR];
1225 static unsigned long addrs[SI_MAX_PARMS];
1226 static unsigned int num_addrs;
1227 static unsigned int ports[SI_MAX_PARMS];
1228 static unsigned int num_ports;
1229 static int irqs[SI_MAX_PARMS];
1230 static unsigned int num_irqs;
1231 static int regspacings[SI_MAX_PARMS];
1232 static unsigned int num_regspacings;
1233 static int regsizes[SI_MAX_PARMS];
1234 static unsigned int num_regsizes;
1235 static int regshifts[SI_MAX_PARMS];
1236 static unsigned int num_regshifts;
1237 static int slave_addrs[SI_MAX_PARMS]; /* Leaving 0 chooses the default value */
1238 static unsigned int num_slave_addrs;
1239
1240 #define IPMI_IO_ADDR_SPACE 0
1241 #define IPMI_MEM_ADDR_SPACE 1
1242 static char *addr_space_to_str[] = { "i/o", "mem" };
1243
1244 static int hotmod_handler(const char *val, struct kernel_param *kp);
1245
1246 module_param_call(hotmod, hotmod_handler, NULL, NULL, 0200);
1247 MODULE_PARM_DESC(hotmod, "Add and remove interfaces. See"
1248 " Documentation/IPMI.txt in the kernel sources for the"
1249 " gory details.");
1250
1251 module_param_named(trydefaults, si_trydefaults, bool, 0);
1252 MODULE_PARM_DESC(trydefaults, "Setting this to 'false' will disable the"
1253 " default scan of the KCS and SMIC interface at the standard"
1254 " address");
1255 module_param_string(type, si_type_str, MAX_SI_TYPE_STR, 0);
1256 MODULE_PARM_DESC(type, "Defines the type of each interface, each"
1257 " interface separated by commas. The types are 'kcs',"
1258 " 'smic', and 'bt'. For example si_type=kcs,bt will set"
1259 " the first interface to kcs and the second to bt");
1260 module_param_array(addrs, ulong, &num_addrs, 0);
1261 MODULE_PARM_DESC(addrs, "Sets the memory address of each interface, the"
1262 " addresses separated by commas. Only use if an interface"
1263 " is in memory. Otherwise, set it to zero or leave"
1264 " it blank.");
1265 module_param_array(ports, uint, &num_ports, 0);
1266 MODULE_PARM_DESC(ports, "Sets the port address of each interface, the"
1267 " addresses separated by commas. Only use if an interface"
1268 " is a port. Otherwise, set it to zero or leave"
1269 " it blank.");
1270 module_param_array(irqs, int, &num_irqs, 0);
1271 MODULE_PARM_DESC(irqs, "Sets the interrupt of each interface, the"
1272 " addresses separated by commas. Only use if an interface"
1273 " has an interrupt. Otherwise, set it to zero or leave"
1274 " it blank.");
1275 module_param_array(regspacings, int, &num_regspacings, 0);
1276 MODULE_PARM_DESC(regspacings, "The number of bytes between the start address"
1277 " and each successive register used by the interface. For"
1278 " instance, if the start address is 0xca2 and the spacing"
1279 " is 2, then the second address is at 0xca4. Defaults"
1280 " to 1.");
1281 module_param_array(regsizes, int, &num_regsizes, 0);
1282 MODULE_PARM_DESC(regsizes, "The size of the specific IPMI register in bytes."
1283 " This should generally be 1, 2, 4, or 8 for an 8-bit,"
1284 " 16-bit, 32-bit, or 64-bit register. Use this if you"
1285 " the 8-bit IPMI register has to be read from a larger"
1286 " register.");
1287 module_param_array(regshifts, int, &num_regshifts, 0);
1288 MODULE_PARM_DESC(regshifts, "The amount to shift the data read from the."
1289 " IPMI register, in bits. For instance, if the data"
1290 " is read from a 32-bit word and the IPMI data is in"
1291 " bit 8-15, then the shift would be 8");
1292 module_param_array(slave_addrs, int, &num_slave_addrs, 0);
1293 MODULE_PARM_DESC(slave_addrs, "Set the default IPMB slave address for"
1294 " the controller. Normally this is 0x20, but can be"
1295 " overridden by this parm. This is an array indexed"
1296 " by interface number.");
1297 module_param_array(force_kipmid, int, &num_force_kipmid, 0);
1298 MODULE_PARM_DESC(force_kipmid, "Force the kipmi daemon to be enabled (1) or"
1299 " disabled(0). Normally the IPMI driver auto-detects"
1300 " this, but the value may be overridden by this parm.");
1301 module_param(unload_when_empty, int, 0);
1302 MODULE_PARM_DESC(unload_when_empty, "Unload the module if no interfaces are"
1303 " specified or found, default is 1. Setting to 0"
1304 " is useful for hot add of devices using hotmod.");
1305 module_param_array(kipmid_max_busy_us, uint, &num_max_busy_us, 0644);
1306 MODULE_PARM_DESC(kipmid_max_busy_us,
1307 "Max time (in microseconds) to busy-wait for IPMI data before"
1308 " sleeping. 0 (default) means to wait forever. Set to 100-500"
1309 " if kipmid is using up a lot of CPU time.");
1310
1311
std_irq_cleanup(struct smi_info * info)1312 static void std_irq_cleanup(struct smi_info *info)
1313 {
1314 if (info->si_type == SI_BT)
1315 /* Disable the interrupt in the BT interface. */
1316 info->io.outputb(&info->io, IPMI_BT_INTMASK_REG, 0);
1317 free_irq(info->irq, info);
1318 }
1319
std_irq_setup(struct smi_info * info)1320 static int std_irq_setup(struct smi_info *info)
1321 {
1322 int rv;
1323
1324 if (!info->irq)
1325 return 0;
1326
1327 if (info->si_type == SI_BT) {
1328 rv = request_irq(info->irq,
1329 si_bt_irq_handler,
1330 IRQF_SHARED | IRQF_DISABLED,
1331 DEVICE_NAME,
1332 info);
1333 if (!rv)
1334 /* Enable the interrupt in the BT interface. */
1335 info->io.outputb(&info->io, IPMI_BT_INTMASK_REG,
1336 IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
1337 } else
1338 rv = request_irq(info->irq,
1339 si_irq_handler,
1340 IRQF_SHARED | IRQF_DISABLED,
1341 DEVICE_NAME,
1342 info);
1343 if (rv) {
1344 dev_warn(info->dev, "%s unable to claim interrupt %d,"
1345 " running polled\n",
1346 DEVICE_NAME, info->irq);
1347 info->irq = 0;
1348 } else {
1349 info->irq_cleanup = std_irq_cleanup;
1350 dev_info(info->dev, "Using irq %d\n", info->irq);
1351 }
1352
1353 return rv;
1354 }
1355
port_inb(struct si_sm_io * io,unsigned int offset)1356 static unsigned char port_inb(struct si_sm_io *io, unsigned int offset)
1357 {
1358 unsigned int addr = io->addr_data;
1359
1360 return inb(addr + (offset * io->regspacing));
1361 }
1362
port_outb(struct si_sm_io * io,unsigned int offset,unsigned char b)1363 static void port_outb(struct si_sm_io *io, unsigned int offset,
1364 unsigned char b)
1365 {
1366 unsigned int addr = io->addr_data;
1367
1368 outb(b, addr + (offset * io->regspacing));
1369 }
1370
port_inw(struct si_sm_io * io,unsigned int offset)1371 static unsigned char port_inw(struct si_sm_io *io, unsigned int offset)
1372 {
1373 unsigned int addr = io->addr_data;
1374
1375 return (inw(addr + (offset * io->regspacing)) >> io->regshift) & 0xff;
1376 }
1377
port_outw(struct si_sm_io * io,unsigned int offset,unsigned char b)1378 static void port_outw(struct si_sm_io *io, unsigned int offset,
1379 unsigned char b)
1380 {
1381 unsigned int addr = io->addr_data;
1382
1383 outw(b << io->regshift, addr + (offset * io->regspacing));
1384 }
1385
port_inl(struct si_sm_io * io,unsigned int offset)1386 static unsigned char port_inl(struct si_sm_io *io, unsigned int offset)
1387 {
1388 unsigned int addr = io->addr_data;
1389
1390 return (inl(addr + (offset * io->regspacing)) >> io->regshift) & 0xff;
1391 }
1392
port_outl(struct si_sm_io * io,unsigned int offset,unsigned char b)1393 static void port_outl(struct si_sm_io *io, unsigned int offset,
1394 unsigned char b)
1395 {
1396 unsigned int addr = io->addr_data;
1397
1398 outl(b << io->regshift, addr+(offset * io->regspacing));
1399 }
1400
port_cleanup(struct smi_info * info)1401 static void port_cleanup(struct smi_info *info)
1402 {
1403 unsigned int addr = info->io.addr_data;
1404 int idx;
1405
1406 if (addr) {
1407 for (idx = 0; idx < info->io_size; idx++)
1408 release_region(addr + idx * info->io.regspacing,
1409 info->io.regsize);
1410 }
1411 }
1412
port_setup(struct smi_info * info)1413 static int port_setup(struct smi_info *info)
1414 {
1415 unsigned int addr = info->io.addr_data;
1416 int idx;
1417
1418 if (!addr)
1419 return -ENODEV;
1420
1421 info->io_cleanup = port_cleanup;
1422
1423 /*
1424 * Figure out the actual inb/inw/inl/etc routine to use based
1425 * upon the register size.
1426 */
1427 switch (info->io.regsize) {
1428 case 1:
1429 info->io.inputb = port_inb;
1430 info->io.outputb = port_outb;
1431 break;
1432 case 2:
1433 info->io.inputb = port_inw;
1434 info->io.outputb = port_outw;
1435 break;
1436 case 4:
1437 info->io.inputb = port_inl;
1438 info->io.outputb = port_outl;
1439 break;
1440 default:
1441 dev_warn(info->dev, "Invalid register size: %d\n",
1442 info->io.regsize);
1443 return -EINVAL;
1444 }
1445
1446 /*
1447 * Some BIOSes reserve disjoint I/O regions in their ACPI
1448 * tables. This causes problems when trying to register the
1449 * entire I/O region. Therefore we must register each I/O
1450 * port separately.
1451 */
1452 for (idx = 0; idx < info->io_size; idx++) {
1453 if (request_region(addr + idx * info->io.regspacing,
1454 info->io.regsize, DEVICE_NAME) == NULL) {
1455 /* Undo allocations */
1456 while (idx--) {
1457 release_region(addr + idx * info->io.regspacing,
1458 info->io.regsize);
1459 }
1460 return -EIO;
1461 }
1462 }
1463 return 0;
1464 }
1465
intf_mem_inb(struct si_sm_io * io,unsigned int offset)1466 static unsigned char intf_mem_inb(struct si_sm_io *io, unsigned int offset)
1467 {
1468 return readb((io->addr)+(offset * io->regspacing));
1469 }
1470
intf_mem_outb(struct si_sm_io * io,unsigned int offset,unsigned char b)1471 static void intf_mem_outb(struct si_sm_io *io, unsigned int offset,
1472 unsigned char b)
1473 {
1474 writeb(b, (io->addr)+(offset * io->regspacing));
1475 }
1476
intf_mem_inw(struct si_sm_io * io,unsigned int offset)1477 static unsigned char intf_mem_inw(struct si_sm_io *io, unsigned int offset)
1478 {
1479 return (readw((io->addr)+(offset * io->regspacing)) >> io->regshift)
1480 & 0xff;
1481 }
1482
intf_mem_outw(struct si_sm_io * io,unsigned int offset,unsigned char b)1483 static void intf_mem_outw(struct si_sm_io *io, unsigned int offset,
1484 unsigned char b)
1485 {
1486 writeb(b << io->regshift, (io->addr)+(offset * io->regspacing));
1487 }
1488
intf_mem_inl(struct si_sm_io * io,unsigned int offset)1489 static unsigned char intf_mem_inl(struct si_sm_io *io, unsigned int offset)
1490 {
1491 return (readl((io->addr)+(offset * io->regspacing)) >> io->regshift)
1492 & 0xff;
1493 }
1494
intf_mem_outl(struct si_sm_io * io,unsigned int offset,unsigned char b)1495 static void intf_mem_outl(struct si_sm_io *io, unsigned int offset,
1496 unsigned char b)
1497 {
1498 writel(b << io->regshift, (io->addr)+(offset * io->regspacing));
1499 }
1500
1501 #ifdef readq
mem_inq(struct si_sm_io * io,unsigned int offset)1502 static unsigned char mem_inq(struct si_sm_io *io, unsigned int offset)
1503 {
1504 return (readq((io->addr)+(offset * io->regspacing)) >> io->regshift)
1505 & 0xff;
1506 }
1507
mem_outq(struct si_sm_io * io,unsigned int offset,unsigned char b)1508 static void mem_outq(struct si_sm_io *io, unsigned int offset,
1509 unsigned char b)
1510 {
1511 writeq(b << io->regshift, (io->addr)+(offset * io->regspacing));
1512 }
1513 #endif
1514
mem_cleanup(struct smi_info * info)1515 static void mem_cleanup(struct smi_info *info)
1516 {
1517 unsigned long addr = info->io.addr_data;
1518 int mapsize;
1519
1520 if (info->io.addr) {
1521 iounmap(info->io.addr);
1522
1523 mapsize = ((info->io_size * info->io.regspacing)
1524 - (info->io.regspacing - info->io.regsize));
1525
1526 release_mem_region(addr, mapsize);
1527 }
1528 }
1529
mem_setup(struct smi_info * info)1530 static int mem_setup(struct smi_info *info)
1531 {
1532 unsigned long addr = info->io.addr_data;
1533 int mapsize;
1534
1535 if (!addr)
1536 return -ENODEV;
1537
1538 info->io_cleanup = mem_cleanup;
1539
1540 /*
1541 * Figure out the actual readb/readw/readl/etc routine to use based
1542 * upon the register size.
1543 */
1544 switch (info->io.regsize) {
1545 case 1:
1546 info->io.inputb = intf_mem_inb;
1547 info->io.outputb = intf_mem_outb;
1548 break;
1549 case 2:
1550 info->io.inputb = intf_mem_inw;
1551 info->io.outputb = intf_mem_outw;
1552 break;
1553 case 4:
1554 info->io.inputb = intf_mem_inl;
1555 info->io.outputb = intf_mem_outl;
1556 break;
1557 #ifdef readq
1558 case 8:
1559 info->io.inputb = mem_inq;
1560 info->io.outputb = mem_outq;
1561 break;
1562 #endif
1563 default:
1564 dev_warn(info->dev, "Invalid register size: %d\n",
1565 info->io.regsize);
1566 return -EINVAL;
1567 }
1568
1569 /*
1570 * Calculate the total amount of memory to claim. This is an
1571 * unusual looking calculation, but it avoids claiming any
1572 * more memory than it has to. It will claim everything
1573 * between the first address to the end of the last full
1574 * register.
1575 */
1576 mapsize = ((info->io_size * info->io.regspacing)
1577 - (info->io.regspacing - info->io.regsize));
1578
1579 if (request_mem_region(addr, mapsize, DEVICE_NAME) == NULL)
1580 return -EIO;
1581
1582 info->io.addr = ioremap(addr, mapsize);
1583 if (info->io.addr == NULL) {
1584 release_mem_region(addr, mapsize);
1585 return -EIO;
1586 }
1587 return 0;
1588 }
1589
1590 /*
1591 * Parms come in as <op1>[:op2[:op3...]]. ops are:
1592 * add|remove,kcs|bt|smic,mem|i/o,<address>[,<opt1>[,<opt2>[,...]]]
1593 * Options are:
1594 * rsp=<regspacing>
1595 * rsi=<regsize>
1596 * rsh=<regshift>
1597 * irq=<irq>
1598 * ipmb=<ipmb addr>
1599 */
1600 enum hotmod_op { HM_ADD, HM_REMOVE };
1601 struct hotmod_vals {
1602 char *name;
1603 int val;
1604 };
1605 static struct hotmod_vals hotmod_ops[] = {
1606 { "add", HM_ADD },
1607 { "remove", HM_REMOVE },
1608 { NULL }
1609 };
1610 static struct hotmod_vals hotmod_si[] = {
1611 { "kcs", SI_KCS },
1612 { "smic", SI_SMIC },
1613 { "bt", SI_BT },
1614 { NULL }
1615 };
1616 static struct hotmod_vals hotmod_as[] = {
1617 { "mem", IPMI_MEM_ADDR_SPACE },
1618 { "i/o", IPMI_IO_ADDR_SPACE },
1619 { NULL }
1620 };
1621
parse_str(struct hotmod_vals * v,int * val,char * name,char ** curr)1622 static int parse_str(struct hotmod_vals *v, int *val, char *name, char **curr)
1623 {
1624 char *s;
1625 int i;
1626
1627 s = strchr(*curr, ',');
1628 if (!s) {
1629 printk(KERN_WARNING PFX "No hotmod %s given.\n", name);
1630 return -EINVAL;
1631 }
1632 *s = '\0';
1633 s++;
1634 for (i = 0; hotmod_ops[i].name; i++) {
1635 if (strcmp(*curr, v[i].name) == 0) {
1636 *val = v[i].val;
1637 *curr = s;
1638 return 0;
1639 }
1640 }
1641
1642 printk(KERN_WARNING PFX "Invalid hotmod %s '%s'\n", name, *curr);
1643 return -EINVAL;
1644 }
1645
check_hotmod_int_op(const char * curr,const char * option,const char * name,int * val)1646 static int check_hotmod_int_op(const char *curr, const char *option,
1647 const char *name, int *val)
1648 {
1649 char *n;
1650
1651 if (strcmp(curr, name) == 0) {
1652 if (!option) {
1653 printk(KERN_WARNING PFX
1654 "No option given for '%s'\n",
1655 curr);
1656 return -EINVAL;
1657 }
1658 *val = simple_strtoul(option, &n, 0);
1659 if ((*n != '\0') || (*option == '\0')) {
1660 printk(KERN_WARNING PFX
1661 "Bad option given for '%s'\n",
1662 curr);
1663 return -EINVAL;
1664 }
1665 return 1;
1666 }
1667 return 0;
1668 }
1669
smi_info_alloc(void)1670 static struct smi_info *smi_info_alloc(void)
1671 {
1672 struct smi_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
1673
1674 if (info)
1675 spin_lock_init(&info->si_lock);
1676 return info;
1677 }
1678
hotmod_handler(const char * val,struct kernel_param * kp)1679 static int hotmod_handler(const char *val, struct kernel_param *kp)
1680 {
1681 char *str = kstrdup(val, GFP_KERNEL);
1682 int rv;
1683 char *next, *curr, *s, *n, *o;
1684 enum hotmod_op op;
1685 enum si_type si_type;
1686 int addr_space;
1687 unsigned long addr;
1688 int regspacing;
1689 int regsize;
1690 int regshift;
1691 int irq;
1692 int ipmb;
1693 int ival;
1694 int len;
1695 struct smi_info *info;
1696
1697 if (!str)
1698 return -ENOMEM;
1699
1700 /* Kill any trailing spaces, as we can get a "\n" from echo. */
1701 len = strlen(str);
1702 ival = len - 1;
1703 while ((ival >= 0) && isspace(str[ival])) {
1704 str[ival] = '\0';
1705 ival--;
1706 }
1707
1708 for (curr = str; curr; curr = next) {
1709 regspacing = 1;
1710 regsize = 1;
1711 regshift = 0;
1712 irq = 0;
1713 ipmb = 0; /* Choose the default if not specified */
1714
1715 next = strchr(curr, ':');
1716 if (next) {
1717 *next = '\0';
1718 next++;
1719 }
1720
1721 rv = parse_str(hotmod_ops, &ival, "operation", &curr);
1722 if (rv)
1723 break;
1724 op = ival;
1725
1726 rv = parse_str(hotmod_si, &ival, "interface type", &curr);
1727 if (rv)
1728 break;
1729 si_type = ival;
1730
1731 rv = parse_str(hotmod_as, &addr_space, "address space", &curr);
1732 if (rv)
1733 break;
1734
1735 s = strchr(curr, ',');
1736 if (s) {
1737 *s = '\0';
1738 s++;
1739 }
1740 addr = simple_strtoul(curr, &n, 0);
1741 if ((*n != '\0') || (*curr == '\0')) {
1742 printk(KERN_WARNING PFX "Invalid hotmod address"
1743 " '%s'\n", curr);
1744 break;
1745 }
1746
1747 while (s) {
1748 curr = s;
1749 s = strchr(curr, ',');
1750 if (s) {
1751 *s = '\0';
1752 s++;
1753 }
1754 o = strchr(curr, '=');
1755 if (o) {
1756 *o = '\0';
1757 o++;
1758 }
1759 rv = check_hotmod_int_op(curr, o, "rsp", ®spacing);
1760 if (rv < 0)
1761 goto out;
1762 else if (rv)
1763 continue;
1764 rv = check_hotmod_int_op(curr, o, "rsi", ®size);
1765 if (rv < 0)
1766 goto out;
1767 else if (rv)
1768 continue;
1769 rv = check_hotmod_int_op(curr, o, "rsh", ®shift);
1770 if (rv < 0)
1771 goto out;
1772 else if (rv)
1773 continue;
1774 rv = check_hotmod_int_op(curr, o, "irq", &irq);
1775 if (rv < 0)
1776 goto out;
1777 else if (rv)
1778 continue;
1779 rv = check_hotmod_int_op(curr, o, "ipmb", &ipmb);
1780 if (rv < 0)
1781 goto out;
1782 else if (rv)
1783 continue;
1784
1785 rv = -EINVAL;
1786 printk(KERN_WARNING PFX
1787 "Invalid hotmod option '%s'\n",
1788 curr);
1789 goto out;
1790 }
1791
1792 if (op == HM_ADD) {
1793 info = smi_info_alloc();
1794 if (!info) {
1795 rv = -ENOMEM;
1796 goto out;
1797 }
1798
1799 info->addr_source = SI_HOTMOD;
1800 info->si_type = si_type;
1801 info->io.addr_data = addr;
1802 info->io.addr_type = addr_space;
1803 if (addr_space == IPMI_MEM_ADDR_SPACE)
1804 info->io_setup = mem_setup;
1805 else
1806 info->io_setup = port_setup;
1807
1808 info->io.addr = NULL;
1809 info->io.regspacing = regspacing;
1810 if (!info->io.regspacing)
1811 info->io.regspacing = DEFAULT_REGSPACING;
1812 info->io.regsize = regsize;
1813 if (!info->io.regsize)
1814 info->io.regsize = DEFAULT_REGSPACING;
1815 info->io.regshift = regshift;
1816 info->irq = irq;
1817 if (info->irq)
1818 info->irq_setup = std_irq_setup;
1819 info->slave_addr = ipmb;
1820
1821 if (!add_smi(info)) {
1822 if (try_smi_init(info))
1823 cleanup_one_si(info);
1824 } else {
1825 kfree(info);
1826 }
1827 } else {
1828 /* remove */
1829 struct smi_info *e, *tmp_e;
1830
1831 mutex_lock(&smi_infos_lock);
1832 list_for_each_entry_safe(e, tmp_e, &smi_infos, link) {
1833 if (e->io.addr_type != addr_space)
1834 continue;
1835 if (e->si_type != si_type)
1836 continue;
1837 if (e->io.addr_data == addr)
1838 cleanup_one_si(e);
1839 }
1840 mutex_unlock(&smi_infos_lock);
1841 }
1842 }
1843 rv = len;
1844 out:
1845 kfree(str);
1846 return rv;
1847 }
1848
hardcode_find_bmc(void)1849 static int __devinit hardcode_find_bmc(void)
1850 {
1851 int ret = -ENODEV;
1852 int i;
1853 struct smi_info *info;
1854
1855 for (i = 0; i < SI_MAX_PARMS; i++) {
1856 if (!ports[i] && !addrs[i])
1857 continue;
1858
1859 info = smi_info_alloc();
1860 if (!info)
1861 return -ENOMEM;
1862
1863 info->addr_source = SI_HARDCODED;
1864 printk(KERN_INFO PFX "probing via hardcoded address\n");
1865
1866 if (!si_type[i] || strcmp(si_type[i], "kcs") == 0) {
1867 info->si_type = SI_KCS;
1868 } else if (strcmp(si_type[i], "smic") == 0) {
1869 info->si_type = SI_SMIC;
1870 } else if (strcmp(si_type[i], "bt") == 0) {
1871 info->si_type = SI_BT;
1872 } else {
1873 printk(KERN_WARNING PFX "Interface type specified "
1874 "for interface %d, was invalid: %s\n",
1875 i, si_type[i]);
1876 kfree(info);
1877 continue;
1878 }
1879
1880 if (ports[i]) {
1881 /* An I/O port */
1882 info->io_setup = port_setup;
1883 info->io.addr_data = ports[i];
1884 info->io.addr_type = IPMI_IO_ADDR_SPACE;
1885 } else if (addrs[i]) {
1886 /* A memory port */
1887 info->io_setup = mem_setup;
1888 info->io.addr_data = addrs[i];
1889 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
1890 } else {
1891 printk(KERN_WARNING PFX "Interface type specified "
1892 "for interface %d, but port and address were "
1893 "not set or set to zero.\n", i);
1894 kfree(info);
1895 continue;
1896 }
1897
1898 info->io.addr = NULL;
1899 info->io.regspacing = regspacings[i];
1900 if (!info->io.regspacing)
1901 info->io.regspacing = DEFAULT_REGSPACING;
1902 info->io.regsize = regsizes[i];
1903 if (!info->io.regsize)
1904 info->io.regsize = DEFAULT_REGSPACING;
1905 info->io.regshift = regshifts[i];
1906 info->irq = irqs[i];
1907 if (info->irq)
1908 info->irq_setup = std_irq_setup;
1909 info->slave_addr = slave_addrs[i];
1910
1911 if (!add_smi(info)) {
1912 if (try_smi_init(info))
1913 cleanup_one_si(info);
1914 ret = 0;
1915 } else {
1916 kfree(info);
1917 }
1918 }
1919 return ret;
1920 }
1921
1922 #ifdef CONFIG_ACPI
1923
1924 #include <linux/acpi.h>
1925
1926 /*
1927 * Once we get an ACPI failure, we don't try any more, because we go
1928 * through the tables sequentially. Once we don't find a table, there
1929 * are no more.
1930 */
1931 static int acpi_failure;
1932
1933 /* For GPE-type interrupts. */
ipmi_acpi_gpe(acpi_handle gpe_device,u32 gpe_number,void * context)1934 static u32 ipmi_acpi_gpe(acpi_handle gpe_device,
1935 u32 gpe_number, void *context)
1936 {
1937 struct smi_info *smi_info = context;
1938 unsigned long flags;
1939 #ifdef DEBUG_TIMING
1940 struct timeval t;
1941 #endif
1942
1943 spin_lock_irqsave(&(smi_info->si_lock), flags);
1944
1945 smi_inc_stat(smi_info, interrupts);
1946
1947 #ifdef DEBUG_TIMING
1948 do_gettimeofday(&t);
1949 printk("**ACPI_GPE: %d.%9.9d\n", t.tv_sec, t.tv_usec);
1950 #endif
1951 smi_event_handler(smi_info, 0);
1952 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1953
1954 return ACPI_INTERRUPT_HANDLED;
1955 }
1956
acpi_gpe_irq_cleanup(struct smi_info * info)1957 static void acpi_gpe_irq_cleanup(struct smi_info *info)
1958 {
1959 if (!info->irq)
1960 return;
1961
1962 acpi_remove_gpe_handler(NULL, info->irq, &ipmi_acpi_gpe);
1963 }
1964
acpi_gpe_irq_setup(struct smi_info * info)1965 static int acpi_gpe_irq_setup(struct smi_info *info)
1966 {
1967 acpi_status status;
1968
1969 if (!info->irq)
1970 return 0;
1971
1972 /* FIXME - is level triggered right? */
1973 status = acpi_install_gpe_handler(NULL,
1974 info->irq,
1975 ACPI_GPE_LEVEL_TRIGGERED,
1976 &ipmi_acpi_gpe,
1977 info);
1978 if (status != AE_OK) {
1979 dev_warn(info->dev, "%s unable to claim ACPI GPE %d,"
1980 " running polled\n", DEVICE_NAME, info->irq);
1981 info->irq = 0;
1982 return -EINVAL;
1983 } else {
1984 info->irq_cleanup = acpi_gpe_irq_cleanup;
1985 dev_info(info->dev, "Using ACPI GPE %d\n", info->irq);
1986 return 0;
1987 }
1988 }
1989
1990 /*
1991 * Defined at
1992 * http://h21007.www2.hp.com/portal/download/files/unprot/hpspmi.pdf
1993 */
1994 struct SPMITable {
1995 s8 Signature[4];
1996 u32 Length;
1997 u8 Revision;
1998 u8 Checksum;
1999 s8 OEMID[6];
2000 s8 OEMTableID[8];
2001 s8 OEMRevision[4];
2002 s8 CreatorID[4];
2003 s8 CreatorRevision[4];
2004 u8 InterfaceType;
2005 u8 IPMIlegacy;
2006 s16 SpecificationRevision;
2007
2008 /*
2009 * Bit 0 - SCI interrupt supported
2010 * Bit 1 - I/O APIC/SAPIC
2011 */
2012 u8 InterruptType;
2013
2014 /*
2015 * If bit 0 of InterruptType is set, then this is the SCI
2016 * interrupt in the GPEx_STS register.
2017 */
2018 u8 GPE;
2019
2020 s16 Reserved;
2021
2022 /*
2023 * If bit 1 of InterruptType is set, then this is the I/O
2024 * APIC/SAPIC interrupt.
2025 */
2026 u32 GlobalSystemInterrupt;
2027
2028 /* The actual register address. */
2029 struct acpi_generic_address addr;
2030
2031 u8 UID[4];
2032
2033 s8 spmi_id[1]; /* A '\0' terminated array starts here. */
2034 };
2035
try_init_spmi(struct SPMITable * spmi)2036 static int __devinit try_init_spmi(struct SPMITable *spmi)
2037 {
2038 struct smi_info *info;
2039
2040 if (spmi->IPMIlegacy != 1) {
2041 printk(KERN_INFO PFX "Bad SPMI legacy %d\n", spmi->IPMIlegacy);
2042 return -ENODEV;
2043 }
2044
2045 info = smi_info_alloc();
2046 if (!info) {
2047 printk(KERN_ERR PFX "Could not allocate SI data (3)\n");
2048 return -ENOMEM;
2049 }
2050
2051 info->addr_source = SI_SPMI;
2052 printk(KERN_INFO PFX "probing via SPMI\n");
2053
2054 /* Figure out the interface type. */
2055 switch (spmi->InterfaceType) {
2056 case 1: /* KCS */
2057 info->si_type = SI_KCS;
2058 break;
2059 case 2: /* SMIC */
2060 info->si_type = SI_SMIC;
2061 break;
2062 case 3: /* BT */
2063 info->si_type = SI_BT;
2064 break;
2065 default:
2066 printk(KERN_INFO PFX "Unknown ACPI/SPMI SI type %d\n",
2067 spmi->InterfaceType);
2068 kfree(info);
2069 return -EIO;
2070 }
2071
2072 if (spmi->InterruptType & 1) {
2073 /* We've got a GPE interrupt. */
2074 info->irq = spmi->GPE;
2075 info->irq_setup = acpi_gpe_irq_setup;
2076 } else if (spmi->InterruptType & 2) {
2077 /* We've got an APIC/SAPIC interrupt. */
2078 info->irq = spmi->GlobalSystemInterrupt;
2079 info->irq_setup = std_irq_setup;
2080 } else {
2081 /* Use the default interrupt setting. */
2082 info->irq = 0;
2083 info->irq_setup = NULL;
2084 }
2085
2086 if (spmi->addr.bit_width) {
2087 /* A (hopefully) properly formed register bit width. */
2088 info->io.regspacing = spmi->addr.bit_width / 8;
2089 } else {
2090 info->io.regspacing = DEFAULT_REGSPACING;
2091 }
2092 info->io.regsize = info->io.regspacing;
2093 info->io.regshift = spmi->addr.bit_offset;
2094
2095 if (spmi->addr.space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
2096 info->io_setup = mem_setup;
2097 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2098 } else if (spmi->addr.space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
2099 info->io_setup = port_setup;
2100 info->io.addr_type = IPMI_IO_ADDR_SPACE;
2101 } else {
2102 kfree(info);
2103 printk(KERN_WARNING PFX "Unknown ACPI I/O Address type\n");
2104 return -EIO;
2105 }
2106 info->io.addr_data = spmi->addr.address;
2107
2108 pr_info("ipmi_si: SPMI: %s %#lx regsize %d spacing %d irq %d\n",
2109 (info->io.addr_type == IPMI_IO_ADDR_SPACE) ? "io" : "mem",
2110 info->io.addr_data, info->io.regsize, info->io.regspacing,
2111 info->irq);
2112
2113 if (add_smi(info))
2114 kfree(info);
2115
2116 return 0;
2117 }
2118
spmi_find_bmc(void)2119 static void __devinit spmi_find_bmc(void)
2120 {
2121 acpi_status status;
2122 struct SPMITable *spmi;
2123 int i;
2124
2125 if (acpi_disabled)
2126 return;
2127
2128 if (acpi_failure)
2129 return;
2130
2131 for (i = 0; ; i++) {
2132 status = acpi_get_table(ACPI_SIG_SPMI, i+1,
2133 (struct acpi_table_header **)&spmi);
2134 if (status != AE_OK)
2135 return;
2136
2137 try_init_spmi(spmi);
2138 }
2139 }
2140
ipmi_pnp_probe(struct pnp_dev * dev,const struct pnp_device_id * dev_id)2141 static int __devinit ipmi_pnp_probe(struct pnp_dev *dev,
2142 const struct pnp_device_id *dev_id)
2143 {
2144 struct acpi_device *acpi_dev;
2145 struct smi_info *info;
2146 struct resource *res, *res_second;
2147 acpi_handle handle;
2148 acpi_status status;
2149 unsigned long long tmp;
2150
2151 acpi_dev = pnp_acpi_device(dev);
2152 if (!acpi_dev)
2153 return -ENODEV;
2154
2155 info = smi_info_alloc();
2156 if (!info)
2157 return -ENOMEM;
2158
2159 info->addr_source = SI_ACPI;
2160 printk(KERN_INFO PFX "probing via ACPI\n");
2161
2162 handle = acpi_dev->handle;
2163 info->addr_info.acpi_info.acpi_handle = handle;
2164
2165 /* _IFT tells us the interface type: KCS, BT, etc */
2166 status = acpi_evaluate_integer(handle, "_IFT", NULL, &tmp);
2167 if (ACPI_FAILURE(status))
2168 goto err_free;
2169
2170 switch (tmp) {
2171 case 1:
2172 info->si_type = SI_KCS;
2173 break;
2174 case 2:
2175 info->si_type = SI_SMIC;
2176 break;
2177 case 3:
2178 info->si_type = SI_BT;
2179 break;
2180 default:
2181 dev_info(&dev->dev, "unknown IPMI type %lld\n", tmp);
2182 goto err_free;
2183 }
2184
2185 res = pnp_get_resource(dev, IORESOURCE_IO, 0);
2186 if (res) {
2187 info->io_setup = port_setup;
2188 info->io.addr_type = IPMI_IO_ADDR_SPACE;
2189 } else {
2190 res = pnp_get_resource(dev, IORESOURCE_MEM, 0);
2191 if (res) {
2192 info->io_setup = mem_setup;
2193 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2194 }
2195 }
2196 if (!res) {
2197 dev_err(&dev->dev, "no I/O or memory address\n");
2198 goto err_free;
2199 }
2200 info->io.addr_data = res->start;
2201
2202 info->io.regspacing = DEFAULT_REGSPACING;
2203 res_second = pnp_get_resource(dev,
2204 (info->io.addr_type == IPMI_IO_ADDR_SPACE) ?
2205 IORESOURCE_IO : IORESOURCE_MEM,
2206 1);
2207 if (res_second) {
2208 if (res_second->start > info->io.addr_data)
2209 info->io.regspacing = res_second->start - info->io.addr_data;
2210 }
2211 info->io.regsize = DEFAULT_REGSPACING;
2212 info->io.regshift = 0;
2213
2214 /* If _GPE exists, use it; otherwise use standard interrupts */
2215 status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
2216 if (ACPI_SUCCESS(status)) {
2217 info->irq = tmp;
2218 info->irq_setup = acpi_gpe_irq_setup;
2219 } else if (pnp_irq_valid(dev, 0)) {
2220 info->irq = pnp_irq(dev, 0);
2221 info->irq_setup = std_irq_setup;
2222 }
2223
2224 info->dev = &dev->dev;
2225 pnp_set_drvdata(dev, info);
2226
2227 dev_info(info->dev, "%pR regsize %d spacing %d irq %d\n",
2228 res, info->io.regsize, info->io.regspacing,
2229 info->irq);
2230
2231 if (add_smi(info))
2232 goto err_free;
2233
2234 return 0;
2235
2236 err_free:
2237 kfree(info);
2238 return -EINVAL;
2239 }
2240
ipmi_pnp_remove(struct pnp_dev * dev)2241 static void __devexit ipmi_pnp_remove(struct pnp_dev *dev)
2242 {
2243 struct smi_info *info = pnp_get_drvdata(dev);
2244
2245 cleanup_one_si(info);
2246 }
2247
2248 static const struct pnp_device_id pnp_dev_table[] = {
2249 {"IPI0001", 0},
2250 {"", 0},
2251 };
2252
2253 static struct pnp_driver ipmi_pnp_driver = {
2254 .name = DEVICE_NAME,
2255 .probe = ipmi_pnp_probe,
2256 .remove = __devexit_p(ipmi_pnp_remove),
2257 .id_table = pnp_dev_table,
2258 };
2259 #endif
2260
2261 #ifdef CONFIG_DMI
2262 struct dmi_ipmi_data {
2263 u8 type;
2264 u8 addr_space;
2265 unsigned long base_addr;
2266 u8 irq;
2267 u8 offset;
2268 u8 slave_addr;
2269 };
2270
decode_dmi(const struct dmi_header * dm,struct dmi_ipmi_data * dmi)2271 static int __devinit decode_dmi(const struct dmi_header *dm,
2272 struct dmi_ipmi_data *dmi)
2273 {
2274 const u8 *data = (const u8 *)dm;
2275 unsigned long base_addr;
2276 u8 reg_spacing;
2277 u8 len = dm->length;
2278
2279 dmi->type = data[4];
2280
2281 memcpy(&base_addr, data+8, sizeof(unsigned long));
2282 if (len >= 0x11) {
2283 if (base_addr & 1) {
2284 /* I/O */
2285 base_addr &= 0xFFFE;
2286 dmi->addr_space = IPMI_IO_ADDR_SPACE;
2287 } else
2288 /* Memory */
2289 dmi->addr_space = IPMI_MEM_ADDR_SPACE;
2290
2291 /* If bit 4 of byte 0x10 is set, then the lsb for the address
2292 is odd. */
2293 dmi->base_addr = base_addr | ((data[0x10] & 0x10) >> 4);
2294
2295 dmi->irq = data[0x11];
2296
2297 /* The top two bits of byte 0x10 hold the register spacing. */
2298 reg_spacing = (data[0x10] & 0xC0) >> 6;
2299 switch (reg_spacing) {
2300 case 0x00: /* Byte boundaries */
2301 dmi->offset = 1;
2302 break;
2303 case 0x01: /* 32-bit boundaries */
2304 dmi->offset = 4;
2305 break;
2306 case 0x02: /* 16-byte boundaries */
2307 dmi->offset = 16;
2308 break;
2309 default:
2310 /* Some other interface, just ignore it. */
2311 return -EIO;
2312 }
2313 } else {
2314 /* Old DMI spec. */
2315 /*
2316 * Note that technically, the lower bit of the base
2317 * address should be 1 if the address is I/O and 0 if
2318 * the address is in memory. So many systems get that
2319 * wrong (and all that I have seen are I/O) so we just
2320 * ignore that bit and assume I/O. Systems that use
2321 * memory should use the newer spec, anyway.
2322 */
2323 dmi->base_addr = base_addr & 0xfffe;
2324 dmi->addr_space = IPMI_IO_ADDR_SPACE;
2325 dmi->offset = 1;
2326 }
2327
2328 dmi->slave_addr = data[6];
2329
2330 return 0;
2331 }
2332
try_init_dmi(struct dmi_ipmi_data * ipmi_data)2333 static void __devinit try_init_dmi(struct dmi_ipmi_data *ipmi_data)
2334 {
2335 struct smi_info *info;
2336
2337 info = smi_info_alloc();
2338 if (!info) {
2339 printk(KERN_ERR PFX "Could not allocate SI data\n");
2340 return;
2341 }
2342
2343 info->addr_source = SI_SMBIOS;
2344 printk(KERN_INFO PFX "probing via SMBIOS\n");
2345
2346 switch (ipmi_data->type) {
2347 case 0x01: /* KCS */
2348 info->si_type = SI_KCS;
2349 break;
2350 case 0x02: /* SMIC */
2351 info->si_type = SI_SMIC;
2352 break;
2353 case 0x03: /* BT */
2354 info->si_type = SI_BT;
2355 break;
2356 default:
2357 kfree(info);
2358 return;
2359 }
2360
2361 switch (ipmi_data->addr_space) {
2362 case IPMI_MEM_ADDR_SPACE:
2363 info->io_setup = mem_setup;
2364 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2365 break;
2366
2367 case IPMI_IO_ADDR_SPACE:
2368 info->io_setup = port_setup;
2369 info->io.addr_type = IPMI_IO_ADDR_SPACE;
2370 break;
2371
2372 default:
2373 kfree(info);
2374 printk(KERN_WARNING PFX "Unknown SMBIOS I/O Address type: %d\n",
2375 ipmi_data->addr_space);
2376 return;
2377 }
2378 info->io.addr_data = ipmi_data->base_addr;
2379
2380 info->io.regspacing = ipmi_data->offset;
2381 if (!info->io.regspacing)
2382 info->io.regspacing = DEFAULT_REGSPACING;
2383 info->io.regsize = DEFAULT_REGSPACING;
2384 info->io.regshift = 0;
2385
2386 info->slave_addr = ipmi_data->slave_addr;
2387
2388 info->irq = ipmi_data->irq;
2389 if (info->irq)
2390 info->irq_setup = std_irq_setup;
2391
2392 pr_info("ipmi_si: SMBIOS: %s %#lx regsize %d spacing %d irq %d\n",
2393 (info->io.addr_type == IPMI_IO_ADDR_SPACE) ? "io" : "mem",
2394 info->io.addr_data, info->io.regsize, info->io.regspacing,
2395 info->irq);
2396
2397 if (add_smi(info))
2398 kfree(info);
2399 }
2400
dmi_find_bmc(void)2401 static void __devinit dmi_find_bmc(void)
2402 {
2403 const struct dmi_device *dev = NULL;
2404 struct dmi_ipmi_data data;
2405 int rv;
2406
2407 while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev))) {
2408 memset(&data, 0, sizeof(data));
2409 rv = decode_dmi((const struct dmi_header *) dev->device_data,
2410 &data);
2411 if (!rv)
2412 try_init_dmi(&data);
2413 }
2414 }
2415 #endif /* CONFIG_DMI */
2416
2417 #ifdef CONFIG_PCI
2418
2419 #define PCI_ERMC_CLASSCODE 0x0C0700
2420 #define PCI_ERMC_CLASSCODE_MASK 0xffffff00
2421 #define PCI_ERMC_CLASSCODE_TYPE_MASK 0xff
2422 #define PCI_ERMC_CLASSCODE_TYPE_SMIC 0x00
2423 #define PCI_ERMC_CLASSCODE_TYPE_KCS 0x01
2424 #define PCI_ERMC_CLASSCODE_TYPE_BT 0x02
2425
2426 #define PCI_HP_VENDOR_ID 0x103C
2427 #define PCI_MMC_DEVICE_ID 0x121A
2428 #define PCI_MMC_ADDR_CW 0x10
2429
ipmi_pci_cleanup(struct smi_info * info)2430 static void ipmi_pci_cleanup(struct smi_info *info)
2431 {
2432 struct pci_dev *pdev = info->addr_source_data;
2433
2434 pci_disable_device(pdev);
2435 }
2436
ipmi_pci_probe(struct pci_dev * pdev,const struct pci_device_id * ent)2437 static int __devinit ipmi_pci_probe(struct pci_dev *pdev,
2438 const struct pci_device_id *ent)
2439 {
2440 int rv;
2441 int class_type = pdev->class & PCI_ERMC_CLASSCODE_TYPE_MASK;
2442 struct smi_info *info;
2443
2444 info = smi_info_alloc();
2445 if (!info)
2446 return -ENOMEM;
2447
2448 info->addr_source = SI_PCI;
2449 dev_info(&pdev->dev, "probing via PCI");
2450
2451 switch (class_type) {
2452 case PCI_ERMC_CLASSCODE_TYPE_SMIC:
2453 info->si_type = SI_SMIC;
2454 break;
2455
2456 case PCI_ERMC_CLASSCODE_TYPE_KCS:
2457 info->si_type = SI_KCS;
2458 break;
2459
2460 case PCI_ERMC_CLASSCODE_TYPE_BT:
2461 info->si_type = SI_BT;
2462 break;
2463
2464 default:
2465 kfree(info);
2466 dev_info(&pdev->dev, "Unknown IPMI type: %d\n", class_type);
2467 return -ENOMEM;
2468 }
2469
2470 rv = pci_enable_device(pdev);
2471 if (rv) {
2472 dev_err(&pdev->dev, "couldn't enable PCI device\n");
2473 kfree(info);
2474 return rv;
2475 }
2476
2477 info->addr_source_cleanup = ipmi_pci_cleanup;
2478 info->addr_source_data = pdev;
2479
2480 if (pci_resource_flags(pdev, 0) & IORESOURCE_IO) {
2481 info->io_setup = port_setup;
2482 info->io.addr_type = IPMI_IO_ADDR_SPACE;
2483 } else {
2484 info->io_setup = mem_setup;
2485 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2486 }
2487 info->io.addr_data = pci_resource_start(pdev, 0);
2488
2489 info->io.regspacing = DEFAULT_REGSPACING;
2490 info->io.regsize = DEFAULT_REGSPACING;
2491 info->io.regshift = 0;
2492
2493 info->irq = pdev->irq;
2494 if (info->irq)
2495 info->irq_setup = std_irq_setup;
2496
2497 info->dev = &pdev->dev;
2498 pci_set_drvdata(pdev, info);
2499
2500 dev_info(&pdev->dev, "%pR regsize %d spacing %d irq %d\n",
2501 &pdev->resource[0], info->io.regsize, info->io.regspacing,
2502 info->irq);
2503
2504 if (add_smi(info))
2505 kfree(info);
2506
2507 return 0;
2508 }
2509
ipmi_pci_remove(struct pci_dev * pdev)2510 static void __devexit ipmi_pci_remove(struct pci_dev *pdev)
2511 {
2512 struct smi_info *info = pci_get_drvdata(pdev);
2513 cleanup_one_si(info);
2514 }
2515
2516 #ifdef CONFIG_PM
ipmi_pci_suspend(struct pci_dev * pdev,pm_message_t state)2517 static int ipmi_pci_suspend(struct pci_dev *pdev, pm_message_t state)
2518 {
2519 return 0;
2520 }
2521
ipmi_pci_resume(struct pci_dev * pdev)2522 static int ipmi_pci_resume(struct pci_dev *pdev)
2523 {
2524 return 0;
2525 }
2526 #endif
2527
2528 static struct pci_device_id ipmi_pci_devices[] = {
2529 { PCI_DEVICE(PCI_HP_VENDOR_ID, PCI_MMC_DEVICE_ID) },
2530 { PCI_DEVICE_CLASS(PCI_ERMC_CLASSCODE, PCI_ERMC_CLASSCODE_MASK) },
2531 { 0, }
2532 };
2533 MODULE_DEVICE_TABLE(pci, ipmi_pci_devices);
2534
2535 static struct pci_driver ipmi_pci_driver = {
2536 .name = DEVICE_NAME,
2537 .id_table = ipmi_pci_devices,
2538 .probe = ipmi_pci_probe,
2539 .remove = __devexit_p(ipmi_pci_remove),
2540 #ifdef CONFIG_PM
2541 .suspend = ipmi_pci_suspend,
2542 .resume = ipmi_pci_resume,
2543 #endif
2544 };
2545 #endif /* CONFIG_PCI */
2546
2547 static struct of_device_id ipmi_match[];
ipmi_probe(struct platform_device * dev)2548 static int __devinit ipmi_probe(struct platform_device *dev)
2549 {
2550 #ifdef CONFIG_OF
2551 const struct of_device_id *match;
2552 struct smi_info *info;
2553 struct resource resource;
2554 const __be32 *regsize, *regspacing, *regshift;
2555 struct device_node *np = dev->dev.of_node;
2556 int ret;
2557 int proplen;
2558
2559 dev_info(&dev->dev, "probing via device tree\n");
2560
2561 match = of_match_device(ipmi_match, &dev->dev);
2562 if (!match)
2563 return -EINVAL;
2564
2565 ret = of_address_to_resource(np, 0, &resource);
2566 if (ret) {
2567 dev_warn(&dev->dev, PFX "invalid address from OF\n");
2568 return ret;
2569 }
2570
2571 regsize = of_get_property(np, "reg-size", &proplen);
2572 if (regsize && proplen != 4) {
2573 dev_warn(&dev->dev, PFX "invalid regsize from OF\n");
2574 return -EINVAL;
2575 }
2576
2577 regspacing = of_get_property(np, "reg-spacing", &proplen);
2578 if (regspacing && proplen != 4) {
2579 dev_warn(&dev->dev, PFX "invalid regspacing from OF\n");
2580 return -EINVAL;
2581 }
2582
2583 regshift = of_get_property(np, "reg-shift", &proplen);
2584 if (regshift && proplen != 4) {
2585 dev_warn(&dev->dev, PFX "invalid regshift from OF\n");
2586 return -EINVAL;
2587 }
2588
2589 info = smi_info_alloc();
2590
2591 if (!info) {
2592 dev_err(&dev->dev,
2593 "could not allocate memory for OF probe\n");
2594 return -ENOMEM;
2595 }
2596
2597 info->si_type = (enum si_type) match->data;
2598 info->addr_source = SI_DEVICETREE;
2599 info->irq_setup = std_irq_setup;
2600
2601 if (resource.flags & IORESOURCE_IO) {
2602 info->io_setup = port_setup;
2603 info->io.addr_type = IPMI_IO_ADDR_SPACE;
2604 } else {
2605 info->io_setup = mem_setup;
2606 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2607 }
2608
2609 info->io.addr_data = resource.start;
2610
2611 info->io.regsize = regsize ? be32_to_cpup(regsize) : DEFAULT_REGSIZE;
2612 info->io.regspacing = regspacing ? be32_to_cpup(regspacing) : DEFAULT_REGSPACING;
2613 info->io.regshift = regshift ? be32_to_cpup(regshift) : 0;
2614
2615 info->irq = irq_of_parse_and_map(dev->dev.of_node, 0);
2616 info->dev = &dev->dev;
2617
2618 dev_dbg(&dev->dev, "addr 0x%lx regsize %d spacing %d irq %d\n",
2619 info->io.addr_data, info->io.regsize, info->io.regspacing,
2620 info->irq);
2621
2622 dev_set_drvdata(&dev->dev, info);
2623
2624 if (add_smi(info)) {
2625 kfree(info);
2626 return -EBUSY;
2627 }
2628 #endif
2629 return 0;
2630 }
2631
ipmi_remove(struct platform_device * dev)2632 static int __devexit ipmi_remove(struct platform_device *dev)
2633 {
2634 #ifdef CONFIG_OF
2635 cleanup_one_si(dev_get_drvdata(&dev->dev));
2636 #endif
2637 return 0;
2638 }
2639
2640 static struct of_device_id ipmi_match[] =
2641 {
2642 { .type = "ipmi", .compatible = "ipmi-kcs",
2643 .data = (void *)(unsigned long) SI_KCS },
2644 { .type = "ipmi", .compatible = "ipmi-smic",
2645 .data = (void *)(unsigned long) SI_SMIC },
2646 { .type = "ipmi", .compatible = "ipmi-bt",
2647 .data = (void *)(unsigned long) SI_BT },
2648 {},
2649 };
2650
2651 static struct platform_driver ipmi_driver = {
2652 .driver = {
2653 .name = DEVICE_NAME,
2654 .owner = THIS_MODULE,
2655 .of_match_table = ipmi_match,
2656 },
2657 .probe = ipmi_probe,
2658 .remove = __devexit_p(ipmi_remove),
2659 };
2660
wait_for_msg_done(struct smi_info * smi_info)2661 static int wait_for_msg_done(struct smi_info *smi_info)
2662 {
2663 enum si_sm_result smi_result;
2664
2665 smi_result = smi_info->handlers->event(smi_info->si_sm, 0);
2666 for (;;) {
2667 if (smi_result == SI_SM_CALL_WITH_DELAY ||
2668 smi_result == SI_SM_CALL_WITH_TICK_DELAY) {
2669 schedule_timeout_uninterruptible(1);
2670 smi_result = smi_info->handlers->event(
2671 smi_info->si_sm, 100);
2672 } else if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
2673 smi_result = smi_info->handlers->event(
2674 smi_info->si_sm, 0);
2675 } else
2676 break;
2677 }
2678 if (smi_result == SI_SM_HOSED)
2679 /*
2680 * We couldn't get the state machine to run, so whatever's at
2681 * the port is probably not an IPMI SMI interface.
2682 */
2683 return -ENODEV;
2684
2685 return 0;
2686 }
2687
try_get_dev_id(struct smi_info * smi_info)2688 static int try_get_dev_id(struct smi_info *smi_info)
2689 {
2690 unsigned char msg[2];
2691 unsigned char *resp;
2692 unsigned long resp_len;
2693 int rv = 0;
2694
2695 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
2696 if (!resp)
2697 return -ENOMEM;
2698
2699 /*
2700 * Do a Get Device ID command, since it comes back with some
2701 * useful info.
2702 */
2703 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
2704 msg[1] = IPMI_GET_DEVICE_ID_CMD;
2705 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
2706
2707 rv = wait_for_msg_done(smi_info);
2708 if (rv)
2709 goto out;
2710
2711 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
2712 resp, IPMI_MAX_MSG_LENGTH);
2713
2714 /* Check and record info from the get device id, in case we need it. */
2715 rv = ipmi_demangle_device_id(resp, resp_len, &smi_info->device_id);
2716
2717 out:
2718 kfree(resp);
2719 return rv;
2720 }
2721
try_enable_event_buffer(struct smi_info * smi_info)2722 static int try_enable_event_buffer(struct smi_info *smi_info)
2723 {
2724 unsigned char msg[3];
2725 unsigned char *resp;
2726 unsigned long resp_len;
2727 int rv = 0;
2728
2729 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
2730 if (!resp)
2731 return -ENOMEM;
2732
2733 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
2734 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
2735 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
2736
2737 rv = wait_for_msg_done(smi_info);
2738 if (rv) {
2739 printk(KERN_WARNING PFX "Error getting response from get"
2740 " global enables command, the event buffer is not"
2741 " enabled.\n");
2742 goto out;
2743 }
2744
2745 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
2746 resp, IPMI_MAX_MSG_LENGTH);
2747
2748 if (resp_len < 4 ||
2749 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
2750 resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD ||
2751 resp[2] != 0) {
2752 printk(KERN_WARNING PFX "Invalid return from get global"
2753 " enables command, cannot enable the event buffer.\n");
2754 rv = -EINVAL;
2755 goto out;
2756 }
2757
2758 if (resp[3] & IPMI_BMC_EVT_MSG_BUFF)
2759 /* buffer is already enabled, nothing to do. */
2760 goto out;
2761
2762 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
2763 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
2764 msg[2] = resp[3] | IPMI_BMC_EVT_MSG_BUFF;
2765 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
2766
2767 rv = wait_for_msg_done(smi_info);
2768 if (rv) {
2769 printk(KERN_WARNING PFX "Error getting response from set"
2770 " global, enables command, the event buffer is not"
2771 " enabled.\n");
2772 goto out;
2773 }
2774
2775 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
2776 resp, IPMI_MAX_MSG_LENGTH);
2777
2778 if (resp_len < 3 ||
2779 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
2780 resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
2781 printk(KERN_WARNING PFX "Invalid return from get global,"
2782 "enables command, not enable the event buffer.\n");
2783 rv = -EINVAL;
2784 goto out;
2785 }
2786
2787 if (resp[2] != 0)
2788 /*
2789 * An error when setting the event buffer bit means
2790 * that the event buffer is not supported.
2791 */
2792 rv = -ENOENT;
2793 out:
2794 kfree(resp);
2795 return rv;
2796 }
2797
smi_type_proc_show(struct seq_file * m,void * v)2798 static int smi_type_proc_show(struct seq_file *m, void *v)
2799 {
2800 struct smi_info *smi = m->private;
2801
2802 return seq_printf(m, "%s\n", si_to_str[smi->si_type]);
2803 }
2804
smi_type_proc_open(struct inode * inode,struct file * file)2805 static int smi_type_proc_open(struct inode *inode, struct file *file)
2806 {
2807 return single_open(file, smi_type_proc_show, PDE(inode)->data);
2808 }
2809
2810 static const struct file_operations smi_type_proc_ops = {
2811 .open = smi_type_proc_open,
2812 .read = seq_read,
2813 .llseek = seq_lseek,
2814 .release = single_release,
2815 };
2816
smi_si_stats_proc_show(struct seq_file * m,void * v)2817 static int smi_si_stats_proc_show(struct seq_file *m, void *v)
2818 {
2819 struct smi_info *smi = m->private;
2820
2821 seq_printf(m, "interrupts_enabled: %d\n",
2822 smi->irq && !smi->interrupt_disabled);
2823 seq_printf(m, "short_timeouts: %u\n",
2824 smi_get_stat(smi, short_timeouts));
2825 seq_printf(m, "long_timeouts: %u\n",
2826 smi_get_stat(smi, long_timeouts));
2827 seq_printf(m, "idles: %u\n",
2828 smi_get_stat(smi, idles));
2829 seq_printf(m, "interrupts: %u\n",
2830 smi_get_stat(smi, interrupts));
2831 seq_printf(m, "attentions: %u\n",
2832 smi_get_stat(smi, attentions));
2833 seq_printf(m, "flag_fetches: %u\n",
2834 smi_get_stat(smi, flag_fetches));
2835 seq_printf(m, "hosed_count: %u\n",
2836 smi_get_stat(smi, hosed_count));
2837 seq_printf(m, "complete_transactions: %u\n",
2838 smi_get_stat(smi, complete_transactions));
2839 seq_printf(m, "events: %u\n",
2840 smi_get_stat(smi, events));
2841 seq_printf(m, "watchdog_pretimeouts: %u\n",
2842 smi_get_stat(smi, watchdog_pretimeouts));
2843 seq_printf(m, "incoming_messages: %u\n",
2844 smi_get_stat(smi, incoming_messages));
2845 return 0;
2846 }
2847
smi_si_stats_proc_open(struct inode * inode,struct file * file)2848 static int smi_si_stats_proc_open(struct inode *inode, struct file *file)
2849 {
2850 return single_open(file, smi_si_stats_proc_show, PDE(inode)->data);
2851 }
2852
2853 static const struct file_operations smi_si_stats_proc_ops = {
2854 .open = smi_si_stats_proc_open,
2855 .read = seq_read,
2856 .llseek = seq_lseek,
2857 .release = single_release,
2858 };
2859
smi_params_proc_show(struct seq_file * m,void * v)2860 static int smi_params_proc_show(struct seq_file *m, void *v)
2861 {
2862 struct smi_info *smi = m->private;
2863
2864 return seq_printf(m,
2865 "%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n",
2866 si_to_str[smi->si_type],
2867 addr_space_to_str[smi->io.addr_type],
2868 smi->io.addr_data,
2869 smi->io.regspacing,
2870 smi->io.regsize,
2871 smi->io.regshift,
2872 smi->irq,
2873 smi->slave_addr);
2874 }
2875
smi_params_proc_open(struct inode * inode,struct file * file)2876 static int smi_params_proc_open(struct inode *inode, struct file *file)
2877 {
2878 return single_open(file, smi_params_proc_show, PDE(inode)->data);
2879 }
2880
2881 static const struct file_operations smi_params_proc_ops = {
2882 .open = smi_params_proc_open,
2883 .read = seq_read,
2884 .llseek = seq_lseek,
2885 .release = single_release,
2886 };
2887
2888 /*
2889 * oem_data_avail_to_receive_msg_avail
2890 * @info - smi_info structure with msg_flags set
2891 *
2892 * Converts flags from OEM_DATA_AVAIL to RECEIVE_MSG_AVAIL
2893 * Returns 1 indicating need to re-run handle_flags().
2894 */
oem_data_avail_to_receive_msg_avail(struct smi_info * smi_info)2895 static int oem_data_avail_to_receive_msg_avail(struct smi_info *smi_info)
2896 {
2897 smi_info->msg_flags = ((smi_info->msg_flags & ~OEM_DATA_AVAIL) |
2898 RECEIVE_MSG_AVAIL);
2899 return 1;
2900 }
2901
2902 /*
2903 * setup_dell_poweredge_oem_data_handler
2904 * @info - smi_info.device_id must be populated
2905 *
2906 * Systems that match, but have firmware version < 1.40 may assert
2907 * OEM0_DATA_AVAIL on their own, without being told via Set Flags that
2908 * it's safe to do so. Such systems will de-assert OEM1_DATA_AVAIL
2909 * upon receipt of IPMI_GET_MSG_CMD, so we should treat these flags
2910 * as RECEIVE_MSG_AVAIL instead.
2911 *
2912 * As Dell has no plans to release IPMI 1.5 firmware that *ever*
2913 * assert the OEM[012] bits, and if it did, the driver would have to
2914 * change to handle that properly, we don't actually check for the
2915 * firmware version.
2916 * Device ID = 0x20 BMC on PowerEdge 8G servers
2917 * Device Revision = 0x80
2918 * Firmware Revision1 = 0x01 BMC version 1.40
2919 * Firmware Revision2 = 0x40 BCD encoded
2920 * IPMI Version = 0x51 IPMI 1.5
2921 * Manufacturer ID = A2 02 00 Dell IANA
2922 *
2923 * Additionally, PowerEdge systems with IPMI < 1.5 may also assert
2924 * OEM0_DATA_AVAIL and needs to be treated as RECEIVE_MSG_AVAIL.
2925 *
2926 */
2927 #define DELL_POWEREDGE_8G_BMC_DEVICE_ID 0x20
2928 #define DELL_POWEREDGE_8G_BMC_DEVICE_REV 0x80
2929 #define DELL_POWEREDGE_8G_BMC_IPMI_VERSION 0x51
2930 #define DELL_IANA_MFR_ID 0x0002a2
setup_dell_poweredge_oem_data_handler(struct smi_info * smi_info)2931 static void setup_dell_poweredge_oem_data_handler(struct smi_info *smi_info)
2932 {
2933 struct ipmi_device_id *id = &smi_info->device_id;
2934 if (id->manufacturer_id == DELL_IANA_MFR_ID) {
2935 if (id->device_id == DELL_POWEREDGE_8G_BMC_DEVICE_ID &&
2936 id->device_revision == DELL_POWEREDGE_8G_BMC_DEVICE_REV &&
2937 id->ipmi_version == DELL_POWEREDGE_8G_BMC_IPMI_VERSION) {
2938 smi_info->oem_data_avail_handler =
2939 oem_data_avail_to_receive_msg_avail;
2940 } else if (ipmi_version_major(id) < 1 ||
2941 (ipmi_version_major(id) == 1 &&
2942 ipmi_version_minor(id) < 5)) {
2943 smi_info->oem_data_avail_handler =
2944 oem_data_avail_to_receive_msg_avail;
2945 }
2946 }
2947 }
2948
2949 #define CANNOT_RETURN_REQUESTED_LENGTH 0xCA
return_hosed_msg_badsize(struct smi_info * smi_info)2950 static void return_hosed_msg_badsize(struct smi_info *smi_info)
2951 {
2952 struct ipmi_smi_msg *msg = smi_info->curr_msg;
2953
2954 /* Make it a response */
2955 msg->rsp[0] = msg->data[0] | 4;
2956 msg->rsp[1] = msg->data[1];
2957 msg->rsp[2] = CANNOT_RETURN_REQUESTED_LENGTH;
2958 msg->rsp_size = 3;
2959 smi_info->curr_msg = NULL;
2960 deliver_recv_msg(smi_info, msg);
2961 }
2962
2963 /*
2964 * dell_poweredge_bt_xaction_handler
2965 * @info - smi_info.device_id must be populated
2966 *
2967 * Dell PowerEdge servers with the BT interface (x6xx and 1750) will
2968 * not respond to a Get SDR command if the length of the data
2969 * requested is exactly 0x3A, which leads to command timeouts and no
2970 * data returned. This intercepts such commands, and causes userspace
2971 * callers to try again with a different-sized buffer, which succeeds.
2972 */
2973
2974 #define STORAGE_NETFN 0x0A
2975 #define STORAGE_CMD_GET_SDR 0x23
dell_poweredge_bt_xaction_handler(struct notifier_block * self,unsigned long unused,void * in)2976 static int dell_poweredge_bt_xaction_handler(struct notifier_block *self,
2977 unsigned long unused,
2978 void *in)
2979 {
2980 struct smi_info *smi_info = in;
2981 unsigned char *data = smi_info->curr_msg->data;
2982 unsigned int size = smi_info->curr_msg->data_size;
2983 if (size >= 8 &&
2984 (data[0]>>2) == STORAGE_NETFN &&
2985 data[1] == STORAGE_CMD_GET_SDR &&
2986 data[7] == 0x3A) {
2987 return_hosed_msg_badsize(smi_info);
2988 return NOTIFY_STOP;
2989 }
2990 return NOTIFY_DONE;
2991 }
2992
2993 static struct notifier_block dell_poweredge_bt_xaction_notifier = {
2994 .notifier_call = dell_poweredge_bt_xaction_handler,
2995 };
2996
2997 /*
2998 * setup_dell_poweredge_bt_xaction_handler
2999 * @info - smi_info.device_id must be filled in already
3000 *
3001 * Fills in smi_info.device_id.start_transaction_pre_hook
3002 * when we know what function to use there.
3003 */
3004 static void
setup_dell_poweredge_bt_xaction_handler(struct smi_info * smi_info)3005 setup_dell_poweredge_bt_xaction_handler(struct smi_info *smi_info)
3006 {
3007 struct ipmi_device_id *id = &smi_info->device_id;
3008 if (id->manufacturer_id == DELL_IANA_MFR_ID &&
3009 smi_info->si_type == SI_BT)
3010 register_xaction_notifier(&dell_poweredge_bt_xaction_notifier);
3011 }
3012
3013 /*
3014 * setup_oem_data_handler
3015 * @info - smi_info.device_id must be filled in already
3016 *
3017 * Fills in smi_info.device_id.oem_data_available_handler
3018 * when we know what function to use there.
3019 */
3020
setup_oem_data_handler(struct smi_info * smi_info)3021 static void setup_oem_data_handler(struct smi_info *smi_info)
3022 {
3023 setup_dell_poweredge_oem_data_handler(smi_info);
3024 }
3025
setup_xaction_handlers(struct smi_info * smi_info)3026 static void setup_xaction_handlers(struct smi_info *smi_info)
3027 {
3028 setup_dell_poweredge_bt_xaction_handler(smi_info);
3029 }
3030
wait_for_timer_and_thread(struct smi_info * smi_info)3031 static inline void wait_for_timer_and_thread(struct smi_info *smi_info)
3032 {
3033 if (smi_info->intf) {
3034 /*
3035 * The timer and thread are only running if the
3036 * interface has been started up and registered.
3037 */
3038 if (smi_info->thread != NULL)
3039 kthread_stop(smi_info->thread);
3040 del_timer_sync(&smi_info->si_timer);
3041 }
3042 }
3043
3044 static __devinitdata struct ipmi_default_vals
3045 {
3046 int type;
3047 int port;
3048 } ipmi_defaults[] =
3049 {
3050 { .type = SI_KCS, .port = 0xca2 },
3051 { .type = SI_SMIC, .port = 0xca9 },
3052 { .type = SI_BT, .port = 0xe4 },
3053 { .port = 0 }
3054 };
3055
default_find_bmc(void)3056 static void __devinit default_find_bmc(void)
3057 {
3058 struct smi_info *info;
3059 int i;
3060
3061 for (i = 0; ; i++) {
3062 if (!ipmi_defaults[i].port)
3063 break;
3064 #ifdef CONFIG_PPC
3065 if (check_legacy_ioport(ipmi_defaults[i].port))
3066 continue;
3067 #endif
3068 info = smi_info_alloc();
3069 if (!info)
3070 return;
3071
3072 info->addr_source = SI_DEFAULT;
3073
3074 info->si_type = ipmi_defaults[i].type;
3075 info->io_setup = port_setup;
3076 info->io.addr_data = ipmi_defaults[i].port;
3077 info->io.addr_type = IPMI_IO_ADDR_SPACE;
3078
3079 info->io.addr = NULL;
3080 info->io.regspacing = DEFAULT_REGSPACING;
3081 info->io.regsize = DEFAULT_REGSPACING;
3082 info->io.regshift = 0;
3083
3084 if (add_smi(info) == 0) {
3085 if ((try_smi_init(info)) == 0) {
3086 /* Found one... */
3087 printk(KERN_INFO PFX "Found default %s"
3088 " state machine at %s address 0x%lx\n",
3089 si_to_str[info->si_type],
3090 addr_space_to_str[info->io.addr_type],
3091 info->io.addr_data);
3092 } else
3093 cleanup_one_si(info);
3094 } else {
3095 kfree(info);
3096 }
3097 }
3098 }
3099
is_new_interface(struct smi_info * info)3100 static int is_new_interface(struct smi_info *info)
3101 {
3102 struct smi_info *e;
3103
3104 list_for_each_entry(e, &smi_infos, link) {
3105 if (e->io.addr_type != info->io.addr_type)
3106 continue;
3107 if (e->io.addr_data == info->io.addr_data)
3108 return 0;
3109 }
3110
3111 return 1;
3112 }
3113
add_smi(struct smi_info * new_smi)3114 static int add_smi(struct smi_info *new_smi)
3115 {
3116 int rv = 0;
3117
3118 printk(KERN_INFO PFX "Adding %s-specified %s state machine",
3119 ipmi_addr_src_to_str[new_smi->addr_source],
3120 si_to_str[new_smi->si_type]);
3121 mutex_lock(&smi_infos_lock);
3122 if (!is_new_interface(new_smi)) {
3123 printk(KERN_CONT " duplicate interface\n");
3124 rv = -EBUSY;
3125 goto out_err;
3126 }
3127
3128 printk(KERN_CONT "\n");
3129
3130 /* So we know not to free it unless we have allocated one. */
3131 new_smi->intf = NULL;
3132 new_smi->si_sm = NULL;
3133 new_smi->handlers = NULL;
3134
3135 list_add_tail(&new_smi->link, &smi_infos);
3136
3137 out_err:
3138 mutex_unlock(&smi_infos_lock);
3139 return rv;
3140 }
3141
try_smi_init(struct smi_info * new_smi)3142 static int try_smi_init(struct smi_info *new_smi)
3143 {
3144 int rv = 0;
3145 int i;
3146
3147 printk(KERN_INFO PFX "Trying %s-specified %s state"
3148 " machine at %s address 0x%lx, slave address 0x%x,"
3149 " irq %d\n",
3150 ipmi_addr_src_to_str[new_smi->addr_source],
3151 si_to_str[new_smi->si_type],
3152 addr_space_to_str[new_smi->io.addr_type],
3153 new_smi->io.addr_data,
3154 new_smi->slave_addr, new_smi->irq);
3155
3156 switch (new_smi->si_type) {
3157 case SI_KCS:
3158 new_smi->handlers = &kcs_smi_handlers;
3159 break;
3160
3161 case SI_SMIC:
3162 new_smi->handlers = &smic_smi_handlers;
3163 break;
3164
3165 case SI_BT:
3166 new_smi->handlers = &bt_smi_handlers;
3167 break;
3168
3169 default:
3170 /* No support for anything else yet. */
3171 rv = -EIO;
3172 goto out_err;
3173 }
3174
3175 /* Allocate the state machine's data and initialize it. */
3176 new_smi->si_sm = kmalloc(new_smi->handlers->size(), GFP_KERNEL);
3177 if (!new_smi->si_sm) {
3178 printk(KERN_ERR PFX
3179 "Could not allocate state machine memory\n");
3180 rv = -ENOMEM;
3181 goto out_err;
3182 }
3183 new_smi->io_size = new_smi->handlers->init_data(new_smi->si_sm,
3184 &new_smi->io);
3185
3186 /* Now that we know the I/O size, we can set up the I/O. */
3187 rv = new_smi->io_setup(new_smi);
3188 if (rv) {
3189 printk(KERN_ERR PFX "Could not set up I/O space\n");
3190 goto out_err;
3191 }
3192
3193 /* Do low-level detection first. */
3194 if (new_smi->handlers->detect(new_smi->si_sm)) {
3195 if (new_smi->addr_source)
3196 printk(KERN_INFO PFX "Interface detection failed\n");
3197 rv = -ENODEV;
3198 goto out_err;
3199 }
3200
3201 /*
3202 * Attempt a get device id command. If it fails, we probably
3203 * don't have a BMC here.
3204 */
3205 rv = try_get_dev_id(new_smi);
3206 if (rv) {
3207 if (new_smi->addr_source)
3208 printk(KERN_INFO PFX "There appears to be no BMC"
3209 " at this location\n");
3210 goto out_err;
3211 }
3212
3213 setup_oem_data_handler(new_smi);
3214 setup_xaction_handlers(new_smi);
3215
3216 INIT_LIST_HEAD(&(new_smi->xmit_msgs));
3217 INIT_LIST_HEAD(&(new_smi->hp_xmit_msgs));
3218 new_smi->curr_msg = NULL;
3219 atomic_set(&new_smi->req_events, 0);
3220 new_smi->run_to_completion = 0;
3221 for (i = 0; i < SI_NUM_STATS; i++)
3222 atomic_set(&new_smi->stats[i], 0);
3223
3224 new_smi->interrupt_disabled = 1;
3225 atomic_set(&new_smi->stop_operation, 0);
3226 new_smi->intf_num = smi_num;
3227 smi_num++;
3228
3229 rv = try_enable_event_buffer(new_smi);
3230 if (rv == 0)
3231 new_smi->has_event_buffer = 1;
3232
3233 /*
3234 * Start clearing the flags before we enable interrupts or the
3235 * timer to avoid racing with the timer.
3236 */
3237 start_clear_flags(new_smi);
3238 /* IRQ is defined to be set when non-zero. */
3239 if (new_smi->irq)
3240 new_smi->si_state = SI_CLEARING_FLAGS_THEN_SET_IRQ;
3241
3242 if (!new_smi->dev) {
3243 /*
3244 * If we don't already have a device from something
3245 * else (like PCI), then register a new one.
3246 */
3247 new_smi->pdev = platform_device_alloc("ipmi_si",
3248 new_smi->intf_num);
3249 if (!new_smi->pdev) {
3250 printk(KERN_ERR PFX
3251 "Unable to allocate platform device\n");
3252 goto out_err;
3253 }
3254 new_smi->dev = &new_smi->pdev->dev;
3255 new_smi->dev->driver = &ipmi_driver.driver;
3256
3257 rv = platform_device_add(new_smi->pdev);
3258 if (rv) {
3259 printk(KERN_ERR PFX
3260 "Unable to register system interface device:"
3261 " %d\n",
3262 rv);
3263 goto out_err;
3264 }
3265 new_smi->dev_registered = 1;
3266 }
3267
3268 rv = ipmi_register_smi(&handlers,
3269 new_smi,
3270 &new_smi->device_id,
3271 new_smi->dev,
3272 "bmc",
3273 new_smi->slave_addr);
3274 if (rv) {
3275 dev_err(new_smi->dev, "Unable to register device: error %d\n",
3276 rv);
3277 goto out_err_stop_timer;
3278 }
3279
3280 rv = ipmi_smi_add_proc_entry(new_smi->intf, "type",
3281 &smi_type_proc_ops,
3282 new_smi);
3283 if (rv) {
3284 dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);
3285 goto out_err_stop_timer;
3286 }
3287
3288 rv = ipmi_smi_add_proc_entry(new_smi->intf, "si_stats",
3289 &smi_si_stats_proc_ops,
3290 new_smi);
3291 if (rv) {
3292 dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);
3293 goto out_err_stop_timer;
3294 }
3295
3296 rv = ipmi_smi_add_proc_entry(new_smi->intf, "params",
3297 &smi_params_proc_ops,
3298 new_smi);
3299 if (rv) {
3300 dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);
3301 goto out_err_stop_timer;
3302 }
3303
3304 dev_info(new_smi->dev, "IPMI %s interface initialized\n",
3305 si_to_str[new_smi->si_type]);
3306
3307 return 0;
3308
3309 out_err_stop_timer:
3310 atomic_inc(&new_smi->stop_operation);
3311 wait_for_timer_and_thread(new_smi);
3312
3313 out_err:
3314 new_smi->interrupt_disabled = 1;
3315
3316 if (new_smi->intf) {
3317 ipmi_unregister_smi(new_smi->intf);
3318 new_smi->intf = NULL;
3319 }
3320
3321 if (new_smi->irq_cleanup) {
3322 new_smi->irq_cleanup(new_smi);
3323 new_smi->irq_cleanup = NULL;
3324 }
3325
3326 /*
3327 * Wait until we know that we are out of any interrupt
3328 * handlers might have been running before we freed the
3329 * interrupt.
3330 */
3331 synchronize_sched();
3332
3333 if (new_smi->si_sm) {
3334 if (new_smi->handlers)
3335 new_smi->handlers->cleanup(new_smi->si_sm);
3336 kfree(new_smi->si_sm);
3337 new_smi->si_sm = NULL;
3338 }
3339 if (new_smi->addr_source_cleanup) {
3340 new_smi->addr_source_cleanup(new_smi);
3341 new_smi->addr_source_cleanup = NULL;
3342 }
3343 if (new_smi->io_cleanup) {
3344 new_smi->io_cleanup(new_smi);
3345 new_smi->io_cleanup = NULL;
3346 }
3347
3348 if (new_smi->dev_registered) {
3349 platform_device_unregister(new_smi->pdev);
3350 new_smi->dev_registered = 0;
3351 }
3352
3353 return rv;
3354 }
3355
init_ipmi_si(void)3356 static int __devinit init_ipmi_si(void)
3357 {
3358 int i;
3359 char *str;
3360 int rv;
3361 struct smi_info *e;
3362 enum ipmi_addr_src type = SI_INVALID;
3363
3364 if (initialized)
3365 return 0;
3366 initialized = 1;
3367
3368 rv = platform_driver_register(&ipmi_driver);
3369 if (rv) {
3370 printk(KERN_ERR PFX "Unable to register driver: %d\n", rv);
3371 return rv;
3372 }
3373
3374
3375 /* Parse out the si_type string into its components. */
3376 str = si_type_str;
3377 if (*str != '\0') {
3378 for (i = 0; (i < SI_MAX_PARMS) && (*str != '\0'); i++) {
3379 si_type[i] = str;
3380 str = strchr(str, ',');
3381 if (str) {
3382 *str = '\0';
3383 str++;
3384 } else {
3385 break;
3386 }
3387 }
3388 }
3389
3390 printk(KERN_INFO "IPMI System Interface driver.\n");
3391
3392 /* If the user gave us a device, they presumably want us to use it */
3393 if (!hardcode_find_bmc())
3394 return 0;
3395
3396 #ifdef CONFIG_PCI
3397 rv = pci_register_driver(&ipmi_pci_driver);
3398 if (rv)
3399 printk(KERN_ERR PFX "Unable to register PCI driver: %d\n", rv);
3400 else
3401 pci_registered = 1;
3402 #endif
3403
3404 #ifdef CONFIG_ACPI
3405 pnp_register_driver(&ipmi_pnp_driver);
3406 pnp_registered = 1;
3407 #endif
3408
3409 #ifdef CONFIG_DMI
3410 dmi_find_bmc();
3411 #endif
3412
3413 #ifdef CONFIG_ACPI
3414 spmi_find_bmc();
3415 #endif
3416
3417 /* We prefer devices with interrupts, but in the case of a machine
3418 with multiple BMCs we assume that there will be several instances
3419 of a given type so if we succeed in registering a type then also
3420 try to register everything else of the same type */
3421
3422 mutex_lock(&smi_infos_lock);
3423 list_for_each_entry(e, &smi_infos, link) {
3424 /* Try to register a device if it has an IRQ and we either
3425 haven't successfully registered a device yet or this
3426 device has the same type as one we successfully registered */
3427 if (e->irq && (!type || e->addr_source == type)) {
3428 if (!try_smi_init(e)) {
3429 type = e->addr_source;
3430 }
3431 }
3432 }
3433
3434 /* type will only have been set if we successfully registered an si */
3435 if (type) {
3436 mutex_unlock(&smi_infos_lock);
3437 return 0;
3438 }
3439
3440 /* Fall back to the preferred device */
3441
3442 list_for_each_entry(e, &smi_infos, link) {
3443 if (!e->irq && (!type || e->addr_source == type)) {
3444 if (!try_smi_init(e)) {
3445 type = e->addr_source;
3446 }
3447 }
3448 }
3449 mutex_unlock(&smi_infos_lock);
3450
3451 if (type)
3452 return 0;
3453
3454 if (si_trydefaults) {
3455 mutex_lock(&smi_infos_lock);
3456 if (list_empty(&smi_infos)) {
3457 /* No BMC was found, try defaults. */
3458 mutex_unlock(&smi_infos_lock);
3459 default_find_bmc();
3460 } else
3461 mutex_unlock(&smi_infos_lock);
3462 }
3463
3464 mutex_lock(&smi_infos_lock);
3465 if (unload_when_empty && list_empty(&smi_infos)) {
3466 mutex_unlock(&smi_infos_lock);
3467 cleanup_ipmi_si();
3468 printk(KERN_WARNING PFX
3469 "Unable to find any System Interface(s)\n");
3470 return -ENODEV;
3471 } else {
3472 mutex_unlock(&smi_infos_lock);
3473 return 0;
3474 }
3475 }
3476 module_init(init_ipmi_si);
3477
cleanup_one_si(struct smi_info * to_clean)3478 static void cleanup_one_si(struct smi_info *to_clean)
3479 {
3480 int rv = 0;
3481 unsigned long flags;
3482
3483 if (!to_clean)
3484 return;
3485
3486 list_del(&to_clean->link);
3487
3488 /* Tell the driver that we are shutting down. */
3489 atomic_inc(&to_clean->stop_operation);
3490
3491 /*
3492 * Make sure the timer and thread are stopped and will not run
3493 * again.
3494 */
3495 wait_for_timer_and_thread(to_clean);
3496
3497 /*
3498 * Timeouts are stopped, now make sure the interrupts are off
3499 * for the device. A little tricky with locks to make sure
3500 * there are no races.
3501 */
3502 spin_lock_irqsave(&to_clean->si_lock, flags);
3503 while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
3504 spin_unlock_irqrestore(&to_clean->si_lock, flags);
3505 poll(to_clean);
3506 schedule_timeout_uninterruptible(1);
3507 spin_lock_irqsave(&to_clean->si_lock, flags);
3508 }
3509 disable_si_irq(to_clean);
3510 spin_unlock_irqrestore(&to_clean->si_lock, flags);
3511 while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
3512 poll(to_clean);
3513 schedule_timeout_uninterruptible(1);
3514 }
3515
3516 /* Clean up interrupts and make sure that everything is done. */
3517 if (to_clean->irq_cleanup)
3518 to_clean->irq_cleanup(to_clean);
3519 while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
3520 poll(to_clean);
3521 schedule_timeout_uninterruptible(1);
3522 }
3523
3524 if (to_clean->intf)
3525 rv = ipmi_unregister_smi(to_clean->intf);
3526
3527 if (rv) {
3528 printk(KERN_ERR PFX "Unable to unregister device: errno=%d\n",
3529 rv);
3530 }
3531
3532 if (to_clean->handlers)
3533 to_clean->handlers->cleanup(to_clean->si_sm);
3534
3535 kfree(to_clean->si_sm);
3536
3537 if (to_clean->addr_source_cleanup)
3538 to_clean->addr_source_cleanup(to_clean);
3539 if (to_clean->io_cleanup)
3540 to_clean->io_cleanup(to_clean);
3541
3542 if (to_clean->dev_registered)
3543 platform_device_unregister(to_clean->pdev);
3544
3545 kfree(to_clean);
3546 }
3547
cleanup_ipmi_si(void)3548 static void cleanup_ipmi_si(void)
3549 {
3550 struct smi_info *e, *tmp_e;
3551
3552 if (!initialized)
3553 return;
3554
3555 #ifdef CONFIG_PCI
3556 if (pci_registered)
3557 pci_unregister_driver(&ipmi_pci_driver);
3558 #endif
3559 #ifdef CONFIG_ACPI
3560 if (pnp_registered)
3561 pnp_unregister_driver(&ipmi_pnp_driver);
3562 #endif
3563
3564 platform_driver_unregister(&ipmi_driver);
3565
3566 mutex_lock(&smi_infos_lock);
3567 list_for_each_entry_safe(e, tmp_e, &smi_infos, link)
3568 cleanup_one_si(e);
3569 mutex_unlock(&smi_infos_lock);
3570 }
3571 module_exit(cleanup_ipmi_si);
3572
3573 MODULE_LICENSE("GPL");
3574 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
3575 MODULE_DESCRIPTION("Interface to the IPMI driver for the KCS, SMIC, and BT"
3576 " system interfaces.");
3577