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