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