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