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