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