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