blob: fa3be622ca97bbb4796241242126d21c9a825db3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 *
18 *
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
28 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * You should have received a copy of the GNU General Public License along
31 * with this program; if not, write to the Free Software Foundation, Inc.,
32 * 675 Mass Ave, Cambridge, MA 02139, USA.
33 */
34
35/*
36 * This file holds the "policy" for the interface to the SMI state
37 * machine. It does the configuration, handles timers and interrupts,
38 * and drives the real SMI state machine.
39 */
40
41#include <linux/config.h>
42#include <linux/module.h>
43#include <linux/moduleparam.h>
44#include <asm/system.h>
45#include <linux/sched.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>
Corey Minyardea940272005-11-07 00:59:59 -080054#include <linux/notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/irq.h>
56#ifdef CONFIG_HIGH_RES_TIMERS
57#include <linux/hrtime.h>
58# if defined(schedule_next_int)
59/* Old high-res timer code, do translations. */
60# define get_arch_cycles(a) quick_update_jiffies_sub(a)
61# define arch_cycles_per_jiffy cycles_per_jiffies
62# endif
63static inline void add_usec_to_timer(struct timer_list *t, long v)
64{
Corey Minyard75b07682005-09-06 15:18:38 -070065 t->arch_cycle_expires += nsec_to_arch_cycle(v * 1000);
66 while (t->arch_cycle_expires >= arch_cycles_per_jiffy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 {
68 t->expires++;
Corey Minyard75b07682005-09-06 15:18:38 -070069 t->arch_cycle_expires -= arch_cycles_per_jiffy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 }
71}
72#endif
73#include <linux/interrupt.h>
74#include <linux/rcupdate.h>
75#include <linux/ipmi_smi.h>
76#include <asm/io.h>
77#include "ipmi_si_sm.h"
78#include <linux/init.h>
Andrey Paninb224cd32005-09-06 15:18:37 -070079#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/* Measure times between events in the driver. */
82#undef DEBUG_TIMING
83
84/* Call every 10 ms. */
85#define SI_TIMEOUT_TIME_USEC 10000
86#define SI_USEC_PER_JIFFY (1000000/HZ)
87#define SI_TIMEOUT_JIFFIES (SI_TIMEOUT_TIME_USEC/SI_USEC_PER_JIFFY)
88#define SI_SHORT_TIMEOUT_USEC 250 /* .25ms when the SM request a
89 short timeout */
90
91enum si_intf_state {
92 SI_NORMAL,
93 SI_GETTING_FLAGS,
94 SI_GETTING_EVENTS,
95 SI_CLEARING_FLAGS,
96 SI_CLEARING_FLAGS_THEN_SET_IRQ,
97 SI_GETTING_MESSAGES,
98 SI_ENABLE_INTERRUPTS1,
99 SI_ENABLE_INTERRUPTS2
100 /* FIXME - add watchdog stuff. */
101};
102
Corey Minyard9dbf68f2005-05-01 08:59:11 -0700103/* Some BT-specific defines we need here. */
104#define IPMI_BT_INTMASK_REG 2
105#define IPMI_BT_INTMASK_CLEAR_IRQ_BIT 2
106#define IPMI_BT_INTMASK_ENABLE_IRQ_BIT 1
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108enum si_type {
109 SI_KCS, SI_SMIC, SI_BT
110};
111
Corey Minyard3ae0e0f2005-09-06 15:18:41 -0700112struct ipmi_device_id {
113 unsigned char device_id;
114 unsigned char device_revision;
115 unsigned char firmware_revision_1;
116 unsigned char firmware_revision_2;
117 unsigned char ipmi_version;
118 unsigned char additional_device_support;
119 unsigned char manufacturer_id[3];
120 unsigned char product_id[2];
121 unsigned char aux_firmware_revision[4];
122} __attribute__((packed));
123
124#define ipmi_version_major(v) ((v)->ipmi_version & 0xf)
125#define ipmi_version_minor(v) ((v)->ipmi_version >> 4)
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127struct smi_info
128{
Corey Minyarda9a2c442005-11-07 01:00:03 -0800129 int intf_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 ipmi_smi_t intf;
131 struct si_sm_data *si_sm;
132 struct si_sm_handlers *handlers;
133 enum si_type si_type;
134 spinlock_t si_lock;
135 spinlock_t msg_lock;
136 struct list_head xmit_msgs;
137 struct list_head hp_xmit_msgs;
138 struct ipmi_smi_msg *curr_msg;
139 enum si_intf_state si_state;
140
141 /* Used to handle the various types of I/O that can occur with
142 IPMI */
143 struct si_sm_io io;
144 int (*io_setup)(struct smi_info *info);
145 void (*io_cleanup)(struct smi_info *info);
146 int (*irq_setup)(struct smi_info *info);
147 void (*irq_cleanup)(struct smi_info *info);
148 unsigned int io_size;
149
Corey Minyard3ae0e0f2005-09-06 15:18:41 -0700150 /* Per-OEM handler, called from handle_flags().
151 Returns 1 when handle_flags() needs to be re-run
152 or 0 indicating it set si_state itself.
153 */
154 int (*oem_data_avail_handler)(struct smi_info *smi_info);
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 /* Flags from the last GET_MSG_FLAGS command, used when an ATTN
157 is set to hold the flags until we are done handling everything
158 from the flags. */
159#define RECEIVE_MSG_AVAIL 0x01
160#define EVENT_MSG_BUFFER_FULL 0x02
161#define WDT_PRE_TIMEOUT_INT 0x08
Corey Minyard3ae0e0f2005-09-06 15:18:41 -0700162#define OEM0_DATA_AVAIL 0x20
163#define OEM1_DATA_AVAIL 0x40
164#define OEM2_DATA_AVAIL 0x80
165#define OEM_DATA_AVAIL (OEM0_DATA_AVAIL | \
166 OEM1_DATA_AVAIL | \
167 OEM2_DATA_AVAIL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 unsigned char msg_flags;
169
170 /* If set to true, this will request events the next time the
171 state machine is idle. */
172 atomic_t req_events;
173
174 /* If true, run the state machine to completion on every send
175 call. Generally used after a panic to make sure stuff goes
176 out. */
177 int run_to_completion;
178
179 /* The I/O port of an SI interface. */
180 int port;
181
182 /* The space between start addresses of the two ports. For
183 instance, if the first port is 0xca2 and the spacing is 4, then
184 the second port is 0xca6. */
185 unsigned int spacing;
186
187 /* zero if no irq; */
188 int irq;
189
190 /* The timer for this si. */
191 struct timer_list si_timer;
192
193 /* The time (in jiffies) the last timeout occurred at. */
194 unsigned long last_timeout_jiffies;
195
196 /* Used to gracefully stop the timer without race conditions. */
Corey Minyarda9a2c442005-11-07 01:00:03 -0800197 atomic_t stop_operation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 /* The driver will disable interrupts when it gets into a
200 situation where it cannot handle messages due to lack of
201 memory. Once that situation clears up, it will re-enable
202 interrupts. */
203 int interrupt_disabled;
204
Corey Minyard3ae0e0f2005-09-06 15:18:41 -0700205 struct ipmi_device_id device_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 /* Slave address, could be reported from DMI. */
208 unsigned char slave_addr;
209
210 /* Counters and things for the proc filesystem. */
211 spinlock_t count_lock;
212 unsigned long short_timeouts;
213 unsigned long long_timeouts;
214 unsigned long timeout_restarts;
215 unsigned long idles;
216 unsigned long interrupts;
217 unsigned long attentions;
218 unsigned long flag_fetches;
219 unsigned long hosed_count;
220 unsigned long complete_transactions;
221 unsigned long events;
222 unsigned long watchdog_pretimeouts;
223 unsigned long incoming_messages;
Corey Minyarda9a2c442005-11-07 01:00:03 -0800224
225 struct completion exiting;
226 long thread_pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227};
228
Corey Minyardea940272005-11-07 00:59:59 -0800229static struct notifier_block *xaction_notifier_list;
230static int register_xaction_notifier(struct notifier_block * nb)
231{
232 return notifier_chain_register(&xaction_notifier_list, nb);
233}
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235static void si_restart_short_timer(struct smi_info *smi_info);
236
237static void deliver_recv_msg(struct smi_info *smi_info,
238 struct ipmi_smi_msg *msg)
239{
240 /* Deliver the message to the upper layer with the lock
241 released. */
242 spin_unlock(&(smi_info->si_lock));
243 ipmi_smi_msg_received(smi_info->intf, msg);
244 spin_lock(&(smi_info->si_lock));
245}
246
247static void return_hosed_msg(struct smi_info *smi_info)
248{
249 struct ipmi_smi_msg *msg = smi_info->curr_msg;
250
251 /* Make it a reponse */
252 msg->rsp[0] = msg->data[0] | 4;
253 msg->rsp[1] = msg->data[1];
254 msg->rsp[2] = 0xFF; /* Unknown error. */
255 msg->rsp_size = 3;
256
257 smi_info->curr_msg = NULL;
258 deliver_recv_msg(smi_info, msg);
259}
260
261static enum si_sm_result start_next_msg(struct smi_info *smi_info)
262{
263 int rv;
264 struct list_head *entry = NULL;
265#ifdef DEBUG_TIMING
266 struct timeval t;
267#endif
268
269 /* No need to save flags, we aleady have interrupts off and we
270 already hold the SMI lock. */
271 spin_lock(&(smi_info->msg_lock));
272
273 /* Pick the high priority queue first. */
274 if (! list_empty(&(smi_info->hp_xmit_msgs))) {
275 entry = smi_info->hp_xmit_msgs.next;
276 } else if (! list_empty(&(smi_info->xmit_msgs))) {
277 entry = smi_info->xmit_msgs.next;
278 }
279
Corey Minyarde8b33612005-09-06 15:18:45 -0700280 if (! entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 smi_info->curr_msg = NULL;
282 rv = SI_SM_IDLE;
283 } else {
284 int err;
285
286 list_del(entry);
287 smi_info->curr_msg = list_entry(entry,
288 struct ipmi_smi_msg,
289 link);
290#ifdef DEBUG_TIMING
291 do_gettimeofday(&t);
292 printk("**Start2: %d.%9.9d\n", t.tv_sec, t.tv_usec);
293#endif
Corey Minyardea940272005-11-07 00:59:59 -0800294 err = notifier_call_chain(&xaction_notifier_list, 0, smi_info);
295 if (err & NOTIFY_STOP_MASK) {
296 rv = SI_SM_CALL_WITHOUT_DELAY;
297 goto out;
298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 err = smi_info->handlers->start_transaction(
300 smi_info->si_sm,
301 smi_info->curr_msg->data,
302 smi_info->curr_msg->data_size);
303 if (err) {
304 return_hosed_msg(smi_info);
305 }
306
307 rv = SI_SM_CALL_WITHOUT_DELAY;
308 }
Corey Minyardea940272005-11-07 00:59:59 -0800309 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 spin_unlock(&(smi_info->msg_lock));
311
312 return rv;
313}
314
315static void start_enable_irq(struct smi_info *smi_info)
316{
317 unsigned char msg[2];
318
319 /* If we are enabling interrupts, we have to tell the
320 BMC to use them. */
321 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
322 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
323
324 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
325 smi_info->si_state = SI_ENABLE_INTERRUPTS1;
326}
327
328static void start_clear_flags(struct smi_info *smi_info)
329{
330 unsigned char msg[3];
331
332 /* Make sure the watchdog pre-timeout flag is not set at startup. */
333 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
334 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
335 msg[2] = WDT_PRE_TIMEOUT_INT;
336
337 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
338 smi_info->si_state = SI_CLEARING_FLAGS;
339}
340
341/* When we have a situtaion where we run out of memory and cannot
342 allocate messages, we just leave them in the BMC and run the system
343 polled until we can allocate some memory. Once we have some
344 memory, we will re-enable the interrupt. */
345static inline void disable_si_irq(struct smi_info *smi_info)
346{
Corey Minyarde8b33612005-09-06 15:18:45 -0700347 if ((smi_info->irq) && (! smi_info->interrupt_disabled)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 disable_irq_nosync(smi_info->irq);
349 smi_info->interrupt_disabled = 1;
350 }
351}
352
353static inline void enable_si_irq(struct smi_info *smi_info)
354{
355 if ((smi_info->irq) && (smi_info->interrupt_disabled)) {
356 enable_irq(smi_info->irq);
357 smi_info->interrupt_disabled = 0;
358 }
359}
360
361static void handle_flags(struct smi_info *smi_info)
362{
Corey Minyard3ae0e0f2005-09-06 15:18:41 -0700363 retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (smi_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
365 /* Watchdog pre-timeout */
366 spin_lock(&smi_info->count_lock);
367 smi_info->watchdog_pretimeouts++;
368 spin_unlock(&smi_info->count_lock);
369
370 start_clear_flags(smi_info);
371 smi_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
372 spin_unlock(&(smi_info->si_lock));
373 ipmi_smi_watchdog_pretimeout(smi_info->intf);
374 spin_lock(&(smi_info->si_lock));
375 } else if (smi_info->msg_flags & RECEIVE_MSG_AVAIL) {
376 /* Messages available. */
377 smi_info->curr_msg = ipmi_alloc_smi_msg();
Corey Minyarde8b33612005-09-06 15:18:45 -0700378 if (! smi_info->curr_msg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 disable_si_irq(smi_info);
380 smi_info->si_state = SI_NORMAL;
381 return;
382 }
383 enable_si_irq(smi_info);
384
385 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
386 smi_info->curr_msg->data[1] = IPMI_GET_MSG_CMD;
387 smi_info->curr_msg->data_size = 2;
388
389 smi_info->handlers->start_transaction(
390 smi_info->si_sm,
391 smi_info->curr_msg->data,
392 smi_info->curr_msg->data_size);
393 smi_info->si_state = SI_GETTING_MESSAGES;
394 } else if (smi_info->msg_flags & EVENT_MSG_BUFFER_FULL) {
395 /* Events available. */
396 smi_info->curr_msg = ipmi_alloc_smi_msg();
Corey Minyarde8b33612005-09-06 15:18:45 -0700397 if (! smi_info->curr_msg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 disable_si_irq(smi_info);
399 smi_info->si_state = SI_NORMAL;
400 return;
401 }
402 enable_si_irq(smi_info);
403
404 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
405 smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
406 smi_info->curr_msg->data_size = 2;
407
408 smi_info->handlers->start_transaction(
409 smi_info->si_sm,
410 smi_info->curr_msg->data,
411 smi_info->curr_msg->data_size);
412 smi_info->si_state = SI_GETTING_EVENTS;
Corey Minyard3ae0e0f2005-09-06 15:18:41 -0700413 } else if (smi_info->msg_flags & OEM_DATA_AVAIL) {
414 if (smi_info->oem_data_avail_handler)
415 if (smi_info->oem_data_avail_handler(smi_info))
416 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 } else {
418 smi_info->si_state = SI_NORMAL;
419 }
420}
421
422static void handle_transaction_done(struct smi_info *smi_info)
423{
424 struct ipmi_smi_msg *msg;
425#ifdef DEBUG_TIMING
426 struct timeval t;
427
428 do_gettimeofday(&t);
429 printk("**Done: %d.%9.9d\n", t.tv_sec, t.tv_usec);
430#endif
431 switch (smi_info->si_state) {
432 case SI_NORMAL:
Corey Minyarde8b33612005-09-06 15:18:45 -0700433 if (! smi_info->curr_msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 break;
435
436 smi_info->curr_msg->rsp_size
437 = smi_info->handlers->get_result(
438 smi_info->si_sm,
439 smi_info->curr_msg->rsp,
440 IPMI_MAX_MSG_LENGTH);
441
442 /* Do this here becase deliver_recv_msg() releases the
443 lock, and a new message can be put in during the
444 time the lock is released. */
445 msg = smi_info->curr_msg;
446 smi_info->curr_msg = NULL;
447 deliver_recv_msg(smi_info, msg);
448 break;
449
450 case SI_GETTING_FLAGS:
451 {
452 unsigned char msg[4];
453 unsigned int len;
454
455 /* We got the flags from the SMI, now handle them. */
456 len = smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
457 if (msg[2] != 0) {
458 /* Error fetching flags, just give up for
459 now. */
460 smi_info->si_state = SI_NORMAL;
461 } else if (len < 4) {
462 /* Hmm, no flags. That's technically illegal, but
463 don't use uninitialized data. */
464 smi_info->si_state = SI_NORMAL;
465 } else {
466 smi_info->msg_flags = msg[3];
467 handle_flags(smi_info);
468 }
469 break;
470 }
471
472 case SI_CLEARING_FLAGS:
473 case SI_CLEARING_FLAGS_THEN_SET_IRQ:
474 {
475 unsigned char msg[3];
476
477 /* We cleared the flags. */
478 smi_info->handlers->get_result(smi_info->si_sm, msg, 3);
479 if (msg[2] != 0) {
480 /* Error clearing flags */
481 printk(KERN_WARNING
482 "ipmi_si: Error clearing flags: %2.2x\n",
483 msg[2]);
484 }
485 if (smi_info->si_state == SI_CLEARING_FLAGS_THEN_SET_IRQ)
486 start_enable_irq(smi_info);
487 else
488 smi_info->si_state = SI_NORMAL;
489 break;
490 }
491
492 case SI_GETTING_EVENTS:
493 {
494 smi_info->curr_msg->rsp_size
495 = smi_info->handlers->get_result(
496 smi_info->si_sm,
497 smi_info->curr_msg->rsp,
498 IPMI_MAX_MSG_LENGTH);
499
500 /* Do this here becase deliver_recv_msg() releases the
501 lock, and a new message can be put in during the
502 time the lock is released. */
503 msg = smi_info->curr_msg;
504 smi_info->curr_msg = NULL;
505 if (msg->rsp[2] != 0) {
506 /* Error getting event, probably done. */
507 msg->done(msg);
508
509 /* Take off the event flag. */
510 smi_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
511 handle_flags(smi_info);
512 } else {
513 spin_lock(&smi_info->count_lock);
514 smi_info->events++;
515 spin_unlock(&smi_info->count_lock);
516
517 /* Do this before we deliver the message
518 because delivering the message releases the
519 lock and something else can mess with the
520 state. */
521 handle_flags(smi_info);
522
523 deliver_recv_msg(smi_info, msg);
524 }
525 break;
526 }
527
528 case SI_GETTING_MESSAGES:
529 {
530 smi_info->curr_msg->rsp_size
531 = smi_info->handlers->get_result(
532 smi_info->si_sm,
533 smi_info->curr_msg->rsp,
534 IPMI_MAX_MSG_LENGTH);
535
536 /* Do this here becase deliver_recv_msg() releases the
537 lock, and a new message can be put in during the
538 time the lock is released. */
539 msg = smi_info->curr_msg;
540 smi_info->curr_msg = NULL;
541 if (msg->rsp[2] != 0) {
542 /* Error getting event, probably done. */
543 msg->done(msg);
544
545 /* Take off the msg flag. */
546 smi_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
547 handle_flags(smi_info);
548 } else {
549 spin_lock(&smi_info->count_lock);
550 smi_info->incoming_messages++;
551 spin_unlock(&smi_info->count_lock);
552
553 /* Do this before we deliver the message
554 because delivering the message releases the
555 lock and something else can mess with the
556 state. */
557 handle_flags(smi_info);
558
559 deliver_recv_msg(smi_info, msg);
560 }
561 break;
562 }
563
564 case SI_ENABLE_INTERRUPTS1:
565 {
566 unsigned char msg[4];
567
568 /* We got the flags from the SMI, now handle them. */
569 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
570 if (msg[2] != 0) {
571 printk(KERN_WARNING
572 "ipmi_si: Could not enable interrupts"
573 ", failed get, using polled mode.\n");
574 smi_info->si_state = SI_NORMAL;
575 } else {
576 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
577 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
578 msg[2] = msg[3] | 1; /* enable msg queue int */
579 smi_info->handlers->start_transaction(
580 smi_info->si_sm, msg, 3);
581 smi_info->si_state = SI_ENABLE_INTERRUPTS2;
582 }
583 break;
584 }
585
586 case SI_ENABLE_INTERRUPTS2:
587 {
588 unsigned char msg[4];
589
590 /* We got the flags from the SMI, now handle them. */
591 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
592 if (msg[2] != 0) {
593 printk(KERN_WARNING
594 "ipmi_si: Could not enable interrupts"
595 ", failed set, using polled mode.\n");
596 }
597 smi_info->si_state = SI_NORMAL;
598 break;
599 }
600 }
601}
602
603/* Called on timeouts and events. Timeouts should pass the elapsed
604 time, interrupts should pass in zero. */
605static enum si_sm_result smi_event_handler(struct smi_info *smi_info,
606 int time)
607{
608 enum si_sm_result si_sm_result;
609
610 restart:
611 /* There used to be a loop here that waited a little while
612 (around 25us) before giving up. That turned out to be
613 pointless, the minimum delays I was seeing were in the 300us
614 range, which is far too long to wait in an interrupt. So
615 we just run until the state machine tells us something
616 happened or it needs a delay. */
617 si_sm_result = smi_info->handlers->event(smi_info->si_sm, time);
618 time = 0;
619 while (si_sm_result == SI_SM_CALL_WITHOUT_DELAY)
620 {
621 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
622 }
623
624 if (si_sm_result == SI_SM_TRANSACTION_COMPLETE)
625 {
626 spin_lock(&smi_info->count_lock);
627 smi_info->complete_transactions++;
628 spin_unlock(&smi_info->count_lock);
629
630 handle_transaction_done(smi_info);
631 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
632 }
633 else if (si_sm_result == SI_SM_HOSED)
634 {
635 spin_lock(&smi_info->count_lock);
636 smi_info->hosed_count++;
637 spin_unlock(&smi_info->count_lock);
638
639 /* Do the before return_hosed_msg, because that
640 releases the lock. */
641 smi_info->si_state = SI_NORMAL;
642 if (smi_info->curr_msg != NULL) {
643 /* If we were handling a user message, format
644 a response to send to the upper layer to
645 tell it about the error. */
646 return_hosed_msg(smi_info);
647 }
648 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
649 }
650
651 /* We prefer handling attn over new messages. */
652 if (si_sm_result == SI_SM_ATTN)
653 {
654 unsigned char msg[2];
655
656 spin_lock(&smi_info->count_lock);
657 smi_info->attentions++;
658 spin_unlock(&smi_info->count_lock);
659
660 /* Got a attn, send down a get message flags to see
661 what's causing it. It would be better to handle
662 this in the upper layer, but due to the way
663 interrupts work with the SMI, that's not really
664 possible. */
665 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
666 msg[1] = IPMI_GET_MSG_FLAGS_CMD;
667
668 smi_info->handlers->start_transaction(
669 smi_info->si_sm, msg, 2);
670 smi_info->si_state = SI_GETTING_FLAGS;
671 goto restart;
672 }
673
674 /* If we are currently idle, try to start the next message. */
675 if (si_sm_result == SI_SM_IDLE) {
676 spin_lock(&smi_info->count_lock);
677 smi_info->idles++;
678 spin_unlock(&smi_info->count_lock);
679
680 si_sm_result = start_next_msg(smi_info);
681 if (si_sm_result != SI_SM_IDLE)
682 goto restart;
683 }
684
685 if ((si_sm_result == SI_SM_IDLE)
686 && (atomic_read(&smi_info->req_events)))
687 {
688 /* We are idle and the upper layer requested that I fetch
689 events, so do so. */
690 unsigned char msg[2];
691
692 spin_lock(&smi_info->count_lock);
693 smi_info->flag_fetches++;
694 spin_unlock(&smi_info->count_lock);
695
696 atomic_set(&smi_info->req_events, 0);
697 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
698 msg[1] = IPMI_GET_MSG_FLAGS_CMD;
699
700 smi_info->handlers->start_transaction(
701 smi_info->si_sm, msg, 2);
702 smi_info->si_state = SI_GETTING_FLAGS;
703 goto restart;
704 }
705
706 return si_sm_result;
707}
708
709static void sender(void *send_info,
710 struct ipmi_smi_msg *msg,
711 int priority)
712{
713 struct smi_info *smi_info = send_info;
714 enum si_sm_result result;
715 unsigned long flags;
716#ifdef DEBUG_TIMING
717 struct timeval t;
718#endif
719
720 spin_lock_irqsave(&(smi_info->msg_lock), flags);
721#ifdef DEBUG_TIMING
722 do_gettimeofday(&t);
723 printk("**Enqueue: %d.%9.9d\n", t.tv_sec, t.tv_usec);
724#endif
725
726 if (smi_info->run_to_completion) {
727 /* If we are running to completion, then throw it in
728 the list and run transactions until everything is
729 clear. Priority doesn't matter here. */
730 list_add_tail(&(msg->link), &(smi_info->xmit_msgs));
731
732 /* We have to release the msg lock and claim the smi
733 lock in this case, because of race conditions. */
734 spin_unlock_irqrestore(&(smi_info->msg_lock), flags);
735
736 spin_lock_irqsave(&(smi_info->si_lock), flags);
737 result = smi_event_handler(smi_info, 0);
738 while (result != SI_SM_IDLE) {
739 udelay(SI_SHORT_TIMEOUT_USEC);
740 result = smi_event_handler(smi_info,
741 SI_SHORT_TIMEOUT_USEC);
742 }
743 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
744 return;
745 } else {
746 if (priority > 0) {
747 list_add_tail(&(msg->link), &(smi_info->hp_xmit_msgs));
748 } else {
749 list_add_tail(&(msg->link), &(smi_info->xmit_msgs));
750 }
751 }
752 spin_unlock_irqrestore(&(smi_info->msg_lock), flags);
753
754 spin_lock_irqsave(&(smi_info->si_lock), flags);
755 if ((smi_info->si_state == SI_NORMAL)
756 && (smi_info->curr_msg == NULL))
757 {
758 start_next_msg(smi_info);
759 si_restart_short_timer(smi_info);
760 }
761 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
762}
763
764static void set_run_to_completion(void *send_info, int i_run_to_completion)
765{
766 struct smi_info *smi_info = send_info;
767 enum si_sm_result result;
768 unsigned long flags;
769
770 spin_lock_irqsave(&(smi_info->si_lock), flags);
771
772 smi_info->run_to_completion = i_run_to_completion;
773 if (i_run_to_completion) {
774 result = smi_event_handler(smi_info, 0);
775 while (result != SI_SM_IDLE) {
776 udelay(SI_SHORT_TIMEOUT_USEC);
777 result = smi_event_handler(smi_info,
778 SI_SHORT_TIMEOUT_USEC);
779 }
780 }
781
782 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
783}
784
Corey Minyarda9a2c442005-11-07 01:00:03 -0800785static int ipmi_thread(void *data)
786{
787 struct smi_info *smi_info = data;
788 unsigned long flags, last=1;
789 enum si_sm_result smi_result;
790
791 daemonize("kipmi%d", smi_info->intf_num);
792 allow_signal(SIGKILL);
793 set_user_nice(current, 19);
794 while (!atomic_read(&smi_info->stop_operation)) {
795 schedule_timeout(last);
796 spin_lock_irqsave(&(smi_info->si_lock), flags);
797 smi_result=smi_event_handler(smi_info, 0);
798 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
799 if (smi_result == SI_SM_CALL_WITHOUT_DELAY)
800 last = 0;
801 else if (smi_result == SI_SM_CALL_WITH_DELAY) {
802 udelay(1);
803 last = 0;
804 }
805 else {
806 /* System is idle; go to sleep */
807 last = 1;
808 current->state = TASK_INTERRUPTIBLE;
809 }
810 }
811 smi_info->thread_pid = 0;
812 complete_and_exit(&(smi_info->exiting), 0);
813 return 0;
814}
815
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817static void poll(void *send_info)
818{
819 struct smi_info *smi_info = send_info;
820
821 smi_event_handler(smi_info, 0);
822}
823
824static void request_events(void *send_info)
825{
826 struct smi_info *smi_info = send_info;
827
828 atomic_set(&smi_info->req_events, 1);
829}
830
831static int initialized = 0;
832
833/* Must be called with interrupts off and with the si_lock held. */
834static void si_restart_short_timer(struct smi_info *smi_info)
835{
836#if defined(CONFIG_HIGH_RES_TIMERS)
837 unsigned long flags;
838 unsigned long jiffies_now;
Corey Minyard75b07682005-09-06 15:18:38 -0700839 unsigned long seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 if (del_timer(&(smi_info->si_timer))) {
842 /* If we don't delete the timer, then it will go off
843 immediately, anyway. So we only process if we
844 actually delete the timer. */
845
Corey Minyard75b07682005-09-06 15:18:38 -0700846 do {
847 seq = read_seqbegin_irqsave(&xtime_lock, flags);
848 jiffies_now = jiffies;
849 smi_info->si_timer.expires = jiffies_now;
850 smi_info->si_timer.arch_cycle_expires
851 = get_arch_cycles(jiffies_now);
852 } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 add_usec_to_timer(&smi_info->si_timer, SI_SHORT_TIMEOUT_USEC);
855
856 add_timer(&(smi_info->si_timer));
857 spin_lock_irqsave(&smi_info->count_lock, flags);
858 smi_info->timeout_restarts++;
859 spin_unlock_irqrestore(&smi_info->count_lock, flags);
860 }
861#endif
862}
863
864static void smi_timeout(unsigned long data)
865{
866 struct smi_info *smi_info = (struct smi_info *) data;
867 enum si_sm_result smi_result;
868 unsigned long flags;
869 unsigned long jiffies_now;
Corey Minyardc4edff12005-11-07 00:59:56 -0800870 long time_diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871#ifdef DEBUG_TIMING
872 struct timeval t;
873#endif
874
Corey Minyarda9a2c442005-11-07 01:00:03 -0800875 if (atomic_read(&smi_info->stop_operation))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 spin_lock_irqsave(&(smi_info->si_lock), flags);
879#ifdef DEBUG_TIMING
880 do_gettimeofday(&t);
881 printk("**Timer: %d.%9.9d\n", t.tv_sec, t.tv_usec);
882#endif
883 jiffies_now = jiffies;
Corey Minyardc4edff12005-11-07 00:59:56 -0800884 time_diff = (((long)jiffies_now - (long)smi_info->last_timeout_jiffies)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 * SI_USEC_PER_JIFFY);
886 smi_result = smi_event_handler(smi_info, time_diff);
887
888 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
889
890 smi_info->last_timeout_jiffies = jiffies_now;
891
892 if ((smi_info->irq) && (! smi_info->interrupt_disabled)) {
893 /* Running with interrupts, only do long timeouts. */
894 smi_info->si_timer.expires = jiffies + SI_TIMEOUT_JIFFIES;
895 spin_lock_irqsave(&smi_info->count_lock, flags);
896 smi_info->long_timeouts++;
897 spin_unlock_irqrestore(&smi_info->count_lock, flags);
898 goto do_add_timer;
899 }
900
901 /* If the state machine asks for a short delay, then shorten
902 the timer timeout. */
903 if (smi_result == SI_SM_CALL_WITH_DELAY) {
Corey Minyard75b07682005-09-06 15:18:38 -0700904#if defined(CONFIG_HIGH_RES_TIMERS)
905 unsigned long seq;
906#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 spin_lock_irqsave(&smi_info->count_lock, flags);
908 smi_info->short_timeouts++;
909 spin_unlock_irqrestore(&smi_info->count_lock, flags);
910#if defined(CONFIG_HIGH_RES_TIMERS)
Corey Minyard75b07682005-09-06 15:18:38 -0700911 do {
912 seq = read_seqbegin_irqsave(&xtime_lock, flags);
913 smi_info->si_timer.expires = jiffies;
914 smi_info->si_timer.arch_cycle_expires
915 = get_arch_cycles(smi_info->si_timer.expires);
916 } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 add_usec_to_timer(&smi_info->si_timer, SI_SHORT_TIMEOUT_USEC);
918#else
919 smi_info->si_timer.expires = jiffies + 1;
920#endif
921 } else {
922 spin_lock_irqsave(&smi_info->count_lock, flags);
923 smi_info->long_timeouts++;
924 spin_unlock_irqrestore(&smi_info->count_lock, flags);
925 smi_info->si_timer.expires = jiffies + SI_TIMEOUT_JIFFIES;
926#if defined(CONFIG_HIGH_RES_TIMERS)
Corey Minyard75b07682005-09-06 15:18:38 -0700927 smi_info->si_timer.arch_cycle_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928#endif
929 }
930
931 do_add_timer:
932 add_timer(&(smi_info->si_timer));
933}
934
935static irqreturn_t si_irq_handler(int irq, void *data, struct pt_regs *regs)
936{
937 struct smi_info *smi_info = data;
938 unsigned long flags;
939#ifdef DEBUG_TIMING
940 struct timeval t;
941#endif
942
943 spin_lock_irqsave(&(smi_info->si_lock), flags);
944
945 spin_lock(&smi_info->count_lock);
946 smi_info->interrupts++;
947 spin_unlock(&smi_info->count_lock);
948
Corey Minyarda9a2c442005-11-07 01:00:03 -0800949 if (atomic_read(&smi_info->stop_operation))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 goto out;
951
952#ifdef DEBUG_TIMING
953 do_gettimeofday(&t);
954 printk("**Interrupt: %d.%9.9d\n", t.tv_sec, t.tv_usec);
955#endif
956 smi_event_handler(smi_info, 0);
957 out:
958 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
959 return IRQ_HANDLED;
960}
961
Corey Minyard9dbf68f2005-05-01 08:59:11 -0700962static irqreturn_t si_bt_irq_handler(int irq, void *data, struct pt_regs *regs)
963{
964 struct smi_info *smi_info = data;
965 /* We need to clear the IRQ flag for the BT interface. */
966 smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
967 IPMI_BT_INTMASK_CLEAR_IRQ_BIT
968 | IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
969 return si_irq_handler(irq, data, regs);
970}
971
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973static struct ipmi_smi_handlers handlers =
974{
975 .owner = THIS_MODULE,
976 .sender = sender,
977 .request_events = request_events,
978 .set_run_to_completion = set_run_to_completion,
979 .poll = poll,
980};
981
982/* There can be 4 IO ports passed in (with or without IRQs), 4 addresses,
983 a default IO port, and 1 ACPI/SPMI address. That sets SI_MAX_DRIVERS */
984
985#define SI_MAX_PARMS 4
986#define SI_MAX_DRIVERS ((SI_MAX_PARMS * 2) + 2)
987static struct smi_info *smi_infos[SI_MAX_DRIVERS] =
988{ NULL, NULL, NULL, NULL };
989
990#define DEVICE_NAME "ipmi_si"
991
992#define DEFAULT_KCS_IO_PORT 0xca2
993#define DEFAULT_SMIC_IO_PORT 0xca9
994#define DEFAULT_BT_IO_PORT 0xe4
995#define DEFAULT_REGSPACING 1
996
997static int si_trydefaults = 1;
998static char *si_type[SI_MAX_PARMS];
999#define MAX_SI_TYPE_STR 30
1000static char si_type_str[MAX_SI_TYPE_STR];
1001static unsigned long addrs[SI_MAX_PARMS];
1002static int num_addrs;
1003static unsigned int ports[SI_MAX_PARMS];
1004static int num_ports;
1005static int irqs[SI_MAX_PARMS];
1006static int num_irqs;
1007static int regspacings[SI_MAX_PARMS];
1008static int num_regspacings = 0;
1009static int regsizes[SI_MAX_PARMS];
1010static int num_regsizes = 0;
1011static int regshifts[SI_MAX_PARMS];
1012static int num_regshifts = 0;
1013static int slave_addrs[SI_MAX_PARMS];
1014static int num_slave_addrs = 0;
1015
1016
1017module_param_named(trydefaults, si_trydefaults, bool, 0);
1018MODULE_PARM_DESC(trydefaults, "Setting this to 'false' will disable the"
1019 " default scan of the KCS and SMIC interface at the standard"
1020 " address");
1021module_param_string(type, si_type_str, MAX_SI_TYPE_STR, 0);
1022MODULE_PARM_DESC(type, "Defines the type of each interface, each"
1023 " interface separated by commas. The types are 'kcs',"
1024 " 'smic', and 'bt'. For example si_type=kcs,bt will set"
1025 " the first interface to kcs and the second to bt");
1026module_param_array(addrs, long, &num_addrs, 0);
1027MODULE_PARM_DESC(addrs, "Sets the memory address of each interface, the"
1028 " addresses separated by commas. Only use if an interface"
1029 " is in memory. Otherwise, set it to zero or leave"
1030 " it blank.");
1031module_param_array(ports, int, &num_ports, 0);
1032MODULE_PARM_DESC(ports, "Sets the port address of each interface, the"
1033 " addresses separated by commas. Only use if an interface"
1034 " is a port. Otherwise, set it to zero or leave"
1035 " it blank.");
1036module_param_array(irqs, int, &num_irqs, 0);
1037MODULE_PARM_DESC(irqs, "Sets the interrupt of each interface, the"
1038 " addresses separated by commas. Only use if an interface"
1039 " has an interrupt. Otherwise, set it to zero or leave"
1040 " it blank.");
1041module_param_array(regspacings, int, &num_regspacings, 0);
1042MODULE_PARM_DESC(regspacings, "The number of bytes between the start address"
1043 " and each successive register used by the interface. For"
1044 " instance, if the start address is 0xca2 and the spacing"
1045 " is 2, then the second address is at 0xca4. Defaults"
1046 " to 1.");
1047module_param_array(regsizes, int, &num_regsizes, 0);
1048MODULE_PARM_DESC(regsizes, "The size of the specific IPMI register in bytes."
1049 " This should generally be 1, 2, 4, or 8 for an 8-bit,"
1050 " 16-bit, 32-bit, or 64-bit register. Use this if you"
1051 " the 8-bit IPMI register has to be read from a larger"
1052 " register.");
1053module_param_array(regshifts, int, &num_regshifts, 0);
1054MODULE_PARM_DESC(regshifts, "The amount to shift the data read from the."
1055 " IPMI register, in bits. For instance, if the data"
1056 " is read from a 32-bit word and the IPMI data is in"
1057 " bit 8-15, then the shift would be 8");
1058module_param_array(slave_addrs, int, &num_slave_addrs, 0);
1059MODULE_PARM_DESC(slave_addrs, "Set the default IPMB slave address for"
1060 " the controller. Normally this is 0x20, but can be"
1061 " overridden by this parm. This is an array indexed"
1062 " by interface number.");
1063
1064
1065#define IPMI_MEM_ADDR_SPACE 1
1066#define IPMI_IO_ADDR_SPACE 2
1067
Len Brown84663612005-08-24 12:09:07 -04001068#if defined(CONFIG_ACPI) || defined(CONFIG_X86) || defined(CONFIG_PCI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069static int is_new_interface(int intf, u8 addr_space, unsigned long base_addr)
1070{
1071 int i;
1072
1073 for (i = 0; i < SI_MAX_PARMS; ++i) {
1074 /* Don't check our address. */
1075 if (i == intf)
1076 continue;
1077 if (si_type[i] != NULL) {
1078 if ((addr_space == IPMI_MEM_ADDR_SPACE &&
1079 base_addr == addrs[i]) ||
1080 (addr_space == IPMI_IO_ADDR_SPACE &&
1081 base_addr == ports[i]))
1082 return 0;
1083 }
1084 else
1085 break;
1086 }
1087
1088 return 1;
1089}
1090#endif
1091
1092static int std_irq_setup(struct smi_info *info)
1093{
1094 int rv;
1095
Corey Minyarde8b33612005-09-06 15:18:45 -07001096 if (! info->irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return 0;
1098
Corey Minyard9dbf68f2005-05-01 08:59:11 -07001099 if (info->si_type == SI_BT) {
1100 rv = request_irq(info->irq,
1101 si_bt_irq_handler,
1102 SA_INTERRUPT,
1103 DEVICE_NAME,
1104 info);
Corey Minyarde8b33612005-09-06 15:18:45 -07001105 if (! rv)
Corey Minyard9dbf68f2005-05-01 08:59:11 -07001106 /* Enable the interrupt in the BT interface. */
1107 info->io.outputb(&info->io, IPMI_BT_INTMASK_REG,
1108 IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
1109 } else
1110 rv = request_irq(info->irq,
1111 si_irq_handler,
1112 SA_INTERRUPT,
1113 DEVICE_NAME,
1114 info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (rv) {
1116 printk(KERN_WARNING
1117 "ipmi_si: %s unable to claim interrupt %d,"
1118 " running polled\n",
1119 DEVICE_NAME, info->irq);
1120 info->irq = 0;
1121 } else {
1122 printk(" Using irq %d\n", info->irq);
1123 }
1124
1125 return rv;
1126}
1127
1128static void std_irq_cleanup(struct smi_info *info)
1129{
Corey Minyarde8b33612005-09-06 15:18:45 -07001130 if (! info->irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return;
1132
Corey Minyard9dbf68f2005-05-01 08:59:11 -07001133 if (info->si_type == SI_BT)
1134 /* Disable the interrupt in the BT interface. */
1135 info->io.outputb(&info->io, IPMI_BT_INTMASK_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 free_irq(info->irq, info);
1137}
1138
1139static unsigned char port_inb(struct si_sm_io *io, unsigned int offset)
1140{
1141 unsigned int *addr = io->info;
1142
1143 return inb((*addr)+(offset*io->regspacing));
1144}
1145
1146static void port_outb(struct si_sm_io *io, unsigned int offset,
1147 unsigned char b)
1148{
1149 unsigned int *addr = io->info;
1150
1151 outb(b, (*addr)+(offset * io->regspacing));
1152}
1153
1154static unsigned char port_inw(struct si_sm_io *io, unsigned int offset)
1155{
1156 unsigned int *addr = io->info;
1157
1158 return (inw((*addr)+(offset * io->regspacing)) >> io->regshift) & 0xff;
1159}
1160
1161static void port_outw(struct si_sm_io *io, unsigned int offset,
1162 unsigned char b)
1163{
1164 unsigned int *addr = io->info;
1165
1166 outw(b << io->regshift, (*addr)+(offset * io->regspacing));
1167}
1168
1169static unsigned char port_inl(struct si_sm_io *io, unsigned int offset)
1170{
1171 unsigned int *addr = io->info;
1172
1173 return (inl((*addr)+(offset * io->regspacing)) >> io->regshift) & 0xff;
1174}
1175
1176static void port_outl(struct si_sm_io *io, unsigned int offset,
1177 unsigned char b)
1178{
1179 unsigned int *addr = io->info;
1180
1181 outl(b << io->regshift, (*addr)+(offset * io->regspacing));
1182}
1183
1184static void port_cleanup(struct smi_info *info)
1185{
1186 unsigned int *addr = info->io.info;
1187 int mapsize;
1188
1189 if (addr && (*addr)) {
1190 mapsize = ((info->io_size * info->io.regspacing)
1191 - (info->io.regspacing - info->io.regsize));
1192
1193 release_region (*addr, mapsize);
1194 }
1195 kfree(info);
1196}
1197
1198static int port_setup(struct smi_info *info)
1199{
1200 unsigned int *addr = info->io.info;
1201 int mapsize;
1202
Corey Minyarde8b33612005-09-06 15:18:45 -07001203 if (! addr || (! *addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return -ENODEV;
1205
1206 info->io_cleanup = port_cleanup;
1207
1208 /* Figure out the actual inb/inw/inl/etc routine to use based
1209 upon the register size. */
1210 switch (info->io.regsize) {
1211 case 1:
1212 info->io.inputb = port_inb;
1213 info->io.outputb = port_outb;
1214 break;
1215 case 2:
1216 info->io.inputb = port_inw;
1217 info->io.outputb = port_outw;
1218 break;
1219 case 4:
1220 info->io.inputb = port_inl;
1221 info->io.outputb = port_outl;
1222 break;
1223 default:
1224 printk("ipmi_si: Invalid register size: %d\n",
1225 info->io.regsize);
1226 return -EINVAL;
1227 }
1228
1229 /* Calculate the total amount of memory to claim. This is an
1230 * unusual looking calculation, but it avoids claiming any
1231 * more memory than it has to. It will claim everything
1232 * between the first address to the end of the last full
1233 * register. */
1234 mapsize = ((info->io_size * info->io.regspacing)
1235 - (info->io.regspacing - info->io.regsize));
1236
1237 if (request_region(*addr, mapsize, DEVICE_NAME) == NULL)
1238 return -EIO;
1239 return 0;
1240}
1241
1242static int try_init_port(int intf_num, struct smi_info **new_info)
1243{
1244 struct smi_info *info;
1245
Corey Minyarde8b33612005-09-06 15:18:45 -07001246 if (! ports[intf_num])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return -ENODEV;
1248
Corey Minyarde8b33612005-09-06 15:18:45 -07001249 if (! is_new_interface(intf_num, IPMI_IO_ADDR_SPACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 ports[intf_num]))
1251 return -ENODEV;
1252
1253 info = kmalloc(sizeof(*info), GFP_KERNEL);
Corey Minyarde8b33612005-09-06 15:18:45 -07001254 if (! info) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 printk(KERN_ERR "ipmi_si: Could not allocate SI data (1)\n");
1256 return -ENOMEM;
1257 }
1258 memset(info, 0, sizeof(*info));
1259
1260 info->io_setup = port_setup;
1261 info->io.info = &(ports[intf_num]);
1262 info->io.addr = NULL;
1263 info->io.regspacing = regspacings[intf_num];
Corey Minyarde8b33612005-09-06 15:18:45 -07001264 if (! info->io.regspacing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 info->io.regspacing = DEFAULT_REGSPACING;
1266 info->io.regsize = regsizes[intf_num];
Corey Minyarde8b33612005-09-06 15:18:45 -07001267 if (! info->io.regsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 info->io.regsize = DEFAULT_REGSPACING;
1269 info->io.regshift = regshifts[intf_num];
1270 info->irq = 0;
1271 info->irq_setup = NULL;
1272 *new_info = info;
1273
1274 if (si_type[intf_num] == NULL)
1275 si_type[intf_num] = "kcs";
1276
1277 printk("ipmi_si: Trying \"%s\" at I/O port 0x%x\n",
1278 si_type[intf_num], ports[intf_num]);
1279 return 0;
1280}
1281
1282static unsigned char mem_inb(struct si_sm_io *io, unsigned int offset)
1283{
1284 return readb((io->addr)+(offset * io->regspacing));
1285}
1286
1287static void mem_outb(struct si_sm_io *io, unsigned int offset,
1288 unsigned char b)
1289{
1290 writeb(b, (io->addr)+(offset * io->regspacing));
1291}
1292
1293static unsigned char mem_inw(struct si_sm_io *io, unsigned int offset)
1294{
1295 return (readw((io->addr)+(offset * io->regspacing)) >> io->regshift)
1296 && 0xff;
1297}
1298
1299static void mem_outw(struct si_sm_io *io, unsigned int offset,
1300 unsigned char b)
1301{
1302 writeb(b << io->regshift, (io->addr)+(offset * io->regspacing));
1303}
1304
1305static unsigned char mem_inl(struct si_sm_io *io, unsigned int offset)
1306{
1307 return (readl((io->addr)+(offset * io->regspacing)) >> io->regshift)
1308 && 0xff;
1309}
1310
1311static void mem_outl(struct si_sm_io *io, unsigned int offset,
1312 unsigned char b)
1313{
1314 writel(b << io->regshift, (io->addr)+(offset * io->regspacing));
1315}
1316
1317#ifdef readq
1318static unsigned char mem_inq(struct si_sm_io *io, unsigned int offset)
1319{
1320 return (readq((io->addr)+(offset * io->regspacing)) >> io->regshift)
1321 && 0xff;
1322}
1323
1324static void mem_outq(struct si_sm_io *io, unsigned int offset,
1325 unsigned char b)
1326{
1327 writeq(b << io->regshift, (io->addr)+(offset * io->regspacing));
1328}
1329#endif
1330
1331static void mem_cleanup(struct smi_info *info)
1332{
1333 unsigned long *addr = info->io.info;
1334 int mapsize;
1335
1336 if (info->io.addr) {
1337 iounmap(info->io.addr);
1338
1339 mapsize = ((info->io_size * info->io.regspacing)
1340 - (info->io.regspacing - info->io.regsize));
1341
1342 release_mem_region(*addr, mapsize);
1343 }
1344 kfree(info);
1345}
1346
1347static int mem_setup(struct smi_info *info)
1348{
1349 unsigned long *addr = info->io.info;
1350 int mapsize;
1351
Corey Minyarde8b33612005-09-06 15:18:45 -07001352 if (! addr || (! *addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return -ENODEV;
1354
1355 info->io_cleanup = mem_cleanup;
1356
1357 /* Figure out the actual readb/readw/readl/etc routine to use based
1358 upon the register size. */
1359 switch (info->io.regsize) {
1360 case 1:
1361 info->io.inputb = mem_inb;
1362 info->io.outputb = mem_outb;
1363 break;
1364 case 2:
1365 info->io.inputb = mem_inw;
1366 info->io.outputb = mem_outw;
1367 break;
1368 case 4:
1369 info->io.inputb = mem_inl;
1370 info->io.outputb = mem_outl;
1371 break;
1372#ifdef readq
1373 case 8:
1374 info->io.inputb = mem_inq;
1375 info->io.outputb = mem_outq;
1376 break;
1377#endif
1378 default:
1379 printk("ipmi_si: Invalid register size: %d\n",
1380 info->io.regsize);
1381 return -EINVAL;
1382 }
1383
1384 /* Calculate the total amount of memory to claim. This is an
1385 * unusual looking calculation, but it avoids claiming any
1386 * more memory than it has to. It will claim everything
1387 * between the first address to the end of the last full
1388 * register. */
1389 mapsize = ((info->io_size * info->io.regspacing)
1390 - (info->io.regspacing - info->io.regsize));
1391
1392 if (request_mem_region(*addr, mapsize, DEVICE_NAME) == NULL)
1393 return -EIO;
1394
1395 info->io.addr = ioremap(*addr, mapsize);
1396 if (info->io.addr == NULL) {
1397 release_mem_region(*addr, mapsize);
1398 return -EIO;
1399 }
1400 return 0;
1401}
1402
1403static int try_init_mem(int intf_num, struct smi_info **new_info)
1404{
1405 struct smi_info *info;
1406
Corey Minyarde8b33612005-09-06 15:18:45 -07001407 if (! addrs[intf_num])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return -ENODEV;
1409
Corey Minyarde8b33612005-09-06 15:18:45 -07001410 if (! is_new_interface(intf_num, IPMI_MEM_ADDR_SPACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 addrs[intf_num]))
1412 return -ENODEV;
1413
1414 info = kmalloc(sizeof(*info), GFP_KERNEL);
Corey Minyarde8b33612005-09-06 15:18:45 -07001415 if (! info) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 printk(KERN_ERR "ipmi_si: Could not allocate SI data (2)\n");
1417 return -ENOMEM;
1418 }
1419 memset(info, 0, sizeof(*info));
1420
1421 info->io_setup = mem_setup;
1422 info->io.info = &addrs[intf_num];
1423 info->io.addr = NULL;
1424 info->io.regspacing = regspacings[intf_num];
Corey Minyarde8b33612005-09-06 15:18:45 -07001425 if (! info->io.regspacing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 info->io.regspacing = DEFAULT_REGSPACING;
1427 info->io.regsize = regsizes[intf_num];
Corey Minyarde8b33612005-09-06 15:18:45 -07001428 if (! info->io.regsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 info->io.regsize = DEFAULT_REGSPACING;
1430 info->io.regshift = regshifts[intf_num];
1431 info->irq = 0;
1432 info->irq_setup = NULL;
1433 *new_info = info;
1434
1435 if (si_type[intf_num] == NULL)
1436 si_type[intf_num] = "kcs";
1437
1438 printk("ipmi_si: Trying \"%s\" at memory address 0x%lx\n",
1439 si_type[intf_num], addrs[intf_num]);
1440 return 0;
1441}
1442
1443
Len Brown84663612005-08-24 12:09:07 -04001444#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446#include <linux/acpi.h>
1447
1448/* Once we get an ACPI failure, we don't try any more, because we go
1449 through the tables sequentially. Once we don't find a table, there
1450 are no more. */
1451static int acpi_failure = 0;
1452
1453/* For GPE-type interrupts. */
1454static u32 ipmi_acpi_gpe(void *context)
1455{
1456 struct smi_info *smi_info = context;
1457 unsigned long flags;
1458#ifdef DEBUG_TIMING
1459 struct timeval t;
1460#endif
1461
1462 spin_lock_irqsave(&(smi_info->si_lock), flags);
1463
1464 spin_lock(&smi_info->count_lock);
1465 smi_info->interrupts++;
1466 spin_unlock(&smi_info->count_lock);
1467
Corey Minyarda9a2c442005-11-07 01:00:03 -08001468 if (atomic_read(&smi_info->stop_operation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 goto out;
1470
1471#ifdef DEBUG_TIMING
1472 do_gettimeofday(&t);
1473 printk("**ACPI_GPE: %d.%9.9d\n", t.tv_sec, t.tv_usec);
1474#endif
1475 smi_event_handler(smi_info, 0);
1476 out:
1477 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1478
1479 return ACPI_INTERRUPT_HANDLED;
1480}
1481
1482static int acpi_gpe_irq_setup(struct smi_info *info)
1483{
1484 acpi_status status;
1485
Corey Minyarde8b33612005-09-06 15:18:45 -07001486 if (! info->irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 return 0;
1488
1489 /* FIXME - is level triggered right? */
1490 status = acpi_install_gpe_handler(NULL,
1491 info->irq,
1492 ACPI_GPE_LEVEL_TRIGGERED,
1493 &ipmi_acpi_gpe,
1494 info);
1495 if (status != AE_OK) {
1496 printk(KERN_WARNING
1497 "ipmi_si: %s unable to claim ACPI GPE %d,"
1498 " running polled\n",
1499 DEVICE_NAME, info->irq);
1500 info->irq = 0;
1501 return -EINVAL;
1502 } else {
1503 printk(" Using ACPI GPE %d\n", info->irq);
1504 return 0;
1505 }
1506}
1507
1508static void acpi_gpe_irq_cleanup(struct smi_info *info)
1509{
Corey Minyarde8b33612005-09-06 15:18:45 -07001510 if (! info->irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 return;
1512
1513 acpi_remove_gpe_handler(NULL, info->irq, &ipmi_acpi_gpe);
1514}
1515
1516/*
1517 * Defined at
1518 * http://h21007.www2.hp.com/dspp/files/unprotected/devresource/Docs/TechPapers/IA64/hpspmi.pdf
1519 */
1520struct SPMITable {
1521 s8 Signature[4];
1522 u32 Length;
1523 u8 Revision;
1524 u8 Checksum;
1525 s8 OEMID[6];
1526 s8 OEMTableID[8];
1527 s8 OEMRevision[4];
1528 s8 CreatorID[4];
1529 s8 CreatorRevision[4];
1530 u8 InterfaceType;
1531 u8 IPMIlegacy;
1532 s16 SpecificationRevision;
1533
1534 /*
1535 * Bit 0 - SCI interrupt supported
1536 * Bit 1 - I/O APIC/SAPIC
1537 */
1538 u8 InterruptType;
1539
1540 /* If bit 0 of InterruptType is set, then this is the SCI
1541 interrupt in the GPEx_STS register. */
1542 u8 GPE;
1543
1544 s16 Reserved;
1545
1546 /* If bit 1 of InterruptType is set, then this is the I/O
1547 APIC/SAPIC interrupt. */
1548 u32 GlobalSystemInterrupt;
1549
1550 /* The actual register address. */
1551 struct acpi_generic_address addr;
1552
1553 u8 UID[4];
1554
1555 s8 spmi_id[1]; /* A '\0' terminated array starts here. */
1556};
1557
1558static int try_init_acpi(int intf_num, struct smi_info **new_info)
1559{
1560 struct smi_info *info;
1561 acpi_status status;
1562 struct SPMITable *spmi;
1563 char *io_type;
1564 u8 addr_space;
1565
Yann Droneaud4fbd1512005-06-07 16:54:01 +02001566 if (acpi_disabled)
1567 return -ENODEV;
1568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if (acpi_failure)
1570 return -ENODEV;
1571
1572 status = acpi_get_firmware_table("SPMI", intf_num+1,
1573 ACPI_LOGICAL_ADDRESSING,
1574 (struct acpi_table_header **) &spmi);
1575 if (status != AE_OK) {
1576 acpi_failure = 1;
1577 return -ENODEV;
1578 }
1579
1580 if (spmi->IPMIlegacy != 1) {
1581 printk(KERN_INFO "IPMI: Bad SPMI legacy %d\n", spmi->IPMIlegacy);
1582 return -ENODEV;
1583 }
1584
1585 if (spmi->addr.address_space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
1586 addr_space = IPMI_MEM_ADDR_SPACE;
1587 else
1588 addr_space = IPMI_IO_ADDR_SPACE;
Corey Minyarde8b33612005-09-06 15:18:45 -07001589 if (! is_new_interface(-1, addr_space, spmi->addr.address))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 return -ENODEV;
1591
Corey Minyarde8b33612005-09-06 15:18:45 -07001592 if (! spmi->addr.register_bit_width) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 acpi_failure = 1;
1594 return -ENODEV;
1595 }
1596
1597 /* Figure out the interface type. */
1598 switch (spmi->InterfaceType)
1599 {
1600 case 1: /* KCS */
1601 si_type[intf_num] = "kcs";
1602 break;
1603
1604 case 2: /* SMIC */
1605 si_type[intf_num] = "smic";
1606 break;
1607
1608 case 3: /* BT */
1609 si_type[intf_num] = "bt";
1610 break;
1611
1612 default:
1613 printk(KERN_INFO "ipmi_si: Unknown ACPI/SPMI SI type %d\n",
1614 spmi->InterfaceType);
1615 return -EIO;
1616 }
1617
1618 info = kmalloc(sizeof(*info), GFP_KERNEL);
Corey Minyarde8b33612005-09-06 15:18:45 -07001619 if (! info) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 printk(KERN_ERR "ipmi_si: Could not allocate SI data (3)\n");
1621 return -ENOMEM;
1622 }
1623 memset(info, 0, sizeof(*info));
1624
1625 if (spmi->InterruptType & 1) {
1626 /* We've got a GPE interrupt. */
1627 info->irq = spmi->GPE;
1628 info->irq_setup = acpi_gpe_irq_setup;
1629 info->irq_cleanup = acpi_gpe_irq_cleanup;
1630 } else if (spmi->InterruptType & 2) {
1631 /* We've got an APIC/SAPIC interrupt. */
1632 info->irq = spmi->GlobalSystemInterrupt;
1633 info->irq_setup = std_irq_setup;
1634 info->irq_cleanup = std_irq_cleanup;
1635 } else {
1636 /* Use the default interrupt setting. */
1637 info->irq = 0;
1638 info->irq_setup = NULL;
1639 }
1640
Corey Minyard35bc37a2005-05-01 08:59:10 -07001641 if (spmi->addr.register_bit_width) {
1642 /* A (hopefully) properly formed register bit width. */
1643 regspacings[intf_num] = spmi->addr.register_bit_width / 8;
1644 info->io.regspacing = spmi->addr.register_bit_width / 8;
1645 } else {
1646 /* Some broken systems get this wrong and set the value
1647 * to zero. Assume it is the default spacing. If that
1648 * is wrong, too bad, the vendor should fix the tables. */
1649 regspacings[intf_num] = DEFAULT_REGSPACING;
1650 info->io.regspacing = DEFAULT_REGSPACING;
1651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 regsizes[intf_num] = regspacings[intf_num];
1653 info->io.regsize = regsizes[intf_num];
1654 regshifts[intf_num] = spmi->addr.register_bit_offset;
1655 info->io.regshift = regshifts[intf_num];
1656
1657 if (spmi->addr.address_space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
1658 io_type = "memory";
1659 info->io_setup = mem_setup;
1660 addrs[intf_num] = spmi->addr.address;
1661 info->io.info = &(addrs[intf_num]);
1662 } else if (spmi->addr.address_space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
1663 io_type = "I/O";
1664 info->io_setup = port_setup;
1665 ports[intf_num] = spmi->addr.address;
1666 info->io.info = &(ports[intf_num]);
1667 } else {
1668 kfree(info);
1669 printk("ipmi_si: Unknown ACPI I/O Address type\n");
1670 return -EIO;
1671 }
1672
1673 *new_info = info;
1674
1675 printk("ipmi_si: ACPI/SPMI specifies \"%s\" %s SI @ 0x%lx\n",
1676 si_type[intf_num], io_type, (unsigned long) spmi->addr.address);
1677 return 0;
1678}
1679#endif
1680
1681#ifdef CONFIG_X86
1682typedef struct dmi_ipmi_data
1683{
1684 u8 type;
1685 u8 addr_space;
1686 unsigned long base_addr;
1687 u8 irq;
1688 u8 offset;
1689 u8 slave_addr;
1690} dmi_ipmi_data_t;
1691
1692static dmi_ipmi_data_t dmi_data[SI_MAX_DRIVERS];
1693static int dmi_data_entries;
1694
Andrey Paninb224cd32005-09-06 15:18:37 -07001695static int __init decode_dmi(struct dmi_header *dm, int intf_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
Corey Minyarde8b33612005-09-06 15:18:45 -07001697 u8 *data = (u8 *)dm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 unsigned long base_addr;
1699 u8 reg_spacing;
Andrey Paninb224cd32005-09-06 15:18:37 -07001700 u8 len = dm->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 dmi_ipmi_data_t *ipmi_data = dmi_data+intf_num;
1702
Andrey Paninb224cd32005-09-06 15:18:37 -07001703 ipmi_data->type = data[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705 memcpy(&base_addr, data+8, sizeof(unsigned long));
1706 if (len >= 0x11) {
1707 if (base_addr & 1) {
1708 /* I/O */
1709 base_addr &= 0xFFFE;
1710 ipmi_data->addr_space = IPMI_IO_ADDR_SPACE;
1711 }
1712 else {
1713 /* Memory */
1714 ipmi_data->addr_space = IPMI_MEM_ADDR_SPACE;
1715 }
1716 /* If bit 4 of byte 0x10 is set, then the lsb for the address
1717 is odd. */
Andrey Paninb224cd32005-09-06 15:18:37 -07001718 ipmi_data->base_addr = base_addr | ((data[0x10] & 0x10) >> 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Andrey Paninb224cd32005-09-06 15:18:37 -07001720 ipmi_data->irq = data[0x11];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 /* The top two bits of byte 0x10 hold the register spacing. */
Andrey Paninb224cd32005-09-06 15:18:37 -07001723 reg_spacing = (data[0x10] & 0xC0) >> 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 switch(reg_spacing){
1725 case 0x00: /* Byte boundaries */
1726 ipmi_data->offset = 1;
1727 break;
1728 case 0x01: /* 32-bit boundaries */
1729 ipmi_data->offset = 4;
1730 break;
1731 case 0x02: /* 16-byte boundaries */
1732 ipmi_data->offset = 16;
1733 break;
1734 default:
1735 /* Some other interface, just ignore it. */
1736 return -EIO;
1737 }
1738 } else {
1739 /* Old DMI spec. */
Corey Minyard92068802005-05-01 08:59:10 -07001740 /* Note that technically, the lower bit of the base
1741 * address should be 1 if the address is I/O and 0 if
1742 * the address is in memory. So many systems get that
1743 * wrong (and all that I have seen are I/O) so we just
1744 * ignore that bit and assume I/O. Systems that use
1745 * memory should use the newer spec, anyway. */
1746 ipmi_data->base_addr = base_addr & 0xfffe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 ipmi_data->addr_space = IPMI_IO_ADDR_SPACE;
1748 ipmi_data->offset = 1;
1749 }
1750
Andrey Paninb224cd32005-09-06 15:18:37 -07001751 ipmi_data->slave_addr = data[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 if (is_new_interface(-1, ipmi_data->addr_space,ipmi_data->base_addr)) {
1754 dmi_data_entries++;
1755 return 0;
1756 }
1757
1758 memset(ipmi_data, 0, sizeof(dmi_ipmi_data_t));
1759
1760 return -1;
1761}
1762
Andrey Paninb224cd32005-09-06 15:18:37 -07001763static void __init dmi_find_bmc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
Andrey Paninb224cd32005-09-06 15:18:37 -07001765 struct dmi_device *dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 int intf_num = 0;
1767
Andrey Paninb224cd32005-09-06 15:18:37 -07001768 while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev))) {
1769 if (intf_num >= SI_MAX_DRIVERS)
1770 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Andrey Paninb224cd32005-09-06 15:18:37 -07001772 decode_dmi((struct dmi_header *) dev->device_data, intf_num++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
1776static int try_init_smbios(int intf_num, struct smi_info **new_info)
1777{
Corey Minyarde8b33612005-09-06 15:18:45 -07001778 struct smi_info *info;
1779 dmi_ipmi_data_t *ipmi_data = dmi_data+intf_num;
1780 char *io_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 if (intf_num >= dmi_data_entries)
1783 return -ENODEV;
1784
Corey Minyarde8b33612005-09-06 15:18:45 -07001785 switch (ipmi_data->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 case 0x01: /* KCS */
1787 si_type[intf_num] = "kcs";
1788 break;
1789 case 0x02: /* SMIC */
1790 si_type[intf_num] = "smic";
1791 break;
1792 case 0x03: /* BT */
1793 si_type[intf_num] = "bt";
1794 break;
1795 default:
1796 return -EIO;
1797 }
1798
1799 info = kmalloc(sizeof(*info), GFP_KERNEL);
Corey Minyarde8b33612005-09-06 15:18:45 -07001800 if (! info) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 printk(KERN_ERR "ipmi_si: Could not allocate SI data (4)\n");
1802 return -ENOMEM;
1803 }
1804 memset(info, 0, sizeof(*info));
1805
1806 if (ipmi_data->addr_space == 1) {
1807 io_type = "memory";
1808 info->io_setup = mem_setup;
1809 addrs[intf_num] = ipmi_data->base_addr;
1810 info->io.info = &(addrs[intf_num]);
1811 } else if (ipmi_data->addr_space == 2) {
1812 io_type = "I/O";
1813 info->io_setup = port_setup;
1814 ports[intf_num] = ipmi_data->base_addr;
1815 info->io.info = &(ports[intf_num]);
1816 } else {
1817 kfree(info);
1818 printk("ipmi_si: Unknown SMBIOS I/O Address type.\n");
1819 return -EIO;
1820 }
1821
1822 regspacings[intf_num] = ipmi_data->offset;
1823 info->io.regspacing = regspacings[intf_num];
Corey Minyarde8b33612005-09-06 15:18:45 -07001824 if (! info->io.regspacing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 info->io.regspacing = DEFAULT_REGSPACING;
1826 info->io.regsize = DEFAULT_REGSPACING;
1827 info->io.regshift = regshifts[intf_num];
1828
1829 info->slave_addr = ipmi_data->slave_addr;
1830
1831 irqs[intf_num] = ipmi_data->irq;
1832
1833 *new_info = info;
1834
1835 printk("ipmi_si: Found SMBIOS-specified state machine at %s"
1836 " address 0x%lx, slave address 0x%x\n",
1837 io_type, (unsigned long)ipmi_data->base_addr,
1838 ipmi_data->slave_addr);
1839 return 0;
1840}
1841#endif /* CONFIG_X86 */
1842
1843#ifdef CONFIG_PCI
1844
1845#define PCI_ERMC_CLASSCODE 0x0C0700
1846#define PCI_HP_VENDOR_ID 0x103C
1847#define PCI_MMC_DEVICE_ID 0x121A
1848#define PCI_MMC_ADDR_CW 0x10
1849
1850/* Avoid more than one attempt to probe pci smic. */
1851static int pci_smic_checked = 0;
1852
1853static int find_pci_smic(int intf_num, struct smi_info **new_info)
1854{
1855 struct smi_info *info;
1856 int error;
1857 struct pci_dev *pci_dev = NULL;
1858 u16 base_addr;
1859 int fe_rmc = 0;
1860
1861 if (pci_smic_checked)
1862 return -ENODEV;
1863
1864 pci_smic_checked = 1;
1865
Corey Minyarde8b33612005-09-06 15:18:45 -07001866 pci_dev = pci_get_device(PCI_HP_VENDOR_ID, PCI_MMC_DEVICE_ID, NULL);
1867 if (! pci_dev) {
1868 pci_dev = pci_get_class(PCI_ERMC_CLASSCODE, NULL);
1869 if (pci_dev && (pci_dev->subsystem_vendor == PCI_HP_VENDOR_ID))
1870 fe_rmc = 1;
1871 else
1872 return -ENODEV;
1873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 error = pci_read_config_word(pci_dev, PCI_MMC_ADDR_CW, &base_addr);
1876 if (error)
1877 {
1878 pci_dev_put(pci_dev);
1879 printk(KERN_ERR
1880 "ipmi_si: pci_read_config_word() failed (%d).\n",
1881 error);
1882 return -ENODEV;
1883 }
1884
1885 /* Bit 0: 1 specifies programmed I/O, 0 specifies memory mapped I/O */
Corey Minyarde8b33612005-09-06 15:18:45 -07001886 if (! (base_addr & 0x0001))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 {
1888 pci_dev_put(pci_dev);
1889 printk(KERN_ERR
1890 "ipmi_si: memory mapped I/O not supported for PCI"
1891 " smic.\n");
1892 return -ENODEV;
1893 }
1894
1895 base_addr &= 0xFFFE;
Corey Minyarde8b33612005-09-06 15:18:45 -07001896 if (! fe_rmc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 /* Data register starts at base address + 1 in eRMC */
1898 ++base_addr;
1899
Corey Minyarde8b33612005-09-06 15:18:45 -07001900 if (! is_new_interface(-1, IPMI_IO_ADDR_SPACE, base_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 pci_dev_put(pci_dev);
1902 return -ENODEV;
1903 }
1904
1905 info = kmalloc(sizeof(*info), GFP_KERNEL);
Corey Minyarde8b33612005-09-06 15:18:45 -07001906 if (! info) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 pci_dev_put(pci_dev);
1908 printk(KERN_ERR "ipmi_si: Could not allocate SI data (5)\n");
1909 return -ENOMEM;
1910 }
1911 memset(info, 0, sizeof(*info));
1912
1913 info->io_setup = port_setup;
1914 ports[intf_num] = base_addr;
1915 info->io.info = &(ports[intf_num]);
1916 info->io.regspacing = regspacings[intf_num];
Corey Minyarde8b33612005-09-06 15:18:45 -07001917 if (! info->io.regspacing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 info->io.regspacing = DEFAULT_REGSPACING;
1919 info->io.regsize = DEFAULT_REGSPACING;
1920 info->io.regshift = regshifts[intf_num];
1921
1922 *new_info = info;
1923
1924 irqs[intf_num] = pci_dev->irq;
1925 si_type[intf_num] = "smic";
1926
1927 printk("ipmi_si: Found PCI SMIC at I/O address 0x%lx\n",
1928 (long unsigned int) base_addr);
1929
1930 pci_dev_put(pci_dev);
1931 return 0;
1932}
1933#endif /* CONFIG_PCI */
1934
1935static int try_init_plug_and_play(int intf_num, struct smi_info **new_info)
1936{
1937#ifdef CONFIG_PCI
Corey Minyarde8b33612005-09-06 15:18:45 -07001938 if (find_pci_smic(intf_num, new_info) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 return 0;
1940#endif
1941 /* Include other methods here. */
1942
1943 return -ENODEV;
1944}
1945
1946
1947static int try_get_dev_id(struct smi_info *smi_info)
1948{
1949 unsigned char msg[2];
1950 unsigned char *resp;
1951 unsigned long resp_len;
1952 enum si_sm_result smi_result;
1953 int rv = 0;
1954
1955 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
Corey Minyarde8b33612005-09-06 15:18:45 -07001956 if (! resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return -ENOMEM;
1958
1959 /* Do a Get Device ID command, since it comes back with some
1960 useful info. */
1961 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1962 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1963 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
1964
1965 smi_result = smi_info->handlers->event(smi_info->si_sm, 0);
1966 for (;;)
1967 {
Corey Minyardc3e7e792005-11-07 01:00:02 -08001968 if (smi_result == SI_SM_CALL_WITH_DELAY ||
1969 smi_result == SI_SM_CALL_WITH_TICK_DELAY) {
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001970 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 smi_result = smi_info->handlers->event(
1972 smi_info->si_sm, 100);
1973 }
1974 else if (smi_result == SI_SM_CALL_WITHOUT_DELAY)
1975 {
1976 smi_result = smi_info->handlers->event(
1977 smi_info->si_sm, 0);
1978 }
1979 else
1980 break;
1981 }
1982 if (smi_result == SI_SM_HOSED) {
1983 /* We couldn't get the state machine to run, so whatever's at
1984 the port is probably not an IPMI SMI interface. */
1985 rv = -ENODEV;
1986 goto out;
1987 }
1988
1989 /* Otherwise, we got some data. */
1990 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
1991 resp, IPMI_MAX_MSG_LENGTH);
1992 if (resp_len < 6) {
1993 /* That's odd, it should be longer. */
1994 rv = -EINVAL;
1995 goto out;
1996 }
1997
1998 if ((resp[1] != IPMI_GET_DEVICE_ID_CMD) || (resp[2] != 0)) {
1999 /* That's odd, it shouldn't be able to fail. */
2000 rv = -EINVAL;
2001 goto out;
2002 }
2003
2004 /* Record info from the get device id, in case we need it. */
Corey Minyard3ae0e0f2005-09-06 15:18:41 -07002005 memcpy(&smi_info->device_id, &resp[3],
2006 min_t(unsigned long, resp_len-3, sizeof(smi_info->device_id)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 out:
2009 kfree(resp);
2010 return rv;
2011}
2012
2013static int type_file_read_proc(char *page, char **start, off_t off,
2014 int count, int *eof, void *data)
2015{
2016 char *out = (char *) page;
2017 struct smi_info *smi = data;
2018
2019 switch (smi->si_type) {
2020 case SI_KCS:
2021 return sprintf(out, "kcs\n");
2022 case SI_SMIC:
2023 return sprintf(out, "smic\n");
2024 case SI_BT:
2025 return sprintf(out, "bt\n");
2026 default:
2027 return 0;
2028 }
2029}
2030
2031static int stat_file_read_proc(char *page, char **start, off_t off,
2032 int count, int *eof, void *data)
2033{
2034 char *out = (char *) page;
2035 struct smi_info *smi = data;
2036
2037 out += sprintf(out, "interrupts_enabled: %d\n",
Corey Minyarde8b33612005-09-06 15:18:45 -07002038 smi->irq && ! smi->interrupt_disabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 out += sprintf(out, "short_timeouts: %ld\n",
2040 smi->short_timeouts);
2041 out += sprintf(out, "long_timeouts: %ld\n",
2042 smi->long_timeouts);
2043 out += sprintf(out, "timeout_restarts: %ld\n",
2044 smi->timeout_restarts);
2045 out += sprintf(out, "idles: %ld\n",
2046 smi->idles);
2047 out += sprintf(out, "interrupts: %ld\n",
2048 smi->interrupts);
2049 out += sprintf(out, "attentions: %ld\n",
2050 smi->attentions);
2051 out += sprintf(out, "flag_fetches: %ld\n",
2052 smi->flag_fetches);
2053 out += sprintf(out, "hosed_count: %ld\n",
2054 smi->hosed_count);
2055 out += sprintf(out, "complete_transactions: %ld\n",
2056 smi->complete_transactions);
2057 out += sprintf(out, "events: %ld\n",
2058 smi->events);
2059 out += sprintf(out, "watchdog_pretimeouts: %ld\n",
2060 smi->watchdog_pretimeouts);
2061 out += sprintf(out, "incoming_messages: %ld\n",
2062 smi->incoming_messages);
2063
2064 return (out - ((char *) page));
2065}
2066
Corey Minyard3ae0e0f2005-09-06 15:18:41 -07002067/*
2068 * oem_data_avail_to_receive_msg_avail
2069 * @info - smi_info structure with msg_flags set
2070 *
2071 * Converts flags from OEM_DATA_AVAIL to RECEIVE_MSG_AVAIL
2072 * Returns 1 indicating need to re-run handle_flags().
2073 */
2074static int oem_data_avail_to_receive_msg_avail(struct smi_info *smi_info)
2075{
Corey Minyarde8b33612005-09-06 15:18:45 -07002076 smi_info->msg_flags = ((smi_info->msg_flags & ~OEM_DATA_AVAIL) |
2077 RECEIVE_MSG_AVAIL);
Corey Minyard3ae0e0f2005-09-06 15:18:41 -07002078 return 1;
2079}
2080
2081/*
2082 * setup_dell_poweredge_oem_data_handler
2083 * @info - smi_info.device_id must be populated
2084 *
2085 * Systems that match, but have firmware version < 1.40 may assert
2086 * OEM0_DATA_AVAIL on their own, without being told via Set Flags that
2087 * it's safe to do so. Such systems will de-assert OEM1_DATA_AVAIL
2088 * upon receipt of IPMI_GET_MSG_CMD, so we should treat these flags
2089 * as RECEIVE_MSG_AVAIL instead.
2090 *
2091 * As Dell has no plans to release IPMI 1.5 firmware that *ever*
2092 * assert the OEM[012] bits, and if it did, the driver would have to
2093 * change to handle that properly, we don't actually check for the
2094 * firmware version.
2095 * Device ID = 0x20 BMC on PowerEdge 8G servers
2096 * Device Revision = 0x80
2097 * Firmware Revision1 = 0x01 BMC version 1.40
2098 * Firmware Revision2 = 0x40 BCD encoded
2099 * IPMI Version = 0x51 IPMI 1.5
2100 * Manufacturer ID = A2 02 00 Dell IANA
2101 *
Corey Minyardd5a2b892005-11-07 00:59:58 -08002102 * Additionally, PowerEdge systems with IPMI < 1.5 may also assert
2103 * OEM0_DATA_AVAIL and needs to be treated as RECEIVE_MSG_AVAIL.
2104 *
Corey Minyard3ae0e0f2005-09-06 15:18:41 -07002105 */
2106#define DELL_POWEREDGE_8G_BMC_DEVICE_ID 0x20
2107#define DELL_POWEREDGE_8G_BMC_DEVICE_REV 0x80
2108#define DELL_POWEREDGE_8G_BMC_IPMI_VERSION 0x51
2109#define DELL_IANA_MFR_ID {0xA2, 0x02, 0x00}
2110static void setup_dell_poweredge_oem_data_handler(struct smi_info *smi_info)
2111{
2112 struct ipmi_device_id *id = &smi_info->device_id;
2113 const char mfr[3]=DELL_IANA_MFR_ID;
Corey Minyardd5a2b892005-11-07 00:59:58 -08002114 if (! memcmp(mfr, id->manufacturer_id, sizeof(mfr))) {
2115 if (id->device_id == DELL_POWEREDGE_8G_BMC_DEVICE_ID &&
2116 id->device_revision == DELL_POWEREDGE_8G_BMC_DEVICE_REV &&
2117 id->ipmi_version == DELL_POWEREDGE_8G_BMC_IPMI_VERSION) {
2118 smi_info->oem_data_avail_handler =
2119 oem_data_avail_to_receive_msg_avail;
2120 }
2121 else if (ipmi_version_major(id) < 1 ||
2122 (ipmi_version_major(id) == 1 &&
2123 ipmi_version_minor(id) < 5)) {
2124 smi_info->oem_data_avail_handler =
2125 oem_data_avail_to_receive_msg_avail;
2126 }
Corey Minyard3ae0e0f2005-09-06 15:18:41 -07002127 }
2128}
2129
Corey Minyardea940272005-11-07 00:59:59 -08002130#define CANNOT_RETURN_REQUESTED_LENGTH 0xCA
2131static void return_hosed_msg_badsize(struct smi_info *smi_info)
2132{
2133 struct ipmi_smi_msg *msg = smi_info->curr_msg;
2134
2135 /* Make it a reponse */
2136 msg->rsp[0] = msg->data[0] | 4;
2137 msg->rsp[1] = msg->data[1];
2138 msg->rsp[2] = CANNOT_RETURN_REQUESTED_LENGTH;
2139 msg->rsp_size = 3;
2140 smi_info->curr_msg = NULL;
2141 deliver_recv_msg(smi_info, msg);
2142}
2143
2144/*
2145 * dell_poweredge_bt_xaction_handler
2146 * @info - smi_info.device_id must be populated
2147 *
2148 * Dell PowerEdge servers with the BT interface (x6xx and 1750) will
2149 * not respond to a Get SDR command if the length of the data
2150 * requested is exactly 0x3A, which leads to command timeouts and no
2151 * data returned. This intercepts such commands, and causes userspace
2152 * callers to try again with a different-sized buffer, which succeeds.
2153 */
2154
2155#define STORAGE_NETFN 0x0A
2156#define STORAGE_CMD_GET_SDR 0x23
2157static int dell_poweredge_bt_xaction_handler(struct notifier_block *self,
2158 unsigned long unused,
2159 void *in)
2160{
2161 struct smi_info *smi_info = in;
2162 unsigned char *data = smi_info->curr_msg->data;
2163 unsigned int size = smi_info->curr_msg->data_size;
2164 if (size >= 8 &&
2165 (data[0]>>2) == STORAGE_NETFN &&
2166 data[1] == STORAGE_CMD_GET_SDR &&
2167 data[7] == 0x3A) {
2168 return_hosed_msg_badsize(smi_info);
2169 return NOTIFY_STOP;
2170 }
2171 return NOTIFY_DONE;
2172}
2173
2174static struct notifier_block dell_poweredge_bt_xaction_notifier = {
2175 .notifier_call = dell_poweredge_bt_xaction_handler,
2176};
2177
2178/*
2179 * setup_dell_poweredge_bt_xaction_handler
2180 * @info - smi_info.device_id must be filled in already
2181 *
2182 * Fills in smi_info.device_id.start_transaction_pre_hook
2183 * when we know what function to use there.
2184 */
2185static void
2186setup_dell_poweredge_bt_xaction_handler(struct smi_info *smi_info)
2187{
2188 struct ipmi_device_id *id = &smi_info->device_id;
2189 const char mfr[3]=DELL_IANA_MFR_ID;
2190 if (! memcmp(mfr, id->manufacturer_id, sizeof(mfr)) &&
2191 smi_info->si_type == SI_BT)
2192 register_xaction_notifier(&dell_poweredge_bt_xaction_notifier);
2193}
2194
Corey Minyard3ae0e0f2005-09-06 15:18:41 -07002195/*
2196 * setup_oem_data_handler
2197 * @info - smi_info.device_id must be filled in already
2198 *
2199 * Fills in smi_info.device_id.oem_data_available_handler
2200 * when we know what function to use there.
2201 */
2202
2203static void setup_oem_data_handler(struct smi_info *smi_info)
2204{
2205 setup_dell_poweredge_oem_data_handler(smi_info);
2206}
2207
Corey Minyardea940272005-11-07 00:59:59 -08002208static void setup_xaction_handlers(struct smi_info *smi_info)
2209{
2210 setup_dell_poweredge_bt_xaction_handler(smi_info);
2211}
2212
Corey Minyarda9a2c442005-11-07 01:00:03 -08002213static inline void wait_for_timer_and_thread(struct smi_info *smi_info)
2214{
2215 if (smi_info->thread_pid > 0) {
2216 /* wake the potentially sleeping thread */
2217 kill_proc(smi_info->thread_pid, SIGKILL, 0);
2218 wait_for_completion(&(smi_info->exiting));
2219 }
2220 del_timer_sync(&smi_info->si_timer);
2221}
2222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223/* Returns 0 if initialized, or negative on an error. */
2224static int init_one_smi(int intf_num, struct smi_info **smi)
2225{
2226 int rv;
2227 struct smi_info *new_smi;
2228
2229
2230 rv = try_init_mem(intf_num, &new_smi);
2231 if (rv)
2232 rv = try_init_port(intf_num, &new_smi);
Len Brown84663612005-08-24 12:09:07 -04002233#ifdef CONFIG_ACPI
Corey Minyarde8b33612005-09-06 15:18:45 -07002234 if (rv && si_trydefaults)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 rv = try_init_acpi(intf_num, &new_smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236#endif
2237#ifdef CONFIG_X86
Corey Minyarde8b33612005-09-06 15:18:45 -07002238 if (rv && si_trydefaults)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 rv = try_init_smbios(intf_num, &new_smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240#endif
Corey Minyarde8b33612005-09-06 15:18:45 -07002241 if (rv && si_trydefaults)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 rv = try_init_plug_and_play(intf_num, &new_smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
2244 if (rv)
2245 return rv;
2246
2247 /* So we know not to free it unless we have allocated one. */
2248 new_smi->intf = NULL;
2249 new_smi->si_sm = NULL;
2250 new_smi->handlers = NULL;
2251
Corey Minyarde8b33612005-09-06 15:18:45 -07002252 if (! new_smi->irq_setup) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 new_smi->irq = irqs[intf_num];
2254 new_smi->irq_setup = std_irq_setup;
2255 new_smi->irq_cleanup = std_irq_cleanup;
2256 }
2257
2258 /* Default to KCS if no type is specified. */
2259 if (si_type[intf_num] == NULL) {
2260 if (si_trydefaults)
2261 si_type[intf_num] = "kcs";
2262 else {
2263 rv = -EINVAL;
2264 goto out_err;
2265 }
2266 }
2267
2268 /* Set up the state machine to use. */
2269 if (strcmp(si_type[intf_num], "kcs") == 0) {
2270 new_smi->handlers = &kcs_smi_handlers;
2271 new_smi->si_type = SI_KCS;
2272 } else if (strcmp(si_type[intf_num], "smic") == 0) {
2273 new_smi->handlers = &smic_smi_handlers;
2274 new_smi->si_type = SI_SMIC;
2275 } else if (strcmp(si_type[intf_num], "bt") == 0) {
2276 new_smi->handlers = &bt_smi_handlers;
2277 new_smi->si_type = SI_BT;
2278 } else {
2279 /* No support for anything else yet. */
2280 rv = -EIO;
2281 goto out_err;
2282 }
2283
2284 /* Allocate the state machine's data and initialize it. */
2285 new_smi->si_sm = kmalloc(new_smi->handlers->size(), GFP_KERNEL);
Corey Minyarde8b33612005-09-06 15:18:45 -07002286 if (! new_smi->si_sm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 printk(" Could not allocate state machine memory\n");
2288 rv = -ENOMEM;
2289 goto out_err;
2290 }
2291 new_smi->io_size = new_smi->handlers->init_data(new_smi->si_sm,
2292 &new_smi->io);
2293
2294 /* Now that we know the I/O size, we can set up the I/O. */
2295 rv = new_smi->io_setup(new_smi);
2296 if (rv) {
2297 printk(" Could not set up I/O space\n");
2298 goto out_err;
2299 }
2300
2301 spin_lock_init(&(new_smi->si_lock));
2302 spin_lock_init(&(new_smi->msg_lock));
2303 spin_lock_init(&(new_smi->count_lock));
2304
2305 /* Do low-level detection first. */
2306 if (new_smi->handlers->detect(new_smi->si_sm)) {
2307 rv = -ENODEV;
2308 goto out_err;
2309 }
2310
2311 /* Attempt a get device id command. If it fails, we probably
2312 don't have a SMI here. */
2313 rv = try_get_dev_id(new_smi);
2314 if (rv)
2315 goto out_err;
2316
Corey Minyard3ae0e0f2005-09-06 15:18:41 -07002317 setup_oem_data_handler(new_smi);
Corey Minyardea940272005-11-07 00:59:59 -08002318 setup_xaction_handlers(new_smi);
Corey Minyard3ae0e0f2005-09-06 15:18:41 -07002319
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 /* Try to claim any interrupts. */
2321 new_smi->irq_setup(new_smi);
2322
2323 INIT_LIST_HEAD(&(new_smi->xmit_msgs));
2324 INIT_LIST_HEAD(&(new_smi->hp_xmit_msgs));
2325 new_smi->curr_msg = NULL;
2326 atomic_set(&new_smi->req_events, 0);
2327 new_smi->run_to_completion = 0;
2328
2329 new_smi->interrupt_disabled = 0;
Corey Minyarda9a2c442005-11-07 01:00:03 -08002330 atomic_set(&new_smi->stop_operation, 0);
2331 new_smi->intf_num = intf_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
2333 /* Start clearing the flags before we enable interrupts or the
2334 timer to avoid racing with the timer. */
2335 start_clear_flags(new_smi);
2336 /* IRQ is defined to be set when non-zero. */
2337 if (new_smi->irq)
2338 new_smi->si_state = SI_CLEARING_FLAGS_THEN_SET_IRQ;
2339
2340 /* The ipmi_register_smi() code does some operations to
2341 determine the channel information, so we must be ready to
2342 handle operations before it is called. This means we have
2343 to stop the timer if we get an error after this point. */
2344 init_timer(&(new_smi->si_timer));
2345 new_smi->si_timer.data = (long) new_smi;
2346 new_smi->si_timer.function = smi_timeout;
2347 new_smi->last_timeout_jiffies = jiffies;
2348 new_smi->si_timer.expires = jiffies + SI_TIMEOUT_JIFFIES;
Corey Minyarda9a2c442005-11-07 01:00:03 -08002349
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 add_timer(&(new_smi->si_timer));
Corey Minyarda9a2c442005-11-07 01:00:03 -08002351 if (new_smi->si_type != SI_BT) {
2352 init_completion(&(new_smi->exiting));
2353 new_smi->thread_pid = kernel_thread(ipmi_thread, new_smi,
2354 CLONE_FS|CLONE_FILES|
2355 CLONE_SIGHAND);
2356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
2358 rv = ipmi_register_smi(&handlers,
2359 new_smi,
Corey Minyard3ae0e0f2005-09-06 15:18:41 -07002360 ipmi_version_major(&new_smi->device_id),
2361 ipmi_version_minor(&new_smi->device_id),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 new_smi->slave_addr,
2363 &(new_smi->intf));
2364 if (rv) {
2365 printk(KERN_ERR
2366 "ipmi_si: Unable to register device: error %d\n",
2367 rv);
2368 goto out_err_stop_timer;
2369 }
2370
2371 rv = ipmi_smi_add_proc_entry(new_smi->intf, "type",
2372 type_file_read_proc, NULL,
2373 new_smi, THIS_MODULE);
2374 if (rv) {
2375 printk(KERN_ERR
2376 "ipmi_si: Unable to create proc entry: %d\n",
2377 rv);
2378 goto out_err_stop_timer;
2379 }
2380
2381 rv = ipmi_smi_add_proc_entry(new_smi->intf, "si_stats",
2382 stat_file_read_proc, NULL,
2383 new_smi, THIS_MODULE);
2384 if (rv) {
2385 printk(KERN_ERR
2386 "ipmi_si: Unable to create proc entry: %d\n",
2387 rv);
2388 goto out_err_stop_timer;
2389 }
2390
2391 *smi = new_smi;
2392
2393 printk(" IPMI %s interface initialized\n", si_type[intf_num]);
2394
2395 return 0;
2396
2397 out_err_stop_timer:
Corey Minyarda9a2c442005-11-07 01:00:03 -08002398 atomic_inc(&new_smi->stop_operation);
2399 wait_for_timer_and_thread(new_smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
2401 out_err:
2402 if (new_smi->intf)
2403 ipmi_unregister_smi(new_smi->intf);
2404
2405 new_smi->irq_cleanup(new_smi);
2406
2407 /* Wait until we know that we are out of any interrupt
2408 handlers might have been running before we freed the
2409 interrupt. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002410 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
2412 if (new_smi->si_sm) {
2413 if (new_smi->handlers)
2414 new_smi->handlers->cleanup(new_smi->si_sm);
2415 kfree(new_smi->si_sm);
2416 }
2417 new_smi->io_cleanup(new_smi);
2418
2419 return rv;
2420}
2421
2422static __init int init_ipmi_si(void)
2423{
2424 int rv = 0;
2425 int pos = 0;
2426 int i;
2427 char *str;
2428
2429 if (initialized)
2430 return 0;
2431 initialized = 1;
2432
2433 /* Parse out the si_type string into its components. */
2434 str = si_type_str;
2435 if (*str != '\0') {
Corey Minyarde8b33612005-09-06 15:18:45 -07002436 for (i = 0; (i < SI_MAX_PARMS) && (*str != '\0'); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 si_type[i] = str;
2438 str = strchr(str, ',');
2439 if (str) {
2440 *str = '\0';
2441 str++;
2442 } else {
2443 break;
2444 }
2445 }
2446 }
2447
Corey Minyard1fdd75b2005-09-06 15:18:42 -07002448 printk(KERN_INFO "IPMI System Interface driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
2450#ifdef CONFIG_X86
Andrey Paninb224cd32005-09-06 15:18:37 -07002451 dmi_find_bmc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452#endif
2453
2454 rv = init_one_smi(0, &(smi_infos[pos]));
Corey Minyarde8b33612005-09-06 15:18:45 -07002455 if (rv && ! ports[0] && si_trydefaults) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 /* If we are trying defaults and the initial port is
2457 not set, then set it. */
2458 si_type[0] = "kcs";
2459 ports[0] = DEFAULT_KCS_IO_PORT;
2460 rv = init_one_smi(0, &(smi_infos[pos]));
2461 if (rv) {
2462 /* No KCS - try SMIC */
2463 si_type[0] = "smic";
2464 ports[0] = DEFAULT_SMIC_IO_PORT;
2465 rv = init_one_smi(0, &(smi_infos[pos]));
2466 }
2467 if (rv) {
2468 /* No SMIC - try BT */
2469 si_type[0] = "bt";
2470 ports[0] = DEFAULT_BT_IO_PORT;
2471 rv = init_one_smi(0, &(smi_infos[pos]));
2472 }
2473 }
2474 if (rv == 0)
2475 pos++;
2476
Corey Minyarde8b33612005-09-06 15:18:45 -07002477 for (i = 1; i < SI_MAX_PARMS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 rv = init_one_smi(i, &(smi_infos[pos]));
2479 if (rv == 0)
2480 pos++;
2481 }
2482
2483 if (smi_infos[0] == NULL) {
2484 printk("ipmi_si: Unable to find any System Interface(s)\n");
2485 return -ENODEV;
2486 }
2487
2488 return 0;
2489}
2490module_init(init_ipmi_si);
2491
2492static void __exit cleanup_one_si(struct smi_info *to_clean)
2493{
2494 int rv;
2495 unsigned long flags;
2496
2497 if (! to_clean)
2498 return;
2499
2500 /* Tell the timer and interrupt handlers that we are shutting
2501 down. */
2502 spin_lock_irqsave(&(to_clean->si_lock), flags);
2503 spin_lock(&(to_clean->msg_lock));
2504
Corey Minyarda9a2c442005-11-07 01:00:03 -08002505 atomic_inc(&to_clean->stop_operation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 to_clean->irq_cleanup(to_clean);
2507
2508 spin_unlock(&(to_clean->msg_lock));
2509 spin_unlock_irqrestore(&(to_clean->si_lock), flags);
2510
2511 /* Wait until we know that we are out of any interrupt
2512 handlers might have been running before we freed the
2513 interrupt. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002514 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
Corey Minyarda9a2c442005-11-07 01:00:03 -08002516 wait_for_timer_and_thread(to_clean);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
2518 /* Interrupts and timeouts are stopped, now make sure the
2519 interface is in a clean state. */
Corey Minyarde8b33612005-09-06 15:18:45 -07002520 while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 poll(to_clean);
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07002522 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 }
2524
2525 rv = ipmi_unregister_smi(to_clean->intf);
2526 if (rv) {
2527 printk(KERN_ERR
2528 "ipmi_si: Unable to unregister device: errno=%d\n",
2529 rv);
2530 }
2531
2532 to_clean->handlers->cleanup(to_clean->si_sm);
2533
2534 kfree(to_clean->si_sm);
2535
2536 to_clean->io_cleanup(to_clean);
2537}
2538
2539static __exit void cleanup_ipmi_si(void)
2540{
2541 int i;
2542
Corey Minyarde8b33612005-09-06 15:18:45 -07002543 if (! initialized)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 return;
2545
Corey Minyarde8b33612005-09-06 15:18:45 -07002546 for (i = 0; i < SI_MAX_DRIVERS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 cleanup_one_si(smi_infos[i]);
2548 }
2549}
2550module_exit(cleanup_ipmi_si);
2551
2552MODULE_LICENSE("GPL");
Corey Minyard1fdd75b2005-09-06 15:18:42 -07002553MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
2554MODULE_DESCRIPTION("Interface to the IPMI driver for the KCS, SMIC, and BT system interfaces.");