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