xref: /openbmc/linux/drivers/i2c/busses/i2c-virtio.c (revision c4a11bf4)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Virtio I2C Bus Driver
4  *
5  * The Virtio I2C Specification:
6  * https://raw.githubusercontent.com/oasis-tcs/virtio-spec/master/virtio-i2c.tex
7  *
8  * Copyright (c) 2021 Intel Corporation. All rights reserved.
9  */
10 
11 #include <linux/acpi.h>
12 #include <linux/completion.h>
13 #include <linux/err.h>
14 #include <linux/i2c.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/virtio.h>
18 #include <linux/virtio_ids.h>
19 #include <linux/virtio_config.h>
20 #include <linux/virtio_i2c.h>
21 
22 /**
23  * struct virtio_i2c - virtio I2C data
24  * @vdev: virtio device for this controller
25  * @completion: completion of virtio I2C message
26  * @adap: I2C adapter for this controller
27  * @vq: the virtio virtqueue for communication
28  */
29 struct virtio_i2c {
30 	struct virtio_device *vdev;
31 	struct completion completion;
32 	struct i2c_adapter adap;
33 	struct virtqueue *vq;
34 };
35 
36 /**
37  * struct virtio_i2c_req - the virtio I2C request structure
38  * @out_hdr: the OUT header of the virtio I2C message
39  * @buf: the buffer into which data is read, or from which it's written
40  * @in_hdr: the IN header of the virtio I2C message
41  */
42 struct virtio_i2c_req {
43 	struct virtio_i2c_out_hdr out_hdr	____cacheline_aligned;
44 	uint8_t *buf				____cacheline_aligned;
45 	struct virtio_i2c_in_hdr in_hdr		____cacheline_aligned;
46 };
47 
48 static void virtio_i2c_msg_done(struct virtqueue *vq)
49 {
50 	struct virtio_i2c *vi = vq->vdev->priv;
51 
52 	complete(&vi->completion);
53 }
54 
55 static int virtio_i2c_prepare_reqs(struct virtqueue *vq,
56 				   struct virtio_i2c_req *reqs,
57 				   struct i2c_msg *msgs, int num)
58 {
59 	struct scatterlist *sgs[3], out_hdr, msg_buf, in_hdr;
60 	int i;
61 
62 	for (i = 0; i < num; i++) {
63 		int outcnt = 0, incnt = 0;
64 
65 		/*
66 		 * Only 7-bit mode supported for this moment. For the address
67 		 * format, Please check the Virtio I2C Specification.
68 		 */
69 		reqs[i].out_hdr.addr = cpu_to_le16(msgs[i].addr << 1);
70 
71 		if (msgs[i].flags & I2C_M_RD)
72 			reqs[i].out_hdr.flags |= cpu_to_le32(VIRTIO_I2C_FLAGS_M_RD);
73 
74 		if (i != num - 1)
75 			reqs[i].out_hdr.flags |= cpu_to_le32(VIRTIO_I2C_FLAGS_FAIL_NEXT);
76 
77 		sg_init_one(&out_hdr, &reqs[i].out_hdr, sizeof(reqs[i].out_hdr));
78 		sgs[outcnt++] = &out_hdr;
79 
80 		if (msgs[i].len) {
81 			reqs[i].buf = i2c_get_dma_safe_msg_buf(&msgs[i], 1);
82 			if (!reqs[i].buf)
83 				break;
84 
85 			sg_init_one(&msg_buf, reqs[i].buf, msgs[i].len);
86 
87 			if (msgs[i].flags & I2C_M_RD)
88 				sgs[outcnt + incnt++] = &msg_buf;
89 			else
90 				sgs[outcnt++] = &msg_buf;
91 		}
92 
93 		sg_init_one(&in_hdr, &reqs[i].in_hdr, sizeof(reqs[i].in_hdr));
94 		sgs[outcnt + incnt++] = &in_hdr;
95 
96 		if (virtqueue_add_sgs(vq, sgs, outcnt, incnt, &reqs[i], GFP_KERNEL)) {
97 			i2c_put_dma_safe_msg_buf(reqs[i].buf, &msgs[i], false);
98 			break;
99 		}
100 	}
101 
102 	return i;
103 }
104 
105 static int virtio_i2c_complete_reqs(struct virtqueue *vq,
106 				    struct virtio_i2c_req *reqs,
107 				    struct i2c_msg *msgs, int num,
108 				    bool timedout)
109 {
110 	struct virtio_i2c_req *req;
111 	bool failed = timedout;
112 	unsigned int len;
113 	int i, j = 0;
114 
115 	for (i = 0; i < num; i++) {
116 		/* Detach the ith request from the vq */
117 		req = virtqueue_get_buf(vq, &len);
118 
119 		/*
120 		 * Condition req == &reqs[i] should always meet since we have
121 		 * total num requests in the vq. reqs[i] can never be NULL here.
122 		 */
123 		if (!failed && (WARN_ON(req != &reqs[i]) ||
124 				req->in_hdr.status != VIRTIO_I2C_MSG_OK))
125 			failed = true;
126 
127 		i2c_put_dma_safe_msg_buf(reqs[i].buf, &msgs[i], !failed);
128 
129 		if (!failed)
130 			j++;
131 	}
132 
133 	return timedout ? -ETIMEDOUT : j;
134 }
135 
136 static int virtio_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
137 			   int num)
138 {
139 	struct virtio_i2c *vi = i2c_get_adapdata(adap);
140 	struct virtqueue *vq = vi->vq;
141 	struct virtio_i2c_req *reqs;
142 	unsigned long time_left;
143 	int count;
144 
145 	reqs = kcalloc(num, sizeof(*reqs), GFP_KERNEL);
146 	if (!reqs)
147 		return -ENOMEM;
148 
149 	count = virtio_i2c_prepare_reqs(vq, reqs, msgs, num);
150 	if (!count)
151 		goto err_free;
152 
153 	/*
154 	 * For the case where count < num, i.e. we weren't able to queue all the
155 	 * msgs, ideally we should abort right away and return early, but some
156 	 * of the messages are already sent to the remote I2C controller and the
157 	 * virtqueue will be left in undefined state in that case. We kick the
158 	 * remote here to clear the virtqueue, so we can try another set of
159 	 * messages later on.
160 	 */
161 
162 	reinit_completion(&vi->completion);
163 	virtqueue_kick(vq);
164 
165 	time_left = wait_for_completion_timeout(&vi->completion, adap->timeout);
166 	if (!time_left)
167 		dev_err(&adap->dev, "virtio i2c backend timeout.\n");
168 
169 	count = virtio_i2c_complete_reqs(vq, reqs, msgs, count, !time_left);
170 
171 err_free:
172 	kfree(reqs);
173 	return count;
174 }
175 
176 static void virtio_i2c_del_vqs(struct virtio_device *vdev)
177 {
178 	vdev->config->reset(vdev);
179 	vdev->config->del_vqs(vdev);
180 }
181 
182 static int virtio_i2c_setup_vqs(struct virtio_i2c *vi)
183 {
184 	struct virtio_device *vdev = vi->vdev;
185 
186 	vi->vq = virtio_find_single_vq(vdev, virtio_i2c_msg_done, "msg");
187 	return PTR_ERR_OR_ZERO(vi->vq);
188 }
189 
190 static u32 virtio_i2c_func(struct i2c_adapter *adap)
191 {
192 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
193 }
194 
195 static struct i2c_algorithm virtio_algorithm = {
196 	.master_xfer = virtio_i2c_xfer,
197 	.functionality = virtio_i2c_func,
198 };
199 
200 static int virtio_i2c_probe(struct virtio_device *vdev)
201 {
202 	struct virtio_i2c *vi;
203 	int ret;
204 
205 	if (!virtio_has_feature(vdev, VIRTIO_I2C_F_ZERO_LENGTH_REQUEST)) {
206 		dev_err(&vdev->dev, "Zero-length request feature is mandatory\n");
207 		return -EINVAL;
208 	}
209 
210 	vi = devm_kzalloc(&vdev->dev, sizeof(*vi), GFP_KERNEL);
211 	if (!vi)
212 		return -ENOMEM;
213 
214 	vdev->priv = vi;
215 	vi->vdev = vdev;
216 
217 	init_completion(&vi->completion);
218 
219 	ret = virtio_i2c_setup_vqs(vi);
220 	if (ret)
221 		return ret;
222 
223 	vi->adap.owner = THIS_MODULE;
224 	snprintf(vi->adap.name, sizeof(vi->adap.name),
225 		 "i2c_virtio at virtio bus %d", vdev->index);
226 	vi->adap.algo = &virtio_algorithm;
227 	vi->adap.dev.parent = &vdev->dev;
228 	vi->adap.dev.of_node = vdev->dev.of_node;
229 	i2c_set_adapdata(&vi->adap, vi);
230 
231 	/*
232 	 * Setup ACPI node for controlled devices which will be probed through
233 	 * ACPI.
234 	 */
235 	ACPI_COMPANION_SET(&vi->adap.dev, ACPI_COMPANION(vdev->dev.parent));
236 
237 	ret = i2c_add_adapter(&vi->adap);
238 	if (ret)
239 		virtio_i2c_del_vqs(vdev);
240 
241 	return ret;
242 }
243 
244 static void virtio_i2c_remove(struct virtio_device *vdev)
245 {
246 	struct virtio_i2c *vi = vdev->priv;
247 
248 	i2c_del_adapter(&vi->adap);
249 	virtio_i2c_del_vqs(vdev);
250 }
251 
252 static struct virtio_device_id id_table[] = {
253 	{ VIRTIO_ID_I2C_ADAPTER, VIRTIO_DEV_ANY_ID },
254 	{}
255 };
256 MODULE_DEVICE_TABLE(virtio, id_table);
257 
258 #ifdef CONFIG_PM_SLEEP
259 static int virtio_i2c_freeze(struct virtio_device *vdev)
260 {
261 	virtio_i2c_del_vqs(vdev);
262 	return 0;
263 }
264 
265 static int virtio_i2c_restore(struct virtio_device *vdev)
266 {
267 	return virtio_i2c_setup_vqs(vdev->priv);
268 }
269 #endif
270 
271 static const unsigned int features[] = {
272 	VIRTIO_I2C_F_ZERO_LENGTH_REQUEST,
273 };
274 
275 static struct virtio_driver virtio_i2c_driver = {
276 	.feature_table		= features,
277 	.feature_table_size	= ARRAY_SIZE(features),
278 	.id_table		= id_table,
279 	.probe			= virtio_i2c_probe,
280 	.remove			= virtio_i2c_remove,
281 	.driver			= {
282 		.name	= "i2c_virtio",
283 	},
284 #ifdef CONFIG_PM_SLEEP
285 	.freeze = virtio_i2c_freeze,
286 	.restore = virtio_i2c_restore,
287 #endif
288 };
289 module_virtio_driver(virtio_i2c_driver);
290 
291 MODULE_AUTHOR("Jie Deng <jie.deng@intel.com>");
292 MODULE_AUTHOR("Conghui Chen <conghui.chen@intel.com>");
293 MODULE_DESCRIPTION("Virtio i2c bus driver");
294 MODULE_LICENSE("GPL");
295