summaryrefslogtreecommitdiff
path: root/drivers/i2c/busses/xen-i2cback.c
blob: b9036b864451ddec39a83be7c567436efbffb8ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright 2018-2019 NXP
 *
 * Peng Fan <peng.fan@nxp.com>
 */

#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/i2c.h>
#include <linux/slab.h>

#include <xen/xen.h>
#include <xen/events.h>
#include <xen/xenbus.h>
#include <xen/grant_table.h>
#include <xen/page.h>

#include <xen/interface/grant_table.h>
#include <xen/interface/io/i2cif.h>

struct i2cback_info {
	domid_t domid;
	u32 irq;
	u64 handle;
	struct xenbus_device *i2cdev;
	spinlock_t i2c_ring_lock;
	struct i2cif_back_ring i2c_ring;
	int is_connected;
	int ring_error;
	struct i2c_adapter *adapter;
	u32 num_slaves;
	u32 *allowed_slaves;
};

static bool i2cback_access_allowed(struct i2cback_info *info,
				   struct i2cif_request *req)
{
	int i;

	if (req->is_smbus) {/*check for smbus access permission*/
		for (i = 0; i < info->num_slaves; i++)
			if (req->addr == info->allowed_slaves[i])
				return true;

		return false;
	}

	/*check for master_xfer access permission*/
	if (req->num_msg == I2CIF_MAX_MSG) {
		if (req->msg[0].addr != req->msg[1].addr)
			return false;
	}

	for (i = 0; i < info->num_slaves; i++) {
		if (req->msg[0].addr == info->allowed_slaves[i])
			return true;
	}

	return false;
}

static bool i2cback_handle_int(struct i2cback_info *info)
{
	struct i2cif_back_ring *i2c_ring = &info->i2c_ring;
	struct i2cif_request req;
	struct i2cif_response *res;
	RING_IDX rc, rp;
	int more_to_do, notify, num_msg = 0, ret;
	struct i2c_msg msg[I2CIF_MAX_MSG];
	union i2c_smbus_data smbus_data;
	char tmp_buf[I2CIF_BUF_LEN];
	unsigned long flags;
	bool allow_access;
	int i;

	rc = i2c_ring->req_cons;
	rp = i2c_ring->sring->req_prod;
	rmb();	/* req_cons is written by frontend. */

	if (RING_REQUEST_PROD_OVERFLOW(i2c_ring, rp)) {
		rc = i2c_ring->rsp_prod_pvt;
		dev_err(&info->i2cdev->dev, "ring overflow\n");
		info->ring_error = 1;
		return 0;
	}

	while (rc != rp) {
		if (RING_REQUEST_CONS_OVERFLOW(i2c_ring, rc)) {
			dev_err(&info->i2cdev->dev, "%s overflow\n", __func__);
			break;
		}

		req = *RING_GET_REQUEST(i2c_ring, rc);
		allow_access = i2cback_access_allowed(info, &req);
		if (allow_access && !req.is_smbus) {
			/* Write/Read sequence */
			num_msg = req.num_msg;
			if (num_msg > I2CIF_MAX_MSG)
				num_msg = I2CIF_MAX_MSG;

			for (i = 0; i < num_msg; i++) {
				msg[i].addr = req.msg[i].addr;
				msg[i].len = req.msg[i].len;
				msg[i].flags = 0;
				if (req.msg[i].flags & I2CIF_M_RD)
					msg[i].flags |= I2C_M_RD;
				if (req.msg[i].flags & I2CIF_M_TEN)
					msg[i].flags |= I2C_M_TEN;
				if (req.msg[i].flags & I2CIF_M_RECV_LEN)
					msg[i].flags |= I2C_M_RECV_LEN;
				if (req.msg[i].flags & I2CIF_M_NO_RD_ACK)
					msg[i].flags |= I2C_M_NO_RD_ACK;
				if (req.msg[i].flags & I2CIF_M_IGNORE_NAK)
					msg[i].flags |= I2C_M_IGNORE_NAK;
				if (req.msg[i].flags & I2CIF_M_REV_DIR_ADDR)
					msg[i].flags |= I2C_M_REV_DIR_ADDR;
				if (req.msg[i].flags & I2CIF_M_NOSTART)
					msg[i].flags |= I2C_M_NOSTART;
				if (req.msg[i].flags & I2CIF_M_STOP)
					msg[i].flags |= I2C_M_STOP;
			}

			if ((num_msg == 2) &&
			    (!(msg[0].flags & I2C_M_RD)) &&
			    (msg[1].flags & I2C_M_RD)) {

				/* overwrite the remote buf with local buf */
				msg[0].buf = tmp_buf;
				msg[1].buf = tmp_buf;

				/* msg[0] write buf */
				memcpy(tmp_buf, req.write_buf, I2CIF_BUF_LEN);
				ret = i2c_transfer(info->adapter, msg,
						   num_msg);
			} else if (num_msg == 1) {
				msg[0].buf = tmp_buf;
				if (!(msg[0].flags & I2C_M_RD))
					memcpy(tmp_buf, req.write_buf,
					       I2CIF_BUF_LEN);
				ret = i2c_transfer(info->adapter, msg,
						   req.num_msg);
			} else {
				dev_dbg(&info->i2cdev->dev, "too many msgs\n");

				ret = -EIO;
			}
		} else if (allow_access && req.is_smbus) {
			memcpy(&smbus_data, &req.write_buf, sizeof(smbus_data));

			ret = i2c_smbus_xfer(info->adapter,
								req.addr,
								req.flags,
								req.read_write,
								req.command,
								req.protocol,
								&smbus_data);
		}

		spin_lock_irqsave(&info->i2c_ring_lock, flags);
		res = RING_GET_RESPONSE(&info->i2c_ring,
					info->i2c_ring.rsp_prod_pvt);

		if (allow_access && !req.is_smbus) {
			res->result = ret;

			if ((req.num_msg == 2) &&
			    (!(msg[0].flags & I2C_M_RD)) &&
			    (msg[1].flags & I2C_M_RD) && (ret >= 0)) {
				memcpy(res->read_buf, tmp_buf, I2CIF_BUF_LEN);
			} else if (req.num_msg == 1) {
				if ((msg[0].flags & I2C_M_RD) && (ret >= 0))
					memcpy(res->read_buf, tmp_buf,
					       I2CIF_BUF_LEN);
			}
		} else if (allow_access && req.is_smbus) {
			if (req.read_write == I2C_SMBUS_READ)
				memcpy(&res->read_buf, &smbus_data, sizeof(smbus_data));
			res->result = ret;
		} else
			res->result = -EPERM;

		info->i2c_ring.rsp_prod_pvt++;

		barrier();
		RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&info->i2c_ring,
						     notify);
		spin_unlock_irqrestore(&info->i2c_ring_lock, flags);

		if (notify)
			notify_remote_via_irq(info->irq);

		i2c_ring->req_cons = ++rc;

		cond_resched();
	}

	RING_FINAL_CHECK_FOR_REQUESTS(i2c_ring, more_to_do);

	return !!more_to_do;
}

static irqreturn_t i2cback_be_int(int irq, void *dev_id)
{
	struct i2cback_info *info = dev_id;

	if (info->ring_error)
		return IRQ_HANDLED;

	while (i2cback_handle_int(info))
		cond_resched();

	return IRQ_HANDLED;
}

static int i2cback_map(struct i2cback_info *info, grant_ref_t *i2c_ring_ref,
		       evtchn_port_t evtchn)
{
	int err;
	void *addr;
	struct i2cif_sring *i2c_sring;

	if (info->irq)
		return 0;

	err = xenbus_map_ring_valloc(info->i2cdev, i2c_ring_ref, 1, &addr);
	if (err)
		return err;

	i2c_sring = addr;

	BACK_RING_INIT(&info->i2c_ring, i2c_sring, PAGE_SIZE);

	err = bind_interdomain_evtchn_to_irq(info->domid, evtchn);
	if (err < 0)
		goto fail_evtchn;
	info->irq = err;

	err = request_threaded_irq(info->irq, NULL, i2cback_be_int,
				   IRQF_ONESHOT, "xen-i2cback", info);
	if (err) {
		dev_err(&info->i2cdev->dev, "bind evtchn to irq failure!\n");
		goto free_irq;
	}

	return 0;
free_irq:
	unbind_from_irqhandler(info->irq, info);
	info->irq = 0;
	info->i2c_ring.sring = NULL;
fail_evtchn:
	xenbus_unmap_ring_vfree(info->i2cdev, i2c_sring);
	return err;
}

static int i2cback_connect_rings(struct i2cback_info *info)
{
	struct xenbus_device *dev = info->i2cdev;
	unsigned int i2c_ring_ref, evtchn;
	int i, err;
	char *buf;
	u32 adapter_id;

	err = xenbus_scanf(XBT_NIL, dev->nodename,
			   "adapter", "%u", &adapter_id);
	if (err != 1) {
		xenbus_dev_fatal(dev, err, "%s reading adapter", dev->nodename);
		return err;
	}

	info->adapter = i2c_get_adapter(adapter_id);
	if (!info->adapter)
		return -ENODEV;

	err = xenbus_scanf(XBT_NIL, dev->nodename,
			   "num-slaves", "%u", &info->num_slaves);
	if (err != 1) {
		xenbus_dev_fatal(dev, err, "%s reading num-slaves",
				 dev->nodename);
		return err;
	}

	info->allowed_slaves = devm_kmalloc(&dev->dev,
					    info->num_slaves * sizeof(u32),
					    GFP_KERNEL);
	if (!info->allowed_slaves)
		return -ENOMEM;

	/* 128 bytes is enough */
	buf = kmalloc(128, GFP_KERNEL);

	for (i = 0; i < info->num_slaves; i++) {
		snprintf(buf, 128, "%s/%d", dev->nodename, i);
		err = xenbus_scanf(XBT_NIL, buf, "addr", "%x",
				   &info->allowed_slaves[i]);
		if (err != 1) {
			kfree(buf);
			return err;
		}
	}

	kfree(buf);

	err = xenbus_gather(XBT_NIL, dev->otherend,
			    "ring-ref", "%u", &i2c_ring_ref,
			    "event-channel", "%u", &evtchn, NULL);
	if (err) {
		xenbus_dev_fatal(dev, err,
				 "reading %s/ring-ref and event-channel",
				 dev->otherend);
		return err;
	}

	dev_info(&info->i2cdev->dev,
		 "xen-pvi2c: ring-ref %u, event-channel %u\n",
		 i2c_ring_ref, evtchn);

	err = i2cback_map(info, &i2c_ring_ref, evtchn);
	if (err)
		xenbus_dev_fatal(dev, err, "mapping ring-ref %u evtchn %u",
			i2c_ring_ref, evtchn);

	return err;
}

static void i2cback_disconnect(struct i2cback_info *info)
{
	if (info->irq) {
		unbind_from_irqhandler(info->irq, info);
		info->irq = 0;
	}

	if (info->i2c_ring.sring) {
		xenbus_unmap_ring_vfree(info->i2cdev, info->i2c_ring.sring);
		info->i2c_ring.sring = NULL;
	}
}

static void i2cback_frontend_changed(struct xenbus_device *dev,
				     enum xenbus_state frontend_state)
{
	struct i2cback_info *info = dev_get_drvdata(&dev->dev);
	int ret;

	switch (frontend_state) {
	case XenbusStateInitialised:
	case XenbusStateReconfiguring:
	case XenbusStateReconfigured:
		break;

	case XenbusStateInitialising:
		if (dev->state == XenbusStateClosed) {
			dev_info(&dev->dev,
				 "xen-pvi2c: %s: prepare for reconnect\n",
				 dev->nodename);
			xenbus_switch_state(dev, XenbusStateInitWait);
		}
		break;
	case XenbusStateConnected:
		if (dev->state == XenbusStateConnected)
			break;

		xenbus_switch_state(dev, XenbusStateConnected);

		ret = i2cback_connect_rings(info);
		if (ret)
			xenbus_dev_fatal(dev, ret, "connect ring fail");
		break;
	case XenbusStateClosing:
		i2cback_disconnect(info);
		xenbus_switch_state(dev, XenbusStateClosing);
		break;
	case XenbusStateClosed:
		xenbus_switch_state(dev, XenbusStateClosed);
		if (xenbus_dev_is_online(dev))
			break;
		device_unregister(&dev->dev);
		break;
	case XenbusStateUnknown:
		device_unregister(&dev->dev);
		break;

	default:
		xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
				 frontend_state);
		break;
	}
}

static struct i2cback_info *i2cback_alloc(domid_t domid, u64 handle)
{
	struct i2cback_info *info;

	info = kzalloc(sizeof(struct i2cback_info), GFP_KERNEL);
	if (!info)
		return NULL;

	info->domid = domid;
	info->handle = handle;
	spin_lock_init(&info->i2c_ring_lock);
	info->ring_error = 0;

	return info;
}

static int i2cback_probe(struct xenbus_device *dev,
			     const struct xenbus_device_id *id)
{
	struct i2cback_info *info;
	unsigned long handle;
	int err;

	if (kstrtoul(strrchr(dev->otherend, '/') + 1, 0, &handle))
		return -EINVAL;

	info = i2cback_alloc(dev->otherend_id, handle);
	if (!info) {
		xenbus_dev_fatal(dev, -ENOMEM, "Allocating backend interface");
		return -ENOMEM;
	}

	info->i2cdev = dev;
	dev_set_drvdata(&dev->dev, info);

	err = xenbus_switch_state(dev, XenbusStateInitWait);
	if (err)
		return err;

	return 0;
}

static int i2cback_remove(struct xenbus_device *dev)
{
	struct i2cback_info *info = dev_get_drvdata(&dev->dev);

	if (!info)
		return 0;

	i2cback_disconnect(info);

	kfree(info);
	dev_set_drvdata(&dev->dev, NULL);

	dev_info(&dev->dev, "%s\n", __func__);

	return 0;
}

static const struct xenbus_device_id i2cback_ids[] = {
	{ "vi2c" },
	{ "" },
};

static struct xenbus_driver i2cback_driver = {
	.ids			= i2cback_ids,
	.probe			= i2cback_probe,
	.otherend_changed	= i2cback_frontend_changed,
	.remove			= i2cback_remove,
};

static int __init i2cback_init(void)
{
	int err;

	if (!xen_domain())
		return -ENODEV;

	err = xenbus_register_backend(&i2cback_driver);
	if (err)
		return err;

	return 0;
}
module_init(i2cback_init);

static void __exit i2cback_exit(void)
{
	xenbus_unregister_driver(&i2cback_driver);
}
module_exit(i2cback_exit);

MODULE_ALIAS("xen-i2cback:vi2c");
MODULE_AUTHOR("Peng Fan <peng.fan@nxp.com>");
MODULE_DESCRIPTION("Xen I2C backend driver (i2cback)");
MODULE_LICENSE("GPL");