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