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