blob: 701698de5ac7c9c196681421059e7120558f47cb [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;
197 int i;
198 int rets;
199 int err;
200
201
202 if (WARN_ON(!cl || !cl->dev))
203 return -ENODEV;
204
205 dev = cl->dev;
206
207 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +0300208 if (dev->dev_state != MEI_DEV_ENABLED) {
Oren Weilab841162011-05-15 13:43:41 +0300209 rets = -ENODEV;
210 goto out;
211 }
212
213 if ((cl->sm_state & MEI_WD_STATE_INDEPENDENCE_MSG_SENT) == 0) {
214 /* Do not allow to read watchdog client */
Tomas Winkler07b509b2012-07-23 14:05:39 +0300215 i = mei_me_cl_by_uuid(dev, &mei_wd_guid);
Oren Weilab841162011-05-15 13:43:41 +0300216 if (i >= 0) {
217 struct mei_me_client *me_client = &dev->me_clients[i];
Oren Weilab841162011-05-15 13:43:41 +0300218 if (cl->me_client_id == me_client->client_id) {
219 rets = -EBADF;
220 goto out;
221 }
222 }
223 } else {
224 cl->sm_state &= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
225 }
226
227 if (cl == &dev->iamthif_cl) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200228 rets = mei_amthif_read(dev, file, ubuf, length, offset);
Oren Weilab841162011-05-15 13:43:41 +0300229 goto out;
230 }
231
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200232 if (cl->read_cb && cl->read_cb->buf_idx > *offset) {
Oren Weilab841162011-05-15 13:43:41 +0300233 cb = cl->read_cb;
234 goto copy_buffer;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200235 } else if (cl->read_cb && cl->read_cb->buf_idx > 0 &&
236 cl->read_cb->buf_idx <= *offset) {
Oren Weilab841162011-05-15 13:43:41 +0300237 cb = cl->read_cb;
238 rets = 0;
239 goto free;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200240 } else if ((!cl->read_cb || !cl->read_cb->buf_idx) && *offset > 0) {
Justin P. Mattock5f9092f32012-03-12 07:18:09 -0700241 /*Offset needs to be cleaned for contiguous reads*/
Oren Weilab841162011-05-15 13:43:41 +0300242 *offset = 0;
243 rets = 0;
244 goto out;
245 }
246
Tomas Winklerfcb136e2013-04-19 22:01:35 +0300247 err = mei_cl_read_start(cl, length);
Oren Weilab841162011-05-15 13:43:41 +0300248 if (err && err != -EBUSY) {
249 dev_dbg(&dev->pdev->dev,
250 "mei start read failure with status = %d\n", err);
251 rets = err;
252 goto out;
253 }
254
255 if (MEI_READ_COMPLETE != cl->reading_state &&
256 !waitqueue_active(&cl->rx_wait)) {
257 if (file->f_flags & O_NONBLOCK) {
258 rets = -EAGAIN;
259 goto out;
260 }
261
262 mutex_unlock(&dev->device_lock);
263
264 if (wait_event_interruptible(cl->rx_wait,
Tomas Winkler18f48d92013-09-02 13:29:46 +0300265 MEI_READ_COMPLETE == cl->reading_state ||
266 mei_cl_is_transitioning(cl))) {
267
Oren Weilab841162011-05-15 13:43:41 +0300268 if (signal_pending(current))
269 return -EINTR;
270 return -ERESTARTSYS;
271 }
272
273 mutex_lock(&dev->device_lock);
Tomas Winkler18f48d92013-09-02 13:29:46 +0300274 if (mei_cl_is_transitioning(cl)) {
Oren Weilab841162011-05-15 13:43:41 +0300275 rets = -EBUSY;
276 goto out;
277 }
278 }
279
280 cb = cl->read_cb;
281
282 if (!cb) {
283 rets = -ENODEV;
284 goto out;
285 }
286 if (cl->reading_state != MEI_READ_COMPLETE) {
287 rets = 0;
288 goto out;
289 }
290 /* now copy the data to user space */
291copy_buffer:
Tomas Winklerfcb136e2013-04-19 22:01:35 +0300292 dev_dbg(&dev->pdev->dev, "buf.size = %d buf.idx= %ld\n",
293 cb->response_buffer.size, cb->buf_idx);
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200294 if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) {
Oren Weilab841162011-05-15 13:43:41 +0300295 rets = -EMSGSIZE;
296 goto free;
297 }
298
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200299 /* length is being truncated to PAGE_SIZE,
300 * however buf_idx may point beyond that */
301 length = min_t(size_t, length, cb->buf_idx - *offset);
Oren Weilab841162011-05-15 13:43:41 +0300302
Tomas Winkler441ab502011-12-13 23:39:34 +0200303 if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) {
Oren Weilab841162011-05-15 13:43:41 +0300304 rets = -EFAULT;
305 goto free;
306 }
307
308 rets = length;
309 *offset += length;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200310 if ((unsigned long)*offset < cb->buf_idx)
Oren Weilab841162011-05-15 13:43:41 +0300311 goto out;
312
313free:
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200314 cb_pos = mei_cl_find_read_cb(cl);
Oren Weilab841162011-05-15 13:43:41 +0300315 /* Remove entry from read list */
316 if (cb_pos)
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200317 list_del(&cb_pos->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200318 mei_io_cb_free(cb);
Oren Weilab841162011-05-15 13:43:41 +0300319 cl->reading_state = MEI_IDLE;
320 cl->read_cb = NULL;
Oren Weilab841162011-05-15 13:43:41 +0300321out:
322 dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets);
323 mutex_unlock(&dev->device_lock);
324 return rets;
325}
Tomas Winkler33d28c92012-10-09 16:50:17 +0200326/**
Oren Weilab841162011-05-15 13:43:41 +0300327 * mei_write - the write function.
328 *
329 * @file: pointer to file structure
330 * @ubuf: pointer to user buffer
331 * @length: buffer length
332 * @offset: data offset in buffer
333 *
334 * returns >=0 data length on success , <0 on error
335 */
336static ssize_t mei_write(struct file *file, const char __user *ubuf,
Tomas Winkler441ab502011-12-13 23:39:34 +0200337 size_t length, loff_t *offset)
Oren Weilab841162011-05-15 13:43:41 +0300338{
339 struct mei_cl *cl = file->private_data;
340 struct mei_cl_cb *write_cb = NULL;
Oren Weilab841162011-05-15 13:43:41 +0300341 struct mei_device *dev;
342 unsigned long timeout = 0;
343 int rets;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300344 int id;
Oren Weilab841162011-05-15 13:43:41 +0300345
346 if (WARN_ON(!cl || !cl->dev))
347 return -ENODEV;
348
349 dev = cl->dev;
350
351 mutex_lock(&dev->device_lock);
352
Tomas Winklerb210d752012-08-07 00:03:56 +0300353 if (dev->dev_state != MEI_DEV_ENABLED) {
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200354 rets = -ENODEV;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300355 goto out;
Oren Weilab841162011-05-15 13:43:41 +0300356 }
357
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300358 id = mei_me_cl_by_id(dev, cl->me_client_id);
359 if (id < 0) {
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200360 rets = -ENODEV;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300361 goto out;
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200362 }
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300363 if (length > dev->me_clients[id].props.max_msg_length || length <= 0) {
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200364 rets = -EMSGSIZE;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300365 goto out;
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200366 }
367
368 if (cl->state != MEI_FILE_CONNECTED) {
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200369 dev_err(&dev->pdev->dev, "host client = %d, is not connected to ME client = %d",
370 cl->host_client_id, cl->me_client_id);
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300371 rets = -ENODEV;
372 goto out;
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200373 }
Oren Weilab841162011-05-15 13:43:41 +0300374 if (cl == &dev->iamthif_cl) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200375 write_cb = mei_amthif_find_read_list_entry(dev, file);
Oren Weilab841162011-05-15 13:43:41 +0300376
377 if (write_cb) {
378 timeout = write_cb->read_time +
Tomas Winkler3870c322012-11-01 21:17:14 +0200379 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
Oren Weilab841162011-05-15 13:43:41 +0300380
381 if (time_after(jiffies, timeout) ||
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200382 cl->reading_state == MEI_READ_COMPLETE) {
383 *offset = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200384 list_del(&write_cb->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200385 mei_io_cb_free(write_cb);
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200386 write_cb = NULL;
Oren Weilab841162011-05-15 13:43:41 +0300387 }
388 }
389 }
390
391 /* free entry used in read */
392 if (cl->reading_state == MEI_READ_COMPLETE) {
393 *offset = 0;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200394 write_cb = mei_cl_find_read_cb(cl);
Oren Weilab841162011-05-15 13:43:41 +0300395 if (write_cb) {
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200396 list_del(&write_cb->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200397 mei_io_cb_free(write_cb);
Oren Weilab841162011-05-15 13:43:41 +0300398 write_cb = NULL;
399 cl->reading_state = MEI_IDLE;
400 cl->read_cb = NULL;
Oren Weilab841162011-05-15 13:43:41 +0300401 }
Tomas Winklerd91aaed2013-01-08 23:07:18 +0200402 } else if (cl->reading_state == MEI_IDLE)
Oren Weilab841162011-05-15 13:43:41 +0300403 *offset = 0;
404
405
Tomas Winkler33d28c92012-10-09 16:50:17 +0200406 write_cb = mei_io_cb_init(cl, file);
Oren Weilab841162011-05-15 13:43:41 +0300407 if (!write_cb) {
Tomas Winkler33d28c92012-10-09 16:50:17 +0200408 dev_err(&dev->pdev->dev, "write cb allocation failed\n");
409 rets = -ENOMEM;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300410 goto out;
Oren Weilab841162011-05-15 13:43:41 +0300411 }
Tomas Winkler33d28c92012-10-09 16:50:17 +0200412 rets = mei_io_cb_alloc_req_buf(write_cb, length);
413 if (rets)
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300414 goto out;
Oren Weilab841162011-05-15 13:43:41 +0300415
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200416 rets = copy_from_user(write_cb->request_buffer.data, ubuf, length);
417 if (rets)
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300418 goto out;
Oren Weilab841162011-05-15 13:43:41 +0300419
420 cl->sm_state = 0;
421 if (length == 4 &&
422 ((memcmp(mei_wd_state_independence_msg[0],
423 write_cb->request_buffer.data, 4) == 0) ||
424 (memcmp(mei_wd_state_independence_msg[1],
425 write_cb->request_buffer.data, 4) == 0) ||
426 (memcmp(mei_wd_state_independence_msg[2],
427 write_cb->request_buffer.data, 4) == 0)))
428 cl->sm_state |= MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
429
Oren Weilab841162011-05-15 13:43:41 +0300430 if (cl == &dev->iamthif_cl) {
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200431 rets = mei_amthif_write(dev, write_cb);
432
433 if (rets) {
434 dev_err(&dev->pdev->dev,
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200435 "amthif write failed with status = %d\n", rets);
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300436 goto out;
Oren Weilab841162011-05-15 13:43:41 +0300437 }
438 mutex_unlock(&dev->device_lock);
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200439 return length;
Oren Weilab841162011-05-15 13:43:41 +0300440 }
441
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300442 rets = mei_cl_write(cl, write_cb, false);
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200443out:
Oren Weilab841162011-05-15 13:43:41 +0300444 mutex_unlock(&dev->device_lock);
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300445 if (rets < 0)
446 mei_io_cb_free(write_cb);
Oren Weilab841162011-05-15 13:43:41 +0300447 return rets;
448}
449
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200450/**
451 * mei_ioctl_connect_client - the connect to fw client IOCTL function
452 *
453 * @dev: the device structure
454 * @data: IOCTL connect data, input and output parameters
455 * @file: private data of the file object
456 *
457 * Locking: called under "dev->device_lock" lock
458 *
459 * returns 0 on success, <0 on failure.
460 */
461static int mei_ioctl_connect_client(struct file *file,
462 struct mei_connect_client_data *data)
463{
464 struct mei_device *dev;
465 struct mei_client *client;
466 struct mei_cl *cl;
467 int i;
468 int rets;
469
470 cl = file->private_data;
471 if (WARN_ON(!cl || !cl->dev))
472 return -ENODEV;
473
474 dev = cl->dev;
475
476 if (dev->dev_state != MEI_DEV_ENABLED) {
477 rets = -ENODEV;
478 goto end;
479 }
480
481 if (cl->state != MEI_FILE_INITIALIZING &&
482 cl->state != MEI_FILE_DISCONNECTED) {
483 rets = -EBUSY;
484 goto end;
485 }
486
487 /* find ME client we're trying to connect to */
488 i = mei_me_cl_by_uuid(dev, &data->in_client_uuid);
Tomas Winkler46e0cd82013-05-07 21:12:31 +0300489 if (i < 0 || dev->me_clients[i].props.fixed_address) {
490 dev_dbg(&dev->pdev->dev, "Cannot connect to FW Client UUID = %pUl\n",
491 &data->in_client_uuid);
492 rets = -ENODEV;
493 goto end;
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200494 }
495
Tomas Winkler46e0cd82013-05-07 21:12:31 +0300496 cl->me_client_id = dev->me_clients[i].client_id;
497 cl->state = MEI_FILE_CONNECTING;
498
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200499 dev_dbg(&dev->pdev->dev, "Connect to FW Client ID = %d\n",
500 cl->me_client_id);
501 dev_dbg(&dev->pdev->dev, "FW Client - Protocol Version = %d\n",
502 dev->me_clients[i].props.protocol_version);
503 dev_dbg(&dev->pdev->dev, "FW Client - Max Msg Len = %d\n",
504 dev->me_clients[i].props.max_msg_length);
505
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200506 /* if we're connecting to amthif client then we will use the
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200507 * existing connection
508 */
Tomas Winkler1a1aca42013-01-08 23:07:21 +0200509 if (uuid_le_cmp(data->in_client_uuid, mei_amthif_guid) == 0) {
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200510 dev_dbg(&dev->pdev->dev, "FW Client is amthi\n");
511 if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) {
512 rets = -ENODEV;
513 goto end;
514 }
515 clear_bit(cl->host_client_id, dev->host_clients_map);
516 mei_cl_unlink(cl);
517
518 kfree(cl);
519 cl = NULL;
520 file->private_data = &dev->iamthif_cl;
521
522 client = &data->out_client_properties;
523 client->max_msg_length =
524 dev->me_clients[i].props.max_msg_length;
525 client->protocol_version =
526 dev->me_clients[i].props.protocol_version;
527 rets = dev->iamthif_cl.status;
528
529 goto end;
530 }
531
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200532
533 /* prepare the output buffer */
534 client = &data->out_client_properties;
535 client->max_msg_length = dev->me_clients[i].props.max_msg_length;
536 client->protocol_version = dev->me_clients[i].props.protocol_version;
537 dev_dbg(&dev->pdev->dev, "Can connect?\n");
538
539
540 rets = mei_cl_connect(cl, file);
541
542end:
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200543 return rets;
544}
545
Oren Weilab841162011-05-15 13:43:41 +0300546
547/**
548 * mei_ioctl - the IOCTL function
549 *
550 * @file: pointer to file structure
551 * @cmd: ioctl command
552 * @data: pointer to mei message structure
553 *
554 * returns 0 on success , <0 on error
555 */
556static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
557{
558 struct mei_device *dev;
559 struct mei_cl *cl = file->private_data;
560 struct mei_connect_client_data *connect_data = NULL;
561 int rets;
562
563 if (cmd != IOCTL_MEI_CONNECT_CLIENT)
564 return -EINVAL;
565
566 if (WARN_ON(!cl || !cl->dev))
567 return -ENODEV;
568
569 dev = cl->dev;
570
571 dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd);
572
573 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +0300574 if (dev->dev_state != MEI_DEV_ENABLED) {
Oren Weilab841162011-05-15 13:43:41 +0300575 rets = -ENODEV;
576 goto out;
577 }
578
579 dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
580
581 connect_data = kzalloc(sizeof(struct mei_connect_client_data),
582 GFP_KERNEL);
583 if (!connect_data) {
584 rets = -ENOMEM;
585 goto out;
586 }
587 dev_dbg(&dev->pdev->dev, "copy connect data from user\n");
588 if (copy_from_user(connect_data, (char __user *)data,
589 sizeof(struct mei_connect_client_data))) {
590 dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n");
591 rets = -EFAULT;
592 goto out;
593 }
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200594
Oren Weilab841162011-05-15 13:43:41 +0300595 rets = mei_ioctl_connect_client(file, connect_data);
596
597 /* if all is ok, copying the data back to user. */
598 if (rets)
599 goto out;
600
601 dev_dbg(&dev->pdev->dev, "copy connect data to user\n");
602 if (copy_to_user((char __user *)data, connect_data,
603 sizeof(struct mei_connect_client_data))) {
604 dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
605 rets = -EFAULT;
606 goto out;
607 }
608
609out:
610 kfree(connect_data);
611 mutex_unlock(&dev->device_lock);
612 return rets;
613}
614
615/**
616 * mei_compat_ioctl - the compat IOCTL function
617 *
618 * @file: pointer to file structure
619 * @cmd: ioctl command
620 * @data: pointer to mei message structure
621 *
622 * returns 0 on success , <0 on error
623 */
624#ifdef CONFIG_COMPAT
625static long mei_compat_ioctl(struct file *file,
Tomas Winkler441ab502011-12-13 23:39:34 +0200626 unsigned int cmd, unsigned long data)
Oren Weilab841162011-05-15 13:43:41 +0300627{
628 return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
629}
630#endif
631
632
633/**
634 * mei_poll - the poll function
635 *
636 * @file: pointer to file structure
637 * @wait: pointer to poll_table structure
638 *
639 * returns poll mask
640 */
641static unsigned int mei_poll(struct file *file, poll_table *wait)
642{
643 struct mei_cl *cl = file->private_data;
644 struct mei_device *dev;
645 unsigned int mask = 0;
646
647 if (WARN_ON(!cl || !cl->dev))
648 return mask;
649
650 dev = cl->dev;
651
652 mutex_lock(&dev->device_lock);
653
Tomas Winklerb210d752012-08-07 00:03:56 +0300654 if (dev->dev_state != MEI_DEV_ENABLED)
Oren Weilab841162011-05-15 13:43:41 +0300655 goto out;
656
657
658 if (cl == &dev->iamthif_cl) {
Tomas Winkler744f0f22012-11-11 17:38:02 +0200659 mask = mei_amthif_poll(dev, file, wait);
Oren Weilab841162011-05-15 13:43:41 +0300660 goto out;
661 }
662
663 mutex_unlock(&dev->device_lock);
664 poll_wait(file, &cl->tx_wait, wait);
665 mutex_lock(&dev->device_lock);
666 if (MEI_WRITE_COMPLETE == cl->writing_state)
667 mask |= (POLLIN | POLLRDNORM);
668
669out:
670 mutex_unlock(&dev->device_lock);
671 return mask;
672}
673
Oren Weil5b881e32011-11-13 09:41:14 +0200674/*
675 * file operations structure will be used for mei char device.
676 */
677static const struct file_operations mei_fops = {
678 .owner = THIS_MODULE,
679 .read = mei_read,
680 .unlocked_ioctl = mei_ioctl,
681#ifdef CONFIG_COMPAT
682 .compat_ioctl = mei_compat_ioctl,
683#endif
684 .open = mei_open,
685 .release = mei_release,
686 .write = mei_write,
687 .poll = mei_poll,
688 .llseek = no_llseek
689};
690
Oren Weil5b881e32011-11-13 09:41:14 +0200691/*
692 * Misc Device Struct
693 */
694static struct miscdevice mei_misc_device = {
Tomas Winklerc38ea242012-04-02 20:32:39 +0300695 .name = "mei",
Oren Weil5b881e32011-11-13 09:41:14 +0200696 .fops = &mei_fops,
697 .minor = MISC_DYNAMIC_MINOR,
698};
699
Tomas Winkler30e53bb2013-04-05 22:10:34 +0300700
701int mei_register(struct mei_device *dev)
Tomas Winkler9a123f12012-08-06 15:23:55 +0300702{
Tomas Winkler30e53bb2013-04-05 22:10:34 +0300703 int ret;
704 mei_misc_device.parent = &dev->pdev->dev;
705 ret = misc_register(&mei_misc_device);
706 if (ret)
707 return ret;
708
709 if (mei_dbgfs_register(dev, mei_misc_device.name))
710 dev_err(&dev->pdev->dev, "cannot register debugfs\n");
711
712 return 0;
Oren Weil5b881e32011-11-13 09:41:14 +0200713}
Tomas Winkler40e0b672013-03-27 16:58:30 +0200714EXPORT_SYMBOL_GPL(mei_register);
Oren Weil5b881e32011-11-13 09:41:14 +0200715
Tomas Winkler30e53bb2013-04-05 22:10:34 +0300716void mei_deregister(struct mei_device *dev)
Oren Weil5b881e32011-11-13 09:41:14 +0200717{
Tomas Winkler30e53bb2013-04-05 22:10:34 +0300718 mei_dbgfs_deregister(dev);
Tomas Winklera44cab42012-05-29 16:39:11 +0300719 misc_deregister(&mei_misc_device);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200720 mei_misc_device.parent = NULL;
Oren Weilab841162011-05-15 13:43:41 +0300721}
Tomas Winkler40e0b672013-03-27 16:58:30 +0200722EXPORT_SYMBOL_GPL(mei_deregister);
Oren Weilab841162011-05-15 13:43:41 +0300723
Samuel Ortizcf3baef2013-03-27 17:29:57 +0200724static int __init mei_init(void)
725{
726 return mei_cl_bus_init();
727}
728
729static void __exit mei_exit(void)
730{
731 mei_cl_bus_exit();
732}
733
734module_init(mei_init);
735module_exit(mei_exit);
736
Tomas Winkler40e0b672013-03-27 16:58:30 +0200737MODULE_AUTHOR("Intel Corporation");
738MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
Tomas Winkler827eef52013-02-06 14:06:41 +0200739MODULE_LICENSE("GPL v2");
Oren Weilab841162011-05-15 13:43:41 +0300740