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