blob: 90e6246622572966a9037350e8fdabed743d7961 [file] [log] [blame]
Corey Minyard25930702012-03-19 16:00:55 -05001/*
2 * ipmi_ssif.c
3 *
4 * The interface to the IPMI driver for SMBus access to a SMBus
5 * compliant device. Called SSIF by the IPMI spec.
6 *
7 * Author: Intel Corporation
8 * Todd Davis <todd.c.davis@intel.com>
9 *
10 * Rewritten by Corey Minyard <minyard@acm.org> to support the
11 * non-blocking I2C interface, add support for multi-part
12 * transactions, add PEC support, and general clenaup.
13 *
14 * Copyright 2003 Intel Corporation
15 * Copyright 2005 MontaVista Software
16 *
17 * This program is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the
19 * Free Software Foundation; either version 2 of the License, or (at your
20 * option) any later version.
21 */
22
23/*
24 * This file holds the "policy" for the interface to the SSIF state
25 * machine. It does the configuration, handles timers and interrupts,
26 * and drives the real SSIF state machine.
27 */
28
29/*
30 * TODO: Figure out how to use SMB alerts. This will require a new
31 * interface into the I2C driver, I believe.
32 */
33
Corey Minyard25930702012-03-19 16:00:55 -050034#if defined(MODVERSIONS)
35#include <linux/modversions.h>
36#endif
37
38#include <linux/module.h>
39#include <linux/moduleparam.h>
40#include <linux/sched.h>
41#include <linux/seq_file.h>
42#include <linux/timer.h>
43#include <linux/delay.h>
44#include <linux/errno.h>
45#include <linux/spinlock.h>
46#include <linux/slab.h>
47#include <linux/list.h>
48#include <linux/i2c.h>
49#include <linux/ipmi_smi.h>
50#include <linux/init.h>
51#include <linux/dmi.h>
52#include <linux/kthread.h>
53#include <linux/acpi.h>
Corey Minyarde3fe1422014-12-16 08:36:32 -060054#include <linux/ctype.h>
Amitoj Kaur Chawla526290a2015-10-24 01:21:04 +053055#include <linux/time64.h>
Corey Minyard25930702012-03-19 16:00:55 -050056
57#define PFX "ipmi_ssif: "
58#define DEVICE_NAME "ipmi_ssif"
59
60#define IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD 0x57
61
62#define SSIF_IPMI_REQUEST 2
63#define SSIF_IPMI_MULTI_PART_REQUEST_START 6
64#define SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE 7
65#define SSIF_IPMI_RESPONSE 3
66#define SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE 9
67
68/* ssif_debug is a bit-field
69 * SSIF_DEBUG_MSG - commands and their responses
70 * SSIF_DEBUG_STATES - message states
71 * SSIF_DEBUG_TIMING - Measure times between events in the driver
72 */
73#define SSIF_DEBUG_TIMING 4
74#define SSIF_DEBUG_STATE 2
75#define SSIF_DEBUG_MSG 1
76#define SSIF_NODEBUG 0
77#define SSIF_DEFAULT_DEBUG (SSIF_NODEBUG)
78
79/*
80 * Timer values
81 */
82#define SSIF_MSG_USEC 20000 /* 20ms between message tries. */
83#define SSIF_MSG_PART_USEC 5000 /* 5ms for a message part */
84
85/* How many times to we retry sending/receiving the message. */
86#define SSIF_SEND_RETRIES 5
87#define SSIF_RECV_RETRIES 250
88
89#define SSIF_MSG_MSEC (SSIF_MSG_USEC / 1000)
90#define SSIF_MSG_JIFFIES ((SSIF_MSG_USEC * 1000) / TICK_NSEC)
91#define SSIF_MSG_PART_JIFFIES ((SSIF_MSG_PART_USEC * 1000) / TICK_NSEC)
92
93enum ssif_intf_state {
94 SSIF_NORMAL,
95 SSIF_GETTING_FLAGS,
96 SSIF_GETTING_EVENTS,
97 SSIF_CLEARING_FLAGS,
98 SSIF_GETTING_MESSAGES,
99 /* FIXME - add watchdog stuff. */
100};
101
102#define SSIF_IDLE(ssif) ((ssif)->ssif_state == SSIF_NORMAL \
103 && (ssif)->curr_msg == NULL)
104
105/*
106 * Indexes into stats[] in ssif_info below.
107 */
108enum ssif_stat_indexes {
109 /* Number of total messages sent. */
110 SSIF_STAT_sent_messages = 0,
111
112 /*
113 * Number of message parts sent. Messages may be broken into
114 * parts if they are long.
115 */
116 SSIF_STAT_sent_messages_parts,
117
118 /*
119 * Number of time a message was retried.
120 */
121 SSIF_STAT_send_retries,
122
123 /*
124 * Number of times the send of a message failed.
125 */
126 SSIF_STAT_send_errors,
127
128 /*
129 * Number of message responses received.
130 */
131 SSIF_STAT_received_messages,
132
133 /*
134 * Number of message fragments received.
135 */
136 SSIF_STAT_received_message_parts,
137
138 /*
139 * Number of times the receive of a message was retried.
140 */
141 SSIF_STAT_receive_retries,
142
143 /*
144 * Number of errors receiving messages.
145 */
146 SSIF_STAT_receive_errors,
147
148 /*
149 * Number of times a flag fetch was requested.
150 */
151 SSIF_STAT_flag_fetches,
152
153 /*
154 * Number of times the hardware didn't follow the state machine.
155 */
156 SSIF_STAT_hosed,
157
158 /*
159 * Number of received events.
160 */
161 SSIF_STAT_events,
162
163 /* Number of asyncronous messages received. */
164 SSIF_STAT_incoming_messages,
165
166 /* Number of watchdog pretimeouts. */
167 SSIF_STAT_watchdog_pretimeouts,
168
Corey Minyard91620522015-04-24 07:46:06 -0500169 /* Number of alers received. */
170 SSIF_STAT_alerts,
171
Corey Minyard25930702012-03-19 16:00:55 -0500172 /* Always add statistics before this value, it must be last. */
173 SSIF_NUM_STATS
174};
175
176struct ssif_addr_info {
177 unsigned short addr;
178 struct i2c_board_info binfo;
179 char *adapter_name;
180 int debug;
181 int slave_addr;
182 enum ipmi_addr_src addr_src;
183 union ipmi_smi_info_union addr_info;
184
185 struct mutex clients_mutex;
186 struct list_head clients;
187
188 struct list_head link;
189};
190
191struct ssif_info;
192
193typedef void (*ssif_i2c_done)(struct ssif_info *ssif_info, int result,
194 unsigned char *data, unsigned int len);
195
196struct ssif_info {
197 ipmi_smi_t intf;
198 int intf_num;
199 spinlock_t lock;
200 struct ipmi_smi_msg *waiting_msg;
201 struct ipmi_smi_msg *curr_msg;
202 enum ssif_intf_state ssif_state;
203 unsigned long ssif_debug;
204
205 struct ipmi_smi_handlers handlers;
206
207 enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */
208 union ipmi_smi_info_union addr_info;
209
210 /*
211 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
212 * is set to hold the flags until we are done handling everything
213 * from the flags.
214 */
215#define RECEIVE_MSG_AVAIL 0x01
216#define EVENT_MSG_BUFFER_FULL 0x02
217#define WDT_PRE_TIMEOUT_INT 0x08
218 unsigned char msg_flags;
219
Corey Minyard91620522015-04-24 07:46:06 -0500220 u8 global_enables;
Corey Minyard25930702012-03-19 16:00:55 -0500221 bool has_event_buffer;
Corey Minyard91620522015-04-24 07:46:06 -0500222 bool supports_alert;
223
224 /*
225 * Used to tell what we should do with alerts. If we are
226 * waiting on a response, read the data immediately.
227 */
228 bool got_alert;
229 bool waiting_alert;
Corey Minyard25930702012-03-19 16:00:55 -0500230
231 /*
232 * If set to true, this will request events the next time the
233 * state machine is idle.
234 */
235 bool req_events;
236
237 /*
238 * If set to true, this will request flags the next time the
239 * state machine is idle.
240 */
241 bool req_flags;
242
243 /*
244 * Used to perform timer operations when run-to-completion
245 * mode is on. This is a countdown timer.
246 */
247 int rtc_us_timer;
248
249 /* Used for sending/receiving data. +1 for the length. */
250 unsigned char data[IPMI_MAX_MSG_LENGTH + 1];
251 unsigned int data_len;
252
253 /* Temp receive buffer, gets copied into data. */
254 unsigned char recv[I2C_SMBUS_BLOCK_MAX];
255
256 struct i2c_client *client;
257 ssif_i2c_done done_handler;
258
259 /* Thread interface handling */
260 struct task_struct *thread;
261 struct completion wake_thread;
262 bool stopping;
263 int i2c_read_write;
264 int i2c_command;
265 unsigned char *i2c_data;
266 unsigned int i2c_size;
267
268 /* From the device id response. */
269 struct ipmi_device_id device_id;
270
271 struct timer_list retry_timer;
272 int retries_left;
273
274 /* Info from SSIF cmd */
275 unsigned char max_xmit_msg_size;
276 unsigned char max_recv_msg_size;
277 unsigned int multi_support;
278 int supports_pec;
279
280#define SSIF_NO_MULTI 0
281#define SSIF_MULTI_2_PART 1
282#define SSIF_MULTI_n_PART 2
283 unsigned char *multi_data;
284 unsigned int multi_len;
285 unsigned int multi_pos;
286
287 atomic_t stats[SSIF_NUM_STATS];
288};
289
290#define ssif_inc_stat(ssif, stat) \
291 atomic_inc(&(ssif)->stats[SSIF_STAT_ ## stat])
292#define ssif_get_stat(ssif, stat) \
293 ((unsigned int) atomic_read(&(ssif)->stats[SSIF_STAT_ ## stat]))
294
295static bool initialized;
296
297static atomic_t next_intf = ATOMIC_INIT(0);
298
299static void return_hosed_msg(struct ssif_info *ssif_info,
300 struct ipmi_smi_msg *msg);
301static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags);
302static int start_send(struct ssif_info *ssif_info,
303 unsigned char *data,
304 unsigned int len);
305
306static unsigned long *ipmi_ssif_lock_cond(struct ssif_info *ssif_info,
307 unsigned long *flags)
308{
309 spin_lock_irqsave(&ssif_info->lock, *flags);
310 return flags;
311}
312
313static void ipmi_ssif_unlock_cond(struct ssif_info *ssif_info,
314 unsigned long *flags)
315{
316 spin_unlock_irqrestore(&ssif_info->lock, *flags);
317}
318
319static void deliver_recv_msg(struct ssif_info *ssif_info,
320 struct ipmi_smi_msg *msg)
321{
322 ipmi_smi_t intf = ssif_info->intf;
323
324 if (!intf) {
325 ipmi_free_smi_msg(msg);
326 } else if (msg->rsp_size < 0) {
327 return_hosed_msg(ssif_info, msg);
328 pr_err(PFX
329 "Malformed message in deliver_recv_msg: rsp_size = %d\n",
330 msg->rsp_size);
331 } else {
332 ipmi_smi_msg_received(intf, msg);
333 }
334}
335
336static void return_hosed_msg(struct ssif_info *ssif_info,
337 struct ipmi_smi_msg *msg)
338{
339 ssif_inc_stat(ssif_info, hosed);
340
341 /* Make it a response */
342 msg->rsp[0] = msg->data[0] | 4;
343 msg->rsp[1] = msg->data[1];
344 msg->rsp[2] = 0xFF; /* Unknown error. */
345 msg->rsp_size = 3;
346
347 deliver_recv_msg(ssif_info, msg);
348}
349
350/*
351 * Must be called with the message lock held. This will release the
352 * message lock. Note that the caller will check SSIF_IDLE and start a
353 * new operation, so there is no need to check for new messages to
354 * start in here.
355 */
356static void start_clear_flags(struct ssif_info *ssif_info, unsigned long *flags)
357{
358 unsigned char msg[3];
359
360 ssif_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
361 ssif_info->ssif_state = SSIF_CLEARING_FLAGS;
362 ipmi_ssif_unlock_cond(ssif_info, flags);
363
364 /* Make sure the watchdog pre-timeout flag is not set at startup. */
365 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
366 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
367 msg[2] = WDT_PRE_TIMEOUT_INT;
368
369 if (start_send(ssif_info, msg, 3) != 0) {
370 /* Error, just go to normal state. */
371 ssif_info->ssif_state = SSIF_NORMAL;
372 }
373}
374
375static void start_flag_fetch(struct ssif_info *ssif_info, unsigned long *flags)
376{
377 unsigned char mb[2];
378
379 ssif_info->req_flags = false;
380 ssif_info->ssif_state = SSIF_GETTING_FLAGS;
381 ipmi_ssif_unlock_cond(ssif_info, flags);
382
383 mb[0] = (IPMI_NETFN_APP_REQUEST << 2);
384 mb[1] = IPMI_GET_MSG_FLAGS_CMD;
385 if (start_send(ssif_info, mb, 2) != 0)
386 ssif_info->ssif_state = SSIF_NORMAL;
387}
388
389static void check_start_send(struct ssif_info *ssif_info, unsigned long *flags,
390 struct ipmi_smi_msg *msg)
391{
392 if (start_send(ssif_info, msg->data, msg->data_size) != 0) {
393 unsigned long oflags;
394
395 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
396 ssif_info->curr_msg = NULL;
397 ssif_info->ssif_state = SSIF_NORMAL;
398 ipmi_ssif_unlock_cond(ssif_info, flags);
399 ipmi_free_smi_msg(msg);
400 }
401}
402
403static void start_event_fetch(struct ssif_info *ssif_info, unsigned long *flags)
404{
405 struct ipmi_smi_msg *msg;
406
407 ssif_info->req_events = false;
408
409 msg = ipmi_alloc_smi_msg();
410 if (!msg) {
411 ssif_info->ssif_state = SSIF_NORMAL;
412 return;
413 }
414
415 ssif_info->curr_msg = msg;
416 ssif_info->ssif_state = SSIF_GETTING_EVENTS;
417 ipmi_ssif_unlock_cond(ssif_info, flags);
418
419 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
420 msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
421 msg->data_size = 2;
422
423 check_start_send(ssif_info, flags, msg);
424}
425
426static void start_recv_msg_fetch(struct ssif_info *ssif_info,
427 unsigned long *flags)
428{
429 struct ipmi_smi_msg *msg;
430
431 msg = ipmi_alloc_smi_msg();
432 if (!msg) {
433 ssif_info->ssif_state = SSIF_NORMAL;
434 return;
435 }
436
437 ssif_info->curr_msg = msg;
438 ssif_info->ssif_state = SSIF_GETTING_MESSAGES;
439 ipmi_ssif_unlock_cond(ssif_info, flags);
440
441 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
442 msg->data[1] = IPMI_GET_MSG_CMD;
443 msg->data_size = 2;
444
445 check_start_send(ssif_info, flags, msg);
446}
447
448/*
449 * Must be called with the message lock held. This will release the
450 * message lock. Note that the caller will check SSIF_IDLE and start a
451 * new operation, so there is no need to check for new messages to
452 * start in here.
453 */
454static void handle_flags(struct ssif_info *ssif_info, unsigned long *flags)
455{
456 if (ssif_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
457 ipmi_smi_t intf = ssif_info->intf;
458 /* Watchdog pre-timeout */
459 ssif_inc_stat(ssif_info, watchdog_pretimeouts);
460 start_clear_flags(ssif_info, flags);
461 if (intf)
462 ipmi_smi_watchdog_pretimeout(intf);
463 } else if (ssif_info->msg_flags & RECEIVE_MSG_AVAIL)
464 /* Messages available. */
465 start_recv_msg_fetch(ssif_info, flags);
466 else if (ssif_info->msg_flags & EVENT_MSG_BUFFER_FULL)
467 /* Events available. */
468 start_event_fetch(ssif_info, flags);
469 else {
470 ssif_info->ssif_state = SSIF_NORMAL;
471 ipmi_ssif_unlock_cond(ssif_info, flags);
472 }
473}
474
475static int ipmi_ssif_thread(void *data)
476{
477 struct ssif_info *ssif_info = data;
478
479 while (!kthread_should_stop()) {
480 int result;
481
482 /* Wait for something to do */
Corey Minyardd0acf732015-04-04 01:54:26 -0500483 result = wait_for_completion_interruptible(
484 &ssif_info->wake_thread);
Corey Minyard25930702012-03-19 16:00:55 -0500485 if (ssif_info->stopping)
486 break;
Corey Minyardd0acf732015-04-04 01:54:26 -0500487 if (result == -ERESTARTSYS)
488 continue;
489 init_completion(&ssif_info->wake_thread);
Corey Minyard25930702012-03-19 16:00:55 -0500490
491 if (ssif_info->i2c_read_write == I2C_SMBUS_WRITE) {
492 result = i2c_smbus_write_block_data(
Corey Minyard3d69d432015-04-29 17:59:21 -0500493 ssif_info->client, ssif_info->i2c_command,
Corey Minyard25930702012-03-19 16:00:55 -0500494 ssif_info->i2c_data[0],
495 ssif_info->i2c_data + 1);
496 ssif_info->done_handler(ssif_info, result, NULL, 0);
497 } else {
498 result = i2c_smbus_read_block_data(
Corey Minyard3d69d432015-04-29 17:59:21 -0500499 ssif_info->client, ssif_info->i2c_command,
Corey Minyard25930702012-03-19 16:00:55 -0500500 ssif_info->i2c_data);
501 if (result < 0)
502 ssif_info->done_handler(ssif_info, result,
503 NULL, 0);
504 else
505 ssif_info->done_handler(ssif_info, 0,
506 ssif_info->i2c_data,
507 result);
508 }
509 }
510
511 return 0;
512}
513
514static int ssif_i2c_send(struct ssif_info *ssif_info,
515 ssif_i2c_done handler,
516 int read_write, int command,
517 unsigned char *data, unsigned int size)
518{
519 ssif_info->done_handler = handler;
520
521 ssif_info->i2c_read_write = read_write;
522 ssif_info->i2c_command = command;
523 ssif_info->i2c_data = data;
524 ssif_info->i2c_size = size;
525 complete(&ssif_info->wake_thread);
526 return 0;
527}
528
529
530static void msg_done_handler(struct ssif_info *ssif_info, int result,
531 unsigned char *data, unsigned int len);
532
Corey Minyard91620522015-04-24 07:46:06 -0500533static void start_get(struct ssif_info *ssif_info)
Corey Minyard25930702012-03-19 16:00:55 -0500534{
Corey Minyard25930702012-03-19 16:00:55 -0500535 int rv;
536
Corey Minyard25930702012-03-19 16:00:55 -0500537 ssif_info->rtc_us_timer = 0;
Corey Minyard3d69d432015-04-29 17:59:21 -0500538 ssif_info->multi_pos = 0;
Corey Minyard25930702012-03-19 16:00:55 -0500539
540 rv = ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
541 SSIF_IPMI_RESPONSE,
542 ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
543 if (rv < 0) {
544 /* request failed, just return the error. */
545 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
546 pr_info("Error from i2c_non_blocking_op(5)\n");
547
548 msg_done_handler(ssif_info, -EIO, NULL, 0);
549 }
550}
551
Corey Minyard91620522015-04-24 07:46:06 -0500552static void retry_timeout(unsigned long data)
553{
554 struct ssif_info *ssif_info = (void *) data;
555 unsigned long oflags, *flags;
556 bool waiting;
557
558 if (ssif_info->stopping)
559 return;
560
561 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
562 waiting = ssif_info->waiting_alert;
563 ssif_info->waiting_alert = false;
564 ipmi_ssif_unlock_cond(ssif_info, flags);
565
566 if (waiting)
567 start_get(ssif_info);
568}
569
570
571static void ssif_alert(struct i2c_client *client, unsigned int data)
572{
573 struct ssif_info *ssif_info = i2c_get_clientdata(client);
574 unsigned long oflags, *flags;
575 bool do_get = false;
576
577 ssif_inc_stat(ssif_info, alerts);
578
579 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
580 if (ssif_info->waiting_alert) {
581 ssif_info->waiting_alert = false;
582 del_timer(&ssif_info->retry_timer);
583 do_get = true;
584 } else if (ssif_info->curr_msg) {
585 ssif_info->got_alert = true;
586 }
587 ipmi_ssif_unlock_cond(ssif_info, flags);
588 if (do_get)
589 start_get(ssif_info);
590}
591
Corey Minyard25930702012-03-19 16:00:55 -0500592static int start_resend(struct ssif_info *ssif_info);
593
594static void msg_done_handler(struct ssif_info *ssif_info, int result,
595 unsigned char *data, unsigned int len)
596{
597 struct ipmi_smi_msg *msg;
598 unsigned long oflags, *flags;
599 int rv;
600
601 /*
602 * We are single-threaded here, so no need for a lock until we
603 * start messing with driver states or the queues.
604 */
605
606 if (result < 0) {
607 ssif_info->retries_left--;
608 if (ssif_info->retries_left > 0) {
609 ssif_inc_stat(ssif_info, receive_retries);
610
Corey Minyard91620522015-04-24 07:46:06 -0500611 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
612 ssif_info->waiting_alert = true;
613 ssif_info->rtc_us_timer = SSIF_MSG_USEC;
Corey Minyard25930702012-03-19 16:00:55 -0500614 mod_timer(&ssif_info->retry_timer,
615 jiffies + SSIF_MSG_JIFFIES);
Corey Minyard91620522015-04-24 07:46:06 -0500616 ipmi_ssif_unlock_cond(ssif_info, flags);
Corey Minyard25930702012-03-19 16:00:55 -0500617 return;
618 }
619
620 ssif_inc_stat(ssif_info, receive_errors);
621
622 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
623 pr_info("Error in msg_done_handler: %d\n", result);
624 len = 0;
625 goto continue_op;
626 }
627
628 if ((len > 1) && (ssif_info->multi_pos == 0)
629 && (data[0] == 0x00) && (data[1] == 0x01)) {
630 /* Start of multi-part read. Start the next transaction. */
631 int i;
632
633 ssif_inc_stat(ssif_info, received_message_parts);
634
635 /* Remove the multi-part read marker. */
Corey Minyard25930702012-03-19 16:00:55 -0500636 len -= 2;
Corey Minyard3d69d432015-04-29 17:59:21 -0500637 for (i = 0; i < len; i++)
638 ssif_info->data[i] = data[i+2];
Corey Minyard25930702012-03-19 16:00:55 -0500639 ssif_info->multi_len = len;
640 ssif_info->multi_pos = 1;
641
642 rv = ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
643 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
644 ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
645 if (rv < 0) {
646 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
647 pr_info("Error from i2c_non_blocking_op(1)\n");
648
649 result = -EIO;
650 } else
651 return;
652 } else if (ssif_info->multi_pos) {
653 /* Middle of multi-part read. Start the next transaction. */
654 int i;
655 unsigned char blocknum;
656
657 if (len == 0) {
658 result = -EIO;
659 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
660 pr_info(PFX "Middle message with no data\n");
661
662 goto continue_op;
663 }
664
Corey Minyard3d69d432015-04-29 17:59:21 -0500665 blocknum = data[0];
Corey Minyard25930702012-03-19 16:00:55 -0500666
Corey Minyard3d69d432015-04-29 17:59:21 -0500667 if (ssif_info->multi_len + len - 1 > IPMI_MAX_MSG_LENGTH) {
Corey Minyard25930702012-03-19 16:00:55 -0500668 /* Received message too big, abort the operation. */
669 result = -E2BIG;
670 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
671 pr_info("Received message too big\n");
672
673 goto continue_op;
674 }
675
676 /* Remove the blocknum from the data. */
Corey Minyard25930702012-03-19 16:00:55 -0500677 len--;
Corey Minyard3d69d432015-04-29 17:59:21 -0500678 for (i = 0; i < len; i++)
679 ssif_info->data[i + ssif_info->multi_len] = data[i + 1];
Corey Minyard25930702012-03-19 16:00:55 -0500680 ssif_info->multi_len += len;
681 if (blocknum == 0xff) {
682 /* End of read */
683 len = ssif_info->multi_len;
684 data = ssif_info->data;
Corey Minyard3d69d432015-04-29 17:59:21 -0500685 } else if (blocknum + 1 != ssif_info->multi_pos) {
Corey Minyard25930702012-03-19 16:00:55 -0500686 /*
687 * Out of sequence block, just abort. Block
688 * numbers start at zero for the second block,
689 * but multi_pos starts at one, so the +1.
690 */
691 result = -EIO;
692 } else {
693 ssif_inc_stat(ssif_info, received_message_parts);
694
695 ssif_info->multi_pos++;
696
697 rv = ssif_i2c_send(ssif_info, msg_done_handler,
698 I2C_SMBUS_READ,
699 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
700 ssif_info->recv,
701 I2C_SMBUS_BLOCK_DATA);
702 if (rv < 0) {
703 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
704 pr_info(PFX
Corey Minyard91620522015-04-24 07:46:06 -0500705 "Error from ssif_i2c_send\n");
Corey Minyard25930702012-03-19 16:00:55 -0500706
707 result = -EIO;
708 } else
709 return;
710 }
711 }
712
713 if (result < 0) {
714 ssif_inc_stat(ssif_info, receive_errors);
715 } else {
716 ssif_inc_stat(ssif_info, received_messages);
717 ssif_inc_stat(ssif_info, received_message_parts);
718 }
719
720
721 continue_op:
722 if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
723 pr_info(PFX "DONE 1: state = %d, result=%d.\n",
724 ssif_info->ssif_state, result);
725
726 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
727 msg = ssif_info->curr_msg;
728 if (msg) {
729 msg->rsp_size = len;
730 if (msg->rsp_size > IPMI_MAX_MSG_LENGTH)
731 msg->rsp_size = IPMI_MAX_MSG_LENGTH;
732 memcpy(msg->rsp, data, msg->rsp_size);
733 ssif_info->curr_msg = NULL;
734 }
735
736 switch (ssif_info->ssif_state) {
737 case SSIF_NORMAL:
738 ipmi_ssif_unlock_cond(ssif_info, flags);
739 if (!msg)
740 break;
741
742 if (result < 0)
743 return_hosed_msg(ssif_info, msg);
744 else
745 deliver_recv_msg(ssif_info, msg);
746 break;
747
748 case SSIF_GETTING_FLAGS:
749 /* We got the flags from the SSIF, now handle them. */
750 if ((result < 0) || (len < 4) || (data[2] != 0)) {
751 /*
752 * Error fetching flags, or invalid length,
753 * just give up for now.
754 */
755 ssif_info->ssif_state = SSIF_NORMAL;
756 ipmi_ssif_unlock_cond(ssif_info, flags);
757 pr_warn(PFX "Error getting flags: %d %d, %x\n",
758 result, len, data[2]);
759 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
760 || data[1] != IPMI_GET_MSG_FLAGS_CMD) {
761 pr_warn(PFX "Invalid response getting flags: %x %x\n",
762 data[0], data[1]);
763 } else {
764 ssif_inc_stat(ssif_info, flag_fetches);
765 ssif_info->msg_flags = data[3];
766 handle_flags(ssif_info, flags);
767 }
768 break;
769
770 case SSIF_CLEARING_FLAGS:
771 /* We cleared the flags. */
772 if ((result < 0) || (len < 3) || (data[2] != 0)) {
773 /* Error clearing flags */
774 pr_warn(PFX "Error clearing flags: %d %d, %x\n",
775 result, len, data[2]);
776 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
777 || data[1] != IPMI_CLEAR_MSG_FLAGS_CMD) {
778 pr_warn(PFX "Invalid response clearing flags: %x %x\n",
779 data[0], data[1]);
780 }
781 ssif_info->ssif_state = SSIF_NORMAL;
782 ipmi_ssif_unlock_cond(ssif_info, flags);
783 break;
784
785 case SSIF_GETTING_EVENTS:
786 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
787 /* Error getting event, probably done. */
788 msg->done(msg);
789
790 /* Take off the event flag. */
791 ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
792 handle_flags(ssif_info, flags);
793 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
794 || msg->rsp[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD) {
795 pr_warn(PFX "Invalid response getting events: %x %x\n",
796 msg->rsp[0], msg->rsp[1]);
797 msg->done(msg);
798 /* Take off the event flag. */
799 ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
800 handle_flags(ssif_info, flags);
801 } else {
802 handle_flags(ssif_info, flags);
803 ssif_inc_stat(ssif_info, events);
804 deliver_recv_msg(ssif_info, msg);
805 }
806 break;
807
808 case SSIF_GETTING_MESSAGES:
809 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
810 /* Error getting event, probably done. */
811 msg->done(msg);
812
813 /* Take off the msg flag. */
814 ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
815 handle_flags(ssif_info, flags);
816 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
817 || msg->rsp[1] != IPMI_GET_MSG_CMD) {
818 pr_warn(PFX "Invalid response clearing flags: %x %x\n",
819 msg->rsp[0], msg->rsp[1]);
820 msg->done(msg);
821
822 /* Take off the msg flag. */
823 ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
824 handle_flags(ssif_info, flags);
825 } else {
826 ssif_inc_stat(ssif_info, incoming_messages);
827 handle_flags(ssif_info, flags);
828 deliver_recv_msg(ssif_info, msg);
829 }
830 break;
831 }
832
833 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
834 if (SSIF_IDLE(ssif_info) && !ssif_info->stopping) {
835 if (ssif_info->req_events)
836 start_event_fetch(ssif_info, flags);
837 else if (ssif_info->req_flags)
838 start_flag_fetch(ssif_info, flags);
839 else
840 start_next_msg(ssif_info, flags);
841 } else
842 ipmi_ssif_unlock_cond(ssif_info, flags);
843
844 if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
845 pr_info(PFX "DONE 2: state = %d.\n", ssif_info->ssif_state);
846}
847
848static void msg_written_handler(struct ssif_info *ssif_info, int result,
849 unsigned char *data, unsigned int len)
850{
851 int rv;
852
853 /* We are single-threaded here, so no need for a lock. */
854 if (result < 0) {
855 ssif_info->retries_left--;
856 if (ssif_info->retries_left > 0) {
857 if (!start_resend(ssif_info)) {
858 ssif_inc_stat(ssif_info, send_retries);
859 return;
860 }
861 /* request failed, just return the error. */
862 ssif_inc_stat(ssif_info, send_errors);
863
864 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
865 pr_info(PFX
866 "Out of retries in msg_written_handler\n");
867 msg_done_handler(ssif_info, -EIO, NULL, 0);
868 return;
869 }
870
871 ssif_inc_stat(ssif_info, send_errors);
872
873 /*
874 * Got an error on transmit, let the done routine
875 * handle it.
876 */
877 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
878 pr_info("Error in msg_written_handler: %d\n", result);
879
880 msg_done_handler(ssif_info, result, NULL, 0);
881 return;
882 }
883
884 if (ssif_info->multi_data) {
Corey Minyard3d69d432015-04-29 17:59:21 -0500885 /*
886 * In the middle of a multi-data write. See the comment
887 * in the SSIF_MULTI_n_PART case in the probe function
888 * for details on the intricacies of this.
889 */
Corey Minyard25930702012-03-19 16:00:55 -0500890 int left;
891
892 ssif_inc_stat(ssif_info, sent_messages_parts);
893
894 left = ssif_info->multi_len - ssif_info->multi_pos;
895 if (left > 32)
896 left = 32;
897 /* Length byte. */
898 ssif_info->multi_data[ssif_info->multi_pos] = left;
899 ssif_info->multi_pos += left;
900 if (left < 32)
901 /*
902 * Write is finished. Note that we must end
903 * with a write of less than 32 bytes to
904 * complete the transaction, even if it is
905 * zero bytes.
906 */
907 ssif_info->multi_data = NULL;
908
909 rv = ssif_i2c_send(ssif_info, msg_written_handler,
910 I2C_SMBUS_WRITE,
911 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
912 ssif_info->multi_data + ssif_info->multi_pos,
913 I2C_SMBUS_BLOCK_DATA);
914 if (rv < 0) {
915 /* request failed, just return the error. */
916 ssif_inc_stat(ssif_info, send_errors);
917
918 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
919 pr_info("Error from i2c_non_blocking_op(3)\n");
920 msg_done_handler(ssif_info, -EIO, NULL, 0);
921 }
922 } else {
Corey Minyard91620522015-04-24 07:46:06 -0500923 unsigned long oflags, *flags;
924 bool got_alert;
925
Corey Minyard25930702012-03-19 16:00:55 -0500926 ssif_inc_stat(ssif_info, sent_messages);
927 ssif_inc_stat(ssif_info, sent_messages_parts);
928
Corey Minyard91620522015-04-24 07:46:06 -0500929 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
930 got_alert = ssif_info->got_alert;
931 if (got_alert) {
932 ssif_info->got_alert = false;
933 ssif_info->waiting_alert = false;
934 }
935
936 if (got_alert) {
937 ipmi_ssif_unlock_cond(ssif_info, flags);
938 /* The alert already happened, try now. */
939 retry_timeout((unsigned long) ssif_info);
940 } else {
941 /* Wait a jiffie then request the next message */
942 ssif_info->waiting_alert = true;
943 ssif_info->retries_left = SSIF_RECV_RETRIES;
944 ssif_info->rtc_us_timer = SSIF_MSG_PART_USEC;
945 mod_timer(&ssif_info->retry_timer,
946 jiffies + SSIF_MSG_PART_JIFFIES);
947 ipmi_ssif_unlock_cond(ssif_info, flags);
948 }
Corey Minyard25930702012-03-19 16:00:55 -0500949 }
950}
951
952static int start_resend(struct ssif_info *ssif_info)
953{
954 int rv;
955 int command;
956
Corey Minyard91620522015-04-24 07:46:06 -0500957 ssif_info->got_alert = false;
958
Corey Minyard25930702012-03-19 16:00:55 -0500959 if (ssif_info->data_len > 32) {
960 command = SSIF_IPMI_MULTI_PART_REQUEST_START;
961 ssif_info->multi_data = ssif_info->data;
962 ssif_info->multi_len = ssif_info->data_len;
963 /*
964 * Subtle thing, this is 32, not 33, because we will
965 * overwrite the thing at position 32 (which was just
966 * transmitted) with the new length.
967 */
968 ssif_info->multi_pos = 32;
969 ssif_info->data[0] = 32;
970 } else {
971 ssif_info->multi_data = NULL;
972 command = SSIF_IPMI_REQUEST;
973 ssif_info->data[0] = ssif_info->data_len;
974 }
975
976 rv = ssif_i2c_send(ssif_info, msg_written_handler, I2C_SMBUS_WRITE,
977 command, ssif_info->data, I2C_SMBUS_BLOCK_DATA);
978 if (rv && (ssif_info->ssif_debug & SSIF_DEBUG_MSG))
979 pr_info("Error from i2c_non_blocking_op(4)\n");
980 return rv;
981}
982
983static int start_send(struct ssif_info *ssif_info,
984 unsigned char *data,
985 unsigned int len)
986{
987 if (len > IPMI_MAX_MSG_LENGTH)
988 return -E2BIG;
989 if (len > ssif_info->max_xmit_msg_size)
990 return -E2BIG;
991
992 ssif_info->retries_left = SSIF_SEND_RETRIES;
Corey Minyard3d69d432015-04-29 17:59:21 -0500993 memcpy(ssif_info->data + 1, data, len);
Corey Minyard25930702012-03-19 16:00:55 -0500994 ssif_info->data_len = len;
995 return start_resend(ssif_info);
996}
997
998/* Must be called with the message lock held. */
999static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags)
1000{
1001 struct ipmi_smi_msg *msg;
1002 unsigned long oflags;
1003
1004 restart:
1005 if (!SSIF_IDLE(ssif_info)) {
1006 ipmi_ssif_unlock_cond(ssif_info, flags);
1007 return;
1008 }
1009
1010 if (!ssif_info->waiting_msg) {
1011 ssif_info->curr_msg = NULL;
1012 ipmi_ssif_unlock_cond(ssif_info, flags);
1013 } else {
1014 int rv;
1015
1016 ssif_info->curr_msg = ssif_info->waiting_msg;
1017 ssif_info->waiting_msg = NULL;
1018 ipmi_ssif_unlock_cond(ssif_info, flags);
1019 rv = start_send(ssif_info,
1020 ssif_info->curr_msg->data,
1021 ssif_info->curr_msg->data_size);
1022 if (rv) {
1023 msg = ssif_info->curr_msg;
1024 ssif_info->curr_msg = NULL;
1025 return_hosed_msg(ssif_info, msg);
1026 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1027 goto restart;
1028 }
1029 }
1030}
1031
1032static void sender(void *send_info,
1033 struct ipmi_smi_msg *msg)
1034{
1035 struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1036 unsigned long oflags, *flags;
1037
1038 BUG_ON(ssif_info->waiting_msg);
1039 ssif_info->waiting_msg = msg;
1040
1041 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1042 start_next_msg(ssif_info, flags);
1043
1044 if (ssif_info->ssif_debug & SSIF_DEBUG_TIMING) {
Amitoj Kaur Chawla526290a2015-10-24 01:21:04 +05301045 struct timespec64 t;
Corey Minyard25930702012-03-19 16:00:55 -05001046
Amitoj Kaur Chawla526290a2015-10-24 01:21:04 +05301047 ktime_get_real_ts64(&t);
1048 pr_info("**Enqueue %02x %02x: %lld.%6.6ld\n",
Corey Minyard1421c932014-12-30 13:31:45 -06001049 msg->data[0], msg->data[1],
Amitoj Kaur Chawla526290a2015-10-24 01:21:04 +05301050 (long long) t.tv_sec, (long) t.tv_nsec / NSEC_PER_USEC);
Corey Minyard25930702012-03-19 16:00:55 -05001051 }
1052}
1053
1054static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1055{
1056 struct ssif_info *ssif_info = send_info;
1057
1058 data->addr_src = ssif_info->addr_source;
1059 data->dev = &ssif_info->client->dev;
1060 data->addr_info = ssif_info->addr_info;
1061 get_device(data->dev);
1062
1063 return 0;
1064}
1065
1066/*
1067 * Instead of having our own timer to periodically check the message
1068 * flags, we let the message handler drive us.
1069 */
1070static void request_events(void *send_info)
1071{
1072 struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1073 unsigned long oflags, *flags;
1074
1075 if (!ssif_info->has_event_buffer)
1076 return;
1077
1078 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1079 /*
1080 * Request flags first, not events, because the lower layer
1081 * doesn't have a way to send an attention. But make sure
1082 * event checking still happens.
1083 */
1084 ssif_info->req_events = true;
1085 if (SSIF_IDLE(ssif_info))
1086 start_flag_fetch(ssif_info, flags);
1087 else {
1088 ssif_info->req_flags = true;
1089 ipmi_ssif_unlock_cond(ssif_info, flags);
1090 }
1091}
1092
1093static int inc_usecount(void *send_info)
1094{
1095 struct ssif_info *ssif_info = send_info;
1096
1097 if (!i2c_get_adapter(ssif_info->client->adapter->nr))
1098 return -ENODEV;
1099
1100 i2c_use_client(ssif_info->client);
1101 return 0;
1102}
1103
1104static void dec_usecount(void *send_info)
1105{
1106 struct ssif_info *ssif_info = send_info;
1107
1108 i2c_release_client(ssif_info->client);
1109 i2c_put_adapter(ssif_info->client->adapter);
1110}
1111
1112static int ssif_start_processing(void *send_info,
1113 ipmi_smi_t intf)
1114{
1115 struct ssif_info *ssif_info = send_info;
1116
1117 ssif_info->intf = intf;
1118
1119 return 0;
1120}
1121
1122#define MAX_SSIF_BMCS 4
1123
1124static unsigned short addr[MAX_SSIF_BMCS];
1125static int num_addrs;
1126module_param_array(addr, ushort, &num_addrs, 0);
1127MODULE_PARM_DESC(addr, "The addresses to scan for IPMI BMCs on the SSIFs.");
1128
1129static char *adapter_name[MAX_SSIF_BMCS];
1130static int num_adapter_names;
1131module_param_array(adapter_name, charp, &num_adapter_names, 0);
1132MODULE_PARM_DESC(adapter_name, "The string name of the I2C device that has the BMC. By default all devices are scanned.");
1133
1134static int slave_addrs[MAX_SSIF_BMCS];
1135static int num_slave_addrs;
1136module_param_array(slave_addrs, int, &num_slave_addrs, 0);
1137MODULE_PARM_DESC(slave_addrs,
1138 "The default IPMB slave address for the controller.");
1139
Corey Minyardbf2d0872015-08-27 15:49:18 -05001140static bool alerts_broken;
1141module_param(alerts_broken, bool, 0);
1142MODULE_PARM_DESC(alerts_broken, "Don't enable alerts for the controller.");
1143
Corey Minyard25930702012-03-19 16:00:55 -05001144/*
1145 * Bit 0 enables message debugging, bit 1 enables state debugging, and
1146 * bit 2 enables timing debugging. This is an array indexed by
1147 * interface number"
1148 */
1149static int dbg[MAX_SSIF_BMCS];
1150static int num_dbg;
1151module_param_array(dbg, int, &num_dbg, 0);
1152MODULE_PARM_DESC(dbg, "Turn on debugging.");
1153
1154static bool ssif_dbg_probe;
1155module_param_named(dbg_probe, ssif_dbg_probe, bool, 0);
1156MODULE_PARM_DESC(dbg_probe, "Enable debugging of probing of adapters.");
1157
1158static int use_thread;
1159module_param(use_thread, int, 0);
1160MODULE_PARM_DESC(use_thread, "Use the thread interface.");
1161
Shailendra Vermafedb25e2015-05-26 00:54:57 +05301162static bool ssif_tryacpi = true;
Corey Minyard25930702012-03-19 16:00:55 -05001163module_param_named(tryacpi, ssif_tryacpi, bool, 0);
1164MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
1165
Shailendra Vermafedb25e2015-05-26 00:54:57 +05301166static bool ssif_trydmi = true;
Corey Minyard25930702012-03-19 16:00:55 -05001167module_param_named(trydmi, ssif_trydmi, bool, 0);
1168MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the default scan of the interfaces identified via DMI (SMBIOS)");
1169
1170static DEFINE_MUTEX(ssif_infos_mutex);
1171static LIST_HEAD(ssif_infos);
1172
1173static int ssif_remove(struct i2c_client *client)
1174{
1175 struct ssif_info *ssif_info = i2c_get_clientdata(client);
1176 int rv;
1177
1178 if (!ssif_info)
1179 return 0;
1180
Corey Minyard25930702012-03-19 16:00:55 -05001181 /*
1182 * After this point, we won't deliver anything asychronously
1183 * to the message handler. We can unregister ourself.
1184 */
1185 rv = ipmi_unregister_smi(ssif_info->intf);
1186 if (rv) {
1187 pr_err(PFX "Unable to unregister device: errno=%d\n", rv);
1188 return rv;
1189 }
1190 ssif_info->intf = NULL;
1191
1192 /* make sure the driver is not looking for flags any more. */
1193 while (ssif_info->ssif_state != SSIF_NORMAL)
1194 schedule_timeout(1);
1195
1196 ssif_info->stopping = true;
1197 del_timer_sync(&ssif_info->retry_timer);
1198 if (ssif_info->thread) {
1199 complete(&ssif_info->wake_thread);
1200 kthread_stop(ssif_info->thread);
1201 }
1202
1203 /*
1204 * No message can be outstanding now, we have removed the
1205 * upper layer and it permitted us to do so.
1206 */
1207 kfree(ssif_info);
1208 return 0;
1209}
1210
1211static int do_cmd(struct i2c_client *client, int len, unsigned char *msg,
1212 int *resp_len, unsigned char *resp)
1213{
1214 int retry_cnt;
1215 int ret;
1216
1217 retry_cnt = SSIF_SEND_RETRIES;
1218 retry1:
1219 ret = i2c_smbus_write_block_data(client, SSIF_IPMI_REQUEST, len, msg);
1220 if (ret) {
1221 retry_cnt--;
1222 if (retry_cnt > 0)
1223 goto retry1;
1224 return -ENODEV;
1225 }
1226
1227 ret = -ENODEV;
1228 retry_cnt = SSIF_RECV_RETRIES;
1229 while (retry_cnt > 0) {
1230 ret = i2c_smbus_read_block_data(client, SSIF_IPMI_RESPONSE,
1231 resp);
1232 if (ret > 0)
1233 break;
1234 msleep(SSIF_MSG_MSEC);
1235 retry_cnt--;
1236 if (retry_cnt <= 0)
1237 break;
1238 }
1239
1240 if (ret > 0) {
1241 /* Validate that the response is correct. */
1242 if (ret < 3 ||
1243 (resp[0] != (msg[0] | (1 << 2))) ||
1244 (resp[1] != msg[1]))
1245 ret = -EINVAL;
1246 else {
1247 *resp_len = ret;
1248 ret = 0;
1249 }
1250 }
1251
1252 return ret;
1253}
1254
1255static int ssif_detect(struct i2c_client *client, struct i2c_board_info *info)
1256{
1257 unsigned char *resp;
1258 unsigned char msg[3];
1259 int rv;
1260 int len;
1261
1262 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1263 if (!resp)
1264 return -ENOMEM;
1265
1266 /* Do a Get Device ID command, since it is required. */
1267 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1268 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1269 rv = do_cmd(client, 2, msg, &len, resp);
1270 if (rv)
1271 rv = -ENODEV;
1272 else
1273 strlcpy(info->type, DEVICE_NAME, I2C_NAME_SIZE);
1274 kfree(resp);
1275 return rv;
1276}
1277
1278static int smi_type_proc_show(struct seq_file *m, void *v)
1279{
Joe Perchesd6c5dc12015-02-17 11:10:56 -08001280 seq_puts(m, "ssif\n");
1281
Joe Perches5e33cd02015-02-22 10:21:07 -08001282 return 0;
Corey Minyard25930702012-03-19 16:00:55 -05001283}
1284
1285static int smi_type_proc_open(struct inode *inode, struct file *file)
1286{
1287 return single_open(file, smi_type_proc_show, inode->i_private);
1288}
1289
1290static const struct file_operations smi_type_proc_ops = {
1291 .open = smi_type_proc_open,
1292 .read = seq_read,
1293 .llseek = seq_lseek,
1294 .release = single_release,
1295};
1296
1297static int smi_stats_proc_show(struct seq_file *m, void *v)
1298{
1299 struct ssif_info *ssif_info = m->private;
1300
1301 seq_printf(m, "sent_messages: %u\n",
1302 ssif_get_stat(ssif_info, sent_messages));
1303 seq_printf(m, "sent_messages_parts: %u\n",
1304 ssif_get_stat(ssif_info, sent_messages_parts));
1305 seq_printf(m, "send_retries: %u\n",
1306 ssif_get_stat(ssif_info, send_retries));
1307 seq_printf(m, "send_errors: %u\n",
1308 ssif_get_stat(ssif_info, send_errors));
1309 seq_printf(m, "received_messages: %u\n",
1310 ssif_get_stat(ssif_info, received_messages));
1311 seq_printf(m, "received_message_parts: %u\n",
1312 ssif_get_stat(ssif_info, received_message_parts));
1313 seq_printf(m, "receive_retries: %u\n",
1314 ssif_get_stat(ssif_info, receive_retries));
1315 seq_printf(m, "receive_errors: %u\n",
1316 ssif_get_stat(ssif_info, receive_errors));
1317 seq_printf(m, "flag_fetches: %u\n",
1318 ssif_get_stat(ssif_info, flag_fetches));
1319 seq_printf(m, "hosed: %u\n",
1320 ssif_get_stat(ssif_info, hosed));
1321 seq_printf(m, "events: %u\n",
1322 ssif_get_stat(ssif_info, events));
1323 seq_printf(m, "watchdog_pretimeouts: %u\n",
1324 ssif_get_stat(ssif_info, watchdog_pretimeouts));
Corey Minyard91620522015-04-24 07:46:06 -05001325 seq_printf(m, "alerts: %u\n",
1326 ssif_get_stat(ssif_info, alerts));
Corey Minyard25930702012-03-19 16:00:55 -05001327 return 0;
1328}
1329
1330static int smi_stats_proc_open(struct inode *inode, struct file *file)
1331{
1332 return single_open(file, smi_stats_proc_show, PDE_DATA(inode));
1333}
1334
1335static const struct file_operations smi_stats_proc_ops = {
1336 .open = smi_stats_proc_open,
1337 .read = seq_read,
1338 .llseek = seq_lseek,
1339 .release = single_release,
1340};
1341
Corey Minyardb0e9aaa2015-03-31 12:48:53 -05001342static int strcmp_nospace(char *s1, char *s2)
1343{
1344 while (*s1 && *s2) {
1345 while (isspace(*s1))
1346 s1++;
1347 while (isspace(*s2))
1348 s2++;
1349 if (*s1 > *s2)
1350 return 1;
1351 if (*s1 < *s2)
1352 return -1;
1353 s1++;
1354 s2++;
1355 }
1356 return 0;
1357}
1358
Corey Minyard25930702012-03-19 16:00:55 -05001359static struct ssif_addr_info *ssif_info_find(unsigned short addr,
1360 char *adapter_name,
1361 bool match_null_name)
1362{
1363 struct ssif_addr_info *info, *found = NULL;
1364
1365restart:
1366 list_for_each_entry(info, &ssif_infos, link) {
1367 if (info->binfo.addr == addr) {
1368 if (info->adapter_name || adapter_name) {
1369 if (!info->adapter_name != !adapter_name) {
1370 /* One is NULL and one is not */
1371 continue;
1372 }
Corey Minyardb0e9aaa2015-03-31 12:48:53 -05001373 if (adapter_name &&
1374 strcmp_nospace(info->adapter_name,
1375 adapter_name))
1376 /* Names do not match */
Corey Minyard25930702012-03-19 16:00:55 -05001377 continue;
1378 }
1379 found = info;
1380 break;
1381 }
1382 }
1383
1384 if (!found && match_null_name) {
1385 /* Try to get an exact match first, then try with a NULL name */
1386 adapter_name = NULL;
1387 match_null_name = false;
1388 goto restart;
1389 }
1390
1391 return found;
1392}
1393
1394static bool check_acpi(struct ssif_info *ssif_info, struct device *dev)
1395{
1396#ifdef CONFIG_ACPI
1397 acpi_handle acpi_handle;
1398
1399 acpi_handle = ACPI_HANDLE(dev);
1400 if (acpi_handle) {
1401 ssif_info->addr_source = SI_ACPI;
1402 ssif_info->addr_info.acpi_info.acpi_handle = acpi_handle;
1403 return true;
1404 }
1405#endif
1406 return false;
1407}
1408
Corey Minyard91620522015-04-24 07:46:06 -05001409/*
1410 * Global enables we care about.
1411 */
1412#define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
1413 IPMI_BMC_EVT_MSG_INTR)
1414
Corey Minyard25930702012-03-19 16:00:55 -05001415static int ssif_probe(struct i2c_client *client, const struct i2c_device_id *id)
1416{
1417 unsigned char msg[3];
1418 unsigned char *resp;
1419 struct ssif_info *ssif_info;
1420 int rv = 0;
1421 int len;
1422 int i;
1423 u8 slave_addr = 0;
1424 struct ssif_addr_info *addr_info = NULL;
1425
1426
1427 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1428 if (!resp)
1429 return -ENOMEM;
1430
1431 ssif_info = kzalloc(sizeof(*ssif_info), GFP_KERNEL);
1432 if (!ssif_info) {
1433 kfree(resp);
1434 return -ENOMEM;
1435 }
1436
1437 if (!check_acpi(ssif_info, &client->dev)) {
1438 addr_info = ssif_info_find(client->addr, client->adapter->name,
1439 true);
1440 if (!addr_info) {
1441 /* Must have come in through sysfs. */
1442 ssif_info->addr_source = SI_HOTMOD;
1443 } else {
1444 ssif_info->addr_source = addr_info->addr_src;
1445 ssif_info->ssif_debug = addr_info->debug;
1446 ssif_info->addr_info = addr_info->addr_info;
1447 slave_addr = addr_info->slave_addr;
1448 }
1449 }
1450
1451 pr_info(PFX "Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
1452 ipmi_addr_src_to_str(ssif_info->addr_source),
1453 client->addr, client->adapter->name, slave_addr);
1454
1455 /*
1456 * Do a Get Device ID command, since it comes back with some
1457 * useful info.
1458 */
1459 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1460 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1461 rv = do_cmd(client, 2, msg, &len, resp);
1462 if (rv)
1463 goto out;
1464
1465 rv = ipmi_demangle_device_id(resp, len, &ssif_info->device_id);
1466 if (rv)
1467 goto out;
1468
1469 ssif_info->client = client;
1470 i2c_set_clientdata(client, ssif_info);
1471
1472 /* Now check for system interface capabilities */
1473 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1474 msg[1] = IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD;
1475 msg[2] = 0; /* SSIF */
1476 rv = do_cmd(client, 3, msg, &len, resp);
1477 if (!rv && (len >= 3) && (resp[2] == 0)) {
1478 if (len < 7) {
1479 if (ssif_dbg_probe)
1480 pr_info(PFX "SSIF info too short: %d\n", len);
1481 goto no_support;
1482 }
1483
1484 /* Got a good SSIF response, handle it. */
1485 ssif_info->max_xmit_msg_size = resp[5];
1486 ssif_info->max_recv_msg_size = resp[6];
1487 ssif_info->multi_support = (resp[4] >> 6) & 0x3;
1488 ssif_info->supports_pec = (resp[4] >> 3) & 0x1;
1489
1490 /* Sanitize the data */
1491 switch (ssif_info->multi_support) {
1492 case SSIF_NO_MULTI:
1493 if (ssif_info->max_xmit_msg_size > 32)
1494 ssif_info->max_xmit_msg_size = 32;
1495 if (ssif_info->max_recv_msg_size > 32)
1496 ssif_info->max_recv_msg_size = 32;
1497 break;
1498
1499 case SSIF_MULTI_2_PART:
Corey Minyard3d69d432015-04-29 17:59:21 -05001500 if (ssif_info->max_xmit_msg_size > 63)
1501 ssif_info->max_xmit_msg_size = 63;
Corey Minyard25930702012-03-19 16:00:55 -05001502 if (ssif_info->max_recv_msg_size > 62)
1503 ssif_info->max_recv_msg_size = 62;
1504 break;
1505
1506 case SSIF_MULTI_n_PART:
Corey Minyard3d69d432015-04-29 17:59:21 -05001507 /*
1508 * The specification is rather confusing at
1509 * this point, but I think I understand what
1510 * is meant. At least I have a workable
1511 * solution. With multi-part messages, you
1512 * cannot send a message that is a multiple of
1513 * 32-bytes in length, because the start and
1514 * middle messages are 32-bytes and the end
1515 * message must be at least one byte. You
1516 * can't fudge on an extra byte, that would
1517 * screw up things like fru data writes. So
1518 * we limit the length to 63 bytes. That way
1519 * a 32-byte message gets sent as a single
1520 * part. A larger message will be a 32-byte
1521 * start and the next message is always going
1522 * to be 1-31 bytes in length. Not ideal, but
1523 * it should work.
1524 */
1525 if (ssif_info->max_xmit_msg_size > 63)
1526 ssif_info->max_xmit_msg_size = 63;
Corey Minyard25930702012-03-19 16:00:55 -05001527 break;
1528
1529 default:
1530 /* Data is not sane, just give up. */
1531 goto no_support;
1532 }
1533 } else {
1534 no_support:
1535 /* Assume no multi-part or PEC support */
Corey Minyardb0e9aaa2015-03-31 12:48:53 -05001536 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 -05001537 rv, len, resp[2]);
1538
1539 ssif_info->max_xmit_msg_size = 32;
1540 ssif_info->max_recv_msg_size = 32;
1541 ssif_info->multi_support = SSIF_NO_MULTI;
1542 ssif_info->supports_pec = 0;
1543 }
1544
1545 /* Make sure the NMI timeout is cleared. */
1546 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1547 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
1548 msg[2] = WDT_PRE_TIMEOUT_INT;
1549 rv = do_cmd(client, 3, msg, &len, resp);
1550 if (rv || (len < 3) || (resp[2] != 0))
1551 pr_warn(PFX "Unable to clear message flags: %d %d %2.2x\n",
1552 rv, len, resp[2]);
1553
1554 /* Attempt to enable the event buffer. */
1555 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1556 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
1557 rv = do_cmd(client, 2, msg, &len, resp);
1558 if (rv || (len < 4) || (resp[2] != 0)) {
1559 pr_warn(PFX "Error getting global enables: %d %d %2.2x\n",
1560 rv, len, resp[2]);
1561 rv = 0; /* Not fatal */
1562 goto found;
1563 }
1564
Corey Minyard91620522015-04-24 07:46:06 -05001565 ssif_info->global_enables = resp[3];
1566
Corey Minyard25930702012-03-19 16:00:55 -05001567 if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
1568 ssif_info->has_event_buffer = true;
1569 /* buffer is already enabled, nothing to do. */
1570 goto found;
1571 }
1572
1573 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1574 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
Corey Minyard91620522015-04-24 07:46:06 -05001575 msg[2] = ssif_info->global_enables | IPMI_BMC_EVT_MSG_BUFF;
Corey Minyard25930702012-03-19 16:00:55 -05001576 rv = do_cmd(client, 3, msg, &len, resp);
1577 if (rv || (len < 2)) {
Corey Minyard91620522015-04-24 07:46:06 -05001578 pr_warn(PFX "Error setting global enables: %d %d %2.2x\n",
Corey Minyard25930702012-03-19 16:00:55 -05001579 rv, len, resp[2]);
1580 rv = 0; /* Not fatal */
1581 goto found;
1582 }
1583
Corey Minyard91620522015-04-24 07:46:06 -05001584 if (resp[2] == 0) {
Corey Minyard25930702012-03-19 16:00:55 -05001585 /* A successful return means the event buffer is supported. */
1586 ssif_info->has_event_buffer = true;
Corey Minyard91620522015-04-24 07:46:06 -05001587 ssif_info->global_enables |= IPMI_BMC_EVT_MSG_BUFF;
1588 }
1589
Corey Minyardbf2d0872015-08-27 15:49:18 -05001590 /* Some systems don't behave well if you enable alerts. */
1591 if (alerts_broken)
1592 goto found;
1593
Corey Minyard91620522015-04-24 07:46:06 -05001594 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1595 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1596 msg[2] = ssif_info->global_enables | IPMI_BMC_RCV_MSG_INTR;
1597 rv = do_cmd(client, 3, msg, &len, resp);
1598 if (rv || (len < 2)) {
1599 pr_warn(PFX "Error setting global enables: %d %d %2.2x\n",
1600 rv, len, resp[2]);
1601 rv = 0; /* Not fatal */
1602 goto found;
1603 }
1604
1605 if (resp[2] == 0) {
1606 /* A successful return means the alert is supported. */
1607 ssif_info->supports_alert = true;
1608 ssif_info->global_enables |= IPMI_BMC_RCV_MSG_INTR;
1609 }
Corey Minyard25930702012-03-19 16:00:55 -05001610
1611 found:
1612 ssif_info->intf_num = atomic_inc_return(&next_intf);
1613
1614 if (ssif_dbg_probe) {
1615 pr_info("ssif_probe: i2c_probe found device at i2c address %x\n",
1616 client->addr);
1617 }
1618
1619 spin_lock_init(&ssif_info->lock);
1620 ssif_info->ssif_state = SSIF_NORMAL;
1621 init_timer(&ssif_info->retry_timer);
1622 ssif_info->retry_timer.data = (unsigned long) ssif_info;
1623 ssif_info->retry_timer.function = retry_timeout;
1624
1625 for (i = 0; i < SSIF_NUM_STATS; i++)
1626 atomic_set(&ssif_info->stats[i], 0);
1627
1628 if (ssif_info->supports_pec)
1629 ssif_info->client->flags |= I2C_CLIENT_PEC;
1630
1631 ssif_info->handlers.owner = THIS_MODULE;
1632 ssif_info->handlers.start_processing = ssif_start_processing;
1633 ssif_info->handlers.get_smi_info = get_smi_info;
1634 ssif_info->handlers.sender = sender;
1635 ssif_info->handlers.request_events = request_events;
1636 ssif_info->handlers.inc_usecount = inc_usecount;
1637 ssif_info->handlers.dec_usecount = dec_usecount;
1638
1639 {
1640 unsigned int thread_num;
1641
1642 thread_num = ((ssif_info->client->adapter->nr << 8) |
1643 ssif_info->client->addr);
1644 init_completion(&ssif_info->wake_thread);
1645 ssif_info->thread = kthread_run(ipmi_ssif_thread, ssif_info,
1646 "kssif%4.4x", thread_num);
1647 if (IS_ERR(ssif_info->thread)) {
1648 rv = PTR_ERR(ssif_info->thread);
1649 dev_notice(&ssif_info->client->dev,
1650 "Could not start kernel thread: error %d\n",
1651 rv);
1652 goto out;
1653 }
1654 }
1655
1656 rv = ipmi_register_smi(&ssif_info->handlers,
1657 ssif_info,
1658 &ssif_info->device_id,
1659 &ssif_info->client->dev,
1660 slave_addr);
1661 if (rv) {
1662 pr_err(PFX "Unable to register device: error %d\n", rv);
1663 goto out;
1664 }
1665
1666 rv = ipmi_smi_add_proc_entry(ssif_info->intf, "type",
1667 &smi_type_proc_ops,
1668 ssif_info);
1669 if (rv) {
1670 pr_err(PFX "Unable to create proc entry: %d\n", rv);
1671 goto out_err_unreg;
1672 }
1673
1674 rv = ipmi_smi_add_proc_entry(ssif_info->intf, "ssif_stats",
1675 &smi_stats_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 out:
1683 if (rv)
1684 kfree(ssif_info);
1685 kfree(resp);
1686 return rv;
1687
1688 out_err_unreg:
1689 ipmi_unregister_smi(ssif_info->intf);
1690 goto out;
1691}
1692
1693static int ssif_adapter_handler(struct device *adev, void *opaque)
1694{
1695 struct ssif_addr_info *addr_info = opaque;
1696
1697 if (adev->type != &i2c_adapter_type)
1698 return 0;
1699
1700 i2c_new_device(to_i2c_adapter(adev), &addr_info->binfo);
1701
1702 if (!addr_info->adapter_name)
1703 return 1; /* Only try the first I2C adapter by default. */
1704 return 0;
1705}
1706
1707static int new_ssif_client(int addr, char *adapter_name,
1708 int debug, int slave_addr,
1709 enum ipmi_addr_src addr_src)
1710{
1711 struct ssif_addr_info *addr_info;
1712 int rv = 0;
1713
1714 mutex_lock(&ssif_infos_mutex);
1715 if (ssif_info_find(addr, adapter_name, false)) {
1716 rv = -EEXIST;
1717 goto out_unlock;
1718 }
1719
1720 addr_info = kzalloc(sizeof(*addr_info), GFP_KERNEL);
1721 if (!addr_info) {
1722 rv = -ENOMEM;
1723 goto out_unlock;
1724 }
1725
1726 if (adapter_name) {
1727 addr_info->adapter_name = kstrdup(adapter_name, GFP_KERNEL);
1728 if (!addr_info->adapter_name) {
1729 kfree(addr_info);
1730 rv = -ENOMEM;
1731 goto out_unlock;
1732 }
1733 }
1734
1735 strncpy(addr_info->binfo.type, DEVICE_NAME,
1736 sizeof(addr_info->binfo.type));
1737 addr_info->binfo.addr = addr;
1738 addr_info->binfo.platform_data = addr_info;
1739 addr_info->debug = debug;
1740 addr_info->slave_addr = slave_addr;
1741 addr_info->addr_src = addr_src;
1742
1743 list_add_tail(&addr_info->link, &ssif_infos);
1744
1745 if (initialized)
1746 i2c_for_each_dev(addr_info, ssif_adapter_handler);
1747 /* Otherwise address list will get it */
1748
1749out_unlock:
1750 mutex_unlock(&ssif_infos_mutex);
1751 return rv;
1752}
1753
1754static void free_ssif_clients(void)
1755{
1756 struct ssif_addr_info *info, *tmp;
1757
1758 mutex_lock(&ssif_infos_mutex);
1759 list_for_each_entry_safe(info, tmp, &ssif_infos, link) {
1760 list_del(&info->link);
1761 kfree(info->adapter_name);
1762 kfree(info);
1763 }
1764 mutex_unlock(&ssif_infos_mutex);
1765}
1766
1767static unsigned short *ssif_address_list(void)
1768{
1769 struct ssif_addr_info *info;
1770 unsigned int count = 0, i;
1771 unsigned short *address_list;
1772
1773 list_for_each_entry(info, &ssif_infos, link)
1774 count++;
1775
1776 address_list = kzalloc(sizeof(*address_list) * (count + 1), GFP_KERNEL);
1777 if (!address_list)
1778 return NULL;
1779
1780 i = 0;
1781 list_for_each_entry(info, &ssif_infos, link) {
1782 unsigned short addr = info->binfo.addr;
1783 int j;
1784
1785 for (j = 0; j < i; j++) {
1786 if (address_list[j] == addr)
1787 goto skip_addr;
1788 }
1789 address_list[i] = addr;
1790skip_addr:
1791 i++;
1792 }
1793 address_list[i] = I2C_CLIENT_END;
1794
1795 return address_list;
1796}
1797
1798#ifdef CONFIG_ACPI
Mathias Krause5186cf92015-06-13 14:19:33 +02001799static const struct acpi_device_id ssif_acpi_match[] = {
Corey Minyard25930702012-03-19 16:00:55 -05001800 { "IPI0001", 0 },
1801 { },
1802};
1803MODULE_DEVICE_TABLE(acpi, ssif_acpi_match);
1804
1805/*
1806 * Once we get an ACPI failure, we don't try any more, because we go
1807 * through the tables sequentially. Once we don't find a table, there
1808 * are no more.
1809 */
1810static int acpi_failure;
1811
1812/*
1813 * Defined in the IPMI 2.0 spec.
1814 */
1815struct SPMITable {
1816 s8 Signature[4];
1817 u32 Length;
1818 u8 Revision;
1819 u8 Checksum;
1820 s8 OEMID[6];
1821 s8 OEMTableID[8];
1822 s8 OEMRevision[4];
1823 s8 CreatorID[4];
1824 s8 CreatorRevision[4];
1825 u8 InterfaceType;
1826 u8 IPMIlegacy;
1827 s16 SpecificationRevision;
1828
1829 /*
1830 * Bit 0 - SCI interrupt supported
1831 * Bit 1 - I/O APIC/SAPIC
1832 */
1833 u8 InterruptType;
1834
1835 /*
1836 * If bit 0 of InterruptType is set, then this is the SCI
1837 * interrupt in the GPEx_STS register.
1838 */
1839 u8 GPE;
1840
1841 s16 Reserved;
1842
1843 /*
1844 * If bit 1 of InterruptType is set, then this is the I/O
1845 * APIC/SAPIC interrupt.
1846 */
1847 u32 GlobalSystemInterrupt;
1848
1849 /* The actual register address. */
1850 struct acpi_generic_address addr;
1851
1852 u8 UID[4];
1853
1854 s8 spmi_id[1]; /* A '\0' terminated array starts here. */
1855};
1856
1857static int try_init_spmi(struct SPMITable *spmi)
1858{
1859 unsigned short myaddr;
1860
1861 if (num_addrs >= MAX_SSIF_BMCS)
1862 return -1;
1863
1864 if (spmi->IPMIlegacy != 1) {
1865 pr_warn("IPMI: Bad SPMI legacy: %d\n", spmi->IPMIlegacy);
1866 return -ENODEV;
1867 }
1868
1869 if (spmi->InterfaceType != 4)
1870 return -ENODEV;
1871
1872 if (spmi->addr.space_id != ACPI_ADR_SPACE_SMBUS) {
1873 pr_warn(PFX "Invalid ACPI SSIF I/O Address type: %d\n",
1874 spmi->addr.space_id);
1875 return -EIO;
1876 }
1877
1878 myaddr = spmi->addr.address >> 1;
1879
1880 return new_ssif_client(myaddr, NULL, 0, 0, SI_SPMI);
1881}
1882
1883static void spmi_find_bmc(void)
1884{
1885 acpi_status status;
1886 struct SPMITable *spmi;
1887 int i;
1888
1889 if (acpi_disabled)
1890 return;
1891
1892 if (acpi_failure)
1893 return;
1894
1895 for (i = 0; ; i++) {
1896 status = acpi_get_table(ACPI_SIG_SPMI, i+1,
1897 (struct acpi_table_header **)&spmi);
1898 if (status != AE_OK)
1899 return;
1900
1901 try_init_spmi(spmi);
1902 }
1903}
1904#else
1905static void spmi_find_bmc(void) { }
1906#endif
1907
1908#ifdef CONFIG_DMI
1909static int decode_dmi(const struct dmi_device *dmi_dev)
1910{
1911 struct dmi_header *dm = dmi_dev->device_data;
1912 u8 *data = (u8 *) dm;
1913 u8 len = dm->length;
1914 unsigned short myaddr;
1915 int slave_addr;
1916
1917 if (num_addrs >= MAX_SSIF_BMCS)
1918 return -1;
1919
1920 if (len < 9)
1921 return -1;
1922
1923 if (data[0x04] != 4) /* Not SSIF */
1924 return -1;
1925
1926 if ((data[8] >> 1) == 0) {
1927 /*
1928 * Some broken systems put the I2C address in
1929 * the slave address field. We try to
1930 * accommodate them here.
1931 */
1932 myaddr = data[6] >> 1;
1933 slave_addr = 0;
1934 } else {
1935 myaddr = data[8] >> 1;
1936 slave_addr = data[6];
1937 }
1938
1939 return new_ssif_client(myaddr, NULL, 0, 0, SI_SMBIOS);
1940}
1941
1942static void dmi_iterator(void)
1943{
1944 const struct dmi_device *dev = NULL;
1945
1946 while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev)))
1947 decode_dmi(dev);
1948}
1949#else
1950static void dmi_iterator(void) { }
1951#endif
1952
1953static const struct i2c_device_id ssif_id[] = {
1954 { DEVICE_NAME, 0 },
1955 { }
1956};
1957MODULE_DEVICE_TABLE(i2c, ssif_id);
1958
1959static struct i2c_driver ssif_i2c_driver = {
1960 .class = I2C_CLASS_HWMON,
1961 .driver = {
1962 .owner = THIS_MODULE,
1963 .name = DEVICE_NAME
1964 },
1965 .probe = ssif_probe,
1966 .remove = ssif_remove,
Corey Minyard91620522015-04-24 07:46:06 -05001967 .alert = ssif_alert,
Corey Minyard25930702012-03-19 16:00:55 -05001968 .id_table = ssif_id,
1969 .detect = ssif_detect
1970};
1971
1972static int init_ipmi_ssif(void)
1973{
1974 int i;
1975 int rv;
1976
1977 if (initialized)
1978 return 0;
1979
1980 pr_info("IPMI SSIF Interface driver\n");
1981
1982 /* build list for i2c from addr list */
1983 for (i = 0; i < num_addrs; i++) {
1984 rv = new_ssif_client(addr[i], adapter_name[i],
1985 dbg[i], slave_addrs[i],
1986 SI_HARDCODED);
Corey Minyardd467f7a2015-03-26 13:35:18 -05001987 if (rv)
Corey Minyard25930702012-03-19 16:00:55 -05001988 pr_err(PFX
1989 "Couldn't add hardcoded device at addr 0x%x\n",
1990 addr[i]);
1991 }
1992
1993 if (ssif_tryacpi)
1994 ssif_i2c_driver.driver.acpi_match_table =
1995 ACPI_PTR(ssif_acpi_match);
1996 if (ssif_trydmi)
1997 dmi_iterator();
1998 if (ssif_tryacpi)
1999 spmi_find_bmc();
2000
2001 ssif_i2c_driver.address_list = ssif_address_list();
2002
2003 rv = i2c_add_driver(&ssif_i2c_driver);
2004 if (!rv)
2005 initialized = true;
2006
2007 return rv;
2008}
2009module_init(init_ipmi_ssif);
2010
2011static void cleanup_ipmi_ssif(void)
2012{
2013 if (!initialized)
2014 return;
2015
2016 initialized = false;
2017
2018 i2c_del_driver(&ssif_i2c_driver);
2019
2020 free_ssif_clients();
2021}
2022module_exit(cleanup_ipmi_ssif);
2023
2024MODULE_AUTHOR("Todd C Davis <todd.c.davis@intel.com>, Corey Minyard <minyard@acm.org>");
2025MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus");
2026MODULE_LICENSE("GPL");