• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * ipmi_ssif.c
4  *
5  * The interface to the IPMI driver for SMBus access to a SMBus
6  * compliant device.  Called SSIF by the IPMI spec.
7  *
8  * Author: Intel Corporation
9  *         Todd Davis <todd.c.davis@intel.com>
10  *
11  * Rewritten by Corey Minyard <minyard@acm.org> to support the
12  * non-blocking I2C interface, add support for multi-part
13  * transactions, add PEC support, and general clenaup.
14  *
15  * Copyright 2003 Intel Corporation
16  * Copyright 2005 MontaVista Software
17  */
18 
19 /*
20  * This file holds the "policy" for the interface to the SSIF state
21  * machine.  It does the configuration, handles timers and interrupts,
22  * and drives the real SSIF state machine.
23  */
24 
25 #define pr_fmt(fmt) "ipmi_ssif: " fmt
26 #define dev_fmt(fmt) "ipmi_ssif: " fmt
27 
28 #if defined(MODVERSIONS)
29 #include <linux/modversions.h>
30 #endif
31 
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/sched.h>
35 #include <linux/seq_file.h>
36 #include <linux/timer.h>
37 #include <linux/delay.h>
38 #include <linux/errno.h>
39 #include <linux/spinlock.h>
40 #include <linux/slab.h>
41 #include <linux/list.h>
42 #include <linux/i2c.h>
43 #include <linux/ipmi_smi.h>
44 #include <linux/init.h>
45 #include <linux/dmi.h>
46 #include <linux/kthread.h>
47 #include <linux/acpi.h>
48 #include <linux/ctype.h>
49 #include <linux/time64.h>
50 #include "ipmi_dmi.h"
51 
52 #define DEVICE_NAME "ipmi_ssif"
53 
54 #define IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD	0x57
55 
56 #define	SSIF_IPMI_REQUEST			2
57 #define	SSIF_IPMI_MULTI_PART_REQUEST_START	6
58 #define	SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE	7
59 #define	SSIF_IPMI_MULTI_PART_REQUEST_END	8
60 #define	SSIF_IPMI_RESPONSE			3
61 #define	SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE	9
62 
63 /* ssif_debug is a bit-field
64  *	SSIF_DEBUG_MSG -	commands and their responses
65  *	SSIF_DEBUG_STATES -	message states
66  *	SSIF_DEBUG_TIMING -	 Measure times between events in the driver
67  */
68 #define SSIF_DEBUG_TIMING	4
69 #define SSIF_DEBUG_STATE	2
70 #define SSIF_DEBUG_MSG		1
71 #define SSIF_NODEBUG		0
72 #define SSIF_DEFAULT_DEBUG	(SSIF_NODEBUG)
73 
74 /*
75  * Timer values
76  */
77 #define SSIF_MSG_USEC		20000	/* 20ms between message tries. */
78 #define SSIF_MSG_PART_USEC	5000	/* 5ms for a message part */
79 
80 /* How many times to we retry sending/receiving the message. */
81 #define	SSIF_SEND_RETRIES	5
82 #define	SSIF_RECV_RETRIES	250
83 
84 #define SSIF_MSG_MSEC		(SSIF_MSG_USEC / 1000)
85 #define SSIF_MSG_JIFFIES	((SSIF_MSG_USEC * 1000) / TICK_NSEC)
86 #define SSIF_MSG_PART_JIFFIES	((SSIF_MSG_PART_USEC * 1000) / TICK_NSEC)
87 
88 /*
89  * Timeout for the watch, only used for get flag timer.
90  */
91 #define SSIF_WATCH_MSG_TIMEOUT		msecs_to_jiffies(10)
92 #define SSIF_WATCH_WATCHDOG_TIMEOUT	msecs_to_jiffies(250)
93 
94 enum ssif_intf_state {
95 	SSIF_NORMAL,
96 	SSIF_GETTING_FLAGS,
97 	SSIF_GETTING_EVENTS,
98 	SSIF_CLEARING_FLAGS,
99 	SSIF_GETTING_MESSAGES,
100 	/* FIXME - add watchdog stuff. */
101 };
102 
103 #define SSIF_IDLE(ssif)	 ((ssif)->ssif_state == SSIF_NORMAL \
104 			  && (ssif)->curr_msg == NULL)
105 
106 /*
107  * Indexes into stats[] in ssif_info below.
108  */
109 enum ssif_stat_indexes {
110 	/* Number of total messages sent. */
111 	SSIF_STAT_sent_messages = 0,
112 
113 	/*
114 	 * Number of message parts sent.  Messages may be broken into
115 	 * parts if they are long.
116 	 */
117 	SSIF_STAT_sent_messages_parts,
118 
119 	/*
120 	 * Number of time a message was retried.
121 	 */
122 	SSIF_STAT_send_retries,
123 
124 	/*
125 	 * Number of times the send of a message failed.
126 	 */
127 	SSIF_STAT_send_errors,
128 
129 	/*
130 	 * Number of message responses received.
131 	 */
132 	SSIF_STAT_received_messages,
133 
134 	/*
135 	 * Number of message fragments received.
136 	 */
137 	SSIF_STAT_received_message_parts,
138 
139 	/*
140 	 * Number of times the receive of a message was retried.
141 	 */
142 	SSIF_STAT_receive_retries,
143 
144 	/*
145 	 * Number of errors receiving messages.
146 	 */
147 	SSIF_STAT_receive_errors,
148 
149 	/*
150 	 * Number of times a flag fetch was requested.
151 	 */
152 	SSIF_STAT_flag_fetches,
153 
154 	/*
155 	 * Number of times the hardware didn't follow the state machine.
156 	 */
157 	SSIF_STAT_hosed,
158 
159 	/*
160 	 * Number of received events.
161 	 */
162 	SSIF_STAT_events,
163 
164 	/* Number of asyncronous messages received. */
165 	SSIF_STAT_incoming_messages,
166 
167 	/* Number of watchdog pretimeouts. */
168 	SSIF_STAT_watchdog_pretimeouts,
169 
170 	/* Number of alers received. */
171 	SSIF_STAT_alerts,
172 
173 	/* Always add statistics before this value, it must be last. */
174 	SSIF_NUM_STATS
175 };
176 
177 struct ssif_addr_info {
178 	struct i2c_board_info binfo;
179 	char *adapter_name;
180 	int debug;
181 	int slave_addr;
182 	enum ipmi_addr_src addr_src;
183 	union ipmi_smi_info_union addr_info;
184 	struct device *dev;
185 	struct i2c_client *client;
186 
187 	struct mutex clients_mutex;
188 	struct list_head clients;
189 
190 	struct list_head link;
191 };
192 
193 struct ssif_info;
194 
195 typedef void (*ssif_i2c_done)(struct ssif_info *ssif_info, int result,
196 			     unsigned char *data, unsigned int len);
197 
198 struct ssif_info {
199 	struct ipmi_smi     *intf;
200 	spinlock_t	    lock;
201 	struct ipmi_smi_msg *waiting_msg;
202 	struct ipmi_smi_msg *curr_msg;
203 	enum ssif_intf_state ssif_state;
204 	unsigned long       ssif_debug;
205 
206 	struct ipmi_smi_handlers handlers;
207 
208 	enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */
209 	union ipmi_smi_info_union addr_info;
210 
211 	/*
212 	 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
213 	 * is set to hold the flags until we are done handling everything
214 	 * from the flags.
215 	 */
216 #define RECEIVE_MSG_AVAIL	0x01
217 #define EVENT_MSG_BUFFER_FULL	0x02
218 #define WDT_PRE_TIMEOUT_INT	0x08
219 	unsigned char       msg_flags;
220 
221 	u8		    global_enables;
222 	bool		    has_event_buffer;
223 	bool		    supports_alert;
224 
225 	/*
226 	 * Used to tell what we should do with alerts.  If we are
227 	 * waiting on a response, read the data immediately.
228 	 */
229 	bool		    got_alert;
230 	bool		    waiting_alert;
231 
232 	/*
233 	 * If set to true, this will request events the next time the
234 	 * state machine is idle.
235 	 */
236 	bool                req_events;
237 
238 	/*
239 	 * If set to true, this will request flags the next time the
240 	 * state machine is idle.
241 	 */
242 	bool                req_flags;
243 
244 	/*
245 	 * Used to perform timer operations when run-to-completion
246 	 * mode is on.  This is a countdown timer.
247 	 */
248 	int                 rtc_us_timer;
249 
250 	/* Used for sending/receiving data.  +1 for the length. */
251 	unsigned char data[IPMI_MAX_MSG_LENGTH + 1];
252 	unsigned int  data_len;
253 
254 	/* Temp receive buffer, gets copied into data. */
255 	unsigned char recv[I2C_SMBUS_BLOCK_MAX];
256 
257 	struct i2c_client *client;
258 	ssif_i2c_done done_handler;
259 
260 	/* Thread interface handling */
261 	struct task_struct *thread;
262 	struct completion wake_thread;
263 	bool stopping;
264 	int i2c_read_write;
265 	int i2c_command;
266 	unsigned char *i2c_data;
267 	unsigned int i2c_size;
268 
269 	struct timer_list retry_timer;
270 	int retries_left;
271 
272 	long watch_timeout;		/* Timeout for flags check, 0 if off. */
273 	struct timer_list watch_timer;	/* Flag fetch timer. */
274 
275 	/* Info from SSIF cmd */
276 	unsigned char max_xmit_msg_size;
277 	unsigned char max_recv_msg_size;
278 	bool cmd8_works; /* See test_multipart_messages() for details. */
279 	unsigned int  multi_support;
280 	int           supports_pec;
281 
282 #define SSIF_NO_MULTI		0
283 #define SSIF_MULTI_2_PART	1
284 #define SSIF_MULTI_n_PART	2
285 	unsigned char *multi_data;
286 	unsigned int  multi_len;
287 	unsigned int  multi_pos;
288 
289 	atomic_t stats[SSIF_NUM_STATS];
290 };
291 
292 #define ssif_inc_stat(ssif, stat) \
293 	atomic_inc(&(ssif)->stats[SSIF_STAT_ ## stat])
294 #define ssif_get_stat(ssif, stat) \
295 	((unsigned int) atomic_read(&(ssif)->stats[SSIF_STAT_ ## stat]))
296 
297 static bool initialized;
298 static bool platform_registered;
299 
300 static void return_hosed_msg(struct ssif_info *ssif_info,
301 			     struct ipmi_smi_msg *msg);
302 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags);
303 static int start_send(struct ssif_info *ssif_info,
304 		      unsigned char   *data,
305 		      unsigned int    len);
306 
ipmi_ssif_lock_cond(struct ssif_info * ssif_info,unsigned long * flags)307 static unsigned long *ipmi_ssif_lock_cond(struct ssif_info *ssif_info,
308 					  unsigned long *flags)
309 	__acquires(&ssif_info->lock)
310 {
311 	spin_lock_irqsave(&ssif_info->lock, *flags);
312 	return flags;
313 }
314 
ipmi_ssif_unlock_cond(struct ssif_info * ssif_info,unsigned long * flags)315 static void ipmi_ssif_unlock_cond(struct ssif_info *ssif_info,
316 				  unsigned long *flags)
317 	__releases(&ssif_info->lock)
318 {
319 	spin_unlock_irqrestore(&ssif_info->lock, *flags);
320 }
321 
deliver_recv_msg(struct ssif_info * ssif_info,struct ipmi_smi_msg * msg)322 static void deliver_recv_msg(struct ssif_info *ssif_info,
323 			     struct ipmi_smi_msg *msg)
324 {
325 	if (msg->rsp_size < 0) {
326 		return_hosed_msg(ssif_info, msg);
327 		dev_err(&ssif_info->client->dev,
328 			"%s: Malformed message: rsp_size = %d\n",
329 		       __func__, msg->rsp_size);
330 	} else {
331 		ipmi_smi_msg_received(ssif_info->intf, msg);
332 	}
333 }
334 
return_hosed_msg(struct ssif_info * ssif_info,struct ipmi_smi_msg * msg)335 static 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 	/* Make it a response */
341 	msg->rsp[0] = msg->data[0] | 4;
342 	msg->rsp[1] = msg->data[1];
343 	msg->rsp[2] = 0xFF; /* Unknown error. */
344 	msg->rsp_size = 3;
345 
346 	deliver_recv_msg(ssif_info, msg);
347 }
348 
349 /*
350  * Must be called with the message lock held.  This will release the
351  * message lock.  Note that the caller will check SSIF_IDLE and start a
352  * new operation, so there is no need to check for new messages to
353  * start in here.
354  */
start_clear_flags(struct ssif_info * ssif_info,unsigned long * flags)355 static 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 	/* Make sure the watchdog pre-timeout flag is not set at startup. */
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 		/* Error, just go to normal state. */
370 		ssif_info->ssif_state = SSIF_NORMAL;
371 	}
372 }
373 
start_flag_fetch(struct ssif_info * ssif_info,unsigned long * flags)374 static 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 
check_start_send(struct ssif_info * ssif_info,unsigned long * flags,struct ipmi_smi_msg * msg)388 static 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 
start_event_fetch(struct ssif_info * ssif_info,unsigned long * flags)402 static 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 
start_recv_msg_fetch(struct ssif_info * ssif_info,unsigned long * flags)426 static 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  * Must be called with the message lock held.  This will release the
451  * message lock.  Note that the caller will check SSIF_IDLE and start a
452  * new operation, so there is no need to check for new messages to
453  * start in here.
454  */
handle_flags(struct ssif_info * ssif_info,unsigned long * flags)455 static void handle_flags(struct ssif_info *ssif_info, unsigned long *flags)
456 {
457 	if (ssif_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
458 		/* Watchdog pre-timeout */
459 		ssif_inc_stat(ssif_info, watchdog_pretimeouts);
460 		start_clear_flags(ssif_info, flags);
461 		ipmi_smi_watchdog_pretimeout(ssif_info->intf);
462 	} else if (ssif_info->msg_flags & RECEIVE_MSG_AVAIL)
463 		/* Messages available. */
464 		start_recv_msg_fetch(ssif_info, flags);
465 	else if (ssif_info->msg_flags & EVENT_MSG_BUFFER_FULL)
466 		/* Events available. */
467 		start_event_fetch(ssif_info, flags);
468 	else {
469 		ssif_info->ssif_state = SSIF_NORMAL;
470 		ipmi_ssif_unlock_cond(ssif_info, flags);
471 	}
472 }
473 
ipmi_ssif_thread(void * data)474 static int ipmi_ssif_thread(void *data)
475 {
476 	struct ssif_info *ssif_info = data;
477 
478 	while (!kthread_should_stop()) {
479 		int result;
480 
481 		/* Wait for something to do */
482 		result = wait_for_completion_interruptible(
483 						&ssif_info->wake_thread);
484 		if (ssif_info->stopping)
485 			break;
486 		if (result == -ERESTARTSYS)
487 			continue;
488 		init_completion(&ssif_info->wake_thread);
489 
490 		if (ssif_info->i2c_read_write == I2C_SMBUS_WRITE) {
491 			result = i2c_smbus_write_block_data(
492 				ssif_info->client, ssif_info->i2c_command,
493 				ssif_info->i2c_data[0],
494 				ssif_info->i2c_data + 1);
495 			ssif_info->done_handler(ssif_info, result, NULL, 0);
496 		} else {
497 			result = i2c_smbus_read_block_data(
498 				ssif_info->client, ssif_info->i2c_command,
499 				ssif_info->i2c_data);
500 			if (result < 0)
501 				ssif_info->done_handler(ssif_info, result,
502 							NULL, 0);
503 			else
504 				ssif_info->done_handler(ssif_info, 0,
505 							ssif_info->i2c_data,
506 							result);
507 		}
508 	}
509 
510 	return 0;
511 }
512 
ssif_i2c_send(struct ssif_info * ssif_info,ssif_i2c_done handler,int read_write,int command,unsigned char * data,unsigned int size)513 static int ssif_i2c_send(struct ssif_info *ssif_info,
514 			ssif_i2c_done handler,
515 			int read_write, int command,
516 			unsigned char *data, unsigned int size)
517 {
518 	ssif_info->done_handler = handler;
519 
520 	ssif_info->i2c_read_write = read_write;
521 	ssif_info->i2c_command = command;
522 	ssif_info->i2c_data = data;
523 	ssif_info->i2c_size = size;
524 	complete(&ssif_info->wake_thread);
525 	return 0;
526 }
527 
528 
529 static void msg_done_handler(struct ssif_info *ssif_info, int result,
530 			     unsigned char *data, unsigned int len);
531 
start_get(struct ssif_info * ssif_info)532 static void start_get(struct ssif_info *ssif_info)
533 {
534 	int rv;
535 
536 	ssif_info->rtc_us_timer = 0;
537 	ssif_info->multi_pos = 0;
538 
539 	rv = ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
540 			  SSIF_IPMI_RESPONSE,
541 			  ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
542 	if (rv < 0) {
543 		/* request failed, just return the error. */
544 		if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
545 			dev_dbg(&ssif_info->client->dev,
546 				"Error from i2c_non_blocking_op(5)\n");
547 
548 		msg_done_handler(ssif_info, -EIO, NULL, 0);
549 	}
550 }
551 
retry_timeout(struct timer_list * t)552 static void retry_timeout(struct timer_list *t)
553 {
554 	struct ssif_info *ssif_info = from_timer(ssif_info, t, retry_timer);
555 	unsigned long oflags, *flags;
556 	bool waiting;
557 
558 	if (ssif_info->stopping)
559 		return;
560 
561 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
562 	waiting = ssif_info->waiting_alert;
563 	ssif_info->waiting_alert = false;
564 	ipmi_ssif_unlock_cond(ssif_info, flags);
565 
566 	if (waiting)
567 		start_get(ssif_info);
568 }
569 
watch_timeout(struct timer_list * t)570 static void watch_timeout(struct timer_list *t)
571 {
572 	struct ssif_info *ssif_info = from_timer(ssif_info, t, watch_timer);
573 	unsigned long oflags, *flags;
574 
575 	if (ssif_info->stopping)
576 		return;
577 
578 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
579 	if (ssif_info->watch_timeout) {
580 		mod_timer(&ssif_info->watch_timer,
581 			  jiffies + ssif_info->watch_timeout);
582 		if (SSIF_IDLE(ssif_info)) {
583 			start_flag_fetch(ssif_info, flags); /* Releases lock */
584 			return;
585 		}
586 		ssif_info->req_flags = true;
587 	}
588 	ipmi_ssif_unlock_cond(ssif_info, flags);
589 }
590 
ssif_alert(struct i2c_client * client,enum i2c_alert_protocol type,unsigned int data)591 static void ssif_alert(struct i2c_client *client, enum i2c_alert_protocol type,
592 		       unsigned int data)
593 {
594 	struct ssif_info *ssif_info = i2c_get_clientdata(client);
595 	unsigned long oflags, *flags;
596 	bool do_get = false;
597 
598 	if (type != I2C_PROTOCOL_SMBUS_ALERT)
599 		return;
600 
601 	ssif_inc_stat(ssif_info, alerts);
602 
603 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
604 	if (ssif_info->waiting_alert) {
605 		ssif_info->waiting_alert = false;
606 		del_timer(&ssif_info->retry_timer);
607 		do_get = true;
608 	} else if (ssif_info->curr_msg) {
609 		ssif_info->got_alert = true;
610 	}
611 	ipmi_ssif_unlock_cond(ssif_info, flags);
612 	if (do_get)
613 		start_get(ssif_info);
614 }
615 
616 static int start_resend(struct ssif_info *ssif_info);
617 
msg_done_handler(struct ssif_info * ssif_info,int result,unsigned char * data,unsigned int len)618 static void msg_done_handler(struct ssif_info *ssif_info, int result,
619 			     unsigned char *data, unsigned int len)
620 {
621 	struct ipmi_smi_msg *msg;
622 	unsigned long oflags, *flags;
623 	int rv;
624 
625 	/*
626 	 * We are single-threaded here, so no need for a lock until we
627 	 * start messing with driver states or the queues.
628 	 */
629 
630 	if (result < 0) {
631 		ssif_info->retries_left--;
632 		if (ssif_info->retries_left > 0) {
633 			ssif_inc_stat(ssif_info, receive_retries);
634 
635 			flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
636 			ssif_info->waiting_alert = true;
637 			ssif_info->rtc_us_timer = SSIF_MSG_USEC;
638 			if (!ssif_info->stopping)
639 				mod_timer(&ssif_info->retry_timer,
640 					  jiffies + SSIF_MSG_JIFFIES);
641 			ipmi_ssif_unlock_cond(ssif_info, flags);
642 			return;
643 		}
644 
645 		ssif_inc_stat(ssif_info, receive_errors);
646 
647 		if  (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
648 			dev_dbg(&ssif_info->client->dev,
649 				"%s: Error %d\n", __func__, result);
650 		len = 0;
651 		goto continue_op;
652 	}
653 
654 	if ((len > 1) && (ssif_info->multi_pos == 0)
655 				&& (data[0] == 0x00) && (data[1] == 0x01)) {
656 		/* Start of multi-part read.  Start the next transaction. */
657 		int i;
658 
659 		ssif_inc_stat(ssif_info, received_message_parts);
660 
661 		/* Remove the multi-part read marker. */
662 		len -= 2;
663 		data += 2;
664 		for (i = 0; i < len; i++)
665 			ssif_info->data[i] = data[i];
666 		ssif_info->multi_len = len;
667 		ssif_info->multi_pos = 1;
668 
669 		rv = ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
670 				  SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
671 				  ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
672 		if (rv < 0) {
673 			if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
674 				dev_dbg(&ssif_info->client->dev,
675 					"Error from i2c_non_blocking_op(1)\n");
676 
677 			result = -EIO;
678 		} else
679 			return;
680 	} else if (ssif_info->multi_pos) {
681 		/* Middle of multi-part read.  Start the next transaction. */
682 		int i;
683 		unsigned char blocknum;
684 
685 		if (len == 0) {
686 			result = -EIO;
687 			if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
688 				dev_dbg(&ssif_info->client->dev,
689 					"Middle message with no data\n");
690 
691 			goto continue_op;
692 		}
693 
694 		blocknum = data[0];
695 		len--;
696 		data++;
697 
698 		if (blocknum != 0xff && len != 31) {
699 		    /* All blocks but the last must have 31 data bytes. */
700 			result = -EIO;
701 			if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
702 				dev_dbg(&ssif_info->client->dev,
703 					"Received middle message <31\n");
704 
705 			goto continue_op;
706 		}
707 
708 		if (ssif_info->multi_len + len > IPMI_MAX_MSG_LENGTH) {
709 			/* Received message too big, abort the operation. */
710 			result = -E2BIG;
711 			if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
712 				dev_dbg(&ssif_info->client->dev,
713 					"Received message too big\n");
714 
715 			goto continue_op;
716 		}
717 
718 		for (i = 0; i < len; i++)
719 			ssif_info->data[i + ssif_info->multi_len] = data[i];
720 		ssif_info->multi_len += len;
721 		if (blocknum == 0xff) {
722 			/* End of read */
723 			len = ssif_info->multi_len;
724 			data = ssif_info->data;
725 		} else if (blocknum + 1 != ssif_info->multi_pos) {
726 			/*
727 			 * Out of sequence block, just abort.  Block
728 			 * numbers start at zero for the second block,
729 			 * but multi_pos starts at one, so the +1.
730 			 */
731 			if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
732 				dev_dbg(&ssif_info->client->dev,
733 					"Received message out of sequence, expected %u, got %u\n",
734 					ssif_info->multi_pos - 1, blocknum);
735 			result = -EIO;
736 		} else {
737 			ssif_inc_stat(ssif_info, received_message_parts);
738 
739 			ssif_info->multi_pos++;
740 
741 			rv = ssif_i2c_send(ssif_info, msg_done_handler,
742 					   I2C_SMBUS_READ,
743 					   SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
744 					   ssif_info->recv,
745 					   I2C_SMBUS_BLOCK_DATA);
746 			if (rv < 0) {
747 				if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
748 					dev_dbg(&ssif_info->client->dev,
749 						"Error from ssif_i2c_send\n");
750 
751 				result = -EIO;
752 			} else
753 				return;
754 		}
755 	}
756 
757  continue_op:
758 	if (result < 0) {
759 		ssif_inc_stat(ssif_info, receive_errors);
760 	} else {
761 		ssif_inc_stat(ssif_info, received_messages);
762 		ssif_inc_stat(ssif_info, received_message_parts);
763 	}
764 
765 	if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
766 		dev_dbg(&ssif_info->client->dev,
767 			"DONE 1: state = %d, result=%d\n",
768 			ssif_info->ssif_state, result);
769 
770 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
771 	msg = ssif_info->curr_msg;
772 	if (msg) {
773 		if (data) {
774 			if (len > IPMI_MAX_MSG_LENGTH)
775 				len = IPMI_MAX_MSG_LENGTH;
776 			memcpy(msg->rsp, data, len);
777 		} else {
778 			len = 0;
779 		}
780 		msg->rsp_size = len;
781 		ssif_info->curr_msg = NULL;
782 	}
783 
784 	switch (ssif_info->ssif_state) {
785 	case SSIF_NORMAL:
786 		ipmi_ssif_unlock_cond(ssif_info, flags);
787 		if (!msg)
788 			break;
789 
790 		if (result < 0)
791 			return_hosed_msg(ssif_info, msg);
792 		else
793 			deliver_recv_msg(ssif_info, msg);
794 		break;
795 
796 	case SSIF_GETTING_FLAGS:
797 		/* We got the flags from the SSIF, now handle them. */
798 		if ((result < 0) || (len < 4) || (data[2] != 0)) {
799 			/*
800 			 * Error fetching flags, or invalid length,
801 			 * just give up for now.
802 			 */
803 			ssif_info->ssif_state = SSIF_NORMAL;
804 			ipmi_ssif_unlock_cond(ssif_info, flags);
805 			dev_warn(&ssif_info->client->dev,
806 				 "Error getting flags: %d %d, %x\n",
807 				 result, len, (len >= 3) ? data[2] : 0);
808 		} else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
809 			   || data[1] != IPMI_GET_MSG_FLAGS_CMD) {
810 			/*
811 			 * Don't abort here, maybe it was a queued
812 			 * response to a previous command.
813 			 */
814 			ipmi_ssif_unlock_cond(ssif_info, flags);
815 			dev_warn(&ssif_info->client->dev,
816 				 "Invalid response getting flags: %x %x\n",
817 				 data[0], data[1]);
818 		} else {
819 			ssif_inc_stat(ssif_info, flag_fetches);
820 			ssif_info->msg_flags = data[3];
821 			handle_flags(ssif_info, flags);
822 		}
823 		break;
824 
825 	case SSIF_CLEARING_FLAGS:
826 		/* We cleared the flags. */
827 		if ((result < 0) || (len < 3) || (data[2] != 0)) {
828 			/* Error clearing flags */
829 			dev_warn(&ssif_info->client->dev,
830 				 "Error clearing flags: %d %d, %x\n",
831 				 result, len, (len >= 3) ? data[2] : 0);
832 		} else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
833 			   || data[1] != IPMI_CLEAR_MSG_FLAGS_CMD) {
834 			dev_warn(&ssif_info->client->dev,
835 				 "Invalid response clearing flags: %x %x\n",
836 				 data[0], data[1]);
837 		}
838 		ssif_info->ssif_state = SSIF_NORMAL;
839 		ipmi_ssif_unlock_cond(ssif_info, flags);
840 		break;
841 
842 	case SSIF_GETTING_EVENTS:
843 		if (!msg) {
844 			/* Should never happen, but just in case. */
845 			dev_warn(&ssif_info->client->dev,
846 				 "No message set while getting events\n");
847 			ipmi_ssif_unlock_cond(ssif_info, flags);
848 			break;
849 		}
850 
851 		if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
852 			/* Error getting event, probably done. */
853 			msg->done(msg);
854 
855 			/* Take off the event flag. */
856 			ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
857 			handle_flags(ssif_info, flags);
858 		} else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
859 			   || msg->rsp[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD) {
860 			dev_warn(&ssif_info->client->dev,
861 				 "Invalid response getting events: %x %x\n",
862 				 msg->rsp[0], msg->rsp[1]);
863 			msg->done(msg);
864 			/* Take off the event flag. */
865 			ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
866 			handle_flags(ssif_info, flags);
867 		} else {
868 			handle_flags(ssif_info, flags);
869 			ssif_inc_stat(ssif_info, events);
870 			deliver_recv_msg(ssif_info, msg);
871 		}
872 		break;
873 
874 	case SSIF_GETTING_MESSAGES:
875 		if (!msg) {
876 			/* Should never happen, but just in case. */
877 			dev_warn(&ssif_info->client->dev,
878 				 "No message set while getting messages\n");
879 			ipmi_ssif_unlock_cond(ssif_info, flags);
880 			break;
881 		}
882 
883 		if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
884 			/* Error getting event, probably done. */
885 			msg->done(msg);
886 
887 			/* Take off the msg flag. */
888 			ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
889 			handle_flags(ssif_info, flags);
890 		} else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
891 			   || msg->rsp[1] != IPMI_GET_MSG_CMD) {
892 			dev_warn(&ssif_info->client->dev,
893 				 "Invalid response clearing flags: %x %x\n",
894 				 msg->rsp[0], msg->rsp[1]);
895 			msg->done(msg);
896 
897 			/* Take off the msg flag. */
898 			ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
899 			handle_flags(ssif_info, flags);
900 		} else {
901 			ssif_inc_stat(ssif_info, incoming_messages);
902 			handle_flags(ssif_info, flags);
903 			deliver_recv_msg(ssif_info, msg);
904 		}
905 		break;
906 
907 	default:
908 		/* Should never happen, but just in case. */
909 		dev_warn(&ssif_info->client->dev,
910 			 "Invalid state in message done handling: %d\n",
911 			 ssif_info->ssif_state);
912 		ipmi_ssif_unlock_cond(ssif_info, flags);
913 	}
914 
915 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
916 	if (SSIF_IDLE(ssif_info) && !ssif_info->stopping) {
917 		if (ssif_info->req_events)
918 			start_event_fetch(ssif_info, flags);
919 		else if (ssif_info->req_flags)
920 			start_flag_fetch(ssif_info, flags);
921 		else
922 			start_next_msg(ssif_info, flags);
923 	} else
924 		ipmi_ssif_unlock_cond(ssif_info, flags);
925 
926 	if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
927 		dev_dbg(&ssif_info->client->dev,
928 			"DONE 2: state = %d.\n", ssif_info->ssif_state);
929 }
930 
msg_written_handler(struct ssif_info * ssif_info,int result,unsigned char * data,unsigned int len)931 static void msg_written_handler(struct ssif_info *ssif_info, int result,
932 				unsigned char *data, unsigned int len)
933 {
934 	int rv;
935 
936 	/* We are single-threaded here, so no need for a lock. */
937 	if (result < 0) {
938 		ssif_info->retries_left--;
939 		if (ssif_info->retries_left > 0) {
940 			if (!start_resend(ssif_info)) {
941 				ssif_inc_stat(ssif_info, send_retries);
942 				return;
943 			}
944 			/* request failed, just return the error. */
945 			ssif_inc_stat(ssif_info, send_errors);
946 
947 			if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
948 				dev_dbg(&ssif_info->client->dev,
949 					"%s: Out of retries\n", __func__);
950 			msg_done_handler(ssif_info, -EIO, NULL, 0);
951 			return;
952 		}
953 
954 		ssif_inc_stat(ssif_info, send_errors);
955 
956 		/*
957 		 * Got an error on transmit, let the done routine
958 		 * handle it.
959 		 */
960 		if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
961 			dev_dbg(&ssif_info->client->dev,
962 				"%s: Error  %d\n", __func__, result);
963 
964 		msg_done_handler(ssif_info, result, NULL, 0);
965 		return;
966 	}
967 
968 	if (ssif_info->multi_data) {
969 		/*
970 		 * In the middle of a multi-data write.  See the comment
971 		 * in the SSIF_MULTI_n_PART case in the probe function
972 		 * for details on the intricacies of this.
973 		 */
974 		int left, to_write;
975 		unsigned char *data_to_send;
976 		unsigned char cmd;
977 
978 		ssif_inc_stat(ssif_info, sent_messages_parts);
979 
980 		left = ssif_info->multi_len - ssif_info->multi_pos;
981 		to_write = left;
982 		if (to_write > 32)
983 			to_write = 32;
984 		/* Length byte. */
985 		ssif_info->multi_data[ssif_info->multi_pos] = to_write;
986 		data_to_send = ssif_info->multi_data + ssif_info->multi_pos;
987 		ssif_info->multi_pos += to_write;
988 		cmd = SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE;
989 		if (ssif_info->cmd8_works) {
990 			if (left == to_write) {
991 				cmd = SSIF_IPMI_MULTI_PART_REQUEST_END;
992 				ssif_info->multi_data = NULL;
993 			}
994 		} else if (to_write < 32) {
995 			ssif_info->multi_data = NULL;
996 		}
997 
998 		rv = ssif_i2c_send(ssif_info, msg_written_handler,
999 				   I2C_SMBUS_WRITE, cmd,
1000 				   data_to_send, I2C_SMBUS_BLOCK_DATA);
1001 		if (rv < 0) {
1002 			/* request failed, just return the error. */
1003 			ssif_inc_stat(ssif_info, send_errors);
1004 
1005 			if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
1006 				dev_dbg(&ssif_info->client->dev,
1007 					"Error from i2c_non_blocking_op(3)\n");
1008 			msg_done_handler(ssif_info, -EIO, NULL, 0);
1009 		}
1010 	} else {
1011 		/* Ready to request the result. */
1012 		unsigned long oflags, *flags;
1013 
1014 		ssif_inc_stat(ssif_info, sent_messages);
1015 		ssif_inc_stat(ssif_info, sent_messages_parts);
1016 
1017 		flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1018 		if (ssif_info->got_alert) {
1019 			/* The result is already ready, just start it. */
1020 			ssif_info->got_alert = false;
1021 			ipmi_ssif_unlock_cond(ssif_info, flags);
1022 			start_get(ssif_info);
1023 		} else {
1024 			/* Wait a jiffie then request the next message */
1025 			ssif_info->waiting_alert = true;
1026 			ssif_info->retries_left = SSIF_RECV_RETRIES;
1027 			ssif_info->rtc_us_timer = SSIF_MSG_PART_USEC;
1028 			if (!ssif_info->stopping)
1029 				mod_timer(&ssif_info->retry_timer,
1030 					  jiffies + SSIF_MSG_PART_JIFFIES);
1031 			ipmi_ssif_unlock_cond(ssif_info, flags);
1032 		}
1033 	}
1034 }
1035 
start_resend(struct ssif_info * ssif_info)1036 static int start_resend(struct ssif_info *ssif_info)
1037 {
1038 	int rv;
1039 	int command;
1040 
1041 	ssif_info->got_alert = false;
1042 
1043 	if (ssif_info->data_len > 32) {
1044 		command = SSIF_IPMI_MULTI_PART_REQUEST_START;
1045 		ssif_info->multi_data = ssif_info->data;
1046 		ssif_info->multi_len = ssif_info->data_len;
1047 		/*
1048 		 * Subtle thing, this is 32, not 33, because we will
1049 		 * overwrite the thing at position 32 (which was just
1050 		 * transmitted) with the new length.
1051 		 */
1052 		ssif_info->multi_pos = 32;
1053 		ssif_info->data[0] = 32;
1054 	} else {
1055 		ssif_info->multi_data = NULL;
1056 		command = SSIF_IPMI_REQUEST;
1057 		ssif_info->data[0] = ssif_info->data_len;
1058 	}
1059 
1060 	rv = ssif_i2c_send(ssif_info, msg_written_handler, I2C_SMBUS_WRITE,
1061 			  command, ssif_info->data, I2C_SMBUS_BLOCK_DATA);
1062 	if (rv && (ssif_info->ssif_debug & SSIF_DEBUG_MSG))
1063 		dev_dbg(&ssif_info->client->dev,
1064 			"Error from i2c_non_blocking_op(4)\n");
1065 	return rv;
1066 }
1067 
start_send(struct ssif_info * ssif_info,unsigned char * data,unsigned int len)1068 static int start_send(struct ssif_info *ssif_info,
1069 		      unsigned char   *data,
1070 		      unsigned int    len)
1071 {
1072 	if (len > IPMI_MAX_MSG_LENGTH)
1073 		return -E2BIG;
1074 	if (len > ssif_info->max_xmit_msg_size)
1075 		return -E2BIG;
1076 
1077 	ssif_info->retries_left = SSIF_SEND_RETRIES;
1078 	memcpy(ssif_info->data + 1, data, len);
1079 	ssif_info->data_len = len;
1080 	return start_resend(ssif_info);
1081 }
1082 
1083 /* Must be called with the message lock held. */
start_next_msg(struct ssif_info * ssif_info,unsigned long * flags)1084 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags)
1085 {
1086 	struct ipmi_smi_msg *msg;
1087 	unsigned long oflags;
1088 
1089  restart:
1090 	if (!SSIF_IDLE(ssif_info)) {
1091 		ipmi_ssif_unlock_cond(ssif_info, flags);
1092 		return;
1093 	}
1094 
1095 	if (!ssif_info->waiting_msg) {
1096 		ssif_info->curr_msg = NULL;
1097 		ipmi_ssif_unlock_cond(ssif_info, flags);
1098 	} else {
1099 		int rv;
1100 
1101 		ssif_info->curr_msg = ssif_info->waiting_msg;
1102 		ssif_info->waiting_msg = NULL;
1103 		ipmi_ssif_unlock_cond(ssif_info, flags);
1104 		rv = start_send(ssif_info,
1105 				ssif_info->curr_msg->data,
1106 				ssif_info->curr_msg->data_size);
1107 		if (rv) {
1108 			msg = ssif_info->curr_msg;
1109 			ssif_info->curr_msg = NULL;
1110 			return_hosed_msg(ssif_info, msg);
1111 			flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1112 			goto restart;
1113 		}
1114 	}
1115 }
1116 
sender(void * send_info,struct ipmi_smi_msg * msg)1117 static void sender(void                *send_info,
1118 		   struct ipmi_smi_msg *msg)
1119 {
1120 	struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1121 	unsigned long oflags, *flags;
1122 
1123 	BUG_ON(ssif_info->waiting_msg);
1124 	ssif_info->waiting_msg = msg;
1125 
1126 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1127 	start_next_msg(ssif_info, flags);
1128 
1129 	if (ssif_info->ssif_debug & SSIF_DEBUG_TIMING) {
1130 		struct timespec64 t;
1131 
1132 		ktime_get_real_ts64(&t);
1133 		dev_dbg(&ssif_info->client->dev,
1134 			"**Enqueue %02x %02x: %lld.%6.6ld\n",
1135 			msg->data[0], msg->data[1],
1136 			(long long)t.tv_sec, (long)t.tv_nsec / NSEC_PER_USEC);
1137 	}
1138 }
1139 
get_smi_info(void * send_info,struct ipmi_smi_info * data)1140 static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1141 {
1142 	struct ssif_info *ssif_info = send_info;
1143 
1144 	data->addr_src = ssif_info->addr_source;
1145 	data->dev = &ssif_info->client->dev;
1146 	data->addr_info = ssif_info->addr_info;
1147 	get_device(data->dev);
1148 
1149 	return 0;
1150 }
1151 
1152 /*
1153  * Upper layer wants us to request events.
1154  */
request_events(void * send_info)1155 static void request_events(void *send_info)
1156 {
1157 	struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1158 	unsigned long oflags, *flags;
1159 
1160 	if (!ssif_info->has_event_buffer)
1161 		return;
1162 
1163 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1164 	ssif_info->req_events = true;
1165 	ipmi_ssif_unlock_cond(ssif_info, flags);
1166 }
1167 
1168 /*
1169  * Upper layer is changing the flag saying whether we need to request
1170  * flags periodically or not.
1171  */
ssif_set_need_watch(void * send_info,unsigned int watch_mask)1172 static void ssif_set_need_watch(void *send_info, unsigned int watch_mask)
1173 {
1174 	struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1175 	unsigned long oflags, *flags;
1176 	long timeout = 0;
1177 
1178 	if (watch_mask & IPMI_WATCH_MASK_CHECK_MESSAGES)
1179 		timeout = SSIF_WATCH_MSG_TIMEOUT;
1180 	else if (watch_mask)
1181 		timeout = SSIF_WATCH_WATCHDOG_TIMEOUT;
1182 
1183 	flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1184 	if (timeout != ssif_info->watch_timeout) {
1185 		ssif_info->watch_timeout = timeout;
1186 		if (ssif_info->watch_timeout)
1187 			mod_timer(&ssif_info->watch_timer,
1188 				  jiffies + ssif_info->watch_timeout);
1189 	}
1190 	ipmi_ssif_unlock_cond(ssif_info, flags);
1191 }
1192 
ssif_start_processing(void * send_info,struct ipmi_smi * intf)1193 static int ssif_start_processing(void            *send_info,
1194 				 struct ipmi_smi *intf)
1195 {
1196 	struct ssif_info *ssif_info = send_info;
1197 
1198 	ssif_info->intf = intf;
1199 
1200 	return 0;
1201 }
1202 
1203 #define MAX_SSIF_BMCS 4
1204 
1205 static unsigned short addr[MAX_SSIF_BMCS];
1206 static int num_addrs;
1207 module_param_array(addr, ushort, &num_addrs, 0);
1208 MODULE_PARM_DESC(addr, "The addresses to scan for IPMI BMCs on the SSIFs.");
1209 
1210 static char *adapter_name[MAX_SSIF_BMCS];
1211 static int num_adapter_names;
1212 module_param_array(adapter_name, charp, &num_adapter_names, 0);
1213 MODULE_PARM_DESC(adapter_name, "The string name of the I2C device that has the BMC.  By default all devices are scanned.");
1214 
1215 static int slave_addrs[MAX_SSIF_BMCS];
1216 static int num_slave_addrs;
1217 module_param_array(slave_addrs, int, &num_slave_addrs, 0);
1218 MODULE_PARM_DESC(slave_addrs,
1219 		 "The default IPMB slave address for the controller.");
1220 
1221 static bool alerts_broken;
1222 module_param(alerts_broken, bool, 0);
1223 MODULE_PARM_DESC(alerts_broken, "Don't enable alerts for the controller.");
1224 
1225 /*
1226  * Bit 0 enables message debugging, bit 1 enables state debugging, and
1227  * bit 2 enables timing debugging.  This is an array indexed by
1228  * interface number"
1229  */
1230 static int dbg[MAX_SSIF_BMCS];
1231 static int num_dbg;
1232 module_param_array(dbg, int, &num_dbg, 0);
1233 MODULE_PARM_DESC(dbg, "Turn on debugging.");
1234 
1235 static bool ssif_dbg_probe;
1236 module_param_named(dbg_probe, ssif_dbg_probe, bool, 0);
1237 MODULE_PARM_DESC(dbg_probe, "Enable debugging of probing of adapters.");
1238 
1239 static bool ssif_tryacpi = true;
1240 module_param_named(tryacpi, ssif_tryacpi, bool, 0);
1241 MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
1242 
1243 static bool ssif_trydmi = true;
1244 module_param_named(trydmi, ssif_trydmi, bool, 0);
1245 MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the default scan of the interfaces identified via DMI (SMBIOS)");
1246 
1247 static DEFINE_MUTEX(ssif_infos_mutex);
1248 static LIST_HEAD(ssif_infos);
1249 
1250 #define IPMI_SSIF_ATTR(name) \
1251 static ssize_t ipmi_##name##_show(struct device *dev,			\
1252 				  struct device_attribute *attr,	\
1253 				  char *buf)				\
1254 {									\
1255 	struct ssif_info *ssif_info = dev_get_drvdata(dev);		\
1256 									\
1257 	return snprintf(buf, 10, "%u\n", ssif_get_stat(ssif_info, name));\
1258 }									\
1259 static DEVICE_ATTR(name, S_IRUGO, ipmi_##name##_show, NULL)
1260 
ipmi_type_show(struct device * dev,struct device_attribute * attr,char * buf)1261 static ssize_t ipmi_type_show(struct device *dev,
1262 			      struct device_attribute *attr,
1263 			      char *buf)
1264 {
1265 	return snprintf(buf, 10, "ssif\n");
1266 }
1267 static DEVICE_ATTR(type, S_IRUGO, ipmi_type_show, NULL);
1268 
1269 IPMI_SSIF_ATTR(sent_messages);
1270 IPMI_SSIF_ATTR(sent_messages_parts);
1271 IPMI_SSIF_ATTR(send_retries);
1272 IPMI_SSIF_ATTR(send_errors);
1273 IPMI_SSIF_ATTR(received_messages);
1274 IPMI_SSIF_ATTR(received_message_parts);
1275 IPMI_SSIF_ATTR(receive_retries);
1276 IPMI_SSIF_ATTR(receive_errors);
1277 IPMI_SSIF_ATTR(flag_fetches);
1278 IPMI_SSIF_ATTR(hosed);
1279 IPMI_SSIF_ATTR(events);
1280 IPMI_SSIF_ATTR(watchdog_pretimeouts);
1281 IPMI_SSIF_ATTR(alerts);
1282 
1283 static struct attribute *ipmi_ssif_dev_attrs[] = {
1284 	&dev_attr_type.attr,
1285 	&dev_attr_sent_messages.attr,
1286 	&dev_attr_sent_messages_parts.attr,
1287 	&dev_attr_send_retries.attr,
1288 	&dev_attr_send_errors.attr,
1289 	&dev_attr_received_messages.attr,
1290 	&dev_attr_received_message_parts.attr,
1291 	&dev_attr_receive_retries.attr,
1292 	&dev_attr_receive_errors.attr,
1293 	&dev_attr_flag_fetches.attr,
1294 	&dev_attr_hosed.attr,
1295 	&dev_attr_events.attr,
1296 	&dev_attr_watchdog_pretimeouts.attr,
1297 	&dev_attr_alerts.attr,
1298 	NULL
1299 };
1300 
1301 static const struct attribute_group ipmi_ssif_dev_attr_group = {
1302 	.attrs		= ipmi_ssif_dev_attrs,
1303 };
1304 
shutdown_ssif(void * send_info)1305 static void shutdown_ssif(void *send_info)
1306 {
1307 	struct ssif_info *ssif_info = send_info;
1308 
1309 	device_remove_group(&ssif_info->client->dev, &ipmi_ssif_dev_attr_group);
1310 	dev_set_drvdata(&ssif_info->client->dev, NULL);
1311 
1312 	/* make sure the driver is not looking for flags any more. */
1313 	while (ssif_info->ssif_state != SSIF_NORMAL)
1314 		schedule_timeout(1);
1315 
1316 	ssif_info->stopping = true;
1317 	del_timer_sync(&ssif_info->watch_timer);
1318 	del_timer_sync(&ssif_info->retry_timer);
1319 	if (ssif_info->thread) {
1320 		complete(&ssif_info->wake_thread);
1321 		kthread_stop(ssif_info->thread);
1322 	}
1323 }
1324 
ssif_remove(struct i2c_client * client)1325 static int ssif_remove(struct i2c_client *client)
1326 {
1327 	struct ssif_info *ssif_info = i2c_get_clientdata(client);
1328 	struct ssif_addr_info *addr_info;
1329 
1330 	if (!ssif_info)
1331 		return 0;
1332 
1333 	/*
1334 	 * After this point, we won't deliver anything asychronously
1335 	 * to the message handler.  We can unregister ourself.
1336 	 */
1337 	ipmi_unregister_smi(ssif_info->intf);
1338 
1339 	list_for_each_entry(addr_info, &ssif_infos, link) {
1340 		if (addr_info->client == client) {
1341 			addr_info->client = NULL;
1342 			break;
1343 		}
1344 	}
1345 
1346 	kfree(ssif_info);
1347 
1348 	return 0;
1349 }
1350 
read_response(struct i2c_client * client,unsigned char * resp)1351 static int read_response(struct i2c_client *client, unsigned char *resp)
1352 {
1353 	int ret = -ENODEV, retry_cnt = SSIF_RECV_RETRIES;
1354 
1355 	while (retry_cnt > 0) {
1356 		ret = i2c_smbus_read_block_data(client, SSIF_IPMI_RESPONSE,
1357 						resp);
1358 		if (ret > 0)
1359 			break;
1360 		msleep(SSIF_MSG_MSEC);
1361 		retry_cnt--;
1362 		if (retry_cnt <= 0)
1363 			break;
1364 	}
1365 
1366 	return ret;
1367 }
1368 
do_cmd(struct i2c_client * client,int len,unsigned char * msg,int * resp_len,unsigned char * resp)1369 static int do_cmd(struct i2c_client *client, int len, unsigned char *msg,
1370 		  int *resp_len, unsigned char *resp)
1371 {
1372 	int retry_cnt;
1373 	int ret;
1374 
1375 	retry_cnt = SSIF_SEND_RETRIES;
1376  retry1:
1377 	ret = i2c_smbus_write_block_data(client, SSIF_IPMI_REQUEST, len, msg);
1378 	if (ret) {
1379 		retry_cnt--;
1380 		if (retry_cnt > 0)
1381 			goto retry1;
1382 		return -ENODEV;
1383 	}
1384 
1385 	ret = read_response(client, resp);
1386 	if (ret > 0) {
1387 		/* Validate that the response is correct. */
1388 		if (ret < 3 ||
1389 		    (resp[0] != (msg[0] | (1 << 2))) ||
1390 		    (resp[1] != msg[1]))
1391 			ret = -EINVAL;
1392 		else if (ret > IPMI_MAX_MSG_LENGTH) {
1393 			ret = -E2BIG;
1394 		} else {
1395 			*resp_len = ret;
1396 			ret = 0;
1397 		}
1398 	}
1399 
1400 	return ret;
1401 }
1402 
ssif_detect(struct i2c_client * client,struct i2c_board_info * info)1403 static int ssif_detect(struct i2c_client *client, struct i2c_board_info *info)
1404 {
1405 	unsigned char *resp;
1406 	unsigned char msg[3];
1407 	int           rv;
1408 	int           len;
1409 
1410 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1411 	if (!resp)
1412 		return -ENOMEM;
1413 
1414 	/* Do a Get Device ID command, since it is required. */
1415 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1416 	msg[1] = IPMI_GET_DEVICE_ID_CMD;
1417 	rv = do_cmd(client, 2, msg, &len, resp);
1418 	if (rv)
1419 		rv = -ENODEV;
1420 	else
1421 		strlcpy(info->type, DEVICE_NAME, I2C_NAME_SIZE);
1422 	kfree(resp);
1423 	return rv;
1424 }
1425 
strcmp_nospace(char * s1,char * s2)1426 static int strcmp_nospace(char *s1, char *s2)
1427 {
1428 	while (*s1 && *s2) {
1429 		while (isspace(*s1))
1430 			s1++;
1431 		while (isspace(*s2))
1432 			s2++;
1433 		if (*s1 > *s2)
1434 			return 1;
1435 		if (*s1 < *s2)
1436 			return -1;
1437 		s1++;
1438 		s2++;
1439 	}
1440 	return 0;
1441 }
1442 
ssif_info_find(unsigned short addr,char * adapter_name,bool match_null_name)1443 static struct ssif_addr_info *ssif_info_find(unsigned short addr,
1444 					     char *adapter_name,
1445 					     bool match_null_name)
1446 {
1447 	struct ssif_addr_info *info, *found = NULL;
1448 
1449 restart:
1450 	list_for_each_entry(info, &ssif_infos, link) {
1451 		if (info->binfo.addr == addr) {
1452 			if (info->addr_src == SI_SMBIOS)
1453 				info->adapter_name = kstrdup(adapter_name,
1454 							     GFP_KERNEL);
1455 
1456 			if (info->adapter_name || adapter_name) {
1457 				if (!info->adapter_name != !adapter_name) {
1458 					/* One is NULL and one is not */
1459 					continue;
1460 				}
1461 				if (adapter_name &&
1462 				    strcmp_nospace(info->adapter_name,
1463 						   adapter_name))
1464 					/* Names do not match */
1465 					continue;
1466 			}
1467 			found = info;
1468 			break;
1469 		}
1470 	}
1471 
1472 	if (!found && match_null_name) {
1473 		/* Try to get an exact match first, then try with a NULL name */
1474 		adapter_name = NULL;
1475 		match_null_name = false;
1476 		goto restart;
1477 	}
1478 
1479 	return found;
1480 }
1481 
check_acpi(struct ssif_info * ssif_info,struct device * dev)1482 static bool check_acpi(struct ssif_info *ssif_info, struct device *dev)
1483 {
1484 #ifdef CONFIG_ACPI
1485 	acpi_handle acpi_handle;
1486 
1487 	acpi_handle = ACPI_HANDLE(dev);
1488 	if (acpi_handle) {
1489 		ssif_info->addr_source = SI_ACPI;
1490 		ssif_info->addr_info.acpi_info.acpi_handle = acpi_handle;
1491 		request_module("acpi_ipmi");
1492 		return true;
1493 	}
1494 #endif
1495 	return false;
1496 }
1497 
find_slave_address(struct i2c_client * client,int slave_addr)1498 static int find_slave_address(struct i2c_client *client, int slave_addr)
1499 {
1500 #ifdef CONFIG_IPMI_DMI_DECODE
1501 	if (!slave_addr)
1502 		slave_addr = ipmi_dmi_get_slave_addr(
1503 			SI_TYPE_INVALID,
1504 			i2c_adapter_id(client->adapter),
1505 			client->addr);
1506 #endif
1507 
1508 	return slave_addr;
1509 }
1510 
start_multipart_test(struct i2c_client * client,unsigned char * msg,bool do_middle)1511 static int start_multipart_test(struct i2c_client *client,
1512 				unsigned char *msg, bool do_middle)
1513 {
1514 	int retry_cnt = SSIF_SEND_RETRIES, ret;
1515 
1516 retry_write:
1517 	ret = i2c_smbus_write_block_data(client,
1518 					 SSIF_IPMI_MULTI_PART_REQUEST_START,
1519 					 32, msg);
1520 	if (ret) {
1521 		retry_cnt--;
1522 		if (retry_cnt > 0)
1523 			goto retry_write;
1524 		dev_err(&client->dev, "Could not write multi-part start, though the BMC said it could handle it.  Just limit sends to one part.\n");
1525 		return ret;
1526 	}
1527 
1528 	if (!do_middle)
1529 		return 0;
1530 
1531 	ret = i2c_smbus_write_block_data(client,
1532 					 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
1533 					 32, msg + 32);
1534 	if (ret) {
1535 		dev_err(&client->dev, "Could not write multi-part middle, though the BMC said it could handle it.  Just limit sends to one part.\n");
1536 		return ret;
1537 	}
1538 
1539 	return 0;
1540 }
1541 
test_multipart_messages(struct i2c_client * client,struct ssif_info * ssif_info,unsigned char * resp)1542 static void test_multipart_messages(struct i2c_client *client,
1543 				    struct ssif_info *ssif_info,
1544 				    unsigned char *resp)
1545 {
1546 	unsigned char msg[65];
1547 	int ret;
1548 	bool do_middle;
1549 
1550 	if (ssif_info->max_xmit_msg_size <= 32)
1551 		return;
1552 
1553 	do_middle = ssif_info->max_xmit_msg_size > 63;
1554 
1555 	memset(msg, 0, sizeof(msg));
1556 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1557 	msg[1] = IPMI_GET_DEVICE_ID_CMD;
1558 
1559 	/*
1560 	 * The specification is all messed up dealing with sending
1561 	 * multi-part messages.  Per what the specification says, it
1562 	 * is impossible to send a message that is a multiple of 32
1563 	 * bytes, except for 32 itself.  It talks about a "start"
1564 	 * transaction (cmd=6) that must be 32 bytes, "middle"
1565 	 * transaction (cmd=7) that must be 32 bytes, and an "end"
1566 	 * transaction.  The "end" transaction is shown as cmd=7 in
1567 	 * the text, but if that's the case there is no way to
1568 	 * differentiate between a middle and end part except the
1569 	 * length being less than 32.  But there is a table at the far
1570 	 * end of the section (that I had never noticed until someone
1571 	 * pointed it out to me) that mentions it as cmd=8.
1572 	 *
1573 	 * After some thought, I think the example is wrong and the
1574 	 * end transaction should be cmd=8.  But some systems don't
1575 	 * implement cmd=8, they use a zero-length end transaction,
1576 	 * even though that violates the SMBus specification.
1577 	 *
1578 	 * So, to work around this, this code tests if cmd=8 works.
1579 	 * If it does, then we use that.  If not, it tests zero-
1580 	 * byte end transactions.  If that works, good.  If not,
1581 	 * we only allow 63-byte transactions max.
1582 	 */
1583 
1584 	ret = start_multipart_test(client, msg, do_middle);
1585 	if (ret)
1586 		goto out_no_multi_part;
1587 
1588 	ret = i2c_smbus_write_block_data(client,
1589 					 SSIF_IPMI_MULTI_PART_REQUEST_END,
1590 					 1, msg + 64);
1591 
1592 	if (!ret)
1593 		ret = read_response(client, resp);
1594 
1595 	if (ret > 0) {
1596 		/* End transactions work, we are good. */
1597 		ssif_info->cmd8_works = true;
1598 		return;
1599 	}
1600 
1601 	ret = start_multipart_test(client, msg, do_middle);
1602 	if (ret) {
1603 		dev_err(&client->dev, "Second multipart test failed.\n");
1604 		goto out_no_multi_part;
1605 	}
1606 
1607 	ret = i2c_smbus_write_block_data(client,
1608 					 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
1609 					 0, msg + 64);
1610 	if (!ret)
1611 		ret = read_response(client, resp);
1612 	if (ret > 0)
1613 		/* Zero-size end parts work, use those. */
1614 		return;
1615 
1616 	/* Limit to 63 bytes and use a short middle command to mark the end. */
1617 	if (ssif_info->max_xmit_msg_size > 63)
1618 		ssif_info->max_xmit_msg_size = 63;
1619 	return;
1620 
1621 out_no_multi_part:
1622 	ssif_info->max_xmit_msg_size = 32;
1623 	return;
1624 }
1625 
1626 /*
1627  * Global enables we care about.
1628  */
1629 #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
1630 			     IPMI_BMC_EVT_MSG_INTR)
1631 
ssif_remove_dup(struct i2c_client * client)1632 static void ssif_remove_dup(struct i2c_client *client)
1633 {
1634 	struct ssif_info *ssif_info = i2c_get_clientdata(client);
1635 
1636 	ipmi_unregister_smi(ssif_info->intf);
1637 	kfree(ssif_info);
1638 }
1639 
ssif_add_infos(struct i2c_client * client)1640 static int ssif_add_infos(struct i2c_client *client)
1641 {
1642 	struct ssif_addr_info *info;
1643 
1644 	info = kzalloc(sizeof(*info), GFP_KERNEL);
1645 	if (!info)
1646 		return -ENOMEM;
1647 	info->addr_src = SI_ACPI;
1648 	info->client = client;
1649 	info->adapter_name = kstrdup(client->adapter->name, GFP_KERNEL);
1650 	info->binfo.addr = client->addr;
1651 	list_add_tail(&info->link, &ssif_infos);
1652 	return 0;
1653 }
1654 
1655 /*
1656  * Prefer ACPI over SMBIOS, if both are available.
1657  * So if we get an ACPI interface and have already registered a SMBIOS
1658  * interface at the same address, remove the SMBIOS and add the ACPI one.
1659  */
ssif_check_and_remove(struct i2c_client * client,struct ssif_info * ssif_info)1660 static int ssif_check_and_remove(struct i2c_client *client,
1661 			      struct ssif_info *ssif_info)
1662 {
1663 	struct ssif_addr_info *info;
1664 
1665 	list_for_each_entry(info, &ssif_infos, link) {
1666 		if (!info->client)
1667 			return 0;
1668 		if (!strcmp(info->adapter_name, client->adapter->name) &&
1669 		    info->binfo.addr == client->addr) {
1670 			if (info->addr_src == SI_ACPI)
1671 				return -EEXIST;
1672 
1673 			if (ssif_info->addr_source == SI_ACPI &&
1674 			    info->addr_src == SI_SMBIOS) {
1675 				dev_info(&client->dev,
1676 					 "Removing %s-specified SSIF interface in favor of ACPI\n",
1677 					 ipmi_addr_src_to_str(info->addr_src));
1678 				ssif_remove_dup(info->client);
1679 				return 0;
1680 			}
1681 		}
1682 	}
1683 	return 0;
1684 }
1685 
ssif_probe(struct i2c_client * client,const struct i2c_device_id * id)1686 static int ssif_probe(struct i2c_client *client, const struct i2c_device_id *id)
1687 {
1688 	unsigned char     msg[3];
1689 	unsigned char     *resp;
1690 	struct ssif_info   *ssif_info;
1691 	int               rv = 0;
1692 	int               len;
1693 	int               i;
1694 	u8		  slave_addr = 0;
1695 	struct ssif_addr_info *addr_info = NULL;
1696 
1697 	mutex_lock(&ssif_infos_mutex);
1698 	resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1699 	if (!resp) {
1700 		mutex_unlock(&ssif_infos_mutex);
1701 		return -ENOMEM;
1702 	}
1703 
1704 	ssif_info = kzalloc(sizeof(*ssif_info), GFP_KERNEL);
1705 	if (!ssif_info) {
1706 		kfree(resp);
1707 		mutex_unlock(&ssif_infos_mutex);
1708 		return -ENOMEM;
1709 	}
1710 
1711 	if (!check_acpi(ssif_info, &client->dev)) {
1712 		addr_info = ssif_info_find(client->addr, client->adapter->name,
1713 					   true);
1714 		if (!addr_info) {
1715 			/* Must have come in through sysfs. */
1716 			ssif_info->addr_source = SI_HOTMOD;
1717 		} else {
1718 			ssif_info->addr_source = addr_info->addr_src;
1719 			ssif_info->ssif_debug = addr_info->debug;
1720 			ssif_info->addr_info = addr_info->addr_info;
1721 			addr_info->client = client;
1722 			slave_addr = addr_info->slave_addr;
1723 		}
1724 	}
1725 
1726 	ssif_info->client = client;
1727 	i2c_set_clientdata(client, ssif_info);
1728 
1729 	rv = ssif_check_and_remove(client, ssif_info);
1730 	/* If rv is 0 and addr source is not SI_ACPI, continue probing */
1731 	if (!rv && ssif_info->addr_source == SI_ACPI) {
1732 		rv = ssif_add_infos(client);
1733 		if (rv) {
1734 			dev_err(&client->dev, "Out of memory!, exiting ..\n");
1735 			goto out;
1736 		}
1737 	} else if (rv) {
1738 		dev_err(&client->dev, "Not probing, Interface already present\n");
1739 		goto out;
1740 	}
1741 
1742 	slave_addr = find_slave_address(client, slave_addr);
1743 
1744 	dev_info(&client->dev,
1745 		 "Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
1746 		ipmi_addr_src_to_str(ssif_info->addr_source),
1747 		client->addr, client->adapter->name, slave_addr);
1748 
1749 	/* Now check for system interface capabilities */
1750 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1751 	msg[1] = IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD;
1752 	msg[2] = 0; /* SSIF */
1753 	rv = do_cmd(client, 3, msg, &len, resp);
1754 	if (!rv && (len >= 3) && (resp[2] == 0)) {
1755 		if (len < 7) {
1756 			if (ssif_dbg_probe)
1757 				dev_dbg(&ssif_info->client->dev,
1758 					"SSIF info too short: %d\n", len);
1759 			goto no_support;
1760 		}
1761 
1762 		/* Got a good SSIF response, handle it. */
1763 		ssif_info->max_xmit_msg_size = resp[5];
1764 		ssif_info->max_recv_msg_size = resp[6];
1765 		ssif_info->multi_support = (resp[4] >> 6) & 0x3;
1766 		ssif_info->supports_pec = (resp[4] >> 3) & 0x1;
1767 
1768 		/* Sanitize the data */
1769 		switch (ssif_info->multi_support) {
1770 		case SSIF_NO_MULTI:
1771 			if (ssif_info->max_xmit_msg_size > 32)
1772 				ssif_info->max_xmit_msg_size = 32;
1773 			if (ssif_info->max_recv_msg_size > 32)
1774 				ssif_info->max_recv_msg_size = 32;
1775 			break;
1776 
1777 		case SSIF_MULTI_2_PART:
1778 			if (ssif_info->max_xmit_msg_size > 63)
1779 				ssif_info->max_xmit_msg_size = 63;
1780 			if (ssif_info->max_recv_msg_size > 62)
1781 				ssif_info->max_recv_msg_size = 62;
1782 			break;
1783 
1784 		case SSIF_MULTI_n_PART:
1785 			/* We take whatever size given, but do some testing. */
1786 			break;
1787 
1788 		default:
1789 			/* Data is not sane, just give up. */
1790 			goto no_support;
1791 		}
1792 	} else {
1793  no_support:
1794 		/* Assume no multi-part or PEC support */
1795 		dev_info(&ssif_info->client->dev,
1796 			 "Error fetching SSIF: %d %d %2.2x, your system probably doesn't support this command so using defaults\n",
1797 			rv, len, resp[2]);
1798 
1799 		ssif_info->max_xmit_msg_size = 32;
1800 		ssif_info->max_recv_msg_size = 32;
1801 		ssif_info->multi_support = SSIF_NO_MULTI;
1802 		ssif_info->supports_pec = 0;
1803 	}
1804 
1805 	test_multipart_messages(client, ssif_info, resp);
1806 
1807 	/* Make sure the NMI timeout is cleared. */
1808 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1809 	msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
1810 	msg[2] = WDT_PRE_TIMEOUT_INT;
1811 	rv = do_cmd(client, 3, msg, &len, resp);
1812 	if (rv || (len < 3) || (resp[2] != 0))
1813 		dev_warn(&ssif_info->client->dev,
1814 			 "Unable to clear message flags: %d %d %2.2x\n",
1815 			 rv, len, resp[2]);
1816 
1817 	/* Attempt to enable the event buffer. */
1818 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1819 	msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
1820 	rv = do_cmd(client, 2, msg, &len, resp);
1821 	if (rv || (len < 4) || (resp[2] != 0)) {
1822 		dev_warn(&ssif_info->client->dev,
1823 			 "Error getting global enables: %d %d %2.2x\n",
1824 			 rv, len, resp[2]);
1825 		rv = 0; /* Not fatal */
1826 		goto found;
1827 	}
1828 
1829 	ssif_info->global_enables = resp[3];
1830 
1831 	if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
1832 		ssif_info->has_event_buffer = true;
1833 		/* buffer is already enabled, nothing to do. */
1834 		goto found;
1835 	}
1836 
1837 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1838 	msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1839 	msg[2] = ssif_info->global_enables | IPMI_BMC_EVT_MSG_BUFF;
1840 	rv = do_cmd(client, 3, msg, &len, resp);
1841 	if (rv || (len < 2)) {
1842 		dev_warn(&ssif_info->client->dev,
1843 			 "Error setting global enables: %d %d %2.2x\n",
1844 			 rv, len, resp[2]);
1845 		rv = 0; /* Not fatal */
1846 		goto found;
1847 	}
1848 
1849 	if (resp[2] == 0) {
1850 		/* A successful return means the event buffer is supported. */
1851 		ssif_info->has_event_buffer = true;
1852 		ssif_info->global_enables |= IPMI_BMC_EVT_MSG_BUFF;
1853 	}
1854 
1855 	/* Some systems don't behave well if you enable alerts. */
1856 	if (alerts_broken)
1857 		goto found;
1858 
1859 	msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1860 	msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1861 	msg[2] = ssif_info->global_enables | IPMI_BMC_RCV_MSG_INTR;
1862 	rv = do_cmd(client, 3, msg, &len, resp);
1863 	if (rv || (len < 2)) {
1864 		dev_warn(&ssif_info->client->dev,
1865 			 "Error setting global enables: %d %d %2.2x\n",
1866 			 rv, len, resp[2]);
1867 		rv = 0; /* Not fatal */
1868 		goto found;
1869 	}
1870 
1871 	if (resp[2] == 0) {
1872 		/* A successful return means the alert is supported. */
1873 		ssif_info->supports_alert = true;
1874 		ssif_info->global_enables |= IPMI_BMC_RCV_MSG_INTR;
1875 	}
1876 
1877  found:
1878 	if (ssif_dbg_probe) {
1879 		dev_dbg(&ssif_info->client->dev,
1880 		       "%s: i2c_probe found device at i2c address %x\n",
1881 		       __func__, client->addr);
1882 	}
1883 
1884 	spin_lock_init(&ssif_info->lock);
1885 	ssif_info->ssif_state = SSIF_NORMAL;
1886 	timer_setup(&ssif_info->retry_timer, retry_timeout, 0);
1887 	timer_setup(&ssif_info->watch_timer, watch_timeout, 0);
1888 
1889 	for (i = 0; i < SSIF_NUM_STATS; i++)
1890 		atomic_set(&ssif_info->stats[i], 0);
1891 
1892 	if (ssif_info->supports_pec)
1893 		ssif_info->client->flags |= I2C_CLIENT_PEC;
1894 
1895 	ssif_info->handlers.owner = THIS_MODULE;
1896 	ssif_info->handlers.start_processing = ssif_start_processing;
1897 	ssif_info->handlers.shutdown = shutdown_ssif;
1898 	ssif_info->handlers.get_smi_info = get_smi_info;
1899 	ssif_info->handlers.sender = sender;
1900 	ssif_info->handlers.request_events = request_events;
1901 	ssif_info->handlers.set_need_watch = ssif_set_need_watch;
1902 
1903 	{
1904 		unsigned int thread_num;
1905 
1906 		thread_num = ((i2c_adapter_id(ssif_info->client->adapter)
1907 			       << 8) |
1908 			      ssif_info->client->addr);
1909 		init_completion(&ssif_info->wake_thread);
1910 		ssif_info->thread = kthread_run(ipmi_ssif_thread, ssif_info,
1911 					       "kssif%4.4x", thread_num);
1912 		if (IS_ERR(ssif_info->thread)) {
1913 			rv = PTR_ERR(ssif_info->thread);
1914 			dev_notice(&ssif_info->client->dev,
1915 				   "Could not start kernel thread: error %d\n",
1916 				   rv);
1917 			goto out;
1918 		}
1919 	}
1920 
1921 	dev_set_drvdata(&ssif_info->client->dev, ssif_info);
1922 	rv = device_add_group(&ssif_info->client->dev,
1923 			      &ipmi_ssif_dev_attr_group);
1924 	if (rv) {
1925 		dev_err(&ssif_info->client->dev,
1926 			"Unable to add device attributes: error %d\n",
1927 			rv);
1928 		goto out;
1929 	}
1930 
1931 	rv = ipmi_register_smi(&ssif_info->handlers,
1932 			       ssif_info,
1933 			       &ssif_info->client->dev,
1934 			       slave_addr);
1935 	if (rv) {
1936 		dev_err(&ssif_info->client->dev,
1937 			"Unable to register device: error %d\n", rv);
1938 		goto out_remove_attr;
1939 	}
1940 
1941  out:
1942 	if (rv) {
1943 		if (addr_info)
1944 			addr_info->client = NULL;
1945 
1946 		dev_err(&ssif_info->client->dev,
1947 			"Unable to start IPMI SSIF: %d\n", rv);
1948 		i2c_set_clientdata(client, NULL);
1949 		kfree(ssif_info);
1950 	}
1951 	kfree(resp);
1952 	mutex_unlock(&ssif_infos_mutex);
1953 	return rv;
1954 
1955 out_remove_attr:
1956 	device_remove_group(&ssif_info->client->dev, &ipmi_ssif_dev_attr_group);
1957 	dev_set_drvdata(&ssif_info->client->dev, NULL);
1958 	goto out;
1959 }
1960 
new_ssif_client(int addr,char * adapter_name,int debug,int slave_addr,enum ipmi_addr_src addr_src,struct device * dev)1961 static int new_ssif_client(int addr, char *adapter_name,
1962 			   int debug, int slave_addr,
1963 			   enum ipmi_addr_src addr_src,
1964 			   struct device *dev)
1965 {
1966 	struct ssif_addr_info *addr_info;
1967 	int rv = 0;
1968 
1969 	mutex_lock(&ssif_infos_mutex);
1970 	if (ssif_info_find(addr, adapter_name, false)) {
1971 		rv = -EEXIST;
1972 		goto out_unlock;
1973 	}
1974 
1975 	addr_info = kzalloc(sizeof(*addr_info), GFP_KERNEL);
1976 	if (!addr_info) {
1977 		rv = -ENOMEM;
1978 		goto out_unlock;
1979 	}
1980 
1981 	if (adapter_name) {
1982 		addr_info->adapter_name = kstrdup(adapter_name, GFP_KERNEL);
1983 		if (!addr_info->adapter_name) {
1984 			kfree(addr_info);
1985 			rv = -ENOMEM;
1986 			goto out_unlock;
1987 		}
1988 	}
1989 
1990 	strncpy(addr_info->binfo.type, DEVICE_NAME,
1991 		sizeof(addr_info->binfo.type));
1992 	addr_info->binfo.addr = addr;
1993 	addr_info->binfo.platform_data = addr_info;
1994 	addr_info->debug = debug;
1995 	addr_info->slave_addr = slave_addr;
1996 	addr_info->addr_src = addr_src;
1997 	addr_info->dev = dev;
1998 
1999 	if (dev)
2000 		dev_set_drvdata(dev, addr_info);
2001 
2002 	list_add_tail(&addr_info->link, &ssif_infos);
2003 
2004 	/* Address list will get it */
2005 
2006 out_unlock:
2007 	mutex_unlock(&ssif_infos_mutex);
2008 	return rv;
2009 }
2010 
free_ssif_clients(void)2011 static void free_ssif_clients(void)
2012 {
2013 	struct ssif_addr_info *info, *tmp;
2014 
2015 	mutex_lock(&ssif_infos_mutex);
2016 	list_for_each_entry_safe(info, tmp, &ssif_infos, link) {
2017 		list_del(&info->link);
2018 		kfree(info->adapter_name);
2019 		kfree(info);
2020 	}
2021 	mutex_unlock(&ssif_infos_mutex);
2022 }
2023 
ssif_address_list(void)2024 static unsigned short *ssif_address_list(void)
2025 {
2026 	struct ssif_addr_info *info;
2027 	unsigned int count = 0, i = 0;
2028 	unsigned short *address_list;
2029 
2030 	list_for_each_entry(info, &ssif_infos, link)
2031 		count++;
2032 
2033 	address_list = kcalloc(count + 1, sizeof(*address_list),
2034 			       GFP_KERNEL);
2035 	if (!address_list)
2036 		return NULL;
2037 
2038 	list_for_each_entry(info, &ssif_infos, link) {
2039 		unsigned short addr = info->binfo.addr;
2040 		int j;
2041 
2042 		for (j = 0; j < i; j++) {
2043 			if (address_list[j] == addr)
2044 				/* Found a dup. */
2045 				break;
2046 		}
2047 		if (j == i) /* Didn't find it in the list. */
2048 			address_list[i++] = addr;
2049 	}
2050 	address_list[i] = I2C_CLIENT_END;
2051 
2052 	return address_list;
2053 }
2054 
2055 #ifdef CONFIG_ACPI
2056 static const struct acpi_device_id ssif_acpi_match[] = {
2057 	{ "IPI0001", 0 },
2058 	{ },
2059 };
2060 MODULE_DEVICE_TABLE(acpi, ssif_acpi_match);
2061 #endif
2062 
2063 #ifdef CONFIG_DMI
dmi_ipmi_probe(struct platform_device * pdev)2064 static int dmi_ipmi_probe(struct platform_device *pdev)
2065 {
2066 	u8 slave_addr = 0;
2067 	u16 i2c_addr;
2068 	int rv;
2069 
2070 	if (!ssif_trydmi)
2071 		return -ENODEV;
2072 
2073 	rv = device_property_read_u16(&pdev->dev, "i2c-addr", &i2c_addr);
2074 	if (rv) {
2075 		dev_warn(&pdev->dev, "No i2c-addr property\n");
2076 		return -ENODEV;
2077 	}
2078 
2079 	rv = device_property_read_u8(&pdev->dev, "slave-addr", &slave_addr);
2080 	if (rv)
2081 		slave_addr = 0x20;
2082 
2083 	return new_ssif_client(i2c_addr, NULL, 0,
2084 			       slave_addr, SI_SMBIOS, &pdev->dev);
2085 }
2086 #else
dmi_ipmi_probe(struct platform_device * pdev)2087 static int dmi_ipmi_probe(struct platform_device *pdev)
2088 {
2089 	return -ENODEV;
2090 }
2091 #endif
2092 
2093 static const struct i2c_device_id ssif_id[] = {
2094 	{ DEVICE_NAME, 0 },
2095 	{ }
2096 };
2097 MODULE_DEVICE_TABLE(i2c, ssif_id);
2098 
2099 static struct i2c_driver ssif_i2c_driver = {
2100 	.class		= I2C_CLASS_HWMON,
2101 	.driver		= {
2102 		.name			= DEVICE_NAME
2103 	},
2104 	.probe		= ssif_probe,
2105 	.remove		= ssif_remove,
2106 	.alert		= ssif_alert,
2107 	.id_table	= ssif_id,
2108 	.detect		= ssif_detect
2109 };
2110 
ssif_platform_probe(struct platform_device * dev)2111 static int ssif_platform_probe(struct platform_device *dev)
2112 {
2113 	return dmi_ipmi_probe(dev);
2114 }
2115 
ssif_platform_remove(struct platform_device * dev)2116 static int ssif_platform_remove(struct platform_device *dev)
2117 {
2118 	struct ssif_addr_info *addr_info = dev_get_drvdata(&dev->dev);
2119 
2120 	if (!addr_info)
2121 		return 0;
2122 
2123 	mutex_lock(&ssif_infos_mutex);
2124 	list_del(&addr_info->link);
2125 	kfree(addr_info);
2126 	mutex_unlock(&ssif_infos_mutex);
2127 	return 0;
2128 }
2129 
2130 static const struct platform_device_id ssif_plat_ids[] = {
2131     { "dmi-ipmi-ssif", 0 },
2132     { }
2133 };
2134 
2135 static struct platform_driver ipmi_driver = {
2136 	.driver = {
2137 		.name = DEVICE_NAME,
2138 	},
2139 	.probe		= ssif_platform_probe,
2140 	.remove		= ssif_platform_remove,
2141 	.id_table       = ssif_plat_ids
2142 };
2143 
init_ipmi_ssif(void)2144 static int init_ipmi_ssif(void)
2145 {
2146 	int i;
2147 	int rv;
2148 
2149 	if (initialized)
2150 		return 0;
2151 
2152 	pr_info("IPMI SSIF Interface driver\n");
2153 
2154 	/* build list for i2c from addr list */
2155 	for (i = 0; i < num_addrs; i++) {
2156 		rv = new_ssif_client(addr[i], adapter_name[i],
2157 				     dbg[i], slave_addrs[i],
2158 				     SI_HARDCODED, NULL);
2159 		if (rv)
2160 			pr_err("Couldn't add hardcoded device at addr 0x%x\n",
2161 			       addr[i]);
2162 	}
2163 
2164 	if (ssif_tryacpi)
2165 		ssif_i2c_driver.driver.acpi_match_table	=
2166 			ACPI_PTR(ssif_acpi_match);
2167 
2168 	if (ssif_trydmi) {
2169 		rv = platform_driver_register(&ipmi_driver);
2170 		if (rv)
2171 			pr_err("Unable to register driver: %d\n", rv);
2172 		else
2173 			platform_registered = true;
2174 	}
2175 
2176 	ssif_i2c_driver.address_list = ssif_address_list();
2177 
2178 	rv = i2c_add_driver(&ssif_i2c_driver);
2179 	if (!rv)
2180 		initialized = true;
2181 
2182 	return rv;
2183 }
2184 module_init(init_ipmi_ssif);
2185 
cleanup_ipmi_ssif(void)2186 static void cleanup_ipmi_ssif(void)
2187 {
2188 	if (!initialized)
2189 		return;
2190 
2191 	initialized = false;
2192 
2193 	i2c_del_driver(&ssif_i2c_driver);
2194 
2195 	kfree(ssif_i2c_driver.address_list);
2196 
2197 	if (ssif_trydmi && platform_registered)
2198 		platform_driver_unregister(&ipmi_driver);
2199 
2200 	free_ssif_clients();
2201 }
2202 module_exit(cleanup_ipmi_ssif);
2203 
2204 MODULE_ALIAS("platform:dmi-ipmi-ssif");
2205 MODULE_AUTHOR("Todd C Davis <todd.c.davis@intel.com>, Corey Minyard <minyard@acm.org>");
2206 MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus");
2207 MODULE_LICENSE("GPL");
2208