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