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