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