blob: b43172aee4c69429921d35ca06fd96fc03f6d14f [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>
54#include <asm/irq.h>
55#ifdef CONFIG_HIGH_RES_TIMERS
56#include <linux/hrtime.h>
57# if defined(schedule_next_int)
58/* Old high-res timer code, do translations. */
59# define get_arch_cycles(a) quick_update_jiffies_sub(a)
60# define arch_cycles_per_jiffy cycles_per_jiffies
61# endif
62static inline void add_usec_to_timer(struct timer_list *t, long v)
63{
Corey Minyard75b07682005-09-06 15:18:38 -070064 t->arch_cycle_expires += nsec_to_arch_cycle(v * 1000);
65 while (t->arch_cycle_expires >= arch_cycles_per_jiffy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 {
67 t->expires++;
Corey Minyard75b07682005-09-06 15:18:38 -070068 t->arch_cycle_expires -= arch_cycles_per_jiffy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 }
70}
71#endif
72#include <linux/interrupt.h>
73#include <linux/rcupdate.h>
74#include <linux/ipmi_smi.h>
75#include <asm/io.h>
76#include "ipmi_si_sm.h"
77#include <linux/init.h>
Andrey Paninb224cd32005-09-06 15:18:37 -070078#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/* Measure times between events in the driver. */
81#undef DEBUG_TIMING
82
83/* Call every 10 ms. */
84#define SI_TIMEOUT_TIME_USEC 10000
85#define SI_USEC_PER_JIFFY (1000000/HZ)
86#define SI_TIMEOUT_JIFFIES (SI_TIMEOUT_TIME_USEC/SI_USEC_PER_JIFFY)
87#define SI_SHORT_TIMEOUT_USEC 250 /* .25ms when the SM request a
88 short timeout */
89
90enum si_intf_state {
91 SI_NORMAL,
92 SI_GETTING_FLAGS,
93 SI_GETTING_EVENTS,
94 SI_CLEARING_FLAGS,
95 SI_CLEARING_FLAGS_THEN_SET_IRQ,
96 SI_GETTING_MESSAGES,
97 SI_ENABLE_INTERRUPTS1,
98 SI_ENABLE_INTERRUPTS2
99 /* FIXME - add watchdog stuff. */
100};
101
Corey Minyard9dbf68f2005-05-01 08:59:11 -0700102/* Some BT-specific defines we need here. */
103#define IPMI_BT_INTMASK_REG 2
104#define IPMI_BT_INTMASK_CLEAR_IRQ_BIT 2
105#define IPMI_BT_INTMASK_ENABLE_IRQ_BIT 1
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107enum si_type {
108 SI_KCS, SI_SMIC, SI_BT
109};
110
Corey Minyard3ae0e0f2005-09-06 15:18:41 -0700111struct ipmi_device_id {
112 unsigned char device_id;
113 unsigned char device_revision;
114 unsigned char firmware_revision_1;
115 unsigned char firmware_revision_2;
116 unsigned char ipmi_version;
117 unsigned char additional_device_support;
118 unsigned char manufacturer_id[3];
119 unsigned char product_id[2];
120 unsigned char aux_firmware_revision[4];
121} __attribute__((packed));
122
123#define ipmi_version_major(v) ((v)->ipmi_version & 0xf)
124#define ipmi_version_minor(v) ((v)->ipmi_version >> 4)
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126struct smi_info
127{
128 ipmi_smi_t intf;
129 struct si_sm_data *si_sm;
130 struct si_sm_handlers *handlers;
131 enum si_type si_type;
132 spinlock_t si_lock;
133 spinlock_t msg_lock;
134 struct list_head xmit_msgs;
135 struct list_head hp_xmit_msgs;
136 struct ipmi_smi_msg *curr_msg;
137 enum si_intf_state si_state;
138
139 /* Used to handle the various types of I/O that can occur with
140 IPMI */
141 struct si_sm_io io;
142 int (*io_setup)(struct smi_info *info);
143 void (*io_cleanup)(struct smi_info *info);
144 int (*irq_setup)(struct smi_info *info);
145 void (*irq_cleanup)(struct smi_info *info);
146 unsigned int io_size;
147
Corey Minyard3ae0e0f2005-09-06 15:18:41 -0700148 /* Per-OEM handler, called from handle_flags().
149 Returns 1 when handle_flags() needs to be re-run
150 or 0 indicating it set si_state itself.
151 */
152 int (*oem_data_avail_handler)(struct smi_info *smi_info);
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 /* Flags from the last GET_MSG_FLAGS command, used when an ATTN
155 is set to hold the flags until we are done handling everything
156 from the flags. */
157#define RECEIVE_MSG_AVAIL 0x01
158#define EVENT_MSG_BUFFER_FULL 0x02
159#define WDT_PRE_TIMEOUT_INT 0x08
Corey Minyard3ae0e0f2005-09-06 15:18:41 -0700160#define OEM0_DATA_AVAIL 0x20
161#define OEM1_DATA_AVAIL 0x40
162#define OEM2_DATA_AVAIL 0x80
163#define OEM_DATA_AVAIL (OEM0_DATA_AVAIL | \
164 OEM1_DATA_AVAIL | \
165 OEM2_DATA_AVAIL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 unsigned char msg_flags;
167
168 /* If set to true, this will request events the next time the
169 state machine is idle. */
170 atomic_t req_events;
171
172 /* If true, run the state machine to completion on every send
173 call. Generally used after a panic to make sure stuff goes
174 out. */
175 int run_to_completion;
176
177 /* The I/O port of an SI interface. */
178 int port;
179
180 /* The space between start addresses of the two ports. For
181 instance, if the first port is 0xca2 and the spacing is 4, then
182 the second port is 0xca6. */
183 unsigned int spacing;
184
185 /* zero if no irq; */
186 int irq;
187
188 /* The timer for this si. */
189 struct timer_list si_timer;
190
191 /* The time (in jiffies) the last timeout occurred at. */
192 unsigned long last_timeout_jiffies;
193
194 /* Used to gracefully stop the timer without race conditions. */
195 volatile int stop_operation;
196 volatile int timer_stopped;
197
198 /* The driver will disable interrupts when it gets into a
199 situation where it cannot handle messages due to lack of
200 memory. Once that situation clears up, it will re-enable
201 interrupts. */
202 int interrupt_disabled;
203
Corey Minyard3ae0e0f2005-09-06 15:18:41 -0700204 struct ipmi_device_id device_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 /* Slave address, could be reported from DMI. */
207 unsigned char slave_addr;
208
209 /* Counters and things for the proc filesystem. */
210 spinlock_t count_lock;
211 unsigned long short_timeouts;
212 unsigned long long_timeouts;
213 unsigned long timeout_restarts;
214 unsigned long idles;
215 unsigned long interrupts;
216 unsigned long attentions;
217 unsigned long flag_fetches;
218 unsigned long hosed_count;
219 unsigned long complete_transactions;
220 unsigned long events;
221 unsigned long watchdog_pretimeouts;
222 unsigned long incoming_messages;
223};
224
225static void si_restart_short_timer(struct smi_info *smi_info);
226
227static void deliver_recv_msg(struct smi_info *smi_info,
228 struct ipmi_smi_msg *msg)
229{
230 /* Deliver the message to the upper layer with the lock
231 released. */
232 spin_unlock(&(smi_info->si_lock));
233 ipmi_smi_msg_received(smi_info->intf, msg);
234 spin_lock(&(smi_info->si_lock));
235}
236
237static void return_hosed_msg(struct smi_info *smi_info)
238{
239 struct ipmi_smi_msg *msg = smi_info->curr_msg;
240
241 /* Make it a reponse */
242 msg->rsp[0] = msg->data[0] | 4;
243 msg->rsp[1] = msg->data[1];
244 msg->rsp[2] = 0xFF; /* Unknown error. */
245 msg->rsp_size = 3;
246
247 smi_info->curr_msg = NULL;
248 deliver_recv_msg(smi_info, msg);
249}
250
251static enum si_sm_result start_next_msg(struct smi_info *smi_info)
252{
253 int rv;
254 struct list_head *entry = NULL;
255#ifdef DEBUG_TIMING
256 struct timeval t;
257#endif
258
259 /* No need to save flags, we aleady have interrupts off and we
260 already hold the SMI lock. */
261 spin_lock(&(smi_info->msg_lock));
262
263 /* Pick the high priority queue first. */
264 if (! list_empty(&(smi_info->hp_xmit_msgs))) {
265 entry = smi_info->hp_xmit_msgs.next;
266 } else if (! list_empty(&(smi_info->xmit_msgs))) {
267 entry = smi_info->xmit_msgs.next;
268 }
269
270 if (!entry) {
271 smi_info->curr_msg = NULL;
272 rv = SI_SM_IDLE;
273 } else {
274 int err;
275
276 list_del(entry);
277 smi_info->curr_msg = list_entry(entry,
278 struct ipmi_smi_msg,
279 link);
280#ifdef DEBUG_TIMING
281 do_gettimeofday(&t);
282 printk("**Start2: %d.%9.9d\n", t.tv_sec, t.tv_usec);
283#endif
284 err = smi_info->handlers->start_transaction(
285 smi_info->si_sm,
286 smi_info->curr_msg->data,
287 smi_info->curr_msg->data_size);
288 if (err) {
289 return_hosed_msg(smi_info);
290 }
291
292 rv = SI_SM_CALL_WITHOUT_DELAY;
293 }
294 spin_unlock(&(smi_info->msg_lock));
295
296 return rv;
297}
298
299static void start_enable_irq(struct smi_info *smi_info)
300{
301 unsigned char msg[2];
302
303 /* If we are enabling interrupts, we have to tell the
304 BMC to use them. */
305 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
306 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
307
308 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
309 smi_info->si_state = SI_ENABLE_INTERRUPTS1;
310}
311
312static void start_clear_flags(struct smi_info *smi_info)
313{
314 unsigned char msg[3];
315
316 /* Make sure the watchdog pre-timeout flag is not set at startup. */
317 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
318 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
319 msg[2] = WDT_PRE_TIMEOUT_INT;
320
321 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
322 smi_info->si_state = SI_CLEARING_FLAGS;
323}
324
325/* When we have a situtaion where we run out of memory and cannot
326 allocate messages, we just leave them in the BMC and run the system
327 polled until we can allocate some memory. Once we have some
328 memory, we will re-enable the interrupt. */
329static inline void disable_si_irq(struct smi_info *smi_info)
330{
331 if ((smi_info->irq) && (!smi_info->interrupt_disabled)) {
332 disable_irq_nosync(smi_info->irq);
333 smi_info->interrupt_disabled = 1;
334 }
335}
336
337static inline void enable_si_irq(struct smi_info *smi_info)
338{
339 if ((smi_info->irq) && (smi_info->interrupt_disabled)) {
340 enable_irq(smi_info->irq);
341 smi_info->interrupt_disabled = 0;
342 }
343}
344
345static void handle_flags(struct smi_info *smi_info)
346{
Corey Minyard3ae0e0f2005-09-06 15:18:41 -0700347 retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (smi_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
349 /* Watchdog pre-timeout */
350 spin_lock(&smi_info->count_lock);
351 smi_info->watchdog_pretimeouts++;
352 spin_unlock(&smi_info->count_lock);
353
354 start_clear_flags(smi_info);
355 smi_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
356 spin_unlock(&(smi_info->si_lock));
357 ipmi_smi_watchdog_pretimeout(smi_info->intf);
358 spin_lock(&(smi_info->si_lock));
359 } else if (smi_info->msg_flags & RECEIVE_MSG_AVAIL) {
360 /* Messages available. */
361 smi_info->curr_msg = ipmi_alloc_smi_msg();
362 if (!smi_info->curr_msg) {
363 disable_si_irq(smi_info);
364 smi_info->si_state = SI_NORMAL;
365 return;
366 }
367 enable_si_irq(smi_info);
368
369 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
370 smi_info->curr_msg->data[1] = IPMI_GET_MSG_CMD;
371 smi_info->curr_msg->data_size = 2;
372
373 smi_info->handlers->start_transaction(
374 smi_info->si_sm,
375 smi_info->curr_msg->data,
376 smi_info->curr_msg->data_size);
377 smi_info->si_state = SI_GETTING_MESSAGES;
378 } else if (smi_info->msg_flags & EVENT_MSG_BUFFER_FULL) {
379 /* Events available. */
380 smi_info->curr_msg = ipmi_alloc_smi_msg();
381 if (!smi_info->curr_msg) {
382 disable_si_irq(smi_info);
383 smi_info->si_state = SI_NORMAL;
384 return;
385 }
386 enable_si_irq(smi_info);
387
388 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
389 smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
390 smi_info->curr_msg->data_size = 2;
391
392 smi_info->handlers->start_transaction(
393 smi_info->si_sm,
394 smi_info->curr_msg->data,
395 smi_info->curr_msg->data_size);
396 smi_info->si_state = SI_GETTING_EVENTS;
Corey Minyard3ae0e0f2005-09-06 15:18:41 -0700397 } else if (smi_info->msg_flags & OEM_DATA_AVAIL) {
398 if (smi_info->oem_data_avail_handler)
399 if (smi_info->oem_data_avail_handler(smi_info))
400 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 } else {
402 smi_info->si_state = SI_NORMAL;
403 }
404}
405
406static void handle_transaction_done(struct smi_info *smi_info)
407{
408 struct ipmi_smi_msg *msg;
409#ifdef DEBUG_TIMING
410 struct timeval t;
411
412 do_gettimeofday(&t);
413 printk("**Done: %d.%9.9d\n", t.tv_sec, t.tv_usec);
414#endif
415 switch (smi_info->si_state) {
416 case SI_NORMAL:
417 if (!smi_info->curr_msg)
418 break;
419
420 smi_info->curr_msg->rsp_size
421 = smi_info->handlers->get_result(
422 smi_info->si_sm,
423 smi_info->curr_msg->rsp,
424 IPMI_MAX_MSG_LENGTH);
425
426 /* Do this here becase deliver_recv_msg() releases the
427 lock, and a new message can be put in during the
428 time the lock is released. */
429 msg = smi_info->curr_msg;
430 smi_info->curr_msg = NULL;
431 deliver_recv_msg(smi_info, msg);
432 break;
433
434 case SI_GETTING_FLAGS:
435 {
436 unsigned char msg[4];
437 unsigned int len;
438
439 /* We got the flags from the SMI, now handle them. */
440 len = smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
441 if (msg[2] != 0) {
442 /* Error fetching flags, just give up for
443 now. */
444 smi_info->si_state = SI_NORMAL;
445 } else if (len < 4) {
446 /* Hmm, no flags. That's technically illegal, but
447 don't use uninitialized data. */
448 smi_info->si_state = SI_NORMAL;
449 } else {
450 smi_info->msg_flags = msg[3];
451 handle_flags(smi_info);
452 }
453 break;
454 }
455
456 case SI_CLEARING_FLAGS:
457 case SI_CLEARING_FLAGS_THEN_SET_IRQ:
458 {
459 unsigned char msg[3];
460
461 /* We cleared the flags. */
462 smi_info->handlers->get_result(smi_info->si_sm, msg, 3);
463 if (msg[2] != 0) {
464 /* Error clearing flags */
465 printk(KERN_WARNING
466 "ipmi_si: Error clearing flags: %2.2x\n",
467 msg[2]);
468 }
469 if (smi_info->si_state == SI_CLEARING_FLAGS_THEN_SET_IRQ)
470 start_enable_irq(smi_info);
471 else
472 smi_info->si_state = SI_NORMAL;
473 break;
474 }
475
476 case SI_GETTING_EVENTS:
477 {
478 smi_info->curr_msg->rsp_size
479 = smi_info->handlers->get_result(
480 smi_info->si_sm,
481 smi_info->curr_msg->rsp,
482 IPMI_MAX_MSG_LENGTH);
483
484 /* Do this here becase deliver_recv_msg() releases the
485 lock, and a new message can be put in during the
486 time the lock is released. */
487 msg = smi_info->curr_msg;
488 smi_info->curr_msg = NULL;
489 if (msg->rsp[2] != 0) {
490 /* Error getting event, probably done. */
491 msg->done(msg);
492
493 /* Take off the event flag. */
494 smi_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
495 handle_flags(smi_info);
496 } else {
497 spin_lock(&smi_info->count_lock);
498 smi_info->events++;
499 spin_unlock(&smi_info->count_lock);
500
501 /* Do this before we deliver the message
502 because delivering the message releases the
503 lock and something else can mess with the
504 state. */
505 handle_flags(smi_info);
506
507 deliver_recv_msg(smi_info, msg);
508 }
509 break;
510 }
511
512 case SI_GETTING_MESSAGES:
513 {
514 smi_info->curr_msg->rsp_size
515 = smi_info->handlers->get_result(
516 smi_info->si_sm,
517 smi_info->curr_msg->rsp,
518 IPMI_MAX_MSG_LENGTH);
519
520 /* Do this here becase deliver_recv_msg() releases the
521 lock, and a new message can be put in during the
522 time the lock is released. */
523 msg = smi_info->curr_msg;
524 smi_info->curr_msg = NULL;
525 if (msg->rsp[2] != 0) {
526 /* Error getting event, probably done. */
527 msg->done(msg);
528
529 /* Take off the msg flag. */
530 smi_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
531 handle_flags(smi_info);
532 } else {
533 spin_lock(&smi_info->count_lock);
534 smi_info->incoming_messages++;
535 spin_unlock(&smi_info->count_lock);
536
537 /* Do this before we deliver the message
538 because delivering the message releases the
539 lock and something else can mess with the
540 state. */
541 handle_flags(smi_info);
542
543 deliver_recv_msg(smi_info, msg);
544 }
545 break;
546 }
547
548 case SI_ENABLE_INTERRUPTS1:
549 {
550 unsigned char msg[4];
551
552 /* We got the flags from the SMI, now handle them. */
553 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
554 if (msg[2] != 0) {
555 printk(KERN_WARNING
556 "ipmi_si: Could not enable interrupts"
557 ", failed get, using polled mode.\n");
558 smi_info->si_state = SI_NORMAL;
559 } else {
560 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
561 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
562 msg[2] = msg[3] | 1; /* enable msg queue int */
563 smi_info->handlers->start_transaction(
564 smi_info->si_sm, msg, 3);
565 smi_info->si_state = SI_ENABLE_INTERRUPTS2;
566 }
567 break;
568 }
569
570 case SI_ENABLE_INTERRUPTS2:
571 {
572 unsigned char msg[4];
573
574 /* We got the flags from the SMI, now handle them. */
575 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
576 if (msg[2] != 0) {
577 printk(KERN_WARNING
578 "ipmi_si: Could not enable interrupts"
579 ", failed set, using polled mode.\n");
580 }
581 smi_info->si_state = SI_NORMAL;
582 break;
583 }
584 }
585}
586
587/* Called on timeouts and events. Timeouts should pass the elapsed
588 time, interrupts should pass in zero. */
589static enum si_sm_result smi_event_handler(struct smi_info *smi_info,
590 int time)
591{
592 enum si_sm_result si_sm_result;
593
594 restart:
595 /* There used to be a loop here that waited a little while
596 (around 25us) before giving up. That turned out to be
597 pointless, the minimum delays I was seeing were in the 300us
598 range, which is far too long to wait in an interrupt. So
599 we just run until the state machine tells us something
600 happened or it needs a delay. */
601 si_sm_result = smi_info->handlers->event(smi_info->si_sm, time);
602 time = 0;
603 while (si_sm_result == SI_SM_CALL_WITHOUT_DELAY)
604 {
605 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
606 }
607
608 if (si_sm_result == SI_SM_TRANSACTION_COMPLETE)
609 {
610 spin_lock(&smi_info->count_lock);
611 smi_info->complete_transactions++;
612 spin_unlock(&smi_info->count_lock);
613
614 handle_transaction_done(smi_info);
615 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
616 }
617 else if (si_sm_result == SI_SM_HOSED)
618 {
619 spin_lock(&smi_info->count_lock);
620 smi_info->hosed_count++;
621 spin_unlock(&smi_info->count_lock);
622
623 /* Do the before return_hosed_msg, because that
624 releases the lock. */
625 smi_info->si_state = SI_NORMAL;
626 if (smi_info->curr_msg != NULL) {
627 /* If we were handling a user message, format
628 a response to send to the upper layer to
629 tell it about the error. */
630 return_hosed_msg(smi_info);
631 }
632 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
633 }
634
635 /* We prefer handling attn over new messages. */
636 if (si_sm_result == SI_SM_ATTN)
637 {
638 unsigned char msg[2];
639
640 spin_lock(&smi_info->count_lock);
641 smi_info->attentions++;
642 spin_unlock(&smi_info->count_lock);
643
644 /* Got a attn, send down a get message flags to see
645 what's causing it. It would be better to handle
646 this in the upper layer, but due to the way
647 interrupts work with the SMI, that's not really
648 possible. */
649 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
650 msg[1] = IPMI_GET_MSG_FLAGS_CMD;
651
652 smi_info->handlers->start_transaction(
653 smi_info->si_sm, msg, 2);
654 smi_info->si_state = SI_GETTING_FLAGS;
655 goto restart;
656 }
657
658 /* If we are currently idle, try to start the next message. */
659 if (si_sm_result == SI_SM_IDLE) {
660 spin_lock(&smi_info->count_lock);
661 smi_info->idles++;
662 spin_unlock(&smi_info->count_lock);
663
664 si_sm_result = start_next_msg(smi_info);
665 if (si_sm_result != SI_SM_IDLE)
666 goto restart;
667 }
668
669 if ((si_sm_result == SI_SM_IDLE)
670 && (atomic_read(&smi_info->req_events)))
671 {
672 /* We are idle and the upper layer requested that I fetch
673 events, so do so. */
674 unsigned char msg[2];
675
676 spin_lock(&smi_info->count_lock);
677 smi_info->flag_fetches++;
678 spin_unlock(&smi_info->count_lock);
679
680 atomic_set(&smi_info->req_events, 0);
681 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
682 msg[1] = IPMI_GET_MSG_FLAGS_CMD;
683
684 smi_info->handlers->start_transaction(
685 smi_info->si_sm, msg, 2);
686 smi_info->si_state = SI_GETTING_FLAGS;
687 goto restart;
688 }
689
690 return si_sm_result;
691}
692
693static void sender(void *send_info,
694 struct ipmi_smi_msg *msg,
695 int priority)
696{
697 struct smi_info *smi_info = send_info;
698 enum si_sm_result result;
699 unsigned long flags;
700#ifdef DEBUG_TIMING
701 struct timeval t;
702#endif
703
704 spin_lock_irqsave(&(smi_info->msg_lock), flags);
705#ifdef DEBUG_TIMING
706 do_gettimeofday(&t);
707 printk("**Enqueue: %d.%9.9d\n", t.tv_sec, t.tv_usec);
708#endif
709
710 if (smi_info->run_to_completion) {
711 /* If we are running to completion, then throw it in
712 the list and run transactions until everything is
713 clear. Priority doesn't matter here. */
714 list_add_tail(&(msg->link), &(smi_info->xmit_msgs));
715
716 /* We have to release the msg lock and claim the smi
717 lock in this case, because of race conditions. */
718 spin_unlock_irqrestore(&(smi_info->msg_lock), flags);
719
720 spin_lock_irqsave(&(smi_info->si_lock), flags);
721 result = smi_event_handler(smi_info, 0);
722 while (result != SI_SM_IDLE) {
723 udelay(SI_SHORT_TIMEOUT_USEC);
724 result = smi_event_handler(smi_info,
725 SI_SHORT_TIMEOUT_USEC);
726 }
727 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
728 return;
729 } else {
730 if (priority > 0) {
731 list_add_tail(&(msg->link), &(smi_info->hp_xmit_msgs));
732 } else {
733 list_add_tail(&(msg->link), &(smi_info->xmit_msgs));
734 }
735 }
736 spin_unlock_irqrestore(&(smi_info->msg_lock), flags);
737
738 spin_lock_irqsave(&(smi_info->si_lock), flags);
739 if ((smi_info->si_state == SI_NORMAL)
740 && (smi_info->curr_msg == NULL))
741 {
742 start_next_msg(smi_info);
743 si_restart_short_timer(smi_info);
744 }
745 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
746}
747
748static void set_run_to_completion(void *send_info, int i_run_to_completion)
749{
750 struct smi_info *smi_info = send_info;
751 enum si_sm_result result;
752 unsigned long flags;
753
754 spin_lock_irqsave(&(smi_info->si_lock), flags);
755
756 smi_info->run_to_completion = i_run_to_completion;
757 if (i_run_to_completion) {
758 result = smi_event_handler(smi_info, 0);
759 while (result != SI_SM_IDLE) {
760 udelay(SI_SHORT_TIMEOUT_USEC);
761 result = smi_event_handler(smi_info,
762 SI_SHORT_TIMEOUT_USEC);
763 }
764 }
765
766 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
767}
768
769static void poll(void *send_info)
770{
771 struct smi_info *smi_info = send_info;
772
773 smi_event_handler(smi_info, 0);
774}
775
776static void request_events(void *send_info)
777{
778 struct smi_info *smi_info = send_info;
779
780 atomic_set(&smi_info->req_events, 1);
781}
782
783static int initialized = 0;
784
785/* Must be called with interrupts off and with the si_lock held. */
786static void si_restart_short_timer(struct smi_info *smi_info)
787{
788#if defined(CONFIG_HIGH_RES_TIMERS)
789 unsigned long flags;
790 unsigned long jiffies_now;
Corey Minyard75b07682005-09-06 15:18:38 -0700791 unsigned long seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 if (del_timer(&(smi_info->si_timer))) {
794 /* If we don't delete the timer, then it will go off
795 immediately, anyway. So we only process if we
796 actually delete the timer. */
797
Corey Minyard75b07682005-09-06 15:18:38 -0700798 do {
799 seq = read_seqbegin_irqsave(&xtime_lock, flags);
800 jiffies_now = jiffies;
801 smi_info->si_timer.expires = jiffies_now;
802 smi_info->si_timer.arch_cycle_expires
803 = get_arch_cycles(jiffies_now);
804 } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 add_usec_to_timer(&smi_info->si_timer, SI_SHORT_TIMEOUT_USEC);
807
808 add_timer(&(smi_info->si_timer));
809 spin_lock_irqsave(&smi_info->count_lock, flags);
810 smi_info->timeout_restarts++;
811 spin_unlock_irqrestore(&smi_info->count_lock, flags);
812 }
813#endif
814}
815
816static void smi_timeout(unsigned long data)
817{
818 struct smi_info *smi_info = (struct smi_info *) data;
819 enum si_sm_result smi_result;
820 unsigned long flags;
821 unsigned long jiffies_now;
822 unsigned long time_diff;
823#ifdef DEBUG_TIMING
824 struct timeval t;
825#endif
826
827 if (smi_info->stop_operation) {
828 smi_info->timer_stopped = 1;
829 return;
830 }
831
832 spin_lock_irqsave(&(smi_info->si_lock), flags);
833#ifdef DEBUG_TIMING
834 do_gettimeofday(&t);
835 printk("**Timer: %d.%9.9d\n", t.tv_sec, t.tv_usec);
836#endif
837 jiffies_now = jiffies;
838 time_diff = ((jiffies_now - smi_info->last_timeout_jiffies)
839 * SI_USEC_PER_JIFFY);
840 smi_result = smi_event_handler(smi_info, time_diff);
841
842 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
843
844 smi_info->last_timeout_jiffies = jiffies_now;
845
846 if ((smi_info->irq) && (! smi_info->interrupt_disabled)) {
847 /* Running with interrupts, only do long timeouts. */
848 smi_info->si_timer.expires = jiffies + SI_TIMEOUT_JIFFIES;
849 spin_lock_irqsave(&smi_info->count_lock, flags);
850 smi_info->long_timeouts++;
851 spin_unlock_irqrestore(&smi_info->count_lock, flags);
852 goto do_add_timer;
853 }
854
855 /* If the state machine asks for a short delay, then shorten
856 the timer timeout. */
857 if (smi_result == SI_SM_CALL_WITH_DELAY) {
Corey Minyard75b07682005-09-06 15:18:38 -0700858#if defined(CONFIG_HIGH_RES_TIMERS)
859 unsigned long seq;
860#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 spin_lock_irqsave(&smi_info->count_lock, flags);
862 smi_info->short_timeouts++;
863 spin_unlock_irqrestore(&smi_info->count_lock, flags);
864#if defined(CONFIG_HIGH_RES_TIMERS)
Corey Minyard75b07682005-09-06 15:18:38 -0700865 do {
866 seq = read_seqbegin_irqsave(&xtime_lock, flags);
867 smi_info->si_timer.expires = jiffies;
868 smi_info->si_timer.arch_cycle_expires
869 = get_arch_cycles(smi_info->si_timer.expires);
870 } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 add_usec_to_timer(&smi_info->si_timer, SI_SHORT_TIMEOUT_USEC);
872#else
873 smi_info->si_timer.expires = jiffies + 1;
874#endif
875 } else {
876 spin_lock_irqsave(&smi_info->count_lock, flags);
877 smi_info->long_timeouts++;
878 spin_unlock_irqrestore(&smi_info->count_lock, flags);
879 smi_info->si_timer.expires = jiffies + SI_TIMEOUT_JIFFIES;
880#if defined(CONFIG_HIGH_RES_TIMERS)
Corey Minyard75b07682005-09-06 15:18:38 -0700881 smi_info->si_timer.arch_cycle_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882#endif
883 }
884
885 do_add_timer:
886 add_timer(&(smi_info->si_timer));
887}
888
889static irqreturn_t si_irq_handler(int irq, void *data, struct pt_regs *regs)
890{
891 struct smi_info *smi_info = data;
892 unsigned long flags;
893#ifdef DEBUG_TIMING
894 struct timeval t;
895#endif
896
897 spin_lock_irqsave(&(smi_info->si_lock), flags);
898
899 spin_lock(&smi_info->count_lock);
900 smi_info->interrupts++;
901 spin_unlock(&smi_info->count_lock);
902
903 if (smi_info->stop_operation)
904 goto out;
905
906#ifdef DEBUG_TIMING
907 do_gettimeofday(&t);
908 printk("**Interrupt: %d.%9.9d\n", t.tv_sec, t.tv_usec);
909#endif
910 smi_event_handler(smi_info, 0);
911 out:
912 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
913 return IRQ_HANDLED;
914}
915
Corey Minyard9dbf68f2005-05-01 08:59:11 -0700916static irqreturn_t si_bt_irq_handler(int irq, void *data, struct pt_regs *regs)
917{
918 struct smi_info *smi_info = data;
919 /* We need to clear the IRQ flag for the BT interface. */
920 smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
921 IPMI_BT_INTMASK_CLEAR_IRQ_BIT
922 | IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
923 return si_irq_handler(irq, data, regs);
924}
925
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927static struct ipmi_smi_handlers handlers =
928{
929 .owner = THIS_MODULE,
930 .sender = sender,
931 .request_events = request_events,
932 .set_run_to_completion = set_run_to_completion,
933 .poll = poll,
934};
935
936/* There can be 4 IO ports passed in (with or without IRQs), 4 addresses,
937 a default IO port, and 1 ACPI/SPMI address. That sets SI_MAX_DRIVERS */
938
939#define SI_MAX_PARMS 4
940#define SI_MAX_DRIVERS ((SI_MAX_PARMS * 2) + 2)
941static struct smi_info *smi_infos[SI_MAX_DRIVERS] =
942{ NULL, NULL, NULL, NULL };
943
944#define DEVICE_NAME "ipmi_si"
945
946#define DEFAULT_KCS_IO_PORT 0xca2
947#define DEFAULT_SMIC_IO_PORT 0xca9
948#define DEFAULT_BT_IO_PORT 0xe4
949#define DEFAULT_REGSPACING 1
950
951static int si_trydefaults = 1;
952static char *si_type[SI_MAX_PARMS];
953#define MAX_SI_TYPE_STR 30
954static char si_type_str[MAX_SI_TYPE_STR];
955static unsigned long addrs[SI_MAX_PARMS];
956static int num_addrs;
957static unsigned int ports[SI_MAX_PARMS];
958static int num_ports;
959static int irqs[SI_MAX_PARMS];
960static int num_irqs;
961static int regspacings[SI_MAX_PARMS];
962static int num_regspacings = 0;
963static int regsizes[SI_MAX_PARMS];
964static int num_regsizes = 0;
965static int regshifts[SI_MAX_PARMS];
966static int num_regshifts = 0;
967static int slave_addrs[SI_MAX_PARMS];
968static int num_slave_addrs = 0;
969
970
971module_param_named(trydefaults, si_trydefaults, bool, 0);
972MODULE_PARM_DESC(trydefaults, "Setting this to 'false' will disable the"
973 " default scan of the KCS and SMIC interface at the standard"
974 " address");
975module_param_string(type, si_type_str, MAX_SI_TYPE_STR, 0);
976MODULE_PARM_DESC(type, "Defines the type of each interface, each"
977 " interface separated by commas. The types are 'kcs',"
978 " 'smic', and 'bt'. For example si_type=kcs,bt will set"
979 " the first interface to kcs and the second to bt");
980module_param_array(addrs, long, &num_addrs, 0);
981MODULE_PARM_DESC(addrs, "Sets the memory address of each interface, the"
982 " addresses separated by commas. Only use if an interface"
983 " is in memory. Otherwise, set it to zero or leave"
984 " it blank.");
985module_param_array(ports, int, &num_ports, 0);
986MODULE_PARM_DESC(ports, "Sets the port address of each interface, the"
987 " addresses separated by commas. Only use if an interface"
988 " is a port. Otherwise, set it to zero or leave"
989 " it blank.");
990module_param_array(irqs, int, &num_irqs, 0);
991MODULE_PARM_DESC(irqs, "Sets the interrupt of each interface, the"
992 " addresses separated by commas. Only use if an interface"
993 " has an interrupt. Otherwise, set it to zero or leave"
994 " it blank.");
995module_param_array(regspacings, int, &num_regspacings, 0);
996MODULE_PARM_DESC(regspacings, "The number of bytes between the start address"
997 " and each successive register used by the interface. For"
998 " instance, if the start address is 0xca2 and the spacing"
999 " is 2, then the second address is at 0xca4. Defaults"
1000 " to 1.");
1001module_param_array(regsizes, int, &num_regsizes, 0);
1002MODULE_PARM_DESC(regsizes, "The size of the specific IPMI register in bytes."
1003 " This should generally be 1, 2, 4, or 8 for an 8-bit,"
1004 " 16-bit, 32-bit, or 64-bit register. Use this if you"
1005 " the 8-bit IPMI register has to be read from a larger"
1006 " register.");
1007module_param_array(regshifts, int, &num_regshifts, 0);
1008MODULE_PARM_DESC(regshifts, "The amount to shift the data read from the."
1009 " IPMI register, in bits. For instance, if the data"
1010 " is read from a 32-bit word and the IPMI data is in"
1011 " bit 8-15, then the shift would be 8");
1012module_param_array(slave_addrs, int, &num_slave_addrs, 0);
1013MODULE_PARM_DESC(slave_addrs, "Set the default IPMB slave address for"
1014 " the controller. Normally this is 0x20, but can be"
1015 " overridden by this parm. This is an array indexed"
1016 " by interface number.");
1017
1018
1019#define IPMI_MEM_ADDR_SPACE 1
1020#define IPMI_IO_ADDR_SPACE 2
1021
1022#if defined(CONFIG_ACPI_INTERPRETER) || defined(CONFIG_X86) || defined(CONFIG_PCI)
1023static int is_new_interface(int intf, u8 addr_space, unsigned long base_addr)
1024{
1025 int i;
1026
1027 for (i = 0; i < SI_MAX_PARMS; ++i) {
1028 /* Don't check our address. */
1029 if (i == intf)
1030 continue;
1031 if (si_type[i] != NULL) {
1032 if ((addr_space == IPMI_MEM_ADDR_SPACE &&
1033 base_addr == addrs[i]) ||
1034 (addr_space == IPMI_IO_ADDR_SPACE &&
1035 base_addr == ports[i]))
1036 return 0;
1037 }
1038 else
1039 break;
1040 }
1041
1042 return 1;
1043}
1044#endif
1045
1046static int std_irq_setup(struct smi_info *info)
1047{
1048 int rv;
1049
1050 if (!info->irq)
1051 return 0;
1052
Corey Minyard9dbf68f2005-05-01 08:59:11 -07001053 if (info->si_type == SI_BT) {
1054 rv = request_irq(info->irq,
1055 si_bt_irq_handler,
1056 SA_INTERRUPT,
1057 DEVICE_NAME,
1058 info);
1059 if (!rv)
1060 /* Enable the interrupt in the BT interface. */
1061 info->io.outputb(&info->io, IPMI_BT_INTMASK_REG,
1062 IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
1063 } else
1064 rv = request_irq(info->irq,
1065 si_irq_handler,
1066 SA_INTERRUPT,
1067 DEVICE_NAME,
1068 info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (rv) {
1070 printk(KERN_WARNING
1071 "ipmi_si: %s unable to claim interrupt %d,"
1072 " running polled\n",
1073 DEVICE_NAME, info->irq);
1074 info->irq = 0;
1075 } else {
1076 printk(" Using irq %d\n", info->irq);
1077 }
1078
1079 return rv;
1080}
1081
1082static void std_irq_cleanup(struct smi_info *info)
1083{
1084 if (!info->irq)
1085 return;
1086
Corey Minyard9dbf68f2005-05-01 08:59:11 -07001087 if (info->si_type == SI_BT)
1088 /* Disable the interrupt in the BT interface. */
1089 info->io.outputb(&info->io, IPMI_BT_INTMASK_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 free_irq(info->irq, info);
1091}
1092
1093static unsigned char port_inb(struct si_sm_io *io, unsigned int offset)
1094{
1095 unsigned int *addr = io->info;
1096
1097 return inb((*addr)+(offset*io->regspacing));
1098}
1099
1100static void port_outb(struct si_sm_io *io, unsigned int offset,
1101 unsigned char b)
1102{
1103 unsigned int *addr = io->info;
1104
1105 outb(b, (*addr)+(offset * io->regspacing));
1106}
1107
1108static unsigned char port_inw(struct si_sm_io *io, unsigned int offset)
1109{
1110 unsigned int *addr = io->info;
1111
1112 return (inw((*addr)+(offset * io->regspacing)) >> io->regshift) & 0xff;
1113}
1114
1115static void port_outw(struct si_sm_io *io, unsigned int offset,
1116 unsigned char b)
1117{
1118 unsigned int *addr = io->info;
1119
1120 outw(b << io->regshift, (*addr)+(offset * io->regspacing));
1121}
1122
1123static unsigned char port_inl(struct si_sm_io *io, unsigned int offset)
1124{
1125 unsigned int *addr = io->info;
1126
1127 return (inl((*addr)+(offset * io->regspacing)) >> io->regshift) & 0xff;
1128}
1129
1130static void port_outl(struct si_sm_io *io, unsigned int offset,
1131 unsigned char b)
1132{
1133 unsigned int *addr = io->info;
1134
1135 outl(b << io->regshift, (*addr)+(offset * io->regspacing));
1136}
1137
1138static void port_cleanup(struct smi_info *info)
1139{
1140 unsigned int *addr = info->io.info;
1141 int mapsize;
1142
1143 if (addr && (*addr)) {
1144 mapsize = ((info->io_size * info->io.regspacing)
1145 - (info->io.regspacing - info->io.regsize));
1146
1147 release_region (*addr, mapsize);
1148 }
1149 kfree(info);
1150}
1151
1152static int port_setup(struct smi_info *info)
1153{
1154 unsigned int *addr = info->io.info;
1155 int mapsize;
1156
1157 if (!addr || (!*addr))
1158 return -ENODEV;
1159
1160 info->io_cleanup = port_cleanup;
1161
1162 /* Figure out the actual inb/inw/inl/etc routine to use based
1163 upon the register size. */
1164 switch (info->io.regsize) {
1165 case 1:
1166 info->io.inputb = port_inb;
1167 info->io.outputb = port_outb;
1168 break;
1169 case 2:
1170 info->io.inputb = port_inw;
1171 info->io.outputb = port_outw;
1172 break;
1173 case 4:
1174 info->io.inputb = port_inl;
1175 info->io.outputb = port_outl;
1176 break;
1177 default:
1178 printk("ipmi_si: Invalid register size: %d\n",
1179 info->io.regsize);
1180 return -EINVAL;
1181 }
1182
1183 /* Calculate the total amount of memory to claim. This is an
1184 * unusual looking calculation, but it avoids claiming any
1185 * more memory than it has to. It will claim everything
1186 * between the first address to the end of the last full
1187 * register. */
1188 mapsize = ((info->io_size * info->io.regspacing)
1189 - (info->io.regspacing - info->io.regsize));
1190
1191 if (request_region(*addr, mapsize, DEVICE_NAME) == NULL)
1192 return -EIO;
1193 return 0;
1194}
1195
1196static int try_init_port(int intf_num, struct smi_info **new_info)
1197{
1198 struct smi_info *info;
1199
1200 if (!ports[intf_num])
1201 return -ENODEV;
1202
1203 if (!is_new_interface(intf_num, IPMI_IO_ADDR_SPACE,
1204 ports[intf_num]))
1205 return -ENODEV;
1206
1207 info = kmalloc(sizeof(*info), GFP_KERNEL);
1208 if (!info) {
1209 printk(KERN_ERR "ipmi_si: Could not allocate SI data (1)\n");
1210 return -ENOMEM;
1211 }
1212 memset(info, 0, sizeof(*info));
1213
1214 info->io_setup = port_setup;
1215 info->io.info = &(ports[intf_num]);
1216 info->io.addr = NULL;
1217 info->io.regspacing = regspacings[intf_num];
1218 if (!info->io.regspacing)
1219 info->io.regspacing = DEFAULT_REGSPACING;
1220 info->io.regsize = regsizes[intf_num];
1221 if (!info->io.regsize)
1222 info->io.regsize = DEFAULT_REGSPACING;
1223 info->io.regshift = regshifts[intf_num];
1224 info->irq = 0;
1225 info->irq_setup = NULL;
1226 *new_info = info;
1227
1228 if (si_type[intf_num] == NULL)
1229 si_type[intf_num] = "kcs";
1230
1231 printk("ipmi_si: Trying \"%s\" at I/O port 0x%x\n",
1232 si_type[intf_num], ports[intf_num]);
1233 return 0;
1234}
1235
1236static unsigned char mem_inb(struct si_sm_io *io, unsigned int offset)
1237{
1238 return readb((io->addr)+(offset * io->regspacing));
1239}
1240
1241static void mem_outb(struct si_sm_io *io, unsigned int offset,
1242 unsigned char b)
1243{
1244 writeb(b, (io->addr)+(offset * io->regspacing));
1245}
1246
1247static unsigned char mem_inw(struct si_sm_io *io, unsigned int offset)
1248{
1249 return (readw((io->addr)+(offset * io->regspacing)) >> io->regshift)
1250 && 0xff;
1251}
1252
1253static void mem_outw(struct si_sm_io *io, unsigned int offset,
1254 unsigned char b)
1255{
1256 writeb(b << io->regshift, (io->addr)+(offset * io->regspacing));
1257}
1258
1259static unsigned char mem_inl(struct si_sm_io *io, unsigned int offset)
1260{
1261 return (readl((io->addr)+(offset * io->regspacing)) >> io->regshift)
1262 && 0xff;
1263}
1264
1265static void mem_outl(struct si_sm_io *io, unsigned int offset,
1266 unsigned char b)
1267{
1268 writel(b << io->regshift, (io->addr)+(offset * io->regspacing));
1269}
1270
1271#ifdef readq
1272static unsigned char mem_inq(struct si_sm_io *io, unsigned int offset)
1273{
1274 return (readq((io->addr)+(offset * io->regspacing)) >> io->regshift)
1275 && 0xff;
1276}
1277
1278static void mem_outq(struct si_sm_io *io, unsigned int offset,
1279 unsigned char b)
1280{
1281 writeq(b << io->regshift, (io->addr)+(offset * io->regspacing));
1282}
1283#endif
1284
1285static void mem_cleanup(struct smi_info *info)
1286{
1287 unsigned long *addr = info->io.info;
1288 int mapsize;
1289
1290 if (info->io.addr) {
1291 iounmap(info->io.addr);
1292
1293 mapsize = ((info->io_size * info->io.regspacing)
1294 - (info->io.regspacing - info->io.regsize));
1295
1296 release_mem_region(*addr, mapsize);
1297 }
1298 kfree(info);
1299}
1300
1301static int mem_setup(struct smi_info *info)
1302{
1303 unsigned long *addr = info->io.info;
1304 int mapsize;
1305
1306 if (!addr || (!*addr))
1307 return -ENODEV;
1308
1309 info->io_cleanup = mem_cleanup;
1310
1311 /* Figure out the actual readb/readw/readl/etc routine to use based
1312 upon the register size. */
1313 switch (info->io.regsize) {
1314 case 1:
1315 info->io.inputb = mem_inb;
1316 info->io.outputb = mem_outb;
1317 break;
1318 case 2:
1319 info->io.inputb = mem_inw;
1320 info->io.outputb = mem_outw;
1321 break;
1322 case 4:
1323 info->io.inputb = mem_inl;
1324 info->io.outputb = mem_outl;
1325 break;
1326#ifdef readq
1327 case 8:
1328 info->io.inputb = mem_inq;
1329 info->io.outputb = mem_outq;
1330 break;
1331#endif
1332 default:
1333 printk("ipmi_si: Invalid register size: %d\n",
1334 info->io.regsize);
1335 return -EINVAL;
1336 }
1337
1338 /* Calculate the total amount of memory to claim. This is an
1339 * unusual looking calculation, but it avoids claiming any
1340 * more memory than it has to. It will claim everything
1341 * between the first address to the end of the last full
1342 * register. */
1343 mapsize = ((info->io_size * info->io.regspacing)
1344 - (info->io.regspacing - info->io.regsize));
1345
1346 if (request_mem_region(*addr, mapsize, DEVICE_NAME) == NULL)
1347 return -EIO;
1348
1349 info->io.addr = ioremap(*addr, mapsize);
1350 if (info->io.addr == NULL) {
1351 release_mem_region(*addr, mapsize);
1352 return -EIO;
1353 }
1354 return 0;
1355}
1356
1357static int try_init_mem(int intf_num, struct smi_info **new_info)
1358{
1359 struct smi_info *info;
1360
1361 if (!addrs[intf_num])
1362 return -ENODEV;
1363
1364 if (!is_new_interface(intf_num, IPMI_MEM_ADDR_SPACE,
1365 addrs[intf_num]))
1366 return -ENODEV;
1367
1368 info = kmalloc(sizeof(*info), GFP_KERNEL);
1369 if (!info) {
1370 printk(KERN_ERR "ipmi_si: Could not allocate SI data (2)\n");
1371 return -ENOMEM;
1372 }
1373 memset(info, 0, sizeof(*info));
1374
1375 info->io_setup = mem_setup;
1376 info->io.info = &addrs[intf_num];
1377 info->io.addr = NULL;
1378 info->io.regspacing = regspacings[intf_num];
1379 if (!info->io.regspacing)
1380 info->io.regspacing = DEFAULT_REGSPACING;
1381 info->io.regsize = regsizes[intf_num];
1382 if (!info->io.regsize)
1383 info->io.regsize = DEFAULT_REGSPACING;
1384 info->io.regshift = regshifts[intf_num];
1385 info->irq = 0;
1386 info->irq_setup = NULL;
1387 *new_info = info;
1388
1389 if (si_type[intf_num] == NULL)
1390 si_type[intf_num] = "kcs";
1391
1392 printk("ipmi_si: Trying \"%s\" at memory address 0x%lx\n",
1393 si_type[intf_num], addrs[intf_num]);
1394 return 0;
1395}
1396
1397
1398#ifdef CONFIG_ACPI_INTERPRETER
1399
1400#include <linux/acpi.h>
1401
1402/* Once we get an ACPI failure, we don't try any more, because we go
1403 through the tables sequentially. Once we don't find a table, there
1404 are no more. */
1405static int acpi_failure = 0;
1406
1407/* For GPE-type interrupts. */
1408static u32 ipmi_acpi_gpe(void *context)
1409{
1410 struct smi_info *smi_info = context;
1411 unsigned long flags;
1412#ifdef DEBUG_TIMING
1413 struct timeval t;
1414#endif
1415
1416 spin_lock_irqsave(&(smi_info->si_lock), flags);
1417
1418 spin_lock(&smi_info->count_lock);
1419 smi_info->interrupts++;
1420 spin_unlock(&smi_info->count_lock);
1421
1422 if (smi_info->stop_operation)
1423 goto out;
1424
1425#ifdef DEBUG_TIMING
1426 do_gettimeofday(&t);
1427 printk("**ACPI_GPE: %d.%9.9d\n", t.tv_sec, t.tv_usec);
1428#endif
1429 smi_event_handler(smi_info, 0);
1430 out:
1431 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1432
1433 return ACPI_INTERRUPT_HANDLED;
1434}
1435
1436static int acpi_gpe_irq_setup(struct smi_info *info)
1437{
1438 acpi_status status;
1439
1440 if (!info->irq)
1441 return 0;
1442
1443 /* FIXME - is level triggered right? */
1444 status = acpi_install_gpe_handler(NULL,
1445 info->irq,
1446 ACPI_GPE_LEVEL_TRIGGERED,
1447 &ipmi_acpi_gpe,
1448 info);
1449 if (status != AE_OK) {
1450 printk(KERN_WARNING
1451 "ipmi_si: %s unable to claim ACPI GPE %d,"
1452 " running polled\n",
1453 DEVICE_NAME, info->irq);
1454 info->irq = 0;
1455 return -EINVAL;
1456 } else {
1457 printk(" Using ACPI GPE %d\n", info->irq);
1458 return 0;
1459 }
1460}
1461
1462static void acpi_gpe_irq_cleanup(struct smi_info *info)
1463{
1464 if (!info->irq)
1465 return;
1466
1467 acpi_remove_gpe_handler(NULL, info->irq, &ipmi_acpi_gpe);
1468}
1469
1470/*
1471 * Defined at
1472 * http://h21007.www2.hp.com/dspp/files/unprotected/devresource/Docs/TechPapers/IA64/hpspmi.pdf
1473 */
1474struct SPMITable {
1475 s8 Signature[4];
1476 u32 Length;
1477 u8 Revision;
1478 u8 Checksum;
1479 s8 OEMID[6];
1480 s8 OEMTableID[8];
1481 s8 OEMRevision[4];
1482 s8 CreatorID[4];
1483 s8 CreatorRevision[4];
1484 u8 InterfaceType;
1485 u8 IPMIlegacy;
1486 s16 SpecificationRevision;
1487
1488 /*
1489 * Bit 0 - SCI interrupt supported
1490 * Bit 1 - I/O APIC/SAPIC
1491 */
1492 u8 InterruptType;
1493
1494 /* If bit 0 of InterruptType is set, then this is the SCI
1495 interrupt in the GPEx_STS register. */
1496 u8 GPE;
1497
1498 s16 Reserved;
1499
1500 /* If bit 1 of InterruptType is set, then this is the I/O
1501 APIC/SAPIC interrupt. */
1502 u32 GlobalSystemInterrupt;
1503
1504 /* The actual register address. */
1505 struct acpi_generic_address addr;
1506
1507 u8 UID[4];
1508
1509 s8 spmi_id[1]; /* A '\0' terminated array starts here. */
1510};
1511
1512static int try_init_acpi(int intf_num, struct smi_info **new_info)
1513{
1514 struct smi_info *info;
1515 acpi_status status;
1516 struct SPMITable *spmi;
1517 char *io_type;
1518 u8 addr_space;
1519
1520 if (acpi_failure)
1521 return -ENODEV;
1522
1523 status = acpi_get_firmware_table("SPMI", intf_num+1,
1524 ACPI_LOGICAL_ADDRESSING,
1525 (struct acpi_table_header **) &spmi);
1526 if (status != AE_OK) {
1527 acpi_failure = 1;
1528 return -ENODEV;
1529 }
1530
1531 if (spmi->IPMIlegacy != 1) {
1532 printk(KERN_INFO "IPMI: Bad SPMI legacy %d\n", spmi->IPMIlegacy);
1533 return -ENODEV;
1534 }
1535
1536 if (spmi->addr.address_space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
1537 addr_space = IPMI_MEM_ADDR_SPACE;
1538 else
1539 addr_space = IPMI_IO_ADDR_SPACE;
1540 if (!is_new_interface(-1, addr_space, spmi->addr.address))
1541 return -ENODEV;
1542
1543 if (!spmi->addr.register_bit_width) {
1544 acpi_failure = 1;
1545 return -ENODEV;
1546 }
1547
1548 /* Figure out the interface type. */
1549 switch (spmi->InterfaceType)
1550 {
1551 case 1: /* KCS */
1552 si_type[intf_num] = "kcs";
1553 break;
1554
1555 case 2: /* SMIC */
1556 si_type[intf_num] = "smic";
1557 break;
1558
1559 case 3: /* BT */
1560 si_type[intf_num] = "bt";
1561 break;
1562
1563 default:
1564 printk(KERN_INFO "ipmi_si: Unknown ACPI/SPMI SI type %d\n",
1565 spmi->InterfaceType);
1566 return -EIO;
1567 }
1568
1569 info = kmalloc(sizeof(*info), GFP_KERNEL);
1570 if (!info) {
1571 printk(KERN_ERR "ipmi_si: Could not allocate SI data (3)\n");
1572 return -ENOMEM;
1573 }
1574 memset(info, 0, sizeof(*info));
1575
1576 if (spmi->InterruptType & 1) {
1577 /* We've got a GPE interrupt. */
1578 info->irq = spmi->GPE;
1579 info->irq_setup = acpi_gpe_irq_setup;
1580 info->irq_cleanup = acpi_gpe_irq_cleanup;
1581 } else if (spmi->InterruptType & 2) {
1582 /* We've got an APIC/SAPIC interrupt. */
1583 info->irq = spmi->GlobalSystemInterrupt;
1584 info->irq_setup = std_irq_setup;
1585 info->irq_cleanup = std_irq_cleanup;
1586 } else {
1587 /* Use the default interrupt setting. */
1588 info->irq = 0;
1589 info->irq_setup = NULL;
1590 }
1591
Corey Minyard35bc37a2005-05-01 08:59:10 -07001592 if (spmi->addr.register_bit_width) {
1593 /* A (hopefully) properly formed register bit width. */
1594 regspacings[intf_num] = spmi->addr.register_bit_width / 8;
1595 info->io.regspacing = spmi->addr.register_bit_width / 8;
1596 } else {
1597 /* Some broken systems get this wrong and set the value
1598 * to zero. Assume it is the default spacing. If that
1599 * is wrong, too bad, the vendor should fix the tables. */
1600 regspacings[intf_num] = DEFAULT_REGSPACING;
1601 info->io.regspacing = DEFAULT_REGSPACING;
1602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 regsizes[intf_num] = regspacings[intf_num];
1604 info->io.regsize = regsizes[intf_num];
1605 regshifts[intf_num] = spmi->addr.register_bit_offset;
1606 info->io.regshift = regshifts[intf_num];
1607
1608 if (spmi->addr.address_space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
1609 io_type = "memory";
1610 info->io_setup = mem_setup;
1611 addrs[intf_num] = spmi->addr.address;
1612 info->io.info = &(addrs[intf_num]);
1613 } else if (spmi->addr.address_space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
1614 io_type = "I/O";
1615 info->io_setup = port_setup;
1616 ports[intf_num] = spmi->addr.address;
1617 info->io.info = &(ports[intf_num]);
1618 } else {
1619 kfree(info);
1620 printk("ipmi_si: Unknown ACPI I/O Address type\n");
1621 return -EIO;
1622 }
1623
1624 *new_info = info;
1625
1626 printk("ipmi_si: ACPI/SPMI specifies \"%s\" %s SI @ 0x%lx\n",
1627 si_type[intf_num], io_type, (unsigned long) spmi->addr.address);
1628 return 0;
1629}
1630#endif
1631
1632#ifdef CONFIG_X86
1633typedef struct dmi_ipmi_data
1634{
1635 u8 type;
1636 u8 addr_space;
1637 unsigned long base_addr;
1638 u8 irq;
1639 u8 offset;
1640 u8 slave_addr;
1641} dmi_ipmi_data_t;
1642
1643static dmi_ipmi_data_t dmi_data[SI_MAX_DRIVERS];
1644static int dmi_data_entries;
1645
Andrey Paninb224cd32005-09-06 15:18:37 -07001646static int __init decode_dmi(struct dmi_header *dm, int intf_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
Andrey Paninb224cd32005-09-06 15:18:37 -07001648 u8 *data = (u8 *)dm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 unsigned long base_addr;
1650 u8 reg_spacing;
Andrey Paninb224cd32005-09-06 15:18:37 -07001651 u8 len = dm->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 dmi_ipmi_data_t *ipmi_data = dmi_data+intf_num;
1653
Andrey Paninb224cd32005-09-06 15:18:37 -07001654 ipmi_data->type = data[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 memcpy(&base_addr, data+8, sizeof(unsigned long));
1657 if (len >= 0x11) {
1658 if (base_addr & 1) {
1659 /* I/O */
1660 base_addr &= 0xFFFE;
1661 ipmi_data->addr_space = IPMI_IO_ADDR_SPACE;
1662 }
1663 else {
1664 /* Memory */
1665 ipmi_data->addr_space = IPMI_MEM_ADDR_SPACE;
1666 }
1667 /* If bit 4 of byte 0x10 is set, then the lsb for the address
1668 is odd. */
Andrey Paninb224cd32005-09-06 15:18:37 -07001669 ipmi_data->base_addr = base_addr | ((data[0x10] & 0x10) >> 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Andrey Paninb224cd32005-09-06 15:18:37 -07001671 ipmi_data->irq = data[0x11];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 /* The top two bits of byte 0x10 hold the register spacing. */
Andrey Paninb224cd32005-09-06 15:18:37 -07001674 reg_spacing = (data[0x10] & 0xC0) >> 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 switch(reg_spacing){
1676 case 0x00: /* Byte boundaries */
1677 ipmi_data->offset = 1;
1678 break;
1679 case 0x01: /* 32-bit boundaries */
1680 ipmi_data->offset = 4;
1681 break;
1682 case 0x02: /* 16-byte boundaries */
1683 ipmi_data->offset = 16;
1684 break;
1685 default:
1686 /* Some other interface, just ignore it. */
1687 return -EIO;
1688 }
1689 } else {
1690 /* Old DMI spec. */
Corey Minyard92068802005-05-01 08:59:10 -07001691 /* Note that technically, the lower bit of the base
1692 * address should be 1 if the address is I/O and 0 if
1693 * the address is in memory. So many systems get that
1694 * wrong (and all that I have seen are I/O) so we just
1695 * ignore that bit and assume I/O. Systems that use
1696 * memory should use the newer spec, anyway. */
1697 ipmi_data->base_addr = base_addr & 0xfffe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 ipmi_data->addr_space = IPMI_IO_ADDR_SPACE;
1699 ipmi_data->offset = 1;
1700 }
1701
Andrey Paninb224cd32005-09-06 15:18:37 -07001702 ipmi_data->slave_addr = data[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 if (is_new_interface(-1, ipmi_data->addr_space,ipmi_data->base_addr)) {
1705 dmi_data_entries++;
1706 return 0;
1707 }
1708
1709 memset(ipmi_data, 0, sizeof(dmi_ipmi_data_t));
1710
1711 return -1;
1712}
1713
Andrey Paninb224cd32005-09-06 15:18:37 -07001714static void __init dmi_find_bmc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
Andrey Paninb224cd32005-09-06 15:18:37 -07001716 struct dmi_device *dev = NULL;
1717 int intf_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Andrey Paninb224cd32005-09-06 15:18:37 -07001719 while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev))) {
1720 if (intf_num >= SI_MAX_DRIVERS)
1721 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Andrey Paninb224cd32005-09-06 15:18:37 -07001723 decode_dmi((struct dmi_header *) dev->device_data, intf_num++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726
1727static int try_init_smbios(int intf_num, struct smi_info **new_info)
1728{
1729 struct smi_info *info;
1730 dmi_ipmi_data_t *ipmi_data = dmi_data+intf_num;
1731 char *io_type;
1732
1733 if (intf_num >= dmi_data_entries)
1734 return -ENODEV;
1735
1736 switch(ipmi_data->type) {
1737 case 0x01: /* KCS */
1738 si_type[intf_num] = "kcs";
1739 break;
1740 case 0x02: /* SMIC */
1741 si_type[intf_num] = "smic";
1742 break;
1743 case 0x03: /* BT */
1744 si_type[intf_num] = "bt";
1745 break;
1746 default:
1747 return -EIO;
1748 }
1749
1750 info = kmalloc(sizeof(*info), GFP_KERNEL);
1751 if (!info) {
1752 printk(KERN_ERR "ipmi_si: Could not allocate SI data (4)\n");
1753 return -ENOMEM;
1754 }
1755 memset(info, 0, sizeof(*info));
1756
1757 if (ipmi_data->addr_space == 1) {
1758 io_type = "memory";
1759 info->io_setup = mem_setup;
1760 addrs[intf_num] = ipmi_data->base_addr;
1761 info->io.info = &(addrs[intf_num]);
1762 } else if (ipmi_data->addr_space == 2) {
1763 io_type = "I/O";
1764 info->io_setup = port_setup;
1765 ports[intf_num] = ipmi_data->base_addr;
1766 info->io.info = &(ports[intf_num]);
1767 } else {
1768 kfree(info);
1769 printk("ipmi_si: Unknown SMBIOS I/O Address type.\n");
1770 return -EIO;
1771 }
1772
1773 regspacings[intf_num] = ipmi_data->offset;
1774 info->io.regspacing = regspacings[intf_num];
1775 if (!info->io.regspacing)
1776 info->io.regspacing = DEFAULT_REGSPACING;
1777 info->io.regsize = DEFAULT_REGSPACING;
1778 info->io.regshift = regshifts[intf_num];
1779
1780 info->slave_addr = ipmi_data->slave_addr;
1781
1782 irqs[intf_num] = ipmi_data->irq;
1783
1784 *new_info = info;
1785
1786 printk("ipmi_si: Found SMBIOS-specified state machine at %s"
1787 " address 0x%lx, slave address 0x%x\n",
1788 io_type, (unsigned long)ipmi_data->base_addr,
1789 ipmi_data->slave_addr);
1790 return 0;
1791}
1792#endif /* CONFIG_X86 */
1793
1794#ifdef CONFIG_PCI
1795
1796#define PCI_ERMC_CLASSCODE 0x0C0700
1797#define PCI_HP_VENDOR_ID 0x103C
1798#define PCI_MMC_DEVICE_ID 0x121A
1799#define PCI_MMC_ADDR_CW 0x10
1800
1801/* Avoid more than one attempt to probe pci smic. */
1802static int pci_smic_checked = 0;
1803
1804static int find_pci_smic(int intf_num, struct smi_info **new_info)
1805{
1806 struct smi_info *info;
1807 int error;
1808 struct pci_dev *pci_dev = NULL;
1809 u16 base_addr;
1810 int fe_rmc = 0;
1811
1812 if (pci_smic_checked)
1813 return -ENODEV;
1814
1815 pci_smic_checked = 1;
1816
1817 if ((pci_dev = pci_get_device(PCI_HP_VENDOR_ID, PCI_MMC_DEVICE_ID,
1818 NULL)))
1819 ;
1820 else if ((pci_dev = pci_get_class(PCI_ERMC_CLASSCODE, NULL)) &&
1821 pci_dev->subsystem_vendor == PCI_HP_VENDOR_ID)
1822 fe_rmc = 1;
1823 else
1824 return -ENODEV;
1825
1826 error = pci_read_config_word(pci_dev, PCI_MMC_ADDR_CW, &base_addr);
1827 if (error)
1828 {
1829 pci_dev_put(pci_dev);
1830 printk(KERN_ERR
1831 "ipmi_si: pci_read_config_word() failed (%d).\n",
1832 error);
1833 return -ENODEV;
1834 }
1835
1836 /* Bit 0: 1 specifies programmed I/O, 0 specifies memory mapped I/O */
1837 if (!(base_addr & 0x0001))
1838 {
1839 pci_dev_put(pci_dev);
1840 printk(KERN_ERR
1841 "ipmi_si: memory mapped I/O not supported for PCI"
1842 " smic.\n");
1843 return -ENODEV;
1844 }
1845
1846 base_addr &= 0xFFFE;
1847 if (!fe_rmc)
1848 /* Data register starts at base address + 1 in eRMC */
1849 ++base_addr;
1850
1851 if (!is_new_interface(-1, IPMI_IO_ADDR_SPACE, base_addr)) {
1852 pci_dev_put(pci_dev);
1853 return -ENODEV;
1854 }
1855
1856 info = kmalloc(sizeof(*info), GFP_KERNEL);
1857 if (!info) {
1858 pci_dev_put(pci_dev);
1859 printk(KERN_ERR "ipmi_si: Could not allocate SI data (5)\n");
1860 return -ENOMEM;
1861 }
1862 memset(info, 0, sizeof(*info));
1863
1864 info->io_setup = port_setup;
1865 ports[intf_num] = base_addr;
1866 info->io.info = &(ports[intf_num]);
1867 info->io.regspacing = regspacings[intf_num];
1868 if (!info->io.regspacing)
1869 info->io.regspacing = DEFAULT_REGSPACING;
1870 info->io.regsize = DEFAULT_REGSPACING;
1871 info->io.regshift = regshifts[intf_num];
1872
1873 *new_info = info;
1874
1875 irqs[intf_num] = pci_dev->irq;
1876 si_type[intf_num] = "smic";
1877
1878 printk("ipmi_si: Found PCI SMIC at I/O address 0x%lx\n",
1879 (long unsigned int) base_addr);
1880
1881 pci_dev_put(pci_dev);
1882 return 0;
1883}
1884#endif /* CONFIG_PCI */
1885
1886static int try_init_plug_and_play(int intf_num, struct smi_info **new_info)
1887{
1888#ifdef CONFIG_PCI
1889 if (find_pci_smic(intf_num, new_info)==0)
1890 return 0;
1891#endif
1892 /* Include other methods here. */
1893
1894 return -ENODEV;
1895}
1896
1897
1898static int try_get_dev_id(struct smi_info *smi_info)
1899{
1900 unsigned char msg[2];
1901 unsigned char *resp;
1902 unsigned long resp_len;
1903 enum si_sm_result smi_result;
1904 int rv = 0;
1905
1906 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1907 if (!resp)
1908 return -ENOMEM;
1909
1910 /* Do a Get Device ID command, since it comes back with some
1911 useful info. */
1912 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1913 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1914 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
1915
1916 smi_result = smi_info->handlers->event(smi_info->si_sm, 0);
1917 for (;;)
1918 {
1919 if (smi_result == SI_SM_CALL_WITH_DELAY) {
1920 set_current_state(TASK_UNINTERRUPTIBLE);
1921 schedule_timeout(1);
1922 smi_result = smi_info->handlers->event(
1923 smi_info->si_sm, 100);
1924 }
1925 else if (smi_result == SI_SM_CALL_WITHOUT_DELAY)
1926 {
1927 smi_result = smi_info->handlers->event(
1928 smi_info->si_sm, 0);
1929 }
1930 else
1931 break;
1932 }
1933 if (smi_result == SI_SM_HOSED) {
1934 /* We couldn't get the state machine to run, so whatever's at
1935 the port is probably not an IPMI SMI interface. */
1936 rv = -ENODEV;
1937 goto out;
1938 }
1939
1940 /* Otherwise, we got some data. */
1941 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
1942 resp, IPMI_MAX_MSG_LENGTH);
1943 if (resp_len < 6) {
1944 /* That's odd, it should be longer. */
1945 rv = -EINVAL;
1946 goto out;
1947 }
1948
1949 if ((resp[1] != IPMI_GET_DEVICE_ID_CMD) || (resp[2] != 0)) {
1950 /* That's odd, it shouldn't be able to fail. */
1951 rv = -EINVAL;
1952 goto out;
1953 }
1954
1955 /* Record info from the get device id, in case we need it. */
Corey Minyard3ae0e0f2005-09-06 15:18:41 -07001956 memcpy(&smi_info->device_id, &resp[3],
1957 min_t(unsigned long, resp_len-3, sizeof(smi_info->device_id)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959 out:
1960 kfree(resp);
1961 return rv;
1962}
1963
1964static int type_file_read_proc(char *page, char **start, off_t off,
1965 int count, int *eof, void *data)
1966{
1967 char *out = (char *) page;
1968 struct smi_info *smi = data;
1969
1970 switch (smi->si_type) {
1971 case SI_KCS:
1972 return sprintf(out, "kcs\n");
1973 case SI_SMIC:
1974 return sprintf(out, "smic\n");
1975 case SI_BT:
1976 return sprintf(out, "bt\n");
1977 default:
1978 return 0;
1979 }
1980}
1981
1982static int stat_file_read_proc(char *page, char **start, off_t off,
1983 int count, int *eof, void *data)
1984{
1985 char *out = (char *) page;
1986 struct smi_info *smi = data;
1987
1988 out += sprintf(out, "interrupts_enabled: %d\n",
1989 smi->irq && !smi->interrupt_disabled);
1990 out += sprintf(out, "short_timeouts: %ld\n",
1991 smi->short_timeouts);
1992 out += sprintf(out, "long_timeouts: %ld\n",
1993 smi->long_timeouts);
1994 out += sprintf(out, "timeout_restarts: %ld\n",
1995 smi->timeout_restarts);
1996 out += sprintf(out, "idles: %ld\n",
1997 smi->idles);
1998 out += sprintf(out, "interrupts: %ld\n",
1999 smi->interrupts);
2000 out += sprintf(out, "attentions: %ld\n",
2001 smi->attentions);
2002 out += sprintf(out, "flag_fetches: %ld\n",
2003 smi->flag_fetches);
2004 out += sprintf(out, "hosed_count: %ld\n",
2005 smi->hosed_count);
2006 out += sprintf(out, "complete_transactions: %ld\n",
2007 smi->complete_transactions);
2008 out += sprintf(out, "events: %ld\n",
2009 smi->events);
2010 out += sprintf(out, "watchdog_pretimeouts: %ld\n",
2011 smi->watchdog_pretimeouts);
2012 out += sprintf(out, "incoming_messages: %ld\n",
2013 smi->incoming_messages);
2014
2015 return (out - ((char *) page));
2016}
2017
Corey Minyard3ae0e0f2005-09-06 15:18:41 -07002018/*
2019 * oem_data_avail_to_receive_msg_avail
2020 * @info - smi_info structure with msg_flags set
2021 *
2022 * Converts flags from OEM_DATA_AVAIL to RECEIVE_MSG_AVAIL
2023 * Returns 1 indicating need to re-run handle_flags().
2024 */
2025static int oem_data_avail_to_receive_msg_avail(struct smi_info *smi_info)
2026{
2027 smi_info->msg_flags = (smi_info->msg_flags & ~OEM_DATA_AVAIL) |
2028 RECEIVE_MSG_AVAIL;
2029 return 1;
2030}
2031
2032/*
2033 * setup_dell_poweredge_oem_data_handler
2034 * @info - smi_info.device_id must be populated
2035 *
2036 * Systems that match, but have firmware version < 1.40 may assert
2037 * OEM0_DATA_AVAIL on their own, without being told via Set Flags that
2038 * it's safe to do so. Such systems will de-assert OEM1_DATA_AVAIL
2039 * upon receipt of IPMI_GET_MSG_CMD, so we should treat these flags
2040 * as RECEIVE_MSG_AVAIL instead.
2041 *
2042 * As Dell has no plans to release IPMI 1.5 firmware that *ever*
2043 * assert the OEM[012] bits, and if it did, the driver would have to
2044 * change to handle that properly, we don't actually check for the
2045 * firmware version.
2046 * Device ID = 0x20 BMC on PowerEdge 8G servers
2047 * Device Revision = 0x80
2048 * Firmware Revision1 = 0x01 BMC version 1.40
2049 * Firmware Revision2 = 0x40 BCD encoded
2050 * IPMI Version = 0x51 IPMI 1.5
2051 * Manufacturer ID = A2 02 00 Dell IANA
2052 *
2053 */
2054#define DELL_POWEREDGE_8G_BMC_DEVICE_ID 0x20
2055#define DELL_POWEREDGE_8G_BMC_DEVICE_REV 0x80
2056#define DELL_POWEREDGE_8G_BMC_IPMI_VERSION 0x51
2057#define DELL_IANA_MFR_ID {0xA2, 0x02, 0x00}
2058static void setup_dell_poweredge_oem_data_handler(struct smi_info *smi_info)
2059{
2060 struct ipmi_device_id *id = &smi_info->device_id;
2061 const char mfr[3]=DELL_IANA_MFR_ID;
2062 if (!memcmp(mfr, id->manufacturer_id, sizeof(mfr)) &&
2063 id->device_id == DELL_POWEREDGE_8G_BMC_DEVICE_ID &&
2064 id->device_revision == DELL_POWEREDGE_8G_BMC_DEVICE_REV &&
2065 id->ipmi_version == DELL_POWEREDGE_8G_BMC_IPMI_VERSION) {
2066 smi_info->oem_data_avail_handler =
2067 oem_data_avail_to_receive_msg_avail;
2068 }
2069}
2070
2071/*
2072 * setup_oem_data_handler
2073 * @info - smi_info.device_id must be filled in already
2074 *
2075 * Fills in smi_info.device_id.oem_data_available_handler
2076 * when we know what function to use there.
2077 */
2078
2079static void setup_oem_data_handler(struct smi_info *smi_info)
2080{
2081 setup_dell_poweredge_oem_data_handler(smi_info);
2082}
2083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084/* Returns 0 if initialized, or negative on an error. */
2085static int init_one_smi(int intf_num, struct smi_info **smi)
2086{
2087 int rv;
2088 struct smi_info *new_smi;
2089
2090
2091 rv = try_init_mem(intf_num, &new_smi);
2092 if (rv)
2093 rv = try_init_port(intf_num, &new_smi);
2094#ifdef CONFIG_ACPI_INTERPRETER
2095 if ((rv) && (si_trydefaults)) {
2096 rv = try_init_acpi(intf_num, &new_smi);
2097 }
2098#endif
2099#ifdef CONFIG_X86
2100 if ((rv) && (si_trydefaults)) {
2101 rv = try_init_smbios(intf_num, &new_smi);
2102 }
2103#endif
2104 if ((rv) && (si_trydefaults)) {
2105 rv = try_init_plug_and_play(intf_num, &new_smi);
2106 }
2107
2108
2109 if (rv)
2110 return rv;
2111
2112 /* So we know not to free it unless we have allocated one. */
2113 new_smi->intf = NULL;
2114 new_smi->si_sm = NULL;
2115 new_smi->handlers = NULL;
2116
2117 if (!new_smi->irq_setup) {
2118 new_smi->irq = irqs[intf_num];
2119 new_smi->irq_setup = std_irq_setup;
2120 new_smi->irq_cleanup = std_irq_cleanup;
2121 }
2122
2123 /* Default to KCS if no type is specified. */
2124 if (si_type[intf_num] == NULL) {
2125 if (si_trydefaults)
2126 si_type[intf_num] = "kcs";
2127 else {
2128 rv = -EINVAL;
2129 goto out_err;
2130 }
2131 }
2132
2133 /* Set up the state machine to use. */
2134 if (strcmp(si_type[intf_num], "kcs") == 0) {
2135 new_smi->handlers = &kcs_smi_handlers;
2136 new_smi->si_type = SI_KCS;
2137 } else if (strcmp(si_type[intf_num], "smic") == 0) {
2138 new_smi->handlers = &smic_smi_handlers;
2139 new_smi->si_type = SI_SMIC;
2140 } else if (strcmp(si_type[intf_num], "bt") == 0) {
2141 new_smi->handlers = &bt_smi_handlers;
2142 new_smi->si_type = SI_BT;
2143 } else {
2144 /* No support for anything else yet. */
2145 rv = -EIO;
2146 goto out_err;
2147 }
2148
2149 /* Allocate the state machine's data and initialize it. */
2150 new_smi->si_sm = kmalloc(new_smi->handlers->size(), GFP_KERNEL);
2151 if (!new_smi->si_sm) {
2152 printk(" Could not allocate state machine memory\n");
2153 rv = -ENOMEM;
2154 goto out_err;
2155 }
2156 new_smi->io_size = new_smi->handlers->init_data(new_smi->si_sm,
2157 &new_smi->io);
2158
2159 /* Now that we know the I/O size, we can set up the I/O. */
2160 rv = new_smi->io_setup(new_smi);
2161 if (rv) {
2162 printk(" Could not set up I/O space\n");
2163 goto out_err;
2164 }
2165
2166 spin_lock_init(&(new_smi->si_lock));
2167 spin_lock_init(&(new_smi->msg_lock));
2168 spin_lock_init(&(new_smi->count_lock));
2169
2170 /* Do low-level detection first. */
2171 if (new_smi->handlers->detect(new_smi->si_sm)) {
2172 rv = -ENODEV;
2173 goto out_err;
2174 }
2175
2176 /* Attempt a get device id command. If it fails, we probably
2177 don't have a SMI here. */
2178 rv = try_get_dev_id(new_smi);
2179 if (rv)
2180 goto out_err;
2181
Corey Minyard3ae0e0f2005-09-06 15:18:41 -07002182 setup_oem_data_handler(new_smi);
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 /* Try to claim any interrupts. */
2185 new_smi->irq_setup(new_smi);
2186
2187 INIT_LIST_HEAD(&(new_smi->xmit_msgs));
2188 INIT_LIST_HEAD(&(new_smi->hp_xmit_msgs));
2189 new_smi->curr_msg = NULL;
2190 atomic_set(&new_smi->req_events, 0);
2191 new_smi->run_to_completion = 0;
2192
2193 new_smi->interrupt_disabled = 0;
2194 new_smi->timer_stopped = 0;
2195 new_smi->stop_operation = 0;
2196
2197 /* Start clearing the flags before we enable interrupts or the
2198 timer to avoid racing with the timer. */
2199 start_clear_flags(new_smi);
2200 /* IRQ is defined to be set when non-zero. */
2201 if (new_smi->irq)
2202 new_smi->si_state = SI_CLEARING_FLAGS_THEN_SET_IRQ;
2203
2204 /* The ipmi_register_smi() code does some operations to
2205 determine the channel information, so we must be ready to
2206 handle operations before it is called. This means we have
2207 to stop the timer if we get an error after this point. */
2208 init_timer(&(new_smi->si_timer));
2209 new_smi->si_timer.data = (long) new_smi;
2210 new_smi->si_timer.function = smi_timeout;
2211 new_smi->last_timeout_jiffies = jiffies;
2212 new_smi->si_timer.expires = jiffies + SI_TIMEOUT_JIFFIES;
2213 add_timer(&(new_smi->si_timer));
2214
2215 rv = ipmi_register_smi(&handlers,
2216 new_smi,
Corey Minyard3ae0e0f2005-09-06 15:18:41 -07002217 ipmi_version_major(&new_smi->device_id),
2218 ipmi_version_minor(&new_smi->device_id),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 new_smi->slave_addr,
2220 &(new_smi->intf));
2221 if (rv) {
2222 printk(KERN_ERR
2223 "ipmi_si: Unable to register device: error %d\n",
2224 rv);
2225 goto out_err_stop_timer;
2226 }
2227
2228 rv = ipmi_smi_add_proc_entry(new_smi->intf, "type",
2229 type_file_read_proc, NULL,
2230 new_smi, THIS_MODULE);
2231 if (rv) {
2232 printk(KERN_ERR
2233 "ipmi_si: Unable to create proc entry: %d\n",
2234 rv);
2235 goto out_err_stop_timer;
2236 }
2237
2238 rv = ipmi_smi_add_proc_entry(new_smi->intf, "si_stats",
2239 stat_file_read_proc, NULL,
2240 new_smi, THIS_MODULE);
2241 if (rv) {
2242 printk(KERN_ERR
2243 "ipmi_si: Unable to create proc entry: %d\n",
2244 rv);
2245 goto out_err_stop_timer;
2246 }
2247
2248 *smi = new_smi;
2249
2250 printk(" IPMI %s interface initialized\n", si_type[intf_num]);
2251
2252 return 0;
2253
2254 out_err_stop_timer:
2255 new_smi->stop_operation = 1;
2256
2257 /* Wait for the timer to stop. This avoids problems with race
2258 conditions removing the timer here. */
2259 while (!new_smi->timer_stopped) {
2260 set_current_state(TASK_UNINTERRUPTIBLE);
2261 schedule_timeout(1);
2262 }
2263
2264 out_err:
2265 if (new_smi->intf)
2266 ipmi_unregister_smi(new_smi->intf);
2267
2268 new_smi->irq_cleanup(new_smi);
2269
2270 /* Wait until we know that we are out of any interrupt
2271 handlers might have been running before we freed the
2272 interrupt. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002273 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
2275 if (new_smi->si_sm) {
2276 if (new_smi->handlers)
2277 new_smi->handlers->cleanup(new_smi->si_sm);
2278 kfree(new_smi->si_sm);
2279 }
2280 new_smi->io_cleanup(new_smi);
2281
2282 return rv;
2283}
2284
2285static __init int init_ipmi_si(void)
2286{
2287 int rv = 0;
2288 int pos = 0;
2289 int i;
2290 char *str;
2291
2292 if (initialized)
2293 return 0;
2294 initialized = 1;
2295
2296 /* Parse out the si_type string into its components. */
2297 str = si_type_str;
2298 if (*str != '\0') {
2299 for (i=0; (i<SI_MAX_PARMS) && (*str != '\0'); i++) {
2300 si_type[i] = str;
2301 str = strchr(str, ',');
2302 if (str) {
2303 *str = '\0';
2304 str++;
2305 } else {
2306 break;
2307 }
2308 }
2309 }
2310
Corey Minyard1fdd75b2005-09-06 15:18:42 -07002311 printk(KERN_INFO "IPMI System Interface driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313#ifdef CONFIG_X86
Andrey Paninb224cd32005-09-06 15:18:37 -07002314 dmi_find_bmc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315#endif
2316
2317 rv = init_one_smi(0, &(smi_infos[pos]));
2318 if (rv && !ports[0] && si_trydefaults) {
2319 /* If we are trying defaults and the initial port is
2320 not set, then set it. */
2321 si_type[0] = "kcs";
2322 ports[0] = DEFAULT_KCS_IO_PORT;
2323 rv = init_one_smi(0, &(smi_infos[pos]));
2324 if (rv) {
2325 /* No KCS - try SMIC */
2326 si_type[0] = "smic";
2327 ports[0] = DEFAULT_SMIC_IO_PORT;
2328 rv = init_one_smi(0, &(smi_infos[pos]));
2329 }
2330 if (rv) {
2331 /* No SMIC - try BT */
2332 si_type[0] = "bt";
2333 ports[0] = DEFAULT_BT_IO_PORT;
2334 rv = init_one_smi(0, &(smi_infos[pos]));
2335 }
2336 }
2337 if (rv == 0)
2338 pos++;
2339
2340 for (i=1; i < SI_MAX_PARMS; i++) {
2341 rv = init_one_smi(i, &(smi_infos[pos]));
2342 if (rv == 0)
2343 pos++;
2344 }
2345
2346 if (smi_infos[0] == NULL) {
2347 printk("ipmi_si: Unable to find any System Interface(s)\n");
2348 return -ENODEV;
2349 }
2350
2351 return 0;
2352}
2353module_init(init_ipmi_si);
2354
2355static void __exit cleanup_one_si(struct smi_info *to_clean)
2356{
2357 int rv;
2358 unsigned long flags;
2359
2360 if (! to_clean)
2361 return;
2362
2363 /* Tell the timer and interrupt handlers that we are shutting
2364 down. */
2365 spin_lock_irqsave(&(to_clean->si_lock), flags);
2366 spin_lock(&(to_clean->msg_lock));
2367
2368 to_clean->stop_operation = 1;
2369
2370 to_clean->irq_cleanup(to_clean);
2371
2372 spin_unlock(&(to_clean->msg_lock));
2373 spin_unlock_irqrestore(&(to_clean->si_lock), flags);
2374
2375 /* Wait until we know that we are out of any interrupt
2376 handlers might have been running before we freed the
2377 interrupt. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002378 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
2380 /* Wait for the timer to stop. This avoids problems with race
2381 conditions removing the timer here. */
2382 while (!to_clean->timer_stopped) {
2383 set_current_state(TASK_UNINTERRUPTIBLE);
2384 schedule_timeout(1);
2385 }
2386
2387 /* Interrupts and timeouts are stopped, now make sure the
2388 interface is in a clean state. */
2389 while ((to_clean->curr_msg) || (to_clean->si_state != SI_NORMAL)) {
2390 poll(to_clean);
2391 set_current_state(TASK_UNINTERRUPTIBLE);
2392 schedule_timeout(1);
2393 }
2394
2395 rv = ipmi_unregister_smi(to_clean->intf);
2396 if (rv) {
2397 printk(KERN_ERR
2398 "ipmi_si: Unable to unregister device: errno=%d\n",
2399 rv);
2400 }
2401
2402 to_clean->handlers->cleanup(to_clean->si_sm);
2403
2404 kfree(to_clean->si_sm);
2405
2406 to_clean->io_cleanup(to_clean);
2407}
2408
2409static __exit void cleanup_ipmi_si(void)
2410{
2411 int i;
2412
2413 if (!initialized)
2414 return;
2415
2416 for (i=0; i<SI_MAX_DRIVERS; i++) {
2417 cleanup_one_si(smi_infos[i]);
2418 }
2419}
2420module_exit(cleanup_ipmi_si);
2421
2422MODULE_LICENSE("GPL");
Corey Minyard1fdd75b2005-09-06 15:18:42 -07002423MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
2424MODULE_DESCRIPTION("Interface to the IPMI driver for the KCS, SMIC, and BT system interfaces.");