blob: 957f44aaa97ab7e8e3965dd7011c81d786ad5354 [file] [log] [blame]
Oren Weilab841162011-05-15 13:43:41 +03001/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
Tomas Winkler733ba912012-02-09 19:25:53 +02004 * Copyright (c) 2003-2012, Intel Corporation.
Oren Weilab841162011-05-15 13:43:41 +03005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 */
Oren Weilab841162011-05-15 13:43:41 +030016#include <linux/module.h>
17#include <linux/moduleparam.h>
18#include <linux/kernel.h>
19#include <linux/device.h>
20#include <linux/fs.h>
21#include <linux/errno.h>
22#include <linux/types.h>
23#include <linux/fcntl.h>
24#include <linux/aio.h>
25#include <linux/pci.h>
26#include <linux/poll.h>
27#include <linux/init.h>
28#include <linux/ioctl.h>
29#include <linux/cdev.h>
Oren Weilab841162011-05-15 13:43:41 +030030#include <linux/sched.h>
31#include <linux/uuid.h>
32#include <linux/compat.h>
33#include <linux/jiffies.h>
34#include <linux/interrupt.h>
35
Tomas Winkler4f3afe12012-05-09 16:38:59 +030036#include <linux/mei.h>
Tomas Winkler47a73802012-12-25 19:06:03 +020037
38#include "mei_dev.h"
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020039#include "client.h"
Oren Weilab841162011-05-15 13:43:41 +030040
Oren Weilab841162011-05-15 13:43:41 +030041/**
Oren Weilab841162011-05-15 13:43:41 +030042 * mei_open - the open function
43 *
44 * @inode: pointer to inode structure
45 * @file: pointer to file structure
Alexander Usyskin83ce0742014-01-08 22:31:46 +020046 *
Oren Weilab841162011-05-15 13:43:41 +030047 * returns 0 on success, <0 on error
48 */
49static int mei_open(struct inode *inode, struct file *file)
50{
Oren Weilab841162011-05-15 13:43:41 +030051 struct mei_device *dev;
Alexander Usyskinf3d8e872014-06-23 15:10:35 +030052 struct mei_cl *cl;
Tomas Winkler2703d4b2013-02-06 14:06:39 +020053
Tomas Winkler6f37aca2011-11-13 09:41:15 +020054 int err;
Oren Weilab841162011-05-15 13:43:41 +030055
Alexander Usyskinf3d8e872014-06-23 15:10:35 +030056 dev = container_of(inode->i_cdev, struct mei_device, cdev);
Oren Weil5b881e32011-11-13 09:41:14 +020057 if (!dev)
Tomas Winklere036cc52013-09-16 23:44:46 +030058 return -ENODEV;
Oren Weilab841162011-05-15 13:43:41 +030059
60 mutex_lock(&dev->device_lock);
Tomas Winklere036cc52013-09-16 23:44:46 +030061
62 cl = NULL;
Oren Weilab841162011-05-15 13:43:41 +030063
64 err = -ENODEV;
Tomas Winklerb210d752012-08-07 00:03:56 +030065 if (dev->dev_state != MEI_DEV_ENABLED) {
66 dev_dbg(&dev->pdev->dev, "dev_state != MEI_ENABLED dev_state = %s\n",
67 mei_dev_state_str(dev->dev_state));
Tomas Winklere036cc52013-09-16 23:44:46 +030068 goto err_unlock;
Tomas Winkler1b812942012-09-11 00:43:20 +030069 }
Oren Weilab841162011-05-15 13:43:41 +030070
Tomas Winklere036cc52013-09-16 23:44:46 +030071 err = -ENOMEM;
72 cl = mei_cl_allocate(dev);
73 if (!cl)
74 goto err_unlock;
75
76 /* open_handle_count check is handled in the mei_cl_link */
Tomas Winkler781d0d82013-01-08 23:07:22 +020077 err = mei_cl_link(cl, MEI_HOST_CLIENT_ID_ANY);
78 if (err)
Tomas Winklere036cc52013-09-16 23:44:46 +030079 goto err_unlock;
Oren Weilab841162011-05-15 13:43:41 +030080
81 file->private_data = cl;
Tomas Winklere036cc52013-09-16 23:44:46 +030082
Oren Weilab841162011-05-15 13:43:41 +030083 mutex_unlock(&dev->device_lock);
84
Oren Weil5b881e32011-11-13 09:41:14 +020085 return nonseekable_open(inode, file);
Oren Weilab841162011-05-15 13:43:41 +030086
Tomas Winklere036cc52013-09-16 23:44:46 +030087err_unlock:
Oren Weilab841162011-05-15 13:43:41 +030088 mutex_unlock(&dev->device_lock);
89 kfree(cl);
Oren Weilab841162011-05-15 13:43:41 +030090 return err;
91}
92
93/**
94 * mei_release - the release function
95 *
96 * @inode: pointer to inode structure
97 * @file: pointer to file structure
98 *
99 * returns 0 on success, <0 on error
100 */
101static int mei_release(struct inode *inode, struct file *file)
102{
103 struct mei_cl *cl = file->private_data;
104 struct mei_cl_cb *cb;
105 struct mei_device *dev;
106 int rets = 0;
107
108 if (WARN_ON(!cl || !cl->dev))
109 return -ENODEV;
110
111 dev = cl->dev;
112
113 mutex_lock(&dev->device_lock);
Tomas Winklera562d5c2012-11-11 17:38:01 +0200114 if (cl == &dev->iamthif_cl) {
115 rets = mei_amthif_release(dev, file);
116 goto out;
117 }
118 if (cl->state == MEI_FILE_CONNECTED) {
119 cl->state = MEI_FILE_DISCONNECTING;
Tomas Winkler46922182014-03-16 14:35:55 +0200120 cl_dbg(dev, cl, "disconnecting\n");
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200121 rets = mei_cl_disconnect(cl);
Oren Weilab841162011-05-15 13:43:41 +0300122 }
Tomas Winklera562d5c2012-11-11 17:38:01 +0200123 mei_cl_flush_queues(cl);
Tomas Winkler46922182014-03-16 14:35:55 +0200124 cl_dbg(dev, cl, "removing\n");
Tomas Winklera562d5c2012-11-11 17:38:01 +0200125
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200126 mei_cl_unlink(cl);
Tomas Winklera562d5c2012-11-11 17:38:01 +0200127
Tomas Winkler781d0d82013-01-08 23:07:22 +0200128
Tomas Winklera562d5c2012-11-11 17:38:01 +0200129 /* free read cb */
130 cb = NULL;
131 if (cl->read_cb) {
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200132 cb = mei_cl_find_read_cb(cl);
Tomas Winklera562d5c2012-11-11 17:38:01 +0200133 /* Remove entry from read list */
134 if (cb)
135 list_del(&cb->list);
136
137 cb = cl->read_cb;
138 cl->read_cb = NULL;
139 }
140
141 file->private_data = NULL;
142
Tomas Winklera9c8a172013-09-15 18:11:06 +0300143 mei_io_cb_free(cb);
Tomas Winklera562d5c2012-11-11 17:38:01 +0200144
145 kfree(cl);
146out:
Oren Weilab841162011-05-15 13:43:41 +0300147 mutex_unlock(&dev->device_lock);
148 return rets;
149}
150
151
152/**
153 * mei_read - the read function.
154 *
155 * @file: pointer to file structure
156 * @ubuf: pointer to user buffer
157 * @length: buffer length
158 * @offset: data offset in buffer
159 *
160 * returns >=0 data length on success , <0 on error
161 */
162static ssize_t mei_read(struct file *file, char __user *ubuf,
Tomas Winkler441ab502011-12-13 23:39:34 +0200163 size_t length, loff_t *offset)
Oren Weilab841162011-05-15 13:43:41 +0300164{
165 struct mei_cl *cl = file->private_data;
166 struct mei_cl_cb *cb_pos = NULL;
167 struct mei_cl_cb *cb = NULL;
168 struct mei_device *dev;
Oren Weilab841162011-05-15 13:43:41 +0300169 int rets;
170 int err;
171
172
173 if (WARN_ON(!cl || !cl->dev))
174 return -ENODEV;
175
176 dev = cl->dev;
177
Tomas Winklerdd5de1f2013-09-02 03:11:04 +0300178
Oren Weilab841162011-05-15 13:43:41 +0300179 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +0300180 if (dev->dev_state != MEI_DEV_ENABLED) {
Oren Weilab841162011-05-15 13:43:41 +0300181 rets = -ENODEV;
182 goto out;
183 }
184
Tomas Winklerdd5de1f2013-09-02 03:11:04 +0300185 if (length == 0) {
186 rets = 0;
187 goto out;
188 }
189
Oren Weilab841162011-05-15 13:43:41 +0300190 if (cl == &dev->iamthif_cl) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200191 rets = mei_amthif_read(dev, file, ubuf, length, offset);
Oren Weilab841162011-05-15 13:43:41 +0300192 goto out;
193 }
194
Tomas Winkler139aacf2013-05-29 20:09:30 +0300195 if (cl->read_cb) {
Oren Weilab841162011-05-15 13:43:41 +0300196 cb = cl->read_cb;
Tomas Winkler139aacf2013-05-29 20:09:30 +0300197 /* read what left */
198 if (cb->buf_idx > *offset)
199 goto copy_buffer;
200 /* offset is beyond buf_idx we have no more data return 0 */
201 if (cb->buf_idx > 0 && cb->buf_idx <= *offset) {
202 rets = 0;
203 goto free;
204 }
205 /* Offset needs to be cleaned for contiguous reads*/
206 if (cb->buf_idx == 0 && *offset > 0)
207 *offset = 0;
208 } else if (*offset > 0) {
Oren Weilab841162011-05-15 13:43:41 +0300209 *offset = 0;
Oren Weilab841162011-05-15 13:43:41 +0300210 }
211
Tomas Winklerfcb136e2013-04-19 22:01:35 +0300212 err = mei_cl_read_start(cl, length);
Oren Weilab841162011-05-15 13:43:41 +0300213 if (err && err != -EBUSY) {
214 dev_dbg(&dev->pdev->dev,
215 "mei start read failure with status = %d\n", err);
216 rets = err;
217 goto out;
218 }
219
220 if (MEI_READ_COMPLETE != cl->reading_state &&
221 !waitqueue_active(&cl->rx_wait)) {
222 if (file->f_flags & O_NONBLOCK) {
223 rets = -EAGAIN;
224 goto out;
225 }
226
227 mutex_unlock(&dev->device_lock);
228
229 if (wait_event_interruptible(cl->rx_wait,
Tomas Winklere2b31642013-09-02 13:29:46 +0300230 MEI_READ_COMPLETE == cl->reading_state ||
231 mei_cl_is_transitioning(cl))) {
232
Oren Weilab841162011-05-15 13:43:41 +0300233 if (signal_pending(current))
234 return -EINTR;
235 return -ERESTARTSYS;
236 }
237
238 mutex_lock(&dev->device_lock);
Tomas Winklere2b31642013-09-02 13:29:46 +0300239 if (mei_cl_is_transitioning(cl)) {
Oren Weilab841162011-05-15 13:43:41 +0300240 rets = -EBUSY;
241 goto out;
242 }
243 }
244
245 cb = cl->read_cb;
246
247 if (!cb) {
248 rets = -ENODEV;
249 goto out;
250 }
251 if (cl->reading_state != MEI_READ_COMPLETE) {
252 rets = 0;
253 goto out;
254 }
255 /* now copy the data to user space */
256copy_buffer:
Tomas Winklerfcb136e2013-04-19 22:01:35 +0300257 dev_dbg(&dev->pdev->dev, "buf.size = %d buf.idx= %ld\n",
258 cb->response_buffer.size, cb->buf_idx);
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200259 if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) {
Oren Weilab841162011-05-15 13:43:41 +0300260 rets = -EMSGSIZE;
261 goto free;
262 }
263
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200264 /* length is being truncated to PAGE_SIZE,
265 * however buf_idx may point beyond that */
266 length = min_t(size_t, length, cb->buf_idx - *offset);
Oren Weilab841162011-05-15 13:43:41 +0300267
Tomas Winkler441ab502011-12-13 23:39:34 +0200268 if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) {
Alexander Usyskindbac4742014-03-12 13:19:13 +0200269 dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
Oren Weilab841162011-05-15 13:43:41 +0300270 rets = -EFAULT;
271 goto free;
272 }
273
274 rets = length;
275 *offset += length;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200276 if ((unsigned long)*offset < cb->buf_idx)
Oren Weilab841162011-05-15 13:43:41 +0300277 goto out;
278
279free:
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200280 cb_pos = mei_cl_find_read_cb(cl);
Oren Weilab841162011-05-15 13:43:41 +0300281 /* Remove entry from read list */
282 if (cb_pos)
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200283 list_del(&cb_pos->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200284 mei_io_cb_free(cb);
Oren Weilab841162011-05-15 13:43:41 +0300285 cl->reading_state = MEI_IDLE;
286 cl->read_cb = NULL;
Oren Weilab841162011-05-15 13:43:41 +0300287out:
288 dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets);
289 mutex_unlock(&dev->device_lock);
290 return rets;
291}
Tomas Winkler33d28c92012-10-09 16:50:17 +0200292/**
Oren Weilab841162011-05-15 13:43:41 +0300293 * mei_write - the write function.
294 *
295 * @file: pointer to file structure
296 * @ubuf: pointer to user buffer
297 * @length: buffer length
298 * @offset: data offset in buffer
299 *
300 * returns >=0 data length on success , <0 on error
301 */
302static ssize_t mei_write(struct file *file, const char __user *ubuf,
Tomas Winkler441ab502011-12-13 23:39:34 +0200303 size_t length, loff_t *offset)
Oren Weilab841162011-05-15 13:43:41 +0300304{
305 struct mei_cl *cl = file->private_data;
Tomas Winklerd3208322014-08-24 12:08:55 +0300306 struct mei_me_client *me_cl;
Oren Weilab841162011-05-15 13:43:41 +0300307 struct mei_cl_cb *write_cb = NULL;
Oren Weilab841162011-05-15 13:43:41 +0300308 struct mei_device *dev;
309 unsigned long timeout = 0;
310 int rets;
Oren Weilab841162011-05-15 13:43:41 +0300311
312 if (WARN_ON(!cl || !cl->dev))
313 return -ENODEV;
314
315 dev = cl->dev;
316
317 mutex_lock(&dev->device_lock);
318
Tomas Winklerb210d752012-08-07 00:03:56 +0300319 if (dev->dev_state != MEI_DEV_ENABLED) {
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200320 rets = -ENODEV;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300321 goto out;
Oren Weilab841162011-05-15 13:43:41 +0300322 }
323
Tomas Winklerd880f322014-08-21 14:29:15 +0300324 me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id);
Tomas Winklerd3208322014-08-24 12:08:55 +0300325 if (!me_cl) {
Alexander Usyskin7ca96aa2014-02-19 17:35:49 +0200326 rets = -ENOTTY;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300327 goto out;
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200328 }
Tomas Winklerdd5de1f2013-09-02 03:11:04 +0300329
330 if (length == 0) {
331 rets = 0;
332 goto out;
333 }
334
Tomas Winklerd3208322014-08-24 12:08:55 +0300335 if (length > me_cl->props.max_msg_length) {
Tomas Winklerdd5de1f2013-09-02 03:11:04 +0300336 rets = -EFBIG;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300337 goto out;
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200338 }
339
340 if (cl->state != MEI_FILE_CONNECTED) {
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200341 dev_err(&dev->pdev->dev, "host client = %d, is not connected to ME client = %d",
342 cl->host_client_id, cl->me_client_id);
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300343 rets = -ENODEV;
344 goto out;
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200345 }
Oren Weilab841162011-05-15 13:43:41 +0300346 if (cl == &dev->iamthif_cl) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200347 write_cb = mei_amthif_find_read_list_entry(dev, file);
Oren Weilab841162011-05-15 13:43:41 +0300348
349 if (write_cb) {
350 timeout = write_cb->read_time +
Tomas Winkler3870c322012-11-01 21:17:14 +0200351 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
Oren Weilab841162011-05-15 13:43:41 +0300352
353 if (time_after(jiffies, timeout) ||
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200354 cl->reading_state == MEI_READ_COMPLETE) {
355 *offset = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200356 list_del(&write_cb->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200357 mei_io_cb_free(write_cb);
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200358 write_cb = NULL;
Oren Weilab841162011-05-15 13:43:41 +0300359 }
360 }
361 }
362
363 /* free entry used in read */
364 if (cl->reading_state == MEI_READ_COMPLETE) {
365 *offset = 0;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200366 write_cb = mei_cl_find_read_cb(cl);
Oren Weilab841162011-05-15 13:43:41 +0300367 if (write_cb) {
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200368 list_del(&write_cb->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200369 mei_io_cb_free(write_cb);
Oren Weilab841162011-05-15 13:43:41 +0300370 write_cb = NULL;
371 cl->reading_state = MEI_IDLE;
372 cl->read_cb = NULL;
Oren Weilab841162011-05-15 13:43:41 +0300373 }
Tomas Winklerd91aaed2013-01-08 23:07:18 +0200374 } else if (cl->reading_state == MEI_IDLE)
Oren Weilab841162011-05-15 13:43:41 +0300375 *offset = 0;
376
377
Tomas Winkler33d28c92012-10-09 16:50:17 +0200378 write_cb = mei_io_cb_init(cl, file);
Oren Weilab841162011-05-15 13:43:41 +0300379 if (!write_cb) {
Tomas Winkler33d28c92012-10-09 16:50:17 +0200380 dev_err(&dev->pdev->dev, "write cb allocation failed\n");
381 rets = -ENOMEM;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300382 goto out;
Oren Weilab841162011-05-15 13:43:41 +0300383 }
Tomas Winkler33d28c92012-10-09 16:50:17 +0200384 rets = mei_io_cb_alloc_req_buf(write_cb, length);
385 if (rets)
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300386 goto out;
Oren Weilab841162011-05-15 13:43:41 +0300387
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200388 rets = copy_from_user(write_cb->request_buffer.data, ubuf, length);
Alexander Usyskind8b29ef2013-09-02 03:11:02 +0300389 if (rets) {
Alexander Usyskindbac4742014-03-12 13:19:13 +0200390 dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n");
Alexander Usyskind8b29ef2013-09-02 03:11:02 +0300391 rets = -EFAULT;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300392 goto out;
Alexander Usyskind8b29ef2013-09-02 03:11:02 +0300393 }
Oren Weilab841162011-05-15 13:43:41 +0300394
Oren Weilab841162011-05-15 13:43:41 +0300395 if (cl == &dev->iamthif_cl) {
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200396 rets = mei_amthif_write(dev, write_cb);
397
398 if (rets) {
399 dev_err(&dev->pdev->dev,
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200400 "amthif write failed with status = %d\n", rets);
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300401 goto out;
Oren Weilab841162011-05-15 13:43:41 +0300402 }
403 mutex_unlock(&dev->device_lock);
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200404 return length;
Oren Weilab841162011-05-15 13:43:41 +0300405 }
406
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300407 rets = mei_cl_write(cl, write_cb, false);
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200408out:
Oren Weilab841162011-05-15 13:43:41 +0300409 mutex_unlock(&dev->device_lock);
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300410 if (rets < 0)
411 mei_io_cb_free(write_cb);
Oren Weilab841162011-05-15 13:43:41 +0300412 return rets;
413}
414
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200415/**
416 * mei_ioctl_connect_client - the connect to fw client IOCTL function
417 *
418 * @dev: the device structure
419 * @data: IOCTL connect data, input and output parameters
420 * @file: private data of the file object
421 *
422 * Locking: called under "dev->device_lock" lock
423 *
424 * returns 0 on success, <0 on failure.
425 */
426static int mei_ioctl_connect_client(struct file *file,
427 struct mei_connect_client_data *data)
428{
429 struct mei_device *dev;
430 struct mei_client *client;
Tomas Winklerd3208322014-08-24 12:08:55 +0300431 struct mei_me_client *me_cl;
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200432 struct mei_cl *cl;
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200433 int rets;
434
435 cl = file->private_data;
436 if (WARN_ON(!cl || !cl->dev))
437 return -ENODEV;
438
439 dev = cl->dev;
440
441 if (dev->dev_state != MEI_DEV_ENABLED) {
442 rets = -ENODEV;
443 goto end;
444 }
445
446 if (cl->state != MEI_FILE_INITIALIZING &&
447 cl->state != MEI_FILE_DISCONNECTED) {
448 rets = -EBUSY;
449 goto end;
450 }
451
452 /* find ME client we're trying to connect to */
Tomas Winklerd3208322014-08-24 12:08:55 +0300453 me_cl = mei_me_cl_by_uuid(dev, &data->in_client_uuid);
454 if (!me_cl || me_cl->props.fixed_address) {
Tomas Winkler80fe6362013-05-07 21:12:31 +0300455 dev_dbg(&dev->pdev->dev, "Cannot connect to FW Client UUID = %pUl\n",
456 &data->in_client_uuid);
Alexander Usyskin285e2992014-02-17 15:13:20 +0200457 rets = -ENOTTY;
Tomas Winkler80fe6362013-05-07 21:12:31 +0300458 goto end;
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200459 }
460
Tomas Winklerd3208322014-08-24 12:08:55 +0300461 cl->me_client_id = me_cl->client_id;
Tomas Winklerd880f322014-08-21 14:29:15 +0300462 cl->cl_uuid = me_cl->props.protocol_name;
Tomas Winkler80fe6362013-05-07 21:12:31 +0300463
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200464 dev_dbg(&dev->pdev->dev, "Connect to FW Client ID = %d\n",
465 cl->me_client_id);
466 dev_dbg(&dev->pdev->dev, "FW Client - Protocol Version = %d\n",
Tomas Winklerd3208322014-08-24 12:08:55 +0300467 me_cl->props.protocol_version);
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200468 dev_dbg(&dev->pdev->dev, "FW Client - Max Msg Len = %d\n",
Tomas Winklerd3208322014-08-24 12:08:55 +0300469 me_cl->props.max_msg_length);
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200470
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200471 /* if we're connecting to amthif client then we will use the
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200472 * existing connection
473 */
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200474 if (uuid_le_cmp(data->in_client_uuid, mei_amthif_guid) == 0) {
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200475 dev_dbg(&dev->pdev->dev, "FW Client is amthi\n");
476 if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) {
477 rets = -ENODEV;
478 goto end;
479 }
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200480 mei_cl_unlink(cl);
481
482 kfree(cl);
483 cl = NULL;
Tomas Winkler22f96a02013-09-16 23:44:47 +0300484 dev->iamthif_open_count++;
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200485 file->private_data = &dev->iamthif_cl;
486
487 client = &data->out_client_properties;
Tomas Winklerd3208322014-08-24 12:08:55 +0300488 client->max_msg_length = me_cl->props.max_msg_length;
489 client->protocol_version = me_cl->props.protocol_version;
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200490 rets = dev->iamthif_cl.status;
491
492 goto end;
493 }
494
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200495
496 /* prepare the output buffer */
497 client = &data->out_client_properties;
Tomas Winklerd3208322014-08-24 12:08:55 +0300498 client->max_msg_length = me_cl->props.max_msg_length;
499 client->protocol_version = me_cl->props.protocol_version;
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200500 dev_dbg(&dev->pdev->dev, "Can connect?\n");
501
502
503 rets = mei_cl_connect(cl, file);
504
505end:
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200506 return rets;
507}
508
Oren Weilab841162011-05-15 13:43:41 +0300509
510/**
511 * mei_ioctl - the IOCTL function
512 *
513 * @file: pointer to file structure
514 * @cmd: ioctl command
515 * @data: pointer to mei message structure
516 *
517 * returns 0 on success , <0 on error
518 */
519static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
520{
521 struct mei_device *dev;
522 struct mei_cl *cl = file->private_data;
523 struct mei_connect_client_data *connect_data = NULL;
524 int rets;
525
526 if (cmd != IOCTL_MEI_CONNECT_CLIENT)
527 return -EINVAL;
528
529 if (WARN_ON(!cl || !cl->dev))
530 return -ENODEV;
531
532 dev = cl->dev;
533
534 dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd);
535
536 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +0300537 if (dev->dev_state != MEI_DEV_ENABLED) {
Oren Weilab841162011-05-15 13:43:41 +0300538 rets = -ENODEV;
539 goto out;
540 }
541
542 dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
543
544 connect_data = kzalloc(sizeof(struct mei_connect_client_data),
545 GFP_KERNEL);
546 if (!connect_data) {
547 rets = -ENOMEM;
548 goto out;
549 }
550 dev_dbg(&dev->pdev->dev, "copy connect data from user\n");
551 if (copy_from_user(connect_data, (char __user *)data,
552 sizeof(struct mei_connect_client_data))) {
Alexander Usyskindbac4742014-03-12 13:19:13 +0200553 dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n");
Oren Weilab841162011-05-15 13:43:41 +0300554 rets = -EFAULT;
555 goto out;
556 }
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200557
Oren Weilab841162011-05-15 13:43:41 +0300558 rets = mei_ioctl_connect_client(file, connect_data);
559
560 /* if all is ok, copying the data back to user. */
561 if (rets)
562 goto out;
563
564 dev_dbg(&dev->pdev->dev, "copy connect data to user\n");
565 if (copy_to_user((char __user *)data, connect_data,
566 sizeof(struct mei_connect_client_data))) {
567 dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
568 rets = -EFAULT;
569 goto out;
570 }
571
572out:
573 kfree(connect_data);
574 mutex_unlock(&dev->device_lock);
575 return rets;
576}
577
578/**
579 * mei_compat_ioctl - the compat IOCTL function
580 *
581 * @file: pointer to file structure
582 * @cmd: ioctl command
583 * @data: pointer to mei message structure
584 *
585 * returns 0 on success , <0 on error
586 */
587#ifdef CONFIG_COMPAT
588static long mei_compat_ioctl(struct file *file,
Tomas Winkler441ab502011-12-13 23:39:34 +0200589 unsigned int cmd, unsigned long data)
Oren Weilab841162011-05-15 13:43:41 +0300590{
591 return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
592}
593#endif
594
595
596/**
597 * mei_poll - the poll function
598 *
599 * @file: pointer to file structure
600 * @wait: pointer to poll_table structure
601 *
602 * returns poll mask
603 */
604static unsigned int mei_poll(struct file *file, poll_table *wait)
605{
606 struct mei_cl *cl = file->private_data;
607 struct mei_device *dev;
608 unsigned int mask = 0;
609
610 if (WARN_ON(!cl || !cl->dev))
Tomas Winklerb950ac12013-07-25 20:15:53 +0300611 return POLLERR;
Oren Weilab841162011-05-15 13:43:41 +0300612
613 dev = cl->dev;
614
615 mutex_lock(&dev->device_lock);
616
Tomas Winklerb950ac12013-07-25 20:15:53 +0300617 if (!mei_cl_is_connected(cl)) {
618 mask = POLLERR;
Oren Weilab841162011-05-15 13:43:41 +0300619 goto out;
620 }
621
622 mutex_unlock(&dev->device_lock);
Tomas Winklerb950ac12013-07-25 20:15:53 +0300623
624
625 if (cl == &dev->iamthif_cl)
626 return mei_amthif_poll(dev, file, wait);
627
Oren Weilab841162011-05-15 13:43:41 +0300628 poll_wait(file, &cl->tx_wait, wait);
Tomas Winklerb950ac12013-07-25 20:15:53 +0300629
Oren Weilab841162011-05-15 13:43:41 +0300630 mutex_lock(&dev->device_lock);
Tomas Winklerb950ac12013-07-25 20:15:53 +0300631
632 if (!mei_cl_is_connected(cl)) {
633 mask = POLLERR;
634 goto out;
635 }
636
Alexander Usyskin34ec4362014-04-01 23:50:41 +0300637 mask |= (POLLIN | POLLRDNORM);
Oren Weilab841162011-05-15 13:43:41 +0300638
639out:
640 mutex_unlock(&dev->device_lock);
641 return mask;
642}
643
Oren Weil5b881e32011-11-13 09:41:14 +0200644/*
645 * file operations structure will be used for mei char device.
646 */
647static const struct file_operations mei_fops = {
648 .owner = THIS_MODULE,
649 .read = mei_read,
650 .unlocked_ioctl = mei_ioctl,
651#ifdef CONFIG_COMPAT
652 .compat_ioctl = mei_compat_ioctl,
653#endif
654 .open = mei_open,
655 .release = mei_release,
656 .write = mei_write,
657 .poll = mei_poll,
658 .llseek = no_llseek
659};
660
Alexander Usyskinf3d8e872014-06-23 15:10:35 +0300661static struct class *mei_class;
662static dev_t mei_devt;
663#define MEI_MAX_DEVS MINORMASK
664static DEFINE_MUTEX(mei_minor_lock);
665static DEFINE_IDR(mei_idr);
666
667/**
668 * mei_minor_get - obtain next free device minor number
669 *
670 * @dev: device pointer
671 *
672 * returns allocated minor, or -ENOSPC if no free minor left
Oren Weil5b881e32011-11-13 09:41:14 +0200673 */
Alexander Usyskinf3d8e872014-06-23 15:10:35 +0300674static int mei_minor_get(struct mei_device *dev)
Tomas Winkler9a123f12012-08-06 15:23:55 +0300675{
Tomas Winkler30e53bb2013-04-05 22:10:34 +0300676 int ret;
Alexander Usyskinf3d8e872014-06-23 15:10:35 +0300677
678 mutex_lock(&mei_minor_lock);
679 ret = idr_alloc(&mei_idr, dev, 0, MEI_MAX_DEVS, GFP_KERNEL);
680 if (ret >= 0)
681 dev->minor = ret;
682 else if (ret == -ENOSPC)
683 dev_err(&dev->pdev->dev, "too many mei devices\n");
684
685 mutex_unlock(&mei_minor_lock);
686 return ret;
687}
688
689/**
690 * mei_minor_free - mark device minor number as free
691 *
692 * @dev: device pointer
693 */
694static void mei_minor_free(struct mei_device *dev)
695{
696 mutex_lock(&mei_minor_lock);
697 idr_remove(&mei_idr, dev->minor);
698 mutex_unlock(&mei_minor_lock);
699}
700
701int mei_register(struct mei_device *dev, struct device *parent)
702{
703 struct device *clsdev; /* class device */
704 int ret, devno;
705
706 ret = mei_minor_get(dev);
707 if (ret < 0)
Tomas Winkler30e53bb2013-04-05 22:10:34 +0300708 return ret;
709
Alexander Usyskinf3d8e872014-06-23 15:10:35 +0300710 /* Fill in the data structures */
711 devno = MKDEV(MAJOR(mei_devt), dev->minor);
712 cdev_init(&dev->cdev, &mei_fops);
713 dev->cdev.owner = mei_fops.owner;
714
715 /* Add the device */
716 ret = cdev_add(&dev->cdev, devno, 1);
717 if (ret) {
718 dev_err(parent, "unable to add device %d:%d\n",
719 MAJOR(mei_devt), dev->minor);
720 goto err_dev_add;
721 }
722
723 clsdev = device_create(mei_class, parent, devno,
724 NULL, "mei%d", dev->minor);
725
726 if (IS_ERR(clsdev)) {
727 dev_err(parent, "unable to create device %d:%d\n",
728 MAJOR(mei_devt), dev->minor);
729 ret = PTR_ERR(clsdev);
730 goto err_dev_create;
731 }
732
733 ret = mei_dbgfs_register(dev, dev_name(clsdev));
734 if (ret) {
735 dev_err(clsdev, "cannot register debugfs ret = %d\n", ret);
736 goto err_dev_dbgfs;
737 }
Tomas Winkler30e53bb2013-04-05 22:10:34 +0300738
739 return 0;
Alexander Usyskinf3d8e872014-06-23 15:10:35 +0300740
741err_dev_dbgfs:
742 device_destroy(mei_class, devno);
743err_dev_create:
744 cdev_del(&dev->cdev);
745err_dev_add:
746 mei_minor_free(dev);
747 return ret;
Oren Weil5b881e32011-11-13 09:41:14 +0200748}
Tomas Winkler40e0b672013-03-27 16:58:30 +0200749EXPORT_SYMBOL_GPL(mei_register);
Oren Weil5b881e32011-11-13 09:41:14 +0200750
Tomas Winkler30e53bb2013-04-05 22:10:34 +0300751void mei_deregister(struct mei_device *dev)
Oren Weil5b881e32011-11-13 09:41:14 +0200752{
Alexander Usyskinf3d8e872014-06-23 15:10:35 +0300753 int devno;
754
755 devno = dev->cdev.dev;
756 cdev_del(&dev->cdev);
757
Tomas Winkler30e53bb2013-04-05 22:10:34 +0300758 mei_dbgfs_deregister(dev);
Alexander Usyskinf3d8e872014-06-23 15:10:35 +0300759
760 device_destroy(mei_class, devno);
761
762 mei_minor_free(dev);
Oren Weilab841162011-05-15 13:43:41 +0300763}
Tomas Winkler40e0b672013-03-27 16:58:30 +0200764EXPORT_SYMBOL_GPL(mei_deregister);
Oren Weilab841162011-05-15 13:43:41 +0300765
Samuel Ortizcf3baef2013-03-27 17:29:57 +0200766static int __init mei_init(void)
767{
Alexander Usyskinf3d8e872014-06-23 15:10:35 +0300768 int ret;
769
770 mei_class = class_create(THIS_MODULE, "mei");
771 if (IS_ERR(mei_class)) {
772 pr_err("couldn't create class\n");
773 ret = PTR_ERR(mei_class);
774 goto err;
775 }
776
777 ret = alloc_chrdev_region(&mei_devt, 0, MEI_MAX_DEVS, "mei");
778 if (ret < 0) {
779 pr_err("unable to allocate char dev region\n");
780 goto err_class;
781 }
782
783 ret = mei_cl_bus_init();
784 if (ret < 0) {
785 pr_err("unable to initialize bus\n");
786 goto err_chrdev;
787 }
788
789 return 0;
790
791err_chrdev:
792 unregister_chrdev_region(mei_devt, MEI_MAX_DEVS);
793err_class:
794 class_destroy(mei_class);
795err:
796 return ret;
Samuel Ortizcf3baef2013-03-27 17:29:57 +0200797}
798
799static void __exit mei_exit(void)
800{
Alexander Usyskinf3d8e872014-06-23 15:10:35 +0300801 unregister_chrdev_region(mei_devt, MEI_MAX_DEVS);
802 class_destroy(mei_class);
Samuel Ortizcf3baef2013-03-27 17:29:57 +0200803 mei_cl_bus_exit();
804}
805
806module_init(mei_init);
807module_exit(mei_exit);
808
Tomas Winkler40e0b672013-03-27 16:58:30 +0200809MODULE_AUTHOR("Intel Corporation");
810MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
Tomas Winkler827eef52013-02-06 14:06:41 +0200811MODULE_LICENSE("GPL v2");
Oren Weilab841162011-05-15 13:43:41 +0300812