]> git.ipfire.org Git - thirdparty/linux.git/blame - drivers/char/ipmi/ipmi_ssif.c
Merge tag 'io_uring-5.7-2020-05-22' of git://git.kernel.dk/linux-block
[thirdparty/linux.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;
1475 return true;
1476 }
1477#endif
1478 return false;
1479}
1480
94671710
CM
1481static int find_slave_address(struct i2c_client *client, int slave_addr)
1482{
0944d889
CM
1483#ifdef CONFIG_IPMI_DMI_DECODE
1484 if (!slave_addr)
1485 slave_addr = ipmi_dmi_get_slave_addr(
95e300c0 1486 SI_TYPE_INVALID,
0944d889
CM
1487 i2c_adapter_id(client->adapter),
1488 client->addr);
1489#endif
94671710
CM
1490
1491 return slave_addr;
1492}
1493
10042504
CM
1494static int start_multipart_test(struct i2c_client *client,
1495 unsigned char *msg, bool do_middle)
1496{
1497 int retry_cnt = SSIF_SEND_RETRIES, ret;
1498
1499retry_write:
1500 ret = i2c_smbus_write_block_data(client,
1501 SSIF_IPMI_MULTI_PART_REQUEST_START,
1502 32, msg);
1503 if (ret) {
1504 retry_cnt--;
1505 if (retry_cnt > 0)
1506 goto retry_write;
1507 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");
1508 return ret;
1509 }
1510
1511 if (!do_middle)
1512 return 0;
1513
1514 ret = i2c_smbus_write_block_data(client,
1515 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
1516 32, msg + 32);
1517 if (ret) {
1518 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");
1519 return ret;
1520 }
1521
1522 return 0;
1523}
1524
1525static void test_multipart_messages(struct i2c_client *client,
1526 struct ssif_info *ssif_info,
1527 unsigned char *resp)
1528{
1529 unsigned char msg[65];
1530 int ret;
1531 bool do_middle;
1532
1533 if (ssif_info->max_xmit_msg_size <= 32)
1534 return;
1535
1536 do_middle = ssif_info->max_xmit_msg_size > 63;
1537
1538 memset(msg, 0, sizeof(msg));
1539 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1540 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1541
1542 /*
1543 * The specification is all messed up dealing with sending
1544 * multi-part messages. Per what the specification says, it
1545 * is impossible to send a message that is a multiple of 32
1546 * bytes, except for 32 itself. It talks about a "start"
1547 * transaction (cmd=6) that must be 32 bytes, "middle"
1548 * transaction (cmd=7) that must be 32 bytes, and an "end"
1549 * transaction. The "end" transaction is shown as cmd=7 in
1550 * the text, but if that's the case there is no way to
1551 * differentiate between a middle and end part except the
1552 * length being less than 32. But there is a table at the far
1553 * end of the section (that I had never noticed until someone
1554 * pointed it out to me) that mentions it as cmd=8.
1555 *
1556 * After some thought, I think the example is wrong and the
1557 * end transaction should be cmd=8. But some systems don't
1558 * implement cmd=8, they use a zero-length end transaction,
1559 * even though that violates the SMBus specification.
1560 *
1561 * So, to work around this, this code tests if cmd=8 works.
1562 * If it does, then we use that. If not, it tests zero-
1563 * byte end transactions. If that works, good. If not,
1564 * we only allow 63-byte transactions max.
1565 */
1566
1567 ret = start_multipart_test(client, msg, do_middle);
1568 if (ret)
1569 goto out_no_multi_part;
1570
1571 ret = i2c_smbus_write_block_data(client,
1572 SSIF_IPMI_MULTI_PART_REQUEST_END,
1573 1, msg + 64);
1574
1575 if (!ret)
1576 ret = read_response(client, resp);
1577
1578 if (ret > 0) {
1579 /* End transactions work, we are good. */
1580 ssif_info->cmd8_works = true;
1581 return;
1582 }
1583
1584 ret = start_multipart_test(client, msg, do_middle);
1585 if (ret) {
1586 dev_err(&client->dev, "Second multipart test failed.\n");
1587 goto out_no_multi_part;
1588 }
1589
1590 ret = i2c_smbus_write_block_data(client,
1591 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
1592 0, msg + 64);
1593 if (!ret)
1594 ret = read_response(client, resp);
1595 if (ret > 0)
1596 /* Zero-size end parts work, use those. */
1597 return;
1598
1599 /* Limit to 63 bytes and use a short middle command to mark the end. */
1600 if (ssif_info->max_xmit_msg_size > 63)
1601 ssif_info->max_xmit_msg_size = 63;
1602 return;
1603
1604out_no_multi_part:
1605 ssif_info->max_xmit_msg_size = 32;
1606 return;
1607}
1608
91620521
CM
1609/*
1610 * Global enables we care about.
1611 */
1612#define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
1613 IPMI_BMC_EVT_MSG_INTR)
1614
c4436c91
KP
1615static void ssif_remove_dup(struct i2c_client *client)
1616{
1617 struct ssif_info *ssif_info = i2c_get_clientdata(client);
1618
1619 ipmi_unregister_smi(ssif_info->intf);
1620 kfree(ssif_info);
1621}
1622
1623static int ssif_add_infos(struct i2c_client *client)
1624{
1625 struct ssif_addr_info *info;
1626
1627 info = kzalloc(sizeof(*info), GFP_KERNEL);
1628 if (!info)
1629 return -ENOMEM;
1630 info->addr_src = SI_ACPI;
1631 info->client = client;
1632 info->adapter_name = kstrdup(client->adapter->name, GFP_KERNEL);
1633 info->binfo.addr = client->addr;
1634 list_add_tail(&info->link, &ssif_infos);
1635 return 0;
1636}
1637
1638/*
1639 * Prefer ACPI over SMBIOS, if both are available.
1640 * So if we get an ACPI interface and have already registered a SMBIOS
1641 * interface at the same address, remove the SMBIOS and add the ACPI one.
1642 */
1643static int ssif_check_and_remove(struct i2c_client *client,
1644 struct ssif_info *ssif_info)
1645{
1646 struct ssif_addr_info *info;
1647
1648 list_for_each_entry(info, &ssif_infos, link) {
1649 if (!info->client)
1650 return 0;
1651 if (!strcmp(info->adapter_name, client->adapter->name) &&
1652 info->binfo.addr == client->addr) {
1653 if (info->addr_src == SI_ACPI)
1654 return -EEXIST;
1655
1656 if (ssif_info->addr_source == SI_ACPI &&
1657 info->addr_src == SI_SMBIOS) {
1658 dev_info(&client->dev,
1659 "Removing %s-specified SSIF interface in favor of ACPI\n",
1660 ipmi_addr_src_to_str(info->addr_src));
1661 ssif_remove_dup(info->client);
1662 return 0;
1663 }
1664 }
1665 }
1666 return 0;
1667}
1668
25930707
CM
1669static int ssif_probe(struct i2c_client *client, const struct i2c_device_id *id)
1670{
1671 unsigned char msg[3];
1672 unsigned char *resp;
1673 struct ssif_info *ssif_info;
1674 int rv = 0;
1675 int len;
1676 int i;
1677 u8 slave_addr = 0;
1678 struct ssif_addr_info *addr_info = NULL;
1679
c4436c91 1680 mutex_lock(&ssif_infos_mutex);
25930707 1681 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
c4436c91
KP
1682 if (!resp) {
1683 mutex_unlock(&ssif_infos_mutex);
25930707 1684 return -ENOMEM;
c4436c91 1685 }
25930707
CM
1686
1687 ssif_info = kzalloc(sizeof(*ssif_info), GFP_KERNEL);
1688 if (!ssif_info) {
1689 kfree(resp);
c4436c91 1690 mutex_unlock(&ssif_infos_mutex);
25930707
CM
1691 return -ENOMEM;
1692 }
1693
1694 if (!check_acpi(ssif_info, &client->dev)) {
1695 addr_info = ssif_info_find(client->addr, client->adapter->name,
1696 true);
1697 if (!addr_info) {
1698 /* Must have come in through sysfs. */
1699 ssif_info->addr_source = SI_HOTMOD;
1700 } else {
1701 ssif_info->addr_source = addr_info->addr_src;
1702 ssif_info->ssif_debug = addr_info->debug;
1703 ssif_info->addr_info = addr_info->addr_info;
0944d889 1704 addr_info->client = client;
25930707
CM
1705 slave_addr = addr_info->slave_addr;
1706 }
1707 }
1708
c4436c91
KP
1709 rv = ssif_check_and_remove(client, ssif_info);
1710 /* If rv is 0 and addr source is not SI_ACPI, continue probing */
1711 if (!rv && ssif_info->addr_source == SI_ACPI) {
1712 rv = ssif_add_infos(client);
1713 if (rv) {
1714 dev_err(&client->dev, "Out of memory!, exiting ..\n");
1715 goto out;
1716 }
1717 } else if (rv) {
1718 dev_err(&client->dev, "Not probing, Interface already present\n");
1719 goto out;
1720 }
1721
94671710
CM
1722 slave_addr = find_slave_address(client, slave_addr);
1723
83af4194
CM
1724 dev_info(&client->dev,
1725 "Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
25880f7d
JP
1726 ipmi_addr_src_to_str(ssif_info->addr_source),
1727 client->addr, client->adapter->name, slave_addr);
25930707 1728
25930707
CM
1729 ssif_info->client = client;
1730 i2c_set_clientdata(client, ssif_info);
1731
1732 /* Now check for system interface capabilities */
1733 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1734 msg[1] = IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD;
1735 msg[2] = 0; /* SSIF */
1736 rv = do_cmd(client, 3, msg, &len, resp);
1737 if (!rv && (len >= 3) && (resp[2] == 0)) {
1738 if (len < 7) {
1739 if (ssif_dbg_probe)
83af4194
CM
1740 dev_dbg(&ssif_info->client->dev,
1741 "SSIF info too short: %d\n", len);
25930707
CM
1742 goto no_support;
1743 }
1744
1745 /* Got a good SSIF response, handle it. */
1746 ssif_info->max_xmit_msg_size = resp[5];
1747 ssif_info->max_recv_msg_size = resp[6];
1748 ssif_info->multi_support = (resp[4] >> 6) & 0x3;
1749 ssif_info->supports_pec = (resp[4] >> 3) & 0x1;
1750
1751 /* Sanitize the data */
1752 switch (ssif_info->multi_support) {
1753 case SSIF_NO_MULTI:
1754 if (ssif_info->max_xmit_msg_size > 32)
1755 ssif_info->max_xmit_msg_size = 32;
1756 if (ssif_info->max_recv_msg_size > 32)
1757 ssif_info->max_recv_msg_size = 32;
1758 break;
1759
1760 case SSIF_MULTI_2_PART:
3d69d43b
CM
1761 if (ssif_info->max_xmit_msg_size > 63)
1762 ssif_info->max_xmit_msg_size = 63;
25930707
CM
1763 if (ssif_info->max_recv_msg_size > 62)
1764 ssif_info->max_recv_msg_size = 62;
1765 break;
1766
1767 case SSIF_MULTI_n_PART:
10042504 1768 /* We take whatever size given, but do some testing. */
25930707
CM
1769 break;
1770
1771 default:
1772 /* Data is not sane, just give up. */
1773 goto no_support;
1774 }
1775 } else {
1776 no_support:
1777 /* Assume no multi-part or PEC support */
83af4194
CM
1778 dev_info(&ssif_info->client->dev,
1779 "Error fetching SSIF: %d %d %2.2x, your system probably doesn't support this command so using defaults\n",
25880f7d 1780 rv, len, resp[2]);
25930707
CM
1781
1782 ssif_info->max_xmit_msg_size = 32;
1783 ssif_info->max_recv_msg_size = 32;
1784 ssif_info->multi_support = SSIF_NO_MULTI;
1785 ssif_info->supports_pec = 0;
1786 }
1787
10042504
CM
1788 test_multipart_messages(client, ssif_info, resp);
1789
25930707
CM
1790 /* Make sure the NMI timeout is cleared. */
1791 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1792 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
1793 msg[2] = WDT_PRE_TIMEOUT_INT;
1794 rv = do_cmd(client, 3, msg, &len, resp);
1795 if (rv || (len < 3) || (resp[2] != 0))
83af4194
CM
1796 dev_warn(&ssif_info->client->dev,
1797 "Unable to clear message flags: %d %d %2.2x\n",
1798 rv, len, resp[2]);
25930707
CM
1799
1800 /* Attempt to enable the event buffer. */
1801 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1802 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
1803 rv = do_cmd(client, 2, msg, &len, resp);
1804 if (rv || (len < 4) || (resp[2] != 0)) {
83af4194
CM
1805 dev_warn(&ssif_info->client->dev,
1806 "Error getting global enables: %d %d %2.2x\n",
1807 rv, len, resp[2]);
25930707
CM
1808 rv = 0; /* Not fatal */
1809 goto found;
1810 }
1811
91620521
CM
1812 ssif_info->global_enables = resp[3];
1813
25930707
CM
1814 if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
1815 ssif_info->has_event_buffer = true;
1816 /* buffer is already enabled, nothing to do. */
1817 goto found;
1818 }
1819
1820 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1821 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
91620521 1822 msg[2] = ssif_info->global_enables | IPMI_BMC_EVT_MSG_BUFF;
25930707
CM
1823 rv = do_cmd(client, 3, msg, &len, resp);
1824 if (rv || (len < 2)) {
83af4194
CM
1825 dev_warn(&ssif_info->client->dev,
1826 "Error setting global enables: %d %d %2.2x\n",
1827 rv, len, resp[2]);
25930707
CM
1828 rv = 0; /* Not fatal */
1829 goto found;
1830 }
1831
91620521 1832 if (resp[2] == 0) {
25930707
CM
1833 /* A successful return means the event buffer is supported. */
1834 ssif_info->has_event_buffer = true;
91620521
CM
1835 ssif_info->global_enables |= IPMI_BMC_EVT_MSG_BUFF;
1836 }
1837
bf2d0877
CM
1838 /* Some systems don't behave well if you enable alerts. */
1839 if (alerts_broken)
1840 goto found;
1841
91620521
CM
1842 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1843 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1844 msg[2] = ssif_info->global_enables | IPMI_BMC_RCV_MSG_INTR;
1845 rv = do_cmd(client, 3, msg, &len, resp);
1846 if (rv || (len < 2)) {
83af4194
CM
1847 dev_warn(&ssif_info->client->dev,
1848 "Error setting global enables: %d %d %2.2x\n",
1849 rv, len, resp[2]);
91620521
CM
1850 rv = 0; /* Not fatal */
1851 goto found;
1852 }
1853
1854 if (resp[2] == 0) {
1855 /* A successful return means the alert is supported. */
1856 ssif_info->supports_alert = true;
1857 ssif_info->global_enables |= IPMI_BMC_RCV_MSG_INTR;
1858 }
25930707
CM
1859
1860 found:
25930707 1861 if (ssif_dbg_probe) {
83af4194
CM
1862 dev_dbg(&ssif_info->client->dev,
1863 "%s: i2c_probe found device at i2c address %x\n",
1864 __func__, client->addr);
25930707
CM
1865 }
1866
1867 spin_lock_init(&ssif_info->lock);
1868 ssif_info->ssif_state = SSIF_NORMAL;
e99e88a9 1869 timer_setup(&ssif_info->retry_timer, retry_timeout, 0);
a1466ec5 1870 timer_setup(&ssif_info->watch_timer, watch_timeout, 0);
25930707
CM
1871
1872 for (i = 0; i < SSIF_NUM_STATS; i++)
1873 atomic_set(&ssif_info->stats[i], 0);
1874
1875 if (ssif_info->supports_pec)
1876 ssif_info->client->flags |= I2C_CLIENT_PEC;
1877
1878 ssif_info->handlers.owner = THIS_MODULE;
1879 ssif_info->handlers.start_processing = ssif_start_processing;
a313dec6 1880 ssif_info->handlers.shutdown = shutdown_ssif;
25930707
CM
1881 ssif_info->handlers.get_smi_info = get_smi_info;
1882 ssif_info->handlers.sender = sender;
1883 ssif_info->handlers.request_events = request_events;
a1466ec5 1884 ssif_info->handlers.set_need_watch = ssif_set_need_watch;
25930707
CM
1885
1886 {
1887 unsigned int thread_num;
1888
be8647d2
CM
1889 thread_num = ((i2c_adapter_id(ssif_info->client->adapter)
1890 << 8) |
25930707
CM
1891 ssif_info->client->addr);
1892 init_completion(&ssif_info->wake_thread);
1893 ssif_info->thread = kthread_run(ipmi_ssif_thread, ssif_info,
1894 "kssif%4.4x", thread_num);
1895 if (IS_ERR(ssif_info->thread)) {
1896 rv = PTR_ERR(ssif_info->thread);
1897 dev_notice(&ssif_info->client->dev,
1898 "Could not start kernel thread: error %d\n",
1899 rv);
1900 goto out;
1901 }
1902 }
1903
ac2673d5
CM
1904 dev_set_drvdata(&ssif_info->client->dev, ssif_info);
1905 rv = device_add_group(&ssif_info->client->dev,
1906 &ipmi_ssif_dev_attr_group);
1907 if (rv) {
1908 dev_err(&ssif_info->client->dev,
1909 "Unable to add device attributes: error %d\n",
1910 rv);
1911 goto out;
1912 }
1913
25930707
CM
1914 rv = ipmi_register_smi(&ssif_info->handlers,
1915 ssif_info,
25930707
CM
1916 &ssif_info->client->dev,
1917 slave_addr);
d5a2197b 1918 if (rv) {
83af4194
CM
1919 dev_err(&ssif_info->client->dev,
1920 "Unable to register device: error %d\n", rv);
ac2673d5 1921 goto out_remove_attr;
25930707
CM
1922 }
1923
25930707 1924 out:
0944d889 1925 if (rv) {
a8627cda
GS
1926 if (addr_info)
1927 addr_info->client = NULL;
1928
83af4194
CM
1929 dev_err(&ssif_info->client->dev,
1930 "Unable to start IPMI SSIF: %d\n", rv);
25930707 1931 kfree(ssif_info);
0944d889 1932 }
25930707 1933 kfree(resp);
c4436c91 1934 mutex_unlock(&ssif_infos_mutex);
25930707
CM
1935 return rv;
1936
ac2673d5
CM
1937out_remove_attr:
1938 device_remove_group(&ssif_info->client->dev, &ipmi_ssif_dev_attr_group);
1939 dev_set_drvdata(&ssif_info->client->dev, NULL);
25930707
CM
1940 goto out;
1941}
1942
1943static int ssif_adapter_handler(struct device *adev, void *opaque)
1944{
1945 struct ssif_addr_info *addr_info = opaque;
1946
1947 if (adev->type != &i2c_adapter_type)
1948 return 0;
1949
653d3747
WS
1950 addr_info->added_client = i2c_new_client_device(to_i2c_adapter(adev),
1951 &addr_info->binfo);
25930707
CM
1952
1953 if (!addr_info->adapter_name)
1954 return 1; /* Only try the first I2C adapter by default. */
1955 return 0;
1956}
1957
1958static int new_ssif_client(int addr, char *adapter_name,
1959 int debug, int slave_addr,
0944d889
CM
1960 enum ipmi_addr_src addr_src,
1961 struct device *dev)
25930707
CM
1962{
1963 struct ssif_addr_info *addr_info;
1964 int rv = 0;
1965
1966 mutex_lock(&ssif_infos_mutex);
1967 if (ssif_info_find(addr, adapter_name, false)) {
1968 rv = -EEXIST;
1969 goto out_unlock;
1970 }
1971
1972 addr_info = kzalloc(sizeof(*addr_info), GFP_KERNEL);
1973 if (!addr_info) {
1974 rv = -ENOMEM;
1975 goto out_unlock;
1976 }
1977
1978 if (adapter_name) {
1979 addr_info->adapter_name = kstrdup(adapter_name, GFP_KERNEL);
1980 if (!addr_info->adapter_name) {
1981 kfree(addr_info);
1982 rv = -ENOMEM;
1983 goto out_unlock;
1984 }
1985 }
1986
1987 strncpy(addr_info->binfo.type, DEVICE_NAME,
1988 sizeof(addr_info->binfo.type));
1989 addr_info->binfo.addr = addr;
1990 addr_info->binfo.platform_data = addr_info;
1991 addr_info->debug = debug;
1992 addr_info->slave_addr = slave_addr;
1993 addr_info->addr_src = addr_src;
0944d889
CM
1994 addr_info->dev = dev;
1995
87ff091c
CM
1996 if (dev)
1997 dev_set_drvdata(dev, addr_info);
25930707
CM
1998
1999 list_add_tail(&addr_info->link, &ssif_infos);
2000
2001 if (initialized)
2002 i2c_for_each_dev(addr_info, ssif_adapter_handler);
2003 /* Otherwise address list will get it */
2004
2005out_unlock:
2006 mutex_unlock(&ssif_infos_mutex);
2007 return rv;
2008}
2009
2010static void free_ssif_clients(void)
2011{
2012 struct ssif_addr_info *info, *tmp;
2013
2014 mutex_lock(&ssif_infos_mutex);
2015 list_for_each_entry_safe(info, tmp, &ssif_infos, link) {
2016 list_del(&info->link);
2017 kfree(info->adapter_name);
2018 kfree(info);
2019 }
2020 mutex_unlock(&ssif_infos_mutex);
2021}
2022
2023static unsigned short *ssif_address_list(void)
2024{
2025 struct ssif_addr_info *info;
c75c5075 2026 unsigned int count = 0, i = 0;
25930707
CM
2027 unsigned short *address_list;
2028
2029 list_for_each_entry(info, &ssif_infos, link)
2030 count++;
2031
6396bb22
KC
2032 address_list = kcalloc(count + 1, sizeof(*address_list),
2033 GFP_KERNEL);
25930707
CM
2034 if (!address_list)
2035 return NULL;
2036
25930707
CM
2037 list_for_each_entry(info, &ssif_infos, link) {
2038 unsigned short addr = info->binfo.addr;
2039 int j;
2040
2041 for (j = 0; j < i; j++) {
2042 if (address_list[j] == addr)
c75c5075
CM
2043 /* Found a dup. */
2044 break;
25930707 2045 }
c75c5075
CM
2046 if (j == i) /* Didn't find it in the list. */
2047 address_list[i++] = addr;
25930707
CM
2048 }
2049 address_list[i] = I2C_CLIENT_END;
2050
2051 return address_list;
2052}
2053
2054#ifdef CONFIG_ACPI
5186cf9c 2055static const struct acpi_device_id ssif_acpi_match[] = {
25930707
CM
2056 { "IPI0001", 0 },
2057 { },
2058};
2059MODULE_DEVICE_TABLE(acpi, ssif_acpi_match);
25930707
CM
2060#endif
2061
2062#ifdef CONFIG_DMI
0944d889 2063static int dmi_ipmi_probe(struct platform_device *pdev)
25930707 2064{
95e300c0 2065 u8 slave_addr = 0;
0944d889
CM
2066 u16 i2c_addr;
2067 int rv;
25930707 2068
0944d889
CM
2069 if (!ssif_trydmi)
2070 return -ENODEV;
25930707 2071
0944d889
CM
2072 rv = device_property_read_u16(&pdev->dev, "i2c-addr", &i2c_addr);
2073 if (rv) {
25880f7d 2074 dev_warn(&pdev->dev, "No i2c-addr property\n");
0944d889 2075 return -ENODEV;
25930707
CM
2076 }
2077
0944d889
CM
2078 rv = device_property_read_u8(&pdev->dev, "slave-addr", &slave_addr);
2079 if (rv)
ed6c3a6d 2080 slave_addr = 0x20;
25930707 2081
0944d889
CM
2082 return new_ssif_client(i2c_addr, NULL, 0,
2083 slave_addr, SI_SMBIOS, &pdev->dev);
25930707
CM
2084}
2085#else
0944d889
CM
2086static int dmi_ipmi_probe(struct platform_device *pdev)
2087{
2088 return -ENODEV;
2089}
25930707
CM
2090#endif
2091
2092static const struct i2c_device_id ssif_id[] = {
2093 { DEVICE_NAME, 0 },
2094 { }
2095};
2096MODULE_DEVICE_TABLE(i2c, ssif_id);
2097
2098static struct i2c_driver ssif_i2c_driver = {
2099 .class = I2C_CLASS_HWMON,
2100 .driver = {
25930707
CM
2101 .name = DEVICE_NAME
2102 },
2103 .probe = ssif_probe,
2104 .remove = ssif_remove,
91620521 2105 .alert = ssif_alert,
25930707
CM
2106 .id_table = ssif_id,
2107 .detect = ssif_detect
2108};
2109
0944d889
CM
2110static int ssif_platform_probe(struct platform_device *dev)
2111{
2112 return dmi_ipmi_probe(dev);
2113}
2114
2115static int ssif_platform_remove(struct platform_device *dev)
2116{
2117 struct ssif_addr_info *addr_info = dev_get_drvdata(&dev->dev);
2118
2119 if (!addr_info)
2120 return 0;
2121
2122 mutex_lock(&ssif_infos_mutex);
0745dde6 2123 i2c_unregister_device(addr_info->added_client);
0944d889
CM
2124
2125 list_del(&addr_info->link);
2126 kfree(addr_info);
2127 mutex_unlock(&ssif_infos_mutex);
2128 return 0;
2129}
2130
b3096c70
CM
2131static const struct platform_device_id ssif_plat_ids[] = {
2132 { "dmi-ipmi-ssif", 0 },
2133 { }
2134};
2135
0944d889
CM
2136static struct platform_driver ipmi_driver = {
2137 .driver = {
2138 .name = DEVICE_NAME,
2139 },
2140 .probe = ssif_platform_probe,
2141 .remove = ssif_platform_remove,
b3096c70 2142 .id_table = ssif_plat_ids
0944d889
CM
2143};
2144
25930707
CM
2145static int init_ipmi_ssif(void)
2146{
2147 int i;
2148 int rv;
2149
2150 if (initialized)
2151 return 0;
2152
2153 pr_info("IPMI SSIF Interface driver\n");
2154
2155 /* build list for i2c from addr list */
2156 for (i = 0; i < num_addrs; i++) {
2157 rv = new_ssif_client(addr[i], adapter_name[i],
2158 dbg[i], slave_addrs[i],
0944d889 2159 SI_HARDCODED, NULL);
d467f7a4 2160 if (rv)
25880f7d 2161 pr_err("Couldn't add hardcoded device at addr 0x%x\n",
25930707
CM
2162 addr[i]);
2163 }
2164
2165 if (ssif_tryacpi)
2166 ssif_i2c_driver.driver.acpi_match_table =
2167 ACPI_PTR(ssif_acpi_match);
0944d889 2168
0944d889
CM
2169 if (ssif_trydmi) {
2170 rv = platform_driver_register(&ipmi_driver);
2171 if (rv)
25880f7d 2172 pr_err("Unable to register driver: %d\n", rv);
2cd0e544
KW
2173 else
2174 platform_registered = true;
0944d889
CM
2175 }
2176
25930707
CM
2177 ssif_i2c_driver.address_list = ssif_address_list();
2178
2179 rv = i2c_add_driver(&ssif_i2c_driver);
2180 if (!rv)
2181 initialized = true;
2182
2183 return rv;
2184}
2185module_init(init_ipmi_ssif);
2186
2187static void cleanup_ipmi_ssif(void)
2188{
2189 if (!initialized)
2190 return;
2191
2192 initialized = false;
2193
2194 i2c_del_driver(&ssif_i2c_driver);
2195
36e398d7
CM
2196 kfree(ssif_i2c_driver.address_list);
2197
2cd0e544 2198 if (ssif_trydmi && platform_registered)
44f56a39 2199 platform_driver_unregister(&ipmi_driver);
0944d889 2200
25930707
CM
2201 free_ssif_clients();
2202}
2203module_exit(cleanup_ipmi_ssif);
2204
0944d889 2205MODULE_ALIAS("platform:dmi-ipmi-ssif");
25930707
CM
2206MODULE_AUTHOR("Todd C Davis <todd.c.davis@intel.com>, Corey Minyard <minyard@acm.org>");
2207MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus");
2208MODULE_LICENSE("GPL");