blob: f9d458cced2185ba84b2f1f467f0dfe192579443 [file] [log] [blame]
Tomas Winkler19838fb2012-11-01 21:17:15 +02001/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2012, Intel Corporation.
5 *
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
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/aio.h>
23#include <linux/pci.h>
24#include <linux/init.h>
25#include <linux/ioctl.h>
26#include <linux/cdev.h>
27#include <linux/list.h>
28#include <linux/delay.h>
29#include <linux/sched.h>
30#include <linux/uuid.h>
31#include <linux/jiffies.h>
32#include <linux/uaccess.h>
33
Tomas Winkler47a73802012-12-25 19:06:03 +020034#include <linux/mei.h>
Tomas Winkler19838fb2012-11-01 21:17:15 +020035
36#include "mei_dev.h"
Tomas Winkler19838fb2012-11-01 21:17:15 +020037#include "interface.h"
38
39const uuid_le mei_amthi_guid = UUID_LE(0x12f80028, 0xb4b7, 0x4b2d, 0xac,
40 0xa8, 0x46, 0xe0, 0xff, 0x65,
41 0x81, 0x4c);
42
43/**
44 * mei_amthif_reset_params - initializes mei device iamthif
45 *
46 * @dev: the device structure
47 */
48void mei_amthif_reset_params(struct mei_device *dev)
49{
50 /* reset iamthif parameters. */
51 dev->iamthif_current_cb = NULL;
52 dev->iamthif_msg_buf_size = 0;
53 dev->iamthif_msg_buf_index = 0;
54 dev->iamthif_canceled = false;
55 dev->iamthif_ioctl = false;
56 dev->iamthif_state = MEI_IAMTHIF_IDLE;
57 dev->iamthif_timer = 0;
58}
59
60/**
61 * mei_amthif_host_init_ - mei initialization amthif client.
62 *
63 * @dev: the device structure
64 *
65 */
66void mei_amthif_host_init(struct mei_device *dev)
67{
68 int i;
69 unsigned char *msg_buf;
70
71 mei_cl_init(&dev->iamthif_cl, dev);
72 dev->iamthif_cl.state = MEI_FILE_DISCONNECTED;
73
74 /* find ME amthi client */
Tomas Winklerff8b2f42012-11-11 17:38:03 +020075 i = mei_me_cl_link(dev, &dev->iamthif_cl,
Tomas Winkler19838fb2012-11-01 21:17:15 +020076 &mei_amthi_guid, MEI_IAMTHIF_HOST_CLIENT_ID);
77 if (i < 0) {
Tomas Winklerff8b2f42012-11-11 17:38:03 +020078 dev_info(&dev->pdev->dev, "failed to find iamthif client.\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +020079 return;
80 }
81
82 /* Assign iamthif_mtu to the value received from ME */
83
84 dev->iamthif_mtu = dev->me_clients[i].props.max_msg_length;
85 dev_dbg(&dev->pdev->dev, "IAMTHIF_MTU = %d\n",
86 dev->me_clients[i].props.max_msg_length);
87
88 kfree(dev->iamthif_msg_buf);
89 dev->iamthif_msg_buf = NULL;
90
91 /* allocate storage for ME message buffer */
92 msg_buf = kcalloc(dev->iamthif_mtu,
93 sizeof(unsigned char), GFP_KERNEL);
94 if (!msg_buf) {
95 dev_dbg(&dev->pdev->dev, "memory allocation for ME message buffer failed.\n");
96 return;
97 }
98
99 dev->iamthif_msg_buf = msg_buf;
100
101 if (mei_connect(dev, &dev->iamthif_cl)) {
102 dev_dbg(&dev->pdev->dev, "Failed to connect to AMTHI client\n");
103 dev->iamthif_cl.state = MEI_FILE_DISCONNECTED;
104 dev->iamthif_cl.host_client_id = 0;
105 } else {
106 dev->iamthif_cl.timer_count = MEI_CONNECT_TIMEOUT;
107 }
108}
109
110/**
111 * mei_amthif_find_read_list_entry - finds a amthilist entry for current file
112 *
113 * @dev: the device structure
114 * @file: pointer to file object
115 *
116 * returns returned a list entry on success, NULL on failure.
117 */
118struct mei_cl_cb *mei_amthif_find_read_list_entry(struct mei_device *dev,
119 struct file *file)
120{
Tomas Winkler19838fb2012-11-01 21:17:15 +0200121 struct mei_cl_cb *pos = NULL;
122 struct mei_cl_cb *next = NULL;
123
124 list_for_each_entry_safe(pos, next,
Tomas Winklere773efc2012-11-11 17:37:58 +0200125 &dev->amthif_rd_complete_list.list, list) {
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200126 if (pos->cl && pos->cl == &dev->iamthif_cl &&
Tomas Winkler19838fb2012-11-01 21:17:15 +0200127 pos->file_object == file)
128 return pos;
129 }
130 return NULL;
131}
132
133
134/**
135 * mei_amthif_read - read data from AMTHIF client
136 *
137 * @dev: the device structure
138 * @if_num: minor number
139 * @file: pointer to file object
140 * @*ubuf: pointer to user data in user space
141 * @length: data length to read
142 * @offset: data read offset
143 *
144 * Locking: called under "dev->device_lock" lock
145 *
146 * returns
147 * returned data length on success,
148 * zero if no data to read,
149 * negative on failure.
150 */
151int mei_amthif_read(struct mei_device *dev, struct file *file,
152 char __user *ubuf, size_t length, loff_t *offset)
153{
154 int rets;
155 int wait_ret;
156 struct mei_cl_cb *cb = NULL;
157 struct mei_cl *cl = file->private_data;
158 unsigned long timeout;
159 int i;
160
161 /* Only Posible if we are in timeout */
162 if (!cl || cl != &dev->iamthif_cl) {
163 dev_dbg(&dev->pdev->dev, "bad file ext.\n");
164 return -ETIMEDOUT;
165 }
166
167 i = mei_me_cl_by_id(dev, dev->iamthif_cl.me_client_id);
168
169 if (i < 0) {
170 dev_dbg(&dev->pdev->dev, "amthi client not found.\n");
171 return -ENODEV;
172 }
173 dev_dbg(&dev->pdev->dev, "checking amthi data\n");
174 cb = mei_amthif_find_read_list_entry(dev, file);
175
176 /* Check for if we can block or not*/
177 if (cb == NULL && file->f_flags & O_NONBLOCK)
178 return -EAGAIN;
179
180
181 dev_dbg(&dev->pdev->dev, "waiting for amthi data\n");
182 while (cb == NULL) {
183 /* unlock the Mutex */
184 mutex_unlock(&dev->device_lock);
185
186 wait_ret = wait_event_interruptible(dev->iamthif_cl.wait,
187 (cb = mei_amthif_find_read_list_entry(dev, file)));
188
189 if (wait_ret)
190 return -ERESTARTSYS;
191
192 dev_dbg(&dev->pdev->dev, "woke up from sleep\n");
193
194 /* Locking again the Mutex */
195 mutex_lock(&dev->device_lock);
196 }
197
198
199 dev_dbg(&dev->pdev->dev, "Got amthi data\n");
200 dev->iamthif_timer = 0;
201
202 if (cb) {
203 timeout = cb->read_time +
204 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
205 dev_dbg(&dev->pdev->dev, "amthi timeout = %lud\n",
206 timeout);
207
208 if (time_after(jiffies, timeout)) {
209 dev_dbg(&dev->pdev->dev, "amthi Time out\n");
210 /* 15 sec for the message has expired */
211 list_del(&cb->list);
212 rets = -ETIMEDOUT;
213 goto free;
214 }
215 }
216 /* if the whole message will fit remove it from the list */
217 if (cb->buf_idx >= *offset && length >= (cb->buf_idx - *offset))
218 list_del(&cb->list);
219 else if (cb->buf_idx > 0 && cb->buf_idx <= *offset) {
220 /* end of the message has been reached */
221 list_del(&cb->list);
222 rets = 0;
223 goto free;
224 }
225 /* else means that not full buffer will be read and do not
226 * remove message from deletion list
227 */
228
229 dev_dbg(&dev->pdev->dev, "amthi cb->response_buffer size - %d\n",
230 cb->response_buffer.size);
231 dev_dbg(&dev->pdev->dev, "amthi cb->buf_idx - %lu\n", cb->buf_idx);
232
233 /* length is being turncated to PAGE_SIZE, however,
234 * the buf_idx may point beyond */
235 length = min_t(size_t, length, (cb->buf_idx - *offset));
236
237 if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length))
238 rets = -EFAULT;
239 else {
240 rets = length;
241 if ((*offset + length) < cb->buf_idx) {
242 *offset += length;
243 goto out;
244 }
245 }
246free:
247 dev_dbg(&dev->pdev->dev, "free amthi cb memory.\n");
248 *offset = 0;
249 mei_io_cb_free(cb);
250out:
251 return rets;
252}
253
254/**
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200255 * mei_amthif_send_cmd - send amthif command to the ME
Tomas Winkler19838fb2012-11-01 21:17:15 +0200256 *
257 * @dev: the device structure
258 * @cb: mei call back struct
259 *
260 * returns 0 on success, <0 on failure.
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200261 *
Tomas Winkler19838fb2012-11-01 21:17:15 +0200262 */
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200263static int mei_amthif_send_cmd(struct mei_device *dev, struct mei_cl_cb *cb)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200264{
265 struct mei_msg_hdr mei_hdr;
266 int ret;
267
268 if (!dev || !cb)
269 return -ENODEV;
270
271 dev_dbg(&dev->pdev->dev, "write data to amthi client.\n");
272
273 dev->iamthif_state = MEI_IAMTHIF_WRITING;
274 dev->iamthif_current_cb = cb;
275 dev->iamthif_file_object = cb->file_object;
276 dev->iamthif_canceled = false;
277 dev->iamthif_ioctl = true;
278 dev->iamthif_msg_buf_size = cb->request_buffer.size;
279 memcpy(dev->iamthif_msg_buf, cb->request_buffer.data,
280 cb->request_buffer.size);
281
282 ret = mei_flow_ctrl_creds(dev, &dev->iamthif_cl);
283 if (ret < 0)
284 return ret;
285
286 if (ret && dev->mei_host_buffer_is_empty) {
287 ret = 0;
288 dev->mei_host_buffer_is_empty = false;
289 if (cb->request_buffer.size > mei_hbuf_max_data(dev)) {
290 mei_hdr.length = mei_hbuf_max_data(dev);
291 mei_hdr.msg_complete = 0;
292 } else {
293 mei_hdr.length = cb->request_buffer.size;
294 mei_hdr.msg_complete = 1;
295 }
296
297 mei_hdr.host_addr = dev->iamthif_cl.host_client_id;
298 mei_hdr.me_addr = dev->iamthif_cl.me_client_id;
299 mei_hdr.reserved = 0;
300 dev->iamthif_msg_buf_index += mei_hdr.length;
301 if (mei_write_message(dev, &mei_hdr,
Tomas Winkler438763f2012-12-25 19:05:59 +0200302 (unsigned char *)dev->iamthif_msg_buf))
Tomas Winkler19838fb2012-11-01 21:17:15 +0200303 return -ENODEV;
304
305 if (mei_hdr.msg_complete) {
306 if (mei_flow_ctrl_reduce(dev, &dev->iamthif_cl))
307 return -ENODEV;
308 dev->iamthif_flow_control_pending = true;
309 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
310 dev_dbg(&dev->pdev->dev, "add amthi cb to write waiting list\n");
311 dev->iamthif_current_cb = cb;
312 dev->iamthif_file_object = cb->file_object;
313 list_add_tail(&cb->list, &dev->write_waiting_list.list);
314 } else {
315 dev_dbg(&dev->pdev->dev, "message does not complete, so add amthi cb to write list.\n");
316 list_add_tail(&cb->list, &dev->write_list.list);
317 }
318 } else {
319 if (!(dev->mei_host_buffer_is_empty))
320 dev_dbg(&dev->pdev->dev, "host buffer is not empty");
321
322 dev_dbg(&dev->pdev->dev, "No flow control credentials, so add iamthif cb to write list.\n");
323 list_add_tail(&cb->list, &dev->write_list.list);
324 }
325 return 0;
326}
327
328/**
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200329 * mei_amthif_write - write amthif data to amthif client
330 *
331 * @dev: the device structure
332 * @cb: mei call back struct
333 *
334 * returns 0 on success, <0 on failure.
335 *
336 */
337int mei_amthif_write(struct mei_device *dev, struct mei_cl_cb *cb)
338{
339 int ret;
340
341 if (!dev || !cb)
342 return -ENODEV;
343
344 ret = mei_io_cb_alloc_resp_buf(cb, dev->iamthif_mtu);
345 if (ret)
346 return ret;
347
Tomas Winkler4b8960b2012-11-11 17:38:00 +0200348 cb->fop_type = MEI_FOP_IOCTL;
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200349
Tomas Winklere773efc2012-11-11 17:37:58 +0200350 if (!list_empty(&dev->amthif_cmd_list.list) ||
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200351 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
352 dev_dbg(&dev->pdev->dev,
353 "amthif state = %d\n", dev->iamthif_state);
354 dev_dbg(&dev->pdev->dev, "AMTHIF: add cb to the wait list\n");
Tomas Winklere773efc2012-11-11 17:37:58 +0200355 list_add_tail(&cb->list, &dev->amthif_cmd_list.list);
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200356 return 0;
357 }
358 return mei_amthif_send_cmd(dev, cb);
359}
360/**
Tomas Winkler19838fb2012-11-01 21:17:15 +0200361 * mei_amthif_run_next_cmd
362 *
363 * @dev: the device structure
364 *
365 * returns 0 on success, <0 on failure.
366 */
367void mei_amthif_run_next_cmd(struct mei_device *dev)
368{
Tomas Winkler19838fb2012-11-01 21:17:15 +0200369 struct mei_cl_cb *pos = NULL;
370 struct mei_cl_cb *next = NULL;
371 int status;
372
373 if (!dev)
374 return;
375
376 dev->iamthif_msg_buf_size = 0;
377 dev->iamthif_msg_buf_index = 0;
378 dev->iamthif_canceled = false;
379 dev->iamthif_ioctl = true;
380 dev->iamthif_state = MEI_IAMTHIF_IDLE;
381 dev->iamthif_timer = 0;
382 dev->iamthif_file_object = NULL;
383
384 dev_dbg(&dev->pdev->dev, "complete amthi cmd_list cb.\n");
385
Tomas Winklere773efc2012-11-11 17:37:58 +0200386 list_for_each_entry_safe(pos, next, &dev->amthif_cmd_list.list, list) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200387 list_del(&pos->list);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200388
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200389 if (pos->cl && pos->cl == &dev->iamthif_cl) {
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200390 status = mei_amthif_send_cmd(dev, pos);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200391 if (status) {
392 dev_dbg(&dev->pdev->dev,
393 "amthi write failed status = %d\n",
394 status);
395 return;
396 }
397 break;
398 }
399 }
400}
401
Tomas Winkler744f0f22012-11-11 17:38:02 +0200402
403unsigned int mei_amthif_poll(struct mei_device *dev,
404 struct file *file, poll_table *wait)
405{
406 unsigned int mask = 0;
407 mutex_unlock(&dev->device_lock);
408 poll_wait(file, &dev->iamthif_cl.wait, wait);
409 mutex_lock(&dev->device_lock);
410 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE &&
411 dev->iamthif_file_object == file) {
412 mask |= (POLLIN | POLLRDNORM);
413 dev_dbg(&dev->pdev->dev, "run next amthi cb\n");
414 mei_amthif_run_next_cmd(dev);
415 }
416 return mask;
417}
418
419
420
Tomas Winkler19838fb2012-11-01 21:17:15 +0200421/**
422 * mei_amthif_irq_process_completed - processes completed iamthif operation.
423 *
424 * @dev: the device structure.
425 * @slots: free slots.
426 * @cb_pos: callback block.
427 * @cl: private data of the file object.
428 * @cmpl_list: complete list.
429 *
430 * returns 0, OK; otherwise, error.
431 */
Tomas Winkler24c656e2012-11-18 15:13:17 +0200432int mei_amthif_irq_write_complete(struct mei_device *dev, s32 *slots,
433 struct mei_cl_cb *cb, struct mei_cl_cb *cmpl_list)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200434{
Tomas Winklere46f1872012-12-25 19:06:10 +0200435 struct mei_msg_hdr mei_hdr;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200436 struct mei_cl *cl = cb->cl;
437 size_t len = dev->iamthif_msg_buf_size - dev->iamthif_msg_buf_index;
438 size_t msg_slots = mei_data2slots(len);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200439
Tomas Winklere46f1872012-12-25 19:06:10 +0200440 mei_hdr.host_addr = cl->host_client_id;
441 mei_hdr.me_addr = cl->me_client_id;
442 mei_hdr.reserved = 0;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200443
444 if (*slots >= msg_slots) {
Tomas Winklere46f1872012-12-25 19:06:10 +0200445 mei_hdr.length = len;
446 mei_hdr.msg_complete = 1;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200447 /* Split the message only if we can write the whole host buffer */
448 } else if (*slots == dev->hbuf_depth) {
449 msg_slots = *slots;
450 len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
Tomas Winklere46f1872012-12-25 19:06:10 +0200451 mei_hdr.length = len;
452 mei_hdr.msg_complete = 0;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200453 } else {
454 /* wait for next time the host buffer is empty */
455 return 0;
456 }
Tomas Winkler19838fb2012-11-01 21:17:15 +0200457
Tomas Winklere46f1872012-12-25 19:06:10 +0200458 dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(&mei_hdr));
Tomas Winkler19838fb2012-11-01 21:17:15 +0200459
Tomas Winkler24c656e2012-11-18 15:13:17 +0200460 *slots -= msg_slots;
Tomas Winklere46f1872012-12-25 19:06:10 +0200461 if (mei_write_message(dev, &mei_hdr,
Tomas Winkler438763f2012-12-25 19:05:59 +0200462 dev->iamthif_msg_buf + dev->iamthif_msg_buf_index)) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200463 dev->iamthif_state = MEI_IAMTHIF_IDLE;
464 cl->status = -ENODEV;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200465 list_del(&cb->list);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200466 return -ENODEV;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200467 }
468
Tomas Winkler24c656e2012-11-18 15:13:17 +0200469 if (mei_flow_ctrl_reduce(dev, cl))
470 return -ENODEV;
471
Tomas Winklere46f1872012-12-25 19:06:10 +0200472 dev->iamthif_msg_buf_index += mei_hdr.length;
Tomas Winkler24c656e2012-11-18 15:13:17 +0200473 cl->status = 0;
474
Tomas Winklere46f1872012-12-25 19:06:10 +0200475 if (mei_hdr.msg_complete) {
Tomas Winkler24c656e2012-11-18 15:13:17 +0200476 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
477 dev->iamthif_flow_control_pending = true;
478
479 /* save iamthif cb sent to amthi client */
480 cb->buf_idx = dev->iamthif_msg_buf_index;
481 dev->iamthif_current_cb = cb;
482
483 list_move_tail(&cb->list, &dev->write_waiting_list.list);
484 }
485
486
Tomas Winkler19838fb2012-11-01 21:17:15 +0200487 return 0;
488}
489
490/**
491 * mei_amthif_irq_read_message - read routine after ISR to
492 * handle the read amthi message
493 *
494 * @complete_list: An instance of our list structure
495 * @dev: the device structure
496 * @mei_hdr: header of amthi message
497 *
498 * returns 0 on success, <0 on failure.
499 */
500int mei_amthif_irq_read_message(struct mei_cl_cb *complete_list,
501 struct mei_device *dev, struct mei_msg_hdr *mei_hdr)
502{
Tomas Winkler19838fb2012-11-01 21:17:15 +0200503 struct mei_cl_cb *cb;
504 unsigned char *buffer;
505
506 BUG_ON(mei_hdr->me_addr != dev->iamthif_cl.me_client_id);
507 BUG_ON(dev->iamthif_state != MEI_IAMTHIF_READING);
508
509 buffer = dev->iamthif_msg_buf + dev->iamthif_msg_buf_index;
510 BUG_ON(dev->iamthif_mtu < dev->iamthif_msg_buf_index + mei_hdr->length);
511
512 mei_read_slots(dev, buffer, mei_hdr->length);
513
514 dev->iamthif_msg_buf_index += mei_hdr->length;
515
516 if (!mei_hdr->msg_complete)
517 return 0;
518
519 dev_dbg(&dev->pdev->dev,
520 "amthi_message_buffer_index =%d\n",
521 mei_hdr->length);
522
523 dev_dbg(&dev->pdev->dev, "completed amthi read.\n ");
524 if (!dev->iamthif_current_cb)
525 return -ENODEV;
526
527 cb = dev->iamthif_current_cb;
528 dev->iamthif_current_cb = NULL;
529
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200530 if (!cb->cl)
Tomas Winkler19838fb2012-11-01 21:17:15 +0200531 return -ENODEV;
532
533 dev->iamthif_stall_timer = 0;
534 cb->buf_idx = dev->iamthif_msg_buf_index;
535 cb->read_time = jiffies;
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200536 if (dev->iamthif_ioctl && cb->cl == &dev->iamthif_cl) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200537 /* found the iamthif cb */
538 dev_dbg(&dev->pdev->dev, "complete the amthi read cb.\n ");
539 dev_dbg(&dev->pdev->dev, "add the amthi read cb to complete.\n ");
540 list_add_tail(&cb->list, &complete_list->list);
541 }
542 return 0;
543}
544
545/**
546 * mei_amthif_irq_read - prepares to read amthif data.
547 *
548 * @dev: the device structure.
549 * @slots: free slots.
550 *
551 * returns 0, OK; otherwise, error.
552 */
553int mei_amthif_irq_read(struct mei_device *dev, s32 *slots)
554{
555
556 if (((*slots) * sizeof(u32)) < (sizeof(struct mei_msg_hdr)
557 + sizeof(struct hbm_flow_control))) {
558 return -EMSGSIZE;
559 }
560 *slots -= mei_data2slots(sizeof(struct hbm_flow_control));
561 if (mei_send_flow_control(dev, &dev->iamthif_cl)) {
562 dev_dbg(&dev->pdev->dev, "iamthif flow control failed\n");
563 return -EIO;
564 }
565
566 dev_dbg(&dev->pdev->dev, "iamthif flow control success\n");
567 dev->iamthif_state = MEI_IAMTHIF_READING;
568 dev->iamthif_flow_control_pending = false;
569 dev->iamthif_msg_buf_index = 0;
570 dev->iamthif_msg_buf_size = 0;
571 dev->iamthif_stall_timer = MEI_IAMTHIF_STALL_TIMER;
572 dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev);
573 return 0;
574}
575
576/**
577 * mei_amthif_complete - complete amthif callback.
578 *
579 * @dev: the device structure.
580 * @cb_pos: callback block.
581 */
582void mei_amthif_complete(struct mei_device *dev, struct mei_cl_cb *cb)
583{
584 if (dev->iamthif_canceled != 1) {
585 dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE;
586 dev->iamthif_stall_timer = 0;
587 memcpy(cb->response_buffer.data,
588 dev->iamthif_msg_buf,
589 dev->iamthif_msg_buf_index);
Tomas Winklere773efc2012-11-11 17:37:58 +0200590 list_add_tail(&cb->list, &dev->amthif_rd_complete_list.list);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200591 dev_dbg(&dev->pdev->dev, "amthi read completed\n");
592 dev->iamthif_timer = jiffies;
593 dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
594 dev->iamthif_timer);
595 } else {
596 mei_amthif_run_next_cmd(dev);
597 }
598
599 dev_dbg(&dev->pdev->dev, "completing amthi call back.\n");
600 wake_up_interruptible(&dev->iamthif_cl.wait);
601}
602
Tomas Winklera562d5c2012-11-11 17:38:01 +0200603/**
604 * mei_clear_list - removes all callbacks associated with file
605 * from mei_cb_list
606 *
607 * @dev: device structure.
608 * @file: file structure
609 * @mei_cb_list: callbacks list
610 *
611 * mei_clear_list is called to clear resources associated with file
612 * when application calls close function or Ctrl-C was pressed
613 *
614 * returns true if callback removed from the list, false otherwise
615 */
616static bool mei_clear_list(struct mei_device *dev,
617 const struct file *file, struct list_head *mei_cb_list)
618{
619 struct mei_cl_cb *cb_pos = NULL;
620 struct mei_cl_cb *cb_next = NULL;
621 bool removed = false;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200622
Tomas Winklera562d5c2012-11-11 17:38:01 +0200623 /* list all list member */
624 list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, list) {
625 /* check if list member associated with a file */
626 if (file == cb_pos->file_object) {
627 /* remove member from the list */
628 list_del(&cb_pos->list);
629 /* check if cb equal to current iamthif cb */
630 if (dev->iamthif_current_cb == cb_pos) {
631 dev->iamthif_current_cb = NULL;
632 /* send flow control to iamthif client */
633 mei_send_flow_control(dev, &dev->iamthif_cl);
634 }
635 /* free all allocated buffers */
636 mei_io_cb_free(cb_pos);
637 cb_pos = NULL;
638 removed = true;
639 }
640 }
641 return removed;
642}
643
644/**
645 * mei_clear_lists - removes all callbacks associated with file
646 *
647 * @dev: device structure
648 * @file: file structure
649 *
650 * mei_clear_lists is called to clear resources associated with file
651 * when application calls close function or Ctrl-C was pressed
652 *
653 * returns true if callback removed from the list, false otherwise
654 */
655static bool mei_clear_lists(struct mei_device *dev, struct file *file)
656{
657 bool removed = false;
658
659 /* remove callbacks associated with a file */
660 mei_clear_list(dev, file, &dev->amthif_cmd_list.list);
661 if (mei_clear_list(dev, file, &dev->amthif_rd_complete_list.list))
662 removed = true;
663
664 mei_clear_list(dev, file, &dev->ctrl_rd_list.list);
665
666 if (mei_clear_list(dev, file, &dev->ctrl_wr_list.list))
667 removed = true;
668
669 if (mei_clear_list(dev, file, &dev->write_waiting_list.list))
670 removed = true;
671
672 if (mei_clear_list(dev, file, &dev->write_list.list))
673 removed = true;
674
675 /* check if iamthif_current_cb not NULL */
676 if (dev->iamthif_current_cb && !removed) {
677 /* check file and iamthif current cb association */
678 if (dev->iamthif_current_cb->file_object == file) {
679 /* remove cb */
680 mei_io_cb_free(dev->iamthif_current_cb);
681 dev->iamthif_current_cb = NULL;
682 removed = true;
683 }
684 }
685 return removed;
686}
687
688/**
689* mei_amthif_release - the release function
690*
691* @inode: pointer to inode structure
692* @file: pointer to file structure
693*
694* returns 0 on success, <0 on error
695*/
696int mei_amthif_release(struct mei_device *dev, struct file *file)
697{
698 if (dev->open_handle_count > 0)
699 dev->open_handle_count--;
700
701 if (dev->iamthif_file_object == file &&
702 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
703
704 dev_dbg(&dev->pdev->dev, "amthi canceled iamthif state %d\n",
705 dev->iamthif_state);
706 dev->iamthif_canceled = true;
707 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
708 dev_dbg(&dev->pdev->dev, "run next amthi iamthif cb\n");
709 mei_amthif_run_next_cmd(dev);
710 }
711 }
712
713 if (mei_clear_lists(dev, file))
714 dev->iamthif_state = MEI_IAMTHIF_IDLE;
715
716 return 0;
717}