mei: revamp open handler counts
[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"
9dc64d6a 38#include "hw-me.h"
90e0b5f1 39#include "client.h"
19838fb8 40
1a1aca42
TW
41const uuid_le mei_amthif_guid = UUID_LE(0x12f80028, 0xb4b7, 0x4b2d,
42 0xac, 0xa8, 0x46, 0xe0,
43 0xff, 0x65, 0x81, 0x4c);
19838fb8
TW
44
45/**
46 * mei_amthif_reset_params - initializes mei device iamthif
47 *
48 * @dev: the device structure
49 */
50void 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;
4a704575 60 dev->iamthif_stall_timer = 0;
22f96a0e 61 dev->iamthif_open_count = 0;
19838fb8
TW
62}
63
64/**
393b148f 65 * mei_amthif_host_init - mei initialization amthif client.
19838fb8
TW
66 *
67 * @dev: the device structure
68 *
69 */
781d0d89 70int mei_amthif_host_init(struct mei_device *dev)
19838fb8 71{
781d0d89 72 struct mei_cl *cl = &dev->iamthif_cl;
19838fb8 73 unsigned char *msg_buf;
781d0d89 74 int ret, i;
19838fb8 75
6222f7bf
TW
76 dev->iamthif_state = MEI_IAMTHIF_IDLE;
77
781d0d89 78 mei_cl_init(cl, dev);
19838fb8 79
781d0d89 80 i = mei_me_cl_by_uuid(dev, &mei_amthif_guid);
19838fb8 81 if (i < 0) {
781d0d89
TW
82 dev_info(&dev->pdev->dev, "amthif: failed to find the client\n");
83 return -ENOENT;
19838fb8
TW
84 }
85
781d0d89
TW
86 cl->me_client_id = dev->me_clients[i].client_id;
87
19838fb8
TW
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) {
781d0d89
TW
101 dev_err(&dev->pdev->dev, "amthif: memory allocation for ME message buffer failed.\n");
102 return -ENOMEM;
19838fb8
TW
103 }
104
105 dev->iamthif_msg_buf = msg_buf;
106
781d0d89
TW
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;
19838fb8 120 } else {
781d0d89 121 cl->timer_count = MEI_CONNECT_TIMEOUT;
19838fb8 122 }
781d0d89 123 return 0;
19838fb8
TW
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 */
134struct mei_cl_cb *mei_amthif_find_read_list_entry(struct mei_device *dev,
135 struct file *file)
136{
19838fb8
TW
137 struct mei_cl_cb *pos = NULL;
138 struct mei_cl_cb *next = NULL;
139
140 list_for_each_entry_safe(pos, next,
e773efc4 141 &dev->amthif_rd_complete_list.list, list) {
db3ed431 142 if (pos->cl && pos->cl == &dev->iamthif_cl &&
19838fb8
TW
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 */
167int 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) {
1a1aca42 186 dev_dbg(&dev->pdev->dev, "amthif client not found.\n");
19838fb8
TW
187 return -ENODEV;
188 }
1a1aca42 189 dev_dbg(&dev->pdev->dev, "checking amthif data\n");
19838fb8
TW
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
1a1aca42 197 dev_dbg(&dev->pdev->dev, "waiting for amthif data\n");
19838fb8
TW
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
e6028db0
AK
205 /* Locking again the Mutex */
206 mutex_lock(&dev->device_lock);
207
19838fb8
TW
208 if (wait_ret)
209 return -ERESTARTSYS;
210
211 dev_dbg(&dev->pdev->dev, "woke up from sleep\n");
19838fb8
TW
212 }
213
214
1a1aca42 215 dev_dbg(&dev->pdev->dev, "Got amthif data\n");
19838fb8
TW
216 dev->iamthif_timer = 0;
217
218 if (cb) {
219 timeout = cb->read_time +
220 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
1a1aca42 221 dev_dbg(&dev->pdev->dev, "amthif timeout = %lud\n",
19838fb8
TW
222 timeout);
223
224 if (time_after(jiffies, timeout)) {
1a1aca42 225 dev_dbg(&dev->pdev->dev, "amthif Time out\n");
19838fb8
TW
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
1a1aca42 245 dev_dbg(&dev->pdev->dev, "amthif cb->response_buffer size - %d\n",
19838fb8 246 cb->response_buffer.size);
1a1aca42 247 dev_dbg(&dev->pdev->dev, "amthif cb->buf_idx - %lu\n", cb->buf_idx);
19838fb8
TW
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 }
262free:
1a1aca42 263 dev_dbg(&dev->pdev->dev, "free amthif cb memory.\n");
19838fb8
TW
264 *offset = 0;
265 mei_io_cb_free(cb);
266out:
267 return rets;
268}
269
270/**
ab5c4a56 271 * mei_amthif_send_cmd - send amthif command to the ME
19838fb8
TW
272 *
273 * @dev: the device structure
274 * @cb: mei call back struct
275 *
276 * returns 0 on success, <0 on failure.
ab5c4a56 277 *
19838fb8 278 */
ab5c4a56 279static int mei_amthif_send_cmd(struct mei_device *dev, struct mei_cl_cb *cb)
19838fb8
TW
280{
281 struct mei_msg_hdr mei_hdr;
282 int ret;
283
284 if (!dev || !cb)
285 return -ENODEV;
286
1a1aca42 287 dev_dbg(&dev->pdev->dev, "write data to amthif client.\n");
19838fb8
TW
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
90e0b5f1 298 ret = mei_cl_flow_ctrl_creds(&dev->iamthif_cl);
19838fb8
TW
299 if (ret < 0)
300 return ret;
301
330dd7da 302 if (ret && dev->hbuf_is_ready) {
19838fb8 303 ret = 0;
330dd7da 304 dev->hbuf_is_ready = false;
827eef51
TW
305 if (cb->request_buffer.size > mei_hbuf_max_len(dev)) {
306 mei_hdr.length = mei_hbuf_max_len(dev);
19838fb8
TW
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;
2ebf8c94
TW
317 ret = mei_write_message(dev, &mei_hdr, dev->iamthif_msg_buf);
318 if (ret)
319 return ret;
19838fb8
TW
320
321 if (mei_hdr.msg_complete) {
90e0b5f1 322 if (mei_cl_flow_ctrl_reduce(&dev->iamthif_cl))
2ebf8c94 323 return -EIO;
19838fb8
TW
324 dev->iamthif_flow_control_pending = true;
325 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
1a1aca42 326 dev_dbg(&dev->pdev->dev, "add amthif cb to write waiting list\n");
19838fb8
TW
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 {
1a1aca42 331 dev_dbg(&dev->pdev->dev, "message does not complete, so add amthif cb to write list.\n");
19838fb8
TW
332 list_add_tail(&cb->list, &dev->write_list.list);
333 }
334 } else {
330dd7da 335 if (!dev->hbuf_is_ready)
19838fb8
TW
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
ab5c4a56
TW
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 */
353int 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
4b8960b4 364 cb->fop_type = MEI_FOP_IOCTL;
ab5c4a56 365
e773efc4 366 if (!list_empty(&dev->amthif_cmd_list.list) ||
ab5c4a56
TW
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");
e773efc4 371 list_add_tail(&cb->list, &dev->amthif_cmd_list.list);
ab5c4a56
TW
372 return 0;
373 }
374 return mei_amthif_send_cmd(dev, cb);
375}
19838fb8
TW
376/**
377 * mei_amthif_run_next_cmd
378 *
379 * @dev: the device structure
380 *
381 * returns 0 on success, <0 on failure.
382 */
383void mei_amthif_run_next_cmd(struct mei_device *dev)
384{
19838fb8
TW
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
1a1aca42 400 dev_dbg(&dev->pdev->dev, "complete amthif cmd_list cb.\n");
19838fb8 401
e773efc4 402 list_for_each_entry_safe(pos, next, &dev->amthif_cmd_list.list, list) {
19838fb8 403 list_del(&pos->list);
19838fb8 404
db3ed431 405 if (pos->cl && pos->cl == &dev->iamthif_cl) {
ab5c4a56 406 status = mei_amthif_send_cmd(dev, pos);
19838fb8
TW
407 if (status) {
408 dev_dbg(&dev->pdev->dev,
1a1aca42 409 "amthif write failed status = %d\n",
19838fb8
TW
410 status);
411 return;
412 }
413 break;
414 }
415 }
416}
417
744f0f2f
TW
418
419unsigned int mei_amthif_poll(struct mei_device *dev,
420 struct file *file, poll_table *wait)
421{
422 unsigned int mask = 0;
b950ac1d 423
744f0f2f 424 poll_wait(file, &dev->iamthif_cl.wait, wait);
b950ac1d 425
744f0f2f 426 mutex_lock(&dev->device_lock);
b950ac1d
TW
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
744f0f2f 434 mask |= (POLLIN | POLLRDNORM);
1a1aca42 435 dev_dbg(&dev->pdev->dev, "run next amthif cb\n");
744f0f2f
TW
436 mei_amthif_run_next_cmd(dev);
437 }
b950ac1d
TW
438 mutex_unlock(&dev->device_lock);
439
744f0f2f
TW
440 return mask;
441}
442
443
444
19838fb8 445/**
393b148f 446 * mei_amthif_irq_write_completed - processes completed iamthif operation.
19838fb8
TW
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 */
6220d6a0
TW
456int mei_amthif_irq_write_complete(struct mei_cl *cl, struct mei_cl_cb *cb,
457 s32 *slots, struct mei_cl_cb *cmpl_list)
19838fb8 458{
6220d6a0 459 struct mei_device *dev = cl->dev;
e46f1874 460 struct mei_msg_hdr mei_hdr;
24c656e5 461 size_t len = dev->iamthif_msg_buf_size - dev->iamthif_msg_buf_index;
c8c8d080 462 u32 msg_slots = mei_data2slots(len);
2ebf8c94 463 int rets;
19838fb8 464
136698e5
TW
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
e46f1874
TW
474 mei_hdr.host_addr = cl->host_client_id;
475 mei_hdr.me_addr = cl->me_client_id;
476 mei_hdr.reserved = 0;
24c656e5
TW
477
478 if (*slots >= msg_slots) {
e46f1874
TW
479 mei_hdr.length = len;
480 mei_hdr.msg_complete = 1;
24c656e5
TW
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);
e46f1874
TW
485 mei_hdr.length = len;
486 mei_hdr.msg_complete = 0;
24c656e5
TW
487 } else {
488 /* wait for next time the host buffer is empty */
489 return 0;
490 }
19838fb8 491
e46f1874 492 dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(&mei_hdr));
19838fb8 493
24c656e5 494 *slots -= msg_slots;
2ebf8c94
TW
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;
24c656e5 502 }
19838fb8 503
90e0b5f1 504 if (mei_cl_flow_ctrl_reduce(cl))
2ebf8c94 505 return -EIO;
19838fb8 506
e46f1874 507 dev->iamthif_msg_buf_index += mei_hdr.length;
24c656e5 508 cl->status = 0;
19838fb8 509
e46f1874 510 if (mei_hdr.msg_complete) {
24c656e5
TW
511 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
512 dev->iamthif_flow_control_pending = true;
513
1a1aca42 514 /* save iamthif cb sent to amthif client */
24c656e5
TW
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);
19838fb8
TW
519 }
520
24c656e5 521
19838fb8
TW
522 return 0;
523}
524
525/**
526 * mei_amthif_irq_read_message - read routine after ISR to
1a1aca42 527 * handle the read amthif message
19838fb8 528 *
19838fb8 529 * @dev: the device structure
1a1aca42 530 * @mei_hdr: header of amthif message
5ceb46e2 531 * @complete_list: An instance of our list structure
19838fb8
TW
532 *
533 * returns 0 on success, <0 on failure.
534 */
5ceb46e2
TW
535int mei_amthif_irq_read_msg(struct mei_device *dev,
536 struct mei_msg_hdr *mei_hdr,
537 struct mei_cl_cb *complete_list)
19838fb8 538{
19838fb8
TW
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
5ceb46e2 555 dev_dbg(&dev->pdev->dev, "amthif_message_buffer_index =%d\n",
19838fb8
TW
556 mei_hdr->length);
557
1a1aca42 558 dev_dbg(&dev->pdev->dev, "completed amthif read.\n ");
19838fb8
TW
559 if (!dev->iamthif_current_cb)
560 return -ENODEV;
561
562 cb = dev->iamthif_current_cb;
563 dev->iamthif_current_cb = NULL;
564
db3ed431 565 if (!cb->cl)
19838fb8
TW
566 return -ENODEV;
567
568 dev->iamthif_stall_timer = 0;
569 cb->buf_idx = dev->iamthif_msg_buf_index;
570 cb->read_time = jiffies;
db3ed431 571 if (dev->iamthif_ioctl && cb->cl == &dev->iamthif_cl) {
19838fb8 572 /* found the iamthif cb */
1a1aca42
TW
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 ");
19838fb8
TW
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 */
588int mei_amthif_irq_read(struct mei_device *dev, s32 *slots)
589{
c8c8d080 590 u32 msg_slots = mei_data2slots(sizeof(struct hbm_flow_control));
19838fb8 591
c8c8d080 592 if (*slots < msg_slots)
19838fb8 593 return -EMSGSIZE;
c8c8d080
TW
594
595 *slots -= msg_slots;
596
8120e720 597 if (mei_hbm_cl_flow_control_req(dev, &dev->iamthif_cl)) {
19838fb8
TW
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;
330dd7da 608 dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
19838fb8
TW
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 */
618void 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);
e773efc4 626 list_add_tail(&cb->list, &dev->amthif_rd_complete_list.list);
1a1aca42 627 dev_dbg(&dev->pdev->dev, "amthif read completed\n");
19838fb8
TW
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
1a1aca42 635 dev_dbg(&dev->pdev->dev, "completing amthif call back.\n");
19838fb8
TW
636 wake_up_interruptible(&dev->iamthif_cl.wait);
637}
638
a562d5c2
TW
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 */
652static 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 */
8120e720
TW
669 mei_hbm_cl_flow_control_req(dev,
670 &dev->iamthif_cl);
a562d5c2
TW
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 */
692static 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;
19838fb8 700
a562d5c2
TW
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*
393b148f 728* @dev: device structure
a562d5c2
TW
729* @file: pointer to file structure
730*
731* returns 0 on success, <0 on error
732*/
733int mei_amthif_release(struct mei_device *dev, struct file *file)
734{
22f96a0e
TW
735 if (dev->iamthif_open_count > 0)
736 dev->iamthif_open_count--;
a562d5c2
TW
737
738 if (dev->iamthif_file_object == file &&
739 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
740
1a1aca42 741 dev_dbg(&dev->pdev->dev, "amthif canceled iamthif state %d\n",
a562d5c2
TW
742 dev->iamthif_state);
743 dev->iamthif_canceled = true;
744 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
1a1aca42 745 dev_dbg(&dev->pdev->dev, "run next amthif iamthif cb\n");
a562d5c2
TW
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}