1 /* 2 * ipmi_ssif.c 3 * 4 * The interface to the IPMI driver for SMBus access to a SMBus 5 * compliant device. Called SSIF by the IPMI spec. 6 * 7 * Author: Intel Corporation 8 * Todd Davis <todd.c.davis@intel.com> 9 * 10 * Rewritten by Corey Minyard <minyard@acm.org> to support the 11 * non-blocking I2C interface, add support for multi-part 12 * transactions, add PEC support, and general clenaup. 13 * 14 * Copyright 2003 Intel Corporation 15 * Copyright 2005 MontaVista Software 16 * 17 * This program is free software; you can redistribute it and/or modify it 18 * under the terms of the GNU General Public License as published by the 19 * Free Software Foundation; either version 2 of the License, or (at your 20 * option) any later version. 21 */ 22 23 /* 24 * This file holds the "policy" for the interface to the SSIF state 25 * machine. It does the configuration, handles timers and interrupts, 26 * and drives the real SSIF state machine. 27 */ 28 29 /* 30 * TODO: Figure out how to use SMB alerts. This will require a new 31 * interface into the I2C driver, I believe. 32 */ 33 34 #if defined(MODVERSIONS) 35 #include <linux/modversions.h> 36 #endif 37 38 #include <linux/module.h> 39 #include <linux/moduleparam.h> 40 #include <linux/sched.h> 41 #include <linux/seq_file.h> 42 #include <linux/timer.h> 43 #include <linux/delay.h> 44 #include <linux/errno.h> 45 #include <linux/spinlock.h> 46 #include <linux/slab.h> 47 #include <linux/list.h> 48 #include <linux/i2c.h> 49 #include <linux/ipmi_smi.h> 50 #include <linux/init.h> 51 #include <linux/dmi.h> 52 #include <linux/kthread.h> 53 #include <linux/acpi.h> 54 #include <linux/ctype.h> 55 #include <linux/time64.h> 56 57 #define PFX "ipmi_ssif: " 58 #define DEVICE_NAME "ipmi_ssif" 59 60 #define IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD 0x57 61 62 #define SSIF_IPMI_REQUEST 2 63 #define SSIF_IPMI_MULTI_PART_REQUEST_START 6 64 #define SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE 7 65 #define SSIF_IPMI_RESPONSE 3 66 #define SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE 9 67 68 /* ssif_debug is a bit-field 69 * SSIF_DEBUG_MSG - commands and their responses 70 * SSIF_DEBUG_STATES - message states 71 * SSIF_DEBUG_TIMING - Measure times between events in the driver 72 */ 73 #define SSIF_DEBUG_TIMING 4 74 #define SSIF_DEBUG_STATE 2 75 #define SSIF_DEBUG_MSG 1 76 #define SSIF_NODEBUG 0 77 #define SSIF_DEFAULT_DEBUG (SSIF_NODEBUG) 78 79 /* 80 * Timer values 81 */ 82 #define SSIF_MSG_USEC 20000 /* 20ms between message tries. */ 83 #define SSIF_MSG_PART_USEC 5000 /* 5ms for a message part */ 84 85 /* How many times to we retry sending/receiving the message. */ 86 #define SSIF_SEND_RETRIES 5 87 #define SSIF_RECV_RETRIES 250 88 89 #define SSIF_MSG_MSEC (SSIF_MSG_USEC / 1000) 90 #define SSIF_MSG_JIFFIES ((SSIF_MSG_USEC * 1000) / TICK_NSEC) 91 #define SSIF_MSG_PART_JIFFIES ((SSIF_MSG_PART_USEC * 1000) / TICK_NSEC) 92 93 enum ssif_intf_state { 94 SSIF_NORMAL, 95 SSIF_GETTING_FLAGS, 96 SSIF_GETTING_EVENTS, 97 SSIF_CLEARING_FLAGS, 98 SSIF_GETTING_MESSAGES, 99 /* FIXME - add watchdog stuff. */ 100 }; 101 102 #define SSIF_IDLE(ssif) ((ssif)->ssif_state == SSIF_NORMAL \ 103 && (ssif)->curr_msg == NULL) 104 105 /* 106 * Indexes into stats[] in ssif_info below. 107 */ 108 enum ssif_stat_indexes { 109 /* Number of total messages sent. */ 110 SSIF_STAT_sent_messages = 0, 111 112 /* 113 * Number of message parts sent. Messages may be broken into 114 * parts if they are long. 115 */ 116 SSIF_STAT_sent_messages_parts, 117 118 /* 119 * Number of time a message was retried. 120 */ 121 SSIF_STAT_send_retries, 122 123 /* 124 * Number of times the send of a message failed. 125 */ 126 SSIF_STAT_send_errors, 127 128 /* 129 * Number of message responses received. 130 */ 131 SSIF_STAT_received_messages, 132 133 /* 134 * Number of message fragments received. 135 */ 136 SSIF_STAT_received_message_parts, 137 138 /* 139 * Number of times the receive of a message was retried. 140 */ 141 SSIF_STAT_receive_retries, 142 143 /* 144 * Number of errors receiving messages. 145 */ 146 SSIF_STAT_receive_errors, 147 148 /* 149 * Number of times a flag fetch was requested. 150 */ 151 SSIF_STAT_flag_fetches, 152 153 /* 154 * Number of times the hardware didn't follow the state machine. 155 */ 156 SSIF_STAT_hosed, 157 158 /* 159 * Number of received events. 160 */ 161 SSIF_STAT_events, 162 163 /* Number of asyncronous messages received. */ 164 SSIF_STAT_incoming_messages, 165 166 /* Number of watchdog pretimeouts. */ 167 SSIF_STAT_watchdog_pretimeouts, 168 169 /* Number of alers received. */ 170 SSIF_STAT_alerts, 171 172 /* Always add statistics before this value, it must be last. */ 173 SSIF_NUM_STATS 174 }; 175 176 struct ssif_addr_info { 177 unsigned short addr; 178 struct i2c_board_info binfo; 179 char *adapter_name; 180 int debug; 181 int slave_addr; 182 enum ipmi_addr_src addr_src; 183 union ipmi_smi_info_union addr_info; 184 185 struct mutex clients_mutex; 186 struct list_head clients; 187 188 struct list_head link; 189 }; 190 191 struct ssif_info; 192 193 typedef void (*ssif_i2c_done)(struct ssif_info *ssif_info, int result, 194 unsigned char *data, unsigned int len); 195 196 struct ssif_info { 197 ipmi_smi_t intf; 198 int intf_num; 199 spinlock_t lock; 200 struct ipmi_smi_msg *waiting_msg; 201 struct ipmi_smi_msg *curr_msg; 202 enum ssif_intf_state ssif_state; 203 unsigned long ssif_debug; 204 205 struct ipmi_smi_handlers handlers; 206 207 enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */ 208 union ipmi_smi_info_union addr_info; 209 210 /* 211 * Flags from the last GET_MSG_FLAGS command, used when an ATTN 212 * is set to hold the flags until we are done handling everything 213 * from the flags. 214 */ 215 #define RECEIVE_MSG_AVAIL 0x01 216 #define EVENT_MSG_BUFFER_FULL 0x02 217 #define WDT_PRE_TIMEOUT_INT 0x08 218 unsigned char msg_flags; 219 220 u8 global_enables; 221 bool has_event_buffer; 222 bool supports_alert; 223 224 /* 225 * Used to tell what we should do with alerts. If we are 226 * waiting on a response, read the data immediately. 227 */ 228 bool got_alert; 229 bool waiting_alert; 230 231 /* 232 * If set to true, this will request events the next time the 233 * state machine is idle. 234 */ 235 bool req_events; 236 237 /* 238 * If set to true, this will request flags the next time the 239 * state machine is idle. 240 */ 241 bool req_flags; 242 243 /* 244 * Used to perform timer operations when run-to-completion 245 * mode is on. This is a countdown timer. 246 */ 247 int rtc_us_timer; 248 249 /* Used for sending/receiving data. +1 for the length. */ 250 unsigned char data[IPMI_MAX_MSG_LENGTH + 1]; 251 unsigned int data_len; 252 253 /* Temp receive buffer, gets copied into data. */ 254 unsigned char recv[I2C_SMBUS_BLOCK_MAX]; 255 256 struct i2c_client *client; 257 ssif_i2c_done done_handler; 258 259 /* Thread interface handling */ 260 struct task_struct *thread; 261 struct completion wake_thread; 262 bool stopping; 263 int i2c_read_write; 264 int i2c_command; 265 unsigned char *i2c_data; 266 unsigned int i2c_size; 267 268 /* From the device id response. */ 269 struct ipmi_device_id device_id; 270 271 struct timer_list retry_timer; 272 int retries_left; 273 274 /* Info from SSIF cmd */ 275 unsigned char max_xmit_msg_size; 276 unsigned char max_recv_msg_size; 277 unsigned int multi_support; 278 int supports_pec; 279 280 #define SSIF_NO_MULTI 0 281 #define SSIF_MULTI_2_PART 1 282 #define SSIF_MULTI_n_PART 2 283 unsigned char *multi_data; 284 unsigned int multi_len; 285 unsigned int multi_pos; 286 287 atomic_t stats[SSIF_NUM_STATS]; 288 }; 289 290 #define ssif_inc_stat(ssif, stat) \ 291 atomic_inc(&(ssif)->stats[SSIF_STAT_ ## stat]) 292 #define ssif_get_stat(ssif, stat) \ 293 ((unsigned int) atomic_read(&(ssif)->stats[SSIF_STAT_ ## stat])) 294 295 static bool initialized; 296 297 static atomic_t next_intf = ATOMIC_INIT(0); 298 299 static void return_hosed_msg(struct ssif_info *ssif_info, 300 struct ipmi_smi_msg *msg); 301 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags); 302 static int start_send(struct ssif_info *ssif_info, 303 unsigned char *data, 304 unsigned int len); 305 306 static unsigned long *ipmi_ssif_lock_cond(struct ssif_info *ssif_info, 307 unsigned long *flags) 308 { 309 spin_lock_irqsave(&ssif_info->lock, *flags); 310 return flags; 311 } 312 313 static void ipmi_ssif_unlock_cond(struct ssif_info *ssif_info, 314 unsigned long *flags) 315 { 316 spin_unlock_irqrestore(&ssif_info->lock, *flags); 317 } 318 319 static void deliver_recv_msg(struct ssif_info *ssif_info, 320 struct ipmi_smi_msg *msg) 321 { 322 ipmi_smi_t intf = ssif_info->intf; 323 324 if (!intf) { 325 ipmi_free_smi_msg(msg); 326 } else if (msg->rsp_size < 0) { 327 return_hosed_msg(ssif_info, msg); 328 pr_err(PFX 329 "Malformed message in deliver_recv_msg: rsp_size = %d\n", 330 msg->rsp_size); 331 } else { 332 ipmi_smi_msg_received(intf, msg); 333 } 334 } 335 336 static void return_hosed_msg(struct ssif_info *ssif_info, 337 struct ipmi_smi_msg *msg) 338 { 339 ssif_inc_stat(ssif_info, hosed); 340 341 /* Make it a response */ 342 msg->rsp[0] = msg->data[0] | 4; 343 msg->rsp[1] = msg->data[1]; 344 msg->rsp[2] = 0xFF; /* Unknown error. */ 345 msg->rsp_size = 3; 346 347 deliver_recv_msg(ssif_info, msg); 348 } 349 350 /* 351 * Must be called with the message lock held. This will release the 352 * message lock. Note that the caller will check SSIF_IDLE and start a 353 * new operation, so there is no need to check for new messages to 354 * start in here. 355 */ 356 static void start_clear_flags(struct ssif_info *ssif_info, unsigned long *flags) 357 { 358 unsigned char msg[3]; 359 360 ssif_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT; 361 ssif_info->ssif_state = SSIF_CLEARING_FLAGS; 362 ipmi_ssif_unlock_cond(ssif_info, flags); 363 364 /* Make sure the watchdog pre-timeout flag is not set at startup. */ 365 msg[0] = (IPMI_NETFN_APP_REQUEST << 2); 366 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD; 367 msg[2] = WDT_PRE_TIMEOUT_INT; 368 369 if (start_send(ssif_info, msg, 3) != 0) { 370 /* Error, just go to normal state. */ 371 ssif_info->ssif_state = SSIF_NORMAL; 372 } 373 } 374 375 static void start_flag_fetch(struct ssif_info *ssif_info, unsigned long *flags) 376 { 377 unsigned char mb[2]; 378 379 ssif_info->req_flags = false; 380 ssif_info->ssif_state = SSIF_GETTING_FLAGS; 381 ipmi_ssif_unlock_cond(ssif_info, flags); 382 383 mb[0] = (IPMI_NETFN_APP_REQUEST << 2); 384 mb[1] = IPMI_GET_MSG_FLAGS_CMD; 385 if (start_send(ssif_info, mb, 2) != 0) 386 ssif_info->ssif_state = SSIF_NORMAL; 387 } 388 389 static void check_start_send(struct ssif_info *ssif_info, unsigned long *flags, 390 struct ipmi_smi_msg *msg) 391 { 392 if (start_send(ssif_info, msg->data, msg->data_size) != 0) { 393 unsigned long oflags; 394 395 flags = ipmi_ssif_lock_cond(ssif_info, &oflags); 396 ssif_info->curr_msg = NULL; 397 ssif_info->ssif_state = SSIF_NORMAL; 398 ipmi_ssif_unlock_cond(ssif_info, flags); 399 ipmi_free_smi_msg(msg); 400 } 401 } 402 403 static void start_event_fetch(struct ssif_info *ssif_info, unsigned long *flags) 404 { 405 struct ipmi_smi_msg *msg; 406 407 ssif_info->req_events = false; 408 409 msg = ipmi_alloc_smi_msg(); 410 if (!msg) { 411 ssif_info->ssif_state = SSIF_NORMAL; 412 return; 413 } 414 415 ssif_info->curr_msg = msg; 416 ssif_info->ssif_state = SSIF_GETTING_EVENTS; 417 ipmi_ssif_unlock_cond(ssif_info, flags); 418 419 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2); 420 msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD; 421 msg->data_size = 2; 422 423 check_start_send(ssif_info, flags, msg); 424 } 425 426 static void start_recv_msg_fetch(struct ssif_info *ssif_info, 427 unsigned long *flags) 428 { 429 struct ipmi_smi_msg *msg; 430 431 msg = ipmi_alloc_smi_msg(); 432 if (!msg) { 433 ssif_info->ssif_state = SSIF_NORMAL; 434 return; 435 } 436 437 ssif_info->curr_msg = msg; 438 ssif_info->ssif_state = SSIF_GETTING_MESSAGES; 439 ipmi_ssif_unlock_cond(ssif_info, flags); 440 441 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2); 442 msg->data[1] = IPMI_GET_MSG_CMD; 443 msg->data_size = 2; 444 445 check_start_send(ssif_info, flags, msg); 446 } 447 448 /* 449 * Must be called with the message lock held. This will release the 450 * message lock. Note that the caller will check SSIF_IDLE and start a 451 * new operation, so there is no need to check for new messages to 452 * start in here. 453 */ 454 static void handle_flags(struct ssif_info *ssif_info, unsigned long *flags) 455 { 456 if (ssif_info->msg_flags & WDT_PRE_TIMEOUT_INT) { 457 ipmi_smi_t intf = ssif_info->intf; 458 /* Watchdog pre-timeout */ 459 ssif_inc_stat(ssif_info, watchdog_pretimeouts); 460 start_clear_flags(ssif_info, flags); 461 if (intf) 462 ipmi_smi_watchdog_pretimeout(intf); 463 } else if (ssif_info->msg_flags & RECEIVE_MSG_AVAIL) 464 /* Messages available. */ 465 start_recv_msg_fetch(ssif_info, flags); 466 else if (ssif_info->msg_flags & EVENT_MSG_BUFFER_FULL) 467 /* Events available. */ 468 start_event_fetch(ssif_info, flags); 469 else { 470 ssif_info->ssif_state = SSIF_NORMAL; 471 ipmi_ssif_unlock_cond(ssif_info, flags); 472 } 473 } 474 475 static int ipmi_ssif_thread(void *data) 476 { 477 struct ssif_info *ssif_info = data; 478 479 while (!kthread_should_stop()) { 480 int result; 481 482 /* Wait for something to do */ 483 result = wait_for_completion_interruptible( 484 &ssif_info->wake_thread); 485 if (ssif_info->stopping) 486 break; 487 if (result == -ERESTARTSYS) 488 continue; 489 init_completion(&ssif_info->wake_thread); 490 491 if (ssif_info->i2c_read_write == I2C_SMBUS_WRITE) { 492 result = i2c_smbus_write_block_data( 493 ssif_info->client, ssif_info->i2c_command, 494 ssif_info->i2c_data[0], 495 ssif_info->i2c_data + 1); 496 ssif_info->done_handler(ssif_info, result, NULL, 0); 497 } else { 498 result = i2c_smbus_read_block_data( 499 ssif_info->client, ssif_info->i2c_command, 500 ssif_info->i2c_data); 501 if (result < 0) 502 ssif_info->done_handler(ssif_info, result, 503 NULL, 0); 504 else 505 ssif_info->done_handler(ssif_info, 0, 506 ssif_info->i2c_data, 507 result); 508 } 509 } 510 511 return 0; 512 } 513 514 static int ssif_i2c_send(struct ssif_info *ssif_info, 515 ssif_i2c_done handler, 516 int read_write, int command, 517 unsigned char *data, unsigned int size) 518 { 519 ssif_info->done_handler = handler; 520 521 ssif_info->i2c_read_write = read_write; 522 ssif_info->i2c_command = command; 523 ssif_info->i2c_data = data; 524 ssif_info->i2c_size = size; 525 complete(&ssif_info->wake_thread); 526 return 0; 527 } 528 529 530 static void msg_done_handler(struct ssif_info *ssif_info, int result, 531 unsigned char *data, unsigned int len); 532 533 static void start_get(struct ssif_info *ssif_info) 534 { 535 int rv; 536 537 ssif_info->rtc_us_timer = 0; 538 ssif_info->multi_pos = 0; 539 540 rv = ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ, 541 SSIF_IPMI_RESPONSE, 542 ssif_info->recv, I2C_SMBUS_BLOCK_DATA); 543 if (rv < 0) { 544 /* request failed, just return the error. */ 545 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG) 546 pr_info("Error from i2c_non_blocking_op(5)\n"); 547 548 msg_done_handler(ssif_info, -EIO, NULL, 0); 549 } 550 } 551 552 static void retry_timeout(unsigned long data) 553 { 554 struct ssif_info *ssif_info = (void *) data; 555 unsigned long oflags, *flags; 556 bool waiting; 557 558 if (ssif_info->stopping) 559 return; 560 561 flags = ipmi_ssif_lock_cond(ssif_info, &oflags); 562 waiting = ssif_info->waiting_alert; 563 ssif_info->waiting_alert = false; 564 ipmi_ssif_unlock_cond(ssif_info, flags); 565 566 if (waiting) 567 start_get(ssif_info); 568 } 569 570 571 static void ssif_alert(struct i2c_client *client, enum i2c_alert_protocol type, 572 unsigned int data) 573 { 574 struct ssif_info *ssif_info = i2c_get_clientdata(client); 575 unsigned long oflags, *flags; 576 bool do_get = false; 577 578 if (type != I2C_PROTOCOL_SMBUS_ALERT) 579 return; 580 581 ssif_inc_stat(ssif_info, alerts); 582 583 flags = ipmi_ssif_lock_cond(ssif_info, &oflags); 584 if (ssif_info->waiting_alert) { 585 ssif_info->waiting_alert = false; 586 del_timer(&ssif_info->retry_timer); 587 do_get = true; 588 } else if (ssif_info->curr_msg) { 589 ssif_info->got_alert = true; 590 } 591 ipmi_ssif_unlock_cond(ssif_info, flags); 592 if (do_get) 593 start_get(ssif_info); 594 } 595 596 static int start_resend(struct ssif_info *ssif_info); 597 598 static void msg_done_handler(struct ssif_info *ssif_info, int result, 599 unsigned char *data, unsigned int len) 600 { 601 struct ipmi_smi_msg *msg; 602 unsigned long oflags, *flags; 603 int rv; 604 605 /* 606 * We are single-threaded here, so no need for a lock until we 607 * start messing with driver states or the queues. 608 */ 609 610 if (result < 0) { 611 ssif_info->retries_left--; 612 if (ssif_info->retries_left > 0) { 613 ssif_inc_stat(ssif_info, receive_retries); 614 615 flags = ipmi_ssif_lock_cond(ssif_info, &oflags); 616 ssif_info->waiting_alert = true; 617 ssif_info->rtc_us_timer = SSIF_MSG_USEC; 618 mod_timer(&ssif_info->retry_timer, 619 jiffies + SSIF_MSG_JIFFIES); 620 ipmi_ssif_unlock_cond(ssif_info, flags); 621 return; 622 } 623 624 ssif_inc_stat(ssif_info, receive_errors); 625 626 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG) 627 pr_info("Error in msg_done_handler: %d\n", result); 628 len = 0; 629 goto continue_op; 630 } 631 632 if ((len > 1) && (ssif_info->multi_pos == 0) 633 && (data[0] == 0x00) && (data[1] == 0x01)) { 634 /* Start of multi-part read. Start the next transaction. */ 635 int i; 636 637 ssif_inc_stat(ssif_info, received_message_parts); 638 639 /* Remove the multi-part read marker. */ 640 len -= 2; 641 for (i = 0; i < len; i++) 642 ssif_info->data[i] = data[i+2]; 643 ssif_info->multi_len = len; 644 ssif_info->multi_pos = 1; 645 646 rv = ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ, 647 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE, 648 ssif_info->recv, I2C_SMBUS_BLOCK_DATA); 649 if (rv < 0) { 650 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG) 651 pr_info("Error from i2c_non_blocking_op(1)\n"); 652 653 result = -EIO; 654 } else 655 return; 656 } else if (ssif_info->multi_pos) { 657 /* Middle of multi-part read. Start the next transaction. */ 658 int i; 659 unsigned char blocknum; 660 661 if (len == 0) { 662 result = -EIO; 663 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG) 664 pr_info(PFX "Middle message with no data\n"); 665 666 goto continue_op; 667 } 668 669 blocknum = data[0]; 670 671 if (ssif_info->multi_len + len - 1 > IPMI_MAX_MSG_LENGTH) { 672 /* Received message too big, abort the operation. */ 673 result = -E2BIG; 674 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG) 675 pr_info("Received message too big\n"); 676 677 goto continue_op; 678 } 679 680 /* Remove the blocknum from the data. */ 681 len--; 682 for (i = 0; i < len; i++) 683 ssif_info->data[i + ssif_info->multi_len] = data[i + 1]; 684 ssif_info->multi_len += len; 685 if (blocknum == 0xff) { 686 /* End of read */ 687 len = ssif_info->multi_len; 688 data = ssif_info->data; 689 } else if (blocknum + 1 != ssif_info->multi_pos) { 690 /* 691 * Out of sequence block, just abort. Block 692 * numbers start at zero for the second block, 693 * but multi_pos starts at one, so the +1. 694 */ 695 result = -EIO; 696 } else { 697 ssif_inc_stat(ssif_info, received_message_parts); 698 699 ssif_info->multi_pos++; 700 701 rv = ssif_i2c_send(ssif_info, msg_done_handler, 702 I2C_SMBUS_READ, 703 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE, 704 ssif_info->recv, 705 I2C_SMBUS_BLOCK_DATA); 706 if (rv < 0) { 707 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG) 708 pr_info(PFX 709 "Error from ssif_i2c_send\n"); 710 711 result = -EIO; 712 } else 713 return; 714 } 715 } 716 717 if (result < 0) { 718 ssif_inc_stat(ssif_info, receive_errors); 719 } else { 720 ssif_inc_stat(ssif_info, received_messages); 721 ssif_inc_stat(ssif_info, received_message_parts); 722 } 723 724 725 continue_op: 726 if (ssif_info->ssif_debug & SSIF_DEBUG_STATE) 727 pr_info(PFX "DONE 1: state = %d, result=%d.\n", 728 ssif_info->ssif_state, result); 729 730 flags = ipmi_ssif_lock_cond(ssif_info, &oflags); 731 msg = ssif_info->curr_msg; 732 if (msg) { 733 msg->rsp_size = len; 734 if (msg->rsp_size > IPMI_MAX_MSG_LENGTH) 735 msg->rsp_size = IPMI_MAX_MSG_LENGTH; 736 memcpy(msg->rsp, data, msg->rsp_size); 737 ssif_info->curr_msg = NULL; 738 } 739 740 switch (ssif_info->ssif_state) { 741 case SSIF_NORMAL: 742 ipmi_ssif_unlock_cond(ssif_info, flags); 743 if (!msg) 744 break; 745 746 if (result < 0) 747 return_hosed_msg(ssif_info, msg); 748 else 749 deliver_recv_msg(ssif_info, msg); 750 break; 751 752 case SSIF_GETTING_FLAGS: 753 /* We got the flags from the SSIF, now handle them. */ 754 if ((result < 0) || (len < 4) || (data[2] != 0)) { 755 /* 756 * Error fetching flags, or invalid length, 757 * just give up for now. 758 */ 759 ssif_info->ssif_state = SSIF_NORMAL; 760 ipmi_ssif_unlock_cond(ssif_info, flags); 761 pr_warn(PFX "Error getting flags: %d %d, %x\n", 762 result, len, data[2]); 763 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 764 || data[1] != IPMI_GET_MSG_FLAGS_CMD) { 765 pr_warn(PFX "Invalid response getting flags: %x %x\n", 766 data[0], data[1]); 767 } else { 768 ssif_inc_stat(ssif_info, flag_fetches); 769 ssif_info->msg_flags = data[3]; 770 handle_flags(ssif_info, flags); 771 } 772 break; 773 774 case SSIF_CLEARING_FLAGS: 775 /* We cleared the flags. */ 776 if ((result < 0) || (len < 3) || (data[2] != 0)) { 777 /* Error clearing flags */ 778 pr_warn(PFX "Error clearing flags: %d %d, %x\n", 779 result, len, data[2]); 780 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 781 || data[1] != IPMI_CLEAR_MSG_FLAGS_CMD) { 782 pr_warn(PFX "Invalid response clearing flags: %x %x\n", 783 data[0], data[1]); 784 } 785 ssif_info->ssif_state = SSIF_NORMAL; 786 ipmi_ssif_unlock_cond(ssif_info, flags); 787 break; 788 789 case SSIF_GETTING_EVENTS: 790 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) { 791 /* Error getting event, probably done. */ 792 msg->done(msg); 793 794 /* Take off the event flag. */ 795 ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL; 796 handle_flags(ssif_info, flags); 797 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 798 || msg->rsp[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD) { 799 pr_warn(PFX "Invalid response getting events: %x %x\n", 800 msg->rsp[0], msg->rsp[1]); 801 msg->done(msg); 802 /* Take off the event flag. */ 803 ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL; 804 handle_flags(ssif_info, flags); 805 } else { 806 handle_flags(ssif_info, flags); 807 ssif_inc_stat(ssif_info, events); 808 deliver_recv_msg(ssif_info, msg); 809 } 810 break; 811 812 case SSIF_GETTING_MESSAGES: 813 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) { 814 /* Error getting event, probably done. */ 815 msg->done(msg); 816 817 /* Take off the msg flag. */ 818 ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL; 819 handle_flags(ssif_info, flags); 820 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 821 || msg->rsp[1] != IPMI_GET_MSG_CMD) { 822 pr_warn(PFX "Invalid response clearing flags: %x %x\n", 823 msg->rsp[0], msg->rsp[1]); 824 msg->done(msg); 825 826 /* Take off the msg flag. */ 827 ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL; 828 handle_flags(ssif_info, flags); 829 } else { 830 ssif_inc_stat(ssif_info, incoming_messages); 831 handle_flags(ssif_info, flags); 832 deliver_recv_msg(ssif_info, msg); 833 } 834 break; 835 } 836 837 flags = ipmi_ssif_lock_cond(ssif_info, &oflags); 838 if (SSIF_IDLE(ssif_info) && !ssif_info->stopping) { 839 if (ssif_info->req_events) 840 start_event_fetch(ssif_info, flags); 841 else if (ssif_info->req_flags) 842 start_flag_fetch(ssif_info, flags); 843 else 844 start_next_msg(ssif_info, flags); 845 } else 846 ipmi_ssif_unlock_cond(ssif_info, flags); 847 848 if (ssif_info->ssif_debug & SSIF_DEBUG_STATE) 849 pr_info(PFX "DONE 2: state = %d.\n", ssif_info->ssif_state); 850 } 851 852 static void msg_written_handler(struct ssif_info *ssif_info, int result, 853 unsigned char *data, unsigned int len) 854 { 855 int rv; 856 857 /* We are single-threaded here, so no need for a lock. */ 858 if (result < 0) { 859 ssif_info->retries_left--; 860 if (ssif_info->retries_left > 0) { 861 if (!start_resend(ssif_info)) { 862 ssif_inc_stat(ssif_info, send_retries); 863 return; 864 } 865 /* request failed, just return the error. */ 866 ssif_inc_stat(ssif_info, send_errors); 867 868 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG) 869 pr_info(PFX 870 "Out of retries in msg_written_handler\n"); 871 msg_done_handler(ssif_info, -EIO, NULL, 0); 872 return; 873 } 874 875 ssif_inc_stat(ssif_info, send_errors); 876 877 /* 878 * Got an error on transmit, let the done routine 879 * handle it. 880 */ 881 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG) 882 pr_info("Error in msg_written_handler: %d\n", result); 883 884 msg_done_handler(ssif_info, result, NULL, 0); 885 return; 886 } 887 888 if (ssif_info->multi_data) { 889 /* 890 * In the middle of a multi-data write. See the comment 891 * in the SSIF_MULTI_n_PART case in the probe function 892 * for details on the intricacies of this. 893 */ 894 int left; 895 896 ssif_inc_stat(ssif_info, sent_messages_parts); 897 898 left = ssif_info->multi_len - ssif_info->multi_pos; 899 if (left > 32) 900 left = 32; 901 /* Length byte. */ 902 ssif_info->multi_data[ssif_info->multi_pos] = left; 903 ssif_info->multi_pos += left; 904 if (left < 32) 905 /* 906 * Write is finished. Note that we must end 907 * with a write of less than 32 bytes to 908 * complete the transaction, even if it is 909 * zero bytes. 910 */ 911 ssif_info->multi_data = NULL; 912 913 rv = ssif_i2c_send(ssif_info, msg_written_handler, 914 I2C_SMBUS_WRITE, 915 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE, 916 ssif_info->multi_data + ssif_info->multi_pos, 917 I2C_SMBUS_BLOCK_DATA); 918 if (rv < 0) { 919 /* request failed, just return the error. */ 920 ssif_inc_stat(ssif_info, send_errors); 921 922 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG) 923 pr_info("Error from i2c_non_blocking_op(3)\n"); 924 msg_done_handler(ssif_info, -EIO, NULL, 0); 925 } 926 } else { 927 /* Ready to request the result. */ 928 unsigned long oflags, *flags; 929 930 ssif_inc_stat(ssif_info, sent_messages); 931 ssif_inc_stat(ssif_info, sent_messages_parts); 932 933 flags = ipmi_ssif_lock_cond(ssif_info, &oflags); 934 if (ssif_info->got_alert) { 935 /* The result is already ready, just start it. */ 936 ssif_info->got_alert = false; 937 ipmi_ssif_unlock_cond(ssif_info, flags); 938 start_get(ssif_info); 939 } else { 940 /* Wait a jiffie then request the next message */ 941 ssif_info->waiting_alert = true; 942 ssif_info->retries_left = SSIF_RECV_RETRIES; 943 ssif_info->rtc_us_timer = SSIF_MSG_PART_USEC; 944 mod_timer(&ssif_info->retry_timer, 945 jiffies + SSIF_MSG_PART_JIFFIES); 946 ipmi_ssif_unlock_cond(ssif_info, flags); 947 } 948 } 949 } 950 951 static int start_resend(struct ssif_info *ssif_info) 952 { 953 int rv; 954 int command; 955 956 ssif_info->got_alert = false; 957 958 if (ssif_info->data_len > 32) { 959 command = SSIF_IPMI_MULTI_PART_REQUEST_START; 960 ssif_info->multi_data = ssif_info->data; 961 ssif_info->multi_len = ssif_info->data_len; 962 /* 963 * Subtle thing, this is 32, not 33, because we will 964 * overwrite the thing at position 32 (which was just 965 * transmitted) with the new length. 966 */ 967 ssif_info->multi_pos = 32; 968 ssif_info->data[0] = 32; 969 } else { 970 ssif_info->multi_data = NULL; 971 command = SSIF_IPMI_REQUEST; 972 ssif_info->data[0] = ssif_info->data_len; 973 } 974 975 rv = ssif_i2c_send(ssif_info, msg_written_handler, I2C_SMBUS_WRITE, 976 command, ssif_info->data, I2C_SMBUS_BLOCK_DATA); 977 if (rv && (ssif_info->ssif_debug & SSIF_DEBUG_MSG)) 978 pr_info("Error from i2c_non_blocking_op(4)\n"); 979 return rv; 980 } 981 982 static int start_send(struct ssif_info *ssif_info, 983 unsigned char *data, 984 unsigned int len) 985 { 986 if (len > IPMI_MAX_MSG_LENGTH) 987 return -E2BIG; 988 if (len > ssif_info->max_xmit_msg_size) 989 return -E2BIG; 990 991 ssif_info->retries_left = SSIF_SEND_RETRIES; 992 memcpy(ssif_info->data + 1, data, len); 993 ssif_info->data_len = len; 994 return start_resend(ssif_info); 995 } 996 997 /* Must be called with the message lock held. */ 998 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags) 999 { 1000 struct ipmi_smi_msg *msg; 1001 unsigned long oflags; 1002 1003 restart: 1004 if (!SSIF_IDLE(ssif_info)) { 1005 ipmi_ssif_unlock_cond(ssif_info, flags); 1006 return; 1007 } 1008 1009 if (!ssif_info->waiting_msg) { 1010 ssif_info->curr_msg = NULL; 1011 ipmi_ssif_unlock_cond(ssif_info, flags); 1012 } else { 1013 int rv; 1014 1015 ssif_info->curr_msg = ssif_info->waiting_msg; 1016 ssif_info->waiting_msg = NULL; 1017 ipmi_ssif_unlock_cond(ssif_info, flags); 1018 rv = start_send(ssif_info, 1019 ssif_info->curr_msg->data, 1020 ssif_info->curr_msg->data_size); 1021 if (rv) { 1022 msg = ssif_info->curr_msg; 1023 ssif_info->curr_msg = NULL; 1024 return_hosed_msg(ssif_info, msg); 1025 flags = ipmi_ssif_lock_cond(ssif_info, &oflags); 1026 goto restart; 1027 } 1028 } 1029 } 1030 1031 static void sender(void *send_info, 1032 struct ipmi_smi_msg *msg) 1033 { 1034 struct ssif_info *ssif_info = (struct ssif_info *) send_info; 1035 unsigned long oflags, *flags; 1036 1037 BUG_ON(ssif_info->waiting_msg); 1038 ssif_info->waiting_msg = msg; 1039 1040 flags = ipmi_ssif_lock_cond(ssif_info, &oflags); 1041 start_next_msg(ssif_info, flags); 1042 1043 if (ssif_info->ssif_debug & SSIF_DEBUG_TIMING) { 1044 struct timespec64 t; 1045 1046 ktime_get_real_ts64(&t); 1047 pr_info("**Enqueue %02x %02x: %lld.%6.6ld\n", 1048 msg->data[0], msg->data[1], 1049 (long long) t.tv_sec, (long) t.tv_nsec / NSEC_PER_USEC); 1050 } 1051 } 1052 1053 static int get_smi_info(void *send_info, struct ipmi_smi_info *data) 1054 { 1055 struct ssif_info *ssif_info = send_info; 1056 1057 data->addr_src = ssif_info->addr_source; 1058 data->dev = &ssif_info->client->dev; 1059 data->addr_info = ssif_info->addr_info; 1060 get_device(data->dev); 1061 1062 return 0; 1063 } 1064 1065 /* 1066 * Instead of having our own timer to periodically check the message 1067 * flags, we let the message handler drive us. 1068 */ 1069 static void request_events(void *send_info) 1070 { 1071 struct ssif_info *ssif_info = (struct ssif_info *) send_info; 1072 unsigned long oflags, *flags; 1073 1074 if (!ssif_info->has_event_buffer) 1075 return; 1076 1077 flags = ipmi_ssif_lock_cond(ssif_info, &oflags); 1078 /* 1079 * Request flags first, not events, because the lower layer 1080 * doesn't have a way to send an attention. But make sure 1081 * event checking still happens. 1082 */ 1083 ssif_info->req_events = true; 1084 if (SSIF_IDLE(ssif_info)) 1085 start_flag_fetch(ssif_info, flags); 1086 else { 1087 ssif_info->req_flags = true; 1088 ipmi_ssif_unlock_cond(ssif_info, flags); 1089 } 1090 } 1091 1092 static int inc_usecount(void *send_info) 1093 { 1094 struct ssif_info *ssif_info = send_info; 1095 1096 if (!i2c_get_adapter(ssif_info->client->adapter->nr)) 1097 return -ENODEV; 1098 1099 i2c_use_client(ssif_info->client); 1100 return 0; 1101 } 1102 1103 static void dec_usecount(void *send_info) 1104 { 1105 struct ssif_info *ssif_info = send_info; 1106 1107 i2c_release_client(ssif_info->client); 1108 i2c_put_adapter(ssif_info->client->adapter); 1109 } 1110 1111 static int ssif_start_processing(void *send_info, 1112 ipmi_smi_t intf) 1113 { 1114 struct ssif_info *ssif_info = send_info; 1115 1116 ssif_info->intf = intf; 1117 1118 return 0; 1119 } 1120 1121 #define MAX_SSIF_BMCS 4 1122 1123 static unsigned short addr[MAX_SSIF_BMCS]; 1124 static int num_addrs; 1125 module_param_array(addr, ushort, &num_addrs, 0); 1126 MODULE_PARM_DESC(addr, "The addresses to scan for IPMI BMCs on the SSIFs."); 1127 1128 static char *adapter_name[MAX_SSIF_BMCS]; 1129 static int num_adapter_names; 1130 module_param_array(adapter_name, charp, &num_adapter_names, 0); 1131 MODULE_PARM_DESC(adapter_name, "The string name of the I2C device that has the BMC. By default all devices are scanned."); 1132 1133 static int slave_addrs[MAX_SSIF_BMCS]; 1134 static int num_slave_addrs; 1135 module_param_array(slave_addrs, int, &num_slave_addrs, 0); 1136 MODULE_PARM_DESC(slave_addrs, 1137 "The default IPMB slave address for the controller."); 1138 1139 static bool alerts_broken; 1140 module_param(alerts_broken, bool, 0); 1141 MODULE_PARM_DESC(alerts_broken, "Don't enable alerts for the controller."); 1142 1143 /* 1144 * Bit 0 enables message debugging, bit 1 enables state debugging, and 1145 * bit 2 enables timing debugging. This is an array indexed by 1146 * interface number" 1147 */ 1148 static int dbg[MAX_SSIF_BMCS]; 1149 static int num_dbg; 1150 module_param_array(dbg, int, &num_dbg, 0); 1151 MODULE_PARM_DESC(dbg, "Turn on debugging."); 1152 1153 static bool ssif_dbg_probe; 1154 module_param_named(dbg_probe, ssif_dbg_probe, bool, 0); 1155 MODULE_PARM_DESC(dbg_probe, "Enable debugging of probing of adapters."); 1156 1157 static int use_thread; 1158 module_param(use_thread, int, 0); 1159 MODULE_PARM_DESC(use_thread, "Use the thread interface."); 1160 1161 static bool ssif_tryacpi = true; 1162 module_param_named(tryacpi, ssif_tryacpi, bool, 0); 1163 MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the default scan of the interfaces identified via ACPI"); 1164 1165 static bool ssif_trydmi = true; 1166 module_param_named(trydmi, ssif_trydmi, bool, 0); 1167 MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the default scan of the interfaces identified via DMI (SMBIOS)"); 1168 1169 static DEFINE_MUTEX(ssif_infos_mutex); 1170 static LIST_HEAD(ssif_infos); 1171 1172 static int ssif_remove(struct i2c_client *client) 1173 { 1174 struct ssif_info *ssif_info = i2c_get_clientdata(client); 1175 int rv; 1176 1177 if (!ssif_info) 1178 return 0; 1179 1180 /* 1181 * After this point, we won't deliver anything asychronously 1182 * to the message handler. We can unregister ourself. 1183 */ 1184 rv = ipmi_unregister_smi(ssif_info->intf); 1185 if (rv) { 1186 pr_err(PFX "Unable to unregister device: errno=%d\n", rv); 1187 return rv; 1188 } 1189 ssif_info->intf = NULL; 1190 1191 /* make sure the driver is not looking for flags any more. */ 1192 while (ssif_info->ssif_state != SSIF_NORMAL) 1193 schedule_timeout(1); 1194 1195 ssif_info->stopping = true; 1196 del_timer_sync(&ssif_info->retry_timer); 1197 if (ssif_info->thread) { 1198 complete(&ssif_info->wake_thread); 1199 kthread_stop(ssif_info->thread); 1200 } 1201 1202 /* 1203 * No message can be outstanding now, we have removed the 1204 * upper layer and it permitted us to do so. 1205 */ 1206 kfree(ssif_info); 1207 return 0; 1208 } 1209 1210 static int do_cmd(struct i2c_client *client, int len, unsigned char *msg, 1211 int *resp_len, unsigned char *resp) 1212 { 1213 int retry_cnt; 1214 int ret; 1215 1216 retry_cnt = SSIF_SEND_RETRIES; 1217 retry1: 1218 ret = i2c_smbus_write_block_data(client, SSIF_IPMI_REQUEST, len, msg); 1219 if (ret) { 1220 retry_cnt--; 1221 if (retry_cnt > 0) 1222 goto retry1; 1223 return -ENODEV; 1224 } 1225 1226 ret = -ENODEV; 1227 retry_cnt = SSIF_RECV_RETRIES; 1228 while (retry_cnt > 0) { 1229 ret = i2c_smbus_read_block_data(client, SSIF_IPMI_RESPONSE, 1230 resp); 1231 if (ret > 0) 1232 break; 1233 msleep(SSIF_MSG_MSEC); 1234 retry_cnt--; 1235 if (retry_cnt <= 0) 1236 break; 1237 } 1238 1239 if (ret > 0) { 1240 /* Validate that the response is correct. */ 1241 if (ret < 3 || 1242 (resp[0] != (msg[0] | (1 << 2))) || 1243 (resp[1] != msg[1])) 1244 ret = -EINVAL; 1245 else { 1246 *resp_len = ret; 1247 ret = 0; 1248 } 1249 } 1250 1251 return ret; 1252 } 1253 1254 static int ssif_detect(struct i2c_client *client, struct i2c_board_info *info) 1255 { 1256 unsigned char *resp; 1257 unsigned char msg[3]; 1258 int rv; 1259 int len; 1260 1261 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL); 1262 if (!resp) 1263 return -ENOMEM; 1264 1265 /* Do a Get Device ID command, since it is required. */ 1266 msg[0] = IPMI_NETFN_APP_REQUEST << 2; 1267 msg[1] = IPMI_GET_DEVICE_ID_CMD; 1268 rv = do_cmd(client, 2, msg, &len, resp); 1269 if (rv) 1270 rv = -ENODEV; 1271 else 1272 strlcpy(info->type, DEVICE_NAME, I2C_NAME_SIZE); 1273 kfree(resp); 1274 return rv; 1275 } 1276 1277 static int smi_type_proc_show(struct seq_file *m, void *v) 1278 { 1279 seq_puts(m, "ssif\n"); 1280 1281 return 0; 1282 } 1283 1284 static int smi_type_proc_open(struct inode *inode, struct file *file) 1285 { 1286 return single_open(file, smi_type_proc_show, inode->i_private); 1287 } 1288 1289 static const struct file_operations smi_type_proc_ops = { 1290 .open = smi_type_proc_open, 1291 .read = seq_read, 1292 .llseek = seq_lseek, 1293 .release = single_release, 1294 }; 1295 1296 static int smi_stats_proc_show(struct seq_file *m, void *v) 1297 { 1298 struct ssif_info *ssif_info = m->private; 1299 1300 seq_printf(m, "sent_messages: %u\n", 1301 ssif_get_stat(ssif_info, sent_messages)); 1302 seq_printf(m, "sent_messages_parts: %u\n", 1303 ssif_get_stat(ssif_info, sent_messages_parts)); 1304 seq_printf(m, "send_retries: %u\n", 1305 ssif_get_stat(ssif_info, send_retries)); 1306 seq_printf(m, "send_errors: %u\n", 1307 ssif_get_stat(ssif_info, send_errors)); 1308 seq_printf(m, "received_messages: %u\n", 1309 ssif_get_stat(ssif_info, received_messages)); 1310 seq_printf(m, "received_message_parts: %u\n", 1311 ssif_get_stat(ssif_info, received_message_parts)); 1312 seq_printf(m, "receive_retries: %u\n", 1313 ssif_get_stat(ssif_info, receive_retries)); 1314 seq_printf(m, "receive_errors: %u\n", 1315 ssif_get_stat(ssif_info, receive_errors)); 1316 seq_printf(m, "flag_fetches: %u\n", 1317 ssif_get_stat(ssif_info, flag_fetches)); 1318 seq_printf(m, "hosed: %u\n", 1319 ssif_get_stat(ssif_info, hosed)); 1320 seq_printf(m, "events: %u\n", 1321 ssif_get_stat(ssif_info, events)); 1322 seq_printf(m, "watchdog_pretimeouts: %u\n", 1323 ssif_get_stat(ssif_info, watchdog_pretimeouts)); 1324 seq_printf(m, "alerts: %u\n", 1325 ssif_get_stat(ssif_info, alerts)); 1326 return 0; 1327 } 1328 1329 static int smi_stats_proc_open(struct inode *inode, struct file *file) 1330 { 1331 return single_open(file, smi_stats_proc_show, PDE_DATA(inode)); 1332 } 1333 1334 static const struct file_operations smi_stats_proc_ops = { 1335 .open = smi_stats_proc_open, 1336 .read = seq_read, 1337 .llseek = seq_lseek, 1338 .release = single_release, 1339 }; 1340 1341 static int strcmp_nospace(char *s1, char *s2) 1342 { 1343 while (*s1 && *s2) { 1344 while (isspace(*s1)) 1345 s1++; 1346 while (isspace(*s2)) 1347 s2++; 1348 if (*s1 > *s2) 1349 return 1; 1350 if (*s1 < *s2) 1351 return -1; 1352 s1++; 1353 s2++; 1354 } 1355 return 0; 1356 } 1357 1358 static struct ssif_addr_info *ssif_info_find(unsigned short addr, 1359 char *adapter_name, 1360 bool match_null_name) 1361 { 1362 struct ssif_addr_info *info, *found = NULL; 1363 1364 restart: 1365 list_for_each_entry(info, &ssif_infos, link) { 1366 if (info->binfo.addr == addr) { 1367 if (info->adapter_name || adapter_name) { 1368 if (!info->adapter_name != !adapter_name) { 1369 /* One is NULL and one is not */ 1370 continue; 1371 } 1372 if (adapter_name && 1373 strcmp_nospace(info->adapter_name, 1374 adapter_name)) 1375 /* Names do not match */ 1376 continue; 1377 } 1378 found = info; 1379 break; 1380 } 1381 } 1382 1383 if (!found && match_null_name) { 1384 /* Try to get an exact match first, then try with a NULL name */ 1385 adapter_name = NULL; 1386 match_null_name = false; 1387 goto restart; 1388 } 1389 1390 return found; 1391 } 1392 1393 static bool check_acpi(struct ssif_info *ssif_info, struct device *dev) 1394 { 1395 #ifdef CONFIG_ACPI 1396 acpi_handle acpi_handle; 1397 1398 acpi_handle = ACPI_HANDLE(dev); 1399 if (acpi_handle) { 1400 ssif_info->addr_source = SI_ACPI; 1401 ssif_info->addr_info.acpi_info.acpi_handle = acpi_handle; 1402 return true; 1403 } 1404 #endif 1405 return false; 1406 } 1407 1408 /* 1409 * Global enables we care about. 1410 */ 1411 #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \ 1412 IPMI_BMC_EVT_MSG_INTR) 1413 1414 static int ssif_probe(struct i2c_client *client, const struct i2c_device_id *id) 1415 { 1416 unsigned char msg[3]; 1417 unsigned char *resp; 1418 struct ssif_info *ssif_info; 1419 int rv = 0; 1420 int len; 1421 int i; 1422 u8 slave_addr = 0; 1423 struct ssif_addr_info *addr_info = NULL; 1424 1425 1426 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL); 1427 if (!resp) 1428 return -ENOMEM; 1429 1430 ssif_info = kzalloc(sizeof(*ssif_info), GFP_KERNEL); 1431 if (!ssif_info) { 1432 kfree(resp); 1433 return -ENOMEM; 1434 } 1435 1436 if (!check_acpi(ssif_info, &client->dev)) { 1437 addr_info = ssif_info_find(client->addr, client->adapter->name, 1438 true); 1439 if (!addr_info) { 1440 /* Must have come in through sysfs. */ 1441 ssif_info->addr_source = SI_HOTMOD; 1442 } else { 1443 ssif_info->addr_source = addr_info->addr_src; 1444 ssif_info->ssif_debug = addr_info->debug; 1445 ssif_info->addr_info = addr_info->addr_info; 1446 slave_addr = addr_info->slave_addr; 1447 } 1448 } 1449 1450 pr_info(PFX "Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n", 1451 ipmi_addr_src_to_str(ssif_info->addr_source), 1452 client->addr, client->adapter->name, slave_addr); 1453 1454 /* 1455 * Do a Get Device ID command, since it comes back with some 1456 * useful info. 1457 */ 1458 msg[0] = IPMI_NETFN_APP_REQUEST << 2; 1459 msg[1] = IPMI_GET_DEVICE_ID_CMD; 1460 rv = do_cmd(client, 2, msg, &len, resp); 1461 if (rv) 1462 goto out; 1463 1464 rv = ipmi_demangle_device_id(resp, len, &ssif_info->device_id); 1465 if (rv) 1466 goto out; 1467 1468 ssif_info->client = client; 1469 i2c_set_clientdata(client, ssif_info); 1470 1471 /* Now check for system interface capabilities */ 1472 msg[0] = IPMI_NETFN_APP_REQUEST << 2; 1473 msg[1] = IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD; 1474 msg[2] = 0; /* SSIF */ 1475 rv = do_cmd(client, 3, msg, &len, resp); 1476 if (!rv && (len >= 3) && (resp[2] == 0)) { 1477 if (len < 7) { 1478 if (ssif_dbg_probe) 1479 pr_info(PFX "SSIF info too short: %d\n", len); 1480 goto no_support; 1481 } 1482 1483 /* Got a good SSIF response, handle it. */ 1484 ssif_info->max_xmit_msg_size = resp[5]; 1485 ssif_info->max_recv_msg_size = resp[6]; 1486 ssif_info->multi_support = (resp[4] >> 6) & 0x3; 1487 ssif_info->supports_pec = (resp[4] >> 3) & 0x1; 1488 1489 /* Sanitize the data */ 1490 switch (ssif_info->multi_support) { 1491 case SSIF_NO_MULTI: 1492 if (ssif_info->max_xmit_msg_size > 32) 1493 ssif_info->max_xmit_msg_size = 32; 1494 if (ssif_info->max_recv_msg_size > 32) 1495 ssif_info->max_recv_msg_size = 32; 1496 break; 1497 1498 case SSIF_MULTI_2_PART: 1499 if (ssif_info->max_xmit_msg_size > 63) 1500 ssif_info->max_xmit_msg_size = 63; 1501 if (ssif_info->max_recv_msg_size > 62) 1502 ssif_info->max_recv_msg_size = 62; 1503 break; 1504 1505 case SSIF_MULTI_n_PART: 1506 /* 1507 * The specification is rather confusing at 1508 * this point, but I think I understand what 1509 * is meant. At least I have a workable 1510 * solution. With multi-part messages, you 1511 * cannot send a message that is a multiple of 1512 * 32-bytes in length, because the start and 1513 * middle messages are 32-bytes and the end 1514 * message must be at least one byte. You 1515 * can't fudge on an extra byte, that would 1516 * screw up things like fru data writes. So 1517 * we limit the length to 63 bytes. That way 1518 * a 32-byte message gets sent as a single 1519 * part. A larger message will be a 32-byte 1520 * start and the next message is always going 1521 * to be 1-31 bytes in length. Not ideal, but 1522 * it should work. 1523 */ 1524 if (ssif_info->max_xmit_msg_size > 63) 1525 ssif_info->max_xmit_msg_size = 63; 1526 break; 1527 1528 default: 1529 /* Data is not sane, just give up. */ 1530 goto no_support; 1531 } 1532 } else { 1533 no_support: 1534 /* Assume no multi-part or PEC support */ 1535 pr_info(PFX "Error fetching SSIF: %d %d %2.2x, your system probably doesn't support this command so using defaults\n", 1536 rv, len, resp[2]); 1537 1538 ssif_info->max_xmit_msg_size = 32; 1539 ssif_info->max_recv_msg_size = 32; 1540 ssif_info->multi_support = SSIF_NO_MULTI; 1541 ssif_info->supports_pec = 0; 1542 } 1543 1544 /* Make sure the NMI timeout is cleared. */ 1545 msg[0] = IPMI_NETFN_APP_REQUEST << 2; 1546 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD; 1547 msg[2] = WDT_PRE_TIMEOUT_INT; 1548 rv = do_cmd(client, 3, msg, &len, resp); 1549 if (rv || (len < 3) || (resp[2] != 0)) 1550 pr_warn(PFX "Unable to clear message flags: %d %d %2.2x\n", 1551 rv, len, resp[2]); 1552 1553 /* Attempt to enable the event buffer. */ 1554 msg[0] = IPMI_NETFN_APP_REQUEST << 2; 1555 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD; 1556 rv = do_cmd(client, 2, msg, &len, resp); 1557 if (rv || (len < 4) || (resp[2] != 0)) { 1558 pr_warn(PFX "Error getting global enables: %d %d %2.2x\n", 1559 rv, len, resp[2]); 1560 rv = 0; /* Not fatal */ 1561 goto found; 1562 } 1563 1564 ssif_info->global_enables = resp[3]; 1565 1566 if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) { 1567 ssif_info->has_event_buffer = true; 1568 /* buffer is already enabled, nothing to do. */ 1569 goto found; 1570 } 1571 1572 msg[0] = IPMI_NETFN_APP_REQUEST << 2; 1573 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD; 1574 msg[2] = ssif_info->global_enables | IPMI_BMC_EVT_MSG_BUFF; 1575 rv = do_cmd(client, 3, msg, &len, resp); 1576 if (rv || (len < 2)) { 1577 pr_warn(PFX "Error setting global enables: %d %d %2.2x\n", 1578 rv, len, resp[2]); 1579 rv = 0; /* Not fatal */ 1580 goto found; 1581 } 1582 1583 if (resp[2] == 0) { 1584 /* A successful return means the event buffer is supported. */ 1585 ssif_info->has_event_buffer = true; 1586 ssif_info->global_enables |= IPMI_BMC_EVT_MSG_BUFF; 1587 } 1588 1589 /* Some systems don't behave well if you enable alerts. */ 1590 if (alerts_broken) 1591 goto found; 1592 1593 msg[0] = IPMI_NETFN_APP_REQUEST << 2; 1594 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD; 1595 msg[2] = ssif_info->global_enables | IPMI_BMC_RCV_MSG_INTR; 1596 rv = do_cmd(client, 3, msg, &len, resp); 1597 if (rv || (len < 2)) { 1598 pr_warn(PFX "Error setting global enables: %d %d %2.2x\n", 1599 rv, len, resp[2]); 1600 rv = 0; /* Not fatal */ 1601 goto found; 1602 } 1603 1604 if (resp[2] == 0) { 1605 /* A successful return means the alert is supported. */ 1606 ssif_info->supports_alert = true; 1607 ssif_info->global_enables |= IPMI_BMC_RCV_MSG_INTR; 1608 } 1609 1610 found: 1611 ssif_info->intf_num = atomic_inc_return(&next_intf); 1612 1613 if (ssif_dbg_probe) { 1614 pr_info("ssif_probe: i2c_probe found device at i2c address %x\n", 1615 client->addr); 1616 } 1617 1618 spin_lock_init(&ssif_info->lock); 1619 ssif_info->ssif_state = SSIF_NORMAL; 1620 init_timer(&ssif_info->retry_timer); 1621 ssif_info->retry_timer.data = (unsigned long) ssif_info; 1622 ssif_info->retry_timer.function = retry_timeout; 1623 1624 for (i = 0; i < SSIF_NUM_STATS; i++) 1625 atomic_set(&ssif_info->stats[i], 0); 1626 1627 if (ssif_info->supports_pec) 1628 ssif_info->client->flags |= I2C_CLIENT_PEC; 1629 1630 ssif_info->handlers.owner = THIS_MODULE; 1631 ssif_info->handlers.start_processing = ssif_start_processing; 1632 ssif_info->handlers.get_smi_info = get_smi_info; 1633 ssif_info->handlers.sender = sender; 1634 ssif_info->handlers.request_events = request_events; 1635 ssif_info->handlers.inc_usecount = inc_usecount; 1636 ssif_info->handlers.dec_usecount = dec_usecount; 1637 1638 { 1639 unsigned int thread_num; 1640 1641 thread_num = ((ssif_info->client->adapter->nr << 8) | 1642 ssif_info->client->addr); 1643 init_completion(&ssif_info->wake_thread); 1644 ssif_info->thread = kthread_run(ipmi_ssif_thread, ssif_info, 1645 "kssif%4.4x", thread_num); 1646 if (IS_ERR(ssif_info->thread)) { 1647 rv = PTR_ERR(ssif_info->thread); 1648 dev_notice(&ssif_info->client->dev, 1649 "Could not start kernel thread: error %d\n", 1650 rv); 1651 goto out; 1652 } 1653 } 1654 1655 rv = ipmi_register_smi(&ssif_info->handlers, 1656 ssif_info, 1657 &ssif_info->device_id, 1658 &ssif_info->client->dev, 1659 slave_addr); 1660 if (rv) { 1661 pr_err(PFX "Unable to register device: error %d\n", rv); 1662 goto out; 1663 } 1664 1665 rv = ipmi_smi_add_proc_entry(ssif_info->intf, "type", 1666 &smi_type_proc_ops, 1667 ssif_info); 1668 if (rv) { 1669 pr_err(PFX "Unable to create proc entry: %d\n", rv); 1670 goto out_err_unreg; 1671 } 1672 1673 rv = ipmi_smi_add_proc_entry(ssif_info->intf, "ssif_stats", 1674 &smi_stats_proc_ops, 1675 ssif_info); 1676 if (rv) { 1677 pr_err(PFX "Unable to create proc entry: %d\n", rv); 1678 goto out_err_unreg; 1679 } 1680 1681 out: 1682 if (rv) 1683 kfree(ssif_info); 1684 kfree(resp); 1685 return rv; 1686 1687 out_err_unreg: 1688 ipmi_unregister_smi(ssif_info->intf); 1689 goto out; 1690 } 1691 1692 static int ssif_adapter_handler(struct device *adev, void *opaque) 1693 { 1694 struct ssif_addr_info *addr_info = opaque; 1695 1696 if (adev->type != &i2c_adapter_type) 1697 return 0; 1698 1699 i2c_new_device(to_i2c_adapter(adev), &addr_info->binfo); 1700 1701 if (!addr_info->adapter_name) 1702 return 1; /* Only try the first I2C adapter by default. */ 1703 return 0; 1704 } 1705 1706 static int new_ssif_client(int addr, char *adapter_name, 1707 int debug, int slave_addr, 1708 enum ipmi_addr_src addr_src) 1709 { 1710 struct ssif_addr_info *addr_info; 1711 int rv = 0; 1712 1713 mutex_lock(&ssif_infos_mutex); 1714 if (ssif_info_find(addr, adapter_name, false)) { 1715 rv = -EEXIST; 1716 goto out_unlock; 1717 } 1718 1719 addr_info = kzalloc(sizeof(*addr_info), GFP_KERNEL); 1720 if (!addr_info) { 1721 rv = -ENOMEM; 1722 goto out_unlock; 1723 } 1724 1725 if (adapter_name) { 1726 addr_info->adapter_name = kstrdup(adapter_name, GFP_KERNEL); 1727 if (!addr_info->adapter_name) { 1728 kfree(addr_info); 1729 rv = -ENOMEM; 1730 goto out_unlock; 1731 } 1732 } 1733 1734 strncpy(addr_info->binfo.type, DEVICE_NAME, 1735 sizeof(addr_info->binfo.type)); 1736 addr_info->binfo.addr = addr; 1737 addr_info->binfo.platform_data = addr_info; 1738 addr_info->debug = debug; 1739 addr_info->slave_addr = slave_addr; 1740 addr_info->addr_src = addr_src; 1741 1742 list_add_tail(&addr_info->link, &ssif_infos); 1743 1744 if (initialized) 1745 i2c_for_each_dev(addr_info, ssif_adapter_handler); 1746 /* Otherwise address list will get it */ 1747 1748 out_unlock: 1749 mutex_unlock(&ssif_infos_mutex); 1750 return rv; 1751 } 1752 1753 static void free_ssif_clients(void) 1754 { 1755 struct ssif_addr_info *info, *tmp; 1756 1757 mutex_lock(&ssif_infos_mutex); 1758 list_for_each_entry_safe(info, tmp, &ssif_infos, link) { 1759 list_del(&info->link); 1760 kfree(info->adapter_name); 1761 kfree(info); 1762 } 1763 mutex_unlock(&ssif_infos_mutex); 1764 } 1765 1766 static unsigned short *ssif_address_list(void) 1767 { 1768 struct ssif_addr_info *info; 1769 unsigned int count = 0, i; 1770 unsigned short *address_list; 1771 1772 list_for_each_entry(info, &ssif_infos, link) 1773 count++; 1774 1775 address_list = kzalloc(sizeof(*address_list) * (count + 1), GFP_KERNEL); 1776 if (!address_list) 1777 return NULL; 1778 1779 i = 0; 1780 list_for_each_entry(info, &ssif_infos, link) { 1781 unsigned short addr = info->binfo.addr; 1782 int j; 1783 1784 for (j = 0; j < i; j++) { 1785 if (address_list[j] == addr) 1786 goto skip_addr; 1787 } 1788 address_list[i] = addr; 1789 skip_addr: 1790 i++; 1791 } 1792 address_list[i] = I2C_CLIENT_END; 1793 1794 return address_list; 1795 } 1796 1797 #ifdef CONFIG_ACPI 1798 static const struct acpi_device_id ssif_acpi_match[] = { 1799 { "IPI0001", 0 }, 1800 { }, 1801 }; 1802 MODULE_DEVICE_TABLE(acpi, ssif_acpi_match); 1803 1804 /* 1805 * Once we get an ACPI failure, we don't try any more, because we go 1806 * through the tables sequentially. Once we don't find a table, there 1807 * are no more. 1808 */ 1809 static int acpi_failure; 1810 1811 /* 1812 * Defined in the IPMI 2.0 spec. 1813 */ 1814 struct SPMITable { 1815 s8 Signature[4]; 1816 u32 Length; 1817 u8 Revision; 1818 u8 Checksum; 1819 s8 OEMID[6]; 1820 s8 OEMTableID[8]; 1821 s8 OEMRevision[4]; 1822 s8 CreatorID[4]; 1823 s8 CreatorRevision[4]; 1824 u8 InterfaceType; 1825 u8 IPMIlegacy; 1826 s16 SpecificationRevision; 1827 1828 /* 1829 * Bit 0 - SCI interrupt supported 1830 * Bit 1 - I/O APIC/SAPIC 1831 */ 1832 u8 InterruptType; 1833 1834 /* 1835 * If bit 0 of InterruptType is set, then this is the SCI 1836 * interrupt in the GPEx_STS register. 1837 */ 1838 u8 GPE; 1839 1840 s16 Reserved; 1841 1842 /* 1843 * If bit 1 of InterruptType is set, then this is the I/O 1844 * APIC/SAPIC interrupt. 1845 */ 1846 u32 GlobalSystemInterrupt; 1847 1848 /* The actual register address. */ 1849 struct acpi_generic_address addr; 1850 1851 u8 UID[4]; 1852 1853 s8 spmi_id[1]; /* A '\0' terminated array starts here. */ 1854 }; 1855 1856 static int try_init_spmi(struct SPMITable *spmi) 1857 { 1858 unsigned short myaddr; 1859 1860 if (num_addrs >= MAX_SSIF_BMCS) 1861 return -1; 1862 1863 if (spmi->IPMIlegacy != 1) { 1864 pr_warn("IPMI: Bad SPMI legacy: %d\n", spmi->IPMIlegacy); 1865 return -ENODEV; 1866 } 1867 1868 if (spmi->InterfaceType != 4) 1869 return -ENODEV; 1870 1871 if (spmi->addr.space_id != ACPI_ADR_SPACE_SMBUS) { 1872 pr_warn(PFX "Invalid ACPI SSIF I/O Address type: %d\n", 1873 spmi->addr.space_id); 1874 return -EIO; 1875 } 1876 1877 myaddr = spmi->addr.address & 0x7f; 1878 1879 return new_ssif_client(myaddr, NULL, 0, 0, SI_SPMI); 1880 } 1881 1882 static void spmi_find_bmc(void) 1883 { 1884 acpi_status status; 1885 struct SPMITable *spmi; 1886 int i; 1887 1888 if (acpi_disabled) 1889 return; 1890 1891 if (acpi_failure) 1892 return; 1893 1894 for (i = 0; ; i++) { 1895 status = acpi_get_table(ACPI_SIG_SPMI, i+1, 1896 (struct acpi_table_header **)&spmi); 1897 if (status != AE_OK) 1898 return; 1899 1900 try_init_spmi(spmi); 1901 } 1902 } 1903 #else 1904 static void spmi_find_bmc(void) { } 1905 #endif 1906 1907 #ifdef CONFIG_DMI 1908 static int decode_dmi(const struct dmi_device *dmi_dev) 1909 { 1910 struct dmi_header *dm = dmi_dev->device_data; 1911 u8 *data = (u8 *) dm; 1912 u8 len = dm->length; 1913 unsigned short myaddr; 1914 int slave_addr; 1915 1916 if (num_addrs >= MAX_SSIF_BMCS) 1917 return -1; 1918 1919 if (len < 9) 1920 return -1; 1921 1922 if (data[0x04] != 4) /* Not SSIF */ 1923 return -1; 1924 1925 if ((data[8] >> 1) == 0) { 1926 /* 1927 * Some broken systems put the I2C address in 1928 * the slave address field. We try to 1929 * accommodate them here. 1930 */ 1931 myaddr = data[6] >> 1; 1932 slave_addr = 0; 1933 } else { 1934 myaddr = data[8] >> 1; 1935 slave_addr = data[6]; 1936 } 1937 1938 return new_ssif_client(myaddr, NULL, 0, 0, SI_SMBIOS); 1939 } 1940 1941 static void dmi_iterator(void) 1942 { 1943 const struct dmi_device *dev = NULL; 1944 1945 while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev))) 1946 decode_dmi(dev); 1947 } 1948 #else 1949 static void dmi_iterator(void) { } 1950 #endif 1951 1952 static const struct i2c_device_id ssif_id[] = { 1953 { DEVICE_NAME, 0 }, 1954 { } 1955 }; 1956 MODULE_DEVICE_TABLE(i2c, ssif_id); 1957 1958 static struct i2c_driver ssif_i2c_driver = { 1959 .class = I2C_CLASS_HWMON, 1960 .driver = { 1961 .name = DEVICE_NAME 1962 }, 1963 .probe = ssif_probe, 1964 .remove = ssif_remove, 1965 .alert = ssif_alert, 1966 .id_table = ssif_id, 1967 .detect = ssif_detect 1968 }; 1969 1970 static int init_ipmi_ssif(void) 1971 { 1972 int i; 1973 int rv; 1974 1975 if (initialized) 1976 return 0; 1977 1978 pr_info("IPMI SSIF Interface driver\n"); 1979 1980 /* build list for i2c from addr list */ 1981 for (i = 0; i < num_addrs; i++) { 1982 rv = new_ssif_client(addr[i], adapter_name[i], 1983 dbg[i], slave_addrs[i], 1984 SI_HARDCODED); 1985 if (rv) 1986 pr_err(PFX 1987 "Couldn't add hardcoded device at addr 0x%x\n", 1988 addr[i]); 1989 } 1990 1991 if (ssif_tryacpi) 1992 ssif_i2c_driver.driver.acpi_match_table = 1993 ACPI_PTR(ssif_acpi_match); 1994 if (ssif_trydmi) 1995 dmi_iterator(); 1996 if (ssif_tryacpi) 1997 spmi_find_bmc(); 1998 1999 ssif_i2c_driver.address_list = ssif_address_list(); 2000 2001 rv = i2c_add_driver(&ssif_i2c_driver); 2002 if (!rv) 2003 initialized = true; 2004 2005 return rv; 2006 } 2007 module_init(init_ipmi_ssif); 2008 2009 static void cleanup_ipmi_ssif(void) 2010 { 2011 if (!initialized) 2012 return; 2013 2014 initialized = false; 2015 2016 i2c_del_driver(&ssif_i2c_driver); 2017 2018 free_ssif_clients(); 2019 } 2020 module_exit(cleanup_ipmi_ssif); 2021 2022 MODULE_AUTHOR("Todd C Davis <todd.c.davis@intel.com>, Corey Minyard <minyard@acm.org>"); 2023 MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus"); 2024 MODULE_LICENSE("GPL"); 2025