gro: Allow tunnel stacking in the case of FOU/GUE
[linux/fpc-iii.git] / drivers / misc / mei / amthif.c
blobd2cd53e3fac378b27e0694e04ea8cee0438d784d
1 /*
3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2012, Intel Corporation.
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.
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.
17 #include <linux/kernel.h>
18 #include <linux/fs.h>
19 #include <linux/errno.h>
20 #include <linux/types.h>
21 #include <linux/fcntl.h>
22 #include <linux/ioctl.h>
23 #include <linux/cdev.h>
24 #include <linux/list.h>
25 #include <linux/delay.h>
26 #include <linux/sched.h>
27 #include <linux/uuid.h>
28 #include <linux/jiffies.h>
29 #include <linux/uaccess.h>
30 #include <linux/slab.h>
32 #include <linux/mei.h>
34 #include "mei_dev.h"
35 #include "hbm.h"
36 #include "client.h"
38 const uuid_le mei_amthif_guid = UUID_LE(0x12f80028, 0xb4b7, 0x4b2d,
39 0xac, 0xa8, 0x46, 0xe0,
40 0xff, 0x65, 0x81, 0x4c);
42 /**
43 * mei_amthif_reset_params - initializes mei device iamthif
45 * @dev: the device structure
47 void mei_amthif_reset_params(struct mei_device *dev)
49 /* reset iamthif parameters. */
50 dev->iamthif_current_cb = NULL;
51 dev->iamthif_canceled = false;
52 dev->iamthif_state = MEI_IAMTHIF_IDLE;
53 dev->iamthif_timer = 0;
54 dev->iamthif_stall_timer = 0;
55 dev->iamthif_open_count = 0;
58 /**
59 * mei_amthif_host_init - mei initialization amthif client.
61 * @dev: the device structure
63 * Return: 0 on success, <0 on failure.
65 int mei_amthif_host_init(struct mei_device *dev)
67 struct mei_cl *cl = &dev->iamthif_cl;
68 struct mei_me_client *me_cl;
69 int ret;
71 dev->iamthif_state = MEI_IAMTHIF_IDLE;
73 mei_cl_init(cl, dev);
75 me_cl = mei_me_cl_by_uuid(dev, &mei_amthif_guid);
76 if (!me_cl) {
77 dev_info(dev->dev, "amthif: failed to find the client");
78 return -ENOTTY;
81 cl->me_client_id = me_cl->client_id;
82 cl->cl_uuid = me_cl->props.protocol_name;
84 /* Assign iamthif_mtu to the value received from ME */
86 dev->iamthif_mtu = me_cl->props.max_msg_length;
87 dev_dbg(dev->dev, "IAMTHIF_MTU = %d\n", dev->iamthif_mtu);
90 ret = mei_cl_link(cl, MEI_IAMTHIF_HOST_CLIENT_ID);
91 if (ret < 0) {
92 dev_err(dev->dev, "amthif: failed cl_link %d\n", ret);
93 goto out;
96 ret = mei_cl_connect(cl, NULL);
98 dev->iamthif_state = MEI_IAMTHIF_IDLE;
100 out:
101 mei_me_cl_put(me_cl);
102 return ret;
106 * mei_amthif_find_read_list_entry - finds a amthilist entry for current file
108 * @dev: the device structure
109 * @file: pointer to file object
111 * Return: returned a list entry on success, NULL on failure.
113 struct mei_cl_cb *mei_amthif_find_read_list_entry(struct mei_device *dev,
114 struct file *file)
116 struct mei_cl_cb *cb;
118 list_for_each_entry(cb, &dev->amthif_rd_complete_list.list, list)
119 if (cb->file_object == file)
120 return cb;
121 return NULL;
126 * mei_amthif_read - read data from AMTHIF client
128 * @dev: the device structure
129 * @file: pointer to file object
130 * @ubuf: pointer to user data in user space
131 * @length: data length to read
132 * @offset: data read offset
134 * Locking: called under "dev->device_lock" lock
136 * Return:
137 * returned data length on success,
138 * zero if no data to read,
139 * negative on failure.
141 int mei_amthif_read(struct mei_device *dev, struct file *file,
142 char __user *ubuf, size_t length, loff_t *offset)
144 struct mei_cl *cl = file->private_data;
145 struct mei_cl_cb *cb;
146 unsigned long timeout;
147 int rets;
148 int wait_ret;
150 /* Only possible if we are in timeout */
151 if (!cl) {
152 dev_err(dev->dev, "bad file ext.\n");
153 return -ETIME;
156 dev_dbg(dev->dev, "checking amthif data\n");
157 cb = mei_amthif_find_read_list_entry(dev, file);
159 /* Check for if we can block or not*/
160 if (cb == NULL && file->f_flags & O_NONBLOCK)
161 return -EAGAIN;
164 dev_dbg(dev->dev, "waiting for amthif data\n");
165 while (cb == NULL) {
166 /* unlock the Mutex */
167 mutex_unlock(&dev->device_lock);
169 wait_ret = wait_event_interruptible(dev->iamthif_cl.wait,
170 (cb = mei_amthif_find_read_list_entry(dev, file)));
172 /* Locking again the Mutex */
173 mutex_lock(&dev->device_lock);
175 if (wait_ret)
176 return -ERESTARTSYS;
178 dev_dbg(dev->dev, "woke up from sleep\n");
181 if (cb->status) {
182 rets = cb->status;
183 dev_dbg(dev->dev, "read operation failed %d\n", rets);
184 goto free;
187 dev_dbg(dev->dev, "Got amthif data\n");
188 dev->iamthif_timer = 0;
190 timeout = cb->read_time +
191 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
192 dev_dbg(dev->dev, "amthif timeout = %lud\n",
193 timeout);
195 if (time_after(jiffies, timeout)) {
196 dev_dbg(dev->dev, "amthif Time out\n");
197 /* 15 sec for the message has expired */
198 list_del_init(&cb->list);
199 rets = -ETIME;
200 goto free;
202 /* if the whole message will fit remove it from the list */
203 if (cb->buf_idx >= *offset && length >= (cb->buf_idx - *offset))
204 list_del_init(&cb->list);
205 else if (cb->buf_idx > 0 && cb->buf_idx <= *offset) {
206 /* end of the message has been reached */
207 list_del_init(&cb->list);
208 rets = 0;
209 goto free;
211 /* else means that not full buffer will be read and do not
212 * remove message from deletion list
215 dev_dbg(dev->dev, "amthif cb->buf size - %d\n",
216 cb->buf.size);
217 dev_dbg(dev->dev, "amthif cb->buf_idx - %lu\n", cb->buf_idx);
219 /* length is being truncated to PAGE_SIZE, however,
220 * the buf_idx may point beyond */
221 length = min_t(size_t, length, (cb->buf_idx - *offset));
223 if (copy_to_user(ubuf, cb->buf.data + *offset, length)) {
224 dev_dbg(dev->dev, "failed to copy data to userland\n");
225 rets = -EFAULT;
226 } else {
227 rets = length;
228 if ((*offset + length) < cb->buf_idx) {
229 *offset += length;
230 goto out;
233 free:
234 dev_dbg(dev->dev, "free amthif cb memory.\n");
235 *offset = 0;
236 mei_io_cb_free(cb);
237 out:
238 return rets;
242 * mei_amthif_read_start - queue message for sending read credential
244 * @cl: host client
245 * @file: file pointer of message recipient
247 * Return: 0 on success, <0 on failure.
249 static int mei_amthif_read_start(struct mei_cl *cl, struct file *file)
251 struct mei_device *dev = cl->dev;
252 struct mei_cl_cb *cb;
253 size_t length = dev->iamthif_mtu;
254 int rets;
256 cb = mei_io_cb_init(cl, MEI_FOP_READ, file);
257 if (!cb) {
258 rets = -ENOMEM;
259 goto err;
262 rets = mei_io_cb_alloc_buf(cb, length);
263 if (rets)
264 goto err;
266 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
268 dev->iamthif_state = MEI_IAMTHIF_READING;
269 dev->iamthif_file_object = cb->file_object;
270 dev->iamthif_current_cb = cb;
272 return 0;
273 err:
274 mei_io_cb_free(cb);
275 return rets;
279 * mei_amthif_send_cmd - send amthif command to the ME
281 * @cl: the host client
282 * @cb: mei call back struct
284 * Return: 0 on success, <0 on failure.
286 static int mei_amthif_send_cmd(struct mei_cl *cl, struct mei_cl_cb *cb)
288 struct mei_device *dev;
289 int ret;
291 if (!cl->dev || !cb)
292 return -ENODEV;
294 dev = cl->dev;
296 dev->iamthif_state = MEI_IAMTHIF_WRITING;
297 dev->iamthif_current_cb = cb;
298 dev->iamthif_file_object = cb->file_object;
299 dev->iamthif_canceled = false;
301 ret = mei_cl_write(cl, cb, false);
302 if (ret < 0)
303 return ret;
305 if (cb->completed)
306 cb->status = mei_amthif_read_start(cl, cb->file_object);
308 return 0;
312 * mei_amthif_run_next_cmd - send next amt command from queue
314 * @dev: the device structure
316 * Return: 0 on success, <0 on failure.
318 int mei_amthif_run_next_cmd(struct mei_device *dev)
320 struct mei_cl *cl = &dev->iamthif_cl;
321 struct mei_cl_cb *cb;
323 dev->iamthif_canceled = false;
324 dev->iamthif_state = MEI_IAMTHIF_IDLE;
325 dev->iamthif_timer = 0;
326 dev->iamthif_file_object = NULL;
328 dev_dbg(dev->dev, "complete amthif cmd_list cb.\n");
330 cb = list_first_entry_or_null(&dev->amthif_cmd_list.list,
331 typeof(*cb), list);
332 if (!cb)
333 return 0;
335 list_del_init(&cb->list);
336 return mei_amthif_send_cmd(cl, cb);
340 * mei_amthif_write - write amthif data to amthif client
342 * @cl: host client
343 * @cb: mei call back struct
345 * Return: 0 on success, <0 on failure.
347 int mei_amthif_write(struct mei_cl *cl, struct mei_cl_cb *cb)
350 struct mei_device *dev;
352 if (WARN_ON(!cl || !cl->dev))
353 return -ENODEV;
355 if (WARN_ON(!cb))
356 return -EINVAL;
358 dev = cl->dev;
360 list_add_tail(&cb->list, &dev->amthif_cmd_list.list);
361 return mei_amthif_run_next_cmd(dev);
365 * mei_amthif_poll - the amthif poll function
367 * @dev: the device structure
368 * @file: pointer to file structure
369 * @wait: pointer to poll_table structure
371 * Return: poll mask
373 * Locking: called under "dev->device_lock" lock
376 unsigned int mei_amthif_poll(struct mei_device *dev,
377 struct file *file, poll_table *wait)
379 unsigned int mask = 0;
381 poll_wait(file, &dev->iamthif_cl.wait, wait);
383 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE &&
384 dev->iamthif_file_object == file) {
386 mask |= POLLIN | POLLRDNORM;
387 mei_amthif_run_next_cmd(dev);
390 return mask;
396 * mei_amthif_irq_write - write iamthif command in irq thread context.
398 * @cl: private data of the file object.
399 * @cb: callback block.
400 * @cmpl_list: complete list.
402 * Return: 0, OK; otherwise, error.
404 int mei_amthif_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
405 struct mei_cl_cb *cmpl_list)
407 int ret;
409 ret = mei_cl_irq_write(cl, cb, cmpl_list);
410 if (ret)
411 return ret;
413 if (cb->completed)
414 cb->status = mei_amthif_read_start(cl, cb->file_object);
416 return 0;
420 * mei_amthif_irq_read_msg - read routine after ISR to
421 * handle the read amthif message
423 * @cl: mei client
424 * @mei_hdr: header of amthif message
425 * @cmpl_list: completed callbacks list
427 * Return: -ENODEV if cb is NULL 0 otherwise; error message is in cb->status
429 int mei_amthif_irq_read_msg(struct mei_cl *cl,
430 struct mei_msg_hdr *mei_hdr,
431 struct mei_cl_cb *cmpl_list)
433 struct mei_device *dev;
434 int ret;
436 dev = cl->dev;
438 if (dev->iamthif_state != MEI_IAMTHIF_READING)
439 return 0;
441 ret = mei_cl_irq_read_msg(cl, mei_hdr, cmpl_list);
442 if (ret)
443 return ret;
445 if (!mei_hdr->msg_complete)
446 return 0;
448 dev_dbg(dev->dev, "completed amthif read.\n ");
449 dev->iamthif_current_cb = NULL;
450 dev->iamthif_stall_timer = 0;
452 return 0;
456 * mei_amthif_complete - complete amthif callback.
458 * @dev: the device structure.
459 * @cb: callback block.
461 void mei_amthif_complete(struct mei_device *dev, struct mei_cl_cb *cb)
464 if (cb->fop_type == MEI_FOP_WRITE) {
465 if (!cb->status) {
466 dev->iamthif_stall_timer = MEI_IAMTHIF_STALL_TIMER;
467 mei_io_cb_free(cb);
468 return;
471 * in case of error enqueue the write cb to complete read list
472 * so it can be propagated to the reader
474 list_add_tail(&cb->list, &dev->amthif_rd_complete_list.list);
475 wake_up_interruptible(&dev->iamthif_cl.wait);
476 return;
479 if (dev->iamthif_canceled != 1) {
480 dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE;
481 dev->iamthif_stall_timer = 0;
482 list_add_tail(&cb->list, &dev->amthif_rd_complete_list.list);
483 dev_dbg(dev->dev, "amthif read completed\n");
484 dev->iamthif_timer = jiffies;
485 dev_dbg(dev->dev, "dev->iamthif_timer = %ld\n",
486 dev->iamthif_timer);
487 } else {
488 mei_amthif_run_next_cmd(dev);
491 dev_dbg(dev->dev, "completing amthif call back.\n");
492 wake_up_interruptible(&dev->iamthif_cl.wait);
496 * mei_clear_list - removes all callbacks associated with file
497 * from mei_cb_list
499 * @dev: device structure.
500 * @file: file structure
501 * @mei_cb_list: callbacks list
503 * mei_clear_list is called to clear resources associated with file
504 * when application calls close function or Ctrl-C was pressed
506 * Return: true if callback removed from the list, false otherwise
508 static bool mei_clear_list(struct mei_device *dev,
509 const struct file *file, struct list_head *mei_cb_list)
511 struct mei_cl *cl = &dev->iamthif_cl;
512 struct mei_cl_cb *cb, *next;
513 bool removed = false;
515 /* list all list member */
516 list_for_each_entry_safe(cb, next, mei_cb_list, list) {
517 /* check if list member associated with a file */
518 if (file == cb->file_object) {
519 /* check if cb equal to current iamthif cb */
520 if (dev->iamthif_current_cb == cb) {
521 dev->iamthif_current_cb = NULL;
522 /* send flow control to iamthif client */
523 mei_hbm_cl_flow_control_req(dev, cl);
525 /* free all allocated buffers */
526 mei_io_cb_free(cb);
527 removed = true;
530 return removed;
534 * mei_clear_lists - removes all callbacks associated with file
536 * @dev: device structure
537 * @file: file structure
539 * mei_clear_lists is called to clear resources associated with file
540 * when application calls close function or Ctrl-C was pressed
542 * Return: true if callback removed from the list, false otherwise
544 static bool mei_clear_lists(struct mei_device *dev, struct file *file)
546 bool removed = false;
548 /* remove callbacks associated with a file */
549 mei_clear_list(dev, file, &dev->amthif_cmd_list.list);
550 if (mei_clear_list(dev, file, &dev->amthif_rd_complete_list.list))
551 removed = true;
553 mei_clear_list(dev, file, &dev->ctrl_rd_list.list);
555 if (mei_clear_list(dev, file, &dev->ctrl_wr_list.list))
556 removed = true;
558 if (mei_clear_list(dev, file, &dev->write_waiting_list.list))
559 removed = true;
561 if (mei_clear_list(dev, file, &dev->write_list.list))
562 removed = true;
564 /* check if iamthif_current_cb not NULL */
565 if (dev->iamthif_current_cb && !removed) {
566 /* check file and iamthif current cb association */
567 if (dev->iamthif_current_cb->file_object == file) {
568 /* remove cb */
569 mei_io_cb_free(dev->iamthif_current_cb);
570 dev->iamthif_current_cb = NULL;
571 removed = true;
574 return removed;
578 * mei_amthif_release - the release function
580 * @dev: device structure
581 * @file: pointer to file structure
583 * Return: 0 on success, <0 on error
585 int mei_amthif_release(struct mei_device *dev, struct file *file)
587 if (dev->iamthif_open_count > 0)
588 dev->iamthif_open_count--;
590 if (dev->iamthif_file_object == file &&
591 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
593 dev_dbg(dev->dev, "amthif canceled iamthif state %d\n",
594 dev->iamthif_state);
595 dev->iamthif_canceled = true;
596 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
597 dev_dbg(dev->dev, "run next amthif iamthif cb\n");
598 mei_amthif_run_next_cmd(dev);
602 if (mei_clear_lists(dev, file))
603 dev->iamthif_state = MEI_IAMTHIF_IDLE;
605 return 0;