1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * ipmi_msghandler.c 4 * 5 * Incoming and outgoing message routing for an IPMI interface. 6 * 7 * Author: MontaVista Software, Inc. 8 * Corey Minyard <minyard@mvista.com> 9 * source@mvista.com 10 * 11 * Copyright 2002 MontaVista Software Inc. 12 */ 13 14 #include <linux/module.h> 15 #include <linux/errno.h> 16 #include <linux/poll.h> 17 #include <linux/sched.h> 18 #include <linux/seq_file.h> 19 #include <linux/spinlock.h> 20 #include <linux/mutex.h> 21 #include <linux/slab.h> 22 #include <linux/ipmi.h> 23 #include <linux/ipmi_smi.h> 24 #include <linux/notifier.h> 25 #include <linux/init.h> 26 #include <linux/proc_fs.h> 27 #include <linux/rcupdate.h> 28 #include <linux/interrupt.h> 29 #include <linux/moduleparam.h> 30 #include <linux/workqueue.h> 31 #include <linux/uuid.h> 32 33 #define PFX "IPMI message handler: " 34 35 #define IPMI_DRIVER_VERSION "39.2" 36 37 static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void); 38 static int ipmi_init_msghandler(void); 39 static void smi_recv_tasklet(unsigned long); 40 static void handle_new_recv_msgs(struct ipmi_smi *intf); 41 static void need_waiter(struct ipmi_smi *intf); 42 static int handle_one_recv_msg(struct ipmi_smi *intf, 43 struct ipmi_smi_msg *msg); 44 45 #ifdef DEBUG 46 static void ipmi_debug_msg(const char *title, unsigned char *data, 47 unsigned int len) 48 { 49 int i, pos; 50 char buf[100]; 51 52 pos = snprintf(buf, sizeof(buf), "%s: ", title); 53 for (i = 0; i < len; i++) 54 pos += snprintf(buf + pos, sizeof(buf) - pos, 55 " %2.2x", data[i]); 56 pr_debug("%s\n", buf); 57 } 58 #else 59 static void ipmi_debug_msg(const char *title, unsigned char *data, 60 unsigned int len) 61 { } 62 #endif 63 64 static int initialized; 65 66 enum ipmi_panic_event_op { 67 IPMI_SEND_PANIC_EVENT_NONE, 68 IPMI_SEND_PANIC_EVENT, 69 IPMI_SEND_PANIC_EVENT_STRING 70 }; 71 #ifdef CONFIG_IPMI_PANIC_STRING 72 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT_STRING 73 #elif defined(CONFIG_IPMI_PANIC_EVENT) 74 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT 75 #else 76 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT_NONE 77 #endif 78 static enum ipmi_panic_event_op ipmi_send_panic_event = IPMI_PANIC_DEFAULT; 79 80 static int panic_op_write_handler(const char *val, 81 const struct kernel_param *kp) 82 { 83 char valcp[16]; 84 char *s; 85 86 strncpy(valcp, val, 15); 87 valcp[15] = '\0'; 88 89 s = strstrip(valcp); 90 91 if (strcmp(s, "none") == 0) 92 ipmi_send_panic_event = IPMI_SEND_PANIC_EVENT_NONE; 93 else if (strcmp(s, "event") == 0) 94 ipmi_send_panic_event = IPMI_SEND_PANIC_EVENT; 95 else if (strcmp(s, "string") == 0) 96 ipmi_send_panic_event = IPMI_SEND_PANIC_EVENT_STRING; 97 else 98 return -EINVAL; 99 100 return 0; 101 } 102 103 static int panic_op_read_handler(char *buffer, const struct kernel_param *kp) 104 { 105 switch (ipmi_send_panic_event) { 106 case IPMI_SEND_PANIC_EVENT_NONE: 107 strcpy(buffer, "none"); 108 break; 109 110 case IPMI_SEND_PANIC_EVENT: 111 strcpy(buffer, "event"); 112 break; 113 114 case IPMI_SEND_PANIC_EVENT_STRING: 115 strcpy(buffer, "string"); 116 break; 117 118 default: 119 strcpy(buffer, "???"); 120 break; 121 } 122 123 return strlen(buffer); 124 } 125 126 static const struct kernel_param_ops panic_op_ops = { 127 .set = panic_op_write_handler, 128 .get = panic_op_read_handler 129 }; 130 module_param_cb(panic_op, &panic_op_ops, NULL, 0600); 131 MODULE_PARM_DESC(panic_op, "Sets if the IPMI driver will attempt to store panic information in the event log in the event of a panic. Set to 'none' for no, 'event' for a single event, or 'string' for a generic event and the panic string in IPMI OEM events."); 132 133 134 #define MAX_EVENTS_IN_QUEUE 25 135 136 /* Remain in auto-maintenance mode for this amount of time (in ms). */ 137 static unsigned long maintenance_mode_timeout_ms = 30000; 138 module_param(maintenance_mode_timeout_ms, ulong, 0644); 139 MODULE_PARM_DESC(maintenance_mode_timeout_ms, 140 "The time (milliseconds) after the last maintenance message that the connection stays in maintenance mode."); 141 142 /* 143 * Don't let a message sit in a queue forever, always time it with at lest 144 * the max message timer. This is in milliseconds. 145 */ 146 #define MAX_MSG_TIMEOUT 60000 147 148 /* 149 * Timeout times below are in milliseconds, and are done off a 1 150 * second timer. So setting the value to 1000 would mean anything 151 * between 0 and 1000ms. So really the only reasonable minimum 152 * setting it 2000ms, which is between 1 and 2 seconds. 153 */ 154 155 /* The default timeout for message retries. */ 156 static unsigned long default_retry_ms = 2000; 157 module_param(default_retry_ms, ulong, 0644); 158 MODULE_PARM_DESC(default_retry_ms, 159 "The time (milliseconds) between retry sends"); 160 161 /* The default timeout for maintenance mode message retries. */ 162 static unsigned long default_maintenance_retry_ms = 3000; 163 module_param(default_maintenance_retry_ms, ulong, 0644); 164 MODULE_PARM_DESC(default_maintenance_retry_ms, 165 "The time (milliseconds) between retry sends in maintenance mode"); 166 167 /* The default maximum number of retries */ 168 static unsigned int default_max_retries = 4; 169 module_param(default_max_retries, uint, 0644); 170 MODULE_PARM_DESC(default_max_retries, 171 "The time (milliseconds) between retry sends in maintenance mode"); 172 173 /* Call every ~1000 ms. */ 174 #define IPMI_TIMEOUT_TIME 1000 175 176 /* How many jiffies does it take to get to the timeout time. */ 177 #define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000) 178 179 /* 180 * Request events from the queue every second (this is the number of 181 * IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the 182 * future, IPMI will add a way to know immediately if an event is in 183 * the queue and this silliness can go away. 184 */ 185 #define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME)) 186 187 /* How long should we cache dynamic device IDs? */ 188 #define IPMI_DYN_DEV_ID_EXPIRY (10 * HZ) 189 190 /* 191 * The main "user" data structure. 192 */ 193 struct ipmi_user { 194 struct list_head link; 195 196 /* 197 * Set to NULL when the user is destroyed, a pointer to myself 198 * so srcu_dereference can be used on it. 199 */ 200 struct ipmi_user *self; 201 struct srcu_struct release_barrier; 202 203 struct kref refcount; 204 205 /* The upper layer that handles receive messages. */ 206 const struct ipmi_user_hndl *handler; 207 void *handler_data; 208 209 /* The interface this user is bound to. */ 210 struct ipmi_smi *intf; 211 212 /* Does this interface receive IPMI events? */ 213 bool gets_events; 214 }; 215 216 static struct ipmi_user *acquire_ipmi_user(struct ipmi_user *user, int *index) 217 __acquires(user->release_barrier) 218 { 219 struct ipmi_user *ruser; 220 221 *index = srcu_read_lock(&user->release_barrier); 222 ruser = srcu_dereference(user->self, &user->release_barrier); 223 if (!ruser) 224 srcu_read_unlock(&user->release_barrier, *index); 225 return ruser; 226 } 227 228 static void release_ipmi_user(struct ipmi_user *user, int index) 229 { 230 srcu_read_unlock(&user->release_barrier, index); 231 } 232 233 struct cmd_rcvr { 234 struct list_head link; 235 236 struct ipmi_user *user; 237 unsigned char netfn; 238 unsigned char cmd; 239 unsigned int chans; 240 241 /* 242 * This is used to form a linked lised during mass deletion. 243 * Since this is in an RCU list, we cannot use the link above 244 * or change any data until the RCU period completes. So we 245 * use this next variable during mass deletion so we can have 246 * a list and don't have to wait and restart the search on 247 * every individual deletion of a command. 248 */ 249 struct cmd_rcvr *next; 250 }; 251 252 struct seq_table { 253 unsigned int inuse : 1; 254 unsigned int broadcast : 1; 255 256 unsigned long timeout; 257 unsigned long orig_timeout; 258 unsigned int retries_left; 259 260 /* 261 * To verify on an incoming send message response that this is 262 * the message that the response is for, we keep a sequence id 263 * and increment it every time we send a message. 264 */ 265 long seqid; 266 267 /* 268 * This is held so we can properly respond to the message on a 269 * timeout, and it is used to hold the temporary data for 270 * retransmission, too. 271 */ 272 struct ipmi_recv_msg *recv_msg; 273 }; 274 275 /* 276 * Store the information in a msgid (long) to allow us to find a 277 * sequence table entry from the msgid. 278 */ 279 #define STORE_SEQ_IN_MSGID(seq, seqid) \ 280 ((((seq) & 0x3f) << 26) | ((seqid) & 0x3ffffff)) 281 282 #define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \ 283 do { \ 284 seq = (((msgid) >> 26) & 0x3f); \ 285 seqid = ((msgid) & 0x3ffffff); \ 286 } while (0) 287 288 #define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3ffffff) 289 290 #define IPMI_MAX_CHANNELS 16 291 struct ipmi_channel { 292 unsigned char medium; 293 unsigned char protocol; 294 }; 295 296 struct ipmi_channel_set { 297 struct ipmi_channel c[IPMI_MAX_CHANNELS]; 298 }; 299 300 struct ipmi_my_addrinfo { 301 /* 302 * My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR, 303 * but may be changed by the user. 304 */ 305 unsigned char address; 306 307 /* 308 * My LUN. This should generally stay the SMS LUN, but just in 309 * case... 310 */ 311 unsigned char lun; 312 }; 313 314 /* 315 * Note that the product id, manufacturer id, guid, and device id are 316 * immutable in this structure, so dyn_mutex is not required for 317 * accessing those. If those change on a BMC, a new BMC is allocated. 318 */ 319 struct bmc_device { 320 struct platform_device pdev; 321 struct list_head intfs; /* Interfaces on this BMC. */ 322 struct ipmi_device_id id; 323 struct ipmi_device_id fetch_id; 324 int dyn_id_set; 325 unsigned long dyn_id_expiry; 326 struct mutex dyn_mutex; /* Protects id, intfs, & dyn* */ 327 guid_t guid; 328 guid_t fetch_guid; 329 int dyn_guid_set; 330 struct kref usecount; 331 struct work_struct remove_work; 332 }; 333 #define to_bmc_device(x) container_of((x), struct bmc_device, pdev.dev) 334 335 static int bmc_get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc, 336 struct ipmi_device_id *id, 337 bool *guid_set, guid_t *guid); 338 339 /* 340 * Various statistics for IPMI, these index stats[] in the ipmi_smi 341 * structure. 342 */ 343 enum ipmi_stat_indexes { 344 /* Commands we got from the user that were invalid. */ 345 IPMI_STAT_sent_invalid_commands = 0, 346 347 /* Commands we sent to the MC. */ 348 IPMI_STAT_sent_local_commands, 349 350 /* Responses from the MC that were delivered to a user. */ 351 IPMI_STAT_handled_local_responses, 352 353 /* Responses from the MC that were not delivered to a user. */ 354 IPMI_STAT_unhandled_local_responses, 355 356 /* Commands we sent out to the IPMB bus. */ 357 IPMI_STAT_sent_ipmb_commands, 358 359 /* Commands sent on the IPMB that had errors on the SEND CMD */ 360 IPMI_STAT_sent_ipmb_command_errs, 361 362 /* Each retransmit increments this count. */ 363 IPMI_STAT_retransmitted_ipmb_commands, 364 365 /* 366 * When a message times out (runs out of retransmits) this is 367 * incremented. 368 */ 369 IPMI_STAT_timed_out_ipmb_commands, 370 371 /* 372 * This is like above, but for broadcasts. Broadcasts are 373 * *not* included in the above count (they are expected to 374 * time out). 375 */ 376 IPMI_STAT_timed_out_ipmb_broadcasts, 377 378 /* Responses I have sent to the IPMB bus. */ 379 IPMI_STAT_sent_ipmb_responses, 380 381 /* The response was delivered to the user. */ 382 IPMI_STAT_handled_ipmb_responses, 383 384 /* The response had invalid data in it. */ 385 IPMI_STAT_invalid_ipmb_responses, 386 387 /* The response didn't have anyone waiting for it. */ 388 IPMI_STAT_unhandled_ipmb_responses, 389 390 /* Commands we sent out to the IPMB bus. */ 391 IPMI_STAT_sent_lan_commands, 392 393 /* Commands sent on the IPMB that had errors on the SEND CMD */ 394 IPMI_STAT_sent_lan_command_errs, 395 396 /* Each retransmit increments this count. */ 397 IPMI_STAT_retransmitted_lan_commands, 398 399 /* 400 * When a message times out (runs out of retransmits) this is 401 * incremented. 402 */ 403 IPMI_STAT_timed_out_lan_commands, 404 405 /* Responses I have sent to the IPMB bus. */ 406 IPMI_STAT_sent_lan_responses, 407 408 /* The response was delivered to the user. */ 409 IPMI_STAT_handled_lan_responses, 410 411 /* The response had invalid data in it. */ 412 IPMI_STAT_invalid_lan_responses, 413 414 /* The response didn't have anyone waiting for it. */ 415 IPMI_STAT_unhandled_lan_responses, 416 417 /* The command was delivered to the user. */ 418 IPMI_STAT_handled_commands, 419 420 /* The command had invalid data in it. */ 421 IPMI_STAT_invalid_commands, 422 423 /* The command didn't have anyone waiting for it. */ 424 IPMI_STAT_unhandled_commands, 425 426 /* Invalid data in an event. */ 427 IPMI_STAT_invalid_events, 428 429 /* Events that were received with the proper format. */ 430 IPMI_STAT_events, 431 432 /* Retransmissions on IPMB that failed. */ 433 IPMI_STAT_dropped_rexmit_ipmb_commands, 434 435 /* Retransmissions on LAN that failed. */ 436 IPMI_STAT_dropped_rexmit_lan_commands, 437 438 /* This *must* remain last, add new values above this. */ 439 IPMI_NUM_STATS 440 }; 441 442 443 #define IPMI_IPMB_NUM_SEQ 64 444 struct ipmi_smi { 445 /* What interface number are we? */ 446 int intf_num; 447 448 struct kref refcount; 449 450 /* Set when the interface is being unregistered. */ 451 bool in_shutdown; 452 453 /* Used for a list of interfaces. */ 454 struct list_head link; 455 456 /* 457 * The list of upper layers that are using me. seq_lock write 458 * protects this. Read protection is with srcu. 459 */ 460 struct list_head users; 461 struct srcu_struct users_srcu; 462 463 /* Used for wake ups at startup. */ 464 wait_queue_head_t waitq; 465 466 /* 467 * Prevents the interface from being unregistered when the 468 * interface is used by being looked up through the BMC 469 * structure. 470 */ 471 struct mutex bmc_reg_mutex; 472 473 struct bmc_device tmp_bmc; 474 struct bmc_device *bmc; 475 bool bmc_registered; 476 struct list_head bmc_link; 477 char *my_dev_name; 478 bool in_bmc_register; /* Handle recursive situations. Yuck. */ 479 struct work_struct bmc_reg_work; 480 481 const struct ipmi_smi_handlers *handlers; 482 void *send_info; 483 484 /* Driver-model device for the system interface. */ 485 struct device *si_dev; 486 487 /* 488 * A table of sequence numbers for this interface. We use the 489 * sequence numbers for IPMB messages that go out of the 490 * interface to match them up with their responses. A routine 491 * is called periodically to time the items in this list. 492 */ 493 spinlock_t seq_lock; 494 struct seq_table seq_table[IPMI_IPMB_NUM_SEQ]; 495 int curr_seq; 496 497 /* 498 * Messages queued for delivery. If delivery fails (out of memory 499 * for instance), They will stay in here to be processed later in a 500 * periodic timer interrupt. The tasklet is for handling received 501 * messages directly from the handler. 502 */ 503 spinlock_t waiting_rcv_msgs_lock; 504 struct list_head waiting_rcv_msgs; 505 atomic_t watchdog_pretimeouts_to_deliver; 506 struct tasklet_struct recv_tasklet; 507 508 spinlock_t xmit_msgs_lock; 509 struct list_head xmit_msgs; 510 struct ipmi_smi_msg *curr_msg; 511 struct list_head hp_xmit_msgs; 512 513 /* 514 * The list of command receivers that are registered for commands 515 * on this interface. 516 */ 517 struct mutex cmd_rcvrs_mutex; 518 struct list_head cmd_rcvrs; 519 520 /* 521 * Events that were queues because no one was there to receive 522 * them. 523 */ 524 spinlock_t events_lock; /* For dealing with event stuff. */ 525 struct list_head waiting_events; 526 unsigned int waiting_events_count; /* How many events in queue? */ 527 char delivering_events; 528 char event_msg_printed; 529 atomic_t event_waiters; 530 unsigned int ticks_to_req_ev; 531 int last_needs_timer; 532 533 /* 534 * The event receiver for my BMC, only really used at panic 535 * shutdown as a place to store this. 536 */ 537 unsigned char event_receiver; 538 unsigned char event_receiver_lun; 539 unsigned char local_sel_device; 540 unsigned char local_event_generator; 541 542 /* For handling of maintenance mode. */ 543 int maintenance_mode; 544 bool maintenance_mode_enable; 545 int auto_maintenance_timeout; 546 spinlock_t maintenance_mode_lock; /* Used in a timer... */ 547 548 /* 549 * If we are doing maintenance on something on IPMB, extend 550 * the timeout time to avoid timeouts writing firmware and 551 * such. 552 */ 553 int ipmb_maintenance_mode_timeout; 554 555 /* 556 * A cheap hack, if this is non-null and a message to an 557 * interface comes in with a NULL user, call this routine with 558 * it. Note that the message will still be freed by the 559 * caller. This only works on the system interface. 560 * 561 * Protected by bmc_reg_mutex. 562 */ 563 void (*null_user_handler)(struct ipmi_smi *intf, 564 struct ipmi_recv_msg *msg); 565 566 /* 567 * When we are scanning the channels for an SMI, this will 568 * tell which channel we are scanning. 569 */ 570 int curr_channel; 571 572 /* Channel information */ 573 struct ipmi_channel_set *channel_list; 574 unsigned int curr_working_cset; /* First index into the following. */ 575 struct ipmi_channel_set wchannels[2]; 576 struct ipmi_my_addrinfo addrinfo[IPMI_MAX_CHANNELS]; 577 bool channels_ready; 578 579 atomic_t stats[IPMI_NUM_STATS]; 580 581 /* 582 * run_to_completion duplicate of smb_info, smi_info 583 * and ipmi_serial_info structures. Used to decrease numbers of 584 * parameters passed by "low" level IPMI code. 585 */ 586 int run_to_completion; 587 }; 588 #define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev) 589 590 static void __get_guid(struct ipmi_smi *intf); 591 static void __ipmi_bmc_unregister(struct ipmi_smi *intf); 592 static int __ipmi_bmc_register(struct ipmi_smi *intf, 593 struct ipmi_device_id *id, 594 bool guid_set, guid_t *guid, int intf_num); 595 static int __scan_channels(struct ipmi_smi *intf, struct ipmi_device_id *id); 596 597 598 /** 599 * The driver model view of the IPMI messaging driver. 600 */ 601 static struct platform_driver ipmidriver = { 602 .driver = { 603 .name = "ipmi", 604 .bus = &platform_bus_type 605 } 606 }; 607 /* 608 * This mutex keeps us from adding the same BMC twice. 609 */ 610 static DEFINE_MUTEX(ipmidriver_mutex); 611 612 static LIST_HEAD(ipmi_interfaces); 613 static DEFINE_MUTEX(ipmi_interfaces_mutex); 614 DEFINE_STATIC_SRCU(ipmi_interfaces_srcu); 615 616 /* 617 * List of watchers that want to know when smi's are added and deleted. 618 */ 619 static LIST_HEAD(smi_watchers); 620 static DEFINE_MUTEX(smi_watchers_mutex); 621 622 #define ipmi_inc_stat(intf, stat) \ 623 atomic_inc(&(intf)->stats[IPMI_STAT_ ## stat]) 624 #define ipmi_get_stat(intf, stat) \ 625 ((unsigned int) atomic_read(&(intf)->stats[IPMI_STAT_ ## stat])) 626 627 static const char * const addr_src_to_str[] = { 628 "invalid", "hotmod", "hardcoded", "SPMI", "ACPI", "SMBIOS", "PCI", 629 "device-tree", "platform" 630 }; 631 632 const char *ipmi_addr_src_to_str(enum ipmi_addr_src src) 633 { 634 if (src >= SI_LAST) 635 src = 0; /* Invalid */ 636 return addr_src_to_str[src]; 637 } 638 EXPORT_SYMBOL(ipmi_addr_src_to_str); 639 640 static int is_lan_addr(struct ipmi_addr *addr) 641 { 642 return addr->addr_type == IPMI_LAN_ADDR_TYPE; 643 } 644 645 static int is_ipmb_addr(struct ipmi_addr *addr) 646 { 647 return addr->addr_type == IPMI_IPMB_ADDR_TYPE; 648 } 649 650 static int is_ipmb_bcast_addr(struct ipmi_addr *addr) 651 { 652 return addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE; 653 } 654 655 static void free_recv_msg_list(struct list_head *q) 656 { 657 struct ipmi_recv_msg *msg, *msg2; 658 659 list_for_each_entry_safe(msg, msg2, q, link) { 660 list_del(&msg->link); 661 ipmi_free_recv_msg(msg); 662 } 663 } 664 665 static void free_smi_msg_list(struct list_head *q) 666 { 667 struct ipmi_smi_msg *msg, *msg2; 668 669 list_for_each_entry_safe(msg, msg2, q, link) { 670 list_del(&msg->link); 671 ipmi_free_smi_msg(msg); 672 } 673 } 674 675 static void clean_up_interface_data(struct ipmi_smi *intf) 676 { 677 int i; 678 struct cmd_rcvr *rcvr, *rcvr2; 679 struct list_head list; 680 681 tasklet_kill(&intf->recv_tasklet); 682 683 free_smi_msg_list(&intf->waiting_rcv_msgs); 684 free_recv_msg_list(&intf->waiting_events); 685 686 /* 687 * Wholesale remove all the entries from the list in the 688 * interface and wait for RCU to know that none are in use. 689 */ 690 mutex_lock(&intf->cmd_rcvrs_mutex); 691 INIT_LIST_HEAD(&list); 692 list_splice_init_rcu(&intf->cmd_rcvrs, &list, synchronize_rcu); 693 mutex_unlock(&intf->cmd_rcvrs_mutex); 694 695 list_for_each_entry_safe(rcvr, rcvr2, &list, link) 696 kfree(rcvr); 697 698 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) { 699 if ((intf->seq_table[i].inuse) 700 && (intf->seq_table[i].recv_msg)) 701 ipmi_free_recv_msg(intf->seq_table[i].recv_msg); 702 } 703 } 704 705 static void intf_free(struct kref *ref) 706 { 707 struct ipmi_smi *intf = container_of(ref, struct ipmi_smi, refcount); 708 709 clean_up_interface_data(intf); 710 kfree(intf); 711 } 712 713 struct watcher_entry { 714 int intf_num; 715 struct ipmi_smi *intf; 716 struct list_head link; 717 }; 718 719 int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher) 720 { 721 struct ipmi_smi *intf; 722 int index; 723 724 mutex_lock(&smi_watchers_mutex); 725 726 list_add(&watcher->link, &smi_watchers); 727 728 index = srcu_read_lock(&ipmi_interfaces_srcu); 729 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) { 730 int intf_num = READ_ONCE(intf->intf_num); 731 732 if (intf_num == -1) 733 continue; 734 watcher->new_smi(intf_num, intf->si_dev); 735 } 736 srcu_read_unlock(&ipmi_interfaces_srcu, index); 737 738 mutex_unlock(&smi_watchers_mutex); 739 740 return 0; 741 } 742 EXPORT_SYMBOL(ipmi_smi_watcher_register); 743 744 int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher) 745 { 746 mutex_lock(&smi_watchers_mutex); 747 list_del(&watcher->link); 748 mutex_unlock(&smi_watchers_mutex); 749 return 0; 750 } 751 EXPORT_SYMBOL(ipmi_smi_watcher_unregister); 752 753 /* 754 * Must be called with smi_watchers_mutex held. 755 */ 756 static void 757 call_smi_watchers(int i, struct device *dev) 758 { 759 struct ipmi_smi_watcher *w; 760 761 mutex_lock(&smi_watchers_mutex); 762 list_for_each_entry(w, &smi_watchers, link) { 763 if (try_module_get(w->owner)) { 764 w->new_smi(i, dev); 765 module_put(w->owner); 766 } 767 } 768 mutex_unlock(&smi_watchers_mutex); 769 } 770 771 static int 772 ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2) 773 { 774 if (addr1->addr_type != addr2->addr_type) 775 return 0; 776 777 if (addr1->channel != addr2->channel) 778 return 0; 779 780 if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) { 781 struct ipmi_system_interface_addr *smi_addr1 782 = (struct ipmi_system_interface_addr *) addr1; 783 struct ipmi_system_interface_addr *smi_addr2 784 = (struct ipmi_system_interface_addr *) addr2; 785 return (smi_addr1->lun == smi_addr2->lun); 786 } 787 788 if (is_ipmb_addr(addr1) || is_ipmb_bcast_addr(addr1)) { 789 struct ipmi_ipmb_addr *ipmb_addr1 790 = (struct ipmi_ipmb_addr *) addr1; 791 struct ipmi_ipmb_addr *ipmb_addr2 792 = (struct ipmi_ipmb_addr *) addr2; 793 794 return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr) 795 && (ipmb_addr1->lun == ipmb_addr2->lun)); 796 } 797 798 if (is_lan_addr(addr1)) { 799 struct ipmi_lan_addr *lan_addr1 800 = (struct ipmi_lan_addr *) addr1; 801 struct ipmi_lan_addr *lan_addr2 802 = (struct ipmi_lan_addr *) addr2; 803 804 return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID) 805 && (lan_addr1->local_SWID == lan_addr2->local_SWID) 806 && (lan_addr1->session_handle 807 == lan_addr2->session_handle) 808 && (lan_addr1->lun == lan_addr2->lun)); 809 } 810 811 return 1; 812 } 813 814 int ipmi_validate_addr(struct ipmi_addr *addr, int len) 815 { 816 if (len < sizeof(struct ipmi_system_interface_addr)) 817 return -EINVAL; 818 819 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) { 820 if (addr->channel != IPMI_BMC_CHANNEL) 821 return -EINVAL; 822 return 0; 823 } 824 825 if ((addr->channel == IPMI_BMC_CHANNEL) 826 || (addr->channel >= IPMI_MAX_CHANNELS) 827 || (addr->channel < 0)) 828 return -EINVAL; 829 830 if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) { 831 if (len < sizeof(struct ipmi_ipmb_addr)) 832 return -EINVAL; 833 return 0; 834 } 835 836 if (is_lan_addr(addr)) { 837 if (len < sizeof(struct ipmi_lan_addr)) 838 return -EINVAL; 839 return 0; 840 } 841 842 return -EINVAL; 843 } 844 EXPORT_SYMBOL(ipmi_validate_addr); 845 846 unsigned int ipmi_addr_length(int addr_type) 847 { 848 if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) 849 return sizeof(struct ipmi_system_interface_addr); 850 851 if ((addr_type == IPMI_IPMB_ADDR_TYPE) 852 || (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE)) 853 return sizeof(struct ipmi_ipmb_addr); 854 855 if (addr_type == IPMI_LAN_ADDR_TYPE) 856 return sizeof(struct ipmi_lan_addr); 857 858 return 0; 859 } 860 EXPORT_SYMBOL(ipmi_addr_length); 861 862 static int deliver_response(struct ipmi_smi *intf, struct ipmi_recv_msg *msg) 863 { 864 int rv = 0; 865 866 if (!msg->user) { 867 /* Special handling for NULL users. */ 868 if (intf->null_user_handler) { 869 intf->null_user_handler(intf, msg); 870 } else { 871 /* No handler, so give up. */ 872 rv = -EINVAL; 873 } 874 ipmi_free_recv_msg(msg); 875 } else if (!oops_in_progress) { 876 /* 877 * If we are running in the panic context, calling the 878 * receive handler doesn't much meaning and has a deadlock 879 * risk. At this moment, simply skip it in that case. 880 */ 881 int index; 882 struct ipmi_user *user = acquire_ipmi_user(msg->user, &index); 883 884 if (user) { 885 user->handler->ipmi_recv_hndl(msg, user->handler_data); 886 release_ipmi_user(msg->user, index); 887 } else { 888 /* User went away, give up. */ 889 ipmi_free_recv_msg(msg); 890 rv = -EINVAL; 891 } 892 } 893 894 return rv; 895 } 896 897 static void deliver_local_response(struct ipmi_smi *intf, 898 struct ipmi_recv_msg *msg) 899 { 900 if (deliver_response(intf, msg)) 901 ipmi_inc_stat(intf, unhandled_local_responses); 902 else 903 ipmi_inc_stat(intf, handled_local_responses); 904 } 905 906 static void deliver_err_response(struct ipmi_smi *intf, 907 struct ipmi_recv_msg *msg, int err) 908 { 909 msg->recv_type = IPMI_RESPONSE_RECV_TYPE; 910 msg->msg_data[0] = err; 911 msg->msg.netfn |= 1; /* Convert to a response. */ 912 msg->msg.data_len = 1; 913 msg->msg.data = msg->msg_data; 914 deliver_local_response(intf, msg); 915 } 916 917 /* 918 * Find the next sequence number not being used and add the given 919 * message with the given timeout to the sequence table. This must be 920 * called with the interface's seq_lock held. 921 */ 922 static int intf_next_seq(struct ipmi_smi *intf, 923 struct ipmi_recv_msg *recv_msg, 924 unsigned long timeout, 925 int retries, 926 int broadcast, 927 unsigned char *seq, 928 long *seqid) 929 { 930 int rv = 0; 931 unsigned int i; 932 933 if (timeout == 0) 934 timeout = default_retry_ms; 935 if (retries < 0) 936 retries = default_max_retries; 937 938 for (i = intf->curr_seq; (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq; 939 i = (i+1)%IPMI_IPMB_NUM_SEQ) { 940 if (!intf->seq_table[i].inuse) 941 break; 942 } 943 944 if (!intf->seq_table[i].inuse) { 945 intf->seq_table[i].recv_msg = recv_msg; 946 947 /* 948 * Start with the maximum timeout, when the send response 949 * comes in we will start the real timer. 950 */ 951 intf->seq_table[i].timeout = MAX_MSG_TIMEOUT; 952 intf->seq_table[i].orig_timeout = timeout; 953 intf->seq_table[i].retries_left = retries; 954 intf->seq_table[i].broadcast = broadcast; 955 intf->seq_table[i].inuse = 1; 956 intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid); 957 *seq = i; 958 *seqid = intf->seq_table[i].seqid; 959 intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ; 960 need_waiter(intf); 961 } else { 962 rv = -EAGAIN; 963 } 964 965 return rv; 966 } 967 968 /* 969 * Return the receive message for the given sequence number and 970 * release the sequence number so it can be reused. Some other data 971 * is passed in to be sure the message matches up correctly (to help 972 * guard against message coming in after their timeout and the 973 * sequence number being reused). 974 */ 975 static int intf_find_seq(struct ipmi_smi *intf, 976 unsigned char seq, 977 short channel, 978 unsigned char cmd, 979 unsigned char netfn, 980 struct ipmi_addr *addr, 981 struct ipmi_recv_msg **recv_msg) 982 { 983 int rv = -ENODEV; 984 unsigned long flags; 985 986 if (seq >= IPMI_IPMB_NUM_SEQ) 987 return -EINVAL; 988 989 spin_lock_irqsave(&intf->seq_lock, flags); 990 if (intf->seq_table[seq].inuse) { 991 struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg; 992 993 if ((msg->addr.channel == channel) && (msg->msg.cmd == cmd) 994 && (msg->msg.netfn == netfn) 995 && (ipmi_addr_equal(addr, &msg->addr))) { 996 *recv_msg = msg; 997 intf->seq_table[seq].inuse = 0; 998 rv = 0; 999 } 1000 } 1001 spin_unlock_irqrestore(&intf->seq_lock, flags); 1002 1003 return rv; 1004 } 1005 1006 1007 /* Start the timer for a specific sequence table entry. */ 1008 static int intf_start_seq_timer(struct ipmi_smi *intf, 1009 long msgid) 1010 { 1011 int rv = -ENODEV; 1012 unsigned long flags; 1013 unsigned char seq; 1014 unsigned long seqid; 1015 1016 1017 GET_SEQ_FROM_MSGID(msgid, seq, seqid); 1018 1019 spin_lock_irqsave(&intf->seq_lock, flags); 1020 /* 1021 * We do this verification because the user can be deleted 1022 * while a message is outstanding. 1023 */ 1024 if ((intf->seq_table[seq].inuse) 1025 && (intf->seq_table[seq].seqid == seqid)) { 1026 struct seq_table *ent = &intf->seq_table[seq]; 1027 ent->timeout = ent->orig_timeout; 1028 rv = 0; 1029 } 1030 spin_unlock_irqrestore(&intf->seq_lock, flags); 1031 1032 return rv; 1033 } 1034 1035 /* Got an error for the send message for a specific sequence number. */ 1036 static int intf_err_seq(struct ipmi_smi *intf, 1037 long msgid, 1038 unsigned int err) 1039 { 1040 int rv = -ENODEV; 1041 unsigned long flags; 1042 unsigned char seq; 1043 unsigned long seqid; 1044 struct ipmi_recv_msg *msg = NULL; 1045 1046 1047 GET_SEQ_FROM_MSGID(msgid, seq, seqid); 1048 1049 spin_lock_irqsave(&intf->seq_lock, flags); 1050 /* 1051 * We do this verification because the user can be deleted 1052 * while a message is outstanding. 1053 */ 1054 if ((intf->seq_table[seq].inuse) 1055 && (intf->seq_table[seq].seqid == seqid)) { 1056 struct seq_table *ent = &intf->seq_table[seq]; 1057 1058 ent->inuse = 0; 1059 msg = ent->recv_msg; 1060 rv = 0; 1061 } 1062 spin_unlock_irqrestore(&intf->seq_lock, flags); 1063 1064 if (msg) 1065 deliver_err_response(intf, msg, err); 1066 1067 return rv; 1068 } 1069 1070 1071 int ipmi_create_user(unsigned int if_num, 1072 const struct ipmi_user_hndl *handler, 1073 void *handler_data, 1074 struct ipmi_user **user) 1075 { 1076 unsigned long flags; 1077 struct ipmi_user *new_user; 1078 int rv = 0, index; 1079 struct ipmi_smi *intf; 1080 1081 /* 1082 * There is no module usecount here, because it's not 1083 * required. Since this can only be used by and called from 1084 * other modules, they will implicitly use this module, and 1085 * thus this can't be removed unless the other modules are 1086 * removed. 1087 */ 1088 1089 if (handler == NULL) 1090 return -EINVAL; 1091 1092 /* 1093 * Make sure the driver is actually initialized, this handles 1094 * problems with initialization order. 1095 */ 1096 if (!initialized) { 1097 rv = ipmi_init_msghandler(); 1098 if (rv) 1099 return rv; 1100 1101 /* 1102 * The init code doesn't return an error if it was turned 1103 * off, but it won't initialize. Check that. 1104 */ 1105 if (!initialized) 1106 return -ENODEV; 1107 } 1108 1109 new_user = kmalloc(sizeof(*new_user), GFP_KERNEL); 1110 if (!new_user) 1111 return -ENOMEM; 1112 1113 index = srcu_read_lock(&ipmi_interfaces_srcu); 1114 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) { 1115 if (intf->intf_num == if_num) 1116 goto found; 1117 } 1118 /* Not found, return an error */ 1119 rv = -EINVAL; 1120 goto out_kfree; 1121 1122 found: 1123 rv = init_srcu_struct(&new_user->release_barrier); 1124 if (rv) 1125 goto out_kfree; 1126 1127 /* Note that each existing user holds a refcount to the interface. */ 1128 kref_get(&intf->refcount); 1129 1130 kref_init(&new_user->refcount); 1131 new_user->handler = handler; 1132 new_user->handler_data = handler_data; 1133 new_user->intf = intf; 1134 new_user->gets_events = false; 1135 1136 rcu_assign_pointer(new_user->self, new_user); 1137 spin_lock_irqsave(&intf->seq_lock, flags); 1138 list_add_rcu(&new_user->link, &intf->users); 1139 spin_unlock_irqrestore(&intf->seq_lock, flags); 1140 if (handler->ipmi_watchdog_pretimeout) { 1141 /* User wants pretimeouts, so make sure to watch for them. */ 1142 if (atomic_inc_return(&intf->event_waiters) == 1) 1143 need_waiter(intf); 1144 } 1145 srcu_read_unlock(&ipmi_interfaces_srcu, index); 1146 *user = new_user; 1147 return 0; 1148 1149 out_kfree: 1150 srcu_read_unlock(&ipmi_interfaces_srcu, index); 1151 kfree(new_user); 1152 return rv; 1153 } 1154 EXPORT_SYMBOL(ipmi_create_user); 1155 1156 int ipmi_get_smi_info(int if_num, struct ipmi_smi_info *data) 1157 { 1158 int rv, index; 1159 struct ipmi_smi *intf; 1160 1161 index = srcu_read_lock(&ipmi_interfaces_srcu); 1162 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) { 1163 if (intf->intf_num == if_num) 1164 goto found; 1165 } 1166 srcu_read_unlock(&ipmi_interfaces_srcu, index); 1167 1168 /* Not found, return an error */ 1169 return -EINVAL; 1170 1171 found: 1172 if (!intf->handlers->get_smi_info) 1173 rv = -ENOTTY; 1174 else 1175 rv = intf->handlers->get_smi_info(intf->send_info, data); 1176 srcu_read_unlock(&ipmi_interfaces_srcu, index); 1177 1178 return rv; 1179 } 1180 EXPORT_SYMBOL(ipmi_get_smi_info); 1181 1182 static void free_user(struct kref *ref) 1183 { 1184 struct ipmi_user *user = container_of(ref, struct ipmi_user, refcount); 1185 kfree(user); 1186 } 1187 1188 static void _ipmi_destroy_user(struct ipmi_user *user) 1189 { 1190 struct ipmi_smi *intf = user->intf; 1191 int i; 1192 unsigned long flags; 1193 struct cmd_rcvr *rcvr; 1194 struct cmd_rcvr *rcvrs = NULL; 1195 1196 if (!acquire_ipmi_user(user, &i)) { 1197 /* 1198 * The user has already been cleaned up, just make sure 1199 * nothing is using it and return. 1200 */ 1201 synchronize_srcu(&user->release_barrier); 1202 return; 1203 } 1204 1205 rcu_assign_pointer(user->self, NULL); 1206 release_ipmi_user(user, i); 1207 1208 synchronize_srcu(&user->release_barrier); 1209 1210 if (user->handler->shutdown) 1211 user->handler->shutdown(user->handler_data); 1212 1213 if (user->handler->ipmi_watchdog_pretimeout) 1214 atomic_dec(&intf->event_waiters); 1215 1216 if (user->gets_events) 1217 atomic_dec(&intf->event_waiters); 1218 1219 /* Remove the user from the interface's sequence table. */ 1220 spin_lock_irqsave(&intf->seq_lock, flags); 1221 list_del_rcu(&user->link); 1222 1223 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) { 1224 if (intf->seq_table[i].inuse 1225 && (intf->seq_table[i].recv_msg->user == user)) { 1226 intf->seq_table[i].inuse = 0; 1227 ipmi_free_recv_msg(intf->seq_table[i].recv_msg); 1228 } 1229 } 1230 spin_unlock_irqrestore(&intf->seq_lock, flags); 1231 1232 /* 1233 * Remove the user from the command receiver's table. First 1234 * we build a list of everything (not using the standard link, 1235 * since other things may be using it till we do 1236 * synchronize_srcu()) then free everything in that list. 1237 */ 1238 mutex_lock(&intf->cmd_rcvrs_mutex); 1239 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) { 1240 if (rcvr->user == user) { 1241 list_del_rcu(&rcvr->link); 1242 rcvr->next = rcvrs; 1243 rcvrs = rcvr; 1244 } 1245 } 1246 mutex_unlock(&intf->cmd_rcvrs_mutex); 1247 synchronize_rcu(); 1248 while (rcvrs) { 1249 rcvr = rcvrs; 1250 rcvrs = rcvr->next; 1251 kfree(rcvr); 1252 } 1253 1254 kref_put(&intf->refcount, intf_free); 1255 } 1256 1257 int ipmi_destroy_user(struct ipmi_user *user) 1258 { 1259 _ipmi_destroy_user(user); 1260 1261 cleanup_srcu_struct(&user->release_barrier); 1262 kref_put(&user->refcount, free_user); 1263 1264 return 0; 1265 } 1266 EXPORT_SYMBOL(ipmi_destroy_user); 1267 1268 int ipmi_get_version(struct ipmi_user *user, 1269 unsigned char *major, 1270 unsigned char *minor) 1271 { 1272 struct ipmi_device_id id; 1273 int rv, index; 1274 1275 user = acquire_ipmi_user(user, &index); 1276 if (!user) 1277 return -ENODEV; 1278 1279 rv = bmc_get_device_id(user->intf, NULL, &id, NULL, NULL); 1280 if (!rv) { 1281 *major = ipmi_version_major(&id); 1282 *minor = ipmi_version_minor(&id); 1283 } 1284 release_ipmi_user(user, index); 1285 1286 return rv; 1287 } 1288 EXPORT_SYMBOL(ipmi_get_version); 1289 1290 int ipmi_set_my_address(struct ipmi_user *user, 1291 unsigned int channel, 1292 unsigned char address) 1293 { 1294 int index, rv = 0; 1295 1296 user = acquire_ipmi_user(user, &index); 1297 if (!user) 1298 return -ENODEV; 1299 1300 if (channel >= IPMI_MAX_CHANNELS) 1301 rv = -EINVAL; 1302 else 1303 user->intf->addrinfo[channel].address = address; 1304 release_ipmi_user(user, index); 1305 1306 return rv; 1307 } 1308 EXPORT_SYMBOL(ipmi_set_my_address); 1309 1310 int ipmi_get_my_address(struct ipmi_user *user, 1311 unsigned int channel, 1312 unsigned char *address) 1313 { 1314 int index, rv = 0; 1315 1316 user = acquire_ipmi_user(user, &index); 1317 if (!user) 1318 return -ENODEV; 1319 1320 if (channel >= IPMI_MAX_CHANNELS) 1321 rv = -EINVAL; 1322 else 1323 *address = user->intf->addrinfo[channel].address; 1324 release_ipmi_user(user, index); 1325 1326 return rv; 1327 } 1328 EXPORT_SYMBOL(ipmi_get_my_address); 1329 1330 int ipmi_set_my_LUN(struct ipmi_user *user, 1331 unsigned int channel, 1332 unsigned char LUN) 1333 { 1334 int index, rv = 0; 1335 1336 user = acquire_ipmi_user(user, &index); 1337 if (!user) 1338 return -ENODEV; 1339 1340 if (channel >= IPMI_MAX_CHANNELS) 1341 rv = -EINVAL; 1342 else 1343 user->intf->addrinfo[channel].lun = LUN & 0x3; 1344 release_ipmi_user(user, index); 1345 1346 return 0; 1347 } 1348 EXPORT_SYMBOL(ipmi_set_my_LUN); 1349 1350 int ipmi_get_my_LUN(struct ipmi_user *user, 1351 unsigned int channel, 1352 unsigned char *address) 1353 { 1354 int index, rv = 0; 1355 1356 user = acquire_ipmi_user(user, &index); 1357 if (!user) 1358 return -ENODEV; 1359 1360 if (channel >= IPMI_MAX_CHANNELS) 1361 rv = -EINVAL; 1362 else 1363 *address = user->intf->addrinfo[channel].lun; 1364 release_ipmi_user(user, index); 1365 1366 return rv; 1367 } 1368 EXPORT_SYMBOL(ipmi_get_my_LUN); 1369 1370 int ipmi_get_maintenance_mode(struct ipmi_user *user) 1371 { 1372 int mode, index; 1373 unsigned long flags; 1374 1375 user = acquire_ipmi_user(user, &index); 1376 if (!user) 1377 return -ENODEV; 1378 1379 spin_lock_irqsave(&user->intf->maintenance_mode_lock, flags); 1380 mode = user->intf->maintenance_mode; 1381 spin_unlock_irqrestore(&user->intf->maintenance_mode_lock, flags); 1382 release_ipmi_user(user, index); 1383 1384 return mode; 1385 } 1386 EXPORT_SYMBOL(ipmi_get_maintenance_mode); 1387 1388 static void maintenance_mode_update(struct ipmi_smi *intf) 1389 { 1390 if (intf->handlers->set_maintenance_mode) 1391 intf->handlers->set_maintenance_mode( 1392 intf->send_info, intf->maintenance_mode_enable); 1393 } 1394 1395 int ipmi_set_maintenance_mode(struct ipmi_user *user, int mode) 1396 { 1397 int rv = 0, index; 1398 unsigned long flags; 1399 struct ipmi_smi *intf = user->intf; 1400 1401 user = acquire_ipmi_user(user, &index); 1402 if (!user) 1403 return -ENODEV; 1404 1405 spin_lock_irqsave(&intf->maintenance_mode_lock, flags); 1406 if (intf->maintenance_mode != mode) { 1407 switch (mode) { 1408 case IPMI_MAINTENANCE_MODE_AUTO: 1409 intf->maintenance_mode_enable 1410 = (intf->auto_maintenance_timeout > 0); 1411 break; 1412 1413 case IPMI_MAINTENANCE_MODE_OFF: 1414 intf->maintenance_mode_enable = false; 1415 break; 1416 1417 case IPMI_MAINTENANCE_MODE_ON: 1418 intf->maintenance_mode_enable = true; 1419 break; 1420 1421 default: 1422 rv = -EINVAL; 1423 goto out_unlock; 1424 } 1425 intf->maintenance_mode = mode; 1426 1427 maintenance_mode_update(intf); 1428 } 1429 out_unlock: 1430 spin_unlock_irqrestore(&intf->maintenance_mode_lock, flags); 1431 release_ipmi_user(user, index); 1432 1433 return rv; 1434 } 1435 EXPORT_SYMBOL(ipmi_set_maintenance_mode); 1436 1437 int ipmi_set_gets_events(struct ipmi_user *user, bool val) 1438 { 1439 unsigned long flags; 1440 struct ipmi_smi *intf = user->intf; 1441 struct ipmi_recv_msg *msg, *msg2; 1442 struct list_head msgs; 1443 int index; 1444 1445 user = acquire_ipmi_user(user, &index); 1446 if (!user) 1447 return -ENODEV; 1448 1449 INIT_LIST_HEAD(&msgs); 1450 1451 spin_lock_irqsave(&intf->events_lock, flags); 1452 if (user->gets_events == val) 1453 goto out; 1454 1455 user->gets_events = val; 1456 1457 if (val) { 1458 if (atomic_inc_return(&intf->event_waiters) == 1) 1459 need_waiter(intf); 1460 } else { 1461 atomic_dec(&intf->event_waiters); 1462 } 1463 1464 if (intf->delivering_events) 1465 /* 1466 * Another thread is delivering events for this, so 1467 * let it handle any new events. 1468 */ 1469 goto out; 1470 1471 /* Deliver any queued events. */ 1472 while (user->gets_events && !list_empty(&intf->waiting_events)) { 1473 list_for_each_entry_safe(msg, msg2, &intf->waiting_events, link) 1474 list_move_tail(&msg->link, &msgs); 1475 intf->waiting_events_count = 0; 1476 if (intf->event_msg_printed) { 1477 dev_warn(intf->si_dev, 1478 PFX "Event queue no longer full\n"); 1479 intf->event_msg_printed = 0; 1480 } 1481 1482 intf->delivering_events = 1; 1483 spin_unlock_irqrestore(&intf->events_lock, flags); 1484 1485 list_for_each_entry_safe(msg, msg2, &msgs, link) { 1486 msg->user = user; 1487 kref_get(&user->refcount); 1488 deliver_local_response(intf, msg); 1489 } 1490 1491 spin_lock_irqsave(&intf->events_lock, flags); 1492 intf->delivering_events = 0; 1493 } 1494 1495 out: 1496 spin_unlock_irqrestore(&intf->events_lock, flags); 1497 release_ipmi_user(user, index); 1498 1499 return 0; 1500 } 1501 EXPORT_SYMBOL(ipmi_set_gets_events); 1502 1503 static struct cmd_rcvr *find_cmd_rcvr(struct ipmi_smi *intf, 1504 unsigned char netfn, 1505 unsigned char cmd, 1506 unsigned char chan) 1507 { 1508 struct cmd_rcvr *rcvr; 1509 1510 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) { 1511 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd) 1512 && (rcvr->chans & (1 << chan))) 1513 return rcvr; 1514 } 1515 return NULL; 1516 } 1517 1518 static int is_cmd_rcvr_exclusive(struct ipmi_smi *intf, 1519 unsigned char netfn, 1520 unsigned char cmd, 1521 unsigned int chans) 1522 { 1523 struct cmd_rcvr *rcvr; 1524 1525 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) { 1526 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd) 1527 && (rcvr->chans & chans)) 1528 return 0; 1529 } 1530 return 1; 1531 } 1532 1533 int ipmi_register_for_cmd(struct ipmi_user *user, 1534 unsigned char netfn, 1535 unsigned char cmd, 1536 unsigned int chans) 1537 { 1538 struct ipmi_smi *intf = user->intf; 1539 struct cmd_rcvr *rcvr; 1540 int rv = 0, index; 1541 1542 user = acquire_ipmi_user(user, &index); 1543 if (!user) 1544 return -ENODEV; 1545 1546 rcvr = kmalloc(sizeof(*rcvr), GFP_KERNEL); 1547 if (!rcvr) { 1548 rv = -ENOMEM; 1549 goto out_release; 1550 } 1551 rcvr->cmd = cmd; 1552 rcvr->netfn = netfn; 1553 rcvr->chans = chans; 1554 rcvr->user = user; 1555 1556 mutex_lock(&intf->cmd_rcvrs_mutex); 1557 /* Make sure the command/netfn is not already registered. */ 1558 if (!is_cmd_rcvr_exclusive(intf, netfn, cmd, chans)) { 1559 rv = -EBUSY; 1560 goto out_unlock; 1561 } 1562 1563 if (atomic_inc_return(&intf->event_waiters) == 1) 1564 need_waiter(intf); 1565 1566 list_add_rcu(&rcvr->link, &intf->cmd_rcvrs); 1567 1568 out_unlock: 1569 mutex_unlock(&intf->cmd_rcvrs_mutex); 1570 if (rv) 1571 kfree(rcvr); 1572 out_release: 1573 release_ipmi_user(user, index); 1574 1575 return rv; 1576 } 1577 EXPORT_SYMBOL(ipmi_register_for_cmd); 1578 1579 int ipmi_unregister_for_cmd(struct ipmi_user *user, 1580 unsigned char netfn, 1581 unsigned char cmd, 1582 unsigned int chans) 1583 { 1584 struct ipmi_smi *intf = user->intf; 1585 struct cmd_rcvr *rcvr; 1586 struct cmd_rcvr *rcvrs = NULL; 1587 int i, rv = -ENOENT, index; 1588 1589 user = acquire_ipmi_user(user, &index); 1590 if (!user) 1591 return -ENODEV; 1592 1593 mutex_lock(&intf->cmd_rcvrs_mutex); 1594 for (i = 0; i < IPMI_NUM_CHANNELS; i++) { 1595 if (((1 << i) & chans) == 0) 1596 continue; 1597 rcvr = find_cmd_rcvr(intf, netfn, cmd, i); 1598 if (rcvr == NULL) 1599 continue; 1600 if (rcvr->user == user) { 1601 rv = 0; 1602 rcvr->chans &= ~chans; 1603 if (rcvr->chans == 0) { 1604 list_del_rcu(&rcvr->link); 1605 rcvr->next = rcvrs; 1606 rcvrs = rcvr; 1607 } 1608 } 1609 } 1610 mutex_unlock(&intf->cmd_rcvrs_mutex); 1611 synchronize_rcu(); 1612 release_ipmi_user(user, index); 1613 while (rcvrs) { 1614 atomic_dec(&intf->event_waiters); 1615 rcvr = rcvrs; 1616 rcvrs = rcvr->next; 1617 kfree(rcvr); 1618 } 1619 1620 return rv; 1621 } 1622 EXPORT_SYMBOL(ipmi_unregister_for_cmd); 1623 1624 static unsigned char 1625 ipmb_checksum(unsigned char *data, int size) 1626 { 1627 unsigned char csum = 0; 1628 1629 for (; size > 0; size--, data++) 1630 csum += *data; 1631 1632 return -csum; 1633 } 1634 1635 static inline void format_ipmb_msg(struct ipmi_smi_msg *smi_msg, 1636 struct kernel_ipmi_msg *msg, 1637 struct ipmi_ipmb_addr *ipmb_addr, 1638 long msgid, 1639 unsigned char ipmb_seq, 1640 int broadcast, 1641 unsigned char source_address, 1642 unsigned char source_lun) 1643 { 1644 int i = broadcast; 1645 1646 /* Format the IPMB header data. */ 1647 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2); 1648 smi_msg->data[1] = IPMI_SEND_MSG_CMD; 1649 smi_msg->data[2] = ipmb_addr->channel; 1650 if (broadcast) 1651 smi_msg->data[3] = 0; 1652 smi_msg->data[i+3] = ipmb_addr->slave_addr; 1653 smi_msg->data[i+4] = (msg->netfn << 2) | (ipmb_addr->lun & 0x3); 1654 smi_msg->data[i+5] = ipmb_checksum(&smi_msg->data[i + 3], 2); 1655 smi_msg->data[i+6] = source_address; 1656 smi_msg->data[i+7] = (ipmb_seq << 2) | source_lun; 1657 smi_msg->data[i+8] = msg->cmd; 1658 1659 /* Now tack on the data to the message. */ 1660 if (msg->data_len > 0) 1661 memcpy(&smi_msg->data[i + 9], msg->data, msg->data_len); 1662 smi_msg->data_size = msg->data_len + 9; 1663 1664 /* Now calculate the checksum and tack it on. */ 1665 smi_msg->data[i+smi_msg->data_size] 1666 = ipmb_checksum(&smi_msg->data[i + 6], smi_msg->data_size - 6); 1667 1668 /* 1669 * Add on the checksum size and the offset from the 1670 * broadcast. 1671 */ 1672 smi_msg->data_size += 1 + i; 1673 1674 smi_msg->msgid = msgid; 1675 } 1676 1677 static inline void format_lan_msg(struct ipmi_smi_msg *smi_msg, 1678 struct kernel_ipmi_msg *msg, 1679 struct ipmi_lan_addr *lan_addr, 1680 long msgid, 1681 unsigned char ipmb_seq, 1682 unsigned char source_lun) 1683 { 1684 /* Format the IPMB header data. */ 1685 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2); 1686 smi_msg->data[1] = IPMI_SEND_MSG_CMD; 1687 smi_msg->data[2] = lan_addr->channel; 1688 smi_msg->data[3] = lan_addr->session_handle; 1689 smi_msg->data[4] = lan_addr->remote_SWID; 1690 smi_msg->data[5] = (msg->netfn << 2) | (lan_addr->lun & 0x3); 1691 smi_msg->data[6] = ipmb_checksum(&smi_msg->data[4], 2); 1692 smi_msg->data[7] = lan_addr->local_SWID; 1693 smi_msg->data[8] = (ipmb_seq << 2) | source_lun; 1694 smi_msg->data[9] = msg->cmd; 1695 1696 /* Now tack on the data to the message. */ 1697 if (msg->data_len > 0) 1698 memcpy(&smi_msg->data[10], msg->data, msg->data_len); 1699 smi_msg->data_size = msg->data_len + 10; 1700 1701 /* Now calculate the checksum and tack it on. */ 1702 smi_msg->data[smi_msg->data_size] 1703 = ipmb_checksum(&smi_msg->data[7], smi_msg->data_size - 7); 1704 1705 /* 1706 * Add on the checksum size and the offset from the 1707 * broadcast. 1708 */ 1709 smi_msg->data_size += 1; 1710 1711 smi_msg->msgid = msgid; 1712 } 1713 1714 static struct ipmi_smi_msg *smi_add_send_msg(struct ipmi_smi *intf, 1715 struct ipmi_smi_msg *smi_msg, 1716 int priority) 1717 { 1718 if (intf->curr_msg) { 1719 if (priority > 0) 1720 list_add_tail(&smi_msg->link, &intf->hp_xmit_msgs); 1721 else 1722 list_add_tail(&smi_msg->link, &intf->xmit_msgs); 1723 smi_msg = NULL; 1724 } else { 1725 intf->curr_msg = smi_msg; 1726 } 1727 1728 return smi_msg; 1729 } 1730 1731 1732 static void smi_send(struct ipmi_smi *intf, 1733 const struct ipmi_smi_handlers *handlers, 1734 struct ipmi_smi_msg *smi_msg, int priority) 1735 { 1736 int run_to_completion = intf->run_to_completion; 1737 1738 if (run_to_completion) { 1739 smi_msg = smi_add_send_msg(intf, smi_msg, priority); 1740 } else { 1741 unsigned long flags; 1742 1743 spin_lock_irqsave(&intf->xmit_msgs_lock, flags); 1744 smi_msg = smi_add_send_msg(intf, smi_msg, priority); 1745 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags); 1746 } 1747 1748 if (smi_msg) 1749 handlers->sender(intf->send_info, smi_msg); 1750 } 1751 1752 static bool is_maintenance_mode_cmd(struct kernel_ipmi_msg *msg) 1753 { 1754 return (((msg->netfn == IPMI_NETFN_APP_REQUEST) 1755 && ((msg->cmd == IPMI_COLD_RESET_CMD) 1756 || (msg->cmd == IPMI_WARM_RESET_CMD))) 1757 || (msg->netfn == IPMI_NETFN_FIRMWARE_REQUEST)); 1758 } 1759 1760 static int i_ipmi_req_sysintf(struct ipmi_smi *intf, 1761 struct ipmi_addr *addr, 1762 long msgid, 1763 struct kernel_ipmi_msg *msg, 1764 struct ipmi_smi_msg *smi_msg, 1765 struct ipmi_recv_msg *recv_msg, 1766 int retries, 1767 unsigned int retry_time_ms) 1768 { 1769 struct ipmi_system_interface_addr *smi_addr; 1770 1771 if (msg->netfn & 1) 1772 /* Responses are not allowed to the SMI. */ 1773 return -EINVAL; 1774 1775 smi_addr = (struct ipmi_system_interface_addr *) addr; 1776 if (smi_addr->lun > 3) { 1777 ipmi_inc_stat(intf, sent_invalid_commands); 1778 return -EINVAL; 1779 } 1780 1781 memcpy(&recv_msg->addr, smi_addr, sizeof(*smi_addr)); 1782 1783 if ((msg->netfn == IPMI_NETFN_APP_REQUEST) 1784 && ((msg->cmd == IPMI_SEND_MSG_CMD) 1785 || (msg->cmd == IPMI_GET_MSG_CMD) 1786 || (msg->cmd == IPMI_READ_EVENT_MSG_BUFFER_CMD))) { 1787 /* 1788 * We don't let the user do these, since we manage 1789 * the sequence numbers. 1790 */ 1791 ipmi_inc_stat(intf, sent_invalid_commands); 1792 return -EINVAL; 1793 } 1794 1795 if (is_maintenance_mode_cmd(msg)) { 1796 unsigned long flags; 1797 1798 spin_lock_irqsave(&intf->maintenance_mode_lock, flags); 1799 intf->auto_maintenance_timeout 1800 = maintenance_mode_timeout_ms; 1801 if (!intf->maintenance_mode 1802 && !intf->maintenance_mode_enable) { 1803 intf->maintenance_mode_enable = true; 1804 maintenance_mode_update(intf); 1805 } 1806 spin_unlock_irqrestore(&intf->maintenance_mode_lock, 1807 flags); 1808 } 1809 1810 if (msg->data_len + 2 > IPMI_MAX_MSG_LENGTH) { 1811 ipmi_inc_stat(intf, sent_invalid_commands); 1812 return -EMSGSIZE; 1813 } 1814 1815 smi_msg->data[0] = (msg->netfn << 2) | (smi_addr->lun & 0x3); 1816 smi_msg->data[1] = msg->cmd; 1817 smi_msg->msgid = msgid; 1818 smi_msg->user_data = recv_msg; 1819 if (msg->data_len > 0) 1820 memcpy(&smi_msg->data[2], msg->data, msg->data_len); 1821 smi_msg->data_size = msg->data_len + 2; 1822 ipmi_inc_stat(intf, sent_local_commands); 1823 1824 return 0; 1825 } 1826 1827 static int i_ipmi_req_ipmb(struct ipmi_smi *intf, 1828 struct ipmi_addr *addr, 1829 long msgid, 1830 struct kernel_ipmi_msg *msg, 1831 struct ipmi_smi_msg *smi_msg, 1832 struct ipmi_recv_msg *recv_msg, 1833 unsigned char source_address, 1834 unsigned char source_lun, 1835 int retries, 1836 unsigned int retry_time_ms) 1837 { 1838 struct ipmi_ipmb_addr *ipmb_addr; 1839 unsigned char ipmb_seq; 1840 long seqid; 1841 int broadcast = 0; 1842 struct ipmi_channel *chans; 1843 int rv = 0; 1844 1845 if (addr->channel >= IPMI_MAX_CHANNELS) { 1846 ipmi_inc_stat(intf, sent_invalid_commands); 1847 return -EINVAL; 1848 } 1849 1850 chans = READ_ONCE(intf->channel_list)->c; 1851 1852 if (chans[addr->channel].medium != IPMI_CHANNEL_MEDIUM_IPMB) { 1853 ipmi_inc_stat(intf, sent_invalid_commands); 1854 return -EINVAL; 1855 } 1856 1857 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) { 1858 /* 1859 * Broadcasts add a zero at the beginning of the 1860 * message, but otherwise is the same as an IPMB 1861 * address. 1862 */ 1863 addr->addr_type = IPMI_IPMB_ADDR_TYPE; 1864 broadcast = 1; 1865 retries = 0; /* Don't retry broadcasts. */ 1866 } 1867 1868 /* 1869 * 9 for the header and 1 for the checksum, plus 1870 * possibly one for the broadcast. 1871 */ 1872 if ((msg->data_len + 10 + broadcast) > IPMI_MAX_MSG_LENGTH) { 1873 ipmi_inc_stat(intf, sent_invalid_commands); 1874 return -EMSGSIZE; 1875 } 1876 1877 ipmb_addr = (struct ipmi_ipmb_addr *) addr; 1878 if (ipmb_addr->lun > 3) { 1879 ipmi_inc_stat(intf, sent_invalid_commands); 1880 return -EINVAL; 1881 } 1882 1883 memcpy(&recv_msg->addr, ipmb_addr, sizeof(*ipmb_addr)); 1884 1885 if (recv_msg->msg.netfn & 0x1) { 1886 /* 1887 * It's a response, so use the user's sequence 1888 * from msgid. 1889 */ 1890 ipmi_inc_stat(intf, sent_ipmb_responses); 1891 format_ipmb_msg(smi_msg, msg, ipmb_addr, msgid, 1892 msgid, broadcast, 1893 source_address, source_lun); 1894 1895 /* 1896 * Save the receive message so we can use it 1897 * to deliver the response. 1898 */ 1899 smi_msg->user_data = recv_msg; 1900 } else { 1901 /* It's a command, so get a sequence for it. */ 1902 unsigned long flags; 1903 1904 spin_lock_irqsave(&intf->seq_lock, flags); 1905 1906 if (is_maintenance_mode_cmd(msg)) 1907 intf->ipmb_maintenance_mode_timeout = 1908 maintenance_mode_timeout_ms; 1909 1910 if (intf->ipmb_maintenance_mode_timeout && retry_time_ms == 0) 1911 /* Different default in maintenance mode */ 1912 retry_time_ms = default_maintenance_retry_ms; 1913 1914 /* 1915 * Create a sequence number with a 1 second 1916 * timeout and 4 retries. 1917 */ 1918 rv = intf_next_seq(intf, 1919 recv_msg, 1920 retry_time_ms, 1921 retries, 1922 broadcast, 1923 &ipmb_seq, 1924 &seqid); 1925 if (rv) 1926 /* 1927 * We have used up all the sequence numbers, 1928 * probably, so abort. 1929 */ 1930 goto out_err; 1931 1932 ipmi_inc_stat(intf, sent_ipmb_commands); 1933 1934 /* 1935 * Store the sequence number in the message, 1936 * so that when the send message response 1937 * comes back we can start the timer. 1938 */ 1939 format_ipmb_msg(smi_msg, msg, ipmb_addr, 1940 STORE_SEQ_IN_MSGID(ipmb_seq, seqid), 1941 ipmb_seq, broadcast, 1942 source_address, source_lun); 1943 1944 /* 1945 * Copy the message into the recv message data, so we 1946 * can retransmit it later if necessary. 1947 */ 1948 memcpy(recv_msg->msg_data, smi_msg->data, 1949 smi_msg->data_size); 1950 recv_msg->msg.data = recv_msg->msg_data; 1951 recv_msg->msg.data_len = smi_msg->data_size; 1952 1953 /* 1954 * We don't unlock until here, because we need 1955 * to copy the completed message into the 1956 * recv_msg before we release the lock. 1957 * Otherwise, race conditions may bite us. I 1958 * know that's pretty paranoid, but I prefer 1959 * to be correct. 1960 */ 1961 out_err: 1962 spin_unlock_irqrestore(&intf->seq_lock, flags); 1963 } 1964 1965 return rv; 1966 } 1967 1968 static int i_ipmi_req_lan(struct ipmi_smi *intf, 1969 struct ipmi_addr *addr, 1970 long msgid, 1971 struct kernel_ipmi_msg *msg, 1972 struct ipmi_smi_msg *smi_msg, 1973 struct ipmi_recv_msg *recv_msg, 1974 unsigned char source_lun, 1975 int retries, 1976 unsigned int retry_time_ms) 1977 { 1978 struct ipmi_lan_addr *lan_addr; 1979 unsigned char ipmb_seq; 1980 long seqid; 1981 struct ipmi_channel *chans; 1982 int rv = 0; 1983 1984 if (addr->channel >= IPMI_MAX_CHANNELS) { 1985 ipmi_inc_stat(intf, sent_invalid_commands); 1986 return -EINVAL; 1987 } 1988 1989 chans = READ_ONCE(intf->channel_list)->c; 1990 1991 if ((chans[addr->channel].medium 1992 != IPMI_CHANNEL_MEDIUM_8023LAN) 1993 && (chans[addr->channel].medium 1994 != IPMI_CHANNEL_MEDIUM_ASYNC)) { 1995 ipmi_inc_stat(intf, sent_invalid_commands); 1996 return -EINVAL; 1997 } 1998 1999 /* 11 for the header and 1 for the checksum. */ 2000 if ((msg->data_len + 12) > IPMI_MAX_MSG_LENGTH) { 2001 ipmi_inc_stat(intf, sent_invalid_commands); 2002 return -EMSGSIZE; 2003 } 2004 2005 lan_addr = (struct ipmi_lan_addr *) addr; 2006 if (lan_addr->lun > 3) { 2007 ipmi_inc_stat(intf, sent_invalid_commands); 2008 return -EINVAL; 2009 } 2010 2011 memcpy(&recv_msg->addr, lan_addr, sizeof(*lan_addr)); 2012 2013 if (recv_msg->msg.netfn & 0x1) { 2014 /* 2015 * It's a response, so use the user's sequence 2016 * from msgid. 2017 */ 2018 ipmi_inc_stat(intf, sent_lan_responses); 2019 format_lan_msg(smi_msg, msg, lan_addr, msgid, 2020 msgid, source_lun); 2021 2022 /* 2023 * Save the receive message so we can use it 2024 * to deliver the response. 2025 */ 2026 smi_msg->user_data = recv_msg; 2027 } else { 2028 /* It's a command, so get a sequence for it. */ 2029 unsigned long flags; 2030 2031 spin_lock_irqsave(&intf->seq_lock, flags); 2032 2033 /* 2034 * Create a sequence number with a 1 second 2035 * timeout and 4 retries. 2036 */ 2037 rv = intf_next_seq(intf, 2038 recv_msg, 2039 retry_time_ms, 2040 retries, 2041 0, 2042 &ipmb_seq, 2043 &seqid); 2044 if (rv) 2045 /* 2046 * We have used up all the sequence numbers, 2047 * probably, so abort. 2048 */ 2049 goto out_err; 2050 2051 ipmi_inc_stat(intf, sent_lan_commands); 2052 2053 /* 2054 * Store the sequence number in the message, 2055 * so that when the send message response 2056 * comes back we can start the timer. 2057 */ 2058 format_lan_msg(smi_msg, msg, lan_addr, 2059 STORE_SEQ_IN_MSGID(ipmb_seq, seqid), 2060 ipmb_seq, source_lun); 2061 2062 /* 2063 * Copy the message into the recv message data, so we 2064 * can retransmit it later if necessary. 2065 */ 2066 memcpy(recv_msg->msg_data, smi_msg->data, 2067 smi_msg->data_size); 2068 recv_msg->msg.data = recv_msg->msg_data; 2069 recv_msg->msg.data_len = smi_msg->data_size; 2070 2071 /* 2072 * We don't unlock until here, because we need 2073 * to copy the completed message into the 2074 * recv_msg before we release the lock. 2075 * Otherwise, race conditions may bite us. I 2076 * know that's pretty paranoid, but I prefer 2077 * to be correct. 2078 */ 2079 out_err: 2080 spin_unlock_irqrestore(&intf->seq_lock, flags); 2081 } 2082 2083 return rv; 2084 } 2085 2086 /* 2087 * Separate from ipmi_request so that the user does not have to be 2088 * supplied in certain circumstances (mainly at panic time). If 2089 * messages are supplied, they will be freed, even if an error 2090 * occurs. 2091 */ 2092 static int i_ipmi_request(struct ipmi_user *user, 2093 struct ipmi_smi *intf, 2094 struct ipmi_addr *addr, 2095 long msgid, 2096 struct kernel_ipmi_msg *msg, 2097 void *user_msg_data, 2098 void *supplied_smi, 2099 struct ipmi_recv_msg *supplied_recv, 2100 int priority, 2101 unsigned char source_address, 2102 unsigned char source_lun, 2103 int retries, 2104 unsigned int retry_time_ms) 2105 { 2106 struct ipmi_smi_msg *smi_msg; 2107 struct ipmi_recv_msg *recv_msg; 2108 int rv = 0; 2109 2110 if (supplied_recv) 2111 recv_msg = supplied_recv; 2112 else { 2113 recv_msg = ipmi_alloc_recv_msg(); 2114 if (recv_msg == NULL) { 2115 rv = -ENOMEM; 2116 goto out; 2117 } 2118 } 2119 recv_msg->user_msg_data = user_msg_data; 2120 2121 if (supplied_smi) 2122 smi_msg = (struct ipmi_smi_msg *) supplied_smi; 2123 else { 2124 smi_msg = ipmi_alloc_smi_msg(); 2125 if (smi_msg == NULL) { 2126 ipmi_free_recv_msg(recv_msg); 2127 rv = -ENOMEM; 2128 goto out; 2129 } 2130 } 2131 2132 rcu_read_lock(); 2133 if (intf->in_shutdown) { 2134 rv = -ENODEV; 2135 goto out_err; 2136 } 2137 2138 recv_msg->user = user; 2139 if (user) 2140 /* The put happens when the message is freed. */ 2141 kref_get(&user->refcount); 2142 recv_msg->msgid = msgid; 2143 /* 2144 * Store the message to send in the receive message so timeout 2145 * responses can get the proper response data. 2146 */ 2147 recv_msg->msg = *msg; 2148 2149 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) { 2150 rv = i_ipmi_req_sysintf(intf, addr, msgid, msg, smi_msg, 2151 recv_msg, retries, retry_time_ms); 2152 } else if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) { 2153 rv = i_ipmi_req_ipmb(intf, addr, msgid, msg, smi_msg, recv_msg, 2154 source_address, source_lun, 2155 retries, retry_time_ms); 2156 } else if (is_lan_addr(addr)) { 2157 rv = i_ipmi_req_lan(intf, addr, msgid, msg, smi_msg, recv_msg, 2158 source_lun, retries, retry_time_ms); 2159 } else { 2160 /* Unknown address type. */ 2161 ipmi_inc_stat(intf, sent_invalid_commands); 2162 rv = -EINVAL; 2163 } 2164 2165 if (rv) { 2166 out_err: 2167 ipmi_free_smi_msg(smi_msg); 2168 ipmi_free_recv_msg(recv_msg); 2169 } else { 2170 ipmi_debug_msg("Send", smi_msg->data, smi_msg->data_size); 2171 2172 smi_send(intf, intf->handlers, smi_msg, priority); 2173 } 2174 rcu_read_unlock(); 2175 2176 out: 2177 return rv; 2178 } 2179 2180 static int check_addr(struct ipmi_smi *intf, 2181 struct ipmi_addr *addr, 2182 unsigned char *saddr, 2183 unsigned char *lun) 2184 { 2185 if (addr->channel >= IPMI_MAX_CHANNELS) 2186 return -EINVAL; 2187 *lun = intf->addrinfo[addr->channel].lun; 2188 *saddr = intf->addrinfo[addr->channel].address; 2189 return 0; 2190 } 2191 2192 int ipmi_request_settime(struct ipmi_user *user, 2193 struct ipmi_addr *addr, 2194 long msgid, 2195 struct kernel_ipmi_msg *msg, 2196 void *user_msg_data, 2197 int priority, 2198 int retries, 2199 unsigned int retry_time_ms) 2200 { 2201 unsigned char saddr = 0, lun = 0; 2202 int rv, index; 2203 2204 if (!user) 2205 return -EINVAL; 2206 2207 user = acquire_ipmi_user(user, &index); 2208 if (!user) 2209 return -ENODEV; 2210 2211 rv = check_addr(user->intf, addr, &saddr, &lun); 2212 if (!rv) 2213 rv = i_ipmi_request(user, 2214 user->intf, 2215 addr, 2216 msgid, 2217 msg, 2218 user_msg_data, 2219 NULL, NULL, 2220 priority, 2221 saddr, 2222 lun, 2223 retries, 2224 retry_time_ms); 2225 2226 release_ipmi_user(user, index); 2227 return rv; 2228 } 2229 EXPORT_SYMBOL(ipmi_request_settime); 2230 2231 int ipmi_request_supply_msgs(struct ipmi_user *user, 2232 struct ipmi_addr *addr, 2233 long msgid, 2234 struct kernel_ipmi_msg *msg, 2235 void *user_msg_data, 2236 void *supplied_smi, 2237 struct ipmi_recv_msg *supplied_recv, 2238 int priority) 2239 { 2240 unsigned char saddr = 0, lun = 0; 2241 int rv, index; 2242 2243 if (!user) 2244 return -EINVAL; 2245 2246 user = acquire_ipmi_user(user, &index); 2247 if (!user) 2248 return -ENODEV; 2249 2250 rv = check_addr(user->intf, addr, &saddr, &lun); 2251 if (!rv) 2252 rv = i_ipmi_request(user, 2253 user->intf, 2254 addr, 2255 msgid, 2256 msg, 2257 user_msg_data, 2258 supplied_smi, 2259 supplied_recv, 2260 priority, 2261 saddr, 2262 lun, 2263 -1, 0); 2264 2265 release_ipmi_user(user, index); 2266 return rv; 2267 } 2268 EXPORT_SYMBOL(ipmi_request_supply_msgs); 2269 2270 static void bmc_device_id_handler(struct ipmi_smi *intf, 2271 struct ipmi_recv_msg *msg) 2272 { 2273 int rv; 2274 2275 if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE) 2276 || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE) 2277 || (msg->msg.cmd != IPMI_GET_DEVICE_ID_CMD)) { 2278 dev_warn(intf->si_dev, 2279 PFX "invalid device_id msg: addr_type=%d netfn=%x cmd=%x\n", 2280 msg->addr.addr_type, msg->msg.netfn, msg->msg.cmd); 2281 return; 2282 } 2283 2284 rv = ipmi_demangle_device_id(msg->msg.netfn, msg->msg.cmd, 2285 msg->msg.data, msg->msg.data_len, &intf->bmc->fetch_id); 2286 if (rv) { 2287 dev_warn(intf->si_dev, 2288 PFX "device id demangle failed: %d\n", rv); 2289 intf->bmc->dyn_id_set = 0; 2290 } else { 2291 /* 2292 * Make sure the id data is available before setting 2293 * dyn_id_set. 2294 */ 2295 smp_wmb(); 2296 intf->bmc->dyn_id_set = 1; 2297 } 2298 2299 wake_up(&intf->waitq); 2300 } 2301 2302 static int 2303 send_get_device_id_cmd(struct ipmi_smi *intf) 2304 { 2305 struct ipmi_system_interface_addr si; 2306 struct kernel_ipmi_msg msg; 2307 2308 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; 2309 si.channel = IPMI_BMC_CHANNEL; 2310 si.lun = 0; 2311 2312 msg.netfn = IPMI_NETFN_APP_REQUEST; 2313 msg.cmd = IPMI_GET_DEVICE_ID_CMD; 2314 msg.data = NULL; 2315 msg.data_len = 0; 2316 2317 return i_ipmi_request(NULL, 2318 intf, 2319 (struct ipmi_addr *) &si, 2320 0, 2321 &msg, 2322 intf, 2323 NULL, 2324 NULL, 2325 0, 2326 intf->addrinfo[0].address, 2327 intf->addrinfo[0].lun, 2328 -1, 0); 2329 } 2330 2331 static int __get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc) 2332 { 2333 int rv; 2334 2335 bmc->dyn_id_set = 2; 2336 2337 intf->null_user_handler = bmc_device_id_handler; 2338 2339 rv = send_get_device_id_cmd(intf); 2340 if (rv) 2341 return rv; 2342 2343 wait_event(intf->waitq, bmc->dyn_id_set != 2); 2344 2345 if (!bmc->dyn_id_set) 2346 rv = -EIO; /* Something went wrong in the fetch. */ 2347 2348 /* dyn_id_set makes the id data available. */ 2349 smp_rmb(); 2350 2351 intf->null_user_handler = NULL; 2352 2353 return rv; 2354 } 2355 2356 /* 2357 * Fetch the device id for the bmc/interface. You must pass in either 2358 * bmc or intf, this code will get the other one. If the data has 2359 * been recently fetched, this will just use the cached data. Otherwise 2360 * it will run a new fetch. 2361 * 2362 * Except for the first time this is called (in ipmi_register_smi()), 2363 * this will always return good data; 2364 */ 2365 static int __bmc_get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc, 2366 struct ipmi_device_id *id, 2367 bool *guid_set, guid_t *guid, int intf_num) 2368 { 2369 int rv = 0; 2370 int prev_dyn_id_set, prev_guid_set; 2371 bool intf_set = intf != NULL; 2372 2373 if (!intf) { 2374 mutex_lock(&bmc->dyn_mutex); 2375 retry_bmc_lock: 2376 if (list_empty(&bmc->intfs)) { 2377 mutex_unlock(&bmc->dyn_mutex); 2378 return -ENOENT; 2379 } 2380 intf = list_first_entry(&bmc->intfs, struct ipmi_smi, 2381 bmc_link); 2382 kref_get(&intf->refcount); 2383 mutex_unlock(&bmc->dyn_mutex); 2384 mutex_lock(&intf->bmc_reg_mutex); 2385 mutex_lock(&bmc->dyn_mutex); 2386 if (intf != list_first_entry(&bmc->intfs, struct ipmi_smi, 2387 bmc_link)) { 2388 mutex_unlock(&intf->bmc_reg_mutex); 2389 kref_put(&intf->refcount, intf_free); 2390 goto retry_bmc_lock; 2391 } 2392 } else { 2393 mutex_lock(&intf->bmc_reg_mutex); 2394 bmc = intf->bmc; 2395 mutex_lock(&bmc->dyn_mutex); 2396 kref_get(&intf->refcount); 2397 } 2398 2399 /* If we have a valid and current ID, just return that. */ 2400 if (intf->in_bmc_register || 2401 (bmc->dyn_id_set && time_is_after_jiffies(bmc->dyn_id_expiry))) 2402 goto out_noprocessing; 2403 2404 prev_guid_set = bmc->dyn_guid_set; 2405 __get_guid(intf); 2406 2407 prev_dyn_id_set = bmc->dyn_id_set; 2408 rv = __get_device_id(intf, bmc); 2409 if (rv) 2410 goto out; 2411 2412 /* 2413 * The guid, device id, manufacturer id, and product id should 2414 * not change on a BMC. If it does we have to do some dancing. 2415 */ 2416 if (!intf->bmc_registered 2417 || (!prev_guid_set && bmc->dyn_guid_set) 2418 || (!prev_dyn_id_set && bmc->dyn_id_set) 2419 || (prev_guid_set && bmc->dyn_guid_set 2420 && !guid_equal(&bmc->guid, &bmc->fetch_guid)) 2421 || bmc->id.device_id != bmc->fetch_id.device_id 2422 || bmc->id.manufacturer_id != bmc->fetch_id.manufacturer_id 2423 || bmc->id.product_id != bmc->fetch_id.product_id) { 2424 struct ipmi_device_id id = bmc->fetch_id; 2425 int guid_set = bmc->dyn_guid_set; 2426 guid_t guid; 2427 2428 guid = bmc->fetch_guid; 2429 mutex_unlock(&bmc->dyn_mutex); 2430 2431 __ipmi_bmc_unregister(intf); 2432 /* Fill in the temporary BMC for good measure. */ 2433 intf->bmc->id = id; 2434 intf->bmc->dyn_guid_set = guid_set; 2435 intf->bmc->guid = guid; 2436 if (__ipmi_bmc_register(intf, &id, guid_set, &guid, intf_num)) 2437 need_waiter(intf); /* Retry later on an error. */ 2438 else 2439 __scan_channels(intf, &id); 2440 2441 2442 if (!intf_set) { 2443 /* 2444 * We weren't given the interface on the 2445 * command line, so restart the operation on 2446 * the next interface for the BMC. 2447 */ 2448 mutex_unlock(&intf->bmc_reg_mutex); 2449 mutex_lock(&bmc->dyn_mutex); 2450 goto retry_bmc_lock; 2451 } 2452 2453 /* We have a new BMC, set it up. */ 2454 bmc = intf->bmc; 2455 mutex_lock(&bmc->dyn_mutex); 2456 goto out_noprocessing; 2457 } else if (memcmp(&bmc->fetch_id, &bmc->id, sizeof(bmc->id))) 2458 /* Version info changes, scan the channels again. */ 2459 __scan_channels(intf, &bmc->fetch_id); 2460 2461 bmc->dyn_id_expiry = jiffies + IPMI_DYN_DEV_ID_EXPIRY; 2462 2463 out: 2464 if (rv && prev_dyn_id_set) { 2465 rv = 0; /* Ignore failures if we have previous data. */ 2466 bmc->dyn_id_set = prev_dyn_id_set; 2467 } 2468 if (!rv) { 2469 bmc->id = bmc->fetch_id; 2470 if (bmc->dyn_guid_set) 2471 bmc->guid = bmc->fetch_guid; 2472 else if (prev_guid_set) 2473 /* 2474 * The guid used to be valid and it failed to fetch, 2475 * just use the cached value. 2476 */ 2477 bmc->dyn_guid_set = prev_guid_set; 2478 } 2479 out_noprocessing: 2480 if (!rv) { 2481 if (id) 2482 *id = bmc->id; 2483 2484 if (guid_set) 2485 *guid_set = bmc->dyn_guid_set; 2486 2487 if (guid && bmc->dyn_guid_set) 2488 *guid = bmc->guid; 2489 } 2490 2491 mutex_unlock(&bmc->dyn_mutex); 2492 mutex_unlock(&intf->bmc_reg_mutex); 2493 2494 kref_put(&intf->refcount, intf_free); 2495 return rv; 2496 } 2497 2498 static int bmc_get_device_id(struct ipmi_smi *intf, struct bmc_device *bmc, 2499 struct ipmi_device_id *id, 2500 bool *guid_set, guid_t *guid) 2501 { 2502 return __bmc_get_device_id(intf, bmc, id, guid_set, guid, -1); 2503 } 2504 2505 static ssize_t device_id_show(struct device *dev, 2506 struct device_attribute *attr, 2507 char *buf) 2508 { 2509 struct bmc_device *bmc = to_bmc_device(dev); 2510 struct ipmi_device_id id; 2511 int rv; 2512 2513 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL); 2514 if (rv) 2515 return rv; 2516 2517 return snprintf(buf, 10, "%u\n", id.device_id); 2518 } 2519 static DEVICE_ATTR_RO(device_id); 2520 2521 static ssize_t provides_device_sdrs_show(struct device *dev, 2522 struct device_attribute *attr, 2523 char *buf) 2524 { 2525 struct bmc_device *bmc = to_bmc_device(dev); 2526 struct ipmi_device_id id; 2527 int rv; 2528 2529 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL); 2530 if (rv) 2531 return rv; 2532 2533 return snprintf(buf, 10, "%u\n", (id.device_revision & 0x80) >> 7); 2534 } 2535 static DEVICE_ATTR_RO(provides_device_sdrs); 2536 2537 static ssize_t revision_show(struct device *dev, struct device_attribute *attr, 2538 char *buf) 2539 { 2540 struct bmc_device *bmc = to_bmc_device(dev); 2541 struct ipmi_device_id id; 2542 int rv; 2543 2544 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL); 2545 if (rv) 2546 return rv; 2547 2548 return snprintf(buf, 20, "%u\n", id.device_revision & 0x0F); 2549 } 2550 static DEVICE_ATTR_RO(revision); 2551 2552 static ssize_t firmware_revision_show(struct device *dev, 2553 struct device_attribute *attr, 2554 char *buf) 2555 { 2556 struct bmc_device *bmc = to_bmc_device(dev); 2557 struct ipmi_device_id id; 2558 int rv; 2559 2560 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL); 2561 if (rv) 2562 return rv; 2563 2564 return snprintf(buf, 20, "%u.%x\n", id.firmware_revision_1, 2565 id.firmware_revision_2); 2566 } 2567 static DEVICE_ATTR_RO(firmware_revision); 2568 2569 static ssize_t ipmi_version_show(struct device *dev, 2570 struct device_attribute *attr, 2571 char *buf) 2572 { 2573 struct bmc_device *bmc = to_bmc_device(dev); 2574 struct ipmi_device_id id; 2575 int rv; 2576 2577 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL); 2578 if (rv) 2579 return rv; 2580 2581 return snprintf(buf, 20, "%u.%u\n", 2582 ipmi_version_major(&id), 2583 ipmi_version_minor(&id)); 2584 } 2585 static DEVICE_ATTR_RO(ipmi_version); 2586 2587 static ssize_t add_dev_support_show(struct device *dev, 2588 struct device_attribute *attr, 2589 char *buf) 2590 { 2591 struct bmc_device *bmc = to_bmc_device(dev); 2592 struct ipmi_device_id id; 2593 int rv; 2594 2595 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL); 2596 if (rv) 2597 return rv; 2598 2599 return snprintf(buf, 10, "0x%02x\n", id.additional_device_support); 2600 } 2601 static DEVICE_ATTR(additional_device_support, S_IRUGO, add_dev_support_show, 2602 NULL); 2603 2604 static ssize_t manufacturer_id_show(struct device *dev, 2605 struct device_attribute *attr, 2606 char *buf) 2607 { 2608 struct bmc_device *bmc = to_bmc_device(dev); 2609 struct ipmi_device_id id; 2610 int rv; 2611 2612 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL); 2613 if (rv) 2614 return rv; 2615 2616 return snprintf(buf, 20, "0x%6.6x\n", id.manufacturer_id); 2617 } 2618 static DEVICE_ATTR_RO(manufacturer_id); 2619 2620 static ssize_t product_id_show(struct device *dev, 2621 struct device_attribute *attr, 2622 char *buf) 2623 { 2624 struct bmc_device *bmc = to_bmc_device(dev); 2625 struct ipmi_device_id id; 2626 int rv; 2627 2628 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL); 2629 if (rv) 2630 return rv; 2631 2632 return snprintf(buf, 10, "0x%4.4x\n", id.product_id); 2633 } 2634 static DEVICE_ATTR_RO(product_id); 2635 2636 static ssize_t aux_firmware_rev_show(struct device *dev, 2637 struct device_attribute *attr, 2638 char *buf) 2639 { 2640 struct bmc_device *bmc = to_bmc_device(dev); 2641 struct ipmi_device_id id; 2642 int rv; 2643 2644 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL); 2645 if (rv) 2646 return rv; 2647 2648 return snprintf(buf, 21, "0x%02x 0x%02x 0x%02x 0x%02x\n", 2649 id.aux_firmware_revision[3], 2650 id.aux_firmware_revision[2], 2651 id.aux_firmware_revision[1], 2652 id.aux_firmware_revision[0]); 2653 } 2654 static DEVICE_ATTR(aux_firmware_revision, S_IRUGO, aux_firmware_rev_show, NULL); 2655 2656 static ssize_t guid_show(struct device *dev, struct device_attribute *attr, 2657 char *buf) 2658 { 2659 struct bmc_device *bmc = to_bmc_device(dev); 2660 bool guid_set; 2661 guid_t guid; 2662 int rv; 2663 2664 rv = bmc_get_device_id(NULL, bmc, NULL, &guid_set, &guid); 2665 if (rv) 2666 return rv; 2667 if (!guid_set) 2668 return -ENOENT; 2669 2670 return snprintf(buf, 38, "%pUl\n", guid.b); 2671 } 2672 static DEVICE_ATTR_RO(guid); 2673 2674 static struct attribute *bmc_dev_attrs[] = { 2675 &dev_attr_device_id.attr, 2676 &dev_attr_provides_device_sdrs.attr, 2677 &dev_attr_revision.attr, 2678 &dev_attr_firmware_revision.attr, 2679 &dev_attr_ipmi_version.attr, 2680 &dev_attr_additional_device_support.attr, 2681 &dev_attr_manufacturer_id.attr, 2682 &dev_attr_product_id.attr, 2683 &dev_attr_aux_firmware_revision.attr, 2684 &dev_attr_guid.attr, 2685 NULL 2686 }; 2687 2688 static umode_t bmc_dev_attr_is_visible(struct kobject *kobj, 2689 struct attribute *attr, int idx) 2690 { 2691 struct device *dev = kobj_to_dev(kobj); 2692 struct bmc_device *bmc = to_bmc_device(dev); 2693 umode_t mode = attr->mode; 2694 int rv; 2695 2696 if (attr == &dev_attr_aux_firmware_revision.attr) { 2697 struct ipmi_device_id id; 2698 2699 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL); 2700 return (!rv && id.aux_firmware_revision_set) ? mode : 0; 2701 } 2702 if (attr == &dev_attr_guid.attr) { 2703 bool guid_set; 2704 2705 rv = bmc_get_device_id(NULL, bmc, NULL, &guid_set, NULL); 2706 return (!rv && guid_set) ? mode : 0; 2707 } 2708 return mode; 2709 } 2710 2711 static const struct attribute_group bmc_dev_attr_group = { 2712 .attrs = bmc_dev_attrs, 2713 .is_visible = bmc_dev_attr_is_visible, 2714 }; 2715 2716 static const struct attribute_group *bmc_dev_attr_groups[] = { 2717 &bmc_dev_attr_group, 2718 NULL 2719 }; 2720 2721 static const struct device_type bmc_device_type = { 2722 .groups = bmc_dev_attr_groups, 2723 }; 2724 2725 static int __find_bmc_guid(struct device *dev, void *data) 2726 { 2727 guid_t *guid = data; 2728 struct bmc_device *bmc; 2729 int rv; 2730 2731 if (dev->type != &bmc_device_type) 2732 return 0; 2733 2734 bmc = to_bmc_device(dev); 2735 rv = bmc->dyn_guid_set && guid_equal(&bmc->guid, guid); 2736 if (rv) 2737 rv = kref_get_unless_zero(&bmc->usecount); 2738 return rv; 2739 } 2740 2741 /* 2742 * Returns with the bmc's usecount incremented, if it is non-NULL. 2743 */ 2744 static struct bmc_device *ipmi_find_bmc_guid(struct device_driver *drv, 2745 guid_t *guid) 2746 { 2747 struct device *dev; 2748 struct bmc_device *bmc = NULL; 2749 2750 dev = driver_find_device(drv, NULL, guid, __find_bmc_guid); 2751 if (dev) { 2752 bmc = to_bmc_device(dev); 2753 put_device(dev); 2754 } 2755 return bmc; 2756 } 2757 2758 struct prod_dev_id { 2759 unsigned int product_id; 2760 unsigned char device_id; 2761 }; 2762 2763 static int __find_bmc_prod_dev_id(struct device *dev, void *data) 2764 { 2765 struct prod_dev_id *cid = data; 2766 struct bmc_device *bmc; 2767 int rv; 2768 2769 if (dev->type != &bmc_device_type) 2770 return 0; 2771 2772 bmc = to_bmc_device(dev); 2773 rv = (bmc->id.product_id == cid->product_id 2774 && bmc->id.device_id == cid->device_id); 2775 if (rv) 2776 rv = kref_get_unless_zero(&bmc->usecount); 2777 return rv; 2778 } 2779 2780 /* 2781 * Returns with the bmc's usecount incremented, if it is non-NULL. 2782 */ 2783 static struct bmc_device *ipmi_find_bmc_prod_dev_id( 2784 struct device_driver *drv, 2785 unsigned int product_id, unsigned char device_id) 2786 { 2787 struct prod_dev_id id = { 2788 .product_id = product_id, 2789 .device_id = device_id, 2790 }; 2791 struct device *dev; 2792 struct bmc_device *bmc = NULL; 2793 2794 dev = driver_find_device(drv, NULL, &id, __find_bmc_prod_dev_id); 2795 if (dev) { 2796 bmc = to_bmc_device(dev); 2797 put_device(dev); 2798 } 2799 return bmc; 2800 } 2801 2802 static DEFINE_IDA(ipmi_bmc_ida); 2803 2804 static void 2805 release_bmc_device(struct device *dev) 2806 { 2807 kfree(to_bmc_device(dev)); 2808 } 2809 2810 static void cleanup_bmc_work(struct work_struct *work) 2811 { 2812 struct bmc_device *bmc = container_of(work, struct bmc_device, 2813 remove_work); 2814 int id = bmc->pdev.id; /* Unregister overwrites id */ 2815 2816 platform_device_unregister(&bmc->pdev); 2817 ida_simple_remove(&ipmi_bmc_ida, id); 2818 } 2819 2820 static void 2821 cleanup_bmc_device(struct kref *ref) 2822 { 2823 struct bmc_device *bmc = container_of(ref, struct bmc_device, usecount); 2824 2825 /* 2826 * Remove the platform device in a work queue to avoid issues 2827 * with removing the device attributes while reading a device 2828 * attribute. 2829 */ 2830 schedule_work(&bmc->remove_work); 2831 } 2832 2833 /* 2834 * Must be called with intf->bmc_reg_mutex held. 2835 */ 2836 static void __ipmi_bmc_unregister(struct ipmi_smi *intf) 2837 { 2838 struct bmc_device *bmc = intf->bmc; 2839 2840 if (!intf->bmc_registered) 2841 return; 2842 2843 sysfs_remove_link(&intf->si_dev->kobj, "bmc"); 2844 sysfs_remove_link(&bmc->pdev.dev.kobj, intf->my_dev_name); 2845 kfree(intf->my_dev_name); 2846 intf->my_dev_name = NULL; 2847 2848 mutex_lock(&bmc->dyn_mutex); 2849 list_del(&intf->bmc_link); 2850 mutex_unlock(&bmc->dyn_mutex); 2851 intf->bmc = &intf->tmp_bmc; 2852 kref_put(&bmc->usecount, cleanup_bmc_device); 2853 intf->bmc_registered = false; 2854 } 2855 2856 static void ipmi_bmc_unregister(struct ipmi_smi *intf) 2857 { 2858 mutex_lock(&intf->bmc_reg_mutex); 2859 __ipmi_bmc_unregister(intf); 2860 mutex_unlock(&intf->bmc_reg_mutex); 2861 } 2862 2863 /* 2864 * Must be called with intf->bmc_reg_mutex held. 2865 */ 2866 static int __ipmi_bmc_register(struct ipmi_smi *intf, 2867 struct ipmi_device_id *id, 2868 bool guid_set, guid_t *guid, int intf_num) 2869 { 2870 int rv; 2871 struct bmc_device *bmc; 2872 struct bmc_device *old_bmc; 2873 2874 /* 2875 * platform_device_register() can cause bmc_reg_mutex to 2876 * be claimed because of the is_visible functions of 2877 * the attributes. Eliminate possible recursion and 2878 * release the lock. 2879 */ 2880 intf->in_bmc_register = true; 2881 mutex_unlock(&intf->bmc_reg_mutex); 2882 2883 /* 2884 * Try to find if there is an bmc_device struct 2885 * representing the interfaced BMC already 2886 */ 2887 mutex_lock(&ipmidriver_mutex); 2888 if (guid_set) 2889 old_bmc = ipmi_find_bmc_guid(&ipmidriver.driver, guid); 2890 else 2891 old_bmc = ipmi_find_bmc_prod_dev_id(&ipmidriver.driver, 2892 id->product_id, 2893 id->device_id); 2894 2895 /* 2896 * If there is already an bmc_device, free the new one, 2897 * otherwise register the new BMC device 2898 */ 2899 if (old_bmc) { 2900 bmc = old_bmc; 2901 /* 2902 * Note: old_bmc already has usecount incremented by 2903 * the BMC find functions. 2904 */ 2905 intf->bmc = old_bmc; 2906 mutex_lock(&bmc->dyn_mutex); 2907 list_add_tail(&intf->bmc_link, &bmc->intfs); 2908 mutex_unlock(&bmc->dyn_mutex); 2909 2910 dev_info(intf->si_dev, 2911 "ipmi: interfacing existing BMC (man_id: 0x%6.6x," 2912 " prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n", 2913 bmc->id.manufacturer_id, 2914 bmc->id.product_id, 2915 bmc->id.device_id); 2916 } else { 2917 bmc = kzalloc(sizeof(*bmc), GFP_KERNEL); 2918 if (!bmc) { 2919 rv = -ENOMEM; 2920 goto out; 2921 } 2922 INIT_LIST_HEAD(&bmc->intfs); 2923 mutex_init(&bmc->dyn_mutex); 2924 INIT_WORK(&bmc->remove_work, cleanup_bmc_work); 2925 2926 bmc->id = *id; 2927 bmc->dyn_id_set = 1; 2928 bmc->dyn_guid_set = guid_set; 2929 bmc->guid = *guid; 2930 bmc->dyn_id_expiry = jiffies + IPMI_DYN_DEV_ID_EXPIRY; 2931 2932 bmc->pdev.name = "ipmi_bmc"; 2933 2934 rv = ida_simple_get(&ipmi_bmc_ida, 0, 0, GFP_KERNEL); 2935 if (rv < 0) 2936 goto out; 2937 bmc->pdev.dev.driver = &ipmidriver.driver; 2938 bmc->pdev.id = rv; 2939 bmc->pdev.dev.release = release_bmc_device; 2940 bmc->pdev.dev.type = &bmc_device_type; 2941 kref_init(&bmc->usecount); 2942 2943 intf->bmc = bmc; 2944 mutex_lock(&bmc->dyn_mutex); 2945 list_add_tail(&intf->bmc_link, &bmc->intfs); 2946 mutex_unlock(&bmc->dyn_mutex); 2947 2948 rv = platform_device_register(&bmc->pdev); 2949 if (rv) { 2950 dev_err(intf->si_dev, 2951 PFX " Unable to register bmc device: %d\n", 2952 rv); 2953 goto out_list_del; 2954 } 2955 2956 dev_info(intf->si_dev, 2957 "Found new BMC (man_id: 0x%6.6x, prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n", 2958 bmc->id.manufacturer_id, 2959 bmc->id.product_id, 2960 bmc->id.device_id); 2961 } 2962 2963 /* 2964 * create symlink from system interface device to bmc device 2965 * and back. 2966 */ 2967 rv = sysfs_create_link(&intf->si_dev->kobj, &bmc->pdev.dev.kobj, "bmc"); 2968 if (rv) { 2969 dev_err(intf->si_dev, 2970 PFX "Unable to create bmc symlink: %d\n", rv); 2971 goto out_put_bmc; 2972 } 2973 2974 if (intf_num == -1) 2975 intf_num = intf->intf_num; 2976 intf->my_dev_name = kasprintf(GFP_KERNEL, "ipmi%d", intf_num); 2977 if (!intf->my_dev_name) { 2978 rv = -ENOMEM; 2979 dev_err(intf->si_dev, 2980 PFX "Unable to allocate link from BMC: %d\n", rv); 2981 goto out_unlink1; 2982 } 2983 2984 rv = sysfs_create_link(&bmc->pdev.dev.kobj, &intf->si_dev->kobj, 2985 intf->my_dev_name); 2986 if (rv) { 2987 kfree(intf->my_dev_name); 2988 intf->my_dev_name = NULL; 2989 dev_err(intf->si_dev, 2990 PFX "Unable to create symlink to bmc: %d\n", rv); 2991 goto out_free_my_dev_name; 2992 } 2993 2994 intf->bmc_registered = true; 2995 2996 out: 2997 mutex_unlock(&ipmidriver_mutex); 2998 mutex_lock(&intf->bmc_reg_mutex); 2999 intf->in_bmc_register = false; 3000 return rv; 3001 3002 3003 out_free_my_dev_name: 3004 kfree(intf->my_dev_name); 3005 intf->my_dev_name = NULL; 3006 3007 out_unlink1: 3008 sysfs_remove_link(&intf->si_dev->kobj, "bmc"); 3009 3010 out_put_bmc: 3011 mutex_lock(&bmc->dyn_mutex); 3012 list_del(&intf->bmc_link); 3013 mutex_unlock(&bmc->dyn_mutex); 3014 intf->bmc = &intf->tmp_bmc; 3015 kref_put(&bmc->usecount, cleanup_bmc_device); 3016 goto out; 3017 3018 out_list_del: 3019 mutex_lock(&bmc->dyn_mutex); 3020 list_del(&intf->bmc_link); 3021 mutex_unlock(&bmc->dyn_mutex); 3022 intf->bmc = &intf->tmp_bmc; 3023 put_device(&bmc->pdev.dev); 3024 goto out; 3025 } 3026 3027 static int 3028 send_guid_cmd(struct ipmi_smi *intf, int chan) 3029 { 3030 struct kernel_ipmi_msg msg; 3031 struct ipmi_system_interface_addr si; 3032 3033 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; 3034 si.channel = IPMI_BMC_CHANNEL; 3035 si.lun = 0; 3036 3037 msg.netfn = IPMI_NETFN_APP_REQUEST; 3038 msg.cmd = IPMI_GET_DEVICE_GUID_CMD; 3039 msg.data = NULL; 3040 msg.data_len = 0; 3041 return i_ipmi_request(NULL, 3042 intf, 3043 (struct ipmi_addr *) &si, 3044 0, 3045 &msg, 3046 intf, 3047 NULL, 3048 NULL, 3049 0, 3050 intf->addrinfo[0].address, 3051 intf->addrinfo[0].lun, 3052 -1, 0); 3053 } 3054 3055 static void guid_handler(struct ipmi_smi *intf, struct ipmi_recv_msg *msg) 3056 { 3057 struct bmc_device *bmc = intf->bmc; 3058 3059 if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE) 3060 || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE) 3061 || (msg->msg.cmd != IPMI_GET_DEVICE_GUID_CMD)) 3062 /* Not for me */ 3063 return; 3064 3065 if (msg->msg.data[0] != 0) { 3066 /* Error from getting the GUID, the BMC doesn't have one. */ 3067 bmc->dyn_guid_set = 0; 3068 goto out; 3069 } 3070 3071 if (msg->msg.data_len < 17) { 3072 bmc->dyn_guid_set = 0; 3073 dev_warn(intf->si_dev, 3074 PFX "The GUID response from the BMC was too short, it was %d but should have been 17. Assuming GUID is not available.\n", 3075 msg->msg.data_len); 3076 goto out; 3077 } 3078 3079 memcpy(bmc->fetch_guid.b, msg->msg.data + 1, 16); 3080 /* 3081 * Make sure the guid data is available before setting 3082 * dyn_guid_set. 3083 */ 3084 smp_wmb(); 3085 bmc->dyn_guid_set = 1; 3086 out: 3087 wake_up(&intf->waitq); 3088 } 3089 3090 static void __get_guid(struct ipmi_smi *intf) 3091 { 3092 int rv; 3093 struct bmc_device *bmc = intf->bmc; 3094 3095 bmc->dyn_guid_set = 2; 3096 intf->null_user_handler = guid_handler; 3097 rv = send_guid_cmd(intf, 0); 3098 if (rv) 3099 /* Send failed, no GUID available. */ 3100 bmc->dyn_guid_set = 0; 3101 3102 wait_event(intf->waitq, bmc->dyn_guid_set != 2); 3103 3104 /* dyn_guid_set makes the guid data available. */ 3105 smp_rmb(); 3106 3107 intf->null_user_handler = NULL; 3108 } 3109 3110 static int 3111 send_channel_info_cmd(struct ipmi_smi *intf, int chan) 3112 { 3113 struct kernel_ipmi_msg msg; 3114 unsigned char data[1]; 3115 struct ipmi_system_interface_addr si; 3116 3117 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; 3118 si.channel = IPMI_BMC_CHANNEL; 3119 si.lun = 0; 3120 3121 msg.netfn = IPMI_NETFN_APP_REQUEST; 3122 msg.cmd = IPMI_GET_CHANNEL_INFO_CMD; 3123 msg.data = data; 3124 msg.data_len = 1; 3125 data[0] = chan; 3126 return i_ipmi_request(NULL, 3127 intf, 3128 (struct ipmi_addr *) &si, 3129 0, 3130 &msg, 3131 intf, 3132 NULL, 3133 NULL, 3134 0, 3135 intf->addrinfo[0].address, 3136 intf->addrinfo[0].lun, 3137 -1, 0); 3138 } 3139 3140 static void 3141 channel_handler(struct ipmi_smi *intf, struct ipmi_recv_msg *msg) 3142 { 3143 int rv = 0; 3144 int ch; 3145 unsigned int set = intf->curr_working_cset; 3146 struct ipmi_channel *chans; 3147 3148 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) 3149 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE) 3150 && (msg->msg.cmd == IPMI_GET_CHANNEL_INFO_CMD)) { 3151 /* It's the one we want */ 3152 if (msg->msg.data[0] != 0) { 3153 /* Got an error from the channel, just go on. */ 3154 3155 if (msg->msg.data[0] == IPMI_INVALID_COMMAND_ERR) { 3156 /* 3157 * If the MC does not support this 3158 * command, that is legal. We just 3159 * assume it has one IPMB at channel 3160 * zero. 3161 */ 3162 intf->wchannels[set].c[0].medium 3163 = IPMI_CHANNEL_MEDIUM_IPMB; 3164 intf->wchannels[set].c[0].protocol 3165 = IPMI_CHANNEL_PROTOCOL_IPMB; 3166 3167 intf->channel_list = intf->wchannels + set; 3168 intf->channels_ready = true; 3169 wake_up(&intf->waitq); 3170 goto out; 3171 } 3172 goto next_channel; 3173 } 3174 if (msg->msg.data_len < 4) { 3175 /* Message not big enough, just go on. */ 3176 goto next_channel; 3177 } 3178 ch = intf->curr_channel; 3179 chans = intf->wchannels[set].c; 3180 chans[ch].medium = msg->msg.data[2] & 0x7f; 3181 chans[ch].protocol = msg->msg.data[3] & 0x1f; 3182 3183 next_channel: 3184 intf->curr_channel++; 3185 if (intf->curr_channel >= IPMI_MAX_CHANNELS) { 3186 intf->channel_list = intf->wchannels + set; 3187 intf->channels_ready = true; 3188 wake_up(&intf->waitq); 3189 } else { 3190 intf->channel_list = intf->wchannels + set; 3191 intf->channels_ready = true; 3192 rv = send_channel_info_cmd(intf, intf->curr_channel); 3193 } 3194 3195 if (rv) { 3196 /* Got an error somehow, just give up. */ 3197 dev_warn(intf->si_dev, 3198 PFX "Error sending channel information for channel %d: %d\n", 3199 intf->curr_channel, rv); 3200 3201 intf->channel_list = intf->wchannels + set; 3202 intf->channels_ready = true; 3203 wake_up(&intf->waitq); 3204 } 3205 } 3206 out: 3207 return; 3208 } 3209 3210 /* 3211 * Must be holding intf->bmc_reg_mutex to call this. 3212 */ 3213 static int __scan_channels(struct ipmi_smi *intf, struct ipmi_device_id *id) 3214 { 3215 int rv; 3216 3217 if (ipmi_version_major(id) > 1 3218 || (ipmi_version_major(id) == 1 3219 && ipmi_version_minor(id) >= 5)) { 3220 unsigned int set; 3221 3222 /* 3223 * Start scanning the channels to see what is 3224 * available. 3225 */ 3226 set = !intf->curr_working_cset; 3227 intf->curr_working_cset = set; 3228 memset(&intf->wchannels[set], 0, 3229 sizeof(struct ipmi_channel_set)); 3230 3231 intf->null_user_handler = channel_handler; 3232 intf->curr_channel = 0; 3233 rv = send_channel_info_cmd(intf, 0); 3234 if (rv) { 3235 dev_warn(intf->si_dev, 3236 "Error sending channel information for channel 0, %d\n", 3237 rv); 3238 return -EIO; 3239 } 3240 3241 /* Wait for the channel info to be read. */ 3242 wait_event(intf->waitq, intf->channels_ready); 3243 intf->null_user_handler = NULL; 3244 } else { 3245 unsigned int set = intf->curr_working_cset; 3246 3247 /* Assume a single IPMB channel at zero. */ 3248 intf->wchannels[set].c[0].medium = IPMI_CHANNEL_MEDIUM_IPMB; 3249 intf->wchannels[set].c[0].protocol = IPMI_CHANNEL_PROTOCOL_IPMB; 3250 intf->channel_list = intf->wchannels + set; 3251 intf->channels_ready = true; 3252 } 3253 3254 return 0; 3255 } 3256 3257 static void ipmi_poll(struct ipmi_smi *intf) 3258 { 3259 if (intf->handlers->poll) 3260 intf->handlers->poll(intf->send_info); 3261 /* In case something came in */ 3262 handle_new_recv_msgs(intf); 3263 } 3264 3265 void ipmi_poll_interface(struct ipmi_user *user) 3266 { 3267 ipmi_poll(user->intf); 3268 } 3269 EXPORT_SYMBOL(ipmi_poll_interface); 3270 3271 static void redo_bmc_reg(struct work_struct *work) 3272 { 3273 struct ipmi_smi *intf = container_of(work, struct ipmi_smi, 3274 bmc_reg_work); 3275 3276 if (!intf->in_shutdown) 3277 bmc_get_device_id(intf, NULL, NULL, NULL, NULL); 3278 3279 kref_put(&intf->refcount, intf_free); 3280 } 3281 3282 int ipmi_register_smi(const struct ipmi_smi_handlers *handlers, 3283 void *send_info, 3284 struct device *si_dev, 3285 unsigned char slave_addr) 3286 { 3287 int i, j; 3288 int rv; 3289 struct ipmi_smi *intf, *tintf; 3290 struct list_head *link; 3291 struct ipmi_device_id id; 3292 3293 /* 3294 * Make sure the driver is actually initialized, this handles 3295 * problems with initialization order. 3296 */ 3297 if (!initialized) { 3298 rv = ipmi_init_msghandler(); 3299 if (rv) 3300 return rv; 3301 /* 3302 * The init code doesn't return an error if it was turned 3303 * off, but it won't initialize. Check that. 3304 */ 3305 if (!initialized) 3306 return -ENODEV; 3307 } 3308 3309 intf = kzalloc(sizeof(*intf), GFP_KERNEL); 3310 if (!intf) 3311 return -ENOMEM; 3312 3313 rv = init_srcu_struct(&intf->users_srcu); 3314 if (rv) { 3315 kfree(intf); 3316 return rv; 3317 } 3318 3319 3320 intf->bmc = &intf->tmp_bmc; 3321 INIT_LIST_HEAD(&intf->bmc->intfs); 3322 mutex_init(&intf->bmc->dyn_mutex); 3323 INIT_LIST_HEAD(&intf->bmc_link); 3324 mutex_init(&intf->bmc_reg_mutex); 3325 intf->intf_num = -1; /* Mark it invalid for now. */ 3326 kref_init(&intf->refcount); 3327 INIT_WORK(&intf->bmc_reg_work, redo_bmc_reg); 3328 intf->si_dev = si_dev; 3329 for (j = 0; j < IPMI_MAX_CHANNELS; j++) { 3330 intf->addrinfo[j].address = IPMI_BMC_SLAVE_ADDR; 3331 intf->addrinfo[j].lun = 2; 3332 } 3333 if (slave_addr != 0) 3334 intf->addrinfo[0].address = slave_addr; 3335 INIT_LIST_HEAD(&intf->users); 3336 intf->handlers = handlers; 3337 intf->send_info = send_info; 3338 spin_lock_init(&intf->seq_lock); 3339 for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++) { 3340 intf->seq_table[j].inuse = 0; 3341 intf->seq_table[j].seqid = 0; 3342 } 3343 intf->curr_seq = 0; 3344 spin_lock_init(&intf->waiting_rcv_msgs_lock); 3345 INIT_LIST_HEAD(&intf->waiting_rcv_msgs); 3346 tasklet_init(&intf->recv_tasklet, 3347 smi_recv_tasklet, 3348 (unsigned long) intf); 3349 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 0); 3350 spin_lock_init(&intf->xmit_msgs_lock); 3351 INIT_LIST_HEAD(&intf->xmit_msgs); 3352 INIT_LIST_HEAD(&intf->hp_xmit_msgs); 3353 spin_lock_init(&intf->events_lock); 3354 atomic_set(&intf->event_waiters, 0); 3355 intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME; 3356 INIT_LIST_HEAD(&intf->waiting_events); 3357 intf->waiting_events_count = 0; 3358 mutex_init(&intf->cmd_rcvrs_mutex); 3359 spin_lock_init(&intf->maintenance_mode_lock); 3360 INIT_LIST_HEAD(&intf->cmd_rcvrs); 3361 init_waitqueue_head(&intf->waitq); 3362 for (i = 0; i < IPMI_NUM_STATS; i++) 3363 atomic_set(&intf->stats[i], 0); 3364 3365 mutex_lock(&ipmi_interfaces_mutex); 3366 /* Look for a hole in the numbers. */ 3367 i = 0; 3368 link = &ipmi_interfaces; 3369 list_for_each_entry_rcu(tintf, &ipmi_interfaces, link) { 3370 if (tintf->intf_num != i) { 3371 link = &tintf->link; 3372 break; 3373 } 3374 i++; 3375 } 3376 /* Add the new interface in numeric order. */ 3377 if (i == 0) 3378 list_add_rcu(&intf->link, &ipmi_interfaces); 3379 else 3380 list_add_tail_rcu(&intf->link, link); 3381 3382 rv = handlers->start_processing(send_info, intf); 3383 if (rv) 3384 goto out_err; 3385 3386 rv = __bmc_get_device_id(intf, NULL, &id, NULL, NULL, i); 3387 if (rv) { 3388 dev_err(si_dev, "Unable to get the device id: %d\n", rv); 3389 goto out_err_started; 3390 } 3391 3392 mutex_lock(&intf->bmc_reg_mutex); 3393 rv = __scan_channels(intf, &id); 3394 mutex_unlock(&intf->bmc_reg_mutex); 3395 if (rv) 3396 goto out_err_bmc_reg; 3397 3398 /* 3399 * Keep memory order straight for RCU readers. Make 3400 * sure everything else is committed to memory before 3401 * setting intf_num to mark the interface valid. 3402 */ 3403 smp_wmb(); 3404 intf->intf_num = i; 3405 mutex_unlock(&ipmi_interfaces_mutex); 3406 3407 /* After this point the interface is legal to use. */ 3408 call_smi_watchers(i, intf->si_dev); 3409 3410 return 0; 3411 3412 out_err_bmc_reg: 3413 ipmi_bmc_unregister(intf); 3414 out_err_started: 3415 if (intf->handlers->shutdown) 3416 intf->handlers->shutdown(intf->send_info); 3417 out_err: 3418 list_del_rcu(&intf->link); 3419 mutex_unlock(&ipmi_interfaces_mutex); 3420 synchronize_srcu(&ipmi_interfaces_srcu); 3421 cleanup_srcu_struct(&intf->users_srcu); 3422 kref_put(&intf->refcount, intf_free); 3423 3424 return rv; 3425 } 3426 EXPORT_SYMBOL(ipmi_register_smi); 3427 3428 static void deliver_smi_err_response(struct ipmi_smi *intf, 3429 struct ipmi_smi_msg *msg, 3430 unsigned char err) 3431 { 3432 msg->rsp[0] = msg->data[0] | 4; 3433 msg->rsp[1] = msg->data[1]; 3434 msg->rsp[2] = err; 3435 msg->rsp_size = 3; 3436 /* It's an error, so it will never requeue, no need to check return. */ 3437 handle_one_recv_msg(intf, msg); 3438 } 3439 3440 static void cleanup_smi_msgs(struct ipmi_smi *intf) 3441 { 3442 int i; 3443 struct seq_table *ent; 3444 struct ipmi_smi_msg *msg; 3445 struct list_head *entry; 3446 struct list_head tmplist; 3447 3448 /* Clear out our transmit queues and hold the messages. */ 3449 INIT_LIST_HEAD(&tmplist); 3450 list_splice_tail(&intf->hp_xmit_msgs, &tmplist); 3451 list_splice_tail(&intf->xmit_msgs, &tmplist); 3452 3453 /* Current message first, to preserve order */ 3454 while (intf->curr_msg && !list_empty(&intf->waiting_rcv_msgs)) { 3455 /* Wait for the message to clear out. */ 3456 schedule_timeout(1); 3457 } 3458 3459 /* No need for locks, the interface is down. */ 3460 3461 /* 3462 * Return errors for all pending messages in queue and in the 3463 * tables waiting for remote responses. 3464 */ 3465 while (!list_empty(&tmplist)) { 3466 entry = tmplist.next; 3467 list_del(entry); 3468 msg = list_entry(entry, struct ipmi_smi_msg, link); 3469 deliver_smi_err_response(intf, msg, IPMI_ERR_UNSPECIFIED); 3470 } 3471 3472 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) { 3473 ent = &intf->seq_table[i]; 3474 if (!ent->inuse) 3475 continue; 3476 deliver_err_response(intf, ent->recv_msg, IPMI_ERR_UNSPECIFIED); 3477 } 3478 } 3479 3480 void ipmi_unregister_smi(struct ipmi_smi *intf) 3481 { 3482 struct ipmi_smi_watcher *w; 3483 int intf_num = intf->intf_num, index; 3484 3485 mutex_lock(&ipmi_interfaces_mutex); 3486 intf->intf_num = -1; 3487 intf->in_shutdown = true; 3488 list_del_rcu(&intf->link); 3489 mutex_unlock(&ipmi_interfaces_mutex); 3490 synchronize_srcu(&ipmi_interfaces_srcu); 3491 3492 /* At this point no users can be added to the interface. */ 3493 3494 /* 3495 * Call all the watcher interfaces to tell them that 3496 * an interface is going away. 3497 */ 3498 mutex_lock(&smi_watchers_mutex); 3499 list_for_each_entry(w, &smi_watchers, link) 3500 w->smi_gone(intf_num); 3501 mutex_unlock(&smi_watchers_mutex); 3502 3503 index = srcu_read_lock(&intf->users_srcu); 3504 while (!list_empty(&intf->users)) { 3505 struct ipmi_user *user = 3506 container_of(list_next_rcu(&intf->users), 3507 struct ipmi_user, link); 3508 3509 _ipmi_destroy_user(user); 3510 } 3511 srcu_read_unlock(&intf->users_srcu, index); 3512 3513 if (intf->handlers->shutdown) 3514 intf->handlers->shutdown(intf->send_info); 3515 3516 cleanup_smi_msgs(intf); 3517 3518 ipmi_bmc_unregister(intf); 3519 3520 cleanup_srcu_struct(&intf->users_srcu); 3521 kref_put(&intf->refcount, intf_free); 3522 } 3523 EXPORT_SYMBOL(ipmi_unregister_smi); 3524 3525 static int handle_ipmb_get_msg_rsp(struct ipmi_smi *intf, 3526 struct ipmi_smi_msg *msg) 3527 { 3528 struct ipmi_ipmb_addr ipmb_addr; 3529 struct ipmi_recv_msg *recv_msg; 3530 3531 /* 3532 * This is 11, not 10, because the response must contain a 3533 * completion code. 3534 */ 3535 if (msg->rsp_size < 11) { 3536 /* Message not big enough, just ignore it. */ 3537 ipmi_inc_stat(intf, invalid_ipmb_responses); 3538 return 0; 3539 } 3540 3541 if (msg->rsp[2] != 0) { 3542 /* An error getting the response, just ignore it. */ 3543 return 0; 3544 } 3545 3546 ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE; 3547 ipmb_addr.slave_addr = msg->rsp[6]; 3548 ipmb_addr.channel = msg->rsp[3] & 0x0f; 3549 ipmb_addr.lun = msg->rsp[7] & 3; 3550 3551 /* 3552 * It's a response from a remote entity. Look up the sequence 3553 * number and handle the response. 3554 */ 3555 if (intf_find_seq(intf, 3556 msg->rsp[7] >> 2, 3557 msg->rsp[3] & 0x0f, 3558 msg->rsp[8], 3559 (msg->rsp[4] >> 2) & (~1), 3560 (struct ipmi_addr *) &ipmb_addr, 3561 &recv_msg)) { 3562 /* 3563 * We were unable to find the sequence number, 3564 * so just nuke the message. 3565 */ 3566 ipmi_inc_stat(intf, unhandled_ipmb_responses); 3567 return 0; 3568 } 3569 3570 memcpy(recv_msg->msg_data, &msg->rsp[9], msg->rsp_size - 9); 3571 /* 3572 * The other fields matched, so no need to set them, except 3573 * for netfn, which needs to be the response that was 3574 * returned, not the request value. 3575 */ 3576 recv_msg->msg.netfn = msg->rsp[4] >> 2; 3577 recv_msg->msg.data = recv_msg->msg_data; 3578 recv_msg->msg.data_len = msg->rsp_size - 10; 3579 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE; 3580 if (deliver_response(intf, recv_msg)) 3581 ipmi_inc_stat(intf, unhandled_ipmb_responses); 3582 else 3583 ipmi_inc_stat(intf, handled_ipmb_responses); 3584 3585 return 0; 3586 } 3587 3588 static int handle_ipmb_get_msg_cmd(struct ipmi_smi *intf, 3589 struct ipmi_smi_msg *msg) 3590 { 3591 struct cmd_rcvr *rcvr; 3592 int rv = 0; 3593 unsigned char netfn; 3594 unsigned char cmd; 3595 unsigned char chan; 3596 struct ipmi_user *user = NULL; 3597 struct ipmi_ipmb_addr *ipmb_addr; 3598 struct ipmi_recv_msg *recv_msg; 3599 3600 if (msg->rsp_size < 10) { 3601 /* Message not big enough, just ignore it. */ 3602 ipmi_inc_stat(intf, invalid_commands); 3603 return 0; 3604 } 3605 3606 if (msg->rsp[2] != 0) { 3607 /* An error getting the response, just ignore it. */ 3608 return 0; 3609 } 3610 3611 netfn = msg->rsp[4] >> 2; 3612 cmd = msg->rsp[8]; 3613 chan = msg->rsp[3] & 0xf; 3614 3615 rcu_read_lock(); 3616 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan); 3617 if (rcvr) { 3618 user = rcvr->user; 3619 kref_get(&user->refcount); 3620 } else 3621 user = NULL; 3622 rcu_read_unlock(); 3623 3624 if (user == NULL) { 3625 /* We didn't find a user, deliver an error response. */ 3626 ipmi_inc_stat(intf, unhandled_commands); 3627 3628 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2); 3629 msg->data[1] = IPMI_SEND_MSG_CMD; 3630 msg->data[2] = msg->rsp[3]; 3631 msg->data[3] = msg->rsp[6]; 3632 msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3); 3633 msg->data[5] = ipmb_checksum(&msg->data[3], 2); 3634 msg->data[6] = intf->addrinfo[msg->rsp[3] & 0xf].address; 3635 /* rqseq/lun */ 3636 msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3); 3637 msg->data[8] = msg->rsp[8]; /* cmd */ 3638 msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE; 3639 msg->data[10] = ipmb_checksum(&msg->data[6], 4); 3640 msg->data_size = 11; 3641 3642 ipmi_debug_msg("Invalid command:", msg->data, msg->data_size); 3643 3644 rcu_read_lock(); 3645 if (!intf->in_shutdown) { 3646 smi_send(intf, intf->handlers, msg, 0); 3647 /* 3648 * We used the message, so return the value 3649 * that causes it to not be freed or 3650 * queued. 3651 */ 3652 rv = -1; 3653 } 3654 rcu_read_unlock(); 3655 } else { 3656 recv_msg = ipmi_alloc_recv_msg(); 3657 if (!recv_msg) { 3658 /* 3659 * We couldn't allocate memory for the 3660 * message, so requeue it for handling 3661 * later. 3662 */ 3663 rv = 1; 3664 kref_put(&user->refcount, free_user); 3665 } else { 3666 /* Extract the source address from the data. */ 3667 ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr; 3668 ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE; 3669 ipmb_addr->slave_addr = msg->rsp[6]; 3670 ipmb_addr->lun = msg->rsp[7] & 3; 3671 ipmb_addr->channel = msg->rsp[3] & 0xf; 3672 3673 /* 3674 * Extract the rest of the message information 3675 * from the IPMB header. 3676 */ 3677 recv_msg->user = user; 3678 recv_msg->recv_type = IPMI_CMD_RECV_TYPE; 3679 recv_msg->msgid = msg->rsp[7] >> 2; 3680 recv_msg->msg.netfn = msg->rsp[4] >> 2; 3681 recv_msg->msg.cmd = msg->rsp[8]; 3682 recv_msg->msg.data = recv_msg->msg_data; 3683 3684 /* 3685 * We chop off 10, not 9 bytes because the checksum 3686 * at the end also needs to be removed. 3687 */ 3688 recv_msg->msg.data_len = msg->rsp_size - 10; 3689 memcpy(recv_msg->msg_data, &msg->rsp[9], 3690 msg->rsp_size - 10); 3691 if (deliver_response(intf, recv_msg)) 3692 ipmi_inc_stat(intf, unhandled_commands); 3693 else 3694 ipmi_inc_stat(intf, handled_commands); 3695 } 3696 } 3697 3698 return rv; 3699 } 3700 3701 static int handle_lan_get_msg_rsp(struct ipmi_smi *intf, 3702 struct ipmi_smi_msg *msg) 3703 { 3704 struct ipmi_lan_addr lan_addr; 3705 struct ipmi_recv_msg *recv_msg; 3706 3707 3708 /* 3709 * This is 13, not 12, because the response must contain a 3710 * completion code. 3711 */ 3712 if (msg->rsp_size < 13) { 3713 /* Message not big enough, just ignore it. */ 3714 ipmi_inc_stat(intf, invalid_lan_responses); 3715 return 0; 3716 } 3717 3718 if (msg->rsp[2] != 0) { 3719 /* An error getting the response, just ignore it. */ 3720 return 0; 3721 } 3722 3723 lan_addr.addr_type = IPMI_LAN_ADDR_TYPE; 3724 lan_addr.session_handle = msg->rsp[4]; 3725 lan_addr.remote_SWID = msg->rsp[8]; 3726 lan_addr.local_SWID = msg->rsp[5]; 3727 lan_addr.channel = msg->rsp[3] & 0x0f; 3728 lan_addr.privilege = msg->rsp[3] >> 4; 3729 lan_addr.lun = msg->rsp[9] & 3; 3730 3731 /* 3732 * It's a response from a remote entity. Look up the sequence 3733 * number and handle the response. 3734 */ 3735 if (intf_find_seq(intf, 3736 msg->rsp[9] >> 2, 3737 msg->rsp[3] & 0x0f, 3738 msg->rsp[10], 3739 (msg->rsp[6] >> 2) & (~1), 3740 (struct ipmi_addr *) &lan_addr, 3741 &recv_msg)) { 3742 /* 3743 * We were unable to find the sequence number, 3744 * so just nuke the message. 3745 */ 3746 ipmi_inc_stat(intf, unhandled_lan_responses); 3747 return 0; 3748 } 3749 3750 memcpy(recv_msg->msg_data, &msg->rsp[11], msg->rsp_size - 11); 3751 /* 3752 * The other fields matched, so no need to set them, except 3753 * for netfn, which needs to be the response that was 3754 * returned, not the request value. 3755 */ 3756 recv_msg->msg.netfn = msg->rsp[6] >> 2; 3757 recv_msg->msg.data = recv_msg->msg_data; 3758 recv_msg->msg.data_len = msg->rsp_size - 12; 3759 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE; 3760 if (deliver_response(intf, recv_msg)) 3761 ipmi_inc_stat(intf, unhandled_lan_responses); 3762 else 3763 ipmi_inc_stat(intf, handled_lan_responses); 3764 3765 return 0; 3766 } 3767 3768 static int handle_lan_get_msg_cmd(struct ipmi_smi *intf, 3769 struct ipmi_smi_msg *msg) 3770 { 3771 struct cmd_rcvr *rcvr; 3772 int rv = 0; 3773 unsigned char netfn; 3774 unsigned char cmd; 3775 unsigned char chan; 3776 struct ipmi_user *user = NULL; 3777 struct ipmi_lan_addr *lan_addr; 3778 struct ipmi_recv_msg *recv_msg; 3779 3780 if (msg->rsp_size < 12) { 3781 /* Message not big enough, just ignore it. */ 3782 ipmi_inc_stat(intf, invalid_commands); 3783 return 0; 3784 } 3785 3786 if (msg->rsp[2] != 0) { 3787 /* An error getting the response, just ignore it. */ 3788 return 0; 3789 } 3790 3791 netfn = msg->rsp[6] >> 2; 3792 cmd = msg->rsp[10]; 3793 chan = msg->rsp[3] & 0xf; 3794 3795 rcu_read_lock(); 3796 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan); 3797 if (rcvr) { 3798 user = rcvr->user; 3799 kref_get(&user->refcount); 3800 } else 3801 user = NULL; 3802 rcu_read_unlock(); 3803 3804 if (user == NULL) { 3805 /* We didn't find a user, just give up. */ 3806 ipmi_inc_stat(intf, unhandled_commands); 3807 3808 /* 3809 * Don't do anything with these messages, just allow 3810 * them to be freed. 3811 */ 3812 rv = 0; 3813 } else { 3814 recv_msg = ipmi_alloc_recv_msg(); 3815 if (!recv_msg) { 3816 /* 3817 * We couldn't allocate memory for the 3818 * message, so requeue it for handling later. 3819 */ 3820 rv = 1; 3821 kref_put(&user->refcount, free_user); 3822 } else { 3823 /* Extract the source address from the data. */ 3824 lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr; 3825 lan_addr->addr_type = IPMI_LAN_ADDR_TYPE; 3826 lan_addr->session_handle = msg->rsp[4]; 3827 lan_addr->remote_SWID = msg->rsp[8]; 3828 lan_addr->local_SWID = msg->rsp[5]; 3829 lan_addr->lun = msg->rsp[9] & 3; 3830 lan_addr->channel = msg->rsp[3] & 0xf; 3831 lan_addr->privilege = msg->rsp[3] >> 4; 3832 3833 /* 3834 * Extract the rest of the message information 3835 * from the IPMB header. 3836 */ 3837 recv_msg->user = user; 3838 recv_msg->recv_type = IPMI_CMD_RECV_TYPE; 3839 recv_msg->msgid = msg->rsp[9] >> 2; 3840 recv_msg->msg.netfn = msg->rsp[6] >> 2; 3841 recv_msg->msg.cmd = msg->rsp[10]; 3842 recv_msg->msg.data = recv_msg->msg_data; 3843 3844 /* 3845 * We chop off 12, not 11 bytes because the checksum 3846 * at the end also needs to be removed. 3847 */ 3848 recv_msg->msg.data_len = msg->rsp_size - 12; 3849 memcpy(recv_msg->msg_data, &msg->rsp[11], 3850 msg->rsp_size - 12); 3851 if (deliver_response(intf, recv_msg)) 3852 ipmi_inc_stat(intf, unhandled_commands); 3853 else 3854 ipmi_inc_stat(intf, handled_commands); 3855 } 3856 } 3857 3858 return rv; 3859 } 3860 3861 /* 3862 * This routine will handle "Get Message" command responses with 3863 * channels that use an OEM Medium. The message format belongs to 3864 * the OEM. See IPMI 2.0 specification, Chapter 6 and 3865 * Chapter 22, sections 22.6 and 22.24 for more details. 3866 */ 3867 static int handle_oem_get_msg_cmd(struct ipmi_smi *intf, 3868 struct ipmi_smi_msg *msg) 3869 { 3870 struct cmd_rcvr *rcvr; 3871 int rv = 0; 3872 unsigned char netfn; 3873 unsigned char cmd; 3874 unsigned char chan; 3875 struct ipmi_user *user = NULL; 3876 struct ipmi_system_interface_addr *smi_addr; 3877 struct ipmi_recv_msg *recv_msg; 3878 3879 /* 3880 * We expect the OEM SW to perform error checking 3881 * so we just do some basic sanity checks 3882 */ 3883 if (msg->rsp_size < 4) { 3884 /* Message not big enough, just ignore it. */ 3885 ipmi_inc_stat(intf, invalid_commands); 3886 return 0; 3887 } 3888 3889 if (msg->rsp[2] != 0) { 3890 /* An error getting the response, just ignore it. */ 3891 return 0; 3892 } 3893 3894 /* 3895 * This is an OEM Message so the OEM needs to know how 3896 * handle the message. We do no interpretation. 3897 */ 3898 netfn = msg->rsp[0] >> 2; 3899 cmd = msg->rsp[1]; 3900 chan = msg->rsp[3] & 0xf; 3901 3902 rcu_read_lock(); 3903 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan); 3904 if (rcvr) { 3905 user = rcvr->user; 3906 kref_get(&user->refcount); 3907 } else 3908 user = NULL; 3909 rcu_read_unlock(); 3910 3911 if (user == NULL) { 3912 /* We didn't find a user, just give up. */ 3913 ipmi_inc_stat(intf, unhandled_commands); 3914 3915 /* 3916 * Don't do anything with these messages, just allow 3917 * them to be freed. 3918 */ 3919 3920 rv = 0; 3921 } else { 3922 recv_msg = ipmi_alloc_recv_msg(); 3923 if (!recv_msg) { 3924 /* 3925 * We couldn't allocate memory for the 3926 * message, so requeue it for handling 3927 * later. 3928 */ 3929 rv = 1; 3930 kref_put(&user->refcount, free_user); 3931 } else { 3932 /* 3933 * OEM Messages are expected to be delivered via 3934 * the system interface to SMS software. We might 3935 * need to visit this again depending on OEM 3936 * requirements 3937 */ 3938 smi_addr = ((struct ipmi_system_interface_addr *) 3939 &recv_msg->addr); 3940 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; 3941 smi_addr->channel = IPMI_BMC_CHANNEL; 3942 smi_addr->lun = msg->rsp[0] & 3; 3943 3944 recv_msg->user = user; 3945 recv_msg->user_msg_data = NULL; 3946 recv_msg->recv_type = IPMI_OEM_RECV_TYPE; 3947 recv_msg->msg.netfn = msg->rsp[0] >> 2; 3948 recv_msg->msg.cmd = msg->rsp[1]; 3949 recv_msg->msg.data = recv_msg->msg_data; 3950 3951 /* 3952 * The message starts at byte 4 which follows the 3953 * the Channel Byte in the "GET MESSAGE" command 3954 */ 3955 recv_msg->msg.data_len = msg->rsp_size - 4; 3956 memcpy(recv_msg->msg_data, &msg->rsp[4], 3957 msg->rsp_size - 4); 3958 if (deliver_response(intf, recv_msg)) 3959 ipmi_inc_stat(intf, unhandled_commands); 3960 else 3961 ipmi_inc_stat(intf, handled_commands); 3962 } 3963 } 3964 3965 return rv; 3966 } 3967 3968 static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg, 3969 struct ipmi_smi_msg *msg) 3970 { 3971 struct ipmi_system_interface_addr *smi_addr; 3972 3973 recv_msg->msgid = 0; 3974 smi_addr = (struct ipmi_system_interface_addr *) &recv_msg->addr; 3975 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; 3976 smi_addr->channel = IPMI_BMC_CHANNEL; 3977 smi_addr->lun = msg->rsp[0] & 3; 3978 recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE; 3979 recv_msg->msg.netfn = msg->rsp[0] >> 2; 3980 recv_msg->msg.cmd = msg->rsp[1]; 3981 memcpy(recv_msg->msg_data, &msg->rsp[3], msg->rsp_size - 3); 3982 recv_msg->msg.data = recv_msg->msg_data; 3983 recv_msg->msg.data_len = msg->rsp_size - 3; 3984 } 3985 3986 static int handle_read_event_rsp(struct ipmi_smi *intf, 3987 struct ipmi_smi_msg *msg) 3988 { 3989 struct ipmi_recv_msg *recv_msg, *recv_msg2; 3990 struct list_head msgs; 3991 struct ipmi_user *user; 3992 int rv = 0, deliver_count = 0, index; 3993 unsigned long flags; 3994 3995 if (msg->rsp_size < 19) { 3996 /* Message is too small to be an IPMB event. */ 3997 ipmi_inc_stat(intf, invalid_events); 3998 return 0; 3999 } 4000 4001 if (msg->rsp[2] != 0) { 4002 /* An error getting the event, just ignore it. */ 4003 return 0; 4004 } 4005 4006 INIT_LIST_HEAD(&msgs); 4007 4008 spin_lock_irqsave(&intf->events_lock, flags); 4009 4010 ipmi_inc_stat(intf, events); 4011 4012 /* 4013 * Allocate and fill in one message for every user that is 4014 * getting events. 4015 */ 4016 index = srcu_read_lock(&intf->users_srcu); 4017 list_for_each_entry_rcu(user, &intf->users, link) { 4018 if (!user->gets_events) 4019 continue; 4020 4021 recv_msg = ipmi_alloc_recv_msg(); 4022 if (!recv_msg) { 4023 rcu_read_unlock(); 4024 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, 4025 link) { 4026 list_del(&recv_msg->link); 4027 ipmi_free_recv_msg(recv_msg); 4028 } 4029 /* 4030 * We couldn't allocate memory for the 4031 * message, so requeue it for handling 4032 * later. 4033 */ 4034 rv = 1; 4035 goto out; 4036 } 4037 4038 deliver_count++; 4039 4040 copy_event_into_recv_msg(recv_msg, msg); 4041 recv_msg->user = user; 4042 kref_get(&user->refcount); 4043 list_add_tail(&recv_msg->link, &msgs); 4044 } 4045 srcu_read_unlock(&intf->users_srcu, index); 4046 4047 if (deliver_count) { 4048 /* Now deliver all the messages. */ 4049 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) { 4050 list_del(&recv_msg->link); 4051 deliver_local_response(intf, recv_msg); 4052 } 4053 } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) { 4054 /* 4055 * No one to receive the message, put it in queue if there's 4056 * not already too many things in the queue. 4057 */ 4058 recv_msg = ipmi_alloc_recv_msg(); 4059 if (!recv_msg) { 4060 /* 4061 * We couldn't allocate memory for the 4062 * message, so requeue it for handling 4063 * later. 4064 */ 4065 rv = 1; 4066 goto out; 4067 } 4068 4069 copy_event_into_recv_msg(recv_msg, msg); 4070 list_add_tail(&recv_msg->link, &intf->waiting_events); 4071 intf->waiting_events_count++; 4072 } else if (!intf->event_msg_printed) { 4073 /* 4074 * There's too many things in the queue, discard this 4075 * message. 4076 */ 4077 dev_warn(intf->si_dev, 4078 PFX "Event queue full, discarding incoming events\n"); 4079 intf->event_msg_printed = 1; 4080 } 4081 4082 out: 4083 spin_unlock_irqrestore(&intf->events_lock, flags); 4084 4085 return rv; 4086 } 4087 4088 static int handle_bmc_rsp(struct ipmi_smi *intf, 4089 struct ipmi_smi_msg *msg) 4090 { 4091 struct ipmi_recv_msg *recv_msg; 4092 struct ipmi_system_interface_addr *smi_addr; 4093 4094 recv_msg = (struct ipmi_recv_msg *) msg->user_data; 4095 if (recv_msg == NULL) { 4096 dev_warn(intf->si_dev, 4097 "IPMI message received with no owner. This could be because of a malformed message, or because of a hardware error. Contact your hardware vender for assistance\n"); 4098 return 0; 4099 } 4100 4101 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE; 4102 recv_msg->msgid = msg->msgid; 4103 smi_addr = ((struct ipmi_system_interface_addr *) 4104 &recv_msg->addr); 4105 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; 4106 smi_addr->channel = IPMI_BMC_CHANNEL; 4107 smi_addr->lun = msg->rsp[0] & 3; 4108 recv_msg->msg.netfn = msg->rsp[0] >> 2; 4109 recv_msg->msg.cmd = msg->rsp[1]; 4110 memcpy(recv_msg->msg_data, &msg->rsp[2], msg->rsp_size - 2); 4111 recv_msg->msg.data = recv_msg->msg_data; 4112 recv_msg->msg.data_len = msg->rsp_size - 2; 4113 deliver_local_response(intf, recv_msg); 4114 4115 return 0; 4116 } 4117 4118 /* 4119 * Handle a received message. Return 1 if the message should be requeued, 4120 * 0 if the message should be freed, or -1 if the message should not 4121 * be freed or requeued. 4122 */ 4123 static int handle_one_recv_msg(struct ipmi_smi *intf, 4124 struct ipmi_smi_msg *msg) 4125 { 4126 int requeue; 4127 int chan; 4128 4129 ipmi_debug_msg("Recv:", msg->rsp, msg->rsp_size); 4130 if (msg->rsp_size < 2) { 4131 /* Message is too small to be correct. */ 4132 dev_warn(intf->si_dev, 4133 PFX "BMC returned to small a message for netfn %x cmd %x, got %d bytes\n", 4134 (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size); 4135 4136 /* Generate an error response for the message. */ 4137 msg->rsp[0] = msg->data[0] | (1 << 2); 4138 msg->rsp[1] = msg->data[1]; 4139 msg->rsp[2] = IPMI_ERR_UNSPECIFIED; 4140 msg->rsp_size = 3; 4141 } else if (((msg->rsp[0] >> 2) != ((msg->data[0] >> 2) | 1)) 4142 || (msg->rsp[1] != msg->data[1])) { 4143 /* 4144 * The NetFN and Command in the response is not even 4145 * marginally correct. 4146 */ 4147 dev_warn(intf->si_dev, 4148 PFX "BMC returned incorrect response, expected netfn %x cmd %x, got netfn %x cmd %x\n", 4149 (msg->data[0] >> 2) | 1, msg->data[1], 4150 msg->rsp[0] >> 2, msg->rsp[1]); 4151 4152 /* Generate an error response for the message. */ 4153 msg->rsp[0] = msg->data[0] | (1 << 2); 4154 msg->rsp[1] = msg->data[1]; 4155 msg->rsp[2] = IPMI_ERR_UNSPECIFIED; 4156 msg->rsp_size = 3; 4157 } 4158 4159 if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2)) 4160 && (msg->rsp[1] == IPMI_SEND_MSG_CMD) 4161 && (msg->user_data != NULL)) { 4162 /* 4163 * It's a response to a response we sent. For this we 4164 * deliver a send message response to the user. 4165 */ 4166 struct ipmi_recv_msg *recv_msg = msg->user_data; 4167 4168 requeue = 0; 4169 if (msg->rsp_size < 2) 4170 /* Message is too small to be correct. */ 4171 goto out; 4172 4173 chan = msg->data[2] & 0x0f; 4174 if (chan >= IPMI_MAX_CHANNELS) 4175 /* Invalid channel number */ 4176 goto out; 4177 4178 if (!recv_msg) 4179 goto out; 4180 4181 recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE; 4182 recv_msg->msg.data = recv_msg->msg_data; 4183 recv_msg->msg.data_len = 1; 4184 recv_msg->msg_data[0] = msg->rsp[2]; 4185 deliver_local_response(intf, recv_msg); 4186 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2)) 4187 && (msg->rsp[1] == IPMI_GET_MSG_CMD)) { 4188 struct ipmi_channel *chans; 4189 4190 /* It's from the receive queue. */ 4191 chan = msg->rsp[3] & 0xf; 4192 if (chan >= IPMI_MAX_CHANNELS) { 4193 /* Invalid channel number */ 4194 requeue = 0; 4195 goto out; 4196 } 4197 4198 /* 4199 * We need to make sure the channels have been initialized. 4200 * The channel_handler routine will set the "curr_channel" 4201 * equal to or greater than IPMI_MAX_CHANNELS when all the 4202 * channels for this interface have been initialized. 4203 */ 4204 if (!intf->channels_ready) { 4205 requeue = 0; /* Throw the message away */ 4206 goto out; 4207 } 4208 4209 chans = READ_ONCE(intf->channel_list)->c; 4210 4211 switch (chans[chan].medium) { 4212 case IPMI_CHANNEL_MEDIUM_IPMB: 4213 if (msg->rsp[4] & 0x04) { 4214 /* 4215 * It's a response, so find the 4216 * requesting message and send it up. 4217 */ 4218 requeue = handle_ipmb_get_msg_rsp(intf, msg); 4219 } else { 4220 /* 4221 * It's a command to the SMS from some other 4222 * entity. Handle that. 4223 */ 4224 requeue = handle_ipmb_get_msg_cmd(intf, msg); 4225 } 4226 break; 4227 4228 case IPMI_CHANNEL_MEDIUM_8023LAN: 4229 case IPMI_CHANNEL_MEDIUM_ASYNC: 4230 if (msg->rsp[6] & 0x04) { 4231 /* 4232 * It's a response, so find the 4233 * requesting message and send it up. 4234 */ 4235 requeue = handle_lan_get_msg_rsp(intf, msg); 4236 } else { 4237 /* 4238 * It's a command to the SMS from some other 4239 * entity. Handle that. 4240 */ 4241 requeue = handle_lan_get_msg_cmd(intf, msg); 4242 } 4243 break; 4244 4245 default: 4246 /* Check for OEM Channels. Clients had better 4247 register for these commands. */ 4248 if ((chans[chan].medium >= IPMI_CHANNEL_MEDIUM_OEM_MIN) 4249 && (chans[chan].medium 4250 <= IPMI_CHANNEL_MEDIUM_OEM_MAX)) { 4251 requeue = handle_oem_get_msg_cmd(intf, msg); 4252 } else { 4253 /* 4254 * We don't handle the channel type, so just 4255 * free the message. 4256 */ 4257 requeue = 0; 4258 } 4259 } 4260 4261 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2)) 4262 && (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD)) { 4263 /* It's an asynchronous event. */ 4264 requeue = handle_read_event_rsp(intf, msg); 4265 } else { 4266 /* It's a response from the local BMC. */ 4267 requeue = handle_bmc_rsp(intf, msg); 4268 } 4269 4270 out: 4271 return requeue; 4272 } 4273 4274 /* 4275 * If there are messages in the queue or pretimeouts, handle them. 4276 */ 4277 static void handle_new_recv_msgs(struct ipmi_smi *intf) 4278 { 4279 struct ipmi_smi_msg *smi_msg; 4280 unsigned long flags = 0; 4281 int rv; 4282 int run_to_completion = intf->run_to_completion; 4283 4284 /* See if any waiting messages need to be processed. */ 4285 if (!run_to_completion) 4286 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags); 4287 while (!list_empty(&intf->waiting_rcv_msgs)) { 4288 smi_msg = list_entry(intf->waiting_rcv_msgs.next, 4289 struct ipmi_smi_msg, link); 4290 list_del(&smi_msg->link); 4291 if (!run_to_completion) 4292 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock, 4293 flags); 4294 rv = handle_one_recv_msg(intf, smi_msg); 4295 if (!run_to_completion) 4296 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags); 4297 if (rv > 0) { 4298 /* 4299 * To preserve message order, quit if we 4300 * can't handle a message. Add the message 4301 * back at the head, this is safe because this 4302 * tasklet is the only thing that pulls the 4303 * messages. 4304 */ 4305 list_add(&smi_msg->link, &intf->waiting_rcv_msgs); 4306 break; 4307 } else { 4308 if (rv == 0) 4309 /* Message handled */ 4310 ipmi_free_smi_msg(smi_msg); 4311 /* If rv < 0, fatal error, del but don't free. */ 4312 } 4313 } 4314 if (!run_to_completion) 4315 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock, flags); 4316 4317 /* 4318 * If the pretimout count is non-zero, decrement one from it and 4319 * deliver pretimeouts to all the users. 4320 */ 4321 if (atomic_add_unless(&intf->watchdog_pretimeouts_to_deliver, -1, 0)) { 4322 struct ipmi_user *user; 4323 int index; 4324 4325 index = srcu_read_lock(&intf->users_srcu); 4326 list_for_each_entry_rcu(user, &intf->users, link) { 4327 if (user->handler->ipmi_watchdog_pretimeout) 4328 user->handler->ipmi_watchdog_pretimeout( 4329 user->handler_data); 4330 } 4331 srcu_read_unlock(&intf->users_srcu, index); 4332 } 4333 } 4334 4335 static void smi_recv_tasklet(unsigned long val) 4336 { 4337 unsigned long flags = 0; /* keep us warning-free. */ 4338 struct ipmi_smi *intf = (struct ipmi_smi *) val; 4339 int run_to_completion = intf->run_to_completion; 4340 struct ipmi_smi_msg *newmsg = NULL; 4341 4342 /* 4343 * Start the next message if available. 4344 * 4345 * Do this here, not in the actual receiver, because we may deadlock 4346 * because the lower layer is allowed to hold locks while calling 4347 * message delivery. 4348 */ 4349 4350 rcu_read_lock(); 4351 4352 if (!run_to_completion) 4353 spin_lock_irqsave(&intf->xmit_msgs_lock, flags); 4354 if (intf->curr_msg == NULL && !intf->in_shutdown) { 4355 struct list_head *entry = NULL; 4356 4357 /* Pick the high priority queue first. */ 4358 if (!list_empty(&intf->hp_xmit_msgs)) 4359 entry = intf->hp_xmit_msgs.next; 4360 else if (!list_empty(&intf->xmit_msgs)) 4361 entry = intf->xmit_msgs.next; 4362 4363 if (entry) { 4364 list_del(entry); 4365 newmsg = list_entry(entry, struct ipmi_smi_msg, link); 4366 intf->curr_msg = newmsg; 4367 } 4368 } 4369 if (!run_to_completion) 4370 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags); 4371 if (newmsg) 4372 intf->handlers->sender(intf->send_info, newmsg); 4373 4374 rcu_read_unlock(); 4375 4376 handle_new_recv_msgs(intf); 4377 } 4378 4379 /* Handle a new message from the lower layer. */ 4380 void ipmi_smi_msg_received(struct ipmi_smi *intf, 4381 struct ipmi_smi_msg *msg) 4382 { 4383 unsigned long flags = 0; /* keep us warning-free. */ 4384 int run_to_completion = intf->run_to_completion; 4385 4386 if ((msg->data_size >= 2) 4387 && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2)) 4388 && (msg->data[1] == IPMI_SEND_MSG_CMD) 4389 && (msg->user_data == NULL)) { 4390 4391 if (intf->in_shutdown) 4392 goto free_msg; 4393 4394 /* 4395 * This is the local response to a command send, start 4396 * the timer for these. The user_data will not be 4397 * NULL if this is a response send, and we will let 4398 * response sends just go through. 4399 */ 4400 4401 /* 4402 * Check for errors, if we get certain errors (ones 4403 * that mean basically we can try again later), we 4404 * ignore them and start the timer. Otherwise we 4405 * report the error immediately. 4406 */ 4407 if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0) 4408 && (msg->rsp[2] != IPMI_NODE_BUSY_ERR) 4409 && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR) 4410 && (msg->rsp[2] != IPMI_BUS_ERR) 4411 && (msg->rsp[2] != IPMI_NAK_ON_WRITE_ERR)) { 4412 int ch = msg->rsp[3] & 0xf; 4413 struct ipmi_channel *chans; 4414 4415 /* Got an error sending the message, handle it. */ 4416 4417 chans = READ_ONCE(intf->channel_list)->c; 4418 if ((chans[ch].medium == IPMI_CHANNEL_MEDIUM_8023LAN) 4419 || (chans[ch].medium == IPMI_CHANNEL_MEDIUM_ASYNC)) 4420 ipmi_inc_stat(intf, sent_lan_command_errs); 4421 else 4422 ipmi_inc_stat(intf, sent_ipmb_command_errs); 4423 intf_err_seq(intf, msg->msgid, msg->rsp[2]); 4424 } else 4425 /* The message was sent, start the timer. */ 4426 intf_start_seq_timer(intf, msg->msgid); 4427 4428 free_msg: 4429 ipmi_free_smi_msg(msg); 4430 } else { 4431 /* 4432 * To preserve message order, we keep a queue and deliver from 4433 * a tasklet. 4434 */ 4435 if (!run_to_completion) 4436 spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags); 4437 list_add_tail(&msg->link, &intf->waiting_rcv_msgs); 4438 if (!run_to_completion) 4439 spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock, 4440 flags); 4441 } 4442 4443 if (!run_to_completion) 4444 spin_lock_irqsave(&intf->xmit_msgs_lock, flags); 4445 /* 4446 * We can get an asynchronous event or receive message in addition 4447 * to commands we send. 4448 */ 4449 if (msg == intf->curr_msg) 4450 intf->curr_msg = NULL; 4451 if (!run_to_completion) 4452 spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags); 4453 4454 if (run_to_completion) 4455 smi_recv_tasklet((unsigned long) intf); 4456 else 4457 tasklet_schedule(&intf->recv_tasklet); 4458 } 4459 EXPORT_SYMBOL(ipmi_smi_msg_received); 4460 4461 void ipmi_smi_watchdog_pretimeout(struct ipmi_smi *intf) 4462 { 4463 if (intf->in_shutdown) 4464 return; 4465 4466 atomic_set(&intf->watchdog_pretimeouts_to_deliver, 1); 4467 tasklet_schedule(&intf->recv_tasklet); 4468 } 4469 EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout); 4470 4471 static struct ipmi_smi_msg * 4472 smi_from_recv_msg(struct ipmi_smi *intf, struct ipmi_recv_msg *recv_msg, 4473 unsigned char seq, long seqid) 4474 { 4475 struct ipmi_smi_msg *smi_msg = ipmi_alloc_smi_msg(); 4476 if (!smi_msg) 4477 /* 4478 * If we can't allocate the message, then just return, we 4479 * get 4 retries, so this should be ok. 4480 */ 4481 return NULL; 4482 4483 memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len); 4484 smi_msg->data_size = recv_msg->msg.data_len; 4485 smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid); 4486 4487 ipmi_debug_msg("Resend: ", smi_msg->data, smi_msg->data_size); 4488 4489 return smi_msg; 4490 } 4491 4492 static void check_msg_timeout(struct ipmi_smi *intf, struct seq_table *ent, 4493 struct list_head *timeouts, 4494 unsigned long timeout_period, 4495 int slot, unsigned long *flags, 4496 unsigned int *waiting_msgs) 4497 { 4498 struct ipmi_recv_msg *msg; 4499 4500 if (intf->in_shutdown) 4501 return; 4502 4503 if (!ent->inuse) 4504 return; 4505 4506 if (timeout_period < ent->timeout) { 4507 ent->timeout -= timeout_period; 4508 (*waiting_msgs)++; 4509 return; 4510 } 4511 4512 if (ent->retries_left == 0) { 4513 /* The message has used all its retries. */ 4514 ent->inuse = 0; 4515 msg = ent->recv_msg; 4516 list_add_tail(&msg->link, timeouts); 4517 if (ent->broadcast) 4518 ipmi_inc_stat(intf, timed_out_ipmb_broadcasts); 4519 else if (is_lan_addr(&ent->recv_msg->addr)) 4520 ipmi_inc_stat(intf, timed_out_lan_commands); 4521 else 4522 ipmi_inc_stat(intf, timed_out_ipmb_commands); 4523 } else { 4524 struct ipmi_smi_msg *smi_msg; 4525 /* More retries, send again. */ 4526 4527 (*waiting_msgs)++; 4528 4529 /* 4530 * Start with the max timer, set to normal timer after 4531 * the message is sent. 4532 */ 4533 ent->timeout = MAX_MSG_TIMEOUT; 4534 ent->retries_left--; 4535 smi_msg = smi_from_recv_msg(intf, ent->recv_msg, slot, 4536 ent->seqid); 4537 if (!smi_msg) { 4538 if (is_lan_addr(&ent->recv_msg->addr)) 4539 ipmi_inc_stat(intf, 4540 dropped_rexmit_lan_commands); 4541 else 4542 ipmi_inc_stat(intf, 4543 dropped_rexmit_ipmb_commands); 4544 return; 4545 } 4546 4547 spin_unlock_irqrestore(&intf->seq_lock, *flags); 4548 4549 /* 4550 * Send the new message. We send with a zero 4551 * priority. It timed out, I doubt time is that 4552 * critical now, and high priority messages are really 4553 * only for messages to the local MC, which don't get 4554 * resent. 4555 */ 4556 if (intf->handlers) { 4557 if (is_lan_addr(&ent->recv_msg->addr)) 4558 ipmi_inc_stat(intf, 4559 retransmitted_lan_commands); 4560 else 4561 ipmi_inc_stat(intf, 4562 retransmitted_ipmb_commands); 4563 4564 smi_send(intf, intf->handlers, smi_msg, 0); 4565 } else 4566 ipmi_free_smi_msg(smi_msg); 4567 4568 spin_lock_irqsave(&intf->seq_lock, *flags); 4569 } 4570 } 4571 4572 static unsigned int ipmi_timeout_handler(struct ipmi_smi *intf, 4573 unsigned long timeout_period) 4574 { 4575 struct list_head timeouts; 4576 struct ipmi_recv_msg *msg, *msg2; 4577 unsigned long flags; 4578 int i; 4579 unsigned int waiting_msgs = 0; 4580 4581 if (!intf->bmc_registered) { 4582 kref_get(&intf->refcount); 4583 if (!schedule_work(&intf->bmc_reg_work)) { 4584 kref_put(&intf->refcount, intf_free); 4585 waiting_msgs++; 4586 } 4587 } 4588 4589 /* 4590 * Go through the seq table and find any messages that 4591 * have timed out, putting them in the timeouts 4592 * list. 4593 */ 4594 INIT_LIST_HEAD(&timeouts); 4595 spin_lock_irqsave(&intf->seq_lock, flags); 4596 if (intf->ipmb_maintenance_mode_timeout) { 4597 if (intf->ipmb_maintenance_mode_timeout <= timeout_period) 4598 intf->ipmb_maintenance_mode_timeout = 0; 4599 else 4600 intf->ipmb_maintenance_mode_timeout -= timeout_period; 4601 } 4602 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) 4603 check_msg_timeout(intf, &intf->seq_table[i], 4604 &timeouts, timeout_period, i, 4605 &flags, &waiting_msgs); 4606 spin_unlock_irqrestore(&intf->seq_lock, flags); 4607 4608 list_for_each_entry_safe(msg, msg2, &timeouts, link) 4609 deliver_err_response(intf, msg, IPMI_TIMEOUT_COMPLETION_CODE); 4610 4611 /* 4612 * Maintenance mode handling. Check the timeout 4613 * optimistically before we claim the lock. It may 4614 * mean a timeout gets missed occasionally, but that 4615 * only means the timeout gets extended by one period 4616 * in that case. No big deal, and it avoids the lock 4617 * most of the time. 4618 */ 4619 if (intf->auto_maintenance_timeout > 0) { 4620 spin_lock_irqsave(&intf->maintenance_mode_lock, flags); 4621 if (intf->auto_maintenance_timeout > 0) { 4622 intf->auto_maintenance_timeout 4623 -= timeout_period; 4624 if (!intf->maintenance_mode 4625 && (intf->auto_maintenance_timeout <= 0)) { 4626 intf->maintenance_mode_enable = false; 4627 maintenance_mode_update(intf); 4628 } 4629 } 4630 spin_unlock_irqrestore(&intf->maintenance_mode_lock, 4631 flags); 4632 } 4633 4634 tasklet_schedule(&intf->recv_tasklet); 4635 4636 return waiting_msgs; 4637 } 4638 4639 static void ipmi_request_event(struct ipmi_smi *intf) 4640 { 4641 /* No event requests when in maintenance mode. */ 4642 if (intf->maintenance_mode_enable) 4643 return; 4644 4645 if (!intf->in_shutdown) 4646 intf->handlers->request_events(intf->send_info); 4647 } 4648 4649 static struct timer_list ipmi_timer; 4650 4651 static atomic_t stop_operation; 4652 4653 static void ipmi_timeout(struct timer_list *unused) 4654 { 4655 struct ipmi_smi *intf; 4656 int nt = 0, index; 4657 4658 if (atomic_read(&stop_operation)) 4659 return; 4660 4661 index = srcu_read_lock(&ipmi_interfaces_srcu); 4662 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) { 4663 int lnt = 0; 4664 4665 if (atomic_read(&intf->event_waiters)) { 4666 intf->ticks_to_req_ev--; 4667 if (intf->ticks_to_req_ev == 0) { 4668 ipmi_request_event(intf); 4669 intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME; 4670 } 4671 lnt++; 4672 } 4673 4674 lnt += ipmi_timeout_handler(intf, IPMI_TIMEOUT_TIME); 4675 4676 lnt = !!lnt; 4677 if (lnt != intf->last_needs_timer && 4678 intf->handlers->set_need_watch) 4679 intf->handlers->set_need_watch(intf->send_info, lnt); 4680 intf->last_needs_timer = lnt; 4681 4682 nt += lnt; 4683 } 4684 srcu_read_unlock(&ipmi_interfaces_srcu, index); 4685 4686 if (nt) 4687 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES); 4688 } 4689 4690 static void need_waiter(struct ipmi_smi *intf) 4691 { 4692 /* Racy, but worst case we start the timer twice. */ 4693 if (!timer_pending(&ipmi_timer)) 4694 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES); 4695 } 4696 4697 static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0); 4698 static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0); 4699 4700 static void free_smi_msg(struct ipmi_smi_msg *msg) 4701 { 4702 atomic_dec(&smi_msg_inuse_count); 4703 kfree(msg); 4704 } 4705 4706 struct ipmi_smi_msg *ipmi_alloc_smi_msg(void) 4707 { 4708 struct ipmi_smi_msg *rv; 4709 rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC); 4710 if (rv) { 4711 rv->done = free_smi_msg; 4712 rv->user_data = NULL; 4713 atomic_inc(&smi_msg_inuse_count); 4714 } 4715 return rv; 4716 } 4717 EXPORT_SYMBOL(ipmi_alloc_smi_msg); 4718 4719 static void free_recv_msg(struct ipmi_recv_msg *msg) 4720 { 4721 atomic_dec(&recv_msg_inuse_count); 4722 kfree(msg); 4723 } 4724 4725 static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void) 4726 { 4727 struct ipmi_recv_msg *rv; 4728 4729 rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC); 4730 if (rv) { 4731 rv->user = NULL; 4732 rv->done = free_recv_msg; 4733 atomic_inc(&recv_msg_inuse_count); 4734 } 4735 return rv; 4736 } 4737 4738 void ipmi_free_recv_msg(struct ipmi_recv_msg *msg) 4739 { 4740 if (msg->user) 4741 kref_put(&msg->user->refcount, free_user); 4742 msg->done(msg); 4743 } 4744 EXPORT_SYMBOL(ipmi_free_recv_msg); 4745 4746 static atomic_t panic_done_count = ATOMIC_INIT(0); 4747 4748 static void dummy_smi_done_handler(struct ipmi_smi_msg *msg) 4749 { 4750 atomic_dec(&panic_done_count); 4751 } 4752 4753 static void dummy_recv_done_handler(struct ipmi_recv_msg *msg) 4754 { 4755 atomic_dec(&panic_done_count); 4756 } 4757 4758 /* 4759 * Inside a panic, send a message and wait for a response. 4760 */ 4761 static void ipmi_panic_request_and_wait(struct ipmi_smi *intf, 4762 struct ipmi_addr *addr, 4763 struct kernel_ipmi_msg *msg) 4764 { 4765 struct ipmi_smi_msg smi_msg; 4766 struct ipmi_recv_msg recv_msg; 4767 int rv; 4768 4769 smi_msg.done = dummy_smi_done_handler; 4770 recv_msg.done = dummy_recv_done_handler; 4771 atomic_add(2, &panic_done_count); 4772 rv = i_ipmi_request(NULL, 4773 intf, 4774 addr, 4775 0, 4776 msg, 4777 intf, 4778 &smi_msg, 4779 &recv_msg, 4780 0, 4781 intf->addrinfo[0].address, 4782 intf->addrinfo[0].lun, 4783 0, 1); /* Don't retry, and don't wait. */ 4784 if (rv) 4785 atomic_sub(2, &panic_done_count); 4786 else if (intf->handlers->flush_messages) 4787 intf->handlers->flush_messages(intf->send_info); 4788 4789 while (atomic_read(&panic_done_count) != 0) 4790 ipmi_poll(intf); 4791 } 4792 4793 static void event_receiver_fetcher(struct ipmi_smi *intf, 4794 struct ipmi_recv_msg *msg) 4795 { 4796 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) 4797 && (msg->msg.netfn == IPMI_NETFN_SENSOR_EVENT_RESPONSE) 4798 && (msg->msg.cmd == IPMI_GET_EVENT_RECEIVER_CMD) 4799 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) { 4800 /* A get event receiver command, save it. */ 4801 intf->event_receiver = msg->msg.data[1]; 4802 intf->event_receiver_lun = msg->msg.data[2] & 0x3; 4803 } 4804 } 4805 4806 static void device_id_fetcher(struct ipmi_smi *intf, struct ipmi_recv_msg *msg) 4807 { 4808 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) 4809 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE) 4810 && (msg->msg.cmd == IPMI_GET_DEVICE_ID_CMD) 4811 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) { 4812 /* 4813 * A get device id command, save if we are an event 4814 * receiver or generator. 4815 */ 4816 intf->local_sel_device = (msg->msg.data[6] >> 2) & 1; 4817 intf->local_event_generator = (msg->msg.data[6] >> 5) & 1; 4818 } 4819 } 4820 4821 static void send_panic_events(struct ipmi_smi *intf, char *str) 4822 { 4823 struct kernel_ipmi_msg msg; 4824 unsigned char data[16]; 4825 struct ipmi_system_interface_addr *si; 4826 struct ipmi_addr addr; 4827 char *p = str; 4828 struct ipmi_ipmb_addr *ipmb; 4829 int j; 4830 4831 if (ipmi_send_panic_event == IPMI_SEND_PANIC_EVENT_NONE) 4832 return; 4833 4834 si = (struct ipmi_system_interface_addr *) &addr; 4835 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; 4836 si->channel = IPMI_BMC_CHANNEL; 4837 si->lun = 0; 4838 4839 /* Fill in an event telling that we have failed. */ 4840 msg.netfn = 0x04; /* Sensor or Event. */ 4841 msg.cmd = 2; /* Platform event command. */ 4842 msg.data = data; 4843 msg.data_len = 8; 4844 data[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */ 4845 data[1] = 0x03; /* This is for IPMI 1.0. */ 4846 data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */ 4847 data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */ 4848 data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */ 4849 4850 /* 4851 * Put a few breadcrumbs in. Hopefully later we can add more things 4852 * to make the panic events more useful. 4853 */ 4854 if (str) { 4855 data[3] = str[0]; 4856 data[6] = str[1]; 4857 data[7] = str[2]; 4858 } 4859 4860 /* Send the event announcing the panic. */ 4861 ipmi_panic_request_and_wait(intf, &addr, &msg); 4862 4863 /* 4864 * On every interface, dump a bunch of OEM event holding the 4865 * string. 4866 */ 4867 if (ipmi_send_panic_event != IPMI_SEND_PANIC_EVENT_STRING || !str) 4868 return; 4869 4870 /* 4871 * intf_num is used as an marker to tell if the 4872 * interface is valid. Thus we need a read barrier to 4873 * make sure data fetched before checking intf_num 4874 * won't be used. 4875 */ 4876 smp_rmb(); 4877 4878 /* 4879 * First job here is to figure out where to send the 4880 * OEM events. There's no way in IPMI to send OEM 4881 * events using an event send command, so we have to 4882 * find the SEL to put them in and stick them in 4883 * there. 4884 */ 4885 4886 /* Get capabilities from the get device id. */ 4887 intf->local_sel_device = 0; 4888 intf->local_event_generator = 0; 4889 intf->event_receiver = 0; 4890 4891 /* Request the device info from the local MC. */ 4892 msg.netfn = IPMI_NETFN_APP_REQUEST; 4893 msg.cmd = IPMI_GET_DEVICE_ID_CMD; 4894 msg.data = NULL; 4895 msg.data_len = 0; 4896 intf->null_user_handler = device_id_fetcher; 4897 ipmi_panic_request_and_wait(intf, &addr, &msg); 4898 4899 if (intf->local_event_generator) { 4900 /* Request the event receiver from the local MC. */ 4901 msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST; 4902 msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD; 4903 msg.data = NULL; 4904 msg.data_len = 0; 4905 intf->null_user_handler = event_receiver_fetcher; 4906 ipmi_panic_request_and_wait(intf, &addr, &msg); 4907 } 4908 intf->null_user_handler = NULL; 4909 4910 /* 4911 * Validate the event receiver. The low bit must not 4912 * be 1 (it must be a valid IPMB address), it cannot 4913 * be zero, and it must not be my address. 4914 */ 4915 if (((intf->event_receiver & 1) == 0) 4916 && (intf->event_receiver != 0) 4917 && (intf->event_receiver != intf->addrinfo[0].address)) { 4918 /* 4919 * The event receiver is valid, send an IPMB 4920 * message. 4921 */ 4922 ipmb = (struct ipmi_ipmb_addr *) &addr; 4923 ipmb->addr_type = IPMI_IPMB_ADDR_TYPE; 4924 ipmb->channel = 0; /* FIXME - is this right? */ 4925 ipmb->lun = intf->event_receiver_lun; 4926 ipmb->slave_addr = intf->event_receiver; 4927 } else if (intf->local_sel_device) { 4928 /* 4929 * The event receiver was not valid (or was 4930 * me), but I am an SEL device, just dump it 4931 * in my SEL. 4932 */ 4933 si = (struct ipmi_system_interface_addr *) &addr; 4934 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; 4935 si->channel = IPMI_BMC_CHANNEL; 4936 si->lun = 0; 4937 } else 4938 return; /* No where to send the event. */ 4939 4940 msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */ 4941 msg.cmd = IPMI_ADD_SEL_ENTRY_CMD; 4942 msg.data = data; 4943 msg.data_len = 16; 4944 4945 j = 0; 4946 while (*p) { 4947 int size = strlen(p); 4948 4949 if (size > 11) 4950 size = 11; 4951 data[0] = 0; 4952 data[1] = 0; 4953 data[2] = 0xf0; /* OEM event without timestamp. */ 4954 data[3] = intf->addrinfo[0].address; 4955 data[4] = j++; /* sequence # */ 4956 /* 4957 * Always give 11 bytes, so strncpy will fill 4958 * it with zeroes for me. 4959 */ 4960 strncpy(data+5, p, 11); 4961 p += size; 4962 4963 ipmi_panic_request_and_wait(intf, &addr, &msg); 4964 } 4965 } 4966 4967 static int has_panicked; 4968 4969 static int panic_event(struct notifier_block *this, 4970 unsigned long event, 4971 void *ptr) 4972 { 4973 struct ipmi_smi *intf; 4974 struct ipmi_user *user; 4975 4976 if (has_panicked) 4977 return NOTIFY_DONE; 4978 has_panicked = 1; 4979 4980 /* For every registered interface, set it to run to completion. */ 4981 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) { 4982 if (!intf->handlers || intf->intf_num == -1) 4983 /* Interface is not ready. */ 4984 continue; 4985 4986 if (!intf->handlers->poll) 4987 continue; 4988 4989 /* 4990 * If we were interrupted while locking xmit_msgs_lock or 4991 * waiting_rcv_msgs_lock, the corresponding list may be 4992 * corrupted. In this case, drop items on the list for 4993 * the safety. 4994 */ 4995 if (!spin_trylock(&intf->xmit_msgs_lock)) { 4996 INIT_LIST_HEAD(&intf->xmit_msgs); 4997 INIT_LIST_HEAD(&intf->hp_xmit_msgs); 4998 } else 4999 spin_unlock(&intf->xmit_msgs_lock); 5000 5001 if (!spin_trylock(&intf->waiting_rcv_msgs_lock)) 5002 INIT_LIST_HEAD(&intf->waiting_rcv_msgs); 5003 else 5004 spin_unlock(&intf->waiting_rcv_msgs_lock); 5005 5006 intf->run_to_completion = 1; 5007 if (intf->handlers->set_run_to_completion) 5008 intf->handlers->set_run_to_completion(intf->send_info, 5009 1); 5010 5011 list_for_each_entry_rcu(user, &intf->users, link) { 5012 if (user->handler->ipmi_panic_handler) 5013 user->handler->ipmi_panic_handler( 5014 user->handler_data); 5015 } 5016 5017 send_panic_events(intf, ptr); 5018 } 5019 5020 return NOTIFY_DONE; 5021 } 5022 5023 static struct notifier_block panic_block = { 5024 .notifier_call = panic_event, 5025 .next = NULL, 5026 .priority = 200 /* priority: INT_MAX >= x >= 0 */ 5027 }; 5028 5029 static int ipmi_init_msghandler(void) 5030 { 5031 int rv; 5032 5033 if (initialized) 5034 return 0; 5035 5036 rv = driver_register(&ipmidriver.driver); 5037 if (rv) { 5038 pr_err(PFX "Could not register IPMI driver\n"); 5039 return rv; 5040 } 5041 5042 pr_info("ipmi message handler version " IPMI_DRIVER_VERSION "\n"); 5043 5044 timer_setup(&ipmi_timer, ipmi_timeout, 0); 5045 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES); 5046 5047 atomic_notifier_chain_register(&panic_notifier_list, &panic_block); 5048 5049 initialized = 1; 5050 5051 return 0; 5052 } 5053 5054 static int __init ipmi_init_msghandler_mod(void) 5055 { 5056 ipmi_init_msghandler(); 5057 return 0; 5058 } 5059 5060 static void __exit cleanup_ipmi(void) 5061 { 5062 int count; 5063 5064 if (!initialized) 5065 return; 5066 5067 atomic_notifier_chain_unregister(&panic_notifier_list, &panic_block); 5068 5069 /* 5070 * This can't be called if any interfaces exist, so no worry 5071 * about shutting down the interfaces. 5072 */ 5073 5074 /* 5075 * Tell the timer to stop, then wait for it to stop. This 5076 * avoids problems with race conditions removing the timer 5077 * here. 5078 */ 5079 atomic_inc(&stop_operation); 5080 del_timer_sync(&ipmi_timer); 5081 5082 driver_unregister(&ipmidriver.driver); 5083 5084 initialized = 0; 5085 5086 /* Check for buffer leaks. */ 5087 count = atomic_read(&smi_msg_inuse_count); 5088 if (count != 0) 5089 pr_warn(PFX "SMI message count %d at exit\n", count); 5090 count = atomic_read(&recv_msg_inuse_count); 5091 if (count != 0) 5092 pr_warn(PFX "recv message count %d at exit\n", count); 5093 } 5094 module_exit(cleanup_ipmi); 5095 5096 module_init(ipmi_init_msghandler_mod); 5097 MODULE_LICENSE("GPL"); 5098 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>"); 5099 MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI" 5100 " interface."); 5101 MODULE_VERSION(IPMI_DRIVER_VERSION); 5102 MODULE_SOFTDEP("post: ipmi_devintf"); 5103