blob: 5e11b5b9b65d2b9096164691f7feaf3a54c00ffc [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 */
16
Tomas Winkler2f3d2b42012-03-19 22:38:13 +020017#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
Oren Weilab841162011-05-15 13:43:41 +030019#include <linux/module.h>
20#include <linux/moduleparam.h>
21#include <linux/kernel.h>
22#include <linux/device.h>
23#include <linux/fs.h>
24#include <linux/errno.h>
25#include <linux/types.h>
26#include <linux/fcntl.h>
27#include <linux/aio.h>
28#include <linux/pci.h>
29#include <linux/poll.h>
30#include <linux/init.h>
31#include <linux/ioctl.h>
32#include <linux/cdev.h>
Oren Weilab841162011-05-15 13:43:41 +030033#include <linux/sched.h>
34#include <linux/uuid.h>
35#include <linux/compat.h>
36#include <linux/jiffies.h>
37#include <linux/interrupt.h>
Oren Weil5b881e32011-11-13 09:41:14 +020038#include <linux/miscdevice.h>
Oren Weilab841162011-05-15 13:43:41 +030039
Tomas Winkler4f3afe12012-05-09 16:38:59 +030040#include <linux/mei.h>
Tomas Winkler47a73802012-12-25 19:06:03 +020041
42#include "mei_dev.h"
Tomas Winkler9dc64d62013-01-08 23:07:17 +020043#include "hw-me.h"
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020044#include "client.h"
Oren Weilab841162011-05-15 13:43:41 +030045
Oren Weilab841162011-05-15 13:43:41 +030046/**
Oren Weilab841162011-05-15 13:43:41 +030047 * mei_open - the open function
48 *
49 * @inode: pointer to inode structure
50 * @file: pointer to file structure
Tomas Winkler9b0d5ef2013-04-18 23:03:48 +030051 e
Oren Weilab841162011-05-15 13:43:41 +030052 * returns 0 on success, <0 on error
53 */
54static int mei_open(struct inode *inode, struct file *file)
55{
Tomas Winkler2703d4b2013-02-06 14:06:39 +020056 struct miscdevice *misc = file->private_data;
57 struct pci_dev *pdev;
Oren Weilab841162011-05-15 13:43:41 +030058 struct mei_cl *cl;
Oren Weilab841162011-05-15 13:43:41 +030059 struct mei_device *dev;
Tomas Winkler2703d4b2013-02-06 14:06:39 +020060
Tomas Winkler6f37aca2011-11-13 09:41:15 +020061 int err;
Oren Weilab841162011-05-15 13:43:41 +030062
63 err = -ENODEV;
Tomas Winkler2703d4b2013-02-06 14:06:39 +020064 if (!misc->parent)
Oren Weilab841162011-05-15 13:43:41 +030065 goto out;
66
Tomas Winkler2703d4b2013-02-06 14:06:39 +020067 pdev = container_of(misc->parent, struct pci_dev, dev);
68
69 dev = pci_get_drvdata(pdev);
Oren Weil5b881e32011-11-13 09:41:14 +020070 if (!dev)
Oren Weilab841162011-05-15 13:43:41 +030071 goto out;
72
73 mutex_lock(&dev->device_lock);
74 err = -ENOMEM;
Tomas Winklerc95efb72011-05-25 17:28:21 +030075 cl = mei_cl_allocate(dev);
Oren Weilab841162011-05-15 13:43:41 +030076 if (!cl)
Alexey Khoroshilov303dfbf2011-08-31 00:41:14 +040077 goto out_unlock;
Oren Weilab841162011-05-15 13:43:41 +030078
79 err = -ENODEV;
Tomas Winklerb210d752012-08-07 00:03:56 +030080 if (dev->dev_state != MEI_DEV_ENABLED) {
81 dev_dbg(&dev->pdev->dev, "dev_state != MEI_ENABLED dev_state = %s\n",
82 mei_dev_state_str(dev->dev_state));
Oren Weilab841162011-05-15 13:43:41 +030083 goto out_unlock;
84 }
85 err = -EMFILE;
Tomas Winkler1b812942012-09-11 00:43:20 +030086 if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
87 dev_err(&dev->pdev->dev, "open_handle_count exceded %d",
88 MEI_MAX_OPEN_HANDLE_COUNT);
Oren Weilab841162011-05-15 13:43:41 +030089 goto out_unlock;
Tomas Winkler1b812942012-09-11 00:43:20 +030090 }
Oren Weilab841162011-05-15 13:43:41 +030091
Tomas Winkler781d0d82013-01-08 23:07:22 +020092 err = mei_cl_link(cl, MEI_HOST_CLIENT_ID_ANY);
93 if (err)
Oren Weilab841162011-05-15 13:43:41 +030094 goto out_unlock;
Oren Weilab841162011-05-15 13:43:41 +030095
96 file->private_data = cl;
97 mutex_unlock(&dev->device_lock);
98
Oren Weil5b881e32011-11-13 09:41:14 +020099 return nonseekable_open(inode, file);
Oren Weilab841162011-05-15 13:43:41 +0300100
101out_unlock:
102 mutex_unlock(&dev->device_lock);
103 kfree(cl);
104out:
105 return err;
106}
107
108/**
109 * mei_release - the release function
110 *
111 * @inode: pointer to inode structure
112 * @file: pointer to file structure
113 *
114 * returns 0 on success, <0 on error
115 */
116static int mei_release(struct inode *inode, struct file *file)
117{
118 struct mei_cl *cl = file->private_data;
119 struct mei_cl_cb *cb;
120 struct mei_device *dev;
121 int rets = 0;
122
123 if (WARN_ON(!cl || !cl->dev))
124 return -ENODEV;
125
126 dev = cl->dev;
127
128 mutex_lock(&dev->device_lock);
Tomas Winklera562d5c2012-11-11 17:38:01 +0200129 if (cl == &dev->iamthif_cl) {
130 rets = mei_amthif_release(dev, file);
131 goto out;
132 }
133 if (cl->state == MEI_FILE_CONNECTED) {
134 cl->state = MEI_FILE_DISCONNECTING;
135 dev_dbg(&dev->pdev->dev,
136 "disconnecting client host client = %d, "
137 "ME client = %d\n",
Oren Weilab841162011-05-15 13:43:41 +0300138 cl->host_client_id,
139 cl->me_client_id);
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200140 rets = mei_cl_disconnect(cl);
Oren Weilab841162011-05-15 13:43:41 +0300141 }
Tomas Winklera562d5c2012-11-11 17:38:01 +0200142 mei_cl_flush_queues(cl);
143 dev_dbg(&dev->pdev->dev, "remove client host client = %d, ME client = %d\n",
144 cl->host_client_id,
145 cl->me_client_id);
146
147 if (dev->open_handle_count > 0) {
148 clear_bit(cl->host_client_id, dev->host_clients_map);
149 dev->open_handle_count--;
150 }
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200151 mei_cl_unlink(cl);
Tomas Winklera562d5c2012-11-11 17:38:01 +0200152
Tomas Winkler781d0d82013-01-08 23:07:22 +0200153
Tomas Winklera562d5c2012-11-11 17:38:01 +0200154 /* free read cb */
155 cb = NULL;
156 if (cl->read_cb) {
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200157 cb = mei_cl_find_read_cb(cl);
Tomas Winklera562d5c2012-11-11 17:38:01 +0200158 /* Remove entry from read list */
159 if (cb)
160 list_del(&cb->list);
161
162 cb = cl->read_cb;
163 cl->read_cb = NULL;
164 }
165
166 file->private_data = NULL;
167
168 if (cb) {
169 mei_io_cb_free(cb);
170 cb = NULL;
171 }
172
173 kfree(cl);
174out:
Oren Weilab841162011-05-15 13:43:41 +0300175 mutex_unlock(&dev->device_lock);
176 return rets;
177}
178
179
180/**
181 * mei_read - the read function.
182 *
183 * @file: pointer to file structure
184 * @ubuf: pointer to user buffer
185 * @length: buffer length
186 * @offset: data offset in buffer
187 *
188 * returns >=0 data length on success , <0 on error
189 */
190static ssize_t mei_read(struct file *file, char __user *ubuf,
Tomas Winkler441ab502011-12-13 23:39:34 +0200191 size_t length, loff_t *offset)
Oren Weilab841162011-05-15 13:43:41 +0300192{
193 struct mei_cl *cl = file->private_data;
194 struct mei_cl_cb *cb_pos = NULL;
195 struct mei_cl_cb *cb = NULL;
196 struct mei_device *dev;
Oren Weilab841162011-05-15 13:43:41 +0300197 int rets;
198 int err;
199
200
201 if (WARN_ON(!cl || !cl->dev))
202 return -ENODEV;
203
204 dev = cl->dev;
205
206 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +0300207 if (dev->dev_state != MEI_DEV_ENABLED) {
Oren Weilab841162011-05-15 13:43:41 +0300208 rets = -ENODEV;
209 goto out;
210 }
211
Oren Weilab841162011-05-15 13:43:41 +0300212 if (cl == &dev->iamthif_cl) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200213 rets = mei_amthif_read(dev, file, ubuf, length, offset);
Oren Weilab841162011-05-15 13:43:41 +0300214 goto out;
215 }
216
Tomas Winkler139aacf2013-05-29 20:09:30 +0300217 if (cl->read_cb) {
Oren Weilab841162011-05-15 13:43:41 +0300218 cb = cl->read_cb;
Tomas Winkler139aacf2013-05-29 20:09:30 +0300219 /* read what left */
220 if (cb->buf_idx > *offset)
221 goto copy_buffer;
222 /* offset is beyond buf_idx we have no more data return 0 */
223 if (cb->buf_idx > 0 && cb->buf_idx <= *offset) {
224 rets = 0;
225 goto free;
226 }
227 /* Offset needs to be cleaned for contiguous reads*/
228 if (cb->buf_idx == 0 && *offset > 0)
229 *offset = 0;
230 } else if (*offset > 0) {
Oren Weilab841162011-05-15 13:43:41 +0300231 *offset = 0;
Oren Weilab841162011-05-15 13:43:41 +0300232 }
233
Tomas Winklerfcb136e2013-04-19 22:01:35 +0300234 err = mei_cl_read_start(cl, length);
Oren Weilab841162011-05-15 13:43:41 +0300235 if (err && err != -EBUSY) {
236 dev_dbg(&dev->pdev->dev,
237 "mei start read failure with status = %d\n", err);
238 rets = err;
239 goto out;
240 }
241
242 if (MEI_READ_COMPLETE != cl->reading_state &&
243 !waitqueue_active(&cl->rx_wait)) {
244 if (file->f_flags & O_NONBLOCK) {
245 rets = -EAGAIN;
246 goto out;
247 }
248
249 mutex_unlock(&dev->device_lock);
250
251 if (wait_event_interruptible(cl->rx_wait,
252 (MEI_READ_COMPLETE == cl->reading_state ||
253 MEI_FILE_INITIALIZING == cl->state ||
254 MEI_FILE_DISCONNECTED == cl->state ||
255 MEI_FILE_DISCONNECTING == cl->state))) {
256 if (signal_pending(current))
257 return -EINTR;
258 return -ERESTARTSYS;
259 }
260
261 mutex_lock(&dev->device_lock);
262 if (MEI_FILE_INITIALIZING == cl->state ||
263 MEI_FILE_DISCONNECTED == cl->state ||
264 MEI_FILE_DISCONNECTING == cl->state) {
265 rets = -EBUSY;
266 goto out;
267 }
268 }
269
270 cb = cl->read_cb;
271
272 if (!cb) {
273 rets = -ENODEV;
274 goto out;
275 }
276 if (cl->reading_state != MEI_READ_COMPLETE) {
277 rets = 0;
278 goto out;
279 }
280 /* now copy the data to user space */
281copy_buffer:
Tomas Winklerfcb136e2013-04-19 22:01:35 +0300282 dev_dbg(&dev->pdev->dev, "buf.size = %d buf.idx= %ld\n",
283 cb->response_buffer.size, cb->buf_idx);
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200284 if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) {
Oren Weilab841162011-05-15 13:43:41 +0300285 rets = -EMSGSIZE;
286 goto free;
287 }
288
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200289 /* length is being truncated to PAGE_SIZE,
290 * however buf_idx may point beyond that */
291 length = min_t(size_t, length, cb->buf_idx - *offset);
Oren Weilab841162011-05-15 13:43:41 +0300292
Tomas Winkler441ab502011-12-13 23:39:34 +0200293 if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) {
Oren Weilab841162011-05-15 13:43:41 +0300294 rets = -EFAULT;
295 goto free;
296 }
297
298 rets = length;
299 *offset += length;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200300 if ((unsigned long)*offset < cb->buf_idx)
Oren Weilab841162011-05-15 13:43:41 +0300301 goto out;
302
303free:
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200304 cb_pos = mei_cl_find_read_cb(cl);
Oren Weilab841162011-05-15 13:43:41 +0300305 /* Remove entry from read list */
306 if (cb_pos)
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200307 list_del(&cb_pos->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200308 mei_io_cb_free(cb);
Oren Weilab841162011-05-15 13:43:41 +0300309 cl->reading_state = MEI_IDLE;
310 cl->read_cb = NULL;
Oren Weilab841162011-05-15 13:43:41 +0300311out:
312 dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets);
313 mutex_unlock(&dev->device_lock);
314 return rets;
315}
Tomas Winkler33d28c92012-10-09 16:50:17 +0200316/**
Oren Weilab841162011-05-15 13:43:41 +0300317 * mei_write - the write function.
318 *
319 * @file: pointer to file structure
320 * @ubuf: pointer to user buffer
321 * @length: buffer length
322 * @offset: data offset in buffer
323 *
324 * returns >=0 data length on success , <0 on error
325 */
326static ssize_t mei_write(struct file *file, const char __user *ubuf,
Tomas Winkler441ab502011-12-13 23:39:34 +0200327 size_t length, loff_t *offset)
Oren Weilab841162011-05-15 13:43:41 +0300328{
329 struct mei_cl *cl = file->private_data;
330 struct mei_cl_cb *write_cb = NULL;
Oren Weilab841162011-05-15 13:43:41 +0300331 struct mei_device *dev;
332 unsigned long timeout = 0;
333 int rets;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300334 int id;
Oren Weilab841162011-05-15 13:43:41 +0300335
336 if (WARN_ON(!cl || !cl->dev))
337 return -ENODEV;
338
339 dev = cl->dev;
340
341 mutex_lock(&dev->device_lock);
342
Tomas Winklerb210d752012-08-07 00:03:56 +0300343 if (dev->dev_state != MEI_DEV_ENABLED) {
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200344 rets = -ENODEV;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300345 goto out;
Oren Weilab841162011-05-15 13:43:41 +0300346 }
347
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300348 id = mei_me_cl_by_id(dev, cl->me_client_id);
349 if (id < 0) {
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200350 rets = -ENODEV;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300351 goto out;
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200352 }
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300353 if (length > dev->me_clients[id].props.max_msg_length || length <= 0) {
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200354 rets = -EMSGSIZE;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300355 goto out;
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200356 }
357
358 if (cl->state != MEI_FILE_CONNECTED) {
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200359 dev_err(&dev->pdev->dev, "host client = %d, is not connected to ME client = %d",
360 cl->host_client_id, cl->me_client_id);
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300361 rets = -ENODEV;
362 goto out;
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200363 }
Oren Weilab841162011-05-15 13:43:41 +0300364 if (cl == &dev->iamthif_cl) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200365 write_cb = mei_amthif_find_read_list_entry(dev, file);
Oren Weilab841162011-05-15 13:43:41 +0300366
367 if (write_cb) {
368 timeout = write_cb->read_time +
Tomas Winkler3870c322012-11-01 21:17:14 +0200369 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
Oren Weilab841162011-05-15 13:43:41 +0300370
371 if (time_after(jiffies, timeout) ||
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200372 cl->reading_state == MEI_READ_COMPLETE) {
373 *offset = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200374 list_del(&write_cb->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200375 mei_io_cb_free(write_cb);
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200376 write_cb = NULL;
Oren Weilab841162011-05-15 13:43:41 +0300377 }
378 }
379 }
380
381 /* free entry used in read */
382 if (cl->reading_state == MEI_READ_COMPLETE) {
383 *offset = 0;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200384 write_cb = mei_cl_find_read_cb(cl);
Oren Weilab841162011-05-15 13:43:41 +0300385 if (write_cb) {
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200386 list_del(&write_cb->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200387 mei_io_cb_free(write_cb);
Oren Weilab841162011-05-15 13:43:41 +0300388 write_cb = NULL;
389 cl->reading_state = MEI_IDLE;
390 cl->read_cb = NULL;
Oren Weilab841162011-05-15 13:43:41 +0300391 }
Tomas Winklerd91aaed2013-01-08 23:07:18 +0200392 } else if (cl->reading_state == MEI_IDLE)
Oren Weilab841162011-05-15 13:43:41 +0300393 *offset = 0;
394
395
Tomas Winkler33d28c92012-10-09 16:50:17 +0200396 write_cb = mei_io_cb_init(cl, file);
Oren Weilab841162011-05-15 13:43:41 +0300397 if (!write_cb) {
Tomas Winkler33d28c92012-10-09 16:50:17 +0200398 dev_err(&dev->pdev->dev, "write cb allocation failed\n");
399 rets = -ENOMEM;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300400 goto out;
Oren Weilab841162011-05-15 13:43:41 +0300401 }
Tomas Winkler33d28c92012-10-09 16:50:17 +0200402 rets = mei_io_cb_alloc_req_buf(write_cb, length);
403 if (rets)
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300404 goto out;
Oren Weilab841162011-05-15 13:43:41 +0300405
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200406 rets = copy_from_user(write_cb->request_buffer.data, ubuf, length);
407 if (rets)
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300408 goto out;
Oren Weilab841162011-05-15 13:43:41 +0300409
Oren Weilab841162011-05-15 13:43:41 +0300410 if (cl == &dev->iamthif_cl) {
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200411 rets = mei_amthif_write(dev, write_cb);
412
413 if (rets) {
414 dev_err(&dev->pdev->dev,
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200415 "amthif write failed with status = %d\n", rets);
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300416 goto out;
Oren Weilab841162011-05-15 13:43:41 +0300417 }
418 mutex_unlock(&dev->device_lock);
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200419 return length;
Oren Weilab841162011-05-15 13:43:41 +0300420 }
421
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300422 rets = mei_cl_write(cl, write_cb, false);
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200423out:
Oren Weilab841162011-05-15 13:43:41 +0300424 mutex_unlock(&dev->device_lock);
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300425 if (rets < 0)
426 mei_io_cb_free(write_cb);
Oren Weilab841162011-05-15 13:43:41 +0300427 return rets;
428}
429
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200430/**
431 * mei_ioctl_connect_client - the connect to fw client IOCTL function
432 *
433 * @dev: the device structure
434 * @data: IOCTL connect data, input and output parameters
435 * @file: private data of the file object
436 *
437 * Locking: called under "dev->device_lock" lock
438 *
439 * returns 0 on success, <0 on failure.
440 */
441static int mei_ioctl_connect_client(struct file *file,
442 struct mei_connect_client_data *data)
443{
444 struct mei_device *dev;
445 struct mei_client *client;
446 struct mei_cl *cl;
447 int i;
448 int rets;
449
450 cl = file->private_data;
451 if (WARN_ON(!cl || !cl->dev))
452 return -ENODEV;
453
454 dev = cl->dev;
455
456 if (dev->dev_state != MEI_DEV_ENABLED) {
457 rets = -ENODEV;
458 goto end;
459 }
460
461 if (cl->state != MEI_FILE_INITIALIZING &&
462 cl->state != MEI_FILE_DISCONNECTED) {
463 rets = -EBUSY;
464 goto end;
465 }
466
467 /* find ME client we're trying to connect to */
468 i = mei_me_cl_by_uuid(dev, &data->in_client_uuid);
Tomas Winkler80fe6362013-05-07 21:12:31 +0300469 if (i < 0 || dev->me_clients[i].props.fixed_address) {
470 dev_dbg(&dev->pdev->dev, "Cannot connect to FW Client UUID = %pUl\n",
471 &data->in_client_uuid);
472 rets = -ENODEV;
473 goto end;
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200474 }
475
Tomas Winkler80fe6362013-05-07 21:12:31 +0300476 cl->me_client_id = dev->me_clients[i].client_id;
477 cl->state = MEI_FILE_CONNECTING;
478
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200479 dev_dbg(&dev->pdev->dev, "Connect to FW Client ID = %d\n",
480 cl->me_client_id);
481 dev_dbg(&dev->pdev->dev, "FW Client - Protocol Version = %d\n",
482 dev->me_clients[i].props.protocol_version);
483 dev_dbg(&dev->pdev->dev, "FW Client - Max Msg Len = %d\n",
484 dev->me_clients[i].props.max_msg_length);
485
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200486 /* if we're connecting to amthif client then we will use the
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200487 * existing connection
488 */
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200489 if (uuid_le_cmp(data->in_client_uuid, mei_amthif_guid) == 0) {
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200490 dev_dbg(&dev->pdev->dev, "FW Client is amthi\n");
491 if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) {
492 rets = -ENODEV;
493 goto end;
494 }
495 clear_bit(cl->host_client_id, dev->host_clients_map);
496 mei_cl_unlink(cl);
497
498 kfree(cl);
499 cl = NULL;
500 file->private_data = &dev->iamthif_cl;
501
502 client = &data->out_client_properties;
503 client->max_msg_length =
504 dev->me_clients[i].props.max_msg_length;
505 client->protocol_version =
506 dev->me_clients[i].props.protocol_version;
507 rets = dev->iamthif_cl.status;
508
509 goto end;
510 }
511
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200512
513 /* prepare the output buffer */
514 client = &data->out_client_properties;
515 client->max_msg_length = dev->me_clients[i].props.max_msg_length;
516 client->protocol_version = dev->me_clients[i].props.protocol_version;
517 dev_dbg(&dev->pdev->dev, "Can connect?\n");
518
519
520 rets = mei_cl_connect(cl, file);
521
522end:
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200523 return rets;
524}
525
Oren Weilab841162011-05-15 13:43:41 +0300526
527/**
528 * mei_ioctl - the IOCTL function
529 *
530 * @file: pointer to file structure
531 * @cmd: ioctl command
532 * @data: pointer to mei message structure
533 *
534 * returns 0 on success , <0 on error
535 */
536static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
537{
538 struct mei_device *dev;
539 struct mei_cl *cl = file->private_data;
540 struct mei_connect_client_data *connect_data = NULL;
541 int rets;
542
543 if (cmd != IOCTL_MEI_CONNECT_CLIENT)
544 return -EINVAL;
545
546 if (WARN_ON(!cl || !cl->dev))
547 return -ENODEV;
548
549 dev = cl->dev;
550
551 dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd);
552
553 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +0300554 if (dev->dev_state != MEI_DEV_ENABLED) {
Oren Weilab841162011-05-15 13:43:41 +0300555 rets = -ENODEV;
556 goto out;
557 }
558
559 dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
560
561 connect_data = kzalloc(sizeof(struct mei_connect_client_data),
562 GFP_KERNEL);
563 if (!connect_data) {
564 rets = -ENOMEM;
565 goto out;
566 }
567 dev_dbg(&dev->pdev->dev, "copy connect data from user\n");
568 if (copy_from_user(connect_data, (char __user *)data,
569 sizeof(struct mei_connect_client_data))) {
570 dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n");
571 rets = -EFAULT;
572 goto out;
573 }
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200574
Oren Weilab841162011-05-15 13:43:41 +0300575 rets = mei_ioctl_connect_client(file, connect_data);
576
577 /* if all is ok, copying the data back to user. */
578 if (rets)
579 goto out;
580
581 dev_dbg(&dev->pdev->dev, "copy connect data to user\n");
582 if (copy_to_user((char __user *)data, connect_data,
583 sizeof(struct mei_connect_client_data))) {
584 dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
585 rets = -EFAULT;
586 goto out;
587 }
588
589out:
590 kfree(connect_data);
591 mutex_unlock(&dev->device_lock);
592 return rets;
593}
594
595/**
596 * mei_compat_ioctl - the compat IOCTL function
597 *
598 * @file: pointer to file structure
599 * @cmd: ioctl command
600 * @data: pointer to mei message structure
601 *
602 * returns 0 on success , <0 on error
603 */
604#ifdef CONFIG_COMPAT
605static long mei_compat_ioctl(struct file *file,
Tomas Winkler441ab502011-12-13 23:39:34 +0200606 unsigned int cmd, unsigned long data)
Oren Weilab841162011-05-15 13:43:41 +0300607{
608 return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
609}
610#endif
611
612
613/**
614 * mei_poll - the poll function
615 *
616 * @file: pointer to file structure
617 * @wait: pointer to poll_table structure
618 *
619 * returns poll mask
620 */
621static unsigned int mei_poll(struct file *file, poll_table *wait)
622{
623 struct mei_cl *cl = file->private_data;
624 struct mei_device *dev;
625 unsigned int mask = 0;
626
627 if (WARN_ON(!cl || !cl->dev))
628 return mask;
629
630 dev = cl->dev;
631
632 mutex_lock(&dev->device_lock);
633
Tomas Winklerb210d752012-08-07 00:03:56 +0300634 if (dev->dev_state != MEI_DEV_ENABLED)
Oren Weilab841162011-05-15 13:43:41 +0300635 goto out;
636
637
638 if (cl == &dev->iamthif_cl) {
Tomas Winkler744f0f22012-11-11 17:38:02 +0200639 mask = mei_amthif_poll(dev, file, wait);
Oren Weilab841162011-05-15 13:43:41 +0300640 goto out;
641 }
642
643 mutex_unlock(&dev->device_lock);
644 poll_wait(file, &cl->tx_wait, wait);
645 mutex_lock(&dev->device_lock);
646 if (MEI_WRITE_COMPLETE == cl->writing_state)
647 mask |= (POLLIN | POLLRDNORM);
648
649out:
650 mutex_unlock(&dev->device_lock);
651 return mask;
652}
653
Oren Weil5b881e32011-11-13 09:41:14 +0200654/*
655 * file operations structure will be used for mei char device.
656 */
657static const struct file_operations mei_fops = {
658 .owner = THIS_MODULE,
659 .read = mei_read,
660 .unlocked_ioctl = mei_ioctl,
661#ifdef CONFIG_COMPAT
662 .compat_ioctl = mei_compat_ioctl,
663#endif
664 .open = mei_open,
665 .release = mei_release,
666 .write = mei_write,
667 .poll = mei_poll,
668 .llseek = no_llseek
669};
670
Oren Weil5b881e32011-11-13 09:41:14 +0200671/*
672 * Misc Device Struct
673 */
674static struct miscdevice mei_misc_device = {
Tomas Winklerc38ea242012-04-02 20:32:39 +0300675 .name = "mei",
Oren Weil5b881e32011-11-13 09:41:14 +0200676 .fops = &mei_fops,
677 .minor = MISC_DYNAMIC_MINOR,
678};
679
Tomas Winkler30e53bb2013-04-05 22:10:34 +0300680
681int mei_register(struct mei_device *dev)
Tomas Winkler9a123f12012-08-06 15:23:55 +0300682{
Tomas Winkler30e53bb2013-04-05 22:10:34 +0300683 int ret;
684 mei_misc_device.parent = &dev->pdev->dev;
685 ret = misc_register(&mei_misc_device);
686 if (ret)
687 return ret;
688
689 if (mei_dbgfs_register(dev, mei_misc_device.name))
690 dev_err(&dev->pdev->dev, "cannot register debugfs\n");
691
692 return 0;
Oren Weil5b881e32011-11-13 09:41:14 +0200693}
Tomas Winkler40e0b672013-03-27 16:58:30 +0200694EXPORT_SYMBOL_GPL(mei_register);
Oren Weil5b881e32011-11-13 09:41:14 +0200695
Tomas Winkler30e53bb2013-04-05 22:10:34 +0300696void mei_deregister(struct mei_device *dev)
Oren Weil5b881e32011-11-13 09:41:14 +0200697{
Tomas Winkler30e53bb2013-04-05 22:10:34 +0300698 mei_dbgfs_deregister(dev);
Tomas Winklera44cab42012-05-29 16:39:11 +0300699 misc_deregister(&mei_misc_device);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200700 mei_misc_device.parent = NULL;
Oren Weilab841162011-05-15 13:43:41 +0300701}
Tomas Winkler40e0b672013-03-27 16:58:30 +0200702EXPORT_SYMBOL_GPL(mei_deregister);
Oren Weilab841162011-05-15 13:43:41 +0300703
Samuel Ortizcf3baef2013-03-27 17:29:57 +0200704static int __init mei_init(void)
705{
706 return mei_cl_bus_init();
707}
708
709static void __exit mei_exit(void)
710{
711 mei_cl_bus_exit();
712}
713
714module_init(mei_init);
715module_exit(mei_exit);
716
Tomas Winkler40e0b672013-03-27 16:58:30 +0200717MODULE_AUTHOR("Intel Corporation");
718MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
Tomas Winkler827eef52013-02-06 14:06:41 +0200719MODULE_LICENSE("GPL v2");
Oren Weilab841162011-05-15 13:43:41 +0300720