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