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