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