xref: /linux/drivers/char/ipmi/ssif_bmc.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1dd2bc5ccSQuan Nguyen // SPDX-License-Identifier: GPL-2.0-only
2dd2bc5ccSQuan Nguyen /*
3dd2bc5ccSQuan Nguyen  * The driver for BMC side of SSIF interface
4dd2bc5ccSQuan Nguyen  *
5dd2bc5ccSQuan Nguyen  * Copyright (c) 2022, Ampere Computing LLC
6dd2bc5ccSQuan Nguyen  *
7dd2bc5ccSQuan Nguyen  */
8dd2bc5ccSQuan Nguyen 
9dd2bc5ccSQuan Nguyen #include <linux/i2c.h>
10dd2bc5ccSQuan Nguyen #include <linux/miscdevice.h>
11dd2bc5ccSQuan Nguyen #include <linux/module.h>
12dd2bc5ccSQuan Nguyen #include <linux/of.h>
13dd2bc5ccSQuan Nguyen #include <linux/platform_device.h>
14dd2bc5ccSQuan Nguyen #include <linux/poll.h>
15dd2bc5ccSQuan Nguyen #include <linux/sched.h>
16dd2bc5ccSQuan Nguyen #include <linux/mutex.h>
17dd2bc5ccSQuan Nguyen #include <linux/spinlock.h>
18dd2bc5ccSQuan Nguyen #include <linux/timer.h>
19dd2bc5ccSQuan Nguyen #include <linux/jiffies.h>
20dd2bc5ccSQuan Nguyen #include <linux/ipmi_ssif_bmc.h>
21dd2bc5ccSQuan Nguyen 
22dd2bc5ccSQuan Nguyen #define DEVICE_NAME                             "ipmi-ssif-host"
23dd2bc5ccSQuan Nguyen 
24dd2bc5ccSQuan Nguyen #define GET_8BIT_ADDR(addr_7bit)                (((addr_7bit) << 1) & 0xff)
25dd2bc5ccSQuan Nguyen 
26dd2bc5ccSQuan Nguyen /* A standard SMBus Transaction is limited to 32 data bytes */
27dd2bc5ccSQuan Nguyen #define MAX_PAYLOAD_PER_TRANSACTION             32
28dd2bc5ccSQuan Nguyen /* Transaction includes the address, the command, the length and the PEC byte */
29dd2bc5ccSQuan Nguyen #define MAX_TRANSACTION                         (MAX_PAYLOAD_PER_TRANSACTION + 4)
30dd2bc5ccSQuan Nguyen 
31dd2bc5ccSQuan Nguyen #define MAX_IPMI_DATA_PER_START_TRANSACTION     30
32dd2bc5ccSQuan Nguyen #define MAX_IPMI_DATA_PER_MIDDLE_TRANSACTION    31
33dd2bc5ccSQuan Nguyen 
34dd2bc5ccSQuan Nguyen #define SSIF_IPMI_SINGLEPART_WRITE              0x2
35dd2bc5ccSQuan Nguyen #define SSIF_IPMI_SINGLEPART_READ               0x3
36dd2bc5ccSQuan Nguyen #define SSIF_IPMI_MULTIPART_WRITE_START         0x6
37dd2bc5ccSQuan Nguyen #define SSIF_IPMI_MULTIPART_WRITE_MIDDLE        0x7
38dd2bc5ccSQuan Nguyen #define SSIF_IPMI_MULTIPART_WRITE_END           0x8
39dd2bc5ccSQuan Nguyen #define SSIF_IPMI_MULTIPART_READ_START          0x3
40dd2bc5ccSQuan Nguyen #define SSIF_IPMI_MULTIPART_READ_MIDDLE         0x9
41dd2bc5ccSQuan Nguyen 
42dd2bc5ccSQuan Nguyen /*
43dd2bc5ccSQuan Nguyen  * IPMI 2.0 Spec, section 12.7 SSIF Timing,
44dd2bc5ccSQuan Nguyen  * Request-to-Response Time is T6max(250ms) - T1max(20ms) - 3ms = 227ms
45dd2bc5ccSQuan Nguyen  * Recover ssif_bmc from busy state if it takes up to 500ms
46dd2bc5ccSQuan Nguyen  */
47dd2bc5ccSQuan Nguyen #define RESPONSE_TIMEOUT                        500 /* ms */
48dd2bc5ccSQuan Nguyen 
49dd2bc5ccSQuan Nguyen struct ssif_part_buffer {
50dd2bc5ccSQuan Nguyen 	u8 address;
51dd2bc5ccSQuan Nguyen 	u8 smbus_cmd;
52dd2bc5ccSQuan Nguyen 	u8 length;
53dd2bc5ccSQuan Nguyen 	u8 payload[MAX_PAYLOAD_PER_TRANSACTION];
54dd2bc5ccSQuan Nguyen 	u8 pec;
55dd2bc5ccSQuan Nguyen 	u8 index;
56dd2bc5ccSQuan Nguyen };
57dd2bc5ccSQuan Nguyen 
58dd2bc5ccSQuan Nguyen /*
59dd2bc5ccSQuan Nguyen  * SSIF internal states:
60dd2bc5ccSQuan Nguyen  *   SSIF_READY         0x00 : Ready state
61dd2bc5ccSQuan Nguyen  *   SSIF_START         0x01 : Start smbus transaction
62dd2bc5ccSQuan Nguyen  *   SSIF_SMBUS_CMD     0x02 : Received SMBus command
63dd2bc5ccSQuan Nguyen  *   SSIF_REQ_RECVING   0x03 : Receiving request
64dd2bc5ccSQuan Nguyen  *   SSIF_RES_SENDING   0x04 : Sending response
65dd2bc5ccSQuan Nguyen  *   SSIF_ABORTING      0x05 : Aborting state
66dd2bc5ccSQuan Nguyen  */
67dd2bc5ccSQuan Nguyen enum ssif_state {
68dd2bc5ccSQuan Nguyen 	SSIF_READY,
69dd2bc5ccSQuan Nguyen 	SSIF_START,
70dd2bc5ccSQuan Nguyen 	SSIF_SMBUS_CMD,
71dd2bc5ccSQuan Nguyen 	SSIF_REQ_RECVING,
72dd2bc5ccSQuan Nguyen 	SSIF_RES_SENDING,
73dd2bc5ccSQuan Nguyen 	SSIF_ABORTING,
74dd2bc5ccSQuan Nguyen 	SSIF_STATE_MAX
75dd2bc5ccSQuan Nguyen };
76dd2bc5ccSQuan Nguyen 
77dd2bc5ccSQuan Nguyen struct ssif_bmc_ctx {
78dd2bc5ccSQuan Nguyen 	struct i2c_client       *client;
79dd2bc5ccSQuan Nguyen 	struct miscdevice       miscdev;
80dd2bc5ccSQuan Nguyen 	int                     msg_idx;
81dd2bc5ccSQuan Nguyen 	bool                    pec_support;
82dd2bc5ccSQuan Nguyen 	/* ssif bmc spinlock */
83dd2bc5ccSQuan Nguyen 	spinlock_t              lock;
84dd2bc5ccSQuan Nguyen 	wait_queue_head_t       wait_queue;
85dd2bc5ccSQuan Nguyen 	u8                      running;
86dd2bc5ccSQuan Nguyen 	enum ssif_state         state;
87dd2bc5ccSQuan Nguyen 	/* Timeout waiting for response */
88dd2bc5ccSQuan Nguyen 	struct timer_list       response_timer;
89dd2bc5ccSQuan Nguyen 	bool                    response_timer_inited;
90dd2bc5ccSQuan Nguyen 	/* Flag to identify a Multi-part Read Transaction */
91dd2bc5ccSQuan Nguyen 	bool                    is_singlepart_read;
92dd2bc5ccSQuan Nguyen 	u8                      nbytes_processed;
93dd2bc5ccSQuan Nguyen 	u8                      remain_len;
94dd2bc5ccSQuan Nguyen 	u8                      recv_len;
95dd2bc5ccSQuan Nguyen 	/* Block Number of a Multi-part Read Transaction */
96dd2bc5ccSQuan Nguyen 	u8                      block_num;
97dd2bc5ccSQuan Nguyen 	bool                    request_available;
98dd2bc5ccSQuan Nguyen 	bool                    response_in_progress;
99dd2bc5ccSQuan Nguyen 	bool                    busy;
100dd2bc5ccSQuan Nguyen 	bool                    aborting;
101dd2bc5ccSQuan Nguyen 	/* Buffer for SSIF Transaction part*/
102dd2bc5ccSQuan Nguyen 	struct ssif_part_buffer part_buf;
103dd2bc5ccSQuan Nguyen 	struct ipmi_ssif_msg    response;
104dd2bc5ccSQuan Nguyen 	struct ipmi_ssif_msg    request;
105dd2bc5ccSQuan Nguyen };
106dd2bc5ccSQuan Nguyen 
to_ssif_bmc(struct file * file)107dd2bc5ccSQuan Nguyen static inline struct ssif_bmc_ctx *to_ssif_bmc(struct file *file)
108dd2bc5ccSQuan Nguyen {
109dd2bc5ccSQuan Nguyen 	return container_of(file->private_data, struct ssif_bmc_ctx, miscdev);
110dd2bc5ccSQuan Nguyen }
111dd2bc5ccSQuan Nguyen 
state_to_string(enum ssif_state state)112dd2bc5ccSQuan Nguyen static const char *state_to_string(enum ssif_state state)
113dd2bc5ccSQuan Nguyen {
114dd2bc5ccSQuan Nguyen 	switch (state) {
115dd2bc5ccSQuan Nguyen 	case SSIF_READY:
116dd2bc5ccSQuan Nguyen 		return "SSIF_READY";
117dd2bc5ccSQuan Nguyen 	case SSIF_START:
118dd2bc5ccSQuan Nguyen 		return "SSIF_START";
119dd2bc5ccSQuan Nguyen 	case SSIF_SMBUS_CMD:
120dd2bc5ccSQuan Nguyen 		return "SSIF_SMBUS_CMD";
121dd2bc5ccSQuan Nguyen 	case SSIF_REQ_RECVING:
122dd2bc5ccSQuan Nguyen 		return "SSIF_REQ_RECVING";
123dd2bc5ccSQuan Nguyen 	case SSIF_RES_SENDING:
124dd2bc5ccSQuan Nguyen 		return "SSIF_RES_SENDING";
125dd2bc5ccSQuan Nguyen 	case SSIF_ABORTING:
126dd2bc5ccSQuan Nguyen 		return "SSIF_ABORTING";
127dd2bc5ccSQuan Nguyen 	default:
128dd2bc5ccSQuan Nguyen 		return "SSIF_STATE_UNKNOWN";
129dd2bc5ccSQuan Nguyen 	}
130dd2bc5ccSQuan Nguyen }
131dd2bc5ccSQuan Nguyen 
132dd2bc5ccSQuan Nguyen /* Handle SSIF message that will be sent to user */
ssif_bmc_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)133dd2bc5ccSQuan Nguyen static ssize_t ssif_bmc_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
134dd2bc5ccSQuan Nguyen {
135dd2bc5ccSQuan Nguyen 	struct ssif_bmc_ctx *ssif_bmc = to_ssif_bmc(file);
136dd2bc5ccSQuan Nguyen 	struct ipmi_ssif_msg msg;
137dd2bc5ccSQuan Nguyen 	unsigned long flags;
138dd2bc5ccSQuan Nguyen 	ssize_t ret;
139dd2bc5ccSQuan Nguyen 
140dd2bc5ccSQuan Nguyen 	spin_lock_irqsave(&ssif_bmc->lock, flags);
141dd2bc5ccSQuan Nguyen 	while (!ssif_bmc->request_available) {
142dd2bc5ccSQuan Nguyen 		spin_unlock_irqrestore(&ssif_bmc->lock, flags);
143dd2bc5ccSQuan Nguyen 		if (file->f_flags & O_NONBLOCK)
144dd2bc5ccSQuan Nguyen 			return -EAGAIN;
145dd2bc5ccSQuan Nguyen 		ret = wait_event_interruptible(ssif_bmc->wait_queue,
146dd2bc5ccSQuan Nguyen 					       ssif_bmc->request_available);
147dd2bc5ccSQuan Nguyen 		if (ret)
148dd2bc5ccSQuan Nguyen 			return ret;
149dd2bc5ccSQuan Nguyen 		spin_lock_irqsave(&ssif_bmc->lock, flags);
150dd2bc5ccSQuan Nguyen 	}
151dd2bc5ccSQuan Nguyen 
152dd2bc5ccSQuan Nguyen 	if (count < min_t(ssize_t,
153dd2bc5ccSQuan Nguyen 			  sizeof_field(struct ipmi_ssif_msg, len) + ssif_bmc->request.len,
154dd2bc5ccSQuan Nguyen 			  sizeof(struct ipmi_ssif_msg))) {
155dd2bc5ccSQuan Nguyen 		spin_unlock_irqrestore(&ssif_bmc->lock, flags);
156dd2bc5ccSQuan Nguyen 		ret = -EINVAL;
157dd2bc5ccSQuan Nguyen 	} else {
158dd2bc5ccSQuan Nguyen 		count = min_t(ssize_t,
159dd2bc5ccSQuan Nguyen 			      sizeof_field(struct ipmi_ssif_msg, len) + ssif_bmc->request.len,
160dd2bc5ccSQuan Nguyen 			      sizeof(struct ipmi_ssif_msg));
161dd2bc5ccSQuan Nguyen 		memcpy(&msg, &ssif_bmc->request, count);
162dd2bc5ccSQuan Nguyen 		ssif_bmc->request_available = false;
163dd2bc5ccSQuan Nguyen 		spin_unlock_irqrestore(&ssif_bmc->lock, flags);
164dd2bc5ccSQuan Nguyen 
165dd2bc5ccSQuan Nguyen 		ret = copy_to_user(buf, &msg, count);
166dd2bc5ccSQuan Nguyen 	}
167dd2bc5ccSQuan Nguyen 
168dd2bc5ccSQuan Nguyen 	return (ret < 0) ? ret : count;
169dd2bc5ccSQuan Nguyen }
170dd2bc5ccSQuan Nguyen 
171dd2bc5ccSQuan Nguyen /* Handle SSIF message that is written by user */
ssif_bmc_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)172dd2bc5ccSQuan Nguyen static ssize_t ssif_bmc_write(struct file *file, const char __user *buf, size_t count,
173dd2bc5ccSQuan Nguyen 			      loff_t *ppos)
174dd2bc5ccSQuan Nguyen {
175dd2bc5ccSQuan Nguyen 	struct ssif_bmc_ctx *ssif_bmc = to_ssif_bmc(file);
176dd2bc5ccSQuan Nguyen 	struct ipmi_ssif_msg msg;
177dd2bc5ccSQuan Nguyen 	unsigned long flags;
178dd2bc5ccSQuan Nguyen 	ssize_t ret;
179dd2bc5ccSQuan Nguyen 
1800627cef3SDan Carpenter 	if (count < sizeof(msg.len) ||
1810627cef3SDan Carpenter 	    count > sizeof(struct ipmi_ssif_msg))
182dd2bc5ccSQuan Nguyen 		return -EINVAL;
183dd2bc5ccSQuan Nguyen 
184dd2bc5ccSQuan Nguyen 	if (copy_from_user(&msg, buf, count))
185dd2bc5ccSQuan Nguyen 		return -EFAULT;
186dd2bc5ccSQuan Nguyen 
1870627cef3SDan Carpenter 	if (!msg.len || msg.len > IPMI_SSIF_PAYLOAD_MAX ||
1880627cef3SDan Carpenter 	    count < sizeof_field(struct ipmi_ssif_msg, len) + msg.len)
189dd2bc5ccSQuan Nguyen 		return -EINVAL;
190dd2bc5ccSQuan Nguyen 
191dd2bc5ccSQuan Nguyen 	spin_lock_irqsave(&ssif_bmc->lock, flags);
192dd2bc5ccSQuan Nguyen 	while (ssif_bmc->response_in_progress) {
193dd2bc5ccSQuan Nguyen 		spin_unlock_irqrestore(&ssif_bmc->lock, flags);
194dd2bc5ccSQuan Nguyen 		if (file->f_flags & O_NONBLOCK)
195dd2bc5ccSQuan Nguyen 			return -EAGAIN;
196dd2bc5ccSQuan Nguyen 		ret = wait_event_interruptible(ssif_bmc->wait_queue,
197dd2bc5ccSQuan Nguyen 					       !ssif_bmc->response_in_progress);
198dd2bc5ccSQuan Nguyen 		if (ret)
199dd2bc5ccSQuan Nguyen 			return ret;
200dd2bc5ccSQuan Nguyen 		spin_lock_irqsave(&ssif_bmc->lock, flags);
201dd2bc5ccSQuan Nguyen 	}
202dd2bc5ccSQuan Nguyen 
203dd2bc5ccSQuan Nguyen 	/*
204dd2bc5ccSQuan Nguyen 	 * The write must complete before the response timeout fired, otherwise
205dd2bc5ccSQuan Nguyen 	 * the response is aborted and wait for next request
206dd2bc5ccSQuan Nguyen 	 * Return -EINVAL if the response is aborted
207dd2bc5ccSQuan Nguyen 	 */
208dd2bc5ccSQuan Nguyen 	ret = (ssif_bmc->response_timer_inited) ? 0 : -EINVAL;
209dd2bc5ccSQuan Nguyen 	if (ret)
210dd2bc5ccSQuan Nguyen 		goto exit;
211dd2bc5ccSQuan Nguyen 
212dd2bc5ccSQuan Nguyen 	del_timer(&ssif_bmc->response_timer);
213dd2bc5ccSQuan Nguyen 	ssif_bmc->response_timer_inited = false;
214dd2bc5ccSQuan Nguyen 
215dd2bc5ccSQuan Nguyen 	memcpy(&ssif_bmc->response, &msg, count);
216dd2bc5ccSQuan Nguyen 	ssif_bmc->is_singlepart_read = (msg.len <= MAX_PAYLOAD_PER_TRANSACTION);
217dd2bc5ccSQuan Nguyen 
218dd2bc5ccSQuan Nguyen 	ssif_bmc->response_in_progress = true;
219dd2bc5ccSQuan Nguyen 
220dd2bc5ccSQuan Nguyen 	/* ssif_bmc not busy */
221dd2bc5ccSQuan Nguyen 	ssif_bmc->busy = false;
222dd2bc5ccSQuan Nguyen 
223dd2bc5ccSQuan Nguyen 	/* Clean old request buffer */
224dd2bc5ccSQuan Nguyen 	memset(&ssif_bmc->request, 0, sizeof(struct ipmi_ssif_msg));
225dd2bc5ccSQuan Nguyen exit:
226dd2bc5ccSQuan Nguyen 	spin_unlock_irqrestore(&ssif_bmc->lock, flags);
227dd2bc5ccSQuan Nguyen 
228dd2bc5ccSQuan Nguyen 	return (ret < 0) ? ret : count;
229dd2bc5ccSQuan Nguyen }
230dd2bc5ccSQuan Nguyen 
ssif_bmc_open(struct inode * inode,struct file * file)231dd2bc5ccSQuan Nguyen static int ssif_bmc_open(struct inode *inode, struct file *file)
232dd2bc5ccSQuan Nguyen {
233dd2bc5ccSQuan Nguyen 	struct ssif_bmc_ctx *ssif_bmc = to_ssif_bmc(file);
234dd2bc5ccSQuan Nguyen 	int ret = 0;
235dd2bc5ccSQuan Nguyen 
236dd2bc5ccSQuan Nguyen 	spin_lock_irq(&ssif_bmc->lock);
237dd2bc5ccSQuan Nguyen 	if (!ssif_bmc->running)
238dd2bc5ccSQuan Nguyen 		ssif_bmc->running = 1;
239dd2bc5ccSQuan Nguyen 	else
240dd2bc5ccSQuan Nguyen 		ret = -EBUSY;
241dd2bc5ccSQuan Nguyen 	spin_unlock_irq(&ssif_bmc->lock);
242dd2bc5ccSQuan Nguyen 
243dd2bc5ccSQuan Nguyen 	return ret;
244dd2bc5ccSQuan Nguyen }
245dd2bc5ccSQuan Nguyen 
ssif_bmc_poll(struct file * file,poll_table * wait)246dd2bc5ccSQuan Nguyen static __poll_t ssif_bmc_poll(struct file *file, poll_table *wait)
247dd2bc5ccSQuan Nguyen {
248dd2bc5ccSQuan Nguyen 	struct ssif_bmc_ctx *ssif_bmc = to_ssif_bmc(file);
249dd2bc5ccSQuan Nguyen 	__poll_t mask = 0;
250dd2bc5ccSQuan Nguyen 
251dd2bc5ccSQuan Nguyen 	poll_wait(file, &ssif_bmc->wait_queue, wait);
252dd2bc5ccSQuan Nguyen 
253dd2bc5ccSQuan Nguyen 	spin_lock_irq(&ssif_bmc->lock);
254dd2bc5ccSQuan Nguyen 	/* The request is available, userspace application can get the request */
255dd2bc5ccSQuan Nguyen 	if (ssif_bmc->request_available)
2566dbd4341SQuan Nguyen 		mask |= EPOLLIN;
257dd2bc5ccSQuan Nguyen 
258dd2bc5ccSQuan Nguyen 	spin_unlock_irq(&ssif_bmc->lock);
259dd2bc5ccSQuan Nguyen 
260dd2bc5ccSQuan Nguyen 	return mask;
261dd2bc5ccSQuan Nguyen }
262dd2bc5ccSQuan Nguyen 
ssif_bmc_release(struct inode * inode,struct file * file)263dd2bc5ccSQuan Nguyen static int ssif_bmc_release(struct inode *inode, struct file *file)
264dd2bc5ccSQuan Nguyen {
265dd2bc5ccSQuan Nguyen 	struct ssif_bmc_ctx *ssif_bmc = to_ssif_bmc(file);
266dd2bc5ccSQuan Nguyen 
267dd2bc5ccSQuan Nguyen 	spin_lock_irq(&ssif_bmc->lock);
268dd2bc5ccSQuan Nguyen 	ssif_bmc->running = 0;
269dd2bc5ccSQuan Nguyen 	spin_unlock_irq(&ssif_bmc->lock);
270dd2bc5ccSQuan Nguyen 
271dd2bc5ccSQuan Nguyen 	return 0;
272dd2bc5ccSQuan Nguyen }
273dd2bc5ccSQuan Nguyen 
274dd2bc5ccSQuan Nguyen /*
275dd2bc5ccSQuan Nguyen  * System calls to device interface for user apps
276dd2bc5ccSQuan Nguyen  */
277dd2bc5ccSQuan Nguyen static const struct file_operations ssif_bmc_fops = {
278dd2bc5ccSQuan Nguyen 	.owner		= THIS_MODULE,
279dd2bc5ccSQuan Nguyen 	.open		= ssif_bmc_open,
280dd2bc5ccSQuan Nguyen 	.read		= ssif_bmc_read,
281dd2bc5ccSQuan Nguyen 	.write		= ssif_bmc_write,
282dd2bc5ccSQuan Nguyen 	.release	= ssif_bmc_release,
283dd2bc5ccSQuan Nguyen 	.poll		= ssif_bmc_poll,
284dd2bc5ccSQuan Nguyen };
285dd2bc5ccSQuan Nguyen 
286dd2bc5ccSQuan Nguyen /* Called with ssif_bmc->lock held. */
complete_response(struct ssif_bmc_ctx * ssif_bmc)287dd2bc5ccSQuan Nguyen static void complete_response(struct ssif_bmc_ctx *ssif_bmc)
288dd2bc5ccSQuan Nguyen {
289dd2bc5ccSQuan Nguyen 	/* Invalidate response in buffer to denote it having been sent. */
290dd2bc5ccSQuan Nguyen 	ssif_bmc->response.len = 0;
291dd2bc5ccSQuan Nguyen 	ssif_bmc->response_in_progress = false;
292dd2bc5ccSQuan Nguyen 	ssif_bmc->nbytes_processed = 0;
293dd2bc5ccSQuan Nguyen 	ssif_bmc->remain_len = 0;
294dd2bc5ccSQuan Nguyen 	ssif_bmc->busy = false;
295dd2bc5ccSQuan Nguyen 	memset(&ssif_bmc->part_buf, 0, sizeof(struct ssif_part_buffer));
296dd2bc5ccSQuan Nguyen 	wake_up_all(&ssif_bmc->wait_queue);
297dd2bc5ccSQuan Nguyen }
298dd2bc5ccSQuan Nguyen 
response_timeout(struct timer_list * t)299dd2bc5ccSQuan Nguyen static void response_timeout(struct timer_list *t)
300dd2bc5ccSQuan Nguyen {
301dd2bc5ccSQuan Nguyen 	struct ssif_bmc_ctx *ssif_bmc = from_timer(ssif_bmc, t, response_timer);
302dd2bc5ccSQuan Nguyen 	unsigned long flags;
303dd2bc5ccSQuan Nguyen 
304dd2bc5ccSQuan Nguyen 	spin_lock_irqsave(&ssif_bmc->lock, flags);
305dd2bc5ccSQuan Nguyen 
306dd2bc5ccSQuan Nguyen 	/* Do nothing if the response is in progress */
307dd2bc5ccSQuan Nguyen 	if (!ssif_bmc->response_in_progress) {
308dd2bc5ccSQuan Nguyen 		/* Recover ssif_bmc from busy */
309dd2bc5ccSQuan Nguyen 		ssif_bmc->busy = false;
310dd2bc5ccSQuan Nguyen 		ssif_bmc->response_timer_inited = false;
311dd2bc5ccSQuan Nguyen 		/* Set aborting flag */
312dd2bc5ccSQuan Nguyen 		ssif_bmc->aborting = true;
313dd2bc5ccSQuan Nguyen 	}
314dd2bc5ccSQuan Nguyen 
315dd2bc5ccSQuan Nguyen 	spin_unlock_irqrestore(&ssif_bmc->lock, flags);
316dd2bc5ccSQuan Nguyen }
317dd2bc5ccSQuan Nguyen 
318dd2bc5ccSQuan Nguyen /* Called with ssif_bmc->lock held. */
handle_request(struct ssif_bmc_ctx * ssif_bmc)319dd2bc5ccSQuan Nguyen static void handle_request(struct ssif_bmc_ctx *ssif_bmc)
320dd2bc5ccSQuan Nguyen {
321dd2bc5ccSQuan Nguyen 	/* set ssif_bmc to busy waiting for response */
322dd2bc5ccSQuan Nguyen 	ssif_bmc->busy = true;
323dd2bc5ccSQuan Nguyen 	/* Request message is available to process */
324dd2bc5ccSQuan Nguyen 	ssif_bmc->request_available = true;
325dd2bc5ccSQuan Nguyen 	/* Clean old response buffer */
326dd2bc5ccSQuan Nguyen 	memset(&ssif_bmc->response, 0, sizeof(struct ipmi_ssif_msg));
327dd2bc5ccSQuan Nguyen 	/* This is the new READ request.*/
328dd2bc5ccSQuan Nguyen 	wake_up_all(&ssif_bmc->wait_queue);
329dd2bc5ccSQuan Nguyen 
330dd2bc5ccSQuan Nguyen 	/* Armed timer to recover slave from busy state in case of no response */
331dd2bc5ccSQuan Nguyen 	if (!ssif_bmc->response_timer_inited) {
332dd2bc5ccSQuan Nguyen 		timer_setup(&ssif_bmc->response_timer, response_timeout, 0);
333dd2bc5ccSQuan Nguyen 		ssif_bmc->response_timer_inited = true;
334dd2bc5ccSQuan Nguyen 	}
335dd2bc5ccSQuan Nguyen 	mod_timer(&ssif_bmc->response_timer, jiffies + msecs_to_jiffies(RESPONSE_TIMEOUT));
336dd2bc5ccSQuan Nguyen }
337dd2bc5ccSQuan Nguyen 
calculate_response_part_pec(struct ssif_part_buffer * part)338dd2bc5ccSQuan Nguyen static void calculate_response_part_pec(struct ssif_part_buffer *part)
339dd2bc5ccSQuan Nguyen {
340dd2bc5ccSQuan Nguyen 	u8 addr = part->address;
341dd2bc5ccSQuan Nguyen 
342dd2bc5ccSQuan Nguyen 	/* PEC - Start Read Address */
343dd2bc5ccSQuan Nguyen 	part->pec = i2c_smbus_pec(0, &addr, 1);
344dd2bc5ccSQuan Nguyen 	/* PEC - SSIF Command */
345dd2bc5ccSQuan Nguyen 	part->pec = i2c_smbus_pec(part->pec, &part->smbus_cmd, 1);
346dd2bc5ccSQuan Nguyen 	/* PEC - Restart Write Address */
347dd2bc5ccSQuan Nguyen 	addr = addr | 0x01;
348dd2bc5ccSQuan Nguyen 	part->pec = i2c_smbus_pec(part->pec, &addr, 1);
349dd2bc5ccSQuan Nguyen 	part->pec = i2c_smbus_pec(part->pec, &part->length, 1);
350dd2bc5ccSQuan Nguyen 	if (part->length)
351dd2bc5ccSQuan Nguyen 		part->pec = i2c_smbus_pec(part->pec, part->payload, part->length);
352dd2bc5ccSQuan Nguyen }
353dd2bc5ccSQuan Nguyen 
set_singlepart_response_buffer(struct ssif_bmc_ctx * ssif_bmc)354dd2bc5ccSQuan Nguyen static void set_singlepart_response_buffer(struct ssif_bmc_ctx *ssif_bmc)
355dd2bc5ccSQuan Nguyen {
356dd2bc5ccSQuan Nguyen 	struct ssif_part_buffer *part = &ssif_bmc->part_buf;
357dd2bc5ccSQuan Nguyen 
358dd2bc5ccSQuan Nguyen 	part->address = GET_8BIT_ADDR(ssif_bmc->client->addr);
359dd2bc5ccSQuan Nguyen 	part->length = (u8)ssif_bmc->response.len;
360dd2bc5ccSQuan Nguyen 
361dd2bc5ccSQuan Nguyen 	/* Clear the rest to 0 */
362dd2bc5ccSQuan Nguyen 	memset(part->payload + part->length, 0, MAX_PAYLOAD_PER_TRANSACTION - part->length);
363dd2bc5ccSQuan Nguyen 	memcpy(&part->payload[0], &ssif_bmc->response.payload[0], part->length);
364dd2bc5ccSQuan Nguyen }
365dd2bc5ccSQuan Nguyen 
set_multipart_response_buffer(struct ssif_bmc_ctx * ssif_bmc)366dd2bc5ccSQuan Nguyen static void set_multipart_response_buffer(struct ssif_bmc_ctx *ssif_bmc)
367dd2bc5ccSQuan Nguyen {
368dd2bc5ccSQuan Nguyen 	struct ssif_part_buffer *part = &ssif_bmc->part_buf;
369dd2bc5ccSQuan Nguyen 	u8 part_len = 0;
370dd2bc5ccSQuan Nguyen 
371dd2bc5ccSQuan Nguyen 	part->address = GET_8BIT_ADDR(ssif_bmc->client->addr);
372dd2bc5ccSQuan Nguyen 	switch (part->smbus_cmd) {
373dd2bc5ccSQuan Nguyen 	case SSIF_IPMI_MULTIPART_READ_START:
374dd2bc5ccSQuan Nguyen 		/*
375dd2bc5ccSQuan Nguyen 		 * Read Start length is 32 bytes.
376dd2bc5ccSQuan Nguyen 		 * Read Start transfer first 30 bytes of IPMI response
377dd2bc5ccSQuan Nguyen 		 * and 2 special code 0x00, 0x01.
378dd2bc5ccSQuan Nguyen 		 */
379dd2bc5ccSQuan Nguyen 		ssif_bmc->nbytes_processed = 0;
380dd2bc5ccSQuan Nguyen 		ssif_bmc->block_num = 0;
381dd2bc5ccSQuan Nguyen 		part->length = MAX_PAYLOAD_PER_TRANSACTION;
382dd2bc5ccSQuan Nguyen 		part_len = MAX_IPMI_DATA_PER_START_TRANSACTION;
383dd2bc5ccSQuan Nguyen 		ssif_bmc->remain_len = ssif_bmc->response.len - part_len;
384dd2bc5ccSQuan Nguyen 
385dd2bc5ccSQuan Nguyen 		part->payload[0] = 0x00; /* Start Flag */
386dd2bc5ccSQuan Nguyen 		part->payload[1] = 0x01; /* Start Flag */
387dd2bc5ccSQuan Nguyen 
388dd2bc5ccSQuan Nguyen 		memcpy(&part->payload[2], &ssif_bmc->response.payload[0], part_len);
389dd2bc5ccSQuan Nguyen 		break;
390dd2bc5ccSQuan Nguyen 
391dd2bc5ccSQuan Nguyen 	case SSIF_IPMI_MULTIPART_READ_MIDDLE:
392dd2bc5ccSQuan Nguyen 		/*
393dd2bc5ccSQuan Nguyen 		 * IPMI READ Middle or READ End messages can carry up to 31 bytes
394dd2bc5ccSQuan Nguyen 		 * IPMI data plus block number byte.
395dd2bc5ccSQuan Nguyen 		 */
396dd2bc5ccSQuan Nguyen 		if (ssif_bmc->remain_len <= MAX_IPMI_DATA_PER_MIDDLE_TRANSACTION) {
397dd2bc5ccSQuan Nguyen 			/*
398dd2bc5ccSQuan Nguyen 			 * This is READ End message
399dd2bc5ccSQuan Nguyen 			 *  Return length is the remaining response data length
400dd2bc5ccSQuan Nguyen 			 *  plus block number
401dd2bc5ccSQuan Nguyen 			 *  Block number 0xFF is to indicate this is last message
402dd2bc5ccSQuan Nguyen 			 *
403dd2bc5ccSQuan Nguyen 			 */
404dd2bc5ccSQuan Nguyen 			/* Clean the buffer */
405dd2bc5ccSQuan Nguyen 			memset(&part->payload[0], 0, MAX_PAYLOAD_PER_TRANSACTION);
406dd2bc5ccSQuan Nguyen 			part->length = ssif_bmc->remain_len + 1;
407dd2bc5ccSQuan Nguyen 			part_len = ssif_bmc->remain_len;
408dd2bc5ccSQuan Nguyen 			ssif_bmc->block_num = 0xFF;
409dd2bc5ccSQuan Nguyen 			part->payload[0] = ssif_bmc->block_num;
410dd2bc5ccSQuan Nguyen 		} else {
411dd2bc5ccSQuan Nguyen 			/*
412dd2bc5ccSQuan Nguyen 			 * This is READ Middle message
413dd2bc5ccSQuan Nguyen 			 *  Response length is the maximum SMBUS transfer length
414dd2bc5ccSQuan Nguyen 			 *  Block number byte is incremented
415dd2bc5ccSQuan Nguyen 			 * Return length is maximum SMBUS transfer length
416dd2bc5ccSQuan Nguyen 			 */
417dd2bc5ccSQuan Nguyen 			part->length = MAX_PAYLOAD_PER_TRANSACTION;
418dd2bc5ccSQuan Nguyen 			part_len = MAX_IPMI_DATA_PER_MIDDLE_TRANSACTION;
419dd2bc5ccSQuan Nguyen 			part->payload[0] = ssif_bmc->block_num;
420dd2bc5ccSQuan Nguyen 			ssif_bmc->block_num++;
421dd2bc5ccSQuan Nguyen 		}
422dd2bc5ccSQuan Nguyen 
423dd2bc5ccSQuan Nguyen 		ssif_bmc->remain_len -= part_len;
424dd2bc5ccSQuan Nguyen 		memcpy(&part->payload[1], ssif_bmc->response.payload + ssif_bmc->nbytes_processed,
425dd2bc5ccSQuan Nguyen 		       part_len);
426dd2bc5ccSQuan Nguyen 		break;
427dd2bc5ccSQuan Nguyen 
428dd2bc5ccSQuan Nguyen 	default:
429dd2bc5ccSQuan Nguyen 		/* Do not expect to go to this case */
430dd2bc5ccSQuan Nguyen 		dev_err(&ssif_bmc->client->dev, "%s: Unexpected SMBus command 0x%x\n",
431dd2bc5ccSQuan Nguyen 			__func__, part->smbus_cmd);
432dd2bc5ccSQuan Nguyen 		break;
433dd2bc5ccSQuan Nguyen 	}
434dd2bc5ccSQuan Nguyen 
435dd2bc5ccSQuan Nguyen 	ssif_bmc->nbytes_processed += part_len;
436dd2bc5ccSQuan Nguyen }
437dd2bc5ccSQuan Nguyen 
supported_read_cmd(u8 cmd)438dd2bc5ccSQuan Nguyen static bool supported_read_cmd(u8 cmd)
439dd2bc5ccSQuan Nguyen {
440dd2bc5ccSQuan Nguyen 	if (cmd == SSIF_IPMI_SINGLEPART_READ ||
441dd2bc5ccSQuan Nguyen 	    cmd == SSIF_IPMI_MULTIPART_READ_START ||
442dd2bc5ccSQuan Nguyen 	    cmd == SSIF_IPMI_MULTIPART_READ_MIDDLE)
443dd2bc5ccSQuan Nguyen 		return true;
444dd2bc5ccSQuan Nguyen 
445dd2bc5ccSQuan Nguyen 	return false;
446dd2bc5ccSQuan Nguyen }
447dd2bc5ccSQuan Nguyen 
supported_write_cmd(u8 cmd)448dd2bc5ccSQuan Nguyen static bool supported_write_cmd(u8 cmd)
449dd2bc5ccSQuan Nguyen {
450dd2bc5ccSQuan Nguyen 	if (cmd == SSIF_IPMI_SINGLEPART_WRITE ||
451dd2bc5ccSQuan Nguyen 	    cmd == SSIF_IPMI_MULTIPART_WRITE_START ||
452dd2bc5ccSQuan Nguyen 	    cmd == SSIF_IPMI_MULTIPART_WRITE_MIDDLE ||
453dd2bc5ccSQuan Nguyen 	    cmd == SSIF_IPMI_MULTIPART_WRITE_END)
454dd2bc5ccSQuan Nguyen 		return true;
455dd2bc5ccSQuan Nguyen 
456dd2bc5ccSQuan Nguyen 	return false;
457dd2bc5ccSQuan Nguyen }
458dd2bc5ccSQuan Nguyen 
459dd2bc5ccSQuan Nguyen /* Process the IPMI response that will be read by master */
handle_read_processed(struct ssif_bmc_ctx * ssif_bmc,u8 * val)460dd2bc5ccSQuan Nguyen static void handle_read_processed(struct ssif_bmc_ctx *ssif_bmc, u8 *val)
461dd2bc5ccSQuan Nguyen {
462dd2bc5ccSQuan Nguyen 	struct ssif_part_buffer *part = &ssif_bmc->part_buf;
463dd2bc5ccSQuan Nguyen 
464dd2bc5ccSQuan Nguyen 	/* msg_idx start from 0 */
465dd2bc5ccSQuan Nguyen 	if (part->index < part->length)
466dd2bc5ccSQuan Nguyen 		*val = part->payload[part->index];
467dd2bc5ccSQuan Nguyen 	else if (part->index == part->length && ssif_bmc->pec_support)
468dd2bc5ccSQuan Nguyen 		*val = part->pec;
469dd2bc5ccSQuan Nguyen 	else
470dd2bc5ccSQuan Nguyen 		*val = 0;
471dd2bc5ccSQuan Nguyen 
472dd2bc5ccSQuan Nguyen 	part->index++;
473dd2bc5ccSQuan Nguyen }
474dd2bc5ccSQuan Nguyen 
handle_write_received(struct ssif_bmc_ctx * ssif_bmc,u8 * val)475dd2bc5ccSQuan Nguyen static void handle_write_received(struct ssif_bmc_ctx *ssif_bmc, u8 *val)
476dd2bc5ccSQuan Nguyen {
477dd2bc5ccSQuan Nguyen 	/*
478dd2bc5ccSQuan Nguyen 	 * The msg_idx must be 1 when first enter SSIF_REQ_RECVING state
479dd2bc5ccSQuan Nguyen 	 * And it would never exceeded 36 bytes included the 32 bytes max payload +
480dd2bc5ccSQuan Nguyen 	 * the address + the command + the len and the PEC.
481dd2bc5ccSQuan Nguyen 	 */
482dd2bc5ccSQuan Nguyen 	if (ssif_bmc->msg_idx < 1  || ssif_bmc->msg_idx > MAX_TRANSACTION)
483dd2bc5ccSQuan Nguyen 		return;
484dd2bc5ccSQuan Nguyen 
485dd2bc5ccSQuan Nguyen 	if (ssif_bmc->msg_idx == 1) {
486dd2bc5ccSQuan Nguyen 		ssif_bmc->part_buf.length = *val;
487dd2bc5ccSQuan Nguyen 		ssif_bmc->part_buf.index = 0;
488dd2bc5ccSQuan Nguyen 	} else {
489dd2bc5ccSQuan Nguyen 		ssif_bmc->part_buf.payload[ssif_bmc->part_buf.index++] = *val;
490dd2bc5ccSQuan Nguyen 	}
491dd2bc5ccSQuan Nguyen 
492dd2bc5ccSQuan Nguyen 	ssif_bmc->msg_idx++;
493dd2bc5ccSQuan Nguyen }
494dd2bc5ccSQuan Nguyen 
validate_request_part(struct ssif_bmc_ctx * ssif_bmc)495dd2bc5ccSQuan Nguyen static bool validate_request_part(struct ssif_bmc_ctx *ssif_bmc)
496dd2bc5ccSQuan Nguyen {
497dd2bc5ccSQuan Nguyen 	struct ssif_part_buffer *part = &ssif_bmc->part_buf;
498dd2bc5ccSQuan Nguyen 	bool ret = true;
499dd2bc5ccSQuan Nguyen 	u8 cpec;
500dd2bc5ccSQuan Nguyen 	u8 addr;
501dd2bc5ccSQuan Nguyen 
502dd2bc5ccSQuan Nguyen 	if (part->index == part->length) {
503dd2bc5ccSQuan Nguyen 		/* PEC is not included */
504dd2bc5ccSQuan Nguyen 		ssif_bmc->pec_support = false;
505dd2bc5ccSQuan Nguyen 		ret = true;
506dd2bc5ccSQuan Nguyen 		goto exit;
507dd2bc5ccSQuan Nguyen 	}
508dd2bc5ccSQuan Nguyen 
509dd2bc5ccSQuan Nguyen 	if (part->index != part->length + 1) {
510dd2bc5ccSQuan Nguyen 		ret = false;
511dd2bc5ccSQuan Nguyen 		goto exit;
512dd2bc5ccSQuan Nguyen 	}
513dd2bc5ccSQuan Nguyen 
514dd2bc5ccSQuan Nguyen 	/* PEC is included */
515dd2bc5ccSQuan Nguyen 	ssif_bmc->pec_support = true;
516dd2bc5ccSQuan Nguyen 	part->pec = part->payload[part->length];
517dd2bc5ccSQuan Nguyen 	addr = GET_8BIT_ADDR(ssif_bmc->client->addr);
518dd2bc5ccSQuan Nguyen 	cpec = i2c_smbus_pec(0, &addr, 1);
519dd2bc5ccSQuan Nguyen 	cpec = i2c_smbus_pec(cpec, &part->smbus_cmd, 1);
520dd2bc5ccSQuan Nguyen 	cpec = i2c_smbus_pec(cpec, &part->length, 1);
521dd2bc5ccSQuan Nguyen 	/*
522dd2bc5ccSQuan Nguyen 	 * As SMBus specification does not allow the length
523dd2bc5ccSQuan Nguyen 	 * (byte count) in the Write-Block protocol to be zero.
524dd2bc5ccSQuan Nguyen 	 * Therefore, it is illegal to have the last Middle
525dd2bc5ccSQuan Nguyen 	 * transaction in the sequence carry 32-byte and have
526dd2bc5ccSQuan Nguyen 	 * a length of ‘0’ in the End transaction.
527dd2bc5ccSQuan Nguyen 	 * But some users may try to use this way and we should
528dd2bc5ccSQuan Nguyen 	 * prevent ssif_bmc driver broken in this case.
529dd2bc5ccSQuan Nguyen 	 */
530dd2bc5ccSQuan Nguyen 	if (part->length)
531dd2bc5ccSQuan Nguyen 		cpec = i2c_smbus_pec(cpec, part->payload, part->length);
532dd2bc5ccSQuan Nguyen 
533dd2bc5ccSQuan Nguyen 	if (cpec != part->pec)
534dd2bc5ccSQuan Nguyen 		ret = false;
535dd2bc5ccSQuan Nguyen 
536dd2bc5ccSQuan Nguyen exit:
537dd2bc5ccSQuan Nguyen 	return ret;
538dd2bc5ccSQuan Nguyen }
539dd2bc5ccSQuan Nguyen 
process_request_part(struct ssif_bmc_ctx * ssif_bmc)540dd2bc5ccSQuan Nguyen static void process_request_part(struct ssif_bmc_ctx *ssif_bmc)
541dd2bc5ccSQuan Nguyen {
542dd2bc5ccSQuan Nguyen 	struct ssif_part_buffer *part = &ssif_bmc->part_buf;
543dd2bc5ccSQuan Nguyen 	unsigned int len;
544dd2bc5ccSQuan Nguyen 
545dd2bc5ccSQuan Nguyen 	switch (part->smbus_cmd) {
546dd2bc5ccSQuan Nguyen 	case SSIF_IPMI_SINGLEPART_WRITE:
547dd2bc5ccSQuan Nguyen 		/* save the whole part to request*/
548dd2bc5ccSQuan Nguyen 		ssif_bmc->request.len = part->length;
549dd2bc5ccSQuan Nguyen 		memcpy(ssif_bmc->request.payload, part->payload, part->length);
550dd2bc5ccSQuan Nguyen 
551dd2bc5ccSQuan Nguyen 		break;
552dd2bc5ccSQuan Nguyen 	case SSIF_IPMI_MULTIPART_WRITE_START:
553dd2bc5ccSQuan Nguyen 		ssif_bmc->request.len = 0;
554dd2bc5ccSQuan Nguyen 
555dd2bc5ccSQuan Nguyen 		fallthrough;
556dd2bc5ccSQuan Nguyen 	case SSIF_IPMI_MULTIPART_WRITE_MIDDLE:
557dd2bc5ccSQuan Nguyen 	case SSIF_IPMI_MULTIPART_WRITE_END:
558dd2bc5ccSQuan Nguyen 		len = ssif_bmc->request.len + part->length;
559dd2bc5ccSQuan Nguyen 		/* Do the bound check here, not allow the request len exceed 254 bytes */
560dd2bc5ccSQuan Nguyen 		if (len > IPMI_SSIF_PAYLOAD_MAX) {
561dd2bc5ccSQuan Nguyen 			dev_warn(&ssif_bmc->client->dev,
562dd2bc5ccSQuan Nguyen 				 "Warn: Request exceeded 254 bytes, aborting");
563dd2bc5ccSQuan Nguyen 			/* Request too long, aborting */
564dd2bc5ccSQuan Nguyen 			ssif_bmc->aborting =  true;
565dd2bc5ccSQuan Nguyen 		} else {
566dd2bc5ccSQuan Nguyen 			memcpy(ssif_bmc->request.payload + ssif_bmc->request.len,
567dd2bc5ccSQuan Nguyen 			       part->payload, part->length);
568dd2bc5ccSQuan Nguyen 			ssif_bmc->request.len += part->length;
569dd2bc5ccSQuan Nguyen 		}
570dd2bc5ccSQuan Nguyen 		break;
571dd2bc5ccSQuan Nguyen 	default:
572dd2bc5ccSQuan Nguyen 		/* Do not expect to go to this case */
573dd2bc5ccSQuan Nguyen 		dev_err(&ssif_bmc->client->dev, "%s: Unexpected SMBus command 0x%x\n",
574dd2bc5ccSQuan Nguyen 			__func__, part->smbus_cmd);
575dd2bc5ccSQuan Nguyen 		break;
576dd2bc5ccSQuan Nguyen 	}
577dd2bc5ccSQuan Nguyen }
578dd2bc5ccSQuan Nguyen 
process_smbus_cmd(struct ssif_bmc_ctx * ssif_bmc,u8 * val)579dd2bc5ccSQuan Nguyen static void process_smbus_cmd(struct ssif_bmc_ctx *ssif_bmc, u8 *val)
580dd2bc5ccSQuan Nguyen {
581dd2bc5ccSQuan Nguyen 	/* SMBUS command can vary (single or multi-part) */
582dd2bc5ccSQuan Nguyen 	ssif_bmc->part_buf.smbus_cmd = *val;
583dd2bc5ccSQuan Nguyen 	ssif_bmc->msg_idx = 1;
584dd2bc5ccSQuan Nguyen 	memset(&ssif_bmc->part_buf.payload[0], 0, MAX_PAYLOAD_PER_TRANSACTION);
585dd2bc5ccSQuan Nguyen 
586dd2bc5ccSQuan Nguyen 	if (*val == SSIF_IPMI_SINGLEPART_WRITE || *val == SSIF_IPMI_MULTIPART_WRITE_START) {
587dd2bc5ccSQuan Nguyen 		/*
588dd2bc5ccSQuan Nguyen 		 * The response maybe not come in-time, causing host SSIF driver
589dd2bc5ccSQuan Nguyen 		 * to timeout and resend a new request. In such case check for
590dd2bc5ccSQuan Nguyen 		 * pending response and clear it
591dd2bc5ccSQuan Nguyen 		 */
592dd2bc5ccSQuan Nguyen 		if (ssif_bmc->response_in_progress)
593dd2bc5ccSQuan Nguyen 			complete_response(ssif_bmc);
594dd2bc5ccSQuan Nguyen 
595dd2bc5ccSQuan Nguyen 		/* This is new request, flip aborting flag if set */
596dd2bc5ccSQuan Nguyen 		if (ssif_bmc->aborting)
597dd2bc5ccSQuan Nguyen 			ssif_bmc->aborting = false;
598dd2bc5ccSQuan Nguyen 	}
599dd2bc5ccSQuan Nguyen }
600dd2bc5ccSQuan Nguyen 
on_read_requested_event(struct ssif_bmc_ctx * ssif_bmc,u8 * val)601dd2bc5ccSQuan Nguyen static void on_read_requested_event(struct ssif_bmc_ctx *ssif_bmc, u8 *val)
602dd2bc5ccSQuan Nguyen {
603dd2bc5ccSQuan Nguyen 	if (ssif_bmc->state == SSIF_READY ||
604dd2bc5ccSQuan Nguyen 	    ssif_bmc->state == SSIF_START ||
605dd2bc5ccSQuan Nguyen 	    ssif_bmc->state == SSIF_REQ_RECVING ||
606dd2bc5ccSQuan Nguyen 	    ssif_bmc->state == SSIF_RES_SENDING) {
607dd2bc5ccSQuan Nguyen 		dev_warn(&ssif_bmc->client->dev,
608dd2bc5ccSQuan Nguyen 			 "Warn: %s unexpected READ REQUESTED in state=%s\n",
609dd2bc5ccSQuan Nguyen 			 __func__, state_to_string(ssif_bmc->state));
610dd2bc5ccSQuan Nguyen 		ssif_bmc->state = SSIF_ABORTING;
611dd2bc5ccSQuan Nguyen 		*val = 0;
612dd2bc5ccSQuan Nguyen 		return;
613dd2bc5ccSQuan Nguyen 
614dd2bc5ccSQuan Nguyen 	} else if (ssif_bmc->state == SSIF_SMBUS_CMD) {
615dd2bc5ccSQuan Nguyen 		if (!supported_read_cmd(ssif_bmc->part_buf.smbus_cmd)) {
616dd2bc5ccSQuan Nguyen 			dev_warn(&ssif_bmc->client->dev, "Warn: Unknown SMBus read command=0x%x",
617dd2bc5ccSQuan Nguyen 				 ssif_bmc->part_buf.smbus_cmd);
618dd2bc5ccSQuan Nguyen 			ssif_bmc->aborting = true;
619dd2bc5ccSQuan Nguyen 		}
620dd2bc5ccSQuan Nguyen 
621dd2bc5ccSQuan Nguyen 		if (ssif_bmc->aborting)
622dd2bc5ccSQuan Nguyen 			ssif_bmc->state = SSIF_ABORTING;
623dd2bc5ccSQuan Nguyen 		else
624dd2bc5ccSQuan Nguyen 			ssif_bmc->state = SSIF_RES_SENDING;
625dd2bc5ccSQuan Nguyen 	}
626dd2bc5ccSQuan Nguyen 
627dd2bc5ccSQuan Nguyen 	ssif_bmc->msg_idx = 0;
628dd2bc5ccSQuan Nguyen 
629dd2bc5ccSQuan Nguyen 	/* Send 0 if there is nothing to send */
630dd2bc5ccSQuan Nguyen 	if (!ssif_bmc->response_in_progress || ssif_bmc->state == SSIF_ABORTING) {
631dd2bc5ccSQuan Nguyen 		*val = 0;
632dd2bc5ccSQuan Nguyen 		return;
633dd2bc5ccSQuan Nguyen 	}
634dd2bc5ccSQuan Nguyen 
635dd2bc5ccSQuan Nguyen 	if (ssif_bmc->is_singlepart_read)
636dd2bc5ccSQuan Nguyen 		set_singlepart_response_buffer(ssif_bmc);
637dd2bc5ccSQuan Nguyen 	else
638dd2bc5ccSQuan Nguyen 		set_multipart_response_buffer(ssif_bmc);
639dd2bc5ccSQuan Nguyen 
640dd2bc5ccSQuan Nguyen 	calculate_response_part_pec(&ssif_bmc->part_buf);
641dd2bc5ccSQuan Nguyen 	ssif_bmc->part_buf.index = 0;
642dd2bc5ccSQuan Nguyen 	*val = ssif_bmc->part_buf.length;
643dd2bc5ccSQuan Nguyen }
644dd2bc5ccSQuan Nguyen 
on_read_processed_event(struct ssif_bmc_ctx * ssif_bmc,u8 * val)645dd2bc5ccSQuan Nguyen static void on_read_processed_event(struct ssif_bmc_ctx *ssif_bmc, u8 *val)
646dd2bc5ccSQuan Nguyen {
647dd2bc5ccSQuan Nguyen 	if (ssif_bmc->state == SSIF_READY ||
648dd2bc5ccSQuan Nguyen 	    ssif_bmc->state == SSIF_START ||
649dd2bc5ccSQuan Nguyen 	    ssif_bmc->state == SSIF_REQ_RECVING ||
650dd2bc5ccSQuan Nguyen 	    ssif_bmc->state == SSIF_SMBUS_CMD) {
651dd2bc5ccSQuan Nguyen 		dev_warn(&ssif_bmc->client->dev,
652dd2bc5ccSQuan Nguyen 			 "Warn: %s unexpected READ PROCESSED in state=%s\n",
653dd2bc5ccSQuan Nguyen 			 __func__, state_to_string(ssif_bmc->state));
654dd2bc5ccSQuan Nguyen 		ssif_bmc->state = SSIF_ABORTING;
655dd2bc5ccSQuan Nguyen 		*val = 0;
656dd2bc5ccSQuan Nguyen 		return;
657dd2bc5ccSQuan Nguyen 	}
658dd2bc5ccSQuan Nguyen 
659dd2bc5ccSQuan Nguyen 	/* Send 0 if there is nothing to send */
660dd2bc5ccSQuan Nguyen 	if (!ssif_bmc->response_in_progress || ssif_bmc->state == SSIF_ABORTING) {
661dd2bc5ccSQuan Nguyen 		*val = 0;
662dd2bc5ccSQuan Nguyen 		return;
663dd2bc5ccSQuan Nguyen 	}
664dd2bc5ccSQuan Nguyen 
665dd2bc5ccSQuan Nguyen 	handle_read_processed(ssif_bmc, val);
666dd2bc5ccSQuan Nguyen }
667dd2bc5ccSQuan Nguyen 
on_write_requested_event(struct ssif_bmc_ctx * ssif_bmc,u8 * val)668dd2bc5ccSQuan Nguyen static void on_write_requested_event(struct ssif_bmc_ctx *ssif_bmc, u8 *val)
669dd2bc5ccSQuan Nguyen {
670dd2bc5ccSQuan Nguyen 	if (ssif_bmc->state == SSIF_READY || ssif_bmc->state == SSIF_SMBUS_CMD) {
671dd2bc5ccSQuan Nguyen 		ssif_bmc->state = SSIF_START;
672dd2bc5ccSQuan Nguyen 
673dd2bc5ccSQuan Nguyen 	} else if (ssif_bmc->state == SSIF_START ||
674dd2bc5ccSQuan Nguyen 		   ssif_bmc->state == SSIF_REQ_RECVING ||
675dd2bc5ccSQuan Nguyen 		   ssif_bmc->state == SSIF_RES_SENDING) {
676dd2bc5ccSQuan Nguyen 		dev_warn(&ssif_bmc->client->dev,
677dd2bc5ccSQuan Nguyen 			 "Warn: %s unexpected WRITE REQUEST in state=%s\n",
678dd2bc5ccSQuan Nguyen 			 __func__, state_to_string(ssif_bmc->state));
679dd2bc5ccSQuan Nguyen 		ssif_bmc->state = SSIF_ABORTING;
680dd2bc5ccSQuan Nguyen 		return;
681dd2bc5ccSQuan Nguyen 	}
682dd2bc5ccSQuan Nguyen 
683dd2bc5ccSQuan Nguyen 	ssif_bmc->msg_idx = 0;
684dd2bc5ccSQuan Nguyen 	ssif_bmc->part_buf.address = *val;
685dd2bc5ccSQuan Nguyen }
686dd2bc5ccSQuan Nguyen 
on_write_received_event(struct ssif_bmc_ctx * ssif_bmc,u8 * val)687dd2bc5ccSQuan Nguyen static void on_write_received_event(struct ssif_bmc_ctx *ssif_bmc, u8 *val)
688dd2bc5ccSQuan Nguyen {
689dd2bc5ccSQuan Nguyen 	if (ssif_bmc->state == SSIF_READY ||
690dd2bc5ccSQuan Nguyen 	    ssif_bmc->state == SSIF_RES_SENDING) {
691dd2bc5ccSQuan Nguyen 		dev_warn(&ssif_bmc->client->dev,
692dd2bc5ccSQuan Nguyen 			 "Warn: %s unexpected WRITE RECEIVED in state=%s\n",
693dd2bc5ccSQuan Nguyen 			 __func__, state_to_string(ssif_bmc->state));
694dd2bc5ccSQuan Nguyen 		ssif_bmc->state = SSIF_ABORTING;
695dd2bc5ccSQuan Nguyen 
696dd2bc5ccSQuan Nguyen 	} else if (ssif_bmc->state == SSIF_START) {
697dd2bc5ccSQuan Nguyen 		ssif_bmc->state = SSIF_SMBUS_CMD;
698dd2bc5ccSQuan Nguyen 
699dd2bc5ccSQuan Nguyen 	} else if (ssif_bmc->state == SSIF_SMBUS_CMD) {
700dd2bc5ccSQuan Nguyen 		if (!supported_write_cmd(ssif_bmc->part_buf.smbus_cmd)) {
701dd2bc5ccSQuan Nguyen 			dev_warn(&ssif_bmc->client->dev, "Warn: Unknown SMBus write command=0x%x",
702dd2bc5ccSQuan Nguyen 				 ssif_bmc->part_buf.smbus_cmd);
703dd2bc5ccSQuan Nguyen 			ssif_bmc->aborting = true;
704dd2bc5ccSQuan Nguyen 		}
705dd2bc5ccSQuan Nguyen 
706dd2bc5ccSQuan Nguyen 		if (ssif_bmc->aborting)
707dd2bc5ccSQuan Nguyen 			ssif_bmc->state = SSIF_ABORTING;
708dd2bc5ccSQuan Nguyen 		else
709dd2bc5ccSQuan Nguyen 			ssif_bmc->state = SSIF_REQ_RECVING;
710dd2bc5ccSQuan Nguyen 	}
711dd2bc5ccSQuan Nguyen 
712dd2bc5ccSQuan Nguyen 	/* This is response sending state */
713dd2bc5ccSQuan Nguyen 	if (ssif_bmc->state == SSIF_REQ_RECVING)
714dd2bc5ccSQuan Nguyen 		handle_write_received(ssif_bmc, val);
715dd2bc5ccSQuan Nguyen 	else if (ssif_bmc->state == SSIF_SMBUS_CMD)
716dd2bc5ccSQuan Nguyen 		process_smbus_cmd(ssif_bmc, val);
717dd2bc5ccSQuan Nguyen }
718dd2bc5ccSQuan Nguyen 
on_stop_event(struct ssif_bmc_ctx * ssif_bmc,u8 * val)719dd2bc5ccSQuan Nguyen static void on_stop_event(struct ssif_bmc_ctx *ssif_bmc, u8 *val)
720dd2bc5ccSQuan Nguyen {
721dd2bc5ccSQuan Nguyen 	if (ssif_bmc->state == SSIF_READY ||
722dd2bc5ccSQuan Nguyen 	    ssif_bmc->state == SSIF_START ||
723dd2bc5ccSQuan Nguyen 	    ssif_bmc->state == SSIF_SMBUS_CMD ||
724dd2bc5ccSQuan Nguyen 	    ssif_bmc->state == SSIF_ABORTING) {
725dd2bc5ccSQuan Nguyen 		dev_warn(&ssif_bmc->client->dev,
726dd2bc5ccSQuan Nguyen 			 "Warn: %s unexpected SLAVE STOP in state=%s\n",
727dd2bc5ccSQuan Nguyen 			 __func__, state_to_string(ssif_bmc->state));
728dd2bc5ccSQuan Nguyen 		ssif_bmc->state = SSIF_READY;
729dd2bc5ccSQuan Nguyen 
730dd2bc5ccSQuan Nguyen 	} else if (ssif_bmc->state == SSIF_REQ_RECVING) {
731dd2bc5ccSQuan Nguyen 		if (validate_request_part(ssif_bmc)) {
732dd2bc5ccSQuan Nguyen 			process_request_part(ssif_bmc);
733dd2bc5ccSQuan Nguyen 			if (ssif_bmc->part_buf.smbus_cmd == SSIF_IPMI_SINGLEPART_WRITE ||
734dd2bc5ccSQuan Nguyen 			    ssif_bmc->part_buf.smbus_cmd == SSIF_IPMI_MULTIPART_WRITE_END)
735dd2bc5ccSQuan Nguyen 				handle_request(ssif_bmc);
736dd2bc5ccSQuan Nguyen 			ssif_bmc->state = SSIF_READY;
737dd2bc5ccSQuan Nguyen 		} else {
738dd2bc5ccSQuan Nguyen 			/*
739dd2bc5ccSQuan Nguyen 			 * A BMC that receives an invalid request drop the data for the write
740dd2bc5ccSQuan Nguyen 			 * transaction and any further transactions (read or write) until
741dd2bc5ccSQuan Nguyen 			 * the next valid read or write Start transaction is received
742dd2bc5ccSQuan Nguyen 			 */
743dd2bc5ccSQuan Nguyen 			dev_err(&ssif_bmc->client->dev, "Error: invalid pec\n");
744dd2bc5ccSQuan Nguyen 			ssif_bmc->aborting = true;
745dd2bc5ccSQuan Nguyen 		}
746dd2bc5ccSQuan Nguyen 	} else if (ssif_bmc->state == SSIF_RES_SENDING) {
747dd2bc5ccSQuan Nguyen 		if (ssif_bmc->is_singlepart_read || ssif_bmc->block_num == 0xFF)
748dd2bc5ccSQuan Nguyen 			/* Invalidate response buffer to denote it is sent */
749dd2bc5ccSQuan Nguyen 			complete_response(ssif_bmc);
750dd2bc5ccSQuan Nguyen 		ssif_bmc->state = SSIF_READY;
751dd2bc5ccSQuan Nguyen 	}
752dd2bc5ccSQuan Nguyen 
753dd2bc5ccSQuan Nguyen 	/* Reset message index */
754dd2bc5ccSQuan Nguyen 	ssif_bmc->msg_idx = 0;
755dd2bc5ccSQuan Nguyen }
756dd2bc5ccSQuan Nguyen 
757dd2bc5ccSQuan Nguyen /*
758dd2bc5ccSQuan Nguyen  * Callback function to handle I2C slave events
759dd2bc5ccSQuan Nguyen  */
ssif_bmc_cb(struct i2c_client * client,enum i2c_slave_event event,u8 * val)760dd2bc5ccSQuan Nguyen static int ssif_bmc_cb(struct i2c_client *client, enum i2c_slave_event event, u8 *val)
761dd2bc5ccSQuan Nguyen {
762dd2bc5ccSQuan Nguyen 	unsigned long flags;
763dd2bc5ccSQuan Nguyen 	struct ssif_bmc_ctx *ssif_bmc = i2c_get_clientdata(client);
764dd2bc5ccSQuan Nguyen 	int ret = 0;
765dd2bc5ccSQuan Nguyen 
766dd2bc5ccSQuan Nguyen 	spin_lock_irqsave(&ssif_bmc->lock, flags);
767dd2bc5ccSQuan Nguyen 
768dd2bc5ccSQuan Nguyen 	switch (event) {
769dd2bc5ccSQuan Nguyen 	case I2C_SLAVE_READ_REQUESTED:
770dd2bc5ccSQuan Nguyen 		on_read_requested_event(ssif_bmc, val);
771dd2bc5ccSQuan Nguyen 		break;
772dd2bc5ccSQuan Nguyen 
773dd2bc5ccSQuan Nguyen 	case I2C_SLAVE_WRITE_REQUESTED:
774dd2bc5ccSQuan Nguyen 		on_write_requested_event(ssif_bmc, val);
775dd2bc5ccSQuan Nguyen 		break;
776dd2bc5ccSQuan Nguyen 
777dd2bc5ccSQuan Nguyen 	case I2C_SLAVE_READ_PROCESSED:
778dd2bc5ccSQuan Nguyen 		on_read_processed_event(ssif_bmc, val);
779dd2bc5ccSQuan Nguyen 		break;
780dd2bc5ccSQuan Nguyen 
781dd2bc5ccSQuan Nguyen 	case I2C_SLAVE_WRITE_RECEIVED:
782dd2bc5ccSQuan Nguyen 		on_write_received_event(ssif_bmc, val);
783dd2bc5ccSQuan Nguyen 		break;
784dd2bc5ccSQuan Nguyen 
785dd2bc5ccSQuan Nguyen 	case I2C_SLAVE_STOP:
786dd2bc5ccSQuan Nguyen 		on_stop_event(ssif_bmc, val);
787dd2bc5ccSQuan Nguyen 		break;
788dd2bc5ccSQuan Nguyen 
789dd2bc5ccSQuan Nguyen 	default:
790dd2bc5ccSQuan Nguyen 		dev_warn(&ssif_bmc->client->dev, "Warn: Unknown i2c slave event\n");
791dd2bc5ccSQuan Nguyen 		break;
792dd2bc5ccSQuan Nguyen 	}
793dd2bc5ccSQuan Nguyen 
794dd2bc5ccSQuan Nguyen 	if (!ssif_bmc->aborting && ssif_bmc->busy)
795dd2bc5ccSQuan Nguyen 		ret = -EBUSY;
796dd2bc5ccSQuan Nguyen 
797dd2bc5ccSQuan Nguyen 	spin_unlock_irqrestore(&ssif_bmc->lock, flags);
798dd2bc5ccSQuan Nguyen 
799dd2bc5ccSQuan Nguyen 	return ret;
800dd2bc5ccSQuan Nguyen }
801dd2bc5ccSQuan Nguyen 
ssif_bmc_probe(struct i2c_client * client)802b8fadb39SUwe Kleine-König static int ssif_bmc_probe(struct i2c_client *client)
803dd2bc5ccSQuan Nguyen {
804dd2bc5ccSQuan Nguyen 	struct ssif_bmc_ctx *ssif_bmc;
805dd2bc5ccSQuan Nguyen 	int ret;
806dd2bc5ccSQuan Nguyen 
807dd2bc5ccSQuan Nguyen 	ssif_bmc = devm_kzalloc(&client->dev, sizeof(*ssif_bmc), GFP_KERNEL);
808dd2bc5ccSQuan Nguyen 	if (!ssif_bmc)
809dd2bc5ccSQuan Nguyen 		return -ENOMEM;
810dd2bc5ccSQuan Nguyen 
811dd2bc5ccSQuan Nguyen 	spin_lock_init(&ssif_bmc->lock);
812dd2bc5ccSQuan Nguyen 
813dd2bc5ccSQuan Nguyen 	init_waitqueue_head(&ssif_bmc->wait_queue);
814dd2bc5ccSQuan Nguyen 	ssif_bmc->request_available = false;
815dd2bc5ccSQuan Nguyen 	ssif_bmc->response_in_progress = false;
816dd2bc5ccSQuan Nguyen 	ssif_bmc->busy = false;
817dd2bc5ccSQuan Nguyen 	ssif_bmc->response_timer_inited = false;
818dd2bc5ccSQuan Nguyen 
819dd2bc5ccSQuan Nguyen 	/* Register misc device interface */
820dd2bc5ccSQuan Nguyen 	ssif_bmc->miscdev.minor = MISC_DYNAMIC_MINOR;
821dd2bc5ccSQuan Nguyen 	ssif_bmc->miscdev.name = DEVICE_NAME;
822dd2bc5ccSQuan Nguyen 	ssif_bmc->miscdev.fops = &ssif_bmc_fops;
823dd2bc5ccSQuan Nguyen 	ssif_bmc->miscdev.parent = &client->dev;
824dd2bc5ccSQuan Nguyen 	ret = misc_register(&ssif_bmc->miscdev);
825dd2bc5ccSQuan Nguyen 	if (ret)
826dd2bc5ccSQuan Nguyen 		return ret;
827dd2bc5ccSQuan Nguyen 
828dd2bc5ccSQuan Nguyen 	ssif_bmc->client = client;
829dd2bc5ccSQuan Nguyen 	ssif_bmc->client->flags |= I2C_CLIENT_SLAVE;
830dd2bc5ccSQuan Nguyen 
831dd2bc5ccSQuan Nguyen 	/* Register I2C slave */
832dd2bc5ccSQuan Nguyen 	i2c_set_clientdata(client, ssif_bmc);
833dd2bc5ccSQuan Nguyen 	ret = i2c_slave_register(client, ssif_bmc_cb);
834dd2bc5ccSQuan Nguyen 	if (ret)
835dd2bc5ccSQuan Nguyen 		misc_deregister(&ssif_bmc->miscdev);
836dd2bc5ccSQuan Nguyen 
837dd2bc5ccSQuan Nguyen 	return ret;
838dd2bc5ccSQuan Nguyen }
839dd2bc5ccSQuan Nguyen 
ssif_bmc_remove(struct i2c_client * client)840dd2bc5ccSQuan Nguyen static void ssif_bmc_remove(struct i2c_client *client)
841dd2bc5ccSQuan Nguyen {
842dd2bc5ccSQuan Nguyen 	struct ssif_bmc_ctx *ssif_bmc = i2c_get_clientdata(client);
843dd2bc5ccSQuan Nguyen 
844dd2bc5ccSQuan Nguyen 	i2c_slave_unregister(client);
845dd2bc5ccSQuan Nguyen 	misc_deregister(&ssif_bmc->miscdev);
846dd2bc5ccSQuan Nguyen }
847dd2bc5ccSQuan Nguyen 
848dd2bc5ccSQuan Nguyen static const struct of_device_id ssif_bmc_match[] = {
849dd2bc5ccSQuan Nguyen 	{ .compatible = "ssif-bmc" },
850dd2bc5ccSQuan Nguyen 	{ },
851dd2bc5ccSQuan Nguyen };
852dd2bc5ccSQuan Nguyen MODULE_DEVICE_TABLE(of, ssif_bmc_match);
853dd2bc5ccSQuan Nguyen 
854dd2bc5ccSQuan Nguyen static const struct i2c_device_id ssif_bmc_id[] = {
855*19a01155SUwe Kleine-König 	{ DEVICE_NAME },
856*19a01155SUwe Kleine-König 	{ }
857dd2bc5ccSQuan Nguyen };
858dd2bc5ccSQuan Nguyen MODULE_DEVICE_TABLE(i2c, ssif_bmc_id);
859dd2bc5ccSQuan Nguyen 
860dd2bc5ccSQuan Nguyen static struct i2c_driver ssif_bmc_driver = {
861dd2bc5ccSQuan Nguyen 	.driver         = {
862dd2bc5ccSQuan Nguyen 		.name           = DEVICE_NAME,
863dd2bc5ccSQuan Nguyen 		.of_match_table = ssif_bmc_match,
864dd2bc5ccSQuan Nguyen 	},
865e64c82b8SUwe Kleine-König 	.probe          = ssif_bmc_probe,
866dd2bc5ccSQuan Nguyen 	.remove         = ssif_bmc_remove,
867dd2bc5ccSQuan Nguyen 	.id_table       = ssif_bmc_id,
868dd2bc5ccSQuan Nguyen };
869dd2bc5ccSQuan Nguyen 
870dd2bc5ccSQuan Nguyen module_i2c_driver(ssif_bmc_driver);
871dd2bc5ccSQuan Nguyen 
872dd2bc5ccSQuan Nguyen MODULE_AUTHOR("Quan Nguyen <quan@os.amperecomputing.com>");
873dd2bc5ccSQuan Nguyen MODULE_AUTHOR("Chuong Tran <chuong@os.amperecomputing.com>");
874dd2bc5ccSQuan Nguyen MODULE_DESCRIPTION("Linux device driver of the BMC IPMI SSIF interface.");
875dd2bc5ccSQuan Nguyen MODULE_LICENSE("GPL");
876