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