blob: dac36ef450ba44a3e96b79f2945a00e904e0a9db [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 Minyardcac25902018-11-16 09:59:21 -0600644 data += 2;
Corey Minyard3d69d432015-04-29 17:59:21 -0500645 for (i = 0; i < len; i++)
Corey Minyardcac25902018-11-16 09:59:21 -0600646 ssif_info->data[i] = data[i];
Corey Minyard25930702012-03-19 16:00:55 -0500647 ssif_info->multi_len = len;
648 ssif_info->multi_pos = 1;
649
650 rv = ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
651 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
652 ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
653 if (rv < 0) {
654 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
655 pr_info("Error from i2c_non_blocking_op(1)\n");
656
657 result = -EIO;
658 } else
659 return;
660 } else if (ssif_info->multi_pos) {
661 /* Middle of multi-part read. Start the next transaction. */
662 int i;
663 unsigned char blocknum;
664
665 if (len == 0) {
666 result = -EIO;
667 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
668 pr_info(PFX "Middle message with no data\n");
669
670 goto continue_op;
671 }
672
Corey Minyard3d69d432015-04-29 17:59:21 -0500673 blocknum = data[0];
Corey Minyardcac25902018-11-16 09:59:21 -0600674 len--;
675 data++;
Corey Minyard25930702012-03-19 16:00:55 -0500676
Corey Minyardcac25902018-11-16 09:59:21 -0600677 if (blocknum != 0xff && len != 31) {
678 /* All blocks but the last must have 31 data bytes. */
679 result = -EIO;
680 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
681 pr_info("Received middle message <31\n");
682
683 goto continue_op;
684 }
685
686 if (ssif_info->multi_len + len > IPMI_MAX_MSG_LENGTH) {
Corey Minyard25930702012-03-19 16:00:55 -0500687 /* Received message too big, abort the operation. */
688 result = -E2BIG;
689 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
690 pr_info("Received message too big\n");
691
692 goto continue_op;
693 }
694
Corey Minyard3d69d432015-04-29 17:59:21 -0500695 for (i = 0; i < len; i++)
Corey Minyardcac25902018-11-16 09:59:21 -0600696 ssif_info->data[i + ssif_info->multi_len] = data[i];
Corey Minyard25930702012-03-19 16:00:55 -0500697 ssif_info->multi_len += len;
698 if (blocknum == 0xff) {
699 /* End of read */
700 len = ssif_info->multi_len;
701 data = ssif_info->data;
Corey Minyardcac25902018-11-16 09:59:21 -0600702 } else if (blocknum != ssif_info->multi_pos) {
Corey Minyard25930702012-03-19 16:00:55 -0500703 /*
704 * Out of sequence block, just abort. Block
705 * numbers start at zero for the second block,
706 * but multi_pos starts at one, so the +1.
707 */
708 result = -EIO;
709 } else {
710 ssif_inc_stat(ssif_info, received_message_parts);
711
712 ssif_info->multi_pos++;
713
714 rv = ssif_i2c_send(ssif_info, msg_done_handler,
715 I2C_SMBUS_READ,
716 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
717 ssif_info->recv,
718 I2C_SMBUS_BLOCK_DATA);
719 if (rv < 0) {
720 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
721 pr_info(PFX
Corey Minyard91620522015-04-24 07:46:06 -0500722 "Error from ssif_i2c_send\n");
Corey Minyard25930702012-03-19 16:00:55 -0500723
724 result = -EIO;
725 } else
726 return;
727 }
728 }
729
Corey Minyardcac25902018-11-16 09:59:21 -0600730 continue_op:
Corey Minyard25930702012-03-19 16:00:55 -0500731 if (result < 0) {
732 ssif_inc_stat(ssif_info, receive_errors);
733 } else {
734 ssif_inc_stat(ssif_info, received_messages);
735 ssif_inc_stat(ssif_info, received_message_parts);
736 }
737
Corey Minyard25930702012-03-19 16:00:55 -0500738 if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
739 pr_info(PFX "DONE 1: state = %d, result=%d.\n",
740 ssif_info->ssif_state, result);
741
742 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
743 msg = ssif_info->curr_msg;
744 if (msg) {
745 msg->rsp_size = len;
746 if (msg->rsp_size > IPMI_MAX_MSG_LENGTH)
747 msg->rsp_size = IPMI_MAX_MSG_LENGTH;
748 memcpy(msg->rsp, data, msg->rsp_size);
749 ssif_info->curr_msg = NULL;
750 }
751
752 switch (ssif_info->ssif_state) {
753 case SSIF_NORMAL:
754 ipmi_ssif_unlock_cond(ssif_info, flags);
755 if (!msg)
756 break;
757
758 if (result < 0)
759 return_hosed_msg(ssif_info, msg);
760 else
761 deliver_recv_msg(ssif_info, msg);
762 break;
763
764 case SSIF_GETTING_FLAGS:
765 /* We got the flags from the SSIF, now handle them. */
766 if ((result < 0) || (len < 4) || (data[2] != 0)) {
767 /*
768 * Error fetching flags, or invalid length,
769 * just give up for now.
770 */
771 ssif_info->ssif_state = SSIF_NORMAL;
772 ipmi_ssif_unlock_cond(ssif_info, flags);
773 pr_warn(PFX "Error getting flags: %d %d, %x\n",
Kamlakant Patelb4cc4412018-03-13 16:32:27 +0530774 result, len, (len >= 3) ? data[2] : 0);
Corey Minyard25930702012-03-19 16:00:55 -0500775 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
776 || data[1] != IPMI_GET_MSG_FLAGS_CMD) {
Corey Minyard1b9008c2017-06-30 07:18:08 -0500777 /*
778 * Don't abort here, maybe it was a queued
779 * response to a previous command.
780 */
781 ipmi_ssif_unlock_cond(ssif_info, flags);
Corey Minyard25930702012-03-19 16:00:55 -0500782 pr_warn(PFX "Invalid response getting flags: %x %x\n",
783 data[0], data[1]);
784 } else {
785 ssif_inc_stat(ssif_info, flag_fetches);
786 ssif_info->msg_flags = data[3];
787 handle_flags(ssif_info, flags);
788 }
789 break;
790
791 case SSIF_CLEARING_FLAGS:
792 /* We cleared the flags. */
793 if ((result < 0) || (len < 3) || (data[2] != 0)) {
794 /* Error clearing flags */
795 pr_warn(PFX "Error clearing flags: %d %d, %x\n",
Kamlakant Patelb4cc4412018-03-13 16:32:27 +0530796 result, len, (len >= 3) ? data[2] : 0);
Corey Minyard25930702012-03-19 16:00:55 -0500797 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
798 || data[1] != IPMI_CLEAR_MSG_FLAGS_CMD) {
799 pr_warn(PFX "Invalid response clearing flags: %x %x\n",
800 data[0], data[1]);
801 }
802 ssif_info->ssif_state = SSIF_NORMAL;
803 ipmi_ssif_unlock_cond(ssif_info, flags);
804 break;
805
806 case SSIF_GETTING_EVENTS:
807 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
808 /* Error getting event, probably done. */
809 msg->done(msg);
810
811 /* Take off the event flag. */
812 ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
813 handle_flags(ssif_info, flags);
814 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
815 || msg->rsp[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD) {
816 pr_warn(PFX "Invalid response getting events: %x %x\n",
817 msg->rsp[0], msg->rsp[1]);
818 msg->done(msg);
819 /* Take off the event flag. */
820 ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
821 handle_flags(ssif_info, flags);
822 } else {
823 handle_flags(ssif_info, flags);
824 ssif_inc_stat(ssif_info, events);
825 deliver_recv_msg(ssif_info, msg);
826 }
827 break;
828
829 case SSIF_GETTING_MESSAGES:
830 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
831 /* Error getting event, probably done. */
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 if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
838 || msg->rsp[1] != IPMI_GET_MSG_CMD) {
839 pr_warn(PFX "Invalid response clearing flags: %x %x\n",
840 msg->rsp[0], msg->rsp[1]);
841 msg->done(msg);
842
843 /* Take off the msg flag. */
844 ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
845 handle_flags(ssif_info, flags);
846 } else {
847 ssif_inc_stat(ssif_info, incoming_messages);
848 handle_flags(ssif_info, flags);
849 deliver_recv_msg(ssif_info, msg);
850 }
851 break;
852 }
853
854 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
855 if (SSIF_IDLE(ssif_info) && !ssif_info->stopping) {
856 if (ssif_info->req_events)
857 start_event_fetch(ssif_info, flags);
858 else if (ssif_info->req_flags)
859 start_flag_fetch(ssif_info, flags);
860 else
861 start_next_msg(ssif_info, flags);
862 } else
863 ipmi_ssif_unlock_cond(ssif_info, flags);
864
865 if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
866 pr_info(PFX "DONE 2: state = %d.\n", ssif_info->ssif_state);
867}
868
869static void msg_written_handler(struct ssif_info *ssif_info, int result,
870 unsigned char *data, unsigned int len)
871{
872 int rv;
873
874 /* We are single-threaded here, so no need for a lock. */
875 if (result < 0) {
876 ssif_info->retries_left--;
877 if (ssif_info->retries_left > 0) {
878 if (!start_resend(ssif_info)) {
879 ssif_inc_stat(ssif_info, send_retries);
880 return;
881 }
882 /* request failed, just return the error. */
883 ssif_inc_stat(ssif_info, send_errors);
884
885 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
886 pr_info(PFX
887 "Out of retries in msg_written_handler\n");
888 msg_done_handler(ssif_info, -EIO, NULL, 0);
889 return;
890 }
891
892 ssif_inc_stat(ssif_info, send_errors);
893
894 /*
895 * Got an error on transmit, let the done routine
896 * handle it.
897 */
898 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
899 pr_info("Error in msg_written_handler: %d\n", result);
900
901 msg_done_handler(ssif_info, result, NULL, 0);
902 return;
903 }
904
905 if (ssif_info->multi_data) {
Corey Minyard3d69d432015-04-29 17:59:21 -0500906 /*
907 * In the middle of a multi-data write. See the comment
908 * in the SSIF_MULTI_n_PART case in the probe function
909 * for details on the intricacies of this.
910 */
Corey Minyard25930702012-03-19 16:00:55 -0500911 int left;
Joeseph Chang46ba11b2017-03-27 20:22:09 -0600912 unsigned char *data_to_send;
Corey Minyard25930702012-03-19 16:00:55 -0500913
914 ssif_inc_stat(ssif_info, sent_messages_parts);
915
916 left = ssif_info->multi_len - ssif_info->multi_pos;
917 if (left > 32)
918 left = 32;
919 /* Length byte. */
920 ssif_info->multi_data[ssif_info->multi_pos] = left;
Joeseph Chang46ba11b2017-03-27 20:22:09 -0600921 data_to_send = ssif_info->multi_data + ssif_info->multi_pos;
Corey Minyard25930702012-03-19 16:00:55 -0500922 ssif_info->multi_pos += left;
923 if (left < 32)
924 /*
925 * Write is finished. Note that we must end
926 * with a write of less than 32 bytes to
927 * complete the transaction, even if it is
928 * zero bytes.
929 */
930 ssif_info->multi_data = NULL;
931
932 rv = ssif_i2c_send(ssif_info, msg_written_handler,
933 I2C_SMBUS_WRITE,
934 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
Joeseph Chang46ba11b2017-03-27 20:22:09 -0600935 data_to_send,
Corey Minyard25930702012-03-19 16:00:55 -0500936 I2C_SMBUS_BLOCK_DATA);
937 if (rv < 0) {
938 /* request failed, just return the error. */
939 ssif_inc_stat(ssif_info, send_errors);
940
941 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
942 pr_info("Error from i2c_non_blocking_op(3)\n");
943 msg_done_handler(ssif_info, -EIO, NULL, 0);
944 }
945 } else {
Corey Minyard21c8f912016-01-06 09:32:06 -0600946 /* Ready to request the result. */
Corey Minyard91620522015-04-24 07:46:06 -0500947 unsigned long oflags, *flags;
Corey Minyard91620522015-04-24 07:46:06 -0500948
Corey Minyard25930702012-03-19 16:00:55 -0500949 ssif_inc_stat(ssif_info, sent_messages);
950 ssif_inc_stat(ssif_info, sent_messages_parts);
951
Corey Minyard91620522015-04-24 07:46:06 -0500952 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
Corey Minyard21c8f912016-01-06 09:32:06 -0600953 if (ssif_info->got_alert) {
954 /* The result is already ready, just start it. */
Corey Minyard91620522015-04-24 07:46:06 -0500955 ssif_info->got_alert = false;
Corey Minyard91620522015-04-24 07:46:06 -0500956 ipmi_ssif_unlock_cond(ssif_info, flags);
Corey Minyard21c8f912016-01-06 09:32:06 -0600957 start_get(ssif_info);
Corey Minyard91620522015-04-24 07:46:06 -0500958 } else {
959 /* Wait a jiffie then request the next message */
960 ssif_info->waiting_alert = true;
961 ssif_info->retries_left = SSIF_RECV_RETRIES;
962 ssif_info->rtc_us_timer = SSIF_MSG_PART_USEC;
Jan Glauber9aba7dd2018-10-11 12:13:01 +0200963 if (!ssif_info->stopping)
964 mod_timer(&ssif_info->retry_timer,
965 jiffies + SSIF_MSG_PART_JIFFIES);
Corey Minyard91620522015-04-24 07:46:06 -0500966 ipmi_ssif_unlock_cond(ssif_info, flags);
967 }
Corey Minyard25930702012-03-19 16:00:55 -0500968 }
969}
970
971static int start_resend(struct ssif_info *ssif_info)
972{
973 int rv;
974 int command;
975
Corey Minyard91620522015-04-24 07:46:06 -0500976 ssif_info->got_alert = false;
977
Corey Minyard25930702012-03-19 16:00:55 -0500978 if (ssif_info->data_len > 32) {
979 command = SSIF_IPMI_MULTI_PART_REQUEST_START;
980 ssif_info->multi_data = ssif_info->data;
981 ssif_info->multi_len = ssif_info->data_len;
982 /*
983 * Subtle thing, this is 32, not 33, because we will
984 * overwrite the thing at position 32 (which was just
985 * transmitted) with the new length.
986 */
987 ssif_info->multi_pos = 32;
988 ssif_info->data[0] = 32;
989 } else {
990 ssif_info->multi_data = NULL;
991 command = SSIF_IPMI_REQUEST;
992 ssif_info->data[0] = ssif_info->data_len;
993 }
994
995 rv = ssif_i2c_send(ssif_info, msg_written_handler, I2C_SMBUS_WRITE,
996 command, ssif_info->data, I2C_SMBUS_BLOCK_DATA);
997 if (rv && (ssif_info->ssif_debug & SSIF_DEBUG_MSG))
998 pr_info("Error from i2c_non_blocking_op(4)\n");
999 return rv;
1000}
1001
1002static int start_send(struct ssif_info *ssif_info,
1003 unsigned char *data,
1004 unsigned int len)
1005{
1006 if (len > IPMI_MAX_MSG_LENGTH)
1007 return -E2BIG;
1008 if (len > ssif_info->max_xmit_msg_size)
1009 return -E2BIG;
1010
1011 ssif_info->retries_left = SSIF_SEND_RETRIES;
Corey Minyard3d69d432015-04-29 17:59:21 -05001012 memcpy(ssif_info->data + 1, data, len);
Corey Minyard25930702012-03-19 16:00:55 -05001013 ssif_info->data_len = len;
1014 return start_resend(ssif_info);
1015}
1016
1017/* Must be called with the message lock held. */
1018static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags)
1019{
1020 struct ipmi_smi_msg *msg;
1021 unsigned long oflags;
1022
1023 restart:
1024 if (!SSIF_IDLE(ssif_info)) {
1025 ipmi_ssif_unlock_cond(ssif_info, flags);
1026 return;
1027 }
1028
1029 if (!ssif_info->waiting_msg) {
1030 ssif_info->curr_msg = NULL;
1031 ipmi_ssif_unlock_cond(ssif_info, flags);
1032 } else {
1033 int rv;
1034
1035 ssif_info->curr_msg = ssif_info->waiting_msg;
1036 ssif_info->waiting_msg = NULL;
1037 ipmi_ssif_unlock_cond(ssif_info, flags);
1038 rv = start_send(ssif_info,
1039 ssif_info->curr_msg->data,
1040 ssif_info->curr_msg->data_size);
1041 if (rv) {
1042 msg = ssif_info->curr_msg;
1043 ssif_info->curr_msg = NULL;
1044 return_hosed_msg(ssif_info, msg);
1045 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1046 goto restart;
1047 }
1048 }
1049}
1050
1051static void sender(void *send_info,
1052 struct ipmi_smi_msg *msg)
1053{
1054 struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1055 unsigned long oflags, *flags;
1056
1057 BUG_ON(ssif_info->waiting_msg);
1058 ssif_info->waiting_msg = msg;
1059
1060 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1061 start_next_msg(ssif_info, flags);
1062
1063 if (ssif_info->ssif_debug & SSIF_DEBUG_TIMING) {
Amitoj Kaur Chawla526290a2015-10-24 01:21:04 +05301064 struct timespec64 t;
Corey Minyard25930702012-03-19 16:00:55 -05001065
Amitoj Kaur Chawla526290a2015-10-24 01:21:04 +05301066 ktime_get_real_ts64(&t);
1067 pr_info("**Enqueue %02x %02x: %lld.%6.6ld\n",
Corey Minyard1421c932014-12-30 13:31:45 -06001068 msg->data[0], msg->data[1],
Amitoj Kaur Chawla526290a2015-10-24 01:21:04 +05301069 (long long) t.tv_sec, (long) t.tv_nsec / NSEC_PER_USEC);
Corey Minyard25930702012-03-19 16:00:55 -05001070 }
1071}
1072
1073static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1074{
1075 struct ssif_info *ssif_info = send_info;
1076
1077 data->addr_src = ssif_info->addr_source;
1078 data->dev = &ssif_info->client->dev;
1079 data->addr_info = ssif_info->addr_info;
1080 get_device(data->dev);
1081
1082 return 0;
1083}
1084
1085/*
1086 * Instead of having our own timer to periodically check the message
1087 * flags, we let the message handler drive us.
1088 */
1089static void request_events(void *send_info)
1090{
1091 struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1092 unsigned long oflags, *flags;
1093
1094 if (!ssif_info->has_event_buffer)
1095 return;
1096
1097 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1098 /*
1099 * Request flags first, not events, because the lower layer
1100 * doesn't have a way to send an attention. But make sure
1101 * event checking still happens.
1102 */
1103 ssif_info->req_events = true;
1104 if (SSIF_IDLE(ssif_info))
1105 start_flag_fetch(ssif_info, flags);
1106 else {
1107 ssif_info->req_flags = true;
1108 ipmi_ssif_unlock_cond(ssif_info, flags);
1109 }
1110}
1111
1112static int inc_usecount(void *send_info)
1113{
1114 struct ssif_info *ssif_info = send_info;
1115
1116 if (!i2c_get_adapter(ssif_info->client->adapter->nr))
1117 return -ENODEV;
1118
1119 i2c_use_client(ssif_info->client);
1120 return 0;
1121}
1122
1123static void dec_usecount(void *send_info)
1124{
1125 struct ssif_info *ssif_info = send_info;
1126
1127 i2c_release_client(ssif_info->client);
1128 i2c_put_adapter(ssif_info->client->adapter);
1129}
1130
1131static int ssif_start_processing(void *send_info,
1132 ipmi_smi_t intf)
1133{
1134 struct ssif_info *ssif_info = send_info;
1135
1136 ssif_info->intf = intf;
1137
1138 return 0;
1139}
1140
1141#define MAX_SSIF_BMCS 4
1142
1143static unsigned short addr[MAX_SSIF_BMCS];
1144static int num_addrs;
1145module_param_array(addr, ushort, &num_addrs, 0);
1146MODULE_PARM_DESC(addr, "The addresses to scan for IPMI BMCs on the SSIFs.");
1147
1148static char *adapter_name[MAX_SSIF_BMCS];
1149static int num_adapter_names;
1150module_param_array(adapter_name, charp, &num_adapter_names, 0);
1151MODULE_PARM_DESC(adapter_name, "The string name of the I2C device that has the BMC. By default all devices are scanned.");
1152
1153static int slave_addrs[MAX_SSIF_BMCS];
1154static int num_slave_addrs;
1155module_param_array(slave_addrs, int, &num_slave_addrs, 0);
1156MODULE_PARM_DESC(slave_addrs,
1157 "The default IPMB slave address for the controller.");
1158
Corey Minyardbf2d0872015-08-27 15:49:18 -05001159static bool alerts_broken;
1160module_param(alerts_broken, bool, 0);
1161MODULE_PARM_DESC(alerts_broken, "Don't enable alerts for the controller.");
1162
Corey Minyard25930702012-03-19 16:00:55 -05001163/*
1164 * Bit 0 enables message debugging, bit 1 enables state debugging, and
1165 * bit 2 enables timing debugging. This is an array indexed by
1166 * interface number"
1167 */
1168static int dbg[MAX_SSIF_BMCS];
1169static int num_dbg;
1170module_param_array(dbg, int, &num_dbg, 0);
1171MODULE_PARM_DESC(dbg, "Turn on debugging.");
1172
1173static bool ssif_dbg_probe;
1174module_param_named(dbg_probe, ssif_dbg_probe, bool, 0);
1175MODULE_PARM_DESC(dbg_probe, "Enable debugging of probing of adapters.");
1176
1177static int use_thread;
1178module_param(use_thread, int, 0);
1179MODULE_PARM_DESC(use_thread, "Use the thread interface.");
1180
Shailendra Vermafedb25e2015-05-26 00:54:57 +05301181static bool ssif_tryacpi = true;
Corey Minyard25930702012-03-19 16:00:55 -05001182module_param_named(tryacpi, ssif_tryacpi, bool, 0);
1183MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
1184
Shailendra Vermafedb25e2015-05-26 00:54:57 +05301185static bool ssif_trydmi = true;
Corey Minyard25930702012-03-19 16:00:55 -05001186module_param_named(trydmi, ssif_trydmi, bool, 0);
1187MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the default scan of the interfaces identified via DMI (SMBIOS)");
1188
1189static DEFINE_MUTEX(ssif_infos_mutex);
1190static LIST_HEAD(ssif_infos);
1191
1192static int ssif_remove(struct i2c_client *client)
1193{
1194 struct ssif_info *ssif_info = i2c_get_clientdata(client);
1195 int rv;
1196
1197 if (!ssif_info)
1198 return 0;
1199
Corey Minyard25930702012-03-19 16:00:55 -05001200 /*
1201 * After this point, we won't deliver anything asychronously
1202 * to the message handler. We can unregister ourself.
1203 */
1204 rv = ipmi_unregister_smi(ssif_info->intf);
1205 if (rv) {
1206 pr_err(PFX "Unable to unregister device: errno=%d\n", rv);
1207 return rv;
1208 }
1209 ssif_info->intf = NULL;
1210
1211 /* make sure the driver is not looking for flags any more. */
1212 while (ssif_info->ssif_state != SSIF_NORMAL)
1213 schedule_timeout(1);
1214
1215 ssif_info->stopping = true;
1216 del_timer_sync(&ssif_info->retry_timer);
1217 if (ssif_info->thread) {
1218 complete(&ssif_info->wake_thread);
1219 kthread_stop(ssif_info->thread);
1220 }
1221
1222 /*
1223 * No message can be outstanding now, we have removed the
1224 * upper layer and it permitted us to do so.
1225 */
1226 kfree(ssif_info);
1227 return 0;
1228}
1229
1230static int do_cmd(struct i2c_client *client, int len, unsigned char *msg,
1231 int *resp_len, unsigned char *resp)
1232{
1233 int retry_cnt;
1234 int ret;
1235
1236 retry_cnt = SSIF_SEND_RETRIES;
1237 retry1:
1238 ret = i2c_smbus_write_block_data(client, SSIF_IPMI_REQUEST, len, msg);
1239 if (ret) {
1240 retry_cnt--;
1241 if (retry_cnt > 0)
1242 goto retry1;
1243 return -ENODEV;
1244 }
1245
1246 ret = -ENODEV;
1247 retry_cnt = SSIF_RECV_RETRIES;
1248 while (retry_cnt > 0) {
1249 ret = i2c_smbus_read_block_data(client, SSIF_IPMI_RESPONSE,
1250 resp);
1251 if (ret > 0)
1252 break;
1253 msleep(SSIF_MSG_MSEC);
1254 retry_cnt--;
1255 if (retry_cnt <= 0)
1256 break;
1257 }
1258
1259 if (ret > 0) {
1260 /* Validate that the response is correct. */
1261 if (ret < 3 ||
1262 (resp[0] != (msg[0] | (1 << 2))) ||
1263 (resp[1] != msg[1]))
1264 ret = -EINVAL;
1265 else {
1266 *resp_len = ret;
1267 ret = 0;
1268 }
1269 }
1270
1271 return ret;
1272}
1273
1274static int ssif_detect(struct i2c_client *client, struct i2c_board_info *info)
1275{
1276 unsigned char *resp;
1277 unsigned char msg[3];
1278 int rv;
1279 int len;
1280
1281 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1282 if (!resp)
1283 return -ENOMEM;
1284
1285 /* Do a Get Device ID command, since it is required. */
1286 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1287 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1288 rv = do_cmd(client, 2, msg, &len, resp);
1289 if (rv)
1290 rv = -ENODEV;
1291 else
1292 strlcpy(info->type, DEVICE_NAME, I2C_NAME_SIZE);
1293 kfree(resp);
1294 return rv;
1295}
1296
1297static int smi_type_proc_show(struct seq_file *m, void *v)
1298{
Joe Perchesd6c5dc12015-02-17 11:10:56 -08001299 seq_puts(m, "ssif\n");
1300
Joe Perches5e33cd02015-02-22 10:21:07 -08001301 return 0;
Corey Minyard25930702012-03-19 16:00:55 -05001302}
1303
1304static int smi_type_proc_open(struct inode *inode, struct file *file)
1305{
1306 return single_open(file, smi_type_proc_show, inode->i_private);
1307}
1308
1309static const struct file_operations smi_type_proc_ops = {
1310 .open = smi_type_proc_open,
1311 .read = seq_read,
1312 .llseek = seq_lseek,
1313 .release = single_release,
1314};
1315
1316static int smi_stats_proc_show(struct seq_file *m, void *v)
1317{
1318 struct ssif_info *ssif_info = m->private;
1319
1320 seq_printf(m, "sent_messages: %u\n",
1321 ssif_get_stat(ssif_info, sent_messages));
1322 seq_printf(m, "sent_messages_parts: %u\n",
1323 ssif_get_stat(ssif_info, sent_messages_parts));
1324 seq_printf(m, "send_retries: %u\n",
1325 ssif_get_stat(ssif_info, send_retries));
1326 seq_printf(m, "send_errors: %u\n",
1327 ssif_get_stat(ssif_info, send_errors));
1328 seq_printf(m, "received_messages: %u\n",
1329 ssif_get_stat(ssif_info, received_messages));
1330 seq_printf(m, "received_message_parts: %u\n",
1331 ssif_get_stat(ssif_info, received_message_parts));
1332 seq_printf(m, "receive_retries: %u\n",
1333 ssif_get_stat(ssif_info, receive_retries));
1334 seq_printf(m, "receive_errors: %u\n",
1335 ssif_get_stat(ssif_info, receive_errors));
1336 seq_printf(m, "flag_fetches: %u\n",
1337 ssif_get_stat(ssif_info, flag_fetches));
1338 seq_printf(m, "hosed: %u\n",
1339 ssif_get_stat(ssif_info, hosed));
1340 seq_printf(m, "events: %u\n",
1341 ssif_get_stat(ssif_info, events));
1342 seq_printf(m, "watchdog_pretimeouts: %u\n",
1343 ssif_get_stat(ssif_info, watchdog_pretimeouts));
Corey Minyard91620522015-04-24 07:46:06 -05001344 seq_printf(m, "alerts: %u\n",
1345 ssif_get_stat(ssif_info, alerts));
Corey Minyard25930702012-03-19 16:00:55 -05001346 return 0;
1347}
1348
1349static int smi_stats_proc_open(struct inode *inode, struct file *file)
1350{
1351 return single_open(file, smi_stats_proc_show, PDE_DATA(inode));
1352}
1353
1354static const struct file_operations smi_stats_proc_ops = {
1355 .open = smi_stats_proc_open,
1356 .read = seq_read,
1357 .llseek = seq_lseek,
1358 .release = single_release,
1359};
1360
Corey Minyardb0e9aaa2015-03-31 12:48:53 -05001361static int strcmp_nospace(char *s1, char *s2)
1362{
1363 while (*s1 && *s2) {
1364 while (isspace(*s1))
1365 s1++;
1366 while (isspace(*s2))
1367 s2++;
1368 if (*s1 > *s2)
1369 return 1;
1370 if (*s1 < *s2)
1371 return -1;
1372 s1++;
1373 s2++;
1374 }
1375 return 0;
1376}
1377
Corey Minyard25930702012-03-19 16:00:55 -05001378static struct ssif_addr_info *ssif_info_find(unsigned short addr,
1379 char *adapter_name,
1380 bool match_null_name)
1381{
1382 struct ssif_addr_info *info, *found = NULL;
1383
1384restart:
1385 list_for_each_entry(info, &ssif_infos, link) {
1386 if (info->binfo.addr == addr) {
1387 if (info->adapter_name || adapter_name) {
1388 if (!info->adapter_name != !adapter_name) {
1389 /* One is NULL and one is not */
1390 continue;
1391 }
Corey Minyardb0e9aaa2015-03-31 12:48:53 -05001392 if (adapter_name &&
1393 strcmp_nospace(info->adapter_name,
1394 adapter_name))
1395 /* Names do not match */
Corey Minyard25930702012-03-19 16:00:55 -05001396 continue;
1397 }
1398 found = info;
1399 break;
1400 }
1401 }
1402
1403 if (!found && match_null_name) {
1404 /* Try to get an exact match first, then try with a NULL name */
1405 adapter_name = NULL;
1406 match_null_name = false;
1407 goto restart;
1408 }
1409
1410 return found;
1411}
1412
1413static bool check_acpi(struct ssif_info *ssif_info, struct device *dev)
1414{
1415#ifdef CONFIG_ACPI
1416 acpi_handle acpi_handle;
1417
1418 acpi_handle = ACPI_HANDLE(dev);
1419 if (acpi_handle) {
1420 ssif_info->addr_source = SI_ACPI;
1421 ssif_info->addr_info.acpi_info.acpi_handle = acpi_handle;
1422 return true;
1423 }
1424#endif
1425 return false;
1426}
1427
Corey Minyard91620522015-04-24 07:46:06 -05001428/*
1429 * Global enables we care about.
1430 */
1431#define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
1432 IPMI_BMC_EVT_MSG_INTR)
1433
Corey Minyard25930702012-03-19 16:00:55 -05001434static int ssif_probe(struct i2c_client *client, const struct i2c_device_id *id)
1435{
1436 unsigned char msg[3];
1437 unsigned char *resp;
1438 struct ssif_info *ssif_info;
1439 int rv = 0;
1440 int len;
1441 int i;
1442 u8 slave_addr = 0;
1443 struct ssif_addr_info *addr_info = NULL;
1444
1445
1446 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1447 if (!resp)
1448 return -ENOMEM;
1449
1450 ssif_info = kzalloc(sizeof(*ssif_info), GFP_KERNEL);
1451 if (!ssif_info) {
1452 kfree(resp);
1453 return -ENOMEM;
1454 }
1455
1456 if (!check_acpi(ssif_info, &client->dev)) {
1457 addr_info = ssif_info_find(client->addr, client->adapter->name,
1458 true);
1459 if (!addr_info) {
1460 /* Must have come in through sysfs. */
1461 ssif_info->addr_source = SI_HOTMOD;
1462 } else {
1463 ssif_info->addr_source = addr_info->addr_src;
1464 ssif_info->ssif_debug = addr_info->debug;
1465 ssif_info->addr_info = addr_info->addr_info;
1466 slave_addr = addr_info->slave_addr;
1467 }
1468 }
1469
1470 pr_info(PFX "Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
1471 ipmi_addr_src_to_str(ssif_info->addr_source),
1472 client->addr, client->adapter->name, slave_addr);
1473
1474 /*
1475 * Do a Get Device ID command, since it comes back with some
1476 * useful info.
1477 */
1478 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1479 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1480 rv = do_cmd(client, 2, msg, &len, resp);
1481 if (rv)
1482 goto out;
1483
1484 rv = ipmi_demangle_device_id(resp, len, &ssif_info->device_id);
1485 if (rv)
1486 goto out;
1487
1488 ssif_info->client = client;
1489 i2c_set_clientdata(client, ssif_info);
1490
1491 /* Now check for system interface capabilities */
1492 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1493 msg[1] = IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD;
1494 msg[2] = 0; /* SSIF */
1495 rv = do_cmd(client, 3, msg, &len, resp);
1496 if (!rv && (len >= 3) && (resp[2] == 0)) {
1497 if (len < 7) {
1498 if (ssif_dbg_probe)
1499 pr_info(PFX "SSIF info too short: %d\n", len);
1500 goto no_support;
1501 }
1502
1503 /* Got a good SSIF response, handle it. */
1504 ssif_info->max_xmit_msg_size = resp[5];
1505 ssif_info->max_recv_msg_size = resp[6];
1506 ssif_info->multi_support = (resp[4] >> 6) & 0x3;
1507 ssif_info->supports_pec = (resp[4] >> 3) & 0x1;
1508
1509 /* Sanitize the data */
1510 switch (ssif_info->multi_support) {
1511 case SSIF_NO_MULTI:
1512 if (ssif_info->max_xmit_msg_size > 32)
1513 ssif_info->max_xmit_msg_size = 32;
1514 if (ssif_info->max_recv_msg_size > 32)
1515 ssif_info->max_recv_msg_size = 32;
1516 break;
1517
1518 case SSIF_MULTI_2_PART:
Corey Minyard3d69d432015-04-29 17:59:21 -05001519 if (ssif_info->max_xmit_msg_size > 63)
1520 ssif_info->max_xmit_msg_size = 63;
Corey Minyard25930702012-03-19 16:00:55 -05001521 if (ssif_info->max_recv_msg_size > 62)
1522 ssif_info->max_recv_msg_size = 62;
1523 break;
1524
1525 case SSIF_MULTI_n_PART:
Corey Minyard3d69d432015-04-29 17:59:21 -05001526 /*
1527 * The specification is rather confusing at
1528 * this point, but I think I understand what
1529 * is meant. At least I have a workable
1530 * solution. With multi-part messages, you
1531 * cannot send a message that is a multiple of
1532 * 32-bytes in length, because the start and
1533 * middle messages are 32-bytes and the end
1534 * message must be at least one byte. You
1535 * can't fudge on an extra byte, that would
1536 * screw up things like fru data writes. So
1537 * we limit the length to 63 bytes. That way
1538 * a 32-byte message gets sent as a single
1539 * part. A larger message will be a 32-byte
1540 * start and the next message is always going
1541 * to be 1-31 bytes in length. Not ideal, but
1542 * it should work.
1543 */
1544 if (ssif_info->max_xmit_msg_size > 63)
1545 ssif_info->max_xmit_msg_size = 63;
Corey Minyard25930702012-03-19 16:00:55 -05001546 break;
1547
1548 default:
1549 /* Data is not sane, just give up. */
1550 goto no_support;
1551 }
1552 } else {
1553 no_support:
1554 /* Assume no multi-part or PEC support */
Corey Minyardb0e9aaa2015-03-31 12:48:53 -05001555 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 -05001556 rv, len, resp[2]);
1557
1558 ssif_info->max_xmit_msg_size = 32;
1559 ssif_info->max_recv_msg_size = 32;
1560 ssif_info->multi_support = SSIF_NO_MULTI;
1561 ssif_info->supports_pec = 0;
1562 }
1563
1564 /* Make sure the NMI timeout is cleared. */
1565 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1566 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
1567 msg[2] = WDT_PRE_TIMEOUT_INT;
1568 rv = do_cmd(client, 3, msg, &len, resp);
1569 if (rv || (len < 3) || (resp[2] != 0))
1570 pr_warn(PFX "Unable to clear message flags: %d %d %2.2x\n",
1571 rv, len, resp[2]);
1572
1573 /* Attempt to enable the event buffer. */
1574 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1575 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
1576 rv = do_cmd(client, 2, msg, &len, resp);
1577 if (rv || (len < 4) || (resp[2] != 0)) {
1578 pr_warn(PFX "Error getting global enables: %d %d %2.2x\n",
1579 rv, len, resp[2]);
1580 rv = 0; /* Not fatal */
1581 goto found;
1582 }
1583
Corey Minyard91620522015-04-24 07:46:06 -05001584 ssif_info->global_enables = resp[3];
1585
Corey Minyard25930702012-03-19 16:00:55 -05001586 if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
1587 ssif_info->has_event_buffer = true;
1588 /* buffer is already enabled, nothing to do. */
1589 goto found;
1590 }
1591
1592 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1593 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
Corey Minyard91620522015-04-24 07:46:06 -05001594 msg[2] = ssif_info->global_enables | IPMI_BMC_EVT_MSG_BUFF;
Corey Minyard25930702012-03-19 16:00:55 -05001595 rv = do_cmd(client, 3, msg, &len, resp);
1596 if (rv || (len < 2)) {
Corey Minyard91620522015-04-24 07:46:06 -05001597 pr_warn(PFX "Error setting global enables: %d %d %2.2x\n",
Corey Minyard25930702012-03-19 16:00:55 -05001598 rv, len, resp[2]);
1599 rv = 0; /* Not fatal */
1600 goto found;
1601 }
1602
Corey Minyard91620522015-04-24 07:46:06 -05001603 if (resp[2] == 0) {
Corey Minyard25930702012-03-19 16:00:55 -05001604 /* A successful return means the event buffer is supported. */
1605 ssif_info->has_event_buffer = true;
Corey Minyard91620522015-04-24 07:46:06 -05001606 ssif_info->global_enables |= IPMI_BMC_EVT_MSG_BUFF;
1607 }
1608
Corey Minyardbf2d0872015-08-27 15:49:18 -05001609 /* Some systems don't behave well if you enable alerts. */
1610 if (alerts_broken)
1611 goto found;
1612
Corey Minyard91620522015-04-24 07:46:06 -05001613 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1614 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1615 msg[2] = ssif_info->global_enables | IPMI_BMC_RCV_MSG_INTR;
1616 rv = do_cmd(client, 3, msg, &len, resp);
1617 if (rv || (len < 2)) {
1618 pr_warn(PFX "Error setting global enables: %d %d %2.2x\n",
1619 rv, len, resp[2]);
1620 rv = 0; /* Not fatal */
1621 goto found;
1622 }
1623
1624 if (resp[2] == 0) {
1625 /* A successful return means the alert is supported. */
1626 ssif_info->supports_alert = true;
1627 ssif_info->global_enables |= IPMI_BMC_RCV_MSG_INTR;
1628 }
Corey Minyard25930702012-03-19 16:00:55 -05001629
1630 found:
1631 ssif_info->intf_num = atomic_inc_return(&next_intf);
1632
1633 if (ssif_dbg_probe) {
1634 pr_info("ssif_probe: i2c_probe found device at i2c address %x\n",
1635 client->addr);
1636 }
1637
1638 spin_lock_init(&ssif_info->lock);
1639 ssif_info->ssif_state = SSIF_NORMAL;
1640 init_timer(&ssif_info->retry_timer);
1641 ssif_info->retry_timer.data = (unsigned long) ssif_info;
1642 ssif_info->retry_timer.function = retry_timeout;
1643
1644 for (i = 0; i < SSIF_NUM_STATS; i++)
1645 atomic_set(&ssif_info->stats[i], 0);
1646
1647 if (ssif_info->supports_pec)
1648 ssif_info->client->flags |= I2C_CLIENT_PEC;
1649
1650 ssif_info->handlers.owner = THIS_MODULE;
1651 ssif_info->handlers.start_processing = ssif_start_processing;
1652 ssif_info->handlers.get_smi_info = get_smi_info;
1653 ssif_info->handlers.sender = sender;
1654 ssif_info->handlers.request_events = request_events;
1655 ssif_info->handlers.inc_usecount = inc_usecount;
1656 ssif_info->handlers.dec_usecount = dec_usecount;
1657
1658 {
1659 unsigned int thread_num;
1660
1661 thread_num = ((ssif_info->client->adapter->nr << 8) |
1662 ssif_info->client->addr);
1663 init_completion(&ssif_info->wake_thread);
1664 ssif_info->thread = kthread_run(ipmi_ssif_thread, ssif_info,
1665 "kssif%4.4x", thread_num);
1666 if (IS_ERR(ssif_info->thread)) {
1667 rv = PTR_ERR(ssif_info->thread);
1668 dev_notice(&ssif_info->client->dev,
1669 "Could not start kernel thread: error %d\n",
1670 rv);
1671 goto out;
1672 }
1673 }
1674
1675 rv = ipmi_register_smi(&ssif_info->handlers,
1676 ssif_info,
1677 &ssif_info->device_id,
1678 &ssif_info->client->dev,
1679 slave_addr);
1680 if (rv) {
1681 pr_err(PFX "Unable to register device: error %d\n", rv);
1682 goto out;
1683 }
1684
1685 rv = ipmi_smi_add_proc_entry(ssif_info->intf, "type",
1686 &smi_type_proc_ops,
1687 ssif_info);
1688 if (rv) {
1689 pr_err(PFX "Unable to create proc entry: %d\n", rv);
1690 goto out_err_unreg;
1691 }
1692
1693 rv = ipmi_smi_add_proc_entry(ssif_info->intf, "ssif_stats",
1694 &smi_stats_proc_ops,
1695 ssif_info);
1696 if (rv) {
1697 pr_err(PFX "Unable to create proc entry: %d\n", rv);
1698 goto out_err_unreg;
1699 }
1700
1701 out:
1702 if (rv)
1703 kfree(ssif_info);
1704 kfree(resp);
1705 return rv;
1706
1707 out_err_unreg:
1708 ipmi_unregister_smi(ssif_info->intf);
1709 goto out;
1710}
1711
1712static int ssif_adapter_handler(struct device *adev, void *opaque)
1713{
1714 struct ssif_addr_info *addr_info = opaque;
1715
1716 if (adev->type != &i2c_adapter_type)
1717 return 0;
1718
1719 i2c_new_device(to_i2c_adapter(adev), &addr_info->binfo);
1720
1721 if (!addr_info->adapter_name)
1722 return 1; /* Only try the first I2C adapter by default. */
1723 return 0;
1724}
1725
1726static int new_ssif_client(int addr, char *adapter_name,
1727 int debug, int slave_addr,
1728 enum ipmi_addr_src addr_src)
1729{
1730 struct ssif_addr_info *addr_info;
1731 int rv = 0;
1732
1733 mutex_lock(&ssif_infos_mutex);
1734 if (ssif_info_find(addr, adapter_name, false)) {
1735 rv = -EEXIST;
1736 goto out_unlock;
1737 }
1738
1739 addr_info = kzalloc(sizeof(*addr_info), GFP_KERNEL);
1740 if (!addr_info) {
1741 rv = -ENOMEM;
1742 goto out_unlock;
1743 }
1744
1745 if (adapter_name) {
1746 addr_info->adapter_name = kstrdup(adapter_name, GFP_KERNEL);
1747 if (!addr_info->adapter_name) {
1748 kfree(addr_info);
1749 rv = -ENOMEM;
1750 goto out_unlock;
1751 }
1752 }
1753
1754 strncpy(addr_info->binfo.type, DEVICE_NAME,
1755 sizeof(addr_info->binfo.type));
1756 addr_info->binfo.addr = addr;
1757 addr_info->binfo.platform_data = addr_info;
1758 addr_info->debug = debug;
1759 addr_info->slave_addr = slave_addr;
1760 addr_info->addr_src = addr_src;
1761
1762 list_add_tail(&addr_info->link, &ssif_infos);
1763
1764 if (initialized)
1765 i2c_for_each_dev(addr_info, ssif_adapter_handler);
1766 /* Otherwise address list will get it */
1767
1768out_unlock:
1769 mutex_unlock(&ssif_infos_mutex);
1770 return rv;
1771}
1772
1773static void free_ssif_clients(void)
1774{
1775 struct ssif_addr_info *info, *tmp;
1776
1777 mutex_lock(&ssif_infos_mutex);
1778 list_for_each_entry_safe(info, tmp, &ssif_infos, link) {
1779 list_del(&info->link);
1780 kfree(info->adapter_name);
1781 kfree(info);
1782 }
1783 mutex_unlock(&ssif_infos_mutex);
1784}
1785
1786static unsigned short *ssif_address_list(void)
1787{
1788 struct ssif_addr_info *info;
1789 unsigned int count = 0, i;
1790 unsigned short *address_list;
1791
1792 list_for_each_entry(info, &ssif_infos, link)
1793 count++;
1794
1795 address_list = kzalloc(sizeof(*address_list) * (count + 1), GFP_KERNEL);
1796 if (!address_list)
1797 return NULL;
1798
1799 i = 0;
1800 list_for_each_entry(info, &ssif_infos, link) {
1801 unsigned short addr = info->binfo.addr;
1802 int j;
1803
1804 for (j = 0; j < i; j++) {
1805 if (address_list[j] == addr)
1806 goto skip_addr;
1807 }
1808 address_list[i] = addr;
1809skip_addr:
1810 i++;
1811 }
1812 address_list[i] = I2C_CLIENT_END;
1813
1814 return address_list;
1815}
1816
1817#ifdef CONFIG_ACPI
Mathias Krause5186cf92015-06-13 14:19:33 +02001818static const struct acpi_device_id ssif_acpi_match[] = {
Corey Minyard25930702012-03-19 16:00:55 -05001819 { "IPI0001", 0 },
1820 { },
1821};
1822MODULE_DEVICE_TABLE(acpi, ssif_acpi_match);
1823
1824/*
1825 * Once we get an ACPI failure, we don't try any more, because we go
1826 * through the tables sequentially. Once we don't find a table, there
1827 * are no more.
1828 */
1829static int acpi_failure;
1830
1831/*
1832 * Defined in the IPMI 2.0 spec.
1833 */
1834struct SPMITable {
1835 s8 Signature[4];
1836 u32 Length;
1837 u8 Revision;
1838 u8 Checksum;
1839 s8 OEMID[6];
1840 s8 OEMTableID[8];
1841 s8 OEMRevision[4];
1842 s8 CreatorID[4];
1843 s8 CreatorRevision[4];
1844 u8 InterfaceType;
1845 u8 IPMIlegacy;
1846 s16 SpecificationRevision;
1847
1848 /*
1849 * Bit 0 - SCI interrupt supported
1850 * Bit 1 - I/O APIC/SAPIC
1851 */
1852 u8 InterruptType;
1853
1854 /*
1855 * If bit 0 of InterruptType is set, then this is the SCI
1856 * interrupt in the GPEx_STS register.
1857 */
1858 u8 GPE;
1859
1860 s16 Reserved;
1861
1862 /*
1863 * If bit 1 of InterruptType is set, then this is the I/O
1864 * APIC/SAPIC interrupt.
1865 */
1866 u32 GlobalSystemInterrupt;
1867
1868 /* The actual register address. */
1869 struct acpi_generic_address addr;
1870
1871 u8 UID[4];
1872
1873 s8 spmi_id[1]; /* A '\0' terminated array starts here. */
1874};
1875
1876static int try_init_spmi(struct SPMITable *spmi)
1877{
1878 unsigned short myaddr;
1879
1880 if (num_addrs >= MAX_SSIF_BMCS)
1881 return -1;
1882
1883 if (spmi->IPMIlegacy != 1) {
1884 pr_warn("IPMI: Bad SPMI legacy: %d\n", spmi->IPMIlegacy);
1885 return -ENODEV;
1886 }
1887
1888 if (spmi->InterfaceType != 4)
1889 return -ENODEV;
1890
1891 if (spmi->addr.space_id != ACPI_ADR_SPACE_SMBUS) {
1892 pr_warn(PFX "Invalid ACPI SSIF I/O Address type: %d\n",
1893 spmi->addr.space_id);
1894 return -EIO;
1895 }
1896
Corey Minyard70f95b72016-05-06 12:57:13 -05001897 myaddr = spmi->addr.address & 0x7f;
Corey Minyard25930702012-03-19 16:00:55 -05001898
1899 return new_ssif_client(myaddr, NULL, 0, 0, SI_SPMI);
1900}
1901
1902static void spmi_find_bmc(void)
1903{
1904 acpi_status status;
1905 struct SPMITable *spmi;
1906 int i;
1907
1908 if (acpi_disabled)
1909 return;
1910
1911 if (acpi_failure)
1912 return;
1913
1914 for (i = 0; ; i++) {
1915 status = acpi_get_table(ACPI_SIG_SPMI, i+1,
1916 (struct acpi_table_header **)&spmi);
1917 if (status != AE_OK)
1918 return;
1919
1920 try_init_spmi(spmi);
1921 }
1922}
1923#else
1924static void spmi_find_bmc(void) { }
1925#endif
1926
1927#ifdef CONFIG_DMI
1928static int decode_dmi(const struct dmi_device *dmi_dev)
1929{
1930 struct dmi_header *dm = dmi_dev->device_data;
1931 u8 *data = (u8 *) dm;
1932 u8 len = dm->length;
1933 unsigned short myaddr;
1934 int slave_addr;
1935
1936 if (num_addrs >= MAX_SSIF_BMCS)
1937 return -1;
1938
1939 if (len < 9)
1940 return -1;
1941
1942 if (data[0x04] != 4) /* Not SSIF */
1943 return -1;
1944
1945 if ((data[8] >> 1) == 0) {
1946 /*
1947 * Some broken systems put the I2C address in
1948 * the slave address field. We try to
1949 * accommodate them here.
1950 */
1951 myaddr = data[6] >> 1;
1952 slave_addr = 0;
1953 } else {
1954 myaddr = data[8] >> 1;
1955 slave_addr = data[6];
1956 }
1957
1958 return new_ssif_client(myaddr, NULL, 0, 0, SI_SMBIOS);
1959}
1960
1961static void dmi_iterator(void)
1962{
1963 const struct dmi_device *dev = NULL;
1964
1965 while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev)))
1966 decode_dmi(dev);
1967}
1968#else
1969static void dmi_iterator(void) { }
1970#endif
1971
1972static const struct i2c_device_id ssif_id[] = {
1973 { DEVICE_NAME, 0 },
1974 { }
1975};
1976MODULE_DEVICE_TABLE(i2c, ssif_id);
1977
1978static struct i2c_driver ssif_i2c_driver = {
1979 .class = I2C_CLASS_HWMON,
1980 .driver = {
Corey Minyard25930702012-03-19 16:00:55 -05001981 .name = DEVICE_NAME
1982 },
1983 .probe = ssif_probe,
1984 .remove = ssif_remove,
Corey Minyard91620522015-04-24 07:46:06 -05001985 .alert = ssif_alert,
Corey Minyard25930702012-03-19 16:00:55 -05001986 .id_table = ssif_id,
1987 .detect = ssif_detect
1988};
1989
1990static int init_ipmi_ssif(void)
1991{
1992 int i;
1993 int rv;
1994
1995 if (initialized)
1996 return 0;
1997
1998 pr_info("IPMI SSIF Interface driver\n");
1999
2000 /* build list for i2c from addr list */
2001 for (i = 0; i < num_addrs; i++) {
2002 rv = new_ssif_client(addr[i], adapter_name[i],
2003 dbg[i], slave_addrs[i],
2004 SI_HARDCODED);
Corey Minyardd467f7a2015-03-26 13:35:18 -05002005 if (rv)
Corey Minyard25930702012-03-19 16:00:55 -05002006 pr_err(PFX
2007 "Couldn't add hardcoded device at addr 0x%x\n",
2008 addr[i]);
2009 }
2010
2011 if (ssif_tryacpi)
2012 ssif_i2c_driver.driver.acpi_match_table =
2013 ACPI_PTR(ssif_acpi_match);
2014 if (ssif_trydmi)
2015 dmi_iterator();
2016 if (ssif_tryacpi)
2017 spmi_find_bmc();
2018
2019 ssif_i2c_driver.address_list = ssif_address_list();
2020
2021 rv = i2c_add_driver(&ssif_i2c_driver);
2022 if (!rv)
2023 initialized = true;
2024
2025 return rv;
2026}
2027module_init(init_ipmi_ssif);
2028
2029static void cleanup_ipmi_ssif(void)
2030{
2031 if (!initialized)
2032 return;
2033
2034 initialized = false;
2035
2036 i2c_del_driver(&ssif_i2c_driver);
2037
2038 free_ssif_clients();
2039}
2040module_exit(cleanup_ipmi_ssif);
2041
2042MODULE_AUTHOR("Todd C Davis <todd.c.davis@intel.com>, Corey Minyard <minyard@acm.org>");
2043MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus");
2044MODULE_LICENSE("GPL");