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