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