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