blob: cca6e5bc1cea3c01831b66223f09b22fe7cf4c74 [file] [log] [blame]
Corey Minyard25930702012-03-19 16:00:55 -05001/*
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
Corey Minyard25930702012-03-19 16:00:55 -050034#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>
Corey Minyarde3fe1422014-12-16 08:36:32 -060054#include <linux/ctype.h>
Amitoj Kaur Chawla526290a2015-10-24 01:21:04 +053055#include <linux/time64.h>
Corey Minyard25930702012-03-19 16:00:55 -050056
57#define PFX "ipmi_ssif: "
58#define DEVICE_NAME "ipmi_ssif"
59
60#define IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD 0x57
61
62#define SSIF_IPMI_REQUEST 2
63#define SSIF_IPMI_MULTI_PART_REQUEST_START 6
64#define SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE 7
65#define SSIF_IPMI_RESPONSE 3
66#define SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE 9
67
68/* ssif_debug is a bit-field
69 * SSIF_DEBUG_MSG - commands and their responses
70 * SSIF_DEBUG_STATES - message states
71 * SSIF_DEBUG_TIMING - Measure times between events in the driver
72 */
73#define SSIF_DEBUG_TIMING 4
74#define SSIF_DEBUG_STATE 2
75#define SSIF_DEBUG_MSG 1
76#define SSIF_NODEBUG 0
77#define SSIF_DEFAULT_DEBUG (SSIF_NODEBUG)
78
79/*
80 * Timer values
81 */
82#define SSIF_MSG_USEC 20000 /* 20ms between message tries. */
83#define SSIF_MSG_PART_USEC 5000 /* 5ms for a message part */
84
85/* How many times to we retry sending/receiving the message. */
86#define SSIF_SEND_RETRIES 5
87#define SSIF_RECV_RETRIES 250
88
89#define SSIF_MSG_MSEC (SSIF_MSG_USEC / 1000)
90#define SSIF_MSG_JIFFIES ((SSIF_MSG_USEC * 1000) / TICK_NSEC)
91#define SSIF_MSG_PART_JIFFIES ((SSIF_MSG_PART_USEC * 1000) / TICK_NSEC)
92
93enum ssif_intf_state {
94 SSIF_NORMAL,
95 SSIF_GETTING_FLAGS,
96 SSIF_GETTING_EVENTS,
97 SSIF_CLEARING_FLAGS,
98 SSIF_GETTING_MESSAGES,
99 /* FIXME - add watchdog stuff. */
100};
101
102#define SSIF_IDLE(ssif) ((ssif)->ssif_state == SSIF_NORMAL \
103 && (ssif)->curr_msg == NULL)
104
105/*
106 * Indexes into stats[] in ssif_info below.
107 */
108enum ssif_stat_indexes {
109 /* Number of total messages sent. */
110 SSIF_STAT_sent_messages = 0,
111
112 /*
113 * Number of message parts sent. Messages may be broken into
114 * parts if they are long.
115 */
116 SSIF_STAT_sent_messages_parts,
117
118 /*
119 * Number of time a message was retried.
120 */
121 SSIF_STAT_send_retries,
122
123 /*
124 * Number of times the send of a message failed.
125 */
126 SSIF_STAT_send_errors,
127
128 /*
129 * Number of message responses received.
130 */
131 SSIF_STAT_received_messages,
132
133 /*
134 * Number of message fragments received.
135 */
136 SSIF_STAT_received_message_parts,
137
138 /*
139 * Number of times the receive of a message was retried.
140 */
141 SSIF_STAT_receive_retries,
142
143 /*
144 * Number of errors receiving messages.
145 */
146 SSIF_STAT_receive_errors,
147
148 /*
149 * Number of times a flag fetch was requested.
150 */
151 SSIF_STAT_flag_fetches,
152
153 /*
154 * Number of times the hardware didn't follow the state machine.
155 */
156 SSIF_STAT_hosed,
157
158 /*
159 * Number of received events.
160 */
161 SSIF_STAT_events,
162
163 /* Number of asyncronous messages received. */
164 SSIF_STAT_incoming_messages,
165
166 /* Number of watchdog pretimeouts. */
167 SSIF_STAT_watchdog_pretimeouts,
168
Corey Minyard91620522015-04-24 07:46:06 -0500169 /* Number of alers received. */
170 SSIF_STAT_alerts,
171
Corey Minyard25930702012-03-19 16:00:55 -0500172 /* Always add statistics before this value, it must be last. */
173 SSIF_NUM_STATS
174};
175
176struct ssif_addr_info {
Corey Minyard25930702012-03-19 16:00:55 -0500177 struct i2c_board_info binfo;
178 char *adapter_name;
179 int debug;
180 int slave_addr;
181 enum ipmi_addr_src addr_src;
182 union ipmi_smi_info_union addr_info;
183
184 struct mutex clients_mutex;
185 struct list_head clients;
186
187 struct list_head link;
188};
189
190struct ssif_info;
191
192typedef void (*ssif_i2c_done)(struct ssif_info *ssif_info, int result,
193 unsigned char *data, unsigned int len);
194
195struct ssif_info {
196 ipmi_smi_t intf;
197 int intf_num;
198 spinlock_t lock;
199 struct ipmi_smi_msg *waiting_msg;
200 struct ipmi_smi_msg *curr_msg;
201 enum ssif_intf_state ssif_state;
202 unsigned long ssif_debug;
203
204 struct ipmi_smi_handlers handlers;
205
206 enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */
207 union ipmi_smi_info_union addr_info;
208
209 /*
210 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
211 * is set to hold the flags until we are done handling everything
212 * from the flags.
213 */
214#define RECEIVE_MSG_AVAIL 0x01
215#define EVENT_MSG_BUFFER_FULL 0x02
216#define WDT_PRE_TIMEOUT_INT 0x08
217 unsigned char msg_flags;
218
Corey Minyard91620522015-04-24 07:46:06 -0500219 u8 global_enables;
Corey Minyard25930702012-03-19 16:00:55 -0500220 bool has_event_buffer;
Corey Minyard91620522015-04-24 07:46:06 -0500221 bool supports_alert;
222
223 /*
224 * Used to tell what we should do with alerts. If we are
225 * waiting on a response, read the data immediately.
226 */
227 bool got_alert;
228 bool waiting_alert;
Corey Minyard25930702012-03-19 16:00:55 -0500229
230 /*
231 * If set to true, this will request events the next time the
232 * state machine is idle.
233 */
234 bool req_events;
235
236 /*
237 * If set to true, this will request flags the next time the
238 * state machine is idle.
239 */
240 bool req_flags;
241
242 /*
243 * Used to perform timer operations when run-to-completion
244 * mode is on. This is a countdown timer.
245 */
246 int rtc_us_timer;
247
248 /* Used for sending/receiving data. +1 for the length. */
249 unsigned char data[IPMI_MAX_MSG_LENGTH + 1];
250 unsigned int data_len;
251
252 /* Temp receive buffer, gets copied into data. */
253 unsigned char recv[I2C_SMBUS_BLOCK_MAX];
254
255 struct i2c_client *client;
256 ssif_i2c_done done_handler;
257
258 /* Thread interface handling */
259 struct task_struct *thread;
260 struct completion wake_thread;
261 bool stopping;
262 int i2c_read_write;
263 int i2c_command;
264 unsigned char *i2c_data;
265 unsigned int i2c_size;
266
267 /* From the device id response. */
268 struct ipmi_device_id device_id;
269
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;
411 return;
412 }
413
414 ssif_info->curr_msg = msg;
415 ssif_info->ssif_state = SSIF_GETTING_EVENTS;
416 ipmi_ssif_unlock_cond(ssif_info, flags);
417
418 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
419 msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
420 msg->data_size = 2;
421
422 check_start_send(ssif_info, flags, msg);
423}
424
425static void start_recv_msg_fetch(struct ssif_info *ssif_info,
426 unsigned long *flags)
427{
428 struct ipmi_smi_msg *msg;
429
430 msg = ipmi_alloc_smi_msg();
431 if (!msg) {
432 ssif_info->ssif_state = SSIF_NORMAL;
433 return;
434 }
435
436 ssif_info->curr_msg = msg;
437 ssif_info->ssif_state = SSIF_GETTING_MESSAGES;
438 ipmi_ssif_unlock_cond(ssif_info, flags);
439
440 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
441 msg->data[1] = IPMI_GET_MSG_CMD;
442 msg->data_size = 2;
443
444 check_start_send(ssif_info, flags, msg);
445}
446
447/*
448 * Must be called with the message lock held. This will release the
449 * message lock. Note that the caller will check SSIF_IDLE and start a
450 * new operation, so there is no need to check for new messages to
451 * start in here.
452 */
453static void handle_flags(struct ssif_info *ssif_info, unsigned long *flags)
454{
455 if (ssif_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
456 ipmi_smi_t intf = ssif_info->intf;
457 /* Watchdog pre-timeout */
458 ssif_inc_stat(ssif_info, watchdog_pretimeouts);
459 start_clear_flags(ssif_info, flags);
460 if (intf)
461 ipmi_smi_watchdog_pretimeout(intf);
462 } else if (ssif_info->msg_flags & RECEIVE_MSG_AVAIL)
463 /* Messages available. */
464 start_recv_msg_fetch(ssif_info, flags);
465 else if (ssif_info->msg_flags & EVENT_MSG_BUFFER_FULL)
466 /* Events available. */
467 start_event_fetch(ssif_info, flags);
468 else {
469 ssif_info->ssif_state = SSIF_NORMAL;
470 ipmi_ssif_unlock_cond(ssif_info, flags);
471 }
472}
473
474static int ipmi_ssif_thread(void *data)
475{
476 struct ssif_info *ssif_info = data;
477
478 while (!kthread_should_stop()) {
479 int result;
480
481 /* Wait for something to do */
Corey Minyardd0acf732015-04-04 01:54:26 -0500482 result = wait_for_completion_interruptible(
483 &ssif_info->wake_thread);
Corey Minyard25930702012-03-19 16:00:55 -0500484 if (ssif_info->stopping)
485 break;
Corey Minyardd0acf732015-04-04 01:54:26 -0500486 if (result == -ERESTARTSYS)
487 continue;
488 init_completion(&ssif_info->wake_thread);
Corey Minyard25930702012-03-19 16:00:55 -0500489
490 if (ssif_info->i2c_read_write == I2C_SMBUS_WRITE) {
491 result = i2c_smbus_write_block_data(
Corey Minyard3d69d432015-04-29 17:59:21 -0500492 ssif_info->client, ssif_info->i2c_command,
Corey Minyard25930702012-03-19 16:00:55 -0500493 ssif_info->i2c_data[0],
494 ssif_info->i2c_data + 1);
495 ssif_info->done_handler(ssif_info, result, NULL, 0);
496 } else {
497 result = i2c_smbus_read_block_data(
Corey Minyard3d69d432015-04-29 17:59:21 -0500498 ssif_info->client, ssif_info->i2c_command,
Corey Minyard25930702012-03-19 16:00:55 -0500499 ssif_info->i2c_data);
500 if (result < 0)
501 ssif_info->done_handler(ssif_info, result,
502 NULL, 0);
503 else
504 ssif_info->done_handler(ssif_info, 0,
505 ssif_info->i2c_data,
506 result);
507 }
508 }
509
510 return 0;
511}
512
513static int ssif_i2c_send(struct ssif_info *ssif_info,
514 ssif_i2c_done handler,
515 int read_write, int command,
516 unsigned char *data, unsigned int size)
517{
518 ssif_info->done_handler = handler;
519
520 ssif_info->i2c_read_write = read_write;
521 ssif_info->i2c_command = command;
522 ssif_info->i2c_data = data;
523 ssif_info->i2c_size = size;
524 complete(&ssif_info->wake_thread);
525 return 0;
526}
527
528
529static void msg_done_handler(struct ssif_info *ssif_info, int result,
530 unsigned char *data, unsigned int len);
531
Corey Minyard91620522015-04-24 07:46:06 -0500532static void start_get(struct ssif_info *ssif_info)
Corey Minyard25930702012-03-19 16:00:55 -0500533{
Corey Minyard25930702012-03-19 16:00:55 -0500534 int rv;
535
Corey Minyard25930702012-03-19 16:00:55 -0500536 ssif_info->rtc_us_timer = 0;
Corey Minyard3d69d432015-04-29 17:59:21 -0500537 ssif_info->multi_pos = 0;
Corey Minyard25930702012-03-19 16:00:55 -0500538
539 rv = ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
540 SSIF_IPMI_RESPONSE,
541 ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
542 if (rv < 0) {
543 /* request failed, just return the error. */
544 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
545 pr_info("Error from i2c_non_blocking_op(5)\n");
546
547 msg_done_handler(ssif_info, -EIO, NULL, 0);
548 }
549}
550
Corey Minyard91620522015-04-24 07:46:06 -0500551static void retry_timeout(unsigned long data)
552{
553 struct ssif_info *ssif_info = (void *) data;
554 unsigned long oflags, *flags;
555 bool waiting;
556
557 if (ssif_info->stopping)
558 return;
559
560 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
561 waiting = ssif_info->waiting_alert;
562 ssif_info->waiting_alert = false;
563 ipmi_ssif_unlock_cond(ssif_info, flags);
564
565 if (waiting)
566 start_get(ssif_info);
567}
568
569
Benjamin Tissoiresb4f21052016-06-09 16:53:47 +0200570static void ssif_alert(struct i2c_client *client, enum i2c_alert_protocol type,
571 unsigned int data)
Corey Minyard91620522015-04-24 07:46:06 -0500572{
573 struct ssif_info *ssif_info = i2c_get_clientdata(client);
574 unsigned long oflags, *flags;
575 bool do_get = false;
576
Benjamin Tissoiresb4f21052016-06-09 16:53:47 +0200577 if (type != I2C_PROTOCOL_SMBUS_ALERT)
578 return;
579
Corey Minyard91620522015-04-24 07:46:06 -0500580 ssif_inc_stat(ssif_info, alerts);
581
582 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
583 if (ssif_info->waiting_alert) {
584 ssif_info->waiting_alert = false;
585 del_timer(&ssif_info->retry_timer);
586 do_get = true;
587 } else if (ssif_info->curr_msg) {
588 ssif_info->got_alert = true;
589 }
590 ipmi_ssif_unlock_cond(ssif_info, flags);
591 if (do_get)
592 start_get(ssif_info);
593}
594
Corey Minyard25930702012-03-19 16:00:55 -0500595static int start_resend(struct ssif_info *ssif_info);
596
597static void msg_done_handler(struct ssif_info *ssif_info, int result,
598 unsigned char *data, unsigned int len)
599{
600 struct ipmi_smi_msg *msg;
601 unsigned long oflags, *flags;
602 int rv;
603
604 /*
605 * We are single-threaded here, so no need for a lock until we
606 * start messing with driver states or the queues.
607 */
608
609 if (result < 0) {
610 ssif_info->retries_left--;
611 if (ssif_info->retries_left > 0) {
612 ssif_inc_stat(ssif_info, receive_retries);
613
Corey Minyard91620522015-04-24 07:46:06 -0500614 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
615 ssif_info->waiting_alert = true;
616 ssif_info->rtc_us_timer = SSIF_MSG_USEC;
Corey Minyard25930702012-03-19 16:00:55 -0500617 mod_timer(&ssif_info->retry_timer,
618 jiffies + SSIF_MSG_JIFFIES);
Corey Minyard91620522015-04-24 07:46:06 -0500619 ipmi_ssif_unlock_cond(ssif_info, flags);
Corey Minyard25930702012-03-19 16:00:55 -0500620 return;
621 }
622
623 ssif_inc_stat(ssif_info, receive_errors);
624
625 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
626 pr_info("Error in msg_done_handler: %d\n", result);
627 len = 0;
628 goto continue_op;
629 }
630
631 if ((len > 1) && (ssif_info->multi_pos == 0)
632 && (data[0] == 0x00) && (data[1] == 0x01)) {
633 /* Start of multi-part read. Start the next transaction. */
634 int i;
635
636 ssif_inc_stat(ssif_info, received_message_parts);
637
638 /* Remove the multi-part read marker. */
Corey Minyard25930702012-03-19 16:00:55 -0500639 len -= 2;
Corey Minyard3d69d432015-04-29 17:59:21 -0500640 for (i = 0; i < len; i++)
641 ssif_info->data[i] = data[i+2];
Corey Minyard25930702012-03-19 16:00:55 -0500642 ssif_info->multi_len = len;
643 ssif_info->multi_pos = 1;
644
645 rv = ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
646 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
647 ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
648 if (rv < 0) {
649 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
650 pr_info("Error from i2c_non_blocking_op(1)\n");
651
652 result = -EIO;
653 } else
654 return;
655 } else if (ssif_info->multi_pos) {
656 /* Middle of multi-part read. Start the next transaction. */
657 int i;
658 unsigned char blocknum;
659
660 if (len == 0) {
661 result = -EIO;
662 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
663 pr_info(PFX "Middle message with no data\n");
664
665 goto continue_op;
666 }
667
Corey Minyard3d69d432015-04-29 17:59:21 -0500668 blocknum = data[0];
Corey Minyard25930702012-03-19 16:00:55 -0500669
Corey Minyard3d69d432015-04-29 17:59:21 -0500670 if (ssif_info->multi_len + len - 1 > IPMI_MAX_MSG_LENGTH) {
Corey Minyard25930702012-03-19 16:00:55 -0500671 /* Received message too big, abort the operation. */
672 result = -E2BIG;
673 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
674 pr_info("Received message too big\n");
675
676 goto continue_op;
677 }
678
679 /* Remove the blocknum from the data. */
Corey Minyard25930702012-03-19 16:00:55 -0500680 len--;
Corey Minyard3d69d432015-04-29 17:59:21 -0500681 for (i = 0; i < len; i++)
682 ssif_info->data[i + ssif_info->multi_len] = data[i + 1];
Corey Minyard25930702012-03-19 16:00:55 -0500683 ssif_info->multi_len += len;
684 if (blocknum == 0xff) {
685 /* End of read */
686 len = ssif_info->multi_len;
687 data = ssif_info->data;
Corey Minyard3d69d432015-04-29 17:59:21 -0500688 } else if (blocknum + 1 != ssif_info->multi_pos) {
Corey Minyard25930702012-03-19 16:00:55 -0500689 /*
690 * Out of sequence block, just abort. Block
691 * numbers start at zero for the second block,
692 * but multi_pos starts at one, so the +1.
693 */
694 result = -EIO;
695 } else {
696 ssif_inc_stat(ssif_info, received_message_parts);
697
698 ssif_info->multi_pos++;
699
700 rv = ssif_i2c_send(ssif_info, msg_done_handler,
701 I2C_SMBUS_READ,
702 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
703 ssif_info->recv,
704 I2C_SMBUS_BLOCK_DATA);
705 if (rv < 0) {
706 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
707 pr_info(PFX
Corey Minyard91620522015-04-24 07:46:06 -0500708 "Error from ssif_i2c_send\n");
Corey Minyard25930702012-03-19 16:00:55 -0500709
710 result = -EIO;
711 } else
712 return;
713 }
714 }
715
716 if (result < 0) {
717 ssif_inc_stat(ssif_info, receive_errors);
718 } else {
719 ssif_inc_stat(ssif_info, received_messages);
720 ssif_inc_stat(ssif_info, received_message_parts);
721 }
722
723
724 continue_op:
725 if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
726 pr_info(PFX "DONE 1: state = %d, result=%d.\n",
727 ssif_info->ssif_state, result);
728
729 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
730 msg = ssif_info->curr_msg;
731 if (msg) {
732 msg->rsp_size = len;
733 if (msg->rsp_size > IPMI_MAX_MSG_LENGTH)
734 msg->rsp_size = IPMI_MAX_MSG_LENGTH;
735 memcpy(msg->rsp, data, msg->rsp_size);
736 ssif_info->curr_msg = NULL;
737 }
738
739 switch (ssif_info->ssif_state) {
740 case SSIF_NORMAL:
741 ipmi_ssif_unlock_cond(ssif_info, flags);
742 if (!msg)
743 break;
744
745 if (result < 0)
746 return_hosed_msg(ssif_info, msg);
747 else
748 deliver_recv_msg(ssif_info, msg);
749 break;
750
751 case SSIF_GETTING_FLAGS:
752 /* We got the flags from the SSIF, now handle them. */
753 if ((result < 0) || (len < 4) || (data[2] != 0)) {
754 /*
755 * Error fetching flags, or invalid length,
756 * just give up for now.
757 */
758 ssif_info->ssif_state = SSIF_NORMAL;
759 ipmi_ssif_unlock_cond(ssif_info, flags);
760 pr_warn(PFX "Error getting flags: %d %d, %x\n",
761 result, len, data[2]);
762 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
763 || data[1] != IPMI_GET_MSG_FLAGS_CMD) {
764 pr_warn(PFX "Invalid response getting flags: %x %x\n",
765 data[0], data[1]);
766 } else {
767 ssif_inc_stat(ssif_info, flag_fetches);
768 ssif_info->msg_flags = data[3];
769 handle_flags(ssif_info, flags);
770 }
771 break;
772
773 case SSIF_CLEARING_FLAGS:
774 /* We cleared the flags. */
775 if ((result < 0) || (len < 3) || (data[2] != 0)) {
776 /* Error clearing flags */
777 pr_warn(PFX "Error clearing flags: %d %d, %x\n",
778 result, len, data[2]);
779 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
780 || data[1] != IPMI_CLEAR_MSG_FLAGS_CMD) {
781 pr_warn(PFX "Invalid response clearing flags: %x %x\n",
782 data[0], data[1]);
783 }
784 ssif_info->ssif_state = SSIF_NORMAL;
785 ipmi_ssif_unlock_cond(ssif_info, flags);
786 break;
787
788 case SSIF_GETTING_EVENTS:
789 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
790 /* Error getting event, probably done. */
791 msg->done(msg);
792
793 /* Take off the event flag. */
794 ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
795 handle_flags(ssif_info, flags);
796 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
797 || msg->rsp[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD) {
798 pr_warn(PFX "Invalid response getting events: %x %x\n",
799 msg->rsp[0], msg->rsp[1]);
800 msg->done(msg);
801 /* Take off the event flag. */
802 ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
803 handle_flags(ssif_info, flags);
804 } else {
805 handle_flags(ssif_info, flags);
806 ssif_inc_stat(ssif_info, events);
807 deliver_recv_msg(ssif_info, msg);
808 }
809 break;
810
811 case SSIF_GETTING_MESSAGES:
812 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
813 /* Error getting event, probably done. */
814 msg->done(msg);
815
816 /* Take off the msg flag. */
817 ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
818 handle_flags(ssif_info, flags);
819 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
820 || msg->rsp[1] != IPMI_GET_MSG_CMD) {
821 pr_warn(PFX "Invalid response clearing flags: %x %x\n",
822 msg->rsp[0], msg->rsp[1]);
823 msg->done(msg);
824
825 /* Take off the msg flag. */
826 ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
827 handle_flags(ssif_info, flags);
828 } else {
829 ssif_inc_stat(ssif_info, incoming_messages);
830 handle_flags(ssif_info, flags);
831 deliver_recv_msg(ssif_info, msg);
832 }
833 break;
834 }
835
836 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
837 if (SSIF_IDLE(ssif_info) && !ssif_info->stopping) {
838 if (ssif_info->req_events)
839 start_event_fetch(ssif_info, flags);
840 else if (ssif_info->req_flags)
841 start_flag_fetch(ssif_info, flags);
842 else
843 start_next_msg(ssif_info, flags);
844 } else
845 ipmi_ssif_unlock_cond(ssif_info, flags);
846
847 if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
848 pr_info(PFX "DONE 2: state = %d.\n", ssif_info->ssif_state);
849}
850
851static void msg_written_handler(struct ssif_info *ssif_info, int result,
852 unsigned char *data, unsigned int len)
853{
854 int rv;
855
856 /* We are single-threaded here, so no need for a lock. */
857 if (result < 0) {
858 ssif_info->retries_left--;
859 if (ssif_info->retries_left > 0) {
860 if (!start_resend(ssif_info)) {
861 ssif_inc_stat(ssif_info, send_retries);
862 return;
863 }
864 /* request failed, just return the error. */
865 ssif_inc_stat(ssif_info, send_errors);
866
867 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
868 pr_info(PFX
869 "Out of retries in msg_written_handler\n");
870 msg_done_handler(ssif_info, -EIO, NULL, 0);
871 return;
872 }
873
874 ssif_inc_stat(ssif_info, send_errors);
875
876 /*
877 * Got an error on transmit, let the done routine
878 * handle it.
879 */
880 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
881 pr_info("Error in msg_written_handler: %d\n", result);
882
883 msg_done_handler(ssif_info, result, NULL, 0);
884 return;
885 }
886
887 if (ssif_info->multi_data) {
Corey Minyard3d69d432015-04-29 17:59:21 -0500888 /*
889 * In the middle of a multi-data write. See the comment
890 * in the SSIF_MULTI_n_PART case in the probe function
891 * for details on the intricacies of this.
892 */
Corey Minyard25930702012-03-19 16:00:55 -0500893 int left;
894
895 ssif_inc_stat(ssif_info, sent_messages_parts);
896
897 left = ssif_info->multi_len - ssif_info->multi_pos;
898 if (left > 32)
899 left = 32;
900 /* Length byte. */
901 ssif_info->multi_data[ssif_info->multi_pos] = left;
902 ssif_info->multi_pos += left;
903 if (left < 32)
904 /*
905 * Write is finished. Note that we must end
906 * with a write of less than 32 bytes to
907 * complete the transaction, even if it is
908 * zero bytes.
909 */
910 ssif_info->multi_data = NULL;
911
912 rv = ssif_i2c_send(ssif_info, msg_written_handler,
913 I2C_SMBUS_WRITE,
914 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
915 ssif_info->multi_data + ssif_info->multi_pos,
916 I2C_SMBUS_BLOCK_DATA);
917 if (rv < 0) {
918 /* request failed, just return the error. */
919 ssif_inc_stat(ssif_info, send_errors);
920
921 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
922 pr_info("Error from i2c_non_blocking_op(3)\n");
923 msg_done_handler(ssif_info, -EIO, NULL, 0);
924 }
925 } else {
Corey Minyard21c8f912016-01-06 09:32:06 -0600926 /* Ready to request the result. */
Corey Minyard91620522015-04-24 07:46:06 -0500927 unsigned long oflags, *flags;
Corey Minyard91620522015-04-24 07:46:06 -0500928
Corey Minyard25930702012-03-19 16:00:55 -0500929 ssif_inc_stat(ssif_info, sent_messages);
930 ssif_inc_stat(ssif_info, sent_messages_parts);
931
Corey Minyard91620522015-04-24 07:46:06 -0500932 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
Corey Minyard21c8f912016-01-06 09:32:06 -0600933 if (ssif_info->got_alert) {
934 /* The result is already ready, just start it. */
Corey Minyard91620522015-04-24 07:46:06 -0500935 ssif_info->got_alert = false;
Corey Minyard91620522015-04-24 07:46:06 -0500936 ipmi_ssif_unlock_cond(ssif_info, flags);
Corey Minyard21c8f912016-01-06 09:32:06 -0600937 start_get(ssif_info);
Corey Minyard91620522015-04-24 07:46:06 -0500938 } else {
939 /* Wait a jiffie then request the next message */
940 ssif_info->waiting_alert = true;
941 ssif_info->retries_left = SSIF_RECV_RETRIES;
942 ssif_info->rtc_us_timer = SSIF_MSG_PART_USEC;
943 mod_timer(&ssif_info->retry_timer,
944 jiffies + SSIF_MSG_PART_JIFFIES);
945 ipmi_ssif_unlock_cond(ssif_info, flags);
946 }
Corey Minyard25930702012-03-19 16:00:55 -0500947 }
948}
949
950static int start_resend(struct ssif_info *ssif_info)
951{
952 int rv;
953 int command;
954
Corey Minyard91620522015-04-24 07:46:06 -0500955 ssif_info->got_alert = false;
956
Corey Minyard25930702012-03-19 16:00:55 -0500957 if (ssif_info->data_len > 32) {
958 command = SSIF_IPMI_MULTI_PART_REQUEST_START;
959 ssif_info->multi_data = ssif_info->data;
960 ssif_info->multi_len = ssif_info->data_len;
961 /*
962 * Subtle thing, this is 32, not 33, because we will
963 * overwrite the thing at position 32 (which was just
964 * transmitted) with the new length.
965 */
966 ssif_info->multi_pos = 32;
967 ssif_info->data[0] = 32;
968 } else {
969 ssif_info->multi_data = NULL;
970 command = SSIF_IPMI_REQUEST;
971 ssif_info->data[0] = ssif_info->data_len;
972 }
973
974 rv = ssif_i2c_send(ssif_info, msg_written_handler, I2C_SMBUS_WRITE,
975 command, ssif_info->data, I2C_SMBUS_BLOCK_DATA);
976 if (rv && (ssif_info->ssif_debug & SSIF_DEBUG_MSG))
977 pr_info("Error from i2c_non_blocking_op(4)\n");
978 return rv;
979}
980
981static int start_send(struct ssif_info *ssif_info,
982 unsigned char *data,
983 unsigned int len)
984{
985 if (len > IPMI_MAX_MSG_LENGTH)
986 return -E2BIG;
987 if (len > ssif_info->max_xmit_msg_size)
988 return -E2BIG;
989
990 ssif_info->retries_left = SSIF_SEND_RETRIES;
Corey Minyard3d69d432015-04-29 17:59:21 -0500991 memcpy(ssif_info->data + 1, data, len);
Corey Minyard25930702012-03-19 16:00:55 -0500992 ssif_info->data_len = len;
993 return start_resend(ssif_info);
994}
995
996/* Must be called with the message lock held. */
997static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags)
998{
999 struct ipmi_smi_msg *msg;
1000 unsigned long oflags;
1001
1002 restart:
1003 if (!SSIF_IDLE(ssif_info)) {
1004 ipmi_ssif_unlock_cond(ssif_info, flags);
1005 return;
1006 }
1007
1008 if (!ssif_info->waiting_msg) {
1009 ssif_info->curr_msg = NULL;
1010 ipmi_ssif_unlock_cond(ssif_info, flags);
1011 } else {
1012 int rv;
1013
1014 ssif_info->curr_msg = ssif_info->waiting_msg;
1015 ssif_info->waiting_msg = NULL;
1016 ipmi_ssif_unlock_cond(ssif_info, flags);
1017 rv = start_send(ssif_info,
1018 ssif_info->curr_msg->data,
1019 ssif_info->curr_msg->data_size);
1020 if (rv) {
1021 msg = ssif_info->curr_msg;
1022 ssif_info->curr_msg = NULL;
1023 return_hosed_msg(ssif_info, msg);
1024 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1025 goto restart;
1026 }
1027 }
1028}
1029
1030static void sender(void *send_info,
1031 struct ipmi_smi_msg *msg)
1032{
1033 struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1034 unsigned long oflags, *flags;
1035
1036 BUG_ON(ssif_info->waiting_msg);
1037 ssif_info->waiting_msg = msg;
1038
1039 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1040 start_next_msg(ssif_info, flags);
1041
1042 if (ssif_info->ssif_debug & SSIF_DEBUG_TIMING) {
Amitoj Kaur Chawla526290a2015-10-24 01:21:04 +05301043 struct timespec64 t;
Corey Minyard25930702012-03-19 16:00:55 -05001044
Amitoj Kaur Chawla526290a2015-10-24 01:21:04 +05301045 ktime_get_real_ts64(&t);
1046 pr_info("**Enqueue %02x %02x: %lld.%6.6ld\n",
Corey Minyard1421c932014-12-30 13:31:45 -06001047 msg->data[0], msg->data[1],
Amitoj Kaur Chawla526290a2015-10-24 01:21:04 +05301048 (long long) t.tv_sec, (long) t.tv_nsec / NSEC_PER_USEC);
Corey Minyard25930702012-03-19 16:00:55 -05001049 }
1050}
1051
1052static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1053{
1054 struct ssif_info *ssif_info = send_info;
1055
1056 data->addr_src = ssif_info->addr_source;
1057 data->dev = &ssif_info->client->dev;
1058 data->addr_info = ssif_info->addr_info;
1059 get_device(data->dev);
1060
1061 return 0;
1062}
1063
1064/*
1065 * Instead of having our own timer to periodically check the message
1066 * flags, we let the message handler drive us.
1067 */
1068static void request_events(void *send_info)
1069{
1070 struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1071 unsigned long oflags, *flags;
1072
1073 if (!ssif_info->has_event_buffer)
1074 return;
1075
1076 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1077 /*
1078 * Request flags first, not events, because the lower layer
1079 * doesn't have a way to send an attention. But make sure
1080 * event checking still happens.
1081 */
1082 ssif_info->req_events = true;
1083 if (SSIF_IDLE(ssif_info))
1084 start_flag_fetch(ssif_info, flags);
1085 else {
1086 ssif_info->req_flags = true;
1087 ipmi_ssif_unlock_cond(ssif_info, flags);
1088 }
1089}
1090
1091static int inc_usecount(void *send_info)
1092{
1093 struct ssif_info *ssif_info = send_info;
1094
1095 if (!i2c_get_adapter(ssif_info->client->adapter->nr))
1096 return -ENODEV;
1097
1098 i2c_use_client(ssif_info->client);
1099 return 0;
1100}
1101
1102static void dec_usecount(void *send_info)
1103{
1104 struct ssif_info *ssif_info = send_info;
1105
1106 i2c_release_client(ssif_info->client);
1107 i2c_put_adapter(ssif_info->client->adapter);
1108}
1109
1110static int ssif_start_processing(void *send_info,
1111 ipmi_smi_t intf)
1112{
1113 struct ssif_info *ssif_info = send_info;
1114
1115 ssif_info->intf = intf;
1116
1117 return 0;
1118}
1119
1120#define MAX_SSIF_BMCS 4
1121
1122static unsigned short addr[MAX_SSIF_BMCS];
1123static int num_addrs;
1124module_param_array(addr, ushort, &num_addrs, 0);
1125MODULE_PARM_DESC(addr, "The addresses to scan for IPMI BMCs on the SSIFs.");
1126
1127static char *adapter_name[MAX_SSIF_BMCS];
1128static int num_adapter_names;
1129module_param_array(adapter_name, charp, &num_adapter_names, 0);
1130MODULE_PARM_DESC(adapter_name, "The string name of the I2C device that has the BMC. By default all devices are scanned.");
1131
1132static int slave_addrs[MAX_SSIF_BMCS];
1133static int num_slave_addrs;
1134module_param_array(slave_addrs, int, &num_slave_addrs, 0);
1135MODULE_PARM_DESC(slave_addrs,
1136 "The default IPMB slave address for the controller.");
1137
Corey Minyardbf2d0872015-08-27 15:49:18 -05001138static bool alerts_broken;
1139module_param(alerts_broken, bool, 0);
1140MODULE_PARM_DESC(alerts_broken, "Don't enable alerts for the controller.");
1141
Corey Minyard25930702012-03-19 16:00:55 -05001142/*
1143 * Bit 0 enables message debugging, bit 1 enables state debugging, and
1144 * bit 2 enables timing debugging. This is an array indexed by
1145 * interface number"
1146 */
1147static int dbg[MAX_SSIF_BMCS];
1148static int num_dbg;
1149module_param_array(dbg, int, &num_dbg, 0);
1150MODULE_PARM_DESC(dbg, "Turn on debugging.");
1151
1152static bool ssif_dbg_probe;
1153module_param_named(dbg_probe, ssif_dbg_probe, bool, 0);
1154MODULE_PARM_DESC(dbg_probe, "Enable debugging of probing of adapters.");
1155
Shailendra Vermafedb25e2015-05-26 00:54:57 +05301156static bool ssif_tryacpi = true;
Corey Minyard25930702012-03-19 16:00:55 -05001157module_param_named(tryacpi, ssif_tryacpi, bool, 0);
1158MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
1159
Shailendra Vermafedb25e2015-05-26 00:54:57 +05301160static bool ssif_trydmi = true;
Corey Minyard25930702012-03-19 16:00:55 -05001161module_param_named(trydmi, ssif_trydmi, bool, 0);
1162MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the default scan of the interfaces identified via DMI (SMBIOS)");
1163
1164static DEFINE_MUTEX(ssif_infos_mutex);
1165static LIST_HEAD(ssif_infos);
1166
1167static int ssif_remove(struct i2c_client *client)
1168{
1169 struct ssif_info *ssif_info = i2c_get_clientdata(client);
1170 int rv;
1171
1172 if (!ssif_info)
1173 return 0;
1174
Corey Minyard25930702012-03-19 16:00:55 -05001175 /*
1176 * After this point, we won't deliver anything asychronously
1177 * to the message handler. We can unregister ourself.
1178 */
1179 rv = ipmi_unregister_smi(ssif_info->intf);
1180 if (rv) {
1181 pr_err(PFX "Unable to unregister device: errno=%d\n", rv);
1182 return rv;
1183 }
1184 ssif_info->intf = NULL;
1185
1186 /* make sure the driver is not looking for flags any more. */
1187 while (ssif_info->ssif_state != SSIF_NORMAL)
1188 schedule_timeout(1);
1189
1190 ssif_info->stopping = true;
1191 del_timer_sync(&ssif_info->retry_timer);
1192 if (ssif_info->thread) {
1193 complete(&ssif_info->wake_thread);
1194 kthread_stop(ssif_info->thread);
1195 }
1196
1197 /*
1198 * No message can be outstanding now, we have removed the
1199 * upper layer and it permitted us to do so.
1200 */
1201 kfree(ssif_info);
1202 return 0;
1203}
1204
1205static int do_cmd(struct i2c_client *client, int len, unsigned char *msg,
1206 int *resp_len, unsigned char *resp)
1207{
1208 int retry_cnt;
1209 int ret;
1210
1211 retry_cnt = SSIF_SEND_RETRIES;
1212 retry1:
1213 ret = i2c_smbus_write_block_data(client, SSIF_IPMI_REQUEST, len, msg);
1214 if (ret) {
1215 retry_cnt--;
1216 if (retry_cnt > 0)
1217 goto retry1;
1218 return -ENODEV;
1219 }
1220
1221 ret = -ENODEV;
1222 retry_cnt = SSIF_RECV_RETRIES;
1223 while (retry_cnt > 0) {
1224 ret = i2c_smbus_read_block_data(client, SSIF_IPMI_RESPONSE,
1225 resp);
1226 if (ret > 0)
1227 break;
1228 msleep(SSIF_MSG_MSEC);
1229 retry_cnt--;
1230 if (retry_cnt <= 0)
1231 break;
1232 }
1233
1234 if (ret > 0) {
1235 /* Validate that the response is correct. */
1236 if (ret < 3 ||
1237 (resp[0] != (msg[0] | (1 << 2))) ||
1238 (resp[1] != msg[1]))
1239 ret = -EINVAL;
1240 else {
1241 *resp_len = ret;
1242 ret = 0;
1243 }
1244 }
1245
1246 return ret;
1247}
1248
1249static int ssif_detect(struct i2c_client *client, struct i2c_board_info *info)
1250{
1251 unsigned char *resp;
1252 unsigned char msg[3];
1253 int rv;
1254 int len;
1255
1256 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1257 if (!resp)
1258 return -ENOMEM;
1259
1260 /* Do a Get Device ID command, since it is required. */
1261 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1262 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1263 rv = do_cmd(client, 2, msg, &len, resp);
1264 if (rv)
1265 rv = -ENODEV;
1266 else
1267 strlcpy(info->type, DEVICE_NAME, I2C_NAME_SIZE);
1268 kfree(resp);
1269 return rv;
1270}
1271
1272static int smi_type_proc_show(struct seq_file *m, void *v)
1273{
Joe Perchesd6c5dc12015-02-17 11:10:56 -08001274 seq_puts(m, "ssif\n");
1275
Joe Perches5e33cd02015-02-22 10:21:07 -08001276 return 0;
Corey Minyard25930702012-03-19 16:00:55 -05001277}
1278
1279static int smi_type_proc_open(struct inode *inode, struct file *file)
1280{
1281 return single_open(file, smi_type_proc_show, inode->i_private);
1282}
1283
1284static const struct file_operations smi_type_proc_ops = {
1285 .open = smi_type_proc_open,
1286 .read = seq_read,
1287 .llseek = seq_lseek,
1288 .release = single_release,
1289};
1290
1291static int smi_stats_proc_show(struct seq_file *m, void *v)
1292{
1293 struct ssif_info *ssif_info = m->private;
1294
1295 seq_printf(m, "sent_messages: %u\n",
1296 ssif_get_stat(ssif_info, sent_messages));
1297 seq_printf(m, "sent_messages_parts: %u\n",
1298 ssif_get_stat(ssif_info, sent_messages_parts));
1299 seq_printf(m, "send_retries: %u\n",
1300 ssif_get_stat(ssif_info, send_retries));
1301 seq_printf(m, "send_errors: %u\n",
1302 ssif_get_stat(ssif_info, send_errors));
1303 seq_printf(m, "received_messages: %u\n",
1304 ssif_get_stat(ssif_info, received_messages));
1305 seq_printf(m, "received_message_parts: %u\n",
1306 ssif_get_stat(ssif_info, received_message_parts));
1307 seq_printf(m, "receive_retries: %u\n",
1308 ssif_get_stat(ssif_info, receive_retries));
1309 seq_printf(m, "receive_errors: %u\n",
1310 ssif_get_stat(ssif_info, receive_errors));
1311 seq_printf(m, "flag_fetches: %u\n",
1312 ssif_get_stat(ssif_info, flag_fetches));
1313 seq_printf(m, "hosed: %u\n",
1314 ssif_get_stat(ssif_info, hosed));
1315 seq_printf(m, "events: %u\n",
1316 ssif_get_stat(ssif_info, events));
1317 seq_printf(m, "watchdog_pretimeouts: %u\n",
1318 ssif_get_stat(ssif_info, watchdog_pretimeouts));
Corey Minyard91620522015-04-24 07:46:06 -05001319 seq_printf(m, "alerts: %u\n",
1320 ssif_get_stat(ssif_info, alerts));
Corey Minyard25930702012-03-19 16:00:55 -05001321 return 0;
1322}
1323
1324static int smi_stats_proc_open(struct inode *inode, struct file *file)
1325{
1326 return single_open(file, smi_stats_proc_show, PDE_DATA(inode));
1327}
1328
1329static const struct file_operations smi_stats_proc_ops = {
1330 .open = smi_stats_proc_open,
1331 .read = seq_read,
1332 .llseek = seq_lseek,
1333 .release = single_release,
1334};
1335
Corey Minyardb0e9aaa2015-03-31 12:48:53 -05001336static int strcmp_nospace(char *s1, char *s2)
1337{
1338 while (*s1 && *s2) {
1339 while (isspace(*s1))
1340 s1++;
1341 while (isspace(*s2))
1342 s2++;
1343 if (*s1 > *s2)
1344 return 1;
1345 if (*s1 < *s2)
1346 return -1;
1347 s1++;
1348 s2++;
1349 }
1350 return 0;
1351}
1352
Corey Minyard25930702012-03-19 16:00:55 -05001353static struct ssif_addr_info *ssif_info_find(unsigned short addr,
1354 char *adapter_name,
1355 bool match_null_name)
1356{
1357 struct ssif_addr_info *info, *found = NULL;
1358
1359restart:
1360 list_for_each_entry(info, &ssif_infos, link) {
1361 if (info->binfo.addr == addr) {
1362 if (info->adapter_name || adapter_name) {
1363 if (!info->adapter_name != !adapter_name) {
1364 /* One is NULL and one is not */
1365 continue;
1366 }
Corey Minyardb0e9aaa2015-03-31 12:48:53 -05001367 if (adapter_name &&
1368 strcmp_nospace(info->adapter_name,
1369 adapter_name))
1370 /* Names do not match */
Corey Minyard25930702012-03-19 16:00:55 -05001371 continue;
1372 }
1373 found = info;
1374 break;
1375 }
1376 }
1377
1378 if (!found && match_null_name) {
1379 /* Try to get an exact match first, then try with a NULL name */
1380 adapter_name = NULL;
1381 match_null_name = false;
1382 goto restart;
1383 }
1384
1385 return found;
1386}
1387
1388static bool check_acpi(struct ssif_info *ssif_info, struct device *dev)
1389{
1390#ifdef CONFIG_ACPI
1391 acpi_handle acpi_handle;
1392
1393 acpi_handle = ACPI_HANDLE(dev);
1394 if (acpi_handle) {
1395 ssif_info->addr_source = SI_ACPI;
1396 ssif_info->addr_info.acpi_info.acpi_handle = acpi_handle;
1397 return true;
1398 }
1399#endif
1400 return false;
1401}
1402
Corey Minyard94671712016-11-07 12:07:09 -06001403static int find_slave_address(struct i2c_client *client, int slave_addr)
1404{
1405 struct ssif_addr_info *info;
1406
1407 if (slave_addr)
1408 return slave_addr;
1409
1410 /*
1411 * Came in without a slave address, search around to see if
1412 * the other sources have a slave address. This lets us pick
1413 * up an SMBIOS slave address when using ACPI.
1414 */
1415 list_for_each_entry(info, &ssif_infos, link) {
1416 if (info->binfo.addr != client->addr)
1417 continue;
1418 if (info->adapter_name && client->adapter->name &&
1419 strcmp_nospace(info->adapter_name,
1420 client->adapter->name))
1421 continue;
1422 if (info->slave_addr) {
1423 slave_addr = info->slave_addr;
1424 break;
1425 }
1426 }
1427
1428 return slave_addr;
1429}
1430
Corey Minyard91620522015-04-24 07:46:06 -05001431/*
1432 * Global enables we care about.
1433 */
1434#define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
1435 IPMI_BMC_EVT_MSG_INTR)
1436
Corey Minyard25930702012-03-19 16:00:55 -05001437static int ssif_probe(struct i2c_client *client, const struct i2c_device_id *id)
1438{
1439 unsigned char msg[3];
1440 unsigned char *resp;
1441 struct ssif_info *ssif_info;
1442 int rv = 0;
1443 int len;
1444 int i;
1445 u8 slave_addr = 0;
1446 struct ssif_addr_info *addr_info = NULL;
1447
1448
1449 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1450 if (!resp)
1451 return -ENOMEM;
1452
1453 ssif_info = kzalloc(sizeof(*ssif_info), GFP_KERNEL);
1454 if (!ssif_info) {
1455 kfree(resp);
1456 return -ENOMEM;
1457 }
1458
1459 if (!check_acpi(ssif_info, &client->dev)) {
1460 addr_info = ssif_info_find(client->addr, client->adapter->name,
1461 true);
1462 if (!addr_info) {
1463 /* Must have come in through sysfs. */
1464 ssif_info->addr_source = SI_HOTMOD;
1465 } else {
1466 ssif_info->addr_source = addr_info->addr_src;
1467 ssif_info->ssif_debug = addr_info->debug;
1468 ssif_info->addr_info = addr_info->addr_info;
1469 slave_addr = addr_info->slave_addr;
1470 }
1471 }
1472
Corey Minyard94671712016-11-07 12:07:09 -06001473 slave_addr = find_slave_address(client, slave_addr);
1474
Corey Minyard25930702012-03-19 16:00:55 -05001475 pr_info(PFX "Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
1476 ipmi_addr_src_to_str(ssif_info->addr_source),
1477 client->addr, client->adapter->name, slave_addr);
1478
1479 /*
1480 * Do a Get Device ID command, since it comes back with some
1481 * useful info.
1482 */
1483 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1484 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1485 rv = do_cmd(client, 2, msg, &len, resp);
1486 if (rv)
1487 goto out;
1488
1489 rv = ipmi_demangle_device_id(resp, len, &ssif_info->device_id);
1490 if (rv)
1491 goto out;
1492
1493 ssif_info->client = client;
1494 i2c_set_clientdata(client, ssif_info);
1495
1496 /* Now check for system interface capabilities */
1497 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1498 msg[1] = IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD;
1499 msg[2] = 0; /* SSIF */
1500 rv = do_cmd(client, 3, msg, &len, resp);
1501 if (!rv && (len >= 3) && (resp[2] == 0)) {
1502 if (len < 7) {
1503 if (ssif_dbg_probe)
1504 pr_info(PFX "SSIF info too short: %d\n", len);
1505 goto no_support;
1506 }
1507
1508 /* Got a good SSIF response, handle it. */
1509 ssif_info->max_xmit_msg_size = resp[5];
1510 ssif_info->max_recv_msg_size = resp[6];
1511 ssif_info->multi_support = (resp[4] >> 6) & 0x3;
1512 ssif_info->supports_pec = (resp[4] >> 3) & 0x1;
1513
1514 /* Sanitize the data */
1515 switch (ssif_info->multi_support) {
1516 case SSIF_NO_MULTI:
1517 if (ssif_info->max_xmit_msg_size > 32)
1518 ssif_info->max_xmit_msg_size = 32;
1519 if (ssif_info->max_recv_msg_size > 32)
1520 ssif_info->max_recv_msg_size = 32;
1521 break;
1522
1523 case SSIF_MULTI_2_PART:
Corey Minyard3d69d432015-04-29 17:59:21 -05001524 if (ssif_info->max_xmit_msg_size > 63)
1525 ssif_info->max_xmit_msg_size = 63;
Corey Minyard25930702012-03-19 16:00:55 -05001526 if (ssif_info->max_recv_msg_size > 62)
1527 ssif_info->max_recv_msg_size = 62;
1528 break;
1529
1530 case SSIF_MULTI_n_PART:
Corey Minyard3d69d432015-04-29 17:59:21 -05001531 /*
1532 * The specification is rather confusing at
1533 * this point, but I think I understand what
1534 * is meant. At least I have a workable
1535 * solution. With multi-part messages, you
1536 * cannot send a message that is a multiple of
1537 * 32-bytes in length, because the start and
1538 * middle messages are 32-bytes and the end
1539 * message must be at least one byte. You
1540 * can't fudge on an extra byte, that would
1541 * screw up things like fru data writes. So
1542 * we limit the length to 63 bytes. That way
1543 * a 32-byte message gets sent as a single
1544 * part. A larger message will be a 32-byte
1545 * start and the next message is always going
1546 * to be 1-31 bytes in length. Not ideal, but
1547 * it should work.
1548 */
1549 if (ssif_info->max_xmit_msg_size > 63)
1550 ssif_info->max_xmit_msg_size = 63;
Corey Minyard25930702012-03-19 16:00:55 -05001551 break;
1552
1553 default:
1554 /* Data is not sane, just give up. */
1555 goto no_support;
1556 }
1557 } else {
1558 no_support:
1559 /* Assume no multi-part or PEC support */
Corey Minyardb0e9aaa2015-03-31 12:48:53 -05001560 pr_info(PFX "Error fetching SSIF: %d %d %2.2x, your system probably doesn't support this command so using defaults\n",
Corey Minyard25930702012-03-19 16:00:55 -05001561 rv, len, resp[2]);
1562
1563 ssif_info->max_xmit_msg_size = 32;
1564 ssif_info->max_recv_msg_size = 32;
1565 ssif_info->multi_support = SSIF_NO_MULTI;
1566 ssif_info->supports_pec = 0;
1567 }
1568
1569 /* Make sure the NMI timeout is cleared. */
1570 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1571 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
1572 msg[2] = WDT_PRE_TIMEOUT_INT;
1573 rv = do_cmd(client, 3, msg, &len, resp);
1574 if (rv || (len < 3) || (resp[2] != 0))
1575 pr_warn(PFX "Unable to clear message flags: %d %d %2.2x\n",
1576 rv, len, resp[2]);
1577
1578 /* Attempt to enable the event buffer. */
1579 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1580 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
1581 rv = do_cmd(client, 2, msg, &len, resp);
1582 if (rv || (len < 4) || (resp[2] != 0)) {
1583 pr_warn(PFX "Error getting global enables: %d %d %2.2x\n",
1584 rv, len, resp[2]);
1585 rv = 0; /* Not fatal */
1586 goto found;
1587 }
1588
Corey Minyard91620522015-04-24 07:46:06 -05001589 ssif_info->global_enables = resp[3];
1590
Corey Minyard25930702012-03-19 16:00:55 -05001591 if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
1592 ssif_info->has_event_buffer = true;
1593 /* buffer is already enabled, nothing to do. */
1594 goto found;
1595 }
1596
1597 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1598 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
Corey Minyard91620522015-04-24 07:46:06 -05001599 msg[2] = ssif_info->global_enables | IPMI_BMC_EVT_MSG_BUFF;
Corey Minyard25930702012-03-19 16:00:55 -05001600 rv = do_cmd(client, 3, msg, &len, resp);
1601 if (rv || (len < 2)) {
Corey Minyard91620522015-04-24 07:46:06 -05001602 pr_warn(PFX "Error setting global enables: %d %d %2.2x\n",
Corey Minyard25930702012-03-19 16:00:55 -05001603 rv, len, resp[2]);
1604 rv = 0; /* Not fatal */
1605 goto found;
1606 }
1607
Corey Minyard91620522015-04-24 07:46:06 -05001608 if (resp[2] == 0) {
Corey Minyard25930702012-03-19 16:00:55 -05001609 /* A successful return means the event buffer is supported. */
1610 ssif_info->has_event_buffer = true;
Corey Minyard91620522015-04-24 07:46:06 -05001611 ssif_info->global_enables |= IPMI_BMC_EVT_MSG_BUFF;
1612 }
1613
Corey Minyardbf2d0872015-08-27 15:49:18 -05001614 /* Some systems don't behave well if you enable alerts. */
1615 if (alerts_broken)
1616 goto found;
1617
Corey Minyard91620522015-04-24 07:46:06 -05001618 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1619 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1620 msg[2] = ssif_info->global_enables | IPMI_BMC_RCV_MSG_INTR;
1621 rv = do_cmd(client, 3, msg, &len, resp);
1622 if (rv || (len < 2)) {
1623 pr_warn(PFX "Error setting global enables: %d %d %2.2x\n",
1624 rv, len, resp[2]);
1625 rv = 0; /* Not fatal */
1626 goto found;
1627 }
1628
1629 if (resp[2] == 0) {
1630 /* A successful return means the alert is supported. */
1631 ssif_info->supports_alert = true;
1632 ssif_info->global_enables |= IPMI_BMC_RCV_MSG_INTR;
1633 }
Corey Minyard25930702012-03-19 16:00:55 -05001634
1635 found:
1636 ssif_info->intf_num = atomic_inc_return(&next_intf);
1637
1638 if (ssif_dbg_probe) {
1639 pr_info("ssif_probe: i2c_probe found device at i2c address %x\n",
1640 client->addr);
1641 }
1642
1643 spin_lock_init(&ssif_info->lock);
1644 ssif_info->ssif_state = SSIF_NORMAL;
1645 init_timer(&ssif_info->retry_timer);
1646 ssif_info->retry_timer.data = (unsigned long) ssif_info;
1647 ssif_info->retry_timer.function = retry_timeout;
1648
1649 for (i = 0; i < SSIF_NUM_STATS; i++)
1650 atomic_set(&ssif_info->stats[i], 0);
1651
1652 if (ssif_info->supports_pec)
1653 ssif_info->client->flags |= I2C_CLIENT_PEC;
1654
1655 ssif_info->handlers.owner = THIS_MODULE;
1656 ssif_info->handlers.start_processing = ssif_start_processing;
1657 ssif_info->handlers.get_smi_info = get_smi_info;
1658 ssif_info->handlers.sender = sender;
1659 ssif_info->handlers.request_events = request_events;
1660 ssif_info->handlers.inc_usecount = inc_usecount;
1661 ssif_info->handlers.dec_usecount = dec_usecount;
1662
1663 {
1664 unsigned int thread_num;
1665
1666 thread_num = ((ssif_info->client->adapter->nr << 8) |
1667 ssif_info->client->addr);
1668 init_completion(&ssif_info->wake_thread);
1669 ssif_info->thread = kthread_run(ipmi_ssif_thread, ssif_info,
1670 "kssif%4.4x", thread_num);
1671 if (IS_ERR(ssif_info->thread)) {
1672 rv = PTR_ERR(ssif_info->thread);
1673 dev_notice(&ssif_info->client->dev,
1674 "Could not start kernel thread: error %d\n",
1675 rv);
1676 goto out;
1677 }
1678 }
1679
1680 rv = ipmi_register_smi(&ssif_info->handlers,
1681 ssif_info,
1682 &ssif_info->device_id,
1683 &ssif_info->client->dev,
1684 slave_addr);
1685 if (rv) {
1686 pr_err(PFX "Unable to register device: error %d\n", rv);
1687 goto out;
1688 }
1689
1690 rv = ipmi_smi_add_proc_entry(ssif_info->intf, "type",
1691 &smi_type_proc_ops,
1692 ssif_info);
1693 if (rv) {
1694 pr_err(PFX "Unable to create proc entry: %d\n", rv);
1695 goto out_err_unreg;
1696 }
1697
1698 rv = ipmi_smi_add_proc_entry(ssif_info->intf, "ssif_stats",
1699 &smi_stats_proc_ops,
1700 ssif_info);
1701 if (rv) {
1702 pr_err(PFX "Unable to create proc entry: %d\n", rv);
1703 goto out_err_unreg;
1704 }
1705
1706 out:
1707 if (rv)
1708 kfree(ssif_info);
1709 kfree(resp);
1710 return rv;
1711
1712 out_err_unreg:
1713 ipmi_unregister_smi(ssif_info->intf);
1714 goto out;
1715}
1716
1717static int ssif_adapter_handler(struct device *adev, void *opaque)
1718{
1719 struct ssif_addr_info *addr_info = opaque;
1720
1721 if (adev->type != &i2c_adapter_type)
1722 return 0;
1723
1724 i2c_new_device(to_i2c_adapter(adev), &addr_info->binfo);
1725
1726 if (!addr_info->adapter_name)
1727 return 1; /* Only try the first I2C adapter by default. */
1728 return 0;
1729}
1730
1731static int new_ssif_client(int addr, char *adapter_name,
1732 int debug, int slave_addr,
1733 enum ipmi_addr_src addr_src)
1734{
1735 struct ssif_addr_info *addr_info;
1736 int rv = 0;
1737
1738 mutex_lock(&ssif_infos_mutex);
1739 if (ssif_info_find(addr, adapter_name, false)) {
1740 rv = -EEXIST;
1741 goto out_unlock;
1742 }
1743
1744 addr_info = kzalloc(sizeof(*addr_info), GFP_KERNEL);
1745 if (!addr_info) {
1746 rv = -ENOMEM;
1747 goto out_unlock;
1748 }
1749
1750 if (adapter_name) {
1751 addr_info->adapter_name = kstrdup(adapter_name, GFP_KERNEL);
1752 if (!addr_info->adapter_name) {
1753 kfree(addr_info);
1754 rv = -ENOMEM;
1755 goto out_unlock;
1756 }
1757 }
1758
1759 strncpy(addr_info->binfo.type, DEVICE_NAME,
1760 sizeof(addr_info->binfo.type));
1761 addr_info->binfo.addr = addr;
1762 addr_info->binfo.platform_data = addr_info;
1763 addr_info->debug = debug;
1764 addr_info->slave_addr = slave_addr;
1765 addr_info->addr_src = addr_src;
1766
1767 list_add_tail(&addr_info->link, &ssif_infos);
1768
1769 if (initialized)
1770 i2c_for_each_dev(addr_info, ssif_adapter_handler);
1771 /* Otherwise address list will get it */
1772
1773out_unlock:
1774 mutex_unlock(&ssif_infos_mutex);
1775 return rv;
1776}
1777
1778static void free_ssif_clients(void)
1779{
1780 struct ssif_addr_info *info, *tmp;
1781
1782 mutex_lock(&ssif_infos_mutex);
1783 list_for_each_entry_safe(info, tmp, &ssif_infos, link) {
1784 list_del(&info->link);
1785 kfree(info->adapter_name);
1786 kfree(info);
1787 }
1788 mutex_unlock(&ssif_infos_mutex);
1789}
1790
1791static unsigned short *ssif_address_list(void)
1792{
1793 struct ssif_addr_info *info;
1794 unsigned int count = 0, i;
1795 unsigned short *address_list;
1796
1797 list_for_each_entry(info, &ssif_infos, link)
1798 count++;
1799
1800 address_list = kzalloc(sizeof(*address_list) * (count + 1), GFP_KERNEL);
1801 if (!address_list)
1802 return NULL;
1803
1804 i = 0;
1805 list_for_each_entry(info, &ssif_infos, link) {
1806 unsigned short addr = info->binfo.addr;
1807 int j;
1808
1809 for (j = 0; j < i; j++) {
1810 if (address_list[j] == addr)
1811 goto skip_addr;
1812 }
1813 address_list[i] = addr;
1814skip_addr:
1815 i++;
1816 }
1817 address_list[i] = I2C_CLIENT_END;
1818
1819 return address_list;
1820}
1821
1822#ifdef CONFIG_ACPI
Mathias Krause5186cf92015-06-13 14:19:33 +02001823static const struct acpi_device_id ssif_acpi_match[] = {
Corey Minyard25930702012-03-19 16:00:55 -05001824 { "IPI0001", 0 },
1825 { },
1826};
1827MODULE_DEVICE_TABLE(acpi, ssif_acpi_match);
1828
1829/*
1830 * Once we get an ACPI failure, we don't try any more, because we go
1831 * through the tables sequentially. Once we don't find a table, there
1832 * are no more.
1833 */
1834static int acpi_failure;
1835
1836/*
1837 * Defined in the IPMI 2.0 spec.
1838 */
1839struct SPMITable {
1840 s8 Signature[4];
1841 u32 Length;
1842 u8 Revision;
1843 u8 Checksum;
1844 s8 OEMID[6];
1845 s8 OEMTableID[8];
1846 s8 OEMRevision[4];
1847 s8 CreatorID[4];
1848 s8 CreatorRevision[4];
1849 u8 InterfaceType;
1850 u8 IPMIlegacy;
1851 s16 SpecificationRevision;
1852
1853 /*
1854 * Bit 0 - SCI interrupt supported
1855 * Bit 1 - I/O APIC/SAPIC
1856 */
1857 u8 InterruptType;
1858
1859 /*
1860 * If bit 0 of InterruptType is set, then this is the SCI
1861 * interrupt in the GPEx_STS register.
1862 */
1863 u8 GPE;
1864
1865 s16 Reserved;
1866
1867 /*
1868 * If bit 1 of InterruptType is set, then this is the I/O
1869 * APIC/SAPIC interrupt.
1870 */
1871 u32 GlobalSystemInterrupt;
1872
1873 /* The actual register address. */
1874 struct acpi_generic_address addr;
1875
1876 u8 UID[4];
1877
1878 s8 spmi_id[1]; /* A '\0' terminated array starts here. */
1879};
1880
1881static int try_init_spmi(struct SPMITable *spmi)
1882{
1883 unsigned short myaddr;
1884
1885 if (num_addrs >= MAX_SSIF_BMCS)
1886 return -1;
1887
1888 if (spmi->IPMIlegacy != 1) {
1889 pr_warn("IPMI: Bad SPMI legacy: %d\n", spmi->IPMIlegacy);
1890 return -ENODEV;
1891 }
1892
1893 if (spmi->InterfaceType != 4)
1894 return -ENODEV;
1895
1896 if (spmi->addr.space_id != ACPI_ADR_SPACE_SMBUS) {
1897 pr_warn(PFX "Invalid ACPI SSIF I/O Address type: %d\n",
1898 spmi->addr.space_id);
1899 return -EIO;
1900 }
1901
Corey Minyard70f95b72016-05-06 12:57:13 -05001902 myaddr = spmi->addr.address & 0x7f;
Corey Minyard25930702012-03-19 16:00:55 -05001903
1904 return new_ssif_client(myaddr, NULL, 0, 0, SI_SPMI);
1905}
1906
1907static void spmi_find_bmc(void)
1908{
1909 acpi_status status;
1910 struct SPMITable *spmi;
1911 int i;
1912
1913 if (acpi_disabled)
1914 return;
1915
1916 if (acpi_failure)
1917 return;
1918
1919 for (i = 0; ; i++) {
1920 status = acpi_get_table(ACPI_SIG_SPMI, i+1,
1921 (struct acpi_table_header **)&spmi);
1922 if (status != AE_OK)
1923 return;
1924
1925 try_init_spmi(spmi);
1926 }
1927}
1928#else
1929static void spmi_find_bmc(void) { }
1930#endif
1931
1932#ifdef CONFIG_DMI
1933static int decode_dmi(const struct dmi_device *dmi_dev)
1934{
1935 struct dmi_header *dm = dmi_dev->device_data;
1936 u8 *data = (u8 *) dm;
1937 u8 len = dm->length;
1938 unsigned short myaddr;
1939 int slave_addr;
1940
1941 if (num_addrs >= MAX_SSIF_BMCS)
1942 return -1;
1943
1944 if (len < 9)
1945 return -1;
1946
1947 if (data[0x04] != 4) /* Not SSIF */
1948 return -1;
1949
1950 if ((data[8] >> 1) == 0) {
1951 /*
1952 * Some broken systems put the I2C address in
1953 * the slave address field. We try to
1954 * accommodate them here.
1955 */
1956 myaddr = data[6] >> 1;
1957 slave_addr = 0;
1958 } else {
1959 myaddr = data[8] >> 1;
1960 slave_addr = data[6];
1961 }
1962
Corey Minyard94671712016-11-07 12:07:09 -06001963 return new_ssif_client(myaddr, NULL, 0, slave_addr, SI_SMBIOS);
Corey Minyard25930702012-03-19 16:00:55 -05001964}
1965
1966static void dmi_iterator(void)
1967{
1968 const struct dmi_device *dev = NULL;
1969
1970 while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev)))
1971 decode_dmi(dev);
1972}
1973#else
1974static void dmi_iterator(void) { }
1975#endif
1976
1977static const struct i2c_device_id ssif_id[] = {
1978 { DEVICE_NAME, 0 },
1979 { }
1980};
1981MODULE_DEVICE_TABLE(i2c, ssif_id);
1982
1983static struct i2c_driver ssif_i2c_driver = {
1984 .class = I2C_CLASS_HWMON,
1985 .driver = {
Corey Minyard25930702012-03-19 16:00:55 -05001986 .name = DEVICE_NAME
1987 },
1988 .probe = ssif_probe,
1989 .remove = ssif_remove,
Corey Minyard91620522015-04-24 07:46:06 -05001990 .alert = ssif_alert,
Corey Minyard25930702012-03-19 16:00:55 -05001991 .id_table = ssif_id,
1992 .detect = ssif_detect
1993};
1994
1995static int init_ipmi_ssif(void)
1996{
1997 int i;
1998 int rv;
1999
2000 if (initialized)
2001 return 0;
2002
2003 pr_info("IPMI SSIF Interface driver\n");
2004
2005 /* build list for i2c from addr list */
2006 for (i = 0; i < num_addrs; i++) {
2007 rv = new_ssif_client(addr[i], adapter_name[i],
2008 dbg[i], slave_addrs[i],
2009 SI_HARDCODED);
Corey Minyardd467f7a2015-03-26 13:35:18 -05002010 if (rv)
Corey Minyard25930702012-03-19 16:00:55 -05002011 pr_err(PFX
2012 "Couldn't add hardcoded device at addr 0x%x\n",
2013 addr[i]);
2014 }
2015
2016 if (ssif_tryacpi)
2017 ssif_i2c_driver.driver.acpi_match_table =
2018 ACPI_PTR(ssif_acpi_match);
2019 if (ssif_trydmi)
2020 dmi_iterator();
2021 if (ssif_tryacpi)
2022 spmi_find_bmc();
2023
2024 ssif_i2c_driver.address_list = ssif_address_list();
2025
2026 rv = i2c_add_driver(&ssif_i2c_driver);
2027 if (!rv)
2028 initialized = true;
2029
2030 return rv;
2031}
2032module_init(init_ipmi_ssif);
2033
2034static void cleanup_ipmi_ssif(void)
2035{
2036 if (!initialized)
2037 return;
2038
2039 initialized = false;
2040
2041 i2c_del_driver(&ssif_i2c_driver);
2042
2043 free_ssif_clients();
2044}
2045module_exit(cleanup_ipmi_ssif);
2046
2047MODULE_AUTHOR("Todd C Davis <todd.c.davis@intel.com>, Corey Minyard <minyard@acm.org>");
2048MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus");
2049MODULE_LICENSE("GPL");