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