summaryrefslogtreecommitdiffstats
path: root/drivers/misc/mic/host/mic_fops.c
blob: 8cc1d90cd949c3051e03e730b6110d4c159a5bcd (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
/*
 * Intel MIC Platform Software Stack (MPSS)
 *
 * Copyright(c) 2013 Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * The full GNU General Public License is included in this distribution in
 * the file called "COPYING".
 *
 * Intel MIC Host driver.
 *
 */
#include <linux/poll.h>
#include <linux/pci.h>

#include <linux/mic_common.h>
#include "../common/mic_dev.h"
#include "mic_device.h"
#include "mic_fops.h"
#include "mic_virtio.h"

int mic_open(struct inode *inode, struct file *f)
{
	struct mic_vdev *mvdev;
	struct mic_device *mdev = container_of(f->private_data,
		struct mic_device, miscdev);

	mvdev = kzalloc(sizeof(*mvdev), GFP_KERNEL);
	if (!mvdev)
		return -ENOMEM;

	init_waitqueue_head(&mvdev->waitq);
	INIT_LIST_HEAD(&mvdev->list);
	mvdev->mdev = mdev;
	mvdev->virtio_id = -1;

	f->private_data = mvdev;
	return 0;
}

int mic_release(struct inode *inode, struct file *f)
{
	struct mic_vdev *mvdev = (struct mic_vdev *)f->private_data;

	if (-1 != mvdev->virtio_id)
		mic_virtio_del_device(mvdev);
	f->private_data = NULL;
	kfree(mvdev);
	return 0;
}

long mic_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
{
	struct mic_vdev *mvdev = (struct mic_vdev *)f->private_data;
	void __user *argp = (void __user *)arg;
	int ret;

	switch (cmd) {
	case MIC_VIRTIO_ADD_DEVICE:
	{
		ret = mic_virtio_add_device(mvdev, argp);
		if (ret < 0) {
			dev_err(mic_dev(mvdev),
				"%s %d errno ret %d\n",
				__func__, __LINE__, ret);
			return ret;
		}
		break;
	}
	case MIC_VIRTIO_COPY_DESC:
	{
		struct mic_copy_desc copy;

		ret = mic_vdev_inited(mvdev);
		if (ret)
			return ret;

		if (copy_from_user(&copy, argp, sizeof(copy)))
			return -EFAULT;

		dev_dbg(mic_dev(mvdev),
			"%s %d === iovcnt 0x%x vr_idx 0x%x update_used %d\n",
			__func__, __LINE__, copy.iovcnt, copy.vr_idx,
			copy.update_used);

		ret = mic_virtio_copy_desc(mvdev, &copy);
		if (ret < 0) {
			dev_err(mic_dev(mvdev),
				"%s %d errno ret %d\n",
				__func__, __LINE__, ret);
			return ret;
		}
		if (copy_to_user(
			&((struct mic_copy_desc __user *)argp)->out_len,
			&copy.out_len, sizeof(copy.out_len))) {
			dev_err(mic_dev(mvdev), "%s %d errno ret %d\n",
				__func__, __LINE__, -EFAULT);
			return -EFAULT;
		}
		break;
	}
	case MIC_VIRTIO_CONFIG_CHANGE:
	{
		ret = mic_vdev_inited(mvdev);
		if (ret)
			return ret;

		ret = mic_virtio_config_change(mvdev, argp);
		if (ret < 0) {
			dev_err(mic_dev(mvdev),
				"%s %d errno ret %d\n",
				__func__, __LINE__, ret);
			return ret;
		}
		break;
	}
	default:
		return -ENOIOCTLCMD;
	};
	return 0;
}

/*
 * We return POLLIN | POLLOUT from poll when new buffers are enqueued, and
 * not when previously enqueued buffers may be available. This means that
 * in the card->host (TX) path, when userspace is unblocked by poll it
 * must drain all available descriptors or it can stall.
 */
unsigned int mic_poll(struct file *f, poll_table *wait)
{
	struct mic_vdev *mvdev = (struct mic_vdev *)f->private_data;
	int mask = 0;

	poll_wait(f, &mvdev->waitq, wait);

	if (mic_vdev_inited(mvdev)) {
		mask = POLLERR;
	} else if (mvdev->poll_wake) {
		mvdev->poll_wake = 0;
		mask = POLLIN | POLLOUT;
	}

	return mask;
}

static inline int
mic_query_offset(struct mic_vdev *mvdev, unsigned long offset,
		 unsigned long *size, unsigned long *pa)
{
	struct mic_device *mdev = mvdev->mdev;
	unsigned long start = MIC_DP_SIZE;
	int i;

	/*
	 * MMAP interface is as follows:
	 * offset				region
	 * 0x0					virtio device_page
	 * 0x1000				first vring
	 * 0x1000 + size of 1st vring		second vring
	 * ....
	 */
	if (!offset) {
		*pa = virt_to_phys(mdev->dp);
		*size = MIC_DP_SIZE;
		return 0;
	}

	for (i = 0; i < mvdev->dd->num_vq; i++) {
		struct mic_vringh *mvr = &mvdev->mvr[i];
		if (offset == start) {
			*pa = virt_to_phys(mvr->vring.va);
			*size = mvr->vring.len;
			return 0;
		}
		start += mvr->vring.len;
	}
	return -1;
}

/*
 * Maps the device page and virtio rings to user space for readonly access.
 */
int
mic_mmap(struct file *f, struct vm_area_struct *vma)
{
	struct mic_vdev *mvdev = (struct mic_vdev *)f->private_data;
	unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
	unsigned long pa, size = vma->vm_end - vma->vm_start, size_rem = size;
	int i, err;

	err = mic_vdev_inited(mvdev);
	if (err)
		return err;

	if (vma->vm_flags & VM_WRITE)
		return -EACCES;

	while (size_rem) {
		i = mic_query_offset(mvdev, offset, &size, &pa);
		if (i < 0)
			return -EINVAL;
		err = remap_pfn_range(vma, vma->vm_start + offset,
			pa >> PAGE_SHIFT, size, vma->vm_page_prot);
		if (err)
			return err;
		dev_dbg(mic_dev(mvdev),
			"%s %d type %d size 0x%lx off 0x%lx pa 0x%lx vma 0x%lx\n",
			__func__, __LINE__, mvdev->virtio_id, size, offset,
			pa, vma->vm_start + offset);
		size_rem -= size;
		offset += size;
	}
	return 0;
}