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